blob: 91e330a12721a5d2d48c092ab022e5a2bb9a6200 [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>
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -080075#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <asm/io.h>
77#include <asm/irq.h>
78#include <asm/processor.h>
79#include <scsi/scsi.h>
80#include <scsi/scsi_host.h>
81#include <scsi/scsi_tcq.h>
82#include <scsi/scsi_eh.h>
83#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include "ipr.h"
85
86/*
87 * Global Data
88 */
Denis Chengb7d68ca2007-12-13 16:14:27 -080089static LIST_HEAD(ipr_ioa_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
91static unsigned int ipr_max_speed = 1;
92static int ipr_testmode = 0;
93static unsigned int ipr_fastfail = 0;
Brian King5469cb52007-03-29 12:42:40 -050094static unsigned int ipr_transop_timeout = 0;
brking@us.ibm.com62275042005-11-01 17:01:14 -060095static unsigned int ipr_enable_cache = 1;
brking@us.ibm.comd3c74872005-11-01 17:01:34 -060096static unsigned int ipr_debug = 0;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -080097static unsigned int ipr_max_devs = IPR_DEFAULT_SIS64_DEVS;
Brian Kingac09c342007-04-26 16:00:16 -050098static unsigned int ipr_dual_ioa_raid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static DEFINE_SPINLOCK(ipr_driver_lock);
100
101/* This table describes the differences between DMA controller chips */
102static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
Brian King60e74862006-11-21 10:28:10 -0600103 { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .mailbox = 0x0042C,
105 .cache_line_size = 0x20,
106 {
107 .set_interrupt_mask_reg = 0x0022C,
108 .clr_interrupt_mask_reg = 0x00230,
109 .sense_interrupt_mask_reg = 0x0022C,
110 .clr_interrupt_reg = 0x00228,
111 .sense_interrupt_reg = 0x00224,
112 .ioarrin_reg = 0x00404,
113 .sense_uproc_interrupt_reg = 0x00214,
114 .set_uproc_interrupt_reg = 0x00214,
115 .clr_uproc_interrupt_reg = 0x00218
116 }
117 },
118 { /* Snipe and Scamp */
119 .mailbox = 0x0052C,
120 .cache_line_size = 0x20,
121 {
122 .set_interrupt_mask_reg = 0x00288,
123 .clr_interrupt_mask_reg = 0x0028C,
124 .sense_interrupt_mask_reg = 0x00288,
125 .clr_interrupt_reg = 0x00284,
126 .sense_interrupt_reg = 0x00280,
127 .ioarrin_reg = 0x00504,
128 .sense_uproc_interrupt_reg = 0x00290,
129 .set_uproc_interrupt_reg = 0x00290,
130 .clr_uproc_interrupt_reg = 0x00294
131 }
132 },
Wayne Boyera74c1632010-02-19 13:23:51 -0800133 { /* CRoC */
134 .mailbox = 0x00040,
135 .cache_line_size = 0x20,
136 {
137 .set_interrupt_mask_reg = 0x00010,
138 .clr_interrupt_mask_reg = 0x00018,
139 .sense_interrupt_mask_reg = 0x00010,
140 .clr_interrupt_reg = 0x00008,
141 .sense_interrupt_reg = 0x00000,
142 .ioarrin_reg = 0x00070,
143 .sense_uproc_interrupt_reg = 0x00020,
144 .set_uproc_interrupt_reg = 0x00020,
145 .clr_uproc_interrupt_reg = 0x00028
146 }
147 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
150static const struct ipr_chip_t ipr_chip[] = {
Wayne Boyera32c0552010-02-19 13:23:36 -0800151 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[0] },
152 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[0] },
153 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[0] },
154 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[0] },
155 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, IPR_USE_MSI, IPR_SIS32, &ipr_chip_cfg[0] },
156 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[1] },
157 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[1] }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160static int ipr_max_bus_speeds [] = {
161 IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
162};
163
164MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
165MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
166module_param_named(max_speed, ipr_max_speed, uint, 0);
167MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
168module_param_named(log_level, ipr_log_level, uint, 0);
169MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
170module_param_named(testmode, ipr_testmode, int, 0);
171MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
Wayne Boyer2cf22be2009-02-24 11:36:00 -0800172module_param_named(fastfail, ipr_fastfail, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
174module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
175MODULE_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 -0600176module_param_named(enable_cache, ipr_enable_cache, int, 0);
177MODULE_PARM_DESC(enable_cache, "Enable adapter's non-volatile write cache (default: 1)");
Wayne Boyer2cf22be2009-02-24 11:36:00 -0800178module_param_named(debug, ipr_debug, int, S_IRUGO | S_IWUSR);
brking@us.ibm.comd3c74872005-11-01 17:01:34 -0600179MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
Brian Kingac09c342007-04-26 16:00:16 -0500180module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
181MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -0800182module_param_named(max_devs, ipr_max_devs, int, 0);
183MODULE_PARM_DESC(max_devs, "Specify the maximum number of physical devices. "
184 "[Default=" __stringify(IPR_DEFAULT_SIS64_DEVS) "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185MODULE_LICENSE("GPL");
186MODULE_VERSION(IPR_DRIVER_VERSION);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* A constant array of IOASCs/URCs/Error Messages */
189static const
190struct ipr_error_table_t ipr_error_table[] = {
Brian King933916f2007-03-29 12:43:30 -0500191 {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 "8155: An unknown error was received"},
193 {0x00330000, 0, 0,
194 "Soft underlength error"},
195 {0x005A0000, 0, 0,
196 "Command to be cancelled not found"},
197 {0x00808000, 0, 0,
198 "Qualified success"},
Brian King933916f2007-03-29 12:43:30 -0500199 {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 "FFFE: Soft device bus error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500201 {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500202 "4101: Soft device bus fabric error"},
Brian King933916f2007-03-29 12:43:30 -0500203 {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 "FFF9: Device sector reassign successful"},
Brian King933916f2007-03-29 12:43:30 -0500205 {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 "FFF7: Media error recovered by device rewrite procedures"},
Brian King933916f2007-03-29 12:43:30 -0500207 {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 "7001: IOA sector reassignment successful"},
Brian King933916f2007-03-29 12:43:30 -0500209 {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 "FFF9: Soft media error. Sector reassignment recommended"},
Brian King933916f2007-03-29 12:43:30 -0500211 {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 "FFF7: Media error recovered by IOA rewrite procedures"},
Brian King933916f2007-03-29 12:43:30 -0500213 {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 "FF3D: Soft PCI bus error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500215 {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 "FFF6: Device hardware error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500217 {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 "FFF6: Device hardware error recovered by the device"},
Brian King933916f2007-03-29 12:43:30 -0500219 {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 "FF3D: Soft IOA error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500221 {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 "FFFA: Undefined device response recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500223 {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 "FFF6: Device bus error, message or command phase"},
Brian King933916f2007-03-29 12:43:30 -0500225 {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King35a39692006-09-25 12:39:20 -0500226 "FFFE: Task Management Function failed"},
Brian King933916f2007-03-29 12:43:30 -0500227 {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 "FFF6: Failure prediction threshold exceeded"},
Brian King933916f2007-03-29 12:43:30 -0500229 {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 "8009: Impending cache battery pack failure"},
231 {0x02040400, 0, 0,
232 "34FF: Disk device format in progress"},
Brian King65f56472007-04-26 16:00:12 -0500233 {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
234 "9070: IOA requested reset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 {0x023F0000, 0, 0,
236 "Synchronization required"},
237 {0x024E0000, 0, 0,
238 "No ready, IOA shutdown"},
239 {0x025A0000, 0, 0,
240 "Not ready, IOA has been shutdown"},
Brian King933916f2007-03-29 12:43:30 -0500241 {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 "3020: Storage subsystem configuration error"},
243 {0x03110B00, 0, 0,
244 "FFF5: Medium error, data unreadable, recommend reassign"},
245 {0x03110C00, 0, 0,
246 "7000: Medium error, data unreadable, do not reassign"},
Brian King933916f2007-03-29 12:43:30 -0500247 {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 "FFF3: Disk media format bad"},
Brian King933916f2007-03-29 12:43:30 -0500249 {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 "3002: Addressed device failed to respond to selection"},
Brian King933916f2007-03-29 12:43:30 -0500251 {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 "3100: Device bus error"},
Brian King933916f2007-03-29 12:43:30 -0500253 {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 "3109: IOA timed out a device command"},
255 {0x04088000, 0, 0,
256 "3120: SCSI bus is not operational"},
Brian King933916f2007-03-29 12:43:30 -0500257 {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500258 "4100: Hard device bus fabric error"},
Brian King933916f2007-03-29 12:43:30 -0500259 {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 "9000: IOA reserved area data check"},
Brian King933916f2007-03-29 12:43:30 -0500261 {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 "9001: IOA reserved area invalid data pattern"},
Brian King933916f2007-03-29 12:43:30 -0500263 {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 "9002: IOA reserved area LRC error"},
Brian King933916f2007-03-29 12:43:30 -0500265 {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 "102E: Out of alternate sectors for disk storage"},
Brian King933916f2007-03-29 12:43:30 -0500267 {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 "FFF4: Data transfer underlength error"},
Brian King933916f2007-03-29 12:43:30 -0500269 {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 "FFF4: Data transfer overlength error"},
Brian King933916f2007-03-29 12:43:30 -0500271 {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 "3400: Logical unit failure"},
Brian King933916f2007-03-29 12:43:30 -0500273 {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 "FFF4: Device microcode is corrupt"},
Brian King933916f2007-03-29 12:43:30 -0500275 {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 "8150: PCI bus error"},
277 {0x04430000, 1, 0,
278 "Unsupported device bus message received"},
Brian King933916f2007-03-29 12:43:30 -0500279 {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 "FFF4: Disk device problem"},
Brian King933916f2007-03-29 12:43:30 -0500281 {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 "8150: Permanent IOA failure"},
Brian King933916f2007-03-29 12:43:30 -0500283 {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 "3010: Disk device returned wrong response to IOA"},
Brian King933916f2007-03-29 12:43:30 -0500285 {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 "8151: IOA microcode error"},
287 {0x04448500, 0, 0,
288 "Device bus status error"},
Brian King933916f2007-03-29 12:43:30 -0500289 {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 "8157: IOA error requiring IOA reset to recover"},
Brian King35a39692006-09-25 12:39:20 -0500291 {0x04448700, 0, 0,
292 "ATA device status error"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 {0x04490000, 0, 0,
294 "Message reject received from the device"},
Brian King933916f2007-03-29 12:43:30 -0500295 {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 "8008: A permanent cache battery pack failure occurred"},
Brian King933916f2007-03-29 12:43:30 -0500297 {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 "9090: Disk unit has been modified after the last known status"},
Brian King933916f2007-03-29 12:43:30 -0500299 {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 "9081: IOA detected device error"},
Brian King933916f2007-03-29 12:43:30 -0500301 {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 "9082: IOA detected device error"},
Brian King933916f2007-03-29 12:43:30 -0500303 {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 "3110: Device bus error, message or command phase"},
Brian King933916f2007-03-29 12:43:30 -0500305 {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
Brian King35a39692006-09-25 12:39:20 -0500306 "3110: SAS Command / Task Management Function failed"},
Brian King933916f2007-03-29 12:43:30 -0500307 {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 "9091: Incorrect hardware configuration change has been detected"},
Brian King933916f2007-03-29 12:43:30 -0500309 {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600310 "9073: Invalid multi-adapter configuration"},
Brian King933916f2007-03-29 12:43:30 -0500311 {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500312 "4010: Incorrect connection between cascaded expanders"},
Brian King933916f2007-03-29 12:43:30 -0500313 {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500314 "4020: Connections exceed IOA design limits"},
Brian King933916f2007-03-29 12:43:30 -0500315 {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500316 "4030: Incorrect multipath connection"},
Brian King933916f2007-03-29 12:43:30 -0500317 {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500318 "4110: Unsupported enclosure function"},
Brian King933916f2007-03-29 12:43:30 -0500319 {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 "FFF4: Command to logical unit failed"},
321 {0x05240000, 1, 0,
322 "Illegal request, invalid request type or request packet"},
323 {0x05250000, 0, 0,
324 "Illegal request, invalid resource handle"},
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600325 {0x05258000, 0, 0,
326 "Illegal request, commands not allowed to this device"},
327 {0x05258100, 0, 0,
328 "Illegal request, command not allowed to a secondary adapter"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 {0x05260000, 0, 0,
330 "Illegal request, invalid field in parameter list"},
331 {0x05260100, 0, 0,
332 "Illegal request, parameter not supported"},
333 {0x05260200, 0, 0,
334 "Illegal request, parameter value invalid"},
335 {0x052C0000, 0, 0,
336 "Illegal request, command sequence error"},
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600337 {0x052C8000, 1, 0,
338 "Illegal request, dual adapter support not enabled"},
Brian King933916f2007-03-29 12:43:30 -0500339 {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 "9031: Array protection temporarily suspended, protection resuming"},
Brian King933916f2007-03-29 12:43:30 -0500341 {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 "9040: Array protection temporarily suspended, protection resuming"},
Brian King933916f2007-03-29 12:43:30 -0500343 {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500344 "3140: Device bus not ready to ready transition"},
Brian King933916f2007-03-29 12:43:30 -0500345 {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 "FFFB: SCSI bus was reset"},
347 {0x06290500, 0, 0,
348 "FFFE: SCSI bus transition to single ended"},
349 {0x06290600, 0, 0,
350 "FFFE: SCSI bus transition to LVD"},
Brian King933916f2007-03-29 12:43:30 -0500351 {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 "FFFB: SCSI bus was reset by another initiator"},
Brian King933916f2007-03-29 12:43:30 -0500353 {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 "3029: A device replacement has occurred"},
Brian King933916f2007-03-29 12:43:30 -0500355 {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 "9051: IOA cache data exists for a missing or failed device"},
Brian King933916f2007-03-29 12:43:30 -0500357 {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600358 "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
Brian King933916f2007-03-29 12:43:30 -0500359 {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 "9025: Disk unit is not supported at its physical location"},
Brian King933916f2007-03-29 12:43:30 -0500361 {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 "3020: IOA detected a SCSI bus configuration error"},
Brian King933916f2007-03-29 12:43:30 -0500363 {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 "3150: SCSI bus configuration error"},
Brian King933916f2007-03-29 12:43:30 -0500365 {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600366 "9074: Asymmetric advanced function disk configuration"},
Brian King933916f2007-03-29 12:43:30 -0500367 {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500368 "4040: Incomplete multipath connection between IOA and enclosure"},
Brian King933916f2007-03-29 12:43:30 -0500369 {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500370 "4041: Incomplete multipath connection between enclosure and device"},
Brian King933916f2007-03-29 12:43:30 -0500371 {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500372 "9075: Incomplete multipath connection between IOA and remote IOA"},
Brian King933916f2007-03-29 12:43:30 -0500373 {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500374 "9076: Configuration error, missing remote IOA"},
Brian King933916f2007-03-29 12:43:30 -0500375 {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500376 "4050: Enclosure does not support a required multipath function"},
Wayne Boyerb75424f2009-01-28 08:24:50 -0800377 {0x06690000, 0, IPR_DEFAULT_LOG_LEVEL,
378 "4070: Logically bad block written on device"},
Brian King933916f2007-03-29 12:43:30 -0500379 {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 "9041: Array protection temporarily suspended"},
Brian King933916f2007-03-29 12:43:30 -0500381 {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 "9042: Corrupt array parity detected on specified device"},
Brian King933916f2007-03-29 12:43:30 -0500383 {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 "9030: Array no longer protected due to missing or failed disk unit"},
Brian King933916f2007-03-29 12:43:30 -0500385 {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600386 "9071: Link operational transition"},
Brian King933916f2007-03-29 12:43:30 -0500387 {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600388 "9072: Link not operational transition"},
Brian King933916f2007-03-29 12:43:30 -0500389 {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 "9032: Array exposed but still protected"},
Brian Kinge4353402007-03-29 12:43:37 -0500391 {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
392 "70DD: Device forced failed by disrupt device command"},
Brian King933916f2007-03-29 12:43:30 -0500393 {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500394 "4061: Multipath redundancy level got better"},
Brian King933916f2007-03-29 12:43:30 -0500395 {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500396 "4060: Multipath redundancy level got worse"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 {0x07270000, 0, 0,
398 "Failure due to other device"},
Brian King933916f2007-03-29 12:43:30 -0500399 {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 "9008: IOA does not support functions expected by devices"},
Brian King933916f2007-03-29 12:43:30 -0500401 {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 "9010: Cache data associated with attached devices cannot be found"},
Brian King933916f2007-03-29 12:43:30 -0500403 {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 "9011: Cache data belongs to devices other than those attached"},
Brian King933916f2007-03-29 12:43:30 -0500405 {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 "9020: Array missing 2 or more devices with only 1 device present"},
Brian King933916f2007-03-29 12:43:30 -0500407 {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 "9021: Array missing 2 or more devices with 2 or more devices present"},
Brian King933916f2007-03-29 12:43:30 -0500409 {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 "9022: Exposed array is missing a required device"},
Brian King933916f2007-03-29 12:43:30 -0500411 {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 "9023: Array member(s) not at required physical locations"},
Brian King933916f2007-03-29 12:43:30 -0500413 {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 "9024: Array not functional due to present hardware configuration"},
Brian King933916f2007-03-29 12:43:30 -0500415 {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 "9026: Array not functional due to present hardware configuration"},
Brian King933916f2007-03-29 12:43:30 -0500417 {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 "9027: Array is missing a device and parity is out of sync"},
Brian King933916f2007-03-29 12:43:30 -0500419 {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 "9028: Maximum number of arrays already exist"},
Brian King933916f2007-03-29 12:43:30 -0500421 {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 "9050: Required cache data cannot be located for a disk unit"},
Brian King933916f2007-03-29 12:43:30 -0500423 {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 "9052: Cache data exists for a device that has been modified"},
Brian King933916f2007-03-29 12:43:30 -0500425 {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 "9054: IOA resources not available due to previous problems"},
Brian King933916f2007-03-29 12:43:30 -0500427 {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 "9092: Disk unit requires initialization before use"},
Brian King933916f2007-03-29 12:43:30 -0500429 {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 "9029: Incorrect hardware configuration change has been detected"},
Brian King933916f2007-03-29 12:43:30 -0500431 {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 "9060: One or more disk pairs are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500433 {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 "9061: One or more disks are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500435 {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 "9062: One or more disks are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500437 {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 "9063: Maximum number of functional arrays has been exceeded"},
439 {0x0B260000, 0, 0,
440 "Aborted command, invalid descriptor"},
441 {0x0B5A0000, 0, 0,
442 "Command terminated by host"}
443};
444
445static const struct ipr_ses_table_entry ipr_ses_table[] = {
446 { "2104-DL1 ", "XXXXXXXXXXXXXXXX", 80 },
447 { "2104-TL1 ", "XXXXXXXXXXXXXXXX", 80 },
448 { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
449 { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
450 { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
451 { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
452 { "2104-DU3 ", "XXXXXXXXXXXXXXXX", 160 },
453 { "2104-TU3 ", "XXXXXXXXXXXXXXXX", 160 },
454 { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
455 { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
456 { "St V1S2 ", "XXXXXXXXXXXXXXXX", 160 },
457 { "HSBPD4M PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
458 { "VSBPD1H U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
459};
460
461/*
462 * Function Prototypes
463 */
464static int ipr_reset_alert(struct ipr_cmnd *);
465static void ipr_process_ccn(struct ipr_cmnd *);
466static void ipr_process_error(struct ipr_cmnd *);
467static void ipr_reset_ioa_job(struct ipr_cmnd *);
468static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
469 enum ipr_shutdown_type);
470
471#ifdef CONFIG_SCSI_IPR_TRACE
472/**
473 * ipr_trc_hook - Add a trace entry to the driver trace
474 * @ipr_cmd: ipr command struct
475 * @type: trace type
476 * @add_data: additional data
477 *
478 * Return value:
479 * none
480 **/
481static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
482 u8 type, u32 add_data)
483{
484 struct ipr_trace_entry *trace_entry;
485 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
486
487 trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
488 trace_entry->time = jiffies;
489 trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
490 trace_entry->type = type;
Wayne Boyera32c0552010-02-19 13:23:36 -0800491 if (ipr_cmd->ioa_cfg->sis64)
492 trace_entry->ata_op_code = ipr_cmd->i.ata_ioadl.regs.command;
493 else
494 trace_entry->ata_op_code = ipr_cmd->ioarcb.u.add_data.u.regs.command;
Brian King35a39692006-09-25 12:39:20 -0500495 trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
497 trace_entry->u.add_data = add_data;
498}
499#else
500#define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
501#endif
502
503/**
504 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
505 * @ipr_cmd: ipr command struct
506 *
507 * Return value:
508 * none
509 **/
510static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
511{
512 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
513 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
Wayne Boyera32c0552010-02-19 13:23:36 -0800514 dma_addr_t dma_addr = ipr_cmd->dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
Wayne Boyera32c0552010-02-19 13:23:36 -0800517 ioarcb->data_transfer_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ioarcb->read_data_transfer_length = 0;
Wayne Boyera32c0552010-02-19 13:23:36 -0800519 ioarcb->ioadl_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ioarcb->read_ioadl_len = 0;
Wayne Boyera32c0552010-02-19 13:23:36 -0800521
522 if (ipr_cmd->ioa_cfg->sis64)
523 ioarcb->u.sis64_addr_data.data_ioadl_addr =
524 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
525 else {
526 ioarcb->write_ioadl_addr =
527 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
528 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
529 }
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 ioasa->ioasc = 0;
532 ioasa->residual_data_len = 0;
Brian King35a39692006-09-25 12:39:20 -0500533 ioasa->u.gata.status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 ipr_cmd->scsi_cmd = NULL;
Brian King35a39692006-09-25 12:39:20 -0500536 ipr_cmd->qc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ipr_cmd->sense_buffer[0] = 0;
538 ipr_cmd->dma_use_sg = 0;
539}
540
541/**
542 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
543 * @ipr_cmd: ipr command struct
544 *
545 * Return value:
546 * none
547 **/
548static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
549{
550 ipr_reinit_ipr_cmnd(ipr_cmd);
551 ipr_cmd->u.scratch = 0;
552 ipr_cmd->sibling = NULL;
553 init_timer(&ipr_cmd->timer);
554}
555
556/**
557 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
558 * @ioa_cfg: ioa config struct
559 *
560 * Return value:
561 * pointer to ipr command struct
562 **/
563static
564struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
565{
566 struct ipr_cmnd *ipr_cmd;
567
568 ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
569 list_del(&ipr_cmd->queue);
570 ipr_init_ipr_cmnd(ipr_cmd);
571
572 return ipr_cmd;
573}
574
575/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
577 * @ioa_cfg: ioa config struct
578 * @clr_ints: interrupts to clear
579 *
580 * This function masks all interrupts on the adapter, then clears the
581 * interrupts specified in the mask
582 *
583 * Return value:
584 * none
585 **/
586static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
587 u32 clr_ints)
588{
589 volatile u32 int_reg;
590
591 /* Stop new interrupts */
592 ioa_cfg->allow_interrupts = 0;
593
594 /* Set interrupt mask to stop all new interrupts */
595 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
596
597 /* Clear any pending interrupts */
598 writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
599 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
600}
601
602/**
603 * ipr_save_pcix_cmd_reg - Save PCI-X command register
604 * @ioa_cfg: ioa config struct
605 *
606 * Return value:
607 * 0 on success / -EIO on failure
608 **/
609static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
610{
611 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
612
Brian King7dce0e12007-01-23 11:25:30 -0600613 if (pcix_cmd_reg == 0)
614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
617 &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
618 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
619 return -EIO;
620 }
621
622 ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
623 return 0;
624}
625
626/**
627 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
628 * @ioa_cfg: ioa config struct
629 *
630 * Return value:
631 * 0 on success / -EIO on failure
632 **/
633static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
634{
635 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
636
637 if (pcix_cmd_reg) {
638 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
639 ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
640 dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
641 return -EIO;
642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 return 0;
646}
647
648/**
Brian King35a39692006-09-25 12:39:20 -0500649 * ipr_sata_eh_done - done function for aborted SATA commands
650 * @ipr_cmd: ipr command struct
651 *
652 * This function is invoked for ops generated to SATA
653 * devices which are being aborted.
654 *
655 * Return value:
656 * none
657 **/
658static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
659{
660 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
661 struct ata_queued_cmd *qc = ipr_cmd->qc;
662 struct ipr_sata_port *sata_port = qc->ap->private_data;
663
664 qc->err_mask |= AC_ERR_OTHER;
665 sata_port->ioasa.status |= ATA_BUSY;
666 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
667 ata_qc_complete(qc);
668}
669
670/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * ipr_scsi_eh_done - mid-layer done function for aborted ops
672 * @ipr_cmd: ipr command struct
673 *
674 * This function is invoked by the interrupt handler for
675 * ops generated by the SCSI mid-layer which are being aborted.
676 *
677 * Return value:
678 * none
679 **/
680static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
681{
682 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
683 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
684
685 scsi_cmd->result |= (DID_ERROR << 16);
686
FUJITA Tomonori63015bc2007-05-26 00:26:59 +0900687 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 scsi_cmd->scsi_done(scsi_cmd);
689 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
690}
691
692/**
693 * ipr_fail_all_ops - Fails all outstanding ops.
694 * @ioa_cfg: ioa config struct
695 *
696 * This function fails all outstanding ops.
697 *
698 * Return value:
699 * none
700 **/
701static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
702{
703 struct ipr_cmnd *ipr_cmd, *temp;
704
705 ENTER;
706 list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
707 list_del(&ipr_cmd->queue);
708
709 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
710 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
711
712 if (ipr_cmd->scsi_cmd)
713 ipr_cmd->done = ipr_scsi_eh_done;
Brian King35a39692006-09-25 12:39:20 -0500714 else if (ipr_cmd->qc)
715 ipr_cmd->done = ipr_sata_eh_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
718 del_timer(&ipr_cmd->timer);
719 ipr_cmd->done(ipr_cmd);
720 }
721
722 LEAVE;
723}
724
725/**
Wayne Boyera32c0552010-02-19 13:23:36 -0800726 * ipr_send_command - Send driver initiated requests.
727 * @ipr_cmd: ipr command struct
728 *
729 * This function sends a command to the adapter using the correct write call.
730 * In the case of sis64, calculate the ioarcb size required. Then or in the
731 * appropriate bits.
732 *
733 * Return value:
734 * none
735 **/
736static void ipr_send_command(struct ipr_cmnd *ipr_cmd)
737{
738 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
739 dma_addr_t send_dma_addr = ipr_cmd->dma_addr;
740
741 if (ioa_cfg->sis64) {
742 /* The default size is 256 bytes */
743 send_dma_addr |= 0x1;
744
745 /* If the number of ioadls * size of ioadl > 128 bytes,
746 then use a 512 byte ioarcb */
747 if (ipr_cmd->dma_use_sg * sizeof(struct ipr_ioadl64_desc) > 128 )
748 send_dma_addr |= 0x4;
749 writeq(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
750 } else
751 writel(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
752}
753
754/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * ipr_do_req - Send driver initiated requests.
756 * @ipr_cmd: ipr command struct
757 * @done: done function
758 * @timeout_func: timeout function
759 * @timeout: timeout value
760 *
761 * This function sends the specified command to the adapter with the
762 * timeout given. The done function is invoked on command completion.
763 *
764 * Return value:
765 * none
766 **/
767static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
768 void (*done) (struct ipr_cmnd *),
769 void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
770{
771 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
772
773 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
774
775 ipr_cmd->done = done;
776
777 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
778 ipr_cmd->timer.expires = jiffies + timeout;
779 ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
780
781 add_timer(&ipr_cmd->timer);
782
783 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
784
785 mb();
Wayne Boyera32c0552010-02-19 13:23:36 -0800786
787 ipr_send_command(ipr_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790/**
791 * ipr_internal_cmd_done - Op done function for an internally generated op.
792 * @ipr_cmd: ipr command struct
793 *
794 * This function is the op done function for an internally generated,
795 * blocking op. It simply wakes the sleeping thread.
796 *
797 * Return value:
798 * none
799 **/
800static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
801{
802 if (ipr_cmd->sibling)
803 ipr_cmd->sibling = NULL;
804 else
805 complete(&ipr_cmd->completion);
806}
807
808/**
Wayne Boyera32c0552010-02-19 13:23:36 -0800809 * ipr_init_ioadl - initialize the ioadl for the correct SIS type
810 * @ipr_cmd: ipr command struct
811 * @dma_addr: dma address
812 * @len: transfer length
813 * @flags: ioadl flag value
814 *
815 * This function initializes an ioadl in the case where there is only a single
816 * descriptor.
817 *
818 * Return value:
819 * nothing
820 **/
821static void ipr_init_ioadl(struct ipr_cmnd *ipr_cmd, dma_addr_t dma_addr,
822 u32 len, int flags)
823{
824 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
825 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
826
827 ipr_cmd->dma_use_sg = 1;
828
829 if (ipr_cmd->ioa_cfg->sis64) {
830 ioadl64->flags = cpu_to_be32(flags);
831 ioadl64->data_len = cpu_to_be32(len);
832 ioadl64->address = cpu_to_be64(dma_addr);
833
834 ipr_cmd->ioarcb.ioadl_len =
835 cpu_to_be32(sizeof(struct ipr_ioadl64_desc));
836 ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
837 } else {
838 ioadl->flags_and_data_len = cpu_to_be32(flags | len);
839 ioadl->address = cpu_to_be32(dma_addr);
840
841 if (flags == IPR_IOADL_FLAGS_READ_LAST) {
842 ipr_cmd->ioarcb.read_ioadl_len =
843 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
844 ipr_cmd->ioarcb.read_data_transfer_length = cpu_to_be32(len);
845 } else {
846 ipr_cmd->ioarcb.ioadl_len =
847 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
848 ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
849 }
850 }
851}
852
853/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * ipr_send_blocking_cmd - Send command and sleep on its completion.
855 * @ipr_cmd: ipr command struct
856 * @timeout_func: function to invoke if command times out
857 * @timeout: timeout
858 *
859 * Return value:
860 * none
861 **/
862static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
863 void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
864 u32 timeout)
865{
866 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
867
868 init_completion(&ipr_cmd->completion);
869 ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
870
871 spin_unlock_irq(ioa_cfg->host->host_lock);
872 wait_for_completion(&ipr_cmd->completion);
873 spin_lock_irq(ioa_cfg->host->host_lock);
874}
875
876/**
877 * ipr_send_hcam - Send an HCAM to the adapter.
878 * @ioa_cfg: ioa config struct
879 * @type: HCAM type
880 * @hostrcb: hostrcb struct
881 *
882 * This function will send a Host Controlled Async command to the adapter.
883 * If HCAMs are currently not allowed to be issued to the adapter, it will
884 * place the hostrcb on the free queue.
885 *
886 * Return value:
887 * none
888 **/
889static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
890 struct ipr_hostrcb *hostrcb)
891{
892 struct ipr_cmnd *ipr_cmd;
893 struct ipr_ioarcb *ioarcb;
894
895 if (ioa_cfg->allow_cmds) {
896 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
897 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
898 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
899
900 ipr_cmd->u.hostrcb = hostrcb;
901 ioarcb = &ipr_cmd->ioarcb;
902
903 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
904 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
905 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
906 ioarcb->cmd_pkt.cdb[1] = type;
907 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
908 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
909
Wayne Boyera32c0552010-02-19 13:23:36 -0800910 ipr_init_ioadl(ipr_cmd, hostrcb->hostrcb_dma,
911 sizeof(hostrcb->hcam), IPR_IOADL_FLAGS_READ_LAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
914 ipr_cmd->done = ipr_process_ccn;
915 else
916 ipr_cmd->done = ipr_process_error;
917
918 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
919
920 mb();
Wayne Boyera32c0552010-02-19 13:23:36 -0800921
922 ipr_send_command(ipr_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 } else {
924 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
925 }
926}
927
928/**
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -0800929 * ipr_update_ata_class - Update the ata class in the resource entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * @res: resource entry struct
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -0800931 * @proto: cfgte device bus protocol value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 *
933 * Return value:
934 * none
935 **/
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -0800936static void ipr_update_ata_class(struct ipr_resource_entry *res, unsigned int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -0800938 switch(proto) {
939 case IPR_PROTO_SATA:
940 case IPR_PROTO_SAS_STP:
941 res->ata_class = ATA_DEV_ATA;
942 break;
943 case IPR_PROTO_SATA_ATAPI:
944 case IPR_PROTO_SAS_STP_ATAPI:
945 res->ata_class = ATA_DEV_ATAPI;
946 break;
947 default:
948 res->ata_class = ATA_DEV_UNKNOWN;
949 break;
950 };
951}
952
953/**
954 * ipr_init_res_entry - Initialize a resource entry struct.
955 * @res: resource entry struct
956 * @cfgtew: config table entry wrapper struct
957 *
958 * Return value:
959 * none
960 **/
961static void ipr_init_res_entry(struct ipr_resource_entry *res,
962 struct ipr_config_table_entry_wrapper *cfgtew)
963{
964 int found = 0;
965 unsigned int proto;
966 struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
967 struct ipr_resource_entry *gscsi_res = NULL;
968
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -0600969 res->needs_sync_complete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 res->in_erp = 0;
971 res->add_to_ml = 0;
972 res->del_from_ml = 0;
973 res->resetting_device = 0;
974 res->sdev = NULL;
Brian King35a39692006-09-25 12:39:20 -0500975 res->sata_port = NULL;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -0800976
977 if (ioa_cfg->sis64) {
978 proto = cfgtew->u.cfgte64->proto;
979 res->res_flags = cfgtew->u.cfgte64->res_flags;
980 res->qmodel = IPR_QUEUEING_MODEL64(res);
981 res->type = cfgtew->u.cfgte64->res_type & 0x0f;
982
983 memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
984 sizeof(res->res_path));
985
986 res->bus = 0;
987 res->lun = scsilun_to_int(&res->dev_lun);
988
989 if (res->type == IPR_RES_TYPE_GENERIC_SCSI) {
990 list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue) {
991 if (gscsi_res->dev_id == cfgtew->u.cfgte64->dev_id) {
992 found = 1;
993 res->target = gscsi_res->target;
994 break;
995 }
996 }
997 if (!found) {
998 res->target = find_first_zero_bit(ioa_cfg->target_ids,
999 ioa_cfg->max_devs_supported);
1000 set_bit(res->target, ioa_cfg->target_ids);
1001 }
1002
1003 memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1004 sizeof(res->dev_lun.scsi_lun));
1005 } else if (res->type == IPR_RES_TYPE_IOAFP) {
1006 res->bus = IPR_IOAFP_VIRTUAL_BUS;
1007 res->target = 0;
1008 } else if (res->type == IPR_RES_TYPE_ARRAY) {
1009 res->bus = IPR_ARRAY_VIRTUAL_BUS;
1010 res->target = find_first_zero_bit(ioa_cfg->array_ids,
1011 ioa_cfg->max_devs_supported);
1012 set_bit(res->target, ioa_cfg->array_ids);
1013 } else if (res->type == IPR_RES_TYPE_VOLUME_SET) {
1014 res->bus = IPR_VSET_VIRTUAL_BUS;
1015 res->target = find_first_zero_bit(ioa_cfg->vset_ids,
1016 ioa_cfg->max_devs_supported);
1017 set_bit(res->target, ioa_cfg->vset_ids);
1018 } else {
1019 res->target = find_first_zero_bit(ioa_cfg->target_ids,
1020 ioa_cfg->max_devs_supported);
1021 set_bit(res->target, ioa_cfg->target_ids);
1022 }
1023 } else {
1024 proto = cfgtew->u.cfgte->proto;
1025 res->qmodel = IPR_QUEUEING_MODEL(res);
1026 res->flags = cfgtew->u.cfgte->flags;
1027 if (res->flags & IPR_IS_IOA_RESOURCE)
1028 res->type = IPR_RES_TYPE_IOAFP;
1029 else
1030 res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1031
1032 res->bus = cfgtew->u.cfgte->res_addr.bus;
1033 res->target = cfgtew->u.cfgte->res_addr.target;
1034 res->lun = cfgtew->u.cfgte->res_addr.lun;
1035 }
1036
1037 ipr_update_ata_class(res, proto);
1038}
1039
1040/**
1041 * ipr_is_same_device - Determine if two devices are the same.
1042 * @res: resource entry struct
1043 * @cfgtew: config table entry wrapper struct
1044 *
1045 * Return value:
1046 * 1 if the devices are the same / 0 otherwise
1047 **/
1048static int ipr_is_same_device(struct ipr_resource_entry *res,
1049 struct ipr_config_table_entry_wrapper *cfgtew)
1050{
1051 if (res->ioa_cfg->sis64) {
1052 if (!memcmp(&res->dev_id, &cfgtew->u.cfgte64->dev_id,
1053 sizeof(cfgtew->u.cfgte64->dev_id)) &&
1054 !memcmp(&res->lun, &cfgtew->u.cfgte64->lun,
1055 sizeof(cfgtew->u.cfgte64->lun))) {
1056 return 1;
1057 }
1058 } else {
1059 if (res->bus == cfgtew->u.cfgte->res_addr.bus &&
1060 res->target == cfgtew->u.cfgte->res_addr.target &&
1061 res->lun == cfgtew->u.cfgte->res_addr.lun)
1062 return 1;
1063 }
1064
1065 return 0;
1066}
1067
1068/**
1069 * ipr_format_resource_path - Format the resource path for printing.
1070 * @res_path: resource path
1071 * @buf: buffer
1072 *
1073 * Return value:
1074 * pointer to buffer
1075 **/
1076static char *ipr_format_resource_path(u8 *res_path, char *buffer)
1077{
1078 int i;
1079
1080 sprintf(buffer, "%02X", res_path[0]);
1081 for (i=1; res_path[i] != 0xff; i++)
1082 sprintf(buffer, "%s:%02X", buffer, res_path[i]);
1083
1084 return buffer;
1085}
1086
1087/**
1088 * ipr_update_res_entry - Update the resource entry.
1089 * @res: resource entry struct
1090 * @cfgtew: config table entry wrapper struct
1091 *
1092 * Return value:
1093 * none
1094 **/
1095static void ipr_update_res_entry(struct ipr_resource_entry *res,
1096 struct ipr_config_table_entry_wrapper *cfgtew)
1097{
1098 char buffer[IPR_MAX_RES_PATH_LENGTH];
1099 unsigned int proto;
1100 int new_path = 0;
1101
1102 if (res->ioa_cfg->sis64) {
1103 res->flags = cfgtew->u.cfgte64->flags;
1104 res->res_flags = cfgtew->u.cfgte64->res_flags;
1105 res->type = cfgtew->u.cfgte64->res_type & 0x0f;
1106
1107 memcpy(&res->std_inq_data, &cfgtew->u.cfgte64->std_inq_data,
1108 sizeof(struct ipr_std_inq_data));
1109
1110 res->qmodel = IPR_QUEUEING_MODEL64(res);
1111 proto = cfgtew->u.cfgte64->proto;
1112 res->res_handle = cfgtew->u.cfgte64->res_handle;
1113 res->dev_id = cfgtew->u.cfgte64->dev_id;
1114
1115 memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1116 sizeof(res->dev_lun.scsi_lun));
1117
1118 if (memcmp(res->res_path, &cfgtew->u.cfgte64->res_path,
1119 sizeof(res->res_path))) {
1120 memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1121 sizeof(res->res_path));
1122 new_path = 1;
1123 }
1124
1125 if (res->sdev && new_path)
1126 sdev_printk(KERN_INFO, res->sdev, "Resource path: %s\n",
1127 ipr_format_resource_path(&res->res_path[0], &buffer[0]));
1128 } else {
1129 res->flags = cfgtew->u.cfgte->flags;
1130 if (res->flags & IPR_IS_IOA_RESOURCE)
1131 res->type = IPR_RES_TYPE_IOAFP;
1132 else
1133 res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1134
1135 memcpy(&res->std_inq_data, &cfgtew->u.cfgte->std_inq_data,
1136 sizeof(struct ipr_std_inq_data));
1137
1138 res->qmodel = IPR_QUEUEING_MODEL(res);
1139 proto = cfgtew->u.cfgte->proto;
1140 res->res_handle = cfgtew->u.cfgte->res_handle;
1141 }
1142
1143 ipr_update_ata_class(res, proto);
1144}
1145
1146/**
1147 * ipr_clear_res_target - Clear the bit in the bit map representing the target
1148 * for the resource.
1149 * @res: resource entry struct
1150 * @cfgtew: config table entry wrapper struct
1151 *
1152 * Return value:
1153 * none
1154 **/
1155static void ipr_clear_res_target(struct ipr_resource_entry *res)
1156{
1157 struct ipr_resource_entry *gscsi_res = NULL;
1158 struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
1159
1160 if (!ioa_cfg->sis64)
1161 return;
1162
1163 if (res->bus == IPR_ARRAY_VIRTUAL_BUS)
1164 clear_bit(res->target, ioa_cfg->array_ids);
1165 else if (res->bus == IPR_VSET_VIRTUAL_BUS)
1166 clear_bit(res->target, ioa_cfg->vset_ids);
1167 else if (res->bus == 0 && res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1168 list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue)
1169 if (gscsi_res->dev_id == res->dev_id && gscsi_res != res)
1170 return;
1171 clear_bit(res->target, ioa_cfg->target_ids);
1172
1173 } else if (res->bus == 0)
1174 clear_bit(res->target, ioa_cfg->target_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177/**
1178 * ipr_handle_config_change - Handle a config change from the adapter
1179 * @ioa_cfg: ioa config struct
1180 * @hostrcb: hostrcb
1181 *
1182 * Return value:
1183 * none
1184 **/
1185static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08001186 struct ipr_hostrcb *hostrcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct ipr_resource_entry *res = NULL;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08001189 struct ipr_config_table_entry_wrapper cfgtew;
1190 __be32 cc_res_handle;
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 u32 is_ndn = 1;
1193
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08001194 if (ioa_cfg->sis64) {
1195 cfgtew.u.cfgte64 = &hostrcb->hcam.u.ccn.u.cfgte64;
1196 cc_res_handle = cfgtew.u.cfgte64->res_handle;
1197 } else {
1198 cfgtew.u.cfgte = &hostrcb->hcam.u.ccn.u.cfgte;
1199 cc_res_handle = cfgtew.u.cfgte->res_handle;
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08001203 if (res->res_handle == cc_res_handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 is_ndn = 0;
1205 break;
1206 }
1207 }
1208
1209 if (is_ndn) {
1210 if (list_empty(&ioa_cfg->free_res_q)) {
1211 ipr_send_hcam(ioa_cfg,
1212 IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
1213 hostrcb);
1214 return;
1215 }
1216
1217 res = list_entry(ioa_cfg->free_res_q.next,
1218 struct ipr_resource_entry, queue);
1219
1220 list_del(&res->queue);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08001221 ipr_init_res_entry(res, &cfgtew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
1223 }
1224
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08001225 ipr_update_res_entry(res, &cfgtew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
1228 if (res->sdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 res->del_from_ml = 1;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08001230 res->res_handle = IPR_INVALID_RES_HANDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (ioa_cfg->allow_ml_add_del)
1232 schedule_work(&ioa_cfg->work_q);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08001233 } else {
1234 ipr_clear_res_target(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08001236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 } else if (!res->sdev) {
1238 res->add_to_ml = 1;
1239 if (ioa_cfg->allow_ml_add_del)
1240 schedule_work(&ioa_cfg->work_q);
1241 }
1242
1243 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1244}
1245
1246/**
1247 * ipr_process_ccn - Op done function for a CCN.
1248 * @ipr_cmd: ipr command struct
1249 *
1250 * This function is the op done function for a configuration
1251 * change notification host controlled async from the adapter.
1252 *
1253 * Return value:
1254 * none
1255 **/
1256static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
1257{
1258 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1259 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1260 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
1261
1262 list_del(&hostrcb->queue);
1263 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1264
1265 if (ioasc) {
1266 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
1267 dev_err(&ioa_cfg->pdev->dev,
1268 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1269
1270 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1271 } else {
1272 ipr_handle_config_change(ioa_cfg, hostrcb);
1273 }
1274}
1275
1276/**
Brian King8cf093e2007-04-26 16:00:14 -05001277 * strip_and_pad_whitespace - Strip and pad trailing whitespace.
1278 * @i: index into buffer
1279 * @buf: string to modify
1280 *
1281 * This function will strip all trailing whitespace, pad the end
1282 * of the string with a single space, and NULL terminate the string.
1283 *
1284 * Return value:
1285 * new length of string
1286 **/
1287static int strip_and_pad_whitespace(int i, char *buf)
1288{
1289 while (i && buf[i] == ' ')
1290 i--;
1291 buf[i+1] = ' ';
1292 buf[i+2] = '\0';
1293 return i + 2;
1294}
1295
1296/**
1297 * ipr_log_vpd_compact - Log the passed extended VPD compactly.
1298 * @prefix: string to print at start of printk
1299 * @hostrcb: hostrcb pointer
1300 * @vpd: vendor/product id/sn struct
1301 *
1302 * Return value:
1303 * none
1304 **/
1305static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1306 struct ipr_vpd *vpd)
1307{
1308 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
1309 int i = 0;
1310
1311 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1312 i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
1313
1314 memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
1315 i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
1316
1317 memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
1318 buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
1319
1320 ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
1321}
1322
1323/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * ipr_log_vpd - Log the passed VPD to the error log.
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001325 * @vpd: vendor/product id/sn struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 *
1327 * Return value:
1328 * none
1329 **/
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001330static void ipr_log_vpd(struct ipr_vpd *vpd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
1333 + IPR_SERIAL_NUM_LEN];
1334
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001335 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1336 memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 IPR_PROD_ID_LEN);
1338 buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
1339 ipr_err("Vendor/Product ID: %s\n", buffer);
1340
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001341 memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 buffer[IPR_SERIAL_NUM_LEN] = '\0';
1343 ipr_err(" Serial Number: %s\n", buffer);
1344}
1345
1346/**
Brian King8cf093e2007-04-26 16:00:14 -05001347 * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1348 * @prefix: string to print at start of printk
1349 * @hostrcb: hostrcb pointer
1350 * @vpd: vendor/product id/sn/wwn struct
1351 *
1352 * Return value:
1353 * none
1354 **/
1355static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1356 struct ipr_ext_vpd *vpd)
1357{
1358 ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1359 ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1360 be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1361}
1362
1363/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001364 * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1365 * @vpd: vendor/product id/sn/wwn struct
1366 *
1367 * Return value:
1368 * none
1369 **/
1370static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1371{
1372 ipr_log_vpd(&vpd->vpd);
1373 ipr_err(" WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1374 be32_to_cpu(vpd->wwid[1]));
1375}
1376
1377/**
1378 * ipr_log_enhanced_cache_error - Log a cache error.
1379 * @ioa_cfg: ioa config struct
1380 * @hostrcb: hostrcb struct
1381 *
1382 * Return value:
1383 * none
1384 **/
1385static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1386 struct ipr_hostrcb *hostrcb)
1387{
1388 struct ipr_hostrcb_type_12_error *error =
1389 &hostrcb->hcam.u.error.u.type_12_error;
1390
1391 ipr_err("-----Current Configuration-----\n");
1392 ipr_err("Cache Directory Card Information:\n");
1393 ipr_log_ext_vpd(&error->ioa_vpd);
1394 ipr_err("Adapter Card Information:\n");
1395 ipr_log_ext_vpd(&error->cfc_vpd);
1396
1397 ipr_err("-----Expected Configuration-----\n");
1398 ipr_err("Cache Directory Card Information:\n");
1399 ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1400 ipr_err("Adapter Card Information:\n");
1401 ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1402
1403 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1404 be32_to_cpu(error->ioa_data[0]),
1405 be32_to_cpu(error->ioa_data[1]),
1406 be32_to_cpu(error->ioa_data[2]));
1407}
1408
1409/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 * ipr_log_cache_error - Log a cache error.
1411 * @ioa_cfg: ioa config struct
1412 * @hostrcb: hostrcb struct
1413 *
1414 * Return value:
1415 * none
1416 **/
1417static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1418 struct ipr_hostrcb *hostrcb)
1419{
1420 struct ipr_hostrcb_type_02_error *error =
1421 &hostrcb->hcam.u.error.u.type_02_error;
1422
1423 ipr_err("-----Current Configuration-----\n");
1424 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001425 ipr_log_vpd(&error->ioa_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001427 ipr_log_vpd(&error->cfc_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 ipr_err("-----Expected Configuration-----\n");
1430 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001431 ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001433 ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1436 be32_to_cpu(error->ioa_data[0]),
1437 be32_to_cpu(error->ioa_data[1]),
1438 be32_to_cpu(error->ioa_data[2]));
1439}
1440
1441/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001442 * ipr_log_enhanced_config_error - Log a configuration error.
1443 * @ioa_cfg: ioa config struct
1444 * @hostrcb: hostrcb struct
1445 *
1446 * Return value:
1447 * none
1448 **/
1449static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1450 struct ipr_hostrcb *hostrcb)
1451{
1452 int errors_logged, i;
1453 struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1454 struct ipr_hostrcb_type_13_error *error;
1455
1456 error = &hostrcb->hcam.u.error.u.type_13_error;
1457 errors_logged = be32_to_cpu(error->errors_logged);
1458
1459 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1460 be32_to_cpu(error->errors_detected), errors_logged);
1461
1462 dev_entry = error->dev;
1463
1464 for (i = 0; i < errors_logged; i++, dev_entry++) {
1465 ipr_err_separator;
1466
1467 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1468 ipr_log_ext_vpd(&dev_entry->vpd);
1469
1470 ipr_err("-----New Device Information-----\n");
1471 ipr_log_ext_vpd(&dev_entry->new_vpd);
1472
1473 ipr_err("Cache Directory Card Information:\n");
1474 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1475
1476 ipr_err("Adapter Card Information:\n");
1477 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1478 }
1479}
1480
1481/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 * ipr_log_config_error - Log a configuration error.
1483 * @ioa_cfg: ioa config struct
1484 * @hostrcb: hostrcb struct
1485 *
1486 * Return value:
1487 * none
1488 **/
1489static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1490 struct ipr_hostrcb *hostrcb)
1491{
1492 int errors_logged, i;
1493 struct ipr_hostrcb_device_data_entry *dev_entry;
1494 struct ipr_hostrcb_type_03_error *error;
1495
1496 error = &hostrcb->hcam.u.error.u.type_03_error;
1497 errors_logged = be32_to_cpu(error->errors_logged);
1498
1499 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1500 be32_to_cpu(error->errors_detected), errors_logged);
1501
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001502 dev_entry = error->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 for (i = 0; i < errors_logged; i++, dev_entry++) {
1505 ipr_err_separator;
1506
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001507 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001508 ipr_log_vpd(&dev_entry->vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 ipr_err("-----New Device Information-----\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001511 ipr_log_vpd(&dev_entry->new_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001514 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001517 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1520 be32_to_cpu(dev_entry->ioa_data[0]),
1521 be32_to_cpu(dev_entry->ioa_data[1]),
1522 be32_to_cpu(dev_entry->ioa_data[2]),
1523 be32_to_cpu(dev_entry->ioa_data[3]),
1524 be32_to_cpu(dev_entry->ioa_data[4]));
1525 }
1526}
1527
1528/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001529 * ipr_log_enhanced_array_error - Log an array configuration error.
1530 * @ioa_cfg: ioa config struct
1531 * @hostrcb: hostrcb struct
1532 *
1533 * Return value:
1534 * none
1535 **/
1536static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1537 struct ipr_hostrcb *hostrcb)
1538{
1539 int i, num_entries;
1540 struct ipr_hostrcb_type_14_error *error;
1541 struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1542 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1543
1544 error = &hostrcb->hcam.u.error.u.type_14_error;
1545
1546 ipr_err_separator;
1547
1548 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1549 error->protection_level,
1550 ioa_cfg->host->host_no,
1551 error->last_func_vset_res_addr.bus,
1552 error->last_func_vset_res_addr.target,
1553 error->last_func_vset_res_addr.lun);
1554
1555 ipr_err_separator;
1556
1557 array_entry = error->array_member;
1558 num_entries = min_t(u32, be32_to_cpu(error->num_entries),
1559 sizeof(error->array_member));
1560
1561 for (i = 0; i < num_entries; i++, array_entry++) {
1562 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1563 continue;
1564
1565 if (be32_to_cpu(error->exposed_mode_adn) == i)
1566 ipr_err("Exposed Array Member %d:\n", i);
1567 else
1568 ipr_err("Array Member %d:\n", i);
1569
1570 ipr_log_ext_vpd(&array_entry->vpd);
1571 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1572 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1573 "Expected Location");
1574
1575 ipr_err_separator;
1576 }
1577}
1578
1579/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 * ipr_log_array_error - Log an array configuration error.
1581 * @ioa_cfg: ioa config struct
1582 * @hostrcb: hostrcb struct
1583 *
1584 * Return value:
1585 * none
1586 **/
1587static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1588 struct ipr_hostrcb *hostrcb)
1589{
1590 int i;
1591 struct ipr_hostrcb_type_04_error *error;
1592 struct ipr_hostrcb_array_data_entry *array_entry;
1593 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1594
1595 error = &hostrcb->hcam.u.error.u.type_04_error;
1596
1597 ipr_err_separator;
1598
1599 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1600 error->protection_level,
1601 ioa_cfg->host->host_no,
1602 error->last_func_vset_res_addr.bus,
1603 error->last_func_vset_res_addr.target,
1604 error->last_func_vset_res_addr.lun);
1605
1606 ipr_err_separator;
1607
1608 array_entry = error->array_member;
1609
1610 for (i = 0; i < 18; i++) {
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001611 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 continue;
1613
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001614 if (be32_to_cpu(error->exposed_mode_adn) == i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 ipr_err("Exposed Array Member %d:\n", i);
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001616 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 ipr_err("Array Member %d:\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001619 ipr_log_vpd(&array_entry->vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001621 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1622 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1623 "Expected Location");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 ipr_err_separator;
1626
1627 if (i == 9)
1628 array_entry = error->array_member2;
1629 else
1630 array_entry++;
1631 }
1632}
1633
1634/**
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001635 * ipr_log_hex_data - Log additional hex IOA error data.
Brian Kingac719ab2006-11-21 10:28:42 -06001636 * @ioa_cfg: ioa config struct
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001637 * @data: IOA error data
1638 * @len: data length
1639 *
1640 * Return value:
1641 * none
1642 **/
Brian Kingac719ab2006-11-21 10:28:42 -06001643static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001644{
1645 int i;
1646
1647 if (len == 0)
1648 return;
1649
Brian Kingac719ab2006-11-21 10:28:42 -06001650 if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1651 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1652
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001653 for (i = 0; i < len / 4; i += 4) {
1654 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1655 be32_to_cpu(data[i]),
1656 be32_to_cpu(data[i+1]),
1657 be32_to_cpu(data[i+2]),
1658 be32_to_cpu(data[i+3]));
1659 }
1660}
1661
1662/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001663 * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1664 * @ioa_cfg: ioa config struct
1665 * @hostrcb: hostrcb struct
1666 *
1667 * Return value:
1668 * none
1669 **/
1670static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1671 struct ipr_hostrcb *hostrcb)
1672{
1673 struct ipr_hostrcb_type_17_error *error;
1674
1675 error = &hostrcb->hcam.u.error.u.type_17_error;
1676 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
KOSAKI Motohiroca54cb82009-12-14 18:01:15 -08001677 strim(error->failure_reason);
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001678
Brian King8cf093e2007-04-26 16:00:14 -05001679 ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1680 be32_to_cpu(hostrcb->hcam.u.error.prc));
1681 ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
Brian Kingac719ab2006-11-21 10:28:42 -06001682 ipr_log_hex_data(ioa_cfg, error->data,
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001683 be32_to_cpu(hostrcb->hcam.length) -
1684 (offsetof(struct ipr_hostrcb_error, u) +
1685 offsetof(struct ipr_hostrcb_type_17_error, data)));
1686}
1687
1688/**
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001689 * ipr_log_dual_ioa_error - Log a dual adapter error.
1690 * @ioa_cfg: ioa config struct
1691 * @hostrcb: hostrcb struct
1692 *
1693 * Return value:
1694 * none
1695 **/
1696static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1697 struct ipr_hostrcb *hostrcb)
1698{
1699 struct ipr_hostrcb_type_07_error *error;
1700
1701 error = &hostrcb->hcam.u.error.u.type_07_error;
1702 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
KOSAKI Motohiroca54cb82009-12-14 18:01:15 -08001703 strim(error->failure_reason);
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001704
Brian King8cf093e2007-04-26 16:00:14 -05001705 ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1706 be32_to_cpu(hostrcb->hcam.u.error.prc));
1707 ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
Brian Kingac719ab2006-11-21 10:28:42 -06001708 ipr_log_hex_data(ioa_cfg, error->data,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001709 be32_to_cpu(hostrcb->hcam.length) -
1710 (offsetof(struct ipr_hostrcb_error, u) +
1711 offsetof(struct ipr_hostrcb_type_07_error, data)));
1712}
1713
Brian King49dc6a12006-11-21 10:28:35 -06001714static const struct {
1715 u8 active;
1716 char *desc;
1717} path_active_desc[] = {
1718 { IPR_PATH_NO_INFO, "Path" },
1719 { IPR_PATH_ACTIVE, "Active path" },
1720 { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1721};
1722
1723static const struct {
1724 u8 state;
1725 char *desc;
1726} path_state_desc[] = {
1727 { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1728 { IPR_PATH_HEALTHY, "is healthy" },
1729 { IPR_PATH_DEGRADED, "is degraded" },
1730 { IPR_PATH_FAILED, "is failed" }
1731};
1732
1733/**
1734 * ipr_log_fabric_path - Log a fabric path error
1735 * @hostrcb: hostrcb struct
1736 * @fabric: fabric descriptor
1737 *
1738 * Return value:
1739 * none
1740 **/
1741static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1742 struct ipr_hostrcb_fabric_desc *fabric)
1743{
1744 int i, j;
1745 u8 path_state = fabric->path_state;
1746 u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1747 u8 state = path_state & IPR_PATH_STATE_MASK;
1748
1749 for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1750 if (path_active_desc[i].active != active)
1751 continue;
1752
1753 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1754 if (path_state_desc[j].state != state)
1755 continue;
1756
1757 if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
1758 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
1759 path_active_desc[i].desc, path_state_desc[j].desc,
1760 fabric->ioa_port);
1761 } else if (fabric->cascaded_expander == 0xff) {
1762 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
1763 path_active_desc[i].desc, path_state_desc[j].desc,
1764 fabric->ioa_port, fabric->phy);
1765 } else if (fabric->phy == 0xff) {
1766 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
1767 path_active_desc[i].desc, path_state_desc[j].desc,
1768 fabric->ioa_port, fabric->cascaded_expander);
1769 } else {
1770 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
1771 path_active_desc[i].desc, path_state_desc[j].desc,
1772 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1773 }
1774 return;
1775 }
1776 }
1777
1778 ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
1779 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1780}
1781
1782static const struct {
1783 u8 type;
1784 char *desc;
1785} path_type_desc[] = {
1786 { IPR_PATH_CFG_IOA_PORT, "IOA port" },
1787 { IPR_PATH_CFG_EXP_PORT, "Expander port" },
1788 { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
1789 { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
1790};
1791
1792static const struct {
1793 u8 status;
1794 char *desc;
1795} path_status_desc[] = {
1796 { IPR_PATH_CFG_NO_PROB, "Functional" },
1797 { IPR_PATH_CFG_DEGRADED, "Degraded" },
1798 { IPR_PATH_CFG_FAILED, "Failed" },
1799 { IPR_PATH_CFG_SUSPECT, "Suspect" },
1800 { IPR_PATH_NOT_DETECTED, "Missing" },
1801 { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
1802};
1803
1804static const char *link_rate[] = {
1805 "unknown",
1806 "disabled",
1807 "phy reset problem",
1808 "spinup hold",
1809 "port selector",
1810 "unknown",
1811 "unknown",
1812 "unknown",
1813 "1.5Gbps",
1814 "3.0Gbps",
1815 "unknown",
1816 "unknown",
1817 "unknown",
1818 "unknown",
1819 "unknown",
1820 "unknown"
1821};
1822
1823/**
1824 * ipr_log_path_elem - Log a fabric path element.
1825 * @hostrcb: hostrcb struct
1826 * @cfg: fabric path element struct
1827 *
1828 * Return value:
1829 * none
1830 **/
1831static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
1832 struct ipr_hostrcb_config_element *cfg)
1833{
1834 int i, j;
1835 u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
1836 u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
1837
1838 if (type == IPR_PATH_CFG_NOT_EXIST)
1839 return;
1840
1841 for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
1842 if (path_type_desc[i].type != type)
1843 continue;
1844
1845 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
1846 if (path_status_desc[j].status != status)
1847 continue;
1848
1849 if (type == IPR_PATH_CFG_IOA_PORT) {
1850 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
1851 path_status_desc[j].desc, path_type_desc[i].desc,
1852 cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1853 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1854 } else {
1855 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
1856 ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
1857 path_status_desc[j].desc, path_type_desc[i].desc,
1858 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1859 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1860 } else if (cfg->cascaded_expander == 0xff) {
1861 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
1862 "WWN=%08X%08X\n", path_status_desc[j].desc,
1863 path_type_desc[i].desc, cfg->phy,
1864 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1865 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1866 } else if (cfg->phy == 0xff) {
1867 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
1868 "WWN=%08X%08X\n", path_status_desc[j].desc,
1869 path_type_desc[i].desc, cfg->cascaded_expander,
1870 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1871 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1872 } else {
1873 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
1874 "WWN=%08X%08X\n", path_status_desc[j].desc,
1875 path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
1876 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1877 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1878 }
1879 }
1880 return;
1881 }
1882 }
1883
1884 ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
1885 "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
1886 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1887 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1888}
1889
1890/**
1891 * ipr_log_fabric_error - Log a fabric error.
1892 * @ioa_cfg: ioa config struct
1893 * @hostrcb: hostrcb struct
1894 *
1895 * Return value:
1896 * none
1897 **/
1898static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
1899 struct ipr_hostrcb *hostrcb)
1900{
1901 struct ipr_hostrcb_type_20_error *error;
1902 struct ipr_hostrcb_fabric_desc *fabric;
1903 struct ipr_hostrcb_config_element *cfg;
1904 int i, add_len;
1905
1906 error = &hostrcb->hcam.u.error.u.type_20_error;
1907 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1908 ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
1909
1910 add_len = be32_to_cpu(hostrcb->hcam.length) -
1911 (offsetof(struct ipr_hostrcb_error, u) +
1912 offsetof(struct ipr_hostrcb_type_20_error, desc));
1913
1914 for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
1915 ipr_log_fabric_path(hostrcb, fabric);
1916 for_each_fabric_cfg(fabric, cfg)
1917 ipr_log_path_elem(hostrcb, cfg);
1918
1919 add_len -= be16_to_cpu(fabric->length);
1920 fabric = (struct ipr_hostrcb_fabric_desc *)
1921 ((unsigned long)fabric + be16_to_cpu(fabric->length));
1922 }
1923
Brian Kingac719ab2006-11-21 10:28:42 -06001924 ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
Brian King49dc6a12006-11-21 10:28:35 -06001925}
1926
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001927/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 * ipr_log_generic_error - Log an adapter error.
1929 * @ioa_cfg: ioa config struct
1930 * @hostrcb: hostrcb struct
1931 *
1932 * Return value:
1933 * none
1934 **/
1935static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1936 struct ipr_hostrcb *hostrcb)
1937{
Brian Kingac719ab2006-11-21 10:28:42 -06001938 ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001939 be32_to_cpu(hostrcb->hcam.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
1942/**
1943 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1944 * @ioasc: IOASC
1945 *
1946 * This function will return the index of into the ipr_error_table
1947 * for the specified IOASC. If the IOASC is not in the table,
1948 * 0 will be returned, which points to the entry used for unknown errors.
1949 *
1950 * Return value:
1951 * index into the ipr_error_table
1952 **/
1953static u32 ipr_get_error(u32 ioasc)
1954{
1955 int i;
1956
1957 for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
Brian King35a39692006-09-25 12:39:20 -05001958 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return i;
1960
1961 return 0;
1962}
1963
1964/**
1965 * ipr_handle_log_data - Log an adapter error.
1966 * @ioa_cfg: ioa config struct
1967 * @hostrcb: hostrcb struct
1968 *
1969 * This function logs an adapter error to the system.
1970 *
1971 * Return value:
1972 * none
1973 **/
1974static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1975 struct ipr_hostrcb *hostrcb)
1976{
1977 u32 ioasc;
1978 int error_index;
1979
1980 if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1981 return;
1982
1983 if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1984 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1985
1986 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1987
1988 if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1989 ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1990 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1991 scsi_report_bus_reset(ioa_cfg->host,
1992 hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1993 }
1994
1995 error_index = ipr_get_error(ioasc);
1996
1997 if (!ipr_error_table[error_index].log_hcam)
1998 return;
1999
Brian King49dc6a12006-11-21 10:28:35 -06002000 ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 /* Set indication we have logged an error */
2003 ioa_cfg->errors_logged++;
2004
Brian King933916f2007-03-29 12:43:30 -05002005 if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return;
brking@us.ibm.comcf852032005-11-01 17:00:47 -06002007 if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
2008 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 switch (hostrcb->hcam.overlay_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 case IPR_HOST_RCB_OVERLAY_ID_2:
2012 ipr_log_cache_error(ioa_cfg, hostrcb);
2013 break;
2014 case IPR_HOST_RCB_OVERLAY_ID_3:
2015 ipr_log_config_error(ioa_cfg, hostrcb);
2016 break;
2017 case IPR_HOST_RCB_OVERLAY_ID_4:
2018 case IPR_HOST_RCB_OVERLAY_ID_6:
2019 ipr_log_array_error(ioa_cfg, hostrcb);
2020 break;
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06002021 case IPR_HOST_RCB_OVERLAY_ID_7:
2022 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
2023 break;
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06002024 case IPR_HOST_RCB_OVERLAY_ID_12:
2025 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
2026 break;
2027 case IPR_HOST_RCB_OVERLAY_ID_13:
2028 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
2029 break;
2030 case IPR_HOST_RCB_OVERLAY_ID_14:
2031 case IPR_HOST_RCB_OVERLAY_ID_16:
2032 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
2033 break;
2034 case IPR_HOST_RCB_OVERLAY_ID_17:
2035 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
2036 break;
Brian King49dc6a12006-11-21 10:28:35 -06002037 case IPR_HOST_RCB_OVERLAY_ID_20:
2038 ipr_log_fabric_error(ioa_cfg, hostrcb);
2039 break;
brking@us.ibm.comcf852032005-11-01 17:00:47 -06002040 case IPR_HOST_RCB_OVERLAY_ID_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 default:
brking@us.ibm.coma9cfca92005-11-01 17:00:41 -06002043 ipr_log_generic_error(ioa_cfg, hostrcb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 break;
2045 }
2046}
2047
2048/**
2049 * ipr_process_error - Op done function for an adapter error log.
2050 * @ipr_cmd: ipr command struct
2051 *
2052 * This function is the op done function for an error log host
2053 * controlled async from the adapter. It will log the error and
2054 * send the HCAM back to the adapter.
2055 *
2056 * Return value:
2057 * none
2058 **/
2059static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
2060{
2061 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2062 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
2063 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
Brian King65f56472007-04-26 16:00:12 -05002064 u32 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 list_del(&hostrcb->queue);
2067 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
2068
2069 if (!ioasc) {
2070 ipr_handle_log_data(ioa_cfg, hostrcb);
Brian King65f56472007-04-26 16:00:12 -05002071 if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
2072 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
2074 dev_err(&ioa_cfg->pdev->dev,
2075 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
2076 }
2077
2078 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
2079}
2080
2081/**
2082 * ipr_timeout - An internally generated op has timed out.
2083 * @ipr_cmd: ipr command struct
2084 *
2085 * This function blocks host requests and initiates an
2086 * adapter reset.
2087 *
2088 * Return value:
2089 * none
2090 **/
2091static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
2092{
2093 unsigned long lock_flags = 0;
2094 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2095
2096 ENTER;
2097 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2098
2099 ioa_cfg->errors_logged++;
2100 dev_err(&ioa_cfg->pdev->dev,
2101 "Adapter being reset due to command timeout.\n");
2102
2103 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2104 ioa_cfg->sdt_state = GET_DUMP;
2105
2106 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
2107 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2108
2109 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2110 LEAVE;
2111}
2112
2113/**
2114 * ipr_oper_timeout - Adapter timed out transitioning to operational
2115 * @ipr_cmd: ipr command struct
2116 *
2117 * This function blocks host requests and initiates an
2118 * adapter reset.
2119 *
2120 * Return value:
2121 * none
2122 **/
2123static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
2124{
2125 unsigned long lock_flags = 0;
2126 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2127
2128 ENTER;
2129 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2130
2131 ioa_cfg->errors_logged++;
2132 dev_err(&ioa_cfg->pdev->dev,
2133 "Adapter timed out transitioning to operational.\n");
2134
2135 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2136 ioa_cfg->sdt_state = GET_DUMP;
2137
2138 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
2139 if (ipr_fastfail)
2140 ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
2141 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2142 }
2143
2144 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2145 LEAVE;
2146}
2147
2148/**
2149 * ipr_reset_reload - Reset/Reload the IOA
2150 * @ioa_cfg: ioa config struct
2151 * @shutdown_type: shutdown type
2152 *
2153 * This function resets the adapter and re-initializes it.
2154 * This function assumes that all new host commands have been stopped.
2155 * Return value:
2156 * SUCCESS / FAILED
2157 **/
2158static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
2159 enum ipr_shutdown_type shutdown_type)
2160{
2161 if (!ioa_cfg->in_reset_reload)
2162 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
2163
2164 spin_unlock_irq(ioa_cfg->host->host_lock);
2165 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2166 spin_lock_irq(ioa_cfg->host->host_lock);
2167
2168 /* If we got hit with a host reset while we were already resetting
2169 the adapter for some reason, and the reset failed. */
2170 if (ioa_cfg->ioa_is_dead) {
2171 ipr_trace;
2172 return FAILED;
2173 }
2174
2175 return SUCCESS;
2176}
2177
2178/**
2179 * ipr_find_ses_entry - Find matching SES in SES table
2180 * @res: resource entry struct of SES
2181 *
2182 * Return value:
2183 * pointer to SES table entry / NULL on failure
2184 **/
2185static const struct ipr_ses_table_entry *
2186ipr_find_ses_entry(struct ipr_resource_entry *res)
2187{
2188 int i, j, matches;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08002189 struct ipr_std_inq_vpids *vpids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 const struct ipr_ses_table_entry *ste = ipr_ses_table;
2191
2192 for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
2193 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
2194 if (ste->compare_product_id_byte[j] == 'X') {
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08002195 vpids = &res->std_inq_data.vpids;
2196 if (vpids->product_id[j] == ste->product_id[j])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 matches++;
2198 else
2199 break;
2200 } else
2201 matches++;
2202 }
2203
2204 if (matches == IPR_PROD_ID_LEN)
2205 return ste;
2206 }
2207
2208 return NULL;
2209}
2210
2211/**
2212 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
2213 * @ioa_cfg: ioa config struct
2214 * @bus: SCSI bus
2215 * @bus_width: bus width
2216 *
2217 * Return value:
2218 * SCSI bus speed in units of 100KHz, 1600 is 160 MHz
2219 * For a 2-byte wide SCSI bus, the maximum transfer speed is
2220 * twice the maximum transfer rate (e.g. for a wide enabled bus,
2221 * max 160MHz = max 320MB/sec).
2222 **/
2223static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
2224{
2225 struct ipr_resource_entry *res;
2226 const struct ipr_ses_table_entry *ste;
2227 u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
2228
2229 /* Loop through each config table entry in the config table buffer */
2230 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08002231 if (!(IPR_IS_SES_DEVICE(res->std_inq_data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 continue;
2233
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08002234 if (bus != res->bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 continue;
2236
2237 if (!(ste = ipr_find_ses_entry(res)))
2238 continue;
2239
2240 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
2241 }
2242
2243 return max_xfer_rate;
2244}
2245
2246/**
2247 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
2248 * @ioa_cfg: ioa config struct
2249 * @max_delay: max delay in micro-seconds to wait
2250 *
2251 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
2252 *
2253 * Return value:
2254 * 0 on success / other on failure
2255 **/
2256static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
2257{
2258 volatile u32 pcii_reg;
2259 int delay = 1;
2260
2261 /* Read interrupt reg until IOA signals IO Debug Acknowledge */
2262 while (delay < max_delay) {
2263 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
2264
2265 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
2266 return 0;
2267
2268 /* udelay cannot be used if delay is more than a few milliseconds */
2269 if ((delay / 1000) > MAX_UDELAY_MS)
2270 mdelay(delay / 1000);
2271 else
2272 udelay(delay);
2273
2274 delay += delay;
2275 }
2276 return -EIO;
2277}
2278
2279/**
2280 * ipr_get_ldump_data_section - Dump IOA memory
2281 * @ioa_cfg: ioa config struct
2282 * @start_addr: adapter address to dump
2283 * @dest: destination kernel buffer
2284 * @length_in_words: length to dump in 4 byte words
2285 *
2286 * Return value:
2287 * 0 on success / -EIO on failure
2288 **/
2289static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2290 u32 start_addr,
2291 __be32 *dest, u32 length_in_words)
2292{
2293 volatile u32 temp_pcii_reg;
2294 int i, delay = 0;
2295
2296 /* Write IOA interrupt reg starting LDUMP state */
2297 writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
2298 ioa_cfg->regs.set_uproc_interrupt_reg);
2299
2300 /* Wait for IO debug acknowledge */
2301 if (ipr_wait_iodbg_ack(ioa_cfg,
2302 IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
2303 dev_err(&ioa_cfg->pdev->dev,
2304 "IOA dump long data transfer timeout\n");
2305 return -EIO;
2306 }
2307
2308 /* Signal LDUMP interlocked - clear IO debug ack */
2309 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2310 ioa_cfg->regs.clr_interrupt_reg);
2311
2312 /* Write Mailbox with starting address */
2313 writel(start_addr, ioa_cfg->ioa_mailbox);
2314
2315 /* Signal address valid - clear IOA Reset alert */
2316 writel(IPR_UPROCI_RESET_ALERT,
2317 ioa_cfg->regs.clr_uproc_interrupt_reg);
2318
2319 for (i = 0; i < length_in_words; i++) {
2320 /* Wait for IO debug acknowledge */
2321 if (ipr_wait_iodbg_ack(ioa_cfg,
2322 IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
2323 dev_err(&ioa_cfg->pdev->dev,
2324 "IOA dump short data transfer timeout\n");
2325 return -EIO;
2326 }
2327
2328 /* Read data from mailbox and increment destination pointer */
2329 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
2330 dest++;
2331
2332 /* For all but the last word of data, signal data received */
2333 if (i < (length_in_words - 1)) {
2334 /* Signal dump data received - Clear IO debug Ack */
2335 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2336 ioa_cfg->regs.clr_interrupt_reg);
2337 }
2338 }
2339
2340 /* Signal end of block transfer. Set reset alert then clear IO debug ack */
2341 writel(IPR_UPROCI_RESET_ALERT,
2342 ioa_cfg->regs.set_uproc_interrupt_reg);
2343
2344 writel(IPR_UPROCI_IO_DEBUG_ALERT,
2345 ioa_cfg->regs.clr_uproc_interrupt_reg);
2346
2347 /* Signal dump data received - Clear IO debug Ack */
2348 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2349 ioa_cfg->regs.clr_interrupt_reg);
2350
2351 /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2352 while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2353 temp_pcii_reg =
2354 readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
2355
2356 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2357 return 0;
2358
2359 udelay(10);
2360 delay += 10;
2361 }
2362
2363 return 0;
2364}
2365
2366#ifdef CONFIG_SCSI_IPR_DUMP
2367/**
2368 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2369 * @ioa_cfg: ioa config struct
2370 * @pci_address: adapter address
2371 * @length: length of data to copy
2372 *
2373 * Copy data from PCI adapter to kernel buffer.
2374 * Note: length MUST be a 4 byte multiple
2375 * Return value:
2376 * 0 on success / other on failure
2377 **/
2378static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2379 unsigned long pci_address, u32 length)
2380{
2381 int bytes_copied = 0;
2382 int cur_len, rc, rem_len, rem_page_len;
2383 __be32 *page;
2384 unsigned long lock_flags = 0;
2385 struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2386
2387 while (bytes_copied < length &&
2388 (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
2389 if (ioa_dump->page_offset >= PAGE_SIZE ||
2390 ioa_dump->page_offset == 0) {
2391 page = (__be32 *)__get_free_page(GFP_ATOMIC);
2392
2393 if (!page) {
2394 ipr_trace;
2395 return bytes_copied;
2396 }
2397
2398 ioa_dump->page_offset = 0;
2399 ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2400 ioa_dump->next_page_index++;
2401 } else
2402 page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2403
2404 rem_len = length - bytes_copied;
2405 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2406 cur_len = min(rem_len, rem_page_len);
2407
2408 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2409 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2410 rc = -EIO;
2411 } else {
2412 rc = ipr_get_ldump_data_section(ioa_cfg,
2413 pci_address + bytes_copied,
2414 &page[ioa_dump->page_offset / 4],
2415 (cur_len / sizeof(u32)));
2416 }
2417 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2418
2419 if (!rc) {
2420 ioa_dump->page_offset += cur_len;
2421 bytes_copied += cur_len;
2422 } else {
2423 ipr_trace;
2424 break;
2425 }
2426 schedule();
2427 }
2428
2429 return bytes_copied;
2430}
2431
2432/**
2433 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2434 * @hdr: dump entry header struct
2435 *
2436 * Return value:
2437 * nothing
2438 **/
2439static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2440{
2441 hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2442 hdr->num_elems = 1;
2443 hdr->offset = sizeof(*hdr);
2444 hdr->status = IPR_DUMP_STATUS_SUCCESS;
2445}
2446
2447/**
2448 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2449 * @ioa_cfg: ioa config struct
2450 * @driver_dump: driver dump struct
2451 *
2452 * Return value:
2453 * nothing
2454 **/
2455static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2456 struct ipr_driver_dump *driver_dump)
2457{
2458 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2459
2460 ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2461 driver_dump->ioa_type_entry.hdr.len =
2462 sizeof(struct ipr_dump_ioa_type_entry) -
2463 sizeof(struct ipr_dump_entry_header);
2464 driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2465 driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2466 driver_dump->ioa_type_entry.type = ioa_cfg->type;
2467 driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2468 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2469 ucode_vpd->minor_release[1];
2470 driver_dump->hdr.num_entries++;
2471}
2472
2473/**
2474 * ipr_dump_version_data - Fill in the driver version in the dump.
2475 * @ioa_cfg: ioa config struct
2476 * @driver_dump: driver dump struct
2477 *
2478 * Return value:
2479 * nothing
2480 **/
2481static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2482 struct ipr_driver_dump *driver_dump)
2483{
2484 ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2485 driver_dump->version_entry.hdr.len =
2486 sizeof(struct ipr_dump_version_entry) -
2487 sizeof(struct ipr_dump_entry_header);
2488 driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2489 driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2490 strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2491 driver_dump->hdr.num_entries++;
2492}
2493
2494/**
2495 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2496 * @ioa_cfg: ioa config struct
2497 * @driver_dump: driver dump struct
2498 *
2499 * Return value:
2500 * nothing
2501 **/
2502static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
2503 struct ipr_driver_dump *driver_dump)
2504{
2505 ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
2506 driver_dump->trace_entry.hdr.len =
2507 sizeof(struct ipr_dump_trace_entry) -
2508 sizeof(struct ipr_dump_entry_header);
2509 driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2510 driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
2511 memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
2512 driver_dump->hdr.num_entries++;
2513}
2514
2515/**
2516 * ipr_dump_location_data - Fill in the IOA location in the dump.
2517 * @ioa_cfg: ioa config struct
2518 * @driver_dump: driver dump struct
2519 *
2520 * Return value:
2521 * nothing
2522 **/
2523static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
2524 struct ipr_driver_dump *driver_dump)
2525{
2526 ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
2527 driver_dump->location_entry.hdr.len =
2528 sizeof(struct ipr_dump_location_entry) -
2529 sizeof(struct ipr_dump_entry_header);
2530 driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2531 driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
Kay Sievers71610f52008-12-03 22:41:36 +01002532 strcpy(driver_dump->location_entry.location, dev_name(&ioa_cfg->pdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 driver_dump->hdr.num_entries++;
2534}
2535
2536/**
2537 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
2538 * @ioa_cfg: ioa config struct
2539 * @dump: dump struct
2540 *
2541 * Return value:
2542 * nothing
2543 **/
2544static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
2545{
2546 unsigned long start_addr, sdt_word;
2547 unsigned long lock_flags = 0;
2548 struct ipr_driver_dump *driver_dump = &dump->driver_dump;
2549 struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
2550 u32 num_entries, start_off, end_off;
2551 u32 bytes_to_copy, bytes_copied, rc;
2552 struct ipr_sdt *sdt;
2553 int i;
2554
2555 ENTER;
2556
2557 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2558
2559 if (ioa_cfg->sdt_state != GET_DUMP) {
2560 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2561 return;
2562 }
2563
2564 start_addr = readl(ioa_cfg->ioa_mailbox);
2565
2566 if (!ipr_sdt_is_fmt2(start_addr)) {
2567 dev_err(&ioa_cfg->pdev->dev,
2568 "Invalid dump table format: %lx\n", start_addr);
2569 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2570 return;
2571 }
2572
2573 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
2574
2575 driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
2576
2577 /* Initialize the overall dump header */
2578 driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
2579 driver_dump->hdr.num_entries = 1;
2580 driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
2581 driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
2582 driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
2583 driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
2584
2585 ipr_dump_version_data(ioa_cfg, driver_dump);
2586 ipr_dump_location_data(ioa_cfg, driver_dump);
2587 ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
2588 ipr_dump_trace_data(ioa_cfg, driver_dump);
2589
2590 /* Update dump_header */
2591 driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
2592
2593 /* IOA Dump entry */
2594 ipr_init_dump_entry_hdr(&ioa_dump->hdr);
2595 ioa_dump->format = IPR_SDT_FMT2;
2596 ioa_dump->hdr.len = 0;
2597 ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2598 ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
2599
2600 /* First entries in sdt are actually a list of dump addresses and
2601 lengths to gather the real dump data. sdt represents the pointer
2602 to the ioa generated dump table. Dump data will be extracted based
2603 on entries in this table */
2604 sdt = &ioa_dump->sdt;
2605
2606 rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
2607 sizeof(struct ipr_sdt) / sizeof(__be32));
2608
2609 /* Smart Dump table is ready to use and the first entry is valid */
2610 if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
2611 dev_err(&ioa_cfg->pdev->dev,
2612 "Dump of IOA failed. Dump table not valid: %d, %X.\n",
2613 rc, be32_to_cpu(sdt->hdr.state));
2614 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
2615 ioa_cfg->sdt_state = DUMP_OBTAINED;
2616 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2617 return;
2618 }
2619
2620 num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
2621
2622 if (num_entries > IPR_NUM_SDT_ENTRIES)
2623 num_entries = IPR_NUM_SDT_ENTRIES;
2624
2625 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2626
2627 for (i = 0; i < num_entries; i++) {
2628 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
2629 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2630 break;
2631 }
2632
2633 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
2634 sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
2635 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
2636 end_off = be32_to_cpu(sdt->entry[i].end_offset);
2637
2638 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
2639 bytes_to_copy = end_off - start_off;
2640 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
2641 sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
2642 continue;
2643 }
2644
2645 /* Copy data from adapter to driver buffers */
2646 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
2647 bytes_to_copy);
2648
2649 ioa_dump->hdr.len += bytes_copied;
2650
2651 if (bytes_copied != bytes_to_copy) {
2652 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2653 break;
2654 }
2655 }
2656 }
2657 }
2658
2659 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
2660
2661 /* Update dump_header */
2662 driver_dump->hdr.len += ioa_dump->hdr.len;
2663 wmb();
2664 ioa_cfg->sdt_state = DUMP_OBTAINED;
2665 LEAVE;
2666}
2667
2668#else
2669#define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
2670#endif
2671
2672/**
2673 * ipr_release_dump - Free adapter dump memory
2674 * @kref: kref struct
2675 *
2676 * Return value:
2677 * nothing
2678 **/
2679static void ipr_release_dump(struct kref *kref)
2680{
2681 struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
2682 struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
2683 unsigned long lock_flags = 0;
2684 int i;
2685
2686 ENTER;
2687 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2688 ioa_cfg->dump = NULL;
2689 ioa_cfg->sdt_state = INACTIVE;
2690 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2691
2692 for (i = 0; i < dump->ioa_dump.next_page_index; i++)
2693 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
2694
2695 kfree(dump);
2696 LEAVE;
2697}
2698
2699/**
2700 * ipr_worker_thread - Worker thread
David Howellsc4028952006-11-22 14:57:56 +00002701 * @work: ioa config struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 *
2703 * Called at task level from a work thread. This function takes care
2704 * of adding and removing device from the mid-layer as configuration
2705 * changes are detected by the adapter.
2706 *
2707 * Return value:
2708 * nothing
2709 **/
David Howellsc4028952006-11-22 14:57:56 +00002710static void ipr_worker_thread(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
2712 unsigned long lock_flags;
2713 struct ipr_resource_entry *res;
2714 struct scsi_device *sdev;
2715 struct ipr_dump *dump;
David Howellsc4028952006-11-22 14:57:56 +00002716 struct ipr_ioa_cfg *ioa_cfg =
2717 container_of(work, struct ipr_ioa_cfg, work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 u8 bus, target, lun;
2719 int did_work;
2720
2721 ENTER;
2722 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2723
2724 if (ioa_cfg->sdt_state == GET_DUMP) {
2725 dump = ioa_cfg->dump;
2726 if (!dump) {
2727 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2728 return;
2729 }
2730 kref_get(&dump->kref);
2731 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2732 ipr_get_ioa_dump(ioa_cfg, dump);
2733 kref_put(&dump->kref, ipr_release_dump);
2734
2735 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2736 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
2737 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2738 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2739 return;
2740 }
2741
2742restart:
2743 do {
2744 did_work = 0;
2745 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
2746 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2747 return;
2748 }
2749
2750 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2751 if (res->del_from_ml && res->sdev) {
2752 did_work = 1;
2753 sdev = res->sdev;
2754 if (!scsi_device_get(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
2756 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2757 scsi_remove_device(sdev);
2758 scsi_device_put(sdev);
2759 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2760 }
2761 break;
2762 }
2763 }
2764 } while(did_work);
2765
2766 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2767 if (res->add_to_ml) {
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08002768 bus = res->bus;
2769 target = res->target;
2770 lun = res->lun;
Brian King1121b792006-03-29 09:37:16 -06002771 res->add_to_ml = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2773 scsi_add_device(ioa_cfg->host, bus, target, lun);
2774 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2775 goto restart;
2776 }
2777 }
2778
2779 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
Tony Jonesee959b02008-02-22 00:13:36 +01002780 kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 LEAVE;
2782}
2783
2784#ifdef CONFIG_SCSI_IPR_TRACE
2785/**
2786 * ipr_read_trace - Dump the adapter trace
2787 * @kobj: kobject struct
Zhang Rui91a69022007-06-09 13:57:22 +08002788 * @bin_attr: bin_attribute struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 * @buf: buffer
2790 * @off: offset
2791 * @count: buffer size
2792 *
2793 * Return value:
2794 * number of bytes printed to buffer
2795 **/
Zhang Rui91a69022007-06-09 13:57:22 +08002796static ssize_t ipr_read_trace(struct kobject *kobj,
2797 struct bin_attribute *bin_attr,
2798 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
Tony Jonesee959b02008-02-22 00:13:36 +01002800 struct device *dev = container_of(kobj, struct device, kobj);
2801 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2803 unsigned long lock_flags = 0;
Akinobu Mitad777aaf2008-09-22 14:56:47 -07002804 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Akinobu Mitad777aaf2008-09-22 14:56:47 -07002807 ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
2808 IPR_TRACE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
Akinobu Mitad777aaf2008-09-22 14:56:47 -07002810
2811 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
2814static struct bin_attribute ipr_trace_attr = {
2815 .attr = {
2816 .name = "trace",
2817 .mode = S_IRUGO,
2818 },
2819 .size = 0,
2820 .read = ipr_read_trace,
2821};
2822#endif
2823
brking@us.ibm.com62275042005-11-01 17:01:14 -06002824static const struct {
2825 enum ipr_cache_state state;
2826 char *name;
2827} cache_state [] = {
2828 { CACHE_NONE, "none" },
2829 { CACHE_DISABLED, "disabled" },
2830 { CACHE_ENABLED, "enabled" }
2831};
2832
2833/**
2834 * ipr_show_write_caching - Show the write caching attribute
Tony Jonesee959b02008-02-22 00:13:36 +01002835 * @dev: device struct
2836 * @buf: buffer
brking@us.ibm.com62275042005-11-01 17:01:14 -06002837 *
2838 * Return value:
2839 * number of bytes printed to buffer
2840 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002841static ssize_t ipr_show_write_caching(struct device *dev,
2842 struct device_attribute *attr, char *buf)
brking@us.ibm.com62275042005-11-01 17:01:14 -06002843{
Tony Jonesee959b02008-02-22 00:13:36 +01002844 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.com62275042005-11-01 17:01:14 -06002845 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2846 unsigned long lock_flags = 0;
2847 int i, len = 0;
2848
2849 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2850 for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2851 if (cache_state[i].state == ioa_cfg->cache_state) {
2852 len = snprintf(buf, PAGE_SIZE, "%s\n", cache_state[i].name);
2853 break;
2854 }
2855 }
2856 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2857 return len;
2858}
2859
2860
2861/**
2862 * ipr_store_write_caching - Enable/disable adapter write cache
Tony Jonesee959b02008-02-22 00:13:36 +01002863 * @dev: device struct
2864 * @buf: buffer
2865 * @count: buffer size
brking@us.ibm.com62275042005-11-01 17:01:14 -06002866 *
2867 * This function will enable/disable adapter write cache.
2868 *
2869 * Return value:
2870 * count on success / other on failure
2871 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002872static ssize_t ipr_store_write_caching(struct device *dev,
2873 struct device_attribute *attr,
2874 const char *buf, size_t count)
brking@us.ibm.com62275042005-11-01 17:01:14 -06002875{
Tony Jonesee959b02008-02-22 00:13:36 +01002876 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.com62275042005-11-01 17:01:14 -06002877 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2878 unsigned long lock_flags = 0;
2879 enum ipr_cache_state new_state = CACHE_INVALID;
2880 int i;
2881
2882 if (!capable(CAP_SYS_ADMIN))
2883 return -EACCES;
2884 if (ioa_cfg->cache_state == CACHE_NONE)
2885 return -EINVAL;
2886
2887 for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2888 if (!strncmp(cache_state[i].name, buf, strlen(cache_state[i].name))) {
2889 new_state = cache_state[i].state;
2890 break;
2891 }
2892 }
2893
2894 if (new_state != CACHE_DISABLED && new_state != CACHE_ENABLED)
2895 return -EINVAL;
2896
2897 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2898 if (ioa_cfg->cache_state == new_state) {
2899 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2900 return count;
2901 }
2902
2903 ioa_cfg->cache_state = new_state;
2904 dev_info(&ioa_cfg->pdev->dev, "%s adapter write cache.\n",
2905 new_state == CACHE_ENABLED ? "Enabling" : "Disabling");
2906 if (!ioa_cfg->in_reset_reload)
2907 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2908 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2909 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2910
2911 return count;
2912}
2913
Tony Jonesee959b02008-02-22 00:13:36 +01002914static struct device_attribute ipr_ioa_cache_attr = {
brking@us.ibm.com62275042005-11-01 17:01:14 -06002915 .attr = {
2916 .name = "write_cache",
2917 .mode = S_IRUGO | S_IWUSR,
2918 },
2919 .show = ipr_show_write_caching,
2920 .store = ipr_store_write_caching
2921};
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923/**
2924 * ipr_show_fw_version - Show the firmware version
Tony Jonesee959b02008-02-22 00:13:36 +01002925 * @dev: class device struct
2926 * @buf: buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 *
2928 * Return value:
2929 * number of bytes printed to buffer
2930 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002931static ssize_t ipr_show_fw_version(struct device *dev,
2932 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Tony Jonesee959b02008-02-22 00:13:36 +01002934 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2936 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2937 unsigned long lock_flags = 0;
2938 int len;
2939
2940 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2941 len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
2942 ucode_vpd->major_release, ucode_vpd->card_type,
2943 ucode_vpd->minor_release[0],
2944 ucode_vpd->minor_release[1]);
2945 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2946 return len;
2947}
2948
Tony Jonesee959b02008-02-22 00:13:36 +01002949static struct device_attribute ipr_fw_version_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 .attr = {
2951 .name = "fw_version",
2952 .mode = S_IRUGO,
2953 },
2954 .show = ipr_show_fw_version,
2955};
2956
2957/**
2958 * ipr_show_log_level - Show the adapter's error logging level
Tony Jonesee959b02008-02-22 00:13:36 +01002959 * @dev: class device struct
2960 * @buf: buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 *
2962 * Return value:
2963 * number of bytes printed to buffer
2964 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002965static ssize_t ipr_show_log_level(struct device *dev,
2966 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Tony Jonesee959b02008-02-22 00:13:36 +01002968 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2970 unsigned long lock_flags = 0;
2971 int len;
2972
2973 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2974 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
2975 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2976 return len;
2977}
2978
2979/**
2980 * ipr_store_log_level - Change the adapter's error logging level
Tony Jonesee959b02008-02-22 00:13:36 +01002981 * @dev: class device struct
2982 * @buf: buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 *
2984 * Return value:
2985 * number of bytes printed to buffer
2986 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002987static ssize_t ipr_store_log_level(struct device *dev,
2988 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 const char *buf, size_t count)
2990{
Tony Jonesee959b02008-02-22 00:13:36 +01002991 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2993 unsigned long lock_flags = 0;
2994
2995 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2996 ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
2997 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2998 return strlen(buf);
2999}
3000
Tony Jonesee959b02008-02-22 00:13:36 +01003001static struct device_attribute ipr_log_level_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 .attr = {
3003 .name = "log_level",
3004 .mode = S_IRUGO | S_IWUSR,
3005 },
3006 .show = ipr_show_log_level,
3007 .store = ipr_store_log_level
3008};
3009
3010/**
3011 * ipr_store_diagnostics - IOA Diagnostics interface
Tony Jonesee959b02008-02-22 00:13:36 +01003012 * @dev: device struct
3013 * @buf: buffer
3014 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 *
3016 * This function will reset the adapter and wait a reasonable
3017 * amount of time for any errors that the adapter might log.
3018 *
3019 * Return value:
3020 * count on success / other on failure
3021 **/
Tony Jonesee959b02008-02-22 00:13:36 +01003022static ssize_t ipr_store_diagnostics(struct device *dev,
3023 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 const char *buf, size_t count)
3025{
Tony Jonesee959b02008-02-22 00:13:36 +01003026 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3028 unsigned long lock_flags = 0;
3029 int rc = count;
3030
3031 if (!capable(CAP_SYS_ADMIN))
3032 return -EACCES;
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05003035 while(ioa_cfg->in_reset_reload) {
3036 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3037 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3038 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3039 }
3040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 ioa_cfg->errors_logged = 0;
3042 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3043
3044 if (ioa_cfg->in_reset_reload) {
3045 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3046 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3047
3048 /* Wait for a second for any errors to be logged */
3049 msleep(1000);
3050 } else {
3051 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3052 return -EIO;
3053 }
3054
3055 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3056 if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
3057 rc = -EIO;
3058 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3059
3060 return rc;
3061}
3062
Tony Jonesee959b02008-02-22 00:13:36 +01003063static struct device_attribute ipr_diagnostics_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 .attr = {
3065 .name = "run_diagnostics",
3066 .mode = S_IWUSR,
3067 },
3068 .store = ipr_store_diagnostics
3069};
3070
3071/**
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003072 * ipr_show_adapter_state - Show the adapter's state
Tony Jonesee959b02008-02-22 00:13:36 +01003073 * @class_dev: device struct
3074 * @buf: buffer
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003075 *
3076 * Return value:
3077 * number of bytes printed to buffer
3078 **/
Tony Jonesee959b02008-02-22 00:13:36 +01003079static ssize_t ipr_show_adapter_state(struct device *dev,
3080 struct device_attribute *attr, char *buf)
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003081{
Tony Jonesee959b02008-02-22 00:13:36 +01003082 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003083 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3084 unsigned long lock_flags = 0;
3085 int len;
3086
3087 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3088 if (ioa_cfg->ioa_is_dead)
3089 len = snprintf(buf, PAGE_SIZE, "offline\n");
3090 else
3091 len = snprintf(buf, PAGE_SIZE, "online\n");
3092 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3093 return len;
3094}
3095
3096/**
3097 * ipr_store_adapter_state - Change adapter state
Tony Jonesee959b02008-02-22 00:13:36 +01003098 * @dev: device struct
3099 * @buf: buffer
3100 * @count: buffer size
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003101 *
3102 * This function will change the adapter's state.
3103 *
3104 * Return value:
3105 * count on success / other on failure
3106 **/
Tony Jonesee959b02008-02-22 00:13:36 +01003107static ssize_t ipr_store_adapter_state(struct device *dev,
3108 struct device_attribute *attr,
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003109 const char *buf, size_t count)
3110{
Tony Jonesee959b02008-02-22 00:13:36 +01003111 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003112 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3113 unsigned long lock_flags;
3114 int result = count;
3115
3116 if (!capable(CAP_SYS_ADMIN))
3117 return -EACCES;
3118
3119 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3120 if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
3121 ioa_cfg->ioa_is_dead = 0;
3122 ioa_cfg->reset_retries = 0;
3123 ioa_cfg->in_ioa_bringdown = 0;
3124 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3125 }
3126 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3127 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3128
3129 return result;
3130}
3131
Tony Jonesee959b02008-02-22 00:13:36 +01003132static struct device_attribute ipr_ioa_state_attr = {
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003133 .attr = {
Brian King49dd0962008-04-28 17:36:20 -05003134 .name = "online_state",
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003135 .mode = S_IRUGO | S_IWUSR,
3136 },
3137 .show = ipr_show_adapter_state,
3138 .store = ipr_store_adapter_state
3139};
3140
3141/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 * ipr_store_reset_adapter - Reset the adapter
Tony Jonesee959b02008-02-22 00:13:36 +01003143 * @dev: device struct
3144 * @buf: buffer
3145 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 *
3147 * This function will reset the adapter.
3148 *
3149 * Return value:
3150 * count on success / other on failure
3151 **/
Tony Jonesee959b02008-02-22 00:13:36 +01003152static ssize_t ipr_store_reset_adapter(struct device *dev,
3153 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 const char *buf, size_t count)
3155{
Tony Jonesee959b02008-02-22 00:13:36 +01003156 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3158 unsigned long lock_flags;
3159 int result = count;
3160
3161 if (!capable(CAP_SYS_ADMIN))
3162 return -EACCES;
3163
3164 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3165 if (!ioa_cfg->in_reset_reload)
3166 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3167 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3168 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3169
3170 return result;
3171}
3172
Tony Jonesee959b02008-02-22 00:13:36 +01003173static struct device_attribute ipr_ioa_reset_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 .attr = {
3175 .name = "reset_host",
3176 .mode = S_IWUSR,
3177 },
3178 .store = ipr_store_reset_adapter
3179};
3180
3181/**
3182 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
3183 * @buf_len: buffer length
3184 *
3185 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3186 * list to use for microcode download
3187 *
3188 * Return value:
3189 * pointer to sglist / NULL on failure
3190 **/
3191static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
3192{
3193 int sg_size, order, bsize_elem, num_elem, i, j;
3194 struct ipr_sglist *sglist;
3195 struct scatterlist *scatterlist;
3196 struct page *page;
3197
3198 /* Get the minimum size per scatter/gather element */
3199 sg_size = buf_len / (IPR_MAX_SGLIST - 1);
3200
3201 /* Get the actual size per element */
3202 order = get_order(sg_size);
3203
3204 /* Determine the actual number of bytes per element */
3205 bsize_elem = PAGE_SIZE * (1 << order);
3206
3207 /* Determine the actual number of sg entries needed */
3208 if (buf_len % bsize_elem)
3209 num_elem = (buf_len / bsize_elem) + 1;
3210 else
3211 num_elem = buf_len / bsize_elem;
3212
3213 /* Allocate a scatter/gather list for the DMA */
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06003214 sglist = kzalloc(sizeof(struct ipr_sglist) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 (sizeof(struct scatterlist) * (num_elem - 1)),
3216 GFP_KERNEL);
3217
3218 if (sglist == NULL) {
3219 ipr_trace;
3220 return NULL;
3221 }
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 scatterlist = sglist->scatterlist;
Jens Axboe45711f12007-10-22 21:19:53 +02003224 sg_init_table(scatterlist, num_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
3226 sglist->order = order;
3227 sglist->num_sg = num_elem;
3228
3229 /* Allocate a bunch of sg elements */
3230 for (i = 0; i < num_elem; i++) {
3231 page = alloc_pages(GFP_KERNEL, order);
3232 if (!page) {
3233 ipr_trace;
3234
3235 /* Free up what we already allocated */
3236 for (j = i - 1; j >= 0; j--)
Jens Axboe45711f12007-10-22 21:19:53 +02003237 __free_pages(sg_page(&scatterlist[j]), order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 kfree(sglist);
3239 return NULL;
3240 }
3241
Jens Axboe642f1492007-10-24 11:20:47 +02003242 sg_set_page(&scatterlist[i], page, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
3244
3245 return sglist;
3246}
3247
3248/**
3249 * ipr_free_ucode_buffer - Frees a microcode download buffer
3250 * @p_dnld: scatter/gather list pointer
3251 *
3252 * Free a DMA'able ucode download buffer previously allocated with
3253 * ipr_alloc_ucode_buffer
3254 *
3255 * Return value:
3256 * nothing
3257 **/
3258static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
3259{
3260 int i;
3261
3262 for (i = 0; i < sglist->num_sg; i++)
Jens Axboe45711f12007-10-22 21:19:53 +02003263 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
3265 kfree(sglist);
3266}
3267
3268/**
3269 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
3270 * @sglist: scatter/gather list pointer
3271 * @buffer: buffer pointer
3272 * @len: buffer length
3273 *
3274 * Copy a microcode image from a user buffer into a buffer allocated by
3275 * ipr_alloc_ucode_buffer
3276 *
3277 * Return value:
3278 * 0 on success / other on failure
3279 **/
3280static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
3281 u8 *buffer, u32 len)
3282{
3283 int bsize_elem, i, result = 0;
3284 struct scatterlist *scatterlist;
3285 void *kaddr;
3286
3287 /* Determine the actual number of bytes per element */
3288 bsize_elem = PAGE_SIZE * (1 << sglist->order);
3289
3290 scatterlist = sglist->scatterlist;
3291
3292 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
Jens Axboe45711f12007-10-22 21:19:53 +02003293 struct page *page = sg_page(&scatterlist[i]);
3294
3295 kaddr = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 memcpy(kaddr, buffer, bsize_elem);
Jens Axboe45711f12007-10-22 21:19:53 +02003297 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
3299 scatterlist[i].length = bsize_elem;
3300
3301 if (result != 0) {
3302 ipr_trace;
3303 return result;
3304 }
3305 }
3306
3307 if (len % bsize_elem) {
Jens Axboe45711f12007-10-22 21:19:53 +02003308 struct page *page = sg_page(&scatterlist[i]);
3309
3310 kaddr = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 memcpy(kaddr, buffer, len % bsize_elem);
Jens Axboe45711f12007-10-22 21:19:53 +02003312 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
3314 scatterlist[i].length = len % bsize_elem;
3315 }
3316
3317 sglist->buffer_len = len;
3318 return result;
3319}
3320
3321/**
Wayne Boyera32c0552010-02-19 13:23:36 -08003322 * ipr_build_ucode_ioadl64 - Build a microcode download IOADL
3323 * @ipr_cmd: ipr command struct
3324 * @sglist: scatter/gather list
3325 *
3326 * Builds a microcode download IOA data list (IOADL).
3327 *
3328 **/
3329static void ipr_build_ucode_ioadl64(struct ipr_cmnd *ipr_cmd,
3330 struct ipr_sglist *sglist)
3331{
3332 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3333 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
3334 struct scatterlist *scatterlist = sglist->scatterlist;
3335 int i;
3336
3337 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3338 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3339 ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3340
3341 ioarcb->ioadl_len =
3342 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
3343 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3344 ioadl64[i].flags = cpu_to_be32(IPR_IOADL_FLAGS_WRITE);
3345 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(&scatterlist[i]));
3346 ioadl64[i].address = cpu_to_be64(sg_dma_address(&scatterlist[i]));
3347 }
3348
3349 ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3350}
3351
3352/**
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003353 * ipr_build_ucode_ioadl - Build a microcode download IOADL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 * @ipr_cmd: ipr command struct
3355 * @sglist: scatter/gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 *
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003357 * Builds a microcode download IOA data list (IOADL).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 **/
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003360static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
3361 struct ipr_sglist *sglist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
Wayne Boyera32c0552010-02-19 13:23:36 -08003364 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 struct scatterlist *scatterlist = sglist->scatterlist;
3366 int i;
3367
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003368 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
Wayne Boyera32c0552010-02-19 13:23:36 -08003370 ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3371
3372 ioarcb->ioadl_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3374
3375 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3376 ioadl[i].flags_and_data_len =
3377 cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3378 ioadl[i].address =
3379 cpu_to_be32(sg_dma_address(&scatterlist[i]));
3380 }
3381
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003382 ioadl[i-1].flags_and_data_len |=
3383 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3384}
3385
3386/**
3387 * ipr_update_ioa_ucode - Update IOA's microcode
3388 * @ioa_cfg: ioa config struct
3389 * @sglist: scatter/gather list
3390 *
3391 * Initiate an adapter reset to update the IOA's microcode
3392 *
3393 * Return value:
3394 * 0 on success / -EIO on failure
3395 **/
3396static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3397 struct ipr_sglist *sglist)
3398{
3399 unsigned long lock_flags;
3400
3401 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05003402 while(ioa_cfg->in_reset_reload) {
3403 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3404 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3405 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3406 }
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003407
3408 if (ioa_cfg->ucode_sglist) {
3409 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3410 dev_err(&ioa_cfg->pdev->dev,
3411 "Microcode download already in progress\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return -EIO;
3413 }
3414
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003415 sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
3416 sglist->num_sg, DMA_TO_DEVICE);
3417
3418 if (!sglist->num_dma_sg) {
3419 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3420 dev_err(&ioa_cfg->pdev->dev,
3421 "Failed to map microcode download buffer!\n");
3422 return -EIO;
3423 }
3424
3425 ioa_cfg->ucode_sglist = sglist;
3426 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3427 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3428 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3429
3430 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3431 ioa_cfg->ucode_sglist = NULL;
3432 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 return 0;
3434}
3435
3436/**
3437 * ipr_store_update_fw - Update the firmware on the adapter
Tony Jonesee959b02008-02-22 00:13:36 +01003438 * @class_dev: device struct
3439 * @buf: buffer
3440 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 *
3442 * This function will update the firmware on the adapter.
3443 *
3444 * Return value:
3445 * count on success / other on failure
3446 **/
Tony Jonesee959b02008-02-22 00:13:36 +01003447static ssize_t ipr_store_update_fw(struct device *dev,
3448 struct device_attribute *attr,
3449 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
Tony Jonesee959b02008-02-22 00:13:36 +01003451 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3453 struct ipr_ucode_image_header *image_hdr;
3454 const struct firmware *fw_entry;
3455 struct ipr_sglist *sglist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 char fname[100];
3457 char *src;
3458 int len, result, dnld_size;
3459
3460 if (!capable(CAP_SYS_ADMIN))
3461 return -EACCES;
3462
3463 len = snprintf(fname, 99, "%s", buf);
3464 fname[len-1] = '\0';
3465
3466 if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3467 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3468 return -EIO;
3469 }
3470
3471 image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3472
3473 if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
3474 (ioa_cfg->vpd_cbs->page3_data.card_type &&
3475 ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
3476 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
3477 release_firmware(fw_entry);
3478 return -EINVAL;
3479 }
3480
3481 src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3482 dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3483 sglist = ipr_alloc_ucode_buffer(dnld_size);
3484
3485 if (!sglist) {
3486 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3487 release_firmware(fw_entry);
3488 return -ENOMEM;
3489 }
3490
3491 result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3492
3493 if (result) {
3494 dev_err(&ioa_cfg->pdev->dev,
3495 "Microcode buffer copy to DMA buffer failed\n");
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
3498
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003499 result = ipr_update_ioa_ucode(ioa_cfg, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003501 if (!result)
3502 result = count;
3503out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 ipr_free_ucode_buffer(sglist);
3505 release_firmware(fw_entry);
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003506 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
Tony Jonesee959b02008-02-22 00:13:36 +01003509static struct device_attribute ipr_update_fw_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 .attr = {
3511 .name = "update_fw",
3512 .mode = S_IWUSR,
3513 },
3514 .store = ipr_store_update_fw
3515};
3516
Tony Jonesee959b02008-02-22 00:13:36 +01003517static struct device_attribute *ipr_ioa_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 &ipr_fw_version_attr,
3519 &ipr_log_level_attr,
3520 &ipr_diagnostics_attr,
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003521 &ipr_ioa_state_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 &ipr_ioa_reset_attr,
3523 &ipr_update_fw_attr,
brking@us.ibm.com62275042005-11-01 17:01:14 -06003524 &ipr_ioa_cache_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 NULL,
3526};
3527
3528#ifdef CONFIG_SCSI_IPR_DUMP
3529/**
3530 * ipr_read_dump - Dump the adapter
3531 * @kobj: kobject struct
Zhang Rui91a69022007-06-09 13:57:22 +08003532 * @bin_attr: bin_attribute struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 * @buf: buffer
3534 * @off: offset
3535 * @count: buffer size
3536 *
3537 * Return value:
3538 * number of bytes printed to buffer
3539 **/
Zhang Rui91a69022007-06-09 13:57:22 +08003540static ssize_t ipr_read_dump(struct kobject *kobj,
3541 struct bin_attribute *bin_attr,
3542 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
Tony Jonesee959b02008-02-22 00:13:36 +01003544 struct device *cdev = container_of(kobj, struct device, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 struct Scsi_Host *shost = class_to_shost(cdev);
3546 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3547 struct ipr_dump *dump;
3548 unsigned long lock_flags = 0;
3549 char *src;
3550 int len;
3551 size_t rc = count;
3552
3553 if (!capable(CAP_SYS_ADMIN))
3554 return -EACCES;
3555
3556 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3557 dump = ioa_cfg->dump;
3558
3559 if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
3560 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3561 return 0;
3562 }
3563 kref_get(&dump->kref);
3564 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3565
3566 if (off > dump->driver_dump.hdr.len) {
3567 kref_put(&dump->kref, ipr_release_dump);
3568 return 0;
3569 }
3570
3571 if (off + count > dump->driver_dump.hdr.len) {
3572 count = dump->driver_dump.hdr.len - off;
3573 rc = count;
3574 }
3575
3576 if (count && off < sizeof(dump->driver_dump)) {
3577 if (off + count > sizeof(dump->driver_dump))
3578 len = sizeof(dump->driver_dump) - off;
3579 else
3580 len = count;
3581 src = (u8 *)&dump->driver_dump + off;
3582 memcpy(buf, src, len);
3583 buf += len;
3584 off += len;
3585 count -= len;
3586 }
3587
3588 off -= sizeof(dump->driver_dump);
3589
3590 if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
3591 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
3592 len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
3593 else
3594 len = count;
3595 src = (u8 *)&dump->ioa_dump + off;
3596 memcpy(buf, src, len);
3597 buf += len;
3598 off += len;
3599 count -= len;
3600 }
3601
3602 off -= offsetof(struct ipr_ioa_dump, ioa_data);
3603
3604 while (count) {
3605 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
3606 len = PAGE_ALIGN(off) - off;
3607 else
3608 len = count;
3609 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
3610 src += off & ~PAGE_MASK;
3611 memcpy(buf, src, len);
3612 buf += len;
3613 off += len;
3614 count -= len;
3615 }
3616
3617 kref_put(&dump->kref, ipr_release_dump);
3618 return rc;
3619}
3620
3621/**
3622 * ipr_alloc_dump - Prepare for adapter dump
3623 * @ioa_cfg: ioa config struct
3624 *
3625 * Return value:
3626 * 0 on success / other on failure
3627 **/
3628static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
3629{
3630 struct ipr_dump *dump;
3631 unsigned long lock_flags = 0;
3632
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06003633 dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
3635 if (!dump) {
3636 ipr_err("Dump memory allocation failed\n");
3637 return -ENOMEM;
3638 }
3639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 kref_init(&dump->kref);
3641 dump->ioa_cfg = ioa_cfg;
3642
3643 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3644
3645 if (INACTIVE != ioa_cfg->sdt_state) {
3646 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3647 kfree(dump);
3648 return 0;
3649 }
3650
3651 ioa_cfg->dump = dump;
3652 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
3653 if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
3654 ioa_cfg->dump_taken = 1;
3655 schedule_work(&ioa_cfg->work_q);
3656 }
3657 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 return 0;
3660}
3661
3662/**
3663 * ipr_free_dump - Free adapter dump memory
3664 * @ioa_cfg: ioa config struct
3665 *
3666 * Return value:
3667 * 0 on success / other on failure
3668 **/
3669static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
3670{
3671 struct ipr_dump *dump;
3672 unsigned long lock_flags = 0;
3673
3674 ENTER;
3675
3676 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3677 dump = ioa_cfg->dump;
3678 if (!dump) {
3679 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3680 return 0;
3681 }
3682
3683 ioa_cfg->dump = NULL;
3684 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3685
3686 kref_put(&dump->kref, ipr_release_dump);
3687
3688 LEAVE;
3689 return 0;
3690}
3691
3692/**
3693 * ipr_write_dump - Setup dump state of adapter
3694 * @kobj: kobject struct
Zhang Rui91a69022007-06-09 13:57:22 +08003695 * @bin_attr: bin_attribute struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 * @buf: buffer
3697 * @off: offset
3698 * @count: buffer size
3699 *
3700 * Return value:
3701 * number of bytes printed to buffer
3702 **/
Zhang Rui91a69022007-06-09 13:57:22 +08003703static ssize_t ipr_write_dump(struct kobject *kobj,
3704 struct bin_attribute *bin_attr,
3705 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
Tony Jonesee959b02008-02-22 00:13:36 +01003707 struct device *cdev = container_of(kobj, struct device, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 struct Scsi_Host *shost = class_to_shost(cdev);
3709 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3710 int rc;
3711
3712 if (!capable(CAP_SYS_ADMIN))
3713 return -EACCES;
3714
3715 if (buf[0] == '1')
3716 rc = ipr_alloc_dump(ioa_cfg);
3717 else if (buf[0] == '0')
3718 rc = ipr_free_dump(ioa_cfg);
3719 else
3720 return -EINVAL;
3721
3722 if (rc)
3723 return rc;
3724 else
3725 return count;
3726}
3727
3728static struct bin_attribute ipr_dump_attr = {
3729 .attr = {
3730 .name = "dump",
3731 .mode = S_IRUSR | S_IWUSR,
3732 },
3733 .size = 0,
3734 .read = ipr_read_dump,
3735 .write = ipr_write_dump
3736};
3737#else
3738static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
3739#endif
3740
3741/**
3742 * ipr_change_queue_depth - Change the device's queue depth
3743 * @sdev: scsi device struct
3744 * @qdepth: depth to set
Mike Christiee881a172009-10-15 17:46:39 -07003745 * @reason: calling context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 *
3747 * Return value:
3748 * actual depth set
3749 **/
Mike Christiee881a172009-10-15 17:46:39 -07003750static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth,
3751 int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
Brian King35a39692006-09-25 12:39:20 -05003753 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3754 struct ipr_resource_entry *res;
3755 unsigned long lock_flags = 0;
3756
Mike Christiee881a172009-10-15 17:46:39 -07003757 if (reason != SCSI_QDEPTH_DEFAULT)
3758 return -EOPNOTSUPP;
3759
Brian King35a39692006-09-25 12:39:20 -05003760 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3761 res = (struct ipr_resource_entry *)sdev->hostdata;
3762
3763 if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
3764 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
3765 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3768 return sdev->queue_depth;
3769}
3770
3771/**
3772 * ipr_change_queue_type - Change the device's queue type
3773 * @dsev: scsi device struct
3774 * @tag_type: type of tags to use
3775 *
3776 * Return value:
3777 * actual queue type set
3778 **/
3779static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
3780{
3781 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3782 struct ipr_resource_entry *res;
3783 unsigned long lock_flags = 0;
3784
3785 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3786 res = (struct ipr_resource_entry *)sdev->hostdata;
3787
3788 if (res) {
3789 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
3790 /*
3791 * We don't bother quiescing the device here since the
3792 * adapter firmware does it for us.
3793 */
3794 scsi_set_tag_type(sdev, tag_type);
3795
3796 if (tag_type)
3797 scsi_activate_tcq(sdev, sdev->queue_depth);
3798 else
3799 scsi_deactivate_tcq(sdev, sdev->queue_depth);
3800 } else
3801 tag_type = 0;
3802 } else
3803 tag_type = 0;
3804
3805 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3806 return tag_type;
3807}
3808
3809/**
3810 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
3811 * @dev: device struct
3812 * @buf: buffer
3813 *
3814 * Return value:
3815 * number of bytes printed to buffer
3816 **/
Yani Ioannou10523b32005-05-17 06:43:37 -04003817static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
3819 struct scsi_device *sdev = to_scsi_device(dev);
3820 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3821 struct ipr_resource_entry *res;
3822 unsigned long lock_flags = 0;
3823 ssize_t len = -ENXIO;
3824
3825 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3826 res = (struct ipr_resource_entry *)sdev->hostdata;
3827 if (res)
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08003828 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->res_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3830 return len;
3831}
3832
3833static struct device_attribute ipr_adapter_handle_attr = {
3834 .attr = {
3835 .name = "adapter_handle",
3836 .mode = S_IRUSR,
3837 },
3838 .show = ipr_show_adapter_handle
3839};
3840
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08003841/**
3842 * ipr_show_resource_path - Show the resource path for this device.
3843 * @dev: device struct
3844 * @buf: buffer
3845 *
3846 * Return value:
3847 * number of bytes printed to buffer
3848 **/
3849static ssize_t ipr_show_resource_path(struct device *dev, struct device_attribute *attr, char *buf)
3850{
3851 struct scsi_device *sdev = to_scsi_device(dev);
3852 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3853 struct ipr_resource_entry *res;
3854 unsigned long lock_flags = 0;
3855 ssize_t len = -ENXIO;
3856 char buffer[IPR_MAX_RES_PATH_LENGTH];
3857
3858 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3859 res = (struct ipr_resource_entry *)sdev->hostdata;
3860 if (res)
3861 len = snprintf(buf, PAGE_SIZE, "%s\n",
3862 ipr_format_resource_path(&res->res_path[0], &buffer[0]));
3863 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3864 return len;
3865}
3866
3867static struct device_attribute ipr_resource_path_attr = {
3868 .attr = {
3869 .name = "resource_path",
3870 .mode = S_IRUSR,
3871 },
3872 .show = ipr_show_resource_path
3873};
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875static struct device_attribute *ipr_dev_attrs[] = {
3876 &ipr_adapter_handle_attr,
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08003877 &ipr_resource_path_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 NULL,
3879};
3880
3881/**
3882 * ipr_biosparam - Return the HSC mapping
3883 * @sdev: scsi device struct
3884 * @block_device: block device pointer
3885 * @capacity: capacity of the device
3886 * @parm: Array containing returned HSC values.
3887 *
3888 * This function generates the HSC parms that fdisk uses.
3889 * We want to make sure we return something that places partitions
3890 * on 4k boundaries for best performance with the IOA.
3891 *
3892 * Return value:
3893 * 0 on success
3894 **/
3895static int ipr_biosparam(struct scsi_device *sdev,
3896 struct block_device *block_device,
3897 sector_t capacity, int *parm)
3898{
3899 int heads, sectors;
3900 sector_t cylinders;
3901
3902 heads = 128;
3903 sectors = 32;
3904
3905 cylinders = capacity;
3906 sector_div(cylinders, (128 * 32));
3907
3908 /* return result */
3909 parm[0] = heads;
3910 parm[1] = sectors;
3911 parm[2] = cylinders;
3912
3913 return 0;
3914}
3915
3916/**
Brian King35a39692006-09-25 12:39:20 -05003917 * ipr_find_starget - Find target based on bus/target.
3918 * @starget: scsi target struct
3919 *
3920 * Return value:
3921 * resource entry pointer if found / NULL if not found
3922 **/
3923static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
3924{
3925 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3926 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3927 struct ipr_resource_entry *res;
3928
3929 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08003930 if ((res->bus == starget->channel) &&
3931 (res->target == starget->id) &&
3932 (res->lun == 0)) {
Brian King35a39692006-09-25 12:39:20 -05003933 return res;
3934 }
3935 }
3936
3937 return NULL;
3938}
3939
3940static struct ata_port_info sata_port_info;
3941
3942/**
3943 * ipr_target_alloc - Prepare for commands to a SCSI target
3944 * @starget: scsi target struct
3945 *
3946 * If the device is a SATA device, this function allocates an
3947 * ATA port with libata, else it does nothing.
3948 *
3949 * Return value:
3950 * 0 on success / non-0 on failure
3951 **/
3952static int ipr_target_alloc(struct scsi_target *starget)
3953{
3954 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3955 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3956 struct ipr_sata_port *sata_port;
3957 struct ata_port *ap;
3958 struct ipr_resource_entry *res;
3959 unsigned long lock_flags;
3960
3961 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3962 res = ipr_find_starget(starget);
3963 starget->hostdata = NULL;
3964
3965 if (res && ipr_is_gata(res)) {
3966 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3967 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
3968 if (!sata_port)
3969 return -ENOMEM;
3970
3971 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
3972 if (ap) {
3973 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3974 sata_port->ioa_cfg = ioa_cfg;
3975 sata_port->ap = ap;
3976 sata_port->res = res;
3977
3978 res->sata_port = sata_port;
3979 ap->private_data = sata_port;
3980 starget->hostdata = sata_port;
3981 } else {
3982 kfree(sata_port);
3983 return -ENOMEM;
3984 }
3985 }
3986 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3987
3988 return 0;
3989}
3990
3991/**
3992 * ipr_target_destroy - Destroy a SCSI target
3993 * @starget: scsi target struct
3994 *
3995 * If the device was a SATA device, this function frees the libata
3996 * ATA port, else it does nothing.
3997 *
3998 **/
3999static void ipr_target_destroy(struct scsi_target *starget)
4000{
4001 struct ipr_sata_port *sata_port = starget->hostdata;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08004002 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4003 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4004
4005 if (ioa_cfg->sis64) {
4006 if (starget->channel == IPR_ARRAY_VIRTUAL_BUS)
4007 clear_bit(starget->id, ioa_cfg->array_ids);
4008 else if (starget->channel == IPR_VSET_VIRTUAL_BUS)
4009 clear_bit(starget->id, ioa_cfg->vset_ids);
4010 else if (starget->channel == 0)
4011 clear_bit(starget->id, ioa_cfg->target_ids);
4012 }
Brian King35a39692006-09-25 12:39:20 -05004013
4014 if (sata_port) {
4015 starget->hostdata = NULL;
4016 ata_sas_port_destroy(sata_port->ap);
4017 kfree(sata_port);
4018 }
4019}
4020
4021/**
4022 * ipr_find_sdev - Find device based on bus/target/lun.
4023 * @sdev: scsi device struct
4024 *
4025 * Return value:
4026 * resource entry pointer if found / NULL if not found
4027 **/
4028static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
4029{
4030 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4031 struct ipr_resource_entry *res;
4032
4033 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08004034 if ((res->bus == sdev->channel) &&
4035 (res->target == sdev->id) &&
4036 (res->lun == sdev->lun))
Brian King35a39692006-09-25 12:39:20 -05004037 return res;
4038 }
4039
4040 return NULL;
4041}
4042
4043/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 * ipr_slave_destroy - Unconfigure a SCSI device
4045 * @sdev: scsi device struct
4046 *
4047 * Return value:
4048 * nothing
4049 **/
4050static void ipr_slave_destroy(struct scsi_device *sdev)
4051{
4052 struct ipr_resource_entry *res;
4053 struct ipr_ioa_cfg *ioa_cfg;
4054 unsigned long lock_flags = 0;
4055
4056 ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4057
4058 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4059 res = (struct ipr_resource_entry *) sdev->hostdata;
4060 if (res) {
Brian King35a39692006-09-25 12:39:20 -05004061 if (res->sata_port)
4062 ata_port_disable(res->sata_port->ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 sdev->hostdata = NULL;
4064 res->sdev = NULL;
Brian King35a39692006-09-25 12:39:20 -05004065 res->sata_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 }
4067 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4068}
4069
4070/**
4071 * ipr_slave_configure - Configure a SCSI device
4072 * @sdev: scsi device struct
4073 *
4074 * This function configures the specified scsi device.
4075 *
4076 * Return value:
4077 * 0 on success
4078 **/
4079static int ipr_slave_configure(struct scsi_device *sdev)
4080{
4081 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4082 struct ipr_resource_entry *res;
Brian Kingdd406ef2009-04-22 08:58:02 -05004083 struct ata_port *ap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 unsigned long lock_flags = 0;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08004085 char buffer[IPR_MAX_RES_PATH_LENGTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
4087 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4088 res = sdev->hostdata;
4089 if (res) {
4090 if (ipr_is_af_dasd_device(res))
4091 sdev->type = TYPE_RAID;
brking@us.ibm.com0726ce22005-11-01 17:01:01 -06004092 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 sdev->scsi_level = 4;
brking@us.ibm.com0726ce22005-11-01 17:01:01 -06004094 sdev->no_uld_attach = 1;
4095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 if (ipr_is_vset_device(res)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07004097 blk_queue_rq_timeout(sdev->request_queue,
4098 IPR_VSET_RW_TIMEOUT);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05004099 blk_queue_max_hw_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 }
Brian Kinge4fbf442006-03-29 09:37:22 -06004101 if (ipr_is_vset_device(res) || ipr_is_scsi_disk(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 sdev->allow_restart = 1;
Brian Kingdd406ef2009-04-22 08:58:02 -05004103 if (ipr_is_gata(res) && res->sata_port)
4104 ap = res->sata_port->ap;
4105 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4106
4107 if (ap) {
Brian King35a39692006-09-25 12:39:20 -05004108 scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
Brian Kingdd406ef2009-04-22 08:58:02 -05004109 ata_sas_slave_configure(sdev, ap);
4110 } else
Brian King35a39692006-09-25 12:39:20 -05004111 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08004112 if (ioa_cfg->sis64)
4113 sdev_printk(KERN_INFO, sdev, "Resource path: %s\n",
4114 ipr_format_resource_path(&res->res_path[0], &buffer[0]));
Brian Kingdd406ef2009-04-22 08:58:02 -05004115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
4117 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4118 return 0;
4119}
4120
4121/**
Brian King35a39692006-09-25 12:39:20 -05004122 * ipr_ata_slave_alloc - Prepare for commands to a SATA device
4123 * @sdev: scsi device struct
4124 *
4125 * This function initializes an ATA port so that future commands
4126 * sent through queuecommand will work.
4127 *
4128 * Return value:
4129 * 0 on success
4130 **/
4131static int ipr_ata_slave_alloc(struct scsi_device *sdev)
4132{
4133 struct ipr_sata_port *sata_port = NULL;
4134 int rc = -ENXIO;
4135
4136 ENTER;
4137 if (sdev->sdev_target)
4138 sata_port = sdev->sdev_target->hostdata;
4139 if (sata_port)
4140 rc = ata_sas_port_init(sata_port->ap);
4141 if (rc)
4142 ipr_slave_destroy(sdev);
4143
4144 LEAVE;
4145 return rc;
4146}
4147
4148/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 * ipr_slave_alloc - Prepare for commands to a device.
4150 * @sdev: scsi device struct
4151 *
4152 * This function saves a pointer to the resource entry
4153 * in the scsi device struct if the device exists. We
4154 * can then use this pointer in ipr_queuecommand when
4155 * handling new commands.
4156 *
4157 * Return value:
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06004158 * 0 on success / -ENXIO if device does not exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 **/
4160static int ipr_slave_alloc(struct scsi_device *sdev)
4161{
4162 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4163 struct ipr_resource_entry *res;
4164 unsigned long lock_flags;
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06004165 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
4167 sdev->hostdata = NULL;
4168
4169 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4170
Brian King35a39692006-09-25 12:39:20 -05004171 res = ipr_find_sdev(sdev);
4172 if (res) {
4173 res->sdev = sdev;
4174 res->add_to_ml = 0;
4175 res->in_erp = 0;
4176 sdev->hostdata = res;
4177 if (!ipr_is_naca_model(res))
4178 res->needs_sync_complete = 1;
4179 rc = 0;
4180 if (ipr_is_gata(res)) {
4181 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4182 return ipr_ata_slave_alloc(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 }
4184 }
4185
4186 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4187
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06004188 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189}
4190
4191/**
4192 * ipr_eh_host_reset - Reset the host adapter
4193 * @scsi_cmd: scsi command struct
4194 *
4195 * Return value:
4196 * SUCCESS / FAILED
4197 **/
Jeff Garzik df0ae242005-05-28 07:57:14 -04004198static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199{
4200 struct ipr_ioa_cfg *ioa_cfg;
4201 int rc;
4202
4203 ENTER;
4204 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
4205
4206 dev_err(&ioa_cfg->pdev->dev,
4207 "Adapter being reset as a result of error recovery.\n");
4208
4209 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4210 ioa_cfg->sdt_state = GET_DUMP;
4211
4212 rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
4213
4214 LEAVE;
4215 return rc;
4216}
4217
Jeff Garzik df0ae242005-05-28 07:57:14 -04004218static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
4219{
4220 int rc;
4221
4222 spin_lock_irq(cmd->device->host->host_lock);
4223 rc = __ipr_eh_host_reset(cmd);
4224 spin_unlock_irq(cmd->device->host->host_lock);
4225
4226 return rc;
4227}
4228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229/**
Brian Kingc6513092006-03-29 09:37:43 -06004230 * ipr_device_reset - Reset the device
4231 * @ioa_cfg: ioa config struct
4232 * @res: resource entry struct
4233 *
4234 * This function issues a device reset to the affected device.
4235 * If the device is a SCSI device, a LUN reset will be sent
4236 * to the device first. If that does not work, a target reset
Brian King35a39692006-09-25 12:39:20 -05004237 * will be sent. If the device is a SATA device, a PHY reset will
4238 * be sent.
Brian Kingc6513092006-03-29 09:37:43 -06004239 *
4240 * Return value:
4241 * 0 on success / non-zero on failure
4242 **/
4243static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
4244 struct ipr_resource_entry *res)
4245{
4246 struct ipr_cmnd *ipr_cmd;
4247 struct ipr_ioarcb *ioarcb;
4248 struct ipr_cmd_pkt *cmd_pkt;
Brian King35a39692006-09-25 12:39:20 -05004249 struct ipr_ioarcb_ata_regs *regs;
Brian Kingc6513092006-03-29 09:37:43 -06004250 u32 ioasc;
4251
4252 ENTER;
4253 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4254 ioarcb = &ipr_cmd->ioarcb;
4255 cmd_pkt = &ioarcb->cmd_pkt;
Wayne Boyera32c0552010-02-19 13:23:36 -08004256
4257 if (ipr_cmd->ioa_cfg->sis64) {
4258 regs = &ipr_cmd->i.ata_ioadl.regs;
4259 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
4260 } else
4261 regs = &ioarcb->u.add_data.u.regs;
Brian Kingc6513092006-03-29 09:37:43 -06004262
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08004263 ioarcb->res_handle = res->res_handle;
Brian Kingc6513092006-03-29 09:37:43 -06004264 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4265 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
Brian King35a39692006-09-25 12:39:20 -05004266 if (ipr_is_gata(res)) {
4267 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
Wayne Boyera32c0552010-02-19 13:23:36 -08004268 ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(regs->flags));
Brian King35a39692006-09-25 12:39:20 -05004269 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
4270 }
Brian Kingc6513092006-03-29 09:37:43 -06004271
4272 ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4273 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4274 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
Brian King35a39692006-09-25 12:39:20 -05004275 if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET)
4276 memcpy(&res->sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
4277 sizeof(struct ipr_ioasa_gata));
Brian Kingc6513092006-03-29 09:37:43 -06004278
4279 LEAVE;
4280 return (IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0);
4281}
4282
4283/**
Brian King35a39692006-09-25 12:39:20 -05004284 * ipr_sata_reset - Reset the SATA port
Tejun Heocc0680a2007-08-06 18:36:23 +09004285 * @link: SATA link to reset
Brian King35a39692006-09-25 12:39:20 -05004286 * @classes: class of the attached device
4287 *
Tejun Heocc0680a2007-08-06 18:36:23 +09004288 * This function issues a SATA phy reset to the affected ATA link.
Brian King35a39692006-09-25 12:39:20 -05004289 *
4290 * Return value:
4291 * 0 on success / non-zero on failure
4292 **/
Tejun Heocc0680a2007-08-06 18:36:23 +09004293static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
Andrew Morton120bda32007-03-26 02:17:43 -07004294 unsigned long deadline)
Brian King35a39692006-09-25 12:39:20 -05004295{
Tejun Heocc0680a2007-08-06 18:36:23 +09004296 struct ipr_sata_port *sata_port = link->ap->private_data;
Brian King35a39692006-09-25 12:39:20 -05004297 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4298 struct ipr_resource_entry *res;
4299 unsigned long lock_flags = 0;
4300 int rc = -ENXIO;
4301
4302 ENTER;
4303 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King73d98ff2006-11-21 10:27:58 -06004304 while(ioa_cfg->in_reset_reload) {
4305 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4306 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4307 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4308 }
4309
Brian King35a39692006-09-25 12:39:20 -05004310 res = sata_port->res;
4311 if (res) {
4312 rc = ipr_device_reset(ioa_cfg, res);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08004313 *classes = res->ata_class;
Brian King35a39692006-09-25 12:39:20 -05004314 }
4315
4316 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4317 LEAVE;
4318 return rc;
4319}
4320
4321/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 * ipr_eh_dev_reset - Reset the device
4323 * @scsi_cmd: scsi command struct
4324 *
4325 * This function issues a device reset to the affected device.
4326 * A LUN reset will be sent to the device first. If that does
4327 * not work, a target reset will be sent.
4328 *
4329 * Return value:
4330 * SUCCESS / FAILED
4331 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04004332static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333{
4334 struct ipr_cmnd *ipr_cmd;
4335 struct ipr_ioa_cfg *ioa_cfg;
4336 struct ipr_resource_entry *res;
Brian King35a39692006-09-25 12:39:20 -05004337 struct ata_port *ap;
4338 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 ENTER;
4341 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
4342 res = scsi_cmd->device->hostdata;
4343
brking@us.ibm.comeeb883072005-11-01 17:02:29 -06004344 if (!res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 return FAILED;
4346
4347 /*
4348 * If we are currently going through reset/reload, return failed. This will force the
4349 * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
4350 * reset to complete
4351 */
4352 if (ioa_cfg->in_reset_reload)
4353 return FAILED;
4354 if (ioa_cfg->ioa_is_dead)
4355 return FAILED;
4356
4357 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08004358 if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 if (ipr_cmd->scsi_cmd)
4360 ipr_cmd->done = ipr_scsi_eh_done;
Brian King24d6f2b2007-03-29 12:43:17 -05004361 if (ipr_cmd->qc)
4362 ipr_cmd->done = ipr_sata_eh_done;
Brian King7402ece2006-11-21 10:28:23 -06004363 if (ipr_cmd->qc && !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
4364 ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
4365 ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
4366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 }
4368 }
4369
4370 res->resetting_device = 1;
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004371 scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
Brian King35a39692006-09-25 12:39:20 -05004372
4373 if (ipr_is_gata(res) && res->sata_port) {
4374 ap = res->sata_port->ap;
4375 spin_unlock_irq(scsi_cmd->device->host->host_lock);
Tejun Heoa1efdab2008-03-25 12:22:50 +09004376 ata_std_error_handler(ap);
Brian King35a39692006-09-25 12:39:20 -05004377 spin_lock_irq(scsi_cmd->device->host->host_lock);
Brian King5af23d22007-05-09 15:36:35 -05004378
4379 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08004380 if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
Brian King5af23d22007-05-09 15:36:35 -05004381 rc = -EIO;
4382 break;
4383 }
4384 }
Brian King35a39692006-09-25 12:39:20 -05004385 } else
4386 rc = ipr_device_reset(ioa_cfg, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 res->resetting_device = 0;
4388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 LEAVE;
Brian Kingc6513092006-03-29 09:37:43 -06004390 return (rc ? FAILED : SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391}
4392
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04004393static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
4394{
4395 int rc;
4396
4397 spin_lock_irq(cmd->device->host->host_lock);
4398 rc = __ipr_eh_dev_reset(cmd);
4399 spin_unlock_irq(cmd->device->host->host_lock);
4400
4401 return rc;
4402}
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404/**
4405 * ipr_bus_reset_done - Op done function for bus reset.
4406 * @ipr_cmd: ipr command struct
4407 *
4408 * This function is the op done function for a bus reset
4409 *
4410 * Return value:
4411 * none
4412 **/
4413static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
4414{
4415 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4416 struct ipr_resource_entry *res;
4417
4418 ENTER;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08004419 if (!ioa_cfg->sis64)
4420 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4421 if (res->res_handle == ipr_cmd->ioarcb.res_handle) {
4422 scsi_report_bus_reset(ioa_cfg->host, res->bus);
4423 break;
4424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
4427 /*
4428 * If abort has not completed, indicate the reset has, else call the
4429 * abort's done function to wake the sleeping eh thread
4430 */
4431 if (ipr_cmd->sibling->sibling)
4432 ipr_cmd->sibling->sibling = NULL;
4433 else
4434 ipr_cmd->sibling->done(ipr_cmd->sibling);
4435
4436 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4437 LEAVE;
4438}
4439
4440/**
4441 * ipr_abort_timeout - An abort task has timed out
4442 * @ipr_cmd: ipr command struct
4443 *
4444 * This function handles when an abort task times out. If this
4445 * happens we issue a bus reset since we have resources tied
4446 * up that must be freed before returning to the midlayer.
4447 *
4448 * Return value:
4449 * none
4450 **/
4451static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
4452{
4453 struct ipr_cmnd *reset_cmd;
4454 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4455 struct ipr_cmd_pkt *cmd_pkt;
4456 unsigned long lock_flags = 0;
4457
4458 ENTER;
4459 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4460 if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
4461 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4462 return;
4463 }
4464
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004465 sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4467 ipr_cmd->sibling = reset_cmd;
4468 reset_cmd->sibling = ipr_cmd;
4469 reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
4470 cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
4471 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4472 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4473 cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
4474
4475 ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4476 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4477 LEAVE;
4478}
4479
4480/**
4481 * ipr_cancel_op - Cancel specified op
4482 * @scsi_cmd: scsi command struct
4483 *
4484 * This function cancels specified op.
4485 *
4486 * Return value:
4487 * SUCCESS / FAILED
4488 **/
4489static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
4490{
4491 struct ipr_cmnd *ipr_cmd;
4492 struct ipr_ioa_cfg *ioa_cfg;
4493 struct ipr_resource_entry *res;
4494 struct ipr_cmd_pkt *cmd_pkt;
4495 u32 ioasc;
4496 int op_found = 0;
4497
4498 ENTER;
4499 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4500 res = scsi_cmd->device->hostdata;
4501
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004502 /* If we are currently going through reset/reload, return failed.
4503 * This will force the mid-layer to call ipr_eh_host_reset,
4504 * which will then go to sleep and wait for the reset to complete
4505 */
4506 if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
4507 return FAILED;
Brian King04d97682006-11-21 10:28:04 -06004508 if (!res || !ipr_is_gscsi(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 return FAILED;
4510
4511 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4512 if (ipr_cmd->scsi_cmd == scsi_cmd) {
4513 ipr_cmd->done = ipr_scsi_eh_done;
4514 op_found = 1;
4515 break;
4516 }
4517 }
4518
4519 if (!op_found)
4520 return SUCCESS;
4521
4522 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08004523 ipr_cmd->ioarcb.res_handle = res->res_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4525 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4526 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4527 ipr_cmd->u.sdev = scsi_cmd->device;
4528
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004529 scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
4530 scsi_cmd->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
4532 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4533
4534 /*
4535 * If the abort task timed out and we sent a bus reset, we will get
4536 * one the following responses to the abort
4537 */
4538 if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
4539 ioasc = 0;
4540 ipr_trace;
4541 }
4542
4543 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004544 if (!ipr_is_naca_model(res))
4545 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
4547 LEAVE;
4548 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
4549}
4550
4551/**
4552 * ipr_eh_abort - Abort a single op
4553 * @scsi_cmd: scsi command struct
4554 *
4555 * Return value:
4556 * SUCCESS / FAILED
4557 **/
4558static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
4559{
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004560 unsigned long flags;
4561 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
4563 ENTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004565 spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
4566 rc = ipr_cancel_op(scsi_cmd);
4567 spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
4569 LEAVE;
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004570 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571}
4572
4573/**
4574 * ipr_handle_other_interrupt - Handle "other" interrupts
4575 * @ioa_cfg: ioa config struct
4576 * @int_reg: interrupt register
4577 *
4578 * Return value:
4579 * IRQ_NONE / IRQ_HANDLED
4580 **/
4581static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
4582 volatile u32 int_reg)
4583{
4584 irqreturn_t rc = IRQ_HANDLED;
4585
4586 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4587 /* Mask the interrupt */
4588 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
4589
4590 /* Clear the interrupt */
4591 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
4592 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4593
4594 list_del(&ioa_cfg->reset_cmd->queue);
4595 del_timer(&ioa_cfg->reset_cmd->timer);
4596 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
4597 } else {
4598 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
4599 ioa_cfg->ioa_unit_checked = 1;
4600 else
4601 dev_err(&ioa_cfg->pdev->dev,
4602 "Permanent IOA failure. 0x%08X\n", int_reg);
4603
4604 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4605 ioa_cfg->sdt_state = GET_DUMP;
4606
4607 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
4608 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4609 }
4610
4611 return rc;
4612}
4613
4614/**
Wayne Boyer3feeb89d2009-10-20 11:09:00 -07004615 * ipr_isr_eh - Interrupt service routine error handler
4616 * @ioa_cfg: ioa config struct
4617 * @msg: message to log
4618 *
4619 * Return value:
4620 * none
4621 **/
4622static void ipr_isr_eh(struct ipr_ioa_cfg *ioa_cfg, char *msg)
4623{
4624 ioa_cfg->errors_logged++;
4625 dev_err(&ioa_cfg->pdev->dev, "%s\n", msg);
4626
4627 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4628 ioa_cfg->sdt_state = GET_DUMP;
4629
4630 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4631}
4632
4633/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 * ipr_isr - Interrupt service routine
4635 * @irq: irq number
4636 * @devp: pointer to ioa config struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 *
4638 * Return value:
4639 * IRQ_NONE / IRQ_HANDLED
4640 **/
David Howells7d12e782006-10-05 14:55:46 +01004641static irqreturn_t ipr_isr(int irq, void *devp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642{
4643 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
4644 unsigned long lock_flags = 0;
4645 volatile u32 int_reg, int_mask_reg;
4646 u32 ioasc;
4647 u16 cmd_index;
Wayne Boyer3feeb89d2009-10-20 11:09:00 -07004648 int num_hrrq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 struct ipr_cmnd *ipr_cmd;
4650 irqreturn_t rc = IRQ_NONE;
4651
4652 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4653
4654 /* If interrupts are disabled, ignore the interrupt */
4655 if (!ioa_cfg->allow_interrupts) {
4656 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4657 return IRQ_NONE;
4658 }
4659
4660 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4661 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4662
4663 /* If an interrupt on the adapter did not occur, ignore it */
4664 if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
4665 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4666 return IRQ_NONE;
4667 }
4668
4669 while (1) {
4670 ipr_cmd = NULL;
4671
4672 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
4673 ioa_cfg->toggle_bit) {
4674
4675 cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
4676 IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
4677
4678 if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
Wayne Boyer3feeb89d2009-10-20 11:09:00 -07004679 ipr_isr_eh(ioa_cfg, "Invalid response handle from IOA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4681 return IRQ_HANDLED;
4682 }
4683
4684 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
4685
4686 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4687
4688 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
4689
4690 list_del(&ipr_cmd->queue);
4691 del_timer(&ipr_cmd->timer);
4692 ipr_cmd->done(ipr_cmd);
4693
4694 rc = IRQ_HANDLED;
4695
4696 if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
4697 ioa_cfg->hrrq_curr++;
4698 } else {
4699 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
4700 ioa_cfg->toggle_bit ^= 1u;
4701 }
4702 }
4703
4704 if (ipr_cmd != NULL) {
4705 /* Clear the PCI interrupt */
Wayne Boyer3feeb89d2009-10-20 11:09:00 -07004706 do {
4707 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg);
4708 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4709 } while (int_reg & IPR_PCII_HRRQ_UPDATED &&
4710 num_hrrq++ < IPR_MAX_HRRQ_RETRIES);
4711
4712 if (int_reg & IPR_PCII_HRRQ_UPDATED) {
4713 ipr_isr_eh(ioa_cfg, "Error clearing HRRQ");
4714 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4715 return IRQ_HANDLED;
4716 }
4717
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 } else
4719 break;
4720 }
4721
4722 if (unlikely(rc == IRQ_NONE))
4723 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
4724
4725 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4726 return rc;
4727}
4728
4729/**
Wayne Boyera32c0552010-02-19 13:23:36 -08004730 * ipr_build_ioadl64 - Build a scatter/gather list and map the buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 * @ioa_cfg: ioa config struct
4732 * @ipr_cmd: ipr command struct
4733 *
4734 * Return value:
4735 * 0 on success / -1 on failure
4736 **/
Wayne Boyera32c0552010-02-19 13:23:36 -08004737static int ipr_build_ioadl64(struct ipr_ioa_cfg *ioa_cfg,
4738 struct ipr_cmnd *ipr_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004740 int i, nseg;
4741 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 u32 length;
4743 u32 ioadl_flags = 0;
4744 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4745 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
Wayne Boyera32c0552010-02-19 13:23:36 -08004746 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004748 length = scsi_bufflen(scsi_cmd);
4749 if (!length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 return 0;
4751
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004752 nseg = scsi_dma_map(scsi_cmd);
4753 if (nseg < 0) {
4754 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
4755 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 }
4757
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004758 ipr_cmd->dma_use_sg = nseg;
4759
4760 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
4761 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
4762 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
Wayne Boyera32c0552010-02-19 13:23:36 -08004763 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE)
4764 ioadl_flags = IPR_IOADL_FLAGS_READ;
4765
4766 scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
4767 ioadl64[i].flags = cpu_to_be32(ioadl_flags);
4768 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(sg));
4769 ioadl64[i].address = cpu_to_be64(sg_dma_address(sg));
4770 }
4771
4772 ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
4773 return 0;
4774}
4775
4776/**
4777 * ipr_build_ioadl - Build a scatter/gather list and map the buffer
4778 * @ioa_cfg: ioa config struct
4779 * @ipr_cmd: ipr command struct
4780 *
4781 * Return value:
4782 * 0 on success / -1 on failure
4783 **/
4784static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
4785 struct ipr_cmnd *ipr_cmd)
4786{
4787 int i, nseg;
4788 struct scatterlist *sg;
4789 u32 length;
4790 u32 ioadl_flags = 0;
4791 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4792 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4793 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
4794
4795 length = scsi_bufflen(scsi_cmd);
4796 if (!length)
4797 return 0;
4798
4799 nseg = scsi_dma_map(scsi_cmd);
4800 if (nseg < 0) {
4801 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
4802 return -1;
4803 }
4804
4805 ipr_cmd->dma_use_sg = nseg;
4806
4807 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
4808 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
4809 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4810 ioarcb->data_transfer_length = cpu_to_be32(length);
4811 ioarcb->ioadl_len =
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004812 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
4813 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
4814 ioadl_flags = IPR_IOADL_FLAGS_READ;
4815 ioarcb->read_data_transfer_length = cpu_to_be32(length);
4816 ioarcb->read_ioadl_len =
4817 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
4818 }
4819
Wayne Boyera32c0552010-02-19 13:23:36 -08004820 if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->u.add_data.u.ioadl)) {
4821 ioadl = ioarcb->u.add_data.u.ioadl;
4822 ioarcb->write_ioadl_addr = cpu_to_be32((ipr_cmd->dma_addr) +
4823 offsetof(struct ipr_ioarcb, u.add_data));
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004824 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
4825 }
4826
4827 scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
4828 ioadl[i].flags_and_data_len =
4829 cpu_to_be32(ioadl_flags | sg_dma_len(sg));
4830 ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
4831 }
4832
4833 ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
4834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
4837/**
4838 * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
4839 * @scsi_cmd: scsi command struct
4840 *
4841 * Return value:
4842 * task attributes
4843 **/
4844static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
4845{
4846 u8 tag[2];
4847 u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
4848
4849 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
4850 switch (tag[0]) {
4851 case MSG_SIMPLE_TAG:
4852 rc = IPR_FLAGS_LO_SIMPLE_TASK;
4853 break;
4854 case MSG_HEAD_TAG:
4855 rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
4856 break;
4857 case MSG_ORDERED_TAG:
4858 rc = IPR_FLAGS_LO_ORDERED_TASK;
4859 break;
4860 };
4861 }
4862
4863 return rc;
4864}
4865
4866/**
4867 * ipr_erp_done - Process completion of ERP for a device
4868 * @ipr_cmd: ipr command struct
4869 *
4870 * This function copies the sense buffer into the scsi_cmd
4871 * struct and pushes the scsi_done function.
4872 *
4873 * Return value:
4874 * nothing
4875 **/
4876static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
4877{
4878 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4879 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4880 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4881 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4882
4883 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
4884 scsi_cmd->result |= (DID_ERROR << 16);
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004885 scmd_printk(KERN_ERR, scsi_cmd,
4886 "Request Sense failed with IOASC: 0x%08X\n", ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 } else {
4888 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
4889 SCSI_SENSE_BUFFERSIZE);
4890 }
4891
4892 if (res) {
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004893 if (!ipr_is_naca_model(res))
4894 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 res->in_erp = 0;
4896 }
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004897 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4899 scsi_cmd->scsi_done(scsi_cmd);
4900}
4901
4902/**
4903 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
4904 * @ipr_cmd: ipr command struct
4905 *
4906 * Return value:
4907 * none
4908 **/
4909static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
4910{
Brian King51b1c7e2007-03-29 12:43:50 -05004911 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4912 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
Wayne Boyera32c0552010-02-19 13:23:36 -08004913 dma_addr_t dma_addr = ipr_cmd->dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
4915 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
Wayne Boyera32c0552010-02-19 13:23:36 -08004916 ioarcb->data_transfer_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 ioarcb->read_data_transfer_length = 0;
Wayne Boyera32c0552010-02-19 13:23:36 -08004918 ioarcb->ioadl_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 ioarcb->read_ioadl_len = 0;
4920 ioasa->ioasc = 0;
4921 ioasa->residual_data_len = 0;
Wayne Boyera32c0552010-02-19 13:23:36 -08004922
4923 if (ipr_cmd->ioa_cfg->sis64)
4924 ioarcb->u.sis64_addr_data.data_ioadl_addr =
4925 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
4926 else {
4927 ioarcb->write_ioadl_addr =
4928 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
4929 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
4930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931}
4932
4933/**
4934 * ipr_erp_request_sense - Send request sense to a device
4935 * @ipr_cmd: ipr command struct
4936 *
4937 * This function sends a request sense to a device as a result
4938 * of a check condition.
4939 *
4940 * Return value:
4941 * nothing
4942 **/
4943static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
4944{
4945 struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4946 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4947
4948 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
4949 ipr_erp_done(ipr_cmd);
4950 return;
4951 }
4952
4953 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
4954
4955 cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
4956 cmd_pkt->cdb[0] = REQUEST_SENSE;
4957 cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
4958 cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
4959 cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
4960 cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
4961
Wayne Boyera32c0552010-02-19 13:23:36 -08004962 ipr_init_ioadl(ipr_cmd, ipr_cmd->sense_buffer_dma,
4963 SCSI_SENSE_BUFFERSIZE, IPR_IOADL_FLAGS_READ_LAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
4965 ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
4966 IPR_REQUEST_SENSE_TIMEOUT * 2);
4967}
4968
4969/**
4970 * ipr_erp_cancel_all - Send cancel all to a device
4971 * @ipr_cmd: ipr command struct
4972 *
4973 * This function sends a cancel all to a device to clear the
4974 * queue. If we are running TCQ on the device, QERR is set to 1,
4975 * which means all outstanding ops have been dropped on the floor.
4976 * Cancel all will return them to us.
4977 *
4978 * Return value:
4979 * nothing
4980 **/
4981static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
4982{
4983 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4984 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4985 struct ipr_cmd_pkt *cmd_pkt;
4986
4987 res->in_erp = 1;
4988
4989 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
4990
4991 if (!scsi_get_tag_type(scsi_cmd->device)) {
4992 ipr_erp_request_sense(ipr_cmd);
4993 return;
4994 }
4995
4996 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4997 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4998 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4999
5000 ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
5001 IPR_CANCEL_ALL_TIMEOUT);
5002}
5003
5004/**
5005 * ipr_dump_ioasa - Dump contents of IOASA
5006 * @ioa_cfg: ioa config struct
5007 * @ipr_cmd: ipr command struct
Brian Kingfe964d02006-03-29 09:37:29 -06005008 * @res: resource entry struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 *
5010 * This function is invoked by the interrupt handler when ops
5011 * fail. It will log the IOASA if appropriate. Only called
5012 * for GPDD ops.
5013 *
5014 * Return value:
5015 * none
5016 **/
5017static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
Brian Kingfe964d02006-03-29 09:37:29 -06005018 struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019{
5020 int i;
5021 u16 data_len;
Brian Kingb0692dd2007-03-29 12:43:09 -05005022 u32 ioasc, fd_ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
5024 __be32 *ioasa_data = (__be32 *)ioasa;
5025 int error_index;
5026
5027 ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
Brian Kingb0692dd2007-03-29 12:43:09 -05005028 fd_ioasc = be32_to_cpu(ioasa->fd_ioasc) & IPR_IOASC_IOASC_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 if (0 == ioasc)
5031 return;
5032
5033 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
5034 return;
5035
Brian Kingb0692dd2007-03-29 12:43:09 -05005036 if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
5037 error_index = ipr_get_error(fd_ioasc);
5038 else
5039 error_index = ipr_get_error(ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
5041 if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
5042 /* Don't log an error if the IOA already logged one */
5043 if (ioasa->ilid != 0)
5044 return;
5045
Brian Kingcc9bd5d2007-03-29 12:43:01 -05005046 if (!ipr_is_gscsi(res))
5047 return;
5048
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 if (ipr_error_table[error_index].log_ioasa == 0)
5050 return;
5051 }
5052
Brian Kingfe964d02006-03-29 09:37:29 -06005053 ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
5055 if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
5056 data_len = sizeof(struct ipr_ioasa);
5057 else
5058 data_len = be16_to_cpu(ioasa->ret_stat_len);
5059
5060 ipr_err("IOASA Dump:\n");
5061
5062 for (i = 0; i < data_len / 4; i += 4) {
5063 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
5064 be32_to_cpu(ioasa_data[i]),
5065 be32_to_cpu(ioasa_data[i+1]),
5066 be32_to_cpu(ioasa_data[i+2]),
5067 be32_to_cpu(ioasa_data[i+3]));
5068 }
5069}
5070
5071/**
5072 * ipr_gen_sense - Generate SCSI sense data from an IOASA
5073 * @ioasa: IOASA
5074 * @sense_buf: sense data buffer
5075 *
5076 * Return value:
5077 * none
5078 **/
5079static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
5080{
5081 u32 failing_lba;
5082 u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
5083 struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
5084 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
5085 u32 ioasc = be32_to_cpu(ioasa->ioasc);
5086
5087 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
5088
5089 if (ioasc >= IPR_FIRST_DRIVER_IOASC)
5090 return;
5091
5092 ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
5093
5094 if (ipr_is_vset_device(res) &&
5095 ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
5096 ioasa->u.vset.failing_lba_hi != 0) {
5097 sense_buf[0] = 0x72;
5098 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
5099 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
5100 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
5101
5102 sense_buf[7] = 12;
5103 sense_buf[8] = 0;
5104 sense_buf[9] = 0x0A;
5105 sense_buf[10] = 0x80;
5106
5107 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
5108
5109 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
5110 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
5111 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
5112 sense_buf[15] = failing_lba & 0x000000ff;
5113
5114 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5115
5116 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
5117 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
5118 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
5119 sense_buf[19] = failing_lba & 0x000000ff;
5120 } else {
5121 sense_buf[0] = 0x70;
5122 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
5123 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
5124 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
5125
5126 /* Illegal request */
5127 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
5128 (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
5129 sense_buf[7] = 10; /* additional length */
5130
5131 /* IOARCB was in error */
5132 if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
5133 sense_buf[15] = 0xC0;
5134 else /* Parameter data was invalid */
5135 sense_buf[15] = 0x80;
5136
5137 sense_buf[16] =
5138 ((IPR_FIELD_POINTER_MASK &
5139 be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
5140 sense_buf[17] =
5141 (IPR_FIELD_POINTER_MASK &
5142 be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
5143 } else {
5144 if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
5145 if (ipr_is_vset_device(res))
5146 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5147 else
5148 failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
5149
5150 sense_buf[0] |= 0x80; /* Or in the Valid bit */
5151 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
5152 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
5153 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
5154 sense_buf[6] = failing_lba & 0x000000ff;
5155 }
5156
5157 sense_buf[7] = 6; /* additional length */
5158 }
5159 }
5160}
5161
5162/**
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06005163 * ipr_get_autosense - Copy autosense data to sense buffer
5164 * @ipr_cmd: ipr command struct
5165 *
5166 * This function copies the autosense buffer to the buffer
5167 * in the scsi_cmd, if there is autosense available.
5168 *
5169 * Return value:
5170 * 1 if autosense was available / 0 if not
5171 **/
5172static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
5173{
5174 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
5175
Brian King117d2ce2006-08-02 14:57:58 -05005176 if ((be32_to_cpu(ioasa->ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06005177 return 0;
5178
5179 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
5180 min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
5181 SCSI_SENSE_BUFFERSIZE));
5182 return 1;
5183}
5184
5185/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 * ipr_erp_start - Process an error response for a SCSI op
5187 * @ioa_cfg: ioa config struct
5188 * @ipr_cmd: ipr command struct
5189 *
5190 * This function determines whether or not to initiate ERP
5191 * on the affected device.
5192 *
5193 * Return value:
5194 * nothing
5195 **/
5196static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
5197 struct ipr_cmnd *ipr_cmd)
5198{
5199 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5200 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5201 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
Brian King8a048992007-04-26 16:00:10 -05005202 u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
5204 if (!res) {
5205 ipr_scsi_eh_done(ipr_cmd);
5206 return;
5207 }
5208
Brian King8a048992007-04-26 16:00:10 -05005209 if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 ipr_gen_sense(ipr_cmd);
5211
Brian Kingcc9bd5d2007-03-29 12:43:01 -05005212 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5213
Brian King8a048992007-04-26 16:00:10 -05005214 switch (masked_ioasc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06005216 if (ipr_is_naca_model(res))
5217 scsi_cmd->result |= (DID_ABORT << 16);
5218 else
5219 scsi_cmd->result |= (DID_IMM_RETRY << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 break;
5221 case IPR_IOASC_IR_RESOURCE_HANDLE:
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06005222 case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 scsi_cmd->result |= (DID_NO_CONNECT << 16);
5224 break;
5225 case IPR_IOASC_HW_SEL_TIMEOUT:
5226 scsi_cmd->result |= (DID_NO_CONNECT << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06005227 if (!ipr_is_naca_model(res))
5228 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 break;
5230 case IPR_IOASC_SYNC_REQUIRED:
5231 if (!res->in_erp)
5232 res->needs_sync_complete = 1;
5233 scsi_cmd->result |= (DID_IMM_RETRY << 16);
5234 break;
5235 case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06005236 case IPR_IOASA_IR_DUAL_IOA_DISABLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
5238 break;
5239 case IPR_IOASC_BUS_WAS_RESET:
5240 case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
5241 /*
5242 * Report the bus reset and ask for a retry. The device
5243 * will give CC/UA the next command.
5244 */
5245 if (!res->resetting_device)
5246 scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
5247 scsi_cmd->result |= (DID_ERROR << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06005248 if (!ipr_is_naca_model(res))
5249 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 break;
5251 case IPR_IOASC_HW_DEV_BUS_STATUS:
5252 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
5253 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06005254 if (!ipr_get_autosense(ipr_cmd)) {
5255 if (!ipr_is_naca_model(res)) {
5256 ipr_erp_cancel_all(ipr_cmd);
5257 return;
5258 }
5259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 }
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06005261 if (!ipr_is_naca_model(res))
5262 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 break;
5264 case IPR_IOASC_NR_INIT_CMD_REQUIRED:
5265 break;
5266 default:
Brian King5b7304f2006-08-02 14:57:51 -05005267 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5268 scsi_cmd->result |= (DID_ERROR << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06005269 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 res->needs_sync_complete = 1;
5271 break;
5272 }
5273
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09005274 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5276 scsi_cmd->scsi_done(scsi_cmd);
5277}
5278
5279/**
5280 * ipr_scsi_done - mid-layer done function
5281 * @ipr_cmd: ipr command struct
5282 *
5283 * This function is invoked by the interrupt handler for
5284 * ops generated by the SCSI mid-layer
5285 *
5286 * Return value:
5287 * none
5288 **/
5289static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
5290{
5291 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5292 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5293 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5294
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09005295 scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->ioasa.residual_data_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
5297 if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09005298 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5300 scsi_cmd->scsi_done(scsi_cmd);
5301 } else
5302 ipr_erp_start(ioa_cfg, ipr_cmd);
5303}
5304
5305/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 * ipr_queuecommand - Queue a mid-layer request
5307 * @scsi_cmd: scsi command struct
5308 * @done: done function
5309 *
5310 * This function queues a request generated by the mid-layer.
5311 *
5312 * Return value:
5313 * 0 on success
5314 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
5315 * SCSI_MLQUEUE_HOST_BUSY if host is busy
5316 **/
5317static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
5318 void (*done) (struct scsi_cmnd *))
5319{
5320 struct ipr_ioa_cfg *ioa_cfg;
5321 struct ipr_resource_entry *res;
5322 struct ipr_ioarcb *ioarcb;
5323 struct ipr_cmnd *ipr_cmd;
5324 int rc = 0;
5325
5326 scsi_cmd->scsi_done = done;
5327 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
5328 res = scsi_cmd->device->hostdata;
5329 scsi_cmd->result = (DID_OK << 16);
5330
5331 /*
5332 * We are currently blocking all devices due to a host reset
5333 * We have told the host to stop giving us new requests, but
5334 * ERP ops don't count. FIXME
5335 */
5336 if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
5337 return SCSI_MLQUEUE_HOST_BUSY;
5338
5339 /*
5340 * FIXME - Create scsi_set_host_offline interface
5341 * and the ioa_is_dead check can be removed
5342 */
5343 if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
5344 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
5345 scsi_cmd->result = (DID_NO_CONNECT << 16);
5346 scsi_cmd->scsi_done(scsi_cmd);
5347 return 0;
5348 }
5349
Brian King35a39692006-09-25 12:39:20 -05005350 if (ipr_is_gata(res) && res->sata_port)
5351 return ata_sas_queuecmd(scsi_cmd, done, res->sata_port->ap);
5352
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5354 ioarcb = &ipr_cmd->ioarcb;
5355 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5356
5357 memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
5358 ipr_cmd->scsi_cmd = scsi_cmd;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08005359 ioarcb->res_handle = res->res_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 ipr_cmd->done = ipr_scsi_done;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08005361 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
5363 if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
5364 if (scsi_cmd->underflow == 0)
5365 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5366
5367 if (res->needs_sync_complete) {
5368 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
5369 res->needs_sync_complete = 0;
5370 }
5371
5372 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
5373 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
5374 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
5375 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
5376 }
5377
5378 if (scsi_cmd->cmnd[0] >= 0xC0 &&
5379 (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
5380 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5381
Wayne Boyera32c0552010-02-19 13:23:36 -08005382 if (likely(rc == 0)) {
5383 if (ioa_cfg->sis64)
5384 rc = ipr_build_ioadl64(ioa_cfg, ipr_cmd);
5385 else
5386 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
5387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
5389 if (likely(rc == 0)) {
5390 mb();
Wayne Boyera32c0552010-02-19 13:23:36 -08005391 ipr_send_command(ipr_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 } else {
5393 list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5394 return SCSI_MLQUEUE_HOST_BUSY;
5395 }
5396
5397 return 0;
5398}
5399
5400/**
Brian King35a39692006-09-25 12:39:20 -05005401 * ipr_ioctl - IOCTL handler
5402 * @sdev: scsi device struct
5403 * @cmd: IOCTL cmd
5404 * @arg: IOCTL arg
5405 *
5406 * Return value:
5407 * 0 on success / other on failure
5408 **/
Adrian Bunkbd705f22006-11-21 10:28:48 -06005409static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
Brian King35a39692006-09-25 12:39:20 -05005410{
5411 struct ipr_resource_entry *res;
5412
5413 res = (struct ipr_resource_entry *)sdev->hostdata;
Brian King0ce3a7e2008-07-11 13:37:50 -05005414 if (res && ipr_is_gata(res)) {
5415 if (cmd == HDIO_GET_IDENTITY)
5416 return -ENOTTY;
Jeff Garzik94be9a52009-01-16 10:17:09 -05005417 return ata_sas_scsi_ioctl(res->sata_port->ap, sdev, cmd, arg);
Brian King0ce3a7e2008-07-11 13:37:50 -05005418 }
Brian King35a39692006-09-25 12:39:20 -05005419
5420 return -EINVAL;
5421}
5422
5423/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 * ipr_info - Get information about the card/driver
5425 * @scsi_host: scsi host struct
5426 *
5427 * Return value:
5428 * pointer to buffer with description string
5429 **/
5430static const char * ipr_ioa_info(struct Scsi_Host *host)
5431{
5432 static char buffer[512];
5433 struct ipr_ioa_cfg *ioa_cfg;
5434 unsigned long lock_flags = 0;
5435
5436 ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
5437
5438 spin_lock_irqsave(host->host_lock, lock_flags);
5439 sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
5440 spin_unlock_irqrestore(host->host_lock, lock_flags);
5441
5442 return buffer;
5443}
5444
5445static struct scsi_host_template driver_template = {
5446 .module = THIS_MODULE,
5447 .name = "IPR",
5448 .info = ipr_ioa_info,
Brian King35a39692006-09-25 12:39:20 -05005449 .ioctl = ipr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 .queuecommand = ipr_queuecommand,
5451 .eh_abort_handler = ipr_eh_abort,
5452 .eh_device_reset_handler = ipr_eh_dev_reset,
5453 .eh_host_reset_handler = ipr_eh_host_reset,
5454 .slave_alloc = ipr_slave_alloc,
5455 .slave_configure = ipr_slave_configure,
5456 .slave_destroy = ipr_slave_destroy,
Brian King35a39692006-09-25 12:39:20 -05005457 .target_alloc = ipr_target_alloc,
5458 .target_destroy = ipr_target_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 .change_queue_depth = ipr_change_queue_depth,
5460 .change_queue_type = ipr_change_queue_type,
5461 .bios_param = ipr_biosparam,
5462 .can_queue = IPR_MAX_COMMANDS,
5463 .this_id = -1,
5464 .sg_tablesize = IPR_MAX_SGLIST,
5465 .max_sectors = IPR_IOA_MAX_SECTORS,
5466 .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
5467 .use_clustering = ENABLE_CLUSTERING,
5468 .shost_attrs = ipr_ioa_attrs,
5469 .sdev_attrs = ipr_dev_attrs,
5470 .proc_name = IPR_NAME
5471};
5472
Brian King35a39692006-09-25 12:39:20 -05005473/**
5474 * ipr_ata_phy_reset - libata phy_reset handler
5475 * @ap: ata port to reset
5476 *
5477 **/
5478static void ipr_ata_phy_reset(struct ata_port *ap)
5479{
5480 unsigned long flags;
5481 struct ipr_sata_port *sata_port = ap->private_data;
5482 struct ipr_resource_entry *res = sata_port->res;
5483 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5484 int rc;
5485
5486 ENTER;
5487 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5488 while(ioa_cfg->in_reset_reload) {
5489 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5490 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5491 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5492 }
5493
5494 if (!ioa_cfg->allow_cmds)
5495 goto out_unlock;
5496
5497 rc = ipr_device_reset(ioa_cfg, res);
5498
5499 if (rc) {
Jeff Garzikac8869d2007-08-16 03:17:03 -04005500 ata_port_disable(ap);
Brian King35a39692006-09-25 12:39:20 -05005501 goto out_unlock;
5502 }
5503
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08005504 ap->link.device[0].class = res->ata_class;
5505 if (ap->link.device[0].class == ATA_DEV_UNKNOWN)
Jeff Garzikac8869d2007-08-16 03:17:03 -04005506 ata_port_disable(ap);
Brian King35a39692006-09-25 12:39:20 -05005507
5508out_unlock:
5509 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5510 LEAVE;
5511}
5512
5513/**
5514 * ipr_ata_post_internal - Cleanup after an internal command
5515 * @qc: ATA queued command
5516 *
5517 * Return value:
5518 * none
5519 **/
5520static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
5521{
5522 struct ipr_sata_port *sata_port = qc->ap->private_data;
5523 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5524 struct ipr_cmnd *ipr_cmd;
5525 unsigned long flags;
5526
5527 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
Brian King73d98ff2006-11-21 10:27:58 -06005528 while(ioa_cfg->in_reset_reload) {
5529 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5530 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5531 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5532 }
5533
Brian King35a39692006-09-25 12:39:20 -05005534 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
5535 if (ipr_cmd->qc == qc) {
5536 ipr_device_reset(ioa_cfg, sata_port->res);
5537 break;
5538 }
5539 }
5540 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5541}
5542
5543/**
Brian King35a39692006-09-25 12:39:20 -05005544 * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
5545 * @regs: destination
5546 * @tf: source ATA taskfile
5547 *
5548 * Return value:
5549 * none
5550 **/
5551static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
5552 struct ata_taskfile *tf)
5553{
5554 regs->feature = tf->feature;
5555 regs->nsect = tf->nsect;
5556 regs->lbal = tf->lbal;
5557 regs->lbam = tf->lbam;
5558 regs->lbah = tf->lbah;
5559 regs->device = tf->device;
5560 regs->command = tf->command;
5561 regs->hob_feature = tf->hob_feature;
5562 regs->hob_nsect = tf->hob_nsect;
5563 regs->hob_lbal = tf->hob_lbal;
5564 regs->hob_lbam = tf->hob_lbam;
5565 regs->hob_lbah = tf->hob_lbah;
5566 regs->ctl = tf->ctl;
5567}
5568
5569/**
5570 * ipr_sata_done - done function for SATA commands
5571 * @ipr_cmd: ipr command struct
5572 *
5573 * This function is invoked by the interrupt handler for
5574 * ops generated by the SCSI mid-layer to SATA devices
5575 *
5576 * Return value:
5577 * none
5578 **/
5579static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
5580{
5581 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5582 struct ata_queued_cmd *qc = ipr_cmd->qc;
5583 struct ipr_sata_port *sata_port = qc->ap->private_data;
5584 struct ipr_resource_entry *res = sata_port->res;
5585 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5586
5587 memcpy(&sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
5588 sizeof(struct ipr_ioasa_gata));
5589 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5590
5591 if (be32_to_cpu(ipr_cmd->ioasa.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08005592 scsi_report_device_reset(ioa_cfg->host, res->bus, res->target);
Brian King35a39692006-09-25 12:39:20 -05005593
5594 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5595 qc->err_mask |= __ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5596 else
5597 qc->err_mask |= ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5598 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5599 ata_qc_complete(qc);
5600}
5601
5602/**
Wayne Boyera32c0552010-02-19 13:23:36 -08005603 * ipr_build_ata_ioadl64 - Build an ATA scatter/gather list
5604 * @ipr_cmd: ipr command struct
5605 * @qc: ATA queued command
5606 *
5607 **/
5608static void ipr_build_ata_ioadl64(struct ipr_cmnd *ipr_cmd,
5609 struct ata_queued_cmd *qc)
5610{
5611 u32 ioadl_flags = 0;
5612 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5613 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
5614 struct ipr_ioadl64_desc *last_ioadl64 = NULL;
5615 int len = qc->nbytes;
5616 struct scatterlist *sg;
5617 unsigned int si;
5618 dma_addr_t dma_addr = ipr_cmd->dma_addr;
5619
5620 if (len == 0)
5621 return;
5622
5623 if (qc->dma_dir == DMA_TO_DEVICE) {
5624 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5625 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5626 } else if (qc->dma_dir == DMA_FROM_DEVICE)
5627 ioadl_flags = IPR_IOADL_FLAGS_READ;
5628
5629 ioarcb->data_transfer_length = cpu_to_be32(len);
5630 ioarcb->ioadl_len =
5631 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
5632 ioarcb->u.sis64_addr_data.data_ioadl_addr =
5633 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ata_ioadl));
5634
5635 for_each_sg(qc->sg, sg, qc->n_elem, si) {
5636 ioadl64->flags = cpu_to_be32(ioadl_flags);
5637 ioadl64->data_len = cpu_to_be32(sg_dma_len(sg));
5638 ioadl64->address = cpu_to_be64(sg_dma_address(sg));
5639
5640 last_ioadl64 = ioadl64;
5641 ioadl64++;
5642 }
5643
5644 if (likely(last_ioadl64))
5645 last_ioadl64->flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5646}
5647
5648/**
Brian King35a39692006-09-25 12:39:20 -05005649 * ipr_build_ata_ioadl - Build an ATA scatter/gather list
5650 * @ipr_cmd: ipr command struct
5651 * @qc: ATA queued command
5652 *
5653 **/
5654static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
5655 struct ata_queued_cmd *qc)
5656{
5657 u32 ioadl_flags = 0;
5658 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
Wayne Boyera32c0552010-02-19 13:23:36 -08005659 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04005660 struct ipr_ioadl_desc *last_ioadl = NULL;
James Bottomleydde20202008-02-19 11:36:56 +01005661 int len = qc->nbytes;
Brian King35a39692006-09-25 12:39:20 -05005662 struct scatterlist *sg;
Tejun Heoff2aeb12007-12-05 16:43:11 +09005663 unsigned int si;
Brian King35a39692006-09-25 12:39:20 -05005664
5665 if (len == 0)
5666 return;
5667
5668 if (qc->dma_dir == DMA_TO_DEVICE) {
5669 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5670 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
Wayne Boyera32c0552010-02-19 13:23:36 -08005671 ioarcb->data_transfer_length = cpu_to_be32(len);
5672 ioarcb->ioadl_len =
Brian King35a39692006-09-25 12:39:20 -05005673 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5674 } else if (qc->dma_dir == DMA_FROM_DEVICE) {
5675 ioadl_flags = IPR_IOADL_FLAGS_READ;
5676 ioarcb->read_data_transfer_length = cpu_to_be32(len);
5677 ioarcb->read_ioadl_len =
5678 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5679 }
5680
Tejun Heoff2aeb12007-12-05 16:43:11 +09005681 for_each_sg(qc->sg, sg, qc->n_elem, si) {
Brian King35a39692006-09-25 12:39:20 -05005682 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5683 ioadl->address = cpu_to_be32(sg_dma_address(sg));
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04005684
5685 last_ioadl = ioadl;
5686 ioadl++;
Brian King35a39692006-09-25 12:39:20 -05005687 }
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04005688
5689 if (likely(last_ioadl))
5690 last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
Brian King35a39692006-09-25 12:39:20 -05005691}
5692
5693/**
5694 * ipr_qc_issue - Issue a SATA qc to a device
5695 * @qc: queued command
5696 *
5697 * Return value:
5698 * 0 if success
5699 **/
5700static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
5701{
5702 struct ata_port *ap = qc->ap;
5703 struct ipr_sata_port *sata_port = ap->private_data;
5704 struct ipr_resource_entry *res = sata_port->res;
5705 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5706 struct ipr_cmnd *ipr_cmd;
5707 struct ipr_ioarcb *ioarcb;
5708 struct ipr_ioarcb_ata_regs *regs;
5709
5710 if (unlikely(!ioa_cfg->allow_cmds || ioa_cfg->ioa_is_dead))
Brian King0feeed82007-03-29 12:43:43 -05005711 return AC_ERR_SYSTEM;
Brian King35a39692006-09-25 12:39:20 -05005712
5713 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5714 ioarcb = &ipr_cmd->ioarcb;
Brian King35a39692006-09-25 12:39:20 -05005715
Wayne Boyera32c0552010-02-19 13:23:36 -08005716 if (ioa_cfg->sis64) {
5717 regs = &ipr_cmd->i.ata_ioadl.regs;
5718 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
5719 } else
5720 regs = &ioarcb->u.add_data.u.regs;
5721
5722 memset(regs, 0, sizeof(*regs));
5723 ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(*regs));
Brian King35a39692006-09-25 12:39:20 -05005724
5725 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5726 ipr_cmd->qc = qc;
5727 ipr_cmd->done = ipr_sata_done;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08005728 ipr_cmd->ioarcb.res_handle = res->res_handle;
Brian King35a39692006-09-25 12:39:20 -05005729 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
5730 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
5731 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
James Bottomleydde20202008-02-19 11:36:56 +01005732 ipr_cmd->dma_use_sg = qc->n_elem;
Brian King35a39692006-09-25 12:39:20 -05005733
Wayne Boyera32c0552010-02-19 13:23:36 -08005734 if (ioa_cfg->sis64)
5735 ipr_build_ata_ioadl64(ipr_cmd, qc);
5736 else
5737 ipr_build_ata_ioadl(ipr_cmd, qc);
5738
Brian King35a39692006-09-25 12:39:20 -05005739 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
5740 ipr_copy_sata_tf(regs, &qc->tf);
5741 memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08005742 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
Brian King35a39692006-09-25 12:39:20 -05005743
5744 switch (qc->tf.protocol) {
5745 case ATA_PROT_NODATA:
5746 case ATA_PROT_PIO:
5747 break;
5748
5749 case ATA_PROT_DMA:
5750 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5751 break;
5752
Tejun Heo0dc36882007-12-18 16:34:43 -05005753 case ATAPI_PROT_PIO:
5754 case ATAPI_PROT_NODATA:
Brian King35a39692006-09-25 12:39:20 -05005755 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5756 break;
5757
Tejun Heo0dc36882007-12-18 16:34:43 -05005758 case ATAPI_PROT_DMA:
Brian King35a39692006-09-25 12:39:20 -05005759 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5760 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5761 break;
5762
5763 default:
5764 WARN_ON(1);
Brian King0feeed82007-03-29 12:43:43 -05005765 return AC_ERR_INVALID;
Brian King35a39692006-09-25 12:39:20 -05005766 }
5767
5768 mb();
Wayne Boyera32c0552010-02-19 13:23:36 -08005769
5770 ipr_send_command(ipr_cmd);
5771
Brian King35a39692006-09-25 12:39:20 -05005772 return 0;
5773}
5774
5775/**
Tejun Heo4c9bf4e2008-04-07 22:47:20 +09005776 * ipr_qc_fill_rtf - Read result TF
5777 * @qc: ATA queued command
5778 *
5779 * Return value:
5780 * true
5781 **/
5782static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
5783{
5784 struct ipr_sata_port *sata_port = qc->ap->private_data;
5785 struct ipr_ioasa_gata *g = &sata_port->ioasa;
5786 struct ata_taskfile *tf = &qc->result_tf;
5787
5788 tf->feature = g->error;
5789 tf->nsect = g->nsect;
5790 tf->lbal = g->lbal;
5791 tf->lbam = g->lbam;
5792 tf->lbah = g->lbah;
5793 tf->device = g->device;
5794 tf->command = g->status;
5795 tf->hob_nsect = g->hob_nsect;
5796 tf->hob_lbal = g->hob_lbal;
5797 tf->hob_lbam = g->hob_lbam;
5798 tf->hob_lbah = g->hob_lbah;
5799 tf->ctl = g->alt_status;
5800
5801 return true;
5802}
5803
Brian King35a39692006-09-25 12:39:20 -05005804static struct ata_port_operations ipr_sata_ops = {
Brian King35a39692006-09-25 12:39:20 -05005805 .phy_reset = ipr_ata_phy_reset,
Tejun Heoa1efdab2008-03-25 12:22:50 +09005806 .hardreset = ipr_sata_reset,
Brian King35a39692006-09-25 12:39:20 -05005807 .post_internal_cmd = ipr_ata_post_internal,
Brian King35a39692006-09-25 12:39:20 -05005808 .qc_prep = ata_noop_qc_prep,
5809 .qc_issue = ipr_qc_issue,
Tejun Heo4c9bf4e2008-04-07 22:47:20 +09005810 .qc_fill_rtf = ipr_qc_fill_rtf,
Brian King35a39692006-09-25 12:39:20 -05005811 .port_start = ata_sas_port_start,
5812 .port_stop = ata_sas_port_stop
5813};
5814
5815static struct ata_port_info sata_port_info = {
5816 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
5817 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
5818 .pio_mask = 0x10, /* pio4 */
5819 .mwdma_mask = 0x07,
5820 .udma_mask = 0x7f, /* udma0-6 */
5821 .port_ops = &ipr_sata_ops
5822};
5823
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824#ifdef CONFIG_PPC_PSERIES
5825static const u16 ipr_blocked_processors[] = {
5826 PV_NORTHSTAR,
5827 PV_PULSAR,
5828 PV_POWER4,
5829 PV_ICESTAR,
5830 PV_SSTAR,
5831 PV_POWER4p,
5832 PV_630,
5833 PV_630p
5834};
5835
5836/**
5837 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
5838 * @ioa_cfg: ioa cfg struct
5839 *
5840 * Adapters that use Gemstone revision < 3.1 do not work reliably on
5841 * certain pSeries hardware. This function determines if the given
5842 * adapter is in one of these confgurations or not.
5843 *
5844 * Return value:
5845 * 1 if adapter is not supported / 0 if adapter is supported
5846 **/
5847static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
5848{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 int i;
5850
Auke Kok44c10132007-06-08 15:46:36 -07005851 if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
5852 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
5853 if (__is_processor(ipr_blocked_processors[i]))
5854 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 }
5856 }
5857 return 0;
5858}
5859#else
5860#define ipr_invalid_adapter(ioa_cfg) 0
5861#endif
5862
5863/**
5864 * ipr_ioa_bringdown_done - IOA bring down completion.
5865 * @ipr_cmd: ipr command struct
5866 *
5867 * This function processes the completion of an adapter bring down.
5868 * It wakes any reset sleepers.
5869 *
5870 * Return value:
5871 * IPR_RC_JOB_RETURN
5872 **/
5873static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
5874{
5875 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5876
5877 ENTER;
5878 ioa_cfg->in_reset_reload = 0;
5879 ioa_cfg->reset_retries = 0;
5880 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5881 wake_up_all(&ioa_cfg->reset_wait_q);
5882
5883 spin_unlock_irq(ioa_cfg->host->host_lock);
5884 scsi_unblock_requests(ioa_cfg->host);
5885 spin_lock_irq(ioa_cfg->host->host_lock);
5886 LEAVE;
5887
5888 return IPR_RC_JOB_RETURN;
5889}
5890
5891/**
5892 * ipr_ioa_reset_done - IOA reset completion.
5893 * @ipr_cmd: ipr command struct
5894 *
5895 * This function processes the completion of an adapter reset.
5896 * It schedules any necessary mid-layer add/removes and
5897 * wakes any reset sleepers.
5898 *
5899 * Return value:
5900 * IPR_RC_JOB_RETURN
5901 **/
5902static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
5903{
5904 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5905 struct ipr_resource_entry *res;
5906 struct ipr_hostrcb *hostrcb, *temp;
5907 int i = 0;
5908
5909 ENTER;
5910 ioa_cfg->in_reset_reload = 0;
5911 ioa_cfg->allow_cmds = 1;
5912 ioa_cfg->reset_cmd = NULL;
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06005913 ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
5915 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
5916 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
5917 ipr_trace;
5918 break;
5919 }
5920 }
5921 schedule_work(&ioa_cfg->work_q);
5922
5923 list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
5924 list_del(&hostrcb->queue);
5925 if (i++ < IPR_NUM_LOG_HCAMS)
5926 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
5927 else
5928 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
5929 }
5930
Brian King6bb04172007-04-26 16:00:08 -05005931 scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
5933
5934 ioa_cfg->reset_retries = 0;
5935 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5936 wake_up_all(&ioa_cfg->reset_wait_q);
5937
Mark Nelson30237852008-12-10 12:23:20 +11005938 spin_unlock(ioa_cfg->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 scsi_unblock_requests(ioa_cfg->host);
Mark Nelson30237852008-12-10 12:23:20 +11005940 spin_lock(ioa_cfg->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
5942 if (!ioa_cfg->allow_cmds)
5943 scsi_block_requests(ioa_cfg->host);
5944
5945 LEAVE;
5946 return IPR_RC_JOB_RETURN;
5947}
5948
5949/**
5950 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
5951 * @supported_dev: supported device struct
5952 * @vpids: vendor product id struct
5953 *
5954 * Return value:
5955 * none
5956 **/
5957static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
5958 struct ipr_std_inq_vpids *vpids)
5959{
5960 memset(supported_dev, 0, sizeof(struct ipr_supported_device));
5961 memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
5962 supported_dev->num_records = 1;
5963 supported_dev->data_length =
5964 cpu_to_be16(sizeof(struct ipr_supported_device));
5965 supported_dev->reserved = 0;
5966}
5967
5968/**
5969 * ipr_set_supported_devs - Send Set Supported Devices for a device
5970 * @ipr_cmd: ipr command struct
5971 *
Wayne Boyera32c0552010-02-19 13:23:36 -08005972 * This function sends a Set Supported Devices to the adapter
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 *
5974 * Return value:
5975 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5976 **/
5977static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
5978{
5979 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5980 struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5982 struct ipr_resource_entry *res = ipr_cmd->u.res;
5983
5984 ipr_cmd->job_step = ipr_ioa_reset_done;
5985
5986 list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
Brian Kinge4fbf442006-03-29 09:37:22 -06005987 if (!ipr_is_scsi_disk(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 continue;
5989
5990 ipr_cmd->u.res = res;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08005991 ipr_set_sup_dev_dflt(supp_dev, &res->std_inq_data.vpids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
5993 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5994 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5995 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5996
5997 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08005998 ioarcb->cmd_pkt.cdb[1] = IPR_SET_ALL_SUPPORTED_DEVICES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
6000 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
6001
Wayne Boyera32c0552010-02-19 13:23:36 -08006002 ipr_init_ioadl(ipr_cmd,
6003 ioa_cfg->vpd_cbs_dma +
6004 offsetof(struct ipr_misc_cbs, supp_dev),
6005 sizeof(struct ipr_supported_device),
6006 IPR_IOADL_FLAGS_WRITE_LAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
6008 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
6009 IPR_SET_SUP_DEVICE_TIMEOUT);
6010
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006011 if (!ioa_cfg->sis64)
6012 ipr_cmd->job_step = ipr_set_supported_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 return IPR_RC_JOB_RETURN;
6014 }
6015
6016 return IPR_RC_JOB_CONTINUE;
6017}
6018
6019/**
brking@us.ibm.com62275042005-11-01 17:01:14 -06006020 * ipr_setup_write_cache - Disable write cache if needed
6021 * @ipr_cmd: ipr command struct
6022 *
6023 * This function sets up adapters write cache to desired setting
6024 *
6025 * Return value:
6026 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6027 **/
6028static int ipr_setup_write_cache(struct ipr_cmnd *ipr_cmd)
6029{
6030 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6031
6032 ipr_cmd->job_step = ipr_set_supported_devs;
6033 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
6034 struct ipr_resource_entry, queue);
6035
6036 if (ioa_cfg->cache_state != CACHE_DISABLED)
6037 return IPR_RC_JOB_CONTINUE;
6038
6039 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6040 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6041 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
6042 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
6043
6044 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6045
6046 return IPR_RC_JOB_RETURN;
6047}
6048
6049/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 * ipr_get_mode_page - Locate specified mode page
6051 * @mode_pages: mode page buffer
6052 * @page_code: page code to find
6053 * @len: minimum required length for mode page
6054 *
6055 * Return value:
6056 * pointer to mode page / NULL on failure
6057 **/
6058static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
6059 u32 page_code, u32 len)
6060{
6061 struct ipr_mode_page_hdr *mode_hdr;
6062 u32 page_length;
6063 u32 length;
6064
6065 if (!mode_pages || (mode_pages->hdr.length == 0))
6066 return NULL;
6067
6068 length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
6069 mode_hdr = (struct ipr_mode_page_hdr *)
6070 (mode_pages->data + mode_pages->hdr.block_desc_len);
6071
6072 while (length) {
6073 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
6074 if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
6075 return mode_hdr;
6076 break;
6077 } else {
6078 page_length = (sizeof(struct ipr_mode_page_hdr) +
6079 mode_hdr->page_length);
6080 length -= page_length;
6081 mode_hdr = (struct ipr_mode_page_hdr *)
6082 ((unsigned long)mode_hdr + page_length);
6083 }
6084 }
6085 return NULL;
6086}
6087
6088/**
6089 * ipr_check_term_power - Check for term power errors
6090 * @ioa_cfg: ioa config struct
6091 * @mode_pages: IOAFP mode pages buffer
6092 *
6093 * Check the IOAFP's mode page 28 for term power errors
6094 *
6095 * Return value:
6096 * nothing
6097 **/
6098static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
6099 struct ipr_mode_pages *mode_pages)
6100{
6101 int i;
6102 int entry_length;
6103 struct ipr_dev_bus_entry *bus;
6104 struct ipr_mode_page28 *mode_page;
6105
6106 mode_page = ipr_get_mode_page(mode_pages, 0x28,
6107 sizeof(struct ipr_mode_page28));
6108
6109 entry_length = mode_page->entry_length;
6110
6111 bus = mode_page->bus;
6112
6113 for (i = 0; i < mode_page->num_entries; i++) {
6114 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
6115 dev_err(&ioa_cfg->pdev->dev,
6116 "Term power is absent on scsi bus %d\n",
6117 bus->res_addr.bus);
6118 }
6119
6120 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
6121 }
6122}
6123
6124/**
6125 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
6126 * @ioa_cfg: ioa config struct
6127 *
6128 * Looks through the config table checking for SES devices. If
6129 * the SES device is in the SES table indicating a maximum SCSI
6130 * bus speed, the speed is limited for the bus.
6131 *
6132 * Return value:
6133 * none
6134 **/
6135static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
6136{
6137 u32 max_xfer_rate;
6138 int i;
6139
6140 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
6141 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
6142 ioa_cfg->bus_attr[i].bus_width);
6143
6144 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
6145 ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
6146 }
6147}
6148
6149/**
6150 * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
6151 * @ioa_cfg: ioa config struct
6152 * @mode_pages: mode page 28 buffer
6153 *
6154 * Updates mode page 28 based on driver configuration
6155 *
6156 * Return value:
6157 * none
6158 **/
6159static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
6160 struct ipr_mode_pages *mode_pages)
6161{
6162 int i, entry_length;
6163 struct ipr_dev_bus_entry *bus;
6164 struct ipr_bus_attributes *bus_attr;
6165 struct ipr_mode_page28 *mode_page;
6166
6167 mode_page = ipr_get_mode_page(mode_pages, 0x28,
6168 sizeof(struct ipr_mode_page28));
6169
6170 entry_length = mode_page->entry_length;
6171
6172 /* Loop for each device bus entry */
6173 for (i = 0, bus = mode_page->bus;
6174 i < mode_page->num_entries;
6175 i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
6176 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
6177 dev_err(&ioa_cfg->pdev->dev,
6178 "Invalid resource address reported: 0x%08X\n",
6179 IPR_GET_PHYS_LOC(bus->res_addr));
6180 continue;
6181 }
6182
6183 bus_attr = &ioa_cfg->bus_attr[i];
6184 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
6185 bus->bus_width = bus_attr->bus_width;
6186 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
6187 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
6188 if (bus_attr->qas_enabled)
6189 bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
6190 else
6191 bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
6192 }
6193}
6194
6195/**
6196 * ipr_build_mode_select - Build a mode select command
6197 * @ipr_cmd: ipr command struct
6198 * @res_handle: resource handle to send command to
6199 * @parm: Byte 2 of Mode Sense command
6200 * @dma_addr: DMA buffer address
6201 * @xfer_len: data transfer length
6202 *
6203 * Return value:
6204 * none
6205 **/
6206static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
Wayne Boyera32c0552010-02-19 13:23:36 -08006207 __be32 res_handle, u8 parm,
6208 dma_addr_t dma_addr, u8 xfer_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6211
6212 ioarcb->res_handle = res_handle;
6213 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6214 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6215 ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
6216 ioarcb->cmd_pkt.cdb[1] = parm;
6217 ioarcb->cmd_pkt.cdb[4] = xfer_len;
6218
Wayne Boyera32c0552010-02-19 13:23:36 -08006219 ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_WRITE_LAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220}
6221
6222/**
6223 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
6224 * @ipr_cmd: ipr command struct
6225 *
6226 * This function sets up the SCSI bus attributes and sends
6227 * a Mode Select for Page 28 to activate them.
6228 *
6229 * Return value:
6230 * IPR_RC_JOB_RETURN
6231 **/
6232static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
6233{
6234 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6235 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
6236 int length;
6237
6238 ENTER;
Brian King47338042006-02-08 20:57:42 -06006239 ipr_scsi_bus_speed_limit(ioa_cfg);
6240 ipr_check_term_power(ioa_cfg, mode_pages);
6241 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
6242 length = mode_pages->hdr.length + 1;
6243 mode_pages->hdr.length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
6245 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
6246 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
6247 length);
6248
brking@us.ibm.com62275042005-11-01 17:01:14 -06006249 ipr_cmd->job_step = ipr_setup_write_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6251
6252 LEAVE;
6253 return IPR_RC_JOB_RETURN;
6254}
6255
6256/**
6257 * ipr_build_mode_sense - Builds a mode sense command
6258 * @ipr_cmd: ipr command struct
6259 * @res: resource entry struct
6260 * @parm: Byte 2 of mode sense command
6261 * @dma_addr: DMA address of mode sense buffer
6262 * @xfer_len: Size of DMA buffer
6263 *
6264 * Return value:
6265 * none
6266 **/
6267static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
6268 __be32 res_handle,
Wayne Boyera32c0552010-02-19 13:23:36 -08006269 u8 parm, dma_addr_t dma_addr, u8 xfer_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6272
6273 ioarcb->res_handle = res_handle;
6274 ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
6275 ioarcb->cmd_pkt.cdb[2] = parm;
6276 ioarcb->cmd_pkt.cdb[4] = xfer_len;
6277 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6278
Wayne Boyera32c0552010-02-19 13:23:36 -08006279 ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280}
6281
6282/**
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06006283 * ipr_reset_cmd_failed - Handle failure of IOA reset command
6284 * @ipr_cmd: ipr command struct
6285 *
6286 * This function handles the failure of an IOA bringup command.
6287 *
6288 * Return value:
6289 * IPR_RC_JOB_RETURN
6290 **/
6291static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
6292{
6293 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6294 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
6295
6296 dev_err(&ioa_cfg->pdev->dev,
6297 "0x%02X failed with IOASC: 0x%08X\n",
6298 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
6299
6300 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
6301 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6302 return IPR_RC_JOB_RETURN;
6303}
6304
6305/**
6306 * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
6307 * @ipr_cmd: ipr command struct
6308 *
6309 * This function handles the failure of a Mode Sense to the IOAFP.
6310 * Some adapters do not handle all mode pages.
6311 *
6312 * Return value:
6313 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6314 **/
6315static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
6316{
6317 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
6318
6319 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
6320 ipr_cmd->job_step = ipr_setup_write_cache;
6321 return IPR_RC_JOB_CONTINUE;
6322 }
6323
6324 return ipr_reset_cmd_failed(ipr_cmd);
6325}
6326
6327/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
6329 * @ipr_cmd: ipr command struct
6330 *
6331 * This function send a Page 28 mode sense to the IOA to
6332 * retrieve SCSI bus attributes.
6333 *
6334 * Return value:
6335 * IPR_RC_JOB_RETURN
6336 **/
6337static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
6338{
6339 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6340
6341 ENTER;
6342 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
6343 0x28, ioa_cfg->vpd_cbs_dma +
6344 offsetof(struct ipr_misc_cbs, mode_pages),
6345 sizeof(struct ipr_mode_pages));
6346
6347 ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06006348 ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
6350 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6351
6352 LEAVE;
6353 return IPR_RC_JOB_RETURN;
6354}
6355
6356/**
Brian Kingac09c342007-04-26 16:00:16 -05006357 * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
6358 * @ipr_cmd: ipr command struct
6359 *
6360 * This function enables dual IOA RAID support if possible.
6361 *
6362 * Return value:
6363 * IPR_RC_JOB_RETURN
6364 **/
6365static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
6366{
6367 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6368 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
6369 struct ipr_mode_page24 *mode_page;
6370 int length;
6371
6372 ENTER;
6373 mode_page = ipr_get_mode_page(mode_pages, 0x24,
6374 sizeof(struct ipr_mode_page24));
6375
6376 if (mode_page)
6377 mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;
6378
6379 length = mode_pages->hdr.length + 1;
6380 mode_pages->hdr.length = 0;
6381
6382 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
6383 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
6384 length);
6385
6386 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
6387 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6388
6389 LEAVE;
6390 return IPR_RC_JOB_RETURN;
6391}
6392
6393/**
6394 * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
6395 * @ipr_cmd: ipr command struct
6396 *
6397 * This function handles the failure of a Mode Sense to the IOAFP.
6398 * Some adapters do not handle all mode pages.
6399 *
6400 * Return value:
6401 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6402 **/
6403static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
6404{
6405 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
6406
6407 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
6408 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
6409 return IPR_RC_JOB_CONTINUE;
6410 }
6411
6412 return ipr_reset_cmd_failed(ipr_cmd);
6413}
6414
6415/**
6416 * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
6417 * @ipr_cmd: ipr command struct
6418 *
6419 * This function send a mode sense to the IOA to retrieve
6420 * the IOA Advanced Function Control mode page.
6421 *
6422 * Return value:
6423 * IPR_RC_JOB_RETURN
6424 **/
6425static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
6426{
6427 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6428
6429 ENTER;
6430 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
6431 0x24, ioa_cfg->vpd_cbs_dma +
6432 offsetof(struct ipr_misc_cbs, mode_pages),
6433 sizeof(struct ipr_mode_pages));
6434
6435 ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
6436 ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;
6437
6438 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6439
6440 LEAVE;
6441 return IPR_RC_JOB_RETURN;
6442}
6443
6444/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 * ipr_init_res_table - Initialize the resource table
6446 * @ipr_cmd: ipr command struct
6447 *
6448 * This function looks through the existing resource table, comparing
6449 * it with the config table. This function will take care of old/new
6450 * devices and schedule adding/removing them from the mid-layer
6451 * as appropriate.
6452 *
6453 * Return value:
6454 * IPR_RC_JOB_CONTINUE
6455 **/
6456static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
6457{
6458 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6459 struct ipr_resource_entry *res, *temp;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006460 struct ipr_config_table_entry_wrapper cfgtew;
6461 int entries, found, flag, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 LIST_HEAD(old_res);
6463
6464 ENTER;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006465 if (ioa_cfg->sis64)
6466 flag = ioa_cfg->u.cfg_table64->hdr64.flags;
6467 else
6468 flag = ioa_cfg->u.cfg_table->hdr.flags;
6469
6470 if (flag & IPR_UCODE_DOWNLOAD_REQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
6472
6473 list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
6474 list_move_tail(&res->queue, &old_res);
6475
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006476 if (ioa_cfg->sis64)
6477 entries = ioa_cfg->u.cfg_table64->hdr64.num_entries;
6478 else
6479 entries = ioa_cfg->u.cfg_table->hdr.num_entries;
6480
6481 for (i = 0; i < entries; i++) {
6482 if (ioa_cfg->sis64)
6483 cfgtew.u.cfgte64 = &ioa_cfg->u.cfg_table64->dev[i];
6484 else
6485 cfgtew.u.cfgte = &ioa_cfg->u.cfg_table->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 found = 0;
6487
6488 list_for_each_entry_safe(res, temp, &old_res, queue) {
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006489 if (ipr_is_same_device(res, &cfgtew)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
6491 found = 1;
6492 break;
6493 }
6494 }
6495
6496 if (!found) {
6497 if (list_empty(&ioa_cfg->free_res_q)) {
6498 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
6499 break;
6500 }
6501
6502 found = 1;
6503 res = list_entry(ioa_cfg->free_res_q.next,
6504 struct ipr_resource_entry, queue);
6505 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006506 ipr_init_res_entry(res, &cfgtew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 res->add_to_ml = 1;
6508 }
6509
6510 if (found)
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006511 ipr_update_res_entry(res, &cfgtew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 }
6513
6514 list_for_each_entry_safe(res, temp, &old_res, queue) {
6515 if (res->sdev) {
6516 res->del_from_ml = 1;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006517 res->res_handle = IPR_INVALID_RES_HANDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519 }
6520 }
6521
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006522 list_for_each_entry_safe(res, temp, &old_res, queue) {
6523 ipr_clear_res_target(res);
6524 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
6525 }
6526
Brian Kingac09c342007-04-26 16:00:16 -05006527 if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
6528 ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
6529 else
6530 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531
6532 LEAVE;
6533 return IPR_RC_JOB_CONTINUE;
6534}
6535
6536/**
6537 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
6538 * @ipr_cmd: ipr command struct
6539 *
6540 * This function sends a Query IOA Configuration command
6541 * to the adapter to retrieve the IOA configuration table.
6542 *
6543 * Return value:
6544 * IPR_RC_JOB_RETURN
6545 **/
6546static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
6547{
6548 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6549 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
Brian Kingac09c342007-04-26 16:00:16 -05006551 struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552
6553 ENTER;
Brian Kingac09c342007-04-26 16:00:16 -05006554 if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
6555 ioa_cfg->dual_raid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
6557 ucode_vpd->major_release, ucode_vpd->card_type,
6558 ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
6559 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6560 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6561
6562 ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006563 ioarcb->cmd_pkt.cdb[7] = (ioa_cfg->cfg_table_size >> 8) & 0xff;
6564 ioarcb->cmd_pkt.cdb[8] = ioa_cfg->cfg_table_size & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006566 ipr_init_ioadl(ipr_cmd, ioa_cfg->cfg_table_dma, ioa_cfg->cfg_table_size,
Wayne Boyera32c0552010-02-19 13:23:36 -08006567 IPR_IOADL_FLAGS_READ_LAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568
6569 ipr_cmd->job_step = ipr_init_res_table;
6570
6571 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6572
6573 LEAVE;
6574 return IPR_RC_JOB_RETURN;
6575}
6576
6577/**
6578 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
6579 * @ipr_cmd: ipr command struct
6580 *
6581 * This utility function sends an inquiry to the adapter.
6582 *
6583 * Return value:
6584 * none
6585 **/
6586static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
Wayne Boyera32c0552010-02-19 13:23:36 -08006587 dma_addr_t dma_addr, u8 xfer_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588{
6589 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590
6591 ENTER;
6592 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6593 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6594
6595 ioarcb->cmd_pkt.cdb[0] = INQUIRY;
6596 ioarcb->cmd_pkt.cdb[1] = flags;
6597 ioarcb->cmd_pkt.cdb[2] = page;
6598 ioarcb->cmd_pkt.cdb[4] = xfer_len;
6599
Wayne Boyera32c0552010-02-19 13:23:36 -08006600 ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601
6602 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6603 LEAVE;
6604}
6605
6606/**
brking@us.ibm.com62275042005-11-01 17:01:14 -06006607 * ipr_inquiry_page_supported - Is the given inquiry page supported
6608 * @page0: inquiry page 0 buffer
6609 * @page: page code.
6610 *
6611 * This function determines if the specified inquiry page is supported.
6612 *
6613 * Return value:
6614 * 1 if page is supported / 0 if not
6615 **/
6616static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
6617{
6618 int i;
6619
6620 for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
6621 if (page0->page[i] == page)
6622 return 1;
6623
6624 return 0;
6625}
6626
6627/**
Brian Kingac09c342007-04-26 16:00:16 -05006628 * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
6629 * @ipr_cmd: ipr command struct
6630 *
6631 * This function sends a Page 0xD0 inquiry to the adapter
6632 * to retrieve adapter capabilities.
6633 *
6634 * Return value:
6635 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6636 **/
6637static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
6638{
6639 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6640 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
6641 struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
6642
6643 ENTER;
6644 ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
6645 memset(cap, 0, sizeof(*cap));
6646
6647 if (ipr_inquiry_page_supported(page0, 0xD0)) {
6648 ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
6649 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
6650 sizeof(struct ipr_inquiry_cap));
6651 return IPR_RC_JOB_RETURN;
6652 }
6653
6654 LEAVE;
6655 return IPR_RC_JOB_CONTINUE;
6656}
6657
6658/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
6660 * @ipr_cmd: ipr command struct
6661 *
6662 * This function sends a Page 3 inquiry to the adapter
6663 * to retrieve software VPD information.
6664 *
6665 * Return value:
6666 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6667 **/
6668static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
6669{
6670 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006671 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
6672
6673 ENTER;
6674
6675 if (!ipr_inquiry_page_supported(page0, 1))
6676 ioa_cfg->cache_state = CACHE_NONE;
6677
Brian Kingac09c342007-04-26 16:00:16 -05006678 ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006679
6680 ipr_ioafp_inquiry(ipr_cmd, 1, 3,
6681 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
6682 sizeof(struct ipr_inquiry_page3));
6683
6684 LEAVE;
6685 return IPR_RC_JOB_RETURN;
6686}
6687
6688/**
6689 * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
6690 * @ipr_cmd: ipr command struct
6691 *
6692 * This function sends a Page 0 inquiry to the adapter
6693 * to retrieve supported inquiry pages.
6694 *
6695 * Return value:
6696 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6697 **/
6698static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
6699{
6700 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 char type[5];
6702
6703 ENTER;
6704
6705 /* Grab the type out of the VPD and store it away */
6706 memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
6707 type[4] = '\0';
6708 ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
6709
brking@us.ibm.com62275042005-11-01 17:01:14 -06006710 ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711
brking@us.ibm.com62275042005-11-01 17:01:14 -06006712 ipr_ioafp_inquiry(ipr_cmd, 1, 0,
6713 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
6714 sizeof(struct ipr_inquiry_page0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715
6716 LEAVE;
6717 return IPR_RC_JOB_RETURN;
6718}
6719
6720/**
6721 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
6722 * @ipr_cmd: ipr command struct
6723 *
6724 * This function sends a standard inquiry to the adapter.
6725 *
6726 * Return value:
6727 * IPR_RC_JOB_RETURN
6728 **/
6729static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
6730{
6731 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6732
6733 ENTER;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006734 ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735
6736 ipr_ioafp_inquiry(ipr_cmd, 0, 0,
6737 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
6738 sizeof(struct ipr_ioa_vpd));
6739
6740 LEAVE;
6741 return IPR_RC_JOB_RETURN;
6742}
6743
6744/**
6745 * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
6746 * @ipr_cmd: ipr command struct
6747 *
6748 * This function send an Identify Host Request Response Queue
6749 * command to establish the HRRQ with the adapter.
6750 *
6751 * Return value:
6752 * IPR_RC_JOB_RETURN
6753 **/
6754static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
6755{
6756 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6757 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6758
6759 ENTER;
6760 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
6761
6762 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
6763 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6764
6765 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6766 ioarcb->cmd_pkt.cdb[2] =
6767 ((u32) ioa_cfg->host_rrq_dma >> 24) & 0xff;
6768 ioarcb->cmd_pkt.cdb[3] =
6769 ((u32) ioa_cfg->host_rrq_dma >> 16) & 0xff;
6770 ioarcb->cmd_pkt.cdb[4] =
6771 ((u32) ioa_cfg->host_rrq_dma >> 8) & 0xff;
6772 ioarcb->cmd_pkt.cdb[5] =
6773 ((u32) ioa_cfg->host_rrq_dma) & 0xff;
6774 ioarcb->cmd_pkt.cdb[7] =
6775 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
6776 ioarcb->cmd_pkt.cdb[8] =
6777 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
6778
6779 ipr_cmd->job_step = ipr_ioafp_std_inquiry;
6780
6781 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6782
6783 LEAVE;
6784 return IPR_RC_JOB_RETURN;
6785}
6786
6787/**
6788 * ipr_reset_timer_done - Adapter reset timer function
6789 * @ipr_cmd: ipr command struct
6790 *
6791 * Description: This function is used in adapter reset processing
6792 * for timing events. If the reset_cmd pointer in the IOA
6793 * config struct is not this adapter's we are doing nested
6794 * resets and fail_all_ops will take care of freeing the
6795 * command block.
6796 *
6797 * Return value:
6798 * none
6799 **/
6800static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
6801{
6802 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6803 unsigned long lock_flags = 0;
6804
6805 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6806
6807 if (ioa_cfg->reset_cmd == ipr_cmd) {
6808 list_del(&ipr_cmd->queue);
6809 ipr_cmd->done(ipr_cmd);
6810 }
6811
6812 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6813}
6814
6815/**
6816 * ipr_reset_start_timer - Start a timer for adapter reset job
6817 * @ipr_cmd: ipr command struct
6818 * @timeout: timeout value
6819 *
6820 * Description: This function is used in adapter reset processing
6821 * for timing events. If the reset_cmd pointer in the IOA
6822 * config struct is not this adapter's we are doing nested
6823 * resets and fail_all_ops will take care of freeing the
6824 * command block.
6825 *
6826 * Return value:
6827 * none
6828 **/
6829static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
6830 unsigned long timeout)
6831{
6832 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
6833 ipr_cmd->done = ipr_reset_ioa_job;
6834
6835 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
6836 ipr_cmd->timer.expires = jiffies + timeout;
6837 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
6838 add_timer(&ipr_cmd->timer);
6839}
6840
6841/**
6842 * ipr_init_ioa_mem - Initialize ioa_cfg control block
6843 * @ioa_cfg: ioa cfg struct
6844 *
6845 * Return value:
6846 * nothing
6847 **/
6848static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
6849{
6850 memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
6851
6852 /* Initialize Host RRQ pointers */
6853 ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
6854 ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
6855 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
6856 ioa_cfg->toggle_bit = 1;
6857
6858 /* Zero out config table */
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08006859 memset(ioa_cfg->u.cfg_table, 0, ioa_cfg->cfg_table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860}
6861
6862/**
6863 * ipr_reset_enable_ioa - Enable the IOA following a reset.
6864 * @ipr_cmd: ipr command struct
6865 *
6866 * This function reinitializes some control blocks and
6867 * enables destructive diagnostics on the adapter.
6868 *
6869 * Return value:
6870 * IPR_RC_JOB_RETURN
6871 **/
6872static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
6873{
6874 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6875 volatile u32 int_reg;
6876
6877 ENTER;
6878 ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
6879 ipr_init_ioa_mem(ioa_cfg);
6880
6881 ioa_cfg->allow_interrupts = 1;
6882 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
6883
6884 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
6885 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
6886 ioa_cfg->regs.clr_interrupt_mask_reg);
6887 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
6888 return IPR_RC_JOB_CONTINUE;
6889 }
6890
6891 /* Enable destructive diagnostics on IOA */
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06006892 writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893
6894 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
6895 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
6896
6897 dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
6898
6899 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
Brian King5469cb52007-03-29 12:42:40 -05006900 ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
6902 ipr_cmd->done = ipr_reset_ioa_job;
6903 add_timer(&ipr_cmd->timer);
6904 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
6905
6906 LEAVE;
6907 return IPR_RC_JOB_RETURN;
6908}
6909
6910/**
6911 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
6912 * @ipr_cmd: ipr command struct
6913 *
6914 * This function is invoked when an adapter dump has run out
6915 * of processing time.
6916 *
6917 * Return value:
6918 * IPR_RC_JOB_CONTINUE
6919 **/
6920static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
6921{
6922 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6923
6924 if (ioa_cfg->sdt_state == GET_DUMP)
6925 ioa_cfg->sdt_state = ABORT_DUMP;
6926
6927 ipr_cmd->job_step = ipr_reset_alert;
6928
6929 return IPR_RC_JOB_CONTINUE;
6930}
6931
6932/**
6933 * ipr_unit_check_no_data - Log a unit check/no data error log
6934 * @ioa_cfg: ioa config struct
6935 *
6936 * Logs an error indicating the adapter unit checked, but for some
6937 * reason, we were unable to fetch the unit check buffer.
6938 *
6939 * Return value:
6940 * nothing
6941 **/
6942static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
6943{
6944 ioa_cfg->errors_logged++;
6945 dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
6946}
6947
6948/**
6949 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
6950 * @ioa_cfg: ioa config struct
6951 *
6952 * Fetches the unit check buffer from the adapter by clocking the data
6953 * through the mailbox register.
6954 *
6955 * Return value:
6956 * nothing
6957 **/
6958static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
6959{
6960 unsigned long mailbox;
6961 struct ipr_hostrcb *hostrcb;
6962 struct ipr_uc_sdt sdt;
6963 int rc, length;
Brian King65f56472007-04-26 16:00:12 -05006964 u32 ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
6966 mailbox = readl(ioa_cfg->ioa_mailbox);
6967
6968 if (!ipr_sdt_is_fmt2(mailbox)) {
6969 ipr_unit_check_no_data(ioa_cfg);
6970 return;
6971 }
6972
6973 memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
6974 rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
6975 (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
6976
6977 if (rc || (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE) ||
6978 !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY)) {
6979 ipr_unit_check_no_data(ioa_cfg);
6980 return;
6981 }
6982
6983 /* Find length of the first sdt entry (UC buffer) */
6984 length = (be32_to_cpu(sdt.entry[0].end_offset) -
6985 be32_to_cpu(sdt.entry[0].bar_str_offset)) & IPR_FMT2_MBX_ADDR_MASK;
6986
6987 hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
6988 struct ipr_hostrcb, queue);
6989 list_del(&hostrcb->queue);
6990 memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
6991
6992 rc = ipr_get_ldump_data_section(ioa_cfg,
6993 be32_to_cpu(sdt.entry[0].bar_str_offset),
6994 (__be32 *)&hostrcb->hcam,
6995 min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
6996
Brian King65f56472007-04-26 16:00:12 -05006997 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 ipr_handle_log_data(ioa_cfg, hostrcb);
Brian King65f56472007-04-26 16:00:12 -05006999 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
7000 if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
7001 ioa_cfg->sdt_state == GET_DUMP)
7002 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7003 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 ipr_unit_check_no_data(ioa_cfg);
7005
7006 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
7007}
7008
7009/**
7010 * ipr_reset_restore_cfg_space - Restore PCI config space.
7011 * @ipr_cmd: ipr command struct
7012 *
7013 * Description: This function restores the saved PCI config space of
7014 * the adapter, fails all outstanding ops back to the callers, and
7015 * fetches the dump/unit check if applicable to this reset.
7016 *
7017 * Return value:
7018 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7019 **/
7020static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
7021{
7022 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7023 int rc;
7024
7025 ENTER;
Kleber Sacilotto de Souza99c965d2009-11-25 20:13:43 -02007026 ioa_cfg->pdev->state_saved = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 rc = pci_restore_state(ioa_cfg->pdev);
7028
7029 if (rc != PCIBIOS_SUCCESSFUL) {
7030 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
7031 return IPR_RC_JOB_CONTINUE;
7032 }
7033
7034 if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
7035 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
7036 return IPR_RC_JOB_CONTINUE;
7037 }
7038
7039 ipr_fail_all_ops(ioa_cfg);
7040
7041 if (ioa_cfg->ioa_unit_checked) {
7042 ioa_cfg->ioa_unit_checked = 0;
7043 ipr_get_unit_check_buffer(ioa_cfg);
7044 ipr_cmd->job_step = ipr_reset_alert;
7045 ipr_reset_start_timer(ipr_cmd, 0);
7046 return IPR_RC_JOB_RETURN;
7047 }
7048
7049 if (ioa_cfg->in_ioa_bringdown) {
7050 ipr_cmd->job_step = ipr_ioa_bringdown_done;
7051 } else {
7052 ipr_cmd->job_step = ipr_reset_enable_ioa;
7053
7054 if (GET_DUMP == ioa_cfg->sdt_state) {
7055 ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
7056 ipr_cmd->job_step = ipr_reset_wait_for_dump;
7057 schedule_work(&ioa_cfg->work_q);
7058 return IPR_RC_JOB_RETURN;
7059 }
7060 }
7061
7062 ENTER;
7063 return IPR_RC_JOB_CONTINUE;
7064}
7065
7066/**
Brian Kinge619e1a2007-01-23 11:25:37 -06007067 * ipr_reset_bist_done - BIST has completed on the adapter.
7068 * @ipr_cmd: ipr command struct
7069 *
7070 * Description: Unblock config space and resume the reset process.
7071 *
7072 * Return value:
7073 * IPR_RC_JOB_CONTINUE
7074 **/
7075static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
7076{
7077 ENTER;
7078 pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
7079 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
7080 LEAVE;
7081 return IPR_RC_JOB_CONTINUE;
7082}
7083
7084/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 * ipr_reset_start_bist - Run BIST on the adapter.
7086 * @ipr_cmd: ipr command struct
7087 *
7088 * Description: This function runs BIST on the adapter, then delays 2 seconds.
7089 *
7090 * Return value:
7091 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7092 **/
7093static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
7094{
7095 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7096 int rc;
7097
7098 ENTER;
Brian Kingb30197d2005-09-27 01:21:56 -07007099 pci_block_user_cfg_access(ioa_cfg->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
7101
7102 if (rc != PCIBIOS_SUCCESSFUL) {
Brian Kinga9aedb02007-03-29 12:43:23 -05007103 pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
7105 rc = IPR_RC_JOB_CONTINUE;
7106 } else {
Brian Kinge619e1a2007-01-23 11:25:37 -06007107 ipr_cmd->job_step = ipr_reset_bist_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
7109 rc = IPR_RC_JOB_RETURN;
7110 }
7111
7112 LEAVE;
7113 return rc;
7114}
7115
7116/**
Brian King463fc692007-05-07 17:09:05 -05007117 * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
7118 * @ipr_cmd: ipr command struct
7119 *
7120 * Description: This clears PCI reset to the adapter and delays two seconds.
7121 *
7122 * Return value:
7123 * IPR_RC_JOB_RETURN
7124 **/
7125static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
7126{
7127 ENTER;
7128 pci_set_pcie_reset_state(ipr_cmd->ioa_cfg->pdev, pcie_deassert_reset);
7129 ipr_cmd->job_step = ipr_reset_bist_done;
7130 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
7131 LEAVE;
7132 return IPR_RC_JOB_RETURN;
7133}
7134
7135/**
7136 * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
7137 * @ipr_cmd: ipr command struct
7138 *
7139 * Description: This asserts PCI reset to the adapter.
7140 *
7141 * Return value:
7142 * IPR_RC_JOB_RETURN
7143 **/
7144static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
7145{
7146 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7147 struct pci_dev *pdev = ioa_cfg->pdev;
7148
7149 ENTER;
7150 pci_block_user_cfg_access(pdev);
7151 pci_set_pcie_reset_state(pdev, pcie_warm_reset);
7152 ipr_cmd->job_step = ipr_reset_slot_reset_done;
7153 ipr_reset_start_timer(ipr_cmd, IPR_PCI_RESET_TIMEOUT);
7154 LEAVE;
7155 return IPR_RC_JOB_RETURN;
7156}
7157
7158/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 * ipr_reset_allowed - Query whether or not IOA can be reset
7160 * @ioa_cfg: ioa config struct
7161 *
7162 * Return value:
7163 * 0 if reset not allowed / non-zero if reset is allowed
7164 **/
7165static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
7166{
7167 volatile u32 temp_reg;
7168
7169 temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
7170 return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
7171}
7172
7173/**
7174 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
7175 * @ipr_cmd: ipr command struct
7176 *
7177 * Description: This function waits for adapter permission to run BIST,
7178 * then runs BIST. If the adapter does not give permission after a
7179 * reasonable time, we will reset the adapter anyway. The impact of
7180 * resetting the adapter without warning the adapter is the risk of
7181 * losing the persistent error log on the adapter. If the adapter is
7182 * reset while it is writing to the flash on the adapter, the flash
7183 * segment will have bad ECC and be zeroed.
7184 *
7185 * Return value:
7186 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7187 **/
7188static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
7189{
7190 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7191 int rc = IPR_RC_JOB_RETURN;
7192
7193 if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
7194 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
7195 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
7196 } else {
Brian King463fc692007-05-07 17:09:05 -05007197 ipr_cmd->job_step = ioa_cfg->reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 rc = IPR_RC_JOB_CONTINUE;
7199 }
7200
7201 return rc;
7202}
7203
7204/**
7205 * ipr_reset_alert_part2 - Alert the adapter of a pending reset
7206 * @ipr_cmd: ipr command struct
7207 *
7208 * Description: This function alerts the adapter that it will be reset.
7209 * If memory space is not currently enabled, proceed directly
7210 * to running BIST on the adapter. The timer must always be started
7211 * so we guarantee we do not run BIST from ipr_isr.
7212 *
7213 * Return value:
7214 * IPR_RC_JOB_RETURN
7215 **/
7216static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
7217{
7218 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7219 u16 cmd_reg;
7220 int rc;
7221
7222 ENTER;
7223 rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
7224
7225 if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
7226 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
7227 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg);
7228 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
7229 } else {
Brian King463fc692007-05-07 17:09:05 -05007230 ipr_cmd->job_step = ioa_cfg->reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 }
7232
7233 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
7234 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
7235
7236 LEAVE;
7237 return IPR_RC_JOB_RETURN;
7238}
7239
7240/**
7241 * ipr_reset_ucode_download_done - Microcode download completion
7242 * @ipr_cmd: ipr command struct
7243 *
7244 * Description: This function unmaps the microcode download buffer.
7245 *
7246 * Return value:
7247 * IPR_RC_JOB_CONTINUE
7248 **/
7249static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
7250{
7251 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7252 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
7253
7254 pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
7255 sglist->num_sg, DMA_TO_DEVICE);
7256
7257 ipr_cmd->job_step = ipr_reset_alert;
7258 return IPR_RC_JOB_CONTINUE;
7259}
7260
7261/**
7262 * ipr_reset_ucode_download - Download microcode to the adapter
7263 * @ipr_cmd: ipr command struct
7264 *
7265 * Description: This function checks to see if it there is microcode
7266 * to download to the adapter. If there is, a download is performed.
7267 *
7268 * Return value:
7269 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7270 **/
7271static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
7272{
7273 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7274 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
7275
7276 ENTER;
7277 ipr_cmd->job_step = ipr_reset_alert;
7278
7279 if (!sglist)
7280 return IPR_RC_JOB_CONTINUE;
7281
7282 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7283 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7284 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
7285 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
7286 ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
7287 ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
7288 ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
7289
Wayne Boyera32c0552010-02-19 13:23:36 -08007290 if (ioa_cfg->sis64)
7291 ipr_build_ucode_ioadl64(ipr_cmd, sglist);
7292 else
7293 ipr_build_ucode_ioadl(ipr_cmd, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 ipr_cmd->job_step = ipr_reset_ucode_download_done;
7295
7296 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
7297 IPR_WRITE_BUFFER_TIMEOUT);
7298
7299 LEAVE;
7300 return IPR_RC_JOB_RETURN;
7301}
7302
7303/**
7304 * ipr_reset_shutdown_ioa - Shutdown the adapter
7305 * @ipr_cmd: ipr command struct
7306 *
7307 * Description: This function issues an adapter shutdown of the
7308 * specified type to the specified adapter as part of the
7309 * adapter reset job.
7310 *
7311 * Return value:
7312 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7313 **/
7314static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
7315{
7316 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7317 enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
7318 unsigned long timeout;
7319 int rc = IPR_RC_JOB_CONTINUE;
7320
7321 ENTER;
7322 if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
7323 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7324 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7325 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
7326 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
7327
Brian Kingac09c342007-04-26 16:00:16 -05007328 if (shutdown_type == IPR_SHUTDOWN_NORMAL)
7329 timeout = IPR_SHUTDOWN_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
7331 timeout = IPR_INTERNAL_TIMEOUT;
Brian Kingac09c342007-04-26 16:00:16 -05007332 else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
7333 timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 else
Brian Kingac09c342007-04-26 16:00:16 -05007335 timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336
7337 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
7338
7339 rc = IPR_RC_JOB_RETURN;
7340 ipr_cmd->job_step = ipr_reset_ucode_download;
7341 } else
7342 ipr_cmd->job_step = ipr_reset_alert;
7343
7344 LEAVE;
7345 return rc;
7346}
7347
7348/**
7349 * ipr_reset_ioa_job - Adapter reset job
7350 * @ipr_cmd: ipr command struct
7351 *
7352 * Description: This function is the job router for the adapter reset job.
7353 *
7354 * Return value:
7355 * none
7356 **/
7357static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
7358{
7359 u32 rc, ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7361
7362 do {
7363 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
7364
7365 if (ioa_cfg->reset_cmd != ipr_cmd) {
7366 /*
7367 * We are doing nested adapter resets and this is
7368 * not the current reset job.
7369 */
7370 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
7371 return;
7372 }
7373
7374 if (IPR_IOASC_SENSE_KEY(ioasc)) {
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06007375 rc = ipr_cmd->job_step_failed(ipr_cmd);
7376 if (rc == IPR_RC_JOB_RETURN)
7377 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 }
7379
7380 ipr_reinit_ipr_cmnd(ipr_cmd);
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06007381 ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 rc = ipr_cmd->job_step(ipr_cmd);
7383 } while(rc == IPR_RC_JOB_CONTINUE);
7384}
7385
7386/**
7387 * _ipr_initiate_ioa_reset - Initiate an adapter reset
7388 * @ioa_cfg: ioa config struct
7389 * @job_step: first job step of reset job
7390 * @shutdown_type: shutdown type
7391 *
7392 * Description: This function will initiate the reset of the given adapter
7393 * starting at the selected job step.
7394 * If the caller needs to wait on the completion of the reset,
7395 * the caller must sleep on the reset_wait_q.
7396 *
7397 * Return value:
7398 * none
7399 **/
7400static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
7401 int (*job_step) (struct ipr_cmnd *),
7402 enum ipr_shutdown_type shutdown_type)
7403{
7404 struct ipr_cmnd *ipr_cmd;
7405
7406 ioa_cfg->in_reset_reload = 1;
7407 ioa_cfg->allow_cmds = 0;
7408 scsi_block_requests(ioa_cfg->host);
7409
7410 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
7411 ioa_cfg->reset_cmd = ipr_cmd;
7412 ipr_cmd->job_step = job_step;
7413 ipr_cmd->u.shutdown_type = shutdown_type;
7414
7415 ipr_reset_ioa_job(ipr_cmd);
7416}
7417
7418/**
7419 * ipr_initiate_ioa_reset - Initiate an adapter reset
7420 * @ioa_cfg: ioa config struct
7421 * @shutdown_type: shutdown type
7422 *
7423 * Description: This function will initiate the reset of the given adapter.
7424 * If the caller needs to wait on the completion of the reset,
7425 * the caller must sleep on the reset_wait_q.
7426 *
7427 * Return value:
7428 * none
7429 **/
7430static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
7431 enum ipr_shutdown_type shutdown_type)
7432{
7433 if (ioa_cfg->ioa_is_dead)
7434 return;
7435
7436 if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
7437 ioa_cfg->sdt_state = ABORT_DUMP;
7438
7439 if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
7440 dev_err(&ioa_cfg->pdev->dev,
7441 "IOA taken offline - error recovery failed\n");
7442
7443 ioa_cfg->reset_retries = 0;
7444 ioa_cfg->ioa_is_dead = 1;
7445
7446 if (ioa_cfg->in_ioa_bringdown) {
7447 ioa_cfg->reset_cmd = NULL;
7448 ioa_cfg->in_reset_reload = 0;
7449 ipr_fail_all_ops(ioa_cfg);
7450 wake_up_all(&ioa_cfg->reset_wait_q);
7451
7452 spin_unlock_irq(ioa_cfg->host->host_lock);
7453 scsi_unblock_requests(ioa_cfg->host);
7454 spin_lock_irq(ioa_cfg->host->host_lock);
7455 return;
7456 } else {
7457 ioa_cfg->in_ioa_bringdown = 1;
7458 shutdown_type = IPR_SHUTDOWN_NONE;
7459 }
7460 }
7461
7462 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
7463 shutdown_type);
7464}
7465
7466/**
Linas Vepstasf8a88b192006-02-03 16:52:42 -06007467 * ipr_reset_freeze - Hold off all I/O activity
7468 * @ipr_cmd: ipr command struct
7469 *
7470 * Description: If the PCI slot is frozen, hold off all I/O
7471 * activity; then, as soon as the slot is available again,
7472 * initiate an adapter reset.
7473 */
7474static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
7475{
7476 /* Disallow new interrupts, avoid loop */
7477 ipr_cmd->ioa_cfg->allow_interrupts = 0;
7478 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
7479 ipr_cmd->done = ipr_reset_ioa_job;
7480 return IPR_RC_JOB_RETURN;
7481}
7482
7483/**
7484 * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
7485 * @pdev: PCI device struct
7486 *
7487 * Description: This routine is called to tell us that the PCI bus
7488 * is down. Can't do anything here, except put the device driver
7489 * into a holding pattern, waiting for the PCI bus to come back.
7490 */
7491static void ipr_pci_frozen(struct pci_dev *pdev)
7492{
7493 unsigned long flags = 0;
7494 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7495
7496 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
7497 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
7498 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
7499}
7500
7501/**
7502 * ipr_pci_slot_reset - Called when PCI slot has been reset.
7503 * @pdev: PCI device struct
7504 *
7505 * Description: This routine is called by the pci error recovery
7506 * code after the PCI slot has been reset, just before we
7507 * should resume normal operations.
7508 */
7509static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
7510{
7511 unsigned long flags = 0;
7512 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7513
7514 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
Brian King463fc692007-05-07 17:09:05 -05007515 if (ioa_cfg->needs_warm_reset)
7516 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7517 else
7518 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
7519 IPR_SHUTDOWN_NONE);
Linas Vepstasf8a88b192006-02-03 16:52:42 -06007520 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
7521 return PCI_ERS_RESULT_RECOVERED;
7522}
7523
7524/**
7525 * ipr_pci_perm_failure - Called when PCI slot is dead for good.
7526 * @pdev: PCI device struct
7527 *
7528 * Description: This routine is called when the PCI bus has
7529 * permanently failed.
7530 */
7531static void ipr_pci_perm_failure(struct pci_dev *pdev)
7532{
7533 unsigned long flags = 0;
7534 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7535
7536 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
7537 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
7538 ioa_cfg->sdt_state = ABORT_DUMP;
7539 ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES;
7540 ioa_cfg->in_ioa_bringdown = 1;
Kleber S. Souza6ff63892009-05-04 10:41:02 -03007541 ioa_cfg->allow_cmds = 0;
Linas Vepstasf8a88b192006-02-03 16:52:42 -06007542 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7543 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
7544}
7545
7546/**
7547 * ipr_pci_error_detected - Called when a PCI error is detected.
7548 * @pdev: PCI device struct
7549 * @state: PCI channel state
7550 *
7551 * Description: Called when a PCI error is detected.
7552 *
7553 * Return value:
7554 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7555 */
7556static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
7557 pci_channel_state_t state)
7558{
7559 switch (state) {
7560 case pci_channel_io_frozen:
7561 ipr_pci_frozen(pdev);
7562 return PCI_ERS_RESULT_NEED_RESET;
7563 case pci_channel_io_perm_failure:
7564 ipr_pci_perm_failure(pdev);
7565 return PCI_ERS_RESULT_DISCONNECT;
7566 break;
7567 default:
7568 break;
7569 }
7570 return PCI_ERS_RESULT_NEED_RESET;
7571}
7572
7573/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
7575 * @ioa_cfg: ioa cfg struct
7576 *
7577 * Description: This is the second phase of adapter intialization
7578 * This function takes care of initilizing the adapter to the point
7579 * where it can accept new commands.
7580
7581 * Return value:
Joe Perchesb1c11812008-02-03 17:28:22 +02007582 * 0 on success / -EIO on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 **/
7584static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
7585{
7586 int rc = 0;
7587 unsigned long host_lock_flags = 0;
7588
7589 ENTER;
7590 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7591 dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007592 if (ioa_cfg->needs_hard_reset) {
7593 ioa_cfg->needs_hard_reset = 0;
7594 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7595 } else
7596 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
7597 IPR_SHUTDOWN_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598
7599 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7600 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7601 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7602
7603 if (ioa_cfg->ioa_is_dead) {
7604 rc = -EIO;
7605 } else if (ipr_invalid_adapter(ioa_cfg)) {
7606 if (!ipr_testmode)
7607 rc = -EIO;
7608
7609 dev_err(&ioa_cfg->pdev->dev,
7610 "Adapter not supported in this hardware configuration.\n");
7611 }
7612
7613 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7614
7615 LEAVE;
7616 return rc;
7617}
7618
7619/**
7620 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
7621 * @ioa_cfg: ioa config struct
7622 *
7623 * Return value:
7624 * none
7625 **/
7626static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
7627{
7628 int i;
7629
7630 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
7631 if (ioa_cfg->ipr_cmnd_list[i])
7632 pci_pool_free(ioa_cfg->ipr_cmd_pool,
7633 ioa_cfg->ipr_cmnd_list[i],
7634 ioa_cfg->ipr_cmnd_list_dma[i]);
7635
7636 ioa_cfg->ipr_cmnd_list[i] = NULL;
7637 }
7638
7639 if (ioa_cfg->ipr_cmd_pool)
7640 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
7641
7642 ioa_cfg->ipr_cmd_pool = NULL;
7643}
7644
7645/**
7646 * ipr_free_mem - Frees memory allocated for an adapter
7647 * @ioa_cfg: ioa cfg struct
7648 *
7649 * Return value:
7650 * nothing
7651 **/
7652static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
7653{
7654 int i;
7655
7656 kfree(ioa_cfg->res_entries);
7657 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
7658 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
7659 ipr_free_cmd_blks(ioa_cfg);
7660 pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
7661 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08007662 pci_free_consistent(ioa_cfg->pdev, ioa_cfg->cfg_table_size,
7663 ioa_cfg->u.cfg_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664 ioa_cfg->cfg_table_dma);
7665
7666 for (i = 0; i < IPR_NUM_HCAMS; i++) {
7667 pci_free_consistent(ioa_cfg->pdev,
7668 sizeof(struct ipr_hostrcb),
7669 ioa_cfg->hostrcb[i],
7670 ioa_cfg->hostrcb_dma[i]);
7671 }
7672
7673 ipr_free_dump(ioa_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 kfree(ioa_cfg->trace);
7675}
7676
7677/**
7678 * ipr_free_all_resources - Free all allocated resources for an adapter.
7679 * @ipr_cmd: ipr command struct
7680 *
7681 * This function frees all allocated resources for the
7682 * specified adapter.
7683 *
7684 * Return value:
7685 * none
7686 **/
7687static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
7688{
7689 struct pci_dev *pdev = ioa_cfg->pdev;
7690
7691 ENTER;
7692 free_irq(pdev->irq, ioa_cfg);
Wayne Boyer5a9ef252009-01-23 09:17:35 -08007693 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 iounmap(ioa_cfg->hdw_dma_regs);
7695 pci_release_regions(pdev);
7696 ipr_free_mem(ioa_cfg);
7697 scsi_host_put(ioa_cfg->host);
7698 pci_disable_device(pdev);
7699 LEAVE;
7700}
7701
7702/**
7703 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
7704 * @ioa_cfg: ioa config struct
7705 *
7706 * Return value:
7707 * 0 on success / -ENOMEM on allocation failure
7708 **/
7709static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
7710{
7711 struct ipr_cmnd *ipr_cmd;
7712 struct ipr_ioarcb *ioarcb;
7713 dma_addr_t dma_addr;
7714 int i;
7715
7716 ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
Wayne Boyera32c0552010-02-19 13:23:36 -08007717 sizeof(struct ipr_cmnd), 16, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718
7719 if (!ioa_cfg->ipr_cmd_pool)
7720 return -ENOMEM;
7721
7722 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
Christoph Lametere94b1762006-12-06 20:33:17 -08007723 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724
7725 if (!ipr_cmd) {
7726 ipr_free_cmd_blks(ioa_cfg);
7727 return -ENOMEM;
7728 }
7729
7730 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
7731 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
7732 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
7733
7734 ioarcb = &ipr_cmd->ioarcb;
Wayne Boyera32c0552010-02-19 13:23:36 -08007735 ipr_cmd->dma_addr = dma_addr;
7736 if (ioa_cfg->sis64)
7737 ioarcb->a.ioarcb_host_pci_addr64 = cpu_to_be64(dma_addr);
7738 else
7739 ioarcb->a.ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
7740
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741 ioarcb->host_response_handle = cpu_to_be32(i << 2);
Wayne Boyera32c0552010-02-19 13:23:36 -08007742 if (ioa_cfg->sis64) {
7743 ioarcb->u.sis64_addr_data.data_ioadl_addr =
7744 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
7745 ioarcb->u.sis64_addr_data.ioasa_host_pci_addr =
7746 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, ioasa));
7747 } else {
7748 ioarcb->write_ioadl_addr =
7749 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
7750 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
7751 ioarcb->ioasa_host_pci_addr =
7752 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
7753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
7755 ipr_cmd->cmd_index = i;
7756 ipr_cmd->ioa_cfg = ioa_cfg;
7757 ipr_cmd->sense_buffer_dma = dma_addr +
7758 offsetof(struct ipr_cmnd, sense_buffer);
7759
7760 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
7761 }
7762
7763 return 0;
7764}
7765
7766/**
7767 * ipr_alloc_mem - Allocate memory for an adapter
7768 * @ioa_cfg: ioa config struct
7769 *
7770 * Return value:
7771 * 0 on success / non-zero for error
7772 **/
7773static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
7774{
7775 struct pci_dev *pdev = ioa_cfg->pdev;
7776 int i, rc = -ENOMEM;
7777
7778 ENTER;
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06007779 ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08007780 ioa_cfg->max_devs_supported, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781
7782 if (!ioa_cfg->res_entries)
7783 goto out;
7784
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08007785 if (ioa_cfg->sis64) {
7786 ioa_cfg->target_ids = kzalloc(sizeof(unsigned long) *
7787 BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
7788 ioa_cfg->array_ids = kzalloc(sizeof(unsigned long) *
7789 BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
7790 ioa_cfg->vset_ids = kzalloc(sizeof(unsigned long) *
7791 BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
7792 }
7793
7794 for (i = 0; i < ioa_cfg->max_devs_supported; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08007796 ioa_cfg->res_entries[i].ioa_cfg = ioa_cfg;
7797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798
7799 ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
7800 sizeof(struct ipr_misc_cbs),
7801 &ioa_cfg->vpd_cbs_dma);
7802
7803 if (!ioa_cfg->vpd_cbs)
7804 goto out_free_res_entries;
7805
7806 if (ipr_alloc_cmd_blks(ioa_cfg))
7807 goto out_free_vpd_cbs;
7808
7809 ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
7810 sizeof(u32) * IPR_NUM_CMD_BLKS,
7811 &ioa_cfg->host_rrq_dma);
7812
7813 if (!ioa_cfg->host_rrq)
7814 goto out_ipr_free_cmd_blocks;
7815
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08007816 ioa_cfg->u.cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
7817 ioa_cfg->cfg_table_size,
7818 &ioa_cfg->cfg_table_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08007820 if (!ioa_cfg->u.cfg_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 goto out_free_host_rrq;
7822
7823 for (i = 0; i < IPR_NUM_HCAMS; i++) {
7824 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
7825 sizeof(struct ipr_hostrcb),
7826 &ioa_cfg->hostrcb_dma[i]);
7827
7828 if (!ioa_cfg->hostrcb[i])
7829 goto out_free_hostrcb_dma;
7830
7831 ioa_cfg->hostrcb[i]->hostrcb_dma =
7832 ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
Brian King49dc6a12006-11-21 10:28:35 -06007833 ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
7835 }
7836
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06007837 ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
7839
7840 if (!ioa_cfg->trace)
7841 goto out_free_hostrcb_dma;
7842
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 rc = 0;
7844out:
7845 LEAVE;
7846 return rc;
7847
7848out_free_hostrcb_dma:
7849 while (i-- > 0) {
7850 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
7851 ioa_cfg->hostrcb[i],
7852 ioa_cfg->hostrcb_dma[i]);
7853 }
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08007854 pci_free_consistent(pdev, ioa_cfg->cfg_table_size,
7855 ioa_cfg->u.cfg_table,
7856 ioa_cfg->cfg_table_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857out_free_host_rrq:
7858 pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
7859 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
7860out_ipr_free_cmd_blocks:
7861 ipr_free_cmd_blks(ioa_cfg);
7862out_free_vpd_cbs:
7863 pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
7864 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
7865out_free_res_entries:
7866 kfree(ioa_cfg->res_entries);
7867 goto out;
7868}
7869
7870/**
7871 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
7872 * @ioa_cfg: ioa config struct
7873 *
7874 * Return value:
7875 * none
7876 **/
7877static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
7878{
7879 int i;
7880
7881 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
7882 ioa_cfg->bus_attr[i].bus = i;
7883 ioa_cfg->bus_attr[i].qas_enabled = 0;
7884 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
7885 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
7886 ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
7887 else
7888 ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
7889 }
7890}
7891
7892/**
7893 * ipr_init_ioa_cfg - Initialize IOA config struct
7894 * @ioa_cfg: ioa config struct
7895 * @host: scsi host struct
7896 * @pdev: PCI dev struct
7897 *
7898 * Return value:
7899 * none
7900 **/
7901static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
7902 struct Scsi_Host *host, struct pci_dev *pdev)
7903{
7904 const struct ipr_interrupt_offsets *p;
7905 struct ipr_interrupts *t;
7906 void __iomem *base;
7907
7908 ioa_cfg->host = host;
7909 ioa_cfg->pdev = pdev;
7910 ioa_cfg->log_level = ipr_log_level;
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06007911 ioa_cfg->doorbell = IPR_DOORBELL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
7913 sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
7914 sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
7915 sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
7916 sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
7917 sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
7918 sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
7919 sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
7920
7921 INIT_LIST_HEAD(&ioa_cfg->free_q);
7922 INIT_LIST_HEAD(&ioa_cfg->pending_q);
7923 INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
7924 INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
7925 INIT_LIST_HEAD(&ioa_cfg->free_res_q);
7926 INIT_LIST_HEAD(&ioa_cfg->used_res_q);
David Howellsc4028952006-11-22 14:57:56 +00007927 INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 init_waitqueue_head(&ioa_cfg->reset_wait_q);
Wayne Boyer95fecd92009-06-16 15:13:28 -07007929 init_waitqueue_head(&ioa_cfg->msi_wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930 ioa_cfg->sdt_state = INACTIVE;
brking@us.ibm.com62275042005-11-01 17:01:14 -06007931 if (ipr_enable_cache)
7932 ioa_cfg->cache_state = CACHE_ENABLED;
7933 else
7934 ioa_cfg->cache_state = CACHE_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935
7936 ipr_initialize_bus_attr(ioa_cfg);
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08007937 ioa_cfg->max_devs_supported = ipr_max_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08007939 if (ioa_cfg->sis64) {
7940 host->max_id = IPR_MAX_SIS64_TARGETS_PER_BUS;
7941 host->max_lun = IPR_MAX_SIS64_LUNS_PER_TARGET;
7942 if (ipr_max_devs > IPR_MAX_SIS64_DEVS)
7943 ioa_cfg->max_devs_supported = IPR_MAX_SIS64_DEVS;
7944 } else {
7945 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
7946 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
7947 if (ipr_max_devs > IPR_MAX_PHYSICAL_DEVS)
7948 ioa_cfg->max_devs_supported = IPR_MAX_PHYSICAL_DEVS;
7949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 host->max_channel = IPR_MAX_BUS_TO_SCAN;
7951 host->unique_id = host->host_no;
7952 host->max_cmd_len = IPR_MAX_CDB_LEN;
7953 pci_set_drvdata(pdev, ioa_cfg);
7954
7955 p = &ioa_cfg->chip_cfg->regs;
7956 t = &ioa_cfg->regs;
7957 base = ioa_cfg->hdw_dma_regs;
7958
7959 t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
7960 t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
7961 t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
7962 t->clr_interrupt_reg = base + p->clr_interrupt_reg;
7963 t->sense_interrupt_reg = base + p->sense_interrupt_reg;
7964 t->ioarrin_reg = base + p->ioarrin_reg;
7965 t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
7966 t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
7967 t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
7968}
7969
7970/**
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007971 * ipr_get_chip_info - Find adapter chip information
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 * @dev_id: PCI device id struct
7973 *
7974 * Return value:
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007975 * ptr to chip information on success / NULL on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976 **/
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007977static const struct ipr_chip_t * __devinit
7978ipr_get_chip_info(const struct pci_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979{
7980 int i;
7981
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
7983 if (ipr_chip[i].vendor == dev_id->vendor &&
7984 ipr_chip[i].device == dev_id->device)
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007985 return &ipr_chip[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986 return NULL;
7987}
7988
7989/**
Wayne Boyer95fecd92009-06-16 15:13:28 -07007990 * ipr_test_intr - Handle the interrupt generated in ipr_test_msi().
7991 * @pdev: PCI device struct
7992 *
7993 * Description: Simply set the msi_received flag to 1 indicating that
7994 * Message Signaled Interrupts are supported.
7995 *
7996 * Return value:
7997 * 0 on success / non-zero on failure
7998 **/
7999static irqreturn_t __devinit ipr_test_intr(int irq, void *devp)
8000{
8001 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
8002 unsigned long lock_flags = 0;
8003 irqreturn_t rc = IRQ_HANDLED;
8004
8005 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8006
8007 ioa_cfg->msi_received = 1;
8008 wake_up(&ioa_cfg->msi_wait_q);
8009
8010 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8011 return rc;
8012}
8013
8014/**
8015 * ipr_test_msi - Test for Message Signaled Interrupt (MSI) support.
8016 * @pdev: PCI device struct
8017 *
8018 * Description: The return value from pci_enable_msi() can not always be
8019 * trusted. This routine sets up and initiates a test interrupt to determine
8020 * if the interrupt is received via the ipr_test_intr() service routine.
8021 * If the tests fails, the driver will fall back to LSI.
8022 *
8023 * Return value:
8024 * 0 on success / non-zero on failure
8025 **/
8026static int __devinit ipr_test_msi(struct ipr_ioa_cfg *ioa_cfg,
8027 struct pci_dev *pdev)
8028{
8029 int rc;
8030 volatile u32 int_reg;
8031 unsigned long lock_flags = 0;
8032
8033 ENTER;
8034
8035 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8036 init_waitqueue_head(&ioa_cfg->msi_wait_q);
8037 ioa_cfg->msi_received = 0;
8038 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
8039 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.clr_interrupt_mask_reg);
8040 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
8041 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8042
8043 rc = request_irq(pdev->irq, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
8044 if (rc) {
8045 dev_err(&pdev->dev, "Can not assign irq %d\n", pdev->irq);
8046 return rc;
8047 } else if (ipr_debug)
8048 dev_info(&pdev->dev, "IRQ assigned: %d\n", pdev->irq);
8049
8050 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.sense_interrupt_reg);
8051 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
8052 wait_event_timeout(ioa_cfg->msi_wait_q, ioa_cfg->msi_received, HZ);
8053 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
8054
8055 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8056 if (!ioa_cfg->msi_received) {
8057 /* MSI test failed */
8058 dev_info(&pdev->dev, "MSI test failed. Falling back to LSI.\n");
8059 rc = -EOPNOTSUPP;
8060 } else if (ipr_debug)
8061 dev_info(&pdev->dev, "MSI test succeeded.\n");
8062
8063 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8064
8065 free_irq(pdev->irq, ioa_cfg);
8066
8067 LEAVE;
8068
8069 return rc;
8070}
8071
8072/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 * ipr_probe_ioa - Allocates memory and does first stage of initialization
8074 * @pdev: PCI device struct
8075 * @dev_id: PCI device id struct
8076 *
8077 * Return value:
8078 * 0 on success / non-zero on failure
8079 **/
8080static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
8081 const struct pci_device_id *dev_id)
8082{
8083 struct ipr_ioa_cfg *ioa_cfg;
8084 struct Scsi_Host *host;
8085 unsigned long ipr_regs_pci;
8086 void __iomem *ipr_regs;
Eric Sesterhenna2a65a32006-09-25 16:59:07 -07008087 int rc = PCIBIOS_SUCCESSFUL;
Brian King473b1e82007-05-02 10:44:11 -05008088 volatile u32 mask, uproc, interrupts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089
8090 ENTER;
8091
8092 if ((rc = pci_enable_device(pdev))) {
8093 dev_err(&pdev->dev, "Cannot enable adapter\n");
8094 goto out;
8095 }
8096
8097 dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
8098
8099 host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
8100
8101 if (!host) {
8102 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
8103 rc = -ENOMEM;
8104 goto out_disable;
8105 }
8106
8107 ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
8108 memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
Brian King35a39692006-09-25 12:39:20 -05008109 ata_host_init(&ioa_cfg->ata_host, &pdev->dev,
8110 sata_port_info.flags, &ipr_sata_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111
Wayne Boyer1be7bd82009-06-17 09:55:35 -07008112 ioa_cfg->ipr_chip = ipr_get_chip_info(dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113
Wayne Boyer1be7bd82009-06-17 09:55:35 -07008114 if (!ioa_cfg->ipr_chip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
8116 dev_id->vendor, dev_id->device);
8117 goto out_scsi_host_put;
8118 }
8119
Wayne Boyera32c0552010-02-19 13:23:36 -08008120 /* set SIS 32 or SIS 64 */
8121 ioa_cfg->sis64 = ioa_cfg->ipr_chip->sis_type == IPR_SIS64 ? 1 : 0;
Wayne Boyer1be7bd82009-06-17 09:55:35 -07008122 ioa_cfg->chip_cfg = ioa_cfg->ipr_chip->cfg;
8123
Brian King5469cb52007-03-29 12:42:40 -05008124 if (ipr_transop_timeout)
8125 ioa_cfg->transop_timeout = ipr_transop_timeout;
8126 else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
8127 ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
8128 else
8129 ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
8130
Auke Kok44c10132007-06-08 15:46:36 -07008131 ioa_cfg->revid = pdev->revision;
Brian King463fc692007-05-07 17:09:05 -05008132
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133 ipr_regs_pci = pci_resource_start(pdev, 0);
8134
8135 rc = pci_request_regions(pdev, IPR_NAME);
8136 if (rc < 0) {
8137 dev_err(&pdev->dev,
8138 "Couldn't register memory range of registers\n");
8139 goto out_scsi_host_put;
8140 }
8141
Arjan van de Ven25729a72008-09-28 16:18:02 -07008142 ipr_regs = pci_ioremap_bar(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008143
8144 if (!ipr_regs) {
8145 dev_err(&pdev->dev,
8146 "Couldn't map memory range of registers\n");
8147 rc = -ENOMEM;
8148 goto out_release_regions;
8149 }
8150
8151 ioa_cfg->hdw_dma_regs = ipr_regs;
8152 ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
8153 ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
8154
8155 ipr_init_ioa_cfg(ioa_cfg, host, pdev);
8156
8157 pci_set_master(pdev);
8158
Wayne Boyera32c0552010-02-19 13:23:36 -08008159 if (ioa_cfg->sis64) {
8160 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
8161 if (rc < 0) {
8162 dev_dbg(&pdev->dev, "Failed to set 64 bit PCI DMA mask\n");
8163 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8164 }
8165
8166 } else
8167 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8168
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169 if (rc < 0) {
8170 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
8171 goto cleanup_nomem;
8172 }
8173
8174 rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
8175 ioa_cfg->chip_cfg->cache_line_size);
8176
8177 if (rc != PCIBIOS_SUCCESSFUL) {
8178 dev_err(&pdev->dev, "Write of cache line size failed\n");
8179 rc = -EIO;
8180 goto cleanup_nomem;
8181 }
8182
Wayne Boyer95fecd92009-06-16 15:13:28 -07008183 /* Enable MSI style interrupts if they are supported. */
Wayne Boyer1be7bd82009-06-17 09:55:35 -07008184 if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI && !pci_enable_msi(pdev)) {
Wayne Boyer95fecd92009-06-16 15:13:28 -07008185 rc = ipr_test_msi(ioa_cfg, pdev);
8186 if (rc == -EOPNOTSUPP)
8187 pci_disable_msi(pdev);
8188 else if (rc)
8189 goto out_msi_disable;
8190 else
8191 dev_info(&pdev->dev, "MSI enabled with IRQ: %d\n", pdev->irq);
8192 } else if (ipr_debug)
8193 dev_info(&pdev->dev, "Cannot enable MSI.\n");
8194
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195 /* Save away PCI config space for use following IOA reset */
8196 rc = pci_save_state(pdev);
8197
8198 if (rc != PCIBIOS_SUCCESSFUL) {
8199 dev_err(&pdev->dev, "Failed to save PCI config space\n");
8200 rc = -EIO;
8201 goto cleanup_nomem;
8202 }
8203
8204 if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
8205 goto cleanup_nomem;
8206
8207 if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
8208 goto cleanup_nomem;
8209
Wayne Boyer3e7ebdf2010-02-19 13:23:59 -08008210 if (ioa_cfg->sis64)
8211 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr64)
8212 + ((sizeof(struct ipr_config_table_entry64)
8213 * ioa_cfg->max_devs_supported)));
8214 else
8215 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr)
8216 + ((sizeof(struct ipr_config_table_entry)
8217 * ioa_cfg->max_devs_supported)));
8218
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219 rc = ipr_alloc_mem(ioa_cfg);
8220 if (rc < 0) {
8221 dev_err(&pdev->dev,
8222 "Couldn't allocate enough memory for device driver!\n");
8223 goto cleanup_nomem;
8224 }
8225
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06008226 /*
8227 * If HRRQ updated interrupt is not masked, or reset alert is set,
8228 * the card is in an unknown state and needs a hard reset
8229 */
8230 mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
Brian King473b1e82007-05-02 10:44:11 -05008231 interrupts = readl(ioa_cfg->regs.sense_interrupt_reg);
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06008232 uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
8233 if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
8234 ioa_cfg->needs_hard_reset = 1;
Brian King473b1e82007-05-02 10:44:11 -05008235 if (interrupts & IPR_PCII_ERROR_INTERRUPTS)
8236 ioa_cfg->needs_hard_reset = 1;
8237 if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
8238 ioa_cfg->ioa_unit_checked = 1;
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06008239
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
Wayne Boyer95fecd92009-06-16 15:13:28 -07008241 rc = request_irq(pdev->irq, ipr_isr,
8242 ioa_cfg->msi_received ? 0 : IRQF_SHARED,
8243 IPR_NAME, ioa_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244
8245 if (rc) {
8246 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
8247 pdev->irq, rc);
8248 goto cleanup_nolog;
8249 }
8250
Brian King463fc692007-05-07 17:09:05 -05008251 if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
8252 (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
8253 ioa_cfg->needs_warm_reset = 1;
8254 ioa_cfg->reset = ipr_reset_slot_reset;
8255 } else
8256 ioa_cfg->reset = ipr_reset_start_bist;
8257
Linus Torvalds1da177e2005-04-16 15:20:36 -07008258 spin_lock(&ipr_driver_lock);
8259 list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
8260 spin_unlock(&ipr_driver_lock);
8261
8262 LEAVE;
8263out:
8264 return rc;
8265
8266cleanup_nolog:
8267 ipr_free_mem(ioa_cfg);
8268cleanup_nomem:
8269 iounmap(ipr_regs);
Wayne Boyer95fecd92009-06-16 15:13:28 -07008270out_msi_disable:
8271 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272out_release_regions:
8273 pci_release_regions(pdev);
8274out_scsi_host_put:
8275 scsi_host_put(host);
8276out_disable:
8277 pci_disable_device(pdev);
8278 goto out;
8279}
8280
8281/**
8282 * ipr_scan_vsets - Scans for VSET devices
8283 * @ioa_cfg: ioa config struct
8284 *
8285 * Description: Since the VSET resources do not follow SAM in that we can have
8286 * sparse LUNs with no LUN 0, we have to scan for these ourselves.
8287 *
8288 * Return value:
8289 * none
8290 **/
8291static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
8292{
8293 int target, lun;
8294
8295 for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
8296 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
8297 scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
8298}
8299
8300/**
8301 * ipr_initiate_ioa_bringdown - Bring down an adapter
8302 * @ioa_cfg: ioa config struct
8303 * @shutdown_type: shutdown type
8304 *
8305 * Description: This function will initiate bringing down the adapter.
8306 * This consists of issuing an IOA shutdown to the adapter
8307 * to flush the cache, and running BIST.
8308 * If the caller needs to wait on the completion of the reset,
8309 * the caller must sleep on the reset_wait_q.
8310 *
8311 * Return value:
8312 * none
8313 **/
8314static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
8315 enum ipr_shutdown_type shutdown_type)
8316{
8317 ENTER;
8318 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
8319 ioa_cfg->sdt_state = ABORT_DUMP;
8320 ioa_cfg->reset_retries = 0;
8321 ioa_cfg->in_ioa_bringdown = 1;
8322 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
8323 LEAVE;
8324}
8325
8326/**
8327 * __ipr_remove - Remove a single adapter
8328 * @pdev: pci device struct
8329 *
8330 * Adapter hot plug remove entry point.
8331 *
8332 * Return value:
8333 * none
8334 **/
8335static void __ipr_remove(struct pci_dev *pdev)
8336{
8337 unsigned long host_lock_flags = 0;
8338 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8339 ENTER;
8340
8341 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
Brian King970ea292007-04-26 16:00:06 -05008342 while(ioa_cfg->in_reset_reload) {
8343 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8344 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
8345 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8346 }
8347
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
8349
8350 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8351 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
brking@us.ibm.com 5cbf5ea2005-05-02 19:50:47 -05008352 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8354
8355 spin_lock(&ipr_driver_lock);
8356 list_del(&ioa_cfg->queue);
8357 spin_unlock(&ipr_driver_lock);
8358
8359 if (ioa_cfg->sdt_state == ABORT_DUMP)
8360 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
8361 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8362
8363 ipr_free_all_resources(ioa_cfg);
8364
8365 LEAVE;
8366}
8367
8368/**
8369 * ipr_remove - IOA hot plug remove entry point
8370 * @pdev: pci device struct
8371 *
8372 * Adapter hot plug remove entry point.
8373 *
8374 * Return value:
8375 * none
8376 **/
Kleber S. Souzaf3816422009-04-22 10:50:28 -03008377static void __devexit ipr_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378{
8379 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8380
8381 ENTER;
8382
Tony Jonesee959b02008-02-22 00:13:36 +01008383 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 &ipr_trace_attr);
Tony Jonesee959b02008-02-22 00:13:36 +01008385 ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 &ipr_dump_attr);
8387 scsi_remove_host(ioa_cfg->host);
8388
8389 __ipr_remove(pdev);
8390
8391 LEAVE;
8392}
8393
8394/**
8395 * ipr_probe - Adapter hot plug add entry point
8396 *
8397 * Return value:
8398 * 0 on success / non-zero on failure
8399 **/
8400static int __devinit ipr_probe(struct pci_dev *pdev,
8401 const struct pci_device_id *dev_id)
8402{
8403 struct ipr_ioa_cfg *ioa_cfg;
8404 int rc;
8405
8406 rc = ipr_probe_ioa(pdev, dev_id);
8407
8408 if (rc)
8409 return rc;
8410
8411 ioa_cfg = pci_get_drvdata(pdev);
8412 rc = ipr_probe_ioa_part2(ioa_cfg);
8413
8414 if (rc) {
8415 __ipr_remove(pdev);
8416 return rc;
8417 }
8418
8419 rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
8420
8421 if (rc) {
8422 __ipr_remove(pdev);
8423 return rc;
8424 }
8425
Tony Jonesee959b02008-02-22 00:13:36 +01008426 rc = ipr_create_trace_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008427 &ipr_trace_attr);
8428
8429 if (rc) {
8430 scsi_remove_host(ioa_cfg->host);
8431 __ipr_remove(pdev);
8432 return rc;
8433 }
8434
Tony Jonesee959b02008-02-22 00:13:36 +01008435 rc = ipr_create_dump_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436 &ipr_dump_attr);
8437
8438 if (rc) {
Tony Jonesee959b02008-02-22 00:13:36 +01008439 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440 &ipr_trace_attr);
8441 scsi_remove_host(ioa_cfg->host);
8442 __ipr_remove(pdev);
8443 return rc;
8444 }
8445
8446 scsi_scan_host(ioa_cfg->host);
8447 ipr_scan_vsets(ioa_cfg);
8448 scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
8449 ioa_cfg->allow_ml_add_del = 1;
brking@us.ibm.com11cd8f12005-11-01 17:00:11 -06008450 ioa_cfg->host->max_channel = IPR_VSET_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 schedule_work(&ioa_cfg->work_q);
8452 return 0;
8453}
8454
8455/**
8456 * ipr_shutdown - Shutdown handler.
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07008457 * @pdev: pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458 *
8459 * This function is invoked upon system shutdown/reboot. It will issue
8460 * an adapter shutdown to the adapter to flush the write cache.
8461 *
8462 * Return value:
8463 * none
8464 **/
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07008465static void ipr_shutdown(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466{
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07008467 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008468 unsigned long lock_flags = 0;
8469
8470 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05008471 while(ioa_cfg->in_reset_reload) {
8472 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8473 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
8474 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8475 }
8476
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
8478 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8479 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
8480}
8481
8482static struct pci_device_id ipr_pci_table[] __devinitdata = {
8483 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06008484 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06008486 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06008488 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06008490 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06008492 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06008494 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06008496 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06008497 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King5469cb52007-03-29 12:42:40 -05008498 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
8499 IPR_USE_LONG_TRANSOP_TIMEOUT },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06008500 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King6d84c942007-01-23 11:25:23 -06008501 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06008502 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King22d2e402007-04-26 16:00:13 -05008503 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
8504 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06008505 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King5469cb52007-03-29 12:42:40 -05008506 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
8507 IPR_USE_LONG_TRANSOP_TIMEOUT },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06008508 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King6d84c942007-01-23 11:25:23 -06008509 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06008510 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King22d2e402007-04-26 16:00:13 -05008511 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
8512 IPR_USE_LONG_TRANSOP_TIMEOUT},
Brian King60e74862006-11-21 10:28:10 -06008513 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King5469cb52007-03-29 12:42:40 -05008514 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
8515 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06008516 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
Brian King22d2e402007-04-26 16:00:13 -05008517 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
8518 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King185eb312007-03-29 12:42:53 -05008519 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
8520 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575D, 0, 0,
8521 IPR_USE_LONG_TRANSOP_TIMEOUT },
8522 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
8523 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
8524 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
Brian King5469cb52007-03-29 12:42:40 -05008525 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
Brian King463fc692007-05-07 17:09:05 -05008526 IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
Brian King6d84c942007-01-23 11:25:23 -06008528 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King6d84c942007-01-23 11:25:23 -06008530 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06008531 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King5469cb52007-03-29 12:42:40 -05008532 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
8533 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06008534 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King5469cb52007-03-29 12:42:40 -05008535 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
8536 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King185eb312007-03-29 12:42:53 -05008537 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SCAMP_E,
8538 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0,
8539 IPR_USE_LONG_TRANSOP_TIMEOUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540 { }
8541};
8542MODULE_DEVICE_TABLE(pci, ipr_pci_table);
8543
Linas Vepstasf8a88b192006-02-03 16:52:42 -06008544static struct pci_error_handlers ipr_err_handler = {
8545 .error_detected = ipr_pci_error_detected,
8546 .slot_reset = ipr_pci_slot_reset,
8547};
8548
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549static struct pci_driver ipr_driver = {
8550 .name = IPR_NAME,
8551 .id_table = ipr_pci_table,
8552 .probe = ipr_probe,
Kleber S. Souzaf3816422009-04-22 10:50:28 -03008553 .remove = __devexit_p(ipr_remove),
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07008554 .shutdown = ipr_shutdown,
Linas Vepstasf8a88b192006-02-03 16:52:42 -06008555 .err_handler = &ipr_err_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556};
8557
8558/**
8559 * ipr_init - Module entry point
8560 *
8561 * Return value:
8562 * 0 on success / negative value on failure
8563 **/
8564static int __init ipr_init(void)
8565{
8566 ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
8567 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
8568
Henrik Kretzschmardcbccbde2006-09-25 16:58:58 -07008569 return pci_register_driver(&ipr_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570}
8571
8572/**
8573 * ipr_exit - Module unload
8574 *
8575 * Module unload entry point.
8576 *
8577 * Return value:
8578 * none
8579 **/
8580static void __exit ipr_exit(void)
8581{
8582 pci_unregister_driver(&ipr_driver);
8583}
8584
8585module_init(ipr_init);
8586module_exit(ipr_exit);