blob: d40d5c79fff1ea25a7aaaf860d53885e844123be [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ipr.c -- driver for IBM Power Linux RAID adapters
3 *
4 * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
5 *
6 * Copyright (C) 2003, 2004 IBM Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24/*
25 * Notes:
26 *
27 * This driver is used to control the following SCSI adapters:
28 *
29 * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
30 *
31 * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
32 * PCI-X Dual Channel Ultra 320 SCSI Adapter
33 * PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
34 * Embedded SCSI adapter on p615 and p655 systems
35 *
36 * Supported Hardware Features:
37 * - Ultra 320 SCSI controller
38 * - PCI-X host interface
39 * - Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
40 * - Non-Volatile Write Cache
41 * - Supports attachment of non-RAID disks, tape, and optical devices
42 * - RAID Levels 0, 5, 10
43 * - Hot spare
44 * - Background Parity Checking
45 * - Background Data Scrubbing
46 * - Ability to increase the capacity of an existing RAID 5 disk array
47 * by adding disks
48 *
49 * Driver Features:
50 * - Tagged command queuing
51 * - Adapter microcode download
52 * - PCI hot plug
53 * - SCSI device hot plug
54 *
55 */
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/fs.h>
58#include <linux/init.h>
59#include <linux/types.h>
60#include <linux/errno.h>
61#include <linux/kernel.h>
62#include <linux/ioport.h>
63#include <linux/delay.h>
64#include <linux/pci.h>
65#include <linux/wait.h>
66#include <linux/spinlock.h>
67#include <linux/sched.h>
68#include <linux/interrupt.h>
69#include <linux/blkdev.h>
70#include <linux/firmware.h>
71#include <linux/module.h>
72#include <linux/moduleparam.h>
Brian King35a39692006-09-25 12:39:20 -050073#include <linux/libata.h>
Brian King0ce3a7e2008-07-11 13:37:50 -050074#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/io.h>
76#include <asm/irq.h>
77#include <asm/processor.h>
78#include <scsi/scsi.h>
79#include <scsi/scsi_host.h>
80#include <scsi/scsi_tcq.h>
81#include <scsi/scsi_eh.h>
82#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include "ipr.h"
84
85/*
86 * Global Data
87 */
Denis Chengb7d68ca2007-12-13 16:14:27 -080088static LIST_HEAD(ipr_ioa_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
90static unsigned int ipr_max_speed = 1;
91static int ipr_testmode = 0;
92static unsigned int ipr_fastfail = 0;
Brian King5469cb52007-03-29 12:42:40 -050093static unsigned int ipr_transop_timeout = 0;
brking@us.ibm.com62275042005-11-01 17:01:14 -060094static unsigned int ipr_enable_cache = 1;
brking@us.ibm.comd3c74872005-11-01 17:01:34 -060095static unsigned int ipr_debug = 0;
Brian Kingac09c342007-04-26 16:00:16 -050096static unsigned int ipr_dual_ioa_raid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static DEFINE_SPINLOCK(ipr_driver_lock);
98
99/* This table describes the differences between DMA controller chips */
100static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
Brian King60e74862006-11-21 10:28:10 -0600101 { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .mailbox = 0x0042C,
103 .cache_line_size = 0x20,
104 {
105 .set_interrupt_mask_reg = 0x0022C,
106 .clr_interrupt_mask_reg = 0x00230,
107 .sense_interrupt_mask_reg = 0x0022C,
108 .clr_interrupt_reg = 0x00228,
109 .sense_interrupt_reg = 0x00224,
110 .ioarrin_reg = 0x00404,
111 .sense_uproc_interrupt_reg = 0x00214,
112 .set_uproc_interrupt_reg = 0x00214,
113 .clr_uproc_interrupt_reg = 0x00218
114 }
115 },
116 { /* Snipe and Scamp */
117 .mailbox = 0x0052C,
118 .cache_line_size = 0x20,
119 {
120 .set_interrupt_mask_reg = 0x00288,
121 .clr_interrupt_mask_reg = 0x0028C,
122 .sense_interrupt_mask_reg = 0x00288,
123 .clr_interrupt_reg = 0x00284,
124 .sense_interrupt_reg = 0x00280,
125 .ioarrin_reg = 0x00504,
126 .sense_uproc_interrupt_reg = 0x00290,
127 .set_uproc_interrupt_reg = 0x00290,
128 .clr_uproc_interrupt_reg = 0x00294
129 }
130 },
131};
132
133static const struct ipr_chip_t ipr_chip[] = {
Wayne Boyer1be7bd82009-06-17 09:55:35 -0700134 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, IPR_USE_LSI, &ipr_chip_cfg[0] },
135 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, IPR_USE_LSI, &ipr_chip_cfg[0] },
136 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, IPR_USE_LSI, &ipr_chip_cfg[0] },
137 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, IPR_USE_LSI, &ipr_chip_cfg[0] },
138 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, IPR_USE_MSI, &ipr_chip_cfg[0] },
139 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, IPR_USE_LSI, &ipr_chip_cfg[1] },
140 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, IPR_USE_LSI, &ipr_chip_cfg[1] }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143static int ipr_max_bus_speeds [] = {
144 IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
145};
146
147MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
148MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
149module_param_named(max_speed, ipr_max_speed, uint, 0);
150MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
151module_param_named(log_level, ipr_log_level, uint, 0);
152MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
153module_param_named(testmode, ipr_testmode, int, 0);
154MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
Wayne Boyer2cf22be2009-02-24 11:36:00 -0800155module_param_named(fastfail, ipr_fastfail, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
157module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
158MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
brking@us.ibm.com62275042005-11-01 17:01:14 -0600159module_param_named(enable_cache, ipr_enable_cache, int, 0);
160MODULE_PARM_DESC(enable_cache, "Enable adapter's non-volatile write cache (default: 1)");
Wayne Boyer2cf22be2009-02-24 11:36:00 -0800161module_param_named(debug, ipr_debug, int, S_IRUGO | S_IWUSR);
brking@us.ibm.comd3c74872005-11-01 17:01:34 -0600162MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
Brian Kingac09c342007-04-26 16:00:16 -0500163module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
164MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165MODULE_LICENSE("GPL");
166MODULE_VERSION(IPR_DRIVER_VERSION);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* A constant array of IOASCs/URCs/Error Messages */
169static const
170struct ipr_error_table_t ipr_error_table[] = {
Brian King933916f2007-03-29 12:43:30 -0500171 {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 "8155: An unknown error was received"},
173 {0x00330000, 0, 0,
174 "Soft underlength error"},
175 {0x005A0000, 0, 0,
176 "Command to be cancelled not found"},
177 {0x00808000, 0, 0,
178 "Qualified success"},
Brian King933916f2007-03-29 12:43:30 -0500179 {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 "FFFE: Soft device bus error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500181 {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500182 "4101: Soft device bus fabric error"},
Brian King933916f2007-03-29 12:43:30 -0500183 {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 "FFF9: Device sector reassign successful"},
Brian King933916f2007-03-29 12:43:30 -0500185 {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 "FFF7: Media error recovered by device rewrite procedures"},
Brian King933916f2007-03-29 12:43:30 -0500187 {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 "7001: IOA sector reassignment successful"},
Brian King933916f2007-03-29 12:43:30 -0500189 {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 "FFF9: Soft media error. Sector reassignment recommended"},
Brian King933916f2007-03-29 12:43:30 -0500191 {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 "FFF7: Media error recovered by IOA rewrite procedures"},
Brian King933916f2007-03-29 12:43:30 -0500193 {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 "FF3D: Soft PCI bus error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500195 {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 "FFF6: Device hardware error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500197 {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 "FFF6: Device hardware error recovered by the device"},
Brian King933916f2007-03-29 12:43:30 -0500199 {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 "FF3D: Soft IOA error recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500201 {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 "FFFA: Undefined device response recovered by the IOA"},
Brian King933916f2007-03-29 12:43:30 -0500203 {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 "FFF6: Device bus error, message or command phase"},
Brian King933916f2007-03-29 12:43:30 -0500205 {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King35a39692006-09-25 12:39:20 -0500206 "FFFE: Task Management Function failed"},
Brian King933916f2007-03-29 12:43:30 -0500207 {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 "FFF6: Failure prediction threshold exceeded"},
Brian King933916f2007-03-29 12:43:30 -0500209 {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 "8009: Impending cache battery pack failure"},
211 {0x02040400, 0, 0,
212 "34FF: Disk device format in progress"},
Brian King65f56472007-04-26 16:00:12 -0500213 {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
214 "9070: IOA requested reset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 {0x023F0000, 0, 0,
216 "Synchronization required"},
217 {0x024E0000, 0, 0,
218 "No ready, IOA shutdown"},
219 {0x025A0000, 0, 0,
220 "Not ready, IOA has been shutdown"},
Brian King933916f2007-03-29 12:43:30 -0500221 {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 "3020: Storage subsystem configuration error"},
223 {0x03110B00, 0, 0,
224 "FFF5: Medium error, data unreadable, recommend reassign"},
225 {0x03110C00, 0, 0,
226 "7000: Medium error, data unreadable, do not reassign"},
Brian King933916f2007-03-29 12:43:30 -0500227 {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 "FFF3: Disk media format bad"},
Brian King933916f2007-03-29 12:43:30 -0500229 {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 "3002: Addressed device failed to respond to selection"},
Brian King933916f2007-03-29 12:43:30 -0500231 {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 "3100: Device bus error"},
Brian King933916f2007-03-29 12:43:30 -0500233 {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 "3109: IOA timed out a device command"},
235 {0x04088000, 0, 0,
236 "3120: SCSI bus is not operational"},
Brian King933916f2007-03-29 12:43:30 -0500237 {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500238 "4100: Hard device bus fabric error"},
Brian King933916f2007-03-29 12:43:30 -0500239 {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 "9000: IOA reserved area data check"},
Brian King933916f2007-03-29 12:43:30 -0500241 {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 "9001: IOA reserved area invalid data pattern"},
Brian King933916f2007-03-29 12:43:30 -0500243 {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 "9002: IOA reserved area LRC error"},
Brian King933916f2007-03-29 12:43:30 -0500245 {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 "102E: Out of alternate sectors for disk storage"},
Brian King933916f2007-03-29 12:43:30 -0500247 {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 "FFF4: Data transfer underlength error"},
Brian King933916f2007-03-29 12:43:30 -0500249 {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 "FFF4: Data transfer overlength error"},
Brian King933916f2007-03-29 12:43:30 -0500251 {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 "3400: Logical unit failure"},
Brian King933916f2007-03-29 12:43:30 -0500253 {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 "FFF4: Device microcode is corrupt"},
Brian King933916f2007-03-29 12:43:30 -0500255 {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 "8150: PCI bus error"},
257 {0x04430000, 1, 0,
258 "Unsupported device bus message received"},
Brian King933916f2007-03-29 12:43:30 -0500259 {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 "FFF4: Disk device problem"},
Brian King933916f2007-03-29 12:43:30 -0500261 {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 "8150: Permanent IOA failure"},
Brian King933916f2007-03-29 12:43:30 -0500263 {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 "3010: Disk device returned wrong response to IOA"},
Brian King933916f2007-03-29 12:43:30 -0500265 {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 "8151: IOA microcode error"},
267 {0x04448500, 0, 0,
268 "Device bus status error"},
Brian King933916f2007-03-29 12:43:30 -0500269 {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 "8157: IOA error requiring IOA reset to recover"},
Brian King35a39692006-09-25 12:39:20 -0500271 {0x04448700, 0, 0,
272 "ATA device status error"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 {0x04490000, 0, 0,
274 "Message reject received from the device"},
Brian King933916f2007-03-29 12:43:30 -0500275 {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 "8008: A permanent cache battery pack failure occurred"},
Brian King933916f2007-03-29 12:43:30 -0500277 {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 "9090: Disk unit has been modified after the last known status"},
Brian King933916f2007-03-29 12:43:30 -0500279 {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 "9081: IOA detected device error"},
Brian King933916f2007-03-29 12:43:30 -0500281 {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 "9082: IOA detected device error"},
Brian King933916f2007-03-29 12:43:30 -0500283 {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 "3110: Device bus error, message or command phase"},
Brian King933916f2007-03-29 12:43:30 -0500285 {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
Brian King35a39692006-09-25 12:39:20 -0500286 "3110: SAS Command / Task Management Function failed"},
Brian King933916f2007-03-29 12:43:30 -0500287 {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 "9091: Incorrect hardware configuration change has been detected"},
Brian King933916f2007-03-29 12:43:30 -0500289 {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600290 "9073: Invalid multi-adapter configuration"},
Brian King933916f2007-03-29 12:43:30 -0500291 {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500292 "4010: Incorrect connection between cascaded expanders"},
Brian King933916f2007-03-29 12:43:30 -0500293 {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500294 "4020: Connections exceed IOA design limits"},
Brian King933916f2007-03-29 12:43:30 -0500295 {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500296 "4030: Incorrect multipath connection"},
Brian King933916f2007-03-29 12:43:30 -0500297 {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500298 "4110: Unsupported enclosure function"},
Brian King933916f2007-03-29 12:43:30 -0500299 {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 "FFF4: Command to logical unit failed"},
301 {0x05240000, 1, 0,
302 "Illegal request, invalid request type or request packet"},
303 {0x05250000, 0, 0,
304 "Illegal request, invalid resource handle"},
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600305 {0x05258000, 0, 0,
306 "Illegal request, commands not allowed to this device"},
307 {0x05258100, 0, 0,
308 "Illegal request, command not allowed to a secondary adapter"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 {0x05260000, 0, 0,
310 "Illegal request, invalid field in parameter list"},
311 {0x05260100, 0, 0,
312 "Illegal request, parameter not supported"},
313 {0x05260200, 0, 0,
314 "Illegal request, parameter value invalid"},
315 {0x052C0000, 0, 0,
316 "Illegal request, command sequence error"},
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600317 {0x052C8000, 1, 0,
318 "Illegal request, dual adapter support not enabled"},
Brian King933916f2007-03-29 12:43:30 -0500319 {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 "9031: Array protection temporarily suspended, protection resuming"},
Brian King933916f2007-03-29 12:43:30 -0500321 {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 "9040: Array protection temporarily suspended, protection resuming"},
Brian King933916f2007-03-29 12:43:30 -0500323 {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500324 "3140: Device bus not ready to ready transition"},
Brian King933916f2007-03-29 12:43:30 -0500325 {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 "FFFB: SCSI bus was reset"},
327 {0x06290500, 0, 0,
328 "FFFE: SCSI bus transition to single ended"},
329 {0x06290600, 0, 0,
330 "FFFE: SCSI bus transition to LVD"},
Brian King933916f2007-03-29 12:43:30 -0500331 {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 "FFFB: SCSI bus was reset by another initiator"},
Brian King933916f2007-03-29 12:43:30 -0500333 {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 "3029: A device replacement has occurred"},
Brian King933916f2007-03-29 12:43:30 -0500335 {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 "9051: IOA cache data exists for a missing or failed device"},
Brian King933916f2007-03-29 12:43:30 -0500337 {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600338 "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
Brian King933916f2007-03-29 12:43:30 -0500339 {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 "9025: Disk unit is not supported at its physical location"},
Brian King933916f2007-03-29 12:43:30 -0500341 {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 "3020: IOA detected a SCSI bus configuration error"},
Brian King933916f2007-03-29 12:43:30 -0500343 {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 "3150: SCSI bus configuration error"},
Brian King933916f2007-03-29 12:43:30 -0500345 {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600346 "9074: Asymmetric advanced function disk configuration"},
Brian King933916f2007-03-29 12:43:30 -0500347 {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500348 "4040: Incomplete multipath connection between IOA and enclosure"},
Brian King933916f2007-03-29 12:43:30 -0500349 {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500350 "4041: Incomplete multipath connection between enclosure and device"},
Brian King933916f2007-03-29 12:43:30 -0500351 {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500352 "9075: Incomplete multipath connection between IOA and remote IOA"},
Brian King933916f2007-03-29 12:43:30 -0500353 {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500354 "9076: Configuration error, missing remote IOA"},
Brian King933916f2007-03-29 12:43:30 -0500355 {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500356 "4050: Enclosure does not support a required multipath function"},
Wayne Boyerb75424f2009-01-28 08:24:50 -0800357 {0x06690000, 0, IPR_DEFAULT_LOG_LEVEL,
358 "4070: Logically bad block written on device"},
Brian King933916f2007-03-29 12:43:30 -0500359 {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 "9041: Array protection temporarily suspended"},
Brian King933916f2007-03-29 12:43:30 -0500361 {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 "9042: Corrupt array parity detected on specified device"},
Brian King933916f2007-03-29 12:43:30 -0500363 {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 "9030: Array no longer protected due to missing or failed disk unit"},
Brian King933916f2007-03-29 12:43:30 -0500365 {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600366 "9071: Link operational transition"},
Brian King933916f2007-03-29 12:43:30 -0500367 {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -0600368 "9072: Link not operational transition"},
Brian King933916f2007-03-29 12:43:30 -0500369 {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 "9032: Array exposed but still protected"},
Brian Kinge4353402007-03-29 12:43:37 -0500371 {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
372 "70DD: Device forced failed by disrupt device command"},
Brian King933916f2007-03-29 12:43:30 -0500373 {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500374 "4061: Multipath redundancy level got better"},
Brian King933916f2007-03-29 12:43:30 -0500375 {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
Brian King896bbd22006-08-02 14:57:44 -0500376 "4060: Multipath redundancy level got worse"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 {0x07270000, 0, 0,
378 "Failure due to other device"},
Brian King933916f2007-03-29 12:43:30 -0500379 {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 "9008: IOA does not support functions expected by devices"},
Brian King933916f2007-03-29 12:43:30 -0500381 {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 "9010: Cache data associated with attached devices cannot be found"},
Brian King933916f2007-03-29 12:43:30 -0500383 {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 "9011: Cache data belongs to devices other than those attached"},
Brian King933916f2007-03-29 12:43:30 -0500385 {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 "9020: Array missing 2 or more devices with only 1 device present"},
Brian King933916f2007-03-29 12:43:30 -0500387 {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 "9021: Array missing 2 or more devices with 2 or more devices present"},
Brian King933916f2007-03-29 12:43:30 -0500389 {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 "9022: Exposed array is missing a required device"},
Brian King933916f2007-03-29 12:43:30 -0500391 {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 "9023: Array member(s) not at required physical locations"},
Brian King933916f2007-03-29 12:43:30 -0500393 {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 "9024: Array not functional due to present hardware configuration"},
Brian King933916f2007-03-29 12:43:30 -0500395 {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 "9026: Array not functional due to present hardware configuration"},
Brian King933916f2007-03-29 12:43:30 -0500397 {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 "9027: Array is missing a device and parity is out of sync"},
Brian King933916f2007-03-29 12:43:30 -0500399 {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 "9028: Maximum number of arrays already exist"},
Brian King933916f2007-03-29 12:43:30 -0500401 {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 "9050: Required cache data cannot be located for a disk unit"},
Brian King933916f2007-03-29 12:43:30 -0500403 {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 "9052: Cache data exists for a device that has been modified"},
Brian King933916f2007-03-29 12:43:30 -0500405 {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 "9054: IOA resources not available due to previous problems"},
Brian King933916f2007-03-29 12:43:30 -0500407 {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 "9092: Disk unit requires initialization before use"},
Brian King933916f2007-03-29 12:43:30 -0500409 {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 "9029: Incorrect hardware configuration change has been detected"},
Brian King933916f2007-03-29 12:43:30 -0500411 {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 "9060: One or more disk pairs are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500413 {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 "9061: One or more disks are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500415 {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 "9062: One or more disks are missing from an array"},
Brian King933916f2007-03-29 12:43:30 -0500417 {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 "9063: Maximum number of functional arrays has been exceeded"},
419 {0x0B260000, 0, 0,
420 "Aborted command, invalid descriptor"},
421 {0x0B5A0000, 0, 0,
422 "Command terminated by host"}
423};
424
425static const struct ipr_ses_table_entry ipr_ses_table[] = {
426 { "2104-DL1 ", "XXXXXXXXXXXXXXXX", 80 },
427 { "2104-TL1 ", "XXXXXXXXXXXXXXXX", 80 },
428 { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
429 { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
430 { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
431 { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
432 { "2104-DU3 ", "XXXXXXXXXXXXXXXX", 160 },
433 { "2104-TU3 ", "XXXXXXXXXXXXXXXX", 160 },
434 { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
435 { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
436 { "St V1S2 ", "XXXXXXXXXXXXXXXX", 160 },
437 { "HSBPD4M PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
438 { "VSBPD1H U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
439};
440
441/*
442 * Function Prototypes
443 */
444static int ipr_reset_alert(struct ipr_cmnd *);
445static void ipr_process_ccn(struct ipr_cmnd *);
446static void ipr_process_error(struct ipr_cmnd *);
447static void ipr_reset_ioa_job(struct ipr_cmnd *);
448static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
449 enum ipr_shutdown_type);
450
451#ifdef CONFIG_SCSI_IPR_TRACE
452/**
453 * ipr_trc_hook - Add a trace entry to the driver trace
454 * @ipr_cmd: ipr command struct
455 * @type: trace type
456 * @add_data: additional data
457 *
458 * Return value:
459 * none
460 **/
461static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
462 u8 type, u32 add_data)
463{
464 struct ipr_trace_entry *trace_entry;
465 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
466
467 trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
468 trace_entry->time = jiffies;
469 trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
470 trace_entry->type = type;
Brian King35a39692006-09-25 12:39:20 -0500471 trace_entry->ata_op_code = ipr_cmd->ioarcb.add_data.u.regs.command;
472 trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
474 trace_entry->u.add_data = add_data;
475}
476#else
477#define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
478#endif
479
480/**
481 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
482 * @ipr_cmd: ipr command struct
483 *
484 * Return value:
485 * none
486 **/
487static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
488{
489 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
490 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
Brian King51b1c7e2007-03-29 12:43:50 -0500491 dma_addr_t dma_addr = be32_to_cpu(ioarcb->ioarcb_host_pci_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
494 ioarcb->write_data_transfer_length = 0;
495 ioarcb->read_data_transfer_length = 0;
496 ioarcb->write_ioadl_len = 0;
497 ioarcb->read_ioadl_len = 0;
Brian King51b1c7e2007-03-29 12:43:50 -0500498 ioarcb->write_ioadl_addr =
499 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
500 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 ioasa->ioasc = 0;
502 ioasa->residual_data_len = 0;
Brian King35a39692006-09-25 12:39:20 -0500503 ioasa->u.gata.status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 ipr_cmd->scsi_cmd = NULL;
Brian King35a39692006-09-25 12:39:20 -0500506 ipr_cmd->qc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 ipr_cmd->sense_buffer[0] = 0;
508 ipr_cmd->dma_use_sg = 0;
509}
510
511/**
512 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
513 * @ipr_cmd: ipr command struct
514 *
515 * Return value:
516 * none
517 **/
518static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
519{
520 ipr_reinit_ipr_cmnd(ipr_cmd);
521 ipr_cmd->u.scratch = 0;
522 ipr_cmd->sibling = NULL;
523 init_timer(&ipr_cmd->timer);
524}
525
526/**
527 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
528 * @ioa_cfg: ioa config struct
529 *
530 * Return value:
531 * pointer to ipr command struct
532 **/
533static
534struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
535{
536 struct ipr_cmnd *ipr_cmd;
537
538 ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
539 list_del(&ipr_cmd->queue);
540 ipr_init_ipr_cmnd(ipr_cmd);
541
542 return ipr_cmd;
543}
544
545/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
547 * @ioa_cfg: ioa config struct
548 * @clr_ints: interrupts to clear
549 *
550 * This function masks all interrupts on the adapter, then clears the
551 * interrupts specified in the mask
552 *
553 * Return value:
554 * none
555 **/
556static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
557 u32 clr_ints)
558{
559 volatile u32 int_reg;
560
561 /* Stop new interrupts */
562 ioa_cfg->allow_interrupts = 0;
563
564 /* Set interrupt mask to stop all new interrupts */
565 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
566
567 /* Clear any pending interrupts */
568 writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
569 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
570}
571
572/**
573 * ipr_save_pcix_cmd_reg - Save PCI-X command register
574 * @ioa_cfg: ioa config struct
575 *
576 * Return value:
577 * 0 on success / -EIO on failure
578 **/
579static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
580{
581 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
582
Brian King7dce0e12007-01-23 11:25:30 -0600583 if (pcix_cmd_reg == 0)
584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
587 &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
588 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
589 return -EIO;
590 }
591
592 ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
593 return 0;
594}
595
596/**
597 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
598 * @ioa_cfg: ioa config struct
599 *
600 * Return value:
601 * 0 on success / -EIO on failure
602 **/
603static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
604{
605 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
606
607 if (pcix_cmd_reg) {
608 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
609 ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
610 dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
611 return -EIO;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 return 0;
616}
617
618/**
Brian King35a39692006-09-25 12:39:20 -0500619 * ipr_sata_eh_done - done function for aborted SATA commands
620 * @ipr_cmd: ipr command struct
621 *
622 * This function is invoked for ops generated to SATA
623 * devices which are being aborted.
624 *
625 * Return value:
626 * none
627 **/
628static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
629{
630 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
631 struct ata_queued_cmd *qc = ipr_cmd->qc;
632 struct ipr_sata_port *sata_port = qc->ap->private_data;
633
634 qc->err_mask |= AC_ERR_OTHER;
635 sata_port->ioasa.status |= ATA_BUSY;
636 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
637 ata_qc_complete(qc);
638}
639
640/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * ipr_scsi_eh_done - mid-layer done function for aborted ops
642 * @ipr_cmd: ipr command struct
643 *
644 * This function is invoked by the interrupt handler for
645 * ops generated by the SCSI mid-layer which are being aborted.
646 *
647 * Return value:
648 * none
649 **/
650static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
651{
652 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
653 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
654
655 scsi_cmd->result |= (DID_ERROR << 16);
656
FUJITA Tomonori63015bc2007-05-26 00:26:59 +0900657 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 scsi_cmd->scsi_done(scsi_cmd);
659 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
660}
661
662/**
663 * ipr_fail_all_ops - Fails all outstanding ops.
664 * @ioa_cfg: ioa config struct
665 *
666 * This function fails all outstanding ops.
667 *
668 * Return value:
669 * none
670 **/
671static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
672{
673 struct ipr_cmnd *ipr_cmd, *temp;
674
675 ENTER;
676 list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
677 list_del(&ipr_cmd->queue);
678
679 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
680 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
681
682 if (ipr_cmd->scsi_cmd)
683 ipr_cmd->done = ipr_scsi_eh_done;
Brian King35a39692006-09-25 12:39:20 -0500684 else if (ipr_cmd->qc)
685 ipr_cmd->done = ipr_sata_eh_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
688 del_timer(&ipr_cmd->timer);
689 ipr_cmd->done(ipr_cmd);
690 }
691
692 LEAVE;
693}
694
695/**
696 * ipr_do_req - Send driver initiated requests.
697 * @ipr_cmd: ipr command struct
698 * @done: done function
699 * @timeout_func: timeout function
700 * @timeout: timeout value
701 *
702 * This function sends the specified command to the adapter with the
703 * timeout given. The done function is invoked on command completion.
704 *
705 * Return value:
706 * none
707 **/
708static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
709 void (*done) (struct ipr_cmnd *),
710 void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
711{
712 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
713
714 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
715
716 ipr_cmd->done = done;
717
718 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
719 ipr_cmd->timer.expires = jiffies + timeout;
720 ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
721
722 add_timer(&ipr_cmd->timer);
723
724 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
725
726 mb();
727 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
728 ioa_cfg->regs.ioarrin_reg);
729}
730
731/**
732 * ipr_internal_cmd_done - Op done function for an internally generated op.
733 * @ipr_cmd: ipr command struct
734 *
735 * This function is the op done function for an internally generated,
736 * blocking op. It simply wakes the sleeping thread.
737 *
738 * Return value:
739 * none
740 **/
741static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
742{
743 if (ipr_cmd->sibling)
744 ipr_cmd->sibling = NULL;
745 else
746 complete(&ipr_cmd->completion);
747}
748
749/**
750 * ipr_send_blocking_cmd - Send command and sleep on its completion.
751 * @ipr_cmd: ipr command struct
752 * @timeout_func: function to invoke if command times out
753 * @timeout: timeout
754 *
755 * Return value:
756 * none
757 **/
758static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
759 void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
760 u32 timeout)
761{
762 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
763
764 init_completion(&ipr_cmd->completion);
765 ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
766
767 spin_unlock_irq(ioa_cfg->host->host_lock);
768 wait_for_completion(&ipr_cmd->completion);
769 spin_lock_irq(ioa_cfg->host->host_lock);
770}
771
772/**
773 * ipr_send_hcam - Send an HCAM to the adapter.
774 * @ioa_cfg: ioa config struct
775 * @type: HCAM type
776 * @hostrcb: hostrcb struct
777 *
778 * This function will send a Host Controlled Async command to the adapter.
779 * If HCAMs are currently not allowed to be issued to the adapter, it will
780 * place the hostrcb on the free queue.
781 *
782 * Return value:
783 * none
784 **/
785static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
786 struct ipr_hostrcb *hostrcb)
787{
788 struct ipr_cmnd *ipr_cmd;
789 struct ipr_ioarcb *ioarcb;
790
791 if (ioa_cfg->allow_cmds) {
792 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
793 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
794 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
795
796 ipr_cmd->u.hostrcb = hostrcb;
797 ioarcb = &ipr_cmd->ioarcb;
798
799 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
800 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
801 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
802 ioarcb->cmd_pkt.cdb[1] = type;
803 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
804 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
805
806 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
807 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
808 ipr_cmd->ioadl[0].flags_and_data_len =
809 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
810 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
811
812 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
813 ipr_cmd->done = ipr_process_ccn;
814 else
815 ipr_cmd->done = ipr_process_error;
816
817 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
818
819 mb();
820 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
821 ioa_cfg->regs.ioarrin_reg);
822 } else {
823 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
824 }
825}
826
827/**
828 * ipr_init_res_entry - Initialize a resource entry struct.
829 * @res: resource entry struct
830 *
831 * Return value:
832 * none
833 **/
834static void ipr_init_res_entry(struct ipr_resource_entry *res)
835{
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -0600836 res->needs_sync_complete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 res->in_erp = 0;
838 res->add_to_ml = 0;
839 res->del_from_ml = 0;
840 res->resetting_device = 0;
841 res->sdev = NULL;
Brian King35a39692006-09-25 12:39:20 -0500842 res->sata_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
845/**
846 * ipr_handle_config_change - Handle a config change from the adapter
847 * @ioa_cfg: ioa config struct
848 * @hostrcb: hostrcb
849 *
850 * Return value:
851 * none
852 **/
853static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
854 struct ipr_hostrcb *hostrcb)
855{
856 struct ipr_resource_entry *res = NULL;
857 struct ipr_config_table_entry *cfgte;
858 u32 is_ndn = 1;
859
860 cfgte = &hostrcb->hcam.u.ccn.cfgte;
861
862 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
863 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
864 sizeof(cfgte->res_addr))) {
865 is_ndn = 0;
866 break;
867 }
868 }
869
870 if (is_ndn) {
871 if (list_empty(&ioa_cfg->free_res_q)) {
872 ipr_send_hcam(ioa_cfg,
873 IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
874 hostrcb);
875 return;
876 }
877
878 res = list_entry(ioa_cfg->free_res_q.next,
879 struct ipr_resource_entry, queue);
880
881 list_del(&res->queue);
882 ipr_init_res_entry(res);
883 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
884 }
885
886 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
887
888 if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
889 if (res->sdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 res->del_from_ml = 1;
Brian King1121b792006-03-29 09:37:16 -0600891 res->cfgte.res_handle = IPR_INVALID_RES_HANDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (ioa_cfg->allow_ml_add_del)
893 schedule_work(&ioa_cfg->work_q);
894 } else
895 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
896 } else if (!res->sdev) {
897 res->add_to_ml = 1;
898 if (ioa_cfg->allow_ml_add_del)
899 schedule_work(&ioa_cfg->work_q);
900 }
901
902 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
903}
904
905/**
906 * ipr_process_ccn - Op done function for a CCN.
907 * @ipr_cmd: ipr command struct
908 *
909 * This function is the op done function for a configuration
910 * change notification host controlled async from the adapter.
911 *
912 * Return value:
913 * none
914 **/
915static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
916{
917 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
918 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
919 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
920
921 list_del(&hostrcb->queue);
922 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
923
924 if (ioasc) {
925 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
926 dev_err(&ioa_cfg->pdev->dev,
927 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
928
929 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
930 } else {
931 ipr_handle_config_change(ioa_cfg, hostrcb);
932 }
933}
934
935/**
Brian King8cf093e2007-04-26 16:00:14 -0500936 * strip_and_pad_whitespace - Strip and pad trailing whitespace.
937 * @i: index into buffer
938 * @buf: string to modify
939 *
940 * This function will strip all trailing whitespace, pad the end
941 * of the string with a single space, and NULL terminate the string.
942 *
943 * Return value:
944 * new length of string
945 **/
946static int strip_and_pad_whitespace(int i, char *buf)
947{
948 while (i && buf[i] == ' ')
949 i--;
950 buf[i+1] = ' ';
951 buf[i+2] = '\0';
952 return i + 2;
953}
954
955/**
956 * ipr_log_vpd_compact - Log the passed extended VPD compactly.
957 * @prefix: string to print at start of printk
958 * @hostrcb: hostrcb pointer
959 * @vpd: vendor/product id/sn struct
960 *
961 * Return value:
962 * none
963 **/
964static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
965 struct ipr_vpd *vpd)
966{
967 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
968 int i = 0;
969
970 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
971 i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
972
973 memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
974 i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
975
976 memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
977 buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
978
979 ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
980}
981
982/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * ipr_log_vpd - Log the passed VPD to the error log.
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600984 * @vpd: vendor/product id/sn struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 *
986 * Return value:
987 * none
988 **/
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600989static void ipr_log_vpd(struct ipr_vpd *vpd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
992 + IPR_SERIAL_NUM_LEN];
993
brking@us.ibm.comcfc32132005-11-01 17:00:18 -0600994 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
995 memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 IPR_PROD_ID_LEN);
997 buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
998 ipr_err("Vendor/Product ID: %s\n", buffer);
999
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001000 memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 buffer[IPR_SERIAL_NUM_LEN] = '\0';
1002 ipr_err(" Serial Number: %s\n", buffer);
1003}
1004
1005/**
Brian King8cf093e2007-04-26 16:00:14 -05001006 * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1007 * @prefix: string to print at start of printk
1008 * @hostrcb: hostrcb pointer
1009 * @vpd: vendor/product id/sn/wwn struct
1010 *
1011 * Return value:
1012 * none
1013 **/
1014static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1015 struct ipr_ext_vpd *vpd)
1016{
1017 ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1018 ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1019 be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1020}
1021
1022/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001023 * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1024 * @vpd: vendor/product id/sn/wwn struct
1025 *
1026 * Return value:
1027 * none
1028 **/
1029static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1030{
1031 ipr_log_vpd(&vpd->vpd);
1032 ipr_err(" WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1033 be32_to_cpu(vpd->wwid[1]));
1034}
1035
1036/**
1037 * ipr_log_enhanced_cache_error - Log a cache error.
1038 * @ioa_cfg: ioa config struct
1039 * @hostrcb: hostrcb struct
1040 *
1041 * Return value:
1042 * none
1043 **/
1044static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1045 struct ipr_hostrcb *hostrcb)
1046{
1047 struct ipr_hostrcb_type_12_error *error =
1048 &hostrcb->hcam.u.error.u.type_12_error;
1049
1050 ipr_err("-----Current Configuration-----\n");
1051 ipr_err("Cache Directory Card Information:\n");
1052 ipr_log_ext_vpd(&error->ioa_vpd);
1053 ipr_err("Adapter Card Information:\n");
1054 ipr_log_ext_vpd(&error->cfc_vpd);
1055
1056 ipr_err("-----Expected Configuration-----\n");
1057 ipr_err("Cache Directory Card Information:\n");
1058 ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1059 ipr_err("Adapter Card Information:\n");
1060 ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1061
1062 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1063 be32_to_cpu(error->ioa_data[0]),
1064 be32_to_cpu(error->ioa_data[1]),
1065 be32_to_cpu(error->ioa_data[2]));
1066}
1067
1068/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * ipr_log_cache_error - Log a cache error.
1070 * @ioa_cfg: ioa config struct
1071 * @hostrcb: hostrcb struct
1072 *
1073 * Return value:
1074 * none
1075 **/
1076static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1077 struct ipr_hostrcb *hostrcb)
1078{
1079 struct ipr_hostrcb_type_02_error *error =
1080 &hostrcb->hcam.u.error.u.type_02_error;
1081
1082 ipr_err("-----Current Configuration-----\n");
1083 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001084 ipr_log_vpd(&error->ioa_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001086 ipr_log_vpd(&error->cfc_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 ipr_err("-----Expected Configuration-----\n");
1089 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001090 ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001092 ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1095 be32_to_cpu(error->ioa_data[0]),
1096 be32_to_cpu(error->ioa_data[1]),
1097 be32_to_cpu(error->ioa_data[2]));
1098}
1099
1100/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001101 * ipr_log_enhanced_config_error - Log a configuration error.
1102 * @ioa_cfg: ioa config struct
1103 * @hostrcb: hostrcb struct
1104 *
1105 * Return value:
1106 * none
1107 **/
1108static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1109 struct ipr_hostrcb *hostrcb)
1110{
1111 int errors_logged, i;
1112 struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1113 struct ipr_hostrcb_type_13_error *error;
1114
1115 error = &hostrcb->hcam.u.error.u.type_13_error;
1116 errors_logged = be32_to_cpu(error->errors_logged);
1117
1118 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1119 be32_to_cpu(error->errors_detected), errors_logged);
1120
1121 dev_entry = error->dev;
1122
1123 for (i = 0; i < errors_logged; i++, dev_entry++) {
1124 ipr_err_separator;
1125
1126 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1127 ipr_log_ext_vpd(&dev_entry->vpd);
1128
1129 ipr_err("-----New Device Information-----\n");
1130 ipr_log_ext_vpd(&dev_entry->new_vpd);
1131
1132 ipr_err("Cache Directory Card Information:\n");
1133 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1134
1135 ipr_err("Adapter Card Information:\n");
1136 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1137 }
1138}
1139
1140/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * ipr_log_config_error - Log a configuration error.
1142 * @ioa_cfg: ioa config struct
1143 * @hostrcb: hostrcb struct
1144 *
1145 * Return value:
1146 * none
1147 **/
1148static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1149 struct ipr_hostrcb *hostrcb)
1150{
1151 int errors_logged, i;
1152 struct ipr_hostrcb_device_data_entry *dev_entry;
1153 struct ipr_hostrcb_type_03_error *error;
1154
1155 error = &hostrcb->hcam.u.error.u.type_03_error;
1156 errors_logged = be32_to_cpu(error->errors_logged);
1157
1158 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1159 be32_to_cpu(error->errors_detected), errors_logged);
1160
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001161 dev_entry = error->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 for (i = 0; i < errors_logged; i++, dev_entry++) {
1164 ipr_err_separator;
1165
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001166 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001167 ipr_log_vpd(&dev_entry->vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 ipr_err("-----New Device Information-----\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001170 ipr_log_vpd(&dev_entry->new_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 ipr_err("Cache Directory Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001173 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 ipr_err("Adapter Card Information:\n");
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001176 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1179 be32_to_cpu(dev_entry->ioa_data[0]),
1180 be32_to_cpu(dev_entry->ioa_data[1]),
1181 be32_to_cpu(dev_entry->ioa_data[2]),
1182 be32_to_cpu(dev_entry->ioa_data[3]),
1183 be32_to_cpu(dev_entry->ioa_data[4]));
1184 }
1185}
1186
1187/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001188 * ipr_log_enhanced_array_error - Log an array configuration error.
1189 * @ioa_cfg: ioa config struct
1190 * @hostrcb: hostrcb struct
1191 *
1192 * Return value:
1193 * none
1194 **/
1195static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1196 struct ipr_hostrcb *hostrcb)
1197{
1198 int i, num_entries;
1199 struct ipr_hostrcb_type_14_error *error;
1200 struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1201 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1202
1203 error = &hostrcb->hcam.u.error.u.type_14_error;
1204
1205 ipr_err_separator;
1206
1207 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1208 error->protection_level,
1209 ioa_cfg->host->host_no,
1210 error->last_func_vset_res_addr.bus,
1211 error->last_func_vset_res_addr.target,
1212 error->last_func_vset_res_addr.lun);
1213
1214 ipr_err_separator;
1215
1216 array_entry = error->array_member;
1217 num_entries = min_t(u32, be32_to_cpu(error->num_entries),
1218 sizeof(error->array_member));
1219
1220 for (i = 0; i < num_entries; i++, array_entry++) {
1221 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1222 continue;
1223
1224 if (be32_to_cpu(error->exposed_mode_adn) == i)
1225 ipr_err("Exposed Array Member %d:\n", i);
1226 else
1227 ipr_err("Array Member %d:\n", i);
1228
1229 ipr_log_ext_vpd(&array_entry->vpd);
1230 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1231 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1232 "Expected Location");
1233
1234 ipr_err_separator;
1235 }
1236}
1237
1238/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * ipr_log_array_error - Log an array configuration error.
1240 * @ioa_cfg: ioa config struct
1241 * @hostrcb: hostrcb struct
1242 *
1243 * Return value:
1244 * none
1245 **/
1246static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1247 struct ipr_hostrcb *hostrcb)
1248{
1249 int i;
1250 struct ipr_hostrcb_type_04_error *error;
1251 struct ipr_hostrcb_array_data_entry *array_entry;
1252 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1253
1254 error = &hostrcb->hcam.u.error.u.type_04_error;
1255
1256 ipr_err_separator;
1257
1258 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1259 error->protection_level,
1260 ioa_cfg->host->host_no,
1261 error->last_func_vset_res_addr.bus,
1262 error->last_func_vset_res_addr.target,
1263 error->last_func_vset_res_addr.lun);
1264
1265 ipr_err_separator;
1266
1267 array_entry = error->array_member;
1268
1269 for (i = 0; i < 18; i++) {
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001270 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 continue;
1272
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001273 if (be32_to_cpu(error->exposed_mode_adn) == i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 ipr_err("Exposed Array Member %d:\n", i);
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001275 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 ipr_err("Array Member %d:\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
brking@us.ibm.comcfc32132005-11-01 17:00:18 -06001278 ipr_log_vpd(&array_entry->vpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
brking@us.ibm.comfa15b1f2005-11-01 17:00:27 -06001280 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1281 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1282 "Expected Location");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 ipr_err_separator;
1285
1286 if (i == 9)
1287 array_entry = error->array_member2;
1288 else
1289 array_entry++;
1290 }
1291}
1292
1293/**
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001294 * ipr_log_hex_data - Log additional hex IOA error data.
Brian Kingac719ab2006-11-21 10:28:42 -06001295 * @ioa_cfg: ioa config struct
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001296 * @data: IOA error data
1297 * @len: data length
1298 *
1299 * Return value:
1300 * none
1301 **/
Brian Kingac719ab2006-11-21 10:28:42 -06001302static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001303{
1304 int i;
1305
1306 if (len == 0)
1307 return;
1308
Brian Kingac719ab2006-11-21 10:28:42 -06001309 if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1310 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1311
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001312 for (i = 0; i < len / 4; i += 4) {
1313 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1314 be32_to_cpu(data[i]),
1315 be32_to_cpu(data[i+1]),
1316 be32_to_cpu(data[i+2]),
1317 be32_to_cpu(data[i+3]));
1318 }
1319}
1320
1321/**
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001322 * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1323 * @ioa_cfg: ioa config struct
1324 * @hostrcb: hostrcb struct
1325 *
1326 * Return value:
1327 * none
1328 **/
1329static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1330 struct ipr_hostrcb *hostrcb)
1331{
1332 struct ipr_hostrcb_type_17_error *error;
1333
1334 error = &hostrcb->hcam.u.error.u.type_17_error;
1335 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
Brian King8cf093e2007-04-26 16:00:14 -05001336 strstrip(error->failure_reason);
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001337
Brian King8cf093e2007-04-26 16:00:14 -05001338 ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1339 be32_to_cpu(hostrcb->hcam.u.error.prc));
1340 ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
Brian Kingac719ab2006-11-21 10:28:42 -06001341 ipr_log_hex_data(ioa_cfg, error->data,
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001342 be32_to_cpu(hostrcb->hcam.length) -
1343 (offsetof(struct ipr_hostrcb_error, u) +
1344 offsetof(struct ipr_hostrcb_type_17_error, data)));
1345}
1346
1347/**
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001348 * ipr_log_dual_ioa_error - Log a dual adapter error.
1349 * @ioa_cfg: ioa config struct
1350 * @hostrcb: hostrcb struct
1351 *
1352 * Return value:
1353 * none
1354 **/
1355static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1356 struct ipr_hostrcb *hostrcb)
1357{
1358 struct ipr_hostrcb_type_07_error *error;
1359
1360 error = &hostrcb->hcam.u.error.u.type_07_error;
1361 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
Brian King8cf093e2007-04-26 16:00:14 -05001362 strstrip(error->failure_reason);
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001363
Brian King8cf093e2007-04-26 16:00:14 -05001364 ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1365 be32_to_cpu(hostrcb->hcam.u.error.prc));
1366 ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
Brian Kingac719ab2006-11-21 10:28:42 -06001367 ipr_log_hex_data(ioa_cfg, error->data,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001368 be32_to_cpu(hostrcb->hcam.length) -
1369 (offsetof(struct ipr_hostrcb_error, u) +
1370 offsetof(struct ipr_hostrcb_type_07_error, data)));
1371}
1372
Brian King49dc6a12006-11-21 10:28:35 -06001373static const struct {
1374 u8 active;
1375 char *desc;
1376} path_active_desc[] = {
1377 { IPR_PATH_NO_INFO, "Path" },
1378 { IPR_PATH_ACTIVE, "Active path" },
1379 { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1380};
1381
1382static const struct {
1383 u8 state;
1384 char *desc;
1385} path_state_desc[] = {
1386 { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1387 { IPR_PATH_HEALTHY, "is healthy" },
1388 { IPR_PATH_DEGRADED, "is degraded" },
1389 { IPR_PATH_FAILED, "is failed" }
1390};
1391
1392/**
1393 * ipr_log_fabric_path - Log a fabric path error
1394 * @hostrcb: hostrcb struct
1395 * @fabric: fabric descriptor
1396 *
1397 * Return value:
1398 * none
1399 **/
1400static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1401 struct ipr_hostrcb_fabric_desc *fabric)
1402{
1403 int i, j;
1404 u8 path_state = fabric->path_state;
1405 u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1406 u8 state = path_state & IPR_PATH_STATE_MASK;
1407
1408 for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1409 if (path_active_desc[i].active != active)
1410 continue;
1411
1412 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1413 if (path_state_desc[j].state != state)
1414 continue;
1415
1416 if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
1417 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
1418 path_active_desc[i].desc, path_state_desc[j].desc,
1419 fabric->ioa_port);
1420 } else if (fabric->cascaded_expander == 0xff) {
1421 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
1422 path_active_desc[i].desc, path_state_desc[j].desc,
1423 fabric->ioa_port, fabric->phy);
1424 } else if (fabric->phy == 0xff) {
1425 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
1426 path_active_desc[i].desc, path_state_desc[j].desc,
1427 fabric->ioa_port, fabric->cascaded_expander);
1428 } else {
1429 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
1430 path_active_desc[i].desc, path_state_desc[j].desc,
1431 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1432 }
1433 return;
1434 }
1435 }
1436
1437 ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
1438 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1439}
1440
1441static const struct {
1442 u8 type;
1443 char *desc;
1444} path_type_desc[] = {
1445 { IPR_PATH_CFG_IOA_PORT, "IOA port" },
1446 { IPR_PATH_CFG_EXP_PORT, "Expander port" },
1447 { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
1448 { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
1449};
1450
1451static const struct {
1452 u8 status;
1453 char *desc;
1454} path_status_desc[] = {
1455 { IPR_PATH_CFG_NO_PROB, "Functional" },
1456 { IPR_PATH_CFG_DEGRADED, "Degraded" },
1457 { IPR_PATH_CFG_FAILED, "Failed" },
1458 { IPR_PATH_CFG_SUSPECT, "Suspect" },
1459 { IPR_PATH_NOT_DETECTED, "Missing" },
1460 { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
1461};
1462
1463static const char *link_rate[] = {
1464 "unknown",
1465 "disabled",
1466 "phy reset problem",
1467 "spinup hold",
1468 "port selector",
1469 "unknown",
1470 "unknown",
1471 "unknown",
1472 "1.5Gbps",
1473 "3.0Gbps",
1474 "unknown",
1475 "unknown",
1476 "unknown",
1477 "unknown",
1478 "unknown",
1479 "unknown"
1480};
1481
1482/**
1483 * ipr_log_path_elem - Log a fabric path element.
1484 * @hostrcb: hostrcb struct
1485 * @cfg: fabric path element struct
1486 *
1487 * Return value:
1488 * none
1489 **/
1490static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
1491 struct ipr_hostrcb_config_element *cfg)
1492{
1493 int i, j;
1494 u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
1495 u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
1496
1497 if (type == IPR_PATH_CFG_NOT_EXIST)
1498 return;
1499
1500 for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
1501 if (path_type_desc[i].type != type)
1502 continue;
1503
1504 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
1505 if (path_status_desc[j].status != status)
1506 continue;
1507
1508 if (type == IPR_PATH_CFG_IOA_PORT) {
1509 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
1510 path_status_desc[j].desc, path_type_desc[i].desc,
1511 cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1512 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1513 } else {
1514 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
1515 ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
1516 path_status_desc[j].desc, path_type_desc[i].desc,
1517 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1518 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1519 } else if (cfg->cascaded_expander == 0xff) {
1520 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
1521 "WWN=%08X%08X\n", path_status_desc[j].desc,
1522 path_type_desc[i].desc, cfg->phy,
1523 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1524 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1525 } else if (cfg->phy == 0xff) {
1526 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
1527 "WWN=%08X%08X\n", path_status_desc[j].desc,
1528 path_type_desc[i].desc, cfg->cascaded_expander,
1529 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1530 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1531 } else {
1532 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
1533 "WWN=%08X%08X\n", path_status_desc[j].desc,
1534 path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
1535 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1536 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1537 }
1538 }
1539 return;
1540 }
1541 }
1542
1543 ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
1544 "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
1545 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
1546 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
1547}
1548
1549/**
1550 * ipr_log_fabric_error - Log a fabric error.
1551 * @ioa_cfg: ioa config struct
1552 * @hostrcb: hostrcb struct
1553 *
1554 * Return value:
1555 * none
1556 **/
1557static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
1558 struct ipr_hostrcb *hostrcb)
1559{
1560 struct ipr_hostrcb_type_20_error *error;
1561 struct ipr_hostrcb_fabric_desc *fabric;
1562 struct ipr_hostrcb_config_element *cfg;
1563 int i, add_len;
1564
1565 error = &hostrcb->hcam.u.error.u.type_20_error;
1566 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1567 ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
1568
1569 add_len = be32_to_cpu(hostrcb->hcam.length) -
1570 (offsetof(struct ipr_hostrcb_error, u) +
1571 offsetof(struct ipr_hostrcb_type_20_error, desc));
1572
1573 for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
1574 ipr_log_fabric_path(hostrcb, fabric);
1575 for_each_fabric_cfg(fabric, cfg)
1576 ipr_log_path_elem(hostrcb, cfg);
1577
1578 add_len -= be16_to_cpu(fabric->length);
1579 fabric = (struct ipr_hostrcb_fabric_desc *)
1580 ((unsigned long)fabric + be16_to_cpu(fabric->length));
1581 }
1582
Brian Kingac719ab2006-11-21 10:28:42 -06001583 ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
Brian King49dc6a12006-11-21 10:28:35 -06001584}
1585
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001586/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 * ipr_log_generic_error - Log an adapter error.
1588 * @ioa_cfg: ioa config struct
1589 * @hostrcb: hostrcb struct
1590 *
1591 * Return value:
1592 * none
1593 **/
1594static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1595 struct ipr_hostrcb *hostrcb)
1596{
Brian Kingac719ab2006-11-21 10:28:42 -06001597 ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001598 be32_to_cpu(hostrcb->hcam.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601/**
1602 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1603 * @ioasc: IOASC
1604 *
1605 * This function will return the index of into the ipr_error_table
1606 * for the specified IOASC. If the IOASC is not in the table,
1607 * 0 will be returned, which points to the entry used for unknown errors.
1608 *
1609 * Return value:
1610 * index into the ipr_error_table
1611 **/
1612static u32 ipr_get_error(u32 ioasc)
1613{
1614 int i;
1615
1616 for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
Brian King35a39692006-09-25 12:39:20 -05001617 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return i;
1619
1620 return 0;
1621}
1622
1623/**
1624 * ipr_handle_log_data - Log an adapter error.
1625 * @ioa_cfg: ioa config struct
1626 * @hostrcb: hostrcb struct
1627 *
1628 * This function logs an adapter error to the system.
1629 *
1630 * Return value:
1631 * none
1632 **/
1633static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1634 struct ipr_hostrcb *hostrcb)
1635{
1636 u32 ioasc;
1637 int error_index;
1638
1639 if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1640 return;
1641
1642 if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1643 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1644
1645 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1646
1647 if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1648 ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1649 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1650 scsi_report_bus_reset(ioa_cfg->host,
1651 hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1652 }
1653
1654 error_index = ipr_get_error(ioasc);
1655
1656 if (!ipr_error_table[error_index].log_hcam)
1657 return;
1658
Brian King49dc6a12006-11-21 10:28:35 -06001659 ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* Set indication we have logged an error */
1662 ioa_cfg->errors_logged++;
1663
Brian King933916f2007-03-29 12:43:30 -05001664 if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return;
brking@us.ibm.comcf852032005-11-01 17:00:47 -06001666 if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
1667 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 switch (hostrcb->hcam.overlay_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 case IPR_HOST_RCB_OVERLAY_ID_2:
1671 ipr_log_cache_error(ioa_cfg, hostrcb);
1672 break;
1673 case IPR_HOST_RCB_OVERLAY_ID_3:
1674 ipr_log_config_error(ioa_cfg, hostrcb);
1675 break;
1676 case IPR_HOST_RCB_OVERLAY_ID_4:
1677 case IPR_HOST_RCB_OVERLAY_ID_6:
1678 ipr_log_array_error(ioa_cfg, hostrcb);
1679 break;
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06001680 case IPR_HOST_RCB_OVERLAY_ID_7:
1681 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
1682 break;
brking@us.ibm.comee0f05b2005-11-01 17:02:15 -06001683 case IPR_HOST_RCB_OVERLAY_ID_12:
1684 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
1685 break;
1686 case IPR_HOST_RCB_OVERLAY_ID_13:
1687 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
1688 break;
1689 case IPR_HOST_RCB_OVERLAY_ID_14:
1690 case IPR_HOST_RCB_OVERLAY_ID_16:
1691 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
1692 break;
1693 case IPR_HOST_RCB_OVERLAY_ID_17:
1694 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
1695 break;
Brian King49dc6a12006-11-21 10:28:35 -06001696 case IPR_HOST_RCB_OVERLAY_ID_20:
1697 ipr_log_fabric_error(ioa_cfg, hostrcb);
1698 break;
brking@us.ibm.comcf852032005-11-01 17:00:47 -06001699 case IPR_HOST_RCB_OVERLAY_ID_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 default:
brking@us.ibm.coma9cfca92005-11-01 17:00:41 -06001702 ipr_log_generic_error(ioa_cfg, hostrcb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 break;
1704 }
1705}
1706
1707/**
1708 * ipr_process_error - Op done function for an adapter error log.
1709 * @ipr_cmd: ipr command struct
1710 *
1711 * This function is the op done function for an error log host
1712 * controlled async from the adapter. It will log the error and
1713 * send the HCAM back to the adapter.
1714 *
1715 * Return value:
1716 * none
1717 **/
1718static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1719{
1720 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1721 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1722 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
Brian King65f56472007-04-26 16:00:12 -05001723 u32 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 list_del(&hostrcb->queue);
1726 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1727
1728 if (!ioasc) {
1729 ipr_handle_log_data(ioa_cfg, hostrcb);
Brian King65f56472007-04-26 16:00:12 -05001730 if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
1731 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1733 dev_err(&ioa_cfg->pdev->dev,
1734 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1735 }
1736
1737 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1738}
1739
1740/**
1741 * ipr_timeout - An internally generated op has timed out.
1742 * @ipr_cmd: ipr command struct
1743 *
1744 * This function blocks host requests and initiates an
1745 * adapter reset.
1746 *
1747 * Return value:
1748 * none
1749 **/
1750static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1751{
1752 unsigned long lock_flags = 0;
1753 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1754
1755 ENTER;
1756 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1757
1758 ioa_cfg->errors_logged++;
1759 dev_err(&ioa_cfg->pdev->dev,
1760 "Adapter being reset due to command timeout.\n");
1761
1762 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1763 ioa_cfg->sdt_state = GET_DUMP;
1764
1765 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1766 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1767
1768 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1769 LEAVE;
1770}
1771
1772/**
1773 * ipr_oper_timeout - Adapter timed out transitioning to operational
1774 * @ipr_cmd: ipr command struct
1775 *
1776 * This function blocks host requests and initiates an
1777 * adapter reset.
1778 *
1779 * Return value:
1780 * none
1781 **/
1782static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
1783{
1784 unsigned long lock_flags = 0;
1785 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1786
1787 ENTER;
1788 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1789
1790 ioa_cfg->errors_logged++;
1791 dev_err(&ioa_cfg->pdev->dev,
1792 "Adapter timed out transitioning to operational.\n");
1793
1794 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1795 ioa_cfg->sdt_state = GET_DUMP;
1796
1797 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
1798 if (ipr_fastfail)
1799 ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
1800 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1801 }
1802
1803 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1804 LEAVE;
1805}
1806
1807/**
1808 * ipr_reset_reload - Reset/Reload the IOA
1809 * @ioa_cfg: ioa config struct
1810 * @shutdown_type: shutdown type
1811 *
1812 * This function resets the adapter and re-initializes it.
1813 * This function assumes that all new host commands have been stopped.
1814 * Return value:
1815 * SUCCESS / FAILED
1816 **/
1817static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1818 enum ipr_shutdown_type shutdown_type)
1819{
1820 if (!ioa_cfg->in_reset_reload)
1821 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1822
1823 spin_unlock_irq(ioa_cfg->host->host_lock);
1824 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1825 spin_lock_irq(ioa_cfg->host->host_lock);
1826
1827 /* If we got hit with a host reset while we were already resetting
1828 the adapter for some reason, and the reset failed. */
1829 if (ioa_cfg->ioa_is_dead) {
1830 ipr_trace;
1831 return FAILED;
1832 }
1833
1834 return SUCCESS;
1835}
1836
1837/**
1838 * ipr_find_ses_entry - Find matching SES in SES table
1839 * @res: resource entry struct of SES
1840 *
1841 * Return value:
1842 * pointer to SES table entry / NULL on failure
1843 **/
1844static const struct ipr_ses_table_entry *
1845ipr_find_ses_entry(struct ipr_resource_entry *res)
1846{
1847 int i, j, matches;
1848 const struct ipr_ses_table_entry *ste = ipr_ses_table;
1849
1850 for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1851 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1852 if (ste->compare_product_id_byte[j] == 'X') {
1853 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1854 matches++;
1855 else
1856 break;
1857 } else
1858 matches++;
1859 }
1860
1861 if (matches == IPR_PROD_ID_LEN)
1862 return ste;
1863 }
1864
1865 return NULL;
1866}
1867
1868/**
1869 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1870 * @ioa_cfg: ioa config struct
1871 * @bus: SCSI bus
1872 * @bus_width: bus width
1873 *
1874 * Return value:
1875 * SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1876 * For a 2-byte wide SCSI bus, the maximum transfer speed is
1877 * twice the maximum transfer rate (e.g. for a wide enabled bus,
1878 * max 160MHz = max 320MB/sec).
1879 **/
1880static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1881{
1882 struct ipr_resource_entry *res;
1883 const struct ipr_ses_table_entry *ste;
1884 u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1885
1886 /* Loop through each config table entry in the config table buffer */
1887 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1888 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1889 continue;
1890
1891 if (bus != res->cfgte.res_addr.bus)
1892 continue;
1893
1894 if (!(ste = ipr_find_ses_entry(res)))
1895 continue;
1896
1897 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1898 }
1899
1900 return max_xfer_rate;
1901}
1902
1903/**
1904 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1905 * @ioa_cfg: ioa config struct
1906 * @max_delay: max delay in micro-seconds to wait
1907 *
1908 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1909 *
1910 * Return value:
1911 * 0 on success / other on failure
1912 **/
1913static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1914{
1915 volatile u32 pcii_reg;
1916 int delay = 1;
1917
1918 /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1919 while (delay < max_delay) {
1920 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1921
1922 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1923 return 0;
1924
1925 /* udelay cannot be used if delay is more than a few milliseconds */
1926 if ((delay / 1000) > MAX_UDELAY_MS)
1927 mdelay(delay / 1000);
1928 else
1929 udelay(delay);
1930
1931 delay += delay;
1932 }
1933 return -EIO;
1934}
1935
1936/**
1937 * ipr_get_ldump_data_section - Dump IOA memory
1938 * @ioa_cfg: ioa config struct
1939 * @start_addr: adapter address to dump
1940 * @dest: destination kernel buffer
1941 * @length_in_words: length to dump in 4 byte words
1942 *
1943 * Return value:
1944 * 0 on success / -EIO on failure
1945 **/
1946static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1947 u32 start_addr,
1948 __be32 *dest, u32 length_in_words)
1949{
1950 volatile u32 temp_pcii_reg;
1951 int i, delay = 0;
1952
1953 /* Write IOA interrupt reg starting LDUMP state */
1954 writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1955 ioa_cfg->regs.set_uproc_interrupt_reg);
1956
1957 /* Wait for IO debug acknowledge */
1958 if (ipr_wait_iodbg_ack(ioa_cfg,
1959 IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1960 dev_err(&ioa_cfg->pdev->dev,
1961 "IOA dump long data transfer timeout\n");
1962 return -EIO;
1963 }
1964
1965 /* Signal LDUMP interlocked - clear IO debug ack */
1966 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1967 ioa_cfg->regs.clr_interrupt_reg);
1968
1969 /* Write Mailbox with starting address */
1970 writel(start_addr, ioa_cfg->ioa_mailbox);
1971
1972 /* Signal address valid - clear IOA Reset alert */
1973 writel(IPR_UPROCI_RESET_ALERT,
1974 ioa_cfg->regs.clr_uproc_interrupt_reg);
1975
1976 for (i = 0; i < length_in_words; i++) {
1977 /* Wait for IO debug acknowledge */
1978 if (ipr_wait_iodbg_ack(ioa_cfg,
1979 IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1980 dev_err(&ioa_cfg->pdev->dev,
1981 "IOA dump short data transfer timeout\n");
1982 return -EIO;
1983 }
1984
1985 /* Read data from mailbox and increment destination pointer */
1986 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1987 dest++;
1988
1989 /* For all but the last word of data, signal data received */
1990 if (i < (length_in_words - 1)) {
1991 /* Signal dump data received - Clear IO debug Ack */
1992 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1993 ioa_cfg->regs.clr_interrupt_reg);
1994 }
1995 }
1996
1997 /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1998 writel(IPR_UPROCI_RESET_ALERT,
1999 ioa_cfg->regs.set_uproc_interrupt_reg);
2000
2001 writel(IPR_UPROCI_IO_DEBUG_ALERT,
2002 ioa_cfg->regs.clr_uproc_interrupt_reg);
2003
2004 /* Signal dump data received - Clear IO debug Ack */
2005 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2006 ioa_cfg->regs.clr_interrupt_reg);
2007
2008 /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2009 while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2010 temp_pcii_reg =
2011 readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
2012
2013 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2014 return 0;
2015
2016 udelay(10);
2017 delay += 10;
2018 }
2019
2020 return 0;
2021}
2022
2023#ifdef CONFIG_SCSI_IPR_DUMP
2024/**
2025 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2026 * @ioa_cfg: ioa config struct
2027 * @pci_address: adapter address
2028 * @length: length of data to copy
2029 *
2030 * Copy data from PCI adapter to kernel buffer.
2031 * Note: length MUST be a 4 byte multiple
2032 * Return value:
2033 * 0 on success / other on failure
2034 **/
2035static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2036 unsigned long pci_address, u32 length)
2037{
2038 int bytes_copied = 0;
2039 int cur_len, rc, rem_len, rem_page_len;
2040 __be32 *page;
2041 unsigned long lock_flags = 0;
2042 struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2043
2044 while (bytes_copied < length &&
2045 (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
2046 if (ioa_dump->page_offset >= PAGE_SIZE ||
2047 ioa_dump->page_offset == 0) {
2048 page = (__be32 *)__get_free_page(GFP_ATOMIC);
2049
2050 if (!page) {
2051 ipr_trace;
2052 return bytes_copied;
2053 }
2054
2055 ioa_dump->page_offset = 0;
2056 ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2057 ioa_dump->next_page_index++;
2058 } else
2059 page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2060
2061 rem_len = length - bytes_copied;
2062 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2063 cur_len = min(rem_len, rem_page_len);
2064
2065 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2066 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2067 rc = -EIO;
2068 } else {
2069 rc = ipr_get_ldump_data_section(ioa_cfg,
2070 pci_address + bytes_copied,
2071 &page[ioa_dump->page_offset / 4],
2072 (cur_len / sizeof(u32)));
2073 }
2074 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2075
2076 if (!rc) {
2077 ioa_dump->page_offset += cur_len;
2078 bytes_copied += cur_len;
2079 } else {
2080 ipr_trace;
2081 break;
2082 }
2083 schedule();
2084 }
2085
2086 return bytes_copied;
2087}
2088
2089/**
2090 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2091 * @hdr: dump entry header struct
2092 *
2093 * Return value:
2094 * nothing
2095 **/
2096static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2097{
2098 hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2099 hdr->num_elems = 1;
2100 hdr->offset = sizeof(*hdr);
2101 hdr->status = IPR_DUMP_STATUS_SUCCESS;
2102}
2103
2104/**
2105 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2106 * @ioa_cfg: ioa config struct
2107 * @driver_dump: driver dump struct
2108 *
2109 * Return value:
2110 * nothing
2111 **/
2112static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2113 struct ipr_driver_dump *driver_dump)
2114{
2115 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2116
2117 ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2118 driver_dump->ioa_type_entry.hdr.len =
2119 sizeof(struct ipr_dump_ioa_type_entry) -
2120 sizeof(struct ipr_dump_entry_header);
2121 driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2122 driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2123 driver_dump->ioa_type_entry.type = ioa_cfg->type;
2124 driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2125 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2126 ucode_vpd->minor_release[1];
2127 driver_dump->hdr.num_entries++;
2128}
2129
2130/**
2131 * ipr_dump_version_data - Fill in the driver version in the dump.
2132 * @ioa_cfg: ioa config struct
2133 * @driver_dump: driver dump struct
2134 *
2135 * Return value:
2136 * nothing
2137 **/
2138static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2139 struct ipr_driver_dump *driver_dump)
2140{
2141 ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2142 driver_dump->version_entry.hdr.len =
2143 sizeof(struct ipr_dump_version_entry) -
2144 sizeof(struct ipr_dump_entry_header);
2145 driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2146 driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2147 strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2148 driver_dump->hdr.num_entries++;
2149}
2150
2151/**
2152 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2153 * @ioa_cfg: ioa config struct
2154 * @driver_dump: driver dump struct
2155 *
2156 * Return value:
2157 * nothing
2158 **/
2159static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
2160 struct ipr_driver_dump *driver_dump)
2161{
2162 ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
2163 driver_dump->trace_entry.hdr.len =
2164 sizeof(struct ipr_dump_trace_entry) -
2165 sizeof(struct ipr_dump_entry_header);
2166 driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2167 driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
2168 memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
2169 driver_dump->hdr.num_entries++;
2170}
2171
2172/**
2173 * ipr_dump_location_data - Fill in the IOA location in the dump.
2174 * @ioa_cfg: ioa config struct
2175 * @driver_dump: driver dump struct
2176 *
2177 * Return value:
2178 * nothing
2179 **/
2180static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
2181 struct ipr_driver_dump *driver_dump)
2182{
2183 ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
2184 driver_dump->location_entry.hdr.len =
2185 sizeof(struct ipr_dump_location_entry) -
2186 sizeof(struct ipr_dump_entry_header);
2187 driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2188 driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
Kay Sievers71610f52008-12-03 22:41:36 +01002189 strcpy(driver_dump->location_entry.location, dev_name(&ioa_cfg->pdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 driver_dump->hdr.num_entries++;
2191}
2192
2193/**
2194 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
2195 * @ioa_cfg: ioa config struct
2196 * @dump: dump struct
2197 *
2198 * Return value:
2199 * nothing
2200 **/
2201static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
2202{
2203 unsigned long start_addr, sdt_word;
2204 unsigned long lock_flags = 0;
2205 struct ipr_driver_dump *driver_dump = &dump->driver_dump;
2206 struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
2207 u32 num_entries, start_off, end_off;
2208 u32 bytes_to_copy, bytes_copied, rc;
2209 struct ipr_sdt *sdt;
2210 int i;
2211
2212 ENTER;
2213
2214 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2215
2216 if (ioa_cfg->sdt_state != GET_DUMP) {
2217 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2218 return;
2219 }
2220
2221 start_addr = readl(ioa_cfg->ioa_mailbox);
2222
2223 if (!ipr_sdt_is_fmt2(start_addr)) {
2224 dev_err(&ioa_cfg->pdev->dev,
2225 "Invalid dump table format: %lx\n", start_addr);
2226 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2227 return;
2228 }
2229
2230 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
2231
2232 driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
2233
2234 /* Initialize the overall dump header */
2235 driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
2236 driver_dump->hdr.num_entries = 1;
2237 driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
2238 driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
2239 driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
2240 driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
2241
2242 ipr_dump_version_data(ioa_cfg, driver_dump);
2243 ipr_dump_location_data(ioa_cfg, driver_dump);
2244 ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
2245 ipr_dump_trace_data(ioa_cfg, driver_dump);
2246
2247 /* Update dump_header */
2248 driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
2249
2250 /* IOA Dump entry */
2251 ipr_init_dump_entry_hdr(&ioa_dump->hdr);
2252 ioa_dump->format = IPR_SDT_FMT2;
2253 ioa_dump->hdr.len = 0;
2254 ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2255 ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
2256
2257 /* First entries in sdt are actually a list of dump addresses and
2258 lengths to gather the real dump data. sdt represents the pointer
2259 to the ioa generated dump table. Dump data will be extracted based
2260 on entries in this table */
2261 sdt = &ioa_dump->sdt;
2262
2263 rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
2264 sizeof(struct ipr_sdt) / sizeof(__be32));
2265
2266 /* Smart Dump table is ready to use and the first entry is valid */
2267 if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
2268 dev_err(&ioa_cfg->pdev->dev,
2269 "Dump of IOA failed. Dump table not valid: %d, %X.\n",
2270 rc, be32_to_cpu(sdt->hdr.state));
2271 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
2272 ioa_cfg->sdt_state = DUMP_OBTAINED;
2273 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2274 return;
2275 }
2276
2277 num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
2278
2279 if (num_entries > IPR_NUM_SDT_ENTRIES)
2280 num_entries = IPR_NUM_SDT_ENTRIES;
2281
2282 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2283
2284 for (i = 0; i < num_entries; i++) {
2285 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
2286 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2287 break;
2288 }
2289
2290 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
2291 sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
2292 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
2293 end_off = be32_to_cpu(sdt->entry[i].end_offset);
2294
2295 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
2296 bytes_to_copy = end_off - start_off;
2297 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
2298 sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
2299 continue;
2300 }
2301
2302 /* Copy data from adapter to driver buffers */
2303 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
2304 bytes_to_copy);
2305
2306 ioa_dump->hdr.len += bytes_copied;
2307
2308 if (bytes_copied != bytes_to_copy) {
2309 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
2310 break;
2311 }
2312 }
2313 }
2314 }
2315
2316 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
2317
2318 /* Update dump_header */
2319 driver_dump->hdr.len += ioa_dump->hdr.len;
2320 wmb();
2321 ioa_cfg->sdt_state = DUMP_OBTAINED;
2322 LEAVE;
2323}
2324
2325#else
2326#define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
2327#endif
2328
2329/**
2330 * ipr_release_dump - Free adapter dump memory
2331 * @kref: kref struct
2332 *
2333 * Return value:
2334 * nothing
2335 **/
2336static void ipr_release_dump(struct kref *kref)
2337{
2338 struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
2339 struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
2340 unsigned long lock_flags = 0;
2341 int i;
2342
2343 ENTER;
2344 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2345 ioa_cfg->dump = NULL;
2346 ioa_cfg->sdt_state = INACTIVE;
2347 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2348
2349 for (i = 0; i < dump->ioa_dump.next_page_index; i++)
2350 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
2351
2352 kfree(dump);
2353 LEAVE;
2354}
2355
2356/**
2357 * ipr_worker_thread - Worker thread
David Howellsc4028952006-11-22 14:57:56 +00002358 * @work: ioa config struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 *
2360 * Called at task level from a work thread. This function takes care
2361 * of adding and removing device from the mid-layer as configuration
2362 * changes are detected by the adapter.
2363 *
2364 * Return value:
2365 * nothing
2366 **/
David Howellsc4028952006-11-22 14:57:56 +00002367static void ipr_worker_thread(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 unsigned long lock_flags;
2370 struct ipr_resource_entry *res;
2371 struct scsi_device *sdev;
2372 struct ipr_dump *dump;
David Howellsc4028952006-11-22 14:57:56 +00002373 struct ipr_ioa_cfg *ioa_cfg =
2374 container_of(work, struct ipr_ioa_cfg, work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 u8 bus, target, lun;
2376 int did_work;
2377
2378 ENTER;
2379 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2380
2381 if (ioa_cfg->sdt_state == GET_DUMP) {
2382 dump = ioa_cfg->dump;
2383 if (!dump) {
2384 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2385 return;
2386 }
2387 kref_get(&dump->kref);
2388 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2389 ipr_get_ioa_dump(ioa_cfg, dump);
2390 kref_put(&dump->kref, ipr_release_dump);
2391
2392 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2393 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
2394 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2395 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2396 return;
2397 }
2398
2399restart:
2400 do {
2401 did_work = 0;
2402 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
2403 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2404 return;
2405 }
2406
2407 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2408 if (res->del_from_ml && res->sdev) {
2409 did_work = 1;
2410 sdev = res->sdev;
2411 if (!scsi_device_get(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
2413 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2414 scsi_remove_device(sdev);
2415 scsi_device_put(sdev);
2416 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2417 }
2418 break;
2419 }
2420 }
2421 } while(did_work);
2422
2423 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2424 if (res->add_to_ml) {
2425 bus = res->cfgte.res_addr.bus;
2426 target = res->cfgte.res_addr.target;
2427 lun = res->cfgte.res_addr.lun;
Brian King1121b792006-03-29 09:37:16 -06002428 res->add_to_ml = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2430 scsi_add_device(ioa_cfg->host, bus, target, lun);
2431 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2432 goto restart;
2433 }
2434 }
2435
2436 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
Tony Jonesee959b02008-02-22 00:13:36 +01002437 kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 LEAVE;
2439}
2440
2441#ifdef CONFIG_SCSI_IPR_TRACE
2442/**
2443 * ipr_read_trace - Dump the adapter trace
2444 * @kobj: kobject struct
Zhang Rui91a69022007-06-09 13:57:22 +08002445 * @bin_attr: bin_attribute struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 * @buf: buffer
2447 * @off: offset
2448 * @count: buffer size
2449 *
2450 * Return value:
2451 * number of bytes printed to buffer
2452 **/
Zhang Rui91a69022007-06-09 13:57:22 +08002453static ssize_t ipr_read_trace(struct kobject *kobj,
2454 struct bin_attribute *bin_attr,
2455 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Tony Jonesee959b02008-02-22 00:13:36 +01002457 struct device *dev = container_of(kobj, struct device, kobj);
2458 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2460 unsigned long lock_flags = 0;
Akinobu Mitad777aaf2008-09-22 14:56:47 -07002461 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Akinobu Mitad777aaf2008-09-22 14:56:47 -07002464 ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
2465 IPR_TRACE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
Akinobu Mitad777aaf2008-09-22 14:56:47 -07002467
2468 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
2471static struct bin_attribute ipr_trace_attr = {
2472 .attr = {
2473 .name = "trace",
2474 .mode = S_IRUGO,
2475 },
2476 .size = 0,
2477 .read = ipr_read_trace,
2478};
2479#endif
2480
brking@us.ibm.com62275042005-11-01 17:01:14 -06002481static const struct {
2482 enum ipr_cache_state state;
2483 char *name;
2484} cache_state [] = {
2485 { CACHE_NONE, "none" },
2486 { CACHE_DISABLED, "disabled" },
2487 { CACHE_ENABLED, "enabled" }
2488};
2489
2490/**
2491 * ipr_show_write_caching - Show the write caching attribute
Tony Jonesee959b02008-02-22 00:13:36 +01002492 * @dev: device struct
2493 * @buf: buffer
brking@us.ibm.com62275042005-11-01 17:01:14 -06002494 *
2495 * Return value:
2496 * number of bytes printed to buffer
2497 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002498static ssize_t ipr_show_write_caching(struct device *dev,
2499 struct device_attribute *attr, char *buf)
brking@us.ibm.com62275042005-11-01 17:01:14 -06002500{
Tony Jonesee959b02008-02-22 00:13:36 +01002501 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.com62275042005-11-01 17:01:14 -06002502 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2503 unsigned long lock_flags = 0;
2504 int i, len = 0;
2505
2506 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2507 for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2508 if (cache_state[i].state == ioa_cfg->cache_state) {
2509 len = snprintf(buf, PAGE_SIZE, "%s\n", cache_state[i].name);
2510 break;
2511 }
2512 }
2513 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2514 return len;
2515}
2516
2517
2518/**
2519 * ipr_store_write_caching - Enable/disable adapter write cache
Tony Jonesee959b02008-02-22 00:13:36 +01002520 * @dev: device struct
2521 * @buf: buffer
2522 * @count: buffer size
brking@us.ibm.com62275042005-11-01 17:01:14 -06002523 *
2524 * This function will enable/disable adapter write cache.
2525 *
2526 * Return value:
2527 * count on success / other on failure
2528 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002529static ssize_t ipr_store_write_caching(struct device *dev,
2530 struct device_attribute *attr,
2531 const char *buf, size_t count)
brking@us.ibm.com62275042005-11-01 17:01:14 -06002532{
Tony Jonesee959b02008-02-22 00:13:36 +01002533 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.com62275042005-11-01 17:01:14 -06002534 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2535 unsigned long lock_flags = 0;
2536 enum ipr_cache_state new_state = CACHE_INVALID;
2537 int i;
2538
2539 if (!capable(CAP_SYS_ADMIN))
2540 return -EACCES;
2541 if (ioa_cfg->cache_state == CACHE_NONE)
2542 return -EINVAL;
2543
2544 for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2545 if (!strncmp(cache_state[i].name, buf, strlen(cache_state[i].name))) {
2546 new_state = cache_state[i].state;
2547 break;
2548 }
2549 }
2550
2551 if (new_state != CACHE_DISABLED && new_state != CACHE_ENABLED)
2552 return -EINVAL;
2553
2554 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2555 if (ioa_cfg->cache_state == new_state) {
2556 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2557 return count;
2558 }
2559
2560 ioa_cfg->cache_state = new_state;
2561 dev_info(&ioa_cfg->pdev->dev, "%s adapter write cache.\n",
2562 new_state == CACHE_ENABLED ? "Enabling" : "Disabling");
2563 if (!ioa_cfg->in_reset_reload)
2564 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2565 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2566 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2567
2568 return count;
2569}
2570
Tony Jonesee959b02008-02-22 00:13:36 +01002571static struct device_attribute ipr_ioa_cache_attr = {
brking@us.ibm.com62275042005-11-01 17:01:14 -06002572 .attr = {
2573 .name = "write_cache",
2574 .mode = S_IRUGO | S_IWUSR,
2575 },
2576 .show = ipr_show_write_caching,
2577 .store = ipr_store_write_caching
2578};
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580/**
2581 * ipr_show_fw_version - Show the firmware version
Tony Jonesee959b02008-02-22 00:13:36 +01002582 * @dev: class device struct
2583 * @buf: buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 *
2585 * Return value:
2586 * number of bytes printed to buffer
2587 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002588static ssize_t ipr_show_fw_version(struct device *dev,
2589 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
Tony Jonesee959b02008-02-22 00:13:36 +01002591 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2593 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2594 unsigned long lock_flags = 0;
2595 int len;
2596
2597 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2598 len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
2599 ucode_vpd->major_release, ucode_vpd->card_type,
2600 ucode_vpd->minor_release[0],
2601 ucode_vpd->minor_release[1]);
2602 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2603 return len;
2604}
2605
Tony Jonesee959b02008-02-22 00:13:36 +01002606static struct device_attribute ipr_fw_version_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 .attr = {
2608 .name = "fw_version",
2609 .mode = S_IRUGO,
2610 },
2611 .show = ipr_show_fw_version,
2612};
2613
2614/**
2615 * ipr_show_log_level - Show the adapter's error logging level
Tony Jonesee959b02008-02-22 00:13:36 +01002616 * @dev: class device struct
2617 * @buf: buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 *
2619 * Return value:
2620 * number of bytes printed to buffer
2621 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002622static ssize_t ipr_show_log_level(struct device *dev,
2623 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
Tony Jonesee959b02008-02-22 00:13:36 +01002625 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2627 unsigned long lock_flags = 0;
2628 int len;
2629
2630 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2631 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
2632 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2633 return len;
2634}
2635
2636/**
2637 * ipr_store_log_level - Change the adapter's error logging level
Tony Jonesee959b02008-02-22 00:13:36 +01002638 * @dev: class device struct
2639 * @buf: buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 *
2641 * Return value:
2642 * number of bytes printed to buffer
2643 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002644static ssize_t ipr_store_log_level(struct device *dev,
2645 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 const char *buf, size_t count)
2647{
Tony Jonesee959b02008-02-22 00:13:36 +01002648 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2650 unsigned long lock_flags = 0;
2651
2652 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2653 ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
2654 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2655 return strlen(buf);
2656}
2657
Tony Jonesee959b02008-02-22 00:13:36 +01002658static struct device_attribute ipr_log_level_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 .attr = {
2660 .name = "log_level",
2661 .mode = S_IRUGO | S_IWUSR,
2662 },
2663 .show = ipr_show_log_level,
2664 .store = ipr_store_log_level
2665};
2666
2667/**
2668 * ipr_store_diagnostics - IOA Diagnostics interface
Tony Jonesee959b02008-02-22 00:13:36 +01002669 * @dev: device struct
2670 * @buf: buffer
2671 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 *
2673 * This function will reset the adapter and wait a reasonable
2674 * amount of time for any errors that the adapter might log.
2675 *
2676 * Return value:
2677 * count on success / other on failure
2678 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002679static ssize_t ipr_store_diagnostics(struct device *dev,
2680 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 const char *buf, size_t count)
2682{
Tony Jonesee959b02008-02-22 00:13:36 +01002683 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2685 unsigned long lock_flags = 0;
2686 int rc = count;
2687
2688 if (!capable(CAP_SYS_ADMIN))
2689 return -EACCES;
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05002692 while(ioa_cfg->in_reset_reload) {
2693 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2694 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2695 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2696 }
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 ioa_cfg->errors_logged = 0;
2699 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2700
2701 if (ioa_cfg->in_reset_reload) {
2702 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2703 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2704
2705 /* Wait for a second for any errors to be logged */
2706 msleep(1000);
2707 } else {
2708 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2709 return -EIO;
2710 }
2711
2712 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2713 if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2714 rc = -EIO;
2715 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2716
2717 return rc;
2718}
2719
Tony Jonesee959b02008-02-22 00:13:36 +01002720static struct device_attribute ipr_diagnostics_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 .attr = {
2722 .name = "run_diagnostics",
2723 .mode = S_IWUSR,
2724 },
2725 .store = ipr_store_diagnostics
2726};
2727
2728/**
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002729 * ipr_show_adapter_state - Show the adapter's state
Tony Jonesee959b02008-02-22 00:13:36 +01002730 * @class_dev: device struct
2731 * @buf: buffer
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002732 *
2733 * Return value:
2734 * number of bytes printed to buffer
2735 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002736static ssize_t ipr_show_adapter_state(struct device *dev,
2737 struct device_attribute *attr, char *buf)
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002738{
Tony Jonesee959b02008-02-22 00:13:36 +01002739 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002740 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2741 unsigned long lock_flags = 0;
2742 int len;
2743
2744 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2745 if (ioa_cfg->ioa_is_dead)
2746 len = snprintf(buf, PAGE_SIZE, "offline\n");
2747 else
2748 len = snprintf(buf, PAGE_SIZE, "online\n");
2749 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2750 return len;
2751}
2752
2753/**
2754 * ipr_store_adapter_state - Change adapter state
Tony Jonesee959b02008-02-22 00:13:36 +01002755 * @dev: device struct
2756 * @buf: buffer
2757 * @count: buffer size
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002758 *
2759 * This function will change the adapter's state.
2760 *
2761 * Return value:
2762 * count on success / other on failure
2763 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002764static ssize_t ipr_store_adapter_state(struct device *dev,
2765 struct device_attribute *attr,
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002766 const char *buf, size_t count)
2767{
Tony Jonesee959b02008-02-22 00:13:36 +01002768 struct Scsi_Host *shost = class_to_shost(dev);
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002769 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2770 unsigned long lock_flags;
2771 int result = count;
2772
2773 if (!capable(CAP_SYS_ADMIN))
2774 return -EACCES;
2775
2776 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2777 if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
2778 ioa_cfg->ioa_is_dead = 0;
2779 ioa_cfg->reset_retries = 0;
2780 ioa_cfg->in_ioa_bringdown = 0;
2781 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2782 }
2783 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2784 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2785
2786 return result;
2787}
2788
Tony Jonesee959b02008-02-22 00:13:36 +01002789static struct device_attribute ipr_ioa_state_attr = {
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002790 .attr = {
Brian King49dd0962008-04-28 17:36:20 -05002791 .name = "online_state",
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06002792 .mode = S_IRUGO | S_IWUSR,
2793 },
2794 .show = ipr_show_adapter_state,
2795 .store = ipr_store_adapter_state
2796};
2797
2798/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 * ipr_store_reset_adapter - Reset the adapter
Tony Jonesee959b02008-02-22 00:13:36 +01002800 * @dev: device struct
2801 * @buf: buffer
2802 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 *
2804 * This function will reset the adapter.
2805 *
2806 * Return value:
2807 * count on success / other on failure
2808 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002809static ssize_t ipr_store_reset_adapter(struct device *dev,
2810 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 const char *buf, size_t count)
2812{
Tony Jonesee959b02008-02-22 00:13:36 +01002813 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2815 unsigned long lock_flags;
2816 int result = count;
2817
2818 if (!capable(CAP_SYS_ADMIN))
2819 return -EACCES;
2820
2821 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2822 if (!ioa_cfg->in_reset_reload)
2823 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2824 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2825 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2826
2827 return result;
2828}
2829
Tony Jonesee959b02008-02-22 00:13:36 +01002830static struct device_attribute ipr_ioa_reset_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 .attr = {
2832 .name = "reset_host",
2833 .mode = S_IWUSR,
2834 },
2835 .store = ipr_store_reset_adapter
2836};
2837
2838/**
2839 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2840 * @buf_len: buffer length
2841 *
2842 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2843 * list to use for microcode download
2844 *
2845 * Return value:
2846 * pointer to sglist / NULL on failure
2847 **/
2848static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2849{
2850 int sg_size, order, bsize_elem, num_elem, i, j;
2851 struct ipr_sglist *sglist;
2852 struct scatterlist *scatterlist;
2853 struct page *page;
2854
2855 /* Get the minimum size per scatter/gather element */
2856 sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2857
2858 /* Get the actual size per element */
2859 order = get_order(sg_size);
2860
2861 /* Determine the actual number of bytes per element */
2862 bsize_elem = PAGE_SIZE * (1 << order);
2863
2864 /* Determine the actual number of sg entries needed */
2865 if (buf_len % bsize_elem)
2866 num_elem = (buf_len / bsize_elem) + 1;
2867 else
2868 num_elem = buf_len / bsize_elem;
2869
2870 /* Allocate a scatter/gather list for the DMA */
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06002871 sglist = kzalloc(sizeof(struct ipr_sglist) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 (sizeof(struct scatterlist) * (num_elem - 1)),
2873 GFP_KERNEL);
2874
2875 if (sglist == NULL) {
2876 ipr_trace;
2877 return NULL;
2878 }
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 scatterlist = sglist->scatterlist;
Jens Axboe45711f12007-10-22 21:19:53 +02002881 sg_init_table(scatterlist, num_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 sglist->order = order;
2884 sglist->num_sg = num_elem;
2885
2886 /* Allocate a bunch of sg elements */
2887 for (i = 0; i < num_elem; i++) {
2888 page = alloc_pages(GFP_KERNEL, order);
2889 if (!page) {
2890 ipr_trace;
2891
2892 /* Free up what we already allocated */
2893 for (j = i - 1; j >= 0; j--)
Jens Axboe45711f12007-10-22 21:19:53 +02002894 __free_pages(sg_page(&scatterlist[j]), order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 kfree(sglist);
2896 return NULL;
2897 }
2898
Jens Axboe642f1492007-10-24 11:20:47 +02002899 sg_set_page(&scatterlist[i], page, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
2901
2902 return sglist;
2903}
2904
2905/**
2906 * ipr_free_ucode_buffer - Frees a microcode download buffer
2907 * @p_dnld: scatter/gather list pointer
2908 *
2909 * Free a DMA'able ucode download buffer previously allocated with
2910 * ipr_alloc_ucode_buffer
2911 *
2912 * Return value:
2913 * nothing
2914 **/
2915static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2916{
2917 int i;
2918
2919 for (i = 0; i < sglist->num_sg; i++)
Jens Axboe45711f12007-10-22 21:19:53 +02002920 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922 kfree(sglist);
2923}
2924
2925/**
2926 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2927 * @sglist: scatter/gather list pointer
2928 * @buffer: buffer pointer
2929 * @len: buffer length
2930 *
2931 * Copy a microcode image from a user buffer into a buffer allocated by
2932 * ipr_alloc_ucode_buffer
2933 *
2934 * Return value:
2935 * 0 on success / other on failure
2936 **/
2937static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2938 u8 *buffer, u32 len)
2939{
2940 int bsize_elem, i, result = 0;
2941 struct scatterlist *scatterlist;
2942 void *kaddr;
2943
2944 /* Determine the actual number of bytes per element */
2945 bsize_elem = PAGE_SIZE * (1 << sglist->order);
2946
2947 scatterlist = sglist->scatterlist;
2948
2949 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
Jens Axboe45711f12007-10-22 21:19:53 +02002950 struct page *page = sg_page(&scatterlist[i]);
2951
2952 kaddr = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 memcpy(kaddr, buffer, bsize_elem);
Jens Axboe45711f12007-10-22 21:19:53 +02002954 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 scatterlist[i].length = bsize_elem;
2957
2958 if (result != 0) {
2959 ipr_trace;
2960 return result;
2961 }
2962 }
2963
2964 if (len % bsize_elem) {
Jens Axboe45711f12007-10-22 21:19:53 +02002965 struct page *page = sg_page(&scatterlist[i]);
2966
2967 kaddr = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 memcpy(kaddr, buffer, len % bsize_elem);
Jens Axboe45711f12007-10-22 21:19:53 +02002969 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 scatterlist[i].length = len % bsize_elem;
2972 }
2973
2974 sglist->buffer_len = len;
2975 return result;
2976}
2977
2978/**
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002979 * ipr_build_ucode_ioadl - Build a microcode download IOADL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 * @ipr_cmd: ipr command struct
2981 * @sglist: scatter/gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 *
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002983 * Builds a microcode download IOA data list (IOADL).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 **/
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002986static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
2987 struct ipr_sglist *sglist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2990 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2991 struct scatterlist *scatterlist = sglist->scatterlist;
2992 int i;
2993
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002994 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
brking@us.ibm.com12baa422005-11-01 17:01:27 -06002996 ioarcb->write_data_transfer_length = cpu_to_be32(sglist->buffer_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 ioarcb->write_ioadl_len =
2998 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
2999
3000 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3001 ioadl[i].flags_and_data_len =
3002 cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3003 ioadl[i].address =
3004 cpu_to_be32(sg_dma_address(&scatterlist[i]));
3005 }
3006
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003007 ioadl[i-1].flags_and_data_len |=
3008 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3009}
3010
3011/**
3012 * ipr_update_ioa_ucode - Update IOA's microcode
3013 * @ioa_cfg: ioa config struct
3014 * @sglist: scatter/gather list
3015 *
3016 * Initiate an adapter reset to update the IOA's microcode
3017 *
3018 * Return value:
3019 * 0 on success / -EIO on failure
3020 **/
3021static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3022 struct ipr_sglist *sglist)
3023{
3024 unsigned long lock_flags;
3025
3026 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05003027 while(ioa_cfg->in_reset_reload) {
3028 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3029 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3030 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3031 }
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003032
3033 if (ioa_cfg->ucode_sglist) {
3034 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3035 dev_err(&ioa_cfg->pdev->dev,
3036 "Microcode download already in progress\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return -EIO;
3038 }
3039
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003040 sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
3041 sglist->num_sg, DMA_TO_DEVICE);
3042
3043 if (!sglist->num_dma_sg) {
3044 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3045 dev_err(&ioa_cfg->pdev->dev,
3046 "Failed to map microcode download buffer!\n");
3047 return -EIO;
3048 }
3049
3050 ioa_cfg->ucode_sglist = sglist;
3051 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3052 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3053 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3054
3055 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3056 ioa_cfg->ucode_sglist = NULL;
3057 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return 0;
3059}
3060
3061/**
3062 * ipr_store_update_fw - Update the firmware on the adapter
Tony Jonesee959b02008-02-22 00:13:36 +01003063 * @class_dev: device struct
3064 * @buf: buffer
3065 * @count: buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 *
3067 * This function will update the firmware on the adapter.
3068 *
3069 * Return value:
3070 * count on success / other on failure
3071 **/
Tony Jonesee959b02008-02-22 00:13:36 +01003072static ssize_t ipr_store_update_fw(struct device *dev,
3073 struct device_attribute *attr,
3074 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Tony Jonesee959b02008-02-22 00:13:36 +01003076 struct Scsi_Host *shost = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3078 struct ipr_ucode_image_header *image_hdr;
3079 const struct firmware *fw_entry;
3080 struct ipr_sglist *sglist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 char fname[100];
3082 char *src;
3083 int len, result, dnld_size;
3084
3085 if (!capable(CAP_SYS_ADMIN))
3086 return -EACCES;
3087
3088 len = snprintf(fname, 99, "%s", buf);
3089 fname[len-1] = '\0';
3090
3091 if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3092 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3093 return -EIO;
3094 }
3095
3096 image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3097
3098 if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
3099 (ioa_cfg->vpd_cbs->page3_data.card_type &&
3100 ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
3101 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
3102 release_firmware(fw_entry);
3103 return -EINVAL;
3104 }
3105
3106 src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3107 dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3108 sglist = ipr_alloc_ucode_buffer(dnld_size);
3109
3110 if (!sglist) {
3111 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3112 release_firmware(fw_entry);
3113 return -ENOMEM;
3114 }
3115
3116 result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3117
3118 if (result) {
3119 dev_err(&ioa_cfg->pdev->dev,
3120 "Microcode buffer copy to DMA buffer failed\n");
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003121 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
3123
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003124 result = ipr_update_ioa_ucode(ioa_cfg, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003126 if (!result)
3127 result = count;
3128out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 ipr_free_ucode_buffer(sglist);
3130 release_firmware(fw_entry);
brking@us.ibm.com12baa422005-11-01 17:01:27 -06003131 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132}
3133
Tony Jonesee959b02008-02-22 00:13:36 +01003134static struct device_attribute ipr_update_fw_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 .attr = {
3136 .name = "update_fw",
3137 .mode = S_IWUSR,
3138 },
3139 .store = ipr_store_update_fw
3140};
3141
Tony Jonesee959b02008-02-22 00:13:36 +01003142static struct device_attribute *ipr_ioa_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 &ipr_fw_version_attr,
3144 &ipr_log_level_attr,
3145 &ipr_diagnostics_attr,
brking@us.ibm.comf37eb542005-11-01 17:01:40 -06003146 &ipr_ioa_state_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 &ipr_ioa_reset_attr,
3148 &ipr_update_fw_attr,
brking@us.ibm.com62275042005-11-01 17:01:14 -06003149 &ipr_ioa_cache_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 NULL,
3151};
3152
3153#ifdef CONFIG_SCSI_IPR_DUMP
3154/**
3155 * ipr_read_dump - Dump the adapter
3156 * @kobj: kobject struct
Zhang Rui91a69022007-06-09 13:57:22 +08003157 * @bin_attr: bin_attribute struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 * @buf: buffer
3159 * @off: offset
3160 * @count: buffer size
3161 *
3162 * Return value:
3163 * number of bytes printed to buffer
3164 **/
Zhang Rui91a69022007-06-09 13:57:22 +08003165static ssize_t ipr_read_dump(struct kobject *kobj,
3166 struct bin_attribute *bin_attr,
3167 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
Tony Jonesee959b02008-02-22 00:13:36 +01003169 struct device *cdev = container_of(kobj, struct device, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 struct Scsi_Host *shost = class_to_shost(cdev);
3171 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3172 struct ipr_dump *dump;
3173 unsigned long lock_flags = 0;
3174 char *src;
3175 int len;
3176 size_t rc = count;
3177
3178 if (!capable(CAP_SYS_ADMIN))
3179 return -EACCES;
3180
3181 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3182 dump = ioa_cfg->dump;
3183
3184 if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
3185 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3186 return 0;
3187 }
3188 kref_get(&dump->kref);
3189 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3190
3191 if (off > dump->driver_dump.hdr.len) {
3192 kref_put(&dump->kref, ipr_release_dump);
3193 return 0;
3194 }
3195
3196 if (off + count > dump->driver_dump.hdr.len) {
3197 count = dump->driver_dump.hdr.len - off;
3198 rc = count;
3199 }
3200
3201 if (count && off < sizeof(dump->driver_dump)) {
3202 if (off + count > sizeof(dump->driver_dump))
3203 len = sizeof(dump->driver_dump) - off;
3204 else
3205 len = count;
3206 src = (u8 *)&dump->driver_dump + off;
3207 memcpy(buf, src, len);
3208 buf += len;
3209 off += len;
3210 count -= len;
3211 }
3212
3213 off -= sizeof(dump->driver_dump);
3214
3215 if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
3216 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
3217 len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
3218 else
3219 len = count;
3220 src = (u8 *)&dump->ioa_dump + off;
3221 memcpy(buf, src, len);
3222 buf += len;
3223 off += len;
3224 count -= len;
3225 }
3226
3227 off -= offsetof(struct ipr_ioa_dump, ioa_data);
3228
3229 while (count) {
3230 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
3231 len = PAGE_ALIGN(off) - off;
3232 else
3233 len = count;
3234 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
3235 src += off & ~PAGE_MASK;
3236 memcpy(buf, src, len);
3237 buf += len;
3238 off += len;
3239 count -= len;
3240 }
3241
3242 kref_put(&dump->kref, ipr_release_dump);
3243 return rc;
3244}
3245
3246/**
3247 * ipr_alloc_dump - Prepare for adapter dump
3248 * @ioa_cfg: ioa config struct
3249 *
3250 * Return value:
3251 * 0 on success / other on failure
3252 **/
3253static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
3254{
3255 struct ipr_dump *dump;
3256 unsigned long lock_flags = 0;
3257
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06003258 dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
3260 if (!dump) {
3261 ipr_err("Dump memory allocation failed\n");
3262 return -ENOMEM;
3263 }
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 kref_init(&dump->kref);
3266 dump->ioa_cfg = ioa_cfg;
3267
3268 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3269
3270 if (INACTIVE != ioa_cfg->sdt_state) {
3271 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3272 kfree(dump);
3273 return 0;
3274 }
3275
3276 ioa_cfg->dump = dump;
3277 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
3278 if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
3279 ioa_cfg->dump_taken = 1;
3280 schedule_work(&ioa_cfg->work_q);
3281 }
3282 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 return 0;
3285}
3286
3287/**
3288 * ipr_free_dump - Free adapter dump memory
3289 * @ioa_cfg: ioa config struct
3290 *
3291 * Return value:
3292 * 0 on success / other on failure
3293 **/
3294static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
3295{
3296 struct ipr_dump *dump;
3297 unsigned long lock_flags = 0;
3298
3299 ENTER;
3300
3301 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3302 dump = ioa_cfg->dump;
3303 if (!dump) {
3304 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3305 return 0;
3306 }
3307
3308 ioa_cfg->dump = NULL;
3309 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3310
3311 kref_put(&dump->kref, ipr_release_dump);
3312
3313 LEAVE;
3314 return 0;
3315}
3316
3317/**
3318 * ipr_write_dump - Setup dump state of adapter
3319 * @kobj: kobject struct
Zhang Rui91a69022007-06-09 13:57:22 +08003320 * @bin_attr: bin_attribute struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 * @buf: buffer
3322 * @off: offset
3323 * @count: buffer size
3324 *
3325 * Return value:
3326 * number of bytes printed to buffer
3327 **/
Zhang Rui91a69022007-06-09 13:57:22 +08003328static ssize_t ipr_write_dump(struct kobject *kobj,
3329 struct bin_attribute *bin_attr,
3330 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Tony Jonesee959b02008-02-22 00:13:36 +01003332 struct device *cdev = container_of(kobj, struct device, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 struct Scsi_Host *shost = class_to_shost(cdev);
3334 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3335 int rc;
3336
3337 if (!capable(CAP_SYS_ADMIN))
3338 return -EACCES;
3339
3340 if (buf[0] == '1')
3341 rc = ipr_alloc_dump(ioa_cfg);
3342 else if (buf[0] == '0')
3343 rc = ipr_free_dump(ioa_cfg);
3344 else
3345 return -EINVAL;
3346
3347 if (rc)
3348 return rc;
3349 else
3350 return count;
3351}
3352
3353static struct bin_attribute ipr_dump_attr = {
3354 .attr = {
3355 .name = "dump",
3356 .mode = S_IRUSR | S_IWUSR,
3357 },
3358 .size = 0,
3359 .read = ipr_read_dump,
3360 .write = ipr_write_dump
3361};
3362#else
3363static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
3364#endif
3365
3366/**
3367 * ipr_change_queue_depth - Change the device's queue depth
3368 * @sdev: scsi device struct
3369 * @qdepth: depth to set
Mike Christiee881a172009-10-15 17:46:39 -07003370 * @reason: calling context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 *
3372 * Return value:
3373 * actual depth set
3374 **/
Mike Christiee881a172009-10-15 17:46:39 -07003375static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth,
3376 int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
Brian King35a39692006-09-25 12:39:20 -05003378 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3379 struct ipr_resource_entry *res;
3380 unsigned long lock_flags = 0;
3381
Mike Christiee881a172009-10-15 17:46:39 -07003382 if (reason != SCSI_QDEPTH_DEFAULT)
3383 return -EOPNOTSUPP;
3384
Brian King35a39692006-09-25 12:39:20 -05003385 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3386 res = (struct ipr_resource_entry *)sdev->hostdata;
3387
3388 if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
3389 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
3390 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3393 return sdev->queue_depth;
3394}
3395
3396/**
3397 * ipr_change_queue_type - Change the device's queue type
3398 * @dsev: scsi device struct
3399 * @tag_type: type of tags to use
3400 *
3401 * Return value:
3402 * actual queue type set
3403 **/
3404static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
3405{
3406 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3407 struct ipr_resource_entry *res;
3408 unsigned long lock_flags = 0;
3409
3410 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3411 res = (struct ipr_resource_entry *)sdev->hostdata;
3412
3413 if (res) {
3414 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
3415 /*
3416 * We don't bother quiescing the device here since the
3417 * adapter firmware does it for us.
3418 */
3419 scsi_set_tag_type(sdev, tag_type);
3420
3421 if (tag_type)
3422 scsi_activate_tcq(sdev, sdev->queue_depth);
3423 else
3424 scsi_deactivate_tcq(sdev, sdev->queue_depth);
3425 } else
3426 tag_type = 0;
3427 } else
3428 tag_type = 0;
3429
3430 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3431 return tag_type;
3432}
3433
3434/**
3435 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
3436 * @dev: device struct
3437 * @buf: buffer
3438 *
3439 * Return value:
3440 * number of bytes printed to buffer
3441 **/
Yani Ioannou10523b32005-05-17 06:43:37 -04003442static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
3444 struct scsi_device *sdev = to_scsi_device(dev);
3445 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
3446 struct ipr_resource_entry *res;
3447 unsigned long lock_flags = 0;
3448 ssize_t len = -ENXIO;
3449
3450 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3451 res = (struct ipr_resource_entry *)sdev->hostdata;
3452 if (res)
3453 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->cfgte.res_handle);
3454 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3455 return len;
3456}
3457
3458static struct device_attribute ipr_adapter_handle_attr = {
3459 .attr = {
3460 .name = "adapter_handle",
3461 .mode = S_IRUSR,
3462 },
3463 .show = ipr_show_adapter_handle
3464};
3465
3466static struct device_attribute *ipr_dev_attrs[] = {
3467 &ipr_adapter_handle_attr,
3468 NULL,
3469};
3470
3471/**
3472 * ipr_biosparam - Return the HSC mapping
3473 * @sdev: scsi device struct
3474 * @block_device: block device pointer
3475 * @capacity: capacity of the device
3476 * @parm: Array containing returned HSC values.
3477 *
3478 * This function generates the HSC parms that fdisk uses.
3479 * We want to make sure we return something that places partitions
3480 * on 4k boundaries for best performance with the IOA.
3481 *
3482 * Return value:
3483 * 0 on success
3484 **/
3485static int ipr_biosparam(struct scsi_device *sdev,
3486 struct block_device *block_device,
3487 sector_t capacity, int *parm)
3488{
3489 int heads, sectors;
3490 sector_t cylinders;
3491
3492 heads = 128;
3493 sectors = 32;
3494
3495 cylinders = capacity;
3496 sector_div(cylinders, (128 * 32));
3497
3498 /* return result */
3499 parm[0] = heads;
3500 parm[1] = sectors;
3501 parm[2] = cylinders;
3502
3503 return 0;
3504}
3505
3506/**
Brian King35a39692006-09-25 12:39:20 -05003507 * ipr_find_starget - Find target based on bus/target.
3508 * @starget: scsi target struct
3509 *
3510 * Return value:
3511 * resource entry pointer if found / NULL if not found
3512 **/
3513static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
3514{
3515 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3516 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3517 struct ipr_resource_entry *res;
3518
3519 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3520 if ((res->cfgte.res_addr.bus == starget->channel) &&
3521 (res->cfgte.res_addr.target == starget->id) &&
3522 (res->cfgte.res_addr.lun == 0)) {
3523 return res;
3524 }
3525 }
3526
3527 return NULL;
3528}
3529
3530static struct ata_port_info sata_port_info;
3531
3532/**
3533 * ipr_target_alloc - Prepare for commands to a SCSI target
3534 * @starget: scsi target struct
3535 *
3536 * If the device is a SATA device, this function allocates an
3537 * ATA port with libata, else it does nothing.
3538 *
3539 * Return value:
3540 * 0 on success / non-0 on failure
3541 **/
3542static int ipr_target_alloc(struct scsi_target *starget)
3543{
3544 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3545 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
3546 struct ipr_sata_port *sata_port;
3547 struct ata_port *ap;
3548 struct ipr_resource_entry *res;
3549 unsigned long lock_flags;
3550
3551 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3552 res = ipr_find_starget(starget);
3553 starget->hostdata = NULL;
3554
3555 if (res && ipr_is_gata(res)) {
3556 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3557 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
3558 if (!sata_port)
3559 return -ENOMEM;
3560
3561 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
3562 if (ap) {
3563 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3564 sata_port->ioa_cfg = ioa_cfg;
3565 sata_port->ap = ap;
3566 sata_port->res = res;
3567
3568 res->sata_port = sata_port;
3569 ap->private_data = sata_port;
3570 starget->hostdata = sata_port;
3571 } else {
3572 kfree(sata_port);
3573 return -ENOMEM;
3574 }
3575 }
3576 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3577
3578 return 0;
3579}
3580
3581/**
3582 * ipr_target_destroy - Destroy a SCSI target
3583 * @starget: scsi target struct
3584 *
3585 * If the device was a SATA device, this function frees the libata
3586 * ATA port, else it does nothing.
3587 *
3588 **/
3589static void ipr_target_destroy(struct scsi_target *starget)
3590{
3591 struct ipr_sata_port *sata_port = starget->hostdata;
3592
3593 if (sata_port) {
3594 starget->hostdata = NULL;
3595 ata_sas_port_destroy(sata_port->ap);
3596 kfree(sata_port);
3597 }
3598}
3599
3600/**
3601 * ipr_find_sdev - Find device based on bus/target/lun.
3602 * @sdev: scsi device struct
3603 *
3604 * Return value:
3605 * resource entry pointer if found / NULL if not found
3606 **/
3607static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
3608{
3609 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3610 struct ipr_resource_entry *res;
3611
3612 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3613 if ((res->cfgte.res_addr.bus == sdev->channel) &&
3614 (res->cfgte.res_addr.target == sdev->id) &&
3615 (res->cfgte.res_addr.lun == sdev->lun))
3616 return res;
3617 }
3618
3619 return NULL;
3620}
3621
3622/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 * ipr_slave_destroy - Unconfigure a SCSI device
3624 * @sdev: scsi device struct
3625 *
3626 * Return value:
3627 * nothing
3628 **/
3629static void ipr_slave_destroy(struct scsi_device *sdev)
3630{
3631 struct ipr_resource_entry *res;
3632 struct ipr_ioa_cfg *ioa_cfg;
3633 unsigned long lock_flags = 0;
3634
3635 ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3636
3637 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3638 res = (struct ipr_resource_entry *) sdev->hostdata;
3639 if (res) {
Brian King35a39692006-09-25 12:39:20 -05003640 if (res->sata_port)
3641 ata_port_disable(res->sata_port->ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 sdev->hostdata = NULL;
3643 res->sdev = NULL;
Brian King35a39692006-09-25 12:39:20 -05003644 res->sata_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 }
3646 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3647}
3648
3649/**
3650 * ipr_slave_configure - Configure a SCSI device
3651 * @sdev: scsi device struct
3652 *
3653 * This function configures the specified scsi device.
3654 *
3655 * Return value:
3656 * 0 on success
3657 **/
3658static int ipr_slave_configure(struct scsi_device *sdev)
3659{
3660 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3661 struct ipr_resource_entry *res;
Brian Kingdd406ef2009-04-22 08:58:02 -05003662 struct ata_port *ap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 unsigned long lock_flags = 0;
3664
3665 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3666 res = sdev->hostdata;
3667 if (res) {
3668 if (ipr_is_af_dasd_device(res))
3669 sdev->type = TYPE_RAID;
brking@us.ibm.com0726ce22005-11-01 17:01:01 -06003670 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 sdev->scsi_level = 4;
brking@us.ibm.com0726ce22005-11-01 17:01:01 -06003672 sdev->no_uld_attach = 1;
3673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 if (ipr_is_vset_device(res)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07003675 blk_queue_rq_timeout(sdev->request_queue,
3676 IPR_VSET_RW_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 blk_queue_max_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
3678 }
Brian Kinge4fbf442006-03-29 09:37:22 -06003679 if (ipr_is_vset_device(res) || ipr_is_scsi_disk(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 sdev->allow_restart = 1;
Brian Kingdd406ef2009-04-22 08:58:02 -05003681 if (ipr_is_gata(res) && res->sata_port)
3682 ap = res->sata_port->ap;
3683 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3684
3685 if (ap) {
Brian King35a39692006-09-25 12:39:20 -05003686 scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
Brian Kingdd406ef2009-04-22 08:58:02 -05003687 ata_sas_slave_configure(sdev, ap);
3688 } else
Brian King35a39692006-09-25 12:39:20 -05003689 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
Brian Kingdd406ef2009-04-22 08:58:02 -05003690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 }
3692 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3693 return 0;
3694}
3695
3696/**
Brian King35a39692006-09-25 12:39:20 -05003697 * ipr_ata_slave_alloc - Prepare for commands to a SATA device
3698 * @sdev: scsi device struct
3699 *
3700 * This function initializes an ATA port so that future commands
3701 * sent through queuecommand will work.
3702 *
3703 * Return value:
3704 * 0 on success
3705 **/
3706static int ipr_ata_slave_alloc(struct scsi_device *sdev)
3707{
3708 struct ipr_sata_port *sata_port = NULL;
3709 int rc = -ENXIO;
3710
3711 ENTER;
3712 if (sdev->sdev_target)
3713 sata_port = sdev->sdev_target->hostdata;
3714 if (sata_port)
3715 rc = ata_sas_port_init(sata_port->ap);
3716 if (rc)
3717 ipr_slave_destroy(sdev);
3718
3719 LEAVE;
3720 return rc;
3721}
3722
3723/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 * ipr_slave_alloc - Prepare for commands to a device.
3725 * @sdev: scsi device struct
3726 *
3727 * This function saves a pointer to the resource entry
3728 * in the scsi device struct if the device exists. We
3729 * can then use this pointer in ipr_queuecommand when
3730 * handling new commands.
3731 *
3732 * Return value:
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06003733 * 0 on success / -ENXIO if device does not exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 **/
3735static int ipr_slave_alloc(struct scsi_device *sdev)
3736{
3737 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3738 struct ipr_resource_entry *res;
3739 unsigned long lock_flags;
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06003740 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
3742 sdev->hostdata = NULL;
3743
3744 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3745
Brian King35a39692006-09-25 12:39:20 -05003746 res = ipr_find_sdev(sdev);
3747 if (res) {
3748 res->sdev = sdev;
3749 res->add_to_ml = 0;
3750 res->in_erp = 0;
3751 sdev->hostdata = res;
3752 if (!ipr_is_naca_model(res))
3753 res->needs_sync_complete = 1;
3754 rc = 0;
3755 if (ipr_is_gata(res)) {
3756 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3757 return ipr_ata_slave_alloc(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 }
3759 }
3760
3761 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3762
brking@us.ibm.com692aebf2005-11-01 17:01:07 -06003763 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765
3766/**
3767 * ipr_eh_host_reset - Reset the host adapter
3768 * @scsi_cmd: scsi command struct
3769 *
3770 * Return value:
3771 * SUCCESS / FAILED
3772 **/
Jeff Garzik df0ae242005-05-28 07:57:14 -04003773static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
3775 struct ipr_ioa_cfg *ioa_cfg;
3776 int rc;
3777
3778 ENTER;
3779 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3780
3781 dev_err(&ioa_cfg->pdev->dev,
3782 "Adapter being reset as a result of error recovery.\n");
3783
3784 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3785 ioa_cfg->sdt_state = GET_DUMP;
3786
3787 rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
3788
3789 LEAVE;
3790 return rc;
3791}
3792
Jeff Garzik df0ae242005-05-28 07:57:14 -04003793static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
3794{
3795 int rc;
3796
3797 spin_lock_irq(cmd->device->host->host_lock);
3798 rc = __ipr_eh_host_reset(cmd);
3799 spin_unlock_irq(cmd->device->host->host_lock);
3800
3801 return rc;
3802}
3803
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804/**
Brian Kingc6513092006-03-29 09:37:43 -06003805 * ipr_device_reset - Reset the device
3806 * @ioa_cfg: ioa config struct
3807 * @res: resource entry struct
3808 *
3809 * This function issues a device reset to the affected device.
3810 * If the device is a SCSI device, a LUN reset will be sent
3811 * to the device first. If that does not work, a target reset
Brian King35a39692006-09-25 12:39:20 -05003812 * will be sent. If the device is a SATA device, a PHY reset will
3813 * be sent.
Brian Kingc6513092006-03-29 09:37:43 -06003814 *
3815 * Return value:
3816 * 0 on success / non-zero on failure
3817 **/
3818static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
3819 struct ipr_resource_entry *res)
3820{
3821 struct ipr_cmnd *ipr_cmd;
3822 struct ipr_ioarcb *ioarcb;
3823 struct ipr_cmd_pkt *cmd_pkt;
Brian King35a39692006-09-25 12:39:20 -05003824 struct ipr_ioarcb_ata_regs *regs;
Brian Kingc6513092006-03-29 09:37:43 -06003825 u32 ioasc;
3826
3827 ENTER;
3828 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3829 ioarcb = &ipr_cmd->ioarcb;
3830 cmd_pkt = &ioarcb->cmd_pkt;
Brian King35a39692006-09-25 12:39:20 -05003831 regs = &ioarcb->add_data.u.regs;
Brian Kingc6513092006-03-29 09:37:43 -06003832
3833 ioarcb->res_handle = res->cfgte.res_handle;
3834 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3835 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
Brian King35a39692006-09-25 12:39:20 -05003836 if (ipr_is_gata(res)) {
3837 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
3838 ioarcb->add_cmd_parms_len = cpu_to_be32(sizeof(regs->flags));
3839 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
3840 }
Brian Kingc6513092006-03-29 09:37:43 -06003841
3842 ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3843 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3844 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
Brian King35a39692006-09-25 12:39:20 -05003845 if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET)
3846 memcpy(&res->sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
3847 sizeof(struct ipr_ioasa_gata));
Brian Kingc6513092006-03-29 09:37:43 -06003848
3849 LEAVE;
3850 return (IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0);
3851}
3852
3853/**
Brian King35a39692006-09-25 12:39:20 -05003854 * ipr_sata_reset - Reset the SATA port
Tejun Heocc0680a2007-08-06 18:36:23 +09003855 * @link: SATA link to reset
Brian King35a39692006-09-25 12:39:20 -05003856 * @classes: class of the attached device
3857 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003858 * This function issues a SATA phy reset to the affected ATA link.
Brian King35a39692006-09-25 12:39:20 -05003859 *
3860 * Return value:
3861 * 0 on success / non-zero on failure
3862 **/
Tejun Heocc0680a2007-08-06 18:36:23 +09003863static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
Andrew Morton120bda32007-03-26 02:17:43 -07003864 unsigned long deadline)
Brian King35a39692006-09-25 12:39:20 -05003865{
Tejun Heocc0680a2007-08-06 18:36:23 +09003866 struct ipr_sata_port *sata_port = link->ap->private_data;
Brian King35a39692006-09-25 12:39:20 -05003867 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
3868 struct ipr_resource_entry *res;
3869 unsigned long lock_flags = 0;
3870 int rc = -ENXIO;
3871
3872 ENTER;
3873 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King73d98ff2006-11-21 10:27:58 -06003874 while(ioa_cfg->in_reset_reload) {
3875 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3876 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3877 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3878 }
3879
Brian King35a39692006-09-25 12:39:20 -05003880 res = sata_port->res;
3881 if (res) {
3882 rc = ipr_device_reset(ioa_cfg, res);
3883 switch(res->cfgte.proto) {
3884 case IPR_PROTO_SATA:
3885 case IPR_PROTO_SAS_STP:
3886 *classes = ATA_DEV_ATA;
3887 break;
3888 case IPR_PROTO_SATA_ATAPI:
3889 case IPR_PROTO_SAS_STP_ATAPI:
3890 *classes = ATA_DEV_ATAPI;
3891 break;
3892 default:
3893 *classes = ATA_DEV_UNKNOWN;
3894 break;
3895 };
3896 }
3897
3898 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3899 LEAVE;
3900 return rc;
3901}
3902
3903/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 * ipr_eh_dev_reset - Reset the device
3905 * @scsi_cmd: scsi command struct
3906 *
3907 * This function issues a device reset to the affected device.
3908 * A LUN reset will be sent to the device first. If that does
3909 * not work, a target reset will be sent.
3910 *
3911 * Return value:
3912 * SUCCESS / FAILED
3913 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003914static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915{
3916 struct ipr_cmnd *ipr_cmd;
3917 struct ipr_ioa_cfg *ioa_cfg;
3918 struct ipr_resource_entry *res;
Brian King35a39692006-09-25 12:39:20 -05003919 struct ata_port *ap;
3920 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
3922 ENTER;
3923 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3924 res = scsi_cmd->device->hostdata;
3925
brking@us.ibm.comeeb883072005-11-01 17:02:29 -06003926 if (!res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 return FAILED;
3928
3929 /*
3930 * If we are currently going through reset/reload, return failed. This will force the
3931 * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
3932 * reset to complete
3933 */
3934 if (ioa_cfg->in_reset_reload)
3935 return FAILED;
3936 if (ioa_cfg->ioa_is_dead)
3937 return FAILED;
3938
3939 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3940 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
3941 if (ipr_cmd->scsi_cmd)
3942 ipr_cmd->done = ipr_scsi_eh_done;
Brian King24d6f2b2007-03-29 12:43:17 -05003943 if (ipr_cmd->qc)
3944 ipr_cmd->done = ipr_sata_eh_done;
Brian King7402ece2006-11-21 10:28:23 -06003945 if (ipr_cmd->qc && !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
3946 ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
3947 ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
3948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 }
3950 }
3951
3952 res->resetting_device = 1;
Brian Kingfb3ed3c2006-03-29 09:37:37 -06003953 scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
Brian King35a39692006-09-25 12:39:20 -05003954
3955 if (ipr_is_gata(res) && res->sata_port) {
3956 ap = res->sata_port->ap;
3957 spin_unlock_irq(scsi_cmd->device->host->host_lock);
Tejun Heoa1efdab2008-03-25 12:22:50 +09003958 ata_std_error_handler(ap);
Brian King35a39692006-09-25 12:39:20 -05003959 spin_lock_irq(scsi_cmd->device->host->host_lock);
Brian King5af23d22007-05-09 15:36:35 -05003960
3961 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3962 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
3963 rc = -EIO;
3964 break;
3965 }
3966 }
Brian King35a39692006-09-25 12:39:20 -05003967 } else
3968 rc = ipr_device_reset(ioa_cfg, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 res->resetting_device = 0;
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 LEAVE;
Brian Kingc6513092006-03-29 09:37:43 -06003972 return (rc ? FAILED : SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003975static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
3976{
3977 int rc;
3978
3979 spin_lock_irq(cmd->device->host->host_lock);
3980 rc = __ipr_eh_dev_reset(cmd);
3981 spin_unlock_irq(cmd->device->host->host_lock);
3982
3983 return rc;
3984}
3985
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986/**
3987 * ipr_bus_reset_done - Op done function for bus reset.
3988 * @ipr_cmd: ipr command struct
3989 *
3990 * This function is the op done function for a bus reset
3991 *
3992 * Return value:
3993 * none
3994 **/
3995static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
3996{
3997 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3998 struct ipr_resource_entry *res;
3999
4000 ENTER;
4001 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4002 if (!memcmp(&res->cfgte.res_handle, &ipr_cmd->ioarcb.res_handle,
4003 sizeof(res->cfgte.res_handle))) {
4004 scsi_report_bus_reset(ioa_cfg->host, res->cfgte.res_addr.bus);
4005 break;
4006 }
4007 }
4008
4009 /*
4010 * If abort has not completed, indicate the reset has, else call the
4011 * abort's done function to wake the sleeping eh thread
4012 */
4013 if (ipr_cmd->sibling->sibling)
4014 ipr_cmd->sibling->sibling = NULL;
4015 else
4016 ipr_cmd->sibling->done(ipr_cmd->sibling);
4017
4018 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4019 LEAVE;
4020}
4021
4022/**
4023 * ipr_abort_timeout - An abort task has timed out
4024 * @ipr_cmd: ipr command struct
4025 *
4026 * This function handles when an abort task times out. If this
4027 * happens we issue a bus reset since we have resources tied
4028 * up that must be freed before returning to the midlayer.
4029 *
4030 * Return value:
4031 * none
4032 **/
4033static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
4034{
4035 struct ipr_cmnd *reset_cmd;
4036 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4037 struct ipr_cmd_pkt *cmd_pkt;
4038 unsigned long lock_flags = 0;
4039
4040 ENTER;
4041 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4042 if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
4043 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4044 return;
4045 }
4046
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004047 sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4049 ipr_cmd->sibling = reset_cmd;
4050 reset_cmd->sibling = ipr_cmd;
4051 reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
4052 cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
4053 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4054 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4055 cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
4056
4057 ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4058 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4059 LEAVE;
4060}
4061
4062/**
4063 * ipr_cancel_op - Cancel specified op
4064 * @scsi_cmd: scsi command struct
4065 *
4066 * This function cancels specified op.
4067 *
4068 * Return value:
4069 * SUCCESS / FAILED
4070 **/
4071static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
4072{
4073 struct ipr_cmnd *ipr_cmd;
4074 struct ipr_ioa_cfg *ioa_cfg;
4075 struct ipr_resource_entry *res;
4076 struct ipr_cmd_pkt *cmd_pkt;
4077 u32 ioasc;
4078 int op_found = 0;
4079
4080 ENTER;
4081 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4082 res = scsi_cmd->device->hostdata;
4083
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004084 /* If we are currently going through reset/reload, return failed.
4085 * This will force the mid-layer to call ipr_eh_host_reset,
4086 * which will then go to sleep and wait for the reset to complete
4087 */
4088 if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
4089 return FAILED;
Brian King04d97682006-11-21 10:28:04 -06004090 if (!res || !ipr_is_gscsi(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 return FAILED;
4092
4093 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4094 if (ipr_cmd->scsi_cmd == scsi_cmd) {
4095 ipr_cmd->done = ipr_scsi_eh_done;
4096 op_found = 1;
4097 break;
4098 }
4099 }
4100
4101 if (!op_found)
4102 return SUCCESS;
4103
4104 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4105 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
4106 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4107 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4108 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4109 ipr_cmd->u.sdev = scsi_cmd->device;
4110
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004111 scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
4112 scsi_cmd->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
4114 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4115
4116 /*
4117 * If the abort task timed out and we sent a bus reset, we will get
4118 * one the following responses to the abort
4119 */
4120 if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
4121 ioasc = 0;
4122 ipr_trace;
4123 }
4124
4125 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004126 if (!ipr_is_naca_model(res))
4127 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
4129 LEAVE;
4130 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
4131}
4132
4133/**
4134 * ipr_eh_abort - Abort a single op
4135 * @scsi_cmd: scsi command struct
4136 *
4137 * Return value:
4138 * SUCCESS / FAILED
4139 **/
4140static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
4141{
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004142 unsigned long flags;
4143 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
4145 ENTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004147 spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
4148 rc = ipr_cancel_op(scsi_cmd);
4149 spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
4151 LEAVE;
Jeff Garzik 8fa728a2005-05-28 07:54:40 -04004152 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153}
4154
4155/**
4156 * ipr_handle_other_interrupt - Handle "other" interrupts
4157 * @ioa_cfg: ioa config struct
4158 * @int_reg: interrupt register
4159 *
4160 * Return value:
4161 * IRQ_NONE / IRQ_HANDLED
4162 **/
4163static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
4164 volatile u32 int_reg)
4165{
4166 irqreturn_t rc = IRQ_HANDLED;
4167
4168 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4169 /* Mask the interrupt */
4170 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
4171
4172 /* Clear the interrupt */
4173 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
4174 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4175
4176 list_del(&ioa_cfg->reset_cmd->queue);
4177 del_timer(&ioa_cfg->reset_cmd->timer);
4178 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
4179 } else {
4180 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
4181 ioa_cfg->ioa_unit_checked = 1;
4182 else
4183 dev_err(&ioa_cfg->pdev->dev,
4184 "Permanent IOA failure. 0x%08X\n", int_reg);
4185
4186 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4187 ioa_cfg->sdt_state = GET_DUMP;
4188
4189 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
4190 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4191 }
4192
4193 return rc;
4194}
4195
4196/**
4197 * ipr_isr - Interrupt service routine
4198 * @irq: irq number
4199 * @devp: pointer to ioa config struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 *
4201 * Return value:
4202 * IRQ_NONE / IRQ_HANDLED
4203 **/
David Howells7d12e782006-10-05 14:55:46 +01004204static irqreturn_t ipr_isr(int irq, void *devp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
4206 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
4207 unsigned long lock_flags = 0;
4208 volatile u32 int_reg, int_mask_reg;
4209 u32 ioasc;
4210 u16 cmd_index;
4211 struct ipr_cmnd *ipr_cmd;
4212 irqreturn_t rc = IRQ_NONE;
4213
4214 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4215
4216 /* If interrupts are disabled, ignore the interrupt */
4217 if (!ioa_cfg->allow_interrupts) {
4218 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4219 return IRQ_NONE;
4220 }
4221
4222 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4223 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4224
4225 /* If an interrupt on the adapter did not occur, ignore it */
4226 if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
4227 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4228 return IRQ_NONE;
4229 }
4230
4231 while (1) {
4232 ipr_cmd = NULL;
4233
4234 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
4235 ioa_cfg->toggle_bit) {
4236
4237 cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
4238 IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
4239
4240 if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
4241 ioa_cfg->errors_logged++;
4242 dev_err(&ioa_cfg->pdev->dev, "Invalid response handle from IOA\n");
4243
4244 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4245 ioa_cfg->sdt_state = GET_DUMP;
4246
4247 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
4248 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4249 return IRQ_HANDLED;
4250 }
4251
4252 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
4253
4254 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4255
4256 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
4257
4258 list_del(&ipr_cmd->queue);
4259 del_timer(&ipr_cmd->timer);
4260 ipr_cmd->done(ipr_cmd);
4261
4262 rc = IRQ_HANDLED;
4263
4264 if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
4265 ioa_cfg->hrrq_curr++;
4266 } else {
4267 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
4268 ioa_cfg->toggle_bit ^= 1u;
4269 }
4270 }
4271
4272 if (ipr_cmd != NULL) {
4273 /* Clear the PCI interrupt */
4274 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg);
4275 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
4276 } else
4277 break;
4278 }
4279
4280 if (unlikely(rc == IRQ_NONE))
4281 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
4282
4283 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4284 return rc;
4285}
4286
4287/**
4288 * ipr_build_ioadl - Build a scatter/gather list and map the buffer
4289 * @ioa_cfg: ioa config struct
4290 * @ipr_cmd: ipr command struct
4291 *
4292 * Return value:
4293 * 0 on success / -1 on failure
4294 **/
4295static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
4296 struct ipr_cmnd *ipr_cmd)
4297{
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004298 int i, nseg;
4299 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 u32 length;
4301 u32 ioadl_flags = 0;
4302 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4303 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4304 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4305
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004306 length = scsi_bufflen(scsi_cmd);
4307 if (!length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 return 0;
4309
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004310 nseg = scsi_dma_map(scsi_cmd);
4311 if (nseg < 0) {
4312 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
4313 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 }
4315
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004316 ipr_cmd->dma_use_sg = nseg;
4317
4318 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
4319 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
4320 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4321 ioarcb->write_data_transfer_length = cpu_to_be32(length);
4322 ioarcb->write_ioadl_len =
4323 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
4324 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
4325 ioadl_flags = IPR_IOADL_FLAGS_READ;
4326 ioarcb->read_data_transfer_length = cpu_to_be32(length);
4327 ioarcb->read_ioadl_len =
4328 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
4329 }
4330
4331 if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->add_data.u.ioadl)) {
4332 ioadl = ioarcb->add_data.u.ioadl;
4333 ioarcb->write_ioadl_addr =
4334 cpu_to_be32(be32_to_cpu(ioarcb->ioarcb_host_pci_addr) +
4335 offsetof(struct ipr_ioarcb, add_data));
4336 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
4337 }
4338
4339 scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
4340 ioadl[i].flags_and_data_len =
4341 cpu_to_be32(ioadl_flags | sg_dma_len(sg));
4342 ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
4343 }
4344
4345 ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
4346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347}
4348
4349/**
4350 * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
4351 * @scsi_cmd: scsi command struct
4352 *
4353 * Return value:
4354 * task attributes
4355 **/
4356static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
4357{
4358 u8 tag[2];
4359 u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
4360
4361 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
4362 switch (tag[0]) {
4363 case MSG_SIMPLE_TAG:
4364 rc = IPR_FLAGS_LO_SIMPLE_TASK;
4365 break;
4366 case MSG_HEAD_TAG:
4367 rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
4368 break;
4369 case MSG_ORDERED_TAG:
4370 rc = IPR_FLAGS_LO_ORDERED_TASK;
4371 break;
4372 };
4373 }
4374
4375 return rc;
4376}
4377
4378/**
4379 * ipr_erp_done - Process completion of ERP for a device
4380 * @ipr_cmd: ipr command struct
4381 *
4382 * This function copies the sense buffer into the scsi_cmd
4383 * struct and pushes the scsi_done function.
4384 *
4385 * Return value:
4386 * nothing
4387 **/
4388static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
4389{
4390 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4391 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4392 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4393 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4394
4395 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
4396 scsi_cmd->result |= (DID_ERROR << 16);
Brian Kingfb3ed3c2006-03-29 09:37:37 -06004397 scmd_printk(KERN_ERR, scsi_cmd,
4398 "Request Sense failed with IOASC: 0x%08X\n", ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 } else {
4400 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
4401 SCSI_SENSE_BUFFERSIZE);
4402 }
4403
4404 if (res) {
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004405 if (!ipr_is_naca_model(res))
4406 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 res->in_erp = 0;
4408 }
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004409 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4411 scsi_cmd->scsi_done(scsi_cmd);
4412}
4413
4414/**
4415 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
4416 * @ipr_cmd: ipr command struct
4417 *
4418 * Return value:
4419 * none
4420 **/
4421static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
4422{
Brian King51b1c7e2007-03-29 12:43:50 -05004423 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4424 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4425 dma_addr_t dma_addr = be32_to_cpu(ioarcb->ioarcb_host_pci_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
4427 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
4428 ioarcb->write_data_transfer_length = 0;
4429 ioarcb->read_data_transfer_length = 0;
4430 ioarcb->write_ioadl_len = 0;
4431 ioarcb->read_ioadl_len = 0;
4432 ioasa->ioasc = 0;
4433 ioasa->residual_data_len = 0;
Brian King51b1c7e2007-03-29 12:43:50 -05004434 ioarcb->write_ioadl_addr =
4435 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
4436 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437}
4438
4439/**
4440 * ipr_erp_request_sense - Send request sense to a device
4441 * @ipr_cmd: ipr command struct
4442 *
4443 * This function sends a request sense to a device as a result
4444 * of a check condition.
4445 *
4446 * Return value:
4447 * nothing
4448 **/
4449static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
4450{
4451 struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4452 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4453
4454 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
4455 ipr_erp_done(ipr_cmd);
4456 return;
4457 }
4458
4459 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
4460
4461 cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
4462 cmd_pkt->cdb[0] = REQUEST_SENSE;
4463 cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
4464 cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
4465 cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
4466 cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
4467
4468 ipr_cmd->ioadl[0].flags_and_data_len =
4469 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | SCSI_SENSE_BUFFERSIZE);
4470 ipr_cmd->ioadl[0].address =
4471 cpu_to_be32(ipr_cmd->sense_buffer_dma);
4472
4473 ipr_cmd->ioarcb.read_ioadl_len =
4474 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4475 ipr_cmd->ioarcb.read_data_transfer_length =
4476 cpu_to_be32(SCSI_SENSE_BUFFERSIZE);
4477
4478 ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
4479 IPR_REQUEST_SENSE_TIMEOUT * 2);
4480}
4481
4482/**
4483 * ipr_erp_cancel_all - Send cancel all to a device
4484 * @ipr_cmd: ipr command struct
4485 *
4486 * This function sends a cancel all to a device to clear the
4487 * queue. If we are running TCQ on the device, QERR is set to 1,
4488 * which means all outstanding ops have been dropped on the floor.
4489 * Cancel all will return them to us.
4490 *
4491 * Return value:
4492 * nothing
4493 **/
4494static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
4495{
4496 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4497 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4498 struct ipr_cmd_pkt *cmd_pkt;
4499
4500 res->in_erp = 1;
4501
4502 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
4503
4504 if (!scsi_get_tag_type(scsi_cmd->device)) {
4505 ipr_erp_request_sense(ipr_cmd);
4506 return;
4507 }
4508
4509 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4510 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4511 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4512
4513 ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
4514 IPR_CANCEL_ALL_TIMEOUT);
4515}
4516
4517/**
4518 * ipr_dump_ioasa - Dump contents of IOASA
4519 * @ioa_cfg: ioa config struct
4520 * @ipr_cmd: ipr command struct
Brian Kingfe964d02006-03-29 09:37:29 -06004521 * @res: resource entry struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 *
4523 * This function is invoked by the interrupt handler when ops
4524 * fail. It will log the IOASA if appropriate. Only called
4525 * for GPDD ops.
4526 *
4527 * Return value:
4528 * none
4529 **/
4530static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
Brian Kingfe964d02006-03-29 09:37:29 -06004531 struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532{
4533 int i;
4534 u16 data_len;
Brian Kingb0692dd2007-03-29 12:43:09 -05004535 u32 ioasc, fd_ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4537 __be32 *ioasa_data = (__be32 *)ioasa;
4538 int error_index;
4539
4540 ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
Brian Kingb0692dd2007-03-29 12:43:09 -05004541 fd_ioasc = be32_to_cpu(ioasa->fd_ioasc) & IPR_IOASC_IOASC_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
4543 if (0 == ioasc)
4544 return;
4545
4546 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
4547 return;
4548
Brian Kingb0692dd2007-03-29 12:43:09 -05004549 if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
4550 error_index = ipr_get_error(fd_ioasc);
4551 else
4552 error_index = ipr_get_error(ioasc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
4554 if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
4555 /* Don't log an error if the IOA already logged one */
4556 if (ioasa->ilid != 0)
4557 return;
4558
Brian Kingcc9bd5d2007-03-29 12:43:01 -05004559 if (!ipr_is_gscsi(res))
4560 return;
4561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (ipr_error_table[error_index].log_ioasa == 0)
4563 return;
4564 }
4565
Brian Kingfe964d02006-03-29 09:37:29 -06004566 ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
4568 if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
4569 data_len = sizeof(struct ipr_ioasa);
4570 else
4571 data_len = be16_to_cpu(ioasa->ret_stat_len);
4572
4573 ipr_err("IOASA Dump:\n");
4574
4575 for (i = 0; i < data_len / 4; i += 4) {
4576 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
4577 be32_to_cpu(ioasa_data[i]),
4578 be32_to_cpu(ioasa_data[i+1]),
4579 be32_to_cpu(ioasa_data[i+2]),
4580 be32_to_cpu(ioasa_data[i+3]));
4581 }
4582}
4583
4584/**
4585 * ipr_gen_sense - Generate SCSI sense data from an IOASA
4586 * @ioasa: IOASA
4587 * @sense_buf: sense data buffer
4588 *
4589 * Return value:
4590 * none
4591 **/
4592static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
4593{
4594 u32 failing_lba;
4595 u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
4596 struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
4597 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4598 u32 ioasc = be32_to_cpu(ioasa->ioasc);
4599
4600 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
4601
4602 if (ioasc >= IPR_FIRST_DRIVER_IOASC)
4603 return;
4604
4605 ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
4606
4607 if (ipr_is_vset_device(res) &&
4608 ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
4609 ioasa->u.vset.failing_lba_hi != 0) {
4610 sense_buf[0] = 0x72;
4611 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
4612 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
4613 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
4614
4615 sense_buf[7] = 12;
4616 sense_buf[8] = 0;
4617 sense_buf[9] = 0x0A;
4618 sense_buf[10] = 0x80;
4619
4620 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
4621
4622 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
4623 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
4624 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
4625 sense_buf[15] = failing_lba & 0x000000ff;
4626
4627 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
4628
4629 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
4630 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
4631 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
4632 sense_buf[19] = failing_lba & 0x000000ff;
4633 } else {
4634 sense_buf[0] = 0x70;
4635 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
4636 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
4637 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
4638
4639 /* Illegal request */
4640 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
4641 (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
4642 sense_buf[7] = 10; /* additional length */
4643
4644 /* IOARCB was in error */
4645 if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
4646 sense_buf[15] = 0xC0;
4647 else /* Parameter data was invalid */
4648 sense_buf[15] = 0x80;
4649
4650 sense_buf[16] =
4651 ((IPR_FIELD_POINTER_MASK &
4652 be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
4653 sense_buf[17] =
4654 (IPR_FIELD_POINTER_MASK &
4655 be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
4656 } else {
4657 if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
4658 if (ipr_is_vset_device(res))
4659 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
4660 else
4661 failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
4662
4663 sense_buf[0] |= 0x80; /* Or in the Valid bit */
4664 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
4665 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
4666 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
4667 sense_buf[6] = failing_lba & 0x000000ff;
4668 }
4669
4670 sense_buf[7] = 6; /* additional length */
4671 }
4672 }
4673}
4674
4675/**
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004676 * ipr_get_autosense - Copy autosense data to sense buffer
4677 * @ipr_cmd: ipr command struct
4678 *
4679 * This function copies the autosense buffer to the buffer
4680 * in the scsi_cmd, if there is autosense available.
4681 *
4682 * Return value:
4683 * 1 if autosense was available / 0 if not
4684 **/
4685static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
4686{
4687 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
4688
Brian King117d2ce2006-08-02 14:57:58 -05004689 if ((be32_to_cpu(ioasa->ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004690 return 0;
4691
4692 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
4693 min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
4694 SCSI_SENSE_BUFFERSIZE));
4695 return 1;
4696}
4697
4698/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 * ipr_erp_start - Process an error response for a SCSI op
4700 * @ioa_cfg: ioa config struct
4701 * @ipr_cmd: ipr command struct
4702 *
4703 * This function determines whether or not to initiate ERP
4704 * on the affected device.
4705 *
4706 * Return value:
4707 * nothing
4708 **/
4709static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
4710 struct ipr_cmnd *ipr_cmd)
4711{
4712 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4713 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
4714 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
Brian King8a048992007-04-26 16:00:10 -05004715 u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
4717 if (!res) {
4718 ipr_scsi_eh_done(ipr_cmd);
4719 return;
4720 }
4721
Brian King8a048992007-04-26 16:00:10 -05004722 if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 ipr_gen_sense(ipr_cmd);
4724
Brian Kingcc9bd5d2007-03-29 12:43:01 -05004725 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
4726
Brian King8a048992007-04-26 16:00:10 -05004727 switch (masked_ioasc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004729 if (ipr_is_naca_model(res))
4730 scsi_cmd->result |= (DID_ABORT << 16);
4731 else
4732 scsi_cmd->result |= (DID_IMM_RETRY << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 break;
4734 case IPR_IOASC_IR_RESOURCE_HANDLE:
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06004735 case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 scsi_cmd->result |= (DID_NO_CONNECT << 16);
4737 break;
4738 case IPR_IOASC_HW_SEL_TIMEOUT:
4739 scsi_cmd->result |= (DID_NO_CONNECT << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004740 if (!ipr_is_naca_model(res))
4741 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 break;
4743 case IPR_IOASC_SYNC_REQUIRED:
4744 if (!res->in_erp)
4745 res->needs_sync_complete = 1;
4746 scsi_cmd->result |= (DID_IMM_RETRY << 16);
4747 break;
4748 case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
brking@us.ibm.comb0df54b2005-11-01 17:01:47 -06004749 case IPR_IOASA_IR_DUAL_IOA_DISABLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
4751 break;
4752 case IPR_IOASC_BUS_WAS_RESET:
4753 case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
4754 /*
4755 * Report the bus reset and ask for a retry. The device
4756 * will give CC/UA the next command.
4757 */
4758 if (!res->resetting_device)
4759 scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
4760 scsi_cmd->result |= (DID_ERROR << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004761 if (!ipr_is_naca_model(res))
4762 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 break;
4764 case IPR_IOASC_HW_DEV_BUS_STATUS:
4765 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
4766 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004767 if (!ipr_get_autosense(ipr_cmd)) {
4768 if (!ipr_is_naca_model(res)) {
4769 ipr_erp_cancel_all(ipr_cmd);
4770 return;
4771 }
4772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 }
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004774 if (!ipr_is_naca_model(res))
4775 res->needs_sync_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 break;
4777 case IPR_IOASC_NR_INIT_CMD_REQUIRED:
4778 break;
4779 default:
Brian King5b7304f2006-08-02 14:57:51 -05004780 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
4781 scsi_cmd->result |= (DID_ERROR << 16);
brking@us.ibm.comee0a90f2005-11-01 17:02:22 -06004782 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 res->needs_sync_complete = 1;
4784 break;
4785 }
4786
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004787 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4789 scsi_cmd->scsi_done(scsi_cmd);
4790}
4791
4792/**
4793 * ipr_scsi_done - mid-layer done function
4794 * @ipr_cmd: ipr command struct
4795 *
4796 * This function is invoked by the interrupt handler for
4797 * ops generated by the SCSI mid-layer
4798 *
4799 * Return value:
4800 * none
4801 **/
4802static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
4803{
4804 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4805 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4806 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4807
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004808 scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->ioasa.residual_data_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
4810 if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
FUJITA Tomonori63015bc2007-05-26 00:26:59 +09004811 scsi_dma_unmap(ipr_cmd->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4813 scsi_cmd->scsi_done(scsi_cmd);
4814 } else
4815 ipr_erp_start(ioa_cfg, ipr_cmd);
4816}
4817
4818/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 * ipr_queuecommand - Queue a mid-layer request
4820 * @scsi_cmd: scsi command struct
4821 * @done: done function
4822 *
4823 * This function queues a request generated by the mid-layer.
4824 *
4825 * Return value:
4826 * 0 on success
4827 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
4828 * SCSI_MLQUEUE_HOST_BUSY if host is busy
4829 **/
4830static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
4831 void (*done) (struct scsi_cmnd *))
4832{
4833 struct ipr_ioa_cfg *ioa_cfg;
4834 struct ipr_resource_entry *res;
4835 struct ipr_ioarcb *ioarcb;
4836 struct ipr_cmnd *ipr_cmd;
4837 int rc = 0;
4838
4839 scsi_cmd->scsi_done = done;
4840 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4841 res = scsi_cmd->device->hostdata;
4842 scsi_cmd->result = (DID_OK << 16);
4843
4844 /*
4845 * We are currently blocking all devices due to a host reset
4846 * We have told the host to stop giving us new requests, but
4847 * ERP ops don't count. FIXME
4848 */
4849 if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
4850 return SCSI_MLQUEUE_HOST_BUSY;
4851
4852 /*
4853 * FIXME - Create scsi_set_host_offline interface
4854 * and the ioa_is_dead check can be removed
4855 */
4856 if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
4857 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
4858 scsi_cmd->result = (DID_NO_CONNECT << 16);
4859 scsi_cmd->scsi_done(scsi_cmd);
4860 return 0;
4861 }
4862
Brian King35a39692006-09-25 12:39:20 -05004863 if (ipr_is_gata(res) && res->sata_port)
4864 return ata_sas_queuecmd(scsi_cmd, done, res->sata_port->ap);
4865
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4867 ioarcb = &ipr_cmd->ioarcb;
4868 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
4869
4870 memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
4871 ipr_cmd->scsi_cmd = scsi_cmd;
4872 ioarcb->res_handle = res->cfgte.res_handle;
4873 ipr_cmd->done = ipr_scsi_done;
4874 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
4875
4876 if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
4877 if (scsi_cmd->underflow == 0)
4878 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
4879
4880 if (res->needs_sync_complete) {
4881 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
4882 res->needs_sync_complete = 0;
4883 }
4884
4885 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
4886 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
4887 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
4888 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
4889 }
4890
4891 if (scsi_cmd->cmnd[0] >= 0xC0 &&
4892 (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
4893 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 if (likely(rc == 0))
4896 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
4897
4898 if (likely(rc == 0)) {
4899 mb();
4900 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
4901 ioa_cfg->regs.ioarrin_reg);
4902 } else {
4903 list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4904 return SCSI_MLQUEUE_HOST_BUSY;
4905 }
4906
4907 return 0;
4908}
4909
4910/**
Brian King35a39692006-09-25 12:39:20 -05004911 * ipr_ioctl - IOCTL handler
4912 * @sdev: scsi device struct
4913 * @cmd: IOCTL cmd
4914 * @arg: IOCTL arg
4915 *
4916 * Return value:
4917 * 0 on success / other on failure
4918 **/
Adrian Bunkbd705f22006-11-21 10:28:48 -06004919static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
Brian King35a39692006-09-25 12:39:20 -05004920{
4921 struct ipr_resource_entry *res;
4922
4923 res = (struct ipr_resource_entry *)sdev->hostdata;
Brian King0ce3a7e2008-07-11 13:37:50 -05004924 if (res && ipr_is_gata(res)) {
4925 if (cmd == HDIO_GET_IDENTITY)
4926 return -ENOTTY;
Jeff Garzik94be9a52009-01-16 10:17:09 -05004927 return ata_sas_scsi_ioctl(res->sata_port->ap, sdev, cmd, arg);
Brian King0ce3a7e2008-07-11 13:37:50 -05004928 }
Brian King35a39692006-09-25 12:39:20 -05004929
4930 return -EINVAL;
4931}
4932
4933/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 * ipr_info - Get information about the card/driver
4935 * @scsi_host: scsi host struct
4936 *
4937 * Return value:
4938 * pointer to buffer with description string
4939 **/
4940static const char * ipr_ioa_info(struct Scsi_Host *host)
4941{
4942 static char buffer[512];
4943 struct ipr_ioa_cfg *ioa_cfg;
4944 unsigned long lock_flags = 0;
4945
4946 ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
4947
4948 spin_lock_irqsave(host->host_lock, lock_flags);
4949 sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
4950 spin_unlock_irqrestore(host->host_lock, lock_flags);
4951
4952 return buffer;
4953}
4954
4955static struct scsi_host_template driver_template = {
4956 .module = THIS_MODULE,
4957 .name = "IPR",
4958 .info = ipr_ioa_info,
Brian King35a39692006-09-25 12:39:20 -05004959 .ioctl = ipr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 .queuecommand = ipr_queuecommand,
4961 .eh_abort_handler = ipr_eh_abort,
4962 .eh_device_reset_handler = ipr_eh_dev_reset,
4963 .eh_host_reset_handler = ipr_eh_host_reset,
4964 .slave_alloc = ipr_slave_alloc,
4965 .slave_configure = ipr_slave_configure,
4966 .slave_destroy = ipr_slave_destroy,
Brian King35a39692006-09-25 12:39:20 -05004967 .target_alloc = ipr_target_alloc,
4968 .target_destroy = ipr_target_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 .change_queue_depth = ipr_change_queue_depth,
4970 .change_queue_type = ipr_change_queue_type,
4971 .bios_param = ipr_biosparam,
4972 .can_queue = IPR_MAX_COMMANDS,
4973 .this_id = -1,
4974 .sg_tablesize = IPR_MAX_SGLIST,
4975 .max_sectors = IPR_IOA_MAX_SECTORS,
4976 .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
4977 .use_clustering = ENABLE_CLUSTERING,
4978 .shost_attrs = ipr_ioa_attrs,
4979 .sdev_attrs = ipr_dev_attrs,
4980 .proc_name = IPR_NAME
4981};
4982
Brian King35a39692006-09-25 12:39:20 -05004983/**
4984 * ipr_ata_phy_reset - libata phy_reset handler
4985 * @ap: ata port to reset
4986 *
4987 **/
4988static void ipr_ata_phy_reset(struct ata_port *ap)
4989{
4990 unsigned long flags;
4991 struct ipr_sata_port *sata_port = ap->private_data;
4992 struct ipr_resource_entry *res = sata_port->res;
4993 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4994 int rc;
4995
4996 ENTER;
4997 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
4998 while(ioa_cfg->in_reset_reload) {
4999 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5000 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5001 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5002 }
5003
5004 if (!ioa_cfg->allow_cmds)
5005 goto out_unlock;
5006
5007 rc = ipr_device_reset(ioa_cfg, res);
5008
5009 if (rc) {
Jeff Garzikac8869d2007-08-16 03:17:03 -04005010 ata_port_disable(ap);
Brian King35a39692006-09-25 12:39:20 -05005011 goto out_unlock;
5012 }
5013
5014 switch(res->cfgte.proto) {
5015 case IPR_PROTO_SATA:
5016 case IPR_PROTO_SAS_STP:
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005017 ap->link.device[0].class = ATA_DEV_ATA;
Brian King35a39692006-09-25 12:39:20 -05005018 break;
5019 case IPR_PROTO_SATA_ATAPI:
5020 case IPR_PROTO_SAS_STP_ATAPI:
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005021 ap->link.device[0].class = ATA_DEV_ATAPI;
Brian King35a39692006-09-25 12:39:20 -05005022 break;
5023 default:
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005024 ap->link.device[0].class = ATA_DEV_UNKNOWN;
Jeff Garzikac8869d2007-08-16 03:17:03 -04005025 ata_port_disable(ap);
Brian King35a39692006-09-25 12:39:20 -05005026 break;
5027 };
5028
5029out_unlock:
5030 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5031 LEAVE;
5032}
5033
5034/**
5035 * ipr_ata_post_internal - Cleanup after an internal command
5036 * @qc: ATA queued command
5037 *
5038 * Return value:
5039 * none
5040 **/
5041static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
5042{
5043 struct ipr_sata_port *sata_port = qc->ap->private_data;
5044 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5045 struct ipr_cmnd *ipr_cmd;
5046 unsigned long flags;
5047
5048 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
Brian King73d98ff2006-11-21 10:27:58 -06005049 while(ioa_cfg->in_reset_reload) {
5050 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5051 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5052 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
5053 }
5054
Brian King35a39692006-09-25 12:39:20 -05005055 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
5056 if (ipr_cmd->qc == qc) {
5057 ipr_device_reset(ioa_cfg, sata_port->res);
5058 break;
5059 }
5060 }
5061 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
5062}
5063
5064/**
Brian King35a39692006-09-25 12:39:20 -05005065 * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
5066 * @regs: destination
5067 * @tf: source ATA taskfile
5068 *
5069 * Return value:
5070 * none
5071 **/
5072static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
5073 struct ata_taskfile *tf)
5074{
5075 regs->feature = tf->feature;
5076 regs->nsect = tf->nsect;
5077 regs->lbal = tf->lbal;
5078 regs->lbam = tf->lbam;
5079 regs->lbah = tf->lbah;
5080 regs->device = tf->device;
5081 regs->command = tf->command;
5082 regs->hob_feature = tf->hob_feature;
5083 regs->hob_nsect = tf->hob_nsect;
5084 regs->hob_lbal = tf->hob_lbal;
5085 regs->hob_lbam = tf->hob_lbam;
5086 regs->hob_lbah = tf->hob_lbah;
5087 regs->ctl = tf->ctl;
5088}
5089
5090/**
5091 * ipr_sata_done - done function for SATA commands
5092 * @ipr_cmd: ipr command struct
5093 *
5094 * This function is invoked by the interrupt handler for
5095 * ops generated by the SCSI mid-layer to SATA devices
5096 *
5097 * Return value:
5098 * none
5099 **/
5100static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
5101{
5102 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5103 struct ata_queued_cmd *qc = ipr_cmd->qc;
5104 struct ipr_sata_port *sata_port = qc->ap->private_data;
5105 struct ipr_resource_entry *res = sata_port->res;
5106 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5107
5108 memcpy(&sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
5109 sizeof(struct ipr_ioasa_gata));
5110 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5111
5112 if (be32_to_cpu(ipr_cmd->ioasa.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
5113 scsi_report_device_reset(ioa_cfg->host, res->cfgte.res_addr.bus,
5114 res->cfgte.res_addr.target);
5115
5116 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5117 qc->err_mask |= __ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5118 else
5119 qc->err_mask |= ac_err_mask(ipr_cmd->ioasa.u.gata.status);
5120 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5121 ata_qc_complete(qc);
5122}
5123
5124/**
5125 * ipr_build_ata_ioadl - Build an ATA scatter/gather list
5126 * @ipr_cmd: ipr command struct
5127 * @qc: ATA queued command
5128 *
5129 **/
5130static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
5131 struct ata_queued_cmd *qc)
5132{
5133 u32 ioadl_flags = 0;
5134 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5135 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04005136 struct ipr_ioadl_desc *last_ioadl = NULL;
James Bottomleydde20202008-02-19 11:36:56 +01005137 int len = qc->nbytes;
Brian King35a39692006-09-25 12:39:20 -05005138 struct scatterlist *sg;
Tejun Heoff2aeb12007-12-05 16:43:11 +09005139 unsigned int si;
Brian King35a39692006-09-25 12:39:20 -05005140
5141 if (len == 0)
5142 return;
5143
5144 if (qc->dma_dir == DMA_TO_DEVICE) {
5145 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5146 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5147 ioarcb->write_data_transfer_length = cpu_to_be32(len);
5148 ioarcb->write_ioadl_len =
5149 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5150 } else if (qc->dma_dir == DMA_FROM_DEVICE) {
5151 ioadl_flags = IPR_IOADL_FLAGS_READ;
5152 ioarcb->read_data_transfer_length = cpu_to_be32(len);
5153 ioarcb->read_ioadl_len =
5154 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5155 }
5156
Tejun Heoff2aeb12007-12-05 16:43:11 +09005157 for_each_sg(qc->sg, sg, qc->n_elem, si) {
Brian King35a39692006-09-25 12:39:20 -05005158 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5159 ioadl->address = cpu_to_be32(sg_dma_address(sg));
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04005160
5161 last_ioadl = ioadl;
5162 ioadl++;
Brian King35a39692006-09-25 12:39:20 -05005163 }
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04005164
5165 if (likely(last_ioadl))
5166 last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
Brian King35a39692006-09-25 12:39:20 -05005167}
5168
5169/**
5170 * ipr_qc_issue - Issue a SATA qc to a device
5171 * @qc: queued command
5172 *
5173 * Return value:
5174 * 0 if success
5175 **/
5176static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
5177{
5178 struct ata_port *ap = qc->ap;
5179 struct ipr_sata_port *sata_port = ap->private_data;
5180 struct ipr_resource_entry *res = sata_port->res;
5181 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
5182 struct ipr_cmnd *ipr_cmd;
5183 struct ipr_ioarcb *ioarcb;
5184 struct ipr_ioarcb_ata_regs *regs;
5185
5186 if (unlikely(!ioa_cfg->allow_cmds || ioa_cfg->ioa_is_dead))
Brian King0feeed82007-03-29 12:43:43 -05005187 return AC_ERR_SYSTEM;
Brian King35a39692006-09-25 12:39:20 -05005188
5189 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5190 ioarcb = &ipr_cmd->ioarcb;
5191 regs = &ioarcb->add_data.u.regs;
5192
5193 memset(&ioarcb->add_data, 0, sizeof(ioarcb->add_data));
5194 ioarcb->add_cmd_parms_len = cpu_to_be32(sizeof(ioarcb->add_data.u.regs));
5195
5196 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5197 ipr_cmd->qc = qc;
5198 ipr_cmd->done = ipr_sata_done;
5199 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
5200 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
5201 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
5202 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
James Bottomleydde20202008-02-19 11:36:56 +01005203 ipr_cmd->dma_use_sg = qc->n_elem;
Brian King35a39692006-09-25 12:39:20 -05005204
5205 ipr_build_ata_ioadl(ipr_cmd, qc);
5206 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
5207 ipr_copy_sata_tf(regs, &qc->tf);
5208 memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
5209 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
5210
5211 switch (qc->tf.protocol) {
5212 case ATA_PROT_NODATA:
5213 case ATA_PROT_PIO:
5214 break;
5215
5216 case ATA_PROT_DMA:
5217 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5218 break;
5219
Tejun Heo0dc36882007-12-18 16:34:43 -05005220 case ATAPI_PROT_PIO:
5221 case ATAPI_PROT_NODATA:
Brian King35a39692006-09-25 12:39:20 -05005222 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5223 break;
5224
Tejun Heo0dc36882007-12-18 16:34:43 -05005225 case ATAPI_PROT_DMA:
Brian King35a39692006-09-25 12:39:20 -05005226 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
5227 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
5228 break;
5229
5230 default:
5231 WARN_ON(1);
Brian King0feeed82007-03-29 12:43:43 -05005232 return AC_ERR_INVALID;
Brian King35a39692006-09-25 12:39:20 -05005233 }
5234
5235 mb();
5236 writel(be32_to_cpu(ioarcb->ioarcb_host_pci_addr),
5237 ioa_cfg->regs.ioarrin_reg);
5238 return 0;
5239}
5240
5241/**
Tejun Heo4c9bf4e2008-04-07 22:47:20 +09005242 * ipr_qc_fill_rtf - Read result TF
5243 * @qc: ATA queued command
5244 *
5245 * Return value:
5246 * true
5247 **/
5248static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
5249{
5250 struct ipr_sata_port *sata_port = qc->ap->private_data;
5251 struct ipr_ioasa_gata *g = &sata_port->ioasa;
5252 struct ata_taskfile *tf = &qc->result_tf;
5253
5254 tf->feature = g->error;
5255 tf->nsect = g->nsect;
5256 tf->lbal = g->lbal;
5257 tf->lbam = g->lbam;
5258 tf->lbah = g->lbah;
5259 tf->device = g->device;
5260 tf->command = g->status;
5261 tf->hob_nsect = g->hob_nsect;
5262 tf->hob_lbal = g->hob_lbal;
5263 tf->hob_lbam = g->hob_lbam;
5264 tf->hob_lbah = g->hob_lbah;
5265 tf->ctl = g->alt_status;
5266
5267 return true;
5268}
5269
Brian King35a39692006-09-25 12:39:20 -05005270static struct ata_port_operations ipr_sata_ops = {
Brian King35a39692006-09-25 12:39:20 -05005271 .phy_reset = ipr_ata_phy_reset,
Tejun Heoa1efdab2008-03-25 12:22:50 +09005272 .hardreset = ipr_sata_reset,
Brian King35a39692006-09-25 12:39:20 -05005273 .post_internal_cmd = ipr_ata_post_internal,
Brian King35a39692006-09-25 12:39:20 -05005274 .qc_prep = ata_noop_qc_prep,
5275 .qc_issue = ipr_qc_issue,
Tejun Heo4c9bf4e2008-04-07 22:47:20 +09005276 .qc_fill_rtf = ipr_qc_fill_rtf,
Brian King35a39692006-09-25 12:39:20 -05005277 .port_start = ata_sas_port_start,
5278 .port_stop = ata_sas_port_stop
5279};
5280
5281static struct ata_port_info sata_port_info = {
5282 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
5283 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
5284 .pio_mask = 0x10, /* pio4 */
5285 .mwdma_mask = 0x07,
5286 .udma_mask = 0x7f, /* udma0-6 */
5287 .port_ops = &ipr_sata_ops
5288};
5289
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290#ifdef CONFIG_PPC_PSERIES
5291static const u16 ipr_blocked_processors[] = {
5292 PV_NORTHSTAR,
5293 PV_PULSAR,
5294 PV_POWER4,
5295 PV_ICESTAR,
5296 PV_SSTAR,
5297 PV_POWER4p,
5298 PV_630,
5299 PV_630p
5300};
5301
5302/**
5303 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
5304 * @ioa_cfg: ioa cfg struct
5305 *
5306 * Adapters that use Gemstone revision < 3.1 do not work reliably on
5307 * certain pSeries hardware. This function determines if the given
5308 * adapter is in one of these confgurations or not.
5309 *
5310 * Return value:
5311 * 1 if adapter is not supported / 0 if adapter is supported
5312 **/
5313static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
5314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 int i;
5316
Auke Kok44c10132007-06-08 15:46:36 -07005317 if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
5318 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
5319 if (__is_processor(ipr_blocked_processors[i]))
5320 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 }
5322 }
5323 return 0;
5324}
5325#else
5326#define ipr_invalid_adapter(ioa_cfg) 0
5327#endif
5328
5329/**
5330 * ipr_ioa_bringdown_done - IOA bring down completion.
5331 * @ipr_cmd: ipr command struct
5332 *
5333 * This function processes the completion of an adapter bring down.
5334 * It wakes any reset sleepers.
5335 *
5336 * Return value:
5337 * IPR_RC_JOB_RETURN
5338 **/
5339static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
5340{
5341 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5342
5343 ENTER;
5344 ioa_cfg->in_reset_reload = 0;
5345 ioa_cfg->reset_retries = 0;
5346 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5347 wake_up_all(&ioa_cfg->reset_wait_q);
5348
5349 spin_unlock_irq(ioa_cfg->host->host_lock);
5350 scsi_unblock_requests(ioa_cfg->host);
5351 spin_lock_irq(ioa_cfg->host->host_lock);
5352 LEAVE;
5353
5354 return IPR_RC_JOB_RETURN;
5355}
5356
5357/**
5358 * ipr_ioa_reset_done - IOA reset completion.
5359 * @ipr_cmd: ipr command struct
5360 *
5361 * This function processes the completion of an adapter reset.
5362 * It schedules any necessary mid-layer add/removes and
5363 * wakes any reset sleepers.
5364 *
5365 * Return value:
5366 * IPR_RC_JOB_RETURN
5367 **/
5368static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
5369{
5370 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5371 struct ipr_resource_entry *res;
5372 struct ipr_hostrcb *hostrcb, *temp;
5373 int i = 0;
5374
5375 ENTER;
5376 ioa_cfg->in_reset_reload = 0;
5377 ioa_cfg->allow_cmds = 1;
5378 ioa_cfg->reset_cmd = NULL;
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06005379 ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
5381 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
5382 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
5383 ipr_trace;
5384 break;
5385 }
5386 }
5387 schedule_work(&ioa_cfg->work_q);
5388
5389 list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
5390 list_del(&hostrcb->queue);
5391 if (i++ < IPR_NUM_LOG_HCAMS)
5392 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
5393 else
5394 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
5395 }
5396
Brian King6bb04172007-04-26 16:00:08 -05005397 scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
5399
5400 ioa_cfg->reset_retries = 0;
5401 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5402 wake_up_all(&ioa_cfg->reset_wait_q);
5403
Mark Nelson30237852008-12-10 12:23:20 +11005404 spin_unlock(ioa_cfg->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 scsi_unblock_requests(ioa_cfg->host);
Mark Nelson30237852008-12-10 12:23:20 +11005406 spin_lock(ioa_cfg->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
5408 if (!ioa_cfg->allow_cmds)
5409 scsi_block_requests(ioa_cfg->host);
5410
5411 LEAVE;
5412 return IPR_RC_JOB_RETURN;
5413}
5414
5415/**
5416 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
5417 * @supported_dev: supported device struct
5418 * @vpids: vendor product id struct
5419 *
5420 * Return value:
5421 * none
5422 **/
5423static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
5424 struct ipr_std_inq_vpids *vpids)
5425{
5426 memset(supported_dev, 0, sizeof(struct ipr_supported_device));
5427 memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
5428 supported_dev->num_records = 1;
5429 supported_dev->data_length =
5430 cpu_to_be16(sizeof(struct ipr_supported_device));
5431 supported_dev->reserved = 0;
5432}
5433
5434/**
5435 * ipr_set_supported_devs - Send Set Supported Devices for a device
5436 * @ipr_cmd: ipr command struct
5437 *
5438 * This function send a Set Supported Devices to the adapter
5439 *
5440 * Return value:
5441 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5442 **/
5443static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
5444{
5445 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5446 struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
5447 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5448 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5449 struct ipr_resource_entry *res = ipr_cmd->u.res;
5450
5451 ipr_cmd->job_step = ipr_ioa_reset_done;
5452
5453 list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
Brian Kinge4fbf442006-03-29 09:37:22 -06005454 if (!ipr_is_scsi_disk(res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 continue;
5456
5457 ipr_cmd->u.res = res;
5458 ipr_set_sup_dev_dflt(supp_dev, &res->cfgte.std_inq_data.vpids);
5459
5460 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5461 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5462 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5463
5464 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
5465 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
5466 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
5467
5468 ioadl->flags_and_data_len = cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST |
5469 sizeof(struct ipr_supported_device));
5470 ioadl->address = cpu_to_be32(ioa_cfg->vpd_cbs_dma +
5471 offsetof(struct ipr_misc_cbs, supp_dev));
5472 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5473 ioarcb->write_data_transfer_length =
5474 cpu_to_be32(sizeof(struct ipr_supported_device));
5475
5476 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
5477 IPR_SET_SUP_DEVICE_TIMEOUT);
5478
5479 ipr_cmd->job_step = ipr_set_supported_devs;
5480 return IPR_RC_JOB_RETURN;
5481 }
5482
5483 return IPR_RC_JOB_CONTINUE;
5484}
5485
5486/**
brking@us.ibm.com62275042005-11-01 17:01:14 -06005487 * ipr_setup_write_cache - Disable write cache if needed
5488 * @ipr_cmd: ipr command struct
5489 *
5490 * This function sets up adapters write cache to desired setting
5491 *
5492 * Return value:
5493 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5494 **/
5495static int ipr_setup_write_cache(struct ipr_cmnd *ipr_cmd)
5496{
5497 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5498
5499 ipr_cmd->job_step = ipr_set_supported_devs;
5500 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
5501 struct ipr_resource_entry, queue);
5502
5503 if (ioa_cfg->cache_state != CACHE_DISABLED)
5504 return IPR_RC_JOB_CONTINUE;
5505
5506 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5507 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5508 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
5509 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
5510
5511 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5512
5513 return IPR_RC_JOB_RETURN;
5514}
5515
5516/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 * ipr_get_mode_page - Locate specified mode page
5518 * @mode_pages: mode page buffer
5519 * @page_code: page code to find
5520 * @len: minimum required length for mode page
5521 *
5522 * Return value:
5523 * pointer to mode page / NULL on failure
5524 **/
5525static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
5526 u32 page_code, u32 len)
5527{
5528 struct ipr_mode_page_hdr *mode_hdr;
5529 u32 page_length;
5530 u32 length;
5531
5532 if (!mode_pages || (mode_pages->hdr.length == 0))
5533 return NULL;
5534
5535 length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
5536 mode_hdr = (struct ipr_mode_page_hdr *)
5537 (mode_pages->data + mode_pages->hdr.block_desc_len);
5538
5539 while (length) {
5540 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
5541 if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
5542 return mode_hdr;
5543 break;
5544 } else {
5545 page_length = (sizeof(struct ipr_mode_page_hdr) +
5546 mode_hdr->page_length);
5547 length -= page_length;
5548 mode_hdr = (struct ipr_mode_page_hdr *)
5549 ((unsigned long)mode_hdr + page_length);
5550 }
5551 }
5552 return NULL;
5553}
5554
5555/**
5556 * ipr_check_term_power - Check for term power errors
5557 * @ioa_cfg: ioa config struct
5558 * @mode_pages: IOAFP mode pages buffer
5559 *
5560 * Check the IOAFP's mode page 28 for term power errors
5561 *
5562 * Return value:
5563 * nothing
5564 **/
5565static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
5566 struct ipr_mode_pages *mode_pages)
5567{
5568 int i;
5569 int entry_length;
5570 struct ipr_dev_bus_entry *bus;
5571 struct ipr_mode_page28 *mode_page;
5572
5573 mode_page = ipr_get_mode_page(mode_pages, 0x28,
5574 sizeof(struct ipr_mode_page28));
5575
5576 entry_length = mode_page->entry_length;
5577
5578 bus = mode_page->bus;
5579
5580 for (i = 0; i < mode_page->num_entries; i++) {
5581 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
5582 dev_err(&ioa_cfg->pdev->dev,
5583 "Term power is absent on scsi bus %d\n",
5584 bus->res_addr.bus);
5585 }
5586
5587 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
5588 }
5589}
5590
5591/**
5592 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
5593 * @ioa_cfg: ioa config struct
5594 *
5595 * Looks through the config table checking for SES devices. If
5596 * the SES device is in the SES table indicating a maximum SCSI
5597 * bus speed, the speed is limited for the bus.
5598 *
5599 * Return value:
5600 * none
5601 **/
5602static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
5603{
5604 u32 max_xfer_rate;
5605 int i;
5606
5607 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
5608 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
5609 ioa_cfg->bus_attr[i].bus_width);
5610
5611 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
5612 ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
5613 }
5614}
5615
5616/**
5617 * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
5618 * @ioa_cfg: ioa config struct
5619 * @mode_pages: mode page 28 buffer
5620 *
5621 * Updates mode page 28 based on driver configuration
5622 *
5623 * Return value:
5624 * none
5625 **/
5626static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
5627 struct ipr_mode_pages *mode_pages)
5628{
5629 int i, entry_length;
5630 struct ipr_dev_bus_entry *bus;
5631 struct ipr_bus_attributes *bus_attr;
5632 struct ipr_mode_page28 *mode_page;
5633
5634 mode_page = ipr_get_mode_page(mode_pages, 0x28,
5635 sizeof(struct ipr_mode_page28));
5636
5637 entry_length = mode_page->entry_length;
5638
5639 /* Loop for each device bus entry */
5640 for (i = 0, bus = mode_page->bus;
5641 i < mode_page->num_entries;
5642 i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
5643 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
5644 dev_err(&ioa_cfg->pdev->dev,
5645 "Invalid resource address reported: 0x%08X\n",
5646 IPR_GET_PHYS_LOC(bus->res_addr));
5647 continue;
5648 }
5649
5650 bus_attr = &ioa_cfg->bus_attr[i];
5651 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
5652 bus->bus_width = bus_attr->bus_width;
5653 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
5654 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
5655 if (bus_attr->qas_enabled)
5656 bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
5657 else
5658 bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
5659 }
5660}
5661
5662/**
5663 * ipr_build_mode_select - Build a mode select command
5664 * @ipr_cmd: ipr command struct
5665 * @res_handle: resource handle to send command to
5666 * @parm: Byte 2 of Mode Sense command
5667 * @dma_addr: DMA buffer address
5668 * @xfer_len: data transfer length
5669 *
5670 * Return value:
5671 * none
5672 **/
5673static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
5674 __be32 res_handle, u8 parm, u32 dma_addr,
5675 u8 xfer_len)
5676{
5677 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5678 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5679
5680 ioarcb->res_handle = res_handle;
5681 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5682 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5683 ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
5684 ioarcb->cmd_pkt.cdb[1] = parm;
5685 ioarcb->cmd_pkt.cdb[4] = xfer_len;
5686
5687 ioadl->flags_and_data_len =
5688 cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST | xfer_len);
5689 ioadl->address = cpu_to_be32(dma_addr);
5690 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5691 ioarcb->write_data_transfer_length = cpu_to_be32(xfer_len);
5692}
5693
5694/**
5695 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
5696 * @ipr_cmd: ipr command struct
5697 *
5698 * This function sets up the SCSI bus attributes and sends
5699 * a Mode Select for Page 28 to activate them.
5700 *
5701 * Return value:
5702 * IPR_RC_JOB_RETURN
5703 **/
5704static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
5705{
5706 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5707 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
5708 int length;
5709
5710 ENTER;
Brian King47338042006-02-08 20:57:42 -06005711 ipr_scsi_bus_speed_limit(ioa_cfg);
5712 ipr_check_term_power(ioa_cfg, mode_pages);
5713 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
5714 length = mode_pages->hdr.length + 1;
5715 mode_pages->hdr.length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
5717 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
5718 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
5719 length);
5720
brking@us.ibm.com62275042005-11-01 17:01:14 -06005721 ipr_cmd->job_step = ipr_setup_write_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5723
5724 LEAVE;
5725 return IPR_RC_JOB_RETURN;
5726}
5727
5728/**
5729 * ipr_build_mode_sense - Builds a mode sense command
5730 * @ipr_cmd: ipr command struct
5731 * @res: resource entry struct
5732 * @parm: Byte 2 of mode sense command
5733 * @dma_addr: DMA address of mode sense buffer
5734 * @xfer_len: Size of DMA buffer
5735 *
5736 * Return value:
5737 * none
5738 **/
5739static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
5740 __be32 res_handle,
5741 u8 parm, u32 dma_addr, u8 xfer_len)
5742{
5743 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
5744 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5745
5746 ioarcb->res_handle = res_handle;
5747 ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
5748 ioarcb->cmd_pkt.cdb[2] = parm;
5749 ioarcb->cmd_pkt.cdb[4] = xfer_len;
5750 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5751
5752 ioadl->flags_and_data_len =
5753 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
5754 ioadl->address = cpu_to_be32(dma_addr);
5755 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
5756 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
5757}
5758
5759/**
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06005760 * ipr_reset_cmd_failed - Handle failure of IOA reset command
5761 * @ipr_cmd: ipr command struct
5762 *
5763 * This function handles the failure of an IOA bringup command.
5764 *
5765 * Return value:
5766 * IPR_RC_JOB_RETURN
5767 **/
5768static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
5769{
5770 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5771 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5772
5773 dev_err(&ioa_cfg->pdev->dev,
5774 "0x%02X failed with IOASC: 0x%08X\n",
5775 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
5776
5777 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5778 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5779 return IPR_RC_JOB_RETURN;
5780}
5781
5782/**
5783 * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
5784 * @ipr_cmd: ipr command struct
5785 *
5786 * This function handles the failure of a Mode Sense to the IOAFP.
5787 * Some adapters do not handle all mode pages.
5788 *
5789 * Return value:
5790 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5791 **/
5792static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
5793{
5794 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5795
5796 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
5797 ipr_cmd->job_step = ipr_setup_write_cache;
5798 return IPR_RC_JOB_CONTINUE;
5799 }
5800
5801 return ipr_reset_cmd_failed(ipr_cmd);
5802}
5803
5804/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
5806 * @ipr_cmd: ipr command struct
5807 *
5808 * This function send a Page 28 mode sense to the IOA to
5809 * retrieve SCSI bus attributes.
5810 *
5811 * Return value:
5812 * IPR_RC_JOB_RETURN
5813 **/
5814static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
5815{
5816 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5817
5818 ENTER;
5819 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
5820 0x28, ioa_cfg->vpd_cbs_dma +
5821 offsetof(struct ipr_misc_cbs, mode_pages),
5822 sizeof(struct ipr_mode_pages));
5823
5824 ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06005825 ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
5827 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5828
5829 LEAVE;
5830 return IPR_RC_JOB_RETURN;
5831}
5832
5833/**
Brian Kingac09c342007-04-26 16:00:16 -05005834 * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
5835 * @ipr_cmd: ipr command struct
5836 *
5837 * This function enables dual IOA RAID support if possible.
5838 *
5839 * Return value:
5840 * IPR_RC_JOB_RETURN
5841 **/
5842static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
5843{
5844 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5845 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
5846 struct ipr_mode_page24 *mode_page;
5847 int length;
5848
5849 ENTER;
5850 mode_page = ipr_get_mode_page(mode_pages, 0x24,
5851 sizeof(struct ipr_mode_page24));
5852
5853 if (mode_page)
5854 mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;
5855
5856 length = mode_pages->hdr.length + 1;
5857 mode_pages->hdr.length = 0;
5858
5859 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
5860 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
5861 length);
5862
5863 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
5864 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5865
5866 LEAVE;
5867 return IPR_RC_JOB_RETURN;
5868}
5869
5870/**
5871 * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
5872 * @ipr_cmd: ipr command struct
5873 *
5874 * This function handles the failure of a Mode Sense to the IOAFP.
5875 * Some adapters do not handle all mode pages.
5876 *
5877 * Return value:
5878 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5879 **/
5880static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
5881{
5882 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5883
5884 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
5885 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
5886 return IPR_RC_JOB_CONTINUE;
5887 }
5888
5889 return ipr_reset_cmd_failed(ipr_cmd);
5890}
5891
5892/**
5893 * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
5894 * @ipr_cmd: ipr command struct
5895 *
5896 * This function send a mode sense to the IOA to retrieve
5897 * the IOA Advanced Function Control mode page.
5898 *
5899 * Return value:
5900 * IPR_RC_JOB_RETURN
5901 **/
5902static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
5903{
5904 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5905
5906 ENTER;
5907 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
5908 0x24, ioa_cfg->vpd_cbs_dma +
5909 offsetof(struct ipr_misc_cbs, mode_pages),
5910 sizeof(struct ipr_mode_pages));
5911
5912 ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
5913 ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;
5914
5915 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
5916
5917 LEAVE;
5918 return IPR_RC_JOB_RETURN;
5919}
5920
5921/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 * ipr_init_res_table - Initialize the resource table
5923 * @ipr_cmd: ipr command struct
5924 *
5925 * This function looks through the existing resource table, comparing
5926 * it with the config table. This function will take care of old/new
5927 * devices and schedule adding/removing them from the mid-layer
5928 * as appropriate.
5929 *
5930 * Return value:
5931 * IPR_RC_JOB_CONTINUE
5932 **/
5933static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
5934{
5935 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5936 struct ipr_resource_entry *res, *temp;
5937 struct ipr_config_table_entry *cfgte;
5938 int found, i;
5939 LIST_HEAD(old_res);
5940
5941 ENTER;
5942 if (ioa_cfg->cfg_table->hdr.flags & IPR_UCODE_DOWNLOAD_REQ)
5943 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
5944
5945 list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
5946 list_move_tail(&res->queue, &old_res);
5947
5948 for (i = 0; i < ioa_cfg->cfg_table->hdr.num_entries; i++) {
5949 cfgte = &ioa_cfg->cfg_table->dev[i];
5950 found = 0;
5951
5952 list_for_each_entry_safe(res, temp, &old_res, queue) {
5953 if (!memcmp(&res->cfgte.res_addr,
5954 &cfgte->res_addr, sizeof(cfgte->res_addr))) {
5955 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5956 found = 1;
5957 break;
5958 }
5959 }
5960
5961 if (!found) {
5962 if (list_empty(&ioa_cfg->free_res_q)) {
5963 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
5964 break;
5965 }
5966
5967 found = 1;
5968 res = list_entry(ioa_cfg->free_res_q.next,
5969 struct ipr_resource_entry, queue);
5970 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5971 ipr_init_res_entry(res);
5972 res->add_to_ml = 1;
5973 }
5974
5975 if (found)
5976 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
5977 }
5978
5979 list_for_each_entry_safe(res, temp, &old_res, queue) {
5980 if (res->sdev) {
5981 res->del_from_ml = 1;
Brian King1121b792006-03-29 09:37:16 -06005982 res->cfgte.res_handle = IPR_INVALID_RES_HANDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
5984 } else {
5985 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
5986 }
5987 }
5988
Brian Kingac09c342007-04-26 16:00:16 -05005989 if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
5990 ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
5991 else
5992 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
5994 LEAVE;
5995 return IPR_RC_JOB_CONTINUE;
5996}
5997
5998/**
5999 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
6000 * @ipr_cmd: ipr command struct
6001 *
6002 * This function sends a Query IOA Configuration command
6003 * to the adapter to retrieve the IOA configuration table.
6004 *
6005 * Return value:
6006 * IPR_RC_JOB_RETURN
6007 **/
6008static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
6009{
6010 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6011 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6012 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
6013 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
Brian Kingac09c342007-04-26 16:00:16 -05006014 struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
6016 ENTER;
Brian Kingac09c342007-04-26 16:00:16 -05006017 if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
6018 ioa_cfg->dual_raid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
6020 ucode_vpd->major_release, ucode_vpd->card_type,
6021 ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
6022 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6023 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6024
6025 ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
6026 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_config_table) >> 8) & 0xff;
6027 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_config_table) & 0xff;
6028
6029 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
6030 ioarcb->read_data_transfer_length =
6031 cpu_to_be32(sizeof(struct ipr_config_table));
6032
6033 ioadl->address = cpu_to_be32(ioa_cfg->cfg_table_dma);
6034 ioadl->flags_and_data_len =
6035 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(struct ipr_config_table));
6036
6037 ipr_cmd->job_step = ipr_init_res_table;
6038
6039 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6040
6041 LEAVE;
6042 return IPR_RC_JOB_RETURN;
6043}
6044
6045/**
6046 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
6047 * @ipr_cmd: ipr command struct
6048 *
6049 * This utility function sends an inquiry to the adapter.
6050 *
6051 * Return value:
6052 * none
6053 **/
6054static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
6055 u32 dma_addr, u8 xfer_len)
6056{
6057 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6058 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
6059
6060 ENTER;
6061 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6062 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6063
6064 ioarcb->cmd_pkt.cdb[0] = INQUIRY;
6065 ioarcb->cmd_pkt.cdb[1] = flags;
6066 ioarcb->cmd_pkt.cdb[2] = page;
6067 ioarcb->cmd_pkt.cdb[4] = xfer_len;
6068
6069 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
6070 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
6071
6072 ioadl->address = cpu_to_be32(dma_addr);
6073 ioadl->flags_and_data_len =
6074 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
6075
6076 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6077 LEAVE;
6078}
6079
6080/**
brking@us.ibm.com62275042005-11-01 17:01:14 -06006081 * ipr_inquiry_page_supported - Is the given inquiry page supported
6082 * @page0: inquiry page 0 buffer
6083 * @page: page code.
6084 *
6085 * This function determines if the specified inquiry page is supported.
6086 *
6087 * Return value:
6088 * 1 if page is supported / 0 if not
6089 **/
6090static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
6091{
6092 int i;
6093
6094 for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
6095 if (page0->page[i] == page)
6096 return 1;
6097
6098 return 0;
6099}
6100
6101/**
Brian Kingac09c342007-04-26 16:00:16 -05006102 * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
6103 * @ipr_cmd: ipr command struct
6104 *
6105 * This function sends a Page 0xD0 inquiry to the adapter
6106 * to retrieve adapter capabilities.
6107 *
6108 * Return value:
6109 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6110 **/
6111static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
6112{
6113 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6114 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
6115 struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
6116
6117 ENTER;
6118 ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
6119 memset(cap, 0, sizeof(*cap));
6120
6121 if (ipr_inquiry_page_supported(page0, 0xD0)) {
6122 ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
6123 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
6124 sizeof(struct ipr_inquiry_cap));
6125 return IPR_RC_JOB_RETURN;
6126 }
6127
6128 LEAVE;
6129 return IPR_RC_JOB_CONTINUE;
6130}
6131
6132/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
6134 * @ipr_cmd: ipr command struct
6135 *
6136 * This function sends a Page 3 inquiry to the adapter
6137 * to retrieve software VPD information.
6138 *
6139 * Return value:
6140 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6141 **/
6142static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
6143{
6144 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006145 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
6146
6147 ENTER;
6148
6149 if (!ipr_inquiry_page_supported(page0, 1))
6150 ioa_cfg->cache_state = CACHE_NONE;
6151
Brian Kingac09c342007-04-26 16:00:16 -05006152 ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006153
6154 ipr_ioafp_inquiry(ipr_cmd, 1, 3,
6155 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
6156 sizeof(struct ipr_inquiry_page3));
6157
6158 LEAVE;
6159 return IPR_RC_JOB_RETURN;
6160}
6161
6162/**
6163 * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
6164 * @ipr_cmd: ipr command struct
6165 *
6166 * This function sends a Page 0 inquiry to the adapter
6167 * to retrieve supported inquiry pages.
6168 *
6169 * Return value:
6170 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6171 **/
6172static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
6173{
6174 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 char type[5];
6176
6177 ENTER;
6178
6179 /* Grab the type out of the VPD and store it away */
6180 memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
6181 type[4] = '\0';
6182 ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
6183
brking@us.ibm.com62275042005-11-01 17:01:14 -06006184 ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185
brking@us.ibm.com62275042005-11-01 17:01:14 -06006186 ipr_ioafp_inquiry(ipr_cmd, 1, 0,
6187 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
6188 sizeof(struct ipr_inquiry_page0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
6190 LEAVE;
6191 return IPR_RC_JOB_RETURN;
6192}
6193
6194/**
6195 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
6196 * @ipr_cmd: ipr command struct
6197 *
6198 * This function sends a standard inquiry to the adapter.
6199 *
6200 * Return value:
6201 * IPR_RC_JOB_RETURN
6202 **/
6203static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
6204{
6205 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6206
6207 ENTER;
brking@us.ibm.com62275042005-11-01 17:01:14 -06006208 ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
6210 ipr_ioafp_inquiry(ipr_cmd, 0, 0,
6211 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
6212 sizeof(struct ipr_ioa_vpd));
6213
6214 LEAVE;
6215 return IPR_RC_JOB_RETURN;
6216}
6217
6218/**
6219 * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
6220 * @ipr_cmd: ipr command struct
6221 *
6222 * This function send an Identify Host Request Response Queue
6223 * command to establish the HRRQ with the adapter.
6224 *
6225 * Return value:
6226 * IPR_RC_JOB_RETURN
6227 **/
6228static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
6229{
6230 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6231 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6232
6233 ENTER;
6234 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
6235
6236 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
6237 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6238
6239 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6240 ioarcb->cmd_pkt.cdb[2] =
6241 ((u32) ioa_cfg->host_rrq_dma >> 24) & 0xff;
6242 ioarcb->cmd_pkt.cdb[3] =
6243 ((u32) ioa_cfg->host_rrq_dma >> 16) & 0xff;
6244 ioarcb->cmd_pkt.cdb[4] =
6245 ((u32) ioa_cfg->host_rrq_dma >> 8) & 0xff;
6246 ioarcb->cmd_pkt.cdb[5] =
6247 ((u32) ioa_cfg->host_rrq_dma) & 0xff;
6248 ioarcb->cmd_pkt.cdb[7] =
6249 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
6250 ioarcb->cmd_pkt.cdb[8] =
6251 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
6252
6253 ipr_cmd->job_step = ipr_ioafp_std_inquiry;
6254
6255 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6256
6257 LEAVE;
6258 return IPR_RC_JOB_RETURN;
6259}
6260
6261/**
6262 * ipr_reset_timer_done - Adapter reset timer function
6263 * @ipr_cmd: ipr command struct
6264 *
6265 * Description: This function is used in adapter reset processing
6266 * for timing events. If the reset_cmd pointer in the IOA
6267 * config struct is not this adapter's we are doing nested
6268 * resets and fail_all_ops will take care of freeing the
6269 * command block.
6270 *
6271 * Return value:
6272 * none
6273 **/
6274static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
6275{
6276 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6277 unsigned long lock_flags = 0;
6278
6279 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6280
6281 if (ioa_cfg->reset_cmd == ipr_cmd) {
6282 list_del(&ipr_cmd->queue);
6283 ipr_cmd->done(ipr_cmd);
6284 }
6285
6286 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6287}
6288
6289/**
6290 * ipr_reset_start_timer - Start a timer for adapter reset job
6291 * @ipr_cmd: ipr command struct
6292 * @timeout: timeout value
6293 *
6294 * Description: This function is used in adapter reset processing
6295 * for timing events. If the reset_cmd pointer in the IOA
6296 * config struct is not this adapter's we are doing nested
6297 * resets and fail_all_ops will take care of freeing the
6298 * command block.
6299 *
6300 * Return value:
6301 * none
6302 **/
6303static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
6304 unsigned long timeout)
6305{
6306 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
6307 ipr_cmd->done = ipr_reset_ioa_job;
6308
6309 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
6310 ipr_cmd->timer.expires = jiffies + timeout;
6311 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
6312 add_timer(&ipr_cmd->timer);
6313}
6314
6315/**
6316 * ipr_init_ioa_mem - Initialize ioa_cfg control block
6317 * @ioa_cfg: ioa cfg struct
6318 *
6319 * Return value:
6320 * nothing
6321 **/
6322static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
6323{
6324 memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
6325
6326 /* Initialize Host RRQ pointers */
6327 ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
6328 ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
6329 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
6330 ioa_cfg->toggle_bit = 1;
6331
6332 /* Zero out config table */
6333 memset(ioa_cfg->cfg_table, 0, sizeof(struct ipr_config_table));
6334}
6335
6336/**
6337 * ipr_reset_enable_ioa - Enable the IOA following a reset.
6338 * @ipr_cmd: ipr command struct
6339 *
6340 * This function reinitializes some control blocks and
6341 * enables destructive diagnostics on the adapter.
6342 *
6343 * Return value:
6344 * IPR_RC_JOB_RETURN
6345 **/
6346static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
6347{
6348 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6349 volatile u32 int_reg;
6350
6351 ENTER;
6352 ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
6353 ipr_init_ioa_mem(ioa_cfg);
6354
6355 ioa_cfg->allow_interrupts = 1;
6356 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
6357
6358 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
6359 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
6360 ioa_cfg->regs.clr_interrupt_mask_reg);
6361 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
6362 return IPR_RC_JOB_CONTINUE;
6363 }
6364
6365 /* Enable destructive diagnostics on IOA */
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06006366 writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367
6368 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
6369 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
6370
6371 dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
6372
6373 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
Brian King5469cb52007-03-29 12:42:40 -05006374 ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
6376 ipr_cmd->done = ipr_reset_ioa_job;
6377 add_timer(&ipr_cmd->timer);
6378 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
6379
6380 LEAVE;
6381 return IPR_RC_JOB_RETURN;
6382}
6383
6384/**
6385 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
6386 * @ipr_cmd: ipr command struct
6387 *
6388 * This function is invoked when an adapter dump has run out
6389 * of processing time.
6390 *
6391 * Return value:
6392 * IPR_RC_JOB_CONTINUE
6393 **/
6394static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
6395{
6396 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6397
6398 if (ioa_cfg->sdt_state == GET_DUMP)
6399 ioa_cfg->sdt_state = ABORT_DUMP;
6400
6401 ipr_cmd->job_step = ipr_reset_alert;
6402
6403 return IPR_RC_JOB_CONTINUE;
6404}
6405
6406/**
6407 * ipr_unit_check_no_data - Log a unit check/no data error log
6408 * @ioa_cfg: ioa config struct
6409 *
6410 * Logs an error indicating the adapter unit checked, but for some
6411 * reason, we were unable to fetch the unit check buffer.
6412 *
6413 * Return value:
6414 * nothing
6415 **/
6416static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
6417{
6418 ioa_cfg->errors_logged++;
6419 dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
6420}
6421
6422/**
6423 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
6424 * @ioa_cfg: ioa config struct
6425 *
6426 * Fetches the unit check buffer from the adapter by clocking the data
6427 * through the mailbox register.
6428 *
6429 * Return value:
6430 * nothing
6431 **/
6432static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
6433{
6434 unsigned long mailbox;
6435 struct ipr_hostrcb *hostrcb;
6436 struct ipr_uc_sdt sdt;
6437 int rc, length;
Brian King65f56472007-04-26 16:00:12 -05006438 u32 ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439
6440 mailbox = readl(ioa_cfg->ioa_mailbox);
6441
6442 if (!ipr_sdt_is_fmt2(mailbox)) {
6443 ipr_unit_check_no_data(ioa_cfg);
6444 return;
6445 }
6446
6447 memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
6448 rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
6449 (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
6450
6451 if (rc || (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE) ||
6452 !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY)) {
6453 ipr_unit_check_no_data(ioa_cfg);
6454 return;
6455 }
6456
6457 /* Find length of the first sdt entry (UC buffer) */
6458 length = (be32_to_cpu(sdt.entry[0].end_offset) -
6459 be32_to_cpu(sdt.entry[0].bar_str_offset)) & IPR_FMT2_MBX_ADDR_MASK;
6460
6461 hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
6462 struct ipr_hostrcb, queue);
6463 list_del(&hostrcb->queue);
6464 memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
6465
6466 rc = ipr_get_ldump_data_section(ioa_cfg,
6467 be32_to_cpu(sdt.entry[0].bar_str_offset),
6468 (__be32 *)&hostrcb->hcam,
6469 min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
6470
Brian King65f56472007-04-26 16:00:12 -05006471 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 ipr_handle_log_data(ioa_cfg, hostrcb);
Brian King65f56472007-04-26 16:00:12 -05006473 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
6474 if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
6475 ioa_cfg->sdt_state == GET_DUMP)
6476 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
6477 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 ipr_unit_check_no_data(ioa_cfg);
6479
6480 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
6481}
6482
6483/**
6484 * ipr_reset_restore_cfg_space - Restore PCI config space.
6485 * @ipr_cmd: ipr command struct
6486 *
6487 * Description: This function restores the saved PCI config space of
6488 * the adapter, fails all outstanding ops back to the callers, and
6489 * fetches the dump/unit check if applicable to this reset.
6490 *
6491 * Return value:
6492 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6493 **/
6494static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
6495{
6496 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6497 int rc;
6498
6499 ENTER;
6500 rc = pci_restore_state(ioa_cfg->pdev);
6501
6502 if (rc != PCIBIOS_SUCCESSFUL) {
6503 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6504 return IPR_RC_JOB_CONTINUE;
6505 }
6506
6507 if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
6508 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6509 return IPR_RC_JOB_CONTINUE;
6510 }
6511
6512 ipr_fail_all_ops(ioa_cfg);
6513
6514 if (ioa_cfg->ioa_unit_checked) {
6515 ioa_cfg->ioa_unit_checked = 0;
6516 ipr_get_unit_check_buffer(ioa_cfg);
6517 ipr_cmd->job_step = ipr_reset_alert;
6518 ipr_reset_start_timer(ipr_cmd, 0);
6519 return IPR_RC_JOB_RETURN;
6520 }
6521
6522 if (ioa_cfg->in_ioa_bringdown) {
6523 ipr_cmd->job_step = ipr_ioa_bringdown_done;
6524 } else {
6525 ipr_cmd->job_step = ipr_reset_enable_ioa;
6526
6527 if (GET_DUMP == ioa_cfg->sdt_state) {
6528 ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
6529 ipr_cmd->job_step = ipr_reset_wait_for_dump;
6530 schedule_work(&ioa_cfg->work_q);
6531 return IPR_RC_JOB_RETURN;
6532 }
6533 }
6534
6535 ENTER;
6536 return IPR_RC_JOB_CONTINUE;
6537}
6538
6539/**
Brian Kinge619e1a2007-01-23 11:25:37 -06006540 * ipr_reset_bist_done - BIST has completed on the adapter.
6541 * @ipr_cmd: ipr command struct
6542 *
6543 * Description: Unblock config space and resume the reset process.
6544 *
6545 * Return value:
6546 * IPR_RC_JOB_CONTINUE
6547 **/
6548static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
6549{
6550 ENTER;
6551 pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
6552 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
6553 LEAVE;
6554 return IPR_RC_JOB_CONTINUE;
6555}
6556
6557/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 * ipr_reset_start_bist - Run BIST on the adapter.
6559 * @ipr_cmd: ipr command struct
6560 *
6561 * Description: This function runs BIST on the adapter, then delays 2 seconds.
6562 *
6563 * Return value:
6564 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6565 **/
6566static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
6567{
6568 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6569 int rc;
6570
6571 ENTER;
Brian Kingb30197d2005-09-27 01:21:56 -07006572 pci_block_user_cfg_access(ioa_cfg->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
6574
6575 if (rc != PCIBIOS_SUCCESSFUL) {
Brian Kinga9aedb02007-03-29 12:43:23 -05006576 pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
6578 rc = IPR_RC_JOB_CONTINUE;
6579 } else {
Brian Kinge619e1a2007-01-23 11:25:37 -06006580 ipr_cmd->job_step = ipr_reset_bist_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
6582 rc = IPR_RC_JOB_RETURN;
6583 }
6584
6585 LEAVE;
6586 return rc;
6587}
6588
6589/**
Brian King463fc692007-05-07 17:09:05 -05006590 * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
6591 * @ipr_cmd: ipr command struct
6592 *
6593 * Description: This clears PCI reset to the adapter and delays two seconds.
6594 *
6595 * Return value:
6596 * IPR_RC_JOB_RETURN
6597 **/
6598static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
6599{
6600 ENTER;
6601 pci_set_pcie_reset_state(ipr_cmd->ioa_cfg->pdev, pcie_deassert_reset);
6602 ipr_cmd->job_step = ipr_reset_bist_done;
6603 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
6604 LEAVE;
6605 return IPR_RC_JOB_RETURN;
6606}
6607
6608/**
6609 * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
6610 * @ipr_cmd: ipr command struct
6611 *
6612 * Description: This asserts PCI reset to the adapter.
6613 *
6614 * Return value:
6615 * IPR_RC_JOB_RETURN
6616 **/
6617static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
6618{
6619 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6620 struct pci_dev *pdev = ioa_cfg->pdev;
6621
6622 ENTER;
6623 pci_block_user_cfg_access(pdev);
6624 pci_set_pcie_reset_state(pdev, pcie_warm_reset);
6625 ipr_cmd->job_step = ipr_reset_slot_reset_done;
6626 ipr_reset_start_timer(ipr_cmd, IPR_PCI_RESET_TIMEOUT);
6627 LEAVE;
6628 return IPR_RC_JOB_RETURN;
6629}
6630
6631/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 * ipr_reset_allowed - Query whether or not IOA can be reset
6633 * @ioa_cfg: ioa config struct
6634 *
6635 * Return value:
6636 * 0 if reset not allowed / non-zero if reset is allowed
6637 **/
6638static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
6639{
6640 volatile u32 temp_reg;
6641
6642 temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
6643 return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
6644}
6645
6646/**
6647 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
6648 * @ipr_cmd: ipr command struct
6649 *
6650 * Description: This function waits for adapter permission to run BIST,
6651 * then runs BIST. If the adapter does not give permission after a
6652 * reasonable time, we will reset the adapter anyway. The impact of
6653 * resetting the adapter without warning the adapter is the risk of
6654 * losing the persistent error log on the adapter. If the adapter is
6655 * reset while it is writing to the flash on the adapter, the flash
6656 * segment will have bad ECC and be zeroed.
6657 *
6658 * Return value:
6659 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6660 **/
6661static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
6662{
6663 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6664 int rc = IPR_RC_JOB_RETURN;
6665
6666 if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
6667 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
6668 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
6669 } else {
Brian King463fc692007-05-07 17:09:05 -05006670 ipr_cmd->job_step = ioa_cfg->reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 rc = IPR_RC_JOB_CONTINUE;
6672 }
6673
6674 return rc;
6675}
6676
6677/**
6678 * ipr_reset_alert_part2 - Alert the adapter of a pending reset
6679 * @ipr_cmd: ipr command struct
6680 *
6681 * Description: This function alerts the adapter that it will be reset.
6682 * If memory space is not currently enabled, proceed directly
6683 * to running BIST on the adapter. The timer must always be started
6684 * so we guarantee we do not run BIST from ipr_isr.
6685 *
6686 * Return value:
6687 * IPR_RC_JOB_RETURN
6688 **/
6689static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
6690{
6691 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6692 u16 cmd_reg;
6693 int rc;
6694
6695 ENTER;
6696 rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
6697
6698 if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
6699 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
6700 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg);
6701 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
6702 } else {
Brian King463fc692007-05-07 17:09:05 -05006703 ipr_cmd->job_step = ioa_cfg->reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 }
6705
6706 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
6707 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
6708
6709 LEAVE;
6710 return IPR_RC_JOB_RETURN;
6711}
6712
6713/**
6714 * ipr_reset_ucode_download_done - Microcode download completion
6715 * @ipr_cmd: ipr command struct
6716 *
6717 * Description: This function unmaps the microcode download buffer.
6718 *
6719 * Return value:
6720 * IPR_RC_JOB_CONTINUE
6721 **/
6722static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
6723{
6724 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6725 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
6726
6727 pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
6728 sglist->num_sg, DMA_TO_DEVICE);
6729
6730 ipr_cmd->job_step = ipr_reset_alert;
6731 return IPR_RC_JOB_CONTINUE;
6732}
6733
6734/**
6735 * ipr_reset_ucode_download - Download microcode to the adapter
6736 * @ipr_cmd: ipr command struct
6737 *
6738 * Description: This function checks to see if it there is microcode
6739 * to download to the adapter. If there is, a download is performed.
6740 *
6741 * Return value:
6742 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6743 **/
6744static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
6745{
6746 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6747 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
6748
6749 ENTER;
6750 ipr_cmd->job_step = ipr_reset_alert;
6751
6752 if (!sglist)
6753 return IPR_RC_JOB_CONTINUE;
6754
6755 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6756 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6757 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
6758 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
6759 ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
6760 ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
6761 ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
6762
brking@us.ibm.com12baa422005-11-01 17:01:27 -06006763 ipr_build_ucode_ioadl(ipr_cmd, sglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 ipr_cmd->job_step = ipr_reset_ucode_download_done;
6765
6766 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
6767 IPR_WRITE_BUFFER_TIMEOUT);
6768
6769 LEAVE;
6770 return IPR_RC_JOB_RETURN;
6771}
6772
6773/**
6774 * ipr_reset_shutdown_ioa - Shutdown the adapter
6775 * @ipr_cmd: ipr command struct
6776 *
6777 * Description: This function issues an adapter shutdown of the
6778 * specified type to the specified adapter as part of the
6779 * adapter reset job.
6780 *
6781 * Return value:
6782 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6783 **/
6784static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
6785{
6786 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6787 enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
6788 unsigned long timeout;
6789 int rc = IPR_RC_JOB_CONTINUE;
6790
6791 ENTER;
6792 if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
6793 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6794 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6795 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
6796 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
6797
Brian Kingac09c342007-04-26 16:00:16 -05006798 if (shutdown_type == IPR_SHUTDOWN_NORMAL)
6799 timeout = IPR_SHUTDOWN_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
6801 timeout = IPR_INTERNAL_TIMEOUT;
Brian Kingac09c342007-04-26 16:00:16 -05006802 else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
6803 timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 else
Brian Kingac09c342007-04-26 16:00:16 -05006805 timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
6807 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
6808
6809 rc = IPR_RC_JOB_RETURN;
6810 ipr_cmd->job_step = ipr_reset_ucode_download;
6811 } else
6812 ipr_cmd->job_step = ipr_reset_alert;
6813
6814 LEAVE;
6815 return rc;
6816}
6817
6818/**
6819 * ipr_reset_ioa_job - Adapter reset job
6820 * @ipr_cmd: ipr command struct
6821 *
6822 * Description: This function is the job router for the adapter reset job.
6823 *
6824 * Return value:
6825 * none
6826 **/
6827static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
6828{
6829 u32 rc, ioasc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6831
6832 do {
6833 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
6834
6835 if (ioa_cfg->reset_cmd != ipr_cmd) {
6836 /*
6837 * We are doing nested adapter resets and this is
6838 * not the current reset job.
6839 */
6840 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6841 return;
6842 }
6843
6844 if (IPR_IOASC_SENSE_KEY(ioasc)) {
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06006845 rc = ipr_cmd->job_step_failed(ipr_cmd);
6846 if (rc == IPR_RC_JOB_RETURN)
6847 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 }
6849
6850 ipr_reinit_ipr_cmnd(ipr_cmd);
brking@us.ibm.comdfed8232005-11-01 17:02:55 -06006851 ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 rc = ipr_cmd->job_step(ipr_cmd);
6853 } while(rc == IPR_RC_JOB_CONTINUE);
6854}
6855
6856/**
6857 * _ipr_initiate_ioa_reset - Initiate an adapter reset
6858 * @ioa_cfg: ioa config struct
6859 * @job_step: first job step of reset job
6860 * @shutdown_type: shutdown type
6861 *
6862 * Description: This function will initiate the reset of the given adapter
6863 * starting at the selected job step.
6864 * If the caller needs to wait on the completion of the reset,
6865 * the caller must sleep on the reset_wait_q.
6866 *
6867 * Return value:
6868 * none
6869 **/
6870static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
6871 int (*job_step) (struct ipr_cmnd *),
6872 enum ipr_shutdown_type shutdown_type)
6873{
6874 struct ipr_cmnd *ipr_cmd;
6875
6876 ioa_cfg->in_reset_reload = 1;
6877 ioa_cfg->allow_cmds = 0;
6878 scsi_block_requests(ioa_cfg->host);
6879
6880 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
6881 ioa_cfg->reset_cmd = ipr_cmd;
6882 ipr_cmd->job_step = job_step;
6883 ipr_cmd->u.shutdown_type = shutdown_type;
6884
6885 ipr_reset_ioa_job(ipr_cmd);
6886}
6887
6888/**
6889 * ipr_initiate_ioa_reset - Initiate an adapter reset
6890 * @ioa_cfg: ioa config struct
6891 * @shutdown_type: shutdown type
6892 *
6893 * Description: This function will initiate the reset of the given adapter.
6894 * If the caller needs to wait on the completion of the reset,
6895 * the caller must sleep on the reset_wait_q.
6896 *
6897 * Return value:
6898 * none
6899 **/
6900static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
6901 enum ipr_shutdown_type shutdown_type)
6902{
6903 if (ioa_cfg->ioa_is_dead)
6904 return;
6905
6906 if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
6907 ioa_cfg->sdt_state = ABORT_DUMP;
6908
6909 if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
6910 dev_err(&ioa_cfg->pdev->dev,
6911 "IOA taken offline - error recovery failed\n");
6912
6913 ioa_cfg->reset_retries = 0;
6914 ioa_cfg->ioa_is_dead = 1;
6915
6916 if (ioa_cfg->in_ioa_bringdown) {
6917 ioa_cfg->reset_cmd = NULL;
6918 ioa_cfg->in_reset_reload = 0;
6919 ipr_fail_all_ops(ioa_cfg);
6920 wake_up_all(&ioa_cfg->reset_wait_q);
6921
6922 spin_unlock_irq(ioa_cfg->host->host_lock);
6923 scsi_unblock_requests(ioa_cfg->host);
6924 spin_lock_irq(ioa_cfg->host->host_lock);
6925 return;
6926 } else {
6927 ioa_cfg->in_ioa_bringdown = 1;
6928 shutdown_type = IPR_SHUTDOWN_NONE;
6929 }
6930 }
6931
6932 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
6933 shutdown_type);
6934}
6935
6936/**
Linas Vepstasf8a88b192006-02-03 16:52:42 -06006937 * ipr_reset_freeze - Hold off all I/O activity
6938 * @ipr_cmd: ipr command struct
6939 *
6940 * Description: If the PCI slot is frozen, hold off all I/O
6941 * activity; then, as soon as the slot is available again,
6942 * initiate an adapter reset.
6943 */
6944static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
6945{
6946 /* Disallow new interrupts, avoid loop */
6947 ipr_cmd->ioa_cfg->allow_interrupts = 0;
6948 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
6949 ipr_cmd->done = ipr_reset_ioa_job;
6950 return IPR_RC_JOB_RETURN;
6951}
6952
6953/**
6954 * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
6955 * @pdev: PCI device struct
6956 *
6957 * Description: This routine is called to tell us that the PCI bus
6958 * is down. Can't do anything here, except put the device driver
6959 * into a holding pattern, waiting for the PCI bus to come back.
6960 */
6961static void ipr_pci_frozen(struct pci_dev *pdev)
6962{
6963 unsigned long flags = 0;
6964 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6965
6966 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6967 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
6968 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6969}
6970
6971/**
6972 * ipr_pci_slot_reset - Called when PCI slot has been reset.
6973 * @pdev: PCI device struct
6974 *
6975 * Description: This routine is called by the pci error recovery
6976 * code after the PCI slot has been reset, just before we
6977 * should resume normal operations.
6978 */
6979static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
6980{
6981 unsigned long flags = 0;
6982 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6983
6984 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
Brian King463fc692007-05-07 17:09:05 -05006985 if (ioa_cfg->needs_warm_reset)
6986 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
6987 else
6988 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
6989 IPR_SHUTDOWN_NONE);
Linas Vepstasf8a88b192006-02-03 16:52:42 -06006990 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6991 return PCI_ERS_RESULT_RECOVERED;
6992}
6993
6994/**
6995 * ipr_pci_perm_failure - Called when PCI slot is dead for good.
6996 * @pdev: PCI device struct
6997 *
6998 * Description: This routine is called when the PCI bus has
6999 * permanently failed.
7000 */
7001static void ipr_pci_perm_failure(struct pci_dev *pdev)
7002{
7003 unsigned long flags = 0;
7004 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7005
7006 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
7007 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
7008 ioa_cfg->sdt_state = ABORT_DUMP;
7009 ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES;
7010 ioa_cfg->in_ioa_bringdown = 1;
Kleber S. Souza6ff63892009-05-04 10:41:02 -03007011 ioa_cfg->allow_cmds = 0;
Linas Vepstasf8a88b192006-02-03 16:52:42 -06007012 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7013 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
7014}
7015
7016/**
7017 * ipr_pci_error_detected - Called when a PCI error is detected.
7018 * @pdev: PCI device struct
7019 * @state: PCI channel state
7020 *
7021 * Description: Called when a PCI error is detected.
7022 *
7023 * Return value:
7024 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7025 */
7026static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
7027 pci_channel_state_t state)
7028{
7029 switch (state) {
7030 case pci_channel_io_frozen:
7031 ipr_pci_frozen(pdev);
7032 return PCI_ERS_RESULT_NEED_RESET;
7033 case pci_channel_io_perm_failure:
7034 ipr_pci_perm_failure(pdev);
7035 return PCI_ERS_RESULT_DISCONNECT;
7036 break;
7037 default:
7038 break;
7039 }
7040 return PCI_ERS_RESULT_NEED_RESET;
7041}
7042
7043/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
7045 * @ioa_cfg: ioa cfg struct
7046 *
7047 * Description: This is the second phase of adapter intialization
7048 * This function takes care of initilizing the adapter to the point
7049 * where it can accept new commands.
7050
7051 * Return value:
Joe Perchesb1c11812008-02-03 17:28:22 +02007052 * 0 on success / -EIO on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053 **/
7054static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
7055{
7056 int rc = 0;
7057 unsigned long host_lock_flags = 0;
7058
7059 ENTER;
7060 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7061 dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007062 if (ioa_cfg->needs_hard_reset) {
7063 ioa_cfg->needs_hard_reset = 0;
7064 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7065 } else
7066 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
7067 IPR_SHUTDOWN_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068
7069 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7070 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7071 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7072
7073 if (ioa_cfg->ioa_is_dead) {
7074 rc = -EIO;
7075 } else if (ipr_invalid_adapter(ioa_cfg)) {
7076 if (!ipr_testmode)
7077 rc = -EIO;
7078
7079 dev_err(&ioa_cfg->pdev->dev,
7080 "Adapter not supported in this hardware configuration.\n");
7081 }
7082
7083 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7084
7085 LEAVE;
7086 return rc;
7087}
7088
7089/**
7090 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
7091 * @ioa_cfg: ioa config struct
7092 *
7093 * Return value:
7094 * none
7095 **/
7096static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
7097{
7098 int i;
7099
7100 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
7101 if (ioa_cfg->ipr_cmnd_list[i])
7102 pci_pool_free(ioa_cfg->ipr_cmd_pool,
7103 ioa_cfg->ipr_cmnd_list[i],
7104 ioa_cfg->ipr_cmnd_list_dma[i]);
7105
7106 ioa_cfg->ipr_cmnd_list[i] = NULL;
7107 }
7108
7109 if (ioa_cfg->ipr_cmd_pool)
7110 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
7111
7112 ioa_cfg->ipr_cmd_pool = NULL;
7113}
7114
7115/**
7116 * ipr_free_mem - Frees memory allocated for an adapter
7117 * @ioa_cfg: ioa cfg struct
7118 *
7119 * Return value:
7120 * nothing
7121 **/
7122static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
7123{
7124 int i;
7125
7126 kfree(ioa_cfg->res_entries);
7127 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
7128 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
7129 ipr_free_cmd_blks(ioa_cfg);
7130 pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
7131 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
7132 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_config_table),
7133 ioa_cfg->cfg_table,
7134 ioa_cfg->cfg_table_dma);
7135
7136 for (i = 0; i < IPR_NUM_HCAMS; i++) {
7137 pci_free_consistent(ioa_cfg->pdev,
7138 sizeof(struct ipr_hostrcb),
7139 ioa_cfg->hostrcb[i],
7140 ioa_cfg->hostrcb_dma[i]);
7141 }
7142
7143 ipr_free_dump(ioa_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 kfree(ioa_cfg->trace);
7145}
7146
7147/**
7148 * ipr_free_all_resources - Free all allocated resources for an adapter.
7149 * @ipr_cmd: ipr command struct
7150 *
7151 * This function frees all allocated resources for the
7152 * specified adapter.
7153 *
7154 * Return value:
7155 * none
7156 **/
7157static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
7158{
7159 struct pci_dev *pdev = ioa_cfg->pdev;
7160
7161 ENTER;
7162 free_irq(pdev->irq, ioa_cfg);
Wayne Boyer5a9ef252009-01-23 09:17:35 -08007163 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 iounmap(ioa_cfg->hdw_dma_regs);
7165 pci_release_regions(pdev);
7166 ipr_free_mem(ioa_cfg);
7167 scsi_host_put(ioa_cfg->host);
7168 pci_disable_device(pdev);
7169 LEAVE;
7170}
7171
7172/**
7173 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
7174 * @ioa_cfg: ioa config struct
7175 *
7176 * Return value:
7177 * 0 on success / -ENOMEM on allocation failure
7178 **/
7179static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
7180{
7181 struct ipr_cmnd *ipr_cmd;
7182 struct ipr_ioarcb *ioarcb;
7183 dma_addr_t dma_addr;
7184 int i;
7185
7186 ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
7187 sizeof(struct ipr_cmnd), 8, 0);
7188
7189 if (!ioa_cfg->ipr_cmd_pool)
7190 return -ENOMEM;
7191
7192 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
Christoph Lametere94b1762006-12-06 20:33:17 -08007193 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194
7195 if (!ipr_cmd) {
7196 ipr_free_cmd_blks(ioa_cfg);
7197 return -ENOMEM;
7198 }
7199
7200 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
7201 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
7202 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
7203
7204 ioarcb = &ipr_cmd->ioarcb;
7205 ioarcb->ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
7206 ioarcb->host_response_handle = cpu_to_be32(i << 2);
7207 ioarcb->write_ioadl_addr =
7208 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
7209 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
7210 ioarcb->ioasa_host_pci_addr =
7211 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
7212 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
7213 ipr_cmd->cmd_index = i;
7214 ipr_cmd->ioa_cfg = ioa_cfg;
7215 ipr_cmd->sense_buffer_dma = dma_addr +
7216 offsetof(struct ipr_cmnd, sense_buffer);
7217
7218 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
7219 }
7220
7221 return 0;
7222}
7223
7224/**
7225 * ipr_alloc_mem - Allocate memory for an adapter
7226 * @ioa_cfg: ioa config struct
7227 *
7228 * Return value:
7229 * 0 on success / non-zero for error
7230 **/
7231static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
7232{
7233 struct pci_dev *pdev = ioa_cfg->pdev;
7234 int i, rc = -ENOMEM;
7235
7236 ENTER;
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06007237 ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 IPR_MAX_PHYSICAL_DEVS, GFP_KERNEL);
7239
7240 if (!ioa_cfg->res_entries)
7241 goto out;
7242
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 for (i = 0; i < IPR_MAX_PHYSICAL_DEVS; i++)
7244 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
7245
7246 ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
7247 sizeof(struct ipr_misc_cbs),
7248 &ioa_cfg->vpd_cbs_dma);
7249
7250 if (!ioa_cfg->vpd_cbs)
7251 goto out_free_res_entries;
7252
7253 if (ipr_alloc_cmd_blks(ioa_cfg))
7254 goto out_free_vpd_cbs;
7255
7256 ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
7257 sizeof(u32) * IPR_NUM_CMD_BLKS,
7258 &ioa_cfg->host_rrq_dma);
7259
7260 if (!ioa_cfg->host_rrq)
7261 goto out_ipr_free_cmd_blocks;
7262
7263 ioa_cfg->cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
7264 sizeof(struct ipr_config_table),
7265 &ioa_cfg->cfg_table_dma);
7266
7267 if (!ioa_cfg->cfg_table)
7268 goto out_free_host_rrq;
7269
7270 for (i = 0; i < IPR_NUM_HCAMS; i++) {
7271 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
7272 sizeof(struct ipr_hostrcb),
7273 &ioa_cfg->hostrcb_dma[i]);
7274
7275 if (!ioa_cfg->hostrcb[i])
7276 goto out_free_hostrcb_dma;
7277
7278 ioa_cfg->hostrcb[i]->hostrcb_dma =
7279 ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
Brian King49dc6a12006-11-21 10:28:35 -06007280 ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
7282 }
7283
brking@us.ibm.com0bc42e32005-11-01 17:01:20 -06007284 ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
7286
7287 if (!ioa_cfg->trace)
7288 goto out_free_hostrcb_dma;
7289
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 rc = 0;
7291out:
7292 LEAVE;
7293 return rc;
7294
7295out_free_hostrcb_dma:
7296 while (i-- > 0) {
7297 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
7298 ioa_cfg->hostrcb[i],
7299 ioa_cfg->hostrcb_dma[i]);
7300 }
7301 pci_free_consistent(pdev, sizeof(struct ipr_config_table),
7302 ioa_cfg->cfg_table, ioa_cfg->cfg_table_dma);
7303out_free_host_rrq:
7304 pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
7305 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
7306out_ipr_free_cmd_blocks:
7307 ipr_free_cmd_blks(ioa_cfg);
7308out_free_vpd_cbs:
7309 pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
7310 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
7311out_free_res_entries:
7312 kfree(ioa_cfg->res_entries);
7313 goto out;
7314}
7315
7316/**
7317 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
7318 * @ioa_cfg: ioa config struct
7319 *
7320 * Return value:
7321 * none
7322 **/
7323static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
7324{
7325 int i;
7326
7327 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
7328 ioa_cfg->bus_attr[i].bus = i;
7329 ioa_cfg->bus_attr[i].qas_enabled = 0;
7330 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
7331 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
7332 ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
7333 else
7334 ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
7335 }
7336}
7337
7338/**
7339 * ipr_init_ioa_cfg - Initialize IOA config struct
7340 * @ioa_cfg: ioa config struct
7341 * @host: scsi host struct
7342 * @pdev: PCI dev struct
7343 *
7344 * Return value:
7345 * none
7346 **/
7347static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
7348 struct Scsi_Host *host, struct pci_dev *pdev)
7349{
7350 const struct ipr_interrupt_offsets *p;
7351 struct ipr_interrupts *t;
7352 void __iomem *base;
7353
7354 ioa_cfg->host = host;
7355 ioa_cfg->pdev = pdev;
7356 ioa_cfg->log_level = ipr_log_level;
brking@us.ibm.com3d1d0da2005-11-01 17:01:54 -06007357 ioa_cfg->doorbell = IPR_DOORBELL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
7359 sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
7360 sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
7361 sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
7362 sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
7363 sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
7364 sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
7365 sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
7366
7367 INIT_LIST_HEAD(&ioa_cfg->free_q);
7368 INIT_LIST_HEAD(&ioa_cfg->pending_q);
7369 INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
7370 INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
7371 INIT_LIST_HEAD(&ioa_cfg->free_res_q);
7372 INIT_LIST_HEAD(&ioa_cfg->used_res_q);
David Howellsc4028952006-11-22 14:57:56 +00007373 INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 init_waitqueue_head(&ioa_cfg->reset_wait_q);
Wayne Boyer95fecd92009-06-16 15:13:28 -07007375 init_waitqueue_head(&ioa_cfg->msi_wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 ioa_cfg->sdt_state = INACTIVE;
brking@us.ibm.com62275042005-11-01 17:01:14 -06007377 if (ipr_enable_cache)
7378 ioa_cfg->cache_state = CACHE_ENABLED;
7379 else
7380 ioa_cfg->cache_state = CACHE_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
7382 ipr_initialize_bus_attr(ioa_cfg);
7383
7384 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
7385 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
7386 host->max_channel = IPR_MAX_BUS_TO_SCAN;
7387 host->unique_id = host->host_no;
7388 host->max_cmd_len = IPR_MAX_CDB_LEN;
7389 pci_set_drvdata(pdev, ioa_cfg);
7390
7391 p = &ioa_cfg->chip_cfg->regs;
7392 t = &ioa_cfg->regs;
7393 base = ioa_cfg->hdw_dma_regs;
7394
7395 t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
7396 t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
7397 t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
7398 t->clr_interrupt_reg = base + p->clr_interrupt_reg;
7399 t->sense_interrupt_reg = base + p->sense_interrupt_reg;
7400 t->ioarrin_reg = base + p->ioarrin_reg;
7401 t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
7402 t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
7403 t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
7404}
7405
7406/**
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007407 * ipr_get_chip_info - Find adapter chip information
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 * @dev_id: PCI device id struct
7409 *
7410 * Return value:
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007411 * ptr to chip information on success / NULL on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 **/
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007413static const struct ipr_chip_t * __devinit
7414ipr_get_chip_info(const struct pci_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415{
7416 int i;
7417
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
7419 if (ipr_chip[i].vendor == dev_id->vendor &&
7420 ipr_chip[i].device == dev_id->device)
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007421 return &ipr_chip[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422 return NULL;
7423}
7424
7425/**
Wayne Boyer95fecd92009-06-16 15:13:28 -07007426 * ipr_test_intr - Handle the interrupt generated in ipr_test_msi().
7427 * @pdev: PCI device struct
7428 *
7429 * Description: Simply set the msi_received flag to 1 indicating that
7430 * Message Signaled Interrupts are supported.
7431 *
7432 * Return value:
7433 * 0 on success / non-zero on failure
7434 **/
7435static irqreturn_t __devinit ipr_test_intr(int irq, void *devp)
7436{
7437 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
7438 unsigned long lock_flags = 0;
7439 irqreturn_t rc = IRQ_HANDLED;
7440
7441 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7442
7443 ioa_cfg->msi_received = 1;
7444 wake_up(&ioa_cfg->msi_wait_q);
7445
7446 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7447 return rc;
7448}
7449
7450/**
7451 * ipr_test_msi - Test for Message Signaled Interrupt (MSI) support.
7452 * @pdev: PCI device struct
7453 *
7454 * Description: The return value from pci_enable_msi() can not always be
7455 * trusted. This routine sets up and initiates a test interrupt to determine
7456 * if the interrupt is received via the ipr_test_intr() service routine.
7457 * If the tests fails, the driver will fall back to LSI.
7458 *
7459 * Return value:
7460 * 0 on success / non-zero on failure
7461 **/
7462static int __devinit ipr_test_msi(struct ipr_ioa_cfg *ioa_cfg,
7463 struct pci_dev *pdev)
7464{
7465 int rc;
7466 volatile u32 int_reg;
7467 unsigned long lock_flags = 0;
7468
7469 ENTER;
7470
7471 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7472 init_waitqueue_head(&ioa_cfg->msi_wait_q);
7473 ioa_cfg->msi_received = 0;
7474 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
7475 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.clr_interrupt_mask_reg);
7476 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7477 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7478
7479 rc = request_irq(pdev->irq, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
7480 if (rc) {
7481 dev_err(&pdev->dev, "Can not assign irq %d\n", pdev->irq);
7482 return rc;
7483 } else if (ipr_debug)
7484 dev_info(&pdev->dev, "IRQ assigned: %d\n", pdev->irq);
7485
7486 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.sense_interrupt_reg);
7487 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
7488 wait_event_timeout(ioa_cfg->msi_wait_q, ioa_cfg->msi_received, HZ);
7489 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
7490
7491 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7492 if (!ioa_cfg->msi_received) {
7493 /* MSI test failed */
7494 dev_info(&pdev->dev, "MSI test failed. Falling back to LSI.\n");
7495 rc = -EOPNOTSUPP;
7496 } else if (ipr_debug)
7497 dev_info(&pdev->dev, "MSI test succeeded.\n");
7498
7499 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7500
7501 free_irq(pdev->irq, ioa_cfg);
7502
7503 LEAVE;
7504
7505 return rc;
7506}
7507
7508/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 * ipr_probe_ioa - Allocates memory and does first stage of initialization
7510 * @pdev: PCI device struct
7511 * @dev_id: PCI device id struct
7512 *
7513 * Return value:
7514 * 0 on success / non-zero on failure
7515 **/
7516static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
7517 const struct pci_device_id *dev_id)
7518{
7519 struct ipr_ioa_cfg *ioa_cfg;
7520 struct Scsi_Host *host;
7521 unsigned long ipr_regs_pci;
7522 void __iomem *ipr_regs;
Eric Sesterhenna2a65a32006-09-25 16:59:07 -07007523 int rc = PCIBIOS_SUCCESSFUL;
Brian King473b1e82007-05-02 10:44:11 -05007524 volatile u32 mask, uproc, interrupts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525
7526 ENTER;
7527
7528 if ((rc = pci_enable_device(pdev))) {
7529 dev_err(&pdev->dev, "Cannot enable adapter\n");
7530 goto out;
7531 }
7532
7533 dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
7534
7535 host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
7536
7537 if (!host) {
7538 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
7539 rc = -ENOMEM;
7540 goto out_disable;
7541 }
7542
7543 ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
7544 memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
Brian King35a39692006-09-25 12:39:20 -05007545 ata_host_init(&ioa_cfg->ata_host, &pdev->dev,
7546 sata_port_info.flags, &ipr_sata_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007548 ioa_cfg->ipr_chip = ipr_get_chip_info(dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007550 if (!ioa_cfg->ipr_chip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
7552 dev_id->vendor, dev_id->device);
7553 goto out_scsi_host_put;
7554 }
7555
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007556 ioa_cfg->chip_cfg = ioa_cfg->ipr_chip->cfg;
7557
Brian King5469cb52007-03-29 12:42:40 -05007558 if (ipr_transop_timeout)
7559 ioa_cfg->transop_timeout = ipr_transop_timeout;
7560 else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
7561 ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
7562 else
7563 ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
7564
Auke Kok44c10132007-06-08 15:46:36 -07007565 ioa_cfg->revid = pdev->revision;
Brian King463fc692007-05-07 17:09:05 -05007566
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 ipr_regs_pci = pci_resource_start(pdev, 0);
7568
7569 rc = pci_request_regions(pdev, IPR_NAME);
7570 if (rc < 0) {
7571 dev_err(&pdev->dev,
7572 "Couldn't register memory range of registers\n");
7573 goto out_scsi_host_put;
7574 }
7575
Arjan van de Ven25729a72008-09-28 16:18:02 -07007576 ipr_regs = pci_ioremap_bar(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577
7578 if (!ipr_regs) {
7579 dev_err(&pdev->dev,
7580 "Couldn't map memory range of registers\n");
7581 rc = -ENOMEM;
7582 goto out_release_regions;
7583 }
7584
7585 ioa_cfg->hdw_dma_regs = ipr_regs;
7586 ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
7587 ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
7588
7589 ipr_init_ioa_cfg(ioa_cfg, host, pdev);
7590
7591 pci_set_master(pdev);
7592
Yang Hongyang284901a2009-04-06 19:01:15 -07007593 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 if (rc < 0) {
7595 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
7596 goto cleanup_nomem;
7597 }
7598
7599 rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
7600 ioa_cfg->chip_cfg->cache_line_size);
7601
7602 if (rc != PCIBIOS_SUCCESSFUL) {
7603 dev_err(&pdev->dev, "Write of cache line size failed\n");
7604 rc = -EIO;
7605 goto cleanup_nomem;
7606 }
7607
Wayne Boyer95fecd92009-06-16 15:13:28 -07007608 /* Enable MSI style interrupts if they are supported. */
Wayne Boyer1be7bd82009-06-17 09:55:35 -07007609 if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI && !pci_enable_msi(pdev)) {
Wayne Boyer95fecd92009-06-16 15:13:28 -07007610 rc = ipr_test_msi(ioa_cfg, pdev);
7611 if (rc == -EOPNOTSUPP)
7612 pci_disable_msi(pdev);
7613 else if (rc)
7614 goto out_msi_disable;
7615 else
7616 dev_info(&pdev->dev, "MSI enabled with IRQ: %d\n", pdev->irq);
7617 } else if (ipr_debug)
7618 dev_info(&pdev->dev, "Cannot enable MSI.\n");
7619
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 /* Save away PCI config space for use following IOA reset */
7621 rc = pci_save_state(pdev);
7622
7623 if (rc != PCIBIOS_SUCCESSFUL) {
7624 dev_err(&pdev->dev, "Failed to save PCI config space\n");
7625 rc = -EIO;
7626 goto cleanup_nomem;
7627 }
7628
7629 if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
7630 goto cleanup_nomem;
7631
7632 if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
7633 goto cleanup_nomem;
7634
7635 rc = ipr_alloc_mem(ioa_cfg);
7636 if (rc < 0) {
7637 dev_err(&pdev->dev,
7638 "Couldn't allocate enough memory for device driver!\n");
7639 goto cleanup_nomem;
7640 }
7641
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007642 /*
7643 * If HRRQ updated interrupt is not masked, or reset alert is set,
7644 * the card is in an unknown state and needs a hard reset
7645 */
7646 mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
Brian King473b1e82007-05-02 10:44:11 -05007647 interrupts = readl(ioa_cfg->regs.sense_interrupt_reg);
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007648 uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
7649 if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
7650 ioa_cfg->needs_hard_reset = 1;
Brian King473b1e82007-05-02 10:44:11 -05007651 if (interrupts & IPR_PCII_ERROR_INTERRUPTS)
7652 ioa_cfg->needs_hard_reset = 1;
7653 if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
7654 ioa_cfg->ioa_unit_checked = 1;
brking@us.ibm.comce155cc2005-11-17 09:35:12 -06007655
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
Wayne Boyer95fecd92009-06-16 15:13:28 -07007657 rc = request_irq(pdev->irq, ipr_isr,
7658 ioa_cfg->msi_received ? 0 : IRQF_SHARED,
7659 IPR_NAME, ioa_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660
7661 if (rc) {
7662 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
7663 pdev->irq, rc);
7664 goto cleanup_nolog;
7665 }
7666
Brian King463fc692007-05-07 17:09:05 -05007667 if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
7668 (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
7669 ioa_cfg->needs_warm_reset = 1;
7670 ioa_cfg->reset = ipr_reset_slot_reset;
7671 } else
7672 ioa_cfg->reset = ipr_reset_start_bist;
7673
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 spin_lock(&ipr_driver_lock);
7675 list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
7676 spin_unlock(&ipr_driver_lock);
7677
7678 LEAVE;
7679out:
7680 return rc;
7681
7682cleanup_nolog:
7683 ipr_free_mem(ioa_cfg);
7684cleanup_nomem:
7685 iounmap(ipr_regs);
Wayne Boyer95fecd92009-06-16 15:13:28 -07007686out_msi_disable:
7687 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688out_release_regions:
7689 pci_release_regions(pdev);
7690out_scsi_host_put:
7691 scsi_host_put(host);
7692out_disable:
7693 pci_disable_device(pdev);
7694 goto out;
7695}
7696
7697/**
7698 * ipr_scan_vsets - Scans for VSET devices
7699 * @ioa_cfg: ioa config struct
7700 *
7701 * Description: Since the VSET resources do not follow SAM in that we can have
7702 * sparse LUNs with no LUN 0, we have to scan for these ourselves.
7703 *
7704 * Return value:
7705 * none
7706 **/
7707static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
7708{
7709 int target, lun;
7710
7711 for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
7712 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
7713 scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
7714}
7715
7716/**
7717 * ipr_initiate_ioa_bringdown - Bring down an adapter
7718 * @ioa_cfg: ioa config struct
7719 * @shutdown_type: shutdown type
7720 *
7721 * Description: This function will initiate bringing down the adapter.
7722 * This consists of issuing an IOA shutdown to the adapter
7723 * to flush the cache, and running BIST.
7724 * If the caller needs to wait on the completion of the reset,
7725 * the caller must sleep on the reset_wait_q.
7726 *
7727 * Return value:
7728 * none
7729 **/
7730static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
7731 enum ipr_shutdown_type shutdown_type)
7732{
7733 ENTER;
7734 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
7735 ioa_cfg->sdt_state = ABORT_DUMP;
7736 ioa_cfg->reset_retries = 0;
7737 ioa_cfg->in_ioa_bringdown = 1;
7738 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
7739 LEAVE;
7740}
7741
7742/**
7743 * __ipr_remove - Remove a single adapter
7744 * @pdev: pci device struct
7745 *
7746 * Adapter hot plug remove entry point.
7747 *
7748 * Return value:
7749 * none
7750 **/
7751static void __ipr_remove(struct pci_dev *pdev)
7752{
7753 unsigned long host_lock_flags = 0;
7754 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7755 ENTER;
7756
7757 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
Brian King970ea292007-04-26 16:00:06 -05007758 while(ioa_cfg->in_reset_reload) {
7759 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7760 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7761 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7762 }
7763
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
7765
7766 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7767 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
brking@us.ibm.com 5cbf5ea2005-05-02 19:50:47 -05007768 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
7770
7771 spin_lock(&ipr_driver_lock);
7772 list_del(&ioa_cfg->queue);
7773 spin_unlock(&ipr_driver_lock);
7774
7775 if (ioa_cfg->sdt_state == ABORT_DUMP)
7776 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7777 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
7778
7779 ipr_free_all_resources(ioa_cfg);
7780
7781 LEAVE;
7782}
7783
7784/**
7785 * ipr_remove - IOA hot plug remove entry point
7786 * @pdev: pci device struct
7787 *
7788 * Adapter hot plug remove entry point.
7789 *
7790 * Return value:
7791 * none
7792 **/
Kleber S. Souzaf3816422009-04-22 10:50:28 -03007793static void __devexit ipr_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794{
7795 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
7796
7797 ENTER;
7798
Tony Jonesee959b02008-02-22 00:13:36 +01007799 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 &ipr_trace_attr);
Tony Jonesee959b02008-02-22 00:13:36 +01007801 ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802 &ipr_dump_attr);
7803 scsi_remove_host(ioa_cfg->host);
7804
7805 __ipr_remove(pdev);
7806
7807 LEAVE;
7808}
7809
7810/**
7811 * ipr_probe - Adapter hot plug add entry point
7812 *
7813 * Return value:
7814 * 0 on success / non-zero on failure
7815 **/
7816static int __devinit ipr_probe(struct pci_dev *pdev,
7817 const struct pci_device_id *dev_id)
7818{
7819 struct ipr_ioa_cfg *ioa_cfg;
7820 int rc;
7821
7822 rc = ipr_probe_ioa(pdev, dev_id);
7823
7824 if (rc)
7825 return rc;
7826
7827 ioa_cfg = pci_get_drvdata(pdev);
7828 rc = ipr_probe_ioa_part2(ioa_cfg);
7829
7830 if (rc) {
7831 __ipr_remove(pdev);
7832 return rc;
7833 }
7834
7835 rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
7836
7837 if (rc) {
7838 __ipr_remove(pdev);
7839 return rc;
7840 }
7841
Tony Jonesee959b02008-02-22 00:13:36 +01007842 rc = ipr_create_trace_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 &ipr_trace_attr);
7844
7845 if (rc) {
7846 scsi_remove_host(ioa_cfg->host);
7847 __ipr_remove(pdev);
7848 return rc;
7849 }
7850
Tony Jonesee959b02008-02-22 00:13:36 +01007851 rc = ipr_create_dump_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 &ipr_dump_attr);
7853
7854 if (rc) {
Tony Jonesee959b02008-02-22 00:13:36 +01007855 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 &ipr_trace_attr);
7857 scsi_remove_host(ioa_cfg->host);
7858 __ipr_remove(pdev);
7859 return rc;
7860 }
7861
7862 scsi_scan_host(ioa_cfg->host);
7863 ipr_scan_vsets(ioa_cfg);
7864 scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
7865 ioa_cfg->allow_ml_add_del = 1;
brking@us.ibm.com11cd8f12005-11-01 17:00:11 -06007866 ioa_cfg->host->max_channel = IPR_VSET_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 schedule_work(&ioa_cfg->work_q);
7868 return 0;
7869}
7870
7871/**
7872 * ipr_shutdown - Shutdown handler.
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007873 * @pdev: pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 *
7875 * This function is invoked upon system shutdown/reboot. It will issue
7876 * an adapter shutdown to the adapter to flush the write cache.
7877 *
7878 * Return value:
7879 * none
7880 **/
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007881static void ipr_shutdown(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882{
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007883 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884 unsigned long lock_flags = 0;
7885
7886 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
Brian King970ea292007-04-26 16:00:06 -05007887 while(ioa_cfg->in_reset_reload) {
7888 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7889 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7890 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7891 }
7892
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
7894 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7895 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
7896}
7897
7898static struct pci_device_id ipr_pci_table[] __devinitdata = {
7899 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007900 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007902 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007904 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
Brian King6d84c942007-01-23 11:25:23 -06007906 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06007908 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06007910 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King6d84c942007-01-23 11:25:23 -06007912 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007913 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
Brian King5469cb52007-03-29 12:42:40 -05007914 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
7915 IPR_USE_LONG_TRANSOP_TIMEOUT },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007916 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King6d84c942007-01-23 11:25:23 -06007917 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007918 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King22d2e402007-04-26 16:00:13 -05007919 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
7920 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06007921 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
Brian King5469cb52007-03-29 12:42:40 -05007922 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
7923 IPR_USE_LONG_TRANSOP_TIMEOUT },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007924 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King6d84c942007-01-23 11:25:23 -06007925 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007926 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King22d2e402007-04-26 16:00:13 -05007927 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
7928 IPR_USE_LONG_TRANSOP_TIMEOUT},
Brian King60e74862006-11-21 10:28:10 -06007929 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
Brian King5469cb52007-03-29 12:42:40 -05007930 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
7931 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06007932 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
Brian King22d2e402007-04-26 16:00:13 -05007933 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
7934 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King185eb312007-03-29 12:42:53 -05007935 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
7936 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575D, 0, 0,
7937 IPR_USE_LONG_TRANSOP_TIMEOUT },
7938 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
7939 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
7940 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
Brian King5469cb52007-03-29 12:42:40 -05007941 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
Brian King463fc692007-05-07 17:09:05 -05007942 IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
Brian King6d84c942007-01-23 11:25:23 -06007944 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King6d84c942007-01-23 11:25:23 -06007946 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
brking@us.ibm.com86f51432005-11-01 17:02:42 -06007947 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King5469cb52007-03-29 12:42:40 -05007948 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
7949 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King60e74862006-11-21 10:28:10 -06007950 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
Brian King5469cb52007-03-29 12:42:40 -05007951 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
7952 IPR_USE_LONG_TRANSOP_TIMEOUT },
Brian King185eb312007-03-29 12:42:53 -05007953 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SCAMP_E,
7954 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0,
7955 IPR_USE_LONG_TRANSOP_TIMEOUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956 { }
7957};
7958MODULE_DEVICE_TABLE(pci, ipr_pci_table);
7959
Linas Vepstasf8a88b192006-02-03 16:52:42 -06007960static struct pci_error_handlers ipr_err_handler = {
7961 .error_detected = ipr_pci_error_detected,
7962 .slot_reset = ipr_pci_slot_reset,
7963};
7964
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965static struct pci_driver ipr_driver = {
7966 .name = IPR_NAME,
7967 .id_table = ipr_pci_table,
7968 .probe = ipr_probe,
Kleber S. Souzaf3816422009-04-22 10:50:28 -03007969 .remove = __devexit_p(ipr_remove),
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07007970 .shutdown = ipr_shutdown,
Linas Vepstasf8a88b192006-02-03 16:52:42 -06007971 .err_handler = &ipr_err_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972};
7973
7974/**
7975 * ipr_init - Module entry point
7976 *
7977 * Return value:
7978 * 0 on success / negative value on failure
7979 **/
7980static int __init ipr_init(void)
7981{
7982 ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
7983 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
7984
Henrik Kretzschmardcbccbde2006-09-25 16:58:58 -07007985 return pci_register_driver(&ipr_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986}
7987
7988/**
7989 * ipr_exit - Module unload
7990 *
7991 * Module unload entry point.
7992 *
7993 * Return value:
7994 * none
7995 **/
7996static void __exit ipr_exit(void)
7997{
7998 pci_unregister_driver(&ipr_driver);
7999}
8000
8001module_init(ipr_init);
8002module_exit(ipr_exit);