zram: describe device attrs in documentation
[cascardo/linux.git] / drivers / scsi / ipr.c
1 /*
2  * ipr.c -- driver for IBM Power Linux RAID adapters
3  *
4  * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
5  *
6  * Copyright (C) 2003, 2004 IBM Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 /*
25  * Notes:
26  *
27  * This driver is used to control the following SCSI adapters:
28  *
29  * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
30  *
31  * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
32  *              PCI-X Dual Channel Ultra 320 SCSI Adapter
33  *              PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
34  *              Embedded SCSI adapter on p615 and p655 systems
35  *
36  * Supported Hardware Features:
37  *      - Ultra 320 SCSI controller
38  *      - PCI-X host interface
39  *      - Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
40  *      - Non-Volatile Write Cache
41  *      - Supports attachment of non-RAID disks, tape, and optical devices
42  *      - RAID Levels 0, 5, 10
43  *      - Hot spare
44  *      - Background Parity Checking
45  *      - Background Data Scrubbing
46  *      - Ability to increase the capacity of an existing RAID 5 disk array
47  *              by adding disks
48  *
49  * Driver Features:
50  *      - Tagged command queuing
51  *      - Adapter microcode download
52  *      - PCI hot plug
53  *      - SCSI device hot plug
54  *
55  */
56
57 #include <linux/fs.h>
58 #include <linux/init.h>
59 #include <linux/types.h>
60 #include <linux/errno.h>
61 #include <linux/kernel.h>
62 #include <linux/slab.h>
63 #include <linux/vmalloc.h>
64 #include <linux/ioport.h>
65 #include <linux/delay.h>
66 #include <linux/pci.h>
67 #include <linux/wait.h>
68 #include <linux/spinlock.h>
69 #include <linux/sched.h>
70 #include <linux/interrupt.h>
71 #include <linux/blkdev.h>
72 #include <linux/firmware.h>
73 #include <linux/module.h>
74 #include <linux/moduleparam.h>
75 #include <linux/libata.h>
76 #include <linux/hdreg.h>
77 #include <linux/reboot.h>
78 #include <linux/stringify.h>
79 #include <asm/io.h>
80 #include <asm/irq.h>
81 #include <asm/processor.h>
82 #include <scsi/scsi.h>
83 #include <scsi/scsi_host.h>
84 #include <scsi/scsi_tcq.h>
85 #include <scsi/scsi_eh.h>
86 #include <scsi/scsi_cmnd.h>
87 #include "ipr.h"
88
89 /*
90  *   Global Data
91  */
92 static LIST_HEAD(ipr_ioa_head);
93 static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
94 static unsigned int ipr_max_speed = 1;
95 static int ipr_testmode = 0;
96 static unsigned int ipr_fastfail = 0;
97 static unsigned int ipr_transop_timeout = 0;
98 static unsigned int ipr_debug = 0;
99 static unsigned int ipr_max_devs = IPR_DEFAULT_SIS64_DEVS;
100 static unsigned int ipr_dual_ioa_raid = 1;
101 static unsigned int ipr_number_of_msix = 2;
102 static DEFINE_SPINLOCK(ipr_driver_lock);
103
104 /* This table describes the differences between DMA controller chips */
105 static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
106         { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
107                 .mailbox = 0x0042C,
108                 .max_cmds = 100,
109                 .cache_line_size = 0x20,
110                 .clear_isr = 1,
111                 .iopoll_weight = 0,
112                 {
113                         .set_interrupt_mask_reg = 0x0022C,
114                         .clr_interrupt_mask_reg = 0x00230,
115                         .clr_interrupt_mask_reg32 = 0x00230,
116                         .sense_interrupt_mask_reg = 0x0022C,
117                         .sense_interrupt_mask_reg32 = 0x0022C,
118                         .clr_interrupt_reg = 0x00228,
119                         .clr_interrupt_reg32 = 0x00228,
120                         .sense_interrupt_reg = 0x00224,
121                         .sense_interrupt_reg32 = 0x00224,
122                         .ioarrin_reg = 0x00404,
123                         .sense_uproc_interrupt_reg = 0x00214,
124                         .sense_uproc_interrupt_reg32 = 0x00214,
125                         .set_uproc_interrupt_reg = 0x00214,
126                         .set_uproc_interrupt_reg32 = 0x00214,
127                         .clr_uproc_interrupt_reg = 0x00218,
128                         .clr_uproc_interrupt_reg32 = 0x00218
129                 }
130         },
131         { /* Snipe and Scamp */
132                 .mailbox = 0x0052C,
133                 .max_cmds = 100,
134                 .cache_line_size = 0x20,
135                 .clear_isr = 1,
136                 .iopoll_weight = 0,
137                 {
138                         .set_interrupt_mask_reg = 0x00288,
139                         .clr_interrupt_mask_reg = 0x0028C,
140                         .clr_interrupt_mask_reg32 = 0x0028C,
141                         .sense_interrupt_mask_reg = 0x00288,
142                         .sense_interrupt_mask_reg32 = 0x00288,
143                         .clr_interrupt_reg = 0x00284,
144                         .clr_interrupt_reg32 = 0x00284,
145                         .sense_interrupt_reg = 0x00280,
146                         .sense_interrupt_reg32 = 0x00280,
147                         .ioarrin_reg = 0x00504,
148                         .sense_uproc_interrupt_reg = 0x00290,
149                         .sense_uproc_interrupt_reg32 = 0x00290,
150                         .set_uproc_interrupt_reg = 0x00290,
151                         .set_uproc_interrupt_reg32 = 0x00290,
152                         .clr_uproc_interrupt_reg = 0x00294,
153                         .clr_uproc_interrupt_reg32 = 0x00294
154                 }
155         },
156         { /* CRoC */
157                 .mailbox = 0x00044,
158                 .max_cmds = 1000,
159                 .cache_line_size = 0x20,
160                 .clear_isr = 0,
161                 .iopoll_weight = 64,
162                 {
163                         .set_interrupt_mask_reg = 0x00010,
164                         .clr_interrupt_mask_reg = 0x00018,
165                         .clr_interrupt_mask_reg32 = 0x0001C,
166                         .sense_interrupt_mask_reg = 0x00010,
167                         .sense_interrupt_mask_reg32 = 0x00014,
168                         .clr_interrupt_reg = 0x00008,
169                         .clr_interrupt_reg32 = 0x0000C,
170                         .sense_interrupt_reg = 0x00000,
171                         .sense_interrupt_reg32 = 0x00004,
172                         .ioarrin_reg = 0x00070,
173                         .sense_uproc_interrupt_reg = 0x00020,
174                         .sense_uproc_interrupt_reg32 = 0x00024,
175                         .set_uproc_interrupt_reg = 0x00020,
176                         .set_uproc_interrupt_reg32 = 0x00024,
177                         .clr_uproc_interrupt_reg = 0x00028,
178                         .clr_uproc_interrupt_reg32 = 0x0002C,
179                         .init_feedback_reg = 0x0005C,
180                         .dump_addr_reg = 0x00064,
181                         .dump_data_reg = 0x00068,
182                         .endian_swap_reg = 0x00084
183                 }
184         },
185 };
186
187 static const struct ipr_chip_t ipr_chip[] = {
188         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
189         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
190         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
191         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
192         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, IPR_USE_MSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
193         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[1] },
194         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[1] },
195         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] },
196         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] }
197 };
198
199 static int ipr_max_bus_speeds[] = {
200         IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
201 };
202
203 MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
204 MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
205 module_param_named(max_speed, ipr_max_speed, uint, 0);
206 MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
207 module_param_named(log_level, ipr_log_level, uint, 0);
208 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
209 module_param_named(testmode, ipr_testmode, int, 0);
210 MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
211 module_param_named(fastfail, ipr_fastfail, int, S_IRUGO | S_IWUSR);
212 MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
213 module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
214 MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
215 module_param_named(debug, ipr_debug, int, S_IRUGO | S_IWUSR);
216 MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
217 module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
218 MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
219 module_param_named(max_devs, ipr_max_devs, int, 0);
220 MODULE_PARM_DESC(max_devs, "Specify the maximum number of physical devices. "
221                  "[Default=" __stringify(IPR_DEFAULT_SIS64_DEVS) "]");
222 module_param_named(number_of_msix, ipr_number_of_msix, int, 0);
223 MODULE_PARM_DESC(number_of_msix, "Specify the number of MSIX interrupts to use on capable adapters (1 - 16).  (default:2)");
224 MODULE_LICENSE("GPL");
225 MODULE_VERSION(IPR_DRIVER_VERSION);
226
227 /*  A constant array of IOASCs/URCs/Error Messages */
228 static const
229 struct ipr_error_table_t ipr_error_table[] = {
230         {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
231         "8155: An unknown error was received"},
232         {0x00330000, 0, 0,
233         "Soft underlength error"},
234         {0x005A0000, 0, 0,
235         "Command to be cancelled not found"},
236         {0x00808000, 0, 0,
237         "Qualified success"},
238         {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
239         "FFFE: Soft device bus error recovered by the IOA"},
240         {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
241         "4101: Soft device bus fabric error"},
242         {0x01100100, 0, IPR_DEFAULT_LOG_LEVEL,
243         "FFFC: Logical block guard error recovered by the device"},
244         {0x01100300, 0, IPR_DEFAULT_LOG_LEVEL,
245         "FFFC: Logical block reference tag error recovered by the device"},
246         {0x01108300, 0, IPR_DEFAULT_LOG_LEVEL,
247         "4171: Recovered scatter list tag / sequence number error"},
248         {0x01109000, 0, IPR_DEFAULT_LOG_LEVEL,
249         "FF3D: Recovered logical block CRC error on IOA to Host transfer"},
250         {0x01109200, 0, IPR_DEFAULT_LOG_LEVEL,
251         "4171: Recovered logical block sequence number error on IOA to Host transfer"},
252         {0x0110A000, 0, IPR_DEFAULT_LOG_LEVEL,
253         "FFFD: Recovered logical block reference tag error detected by the IOA"},
254         {0x0110A100, 0, IPR_DEFAULT_LOG_LEVEL,
255         "FFFD: Logical block guard error recovered by the IOA"},
256         {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
257         "FFF9: Device sector reassign successful"},
258         {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
259         "FFF7: Media error recovered by device rewrite procedures"},
260         {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
261         "7001: IOA sector reassignment successful"},
262         {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
263         "FFF9: Soft media error. Sector reassignment recommended"},
264         {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
265         "FFF7: Media error recovered by IOA rewrite procedures"},
266         {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
267         "FF3D: Soft PCI bus error recovered by the IOA"},
268         {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
269         "FFF6: Device hardware error recovered by the IOA"},
270         {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
271         "FFF6: Device hardware error recovered by the device"},
272         {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
273         "FF3D: Soft IOA error recovered by the IOA"},
274         {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
275         "FFFA: Undefined device response recovered by the IOA"},
276         {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
277         "FFF6: Device bus error, message or command phase"},
278         {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
279         "FFFE: Task Management Function failed"},
280         {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
281         "FFF6: Failure prediction threshold exceeded"},
282         {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
283         "8009: Impending cache battery pack failure"},
284         {0x02040100, 0, 0,
285         "Logical Unit in process of becoming ready"},
286         {0x02040200, 0, 0,
287         "Initializing command required"},
288         {0x02040400, 0, 0,
289         "34FF: Disk device format in progress"},
290         {0x02040C00, 0, 0,
291         "Logical unit not accessible, target port in unavailable state"},
292         {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
293         "9070: IOA requested reset"},
294         {0x023F0000, 0, 0,
295         "Synchronization required"},
296         {0x02408500, 0, 0,
297         "IOA microcode download required"},
298         {0x02408600, 0, 0,
299         "Device bus connection is prohibited by host"},
300         {0x024E0000, 0, 0,
301         "No ready, IOA shutdown"},
302         {0x025A0000, 0, 0,
303         "Not ready, IOA has been shutdown"},
304         {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
305         "3020: Storage subsystem configuration error"},
306         {0x03110B00, 0, 0,
307         "FFF5: Medium error, data unreadable, recommend reassign"},
308         {0x03110C00, 0, 0,
309         "7000: Medium error, data unreadable, do not reassign"},
310         {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
311         "FFF3: Disk media format bad"},
312         {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
313         "3002: Addressed device failed to respond to selection"},
314         {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
315         "3100: Device bus error"},
316         {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
317         "3109: IOA timed out a device command"},
318         {0x04088000, 0, 0,
319         "3120: SCSI bus is not operational"},
320         {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
321         "4100: Hard device bus fabric error"},
322         {0x04100100, 0, IPR_DEFAULT_LOG_LEVEL,
323         "310C: Logical block guard error detected by the device"},
324         {0x04100300, 0, IPR_DEFAULT_LOG_LEVEL,
325         "310C: Logical block reference tag error detected by the device"},
326         {0x04108300, 1, IPR_DEFAULT_LOG_LEVEL,
327         "4170: Scatter list tag / sequence number error"},
328         {0x04109000, 1, IPR_DEFAULT_LOG_LEVEL,
329         "8150: Logical block CRC error on IOA to Host transfer"},
330         {0x04109200, 1, IPR_DEFAULT_LOG_LEVEL,
331         "4170: Logical block sequence number error on IOA to Host transfer"},
332         {0x0410A000, 0, IPR_DEFAULT_LOG_LEVEL,
333         "310D: Logical block reference tag error detected by the IOA"},
334         {0x0410A100, 0, IPR_DEFAULT_LOG_LEVEL,
335         "310D: Logical block guard error detected by the IOA"},
336         {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
337         "9000: IOA reserved area data check"},
338         {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
339         "9001: IOA reserved area invalid data pattern"},
340         {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
341         "9002: IOA reserved area LRC error"},
342         {0x04118300, 1, IPR_DEFAULT_LOG_LEVEL,
343         "Hardware Error, IOA metadata access error"},
344         {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
345         "102E: Out of alternate sectors for disk storage"},
346         {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
347         "FFF4: Data transfer underlength error"},
348         {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
349         "FFF4: Data transfer overlength error"},
350         {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
351         "3400: Logical unit failure"},
352         {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
353         "FFF4: Device microcode is corrupt"},
354         {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
355         "8150: PCI bus error"},
356         {0x04430000, 1, 0,
357         "Unsupported device bus message received"},
358         {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
359         "FFF4: Disk device problem"},
360         {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
361         "8150: Permanent IOA failure"},
362         {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
363         "3010: Disk device returned wrong response to IOA"},
364         {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
365         "8151: IOA microcode error"},
366         {0x04448500, 0, 0,
367         "Device bus status error"},
368         {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
369         "8157: IOA error requiring IOA reset to recover"},
370         {0x04448700, 0, 0,
371         "ATA device status error"},
372         {0x04490000, 0, 0,
373         "Message reject received from the device"},
374         {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
375         "8008: A permanent cache battery pack failure occurred"},
376         {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
377         "9090: Disk unit has been modified after the last known status"},
378         {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
379         "9081: IOA detected device error"},
380         {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
381         "9082: IOA detected device error"},
382         {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
383         "3110: Device bus error, message or command phase"},
384         {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
385         "3110: SAS Command / Task Management Function failed"},
386         {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
387         "9091: Incorrect hardware configuration change has been detected"},
388         {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
389         "9073: Invalid multi-adapter configuration"},
390         {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
391         "4010: Incorrect connection between cascaded expanders"},
392         {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
393         "4020: Connections exceed IOA design limits"},
394         {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
395         "4030: Incorrect multipath connection"},
396         {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
397         "4110: Unsupported enclosure function"},
398         {0x04679800, 0, IPR_DEFAULT_LOG_LEVEL,
399         "4120: SAS cable VPD cannot be read"},
400         {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
401         "FFF4: Command to logical unit failed"},
402         {0x05240000, 1, 0,
403         "Illegal request, invalid request type or request packet"},
404         {0x05250000, 0, 0,
405         "Illegal request, invalid resource handle"},
406         {0x05258000, 0, 0,
407         "Illegal request, commands not allowed to this device"},
408         {0x05258100, 0, 0,
409         "Illegal request, command not allowed to a secondary adapter"},
410         {0x05258200, 0, 0,
411         "Illegal request, command not allowed to a non-optimized resource"},
412         {0x05260000, 0, 0,
413         "Illegal request, invalid field in parameter list"},
414         {0x05260100, 0, 0,
415         "Illegal request, parameter not supported"},
416         {0x05260200, 0, 0,
417         "Illegal request, parameter value invalid"},
418         {0x052C0000, 0, 0,
419         "Illegal request, command sequence error"},
420         {0x052C8000, 1, 0,
421         "Illegal request, dual adapter support not enabled"},
422         {0x052C8100, 1, 0,
423         "Illegal request, another cable connector was physically disabled"},
424         {0x054E8000, 1, 0,
425         "Illegal request, inconsistent group id/group count"},
426         {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
427         "9031: Array protection temporarily suspended, protection resuming"},
428         {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
429         "9040: Array protection temporarily suspended, protection resuming"},
430         {0x060B0100, 0, IPR_DEFAULT_LOG_LEVEL,
431         "4080: IOA exceeded maximum operating temperature"},
432         {0x060B8000, 0, IPR_DEFAULT_LOG_LEVEL,
433         "4085: Service required"},
434         {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
435         "3140: Device bus not ready to ready transition"},
436         {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
437         "FFFB: SCSI bus was reset"},
438         {0x06290500, 0, 0,
439         "FFFE: SCSI bus transition to single ended"},
440         {0x06290600, 0, 0,
441         "FFFE: SCSI bus transition to LVD"},
442         {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
443         "FFFB: SCSI bus was reset by another initiator"},
444         {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
445         "3029: A device replacement has occurred"},
446         {0x063F8300, 0, IPR_DEFAULT_LOG_LEVEL,
447         "4102: Device bus fabric performance degradation"},
448         {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
449         "9051: IOA cache data exists for a missing or failed device"},
450         {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
451         "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
452         {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
453         "9025: Disk unit is not supported at its physical location"},
454         {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
455         "3020: IOA detected a SCSI bus configuration error"},
456         {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
457         "3150: SCSI bus configuration error"},
458         {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
459         "9074: Asymmetric advanced function disk configuration"},
460         {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
461         "4040: Incomplete multipath connection between IOA and enclosure"},
462         {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
463         "4041: Incomplete multipath connection between enclosure and device"},
464         {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
465         "9075: Incomplete multipath connection between IOA and remote IOA"},
466         {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
467         "9076: Configuration error, missing remote IOA"},
468         {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
469         "4050: Enclosure does not support a required multipath function"},
470         {0x06679800, 0, IPR_DEFAULT_LOG_LEVEL,
471         "4121: Configuration error, required cable is missing"},
472         {0x06679900, 0, IPR_DEFAULT_LOG_LEVEL,
473         "4122: Cable is not plugged into the correct location on remote IOA"},
474         {0x06679A00, 0, IPR_DEFAULT_LOG_LEVEL,
475         "4123: Configuration error, invalid cable vital product data"},
476         {0x06679B00, 0, IPR_DEFAULT_LOG_LEVEL,
477         "4124: Configuration error, both cable ends are plugged into the same IOA"},
478         {0x06690000, 0, IPR_DEFAULT_LOG_LEVEL,
479         "4070: Logically bad block written on device"},
480         {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
481         "9041: Array protection temporarily suspended"},
482         {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
483         "9042: Corrupt array parity detected on specified device"},
484         {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
485         "9030: Array no longer protected due to missing or failed disk unit"},
486         {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
487         "9071: Link operational transition"},
488         {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
489         "9072: Link not operational transition"},
490         {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
491         "9032: Array exposed but still protected"},
492         {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
493         "70DD: Device forced failed by disrupt device command"},
494         {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
495         "4061: Multipath redundancy level got better"},
496         {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
497         "4060: Multipath redundancy level got worse"},
498         {0x07270000, 0, 0,
499         "Failure due to other device"},
500         {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
501         "9008: IOA does not support functions expected by devices"},
502         {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
503         "9010: Cache data associated with attached devices cannot be found"},
504         {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
505         "9011: Cache data belongs to devices other than those attached"},
506         {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
507         "9020: Array missing 2 or more devices with only 1 device present"},
508         {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
509         "9021: Array missing 2 or more devices with 2 or more devices present"},
510         {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
511         "9022: Exposed array is missing a required device"},
512         {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
513         "9023: Array member(s) not at required physical locations"},
514         {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
515         "9024: Array not functional due to present hardware configuration"},
516         {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
517         "9026: Array not functional due to present hardware configuration"},
518         {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
519         "9027: Array is missing a device and parity is out of sync"},
520         {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
521         "9028: Maximum number of arrays already exist"},
522         {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
523         "9050: Required cache data cannot be located for a disk unit"},
524         {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
525         "9052: Cache data exists for a device that has been modified"},
526         {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
527         "9054: IOA resources not available due to previous problems"},
528         {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
529         "9092: Disk unit requires initialization before use"},
530         {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
531         "9029: Incorrect hardware configuration change has been detected"},
532         {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
533         "9060: One or more disk pairs are missing from an array"},
534         {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
535         "9061: One or more disks are missing from an array"},
536         {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
537         "9062: One or more disks are missing from an array"},
538         {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
539         "9063: Maximum number of functional arrays has been exceeded"},
540         {0x07279A00, 0, 0,
541         "Data protect, other volume set problem"},
542         {0x0B260000, 0, 0,
543         "Aborted command, invalid descriptor"},
544         {0x0B3F9000, 0, 0,
545         "Target operating conditions have changed, dual adapter takeover"},
546         {0x0B530200, 0, 0,
547         "Aborted command, medium removal prevented"},
548         {0x0B5A0000, 0, 0,
549         "Command terminated by host"},
550         {0x0B5B8000, 0, 0,
551         "Aborted command, command terminated by host"}
552 };
553
554 static const struct ipr_ses_table_entry ipr_ses_table[] = {
555         { "2104-DL1        ", "XXXXXXXXXXXXXXXX", 80 },
556         { "2104-TL1        ", "XXXXXXXXXXXXXXXX", 80 },
557         { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
558         { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
559         { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
560         { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
561         { "2104-DU3        ", "XXXXXXXXXXXXXXXX", 160 },
562         { "2104-TU3        ", "XXXXXXXXXXXXXXXX", 160 },
563         { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
564         { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
565         { "St  V1S2        ", "XXXXXXXXXXXXXXXX", 160 },
566         { "HSBPD4M  PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
567         { "VSBPD1H   U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
568 };
569
570 /*
571  *  Function Prototypes
572  */
573 static int ipr_reset_alert(struct ipr_cmnd *);
574 static void ipr_process_ccn(struct ipr_cmnd *);
575 static void ipr_process_error(struct ipr_cmnd *);
576 static void ipr_reset_ioa_job(struct ipr_cmnd *);
577 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
578                                    enum ipr_shutdown_type);
579
580 #ifdef CONFIG_SCSI_IPR_TRACE
581 /**
582  * ipr_trc_hook - Add a trace entry to the driver trace
583  * @ipr_cmd:    ipr command struct
584  * @type:               trace type
585  * @add_data:   additional data
586  *
587  * Return value:
588  *      none
589  **/
590 static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
591                          u8 type, u32 add_data)
592 {
593         struct ipr_trace_entry *trace_entry;
594         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
595
596         trace_entry = &ioa_cfg->trace[atomic_add_return
597                         (1, &ioa_cfg->trace_index)%IPR_NUM_TRACE_ENTRIES];
598         trace_entry->time = jiffies;
599         trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
600         trace_entry->type = type;
601         if (ipr_cmd->ioa_cfg->sis64)
602                 trace_entry->ata_op_code = ipr_cmd->i.ata_ioadl.regs.command;
603         else
604                 trace_entry->ata_op_code = ipr_cmd->ioarcb.u.add_data.u.regs.command;
605         trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
606         trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
607         trace_entry->u.add_data = add_data;
608         wmb();
609 }
610 #else
611 #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while (0)
612 #endif
613
614 /**
615  * ipr_lock_and_done - Acquire lock and complete command
616  * @ipr_cmd:    ipr command struct
617  *
618  * Return value:
619  *      none
620  **/
621 static void ipr_lock_and_done(struct ipr_cmnd *ipr_cmd)
622 {
623         unsigned long lock_flags;
624         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
625
626         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
627         ipr_cmd->done(ipr_cmd);
628         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
629 }
630
631 /**
632  * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
633  * @ipr_cmd:    ipr command struct
634  *
635  * Return value:
636  *      none
637  **/
638 static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
639 {
640         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
641         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
642         struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
643         dma_addr_t dma_addr = ipr_cmd->dma_addr;
644         int hrrq_id;
645
646         hrrq_id = ioarcb->cmd_pkt.hrrq_id;
647         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
648         ioarcb->cmd_pkt.hrrq_id = hrrq_id;
649         ioarcb->data_transfer_length = 0;
650         ioarcb->read_data_transfer_length = 0;
651         ioarcb->ioadl_len = 0;
652         ioarcb->read_ioadl_len = 0;
653
654         if (ipr_cmd->ioa_cfg->sis64) {
655                 ioarcb->u.sis64_addr_data.data_ioadl_addr =
656                         cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
657                 ioasa64->u.gata.status = 0;
658         } else {
659                 ioarcb->write_ioadl_addr =
660                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
661                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
662                 ioasa->u.gata.status = 0;
663         }
664
665         ioasa->hdr.ioasc = 0;
666         ioasa->hdr.residual_data_len = 0;
667         ipr_cmd->scsi_cmd = NULL;
668         ipr_cmd->qc = NULL;
669         ipr_cmd->sense_buffer[0] = 0;
670         ipr_cmd->dma_use_sg = 0;
671 }
672
673 /**
674  * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
675  * @ipr_cmd:    ipr command struct
676  *
677  * Return value:
678  *      none
679  **/
680 static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd,
681                               void (*fast_done) (struct ipr_cmnd *))
682 {
683         ipr_reinit_ipr_cmnd(ipr_cmd);
684         ipr_cmd->u.scratch = 0;
685         ipr_cmd->sibling = NULL;
686         ipr_cmd->eh_comp = NULL;
687         ipr_cmd->fast_done = fast_done;
688         init_timer(&ipr_cmd->timer);
689 }
690
691 /**
692  * __ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
693  * @ioa_cfg:    ioa config struct
694  *
695  * Return value:
696  *      pointer to ipr command struct
697  **/
698 static
699 struct ipr_cmnd *__ipr_get_free_ipr_cmnd(struct ipr_hrr_queue *hrrq)
700 {
701         struct ipr_cmnd *ipr_cmd = NULL;
702
703         if (likely(!list_empty(&hrrq->hrrq_free_q))) {
704                 ipr_cmd = list_entry(hrrq->hrrq_free_q.next,
705                         struct ipr_cmnd, queue);
706                 list_del(&ipr_cmd->queue);
707         }
708
709
710         return ipr_cmd;
711 }
712
713 /**
714  * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block and initialize it
715  * @ioa_cfg:    ioa config struct
716  *
717  * Return value:
718  *      pointer to ipr command struct
719  **/
720 static
721 struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
722 {
723         struct ipr_cmnd *ipr_cmd =
724                 __ipr_get_free_ipr_cmnd(&ioa_cfg->hrrq[IPR_INIT_HRRQ]);
725         ipr_init_ipr_cmnd(ipr_cmd, ipr_lock_and_done);
726         return ipr_cmd;
727 }
728
729 /**
730  * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
731  * @ioa_cfg:    ioa config struct
732  * @clr_ints:     interrupts to clear
733  *
734  * This function masks all interrupts on the adapter, then clears the
735  * interrupts specified in the mask
736  *
737  * Return value:
738  *      none
739  **/
740 static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
741                                           u32 clr_ints)
742 {
743         volatile u32 int_reg;
744         int i;
745
746         /* Stop new interrupts */
747         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
748                 spin_lock(&ioa_cfg->hrrq[i]._lock);
749                 ioa_cfg->hrrq[i].allow_interrupts = 0;
750                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
751         }
752         wmb();
753
754         /* Set interrupt mask to stop all new interrupts */
755         if (ioa_cfg->sis64)
756                 writeq(~0, ioa_cfg->regs.set_interrupt_mask_reg);
757         else
758                 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
759
760         /* Clear any pending interrupts */
761         if (ioa_cfg->sis64)
762                 writel(~0, ioa_cfg->regs.clr_interrupt_reg);
763         writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg32);
764         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
765 }
766
767 /**
768  * ipr_save_pcix_cmd_reg - Save PCI-X command register
769  * @ioa_cfg:    ioa config struct
770  *
771  * Return value:
772  *      0 on success / -EIO on failure
773  **/
774 static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
775 {
776         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
777
778         if (pcix_cmd_reg == 0)
779                 return 0;
780
781         if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
782                                  &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
783                 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
784                 return -EIO;
785         }
786
787         ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
788         return 0;
789 }
790
791 /**
792  * ipr_set_pcix_cmd_reg - Setup PCI-X command register
793  * @ioa_cfg:    ioa config struct
794  *
795  * Return value:
796  *      0 on success / -EIO on failure
797  **/
798 static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
799 {
800         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
801
802         if (pcix_cmd_reg) {
803                 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
804                                           ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
805                         dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
806                         return -EIO;
807                 }
808         }
809
810         return 0;
811 }
812
813 /**
814  * ipr_sata_eh_done - done function for aborted SATA commands
815  * @ipr_cmd:    ipr command struct
816  *
817  * This function is invoked for ops generated to SATA
818  * devices which are being aborted.
819  *
820  * Return value:
821  *      none
822  **/
823 static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
824 {
825         struct ata_queued_cmd *qc = ipr_cmd->qc;
826         struct ipr_sata_port *sata_port = qc->ap->private_data;
827
828         qc->err_mask |= AC_ERR_OTHER;
829         sata_port->ioasa.status |= ATA_BUSY;
830         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
831         ata_qc_complete(qc);
832 }
833
834 /**
835  * ipr_scsi_eh_done - mid-layer done function for aborted ops
836  * @ipr_cmd:    ipr command struct
837  *
838  * This function is invoked by the interrupt handler for
839  * ops generated by the SCSI mid-layer which are being aborted.
840  *
841  * Return value:
842  *      none
843  **/
844 static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
845 {
846         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
847
848         scsi_cmd->result |= (DID_ERROR << 16);
849
850         scsi_dma_unmap(ipr_cmd->scsi_cmd);
851         scsi_cmd->scsi_done(scsi_cmd);
852         if (ipr_cmd->eh_comp)
853                 complete(ipr_cmd->eh_comp);
854         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
855 }
856
857 /**
858  * ipr_fail_all_ops - Fails all outstanding ops.
859  * @ioa_cfg:    ioa config struct
860  *
861  * This function fails all outstanding ops.
862  *
863  * Return value:
864  *      none
865  **/
866 static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
867 {
868         struct ipr_cmnd *ipr_cmd, *temp;
869         struct ipr_hrr_queue *hrrq;
870
871         ENTER;
872         for_each_hrrq(hrrq, ioa_cfg) {
873                 spin_lock(&hrrq->_lock);
874                 list_for_each_entry_safe(ipr_cmd,
875                                         temp, &hrrq->hrrq_pending_q, queue) {
876                         list_del(&ipr_cmd->queue);
877
878                         ipr_cmd->s.ioasa.hdr.ioasc =
879                                 cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
880                         ipr_cmd->s.ioasa.hdr.ilid =
881                                 cpu_to_be32(IPR_DRIVER_ILID);
882
883                         if (ipr_cmd->scsi_cmd)
884                                 ipr_cmd->done = ipr_scsi_eh_done;
885                         else if (ipr_cmd->qc)
886                                 ipr_cmd->done = ipr_sata_eh_done;
887
888                         ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH,
889                                      IPR_IOASC_IOA_WAS_RESET);
890                         del_timer(&ipr_cmd->timer);
891                         ipr_cmd->done(ipr_cmd);
892                 }
893                 spin_unlock(&hrrq->_lock);
894         }
895         LEAVE;
896 }
897
898 /**
899  * ipr_send_command -  Send driver initiated requests.
900  * @ipr_cmd:            ipr command struct
901  *
902  * This function sends a command to the adapter using the correct write call.
903  * In the case of sis64, calculate the ioarcb size required. Then or in the
904  * appropriate bits.
905  *
906  * Return value:
907  *      none
908  **/
909 static void ipr_send_command(struct ipr_cmnd *ipr_cmd)
910 {
911         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
912         dma_addr_t send_dma_addr = ipr_cmd->dma_addr;
913
914         if (ioa_cfg->sis64) {
915                 /* The default size is 256 bytes */
916                 send_dma_addr |= 0x1;
917
918                 /* If the number of ioadls * size of ioadl > 128 bytes,
919                    then use a 512 byte ioarcb */
920                 if (ipr_cmd->dma_use_sg * sizeof(struct ipr_ioadl64_desc) > 128 )
921                         send_dma_addr |= 0x4;
922                 writeq(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
923         } else
924                 writel(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
925 }
926
927 /**
928  * ipr_do_req -  Send driver initiated requests.
929  * @ipr_cmd:            ipr command struct
930  * @done:                       done function
931  * @timeout_func:       timeout function
932  * @timeout:            timeout value
933  *
934  * This function sends the specified command to the adapter with the
935  * timeout given. The done function is invoked on command completion.
936  *
937  * Return value:
938  *      none
939  **/
940 static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
941                        void (*done) (struct ipr_cmnd *),
942                        void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
943 {
944         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
945
946         ipr_cmd->done = done;
947
948         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
949         ipr_cmd->timer.expires = jiffies + timeout;
950         ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
951
952         add_timer(&ipr_cmd->timer);
953
954         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
955
956         ipr_send_command(ipr_cmd);
957 }
958
959 /**
960  * ipr_internal_cmd_done - Op done function for an internally generated op.
961  * @ipr_cmd:    ipr command struct
962  *
963  * This function is the op done function for an internally generated,
964  * blocking op. It simply wakes the sleeping thread.
965  *
966  * Return value:
967  *      none
968  **/
969 static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
970 {
971         if (ipr_cmd->sibling)
972                 ipr_cmd->sibling = NULL;
973         else
974                 complete(&ipr_cmd->completion);
975 }
976
977 /**
978  * ipr_init_ioadl - initialize the ioadl for the correct SIS type
979  * @ipr_cmd:    ipr command struct
980  * @dma_addr:   dma address
981  * @len:        transfer length
982  * @flags:      ioadl flag value
983  *
984  * This function initializes an ioadl in the case where there is only a single
985  * descriptor.
986  *
987  * Return value:
988  *      nothing
989  **/
990 static void ipr_init_ioadl(struct ipr_cmnd *ipr_cmd, dma_addr_t dma_addr,
991                            u32 len, int flags)
992 {
993         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
994         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
995
996         ipr_cmd->dma_use_sg = 1;
997
998         if (ipr_cmd->ioa_cfg->sis64) {
999                 ioadl64->flags = cpu_to_be32(flags);
1000                 ioadl64->data_len = cpu_to_be32(len);
1001                 ioadl64->address = cpu_to_be64(dma_addr);
1002
1003                 ipr_cmd->ioarcb.ioadl_len =
1004                         cpu_to_be32(sizeof(struct ipr_ioadl64_desc));
1005                 ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
1006         } else {
1007                 ioadl->flags_and_data_len = cpu_to_be32(flags | len);
1008                 ioadl->address = cpu_to_be32(dma_addr);
1009
1010                 if (flags == IPR_IOADL_FLAGS_READ_LAST) {
1011                         ipr_cmd->ioarcb.read_ioadl_len =
1012                                 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
1013                         ipr_cmd->ioarcb.read_data_transfer_length = cpu_to_be32(len);
1014                 } else {
1015                         ipr_cmd->ioarcb.ioadl_len =
1016                                 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
1017                         ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
1018                 }
1019         }
1020 }
1021
1022 /**
1023  * ipr_send_blocking_cmd - Send command and sleep on its completion.
1024  * @ipr_cmd:    ipr command struct
1025  * @timeout_func:       function to invoke if command times out
1026  * @timeout:    timeout
1027  *
1028  * Return value:
1029  *      none
1030  **/
1031 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
1032                                   void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
1033                                   u32 timeout)
1034 {
1035         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1036
1037         init_completion(&ipr_cmd->completion);
1038         ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
1039
1040         spin_unlock_irq(ioa_cfg->host->host_lock);
1041         wait_for_completion(&ipr_cmd->completion);
1042         spin_lock_irq(ioa_cfg->host->host_lock);
1043 }
1044
1045 static int ipr_get_hrrq_index(struct ipr_ioa_cfg *ioa_cfg)
1046 {
1047         if (ioa_cfg->hrrq_num == 1)
1048                 return 0;
1049         else
1050                 return (atomic_add_return(1, &ioa_cfg->hrrq_index) % (ioa_cfg->hrrq_num - 1)) + 1;
1051 }
1052
1053 /**
1054  * ipr_send_hcam - Send an HCAM to the adapter.
1055  * @ioa_cfg:    ioa config struct
1056  * @type:               HCAM type
1057  * @hostrcb:    hostrcb struct
1058  *
1059  * This function will send a Host Controlled Async command to the adapter.
1060  * If HCAMs are currently not allowed to be issued to the adapter, it will
1061  * place the hostrcb on the free queue.
1062  *
1063  * Return value:
1064  *      none
1065  **/
1066 static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
1067                           struct ipr_hostrcb *hostrcb)
1068 {
1069         struct ipr_cmnd *ipr_cmd;
1070         struct ipr_ioarcb *ioarcb;
1071
1072         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds) {
1073                 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
1074                 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
1075                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
1076
1077                 ipr_cmd->u.hostrcb = hostrcb;
1078                 ioarcb = &ipr_cmd->ioarcb;
1079
1080                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
1081                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
1082                 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
1083                 ioarcb->cmd_pkt.cdb[1] = type;
1084                 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
1085                 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
1086
1087                 ipr_init_ioadl(ipr_cmd, hostrcb->hostrcb_dma,
1088                                sizeof(hostrcb->hcam), IPR_IOADL_FLAGS_READ_LAST);
1089
1090                 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
1091                         ipr_cmd->done = ipr_process_ccn;
1092                 else
1093                         ipr_cmd->done = ipr_process_error;
1094
1095                 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
1096
1097                 ipr_send_command(ipr_cmd);
1098         } else {
1099                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
1100         }
1101 }
1102
1103 /**
1104  * ipr_update_ata_class - Update the ata class in the resource entry
1105  * @res:        resource entry struct
1106  * @proto:      cfgte device bus protocol value
1107  *
1108  * Return value:
1109  *      none
1110  **/
1111 static void ipr_update_ata_class(struct ipr_resource_entry *res, unsigned int proto)
1112 {
1113         switch (proto) {
1114         case IPR_PROTO_SATA:
1115         case IPR_PROTO_SAS_STP:
1116                 res->ata_class = ATA_DEV_ATA;
1117                 break;
1118         case IPR_PROTO_SATA_ATAPI:
1119         case IPR_PROTO_SAS_STP_ATAPI:
1120                 res->ata_class = ATA_DEV_ATAPI;
1121                 break;
1122         default:
1123                 res->ata_class = ATA_DEV_UNKNOWN;
1124                 break;
1125         };
1126 }
1127
1128 /**
1129  * ipr_init_res_entry - Initialize a resource entry struct.
1130  * @res:        resource entry struct
1131  * @cfgtew:     config table entry wrapper struct
1132  *
1133  * Return value:
1134  *      none
1135  **/
1136 static void ipr_init_res_entry(struct ipr_resource_entry *res,
1137                                struct ipr_config_table_entry_wrapper *cfgtew)
1138 {
1139         int found = 0;
1140         unsigned int proto;
1141         struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
1142         struct ipr_resource_entry *gscsi_res = NULL;
1143
1144         res->needs_sync_complete = 0;
1145         res->in_erp = 0;
1146         res->add_to_ml = 0;
1147         res->del_from_ml = 0;
1148         res->resetting_device = 0;
1149         res->reset_occurred = 0;
1150         res->sdev = NULL;
1151         res->sata_port = NULL;
1152
1153         if (ioa_cfg->sis64) {
1154                 proto = cfgtew->u.cfgte64->proto;
1155                 res->res_flags = cfgtew->u.cfgte64->res_flags;
1156                 res->qmodel = IPR_QUEUEING_MODEL64(res);
1157                 res->type = cfgtew->u.cfgte64->res_type;
1158
1159                 memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1160                         sizeof(res->res_path));
1161
1162                 res->bus = 0;
1163                 memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1164                         sizeof(res->dev_lun.scsi_lun));
1165                 res->lun = scsilun_to_int(&res->dev_lun);
1166
1167                 if (res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1168                         list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue) {
1169                                 if (gscsi_res->dev_id == cfgtew->u.cfgte64->dev_id) {
1170                                         found = 1;
1171                                         res->target = gscsi_res->target;
1172                                         break;
1173                                 }
1174                         }
1175                         if (!found) {
1176                                 res->target = find_first_zero_bit(ioa_cfg->target_ids,
1177                                                                   ioa_cfg->max_devs_supported);
1178                                 set_bit(res->target, ioa_cfg->target_ids);
1179                         }
1180                 } else if (res->type == IPR_RES_TYPE_IOAFP) {
1181                         res->bus = IPR_IOAFP_VIRTUAL_BUS;
1182                         res->target = 0;
1183                 } else if (res->type == IPR_RES_TYPE_ARRAY) {
1184                         res->bus = IPR_ARRAY_VIRTUAL_BUS;
1185                         res->target = find_first_zero_bit(ioa_cfg->array_ids,
1186                                                           ioa_cfg->max_devs_supported);
1187                         set_bit(res->target, ioa_cfg->array_ids);
1188                 } else if (res->type == IPR_RES_TYPE_VOLUME_SET) {
1189                         res->bus = IPR_VSET_VIRTUAL_BUS;
1190                         res->target = find_first_zero_bit(ioa_cfg->vset_ids,
1191                                                           ioa_cfg->max_devs_supported);
1192                         set_bit(res->target, ioa_cfg->vset_ids);
1193                 } else {
1194                         res->target = find_first_zero_bit(ioa_cfg->target_ids,
1195                                                           ioa_cfg->max_devs_supported);
1196                         set_bit(res->target, ioa_cfg->target_ids);
1197                 }
1198         } else {
1199                 proto = cfgtew->u.cfgte->proto;
1200                 res->qmodel = IPR_QUEUEING_MODEL(res);
1201                 res->flags = cfgtew->u.cfgte->flags;
1202                 if (res->flags & IPR_IS_IOA_RESOURCE)
1203                         res->type = IPR_RES_TYPE_IOAFP;
1204                 else
1205                         res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1206
1207                 res->bus = cfgtew->u.cfgte->res_addr.bus;
1208                 res->target = cfgtew->u.cfgte->res_addr.target;
1209                 res->lun = cfgtew->u.cfgte->res_addr.lun;
1210                 res->lun_wwn = get_unaligned_be64(cfgtew->u.cfgte->lun_wwn);
1211         }
1212
1213         ipr_update_ata_class(res, proto);
1214 }
1215
1216 /**
1217  * ipr_is_same_device - Determine if two devices are the same.
1218  * @res:        resource entry struct
1219  * @cfgtew:     config table entry wrapper struct
1220  *
1221  * Return value:
1222  *      1 if the devices are the same / 0 otherwise
1223  **/
1224 static int ipr_is_same_device(struct ipr_resource_entry *res,
1225                               struct ipr_config_table_entry_wrapper *cfgtew)
1226 {
1227         if (res->ioa_cfg->sis64) {
1228                 if (!memcmp(&res->dev_id, &cfgtew->u.cfgte64->dev_id,
1229                                         sizeof(cfgtew->u.cfgte64->dev_id)) &&
1230                         !memcmp(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1231                                         sizeof(cfgtew->u.cfgte64->lun))) {
1232                         return 1;
1233                 }
1234         } else {
1235                 if (res->bus == cfgtew->u.cfgte->res_addr.bus &&
1236                     res->target == cfgtew->u.cfgte->res_addr.target &&
1237                     res->lun == cfgtew->u.cfgte->res_addr.lun)
1238                         return 1;
1239         }
1240
1241         return 0;
1242 }
1243
1244 /**
1245  * __ipr_format_res_path - Format the resource path for printing.
1246  * @res_path:   resource path
1247  * @buf:        buffer
1248  * @len:        length of buffer provided
1249  *
1250  * Return value:
1251  *      pointer to buffer
1252  **/
1253 static char *__ipr_format_res_path(u8 *res_path, char *buffer, int len)
1254 {
1255         int i;
1256         char *p = buffer;
1257
1258         *p = '\0';
1259         p += snprintf(p, buffer + len - p, "%02X", res_path[0]);
1260         for (i = 1; res_path[i] != 0xff && ((i * 3) < len); i++)
1261                 p += snprintf(p, buffer + len - p, "-%02X", res_path[i]);
1262
1263         return buffer;
1264 }
1265
1266 /**
1267  * ipr_format_res_path - Format the resource path for printing.
1268  * @ioa_cfg:    ioa config struct
1269  * @res_path:   resource path
1270  * @buf:        buffer
1271  * @len:        length of buffer provided
1272  *
1273  * Return value:
1274  *      pointer to buffer
1275  **/
1276 static char *ipr_format_res_path(struct ipr_ioa_cfg *ioa_cfg,
1277                                  u8 *res_path, char *buffer, int len)
1278 {
1279         char *p = buffer;
1280
1281         *p = '\0';
1282         p += snprintf(p, buffer + len - p, "%d/", ioa_cfg->host->host_no);
1283         __ipr_format_res_path(res_path, p, len - (buffer - p));
1284         return buffer;
1285 }
1286
1287 /**
1288  * ipr_update_res_entry - Update the resource entry.
1289  * @res:        resource entry struct
1290  * @cfgtew:     config table entry wrapper struct
1291  *
1292  * Return value:
1293  *      none
1294  **/
1295 static void ipr_update_res_entry(struct ipr_resource_entry *res,
1296                                  struct ipr_config_table_entry_wrapper *cfgtew)
1297 {
1298         char buffer[IPR_MAX_RES_PATH_LENGTH];
1299         unsigned int proto;
1300         int new_path = 0;
1301
1302         if (res->ioa_cfg->sis64) {
1303                 res->flags = cfgtew->u.cfgte64->flags;
1304                 res->res_flags = cfgtew->u.cfgte64->res_flags;
1305                 res->type = cfgtew->u.cfgte64->res_type;
1306
1307                 memcpy(&res->std_inq_data, &cfgtew->u.cfgte64->std_inq_data,
1308                         sizeof(struct ipr_std_inq_data));
1309
1310                 res->qmodel = IPR_QUEUEING_MODEL64(res);
1311                 proto = cfgtew->u.cfgte64->proto;
1312                 res->res_handle = cfgtew->u.cfgte64->res_handle;
1313                 res->dev_id = cfgtew->u.cfgte64->dev_id;
1314
1315                 memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1316                         sizeof(res->dev_lun.scsi_lun));
1317
1318                 if (memcmp(res->res_path, &cfgtew->u.cfgte64->res_path,
1319                                         sizeof(res->res_path))) {
1320                         memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1321                                 sizeof(res->res_path));
1322                         new_path = 1;
1323                 }
1324
1325                 if (res->sdev && new_path)
1326                         sdev_printk(KERN_INFO, res->sdev, "Resource path: %s\n",
1327                                     ipr_format_res_path(res->ioa_cfg,
1328                                         res->res_path, buffer, sizeof(buffer)));
1329         } else {
1330                 res->flags = cfgtew->u.cfgte->flags;
1331                 if (res->flags & IPR_IS_IOA_RESOURCE)
1332                         res->type = IPR_RES_TYPE_IOAFP;
1333                 else
1334                         res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1335
1336                 memcpy(&res->std_inq_data, &cfgtew->u.cfgte->std_inq_data,
1337                         sizeof(struct ipr_std_inq_data));
1338
1339                 res->qmodel = IPR_QUEUEING_MODEL(res);
1340                 proto = cfgtew->u.cfgte->proto;
1341                 res->res_handle = cfgtew->u.cfgte->res_handle;
1342         }
1343
1344         ipr_update_ata_class(res, proto);
1345 }
1346
1347 /**
1348  * ipr_clear_res_target - Clear the bit in the bit map representing the target
1349  *                        for the resource.
1350  * @res:        resource entry struct
1351  * @cfgtew:     config table entry wrapper struct
1352  *
1353  * Return value:
1354  *      none
1355  **/
1356 static void ipr_clear_res_target(struct ipr_resource_entry *res)
1357 {
1358         struct ipr_resource_entry *gscsi_res = NULL;
1359         struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
1360
1361         if (!ioa_cfg->sis64)
1362                 return;
1363
1364         if (res->bus == IPR_ARRAY_VIRTUAL_BUS)
1365                 clear_bit(res->target, ioa_cfg->array_ids);
1366         else if (res->bus == IPR_VSET_VIRTUAL_BUS)
1367                 clear_bit(res->target, ioa_cfg->vset_ids);
1368         else if (res->bus == 0 && res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1369                 list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue)
1370                         if (gscsi_res->dev_id == res->dev_id && gscsi_res != res)
1371                                 return;
1372                 clear_bit(res->target, ioa_cfg->target_ids);
1373
1374         } else if (res->bus == 0)
1375                 clear_bit(res->target, ioa_cfg->target_ids);
1376 }
1377
1378 /**
1379  * ipr_handle_config_change - Handle a config change from the adapter
1380  * @ioa_cfg:    ioa config struct
1381  * @hostrcb:    hostrcb
1382  *
1383  * Return value:
1384  *      none
1385  **/
1386 static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
1387                                      struct ipr_hostrcb *hostrcb)
1388 {
1389         struct ipr_resource_entry *res = NULL;
1390         struct ipr_config_table_entry_wrapper cfgtew;
1391         __be32 cc_res_handle;
1392
1393         u32 is_ndn = 1;
1394
1395         if (ioa_cfg->sis64) {
1396                 cfgtew.u.cfgte64 = &hostrcb->hcam.u.ccn.u.cfgte64;
1397                 cc_res_handle = cfgtew.u.cfgte64->res_handle;
1398         } else {
1399                 cfgtew.u.cfgte = &hostrcb->hcam.u.ccn.u.cfgte;
1400                 cc_res_handle = cfgtew.u.cfgte->res_handle;
1401         }
1402
1403         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1404                 if (res->res_handle == cc_res_handle) {
1405                         is_ndn = 0;
1406                         break;
1407                 }
1408         }
1409
1410         if (is_ndn) {
1411                 if (list_empty(&ioa_cfg->free_res_q)) {
1412                         ipr_send_hcam(ioa_cfg,
1413                                       IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
1414                                       hostrcb);
1415                         return;
1416                 }
1417
1418                 res = list_entry(ioa_cfg->free_res_q.next,
1419                                  struct ipr_resource_entry, queue);
1420
1421                 list_del(&res->queue);
1422                 ipr_init_res_entry(res, &cfgtew);
1423                 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
1424         }
1425
1426         ipr_update_res_entry(res, &cfgtew);
1427
1428         if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
1429                 if (res->sdev) {
1430                         res->del_from_ml = 1;
1431                         res->res_handle = IPR_INVALID_RES_HANDLE;
1432                         schedule_work(&ioa_cfg->work_q);
1433                 } else {
1434                         ipr_clear_res_target(res);
1435                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
1436                 }
1437         } else if (!res->sdev || res->del_from_ml) {
1438                 res->add_to_ml = 1;
1439                 schedule_work(&ioa_cfg->work_q);
1440         }
1441
1442         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1443 }
1444
1445 /**
1446  * ipr_process_ccn - Op done function for a CCN.
1447  * @ipr_cmd:    ipr command struct
1448  *
1449  * This function is the op done function for a configuration
1450  * change notification host controlled async from the adapter.
1451  *
1452  * Return value:
1453  *      none
1454  **/
1455 static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
1456 {
1457         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1458         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1459         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1460
1461         list_del(&hostrcb->queue);
1462         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
1463
1464         if (ioasc) {
1465                 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
1466                         dev_err(&ioa_cfg->pdev->dev,
1467                                 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1468
1469                 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1470         } else {
1471                 ipr_handle_config_change(ioa_cfg, hostrcb);
1472         }
1473 }
1474
1475 /**
1476  * strip_and_pad_whitespace - Strip and pad trailing whitespace.
1477  * @i:          index into buffer
1478  * @buf:                string to modify
1479  *
1480  * This function will strip all trailing whitespace, pad the end
1481  * of the string with a single space, and NULL terminate the string.
1482  *
1483  * Return value:
1484  *      new length of string
1485  **/
1486 static int strip_and_pad_whitespace(int i, char *buf)
1487 {
1488         while (i && buf[i] == ' ')
1489                 i--;
1490         buf[i+1] = ' ';
1491         buf[i+2] = '\0';
1492         return i + 2;
1493 }
1494
1495 /**
1496  * ipr_log_vpd_compact - Log the passed extended VPD compactly.
1497  * @prefix:             string to print at start of printk
1498  * @hostrcb:    hostrcb pointer
1499  * @vpd:                vendor/product id/sn struct
1500  *
1501  * Return value:
1502  *      none
1503  **/
1504 static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1505                                 struct ipr_vpd *vpd)
1506 {
1507         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
1508         int i = 0;
1509
1510         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1511         i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
1512
1513         memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
1514         i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
1515
1516         memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
1517         buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
1518
1519         ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
1520 }
1521
1522 /**
1523  * ipr_log_vpd - Log the passed VPD to the error log.
1524  * @vpd:                vendor/product id/sn struct
1525  *
1526  * Return value:
1527  *      none
1528  **/
1529 static void ipr_log_vpd(struct ipr_vpd *vpd)
1530 {
1531         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
1532                     + IPR_SERIAL_NUM_LEN];
1533
1534         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1535         memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
1536                IPR_PROD_ID_LEN);
1537         buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
1538         ipr_err("Vendor/Product ID: %s\n", buffer);
1539
1540         memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
1541         buffer[IPR_SERIAL_NUM_LEN] = '\0';
1542         ipr_err("    Serial Number: %s\n", buffer);
1543 }
1544
1545 /**
1546  * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1547  * @prefix:             string to print at start of printk
1548  * @hostrcb:    hostrcb pointer
1549  * @vpd:                vendor/product id/sn/wwn struct
1550  *
1551  * Return value:
1552  *      none
1553  **/
1554 static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1555                                     struct ipr_ext_vpd *vpd)
1556 {
1557         ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1558         ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1559                      be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1560 }
1561
1562 /**
1563  * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1564  * @vpd:                vendor/product id/sn/wwn struct
1565  *
1566  * Return value:
1567  *      none
1568  **/
1569 static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1570 {
1571         ipr_log_vpd(&vpd->vpd);
1572         ipr_err("    WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1573                 be32_to_cpu(vpd->wwid[1]));
1574 }
1575
1576 /**
1577  * ipr_log_enhanced_cache_error - Log a cache error.
1578  * @ioa_cfg:    ioa config struct
1579  * @hostrcb:    hostrcb struct
1580  *
1581  * Return value:
1582  *      none
1583  **/
1584 static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1585                                          struct ipr_hostrcb *hostrcb)
1586 {
1587         struct ipr_hostrcb_type_12_error *error;
1588
1589         if (ioa_cfg->sis64)
1590                 error = &hostrcb->hcam.u.error64.u.type_12_error;
1591         else
1592                 error = &hostrcb->hcam.u.error.u.type_12_error;
1593
1594         ipr_err("-----Current Configuration-----\n");
1595         ipr_err("Cache Directory Card Information:\n");
1596         ipr_log_ext_vpd(&error->ioa_vpd);
1597         ipr_err("Adapter Card Information:\n");
1598         ipr_log_ext_vpd(&error->cfc_vpd);
1599
1600         ipr_err("-----Expected Configuration-----\n");
1601         ipr_err("Cache Directory Card Information:\n");
1602         ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1603         ipr_err("Adapter Card Information:\n");
1604         ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1605
1606         ipr_err("Additional IOA Data: %08X %08X %08X\n",
1607                      be32_to_cpu(error->ioa_data[0]),
1608                      be32_to_cpu(error->ioa_data[1]),
1609                      be32_to_cpu(error->ioa_data[2]));
1610 }
1611
1612 /**
1613  * ipr_log_cache_error - Log a cache error.
1614  * @ioa_cfg:    ioa config struct
1615  * @hostrcb:    hostrcb struct
1616  *
1617  * Return value:
1618  *      none
1619  **/
1620 static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1621                                 struct ipr_hostrcb *hostrcb)
1622 {
1623         struct ipr_hostrcb_type_02_error *error =
1624                 &hostrcb->hcam.u.error.u.type_02_error;
1625
1626         ipr_err("-----Current Configuration-----\n");
1627         ipr_err("Cache Directory Card Information:\n");
1628         ipr_log_vpd(&error->ioa_vpd);
1629         ipr_err("Adapter Card Information:\n");
1630         ipr_log_vpd(&error->cfc_vpd);
1631
1632         ipr_err("-----Expected Configuration-----\n");
1633         ipr_err("Cache Directory Card Information:\n");
1634         ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
1635         ipr_err("Adapter Card Information:\n");
1636         ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
1637
1638         ipr_err("Additional IOA Data: %08X %08X %08X\n",
1639                      be32_to_cpu(error->ioa_data[0]),
1640                      be32_to_cpu(error->ioa_data[1]),
1641                      be32_to_cpu(error->ioa_data[2]));
1642 }
1643
1644 /**
1645  * ipr_log_enhanced_config_error - Log a configuration error.
1646  * @ioa_cfg:    ioa config struct
1647  * @hostrcb:    hostrcb struct
1648  *
1649  * Return value:
1650  *      none
1651  **/
1652 static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1653                                           struct ipr_hostrcb *hostrcb)
1654 {
1655         int errors_logged, i;
1656         struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1657         struct ipr_hostrcb_type_13_error *error;
1658
1659         error = &hostrcb->hcam.u.error.u.type_13_error;
1660         errors_logged = be32_to_cpu(error->errors_logged);
1661
1662         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1663                 be32_to_cpu(error->errors_detected), errors_logged);
1664
1665         dev_entry = error->dev;
1666
1667         for (i = 0; i < errors_logged; i++, dev_entry++) {
1668                 ipr_err_separator;
1669
1670                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1671                 ipr_log_ext_vpd(&dev_entry->vpd);
1672
1673                 ipr_err("-----New Device Information-----\n");
1674                 ipr_log_ext_vpd(&dev_entry->new_vpd);
1675
1676                 ipr_err("Cache Directory Card Information:\n");
1677                 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1678
1679                 ipr_err("Adapter Card Information:\n");
1680                 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1681         }
1682 }
1683
1684 /**
1685  * ipr_log_sis64_config_error - Log a device error.
1686  * @ioa_cfg:    ioa config struct
1687  * @hostrcb:    hostrcb struct
1688  *
1689  * Return value:
1690  *      none
1691  **/
1692 static void ipr_log_sis64_config_error(struct ipr_ioa_cfg *ioa_cfg,
1693                                        struct ipr_hostrcb *hostrcb)
1694 {
1695         int errors_logged, i;
1696         struct ipr_hostrcb64_device_data_entry_enhanced *dev_entry;
1697         struct ipr_hostrcb_type_23_error *error;
1698         char buffer[IPR_MAX_RES_PATH_LENGTH];
1699
1700         error = &hostrcb->hcam.u.error64.u.type_23_error;
1701         errors_logged = be32_to_cpu(error->errors_logged);
1702
1703         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1704                 be32_to_cpu(error->errors_detected), errors_logged);
1705
1706         dev_entry = error->dev;
1707
1708         for (i = 0; i < errors_logged; i++, dev_entry++) {
1709                 ipr_err_separator;
1710
1711                 ipr_err("Device %d : %s", i + 1,
1712                         __ipr_format_res_path(dev_entry->res_path,
1713                                               buffer, sizeof(buffer)));
1714                 ipr_log_ext_vpd(&dev_entry->vpd);
1715
1716                 ipr_err("-----New Device Information-----\n");
1717                 ipr_log_ext_vpd(&dev_entry->new_vpd);
1718
1719                 ipr_err("Cache Directory Card Information:\n");
1720                 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1721
1722                 ipr_err("Adapter Card Information:\n");
1723                 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1724         }
1725 }
1726
1727 /**
1728  * ipr_log_config_error - Log a configuration error.
1729  * @ioa_cfg:    ioa config struct
1730  * @hostrcb:    hostrcb struct
1731  *
1732  * Return value:
1733  *      none
1734  **/
1735 static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1736                                  struct ipr_hostrcb *hostrcb)
1737 {
1738         int errors_logged, i;
1739         struct ipr_hostrcb_device_data_entry *dev_entry;
1740         struct ipr_hostrcb_type_03_error *error;
1741
1742         error = &hostrcb->hcam.u.error.u.type_03_error;
1743         errors_logged = be32_to_cpu(error->errors_logged);
1744
1745         ipr_err("Device Errors Detected/Logged: %d/%d\n",
1746                 be32_to_cpu(error->errors_detected), errors_logged);
1747
1748         dev_entry = error->dev;
1749
1750         for (i = 0; i < errors_logged; i++, dev_entry++) {
1751                 ipr_err_separator;
1752
1753                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1754                 ipr_log_vpd(&dev_entry->vpd);
1755
1756                 ipr_err("-----New Device Information-----\n");
1757                 ipr_log_vpd(&dev_entry->new_vpd);
1758
1759                 ipr_err("Cache Directory Card Information:\n");
1760                 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
1761
1762                 ipr_err("Adapter Card Information:\n");
1763                 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
1764
1765                 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1766                         be32_to_cpu(dev_entry->ioa_data[0]),
1767                         be32_to_cpu(dev_entry->ioa_data[1]),
1768                         be32_to_cpu(dev_entry->ioa_data[2]),
1769                         be32_to_cpu(dev_entry->ioa_data[3]),
1770                         be32_to_cpu(dev_entry->ioa_data[4]));
1771         }
1772 }
1773
1774 /**
1775  * ipr_log_enhanced_array_error - Log an array configuration error.
1776  * @ioa_cfg:    ioa config struct
1777  * @hostrcb:    hostrcb struct
1778  *
1779  * Return value:
1780  *      none
1781  **/
1782 static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1783                                          struct ipr_hostrcb *hostrcb)
1784 {
1785         int i, num_entries;
1786         struct ipr_hostrcb_type_14_error *error;
1787         struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1788         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1789
1790         error = &hostrcb->hcam.u.error.u.type_14_error;
1791
1792         ipr_err_separator;
1793
1794         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1795                 error->protection_level,
1796                 ioa_cfg->host->host_no,
1797                 error->last_func_vset_res_addr.bus,
1798                 error->last_func_vset_res_addr.target,
1799                 error->last_func_vset_res_addr.lun);
1800
1801         ipr_err_separator;
1802
1803         array_entry = error->array_member;
1804         num_entries = min_t(u32, be32_to_cpu(error->num_entries),
1805                             ARRAY_SIZE(error->array_member));
1806
1807         for (i = 0; i < num_entries; i++, array_entry++) {
1808                 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1809                         continue;
1810
1811                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1812                         ipr_err("Exposed Array Member %d:\n", i);
1813                 else
1814                         ipr_err("Array Member %d:\n", i);
1815
1816                 ipr_log_ext_vpd(&array_entry->vpd);
1817                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1818                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1819                                  "Expected Location");
1820
1821                 ipr_err_separator;
1822         }
1823 }
1824
1825 /**
1826  * ipr_log_array_error - Log an array configuration error.
1827  * @ioa_cfg:    ioa config struct
1828  * @hostrcb:    hostrcb struct
1829  *
1830  * Return value:
1831  *      none
1832  **/
1833 static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1834                                 struct ipr_hostrcb *hostrcb)
1835 {
1836         int i;
1837         struct ipr_hostrcb_type_04_error *error;
1838         struct ipr_hostrcb_array_data_entry *array_entry;
1839         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1840
1841         error = &hostrcb->hcam.u.error.u.type_04_error;
1842
1843         ipr_err_separator;
1844
1845         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1846                 error->protection_level,
1847                 ioa_cfg->host->host_no,
1848                 error->last_func_vset_res_addr.bus,
1849                 error->last_func_vset_res_addr.target,
1850                 error->last_func_vset_res_addr.lun);
1851
1852         ipr_err_separator;
1853
1854         array_entry = error->array_member;
1855
1856         for (i = 0; i < 18; i++) {
1857                 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1858                         continue;
1859
1860                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1861                         ipr_err("Exposed Array Member %d:\n", i);
1862                 else
1863                         ipr_err("Array Member %d:\n", i);
1864
1865                 ipr_log_vpd(&array_entry->vpd);
1866
1867                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1868                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1869                                  "Expected Location");
1870
1871                 ipr_err_separator;
1872
1873                 if (i == 9)
1874                         array_entry = error->array_member2;
1875                 else
1876                         array_entry++;
1877         }
1878 }
1879
1880 /**
1881  * ipr_log_hex_data - Log additional hex IOA error data.
1882  * @ioa_cfg:    ioa config struct
1883  * @data:               IOA error data
1884  * @len:                data length
1885  *
1886  * Return value:
1887  *      none
1888  **/
1889 static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
1890 {
1891         int i;
1892
1893         if (len == 0)
1894                 return;
1895
1896         if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1897                 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1898
1899         for (i = 0; i < len / 4; i += 4) {
1900                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1901                         be32_to_cpu(data[i]),
1902                         be32_to_cpu(data[i+1]),
1903                         be32_to_cpu(data[i+2]),
1904                         be32_to_cpu(data[i+3]));
1905         }
1906 }
1907
1908 /**
1909  * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1910  * @ioa_cfg:    ioa config struct
1911  * @hostrcb:    hostrcb struct
1912  *
1913  * Return value:
1914  *      none
1915  **/
1916 static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1917                                             struct ipr_hostrcb *hostrcb)
1918 {
1919         struct ipr_hostrcb_type_17_error *error;
1920
1921         if (ioa_cfg->sis64)
1922                 error = &hostrcb->hcam.u.error64.u.type_17_error;
1923         else
1924                 error = &hostrcb->hcam.u.error.u.type_17_error;
1925
1926         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1927         strim(error->failure_reason);
1928
1929         ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1930                      be32_to_cpu(hostrcb->hcam.u.error.prc));
1931         ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1932         ipr_log_hex_data(ioa_cfg, error->data,
1933                          be32_to_cpu(hostrcb->hcam.length) -
1934                          (offsetof(struct ipr_hostrcb_error, u) +
1935                           offsetof(struct ipr_hostrcb_type_17_error, data)));
1936 }
1937
1938 /**
1939  * ipr_log_dual_ioa_error - Log a dual adapter error.
1940  * @ioa_cfg:    ioa config struct
1941  * @hostrcb:    hostrcb struct
1942  *
1943  * Return value:
1944  *      none
1945  **/
1946 static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1947                                    struct ipr_hostrcb *hostrcb)
1948 {
1949         struct ipr_hostrcb_type_07_error *error;
1950
1951         error = &hostrcb->hcam.u.error.u.type_07_error;
1952         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1953         strim(error->failure_reason);
1954
1955         ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1956                      be32_to_cpu(hostrcb->hcam.u.error.prc));
1957         ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1958         ipr_log_hex_data(ioa_cfg, error->data,
1959                          be32_to_cpu(hostrcb->hcam.length) -
1960                          (offsetof(struct ipr_hostrcb_error, u) +
1961                           offsetof(struct ipr_hostrcb_type_07_error, data)));
1962 }
1963
1964 static const struct {
1965         u8 active;
1966         char *desc;
1967 } path_active_desc[] = {
1968         { IPR_PATH_NO_INFO, "Path" },
1969         { IPR_PATH_ACTIVE, "Active path" },
1970         { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1971 };
1972
1973 static const struct {
1974         u8 state;
1975         char *desc;
1976 } path_state_desc[] = {
1977         { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1978         { IPR_PATH_HEALTHY, "is healthy" },
1979         { IPR_PATH_DEGRADED, "is degraded" },
1980         { IPR_PATH_FAILED, "is failed" }
1981 };
1982
1983 /**
1984  * ipr_log_fabric_path - Log a fabric path error
1985  * @hostrcb:    hostrcb struct
1986  * @fabric:             fabric descriptor
1987  *
1988  * Return value:
1989  *      none
1990  **/
1991 static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1992                                 struct ipr_hostrcb_fabric_desc *fabric)
1993 {
1994         int i, j;
1995         u8 path_state = fabric->path_state;
1996         u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1997         u8 state = path_state & IPR_PATH_STATE_MASK;
1998
1999         for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
2000                 if (path_active_desc[i].active != active)
2001                         continue;
2002
2003                 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
2004                         if (path_state_desc[j].state != state)
2005                                 continue;
2006
2007                         if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
2008                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
2009                                              path_active_desc[i].desc, path_state_desc[j].desc,
2010                                              fabric->ioa_port);
2011                         } else if (fabric->cascaded_expander == 0xff) {
2012                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
2013                                              path_active_desc[i].desc, path_state_desc[j].desc,
2014                                              fabric->ioa_port, fabric->phy);
2015                         } else if (fabric->phy == 0xff) {
2016                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
2017                                              path_active_desc[i].desc, path_state_desc[j].desc,
2018                                              fabric->ioa_port, fabric->cascaded_expander);
2019                         } else {
2020                                 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
2021                                              path_active_desc[i].desc, path_state_desc[j].desc,
2022                                              fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
2023                         }
2024                         return;
2025                 }
2026         }
2027
2028         ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
2029                 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
2030 }
2031
2032 /**
2033  * ipr_log64_fabric_path - Log a fabric path error
2034  * @hostrcb:    hostrcb struct
2035  * @fabric:             fabric descriptor
2036  *
2037  * Return value:
2038  *      none
2039  **/
2040 static void ipr_log64_fabric_path(struct ipr_hostrcb *hostrcb,
2041                                   struct ipr_hostrcb64_fabric_desc *fabric)
2042 {
2043         int i, j;
2044         u8 path_state = fabric->path_state;
2045         u8 active = path_state & IPR_PATH_ACTIVE_MASK;
2046         u8 state = path_state & IPR_PATH_STATE_MASK;
2047         char buffer[IPR_MAX_RES_PATH_LENGTH];
2048
2049         for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
2050                 if (path_active_desc[i].active != active)
2051                         continue;
2052
2053                 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
2054                         if (path_state_desc[j].state != state)
2055                                 continue;
2056
2057                         ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s\n",
2058                                      path_active_desc[i].desc, path_state_desc[j].desc,
2059                                      ipr_format_res_path(hostrcb->ioa_cfg,
2060                                                 fabric->res_path,
2061                                                 buffer, sizeof(buffer)));
2062                         return;
2063                 }
2064         }
2065
2066         ipr_err("Path state=%02X Resource Path=%s\n", path_state,
2067                 ipr_format_res_path(hostrcb->ioa_cfg, fabric->res_path,
2068                                     buffer, sizeof(buffer)));
2069 }
2070
2071 static const struct {
2072         u8 type;
2073         char *desc;
2074 } path_type_desc[] = {
2075         { IPR_PATH_CFG_IOA_PORT, "IOA port" },
2076         { IPR_PATH_CFG_EXP_PORT, "Expander port" },
2077         { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
2078         { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
2079 };
2080
2081 static const struct {
2082         u8 status;
2083         char *desc;
2084 } path_status_desc[] = {
2085         { IPR_PATH_CFG_NO_PROB, "Functional" },
2086         { IPR_PATH_CFG_DEGRADED, "Degraded" },
2087         { IPR_PATH_CFG_FAILED, "Failed" },
2088         { IPR_PATH_CFG_SUSPECT, "Suspect" },
2089         { IPR_PATH_NOT_DETECTED, "Missing" },
2090         { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
2091 };
2092
2093 static const char *link_rate[] = {
2094         "unknown",
2095         "disabled",
2096         "phy reset problem",
2097         "spinup hold",
2098         "port selector",
2099         "unknown",
2100         "unknown",
2101         "unknown",
2102         "1.5Gbps",
2103         "3.0Gbps",
2104         "unknown",
2105         "unknown",
2106         "unknown",
2107         "unknown",
2108         "unknown",
2109         "unknown"
2110 };
2111
2112 /**
2113  * ipr_log_path_elem - Log a fabric path element.
2114  * @hostrcb:    hostrcb struct
2115  * @cfg:                fabric path element struct
2116  *
2117  * Return value:
2118  *      none
2119  **/
2120 static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
2121                               struct ipr_hostrcb_config_element *cfg)
2122 {
2123         int i, j;
2124         u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
2125         u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
2126
2127         if (type == IPR_PATH_CFG_NOT_EXIST)
2128                 return;
2129
2130         for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
2131                 if (path_type_desc[i].type != type)
2132                         continue;
2133
2134                 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
2135                         if (path_status_desc[j].status != status)
2136                                 continue;
2137
2138                         if (type == IPR_PATH_CFG_IOA_PORT) {
2139                                 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
2140                                              path_status_desc[j].desc, path_type_desc[i].desc,
2141                                              cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2142                                              be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2143                         } else {
2144                                 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
2145                                         ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
2146                                                      path_status_desc[j].desc, path_type_desc[i].desc,
2147                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2148                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2149                                 } else if (cfg->cascaded_expander == 0xff) {
2150                                         ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
2151                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
2152                                                      path_type_desc[i].desc, cfg->phy,
2153                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2154                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2155                                 } else if (cfg->phy == 0xff) {
2156                                         ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
2157                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
2158                                                      path_type_desc[i].desc, cfg->cascaded_expander,
2159                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2160                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2161                                 } else {
2162                                         ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
2163                                                      "WWN=%08X%08X\n", path_status_desc[j].desc,
2164                                                      path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
2165                                                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2166                                                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2167                                 }
2168                         }
2169                         return;
2170                 }
2171         }
2172
2173         ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
2174                      "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
2175                      link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2176                      be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2177 }
2178
2179 /**
2180  * ipr_log64_path_elem - Log a fabric path element.
2181  * @hostrcb:    hostrcb struct
2182  * @cfg:                fabric path element struct
2183  *
2184  * Return value:
2185  *      none
2186  **/
2187 static void ipr_log64_path_elem(struct ipr_hostrcb *hostrcb,
2188                                 struct ipr_hostrcb64_config_element *cfg)
2189 {
2190         int i, j;
2191         u8 desc_id = cfg->descriptor_id & IPR_DESCRIPTOR_MASK;
2192         u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
2193         u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
2194         char buffer[IPR_MAX_RES_PATH_LENGTH];
2195
2196         if (type == IPR_PATH_CFG_NOT_EXIST || desc_id != IPR_DESCRIPTOR_SIS64)
2197                 return;
2198
2199         for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
2200                 if (path_type_desc[i].type != type)
2201                         continue;
2202
2203                 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
2204                         if (path_status_desc[j].status != status)
2205                                 continue;
2206
2207                         ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s, Link rate=%s, WWN=%08X%08X\n",
2208                                      path_status_desc[j].desc, path_type_desc[i].desc,
2209                                      ipr_format_res_path(hostrcb->ioa_cfg,
2210                                         cfg->res_path, buffer, sizeof(buffer)),
2211                                         link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2212                                         be32_to_cpu(cfg->wwid[0]),
2213                                         be32_to_cpu(cfg->wwid[1]));
2214                         return;
2215                 }
2216         }
2217         ipr_hcam_err(hostrcb, "Path element=%02X: Resource Path=%s, Link rate=%s "
2218                      "WWN=%08X%08X\n", cfg->type_status,
2219                      ipr_format_res_path(hostrcb->ioa_cfg,
2220                         cfg->res_path, buffer, sizeof(buffer)),
2221                         link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2222                         be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2223 }
2224
2225 /**
2226  * ipr_log_fabric_error - Log a fabric error.
2227  * @ioa_cfg:    ioa config struct
2228  * @hostrcb:    hostrcb struct
2229  *
2230  * Return value:
2231  *      none
2232  **/
2233 static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
2234                                  struct ipr_hostrcb *hostrcb)
2235 {
2236         struct ipr_hostrcb_type_20_error *error;
2237         struct ipr_hostrcb_fabric_desc *fabric;
2238         struct ipr_hostrcb_config_element *cfg;
2239         int i, add_len;
2240
2241         error = &hostrcb->hcam.u.error.u.type_20_error;
2242         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
2243         ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
2244
2245         add_len = be32_to_cpu(hostrcb->hcam.length) -
2246                 (offsetof(struct ipr_hostrcb_error, u) +
2247                  offsetof(struct ipr_hostrcb_type_20_error, desc));
2248
2249         for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
2250                 ipr_log_fabric_path(hostrcb, fabric);
2251                 for_each_fabric_cfg(fabric, cfg)
2252                         ipr_log_path_elem(hostrcb, cfg);
2253
2254                 add_len -= be16_to_cpu(fabric->length);
2255                 fabric = (struct ipr_hostrcb_fabric_desc *)
2256                         ((unsigned long)fabric + be16_to_cpu(fabric->length));
2257         }
2258
2259         ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
2260 }
2261
2262 /**
2263  * ipr_log_sis64_array_error - Log a sis64 array error.
2264  * @ioa_cfg:    ioa config struct
2265  * @hostrcb:    hostrcb struct
2266  *
2267  * Return value:
2268  *      none
2269  **/
2270 static void ipr_log_sis64_array_error(struct ipr_ioa_cfg *ioa_cfg,
2271                                       struct ipr_hostrcb *hostrcb)
2272 {
2273         int i, num_entries;
2274         struct ipr_hostrcb_type_24_error *error;
2275         struct ipr_hostrcb64_array_data_entry *array_entry;
2276         char buffer[IPR_MAX_RES_PATH_LENGTH];
2277         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
2278
2279         error = &hostrcb->hcam.u.error64.u.type_24_error;
2280
2281         ipr_err_separator;
2282
2283         ipr_err("RAID %s Array Configuration: %s\n",
2284                 error->protection_level,
2285                 ipr_format_res_path(ioa_cfg, error->last_res_path,
2286                         buffer, sizeof(buffer)));
2287
2288         ipr_err_separator;
2289
2290         array_entry = error->array_member;
2291         num_entries = min_t(u32, error->num_entries,
2292                             ARRAY_SIZE(error->array_member));
2293
2294         for (i = 0; i < num_entries; i++, array_entry++) {
2295
2296                 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
2297                         continue;
2298
2299                 if (error->exposed_mode_adn == i)
2300                         ipr_err("Exposed Array Member %d:\n", i);
2301                 else
2302                         ipr_err("Array Member %d:\n", i);
2303
2304                 ipr_err("Array Member %d:\n", i);
2305                 ipr_log_ext_vpd(&array_entry->vpd);
2306                 ipr_err("Current Location: %s\n",
2307                          ipr_format_res_path(ioa_cfg, array_entry->res_path,
2308                                 buffer, sizeof(buffer)));
2309                 ipr_err("Expected Location: %s\n",
2310                          ipr_format_res_path(ioa_cfg,
2311                                 array_entry->expected_res_path,
2312                                 buffer, sizeof(buffer)));
2313
2314                 ipr_err_separator;
2315         }
2316 }
2317
2318 /**
2319  * ipr_log_sis64_fabric_error - Log a sis64 fabric error.
2320  * @ioa_cfg:    ioa config struct
2321  * @hostrcb:    hostrcb struct
2322  *
2323  * Return value:
2324  *      none
2325  **/
2326 static void ipr_log_sis64_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
2327                                        struct ipr_hostrcb *hostrcb)
2328 {
2329         struct ipr_hostrcb_type_30_error *error;
2330         struct ipr_hostrcb64_fabric_desc *fabric;
2331         struct ipr_hostrcb64_config_element *cfg;
2332         int i, add_len;
2333
2334         error = &hostrcb->hcam.u.error64.u.type_30_error;
2335
2336         error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
2337         ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
2338
2339         add_len = be32_to_cpu(hostrcb->hcam.length) -
2340                 (offsetof(struct ipr_hostrcb64_error, u) +
2341                  offsetof(struct ipr_hostrcb_type_30_error, desc));
2342
2343         for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
2344                 ipr_log64_fabric_path(hostrcb, fabric);
2345                 for_each_fabric_cfg(fabric, cfg)
2346                         ipr_log64_path_elem(hostrcb, cfg);
2347
2348                 add_len -= be16_to_cpu(fabric->length);
2349                 fabric = (struct ipr_hostrcb64_fabric_desc *)
2350                         ((unsigned long)fabric + be16_to_cpu(fabric->length));
2351         }
2352
2353         ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
2354 }
2355
2356 /**
2357  * ipr_log_generic_error - Log an adapter error.
2358  * @ioa_cfg:    ioa config struct
2359  * @hostrcb:    hostrcb struct
2360  *
2361  * Return value:
2362  *      none
2363  **/
2364 static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
2365                                   struct ipr_hostrcb *hostrcb)
2366 {
2367         ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
2368                          be32_to_cpu(hostrcb->hcam.length));
2369 }
2370
2371 /**
2372  * ipr_log_sis64_device_error - Log a cache error.
2373  * @ioa_cfg:    ioa config struct
2374  * @hostrcb:    hostrcb struct
2375  *
2376  * Return value:
2377  *      none
2378  **/
2379 static void ipr_log_sis64_device_error(struct ipr_ioa_cfg *ioa_cfg,
2380                                          struct ipr_hostrcb *hostrcb)
2381 {
2382         struct ipr_hostrcb_type_21_error *error;
2383         char buffer[IPR_MAX_RES_PATH_LENGTH];
2384
2385         error = &hostrcb->hcam.u.error64.u.type_21_error;
2386
2387         ipr_err("-----Failing Device Information-----\n");
2388         ipr_err("World Wide Unique ID: %08X%08X%08X%08X\n",
2389                 be32_to_cpu(error->wwn[0]), be32_to_cpu(error->wwn[1]),
2390                  be32_to_cpu(error->wwn[2]), be32_to_cpu(error->wwn[3]));
2391         ipr_err("Device Resource Path: %s\n",
2392                 __ipr_format_res_path(error->res_path,
2393                                       buffer, sizeof(buffer)));
2394         error->primary_problem_desc[sizeof(error->primary_problem_desc) - 1] = '\0';
2395         error->second_problem_desc[sizeof(error->second_problem_desc) - 1] = '\0';
2396         ipr_err("Primary Problem Description: %s\n", error->primary_problem_desc);
2397         ipr_err("Secondary Problem Description:  %s\n", error->second_problem_desc);
2398         ipr_err("SCSI Sense Data:\n");
2399         ipr_log_hex_data(ioa_cfg, error->sense_data, sizeof(error->sense_data));
2400         ipr_err("SCSI Command Descriptor Block: \n");
2401         ipr_log_hex_data(ioa_cfg, error->cdb, sizeof(error->cdb));
2402
2403         ipr_err("Additional IOA Data:\n");
2404         ipr_log_hex_data(ioa_cfg, error->ioa_data, be32_to_cpu(error->length_of_error));
2405 }
2406
2407 /**
2408  * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
2409  * @ioasc:      IOASC
2410  *
2411  * This function will return the index of into the ipr_error_table
2412  * for the specified IOASC. If the IOASC is not in the table,
2413  * 0 will be returned, which points to the entry used for unknown errors.
2414  *
2415  * Return value:
2416  *      index into the ipr_error_table
2417  **/
2418 static u32 ipr_get_error(u32 ioasc)
2419 {
2420         int i;
2421
2422         for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
2423                 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
2424                         return i;
2425
2426         return 0;
2427 }
2428
2429 /**
2430  * ipr_handle_log_data - Log an adapter error.
2431  * @ioa_cfg:    ioa config struct
2432  * @hostrcb:    hostrcb struct
2433  *
2434  * This function logs an adapter error to the system.
2435  *
2436  * Return value:
2437  *      none
2438  **/
2439 static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
2440                                 struct ipr_hostrcb *hostrcb)
2441 {
2442         u32 ioasc;
2443         int error_index;
2444         struct ipr_hostrcb_type_21_error *error;
2445
2446         if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
2447                 return;
2448
2449         if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
2450                 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
2451
2452         if (ioa_cfg->sis64)
2453                 ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
2454         else
2455                 ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
2456
2457         if (!ioa_cfg->sis64 && (ioasc == IPR_IOASC_BUS_WAS_RESET ||
2458             ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER)) {
2459                 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
2460                 scsi_report_bus_reset(ioa_cfg->host,
2461                                       hostrcb->hcam.u.error.fd_res_addr.bus);
2462         }
2463
2464         error_index = ipr_get_error(ioasc);
2465
2466         if (!ipr_error_table[error_index].log_hcam)
2467                 return;
2468
2469         if (ioasc == IPR_IOASC_HW_CMD_FAILED &&
2470             hostrcb->hcam.overlay_id == IPR_HOST_RCB_OVERLAY_ID_21) {
2471                 error = &hostrcb->hcam.u.error64.u.type_21_error;
2472
2473                 if (((be32_to_cpu(error->sense_data[0]) & 0x0000ff00) >> 8) == ILLEGAL_REQUEST &&
2474                         ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
2475                                 return;
2476         }
2477
2478         ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
2479
2480         /* Set indication we have logged an error */
2481         ioa_cfg->errors_logged++;
2482
2483         if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
2484                 return;
2485         if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
2486                 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
2487
2488         switch (hostrcb->hcam.overlay_id) {
2489         case IPR_HOST_RCB_OVERLAY_ID_2:
2490                 ipr_log_cache_error(ioa_cfg, hostrcb);
2491                 break;
2492         case IPR_HOST_RCB_OVERLAY_ID_3:
2493                 ipr_log_config_error(ioa_cfg, hostrcb);
2494                 break;
2495         case IPR_HOST_RCB_OVERLAY_ID_4:
2496         case IPR_HOST_RCB_OVERLAY_ID_6:
2497                 ipr_log_array_error(ioa_cfg, hostrcb);
2498                 break;
2499         case IPR_HOST_RCB_OVERLAY_ID_7:
2500                 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
2501                 break;
2502         case IPR_HOST_RCB_OVERLAY_ID_12:
2503                 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
2504                 break;
2505         case IPR_HOST_RCB_OVERLAY_ID_13:
2506                 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
2507                 break;
2508         case IPR_HOST_RCB_OVERLAY_ID_14:
2509         case IPR_HOST_RCB_OVERLAY_ID_16:
2510                 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
2511                 break;
2512         case IPR_HOST_RCB_OVERLAY_ID_17:
2513                 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
2514                 break;
2515         case IPR_HOST_RCB_OVERLAY_ID_20:
2516                 ipr_log_fabric_error(ioa_cfg, hostrcb);
2517                 break;
2518         case IPR_HOST_RCB_OVERLAY_ID_21:
2519                 ipr_log_sis64_device_error(ioa_cfg, hostrcb);
2520                 break;
2521         case IPR_HOST_RCB_OVERLAY_ID_23:
2522                 ipr_log_sis64_config_error(ioa_cfg, hostrcb);
2523                 break;
2524         case IPR_HOST_RCB_OVERLAY_ID_24:
2525         case IPR_HOST_RCB_OVERLAY_ID_26:
2526                 ipr_log_sis64_array_error(ioa_cfg, hostrcb);
2527                 break;
2528         case IPR_HOST_RCB_OVERLAY_ID_30:
2529                 ipr_log_sis64_fabric_error(ioa_cfg, hostrcb);
2530                 break;
2531         case IPR_HOST_RCB_OVERLAY_ID_1:
2532         case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
2533         default:
2534                 ipr_log_generic_error(ioa_cfg, hostrcb);
2535                 break;
2536         }
2537 }
2538
2539 /**
2540  * ipr_process_error - Op done function for an adapter error log.
2541  * @ipr_cmd:    ipr command struct
2542  *
2543  * This function is the op done function for an error log host
2544  * controlled async from the adapter. It will log the error and
2545  * send the HCAM back to the adapter.
2546  *
2547  * Return value:
2548  *      none
2549  **/
2550 static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
2551 {
2552         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2553         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
2554         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
2555         u32 fd_ioasc;
2556
2557         if (ioa_cfg->sis64)
2558                 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
2559         else
2560                 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
2561
2562         list_del(&hostrcb->queue);
2563         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
2564
2565         if (!ioasc) {
2566                 ipr_handle_log_data(ioa_cfg, hostrcb);
2567                 if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
2568                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
2569         } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
2570                 dev_err(&ioa_cfg->pdev->dev,
2571                         "Host RCB failed with IOASC: 0x%08X\n", ioasc);
2572         }
2573
2574         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
2575 }
2576
2577 /**
2578  * ipr_timeout -  An internally generated op has timed out.
2579  * @ipr_cmd:    ipr command struct
2580  *
2581  * This function blocks host requests and initiates an
2582  * adapter reset.
2583  *
2584  * Return value:
2585  *      none
2586  **/
2587 static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
2588 {
2589         unsigned long lock_flags = 0;
2590         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2591
2592         ENTER;
2593         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2594
2595         ioa_cfg->errors_logged++;
2596         dev_err(&ioa_cfg->pdev->dev,
2597                 "Adapter being reset due to command timeout.\n");
2598
2599         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2600                 ioa_cfg->sdt_state = GET_DUMP;
2601
2602         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
2603                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2604
2605         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2606         LEAVE;
2607 }
2608
2609 /**
2610  * ipr_oper_timeout -  Adapter timed out transitioning to operational
2611  * @ipr_cmd:    ipr command struct
2612  *
2613  * This function blocks host requests and initiates an
2614  * adapter reset.
2615  *
2616  * Return value:
2617  *      none
2618  **/
2619 static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
2620 {
2621         unsigned long lock_flags = 0;
2622         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2623
2624         ENTER;
2625         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2626
2627         ioa_cfg->errors_logged++;
2628         dev_err(&ioa_cfg->pdev->dev,
2629                 "Adapter timed out transitioning to operational.\n");
2630
2631         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2632                 ioa_cfg->sdt_state = GET_DUMP;
2633
2634         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
2635                 if (ipr_fastfail)
2636                         ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
2637                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2638         }
2639
2640         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2641         LEAVE;
2642 }
2643
2644 /**
2645  * ipr_find_ses_entry - Find matching SES in SES table
2646  * @res:        resource entry struct of SES
2647  *
2648  * Return value:
2649  *      pointer to SES table entry / NULL on failure
2650  **/
2651 static const struct ipr_ses_table_entry *
2652 ipr_find_ses_entry(struct ipr_resource_entry *res)
2653 {
2654         int i, j, matches;
2655         struct ipr_std_inq_vpids *vpids;
2656         const struct ipr_ses_table_entry *ste = ipr_ses_table;
2657
2658         for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
2659                 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
2660                         if (ste->compare_product_id_byte[j] == 'X') {
2661                                 vpids = &res->std_inq_data.vpids;
2662                                 if (vpids->product_id[j] == ste->product_id[j])
2663                                         matches++;
2664                                 else
2665                                         break;
2666                         } else
2667                                 matches++;
2668                 }
2669
2670                 if (matches == IPR_PROD_ID_LEN)
2671                         return ste;
2672         }
2673
2674         return NULL;
2675 }
2676
2677 /**
2678  * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
2679  * @ioa_cfg:    ioa config struct
2680  * @bus:                SCSI bus
2681  * @bus_width:  bus width
2682  *
2683  * Return value:
2684  *      SCSI bus speed in units of 100KHz, 1600 is 160 MHz
2685  *      For a 2-byte wide SCSI bus, the maximum transfer speed is
2686  *      twice the maximum transfer rate (e.g. for a wide enabled bus,
2687  *      max 160MHz = max 320MB/sec).
2688  **/
2689 static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
2690 {
2691         struct ipr_resource_entry *res;
2692         const struct ipr_ses_table_entry *ste;
2693         u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
2694
2695         /* Loop through each config table entry in the config table buffer */
2696         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2697                 if (!(IPR_IS_SES_DEVICE(res->std_inq_data)))
2698                         continue;
2699
2700                 if (bus != res->bus)
2701                         continue;
2702
2703                 if (!(ste = ipr_find_ses_entry(res)))
2704                         continue;
2705
2706                 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
2707         }
2708
2709         return max_xfer_rate;
2710 }
2711
2712 /**
2713  * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
2714  * @ioa_cfg:            ioa config struct
2715  * @max_delay:          max delay in micro-seconds to wait
2716  *
2717  * Waits for an IODEBUG ACK from the IOA, doing busy looping.
2718  *
2719  * Return value:
2720  *      0 on success / other on failure
2721  **/
2722 static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
2723 {
2724         volatile u32 pcii_reg;
2725         int delay = 1;
2726
2727         /* Read interrupt reg until IOA signals IO Debug Acknowledge */
2728         while (delay < max_delay) {
2729                 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
2730
2731                 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
2732                         return 0;
2733
2734                 /* udelay cannot be used if delay is more than a few milliseconds */
2735                 if ((delay / 1000) > MAX_UDELAY_MS)
2736                         mdelay(delay / 1000);
2737                 else
2738                         udelay(delay);
2739
2740                 delay += delay;
2741         }
2742         return -EIO;
2743 }
2744
2745 /**
2746  * ipr_get_sis64_dump_data_section - Dump IOA memory
2747  * @ioa_cfg:                    ioa config struct
2748  * @start_addr:                 adapter address to dump
2749  * @dest:                       destination kernel buffer
2750  * @length_in_words:            length to dump in 4 byte words
2751  *
2752  * Return value:
2753  *      0 on success
2754  **/
2755 static int ipr_get_sis64_dump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2756                                            u32 start_addr,
2757                                            __be32 *dest, u32 length_in_words)
2758 {
2759         int i;
2760
2761         for (i = 0; i < length_in_words; i++) {
2762                 writel(start_addr+(i*4), ioa_cfg->regs.dump_addr_reg);
2763                 *dest = cpu_to_be32(readl(ioa_cfg->regs.dump_data_reg));
2764                 dest++;
2765         }
2766
2767         return 0;
2768 }
2769
2770 /**
2771  * ipr_get_ldump_data_section - Dump IOA memory
2772  * @ioa_cfg:                    ioa config struct
2773  * @start_addr:                 adapter address to dump
2774  * @dest:                               destination kernel buffer
2775  * @length_in_words:    length to dump in 4 byte words
2776  *
2777  * Return value:
2778  *      0 on success / -EIO on failure
2779  **/
2780 static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2781                                       u32 start_addr,
2782                                       __be32 *dest, u32 length_in_words)
2783 {
2784         volatile u32 temp_pcii_reg;
2785         int i, delay = 0;
2786
2787         if (ioa_cfg->sis64)
2788                 return ipr_get_sis64_dump_data_section(ioa_cfg, start_addr,
2789                                                        dest, length_in_words);
2790
2791         /* Write IOA interrupt reg starting LDUMP state  */
2792         writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
2793                ioa_cfg->regs.set_uproc_interrupt_reg32);
2794
2795         /* Wait for IO debug acknowledge */
2796         if (ipr_wait_iodbg_ack(ioa_cfg,
2797                                IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
2798                 dev_err(&ioa_cfg->pdev->dev,
2799                         "IOA dump long data transfer timeout\n");
2800                 return -EIO;
2801         }
2802
2803         /* Signal LDUMP interlocked - clear IO debug ack */
2804         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2805                ioa_cfg->regs.clr_interrupt_reg);
2806
2807         /* Write Mailbox with starting address */
2808         writel(start_addr, ioa_cfg->ioa_mailbox);
2809
2810         /* Signal address valid - clear IOA Reset alert */
2811         writel(IPR_UPROCI_RESET_ALERT,
2812                ioa_cfg->regs.clr_uproc_interrupt_reg32);
2813
2814         for (i = 0; i < length_in_words; i++) {
2815                 /* Wait for IO debug acknowledge */
2816                 if (ipr_wait_iodbg_ack(ioa_cfg,
2817                                        IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
2818                         dev_err(&ioa_cfg->pdev->dev,
2819                                 "IOA dump short data transfer timeout\n");
2820                         return -EIO;
2821                 }
2822
2823                 /* Read data from mailbox and increment destination pointer */
2824                 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
2825                 dest++;
2826
2827                 /* For all but the last word of data, signal data received */
2828                 if (i < (length_in_words - 1)) {
2829                         /* Signal dump data received - Clear IO debug Ack */
2830                         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2831                                ioa_cfg->regs.clr_interrupt_reg);
2832                 }
2833         }
2834
2835         /* Signal end of block transfer. Set reset alert then clear IO debug ack */
2836         writel(IPR_UPROCI_RESET_ALERT,
2837                ioa_cfg->regs.set_uproc_interrupt_reg32);
2838
2839         writel(IPR_UPROCI_IO_DEBUG_ALERT,
2840                ioa_cfg->regs.clr_uproc_interrupt_reg32);
2841
2842         /* Signal dump data received - Clear IO debug Ack */
2843         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2844                ioa_cfg->regs.clr_interrupt_reg);
2845
2846         /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2847         while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2848                 temp_pcii_reg =
2849                     readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
2850
2851                 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2852                         return 0;
2853
2854                 udelay(10);
2855                 delay += 10;
2856         }
2857
2858         return 0;
2859 }
2860
2861 #ifdef CONFIG_SCSI_IPR_DUMP
2862 /**
2863  * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2864  * @ioa_cfg:            ioa config struct
2865  * @pci_address:        adapter address
2866  * @length:                     length of data to copy
2867  *
2868  * Copy data from PCI adapter to kernel buffer.
2869  * Note: length MUST be a 4 byte multiple
2870  * Return value:
2871  *      0 on success / other on failure
2872  **/
2873 static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2874                         unsigned long pci_address, u32 length)
2875 {
2876         int bytes_copied = 0;
2877         int cur_len, rc, rem_len, rem_page_len, max_dump_size;
2878         __be32 *page;
2879         unsigned long lock_flags = 0;
2880         struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2881
2882         if (ioa_cfg->sis64)
2883                 max_dump_size = IPR_FMT3_MAX_IOA_DUMP_SIZE;
2884         else
2885                 max_dump_size = IPR_FMT2_MAX_IOA_DUMP_SIZE;
2886
2887         while (bytes_copied < length &&
2888                (ioa_dump->hdr.len + bytes_copied) < max_dump_size) {
2889                 if (ioa_dump->page_offset >= PAGE_SIZE ||
2890                     ioa_dump->page_offset == 0) {
2891                         page = (__be32 *)__get_free_page(GFP_ATOMIC);
2892
2893                         if (!page) {
2894                                 ipr_trace;
2895                                 return bytes_copied;
2896                         }
2897
2898                         ioa_dump->page_offset = 0;
2899                         ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2900                         ioa_dump->next_page_index++;
2901                 } else
2902                         page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2903
2904                 rem_len = length - bytes_copied;
2905                 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2906                 cur_len = min(rem_len, rem_page_len);
2907
2908                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2909                 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2910                         rc = -EIO;
2911                 } else {
2912                         rc = ipr_get_ldump_data_section(ioa_cfg,
2913                                                         pci_address + bytes_copied,
2914                                                         &page[ioa_dump->page_offset / 4],
2915                                                         (cur_len / sizeof(u32)));
2916                 }
2917                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2918
2919                 if (!rc) {
2920                         ioa_dump->page_offset += cur_len;
2921                         bytes_copied += cur_len;
2922                 } else {
2923                         ipr_trace;
2924                         break;
2925                 }
2926                 schedule();
2927         }
2928
2929         return bytes_copied;
2930 }
2931
2932 /**
2933  * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2934  * @hdr:        dump entry header struct
2935  *
2936  * Return value:
2937  *      nothing
2938  **/
2939 static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2940 {
2941         hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2942         hdr->num_elems = 1;
2943         hdr->offset = sizeof(*hdr);
2944         hdr->status = IPR_DUMP_STATUS_SUCCESS;
2945 }
2946
2947 /**
2948  * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2949  * @ioa_cfg:    ioa config struct
2950  * @driver_dump:        driver dump struct
2951  *
2952  * Return value:
2953  *      nothing
2954  **/
2955 static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2956                                    struct ipr_driver_dump *driver_dump)
2957 {
2958         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2959
2960         ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2961         driver_dump->ioa_type_entry.hdr.len =
2962                 sizeof(struct ipr_dump_ioa_type_entry) -
2963                 sizeof(struct ipr_dump_entry_header);
2964         driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2965         driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2966         driver_dump->ioa_type_entry.type = ioa_cfg->type;
2967         driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2968                 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2969                 ucode_vpd->minor_release[1];
2970         driver_dump->hdr.num_entries++;
2971 }
2972
2973 /**
2974  * ipr_dump_version_data - Fill in the driver version in the dump.
2975  * @ioa_cfg:    ioa config struct
2976  * @driver_dump:        driver dump struct
2977  *
2978  * Return value:
2979  *      nothing
2980  **/
2981 static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2982                                   struct ipr_driver_dump *driver_dump)
2983 {
2984         ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2985         driver_dump->version_entry.hdr.len =
2986                 sizeof(struct ipr_dump_version_entry) -
2987                 sizeof(struct ipr_dump_entry_header);
2988         driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2989         driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2990         strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2991         driver_dump->hdr.num_entries++;
2992 }
2993
2994 /**
2995  * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2996  * @ioa_cfg:    ioa config struct
2997  * @driver_dump:        driver dump struct
2998  *
2999  * Return value:
3000  *      nothing
3001  **/
3002 static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
3003                                    struct ipr_driver_dump *driver_dump)
3004 {
3005         ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
3006         driver_dump->trace_entry.hdr.len =
3007                 sizeof(struct ipr_dump_trace_entry) -
3008                 sizeof(struct ipr_dump_entry_header);
3009         driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
3010         driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
3011         memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
3012         driver_dump->hdr.num_entries++;
3013 }
3014
3015 /**
3016  * ipr_dump_location_data - Fill in the IOA location in the dump.
3017  * @ioa_cfg:    ioa config struct
3018  * @driver_dump:        driver dump struct
3019  *
3020  * Return value:
3021  *      nothing
3022  **/
3023 static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
3024                                    struct ipr_driver_dump *driver_dump)
3025 {
3026         ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
3027         driver_dump->location_entry.hdr.len =
3028                 sizeof(struct ipr_dump_location_entry) -
3029                 sizeof(struct ipr_dump_entry_header);
3030         driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
3031         driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
3032         strcpy(driver_dump->location_entry.location, dev_name(&ioa_cfg->pdev->dev));
3033         driver_dump->hdr.num_entries++;
3034 }
3035
3036 /**
3037  * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
3038  * @ioa_cfg:    ioa config struct
3039  * @dump:               dump struct
3040  *
3041  * Return value:
3042  *      nothing
3043  **/
3044 static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
3045 {
3046         unsigned long start_addr, sdt_word;
3047         unsigned long lock_flags = 0;
3048         struct ipr_driver_dump *driver_dump = &dump->driver_dump;
3049         struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
3050         u32 num_entries, max_num_entries, start_off, end_off;
3051         u32 max_dump_size, bytes_to_copy, bytes_copied, rc;
3052         struct ipr_sdt *sdt;
3053         int valid = 1;
3054         int i;
3055
3056         ENTER;
3057
3058         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3059
3060         if (ioa_cfg->sdt_state != READ_DUMP) {
3061                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3062                 return;
3063         }
3064
3065         if (ioa_cfg->sis64) {
3066                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3067                 ssleep(IPR_DUMP_DELAY_SECONDS);
3068                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3069         }
3070
3071         start_addr = readl(ioa_cfg->ioa_mailbox);
3072
3073         if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(start_addr)) {
3074                 dev_err(&ioa_cfg->pdev->dev,
3075                         "Invalid dump table format: %lx\n", start_addr);
3076                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3077                 return;
3078         }
3079
3080         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
3081
3082         driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
3083
3084         /* Initialize the overall dump header */
3085         driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
3086         driver_dump->hdr.num_entries = 1;
3087         driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
3088         driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
3089         driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
3090         driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
3091
3092         ipr_dump_version_data(ioa_cfg, driver_dump);
3093         ipr_dump_location_data(ioa_cfg, driver_dump);
3094         ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
3095         ipr_dump_trace_data(ioa_cfg, driver_dump);
3096
3097         /* Update dump_header */
3098         driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
3099
3100         /* IOA Dump entry */
3101         ipr_init_dump_entry_hdr(&ioa_dump->hdr);
3102         ioa_dump->hdr.len = 0;
3103         ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
3104         ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
3105
3106         /* First entries in sdt are actually a list of dump addresses and
3107          lengths to gather the real dump data.  sdt represents the pointer
3108          to the ioa generated dump table.  Dump data will be extracted based
3109          on entries in this table */
3110         sdt = &ioa_dump->sdt;
3111
3112         if (ioa_cfg->sis64) {
3113                 max_num_entries = IPR_FMT3_NUM_SDT_ENTRIES;
3114                 max_dump_size = IPR_FMT3_MAX_IOA_DUMP_SIZE;
3115         } else {
3116                 max_num_entries = IPR_FMT2_NUM_SDT_ENTRIES;
3117                 max_dump_size = IPR_FMT2_MAX_IOA_DUMP_SIZE;
3118         }
3119
3120         bytes_to_copy = offsetof(struct ipr_sdt, entry) +
3121                         (max_num_entries * sizeof(struct ipr_sdt_entry));
3122         rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
3123                                         bytes_to_copy / sizeof(__be32));
3124
3125         /* Smart Dump table is ready to use and the first entry is valid */
3126         if (rc || ((be32_to_cpu(sdt->hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
3127             (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
3128                 dev_err(&ioa_cfg->pdev->dev,
3129                         "Dump of IOA failed. Dump table not valid: %d, %X.\n",
3130                         rc, be32_to_cpu(sdt->hdr.state));
3131                 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
3132                 ioa_cfg->sdt_state = DUMP_OBTAINED;
3133                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3134                 return;
3135         }
3136
3137         num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
3138
3139         if (num_entries > max_num_entries)
3140                 num_entries = max_num_entries;
3141
3142         /* Update dump length to the actual data to be copied */
3143         dump->driver_dump.hdr.len += sizeof(struct ipr_sdt_header);
3144         if (ioa_cfg->sis64)
3145                 dump->driver_dump.hdr.len += num_entries * sizeof(struct ipr_sdt_entry);
3146         else
3147                 dump->driver_dump.hdr.len += max_num_entries * sizeof(struct ipr_sdt_entry);
3148
3149         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3150
3151         for (i = 0; i < num_entries; i++) {
3152                 if (ioa_dump->hdr.len > max_dump_size) {
3153                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
3154                         break;
3155                 }
3156
3157                 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
3158                         sdt_word = be32_to_cpu(sdt->entry[i].start_token);
3159                         if (ioa_cfg->sis64)
3160                                 bytes_to_copy = be32_to_cpu(sdt->entry[i].end_token);
3161                         else {
3162                                 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
3163                                 end_off = be32_to_cpu(sdt->entry[i].end_token);
3164
3165                                 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word)
3166                                         bytes_to_copy = end_off - start_off;
3167                                 else
3168                                         valid = 0;
3169                         }
3170                         if (valid) {
3171                                 if (bytes_to_copy > max_dump_size) {
3172                                         sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
3173                                         continue;
3174                                 }
3175
3176                                 /* Copy data from adapter to driver buffers */
3177                                 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
3178                                                             bytes_to_copy);
3179
3180                                 ioa_dump->hdr.len += bytes_copied;
3181
3182                                 if (bytes_copied != bytes_to_copy) {
3183                                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
3184                                         break;
3185                                 }
3186                         }
3187                 }
3188         }
3189
3190         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
3191
3192         /* Update dump_header */
3193         driver_dump->hdr.len += ioa_dump->hdr.len;
3194         wmb();
3195         ioa_cfg->sdt_state = DUMP_OBTAINED;
3196         LEAVE;
3197 }
3198
3199 #else
3200 #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while (0)
3201 #endif
3202
3203 /**
3204  * ipr_release_dump - Free adapter dump memory
3205  * @kref:       kref struct
3206  *
3207  * Return value:
3208  *      nothing
3209  **/
3210 static void ipr_release_dump(struct kref *kref)
3211 {
3212         struct ipr_dump *dump = container_of(kref, struct ipr_dump, kref);
3213         struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
3214         unsigned long lock_flags = 0;
3215         int i;
3216
3217         ENTER;
3218         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3219         ioa_cfg->dump = NULL;
3220         ioa_cfg->sdt_state = INACTIVE;
3221         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3222
3223         for (i = 0; i < dump->ioa_dump.next_page_index; i++)
3224                 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
3225
3226         vfree(dump->ioa_dump.ioa_data);
3227         kfree(dump);
3228         LEAVE;
3229 }
3230
3231 /**
3232  * ipr_worker_thread - Worker thread
3233  * @work:               ioa config struct
3234  *
3235  * Called at task level from a work thread. This function takes care
3236  * of adding and removing device from the mid-layer as configuration
3237  * changes are detected by the adapter.
3238  *
3239  * Return value:
3240  *      nothing
3241  **/
3242 static void ipr_worker_thread(struct work_struct *work)
3243 {
3244         unsigned long lock_flags;
3245         struct ipr_resource_entry *res;
3246         struct scsi_device *sdev;
3247         struct ipr_dump *dump;
3248         struct ipr_ioa_cfg *ioa_cfg =
3249                 container_of(work, struct ipr_ioa_cfg, work_q);
3250         u8 bus, target, lun;
3251         int did_work;
3252
3253         ENTER;
3254         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3255
3256         if (ioa_cfg->sdt_state == READ_DUMP) {
3257                 dump = ioa_cfg->dump;
3258                 if (!dump) {
3259                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3260                         return;
3261                 }
3262                 kref_get(&dump->kref);
3263                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3264                 ipr_get_ioa_dump(ioa_cfg, dump);
3265                 kref_put(&dump->kref, ipr_release_dump);
3266
3267                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3268                 if (ioa_cfg->sdt_state == DUMP_OBTAINED && !ioa_cfg->dump_timeout)
3269                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3270                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3271                 return;
3272         }
3273
3274 restart:
3275         do {
3276                 did_work = 0;
3277                 if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds) {
3278                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3279                         return;
3280                 }
3281
3282                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3283                         if (res->del_from_ml && res->sdev) {
3284                                 did_work = 1;
3285                                 sdev = res->sdev;
3286                                 if (!scsi_device_get(sdev)) {
3287                                         if (!res->add_to_ml)
3288                                                 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
3289                                         else
3290                                                 res->del_from_ml = 0;
3291                                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3292                                         scsi_remove_device(sdev);
3293                                         scsi_device_put(sdev);
3294                                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3295                                 }
3296                                 break;
3297                         }
3298                 }
3299         } while (did_work);
3300
3301         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3302                 if (res->add_to_ml) {
3303                         bus = res->bus;
3304                         target = res->target;
3305                         lun = res->lun;
3306                         res->add_to_ml = 0;
3307                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3308                         scsi_add_device(ioa_cfg->host, bus, target, lun);
3309                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3310                         goto restart;
3311                 }
3312         }
3313
3314         ioa_cfg->scan_done = 1;
3315         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3316         kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
3317         LEAVE;
3318 }
3319
3320 #ifdef CONFIG_SCSI_IPR_TRACE
3321 /**
3322  * ipr_read_trace - Dump the adapter trace
3323  * @filp:               open sysfs file
3324  * @kobj:               kobject struct
3325  * @bin_attr:           bin_attribute struct
3326  * @buf:                buffer
3327  * @off:                offset
3328  * @count:              buffer size
3329  *
3330  * Return value:
3331  *      number of bytes printed to buffer
3332  **/
3333 static ssize_t ipr_read_trace(struct file *filp, struct kobject *kobj,
3334                               struct bin_attribute *bin_attr,
3335                               char *buf, loff_t off, size_t count)
3336 {
3337         struct device *dev = container_of(kobj, struct device, kobj);
3338         struct Scsi_Host *shost = class_to_shost(dev);
3339         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3340         unsigned long lock_flags = 0;
3341         ssize_t ret;
3342
3343         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3344         ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
3345                                 IPR_TRACE_SIZE);
3346         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3347
3348         return ret;
3349 }
3350
3351 static struct bin_attribute ipr_trace_attr = {
3352         .attr = {
3353                 .name = "trace",
3354                 .mode = S_IRUGO,
3355         },
3356         .size = 0,
3357         .read = ipr_read_trace,
3358 };
3359 #endif
3360
3361 /**
3362  * ipr_show_fw_version - Show the firmware version
3363  * @dev:        class device struct
3364  * @buf:        buffer
3365  *
3366  * Return value:
3367  *      number of bytes printed to buffer
3368  **/
3369 static ssize_t ipr_show_fw_version(struct device *dev,
3370                                    struct device_attribute *attr, char *buf)
3371 {
3372         struct Scsi_Host *shost = class_to_shost(dev);
3373         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3374         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
3375         unsigned long lock_flags = 0;
3376         int len;
3377
3378         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3379         len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
3380                        ucode_vpd->major_release, ucode_vpd->card_type,
3381                        ucode_vpd->minor_release[0],
3382                        ucode_vpd->minor_release[1]);
3383         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3384         return len;
3385 }
3386
3387 static struct device_attribute ipr_fw_version_attr = {
3388         .attr = {
3389                 .name =         "fw_version",
3390                 .mode =         S_IRUGO,
3391         },
3392         .show = ipr_show_fw_version,
3393 };
3394
3395 /**
3396  * ipr_show_log_level - Show the adapter's error logging level
3397  * @dev:        class device struct
3398  * @buf:        buffer
3399  *
3400  * Return value:
3401  *      number of bytes printed to buffer
3402  **/
3403 static ssize_t ipr_show_log_level(struct device *dev,
3404                                    struct device_attribute *attr, char *buf)
3405 {
3406         struct Scsi_Host *shost = class_to_shost(dev);
3407         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3408         unsigned long lock_flags = 0;
3409         int len;
3410
3411         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3412         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
3413         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3414         return len;
3415 }
3416
3417 /**
3418  * ipr_store_log_level - Change the adapter's error logging level
3419  * @dev:        class device struct
3420  * @buf:        buffer
3421  *
3422  * Return value:
3423  *      number of bytes printed to buffer
3424  **/
3425 static ssize_t ipr_store_log_level(struct device *dev,
3426                                    struct device_attribute *attr,
3427                                    const char *buf, size_t count)
3428 {
3429         struct Scsi_Host *shost = class_to_shost(dev);
3430         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3431         unsigned long lock_flags = 0;
3432
3433         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3434         ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
3435         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3436         return strlen(buf);
3437 }
3438
3439 static struct device_attribute ipr_log_level_attr = {
3440         .attr = {
3441                 .name =         "log_level",
3442                 .mode =         S_IRUGO | S_IWUSR,
3443         },
3444         .show = ipr_show_log_level,
3445         .store = ipr_store_log_level
3446 };
3447
3448 /**
3449  * ipr_store_diagnostics - IOA Diagnostics interface
3450  * @dev:        device struct
3451  * @buf:        buffer
3452  * @count:      buffer size
3453  *
3454  * This function will reset the adapter and wait a reasonable
3455  * amount of time for any errors that the adapter might log.
3456  *
3457  * Return value:
3458  *      count on success / other on failure
3459  **/
3460 static ssize_t ipr_store_diagnostics(struct device *dev,
3461                                      struct device_attribute *attr,
3462                                      const char *buf, size_t count)
3463 {
3464         struct Scsi_Host *shost = class_to_shost(dev);
3465         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3466         unsigned long lock_flags = 0;
3467         int rc = count;
3468
3469         if (!capable(CAP_SYS_ADMIN))
3470                 return -EACCES;
3471
3472         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3473         while (ioa_cfg->in_reset_reload) {
3474                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3475                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3476                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3477         }
3478
3479         ioa_cfg->errors_logged = 0;
3480         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3481
3482         if (ioa_cfg->in_reset_reload) {
3483                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3484                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3485
3486                 /* Wait for a second for any errors to be logged */
3487                 msleep(1000);
3488         } else {
3489                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3490                 return -EIO;
3491         }
3492
3493         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3494         if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
3495                 rc = -EIO;
3496         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3497
3498         return rc;
3499 }
3500
3501 static struct device_attribute ipr_diagnostics_attr = {
3502         .attr = {
3503                 .name =         "run_diagnostics",
3504                 .mode =         S_IWUSR,
3505         },
3506         .store = ipr_store_diagnostics
3507 };
3508
3509 /**
3510  * ipr_show_adapter_state - Show the adapter's state
3511  * @class_dev:  device struct
3512  * @buf:        buffer
3513  *
3514  * Return value:
3515  *      number of bytes printed to buffer
3516  **/
3517 static ssize_t ipr_show_adapter_state(struct device *dev,
3518                                       struct device_attribute *attr, char *buf)
3519 {
3520         struct Scsi_Host *shost = class_to_shost(dev);
3521         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3522         unsigned long lock_flags = 0;
3523         int len;
3524
3525         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3526         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
3527                 len = snprintf(buf, PAGE_SIZE, "offline\n");
3528         else
3529                 len = snprintf(buf, PAGE_SIZE, "online\n");
3530         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3531         return len;
3532 }
3533
3534 /**
3535  * ipr_store_adapter_state - Change adapter state
3536  * @dev:        device struct
3537  * @buf:        buffer
3538  * @count:      buffer size
3539  *
3540  * This function will change the adapter's state.
3541  *
3542  * Return value:
3543  *      count on success / other on failure
3544  **/
3545 static ssize_t ipr_store_adapter_state(struct device *dev,
3546                                        struct device_attribute *attr,
3547                                        const char *buf, size_t count)
3548 {
3549         struct Scsi_Host *shost = class_to_shost(dev);
3550         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3551         unsigned long lock_flags;
3552         int result = count, i;
3553
3554         if (!capable(CAP_SYS_ADMIN))
3555                 return -EACCES;
3556
3557         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3558         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead &&
3559             !strncmp(buf, "online", 6)) {
3560                 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
3561                         spin_lock(&ioa_cfg->hrrq[i]._lock);
3562                         ioa_cfg->hrrq[i].ioa_is_dead = 0;
3563                         spin_unlock(&ioa_cfg->hrrq[i]._lock);
3564                 }
3565                 wmb();
3566                 ioa_cfg->reset_retries = 0;
3567                 ioa_cfg->in_ioa_bringdown = 0;
3568                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3569         }
3570         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3571         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3572
3573         return result;
3574 }
3575
3576 static struct device_attribute ipr_ioa_state_attr = {
3577         .attr = {
3578                 .name =         "online_state",
3579                 .mode =         S_IRUGO | S_IWUSR,
3580         },
3581         .show = ipr_show_adapter_state,
3582         .store = ipr_store_adapter_state
3583 };
3584
3585 /**
3586  * ipr_store_reset_adapter - Reset the adapter
3587  * @dev:        device struct
3588  * @buf:        buffer
3589  * @count:      buffer size
3590  *
3591  * This function will reset the adapter.
3592  *
3593  * Return value:
3594  *      count on success / other on failure
3595  **/
3596 static ssize_t ipr_store_reset_adapter(struct device *dev,
3597                                        struct device_attribute *attr,
3598                                        const char *buf, size_t count)
3599 {
3600         struct Scsi_Host *shost = class_to_shost(dev);
3601         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3602         unsigned long lock_flags;
3603         int result = count;
3604
3605         if (!capable(CAP_SYS_ADMIN))
3606                 return -EACCES;
3607
3608         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3609         if (!ioa_cfg->in_reset_reload)
3610                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3611         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3612         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3613
3614         return result;
3615 }
3616
3617 static struct device_attribute ipr_ioa_reset_attr = {
3618         .attr = {
3619                 .name =         "reset_host",
3620                 .mode =         S_IWUSR,
3621         },
3622         .store = ipr_store_reset_adapter
3623 };
3624
3625 static int ipr_iopoll(struct blk_iopoll *iop, int budget);
3626  /**
3627  * ipr_show_iopoll_weight - Show ipr polling mode
3628  * @dev:        class device struct
3629  * @buf:        buffer
3630  *
3631  * Return value:
3632  *      number of bytes printed to buffer
3633  **/
3634 static ssize_t ipr_show_iopoll_weight(struct device *dev,
3635                                    struct device_attribute *attr, char *buf)
3636 {
3637         struct Scsi_Host *shost = class_to_shost(dev);
3638         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3639         unsigned long lock_flags = 0;
3640         int len;
3641
3642         spin_lock_irqsave(shost->host_lock, lock_flags);
3643         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->iopoll_weight);
3644         spin_unlock_irqrestore(shost->host_lock, lock_flags);
3645
3646         return len;
3647 }
3648
3649 /**
3650  * ipr_store_iopoll_weight - Change the adapter's polling mode
3651  * @dev:        class device struct
3652  * @buf:        buffer
3653  *
3654  * Return value:
3655  *      number of bytes printed to buffer
3656  **/
3657 static ssize_t ipr_store_iopoll_weight(struct device *dev,
3658                                         struct device_attribute *attr,
3659                                         const char *buf, size_t count)
3660 {
3661         struct Scsi_Host *shost = class_to_shost(dev);
3662         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3663         unsigned long user_iopoll_weight;
3664         unsigned long lock_flags = 0;
3665         int i;
3666
3667         if (!ioa_cfg->sis64) {
3668                 dev_info(&ioa_cfg->pdev->dev, "blk-iopoll not supported on this adapter\n");
3669                 return -EINVAL;
3670         }
3671         if (kstrtoul(buf, 10, &user_iopoll_weight))
3672                 return -EINVAL;
3673
3674         if (user_iopoll_weight > 256) {
3675                 dev_info(&ioa_cfg->pdev->dev, "Invalid blk-iopoll weight. It must be less than 256\n");
3676                 return -EINVAL;
3677         }
3678
3679         if (user_iopoll_weight == ioa_cfg->iopoll_weight) {
3680                 dev_info(&ioa_cfg->pdev->dev, "Current blk-iopoll weight has the same weight\n");
3681                 return strlen(buf);
3682         }
3683
3684         if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
3685                 for (i = 1; i < ioa_cfg->hrrq_num; i++)
3686                         blk_iopoll_disable(&ioa_cfg->hrrq[i].iopoll);
3687         }
3688
3689         spin_lock_irqsave(shost->host_lock, lock_flags);
3690         ioa_cfg->iopoll_weight = user_iopoll_weight;
3691         if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
3692                 for (i = 1; i < ioa_cfg->hrrq_num; i++) {
3693                         blk_iopoll_init(&ioa_cfg->hrrq[i].iopoll,
3694                                         ioa_cfg->iopoll_weight, ipr_iopoll);
3695                         blk_iopoll_enable(&ioa_cfg->hrrq[i].iopoll);
3696                 }
3697         }
3698         spin_unlock_irqrestore(shost->host_lock, lock_flags);
3699
3700         return strlen(buf);
3701 }
3702
3703 static struct device_attribute ipr_iopoll_weight_attr = {
3704         .attr = {
3705                 .name =         "iopoll_weight",
3706                 .mode =         S_IRUGO | S_IWUSR,
3707         },
3708         .show = ipr_show_iopoll_weight,
3709         .store = ipr_store_iopoll_weight
3710 };
3711
3712 /**
3713  * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
3714  * @buf_len:            buffer length
3715  *
3716  * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3717  * list to use for microcode download
3718  *
3719  * Return value:
3720  *      pointer to sglist / NULL on failure
3721  **/
3722 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
3723 {
3724         int sg_size, order, bsize_elem, num_elem, i, j;
3725         struct ipr_sglist *sglist;
3726         struct scatterlist *scatterlist;
3727         struct page *page;
3728
3729         /* Get the minimum size per scatter/gather element */
3730         sg_size = buf_len / (IPR_MAX_SGLIST - 1);
3731
3732         /* Get the actual size per element */
3733         order = get_order(sg_size);
3734
3735         /* Determine the actual number of bytes per element */
3736         bsize_elem = PAGE_SIZE * (1 << order);
3737
3738         /* Determine the actual number of sg entries needed */
3739         if (buf_len % bsize_elem)
3740                 num_elem = (buf_len / bsize_elem) + 1;
3741         else
3742                 num_elem = buf_len / bsize_elem;
3743
3744         /* Allocate a scatter/gather list for the DMA */
3745         sglist = kzalloc(sizeof(struct ipr_sglist) +
3746                          (sizeof(struct scatterlist) * (num_elem - 1)),
3747                          GFP_KERNEL);
3748
3749         if (sglist == NULL) {
3750                 ipr_trace;
3751                 return NULL;
3752         }
3753
3754         scatterlist = sglist->scatterlist;
3755         sg_init_table(scatterlist, num_elem);
3756
3757         sglist->order = order;
3758         sglist->num_sg = num_elem;
3759
3760         /* Allocate a bunch of sg elements */
3761         for (i = 0; i < num_elem; i++) {
3762                 page = alloc_pages(GFP_KERNEL, order);
3763                 if (!page) {
3764                         ipr_trace;
3765
3766                         /* Free up what we already allocated */
3767                         for (j = i - 1; j >= 0; j--)
3768                                 __free_pages(sg_page(&scatterlist[j]), order);
3769                         kfree(sglist);
3770                         return NULL;
3771                 }
3772
3773                 sg_set_page(&scatterlist[i], page, 0, 0);
3774         }
3775
3776         return sglist;
3777 }
3778
3779 /**
3780  * ipr_free_ucode_buffer - Frees a microcode download buffer
3781  * @p_dnld:             scatter/gather list pointer
3782  *
3783  * Free a DMA'able ucode download buffer previously allocated with
3784  * ipr_alloc_ucode_buffer
3785  *
3786  * Return value:
3787  *      nothing
3788  **/
3789 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
3790 {
3791         int i;
3792
3793         for (i = 0; i < sglist->num_sg; i++)
3794                 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
3795
3796         kfree(sglist);
3797 }
3798
3799 /**
3800  * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
3801  * @sglist:             scatter/gather list pointer
3802  * @buffer:             buffer pointer
3803  * @len:                buffer length
3804  *
3805  * Copy a microcode image from a user buffer into a buffer allocated by
3806  * ipr_alloc_ucode_buffer
3807  *
3808  * Return value:
3809  *      0 on success / other on failure
3810  **/
3811 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
3812                                  u8 *buffer, u32 len)
3813 {
3814         int bsize_elem, i, result = 0;
3815         struct scatterlist *scatterlist;
3816         void *kaddr;
3817
3818         /* Determine the actual number of bytes per element */
3819         bsize_elem = PAGE_SIZE * (1 << sglist->order);
3820
3821         scatterlist = sglist->scatterlist;
3822
3823         for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
3824                 struct page *page = sg_page(&scatterlist[i]);
3825
3826                 kaddr = kmap(page);
3827                 memcpy(kaddr, buffer, bsize_elem);
3828                 kunmap(page);
3829
3830                 scatterlist[i].length = bsize_elem;
3831
3832                 if (result != 0) {
3833                         ipr_trace;
3834                         return result;
3835                 }
3836         }
3837
3838         if (len % bsize_elem) {
3839                 struct page *page = sg_page(&scatterlist[i]);
3840
3841                 kaddr = kmap(page);
3842                 memcpy(kaddr, buffer, len % bsize_elem);
3843                 kunmap(page);
3844
3845                 scatterlist[i].length = len % bsize_elem;
3846         }
3847
3848         sglist->buffer_len = len;
3849         return result;
3850 }
3851
3852 /**
3853  * ipr_build_ucode_ioadl64 - Build a microcode download IOADL
3854  * @ipr_cmd:            ipr command struct
3855  * @sglist:             scatter/gather list
3856  *
3857  * Builds a microcode download IOA data list (IOADL).
3858  *
3859  **/
3860 static void ipr_build_ucode_ioadl64(struct ipr_cmnd *ipr_cmd,
3861                                     struct ipr_sglist *sglist)
3862 {
3863         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3864         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
3865         struct scatterlist *scatterlist = sglist->scatterlist;
3866         int i;
3867
3868         ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3869         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3870         ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3871
3872         ioarcb->ioadl_len =
3873                 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
3874         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3875                 ioadl64[i].flags = cpu_to_be32(IPR_IOADL_FLAGS_WRITE);
3876                 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(&scatterlist[i]));
3877                 ioadl64[i].address = cpu_to_be64(sg_dma_address(&scatterlist[i]));
3878         }
3879
3880         ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3881 }
3882
3883 /**
3884  * ipr_build_ucode_ioadl - Build a microcode download IOADL
3885  * @ipr_cmd:    ipr command struct
3886  * @sglist:             scatter/gather list
3887  *
3888  * Builds a microcode download IOA data list (IOADL).
3889  *
3890  **/
3891 static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
3892                                   struct ipr_sglist *sglist)
3893 {
3894         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3895         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
3896         struct scatterlist *scatterlist = sglist->scatterlist;
3897         int i;
3898
3899         ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3900         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3901         ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3902
3903         ioarcb->ioadl_len =
3904                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3905
3906         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3907                 ioadl[i].flags_and_data_len =
3908                         cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3909                 ioadl[i].address =
3910                         cpu_to_be32(sg_dma_address(&scatterlist[i]));
3911         }
3912
3913         ioadl[i-1].flags_and_data_len |=
3914                 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3915 }
3916
3917 /**
3918  * ipr_update_ioa_ucode - Update IOA's microcode
3919  * @ioa_cfg:    ioa config struct
3920  * @sglist:             scatter/gather list
3921  *
3922  * Initiate an adapter reset to update the IOA's microcode
3923  *
3924  * Return value:
3925  *      0 on success / -EIO on failure
3926  **/
3927 static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3928                                 struct ipr_sglist *sglist)
3929 {
3930         unsigned long lock_flags;
3931
3932         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3933         while (ioa_cfg->in_reset_reload) {
3934                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3935                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3936                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3937         }
3938
3939         if (ioa_cfg->ucode_sglist) {
3940                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3941                 dev_err(&ioa_cfg->pdev->dev,
3942                         "Microcode download already in progress\n");
3943                 return -EIO;
3944         }
3945
3946         sglist->num_dma_sg = dma_map_sg(&ioa_cfg->pdev->dev,
3947                                         sglist->scatterlist, sglist->num_sg,
3948                                         DMA_TO_DEVICE);
3949
3950         if (!sglist->num_dma_sg) {
3951                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3952                 dev_err(&ioa_cfg->pdev->dev,
3953                         "Failed to map microcode download buffer!\n");
3954                 return -EIO;
3955         }
3956
3957         ioa_cfg->ucode_sglist = sglist;
3958         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3959         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3960         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3961
3962         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3963         ioa_cfg->ucode_sglist = NULL;
3964         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3965         return 0;
3966 }
3967
3968 /**
3969  * ipr_store_update_fw - Update the firmware on the adapter
3970  * @class_dev:  device struct
3971  * @buf:        buffer
3972  * @count:      buffer size
3973  *
3974  * This function will update the firmware on the adapter.
3975  *
3976  * Return value:
3977  *      count on success / other on failure
3978  **/
3979 static ssize_t ipr_store_update_fw(struct device *dev,
3980                                    struct device_attribute *attr,
3981                                    const char *buf, size_t count)
3982 {
3983         struct Scsi_Host *shost = class_to_shost(dev);
3984         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3985         struct ipr_ucode_image_header *image_hdr;
3986         const struct firmware *fw_entry;
3987         struct ipr_sglist *sglist;
3988         char fname[100];
3989         char *src;
3990         int len, result, dnld_size;
3991
3992         if (!capable(CAP_SYS_ADMIN))
3993                 return -EACCES;
3994
3995         len = snprintf(fname, 99, "%s", buf);
3996         fname[len-1] = '\0';
3997
3998         if (request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3999                 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
4000                 return -EIO;
4001         }
4002
4003         image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
4004
4005         src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
4006         dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
4007         sglist = ipr_alloc_ucode_buffer(dnld_size);
4008
4009         if (!sglist) {
4010                 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
4011                 release_firmware(fw_entry);
4012                 return -ENOMEM;
4013         }
4014
4015         result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
4016
4017         if (result) {
4018                 dev_err(&ioa_cfg->pdev->dev,
4019                         "Microcode buffer copy to DMA buffer failed\n");
4020                 goto out;
4021         }
4022
4023         ipr_info("Updating microcode, please be patient.  This may take up to 30 minutes.\n");
4024
4025         result = ipr_update_ioa_ucode(ioa_cfg, sglist);
4026
4027         if (!result)
4028                 result = count;
4029 out:
4030         ipr_free_ucode_buffer(sglist);
4031         release_firmware(fw_entry);
4032         return result;
4033 }
4034
4035 static struct device_attribute ipr_update_fw_attr = {
4036         .attr = {
4037                 .name =         "update_fw",
4038                 .mode =         S_IWUSR,
4039         },
4040         .store = ipr_store_update_fw
4041 };
4042
4043 /**
4044  * ipr_show_fw_type - Show the adapter's firmware type.
4045  * @dev:        class device struct
4046  * @buf:        buffer
4047  *
4048  * Return value:
4049  *      number of bytes printed to buffer
4050  **/
4051 static ssize_t ipr_show_fw_type(struct device *dev,
4052                                 struct device_attribute *attr, char *buf)
4053 {
4054         struct Scsi_Host *shost = class_to_shost(dev);
4055         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
4056         unsigned long lock_flags = 0;
4057         int len;
4058
4059         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4060         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->sis64);
4061         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4062         return len;
4063 }
4064
4065 static struct device_attribute ipr_ioa_fw_type_attr = {
4066         .attr = {
4067                 .name =         "fw_type",
4068                 .mode =         S_IRUGO,
4069         },
4070         .show = ipr_show_fw_type
4071 };
4072
4073 static struct device_attribute *ipr_ioa_attrs[] = {
4074         &ipr_fw_version_attr,
4075         &ipr_log_level_attr,
4076         &ipr_diagnostics_attr,
4077         &ipr_ioa_state_attr,
4078         &ipr_ioa_reset_attr,
4079         &ipr_update_fw_attr,
4080         &ipr_ioa_fw_type_attr,
4081         &ipr_iopoll_weight_attr,
4082         NULL,
4083 };
4084
4085 #ifdef CONFIG_SCSI_IPR_DUMP
4086 /**
4087  * ipr_read_dump - Dump the adapter
4088  * @filp:               open sysfs file
4089  * @kobj:               kobject struct
4090  * @bin_attr:           bin_attribute struct
4091  * @buf:                buffer
4092  * @off:                offset
4093  * @count:              buffer size
4094  *
4095  * Return value:
4096  *      number of bytes printed to buffer
4097  **/
4098 static ssize_t ipr_read_dump(struct file *filp, struct kobject *kobj,
4099                              struct bin_attribute *bin_attr,
4100                              char *buf, loff_t off, size_t count)
4101 {
4102         struct device *cdev = container_of(kobj, struct device, kobj);
4103         struct Scsi_Host *shost = class_to_shost(cdev);
4104         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
4105         struct ipr_dump *dump;
4106         unsigned long lock_flags = 0;
4107         char *src;
4108         int len, sdt_end;
4109         size_t rc = count;
4110
4111         if (!capable(CAP_SYS_ADMIN))
4112                 return -EACCES;
4113
4114         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4115         dump = ioa_cfg->dump;
4116
4117         if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
4118                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4119                 return 0;
4120         }
4121         kref_get(&dump->kref);
4122         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4123
4124         if (off > dump->driver_dump.hdr.len) {
4125                 kref_put(&dump->kref, ipr_release_dump);
4126                 return 0;
4127         }
4128
4129         if (off + count > dump->driver_dump.hdr.len) {
4130                 count = dump->driver_dump.hdr.len - off;
4131                 rc = count;
4132         }
4133
4134         if (count && off < sizeof(dump->driver_dump)) {
4135                 if (off + count > sizeof(dump->driver_dump))
4136                         len = sizeof(dump->driver_dump) - off;
4137                 else
4138                         len = count;
4139                 src = (u8 *)&dump->driver_dump + off;
4140                 memcpy(buf, src, len);
4141                 buf += len;
4142                 off += len;
4143                 count -= len;
4144         }
4145
4146         off -= sizeof(dump->driver_dump);
4147
4148         if (ioa_cfg->sis64)
4149                 sdt_end = offsetof(struct ipr_ioa_dump, sdt.entry) +
4150                           (be32_to_cpu(dump->ioa_dump.sdt.hdr.num_entries_used) *
4151                            sizeof(struct ipr_sdt_entry));
4152         else
4153                 sdt_end = offsetof(struct ipr_ioa_dump, sdt.entry) +
4154                           (IPR_FMT2_NUM_SDT_ENTRIES * sizeof(struct ipr_sdt_entry));
4155
4156         if (count && off < sdt_end) {
4157                 if (off + count > sdt_end)
4158                         len = sdt_end - off;
4159                 else
4160                         len = count;
4161                 src = (u8 *)&dump->ioa_dump + off;
4162                 memcpy(buf, src, len);
4163                 buf += len;
4164                 off += len;
4165                 count -= len;
4166         }
4167
4168         off -= sdt_end;
4169
4170         while (count) {
4171                 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
4172                         len = PAGE_ALIGN(off) - off;
4173                 else
4174                         len = count;
4175                 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
4176                 src += off & ~PAGE_MASK;
4177                 memcpy(buf, src, len);
4178                 buf += len;
4179                 off += len;
4180                 count -= len;
4181         }
4182
4183         kref_put(&dump->kref, ipr_release_dump);
4184         return rc;
4185 }
4186
4187 /**
4188  * ipr_alloc_dump - Prepare for adapter dump
4189  * @ioa_cfg:    ioa config struct
4190  *
4191  * Return value:
4192  *      0 on success / other on failure
4193  **/
4194 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
4195 {
4196         struct ipr_dump *dump;
4197         __be32 **ioa_data;
4198         unsigned long lock_flags = 0;
4199
4200         dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
4201
4202         if (!dump) {
4203                 ipr_err("Dump memory allocation failed\n");
4204                 return -ENOMEM;
4205         }
4206
4207         if (ioa_cfg->sis64)
4208                 ioa_data = vmalloc(IPR_FMT3_MAX_NUM_DUMP_PAGES * sizeof(__be32 *));
4209         else
4210                 ioa_data = vmalloc(IPR_FMT2_MAX_NUM_DUMP_PAGES * sizeof(__be32 *));
4211
4212         if (!ioa_data) {
4213                 ipr_err("Dump memory allocation failed\n");
4214                 kfree(dump);
4215                 return -ENOMEM;
4216         }
4217
4218         dump->ioa_dump.ioa_data = ioa_data;
4219
4220         kref_init(&dump->kref);
4221         dump->ioa_cfg = ioa_cfg;
4222
4223         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4224
4225         if (INACTIVE != ioa_cfg->sdt_state) {
4226                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4227                 vfree(dump->ioa_dump.ioa_data);
4228                 kfree(dump);
4229                 return 0;
4230         }
4231
4232         ioa_cfg->dump = dump;
4233         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
4234         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead && !ioa_cfg->dump_taken) {
4235                 ioa_cfg->dump_taken = 1;
4236                 schedule_work(&ioa_cfg->work_q);
4237         }
4238         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4239
4240         return 0;
4241 }
4242
4243 /**
4244  * ipr_free_dump - Free adapter dump memory
4245  * @ioa_cfg:    ioa config struct
4246  *
4247  * Return value:
4248  *      0 on success / other on failure
4249  **/
4250 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
4251 {
4252         struct ipr_dump *dump;
4253         unsigned long lock_flags = 0;
4254
4255         ENTER;
4256
4257         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4258         dump = ioa_cfg->dump;
4259         if (!dump) {
4260                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4261                 return 0;
4262         }
4263
4264         ioa_cfg->dump = NULL;
4265         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4266
4267         kref_put(&dump->kref, ipr_release_dump);
4268
4269         LEAVE;
4270         return 0;
4271 }
4272
4273 /**
4274  * ipr_write_dump - Setup dump state of adapter
4275  * @filp:               open sysfs file
4276  * @kobj:               kobject struct
4277  * @bin_attr:           bin_attribute struct
4278  * @buf:                buffer
4279  * @off:                offset
4280  * @count:              buffer size
4281  *
4282  * Return value:
4283  *      number of bytes printed to buffer
4284  **/
4285 static ssize_t ipr_write_dump(struct file *filp, struct kobject *kobj,
4286                               struct bin_attribute *bin_attr,
4287                               char *buf, loff_t off, size_t count)
4288 {
4289         struct device *cdev = container_of(kobj, struct device, kobj);
4290         struct Scsi_Host *shost = class_to_shost(cdev);
4291         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
4292         int rc;
4293
4294         if (!capable(CAP_SYS_ADMIN))
4295                 return -EACCES;
4296
4297         if (buf[0] == '1')
4298                 rc = ipr_alloc_dump(ioa_cfg);
4299         else if (buf[0] == '0')
4300                 rc = ipr_free_dump(ioa_cfg);
4301         else
4302                 return -EINVAL;
4303
4304         if (rc)
4305                 return rc;
4306         else
4307                 return count;
4308 }
4309
4310 static struct bin_attribute ipr_dump_attr = {
4311         .attr = {
4312                 .name = "dump",
4313                 .mode = S_IRUSR | S_IWUSR,
4314         },
4315         .size = 0,
4316         .read = ipr_read_dump,
4317         .write = ipr_write_dump
4318 };
4319 #else
4320 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
4321 #endif
4322
4323 /**
4324  * ipr_change_queue_depth - Change the device's queue depth
4325  * @sdev:       scsi device struct
4326  * @qdepth:     depth to set
4327  * @reason:     calling context
4328  *
4329  * Return value:
4330  *      actual depth set
4331  **/
4332 static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
4333 {
4334         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4335         struct ipr_resource_entry *res;
4336         unsigned long lock_flags = 0;
4337
4338         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4339         res = (struct ipr_resource_entry *)sdev->hostdata;
4340
4341         if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
4342                 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
4343         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4344
4345         scsi_change_queue_depth(sdev, qdepth);
4346         return sdev->queue_depth;
4347 }
4348
4349 /**
4350  * ipr_show_adapter_handle - Show the adapter's resource handle for this device
4351  * @dev:        device struct
4352  * @attr:       device attribute structure
4353  * @buf:        buffer
4354  *
4355  * Return value:
4356  *      number of bytes printed to buffer
4357  **/
4358 static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
4359 {
4360         struct scsi_device *sdev = to_scsi_device(dev);
4361         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4362         struct ipr_resource_entry *res;
4363         unsigned long lock_flags = 0;
4364         ssize_t len = -ENXIO;
4365
4366         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4367         res = (struct ipr_resource_entry *)sdev->hostdata;
4368         if (res)
4369                 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->res_handle);
4370         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4371         return len;
4372 }
4373
4374 static struct device_attribute ipr_adapter_handle_attr = {
4375         .attr = {
4376                 .name =         "adapter_handle",
4377                 .mode =         S_IRUSR,
4378         },
4379         .show = ipr_show_adapter_handle
4380 };
4381
4382 /**
4383  * ipr_show_resource_path - Show the resource path or the resource address for
4384  *                          this device.
4385  * @dev:        device struct
4386  * @attr:       device attribute structure
4387  * @buf:        buffer
4388  *
4389  * Return value:
4390  *      number of bytes printed to buffer
4391  **/
4392 static ssize_t ipr_show_resource_path(struct device *dev, struct device_attribute *attr, char *buf)
4393 {
4394         struct scsi_device *sdev = to_scsi_device(dev);
4395         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4396         struct ipr_resource_entry *res;
4397         unsigned long lock_flags = 0;
4398         ssize_t len = -ENXIO;
4399         char buffer[IPR_MAX_RES_PATH_LENGTH];
4400
4401         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4402         res = (struct ipr_resource_entry *)sdev->hostdata;
4403         if (res && ioa_cfg->sis64)
4404                 len = snprintf(buf, PAGE_SIZE, "%s\n",
4405                                __ipr_format_res_path(res->res_path, buffer,
4406                                                      sizeof(buffer)));
4407         else if (res)
4408                 len = snprintf(buf, PAGE_SIZE, "%d:%d:%d:%d\n", ioa_cfg->host->host_no,
4409                                res->bus, res->target, res->lun);
4410
4411         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4412         return len;
4413 }
4414
4415 static struct device_attribute ipr_resource_path_attr = {
4416         .attr = {
4417                 .name =         "resource_path",
4418                 .mode =         S_IRUGO,
4419         },
4420         .show = ipr_show_resource_path
4421 };
4422
4423 /**
4424  * ipr_show_device_id - Show the device_id for this device.
4425  * @dev:        device struct
4426  * @attr:       device attribute structure
4427  * @buf:        buffer
4428  *
4429  * Return value:
4430  *      number of bytes printed to buffer
4431  **/
4432 static ssize_t ipr_show_device_id(struct device *dev, struct device_attribute *attr, char *buf)
4433 {
4434         struct scsi_device *sdev = to_scsi_device(dev);
4435         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4436         struct ipr_resource_entry *res;
4437         unsigned long lock_flags = 0;
4438         ssize_t len = -ENXIO;
4439
4440         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4441         res = (struct ipr_resource_entry *)sdev->hostdata;
4442         if (res && ioa_cfg->sis64)
4443                 len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->dev_id);
4444         else if (res)
4445                 len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->lun_wwn);
4446
4447         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4448         return len;
4449 }
4450
4451 static struct device_attribute ipr_device_id_attr = {
4452         .attr = {
4453                 .name =         "device_id",
4454                 .mode =         S_IRUGO,
4455         },
4456         .show = ipr_show_device_id
4457 };
4458
4459 /**
4460  * ipr_show_resource_type - Show the resource type for this device.
4461  * @dev:        device struct
4462  * @attr:       device attribute structure
4463  * @buf:        buffer
4464  *
4465  * Return value:
4466  *      number of bytes printed to buffer
4467  **/
4468 static ssize_t ipr_show_resource_type(struct device *dev, struct device_attribute *attr, char *buf)
4469 {
4470         struct scsi_device *sdev = to_scsi_device(dev);
4471         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4472         struct ipr_resource_entry *res;
4473         unsigned long lock_flags = 0;
4474         ssize_t len = -ENXIO;
4475
4476         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4477         res = (struct ipr_resource_entry *)sdev->hostdata;
4478
4479         if (res)
4480                 len = snprintf(buf, PAGE_SIZE, "%x\n", res->type);
4481
4482         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4483         return len;
4484 }
4485
4486 static struct device_attribute ipr_resource_type_attr = {
4487         .attr = {
4488                 .name =         "resource_type",
4489                 .mode =         S_IRUGO,
4490         },
4491         .show = ipr_show_resource_type
4492 };
4493
4494 static struct device_attribute *ipr_dev_attrs[] = {
4495         &ipr_adapter_handle_attr,
4496         &ipr_resource_path_attr,
4497         &ipr_device_id_attr,
4498         &ipr_resource_type_attr,
4499         NULL,
4500 };
4501
4502 /**
4503  * ipr_biosparam - Return the HSC mapping
4504  * @sdev:                       scsi device struct
4505  * @block_device:       block device pointer
4506  * @capacity:           capacity of the device
4507  * @parm:                       Array containing returned HSC values.
4508  *
4509  * This function generates the HSC parms that fdisk uses.
4510  * We want to make sure we return something that places partitions
4511  * on 4k boundaries for best performance with the IOA.
4512  *
4513  * Return value:
4514  *      0 on success
4515  **/
4516 static int ipr_biosparam(struct scsi_device *sdev,
4517                          struct block_device *block_device,
4518                          sector_t capacity, int *parm)
4519 {
4520         int heads, sectors;
4521         sector_t cylinders;
4522
4523         heads = 128;
4524         sectors = 32;
4525
4526         cylinders = capacity;
4527         sector_div(cylinders, (128 * 32));
4528
4529         /* return result */
4530         parm[0] = heads;
4531         parm[1] = sectors;
4532         parm[2] = cylinders;
4533
4534         return 0;
4535 }
4536
4537 /**
4538  * ipr_find_starget - Find target based on bus/target.
4539  * @starget:    scsi target struct
4540  *
4541  * Return value:
4542  *      resource entry pointer if found / NULL if not found
4543  **/
4544 static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
4545 {
4546         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4547         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4548         struct ipr_resource_entry *res;
4549
4550         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4551                 if ((res->bus == starget->channel) &&
4552                     (res->target == starget->id)) {
4553                         return res;
4554                 }
4555         }
4556
4557         return NULL;
4558 }
4559
4560 static struct ata_port_info sata_port_info;
4561
4562 /**
4563  * ipr_target_alloc - Prepare for commands to a SCSI target
4564  * @starget:    scsi target struct
4565  *
4566  * If the device is a SATA device, this function allocates an
4567  * ATA port with libata, else it does nothing.
4568  *
4569  * Return value:
4570  *      0 on success / non-0 on failure
4571  **/
4572 static int ipr_target_alloc(struct scsi_target *starget)
4573 {
4574         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4575         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4576         struct ipr_sata_port *sata_port;
4577         struct ata_port *ap;
4578         struct ipr_resource_entry *res;
4579         unsigned long lock_flags;
4580
4581         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4582         res = ipr_find_starget(starget);
4583         starget->hostdata = NULL;
4584
4585         if (res && ipr_is_gata(res)) {
4586                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4587                 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
4588                 if (!sata_port)
4589                         return -ENOMEM;
4590
4591                 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
4592                 if (ap) {
4593                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4594                         sata_port->ioa_cfg = ioa_cfg;
4595                         sata_port->ap = ap;
4596                         sata_port->res = res;
4597
4598                         res->sata_port = sata_port;
4599                         ap->private_data = sata_port;
4600                         starget->hostdata = sata_port;
4601                 } else {
4602                         kfree(sata_port);
4603                         return -ENOMEM;
4604                 }
4605         }
4606         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4607
4608         return 0;
4609 }
4610
4611 /**
4612  * ipr_target_destroy - Destroy a SCSI target
4613  * @starget:    scsi target struct
4614  *
4615  * If the device was a SATA device, this function frees the libata
4616  * ATA port, else it does nothing.
4617  *
4618  **/
4619 static void ipr_target_destroy(struct scsi_target *starget)
4620 {
4621         struct ipr_sata_port *sata_port = starget->hostdata;
4622         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4623         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4624
4625         if (ioa_cfg->sis64) {
4626                 if (!ipr_find_starget(starget)) {
4627                         if (starget->channel == IPR_ARRAY_VIRTUAL_BUS)
4628                                 clear_bit(starget->id, ioa_cfg->array_ids);
4629                         else if (starget->channel == IPR_VSET_VIRTUAL_BUS)
4630                                 clear_bit(starget->id, ioa_cfg->vset_ids);
4631                         else if (starget->channel == 0)
4632                                 clear_bit(starget->id, ioa_cfg->target_ids);
4633                 }
4634         }
4635
4636         if (sata_port) {
4637                 starget->hostdata = NULL;
4638                 ata_sas_port_destroy(sata_port->ap);
4639                 kfree(sata_port);
4640         }
4641 }
4642
4643 /**
4644  * ipr_find_sdev - Find device based on bus/target/lun.
4645  * @sdev:       scsi device struct
4646  *
4647  * Return value:
4648  *      resource entry pointer if found / NULL if not found
4649  **/
4650 static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
4651 {
4652         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4653         struct ipr_resource_entry *res;
4654
4655         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4656                 if ((res->bus == sdev->channel) &&
4657                     (res->target == sdev->id) &&
4658                     (res->lun == sdev->lun))
4659                         return res;
4660         }
4661
4662         return NULL;
4663 }
4664
4665 /**
4666  * ipr_slave_destroy - Unconfigure a SCSI device
4667  * @sdev:       scsi device struct
4668  *
4669  * Return value:
4670  *      nothing
4671  **/
4672 static void ipr_slave_destroy(struct scsi_device *sdev)
4673 {
4674         struct ipr_resource_entry *res;
4675         struct ipr_ioa_cfg *ioa_cfg;
4676         unsigned long lock_flags = 0;
4677
4678         ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4679
4680         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4681         res = (struct ipr_resource_entry *) sdev->hostdata;
4682         if (res) {
4683                 if (res->sata_port)
4684                         res->sata_port->ap->link.device[0].class = ATA_DEV_NONE;
4685                 sdev->hostdata = NULL;
4686                 res->sdev = NULL;
4687                 res->sata_port = NULL;
4688         }
4689         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4690 }
4691
4692 /**
4693  * ipr_slave_configure - Configure a SCSI device
4694  * @sdev:       scsi device struct
4695  *
4696  * This function configures the specified scsi device.
4697  *
4698  * Return value:
4699  *      0 on success
4700  **/
4701 static int ipr_slave_configure(struct scsi_device *sdev)
4702 {
4703         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4704         struct ipr_resource_entry *res;
4705         struct ata_port *ap = NULL;
4706         unsigned long lock_flags = 0;
4707         char buffer[IPR_MAX_RES_PATH_LENGTH];
4708
4709         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4710         res = sdev->hostdata;
4711         if (res) {
4712                 if (ipr_is_af_dasd_device(res))
4713                         sdev->type = TYPE_RAID;
4714                 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
4715                         sdev->scsi_level = 4;
4716                         sdev->no_uld_attach = 1;
4717                 }
4718                 if (ipr_is_vset_device(res)) {
4719                         sdev->scsi_level = SCSI_SPC_3;
4720                         blk_queue_rq_timeout(sdev->request_queue,
4721                                              IPR_VSET_RW_TIMEOUT);
4722                         blk_queue_max_hw_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
4723                 }
4724                 if (ipr_is_gata(res) && res->sata_port)
4725                         ap = res->sata_port->ap;
4726                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4727
4728                 if (ap) {
4729                         scsi_change_queue_depth(sdev, IPR_MAX_CMD_PER_ATA_LUN);
4730                         ata_sas_slave_configure(sdev, ap);
4731                 }
4732
4733                 if (ioa_cfg->sis64)
4734                         sdev_printk(KERN_INFO, sdev, "Resource path: %s\n",
4735                                     ipr_format_res_path(ioa_cfg,
4736                                 res->res_path, buffer, sizeof(buffer)));
4737                 return 0;
4738         }
4739         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4740         return 0;
4741 }
4742
4743 /**
4744  * ipr_ata_slave_alloc - Prepare for commands to a SATA device
4745  * @sdev:       scsi device struct
4746  *
4747  * This function initializes an ATA port so that future commands
4748  * sent through queuecommand will work.
4749  *
4750  * Return value:
4751  *      0 on success
4752  **/
4753 static int ipr_ata_slave_alloc(struct scsi_device *sdev)
4754 {
4755         struct ipr_sata_port *sata_port = NULL;
4756         int rc = -ENXIO;
4757
4758         ENTER;
4759         if (sdev->sdev_target)
4760                 sata_port = sdev->sdev_target->hostdata;
4761         if (sata_port) {
4762                 rc = ata_sas_port_init(sata_port->ap);
4763                 if (rc == 0)
4764                         rc = ata_sas_sync_probe(sata_port->ap);
4765         }
4766
4767         if (rc)
4768                 ipr_slave_destroy(sdev);
4769
4770         LEAVE;
4771         return rc;
4772 }
4773
4774 /**
4775  * ipr_slave_alloc - Prepare for commands to a device.
4776  * @sdev:       scsi device struct
4777  *
4778  * This function saves a pointer to the resource entry
4779  * in the scsi device struct if the device exists. We
4780  * can then use this pointer in ipr_queuecommand when
4781  * handling new commands.
4782  *
4783  * Return value:
4784  *      0 on success / -ENXIO if device does not exist
4785  **/
4786 static int ipr_slave_alloc(struct scsi_device *sdev)
4787 {
4788         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4789         struct ipr_resource_entry *res;
4790         unsigned long lock_flags;
4791         int rc = -ENXIO;
4792
4793         sdev->hostdata = NULL;
4794
4795         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4796
4797         res = ipr_find_sdev(sdev);
4798         if (res) {
4799                 res->sdev = sdev;
4800                 res->add_to_ml = 0;
4801                 res->in_erp = 0;
4802                 sdev->hostdata = res;
4803                 if (!ipr_is_naca_model(res))
4804                         res->needs_sync_complete = 1;
4805                 rc = 0;
4806                 if (ipr_is_gata(res)) {
4807                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4808                         return ipr_ata_slave_alloc(sdev);
4809                 }
4810         }
4811
4812         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4813
4814         return rc;
4815 }
4816
4817 /**
4818  * ipr_match_lun - Match function for specified LUN
4819  * @ipr_cmd:    ipr command struct
4820  * @device:             device to match (sdev)
4821  *
4822  * Returns:
4823  *      1 if command matches sdev / 0 if command does not match sdev
4824  **/
4825 static int ipr_match_lun(struct ipr_cmnd *ipr_cmd, void *device)
4826 {
4827         if (ipr_cmd->scsi_cmd && ipr_cmd->scsi_cmd->device == device)
4828                 return 1;
4829         return 0;
4830 }
4831
4832 /**
4833  * ipr_wait_for_ops - Wait for matching commands to complete
4834  * @ipr_cmd:    ipr command struct
4835  * @device:             device to match (sdev)
4836  * @match:              match function to use
4837  *
4838  * Returns:
4839  *      SUCCESS / FAILED
4840  **/
4841 static int ipr_wait_for_ops(struct ipr_ioa_cfg *ioa_cfg, void *device,
4842                             int (*match)(struct ipr_cmnd *, void *))
4843 {
4844         struct ipr_cmnd *ipr_cmd;
4845         int wait;
4846         unsigned long flags;
4847         struct ipr_hrr_queue *hrrq;
4848         signed long timeout = IPR_ABORT_TASK_TIMEOUT;
4849         DECLARE_COMPLETION_ONSTACK(comp);
4850
4851         ENTER;
4852         do {
4853                 wait = 0;
4854
4855                 for_each_hrrq(hrrq, ioa_cfg) {
4856                         spin_lock_irqsave(hrrq->lock, flags);
4857                         list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
4858                                 if (match(ipr_cmd, device)) {
4859                                         ipr_cmd->eh_comp = &comp;
4860                                         wait++;
4861                                 }
4862                         }
4863                         spin_unlock_irqrestore(hrrq->lock, flags);
4864                 }
4865
4866                 if (wait) {
4867                         timeout = wait_for_completion_timeout(&comp, timeout);
4868
4869                         if (!timeout) {
4870                                 wait = 0;
4871
4872                                 for_each_hrrq(hrrq, ioa_cfg) {
4873                                         spin_lock_irqsave(hrrq->lock, flags);
4874                                         list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
4875                                                 if (match(ipr_cmd, device)) {
4876                                                         ipr_cmd->eh_comp = NULL;
4877                                                         wait++;
4878                                                 }
4879                                         }
4880                                         spin_unlock_irqrestore(hrrq->lock, flags);
4881                                 }
4882
4883                                 if (wait)
4884                                         dev_err(&ioa_cfg->pdev->dev, "Timed out waiting for aborted commands\n");
4885                                 LEAVE;
4886                                 return wait ? FAILED : SUCCESS;
4887                         }
4888                 }
4889         } while (wait);
4890
4891         LEAVE;
4892         return SUCCESS;
4893 }
4894
4895 static int ipr_eh_host_reset(struct scsi_cmnd *cmd)
4896 {
4897         struct ipr_ioa_cfg *ioa_cfg;
4898         unsigned long lock_flags = 0;
4899         int rc = SUCCESS;
4900
4901         ENTER;
4902         ioa_cfg = (struct ipr_ioa_cfg *) cmd->device->host->hostdata;
4903         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4904
4905         if (!ioa_cfg->in_reset_reload && !ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
4906                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
4907                 dev_err(&ioa_cfg->pdev->dev,
4908                         "Adapter being reset as a result of error recovery.\n");
4909
4910                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4911                         ioa_cfg->sdt_state = GET_DUMP;
4912         }
4913
4914         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4915         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4916         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4917
4918         /* If we got hit with a host reset while we were already resetting
4919          the adapter for some reason, and the reset failed. */
4920         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
4921                 ipr_trace;
4922                 rc = FAILED;
4923         }
4924
4925         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4926         LEAVE;
4927         return rc;
4928 }
4929
4930 /**
4931  * ipr_device_reset - Reset the device
4932  * @ioa_cfg:    ioa config struct
4933  * @res:                resource entry struct
4934  *
4935  * This function issues a device reset to the affected device.
4936  * If the device is a SCSI device, a LUN reset will be sent
4937  * to the device first. If that does not work, a target reset
4938  * will be sent. If the device is a SATA device, a PHY reset will
4939  * be sent.
4940  *
4941  * Return value:
4942  *      0 on success / non-zero on failure
4943  **/
4944 static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
4945                             struct ipr_resource_entry *res)
4946 {
4947         struct ipr_cmnd *ipr_cmd;
4948         struct ipr_ioarcb *ioarcb;
4949         struct ipr_cmd_pkt *cmd_pkt;
4950         struct ipr_ioarcb_ata_regs *regs;
4951         u32 ioasc;
4952
4953         ENTER;
4954         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4955         ioarcb = &ipr_cmd->ioarcb;
4956         cmd_pkt = &ioarcb->cmd_pkt;
4957
4958         if (ipr_cmd->ioa_cfg->sis64) {
4959                 regs = &ipr_cmd->i.ata_ioadl.regs;
4960                 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
4961         } else
4962                 regs = &ioarcb->u.add_data.u.regs;
4963
4964         ioarcb->res_handle = res->res_handle;
4965         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4966         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4967         if (ipr_is_gata(res)) {
4968                 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
4969                 ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(regs->flags));
4970                 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
4971         }
4972
4973         ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4974         ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
4975         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
4976         if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET) {
4977                 if (ipr_cmd->ioa_cfg->sis64)
4978                         memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
4979                                sizeof(struct ipr_ioasa_gata));
4980                 else
4981                         memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
4982                                sizeof(struct ipr_ioasa_gata));
4983         }
4984
4985         LEAVE;
4986         return IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0;
4987 }
4988
4989 /**
4990  * ipr_sata_reset - Reset the SATA port
4991  * @link:       SATA link to reset
4992  * @classes:    class of the attached device
4993  *
4994  * This function issues a SATA phy reset to the affected ATA link.
4995  *
4996  * Return value:
4997  *      0 on success / non-zero on failure
4998  **/
4999 static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
5000                                 unsigned long deadline)
5001 {
5002         struct ipr_sata_port *sata_port = link->ap->private_data;
5003         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5004         struct ipr_resource_entry *res;
5005         unsigned long lock_flags = 0;
5006         int rc = -ENXIO;
5007
5008         ENTER;
5009         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5010         while (ioa_cfg->in_reset_reload) {
5011                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5012                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5013                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5014         }
5015
5016         res = sata_port->res;
5017         if (res) {
5018                 rc = ipr_device_reset(ioa_cfg, res);
5019                 *classes = res->ata_class;
5020         }
5021
5022         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5023         LEAVE;
5024         return rc;
5025 }
5026
5027 /**
5028  * ipr_eh_dev_reset - Reset the device
5029  * @scsi_cmd:   scsi command struct
5030  *
5031  * This function issues a device reset to the affected device.
5032  * A LUN reset will be sent to the device first. If that does
5033  * not work, a target reset will be sent.
5034  *
5035  * Return value:
5036  *      SUCCESS / FAILED
5037  **/
5038 static int __ipr_eh_dev_reset(struct scsi_cmnd *scsi_cmd)
5039 {
5040         struct ipr_cmnd *ipr_cmd;
5041         struct ipr_ioa_cfg *ioa_cfg;
5042         struct ipr_resource_entry *res;
5043         struct ata_port *ap;
5044         int rc = 0;
5045         struct ipr_hrr_queue *hrrq;
5046
5047         ENTER;
5048         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
5049         res = scsi_cmd->device->hostdata;
5050
5051         if (!res)
5052                 return FAILED;
5053
5054         /*
5055          * If we are currently going through reset/reload, return failed. This will force the
5056          * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
5057          * reset to complete
5058          */
5059         if (ioa_cfg->in_reset_reload)
5060                 return FAILED;
5061         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
5062                 return FAILED;
5063
5064         for_each_hrrq(hrrq, ioa_cfg) {
5065                 spin_lock(&hrrq->_lock);
5066                 list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
5067                         if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
5068                                 if (ipr_cmd->scsi_cmd)
5069                                         ipr_cmd->done = ipr_scsi_eh_done;
5070                                 if (ipr_cmd->qc)
5071                                         ipr_cmd->done = ipr_sata_eh_done;
5072                                 if (ipr_cmd->qc &&
5073                                     !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
5074                                         ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
5075                                         ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
5076                                 }
5077                         }
5078                 }
5079                 spin_unlock(&hrrq->_lock);
5080         }
5081         res->resetting_device = 1;
5082         scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
5083
5084         if (ipr_is_gata(res) && res->sata_port) {
5085                 ap = res->sata_port->ap;
5086                 spin_unlock_irq(scsi_cmd->device->host->host_lock);
5087                 ata_std_error_handler(ap);
5088                 spin_lock_irq(scsi_cmd->device->host->host_lock);
5089
5090                 for_each_hrrq(hrrq, ioa_cfg) {
5091                         spin_lock(&hrrq->_lock);
5092                         list_for_each_entry(ipr_cmd,
5093                                             &hrrq->hrrq_pending_q, queue) {
5094                                 if (ipr_cmd->ioarcb.res_handle ==
5095                                     res->res_handle) {
5096                                         rc = -EIO;
5097                                         break;
5098                                 }
5099                         }
5100                         spin_unlock(&hrrq->_lock);
5101                 }
5102         } else
5103                 rc = ipr_device_reset(ioa_cfg, res);
5104         res->resetting_device = 0;
5105         res->reset_occurred = 1;
5106
5107         LEAVE;
5108         return rc ? FAILED : SUCCESS;
5109 }
5110
5111 static int ipr_eh_dev_reset(struct scsi_cmnd *cmd)
5112 {
5113         int rc;
5114         struct ipr_ioa_cfg *ioa_cfg;
5115
5116         ioa_cfg = (struct ipr_ioa_cfg *) cmd->device->host->hostdata;
5117
5118         spin_lock_irq(cmd->device->host->host_lock);
5119         rc = __ipr_eh_dev_reset(cmd);
5120         spin_unlock_irq(cmd->device->host->host_lock);
5121
5122         if (rc == SUCCESS)
5123                 rc = ipr_wait_for_ops(ioa_cfg, cmd->device, ipr_match_lun);
5124
5125         return rc;
5126 }
5127
5128 /**
5129  * ipr_bus_reset_done - Op done function for bus reset.
5130  * @ipr_cmd:    ipr command struct
5131  *
5132  * This function is the op done function for a bus reset
5133  *
5134  * Return value:
5135  *      none
5136  **/
5137 static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
5138 {
5139         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5140         struct ipr_resource_entry *res;
5141
5142         ENTER;
5143         if (!ioa_cfg->sis64)
5144                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
5145                         if (res->res_handle == ipr_cmd->ioarcb.res_handle) {
5146                                 scsi_report_bus_reset(ioa_cfg->host, res->bus);
5147                                 break;
5148                         }
5149                 }
5150
5151         /*
5152          * If abort has not completed, indicate the reset has, else call the
5153          * abort's done function to wake the sleeping eh thread
5154          */
5155         if (ipr_cmd->sibling->sibling)
5156                 ipr_cmd->sibling->sibling = NULL;
5157         else
5158                 ipr_cmd->sibling->done(ipr_cmd->sibling);
5159
5160         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5161         LEAVE;
5162 }
5163
5164 /**
5165  * ipr_abort_timeout - An abort task has timed out
5166  * @ipr_cmd:    ipr command struct
5167  *
5168  * This function handles when an abort task times out. If this
5169  * happens we issue a bus reset since we have resources tied
5170  * up that must be freed before returning to the midlayer.
5171  *
5172  * Return value:
5173  *      none
5174  **/
5175 static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
5176 {
5177         struct ipr_cmnd *reset_cmd;
5178         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5179         struct ipr_cmd_pkt *cmd_pkt;
5180         unsigned long lock_flags = 0;
5181
5182         ENTER;
5183         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5184         if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
5185                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5186                 return;
5187         }
5188
5189         sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
5190         reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5191         ipr_cmd->sibling = reset_cmd;
5192         reset_cmd->sibling = ipr_cmd;
5193         reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
5194         cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
5195         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5196         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
5197         cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
5198
5199         ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
5200         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5201         LEAVE;
5202 }
5203
5204 /**
5205  * ipr_cancel_op - Cancel specified op
5206  * @scsi_cmd:   scsi command struct
5207  *
5208  * This function cancels specified op.
5209  *
5210  * Return value:
5211  *      SUCCESS / FAILED
5212  **/
5213 static int ipr_cancel_op(struct scsi_cmnd *scsi_cmd)
5214 {
5215         struct ipr_cmnd *ipr_cmd;
5216         struct ipr_ioa_cfg *ioa_cfg;
5217         struct ipr_resource_entry *res;
5218         struct ipr_cmd_pkt *cmd_pkt;
5219         u32 ioasc, int_reg;
5220         int op_found = 0;
5221         struct ipr_hrr_queue *hrrq;
5222
5223         ENTER;
5224         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
5225         res = scsi_cmd->device->hostdata;
5226
5227         /* If we are currently going through reset/reload, return failed.
5228          * This will force the mid-layer to call ipr_eh_host_reset,
5229          * which will then go to sleep and wait for the reset to complete
5230          */
5231         if (ioa_cfg->in_reset_reload ||
5232             ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
5233                 return FAILED;
5234         if (!res)
5235                 return FAILED;
5236
5237         /*
5238          * If we are aborting a timed out op, chances are that the timeout was caused
5239          * by a still not detected EEH error. In such cases, reading a register will
5240          * trigger the EEH recovery infrastructure.
5241          */
5242         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5243
5244         if (!ipr_is_gscsi(res))
5245                 return FAILED;
5246
5247         for_each_hrrq(hrrq, ioa_cfg) {
5248                 spin_lock(&hrrq->_lock);
5249                 list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
5250                         if (ipr_cmd->scsi_cmd == scsi_cmd) {
5251                                 ipr_cmd->done = ipr_scsi_eh_done;
5252                                 op_found = 1;
5253                                 break;
5254                         }
5255                 }
5256                 spin_unlock(&hrrq->_lock);
5257         }
5258
5259         if (!op_found)
5260                 return SUCCESS;
5261
5262         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5263         ipr_cmd->ioarcb.res_handle = res->res_handle;
5264         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5265         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5266         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
5267         ipr_cmd->u.sdev = scsi_cmd->device;
5268
5269         scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
5270                     scsi_cmd->cmnd[0]);
5271         ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
5272         ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5273
5274         /*
5275          * If the abort task timed out and we sent a bus reset, we will get
5276          * one the following responses to the abort
5277          */
5278         if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
5279                 ioasc = 0;
5280                 ipr_trace;
5281         }
5282
5283         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5284         if (!ipr_is_naca_model(res))
5285                 res->needs_sync_complete = 1;
5286
5287         LEAVE;
5288         return IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
5289 }
5290
5291 /**
5292  * ipr_eh_abort - Abort a single op
5293  * @scsi_cmd:   scsi command struct
5294  *
5295  * Return value:
5296  *      0 if scan in progress / 1 if scan is complete
5297  **/
5298 static int ipr_scan_finished(struct Scsi_Host *shost, unsigned long elapsed_time)
5299 {
5300         unsigned long lock_flags;
5301         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
5302         int rc = 0;
5303
5304         spin_lock_irqsave(shost->host_lock, lock_flags);
5305         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead || ioa_cfg->scan_done)
5306                 rc = 1;
5307         if ((elapsed_time/HZ) > (ioa_cfg->transop_timeout * 2))
5308                 rc = 1;
5309         spin_unlock_irqrestore(shost->host_lock, lock_flags);
5310         return rc;
5311 }
5312
5313 /**
5314  * ipr_eh_host_reset - Reset the host adapter
5315  * @scsi_cmd:   scsi command struct
5316  *
5317  * Return value:
5318  *      SUCCESS / FAILED
5319  **/
5320 static int ipr_eh_abort(struct scsi_cmnd *scsi_cmd)
5321 {
5322         unsigned long flags;
5323         int rc;
5324         struct ipr_ioa_cfg *ioa_cfg;
5325
5326         ENTER;
5327
5328         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
5329
5330         spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
5331         rc = ipr_cancel_op(scsi_cmd);
5332         spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
5333
5334         if (rc == SUCCESS)
5335                 rc = ipr_wait_for_ops(ioa_cfg, scsi_cmd->device, ipr_match_lun);
5336         LEAVE;
5337         return rc;
5338 }
5339
5340 /**
5341  * ipr_handle_other_interrupt - Handle "other" interrupts
5342  * @ioa_cfg:    ioa config struct
5343  * @int_reg:    interrupt register
5344  *
5345  * Return value:
5346  *      IRQ_NONE / IRQ_HANDLED
5347  **/
5348 static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
5349                                               u32 int_reg)
5350 {
5351         irqreturn_t rc = IRQ_HANDLED;
5352         u32 int_mask_reg;
5353
5354         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
5355         int_reg &= ~int_mask_reg;
5356
5357         /* If an interrupt on the adapter did not occur, ignore it.
5358          * Or in the case of SIS 64, check for a stage change interrupt.
5359          */
5360         if ((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0) {
5361                 if (ioa_cfg->sis64) {
5362                         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
5363                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
5364                         if (int_reg & IPR_PCII_IPL_STAGE_CHANGE) {
5365
5366                                 /* clear stage change */
5367                                 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.clr_interrupt_reg);
5368                                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
5369                                 list_del(&ioa_cfg->reset_cmd->queue);
5370                                 del_timer(&ioa_cfg->reset_cmd->timer);
5371                                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
5372                                 return IRQ_HANDLED;
5373                         }
5374                 }
5375
5376                 return IRQ_NONE;
5377         }
5378
5379         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
5380                 /* Mask the interrupt */
5381                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
5382
5383                 /* Clear the interrupt */
5384                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
5385                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5386
5387                 list_del(&ioa_cfg->reset_cmd->queue);
5388                 del_timer(&ioa_cfg->reset_cmd->timer);
5389                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
5390         } else if ((int_reg & IPR_PCII_HRRQ_UPDATED) == int_reg) {
5391                 if (ioa_cfg->clear_isr) {
5392                         if (ipr_debug && printk_ratelimit())
5393                                 dev_err(&ioa_cfg->pdev->dev,
5394                                         "Spurious interrupt detected. 0x%08X\n", int_reg);
5395                         writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg32);
5396                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5397                         return IRQ_NONE;
5398                 }
5399         } else {
5400                 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
5401                         ioa_cfg->ioa_unit_checked = 1;
5402                 else if (int_reg & IPR_PCII_NO_HOST_RRQ)
5403                         dev_err(&ioa_cfg->pdev->dev,
5404                                 "No Host RRQ. 0x%08X\n", int_reg);
5405                 else
5406                         dev_err(&ioa_cfg->pdev->dev,
5407                                 "Permanent IOA failure. 0x%08X\n", int_reg);
5408
5409                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
5410                         ioa_cfg->sdt_state = GET_DUMP;
5411
5412                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
5413                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5414         }
5415
5416         return rc;
5417 }
5418
5419 /**
5420  * ipr_isr_eh - Interrupt service routine error handler
5421  * @ioa_cfg:    ioa config struct
5422  * @msg:        message to log
5423  *
5424  * Return value:
5425  *      none
5426  **/
5427 static void ipr_isr_eh(struct ipr_ioa_cfg *ioa_cfg, char *msg, u16 number)
5428 {
5429         ioa_cfg->errors_logged++;
5430         dev_err(&ioa_cfg->pdev->dev, "%s %d\n", msg, number);
5431
5432         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
5433                 ioa_cfg->sdt_state = GET_DUMP;
5434
5435         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5436 }
5437
5438 static int ipr_process_hrrq(struct ipr_hrr_queue *hrr_queue, int budget,
5439                                                 struct list_head *doneq)
5440 {
5441         u32 ioasc;
5442         u16 cmd_index;
5443         struct ipr_cmnd *ipr_cmd;
5444         struct ipr_ioa_cfg *ioa_cfg = hrr_queue->ioa_cfg;
5445         int num_hrrq = 0;
5446
5447         /* If interrupts are disabled, ignore the interrupt */
5448         if (!hrr_queue->allow_interrupts)
5449                 return 0;
5450
5451         while ((be32_to_cpu(*hrr_queue->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5452                hrr_queue->toggle_bit) {
5453
5454                 cmd_index = (be32_to_cpu(*hrr_queue->hrrq_curr) &
5455                              IPR_HRRQ_REQ_RESP_HANDLE_MASK) >>
5456                              IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
5457
5458                 if (unlikely(cmd_index > hrr_queue->max_cmd_id ||
5459                              cmd_index < hrr_queue->min_cmd_id)) {
5460                         ipr_isr_eh(ioa_cfg,
5461                                 "Invalid response handle from IOA: ",
5462                                 cmd_index);
5463                         break;
5464                 }
5465
5466                 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
5467                 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5468
5469                 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
5470
5471                 list_move_tail(&ipr_cmd->queue, doneq);
5472
5473                 if (hrr_queue->hrrq_curr < hrr_queue->hrrq_end) {
5474                         hrr_queue->hrrq_curr++;
5475                 } else {
5476                         hrr_queue->hrrq_curr = hrr_queue->hrrq_start;
5477                         hrr_queue->toggle_bit ^= 1u;
5478                 }
5479                 num_hrrq++;
5480                 if (budget > 0 && num_hrrq >= budget)
5481                         break;
5482         }
5483
5484         return num_hrrq;
5485 }
5486
5487 static int ipr_iopoll(struct blk_iopoll *iop, int budget)
5488 {
5489         struct ipr_ioa_cfg *ioa_cfg;
5490         struct ipr_hrr_queue *hrrq;
5491         struct ipr_cmnd *ipr_cmd, *temp;
5492         unsigned long hrrq_flags;
5493         int completed_ops;
5494         LIST_HEAD(doneq);
5495
5496         hrrq = container_of(iop, struct ipr_hrr_queue, iopoll);
5497         ioa_cfg = hrrq->ioa_cfg;
5498
5499         spin_lock_irqsave(hrrq->lock, hrrq_flags);
5500         completed_ops = ipr_process_hrrq(hrrq, budget, &doneq);
5501
5502         if (completed_ops < budget)
5503                 blk_iopoll_complete(iop);
5504         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5505
5506         list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
5507                 list_del(&ipr_cmd->queue);
5508                 del_timer(&ipr_cmd->timer);
5509                 ipr_cmd->fast_done(ipr_cmd);
5510         }
5511
5512         return completed_ops;
5513 }
5514
5515 /**
5516  * ipr_isr - Interrupt service routine
5517  * @irq:        irq number
5518  * @devp:       pointer to ioa config struct
5519  *
5520  * Return value:
5521  *      IRQ_NONE / IRQ_HANDLED
5522  **/
5523 static irqreturn_t ipr_isr(int irq, void *devp)
5524 {
5525         struct ipr_hrr_queue *hrrq = (struct ipr_hrr_queue *)devp;
5526         struct ipr_ioa_cfg *ioa_cfg = hrrq->ioa_cfg;
5527         unsigned long hrrq_flags = 0;
5528         u32 int_reg = 0;
5529         int num_hrrq = 0;
5530         int irq_none = 0;
5531         struct ipr_cmnd *ipr_cmd, *temp;
5532         irqreturn_t rc = IRQ_NONE;
5533         LIST_HEAD(doneq);
5534
5535         spin_lock_irqsave(hrrq->lock, hrrq_flags);
5536         /* If interrupts are disabled, ignore the interrupt */
5537         if (!hrrq->allow_interrupts) {
5538                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5539                 return IRQ_NONE;
5540         }
5541
5542         while (1) {
5543                 if (ipr_process_hrrq(hrrq, -1, &doneq)) {
5544                         rc =  IRQ_HANDLED;
5545
5546                         if (!ioa_cfg->clear_isr)
5547                                 break;
5548
5549                         /* Clear the PCI interrupt */
5550                         num_hrrq = 0;
5551                         do {
5552                                 writel(IPR_PCII_HRRQ_UPDATED,
5553                                      ioa_cfg->regs.clr_interrupt_reg32);
5554                                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5555                         } while (int_reg & IPR_PCII_HRRQ_UPDATED &&
5556                                 num_hrrq++ < IPR_MAX_HRRQ_RETRIES);
5557
5558                 } else if (rc == IRQ_NONE && irq_none == 0) {
5559                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5560                         irq_none++;
5561                 } else if (num_hrrq == IPR_MAX_HRRQ_RETRIES &&
5562                            int_reg & IPR_PCII_HRRQ_UPDATED) {
5563                         ipr_isr_eh(ioa_cfg,
5564                                 "Error clearing HRRQ: ", num_hrrq);
5565                         rc = IRQ_HANDLED;
5566                         break;
5567                 } else
5568                         break;
5569         }
5570
5571         if (unlikely(rc == IRQ_NONE))
5572                 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
5573
5574         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5575         list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
5576                 list_del(&ipr_cmd->queue);
5577                 del_timer(&ipr_cmd->timer);
5578                 ipr_cmd->fast_done(ipr_cmd);
5579         }
5580         return rc;
5581 }
5582
5583 /**
5584  * ipr_isr_mhrrq - Interrupt service routine
5585  * @irq:        irq number
5586  * @devp:       pointer to ioa config struct
5587  *
5588  * Return value:
5589  *      IRQ_NONE / IRQ_HANDLED
5590  **/
5591 static irqreturn_t ipr_isr_mhrrq(int irq, void *devp)
5592 {
5593         struct ipr_hrr_queue *hrrq = (struct ipr_hrr_queue *)devp;
5594         struct ipr_ioa_cfg *ioa_cfg = hrrq->ioa_cfg;
5595         unsigned long hrrq_flags = 0;
5596         struct ipr_cmnd *ipr_cmd, *temp;
5597         irqreturn_t rc = IRQ_NONE;
5598         LIST_HEAD(doneq);
5599
5600         spin_lock_irqsave(hrrq->lock, hrrq_flags);
5601
5602         /* If interrupts are disabled, ignore the interrupt */
5603         if (!hrrq->allow_interrupts) {
5604                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5605                 return IRQ_NONE;
5606         }
5607
5608         if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
5609                 if ((be32_to_cpu(*hrrq->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5610                        hrrq->toggle_bit) {
5611                         if (!blk_iopoll_sched_prep(&hrrq->iopoll))
5612                                 blk_iopoll_sched(&hrrq->iopoll);
5613                         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5614                         return IRQ_HANDLED;
5615                 }
5616         } else {
5617                 if ((be32_to_cpu(*hrrq->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5618                         hrrq->toggle_bit)
5619
5620                         if (ipr_process_hrrq(hrrq, -1, &doneq))
5621                                 rc =  IRQ_HANDLED;
5622         }
5623
5624         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5625
5626         list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
5627                 list_del(&ipr_cmd->queue);
5628                 del_timer(&ipr_cmd->timer);
5629                 ipr_cmd->fast_done(ipr_cmd);
5630         }
5631         return rc;
5632 }
5633
5634 /**
5635  * ipr_build_ioadl64 - Build a scatter/gather list and map the buffer
5636  * @ioa_cfg:    ioa config struct
5637  * @ipr_cmd:    ipr command struct
5638  *
5639  * Return value:
5640  *      0 on success / -1 on failure
5641  **/
5642 static int ipr_build_ioadl64(struct ipr_ioa_cfg *ioa_cfg,
5643                              struct ipr_cmnd *ipr_cmd)
5644 {
5645         int i, nseg;
5646         struct scatterlist *sg;
5647         u32 length;
5648         u32 ioadl_flags = 0;
5649         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5650         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5651         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
5652
5653         length = scsi_bufflen(scsi_cmd);
5654         if (!length)
5655                 return 0;
5656
5657         nseg = scsi_dma_map(scsi_cmd);
5658         if (nseg < 0) {
5659                 if (printk_ratelimit())
5660                         dev_err(&ioa_cfg->pdev->dev, "scsi_dma_map failed!\n");
5661                 return -1;
5662         }
5663
5664         ipr_cmd->dma_use_sg = nseg;
5665
5666         ioarcb->data_transfer_length = cpu_to_be32(length);
5667         ioarcb->ioadl_len =
5668                 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
5669
5670         if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5671                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5672                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5673         } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE)
5674                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5675
5676         scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5677                 ioadl64[i].flags = cpu_to_be32(ioadl_flags);
5678                 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(sg));
5679                 ioadl64[i].address = cpu_to_be64(sg_dma_address(sg));
5680         }
5681
5682         ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5683         return 0;
5684 }
5685
5686 /**
5687  * ipr_build_ioadl - Build a scatter/gather list and map the buffer
5688  * @ioa_cfg:    ioa config struct
5689  * @ipr_cmd:    ipr command struct
5690  *
5691  * Return value:
5692  *      0 on success / -1 on failure
5693  **/
5694 static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
5695                            struct ipr_cmnd *ipr_cmd)
5696 {
5697         int i, nseg;
5698         struct scatterlist *sg;
5699         u32 length;
5700         u32 ioadl_flags = 0;
5701         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5702         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5703         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
5704
5705         length = scsi_bufflen(scsi_cmd);
5706         if (!length)
5707                 return 0;
5708
5709         nseg = scsi_dma_map(scsi_cmd);
5710         if (nseg < 0) {
5711                 dev_err(&ioa_cfg->pdev->dev, "scsi_dma_map failed!\n");
5712                 return -1;
5713         }
5714
5715         ipr_cmd->dma_use_sg = nseg;
5716
5717         if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5718                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5719                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5720                 ioarcb->data_transfer_length = cpu_to_be32(length);
5721                 ioarcb->ioadl_len =
5722                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5723         } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
5724                 ioadl_flags = IPR_IOADL_FLAGS_READ;
5725                 ioarcb->read_data_transfer_length = cpu_to_be32(length);
5726                 ioarcb->read_ioadl_len =
5727                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5728         }
5729
5730         if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->u.add_data.u.ioadl)) {
5731                 ioadl = ioarcb->u.add_data.u.ioadl;
5732                 ioarcb->write_ioadl_addr = cpu_to_be32((ipr_cmd->dma_addr) +
5733                                     offsetof(struct ipr_ioarcb, u.add_data));
5734                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5735         }
5736
5737         scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5738                 ioadl[i].flags_and_data_len =
5739                         cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5740                 ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
5741         }
5742
5743         ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5744         return 0;
5745 }
5746
5747 /**
5748  * ipr_erp_done - Process completion of ERP for a device
5749  * @ipr_cmd:            ipr command struct
5750  *
5751  * This function copies the sense buffer into the scsi_cmd
5752  * struct and pushes the scsi_done function.
5753  *
5754  * Return value:
5755  *      nothing
5756  **/
5757 static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
5758 {
5759         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5760         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5761         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5762
5763         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5764                 scsi_cmd->result |= (DID_ERROR << 16);
5765                 scmd_printk(KERN_ERR, scsi_cmd,
5766                             "Request Sense failed with IOASC: 0x%08X\n", ioasc);
5767         } else {
5768                 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
5769                        SCSI_SENSE_BUFFERSIZE);
5770         }
5771
5772         if (res) {
5773                 if (!ipr_is_naca_model(res))
5774                         res->needs_sync_complete = 1;
5775                 res->in_erp = 0;
5776         }
5777         scsi_dma_unmap(ipr_cmd->scsi_cmd);
5778         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5779         scsi_cmd->scsi_done(scsi_cmd);
5780 }
5781
5782 /**
5783  * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
5784  * @ipr_cmd:    ipr command struct
5785  *
5786  * Return value:
5787  *      none
5788  **/
5789 static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
5790 {
5791         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5792         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5793         dma_addr_t dma_addr = ipr_cmd->dma_addr;
5794
5795         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
5796         ioarcb->data_transfer_length = 0;
5797         ioarcb->read_data_transfer_length = 0;
5798         ioarcb->ioadl_len = 0;
5799         ioarcb->read_ioadl_len = 0;
5800         ioasa->hdr.ioasc = 0;
5801         ioasa->hdr.residual_data_len = 0;
5802
5803         if (ipr_cmd->ioa_cfg->sis64)
5804                 ioarcb->u.sis64_addr_data.data_ioadl_addr =
5805                         cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
5806         else {
5807                 ioarcb->write_ioadl_addr =
5808                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
5809                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5810         }
5811 }
5812
5813 /**
5814  * ipr_erp_request_sense - Send request sense to a device
5815  * @ipr_cmd:    ipr command struct
5816  *
5817  * This function sends a request sense to a device as a result
5818  * of a check condition.
5819  *
5820  * Return value:
5821  *      nothing
5822  **/
5823 static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
5824 {
5825         struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5826         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5827
5828         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5829                 ipr_erp_done(ipr_cmd);
5830                 return;
5831         }
5832
5833         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5834
5835         cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
5836         cmd_pkt->cdb[0] = REQUEST_SENSE;
5837         cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
5838         cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
5839         cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5840         cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
5841
5842         ipr_init_ioadl(ipr_cmd, ipr_cmd->sense_buffer_dma,
5843                        SCSI_SENSE_BUFFERSIZE, IPR_IOADL_FLAGS_READ_LAST);
5844
5845         ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
5846                    IPR_REQUEST_SENSE_TIMEOUT * 2);
5847 }
5848
5849 /**
5850  * ipr_erp_cancel_all - Send cancel all to a device
5851  * @ipr_cmd:    ipr command struct
5852  *
5853  * This function sends a cancel all to a device to clear the
5854  * queue. If we are running TCQ on the device, QERR is set to 1,
5855  * which means all outstanding ops have been dropped on the floor.
5856  * Cancel all will return them to us.
5857  *
5858  * Return value:
5859  *      nothing
5860  **/
5861 static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
5862 {
5863         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5864         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5865         struct ipr_cmd_pkt *cmd_pkt;
5866
5867         res->in_erp = 1;
5868
5869         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5870
5871         if (!scsi_cmd->device->simple_tags) {
5872                 ipr_erp_request_sense(ipr_cmd);
5873                 return;
5874         }
5875
5876         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5877         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5878         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
5879
5880         ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
5881                    IPR_CANCEL_ALL_TIMEOUT);
5882 }
5883
5884 /**
5885  * ipr_dump_ioasa - Dump contents of IOASA
5886  * @ioa_cfg:    ioa config struct
5887  * @ipr_cmd:    ipr command struct
5888  * @res:                resource entry struct
5889  *
5890  * This function is invoked by the interrupt handler when ops
5891  * fail. It will log the IOASA if appropriate. Only called
5892  * for GPDD ops.
5893  *
5894  * Return value:
5895  *      none
5896  **/
5897 static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
5898                            struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
5899 {
5900         int i;
5901         u16 data_len;
5902         u32 ioasc, fd_ioasc;
5903         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5904         __be32 *ioasa_data = (__be32 *)ioasa;
5905         int error_index;
5906
5907         ioasc = be32_to_cpu(ioasa->hdr.ioasc) & IPR_IOASC_IOASC_MASK;
5908         fd_ioasc = be32_to_cpu(ioasa->hdr.fd_ioasc) & IPR_IOASC_IOASC_MASK;
5909
5910         if (0 == ioasc)
5911                 return;
5912
5913         if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
5914                 return;
5915
5916         if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
5917                 error_index = ipr_get_error(fd_ioasc);
5918         else
5919                 error_index = ipr_get_error(ioasc);
5920
5921         if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
5922                 /* Don't log an error if the IOA already logged one */
5923                 if (ioasa->hdr.ilid != 0)
5924                         return;
5925
5926                 if (!ipr_is_gscsi(res))
5927                         return;
5928
5929                 if (ipr_error_table[error_index].log_ioasa == 0)
5930                         return;
5931         }
5932
5933         ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
5934
5935         data_len = be16_to_cpu(ioasa->hdr.ret_stat_len);
5936         if (ioa_cfg->sis64 && sizeof(struct ipr_ioasa64) < data_len)
5937                 data_len = sizeof(struct ipr_ioasa64);
5938         else if (!ioa_cfg->sis64 && sizeof(struct ipr_ioasa) < data_len)
5939                 data_len = sizeof(struct ipr_ioasa);
5940
5941         ipr_err("IOASA Dump:\n");
5942
5943         for (i = 0; i < data_len / 4; i += 4) {
5944                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
5945                         be32_to_cpu(ioasa_data[i]),
5946                         be32_to_cpu(ioasa_data[i+1]),
5947                         be32_to_cpu(ioasa_data[i+2]),
5948                         be32_to_cpu(ioasa_data[i+3]));
5949         }
5950 }
5951
5952 /**
5953  * ipr_gen_sense - Generate SCSI sense data from an IOASA
5954  * @ioasa:              IOASA
5955  * @sense_buf:  sense data buffer
5956  *
5957  * Return value:
5958  *      none
5959  **/
5960 static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
5961 {
5962         u32 failing_lba;
5963         u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
5964         struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
5965         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5966         u32 ioasc = be32_to_cpu(ioasa->hdr.ioasc);
5967
5968         memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
5969
5970         if (ioasc >= IPR_FIRST_DRIVER_IOASC)
5971                 return;
5972
5973         ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
5974
5975         if (ipr_is_vset_device(res) &&
5976             ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
5977             ioasa->u.vset.failing_lba_hi != 0) {
5978                 sense_buf[0] = 0x72;
5979                 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
5980                 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
5981                 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
5982
5983                 sense_buf[7] = 12;
5984                 sense_buf[8] = 0;
5985                 sense_buf[9] = 0x0A;
5986                 sense_buf[10] = 0x80;
5987
5988                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
5989
5990                 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
5991                 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
5992                 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
5993                 sense_buf[15] = failing_lba & 0x000000ff;
5994
5995                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5996
5997                 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
5998                 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
5999                 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
6000                 sense_buf[19] = failing_lba & 0x000000ff;
6001         } else {
6002                 sense_buf[0] = 0x70;
6003                 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
6004                 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
6005                 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
6006
6007                 /* Illegal request */
6008                 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
6009                     (be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
6010                         sense_buf[7] = 10;      /* additional length */
6011
6012                         /* IOARCB was in error */
6013                         if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
6014                                 sense_buf[15] = 0xC0;
6015                         else    /* Parameter data was invalid */
6016                                 sense_buf[15] = 0x80;
6017
6018                         sense_buf[16] =
6019                             ((IPR_FIELD_POINTER_MASK &
6020                               be32_to_cpu(ioasa->hdr.ioasc_specific)) >> 8) & 0xff;
6021                         sense_buf[17] =
6022                             (IPR_FIELD_POINTER_MASK &
6023                              be32_to_cpu(ioasa->hdr.ioasc_specific)) & 0xff;
6024                 } else {
6025                         if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
6026                                 if (ipr_is_vset_device(res))
6027                                         failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
6028                                 else
6029                                         failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
6030
6031                                 sense_buf[0] |= 0x80;   /* Or in the Valid bit */
6032                                 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
6033                                 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
6034                                 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
6035                                 sense_buf[6] = failing_lba & 0x000000ff;
6036                         }
6037
6038                         sense_buf[7] = 6;       /* additional length */
6039                 }
6040         }
6041 }
6042
6043 /**
6044  * ipr_get_autosense - Copy autosense data to sense buffer
6045  * @ipr_cmd:    ipr command struct
6046  *
6047  * This function copies the autosense buffer to the buffer
6048  * in the scsi_cmd, if there is autosense available.
6049  *
6050  * Return value:
6051  *      1 if autosense was available / 0 if not
6052  **/
6053 static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
6054 {
6055         struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
6056         struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
6057
6058         if ((be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
6059                 return 0;
6060
6061         if (ipr_cmd->ioa_cfg->sis64)
6062                 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa64->auto_sense.data,
6063                        min_t(u16, be16_to_cpu(ioasa64->auto_sense.auto_sense_len),
6064                            SCSI_SENSE_BUFFERSIZE));
6065         else
6066                 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
6067                        min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
6068                            SCSI_SENSE_BUFFERSIZE));
6069         return 1;
6070 }
6071
6072 /**
6073  * ipr_erp_start - Process an error response for a SCSI op
6074  * @ioa_cfg:    ioa config struct
6075  * @ipr_cmd:    ipr command struct
6076  *
6077  * This function determines whether or not to initiate ERP
6078  * on the affected device.
6079  *
6080  * Return value:
6081  *      nothing
6082  **/
6083 static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
6084                               struct ipr_cmnd *ipr_cmd)
6085 {
6086         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
6087         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
6088         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6089         u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
6090
6091         if (!res) {
6092                 ipr_scsi_eh_done(ipr_cmd);
6093                 return;
6094         }
6095
6096         if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
6097                 ipr_gen_sense(ipr_cmd);
6098
6099         ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
6100
6101         switch (masked_ioasc) {
6102         case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
6103                 if (ipr_is_naca_model(res))
6104                         scsi_cmd->result |= (DID_ABORT << 16);
6105                 else
6106                         scsi_cmd->result |= (DID_IMM_RETRY << 16);
6107                 break;
6108         case IPR_IOASC_IR_RESOURCE_HANDLE:
6109         case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
6110                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
6111                 break;
6112         case IPR_IOASC_HW_SEL_TIMEOUT:
6113                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
6114                 if (!ipr_is_naca_model(res))
6115                         res->needs_sync_complete = 1;
6116                 break;
6117         case IPR_IOASC_SYNC_REQUIRED:
6118                 if (!res->in_erp)
6119                         res->needs_sync_complete = 1;
6120                 scsi_cmd->result |= (DID_IMM_RETRY << 16);
6121                 break;
6122         case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
6123         case IPR_IOASA_IR_DUAL_IOA_DISABLED:
6124                 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
6125                 break;
6126         case IPR_IOASC_BUS_WAS_RESET:
6127         case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
6128                 /*
6129                  * Report the bus reset and ask for a retry. The device
6130                  * will give CC/UA the next command.
6131                  */
6132                 if (!res->resetting_device)
6133                         scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
6134                 scsi_cmd->result |= (DID_ERROR << 16);
6135                 if (!ipr_is_naca_model(res))
6136                         res->needs_sync_complete = 1;
6137                 break;
6138         case IPR_IOASC_HW_DEV_BUS_STATUS:
6139                 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
6140                 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
6141                         if (!ipr_get_autosense(ipr_cmd)) {
6142                                 if (!ipr_is_naca_model(res)) {
6143                                         ipr_erp_cancel_all(ipr_cmd);
6144                                         return;
6145                                 }
6146                         }
6147                 }
6148                 if (!ipr_is_naca_model(res))
6149                         res->needs_sync_complete = 1;
6150                 break;
6151         case IPR_IOASC_NR_INIT_CMD_REQUIRED:
6152                 break;
6153         default:
6154                 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
6155                         scsi_cmd->result |= (DID_ERROR << 16);
6156                 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
6157                         res->needs_sync_complete = 1;
6158                 break;
6159         }
6160
6161         scsi_dma_unmap(ipr_cmd->scsi_cmd);
6162         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6163         scsi_cmd->scsi_done(scsi_cmd);
6164 }
6165
6166 /**
6167  * ipr_scsi_done - mid-layer done function
6168  * @ipr_cmd:    ipr command struct
6169  *
6170  * This function is invoked by the interrupt handler for
6171  * ops generated by the SCSI mid-layer
6172  *
6173  * Return value:
6174  *      none
6175  **/
6176 static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
6177 {
6178         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6179         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
6180         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6181         unsigned long hrrq_flags;
6182
6183         scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->s.ioasa.hdr.residual_data_len));
6184
6185         if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
6186                 scsi_dma_unmap(scsi_cmd);
6187
6188                 spin_lock_irqsave(ipr_cmd->hrrq->lock, hrrq_flags);
6189                 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6190                 scsi_cmd->scsi_done(scsi_cmd);
6191                 spin_unlock_irqrestore(ipr_cmd->hrrq->lock, hrrq_flags);
6192         } else {
6193                 spin_lock_irqsave(ipr_cmd->hrrq->lock, hrrq_flags);
6194                 ipr_erp_start(ioa_cfg, ipr_cmd);
6195                 spin_unlock_irqrestore(ipr_cmd->hrrq->lock, hrrq_flags);
6196         }
6197 }
6198
6199 /**
6200  * ipr_queuecommand - Queue a mid-layer request
6201  * @shost:              scsi host struct
6202  * @scsi_cmd:   scsi command struct
6203  *
6204  * This function queues a request generated by the mid-layer.
6205  *
6206  * Return value:
6207  *      0 on success
6208  *      SCSI_MLQUEUE_DEVICE_BUSY if device is busy
6209  *      SCSI_MLQUEUE_HOST_BUSY if host is busy
6210  **/
6211 static int ipr_queuecommand(struct Scsi_Host *shost,
6212                             struct scsi_cmnd *scsi_cmd)
6213 {
6214         struct ipr_ioa_cfg *ioa_cfg;
6215         struct ipr_resource_entry *res;
6216         struct ipr_ioarcb *ioarcb;
6217         struct ipr_cmnd *ipr_cmd;
6218         unsigned long hrrq_flags, lock_flags;
6219         int rc;
6220         struct ipr_hrr_queue *hrrq;
6221         int hrrq_id;
6222
6223         ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
6224
6225         scsi_cmd->result = (DID_OK << 16);
6226         res = scsi_cmd->device->hostdata;
6227
6228         if (ipr_is_gata(res) && res->sata_port) {
6229                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6230                 rc = ata_sas_queuecmd(scsi_cmd, res->sata_port->ap);
6231                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6232                 return rc;
6233         }
6234
6235         hrrq_id = ipr_get_hrrq_index(ioa_cfg);
6236         hrrq = &ioa_cfg->hrrq[hrrq_id];
6237
6238         spin_lock_irqsave(hrrq->lock, hrrq_flags);
6239         /*
6240          * We are currently blocking all devices due to a host reset
6241          * We have told the host to stop giving us new requests, but
6242          * ERP ops don't count. FIXME
6243          */
6244         if (unlikely(!hrrq->allow_cmds && !hrrq->ioa_is_dead && !hrrq->removing_ioa)) {
6245                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6246                 return SCSI_MLQUEUE_HOST_BUSY;
6247         }
6248
6249         /*
6250          * FIXME - Create scsi_set_host_offline interface
6251          *  and the ioa_is_dead check can be removed
6252          */
6253         if (unlikely(hrrq->ioa_is_dead || hrrq->removing_ioa || !res)) {
6254                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6255                 goto err_nodev;
6256         }
6257
6258         ipr_cmd = __ipr_get_free_ipr_cmnd(hrrq);
6259         if (ipr_cmd == NULL) {
6260                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6261                 return SCSI_MLQUEUE_HOST_BUSY;
6262         }
6263         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6264
6265         ipr_init_ipr_cmnd(ipr_cmd, ipr_scsi_done);
6266         ioarcb = &ipr_cmd->ioarcb;
6267
6268         memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
6269         ipr_cmd->scsi_cmd = scsi_cmd;
6270         ipr_cmd->done = ipr_scsi_eh_done;
6271
6272         if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
6273                 if (scsi_cmd->underflow == 0)
6274                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
6275
6276                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
6277                 if (ipr_is_gscsi(res) && res->reset_occurred) {
6278                         res->reset_occurred = 0;
6279                         ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
6280                 }
6281                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
6282                 if (scsi_cmd->flags & SCMD_TAGGED)
6283                         ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_SIMPLE_TASK;
6284                 else
6285                         ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_UNTAGGED_TASK;
6286         }
6287
6288         if (scsi_cmd->cmnd[0] >= 0xC0 &&
6289             (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE)) {
6290                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6291         }
6292
6293         if (ioa_cfg->sis64)
6294                 rc = ipr_build_ioadl64(ioa_cfg, ipr_cmd);
6295         else
6296                 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
6297
6298         spin_lock_irqsave(hrrq->lock, hrrq_flags);
6299         if (unlikely(rc || (!hrrq->allow_cmds && !hrrq->ioa_is_dead))) {
6300                 list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_free_q);
6301                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6302                 if (!rc)
6303                         scsi_dma_unmap(scsi_cmd);
6304                 return SCSI_MLQUEUE_HOST_BUSY;
6305         }
6306
6307         if (unlikely(hrrq->ioa_is_dead)) {
6308                 list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_free_q);
6309                 spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6310                 scsi_dma_unmap(scsi_cmd);
6311                 goto err_nodev;
6312         }
6313
6314         ioarcb->res_handle = res->res_handle;
6315         if (res->needs_sync_complete) {
6316                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
6317                 res->needs_sync_complete = 0;
6318         }
6319         list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_pending_q);
6320         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
6321         ipr_send_command(ipr_cmd);
6322         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6323         return 0;
6324
6325 err_nodev:
6326         spin_lock_irqsave(hrrq->lock, hrrq_flags);
6327         memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
6328         scsi_cmd->result = (DID_NO_CONNECT << 16);
6329         scsi_cmd->scsi_done(scsi_cmd);
6330         spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6331         return 0;
6332 }
6333
6334 /**
6335  * ipr_ioctl - IOCTL handler
6336  * @sdev:       scsi device struct
6337  * @cmd:        IOCTL cmd
6338  * @arg:        IOCTL arg
6339  *
6340  * Return value:
6341  *      0 on success / other on failure
6342  **/
6343 static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
6344 {
6345         struct ipr_resource_entry *res;
6346
6347         res = (struct ipr_resource_entry *)sdev->hostdata;
6348         if (res && ipr_is_gata(res)) {
6349                 if (cmd == HDIO_GET_IDENTITY)
6350                         return -ENOTTY;
6351                 return ata_sas_scsi_ioctl(res->sata_port->ap, sdev, cmd, arg);
6352         }
6353
6354         return -EINVAL;
6355 }
6356
6357 /**
6358  * ipr_info - Get information about the card/driver
6359  * @scsi_host:  scsi host struct
6360  *
6361  * Return value:
6362  *      pointer to buffer with description string
6363  **/
6364 static const char *ipr_ioa_info(struct Scsi_Host *host)
6365 {
6366         static char buffer[512];
6367         struct ipr_ioa_cfg *ioa_cfg;
6368         unsigned long lock_flags = 0;
6369
6370         ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
6371
6372         spin_lock_irqsave(host->host_lock, lock_flags);
6373         sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
6374         spin_unlock_irqrestore(host->host_lock, lock_flags);
6375
6376         return buffer;
6377 }
6378
6379 static struct scsi_host_template driver_template = {
6380         .module = THIS_MODULE,
6381         .name = "IPR",
6382         .info = ipr_ioa_info,
6383         .ioctl = ipr_ioctl,
6384         .queuecommand = ipr_queuecommand,
6385         .eh_abort_handler = ipr_eh_abort,
6386         .eh_device_reset_handler = ipr_eh_dev_reset,
6387         .eh_host_reset_handler = ipr_eh_host_reset,
6388         .slave_alloc = ipr_slave_alloc,
6389         .slave_configure = ipr_slave_configure,
6390         .slave_destroy = ipr_slave_destroy,
6391         .scan_finished = ipr_scan_finished,
6392         .target_alloc = ipr_target_alloc,
6393         .target_destroy = ipr_target_destroy,
6394         .change_queue_depth = ipr_change_queue_depth,
6395         .bios_param = ipr_biosparam,
6396         .can_queue = IPR_MAX_COMMANDS,
6397         .this_id = -1,
6398         .sg_tablesize = IPR_MAX_SGLIST,
6399         .max_sectors = IPR_IOA_MAX_SECTORS,
6400         .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
6401         .use_clustering = ENABLE_CLUSTERING,
6402         .shost_attrs = ipr_ioa_attrs,
6403         .sdev_attrs = ipr_dev_attrs,
6404         .proc_name = IPR_NAME,
6405         .no_write_same = 1,
6406         .use_blk_tags = 1,
6407 };
6408
6409 /**
6410  * ipr_ata_phy_reset - libata phy_reset handler
6411  * @ap:         ata port to reset
6412  *
6413  **/
6414 static void ipr_ata_phy_reset(struct ata_port *ap)
6415 {
6416         unsigned long flags;
6417         struct ipr_sata_port *sata_port = ap->private_data;
6418         struct ipr_resource_entry *res = sata_port->res;
6419         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6420         int rc;
6421
6422         ENTER;
6423         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6424         while (ioa_cfg->in_reset_reload) {
6425                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6426                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
6427                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6428         }
6429
6430         if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds)
6431                 goto out_unlock;
6432
6433         rc = ipr_device_reset(ioa_cfg, res);
6434
6435         if (rc) {
6436                 ap->link.device[0].class = ATA_DEV_NONE;
6437                 goto out_unlock;
6438         }
6439
6440         ap->link.device[0].class = res->ata_class;
6441         if (ap->link.device[0].class == ATA_DEV_UNKNOWN)
6442                 ap->link.device[0].class = ATA_DEV_NONE;
6443
6444 out_unlock:
6445         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6446         LEAVE;
6447 }
6448
6449 /**
6450  * ipr_ata_post_internal - Cleanup after an internal command
6451  * @qc: ATA queued command
6452  *
6453  * Return value:
6454  *      none
6455  **/
6456 static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
6457 {
6458         struct ipr_sata_port *sata_port = qc->ap->private_data;
6459         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6460         struct ipr_cmnd *ipr_cmd;
6461         struct ipr_hrr_queue *hrrq;
6462         unsigned long flags;
6463
6464         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6465         while (ioa_cfg->in_reset_reload) {
6466                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6467                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
6468                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6469         }
6470
6471         for_each_hrrq(hrrq, ioa_cfg) {
6472                 spin_lock(&hrrq->_lock);
6473                 list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
6474                         if (ipr_cmd->qc == qc) {
6475                                 ipr_device_reset(ioa_cfg, sata_port->res);
6476                                 break;
6477                         }
6478                 }
6479                 spin_unlock(&hrrq->_lock);
6480         }
6481         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6482 }
6483
6484 /**
6485  * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
6486  * @regs:       destination
6487  * @tf: source ATA taskfile
6488  *
6489  * Return value:
6490  *      none
6491  **/
6492 static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
6493                              struct ata_taskfile *tf)
6494 {
6495         regs->feature = tf->feature;
6496         regs->nsect = tf->nsect;
6497         regs->lbal = tf->lbal;
6498         regs->lbam = tf->lbam;
6499         regs->lbah = tf->lbah;
6500         regs->device = tf->device;
6501         regs->command = tf->command;
6502         regs->hob_feature = tf->hob_feature;
6503         regs->hob_nsect = tf->hob_nsect;
6504         regs->hob_lbal = tf->hob_lbal;
6505         regs->hob_lbam = tf->hob_lbam;
6506         regs->hob_lbah = tf->hob_lbah;
6507         regs->ctl = tf->ctl;
6508 }
6509
6510 /**
6511  * ipr_sata_done - done function for SATA commands
6512  * @ipr_cmd:    ipr command struct
6513  *
6514  * This function is invoked by the interrupt handler for
6515  * ops generated by the SCSI mid-layer to SATA devices
6516  *
6517  * Return value:
6518  *      none
6519  **/
6520 static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
6521 {
6522         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6523         struct ata_queued_cmd *qc = ipr_cmd->qc;
6524         struct ipr_sata_port *sata_port = qc->ap->private_data;
6525         struct ipr_resource_entry *res = sata_port->res;
6526         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6527
6528         spin_lock(&ipr_cmd->hrrq->_lock);
6529         if (ipr_cmd->ioa_cfg->sis64)
6530                 memcpy(&sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
6531                        sizeof(struct ipr_ioasa_gata));
6532         else
6533                 memcpy(&sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
6534                        sizeof(struct ipr_ioasa_gata));
6535         ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
6536
6537         if (be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
6538                 scsi_report_device_reset(ioa_cfg->host, res->bus, res->target);
6539
6540         if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
6541                 qc->err_mask |= __ac_err_mask(sata_port->ioasa.status);
6542         else
6543                 qc->err_mask |= ac_err_mask(sata_port->ioasa.status);
6544         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6545         spin_unlock(&ipr_cmd->hrrq->_lock);
6546         ata_qc_complete(qc);
6547 }
6548
6549 /**
6550  * ipr_build_ata_ioadl64 - Build an ATA scatter/gather list
6551  * @ipr_cmd:    ipr command struct
6552  * @qc:         ATA queued command
6553  *
6554  **/
6555 static void ipr_build_ata_ioadl64(struct ipr_cmnd *ipr_cmd,
6556                                   struct ata_queued_cmd *qc)
6557 {
6558         u32 ioadl_flags = 0;
6559         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6560         struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ata_ioadl.ioadl64;
6561         struct ipr_ioadl64_desc *last_ioadl64 = NULL;
6562         int len = qc->nbytes;
6563         struct scatterlist *sg;
6564         unsigned int si;
6565         dma_addr_t dma_addr = ipr_cmd->dma_addr;
6566
6567         if (len == 0)
6568                 return;
6569
6570         if (qc->dma_dir == DMA_TO_DEVICE) {
6571                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
6572                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6573         } else if (qc->dma_dir == DMA_FROM_DEVICE)
6574                 ioadl_flags = IPR_IOADL_FLAGS_READ;
6575
6576         ioarcb->data_transfer_length = cpu_to_be32(len);
6577         ioarcb->ioadl_len =
6578                 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
6579         ioarcb->u.sis64_addr_data.data_ioadl_addr =
6580                 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ata_ioadl.ioadl64));
6581
6582         for_each_sg(qc->sg, sg, qc->n_elem, si) {
6583                 ioadl64->flags = cpu_to_be32(ioadl_flags);
6584                 ioadl64->data_len = cpu_to_be32(sg_dma_len(sg));
6585                 ioadl64->address = cpu_to_be64(sg_dma_address(sg));
6586
6587                 last_ioadl64 = ioadl64;
6588                 ioadl64++;
6589         }
6590
6591         if (likely(last_ioadl64))
6592                 last_ioadl64->flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
6593 }
6594
6595 /**
6596  * ipr_build_ata_ioadl - Build an ATA scatter/gather list
6597  * @ipr_cmd:    ipr command struct
6598  * @qc:         ATA queued command
6599  *
6600  **/
6601 static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
6602                                 struct ata_queued_cmd *qc)
6603 {
6604         u32 ioadl_flags = 0;
6605         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6606         struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
6607         struct ipr_ioadl_desc *last_ioadl = NULL;
6608         int len = qc->nbytes;
6609         struct scatterlist *sg;
6610         unsigned int si;
6611
6612         if (len == 0)
6613                 return;
6614
6615         if (qc->dma_dir == DMA_TO_DEVICE) {
6616                 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
6617                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6618                 ioarcb->data_transfer_length = cpu_to_be32(len);
6619                 ioarcb->ioadl_len =
6620                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
6621         } else if (qc->dma_dir == DMA_FROM_DEVICE) {
6622                 ioadl_flags = IPR_IOADL_FLAGS_READ;
6623                 ioarcb->read_data_transfer_length = cpu_to_be32(len);
6624                 ioarcb->read_ioadl_len =
6625                         cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
6626         }
6627
6628         for_each_sg(qc->sg, sg, qc->n_elem, si) {
6629                 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
6630                 ioadl->address = cpu_to_be32(sg_dma_address(sg));
6631
6632                 last_ioadl = ioadl;
6633                 ioadl++;
6634         }
6635
6636         if (likely(last_ioadl))
6637                 last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
6638 }
6639
6640 /**
6641  * ipr_qc_defer - Get a free ipr_cmd
6642  * @qc: queued command
6643  *
6644  * Return value:
6645  *      0 if success
6646  **/
6647 static int ipr_qc_defer(struct ata_queued_cmd *qc)
6648 {
6649         struct ata_port *ap = qc->ap;
6650         struct ipr_sata_port *sata_port = ap->private_data;
6651         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6652         struct ipr_cmnd *ipr_cmd;
6653         struct ipr_hrr_queue *hrrq;
6654         int hrrq_id;
6655
6656         hrrq_id = ipr_get_hrrq_index(ioa_cfg);
6657         hrrq = &ioa_cfg->hrrq[hrrq_id];
6658
6659         qc->lldd_task = NULL;
6660         spin_lock(&hrrq->_lock);
6661         if (unlikely(hrrq->ioa_is_dead)) {
6662                 spin_unlock(&hrrq->_lock);
6663                 return 0;
6664         }
6665
6666         if (unlikely(!hrrq->allow_cmds)) {
6667                 spin_unlock(&hrrq->_lock);
6668                 return ATA_DEFER_LINK;
6669         }
6670
6671         ipr_cmd = __ipr_get_free_ipr_cmnd(hrrq);
6672         if (ipr_cmd == NULL) {
6673                 spin_unlock(&hrrq->_lock);
6674                 return ATA_DEFER_LINK;
6675         }
6676
6677         qc->lldd_task = ipr_cmd;
6678         spin_unlock(&hrrq->_lock);
6679         return 0;
6680 }
6681
6682 /**
6683  * ipr_qc_issue - Issue a SATA qc to a device
6684  * @qc: queued command
6685  *
6686  * Return value:
6687  *      0 if success
6688  **/
6689 static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
6690 {
6691         struct ata_port *ap = qc->ap;
6692         struct ipr_sata_port *sata_port = ap->private_data;
6693         struct ipr_resource_entry *res = sata_port->res;
6694         struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6695         struct ipr_cmnd *ipr_cmd;
6696         struct ipr_ioarcb *ioarcb;
6697         struct ipr_ioarcb_ata_regs *regs;
6698
6699         if (qc->lldd_task == NULL)
6700                 ipr_qc_defer(qc);
6701
6702         ipr_cmd = qc->lldd_task;
6703         if (ipr_cmd == NULL)
6704                 return AC_ERR_SYSTEM;
6705
6706         qc->lldd_task = NULL;
6707         spin_lock(&ipr_cmd->hrrq->_lock);
6708         if (unlikely(!ipr_cmd->hrrq->allow_cmds ||
6709                         ipr_cmd->hrrq->ioa_is_dead)) {
6710                 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6711                 spin_unlock(&ipr_cmd->hrrq->_lock);
6712                 return AC_ERR_SYSTEM;
6713         }
6714
6715         ipr_init_ipr_cmnd(ipr_cmd, ipr_lock_and_done);
6716         ioarcb = &ipr_cmd->ioarcb;
6717
6718         if (ioa_cfg->sis64) {
6719                 regs = &ipr_cmd->i.ata_ioadl.regs;
6720                 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
6721         } else
6722                 regs = &ioarcb->u.add_data.u.regs;
6723
6724         memset(regs, 0, sizeof(*regs));
6725         ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(*regs));
6726
6727         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
6728         ipr_cmd->qc = qc;
6729         ipr_cmd->done = ipr_sata_done;
6730         ipr_cmd->ioarcb.res_handle = res->res_handle;
6731         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
6732         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
6733         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
6734         ipr_cmd->dma_use_sg = qc->n_elem;
6735
6736         if (ioa_cfg->sis64)
6737                 ipr_build_ata_ioadl64(ipr_cmd, qc);
6738         else
6739                 ipr_build_ata_ioadl(ipr_cmd, qc);
6740
6741         regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
6742         ipr_copy_sata_tf(regs, &qc->tf);
6743         memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
6744         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
6745
6746         switch (qc->tf.protocol) {
6747         case ATA_PROT_NODATA:
6748         case ATA_PROT_PIO:
6749                 break;
6750
6751         case ATA_PROT_DMA:
6752                 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
6753                 break;
6754
6755         case ATAPI_PROT_PIO:
6756         case ATAPI_PROT_NODATA:
6757                 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
6758                 break;
6759
6760         case ATAPI_PROT_DMA:
6761                 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
6762                 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
6763                 break;
6764
6765         default:
6766                 WARN_ON(1);
6767                 spin_unlock(&ipr_cmd->hrrq->_lock);
6768                 return AC_ERR_INVALID;
6769         }
6770
6771         ipr_send_command(ipr_cmd);
6772         spin_unlock(&ipr_cmd->hrrq->_lock);
6773
6774         return 0;
6775 }
6776
6777 /**
6778  * ipr_qc_fill_rtf - Read result TF
6779  * @qc: ATA queued command
6780  *
6781  * Return value:
6782  *      true
6783  **/
6784 static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
6785 {
6786         struct ipr_sata_port *sata_port = qc->ap->private_data;
6787         struct ipr_ioasa_gata *g = &sata_port->ioasa;
6788         struct ata_taskfile *tf = &qc->result_tf;
6789
6790         tf->feature = g->error;
6791         tf->nsect = g->nsect;
6792         tf->lbal = g->lbal;
6793         tf->lbam = g->lbam;
6794         tf->lbah = g->lbah;
6795         tf->device = g->device;
6796         tf->command = g->status;
6797         tf->hob_nsect = g->hob_nsect;
6798         tf->hob_lbal = g->hob_lbal;
6799         tf->hob_lbam = g->hob_lbam;
6800         tf->hob_lbah = g->hob_lbah;
6801
6802         return true;
6803 }
6804
6805 static struct ata_port_operations ipr_sata_ops = {
6806         .phy_reset = ipr_ata_phy_reset,
6807         .hardreset = ipr_sata_reset,
6808         .post_internal_cmd = ipr_ata_post_internal,
6809         .qc_prep = ata_noop_qc_prep,
6810         .qc_defer = ipr_qc_defer,
6811         .qc_issue = ipr_qc_issue,
6812         .qc_fill_rtf = ipr_qc_fill_rtf,
6813         .port_start = ata_sas_port_start,
6814         .port_stop = ata_sas_port_stop
6815 };
6816
6817 static struct ata_port_info sata_port_info = {
6818         .flags          = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA |
6819                           ATA_FLAG_SAS_HOST,
6820         .pio_mask       = ATA_PIO4_ONLY,
6821         .mwdma_mask     = ATA_MWDMA2,
6822         .udma_mask      = ATA_UDMA6,
6823         .port_ops       = &ipr_sata_ops
6824 };
6825
6826 #ifdef CONFIG_PPC_PSERIES
6827 static const u16 ipr_blocked_processors[] = {
6828         PVR_NORTHSTAR,
6829         PVR_PULSAR,
6830         PVR_POWER4,
6831         PVR_ICESTAR,
6832         PVR_SSTAR,
6833         PVR_POWER4p,
6834         PVR_630,
6835         PVR_630p
6836 };
6837
6838 /**
6839  * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
6840  * @ioa_cfg:    ioa cfg struct
6841  *
6842  * Adapters that use Gemstone revision < 3.1 do not work reliably on
6843  * certain pSeries hardware. This function determines if the given
6844  * adapter is in one of these confgurations or not.
6845  *
6846  * Return value:
6847  *      1 if adapter is not supported / 0 if adapter is supported
6848  **/
6849 static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
6850 {
6851         int i;
6852
6853         if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
6854                 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++) {
6855                         if (pvr_version_is(ipr_blocked_processors[i]))
6856                                 return 1;
6857                 }
6858         }
6859         return 0;
6860 }
6861 #else
6862 #define ipr_invalid_adapter(ioa_cfg) 0
6863 #endif
6864
6865 /**
6866  * ipr_ioa_bringdown_done - IOA bring down completion.
6867  * @ipr_cmd:    ipr command struct
6868  *
6869  * This function processes the completion of an adapter bring down.
6870  * It wakes any reset sleepers.
6871  *
6872  * Return value:
6873  *      IPR_RC_JOB_RETURN
6874  **/
6875 static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
6876 {
6877         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6878         int i;
6879
6880         ENTER;
6881         if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa) {
6882                 ipr_trace;
6883                 spin_unlock_irq(ioa_cfg->host->host_lock);
6884                 scsi_unblock_requests(ioa_cfg->host);
6885                 spin_lock_irq(ioa_cfg->host->host_lock);
6886         }
6887
6888         ioa_cfg->in_reset_reload = 0;
6889         ioa_cfg->reset_retries = 0;
6890         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
6891                 spin_lock(&ioa_cfg->hrrq[i]._lock);
6892                 ioa_cfg->hrrq[i].ioa_is_dead = 1;
6893                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
6894         }
6895         wmb();
6896
6897         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6898         wake_up_all(&ioa_cfg->reset_wait_q);
6899         LEAVE;
6900
6901         return IPR_RC_JOB_RETURN;
6902 }
6903
6904 /**
6905  * ipr_ioa_reset_done - IOA reset completion.
6906  * @ipr_cmd:    ipr command struct
6907  *
6908  * This function processes the completion of an adapter reset.
6909  * It schedules any necessary mid-layer add/removes and
6910  * wakes any reset sleepers.
6911  *
6912  * Return value:
6913  *      IPR_RC_JOB_RETURN
6914  **/
6915 static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
6916 {
6917         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6918         struct ipr_resource_entry *res;
6919         struct ipr_hostrcb *hostrcb, *temp;
6920         int i = 0, j;
6921
6922         ENTER;
6923         ioa_cfg->in_reset_reload = 0;
6924         for (j = 0; j < ioa_cfg->hrrq_num; j++) {
6925                 spin_lock(&ioa_cfg->hrrq[j]._lock);
6926                 ioa_cfg->hrrq[j].allow_cmds = 1;
6927                 spin_unlock(&ioa_cfg->hrrq[j]._lock);
6928         }
6929         wmb();
6930         ioa_cfg->reset_cmd = NULL;
6931         ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
6932
6933         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
6934                 if (res->add_to_ml || res->del_from_ml) {
6935                         ipr_trace;
6936                         break;
6937                 }
6938         }
6939         schedule_work(&ioa_cfg->work_q);
6940
6941         list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
6942                 list_del(&hostrcb->queue);
6943                 if (i++ < IPR_NUM_LOG_HCAMS)
6944                         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
6945                 else
6946                         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
6947         }
6948
6949         scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
6950         dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
6951
6952         ioa_cfg->reset_retries = 0;
6953         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6954         wake_up_all(&ioa_cfg->reset_wait_q);
6955
6956         spin_unlock(ioa_cfg->host->host_lock);
6957         scsi_unblock_requests(ioa_cfg->host);
6958         spin_lock(ioa_cfg->host->host_lock);
6959
6960         if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds)
6961                 scsi_block_requests(ioa_cfg->host);
6962
6963         schedule_work(&ioa_cfg->work_q);
6964         LEAVE;
6965         return IPR_RC_JOB_RETURN;
6966 }
6967
6968 /**
6969  * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
6970  * @supported_dev:      supported device struct
6971  * @vpids:                      vendor product id struct
6972  *
6973  * Return value:
6974  *      none
6975  **/
6976 static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
6977                                  struct ipr_std_inq_vpids *vpids)
6978 {
6979         memset(supported_dev, 0, sizeof(struct ipr_supported_device));
6980         memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
6981         supported_dev->num_records = 1;
6982         supported_dev->data_length =
6983                 cpu_to_be16(sizeof(struct ipr_supported_device));
6984         supported_dev->reserved = 0;
6985 }
6986
6987 /**
6988  * ipr_set_supported_devs - Send Set Supported Devices for a device
6989  * @ipr_cmd:    ipr command struct
6990  *
6991  * This function sends a Set Supported Devices to the adapter
6992  *
6993  * Return value:
6994  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6995  **/
6996 static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
6997 {
6998         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6999         struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
7000         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7001         struct ipr_resource_entry *res = ipr_cmd->u.res;
7002
7003         ipr_cmd->job_step = ipr_ioa_reset_done;
7004
7005         list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
7006                 if (!ipr_is_scsi_disk(res))
7007                         continue;
7008
7009                 ipr_cmd->u.res = res;
7010                 ipr_set_sup_dev_dflt(supp_dev, &res->std_inq_data.vpids);
7011
7012                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7013                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
7014                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7015
7016                 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
7017                 ioarcb->cmd_pkt.cdb[1] = IPR_SET_ALL_SUPPORTED_DEVICES;
7018                 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
7019                 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
7020
7021                 ipr_init_ioadl(ipr_cmd,
7022                                ioa_cfg->vpd_cbs_dma +
7023                                  offsetof(struct ipr_misc_cbs, supp_dev),
7024                                sizeof(struct ipr_supported_device),
7025                                IPR_IOADL_FLAGS_WRITE_LAST);
7026
7027                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
7028                            IPR_SET_SUP_DEVICE_TIMEOUT);
7029
7030                 if (!ioa_cfg->sis64)
7031                         ipr_cmd->job_step = ipr_set_supported_devs;
7032                 LEAVE;
7033                 return IPR_RC_JOB_RETURN;
7034         }
7035
7036         LEAVE;
7037         return IPR_RC_JOB_CONTINUE;
7038 }
7039
7040 /**
7041  * ipr_get_mode_page - Locate specified mode page
7042  * @mode_pages: mode page buffer
7043  * @page_code:  page code to find
7044  * @len:                minimum required length for mode page
7045  *
7046  * Return value:
7047  *      pointer to mode page / NULL on failure
7048  **/
7049 static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
7050                                u32 page_code, u32 len)
7051 {
7052         struct ipr_mode_page_hdr *mode_hdr;
7053         u32 page_length;
7054         u32 length;
7055
7056         if (!mode_pages || (mode_pages->hdr.length == 0))
7057                 return NULL;
7058
7059         length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
7060         mode_hdr = (struct ipr_mode_page_hdr *)
7061                 (mode_pages->data + mode_pages->hdr.block_desc_len);
7062
7063         while (length) {
7064                 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
7065                         if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
7066                                 return mode_hdr;
7067                         break;
7068                 } else {
7069                         page_length = (sizeof(struct ipr_mode_page_hdr) +
7070                                        mode_hdr->page_length);
7071                         length -= page_length;
7072                         mode_hdr = (struct ipr_mode_page_hdr *)
7073                                 ((unsigned long)mode_hdr + page_length);
7074                 }
7075         }
7076         return NULL;
7077 }
7078
7079 /**
7080  * ipr_check_term_power - Check for term power errors
7081  * @ioa_cfg:    ioa config struct
7082  * @mode_pages: IOAFP mode pages buffer
7083  *
7084  * Check the IOAFP's mode page 28 for term power errors
7085  *
7086  * Return value:
7087  *      nothing
7088  **/
7089 static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
7090                                  struct ipr_mode_pages *mode_pages)
7091 {
7092         int i;
7093         int entry_length;
7094         struct ipr_dev_bus_entry *bus;
7095         struct ipr_mode_page28 *mode_page;
7096
7097         mode_page = ipr_get_mode_page(mode_pages, 0x28,
7098                                       sizeof(struct ipr_mode_page28));
7099
7100         entry_length = mode_page->entry_length;
7101
7102         bus = mode_page->bus;
7103
7104         for (i = 0; i < mode_page->num_entries; i++) {
7105                 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
7106                         dev_err(&ioa_cfg->pdev->dev,
7107                                 "Term power is absent on scsi bus %d\n",
7108                                 bus->res_addr.bus);
7109                 }
7110
7111                 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
7112         }
7113 }
7114
7115 /**
7116  * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
7117  * @ioa_cfg:    ioa config struct
7118  *
7119  * Looks through the config table checking for SES devices. If
7120  * the SES device is in the SES table indicating a maximum SCSI
7121  * bus speed, the speed is limited for the bus.
7122  *
7123  * Return value:
7124  *      none
7125  **/
7126 static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
7127 {
7128         u32 max_xfer_rate;
7129         int i;
7130
7131         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
7132                 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
7133                                                        ioa_cfg->bus_attr[i].bus_width);
7134
7135                 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
7136                         ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
7137         }
7138 }
7139
7140 /**
7141  * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
7142  * @ioa_cfg:    ioa config struct
7143  * @mode_pages: mode page 28 buffer
7144  *
7145  * Updates mode page 28 based on driver configuration
7146  *
7147  * Return value:
7148  *      none
7149  **/
7150 static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
7151                                           struct ipr_mode_pages *mode_pages)
7152 {
7153         int i, entry_length;
7154         struct ipr_dev_bus_entry *bus;
7155         struct ipr_bus_attributes *bus_attr;
7156         struct ipr_mode_page28 *mode_page;
7157
7158         mode_page = ipr_get_mode_page(mode_pages, 0x28,
7159                                       sizeof(struct ipr_mode_page28));
7160
7161         entry_length = mode_page->entry_length;
7162
7163         /* Loop for each device bus entry */
7164         for (i = 0, bus = mode_page->bus;
7165              i < mode_page->num_entries;
7166              i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
7167                 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
7168                         dev_err(&ioa_cfg->pdev->dev,
7169                                 "Invalid resource address reported: 0x%08X\n",
7170                                 IPR_GET_PHYS_LOC(bus->res_addr));
7171                         continue;
7172                 }
7173
7174                 bus_attr = &ioa_cfg->bus_attr[i];
7175                 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
7176                 bus->bus_width = bus_attr->bus_width;
7177                 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
7178                 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
7179                 if (bus_attr->qas_enabled)
7180                         bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
7181                 else
7182                         bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
7183         }
7184 }
7185
7186 /**
7187  * ipr_build_mode_select - Build a mode select command
7188  * @ipr_cmd:    ipr command struct
7189  * @res_handle: resource handle to send command to
7190  * @parm:               Byte 2 of Mode Sense command
7191  * @dma_addr:   DMA buffer address
7192  * @xfer_len:   data transfer length
7193  *
7194  * Return value:
7195  *      none
7196  **/
7197 static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
7198                                   __be32 res_handle, u8 parm,
7199                                   dma_addr_t dma_addr, u8 xfer_len)
7200 {
7201         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7202
7203         ioarcb->res_handle = res_handle;
7204         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7205         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
7206         ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
7207         ioarcb->cmd_pkt.cdb[1] = parm;
7208         ioarcb->cmd_pkt.cdb[4] = xfer_len;
7209
7210         ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_WRITE_LAST);
7211 }
7212
7213 /**
7214  * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
7215  * @ipr_cmd:    ipr command struct
7216  *
7217  * This function sets up the SCSI bus attributes and sends
7218  * a Mode Select for Page 28 to activate them.
7219  *
7220  * Return value:
7221  *      IPR_RC_JOB_RETURN
7222  **/
7223 static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
7224 {
7225         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7226         struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
7227         int length;
7228
7229         ENTER;
7230         ipr_scsi_bus_speed_limit(ioa_cfg);
7231         ipr_check_term_power(ioa_cfg, mode_pages);
7232         ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
7233         length = mode_pages->hdr.length + 1;
7234         mode_pages->hdr.length = 0;
7235
7236         ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
7237                               ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
7238                               length);
7239
7240         ipr_cmd->job_step = ipr_set_supported_devs;
7241         ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
7242                                     struct ipr_resource_entry, queue);
7243         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7244
7245         LEAVE;
7246         return IPR_RC_JOB_RETURN;
7247 }
7248
7249 /**
7250  * ipr_build_mode_sense - Builds a mode sense command
7251  * @ipr_cmd:    ipr command struct
7252  * @res:                resource entry struct
7253  * @parm:               Byte 2 of mode sense command
7254  * @dma_addr:   DMA address of mode sense buffer
7255  * @xfer_len:   Size of DMA buffer
7256  *
7257  * Return value:
7258  *      none
7259  **/
7260 static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
7261                                  __be32 res_handle,
7262                                  u8 parm, dma_addr_t dma_addr, u8 xfer_len)
7263 {
7264         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7265
7266         ioarcb->res_handle = res_handle;
7267         ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
7268         ioarcb->cmd_pkt.cdb[2] = parm;
7269         ioarcb->cmd_pkt.cdb[4] = xfer_len;
7270         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7271
7272         ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
7273 }
7274
7275 /**
7276  * ipr_reset_cmd_failed - Handle failure of IOA reset command
7277  * @ipr_cmd:    ipr command struct
7278  *
7279  * This function handles the failure of an IOA bringup command.
7280  *
7281  * Return value:
7282  *      IPR_RC_JOB_RETURN
7283  **/
7284 static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
7285 {
7286         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7287         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
7288
7289         dev_err(&ioa_cfg->pdev->dev,
7290                 "0x%02X failed with IOASC: 0x%08X\n",
7291                 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
7292
7293         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7294         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
7295         return IPR_RC_JOB_RETURN;
7296 }
7297
7298 /**
7299  * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
7300  * @ipr_cmd:    ipr command struct
7301  *
7302  * This function handles the failure of a Mode Sense to the IOAFP.
7303  * Some adapters do not handle all mode pages.
7304  *
7305  * Return value:
7306  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7307  **/
7308 static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
7309 {
7310         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7311         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
7312
7313         if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
7314                 ipr_cmd->job_step = ipr_set_supported_devs;
7315                 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
7316                                             struct ipr_resource_entry, queue);
7317                 return IPR_RC_JOB_CONTINUE;
7318         }
7319
7320         return ipr_reset_cmd_failed(ipr_cmd);
7321 }
7322
7323 /**
7324  * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
7325  * @ipr_cmd:    ipr command struct
7326  *
7327  * This function send a Page 28 mode sense to the IOA to
7328  * retrieve SCSI bus attributes.
7329  *
7330  * Return value:
7331  *      IPR_RC_JOB_RETURN
7332  **/
7333 static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
7334 {
7335         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7336
7337         ENTER;
7338         ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
7339                              0x28, ioa_cfg->vpd_cbs_dma +
7340                              offsetof(struct ipr_misc_cbs, mode_pages),
7341                              sizeof(struct ipr_mode_pages));
7342
7343         ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
7344         ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
7345
7346         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7347
7348         LEAVE;
7349         return IPR_RC_JOB_RETURN;
7350 }
7351
7352 /**
7353  * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
7354  * @ipr_cmd:    ipr command struct
7355  *
7356  * This function enables dual IOA RAID support if possible.
7357  *
7358  * Return value:
7359  *      IPR_RC_JOB_RETURN
7360  **/
7361 static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
7362 {
7363         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7364         struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
7365         struct ipr_mode_page24 *mode_page;
7366         int length;
7367
7368         ENTER;
7369         mode_page = ipr_get_mode_page(mode_pages, 0x24,
7370                                       sizeof(struct ipr_mode_page24));
7371
7372         if (mode_page)
7373                 mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;
7374
7375         length = mode_pages->hdr.length + 1;
7376         mode_pages->hdr.length = 0;
7377
7378         ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
7379                               ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
7380                               length);
7381
7382         ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
7383         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7384
7385         LEAVE;
7386         return IPR_RC_JOB_RETURN;
7387 }
7388
7389 /**
7390  * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
7391  * @ipr_cmd:    ipr command struct
7392  *
7393  * This function handles the failure of a Mode Sense to the IOAFP.
7394  * Some adapters do not handle all mode pages.
7395  *
7396  * Return value:
7397  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7398  **/
7399 static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
7400 {
7401         u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
7402
7403         if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
7404                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
7405                 return IPR_RC_JOB_CONTINUE;
7406         }
7407
7408         return ipr_reset_cmd_failed(ipr_cmd);
7409 }
7410
7411 /**
7412  * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
7413  * @ipr_cmd:    ipr command struct
7414  *
7415  * This function send a mode sense to the IOA to retrieve
7416  * the IOA Advanced Function Control mode page.
7417  *
7418  * Return value:
7419  *      IPR_RC_JOB_RETURN
7420  **/
7421 static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
7422 {
7423         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7424
7425         ENTER;
7426         ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
7427                              0x24, ioa_cfg->vpd_cbs_dma +
7428                              offsetof(struct ipr_misc_cbs, mode_pages),
7429                              sizeof(struct ipr_mode_pages));
7430
7431         ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
7432         ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;
7433
7434         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7435
7436         LEAVE;
7437         return IPR_RC_JOB_RETURN;
7438 }
7439
7440 /**
7441  * ipr_init_res_table - Initialize the resource table
7442  * @ipr_cmd:    ipr command struct
7443  *
7444  * This function looks through the existing resource table, comparing
7445  * it with the config table. This function will take care of old/new
7446  * devices and schedule adding/removing them from the mid-layer
7447  * as appropriate.
7448  *
7449  * Return value:
7450  *      IPR_RC_JOB_CONTINUE
7451  **/
7452 static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
7453 {
7454         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7455         struct ipr_resource_entry *res, *temp;
7456         struct ipr_config_table_entry_wrapper cfgtew;
7457         int entries, found, flag, i;
7458         LIST_HEAD(old_res);
7459
7460         ENTER;
7461         if (ioa_cfg->sis64)
7462                 flag = ioa_cfg->u.cfg_table64->hdr64.flags;
7463         else
7464                 flag = ioa_cfg->u.cfg_table->hdr.flags;
7465
7466         if (flag & IPR_UCODE_DOWNLOAD_REQ)
7467                 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
7468
7469         list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
7470                 list_move_tail(&res->queue, &old_res);
7471
7472         if (ioa_cfg->sis64)
7473                 entries = be16_to_cpu(ioa_cfg->u.cfg_table64->hdr64.num_entries);
7474         else
7475                 entries = ioa_cfg->u.cfg_table->hdr.num_entries;
7476
7477         for (i = 0; i < entries; i++) {
7478                 if (ioa_cfg->sis64)
7479                         cfgtew.u.cfgte64 = &ioa_cfg->u.cfg_table64->dev[i];
7480                 else
7481                         cfgtew.u.cfgte = &ioa_cfg->u.cfg_table->dev[i];
7482                 found = 0;
7483
7484                 list_for_each_entry_safe(res, temp, &old_res, queue) {
7485                         if (ipr_is_same_device(res, &cfgtew)) {
7486                                 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
7487                                 found = 1;
7488                                 break;
7489                         }
7490                 }
7491
7492                 if (!found) {
7493                         if (list_empty(&ioa_cfg->free_res_q)) {
7494                                 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
7495                                 break;
7496                         }
7497
7498                         found = 1;
7499                         res = list_entry(ioa_cfg->free_res_q.next,
7500                                          struct ipr_resource_entry, queue);
7501                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
7502                         ipr_init_res_entry(res, &cfgtew);
7503                         res->add_to_ml = 1;
7504                 } else if (res->sdev && (ipr_is_vset_device(res) || ipr_is_scsi_disk(res)))
7505                         res->sdev->allow_restart = 1;
7506
7507                 if (found)
7508                         ipr_update_res_entry(res, &cfgtew);
7509         }
7510
7511         list_for_each_entry_safe(res, temp, &old_res, queue) {
7512                 if (res->sdev) {
7513                         res->del_from_ml = 1;
7514                         res->res_handle = IPR_INVALID_RES_HANDLE;
7515                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
7516                 }
7517         }
7518
7519         list_for_each_entry_safe(res, temp, &old_res, queue) {
7520                 ipr_clear_res_target(res);
7521                 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
7522         }
7523
7524         if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
7525                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
7526         else
7527                 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
7528
7529         LEAVE;
7530         return IPR_RC_JOB_CONTINUE;
7531 }
7532
7533 /**
7534  * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
7535  * @ipr_cmd:    ipr command struct
7536  *
7537  * This function sends a Query IOA Configuration command
7538  * to the adapter to retrieve the IOA configuration table.
7539  *
7540  * Return value:
7541  *      IPR_RC_JOB_RETURN
7542  **/
7543 static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
7544 {
7545         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7546         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7547         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
7548         struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
7549
7550         ENTER;
7551         if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
7552                 ioa_cfg->dual_raid = 1;
7553         dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
7554                  ucode_vpd->major_release, ucode_vpd->card_type,
7555                  ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
7556         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7557         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7558
7559         ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
7560         ioarcb->cmd_pkt.cdb[6] = (ioa_cfg->cfg_table_size >> 16) & 0xff;
7561         ioarcb->cmd_pkt.cdb[7] = (ioa_cfg->cfg_table_size >> 8) & 0xff;
7562         ioarcb->cmd_pkt.cdb[8] = ioa_cfg->cfg_table_size & 0xff;
7563
7564         ipr_init_ioadl(ipr_cmd, ioa_cfg->cfg_table_dma, ioa_cfg->cfg_table_size,
7565                        IPR_IOADL_FLAGS_READ_LAST);
7566
7567         ipr_cmd->job_step = ipr_init_res_table;
7568
7569         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7570
7571         LEAVE;
7572         return IPR_RC_JOB_RETURN;
7573 }
7574
7575 /**
7576  * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
7577  * @ipr_cmd:    ipr command struct
7578  *
7579  * This utility function sends an inquiry to the adapter.
7580  *
7581  * Return value:
7582  *      none
7583  **/
7584 static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
7585                               dma_addr_t dma_addr, u8 xfer_len)
7586 {
7587         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7588
7589         ENTER;
7590         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7591         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7592
7593         ioarcb->cmd_pkt.cdb[0] = INQUIRY;
7594         ioarcb->cmd_pkt.cdb[1] = flags;
7595         ioarcb->cmd_pkt.cdb[2] = page;
7596         ioarcb->cmd_pkt.cdb[4] = xfer_len;
7597
7598         ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
7599
7600         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7601         LEAVE;
7602 }
7603
7604 /**
7605  * ipr_inquiry_page_supported - Is the given inquiry page supported
7606  * @page0:              inquiry page 0 buffer
7607  * @page:               page code.
7608  *
7609  * This function determines if the specified inquiry page is supported.
7610  *
7611  * Return value:
7612  *      1 if page is supported / 0 if not
7613  **/
7614 static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
7615 {
7616         int i;
7617
7618         for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
7619                 if (page0->page[i] == page)
7620                         return 1;
7621
7622         return 0;
7623 }
7624
7625 /**
7626  * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
7627  * @ipr_cmd:    ipr command struct
7628  *
7629  * This function sends a Page 0xD0 inquiry to the adapter
7630  * to retrieve adapter capabilities.
7631  *
7632  * Return value:
7633  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7634  **/
7635 static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
7636 {
7637         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7638         struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
7639         struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
7640
7641         ENTER;
7642         ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
7643         memset(cap, 0, sizeof(*cap));
7644
7645         if (ipr_inquiry_page_supported(page0, 0xD0)) {
7646                 ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
7647                                   ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
7648                                   sizeof(struct ipr_inquiry_cap));
7649                 return IPR_RC_JOB_RETURN;
7650         }
7651
7652         LEAVE;
7653         return IPR_RC_JOB_CONTINUE;
7654 }
7655
7656 /**
7657  * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
7658  * @ipr_cmd:    ipr command struct
7659  *
7660  * This function sends a Page 3 inquiry to the adapter
7661  * to retrieve software VPD information.
7662  *
7663  * Return value:
7664  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7665  **/
7666 static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
7667 {
7668         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7669
7670         ENTER;
7671
7672         ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
7673
7674         ipr_ioafp_inquiry(ipr_cmd, 1, 3,
7675                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
7676                           sizeof(struct ipr_inquiry_page3));
7677
7678         LEAVE;
7679         return IPR_RC_JOB_RETURN;
7680 }
7681
7682 /**
7683  * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
7684  * @ipr_cmd:    ipr command struct
7685  *
7686  * This function sends a Page 0 inquiry to the adapter
7687  * to retrieve supported inquiry pages.
7688  *
7689  * Return value:
7690  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7691  **/
7692 static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
7693 {
7694         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7695         char type[5];
7696
7697         ENTER;
7698
7699         /* Grab the type out of the VPD and store it away */
7700         memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
7701         type[4] = '\0';
7702         ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
7703
7704         if (ipr_invalid_adapter(ioa_cfg)) {
7705                 dev_err(&ioa_cfg->pdev->dev,
7706                         "Adapter not supported in this hardware configuration.\n");
7707
7708                 if (!ipr_testmode) {
7709                         ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
7710                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7711                         list_add_tail(&ipr_cmd->queue,
7712                                         &ioa_cfg->hrrq->hrrq_free_q);
7713                         return IPR_RC_JOB_RETURN;
7714                 }
7715         }
7716
7717         ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
7718
7719         ipr_ioafp_inquiry(ipr_cmd, 1, 0,
7720                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
7721                           sizeof(struct ipr_inquiry_page0));
7722
7723         LEAVE;
7724         return IPR_RC_JOB_RETURN;
7725 }
7726
7727 /**
7728  * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
7729  * @ipr_cmd:    ipr command struct
7730  *
7731  * This function sends a standard inquiry to the adapter.
7732  *
7733  * Return value:
7734  *      IPR_RC_JOB_RETURN
7735  **/
7736 static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
7737 {
7738         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7739
7740         ENTER;
7741         ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
7742
7743         ipr_ioafp_inquiry(ipr_cmd, 0, 0,
7744                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
7745                           sizeof(struct ipr_ioa_vpd));
7746
7747         LEAVE;
7748         return IPR_RC_JOB_RETURN;
7749 }
7750
7751 /**
7752  * ipr_ioafp_identify_hrrq - Send Identify Host RRQ.
7753  * @ipr_cmd:    ipr command struct
7754  *
7755  * This function send an Identify Host Request Response Queue
7756  * command to establish the HRRQ with the adapter.
7757  *
7758  * Return value:
7759  *      IPR_RC_JOB_RETURN
7760  **/
7761 static int ipr_ioafp_identify_hrrq(struct ipr_cmnd *ipr_cmd)
7762 {
7763         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7764         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7765         struct ipr_hrr_queue *hrrq;
7766
7767         ENTER;
7768         ipr_cmd->job_step = ipr_ioafp_std_inquiry;
7769         dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
7770
7771         if (ioa_cfg->identify_hrrq_index < ioa_cfg->hrrq_num) {
7772                 hrrq = &ioa_cfg->hrrq[ioa_cfg->identify_hrrq_index];
7773
7774                 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
7775                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7776
7777                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7778                 if (ioa_cfg->sis64)
7779                         ioarcb->cmd_pkt.cdb[1] = 0x1;
7780
7781                 if (ioa_cfg->nvectors == 1)
7782                         ioarcb->cmd_pkt.cdb[1] &= ~IPR_ID_HRRQ_SELE_ENABLE;
7783                 else
7784                         ioarcb->cmd_pkt.cdb[1] |= IPR_ID_HRRQ_SELE_ENABLE;
7785
7786                 ioarcb->cmd_pkt.cdb[2] =
7787                         ((u64) hrrq->host_rrq_dma >> 24) & 0xff;
7788                 ioarcb->cmd_pkt.cdb[3] =
7789                         ((u64) hrrq->host_rrq_dma >> 16) & 0xff;
7790                 ioarcb->cmd_pkt.cdb[4] =
7791                         ((u64) hrrq->host_rrq_dma >> 8) & 0xff;
7792                 ioarcb->cmd_pkt.cdb[5] =
7793                         ((u64) hrrq->host_rrq_dma) & 0xff;
7794                 ioarcb->cmd_pkt.cdb[7] =
7795                         ((sizeof(u32) * hrrq->size) >> 8) & 0xff;
7796                 ioarcb->cmd_pkt.cdb[8] =
7797                         (sizeof(u32) * hrrq->size) & 0xff;
7798
7799                 if (ioarcb->cmd_pkt.cdb[1] & IPR_ID_HRRQ_SELE_ENABLE)
7800                         ioarcb->cmd_pkt.cdb[9] =
7801                                         ioa_cfg->identify_hrrq_index;
7802
7803                 if (ioa_cfg->sis64) {
7804                         ioarcb->cmd_pkt.cdb[10] =
7805                                 ((u64) hrrq->host_rrq_dma >> 56) & 0xff;
7806                         ioarcb->cmd_pkt.cdb[11] =
7807                                 ((u64) hrrq->host_rrq_dma >> 48) & 0xff;
7808                         ioarcb->cmd_pkt.cdb[12] =
7809                                 ((u64) hrrq->host_rrq_dma >> 40) & 0xff;
7810                         ioarcb->cmd_pkt.cdb[13] =
7811                                 ((u64) hrrq->host_rrq_dma >> 32) & 0xff;
7812                 }
7813
7814                 if (ioarcb->cmd_pkt.cdb[1] & IPR_ID_HRRQ_SELE_ENABLE)
7815                         ioarcb->cmd_pkt.cdb[14] =
7816                                         ioa_cfg->identify_hrrq_index;
7817
7818                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
7819                            IPR_INTERNAL_TIMEOUT);
7820
7821                 if (++ioa_cfg->identify_hrrq_index < ioa_cfg->hrrq_num)
7822                         ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7823
7824                 LEAVE;
7825                 return IPR_RC_JOB_RETURN;
7826         }
7827
7828         LEAVE;
7829         return IPR_RC_JOB_CONTINUE;
7830 }
7831
7832 /**
7833  * ipr_reset_timer_done - Adapter reset timer function
7834  * @ipr_cmd:    ipr command struct
7835  *
7836  * Description: This function is used in adapter reset processing
7837  * for timing events. If the reset_cmd pointer in the IOA
7838  * config struct is not this adapter's we are doing nested
7839  * resets and fail_all_ops will take care of freeing the
7840  * command block.
7841  *
7842  * Return value:
7843  *      none
7844  **/
7845 static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
7846 {
7847         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7848         unsigned long lock_flags = 0;
7849
7850         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7851
7852         if (ioa_cfg->reset_cmd == ipr_cmd) {
7853                 list_del(&ipr_cmd->queue);
7854                 ipr_cmd->done(ipr_cmd);
7855         }
7856
7857         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7858 }
7859
7860 /**
7861  * ipr_reset_start_timer - Start a timer for adapter reset job
7862  * @ipr_cmd:    ipr command struct
7863  * @timeout:    timeout value
7864  *
7865  * Description: This function is used in adapter reset processing
7866  * for timing events. If the reset_cmd pointer in the IOA
7867  * config struct is not this adapter's we are doing nested
7868  * resets and fail_all_ops will take care of freeing the
7869  * command block.
7870  *
7871  * Return value:
7872  *      none
7873  **/
7874 static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
7875                                   unsigned long timeout)
7876 {
7877
7878         ENTER;
7879         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
7880         ipr_cmd->done = ipr_reset_ioa_job;
7881
7882         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
7883         ipr_cmd->timer.expires = jiffies + timeout;
7884         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
7885         add_timer(&ipr_cmd->timer);
7886 }
7887
7888 /**
7889  * ipr_init_ioa_mem - Initialize ioa_cfg control block
7890  * @ioa_cfg:    ioa cfg struct
7891  *
7892  * Return value:
7893  *      nothing
7894  **/
7895 static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
7896 {
7897         struct ipr_hrr_queue *hrrq;
7898
7899         for_each_hrrq(hrrq, ioa_cfg) {
7900                 spin_lock(&hrrq->_lock);
7901                 memset(hrrq->host_rrq, 0, sizeof(u32) * hrrq->size);
7902
7903                 /* Initialize Host RRQ pointers */
7904                 hrrq->hrrq_start = hrrq->host_rrq;
7905                 hrrq->hrrq_end = &hrrq->host_rrq[hrrq->size - 1];
7906                 hrrq->hrrq_curr = hrrq->hrrq_start;
7907                 hrrq->toggle_bit = 1;
7908                 spin_unlock(&hrrq->_lock);
7909         }
7910         wmb();
7911
7912         ioa_cfg->identify_hrrq_index = 0;
7913         if (ioa_cfg->hrrq_num == 1)
7914                 atomic_set(&ioa_cfg->hrrq_index, 0);
7915         else
7916                 atomic_set(&ioa_cfg->hrrq_index, 1);
7917
7918         /* Zero out config table */
7919         memset(ioa_cfg->u.cfg_table, 0, ioa_cfg->cfg_table_size);
7920 }
7921
7922 /**
7923  * ipr_reset_next_stage - Process IPL stage change based on feedback register.
7924  * @ipr_cmd:    ipr command struct
7925  *
7926  * Return value:
7927  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7928  **/
7929 static int ipr_reset_next_stage(struct ipr_cmnd *ipr_cmd)
7930 {
7931         unsigned long stage, stage_time;
7932         u32 feedback;
7933         volatile u32 int_reg;
7934         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7935         u64 maskval = 0;
7936
7937         feedback = readl(ioa_cfg->regs.init_feedback_reg);
7938         stage = feedback & IPR_IPL_INIT_STAGE_MASK;
7939         stage_time = feedback & IPR_IPL_INIT_STAGE_TIME_MASK;
7940
7941         ipr_dbg("IPL stage = 0x%lx, IPL stage time = %ld\n", stage, stage_time);
7942
7943         /* sanity check the stage_time value */
7944         if (stage_time == 0)
7945                 stage_time = IPR_IPL_INIT_DEFAULT_STAGE_TIME;
7946         else if (stage_time < IPR_IPL_INIT_MIN_STAGE_TIME)
7947                 stage_time = IPR_IPL_INIT_MIN_STAGE_TIME;
7948         else if (stage_time > IPR_LONG_OPERATIONAL_TIMEOUT)
7949                 stage_time = IPR_LONG_OPERATIONAL_TIMEOUT;
7950
7951         if (stage == IPR_IPL_INIT_STAGE_UNKNOWN) {
7952                 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.set_interrupt_mask_reg);
7953                 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7954                 stage_time = ioa_cfg->transop_timeout;
7955                 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7956         } else if (stage == IPR_IPL_INIT_STAGE_TRANSOP) {
7957                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
7958                 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
7959                         ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7960                         maskval = IPR_PCII_IPL_STAGE_CHANGE;
7961                         maskval = (maskval << 32) | IPR_PCII_IOA_TRANS_TO_OPER;
7962                         writeq(maskval, ioa_cfg->regs.set_interrupt_mask_reg);
7963                         int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7964                         return IPR_RC_JOB_CONTINUE;
7965                 }
7966         }
7967
7968         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
7969         ipr_cmd->timer.expires = jiffies + stage_time * HZ;
7970         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
7971         ipr_cmd->done = ipr_reset_ioa_job;
7972         add_timer(&ipr_cmd->timer);
7973
7974         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
7975
7976         return IPR_RC_JOB_RETURN;
7977 }
7978
7979 /**
7980  * ipr_reset_enable_ioa - Enable the IOA following a reset.
7981  * @ipr_cmd:    ipr command struct
7982  *
7983  * This function reinitializes some control blocks and
7984  * enables destructive diagnostics on the adapter.
7985  *
7986  * Return value:
7987  *      IPR_RC_JOB_RETURN
7988  **/
7989 static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
7990 {
7991         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7992         volatile u32 int_reg;
7993         volatile u64 maskval;
7994         int i;
7995
7996         ENTER;
7997         ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7998         ipr_init_ioa_mem(ioa_cfg);
7999
8000         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8001                 spin_lock(&ioa_cfg->hrrq[i]._lock);
8002                 ioa_cfg->hrrq[i].allow_interrupts = 1;
8003                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
8004         }
8005         wmb();
8006         if (ioa_cfg->sis64) {
8007                 /* Set the adapter to the correct endian mode. */
8008                 writel(IPR_ENDIAN_SWAP_KEY, ioa_cfg->regs.endian_swap_reg);
8009                 int_reg = readl(ioa_cfg->regs.endian_swap_reg);
8010         }
8011
8012         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
8013
8014         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
8015                 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
8016                        ioa_cfg->regs.clr_interrupt_mask_reg32);
8017                 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
8018                 return IPR_RC_JOB_CONTINUE;
8019         }
8020
8021         /* Enable destructive diagnostics on IOA */
8022         writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg32);
8023
8024         if (ioa_cfg->sis64) {
8025                 maskval = IPR_PCII_IPL_STAGE_CHANGE;
8026                 maskval = (maskval << 32) | IPR_PCII_OPER_INTERRUPTS;
8027                 writeq(maskval, ioa_cfg->regs.clr_interrupt_mask_reg);
8028         } else
8029                 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg32);
8030
8031         int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
8032
8033         dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
8034
8035         if (ioa_cfg->sis64) {
8036                 ipr_cmd->job_step = ipr_reset_next_stage;
8037                 return IPR_RC_JOB_CONTINUE;
8038         }
8039
8040         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
8041         ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
8042         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
8043         ipr_cmd->done = ipr_reset_ioa_job;
8044         add_timer(&ipr_cmd->timer);
8045         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
8046
8047         LEAVE;
8048         return IPR_RC_JOB_RETURN;
8049 }
8050
8051 /**
8052  * ipr_reset_wait_for_dump - Wait for a dump to timeout.
8053  * @ipr_cmd:    ipr command struct
8054  *
8055  * This function is invoked when an adapter dump has run out
8056  * of processing time.
8057  *
8058  * Return value:
8059  *      IPR_RC_JOB_CONTINUE
8060  **/
8061 static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
8062 {
8063         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8064
8065         if (ioa_cfg->sdt_state == GET_DUMP)
8066                 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
8067         else if (ioa_cfg->sdt_state == READ_DUMP)
8068                 ioa_cfg->sdt_state = ABORT_DUMP;
8069
8070         ioa_cfg->dump_timeout = 1;
8071         ipr_cmd->job_step = ipr_reset_alert;
8072
8073         return IPR_RC_JOB_CONTINUE;
8074 }
8075
8076 /**
8077  * ipr_unit_check_no_data - Log a unit check/no data error log
8078  * @ioa_cfg:            ioa config struct
8079  *
8080  * Logs an error indicating the adapter unit checked, but for some
8081  * reason, we were unable to fetch the unit check buffer.
8082  *
8083  * Return value:
8084  *      nothing
8085  **/
8086 static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
8087 {
8088         ioa_cfg->errors_logged++;
8089         dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
8090 }
8091
8092 /**
8093  * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
8094  * @ioa_cfg:            ioa config struct
8095  *
8096  * Fetches the unit check buffer from the adapter by clocking the data
8097  * through the mailbox register.
8098  *
8099  * Return value:
8100  *      nothing
8101  **/
8102 static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
8103 {
8104         unsigned long mailbox;
8105         struct ipr_hostrcb *hostrcb;
8106         struct ipr_uc_sdt sdt;
8107         int rc, length;
8108         u32 ioasc;
8109
8110         mailbox = readl(ioa_cfg->ioa_mailbox);
8111
8112         if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(mailbox)) {
8113                 ipr_unit_check_no_data(ioa_cfg);
8114                 return;
8115         }
8116
8117         memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
8118         rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
8119                                         (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
8120
8121         if (rc || !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY) ||
8122             ((be32_to_cpu(sdt.hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
8123             (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
8124                 ipr_unit_check_no_data(ioa_cfg);
8125                 return;
8126         }
8127
8128         /* Find length of the first sdt entry (UC buffer) */
8129         if (be32_to_cpu(sdt.hdr.state) == IPR_FMT3_SDT_READY_TO_USE)
8130                 length = be32_to_cpu(sdt.entry[0].end_token);
8131         else
8132                 length = (be32_to_cpu(sdt.entry[0].end_token) -
8133                           be32_to_cpu(sdt.entry[0].start_token)) &
8134                           IPR_FMT2_MBX_ADDR_MASK;
8135
8136         hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
8137                              struct ipr_hostrcb, queue);
8138         list_del(&hostrcb->queue);
8139         memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
8140
8141         rc = ipr_get_ldump_data_section(ioa_cfg,
8142                                         be32_to_cpu(sdt.entry[0].start_token),
8143                                         (__be32 *)&hostrcb->hcam,
8144                                         min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
8145
8146         if (!rc) {
8147                 ipr_handle_log_data(ioa_cfg, hostrcb);
8148                 ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
8149                 if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
8150                     ioa_cfg->sdt_state == GET_DUMP)
8151                         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
8152         } else
8153                 ipr_unit_check_no_data(ioa_cfg);
8154
8155         list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
8156 }
8157
8158 /**
8159  * ipr_reset_get_unit_check_job - Call to get the unit check buffer.
8160  * @ipr_cmd:    ipr command struct
8161  *
8162  * Description: This function will call to get the unit check buffer.
8163  *
8164  * Return value:
8165  *      IPR_RC_JOB_RETURN
8166  **/
8167 static int ipr_reset_get_unit_check_job(struct ipr_cmnd *ipr_cmd)
8168 {
8169         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8170
8171         ENTER;
8172         ioa_cfg->ioa_unit_checked = 0;
8173         ipr_get_unit_check_buffer(ioa_cfg);
8174         ipr_cmd->job_step = ipr_reset_alert;
8175         ipr_reset_start_timer(ipr_cmd, 0);
8176
8177         LEAVE;
8178         return IPR_RC_JOB_RETURN;
8179 }
8180
8181 /**
8182  * ipr_reset_restore_cfg_space - Restore PCI config space.
8183  * @ipr_cmd:    ipr command struct
8184  *
8185  * Description: This function restores the saved PCI config space of
8186  * the adapter, fails all outstanding ops back to the callers, and
8187  * fetches the dump/unit check if applicable to this reset.
8188  *
8189  * Return value:
8190  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8191  **/
8192 static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
8193 {
8194         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8195         u32 int_reg;
8196
8197         ENTER;
8198         ioa_cfg->pdev->state_saved = true;
8199         pci_restore_state(ioa_cfg->pdev);
8200
8201         if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
8202                 ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
8203                 return IPR_RC_JOB_CONTINUE;
8204         }
8205
8206         ipr_fail_all_ops(ioa_cfg);
8207
8208         if (ioa_cfg->sis64) {
8209                 /* Set the adapter to the correct endian mode. */
8210                 writel(IPR_ENDIAN_SWAP_KEY, ioa_cfg->regs.endian_swap_reg);
8211                 int_reg = readl(ioa_cfg->regs.endian_swap_reg);
8212         }
8213
8214         if (ioa_cfg->ioa_unit_checked) {
8215                 if (ioa_cfg->sis64) {
8216                         ipr_cmd->job_step = ipr_reset_get_unit_check_job;
8217                         ipr_reset_start_timer(ipr_cmd, IPR_DUMP_DELAY_TIMEOUT);
8218                         return IPR_RC_JOB_RETURN;
8219                 } else {
8220                         ioa_cfg->ioa_unit_checked = 0;
8221                         ipr_get_unit_check_buffer(ioa_cfg);
8222                         ipr_cmd->job_step = ipr_reset_alert;
8223                         ipr_reset_start_timer(ipr_cmd, 0);
8224                         return IPR_RC_JOB_RETURN;
8225                 }
8226         }
8227
8228         if (ioa_cfg->in_ioa_bringdown) {
8229                 ipr_cmd->job_step = ipr_ioa_bringdown_done;
8230         } else {
8231                 ipr_cmd->job_step = ipr_reset_enable_ioa;
8232
8233                 if (GET_DUMP == ioa_cfg->sdt_state) {
8234                         ioa_cfg->sdt_state = READ_DUMP;
8235                         ioa_cfg->dump_timeout = 0;
8236                         if (ioa_cfg->sis64)
8237                                 ipr_reset_start_timer(ipr_cmd, IPR_SIS64_DUMP_TIMEOUT);
8238                         else
8239                                 ipr_reset_start_timer(ipr_cmd, IPR_SIS32_DUMP_TIMEOUT);
8240                         ipr_cmd->job_step = ipr_reset_wait_for_dump;
8241                         schedule_work(&ioa_cfg->work_q);
8242                         return IPR_RC_JOB_RETURN;
8243                 }
8244         }
8245
8246         LEAVE;
8247         return IPR_RC_JOB_CONTINUE;
8248 }
8249
8250 /**
8251  * ipr_reset_bist_done - BIST has completed on the adapter.
8252  * @ipr_cmd:    ipr command struct
8253  *
8254  * Description: Unblock config space and resume the reset process.
8255  *
8256  * Return value:
8257  *      IPR_RC_JOB_CONTINUE
8258  **/
8259 static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
8260 {
8261         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8262
8263         ENTER;
8264         if (ioa_cfg->cfg_locked)
8265                 pci_cfg_access_unlock(ioa_cfg->pdev);
8266         ioa_cfg->cfg_locked = 0;
8267         ipr_cmd->job_step = ipr_reset_restore_cfg_space;
8268         LEAVE;
8269         return IPR_RC_JOB_CONTINUE;
8270 }
8271
8272 /**
8273  * ipr_reset_start_bist - Run BIST on the adapter.
8274  * @ipr_cmd:    ipr command struct
8275  *
8276  * Description: This function runs BIST on the adapter, then delays 2 seconds.
8277  *
8278  * Return value:
8279  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8280  **/
8281 static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
8282 {
8283         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8284         int rc = PCIBIOS_SUCCESSFUL;
8285
8286         ENTER;
8287         if (ioa_cfg->ipr_chip->bist_method == IPR_MMIO)
8288                 writel(IPR_UPROCI_SIS64_START_BIST,
8289                        ioa_cfg->regs.set_uproc_interrupt_reg32);
8290         else
8291                 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
8292
8293         if (rc == PCIBIOS_SUCCESSFUL) {
8294                 ipr_cmd->job_step = ipr_reset_bist_done;
8295                 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
8296                 rc = IPR_RC_JOB_RETURN;
8297         } else {
8298                 if (ioa_cfg->cfg_locked)
8299                         pci_cfg_access_unlock(ipr_cmd->ioa_cfg->pdev);
8300                 ioa_cfg->cfg_locked = 0;
8301                 ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
8302                 rc = IPR_RC_JOB_CONTINUE;
8303         }
8304
8305         LEAVE;
8306         return rc;
8307 }
8308
8309 /**
8310  * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
8311  * @ipr_cmd:    ipr command struct
8312  *
8313  * Description: This clears PCI reset to the adapter and delays two seconds.
8314  *
8315  * Return value:
8316  *      IPR_RC_JOB_RETURN
8317  **/
8318 static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
8319 {
8320         ENTER;
8321         pci_set_pcie_reset_state(ipr_cmd->ioa_cfg->pdev, pcie_deassert_reset);
8322         ipr_cmd->job_step = ipr_reset_bist_done;
8323         ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
8324         LEAVE;
8325         return IPR_RC_JOB_RETURN;
8326 }
8327
8328 /**
8329  * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
8330  * @ipr_cmd:    ipr command struct
8331  *
8332  * Description: This asserts PCI reset to the adapter.
8333  *
8334  * Return value:
8335  *      IPR_RC_JOB_RETURN
8336  **/
8337 static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
8338 {
8339         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8340         struct pci_dev *pdev = ioa_cfg->pdev;
8341
8342         ENTER;
8343         pci_set_pcie_reset_state(pdev, pcie_warm_reset);
8344         ipr_cmd->job_step = ipr_reset_slot_reset_done;
8345         ipr_reset_start_timer(ipr_cmd, IPR_PCI_RESET_TIMEOUT);
8346         LEAVE;
8347         return IPR_RC_JOB_RETURN;
8348 }
8349
8350 /**
8351  * ipr_reset_block_config_access_wait - Wait for permission to block config access
8352  * @ipr_cmd:    ipr command struct
8353  *
8354  * Description: This attempts to block config access to the IOA.
8355  *
8356  * Return value:
8357  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8358  **/
8359 static int ipr_reset_block_config_access_wait(struct ipr_cmnd *ipr_cmd)
8360 {
8361         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8362         int rc = IPR_RC_JOB_CONTINUE;
8363
8364         if (pci_cfg_access_trylock(ioa_cfg->pdev)) {
8365                 ioa_cfg->cfg_locked = 1;
8366                 ipr_cmd->job_step = ioa_cfg->reset;
8367         } else {
8368                 if (ipr_cmd->u.time_left) {
8369                         rc = IPR_RC_JOB_RETURN;
8370                         ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
8371                         ipr_reset_start_timer(ipr_cmd,
8372                                               IPR_CHECK_FOR_RESET_TIMEOUT);
8373                 } else {
8374                         ipr_cmd->job_step = ioa_cfg->reset;
8375                         dev_err(&ioa_cfg->pdev->dev,
8376                                 "Timed out waiting to lock config access. Resetting anyway.\n");
8377                 }
8378         }
8379
8380         return rc;
8381 }
8382
8383 /**
8384  * ipr_reset_block_config_access - Block config access to the IOA
8385  * @ipr_cmd:    ipr command struct
8386  *
8387  * Description: This attempts to block config access to the IOA
8388  *
8389  * Return value:
8390  *      IPR_RC_JOB_CONTINUE
8391  **/
8392 static int ipr_reset_block_config_access(struct ipr_cmnd *ipr_cmd)
8393 {
8394         ipr_cmd->ioa_cfg->cfg_locked = 0;
8395         ipr_cmd->job_step = ipr_reset_block_config_access_wait;
8396         ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
8397         return IPR_RC_JOB_CONTINUE;
8398 }
8399
8400 /**
8401  * ipr_reset_allowed - Query whether or not IOA can be reset
8402  * @ioa_cfg:    ioa config struct
8403  *
8404  * Return value:
8405  *      0 if reset not allowed / non-zero if reset is allowed
8406  **/
8407 static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
8408 {
8409         volatile u32 temp_reg;
8410
8411         temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
8412         return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
8413 }
8414
8415 /**
8416  * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
8417  * @ipr_cmd:    ipr command struct
8418  *
8419  * Description: This function waits for adapter permission to run BIST,
8420  * then runs BIST. If the adapter does not give permission after a
8421  * reasonable time, we will reset the adapter anyway. The impact of
8422  * resetting the adapter without warning the adapter is the risk of
8423  * losing the persistent error log on the adapter. If the adapter is
8424  * reset while it is writing to the flash on the adapter, the flash
8425  * segment will have bad ECC and be zeroed.
8426  *
8427  * Return value:
8428  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8429  **/
8430 static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
8431 {
8432         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8433         int rc = IPR_RC_JOB_RETURN;
8434
8435         if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
8436                 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
8437                 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
8438         } else {
8439                 ipr_cmd->job_step = ipr_reset_block_config_access;
8440                 rc = IPR_RC_JOB_CONTINUE;
8441         }
8442
8443         return rc;
8444 }
8445
8446 /**
8447  * ipr_reset_alert - Alert the adapter of a pending reset
8448  * @ipr_cmd:    ipr command struct
8449  *
8450  * Description: This function alerts the adapter that it will be reset.
8451  * If memory space is not currently enabled, proceed directly
8452  * to running BIST on the adapter. The timer must always be started
8453  * so we guarantee we do not run BIST from ipr_isr.
8454  *
8455  * Return value:
8456  *      IPR_RC_JOB_RETURN
8457  **/
8458 static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
8459 {
8460         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8461         u16 cmd_reg;
8462         int rc;
8463
8464         ENTER;
8465         rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
8466
8467         if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
8468                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
8469                 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg32);
8470                 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
8471         } else {
8472                 ipr_cmd->job_step = ipr_reset_block_config_access;
8473         }
8474
8475         ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
8476         ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
8477
8478         LEAVE;
8479         return IPR_RC_JOB_RETURN;
8480 }
8481
8482 /**
8483  * ipr_reset_ucode_download_done - Microcode download completion
8484  * @ipr_cmd:    ipr command struct
8485  *
8486  * Description: This function unmaps the microcode download buffer.
8487  *
8488  * Return value:
8489  *      IPR_RC_JOB_CONTINUE
8490  **/
8491 static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
8492 {
8493         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8494         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
8495
8496         dma_unmap_sg(&ioa_cfg->pdev->dev, sglist->scatterlist,
8497                      sglist->num_sg, DMA_TO_DEVICE);
8498
8499         ipr_cmd->job_step = ipr_reset_alert;
8500         return IPR_RC_JOB_CONTINUE;
8501 }
8502
8503 /**
8504  * ipr_reset_ucode_download - Download microcode to the adapter
8505  * @ipr_cmd:    ipr command struct
8506  *
8507  * Description: This function checks to see if it there is microcode
8508  * to download to the adapter. If there is, a download is performed.
8509  *
8510  * Return value:
8511  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8512  **/
8513 static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
8514 {
8515         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8516         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
8517
8518         ENTER;
8519         ipr_cmd->job_step = ipr_reset_alert;
8520
8521         if (!sglist)
8522                 return IPR_RC_JOB_CONTINUE;
8523
8524         ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
8525         ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
8526         ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
8527         ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
8528         ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
8529         ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
8530         ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
8531
8532         if (ioa_cfg->sis64)
8533                 ipr_build_ucode_ioadl64(ipr_cmd, sglist);
8534         else
8535                 ipr_build_ucode_ioadl(ipr_cmd, sglist);
8536         ipr_cmd->job_step = ipr_reset_ucode_download_done;
8537
8538         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
8539                    IPR_WRITE_BUFFER_TIMEOUT);
8540
8541         LEAVE;
8542         return IPR_RC_JOB_RETURN;
8543 }
8544
8545 /**
8546  * ipr_reset_shutdown_ioa - Shutdown the adapter
8547  * @ipr_cmd:    ipr command struct
8548  *
8549  * Description: This function issues an adapter shutdown of the
8550  * specified type to the specified adapter as part of the
8551  * adapter reset job.
8552  *
8553  * Return value:
8554  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
8555  **/
8556 static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
8557 {
8558         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8559         enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
8560         unsigned long timeout;
8561         int rc = IPR_RC_JOB_CONTINUE;
8562
8563         ENTER;
8564         if (shutdown_type != IPR_SHUTDOWN_NONE &&
8565                         !ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
8566                 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
8567                 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
8568                 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
8569                 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
8570
8571                 if (shutdown_type == IPR_SHUTDOWN_NORMAL)
8572                         timeout = IPR_SHUTDOWN_TIMEOUT;
8573                 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
8574                         timeout = IPR_INTERNAL_TIMEOUT;
8575                 else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
8576                         timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
8577                 else
8578                         timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
8579
8580                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
8581
8582                 rc = IPR_RC_JOB_RETURN;
8583                 ipr_cmd->job_step = ipr_reset_ucode_download;
8584         } else
8585                 ipr_cmd->job_step = ipr_reset_alert;
8586
8587         LEAVE;
8588         return rc;
8589 }
8590
8591 /**
8592  * ipr_reset_ioa_job - Adapter reset job
8593  * @ipr_cmd:    ipr command struct
8594  *
8595  * Description: This function is the job router for the adapter reset job.
8596  *
8597  * Return value:
8598  *      none
8599  **/
8600 static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
8601 {
8602         u32 rc, ioasc;
8603         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8604
8605         do {
8606                 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
8607
8608                 if (ioa_cfg->reset_cmd != ipr_cmd) {
8609                         /*
8610                          * We are doing nested adapter resets and this is
8611                          * not the current reset job.
8612                          */
8613                         list_add_tail(&ipr_cmd->queue,
8614                                         &ipr_cmd->hrrq->hrrq_free_q);
8615                         return;
8616                 }
8617
8618                 if (IPR_IOASC_SENSE_KEY(ioasc)) {
8619                         rc = ipr_cmd->job_step_failed(ipr_cmd);
8620                         if (rc == IPR_RC_JOB_RETURN)
8621                                 return;
8622                 }
8623
8624                 ipr_reinit_ipr_cmnd(ipr_cmd);
8625                 ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
8626                 rc = ipr_cmd->job_step(ipr_cmd);
8627         } while (rc == IPR_RC_JOB_CONTINUE);
8628 }
8629
8630 /**
8631  * _ipr_initiate_ioa_reset - Initiate an adapter reset
8632  * @ioa_cfg:            ioa config struct
8633  * @job_step:           first job step of reset job
8634  * @shutdown_type:      shutdown type
8635  *
8636  * Description: This function will initiate the reset of the given adapter
8637  * starting at the selected job step.
8638  * If the caller needs to wait on the completion of the reset,
8639  * the caller must sleep on the reset_wait_q.
8640  *
8641  * Return value:
8642  *      none
8643  **/
8644 static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
8645                                     int (*job_step) (struct ipr_cmnd *),
8646                                     enum ipr_shutdown_type shutdown_type)
8647 {
8648         struct ipr_cmnd *ipr_cmd;
8649         int i;
8650
8651         ioa_cfg->in_reset_reload = 1;
8652         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8653                 spin_lock(&ioa_cfg->hrrq[i]._lock);
8654                 ioa_cfg->hrrq[i].allow_cmds = 0;
8655                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
8656         }
8657         wmb();
8658         if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa)
8659                 scsi_block_requests(ioa_cfg->host);
8660
8661         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
8662         ioa_cfg->reset_cmd = ipr_cmd;
8663         ipr_cmd->job_step = job_step;
8664         ipr_cmd->u.shutdown_type = shutdown_type;
8665
8666         ipr_reset_ioa_job(ipr_cmd);
8667 }
8668
8669 /**
8670  * ipr_initiate_ioa_reset - Initiate an adapter reset
8671  * @ioa_cfg:            ioa config struct
8672  * @shutdown_type:      shutdown type
8673  *
8674  * Description: This function will initiate the reset of the given adapter.
8675  * If the caller needs to wait on the completion of the reset,
8676  * the caller must sleep on the reset_wait_q.
8677  *
8678  * Return value:
8679  *      none
8680  **/
8681 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
8682                                    enum ipr_shutdown_type shutdown_type)
8683 {
8684         int i;
8685
8686         if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
8687                 return;
8688
8689         if (ioa_cfg->in_reset_reload) {
8690                 if (ioa_cfg->sdt_state == GET_DUMP)
8691                         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
8692                 else if (ioa_cfg->sdt_state == READ_DUMP)
8693                         ioa_cfg->sdt_state = ABORT_DUMP;
8694         }
8695
8696         if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
8697                 dev_err(&ioa_cfg->pdev->dev,
8698                         "IOA taken offline - error recovery failed\n");
8699
8700                 ioa_cfg->reset_retries = 0;
8701                 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8702                         spin_lock(&ioa_cfg->hrrq[i]._lock);
8703                         ioa_cfg->hrrq[i].ioa_is_dead = 1;
8704                         spin_unlock(&ioa_cfg->hrrq[i]._lock);
8705                 }
8706                 wmb();
8707
8708                 if (ioa_cfg->in_ioa_bringdown) {
8709                         ioa_cfg->reset_cmd = NULL;
8710                         ioa_cfg->in_reset_reload = 0;
8711                         ipr_fail_all_ops(ioa_cfg);
8712                         wake_up_all(&ioa_cfg->reset_wait_q);
8713
8714                         if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa) {
8715                                 spin_unlock_irq(ioa_cfg->host->host_lock);
8716                                 scsi_unblock_requests(ioa_cfg->host);
8717                                 spin_lock_irq(ioa_cfg->host->host_lock);
8718                         }
8719                         return;
8720                 } else {
8721                         ioa_cfg->in_ioa_bringdown = 1;
8722                         shutdown_type = IPR_SHUTDOWN_NONE;
8723                 }
8724         }
8725
8726         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
8727                                 shutdown_type);
8728 }
8729
8730 /**
8731  * ipr_reset_freeze - Hold off all I/O activity
8732  * @ipr_cmd:    ipr command struct
8733  *
8734  * Description: If the PCI slot is frozen, hold off all I/O
8735  * activity; then, as soon as the slot is available again,
8736  * initiate an adapter reset.
8737  */
8738 static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
8739 {
8740         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8741         int i;
8742
8743         /* Disallow new interrupts, avoid loop */
8744         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8745                 spin_lock(&ioa_cfg->hrrq[i]._lock);
8746                 ioa_cfg->hrrq[i].allow_interrupts = 0;
8747                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
8748         }
8749         wmb();
8750         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
8751         ipr_cmd->done = ipr_reset_ioa_job;
8752         return IPR_RC_JOB_RETURN;
8753 }
8754
8755 /**
8756  * ipr_pci_mmio_enabled - Called when MMIO has been re-enabled
8757  * @pdev:       PCI device struct
8758  *
8759  * Description: This routine is called to tell us that the MMIO
8760  * access to the IOA has been restored
8761  */
8762 static pci_ers_result_t ipr_pci_mmio_enabled(struct pci_dev *pdev)
8763 {
8764         unsigned long flags = 0;
8765         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8766
8767         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8768         if (!ioa_cfg->probe_done)
8769                 pci_save_state(pdev);
8770         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8771         return PCI_ERS_RESULT_NEED_RESET;
8772 }
8773
8774 /**
8775  * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
8776  * @pdev:       PCI device struct
8777  *
8778  * Description: This routine is called to tell us that the PCI bus
8779  * is down. Can't do anything here, except put the device driver
8780  * into a holding pattern, waiting for the PCI bus to come back.
8781  */
8782 static void ipr_pci_frozen(struct pci_dev *pdev)
8783 {
8784         unsigned long flags = 0;
8785         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8786
8787         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8788         if (ioa_cfg->probe_done)
8789                 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
8790         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8791 }
8792
8793 /**
8794  * ipr_pci_slot_reset - Called when PCI slot has been reset.
8795  * @pdev:       PCI device struct
8796  *
8797  * Description: This routine is called by the pci error recovery
8798  * code after the PCI slot has been reset, just before we
8799  * should resume normal operations.
8800  */
8801 static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
8802 {
8803         unsigned long flags = 0;
8804         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8805
8806         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8807         if (ioa_cfg->probe_done) {
8808                 if (ioa_cfg->needs_warm_reset)
8809                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8810                 else
8811                         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
8812                                                 IPR_SHUTDOWN_NONE);
8813         } else
8814                 wake_up_all(&ioa_cfg->eeh_wait_q);
8815         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8816         return PCI_ERS_RESULT_RECOVERED;
8817 }
8818
8819 /**
8820  * ipr_pci_perm_failure - Called when PCI slot is dead for good.
8821  * @pdev:       PCI device struct
8822  *
8823  * Description: This routine is called when the PCI bus has
8824  * permanently failed.
8825  */
8826 static void ipr_pci_perm_failure(struct pci_dev *pdev)
8827 {
8828         unsigned long flags = 0;
8829         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8830         int i;
8831
8832         spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8833         if (ioa_cfg->probe_done) {
8834                 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
8835                         ioa_cfg->sdt_state = ABORT_DUMP;
8836                 ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES - 1;
8837                 ioa_cfg->in_ioa_bringdown = 1;
8838                 for (i = 0; i < ioa_cfg->hrrq_num; i++) {
8839                         spin_lock(&ioa_cfg->hrrq[i]._lock);
8840                         ioa_cfg->hrrq[i].allow_cmds = 0;
8841                         spin_unlock(&ioa_cfg->hrrq[i]._lock);
8842                 }
8843                 wmb();
8844                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8845         } else
8846                 wake_up_all(&ioa_cfg->eeh_wait_q);
8847         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8848 }
8849
8850 /**
8851  * ipr_pci_error_detected - Called when a PCI error is detected.
8852  * @pdev:       PCI device struct
8853  * @state:      PCI channel state
8854  *
8855  * Description: Called when a PCI error is detected.
8856  *
8857  * Return value:
8858  *      PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8859  */
8860 static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
8861                                                pci_channel_state_t state)
8862 {
8863         switch (state) {
8864         case pci_channel_io_frozen:
8865                 ipr_pci_frozen(pdev);
8866                 return PCI_ERS_RESULT_CAN_RECOVER;
8867         case pci_channel_io_perm_failure:
8868                 ipr_pci_perm_failure(pdev);
8869                 return PCI_ERS_RESULT_DISCONNECT;
8870                 break;
8871         default:
8872                 break;
8873         }
8874         return PCI_ERS_RESULT_NEED_RESET;
8875 }
8876
8877 /**
8878  * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
8879  * @ioa_cfg:    ioa cfg struct
8880  *
8881  * Description: This is the second phase of adapter intialization
8882  * This function takes care of initilizing the adapter to the point
8883  * where it can accept new commands.
8884
8885  * Return value:
8886  *      0 on success / -EIO on failure
8887  **/
8888 static int ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
8889 {
8890         int rc = 0;
8891         unsigned long host_lock_flags = 0;
8892
8893         ENTER;
8894         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8895         dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
8896         ioa_cfg->probe_done = 1;
8897         if (ioa_cfg->needs_hard_reset) {
8898                 ioa_cfg->needs_hard_reset = 0;
8899                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8900         } else
8901                 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
8902                                         IPR_SHUTDOWN_NONE);
8903         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8904
8905         LEAVE;
8906         return rc;
8907 }
8908
8909 /**
8910  * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
8911  * @ioa_cfg:    ioa config struct
8912  *
8913  * Return value:
8914  *      none
8915  **/
8916 static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
8917 {
8918         int i;
8919
8920         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
8921                 if (ioa_cfg->ipr_cmnd_list[i])
8922                         dma_pool_free(ioa_cfg->ipr_cmd_pool,
8923                                       ioa_cfg->ipr_cmnd_list[i],
8924                                       ioa_cfg->ipr_cmnd_list_dma[i]);
8925
8926                 ioa_cfg->ipr_cmnd_list[i] = NULL;
8927         }
8928
8929         if (ioa_cfg->ipr_cmd_pool)
8930                 dma_pool_destroy(ioa_cfg->ipr_cmd_pool);
8931
8932         kfree(ioa_cfg->ipr_cmnd_list);
8933         kfree(ioa_cfg->ipr_cmnd_list_dma);
8934         ioa_cfg->ipr_cmnd_list = NULL;
8935         ioa_cfg->ipr_cmnd_list_dma = NULL;
8936         ioa_cfg->ipr_cmd_pool = NULL;
8937 }
8938
8939 /**
8940  * ipr_free_mem - Frees memory allocated for an adapter
8941  * @ioa_cfg:    ioa cfg struct
8942  *
8943  * Return value:
8944  *      nothing
8945  **/
8946 static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
8947 {
8948         int i;
8949
8950         kfree(ioa_cfg->res_entries);
8951         dma_free_coherent(&ioa_cfg->pdev->dev, sizeof(struct ipr_misc_cbs),
8952                           ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
8953         ipr_free_cmd_blks(ioa_cfg);
8954
8955         for (i = 0; i < ioa_cfg->hrrq_num; i++)
8956                 dma_free_coherent(&ioa_cfg->pdev->dev,
8957                                   sizeof(u32) * ioa_cfg->hrrq[i].size,
8958                                   ioa_cfg->hrrq[i].host_rrq,
8959                                   ioa_cfg->hrrq[i].host_rrq_dma);
8960
8961         dma_free_coherent(&ioa_cfg->pdev->dev, ioa_cfg->cfg_table_size,
8962                           ioa_cfg->u.cfg_table, ioa_cfg->cfg_table_dma);
8963
8964         for (i = 0; i < IPR_NUM_HCAMS; i++) {
8965                 dma_free_coherent(&ioa_cfg->pdev->dev,
8966                                   sizeof(struct ipr_hostrcb),
8967                                   ioa_cfg->hostrcb[i],
8968                                   ioa_cfg->hostrcb_dma[i]);
8969         }
8970
8971         ipr_free_dump(ioa_cfg);
8972         kfree(ioa_cfg->trace);
8973 }
8974
8975 /**
8976  * ipr_free_all_resources - Free all allocated resources for an adapter.
8977  * @ipr_cmd:    ipr command struct
8978  *
8979  * This function frees all allocated resources for the
8980  * specified adapter.
8981  *
8982  * Return value:
8983  *      none
8984  **/
8985 static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
8986 {
8987         struct pci_dev *pdev = ioa_cfg->pdev;
8988
8989         ENTER;
8990         if (ioa_cfg->intr_flag == IPR_USE_MSI ||
8991             ioa_cfg->intr_flag == IPR_USE_MSIX) {
8992                 int i;
8993                 for (i = 0; i < ioa_cfg->nvectors; i++)
8994                         free_irq(ioa_cfg->vectors_info[i].vec,
8995                                 &ioa_cfg->hrrq[i]);
8996         } else
8997                 free_irq(pdev->irq, &ioa_cfg->hrrq[0]);
8998
8999         if (ioa_cfg->intr_flag == IPR_USE_MSI) {
9000                 pci_disable_msi(pdev);
9001                 ioa_cfg->intr_flag &= ~IPR_USE_MSI;
9002         } else if (ioa_cfg->intr_flag == IPR_USE_MSIX) {
9003                 pci_disable_msix(pdev);
9004                 ioa_cfg->intr_flag &= ~IPR_USE_MSIX;
9005         }
9006
9007         iounmap(ioa_cfg->hdw_dma_regs);
9008         pci_release_regions(pdev);
9009         ipr_free_mem(ioa_cfg);
9010         scsi_host_put(ioa_cfg->host);
9011         pci_disable_device(pdev);
9012         LEAVE;
9013 }
9014
9015 /**
9016  * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
9017  * @ioa_cfg:    ioa config struct
9018  *
9019  * Return value:
9020  *      0 on success / -ENOMEM on allocation failure
9021  **/
9022 static int ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
9023 {
9024         struct ipr_cmnd *ipr_cmd;
9025         struct ipr_ioarcb *ioarcb;
9026         dma_addr_t dma_addr;
9027         int i, entries_each_hrrq, hrrq_id = 0;
9028
9029         ioa_cfg->ipr_cmd_pool = dma_pool_create(IPR_NAME, &ioa_cfg->pdev->dev,
9030                                                 sizeof(struct ipr_cmnd), 512, 0);
9031
9032         if (!ioa_cfg->ipr_cmd_pool)
9033                 return -ENOMEM;
9034
9035         ioa_cfg->ipr_cmnd_list = kcalloc(IPR_NUM_CMD_BLKS, sizeof(struct ipr_cmnd *), GFP_KERNEL);
9036         ioa_cfg->ipr_cmnd_list_dma = kcalloc(IPR_NUM_CMD_BLKS, sizeof(dma_addr_t), GFP_KERNEL);
9037
9038         if (!ioa_cfg->ipr_cmnd_list || !ioa_cfg->ipr_cmnd_list_dma) {
9039                 ipr_free_cmd_blks(ioa_cfg);
9040                 return -ENOMEM;
9041         }
9042
9043         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
9044                 if (ioa_cfg->hrrq_num > 1) {
9045                         if (i == 0) {
9046                                 entries_each_hrrq = IPR_NUM_INTERNAL_CMD_BLKS;
9047                                 ioa_cfg->hrrq[i].min_cmd_id = 0;
9048                                         ioa_cfg->hrrq[i].max_cmd_id =
9049                                                 (entries_each_hrrq - 1);
9050                         } else {
9051                                 entries_each_hrrq =
9052                                         IPR_NUM_BASE_CMD_BLKS/
9053                                         (ioa_cfg->hrrq_num - 1);
9054                                 ioa_cfg->hrrq[i].min_cmd_id =
9055                                         IPR_NUM_INTERNAL_CMD_BLKS +
9056                                         (i - 1) * entries_each_hrrq;
9057                                 ioa_cfg->hrrq[i].max_cmd_id =
9058                                         (IPR_NUM_INTERNAL_CMD_BLKS +
9059                                         i * entries_each_hrrq - 1);
9060                         }
9061                 } else {
9062                         entries_each_hrrq = IPR_NUM_CMD_BLKS;
9063                         ioa_cfg->hrrq[i].min_cmd_id = 0;
9064                         ioa_cfg->hrrq[i].max_cmd_id = (entries_each_hrrq - 1);
9065                 }
9066                 ioa_cfg->hrrq[i].size = entries_each_hrrq;
9067         }
9068
9069         BUG_ON(ioa_cfg->hrrq_num == 0);
9070
9071         i = IPR_NUM_CMD_BLKS -
9072                 ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].max_cmd_id - 1;
9073         if (i > 0) {
9074                 ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].size += i;
9075                 ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].max_cmd_id += i;
9076         }
9077
9078         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
9079                 ipr_cmd = dma_pool_alloc(ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
9080
9081                 if (!ipr_cmd) {
9082                         ipr_free_cmd_blks(ioa_cfg);
9083                         return -ENOMEM;
9084                 }
9085
9086                 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
9087                 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
9088                 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
9089
9090                 ioarcb = &ipr_cmd->ioarcb;
9091                 ipr_cmd->dma_addr = dma_addr;
9092                 if (ioa_cfg->sis64)
9093                         ioarcb->a.ioarcb_host_pci_addr64 = cpu_to_be64(dma_addr);
9094                 else
9095                         ioarcb->a.ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
9096
9097                 ioarcb->host_response_handle = cpu_to_be32(i << 2);
9098                 if (ioa_cfg->sis64) {
9099                         ioarcb->u.sis64_addr_data.data_ioadl_addr =
9100                                 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
9101                         ioarcb->u.sis64_addr_data.ioasa_host_pci_addr =
9102                                 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, s.ioasa64));
9103                 } else {
9104                         ioarcb->write_ioadl_addr =
9105                                 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
9106                         ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
9107                         ioarcb->ioasa_host_pci_addr =
9108                                 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, s.ioasa));
9109                 }
9110                 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
9111                 ipr_cmd->cmd_index = i;
9112                 ipr_cmd->ioa_cfg = ioa_cfg;
9113                 ipr_cmd->sense_buffer_dma = dma_addr +
9114                         offsetof(struct ipr_cmnd, sense_buffer);
9115
9116                 ipr_cmd->ioarcb.cmd_pkt.hrrq_id = hrrq_id;
9117                 ipr_cmd->hrrq = &ioa_cfg->hrrq[hrrq_id];
9118                 list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
9119                 if (i >= ioa_cfg->hrrq[hrrq_id].max_cmd_id)
9120                         hrrq_id++;
9121         }
9122
9123         return 0;
9124 }
9125
9126 /**
9127  * ipr_alloc_mem - Allocate memory for an adapter
9128  * @ioa_cfg:    ioa config struct
9129  *
9130  * Return value:
9131  *      0 on success / non-zero for error
9132  **/
9133 static int ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
9134 {
9135         struct pci_dev *pdev = ioa_cfg->pdev;
9136         int i, rc = -ENOMEM;
9137
9138         ENTER;
9139         ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
9140                                        ioa_cfg->max_devs_supported, GFP_KERNEL);
9141
9142         if (!ioa_cfg->res_entries)
9143                 goto out;
9144
9145         for (i = 0; i < ioa_cfg->max_devs_supported; i++) {
9146                 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
9147                 ioa_cfg->res_entries[i].ioa_cfg = ioa_cfg;
9148         }
9149
9150         ioa_cfg->vpd_cbs = dma_alloc_coherent(&pdev->dev,
9151                                               sizeof(struct ipr_misc_cbs),
9152                                               &ioa_cfg->vpd_cbs_dma,
9153                                               GFP_KERNEL);
9154
9155         if (!ioa_cfg->vpd_cbs)
9156                 goto out_free_res_entries;
9157
9158         if (ipr_alloc_cmd_blks(ioa_cfg))
9159                 goto out_free_vpd_cbs;
9160
9161         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
9162                 ioa_cfg->hrrq[i].host_rrq = dma_alloc_coherent(&pdev->dev,
9163                                         sizeof(u32) * ioa_cfg->hrrq[i].size,
9164                                         &ioa_cfg->hrrq[i].host_rrq_dma,
9165                                         GFP_KERNEL);
9166
9167                 if (!ioa_cfg->hrrq[i].host_rrq)  {
9168                         while (--i > 0)
9169                                 dma_free_coherent(&pdev->dev,
9170                                         sizeof(u32) * ioa_cfg->hrrq[i].size,
9171                                         ioa_cfg->hrrq[i].host_rrq,
9172                                         ioa_cfg->hrrq[i].host_rrq_dma);
9173                         goto out_ipr_free_cmd_blocks;
9174                 }
9175                 ioa_cfg->hrrq[i].ioa_cfg = ioa_cfg;
9176         }
9177
9178         ioa_cfg->u.cfg_table = dma_alloc_coherent(&pdev->dev,
9179                                                   ioa_cfg->cfg_table_size,
9180                                                   &ioa_cfg->cfg_table_dma,
9181                                                   GFP_KERNEL);
9182
9183         if (!ioa_cfg->u.cfg_table)
9184                 goto out_free_host_rrq;
9185
9186         for (i = 0; i < IPR_NUM_HCAMS; i++) {
9187                 ioa_cfg->hostrcb[i] = dma_alloc_coherent(&pdev->dev,
9188                                                          sizeof(struct ipr_hostrcb),
9189                                                          &ioa_cfg->hostrcb_dma[i],
9190                                                          GFP_KERNEL);
9191
9192                 if (!ioa_cfg->hostrcb[i])
9193                         goto out_free_hostrcb_dma;
9194
9195                 ioa_cfg->hostrcb[i]->hostrcb_dma =
9196                         ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
9197                 ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
9198                 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
9199         }
9200
9201         ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
9202                                  IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
9203
9204         if (!ioa_cfg->trace)
9205                 goto out_free_hostrcb_dma;
9206
9207         rc = 0;
9208 out:
9209         LEAVE;
9210         return rc;
9211
9212 out_free_hostrcb_dma:
9213         while (i-- > 0) {
9214                 dma_free_coherent(&pdev->dev, sizeof(struct ipr_hostrcb),
9215                                   ioa_cfg->hostrcb[i],
9216                                   ioa_cfg->hostrcb_dma[i]);
9217         }
9218         dma_free_coherent(&pdev->dev, ioa_cfg->cfg_table_size,
9219                           ioa_cfg->u.cfg_table, ioa_cfg->cfg_table_dma);
9220 out_free_host_rrq:
9221         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
9222                 dma_free_coherent(&pdev->dev,
9223                                   sizeof(u32) * ioa_cfg->hrrq[i].size,
9224                                   ioa_cfg->hrrq[i].host_rrq,
9225                                   ioa_cfg->hrrq[i].host_rrq_dma);
9226         }
9227 out_ipr_free_cmd_blocks:
9228         ipr_free_cmd_blks(ioa_cfg);
9229 out_free_vpd_cbs:
9230         dma_free_coherent(&pdev->dev, sizeof(struct ipr_misc_cbs),
9231                           ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
9232 out_free_res_entries:
9233         kfree(ioa_cfg->res_entries);
9234         goto out;
9235 }
9236
9237 /**
9238  * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
9239  * @ioa_cfg:    ioa config struct
9240  *
9241  * Return value:
9242  *      none
9243  **/
9244 static void ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
9245 {
9246         int i;
9247
9248         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
9249                 ioa_cfg->bus_attr[i].bus = i;
9250                 ioa_cfg->bus_attr[i].qas_enabled = 0;
9251                 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
9252                 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
9253                         ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
9254                 else
9255                         ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
9256         }
9257 }
9258
9259 /**
9260  * ipr_init_regs - Initialize IOA registers
9261  * @ioa_cfg:    ioa config struct
9262  *
9263  * Return value:
9264  *      none
9265  **/
9266 static void ipr_init_regs(struct ipr_ioa_cfg *ioa_cfg)
9267 {
9268         const struct ipr_interrupt_offsets *p;
9269         struct ipr_interrupts *t;
9270         void __iomem *base;
9271
9272         p = &ioa_cfg->chip_cfg->regs;
9273         t = &ioa_cfg->regs;
9274         base = ioa_cfg->hdw_dma_regs;
9275
9276         t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
9277         t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
9278         t->clr_interrupt_mask_reg32 = base + p->clr_interrupt_mask_reg32;
9279         t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
9280         t->sense_interrupt_mask_reg32 = base + p->sense_interrupt_mask_reg32;
9281         t->clr_interrupt_reg = base + p->clr_interrupt_reg;
9282         t->clr_interrupt_reg32 = base + p->clr_interrupt_reg32;
9283         t->sense_interrupt_reg = base + p->sense_interrupt_reg;
9284         t->sense_interrupt_reg32 = base + p->sense_interrupt_reg32;
9285         t->ioarrin_reg = base + p->ioarrin_reg;
9286         t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
9287         t->sense_uproc_interrupt_reg32 = base + p->sense_uproc_interrupt_reg32;
9288         t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
9289         t->set_uproc_interrupt_reg32 = base + p->set_uproc_interrupt_reg32;
9290         t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
9291         t->clr_uproc_interrupt_reg32 = base + p->clr_uproc_interrupt_reg32;
9292
9293         if (ioa_cfg->sis64) {
9294                 t->init_feedback_reg = base + p->init_feedback_reg;
9295                 t->dump_addr_reg = base + p->dump_addr_reg;
9296                 t->dump_data_reg = base + p->dump_data_reg;
9297                 t->endian_swap_reg = base + p->endian_swap_reg;
9298         }
9299 }
9300
9301 /**
9302  * ipr_init_ioa_cfg - Initialize IOA config struct
9303  * @ioa_cfg:    ioa config struct
9304  * @host:               scsi host struct
9305  * @pdev:               PCI dev struct
9306  *
9307  * Return value:
9308  *      none
9309  **/
9310 static void ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
9311                              struct Scsi_Host *host, struct pci_dev *pdev)
9312 {
9313         int i;
9314
9315         ioa_cfg->host = host;
9316         ioa_cfg->pdev = pdev;
9317         ioa_cfg->log_level = ipr_log_level;
9318         ioa_cfg->doorbell = IPR_DOORBELL;
9319         sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
9320         sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
9321         sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
9322         sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
9323         sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
9324         sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
9325
9326         INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
9327         INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
9328         INIT_LIST_HEAD(&ioa_cfg->free_res_q);
9329         INIT_LIST_HEAD(&ioa_cfg->used_res_q);
9330         INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
9331         init_waitqueue_head(&ioa_cfg->reset_wait_q);
9332         init_waitqueue_head(&ioa_cfg->msi_wait_q);
9333         init_waitqueue_head(&ioa_cfg->eeh_wait_q);
9334         ioa_cfg->sdt_state = INACTIVE;
9335
9336         ipr_initialize_bus_attr(ioa_cfg);
9337         ioa_cfg->max_devs_supported = ipr_max_devs;
9338
9339         if (ioa_cfg->sis64) {
9340                 host->max_id = IPR_MAX_SIS64_TARGETS_PER_BUS;
9341                 host->max_lun = IPR_MAX_SIS64_LUNS_PER_TARGET;
9342                 if (ipr_max_devs > IPR_MAX_SIS64_DEVS)
9343                         ioa_cfg->max_devs_supported = IPR_MAX_SIS64_DEVS;
9344                 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr64)
9345                                            + ((sizeof(struct ipr_config_table_entry64)
9346                                                * ioa_cfg->max_devs_supported)));
9347         } else {
9348                 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
9349                 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
9350                 if (ipr_max_devs > IPR_MAX_PHYSICAL_DEVS)
9351                         ioa_cfg->max_devs_supported = IPR_MAX_PHYSICAL_DEVS;
9352                 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr)
9353                                            + ((sizeof(struct ipr_config_table_entry)
9354                                                * ioa_cfg->max_devs_supported)));
9355         }
9356
9357         host->max_channel = IPR_VSET_BUS;
9358         host->unique_id = host->host_no;
9359         host->max_cmd_len = IPR_MAX_CDB_LEN;
9360         host->can_queue = ioa_cfg->max_cmds;
9361         pci_set_drvdata(pdev, ioa_cfg);
9362
9363         for (i = 0; i < ARRAY_SIZE(ioa_cfg->hrrq); i++) {
9364                 INIT_LIST_HEAD(&ioa_cfg->hrrq[i].hrrq_free_q);
9365                 INIT_LIST_HEAD(&ioa_cfg->hrrq[i].hrrq_pending_q);
9366                 spin_lock_init(&ioa_cfg->hrrq[i]._lock);
9367                 if (i == 0)
9368                         ioa_cfg->hrrq[i].lock = ioa_cfg->host->host_lock;
9369                 else
9370                         ioa_cfg->hrrq[i].lock = &ioa_cfg->hrrq[i]._lock;
9371         }
9372 }
9373
9374 /**
9375  * ipr_get_chip_info - Find adapter chip information
9376  * @dev_id:             PCI device id struct
9377  *
9378  * Return value:
9379  *      ptr to chip information on success / NULL on failure
9380  **/
9381 static const struct ipr_chip_t *
9382 ipr_get_chip_info(const struct pci_device_id *dev_id)
9383 {
9384         int i;
9385
9386         for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
9387                 if (ipr_chip[i].vendor == dev_id->vendor &&
9388                     ipr_chip[i].device == dev_id->device)
9389                         return &ipr_chip[i];
9390         return NULL;
9391 }
9392
9393 /**
9394  * ipr_wait_for_pci_err_recovery - Wait for any PCI error recovery to complete
9395  *                                              during probe time
9396  * @ioa_cfg:    ioa config struct
9397  *
9398  * Return value:
9399  *      None
9400  **/
9401 static void ipr_wait_for_pci_err_recovery(struct ipr_ioa_cfg *ioa_cfg)
9402 {
9403         struct pci_dev *pdev = ioa_cfg->pdev;
9404
9405         if (pci_channel_offline(pdev)) {
9406                 wait_event_timeout(ioa_cfg->eeh_wait_q,
9407                                    !pci_channel_offline(pdev),
9408                                    IPR_PCI_ERROR_RECOVERY_TIMEOUT);
9409                 pci_restore_state(pdev);
9410         }
9411 }
9412
9413 static int ipr_enable_msix(struct ipr_ioa_cfg *ioa_cfg)
9414 {
9415         struct msix_entry entries[IPR_MAX_MSIX_VECTORS];
9416         int i, vectors;
9417
9418         for (i = 0; i < ARRAY_SIZE(entries); ++i)
9419                 entries[i].entry = i;
9420
9421         vectors = pci_enable_msix_range(ioa_cfg->pdev,
9422                                         entries, 1, ipr_number_of_msix);
9423         if (vectors < 0) {
9424                 ipr_wait_for_pci_err_recovery(ioa_cfg);
9425                 return vectors;
9426         }
9427
9428         for (i = 0; i < vectors; i++)
9429                 ioa_cfg->vectors_info[i].vec = entries[i].vector;
9430         ioa_cfg->nvectors = vectors;
9431
9432         return 0;
9433 }
9434
9435 static int ipr_enable_msi(struct ipr_ioa_cfg *ioa_cfg)
9436 {
9437         int i, vectors;
9438
9439         vectors = pci_enable_msi_range(ioa_cfg->pdev, 1, ipr_number_of_msix);
9440         if (vectors < 0) {
9441                 ipr_wait_for_pci_err_recovery(ioa_cfg);
9442                 return vectors;
9443         }
9444
9445         for (i = 0; i < vectors; i++)
9446                 ioa_cfg->vectors_info[i].vec = ioa_cfg->pdev->irq + i;
9447         ioa_cfg->nvectors = vectors;
9448
9449         return 0;
9450 }
9451
9452 static void name_msi_vectors(struct ipr_ioa_cfg *ioa_cfg)
9453 {
9454         int vec_idx, n = sizeof(ioa_cfg->vectors_info[0].desc) - 1;
9455
9456         for (vec_idx = 0; vec_idx < ioa_cfg->nvectors; vec_idx++) {
9457                 snprintf(ioa_cfg->vectors_info[vec_idx].desc, n,
9458                          "host%d-%d", ioa_cfg->host->host_no, vec_idx);
9459                 ioa_cfg->vectors_info[vec_idx].
9460                         desc[strlen(ioa_cfg->vectors_info[vec_idx].desc)] = 0;
9461         }
9462 }
9463
9464 static int ipr_request_other_msi_irqs(struct ipr_ioa_cfg *ioa_cfg)
9465 {
9466         int i, rc;
9467
9468         for (i = 1; i < ioa_cfg->nvectors; i++) {
9469                 rc = request_irq(ioa_cfg->vectors_info[i].vec,
9470                         ipr_isr_mhrrq,
9471                         0,
9472                         ioa_cfg->vectors_info[i].desc,
9473                         &ioa_cfg->hrrq[i]);
9474                 if (rc) {
9475                         while (--i >= 0)
9476                                 free_irq(ioa_cfg->vectors_info[i].vec,
9477                                         &ioa_cfg->hrrq[i]);
9478                         return rc;
9479                 }
9480         }
9481         return 0;
9482 }
9483
9484 /**
9485  * ipr_test_intr - Handle the interrupt generated in ipr_test_msi().
9486  * @pdev:               PCI device struct
9487  *
9488  * Description: Simply set the msi_received flag to 1 indicating that
9489  * Message Signaled Interrupts are supported.
9490  *
9491  * Return value:
9492  *      0 on success / non-zero on failure
9493  **/
9494 static irqreturn_t ipr_test_intr(int irq, void *devp)
9495 {
9496         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
9497         unsigned long lock_flags = 0;
9498         irqreturn_t rc = IRQ_HANDLED;
9499
9500         dev_info(&ioa_cfg->pdev->dev, "Received IRQ : %d\n", irq);
9501         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9502
9503         ioa_cfg->msi_received = 1;
9504         wake_up(&ioa_cfg->msi_wait_q);
9505
9506         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9507         return rc;
9508 }
9509
9510 /**
9511  * ipr_test_msi - Test for Message Signaled Interrupt (MSI) support.
9512  * @pdev:               PCI device struct
9513  *
9514  * Description: The return value from pci_enable_msi_range() can not always be
9515  * trusted.  This routine sets up and initiates a test interrupt to determine
9516  * if the interrupt is received via the ipr_test_intr() service routine.
9517  * If the tests fails, the driver will fall back to LSI.
9518  *
9519  * Return value:
9520  *      0 on success / non-zero on failure
9521  **/
9522 static int ipr_test_msi(struct ipr_ioa_cfg *ioa_cfg, struct pci_dev *pdev)
9523 {
9524         int rc;
9525         volatile u32 int_reg;
9526         unsigned long lock_flags = 0;
9527
9528         ENTER;
9529
9530         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9531         init_waitqueue_head(&ioa_cfg->msi_wait_q);
9532         ioa_cfg->msi_received = 0;
9533         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
9534         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.clr_interrupt_mask_reg32);
9535         int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
9536         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9537
9538         if (ioa_cfg->intr_flag == IPR_USE_MSIX)
9539                 rc = request_irq(ioa_cfg->vectors_info[0].vec, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
9540         else
9541                 rc = request_irq(pdev->irq, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
9542         if (rc) {
9543                 dev_err(&pdev->dev, "Can not assign irq %d\n", pdev->irq);
9544                 return rc;
9545         } else if (ipr_debug)
9546                 dev_info(&pdev->dev, "IRQ assigned: %d\n", pdev->irq);
9547
9548         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.sense_interrupt_reg32);
9549         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
9550         wait_event_timeout(ioa_cfg->msi_wait_q, ioa_cfg->msi_received, HZ);
9551         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9552         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
9553
9554         if (!ioa_cfg->msi_received) {
9555                 /* MSI test failed */
9556                 dev_info(&pdev->dev, "MSI test failed.  Falling back to LSI.\n");
9557                 rc = -EOPNOTSUPP;
9558         } else if (ipr_debug)
9559                 dev_info(&pdev->dev, "MSI test succeeded.\n");
9560
9561         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9562
9563         if (ioa_cfg->intr_flag == IPR_USE_MSIX)
9564                 free_irq(ioa_cfg->vectors_info[0].vec, ioa_cfg);
9565         else
9566                 free_irq(pdev->irq, ioa_cfg);
9567
9568         LEAVE;
9569
9570         return rc;
9571 }
9572
9573  /* ipr_probe_ioa - Allocates memory and does first stage of initialization
9574  * @pdev:               PCI device struct
9575  * @dev_id:             PCI device id struct
9576  *
9577  * Return value:
9578  *      0 on success / non-zero on failure
9579  **/
9580 static int ipr_probe_ioa(struct pci_dev *pdev,
9581                          const struct pci_device_id *dev_id)
9582 {
9583         struct ipr_ioa_cfg *ioa_cfg;
9584         struct Scsi_Host *host;
9585         unsigned long ipr_regs_pci;
9586         void __iomem *ipr_regs;
9587         int rc = PCIBIOS_SUCCESSFUL;
9588         volatile u32 mask, uproc, interrupts;
9589         unsigned long lock_flags, driver_lock_flags;
9590
9591         ENTER;
9592
9593         dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
9594         host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
9595
9596         if (!host) {
9597                 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
9598                 rc = -ENOMEM;
9599                 goto out;
9600         }
9601
9602         ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
9603         memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
9604         ata_host_init(&ioa_cfg->ata_host, &pdev->dev, &ipr_sata_ops);
9605
9606         ioa_cfg->ipr_chip = ipr_get_chip_info(dev_id);
9607
9608         if (!ioa_cfg->ipr_chip) {
9609                 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
9610                         dev_id->vendor, dev_id->device);
9611                 goto out_scsi_host_put;
9612         }
9613
9614         /* set SIS 32 or SIS 64 */
9615         ioa_cfg->sis64 = ioa_cfg->ipr_chip->sis_type == IPR_SIS64 ? 1 : 0;
9616         ioa_cfg->chip_cfg = ioa_cfg->ipr_chip->cfg;
9617         ioa_cfg->clear_isr = ioa_cfg->chip_cfg->clear_isr;
9618         ioa_cfg->max_cmds = ioa_cfg->chip_cfg->max_cmds;
9619
9620         if (ipr_transop_timeout)
9621                 ioa_cfg->transop_timeout = ipr_transop_timeout;
9622         else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
9623                 ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
9624         else
9625                 ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
9626
9627         ioa_cfg->revid = pdev->revision;
9628
9629         ipr_init_ioa_cfg(ioa_cfg, host, pdev);
9630
9631         ipr_regs_pci = pci_resource_start(pdev, 0);
9632
9633         rc = pci_request_regions(pdev, IPR_NAME);
9634         if (rc < 0) {
9635                 dev_err(&pdev->dev,
9636                         "Couldn't register memory range of registers\n");
9637                 goto out_scsi_host_put;
9638         }
9639
9640         rc = pci_enable_device(pdev);
9641
9642         if (rc || pci_channel_offline(pdev)) {
9643                 if (pci_channel_offline(pdev)) {
9644                         ipr_wait_for_pci_err_recovery(ioa_cfg);
9645                         rc = pci_enable_device(pdev);
9646                 }
9647
9648                 if (rc) {
9649                         dev_err(&pdev->dev, "Cannot enable adapter\n");
9650                         ipr_wait_for_pci_err_recovery(ioa_cfg);
9651                         goto out_release_regions;
9652                 }
9653         }
9654
9655         ipr_regs = pci_ioremap_bar(pdev, 0);
9656
9657         if (!ipr_regs) {
9658                 dev_err(&pdev->dev,
9659                         "Couldn't map memory range of registers\n");
9660                 rc = -ENOMEM;
9661                 goto out_disable;
9662         }
9663
9664         ioa_cfg->hdw_dma_regs = ipr_regs;
9665         ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
9666         ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
9667
9668         ipr_init_regs(ioa_cfg);
9669
9670         if (ioa_cfg->sis64) {
9671                 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
9672                 if (rc < 0) {
9673                         dev_dbg(&pdev->dev, "Failed to set 64 bit DMA mask\n");
9674                         rc = dma_set_mask_and_coherent(&pdev->dev,
9675                                                        DMA_BIT_MASK(32));
9676                 }
9677         } else
9678                 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
9679
9680         if (rc < 0) {
9681                 dev_err(&pdev->dev, "Failed to set DMA mask\n");
9682                 goto cleanup_nomem;
9683         }
9684
9685         rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
9686                                    ioa_cfg->chip_cfg->cache_line_size);
9687
9688         if (rc != PCIBIOS_SUCCESSFUL) {
9689                 dev_err(&pdev->dev, "Write of cache line size failed\n");
9690                 ipr_wait_for_pci_err_recovery(ioa_cfg);
9691                 rc = -EIO;
9692                 goto cleanup_nomem;
9693         }
9694
9695         /* Issue MMIO read to ensure card is not in EEH */
9696         interrupts = readl(ioa_cfg->regs.sense_interrupt_reg);
9697         ipr_wait_for_pci_err_recovery(ioa_cfg);
9698
9699         if (ipr_number_of_msix > IPR_MAX_MSIX_VECTORS) {
9700                 dev_err(&pdev->dev, "The max number of MSIX is %d\n",
9701                         IPR_MAX_MSIX_VECTORS);
9702                 ipr_number_of_msix = IPR_MAX_MSIX_VECTORS;
9703         }
9704
9705         if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI &&
9706                         ipr_enable_msix(ioa_cfg) == 0)
9707                 ioa_cfg->intr_flag = IPR_USE_MSIX;
9708         else if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI &&
9709                         ipr_enable_msi(ioa_cfg) == 0)
9710                 ioa_cfg->intr_flag = IPR_USE_MSI;
9711         else {
9712                 ioa_cfg->intr_flag = IPR_USE_LSI;
9713                 ioa_cfg->nvectors = 1;
9714                 dev_info(&pdev->dev, "Cannot enable MSI.\n");
9715         }
9716
9717         pci_set_master(pdev);
9718
9719         if (pci_channel_offline(pdev)) {
9720                 ipr_wait_for_pci_err_recovery(ioa_cfg);
9721                 pci_set_master(pdev);
9722                 if (pci_channel_offline(pdev)) {
9723                         rc = -EIO;
9724                         goto out_msi_disable;
9725                 }
9726         }
9727
9728         if (ioa_cfg->intr_flag == IPR_USE_MSI ||
9729             ioa_cfg->intr_flag == IPR_USE_MSIX) {
9730                 rc = ipr_test_msi(ioa_cfg, pdev);
9731                 if (rc == -EOPNOTSUPP) {
9732                         ipr_wait_for_pci_err_recovery(ioa_cfg);
9733                         if (ioa_cfg->intr_flag == IPR_USE_MSI) {
9734                                 ioa_cfg->intr_flag &= ~IPR_USE_MSI;
9735                                 pci_disable_msi(pdev);
9736                          } else if (ioa_cfg->intr_flag == IPR_USE_MSIX) {
9737                                 ioa_cfg->intr_flag &= ~IPR_USE_MSIX;
9738                                 pci_disable_msix(pdev);
9739                         }
9740
9741                         ioa_cfg->intr_flag = IPR_USE_LSI;
9742                         ioa_cfg->nvectors = 1;
9743                 }
9744                 else if (rc)
9745                         goto out_msi_disable;
9746                 else {
9747                         if (ioa_cfg->intr_flag == IPR_USE_MSI)
9748                                 dev_info(&pdev->dev,
9749                                         "Request for %d MSIs succeeded with starting IRQ: %d\n",
9750                                         ioa_cfg->nvectors, pdev->irq);
9751                         else if (ioa_cfg->intr_flag == IPR_USE_MSIX)
9752                                 dev_info(&pdev->dev,
9753                                         "Request for %d MSIXs succeeded.",
9754                                         ioa_cfg->nvectors);
9755                 }
9756         }
9757
9758         ioa_cfg->hrrq_num = min3(ioa_cfg->nvectors,
9759                                 (unsigned int)num_online_cpus(),
9760                                 (unsigned int)IPR_MAX_HRRQ_NUM);
9761
9762         if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
9763                 goto out_msi_disable;
9764
9765         if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
9766                 goto out_msi_disable;
9767
9768         rc = ipr_alloc_mem(ioa_cfg);
9769         if (rc < 0) {
9770                 dev_err(&pdev->dev,
9771                         "Couldn't allocate enough memory for device driver!\n");
9772                 goto out_msi_disable;
9773         }
9774
9775         /* Save away PCI config space for use following IOA reset */
9776         rc = pci_save_state(pdev);
9777
9778         if (rc != PCIBIOS_SUCCESSFUL) {
9779                 dev_err(&pdev->dev, "Failed to save PCI config space\n");
9780                 rc = -EIO;
9781                 goto cleanup_nolog;
9782         }
9783
9784         /*
9785          * If HRRQ updated interrupt is not masked, or reset alert is set,
9786          * the card is in an unknown state and needs a hard reset
9787          */
9788         mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
9789         interrupts = readl(ioa_cfg->regs.sense_interrupt_reg32);
9790         uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
9791         if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
9792                 ioa_cfg->needs_hard_reset = 1;
9793         if ((interrupts & IPR_PCII_ERROR_INTERRUPTS) || reset_devices)
9794                 ioa_cfg->needs_hard_reset = 1;
9795         if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
9796                 ioa_cfg->ioa_unit_checked = 1;
9797
9798         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9799         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
9800         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9801
9802         if (ioa_cfg->intr_flag == IPR_USE_MSI
9803                         || ioa_cfg->intr_flag == IPR_USE_MSIX) {
9804                 name_msi_vectors(ioa_cfg);
9805                 rc = request_irq(ioa_cfg->vectors_info[0].vec, ipr_isr,
9806                         0,
9807                         ioa_cfg->vectors_info[0].desc,
9808                         &ioa_cfg->hrrq[0]);
9809                 if (!rc)
9810                         rc = ipr_request_other_msi_irqs(ioa_cfg);
9811         } else {
9812                 rc = request_irq(pdev->irq, ipr_isr,
9813                          IRQF_SHARED,
9814                          IPR_NAME, &ioa_cfg->hrrq[0]);
9815         }
9816         if (rc) {
9817                 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
9818                         pdev->irq, rc);
9819                 goto cleanup_nolog;
9820         }
9821
9822         if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
9823             (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
9824                 ioa_cfg->needs_warm_reset = 1;
9825                 ioa_cfg->reset = ipr_reset_slot_reset;
9826         } else
9827                 ioa_cfg->reset = ipr_reset_start_bist;
9828
9829         spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
9830         list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
9831         spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
9832
9833         LEAVE;
9834 out:
9835         return rc;
9836
9837 cleanup_nolog:
9838         ipr_free_mem(ioa_cfg);
9839 out_msi_disable:
9840         ipr_wait_for_pci_err_recovery(ioa_cfg);
9841         if (ioa_cfg->intr_flag == IPR_USE_MSI)
9842                 pci_disable_msi(pdev);
9843         else if (ioa_cfg->intr_flag == IPR_USE_MSIX)
9844                 pci_disable_msix(pdev);
9845 cleanup_nomem:
9846         iounmap(ipr_regs);
9847 out_disable:
9848         pci_disable_device(pdev);
9849 out_release_regions:
9850         pci_release_regions(pdev);
9851 out_scsi_host_put:
9852         scsi_host_put(host);
9853         goto out;
9854 }
9855
9856 /**
9857  * ipr_initiate_ioa_bringdown - Bring down an adapter
9858  * @ioa_cfg:            ioa config struct
9859  * @shutdown_type:      shutdown type
9860  *
9861  * Description: This function will initiate bringing down the adapter.
9862  * This consists of issuing an IOA shutdown to the adapter
9863  * to flush the cache, and running BIST.
9864  * If the caller needs to wait on the completion of the reset,
9865  * the caller must sleep on the reset_wait_q.
9866  *
9867  * Return value:
9868  *      none
9869  **/
9870 static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
9871                                        enum ipr_shutdown_type shutdown_type)
9872 {
9873         ENTER;
9874         if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
9875                 ioa_cfg->sdt_state = ABORT_DUMP;
9876         ioa_cfg->reset_retries = 0;
9877         ioa_cfg->in_ioa_bringdown = 1;
9878         ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
9879         LEAVE;
9880 }
9881
9882 /**
9883  * __ipr_remove - Remove a single adapter
9884  * @pdev:       pci device struct
9885  *
9886  * Adapter hot plug remove entry point.
9887  *
9888  * Return value:
9889  *      none
9890  **/
9891 static void __ipr_remove(struct pci_dev *pdev)
9892 {
9893         unsigned long host_lock_flags = 0;
9894         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
9895         int i;
9896         unsigned long driver_lock_flags;
9897         ENTER;
9898
9899         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
9900         while (ioa_cfg->in_reset_reload) {
9901                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9902                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9903                 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
9904         }
9905
9906         for (i = 0; i < ioa_cfg->hrrq_num; i++) {
9907                 spin_lock(&ioa_cfg->hrrq[i]._lock);
9908                 ioa_cfg->hrrq[i].removing_ioa = 1;
9909                 spin_unlock(&ioa_cfg->hrrq[i]._lock);
9910         }
9911         wmb();
9912         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
9913
9914         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9915         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9916         flush_work(&ioa_cfg->work_q);
9917         INIT_LIST_HEAD(&ioa_cfg->used_res_q);
9918         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
9919
9920         spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
9921         list_del(&ioa_cfg->queue);
9922         spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
9923
9924         if (ioa_cfg->sdt_state == ABORT_DUMP)
9925                 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
9926         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9927
9928         ipr_free_all_resources(ioa_cfg);
9929
9930         LEAVE;
9931 }
9932
9933 /**
9934  * ipr_remove - IOA hot plug remove entry point
9935  * @pdev:       pci device struct
9936  *
9937  * Adapter hot plug remove entry point.
9938  *
9939  * Return value:
9940  *      none
9941  **/
9942 static void ipr_remove(struct pci_dev *pdev)
9943 {
9944         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
9945
9946         ENTER;
9947
9948         ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
9949                               &ipr_trace_attr);
9950         ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
9951                              &ipr_dump_attr);
9952         scsi_remove_host(ioa_cfg->host);
9953
9954         __ipr_remove(pdev);
9955
9956         LEAVE;
9957 }
9958
9959 /**
9960  * ipr_probe - Adapter hot plug add entry point
9961  *
9962  * Return value:
9963  *      0 on success / non-zero on failure
9964  **/
9965 static int ipr_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
9966 {
9967         struct ipr_ioa_cfg *ioa_cfg;
9968         int rc, i;
9969
9970         rc = ipr_probe_ioa(pdev, dev_id);
9971
9972         if (rc)
9973                 return rc;
9974
9975         ioa_cfg = pci_get_drvdata(pdev);
9976         rc = ipr_probe_ioa_part2(ioa_cfg);
9977
9978         if (rc) {
9979                 __ipr_remove(pdev);
9980                 return rc;
9981         }
9982
9983         rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
9984
9985         if (rc) {
9986                 __ipr_remove(pdev);
9987                 return rc;
9988         }
9989
9990         rc = ipr_create_trace_file(&ioa_cfg->host->shost_dev.kobj,
9991                                    &ipr_trace_attr);
9992
9993         if (rc) {
9994                 scsi_remove_host(ioa_cfg->host);
9995                 __ipr_remove(pdev);
9996                 return rc;
9997         }
9998
9999         rc = ipr_create_dump_file(&ioa_cfg->host->shost_dev.kobj,
10000                                    &ipr_dump_attr);
10001
10002         if (rc) {
10003                 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
10004                                       &ipr_trace_attr);
10005                 scsi_remove_host(ioa_cfg->host);
10006                 __ipr_remove(pdev);
10007                 return rc;
10008         }
10009
10010         scsi_scan_host(ioa_cfg->host);
10011         ioa_cfg->iopoll_weight = ioa_cfg->chip_cfg->iopoll_weight;
10012
10013         if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
10014                 for (i = 1; i < ioa_cfg->hrrq_num; i++) {
10015                         blk_iopoll_init(&ioa_cfg->hrrq[i].iopoll,
10016                                         ioa_cfg->iopoll_weight, ipr_iopoll);
10017                         blk_iopoll_enable(&ioa_cfg->hrrq[i].iopoll);
10018                 }
10019         }
10020
10021         schedule_work(&ioa_cfg->work_q);
10022         return 0;
10023 }
10024
10025 /**
10026  * ipr_shutdown - Shutdown handler.
10027  * @pdev:       pci device struct
10028  *
10029  * This function is invoked upon system shutdown/reboot. It will issue
10030  * an adapter shutdown to the adapter to flush the write cache.
10031  *
10032  * Return value:
10033  *      none
10034  **/
10035 static void ipr_shutdown(struct pci_dev *pdev)
10036 {
10037         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
10038         unsigned long lock_flags = 0;
10039         int i;
10040
10041         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
10042         if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
10043                 ioa_cfg->iopoll_weight = 0;
10044                 for (i = 1; i < ioa_cfg->hrrq_num; i++)
10045                         blk_iopoll_disable(&ioa_cfg->hrrq[i].iopoll);
10046         }
10047
10048         while (ioa_cfg->in_reset_reload) {
10049                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
10050                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
10051                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
10052         }
10053
10054         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
10055         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
10056         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
10057 }
10058
10059 static struct pci_device_id ipr_pci_table[] = {
10060         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
10061                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
10062         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
10063                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
10064         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
10065                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
10066         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
10067                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
10068         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
10069                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
10070         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
10071                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
10072         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
10073                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
10074         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
10075                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
10076                 IPR_USE_LONG_TRANSOP_TIMEOUT },
10077         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
10078               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
10079         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
10080               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
10081               IPR_USE_LONG_TRANSOP_TIMEOUT },
10082         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
10083               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
10084               IPR_USE_LONG_TRANSOP_TIMEOUT },
10085         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
10086               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
10087         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
10088               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
10089               IPR_USE_LONG_TRANSOP_TIMEOUT},
10090         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
10091               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
10092               IPR_USE_LONG_TRANSOP_TIMEOUT },
10093         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
10094               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
10095               IPR_USE_LONG_TRANSOP_TIMEOUT },
10096         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
10097               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
10098         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
10099               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CC, 0, 0, 0 },
10100         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
10101               PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
10102               IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
10103         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
10104                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
10105         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
10106                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
10107         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
10108                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
10109                 IPR_USE_LONG_TRANSOP_TIMEOUT },
10110         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
10111                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
10112                 IPR_USE_LONG_TRANSOP_TIMEOUT },
10113         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10114                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B5, 0, 0, 0 },
10115         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10116                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0, 0 },
10117         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10118                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B2, 0, 0, 0 },
10119         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10120                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C0, 0, 0, 0 },
10121         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10122                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C3, 0, 0, 0 },
10123         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
10124                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C4, 0, 0, 0 },
10125         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10126                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B4, 0, 0, 0 },
10127         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10128                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B1, 0, 0, 0 },
10129         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10130                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C6, 0, 0, 0 },
10131         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10132                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C8, 0, 0, 0 },
10133         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10134                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CE, 0, 0, 0 },
10135         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10136                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D5, 0, 0, 0 },
10137         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10138                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D6, 0, 0, 0 },
10139         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10140                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D7, 0, 0, 0 },
10141         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10142                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D8, 0, 0, 0 },
10143         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10144                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D9, 0, 0, 0 },
10145         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10146                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57DA, 0, 0, 0 },
10147         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10148                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EB, 0, 0, 0 },
10149         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10150                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EC, 0, 0, 0 },
10151         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10152                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57ED, 0, 0, 0 },
10153         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10154                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EE, 0, 0, 0 },
10155         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10156                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EF, 0, 0, 0 },
10157         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10158                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57F0, 0, 0, 0 },
10159         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10160                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CCA, 0, 0, 0 },
10161         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10162                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CD2, 0, 0, 0 },
10163         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10164                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CCD, 0, 0, 0 },
10165         { }
10166 };
10167 MODULE_DEVICE_TABLE(pci, ipr_pci_table);
10168
10169 static const struct pci_error_handlers ipr_err_handler = {
10170         .error_detected = ipr_pci_error_detected,
10171         .mmio_enabled = ipr_pci_mmio_enabled,
10172         .slot_reset = ipr_pci_slot_reset,
10173 };
10174
10175 static struct pci_driver ipr_driver = {
10176         .name = IPR_NAME,
10177         .id_table = ipr_pci_table,
10178         .probe = ipr_probe,
10179         .remove = ipr_remove,
10180         .shutdown = ipr_shutdown,
10181         .err_handler = &ipr_err_handler,
10182 };
10183
10184 /**
10185  * ipr_halt_done - Shutdown prepare completion
10186  *
10187  * Return value:
10188  *      none
10189  **/
10190 static void ipr_halt_done(struct ipr_cmnd *ipr_cmd)
10191 {
10192         list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
10193 }
10194
10195 /**
10196  * ipr_halt - Issue shutdown prepare to all adapters
10197  *
10198  * Return value:
10199  *      NOTIFY_OK on success / NOTIFY_DONE on failure
10200  **/
10201 static int ipr_halt(struct notifier_block *nb, ulong event, void *buf)
10202 {
10203         struct ipr_cmnd *ipr_cmd;
10204         struct ipr_ioa_cfg *ioa_cfg;
10205         unsigned long flags = 0, driver_lock_flags;
10206
10207         if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
10208                 return NOTIFY_DONE;
10209
10210         spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
10211
10212         list_for_each_entry(ioa_cfg, &ipr_ioa_head, queue) {
10213                 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
10214                 if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds) {
10215                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
10216                         continue;
10217                 }
10218
10219                 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
10220                 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
10221                 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
10222                 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
10223                 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
10224
10225                 ipr_do_req(ipr_cmd, ipr_halt_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
10226                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
10227         }
10228         spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
10229
10230         return NOTIFY_OK;
10231 }
10232
10233 static struct notifier_block ipr_notifier = {
10234         ipr_halt, NULL, 0
10235 };
10236
10237 /**
10238  * ipr_init - Module entry point
10239  *
10240  * Return value:
10241  *      0 on success / negative value on failure
10242  **/
10243 static int __init ipr_init(void)
10244 {
10245         ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
10246                  IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
10247
10248         register_reboot_notifier(&ipr_notifier);
10249         return pci_register_driver(&ipr_driver);
10250 }
10251
10252 /**
10253  * ipr_exit - Module unload
10254  *
10255  * Module unload entry point.
10256  *
10257  * Return value:
10258  *      none
10259  **/
10260 static void __exit ipr_exit(void)
10261 {
10262         unregister_reboot_notifier(&ipr_notifier);
10263         pci_unregister_driver(&ipr_driver);
10264 }
10265
10266 module_init(ipr_init);
10267 module_exit(ipr_exit);