blob: 098739deb02e1394dc466f51061284a484245cf8 [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>
Brian King0ce3a7e2008-07-11 13:37:50 -050074#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/io.h>
76#include <asm/irq.h>
77#include <asm/processor.h>
78#include <scsi/scsi.h>
79#include <scsi/scsi_host.h>
80#include <scsi/scsi_tcq.h>
81#include <scsi/scsi_eh.h>
82#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include "ipr.h"
84
85/*
86 * Global Data
87 */
Denis Chengb7d68ca2007-12-13 16:14:27 -080088static LIST_HEAD(ipr_ioa_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
90static unsigned int ipr_max_speed = 1;
91static int ipr_testmode = 0;
92static unsigned int ipr_fastfail = 0;
Brian King5469cb52007-03-29 12:42:40 -050093static unsigned int ipr_transop_timeout = 0;
brking@us.ibm.com62275042005-11-01 17:01:14 -060094static unsigned int ipr_enable_cache = 1;
brking@us.ibm.comd3c74872005-11-01 17:01:34 -060095static unsigned int ipr_debug = 0;
Brian Kingac09c342007-04-26 16:00:16 -050096static unsigned int ipr_dual_ioa_raid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static DEFINE_SPINLOCK(ipr_driver_lock);
98
99/* This table describes the differences between DMA controller chips */
100static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
Brian King60e74862006-11-21 10:28:10 -0600101 { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .mailbox = 0x0042C,
103 .cache_line_size = 0x20,
104 {
105 .set_interrupt_mask_reg = 0x0022C,
106 .clr_interrupt_mask_reg = 0x00230,
107 .sense_interrupt_mask_reg = 0x0022C,
108 .clr_interrupt_reg = 0x00228,
109 .sense_interrupt_reg = 0x00224,
110 .ioarrin_reg = 0x00404,
111 .sense_uproc_interrupt_reg = 0x00214,
112 .set_uproc_interrupt_reg = 0x00214,
113 .clr_uproc_interrupt_reg = 0x00218
114 }
115 },
116 { /* Snipe and Scamp */
117 .mailbox = 0x0052C,
118 .cache_line_size = 0x20,
119 {
120 .set_interrupt_mask_reg = 0x00288,
121 .clr_interrupt_mask_reg = 0x0028C,
122 .sense_interrupt_mask_reg = 0x00288,
123 .clr_interrupt_reg = 0x00284,
124 .sense_interrupt_reg = 0x00280,
125 .ioarrin_reg = 0x00504,
126 .sense_uproc_interrupt_reg = 0x00290,
127 .set_uproc_interrupt_reg = 0x00290,
128 .clr_uproc_interrupt_reg = 0x00294
129 }
130 },
131};
132
133static const struct ipr_chip_t ipr_chip[] = {
134 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, &ipr_chip_cfg[0] },
135 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, &ipr_chip_cfg[0] },
brking@us.ibm.com86f51432005-11-01 17:02:42 -0600136 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, &ipr_chip_cfg[0] },
137 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, &ipr_chip_cfg[0] },
Brian King60e74862006-11-21 10:28:10 -0600138 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, &ipr_chip_cfg[0] },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, &ipr_chip_cfg[1] },
140 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, &ipr_chip_cfg[1] }
141};
142
143static int ipr_max_bus_speeds [] = {
144 IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
145};
146
147MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
148MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
149module_param_named(max_speed, ipr_max_speed, uint, 0);
150MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
151module_param_named(log_level, ipr_log_level, uint, 0);
152MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
153module_param_named(testmode, ipr_testmode, int, 0);
154MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
155module_param_named(fastfail, ipr_fastfail, int, 0);
156MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
157module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
158MODULE_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 -0600159module_param_named(enable_cache, ipr_enable_cache, int, 0);
160MODULE_PARM_DESC(enable_cache, "Enable adapter's non-volatile write cache (default: 1)");
brking@us.ibm.comd3c74872005-11-01 17:01:34 -0600161module_param_named(debug, ipr_debug, int, 0);
162MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
Brian Kingac09c342007-04-26 16:00:16 -0500163module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
164MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165MODULE_LICENSE("GPL");
166MODULE_VERSION(IPR_DRIVER_VERSION);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* A constant array of IOASCs/URCs/Error Messages */
169static const
170struct ipr_error_table_t ipr_error_table[] = {
Brian King933916f2007-03-29 12:43:30 -0500171 {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 "8155: An unknown error was received"},
173 {0x00330000, 0, 0,
174 "Soft underlength error"},
175 {0x005A0000, 0, 0,
176 "Command to be cancelled not found"},
177 {0x00808000, 0, 0,
178 "Qualified success"},
Brian King933916f2007-03-29 12:43:30 -0500179 {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 "FFFE: Soft device bus error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500181 {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500182 "4101: Soft device bus fabric error"},
Brian King933916f2007-03-29 12:43:30 -0500183 {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 "FFF9: Device sector reassign successful"},
Brian King933916f2007-03-29 12:43:30 -0500185 {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 "FFF7: Media error recovered by device rewrite procedures"},
Brian King933916f2007-03-29 12:43:30 -0500187 {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 "7001: IOA sector reassignment successful"},
Brian King933916f2007-03-29 12:43:30 -0500189 {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 "FFF9: Soft media error. Sector reassignment recommended"},
Brian King933916f2007-03-29 12:43:30 -0500191 {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 "FFF7: Media error recovered by IOA rewrite procedures"},
Brian King933916f2007-03-29 12:43:30 -0500193 {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 "FF3D: Soft PCI bus error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500195 {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 "FFF6: Device hardware error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500197 {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 "FFF6: Device hardware error recovered by the device"},
Brian King933916f2007-03-29 12:43:30 -0500199 {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 "FF3D: Soft IOA error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500201 {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 "FFFA: Undefined device response recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500203 {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 "FFF6: Device bus error, message or command phase"},
Brian King933916f2007-03-29 12:43:30 -0500205 {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King35a39692006-09-25 12:39:20 -0500206 "FFFE: Task Management Function failed"},
Brian King933916f2007-03-29 12:43:30 -0500207 {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 "FFF6: Failure prediction threshold exceeded"},
Brian King933916f2007-03-29 12:43:30 -0500209 {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 "8009: Impending cache battery pack failure"},
211 {0x02040400, 0, 0,
212 "34FF: Disk device format in progress"},
Brian King65f56472007-04-26 16:00:12 -0500213 {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
214 "9070: IOA requested reset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 {0x023F0000, 0, 0,
216 "Synchronization required"},
217 {0x024E0000, 0, 0,
218 "No ready, IOA shutdown"},
219 {0x025A0000, 0, 0,
220 "Not ready, IOA has been shutdown"},
Brian King933916f2007-03-29 12:43:30 -0500221 {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 "3020: Storage subsystem configuration error"},
223 {0x03110B00, 0, 0,
224 "FFF5: Medium error, data unreadable, recommend reassign"},
225 {0x03110C00, 0, 0,
226 "7000: Medium error, data unreadable, do not reassign"},
Brian King933916f2007-03-29 12:43:30 -0500227 {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 "FFF3: Disk media format bad"},
Brian King933916f2007-03-29 12:43:30 -0500229 {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 "3002: Addressed device failed to respond to selection"},
Brian King933916f2007-03-29 12:43:30 -0500231 {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 "3100: Device bus error"},
Brian King933916f2007-03-29 12:43:30 -0500233 {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 "3109: IOA timed out a device command"},
235 {0x04088000, 0, 0,
236 "3120: SCSI bus is not operational"},
Brian King933916f2007-03-29 12:43:30 -0500237 {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500238 "4100: Hard device bus fabric error"},
Brian King933916f2007-03-29 12:43:30 -0500239 {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 "9000: IOA reserved area data check"},
Brian King933916f2007-03-29 12:43:30 -0500241 {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 "9001: IOA reserved area invalid data pattern"},
Brian King933916f2007-03-29 12:43:30 -0500243 {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 "9002: IOA reserved area LRC error"},
Brian King933916f2007-03-29 12:43:30 -0500245 {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 "102E: Out of alternate sectors for disk storage"},
Brian King933916f2007-03-29 12:43:30 -0500247 {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 "FFF4: Data transfer underlength error"},
Brian King933916f2007-03-29 12:43:30 -0500249 {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 "FFF4: Data transfer overlength error"},
Brian King933916f2007-03-29 12:43:30 -0500251 {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 "3400: Logical unit failure"},
Brian King933916f2007-03-29 12:43:30 -0500253 {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 "FFF4: Device microcode is corrupt"},
Brian King933916f2007-03-29 12:43:30 -0500255 {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 "8150: PCI bus error"},
257 {0x04430000, 1, 0,
258 "Unsupported device bus message received"},
Brian King933916f2007-03-29 12:43:30 -0500259 {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 "FFF4: Disk device problem"},
Brian King933916f2007-03-29 12:43:30 -0500261 {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 "8150: Permanent IOA failure"},
Brian King933916f2007-03-29 12:43:30 -0500263 {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 "3010: Disk device returned wrong response to IOA"},
Brian King933916f2007-03-29 12:43:30 -0500265 {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 "8151: IOA microcode error"},
267 {0x04448500, 0, 0,
268 "Device bus status error"},
Brian King933916f2007-03-29 12:43:30 -0500269 {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 "8157: IOA error requiring IOA reset to recover"},
Brian King35a39692006-09-25 12:39:20 -0500271 {0x04448700, 0, 0,
272 "ATA device status error"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 {0x04490000, 0, 0,
274 "Message reject received from the device"},
Brian King933916f2007-03-29 12:43:30 -0500275 {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 "8008: A permanent cache battery pack failure occurred"},
Brian King933916f2007-03-29 12:43:30 -0500277 {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 "9090: Disk unit has been modified after the last known status"},
Brian King933916f2007-03-29 12:43:30 -0500279 {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 "9081: IOA detected device error"},
Brian King933916f2007-03-29 12:43:30 -0500281 {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 "9082: IOA detected device error"},
Brian King933916f2007-03-29 12:43:30 -0500283 {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 "3110: Device bus error, message or command phase"},
Brian King933916f2007-03-29 12:43:30 -0500285 {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
Brian King35a39692006-09-25 12:39:20 -0500286 "3110: SAS Command / Task Management Function failed"},
Brian King933916f2007-03-29 12:43:30 -0500287 {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 "9091: Incorrect hardware configuration change has been detected"},
Brian King933916f2007-03-29 12:43:30 -0500289 {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600290 "9073: Invalid multi-adapter configuration"},
Brian King933916f2007-03-29 12:43:30 -0500291 {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500292 "4010: Incorrect connection between cascaded expanders"},
Brian King933916f2007-03-29 12:43:30 -0500293 {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500294 "4020: Connections exceed IOA design limits"},
Brian King933916f2007-03-29 12:43:30 -0500295 {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500296 "4030: Incorrect multipath connection"},
Brian King933916f2007-03-29 12:43:30 -0500297 {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500298 "4110: Unsupported enclosure function"},
Brian King933916f2007-03-29 12:43:30 -0500299 {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 "FFF4: Command to logical unit failed"},
301 {0x05240000, 1, 0,
302 "Illegal request, invalid request type or request packet"},
303 {0x05250000, 0, 0,
304 "Illegal request, invalid resource handle"},
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600305 {0x05258000, 0, 0,
306 "Illegal request, commands not allowed to this device"},
307 {0x05258100, 0, 0,
308 "Illegal request, command not allowed to a secondary adapter"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 {0x05260000, 0, 0,
310 "Illegal request, invalid field in parameter list"},
311 {0x05260100, 0, 0,
312 "Illegal request, parameter not supported"},
313 {0x05260200, 0, 0,
314 "Illegal request, parameter value invalid"},
315 {0x052C0000, 0, 0,
316 "Illegal request, command sequence error"},
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600317 {0x052C8000, 1, 0,
318 "Illegal request, dual adapter support not enabled"},
Brian King933916f2007-03-29 12:43:30 -0500319 {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 "9031: Array protection temporarily suspended, protection resuming"},
Brian King933916f2007-03-29 12:43:30 -0500321 {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 "9040: Array protection temporarily suspended, protection resuming"},
Brian King933916f2007-03-29 12:43:30 -0500323 {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500324 "3140: Device bus not ready to ready transition"},
Brian King933916f2007-03-29 12:43:30 -0500325 {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 "FFFB: SCSI bus was reset"},
327 {0x06290500, 0, 0,
328 "FFFE: SCSI bus transition to single ended"},
329 {0x06290600, 0, 0,
330 "FFFE: SCSI bus transition to LVD"},
Brian King933916f2007-03-29 12:43:30 -0500331 {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 "FFFB: SCSI bus was reset by another initiator"},
Brian King933916f2007-03-29 12:43:30 -0500333 {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 "3029: A device replacement has occurred"},
Brian King933916f2007-03-29 12:43:30 -0500335 {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 "9051: IOA cache data exists for a missing or failed device"},
Brian King933916f2007-03-29 12:43:30 -0500337 {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600338 "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
Brian King933916f2007-03-29 12:43:30 -0500339 {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 "9025: Disk unit is not supported at its physical location"},
Brian King933916f2007-03-29 12:43:30 -0500341 {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 "3020: IOA detected a SCSI bus configuration error"},
Brian King933916f2007-03-29 12:43:30 -0500343 {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 "3150: SCSI bus configuration error"},
Brian King933916f2007-03-29 12:43:30 -0500345 {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600346 "9074: Asymmetric advanced function disk configuration"},
Brian King933916f2007-03-29 12:43:30 -0500347 {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500348 "4040: Incomplete multipath connection between IOA and enclosure"},
Brian King933916f2007-03-29 12:43:30 -0500349 {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500350 "4041: Incomplete multipath connection between enclosure and device"},
Brian King933916f2007-03-29 12:43:30 -0500351 {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500352 "9075: Incomplete multipath connection between IOA and remote IOA"},
Brian King933916f2007-03-29 12:43:30 -0500353 {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500354 "9076: Configuration error, missing remote IOA"},
Brian King933916f2007-03-29 12:43:30 -0500355 {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500356 "4050: Enclosure does not support a required multipath function"},
Brian King933916f2007-03-29 12:43:30 -0500357 {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 "9041: Array protection temporarily suspended"},
Brian King933916f2007-03-29 12:43:30 -0500359 {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 "9042: Corrupt array parity detected on specified device"},
Brian King933916f2007-03-29 12:43:30 -0500361 {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 "9030: Array no longer protected due to missing or failed disk unit"},
Brian King933916f2007-03-29 12:43:30 -0500363 {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600364 "9071: Link operational transition"},
Brian King933916f2007-03-29 12:43:30 -0500365 {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600366 "9072: Link not operational transition"},
Brian King933916f2007-03-29 12:43:30 -0500367 {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 "9032: Array exposed but still protected"},
Brian Kinge4353402007-03-29 12:43:37 -0500369 {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
370 "70DD: Device forced failed by disrupt device command"},
Brian King933916f2007-03-29 12:43:30 -0500371 {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500372 "4061: Multipath redundancy level got better"},
Brian King933916f2007-03-29 12:43:30 -0500373 {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500374 "4060: Multipath redundancy level got worse"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 {0x07270000, 0, 0,
376 "Failure due to other device"},
Brian King933916f2007-03-29 12:43:30 -0500377 {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 "9008: IOA does not support functions expected by devices"},
Brian King933916f2007-03-29 12:43:30 -0500379 {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 "9010: Cache data associated with attached devices cannot be found"},
Brian King933916f2007-03-29 12:43:30 -0500381 {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 "9011: Cache data belongs to devices other than those attached"},
Brian King933916f2007-03-29 12:43:30 -0500383 {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 "9020: Array missing 2 or more devices with only 1 device present"},
Brian King933916f2007-03-29 12:43:30 -0500385 {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 "9021: Array missing 2 or more devices with 2 or more devices present"},
Brian King933916f2007-03-29 12:43:30 -0500387 {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 "9022: Exposed array is missing a required device"},
Brian King933916f2007-03-29 12:43:30 -0500389 {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 "9023: Array member(s) not at required physical locations"},
Brian King933916f2007-03-29 12:43:30 -0500391 {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 "9024: Array not functional due to present hardware configuration"},
Brian King933916f2007-03-29 12:43:30 -0500393 {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 "9026: Array not functional due to present hardware configuration"},
Brian King933916f2007-03-29 12:43:30 -0500395 {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 "9027: Array is missing a device and parity is out of sync"},
Brian King933916f2007-03-29 12:43:30 -0500397 {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 "9028: Maximum number of arrays already exist"},
Brian King933916f2007-03-29 12:43:30 -0500399 {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 "9050: Required cache data cannot be located for a disk unit"},
Brian King933916f2007-03-29 12:43:30 -0500401 {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 "9052: Cache data exists for a device that has been modified"},
Brian King933916f2007-03-29 12:43:30 -0500403 {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 "9054: IOA resources not available due to previous problems"},
Brian King933916f2007-03-29 12:43:30 -0500405 {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 "9092: Disk unit requires initialization before use"},
Brian King933916f2007-03-29 12:43:30 -0500407 {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 "9029: Incorrect hardware configuration change has been detected"},
Brian King933916f2007-03-29 12:43:30 -0500409 {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 "9060: One or more disk pairs are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500411 {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 "9061: One or more disks are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500413 {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 "9062: One or more disks are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500415 {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 "9063: Maximum number of functional arrays has been exceeded"},
417 {0x0B260000, 0, 0,
418 "Aborted command, invalid descriptor"},
419 {0x0B5A0000, 0, 0,
420 "Command terminated by host"}
421};
422
423static const struct ipr_ses_table_entry ipr_ses_table[] = {
424 { "2104-DL1 ", "XXXXXXXXXXXXXXXX", 80 },
425 { "2104-TL1 ", "XXXXXXXXXXXXXXXX", 80 },
426 { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
427 { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
428 { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
429 { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
430 { "2104-DU3 ", "XXXXXXXXXXXXXXXX", 160 },
431 { "2104-TU3 ", "XXXXXXXXXXXXXXXX", 160 },
432 { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
433 { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
434 { "St V1S2 ", "XXXXXXXXXXXXXXXX", 160 },
435 { "HSBPD4M PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
436 { "VSBPD1H U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
437};
438
439/*
440 * Function Prototypes
441 */
442static int ipr_reset_alert(struct ipr_cmnd *);
443static void ipr_process_ccn(struct ipr_cmnd *);
444static void ipr_process_error(struct ipr_cmnd *);
445static void ipr_reset_ioa_job(struct ipr_cmnd *);
446static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
447 enum ipr_shutdown_type);
448
449#ifdef CONFIG_SCSI_IPR_TRACE
450/**
451 * ipr_trc_hook - Add a trace entry to the driver trace
452 * @ipr_cmd: ipr command struct
453 * @type: trace type
454 * @add_data: additional data
455 *
456 * Return value:
457 * none
458 **/
459static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
460 u8 type, u32 add_data)
461{
462 struct ipr_trace_entry *trace_entry;
463 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
464
465 trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
466 trace_entry->time = jiffies;
467 trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
468 trace_entry->type = type;
Brian King35a39692006-09-25 12:39:20 -0500469 trace_entry->ata_op_code = ipr_cmd->ioarcb.add_data.u.regs.command;
470 trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
472 trace_entry->u.add_data = add_data;
473}
474#else
475#define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
476#endif
477
478/**
479 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
480 * @ipr_cmd: ipr command struct
481 *
482 * Return value:
483 * none
484 **/
485static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
486{
487 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
488 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
Brian King51b1c7e2007-03-29 12:43:50 -0500489 dma_addr_t dma_addr = be32_to_cpu(ioarcb->ioarcb_host_pci_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
492 ioarcb->write_data_transfer_length = 0;
493 ioarcb->read_data_transfer_length = 0;
494 ioarcb->write_ioadl_len = 0;
495 ioarcb->read_ioadl_len = 0;
Brian King51b1c7e2007-03-29 12:43:50 -0500496 ioarcb->write_ioadl_addr =
497 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
498 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 ioasa->ioasc = 0;
500 ioasa->residual_data_len = 0;
Brian King35a39692006-09-25 12:39:20 -0500501 ioasa->u.gata.status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 ipr_cmd->scsi_cmd = NULL;
Brian King35a39692006-09-25 12:39:20 -0500504 ipr_cmd->qc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ipr_cmd->sense_buffer[0] = 0;
506 ipr_cmd->dma_use_sg = 0;
507}
508
509/**
510 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
511 * @ipr_cmd: ipr command struct
512 *
513 * Return value:
514 * none
515 **/
516static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
517{
518 ipr_reinit_ipr_cmnd(ipr_cmd);
519 ipr_cmd->u.scratch = 0;
520 ipr_cmd->sibling = NULL;
521 init_timer(&ipr_cmd->timer);
522}
523
524/**
525 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
526 * @ioa_cfg: ioa config struct
527 *
528 * Return value:
529 * pointer to ipr command struct
530 **/
531static
532struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
533{
534 struct ipr_cmnd *ipr_cmd;
535
536 ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
537 list_del(&ipr_cmd->queue);
538 ipr_init_ipr_cmnd(ipr_cmd);
539
540 return ipr_cmd;
541}
542
543/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
545 * @ioa_cfg: ioa config struct
546 * @clr_ints: interrupts to clear
547 *
548 * This function masks all interrupts on the adapter, then clears the
549 * interrupts specified in the mask
550 *
551 * Return value:
552 * none
553 **/
554static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
555 u32 clr_ints)
556{
557 volatile u32 int_reg;
558
559 /* Stop new interrupts */
560 ioa_cfg->allow_interrupts = 0;
561
562 /* Set interrupt mask to stop all new interrupts */
563 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
564
565 /* Clear any pending interrupts */
566 writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
567 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
568}
569
570/**
571 * ipr_save_pcix_cmd_reg - Save PCI-X command register
572 * @ioa_cfg: ioa config struct
573 *
574 * Return value:
575 * 0 on success / -EIO on failure
576 **/
577static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
578{
579 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
580
Brian King7dce0e12007-01-23 11:25:30 -0600581 if (pcix_cmd_reg == 0)
582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
585 &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
586 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
587 return -EIO;
588 }
589
590 ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
591 return 0;
592}
593
594/**
595 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
596 * @ioa_cfg: ioa config struct
597 *
598 * Return value:
599 * 0 on success / -EIO on failure
600 **/
601static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
602{
603 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
604
605 if (pcix_cmd_reg) {
606 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
607 ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
608 dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
609 return -EIO;
610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 return 0;
614}
615
616/**
Brian King35a39692006-09-25 12:39:20 -0500617 * ipr_sata_eh_done - done function for aborted SATA commands
618 * @ipr_cmd: ipr command struct
619 *
620 * This function is invoked for ops generated to SATA
621 * devices which are being aborted.
622 *
623 * Return value:
624 * none
625 **/
626static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
627{
628 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
629 struct ata_queued_cmd *qc = ipr_cmd->qc;
630 struct ipr_sata_port *sata_port = qc->ap->private_data;
631
632 qc->err_mask |= AC_ERR_OTHER;
633 sata_port->ioasa.status |= ATA_BUSY;
634 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
635 ata_qc_complete(qc);
636}
637
638/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 * ipr_scsi_eh_done - mid-layer done function for aborted ops
640 * @ipr_cmd: ipr command struct
641 *
642 * This function is invoked by the interrupt handler for
643 * ops generated by the SCSI mid-layer which are being aborted.
644 *
645 * Return value:
646 * none
647 **/
648static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
649{
650 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
651 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
652
653 scsi_cmd->result |= (DID_ERROR << 16);
654
FUJITA Tomonori63015bc2007-05-26 00:26:59 +0900655 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 scsi_cmd->scsi_done(scsi_cmd);
657 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
658}
659
660/**
661 * ipr_fail_all_ops - Fails all outstanding ops.
662 * @ioa_cfg: ioa config struct
663 *
664 * This function fails all outstanding ops.
665 *
666 * Return value:
667 * none
668 **/
669static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
670{
671 struct ipr_cmnd *ipr_cmd, *temp;
672
673 ENTER;
674 list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
675 list_del(&ipr_cmd->queue);
676
677 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
678 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
679
680 if (ipr_cmd->scsi_cmd)
681 ipr_cmd->done = ipr_scsi_eh_done;
Brian King35a39692006-09-25 12:39:20 -0500682 else if (ipr_cmd->qc)
683 ipr_cmd->done = ipr_sata_eh_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
686 del_timer(&ipr_cmd->timer);
687 ipr_cmd->done(ipr_cmd);
688 }
689
690 LEAVE;
691}
692
693/**
694 * ipr_do_req - Send driver initiated requests.
695 * @ipr_cmd: ipr command struct
696 * @done: done function
697 * @timeout_func: timeout function
698 * @timeout: timeout value
699 *
700 * This function sends the specified command to the adapter with the
701 * timeout given. The done function is invoked on command completion.
702 *
703 * Return value:
704 * none
705 **/
706static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
707 void (*done) (struct ipr_cmnd *),
708 void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
709{
710 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
711
712 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
713
714 ipr_cmd->done = done;
715
716 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
717 ipr_cmd->timer.expires = jiffies + timeout;
718 ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
719
720 add_timer(&ipr_cmd->timer);
721
722 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
723
724 mb();
725 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
726 ioa_cfg->regs.ioarrin_reg);
727}
728
729/**
730 * ipr_internal_cmd_done - Op done function for an internally generated op.
731 * @ipr_cmd: ipr command struct
732 *
733 * This function is the op done function for an internally generated,
734 * blocking op. It simply wakes the sleeping thread.
735 *
736 * Return value:
737 * none
738 **/
739static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
740{
741 if (ipr_cmd->sibling)
742 ipr_cmd->sibling = NULL;
743 else
744 complete(&ipr_cmd->completion);
745}
746
747/**
748 * ipr_send_blocking_cmd - Send command and sleep on its completion.
749 * @ipr_cmd: ipr command struct
750 * @timeout_func: function to invoke if command times out
751 * @timeout: timeout
752 *
753 * Return value:
754 * none
755 **/
756static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
757 void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
758 u32 timeout)
759{
760 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
761
762 init_completion(&ipr_cmd->completion);
763 ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
764
765 spin_unlock_irq(ioa_cfg->host->host_lock);
766 wait_for_completion(&ipr_cmd->completion);
767 spin_lock_irq(ioa_cfg->host->host_lock);
768}
769
770/**
771 * ipr_send_hcam - Send an HCAM to the adapter.
772 * @ioa_cfg: ioa config struct
773 * @type: HCAM type
774 * @hostrcb: hostrcb struct
775 *
776 * This function will send a Host Controlled Async command to the adapter.
777 * If HCAMs are currently not allowed to be issued to the adapter, it will
778 * place the hostrcb on the free queue.
779 *
780 * Return value:
781 * none
782 **/
783static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
784 struct ipr_hostrcb *hostrcb)
785{
786 struct ipr_cmnd *ipr_cmd;
787 struct ipr_ioarcb *ioarcb;
788
789 if (ioa_cfg->allow_cmds) {
790 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
791 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
792 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
793
794 ipr_cmd->u.hostrcb = hostrcb;
795 ioarcb = &ipr_cmd->ioarcb;
796
797 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
798 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
799 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
800 ioarcb->cmd_pkt.cdb[1] = type;
801 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
802 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
803
804 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
805 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
806 ipr_cmd->ioadl[0].flags_and_data_len =
807 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
808 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
809
810 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
811 ipr_cmd->done = ipr_process_ccn;
812 else
813 ipr_cmd->done = ipr_process_error;
814
815 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
816
817 mb();
818 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
819 ioa_cfg->regs.ioarrin_reg);
820 } else {
821 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
822 }
823}
824
825/**
826 * ipr_init_res_entry - Initialize a resource entry struct.
827 * @res: resource entry struct
828 *
829 * Return value:
830 * none
831 **/
832static void ipr_init_res_entry(struct ipr_resource_entry *res)
833{
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -0600834 res->needs_sync_complete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 res->in_erp = 0;
836 res->add_to_ml = 0;
837 res->del_from_ml = 0;
838 res->resetting_device = 0;
839 res->sdev = NULL;
Brian King35a39692006-09-25 12:39:20 -0500840 res->sata_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843/**
844 * ipr_handle_config_change - Handle a config change from the adapter
845 * @ioa_cfg: ioa config struct
846 * @hostrcb: hostrcb
847 *
848 * Return value:
849 * none
850 **/
851static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
852 struct ipr_hostrcb *hostrcb)
853{
854 struct ipr_resource_entry *res = NULL;
855 struct ipr_config_table_entry *cfgte;
856 u32 is_ndn = 1;
857
858 cfgte = &hostrcb->hcam.u.ccn.cfgte;
859
860 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
861 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
862 sizeof(cfgte->res_addr))) {
863 is_ndn = 0;
864 break;
865 }
866 }
867
868 if (is_ndn) {
869 if (list_empty(&ioa_cfg->free_res_q)) {
870 ipr_send_hcam(ioa_cfg,
871 IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
872 hostrcb);
873 return;
874 }
875
876 res = list_entry(ioa_cfg->free_res_q.next,
877 struct ipr_resource_entry, queue);
878
879 list_del(&res->queue);
880 ipr_init_res_entry(res);
881 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
882 }
883
884 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
885
886 if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
887 if (res->sdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 res->del_from_ml = 1;
Brian King1121b792006-03-29 09:37:16 -0600889 res->cfgte.res_handle = IPR_INVALID_RES_HANDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (ioa_cfg->allow_ml_add_del)
891 schedule_work(&ioa_cfg->work_q);
892 } else
893 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
894 } else if (!res->sdev) {
895 res->add_to_ml = 1;
896 if (ioa_cfg->allow_ml_add_del)
897 schedule_work(&ioa_cfg->work_q);
898 }
899
900 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
901}
902
903/**
904 * ipr_process_ccn - Op done function for a CCN.
905 * @ipr_cmd: ipr command struct
906 *
907 * This function is the op done function for a configuration
908 * change notification host controlled async from the adapter.
909 *
910 * Return value:
911 * none
912 **/
913static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
914{
915 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
916 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
917 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
918
919 list_del(&hostrcb->queue);
920 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
921
922 if (ioasc) {
923 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
924 dev_err(&ioa_cfg->pdev->dev,
925 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
926
927 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
928 } else {
929 ipr_handle_config_change(ioa_cfg, hostrcb);
930 }
931}
932
933/**
Brian King8cf093e2007-04-26 16:00:14 -0500934 * strip_and_pad_whitespace - Strip and pad trailing whitespace.
935 * @i: index into buffer
936 * @buf: string to modify
937 *
938 * This function will strip all trailing whitespace, pad the end
939 * of the string with a single space, and NULL terminate the string.
940 *
941 * Return value:
942 * new length of string
943 **/
944static int strip_and_pad_whitespace(int i, char *buf)
945{
946 while (i && buf[i] == ' ')
947 i--;
948 buf[i+1] = ' ';
949 buf[i+2] = '\0';
950 return i + 2;
951}
952
953/**
954 * ipr_log_vpd_compact - Log the passed extended VPD compactly.
955 * @prefix: string to print at start of printk
956 * @hostrcb: hostrcb pointer
957 * @vpd: vendor/product id/sn struct
958 *
959 * Return value:
960 * none
961 **/
962static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
963 struct ipr_vpd *vpd)
964{
965 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
966 int i = 0;
967
968 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
969 i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
970
971 memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
972 i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
973
974 memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
975 buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
976
977 ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
978}
979
980/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * ipr_log_vpd - Log the passed VPD to the error log.
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600982 * @vpd: vendor/product id/sn struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 *
984 * Return value:
985 * none
986 **/
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600987static void ipr_log_vpd(struct ipr_vpd *vpd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
990 + IPR_SERIAL_NUM_LEN];
991
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600992 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
993 memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 IPR_PROD_ID_LEN);
995 buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
996 ipr_err("Vendor/Product ID: %s\n", buffer);
997
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600998 memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 buffer[IPR_SERIAL_NUM_LEN] = '\0';
1000 ipr_err(" Serial Number: %s\n", buffer);
1001}
1002
1003/**
Brian King8cf093e2007-04-26 16:00:14 -05001004 * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1005 * @prefix: string to print at start of printk
1006 * @hostrcb: hostrcb pointer
1007 * @vpd: vendor/product id/sn/wwn struct
1008 *
1009 * Return value:
1010 * none
1011 **/
1012static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1013 struct ipr_ext_vpd *vpd)
1014{
1015 ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1016 ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1017 be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1018}
1019
1020/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001021 * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1022 * @vpd: vendor/product id/sn/wwn struct
1023 *
1024 * Return value:
1025 * none
1026 **/
1027static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1028{
1029 ipr_log_vpd(&vpd->vpd);
1030 ipr_err(" WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1031 be32_to_cpu(vpd->wwid[1]));
1032}
1033
1034/**
1035 * ipr_log_enhanced_cache_error - Log a cache error.
1036 * @ioa_cfg: ioa config struct
1037 * @hostrcb: hostrcb struct
1038 *
1039 * Return value:
1040 * none
1041 **/
1042static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1043 struct ipr_hostrcb *hostrcb)
1044{
1045 struct ipr_hostrcb_type_12_error *error =
1046 &hostrcb->hcam.u.error.u.type_12_error;
1047
1048 ipr_err("-----Current Configuration-----\n");
1049 ipr_err("Cache Directory Card Information:\n");
1050 ipr_log_ext_vpd(&error->ioa_vpd);
1051 ipr_err("Adapter Card Information:\n");
1052 ipr_log_ext_vpd(&error->cfc_vpd);
1053
1054 ipr_err("-----Expected Configuration-----\n");
1055 ipr_err("Cache Directory Card Information:\n");
1056 ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1057 ipr_err("Adapter Card Information:\n");
1058 ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1059
1060 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1061 be32_to_cpu(error->ioa_data[0]),
1062 be32_to_cpu(error->ioa_data[1]),
1063 be32_to_cpu(error->ioa_data[2]));
1064}
1065
1066/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 * ipr_log_cache_error - Log a cache error.
1068 * @ioa_cfg: ioa config struct
1069 * @hostrcb: hostrcb struct
1070 *
1071 * Return value:
1072 * none
1073 **/
1074static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1075 struct ipr_hostrcb *hostrcb)
1076{
1077 struct ipr_hostrcb_type_02_error *error =
1078 &hostrcb->hcam.u.error.u.type_02_error;
1079
1080 ipr_err("-----Current Configuration-----\n");
1081 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001082 ipr_log_vpd(&error->ioa_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001084 ipr_log_vpd(&error->cfc_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 ipr_err("-----Expected Configuration-----\n");
1087 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001088 ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001090 ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1093 be32_to_cpu(error->ioa_data[0]),
1094 be32_to_cpu(error->ioa_data[1]),
1095 be32_to_cpu(error->ioa_data[2]));
1096}
1097
1098/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001099 * ipr_log_enhanced_config_error - Log a configuration error.
1100 * @ioa_cfg: ioa config struct
1101 * @hostrcb: hostrcb struct
1102 *
1103 * Return value:
1104 * none
1105 **/
1106static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1107 struct ipr_hostrcb *hostrcb)
1108{
1109 int errors_logged, i;
1110 struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1111 struct ipr_hostrcb_type_13_error *error;
1112
1113 error = &hostrcb->hcam.u.error.u.type_13_error;
1114 errors_logged = be32_to_cpu(error->errors_logged);
1115
1116 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1117 be32_to_cpu(error->errors_detected), errors_logged);
1118
1119 dev_entry = error->dev;
1120
1121 for (i = 0; i < errors_logged; i++, dev_entry++) {
1122 ipr_err_separator;
1123
1124 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1125 ipr_log_ext_vpd(&dev_entry->vpd);
1126
1127 ipr_err("-----New Device Information-----\n");
1128 ipr_log_ext_vpd(&dev_entry->new_vpd);
1129
1130 ipr_err("Cache Directory Card Information:\n");
1131 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1132
1133 ipr_err("Adapter Card Information:\n");
1134 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1135 }
1136}
1137
1138/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * ipr_log_config_error - Log a configuration error.
1140 * @ioa_cfg: ioa config struct
1141 * @hostrcb: hostrcb struct
1142 *
1143 * Return value:
1144 * none
1145 **/
1146static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1147 struct ipr_hostrcb *hostrcb)
1148{
1149 int errors_logged, i;
1150 struct ipr_hostrcb_device_data_entry *dev_entry;
1151 struct ipr_hostrcb_type_03_error *error;
1152
1153 error = &hostrcb->hcam.u.error.u.type_03_error;
1154 errors_logged = be32_to_cpu(error->errors_logged);
1155
1156 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1157 be32_to_cpu(error->errors_detected), errors_logged);
1158
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001159 dev_entry = error->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 for (i = 0; i < errors_logged; i++, dev_entry++) {
1162 ipr_err_separator;
1163
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001164 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001165 ipr_log_vpd(&dev_entry->vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 ipr_err("-----New Device Information-----\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001168 ipr_log_vpd(&dev_entry->new_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001171 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001174 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1177 be32_to_cpu(dev_entry->ioa_data[0]),
1178 be32_to_cpu(dev_entry->ioa_data[1]),
1179 be32_to_cpu(dev_entry->ioa_data[2]),
1180 be32_to_cpu(dev_entry->ioa_data[3]),
1181 be32_to_cpu(dev_entry->ioa_data[4]));
1182 }
1183}
1184
1185/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001186 * ipr_log_enhanced_array_error - Log an array configuration error.
1187 * @ioa_cfg: ioa config struct
1188 * @hostrcb: hostrcb struct
1189 *
1190 * Return value:
1191 * none
1192 **/
1193static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1194 struct ipr_hostrcb *hostrcb)
1195{
1196 int i, num_entries;
1197 struct ipr_hostrcb_type_14_error *error;
1198 struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1199 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1200
1201 error = &hostrcb->hcam.u.error.u.type_14_error;
1202
1203 ipr_err_separator;
1204
1205 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1206 error->protection_level,
1207 ioa_cfg->host->host_no,
1208 error->last_func_vset_res_addr.bus,
1209 error->last_func_vset_res_addr.target,
1210 error->last_func_vset_res_addr.lun);
1211
1212 ipr_err_separator;
1213
1214 array_entry = error->array_member;
1215 num_entries = min_t(u32, be32_to_cpu(error->num_entries),
1216 sizeof(error->array_member));
1217
1218 for (i = 0; i < num_entries; i++, array_entry++) {
1219 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1220 continue;
1221
1222 if (be32_to_cpu(error->exposed_mode_adn) == i)
1223 ipr_err("Exposed Array Member %d:\n", i);
1224 else
1225 ipr_err("Array Member %d:\n", i);
1226
1227 ipr_log_ext_vpd(&array_entry->vpd);
1228 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1229 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1230 "Expected Location");
1231
1232 ipr_err_separator;
1233 }
1234}
1235
1236/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 * ipr_log_array_error - Log an array configuration error.
1238 * @ioa_cfg: ioa config struct
1239 * @hostrcb: hostrcb struct
1240 *
1241 * Return value:
1242 * none
1243 **/
1244static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1245 struct ipr_hostrcb *hostrcb)
1246{
1247 int i;
1248 struct ipr_hostrcb_type_04_error *error;
1249 struct ipr_hostrcb_array_data_entry *array_entry;
1250 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1251
1252 error = &hostrcb->hcam.u.error.u.type_04_error;
1253
1254 ipr_err_separator;
1255
1256 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1257 error->protection_level,
1258 ioa_cfg->host->host_no,
1259 error->last_func_vset_res_addr.bus,
1260 error->last_func_vset_res_addr.target,
1261 error->last_func_vset_res_addr.lun);
1262
1263 ipr_err_separator;
1264
1265 array_entry = error->array_member;
1266
1267 for (i = 0; i < 18; i++) {
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001268 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 continue;
1270
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001271 if (be32_to_cpu(error->exposed_mode_adn) == i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 ipr_err("Exposed Array Member %d:\n", i);
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001273 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 ipr_err("Array Member %d:\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001276 ipr_log_vpd(&array_entry->vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001278 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1279 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1280 "Expected Location");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 ipr_err_separator;
1283
1284 if (i == 9)
1285 array_entry = error->array_member2;
1286 else
1287 array_entry++;
1288 }
1289}
1290
1291/**
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001292 * ipr_log_hex_data - Log additional hex IOA error data.
Brian Kingac719ab2006-11-21 10:28:42 -06001293 * @ioa_cfg: ioa config struct
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001294 * @data: IOA error data
1295 * @len: data length
1296 *
1297 * Return value:
1298 * none
1299 **/
Brian Kingac719ab2006-11-21 10:28:42 -06001300static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001301{
1302 int i;
1303
1304 if (len == 0)
1305 return;
1306
Brian Kingac719ab2006-11-21 10:28:42 -06001307 if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1308 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1309
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001310 for (i = 0; i < len / 4; i += 4) {
1311 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1312 be32_to_cpu(data[i]),
1313 be32_to_cpu(data[i+1]),
1314 be32_to_cpu(data[i+2]),
1315 be32_to_cpu(data[i+3]));
1316 }
1317}
1318
1319/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001320 * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1321 * @ioa_cfg: ioa config struct
1322 * @hostrcb: hostrcb struct
1323 *
1324 * Return value:
1325 * none
1326 **/
1327static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1328 struct ipr_hostrcb *hostrcb)
1329{
1330 struct ipr_hostrcb_type_17_error *error;
1331
1332 error = &hostrcb->hcam.u.error.u.type_17_error;
1333 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
Brian King8cf093e2007-04-26 16:00:14 -05001334 strstrip(error->failure_reason);
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001335
Brian King8cf093e2007-04-26 16:00:14 -05001336 ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1337 be32_to_cpu(hostrcb->hcam.u.error.prc));
1338 ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
Brian Kingac719ab2006-11-21 10:28:42 -06001339 ipr_log_hex_data(ioa_cfg, error->data,
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001340 be32_to_cpu(hostrcb->hcam.length) -
1341 (offsetof(struct ipr_hostrcb_error, u) +
1342 offsetof(struct ipr_hostrcb_type_17_error, data)));
1343}
1344
1345/**
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001346 * ipr_log_dual_ioa_error - Log a dual adapter error.
1347 * @ioa_cfg: ioa config struct
1348 * @hostrcb: hostrcb struct
1349 *
1350 * Return value:
1351 * none
1352 **/
1353static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1354 struct ipr_hostrcb *hostrcb)
1355{
1356 struct ipr_hostrcb_type_07_error *error;
1357
1358 error = &hostrcb->hcam.u.error.u.type_07_error;
1359 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
Brian King8cf093e2007-04-26 16:00:14 -05001360 strstrip(error->failure_reason);
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001361
Brian King8cf093e2007-04-26 16:00:14 -05001362 ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1363 be32_to_cpu(hostrcb->hcam.u.error.prc));
1364 ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
Brian Kingac719ab2006-11-21 10:28:42 -06001365 ipr_log_hex_data(ioa_cfg, error->data,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001366 be32_to_cpu(hostrcb->hcam.length) -
1367 (offsetof(struct ipr_hostrcb_error, u) +
1368 offsetof(struct ipr_hostrcb_type_07_error, data)));
1369}
1370
Brian King49dc6a12006-11-21 10:28:35 -06001371static const struct {
1372 u8 active;
1373 char *desc;
1374} path_active_desc[] = {
1375 { IPR_PATH_NO_INFO, "Path" },
1376 { IPR_PATH_ACTIVE, "Active path" },
1377 { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1378};
1379
1380static const struct {
1381 u8 state;
1382 char *desc;
1383} path_state_desc[] = {
1384 { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1385 { IPR_PATH_HEALTHY, "is healthy" },
1386 { IPR_PATH_DEGRADED, "is degraded" },
1387 { IPR_PATH_FAILED, "is failed" }
1388};
1389
1390/**
1391 * ipr_log_fabric_path - Log a fabric path error
1392 * @hostrcb: hostrcb struct
1393 * @fabric: fabric descriptor
1394 *
1395 * Return value:
1396 * none
1397 **/
1398static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1399 struct ipr_hostrcb_fabric_desc *fabric)
1400{
1401 int i, j;
1402 u8 path_state = fabric->path_state;
1403 u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1404 u8 state = path_state & IPR_PATH_STATE_MASK;
1405
1406 for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1407 if (path_active_desc[i].active != active)
1408 continue;
1409
1410 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1411 if (path_state_desc[j].state != state)
1412 continue;
1413
1414 if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
1415 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
1416 path_active_desc[i].desc, path_state_desc[j].desc,
1417 fabric->ioa_port);
1418 } else if (fabric->cascaded_expander == 0xff) {
1419 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
1420 path_active_desc[i].desc, path_state_desc[j].desc,
1421 fabric->ioa_port, fabric->phy);
1422 } else if (fabric->phy == 0xff) {
1423 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
1424 path_active_desc[i].desc, path_state_desc[j].desc,
1425 fabric->ioa_port, fabric->cascaded_expander);
1426 } else {
1427 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
1428 path_active_desc[i].desc, path_state_desc[j].desc,
1429 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1430 }
1431 return;
1432 }
1433 }
1434
1435 ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
1436 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1437}
1438
1439static const struct {
1440 u8 type;
1441 char *desc;
1442} path_type_desc[] = {
1443 { IPR_PATH_CFG_IOA_PORT, "IOA port" },
1444 { IPR_PATH_CFG_EXP_PORT, "Expander port" },
1445 { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
1446 { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
1447};
1448
1449static const struct {
1450 u8 status;
1451 char *desc;
1452} path_status_desc[] = {
1453 { IPR_PATH_CFG_NO_PROB, "Functional" },
1454 { IPR_PATH_CFG_DEGRADED, "Degraded" },
1455 { IPR_PATH_CFG_FAILED, "Failed" },
1456 { IPR_PATH_CFG_SUSPECT, "Suspect" },
1457 { IPR_PATH_NOT_DETECTED, "Missing" },
1458 { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
1459};
1460
1461static const char *link_rate[] = {
1462 "unknown",
1463 "disabled",
1464 "phy reset problem",
1465 "spinup hold",
1466 "port selector",
1467 "unknown",
1468 "unknown",
1469 "unknown",
1470 "1.5Gbps",
1471 "3.0Gbps",
1472 "unknown",
1473 "unknown",
1474 "unknown",
1475 "unknown",
1476 "unknown",
1477 "unknown"
1478};
1479
1480/**
1481 * ipr_log_path_elem - Log a fabric path element.
1482 * @hostrcb: hostrcb struct
1483 * @cfg: fabric path element struct
1484 *
1485 * Return value:
1486 * none
1487 **/
1488static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
1489 struct ipr_hostrcb_config_element *cfg)
1490{
1491 int i, j;
1492 u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
1493 u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
1494
1495 if (type == IPR_PATH_CFG_NOT_EXIST)
1496 return;
1497
1498 for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
1499 if (path_type_desc[i].type != type)
1500 continue;
1501
1502 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
1503 if (path_status_desc[j].status != status)
1504 continue;
1505
1506 if (type == IPR_PATH_CFG_IOA_PORT) {
1507 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
1508 path_status_desc[j].desc, path_type_desc[i].desc,
1509 cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1510 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1511 } else {
1512 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
1513 ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
1514 path_status_desc[j].desc, path_type_desc[i].desc,
1515 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1516 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1517 } else if (cfg->cascaded_expander == 0xff) {
1518 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
1519 "WWN=%08X%08X\n", path_status_desc[j].desc,
1520 path_type_desc[i].desc, cfg->phy,
1521 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1522 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1523 } else if (cfg->phy == 0xff) {
1524 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
1525 "WWN=%08X%08X\n", path_status_desc[j].desc,
1526 path_type_desc[i].desc, cfg->cascaded_expander,
1527 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1528 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1529 } else {
1530 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
1531 "WWN=%08X%08X\n", path_status_desc[j].desc,
1532 path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
1533 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1534 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1535 }
1536 }
1537 return;
1538 }
1539 }
1540
1541 ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
1542 "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
1543 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1544 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1545}
1546
1547/**
1548 * ipr_log_fabric_error - Log a fabric error.
1549 * @ioa_cfg: ioa config struct
1550 * @hostrcb: hostrcb struct
1551 *
1552 * Return value:
1553 * none
1554 **/
1555static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
1556 struct ipr_hostrcb *hostrcb)
1557{
1558 struct ipr_hostrcb_type_20_error *error;
1559 struct ipr_hostrcb_fabric_desc *fabric;
1560 struct ipr_hostrcb_config_element *cfg;
1561 int i, add_len;
1562
1563 error = &hostrcb->hcam.u.error.u.type_20_error;
1564 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1565 ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
1566
1567 add_len = be32_to_cpu(hostrcb->hcam.length) -
1568 (offsetof(struct ipr_hostrcb_error, u) +
1569 offsetof(struct ipr_hostrcb_type_20_error, desc));
1570
1571 for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
1572 ipr_log_fabric_path(hostrcb, fabric);
1573 for_each_fabric_cfg(fabric, cfg)
1574 ipr_log_path_elem(hostrcb, cfg);
1575
1576 add_len -= be16_to_cpu(fabric->length);
1577 fabric = (struct ipr_hostrcb_fabric_desc *)
1578 ((unsigned long)fabric + be16_to_cpu(fabric->length));
1579 }
1580
Brian Kingac719ab2006-11-21 10:28:42 -06001581 ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
Brian King49dc6a12006-11-21 10:28:35 -06001582}
1583
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001584/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 * ipr_log_generic_error - Log an adapter error.
1586 * @ioa_cfg: ioa config struct
1587 * @hostrcb: hostrcb struct
1588 *
1589 * Return value:
1590 * none
1591 **/
1592static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1593 struct ipr_hostrcb *hostrcb)
1594{
Brian Kingac719ab2006-11-21 10:28:42 -06001595 ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001596 be32_to_cpu(hostrcb->hcam.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
1599/**
1600 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1601 * @ioasc: IOASC
1602 *
1603 * This function will return the index of into the ipr_error_table
1604 * for the specified IOASC. If the IOASC is not in the table,
1605 * 0 will be returned, which points to the entry used for unknown errors.
1606 *
1607 * Return value:
1608 * index into the ipr_error_table
1609 **/
1610static u32 ipr_get_error(u32 ioasc)
1611{
1612 int i;
1613
1614 for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
Brian King35a39692006-09-25 12:39:20 -05001615 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return i;
1617
1618 return 0;
1619}
1620
1621/**
1622 * ipr_handle_log_data - Log an adapter error.
1623 * @ioa_cfg: ioa config struct
1624 * @hostrcb: hostrcb struct
1625 *
1626 * This function logs an adapter error to the system.
1627 *
1628 * Return value:
1629 * none
1630 **/
1631static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1632 struct ipr_hostrcb *hostrcb)
1633{
1634 u32 ioasc;
1635 int error_index;
1636
1637 if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1638 return;
1639
1640 if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1641 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1642
1643 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1644
1645 if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1646 ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1647 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1648 scsi_report_bus_reset(ioa_cfg->host,
1649 hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1650 }
1651
1652 error_index = ipr_get_error(ioasc);
1653
1654 if (!ipr_error_table[error_index].log_hcam)
1655 return;
1656
Brian King49dc6a12006-11-21 10:28:35 -06001657 ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /* Set indication we have logged an error */
1660 ioa_cfg->errors_logged++;
1661
Brian King933916f2007-03-29 12:43:30 -05001662 if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return;
brking@us.ibm.comcf852032005-11-01 17:00:47 -06001664 if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
1665 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 switch (hostrcb->hcam.overlay_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 case IPR_HOST_RCB_OVERLAY_ID_2:
1669 ipr_log_cache_error(ioa_cfg, hostrcb);
1670 break;
1671 case IPR_HOST_RCB_OVERLAY_ID_3:
1672 ipr_log_config_error(ioa_cfg, hostrcb);
1673 break;
1674 case IPR_HOST_RCB_OVERLAY_ID_4:
1675 case IPR_HOST_RCB_OVERLAY_ID_6:
1676 ipr_log_array_error(ioa_cfg, hostrcb);
1677 break;
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001678 case IPR_HOST_RCB_OVERLAY_ID_7:
1679 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
1680 break;
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001681 case IPR_HOST_RCB_OVERLAY_ID_12:
1682 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
1683 break;
1684 case IPR_HOST_RCB_OVERLAY_ID_13:
1685 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
1686 break;
1687 case IPR_HOST_RCB_OVERLAY_ID_14:
1688 case IPR_HOST_RCB_OVERLAY_ID_16:
1689 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
1690 break;
1691 case IPR_HOST_RCB_OVERLAY_ID_17:
1692 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
1693 break;
Brian King49dc6a12006-11-21 10:28:35 -06001694 case IPR_HOST_RCB_OVERLAY_ID_20:
1695 ipr_log_fabric_error(ioa_cfg, hostrcb);
1696 break;
brking@us.ibm.comcf852032005-11-01 17:00:47 -06001697 case IPR_HOST_RCB_OVERLAY_ID_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 default:
brking@us.ibm.coma9cfca92005-11-01 17:00:41 -06001700 ipr_log_generic_error(ioa_cfg, hostrcb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 break;
1702 }
1703}
1704
1705/**
1706 * ipr_process_error - Op done function for an adapter error log.
1707 * @ipr_cmd: ipr command struct
1708 *
1709 * This function is the op done function for an error log host
1710 * controlled async from the adapter. It will log the error and
1711 * send the HCAM back to the adapter.
1712 *
1713 * Return value:
1714 * none
1715 **/
1716static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1717{
1718 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1719 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1720 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
Brian King65f56472007-04-26 16:00:12 -05001721 u32 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 list_del(&hostrcb->queue);
1724 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1725
1726 if (!ioasc) {
1727 ipr_handle_log_data(ioa_cfg, hostrcb);
Brian King65f56472007-04-26 16:00:12 -05001728 if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
1729 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1731 dev_err(&ioa_cfg->pdev->dev,
1732 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1733 }
1734
1735 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1736}
1737
1738/**
1739 * ipr_timeout - An internally generated op has timed out.
1740 * @ipr_cmd: ipr command struct
1741 *
1742 * This function blocks host requests and initiates an
1743 * adapter reset.
1744 *
1745 * Return value:
1746 * none
1747 **/
1748static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1749{
1750 unsigned long lock_flags = 0;
1751 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1752
1753 ENTER;
1754 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1755
1756 ioa_cfg->errors_logged++;
1757 dev_err(&ioa_cfg->pdev->dev,
1758 "Adapter being reset due to command timeout.\n");
1759
1760 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1761 ioa_cfg->sdt_state = GET_DUMP;
1762
1763 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1764 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1765
1766 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1767 LEAVE;
1768}
1769
1770/**
1771 * ipr_oper_timeout - Adapter timed out transitioning to operational
1772 * @ipr_cmd: ipr command struct
1773 *
1774 * This function blocks host requests and initiates an
1775 * adapter reset.
1776 *
1777 * Return value:
1778 * none
1779 **/
1780static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
1781{
1782 unsigned long lock_flags = 0;
1783 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1784
1785 ENTER;
1786 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1787
1788 ioa_cfg->errors_logged++;
1789 dev_err(&ioa_cfg->pdev->dev,
1790 "Adapter timed out transitioning to operational.\n");
1791
1792 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1793 ioa_cfg->sdt_state = GET_DUMP;
1794
1795 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
1796 if (ipr_fastfail)
1797 ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
1798 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1799 }
1800
1801 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1802 LEAVE;
1803}
1804
1805/**
1806 * ipr_reset_reload - Reset/Reload the IOA
1807 * @ioa_cfg: ioa config struct
1808 * @shutdown_type: shutdown type
1809 *
1810 * This function resets the adapter and re-initializes it.
1811 * This function assumes that all new host commands have been stopped.
1812 * Return value:
1813 * SUCCESS / FAILED
1814 **/
1815static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1816 enum ipr_shutdown_type shutdown_type)
1817{
1818 if (!ioa_cfg->in_reset_reload)
1819 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1820
1821 spin_unlock_irq(ioa_cfg->host->host_lock);
1822 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1823 spin_lock_irq(ioa_cfg->host->host_lock);
1824
1825 /* If we got hit with a host reset while we were already resetting
1826 the adapter for some reason, and the reset failed. */
1827 if (ioa_cfg->ioa_is_dead) {
1828 ipr_trace;
1829 return FAILED;
1830 }
1831
1832 return SUCCESS;
1833}
1834
1835/**
1836 * ipr_find_ses_entry - Find matching SES in SES table
1837 * @res: resource entry struct of SES
1838 *
1839 * Return value:
1840 * pointer to SES table entry / NULL on failure
1841 **/
1842static const struct ipr_ses_table_entry *
1843ipr_find_ses_entry(struct ipr_resource_entry *res)
1844{
1845 int i, j, matches;
1846 const struct ipr_ses_table_entry *ste = ipr_ses_table;
1847
1848 for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1849 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1850 if (ste->compare_product_id_byte[j] == 'X') {
1851 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1852 matches++;
1853 else
1854 break;
1855 } else
1856 matches++;
1857 }
1858
1859 if (matches == IPR_PROD_ID_LEN)
1860 return ste;
1861 }
1862
1863 return NULL;
1864}
1865
1866/**
1867 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1868 * @ioa_cfg: ioa config struct
1869 * @bus: SCSI bus
1870 * @bus_width: bus width
1871 *
1872 * Return value:
1873 * SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1874 * For a 2-byte wide SCSI bus, the maximum transfer speed is
1875 * twice the maximum transfer rate (e.g. for a wide enabled bus,
1876 * max 160MHz = max 320MB/sec).
1877 **/
1878static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1879{
1880 struct ipr_resource_entry *res;
1881 const struct ipr_ses_table_entry *ste;
1882 u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1883
1884 /* Loop through each config table entry in the config table buffer */
1885 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1886 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1887 continue;
1888
1889 if (bus != res->cfgte.res_addr.bus)
1890 continue;
1891
1892 if (!(ste = ipr_find_ses_entry(res)))
1893 continue;
1894
1895 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1896 }
1897
1898 return max_xfer_rate;
1899}
1900
1901/**
1902 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1903 * @ioa_cfg: ioa config struct
1904 * @max_delay: max delay in micro-seconds to wait
1905 *
1906 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1907 *
1908 * Return value:
1909 * 0 on success / other on failure
1910 **/
1911static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1912{
1913 volatile u32 pcii_reg;
1914 int delay = 1;
1915
1916 /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1917 while (delay < max_delay) {
1918 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1919
1920 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1921 return 0;
1922
1923 /* udelay cannot be used if delay is more than a few milliseconds */
1924 if ((delay / 1000) > MAX_UDELAY_MS)
1925 mdelay(delay / 1000);
1926 else
1927 udelay(delay);
1928
1929 delay += delay;
1930 }
1931 return -EIO;
1932}
1933
1934/**
1935 * ipr_get_ldump_data_section - Dump IOA memory
1936 * @ioa_cfg: ioa config struct
1937 * @start_addr: adapter address to dump
1938 * @dest: destination kernel buffer
1939 * @length_in_words: length to dump in 4 byte words
1940 *
1941 * Return value:
1942 * 0 on success / -EIO on failure
1943 **/
1944static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1945 u32 start_addr,
1946 __be32 *dest, u32 length_in_words)
1947{
1948 volatile u32 temp_pcii_reg;
1949 int i, delay = 0;
1950
1951 /* Write IOA interrupt reg starting LDUMP state */
1952 writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1953 ioa_cfg->regs.set_uproc_interrupt_reg);
1954
1955 /* Wait for IO debug acknowledge */
1956 if (ipr_wait_iodbg_ack(ioa_cfg,
1957 IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1958 dev_err(&ioa_cfg->pdev->dev,
1959 "IOA dump long data transfer timeout\n");
1960 return -EIO;
1961 }
1962
1963 /* Signal LDUMP interlocked - clear IO debug ack */
1964 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1965 ioa_cfg->regs.clr_interrupt_reg);
1966
1967 /* Write Mailbox with starting address */
1968 writel(start_addr, ioa_cfg->ioa_mailbox);
1969
1970 /* Signal address valid - clear IOA Reset alert */
1971 writel(IPR_UPROCI_RESET_ALERT,
1972 ioa_cfg->regs.clr_uproc_interrupt_reg);
1973
1974 for (i = 0; i < length_in_words; i++) {
1975 /* Wait for IO debug acknowledge */
1976 if (ipr_wait_iodbg_ack(ioa_cfg,
1977 IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1978 dev_err(&ioa_cfg->pdev->dev,
1979 "IOA dump short data transfer timeout\n");
1980 return -EIO;
1981 }
1982
1983 /* Read data from mailbox and increment destination pointer */
1984 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1985 dest++;
1986
1987 /* For all but the last word of data, signal data received */
1988 if (i < (length_in_words - 1)) {
1989 /* Signal dump data received - Clear IO debug Ack */
1990 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1991 ioa_cfg->regs.clr_interrupt_reg);
1992 }
1993 }
1994
1995 /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1996 writel(IPR_UPROCI_RESET_ALERT,
1997 ioa_cfg->regs.set_uproc_interrupt_reg);
1998
1999 writel(IPR_UPROCI_IO_DEBUG_ALERT,
2000 ioa_cfg->regs.clr_uproc_interrupt_reg);
2001
2002 /* Signal dump data received - Clear IO debug Ack */
2003 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2004 ioa_cfg->regs.clr_interrupt_reg);
2005
2006 /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2007 while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2008 temp_pcii_reg =
2009 readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
2010
2011 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2012 return 0;
2013
2014 udelay(10);
2015 delay += 10;
2016 }
2017
2018 return 0;
2019}
2020
2021#ifdef CONFIG_SCSI_IPR_DUMP
2022/**
2023 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2024 * @ioa_cfg: ioa config struct
2025 * @pci_address: adapter address
2026 * @length: length of data to copy
2027 *
2028 * Copy data from PCI adapter to kernel buffer.
2029 * Note: length MUST be a 4 byte multiple
2030 * Return value:
2031 * 0 on success / other on failure
2032 **/
2033static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2034 unsigned long pci_address, u32 length)
2035{
2036 int bytes_copied = 0;
2037 int cur_len, rc, rem_len, rem_page_len;
2038 __be32 *page;
2039 unsigned long lock_flags = 0;
2040 struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2041
2042 while (bytes_copied < length &&
2043 (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
2044 if (ioa_dump->page_offset >= PAGE_SIZE ||
2045 ioa_dump->page_offset == 0) {
2046 page = (__be32 *)__get_free_page(GFP_ATOMIC);
2047
2048 if (!page) {
2049 ipr_trace;
2050 return bytes_copied;
2051 }
2052
2053 ioa_dump->page_offset = 0;
2054 ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2055 ioa_dump->next_page_index++;
2056 } else
2057 page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2058
2059 rem_len = length - bytes_copied;
2060 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2061 cur_len = min(rem_len, rem_page_len);
2062
2063 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2064 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2065 rc = -EIO;
2066 } else {
2067 rc = ipr_get_ldump_data_section(ioa_cfg,
2068 pci_address + bytes_copied,
2069 &page[ioa_dump->page_offset / 4],
2070 (cur_len / sizeof(u32)));
2071 }
2072 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2073
2074 if (!rc) {
2075 ioa_dump->page_offset += cur_len;
2076 bytes_copied += cur_len;
2077 } else {
2078 ipr_trace;
2079 break;
2080 }
2081 schedule();
2082 }
2083
2084 return bytes_copied;
2085}
2086
2087/**
2088 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2089 * @hdr: dump entry header struct
2090 *
2091 * Return value:
2092 * nothing
2093 **/
2094static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2095{
2096 hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2097 hdr->num_elems = 1;
2098 hdr->offset = sizeof(*hdr);
2099 hdr->status = IPR_DUMP_STATUS_SUCCESS;
2100}
2101
2102/**
2103 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2104 * @ioa_cfg: ioa config struct
2105 * @driver_dump: driver dump struct
2106 *
2107 * Return value:
2108 * nothing
2109 **/
2110static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2111 struct ipr_driver_dump *driver_dump)
2112{
2113 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2114
2115 ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2116 driver_dump->ioa_type_entry.hdr.len =
2117 sizeof(struct ipr_dump_ioa_type_entry) -
2118 sizeof(struct ipr_dump_entry_header);
2119 driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2120 driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2121 driver_dump->ioa_type_entry.type = ioa_cfg->type;
2122 driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2123 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2124 ucode_vpd->minor_release[1];
2125 driver_dump->hdr.num_entries++;
2126}
2127
2128/**
2129 * ipr_dump_version_data - Fill in the driver version in the dump.
2130 * @ioa_cfg: ioa config struct
2131 * @driver_dump: driver dump struct
2132 *
2133 * Return value:
2134 * nothing
2135 **/
2136static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2137 struct ipr_driver_dump *driver_dump)
2138{
2139 ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2140 driver_dump->version_entry.hdr.len =
2141 sizeof(struct ipr_dump_version_entry) -
2142 sizeof(struct ipr_dump_entry_header);
2143 driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2144 driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2145 strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2146 driver_dump->hdr.num_entries++;
2147}
2148
2149/**
2150 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2151 * @ioa_cfg: ioa config struct
2152 * @driver_dump: driver dump struct
2153 *
2154 * Return value:
2155 * nothing
2156 **/
2157static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
2158 struct ipr_driver_dump *driver_dump)
2159{
2160 ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
2161 driver_dump->trace_entry.hdr.len =
2162 sizeof(struct ipr_dump_trace_entry) -
2163 sizeof(struct ipr_dump_entry_header);
2164 driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2165 driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
2166 memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
2167 driver_dump->hdr.num_entries++;
2168}
2169
2170/**
2171 * ipr_dump_location_data - Fill in the IOA location in the dump.
2172 * @ioa_cfg: ioa config struct
2173 * @driver_dump: driver dump struct
2174 *
2175 * Return value:
2176 * nothing
2177 **/
2178static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
2179 struct ipr_driver_dump *driver_dump)
2180{
2181 ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
2182 driver_dump->location_entry.hdr.len =
2183 sizeof(struct ipr_dump_location_entry) -
2184 sizeof(struct ipr_dump_entry_header);
2185 driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2186 driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
2187 strcpy(driver_dump->location_entry.location, ioa_cfg->pdev->dev.bus_id);
2188 driver_dump->hdr.num_entries++;
2189}
2190
2191/**
2192 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
2193 * @ioa_cfg: ioa config struct
2194 * @dump: dump struct
2195 *
2196 * Return value:
2197 * nothing
2198 **/
2199static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
2200{
2201 unsigned long start_addr, sdt_word;
2202 unsigned long lock_flags = 0;
2203 struct ipr_driver_dump *driver_dump = &dump->driver_dump;
2204 struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
2205 u32 num_entries, start_off, end_off;
2206 u32 bytes_to_copy, bytes_copied, rc;
2207 struct ipr_sdt *sdt;
2208 int i;
2209
2210 ENTER;
2211
2212 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2213
2214 if (ioa_cfg->sdt_state != GET_DUMP) {
2215 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2216 return;
2217 }
2218
2219 start_addr = readl(ioa_cfg->ioa_mailbox);
2220
2221 if (!ipr_sdt_is_fmt2(start_addr)) {
2222 dev_err(&ioa_cfg->pdev->dev,
2223 "Invalid dump table format: %lx\n", start_addr);
2224 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2225 return;
2226 }
2227
2228 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
2229
2230 driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
2231
2232 /* Initialize the overall dump header */
2233 driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
2234 driver_dump->hdr.num_entries = 1;
2235 driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
2236 driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
2237 driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
2238 driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
2239
2240 ipr_dump_version_data(ioa_cfg, driver_dump);
2241 ipr_dump_location_data(ioa_cfg, driver_dump);
2242 ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
2243 ipr_dump_trace_data(ioa_cfg, driver_dump);
2244
2245 /* Update dump_header */
2246 driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
2247
2248 /* IOA Dump entry */
2249 ipr_init_dump_entry_hdr(&ioa_dump->hdr);
2250 ioa_dump->format = IPR_SDT_FMT2;
2251 ioa_dump->hdr.len = 0;
2252 ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2253 ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
2254
2255 /* First entries in sdt are actually a list of dump addresses and
2256 lengths to gather the real dump data. sdt represents the pointer
2257 to the ioa generated dump table. Dump data will be extracted based
2258 on entries in this table */
2259 sdt = &ioa_dump->sdt;
2260
2261 rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
2262 sizeof(struct ipr_sdt) / sizeof(__be32));
2263
2264 /* Smart Dump table is ready to use and the first entry is valid */
2265 if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
2266 dev_err(&ioa_cfg->pdev->dev,
2267 "Dump of IOA failed. Dump table not valid: %d, %X.\n",
2268 rc, be32_to_cpu(sdt->hdr.state));
2269 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
2270 ioa_cfg->sdt_state = DUMP_OBTAINED;
2271 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2272 return;
2273 }
2274
2275 num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
2276
2277 if (num_entries > IPR_NUM_SDT_ENTRIES)
2278 num_entries = IPR_NUM_SDT_ENTRIES;
2279
2280 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2281
2282 for (i = 0; i < num_entries; i++) {
2283 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
2284 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2285 break;
2286 }
2287
2288 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
2289 sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
2290 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
2291 end_off = be32_to_cpu(sdt->entry[i].end_offset);
2292
2293 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
2294 bytes_to_copy = end_off - start_off;
2295 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
2296 sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
2297 continue;
2298 }
2299
2300 /* Copy data from adapter to driver buffers */
2301 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
2302 bytes_to_copy);
2303
2304 ioa_dump->hdr.len += bytes_copied;
2305
2306 if (bytes_copied != bytes_to_copy) {
2307 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2308 break;
2309 }
2310 }
2311 }
2312 }
2313
2314 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
2315
2316 /* Update dump_header */
2317 driver_dump->hdr.len += ioa_dump->hdr.len;
2318 wmb();
2319 ioa_cfg->sdt_state = DUMP_OBTAINED;
2320 LEAVE;
2321}
2322
2323#else
2324#define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
2325#endif
2326
2327/**
2328 * ipr_release_dump - Free adapter dump memory
2329 * @kref: kref struct
2330 *
2331 * Return value:
2332 * nothing
2333 **/
2334static void ipr_release_dump(struct kref *kref)
2335{
2336 struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
2337 struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
2338 unsigned long lock_flags = 0;
2339 int i;
2340
2341 ENTER;
2342 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2343 ioa_cfg->dump = NULL;
2344 ioa_cfg->sdt_state = INACTIVE;
2345 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2346
2347 for (i = 0; i < dump->ioa_dump.next_page_index; i++)
2348 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
2349
2350 kfree(dump);
2351 LEAVE;
2352}
2353
2354/**
2355 * ipr_worker_thread - Worker thread
David Howellsc4028952006-11-22 14:57:56 +00002356 * @work: ioa config struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 *
2358 * Called at task level from a work thread. This function takes care
2359 * of adding and removing device from the mid-layer as configuration
2360 * changes are detected by the adapter.
2361 *
2362 * Return value:
2363 * nothing
2364 **/
David Howellsc4028952006-11-22 14:57:56 +00002365static void ipr_worker_thread(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
2367 unsigned long lock_flags;
2368 struct ipr_resource_entry *res;
2369 struct scsi_device *sdev;
2370 struct ipr_dump *dump;
David Howellsc4028952006-11-22 14:57:56 +00002371 struct ipr_ioa_cfg *ioa_cfg =
2372 container_of(work, struct ipr_ioa_cfg, work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 u8 bus, target, lun;
2374 int did_work;
2375
2376 ENTER;
2377 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2378
2379 if (ioa_cfg->sdt_state == GET_DUMP) {
2380 dump = ioa_cfg->dump;
2381 if (!dump) {
2382 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2383 return;
2384 }
2385 kref_get(&dump->kref);
2386 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2387 ipr_get_ioa_dump(ioa_cfg, dump);
2388 kref_put(&dump->kref, ipr_release_dump);
2389
2390 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2391 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
2392 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2393 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2394 return;
2395 }
2396
2397restart:
2398 do {
2399 did_work = 0;
2400 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
2401 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2402 return;
2403 }
2404
2405 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2406 if (res->del_from_ml && res->sdev) {
2407 did_work = 1;
2408 sdev = res->sdev;
2409 if (!scsi_device_get(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
2411 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2412 scsi_remove_device(sdev);
2413 scsi_device_put(sdev);
2414 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2415 }
2416 break;
2417 }
2418 }
2419 } while(did_work);
2420
2421 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2422 if (res->add_to_ml) {
2423 bus = res->cfgte.res_addr.bus;
2424 target = res->cfgte.res_addr.target;
2425 lun = res->cfgte.res_addr.lun;
Brian King1121b792006-03-29 09:37:16 -06002426 res->add_to_ml = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2428 scsi_add_device(ioa_cfg->host, bus, target, lun);
2429 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2430 goto restart;
2431 }
2432 }
2433
2434 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
Tony Jonesee959b02008-02-22 00:13:36 +01002435 kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 LEAVE;
2437}
2438
2439#ifdef CONFIG_SCSI_IPR_TRACE
2440/**
2441 * ipr_read_trace - Dump the adapter trace
2442 * @kobj: kobject struct
Zhang Rui91a69022007-06-09 13:57:22 +08002443 * @bin_attr: bin_attribute struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 * @buf: buffer
2445 * @off: offset
2446 * @count: buffer size
2447 *
2448 * Return value:
2449 * number of bytes printed to buffer
2450 **/
Zhang Rui91a69022007-06-09 13:57:22 +08002451static ssize_t ipr_read_trace(struct kobject *kobj,
2452 struct bin_attribute *bin_attr,
2453 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Tony Jonesee959b02008-02-22 00:13:36 +01002455 struct device *dev = container_of(kobj, struct device, kobj);
2456 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2458 unsigned long lock_flags = 0;
2459 int size = IPR_TRACE_SIZE;
2460 char *src = (char *)ioa_cfg->trace;
2461
2462 if (off > size)
2463 return 0;
2464 if (off + count > size) {
2465 size -= off;
2466 count = size;
2467 }
2468
2469 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2470 memcpy(buf, &src[off], count);
2471 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2472 return count;
2473}
2474
2475static struct bin_attribute ipr_trace_attr = {
2476 .attr = {
2477 .name = "trace",
2478 .mode = S_IRUGO,
2479 },
2480 .size = 0,
2481 .read = ipr_read_trace,
2482};
2483#endif
2484
brking@us.ibm.com62275042005-11-01 17:01:14 -06002485static const struct {
2486 enum ipr_cache_state state;
2487 char *name;
2488} cache_state [] = {
2489 { CACHE_NONE, "none" },
2490 { CACHE_DISABLED, "disabled" },
2491 { CACHE_ENABLED, "enabled" }
2492};
2493
2494/**
2495 * ipr_show_write_caching - Show the write caching attribute
Tony Jonesee959b02008-02-22 00:13:36 +01002496 * @dev: device struct
2497 * @buf: buffer
brking@us.ibm.com62275042005-11-01 17:01:14 -06002498 *
2499 * Return value:
2500 * number of bytes printed to buffer
2501 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002502static ssize_t ipr_show_write_caching(struct device *dev,
2503 struct device_attribute *attr, char *buf)
brking@us.ibm.com62275042005-11-01 17:01:14 -06002504{
Tony Jonesee959b02008-02-22 00:13:36 +01002505 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.com62275042005-11-01 17:01:14 -06002506 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2507 unsigned long lock_flags = 0;
2508 int i, len = 0;
2509
2510 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2511 for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2512 if (cache_state[i].state == ioa_cfg->cache_state) {
2513 len = snprintf(buf, PAGE_SIZE, "%s\n", cache_state[i].name);
2514 break;
2515 }
2516 }
2517 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2518 return len;
2519}
2520
2521
2522/**
2523 * ipr_store_write_caching - Enable/disable adapter write cache
Tony Jonesee959b02008-02-22 00:13:36 +01002524 * @dev: device struct
2525 * @buf: buffer
2526 * @count: buffer size
brking@us.ibm.com62275042005-11-01 17:01:14 -06002527 *
2528 * This function will enable/disable adapter write cache.
2529 *
2530 * Return value:
2531 * count on success / other on failure
2532 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002533static ssize_t ipr_store_write_caching(struct device *dev,
2534 struct device_attribute *attr,
2535 const char *buf, size_t count)
brking@us.ibm.com62275042005-11-01 17:01:14 -06002536{
Tony Jonesee959b02008-02-22 00:13:36 +01002537 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.com62275042005-11-01 17:01:14 -06002538 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2539 unsigned long lock_flags = 0;
2540 enum ipr_cache_state new_state = CACHE_INVALID;
2541 int i;
2542
2543 if (!capable(CAP_SYS_ADMIN))
2544 return -EACCES;
2545 if (ioa_cfg->cache_state == CACHE_NONE)
2546 return -EINVAL;
2547
2548 for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2549 if (!strncmp(cache_state[i].name, buf, strlen(cache_state[i].name))) {
2550 new_state = cache_state[i].state;
2551 break;
2552 }
2553 }
2554
2555 if (new_state != CACHE_DISABLED && new_state != CACHE_ENABLED)
2556 return -EINVAL;
2557
2558 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2559 if (ioa_cfg->cache_state == new_state) {
2560 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2561 return count;
2562 }
2563
2564 ioa_cfg->cache_state = new_state;
2565 dev_info(&ioa_cfg->pdev->dev, "%s adapter write cache.\n",
2566 new_state == CACHE_ENABLED ? "Enabling" : "Disabling");
2567 if (!ioa_cfg->in_reset_reload)
2568 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2569 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2570 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2571
2572 return count;
2573}
2574
Tony Jonesee959b02008-02-22 00:13:36 +01002575static struct device_attribute ipr_ioa_cache_attr = {
brking@us.ibm.com62275042005-11-01 17:01:14 -06002576 .attr = {
2577 .name = "write_cache",
2578 .mode = S_IRUGO | S_IWUSR,
2579 },
2580 .show = ipr_show_write_caching,
2581 .store = ipr_store_write_caching
2582};
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584/**
2585 * ipr_show_fw_version - Show the firmware version
Tony Jonesee959b02008-02-22 00:13:36 +01002586 * @dev: class device struct
2587 * @buf: buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 *
2589 * Return value:
2590 * number of bytes printed to buffer
2591 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002592static ssize_t ipr_show_fw_version(struct device *dev,
2593 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Tony Jonesee959b02008-02-22 00:13:36 +01002595 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2597 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2598 unsigned long lock_flags = 0;
2599 int len;
2600
2601 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2602 len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
2603 ucode_vpd->major_release, ucode_vpd->card_type,
2604 ucode_vpd->minor_release[0],
2605 ucode_vpd->minor_release[1]);
2606 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2607 return len;
2608}
2609
Tony Jonesee959b02008-02-22 00:13:36 +01002610static struct device_attribute ipr_fw_version_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 .attr = {
2612 .name = "fw_version",
2613 .mode = S_IRUGO,
2614 },
2615 .show = ipr_show_fw_version,
2616};
2617
2618/**
2619 * ipr_show_log_level - Show the adapter's error logging level
Tony Jonesee959b02008-02-22 00:13:36 +01002620 * @dev: class device struct
2621 * @buf: buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 *
2623 * Return value:
2624 * number of bytes printed to buffer
2625 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002626static ssize_t ipr_show_log_level(struct device *dev,
2627 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
Tony Jonesee959b02008-02-22 00:13:36 +01002629 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2631 unsigned long lock_flags = 0;
2632 int len;
2633
2634 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2635 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
2636 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2637 return len;
2638}
2639
2640/**
2641 * ipr_store_log_level - Change the adapter's error logging level
Tony Jonesee959b02008-02-22 00:13:36 +01002642 * @dev: class device struct
2643 * @buf: buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 *
2645 * Return value:
2646 * number of bytes printed to buffer
2647 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002648static ssize_t ipr_store_log_level(struct device *dev,
2649 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 const char *buf, size_t count)
2651{
Tony Jonesee959b02008-02-22 00:13:36 +01002652 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2654 unsigned long lock_flags = 0;
2655
2656 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2657 ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
2658 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2659 return strlen(buf);
2660}
2661
Tony Jonesee959b02008-02-22 00:13:36 +01002662static struct device_attribute ipr_log_level_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 .attr = {
2664 .name = "log_level",
2665 .mode = S_IRUGO | S_IWUSR,
2666 },
2667 .show = ipr_show_log_level,
2668 .store = ipr_store_log_level
2669};
2670
2671/**
2672 * ipr_store_diagnostics - IOA Diagnostics interface
Tony Jonesee959b02008-02-22 00:13:36 +01002673 * @dev: device struct
2674 * @buf: buffer
2675 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 *
2677 * This function will reset the adapter and wait a reasonable
2678 * amount of time for any errors that the adapter might log.
2679 *
2680 * Return value:
2681 * count on success / other on failure
2682 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002683static ssize_t ipr_store_diagnostics(struct device *dev,
2684 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 const char *buf, size_t count)
2686{
Tony Jonesee959b02008-02-22 00:13:36 +01002687 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2689 unsigned long lock_flags = 0;
2690 int rc = count;
2691
2692 if (!capable(CAP_SYS_ADMIN))
2693 return -EACCES;
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05002696 while(ioa_cfg->in_reset_reload) {
2697 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2698 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2699 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2700 }
2701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 ioa_cfg->errors_logged = 0;
2703 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2704
2705 if (ioa_cfg->in_reset_reload) {
2706 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2707 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2708
2709 /* Wait for a second for any errors to be logged */
2710 msleep(1000);
2711 } else {
2712 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2713 return -EIO;
2714 }
2715
2716 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2717 if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2718 rc = -EIO;
2719 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2720
2721 return rc;
2722}
2723
Tony Jonesee959b02008-02-22 00:13:36 +01002724static struct device_attribute ipr_diagnostics_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 .attr = {
2726 .name = "run_diagnostics",
2727 .mode = S_IWUSR,
2728 },
2729 .store = ipr_store_diagnostics
2730};
2731
2732/**
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002733 * ipr_show_adapter_state - Show the adapter's state
Tony Jonesee959b02008-02-22 00:13:36 +01002734 * @class_dev: device struct
2735 * @buf: buffer
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002736 *
2737 * Return value:
2738 * number of bytes printed to buffer
2739 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002740static ssize_t ipr_show_adapter_state(struct device *dev,
2741 struct device_attribute *attr, char *buf)
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002742{
Tony Jonesee959b02008-02-22 00:13:36 +01002743 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002744 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2745 unsigned long lock_flags = 0;
2746 int len;
2747
2748 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2749 if (ioa_cfg->ioa_is_dead)
2750 len = snprintf(buf, PAGE_SIZE, "offline\n");
2751 else
2752 len = snprintf(buf, PAGE_SIZE, "online\n");
2753 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2754 return len;
2755}
2756
2757/**
2758 * ipr_store_adapter_state - Change adapter state
Tony Jonesee959b02008-02-22 00:13:36 +01002759 * @dev: device struct
2760 * @buf: buffer
2761 * @count: buffer size
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002762 *
2763 * This function will change the adapter's state.
2764 *
2765 * Return value:
2766 * count on success / other on failure
2767 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002768static ssize_t ipr_store_adapter_state(struct device *dev,
2769 struct device_attribute *attr,
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002770 const char *buf, size_t count)
2771{
Tony Jonesee959b02008-02-22 00:13:36 +01002772 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002773 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2774 unsigned long lock_flags;
2775 int result = count;
2776
2777 if (!capable(CAP_SYS_ADMIN))
2778 return -EACCES;
2779
2780 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2781 if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
2782 ioa_cfg->ioa_is_dead = 0;
2783 ioa_cfg->reset_retries = 0;
2784 ioa_cfg->in_ioa_bringdown = 0;
2785 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2786 }
2787 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2788 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2789
2790 return result;
2791}
2792
Tony Jonesee959b02008-02-22 00:13:36 +01002793static struct device_attribute ipr_ioa_state_attr = {
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002794 .attr = {
Brian King49dd0962008-04-28 17:36:20 -05002795 .name = "online_state",
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002796 .mode = S_IRUGO | S_IWUSR,
2797 },
2798 .show = ipr_show_adapter_state,
2799 .store = ipr_store_adapter_state
2800};
2801
2802/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 * ipr_store_reset_adapter - Reset the adapter
Tony Jonesee959b02008-02-22 00:13:36 +01002804 * @dev: device struct
2805 * @buf: buffer
2806 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 *
2808 * This function will reset the adapter.
2809 *
2810 * Return value:
2811 * count on success / other on failure
2812 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002813static ssize_t ipr_store_reset_adapter(struct device *dev,
2814 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 const char *buf, size_t count)
2816{
Tony Jonesee959b02008-02-22 00:13:36 +01002817 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2819 unsigned long lock_flags;
2820 int result = count;
2821
2822 if (!capable(CAP_SYS_ADMIN))
2823 return -EACCES;
2824
2825 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2826 if (!ioa_cfg->in_reset_reload)
2827 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2828 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2829 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2830
2831 return result;
2832}
2833
Tony Jonesee959b02008-02-22 00:13:36 +01002834static struct device_attribute ipr_ioa_reset_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 .attr = {
2836 .name = "reset_host",
2837 .mode = S_IWUSR,
2838 },
2839 .store = ipr_store_reset_adapter
2840};
2841
2842/**
2843 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2844 * @buf_len: buffer length
2845 *
2846 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2847 * list to use for microcode download
2848 *
2849 * Return value:
2850 * pointer to sglist / NULL on failure
2851 **/
2852static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2853{
2854 int sg_size, order, bsize_elem, num_elem, i, j;
2855 struct ipr_sglist *sglist;
2856 struct scatterlist *scatterlist;
2857 struct page *page;
2858
2859 /* Get the minimum size per scatter/gather element */
2860 sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2861
2862 /* Get the actual size per element */
2863 order = get_order(sg_size);
2864
2865 /* Determine the actual number of bytes per element */
2866 bsize_elem = PAGE_SIZE * (1 << order);
2867
2868 /* Determine the actual number of sg entries needed */
2869 if (buf_len % bsize_elem)
2870 num_elem = (buf_len / bsize_elem) + 1;
2871 else
2872 num_elem = buf_len / bsize_elem;
2873
2874 /* Allocate a scatter/gather list for the DMA */
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06002875 sglist = kzalloc(sizeof(struct ipr_sglist) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 (sizeof(struct scatterlist) * (num_elem - 1)),
2877 GFP_KERNEL);
2878
2879 if (sglist == NULL) {
2880 ipr_trace;
2881 return NULL;
2882 }
2883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 scatterlist = sglist->scatterlist;
Jens Axboe45711f12007-10-22 21:19:53 +02002885 sg_init_table(scatterlist, num_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
2887 sglist->order = order;
2888 sglist->num_sg = num_elem;
2889
2890 /* Allocate a bunch of sg elements */
2891 for (i = 0; i < num_elem; i++) {
2892 page = alloc_pages(GFP_KERNEL, order);
2893 if (!page) {
2894 ipr_trace;
2895
2896 /* Free up what we already allocated */
2897 for (j = i - 1; j >= 0; j--)
Jens Axboe45711f12007-10-22 21:19:53 +02002898 __free_pages(sg_page(&scatterlist[j]), order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 kfree(sglist);
2900 return NULL;
2901 }
2902
Jens Axboe642f1492007-10-24 11:20:47 +02002903 sg_set_page(&scatterlist[i], page, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905
2906 return sglist;
2907}
2908
2909/**
2910 * ipr_free_ucode_buffer - Frees a microcode download buffer
2911 * @p_dnld: scatter/gather list pointer
2912 *
2913 * Free a DMA'able ucode download buffer previously allocated with
2914 * ipr_alloc_ucode_buffer
2915 *
2916 * Return value:
2917 * nothing
2918 **/
2919static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2920{
2921 int i;
2922
2923 for (i = 0; i < sglist->num_sg; i++)
Jens Axboe45711f12007-10-22 21:19:53 +02002924 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 kfree(sglist);
2927}
2928
2929/**
2930 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2931 * @sglist: scatter/gather list pointer
2932 * @buffer: buffer pointer
2933 * @len: buffer length
2934 *
2935 * Copy a microcode image from a user buffer into a buffer allocated by
2936 * ipr_alloc_ucode_buffer
2937 *
2938 * Return value:
2939 * 0 on success / other on failure
2940 **/
2941static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2942 u8 *buffer, u32 len)
2943{
2944 int bsize_elem, i, result = 0;
2945 struct scatterlist *scatterlist;
2946 void *kaddr;
2947
2948 /* Determine the actual number of bytes per element */
2949 bsize_elem = PAGE_SIZE * (1 << sglist->order);
2950
2951 scatterlist = sglist->scatterlist;
2952
2953 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
Jens Axboe45711f12007-10-22 21:19:53 +02002954 struct page *page = sg_page(&scatterlist[i]);
2955
2956 kaddr = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 memcpy(kaddr, buffer, bsize_elem);
Jens Axboe45711f12007-10-22 21:19:53 +02002958 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 scatterlist[i].length = bsize_elem;
2961
2962 if (result != 0) {
2963 ipr_trace;
2964 return result;
2965 }
2966 }
2967
2968 if (len % bsize_elem) {
Jens Axboe45711f12007-10-22 21:19:53 +02002969 struct page *page = sg_page(&scatterlist[i]);
2970
2971 kaddr = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 memcpy(kaddr, buffer, len % bsize_elem);
Jens Axboe45711f12007-10-22 21:19:53 +02002973 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 scatterlist[i].length = len % bsize_elem;
2976 }
2977
2978 sglist->buffer_len = len;
2979 return result;
2980}
2981
2982/**
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002983 * ipr_build_ucode_ioadl - Build a microcode download IOADL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 * @ipr_cmd: ipr command struct
2985 * @sglist: scatter/gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 *
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002987 * Builds a microcode download IOA data list (IOADL).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 **/
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002990static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
2991 struct ipr_sglist *sglist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2994 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2995 struct scatterlist *scatterlist = sglist->scatterlist;
2996 int i;
2997
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002998 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003000 ioarcb->write_data_transfer_length = cpu_to_be32(sglist->buffer_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 ioarcb->write_ioadl_len =
3002 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3003
3004 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3005 ioadl[i].flags_and_data_len =
3006 cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3007 ioadl[i].address =
3008 cpu_to_be32(sg_dma_address(&scatterlist[i]));
3009 }
3010
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003011 ioadl[i-1].flags_and_data_len |=
3012 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3013}
3014
3015/**
3016 * ipr_update_ioa_ucode - Update IOA's microcode
3017 * @ioa_cfg: ioa config struct
3018 * @sglist: scatter/gather list
3019 *
3020 * Initiate an adapter reset to update the IOA's microcode
3021 *
3022 * Return value:
3023 * 0 on success / -EIO on failure
3024 **/
3025static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3026 struct ipr_sglist *sglist)
3027{
3028 unsigned long lock_flags;
3029
3030 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05003031 while(ioa_cfg->in_reset_reload) {
3032 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3033 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3034 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3035 }
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003036
3037 if (ioa_cfg->ucode_sglist) {
3038 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3039 dev_err(&ioa_cfg->pdev->dev,
3040 "Microcode download already in progress\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return -EIO;
3042 }
3043
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003044 sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
3045 sglist->num_sg, DMA_TO_DEVICE);
3046
3047 if (!sglist->num_dma_sg) {
3048 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3049 dev_err(&ioa_cfg->pdev->dev,
3050 "Failed to map microcode download buffer!\n");
3051 return -EIO;
3052 }
3053
3054 ioa_cfg->ucode_sglist = sglist;
3055 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3056 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3057 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3058
3059 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3060 ioa_cfg->ucode_sglist = NULL;
3061 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 return 0;
3063}
3064
3065/**
3066 * ipr_store_update_fw - Update the firmware on the adapter
Tony Jonesee959b02008-02-22 00:13:36 +01003067 * @class_dev: device struct
3068 * @buf: buffer
3069 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 *
3071 * This function will update the firmware on the adapter.
3072 *
3073 * Return value:
3074 * count on success / other on failure
3075 **/
Tony Jonesee959b02008-02-22 00:13:36 +01003076static ssize_t ipr_store_update_fw(struct device *dev,
3077 struct device_attribute *attr,
3078 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
Tony Jonesee959b02008-02-22 00:13:36 +01003080 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3082 struct ipr_ucode_image_header *image_hdr;
3083 const struct firmware *fw_entry;
3084 struct ipr_sglist *sglist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 char fname[100];
3086 char *src;
3087 int len, result, dnld_size;
3088
3089 if (!capable(CAP_SYS_ADMIN))
3090 return -EACCES;
3091
3092 len = snprintf(fname, 99, "%s", buf);
3093 fname[len-1] = '\0';
3094
3095 if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3096 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3097 return -EIO;
3098 }
3099
3100 image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3101
3102 if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
3103 (ioa_cfg->vpd_cbs->page3_data.card_type &&
3104 ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
3105 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
3106 release_firmware(fw_entry);
3107 return -EINVAL;
3108 }
3109
3110 src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3111 dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3112 sglist = ipr_alloc_ucode_buffer(dnld_size);
3113
3114 if (!sglist) {
3115 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3116 release_firmware(fw_entry);
3117 return -ENOMEM;
3118 }
3119
3120 result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3121
3122 if (result) {
3123 dev_err(&ioa_cfg->pdev->dev,
3124 "Microcode buffer copy to DMA buffer failed\n");
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 }
3127
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003128 result = ipr_update_ioa_ucode(ioa_cfg, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003130 if (!result)
3131 result = count;
3132out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 ipr_free_ucode_buffer(sglist);
3134 release_firmware(fw_entry);
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003135 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Tony Jonesee959b02008-02-22 00:13:36 +01003138static struct device_attribute ipr_update_fw_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 .attr = {
3140 .name = "update_fw",
3141 .mode = S_IWUSR,
3142 },
3143 .store = ipr_store_update_fw
3144};
3145
Tony Jonesee959b02008-02-22 00:13:36 +01003146static struct device_attribute *ipr_ioa_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 &ipr_fw_version_attr,
3148 &ipr_log_level_attr,
3149 &ipr_diagnostics_attr,
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003150 &ipr_ioa_state_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 &ipr_ioa_reset_attr,
3152 &ipr_update_fw_attr,
brking@us.ibm.com62275042005-11-01 17:01:14 -06003153 &ipr_ioa_cache_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 NULL,
3155};
3156
3157#ifdef CONFIG_SCSI_IPR_DUMP
3158/**
3159 * ipr_read_dump - Dump the adapter
3160 * @kobj: kobject struct
Zhang Rui91a69022007-06-09 13:57:22 +08003161 * @bin_attr: bin_attribute struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 * @buf: buffer
3163 * @off: offset
3164 * @count: buffer size
3165 *
3166 * Return value:
3167 * number of bytes printed to buffer
3168 **/
Zhang Rui91a69022007-06-09 13:57:22 +08003169static ssize_t ipr_read_dump(struct kobject *kobj,
3170 struct bin_attribute *bin_attr,
3171 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Tony Jonesee959b02008-02-22 00:13:36 +01003173 struct device *cdev = container_of(kobj, struct device, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 struct Scsi_Host *shost = class_to_shost(cdev);
3175 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3176 struct ipr_dump *dump;
3177 unsigned long lock_flags = 0;
3178 char *src;
3179 int len;
3180 size_t rc = count;
3181
3182 if (!capable(CAP_SYS_ADMIN))
3183 return -EACCES;
3184
3185 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3186 dump = ioa_cfg->dump;
3187
3188 if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
3189 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3190 return 0;
3191 }
3192 kref_get(&dump->kref);
3193 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3194
3195 if (off > dump->driver_dump.hdr.len) {
3196 kref_put(&dump->kref, ipr_release_dump);
3197 return 0;
3198 }
3199
3200 if (off + count > dump->driver_dump.hdr.len) {
3201 count = dump->driver_dump.hdr.len - off;
3202 rc = count;
3203 }
3204
3205 if (count && off < sizeof(dump->driver_dump)) {
3206 if (off + count > sizeof(dump->driver_dump))
3207 len = sizeof(dump->driver_dump) - off;
3208 else
3209 len = count;
3210 src = (u8 *)&dump->driver_dump + off;
3211 memcpy(buf, src, len);
3212 buf += len;
3213 off += len;
3214 count -= len;
3215 }
3216
3217 off -= sizeof(dump->driver_dump);
3218
3219 if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
3220 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
3221 len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
3222 else
3223 len = count;
3224 src = (u8 *)&dump->ioa_dump + off;
3225 memcpy(buf, src, len);
3226 buf += len;
3227 off += len;
3228 count -= len;
3229 }
3230
3231 off -= offsetof(struct ipr_ioa_dump, ioa_data);
3232
3233 while (count) {
3234 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
3235 len = PAGE_ALIGN(off) - off;
3236 else
3237 len = count;
3238 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
3239 src += off & ~PAGE_MASK;
3240 memcpy(buf, src, len);
3241 buf += len;
3242 off += len;
3243 count -= len;
3244 }
3245
3246 kref_put(&dump->kref, ipr_release_dump);
3247 return rc;
3248}
3249
3250/**
3251 * ipr_alloc_dump - Prepare for adapter dump
3252 * @ioa_cfg: ioa config struct
3253 *
3254 * Return value:
3255 * 0 on success / other on failure
3256 **/
3257static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
3258{
3259 struct ipr_dump *dump;
3260 unsigned long lock_flags = 0;
3261
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06003262 dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 if (!dump) {
3265 ipr_err("Dump memory allocation failed\n");
3266 return -ENOMEM;
3267 }
3268
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 kref_init(&dump->kref);
3270 dump->ioa_cfg = ioa_cfg;
3271
3272 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3273
3274 if (INACTIVE != ioa_cfg->sdt_state) {
3275 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3276 kfree(dump);
3277 return 0;
3278 }
3279
3280 ioa_cfg->dump = dump;
3281 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
3282 if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
3283 ioa_cfg->dump_taken = 1;
3284 schedule_work(&ioa_cfg->work_q);
3285 }
3286 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return 0;
3289}
3290
3291/**
3292 * ipr_free_dump - Free adapter dump memory
3293 * @ioa_cfg: ioa config struct
3294 *
3295 * Return value:
3296 * 0 on success / other on failure
3297 **/
3298static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
3299{
3300 struct ipr_dump *dump;
3301 unsigned long lock_flags = 0;
3302
3303 ENTER;
3304
3305 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3306 dump = ioa_cfg->dump;
3307 if (!dump) {
3308 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3309 return 0;
3310 }
3311
3312 ioa_cfg->dump = NULL;
3313 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3314
3315 kref_put(&dump->kref, ipr_release_dump);
3316
3317 LEAVE;
3318 return 0;
3319}
3320
3321/**
3322 * ipr_write_dump - Setup dump state of adapter
3323 * @kobj: kobject struct
Zhang Rui91a69022007-06-09 13:57:22 +08003324 * @bin_attr: bin_attribute struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 * @buf: buffer
3326 * @off: offset
3327 * @count: buffer size
3328 *
3329 * Return value:
3330 * number of bytes printed to buffer
3331 **/
Zhang Rui91a69022007-06-09 13:57:22 +08003332static ssize_t ipr_write_dump(struct kobject *kobj,
3333 struct bin_attribute *bin_attr,
3334 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
Tony Jonesee959b02008-02-22 00:13:36 +01003336 struct device *cdev = container_of(kobj, struct device, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 struct Scsi_Host *shost = class_to_shost(cdev);
3338 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3339 int rc;
3340
3341 if (!capable(CAP_SYS_ADMIN))
3342 return -EACCES;
3343
3344 if (buf[0] == '1')
3345 rc = ipr_alloc_dump(ioa_cfg);
3346 else if (buf[0] == '0')
3347 rc = ipr_free_dump(ioa_cfg);
3348 else
3349 return -EINVAL;
3350
3351 if (rc)
3352 return rc;
3353 else
3354 return count;
3355}
3356
3357static struct bin_attribute ipr_dump_attr = {
3358 .attr = {
3359 .name = "dump",
3360 .mode = S_IRUSR | S_IWUSR,
3361 },
3362 .size = 0,
3363 .read = ipr_read_dump,
3364 .write = ipr_write_dump
3365};
3366#else
3367static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
3368#endif
3369
3370/**
3371 * ipr_change_queue_depth - Change the device's queue depth
3372 * @sdev: scsi device struct
3373 * @qdepth: depth to set
3374 *
3375 * Return value:
3376 * actual depth set
3377 **/
3378static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
3379{
Brian King35a39692006-09-25 12:39:20 -05003380 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3381 struct ipr_resource_entry *res;
3382 unsigned long lock_flags = 0;
3383
3384 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3385 res = (struct ipr_resource_entry *)sdev->hostdata;
3386
3387 if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
3388 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
3389 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3392 return sdev->queue_depth;
3393}
3394
3395/**
3396 * ipr_change_queue_type - Change the device's queue type
3397 * @dsev: scsi device struct
3398 * @tag_type: type of tags to use
3399 *
3400 * Return value:
3401 * actual queue type set
3402 **/
3403static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
3404{
3405 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3406 struct ipr_resource_entry *res;
3407 unsigned long lock_flags = 0;
3408
3409 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3410 res = (struct ipr_resource_entry *)sdev->hostdata;
3411
3412 if (res) {
3413 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
3414 /*
3415 * We don't bother quiescing the device here since the
3416 * adapter firmware does it for us.
3417 */
3418 scsi_set_tag_type(sdev, tag_type);
3419
3420 if (tag_type)
3421 scsi_activate_tcq(sdev, sdev->queue_depth);
3422 else
3423 scsi_deactivate_tcq(sdev, sdev->queue_depth);
3424 } else
3425 tag_type = 0;
3426 } else
3427 tag_type = 0;
3428
3429 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3430 return tag_type;
3431}
3432
3433/**
3434 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
3435 * @dev: device struct
3436 * @buf: buffer
3437 *
3438 * Return value:
3439 * number of bytes printed to buffer
3440 **/
Yani Ioannou10523b32005-05-17 06:43:37 -04003441static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442{
3443 struct scsi_device *sdev = to_scsi_device(dev);
3444 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3445 struct ipr_resource_entry *res;
3446 unsigned long lock_flags = 0;
3447 ssize_t len = -ENXIO;
3448
3449 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3450 res = (struct ipr_resource_entry *)sdev->hostdata;
3451 if (res)
3452 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->cfgte.res_handle);
3453 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3454 return len;
3455}
3456
3457static struct device_attribute ipr_adapter_handle_attr = {
3458 .attr = {
3459 .name = "adapter_handle",
3460 .mode = S_IRUSR,
3461 },
3462 .show = ipr_show_adapter_handle
3463};
3464
3465static struct device_attribute *ipr_dev_attrs[] = {
3466 &ipr_adapter_handle_attr,
3467 NULL,
3468};
3469
3470/**
3471 * ipr_biosparam - Return the HSC mapping
3472 * @sdev: scsi device struct
3473 * @block_device: block device pointer
3474 * @capacity: capacity of the device
3475 * @parm: Array containing returned HSC values.
3476 *
3477 * This function generates the HSC parms that fdisk uses.
3478 * We want to make sure we return something that places partitions
3479 * on 4k boundaries for best performance with the IOA.
3480 *
3481 * Return value:
3482 * 0 on success
3483 **/
3484static int ipr_biosparam(struct scsi_device *sdev,
3485 struct block_device *block_device,
3486 sector_t capacity, int *parm)
3487{
3488 int heads, sectors;
3489 sector_t cylinders;
3490
3491 heads = 128;
3492 sectors = 32;
3493
3494 cylinders = capacity;
3495 sector_div(cylinders, (128 * 32));
3496
3497 /* return result */
3498 parm[0] = heads;
3499 parm[1] = sectors;
3500 parm[2] = cylinders;
3501
3502 return 0;
3503}
3504
3505/**
Brian King35a39692006-09-25 12:39:20 -05003506 * ipr_find_starget - Find target based on bus/target.
3507 * @starget: scsi target struct
3508 *
3509 * Return value:
3510 * resource entry pointer if found / NULL if not found
3511 **/
3512static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
3513{
3514 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3515 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3516 struct ipr_resource_entry *res;
3517
3518 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3519 if ((res->cfgte.res_addr.bus == starget->channel) &&
3520 (res->cfgte.res_addr.target == starget->id) &&
3521 (res->cfgte.res_addr.lun == 0)) {
3522 return res;
3523 }
3524 }
3525
3526 return NULL;
3527}
3528
3529static struct ata_port_info sata_port_info;
3530
3531/**
3532 * ipr_target_alloc - Prepare for commands to a SCSI target
3533 * @starget: scsi target struct
3534 *
3535 * If the device is a SATA device, this function allocates an
3536 * ATA port with libata, else it does nothing.
3537 *
3538 * Return value:
3539 * 0 on success / non-0 on failure
3540 **/
3541static int ipr_target_alloc(struct scsi_target *starget)
3542{
3543 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3544 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3545 struct ipr_sata_port *sata_port;
3546 struct ata_port *ap;
3547 struct ipr_resource_entry *res;
3548 unsigned long lock_flags;
3549
3550 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3551 res = ipr_find_starget(starget);
3552 starget->hostdata = NULL;
3553
3554 if (res && ipr_is_gata(res)) {
3555 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3556 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
3557 if (!sata_port)
3558 return -ENOMEM;
3559
3560 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
3561 if (ap) {
3562 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3563 sata_port->ioa_cfg = ioa_cfg;
3564 sata_port->ap = ap;
3565 sata_port->res = res;
3566
3567 res->sata_port = sata_port;
3568 ap->private_data = sata_port;
3569 starget->hostdata = sata_port;
3570 } else {
3571 kfree(sata_port);
3572 return -ENOMEM;
3573 }
3574 }
3575 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3576
3577 return 0;
3578}
3579
3580/**
3581 * ipr_target_destroy - Destroy a SCSI target
3582 * @starget: scsi target struct
3583 *
3584 * If the device was a SATA device, this function frees the libata
3585 * ATA port, else it does nothing.
3586 *
3587 **/
3588static void ipr_target_destroy(struct scsi_target *starget)
3589{
3590 struct ipr_sata_port *sata_port = starget->hostdata;
3591
3592 if (sata_port) {
3593 starget->hostdata = NULL;
3594 ata_sas_port_destroy(sata_port->ap);
3595 kfree(sata_port);
3596 }
3597}
3598
3599/**
3600 * ipr_find_sdev - Find device based on bus/target/lun.
3601 * @sdev: scsi device struct
3602 *
3603 * Return value:
3604 * resource entry pointer if found / NULL if not found
3605 **/
3606static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
3607{
3608 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3609 struct ipr_resource_entry *res;
3610
3611 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3612 if ((res->cfgte.res_addr.bus == sdev->channel) &&
3613 (res->cfgte.res_addr.target == sdev->id) &&
3614 (res->cfgte.res_addr.lun == sdev->lun))
3615 return res;
3616 }
3617
3618 return NULL;
3619}
3620
3621/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 * ipr_slave_destroy - Unconfigure a SCSI device
3623 * @sdev: scsi device struct
3624 *
3625 * Return value:
3626 * nothing
3627 **/
3628static void ipr_slave_destroy(struct scsi_device *sdev)
3629{
3630 struct ipr_resource_entry *res;
3631 struct ipr_ioa_cfg *ioa_cfg;
3632 unsigned long lock_flags = 0;
3633
3634 ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3635
3636 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3637 res = (struct ipr_resource_entry *) sdev->hostdata;
3638 if (res) {
Brian King35a39692006-09-25 12:39:20 -05003639 if (res->sata_port)
3640 ata_port_disable(res->sata_port->ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 sdev->hostdata = NULL;
3642 res->sdev = NULL;
Brian King35a39692006-09-25 12:39:20 -05003643 res->sata_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
3645 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3646}
3647
3648/**
3649 * ipr_slave_configure - Configure a SCSI device
3650 * @sdev: scsi device struct
3651 *
3652 * This function configures the specified scsi device.
3653 *
3654 * Return value:
3655 * 0 on success
3656 **/
3657static int ipr_slave_configure(struct scsi_device *sdev)
3658{
3659 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3660 struct ipr_resource_entry *res;
3661 unsigned long lock_flags = 0;
3662
3663 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3664 res = sdev->hostdata;
3665 if (res) {
3666 if (ipr_is_af_dasd_device(res))
3667 sdev->type = TYPE_RAID;
brking@us.ibm.com0726ce22005-11-01 17:01:01 -06003668 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 sdev->scsi_level = 4;
brking@us.ibm.com0726ce22005-11-01 17:01:01 -06003670 sdev->no_uld_attach = 1;
3671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 if (ipr_is_vset_device(res)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07003673 blk_queue_rq_timeout(sdev->request_queue,
3674 IPR_VSET_RW_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 blk_queue_max_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
3676 }
Brian Kinge4fbf442006-03-29 09:37:22 -06003677 if (ipr_is_vset_device(res) || ipr_is_scsi_disk(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 sdev->allow_restart = 1;
Brian King35a39692006-09-25 12:39:20 -05003679 if (ipr_is_gata(res) && res->sata_port) {
3680 scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
3681 ata_sas_slave_configure(sdev, res->sata_port->ap);
3682 } else {
3683 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
3684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 }
3686 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3687 return 0;
3688}
3689
3690/**
Brian King35a39692006-09-25 12:39:20 -05003691 * ipr_ata_slave_alloc - Prepare for commands to a SATA device
3692 * @sdev: scsi device struct
3693 *
3694 * This function initializes an ATA port so that future commands
3695 * sent through queuecommand will work.
3696 *
3697 * Return value:
3698 * 0 on success
3699 **/
3700static int ipr_ata_slave_alloc(struct scsi_device *sdev)
3701{
3702 struct ipr_sata_port *sata_port = NULL;
3703 int rc = -ENXIO;
3704
3705 ENTER;
3706 if (sdev->sdev_target)
3707 sata_port = sdev->sdev_target->hostdata;
3708 if (sata_port)
3709 rc = ata_sas_port_init(sata_port->ap);
3710 if (rc)
3711 ipr_slave_destroy(sdev);
3712
3713 LEAVE;
3714 return rc;
3715}
3716
3717/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 * ipr_slave_alloc - Prepare for commands to a device.
3719 * @sdev: scsi device struct
3720 *
3721 * This function saves a pointer to the resource entry
3722 * in the scsi device struct if the device exists. We
3723 * can then use this pointer in ipr_queuecommand when
3724 * handling new commands.
3725 *
3726 * Return value:
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06003727 * 0 on success / -ENXIO if device does not exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 **/
3729static int ipr_slave_alloc(struct scsi_device *sdev)
3730{
3731 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3732 struct ipr_resource_entry *res;
3733 unsigned long lock_flags;
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06003734 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
3736 sdev->hostdata = NULL;
3737
3738 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3739
Brian King35a39692006-09-25 12:39:20 -05003740 res = ipr_find_sdev(sdev);
3741 if (res) {
3742 res->sdev = sdev;
3743 res->add_to_ml = 0;
3744 res->in_erp = 0;
3745 sdev->hostdata = res;
3746 if (!ipr_is_naca_model(res))
3747 res->needs_sync_complete = 1;
3748 rc = 0;
3749 if (ipr_is_gata(res)) {
3750 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3751 return ipr_ata_slave_alloc(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 }
3753 }
3754
3755 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3756
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06003757 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758}
3759
3760/**
3761 * ipr_eh_host_reset - Reset the host adapter
3762 * @scsi_cmd: scsi command struct
3763 *
3764 * Return value:
3765 * SUCCESS / FAILED
3766 **/
Jeff Garzik df0ae242005-05-28 07:57:14 -04003767static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
3769 struct ipr_ioa_cfg *ioa_cfg;
3770 int rc;
3771
3772 ENTER;
3773 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3774
3775 dev_err(&ioa_cfg->pdev->dev,
3776 "Adapter being reset as a result of error recovery.\n");
3777
3778 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3779 ioa_cfg->sdt_state = GET_DUMP;
3780
3781 rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
3782
3783 LEAVE;
3784 return rc;
3785}
3786
Jeff Garzik df0ae242005-05-28 07:57:14 -04003787static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
3788{
3789 int rc;
3790
3791 spin_lock_irq(cmd->device->host->host_lock);
3792 rc = __ipr_eh_host_reset(cmd);
3793 spin_unlock_irq(cmd->device->host->host_lock);
3794
3795 return rc;
3796}
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798/**
Brian Kingc6513092006-03-29 09:37:43 -06003799 * ipr_device_reset - Reset the device
3800 * @ioa_cfg: ioa config struct
3801 * @res: resource entry struct
3802 *
3803 * This function issues a device reset to the affected device.
3804 * If the device is a SCSI device, a LUN reset will be sent
3805 * to the device first. If that does not work, a target reset
Brian King35a39692006-09-25 12:39:20 -05003806 * will be sent. If the device is a SATA device, a PHY reset will
3807 * be sent.
Brian Kingc6513092006-03-29 09:37:43 -06003808 *
3809 * Return value:
3810 * 0 on success / non-zero on failure
3811 **/
3812static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
3813 struct ipr_resource_entry *res)
3814{
3815 struct ipr_cmnd *ipr_cmd;
3816 struct ipr_ioarcb *ioarcb;
3817 struct ipr_cmd_pkt *cmd_pkt;
Brian King35a39692006-09-25 12:39:20 -05003818 struct ipr_ioarcb_ata_regs *regs;
Brian Kingc6513092006-03-29 09:37:43 -06003819 u32 ioasc;
3820
3821 ENTER;
3822 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3823 ioarcb = &ipr_cmd->ioarcb;
3824 cmd_pkt = &ioarcb->cmd_pkt;
Brian King35a39692006-09-25 12:39:20 -05003825 regs = &ioarcb->add_data.u.regs;
Brian Kingc6513092006-03-29 09:37:43 -06003826
3827 ioarcb->res_handle = res->cfgte.res_handle;
3828 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3829 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
Brian King35a39692006-09-25 12:39:20 -05003830 if (ipr_is_gata(res)) {
3831 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
3832 ioarcb->add_cmd_parms_len = cpu_to_be32(sizeof(regs->flags));
3833 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
3834 }
Brian Kingc6513092006-03-29 09:37:43 -06003835
3836 ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3837 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3838 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
Brian King35a39692006-09-25 12:39:20 -05003839 if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET)
3840 memcpy(&res->sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
3841 sizeof(struct ipr_ioasa_gata));
Brian Kingc6513092006-03-29 09:37:43 -06003842
3843 LEAVE;
3844 return (IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0);
3845}
3846
3847/**
Brian King35a39692006-09-25 12:39:20 -05003848 * ipr_sata_reset - Reset the SATA port
Tejun Heocc0680a2007-08-06 18:36:23 +09003849 * @link: SATA link to reset
Brian King35a39692006-09-25 12:39:20 -05003850 * @classes: class of the attached device
3851 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003852 * This function issues a SATA phy reset to the affected ATA link.
Brian King35a39692006-09-25 12:39:20 -05003853 *
3854 * Return value:
3855 * 0 on success / non-zero on failure
3856 **/
Tejun Heocc0680a2007-08-06 18:36:23 +09003857static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
Andrew Morton120bda32007-03-26 02:17:43 -07003858 unsigned long deadline)
Brian King35a39692006-09-25 12:39:20 -05003859{
Tejun Heocc0680a2007-08-06 18:36:23 +09003860 struct ipr_sata_port *sata_port = link->ap->private_data;
Brian King35a39692006-09-25 12:39:20 -05003861 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
3862 struct ipr_resource_entry *res;
3863 unsigned long lock_flags = 0;
3864 int rc = -ENXIO;
3865
3866 ENTER;
3867 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King73d98ff2006-11-21 10:27:58 -06003868 while(ioa_cfg->in_reset_reload) {
3869 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3870 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3871 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3872 }
3873
Brian King35a39692006-09-25 12:39:20 -05003874 res = sata_port->res;
3875 if (res) {
3876 rc = ipr_device_reset(ioa_cfg, res);
3877 switch(res->cfgte.proto) {
3878 case IPR_PROTO_SATA:
3879 case IPR_PROTO_SAS_STP:
3880 *classes = ATA_DEV_ATA;
3881 break;
3882 case IPR_PROTO_SATA_ATAPI:
3883 case IPR_PROTO_SAS_STP_ATAPI:
3884 *classes = ATA_DEV_ATAPI;
3885 break;
3886 default:
3887 *classes = ATA_DEV_UNKNOWN;
3888 break;
3889 };
3890 }
3891
3892 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3893 LEAVE;
3894 return rc;
3895}
3896
3897/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 * ipr_eh_dev_reset - Reset the device
3899 * @scsi_cmd: scsi command struct
3900 *
3901 * This function issues a device reset to the affected device.
3902 * A LUN reset will be sent to the device first. If that does
3903 * not work, a target reset will be sent.
3904 *
3905 * Return value:
3906 * SUCCESS / FAILED
3907 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003908static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909{
3910 struct ipr_cmnd *ipr_cmd;
3911 struct ipr_ioa_cfg *ioa_cfg;
3912 struct ipr_resource_entry *res;
Brian King35a39692006-09-25 12:39:20 -05003913 struct ata_port *ap;
3914 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
3916 ENTER;
3917 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3918 res = scsi_cmd->device->hostdata;
3919
brking@us.ibm.comeeb883072005-11-01 17:02:29 -06003920 if (!res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 return FAILED;
3922
3923 /*
3924 * If we are currently going through reset/reload, return failed. This will force the
3925 * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
3926 * reset to complete
3927 */
3928 if (ioa_cfg->in_reset_reload)
3929 return FAILED;
3930 if (ioa_cfg->ioa_is_dead)
3931 return FAILED;
3932
3933 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3934 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
3935 if (ipr_cmd->scsi_cmd)
3936 ipr_cmd->done = ipr_scsi_eh_done;
Brian King24d6f2b2007-03-29 12:43:17 -05003937 if (ipr_cmd->qc)
3938 ipr_cmd->done = ipr_sata_eh_done;
Brian King7402ece2006-11-21 10:28:23 -06003939 if (ipr_cmd->qc && !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
3940 ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
3941 ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
3942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 }
3944 }
3945
3946 res->resetting_device = 1;
Brian Kingfb3ed3c2006-03-29 09:37:37 -06003947 scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
Brian King35a39692006-09-25 12:39:20 -05003948
3949 if (ipr_is_gata(res) && res->sata_port) {
3950 ap = res->sata_port->ap;
3951 spin_unlock_irq(scsi_cmd->device->host->host_lock);
Tejun Heoa1efdab2008-03-25 12:22:50 +09003952 ata_std_error_handler(ap);
Brian King35a39692006-09-25 12:39:20 -05003953 spin_lock_irq(scsi_cmd->device->host->host_lock);
Brian King5af23d22007-05-09 15:36:35 -05003954
3955 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3956 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
3957 rc = -EIO;
3958 break;
3959 }
3960 }
Brian King35a39692006-09-25 12:39:20 -05003961 } else
3962 rc = ipr_device_reset(ioa_cfg, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 res->resetting_device = 0;
3964
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 LEAVE;
Brian Kingc6513092006-03-29 09:37:43 -06003966 return (rc ? FAILED : SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003969static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
3970{
3971 int rc;
3972
3973 spin_lock_irq(cmd->device->host->host_lock);
3974 rc = __ipr_eh_dev_reset(cmd);
3975 spin_unlock_irq(cmd->device->host->host_lock);
3976
3977 return rc;
3978}
3979
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980/**
3981 * ipr_bus_reset_done - Op done function for bus reset.
3982 * @ipr_cmd: ipr command struct
3983 *
3984 * This function is the op done function for a bus reset
3985 *
3986 * Return value:
3987 * none
3988 **/
3989static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
3990{
3991 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3992 struct ipr_resource_entry *res;
3993
3994 ENTER;
3995 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3996 if (!memcmp(&res->cfgte.res_handle, &ipr_cmd->ioarcb.res_handle,
3997 sizeof(res->cfgte.res_handle))) {
3998 scsi_report_bus_reset(ioa_cfg->host, res->cfgte.res_addr.bus);
3999 break;
4000 }
4001 }
4002
4003 /*
4004 * If abort has not completed, indicate the reset has, else call the
4005 * abort's done function to wake the sleeping eh thread
4006 */
4007 if (ipr_cmd->sibling->sibling)
4008 ipr_cmd->sibling->sibling = NULL;
4009 else
4010 ipr_cmd->sibling->done(ipr_cmd->sibling);
4011
4012 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4013 LEAVE;
4014}
4015
4016/**
4017 * ipr_abort_timeout - An abort task has timed out
4018 * @ipr_cmd: ipr command struct
4019 *
4020 * This function handles when an abort task times out. If this
4021 * happens we issue a bus reset since we have resources tied
4022 * up that must be freed before returning to the midlayer.
4023 *
4024 * Return value:
4025 * none
4026 **/
4027static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
4028{
4029 struct ipr_cmnd *reset_cmd;
4030 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4031 struct ipr_cmd_pkt *cmd_pkt;
4032 unsigned long lock_flags = 0;
4033
4034 ENTER;
4035 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4036 if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
4037 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4038 return;
4039 }
4040
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004041 sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4043 ipr_cmd->sibling = reset_cmd;
4044 reset_cmd->sibling = ipr_cmd;
4045 reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
4046 cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
4047 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4048 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4049 cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
4050
4051 ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4052 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4053 LEAVE;
4054}
4055
4056/**
4057 * ipr_cancel_op - Cancel specified op
4058 * @scsi_cmd: scsi command struct
4059 *
4060 * This function cancels specified op.
4061 *
4062 * Return value:
4063 * SUCCESS / FAILED
4064 **/
4065static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
4066{
4067 struct ipr_cmnd *ipr_cmd;
4068 struct ipr_ioa_cfg *ioa_cfg;
4069 struct ipr_resource_entry *res;
4070 struct ipr_cmd_pkt *cmd_pkt;
4071 u32 ioasc;
4072 int op_found = 0;
4073
4074 ENTER;
4075 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4076 res = scsi_cmd->device->hostdata;
4077
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004078 /* If we are currently going through reset/reload, return failed.
4079 * This will force the mid-layer to call ipr_eh_host_reset,
4080 * which will then go to sleep and wait for the reset to complete
4081 */
4082 if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
4083 return FAILED;
Brian King04d97682006-11-21 10:28:04 -06004084 if (!res || !ipr_is_gscsi(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return FAILED;
4086
4087 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4088 if (ipr_cmd->scsi_cmd == scsi_cmd) {
4089 ipr_cmd->done = ipr_scsi_eh_done;
4090 op_found = 1;
4091 break;
4092 }
4093 }
4094
4095 if (!op_found)
4096 return SUCCESS;
4097
4098 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4099 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
4100 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4101 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4102 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4103 ipr_cmd->u.sdev = scsi_cmd->device;
4104
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004105 scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
4106 scsi_cmd->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
4108 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4109
4110 /*
4111 * If the abort task timed out and we sent a bus reset, we will get
4112 * one the following responses to the abort
4113 */
4114 if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
4115 ioasc = 0;
4116 ipr_trace;
4117 }
4118
4119 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004120 if (!ipr_is_naca_model(res))
4121 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
4123 LEAVE;
4124 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
4125}
4126
4127/**
4128 * ipr_eh_abort - Abort a single op
4129 * @scsi_cmd: scsi command struct
4130 *
4131 * Return value:
4132 * SUCCESS / FAILED
4133 **/
4134static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
4135{
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004136 unsigned long flags;
4137 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
4139 ENTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004141 spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
4142 rc = ipr_cancel_op(scsi_cmd);
4143 spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
4145 LEAVE;
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004146 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
4148
4149/**
4150 * ipr_handle_other_interrupt - Handle "other" interrupts
4151 * @ioa_cfg: ioa config struct
4152 * @int_reg: interrupt register
4153 *
4154 * Return value:
4155 * IRQ_NONE / IRQ_HANDLED
4156 **/
4157static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
4158 volatile u32 int_reg)
4159{
4160 irqreturn_t rc = IRQ_HANDLED;
4161
4162 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4163 /* Mask the interrupt */
4164 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
4165
4166 /* Clear the interrupt */
4167 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
4168 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4169
4170 list_del(&ioa_cfg->reset_cmd->queue);
4171 del_timer(&ioa_cfg->reset_cmd->timer);
4172 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
4173 } else {
4174 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
4175 ioa_cfg->ioa_unit_checked = 1;
4176 else
4177 dev_err(&ioa_cfg->pdev->dev,
4178 "Permanent IOA failure. 0x%08X\n", int_reg);
4179
4180 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4181 ioa_cfg->sdt_state = GET_DUMP;
4182
4183 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
4184 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4185 }
4186
4187 return rc;
4188}
4189
4190/**
4191 * ipr_isr - Interrupt service routine
4192 * @irq: irq number
4193 * @devp: pointer to ioa config struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 *
4195 * Return value:
4196 * IRQ_NONE / IRQ_HANDLED
4197 **/
David Howells7d12e782006-10-05 14:55:46 +01004198static irqreturn_t ipr_isr(int irq, void *devp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199{
4200 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
4201 unsigned long lock_flags = 0;
4202 volatile u32 int_reg, int_mask_reg;
4203 u32 ioasc;
4204 u16 cmd_index;
4205 struct ipr_cmnd *ipr_cmd;
4206 irqreturn_t rc = IRQ_NONE;
4207
4208 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4209
4210 /* If interrupts are disabled, ignore the interrupt */
4211 if (!ioa_cfg->allow_interrupts) {
4212 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4213 return IRQ_NONE;
4214 }
4215
4216 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4217 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4218
4219 /* If an interrupt on the adapter did not occur, ignore it */
4220 if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
4221 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4222 return IRQ_NONE;
4223 }
4224
4225 while (1) {
4226 ipr_cmd = NULL;
4227
4228 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
4229 ioa_cfg->toggle_bit) {
4230
4231 cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
4232 IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
4233
4234 if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
4235 ioa_cfg->errors_logged++;
4236 dev_err(&ioa_cfg->pdev->dev, "Invalid response handle from IOA\n");
4237
4238 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4239 ioa_cfg->sdt_state = GET_DUMP;
4240
4241 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4242 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4243 return IRQ_HANDLED;
4244 }
4245
4246 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
4247
4248 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4249
4250 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
4251
4252 list_del(&ipr_cmd->queue);
4253 del_timer(&ipr_cmd->timer);
4254 ipr_cmd->done(ipr_cmd);
4255
4256 rc = IRQ_HANDLED;
4257
4258 if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
4259 ioa_cfg->hrrq_curr++;
4260 } else {
4261 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
4262 ioa_cfg->toggle_bit ^= 1u;
4263 }
4264 }
4265
4266 if (ipr_cmd != NULL) {
4267 /* Clear the PCI interrupt */
4268 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg);
4269 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4270 } else
4271 break;
4272 }
4273
4274 if (unlikely(rc == IRQ_NONE))
4275 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
4276
4277 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4278 return rc;
4279}
4280
4281/**
4282 * ipr_build_ioadl - Build a scatter/gather list and map the buffer
4283 * @ioa_cfg: ioa config struct
4284 * @ipr_cmd: ipr command struct
4285 *
4286 * Return value:
4287 * 0 on success / -1 on failure
4288 **/
4289static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
4290 struct ipr_cmnd *ipr_cmd)
4291{
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004292 int i, nseg;
4293 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 u32 length;
4295 u32 ioadl_flags = 0;
4296 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4297 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4298 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4299
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004300 length = scsi_bufflen(scsi_cmd);
4301 if (!length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return 0;
4303
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004304 nseg = scsi_dma_map(scsi_cmd);
4305 if (nseg < 0) {
4306 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
4307 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 }
4309
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004310 ipr_cmd->dma_use_sg = nseg;
4311
4312 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
4313 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
4314 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4315 ioarcb->write_data_transfer_length = cpu_to_be32(length);
4316 ioarcb->write_ioadl_len =
4317 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
4318 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
4319 ioadl_flags = IPR_IOADL_FLAGS_READ;
4320 ioarcb->read_data_transfer_length = cpu_to_be32(length);
4321 ioarcb->read_ioadl_len =
4322 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
4323 }
4324
4325 if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->add_data.u.ioadl)) {
4326 ioadl = ioarcb->add_data.u.ioadl;
4327 ioarcb->write_ioadl_addr =
4328 cpu_to_be32(be32_to_cpu(ioarcb->ioarcb_host_pci_addr) +
4329 offsetof(struct ipr_ioarcb, add_data));
4330 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
4331 }
4332
4333 scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
4334 ioadl[i].flags_and_data_len =
4335 cpu_to_be32(ioadl_flags | sg_dma_len(sg));
4336 ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
4337 }
4338
4339 ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
4340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341}
4342
4343/**
4344 * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
4345 * @scsi_cmd: scsi command struct
4346 *
4347 * Return value:
4348 * task attributes
4349 **/
4350static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
4351{
4352 u8 tag[2];
4353 u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
4354
4355 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
4356 switch (tag[0]) {
4357 case MSG_SIMPLE_TAG:
4358 rc = IPR_FLAGS_LO_SIMPLE_TASK;
4359 break;
4360 case MSG_HEAD_TAG:
4361 rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
4362 break;
4363 case MSG_ORDERED_TAG:
4364 rc = IPR_FLAGS_LO_ORDERED_TASK;
4365 break;
4366 };
4367 }
4368
4369 return rc;
4370}
4371
4372/**
4373 * ipr_erp_done - Process completion of ERP for a device
4374 * @ipr_cmd: ipr command struct
4375 *
4376 * This function copies the sense buffer into the scsi_cmd
4377 * struct and pushes the scsi_done function.
4378 *
4379 * Return value:
4380 * nothing
4381 **/
4382static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
4383{
4384 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4385 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4386 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4387 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4388
4389 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
4390 scsi_cmd->result |= (DID_ERROR << 16);
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004391 scmd_printk(KERN_ERR, scsi_cmd,
4392 "Request Sense failed with IOASC: 0x%08X\n", ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 } else {
4394 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
4395 SCSI_SENSE_BUFFERSIZE);
4396 }
4397
4398 if (res) {
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004399 if (!ipr_is_naca_model(res))
4400 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 res->in_erp = 0;
4402 }
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004403 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4405 scsi_cmd->scsi_done(scsi_cmd);
4406}
4407
4408/**
4409 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
4410 * @ipr_cmd: ipr command struct
4411 *
4412 * Return value:
4413 * none
4414 **/
4415static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
4416{
Brian King51b1c7e2007-03-29 12:43:50 -05004417 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4418 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4419 dma_addr_t dma_addr = be32_to_cpu(ioarcb->ioarcb_host_pci_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
4421 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
4422 ioarcb->write_data_transfer_length = 0;
4423 ioarcb->read_data_transfer_length = 0;
4424 ioarcb->write_ioadl_len = 0;
4425 ioarcb->read_ioadl_len = 0;
4426 ioasa->ioasc = 0;
4427 ioasa->residual_data_len = 0;
Brian King51b1c7e2007-03-29 12:43:50 -05004428 ioarcb->write_ioadl_addr =
4429 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
4430 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431}
4432
4433/**
4434 * ipr_erp_request_sense - Send request sense to a device
4435 * @ipr_cmd: ipr command struct
4436 *
4437 * This function sends a request sense to a device as a result
4438 * of a check condition.
4439 *
4440 * Return value:
4441 * nothing
4442 **/
4443static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
4444{
4445 struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4446 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4447
4448 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
4449 ipr_erp_done(ipr_cmd);
4450 return;
4451 }
4452
4453 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
4454
4455 cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
4456 cmd_pkt->cdb[0] = REQUEST_SENSE;
4457 cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
4458 cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
4459 cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
4460 cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
4461
4462 ipr_cmd->ioadl[0].flags_and_data_len =
4463 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | SCSI_SENSE_BUFFERSIZE);
4464 ipr_cmd->ioadl[0].address =
4465 cpu_to_be32(ipr_cmd->sense_buffer_dma);
4466
4467 ipr_cmd->ioarcb.read_ioadl_len =
4468 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4469 ipr_cmd->ioarcb.read_data_transfer_length =
4470 cpu_to_be32(SCSI_SENSE_BUFFERSIZE);
4471
4472 ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
4473 IPR_REQUEST_SENSE_TIMEOUT * 2);
4474}
4475
4476/**
4477 * ipr_erp_cancel_all - Send cancel all to a device
4478 * @ipr_cmd: ipr command struct
4479 *
4480 * This function sends a cancel all to a device to clear the
4481 * queue. If we are running TCQ on the device, QERR is set to 1,
4482 * which means all outstanding ops have been dropped on the floor.
4483 * Cancel all will return them to us.
4484 *
4485 * Return value:
4486 * nothing
4487 **/
4488static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
4489{
4490 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4491 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4492 struct ipr_cmd_pkt *cmd_pkt;
4493
4494 res->in_erp = 1;
4495
4496 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
4497
4498 if (!scsi_get_tag_type(scsi_cmd->device)) {
4499 ipr_erp_request_sense(ipr_cmd);
4500 return;
4501 }
4502
4503 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4504 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4505 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4506
4507 ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
4508 IPR_CANCEL_ALL_TIMEOUT);
4509}
4510
4511/**
4512 * ipr_dump_ioasa - Dump contents of IOASA
4513 * @ioa_cfg: ioa config struct
4514 * @ipr_cmd: ipr command struct
Brian Kingfe964d02006-03-29 09:37:29 -06004515 * @res: resource entry struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 *
4517 * This function is invoked by the interrupt handler when ops
4518 * fail. It will log the IOASA if appropriate. Only called
4519 * for GPDD ops.
4520 *
4521 * Return value:
4522 * none
4523 **/
4524static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
Brian Kingfe964d02006-03-29 09:37:29 -06004525 struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
4527 int i;
4528 u16 data_len;
Brian Kingb0692dd2007-03-29 12:43:09 -05004529 u32 ioasc, fd_ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4531 __be32 *ioasa_data = (__be32 *)ioasa;
4532 int error_index;
4533
4534 ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
Brian Kingb0692dd2007-03-29 12:43:09 -05004535 fd_ioasc = be32_to_cpu(ioasa->fd_ioasc) & IPR_IOASC_IOASC_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
4537 if (0 == ioasc)
4538 return;
4539
4540 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
4541 return;
4542
Brian Kingb0692dd2007-03-29 12:43:09 -05004543 if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
4544 error_index = ipr_get_error(fd_ioasc);
4545 else
4546 error_index = ipr_get_error(ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
4548 if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
4549 /* Don't log an error if the IOA already logged one */
4550 if (ioasa->ilid != 0)
4551 return;
4552
Brian Kingcc9bd5d2007-03-29 12:43:01 -05004553 if (!ipr_is_gscsi(res))
4554 return;
4555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 if (ipr_error_table[error_index].log_ioasa == 0)
4557 return;
4558 }
4559
Brian Kingfe964d02006-03-29 09:37:29 -06004560 ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
4563 data_len = sizeof(struct ipr_ioasa);
4564 else
4565 data_len = be16_to_cpu(ioasa->ret_stat_len);
4566
4567 ipr_err("IOASA Dump:\n");
4568
4569 for (i = 0; i < data_len / 4; i += 4) {
4570 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
4571 be32_to_cpu(ioasa_data[i]),
4572 be32_to_cpu(ioasa_data[i+1]),
4573 be32_to_cpu(ioasa_data[i+2]),
4574 be32_to_cpu(ioasa_data[i+3]));
4575 }
4576}
4577
4578/**
4579 * ipr_gen_sense - Generate SCSI sense data from an IOASA
4580 * @ioasa: IOASA
4581 * @sense_buf: sense data buffer
4582 *
4583 * Return value:
4584 * none
4585 **/
4586static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
4587{
4588 u32 failing_lba;
4589 u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
4590 struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
4591 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4592 u32 ioasc = be32_to_cpu(ioasa->ioasc);
4593
4594 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
4595
4596 if (ioasc >= IPR_FIRST_DRIVER_IOASC)
4597 return;
4598
4599 ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
4600
4601 if (ipr_is_vset_device(res) &&
4602 ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
4603 ioasa->u.vset.failing_lba_hi != 0) {
4604 sense_buf[0] = 0x72;
4605 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
4606 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
4607 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
4608
4609 sense_buf[7] = 12;
4610 sense_buf[8] = 0;
4611 sense_buf[9] = 0x0A;
4612 sense_buf[10] = 0x80;
4613
4614 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
4615
4616 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
4617 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
4618 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
4619 sense_buf[15] = failing_lba & 0x000000ff;
4620
4621 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
4622
4623 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
4624 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
4625 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
4626 sense_buf[19] = failing_lba & 0x000000ff;
4627 } else {
4628 sense_buf[0] = 0x70;
4629 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
4630 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
4631 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
4632
4633 /* Illegal request */
4634 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
4635 (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
4636 sense_buf[7] = 10; /* additional length */
4637
4638 /* IOARCB was in error */
4639 if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
4640 sense_buf[15] = 0xC0;
4641 else /* Parameter data was invalid */
4642 sense_buf[15] = 0x80;
4643
4644 sense_buf[16] =
4645 ((IPR_FIELD_POINTER_MASK &
4646 be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
4647 sense_buf[17] =
4648 (IPR_FIELD_POINTER_MASK &
4649 be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
4650 } else {
4651 if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
4652 if (ipr_is_vset_device(res))
4653 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
4654 else
4655 failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
4656
4657 sense_buf[0] |= 0x80; /* Or in the Valid bit */
4658 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
4659 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
4660 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
4661 sense_buf[6] = failing_lba & 0x000000ff;
4662 }
4663
4664 sense_buf[7] = 6; /* additional length */
4665 }
4666 }
4667}
4668
4669/**
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004670 * ipr_get_autosense - Copy autosense data to sense buffer
4671 * @ipr_cmd: ipr command struct
4672 *
4673 * This function copies the autosense buffer to the buffer
4674 * in the scsi_cmd, if there is autosense available.
4675 *
4676 * Return value:
4677 * 1 if autosense was available / 0 if not
4678 **/
4679static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
4680{
4681 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4682
Brian King117d2ce2006-08-02 14:57:58 -05004683 if ((be32_to_cpu(ioasa->ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004684 return 0;
4685
4686 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
4687 min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
4688 SCSI_SENSE_BUFFERSIZE));
4689 return 1;
4690}
4691
4692/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 * ipr_erp_start - Process an error response for a SCSI op
4694 * @ioa_cfg: ioa config struct
4695 * @ipr_cmd: ipr command struct
4696 *
4697 * This function determines whether or not to initiate ERP
4698 * on the affected device.
4699 *
4700 * Return value:
4701 * nothing
4702 **/
4703static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
4704 struct ipr_cmnd *ipr_cmd)
4705{
4706 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4707 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4708 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
Brian King8a048992007-04-26 16:00:10 -05004709 u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
4711 if (!res) {
4712 ipr_scsi_eh_done(ipr_cmd);
4713 return;
4714 }
4715
Brian King8a048992007-04-26 16:00:10 -05004716 if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 ipr_gen_sense(ipr_cmd);
4718
Brian Kingcc9bd5d2007-03-29 12:43:01 -05004719 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
4720
Brian King8a048992007-04-26 16:00:10 -05004721 switch (masked_ioasc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004723 if (ipr_is_naca_model(res))
4724 scsi_cmd->result |= (DID_ABORT << 16);
4725 else
4726 scsi_cmd->result |= (DID_IMM_RETRY << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 break;
4728 case IPR_IOASC_IR_RESOURCE_HANDLE:
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06004729 case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 scsi_cmd->result |= (DID_NO_CONNECT << 16);
4731 break;
4732 case IPR_IOASC_HW_SEL_TIMEOUT:
4733 scsi_cmd->result |= (DID_NO_CONNECT << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004734 if (!ipr_is_naca_model(res))
4735 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 break;
4737 case IPR_IOASC_SYNC_REQUIRED:
4738 if (!res->in_erp)
4739 res->needs_sync_complete = 1;
4740 scsi_cmd->result |= (DID_IMM_RETRY << 16);
4741 break;
4742 case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06004743 case IPR_IOASA_IR_DUAL_IOA_DISABLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
4745 break;
4746 case IPR_IOASC_BUS_WAS_RESET:
4747 case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
4748 /*
4749 * Report the bus reset and ask for a retry. The device
4750 * will give CC/UA the next command.
4751 */
4752 if (!res->resetting_device)
4753 scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
4754 scsi_cmd->result |= (DID_ERROR << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004755 if (!ipr_is_naca_model(res))
4756 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 break;
4758 case IPR_IOASC_HW_DEV_BUS_STATUS:
4759 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
4760 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004761 if (!ipr_get_autosense(ipr_cmd)) {
4762 if (!ipr_is_naca_model(res)) {
4763 ipr_erp_cancel_all(ipr_cmd);
4764 return;
4765 }
4766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 }
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004768 if (!ipr_is_naca_model(res))
4769 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 break;
4771 case IPR_IOASC_NR_INIT_CMD_REQUIRED:
4772 break;
4773 default:
Brian King5b7304f2006-08-02 14:57:51 -05004774 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
4775 scsi_cmd->result |= (DID_ERROR << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004776 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 res->needs_sync_complete = 1;
4778 break;
4779 }
4780
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004781 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4783 scsi_cmd->scsi_done(scsi_cmd);
4784}
4785
4786/**
4787 * ipr_scsi_done - mid-layer done function
4788 * @ipr_cmd: ipr command struct
4789 *
4790 * This function is invoked by the interrupt handler for
4791 * ops generated by the SCSI mid-layer
4792 *
4793 * Return value:
4794 * none
4795 **/
4796static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
4797{
4798 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4799 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4800 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4801
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004802 scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->ioasa.residual_data_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
4804 if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004805 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4807 scsi_cmd->scsi_done(scsi_cmd);
4808 } else
4809 ipr_erp_start(ioa_cfg, ipr_cmd);
4810}
4811
4812/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 * ipr_queuecommand - Queue a mid-layer request
4814 * @scsi_cmd: scsi command struct
4815 * @done: done function
4816 *
4817 * This function queues a request generated by the mid-layer.
4818 *
4819 * Return value:
4820 * 0 on success
4821 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
4822 * SCSI_MLQUEUE_HOST_BUSY if host is busy
4823 **/
4824static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
4825 void (*done) (struct scsi_cmnd *))
4826{
4827 struct ipr_ioa_cfg *ioa_cfg;
4828 struct ipr_resource_entry *res;
4829 struct ipr_ioarcb *ioarcb;
4830 struct ipr_cmnd *ipr_cmd;
4831 int rc = 0;
4832
4833 scsi_cmd->scsi_done = done;
4834 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4835 res = scsi_cmd->device->hostdata;
4836 scsi_cmd->result = (DID_OK << 16);
4837
4838 /*
4839 * We are currently blocking all devices due to a host reset
4840 * We have told the host to stop giving us new requests, but
4841 * ERP ops don't count. FIXME
4842 */
4843 if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
4844 return SCSI_MLQUEUE_HOST_BUSY;
4845
4846 /*
4847 * FIXME - Create scsi_set_host_offline interface
4848 * and the ioa_is_dead check can be removed
4849 */
4850 if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
4851 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
4852 scsi_cmd->result = (DID_NO_CONNECT << 16);
4853 scsi_cmd->scsi_done(scsi_cmd);
4854 return 0;
4855 }
4856
Brian King35a39692006-09-25 12:39:20 -05004857 if (ipr_is_gata(res) && res->sata_port)
4858 return ata_sas_queuecmd(scsi_cmd, done, res->sata_port->ap);
4859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4861 ioarcb = &ipr_cmd->ioarcb;
4862 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
4863
4864 memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
4865 ipr_cmd->scsi_cmd = scsi_cmd;
4866 ioarcb->res_handle = res->cfgte.res_handle;
4867 ipr_cmd->done = ipr_scsi_done;
4868 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
4869
4870 if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
4871 if (scsi_cmd->underflow == 0)
4872 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
4873
4874 if (res->needs_sync_complete) {
4875 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
4876 res->needs_sync_complete = 0;
4877 }
4878
4879 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
4880 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
4881 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
4882 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
4883 }
4884
4885 if (scsi_cmd->cmnd[0] >= 0xC0 &&
4886 (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
4887 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 if (likely(rc == 0))
4890 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
4891
4892 if (likely(rc == 0)) {
4893 mb();
4894 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
4895 ioa_cfg->regs.ioarrin_reg);
4896 } else {
4897 list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4898 return SCSI_MLQUEUE_HOST_BUSY;
4899 }
4900
4901 return 0;
4902}
4903
4904/**
Brian King35a39692006-09-25 12:39:20 -05004905 * ipr_ioctl - IOCTL handler
4906 * @sdev: scsi device struct
4907 * @cmd: IOCTL cmd
4908 * @arg: IOCTL arg
4909 *
4910 * Return value:
4911 * 0 on success / other on failure
4912 **/
Adrian Bunkbd705f22006-11-21 10:28:48 -06004913static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
Brian King35a39692006-09-25 12:39:20 -05004914{
4915 struct ipr_resource_entry *res;
4916
4917 res = (struct ipr_resource_entry *)sdev->hostdata;
Brian King0ce3a7e2008-07-11 13:37:50 -05004918 if (res && ipr_is_gata(res)) {
4919 if (cmd == HDIO_GET_IDENTITY)
4920 return -ENOTTY;
Brian King35a39692006-09-25 12:39:20 -05004921 return ata_scsi_ioctl(sdev, cmd, arg);
Brian King0ce3a7e2008-07-11 13:37:50 -05004922 }
Brian King35a39692006-09-25 12:39:20 -05004923
4924 return -EINVAL;
4925}
4926
4927/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 * ipr_info - Get information about the card/driver
4929 * @scsi_host: scsi host struct
4930 *
4931 * Return value:
4932 * pointer to buffer with description string
4933 **/
4934static const char * ipr_ioa_info(struct Scsi_Host *host)
4935{
4936 static char buffer[512];
4937 struct ipr_ioa_cfg *ioa_cfg;
4938 unsigned long lock_flags = 0;
4939
4940 ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
4941
4942 spin_lock_irqsave(host->host_lock, lock_flags);
4943 sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
4944 spin_unlock_irqrestore(host->host_lock, lock_flags);
4945
4946 return buffer;
4947}
4948
4949static struct scsi_host_template driver_template = {
4950 .module = THIS_MODULE,
4951 .name = "IPR",
4952 .info = ipr_ioa_info,
Brian King35a39692006-09-25 12:39:20 -05004953 .ioctl = ipr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 .queuecommand = ipr_queuecommand,
4955 .eh_abort_handler = ipr_eh_abort,
4956 .eh_device_reset_handler = ipr_eh_dev_reset,
4957 .eh_host_reset_handler = ipr_eh_host_reset,
4958 .slave_alloc = ipr_slave_alloc,
4959 .slave_configure = ipr_slave_configure,
4960 .slave_destroy = ipr_slave_destroy,
Brian King35a39692006-09-25 12:39:20 -05004961 .target_alloc = ipr_target_alloc,
4962 .target_destroy = ipr_target_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 .change_queue_depth = ipr_change_queue_depth,
4964 .change_queue_type = ipr_change_queue_type,
4965 .bios_param = ipr_biosparam,
4966 .can_queue = IPR_MAX_COMMANDS,
4967 .this_id = -1,
4968 .sg_tablesize = IPR_MAX_SGLIST,
4969 .max_sectors = IPR_IOA_MAX_SECTORS,
4970 .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
4971 .use_clustering = ENABLE_CLUSTERING,
4972 .shost_attrs = ipr_ioa_attrs,
4973 .sdev_attrs = ipr_dev_attrs,
4974 .proc_name = IPR_NAME
4975};
4976
Brian King35a39692006-09-25 12:39:20 -05004977/**
4978 * ipr_ata_phy_reset - libata phy_reset handler
4979 * @ap: ata port to reset
4980 *
4981 **/
4982static void ipr_ata_phy_reset(struct ata_port *ap)
4983{
4984 unsigned long flags;
4985 struct ipr_sata_port *sata_port = ap->private_data;
4986 struct ipr_resource_entry *res = sata_port->res;
4987 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4988 int rc;
4989
4990 ENTER;
4991 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
4992 while(ioa_cfg->in_reset_reload) {
4993 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
4994 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4995 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
4996 }
4997
4998 if (!ioa_cfg->allow_cmds)
4999 goto out_unlock;
5000
5001 rc = ipr_device_reset(ioa_cfg, res);
5002
5003 if (rc) {
Jeff Garzikac8869d2007-08-16 03:17:03 -04005004 ata_port_disable(ap);
Brian King35a39692006-09-25 12:39:20 -05005005 goto out_unlock;
5006 }
5007
5008 switch(res->cfgte.proto) {
5009 case IPR_PROTO_SATA:
5010 case IPR_PROTO_SAS_STP:
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005011 ap->link.device[0].class = ATA_DEV_ATA;
Brian King35a39692006-09-25 12:39:20 -05005012 break;
5013 case IPR_PROTO_SATA_ATAPI:
5014 case IPR_PROTO_SAS_STP_ATAPI:
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005015 ap->link.device[0].class = ATA_DEV_ATAPI;
Brian King35a39692006-09-25 12:39:20 -05005016 break;
5017 default:
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005018 ap->link.device[0].class = ATA_DEV_UNKNOWN;
Jeff Garzikac8869d2007-08-16 03:17:03 -04005019 ata_port_disable(ap);
Brian King35a39692006-09-25 12:39:20 -05005020 break;
5021 };
5022
5023out_unlock:
5024 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5025 LEAVE;
5026}
5027
5028/**
5029 * ipr_ata_post_internal - Cleanup after an internal command
5030 * @qc: ATA queued command
5031 *
5032 * Return value:
5033 * none
5034 **/
5035static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
5036{
5037 struct ipr_sata_port *sata_port = qc->ap->private_data;
5038 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5039 struct ipr_cmnd *ipr_cmd;
5040 unsigned long flags;
5041
5042 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
Brian King73d98ff2006-11-21 10:27:58 -06005043 while(ioa_cfg->in_reset_reload) {
5044 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5045 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5046 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5047 }
5048
Brian King35a39692006-09-25 12:39:20 -05005049 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
5050 if (ipr_cmd->qc == qc) {
5051 ipr_device_reset(ioa_cfg, sata_port->res);
5052 break;
5053 }
5054 }
5055 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5056}
5057
5058/**
Brian King35a39692006-09-25 12:39:20 -05005059 * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
5060 * @regs: destination
5061 * @tf: source ATA taskfile
5062 *
5063 * Return value:
5064 * none
5065 **/
5066static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
5067 struct ata_taskfile *tf)
5068{
5069 regs->feature = tf->feature;
5070 regs->nsect = tf->nsect;
5071 regs->lbal = tf->lbal;
5072 regs->lbam = tf->lbam;
5073 regs->lbah = tf->lbah;
5074 regs->device = tf->device;
5075 regs->command = tf->command;
5076 regs->hob_feature = tf->hob_feature;
5077 regs->hob_nsect = tf->hob_nsect;
5078 regs->hob_lbal = tf->hob_lbal;
5079 regs->hob_lbam = tf->hob_lbam;
5080 regs->hob_lbah = tf->hob_lbah;
5081 regs->ctl = tf->ctl;
5082}
5083
5084/**
5085 * ipr_sata_done - done function for SATA commands
5086 * @ipr_cmd: ipr command struct
5087 *
5088 * This function is invoked by the interrupt handler for
5089 * ops generated by the SCSI mid-layer to SATA devices
5090 *
5091 * Return value:
5092 * none
5093 **/
5094static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
5095{
5096 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5097 struct ata_queued_cmd *qc = ipr_cmd->qc;
5098 struct ipr_sata_port *sata_port = qc->ap->private_data;
5099 struct ipr_resource_entry *res = sata_port->res;
5100 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5101
5102 memcpy(&sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
5103 sizeof(struct ipr_ioasa_gata));
5104 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5105
5106 if (be32_to_cpu(ipr_cmd->ioasa.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
5107 scsi_report_device_reset(ioa_cfg->host, res->cfgte.res_addr.bus,
5108 res->cfgte.res_addr.target);
5109
5110 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5111 qc->err_mask |= __ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5112 else
5113 qc->err_mask |= ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5114 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5115 ata_qc_complete(qc);
5116}
5117
5118/**
5119 * ipr_build_ata_ioadl - Build an ATA scatter/gather list
5120 * @ipr_cmd: ipr command struct
5121 * @qc: ATA queued command
5122 *
5123 **/
5124static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
5125 struct ata_queued_cmd *qc)
5126{
5127 u32 ioadl_flags = 0;
5128 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5129 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04005130 struct ipr_ioadl_desc *last_ioadl = NULL;
James Bottomleydde20202008-02-19 11:36:56 +01005131 int len = qc->nbytes;
Brian King35a39692006-09-25 12:39:20 -05005132 struct scatterlist *sg;
Tejun Heoff2aeb12007-12-05 16:43:11 +09005133 unsigned int si;
Brian King35a39692006-09-25 12:39:20 -05005134
5135 if (len == 0)
5136 return;
5137
5138 if (qc->dma_dir == DMA_TO_DEVICE) {
5139 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5140 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5141 ioarcb->write_data_transfer_length = cpu_to_be32(len);
5142 ioarcb->write_ioadl_len =
5143 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5144 } else if (qc->dma_dir == DMA_FROM_DEVICE) {
5145 ioadl_flags = IPR_IOADL_FLAGS_READ;
5146 ioarcb->read_data_transfer_length = cpu_to_be32(len);
5147 ioarcb->read_ioadl_len =
5148 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5149 }
5150
Tejun Heoff2aeb12007-12-05 16:43:11 +09005151 for_each_sg(qc->sg, sg, qc->n_elem, si) {
Brian King35a39692006-09-25 12:39:20 -05005152 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5153 ioadl->address = cpu_to_be32(sg_dma_address(sg));
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04005154
5155 last_ioadl = ioadl;
5156 ioadl++;
Brian King35a39692006-09-25 12:39:20 -05005157 }
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04005158
5159 if (likely(last_ioadl))
5160 last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
Brian King35a39692006-09-25 12:39:20 -05005161}
5162
5163/**
5164 * ipr_qc_issue - Issue a SATA qc to a device
5165 * @qc: queued command
5166 *
5167 * Return value:
5168 * 0 if success
5169 **/
5170static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
5171{
5172 struct ata_port *ap = qc->ap;
5173 struct ipr_sata_port *sata_port = ap->private_data;
5174 struct ipr_resource_entry *res = sata_port->res;
5175 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5176 struct ipr_cmnd *ipr_cmd;
5177 struct ipr_ioarcb *ioarcb;
5178 struct ipr_ioarcb_ata_regs *regs;
5179
5180 if (unlikely(!ioa_cfg->allow_cmds || ioa_cfg->ioa_is_dead))
Brian King0feeed82007-03-29 12:43:43 -05005181 return AC_ERR_SYSTEM;
Brian King35a39692006-09-25 12:39:20 -05005182
5183 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5184 ioarcb = &ipr_cmd->ioarcb;
5185 regs = &ioarcb->add_data.u.regs;
5186
5187 memset(&ioarcb->add_data, 0, sizeof(ioarcb->add_data));
5188 ioarcb->add_cmd_parms_len = cpu_to_be32(sizeof(ioarcb->add_data.u.regs));
5189
5190 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5191 ipr_cmd->qc = qc;
5192 ipr_cmd->done = ipr_sata_done;
5193 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
5194 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
5195 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
5196 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
James Bottomleydde20202008-02-19 11:36:56 +01005197 ipr_cmd->dma_use_sg = qc->n_elem;
Brian King35a39692006-09-25 12:39:20 -05005198
5199 ipr_build_ata_ioadl(ipr_cmd, qc);
5200 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
5201 ipr_copy_sata_tf(regs, &qc->tf);
5202 memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
5203 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
5204
5205 switch (qc->tf.protocol) {
5206 case ATA_PROT_NODATA:
5207 case ATA_PROT_PIO:
5208 break;
5209
5210 case ATA_PROT_DMA:
5211 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5212 break;
5213
Tejun Heo0dc36882007-12-18 16:34:43 -05005214 case ATAPI_PROT_PIO:
5215 case ATAPI_PROT_NODATA:
Brian King35a39692006-09-25 12:39:20 -05005216 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5217 break;
5218
Tejun Heo0dc36882007-12-18 16:34:43 -05005219 case ATAPI_PROT_DMA:
Brian King35a39692006-09-25 12:39:20 -05005220 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5221 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5222 break;
5223
5224 default:
5225 WARN_ON(1);
Brian King0feeed82007-03-29 12:43:43 -05005226 return AC_ERR_INVALID;
Brian King35a39692006-09-25 12:39:20 -05005227 }
5228
5229 mb();
5230 writel(be32_to_cpu(ioarcb->ioarcb_host_pci_addr),
5231 ioa_cfg->regs.ioarrin_reg);
5232 return 0;
5233}
5234
5235/**
Tejun Heo4c9bf4e2008-04-07 22:47:20 +09005236 * ipr_qc_fill_rtf - Read result TF
5237 * @qc: ATA queued command
5238 *
5239 * Return value:
5240 * true
5241 **/
5242static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
5243{
5244 struct ipr_sata_port *sata_port = qc->ap->private_data;
5245 struct ipr_ioasa_gata *g = &sata_port->ioasa;
5246 struct ata_taskfile *tf = &qc->result_tf;
5247
5248 tf->feature = g->error;
5249 tf->nsect = g->nsect;
5250 tf->lbal = g->lbal;
5251 tf->lbam = g->lbam;
5252 tf->lbah = g->lbah;
5253 tf->device = g->device;
5254 tf->command = g->status;
5255 tf->hob_nsect = g->hob_nsect;
5256 tf->hob_lbal = g->hob_lbal;
5257 tf->hob_lbam = g->hob_lbam;
5258 tf->hob_lbah = g->hob_lbah;
5259 tf->ctl = g->alt_status;
5260
5261 return true;
5262}
5263
Brian King35a39692006-09-25 12:39:20 -05005264static struct ata_port_operations ipr_sata_ops = {
Brian King35a39692006-09-25 12:39:20 -05005265 .phy_reset = ipr_ata_phy_reset,
Tejun Heoa1efdab2008-03-25 12:22:50 +09005266 .hardreset = ipr_sata_reset,
Brian King35a39692006-09-25 12:39:20 -05005267 .post_internal_cmd = ipr_ata_post_internal,
Brian King35a39692006-09-25 12:39:20 -05005268 .qc_prep = ata_noop_qc_prep,
5269 .qc_issue = ipr_qc_issue,
Tejun Heo4c9bf4e2008-04-07 22:47:20 +09005270 .qc_fill_rtf = ipr_qc_fill_rtf,
Brian King35a39692006-09-25 12:39:20 -05005271 .port_start = ata_sas_port_start,
5272 .port_stop = ata_sas_port_stop
5273};
5274
5275static struct ata_port_info sata_port_info = {
5276 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
5277 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
5278 .pio_mask = 0x10, /* pio4 */
5279 .mwdma_mask = 0x07,
5280 .udma_mask = 0x7f, /* udma0-6 */
5281 .port_ops = &ipr_sata_ops
5282};
5283
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284#ifdef CONFIG_PPC_PSERIES
5285static const u16 ipr_blocked_processors[] = {
5286 PV_NORTHSTAR,
5287 PV_PULSAR,
5288 PV_POWER4,
5289 PV_ICESTAR,
5290 PV_SSTAR,
5291 PV_POWER4p,
5292 PV_630,
5293 PV_630p
5294};
5295
5296/**
5297 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
5298 * @ioa_cfg: ioa cfg struct
5299 *
5300 * Adapters that use Gemstone revision < 3.1 do not work reliably on
5301 * certain pSeries hardware. This function determines if the given
5302 * adapter is in one of these confgurations or not.
5303 *
5304 * Return value:
5305 * 1 if adapter is not supported / 0 if adapter is supported
5306 **/
5307static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
5308{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 int i;
5310
Auke Kok44c10132007-06-08 15:46:36 -07005311 if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
5312 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
5313 if (__is_processor(ipr_blocked_processors[i]))
5314 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 }
5316 }
5317 return 0;
5318}
5319#else
5320#define ipr_invalid_adapter(ioa_cfg) 0
5321#endif
5322
5323/**
5324 * ipr_ioa_bringdown_done - IOA bring down completion.
5325 * @ipr_cmd: ipr command struct
5326 *
5327 * This function processes the completion of an adapter bring down.
5328 * It wakes any reset sleepers.
5329 *
5330 * Return value:
5331 * IPR_RC_JOB_RETURN
5332 **/
5333static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
5334{
5335 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5336
5337 ENTER;
5338 ioa_cfg->in_reset_reload = 0;
5339 ioa_cfg->reset_retries = 0;
5340 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5341 wake_up_all(&ioa_cfg->reset_wait_q);
5342
5343 spin_unlock_irq(ioa_cfg->host->host_lock);
5344 scsi_unblock_requests(ioa_cfg->host);
5345 spin_lock_irq(ioa_cfg->host->host_lock);
5346 LEAVE;
5347
5348 return IPR_RC_JOB_RETURN;
5349}
5350
5351/**
5352 * ipr_ioa_reset_done - IOA reset completion.
5353 * @ipr_cmd: ipr command struct
5354 *
5355 * This function processes the completion of an adapter reset.
5356 * It schedules any necessary mid-layer add/removes and
5357 * wakes any reset sleepers.
5358 *
5359 * Return value:
5360 * IPR_RC_JOB_RETURN
5361 **/
5362static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
5363{
5364 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5365 struct ipr_resource_entry *res;
5366 struct ipr_hostrcb *hostrcb, *temp;
5367 int i = 0;
5368
5369 ENTER;
5370 ioa_cfg->in_reset_reload = 0;
5371 ioa_cfg->allow_cmds = 1;
5372 ioa_cfg->reset_cmd = NULL;
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06005373 ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
5375 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
5376 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
5377 ipr_trace;
5378 break;
5379 }
5380 }
5381 schedule_work(&ioa_cfg->work_q);
5382
5383 list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
5384 list_del(&hostrcb->queue);
5385 if (i++ < IPR_NUM_LOG_HCAMS)
5386 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
5387 else
5388 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
5389 }
5390
Brian King6bb04172007-04-26 16:00:08 -05005391 scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
5393
5394 ioa_cfg->reset_retries = 0;
5395 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5396 wake_up_all(&ioa_cfg->reset_wait_q);
5397
5398 spin_unlock_irq(ioa_cfg->host->host_lock);
5399 scsi_unblock_requests(ioa_cfg->host);
5400 spin_lock_irq(ioa_cfg->host->host_lock);
5401
5402 if (!ioa_cfg->allow_cmds)
5403 scsi_block_requests(ioa_cfg->host);
5404
5405 LEAVE;
5406 return IPR_RC_JOB_RETURN;
5407}
5408
5409/**
5410 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
5411 * @supported_dev: supported device struct
5412 * @vpids: vendor product id struct
5413 *
5414 * Return value:
5415 * none
5416 **/
5417static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
5418 struct ipr_std_inq_vpids *vpids)
5419{
5420 memset(supported_dev, 0, sizeof(struct ipr_supported_device));
5421 memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
5422 supported_dev->num_records = 1;
5423 supported_dev->data_length =
5424 cpu_to_be16(sizeof(struct ipr_supported_device));
5425 supported_dev->reserved = 0;
5426}
5427
5428/**
5429 * ipr_set_supported_devs - Send Set Supported Devices for a device
5430 * @ipr_cmd: ipr command struct
5431 *
5432 * This function send a Set Supported Devices to the adapter
5433 *
5434 * Return value:
5435 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5436 **/
5437static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
5438{
5439 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5440 struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
5441 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5442 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5443 struct ipr_resource_entry *res = ipr_cmd->u.res;
5444
5445 ipr_cmd->job_step = ipr_ioa_reset_done;
5446
5447 list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
Brian Kinge4fbf442006-03-29 09:37:22 -06005448 if (!ipr_is_scsi_disk(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 continue;
5450
5451 ipr_cmd->u.res = res;
5452 ipr_set_sup_dev_dflt(supp_dev, &res->cfgte.std_inq_data.vpids);
5453
5454 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5455 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5456 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5457
5458 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
5459 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
5460 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
5461
5462 ioadl->flags_and_data_len = cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST |
5463 sizeof(struct ipr_supported_device));
5464 ioadl->address = cpu_to_be32(ioa_cfg->vpd_cbs_dma +
5465 offsetof(struct ipr_misc_cbs, supp_dev));
5466 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5467 ioarcb->write_data_transfer_length =
5468 cpu_to_be32(sizeof(struct ipr_supported_device));
5469
5470 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
5471 IPR_SET_SUP_DEVICE_TIMEOUT);
5472
5473 ipr_cmd->job_step = ipr_set_supported_devs;
5474 return IPR_RC_JOB_RETURN;
5475 }
5476
5477 return IPR_RC_JOB_CONTINUE;
5478}
5479
5480/**
brking@us.ibm.com62275042005-11-01 17:01:14 -06005481 * ipr_setup_write_cache - Disable write cache if needed
5482 * @ipr_cmd: ipr command struct
5483 *
5484 * This function sets up adapters write cache to desired setting
5485 *
5486 * Return value:
5487 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5488 **/
5489static int ipr_setup_write_cache(struct ipr_cmnd *ipr_cmd)
5490{
5491 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5492
5493 ipr_cmd->job_step = ipr_set_supported_devs;
5494 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
5495 struct ipr_resource_entry, queue);
5496
5497 if (ioa_cfg->cache_state != CACHE_DISABLED)
5498 return IPR_RC_JOB_CONTINUE;
5499
5500 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5501 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5502 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
5503 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
5504
5505 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5506
5507 return IPR_RC_JOB_RETURN;
5508}
5509
5510/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 * ipr_get_mode_page - Locate specified mode page
5512 * @mode_pages: mode page buffer
5513 * @page_code: page code to find
5514 * @len: minimum required length for mode page
5515 *
5516 * Return value:
5517 * pointer to mode page / NULL on failure
5518 **/
5519static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
5520 u32 page_code, u32 len)
5521{
5522 struct ipr_mode_page_hdr *mode_hdr;
5523 u32 page_length;
5524 u32 length;
5525
5526 if (!mode_pages || (mode_pages->hdr.length == 0))
5527 return NULL;
5528
5529 length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
5530 mode_hdr = (struct ipr_mode_page_hdr *)
5531 (mode_pages->data + mode_pages->hdr.block_desc_len);
5532
5533 while (length) {
5534 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
5535 if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
5536 return mode_hdr;
5537 break;
5538 } else {
5539 page_length = (sizeof(struct ipr_mode_page_hdr) +
5540 mode_hdr->page_length);
5541 length -= page_length;
5542 mode_hdr = (struct ipr_mode_page_hdr *)
5543 ((unsigned long)mode_hdr + page_length);
5544 }
5545 }
5546 return NULL;
5547}
5548
5549/**
5550 * ipr_check_term_power - Check for term power errors
5551 * @ioa_cfg: ioa config struct
5552 * @mode_pages: IOAFP mode pages buffer
5553 *
5554 * Check the IOAFP's mode page 28 for term power errors
5555 *
5556 * Return value:
5557 * nothing
5558 **/
5559static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
5560 struct ipr_mode_pages *mode_pages)
5561{
5562 int i;
5563 int entry_length;
5564 struct ipr_dev_bus_entry *bus;
5565 struct ipr_mode_page28 *mode_page;
5566
5567 mode_page = ipr_get_mode_page(mode_pages, 0x28,
5568 sizeof(struct ipr_mode_page28));
5569
5570 entry_length = mode_page->entry_length;
5571
5572 bus = mode_page->bus;
5573
5574 for (i = 0; i < mode_page->num_entries; i++) {
5575 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
5576 dev_err(&ioa_cfg->pdev->dev,
5577 "Term power is absent on scsi bus %d\n",
5578 bus->res_addr.bus);
5579 }
5580
5581 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
5582 }
5583}
5584
5585/**
5586 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
5587 * @ioa_cfg: ioa config struct
5588 *
5589 * Looks through the config table checking for SES devices. If
5590 * the SES device is in the SES table indicating a maximum SCSI
5591 * bus speed, the speed is limited for the bus.
5592 *
5593 * Return value:
5594 * none
5595 **/
5596static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
5597{
5598 u32 max_xfer_rate;
5599 int i;
5600
5601 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
5602 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
5603 ioa_cfg->bus_attr[i].bus_width);
5604
5605 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
5606 ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
5607 }
5608}
5609
5610/**
5611 * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
5612 * @ioa_cfg: ioa config struct
5613 * @mode_pages: mode page 28 buffer
5614 *
5615 * Updates mode page 28 based on driver configuration
5616 *
5617 * Return value:
5618 * none
5619 **/
5620static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
5621 struct ipr_mode_pages *mode_pages)
5622{
5623 int i, entry_length;
5624 struct ipr_dev_bus_entry *bus;
5625 struct ipr_bus_attributes *bus_attr;
5626 struct ipr_mode_page28 *mode_page;
5627
5628 mode_page = ipr_get_mode_page(mode_pages, 0x28,
5629 sizeof(struct ipr_mode_page28));
5630
5631 entry_length = mode_page->entry_length;
5632
5633 /* Loop for each device bus entry */
5634 for (i = 0, bus = mode_page->bus;
5635 i < mode_page->num_entries;
5636 i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
5637 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
5638 dev_err(&ioa_cfg->pdev->dev,
5639 "Invalid resource address reported: 0x%08X\n",
5640 IPR_GET_PHYS_LOC(bus->res_addr));
5641 continue;
5642 }
5643
5644 bus_attr = &ioa_cfg->bus_attr[i];
5645 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
5646 bus->bus_width = bus_attr->bus_width;
5647 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
5648 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
5649 if (bus_attr->qas_enabled)
5650 bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
5651 else
5652 bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
5653 }
5654}
5655
5656/**
5657 * ipr_build_mode_select - Build a mode select command
5658 * @ipr_cmd: ipr command struct
5659 * @res_handle: resource handle to send command to
5660 * @parm: Byte 2 of Mode Sense command
5661 * @dma_addr: DMA buffer address
5662 * @xfer_len: data transfer length
5663 *
5664 * Return value:
5665 * none
5666 **/
5667static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
5668 __be32 res_handle, u8 parm, u32 dma_addr,
5669 u8 xfer_len)
5670{
5671 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5672 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5673
5674 ioarcb->res_handle = res_handle;
5675 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5676 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5677 ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
5678 ioarcb->cmd_pkt.cdb[1] = parm;
5679 ioarcb->cmd_pkt.cdb[4] = xfer_len;
5680
5681 ioadl->flags_and_data_len =
5682 cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST | xfer_len);
5683 ioadl->address = cpu_to_be32(dma_addr);
5684 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5685 ioarcb->write_data_transfer_length = cpu_to_be32(xfer_len);
5686}
5687
5688/**
5689 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
5690 * @ipr_cmd: ipr command struct
5691 *
5692 * This function sets up the SCSI bus attributes and sends
5693 * a Mode Select for Page 28 to activate them.
5694 *
5695 * Return value:
5696 * IPR_RC_JOB_RETURN
5697 **/
5698static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
5699{
5700 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5701 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
5702 int length;
5703
5704 ENTER;
Brian King47338042006-02-08 20:57:42 -06005705 ipr_scsi_bus_speed_limit(ioa_cfg);
5706 ipr_check_term_power(ioa_cfg, mode_pages);
5707 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
5708 length = mode_pages->hdr.length + 1;
5709 mode_pages->hdr.length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
5711 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
5712 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
5713 length);
5714
brking@us.ibm.com62275042005-11-01 17:01:14 -06005715 ipr_cmd->job_step = ipr_setup_write_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5717
5718 LEAVE;
5719 return IPR_RC_JOB_RETURN;
5720}
5721
5722/**
5723 * ipr_build_mode_sense - Builds a mode sense command
5724 * @ipr_cmd: ipr command struct
5725 * @res: resource entry struct
5726 * @parm: Byte 2 of mode sense command
5727 * @dma_addr: DMA address of mode sense buffer
5728 * @xfer_len: Size of DMA buffer
5729 *
5730 * Return value:
5731 * none
5732 **/
5733static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
5734 __be32 res_handle,
5735 u8 parm, u32 dma_addr, u8 xfer_len)
5736{
5737 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5738 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5739
5740 ioarcb->res_handle = res_handle;
5741 ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
5742 ioarcb->cmd_pkt.cdb[2] = parm;
5743 ioarcb->cmd_pkt.cdb[4] = xfer_len;
5744 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5745
5746 ioadl->flags_and_data_len =
5747 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
5748 ioadl->address = cpu_to_be32(dma_addr);
5749 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5750 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
5751}
5752
5753/**
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06005754 * ipr_reset_cmd_failed - Handle failure of IOA reset command
5755 * @ipr_cmd: ipr command struct
5756 *
5757 * This function handles the failure of an IOA bringup command.
5758 *
5759 * Return value:
5760 * IPR_RC_JOB_RETURN
5761 **/
5762static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
5763{
5764 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5765 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5766
5767 dev_err(&ioa_cfg->pdev->dev,
5768 "0x%02X failed with IOASC: 0x%08X\n",
5769 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
5770
5771 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5772 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5773 return IPR_RC_JOB_RETURN;
5774}
5775
5776/**
5777 * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
5778 * @ipr_cmd: ipr command struct
5779 *
5780 * This function handles the failure of a Mode Sense to the IOAFP.
5781 * Some adapters do not handle all mode pages.
5782 *
5783 * Return value:
5784 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5785 **/
5786static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
5787{
5788 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5789
5790 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
5791 ipr_cmd->job_step = ipr_setup_write_cache;
5792 return IPR_RC_JOB_CONTINUE;
5793 }
5794
5795 return ipr_reset_cmd_failed(ipr_cmd);
5796}
5797
5798/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
5800 * @ipr_cmd: ipr command struct
5801 *
5802 * This function send a Page 28 mode sense to the IOA to
5803 * retrieve SCSI bus attributes.
5804 *
5805 * Return value:
5806 * IPR_RC_JOB_RETURN
5807 **/
5808static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
5809{
5810 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5811
5812 ENTER;
5813 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
5814 0x28, ioa_cfg->vpd_cbs_dma +
5815 offsetof(struct ipr_misc_cbs, mode_pages),
5816 sizeof(struct ipr_mode_pages));
5817
5818 ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06005819 ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
5821 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5822
5823 LEAVE;
5824 return IPR_RC_JOB_RETURN;
5825}
5826
5827/**
Brian Kingac09c342007-04-26 16:00:16 -05005828 * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
5829 * @ipr_cmd: ipr command struct
5830 *
5831 * This function enables dual IOA RAID support if possible.
5832 *
5833 * Return value:
5834 * IPR_RC_JOB_RETURN
5835 **/
5836static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
5837{
5838 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5839 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
5840 struct ipr_mode_page24 *mode_page;
5841 int length;
5842
5843 ENTER;
5844 mode_page = ipr_get_mode_page(mode_pages, 0x24,
5845 sizeof(struct ipr_mode_page24));
5846
5847 if (mode_page)
5848 mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;
5849
5850 length = mode_pages->hdr.length + 1;
5851 mode_pages->hdr.length = 0;
5852
5853 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
5854 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
5855 length);
5856
5857 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
5858 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5859
5860 LEAVE;
5861 return IPR_RC_JOB_RETURN;
5862}
5863
5864/**
5865 * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
5866 * @ipr_cmd: ipr command struct
5867 *
5868 * This function handles the failure of a Mode Sense to the IOAFP.
5869 * Some adapters do not handle all mode pages.
5870 *
5871 * Return value:
5872 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5873 **/
5874static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
5875{
5876 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5877
5878 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
5879 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
5880 return IPR_RC_JOB_CONTINUE;
5881 }
5882
5883 return ipr_reset_cmd_failed(ipr_cmd);
5884}
5885
5886/**
5887 * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
5888 * @ipr_cmd: ipr command struct
5889 *
5890 * This function send a mode sense to the IOA to retrieve
5891 * the IOA Advanced Function Control mode page.
5892 *
5893 * Return value:
5894 * IPR_RC_JOB_RETURN
5895 **/
5896static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
5897{
5898 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5899
5900 ENTER;
5901 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
5902 0x24, ioa_cfg->vpd_cbs_dma +
5903 offsetof(struct ipr_misc_cbs, mode_pages),
5904 sizeof(struct ipr_mode_pages));
5905
5906 ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
5907 ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;
5908
5909 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5910
5911 LEAVE;
5912 return IPR_RC_JOB_RETURN;
5913}
5914
5915/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 * ipr_init_res_table - Initialize the resource table
5917 * @ipr_cmd: ipr command struct
5918 *
5919 * This function looks through the existing resource table, comparing
5920 * it with the config table. This function will take care of old/new
5921 * devices and schedule adding/removing them from the mid-layer
5922 * as appropriate.
5923 *
5924 * Return value:
5925 * IPR_RC_JOB_CONTINUE
5926 **/
5927static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
5928{
5929 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5930 struct ipr_resource_entry *res, *temp;
5931 struct ipr_config_table_entry *cfgte;
5932 int found, i;
5933 LIST_HEAD(old_res);
5934
5935 ENTER;
5936 if (ioa_cfg->cfg_table->hdr.flags & IPR_UCODE_DOWNLOAD_REQ)
5937 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
5938
5939 list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
5940 list_move_tail(&res->queue, &old_res);
5941
5942 for (i = 0; i < ioa_cfg->cfg_table->hdr.num_entries; i++) {
5943 cfgte = &ioa_cfg->cfg_table->dev[i];
5944 found = 0;
5945
5946 list_for_each_entry_safe(res, temp, &old_res, queue) {
5947 if (!memcmp(&res->cfgte.res_addr,
5948 &cfgte->res_addr, sizeof(cfgte->res_addr))) {
5949 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5950 found = 1;
5951 break;
5952 }
5953 }
5954
5955 if (!found) {
5956 if (list_empty(&ioa_cfg->free_res_q)) {
5957 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
5958 break;
5959 }
5960
5961 found = 1;
5962 res = list_entry(ioa_cfg->free_res_q.next,
5963 struct ipr_resource_entry, queue);
5964 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5965 ipr_init_res_entry(res);
5966 res->add_to_ml = 1;
5967 }
5968
5969 if (found)
5970 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
5971 }
5972
5973 list_for_each_entry_safe(res, temp, &old_res, queue) {
5974 if (res->sdev) {
5975 res->del_from_ml = 1;
Brian King1121b792006-03-29 09:37:16 -06005976 res->cfgte.res_handle = IPR_INVALID_RES_HANDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5978 } else {
5979 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
5980 }
5981 }
5982
Brian Kingac09c342007-04-26 16:00:16 -05005983 if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
5984 ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
5985 else
5986 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987
5988 LEAVE;
5989 return IPR_RC_JOB_CONTINUE;
5990}
5991
5992/**
5993 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
5994 * @ipr_cmd: ipr command struct
5995 *
5996 * This function sends a Query IOA Configuration command
5997 * to the adapter to retrieve the IOA configuration table.
5998 *
5999 * Return value:
6000 * IPR_RC_JOB_RETURN
6001 **/
6002static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
6003{
6004 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6005 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6006 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
6007 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
Brian Kingac09c342007-04-26 16:00:16 -05006008 struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
6010 ENTER;
Brian Kingac09c342007-04-26 16:00:16 -05006011 if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
6012 ioa_cfg->dual_raid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
6014 ucode_vpd->major_release, ucode_vpd->card_type,
6015 ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
6016 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6017 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6018
6019 ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
6020 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_config_table) >> 8) & 0xff;
6021 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_config_table) & 0xff;
6022
6023 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
6024 ioarcb->read_data_transfer_length =
6025 cpu_to_be32(sizeof(struct ipr_config_table));
6026
6027 ioadl->address = cpu_to_be32(ioa_cfg->cfg_table_dma);
6028 ioadl->flags_and_data_len =
6029 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(struct ipr_config_table));
6030
6031 ipr_cmd->job_step = ipr_init_res_table;
6032
6033 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6034
6035 LEAVE;
6036 return IPR_RC_JOB_RETURN;
6037}
6038
6039/**
6040 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
6041 * @ipr_cmd: ipr command struct
6042 *
6043 * This utility function sends an inquiry to the adapter.
6044 *
6045 * Return value:
6046 * none
6047 **/
6048static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
6049 u32 dma_addr, u8 xfer_len)
6050{
6051 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6052 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
6053
6054 ENTER;
6055 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6056 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6057
6058 ioarcb->cmd_pkt.cdb[0] = INQUIRY;
6059 ioarcb->cmd_pkt.cdb[1] = flags;
6060 ioarcb->cmd_pkt.cdb[2] = page;
6061 ioarcb->cmd_pkt.cdb[4] = xfer_len;
6062
6063 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
6064 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
6065
6066 ioadl->address = cpu_to_be32(dma_addr);
6067 ioadl->flags_and_data_len =
6068 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
6069
6070 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6071 LEAVE;
6072}
6073
6074/**
brking@us.ibm.com62275042005-11-01 17:01:14 -06006075 * ipr_inquiry_page_supported - Is the given inquiry page supported
6076 * @page0: inquiry page 0 buffer
6077 * @page: page code.
6078 *
6079 * This function determines if the specified inquiry page is supported.
6080 *
6081 * Return value:
6082 * 1 if page is supported / 0 if not
6083 **/
6084static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
6085{
6086 int i;
6087
6088 for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
6089 if (page0->page[i] == page)
6090 return 1;
6091
6092 return 0;
6093}
6094
6095/**
Brian Kingac09c342007-04-26 16:00:16 -05006096 * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
6097 * @ipr_cmd: ipr command struct
6098 *
6099 * This function sends a Page 0xD0 inquiry to the adapter
6100 * to retrieve adapter capabilities.
6101 *
6102 * Return value:
6103 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6104 **/
6105static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
6106{
6107 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6108 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
6109 struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
6110
6111 ENTER;
6112 ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
6113 memset(cap, 0, sizeof(*cap));
6114
6115 if (ipr_inquiry_page_supported(page0, 0xD0)) {
6116 ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
6117 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
6118 sizeof(struct ipr_inquiry_cap));
6119 return IPR_RC_JOB_RETURN;
6120 }
6121
6122 LEAVE;
6123 return IPR_RC_JOB_CONTINUE;
6124}
6125
6126/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
6128 * @ipr_cmd: ipr command struct
6129 *
6130 * This function sends a Page 3 inquiry to the adapter
6131 * to retrieve software VPD information.
6132 *
6133 * Return value:
6134 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6135 **/
6136static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
6137{
6138 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006139 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
6140
6141 ENTER;
6142
6143 if (!ipr_inquiry_page_supported(page0, 1))
6144 ioa_cfg->cache_state = CACHE_NONE;
6145
Brian Kingac09c342007-04-26 16:00:16 -05006146 ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006147
6148 ipr_ioafp_inquiry(ipr_cmd, 1, 3,
6149 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
6150 sizeof(struct ipr_inquiry_page3));
6151
6152 LEAVE;
6153 return IPR_RC_JOB_RETURN;
6154}
6155
6156/**
6157 * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
6158 * @ipr_cmd: ipr command struct
6159 *
6160 * This function sends a Page 0 inquiry to the adapter
6161 * to retrieve supported inquiry pages.
6162 *
6163 * Return value:
6164 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6165 **/
6166static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
6167{
6168 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 char type[5];
6170
6171 ENTER;
6172
6173 /* Grab the type out of the VPD and store it away */
6174 memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
6175 type[4] = '\0';
6176 ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
6177
brking@us.ibm.com62275042005-11-01 17:01:14 -06006178 ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179
brking@us.ibm.com62275042005-11-01 17:01:14 -06006180 ipr_ioafp_inquiry(ipr_cmd, 1, 0,
6181 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
6182 sizeof(struct ipr_inquiry_page0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183
6184 LEAVE;
6185 return IPR_RC_JOB_RETURN;
6186}
6187
6188/**
6189 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
6190 * @ipr_cmd: ipr command struct
6191 *
6192 * This function sends a standard inquiry to the adapter.
6193 *
6194 * Return value:
6195 * IPR_RC_JOB_RETURN
6196 **/
6197static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
6198{
6199 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6200
6201 ENTER;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006202 ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203
6204 ipr_ioafp_inquiry(ipr_cmd, 0, 0,
6205 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
6206 sizeof(struct ipr_ioa_vpd));
6207
6208 LEAVE;
6209 return IPR_RC_JOB_RETURN;
6210}
6211
6212/**
6213 * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
6214 * @ipr_cmd: ipr command struct
6215 *
6216 * This function send an Identify Host Request Response Queue
6217 * command to establish the HRRQ with the adapter.
6218 *
6219 * Return value:
6220 * IPR_RC_JOB_RETURN
6221 **/
6222static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
6223{
6224 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6225 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6226
6227 ENTER;
6228 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
6229
6230 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
6231 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6232
6233 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6234 ioarcb->cmd_pkt.cdb[2] =
6235 ((u32) ioa_cfg->host_rrq_dma >> 24) & 0xff;
6236 ioarcb->cmd_pkt.cdb[3] =
6237 ((u32) ioa_cfg->host_rrq_dma >> 16) & 0xff;
6238 ioarcb->cmd_pkt.cdb[4] =
6239 ((u32) ioa_cfg->host_rrq_dma >> 8) & 0xff;
6240 ioarcb->cmd_pkt.cdb[5] =
6241 ((u32) ioa_cfg->host_rrq_dma) & 0xff;
6242 ioarcb->cmd_pkt.cdb[7] =
6243 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
6244 ioarcb->cmd_pkt.cdb[8] =
6245 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
6246
6247 ipr_cmd->job_step = ipr_ioafp_std_inquiry;
6248
6249 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6250
6251 LEAVE;
6252 return IPR_RC_JOB_RETURN;
6253}
6254
6255/**
6256 * ipr_reset_timer_done - Adapter reset timer function
6257 * @ipr_cmd: ipr command struct
6258 *
6259 * Description: This function is used in adapter reset processing
6260 * for timing events. If the reset_cmd pointer in the IOA
6261 * config struct is not this adapter's we are doing nested
6262 * resets and fail_all_ops will take care of freeing the
6263 * command block.
6264 *
6265 * Return value:
6266 * none
6267 **/
6268static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
6269{
6270 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6271 unsigned long lock_flags = 0;
6272
6273 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6274
6275 if (ioa_cfg->reset_cmd == ipr_cmd) {
6276 list_del(&ipr_cmd->queue);
6277 ipr_cmd->done(ipr_cmd);
6278 }
6279
6280 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6281}
6282
6283/**
6284 * ipr_reset_start_timer - Start a timer for adapter reset job
6285 * @ipr_cmd: ipr command struct
6286 * @timeout: timeout value
6287 *
6288 * Description: This function is used in adapter reset processing
6289 * for timing events. If the reset_cmd pointer in the IOA
6290 * config struct is not this adapter's we are doing nested
6291 * resets and fail_all_ops will take care of freeing the
6292 * command block.
6293 *
6294 * Return value:
6295 * none
6296 **/
6297static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
6298 unsigned long timeout)
6299{
6300 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
6301 ipr_cmd->done = ipr_reset_ioa_job;
6302
6303 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
6304 ipr_cmd->timer.expires = jiffies + timeout;
6305 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
6306 add_timer(&ipr_cmd->timer);
6307}
6308
6309/**
6310 * ipr_init_ioa_mem - Initialize ioa_cfg control block
6311 * @ioa_cfg: ioa cfg struct
6312 *
6313 * Return value:
6314 * nothing
6315 **/
6316static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
6317{
6318 memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
6319
6320 /* Initialize Host RRQ pointers */
6321 ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
6322 ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
6323 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
6324 ioa_cfg->toggle_bit = 1;
6325
6326 /* Zero out config table */
6327 memset(ioa_cfg->cfg_table, 0, sizeof(struct ipr_config_table));
6328}
6329
6330/**
6331 * ipr_reset_enable_ioa - Enable the IOA following a reset.
6332 * @ipr_cmd: ipr command struct
6333 *
6334 * This function reinitializes some control blocks and
6335 * enables destructive diagnostics on the adapter.
6336 *
6337 * Return value:
6338 * IPR_RC_JOB_RETURN
6339 **/
6340static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
6341{
6342 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6343 volatile u32 int_reg;
6344
6345 ENTER;
6346 ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
6347 ipr_init_ioa_mem(ioa_cfg);
6348
6349 ioa_cfg->allow_interrupts = 1;
6350 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
6351
6352 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
6353 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
6354 ioa_cfg->regs.clr_interrupt_mask_reg);
6355 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
6356 return IPR_RC_JOB_CONTINUE;
6357 }
6358
6359 /* Enable destructive diagnostics on IOA */
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06006360 writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361
6362 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
6363 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
6364
6365 dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
6366
6367 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
Brian King5469cb52007-03-29 12:42:40 -05006368 ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
6370 ipr_cmd->done = ipr_reset_ioa_job;
6371 add_timer(&ipr_cmd->timer);
6372 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
6373
6374 LEAVE;
6375 return IPR_RC_JOB_RETURN;
6376}
6377
6378/**
6379 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
6380 * @ipr_cmd: ipr command struct
6381 *
6382 * This function is invoked when an adapter dump has run out
6383 * of processing time.
6384 *
6385 * Return value:
6386 * IPR_RC_JOB_CONTINUE
6387 **/
6388static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
6389{
6390 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6391
6392 if (ioa_cfg->sdt_state == GET_DUMP)
6393 ioa_cfg->sdt_state = ABORT_DUMP;
6394
6395 ipr_cmd->job_step = ipr_reset_alert;
6396
6397 return IPR_RC_JOB_CONTINUE;
6398}
6399
6400/**
6401 * ipr_unit_check_no_data - Log a unit check/no data error log
6402 * @ioa_cfg: ioa config struct
6403 *
6404 * Logs an error indicating the adapter unit checked, but for some
6405 * reason, we were unable to fetch the unit check buffer.
6406 *
6407 * Return value:
6408 * nothing
6409 **/
6410static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
6411{
6412 ioa_cfg->errors_logged++;
6413 dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
6414}
6415
6416/**
6417 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
6418 * @ioa_cfg: ioa config struct
6419 *
6420 * Fetches the unit check buffer from the adapter by clocking the data
6421 * through the mailbox register.
6422 *
6423 * Return value:
6424 * nothing
6425 **/
6426static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
6427{
6428 unsigned long mailbox;
6429 struct ipr_hostrcb *hostrcb;
6430 struct ipr_uc_sdt sdt;
6431 int rc, length;
Brian King65f56472007-04-26 16:00:12 -05006432 u32 ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433
6434 mailbox = readl(ioa_cfg->ioa_mailbox);
6435
6436 if (!ipr_sdt_is_fmt2(mailbox)) {
6437 ipr_unit_check_no_data(ioa_cfg);
6438 return;
6439 }
6440
6441 memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
6442 rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
6443 (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
6444
6445 if (rc || (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE) ||
6446 !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY)) {
6447 ipr_unit_check_no_data(ioa_cfg);
6448 return;
6449 }
6450
6451 /* Find length of the first sdt entry (UC buffer) */
6452 length = (be32_to_cpu(sdt.entry[0].end_offset) -
6453 be32_to_cpu(sdt.entry[0].bar_str_offset)) & IPR_FMT2_MBX_ADDR_MASK;
6454
6455 hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
6456 struct ipr_hostrcb, queue);
6457 list_del(&hostrcb->queue);
6458 memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
6459
6460 rc = ipr_get_ldump_data_section(ioa_cfg,
6461 be32_to_cpu(sdt.entry[0].bar_str_offset),
6462 (__be32 *)&hostrcb->hcam,
6463 min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
6464
Brian King65f56472007-04-26 16:00:12 -05006465 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 ipr_handle_log_data(ioa_cfg, hostrcb);
Brian King65f56472007-04-26 16:00:12 -05006467 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
6468 if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
6469 ioa_cfg->sdt_state == GET_DUMP)
6470 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
6471 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 ipr_unit_check_no_data(ioa_cfg);
6473
6474 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
6475}
6476
6477/**
6478 * ipr_reset_restore_cfg_space - Restore PCI config space.
6479 * @ipr_cmd: ipr command struct
6480 *
6481 * Description: This function restores the saved PCI config space of
6482 * the adapter, fails all outstanding ops back to the callers, and
6483 * fetches the dump/unit check if applicable to this reset.
6484 *
6485 * Return value:
6486 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6487 **/
6488static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
6489{
6490 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6491 int rc;
6492
6493 ENTER;
6494 rc = pci_restore_state(ioa_cfg->pdev);
6495
6496 if (rc != PCIBIOS_SUCCESSFUL) {
6497 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6498 return IPR_RC_JOB_CONTINUE;
6499 }
6500
6501 if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
6502 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6503 return IPR_RC_JOB_CONTINUE;
6504 }
6505
6506 ipr_fail_all_ops(ioa_cfg);
6507
6508 if (ioa_cfg->ioa_unit_checked) {
6509 ioa_cfg->ioa_unit_checked = 0;
6510 ipr_get_unit_check_buffer(ioa_cfg);
6511 ipr_cmd->job_step = ipr_reset_alert;
6512 ipr_reset_start_timer(ipr_cmd, 0);
6513 return IPR_RC_JOB_RETURN;
6514 }
6515
6516 if (ioa_cfg->in_ioa_bringdown) {
6517 ipr_cmd->job_step = ipr_ioa_bringdown_done;
6518 } else {
6519 ipr_cmd->job_step = ipr_reset_enable_ioa;
6520
6521 if (GET_DUMP == ioa_cfg->sdt_state) {
6522 ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
6523 ipr_cmd->job_step = ipr_reset_wait_for_dump;
6524 schedule_work(&ioa_cfg->work_q);
6525 return IPR_RC_JOB_RETURN;
6526 }
6527 }
6528
6529 ENTER;
6530 return IPR_RC_JOB_CONTINUE;
6531}
6532
6533/**
Brian Kinge619e1a2007-01-23 11:25:37 -06006534 * ipr_reset_bist_done - BIST has completed on the adapter.
6535 * @ipr_cmd: ipr command struct
6536 *
6537 * Description: Unblock config space and resume the reset process.
6538 *
6539 * Return value:
6540 * IPR_RC_JOB_CONTINUE
6541 **/
6542static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
6543{
6544 ENTER;
6545 pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
6546 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
6547 LEAVE;
6548 return IPR_RC_JOB_CONTINUE;
6549}
6550
6551/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 * ipr_reset_start_bist - Run BIST on the adapter.
6553 * @ipr_cmd: ipr command struct
6554 *
6555 * Description: This function runs BIST on the adapter, then delays 2 seconds.
6556 *
6557 * Return value:
6558 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6559 **/
6560static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
6561{
6562 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6563 int rc;
6564
6565 ENTER;
Brian Kingb30197d2005-09-27 01:21:56 -07006566 pci_block_user_cfg_access(ioa_cfg->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
6568
6569 if (rc != PCIBIOS_SUCCESSFUL) {
Brian Kinga9aedb02007-03-29 12:43:23 -05006570 pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6572 rc = IPR_RC_JOB_CONTINUE;
6573 } else {
Brian Kinge619e1a2007-01-23 11:25:37 -06006574 ipr_cmd->job_step = ipr_reset_bist_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
6576 rc = IPR_RC_JOB_RETURN;
6577 }
6578
6579 LEAVE;
6580 return rc;
6581}
6582
6583/**
Brian King463fc692007-05-07 17:09:05 -05006584 * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
6585 * @ipr_cmd: ipr command struct
6586 *
6587 * Description: This clears PCI reset to the adapter and delays two seconds.
6588 *
6589 * Return value:
6590 * IPR_RC_JOB_RETURN
6591 **/
6592static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
6593{
6594 ENTER;
6595 pci_set_pcie_reset_state(ipr_cmd->ioa_cfg->pdev, pcie_deassert_reset);
6596 ipr_cmd->job_step = ipr_reset_bist_done;
6597 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
6598 LEAVE;
6599 return IPR_RC_JOB_RETURN;
6600}
6601
6602/**
6603 * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
6604 * @ipr_cmd: ipr command struct
6605 *
6606 * Description: This asserts PCI reset to the adapter.
6607 *
6608 * Return value:
6609 * IPR_RC_JOB_RETURN
6610 **/
6611static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
6612{
6613 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6614 struct pci_dev *pdev = ioa_cfg->pdev;
6615
6616 ENTER;
6617 pci_block_user_cfg_access(pdev);
6618 pci_set_pcie_reset_state(pdev, pcie_warm_reset);
6619 ipr_cmd->job_step = ipr_reset_slot_reset_done;
6620 ipr_reset_start_timer(ipr_cmd, IPR_PCI_RESET_TIMEOUT);
6621 LEAVE;
6622 return IPR_RC_JOB_RETURN;
6623}
6624
6625/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 * ipr_reset_allowed - Query whether or not IOA can be reset
6627 * @ioa_cfg: ioa config struct
6628 *
6629 * Return value:
6630 * 0 if reset not allowed / non-zero if reset is allowed
6631 **/
6632static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
6633{
6634 volatile u32 temp_reg;
6635
6636 temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
6637 return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
6638}
6639
6640/**
6641 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
6642 * @ipr_cmd: ipr command struct
6643 *
6644 * Description: This function waits for adapter permission to run BIST,
6645 * then runs BIST. If the adapter does not give permission after a
6646 * reasonable time, we will reset the adapter anyway. The impact of
6647 * resetting the adapter without warning the adapter is the risk of
6648 * losing the persistent error log on the adapter. If the adapter is
6649 * reset while it is writing to the flash on the adapter, the flash
6650 * segment will have bad ECC and be zeroed.
6651 *
6652 * Return value:
6653 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6654 **/
6655static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
6656{
6657 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6658 int rc = IPR_RC_JOB_RETURN;
6659
6660 if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
6661 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
6662 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
6663 } else {
Brian King463fc692007-05-07 17:09:05 -05006664 ipr_cmd->job_step = ioa_cfg->reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 rc = IPR_RC_JOB_CONTINUE;
6666 }
6667
6668 return rc;
6669}
6670
6671/**
6672 * ipr_reset_alert_part2 - Alert the adapter of a pending reset
6673 * @ipr_cmd: ipr command struct
6674 *
6675 * Description: This function alerts the adapter that it will be reset.
6676 * If memory space is not currently enabled, proceed directly
6677 * to running BIST on the adapter. The timer must always be started
6678 * so we guarantee we do not run BIST from ipr_isr.
6679 *
6680 * Return value:
6681 * IPR_RC_JOB_RETURN
6682 **/
6683static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
6684{
6685 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6686 u16 cmd_reg;
6687 int rc;
6688
6689 ENTER;
6690 rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
6691
6692 if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
6693 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
6694 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg);
6695 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
6696 } else {
Brian King463fc692007-05-07 17:09:05 -05006697 ipr_cmd->job_step = ioa_cfg->reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 }
6699
6700 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
6701 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
6702
6703 LEAVE;
6704 return IPR_RC_JOB_RETURN;
6705}
6706
6707/**
6708 * ipr_reset_ucode_download_done - Microcode download completion
6709 * @ipr_cmd: ipr command struct
6710 *
6711 * Description: This function unmaps the microcode download buffer.
6712 *
6713 * Return value:
6714 * IPR_RC_JOB_CONTINUE
6715 **/
6716static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
6717{
6718 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6719 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
6720
6721 pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
6722 sglist->num_sg, DMA_TO_DEVICE);
6723
6724 ipr_cmd->job_step = ipr_reset_alert;
6725 return IPR_RC_JOB_CONTINUE;
6726}
6727
6728/**
6729 * ipr_reset_ucode_download - Download microcode to the adapter
6730 * @ipr_cmd: ipr command struct
6731 *
6732 * Description: This function checks to see if it there is microcode
6733 * to download to the adapter. If there is, a download is performed.
6734 *
6735 * Return value:
6736 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6737 **/
6738static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
6739{
6740 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6741 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
6742
6743 ENTER;
6744 ipr_cmd->job_step = ipr_reset_alert;
6745
6746 if (!sglist)
6747 return IPR_RC_JOB_CONTINUE;
6748
6749 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6750 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6751 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
6752 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
6753 ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
6754 ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
6755 ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
6756
brking@us.ibm.com12baa422005-11-01 17:01:27 -06006757 ipr_build_ucode_ioadl(ipr_cmd, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 ipr_cmd->job_step = ipr_reset_ucode_download_done;
6759
6760 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
6761 IPR_WRITE_BUFFER_TIMEOUT);
6762
6763 LEAVE;
6764 return IPR_RC_JOB_RETURN;
6765}
6766
6767/**
6768 * ipr_reset_shutdown_ioa - Shutdown the adapter
6769 * @ipr_cmd: ipr command struct
6770 *
6771 * Description: This function issues an adapter shutdown of the
6772 * specified type to the specified adapter as part of the
6773 * adapter reset job.
6774 *
6775 * Return value:
6776 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6777 **/
6778static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
6779{
6780 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6781 enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
6782 unsigned long timeout;
6783 int rc = IPR_RC_JOB_CONTINUE;
6784
6785 ENTER;
6786 if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
6787 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6788 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6789 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
6790 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
6791
Brian Kingac09c342007-04-26 16:00:16 -05006792 if (shutdown_type == IPR_SHUTDOWN_NORMAL)
6793 timeout = IPR_SHUTDOWN_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
6795 timeout = IPR_INTERNAL_TIMEOUT;
Brian Kingac09c342007-04-26 16:00:16 -05006796 else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
6797 timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 else
Brian Kingac09c342007-04-26 16:00:16 -05006799 timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
6801 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
6802
6803 rc = IPR_RC_JOB_RETURN;
6804 ipr_cmd->job_step = ipr_reset_ucode_download;
6805 } else
6806 ipr_cmd->job_step = ipr_reset_alert;
6807
6808 LEAVE;
6809 return rc;
6810}
6811
6812/**
6813 * ipr_reset_ioa_job - Adapter reset job
6814 * @ipr_cmd: ipr command struct
6815 *
6816 * Description: This function is the job router for the adapter reset job.
6817 *
6818 * Return value:
6819 * none
6820 **/
6821static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
6822{
6823 u32 rc, ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6825
6826 do {
6827 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
6828
6829 if (ioa_cfg->reset_cmd != ipr_cmd) {
6830 /*
6831 * We are doing nested adapter resets and this is
6832 * not the current reset job.
6833 */
6834 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6835 return;
6836 }
6837
6838 if (IPR_IOASC_SENSE_KEY(ioasc)) {
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06006839 rc = ipr_cmd->job_step_failed(ipr_cmd);
6840 if (rc == IPR_RC_JOB_RETURN)
6841 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 }
6843
6844 ipr_reinit_ipr_cmnd(ipr_cmd);
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06006845 ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 rc = ipr_cmd->job_step(ipr_cmd);
6847 } while(rc == IPR_RC_JOB_CONTINUE);
6848}
6849
6850/**
6851 * _ipr_initiate_ioa_reset - Initiate an adapter reset
6852 * @ioa_cfg: ioa config struct
6853 * @job_step: first job step of reset job
6854 * @shutdown_type: shutdown type
6855 *
6856 * Description: This function will initiate the reset of the given adapter
6857 * starting at the selected job step.
6858 * If the caller needs to wait on the completion of the reset,
6859 * the caller must sleep on the reset_wait_q.
6860 *
6861 * Return value:
6862 * none
6863 **/
6864static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
6865 int (*job_step) (struct ipr_cmnd *),
6866 enum ipr_shutdown_type shutdown_type)
6867{
6868 struct ipr_cmnd *ipr_cmd;
6869
6870 ioa_cfg->in_reset_reload = 1;
6871 ioa_cfg->allow_cmds = 0;
6872 scsi_block_requests(ioa_cfg->host);
6873
6874 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
6875 ioa_cfg->reset_cmd = ipr_cmd;
6876 ipr_cmd->job_step = job_step;
6877 ipr_cmd->u.shutdown_type = shutdown_type;
6878
6879 ipr_reset_ioa_job(ipr_cmd);
6880}
6881
6882/**
6883 * ipr_initiate_ioa_reset - Initiate an adapter reset
6884 * @ioa_cfg: ioa config struct
6885 * @shutdown_type: shutdown type
6886 *
6887 * Description: This function will initiate the reset of the given adapter.
6888 * If the caller needs to wait on the completion of the reset,
6889 * the caller must sleep on the reset_wait_q.
6890 *
6891 * Return value:
6892 * none
6893 **/
6894static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
6895 enum ipr_shutdown_type shutdown_type)
6896{
6897 if (ioa_cfg->ioa_is_dead)
6898 return;
6899
6900 if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
6901 ioa_cfg->sdt_state = ABORT_DUMP;
6902
6903 if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
6904 dev_err(&ioa_cfg->pdev->dev,
6905 "IOA taken offline - error recovery failed\n");
6906
6907 ioa_cfg->reset_retries = 0;
6908 ioa_cfg->ioa_is_dead = 1;
6909
6910 if (ioa_cfg->in_ioa_bringdown) {
6911 ioa_cfg->reset_cmd = NULL;
6912 ioa_cfg->in_reset_reload = 0;
6913 ipr_fail_all_ops(ioa_cfg);
6914 wake_up_all(&ioa_cfg->reset_wait_q);
6915
6916 spin_unlock_irq(ioa_cfg->host->host_lock);
6917 scsi_unblock_requests(ioa_cfg->host);
6918 spin_lock_irq(ioa_cfg->host->host_lock);
6919 return;
6920 } else {
6921 ioa_cfg->in_ioa_bringdown = 1;
6922 shutdown_type = IPR_SHUTDOWN_NONE;
6923 }
6924 }
6925
6926 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
6927 shutdown_type);
6928}
6929
6930/**
Linas Vepstasf8a88b192006-02-03 16:52:42 -06006931 * ipr_reset_freeze - Hold off all I/O activity
6932 * @ipr_cmd: ipr command struct
6933 *
6934 * Description: If the PCI slot is frozen, hold off all I/O
6935 * activity; then, as soon as the slot is available again,
6936 * initiate an adapter reset.
6937 */
6938static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
6939{
6940 /* Disallow new interrupts, avoid loop */
6941 ipr_cmd->ioa_cfg->allow_interrupts = 0;
6942 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
6943 ipr_cmd->done = ipr_reset_ioa_job;
6944 return IPR_RC_JOB_RETURN;
6945}
6946
6947/**
6948 * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
6949 * @pdev: PCI device struct
6950 *
6951 * Description: This routine is called to tell us that the PCI bus
6952 * is down. Can't do anything here, except put the device driver
6953 * into a holding pattern, waiting for the PCI bus to come back.
6954 */
6955static void ipr_pci_frozen(struct pci_dev *pdev)
6956{
6957 unsigned long flags = 0;
6958 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6959
6960 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6961 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
6962 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6963}
6964
6965/**
6966 * ipr_pci_slot_reset - Called when PCI slot has been reset.
6967 * @pdev: PCI device struct
6968 *
6969 * Description: This routine is called by the pci error recovery
6970 * code after the PCI slot has been reset, just before we
6971 * should resume normal operations.
6972 */
6973static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
6974{
6975 unsigned long flags = 0;
6976 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6977
6978 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
Brian King463fc692007-05-07 17:09:05 -05006979 if (ioa_cfg->needs_warm_reset)
6980 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
6981 else
6982 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
6983 IPR_SHUTDOWN_NONE);
Linas Vepstasf8a88b192006-02-03 16:52:42 -06006984 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6985 return PCI_ERS_RESULT_RECOVERED;
6986}
6987
6988/**
6989 * ipr_pci_perm_failure - Called when PCI slot is dead for good.
6990 * @pdev: PCI device struct
6991 *
6992 * Description: This routine is called when the PCI bus has
6993 * permanently failed.
6994 */
6995static void ipr_pci_perm_failure(struct pci_dev *pdev)
6996{
6997 unsigned long flags = 0;
6998 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6999
7000 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
7001 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
7002 ioa_cfg->sdt_state = ABORT_DUMP;
7003 ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES;
7004 ioa_cfg->in_ioa_bringdown = 1;
7005 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7006 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
7007}
7008
7009/**
7010 * ipr_pci_error_detected - Called when a PCI error is detected.
7011 * @pdev: PCI device struct
7012 * @state: PCI channel state
7013 *
7014 * Description: Called when a PCI error is detected.
7015 *
7016 * Return value:
7017 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7018 */
7019static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
7020 pci_channel_state_t state)
7021{
7022 switch (state) {
7023 case pci_channel_io_frozen:
7024 ipr_pci_frozen(pdev);
7025 return PCI_ERS_RESULT_NEED_RESET;
7026 case pci_channel_io_perm_failure:
7027 ipr_pci_perm_failure(pdev);
7028 return PCI_ERS_RESULT_DISCONNECT;
7029 break;
7030 default:
7031 break;
7032 }
7033 return PCI_ERS_RESULT_NEED_RESET;
7034}
7035
7036/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
7038 * @ioa_cfg: ioa cfg struct
7039 *
7040 * Description: This is the second phase of adapter intialization
7041 * This function takes care of initilizing the adapter to the point
7042 * where it can accept new commands.
7043
7044 * Return value:
Joe Perchesb1c11812008-02-03 17:28:22 +02007045 * 0 on success / -EIO on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 **/
7047static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
7048{
7049 int rc = 0;
7050 unsigned long host_lock_flags = 0;
7051
7052 ENTER;
7053 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7054 dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007055 if (ioa_cfg->needs_hard_reset) {
7056 ioa_cfg->needs_hard_reset = 0;
7057 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7058 } else
7059 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
7060 IPR_SHUTDOWN_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
7062 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7063 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7064 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7065
7066 if (ioa_cfg->ioa_is_dead) {
7067 rc = -EIO;
7068 } else if (ipr_invalid_adapter(ioa_cfg)) {
7069 if (!ipr_testmode)
7070 rc = -EIO;
7071
7072 dev_err(&ioa_cfg->pdev->dev,
7073 "Adapter not supported in this hardware configuration.\n");
7074 }
7075
7076 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7077
7078 LEAVE;
7079 return rc;
7080}
7081
7082/**
7083 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
7084 * @ioa_cfg: ioa config struct
7085 *
7086 * Return value:
7087 * none
7088 **/
7089static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
7090{
7091 int i;
7092
7093 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
7094 if (ioa_cfg->ipr_cmnd_list[i])
7095 pci_pool_free(ioa_cfg->ipr_cmd_pool,
7096 ioa_cfg->ipr_cmnd_list[i],
7097 ioa_cfg->ipr_cmnd_list_dma[i]);
7098
7099 ioa_cfg->ipr_cmnd_list[i] = NULL;
7100 }
7101
7102 if (ioa_cfg->ipr_cmd_pool)
7103 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
7104
7105 ioa_cfg->ipr_cmd_pool = NULL;
7106}
7107
7108/**
7109 * ipr_free_mem - Frees memory allocated for an adapter
7110 * @ioa_cfg: ioa cfg struct
7111 *
7112 * Return value:
7113 * nothing
7114 **/
7115static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
7116{
7117 int i;
7118
7119 kfree(ioa_cfg->res_entries);
7120 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
7121 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
7122 ipr_free_cmd_blks(ioa_cfg);
7123 pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
7124 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
7125 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_config_table),
7126 ioa_cfg->cfg_table,
7127 ioa_cfg->cfg_table_dma);
7128
7129 for (i = 0; i < IPR_NUM_HCAMS; i++) {
7130 pci_free_consistent(ioa_cfg->pdev,
7131 sizeof(struct ipr_hostrcb),
7132 ioa_cfg->hostrcb[i],
7133 ioa_cfg->hostrcb_dma[i]);
7134 }
7135
7136 ipr_free_dump(ioa_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 kfree(ioa_cfg->trace);
7138}
7139
7140/**
7141 * ipr_free_all_resources - Free all allocated resources for an adapter.
7142 * @ipr_cmd: ipr command struct
7143 *
7144 * This function frees all allocated resources for the
7145 * specified adapter.
7146 *
7147 * Return value:
7148 * none
7149 **/
7150static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
7151{
7152 struct pci_dev *pdev = ioa_cfg->pdev;
7153
7154 ENTER;
7155 free_irq(pdev->irq, ioa_cfg);
7156 iounmap(ioa_cfg->hdw_dma_regs);
7157 pci_release_regions(pdev);
7158 ipr_free_mem(ioa_cfg);
7159 scsi_host_put(ioa_cfg->host);
7160 pci_disable_device(pdev);
7161 LEAVE;
7162}
7163
7164/**
7165 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
7166 * @ioa_cfg: ioa config struct
7167 *
7168 * Return value:
7169 * 0 on success / -ENOMEM on allocation failure
7170 **/
7171static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
7172{
7173 struct ipr_cmnd *ipr_cmd;
7174 struct ipr_ioarcb *ioarcb;
7175 dma_addr_t dma_addr;
7176 int i;
7177
7178 ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
7179 sizeof(struct ipr_cmnd), 8, 0);
7180
7181 if (!ioa_cfg->ipr_cmd_pool)
7182 return -ENOMEM;
7183
7184 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
Christoph Lametere94b1762006-12-06 20:33:17 -08007185 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186
7187 if (!ipr_cmd) {
7188 ipr_free_cmd_blks(ioa_cfg);
7189 return -ENOMEM;
7190 }
7191
7192 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
7193 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
7194 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
7195
7196 ioarcb = &ipr_cmd->ioarcb;
7197 ioarcb->ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
7198 ioarcb->host_response_handle = cpu_to_be32(i << 2);
7199 ioarcb->write_ioadl_addr =
7200 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
7201 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
7202 ioarcb->ioasa_host_pci_addr =
7203 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
7204 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
7205 ipr_cmd->cmd_index = i;
7206 ipr_cmd->ioa_cfg = ioa_cfg;
7207 ipr_cmd->sense_buffer_dma = dma_addr +
7208 offsetof(struct ipr_cmnd, sense_buffer);
7209
7210 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
7211 }
7212
7213 return 0;
7214}
7215
7216/**
7217 * ipr_alloc_mem - Allocate memory for an adapter
7218 * @ioa_cfg: ioa config struct
7219 *
7220 * Return value:
7221 * 0 on success / non-zero for error
7222 **/
7223static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
7224{
7225 struct pci_dev *pdev = ioa_cfg->pdev;
7226 int i, rc = -ENOMEM;
7227
7228 ENTER;
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06007229 ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 IPR_MAX_PHYSICAL_DEVS, GFP_KERNEL);
7231
7232 if (!ioa_cfg->res_entries)
7233 goto out;
7234
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 for (i = 0; i < IPR_MAX_PHYSICAL_DEVS; i++)
7236 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
7237
7238 ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
7239 sizeof(struct ipr_misc_cbs),
7240 &ioa_cfg->vpd_cbs_dma);
7241
7242 if (!ioa_cfg->vpd_cbs)
7243 goto out_free_res_entries;
7244
7245 if (ipr_alloc_cmd_blks(ioa_cfg))
7246 goto out_free_vpd_cbs;
7247
7248 ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
7249 sizeof(u32) * IPR_NUM_CMD_BLKS,
7250 &ioa_cfg->host_rrq_dma);
7251
7252 if (!ioa_cfg->host_rrq)
7253 goto out_ipr_free_cmd_blocks;
7254
7255 ioa_cfg->cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
7256 sizeof(struct ipr_config_table),
7257 &ioa_cfg->cfg_table_dma);
7258
7259 if (!ioa_cfg->cfg_table)
7260 goto out_free_host_rrq;
7261
7262 for (i = 0; i < IPR_NUM_HCAMS; i++) {
7263 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
7264 sizeof(struct ipr_hostrcb),
7265 &ioa_cfg->hostrcb_dma[i]);
7266
7267 if (!ioa_cfg->hostrcb[i])
7268 goto out_free_hostrcb_dma;
7269
7270 ioa_cfg->hostrcb[i]->hostrcb_dma =
7271 ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
Brian King49dc6a12006-11-21 10:28:35 -06007272 ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
7274 }
7275
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06007276 ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
7278
7279 if (!ioa_cfg->trace)
7280 goto out_free_hostrcb_dma;
7281
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 rc = 0;
7283out:
7284 LEAVE;
7285 return rc;
7286
7287out_free_hostrcb_dma:
7288 while (i-- > 0) {
7289 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
7290 ioa_cfg->hostrcb[i],
7291 ioa_cfg->hostrcb_dma[i]);
7292 }
7293 pci_free_consistent(pdev, sizeof(struct ipr_config_table),
7294 ioa_cfg->cfg_table, ioa_cfg->cfg_table_dma);
7295out_free_host_rrq:
7296 pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
7297 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
7298out_ipr_free_cmd_blocks:
7299 ipr_free_cmd_blks(ioa_cfg);
7300out_free_vpd_cbs:
7301 pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
7302 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
7303out_free_res_entries:
7304 kfree(ioa_cfg->res_entries);
7305 goto out;
7306}
7307
7308/**
7309 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
7310 * @ioa_cfg: ioa config struct
7311 *
7312 * Return value:
7313 * none
7314 **/
7315static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
7316{
7317 int i;
7318
7319 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
7320 ioa_cfg->bus_attr[i].bus = i;
7321 ioa_cfg->bus_attr[i].qas_enabled = 0;
7322 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
7323 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
7324 ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
7325 else
7326 ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
7327 }
7328}
7329
7330/**
7331 * ipr_init_ioa_cfg - Initialize IOA config struct
7332 * @ioa_cfg: ioa config struct
7333 * @host: scsi host struct
7334 * @pdev: PCI dev struct
7335 *
7336 * Return value:
7337 * none
7338 **/
7339static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
7340 struct Scsi_Host *host, struct pci_dev *pdev)
7341{
7342 const struct ipr_interrupt_offsets *p;
7343 struct ipr_interrupts *t;
7344 void __iomem *base;
7345
7346 ioa_cfg->host = host;
7347 ioa_cfg->pdev = pdev;
7348 ioa_cfg->log_level = ipr_log_level;
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06007349 ioa_cfg->doorbell = IPR_DOORBELL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
7351 sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
7352 sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
7353 sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
7354 sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
7355 sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
7356 sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
7357 sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
7358
7359 INIT_LIST_HEAD(&ioa_cfg->free_q);
7360 INIT_LIST_HEAD(&ioa_cfg->pending_q);
7361 INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
7362 INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
7363 INIT_LIST_HEAD(&ioa_cfg->free_res_q);
7364 INIT_LIST_HEAD(&ioa_cfg->used_res_q);
David Howellsc4028952006-11-22 14:57:56 +00007365 INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 init_waitqueue_head(&ioa_cfg->reset_wait_q);
7367 ioa_cfg->sdt_state = INACTIVE;
brking@us.ibm.com62275042005-11-01 17:01:14 -06007368 if (ipr_enable_cache)
7369 ioa_cfg->cache_state = CACHE_ENABLED;
7370 else
7371 ioa_cfg->cache_state = CACHE_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372
7373 ipr_initialize_bus_attr(ioa_cfg);
7374
7375 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
7376 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
7377 host->max_channel = IPR_MAX_BUS_TO_SCAN;
7378 host->unique_id = host->host_no;
7379 host->max_cmd_len = IPR_MAX_CDB_LEN;
7380 pci_set_drvdata(pdev, ioa_cfg);
7381
7382 p = &ioa_cfg->chip_cfg->regs;
7383 t = &ioa_cfg->regs;
7384 base = ioa_cfg->hdw_dma_regs;
7385
7386 t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
7387 t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
7388 t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
7389 t->clr_interrupt_reg = base + p->clr_interrupt_reg;
7390 t->sense_interrupt_reg = base + p->sense_interrupt_reg;
7391 t->ioarrin_reg = base + p->ioarrin_reg;
7392 t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
7393 t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
7394 t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
7395}
7396
7397/**
7398 * ipr_get_chip_cfg - Find adapter chip configuration
7399 * @dev_id: PCI device id struct
7400 *
7401 * Return value:
7402 * ptr to chip config on success / NULL on failure
7403 **/
7404static const struct ipr_chip_cfg_t * __devinit
7405ipr_get_chip_cfg(const struct pci_device_id *dev_id)
7406{
7407 int i;
7408
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
7410 if (ipr_chip[i].vendor == dev_id->vendor &&
7411 ipr_chip[i].device == dev_id->device)
7412 return ipr_chip[i].cfg;
7413 return NULL;
7414}
7415
7416/**
7417 * ipr_probe_ioa - Allocates memory and does first stage of initialization
7418 * @pdev: PCI device struct
7419 * @dev_id: PCI device id struct
7420 *
7421 * Return value:
7422 * 0 on success / non-zero on failure
7423 **/
7424static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
7425 const struct pci_device_id *dev_id)
7426{
7427 struct ipr_ioa_cfg *ioa_cfg;
7428 struct Scsi_Host *host;
7429 unsigned long ipr_regs_pci;
7430 void __iomem *ipr_regs;
Eric Sesterhenna2a65a32006-09-25 16:59:07 -07007431 int rc = PCIBIOS_SUCCESSFUL;
Brian King473b1e82007-05-02 10:44:11 -05007432 volatile u32 mask, uproc, interrupts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433
7434 ENTER;
7435
7436 if ((rc = pci_enable_device(pdev))) {
7437 dev_err(&pdev->dev, "Cannot enable adapter\n");
7438 goto out;
7439 }
7440
7441 dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
7442
7443 host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
7444
7445 if (!host) {
7446 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
7447 rc = -ENOMEM;
7448 goto out_disable;
7449 }
7450
7451 ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
7452 memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
Brian King35a39692006-09-25 12:39:20 -05007453 ata_host_init(&ioa_cfg->ata_host, &pdev->dev,
7454 sata_port_info.flags, &ipr_sata_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455
7456 ioa_cfg->chip_cfg = ipr_get_chip_cfg(dev_id);
7457
7458 if (!ioa_cfg->chip_cfg) {
7459 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
7460 dev_id->vendor, dev_id->device);
7461 goto out_scsi_host_put;
7462 }
7463
Brian King5469cb52007-03-29 12:42:40 -05007464 if (ipr_transop_timeout)
7465 ioa_cfg->transop_timeout = ipr_transop_timeout;
7466 else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
7467 ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
7468 else
7469 ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
7470
Auke Kok44c10132007-06-08 15:46:36 -07007471 ioa_cfg->revid = pdev->revision;
Brian King463fc692007-05-07 17:09:05 -05007472
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 ipr_regs_pci = pci_resource_start(pdev, 0);
7474
7475 rc = pci_request_regions(pdev, IPR_NAME);
7476 if (rc < 0) {
7477 dev_err(&pdev->dev,
7478 "Couldn't register memory range of registers\n");
7479 goto out_scsi_host_put;
7480 }
7481
7482 ipr_regs = ioremap(ipr_regs_pci, pci_resource_len(pdev, 0));
7483
7484 if (!ipr_regs) {
7485 dev_err(&pdev->dev,
7486 "Couldn't map memory range of registers\n");
7487 rc = -ENOMEM;
7488 goto out_release_regions;
7489 }
7490
7491 ioa_cfg->hdw_dma_regs = ipr_regs;
7492 ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
7493 ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
7494
7495 ipr_init_ioa_cfg(ioa_cfg, host, pdev);
7496
7497 pci_set_master(pdev);
7498
7499 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7500 if (rc < 0) {
7501 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
7502 goto cleanup_nomem;
7503 }
7504
7505 rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
7506 ioa_cfg->chip_cfg->cache_line_size);
7507
7508 if (rc != PCIBIOS_SUCCESSFUL) {
7509 dev_err(&pdev->dev, "Write of cache line size failed\n");
7510 rc = -EIO;
7511 goto cleanup_nomem;
7512 }
7513
7514 /* Save away PCI config space for use following IOA reset */
7515 rc = pci_save_state(pdev);
7516
7517 if (rc != PCIBIOS_SUCCESSFUL) {
7518 dev_err(&pdev->dev, "Failed to save PCI config space\n");
7519 rc = -EIO;
7520 goto cleanup_nomem;
7521 }
7522
7523 if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
7524 goto cleanup_nomem;
7525
7526 if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
7527 goto cleanup_nomem;
7528
7529 rc = ipr_alloc_mem(ioa_cfg);
7530 if (rc < 0) {
7531 dev_err(&pdev->dev,
7532 "Couldn't allocate enough memory for device driver!\n");
7533 goto cleanup_nomem;
7534 }
7535
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007536 /*
7537 * If HRRQ updated interrupt is not masked, or reset alert is set,
7538 * the card is in an unknown state and needs a hard reset
7539 */
7540 mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
Brian King473b1e82007-05-02 10:44:11 -05007541 interrupts = readl(ioa_cfg->regs.sense_interrupt_reg);
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007542 uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
7543 if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
7544 ioa_cfg->needs_hard_reset = 1;
Brian King473b1e82007-05-02 10:44:11 -05007545 if (interrupts & IPR_PCII_ERROR_INTERRUPTS)
7546 ioa_cfg->needs_hard_reset = 1;
7547 if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
7548 ioa_cfg->ioa_unit_checked = 1;
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007549
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07007551 rc = request_irq(pdev->irq, ipr_isr, IRQF_SHARED, IPR_NAME, ioa_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552
7553 if (rc) {
7554 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
7555 pdev->irq, rc);
7556 goto cleanup_nolog;
7557 }
7558
Brian King463fc692007-05-07 17:09:05 -05007559 if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
7560 (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
7561 ioa_cfg->needs_warm_reset = 1;
7562 ioa_cfg->reset = ipr_reset_slot_reset;
7563 } else
7564 ioa_cfg->reset = ipr_reset_start_bist;
7565
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 spin_lock(&ipr_driver_lock);
7567 list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
7568 spin_unlock(&ipr_driver_lock);
7569
7570 LEAVE;
7571out:
7572 return rc;
7573
7574cleanup_nolog:
7575 ipr_free_mem(ioa_cfg);
7576cleanup_nomem:
7577 iounmap(ipr_regs);
7578out_release_regions:
7579 pci_release_regions(pdev);
7580out_scsi_host_put:
7581 scsi_host_put(host);
7582out_disable:
7583 pci_disable_device(pdev);
7584 goto out;
7585}
7586
7587/**
7588 * ipr_scan_vsets - Scans for VSET devices
7589 * @ioa_cfg: ioa config struct
7590 *
7591 * Description: Since the VSET resources do not follow SAM in that we can have
7592 * sparse LUNs with no LUN 0, we have to scan for these ourselves.
7593 *
7594 * Return value:
7595 * none
7596 **/
7597static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
7598{
7599 int target, lun;
7600
7601 for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
7602 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
7603 scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
7604}
7605
7606/**
7607 * ipr_initiate_ioa_bringdown - Bring down an adapter
7608 * @ioa_cfg: ioa config struct
7609 * @shutdown_type: shutdown type
7610 *
7611 * Description: This function will initiate bringing down the adapter.
7612 * This consists of issuing an IOA shutdown to the adapter
7613 * to flush the cache, and running BIST.
7614 * If the caller needs to wait on the completion of the reset,
7615 * the caller must sleep on the reset_wait_q.
7616 *
7617 * Return value:
7618 * none
7619 **/
7620static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
7621 enum ipr_shutdown_type shutdown_type)
7622{
7623 ENTER;
7624 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
7625 ioa_cfg->sdt_state = ABORT_DUMP;
7626 ioa_cfg->reset_retries = 0;
7627 ioa_cfg->in_ioa_bringdown = 1;
7628 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
7629 LEAVE;
7630}
7631
7632/**
7633 * __ipr_remove - Remove a single adapter
7634 * @pdev: pci device struct
7635 *
7636 * Adapter hot plug remove entry point.
7637 *
7638 * Return value:
7639 * none
7640 **/
7641static void __ipr_remove(struct pci_dev *pdev)
7642{
7643 unsigned long host_lock_flags = 0;
7644 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7645 ENTER;
7646
7647 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
Brian King970ea292007-04-26 16:00:06 -05007648 while(ioa_cfg->in_reset_reload) {
7649 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7650 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7651 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7652 }
7653
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
7655
7656 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7657 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
brking@us.ibm.com 5cbf5ea2005-05-02 19:50:47 -05007658 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7660
7661 spin_lock(&ipr_driver_lock);
7662 list_del(&ioa_cfg->queue);
7663 spin_unlock(&ipr_driver_lock);
7664
7665 if (ioa_cfg->sdt_state == ABORT_DUMP)
7666 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7667 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7668
7669 ipr_free_all_resources(ioa_cfg);
7670
7671 LEAVE;
7672}
7673
7674/**
7675 * ipr_remove - IOA hot plug remove entry point
7676 * @pdev: pci device struct
7677 *
7678 * Adapter hot plug remove entry point.
7679 *
7680 * Return value:
7681 * none
7682 **/
7683static void ipr_remove(struct pci_dev *pdev)
7684{
7685 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7686
7687 ENTER;
7688
Tony Jonesee959b02008-02-22 00:13:36 +01007689 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 &ipr_trace_attr);
Tony Jonesee959b02008-02-22 00:13:36 +01007691 ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 &ipr_dump_attr);
7693 scsi_remove_host(ioa_cfg->host);
7694
7695 __ipr_remove(pdev);
7696
7697 LEAVE;
7698}
7699
7700/**
7701 * ipr_probe - Adapter hot plug add entry point
7702 *
7703 * Return value:
7704 * 0 on success / non-zero on failure
7705 **/
7706static int __devinit ipr_probe(struct pci_dev *pdev,
7707 const struct pci_device_id *dev_id)
7708{
7709 struct ipr_ioa_cfg *ioa_cfg;
7710 int rc;
7711
7712 rc = ipr_probe_ioa(pdev, dev_id);
7713
7714 if (rc)
7715 return rc;
7716
7717 ioa_cfg = pci_get_drvdata(pdev);
7718 rc = ipr_probe_ioa_part2(ioa_cfg);
7719
7720 if (rc) {
7721 __ipr_remove(pdev);
7722 return rc;
7723 }
7724
7725 rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
7726
7727 if (rc) {
7728 __ipr_remove(pdev);
7729 return rc;
7730 }
7731
Tony Jonesee959b02008-02-22 00:13:36 +01007732 rc = ipr_create_trace_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 &ipr_trace_attr);
7734
7735 if (rc) {
7736 scsi_remove_host(ioa_cfg->host);
7737 __ipr_remove(pdev);
7738 return rc;
7739 }
7740
Tony Jonesee959b02008-02-22 00:13:36 +01007741 rc = ipr_create_dump_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742 &ipr_dump_attr);
7743
7744 if (rc) {
Tony Jonesee959b02008-02-22 00:13:36 +01007745 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746 &ipr_trace_attr);
7747 scsi_remove_host(ioa_cfg->host);
7748 __ipr_remove(pdev);
7749 return rc;
7750 }
7751
7752 scsi_scan_host(ioa_cfg->host);
7753 ipr_scan_vsets(ioa_cfg);
7754 scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
7755 ioa_cfg->allow_ml_add_del = 1;
brking@us.ibm.com11cd8f12005-11-01 17:00:11 -06007756 ioa_cfg->host->max_channel = IPR_VSET_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757 schedule_work(&ioa_cfg->work_q);
7758 return 0;
7759}
7760
7761/**
7762 * ipr_shutdown - Shutdown handler.
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007763 * @pdev: pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764 *
7765 * This function is invoked upon system shutdown/reboot. It will issue
7766 * an adapter shutdown to the adapter to flush the write cache.
7767 *
7768 * Return value:
7769 * none
7770 **/
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007771static void ipr_shutdown(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772{
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007773 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 unsigned long lock_flags = 0;
7775
7776 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05007777 while(ioa_cfg->in_reset_reload) {
7778 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7779 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7780 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7781 }
7782
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
7784 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7785 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7786}
7787
7788static struct pci_device_id ipr_pci_table[] __devinitdata = {
7789 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007790 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007792 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007794 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007796 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06007798 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06007800 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06007802 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007803 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King5469cb52007-03-29 12:42:40 -05007804 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
7805 IPR_USE_LONG_TRANSOP_TIMEOUT },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007806 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King6d84c942007-01-23 11:25:23 -06007807 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007808 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King22d2e402007-04-26 16:00:13 -05007809 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
7810 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06007811 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King5469cb52007-03-29 12:42:40 -05007812 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
7813 IPR_USE_LONG_TRANSOP_TIMEOUT },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007814 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King6d84c942007-01-23 11:25:23 -06007815 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007816 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King22d2e402007-04-26 16:00:13 -05007817 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
7818 IPR_USE_LONG_TRANSOP_TIMEOUT},
Brian King60e74862006-11-21 10:28:10 -06007819 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King5469cb52007-03-29 12:42:40 -05007820 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
7821 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06007822 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
Brian King22d2e402007-04-26 16:00:13 -05007823 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
7824 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King185eb312007-03-29 12:42:53 -05007825 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
7826 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575D, 0, 0,
7827 IPR_USE_LONG_TRANSOP_TIMEOUT },
7828 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
7829 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
7830 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
Brian King5469cb52007-03-29 12:42:40 -05007831 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
Brian King463fc692007-05-07 17:09:05 -05007832 IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
Brian King6d84c942007-01-23 11:25:23 -06007834 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King6d84c942007-01-23 11:25:23 -06007836 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007837 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King5469cb52007-03-29 12:42:40 -05007838 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
7839 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06007840 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King5469cb52007-03-29 12:42:40 -05007841 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
7842 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King185eb312007-03-29 12:42:53 -05007843 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SCAMP_E,
7844 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0,
7845 IPR_USE_LONG_TRANSOP_TIMEOUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846 { }
7847};
7848MODULE_DEVICE_TABLE(pci, ipr_pci_table);
7849
Linas Vepstasf8a88b192006-02-03 16:52:42 -06007850static struct pci_error_handlers ipr_err_handler = {
7851 .error_detected = ipr_pci_error_detected,
7852 .slot_reset = ipr_pci_slot_reset,
7853};
7854
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855static struct pci_driver ipr_driver = {
7856 .name = IPR_NAME,
7857 .id_table = ipr_pci_table,
7858 .probe = ipr_probe,
7859 .remove = ipr_remove,
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007860 .shutdown = ipr_shutdown,
Linas Vepstasf8a88b192006-02-03 16:52:42 -06007861 .err_handler = &ipr_err_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862};
7863
7864/**
7865 * ipr_init - Module entry point
7866 *
7867 * Return value:
7868 * 0 on success / negative value on failure
7869 **/
7870static int __init ipr_init(void)
7871{
7872 ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
7873 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
7874
Henrik Kretzschmardcbccbde2006-09-25 16:58:58 -07007875 return pci_register_driver(&ipr_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876}
7877
7878/**
7879 * ipr_exit - Module unload
7880 *
7881 * Module unload entry point.
7882 *
7883 * Return value:
7884 * none
7885 **/
7886static void __exit ipr_exit(void)
7887{
7888 pci_unregister_driver(&ipr_driver);
7889}
7890
7891module_init(ipr_init);
7892module_exit(ipr_exit);