blob: d871dd37d22c6bf7d0b62db90d1cfc1eb46f4481 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/fs.h>
58#include <linux/init.h>
59#include <linux/types.h>
60#include <linux/errno.h>
61#include <linux/kernel.h>
62#include <linux/ioport.h>
63#include <linux/delay.h>
64#include <linux/pci.h>
65#include <linux/wait.h>
66#include <linux/spinlock.h>
67#include <linux/sched.h>
68#include <linux/interrupt.h>
69#include <linux/blkdev.h>
70#include <linux/firmware.h>
71#include <linux/module.h>
72#include <linux/moduleparam.h>
Brian King35a39692006-09-25 12:39:20 -050073#include <linux/libata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <asm/io.h>
75#include <asm/irq.h>
76#include <asm/processor.h>
77#include <scsi/scsi.h>
78#include <scsi/scsi_host.h>
79#include <scsi/scsi_tcq.h>
80#include <scsi/scsi_eh.h>
81#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include "ipr.h"
83
84/*
85 * Global Data
86 */
87static struct list_head ipr_ioa_head = LIST_HEAD_INIT(ipr_ioa_head);
88static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
89static unsigned int ipr_max_speed = 1;
90static int ipr_testmode = 0;
91static unsigned int ipr_fastfail = 0;
Brian King5469cb52007-03-29 12:42:40 -050092static unsigned int ipr_transop_timeout = 0;
brking@us.ibm.com62275042005-11-01 17:01:14 -060093static unsigned int ipr_enable_cache = 1;
brking@us.ibm.comd3c74872005-11-01 17:01:34 -060094static unsigned int ipr_debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static DEFINE_SPINLOCK(ipr_driver_lock);
96
97/* This table describes the differences between DMA controller chips */
98static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
Brian King60e74862006-11-21 10:28:10 -060099 { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .mailbox = 0x0042C,
101 .cache_line_size = 0x20,
102 {
103 .set_interrupt_mask_reg = 0x0022C,
104 .clr_interrupt_mask_reg = 0x00230,
105 .sense_interrupt_mask_reg = 0x0022C,
106 .clr_interrupt_reg = 0x00228,
107 .sense_interrupt_reg = 0x00224,
108 .ioarrin_reg = 0x00404,
109 .sense_uproc_interrupt_reg = 0x00214,
110 .set_uproc_interrupt_reg = 0x00214,
111 .clr_uproc_interrupt_reg = 0x00218
112 }
113 },
114 { /* Snipe and Scamp */
115 .mailbox = 0x0052C,
116 .cache_line_size = 0x20,
117 {
118 .set_interrupt_mask_reg = 0x00288,
119 .clr_interrupt_mask_reg = 0x0028C,
120 .sense_interrupt_mask_reg = 0x00288,
121 .clr_interrupt_reg = 0x00284,
122 .sense_interrupt_reg = 0x00280,
123 .ioarrin_reg = 0x00504,
124 .sense_uproc_interrupt_reg = 0x00290,
125 .set_uproc_interrupt_reg = 0x00290,
126 .clr_uproc_interrupt_reg = 0x00294
127 }
128 },
129};
130
131static const struct ipr_chip_t ipr_chip[] = {
132 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, &ipr_chip_cfg[0] },
133 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, &ipr_chip_cfg[0] },
brking@us.ibm.com86f51432005-11-01 17:02:42 -0600134 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, &ipr_chip_cfg[0] },
135 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, &ipr_chip_cfg[0] },
Brian King60e74862006-11-21 10:28:10 -0600136 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, &ipr_chip_cfg[0] },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, &ipr_chip_cfg[1] },
138 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, &ipr_chip_cfg[1] }
139};
140
141static int ipr_max_bus_speeds [] = {
142 IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
143};
144
145MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
146MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
147module_param_named(max_speed, ipr_max_speed, uint, 0);
148MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
149module_param_named(log_level, ipr_log_level, uint, 0);
150MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
151module_param_named(testmode, ipr_testmode, int, 0);
152MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
153module_param_named(fastfail, ipr_fastfail, int, 0);
154MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
155module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
156MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
brking@us.ibm.com62275042005-11-01 17:01:14 -0600157module_param_named(enable_cache, ipr_enable_cache, int, 0);
158MODULE_PARM_DESC(enable_cache, "Enable adapter's non-volatile write cache (default: 1)");
brking@us.ibm.comd3c74872005-11-01 17:01:34 -0600159module_param_named(debug, ipr_debug, int, 0);
160MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161MODULE_LICENSE("GPL");
162MODULE_VERSION(IPR_DRIVER_VERSION);
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/* A constant array of IOASCs/URCs/Error Messages */
165static const
166struct ipr_error_table_t ipr_error_table[] = {
Brian King933916f2007-03-29 12:43:30 -0500167 {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 "8155: An unknown error was received"},
169 {0x00330000, 0, 0,
170 "Soft underlength error"},
171 {0x005A0000, 0, 0,
172 "Command to be cancelled not found"},
173 {0x00808000, 0, 0,
174 "Qualified success"},
Brian King933916f2007-03-29 12:43:30 -0500175 {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 "FFFE: Soft device bus error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500177 {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500178 "4101: Soft device bus fabric error"},
Brian King933916f2007-03-29 12:43:30 -0500179 {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 "FFF9: Device sector reassign successful"},
Brian King933916f2007-03-29 12:43:30 -0500181 {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 "FFF7: Media error recovered by device rewrite procedures"},
Brian King933916f2007-03-29 12:43:30 -0500183 {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 "7001: IOA sector reassignment successful"},
Brian King933916f2007-03-29 12:43:30 -0500185 {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 "FFF9: Soft media error. Sector reassignment recommended"},
Brian King933916f2007-03-29 12:43:30 -0500187 {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 "FFF7: Media error recovered by IOA rewrite procedures"},
Brian King933916f2007-03-29 12:43:30 -0500189 {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 "FF3D: Soft PCI bus error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500191 {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 "FFF6: Device hardware error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500193 {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 "FFF6: Device hardware error recovered by the device"},
Brian King933916f2007-03-29 12:43:30 -0500195 {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 "FF3D: Soft IOA error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500197 {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 "FFFA: Undefined device response recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500199 {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 "FFF6: Device bus error, message or command phase"},
Brian King933916f2007-03-29 12:43:30 -0500201 {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King35a39692006-09-25 12:39:20 -0500202 "FFFE: Task Management Function failed"},
Brian King933916f2007-03-29 12:43:30 -0500203 {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 "FFF6: Failure prediction threshold exceeded"},
Brian King933916f2007-03-29 12:43:30 -0500205 {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 "8009: Impending cache battery pack failure"},
207 {0x02040400, 0, 0,
208 "34FF: Disk device format in progress"},
209 {0x023F0000, 0, 0,
210 "Synchronization required"},
211 {0x024E0000, 0, 0,
212 "No ready, IOA shutdown"},
213 {0x025A0000, 0, 0,
214 "Not ready, IOA has been shutdown"},
Brian King933916f2007-03-29 12:43:30 -0500215 {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 "3020: Storage subsystem configuration error"},
217 {0x03110B00, 0, 0,
218 "FFF5: Medium error, data unreadable, recommend reassign"},
219 {0x03110C00, 0, 0,
220 "7000: Medium error, data unreadable, do not reassign"},
Brian King933916f2007-03-29 12:43:30 -0500221 {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 "FFF3: Disk media format bad"},
Brian King933916f2007-03-29 12:43:30 -0500223 {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 "3002: Addressed device failed to respond to selection"},
Brian King933916f2007-03-29 12:43:30 -0500225 {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 "3100: Device bus error"},
Brian King933916f2007-03-29 12:43:30 -0500227 {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 "3109: IOA timed out a device command"},
229 {0x04088000, 0, 0,
230 "3120: SCSI bus is not operational"},
Brian King933916f2007-03-29 12:43:30 -0500231 {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500232 "4100: Hard device bus fabric error"},
Brian King933916f2007-03-29 12:43:30 -0500233 {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 "9000: IOA reserved area data check"},
Brian King933916f2007-03-29 12:43:30 -0500235 {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 "9001: IOA reserved area invalid data pattern"},
Brian King933916f2007-03-29 12:43:30 -0500237 {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 "9002: IOA reserved area LRC error"},
Brian King933916f2007-03-29 12:43:30 -0500239 {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 "102E: Out of alternate sectors for disk storage"},
Brian King933916f2007-03-29 12:43:30 -0500241 {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 "FFF4: Data transfer underlength error"},
Brian King933916f2007-03-29 12:43:30 -0500243 {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 "FFF4: Data transfer overlength error"},
Brian King933916f2007-03-29 12:43:30 -0500245 {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 "3400: Logical unit failure"},
Brian King933916f2007-03-29 12:43:30 -0500247 {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 "FFF4: Device microcode is corrupt"},
Brian King933916f2007-03-29 12:43:30 -0500249 {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 "8150: PCI bus error"},
251 {0x04430000, 1, 0,
252 "Unsupported device bus message received"},
Brian King933916f2007-03-29 12:43:30 -0500253 {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 "FFF4: Disk device problem"},
Brian King933916f2007-03-29 12:43:30 -0500255 {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 "8150: Permanent IOA failure"},
Brian King933916f2007-03-29 12:43:30 -0500257 {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 "3010: Disk device returned wrong response to IOA"},
Brian King933916f2007-03-29 12:43:30 -0500259 {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 "8151: IOA microcode error"},
261 {0x04448500, 0, 0,
262 "Device bus status error"},
Brian King933916f2007-03-29 12:43:30 -0500263 {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 "8157: IOA error requiring IOA reset to recover"},
Brian King35a39692006-09-25 12:39:20 -0500265 {0x04448700, 0, 0,
266 "ATA device status error"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 {0x04490000, 0, 0,
268 "Message reject received from the device"},
Brian King933916f2007-03-29 12:43:30 -0500269 {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 "8008: A permanent cache battery pack failure occurred"},
Brian King933916f2007-03-29 12:43:30 -0500271 {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 "9090: Disk unit has been modified after the last known status"},
Brian King933916f2007-03-29 12:43:30 -0500273 {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 "9081: IOA detected device error"},
Brian King933916f2007-03-29 12:43:30 -0500275 {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 "9082: IOA detected device error"},
Brian King933916f2007-03-29 12:43:30 -0500277 {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 "3110: Device bus error, message or command phase"},
Brian King933916f2007-03-29 12:43:30 -0500279 {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
Brian King35a39692006-09-25 12:39:20 -0500280 "3110: SAS Command / Task Management Function failed"},
Brian King933916f2007-03-29 12:43:30 -0500281 {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 "9091: Incorrect hardware configuration change has been detected"},
Brian King933916f2007-03-29 12:43:30 -0500283 {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600284 "9073: Invalid multi-adapter configuration"},
Brian King933916f2007-03-29 12:43:30 -0500285 {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500286 "4010: Incorrect connection between cascaded expanders"},
Brian King933916f2007-03-29 12:43:30 -0500287 {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500288 "4020: Connections exceed IOA design limits"},
Brian King933916f2007-03-29 12:43:30 -0500289 {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500290 "4030: Incorrect multipath connection"},
Brian King933916f2007-03-29 12:43:30 -0500291 {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500292 "4110: Unsupported enclosure function"},
Brian King933916f2007-03-29 12:43:30 -0500293 {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 "FFF4: Command to logical unit failed"},
295 {0x05240000, 1, 0,
296 "Illegal request, invalid request type or request packet"},
297 {0x05250000, 0, 0,
298 "Illegal request, invalid resource handle"},
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600299 {0x05258000, 0, 0,
300 "Illegal request, commands not allowed to this device"},
301 {0x05258100, 0, 0,
302 "Illegal request, command not allowed to a secondary adapter"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 {0x05260000, 0, 0,
304 "Illegal request, invalid field in parameter list"},
305 {0x05260100, 0, 0,
306 "Illegal request, parameter not supported"},
307 {0x05260200, 0, 0,
308 "Illegal request, parameter value invalid"},
309 {0x052C0000, 0, 0,
310 "Illegal request, command sequence error"},
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600311 {0x052C8000, 1, 0,
312 "Illegal request, dual adapter support not enabled"},
Brian King933916f2007-03-29 12:43:30 -0500313 {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 "9031: Array protection temporarily suspended, protection resuming"},
Brian King933916f2007-03-29 12:43:30 -0500315 {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 "9040: Array protection temporarily suspended, protection resuming"},
Brian King933916f2007-03-29 12:43:30 -0500317 {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500318 "3140: Device bus not ready to ready transition"},
Brian King933916f2007-03-29 12:43:30 -0500319 {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 "FFFB: SCSI bus was reset"},
321 {0x06290500, 0, 0,
322 "FFFE: SCSI bus transition to single ended"},
323 {0x06290600, 0, 0,
324 "FFFE: SCSI bus transition to LVD"},
Brian King933916f2007-03-29 12:43:30 -0500325 {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 "FFFB: SCSI bus was reset by another initiator"},
Brian King933916f2007-03-29 12:43:30 -0500327 {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 "3029: A device replacement has occurred"},
Brian King933916f2007-03-29 12:43:30 -0500329 {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 "9051: IOA cache data exists for a missing or failed device"},
Brian King933916f2007-03-29 12:43:30 -0500331 {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600332 "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
Brian King933916f2007-03-29 12:43:30 -0500333 {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 "9025: Disk unit is not supported at its physical location"},
Brian King933916f2007-03-29 12:43:30 -0500335 {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 "3020: IOA detected a SCSI bus configuration error"},
Brian King933916f2007-03-29 12:43:30 -0500337 {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 "3150: SCSI bus configuration error"},
Brian King933916f2007-03-29 12:43:30 -0500339 {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600340 "9074: Asymmetric advanced function disk configuration"},
Brian King933916f2007-03-29 12:43:30 -0500341 {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500342 "4040: Incomplete multipath connection between IOA and enclosure"},
Brian King933916f2007-03-29 12:43:30 -0500343 {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500344 "4041: Incomplete multipath connection between enclosure and device"},
Brian King933916f2007-03-29 12:43:30 -0500345 {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500346 "9075: Incomplete multipath connection between IOA and remote IOA"},
Brian King933916f2007-03-29 12:43:30 -0500347 {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500348 "9076: Configuration error, missing remote IOA"},
Brian King933916f2007-03-29 12:43:30 -0500349 {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500350 "4050: Enclosure does not support a required multipath function"},
Brian King933916f2007-03-29 12:43:30 -0500351 {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 "9041: Array protection temporarily suspended"},
Brian King933916f2007-03-29 12:43:30 -0500353 {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 "9042: Corrupt array parity detected on specified device"},
Brian King933916f2007-03-29 12:43:30 -0500355 {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 "9030: Array no longer protected due to missing or failed disk unit"},
Brian King933916f2007-03-29 12:43:30 -0500357 {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600358 "9071: Link operational transition"},
Brian King933916f2007-03-29 12:43:30 -0500359 {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600360 "9072: Link not operational transition"},
Brian King933916f2007-03-29 12:43:30 -0500361 {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 "9032: Array exposed but still protected"},
Brian Kinge4353402007-03-29 12:43:37 -0500363 {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
364 "70DD: Device forced failed by disrupt device command"},
Brian King933916f2007-03-29 12:43:30 -0500365 {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500366 "4061: Multipath redundancy level got better"},
Brian King933916f2007-03-29 12:43:30 -0500367 {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500368 "4060: Multipath redundancy level got worse"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 {0x07270000, 0, 0,
370 "Failure due to other device"},
Brian King933916f2007-03-29 12:43:30 -0500371 {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 "9008: IOA does not support functions expected by devices"},
Brian King933916f2007-03-29 12:43:30 -0500373 {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 "9010: Cache data associated with attached devices cannot be found"},
Brian King933916f2007-03-29 12:43:30 -0500375 {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 "9011: Cache data belongs to devices other than those attached"},
Brian King933916f2007-03-29 12:43:30 -0500377 {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 "9020: Array missing 2 or more devices with only 1 device present"},
Brian King933916f2007-03-29 12:43:30 -0500379 {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 "9021: Array missing 2 or more devices with 2 or more devices present"},
Brian King933916f2007-03-29 12:43:30 -0500381 {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 "9022: Exposed array is missing a required device"},
Brian King933916f2007-03-29 12:43:30 -0500383 {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 "9023: Array member(s) not at required physical locations"},
Brian King933916f2007-03-29 12:43:30 -0500385 {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 "9024: Array not functional due to present hardware configuration"},
Brian King933916f2007-03-29 12:43:30 -0500387 {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 "9026: Array not functional due to present hardware configuration"},
Brian King933916f2007-03-29 12:43:30 -0500389 {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 "9027: Array is missing a device and parity is out of sync"},
Brian King933916f2007-03-29 12:43:30 -0500391 {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 "9028: Maximum number of arrays already exist"},
Brian King933916f2007-03-29 12:43:30 -0500393 {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 "9050: Required cache data cannot be located for a disk unit"},
Brian King933916f2007-03-29 12:43:30 -0500395 {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 "9052: Cache data exists for a device that has been modified"},
Brian King933916f2007-03-29 12:43:30 -0500397 {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 "9054: IOA resources not available due to previous problems"},
Brian King933916f2007-03-29 12:43:30 -0500399 {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 "9092: Disk unit requires initialization before use"},
Brian King933916f2007-03-29 12:43:30 -0500401 {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 "9029: Incorrect hardware configuration change has been detected"},
Brian King933916f2007-03-29 12:43:30 -0500403 {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 "9060: One or more disk pairs are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500405 {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 "9061: One or more disks are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500407 {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 "9062: One or more disks are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500409 {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 "9063: Maximum number of functional arrays has been exceeded"},
411 {0x0B260000, 0, 0,
412 "Aborted command, invalid descriptor"},
413 {0x0B5A0000, 0, 0,
414 "Command terminated by host"}
415};
416
417static const struct ipr_ses_table_entry ipr_ses_table[] = {
418 { "2104-DL1 ", "XXXXXXXXXXXXXXXX", 80 },
419 { "2104-TL1 ", "XXXXXXXXXXXXXXXX", 80 },
420 { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
421 { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
422 { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
423 { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
424 { "2104-DU3 ", "XXXXXXXXXXXXXXXX", 160 },
425 { "2104-TU3 ", "XXXXXXXXXXXXXXXX", 160 },
426 { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
427 { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
428 { "St V1S2 ", "XXXXXXXXXXXXXXXX", 160 },
429 { "HSBPD4M PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
430 { "VSBPD1H U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
431};
432
433/*
434 * Function Prototypes
435 */
436static int ipr_reset_alert(struct ipr_cmnd *);
437static void ipr_process_ccn(struct ipr_cmnd *);
438static void ipr_process_error(struct ipr_cmnd *);
439static void ipr_reset_ioa_job(struct ipr_cmnd *);
440static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
441 enum ipr_shutdown_type);
442
443#ifdef CONFIG_SCSI_IPR_TRACE
444/**
445 * ipr_trc_hook - Add a trace entry to the driver trace
446 * @ipr_cmd: ipr command struct
447 * @type: trace type
448 * @add_data: additional data
449 *
450 * Return value:
451 * none
452 **/
453static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
454 u8 type, u32 add_data)
455{
456 struct ipr_trace_entry *trace_entry;
457 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
458
459 trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
460 trace_entry->time = jiffies;
461 trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
462 trace_entry->type = type;
Brian King35a39692006-09-25 12:39:20 -0500463 trace_entry->ata_op_code = ipr_cmd->ioarcb.add_data.u.regs.command;
464 trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
466 trace_entry->u.add_data = add_data;
467}
468#else
469#define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
470#endif
471
472/**
473 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
474 * @ipr_cmd: ipr command struct
475 *
476 * Return value:
477 * none
478 **/
479static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
480{
481 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
482 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
Brian King51b1c7e2007-03-29 12:43:50 -0500483 dma_addr_t dma_addr = be32_to_cpu(ioarcb->ioarcb_host_pci_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
486 ioarcb->write_data_transfer_length = 0;
487 ioarcb->read_data_transfer_length = 0;
488 ioarcb->write_ioadl_len = 0;
489 ioarcb->read_ioadl_len = 0;
Brian King51b1c7e2007-03-29 12:43:50 -0500490 ioarcb->write_ioadl_addr =
491 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
492 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 ioasa->ioasc = 0;
494 ioasa->residual_data_len = 0;
Brian King35a39692006-09-25 12:39:20 -0500495 ioasa->u.gata.status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 ipr_cmd->scsi_cmd = NULL;
Brian King35a39692006-09-25 12:39:20 -0500498 ipr_cmd->qc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 ipr_cmd->sense_buffer[0] = 0;
500 ipr_cmd->dma_use_sg = 0;
501}
502
503/**
504 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
505 * @ipr_cmd: ipr command struct
506 *
507 * Return value:
508 * none
509 **/
510static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
511{
512 ipr_reinit_ipr_cmnd(ipr_cmd);
513 ipr_cmd->u.scratch = 0;
514 ipr_cmd->sibling = NULL;
515 init_timer(&ipr_cmd->timer);
516}
517
518/**
519 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
520 * @ioa_cfg: ioa config struct
521 *
522 * Return value:
523 * pointer to ipr command struct
524 **/
525static
526struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
527{
528 struct ipr_cmnd *ipr_cmd;
529
530 ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
531 list_del(&ipr_cmd->queue);
532 ipr_init_ipr_cmnd(ipr_cmd);
533
534 return ipr_cmd;
535}
536
537/**
538 * ipr_unmap_sglist - Unmap scatterlist if mapped
539 * @ioa_cfg: ioa config struct
540 * @ipr_cmd: ipr command struct
541 *
542 * Return value:
543 * nothing
544 **/
545static void ipr_unmap_sglist(struct ipr_ioa_cfg *ioa_cfg,
546 struct ipr_cmnd *ipr_cmd)
547{
548 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
549
550 if (ipr_cmd->dma_use_sg) {
551 if (scsi_cmd->use_sg > 0) {
552 pci_unmap_sg(ioa_cfg->pdev, scsi_cmd->request_buffer,
553 scsi_cmd->use_sg,
554 scsi_cmd->sc_data_direction);
555 } else {
556 pci_unmap_single(ioa_cfg->pdev, ipr_cmd->dma_handle,
557 scsi_cmd->request_bufflen,
558 scsi_cmd->sc_data_direction);
559 }
560 }
561}
562
563/**
564 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
565 * @ioa_cfg: ioa config struct
566 * @clr_ints: interrupts to clear
567 *
568 * This function masks all interrupts on the adapter, then clears the
569 * interrupts specified in the mask
570 *
571 * Return value:
572 * none
573 **/
574static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
575 u32 clr_ints)
576{
577 volatile u32 int_reg;
578
579 /* Stop new interrupts */
580 ioa_cfg->allow_interrupts = 0;
581
582 /* Set interrupt mask to stop all new interrupts */
583 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
584
585 /* Clear any pending interrupts */
586 writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
587 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
588}
589
590/**
591 * ipr_save_pcix_cmd_reg - Save PCI-X command register
592 * @ioa_cfg: ioa config struct
593 *
594 * Return value:
595 * 0 on success / -EIO on failure
596 **/
597static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
598{
599 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
600
Brian King7dce0e12007-01-23 11:25:30 -0600601 if (pcix_cmd_reg == 0)
602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
605 &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
606 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
607 return -EIO;
608 }
609
610 ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
611 return 0;
612}
613
614/**
615 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
616 * @ioa_cfg: ioa config struct
617 *
618 * Return value:
619 * 0 on success / -EIO on failure
620 **/
621static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
622{
623 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
624
625 if (pcix_cmd_reg) {
626 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
627 ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
628 dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
629 return -EIO;
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
633 return 0;
634}
635
636/**
Brian King35a39692006-09-25 12:39:20 -0500637 * ipr_sata_eh_done - done function for aborted SATA commands
638 * @ipr_cmd: ipr command struct
639 *
640 * This function is invoked for ops generated to SATA
641 * devices which are being aborted.
642 *
643 * Return value:
644 * none
645 **/
646static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
647{
648 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
649 struct ata_queued_cmd *qc = ipr_cmd->qc;
650 struct ipr_sata_port *sata_port = qc->ap->private_data;
651
652 qc->err_mask |= AC_ERR_OTHER;
653 sata_port->ioasa.status |= ATA_BUSY;
654 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
655 ata_qc_complete(qc);
656}
657
658/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * ipr_scsi_eh_done - mid-layer done function for aborted ops
660 * @ipr_cmd: ipr command struct
661 *
662 * This function is invoked by the interrupt handler for
663 * ops generated by the SCSI mid-layer which are being aborted.
664 *
665 * Return value:
666 * none
667 **/
668static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
669{
670 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
671 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
672
673 scsi_cmd->result |= (DID_ERROR << 16);
674
675 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
676 scsi_cmd->scsi_done(scsi_cmd);
677 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
678}
679
680/**
681 * ipr_fail_all_ops - Fails all outstanding ops.
682 * @ioa_cfg: ioa config struct
683 *
684 * This function fails all outstanding ops.
685 *
686 * Return value:
687 * none
688 **/
689static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
690{
691 struct ipr_cmnd *ipr_cmd, *temp;
692
693 ENTER;
694 list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
695 list_del(&ipr_cmd->queue);
696
697 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
698 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
699
700 if (ipr_cmd->scsi_cmd)
701 ipr_cmd->done = ipr_scsi_eh_done;
Brian King35a39692006-09-25 12:39:20 -0500702 else if (ipr_cmd->qc)
703 ipr_cmd->done = ipr_sata_eh_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
706 del_timer(&ipr_cmd->timer);
707 ipr_cmd->done(ipr_cmd);
708 }
709
710 LEAVE;
711}
712
713/**
714 * ipr_do_req - Send driver initiated requests.
715 * @ipr_cmd: ipr command struct
716 * @done: done function
717 * @timeout_func: timeout function
718 * @timeout: timeout value
719 *
720 * This function sends the specified command to the adapter with the
721 * timeout given. The done function is invoked on command completion.
722 *
723 * Return value:
724 * none
725 **/
726static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
727 void (*done) (struct ipr_cmnd *),
728 void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
729{
730 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
731
732 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
733
734 ipr_cmd->done = done;
735
736 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
737 ipr_cmd->timer.expires = jiffies + timeout;
738 ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
739
740 add_timer(&ipr_cmd->timer);
741
742 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
743
744 mb();
745 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
746 ioa_cfg->regs.ioarrin_reg);
747}
748
749/**
750 * ipr_internal_cmd_done - Op done function for an internally generated op.
751 * @ipr_cmd: ipr command struct
752 *
753 * This function is the op done function for an internally generated,
754 * blocking op. It simply wakes the sleeping thread.
755 *
756 * Return value:
757 * none
758 **/
759static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
760{
761 if (ipr_cmd->sibling)
762 ipr_cmd->sibling = NULL;
763 else
764 complete(&ipr_cmd->completion);
765}
766
767/**
768 * ipr_send_blocking_cmd - Send command and sleep on its completion.
769 * @ipr_cmd: ipr command struct
770 * @timeout_func: function to invoke if command times out
771 * @timeout: timeout
772 *
773 * Return value:
774 * none
775 **/
776static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
777 void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
778 u32 timeout)
779{
780 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
781
782 init_completion(&ipr_cmd->completion);
783 ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
784
785 spin_unlock_irq(ioa_cfg->host->host_lock);
786 wait_for_completion(&ipr_cmd->completion);
787 spin_lock_irq(ioa_cfg->host->host_lock);
788}
789
790/**
791 * ipr_send_hcam - Send an HCAM to the adapter.
792 * @ioa_cfg: ioa config struct
793 * @type: HCAM type
794 * @hostrcb: hostrcb struct
795 *
796 * This function will send a Host Controlled Async command to the adapter.
797 * If HCAMs are currently not allowed to be issued to the adapter, it will
798 * place the hostrcb on the free queue.
799 *
800 * Return value:
801 * none
802 **/
803static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
804 struct ipr_hostrcb *hostrcb)
805{
806 struct ipr_cmnd *ipr_cmd;
807 struct ipr_ioarcb *ioarcb;
808
809 if (ioa_cfg->allow_cmds) {
810 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
811 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
812 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
813
814 ipr_cmd->u.hostrcb = hostrcb;
815 ioarcb = &ipr_cmd->ioarcb;
816
817 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
818 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
819 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
820 ioarcb->cmd_pkt.cdb[1] = type;
821 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
822 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
823
824 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
825 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
826 ipr_cmd->ioadl[0].flags_and_data_len =
827 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
828 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
829
830 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
831 ipr_cmd->done = ipr_process_ccn;
832 else
833 ipr_cmd->done = ipr_process_error;
834
835 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
836
837 mb();
838 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
839 ioa_cfg->regs.ioarrin_reg);
840 } else {
841 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
842 }
843}
844
845/**
846 * ipr_init_res_entry - Initialize a resource entry struct.
847 * @res: resource entry struct
848 *
849 * Return value:
850 * none
851 **/
852static void ipr_init_res_entry(struct ipr_resource_entry *res)
853{
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -0600854 res->needs_sync_complete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 res->in_erp = 0;
856 res->add_to_ml = 0;
857 res->del_from_ml = 0;
858 res->resetting_device = 0;
859 res->sdev = NULL;
Brian King35a39692006-09-25 12:39:20 -0500860 res->sata_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
863/**
864 * ipr_handle_config_change - Handle a config change from the adapter
865 * @ioa_cfg: ioa config struct
866 * @hostrcb: hostrcb
867 *
868 * Return value:
869 * none
870 **/
871static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
872 struct ipr_hostrcb *hostrcb)
873{
874 struct ipr_resource_entry *res = NULL;
875 struct ipr_config_table_entry *cfgte;
876 u32 is_ndn = 1;
877
878 cfgte = &hostrcb->hcam.u.ccn.cfgte;
879
880 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
881 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
882 sizeof(cfgte->res_addr))) {
883 is_ndn = 0;
884 break;
885 }
886 }
887
888 if (is_ndn) {
889 if (list_empty(&ioa_cfg->free_res_q)) {
890 ipr_send_hcam(ioa_cfg,
891 IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
892 hostrcb);
893 return;
894 }
895
896 res = list_entry(ioa_cfg->free_res_q.next,
897 struct ipr_resource_entry, queue);
898
899 list_del(&res->queue);
900 ipr_init_res_entry(res);
901 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
902 }
903
904 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
905
906 if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
907 if (res->sdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 res->del_from_ml = 1;
Brian King1121b792006-03-29 09:37:16 -0600909 res->cfgte.res_handle = IPR_INVALID_RES_HANDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (ioa_cfg->allow_ml_add_del)
911 schedule_work(&ioa_cfg->work_q);
912 } else
913 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
914 } else if (!res->sdev) {
915 res->add_to_ml = 1;
916 if (ioa_cfg->allow_ml_add_del)
917 schedule_work(&ioa_cfg->work_q);
918 }
919
920 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
921}
922
923/**
924 * ipr_process_ccn - Op done function for a CCN.
925 * @ipr_cmd: ipr command struct
926 *
927 * This function is the op done function for a configuration
928 * change notification host controlled async from the adapter.
929 *
930 * Return value:
931 * none
932 **/
933static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
934{
935 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
936 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
937 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
938
939 list_del(&hostrcb->queue);
940 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
941
942 if (ioasc) {
943 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
944 dev_err(&ioa_cfg->pdev->dev,
945 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
946
947 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
948 } else {
949 ipr_handle_config_change(ioa_cfg, hostrcb);
950 }
951}
952
953/**
954 * ipr_log_vpd - Log the passed VPD to the error log.
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600955 * @vpd: vendor/product id/sn struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 *
957 * Return value:
958 * none
959 **/
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600960static void ipr_log_vpd(struct ipr_vpd *vpd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
963 + IPR_SERIAL_NUM_LEN];
964
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600965 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
966 memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 IPR_PROD_ID_LEN);
968 buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
969 ipr_err("Vendor/Product ID: %s\n", buffer);
970
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600971 memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 buffer[IPR_SERIAL_NUM_LEN] = '\0';
973 ipr_err(" Serial Number: %s\n", buffer);
974}
975
976/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -0600977 * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
978 * @vpd: vendor/product id/sn/wwn struct
979 *
980 * Return value:
981 * none
982 **/
983static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
984{
985 ipr_log_vpd(&vpd->vpd);
986 ipr_err(" WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
987 be32_to_cpu(vpd->wwid[1]));
988}
989
990/**
991 * ipr_log_enhanced_cache_error - Log a cache error.
992 * @ioa_cfg: ioa config struct
993 * @hostrcb: hostrcb struct
994 *
995 * Return value:
996 * none
997 **/
998static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
999 struct ipr_hostrcb *hostrcb)
1000{
1001 struct ipr_hostrcb_type_12_error *error =
1002 &hostrcb->hcam.u.error.u.type_12_error;
1003
1004 ipr_err("-----Current Configuration-----\n");
1005 ipr_err("Cache Directory Card Information:\n");
1006 ipr_log_ext_vpd(&error->ioa_vpd);
1007 ipr_err("Adapter Card Information:\n");
1008 ipr_log_ext_vpd(&error->cfc_vpd);
1009
1010 ipr_err("-----Expected Configuration-----\n");
1011 ipr_err("Cache Directory Card Information:\n");
1012 ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1013 ipr_err("Adapter Card Information:\n");
1014 ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1015
1016 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1017 be32_to_cpu(error->ioa_data[0]),
1018 be32_to_cpu(error->ioa_data[1]),
1019 be32_to_cpu(error->ioa_data[2]));
1020}
1021
1022/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 * ipr_log_cache_error - Log a cache error.
1024 * @ioa_cfg: ioa config struct
1025 * @hostrcb: hostrcb struct
1026 *
1027 * Return value:
1028 * none
1029 **/
1030static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1031 struct ipr_hostrcb *hostrcb)
1032{
1033 struct ipr_hostrcb_type_02_error *error =
1034 &hostrcb->hcam.u.error.u.type_02_error;
1035
1036 ipr_err("-----Current Configuration-----\n");
1037 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001038 ipr_log_vpd(&error->ioa_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001040 ipr_log_vpd(&error->cfc_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 ipr_err("-----Expected Configuration-----\n");
1043 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001044 ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001046 ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1049 be32_to_cpu(error->ioa_data[0]),
1050 be32_to_cpu(error->ioa_data[1]),
1051 be32_to_cpu(error->ioa_data[2]));
1052}
1053
1054/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001055 * ipr_log_enhanced_config_error - Log a configuration error.
1056 * @ioa_cfg: ioa config struct
1057 * @hostrcb: hostrcb struct
1058 *
1059 * Return value:
1060 * none
1061 **/
1062static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1063 struct ipr_hostrcb *hostrcb)
1064{
1065 int errors_logged, i;
1066 struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1067 struct ipr_hostrcb_type_13_error *error;
1068
1069 error = &hostrcb->hcam.u.error.u.type_13_error;
1070 errors_logged = be32_to_cpu(error->errors_logged);
1071
1072 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1073 be32_to_cpu(error->errors_detected), errors_logged);
1074
1075 dev_entry = error->dev;
1076
1077 for (i = 0; i < errors_logged; i++, dev_entry++) {
1078 ipr_err_separator;
1079
1080 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1081 ipr_log_ext_vpd(&dev_entry->vpd);
1082
1083 ipr_err("-----New Device Information-----\n");
1084 ipr_log_ext_vpd(&dev_entry->new_vpd);
1085
1086 ipr_err("Cache Directory Card Information:\n");
1087 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1088
1089 ipr_err("Adapter Card Information:\n");
1090 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1091 }
1092}
1093
1094/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 * ipr_log_config_error - Log a configuration error.
1096 * @ioa_cfg: ioa config struct
1097 * @hostrcb: hostrcb struct
1098 *
1099 * Return value:
1100 * none
1101 **/
1102static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1103 struct ipr_hostrcb *hostrcb)
1104{
1105 int errors_logged, i;
1106 struct ipr_hostrcb_device_data_entry *dev_entry;
1107 struct ipr_hostrcb_type_03_error *error;
1108
1109 error = &hostrcb->hcam.u.error.u.type_03_error;
1110 errors_logged = be32_to_cpu(error->errors_logged);
1111
1112 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1113 be32_to_cpu(error->errors_detected), errors_logged);
1114
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001115 dev_entry = error->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 for (i = 0; i < errors_logged; i++, dev_entry++) {
1118 ipr_err_separator;
1119
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001120 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001121 ipr_log_vpd(&dev_entry->vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 ipr_err("-----New Device Information-----\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001124 ipr_log_vpd(&dev_entry->new_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001127 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001130 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1133 be32_to_cpu(dev_entry->ioa_data[0]),
1134 be32_to_cpu(dev_entry->ioa_data[1]),
1135 be32_to_cpu(dev_entry->ioa_data[2]),
1136 be32_to_cpu(dev_entry->ioa_data[3]),
1137 be32_to_cpu(dev_entry->ioa_data[4]));
1138 }
1139}
1140
1141/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001142 * ipr_log_enhanced_array_error - Log an array configuration error.
1143 * @ioa_cfg: ioa config struct
1144 * @hostrcb: hostrcb struct
1145 *
1146 * Return value:
1147 * none
1148 **/
1149static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1150 struct ipr_hostrcb *hostrcb)
1151{
1152 int i, num_entries;
1153 struct ipr_hostrcb_type_14_error *error;
1154 struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1155 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1156
1157 error = &hostrcb->hcam.u.error.u.type_14_error;
1158
1159 ipr_err_separator;
1160
1161 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1162 error->protection_level,
1163 ioa_cfg->host->host_no,
1164 error->last_func_vset_res_addr.bus,
1165 error->last_func_vset_res_addr.target,
1166 error->last_func_vset_res_addr.lun);
1167
1168 ipr_err_separator;
1169
1170 array_entry = error->array_member;
1171 num_entries = min_t(u32, be32_to_cpu(error->num_entries),
1172 sizeof(error->array_member));
1173
1174 for (i = 0; i < num_entries; i++, array_entry++) {
1175 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1176 continue;
1177
1178 if (be32_to_cpu(error->exposed_mode_adn) == i)
1179 ipr_err("Exposed Array Member %d:\n", i);
1180 else
1181 ipr_err("Array Member %d:\n", i);
1182
1183 ipr_log_ext_vpd(&array_entry->vpd);
1184 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1185 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1186 "Expected Location");
1187
1188 ipr_err_separator;
1189 }
1190}
1191
1192/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * ipr_log_array_error - Log an array configuration error.
1194 * @ioa_cfg: ioa config struct
1195 * @hostrcb: hostrcb struct
1196 *
1197 * Return value:
1198 * none
1199 **/
1200static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1201 struct ipr_hostrcb *hostrcb)
1202{
1203 int i;
1204 struct ipr_hostrcb_type_04_error *error;
1205 struct ipr_hostrcb_array_data_entry *array_entry;
1206 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1207
1208 error = &hostrcb->hcam.u.error.u.type_04_error;
1209
1210 ipr_err_separator;
1211
1212 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1213 error->protection_level,
1214 ioa_cfg->host->host_no,
1215 error->last_func_vset_res_addr.bus,
1216 error->last_func_vset_res_addr.target,
1217 error->last_func_vset_res_addr.lun);
1218
1219 ipr_err_separator;
1220
1221 array_entry = error->array_member;
1222
1223 for (i = 0; i < 18; i++) {
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001224 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 continue;
1226
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001227 if (be32_to_cpu(error->exposed_mode_adn) == i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 ipr_err("Exposed Array Member %d:\n", i);
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001229 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 ipr_err("Array Member %d:\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001232 ipr_log_vpd(&array_entry->vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001234 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1235 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1236 "Expected Location");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 ipr_err_separator;
1239
1240 if (i == 9)
1241 array_entry = error->array_member2;
1242 else
1243 array_entry++;
1244 }
1245}
1246
1247/**
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001248 * ipr_log_hex_data - Log additional hex IOA error data.
Brian Kingac719ab2006-11-21 10:28:42 -06001249 * @ioa_cfg: ioa config struct
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001250 * @data: IOA error data
1251 * @len: data length
1252 *
1253 * Return value:
1254 * none
1255 **/
Brian Kingac719ab2006-11-21 10:28:42 -06001256static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001257{
1258 int i;
1259
1260 if (len == 0)
1261 return;
1262
Brian Kingac719ab2006-11-21 10:28:42 -06001263 if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1264 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1265
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001266 for (i = 0; i < len / 4; i += 4) {
1267 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1268 be32_to_cpu(data[i]),
1269 be32_to_cpu(data[i+1]),
1270 be32_to_cpu(data[i+2]),
1271 be32_to_cpu(data[i+3]));
1272 }
1273}
1274
1275/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001276 * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1277 * @ioa_cfg: ioa config struct
1278 * @hostrcb: hostrcb struct
1279 *
1280 * Return value:
1281 * none
1282 **/
1283static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1284 struct ipr_hostrcb *hostrcb)
1285{
1286 struct ipr_hostrcb_type_17_error *error;
1287
1288 error = &hostrcb->hcam.u.error.u.type_17_error;
1289 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1290
1291 ipr_err("%s\n", error->failure_reason);
1292 ipr_err("Remote Adapter VPD:\n");
1293 ipr_log_ext_vpd(&error->vpd);
Brian Kingac719ab2006-11-21 10:28:42 -06001294 ipr_log_hex_data(ioa_cfg, error->data,
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001295 be32_to_cpu(hostrcb->hcam.length) -
1296 (offsetof(struct ipr_hostrcb_error, u) +
1297 offsetof(struct ipr_hostrcb_type_17_error, data)));
1298}
1299
1300/**
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001301 * ipr_log_dual_ioa_error - Log a dual adapter error.
1302 * @ioa_cfg: ioa config struct
1303 * @hostrcb: hostrcb struct
1304 *
1305 * Return value:
1306 * none
1307 **/
1308static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1309 struct ipr_hostrcb *hostrcb)
1310{
1311 struct ipr_hostrcb_type_07_error *error;
1312
1313 error = &hostrcb->hcam.u.error.u.type_07_error;
1314 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1315
1316 ipr_err("%s\n", error->failure_reason);
1317 ipr_err("Remote Adapter VPD:\n");
1318 ipr_log_vpd(&error->vpd);
Brian Kingac719ab2006-11-21 10:28:42 -06001319 ipr_log_hex_data(ioa_cfg, error->data,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001320 be32_to_cpu(hostrcb->hcam.length) -
1321 (offsetof(struct ipr_hostrcb_error, u) +
1322 offsetof(struct ipr_hostrcb_type_07_error, data)));
1323}
1324
Brian King49dc6a12006-11-21 10:28:35 -06001325static const struct {
1326 u8 active;
1327 char *desc;
1328} path_active_desc[] = {
1329 { IPR_PATH_NO_INFO, "Path" },
1330 { IPR_PATH_ACTIVE, "Active path" },
1331 { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1332};
1333
1334static const struct {
1335 u8 state;
1336 char *desc;
1337} path_state_desc[] = {
1338 { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1339 { IPR_PATH_HEALTHY, "is healthy" },
1340 { IPR_PATH_DEGRADED, "is degraded" },
1341 { IPR_PATH_FAILED, "is failed" }
1342};
1343
1344/**
1345 * ipr_log_fabric_path - Log a fabric path error
1346 * @hostrcb: hostrcb struct
1347 * @fabric: fabric descriptor
1348 *
1349 * Return value:
1350 * none
1351 **/
1352static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1353 struct ipr_hostrcb_fabric_desc *fabric)
1354{
1355 int i, j;
1356 u8 path_state = fabric->path_state;
1357 u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1358 u8 state = path_state & IPR_PATH_STATE_MASK;
1359
1360 for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1361 if (path_active_desc[i].active != active)
1362 continue;
1363
1364 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1365 if (path_state_desc[j].state != state)
1366 continue;
1367
1368 if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
1369 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
1370 path_active_desc[i].desc, path_state_desc[j].desc,
1371 fabric->ioa_port);
1372 } else if (fabric->cascaded_expander == 0xff) {
1373 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
1374 path_active_desc[i].desc, path_state_desc[j].desc,
1375 fabric->ioa_port, fabric->phy);
1376 } else if (fabric->phy == 0xff) {
1377 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
1378 path_active_desc[i].desc, path_state_desc[j].desc,
1379 fabric->ioa_port, fabric->cascaded_expander);
1380 } else {
1381 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
1382 path_active_desc[i].desc, path_state_desc[j].desc,
1383 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1384 }
1385 return;
1386 }
1387 }
1388
1389 ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
1390 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1391}
1392
1393static const struct {
1394 u8 type;
1395 char *desc;
1396} path_type_desc[] = {
1397 { IPR_PATH_CFG_IOA_PORT, "IOA port" },
1398 { IPR_PATH_CFG_EXP_PORT, "Expander port" },
1399 { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
1400 { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
1401};
1402
1403static const struct {
1404 u8 status;
1405 char *desc;
1406} path_status_desc[] = {
1407 { IPR_PATH_CFG_NO_PROB, "Functional" },
1408 { IPR_PATH_CFG_DEGRADED, "Degraded" },
1409 { IPR_PATH_CFG_FAILED, "Failed" },
1410 { IPR_PATH_CFG_SUSPECT, "Suspect" },
1411 { IPR_PATH_NOT_DETECTED, "Missing" },
1412 { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
1413};
1414
1415static const char *link_rate[] = {
1416 "unknown",
1417 "disabled",
1418 "phy reset problem",
1419 "spinup hold",
1420 "port selector",
1421 "unknown",
1422 "unknown",
1423 "unknown",
1424 "1.5Gbps",
1425 "3.0Gbps",
1426 "unknown",
1427 "unknown",
1428 "unknown",
1429 "unknown",
1430 "unknown",
1431 "unknown"
1432};
1433
1434/**
1435 * ipr_log_path_elem - Log a fabric path element.
1436 * @hostrcb: hostrcb struct
1437 * @cfg: fabric path element struct
1438 *
1439 * Return value:
1440 * none
1441 **/
1442static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
1443 struct ipr_hostrcb_config_element *cfg)
1444{
1445 int i, j;
1446 u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
1447 u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
1448
1449 if (type == IPR_PATH_CFG_NOT_EXIST)
1450 return;
1451
1452 for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
1453 if (path_type_desc[i].type != type)
1454 continue;
1455
1456 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
1457 if (path_status_desc[j].status != status)
1458 continue;
1459
1460 if (type == IPR_PATH_CFG_IOA_PORT) {
1461 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
1462 path_status_desc[j].desc, path_type_desc[i].desc,
1463 cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1464 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1465 } else {
1466 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
1467 ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
1468 path_status_desc[j].desc, path_type_desc[i].desc,
1469 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1470 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1471 } else if (cfg->cascaded_expander == 0xff) {
1472 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
1473 "WWN=%08X%08X\n", path_status_desc[j].desc,
1474 path_type_desc[i].desc, cfg->phy,
1475 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1476 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1477 } else if (cfg->phy == 0xff) {
1478 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
1479 "WWN=%08X%08X\n", path_status_desc[j].desc,
1480 path_type_desc[i].desc, cfg->cascaded_expander,
1481 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1482 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1483 } else {
1484 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
1485 "WWN=%08X%08X\n", path_status_desc[j].desc,
1486 path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
1487 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1488 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1489 }
1490 }
1491 return;
1492 }
1493 }
1494
1495 ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
1496 "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
1497 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1498 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1499}
1500
1501/**
1502 * ipr_log_fabric_error - Log a fabric error.
1503 * @ioa_cfg: ioa config struct
1504 * @hostrcb: hostrcb struct
1505 *
1506 * Return value:
1507 * none
1508 **/
1509static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
1510 struct ipr_hostrcb *hostrcb)
1511{
1512 struct ipr_hostrcb_type_20_error *error;
1513 struct ipr_hostrcb_fabric_desc *fabric;
1514 struct ipr_hostrcb_config_element *cfg;
1515 int i, add_len;
1516
1517 error = &hostrcb->hcam.u.error.u.type_20_error;
1518 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1519 ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
1520
1521 add_len = be32_to_cpu(hostrcb->hcam.length) -
1522 (offsetof(struct ipr_hostrcb_error, u) +
1523 offsetof(struct ipr_hostrcb_type_20_error, desc));
1524
1525 for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
1526 ipr_log_fabric_path(hostrcb, fabric);
1527 for_each_fabric_cfg(fabric, cfg)
1528 ipr_log_path_elem(hostrcb, cfg);
1529
1530 add_len -= be16_to_cpu(fabric->length);
1531 fabric = (struct ipr_hostrcb_fabric_desc *)
1532 ((unsigned long)fabric + be16_to_cpu(fabric->length));
1533 }
1534
Brian Kingac719ab2006-11-21 10:28:42 -06001535 ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
Brian King49dc6a12006-11-21 10:28:35 -06001536}
1537
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001538/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 * ipr_log_generic_error - Log an adapter error.
1540 * @ioa_cfg: ioa config struct
1541 * @hostrcb: hostrcb struct
1542 *
1543 * Return value:
1544 * none
1545 **/
1546static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1547 struct ipr_hostrcb *hostrcb)
1548{
Brian Kingac719ab2006-11-21 10:28:42 -06001549 ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001550 be32_to_cpu(hostrcb->hcam.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553/**
1554 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1555 * @ioasc: IOASC
1556 *
1557 * This function will return the index of into the ipr_error_table
1558 * for the specified IOASC. If the IOASC is not in the table,
1559 * 0 will be returned, which points to the entry used for unknown errors.
1560 *
1561 * Return value:
1562 * index into the ipr_error_table
1563 **/
1564static u32 ipr_get_error(u32 ioasc)
1565{
1566 int i;
1567
1568 for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
Brian King35a39692006-09-25 12:39:20 -05001569 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return i;
1571
1572 return 0;
1573}
1574
1575/**
1576 * ipr_handle_log_data - Log an adapter error.
1577 * @ioa_cfg: ioa config struct
1578 * @hostrcb: hostrcb struct
1579 *
1580 * This function logs an adapter error to the system.
1581 *
1582 * Return value:
1583 * none
1584 **/
1585static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1586 struct ipr_hostrcb *hostrcb)
1587{
1588 u32 ioasc;
1589 int error_index;
1590
1591 if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1592 return;
1593
1594 if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1595 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1596
1597 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1598
1599 if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1600 ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1601 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1602 scsi_report_bus_reset(ioa_cfg->host,
1603 hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1604 }
1605
1606 error_index = ipr_get_error(ioasc);
1607
1608 if (!ipr_error_table[error_index].log_hcam)
1609 return;
1610
Brian King49dc6a12006-11-21 10:28:35 -06001611 ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 /* Set indication we have logged an error */
1614 ioa_cfg->errors_logged++;
1615
Brian King933916f2007-03-29 12:43:30 -05001616 if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return;
brking@us.ibm.comcf852032005-11-01 17:00:47 -06001618 if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
1619 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 switch (hostrcb->hcam.overlay_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 case IPR_HOST_RCB_OVERLAY_ID_2:
1623 ipr_log_cache_error(ioa_cfg, hostrcb);
1624 break;
1625 case IPR_HOST_RCB_OVERLAY_ID_3:
1626 ipr_log_config_error(ioa_cfg, hostrcb);
1627 break;
1628 case IPR_HOST_RCB_OVERLAY_ID_4:
1629 case IPR_HOST_RCB_OVERLAY_ID_6:
1630 ipr_log_array_error(ioa_cfg, hostrcb);
1631 break;
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001632 case IPR_HOST_RCB_OVERLAY_ID_7:
1633 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
1634 break;
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001635 case IPR_HOST_RCB_OVERLAY_ID_12:
1636 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
1637 break;
1638 case IPR_HOST_RCB_OVERLAY_ID_13:
1639 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
1640 break;
1641 case IPR_HOST_RCB_OVERLAY_ID_14:
1642 case IPR_HOST_RCB_OVERLAY_ID_16:
1643 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
1644 break;
1645 case IPR_HOST_RCB_OVERLAY_ID_17:
1646 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
1647 break;
Brian King49dc6a12006-11-21 10:28:35 -06001648 case IPR_HOST_RCB_OVERLAY_ID_20:
1649 ipr_log_fabric_error(ioa_cfg, hostrcb);
1650 break;
brking@us.ibm.comcf852032005-11-01 17:00:47 -06001651 case IPR_HOST_RCB_OVERLAY_ID_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 default:
brking@us.ibm.coma9cfca92005-11-01 17:00:41 -06001654 ipr_log_generic_error(ioa_cfg, hostrcb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 break;
1656 }
1657}
1658
1659/**
1660 * ipr_process_error - Op done function for an adapter error log.
1661 * @ipr_cmd: ipr command struct
1662 *
1663 * This function is the op done function for an error log host
1664 * controlled async from the adapter. It will log the error and
1665 * send the HCAM back to the adapter.
1666 *
1667 * Return value:
1668 * none
1669 **/
1670static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1671{
1672 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1673 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1674 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
1675
1676 list_del(&hostrcb->queue);
1677 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1678
1679 if (!ioasc) {
1680 ipr_handle_log_data(ioa_cfg, hostrcb);
1681 } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1682 dev_err(&ioa_cfg->pdev->dev,
1683 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1684 }
1685
1686 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1687}
1688
1689/**
1690 * ipr_timeout - An internally generated op has timed out.
1691 * @ipr_cmd: ipr command struct
1692 *
1693 * This function blocks host requests and initiates an
1694 * adapter reset.
1695 *
1696 * Return value:
1697 * none
1698 **/
1699static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1700{
1701 unsigned long lock_flags = 0;
1702 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1703
1704 ENTER;
1705 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1706
1707 ioa_cfg->errors_logged++;
1708 dev_err(&ioa_cfg->pdev->dev,
1709 "Adapter being reset due to command timeout.\n");
1710
1711 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1712 ioa_cfg->sdt_state = GET_DUMP;
1713
1714 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1715 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1716
1717 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1718 LEAVE;
1719}
1720
1721/**
1722 * ipr_oper_timeout - Adapter timed out transitioning to operational
1723 * @ipr_cmd: ipr command struct
1724 *
1725 * This function blocks host requests and initiates an
1726 * adapter reset.
1727 *
1728 * Return value:
1729 * none
1730 **/
1731static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
1732{
1733 unsigned long lock_flags = 0;
1734 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1735
1736 ENTER;
1737 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1738
1739 ioa_cfg->errors_logged++;
1740 dev_err(&ioa_cfg->pdev->dev,
1741 "Adapter timed out transitioning to operational.\n");
1742
1743 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1744 ioa_cfg->sdt_state = GET_DUMP;
1745
1746 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
1747 if (ipr_fastfail)
1748 ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
1749 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1750 }
1751
1752 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1753 LEAVE;
1754}
1755
1756/**
1757 * ipr_reset_reload - Reset/Reload the IOA
1758 * @ioa_cfg: ioa config struct
1759 * @shutdown_type: shutdown type
1760 *
1761 * This function resets the adapter and re-initializes it.
1762 * This function assumes that all new host commands have been stopped.
1763 * Return value:
1764 * SUCCESS / FAILED
1765 **/
1766static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1767 enum ipr_shutdown_type shutdown_type)
1768{
1769 if (!ioa_cfg->in_reset_reload)
1770 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1771
1772 spin_unlock_irq(ioa_cfg->host->host_lock);
1773 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1774 spin_lock_irq(ioa_cfg->host->host_lock);
1775
1776 /* If we got hit with a host reset while we were already resetting
1777 the adapter for some reason, and the reset failed. */
1778 if (ioa_cfg->ioa_is_dead) {
1779 ipr_trace;
1780 return FAILED;
1781 }
1782
1783 return SUCCESS;
1784}
1785
1786/**
1787 * ipr_find_ses_entry - Find matching SES in SES table
1788 * @res: resource entry struct of SES
1789 *
1790 * Return value:
1791 * pointer to SES table entry / NULL on failure
1792 **/
1793static const struct ipr_ses_table_entry *
1794ipr_find_ses_entry(struct ipr_resource_entry *res)
1795{
1796 int i, j, matches;
1797 const struct ipr_ses_table_entry *ste = ipr_ses_table;
1798
1799 for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1800 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1801 if (ste->compare_product_id_byte[j] == 'X') {
1802 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1803 matches++;
1804 else
1805 break;
1806 } else
1807 matches++;
1808 }
1809
1810 if (matches == IPR_PROD_ID_LEN)
1811 return ste;
1812 }
1813
1814 return NULL;
1815}
1816
1817/**
1818 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1819 * @ioa_cfg: ioa config struct
1820 * @bus: SCSI bus
1821 * @bus_width: bus width
1822 *
1823 * Return value:
1824 * SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1825 * For a 2-byte wide SCSI bus, the maximum transfer speed is
1826 * twice the maximum transfer rate (e.g. for a wide enabled bus,
1827 * max 160MHz = max 320MB/sec).
1828 **/
1829static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1830{
1831 struct ipr_resource_entry *res;
1832 const struct ipr_ses_table_entry *ste;
1833 u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1834
1835 /* Loop through each config table entry in the config table buffer */
1836 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1837 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1838 continue;
1839
1840 if (bus != res->cfgte.res_addr.bus)
1841 continue;
1842
1843 if (!(ste = ipr_find_ses_entry(res)))
1844 continue;
1845
1846 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1847 }
1848
1849 return max_xfer_rate;
1850}
1851
1852/**
1853 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1854 * @ioa_cfg: ioa config struct
1855 * @max_delay: max delay in micro-seconds to wait
1856 *
1857 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1858 *
1859 * Return value:
1860 * 0 on success / other on failure
1861 **/
1862static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1863{
1864 volatile u32 pcii_reg;
1865 int delay = 1;
1866
1867 /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1868 while (delay < max_delay) {
1869 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1870
1871 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1872 return 0;
1873
1874 /* udelay cannot be used if delay is more than a few milliseconds */
1875 if ((delay / 1000) > MAX_UDELAY_MS)
1876 mdelay(delay / 1000);
1877 else
1878 udelay(delay);
1879
1880 delay += delay;
1881 }
1882 return -EIO;
1883}
1884
1885/**
1886 * ipr_get_ldump_data_section - Dump IOA memory
1887 * @ioa_cfg: ioa config struct
1888 * @start_addr: adapter address to dump
1889 * @dest: destination kernel buffer
1890 * @length_in_words: length to dump in 4 byte words
1891 *
1892 * Return value:
1893 * 0 on success / -EIO on failure
1894 **/
1895static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1896 u32 start_addr,
1897 __be32 *dest, u32 length_in_words)
1898{
1899 volatile u32 temp_pcii_reg;
1900 int i, delay = 0;
1901
1902 /* Write IOA interrupt reg starting LDUMP state */
1903 writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1904 ioa_cfg->regs.set_uproc_interrupt_reg);
1905
1906 /* Wait for IO debug acknowledge */
1907 if (ipr_wait_iodbg_ack(ioa_cfg,
1908 IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1909 dev_err(&ioa_cfg->pdev->dev,
1910 "IOA dump long data transfer timeout\n");
1911 return -EIO;
1912 }
1913
1914 /* Signal LDUMP interlocked - clear IO debug ack */
1915 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1916 ioa_cfg->regs.clr_interrupt_reg);
1917
1918 /* Write Mailbox with starting address */
1919 writel(start_addr, ioa_cfg->ioa_mailbox);
1920
1921 /* Signal address valid - clear IOA Reset alert */
1922 writel(IPR_UPROCI_RESET_ALERT,
1923 ioa_cfg->regs.clr_uproc_interrupt_reg);
1924
1925 for (i = 0; i < length_in_words; i++) {
1926 /* Wait for IO debug acknowledge */
1927 if (ipr_wait_iodbg_ack(ioa_cfg,
1928 IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1929 dev_err(&ioa_cfg->pdev->dev,
1930 "IOA dump short data transfer timeout\n");
1931 return -EIO;
1932 }
1933
1934 /* Read data from mailbox and increment destination pointer */
1935 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1936 dest++;
1937
1938 /* For all but the last word of data, signal data received */
1939 if (i < (length_in_words - 1)) {
1940 /* Signal dump data received - Clear IO debug Ack */
1941 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1942 ioa_cfg->regs.clr_interrupt_reg);
1943 }
1944 }
1945
1946 /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1947 writel(IPR_UPROCI_RESET_ALERT,
1948 ioa_cfg->regs.set_uproc_interrupt_reg);
1949
1950 writel(IPR_UPROCI_IO_DEBUG_ALERT,
1951 ioa_cfg->regs.clr_uproc_interrupt_reg);
1952
1953 /* Signal dump data received - Clear IO debug Ack */
1954 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1955 ioa_cfg->regs.clr_interrupt_reg);
1956
1957 /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
1958 while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
1959 temp_pcii_reg =
1960 readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
1961
1962 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
1963 return 0;
1964
1965 udelay(10);
1966 delay += 10;
1967 }
1968
1969 return 0;
1970}
1971
1972#ifdef CONFIG_SCSI_IPR_DUMP
1973/**
1974 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
1975 * @ioa_cfg: ioa config struct
1976 * @pci_address: adapter address
1977 * @length: length of data to copy
1978 *
1979 * Copy data from PCI adapter to kernel buffer.
1980 * Note: length MUST be a 4 byte multiple
1981 * Return value:
1982 * 0 on success / other on failure
1983 **/
1984static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
1985 unsigned long pci_address, u32 length)
1986{
1987 int bytes_copied = 0;
1988 int cur_len, rc, rem_len, rem_page_len;
1989 __be32 *page;
1990 unsigned long lock_flags = 0;
1991 struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
1992
1993 while (bytes_copied < length &&
1994 (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
1995 if (ioa_dump->page_offset >= PAGE_SIZE ||
1996 ioa_dump->page_offset == 0) {
1997 page = (__be32 *)__get_free_page(GFP_ATOMIC);
1998
1999 if (!page) {
2000 ipr_trace;
2001 return bytes_copied;
2002 }
2003
2004 ioa_dump->page_offset = 0;
2005 ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2006 ioa_dump->next_page_index++;
2007 } else
2008 page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2009
2010 rem_len = length - bytes_copied;
2011 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2012 cur_len = min(rem_len, rem_page_len);
2013
2014 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2015 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2016 rc = -EIO;
2017 } else {
2018 rc = ipr_get_ldump_data_section(ioa_cfg,
2019 pci_address + bytes_copied,
2020 &page[ioa_dump->page_offset / 4],
2021 (cur_len / sizeof(u32)));
2022 }
2023 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2024
2025 if (!rc) {
2026 ioa_dump->page_offset += cur_len;
2027 bytes_copied += cur_len;
2028 } else {
2029 ipr_trace;
2030 break;
2031 }
2032 schedule();
2033 }
2034
2035 return bytes_copied;
2036}
2037
2038/**
2039 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2040 * @hdr: dump entry header struct
2041 *
2042 * Return value:
2043 * nothing
2044 **/
2045static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2046{
2047 hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2048 hdr->num_elems = 1;
2049 hdr->offset = sizeof(*hdr);
2050 hdr->status = IPR_DUMP_STATUS_SUCCESS;
2051}
2052
2053/**
2054 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2055 * @ioa_cfg: ioa config struct
2056 * @driver_dump: driver dump struct
2057 *
2058 * Return value:
2059 * nothing
2060 **/
2061static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2062 struct ipr_driver_dump *driver_dump)
2063{
2064 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2065
2066 ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2067 driver_dump->ioa_type_entry.hdr.len =
2068 sizeof(struct ipr_dump_ioa_type_entry) -
2069 sizeof(struct ipr_dump_entry_header);
2070 driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2071 driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2072 driver_dump->ioa_type_entry.type = ioa_cfg->type;
2073 driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2074 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2075 ucode_vpd->minor_release[1];
2076 driver_dump->hdr.num_entries++;
2077}
2078
2079/**
2080 * ipr_dump_version_data - Fill in the driver version in the dump.
2081 * @ioa_cfg: ioa config struct
2082 * @driver_dump: driver dump struct
2083 *
2084 * Return value:
2085 * nothing
2086 **/
2087static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2088 struct ipr_driver_dump *driver_dump)
2089{
2090 ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2091 driver_dump->version_entry.hdr.len =
2092 sizeof(struct ipr_dump_version_entry) -
2093 sizeof(struct ipr_dump_entry_header);
2094 driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2095 driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2096 strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2097 driver_dump->hdr.num_entries++;
2098}
2099
2100/**
2101 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2102 * @ioa_cfg: ioa config struct
2103 * @driver_dump: driver dump struct
2104 *
2105 * Return value:
2106 * nothing
2107 **/
2108static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
2109 struct ipr_driver_dump *driver_dump)
2110{
2111 ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
2112 driver_dump->trace_entry.hdr.len =
2113 sizeof(struct ipr_dump_trace_entry) -
2114 sizeof(struct ipr_dump_entry_header);
2115 driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2116 driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
2117 memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
2118 driver_dump->hdr.num_entries++;
2119}
2120
2121/**
2122 * ipr_dump_location_data - Fill in the IOA location in the dump.
2123 * @ioa_cfg: ioa config struct
2124 * @driver_dump: driver dump struct
2125 *
2126 * Return value:
2127 * nothing
2128 **/
2129static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
2130 struct ipr_driver_dump *driver_dump)
2131{
2132 ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
2133 driver_dump->location_entry.hdr.len =
2134 sizeof(struct ipr_dump_location_entry) -
2135 sizeof(struct ipr_dump_entry_header);
2136 driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2137 driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
2138 strcpy(driver_dump->location_entry.location, ioa_cfg->pdev->dev.bus_id);
2139 driver_dump->hdr.num_entries++;
2140}
2141
2142/**
2143 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
2144 * @ioa_cfg: ioa config struct
2145 * @dump: dump struct
2146 *
2147 * Return value:
2148 * nothing
2149 **/
2150static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
2151{
2152 unsigned long start_addr, sdt_word;
2153 unsigned long lock_flags = 0;
2154 struct ipr_driver_dump *driver_dump = &dump->driver_dump;
2155 struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
2156 u32 num_entries, start_off, end_off;
2157 u32 bytes_to_copy, bytes_copied, rc;
2158 struct ipr_sdt *sdt;
2159 int i;
2160
2161 ENTER;
2162
2163 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2164
2165 if (ioa_cfg->sdt_state != GET_DUMP) {
2166 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2167 return;
2168 }
2169
2170 start_addr = readl(ioa_cfg->ioa_mailbox);
2171
2172 if (!ipr_sdt_is_fmt2(start_addr)) {
2173 dev_err(&ioa_cfg->pdev->dev,
2174 "Invalid dump table format: %lx\n", start_addr);
2175 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2176 return;
2177 }
2178
2179 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
2180
2181 driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
2182
2183 /* Initialize the overall dump header */
2184 driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
2185 driver_dump->hdr.num_entries = 1;
2186 driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
2187 driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
2188 driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
2189 driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
2190
2191 ipr_dump_version_data(ioa_cfg, driver_dump);
2192 ipr_dump_location_data(ioa_cfg, driver_dump);
2193 ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
2194 ipr_dump_trace_data(ioa_cfg, driver_dump);
2195
2196 /* Update dump_header */
2197 driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
2198
2199 /* IOA Dump entry */
2200 ipr_init_dump_entry_hdr(&ioa_dump->hdr);
2201 ioa_dump->format = IPR_SDT_FMT2;
2202 ioa_dump->hdr.len = 0;
2203 ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2204 ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
2205
2206 /* First entries in sdt are actually a list of dump addresses and
2207 lengths to gather the real dump data. sdt represents the pointer
2208 to the ioa generated dump table. Dump data will be extracted based
2209 on entries in this table */
2210 sdt = &ioa_dump->sdt;
2211
2212 rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
2213 sizeof(struct ipr_sdt) / sizeof(__be32));
2214
2215 /* Smart Dump table is ready to use and the first entry is valid */
2216 if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
2217 dev_err(&ioa_cfg->pdev->dev,
2218 "Dump of IOA failed. Dump table not valid: %d, %X.\n",
2219 rc, be32_to_cpu(sdt->hdr.state));
2220 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
2221 ioa_cfg->sdt_state = DUMP_OBTAINED;
2222 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2223 return;
2224 }
2225
2226 num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
2227
2228 if (num_entries > IPR_NUM_SDT_ENTRIES)
2229 num_entries = IPR_NUM_SDT_ENTRIES;
2230
2231 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2232
2233 for (i = 0; i < num_entries; i++) {
2234 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
2235 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2236 break;
2237 }
2238
2239 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
2240 sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
2241 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
2242 end_off = be32_to_cpu(sdt->entry[i].end_offset);
2243
2244 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
2245 bytes_to_copy = end_off - start_off;
2246 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
2247 sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
2248 continue;
2249 }
2250
2251 /* Copy data from adapter to driver buffers */
2252 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
2253 bytes_to_copy);
2254
2255 ioa_dump->hdr.len += bytes_copied;
2256
2257 if (bytes_copied != bytes_to_copy) {
2258 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2259 break;
2260 }
2261 }
2262 }
2263 }
2264
2265 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
2266
2267 /* Update dump_header */
2268 driver_dump->hdr.len += ioa_dump->hdr.len;
2269 wmb();
2270 ioa_cfg->sdt_state = DUMP_OBTAINED;
2271 LEAVE;
2272}
2273
2274#else
2275#define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
2276#endif
2277
2278/**
2279 * ipr_release_dump - Free adapter dump memory
2280 * @kref: kref struct
2281 *
2282 * Return value:
2283 * nothing
2284 **/
2285static void ipr_release_dump(struct kref *kref)
2286{
2287 struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
2288 struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
2289 unsigned long lock_flags = 0;
2290 int i;
2291
2292 ENTER;
2293 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2294 ioa_cfg->dump = NULL;
2295 ioa_cfg->sdt_state = INACTIVE;
2296 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2297
2298 for (i = 0; i < dump->ioa_dump.next_page_index; i++)
2299 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
2300
2301 kfree(dump);
2302 LEAVE;
2303}
2304
2305/**
2306 * ipr_worker_thread - Worker thread
David Howellsc4028952006-11-22 14:57:56 +00002307 * @work: ioa config struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 *
2309 * Called at task level from a work thread. This function takes care
2310 * of adding and removing device from the mid-layer as configuration
2311 * changes are detected by the adapter.
2312 *
2313 * Return value:
2314 * nothing
2315 **/
David Howellsc4028952006-11-22 14:57:56 +00002316static void ipr_worker_thread(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 unsigned long lock_flags;
2319 struct ipr_resource_entry *res;
2320 struct scsi_device *sdev;
2321 struct ipr_dump *dump;
David Howellsc4028952006-11-22 14:57:56 +00002322 struct ipr_ioa_cfg *ioa_cfg =
2323 container_of(work, struct ipr_ioa_cfg, work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 u8 bus, target, lun;
2325 int did_work;
2326
2327 ENTER;
2328 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2329
2330 if (ioa_cfg->sdt_state == GET_DUMP) {
2331 dump = ioa_cfg->dump;
2332 if (!dump) {
2333 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2334 return;
2335 }
2336 kref_get(&dump->kref);
2337 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2338 ipr_get_ioa_dump(ioa_cfg, dump);
2339 kref_put(&dump->kref, ipr_release_dump);
2340
2341 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2342 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
2343 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2344 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2345 return;
2346 }
2347
2348restart:
2349 do {
2350 did_work = 0;
2351 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
2352 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2353 return;
2354 }
2355
2356 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2357 if (res->del_from_ml && res->sdev) {
2358 did_work = 1;
2359 sdev = res->sdev;
2360 if (!scsi_device_get(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
2362 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2363 scsi_remove_device(sdev);
2364 scsi_device_put(sdev);
2365 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2366 }
2367 break;
2368 }
2369 }
2370 } while(did_work);
2371
2372 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2373 if (res->add_to_ml) {
2374 bus = res->cfgte.res_addr.bus;
2375 target = res->cfgte.res_addr.target;
2376 lun = res->cfgte.res_addr.lun;
Brian King1121b792006-03-29 09:37:16 -06002377 res->add_to_ml = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2379 scsi_add_device(ioa_cfg->host, bus, target, lun);
2380 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2381 goto restart;
2382 }
2383 }
2384
2385 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
Kay Sievers312c0042005-11-16 09:00:00 +01002386 kobject_uevent(&ioa_cfg->host->shost_classdev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 LEAVE;
2388}
2389
2390#ifdef CONFIG_SCSI_IPR_TRACE
2391/**
2392 * ipr_read_trace - Dump the adapter trace
2393 * @kobj: kobject struct
2394 * @buf: buffer
2395 * @off: offset
2396 * @count: buffer size
2397 *
2398 * Return value:
2399 * number of bytes printed to buffer
2400 **/
2401static ssize_t ipr_read_trace(struct kobject *kobj, char *buf,
2402 loff_t off, size_t count)
2403{
2404 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2405 struct Scsi_Host *shost = class_to_shost(cdev);
2406 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2407 unsigned long lock_flags = 0;
2408 int size = IPR_TRACE_SIZE;
2409 char *src = (char *)ioa_cfg->trace;
2410
2411 if (off > size)
2412 return 0;
2413 if (off + count > size) {
2414 size -= off;
2415 count = size;
2416 }
2417
2418 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2419 memcpy(buf, &src[off], count);
2420 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2421 return count;
2422}
2423
2424static struct bin_attribute ipr_trace_attr = {
2425 .attr = {
2426 .name = "trace",
2427 .mode = S_IRUGO,
2428 },
2429 .size = 0,
2430 .read = ipr_read_trace,
2431};
2432#endif
2433
brking@us.ibm.com62275042005-11-01 17:01:14 -06002434static const struct {
2435 enum ipr_cache_state state;
2436 char *name;
2437} cache_state [] = {
2438 { CACHE_NONE, "none" },
2439 { CACHE_DISABLED, "disabled" },
2440 { CACHE_ENABLED, "enabled" }
2441};
2442
2443/**
2444 * ipr_show_write_caching - Show the write caching attribute
2445 * @class_dev: class device struct
2446 * @buf: buffer
2447 *
2448 * Return value:
2449 * number of bytes printed to buffer
2450 **/
2451static ssize_t ipr_show_write_caching(struct class_device *class_dev, char *buf)
2452{
2453 struct Scsi_Host *shost = class_to_shost(class_dev);
2454 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2455 unsigned long lock_flags = 0;
2456 int i, len = 0;
2457
2458 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2459 for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2460 if (cache_state[i].state == ioa_cfg->cache_state) {
2461 len = snprintf(buf, PAGE_SIZE, "%s\n", cache_state[i].name);
2462 break;
2463 }
2464 }
2465 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2466 return len;
2467}
2468
2469
2470/**
2471 * ipr_store_write_caching - Enable/disable adapter write cache
2472 * @class_dev: class_device struct
2473 * @buf: buffer
2474 * @count: buffer size
2475 *
2476 * This function will enable/disable adapter write cache.
2477 *
2478 * Return value:
2479 * count on success / other on failure
2480 **/
2481static ssize_t ipr_store_write_caching(struct class_device *class_dev,
2482 const char *buf, size_t count)
2483{
2484 struct Scsi_Host *shost = class_to_shost(class_dev);
2485 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2486 unsigned long lock_flags = 0;
2487 enum ipr_cache_state new_state = CACHE_INVALID;
2488 int i;
2489
2490 if (!capable(CAP_SYS_ADMIN))
2491 return -EACCES;
2492 if (ioa_cfg->cache_state == CACHE_NONE)
2493 return -EINVAL;
2494
2495 for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2496 if (!strncmp(cache_state[i].name, buf, strlen(cache_state[i].name))) {
2497 new_state = cache_state[i].state;
2498 break;
2499 }
2500 }
2501
2502 if (new_state != CACHE_DISABLED && new_state != CACHE_ENABLED)
2503 return -EINVAL;
2504
2505 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2506 if (ioa_cfg->cache_state == new_state) {
2507 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2508 return count;
2509 }
2510
2511 ioa_cfg->cache_state = new_state;
2512 dev_info(&ioa_cfg->pdev->dev, "%s adapter write cache.\n",
2513 new_state == CACHE_ENABLED ? "Enabling" : "Disabling");
2514 if (!ioa_cfg->in_reset_reload)
2515 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2516 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2517 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2518
2519 return count;
2520}
2521
2522static struct class_device_attribute ipr_ioa_cache_attr = {
2523 .attr = {
2524 .name = "write_cache",
2525 .mode = S_IRUGO | S_IWUSR,
2526 },
2527 .show = ipr_show_write_caching,
2528 .store = ipr_store_write_caching
2529};
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531/**
2532 * ipr_show_fw_version - Show the firmware version
2533 * @class_dev: class device struct
2534 * @buf: buffer
2535 *
2536 * Return value:
2537 * number of bytes printed to buffer
2538 **/
2539static ssize_t ipr_show_fw_version(struct class_device *class_dev, char *buf)
2540{
2541 struct Scsi_Host *shost = class_to_shost(class_dev);
2542 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2543 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2544 unsigned long lock_flags = 0;
2545 int len;
2546
2547 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2548 len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
2549 ucode_vpd->major_release, ucode_vpd->card_type,
2550 ucode_vpd->minor_release[0],
2551 ucode_vpd->minor_release[1]);
2552 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2553 return len;
2554}
2555
2556static struct class_device_attribute ipr_fw_version_attr = {
2557 .attr = {
2558 .name = "fw_version",
2559 .mode = S_IRUGO,
2560 },
2561 .show = ipr_show_fw_version,
2562};
2563
2564/**
2565 * ipr_show_log_level - Show the adapter's error logging level
2566 * @class_dev: class device struct
2567 * @buf: buffer
2568 *
2569 * Return value:
2570 * number of bytes printed to buffer
2571 **/
2572static ssize_t ipr_show_log_level(struct class_device *class_dev, char *buf)
2573{
2574 struct Scsi_Host *shost = class_to_shost(class_dev);
2575 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2576 unsigned long lock_flags = 0;
2577 int len;
2578
2579 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2580 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
2581 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2582 return len;
2583}
2584
2585/**
2586 * ipr_store_log_level - Change the adapter's error logging level
2587 * @class_dev: class device struct
2588 * @buf: buffer
2589 *
2590 * Return value:
2591 * number of bytes printed to buffer
2592 **/
2593static ssize_t ipr_store_log_level(struct class_device *class_dev,
2594 const char *buf, size_t count)
2595{
2596 struct Scsi_Host *shost = class_to_shost(class_dev);
2597 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2598 unsigned long lock_flags = 0;
2599
2600 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2601 ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
2602 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2603 return strlen(buf);
2604}
2605
2606static struct class_device_attribute ipr_log_level_attr = {
2607 .attr = {
2608 .name = "log_level",
2609 .mode = S_IRUGO | S_IWUSR,
2610 },
2611 .show = ipr_show_log_level,
2612 .store = ipr_store_log_level
2613};
2614
2615/**
2616 * ipr_store_diagnostics - IOA Diagnostics interface
2617 * @class_dev: class_device struct
2618 * @buf: buffer
2619 * @count: buffer size
2620 *
2621 * This function will reset the adapter and wait a reasonable
2622 * amount of time for any errors that the adapter might log.
2623 *
2624 * Return value:
2625 * count on success / other on failure
2626 **/
2627static ssize_t ipr_store_diagnostics(struct class_device *class_dev,
2628 const char *buf, size_t count)
2629{
2630 struct Scsi_Host *shost = class_to_shost(class_dev);
2631 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2632 unsigned long lock_flags = 0;
2633 int rc = count;
2634
2635 if (!capable(CAP_SYS_ADMIN))
2636 return -EACCES;
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05002639 while(ioa_cfg->in_reset_reload) {
2640 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2641 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2642 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2643 }
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 ioa_cfg->errors_logged = 0;
2646 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2647
2648 if (ioa_cfg->in_reset_reload) {
2649 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2650 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2651
2652 /* Wait for a second for any errors to be logged */
2653 msleep(1000);
2654 } else {
2655 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2656 return -EIO;
2657 }
2658
2659 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2660 if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2661 rc = -EIO;
2662 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2663
2664 return rc;
2665}
2666
2667static struct class_device_attribute ipr_diagnostics_attr = {
2668 .attr = {
2669 .name = "run_diagnostics",
2670 .mode = S_IWUSR,
2671 },
2672 .store = ipr_store_diagnostics
2673};
2674
2675/**
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002676 * ipr_show_adapter_state - Show the adapter's state
2677 * @class_dev: class device struct
2678 * @buf: buffer
2679 *
2680 * Return value:
2681 * number of bytes printed to buffer
2682 **/
2683static ssize_t ipr_show_adapter_state(struct class_device *class_dev, char *buf)
2684{
2685 struct Scsi_Host *shost = class_to_shost(class_dev);
2686 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2687 unsigned long lock_flags = 0;
2688 int len;
2689
2690 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2691 if (ioa_cfg->ioa_is_dead)
2692 len = snprintf(buf, PAGE_SIZE, "offline\n");
2693 else
2694 len = snprintf(buf, PAGE_SIZE, "online\n");
2695 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2696 return len;
2697}
2698
2699/**
2700 * ipr_store_adapter_state - Change adapter state
2701 * @class_dev: class_device struct
2702 * @buf: buffer
2703 * @count: buffer size
2704 *
2705 * This function will change the adapter's state.
2706 *
2707 * Return value:
2708 * count on success / other on failure
2709 **/
2710static ssize_t ipr_store_adapter_state(struct class_device *class_dev,
2711 const char *buf, size_t count)
2712{
2713 struct Scsi_Host *shost = class_to_shost(class_dev);
2714 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2715 unsigned long lock_flags;
2716 int result = count;
2717
2718 if (!capable(CAP_SYS_ADMIN))
2719 return -EACCES;
2720
2721 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2722 if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
2723 ioa_cfg->ioa_is_dead = 0;
2724 ioa_cfg->reset_retries = 0;
2725 ioa_cfg->in_ioa_bringdown = 0;
2726 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2727 }
2728 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2729 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2730
2731 return result;
2732}
2733
2734static struct class_device_attribute ipr_ioa_state_attr = {
2735 .attr = {
2736 .name = "state",
2737 .mode = S_IRUGO | S_IWUSR,
2738 },
2739 .show = ipr_show_adapter_state,
2740 .store = ipr_store_adapter_state
2741};
2742
2743/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 * ipr_store_reset_adapter - Reset the adapter
2745 * @class_dev: class_device struct
2746 * @buf: buffer
2747 * @count: buffer size
2748 *
2749 * This function will reset the adapter.
2750 *
2751 * Return value:
2752 * count on success / other on failure
2753 **/
2754static ssize_t ipr_store_reset_adapter(struct class_device *class_dev,
2755 const char *buf, size_t count)
2756{
2757 struct Scsi_Host *shost = class_to_shost(class_dev);
2758 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2759 unsigned long lock_flags;
2760 int result = count;
2761
2762 if (!capable(CAP_SYS_ADMIN))
2763 return -EACCES;
2764
2765 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2766 if (!ioa_cfg->in_reset_reload)
2767 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2768 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2769 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2770
2771 return result;
2772}
2773
2774static struct class_device_attribute ipr_ioa_reset_attr = {
2775 .attr = {
2776 .name = "reset_host",
2777 .mode = S_IWUSR,
2778 },
2779 .store = ipr_store_reset_adapter
2780};
2781
2782/**
2783 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2784 * @buf_len: buffer length
2785 *
2786 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2787 * list to use for microcode download
2788 *
2789 * Return value:
2790 * pointer to sglist / NULL on failure
2791 **/
2792static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2793{
2794 int sg_size, order, bsize_elem, num_elem, i, j;
2795 struct ipr_sglist *sglist;
2796 struct scatterlist *scatterlist;
2797 struct page *page;
2798
2799 /* Get the minimum size per scatter/gather element */
2800 sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2801
2802 /* Get the actual size per element */
2803 order = get_order(sg_size);
2804
2805 /* Determine the actual number of bytes per element */
2806 bsize_elem = PAGE_SIZE * (1 << order);
2807
2808 /* Determine the actual number of sg entries needed */
2809 if (buf_len % bsize_elem)
2810 num_elem = (buf_len / bsize_elem) + 1;
2811 else
2812 num_elem = buf_len / bsize_elem;
2813
2814 /* Allocate a scatter/gather list for the DMA */
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06002815 sglist = kzalloc(sizeof(struct ipr_sglist) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 (sizeof(struct scatterlist) * (num_elem - 1)),
2817 GFP_KERNEL);
2818
2819 if (sglist == NULL) {
2820 ipr_trace;
2821 return NULL;
2822 }
2823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 scatterlist = sglist->scatterlist;
2825
2826 sglist->order = order;
2827 sglist->num_sg = num_elem;
2828
2829 /* Allocate a bunch of sg elements */
2830 for (i = 0; i < num_elem; i++) {
2831 page = alloc_pages(GFP_KERNEL, order);
2832 if (!page) {
2833 ipr_trace;
2834
2835 /* Free up what we already allocated */
2836 for (j = i - 1; j >= 0; j--)
2837 __free_pages(scatterlist[j].page, order);
2838 kfree(sglist);
2839 return NULL;
2840 }
2841
2842 scatterlist[i].page = page;
2843 }
2844
2845 return sglist;
2846}
2847
2848/**
2849 * ipr_free_ucode_buffer - Frees a microcode download buffer
2850 * @p_dnld: scatter/gather list pointer
2851 *
2852 * Free a DMA'able ucode download buffer previously allocated with
2853 * ipr_alloc_ucode_buffer
2854 *
2855 * Return value:
2856 * nothing
2857 **/
2858static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2859{
2860 int i;
2861
2862 for (i = 0; i < sglist->num_sg; i++)
2863 __free_pages(sglist->scatterlist[i].page, sglist->order);
2864
2865 kfree(sglist);
2866}
2867
2868/**
2869 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2870 * @sglist: scatter/gather list pointer
2871 * @buffer: buffer pointer
2872 * @len: buffer length
2873 *
2874 * Copy a microcode image from a user buffer into a buffer allocated by
2875 * ipr_alloc_ucode_buffer
2876 *
2877 * Return value:
2878 * 0 on success / other on failure
2879 **/
2880static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2881 u8 *buffer, u32 len)
2882{
2883 int bsize_elem, i, result = 0;
2884 struct scatterlist *scatterlist;
2885 void *kaddr;
2886
2887 /* Determine the actual number of bytes per element */
2888 bsize_elem = PAGE_SIZE * (1 << sglist->order);
2889
2890 scatterlist = sglist->scatterlist;
2891
2892 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
2893 kaddr = kmap(scatterlist[i].page);
2894 memcpy(kaddr, buffer, bsize_elem);
2895 kunmap(scatterlist[i].page);
2896
2897 scatterlist[i].length = bsize_elem;
2898
2899 if (result != 0) {
2900 ipr_trace;
2901 return result;
2902 }
2903 }
2904
2905 if (len % bsize_elem) {
2906 kaddr = kmap(scatterlist[i].page);
2907 memcpy(kaddr, buffer, len % bsize_elem);
2908 kunmap(scatterlist[i].page);
2909
2910 scatterlist[i].length = len % bsize_elem;
2911 }
2912
2913 sglist->buffer_len = len;
2914 return result;
2915}
2916
2917/**
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002918 * ipr_build_ucode_ioadl - Build a microcode download IOADL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 * @ipr_cmd: ipr command struct
2920 * @sglist: scatter/gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 *
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002922 * Builds a microcode download IOA data list (IOADL).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 **/
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002925static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
2926 struct ipr_sglist *sglist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2929 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2930 struct scatterlist *scatterlist = sglist->scatterlist;
2931 int i;
2932
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002933 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002935 ioarcb->write_data_transfer_length = cpu_to_be32(sglist->buffer_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 ioarcb->write_ioadl_len =
2937 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
2938
2939 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
2940 ioadl[i].flags_and_data_len =
2941 cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
2942 ioadl[i].address =
2943 cpu_to_be32(sg_dma_address(&scatterlist[i]));
2944 }
2945
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002946 ioadl[i-1].flags_and_data_len |=
2947 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
2948}
2949
2950/**
2951 * ipr_update_ioa_ucode - Update IOA's microcode
2952 * @ioa_cfg: ioa config struct
2953 * @sglist: scatter/gather list
2954 *
2955 * Initiate an adapter reset to update the IOA's microcode
2956 *
2957 * Return value:
2958 * 0 on success / -EIO on failure
2959 **/
2960static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
2961 struct ipr_sglist *sglist)
2962{
2963 unsigned long lock_flags;
2964
2965 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05002966 while(ioa_cfg->in_reset_reload) {
2967 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2968 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2969 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2970 }
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002971
2972 if (ioa_cfg->ucode_sglist) {
2973 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2974 dev_err(&ioa_cfg->pdev->dev,
2975 "Microcode download already in progress\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return -EIO;
2977 }
2978
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002979 sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
2980 sglist->num_sg, DMA_TO_DEVICE);
2981
2982 if (!sglist->num_dma_sg) {
2983 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2984 dev_err(&ioa_cfg->pdev->dev,
2985 "Failed to map microcode download buffer!\n");
2986 return -EIO;
2987 }
2988
2989 ioa_cfg->ucode_sglist = sglist;
2990 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2991 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2992 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2993
2994 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2995 ioa_cfg->ucode_sglist = NULL;
2996 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return 0;
2998}
2999
3000/**
3001 * ipr_store_update_fw - Update the firmware on the adapter
3002 * @class_dev: class_device struct
3003 * @buf: buffer
3004 * @count: buffer size
3005 *
3006 * This function will update the firmware on the adapter.
3007 *
3008 * Return value:
3009 * count on success / other on failure
3010 **/
3011static ssize_t ipr_store_update_fw(struct class_device *class_dev,
3012 const char *buf, size_t count)
3013{
3014 struct Scsi_Host *shost = class_to_shost(class_dev);
3015 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3016 struct ipr_ucode_image_header *image_hdr;
3017 const struct firmware *fw_entry;
3018 struct ipr_sglist *sglist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 char fname[100];
3020 char *src;
3021 int len, result, dnld_size;
3022
3023 if (!capable(CAP_SYS_ADMIN))
3024 return -EACCES;
3025
3026 len = snprintf(fname, 99, "%s", buf);
3027 fname[len-1] = '\0';
3028
3029 if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3030 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3031 return -EIO;
3032 }
3033
3034 image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3035
3036 if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
3037 (ioa_cfg->vpd_cbs->page3_data.card_type &&
3038 ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
3039 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
3040 release_firmware(fw_entry);
3041 return -EINVAL;
3042 }
3043
3044 src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3045 dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3046 sglist = ipr_alloc_ucode_buffer(dnld_size);
3047
3048 if (!sglist) {
3049 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3050 release_firmware(fw_entry);
3051 return -ENOMEM;
3052 }
3053
3054 result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3055
3056 if (result) {
3057 dev_err(&ioa_cfg->pdev->dev,
3058 "Microcode buffer copy to DMA buffer failed\n");
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003059 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
3061
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003062 result = ipr_update_ioa_ucode(ioa_cfg, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003064 if (!result)
3065 result = count;
3066out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 ipr_free_ucode_buffer(sglist);
3068 release_firmware(fw_entry);
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003069 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070}
3071
3072static struct class_device_attribute ipr_update_fw_attr = {
3073 .attr = {
3074 .name = "update_fw",
3075 .mode = S_IWUSR,
3076 },
3077 .store = ipr_store_update_fw
3078};
3079
3080static struct class_device_attribute *ipr_ioa_attrs[] = {
3081 &ipr_fw_version_attr,
3082 &ipr_log_level_attr,
3083 &ipr_diagnostics_attr,
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003084 &ipr_ioa_state_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 &ipr_ioa_reset_attr,
3086 &ipr_update_fw_attr,
brking@us.ibm.com62275042005-11-01 17:01:14 -06003087 &ipr_ioa_cache_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 NULL,
3089};
3090
3091#ifdef CONFIG_SCSI_IPR_DUMP
3092/**
3093 * ipr_read_dump - Dump the adapter
3094 * @kobj: kobject struct
3095 * @buf: buffer
3096 * @off: offset
3097 * @count: buffer size
3098 *
3099 * Return value:
3100 * number of bytes printed to buffer
3101 **/
3102static ssize_t ipr_read_dump(struct kobject *kobj, char *buf,
3103 loff_t off, size_t count)
3104{
3105 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
3106 struct Scsi_Host *shost = class_to_shost(cdev);
3107 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3108 struct ipr_dump *dump;
3109 unsigned long lock_flags = 0;
3110 char *src;
3111 int len;
3112 size_t rc = count;
3113
3114 if (!capable(CAP_SYS_ADMIN))
3115 return -EACCES;
3116
3117 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3118 dump = ioa_cfg->dump;
3119
3120 if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
3121 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3122 return 0;
3123 }
3124 kref_get(&dump->kref);
3125 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3126
3127 if (off > dump->driver_dump.hdr.len) {
3128 kref_put(&dump->kref, ipr_release_dump);
3129 return 0;
3130 }
3131
3132 if (off + count > dump->driver_dump.hdr.len) {
3133 count = dump->driver_dump.hdr.len - off;
3134 rc = count;
3135 }
3136
3137 if (count && off < sizeof(dump->driver_dump)) {
3138 if (off + count > sizeof(dump->driver_dump))
3139 len = sizeof(dump->driver_dump) - off;
3140 else
3141 len = count;
3142 src = (u8 *)&dump->driver_dump + off;
3143 memcpy(buf, src, len);
3144 buf += len;
3145 off += len;
3146 count -= len;
3147 }
3148
3149 off -= sizeof(dump->driver_dump);
3150
3151 if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
3152 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
3153 len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
3154 else
3155 len = count;
3156 src = (u8 *)&dump->ioa_dump + off;
3157 memcpy(buf, src, len);
3158 buf += len;
3159 off += len;
3160 count -= len;
3161 }
3162
3163 off -= offsetof(struct ipr_ioa_dump, ioa_data);
3164
3165 while (count) {
3166 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
3167 len = PAGE_ALIGN(off) - off;
3168 else
3169 len = count;
3170 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
3171 src += off & ~PAGE_MASK;
3172 memcpy(buf, src, len);
3173 buf += len;
3174 off += len;
3175 count -= len;
3176 }
3177
3178 kref_put(&dump->kref, ipr_release_dump);
3179 return rc;
3180}
3181
3182/**
3183 * ipr_alloc_dump - Prepare for adapter dump
3184 * @ioa_cfg: ioa config struct
3185 *
3186 * Return value:
3187 * 0 on success / other on failure
3188 **/
3189static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
3190{
3191 struct ipr_dump *dump;
3192 unsigned long lock_flags = 0;
3193
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06003194 dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196 if (!dump) {
3197 ipr_err("Dump memory allocation failed\n");
3198 return -ENOMEM;
3199 }
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 kref_init(&dump->kref);
3202 dump->ioa_cfg = ioa_cfg;
3203
3204 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3205
3206 if (INACTIVE != ioa_cfg->sdt_state) {
3207 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3208 kfree(dump);
3209 return 0;
3210 }
3211
3212 ioa_cfg->dump = dump;
3213 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
3214 if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
3215 ioa_cfg->dump_taken = 1;
3216 schedule_work(&ioa_cfg->work_q);
3217 }
3218 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return 0;
3221}
3222
3223/**
3224 * ipr_free_dump - Free adapter dump memory
3225 * @ioa_cfg: ioa config struct
3226 *
3227 * Return value:
3228 * 0 on success / other on failure
3229 **/
3230static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
3231{
3232 struct ipr_dump *dump;
3233 unsigned long lock_flags = 0;
3234
3235 ENTER;
3236
3237 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3238 dump = ioa_cfg->dump;
3239 if (!dump) {
3240 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3241 return 0;
3242 }
3243
3244 ioa_cfg->dump = NULL;
3245 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3246
3247 kref_put(&dump->kref, ipr_release_dump);
3248
3249 LEAVE;
3250 return 0;
3251}
3252
3253/**
3254 * ipr_write_dump - Setup dump state of adapter
3255 * @kobj: kobject struct
3256 * @buf: buffer
3257 * @off: offset
3258 * @count: buffer size
3259 *
3260 * Return value:
3261 * number of bytes printed to buffer
3262 **/
3263static ssize_t ipr_write_dump(struct kobject *kobj, char *buf,
3264 loff_t off, size_t count)
3265{
3266 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
3267 struct Scsi_Host *shost = class_to_shost(cdev);
3268 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3269 int rc;
3270
3271 if (!capable(CAP_SYS_ADMIN))
3272 return -EACCES;
3273
3274 if (buf[0] == '1')
3275 rc = ipr_alloc_dump(ioa_cfg);
3276 else if (buf[0] == '0')
3277 rc = ipr_free_dump(ioa_cfg);
3278 else
3279 return -EINVAL;
3280
3281 if (rc)
3282 return rc;
3283 else
3284 return count;
3285}
3286
3287static struct bin_attribute ipr_dump_attr = {
3288 .attr = {
3289 .name = "dump",
3290 .mode = S_IRUSR | S_IWUSR,
3291 },
3292 .size = 0,
3293 .read = ipr_read_dump,
3294 .write = ipr_write_dump
3295};
3296#else
3297static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
3298#endif
3299
3300/**
3301 * ipr_change_queue_depth - Change the device's queue depth
3302 * @sdev: scsi device struct
3303 * @qdepth: depth to set
3304 *
3305 * Return value:
3306 * actual depth set
3307 **/
3308static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
3309{
Brian King35a39692006-09-25 12:39:20 -05003310 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3311 struct ipr_resource_entry *res;
3312 unsigned long lock_flags = 0;
3313
3314 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3315 res = (struct ipr_resource_entry *)sdev->hostdata;
3316
3317 if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
3318 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
3319 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3322 return sdev->queue_depth;
3323}
3324
3325/**
3326 * ipr_change_queue_type - Change the device's queue type
3327 * @dsev: scsi device struct
3328 * @tag_type: type of tags to use
3329 *
3330 * Return value:
3331 * actual queue type set
3332 **/
3333static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
3334{
3335 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3336 struct ipr_resource_entry *res;
3337 unsigned long lock_flags = 0;
3338
3339 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3340 res = (struct ipr_resource_entry *)sdev->hostdata;
3341
3342 if (res) {
3343 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
3344 /*
3345 * We don't bother quiescing the device here since the
3346 * adapter firmware does it for us.
3347 */
3348 scsi_set_tag_type(sdev, tag_type);
3349
3350 if (tag_type)
3351 scsi_activate_tcq(sdev, sdev->queue_depth);
3352 else
3353 scsi_deactivate_tcq(sdev, sdev->queue_depth);
3354 } else
3355 tag_type = 0;
3356 } else
3357 tag_type = 0;
3358
3359 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3360 return tag_type;
3361}
3362
3363/**
3364 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
3365 * @dev: device struct
3366 * @buf: buffer
3367 *
3368 * Return value:
3369 * number of bytes printed to buffer
3370 **/
Yani Ioannou10523b32005-05-17 06:43:37 -04003371static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
3373 struct scsi_device *sdev = to_scsi_device(dev);
3374 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3375 struct ipr_resource_entry *res;
3376 unsigned long lock_flags = 0;
3377 ssize_t len = -ENXIO;
3378
3379 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3380 res = (struct ipr_resource_entry *)sdev->hostdata;
3381 if (res)
3382 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->cfgte.res_handle);
3383 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3384 return len;
3385}
3386
3387static struct device_attribute ipr_adapter_handle_attr = {
3388 .attr = {
3389 .name = "adapter_handle",
3390 .mode = S_IRUSR,
3391 },
3392 .show = ipr_show_adapter_handle
3393};
3394
3395static struct device_attribute *ipr_dev_attrs[] = {
3396 &ipr_adapter_handle_attr,
3397 NULL,
3398};
3399
3400/**
3401 * ipr_biosparam - Return the HSC mapping
3402 * @sdev: scsi device struct
3403 * @block_device: block device pointer
3404 * @capacity: capacity of the device
3405 * @parm: Array containing returned HSC values.
3406 *
3407 * This function generates the HSC parms that fdisk uses.
3408 * We want to make sure we return something that places partitions
3409 * on 4k boundaries for best performance with the IOA.
3410 *
3411 * Return value:
3412 * 0 on success
3413 **/
3414static int ipr_biosparam(struct scsi_device *sdev,
3415 struct block_device *block_device,
3416 sector_t capacity, int *parm)
3417{
3418 int heads, sectors;
3419 sector_t cylinders;
3420
3421 heads = 128;
3422 sectors = 32;
3423
3424 cylinders = capacity;
3425 sector_div(cylinders, (128 * 32));
3426
3427 /* return result */
3428 parm[0] = heads;
3429 parm[1] = sectors;
3430 parm[2] = cylinders;
3431
3432 return 0;
3433}
3434
3435/**
Brian King35a39692006-09-25 12:39:20 -05003436 * ipr_find_starget - Find target based on bus/target.
3437 * @starget: scsi target struct
3438 *
3439 * Return value:
3440 * resource entry pointer if found / NULL if not found
3441 **/
3442static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
3443{
3444 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3445 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3446 struct ipr_resource_entry *res;
3447
3448 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3449 if ((res->cfgte.res_addr.bus == starget->channel) &&
3450 (res->cfgte.res_addr.target == starget->id) &&
3451 (res->cfgte.res_addr.lun == 0)) {
3452 return res;
3453 }
3454 }
3455
3456 return NULL;
3457}
3458
3459static struct ata_port_info sata_port_info;
3460
3461/**
3462 * ipr_target_alloc - Prepare for commands to a SCSI target
3463 * @starget: scsi target struct
3464 *
3465 * If the device is a SATA device, this function allocates an
3466 * ATA port with libata, else it does nothing.
3467 *
3468 * Return value:
3469 * 0 on success / non-0 on failure
3470 **/
3471static int ipr_target_alloc(struct scsi_target *starget)
3472{
3473 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3474 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3475 struct ipr_sata_port *sata_port;
3476 struct ata_port *ap;
3477 struct ipr_resource_entry *res;
3478 unsigned long lock_flags;
3479
3480 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3481 res = ipr_find_starget(starget);
3482 starget->hostdata = NULL;
3483
3484 if (res && ipr_is_gata(res)) {
3485 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3486 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
3487 if (!sata_port)
3488 return -ENOMEM;
3489
3490 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
3491 if (ap) {
3492 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3493 sata_port->ioa_cfg = ioa_cfg;
3494 sata_port->ap = ap;
3495 sata_port->res = res;
3496
3497 res->sata_port = sata_port;
3498 ap->private_data = sata_port;
3499 starget->hostdata = sata_port;
3500 } else {
3501 kfree(sata_port);
3502 return -ENOMEM;
3503 }
3504 }
3505 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3506
3507 return 0;
3508}
3509
3510/**
3511 * ipr_target_destroy - Destroy a SCSI target
3512 * @starget: scsi target struct
3513 *
3514 * If the device was a SATA device, this function frees the libata
3515 * ATA port, else it does nothing.
3516 *
3517 **/
3518static void ipr_target_destroy(struct scsi_target *starget)
3519{
3520 struct ipr_sata_port *sata_port = starget->hostdata;
3521
3522 if (sata_port) {
3523 starget->hostdata = NULL;
3524 ata_sas_port_destroy(sata_port->ap);
3525 kfree(sata_port);
3526 }
3527}
3528
3529/**
3530 * ipr_find_sdev - Find device based on bus/target/lun.
3531 * @sdev: scsi device struct
3532 *
3533 * Return value:
3534 * resource entry pointer if found / NULL if not found
3535 **/
3536static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
3537{
3538 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3539 struct ipr_resource_entry *res;
3540
3541 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3542 if ((res->cfgte.res_addr.bus == sdev->channel) &&
3543 (res->cfgte.res_addr.target == sdev->id) &&
3544 (res->cfgte.res_addr.lun == sdev->lun))
3545 return res;
3546 }
3547
3548 return NULL;
3549}
3550
3551/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 * ipr_slave_destroy - Unconfigure a SCSI device
3553 * @sdev: scsi device struct
3554 *
3555 * Return value:
3556 * nothing
3557 **/
3558static void ipr_slave_destroy(struct scsi_device *sdev)
3559{
3560 struct ipr_resource_entry *res;
3561 struct ipr_ioa_cfg *ioa_cfg;
3562 unsigned long lock_flags = 0;
3563
3564 ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3565
3566 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3567 res = (struct ipr_resource_entry *) sdev->hostdata;
3568 if (res) {
Brian King35a39692006-09-25 12:39:20 -05003569 if (res->sata_port)
3570 ata_port_disable(res->sata_port->ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 sdev->hostdata = NULL;
3572 res->sdev = NULL;
Brian King35a39692006-09-25 12:39:20 -05003573 res->sata_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 }
3575 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3576}
3577
3578/**
3579 * ipr_slave_configure - Configure a SCSI device
3580 * @sdev: scsi device struct
3581 *
3582 * This function configures the specified scsi device.
3583 *
3584 * Return value:
3585 * 0 on success
3586 **/
3587static int ipr_slave_configure(struct scsi_device *sdev)
3588{
3589 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3590 struct ipr_resource_entry *res;
3591 unsigned long lock_flags = 0;
3592
3593 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3594 res = sdev->hostdata;
3595 if (res) {
3596 if (ipr_is_af_dasd_device(res))
3597 sdev->type = TYPE_RAID;
brking@us.ibm.com0726ce22005-11-01 17:01:01 -06003598 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 sdev->scsi_level = 4;
brking@us.ibm.com0726ce22005-11-01 17:01:01 -06003600 sdev->no_uld_attach = 1;
3601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 if (ipr_is_vset_device(res)) {
3603 sdev->timeout = IPR_VSET_RW_TIMEOUT;
3604 blk_queue_max_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
3605 }
Brian Kinge4fbf442006-03-29 09:37:22 -06003606 if (ipr_is_vset_device(res) || ipr_is_scsi_disk(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 sdev->allow_restart = 1;
Brian King35a39692006-09-25 12:39:20 -05003608 if (ipr_is_gata(res) && res->sata_port) {
3609 scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
3610 ata_sas_slave_configure(sdev, res->sata_port->ap);
3611 } else {
3612 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
3613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 }
3615 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3616 return 0;
3617}
3618
3619/**
Brian King35a39692006-09-25 12:39:20 -05003620 * ipr_ata_slave_alloc - Prepare for commands to a SATA device
3621 * @sdev: scsi device struct
3622 *
3623 * This function initializes an ATA port so that future commands
3624 * sent through queuecommand will work.
3625 *
3626 * Return value:
3627 * 0 on success
3628 **/
3629static int ipr_ata_slave_alloc(struct scsi_device *sdev)
3630{
3631 struct ipr_sata_port *sata_port = NULL;
3632 int rc = -ENXIO;
3633
3634 ENTER;
3635 if (sdev->sdev_target)
3636 sata_port = sdev->sdev_target->hostdata;
3637 if (sata_port)
3638 rc = ata_sas_port_init(sata_port->ap);
3639 if (rc)
3640 ipr_slave_destroy(sdev);
3641
3642 LEAVE;
3643 return rc;
3644}
3645
3646/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 * ipr_slave_alloc - Prepare for commands to a device.
3648 * @sdev: scsi device struct
3649 *
3650 * This function saves a pointer to the resource entry
3651 * in the scsi device struct if the device exists. We
3652 * can then use this pointer in ipr_queuecommand when
3653 * handling new commands.
3654 *
3655 * Return value:
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06003656 * 0 on success / -ENXIO if device does not exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 **/
3658static int ipr_slave_alloc(struct scsi_device *sdev)
3659{
3660 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3661 struct ipr_resource_entry *res;
3662 unsigned long lock_flags;
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06003663 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
3665 sdev->hostdata = NULL;
3666
3667 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3668
Brian King35a39692006-09-25 12:39:20 -05003669 res = ipr_find_sdev(sdev);
3670 if (res) {
3671 res->sdev = sdev;
3672 res->add_to_ml = 0;
3673 res->in_erp = 0;
3674 sdev->hostdata = res;
3675 if (!ipr_is_naca_model(res))
3676 res->needs_sync_complete = 1;
3677 rc = 0;
3678 if (ipr_is_gata(res)) {
3679 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3680 return ipr_ata_slave_alloc(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 }
3682 }
3683
3684 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3685
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06003686 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687}
3688
3689/**
3690 * ipr_eh_host_reset - Reset the host adapter
3691 * @scsi_cmd: scsi command struct
3692 *
3693 * Return value:
3694 * SUCCESS / FAILED
3695 **/
Jeff Garzik df0ae242005-05-28 07:57:14 -04003696static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
3698 struct ipr_ioa_cfg *ioa_cfg;
3699 int rc;
3700
3701 ENTER;
3702 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3703
3704 dev_err(&ioa_cfg->pdev->dev,
3705 "Adapter being reset as a result of error recovery.\n");
3706
3707 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3708 ioa_cfg->sdt_state = GET_DUMP;
3709
3710 rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
3711
3712 LEAVE;
3713 return rc;
3714}
3715
Jeff Garzik df0ae242005-05-28 07:57:14 -04003716static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
3717{
3718 int rc;
3719
3720 spin_lock_irq(cmd->device->host->host_lock);
3721 rc = __ipr_eh_host_reset(cmd);
3722 spin_unlock_irq(cmd->device->host->host_lock);
3723
3724 return rc;
3725}
3726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727/**
Brian Kingc6513092006-03-29 09:37:43 -06003728 * ipr_device_reset - Reset the device
3729 * @ioa_cfg: ioa config struct
3730 * @res: resource entry struct
3731 *
3732 * This function issues a device reset to the affected device.
3733 * If the device is a SCSI device, a LUN reset will be sent
3734 * to the device first. If that does not work, a target reset
Brian King35a39692006-09-25 12:39:20 -05003735 * will be sent. If the device is a SATA device, a PHY reset will
3736 * be sent.
Brian Kingc6513092006-03-29 09:37:43 -06003737 *
3738 * Return value:
3739 * 0 on success / non-zero on failure
3740 **/
3741static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
3742 struct ipr_resource_entry *res)
3743{
3744 struct ipr_cmnd *ipr_cmd;
3745 struct ipr_ioarcb *ioarcb;
3746 struct ipr_cmd_pkt *cmd_pkt;
Brian King35a39692006-09-25 12:39:20 -05003747 struct ipr_ioarcb_ata_regs *regs;
Brian Kingc6513092006-03-29 09:37:43 -06003748 u32 ioasc;
3749
3750 ENTER;
3751 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3752 ioarcb = &ipr_cmd->ioarcb;
3753 cmd_pkt = &ioarcb->cmd_pkt;
Brian King35a39692006-09-25 12:39:20 -05003754 regs = &ioarcb->add_data.u.regs;
Brian Kingc6513092006-03-29 09:37:43 -06003755
3756 ioarcb->res_handle = res->cfgte.res_handle;
3757 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3758 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
Brian King35a39692006-09-25 12:39:20 -05003759 if (ipr_is_gata(res)) {
3760 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
3761 ioarcb->add_cmd_parms_len = cpu_to_be32(sizeof(regs->flags));
3762 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
3763 }
Brian Kingc6513092006-03-29 09:37:43 -06003764
3765 ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3766 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3767 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
Brian King35a39692006-09-25 12:39:20 -05003768 if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET)
3769 memcpy(&res->sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
3770 sizeof(struct ipr_ioasa_gata));
Brian Kingc6513092006-03-29 09:37:43 -06003771
3772 LEAVE;
3773 return (IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0);
3774}
3775
3776/**
Brian King35a39692006-09-25 12:39:20 -05003777 * ipr_sata_reset - Reset the SATA port
3778 * @ap: SATA port to reset
3779 * @classes: class of the attached device
3780 *
3781 * This function issues a SATA phy reset to the affected ATA port.
3782 *
3783 * Return value:
3784 * 0 on success / non-zero on failure
3785 **/
Andrew Morton120bda32007-03-26 02:17:43 -07003786static int ipr_sata_reset(struct ata_port *ap, unsigned int *classes,
3787 unsigned long deadline)
Brian King35a39692006-09-25 12:39:20 -05003788{
3789 struct ipr_sata_port *sata_port = ap->private_data;
3790 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
3791 struct ipr_resource_entry *res;
3792 unsigned long lock_flags = 0;
3793 int rc = -ENXIO;
3794
3795 ENTER;
3796 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King73d98ff2006-11-21 10:27:58 -06003797 while(ioa_cfg->in_reset_reload) {
3798 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3799 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3800 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3801 }
3802
Brian King35a39692006-09-25 12:39:20 -05003803 res = sata_port->res;
3804 if (res) {
3805 rc = ipr_device_reset(ioa_cfg, res);
3806 switch(res->cfgte.proto) {
3807 case IPR_PROTO_SATA:
3808 case IPR_PROTO_SAS_STP:
3809 *classes = ATA_DEV_ATA;
3810 break;
3811 case IPR_PROTO_SATA_ATAPI:
3812 case IPR_PROTO_SAS_STP_ATAPI:
3813 *classes = ATA_DEV_ATAPI;
3814 break;
3815 default:
3816 *classes = ATA_DEV_UNKNOWN;
3817 break;
3818 };
3819 }
3820
3821 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3822 LEAVE;
3823 return rc;
3824}
3825
3826/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 * ipr_eh_dev_reset - Reset the device
3828 * @scsi_cmd: scsi command struct
3829 *
3830 * This function issues a device reset to the affected device.
3831 * A LUN reset will be sent to the device first. If that does
3832 * not work, a target reset will be sent.
3833 *
3834 * Return value:
3835 * SUCCESS / FAILED
3836 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003837static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
3839 struct ipr_cmnd *ipr_cmd;
3840 struct ipr_ioa_cfg *ioa_cfg;
3841 struct ipr_resource_entry *res;
Brian King35a39692006-09-25 12:39:20 -05003842 struct ata_port *ap;
3843 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
3845 ENTER;
3846 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3847 res = scsi_cmd->device->hostdata;
3848
brking@us.ibm.comeeb883072005-11-01 17:02:29 -06003849 if (!res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 return FAILED;
3851
3852 /*
3853 * If we are currently going through reset/reload, return failed. This will force the
3854 * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
3855 * reset to complete
3856 */
3857 if (ioa_cfg->in_reset_reload)
3858 return FAILED;
3859 if (ioa_cfg->ioa_is_dead)
3860 return FAILED;
3861
3862 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3863 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
3864 if (ipr_cmd->scsi_cmd)
3865 ipr_cmd->done = ipr_scsi_eh_done;
Brian King24d6f2b2007-03-29 12:43:17 -05003866 if (ipr_cmd->qc)
3867 ipr_cmd->done = ipr_sata_eh_done;
Brian King7402ece2006-11-21 10:28:23 -06003868 if (ipr_cmd->qc && !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
3869 ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
3870 ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
3871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 }
3873 }
3874
3875 res->resetting_device = 1;
Brian Kingfb3ed3c2006-03-29 09:37:37 -06003876 scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
Brian King35a39692006-09-25 12:39:20 -05003877
3878 if (ipr_is_gata(res) && res->sata_port) {
3879 ap = res->sata_port->ap;
3880 spin_unlock_irq(scsi_cmd->device->host->host_lock);
3881 ata_do_eh(ap, NULL, NULL, ipr_sata_reset, NULL);
3882 spin_lock_irq(scsi_cmd->device->host->host_lock);
3883 } else
3884 rc = ipr_device_reset(ioa_cfg, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 res->resetting_device = 0;
3886
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 LEAVE;
Brian Kingc6513092006-03-29 09:37:43 -06003888 return (rc ? FAILED : SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889}
3890
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003891static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
3892{
3893 int rc;
3894
3895 spin_lock_irq(cmd->device->host->host_lock);
3896 rc = __ipr_eh_dev_reset(cmd);
3897 spin_unlock_irq(cmd->device->host->host_lock);
3898
3899 return rc;
3900}
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902/**
3903 * ipr_bus_reset_done - Op done function for bus reset.
3904 * @ipr_cmd: ipr command struct
3905 *
3906 * This function is the op done function for a bus reset
3907 *
3908 * Return value:
3909 * none
3910 **/
3911static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
3912{
3913 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3914 struct ipr_resource_entry *res;
3915
3916 ENTER;
3917 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3918 if (!memcmp(&res->cfgte.res_handle, &ipr_cmd->ioarcb.res_handle,
3919 sizeof(res->cfgte.res_handle))) {
3920 scsi_report_bus_reset(ioa_cfg->host, res->cfgte.res_addr.bus);
3921 break;
3922 }
3923 }
3924
3925 /*
3926 * If abort has not completed, indicate the reset has, else call the
3927 * abort's done function to wake the sleeping eh thread
3928 */
3929 if (ipr_cmd->sibling->sibling)
3930 ipr_cmd->sibling->sibling = NULL;
3931 else
3932 ipr_cmd->sibling->done(ipr_cmd->sibling);
3933
3934 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3935 LEAVE;
3936}
3937
3938/**
3939 * ipr_abort_timeout - An abort task has timed out
3940 * @ipr_cmd: ipr command struct
3941 *
3942 * This function handles when an abort task times out. If this
3943 * happens we issue a bus reset since we have resources tied
3944 * up that must be freed before returning to the midlayer.
3945 *
3946 * Return value:
3947 * none
3948 **/
3949static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
3950{
3951 struct ipr_cmnd *reset_cmd;
3952 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3953 struct ipr_cmd_pkt *cmd_pkt;
3954 unsigned long lock_flags = 0;
3955
3956 ENTER;
3957 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3958 if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
3959 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3960 return;
3961 }
3962
Brian Kingfb3ed3c2006-03-29 09:37:37 -06003963 sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3965 ipr_cmd->sibling = reset_cmd;
3966 reset_cmd->sibling = ipr_cmd;
3967 reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
3968 cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
3969 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3970 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
3971 cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
3972
3973 ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3974 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3975 LEAVE;
3976}
3977
3978/**
3979 * ipr_cancel_op - Cancel specified op
3980 * @scsi_cmd: scsi command struct
3981 *
3982 * This function cancels specified op.
3983 *
3984 * Return value:
3985 * SUCCESS / FAILED
3986 **/
3987static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
3988{
3989 struct ipr_cmnd *ipr_cmd;
3990 struct ipr_ioa_cfg *ioa_cfg;
3991 struct ipr_resource_entry *res;
3992 struct ipr_cmd_pkt *cmd_pkt;
3993 u32 ioasc;
3994 int op_found = 0;
3995
3996 ENTER;
3997 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3998 res = scsi_cmd->device->hostdata;
3999
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004000 /* If we are currently going through reset/reload, return failed.
4001 * This will force the mid-layer to call ipr_eh_host_reset,
4002 * which will then go to sleep and wait for the reset to complete
4003 */
4004 if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
4005 return FAILED;
Brian King04d97682006-11-21 10:28:04 -06004006 if (!res || !ipr_is_gscsi(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 return FAILED;
4008
4009 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4010 if (ipr_cmd->scsi_cmd == scsi_cmd) {
4011 ipr_cmd->done = ipr_scsi_eh_done;
4012 op_found = 1;
4013 break;
4014 }
4015 }
4016
4017 if (!op_found)
4018 return SUCCESS;
4019
4020 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4021 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
4022 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4023 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4024 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4025 ipr_cmd->u.sdev = scsi_cmd->device;
4026
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004027 scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
4028 scsi_cmd->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
4030 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4031
4032 /*
4033 * If the abort task timed out and we sent a bus reset, we will get
4034 * one the following responses to the abort
4035 */
4036 if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
4037 ioasc = 0;
4038 ipr_trace;
4039 }
4040
4041 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004042 if (!ipr_is_naca_model(res))
4043 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
4045 LEAVE;
4046 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
4047}
4048
4049/**
4050 * ipr_eh_abort - Abort a single op
4051 * @scsi_cmd: scsi command struct
4052 *
4053 * Return value:
4054 * SUCCESS / FAILED
4055 **/
4056static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
4057{
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004058 unsigned long flags;
4059 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
4061 ENTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004063 spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
4064 rc = ipr_cancel_op(scsi_cmd);
4065 spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
4067 LEAVE;
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004068 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
4071/**
4072 * ipr_handle_other_interrupt - Handle "other" interrupts
4073 * @ioa_cfg: ioa config struct
4074 * @int_reg: interrupt register
4075 *
4076 * Return value:
4077 * IRQ_NONE / IRQ_HANDLED
4078 **/
4079static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
4080 volatile u32 int_reg)
4081{
4082 irqreturn_t rc = IRQ_HANDLED;
4083
4084 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4085 /* Mask the interrupt */
4086 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
4087
4088 /* Clear the interrupt */
4089 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
4090 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4091
4092 list_del(&ioa_cfg->reset_cmd->queue);
4093 del_timer(&ioa_cfg->reset_cmd->timer);
4094 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
4095 } else {
4096 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
4097 ioa_cfg->ioa_unit_checked = 1;
4098 else
4099 dev_err(&ioa_cfg->pdev->dev,
4100 "Permanent IOA failure. 0x%08X\n", int_reg);
4101
4102 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4103 ioa_cfg->sdt_state = GET_DUMP;
4104
4105 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
4106 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4107 }
4108
4109 return rc;
4110}
4111
4112/**
4113 * ipr_isr - Interrupt service routine
4114 * @irq: irq number
4115 * @devp: pointer to ioa config struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 *
4117 * Return value:
4118 * IRQ_NONE / IRQ_HANDLED
4119 **/
David Howells7d12e782006-10-05 14:55:46 +01004120static irqreturn_t ipr_isr(int irq, void *devp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121{
4122 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
4123 unsigned long lock_flags = 0;
4124 volatile u32 int_reg, int_mask_reg;
4125 u32 ioasc;
4126 u16 cmd_index;
4127 struct ipr_cmnd *ipr_cmd;
4128 irqreturn_t rc = IRQ_NONE;
4129
4130 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4131
4132 /* If interrupts are disabled, ignore the interrupt */
4133 if (!ioa_cfg->allow_interrupts) {
4134 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4135 return IRQ_NONE;
4136 }
4137
4138 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4139 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4140
4141 /* If an interrupt on the adapter did not occur, ignore it */
4142 if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
4143 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4144 return IRQ_NONE;
4145 }
4146
4147 while (1) {
4148 ipr_cmd = NULL;
4149
4150 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
4151 ioa_cfg->toggle_bit) {
4152
4153 cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
4154 IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
4155
4156 if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
4157 ioa_cfg->errors_logged++;
4158 dev_err(&ioa_cfg->pdev->dev, "Invalid response handle from IOA\n");
4159
4160 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4161 ioa_cfg->sdt_state = GET_DUMP;
4162
4163 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4164 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4165 return IRQ_HANDLED;
4166 }
4167
4168 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
4169
4170 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4171
4172 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
4173
4174 list_del(&ipr_cmd->queue);
4175 del_timer(&ipr_cmd->timer);
4176 ipr_cmd->done(ipr_cmd);
4177
4178 rc = IRQ_HANDLED;
4179
4180 if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
4181 ioa_cfg->hrrq_curr++;
4182 } else {
4183 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
4184 ioa_cfg->toggle_bit ^= 1u;
4185 }
4186 }
4187
4188 if (ipr_cmd != NULL) {
4189 /* Clear the PCI interrupt */
4190 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg);
4191 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4192 } else
4193 break;
4194 }
4195
4196 if (unlikely(rc == IRQ_NONE))
4197 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
4198
4199 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4200 return rc;
4201}
4202
4203/**
4204 * ipr_build_ioadl - Build a scatter/gather list and map the buffer
4205 * @ioa_cfg: ioa config struct
4206 * @ipr_cmd: ipr command struct
4207 *
4208 * Return value:
4209 * 0 on success / -1 on failure
4210 **/
4211static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
4212 struct ipr_cmnd *ipr_cmd)
4213{
4214 int i;
4215 struct scatterlist *sglist;
4216 u32 length;
4217 u32 ioadl_flags = 0;
4218 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4219 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4220 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4221
4222 length = scsi_cmd->request_bufflen;
4223
4224 if (length == 0)
4225 return 0;
4226
4227 if (scsi_cmd->use_sg) {
4228 ipr_cmd->dma_use_sg = pci_map_sg(ioa_cfg->pdev,
4229 scsi_cmd->request_buffer,
4230 scsi_cmd->use_sg,
4231 scsi_cmd->sc_data_direction);
4232
4233 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
4234 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
4235 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4236 ioarcb->write_data_transfer_length = cpu_to_be32(length);
4237 ioarcb->write_ioadl_len =
4238 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
4239 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
4240 ioadl_flags = IPR_IOADL_FLAGS_READ;
4241 ioarcb->read_data_transfer_length = cpu_to_be32(length);
4242 ioarcb->read_ioadl_len =
4243 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
4244 }
4245
4246 sglist = scsi_cmd->request_buffer;
4247
Brian King51b1c7e2007-03-29 12:43:50 -05004248 if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->add_data.u.ioadl)) {
4249 ioadl = ioarcb->add_data.u.ioadl;
4250 ioarcb->write_ioadl_addr =
4251 cpu_to_be32(be32_to_cpu(ioarcb->ioarcb_host_pci_addr) +
4252 offsetof(struct ipr_ioarcb, add_data));
4253 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
4254 }
4255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
4257 ioadl[i].flags_and_data_len =
4258 cpu_to_be32(ioadl_flags | sg_dma_len(&sglist[i]));
4259 ioadl[i].address =
4260 cpu_to_be32(sg_dma_address(&sglist[i]));
4261 }
4262
4263 if (likely(ipr_cmd->dma_use_sg)) {
4264 ioadl[i-1].flags_and_data_len |=
4265 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
4266 return 0;
4267 } else
4268 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
4269 } else {
4270 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
4271 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
4272 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4273 ioarcb->write_data_transfer_length = cpu_to_be32(length);
4274 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4275 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
4276 ioadl_flags = IPR_IOADL_FLAGS_READ;
4277 ioarcb->read_data_transfer_length = cpu_to_be32(length);
4278 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4279 }
4280
4281 ipr_cmd->dma_handle = pci_map_single(ioa_cfg->pdev,
4282 scsi_cmd->request_buffer, length,
4283 scsi_cmd->sc_data_direction);
4284
4285 if (likely(!pci_dma_mapping_error(ipr_cmd->dma_handle))) {
Brian King51b1c7e2007-03-29 12:43:50 -05004286 ioadl = ioarcb->add_data.u.ioadl;
4287 ioarcb->write_ioadl_addr =
4288 cpu_to_be32(be32_to_cpu(ioarcb->ioarcb_host_pci_addr) +
4289 offsetof(struct ipr_ioarcb, add_data));
4290 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 ipr_cmd->dma_use_sg = 1;
4292 ioadl[0].flags_and_data_len =
4293 cpu_to_be32(ioadl_flags | length | IPR_IOADL_FLAGS_LAST);
4294 ioadl[0].address = cpu_to_be32(ipr_cmd->dma_handle);
4295 return 0;
4296 } else
4297 dev_err(&ioa_cfg->pdev->dev, "pci_map_single failed!\n");
4298 }
4299
4300 return -1;
4301}
4302
4303/**
4304 * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
4305 * @scsi_cmd: scsi command struct
4306 *
4307 * Return value:
4308 * task attributes
4309 **/
4310static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
4311{
4312 u8 tag[2];
4313 u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
4314
4315 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
4316 switch (tag[0]) {
4317 case MSG_SIMPLE_TAG:
4318 rc = IPR_FLAGS_LO_SIMPLE_TASK;
4319 break;
4320 case MSG_HEAD_TAG:
4321 rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
4322 break;
4323 case MSG_ORDERED_TAG:
4324 rc = IPR_FLAGS_LO_ORDERED_TASK;
4325 break;
4326 };
4327 }
4328
4329 return rc;
4330}
4331
4332/**
4333 * ipr_erp_done - Process completion of ERP for a device
4334 * @ipr_cmd: ipr command struct
4335 *
4336 * This function copies the sense buffer into the scsi_cmd
4337 * struct and pushes the scsi_done function.
4338 *
4339 * Return value:
4340 * nothing
4341 **/
4342static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
4343{
4344 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4345 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4346 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4347 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4348
4349 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
4350 scsi_cmd->result |= (DID_ERROR << 16);
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004351 scmd_printk(KERN_ERR, scsi_cmd,
4352 "Request Sense failed with IOASC: 0x%08X\n", ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 } else {
4354 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
4355 SCSI_SENSE_BUFFERSIZE);
4356 }
4357
4358 if (res) {
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004359 if (!ipr_is_naca_model(res))
4360 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 res->in_erp = 0;
4362 }
4363 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
4364 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4365 scsi_cmd->scsi_done(scsi_cmd);
4366}
4367
4368/**
4369 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
4370 * @ipr_cmd: ipr command struct
4371 *
4372 * Return value:
4373 * none
4374 **/
4375static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
4376{
Brian King51b1c7e2007-03-29 12:43:50 -05004377 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4378 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4379 dma_addr_t dma_addr = be32_to_cpu(ioarcb->ioarcb_host_pci_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
4381 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
4382 ioarcb->write_data_transfer_length = 0;
4383 ioarcb->read_data_transfer_length = 0;
4384 ioarcb->write_ioadl_len = 0;
4385 ioarcb->read_ioadl_len = 0;
4386 ioasa->ioasc = 0;
4387 ioasa->residual_data_len = 0;
Brian King51b1c7e2007-03-29 12:43:50 -05004388 ioarcb->write_ioadl_addr =
4389 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
4390 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391}
4392
4393/**
4394 * ipr_erp_request_sense - Send request sense to a device
4395 * @ipr_cmd: ipr command struct
4396 *
4397 * This function sends a request sense to a device as a result
4398 * of a check condition.
4399 *
4400 * Return value:
4401 * nothing
4402 **/
4403static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
4404{
4405 struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4406 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4407
4408 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
4409 ipr_erp_done(ipr_cmd);
4410 return;
4411 }
4412
4413 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
4414
4415 cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
4416 cmd_pkt->cdb[0] = REQUEST_SENSE;
4417 cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
4418 cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
4419 cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
4420 cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
4421
4422 ipr_cmd->ioadl[0].flags_and_data_len =
4423 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | SCSI_SENSE_BUFFERSIZE);
4424 ipr_cmd->ioadl[0].address =
4425 cpu_to_be32(ipr_cmd->sense_buffer_dma);
4426
4427 ipr_cmd->ioarcb.read_ioadl_len =
4428 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4429 ipr_cmd->ioarcb.read_data_transfer_length =
4430 cpu_to_be32(SCSI_SENSE_BUFFERSIZE);
4431
4432 ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
4433 IPR_REQUEST_SENSE_TIMEOUT * 2);
4434}
4435
4436/**
4437 * ipr_erp_cancel_all - Send cancel all to a device
4438 * @ipr_cmd: ipr command struct
4439 *
4440 * This function sends a cancel all to a device to clear the
4441 * queue. If we are running TCQ on the device, QERR is set to 1,
4442 * which means all outstanding ops have been dropped on the floor.
4443 * Cancel all will return them to us.
4444 *
4445 * Return value:
4446 * nothing
4447 **/
4448static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
4449{
4450 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4451 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4452 struct ipr_cmd_pkt *cmd_pkt;
4453
4454 res->in_erp = 1;
4455
4456 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
4457
4458 if (!scsi_get_tag_type(scsi_cmd->device)) {
4459 ipr_erp_request_sense(ipr_cmd);
4460 return;
4461 }
4462
4463 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4464 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4465 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4466
4467 ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
4468 IPR_CANCEL_ALL_TIMEOUT);
4469}
4470
4471/**
4472 * ipr_dump_ioasa - Dump contents of IOASA
4473 * @ioa_cfg: ioa config struct
4474 * @ipr_cmd: ipr command struct
Brian Kingfe964d02006-03-29 09:37:29 -06004475 * @res: resource entry struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 *
4477 * This function is invoked by the interrupt handler when ops
4478 * fail. It will log the IOASA if appropriate. Only called
4479 * for GPDD ops.
4480 *
4481 * Return value:
4482 * none
4483 **/
4484static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
Brian Kingfe964d02006-03-29 09:37:29 -06004485 struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486{
4487 int i;
4488 u16 data_len;
Brian Kingb0692dd2007-03-29 12:43:09 -05004489 u32 ioasc, fd_ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4491 __be32 *ioasa_data = (__be32 *)ioasa;
4492 int error_index;
4493
4494 ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
Brian Kingb0692dd2007-03-29 12:43:09 -05004495 fd_ioasc = be32_to_cpu(ioasa->fd_ioasc) & IPR_IOASC_IOASC_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497 if (0 == ioasc)
4498 return;
4499
4500 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
4501 return;
4502
Brian Kingb0692dd2007-03-29 12:43:09 -05004503 if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
4504 error_index = ipr_get_error(fd_ioasc);
4505 else
4506 error_index = ipr_get_error(ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
4508 if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
4509 /* Don't log an error if the IOA already logged one */
4510 if (ioasa->ilid != 0)
4511 return;
4512
Brian Kingcc9bd5d2007-03-29 12:43:01 -05004513 if (!ipr_is_gscsi(res))
4514 return;
4515
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 if (ipr_error_table[error_index].log_ioasa == 0)
4517 return;
4518 }
4519
Brian Kingfe964d02006-03-29 09:37:29 -06004520 ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
4522 if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
4523 data_len = sizeof(struct ipr_ioasa);
4524 else
4525 data_len = be16_to_cpu(ioasa->ret_stat_len);
4526
4527 ipr_err("IOASA Dump:\n");
4528
4529 for (i = 0; i < data_len / 4; i += 4) {
4530 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
4531 be32_to_cpu(ioasa_data[i]),
4532 be32_to_cpu(ioasa_data[i+1]),
4533 be32_to_cpu(ioasa_data[i+2]),
4534 be32_to_cpu(ioasa_data[i+3]));
4535 }
4536}
4537
4538/**
4539 * ipr_gen_sense - Generate SCSI sense data from an IOASA
4540 * @ioasa: IOASA
4541 * @sense_buf: sense data buffer
4542 *
4543 * Return value:
4544 * none
4545 **/
4546static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
4547{
4548 u32 failing_lba;
4549 u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
4550 struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
4551 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4552 u32 ioasc = be32_to_cpu(ioasa->ioasc);
4553
4554 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
4555
4556 if (ioasc >= IPR_FIRST_DRIVER_IOASC)
4557 return;
4558
4559 ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
4560
4561 if (ipr_is_vset_device(res) &&
4562 ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
4563 ioasa->u.vset.failing_lba_hi != 0) {
4564 sense_buf[0] = 0x72;
4565 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
4566 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
4567 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
4568
4569 sense_buf[7] = 12;
4570 sense_buf[8] = 0;
4571 sense_buf[9] = 0x0A;
4572 sense_buf[10] = 0x80;
4573
4574 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
4575
4576 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
4577 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
4578 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
4579 sense_buf[15] = failing_lba & 0x000000ff;
4580
4581 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
4582
4583 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
4584 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
4585 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
4586 sense_buf[19] = failing_lba & 0x000000ff;
4587 } else {
4588 sense_buf[0] = 0x70;
4589 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
4590 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
4591 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
4592
4593 /* Illegal request */
4594 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
4595 (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
4596 sense_buf[7] = 10; /* additional length */
4597
4598 /* IOARCB was in error */
4599 if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
4600 sense_buf[15] = 0xC0;
4601 else /* Parameter data was invalid */
4602 sense_buf[15] = 0x80;
4603
4604 sense_buf[16] =
4605 ((IPR_FIELD_POINTER_MASK &
4606 be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
4607 sense_buf[17] =
4608 (IPR_FIELD_POINTER_MASK &
4609 be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
4610 } else {
4611 if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
4612 if (ipr_is_vset_device(res))
4613 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
4614 else
4615 failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
4616
4617 sense_buf[0] |= 0x80; /* Or in the Valid bit */
4618 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
4619 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
4620 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
4621 sense_buf[6] = failing_lba & 0x000000ff;
4622 }
4623
4624 sense_buf[7] = 6; /* additional length */
4625 }
4626 }
4627}
4628
4629/**
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004630 * ipr_get_autosense - Copy autosense data to sense buffer
4631 * @ipr_cmd: ipr command struct
4632 *
4633 * This function copies the autosense buffer to the buffer
4634 * in the scsi_cmd, if there is autosense available.
4635 *
4636 * Return value:
4637 * 1 if autosense was available / 0 if not
4638 **/
4639static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
4640{
4641 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4642
Brian King117d2ce2006-08-02 14:57:58 -05004643 if ((be32_to_cpu(ioasa->ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004644 return 0;
4645
4646 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
4647 min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
4648 SCSI_SENSE_BUFFERSIZE));
4649 return 1;
4650}
4651
4652/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 * ipr_erp_start - Process an error response for a SCSI op
4654 * @ioa_cfg: ioa config struct
4655 * @ipr_cmd: ipr command struct
4656 *
4657 * This function determines whether or not to initiate ERP
4658 * on the affected device.
4659 *
4660 * Return value:
4661 * nothing
4662 **/
4663static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
4664 struct ipr_cmnd *ipr_cmd)
4665{
4666 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4667 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4668 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
Brian King8a048992007-04-26 16:00:10 -05004669 u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
4671 if (!res) {
4672 ipr_scsi_eh_done(ipr_cmd);
4673 return;
4674 }
4675
Brian King8a048992007-04-26 16:00:10 -05004676 if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 ipr_gen_sense(ipr_cmd);
4678
Brian Kingcc9bd5d2007-03-29 12:43:01 -05004679 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
4680
Brian King8a048992007-04-26 16:00:10 -05004681 switch (masked_ioasc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004683 if (ipr_is_naca_model(res))
4684 scsi_cmd->result |= (DID_ABORT << 16);
4685 else
4686 scsi_cmd->result |= (DID_IMM_RETRY << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 break;
4688 case IPR_IOASC_IR_RESOURCE_HANDLE:
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06004689 case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 scsi_cmd->result |= (DID_NO_CONNECT << 16);
4691 break;
4692 case IPR_IOASC_HW_SEL_TIMEOUT:
4693 scsi_cmd->result |= (DID_NO_CONNECT << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004694 if (!ipr_is_naca_model(res))
4695 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 break;
4697 case IPR_IOASC_SYNC_REQUIRED:
4698 if (!res->in_erp)
4699 res->needs_sync_complete = 1;
4700 scsi_cmd->result |= (DID_IMM_RETRY << 16);
4701 break;
4702 case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06004703 case IPR_IOASA_IR_DUAL_IOA_DISABLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
4705 break;
4706 case IPR_IOASC_BUS_WAS_RESET:
4707 case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
4708 /*
4709 * Report the bus reset and ask for a retry. The device
4710 * will give CC/UA the next command.
4711 */
4712 if (!res->resetting_device)
4713 scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
4714 scsi_cmd->result |= (DID_ERROR << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004715 if (!ipr_is_naca_model(res))
4716 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 break;
4718 case IPR_IOASC_HW_DEV_BUS_STATUS:
4719 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
4720 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004721 if (!ipr_get_autosense(ipr_cmd)) {
4722 if (!ipr_is_naca_model(res)) {
4723 ipr_erp_cancel_all(ipr_cmd);
4724 return;
4725 }
4726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 }
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004728 if (!ipr_is_naca_model(res))
4729 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 break;
4731 case IPR_IOASC_NR_INIT_CMD_REQUIRED:
4732 break;
4733 default:
Brian King5b7304f2006-08-02 14:57:51 -05004734 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
4735 scsi_cmd->result |= (DID_ERROR << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004736 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 res->needs_sync_complete = 1;
4738 break;
4739 }
4740
4741 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
4742 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4743 scsi_cmd->scsi_done(scsi_cmd);
4744}
4745
4746/**
4747 * ipr_scsi_done - mid-layer done function
4748 * @ipr_cmd: ipr command struct
4749 *
4750 * This function is invoked by the interrupt handler for
4751 * ops generated by the SCSI mid-layer
4752 *
4753 * Return value:
4754 * none
4755 **/
4756static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
4757{
4758 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4759 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4760 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4761
4762 scsi_cmd->resid = be32_to_cpu(ipr_cmd->ioasa.residual_data_len);
4763
4764 if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
4765 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
4766 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4767 scsi_cmd->scsi_done(scsi_cmd);
4768 } else
4769 ipr_erp_start(ioa_cfg, ipr_cmd);
4770}
4771
4772/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 * ipr_queuecommand - Queue a mid-layer request
4774 * @scsi_cmd: scsi command struct
4775 * @done: done function
4776 *
4777 * This function queues a request generated by the mid-layer.
4778 *
4779 * Return value:
4780 * 0 on success
4781 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
4782 * SCSI_MLQUEUE_HOST_BUSY if host is busy
4783 **/
4784static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
4785 void (*done) (struct scsi_cmnd *))
4786{
4787 struct ipr_ioa_cfg *ioa_cfg;
4788 struct ipr_resource_entry *res;
4789 struct ipr_ioarcb *ioarcb;
4790 struct ipr_cmnd *ipr_cmd;
4791 int rc = 0;
4792
4793 scsi_cmd->scsi_done = done;
4794 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4795 res = scsi_cmd->device->hostdata;
4796 scsi_cmd->result = (DID_OK << 16);
4797
4798 /*
4799 * We are currently blocking all devices due to a host reset
4800 * We have told the host to stop giving us new requests, but
4801 * ERP ops don't count. FIXME
4802 */
4803 if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
4804 return SCSI_MLQUEUE_HOST_BUSY;
4805
4806 /*
4807 * FIXME - Create scsi_set_host_offline interface
4808 * and the ioa_is_dead check can be removed
4809 */
4810 if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
4811 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
4812 scsi_cmd->result = (DID_NO_CONNECT << 16);
4813 scsi_cmd->scsi_done(scsi_cmd);
4814 return 0;
4815 }
4816
Brian King35a39692006-09-25 12:39:20 -05004817 if (ipr_is_gata(res) && res->sata_port)
4818 return ata_sas_queuecmd(scsi_cmd, done, res->sata_port->ap);
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4821 ioarcb = &ipr_cmd->ioarcb;
4822 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
4823
4824 memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
4825 ipr_cmd->scsi_cmd = scsi_cmd;
4826 ioarcb->res_handle = res->cfgte.res_handle;
4827 ipr_cmd->done = ipr_scsi_done;
4828 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
4829
4830 if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
4831 if (scsi_cmd->underflow == 0)
4832 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
4833
4834 if (res->needs_sync_complete) {
4835 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
4836 res->needs_sync_complete = 0;
4837 }
4838
4839 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
4840 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
4841 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
4842 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
4843 }
4844
4845 if (scsi_cmd->cmnd[0] >= 0xC0 &&
4846 (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
4847 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 if (likely(rc == 0))
4850 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
4851
4852 if (likely(rc == 0)) {
4853 mb();
4854 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
4855 ioa_cfg->regs.ioarrin_reg);
4856 } else {
4857 list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4858 return SCSI_MLQUEUE_HOST_BUSY;
4859 }
4860
4861 return 0;
4862}
4863
4864/**
Brian King35a39692006-09-25 12:39:20 -05004865 * ipr_ioctl - IOCTL handler
4866 * @sdev: scsi device struct
4867 * @cmd: IOCTL cmd
4868 * @arg: IOCTL arg
4869 *
4870 * Return value:
4871 * 0 on success / other on failure
4872 **/
Adrian Bunkbd705f22006-11-21 10:28:48 -06004873static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
Brian King35a39692006-09-25 12:39:20 -05004874{
4875 struct ipr_resource_entry *res;
4876
4877 res = (struct ipr_resource_entry *)sdev->hostdata;
4878 if (res && ipr_is_gata(res))
4879 return ata_scsi_ioctl(sdev, cmd, arg);
4880
4881 return -EINVAL;
4882}
4883
4884/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 * ipr_info - Get information about the card/driver
4886 * @scsi_host: scsi host struct
4887 *
4888 * Return value:
4889 * pointer to buffer with description string
4890 **/
4891static const char * ipr_ioa_info(struct Scsi_Host *host)
4892{
4893 static char buffer[512];
4894 struct ipr_ioa_cfg *ioa_cfg;
4895 unsigned long lock_flags = 0;
4896
4897 ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
4898
4899 spin_lock_irqsave(host->host_lock, lock_flags);
4900 sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
4901 spin_unlock_irqrestore(host->host_lock, lock_flags);
4902
4903 return buffer;
4904}
4905
4906static struct scsi_host_template driver_template = {
4907 .module = THIS_MODULE,
4908 .name = "IPR",
4909 .info = ipr_ioa_info,
Brian King35a39692006-09-25 12:39:20 -05004910 .ioctl = ipr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 .queuecommand = ipr_queuecommand,
4912 .eh_abort_handler = ipr_eh_abort,
4913 .eh_device_reset_handler = ipr_eh_dev_reset,
4914 .eh_host_reset_handler = ipr_eh_host_reset,
4915 .slave_alloc = ipr_slave_alloc,
4916 .slave_configure = ipr_slave_configure,
4917 .slave_destroy = ipr_slave_destroy,
Brian King35a39692006-09-25 12:39:20 -05004918 .target_alloc = ipr_target_alloc,
4919 .target_destroy = ipr_target_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 .change_queue_depth = ipr_change_queue_depth,
4921 .change_queue_type = ipr_change_queue_type,
4922 .bios_param = ipr_biosparam,
4923 .can_queue = IPR_MAX_COMMANDS,
4924 .this_id = -1,
4925 .sg_tablesize = IPR_MAX_SGLIST,
4926 .max_sectors = IPR_IOA_MAX_SECTORS,
4927 .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
4928 .use_clustering = ENABLE_CLUSTERING,
4929 .shost_attrs = ipr_ioa_attrs,
4930 .sdev_attrs = ipr_dev_attrs,
4931 .proc_name = IPR_NAME
4932};
4933
Brian King35a39692006-09-25 12:39:20 -05004934/**
4935 * ipr_ata_phy_reset - libata phy_reset handler
4936 * @ap: ata port to reset
4937 *
4938 **/
4939static void ipr_ata_phy_reset(struct ata_port *ap)
4940{
4941 unsigned long flags;
4942 struct ipr_sata_port *sata_port = ap->private_data;
4943 struct ipr_resource_entry *res = sata_port->res;
4944 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4945 int rc;
4946
4947 ENTER;
4948 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
4949 while(ioa_cfg->in_reset_reload) {
4950 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
4951 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4952 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
4953 }
4954
4955 if (!ioa_cfg->allow_cmds)
4956 goto out_unlock;
4957
4958 rc = ipr_device_reset(ioa_cfg, res);
4959
4960 if (rc) {
4961 ap->ops->port_disable(ap);
4962 goto out_unlock;
4963 }
4964
4965 switch(res->cfgte.proto) {
4966 case IPR_PROTO_SATA:
4967 case IPR_PROTO_SAS_STP:
4968 ap->device[0].class = ATA_DEV_ATA;
4969 break;
4970 case IPR_PROTO_SATA_ATAPI:
4971 case IPR_PROTO_SAS_STP_ATAPI:
4972 ap->device[0].class = ATA_DEV_ATAPI;
4973 break;
4974 default:
4975 ap->device[0].class = ATA_DEV_UNKNOWN;
4976 ap->ops->port_disable(ap);
4977 break;
4978 };
4979
4980out_unlock:
4981 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
4982 LEAVE;
4983}
4984
4985/**
4986 * ipr_ata_post_internal - Cleanup after an internal command
4987 * @qc: ATA queued command
4988 *
4989 * Return value:
4990 * none
4991 **/
4992static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
4993{
4994 struct ipr_sata_port *sata_port = qc->ap->private_data;
4995 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4996 struct ipr_cmnd *ipr_cmd;
4997 unsigned long flags;
4998
4999 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
Brian King73d98ff2006-11-21 10:27:58 -06005000 while(ioa_cfg->in_reset_reload) {
5001 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5002 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5003 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5004 }
5005
Brian King35a39692006-09-25 12:39:20 -05005006 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
5007 if (ipr_cmd->qc == qc) {
5008 ipr_device_reset(ioa_cfg, sata_port->res);
5009 break;
5010 }
5011 }
5012 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5013}
5014
5015/**
5016 * ipr_tf_read - Read the current ATA taskfile for the ATA port
5017 * @ap: ATA port
5018 * @tf: destination ATA taskfile
5019 *
5020 * Return value:
5021 * none
5022 **/
5023static void ipr_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
5024{
5025 struct ipr_sata_port *sata_port = ap->private_data;
5026 struct ipr_ioasa_gata *g = &sata_port->ioasa;
5027
5028 tf->feature = g->error;
5029 tf->nsect = g->nsect;
5030 tf->lbal = g->lbal;
5031 tf->lbam = g->lbam;
5032 tf->lbah = g->lbah;
5033 tf->device = g->device;
5034 tf->command = g->status;
5035 tf->hob_nsect = g->hob_nsect;
5036 tf->hob_lbal = g->hob_lbal;
5037 tf->hob_lbam = g->hob_lbam;
5038 tf->hob_lbah = g->hob_lbah;
5039 tf->ctl = g->alt_status;
5040}
5041
5042/**
5043 * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
5044 * @regs: destination
5045 * @tf: source ATA taskfile
5046 *
5047 * Return value:
5048 * none
5049 **/
5050static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
5051 struct ata_taskfile *tf)
5052{
5053 regs->feature = tf->feature;
5054 regs->nsect = tf->nsect;
5055 regs->lbal = tf->lbal;
5056 regs->lbam = tf->lbam;
5057 regs->lbah = tf->lbah;
5058 regs->device = tf->device;
5059 regs->command = tf->command;
5060 regs->hob_feature = tf->hob_feature;
5061 regs->hob_nsect = tf->hob_nsect;
5062 regs->hob_lbal = tf->hob_lbal;
5063 regs->hob_lbam = tf->hob_lbam;
5064 regs->hob_lbah = tf->hob_lbah;
5065 regs->ctl = tf->ctl;
5066}
5067
5068/**
5069 * ipr_sata_done - done function for SATA commands
5070 * @ipr_cmd: ipr command struct
5071 *
5072 * This function is invoked by the interrupt handler for
5073 * ops generated by the SCSI mid-layer to SATA devices
5074 *
5075 * Return value:
5076 * none
5077 **/
5078static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
5079{
5080 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5081 struct ata_queued_cmd *qc = ipr_cmd->qc;
5082 struct ipr_sata_port *sata_port = qc->ap->private_data;
5083 struct ipr_resource_entry *res = sata_port->res;
5084 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5085
5086 memcpy(&sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
5087 sizeof(struct ipr_ioasa_gata));
5088 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5089
5090 if (be32_to_cpu(ipr_cmd->ioasa.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
5091 scsi_report_device_reset(ioa_cfg->host, res->cfgte.res_addr.bus,
5092 res->cfgte.res_addr.target);
5093
5094 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5095 qc->err_mask |= __ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5096 else
5097 qc->err_mask |= ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5098 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5099 ata_qc_complete(qc);
5100}
5101
5102/**
5103 * ipr_build_ata_ioadl - Build an ATA scatter/gather list
5104 * @ipr_cmd: ipr command struct
5105 * @qc: ATA queued command
5106 *
5107 **/
5108static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
5109 struct ata_queued_cmd *qc)
5110{
5111 u32 ioadl_flags = 0;
5112 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5113 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5114 int len = qc->nbytes + qc->pad_len;
5115 struct scatterlist *sg;
5116
5117 if (len == 0)
5118 return;
5119
5120 if (qc->dma_dir == DMA_TO_DEVICE) {
5121 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5122 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5123 ioarcb->write_data_transfer_length = cpu_to_be32(len);
5124 ioarcb->write_ioadl_len =
5125 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5126 } else if (qc->dma_dir == DMA_FROM_DEVICE) {
5127 ioadl_flags = IPR_IOADL_FLAGS_READ;
5128 ioarcb->read_data_transfer_length = cpu_to_be32(len);
5129 ioarcb->read_ioadl_len =
5130 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5131 }
5132
5133 ata_for_each_sg(sg, qc) {
5134 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5135 ioadl->address = cpu_to_be32(sg_dma_address(sg));
5136 if (ata_sg_is_last(sg, qc))
5137 ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5138 else
5139 ioadl++;
5140 }
5141}
5142
5143/**
5144 * ipr_qc_issue - Issue a SATA qc to a device
5145 * @qc: queued command
5146 *
5147 * Return value:
5148 * 0 if success
5149 **/
5150static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
5151{
5152 struct ata_port *ap = qc->ap;
5153 struct ipr_sata_port *sata_port = ap->private_data;
5154 struct ipr_resource_entry *res = sata_port->res;
5155 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5156 struct ipr_cmnd *ipr_cmd;
5157 struct ipr_ioarcb *ioarcb;
5158 struct ipr_ioarcb_ata_regs *regs;
5159
5160 if (unlikely(!ioa_cfg->allow_cmds || ioa_cfg->ioa_is_dead))
Brian King0feeed82007-03-29 12:43:43 -05005161 return AC_ERR_SYSTEM;
Brian King35a39692006-09-25 12:39:20 -05005162
5163 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5164 ioarcb = &ipr_cmd->ioarcb;
5165 regs = &ioarcb->add_data.u.regs;
5166
5167 memset(&ioarcb->add_data, 0, sizeof(ioarcb->add_data));
5168 ioarcb->add_cmd_parms_len = cpu_to_be32(sizeof(ioarcb->add_data.u.regs));
5169
5170 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5171 ipr_cmd->qc = qc;
5172 ipr_cmd->done = ipr_sata_done;
5173 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
5174 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
5175 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
5176 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5177 ipr_cmd->dma_use_sg = qc->pad_len ? qc->n_elem + 1 : qc->n_elem;
5178
5179 ipr_build_ata_ioadl(ipr_cmd, qc);
5180 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
5181 ipr_copy_sata_tf(regs, &qc->tf);
5182 memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
5183 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
5184
5185 switch (qc->tf.protocol) {
5186 case ATA_PROT_NODATA:
5187 case ATA_PROT_PIO:
5188 break;
5189
5190 case ATA_PROT_DMA:
5191 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5192 break;
5193
5194 case ATA_PROT_ATAPI:
5195 case ATA_PROT_ATAPI_NODATA:
5196 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5197 break;
5198
5199 case ATA_PROT_ATAPI_DMA:
5200 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5201 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5202 break;
5203
5204 default:
5205 WARN_ON(1);
Brian King0feeed82007-03-29 12:43:43 -05005206 return AC_ERR_INVALID;
Brian King35a39692006-09-25 12:39:20 -05005207 }
5208
5209 mb();
5210 writel(be32_to_cpu(ioarcb->ioarcb_host_pci_addr),
5211 ioa_cfg->regs.ioarrin_reg);
5212 return 0;
5213}
5214
5215/**
5216 * ipr_ata_check_status - Return last ATA status
5217 * @ap: ATA port
5218 *
5219 * Return value:
5220 * ATA status
5221 **/
5222static u8 ipr_ata_check_status(struct ata_port *ap)
5223{
5224 struct ipr_sata_port *sata_port = ap->private_data;
5225 return sata_port->ioasa.status;
5226}
5227
5228/**
5229 * ipr_ata_check_altstatus - Return last ATA altstatus
5230 * @ap: ATA port
5231 *
5232 * Return value:
5233 * Alt ATA status
5234 **/
5235static u8 ipr_ata_check_altstatus(struct ata_port *ap)
5236{
5237 struct ipr_sata_port *sata_port = ap->private_data;
5238 return sata_port->ioasa.alt_status;
5239}
5240
5241static struct ata_port_operations ipr_sata_ops = {
5242 .port_disable = ata_port_disable,
5243 .check_status = ipr_ata_check_status,
5244 .check_altstatus = ipr_ata_check_altstatus,
5245 .dev_select = ata_noop_dev_select,
5246 .phy_reset = ipr_ata_phy_reset,
5247 .post_internal_cmd = ipr_ata_post_internal,
5248 .tf_read = ipr_tf_read,
5249 .qc_prep = ata_noop_qc_prep,
5250 .qc_issue = ipr_qc_issue,
5251 .port_start = ata_sas_port_start,
5252 .port_stop = ata_sas_port_stop
5253};
5254
5255static struct ata_port_info sata_port_info = {
5256 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
5257 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
5258 .pio_mask = 0x10, /* pio4 */
5259 .mwdma_mask = 0x07,
5260 .udma_mask = 0x7f, /* udma0-6 */
5261 .port_ops = &ipr_sata_ops
5262};
5263
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264#ifdef CONFIG_PPC_PSERIES
5265static const u16 ipr_blocked_processors[] = {
5266 PV_NORTHSTAR,
5267 PV_PULSAR,
5268 PV_POWER4,
5269 PV_ICESTAR,
5270 PV_SSTAR,
5271 PV_POWER4p,
5272 PV_630,
5273 PV_630p
5274};
5275
5276/**
5277 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
5278 * @ioa_cfg: ioa cfg struct
5279 *
5280 * Adapters that use Gemstone revision < 3.1 do not work reliably on
5281 * certain pSeries hardware. This function determines if the given
5282 * adapter is in one of these confgurations or not.
5283 *
5284 * Return value:
5285 * 1 if adapter is not supported / 0 if adapter is supported
5286 **/
5287static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
5288{
5289 u8 rev_id;
5290 int i;
5291
5292 if (ioa_cfg->type == 0x5702) {
5293 if (pci_read_config_byte(ioa_cfg->pdev, PCI_REVISION_ID,
5294 &rev_id) == PCIBIOS_SUCCESSFUL) {
5295 if (rev_id < 4) {
5296 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
5297 if (__is_processor(ipr_blocked_processors[i]))
5298 return 1;
5299 }
5300 }
5301 }
5302 }
5303 return 0;
5304}
5305#else
5306#define ipr_invalid_adapter(ioa_cfg) 0
5307#endif
5308
5309/**
5310 * ipr_ioa_bringdown_done - IOA bring down completion.
5311 * @ipr_cmd: ipr command struct
5312 *
5313 * This function processes the completion of an adapter bring down.
5314 * It wakes any reset sleepers.
5315 *
5316 * Return value:
5317 * IPR_RC_JOB_RETURN
5318 **/
5319static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
5320{
5321 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5322
5323 ENTER;
5324 ioa_cfg->in_reset_reload = 0;
5325 ioa_cfg->reset_retries = 0;
5326 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5327 wake_up_all(&ioa_cfg->reset_wait_q);
5328
5329 spin_unlock_irq(ioa_cfg->host->host_lock);
5330 scsi_unblock_requests(ioa_cfg->host);
5331 spin_lock_irq(ioa_cfg->host->host_lock);
5332 LEAVE;
5333
5334 return IPR_RC_JOB_RETURN;
5335}
5336
5337/**
5338 * ipr_ioa_reset_done - IOA reset completion.
5339 * @ipr_cmd: ipr command struct
5340 *
5341 * This function processes the completion of an adapter reset.
5342 * It schedules any necessary mid-layer add/removes and
5343 * wakes any reset sleepers.
5344 *
5345 * Return value:
5346 * IPR_RC_JOB_RETURN
5347 **/
5348static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
5349{
5350 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5351 struct ipr_resource_entry *res;
5352 struct ipr_hostrcb *hostrcb, *temp;
5353 int i = 0;
5354
5355 ENTER;
5356 ioa_cfg->in_reset_reload = 0;
5357 ioa_cfg->allow_cmds = 1;
5358 ioa_cfg->reset_cmd = NULL;
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06005359 ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
5361 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
5362 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
5363 ipr_trace;
5364 break;
5365 }
5366 }
5367 schedule_work(&ioa_cfg->work_q);
5368
5369 list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
5370 list_del(&hostrcb->queue);
5371 if (i++ < IPR_NUM_LOG_HCAMS)
5372 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
5373 else
5374 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
5375 }
5376
Brian King6bb04172007-04-26 16:00:08 -05005377 scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
5379
5380 ioa_cfg->reset_retries = 0;
5381 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5382 wake_up_all(&ioa_cfg->reset_wait_q);
5383
5384 spin_unlock_irq(ioa_cfg->host->host_lock);
5385 scsi_unblock_requests(ioa_cfg->host);
5386 spin_lock_irq(ioa_cfg->host->host_lock);
5387
5388 if (!ioa_cfg->allow_cmds)
5389 scsi_block_requests(ioa_cfg->host);
5390
5391 LEAVE;
5392 return IPR_RC_JOB_RETURN;
5393}
5394
5395/**
5396 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
5397 * @supported_dev: supported device struct
5398 * @vpids: vendor product id struct
5399 *
5400 * Return value:
5401 * none
5402 **/
5403static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
5404 struct ipr_std_inq_vpids *vpids)
5405{
5406 memset(supported_dev, 0, sizeof(struct ipr_supported_device));
5407 memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
5408 supported_dev->num_records = 1;
5409 supported_dev->data_length =
5410 cpu_to_be16(sizeof(struct ipr_supported_device));
5411 supported_dev->reserved = 0;
5412}
5413
5414/**
5415 * ipr_set_supported_devs - Send Set Supported Devices for a device
5416 * @ipr_cmd: ipr command struct
5417 *
5418 * This function send a Set Supported Devices to the adapter
5419 *
5420 * Return value:
5421 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5422 **/
5423static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
5424{
5425 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5426 struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
5427 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5428 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5429 struct ipr_resource_entry *res = ipr_cmd->u.res;
5430
5431 ipr_cmd->job_step = ipr_ioa_reset_done;
5432
5433 list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
Brian Kinge4fbf442006-03-29 09:37:22 -06005434 if (!ipr_is_scsi_disk(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 continue;
5436
5437 ipr_cmd->u.res = res;
5438 ipr_set_sup_dev_dflt(supp_dev, &res->cfgte.std_inq_data.vpids);
5439
5440 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5441 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5442 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5443
5444 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
5445 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
5446 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
5447
5448 ioadl->flags_and_data_len = cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST |
5449 sizeof(struct ipr_supported_device));
5450 ioadl->address = cpu_to_be32(ioa_cfg->vpd_cbs_dma +
5451 offsetof(struct ipr_misc_cbs, supp_dev));
5452 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5453 ioarcb->write_data_transfer_length =
5454 cpu_to_be32(sizeof(struct ipr_supported_device));
5455
5456 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
5457 IPR_SET_SUP_DEVICE_TIMEOUT);
5458
5459 ipr_cmd->job_step = ipr_set_supported_devs;
5460 return IPR_RC_JOB_RETURN;
5461 }
5462
5463 return IPR_RC_JOB_CONTINUE;
5464}
5465
5466/**
brking@us.ibm.com62275042005-11-01 17:01:14 -06005467 * ipr_setup_write_cache - Disable write cache if needed
5468 * @ipr_cmd: ipr command struct
5469 *
5470 * This function sets up adapters write cache to desired setting
5471 *
5472 * Return value:
5473 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5474 **/
5475static int ipr_setup_write_cache(struct ipr_cmnd *ipr_cmd)
5476{
5477 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5478
5479 ipr_cmd->job_step = ipr_set_supported_devs;
5480 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
5481 struct ipr_resource_entry, queue);
5482
5483 if (ioa_cfg->cache_state != CACHE_DISABLED)
5484 return IPR_RC_JOB_CONTINUE;
5485
5486 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5487 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5488 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
5489 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
5490
5491 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5492
5493 return IPR_RC_JOB_RETURN;
5494}
5495
5496/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 * ipr_get_mode_page - Locate specified mode page
5498 * @mode_pages: mode page buffer
5499 * @page_code: page code to find
5500 * @len: minimum required length for mode page
5501 *
5502 * Return value:
5503 * pointer to mode page / NULL on failure
5504 **/
5505static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
5506 u32 page_code, u32 len)
5507{
5508 struct ipr_mode_page_hdr *mode_hdr;
5509 u32 page_length;
5510 u32 length;
5511
5512 if (!mode_pages || (mode_pages->hdr.length == 0))
5513 return NULL;
5514
5515 length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
5516 mode_hdr = (struct ipr_mode_page_hdr *)
5517 (mode_pages->data + mode_pages->hdr.block_desc_len);
5518
5519 while (length) {
5520 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
5521 if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
5522 return mode_hdr;
5523 break;
5524 } else {
5525 page_length = (sizeof(struct ipr_mode_page_hdr) +
5526 mode_hdr->page_length);
5527 length -= page_length;
5528 mode_hdr = (struct ipr_mode_page_hdr *)
5529 ((unsigned long)mode_hdr + page_length);
5530 }
5531 }
5532 return NULL;
5533}
5534
5535/**
5536 * ipr_check_term_power - Check for term power errors
5537 * @ioa_cfg: ioa config struct
5538 * @mode_pages: IOAFP mode pages buffer
5539 *
5540 * Check the IOAFP's mode page 28 for term power errors
5541 *
5542 * Return value:
5543 * nothing
5544 **/
5545static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
5546 struct ipr_mode_pages *mode_pages)
5547{
5548 int i;
5549 int entry_length;
5550 struct ipr_dev_bus_entry *bus;
5551 struct ipr_mode_page28 *mode_page;
5552
5553 mode_page = ipr_get_mode_page(mode_pages, 0x28,
5554 sizeof(struct ipr_mode_page28));
5555
5556 entry_length = mode_page->entry_length;
5557
5558 bus = mode_page->bus;
5559
5560 for (i = 0; i < mode_page->num_entries; i++) {
5561 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
5562 dev_err(&ioa_cfg->pdev->dev,
5563 "Term power is absent on scsi bus %d\n",
5564 bus->res_addr.bus);
5565 }
5566
5567 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
5568 }
5569}
5570
5571/**
5572 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
5573 * @ioa_cfg: ioa config struct
5574 *
5575 * Looks through the config table checking for SES devices. If
5576 * the SES device is in the SES table indicating a maximum SCSI
5577 * bus speed, the speed is limited for the bus.
5578 *
5579 * Return value:
5580 * none
5581 **/
5582static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
5583{
5584 u32 max_xfer_rate;
5585 int i;
5586
5587 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
5588 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
5589 ioa_cfg->bus_attr[i].bus_width);
5590
5591 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
5592 ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
5593 }
5594}
5595
5596/**
5597 * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
5598 * @ioa_cfg: ioa config struct
5599 * @mode_pages: mode page 28 buffer
5600 *
5601 * Updates mode page 28 based on driver configuration
5602 *
5603 * Return value:
5604 * none
5605 **/
5606static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
5607 struct ipr_mode_pages *mode_pages)
5608{
5609 int i, entry_length;
5610 struct ipr_dev_bus_entry *bus;
5611 struct ipr_bus_attributes *bus_attr;
5612 struct ipr_mode_page28 *mode_page;
5613
5614 mode_page = ipr_get_mode_page(mode_pages, 0x28,
5615 sizeof(struct ipr_mode_page28));
5616
5617 entry_length = mode_page->entry_length;
5618
5619 /* Loop for each device bus entry */
5620 for (i = 0, bus = mode_page->bus;
5621 i < mode_page->num_entries;
5622 i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
5623 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
5624 dev_err(&ioa_cfg->pdev->dev,
5625 "Invalid resource address reported: 0x%08X\n",
5626 IPR_GET_PHYS_LOC(bus->res_addr));
5627 continue;
5628 }
5629
5630 bus_attr = &ioa_cfg->bus_attr[i];
5631 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
5632 bus->bus_width = bus_attr->bus_width;
5633 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
5634 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
5635 if (bus_attr->qas_enabled)
5636 bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
5637 else
5638 bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
5639 }
5640}
5641
5642/**
5643 * ipr_build_mode_select - Build a mode select command
5644 * @ipr_cmd: ipr command struct
5645 * @res_handle: resource handle to send command to
5646 * @parm: Byte 2 of Mode Sense command
5647 * @dma_addr: DMA buffer address
5648 * @xfer_len: data transfer length
5649 *
5650 * Return value:
5651 * none
5652 **/
5653static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
5654 __be32 res_handle, u8 parm, u32 dma_addr,
5655 u8 xfer_len)
5656{
5657 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5658 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5659
5660 ioarcb->res_handle = res_handle;
5661 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5662 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5663 ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
5664 ioarcb->cmd_pkt.cdb[1] = parm;
5665 ioarcb->cmd_pkt.cdb[4] = xfer_len;
5666
5667 ioadl->flags_and_data_len =
5668 cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST | xfer_len);
5669 ioadl->address = cpu_to_be32(dma_addr);
5670 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5671 ioarcb->write_data_transfer_length = cpu_to_be32(xfer_len);
5672}
5673
5674/**
5675 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
5676 * @ipr_cmd: ipr command struct
5677 *
5678 * This function sets up the SCSI bus attributes and sends
5679 * a Mode Select for Page 28 to activate them.
5680 *
5681 * Return value:
5682 * IPR_RC_JOB_RETURN
5683 **/
5684static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
5685{
5686 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5687 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
5688 int length;
5689
5690 ENTER;
Brian King47338042006-02-08 20:57:42 -06005691 ipr_scsi_bus_speed_limit(ioa_cfg);
5692 ipr_check_term_power(ioa_cfg, mode_pages);
5693 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
5694 length = mode_pages->hdr.length + 1;
5695 mode_pages->hdr.length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
5698 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
5699 length);
5700
brking@us.ibm.com62275042005-11-01 17:01:14 -06005701 ipr_cmd->job_step = ipr_setup_write_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5703
5704 LEAVE;
5705 return IPR_RC_JOB_RETURN;
5706}
5707
5708/**
5709 * ipr_build_mode_sense - Builds a mode sense command
5710 * @ipr_cmd: ipr command struct
5711 * @res: resource entry struct
5712 * @parm: Byte 2 of mode sense command
5713 * @dma_addr: DMA address of mode sense buffer
5714 * @xfer_len: Size of DMA buffer
5715 *
5716 * Return value:
5717 * none
5718 **/
5719static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
5720 __be32 res_handle,
5721 u8 parm, u32 dma_addr, u8 xfer_len)
5722{
5723 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5724 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5725
5726 ioarcb->res_handle = res_handle;
5727 ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
5728 ioarcb->cmd_pkt.cdb[2] = parm;
5729 ioarcb->cmd_pkt.cdb[4] = xfer_len;
5730 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5731
5732 ioadl->flags_and_data_len =
5733 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
5734 ioadl->address = cpu_to_be32(dma_addr);
5735 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5736 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
5737}
5738
5739/**
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06005740 * ipr_reset_cmd_failed - Handle failure of IOA reset command
5741 * @ipr_cmd: ipr command struct
5742 *
5743 * This function handles the failure of an IOA bringup command.
5744 *
5745 * Return value:
5746 * IPR_RC_JOB_RETURN
5747 **/
5748static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
5749{
5750 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5751 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5752
5753 dev_err(&ioa_cfg->pdev->dev,
5754 "0x%02X failed with IOASC: 0x%08X\n",
5755 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
5756
5757 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5758 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5759 return IPR_RC_JOB_RETURN;
5760}
5761
5762/**
5763 * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
5764 * @ipr_cmd: ipr command struct
5765 *
5766 * This function handles the failure of a Mode Sense to the IOAFP.
5767 * Some adapters do not handle all mode pages.
5768 *
5769 * Return value:
5770 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5771 **/
5772static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
5773{
5774 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5775
5776 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
5777 ipr_cmd->job_step = ipr_setup_write_cache;
5778 return IPR_RC_JOB_CONTINUE;
5779 }
5780
5781 return ipr_reset_cmd_failed(ipr_cmd);
5782}
5783
5784/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
5786 * @ipr_cmd: ipr command struct
5787 *
5788 * This function send a Page 28 mode sense to the IOA to
5789 * retrieve SCSI bus attributes.
5790 *
5791 * Return value:
5792 * IPR_RC_JOB_RETURN
5793 **/
5794static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
5795{
5796 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5797
5798 ENTER;
5799 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
5800 0x28, ioa_cfg->vpd_cbs_dma +
5801 offsetof(struct ipr_misc_cbs, mode_pages),
5802 sizeof(struct ipr_mode_pages));
5803
5804 ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06005805 ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
5807 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5808
5809 LEAVE;
5810 return IPR_RC_JOB_RETURN;
5811}
5812
5813/**
5814 * ipr_init_res_table - Initialize the resource table
5815 * @ipr_cmd: ipr command struct
5816 *
5817 * This function looks through the existing resource table, comparing
5818 * it with the config table. This function will take care of old/new
5819 * devices and schedule adding/removing them from the mid-layer
5820 * as appropriate.
5821 *
5822 * Return value:
5823 * IPR_RC_JOB_CONTINUE
5824 **/
5825static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
5826{
5827 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5828 struct ipr_resource_entry *res, *temp;
5829 struct ipr_config_table_entry *cfgte;
5830 int found, i;
5831 LIST_HEAD(old_res);
5832
5833 ENTER;
5834 if (ioa_cfg->cfg_table->hdr.flags & IPR_UCODE_DOWNLOAD_REQ)
5835 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
5836
5837 list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
5838 list_move_tail(&res->queue, &old_res);
5839
5840 for (i = 0; i < ioa_cfg->cfg_table->hdr.num_entries; i++) {
5841 cfgte = &ioa_cfg->cfg_table->dev[i];
5842 found = 0;
5843
5844 list_for_each_entry_safe(res, temp, &old_res, queue) {
5845 if (!memcmp(&res->cfgte.res_addr,
5846 &cfgte->res_addr, sizeof(cfgte->res_addr))) {
5847 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5848 found = 1;
5849 break;
5850 }
5851 }
5852
5853 if (!found) {
5854 if (list_empty(&ioa_cfg->free_res_q)) {
5855 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
5856 break;
5857 }
5858
5859 found = 1;
5860 res = list_entry(ioa_cfg->free_res_q.next,
5861 struct ipr_resource_entry, queue);
5862 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5863 ipr_init_res_entry(res);
5864 res->add_to_ml = 1;
5865 }
5866
5867 if (found)
5868 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
5869 }
5870
5871 list_for_each_entry_safe(res, temp, &old_res, queue) {
5872 if (res->sdev) {
5873 res->del_from_ml = 1;
Brian King1121b792006-03-29 09:37:16 -06005874 res->cfgte.res_handle = IPR_INVALID_RES_HANDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5876 } else {
5877 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
5878 }
5879 }
5880
5881 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
5882
5883 LEAVE;
5884 return IPR_RC_JOB_CONTINUE;
5885}
5886
5887/**
5888 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
5889 * @ipr_cmd: ipr command struct
5890 *
5891 * This function sends a Query IOA Configuration command
5892 * to the adapter to retrieve the IOA configuration table.
5893 *
5894 * Return value:
5895 * IPR_RC_JOB_RETURN
5896 **/
5897static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
5898{
5899 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5900 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5901 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5902 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
5903
5904 ENTER;
5905 dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
5906 ucode_vpd->major_release, ucode_vpd->card_type,
5907 ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
5908 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5909 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5910
5911 ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
5912 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_config_table) >> 8) & 0xff;
5913 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_config_table) & 0xff;
5914
5915 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5916 ioarcb->read_data_transfer_length =
5917 cpu_to_be32(sizeof(struct ipr_config_table));
5918
5919 ioadl->address = cpu_to_be32(ioa_cfg->cfg_table_dma);
5920 ioadl->flags_and_data_len =
5921 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(struct ipr_config_table));
5922
5923 ipr_cmd->job_step = ipr_init_res_table;
5924
5925 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5926
5927 LEAVE;
5928 return IPR_RC_JOB_RETURN;
5929}
5930
5931/**
5932 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
5933 * @ipr_cmd: ipr command struct
5934 *
5935 * This utility function sends an inquiry to the adapter.
5936 *
5937 * Return value:
5938 * none
5939 **/
5940static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
5941 u32 dma_addr, u8 xfer_len)
5942{
5943 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5944 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5945
5946 ENTER;
5947 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5948 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5949
5950 ioarcb->cmd_pkt.cdb[0] = INQUIRY;
5951 ioarcb->cmd_pkt.cdb[1] = flags;
5952 ioarcb->cmd_pkt.cdb[2] = page;
5953 ioarcb->cmd_pkt.cdb[4] = xfer_len;
5954
5955 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5956 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
5957
5958 ioadl->address = cpu_to_be32(dma_addr);
5959 ioadl->flags_and_data_len =
5960 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
5961
5962 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5963 LEAVE;
5964}
5965
5966/**
brking@us.ibm.com62275042005-11-01 17:01:14 -06005967 * ipr_inquiry_page_supported - Is the given inquiry page supported
5968 * @page0: inquiry page 0 buffer
5969 * @page: page code.
5970 *
5971 * This function determines if the specified inquiry page is supported.
5972 *
5973 * Return value:
5974 * 1 if page is supported / 0 if not
5975 **/
5976static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
5977{
5978 int i;
5979
5980 for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
5981 if (page0->page[i] == page)
5982 return 1;
5983
5984 return 0;
5985}
5986
5987/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
5989 * @ipr_cmd: ipr command struct
5990 *
5991 * This function sends a Page 3 inquiry to the adapter
5992 * to retrieve software VPD information.
5993 *
5994 * Return value:
5995 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5996 **/
5997static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
5998{
5999 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006000 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
6001
6002 ENTER;
6003
6004 if (!ipr_inquiry_page_supported(page0, 1))
6005 ioa_cfg->cache_state = CACHE_NONE;
6006
6007 ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
6008
6009 ipr_ioafp_inquiry(ipr_cmd, 1, 3,
6010 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
6011 sizeof(struct ipr_inquiry_page3));
6012
6013 LEAVE;
6014 return IPR_RC_JOB_RETURN;
6015}
6016
6017/**
6018 * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
6019 * @ipr_cmd: ipr command struct
6020 *
6021 * This function sends a Page 0 inquiry to the adapter
6022 * to retrieve supported inquiry pages.
6023 *
6024 * Return value:
6025 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6026 **/
6027static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
6028{
6029 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 char type[5];
6031
6032 ENTER;
6033
6034 /* Grab the type out of the VPD and store it away */
6035 memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
6036 type[4] = '\0';
6037 ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
6038
brking@us.ibm.com62275042005-11-01 17:01:14 -06006039 ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
brking@us.ibm.com62275042005-11-01 17:01:14 -06006041 ipr_ioafp_inquiry(ipr_cmd, 1, 0,
6042 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
6043 sizeof(struct ipr_inquiry_page0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044
6045 LEAVE;
6046 return IPR_RC_JOB_RETURN;
6047}
6048
6049/**
6050 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
6051 * @ipr_cmd: ipr command struct
6052 *
6053 * This function sends a standard inquiry to the adapter.
6054 *
6055 * Return value:
6056 * IPR_RC_JOB_RETURN
6057 **/
6058static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
6059{
6060 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6061
6062 ENTER;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006063 ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
6065 ipr_ioafp_inquiry(ipr_cmd, 0, 0,
6066 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
6067 sizeof(struct ipr_ioa_vpd));
6068
6069 LEAVE;
6070 return IPR_RC_JOB_RETURN;
6071}
6072
6073/**
6074 * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
6075 * @ipr_cmd: ipr command struct
6076 *
6077 * This function send an Identify Host Request Response Queue
6078 * command to establish the HRRQ with the adapter.
6079 *
6080 * Return value:
6081 * IPR_RC_JOB_RETURN
6082 **/
6083static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
6084{
6085 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6086 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6087
6088 ENTER;
6089 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
6090
6091 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
6092 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6093
6094 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6095 ioarcb->cmd_pkt.cdb[2] =
6096 ((u32) ioa_cfg->host_rrq_dma >> 24) & 0xff;
6097 ioarcb->cmd_pkt.cdb[3] =
6098 ((u32) ioa_cfg->host_rrq_dma >> 16) & 0xff;
6099 ioarcb->cmd_pkt.cdb[4] =
6100 ((u32) ioa_cfg->host_rrq_dma >> 8) & 0xff;
6101 ioarcb->cmd_pkt.cdb[5] =
6102 ((u32) ioa_cfg->host_rrq_dma) & 0xff;
6103 ioarcb->cmd_pkt.cdb[7] =
6104 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
6105 ioarcb->cmd_pkt.cdb[8] =
6106 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
6107
6108 ipr_cmd->job_step = ipr_ioafp_std_inquiry;
6109
6110 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6111
6112 LEAVE;
6113 return IPR_RC_JOB_RETURN;
6114}
6115
6116/**
6117 * ipr_reset_timer_done - Adapter reset timer function
6118 * @ipr_cmd: ipr command struct
6119 *
6120 * Description: This function is used in adapter reset processing
6121 * for timing events. If the reset_cmd pointer in the IOA
6122 * config struct is not this adapter's we are doing nested
6123 * resets and fail_all_ops will take care of freeing the
6124 * command block.
6125 *
6126 * Return value:
6127 * none
6128 **/
6129static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
6130{
6131 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6132 unsigned long lock_flags = 0;
6133
6134 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6135
6136 if (ioa_cfg->reset_cmd == ipr_cmd) {
6137 list_del(&ipr_cmd->queue);
6138 ipr_cmd->done(ipr_cmd);
6139 }
6140
6141 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6142}
6143
6144/**
6145 * ipr_reset_start_timer - Start a timer for adapter reset job
6146 * @ipr_cmd: ipr command struct
6147 * @timeout: timeout value
6148 *
6149 * Description: This function is used in adapter reset processing
6150 * for timing events. If the reset_cmd pointer in the IOA
6151 * config struct is not this adapter's we are doing nested
6152 * resets and fail_all_ops will take care of freeing the
6153 * command block.
6154 *
6155 * Return value:
6156 * none
6157 **/
6158static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
6159 unsigned long timeout)
6160{
6161 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
6162 ipr_cmd->done = ipr_reset_ioa_job;
6163
6164 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
6165 ipr_cmd->timer.expires = jiffies + timeout;
6166 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
6167 add_timer(&ipr_cmd->timer);
6168}
6169
6170/**
6171 * ipr_init_ioa_mem - Initialize ioa_cfg control block
6172 * @ioa_cfg: ioa cfg struct
6173 *
6174 * Return value:
6175 * nothing
6176 **/
6177static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
6178{
6179 memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
6180
6181 /* Initialize Host RRQ pointers */
6182 ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
6183 ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
6184 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
6185 ioa_cfg->toggle_bit = 1;
6186
6187 /* Zero out config table */
6188 memset(ioa_cfg->cfg_table, 0, sizeof(struct ipr_config_table));
6189}
6190
6191/**
6192 * ipr_reset_enable_ioa - Enable the IOA following a reset.
6193 * @ipr_cmd: ipr command struct
6194 *
6195 * This function reinitializes some control blocks and
6196 * enables destructive diagnostics on the adapter.
6197 *
6198 * Return value:
6199 * IPR_RC_JOB_RETURN
6200 **/
6201static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
6202{
6203 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6204 volatile u32 int_reg;
6205
6206 ENTER;
6207 ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
6208 ipr_init_ioa_mem(ioa_cfg);
6209
6210 ioa_cfg->allow_interrupts = 1;
6211 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
6212
6213 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
6214 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
6215 ioa_cfg->regs.clr_interrupt_mask_reg);
6216 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
6217 return IPR_RC_JOB_CONTINUE;
6218 }
6219
6220 /* Enable destructive diagnostics on IOA */
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06006221 writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222
6223 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
6224 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
6225
6226 dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
6227
6228 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
Brian King5469cb52007-03-29 12:42:40 -05006229 ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
6231 ipr_cmd->done = ipr_reset_ioa_job;
6232 add_timer(&ipr_cmd->timer);
6233 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
6234
6235 LEAVE;
6236 return IPR_RC_JOB_RETURN;
6237}
6238
6239/**
6240 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
6241 * @ipr_cmd: ipr command struct
6242 *
6243 * This function is invoked when an adapter dump has run out
6244 * of processing time.
6245 *
6246 * Return value:
6247 * IPR_RC_JOB_CONTINUE
6248 **/
6249static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
6250{
6251 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6252
6253 if (ioa_cfg->sdt_state == GET_DUMP)
6254 ioa_cfg->sdt_state = ABORT_DUMP;
6255
6256 ipr_cmd->job_step = ipr_reset_alert;
6257
6258 return IPR_RC_JOB_CONTINUE;
6259}
6260
6261/**
6262 * ipr_unit_check_no_data - Log a unit check/no data error log
6263 * @ioa_cfg: ioa config struct
6264 *
6265 * Logs an error indicating the adapter unit checked, but for some
6266 * reason, we were unable to fetch the unit check buffer.
6267 *
6268 * Return value:
6269 * nothing
6270 **/
6271static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
6272{
6273 ioa_cfg->errors_logged++;
6274 dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
6275}
6276
6277/**
6278 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
6279 * @ioa_cfg: ioa config struct
6280 *
6281 * Fetches the unit check buffer from the adapter by clocking the data
6282 * through the mailbox register.
6283 *
6284 * Return value:
6285 * nothing
6286 **/
6287static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
6288{
6289 unsigned long mailbox;
6290 struct ipr_hostrcb *hostrcb;
6291 struct ipr_uc_sdt sdt;
6292 int rc, length;
6293
6294 mailbox = readl(ioa_cfg->ioa_mailbox);
6295
6296 if (!ipr_sdt_is_fmt2(mailbox)) {
6297 ipr_unit_check_no_data(ioa_cfg);
6298 return;
6299 }
6300
6301 memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
6302 rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
6303 (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
6304
6305 if (rc || (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE) ||
6306 !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY)) {
6307 ipr_unit_check_no_data(ioa_cfg);
6308 return;
6309 }
6310
6311 /* Find length of the first sdt entry (UC buffer) */
6312 length = (be32_to_cpu(sdt.entry[0].end_offset) -
6313 be32_to_cpu(sdt.entry[0].bar_str_offset)) & IPR_FMT2_MBX_ADDR_MASK;
6314
6315 hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
6316 struct ipr_hostrcb, queue);
6317 list_del(&hostrcb->queue);
6318 memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
6319
6320 rc = ipr_get_ldump_data_section(ioa_cfg,
6321 be32_to_cpu(sdt.entry[0].bar_str_offset),
6322 (__be32 *)&hostrcb->hcam,
6323 min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
6324
6325 if (!rc)
6326 ipr_handle_log_data(ioa_cfg, hostrcb);
6327 else
6328 ipr_unit_check_no_data(ioa_cfg);
6329
6330 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
6331}
6332
6333/**
6334 * ipr_reset_restore_cfg_space - Restore PCI config space.
6335 * @ipr_cmd: ipr command struct
6336 *
6337 * Description: This function restores the saved PCI config space of
6338 * the adapter, fails all outstanding ops back to the callers, and
6339 * fetches the dump/unit check if applicable to this reset.
6340 *
6341 * Return value:
6342 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6343 **/
6344static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
6345{
6346 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6347 int rc;
6348
6349 ENTER;
6350 rc = pci_restore_state(ioa_cfg->pdev);
6351
6352 if (rc != PCIBIOS_SUCCESSFUL) {
6353 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6354 return IPR_RC_JOB_CONTINUE;
6355 }
6356
6357 if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
6358 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6359 return IPR_RC_JOB_CONTINUE;
6360 }
6361
6362 ipr_fail_all_ops(ioa_cfg);
6363
6364 if (ioa_cfg->ioa_unit_checked) {
6365 ioa_cfg->ioa_unit_checked = 0;
6366 ipr_get_unit_check_buffer(ioa_cfg);
6367 ipr_cmd->job_step = ipr_reset_alert;
6368 ipr_reset_start_timer(ipr_cmd, 0);
6369 return IPR_RC_JOB_RETURN;
6370 }
6371
6372 if (ioa_cfg->in_ioa_bringdown) {
6373 ipr_cmd->job_step = ipr_ioa_bringdown_done;
6374 } else {
6375 ipr_cmd->job_step = ipr_reset_enable_ioa;
6376
6377 if (GET_DUMP == ioa_cfg->sdt_state) {
6378 ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
6379 ipr_cmd->job_step = ipr_reset_wait_for_dump;
6380 schedule_work(&ioa_cfg->work_q);
6381 return IPR_RC_JOB_RETURN;
6382 }
6383 }
6384
6385 ENTER;
6386 return IPR_RC_JOB_CONTINUE;
6387}
6388
6389/**
Brian Kinge619e1a2007-01-23 11:25:37 -06006390 * ipr_reset_bist_done - BIST has completed on the adapter.
6391 * @ipr_cmd: ipr command struct
6392 *
6393 * Description: Unblock config space and resume the reset process.
6394 *
6395 * Return value:
6396 * IPR_RC_JOB_CONTINUE
6397 **/
6398static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
6399{
6400 ENTER;
6401 pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
6402 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
6403 LEAVE;
6404 return IPR_RC_JOB_CONTINUE;
6405}
6406
6407/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 * ipr_reset_start_bist - Run BIST on the adapter.
6409 * @ipr_cmd: ipr command struct
6410 *
6411 * Description: This function runs BIST on the adapter, then delays 2 seconds.
6412 *
6413 * Return value:
6414 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6415 **/
6416static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
6417{
6418 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6419 int rc;
6420
6421 ENTER;
Brian Kingb30197d2005-09-27 01:21:56 -07006422 pci_block_user_cfg_access(ioa_cfg->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
6424
6425 if (rc != PCIBIOS_SUCCESSFUL) {
Brian Kinga9aedb02007-03-29 12:43:23 -05006426 pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6428 rc = IPR_RC_JOB_CONTINUE;
6429 } else {
Brian Kinge619e1a2007-01-23 11:25:37 -06006430 ipr_cmd->job_step = ipr_reset_bist_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
6432 rc = IPR_RC_JOB_RETURN;
6433 }
6434
6435 LEAVE;
6436 return rc;
6437}
6438
6439/**
6440 * ipr_reset_allowed - Query whether or not IOA can be reset
6441 * @ioa_cfg: ioa config struct
6442 *
6443 * Return value:
6444 * 0 if reset not allowed / non-zero if reset is allowed
6445 **/
6446static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
6447{
6448 volatile u32 temp_reg;
6449
6450 temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
6451 return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
6452}
6453
6454/**
6455 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
6456 * @ipr_cmd: ipr command struct
6457 *
6458 * Description: This function waits for adapter permission to run BIST,
6459 * then runs BIST. If the adapter does not give permission after a
6460 * reasonable time, we will reset the adapter anyway. The impact of
6461 * resetting the adapter without warning the adapter is the risk of
6462 * losing the persistent error log on the adapter. If the adapter is
6463 * reset while it is writing to the flash on the adapter, the flash
6464 * segment will have bad ECC and be zeroed.
6465 *
6466 * Return value:
6467 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6468 **/
6469static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
6470{
6471 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6472 int rc = IPR_RC_JOB_RETURN;
6473
6474 if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
6475 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
6476 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
6477 } else {
6478 ipr_cmd->job_step = ipr_reset_start_bist;
6479 rc = IPR_RC_JOB_CONTINUE;
6480 }
6481
6482 return rc;
6483}
6484
6485/**
6486 * ipr_reset_alert_part2 - Alert the adapter of a pending reset
6487 * @ipr_cmd: ipr command struct
6488 *
6489 * Description: This function alerts the adapter that it will be reset.
6490 * If memory space is not currently enabled, proceed directly
6491 * to running BIST on the adapter. The timer must always be started
6492 * so we guarantee we do not run BIST from ipr_isr.
6493 *
6494 * Return value:
6495 * IPR_RC_JOB_RETURN
6496 **/
6497static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
6498{
6499 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6500 u16 cmd_reg;
6501 int rc;
6502
6503 ENTER;
6504 rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
6505
6506 if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
6507 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
6508 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg);
6509 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
6510 } else {
6511 ipr_cmd->job_step = ipr_reset_start_bist;
6512 }
6513
6514 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
6515 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
6516
6517 LEAVE;
6518 return IPR_RC_JOB_RETURN;
6519}
6520
6521/**
6522 * ipr_reset_ucode_download_done - Microcode download completion
6523 * @ipr_cmd: ipr command struct
6524 *
6525 * Description: This function unmaps the microcode download buffer.
6526 *
6527 * Return value:
6528 * IPR_RC_JOB_CONTINUE
6529 **/
6530static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
6531{
6532 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6533 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
6534
6535 pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
6536 sglist->num_sg, DMA_TO_DEVICE);
6537
6538 ipr_cmd->job_step = ipr_reset_alert;
6539 return IPR_RC_JOB_CONTINUE;
6540}
6541
6542/**
6543 * ipr_reset_ucode_download - Download microcode to the adapter
6544 * @ipr_cmd: ipr command struct
6545 *
6546 * Description: This function checks to see if it there is microcode
6547 * to download to the adapter. If there is, a download is performed.
6548 *
6549 * Return value:
6550 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6551 **/
6552static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
6553{
6554 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6555 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
6556
6557 ENTER;
6558 ipr_cmd->job_step = ipr_reset_alert;
6559
6560 if (!sglist)
6561 return IPR_RC_JOB_CONTINUE;
6562
6563 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6564 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6565 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
6566 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
6567 ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
6568 ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
6569 ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
6570
brking@us.ibm.com12baa422005-11-01 17:01:27 -06006571 ipr_build_ucode_ioadl(ipr_cmd, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 ipr_cmd->job_step = ipr_reset_ucode_download_done;
6573
6574 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
6575 IPR_WRITE_BUFFER_TIMEOUT);
6576
6577 LEAVE;
6578 return IPR_RC_JOB_RETURN;
6579}
6580
6581/**
6582 * ipr_reset_shutdown_ioa - Shutdown the adapter
6583 * @ipr_cmd: ipr command struct
6584 *
6585 * Description: This function issues an adapter shutdown of the
6586 * specified type to the specified adapter as part of the
6587 * adapter reset job.
6588 *
6589 * Return value:
6590 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6591 **/
6592static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
6593{
6594 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6595 enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
6596 unsigned long timeout;
6597 int rc = IPR_RC_JOB_CONTINUE;
6598
6599 ENTER;
6600 if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
6601 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6602 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6603 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
6604 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
6605
6606 if (shutdown_type == IPR_SHUTDOWN_ABBREV)
6607 timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
6608 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
6609 timeout = IPR_INTERNAL_TIMEOUT;
6610 else
6611 timeout = IPR_SHUTDOWN_TIMEOUT;
6612
6613 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
6614
6615 rc = IPR_RC_JOB_RETURN;
6616 ipr_cmd->job_step = ipr_reset_ucode_download;
6617 } else
6618 ipr_cmd->job_step = ipr_reset_alert;
6619
6620 LEAVE;
6621 return rc;
6622}
6623
6624/**
6625 * ipr_reset_ioa_job - Adapter reset job
6626 * @ipr_cmd: ipr command struct
6627 *
6628 * Description: This function is the job router for the adapter reset job.
6629 *
6630 * Return value:
6631 * none
6632 **/
6633static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
6634{
6635 u32 rc, ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6637
6638 do {
6639 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
6640
6641 if (ioa_cfg->reset_cmd != ipr_cmd) {
6642 /*
6643 * We are doing nested adapter resets and this is
6644 * not the current reset job.
6645 */
6646 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6647 return;
6648 }
6649
6650 if (IPR_IOASC_SENSE_KEY(ioasc)) {
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06006651 rc = ipr_cmd->job_step_failed(ipr_cmd);
6652 if (rc == IPR_RC_JOB_RETURN)
6653 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 }
6655
6656 ipr_reinit_ipr_cmnd(ipr_cmd);
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06006657 ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 rc = ipr_cmd->job_step(ipr_cmd);
6659 } while(rc == IPR_RC_JOB_CONTINUE);
6660}
6661
6662/**
6663 * _ipr_initiate_ioa_reset - Initiate an adapter reset
6664 * @ioa_cfg: ioa config struct
6665 * @job_step: first job step of reset job
6666 * @shutdown_type: shutdown type
6667 *
6668 * Description: This function will initiate the reset of the given adapter
6669 * starting at the selected job step.
6670 * If the caller needs to wait on the completion of the reset,
6671 * the caller must sleep on the reset_wait_q.
6672 *
6673 * Return value:
6674 * none
6675 **/
6676static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
6677 int (*job_step) (struct ipr_cmnd *),
6678 enum ipr_shutdown_type shutdown_type)
6679{
6680 struct ipr_cmnd *ipr_cmd;
6681
6682 ioa_cfg->in_reset_reload = 1;
6683 ioa_cfg->allow_cmds = 0;
6684 scsi_block_requests(ioa_cfg->host);
6685
6686 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
6687 ioa_cfg->reset_cmd = ipr_cmd;
6688 ipr_cmd->job_step = job_step;
6689 ipr_cmd->u.shutdown_type = shutdown_type;
6690
6691 ipr_reset_ioa_job(ipr_cmd);
6692}
6693
6694/**
6695 * ipr_initiate_ioa_reset - Initiate an adapter reset
6696 * @ioa_cfg: ioa config struct
6697 * @shutdown_type: shutdown type
6698 *
6699 * Description: This function will initiate the reset of the given adapter.
6700 * If the caller needs to wait on the completion of the reset,
6701 * the caller must sleep on the reset_wait_q.
6702 *
6703 * Return value:
6704 * none
6705 **/
6706static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
6707 enum ipr_shutdown_type shutdown_type)
6708{
6709 if (ioa_cfg->ioa_is_dead)
6710 return;
6711
6712 if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
6713 ioa_cfg->sdt_state = ABORT_DUMP;
6714
6715 if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
6716 dev_err(&ioa_cfg->pdev->dev,
6717 "IOA taken offline - error recovery failed\n");
6718
6719 ioa_cfg->reset_retries = 0;
6720 ioa_cfg->ioa_is_dead = 1;
6721
6722 if (ioa_cfg->in_ioa_bringdown) {
6723 ioa_cfg->reset_cmd = NULL;
6724 ioa_cfg->in_reset_reload = 0;
6725 ipr_fail_all_ops(ioa_cfg);
6726 wake_up_all(&ioa_cfg->reset_wait_q);
6727
6728 spin_unlock_irq(ioa_cfg->host->host_lock);
6729 scsi_unblock_requests(ioa_cfg->host);
6730 spin_lock_irq(ioa_cfg->host->host_lock);
6731 return;
6732 } else {
6733 ioa_cfg->in_ioa_bringdown = 1;
6734 shutdown_type = IPR_SHUTDOWN_NONE;
6735 }
6736 }
6737
6738 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
6739 shutdown_type);
6740}
6741
6742/**
Linas Vepstasf8a88b192006-02-03 16:52:42 -06006743 * ipr_reset_freeze - Hold off all I/O activity
6744 * @ipr_cmd: ipr command struct
6745 *
6746 * Description: If the PCI slot is frozen, hold off all I/O
6747 * activity; then, as soon as the slot is available again,
6748 * initiate an adapter reset.
6749 */
6750static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
6751{
6752 /* Disallow new interrupts, avoid loop */
6753 ipr_cmd->ioa_cfg->allow_interrupts = 0;
6754 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
6755 ipr_cmd->done = ipr_reset_ioa_job;
6756 return IPR_RC_JOB_RETURN;
6757}
6758
6759/**
6760 * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
6761 * @pdev: PCI device struct
6762 *
6763 * Description: This routine is called to tell us that the PCI bus
6764 * is down. Can't do anything here, except put the device driver
6765 * into a holding pattern, waiting for the PCI bus to come back.
6766 */
6767static void ipr_pci_frozen(struct pci_dev *pdev)
6768{
6769 unsigned long flags = 0;
6770 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6771
6772 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6773 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
6774 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6775}
6776
6777/**
6778 * ipr_pci_slot_reset - Called when PCI slot has been reset.
6779 * @pdev: PCI device struct
6780 *
6781 * Description: This routine is called by the pci error recovery
6782 * code after the PCI slot has been reset, just before we
6783 * should resume normal operations.
6784 */
6785static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
6786{
6787 unsigned long flags = 0;
6788 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6789
6790 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6791 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
6792 IPR_SHUTDOWN_NONE);
6793 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6794 return PCI_ERS_RESULT_RECOVERED;
6795}
6796
6797/**
6798 * ipr_pci_perm_failure - Called when PCI slot is dead for good.
6799 * @pdev: PCI device struct
6800 *
6801 * Description: This routine is called when the PCI bus has
6802 * permanently failed.
6803 */
6804static void ipr_pci_perm_failure(struct pci_dev *pdev)
6805{
6806 unsigned long flags = 0;
6807 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6808
6809 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6810 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
6811 ioa_cfg->sdt_state = ABORT_DUMP;
6812 ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES;
6813 ioa_cfg->in_ioa_bringdown = 1;
6814 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
6815 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6816}
6817
6818/**
6819 * ipr_pci_error_detected - Called when a PCI error is detected.
6820 * @pdev: PCI device struct
6821 * @state: PCI channel state
6822 *
6823 * Description: Called when a PCI error is detected.
6824 *
6825 * Return value:
6826 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
6827 */
6828static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
6829 pci_channel_state_t state)
6830{
6831 switch (state) {
6832 case pci_channel_io_frozen:
6833 ipr_pci_frozen(pdev);
6834 return PCI_ERS_RESULT_NEED_RESET;
6835 case pci_channel_io_perm_failure:
6836 ipr_pci_perm_failure(pdev);
6837 return PCI_ERS_RESULT_DISCONNECT;
6838 break;
6839 default:
6840 break;
6841 }
6842 return PCI_ERS_RESULT_NEED_RESET;
6843}
6844
6845/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
6847 * @ioa_cfg: ioa cfg struct
6848 *
6849 * Description: This is the second phase of adapter intialization
6850 * This function takes care of initilizing the adapter to the point
6851 * where it can accept new commands.
6852
6853 * Return value:
6854 * 0 on sucess / -EIO on failure
6855 **/
6856static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
6857{
6858 int rc = 0;
6859 unsigned long host_lock_flags = 0;
6860
6861 ENTER;
6862 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
6863 dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06006864 if (ioa_cfg->needs_hard_reset) {
6865 ioa_cfg->needs_hard_reset = 0;
6866 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
6867 } else
6868 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
6869 IPR_SHUTDOWN_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870
6871 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
6872 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
6873 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
6874
6875 if (ioa_cfg->ioa_is_dead) {
6876 rc = -EIO;
6877 } else if (ipr_invalid_adapter(ioa_cfg)) {
6878 if (!ipr_testmode)
6879 rc = -EIO;
6880
6881 dev_err(&ioa_cfg->pdev->dev,
6882 "Adapter not supported in this hardware configuration.\n");
6883 }
6884
6885 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
6886
6887 LEAVE;
6888 return rc;
6889}
6890
6891/**
6892 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
6893 * @ioa_cfg: ioa config struct
6894 *
6895 * Return value:
6896 * none
6897 **/
6898static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
6899{
6900 int i;
6901
6902 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
6903 if (ioa_cfg->ipr_cmnd_list[i])
6904 pci_pool_free(ioa_cfg->ipr_cmd_pool,
6905 ioa_cfg->ipr_cmnd_list[i],
6906 ioa_cfg->ipr_cmnd_list_dma[i]);
6907
6908 ioa_cfg->ipr_cmnd_list[i] = NULL;
6909 }
6910
6911 if (ioa_cfg->ipr_cmd_pool)
6912 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
6913
6914 ioa_cfg->ipr_cmd_pool = NULL;
6915}
6916
6917/**
6918 * ipr_free_mem - Frees memory allocated for an adapter
6919 * @ioa_cfg: ioa cfg struct
6920 *
6921 * Return value:
6922 * nothing
6923 **/
6924static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
6925{
6926 int i;
6927
6928 kfree(ioa_cfg->res_entries);
6929 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
6930 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
6931 ipr_free_cmd_blks(ioa_cfg);
6932 pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
6933 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
6934 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_config_table),
6935 ioa_cfg->cfg_table,
6936 ioa_cfg->cfg_table_dma);
6937
6938 for (i = 0; i < IPR_NUM_HCAMS; i++) {
6939 pci_free_consistent(ioa_cfg->pdev,
6940 sizeof(struct ipr_hostrcb),
6941 ioa_cfg->hostrcb[i],
6942 ioa_cfg->hostrcb_dma[i]);
6943 }
6944
6945 ipr_free_dump(ioa_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 kfree(ioa_cfg->trace);
6947}
6948
6949/**
6950 * ipr_free_all_resources - Free all allocated resources for an adapter.
6951 * @ipr_cmd: ipr command struct
6952 *
6953 * This function frees all allocated resources for the
6954 * specified adapter.
6955 *
6956 * Return value:
6957 * none
6958 **/
6959static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
6960{
6961 struct pci_dev *pdev = ioa_cfg->pdev;
6962
6963 ENTER;
6964 free_irq(pdev->irq, ioa_cfg);
6965 iounmap(ioa_cfg->hdw_dma_regs);
6966 pci_release_regions(pdev);
6967 ipr_free_mem(ioa_cfg);
6968 scsi_host_put(ioa_cfg->host);
6969 pci_disable_device(pdev);
6970 LEAVE;
6971}
6972
6973/**
6974 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
6975 * @ioa_cfg: ioa config struct
6976 *
6977 * Return value:
6978 * 0 on success / -ENOMEM on allocation failure
6979 **/
6980static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
6981{
6982 struct ipr_cmnd *ipr_cmd;
6983 struct ipr_ioarcb *ioarcb;
6984 dma_addr_t dma_addr;
6985 int i;
6986
6987 ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
6988 sizeof(struct ipr_cmnd), 8, 0);
6989
6990 if (!ioa_cfg->ipr_cmd_pool)
6991 return -ENOMEM;
6992
6993 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
Christoph Lametere94b1762006-12-06 20:33:17 -08006994 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995
6996 if (!ipr_cmd) {
6997 ipr_free_cmd_blks(ioa_cfg);
6998 return -ENOMEM;
6999 }
7000
7001 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
7002 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
7003 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
7004
7005 ioarcb = &ipr_cmd->ioarcb;
7006 ioarcb->ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
7007 ioarcb->host_response_handle = cpu_to_be32(i << 2);
7008 ioarcb->write_ioadl_addr =
7009 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
7010 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
7011 ioarcb->ioasa_host_pci_addr =
7012 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
7013 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
7014 ipr_cmd->cmd_index = i;
7015 ipr_cmd->ioa_cfg = ioa_cfg;
7016 ipr_cmd->sense_buffer_dma = dma_addr +
7017 offsetof(struct ipr_cmnd, sense_buffer);
7018
7019 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
7020 }
7021
7022 return 0;
7023}
7024
7025/**
7026 * ipr_alloc_mem - Allocate memory for an adapter
7027 * @ioa_cfg: ioa config struct
7028 *
7029 * Return value:
7030 * 0 on success / non-zero for error
7031 **/
7032static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
7033{
7034 struct pci_dev *pdev = ioa_cfg->pdev;
7035 int i, rc = -ENOMEM;
7036
7037 ENTER;
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06007038 ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 IPR_MAX_PHYSICAL_DEVS, GFP_KERNEL);
7040
7041 if (!ioa_cfg->res_entries)
7042 goto out;
7043
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 for (i = 0; i < IPR_MAX_PHYSICAL_DEVS; i++)
7045 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
7046
7047 ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
7048 sizeof(struct ipr_misc_cbs),
7049 &ioa_cfg->vpd_cbs_dma);
7050
7051 if (!ioa_cfg->vpd_cbs)
7052 goto out_free_res_entries;
7053
7054 if (ipr_alloc_cmd_blks(ioa_cfg))
7055 goto out_free_vpd_cbs;
7056
7057 ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
7058 sizeof(u32) * IPR_NUM_CMD_BLKS,
7059 &ioa_cfg->host_rrq_dma);
7060
7061 if (!ioa_cfg->host_rrq)
7062 goto out_ipr_free_cmd_blocks;
7063
7064 ioa_cfg->cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
7065 sizeof(struct ipr_config_table),
7066 &ioa_cfg->cfg_table_dma);
7067
7068 if (!ioa_cfg->cfg_table)
7069 goto out_free_host_rrq;
7070
7071 for (i = 0; i < IPR_NUM_HCAMS; i++) {
7072 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
7073 sizeof(struct ipr_hostrcb),
7074 &ioa_cfg->hostrcb_dma[i]);
7075
7076 if (!ioa_cfg->hostrcb[i])
7077 goto out_free_hostrcb_dma;
7078
7079 ioa_cfg->hostrcb[i]->hostrcb_dma =
7080 ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
Brian King49dc6a12006-11-21 10:28:35 -06007081 ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
7083 }
7084
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06007085 ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
7087
7088 if (!ioa_cfg->trace)
7089 goto out_free_hostrcb_dma;
7090
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091 rc = 0;
7092out:
7093 LEAVE;
7094 return rc;
7095
7096out_free_hostrcb_dma:
7097 while (i-- > 0) {
7098 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
7099 ioa_cfg->hostrcb[i],
7100 ioa_cfg->hostrcb_dma[i]);
7101 }
7102 pci_free_consistent(pdev, sizeof(struct ipr_config_table),
7103 ioa_cfg->cfg_table, ioa_cfg->cfg_table_dma);
7104out_free_host_rrq:
7105 pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
7106 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
7107out_ipr_free_cmd_blocks:
7108 ipr_free_cmd_blks(ioa_cfg);
7109out_free_vpd_cbs:
7110 pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
7111 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
7112out_free_res_entries:
7113 kfree(ioa_cfg->res_entries);
7114 goto out;
7115}
7116
7117/**
7118 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
7119 * @ioa_cfg: ioa config struct
7120 *
7121 * Return value:
7122 * none
7123 **/
7124static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
7125{
7126 int i;
7127
7128 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
7129 ioa_cfg->bus_attr[i].bus = i;
7130 ioa_cfg->bus_attr[i].qas_enabled = 0;
7131 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
7132 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
7133 ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
7134 else
7135 ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
7136 }
7137}
7138
7139/**
7140 * ipr_init_ioa_cfg - Initialize IOA config struct
7141 * @ioa_cfg: ioa config struct
7142 * @host: scsi host struct
7143 * @pdev: PCI dev struct
7144 *
7145 * Return value:
7146 * none
7147 **/
7148static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
7149 struct Scsi_Host *host, struct pci_dev *pdev)
7150{
7151 const struct ipr_interrupt_offsets *p;
7152 struct ipr_interrupts *t;
7153 void __iomem *base;
7154
7155 ioa_cfg->host = host;
7156 ioa_cfg->pdev = pdev;
7157 ioa_cfg->log_level = ipr_log_level;
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06007158 ioa_cfg->doorbell = IPR_DOORBELL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
7160 sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
7161 sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
7162 sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
7163 sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
7164 sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
7165 sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
7166 sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
7167
7168 INIT_LIST_HEAD(&ioa_cfg->free_q);
7169 INIT_LIST_HEAD(&ioa_cfg->pending_q);
7170 INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
7171 INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
7172 INIT_LIST_HEAD(&ioa_cfg->free_res_q);
7173 INIT_LIST_HEAD(&ioa_cfg->used_res_q);
David Howellsc4028952006-11-22 14:57:56 +00007174 INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 init_waitqueue_head(&ioa_cfg->reset_wait_q);
7176 ioa_cfg->sdt_state = INACTIVE;
brking@us.ibm.com62275042005-11-01 17:01:14 -06007177 if (ipr_enable_cache)
7178 ioa_cfg->cache_state = CACHE_ENABLED;
7179 else
7180 ioa_cfg->cache_state = CACHE_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181
7182 ipr_initialize_bus_attr(ioa_cfg);
7183
7184 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
7185 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
7186 host->max_channel = IPR_MAX_BUS_TO_SCAN;
7187 host->unique_id = host->host_no;
7188 host->max_cmd_len = IPR_MAX_CDB_LEN;
7189 pci_set_drvdata(pdev, ioa_cfg);
7190
7191 p = &ioa_cfg->chip_cfg->regs;
7192 t = &ioa_cfg->regs;
7193 base = ioa_cfg->hdw_dma_regs;
7194
7195 t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
7196 t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
7197 t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
7198 t->clr_interrupt_reg = base + p->clr_interrupt_reg;
7199 t->sense_interrupt_reg = base + p->sense_interrupt_reg;
7200 t->ioarrin_reg = base + p->ioarrin_reg;
7201 t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
7202 t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
7203 t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
7204}
7205
7206/**
7207 * ipr_get_chip_cfg - Find adapter chip configuration
7208 * @dev_id: PCI device id struct
7209 *
7210 * Return value:
7211 * ptr to chip config on success / NULL on failure
7212 **/
7213static const struct ipr_chip_cfg_t * __devinit
7214ipr_get_chip_cfg(const struct pci_device_id *dev_id)
7215{
7216 int i;
7217
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
7219 if (ipr_chip[i].vendor == dev_id->vendor &&
7220 ipr_chip[i].device == dev_id->device)
7221 return ipr_chip[i].cfg;
7222 return NULL;
7223}
7224
7225/**
7226 * ipr_probe_ioa - Allocates memory and does first stage of initialization
7227 * @pdev: PCI device struct
7228 * @dev_id: PCI device id struct
7229 *
7230 * Return value:
7231 * 0 on success / non-zero on failure
7232 **/
7233static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
7234 const struct pci_device_id *dev_id)
7235{
7236 struct ipr_ioa_cfg *ioa_cfg;
7237 struct Scsi_Host *host;
7238 unsigned long ipr_regs_pci;
7239 void __iomem *ipr_regs;
Eric Sesterhenna2a65a32006-09-25 16:59:07 -07007240 int rc = PCIBIOS_SUCCESSFUL;
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007241 volatile u32 mask, uproc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242
7243 ENTER;
7244
7245 if ((rc = pci_enable_device(pdev))) {
7246 dev_err(&pdev->dev, "Cannot enable adapter\n");
7247 goto out;
7248 }
7249
7250 dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
7251
7252 host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
7253
7254 if (!host) {
7255 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
7256 rc = -ENOMEM;
7257 goto out_disable;
7258 }
7259
7260 ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
7261 memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
Brian King35a39692006-09-25 12:39:20 -05007262 ata_host_init(&ioa_cfg->ata_host, &pdev->dev,
7263 sata_port_info.flags, &ipr_sata_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264
7265 ioa_cfg->chip_cfg = ipr_get_chip_cfg(dev_id);
7266
7267 if (!ioa_cfg->chip_cfg) {
7268 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
7269 dev_id->vendor, dev_id->device);
7270 goto out_scsi_host_put;
7271 }
7272
Brian King5469cb52007-03-29 12:42:40 -05007273 if (ipr_transop_timeout)
7274 ioa_cfg->transop_timeout = ipr_transop_timeout;
7275 else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
7276 ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
7277 else
7278 ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
7279
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 ipr_regs_pci = pci_resource_start(pdev, 0);
7281
7282 rc = pci_request_regions(pdev, IPR_NAME);
7283 if (rc < 0) {
7284 dev_err(&pdev->dev,
7285 "Couldn't register memory range of registers\n");
7286 goto out_scsi_host_put;
7287 }
7288
7289 ipr_regs = ioremap(ipr_regs_pci, pci_resource_len(pdev, 0));
7290
7291 if (!ipr_regs) {
7292 dev_err(&pdev->dev,
7293 "Couldn't map memory range of registers\n");
7294 rc = -ENOMEM;
7295 goto out_release_regions;
7296 }
7297
7298 ioa_cfg->hdw_dma_regs = ipr_regs;
7299 ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
7300 ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
7301
7302 ipr_init_ioa_cfg(ioa_cfg, host, pdev);
7303
7304 pci_set_master(pdev);
7305
7306 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7307 if (rc < 0) {
7308 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
7309 goto cleanup_nomem;
7310 }
7311
7312 rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
7313 ioa_cfg->chip_cfg->cache_line_size);
7314
7315 if (rc != PCIBIOS_SUCCESSFUL) {
7316 dev_err(&pdev->dev, "Write of cache line size failed\n");
7317 rc = -EIO;
7318 goto cleanup_nomem;
7319 }
7320
7321 /* Save away PCI config space for use following IOA reset */
7322 rc = pci_save_state(pdev);
7323
7324 if (rc != PCIBIOS_SUCCESSFUL) {
7325 dev_err(&pdev->dev, "Failed to save PCI config space\n");
7326 rc = -EIO;
7327 goto cleanup_nomem;
7328 }
7329
7330 if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
7331 goto cleanup_nomem;
7332
7333 if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
7334 goto cleanup_nomem;
7335
7336 rc = ipr_alloc_mem(ioa_cfg);
7337 if (rc < 0) {
7338 dev_err(&pdev->dev,
7339 "Couldn't allocate enough memory for device driver!\n");
7340 goto cleanup_nomem;
7341 }
7342
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007343 /*
7344 * If HRRQ updated interrupt is not masked, or reset alert is set,
7345 * the card is in an unknown state and needs a hard reset
7346 */
7347 mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7348 uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
7349 if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
7350 ioa_cfg->needs_hard_reset = 1;
7351
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07007353 rc = request_irq(pdev->irq, ipr_isr, IRQF_SHARED, IPR_NAME, ioa_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354
7355 if (rc) {
7356 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
7357 pdev->irq, rc);
7358 goto cleanup_nolog;
7359 }
7360
7361 spin_lock(&ipr_driver_lock);
7362 list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
7363 spin_unlock(&ipr_driver_lock);
7364
7365 LEAVE;
7366out:
7367 return rc;
7368
7369cleanup_nolog:
7370 ipr_free_mem(ioa_cfg);
7371cleanup_nomem:
7372 iounmap(ipr_regs);
7373out_release_regions:
7374 pci_release_regions(pdev);
7375out_scsi_host_put:
7376 scsi_host_put(host);
7377out_disable:
7378 pci_disable_device(pdev);
7379 goto out;
7380}
7381
7382/**
7383 * ipr_scan_vsets - Scans for VSET devices
7384 * @ioa_cfg: ioa config struct
7385 *
7386 * Description: Since the VSET resources do not follow SAM in that we can have
7387 * sparse LUNs with no LUN 0, we have to scan for these ourselves.
7388 *
7389 * Return value:
7390 * none
7391 **/
7392static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
7393{
7394 int target, lun;
7395
7396 for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
7397 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
7398 scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
7399}
7400
7401/**
7402 * ipr_initiate_ioa_bringdown - Bring down an adapter
7403 * @ioa_cfg: ioa config struct
7404 * @shutdown_type: shutdown type
7405 *
7406 * Description: This function will initiate bringing down the adapter.
7407 * This consists of issuing an IOA shutdown to the adapter
7408 * to flush the cache, and running BIST.
7409 * If the caller needs to wait on the completion of the reset,
7410 * the caller must sleep on the reset_wait_q.
7411 *
7412 * Return value:
7413 * none
7414 **/
7415static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
7416 enum ipr_shutdown_type shutdown_type)
7417{
7418 ENTER;
7419 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
7420 ioa_cfg->sdt_state = ABORT_DUMP;
7421 ioa_cfg->reset_retries = 0;
7422 ioa_cfg->in_ioa_bringdown = 1;
7423 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
7424 LEAVE;
7425}
7426
7427/**
7428 * __ipr_remove - Remove a single adapter
7429 * @pdev: pci device struct
7430 *
7431 * Adapter hot plug remove entry point.
7432 *
7433 * Return value:
7434 * none
7435 **/
7436static void __ipr_remove(struct pci_dev *pdev)
7437{
7438 unsigned long host_lock_flags = 0;
7439 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7440 ENTER;
7441
7442 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
Brian King970ea292007-04-26 16:00:06 -05007443 while(ioa_cfg->in_reset_reload) {
7444 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7445 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7446 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7447 }
7448
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
7450
7451 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7452 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
brking@us.ibm.com 5cbf5ea2005-05-02 19:50:47 -05007453 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7455
7456 spin_lock(&ipr_driver_lock);
7457 list_del(&ioa_cfg->queue);
7458 spin_unlock(&ipr_driver_lock);
7459
7460 if (ioa_cfg->sdt_state == ABORT_DUMP)
7461 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7462 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7463
7464 ipr_free_all_resources(ioa_cfg);
7465
7466 LEAVE;
7467}
7468
7469/**
7470 * ipr_remove - IOA hot plug remove entry point
7471 * @pdev: pci device struct
7472 *
7473 * Adapter hot plug remove entry point.
7474 *
7475 * Return value:
7476 * none
7477 **/
7478static void ipr_remove(struct pci_dev *pdev)
7479{
7480 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7481
7482 ENTER;
7483
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
7485 &ipr_trace_attr);
7486 ipr_remove_dump_file(&ioa_cfg->host->shost_classdev.kobj,
7487 &ipr_dump_attr);
7488 scsi_remove_host(ioa_cfg->host);
7489
7490 __ipr_remove(pdev);
7491
7492 LEAVE;
7493}
7494
7495/**
7496 * ipr_probe - Adapter hot plug add entry point
7497 *
7498 * Return value:
7499 * 0 on success / non-zero on failure
7500 **/
7501static int __devinit ipr_probe(struct pci_dev *pdev,
7502 const struct pci_device_id *dev_id)
7503{
7504 struct ipr_ioa_cfg *ioa_cfg;
7505 int rc;
7506
7507 rc = ipr_probe_ioa(pdev, dev_id);
7508
7509 if (rc)
7510 return rc;
7511
7512 ioa_cfg = pci_get_drvdata(pdev);
7513 rc = ipr_probe_ioa_part2(ioa_cfg);
7514
7515 if (rc) {
7516 __ipr_remove(pdev);
7517 return rc;
7518 }
7519
7520 rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
7521
7522 if (rc) {
7523 __ipr_remove(pdev);
7524 return rc;
7525 }
7526
7527 rc = ipr_create_trace_file(&ioa_cfg->host->shost_classdev.kobj,
7528 &ipr_trace_attr);
7529
7530 if (rc) {
7531 scsi_remove_host(ioa_cfg->host);
7532 __ipr_remove(pdev);
7533 return rc;
7534 }
7535
7536 rc = ipr_create_dump_file(&ioa_cfg->host->shost_classdev.kobj,
7537 &ipr_dump_attr);
7538
7539 if (rc) {
7540 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
7541 &ipr_trace_attr);
7542 scsi_remove_host(ioa_cfg->host);
7543 __ipr_remove(pdev);
7544 return rc;
7545 }
7546
7547 scsi_scan_host(ioa_cfg->host);
7548 ipr_scan_vsets(ioa_cfg);
7549 scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
7550 ioa_cfg->allow_ml_add_del = 1;
brking@us.ibm.com11cd8f12005-11-01 17:00:11 -06007551 ioa_cfg->host->max_channel = IPR_VSET_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 schedule_work(&ioa_cfg->work_q);
7553 return 0;
7554}
7555
7556/**
7557 * ipr_shutdown - Shutdown handler.
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007558 * @pdev: pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 *
7560 * This function is invoked upon system shutdown/reboot. It will issue
7561 * an adapter shutdown to the adapter to flush the write cache.
7562 *
7563 * Return value:
7564 * none
7565 **/
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007566static void ipr_shutdown(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567{
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007568 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 unsigned long lock_flags = 0;
7570
7571 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05007572 while(ioa_cfg->in_reset_reload) {
7573 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7574 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7575 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7576 }
7577
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
7579 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7580 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7581}
7582
7583static struct pci_device_id ipr_pci_table[] __devinitdata = {
7584 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007585 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007587 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007589 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007591 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06007593 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06007595 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06007597 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007598 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King5469cb52007-03-29 12:42:40 -05007599 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
7600 IPR_USE_LONG_TRANSOP_TIMEOUT },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007601 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King6d84c942007-01-23 11:25:23 -06007602 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007603 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King6d84c942007-01-23 11:25:23 -06007604 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0, 0 },
Brian King60e74862006-11-21 10:28:10 -06007605 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King5469cb52007-03-29 12:42:40 -05007606 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
7607 IPR_USE_LONG_TRANSOP_TIMEOUT },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007608 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King6d84c942007-01-23 11:25:23 -06007609 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007610 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King6d84c942007-01-23 11:25:23 -06007611 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0, 0 },
Brian King60e74862006-11-21 10:28:10 -06007612 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King5469cb52007-03-29 12:42:40 -05007613 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
7614 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06007615 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
Brian King185eb312007-03-29 12:42:53 -05007616 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0, 0 },
7617 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
7618 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575D, 0, 0,
7619 IPR_USE_LONG_TRANSOP_TIMEOUT },
7620 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
7621 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
7622 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
Brian King5469cb52007-03-29 12:42:40 -05007623 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
7624 IPR_USE_LONG_TRANSOP_TIMEOUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
Brian King6d84c942007-01-23 11:25:23 -06007626 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King6d84c942007-01-23 11:25:23 -06007628 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007629 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King5469cb52007-03-29 12:42:40 -05007630 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
7631 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06007632 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King5469cb52007-03-29 12:42:40 -05007633 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
7634 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King185eb312007-03-29 12:42:53 -05007635 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SCAMP_E,
7636 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0,
7637 IPR_USE_LONG_TRANSOP_TIMEOUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 { }
7639};
7640MODULE_DEVICE_TABLE(pci, ipr_pci_table);
7641
Linas Vepstasf8a88b192006-02-03 16:52:42 -06007642static struct pci_error_handlers ipr_err_handler = {
7643 .error_detected = ipr_pci_error_detected,
7644 .slot_reset = ipr_pci_slot_reset,
7645};
7646
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647static struct pci_driver ipr_driver = {
7648 .name = IPR_NAME,
7649 .id_table = ipr_pci_table,
7650 .probe = ipr_probe,
7651 .remove = ipr_remove,
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007652 .shutdown = ipr_shutdown,
Linas Vepstasf8a88b192006-02-03 16:52:42 -06007653 .err_handler = &ipr_err_handler,
Brian King68c96e52007-04-26 16:00:07 -05007654 .dynids.use_driver_data = 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655};
7656
7657/**
7658 * ipr_init - Module entry point
7659 *
7660 * Return value:
7661 * 0 on success / negative value on failure
7662 **/
7663static int __init ipr_init(void)
7664{
7665 ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
7666 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
7667
Henrik Kretzschmardcbccbde2006-09-25 16:58:58 -07007668 return pci_register_driver(&ipr_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669}
7670
7671/**
7672 * ipr_exit - Module unload
7673 *
7674 * Module unload entry point.
7675 *
7676 * Return value:
7677 * none
7678 **/
7679static void __exit ipr_exit(void)
7680{
7681 pci_unregister_driver(&ipr_driver);
7682}
7683
7684module_init(ipr_init);
7685module_exit(ipr_exit);