ipr.c 293.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/*
 * ipr.c -- driver for IBM Power Linux RAID adapters
 *
 * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
 *
 * Copyright (C) 2003, 2004 IBM Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * Notes:
 *
 * This driver is used to control the following SCSI adapters:
 *
 * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
 *
 * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
 *              PCI-X Dual Channel Ultra 320 SCSI Adapter
 *              PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
 *              Embedded SCSI adapter on p615 and p655 systems
 *
 * Supported Hardware Features:
 *	- Ultra 320 SCSI controller
 *	- PCI-X host interface
 *	- Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
 *	- Non-Volatile Write Cache
 *	- Supports attachment of non-RAID disks, tape, and optical devices
 *	- RAID Levels 0, 5, 10
 *	- Hot spare
 *	- Background Parity Checking
 *	- Background Data Scrubbing
 *	- Ability to increase the capacity of an existing RAID 5 disk array
 *		by adding disks
 *
 * Driver Features:
 *	- Tagged command queuing
 *	- Adapter microcode download
 *	- PCI hot plug
 *	- SCSI device hot plug
 *
 */

#include <linux/fs.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/kernel.h>
62
#include <linux/slab.h>
63
#include <linux/vmalloc.h>
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/wait.h>
#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/blkdev.h>
#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
75
#include <linux/libata.h>
76
#include <linux/hdreg.h>
77
#include <linux/reboot.h>
78
#include <linux/stringify.h>
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/processor.h>
#include <scsi/scsi.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_tcq.h>
#include <scsi/scsi_eh.h>
#include <scsi/scsi_cmnd.h>
#include "ipr.h"

/*
 *   Global Data
 */
92
static LIST_HEAD(ipr_ioa_head);
L
Linus Torvalds 已提交
93 94 95 96
static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
static unsigned int ipr_max_speed = 1;
static int ipr_testmode = 0;
static unsigned int ipr_fastfail = 0;
97
static unsigned int ipr_transop_timeout = 0;
98
static unsigned int ipr_debug = 0;
99
static unsigned int ipr_max_devs = IPR_DEFAULT_SIS64_DEVS;
100
static unsigned int ipr_dual_ioa_raid = 1;
101
static unsigned int ipr_number_of_msix = 2;
B
Brian King 已提交
102
static unsigned int ipr_fast_reboot;
L
Linus Torvalds 已提交
103 104 105 106
static DEFINE_SPINLOCK(ipr_driver_lock);

/* This table describes the differences between DMA controller chips */
static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
107
	{ /* Gemstone, Citrine, Obsidian, and Obsidian-E */
L
Linus Torvalds 已提交
108
		.mailbox = 0x0042C,
109
		.max_cmds = 100,
L
Linus Torvalds 已提交
110
		.cache_line_size = 0x20,
111
		.clear_isr = 1,
112
		.iopoll_weight = 0,
L
Linus Torvalds 已提交
113 114 115
		{
			.set_interrupt_mask_reg = 0x0022C,
			.clr_interrupt_mask_reg = 0x00230,
116
			.clr_interrupt_mask_reg32 = 0x00230,
L
Linus Torvalds 已提交
117
			.sense_interrupt_mask_reg = 0x0022C,
118
			.sense_interrupt_mask_reg32 = 0x0022C,
L
Linus Torvalds 已提交
119
			.clr_interrupt_reg = 0x00228,
120
			.clr_interrupt_reg32 = 0x00228,
L
Linus Torvalds 已提交
121
			.sense_interrupt_reg = 0x00224,
122
			.sense_interrupt_reg32 = 0x00224,
L
Linus Torvalds 已提交
123 124
			.ioarrin_reg = 0x00404,
			.sense_uproc_interrupt_reg = 0x00214,
125
			.sense_uproc_interrupt_reg32 = 0x00214,
L
Linus Torvalds 已提交
126
			.set_uproc_interrupt_reg = 0x00214,
127 128 129
			.set_uproc_interrupt_reg32 = 0x00214,
			.clr_uproc_interrupt_reg = 0x00218,
			.clr_uproc_interrupt_reg32 = 0x00218
L
Linus Torvalds 已提交
130 131 132 133
		}
	},
	{ /* Snipe and Scamp */
		.mailbox = 0x0052C,
134
		.max_cmds = 100,
L
Linus Torvalds 已提交
135
		.cache_line_size = 0x20,
136
		.clear_isr = 1,
137
		.iopoll_weight = 0,
L
Linus Torvalds 已提交
138 139 140
		{
			.set_interrupt_mask_reg = 0x00288,
			.clr_interrupt_mask_reg = 0x0028C,
141
			.clr_interrupt_mask_reg32 = 0x0028C,
L
Linus Torvalds 已提交
142
			.sense_interrupt_mask_reg = 0x00288,
143
			.sense_interrupt_mask_reg32 = 0x00288,
L
Linus Torvalds 已提交
144
			.clr_interrupt_reg = 0x00284,
145
			.clr_interrupt_reg32 = 0x00284,
L
Linus Torvalds 已提交
146
			.sense_interrupt_reg = 0x00280,
147
			.sense_interrupt_reg32 = 0x00280,
L
Linus Torvalds 已提交
148 149
			.ioarrin_reg = 0x00504,
			.sense_uproc_interrupt_reg = 0x00290,
150
			.sense_uproc_interrupt_reg32 = 0x00290,
L
Linus Torvalds 已提交
151
			.set_uproc_interrupt_reg = 0x00290,
152 153 154
			.set_uproc_interrupt_reg32 = 0x00290,
			.clr_uproc_interrupt_reg = 0x00294,
			.clr_uproc_interrupt_reg32 = 0x00294
L
Linus Torvalds 已提交
155 156
		}
	},
157
	{ /* CRoC */
158
		.mailbox = 0x00044,
159
		.max_cmds = 1000,
160
		.cache_line_size = 0x20,
161
		.clear_isr = 0,
162
		.iopoll_weight = 64,
163 164 165
		{
			.set_interrupt_mask_reg = 0x00010,
			.clr_interrupt_mask_reg = 0x00018,
166
			.clr_interrupt_mask_reg32 = 0x0001C,
167
			.sense_interrupt_mask_reg = 0x00010,
168
			.sense_interrupt_mask_reg32 = 0x00014,
169
			.clr_interrupt_reg = 0x00008,
170
			.clr_interrupt_reg32 = 0x0000C,
171
			.sense_interrupt_reg = 0x00000,
172
			.sense_interrupt_reg32 = 0x00004,
173 174
			.ioarrin_reg = 0x00070,
			.sense_uproc_interrupt_reg = 0x00020,
175
			.sense_uproc_interrupt_reg32 = 0x00024,
176
			.set_uproc_interrupt_reg = 0x00020,
177
			.set_uproc_interrupt_reg32 = 0x00024,
178
			.clr_uproc_interrupt_reg = 0x00028,
179 180
			.clr_uproc_interrupt_reg32 = 0x0002C,
			.init_feedback_reg = 0x0005C,
181
			.dump_addr_reg = 0x00064,
182 183
			.dump_data_reg = 0x00068,
			.endian_swap_reg = 0x00084
184 185
		}
	},
L
Linus Torvalds 已提交
186 187 188
};

static const struct ipr_chip_t ipr_chip[] = {
189 190 191 192 193 194 195 196
	{ PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
	{ PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, IPR_USE_MSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[1] },
	{ PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[1] },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] },
197
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] }
L
Linus Torvalds 已提交
198 199
};

200
static int ipr_max_bus_speeds[] = {
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209 210 211
	IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
};

MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
module_param_named(max_speed, ipr_max_speed, uint, 0);
MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
module_param_named(log_level, ipr_log_level, uint, 0);
MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
module_param_named(testmode, ipr_testmode, int, 0);
MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
212
module_param_named(fastfail, ipr_fastfail, int, S_IRUGO | S_IWUSR);
L
Linus Torvalds 已提交
213 214 215
MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
216
module_param_named(debug, ipr_debug, int, S_IRUGO | S_IWUSR);
217
MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
218 219
module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
220 221 222
module_param_named(max_devs, ipr_max_devs, int, 0);
MODULE_PARM_DESC(max_devs, "Specify the maximum number of physical devices. "
		 "[Default=" __stringify(IPR_DEFAULT_SIS64_DEVS) "]");
223
module_param_named(number_of_msix, ipr_number_of_msix, int, 0);
224
MODULE_PARM_DESC(number_of_msix, "Specify the number of MSIX interrupts to use on capable adapters (1 - 16).  (default:2)");
B
Brian King 已提交
225 226
module_param_named(fast_reboot, ipr_fast_reboot, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(fast_reboot, "Skip adapter shutdown during reboot. Set to 1 to enable. (default: 0)");
L
Linus Torvalds 已提交
227 228 229 230 231 232
MODULE_LICENSE("GPL");
MODULE_VERSION(IPR_DRIVER_VERSION);

/*  A constant array of IOASCs/URCs/Error Messages */
static const
struct ipr_error_table_t ipr_error_table[] = {
233
	{0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
234 235 236 237 238 239 240
	"8155: An unknown error was received"},
	{0x00330000, 0, 0,
	"Soft underlength error"},
	{0x005A0000, 0, 0,
	"Command to be cancelled not found"},
	{0x00808000, 0, 0,
	"Qualified success"},
241
	{0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
242
	"FFFE: Soft device bus error recovered by the IOA"},
243
	{0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
244
	"4101: Soft device bus fabric error"},
245 246 247 248 249 250 251 252 253 254 255 256 257 258
	{0x01100100, 0, IPR_DEFAULT_LOG_LEVEL,
	"FFFC: Logical block guard error recovered by the device"},
	{0x01100300, 0, IPR_DEFAULT_LOG_LEVEL,
	"FFFC: Logical block reference tag error recovered by the device"},
	{0x01108300, 0, IPR_DEFAULT_LOG_LEVEL,
	"4171: Recovered scatter list tag / sequence number error"},
	{0x01109000, 0, IPR_DEFAULT_LOG_LEVEL,
	"FF3D: Recovered logical block CRC error on IOA to Host transfer"},
	{0x01109200, 0, IPR_DEFAULT_LOG_LEVEL,
	"4171: Recovered logical block sequence number error on IOA to Host transfer"},
	{0x0110A000, 0, IPR_DEFAULT_LOG_LEVEL,
	"FFFD: Recovered logical block reference tag error detected by the IOA"},
	{0x0110A100, 0, IPR_DEFAULT_LOG_LEVEL,
	"FFFD: Logical block guard error recovered by the IOA"},
259
	{0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
260
	"FFF9: Device sector reassign successful"},
261
	{0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
262
	"FFF7: Media error recovered by device rewrite procedures"},
263
	{0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
264
	"7001: IOA sector reassignment successful"},
265
	{0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
266
	"FFF9: Soft media error. Sector reassignment recommended"},
267
	{0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
268
	"FFF7: Media error recovered by IOA rewrite procedures"},
269
	{0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
270
	"FF3D: Soft PCI bus error recovered by the IOA"},
271
	{0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
272
	"FFF6: Device hardware error recovered by the IOA"},
273
	{0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
274
	"FFF6: Device hardware error recovered by the device"},
275
	{0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
276
	"FF3D: Soft IOA error recovered by the IOA"},
277
	{0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
278
	"FFFA: Undefined device response recovered by the IOA"},
279
	{0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
280
	"FFF6: Device bus error, message or command phase"},
281
	{0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
282
	"FFFE: Task Management Function failed"},
283
	{0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
284
	"FFF6: Failure prediction threshold exceeded"},
285
	{0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
286
	"8009: Impending cache battery pack failure"},
287 288 289 290
	{0x02040100, 0, 0,
	"Logical Unit in process of becoming ready"},
	{0x02040200, 0, 0,
	"Initializing command required"},
L
Linus Torvalds 已提交
291 292
	{0x02040400, 0, 0,
	"34FF: Disk device format in progress"},
293 294
	{0x02040C00, 0, 0,
	"Logical unit not accessible, target port in unavailable state"},
295 296
	{0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
	"9070: IOA requested reset"},
L
Linus Torvalds 已提交
297 298
	{0x023F0000, 0, 0,
	"Synchronization required"},
299 300 301 302
	{0x02408500, 0, 0,
	"IOA microcode download required"},
	{0x02408600, 0, 0,
	"Device bus connection is prohibited by host"},
L
Linus Torvalds 已提交
303 304 305 306
	{0x024E0000, 0, 0,
	"No ready, IOA shutdown"},
	{0x025A0000, 0, 0,
	"Not ready, IOA has been shutdown"},
307
	{0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
308 309 310 311 312
	"3020: Storage subsystem configuration error"},
	{0x03110B00, 0, 0,
	"FFF5: Medium error, data unreadable, recommend reassign"},
	{0x03110C00, 0, 0,
	"7000: Medium error, data unreadable, do not reassign"},
313
	{0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
314
	"FFF3: Disk media format bad"},
315
	{0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
316
	"3002: Addressed device failed to respond to selection"},
317
	{0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
318
	"3100: Device bus error"},
319
	{0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
320 321 322
	"3109: IOA timed out a device command"},
	{0x04088000, 0, 0,
	"3120: SCSI bus is not operational"},
323
	{0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
324
	"4100: Hard device bus fabric error"},
325 326 327 328 329 330 331 332 333 334 335 336 337 338
	{0x04100100, 0, IPR_DEFAULT_LOG_LEVEL,
	"310C: Logical block guard error detected by the device"},
	{0x04100300, 0, IPR_DEFAULT_LOG_LEVEL,
	"310C: Logical block reference tag error detected by the device"},
	{0x04108300, 1, IPR_DEFAULT_LOG_LEVEL,
	"4170: Scatter list tag / sequence number error"},
	{0x04109000, 1, IPR_DEFAULT_LOG_LEVEL,
	"8150: Logical block CRC error on IOA to Host transfer"},
	{0x04109200, 1, IPR_DEFAULT_LOG_LEVEL,
	"4170: Logical block sequence number error on IOA to Host transfer"},
	{0x0410A000, 0, IPR_DEFAULT_LOG_LEVEL,
	"310D: Logical block reference tag error detected by the IOA"},
	{0x0410A100, 0, IPR_DEFAULT_LOG_LEVEL,
	"310D: Logical block guard error detected by the IOA"},
339
	{0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
340
	"9000: IOA reserved area data check"},
341
	{0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
342
	"9001: IOA reserved area invalid data pattern"},
343
	{0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
344
	"9002: IOA reserved area LRC error"},
345 346
	{0x04118300, 1, IPR_DEFAULT_LOG_LEVEL,
	"Hardware Error, IOA metadata access error"},
347
	{0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
348
	"102E: Out of alternate sectors for disk storage"},
349
	{0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
350
	"FFF4: Data transfer underlength error"},
351
	{0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
352
	"FFF4: Data transfer overlength error"},
353
	{0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
354
	"3400: Logical unit failure"},
355
	{0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
356
	"FFF4: Device microcode is corrupt"},
357
	{0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
358 359 360
	"8150: PCI bus error"},
	{0x04430000, 1, 0,
	"Unsupported device bus message received"},
361
	{0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
362
	"FFF4: Disk device problem"},
363
	{0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
364
	"8150: Permanent IOA failure"},
365
	{0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
366
	"3010: Disk device returned wrong response to IOA"},
367
	{0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
368 369 370
	"8151: IOA microcode error"},
	{0x04448500, 0, 0,
	"Device bus status error"},
371
	{0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
372
	"8157: IOA error requiring IOA reset to recover"},
373 374
	{0x04448700, 0, 0,
	"ATA device status error"},
L
Linus Torvalds 已提交
375 376
	{0x04490000, 0, 0,
	"Message reject received from the device"},
377
	{0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
378
	"8008: A permanent cache battery pack failure occurred"},
379
	{0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
380
	"9090: Disk unit has been modified after the last known status"},
381
	{0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
382
	"9081: IOA detected device error"},
383
	{0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
384
	"9082: IOA detected device error"},
385
	{0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
386
	"3110: Device bus error, message or command phase"},
387
	{0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
388
	"3110: SAS Command / Task Management Function failed"},
389
	{0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
390
	"9091: Incorrect hardware configuration change has been detected"},
391
	{0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
392
	"9073: Invalid multi-adapter configuration"},
393
	{0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
394
	"4010: Incorrect connection between cascaded expanders"},
395
	{0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
396
	"4020: Connections exceed IOA design limits"},
397
	{0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
398
	"4030: Incorrect multipath connection"},
399
	{0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
400
	"4110: Unsupported enclosure function"},
401 402
	{0x04679800, 0, IPR_DEFAULT_LOG_LEVEL,
	"4120: SAS cable VPD cannot be read"},
403
	{0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
404 405 406 407 408
	"FFF4: Command to logical unit failed"},
	{0x05240000, 1, 0,
	"Illegal request, invalid request type or request packet"},
	{0x05250000, 0, 0,
	"Illegal request, invalid resource handle"},
409 410 411 412
	{0x05258000, 0, 0,
	"Illegal request, commands not allowed to this device"},
	{0x05258100, 0, 0,
	"Illegal request, command not allowed to a secondary adapter"},
413 414
	{0x05258200, 0, 0,
	"Illegal request, command not allowed to a non-optimized resource"},
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422
	{0x05260000, 0, 0,
	"Illegal request, invalid field in parameter list"},
	{0x05260100, 0, 0,
	"Illegal request, parameter not supported"},
	{0x05260200, 0, 0,
	"Illegal request, parameter value invalid"},
	{0x052C0000, 0, 0,
	"Illegal request, command sequence error"},
423 424
	{0x052C8000, 1, 0,
	"Illegal request, dual adapter support not enabled"},
425 426 427 428
	{0x052C8100, 1, 0,
	"Illegal request, another cable connector was physically disabled"},
	{0x054E8000, 1, 0,
	"Illegal request, inconsistent group id/group count"},
429
	{0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
430
	"9031: Array protection temporarily suspended, protection resuming"},
431
	{0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
432
	"9040: Array protection temporarily suspended, protection resuming"},
433 434 435 436
	{0x060B0100, 0, IPR_DEFAULT_LOG_LEVEL,
	"4080: IOA exceeded maximum operating temperature"},
	{0x060B8000, 0, IPR_DEFAULT_LOG_LEVEL,
	"4085: Service required"},
437
	{0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
438
	"3140: Device bus not ready to ready transition"},
439
	{0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
440 441 442 443 444
	"FFFB: SCSI bus was reset"},
	{0x06290500, 0, 0,
	"FFFE: SCSI bus transition to single ended"},
	{0x06290600, 0, 0,
	"FFFE: SCSI bus transition to LVD"},
445
	{0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
446
	"FFFB: SCSI bus was reset by another initiator"},
447
	{0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
448
	"3029: A device replacement has occurred"},
449 450
	{0x063F8300, 0, IPR_DEFAULT_LOG_LEVEL,
	"4102: Device bus fabric performance degradation"},
451
	{0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
452
	"9051: IOA cache data exists for a missing or failed device"},
453
	{0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
454
	"9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
455
	{0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
456
	"9025: Disk unit is not supported at its physical location"},
457
	{0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
458
	"3020: IOA detected a SCSI bus configuration error"},
459
	{0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
460
	"3150: SCSI bus configuration error"},
461
	{0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
462
	"9074: Asymmetric advanced function disk configuration"},
463
	{0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
464
	"4040: Incomplete multipath connection between IOA and enclosure"},
465
	{0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
466
	"4041: Incomplete multipath connection between enclosure and device"},
467
	{0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
468
	"9075: Incomplete multipath connection between IOA and remote IOA"},
469
	{0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
470
	"9076: Configuration error, missing remote IOA"},
471
	{0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
472
	"4050: Enclosure does not support a required multipath function"},
473 474 475 476 477 478 479 480
	{0x06679800, 0, IPR_DEFAULT_LOG_LEVEL,
	"4121: Configuration error, required cable is missing"},
	{0x06679900, 0, IPR_DEFAULT_LOG_LEVEL,
	"4122: Cable is not plugged into the correct location on remote IOA"},
	{0x06679A00, 0, IPR_DEFAULT_LOG_LEVEL,
	"4123: Configuration error, invalid cable vital product data"},
	{0x06679B00, 0, IPR_DEFAULT_LOG_LEVEL,
	"4124: Configuration error, both cable ends are plugged into the same IOA"},
481 482
	{0x06690000, 0, IPR_DEFAULT_LOG_LEVEL,
	"4070: Logically bad block written on device"},
483
	{0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
484
	"9041: Array protection temporarily suspended"},
485
	{0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
486
	"9042: Corrupt array parity detected on specified device"},
487
	{0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
488
	"9030: Array no longer protected due to missing or failed disk unit"},
489
	{0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
490
	"9071: Link operational transition"},
491
	{0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
492
	"9072: Link not operational transition"},
493
	{0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
494
	"9032: Array exposed but still protected"},
B
Brian King 已提交
495 496
	{0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
	"70DD: Device forced failed by disrupt device command"},
497
	{0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
498
	"4061: Multipath redundancy level got better"},
499
	{0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
500
	"4060: Multipath redundancy level got worse"},
501 502 503 504
	{0x06808100, 0, IPR_DEFAULT_LOG_LEVEL,
	"9083: Device raw mode enabled"},
	{0x06808200, 0, IPR_DEFAULT_LOG_LEVEL,
	"9084: Device raw mode disabled"},
L
Linus Torvalds 已提交
505 506
	{0x07270000, 0, 0,
	"Failure due to other device"},
507
	{0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
508
	"9008: IOA does not support functions expected by devices"},
509
	{0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
510
	"9010: Cache data associated with attached devices cannot be found"},
511
	{0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
512
	"9011: Cache data belongs to devices other than those attached"},
513
	{0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
514
	"9020: Array missing 2 or more devices with only 1 device present"},
515
	{0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
516
	"9021: Array missing 2 or more devices with 2 or more devices present"},
517
	{0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
518
	"9022: Exposed array is missing a required device"},
519
	{0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
520
	"9023: Array member(s) not at required physical locations"},
521
	{0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
522
	"9024: Array not functional due to present hardware configuration"},
523
	{0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
524
	"9026: Array not functional due to present hardware configuration"},
525
	{0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
526
	"9027: Array is missing a device and parity is out of sync"},
527
	{0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
528
	"9028: Maximum number of arrays already exist"},
529
	{0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
530
	"9050: Required cache data cannot be located for a disk unit"},
531
	{0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
532
	"9052: Cache data exists for a device that has been modified"},
533
	{0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
534
	"9054: IOA resources not available due to previous problems"},
535
	{0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
536
	"9092: Disk unit requires initialization before use"},
537
	{0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
538
	"9029: Incorrect hardware configuration change has been detected"},
539
	{0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
540
	"9060: One or more disk pairs are missing from an array"},
541
	{0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
542
	"9061: One or more disks are missing from an array"},
543
	{0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
544
	"9062: One or more disks are missing from an array"},
545
	{0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
L
Linus Torvalds 已提交
546
	"9063: Maximum number of functional arrays has been exceeded"},
547 548
	{0x07279A00, 0, 0,
	"Data protect, other volume set problem"},
L
Linus Torvalds 已提交
549 550
	{0x0B260000, 0, 0,
	"Aborted command, invalid descriptor"},
551 552 553 554
	{0x0B3F9000, 0, 0,
	"Target operating conditions have changed, dual adapter takeover"},
	{0x0B530200, 0, 0,
	"Aborted command, medium removal prevented"},
L
Linus Torvalds 已提交
555
	{0x0B5A0000, 0, 0,
556 557 558
	"Command terminated by host"},
	{0x0B5B8000, 0, 0,
	"Aborted command, command terminated by host"}
L
Linus Torvalds 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
};

static const struct ipr_ses_table_entry ipr_ses_table[] = {
	{ "2104-DL1        ", "XXXXXXXXXXXXXXXX", 80 },
	{ "2104-TL1        ", "XXXXXXXXXXXXXXXX", 80 },
	{ "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
	{ "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
	{ "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
	{ "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
	{ "2104-DU3        ", "XXXXXXXXXXXXXXXX", 160 },
	{ "2104-TU3        ", "XXXXXXXXXXXXXXXX", 160 },
	{ "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
	{ "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
	{ "St  V1S2        ", "XXXXXXXXXXXXXXXX", 160 },
	{ "HSBPD4M  PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
	{ "VSBPD1H   U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
};

/*
 *  Function Prototypes
 */
static int ipr_reset_alert(struct ipr_cmnd *);
static void ipr_process_ccn(struct ipr_cmnd *);
static void ipr_process_error(struct ipr_cmnd *);
static void ipr_reset_ioa_job(struct ipr_cmnd *);
static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
				   enum ipr_shutdown_type);

#ifdef CONFIG_SCSI_IPR_TRACE
/**
 * ipr_trc_hook - Add a trace entry to the driver trace
 * @ipr_cmd:	ipr command struct
 * @type:		trace type
 * @add_data:	additional data
 *
 * Return value:
 * 	none
 **/
static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
			 u8 type, u32 add_data)
{
	struct ipr_trace_entry *trace_entry;
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
B
Brian King 已提交
602
	unsigned int trace_index;
L
Linus Torvalds 已提交
603

B
Brian King 已提交
604 605
	trace_index = atomic_add_return(1, &ioa_cfg->trace_index) & IPR_TRACE_INDEX_MASK;
	trace_entry = &ioa_cfg->trace[trace_index];
L
Linus Torvalds 已提交
606 607 608
	trace_entry->time = jiffies;
	trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
	trace_entry->type = type;
609 610 611 612
	if (ipr_cmd->ioa_cfg->sis64)
		trace_entry->ata_op_code = ipr_cmd->i.ata_ioadl.regs.command;
	else
		trace_entry->ata_op_code = ipr_cmd->ioarcb.u.add_data.u.regs.command;
613
	trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
L
Linus Torvalds 已提交
614 615
	trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
	trace_entry->u.add_data = add_data;
616
	wmb();
L
Linus Torvalds 已提交
617 618
}
#else
619
#define ipr_trc_hook(ipr_cmd, type, add_data) do { } while (0)
L
Linus Torvalds 已提交
620 621
#endif

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
/**
 * ipr_lock_and_done - Acquire lock and complete command
 * @ipr_cmd:	ipr command struct
 *
 * Return value:
 *	none
 **/
static void ipr_lock_and_done(struct ipr_cmnd *ipr_cmd)
{
	unsigned long lock_flags;
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	ipr_cmd->done(ipr_cmd);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
}

L
Linus Torvalds 已提交
639 640 641 642 643 644 645 646 647 648
/**
 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
 * @ipr_cmd:	ipr command struct
 *
 * Return value:
 * 	none
 **/
static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
649 650
	struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
	struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
651
	dma_addr_t dma_addr = ipr_cmd->dma_addr;
652
	int hrrq_id;
L
Linus Torvalds 已提交
653

654
	hrrq_id = ioarcb->cmd_pkt.hrrq_id;
L
Linus Torvalds 已提交
655
	memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
656
	ioarcb->cmd_pkt.hrrq_id = hrrq_id;
657
	ioarcb->data_transfer_length = 0;
L
Linus Torvalds 已提交
658
	ioarcb->read_data_transfer_length = 0;
659
	ioarcb->ioadl_len = 0;
L
Linus Torvalds 已提交
660
	ioarcb->read_ioadl_len = 0;
661

662
	if (ipr_cmd->ioa_cfg->sis64) {
663 664
		ioarcb->u.sis64_addr_data.data_ioadl_addr =
			cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
665 666
		ioasa64->u.gata.status = 0;
	} else {
667 668 669
		ioarcb->write_ioadl_addr =
			cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
		ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
670
		ioasa->u.gata.status = 0;
671 672
	}

673 674
	ioasa->hdr.ioasc = 0;
	ioasa->hdr.residual_data_len = 0;
L
Linus Torvalds 已提交
675
	ipr_cmd->scsi_cmd = NULL;
676
	ipr_cmd->qc = NULL;
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685 686 687
	ipr_cmd->sense_buffer[0] = 0;
	ipr_cmd->dma_use_sg = 0;
}

/**
 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
 * @ipr_cmd:	ipr command struct
 *
 * Return value:
 * 	none
 **/
688 689
static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd,
			      void (*fast_done) (struct ipr_cmnd *))
L
Linus Torvalds 已提交
690 691 692 693
{
	ipr_reinit_ipr_cmnd(ipr_cmd);
	ipr_cmd->u.scratch = 0;
	ipr_cmd->sibling = NULL;
694
	ipr_cmd->eh_comp = NULL;
695
	ipr_cmd->fast_done = fast_done;
L
Linus Torvalds 已提交
696 697 698 699
	init_timer(&ipr_cmd->timer);
}

/**
700
 * __ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
L
Linus Torvalds 已提交
701 702 703 704 705 706
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 * 	pointer to ipr command struct
 **/
static
707
struct ipr_cmnd *__ipr_get_free_ipr_cmnd(struct ipr_hrr_queue *hrrq)
L
Linus Torvalds 已提交
708
{
709 710 711 712 713 714 715
	struct ipr_cmnd *ipr_cmd = NULL;

	if (likely(!list_empty(&hrrq->hrrq_free_q))) {
		ipr_cmd = list_entry(hrrq->hrrq_free_q.next,
			struct ipr_cmnd, queue);
		list_del(&ipr_cmd->queue);
	}
L
Linus Torvalds 已提交
716 717 718 719 720


	return ipr_cmd;
}

721 722 723 724 725 726 727 728 729 730
/**
 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block and initialize it
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 *	pointer to ipr command struct
 **/
static
struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
{
731 732
	struct ipr_cmnd *ipr_cmd =
		__ipr_get_free_ipr_cmnd(&ioa_cfg->hrrq[IPR_INIT_HRRQ]);
733
	ipr_init_ipr_cmnd(ipr_cmd, ipr_lock_and_done);
734 735 736
	return ipr_cmd;
}

L
Linus Torvalds 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
/**
 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
 * @ioa_cfg:	ioa config struct
 * @clr_ints:     interrupts to clear
 *
 * This function masks all interrupts on the adapter, then clears the
 * interrupts specified in the mask
 *
 * Return value:
 * 	none
 **/
static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
					  u32 clr_ints)
{
	volatile u32 int_reg;
752
	int i;
L
Linus Torvalds 已提交
753 754

	/* Stop new interrupts */
755 756 757 758 759 760
	for (i = 0; i < ioa_cfg->hrrq_num; i++) {
		spin_lock(&ioa_cfg->hrrq[i]._lock);
		ioa_cfg->hrrq[i].allow_interrupts = 0;
		spin_unlock(&ioa_cfg->hrrq[i]._lock);
	}
	wmb();
L
Linus Torvalds 已提交
761 762

	/* Set interrupt mask to stop all new interrupts */
763 764 765 766
	if (ioa_cfg->sis64)
		writeq(~0, ioa_cfg->regs.set_interrupt_mask_reg);
	else
		writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
L
Linus Torvalds 已提交
767 768

	/* Clear any pending interrupts */
769 770 771
	if (ioa_cfg->sis64)
		writel(~0, ioa_cfg->regs.clr_interrupt_reg);
	writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg32);
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785
	int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
}

/**
 * ipr_save_pcix_cmd_reg - Save PCI-X command register
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 * 	0 on success / -EIO on failure
 **/
static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
{
	int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);

786 787
	if (pcix_cmd_reg == 0)
		return 0;
L
Linus Torvalds 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820

	if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
				 &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
		dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
		return -EIO;
	}

	ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
	return 0;
}

/**
 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 * 	0 on success / -EIO on failure
 **/
static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
{
	int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);

	if (pcix_cmd_reg) {
		if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
					  ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
			dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
			return -EIO;
		}
	}

	return 0;
}

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
/**
 * ipr_sata_eh_done - done function for aborted SATA commands
 * @ipr_cmd:	ipr command struct
 *
 * This function is invoked for ops generated to SATA
 * devices which are being aborted.
 *
 * Return value:
 * 	none
 **/
static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
{
	struct ata_queued_cmd *qc = ipr_cmd->qc;
	struct ipr_sata_port *sata_port = qc->ap->private_data;

	qc->err_mask |= AC_ERR_OTHER;
	sata_port->ioasa.status |= ATA_BUSY;
838
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
839 840 841
	ata_qc_complete(qc);
}

L
Linus Torvalds 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
/**
 * ipr_scsi_eh_done - mid-layer done function for aborted ops
 * @ipr_cmd:	ipr command struct
 *
 * This function is invoked by the interrupt handler for
 * ops generated by the SCSI mid-layer which are being aborted.
 *
 * Return value:
 * 	none
 **/
static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
{
	struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;

	scsi_cmd->result |= (DID_ERROR << 16);

858
	scsi_dma_unmap(ipr_cmd->scsi_cmd);
L
Linus Torvalds 已提交
859
	scsi_cmd->scsi_done(scsi_cmd);
860 861
	if (ipr_cmd->eh_comp)
		complete(ipr_cmd->eh_comp);
862
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
L
Linus Torvalds 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876
}

/**
 * ipr_fail_all_ops - Fails all outstanding ops.
 * @ioa_cfg:	ioa config struct
 *
 * This function fails all outstanding ops.
 *
 * Return value:
 * 	none
 **/
static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
{
	struct ipr_cmnd *ipr_cmd, *temp;
877
	struct ipr_hrr_queue *hrrq;
L
Linus Torvalds 已提交
878 879

	ENTER;
880
	for_each_hrrq(hrrq, ioa_cfg) {
881
		spin_lock(&hrrq->_lock);
882 883 884
		list_for_each_entry_safe(ipr_cmd,
					temp, &hrrq->hrrq_pending_q, queue) {
			list_del(&ipr_cmd->queue);
L
Linus Torvalds 已提交
885

886 887 888 889
			ipr_cmd->s.ioasa.hdr.ioasc =
				cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
			ipr_cmd->s.ioasa.hdr.ilid =
				cpu_to_be32(IPR_DRIVER_ILID);
L
Linus Torvalds 已提交
890

891 892 893 894
			if (ipr_cmd->scsi_cmd)
				ipr_cmd->done = ipr_scsi_eh_done;
			else if (ipr_cmd->qc)
				ipr_cmd->done = ipr_sata_eh_done;
L
Linus Torvalds 已提交
895

896 897 898 899 900
			ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH,
				     IPR_IOASC_IOA_WAS_RESET);
			del_timer(&ipr_cmd->timer);
			ipr_cmd->done(ipr_cmd);
		}
901
		spin_unlock(&hrrq->_lock);
L
Linus Torvalds 已提交
902 903 904 905
	}
	LEAVE;
}

906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
/**
 * ipr_send_command -  Send driver initiated requests.
 * @ipr_cmd:		ipr command struct
 *
 * This function sends a command to the adapter using the correct write call.
 * In the case of sis64, calculate the ioarcb size required. Then or in the
 * appropriate bits.
 *
 * Return value:
 * 	none
 **/
static void ipr_send_command(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	dma_addr_t send_dma_addr = ipr_cmd->dma_addr;

	if (ioa_cfg->sis64) {
		/* The default size is 256 bytes */
		send_dma_addr |= 0x1;

		/* If the number of ioadls * size of ioadl > 128 bytes,
		   then use a 512 byte ioarcb */
		if (ipr_cmd->dma_use_sg * sizeof(struct ipr_ioadl64_desc) > 128 )
			send_dma_addr |= 0x4;
		writeq(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
	} else
		writel(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
}

L
Linus Torvalds 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
/**
 * ipr_do_req -  Send driver initiated requests.
 * @ipr_cmd:		ipr command struct
 * @done:			done function
 * @timeout_func:	timeout function
 * @timeout:		timeout value
 *
 * This function sends the specified command to the adapter with the
 * timeout given. The done function is invoked on command completion.
 *
 * Return value:
 * 	none
 **/
static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
		       void (*done) (struct ipr_cmnd *),
		       void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
{
952
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
L
Linus Torvalds 已提交
953 954 955 956 957 958 959 960 961 962 963

	ipr_cmd->done = done;

	ipr_cmd->timer.data = (unsigned long) ipr_cmd;
	ipr_cmd->timer.expires = jiffies + timeout;
	ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;

	add_timer(&ipr_cmd->timer);

	ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);

964
	ipr_send_command(ipr_cmd);
L
Linus Torvalds 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
}

/**
 * ipr_internal_cmd_done - Op done function for an internally generated op.
 * @ipr_cmd:	ipr command struct
 *
 * This function is the op done function for an internally generated,
 * blocking op. It simply wakes the sleeping thread.
 *
 * Return value:
 * 	none
 **/
static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
{
	if (ipr_cmd->sibling)
		ipr_cmd->sibling = NULL;
	else
		complete(&ipr_cmd->completion);
}

985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
/**
 * ipr_init_ioadl - initialize the ioadl for the correct SIS type
 * @ipr_cmd:	ipr command struct
 * @dma_addr:	dma address
 * @len:	transfer length
 * @flags:	ioadl flag value
 *
 * This function initializes an ioadl in the case where there is only a single
 * descriptor.
 *
 * Return value:
 * 	nothing
 **/
static void ipr_init_ioadl(struct ipr_cmnd *ipr_cmd, dma_addr_t dma_addr,
			   u32 len, int flags)
{
	struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
	struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;

	ipr_cmd->dma_use_sg = 1;

	if (ipr_cmd->ioa_cfg->sis64) {
		ioadl64->flags = cpu_to_be32(flags);
		ioadl64->data_len = cpu_to_be32(len);
		ioadl64->address = cpu_to_be64(dma_addr);

		ipr_cmd->ioarcb.ioadl_len =
		       	cpu_to_be32(sizeof(struct ipr_ioadl64_desc));
		ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
	} else {
		ioadl->flags_and_data_len = cpu_to_be32(flags | len);
		ioadl->address = cpu_to_be32(dma_addr);

		if (flags == IPR_IOADL_FLAGS_READ_LAST) {
			ipr_cmd->ioarcb.read_ioadl_len =
				cpu_to_be32(sizeof(struct ipr_ioadl_desc));
			ipr_cmd->ioarcb.read_data_transfer_length = cpu_to_be32(len);
		} else {
			ipr_cmd->ioarcb.ioadl_len =
			       	cpu_to_be32(sizeof(struct ipr_ioadl_desc));
			ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
		}
	}
}

L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
/**
 * ipr_send_blocking_cmd - Send command and sleep on its completion.
 * @ipr_cmd:	ipr command struct
 * @timeout_func:	function to invoke if command times out
 * @timeout:	timeout
 *
 * Return value:
 * 	none
 **/
static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
				  void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
				  u32 timeout)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	init_completion(&ipr_cmd->completion);
	ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);

	spin_unlock_irq(ioa_cfg->host->host_lock);
	wait_for_completion(&ipr_cmd->completion);
	spin_lock_irq(ioa_cfg->host->host_lock);
}

1053 1054
static int ipr_get_hrrq_index(struct ipr_ioa_cfg *ioa_cfg)
{
1055 1056
	unsigned int hrrq;

1057
	if (ioa_cfg->hrrq_num == 1)
1058 1059 1060 1061 1062 1063
		hrrq = 0;
	else {
		hrrq = atomic_add_return(1, &ioa_cfg->hrrq_index);
		hrrq = (hrrq % (ioa_cfg->hrrq_num - 1)) + 1;
	}
	return hrrq;
1064 1065
}

L
Linus Torvalds 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
/**
 * ipr_send_hcam - Send an HCAM to the adapter.
 * @ioa_cfg:	ioa config struct
 * @type:		HCAM type
 * @hostrcb:	hostrcb struct
 *
 * This function will send a Host Controlled Async command to the adapter.
 * If HCAMs are currently not allowed to be issued to the adapter, it will
 * place the hostrcb on the free queue.
 *
 * Return value:
 * 	none
 **/
static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
			  struct ipr_hostrcb *hostrcb)
{
	struct ipr_cmnd *ipr_cmd;
	struct ipr_ioarcb *ioarcb;

1085
	if (ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds) {
L
Linus Torvalds 已提交
1086
		ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
1087
		list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
L
Linus Torvalds 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
		list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);

		ipr_cmd->u.hostrcb = hostrcb;
		ioarcb = &ipr_cmd->ioarcb;

		ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
		ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
		ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
		ioarcb->cmd_pkt.cdb[1] = type;
		ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
		ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;

1100 1101
		ipr_init_ioadl(ipr_cmd, hostrcb->hostrcb_dma,
			       sizeof(hostrcb->hcam), IPR_IOADL_FLAGS_READ_LAST);
L
Linus Torvalds 已提交
1102 1103 1104 1105 1106 1107 1108 1109

		if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
			ipr_cmd->done = ipr_process_ccn;
		else
			ipr_cmd->done = ipr_process_error;

		ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);

1110
		ipr_send_command(ipr_cmd);
L
Linus Torvalds 已提交
1111 1112 1113 1114 1115
	} else {
		list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
	}
}

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
/**
 * ipr_update_ata_class - Update the ata class in the resource entry
 * @res:	resource entry struct
 * @proto:	cfgte device bus protocol value
 *
 * Return value:
 * 	none
 **/
static void ipr_update_ata_class(struct ipr_resource_entry *res, unsigned int proto)
{
1126
	switch (proto) {
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	case IPR_PROTO_SATA:
	case IPR_PROTO_SAS_STP:
		res->ata_class = ATA_DEV_ATA;
		break;
	case IPR_PROTO_SATA_ATAPI:
	case IPR_PROTO_SAS_STP_ATAPI:
		res->ata_class = ATA_DEV_ATAPI;
		break;
	default:
		res->ata_class = ATA_DEV_UNKNOWN;
		break;
	};
}

L
Linus Torvalds 已提交
1141 1142 1143
/**
 * ipr_init_res_entry - Initialize a resource entry struct.
 * @res:	resource entry struct
1144
 * @cfgtew:	config table entry wrapper struct
L
Linus Torvalds 已提交
1145 1146 1147 1148
 *
 * Return value:
 * 	none
 **/
1149 1150
static void ipr_init_res_entry(struct ipr_resource_entry *res,
			       struct ipr_config_table_entry_wrapper *cfgtew)
L
Linus Torvalds 已提交
1151
{
1152 1153 1154 1155 1156
	int found = 0;
	unsigned int proto;
	struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
	struct ipr_resource_entry *gscsi_res = NULL;

1157
	res->needs_sync_complete = 0;
L
Linus Torvalds 已提交
1158 1159 1160 1161
	res->in_erp = 0;
	res->add_to_ml = 0;
	res->del_from_ml = 0;
	res->resetting_device = 0;
1162
	res->reset_occurred = 0;
L
Linus Torvalds 已提交
1163
	res->sdev = NULL;
1164
	res->sata_port = NULL;
1165 1166 1167 1168 1169

	if (ioa_cfg->sis64) {
		proto = cfgtew->u.cfgte64->proto;
		res->res_flags = cfgtew->u.cfgte64->res_flags;
		res->qmodel = IPR_QUEUEING_MODEL64(res);
1170
		res->type = cfgtew->u.cfgte64->res_type;
1171 1172 1173 1174 1175

		memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
			sizeof(res->res_path));

		res->bus = 0;
1176 1177
		memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
			sizeof(res->dev_lun.scsi_lun));
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
		res->lun = scsilun_to_int(&res->dev_lun);

		if (res->type == IPR_RES_TYPE_GENERIC_SCSI) {
			list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue) {
				if (gscsi_res->dev_id == cfgtew->u.cfgte64->dev_id) {
					found = 1;
					res->target = gscsi_res->target;
					break;
				}
			}
			if (!found) {
				res->target = find_first_zero_bit(ioa_cfg->target_ids,
								  ioa_cfg->max_devs_supported);
				set_bit(res->target, ioa_cfg->target_ids);
			}
		} else if (res->type == IPR_RES_TYPE_IOAFP) {
			res->bus = IPR_IOAFP_VIRTUAL_BUS;
			res->target = 0;
		} else if (res->type == IPR_RES_TYPE_ARRAY) {
			res->bus = IPR_ARRAY_VIRTUAL_BUS;
			res->target = find_first_zero_bit(ioa_cfg->array_ids,
							  ioa_cfg->max_devs_supported);
			set_bit(res->target, ioa_cfg->array_ids);
		} else if (res->type == IPR_RES_TYPE_VOLUME_SET) {
			res->bus = IPR_VSET_VIRTUAL_BUS;
			res->target = find_first_zero_bit(ioa_cfg->vset_ids,
							  ioa_cfg->max_devs_supported);
			set_bit(res->target, ioa_cfg->vset_ids);
		} else {
			res->target = find_first_zero_bit(ioa_cfg->target_ids,
							  ioa_cfg->max_devs_supported);
			set_bit(res->target, ioa_cfg->target_ids);
		}
	} else {
		proto = cfgtew->u.cfgte->proto;
		res->qmodel = IPR_QUEUEING_MODEL(res);
		res->flags = cfgtew->u.cfgte->flags;
		if (res->flags & IPR_IS_IOA_RESOURCE)
			res->type = IPR_RES_TYPE_IOAFP;
		else
			res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;

		res->bus = cfgtew->u.cfgte->res_addr.bus;
		res->target = cfgtew->u.cfgte->res_addr.target;
		res->lun = cfgtew->u.cfgte->res_addr.lun;
1223
		res->lun_wwn = get_unaligned_be64(cfgtew->u.cfgte->lun_wwn);
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
	}

	ipr_update_ata_class(res, proto);
}

/**
 * ipr_is_same_device - Determine if two devices are the same.
 * @res:	resource entry struct
 * @cfgtew:	config table entry wrapper struct
 *
 * Return value:
 * 	1 if the devices are the same / 0 otherwise
 **/
static int ipr_is_same_device(struct ipr_resource_entry *res,
			      struct ipr_config_table_entry_wrapper *cfgtew)
{
	if (res->ioa_cfg->sis64) {
		if (!memcmp(&res->dev_id, &cfgtew->u.cfgte64->dev_id,
					sizeof(cfgtew->u.cfgte64->dev_id)) &&
1243
			!memcmp(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
					sizeof(cfgtew->u.cfgte64->lun))) {
			return 1;
		}
	} else {
		if (res->bus == cfgtew->u.cfgte->res_addr.bus &&
		    res->target == cfgtew->u.cfgte->res_addr.target &&
		    res->lun == cfgtew->u.cfgte->res_addr.lun)
			return 1;
	}

	return 0;
}

/**
1258
 * __ipr_format_res_path - Format the resource path for printing.
1259 1260
 * @res_path:	resource path
 * @buf:	buffer
1261
 * @len:	length of buffer provided
1262 1263 1264 1265
 *
 * Return value:
 * 	pointer to buffer
 **/
1266
static char *__ipr_format_res_path(u8 *res_path, char *buffer, int len)
1267 1268
{
	int i;
1269
	char *p = buffer;
1270

1271
	*p = '\0';
1272 1273 1274
	p += snprintf(p, buffer + len - p, "%02X", res_path[0]);
	for (i = 1; res_path[i] != 0xff && ((i * 3) < len); i++)
		p += snprintf(p, buffer + len - p, "-%02X", res_path[i]);
1275 1276 1277 1278

	return buffer;
}

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
/**
 * ipr_format_res_path - Format the resource path for printing.
 * @ioa_cfg:	ioa config struct
 * @res_path:	resource path
 * @buf:	buffer
 * @len:	length of buffer provided
 *
 * Return value:
 *	pointer to buffer
 **/
static char *ipr_format_res_path(struct ipr_ioa_cfg *ioa_cfg,
				 u8 *res_path, char *buffer, int len)
{
	char *p = buffer;

	*p = '\0';
	p += snprintf(p, buffer + len - p, "%d/", ioa_cfg->host->host_no);
	__ipr_format_res_path(res_path, p, len - (buffer - p));
	return buffer;
}

1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
/**
 * ipr_update_res_entry - Update the resource entry.
 * @res:	resource entry struct
 * @cfgtew:	config table entry wrapper struct
 *
 * Return value:
 *      none
 **/
static void ipr_update_res_entry(struct ipr_resource_entry *res,
				 struct ipr_config_table_entry_wrapper *cfgtew)
{
	char buffer[IPR_MAX_RES_PATH_LENGTH];
	unsigned int proto;
	int new_path = 0;

	if (res->ioa_cfg->sis64) {
		res->flags = cfgtew->u.cfgte64->flags;
		res->res_flags = cfgtew->u.cfgte64->res_flags;
1318
		res->type = cfgtew->u.cfgte64->res_type;
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339

		memcpy(&res->std_inq_data, &cfgtew->u.cfgte64->std_inq_data,
			sizeof(struct ipr_std_inq_data));

		res->qmodel = IPR_QUEUEING_MODEL64(res);
		proto = cfgtew->u.cfgte64->proto;
		res->res_handle = cfgtew->u.cfgte64->res_handle;
		res->dev_id = cfgtew->u.cfgte64->dev_id;

		memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
			sizeof(res->dev_lun.scsi_lun));

		if (memcmp(res->res_path, &cfgtew->u.cfgte64->res_path,
					sizeof(res->res_path))) {
			memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
				sizeof(res->res_path));
			new_path = 1;
		}

		if (res->sdev && new_path)
			sdev_printk(KERN_INFO, res->sdev, "Resource path: %s\n",
1340 1341
				    ipr_format_res_path(res->ioa_cfg,
					res->res_path, buffer, sizeof(buffer)));
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
	} else {
		res->flags = cfgtew->u.cfgte->flags;
		if (res->flags & IPR_IS_IOA_RESOURCE)
			res->type = IPR_RES_TYPE_IOAFP;
		else
			res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;

		memcpy(&res->std_inq_data, &cfgtew->u.cfgte->std_inq_data,
			sizeof(struct ipr_std_inq_data));

		res->qmodel = IPR_QUEUEING_MODEL(res);
		proto = cfgtew->u.cfgte->proto;
		res->res_handle = cfgtew->u.cfgte->res_handle;
	}

	ipr_update_ata_class(res, proto);
}

/**
 * ipr_clear_res_target - Clear the bit in the bit map representing the target
 * 			  for the resource.
 * @res:	resource entry struct
 * @cfgtew:	config table entry wrapper struct
 *
 * Return value:
 *      none
 **/
static void ipr_clear_res_target(struct ipr_resource_entry *res)
{
	struct ipr_resource_entry *gscsi_res = NULL;
	struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;

	if (!ioa_cfg->sis64)
		return;

	if (res->bus == IPR_ARRAY_VIRTUAL_BUS)
		clear_bit(res->target, ioa_cfg->array_ids);
	else if (res->bus == IPR_VSET_VIRTUAL_BUS)
		clear_bit(res->target, ioa_cfg->vset_ids);
	else if (res->bus == 0 && res->type == IPR_RES_TYPE_GENERIC_SCSI) {
		list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue)
			if (gscsi_res->dev_id == res->dev_id && gscsi_res != res)
				return;
		clear_bit(res->target, ioa_cfg->target_ids);

	} else if (res->bus == 0)
		clear_bit(res->target, ioa_cfg->target_ids);
L
Linus Torvalds 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
}

/**
 * ipr_handle_config_change - Handle a config change from the adapter
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb
 *
 * Return value:
 * 	none
 **/
static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
1400
				     struct ipr_hostrcb *hostrcb)
L
Linus Torvalds 已提交
1401 1402
{
	struct ipr_resource_entry *res = NULL;
1403 1404 1405
	struct ipr_config_table_entry_wrapper cfgtew;
	__be32 cc_res_handle;

L
Linus Torvalds 已提交
1406 1407
	u32 is_ndn = 1;

1408 1409 1410 1411 1412 1413 1414
	if (ioa_cfg->sis64) {
		cfgtew.u.cfgte64 = &hostrcb->hcam.u.ccn.u.cfgte64;
		cc_res_handle = cfgtew.u.cfgte64->res_handle;
	} else {
		cfgtew.u.cfgte = &hostrcb->hcam.u.ccn.u.cfgte;
		cc_res_handle = cfgtew.u.cfgte->res_handle;
	}
L
Linus Torvalds 已提交
1415 1416

	list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1417
		if (res->res_handle == cc_res_handle) {
L
Linus Torvalds 已提交
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
			is_ndn = 0;
			break;
		}
	}

	if (is_ndn) {
		if (list_empty(&ioa_cfg->free_res_q)) {
			ipr_send_hcam(ioa_cfg,
				      IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
				      hostrcb);
			return;
		}

		res = list_entry(ioa_cfg->free_res_q.next,
				 struct ipr_resource_entry, queue);

		list_del(&res->queue);
1435
		ipr_init_res_entry(res, &cfgtew);
L
Linus Torvalds 已提交
1436 1437 1438
		list_add_tail(&res->queue, &ioa_cfg->used_res_q);
	}

1439
	ipr_update_res_entry(res, &cfgtew);
L
Linus Torvalds 已提交
1440 1441 1442 1443

	if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
		if (res->sdev) {
			res->del_from_ml = 1;
1444
			res->res_handle = IPR_INVALID_RES_HANDLE;
1445
			schedule_work(&ioa_cfg->work_q);
1446 1447
		} else {
			ipr_clear_res_target(res);
L
Linus Torvalds 已提交
1448
			list_move_tail(&res->queue, &ioa_cfg->free_res_q);
1449
		}
1450
	} else if (!res->sdev || res->del_from_ml) {
L
Linus Torvalds 已提交
1451
		res->add_to_ml = 1;
1452
		schedule_work(&ioa_cfg->work_q);
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	}

	ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
}

/**
 * ipr_process_ccn - Op done function for a CCN.
 * @ipr_cmd:	ipr command struct
 *
 * This function is the op done function for a configuration
 * change notification host controlled async from the adapter.
 *
 * Return value:
 * 	none
 **/
static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1472
	u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
L
Linus Torvalds 已提交
1473 1474

	list_del(&hostrcb->queue);
1475
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
L
Linus Torvalds 已提交
1476 1477

	if (ioasc) {
B
Brian King 已提交
1478 1479
		if (ioasc != IPR_IOASC_IOA_WAS_RESET &&
		    ioasc != IPR_IOASC_ABORTED_CMD_TERM_BY_HOST)
L
Linus Torvalds 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488
			dev_err(&ioa_cfg->pdev->dev,
				"Host RCB failed with IOASC: 0x%08X\n", ioasc);

		ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
	} else {
		ipr_handle_config_change(ioa_cfg, hostrcb);
	}
}

1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
/**
 * strip_and_pad_whitespace - Strip and pad trailing whitespace.
 * @i:		index into buffer
 * @buf:		string to modify
 *
 * This function will strip all trailing whitespace, pad the end
 * of the string with a single space, and NULL terminate the string.
 *
 * Return value:
 * 	new length of string
 **/
static int strip_and_pad_whitespace(int i, char *buf)
{
	while (i && buf[i] == ' ')
		i--;
	buf[i+1] = ' ';
	buf[i+2] = '\0';
	return i + 2;
}

/**
 * ipr_log_vpd_compact - Log the passed extended VPD compactly.
 * @prefix:		string to print at start of printk
 * @hostrcb:	hostrcb pointer
 * @vpd:		vendor/product id/sn struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
				struct ipr_vpd *vpd)
{
	char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
	int i = 0;

	memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
	i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);

	memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
	i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);

	memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
	buffer[IPR_SERIAL_NUM_LEN + i] = '\0';

	ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
}

L
Linus Torvalds 已提交
1536 1537
/**
 * ipr_log_vpd - Log the passed VPD to the error log.
1538
 * @vpd:		vendor/product id/sn struct
L
Linus Torvalds 已提交
1539 1540 1541 1542
 *
 * Return value:
 * 	none
 **/
1543
static void ipr_log_vpd(struct ipr_vpd *vpd)
L
Linus Torvalds 已提交
1544 1545 1546 1547
{
	char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
		    + IPR_SERIAL_NUM_LEN];

1548 1549
	memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
	memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
L
Linus Torvalds 已提交
1550 1551 1552 1553
	       IPR_PROD_ID_LEN);
	buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
	ipr_err("Vendor/Product ID: %s\n", buffer);

1554
	memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
L
Linus Torvalds 已提交
1555 1556 1557 1558
	buffer[IPR_SERIAL_NUM_LEN] = '\0';
	ipr_err("    Serial Number: %s\n", buffer);
}

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
/**
 * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
 * @prefix:		string to print at start of printk
 * @hostrcb:	hostrcb pointer
 * @vpd:		vendor/product id/sn/wwn struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
				    struct ipr_ext_vpd *vpd)
{
	ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
	ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
		     be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
}

1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
/**
 * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
 * @vpd:		vendor/product id/sn/wwn struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
{
	ipr_log_vpd(&vpd->vpd);
	ipr_err("    WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
		be32_to_cpu(vpd->wwid[1]));
}

/**
 * ipr_log_enhanced_cache_error - Log a cache error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
					 struct ipr_hostrcb *hostrcb)
{
1601 1602 1603 1604 1605 1606
	struct ipr_hostrcb_type_12_error *error;

	if (ioa_cfg->sis64)
		error = &hostrcb->hcam.u.error64.u.type_12_error;
	else
		error = &hostrcb->hcam.u.error.u.type_12_error;
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625

	ipr_err("-----Current Configuration-----\n");
	ipr_err("Cache Directory Card Information:\n");
	ipr_log_ext_vpd(&error->ioa_vpd);
	ipr_err("Adapter Card Information:\n");
	ipr_log_ext_vpd(&error->cfc_vpd);

	ipr_err("-----Expected Configuration-----\n");
	ipr_err("Cache Directory Card Information:\n");
	ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
	ipr_err("Adapter Card Information:\n");
	ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);

	ipr_err("Additional IOA Data: %08X %08X %08X\n",
		     be32_to_cpu(error->ioa_data[0]),
		     be32_to_cpu(error->ioa_data[1]),
		     be32_to_cpu(error->ioa_data[2]));
}

L
Linus Torvalds 已提交
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
/**
 * ipr_log_cache_error - Log a cache error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
				struct ipr_hostrcb *hostrcb)
{
	struct ipr_hostrcb_type_02_error *error =
		&hostrcb->hcam.u.error.u.type_02_error;

	ipr_err("-----Current Configuration-----\n");
	ipr_err("Cache Directory Card Information:\n");
1642
	ipr_log_vpd(&error->ioa_vpd);
L
Linus Torvalds 已提交
1643
	ipr_err("Adapter Card Information:\n");
1644
	ipr_log_vpd(&error->cfc_vpd);
L
Linus Torvalds 已提交
1645 1646 1647

	ipr_err("-----Expected Configuration-----\n");
	ipr_err("Cache Directory Card Information:\n");
1648
	ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
L
Linus Torvalds 已提交
1649
	ipr_err("Adapter Card Information:\n");
1650
	ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
L
Linus Torvalds 已提交
1651 1652 1653 1654 1655 1656 1657

	ipr_err("Additional IOA Data: %08X %08X %08X\n",
		     be32_to_cpu(error->ioa_data[0]),
		     be32_to_cpu(error->ioa_data[1]),
		     be32_to_cpu(error->ioa_data[2]));
}

1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
/**
 * ipr_log_enhanced_config_error - Log a configuration error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
					  struct ipr_hostrcb *hostrcb)
{
	int errors_logged, i;
	struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
	struct ipr_hostrcb_type_13_error *error;

	error = &hostrcb->hcam.u.error.u.type_13_error;
	errors_logged = be32_to_cpu(error->errors_logged);

	ipr_err("Device Errors Detected/Logged: %d/%d\n",
		be32_to_cpu(error->errors_detected), errors_logged);

	dev_entry = error->dev;

	for (i = 0; i < errors_logged; i++, dev_entry++) {
		ipr_err_separator;

		ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
		ipr_log_ext_vpd(&dev_entry->vpd);

		ipr_err("-----New Device Information-----\n");
		ipr_log_ext_vpd(&dev_entry->new_vpd);

		ipr_err("Cache Directory Card Information:\n");
		ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);

		ipr_err("Adapter Card Information:\n");
		ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
	}
}

1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
/**
 * ipr_log_sis64_config_error - Log a device error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_sis64_config_error(struct ipr_ioa_cfg *ioa_cfg,
				       struct ipr_hostrcb *hostrcb)
{
	int errors_logged, i;
	struct ipr_hostrcb64_device_data_entry_enhanced *dev_entry;
	struct ipr_hostrcb_type_23_error *error;
	char buffer[IPR_MAX_RES_PATH_LENGTH];

	error = &hostrcb->hcam.u.error64.u.type_23_error;
	errors_logged = be32_to_cpu(error->errors_logged);

	ipr_err("Device Errors Detected/Logged: %d/%d\n",
		be32_to_cpu(error->errors_detected), errors_logged);

	dev_entry = error->dev;

	for (i = 0; i < errors_logged; i++, dev_entry++) {
		ipr_err_separator;

		ipr_err("Device %d : %s", i + 1,
1726 1727
			__ipr_format_res_path(dev_entry->res_path,
					      buffer, sizeof(buffer)));
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
		ipr_log_ext_vpd(&dev_entry->vpd);

		ipr_err("-----New Device Information-----\n");
		ipr_log_ext_vpd(&dev_entry->new_vpd);

		ipr_err("Cache Directory Card Information:\n");
		ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);

		ipr_err("Adapter Card Information:\n");
		ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
	}
}

L
Linus Torvalds 已提交
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
/**
 * ipr_log_config_error - Log a configuration error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
				 struct ipr_hostrcb *hostrcb)
{
	int errors_logged, i;
	struct ipr_hostrcb_device_data_entry *dev_entry;
	struct ipr_hostrcb_type_03_error *error;

	error = &hostrcb->hcam.u.error.u.type_03_error;
	errors_logged = be32_to_cpu(error->errors_logged);

	ipr_err("Device Errors Detected/Logged: %d/%d\n",
		be32_to_cpu(error->errors_detected), errors_logged);

1762
	dev_entry = error->dev;
L
Linus Torvalds 已提交
1763 1764 1765 1766

	for (i = 0; i < errors_logged; i++, dev_entry++) {
		ipr_err_separator;

1767
		ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1768
		ipr_log_vpd(&dev_entry->vpd);
L
Linus Torvalds 已提交
1769 1770

		ipr_err("-----New Device Information-----\n");
1771
		ipr_log_vpd(&dev_entry->new_vpd);
L
Linus Torvalds 已提交
1772 1773

		ipr_err("Cache Directory Card Information:\n");
1774
		ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
L
Linus Torvalds 已提交
1775 1776

		ipr_err("Adapter Card Information:\n");
1777
		ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
L
Linus Torvalds 已提交
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787

		ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
			be32_to_cpu(dev_entry->ioa_data[0]),
			be32_to_cpu(dev_entry->ioa_data[1]),
			be32_to_cpu(dev_entry->ioa_data[2]),
			be32_to_cpu(dev_entry->ioa_data[3]),
			be32_to_cpu(dev_entry->ioa_data[4]));
	}
}

1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
/**
 * ipr_log_enhanced_array_error - Log an array configuration error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
					 struct ipr_hostrcb *hostrcb)
{
	int i, num_entries;
	struct ipr_hostrcb_type_14_error *error;
	struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
	const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };

	error = &hostrcb->hcam.u.error.u.type_14_error;

	ipr_err_separator;

	ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
		error->protection_level,
		ioa_cfg->host->host_no,
		error->last_func_vset_res_addr.bus,
		error->last_func_vset_res_addr.target,
		error->last_func_vset_res_addr.lun);

	ipr_err_separator;

	array_entry = error->array_member;
	num_entries = min_t(u32, be32_to_cpu(error->num_entries),
W
Wayne Boyer 已提交
1819
			    ARRAY_SIZE(error->array_member));
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838

	for (i = 0; i < num_entries; i++, array_entry++) {
		if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
			continue;

		if (be32_to_cpu(error->exposed_mode_adn) == i)
			ipr_err("Exposed Array Member %d:\n", i);
		else
			ipr_err("Array Member %d:\n", i);

		ipr_log_ext_vpd(&array_entry->vpd);
		ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
		ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
				 "Expected Location");

		ipr_err_separator;
	}
}

L
Linus Torvalds 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
/**
 * ipr_log_array_error - Log an array configuration error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
				struct ipr_hostrcb *hostrcb)
{
	int i;
	struct ipr_hostrcb_type_04_error *error;
	struct ipr_hostrcb_array_data_entry *array_entry;
	const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };

	error = &hostrcb->hcam.u.error.u.type_04_error;

	ipr_err_separator;

	ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
		error->protection_level,
		ioa_cfg->host->host_no,
		error->last_func_vset_res_addr.bus,
		error->last_func_vset_res_addr.target,
		error->last_func_vset_res_addr.lun);

	ipr_err_separator;

	array_entry = error->array_member;

	for (i = 0; i < 18; i++) {
1871
		if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
L
Linus Torvalds 已提交
1872 1873
			continue;

1874
		if (be32_to_cpu(error->exposed_mode_adn) == i)
L
Linus Torvalds 已提交
1875
			ipr_err("Exposed Array Member %d:\n", i);
1876
		else
L
Linus Torvalds 已提交
1877 1878
			ipr_err("Array Member %d:\n", i);

1879
		ipr_log_vpd(&array_entry->vpd);
L
Linus Torvalds 已提交
1880

1881 1882 1883
		ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
		ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
				 "Expected Location");
L
Linus Torvalds 已提交
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894

		ipr_err_separator;

		if (i == 9)
			array_entry = error->array_member2;
		else
			array_entry++;
	}
}

/**
1895
 * ipr_log_hex_data - Log additional hex IOA error data.
1896
 * @ioa_cfg:	ioa config struct
1897 1898
 * @data:		IOA error data
 * @len:		data length
L
Linus Torvalds 已提交
1899 1900 1901 1902
 *
 * Return value:
 * 	none
 **/
1903
static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
L
Linus Torvalds 已提交
1904 1905 1906
{
	int i;

1907
	if (len == 0)
L
Linus Torvalds 已提交
1908 1909
		return;

1910 1911 1912
	if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
		len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);

1913
	for (i = 0; i < len / 4; i += 4) {
L
Linus Torvalds 已提交
1914
		ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1915 1916 1917 1918
			be32_to_cpu(data[i]),
			be32_to_cpu(data[i+1]),
			be32_to_cpu(data[i+2]),
			be32_to_cpu(data[i+3]));
L
Linus Torvalds 已提交
1919 1920 1921
	}
}

1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
/**
 * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
					    struct ipr_hostrcb *hostrcb)
{
	struct ipr_hostrcb_type_17_error *error;

1935 1936 1937 1938 1939
	if (ioa_cfg->sis64)
		error = &hostrcb->hcam.u.error64.u.type_17_error;
	else
		error = &hostrcb->hcam.u.error.u.type_17_error;

1940
	error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1941
	strim(error->failure_reason);
1942

1943 1944 1945
	ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
		     be32_to_cpu(hostrcb->hcam.u.error.prc));
	ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1946
	ipr_log_hex_data(ioa_cfg, error->data,
1947 1948 1949 1950 1951
			 be32_to_cpu(hostrcb->hcam.length) -
			 (offsetof(struct ipr_hostrcb_error, u) +
			  offsetof(struct ipr_hostrcb_type_17_error, data)));
}

1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
/**
 * ipr_log_dual_ioa_error - Log a dual adapter error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
				   struct ipr_hostrcb *hostrcb)
{
	struct ipr_hostrcb_type_07_error *error;

	error = &hostrcb->hcam.u.error.u.type_07_error;
	error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1967
	strim(error->failure_reason);
1968

1969 1970 1971
	ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
		     be32_to_cpu(hostrcb->hcam.u.error.prc));
	ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
1972
	ipr_log_hex_data(ioa_cfg, error->data,
1973 1974 1975 1976 1977
			 be32_to_cpu(hostrcb->hcam.length) -
			 (offsetof(struct ipr_hostrcb_error, u) +
			  offsetof(struct ipr_hostrcb_type_07_error, data)));
}

1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
static const struct {
	u8 active;
	char *desc;
} path_active_desc[] = {
	{ IPR_PATH_NO_INFO, "Path" },
	{ IPR_PATH_ACTIVE, "Active path" },
	{ IPR_PATH_NOT_ACTIVE, "Inactive path" }
};

static const struct {
	u8 state;
	char *desc;
} path_state_desc[] = {
	{ IPR_PATH_STATE_NO_INFO, "has no path state information available" },
	{ IPR_PATH_HEALTHY, "is healthy" },
	{ IPR_PATH_DEGRADED, "is degraded" },
	{ IPR_PATH_FAILED, "is failed" }
};

/**
 * ipr_log_fabric_path - Log a fabric path error
 * @hostrcb:	hostrcb struct
 * @fabric:		fabric descriptor
 *
 * Return value:
 * 	none
 **/
static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
				struct ipr_hostrcb_fabric_desc *fabric)
{
	int i, j;
	u8 path_state = fabric->path_state;
	u8 active = path_state & IPR_PATH_ACTIVE_MASK;
	u8 state = path_state & IPR_PATH_STATE_MASK;

	for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
		if (path_active_desc[i].active != active)
			continue;

		for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
			if (path_state_desc[j].state != state)
				continue;

			if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
				ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
					     path_active_desc[i].desc, path_state_desc[j].desc,
					     fabric->ioa_port);
			} else if (fabric->cascaded_expander == 0xff) {
				ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
					     path_active_desc[i].desc, path_state_desc[j].desc,
					     fabric->ioa_port, fabric->phy);
			} else if (fabric->phy == 0xff) {
				ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
					     path_active_desc[i].desc, path_state_desc[j].desc,
					     fabric->ioa_port, fabric->cascaded_expander);
			} else {
				ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
					     path_active_desc[i].desc, path_state_desc[j].desc,
					     fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
			}
			return;
		}
	}

	ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
		fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
}

2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
/**
 * ipr_log64_fabric_path - Log a fabric path error
 * @hostrcb:	hostrcb struct
 * @fabric:		fabric descriptor
 *
 * Return value:
 * 	none
 **/
static void ipr_log64_fabric_path(struct ipr_hostrcb *hostrcb,
				  struct ipr_hostrcb64_fabric_desc *fabric)
{
	int i, j;
	u8 path_state = fabric->path_state;
	u8 active = path_state & IPR_PATH_ACTIVE_MASK;
	u8 state = path_state & IPR_PATH_STATE_MASK;
	char buffer[IPR_MAX_RES_PATH_LENGTH];

	for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
		if (path_active_desc[i].active != active)
			continue;

		for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
			if (path_state_desc[j].state != state)
				continue;

			ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s\n",
				     path_active_desc[i].desc, path_state_desc[j].desc,
2073 2074 2075
				     ipr_format_res_path(hostrcb->ioa_cfg,
						fabric->res_path,
						buffer, sizeof(buffer)));
2076 2077 2078 2079 2080
			return;
		}
	}

	ipr_err("Path state=%02X Resource Path=%s\n", path_state,
2081 2082
		ipr_format_res_path(hostrcb->ioa_cfg, fabric->res_path,
				    buffer, sizeof(buffer)));
2083 2084
}

2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
static const struct {
	u8 type;
	char *desc;
} path_type_desc[] = {
	{ IPR_PATH_CFG_IOA_PORT, "IOA port" },
	{ IPR_PATH_CFG_EXP_PORT, "Expander port" },
	{ IPR_PATH_CFG_DEVICE_PORT, "Device port" },
	{ IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
};

static const struct {
	u8 status;
	char *desc;
} path_status_desc[] = {
	{ IPR_PATH_CFG_NO_PROB, "Functional" },
	{ IPR_PATH_CFG_DEGRADED, "Degraded" },
	{ IPR_PATH_CFG_FAILED, "Failed" },
	{ IPR_PATH_CFG_SUSPECT, "Suspect" },
	{ IPR_PATH_NOT_DETECTED, "Missing" },
	{ IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
};

static const char *link_rate[] = {
	"unknown",
	"disabled",
	"phy reset problem",
	"spinup hold",
	"port selector",
	"unknown",
	"unknown",
	"unknown",
	"1.5Gbps",
	"3.0Gbps",
	"unknown",
	"unknown",
	"unknown",
	"unknown",
	"unknown",
	"unknown"
};

/**
 * ipr_log_path_elem - Log a fabric path element.
 * @hostrcb:	hostrcb struct
 * @cfg:		fabric path element struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
			      struct ipr_hostrcb_config_element *cfg)
{
	int i, j;
	u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
	u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;

	if (type == IPR_PATH_CFG_NOT_EXIST)
		return;

	for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
		if (path_type_desc[i].type != type)
			continue;

		for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
			if (path_status_desc[j].status != status)
				continue;

			if (type == IPR_PATH_CFG_IOA_PORT) {
				ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
					     path_status_desc[j].desc, path_type_desc[i].desc,
					     cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
					     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
			} else {
				if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
					ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
						     path_status_desc[j].desc, path_type_desc[i].desc,
						     link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
						     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
				} else if (cfg->cascaded_expander == 0xff) {
					ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
						     "WWN=%08X%08X\n", path_status_desc[j].desc,
						     path_type_desc[i].desc, cfg->phy,
						     link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
						     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
				} else if (cfg->phy == 0xff) {
					ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
						     "WWN=%08X%08X\n", path_status_desc[j].desc,
						     path_type_desc[i].desc, cfg->cascaded_expander,
						     link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
						     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
				} else {
					ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
						     "WWN=%08X%08X\n", path_status_desc[j].desc,
						     path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
						     link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
						     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
				}
			}
			return;
		}
	}

	ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
		     "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
		     link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
		     be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
}

2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
/**
 * ipr_log64_path_elem - Log a fabric path element.
 * @hostrcb:	hostrcb struct
 * @cfg:		fabric path element struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log64_path_elem(struct ipr_hostrcb *hostrcb,
				struct ipr_hostrcb64_config_element *cfg)
{
	int i, j;
	u8 desc_id = cfg->descriptor_id & IPR_DESCRIPTOR_MASK;
	u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
	u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
	char buffer[IPR_MAX_RES_PATH_LENGTH];

	if (type == IPR_PATH_CFG_NOT_EXIST || desc_id != IPR_DESCRIPTOR_SIS64)
		return;

	for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
		if (path_type_desc[i].type != type)
			continue;

		for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
			if (path_status_desc[j].status != status)
				continue;

			ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s, Link rate=%s, WWN=%08X%08X\n",
				     path_status_desc[j].desc, path_type_desc[i].desc,
2223 2224 2225 2226 2227
				     ipr_format_res_path(hostrcb->ioa_cfg,
					cfg->res_path, buffer, sizeof(buffer)),
					link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
					be32_to_cpu(cfg->wwid[0]),
					be32_to_cpu(cfg->wwid[1]));
2228 2229 2230 2231 2232
			return;
		}
	}
	ipr_hcam_err(hostrcb, "Path element=%02X: Resource Path=%s, Link rate=%s "
		     "WWN=%08X%08X\n", cfg->type_status,
2233 2234 2235 2236
		     ipr_format_res_path(hostrcb->ioa_cfg,
			cfg->res_path, buffer, sizeof(buffer)),
			link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
			be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2237 2238
}

2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
/**
 * ipr_log_fabric_error - Log a fabric error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
				 struct ipr_hostrcb *hostrcb)
{
	struct ipr_hostrcb_type_20_error *error;
	struct ipr_hostrcb_fabric_desc *fabric;
	struct ipr_hostrcb_config_element *cfg;
	int i, add_len;

	error = &hostrcb->hcam.u.error.u.type_20_error;
	error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
	ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);

	add_len = be32_to_cpu(hostrcb->hcam.length) -
		(offsetof(struct ipr_hostrcb_error, u) +
		 offsetof(struct ipr_hostrcb_type_20_error, desc));

	for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
		ipr_log_fabric_path(hostrcb, fabric);
		for_each_fabric_cfg(fabric, cfg)
			ipr_log_path_elem(hostrcb, cfg);

		add_len -= be16_to_cpu(fabric->length);
		fabric = (struct ipr_hostrcb_fabric_desc *)
			((unsigned long)fabric + be16_to_cpu(fabric->length));
	}

2273
	ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
2274 2275
}

2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298
/**
 * ipr_log_sis64_array_error - Log a sis64 array error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_sis64_array_error(struct ipr_ioa_cfg *ioa_cfg,
				      struct ipr_hostrcb *hostrcb)
{
	int i, num_entries;
	struct ipr_hostrcb_type_24_error *error;
	struct ipr_hostrcb64_array_data_entry *array_entry;
	char buffer[IPR_MAX_RES_PATH_LENGTH];
	const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };

	error = &hostrcb->hcam.u.error64.u.type_24_error;

	ipr_err_separator;

	ipr_err("RAID %s Array Configuration: %s\n",
		error->protection_level,
2299 2300
		ipr_format_res_path(ioa_cfg, error->last_res_path,
			buffer, sizeof(buffer)));
2301 2302 2303 2304

	ipr_err_separator;

	array_entry = error->array_member;
W
Wayne Boyer 已提交
2305 2306
	num_entries = min_t(u32, error->num_entries,
			    ARRAY_SIZE(error->array_member));
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319

	for (i = 0; i < num_entries; i++, array_entry++) {

		if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
			continue;

		if (error->exposed_mode_adn == i)
			ipr_err("Exposed Array Member %d:\n", i);
		else
			ipr_err("Array Member %d:\n", i);

		ipr_err("Array Member %d:\n", i);
		ipr_log_ext_vpd(&array_entry->vpd);
W
Wayne Boyer 已提交
2320
		ipr_err("Current Location: %s\n",
2321 2322
			 ipr_format_res_path(ioa_cfg, array_entry->res_path,
				buffer, sizeof(buffer)));
W
Wayne Boyer 已提交
2323
		ipr_err("Expected Location: %s\n",
2324 2325 2326
			 ipr_format_res_path(ioa_cfg,
				array_entry->expected_res_path,
				buffer, sizeof(buffer)));
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369

		ipr_err_separator;
	}
}

/**
 * ipr_log_sis64_fabric_error - Log a sis64 fabric error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_sis64_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
				       struct ipr_hostrcb *hostrcb)
{
	struct ipr_hostrcb_type_30_error *error;
	struct ipr_hostrcb64_fabric_desc *fabric;
	struct ipr_hostrcb64_config_element *cfg;
	int i, add_len;

	error = &hostrcb->hcam.u.error64.u.type_30_error;

	error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
	ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);

	add_len = be32_to_cpu(hostrcb->hcam.length) -
		(offsetof(struct ipr_hostrcb64_error, u) +
		 offsetof(struct ipr_hostrcb_type_30_error, desc));

	for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
		ipr_log64_fabric_path(hostrcb, fabric);
		for_each_fabric_cfg(fabric, cfg)
			ipr_log64_path_elem(hostrcb, cfg);

		add_len -= be16_to_cpu(fabric->length);
		fabric = (struct ipr_hostrcb64_fabric_desc *)
			((unsigned long)fabric + be16_to_cpu(fabric->length));
	}

	ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
}

2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
/**
 * ipr_log_generic_error - Log an adapter error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
				  struct ipr_hostrcb *hostrcb)
{
2381
	ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
2382 2383 2384
			 be32_to_cpu(hostrcb->hcam.length));
}

2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
/**
 * ipr_log_sis64_device_error - Log a cache error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * Return value:
 * 	none
 **/
static void ipr_log_sis64_device_error(struct ipr_ioa_cfg *ioa_cfg,
					 struct ipr_hostrcb *hostrcb)
{
	struct ipr_hostrcb_type_21_error *error;
	char buffer[IPR_MAX_RES_PATH_LENGTH];

	error = &hostrcb->hcam.u.error64.u.type_21_error;

	ipr_err("-----Failing Device Information-----\n");
	ipr_err("World Wide Unique ID: %08X%08X%08X%08X\n",
		be32_to_cpu(error->wwn[0]), be32_to_cpu(error->wwn[1]),
		 be32_to_cpu(error->wwn[2]), be32_to_cpu(error->wwn[3]));
	ipr_err("Device Resource Path: %s\n",
		__ipr_format_res_path(error->res_path,
				      buffer, sizeof(buffer)));
	error->primary_problem_desc[sizeof(error->primary_problem_desc) - 1] = '\0';
	error->second_problem_desc[sizeof(error->second_problem_desc) - 1] = '\0';
	ipr_err("Primary Problem Description: %s\n", error->primary_problem_desc);
	ipr_err("Secondary Problem Description:  %s\n", error->second_problem_desc);
	ipr_err("SCSI Sense Data:\n");
	ipr_log_hex_data(ioa_cfg, error->sense_data, sizeof(error->sense_data));
	ipr_err("SCSI Command Descriptor Block: \n");
	ipr_log_hex_data(ioa_cfg, error->cdb, sizeof(error->cdb));

	ipr_err("Additional IOA Data:\n");
	ipr_log_hex_data(ioa_cfg, error->ioa_data, be32_to_cpu(error->length_of_error));
}

L
Linus Torvalds 已提交
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
/**
 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
 * @ioasc:	IOASC
 *
 * This function will return the index of into the ipr_error_table
 * for the specified IOASC. If the IOASC is not in the table,
 * 0 will be returned, which points to the entry used for unknown errors.
 *
 * Return value:
 * 	index into the ipr_error_table
 **/
static u32 ipr_get_error(u32 ioasc)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
2437
		if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
L
Linus Torvalds 已提交
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
			return i;

	return 0;
}

/**
 * ipr_handle_log_data - Log an adapter error.
 * @ioa_cfg:	ioa config struct
 * @hostrcb:	hostrcb struct
 *
 * This function logs an adapter error to the system.
 *
 * Return value:
 * 	none
 **/
static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
				struct ipr_hostrcb *hostrcb)
{
	u32 ioasc;
	int error_index;
2458
	struct ipr_hostrcb_type_21_error *error;
L
Linus Torvalds 已提交
2459 2460 2461 2462 2463 2464 2465

	if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
		return;

	if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
		dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");

2466 2467 2468 2469
	if (ioa_cfg->sis64)
		ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
	else
		ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
L
Linus Torvalds 已提交
2470

2471 2472
	if (!ioa_cfg->sis64 && (ioasc == IPR_IOASC_BUS_WAS_RESET ||
	    ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER)) {
L
Linus Torvalds 已提交
2473 2474
		/* Tell the midlayer we had a bus reset so it will handle the UA properly */
		scsi_report_bus_reset(ioa_cfg->host,
2475
				      hostrcb->hcam.u.error.fd_res_addr.bus);
L
Linus Torvalds 已提交
2476 2477 2478 2479 2480 2481 2482
	}

	error_index = ipr_get_error(ioasc);

	if (!ipr_error_table[error_index].log_hcam)
		return;

2483 2484 2485 2486 2487 2488 2489 2490 2491
	if (ioasc == IPR_IOASC_HW_CMD_FAILED &&
	    hostrcb->hcam.overlay_id == IPR_HOST_RCB_OVERLAY_ID_21) {
		error = &hostrcb->hcam.u.error64.u.type_21_error;

		if (((be32_to_cpu(error->sense_data[0]) & 0x0000ff00) >> 8) == ILLEGAL_REQUEST &&
			ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
				return;
	}

2492
	ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
L
Linus Torvalds 已提交
2493 2494 2495 2496

	/* Set indication we have logged an error */
	ioa_cfg->errors_logged++;

2497
	if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
L
Linus Torvalds 已提交
2498
		return;
2499 2500
	if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
		hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
L
Linus Torvalds 已提交
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512

	switch (hostrcb->hcam.overlay_id) {
	case IPR_HOST_RCB_OVERLAY_ID_2:
		ipr_log_cache_error(ioa_cfg, hostrcb);
		break;
	case IPR_HOST_RCB_OVERLAY_ID_3:
		ipr_log_config_error(ioa_cfg, hostrcb);
		break;
	case IPR_HOST_RCB_OVERLAY_ID_4:
	case IPR_HOST_RCB_OVERLAY_ID_6:
		ipr_log_array_error(ioa_cfg, hostrcb);
		break;
2513 2514 2515
	case IPR_HOST_RCB_OVERLAY_ID_7:
		ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
		break;
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
	case IPR_HOST_RCB_OVERLAY_ID_12:
		ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
		break;
	case IPR_HOST_RCB_OVERLAY_ID_13:
		ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
		break;
	case IPR_HOST_RCB_OVERLAY_ID_14:
	case IPR_HOST_RCB_OVERLAY_ID_16:
		ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
		break;
	case IPR_HOST_RCB_OVERLAY_ID_17:
		ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
		break;
2529 2530 2531
	case IPR_HOST_RCB_OVERLAY_ID_20:
		ipr_log_fabric_error(ioa_cfg, hostrcb);
		break;
2532 2533 2534
	case IPR_HOST_RCB_OVERLAY_ID_21:
		ipr_log_sis64_device_error(ioa_cfg, hostrcb);
		break;
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
	case IPR_HOST_RCB_OVERLAY_ID_23:
		ipr_log_sis64_config_error(ioa_cfg, hostrcb);
		break;
	case IPR_HOST_RCB_OVERLAY_ID_24:
	case IPR_HOST_RCB_OVERLAY_ID_26:
		ipr_log_sis64_array_error(ioa_cfg, hostrcb);
		break;
	case IPR_HOST_RCB_OVERLAY_ID_30:
		ipr_log_sis64_fabric_error(ioa_cfg, hostrcb);
		break;
2545
	case IPR_HOST_RCB_OVERLAY_ID_1:
L
Linus Torvalds 已提交
2546 2547
	case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
	default:
2548
		ipr_log_generic_error(ioa_cfg, hostrcb);
L
Linus Torvalds 已提交
2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567
		break;
	}
}

/**
 * ipr_process_error - Op done function for an adapter error log.
 * @ipr_cmd:	ipr command struct
 *
 * This function is the op done function for an error log host
 * controlled async from the adapter. It will log the error and
 * send the HCAM back to the adapter.
 *
 * Return value:
 * 	none
 **/
static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
2568
	u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
2569 2570 2571 2572 2573 2574
	u32 fd_ioasc;

	if (ioa_cfg->sis64)
		fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
	else
		fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
L
Linus Torvalds 已提交
2575 2576

	list_del(&hostrcb->queue);
2577
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
L
Linus Torvalds 已提交
2578 2579 2580

	if (!ioasc) {
		ipr_handle_log_data(ioa_cfg, hostrcb);
2581 2582
		if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
			ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
B
Brian King 已提交
2583 2584
	} else if (ioasc != IPR_IOASC_IOA_WAS_RESET &&
		   ioasc != IPR_IOASC_ABORTED_CMD_TERM_BY_HOST) {
L
Linus Torvalds 已提交
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
		dev_err(&ioa_cfg->pdev->dev,
			"Host RCB failed with IOASC: 0x%08X\n", ioasc);
	}

	ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
}

/**
 * ipr_timeout -  An internally generated op has timed out.
 * @ipr_cmd:	ipr command struct
 *
 * This function blocks host requests and initiates an
 * adapter reset.
 *
 * Return value:
 * 	none
 **/
static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
{
	unsigned long lock_flags = 0;
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	ENTER;
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);

	ioa_cfg->errors_logged++;
	dev_err(&ioa_cfg->pdev->dev,
		"Adapter being reset due to command timeout.\n");

	if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
		ioa_cfg->sdt_state = GET_DUMP;

	if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
		ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	LEAVE;
}

/**
 * ipr_oper_timeout -  Adapter timed out transitioning to operational
 * @ipr_cmd:	ipr command struct
 *
 * This function blocks host requests and initiates an
 * adapter reset.
 *
 * Return value:
 * 	none
 **/
static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
{
	unsigned long lock_flags = 0;
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	ENTER;
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);

	ioa_cfg->errors_logged++;
	dev_err(&ioa_cfg->pdev->dev,
		"Adapter timed out transitioning to operational.\n");

	if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
		ioa_cfg->sdt_state = GET_DUMP;

	if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
		if (ipr_fastfail)
			ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
		ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
	}

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	LEAVE;
}

/**
 * ipr_find_ses_entry - Find matching SES in SES table
 * @res:	resource entry struct of SES
 *
 * Return value:
 * 	pointer to SES table entry / NULL on failure
 **/
static const struct ipr_ses_table_entry *
ipr_find_ses_entry(struct ipr_resource_entry *res)
{
	int i, j, matches;
2670
	struct ipr_std_inq_vpids *vpids;
L
Linus Torvalds 已提交
2671 2672 2673 2674 2675
	const struct ipr_ses_table_entry *ste = ipr_ses_table;

	for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
		for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
			if (ste->compare_product_id_byte[j] == 'X') {
2676 2677
				vpids = &res->std_inq_data.vpids;
				if (vpids->product_id[j] == ste->product_id[j])
L
Linus Torvalds 已提交
2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
					matches++;
				else
					break;
			} else
				matches++;
		}

		if (matches == IPR_PROD_ID_LEN)
			return ste;
	}

	return NULL;
}

/**
 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
 * @ioa_cfg:	ioa config struct
 * @bus:		SCSI bus
 * @bus_width:	bus width
 *
 * Return value:
 *	SCSI bus speed in units of 100KHz, 1600 is 160 MHz
 *	For a 2-byte wide SCSI bus, the maximum transfer speed is
 *	twice the maximum transfer rate (e.g. for a wide enabled bus,
 *	max 160MHz = max 320MB/sec).
 **/
static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
{
	struct ipr_resource_entry *res;
	const struct ipr_ses_table_entry *ste;
	u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);

	/* Loop through each config table entry in the config table buffer */
	list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2712
		if (!(IPR_IS_SES_DEVICE(res->std_inq_data)))
L
Linus Torvalds 已提交
2713 2714
			continue;

2715
		if (bus != res->bus)
L
Linus Torvalds 已提交
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
			continue;

		if (!(ste = ipr_find_ses_entry(res)))
			continue;

		max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
	}

	return max_xfer_rate;
}

/**
 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
 * @ioa_cfg:		ioa config struct
 * @max_delay:		max delay in micro-seconds to wait
 *
 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
 *
 * Return value:
 * 	0 on success / other on failure
 **/
static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
{
	volatile u32 pcii_reg;
	int delay = 1;

	/* Read interrupt reg until IOA signals IO Debug Acknowledge */
	while (delay < max_delay) {
		pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);

		if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
			return 0;

		/* udelay cannot be used if delay is more than a few milliseconds */
		if ((delay / 1000) > MAX_UDELAY_MS)
			mdelay(delay / 1000);
		else
			udelay(delay);

		delay += delay;
	}
	return -EIO;
}

2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
/**
 * ipr_get_sis64_dump_data_section - Dump IOA memory
 * @ioa_cfg:			ioa config struct
 * @start_addr:			adapter address to dump
 * @dest:			destination kernel buffer
 * @length_in_words:		length to dump in 4 byte words
 *
 * Return value:
 * 	0 on success
 **/
static int ipr_get_sis64_dump_data_section(struct ipr_ioa_cfg *ioa_cfg,
					   u32 start_addr,
					   __be32 *dest, u32 length_in_words)
{
	int i;

	for (i = 0; i < length_in_words; i++) {
		writel(start_addr+(i*4), ioa_cfg->regs.dump_addr_reg);
		*dest = cpu_to_be32(readl(ioa_cfg->regs.dump_data_reg));
		dest++;
	}

	return 0;
}

L
Linus Torvalds 已提交
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
/**
 * ipr_get_ldump_data_section - Dump IOA memory
 * @ioa_cfg:			ioa config struct
 * @start_addr:			adapter address to dump
 * @dest:				destination kernel buffer
 * @length_in_words:	length to dump in 4 byte words
 *
 * Return value:
 * 	0 on success / -EIO on failure
 **/
static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
				      u32 start_addr,
				      __be32 *dest, u32 length_in_words)
{
	volatile u32 temp_pcii_reg;
	int i, delay = 0;

2802 2803 2804 2805
	if (ioa_cfg->sis64)
		return ipr_get_sis64_dump_data_section(ioa_cfg, start_addr,
						       dest, length_in_words);

L
Linus Torvalds 已提交
2806 2807
	/* Write IOA interrupt reg starting LDUMP state  */
	writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
2808
	       ioa_cfg->regs.set_uproc_interrupt_reg32);
L
Linus Torvalds 已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826

	/* Wait for IO debug acknowledge */
	if (ipr_wait_iodbg_ack(ioa_cfg,
			       IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
		dev_err(&ioa_cfg->pdev->dev,
			"IOA dump long data transfer timeout\n");
		return -EIO;
	}

	/* Signal LDUMP interlocked - clear IO debug ack */
	writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
	       ioa_cfg->regs.clr_interrupt_reg);

	/* Write Mailbox with starting address */
	writel(start_addr, ioa_cfg->ioa_mailbox);

	/* Signal address valid - clear IOA Reset alert */
	writel(IPR_UPROCI_RESET_ALERT,
2827
	       ioa_cfg->regs.clr_uproc_interrupt_reg32);
L
Linus Torvalds 已提交
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851

	for (i = 0; i < length_in_words; i++) {
		/* Wait for IO debug acknowledge */
		if (ipr_wait_iodbg_ack(ioa_cfg,
				       IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
			dev_err(&ioa_cfg->pdev->dev,
				"IOA dump short data transfer timeout\n");
			return -EIO;
		}

		/* Read data from mailbox and increment destination pointer */
		*dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
		dest++;

		/* For all but the last word of data, signal data received */
		if (i < (length_in_words - 1)) {
			/* Signal dump data received - Clear IO debug Ack */
			writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
			       ioa_cfg->regs.clr_interrupt_reg);
		}
	}

	/* Signal end of block transfer. Set reset alert then clear IO debug ack */
	writel(IPR_UPROCI_RESET_ALERT,
2852
	       ioa_cfg->regs.set_uproc_interrupt_reg32);
L
Linus Torvalds 已提交
2853 2854

	writel(IPR_UPROCI_IO_DEBUG_ALERT,
2855
	       ioa_cfg->regs.clr_uproc_interrupt_reg32);
L
Linus Torvalds 已提交
2856 2857 2858 2859 2860 2861 2862 2863

	/* Signal dump data received - Clear IO debug Ack */
	writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
	       ioa_cfg->regs.clr_interrupt_reg);

	/* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
	while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
		temp_pcii_reg =
2864
		    readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
L
Linus Torvalds 已提交
2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891

		if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
			return 0;

		udelay(10);
		delay += 10;
	}

	return 0;
}

#ifdef CONFIG_SCSI_IPR_DUMP
/**
 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
 * @ioa_cfg:		ioa config struct
 * @pci_address:	adapter address
 * @length:			length of data to copy
 *
 * Copy data from PCI adapter to kernel buffer.
 * Note: length MUST be a 4 byte multiple
 * Return value:
 * 	0 on success / other on failure
 **/
static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
			unsigned long pci_address, u32 length)
{
	int bytes_copied = 0;
2892
	int cur_len, rc, rem_len, rem_page_len, max_dump_size;
L
Linus Torvalds 已提交
2893 2894 2895 2896
	__be32 *page;
	unsigned long lock_flags = 0;
	struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;

2897 2898 2899 2900 2901
	if (ioa_cfg->sis64)
		max_dump_size = IPR_FMT3_MAX_IOA_DUMP_SIZE;
	else
		max_dump_size = IPR_FMT2_MAX_IOA_DUMP_SIZE;

L
Linus Torvalds 已提交
2902
	while (bytes_copied < length &&
2903
	       (ioa_dump->hdr.len + bytes_copied) < max_dump_size) {
L
Linus Torvalds 已提交
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
		if (ioa_dump->page_offset >= PAGE_SIZE ||
		    ioa_dump->page_offset == 0) {
			page = (__be32 *)__get_free_page(GFP_ATOMIC);

			if (!page) {
				ipr_trace;
				return bytes_copied;
			}

			ioa_dump->page_offset = 0;
			ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
			ioa_dump->next_page_index++;
		} else
			page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];

		rem_len = length - bytes_copied;
		rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
		cur_len = min(rem_len, rem_page_len);

		spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
		if (ioa_cfg->sdt_state == ABORT_DUMP) {
			rc = -EIO;
		} else {
			rc = ipr_get_ldump_data_section(ioa_cfg,
							pci_address + bytes_copied,
							&page[ioa_dump->page_offset / 4],
							(cur_len / sizeof(u32)));
		}
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

		if (!rc) {
			ioa_dump->page_offset += cur_len;
			bytes_copied += cur_len;
		} else {
			ipr_trace;
			break;
		}
		schedule();
	}

	return bytes_copied;
}

/**
 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
 * @hdr:	dump entry header struct
 *
 * Return value:
 * 	nothing
 **/
static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
{
	hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
	hdr->num_elems = 1;
	hdr->offset = sizeof(*hdr);
	hdr->status = IPR_DUMP_STATUS_SUCCESS;
}

/**
 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
 * @ioa_cfg:	ioa config struct
 * @driver_dump:	driver dump struct
 *
 * Return value:
 * 	nothing
 **/
static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
				   struct ipr_driver_dump *driver_dump)
{
	struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;

	ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
	driver_dump->ioa_type_entry.hdr.len =
		sizeof(struct ipr_dump_ioa_type_entry) -
		sizeof(struct ipr_dump_entry_header);
	driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
	driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
	driver_dump->ioa_type_entry.type = ioa_cfg->type;
	driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
		(ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
		ucode_vpd->minor_release[1];
	driver_dump->hdr.num_entries++;
}

/**
 * ipr_dump_version_data - Fill in the driver version in the dump.
 * @ioa_cfg:	ioa config struct
 * @driver_dump:	driver dump struct
 *
 * Return value:
 * 	nothing
 **/
static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
				  struct ipr_driver_dump *driver_dump)
{
	ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
	driver_dump->version_entry.hdr.len =
		sizeof(struct ipr_dump_version_entry) -
		sizeof(struct ipr_dump_entry_header);
	driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
	driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
	strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
	driver_dump->hdr.num_entries++;
}

/**
 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
 * @ioa_cfg:	ioa config struct
 * @driver_dump:	driver dump struct
 *
 * Return value:
 * 	nothing
 **/
static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
				   struct ipr_driver_dump *driver_dump)
{
	ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
	driver_dump->trace_entry.hdr.len =
		sizeof(struct ipr_dump_trace_entry) -
		sizeof(struct ipr_dump_entry_header);
	driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
	driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
	memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
	driver_dump->hdr.num_entries++;
}

/**
 * ipr_dump_location_data - Fill in the IOA location in the dump.
 * @ioa_cfg:	ioa config struct
 * @driver_dump:	driver dump struct
 *
 * Return value:
 * 	nothing
 **/
static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
				   struct ipr_driver_dump *driver_dump)
{
	ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
	driver_dump->location_entry.hdr.len =
		sizeof(struct ipr_dump_location_entry) -
		sizeof(struct ipr_dump_entry_header);
	driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
	driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
3047
	strcpy(driver_dump->location_entry.location, dev_name(&ioa_cfg->pdev->dev));
L
Linus Torvalds 已提交
3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
	driver_dump->hdr.num_entries++;
}

/**
 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
 * @ioa_cfg:	ioa config struct
 * @dump:		dump struct
 *
 * Return value:
 * 	nothing
 **/
static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
{
	unsigned long start_addr, sdt_word;
	unsigned long lock_flags = 0;
	struct ipr_driver_dump *driver_dump = &dump->driver_dump;
	struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
3065 3066
	u32 num_entries, max_num_entries, start_off, end_off;
	u32 max_dump_size, bytes_to_copy, bytes_copied, rc;
L
Linus Torvalds 已提交
3067
	struct ipr_sdt *sdt;
3068
	int valid = 1;
L
Linus Torvalds 已提交
3069 3070 3071 3072 3073 3074
	int i;

	ENTER;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);

3075
	if (ioa_cfg->sdt_state != READ_DUMP) {
L
Linus Torvalds 已提交
3076 3077 3078 3079
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		return;
	}

3080 3081 3082 3083 3084 3085
	if (ioa_cfg->sis64) {
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		ssleep(IPR_DUMP_DELAY_SECONDS);
		spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	}

L
Linus Torvalds 已提交
3086 3087
	start_addr = readl(ioa_cfg->ioa_mailbox);

3088
	if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(start_addr)) {
L
Linus Torvalds 已提交
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
		dev_err(&ioa_cfg->pdev->dev,
			"Invalid dump table format: %lx\n", start_addr);
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		return;
	}

	dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");

	driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;

	/* Initialize the overall dump header */
	driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
	driver_dump->hdr.num_entries = 1;
	driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
	driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
	driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
	driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;

	ipr_dump_version_data(ioa_cfg, driver_dump);
	ipr_dump_location_data(ioa_cfg, driver_dump);
	ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
	ipr_dump_trace_data(ioa_cfg, driver_dump);

	/* Update dump_header */
	driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);

	/* IOA Dump entry */
	ipr_init_dump_entry_hdr(&ioa_dump->hdr);
	ioa_dump->hdr.len = 0;
	ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
	ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;

	/* First entries in sdt are actually a list of dump addresses and
	 lengths to gather the real dump data.  sdt represents the pointer
	 to the ioa generated dump table.  Dump data will be extracted based
	 on entries in this table */
	sdt = &ioa_dump->sdt;

3127 3128 3129 3130 3131 3132 3133 3134 3135 3136
	if (ioa_cfg->sis64) {
		max_num_entries = IPR_FMT3_NUM_SDT_ENTRIES;
		max_dump_size = IPR_FMT3_MAX_IOA_DUMP_SIZE;
	} else {
		max_num_entries = IPR_FMT2_NUM_SDT_ENTRIES;
		max_dump_size = IPR_FMT2_MAX_IOA_DUMP_SIZE;
	}

	bytes_to_copy = offsetof(struct ipr_sdt, entry) +
			(max_num_entries * sizeof(struct ipr_sdt_entry));
L
Linus Torvalds 已提交
3137
	rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
3138
					bytes_to_copy / sizeof(__be32));
L
Linus Torvalds 已提交
3139 3140

	/* Smart Dump table is ready to use and the first entry is valid */
3141 3142
	if (rc || ((be32_to_cpu(sdt->hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
	    (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
L
Linus Torvalds 已提交
3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
		dev_err(&ioa_cfg->pdev->dev,
			"Dump of IOA failed. Dump table not valid: %d, %X.\n",
			rc, be32_to_cpu(sdt->hdr.state));
		driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
		ioa_cfg->sdt_state = DUMP_OBTAINED;
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		return;
	}

	num_entries = be32_to_cpu(sdt->hdr.num_entries_used);

3154 3155 3156 3157 3158 3159 3160 3161 3162
	if (num_entries > max_num_entries)
		num_entries = max_num_entries;

	/* Update dump length to the actual data to be copied */
	dump->driver_dump.hdr.len += sizeof(struct ipr_sdt_header);
	if (ioa_cfg->sis64)
		dump->driver_dump.hdr.len += num_entries * sizeof(struct ipr_sdt_entry);
	else
		dump->driver_dump.hdr.len += max_num_entries * sizeof(struct ipr_sdt_entry);
L
Linus Torvalds 已提交
3163 3164 3165 3166

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

	for (i = 0; i < num_entries; i++) {
3167
		if (ioa_dump->hdr.len > max_dump_size) {
L
Linus Torvalds 已提交
3168 3169 3170 3171 3172
			driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
			break;
		}

		if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
			sdt_word = be32_to_cpu(sdt->entry[i].start_token);
			if (ioa_cfg->sis64)
				bytes_to_copy = be32_to_cpu(sdt->entry[i].end_token);
			else {
				start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
				end_off = be32_to_cpu(sdt->entry[i].end_token);

				if (ipr_sdt_is_fmt2(sdt_word) && sdt_word)
					bytes_to_copy = end_off - start_off;
				else
					valid = 0;
			}
			if (valid) {
3186
				if (bytes_to_copy > max_dump_size) {
L
Linus Torvalds 已提交
3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214
					sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
					continue;
				}

				/* Copy data from adapter to driver buffers */
				bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
							    bytes_to_copy);

				ioa_dump->hdr.len += bytes_copied;

				if (bytes_copied != bytes_to_copy) {
					driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
					break;
				}
			}
		}
	}

	dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");

	/* Update dump_header */
	driver_dump->hdr.len += ioa_dump->hdr.len;
	wmb();
	ioa_cfg->sdt_state = DUMP_OBTAINED;
	LEAVE;
}

#else
3215
#define ipr_get_ioa_dump(ioa_cfg, dump) do { } while (0)
L
Linus Torvalds 已提交
3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
#endif

/**
 * ipr_release_dump - Free adapter dump memory
 * @kref:	kref struct
 *
 * Return value:
 *	nothing
 **/
static void ipr_release_dump(struct kref *kref)
{
3227
	struct ipr_dump *dump = container_of(kref, struct ipr_dump, kref);
L
Linus Torvalds 已提交
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240
	struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
	unsigned long lock_flags = 0;
	int i;

	ENTER;
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	ioa_cfg->dump = NULL;
	ioa_cfg->sdt_state = INACTIVE;
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

	for (i = 0; i < dump->ioa_dump.next_page_index; i++)
		free_page((unsigned long) dump->ioa_dump.ioa_data[i]);

3241
	vfree(dump->ioa_dump.ioa_data);
L
Linus Torvalds 已提交
3242 3243 3244 3245 3246 3247
	kfree(dump);
	LEAVE;
}

/**
 * ipr_worker_thread - Worker thread
D
David Howells 已提交
3248
 * @work:		ioa config struct
L
Linus Torvalds 已提交
3249 3250 3251 3252 3253 3254 3255 3256
 *
 * Called at task level from a work thread. This function takes care
 * of adding and removing device from the mid-layer as configuration
 * changes are detected by the adapter.
 *
 * Return value:
 * 	nothing
 **/
D
David Howells 已提交
3257
static void ipr_worker_thread(struct work_struct *work)
L
Linus Torvalds 已提交
3258 3259 3260 3261 3262
{
	unsigned long lock_flags;
	struct ipr_resource_entry *res;
	struct scsi_device *sdev;
	struct ipr_dump *dump;
D
David Howells 已提交
3263 3264
	struct ipr_ioa_cfg *ioa_cfg =
		container_of(work, struct ipr_ioa_cfg, work_q);
L
Linus Torvalds 已提交
3265 3266 3267 3268 3269 3270
	u8 bus, target, lun;
	int did_work;

	ENTER;
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);

3271
	if (ioa_cfg->sdt_state == READ_DUMP) {
L
Linus Torvalds 已提交
3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
		dump = ioa_cfg->dump;
		if (!dump) {
			spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
			return;
		}
		kref_get(&dump->kref);
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		ipr_get_ioa_dump(ioa_cfg, dump);
		kref_put(&dump->kref, ipr_release_dump);

		spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3283
		if (ioa_cfg->sdt_state == DUMP_OBTAINED && !ioa_cfg->dump_timeout)
L
Linus Torvalds 已提交
3284 3285 3286 3287 3288 3289 3290 3291
			ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		return;
	}

restart:
	do {
		did_work = 0;
3292
		if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds) {
L
Linus Torvalds 已提交
3293 3294 3295 3296 3297 3298 3299 3300 3301
			spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
			return;
		}

		list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
			if (res->del_from_ml && res->sdev) {
				did_work = 1;
				sdev = res->sdev;
				if (!scsi_device_get(sdev)) {
3302 3303 3304 3305
					if (!res->add_to_ml)
						list_move_tail(&res->queue, &ioa_cfg->free_res_q);
					else
						res->del_from_ml = 0;
L
Linus Torvalds 已提交
3306 3307 3308 3309 3310 3311 3312 3313
					spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
					scsi_remove_device(sdev);
					scsi_device_put(sdev);
					spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
				}
				break;
			}
		}
3314
	} while (did_work);
L
Linus Torvalds 已提交
3315 3316 3317

	list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
		if (res->add_to_ml) {
3318 3319 3320
			bus = res->bus;
			target = res->target;
			lun = res->lun;
3321
			res->add_to_ml = 0;
L
Linus Torvalds 已提交
3322 3323 3324 3325 3326 3327 3328
			spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
			scsi_add_device(ioa_cfg->host, bus, target, lun);
			spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
			goto restart;
		}
	}

3329
	ioa_cfg->scan_done = 1;
L
Linus Torvalds 已提交
3330
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3331
	kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
L
Linus Torvalds 已提交
3332 3333 3334 3335 3336 3337
	LEAVE;
}

#ifdef CONFIG_SCSI_IPR_TRACE
/**
 * ipr_read_trace - Dump the adapter trace
3338
 * @filp:		open sysfs file
L
Linus Torvalds 已提交
3339
 * @kobj:		kobject struct
3340
 * @bin_attr:		bin_attribute struct
L
Linus Torvalds 已提交
3341 3342 3343 3344 3345 3346 3347
 * @buf:		buffer
 * @off:		offset
 * @count:		buffer size
 *
 * Return value:
 *	number of bytes printed to buffer
 **/
3348
static ssize_t ipr_read_trace(struct file *filp, struct kobject *kobj,
3349 3350
			      struct bin_attribute *bin_attr,
			      char *buf, loff_t off, size_t count)
L
Linus Torvalds 已提交
3351
{
3352 3353
	struct device *dev = container_of(kobj, struct device, kobj);
	struct Scsi_Host *shost = class_to_shost(dev);
L
Linus Torvalds 已提交
3354 3355
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	unsigned long lock_flags = 0;
3356
	ssize_t ret;
L
Linus Torvalds 已提交
3357 3358

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3359 3360
	ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
				IPR_TRACE_SIZE);
L
Linus Torvalds 已提交
3361
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3362 3363

	return ret;
L
Linus Torvalds 已提交
3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377
}

static struct bin_attribute ipr_trace_attr = {
	.attr =	{
		.name = "trace",
		.mode = S_IRUGO,
	},
	.size = 0,
	.read = ipr_read_trace,
};
#endif

/**
 * ipr_show_fw_version - Show the firmware version
3378 3379
 * @dev:	class device struct
 * @buf:	buffer
L
Linus Torvalds 已提交
3380 3381 3382 3383
 *
 * Return value:
 *	number of bytes printed to buffer
 **/
3384 3385
static ssize_t ipr_show_fw_version(struct device *dev,
				   struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
3386
{
3387
	struct Scsi_Host *shost = class_to_shost(dev);
L
Linus Torvalds 已提交
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
	unsigned long lock_flags = 0;
	int len;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
		       ucode_vpd->major_release, ucode_vpd->card_type,
		       ucode_vpd->minor_release[0],
		       ucode_vpd->minor_release[1]);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return len;
}

3402
static struct device_attribute ipr_fw_version_attr = {
L
Linus Torvalds 已提交
3403 3404 3405 3406 3407 3408 3409 3410 3411
	.attr = {
		.name =		"fw_version",
		.mode =		S_IRUGO,
	},
	.show = ipr_show_fw_version,
};

/**
 * ipr_show_log_level - Show the adapter's error logging level
3412 3413
 * @dev:	class device struct
 * @buf:	buffer
L
Linus Torvalds 已提交
3414 3415 3416 3417
 *
 * Return value:
 * 	number of bytes printed to buffer
 **/
3418 3419
static ssize_t ipr_show_log_level(struct device *dev,
				   struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
3420
{
3421
	struct Scsi_Host *shost = class_to_shost(dev);
L
Linus Torvalds 已提交
3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	unsigned long lock_flags = 0;
	int len;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return len;
}

/**
 * ipr_store_log_level - Change the adapter's error logging level
3434 3435
 * @dev:	class device struct
 * @buf:	buffer
L
Linus Torvalds 已提交
3436 3437 3438 3439
 *
 * Return value:
 * 	number of bytes printed to buffer
 **/
3440
static ssize_t ipr_store_log_level(struct device *dev,
3441
				   struct device_attribute *attr,
L
Linus Torvalds 已提交
3442 3443
				   const char *buf, size_t count)
{
3444
	struct Scsi_Host *shost = class_to_shost(dev);
L
Linus Torvalds 已提交
3445 3446 3447 3448 3449 3450 3451 3452 3453
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	unsigned long lock_flags = 0;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return strlen(buf);
}

3454
static struct device_attribute ipr_log_level_attr = {
L
Linus Torvalds 已提交
3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
	.attr = {
		.name =		"log_level",
		.mode =		S_IRUGO | S_IWUSR,
	},
	.show = ipr_show_log_level,
	.store = ipr_store_log_level
};

/**
 * ipr_store_diagnostics - IOA Diagnostics interface
3465 3466 3467
 * @dev:	device struct
 * @buf:	buffer
 * @count:	buffer size
L
Linus Torvalds 已提交
3468 3469 3470 3471 3472 3473 3474
 *
 * This function will reset the adapter and wait a reasonable
 * amount of time for any errors that the adapter might log.
 *
 * Return value:
 * 	count on success / other on failure
 **/
3475 3476
static ssize_t ipr_store_diagnostics(struct device *dev,
				     struct device_attribute *attr,
L
Linus Torvalds 已提交
3477 3478
				     const char *buf, size_t count)
{
3479
	struct Scsi_Host *shost = class_to_shost(dev);
L
Linus Torvalds 已提交
3480 3481 3482 3483 3484 3485 3486 3487
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	unsigned long lock_flags = 0;
	int rc = count;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3488
	while (ioa_cfg->in_reset_reload) {
3489 3490 3491 3492 3493
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
		spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	}

L
Linus Torvalds 已提交
3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515
	ioa_cfg->errors_logged = 0;
	ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);

	if (ioa_cfg->in_reset_reload) {
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);

		/* Wait for a second for any errors to be logged */
		msleep(1000);
	} else {
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		return -EIO;
	}

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
		rc = -EIO;
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

	return rc;
}

3516
static struct device_attribute ipr_diagnostics_attr = {
L
Linus Torvalds 已提交
3517 3518 3519 3520 3521 3522 3523
	.attr = {
		.name =		"run_diagnostics",
		.mode =		S_IWUSR,
	},
	.store = ipr_store_diagnostics
};

3524 3525
/**
 * ipr_show_adapter_state - Show the adapter's state
3526 3527
 * @class_dev:	device struct
 * @buf:	buffer
3528 3529 3530 3531
 *
 * Return value:
 * 	number of bytes printed to buffer
 **/
3532 3533
static ssize_t ipr_show_adapter_state(struct device *dev,
				      struct device_attribute *attr, char *buf)
3534
{
3535
	struct Scsi_Host *shost = class_to_shost(dev);
3536 3537 3538 3539 3540
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	unsigned long lock_flags = 0;
	int len;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3541
	if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
3542 3543 3544 3545 3546 3547 3548 3549 3550
		len = snprintf(buf, PAGE_SIZE, "offline\n");
	else
		len = snprintf(buf, PAGE_SIZE, "online\n");
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return len;
}

/**
 * ipr_store_adapter_state - Change adapter state
3551 3552 3553
 * @dev:	device struct
 * @buf:	buffer
 * @count:	buffer size
3554 3555 3556 3557 3558 3559
 *
 * This function will change the adapter's state.
 *
 * Return value:
 * 	count on success / other on failure
 **/
3560 3561
static ssize_t ipr_store_adapter_state(struct device *dev,
				       struct device_attribute *attr,
3562 3563
				       const char *buf, size_t count)
{
3564
	struct Scsi_Host *shost = class_to_shost(dev);
3565 3566
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	unsigned long lock_flags;
3567
	int result = count, i;
3568 3569 3570 3571 3572

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3573 3574 3575 3576 3577 3578 3579 3580
	if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead &&
	    !strncmp(buf, "online", 6)) {
		for (i = 0; i < ioa_cfg->hrrq_num; i++) {
			spin_lock(&ioa_cfg->hrrq[i]._lock);
			ioa_cfg->hrrq[i].ioa_is_dead = 0;
			spin_unlock(&ioa_cfg->hrrq[i]._lock);
		}
		wmb();
3581 3582 3583 3584 3585 3586 3587 3588 3589 3590
		ioa_cfg->reset_retries = 0;
		ioa_cfg->in_ioa_bringdown = 0;
		ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
	}
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);

	return result;
}

3591
static struct device_attribute ipr_ioa_state_attr = {
3592
	.attr = {
3593
		.name =		"online_state",
3594 3595 3596 3597 3598 3599
		.mode =		S_IRUGO | S_IWUSR,
	},
	.show = ipr_show_adapter_state,
	.store = ipr_store_adapter_state
};

L
Linus Torvalds 已提交
3600 3601
/**
 * ipr_store_reset_adapter - Reset the adapter
3602 3603 3604
 * @dev:	device struct
 * @buf:	buffer
 * @count:	buffer size
L
Linus Torvalds 已提交
3605 3606 3607 3608 3609 3610
 *
 * This function will reset the adapter.
 *
 * Return value:
 * 	count on success / other on failure
 **/
3611 3612
static ssize_t ipr_store_reset_adapter(struct device *dev,
				       struct device_attribute *attr,
L
Linus Torvalds 已提交
3613 3614
				       const char *buf, size_t count)
{
3615
	struct Scsi_Host *shost = class_to_shost(dev);
L
Linus Torvalds 已提交
3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	unsigned long lock_flags;
	int result = count;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	if (!ioa_cfg->in_reset_reload)
		ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);

	return result;
}

3632
static struct device_attribute ipr_ioa_reset_attr = {
L
Linus Torvalds 已提交
3633 3634 3635 3636 3637 3638 3639
	.attr = {
		.name =		"reset_host",
		.mode =		S_IWUSR,
	},
	.store = ipr_store_reset_adapter
};

3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
static int ipr_iopoll(struct blk_iopoll *iop, int budget);
 /**
 * ipr_show_iopoll_weight - Show ipr polling mode
 * @dev:	class device struct
 * @buf:	buffer
 *
 * Return value:
 *	number of bytes printed to buffer
 **/
static ssize_t ipr_show_iopoll_weight(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	unsigned long lock_flags = 0;
	int len;

	spin_lock_irqsave(shost->host_lock, lock_flags);
	len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->iopoll_weight);
	spin_unlock_irqrestore(shost->host_lock, lock_flags);

	return len;
}

/**
 * ipr_store_iopoll_weight - Change the adapter's polling mode
 * @dev:	class device struct
 * @buf:	buffer
 *
 * Return value:
 *	number of bytes printed to buffer
 **/
static ssize_t ipr_store_iopoll_weight(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	struct Scsi_Host *shost = class_to_shost(dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	unsigned long user_iopoll_weight;
	unsigned long lock_flags = 0;
	int i;

	if (!ioa_cfg->sis64) {
		dev_info(&ioa_cfg->pdev->dev, "blk-iopoll not supported on this adapter\n");
		return -EINVAL;
	}
	if (kstrtoul(buf, 10, &user_iopoll_weight))
		return -EINVAL;

	if (user_iopoll_weight > 256) {
		dev_info(&ioa_cfg->pdev->dev, "Invalid blk-iopoll weight. It must be less than 256\n");
		return -EINVAL;
	}

	if (user_iopoll_weight == ioa_cfg->iopoll_weight) {
		dev_info(&ioa_cfg->pdev->dev, "Current blk-iopoll weight has the same weight\n");
		return strlen(buf);
	}

3699
	if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
3700 3701 3702 3703 3704 3705
		for (i = 1; i < ioa_cfg->hrrq_num; i++)
			blk_iopoll_disable(&ioa_cfg->hrrq[i].iopoll);
	}

	spin_lock_irqsave(shost->host_lock, lock_flags);
	ioa_cfg->iopoll_weight = user_iopoll_weight;
3706
	if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726
		for (i = 1; i < ioa_cfg->hrrq_num; i++) {
			blk_iopoll_init(&ioa_cfg->hrrq[i].iopoll,
					ioa_cfg->iopoll_weight, ipr_iopoll);
			blk_iopoll_enable(&ioa_cfg->hrrq[i].iopoll);
		}
	}
	spin_unlock_irqrestore(shost->host_lock, lock_flags);

	return strlen(buf);
}

static struct device_attribute ipr_iopoll_weight_attr = {
	.attr = {
		.name =		"iopoll_weight",
		.mode =		S_IRUGO | S_IWUSR,
	},
	.show = ipr_show_iopoll_weight,
	.store = ipr_store_iopoll_weight
};

L
Linus Torvalds 已提交
3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759
/**
 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
 * @buf_len:		buffer length
 *
 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
 * list to use for microcode download
 *
 * Return value:
 * 	pointer to sglist / NULL on failure
 **/
static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
{
	int sg_size, order, bsize_elem, num_elem, i, j;
	struct ipr_sglist *sglist;
	struct scatterlist *scatterlist;
	struct page *page;

	/* Get the minimum size per scatter/gather element */
	sg_size = buf_len / (IPR_MAX_SGLIST - 1);

	/* Get the actual size per element */
	order = get_order(sg_size);

	/* Determine the actual number of bytes per element */
	bsize_elem = PAGE_SIZE * (1 << order);

	/* Determine the actual number of sg entries needed */
	if (buf_len % bsize_elem)
		num_elem = (buf_len / bsize_elem) + 1;
	else
		num_elem = buf_len / bsize_elem;

	/* Allocate a scatter/gather list for the DMA */
3760
	sglist = kzalloc(sizeof(struct ipr_sglist) +
L
Linus Torvalds 已提交
3761 3762 3763 3764 3765 3766 3767 3768 3769
			 (sizeof(struct scatterlist) * (num_elem - 1)),
			 GFP_KERNEL);

	if (sglist == NULL) {
		ipr_trace;
		return NULL;
	}

	scatterlist = sglist->scatterlist;
J
Jens Axboe 已提交
3770
	sg_init_table(scatterlist, num_elem);
L
Linus Torvalds 已提交
3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782

	sglist->order = order;
	sglist->num_sg = num_elem;

	/* Allocate a bunch of sg elements */
	for (i = 0; i < num_elem; i++) {
		page = alloc_pages(GFP_KERNEL, order);
		if (!page) {
			ipr_trace;

			/* Free up what we already allocated */
			for (j = i - 1; j >= 0; j--)
J
Jens Axboe 已提交
3783
				__free_pages(sg_page(&scatterlist[j]), order);
L
Linus Torvalds 已提交
3784 3785 3786 3787
			kfree(sglist);
			return NULL;
		}

3788
		sg_set_page(&scatterlist[i], page, 0, 0);
L
Linus Torvalds 已提交
3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808
	}

	return sglist;
}

/**
 * ipr_free_ucode_buffer - Frees a microcode download buffer
 * @p_dnld:		scatter/gather list pointer
 *
 * Free a DMA'able ucode download buffer previously allocated with
 * ipr_alloc_ucode_buffer
 *
 * Return value:
 * 	nothing
 **/
static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
{
	int i;

	for (i = 0; i < sglist->num_sg; i++)
J
Jens Axboe 已提交
3809
		__free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
L
Linus Torvalds 已提交
3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838

	kfree(sglist);
}

/**
 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
 * @sglist:		scatter/gather list pointer
 * @buffer:		buffer pointer
 * @len:		buffer length
 *
 * Copy a microcode image from a user buffer into a buffer allocated by
 * ipr_alloc_ucode_buffer
 *
 * Return value:
 * 	0 on success / other on failure
 **/
static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
				 u8 *buffer, u32 len)
{
	int bsize_elem, i, result = 0;
	struct scatterlist *scatterlist;
	void *kaddr;

	/* Determine the actual number of bytes per element */
	bsize_elem = PAGE_SIZE * (1 << sglist->order);

	scatterlist = sglist->scatterlist;

	for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
J
Jens Axboe 已提交
3839 3840 3841
		struct page *page = sg_page(&scatterlist[i]);

		kaddr = kmap(page);
L
Linus Torvalds 已提交
3842
		memcpy(kaddr, buffer, bsize_elem);
J
Jens Axboe 已提交
3843
		kunmap(page);
L
Linus Torvalds 已提交
3844 3845 3846 3847 3848 3849 3850 3851 3852 3853

		scatterlist[i].length = bsize_elem;

		if (result != 0) {
			ipr_trace;
			return result;
		}
	}

	if (len % bsize_elem) {
J
Jens Axboe 已提交
3854 3855 3856
		struct page *page = sg_page(&scatterlist[i]);

		kaddr = kmap(page);
L
Linus Torvalds 已提交
3857
		memcpy(kaddr, buffer, len % bsize_elem);
J
Jens Axboe 已提交
3858
		kunmap(page);
L
Linus Torvalds 已提交
3859 3860 3861 3862 3863 3864 3865 3866

		scatterlist[i].length = len % bsize_elem;
	}

	sglist->buffer_len = len;
	return result;
}

3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897
/**
 * ipr_build_ucode_ioadl64 - Build a microcode download IOADL
 * @ipr_cmd:		ipr command struct
 * @sglist:		scatter/gather list
 *
 * Builds a microcode download IOA data list (IOADL).
 *
 **/
static void ipr_build_ucode_ioadl64(struct ipr_cmnd *ipr_cmd,
				    struct ipr_sglist *sglist)
{
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
	struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
	struct scatterlist *scatterlist = sglist->scatterlist;
	int i;

	ipr_cmd->dma_use_sg = sglist->num_dma_sg;
	ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
	ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);

	ioarcb->ioadl_len =
		cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
	for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
		ioadl64[i].flags = cpu_to_be32(IPR_IOADL_FLAGS_WRITE);
		ioadl64[i].data_len = cpu_to_be32(sg_dma_len(&scatterlist[i]));
		ioadl64[i].address = cpu_to_be64(sg_dma_address(&scatterlist[i]));
	}

	ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
}

L
Linus Torvalds 已提交
3898
/**
3899
 * ipr_build_ucode_ioadl - Build a microcode download IOADL
L
Linus Torvalds 已提交
3900 3901 3902
 * @ipr_cmd:	ipr command struct
 * @sglist:		scatter/gather list
 *
3903
 * Builds a microcode download IOA data list (IOADL).
L
Linus Torvalds 已提交
3904 3905
 *
 **/
3906 3907
static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
				  struct ipr_sglist *sglist)
L
Linus Torvalds 已提交
3908 3909
{
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3910
	struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
L
Linus Torvalds 已提交
3911 3912 3913
	struct scatterlist *scatterlist = sglist->scatterlist;
	int i;

3914
	ipr_cmd->dma_use_sg = sglist->num_dma_sg;
L
Linus Torvalds 已提交
3915
	ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3916 3917 3918
	ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);

	ioarcb->ioadl_len =
L
Linus Torvalds 已提交
3919 3920 3921 3922 3923 3924 3925 3926 3927
		cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);

	for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
		ioadl[i].flags_and_data_len =
			cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
		ioadl[i].address =
			cpu_to_be32(sg_dma_address(&scatterlist[i]));
	}

3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947
	ioadl[i-1].flags_and_data_len |=
		cpu_to_be32(IPR_IOADL_FLAGS_LAST);
}

/**
 * ipr_update_ioa_ucode - Update IOA's microcode
 * @ioa_cfg:	ioa config struct
 * @sglist:		scatter/gather list
 *
 * Initiate an adapter reset to update the IOA's microcode
 *
 * Return value:
 * 	0 on success / -EIO on failure
 **/
static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
				struct ipr_sglist *sglist)
{
	unsigned long lock_flags;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3948
	while (ioa_cfg->in_reset_reload) {
3949 3950 3951 3952
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
		spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	}
3953 3954 3955 3956 3957 3958

	if (ioa_cfg->ucode_sglist) {
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		dev_err(&ioa_cfg->pdev->dev,
			"Microcode download already in progress\n");
		return -EIO;
L
Linus Torvalds 已提交
3959
	}
3960

A
Anton Blanchard 已提交
3961 3962 3963
	sglist->num_dma_sg = dma_map_sg(&ioa_cfg->pdev->dev,
					sglist->scatterlist, sglist->num_sg,
					DMA_TO_DEVICE);
3964 3965 3966 3967 3968

	if (!sglist->num_dma_sg) {
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		dev_err(&ioa_cfg->pdev->dev,
			"Failed to map microcode download buffer!\n");
L
Linus Torvalds 已提交
3969 3970 3971
		return -EIO;
	}

3972 3973 3974 3975 3976 3977 3978 3979
	ioa_cfg->ucode_sglist = sglist;
	ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	ioa_cfg->ucode_sglist = NULL;
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
L
Linus Torvalds 已提交
3980 3981 3982 3983 3984
	return 0;
}

/**
 * ipr_store_update_fw - Update the firmware on the adapter
3985 3986 3987
 * @class_dev:	device struct
 * @buf:	buffer
 * @count:	buffer size
L
Linus Torvalds 已提交
3988 3989 3990 3991 3992 3993
 *
 * This function will update the firmware on the adapter.
 *
 * Return value:
 * 	count on success / other on failure
 **/
3994 3995 3996
static ssize_t ipr_store_update_fw(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t count)
L
Linus Torvalds 已提交
3997
{
3998
	struct Scsi_Host *shost = class_to_shost(dev);
L
Linus Torvalds 已提交
3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	struct ipr_ucode_image_header *image_hdr;
	const struct firmware *fw_entry;
	struct ipr_sglist *sglist;
	char fname[100];
	char *src;
	int len, result, dnld_size;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	len = snprintf(fname, 99, "%s", buf);
	fname[len-1] = '\0';

4013
	if (request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
L
Linus Torvalds 已提交
4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034
		dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
		return -EIO;
	}

	image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;

	src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
	dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
	sglist = ipr_alloc_ucode_buffer(dnld_size);

	if (!sglist) {
		dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
		release_firmware(fw_entry);
		return -ENOMEM;
	}

	result = ipr_copy_ucode_buffer(sglist, src, dnld_size);

	if (result) {
		dev_err(&ioa_cfg->pdev->dev,
			"Microcode buffer copy to DMA buffer failed\n");
4035
		goto out;
L
Linus Torvalds 已提交
4036 4037
	}

4038 4039
	ipr_info("Updating microcode, please be patient.  This may take up to 30 minutes.\n");

4040
	result = ipr_update_ioa_ucode(ioa_cfg, sglist);
L
Linus Torvalds 已提交
4041

4042 4043 4044
	if (!result)
		result = count;
out:
L
Linus Torvalds 已提交
4045 4046
	ipr_free_ucode_buffer(sglist);
	release_firmware(fw_entry);
4047
	return result;
L
Linus Torvalds 已提交
4048 4049
}

4050
static struct device_attribute ipr_update_fw_attr = {
L
Linus Torvalds 已提交
4051 4052 4053 4054 4055 4056 4057
	.attr = {
		.name =		"update_fw",
		.mode =		S_IWUSR,
	},
	.store = ipr_store_update_fw
};

4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087
/**
 * ipr_show_fw_type - Show the adapter's firmware type.
 * @dev:	class device struct
 * @buf:	buffer
 *
 * Return value:
 *	number of bytes printed to buffer
 **/
static ssize_t ipr_show_fw_type(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	unsigned long lock_flags = 0;
	int len;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->sis64);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return len;
}

static struct device_attribute ipr_ioa_fw_type_attr = {
	.attr = {
		.name =		"fw_type",
		.mode =		S_IRUGO,
	},
	.show = ipr_show_fw_type
};

4088
static struct device_attribute *ipr_ioa_attrs[] = {
L
Linus Torvalds 已提交
4089 4090 4091
	&ipr_fw_version_attr,
	&ipr_log_level_attr,
	&ipr_diagnostics_attr,
4092
	&ipr_ioa_state_attr,
L
Linus Torvalds 已提交
4093 4094
	&ipr_ioa_reset_attr,
	&ipr_update_fw_attr,
4095
	&ipr_ioa_fw_type_attr,
4096
	&ipr_iopoll_weight_attr,
L
Linus Torvalds 已提交
4097 4098 4099 4100 4101 4102
	NULL,
};

#ifdef CONFIG_SCSI_IPR_DUMP
/**
 * ipr_read_dump - Dump the adapter
4103
 * @filp:		open sysfs file
L
Linus Torvalds 已提交
4104
 * @kobj:		kobject struct
4105
 * @bin_attr:		bin_attribute struct
L
Linus Torvalds 已提交
4106 4107 4108 4109 4110 4111 4112
 * @buf:		buffer
 * @off:		offset
 * @count:		buffer size
 *
 * Return value:
 *	number of bytes printed to buffer
 **/
4113
static ssize_t ipr_read_dump(struct file *filp, struct kobject *kobj,
4114 4115
			     struct bin_attribute *bin_attr,
			     char *buf, loff_t off, size_t count)
L
Linus Torvalds 已提交
4116
{
4117
	struct device *cdev = container_of(kobj, struct device, kobj);
L
Linus Torvalds 已提交
4118 4119 4120 4121 4122
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	struct ipr_dump *dump;
	unsigned long lock_flags = 0;
	char *src;
4123
	int len, sdt_end;
L
Linus Torvalds 已提交
4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162
	size_t rc = count;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	dump = ioa_cfg->dump;

	if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		return 0;
	}
	kref_get(&dump->kref);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

	if (off > dump->driver_dump.hdr.len) {
		kref_put(&dump->kref, ipr_release_dump);
		return 0;
	}

	if (off + count > dump->driver_dump.hdr.len) {
		count = dump->driver_dump.hdr.len - off;
		rc = count;
	}

	if (count && off < sizeof(dump->driver_dump)) {
		if (off + count > sizeof(dump->driver_dump))
			len = sizeof(dump->driver_dump) - off;
		else
			len = count;
		src = (u8 *)&dump->driver_dump + off;
		memcpy(buf, src, len);
		buf += len;
		off += len;
		count -= len;
	}

	off -= sizeof(dump->driver_dump);

4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173
	if (ioa_cfg->sis64)
		sdt_end = offsetof(struct ipr_ioa_dump, sdt.entry) +
			  (be32_to_cpu(dump->ioa_dump.sdt.hdr.num_entries_used) *
			   sizeof(struct ipr_sdt_entry));
	else
		sdt_end = offsetof(struct ipr_ioa_dump, sdt.entry) +
			  (IPR_FMT2_NUM_SDT_ENTRIES * sizeof(struct ipr_sdt_entry));

	if (count && off < sdt_end) {
		if (off + count > sdt_end)
			len = sdt_end - off;
L
Linus Torvalds 已提交
4174 4175 4176 4177 4178 4179 4180 4181 4182
		else
			len = count;
		src = (u8 *)&dump->ioa_dump + off;
		memcpy(buf, src, len);
		buf += len;
		off += len;
		count -= len;
	}

4183
	off -= sdt_end;
L
Linus Torvalds 已提交
4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211

	while (count) {
		if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
			len = PAGE_ALIGN(off) - off;
		else
			len = count;
		src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
		src += off & ~PAGE_MASK;
		memcpy(buf, src, len);
		buf += len;
		off += len;
		count -= len;
	}

	kref_put(&dump->kref, ipr_release_dump);
	return rc;
}

/**
 * ipr_alloc_dump - Prepare for adapter dump
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 *	0 on success / other on failure
 **/
static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
{
	struct ipr_dump *dump;
4212
	__be32 **ioa_data;
L
Linus Torvalds 已提交
4213 4214
	unsigned long lock_flags = 0;

4215
	dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
L
Linus Torvalds 已提交
4216 4217 4218 4219 4220 4221

	if (!dump) {
		ipr_err("Dump memory allocation failed\n");
		return -ENOMEM;
	}

4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234
	if (ioa_cfg->sis64)
		ioa_data = vmalloc(IPR_FMT3_MAX_NUM_DUMP_PAGES * sizeof(__be32 *));
	else
		ioa_data = vmalloc(IPR_FMT2_MAX_NUM_DUMP_PAGES * sizeof(__be32 *));

	if (!ioa_data) {
		ipr_err("Dump memory allocation failed\n");
		kfree(dump);
		return -ENOMEM;
	}

	dump->ioa_dump.ioa_data = ioa_data;

L
Linus Torvalds 已提交
4235 4236 4237 4238 4239 4240 4241
	kref_init(&dump->kref);
	dump->ioa_cfg = ioa_cfg;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);

	if (INACTIVE != ioa_cfg->sdt_state) {
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4242
		vfree(dump->ioa_dump.ioa_data);
L
Linus Torvalds 已提交
4243 4244 4245 4246 4247 4248
		kfree(dump);
		return 0;
	}

	ioa_cfg->dump = dump;
	ioa_cfg->sdt_state = WAIT_FOR_DUMP;
4249
	if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead && !ioa_cfg->dump_taken) {
L
Linus Torvalds 已提交
4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289
		ioa_cfg->dump_taken = 1;
		schedule_work(&ioa_cfg->work_q);
	}
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

	return 0;
}

/**
 * ipr_free_dump - Free adapter dump memory
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 *	0 on success / other on failure
 **/
static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
{
	struct ipr_dump *dump;
	unsigned long lock_flags = 0;

	ENTER;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	dump = ioa_cfg->dump;
	if (!dump) {
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		return 0;
	}

	ioa_cfg->dump = NULL;
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

	kref_put(&dump->kref, ipr_release_dump);

	LEAVE;
	return 0;
}

/**
 * ipr_write_dump - Setup dump state of adapter
4290
 * @filp:		open sysfs file
L
Linus Torvalds 已提交
4291
 * @kobj:		kobject struct
4292
 * @bin_attr:		bin_attribute struct
L
Linus Torvalds 已提交
4293 4294 4295 4296 4297 4298 4299
 * @buf:		buffer
 * @off:		offset
 * @count:		buffer size
 *
 * Return value:
 *	number of bytes printed to buffer
 **/
4300
static ssize_t ipr_write_dump(struct file *filp, struct kobject *kobj,
4301 4302
			      struct bin_attribute *bin_attr,
			      char *buf, loff_t off, size_t count)
L
Linus Torvalds 已提交
4303
{
4304
	struct device *cdev = container_of(kobj, struct device, kobj);
L
Linus Torvalds 已提交
4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
	int rc;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	if (buf[0] == '1')
		rc = ipr_alloc_dump(ioa_cfg);
	else if (buf[0] == '0')
		rc = ipr_free_dump(ioa_cfg);
	else
		return -EINVAL;

	if (rc)
		return rc;
	else
		return count;
}

static struct bin_attribute ipr_dump_attr = {
	.attr =	{
		.name = "dump",
		.mode = S_IRUSR | S_IWUSR,
	},
	.size = 0,
	.read = ipr_read_dump,
	.write = ipr_write_dump
};
#else
static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
#endif

/**
 * ipr_change_queue_depth - Change the device's queue depth
 * @sdev:	scsi device struct
 * @qdepth:	depth to set
4342
 * @reason:	calling context
L
Linus Torvalds 已提交
4343 4344 4345 4346
 *
 * Return value:
 * 	actual depth set
 **/
4347
static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
L
Linus Torvalds 已提交
4348
{
4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
	struct ipr_resource_entry *res;
	unsigned long lock_flags = 0;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	res = (struct ipr_resource_entry *)sdev->hostdata;

	if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
		qdepth = IPR_MAX_CMD_PER_ATA_LUN;
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

4360
	scsi_change_queue_depth(sdev, qdepth);
L
Linus Torvalds 已提交
4361 4362 4363 4364 4365 4366
	return sdev->queue_depth;
}

/**
 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
 * @dev:	device struct
4367
 * @attr:	device attribute structure
L
Linus Torvalds 已提交
4368 4369 4370 4371 4372
 * @buf:	buffer
 *
 * Return value:
 * 	number of bytes printed to buffer
 **/
4373
static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
4374 4375 4376 4377 4378 4379 4380 4381 4382 4383
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
	struct ipr_resource_entry *res;
	unsigned long lock_flags = 0;
	ssize_t len = -ENXIO;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	res = (struct ipr_resource_entry *)sdev->hostdata;
	if (res)
4384
		len = snprintf(buf, PAGE_SIZE, "%08X\n", res->res_handle);
L
Linus Torvalds 已提交
4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return len;
}

static struct device_attribute ipr_adapter_handle_attr = {
	.attr = {
		.name = 	"adapter_handle",
		.mode =		S_IRUSR,
	},
	.show = ipr_show_adapter_handle
};

4397
/**
4398 4399
 * ipr_show_resource_path - Show the resource path or the resource address for
 *			    this device.
4400
 * @dev:	device struct
4401
 * @attr:	device attribute structure
4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417
 * @buf:	buffer
 *
 * Return value:
 * 	number of bytes printed to buffer
 **/
static ssize_t ipr_show_resource_path(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
	struct ipr_resource_entry *res;
	unsigned long lock_flags = 0;
	ssize_t len = -ENXIO;
	char buffer[IPR_MAX_RES_PATH_LENGTH];

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	res = (struct ipr_resource_entry *)sdev->hostdata;
4418
	if (res && ioa_cfg->sis64)
4419
		len = snprintf(buf, PAGE_SIZE, "%s\n",
4420 4421
			       __ipr_format_res_path(res->res_path, buffer,
						     sizeof(buffer)));
4422 4423 4424 4425
	else if (res)
		len = snprintf(buf, PAGE_SIZE, "%d:%d:%d:%d\n", ioa_cfg->host->host_no,
			       res->bus, res->target, res->lun);

4426 4427 4428 4429 4430 4431 4432
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return len;
}

static struct device_attribute ipr_resource_path_attr = {
	.attr = {
		.name = 	"resource_path",
4433
		.mode =		S_IRUGO,
4434 4435 4436 4437
	},
	.show = ipr_show_resource_path
};

4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457
/**
 * ipr_show_device_id - Show the device_id for this device.
 * @dev:	device struct
 * @attr:	device attribute structure
 * @buf:	buffer
 *
 * Return value:
 *	number of bytes printed to buffer
 **/
static ssize_t ipr_show_device_id(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
	struct ipr_resource_entry *res;
	unsigned long lock_flags = 0;
	ssize_t len = -ENXIO;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	res = (struct ipr_resource_entry *)sdev->hostdata;
	if (res && ioa_cfg->sis64)
4458
		len = snprintf(buf, PAGE_SIZE, "0x%llx\n", be64_to_cpu(res->dev_id));
4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473
	else if (res)
		len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->lun_wwn);

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return len;
}

static struct device_attribute ipr_device_id_attr = {
	.attr = {
		.name =		"device_id",
		.mode =		S_IRUGO,
	},
	.show = ipr_show_device_id
};

4474 4475 4476
/**
 * ipr_show_resource_type - Show the resource type for this device.
 * @dev:	device struct
4477
 * @attr:	device attribute structure
4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508
 * @buf:	buffer
 *
 * Return value:
 *	number of bytes printed to buffer
 **/
static ssize_t ipr_show_resource_type(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
	struct ipr_resource_entry *res;
	unsigned long lock_flags = 0;
	ssize_t len = -ENXIO;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	res = (struct ipr_resource_entry *)sdev->hostdata;

	if (res)
		len = snprintf(buf, PAGE_SIZE, "%x\n", res->type);

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return len;
}

static struct device_attribute ipr_resource_type_attr = {
	.attr = {
		.name =		"resource_type",
		.mode =		S_IRUGO,
	},
	.show = ipr_show_resource_type
};

4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579
/**
 * ipr_show_raw_mode - Show the adapter's raw mode
 * @dev:	class device struct
 * @buf:	buffer
 *
 * Return value:
 * 	number of bytes printed to buffer
 **/
static ssize_t ipr_show_raw_mode(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
	struct ipr_resource_entry *res;
	unsigned long lock_flags = 0;
	ssize_t len;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	res = (struct ipr_resource_entry *)sdev->hostdata;
	if (res)
		len = snprintf(buf, PAGE_SIZE, "%d\n", res->raw_mode);
	else
		len = -ENXIO;
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return len;
}

/**
 * ipr_store_raw_mode - Change the adapter's raw mode
 * @dev:	class device struct
 * @buf:	buffer
 *
 * Return value:
 * 	number of bytes printed to buffer
 **/
static ssize_t ipr_store_raw_mode(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
	struct ipr_resource_entry *res;
	unsigned long lock_flags = 0;
	ssize_t len;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	res = (struct ipr_resource_entry *)sdev->hostdata;
	if (res) {
		if (ioa_cfg->sis64 && ipr_is_af_dasd_device(res)) {
			res->raw_mode = simple_strtoul(buf, NULL, 10);
			len = strlen(buf);
			if (res->sdev)
				sdev_printk(KERN_INFO, res->sdev, "raw mode is %s\n",
					res->raw_mode ? "enabled" : "disabled");
		} else
			len = -EINVAL;
	} else
		len = -ENXIO;
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return len;
}

static struct device_attribute ipr_raw_mode_attr = {
	.attr = {
		.name =		"raw_mode",
		.mode =		S_IRUGO | S_IWUSR,
	},
	.show = ipr_show_raw_mode,
	.store = ipr_store_raw_mode
};

L
Linus Torvalds 已提交
4580 4581
static struct device_attribute *ipr_dev_attrs[] = {
	&ipr_adapter_handle_attr,
4582
	&ipr_resource_path_attr,
4583
	&ipr_device_id_attr,
4584
	&ipr_resource_type_attr,
4585
	&ipr_raw_mode_attr,
L
Linus Torvalds 已提交
4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623
	NULL,
};

/**
 * ipr_biosparam - Return the HSC mapping
 * @sdev:			scsi device struct
 * @block_device:	block device pointer
 * @capacity:		capacity of the device
 * @parm:			Array containing returned HSC values.
 *
 * This function generates the HSC parms that fdisk uses.
 * We want to make sure we return something that places partitions
 * on 4k boundaries for best performance with the IOA.
 *
 * Return value:
 * 	0 on success
 **/
static int ipr_biosparam(struct scsi_device *sdev,
			 struct block_device *block_device,
			 sector_t capacity, int *parm)
{
	int heads, sectors;
	sector_t cylinders;

	heads = 128;
	sectors = 32;

	cylinders = capacity;
	sector_div(cylinders, (128 * 32));

	/* return result */
	parm[0] = heads;
	parm[1] = sectors;
	parm[2] = cylinders;

	return 0;
}

4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637
/**
 * ipr_find_starget - Find target based on bus/target.
 * @starget:	scsi target struct
 *
 * Return value:
 * 	resource entry pointer if found / NULL if not found
 **/
static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
{
	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
	struct ipr_resource_entry *res;

	list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4638
		if ((res->bus == starget->channel) &&
4639
		    (res->target == starget->id)) {
4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708
			return res;
		}
	}

	return NULL;
}

static struct ata_port_info sata_port_info;

/**
 * ipr_target_alloc - Prepare for commands to a SCSI target
 * @starget:	scsi target struct
 *
 * If the device is a SATA device, this function allocates an
 * ATA port with libata, else it does nothing.
 *
 * Return value:
 * 	0 on success / non-0 on failure
 **/
static int ipr_target_alloc(struct scsi_target *starget)
{
	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
	struct ipr_sata_port *sata_port;
	struct ata_port *ap;
	struct ipr_resource_entry *res;
	unsigned long lock_flags;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	res = ipr_find_starget(starget);
	starget->hostdata = NULL;

	if (res && ipr_is_gata(res)) {
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
		if (!sata_port)
			return -ENOMEM;

		ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
		if (ap) {
			spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
			sata_port->ioa_cfg = ioa_cfg;
			sata_port->ap = ap;
			sata_port->res = res;

			res->sata_port = sata_port;
			ap->private_data = sata_port;
			starget->hostdata = sata_port;
		} else {
			kfree(sata_port);
			return -ENOMEM;
		}
	}
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

	return 0;
}

/**
 * ipr_target_destroy - Destroy a SCSI target
 * @starget:	scsi target struct
 *
 * If the device was a SATA device, this function frees the libata
 * ATA port, else it does nothing.
 *
 **/
static void ipr_target_destroy(struct scsi_target *starget)
{
	struct ipr_sata_port *sata_port = starget->hostdata;
4709 4710 4711 4712
	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;

	if (ioa_cfg->sis64) {
4713 4714 4715 4716 4717 4718 4719 4720
		if (!ipr_find_starget(starget)) {
			if (starget->channel == IPR_ARRAY_VIRTUAL_BUS)
				clear_bit(starget->id, ioa_cfg->array_ids);
			else if (starget->channel == IPR_VSET_VIRTUAL_BUS)
				clear_bit(starget->id, ioa_cfg->vset_ids);
			else if (starget->channel == 0)
				clear_bit(starget->id, ioa_cfg->target_ids);
		}
4721
	}
4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742

	if (sata_port) {
		starget->hostdata = NULL;
		ata_sas_port_destroy(sata_port->ap);
		kfree(sata_port);
	}
}

/**
 * ipr_find_sdev - Find device based on bus/target/lun.
 * @sdev:	scsi device struct
 *
 * Return value:
 * 	resource entry pointer if found / NULL if not found
 **/
static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
{
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
	struct ipr_resource_entry *res;

	list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4743 4744 4745
		if ((res->bus == sdev->channel) &&
		    (res->target == sdev->id) &&
		    (res->lun == sdev->lun))
4746 4747 4748 4749 4750 4751
			return res;
	}

	return NULL;
}

L
Linus Torvalds 已提交
4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769
/**
 * ipr_slave_destroy - Unconfigure a SCSI device
 * @sdev:	scsi device struct
 *
 * Return value:
 * 	nothing
 **/
static void ipr_slave_destroy(struct scsi_device *sdev)
{
	struct ipr_resource_entry *res;
	struct ipr_ioa_cfg *ioa_cfg;
	unsigned long lock_flags = 0;

	ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	res = (struct ipr_resource_entry *) sdev->hostdata;
	if (res) {
4770
		if (res->sata_port)
T
Tejun Heo 已提交
4771
			res->sata_port->ap->link.device[0].class = ATA_DEV_NONE;
L
Linus Torvalds 已提交
4772 4773
		sdev->hostdata = NULL;
		res->sdev = NULL;
4774
		res->sata_port = NULL;
L
Linus Torvalds 已提交
4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791
	}
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
}

/**
 * ipr_slave_configure - Configure a SCSI device
 * @sdev:	scsi device struct
 *
 * This function configures the specified scsi device.
 *
 * Return value:
 * 	0 on success
 **/
static int ipr_slave_configure(struct scsi_device *sdev)
{
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
	struct ipr_resource_entry *res;
4792
	struct ata_port *ap = NULL;
L
Linus Torvalds 已提交
4793
	unsigned long lock_flags = 0;
4794
	char buffer[IPR_MAX_RES_PATH_LENGTH];
L
Linus Torvalds 已提交
4795 4796 4797 4798 4799 4800

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	res = sdev->hostdata;
	if (res) {
		if (ipr_is_af_dasd_device(res))
			sdev->type = TYPE_RAID;
4801
		if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
L
Linus Torvalds 已提交
4802
			sdev->scsi_level = 4;
4803 4804
			sdev->no_uld_attach = 1;
		}
L
Linus Torvalds 已提交
4805
		if (ipr_is_vset_device(res)) {
4806
			sdev->scsi_level = SCSI_SPC_3;
J
Jens Axboe 已提交
4807 4808
			blk_queue_rq_timeout(sdev->request_queue,
					     IPR_VSET_RW_TIMEOUT);
4809
			blk_queue_max_hw_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
L
Linus Torvalds 已提交
4810
		}
4811 4812 4813 4814 4815
		if (ipr_is_gata(res) && res->sata_port)
			ap = res->sata_port->ap;
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

		if (ap) {
4816
			scsi_change_queue_depth(sdev, IPR_MAX_CMD_PER_ATA_LUN);
4817
			ata_sas_slave_configure(sdev, ap);
4818 4819
		}

4820 4821
		if (ioa_cfg->sis64)
			sdev_printk(KERN_INFO, sdev, "Resource path: %s\n",
4822 4823
				    ipr_format_res_path(ioa_cfg,
				res->res_path, buffer, sizeof(buffer)));
4824
		return 0;
L
Linus Torvalds 已提交
4825 4826 4827 4828 4829
	}
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return 0;
}

4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847
/**
 * ipr_ata_slave_alloc - Prepare for commands to a SATA device
 * @sdev:	scsi device struct
 *
 * This function initializes an ATA port so that future commands
 * sent through queuecommand will work.
 *
 * Return value:
 * 	0 on success
 **/
static int ipr_ata_slave_alloc(struct scsi_device *sdev)
{
	struct ipr_sata_port *sata_port = NULL;
	int rc = -ENXIO;

	ENTER;
	if (sdev->sdev_target)
		sata_port = sdev->sdev_target->hostdata;
4848
	if (sata_port) {
4849
		rc = ata_sas_port_init(sata_port->ap);
4850 4851 4852 4853
		if (rc == 0)
			rc = ata_sas_sync_probe(sata_port->ap);
	}

4854 4855 4856 4857 4858 4859 4860
	if (rc)
		ipr_slave_destroy(sdev);

	LEAVE;
	return rc;
}

L
Linus Torvalds 已提交
4861 4862 4863 4864 4865 4866 4867 4868 4869 4870
/**
 * ipr_slave_alloc - Prepare for commands to a device.
 * @sdev:	scsi device struct
 *
 * This function saves a pointer to the resource entry
 * in the scsi device struct if the device exists. We
 * can then use this pointer in ipr_queuecommand when
 * handling new commands.
 *
 * Return value:
4871
 * 	0 on success / -ENXIO if device does not exist
L
Linus Torvalds 已提交
4872 4873 4874 4875 4876 4877
 **/
static int ipr_slave_alloc(struct scsi_device *sdev)
{
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
	struct ipr_resource_entry *res;
	unsigned long lock_flags;
4878
	int rc = -ENXIO;
L
Linus Torvalds 已提交
4879 4880 4881 4882 4883

	sdev->hostdata = NULL;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);

4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895
	res = ipr_find_sdev(sdev);
	if (res) {
		res->sdev = sdev;
		res->add_to_ml = 0;
		res->in_erp = 0;
		sdev->hostdata = res;
		if (!ipr_is_naca_model(res))
			res->needs_sync_complete = 1;
		rc = 0;
		if (ipr_is_gata(res)) {
			spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
			return ipr_ata_slave_alloc(sdev);
L
Linus Torvalds 已提交
4896 4897 4898 4899 4900
		}
	}

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

4901
	return rc;
L
Linus Torvalds 已提交
4902 4903
}

4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981
/**
 * ipr_match_lun - Match function for specified LUN
 * @ipr_cmd:	ipr command struct
 * @device:		device to match (sdev)
 *
 * Returns:
 *	1 if command matches sdev / 0 if command does not match sdev
 **/
static int ipr_match_lun(struct ipr_cmnd *ipr_cmd, void *device)
{
	if (ipr_cmd->scsi_cmd && ipr_cmd->scsi_cmd->device == device)
		return 1;
	return 0;
}

/**
 * ipr_wait_for_ops - Wait for matching commands to complete
 * @ipr_cmd:	ipr command struct
 * @device:		device to match (sdev)
 * @match:		match function to use
 *
 * Returns:
 *	SUCCESS / FAILED
 **/
static int ipr_wait_for_ops(struct ipr_ioa_cfg *ioa_cfg, void *device,
			    int (*match)(struct ipr_cmnd *, void *))
{
	struct ipr_cmnd *ipr_cmd;
	int wait;
	unsigned long flags;
	struct ipr_hrr_queue *hrrq;
	signed long timeout = IPR_ABORT_TASK_TIMEOUT;
	DECLARE_COMPLETION_ONSTACK(comp);

	ENTER;
	do {
		wait = 0;

		for_each_hrrq(hrrq, ioa_cfg) {
			spin_lock_irqsave(hrrq->lock, flags);
			list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
				if (match(ipr_cmd, device)) {
					ipr_cmd->eh_comp = &comp;
					wait++;
				}
			}
			spin_unlock_irqrestore(hrrq->lock, flags);
		}

		if (wait) {
			timeout = wait_for_completion_timeout(&comp, timeout);

			if (!timeout) {
				wait = 0;

				for_each_hrrq(hrrq, ioa_cfg) {
					spin_lock_irqsave(hrrq->lock, flags);
					list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
						if (match(ipr_cmd, device)) {
							ipr_cmd->eh_comp = NULL;
							wait++;
						}
					}
					spin_unlock_irqrestore(hrrq->lock, flags);
				}

				if (wait)
					dev_err(&ioa_cfg->pdev->dev, "Timed out waiting for aborted commands\n");
				LEAVE;
				return wait ? FAILED : SUCCESS;
			}
		}
	} while (wait);

	LEAVE;
	return SUCCESS;
}

4982
static int ipr_eh_host_reset(struct scsi_cmnd *cmd)
L
Linus Torvalds 已提交
4983 4984
{
	struct ipr_ioa_cfg *ioa_cfg;
4985 4986
	unsigned long lock_flags = 0;
	int rc = SUCCESS;
L
Linus Torvalds 已提交
4987 4988

	ENTER;
4989 4990
	ioa_cfg = (struct ipr_ioa_cfg *) cmd->device->host->hostdata;
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
L
Linus Torvalds 已提交
4991

4992
	if (!ioa_cfg->in_reset_reload && !ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
4993
		ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
4994 4995
		dev_err(&ioa_cfg->pdev->dev,
			"Adapter being reset as a result of error recovery.\n");
L
Linus Torvalds 已提交
4996

4997 4998 4999
		if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
			ioa_cfg->sdt_state = GET_DUMP;
	}
L
Linus Torvalds 已提交
5000

5001 5002 5003
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5004

5005 5006 5007 5008 5009 5010
	/* If we got hit with a host reset while we were already resetting
	 the adapter for some reason, and the reset failed. */
	if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
		ipr_trace;
		rc = FAILED;
	}
5011

5012 5013
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	LEAVE;
5014 5015 5016
	return rc;
}

B
Brian King 已提交
5017 5018 5019 5020 5021 5022 5023 5024
/**
 * ipr_device_reset - Reset the device
 * @ioa_cfg:	ioa config struct
 * @res:		resource entry struct
 *
 * This function issues a device reset to the affected device.
 * If the device is a SCSI device, a LUN reset will be sent
 * to the device first. If that does not work, a target reset
5025 5026
 * will be sent. If the device is a SATA device, a PHY reset will
 * be sent.
B
Brian King 已提交
5027 5028 5029 5030 5031 5032 5033 5034 5035 5036
 *
 * Return value:
 *	0 on success / non-zero on failure
 **/
static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
			    struct ipr_resource_entry *res)
{
	struct ipr_cmnd *ipr_cmd;
	struct ipr_ioarcb *ioarcb;
	struct ipr_cmd_pkt *cmd_pkt;
5037
	struct ipr_ioarcb_ata_regs *regs;
B
Brian King 已提交
5038 5039 5040 5041 5042 5043
	u32 ioasc;

	ENTER;
	ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
	ioarcb = &ipr_cmd->ioarcb;
	cmd_pkt = &ioarcb->cmd_pkt;
5044 5045 5046 5047 5048 5049

	if (ipr_cmd->ioa_cfg->sis64) {
		regs = &ipr_cmd->i.ata_ioadl.regs;
		ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
	} else
		regs = &ioarcb->u.add_data.u.regs;
B
Brian King 已提交
5050

5051
	ioarcb->res_handle = res->res_handle;
B
Brian King 已提交
5052 5053
	cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
	cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
5054 5055
	if (ipr_is_gata(res)) {
		cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
5056
		ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(regs->flags));
5057 5058
		regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
	}
B
Brian King 已提交
5059 5060

	ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
5061
	ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
5062
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5063 5064 5065 5066 5067 5068 5069 5070
	if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET) {
		if (ipr_cmd->ioa_cfg->sis64)
			memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
			       sizeof(struct ipr_ioasa_gata));
		else
			memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
			       sizeof(struct ipr_ioasa_gata));
	}
B
Brian King 已提交
5071 5072

	LEAVE;
5073
	return IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0;
B
Brian King 已提交
5074 5075
}

5076 5077
/**
 * ipr_sata_reset - Reset the SATA port
T
Tejun Heo 已提交
5078
 * @link:	SATA link to reset
5079 5080
 * @classes:	class of the attached device
 *
T
Tejun Heo 已提交
5081
 * This function issues a SATA phy reset to the affected ATA link.
5082 5083 5084 5085
 *
 * Return value:
 *	0 on success / non-zero on failure
 **/
T
Tejun Heo 已提交
5086
static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
A
Andrew Morton 已提交
5087
				unsigned long deadline)
5088
{
T
Tejun Heo 已提交
5089
	struct ipr_sata_port *sata_port = link->ap->private_data;
5090 5091 5092 5093 5094 5095 5096
	struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
	struct ipr_resource_entry *res;
	unsigned long lock_flags = 0;
	int rc = -ENXIO;

	ENTER;
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5097
	while (ioa_cfg->in_reset_reload) {
5098 5099 5100 5101 5102
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
		spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	}

5103 5104 5105
	res = sata_port->res;
	if (res) {
		rc = ipr_device_reset(ioa_cfg, res);
5106
		*classes = res->ata_class;
5107 5108 5109 5110 5111 5112 5113
	}

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	LEAVE;
	return rc;
}

L
Linus Torvalds 已提交
5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124
/**
 * ipr_eh_dev_reset - Reset the device
 * @scsi_cmd:	scsi command struct
 *
 * This function issues a device reset to the affected device.
 * A LUN reset will be sent to the device first. If that does
 * not work, a target reset will be sent.
 *
 * Return value:
 *	SUCCESS / FAILED
 **/
5125
static int __ipr_eh_dev_reset(struct scsi_cmnd *scsi_cmd)
L
Linus Torvalds 已提交
5126 5127 5128 5129
{
	struct ipr_cmnd *ipr_cmd;
	struct ipr_ioa_cfg *ioa_cfg;
	struct ipr_resource_entry *res;
5130 5131
	struct ata_port *ap;
	int rc = 0;
5132
	struct ipr_hrr_queue *hrrq;
L
Linus Torvalds 已提交
5133 5134 5135 5136 5137

	ENTER;
	ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
	res = scsi_cmd->device->hostdata;

5138
	if (!res)
L
Linus Torvalds 已提交
5139 5140 5141 5142 5143 5144 5145 5146 5147
		return FAILED;

	/*
	 * If we are currently going through reset/reload, return failed. This will force the
	 * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
	 * reset to complete
	 */
	if (ioa_cfg->in_reset_reload)
		return FAILED;
5148
	if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
L
Linus Torvalds 已提交
5149 5150
		return FAILED;

5151
	for_each_hrrq(hrrq, ioa_cfg) {
5152
		spin_lock(&hrrq->_lock);
5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163
		list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
			if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
				if (ipr_cmd->scsi_cmd)
					ipr_cmd->done = ipr_scsi_eh_done;
				if (ipr_cmd->qc)
					ipr_cmd->done = ipr_sata_eh_done;
				if (ipr_cmd->qc &&
				    !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
					ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
					ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
				}
B
Brian King 已提交
5164
			}
L
Linus Torvalds 已提交
5165
		}
5166
		spin_unlock(&hrrq->_lock);
L
Linus Torvalds 已提交
5167 5168
	}
	res->resetting_device = 1;
5169
	scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
5170 5171 5172 5173

	if (ipr_is_gata(res) && res->sata_port) {
		ap = res->sata_port->ap;
		spin_unlock_irq(scsi_cmd->device->host->host_lock);
5174
		ata_std_error_handler(ap);
5175
		spin_lock_irq(scsi_cmd->device->host->host_lock);
5176

5177
		for_each_hrrq(hrrq, ioa_cfg) {
5178
			spin_lock(&hrrq->_lock);
5179 5180 5181 5182 5183 5184 5185
			list_for_each_entry(ipr_cmd,
					    &hrrq->hrrq_pending_q, queue) {
				if (ipr_cmd->ioarcb.res_handle ==
				    res->res_handle) {
					rc = -EIO;
					break;
				}
5186
			}
5187
			spin_unlock(&hrrq->_lock);
5188
		}
5189 5190
	} else
		rc = ipr_device_reset(ioa_cfg, res);
L
Linus Torvalds 已提交
5191
	res->resetting_device = 0;
5192
	res->reset_occurred = 1;
L
Linus Torvalds 已提交
5193 5194

	LEAVE;
5195
	return rc ? FAILED : SUCCESS;
L
Linus Torvalds 已提交
5196 5197
}

5198
static int ipr_eh_dev_reset(struct scsi_cmnd *cmd)
5199 5200
{
	int rc;
5201 5202 5203
	struct ipr_ioa_cfg *ioa_cfg;

	ioa_cfg = (struct ipr_ioa_cfg *) cmd->device->host->hostdata;
5204 5205 5206 5207 5208

	spin_lock_irq(cmd->device->host->host_lock);
	rc = __ipr_eh_dev_reset(cmd);
	spin_unlock_irq(cmd->device->host->host_lock);

5209 5210 5211
	if (rc == SUCCESS)
		rc = ipr_wait_for_ops(ioa_cfg, cmd->device, ipr_match_lun);

5212 5213 5214
	return rc;
}

L
Linus Torvalds 已提交
5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229
/**
 * ipr_bus_reset_done - Op done function for bus reset.
 * @ipr_cmd:	ipr command struct
 *
 * This function is the op done function for a bus reset
 *
 * Return value:
 * 	none
 **/
static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_resource_entry *res;

	ENTER;
5230 5231 5232 5233 5234 5235
	if (!ioa_cfg->sis64)
		list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
			if (res->res_handle == ipr_cmd->ioarcb.res_handle) {
				scsi_report_bus_reset(ioa_cfg->host, res->bus);
				break;
			}
L
Linus Torvalds 已提交
5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246
		}

	/*
	 * If abort has not completed, indicate the reset has, else call the
	 * abort's done function to wake the sleeping eh thread
	 */
	if (ipr_cmd->sibling->sibling)
		ipr_cmd->sibling->sibling = NULL;
	else
		ipr_cmd->sibling->done(ipr_cmd->sibling);

5247
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
L
Linus Torvalds 已提交
5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275
	LEAVE;
}

/**
 * ipr_abort_timeout - An abort task has timed out
 * @ipr_cmd:	ipr command struct
 *
 * This function handles when an abort task times out. If this
 * happens we issue a bus reset since we have resources tied
 * up that must be freed before returning to the midlayer.
 *
 * Return value:
 *	none
 **/
static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_cmnd *reset_cmd;
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_cmd_pkt *cmd_pkt;
	unsigned long lock_flags = 0;

	ENTER;
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		return;
	}

5276
	sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
L
Linus Torvalds 已提交
5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299
	reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
	ipr_cmd->sibling = reset_cmd;
	reset_cmd->sibling = ipr_cmd;
	reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
	cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
	cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
	cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
	cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;

	ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	LEAVE;
}

/**
 * ipr_cancel_op - Cancel specified op
 * @scsi_cmd:	scsi command struct
 *
 * This function cancels specified op.
 *
 * Return value:
 *	SUCCESS / FAILED
 **/
5300
static int ipr_cancel_op(struct scsi_cmnd *scsi_cmd)
L
Linus Torvalds 已提交
5301 5302 5303 5304 5305
{
	struct ipr_cmnd *ipr_cmd;
	struct ipr_ioa_cfg *ioa_cfg;
	struct ipr_resource_entry *res;
	struct ipr_cmd_pkt *cmd_pkt;
5306
	u32 ioasc, int_reg;
L
Linus Torvalds 已提交
5307
	int op_found = 0;
5308
	struct ipr_hrr_queue *hrrq;
L
Linus Torvalds 已提交
5309 5310 5311 5312 5313

	ENTER;
	ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
	res = scsi_cmd->device->hostdata;

5314 5315 5316 5317
	/* If we are currently going through reset/reload, return failed.
	 * This will force the mid-layer to call ipr_eh_host_reset,
	 * which will then go to sleep and wait for the reset to complete
	 */
5318 5319
	if (ioa_cfg->in_reset_reload ||
	    ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
5320
		return FAILED;
5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331
	if (!res)
		return FAILED;

	/*
	 * If we are aborting a timed out op, chances are that the timeout was caused
	 * by a still not detected EEH error. In such cases, reading a register will
	 * trigger the EEH recovery infrastructure.
	 */
	int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);

	if (!ipr_is_gscsi(res))
L
Linus Torvalds 已提交
5332 5333
		return FAILED;

5334
	for_each_hrrq(hrrq, ioa_cfg) {
5335
		spin_lock(&hrrq->_lock);
5336 5337 5338 5339 5340 5341
		list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
			if (ipr_cmd->scsi_cmd == scsi_cmd) {
				ipr_cmd->done = ipr_scsi_eh_done;
				op_found = 1;
				break;
			}
L
Linus Torvalds 已提交
5342
		}
5343
		spin_unlock(&hrrq->_lock);
L
Linus Torvalds 已提交
5344 5345 5346 5347 5348 5349
	}

	if (!op_found)
		return SUCCESS;

	ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5350
	ipr_cmd->ioarcb.res_handle = res->res_handle;
L
Linus Torvalds 已提交
5351 5352 5353 5354 5355
	cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
	cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
	cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
	ipr_cmd->u.sdev = scsi_cmd->device;

5356 5357
	scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
		    scsi_cmd->cmnd[0]);
L
Linus Torvalds 已提交
5358
	ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
5359
	ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
L
Linus Torvalds 已提交
5360 5361 5362 5363 5364 5365 5366 5367 5368 5369

	/*
	 * If the abort task timed out and we sent a bus reset, we will get
	 * one the following responses to the abort
	 */
	if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
		ioasc = 0;
		ipr_trace;
	}

5370
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
5371 5372
	if (!ipr_is_naca_model(res))
		res->needs_sync_complete = 1;
L
Linus Torvalds 已提交
5373 5374

	LEAVE;
5375
	return IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
L
Linus Torvalds 已提交
5376 5377 5378 5379 5380 5381 5382
}

/**
 * ipr_eh_abort - Abort a single op
 * @scsi_cmd:	scsi command struct
 *
 * Return value:
5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404
 *	0 if scan in progress / 1 if scan is complete
 **/
static int ipr_scan_finished(struct Scsi_Host *shost, unsigned long elapsed_time)
{
	unsigned long lock_flags;
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
	int rc = 0;

	spin_lock_irqsave(shost->host_lock, lock_flags);
	if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead || ioa_cfg->scan_done)
		rc = 1;
	if ((elapsed_time/HZ) > (ioa_cfg->transop_timeout * 2))
		rc = 1;
	spin_unlock_irqrestore(shost->host_lock, lock_flags);
	return rc;
}

/**
 * ipr_eh_host_reset - Reset the host adapter
 * @scsi_cmd:	scsi command struct
 *
 * Return value:
L
Linus Torvalds 已提交
5405 5406
 * 	SUCCESS / FAILED
 **/
5407
static int ipr_eh_abort(struct scsi_cmnd *scsi_cmd)
L
Linus Torvalds 已提交
5408
{
5409 5410
	unsigned long flags;
	int rc;
5411
	struct ipr_ioa_cfg *ioa_cfg;
L
Linus Torvalds 已提交
5412 5413 5414

	ENTER;

5415 5416
	ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;

5417 5418 5419
	spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
	rc = ipr_cancel_op(scsi_cmd);
	spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
L
Linus Torvalds 已提交
5420

5421 5422
	if (rc == SUCCESS)
		rc = ipr_wait_for_ops(ioa_cfg, scsi_cmd->device, ipr_match_lun);
L
Linus Torvalds 已提交
5423
	LEAVE;
5424
	return rc;
L
Linus Torvalds 已提交
5425 5426 5427 5428 5429
}

/**
 * ipr_handle_other_interrupt - Handle "other" interrupts
 * @ioa_cfg:	ioa config struct
5430
 * @int_reg:	interrupt register
L
Linus Torvalds 已提交
5431 5432 5433 5434
 *
 * Return value:
 * 	IRQ_NONE / IRQ_HANDLED
 **/
5435
static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
5436
					      u32 int_reg)
L
Linus Torvalds 已提交
5437 5438
{
	irqreturn_t rc = IRQ_HANDLED;
5439
	u32 int_mask_reg;
5440

5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464
	int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
	int_reg &= ~int_mask_reg;

	/* If an interrupt on the adapter did not occur, ignore it.
	 * Or in the case of SIS 64, check for a stage change interrupt.
	 */
	if ((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0) {
		if (ioa_cfg->sis64) {
			int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
			int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
			if (int_reg & IPR_PCII_IPL_STAGE_CHANGE) {

				/* clear stage change */
				writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.clr_interrupt_reg);
				int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
				list_del(&ioa_cfg->reset_cmd->queue);
				del_timer(&ioa_cfg->reset_cmd->timer);
				ipr_reset_ioa_job(ioa_cfg->reset_cmd);
				return IRQ_HANDLED;
			}
		}

		return IRQ_NONE;
	}
L
Linus Torvalds 已提交
5465 5466 5467 5468 5469 5470 5471 5472 5473

	if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
		/* Mask the interrupt */
		writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
		int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);

		list_del(&ioa_cfg->reset_cmd->queue);
		del_timer(&ioa_cfg->reset_cmd->timer);
		ipr_reset_ioa_job(ioa_cfg->reset_cmd);
5474
	} else if ((int_reg & IPR_PCII_HRRQ_UPDATED) == int_reg) {
5475 5476 5477 5478 5479 5480 5481 5482
		if (ioa_cfg->clear_isr) {
			if (ipr_debug && printk_ratelimit())
				dev_err(&ioa_cfg->pdev->dev,
					"Spurious interrupt detected. 0x%08X\n", int_reg);
			writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg32);
			int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
			return IRQ_NONE;
		}
L
Linus Torvalds 已提交
5483 5484 5485
	} else {
		if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
			ioa_cfg->ioa_unit_checked = 1;
5486 5487 5488
		else if (int_reg & IPR_PCII_NO_HOST_RRQ)
			dev_err(&ioa_cfg->pdev->dev,
				"No Host RRQ. 0x%08X\n", int_reg);
L
Linus Torvalds 已提交
5489 5490 5491 5492 5493 5494 5495 5496 5497 5498
		else
			dev_err(&ioa_cfg->pdev->dev,
				"Permanent IOA failure. 0x%08X\n", int_reg);

		if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
			ioa_cfg->sdt_state = GET_DUMP;

		ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
		ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
	}
5499

L
Linus Torvalds 已提交
5500 5501 5502
	return rc;
}

5503 5504 5505 5506 5507 5508 5509 5510
/**
 * ipr_isr_eh - Interrupt service routine error handler
 * @ioa_cfg:	ioa config struct
 * @msg:	message to log
 *
 * Return value:
 * 	none
 **/
5511
static void ipr_isr_eh(struct ipr_ioa_cfg *ioa_cfg, char *msg, u16 number)
5512 5513
{
	ioa_cfg->errors_logged++;
5514
	dev_err(&ioa_cfg->pdev->dev, "%s %d\n", msg, number);
5515 5516 5517 5518 5519 5520 5521

	if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
		ioa_cfg->sdt_state = GET_DUMP;

	ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
}

5522
static int ipr_process_hrrq(struct ipr_hrr_queue *hrr_queue, int budget,
5523 5524 5525 5526 5527 5528 5529 5530 5531
						struct list_head *doneq)
{
	u32 ioasc;
	u16 cmd_index;
	struct ipr_cmnd *ipr_cmd;
	struct ipr_ioa_cfg *ioa_cfg = hrr_queue->ioa_cfg;
	int num_hrrq = 0;

	/* If interrupts are disabled, ignore the interrupt */
5532
	if (!hrr_queue->allow_interrupts)
5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563
		return 0;

	while ((be32_to_cpu(*hrr_queue->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
	       hrr_queue->toggle_bit) {

		cmd_index = (be32_to_cpu(*hrr_queue->hrrq_curr) &
			     IPR_HRRQ_REQ_RESP_HANDLE_MASK) >>
			     IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;

		if (unlikely(cmd_index > hrr_queue->max_cmd_id ||
			     cmd_index < hrr_queue->min_cmd_id)) {
			ipr_isr_eh(ioa_cfg,
				"Invalid response handle from IOA: ",
				cmd_index);
			break;
		}

		ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
		ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);

		ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);

		list_move_tail(&ipr_cmd->queue, doneq);

		if (hrr_queue->hrrq_curr < hrr_queue->hrrq_end) {
			hrr_queue->hrrq_curr++;
		} else {
			hrr_queue->hrrq_curr = hrr_queue->hrrq_start;
			hrr_queue->toggle_bit ^= 1u;
		}
		num_hrrq++;
5564 5565
		if (budget > 0 && num_hrrq >= budget)
			break;
5566
	}
5567

5568 5569
	return num_hrrq;
}
5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598

static int ipr_iopoll(struct blk_iopoll *iop, int budget)
{
	struct ipr_ioa_cfg *ioa_cfg;
	struct ipr_hrr_queue *hrrq;
	struct ipr_cmnd *ipr_cmd, *temp;
	unsigned long hrrq_flags;
	int completed_ops;
	LIST_HEAD(doneq);

	hrrq = container_of(iop, struct ipr_hrr_queue, iopoll);
	ioa_cfg = hrrq->ioa_cfg;

	spin_lock_irqsave(hrrq->lock, hrrq_flags);
	completed_ops = ipr_process_hrrq(hrrq, budget, &doneq);

	if (completed_ops < budget)
		blk_iopoll_complete(iop);
	spin_unlock_irqrestore(hrrq->lock, hrrq_flags);

	list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
		list_del(&ipr_cmd->queue);
		del_timer(&ipr_cmd->timer);
		ipr_cmd->fast_done(ipr_cmd);
	}

	return completed_ops;
}

L
Linus Torvalds 已提交
5599 5600 5601 5602 5603 5604 5605 5606
/**
 * ipr_isr - Interrupt service routine
 * @irq:	irq number
 * @devp:	pointer to ioa config struct
 *
 * Return value:
 * 	IRQ_NONE / IRQ_HANDLED
 **/
5607
static irqreturn_t ipr_isr(int irq, void *devp)
L
Linus Torvalds 已提交
5608
{
5609 5610
	struct ipr_hrr_queue *hrrq = (struct ipr_hrr_queue *)devp;
	struct ipr_ioa_cfg *ioa_cfg = hrrq->ioa_cfg;
5611
	unsigned long hrrq_flags = 0;
5612
	u32 int_reg = 0;
5613
	int num_hrrq = 0;
5614
	int irq_none = 0;
5615
	struct ipr_cmnd *ipr_cmd, *temp;
L
Linus Torvalds 已提交
5616
	irqreturn_t rc = IRQ_NONE;
5617
	LIST_HEAD(doneq);
L
Linus Torvalds 已提交
5618

5619
	spin_lock_irqsave(hrrq->lock, hrrq_flags);
L
Linus Torvalds 已提交
5620
	/* If interrupts are disabled, ignore the interrupt */
5621 5622
	if (!hrrq->allow_interrupts) {
		spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
L
Linus Torvalds 已提交
5623 5624 5625 5626
		return IRQ_NONE;
	}

	while (1) {
5627 5628
		if (ipr_process_hrrq(hrrq, -1, &doneq)) {
			rc =  IRQ_HANDLED;
L
Linus Torvalds 已提交
5629

5630 5631
			if (!ioa_cfg->clear_isr)
				break;
5632

L
Linus Torvalds 已提交
5633
			/* Clear the PCI interrupt */
5634
			num_hrrq = 0;
5635
			do {
5636 5637
				writel(IPR_PCII_HRRQ_UPDATED,
				     ioa_cfg->regs.clr_interrupt_reg32);
5638
				int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5639
			} while (int_reg & IPR_PCII_HRRQ_UPDATED &&
5640
				num_hrrq++ < IPR_MAX_HRRQ_RETRIES);
5641

5642 5643 5644
		} else if (rc == IRQ_NONE && irq_none == 0) {
			int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
			irq_none++;
5645 5646
		} else if (num_hrrq == IPR_MAX_HRRQ_RETRIES &&
			   int_reg & IPR_PCII_HRRQ_UPDATED) {
5647 5648
			ipr_isr_eh(ioa_cfg,
				"Error clearing HRRQ: ", num_hrrq);
5649
			rc = IRQ_HANDLED;
5650
			break;
L
Linus Torvalds 已提交
5651 5652 5653 5654 5655
		} else
			break;
	}

	if (unlikely(rc == IRQ_NONE))
5656
		rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
L
Linus Torvalds 已提交
5657

5658
	spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5659 5660 5661 5662 5663
	list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
		list_del(&ipr_cmd->queue);
		del_timer(&ipr_cmd->timer);
		ipr_cmd->fast_done(ipr_cmd);
	}
5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677
	return rc;
}

/**
 * ipr_isr_mhrrq - Interrupt service routine
 * @irq:	irq number
 * @devp:	pointer to ioa config struct
 *
 * Return value:
 *	IRQ_NONE / IRQ_HANDLED
 **/
static irqreturn_t ipr_isr_mhrrq(int irq, void *devp)
{
	struct ipr_hrr_queue *hrrq = (struct ipr_hrr_queue *)devp;
5678
	struct ipr_ioa_cfg *ioa_cfg = hrrq->ioa_cfg;
5679
	unsigned long hrrq_flags = 0;
5680 5681 5682
	struct ipr_cmnd *ipr_cmd, *temp;
	irqreturn_t rc = IRQ_NONE;
	LIST_HEAD(doneq);
5683

5684
	spin_lock_irqsave(hrrq->lock, hrrq_flags);
5685 5686

	/* If interrupts are disabled, ignore the interrupt */
5687 5688
	if (!hrrq->allow_interrupts) {
		spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5689 5690 5691
		return IRQ_NONE;
	}

5692
	if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
5693 5694 5695 5696 5697 5698 5699 5700 5701 5702
		if ((be32_to_cpu(*hrrq->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
		       hrrq->toggle_bit) {
			if (!blk_iopoll_sched_prep(&hrrq->iopoll))
				blk_iopoll_sched(&hrrq->iopoll);
			spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
			return IRQ_HANDLED;
		}
	} else {
		if ((be32_to_cpu(*hrrq->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
			hrrq->toggle_bit)
5703

5704 5705 5706
			if (ipr_process_hrrq(hrrq, -1, &doneq))
				rc =  IRQ_HANDLED;
	}
5707

5708
	spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
5709 5710 5711 5712 5713 5714

	list_for_each_entry_safe(ipr_cmd, temp, &doneq, queue) {
		list_del(&ipr_cmd->queue);
		del_timer(&ipr_cmd->timer);
		ipr_cmd->fast_done(ipr_cmd);
	}
L
Linus Torvalds 已提交
5715 5716 5717
	return rc;
}

5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742
/**
 * ipr_build_ioadl64 - Build a scatter/gather list and map the buffer
 * @ioa_cfg:	ioa config struct
 * @ipr_cmd:	ipr command struct
 *
 * Return value:
 * 	0 on success / -1 on failure
 **/
static int ipr_build_ioadl64(struct ipr_ioa_cfg *ioa_cfg,
			     struct ipr_cmnd *ipr_cmd)
{
	int i, nseg;
	struct scatterlist *sg;
	u32 length;
	u32 ioadl_flags = 0;
	struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
	struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;

	length = scsi_bufflen(scsi_cmd);
	if (!length)
		return 0;

	nseg = scsi_dma_map(scsi_cmd);
	if (nseg < 0) {
5743
		if (printk_ratelimit())
A
Anton Blanchard 已提交
5744
			dev_err(&ioa_cfg->pdev->dev, "scsi_dma_map failed!\n");
5745 5746 5747 5748 5749
		return -1;
	}

	ipr_cmd->dma_use_sg = nseg;

5750
	ioarcb->data_transfer_length = cpu_to_be32(length);
5751 5752
	ioarcb->ioadl_len =
		cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
5753

5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769
	if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
		ioadl_flags = IPR_IOADL_FLAGS_WRITE;
		ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
	} else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE)
		ioadl_flags = IPR_IOADL_FLAGS_READ;

	scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
		ioadl64[i].flags = cpu_to_be32(ioadl_flags);
		ioadl64[i].data_len = cpu_to_be32(sg_dma_len(sg));
		ioadl64[i].address = cpu_to_be64(sg_dma_address(sg));
	}

	ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
	return 0;
}

L
Linus Torvalds 已提交
5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780
/**
 * ipr_build_ioadl - Build a scatter/gather list and map the buffer
 * @ioa_cfg:	ioa config struct
 * @ipr_cmd:	ipr command struct
 *
 * Return value:
 * 	0 on success / -1 on failure
 **/
static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
			   struct ipr_cmnd *ipr_cmd)
{
5781 5782
	int i, nseg;
	struct scatterlist *sg;
L
Linus Torvalds 已提交
5783 5784 5785 5786
	u32 length;
	u32 ioadl_flags = 0;
	struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5787
	struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
L
Linus Torvalds 已提交
5788

5789 5790
	length = scsi_bufflen(scsi_cmd);
	if (!length)
L
Linus Torvalds 已提交
5791 5792
		return 0;

5793 5794
	nseg = scsi_dma_map(scsi_cmd);
	if (nseg < 0) {
A
Anton Blanchard 已提交
5795
		dev_err(&ioa_cfg->pdev->dev, "scsi_dma_map failed!\n");
5796 5797
		return -1;
	}
B
Brian King 已提交
5798

5799 5800 5801 5802 5803
	ipr_cmd->dma_use_sg = nseg;

	if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
		ioadl_flags = IPR_IOADL_FLAGS_WRITE;
		ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5804 5805
		ioarcb->data_transfer_length = cpu_to_be32(length);
		ioarcb->ioadl_len =
5806 5807 5808 5809 5810 5811 5812
			cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
	} else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
		ioadl_flags = IPR_IOADL_FLAGS_READ;
		ioarcb->read_data_transfer_length = cpu_to_be32(length);
		ioarcb->read_ioadl_len =
			cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
	}
L
Linus Torvalds 已提交
5813

5814 5815 5816 5817
	if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->u.add_data.u.ioadl)) {
		ioadl = ioarcb->u.add_data.u.ioadl;
		ioarcb->write_ioadl_addr = cpu_to_be32((ipr_cmd->dma_addr) +
				    offsetof(struct ipr_ioarcb, u.add_data));
5818 5819
		ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
	}
L
Linus Torvalds 已提交
5820

5821 5822 5823 5824
	scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
		ioadl[i].flags_and_data_len =
			cpu_to_be32(ioadl_flags | sg_dma_len(sg));
		ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
L
Linus Torvalds 已提交
5825 5826
	}

5827 5828
	ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
	return 0;
L
Linus Torvalds 已提交
5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844
}

/**
 * ipr_erp_done - Process completion of ERP for a device
 * @ipr_cmd:		ipr command struct
 *
 * This function copies the sense buffer into the scsi_cmd
 * struct and pushes the scsi_done function.
 *
 * Return value:
 * 	nothing
 **/
static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
{
	struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
	struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5845
	u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
L
Linus Torvalds 已提交
5846 5847 5848

	if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
		scsi_cmd->result |= (DID_ERROR << 16);
5849 5850
		scmd_printk(KERN_ERR, scsi_cmd,
			    "Request Sense failed with IOASC: 0x%08X\n", ioasc);
L
Linus Torvalds 已提交
5851 5852 5853 5854 5855 5856
	} else {
		memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
		       SCSI_SENSE_BUFFERSIZE);
	}

	if (res) {
5857 5858
		if (!ipr_is_naca_model(res))
			res->needs_sync_complete = 1;
L
Linus Torvalds 已提交
5859 5860
		res->in_erp = 0;
	}
5861
	scsi_dma_unmap(ipr_cmd->scsi_cmd);
5862
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
L
Linus Torvalds 已提交
5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874
	scsi_cmd->scsi_done(scsi_cmd);
}

/**
 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
 * @ipr_cmd:	ipr command struct
 *
 * Return value:
 * 	none
 **/
static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
{
B
Brian King 已提交
5875
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5876
	struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5877
	dma_addr_t dma_addr = ipr_cmd->dma_addr;
L
Linus Torvalds 已提交
5878 5879

	memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
5880
	ioarcb->data_transfer_length = 0;
L
Linus Torvalds 已提交
5881
	ioarcb->read_data_transfer_length = 0;
5882
	ioarcb->ioadl_len = 0;
L
Linus Torvalds 已提交
5883
	ioarcb->read_ioadl_len = 0;
5884 5885
	ioasa->hdr.ioasc = 0;
	ioasa->hdr.residual_data_len = 0;
5886 5887 5888 5889 5890 5891 5892 5893 5894

	if (ipr_cmd->ioa_cfg->sis64)
		ioarcb->u.sis64_addr_data.data_ioadl_addr =
			cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
	else {
		ioarcb->write_ioadl_addr =
			cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
		ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
	}
L
Linus Torvalds 已提交
5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909
}

/**
 * ipr_erp_request_sense - Send request sense to a device
 * @ipr_cmd:	ipr command struct
 *
 * This function sends a request sense to a device as a result
 * of a check condition.
 *
 * Return value:
 * 	nothing
 **/
static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5910
	u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
L
Linus Torvalds 已提交
5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925

	if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
		ipr_erp_done(ipr_cmd);
		return;
	}

	ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);

	cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
	cmd_pkt->cdb[0] = REQUEST_SENSE;
	cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
	cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
	cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
	cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);

5926 5927
	ipr_init_ioadl(ipr_cmd, ipr_cmd->sense_buffer_dma,
		       SCSI_SENSE_BUFFERSIZE, IPR_IOADL_FLAGS_READ_LAST);
L
Linus Torvalds 已提交
5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954

	ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
		   IPR_REQUEST_SENSE_TIMEOUT * 2);
}

/**
 * ipr_erp_cancel_all - Send cancel all to a device
 * @ipr_cmd:	ipr command struct
 *
 * This function sends a cancel all to a device to clear the
 * queue. If we are running TCQ on the device, QERR is set to 1,
 * which means all outstanding ops have been dropped on the floor.
 * Cancel all will return them to us.
 *
 * Return value:
 * 	nothing
 **/
static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
{
	struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
	struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
	struct ipr_cmd_pkt *cmd_pkt;

	res->in_erp = 1;

	ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);

C
Christoph Hellwig 已提交
5955
	if (!scsi_cmd->device->simple_tags) {
L
Linus Torvalds 已提交
5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971
		ipr_erp_request_sense(ipr_cmd);
		return;
	}

	cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
	cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
	cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;

	ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
		   IPR_CANCEL_ALL_TIMEOUT);
}

/**
 * ipr_dump_ioasa - Dump contents of IOASA
 * @ioa_cfg:	ioa config struct
 * @ipr_cmd:	ipr command struct
5972
 * @res:		resource entry struct
L
Linus Torvalds 已提交
5973 5974 5975 5976 5977 5978 5979 5980 5981
 *
 * This function is invoked by the interrupt handler when ops
 * fail. It will log the IOASA if appropriate. Only called
 * for GPDD ops.
 *
 * Return value:
 * 	none
 **/
static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
5982
			   struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
L
Linus Torvalds 已提交
5983 5984 5985
{
	int i;
	u16 data_len;
5986
	u32 ioasc, fd_ioasc;
5987
	struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
L
Linus Torvalds 已提交
5988 5989 5990
	__be32 *ioasa_data = (__be32 *)ioasa;
	int error_index;

5991 5992
	ioasc = be32_to_cpu(ioasa->hdr.ioasc) & IPR_IOASC_IOASC_MASK;
	fd_ioasc = be32_to_cpu(ioasa->hdr.fd_ioasc) & IPR_IOASC_IOASC_MASK;
L
Linus Torvalds 已提交
5993 5994 5995 5996 5997 5998 5999

	if (0 == ioasc)
		return;

	if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
		return;

6000 6001 6002 6003
	if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
		error_index = ipr_get_error(fd_ioasc);
	else
		error_index = ipr_get_error(ioasc);
L
Linus Torvalds 已提交
6004 6005 6006

	if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
		/* Don't log an error if the IOA already logged one */
6007
		if (ioasa->hdr.ilid != 0)
L
Linus Torvalds 已提交
6008 6009
			return;

6010 6011 6012
		if (!ipr_is_gscsi(res))
			return;

L
Linus Torvalds 已提交
6013 6014 6015 6016
		if (ipr_error_table[error_index].log_ioasa == 0)
			return;
	}

6017
	ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
L
Linus Torvalds 已提交
6018

6019 6020 6021 6022
	data_len = be16_to_cpu(ioasa->hdr.ret_stat_len);
	if (ioa_cfg->sis64 && sizeof(struct ipr_ioasa64) < data_len)
		data_len = sizeof(struct ipr_ioasa64);
	else if (!ioa_cfg->sis64 && sizeof(struct ipr_ioasa) < data_len)
L
Linus Torvalds 已提交
6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048
		data_len = sizeof(struct ipr_ioasa);

	ipr_err("IOASA Dump:\n");

	for (i = 0; i < data_len / 4; i += 4) {
		ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
			be32_to_cpu(ioasa_data[i]),
			be32_to_cpu(ioasa_data[i+1]),
			be32_to_cpu(ioasa_data[i+2]),
			be32_to_cpu(ioasa_data[i+3]));
	}
}

/**
 * ipr_gen_sense - Generate SCSI sense data from an IOASA
 * @ioasa:		IOASA
 * @sense_buf:	sense data buffer
 *
 * Return value:
 * 	none
 **/
static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
{
	u32 failing_lba;
	u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
	struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
6049 6050
	struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
	u32 ioasc = be32_to_cpu(ioasa->hdr.ioasc);
L
Linus Torvalds 已提交
6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092

	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);

	if (ioasc >= IPR_FIRST_DRIVER_IOASC)
		return;

	ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;

	if (ipr_is_vset_device(res) &&
	    ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
	    ioasa->u.vset.failing_lba_hi != 0) {
		sense_buf[0] = 0x72;
		sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
		sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
		sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);

		sense_buf[7] = 12;
		sense_buf[8] = 0;
		sense_buf[9] = 0x0A;
		sense_buf[10] = 0x80;

		failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);

		sense_buf[12] = (failing_lba & 0xff000000) >> 24;
		sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
		sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
		sense_buf[15] = failing_lba & 0x000000ff;

		failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);

		sense_buf[16] = (failing_lba & 0xff000000) >> 24;
		sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
		sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
		sense_buf[19] = failing_lba & 0x000000ff;
	} else {
		sense_buf[0] = 0x70;
		sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
		sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
		sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);

		/* Illegal request */
		if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
6093
		    (be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
L
Linus Torvalds 已提交
6094 6095 6096 6097 6098 6099 6100 6101 6102 6103
			sense_buf[7] = 10;	/* additional length */

			/* IOARCB was in error */
			if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
				sense_buf[15] = 0xC0;
			else	/* Parameter data was invalid */
				sense_buf[15] = 0x80;

			sense_buf[16] =
			    ((IPR_FIELD_POINTER_MASK &
6104
			      be32_to_cpu(ioasa->hdr.ioasc_specific)) >> 8) & 0xff;
L
Linus Torvalds 已提交
6105 6106
			sense_buf[17] =
			    (IPR_FIELD_POINTER_MASK &
6107
			     be32_to_cpu(ioasa->hdr.ioasc_specific)) & 0xff;
L
Linus Torvalds 已提交
6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126
		} else {
			if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
				if (ipr_is_vset_device(res))
					failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
				else
					failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);

				sense_buf[0] |= 0x80;	/* Or in the Valid bit */
				sense_buf[3] = (failing_lba & 0xff000000) >> 24;
				sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
				sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
				sense_buf[6] = failing_lba & 0x000000ff;
			}

			sense_buf[7] = 6;	/* additional length */
		}
	}
}

6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138
/**
 * ipr_get_autosense - Copy autosense data to sense buffer
 * @ipr_cmd:	ipr command struct
 *
 * This function copies the autosense buffer to the buffer
 * in the scsi_cmd, if there is autosense available.
 *
 * Return value:
 *	1 if autosense was available / 0 if not
 **/
static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
{
6139 6140
	struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
	struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
6141

6142
	if ((be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
6143 6144
		return 0;

6145 6146 6147 6148 6149 6150 6151 6152
	if (ipr_cmd->ioa_cfg->sis64)
		memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa64->auto_sense.data,
		       min_t(u16, be16_to_cpu(ioasa64->auto_sense.auto_sense_len),
			   SCSI_SENSE_BUFFERSIZE));
	else
		memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
		       min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
			   SCSI_SENSE_BUFFERSIZE));
6153 6154 6155
	return 1;
}

L
Linus Torvalds 已提交
6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171
/**
 * ipr_erp_start - Process an error response for a SCSI op
 * @ioa_cfg:	ioa config struct
 * @ipr_cmd:	ipr command struct
 *
 * This function determines whether or not to initiate ERP
 * on the affected device.
 *
 * Return value:
 * 	nothing
 **/
static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
			      struct ipr_cmnd *ipr_cmd)
{
	struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
	struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
6172
	u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6173
	u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
L
Linus Torvalds 已提交
6174 6175 6176 6177 6178 6179

	if (!res) {
		ipr_scsi_eh_done(ipr_cmd);
		return;
	}

6180
	if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
L
Linus Torvalds 已提交
6181 6182
		ipr_gen_sense(ipr_cmd);

6183 6184
	ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);

6185
	switch (masked_ioasc) {
L
Linus Torvalds 已提交
6186
	case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
6187 6188 6189 6190
		if (ipr_is_naca_model(res))
			scsi_cmd->result |= (DID_ABORT << 16);
		else
			scsi_cmd->result |= (DID_IMM_RETRY << 16);
L
Linus Torvalds 已提交
6191 6192
		break;
	case IPR_IOASC_IR_RESOURCE_HANDLE:
6193
	case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
L
Linus Torvalds 已提交
6194 6195 6196 6197
		scsi_cmd->result |= (DID_NO_CONNECT << 16);
		break;
	case IPR_IOASC_HW_SEL_TIMEOUT:
		scsi_cmd->result |= (DID_NO_CONNECT << 16);
6198 6199
		if (!ipr_is_naca_model(res))
			res->needs_sync_complete = 1;
L
Linus Torvalds 已提交
6200 6201 6202 6203 6204 6205 6206
		break;
	case IPR_IOASC_SYNC_REQUIRED:
		if (!res->in_erp)
			res->needs_sync_complete = 1;
		scsi_cmd->result |= (DID_IMM_RETRY << 16);
		break;
	case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
6207
	case IPR_IOASA_IR_DUAL_IOA_DISABLED:
L
Linus Torvalds 已提交
6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218
		scsi_cmd->result |= (DID_PASSTHROUGH << 16);
		break;
	case IPR_IOASC_BUS_WAS_RESET:
	case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
		/*
		 * Report the bus reset and ask for a retry. The device
		 * will give CC/UA the next command.
		 */
		if (!res->resetting_device)
			scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
		scsi_cmd->result |= (DID_ERROR << 16);
6219 6220
		if (!ipr_is_naca_model(res))
			res->needs_sync_complete = 1;
L
Linus Torvalds 已提交
6221 6222 6223 6224
		break;
	case IPR_IOASC_HW_DEV_BUS_STATUS:
		scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
		if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
6225 6226 6227 6228 6229 6230
			if (!ipr_get_autosense(ipr_cmd)) {
				if (!ipr_is_naca_model(res)) {
					ipr_erp_cancel_all(ipr_cmd);
					return;
				}
			}
L
Linus Torvalds 已提交
6231
		}
6232 6233
		if (!ipr_is_naca_model(res))
			res->needs_sync_complete = 1;
L
Linus Torvalds 已提交
6234 6235 6236
		break;
	case IPR_IOASC_NR_INIT_CMD_REQUIRED:
		break;
6237 6238 6239 6240 6241 6242 6243
	case IPR_IOASC_IR_NON_OPTIMIZED:
		if (res->raw_mode) {
			res->raw_mode = 0;
			scsi_cmd->result |= (DID_IMM_RETRY << 16);
		} else
			scsi_cmd->result |= (DID_ERROR << 16);
		break;
L
Linus Torvalds 已提交
6244
	default:
6245 6246
		if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
			scsi_cmd->result |= (DID_ERROR << 16);
6247
		if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
L
Linus Torvalds 已提交
6248 6249 6250 6251
			res->needs_sync_complete = 1;
		break;
	}

6252
	scsi_dma_unmap(ipr_cmd->scsi_cmd);
6253
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
L
Linus Torvalds 已提交
6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270
	scsi_cmd->scsi_done(scsi_cmd);
}

/**
 * ipr_scsi_done - mid-layer done function
 * @ipr_cmd:	ipr command struct
 *
 * This function is invoked by the interrupt handler for
 * ops generated by the SCSI mid-layer
 *
 * Return value:
 * 	none
 **/
static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
6271
	u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6272
	unsigned long lock_flags;
L
Linus Torvalds 已提交
6273

6274
	scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->s.ioasa.hdr.residual_data_len));
L
Linus Torvalds 已提交
6275 6276

	if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
6277 6278
		scsi_dma_unmap(scsi_cmd);

6279
		spin_lock_irqsave(ipr_cmd->hrrq->lock, lock_flags);
6280
		list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
L
Linus Torvalds 已提交
6281
		scsi_cmd->scsi_done(scsi_cmd);
6282
		spin_unlock_irqrestore(ipr_cmd->hrrq->lock, lock_flags);
6283
	} else {
6284 6285
		spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
		spin_lock(&ipr_cmd->hrrq->_lock);
L
Linus Torvalds 已提交
6286
		ipr_erp_start(ioa_cfg, ipr_cmd);
6287 6288
		spin_unlock(&ipr_cmd->hrrq->_lock);
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6289
	}
L
Linus Torvalds 已提交
6290 6291 6292 6293
}

/**
 * ipr_queuecommand - Queue a mid-layer request
6294
 * @shost:		scsi host struct
L
Linus Torvalds 已提交
6295 6296 6297 6298 6299 6300 6301 6302 6303
 * @scsi_cmd:	scsi command struct
 *
 * This function queues a request generated by the mid-layer.
 *
 * Return value:
 *	0 on success
 *	SCSI_MLQUEUE_DEVICE_BUSY if device is busy
 *	SCSI_MLQUEUE_HOST_BUSY if host is busy
 **/
6304 6305
static int ipr_queuecommand(struct Scsi_Host *shost,
			    struct scsi_cmnd *scsi_cmd)
L
Linus Torvalds 已提交
6306 6307 6308 6309 6310
{
	struct ipr_ioa_cfg *ioa_cfg;
	struct ipr_resource_entry *res;
	struct ipr_ioarcb *ioarcb;
	struct ipr_cmnd *ipr_cmd;
6311
	unsigned long hrrq_flags, lock_flags;
6312
	int rc;
6313 6314
	struct ipr_hrr_queue *hrrq;
	int hrrq_id;
L
Linus Torvalds 已提交
6315

6316 6317
	ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;

L
Linus Torvalds 已提交
6318
	scsi_cmd->result = (DID_OK << 16);
6319
	res = scsi_cmd->device->hostdata;
6320 6321 6322 6323 6324 6325 6326 6327

	if (ipr_is_gata(res) && res->sata_port) {
		spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
		rc = ata_sas_queuecmd(scsi_cmd, res->sata_port->ap);
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		return rc;
	}

6328 6329
	hrrq_id = ipr_get_hrrq_index(ioa_cfg);
	hrrq = &ioa_cfg->hrrq[hrrq_id];
L
Linus Torvalds 已提交
6330

6331
	spin_lock_irqsave(hrrq->lock, hrrq_flags);
L
Linus Torvalds 已提交
6332 6333 6334 6335 6336
	/*
	 * We are currently blocking all devices due to a host reset
	 * We have told the host to stop giving us new requests, but
	 * ERP ops don't count. FIXME
	 */
6337
	if (unlikely(!hrrq->allow_cmds && !hrrq->ioa_is_dead && !hrrq->removing_ioa)) {
6338
		spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
L
Linus Torvalds 已提交
6339
		return SCSI_MLQUEUE_HOST_BUSY;
6340
	}
L
Linus Torvalds 已提交
6341 6342 6343 6344 6345

	/*
	 * FIXME - Create scsi_set_host_offline interface
	 *  and the ioa_is_dead check can be removed
	 */
6346
	if (unlikely(hrrq->ioa_is_dead || hrrq->removing_ioa || !res)) {
6347
		spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6348
		goto err_nodev;
L
Linus Torvalds 已提交
6349 6350
	}

6351 6352
	ipr_cmd = __ipr_get_free_ipr_cmnd(hrrq);
	if (ipr_cmd == NULL) {
6353
		spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6354 6355
		return SCSI_MLQUEUE_HOST_BUSY;
	}
6356
	spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6357

6358
	ipr_init_ipr_cmnd(ipr_cmd, ipr_scsi_done);
L
Linus Torvalds 已提交
6359 6360 6361 6362
	ioarcb = &ipr_cmd->ioarcb;

	memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
	ipr_cmd->scsi_cmd = scsi_cmd;
6363
	ipr_cmd->done = ipr_scsi_eh_done;
L
Linus Torvalds 已提交
6364 6365 6366 6367 6368 6369

	if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
		if (scsi_cmd->underflow == 0)
			ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;

		ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
6370 6371
		if (ipr_is_gscsi(res) && res->reset_occurred) {
			res->reset_occurred = 0;
6372
			ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
6373
		}
L
Linus Torvalds 已提交
6374
		ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
6375 6376 6377 6378
		if (scsi_cmd->flags & SCMD_TAGGED)
			ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_SIMPLE_TASK;
		else
			ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_UNTAGGED_TASK;
L
Linus Torvalds 已提交
6379 6380 6381
	}

	if (scsi_cmd->cmnd[0] >= 0xC0 &&
6382
	    (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE)) {
L
Linus Torvalds 已提交
6383
		ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6384
	}
6385 6386
	if (res->raw_mode && ipr_is_af_dasd_device(res))
		ioarcb->cmd_pkt.request_type = IPR_RQTYPE_PIPE;
L
Linus Torvalds 已提交
6387

6388 6389 6390 6391
	if (ioa_cfg->sis64)
		rc = ipr_build_ioadl64(ioa_cfg, ipr_cmd);
	else
		rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
L
Linus Torvalds 已提交
6392

6393 6394
	spin_lock_irqsave(hrrq->lock, hrrq_flags);
	if (unlikely(rc || (!hrrq->allow_cmds && !hrrq->ioa_is_dead))) {
6395
		list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_free_q);
6396
		spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6397 6398
		if (!rc)
			scsi_dma_unmap(scsi_cmd);
6399
		return SCSI_MLQUEUE_HOST_BUSY;
L
Linus Torvalds 已提交
6400 6401
	}

6402
	if (unlikely(hrrq->ioa_is_dead)) {
6403
		list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_free_q);
6404
		spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6405 6406 6407 6408 6409 6410 6411 6412 6413
		scsi_dma_unmap(scsi_cmd);
		goto err_nodev;
	}

	ioarcb->res_handle = res->res_handle;
	if (res->needs_sync_complete) {
		ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
		res->needs_sync_complete = 0;
	}
6414
	list_add_tail(&ipr_cmd->queue, &hrrq->hrrq_pending_q);
6415
	ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
6416
	ipr_send_command(ipr_cmd);
6417
	spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
L
Linus Torvalds 已提交
6418 6419
	return 0;

6420
err_nodev:
6421
	spin_lock_irqsave(hrrq->lock, hrrq_flags);
6422 6423 6424
	memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
	scsi_cmd->result = (DID_NO_CONNECT << 16);
	scsi_cmd->scsi_done(scsi_cmd);
6425
	spin_unlock_irqrestore(hrrq->lock, hrrq_flags);
6426 6427
	return 0;
}
J
Jeff Garzik 已提交
6428

6429 6430 6431 6432 6433 6434 6435 6436 6437
/**
 * ipr_ioctl - IOCTL handler
 * @sdev:	scsi device struct
 * @cmd:	IOCTL cmd
 * @arg:	IOCTL arg
 *
 * Return value:
 * 	0 on success / other on failure
 **/
A
Adrian Bunk 已提交
6438
static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
6439 6440 6441 6442
{
	struct ipr_resource_entry *res;

	res = (struct ipr_resource_entry *)sdev->hostdata;
6443 6444 6445
	if (res && ipr_is_gata(res)) {
		if (cmd == HDIO_GET_IDENTITY)
			return -ENOTTY;
6446
		return ata_sas_scsi_ioctl(res->sata_port->ap, sdev, cmd, arg);
6447
	}
6448 6449 6450 6451

	return -EINVAL;
}

L
Linus Torvalds 已提交
6452 6453 6454 6455 6456 6457 6458
/**
 * ipr_info - Get information about the card/driver
 * @scsi_host:	scsi host struct
 *
 * Return value:
 * 	pointer to buffer with description string
 **/
6459
static const char *ipr_ioa_info(struct Scsi_Host *host)
L
Linus Torvalds 已提交
6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477
{
	static char buffer[512];
	struct ipr_ioa_cfg *ioa_cfg;
	unsigned long lock_flags = 0;

	ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;

	spin_lock_irqsave(host->host_lock, lock_flags);
	sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
	spin_unlock_irqrestore(host->host_lock, lock_flags);

	return buffer;
}

static struct scsi_host_template driver_template = {
	.module = THIS_MODULE,
	.name = "IPR",
	.info = ipr_ioa_info,
6478
	.ioctl = ipr_ioctl,
L
Linus Torvalds 已提交
6479 6480 6481 6482 6483 6484 6485
	.queuecommand = ipr_queuecommand,
	.eh_abort_handler = ipr_eh_abort,
	.eh_device_reset_handler = ipr_eh_dev_reset,
	.eh_host_reset_handler = ipr_eh_host_reset,
	.slave_alloc = ipr_slave_alloc,
	.slave_configure = ipr_slave_configure,
	.slave_destroy = ipr_slave_destroy,
6486
	.scan_finished = ipr_scan_finished,
6487 6488
	.target_alloc = ipr_target_alloc,
	.target_destroy = ipr_target_destroy,
L
Linus Torvalds 已提交
6489 6490 6491 6492 6493 6494 6495 6496 6497 6498
	.change_queue_depth = ipr_change_queue_depth,
	.bios_param = ipr_biosparam,
	.can_queue = IPR_MAX_COMMANDS,
	.this_id = -1,
	.sg_tablesize = IPR_MAX_SGLIST,
	.max_sectors = IPR_IOA_MAX_SECTORS,
	.cmd_per_lun = IPR_MAX_CMD_PER_LUN,
	.use_clustering = ENABLE_CLUSTERING,
	.shost_attrs = ipr_ioa_attrs,
	.sdev_attrs = ipr_dev_attrs,
6499
	.proc_name = IPR_NAME,
6500
	.use_blk_tags = 1,
L
Linus Torvalds 已提交
6501 6502
};

6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517
/**
 * ipr_ata_phy_reset - libata phy_reset handler
 * @ap:		ata port to reset
 *
 **/
static void ipr_ata_phy_reset(struct ata_port *ap)
{
	unsigned long flags;
	struct ipr_sata_port *sata_port = ap->private_data;
	struct ipr_resource_entry *res = sata_port->res;
	struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
	int rc;

	ENTER;
	spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6518
	while (ioa_cfg->in_reset_reload) {
6519 6520 6521 6522 6523
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
		wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
		spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
	}

6524
	if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds)
6525 6526 6527 6528 6529
		goto out_unlock;

	rc = ipr_device_reset(ioa_cfg, res);

	if (rc) {
T
Tejun Heo 已提交
6530
		ap->link.device[0].class = ATA_DEV_NONE;
6531 6532 6533
		goto out_unlock;
	}

6534 6535
	ap->link.device[0].class = res->ata_class;
	if (ap->link.device[0].class == ATA_DEV_UNKNOWN)
T
Tejun Heo 已提交
6536
		ap->link.device[0].class = ATA_DEV_NONE;
6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554

out_unlock:
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
	LEAVE;
}

/**
 * ipr_ata_post_internal - Cleanup after an internal command
 * @qc:	ATA queued command
 *
 * Return value:
 * 	none
 **/
static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
{
	struct ipr_sata_port *sata_port = qc->ap->private_data;
	struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
	struct ipr_cmnd *ipr_cmd;
6555
	struct ipr_hrr_queue *hrrq;
6556 6557 6558
	unsigned long flags;

	spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6559
	while (ioa_cfg->in_reset_reload) {
6560 6561 6562 6563 6564
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
		wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
		spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
	}

6565
	for_each_hrrq(hrrq, ioa_cfg) {
6566
		spin_lock(&hrrq->_lock);
6567 6568 6569 6570 6571
		list_for_each_entry(ipr_cmd, &hrrq->hrrq_pending_q, queue) {
			if (ipr_cmd->qc == qc) {
				ipr_device_reset(ioa_cfg, sata_port->res);
				break;
			}
6572
		}
6573
		spin_unlock(&hrrq->_lock);
6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619
	}
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
}

/**
 * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
 * @regs:	destination
 * @tf:	source ATA taskfile
 *
 * Return value:
 * 	none
 **/
static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
			     struct ata_taskfile *tf)
{
	regs->feature = tf->feature;
	regs->nsect = tf->nsect;
	regs->lbal = tf->lbal;
	regs->lbam = tf->lbam;
	regs->lbah = tf->lbah;
	regs->device = tf->device;
	regs->command = tf->command;
	regs->hob_feature = tf->hob_feature;
	regs->hob_nsect = tf->hob_nsect;
	regs->hob_lbal = tf->hob_lbal;
	regs->hob_lbam = tf->hob_lbam;
	regs->hob_lbah = tf->hob_lbah;
	regs->ctl = tf->ctl;
}

/**
 * ipr_sata_done - done function for SATA commands
 * @ipr_cmd:	ipr command struct
 *
 * This function is invoked by the interrupt handler for
 * ops generated by the SCSI mid-layer to SATA devices
 *
 * Return value:
 * 	none
 **/
static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ata_queued_cmd *qc = ipr_cmd->qc;
	struct ipr_sata_port *sata_port = qc->ap->private_data;
	struct ipr_resource_entry *res = sata_port->res;
6620
	u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
6621

6622
	spin_lock(&ipr_cmd->hrrq->_lock);
6623 6624 6625 6626 6627 6628
	if (ipr_cmd->ioa_cfg->sis64)
		memcpy(&sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
		       sizeof(struct ipr_ioasa_gata));
	else
		memcpy(&sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
		       sizeof(struct ipr_ioasa_gata));
6629 6630
	ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);

6631
	if (be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
6632
		scsi_report_device_reset(ioa_cfg->host, res->bus, res->target);
6633 6634

	if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
6635
		qc->err_mask |= __ac_err_mask(sata_port->ioasa.status);
6636
	else
6637
		qc->err_mask |= ac_err_mask(sata_port->ioasa.status);
6638
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
6639
	spin_unlock(&ipr_cmd->hrrq->_lock);
6640 6641 6642
	ata_qc_complete(qc);
}

6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653
/**
 * ipr_build_ata_ioadl64 - Build an ATA scatter/gather list
 * @ipr_cmd:	ipr command struct
 * @qc:		ATA queued command
 *
 **/
static void ipr_build_ata_ioadl64(struct ipr_cmnd *ipr_cmd,
				  struct ata_queued_cmd *qc)
{
	u32 ioadl_flags = 0;
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6654
	struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ata_ioadl.ioadl64;
6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673
	struct ipr_ioadl64_desc *last_ioadl64 = NULL;
	int len = qc->nbytes;
	struct scatterlist *sg;
	unsigned int si;
	dma_addr_t dma_addr = ipr_cmd->dma_addr;

	if (len == 0)
		return;

	if (qc->dma_dir == DMA_TO_DEVICE) {
		ioadl_flags = IPR_IOADL_FLAGS_WRITE;
		ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
	} else if (qc->dma_dir == DMA_FROM_DEVICE)
		ioadl_flags = IPR_IOADL_FLAGS_READ;

	ioarcb->data_transfer_length = cpu_to_be32(len);
	ioarcb->ioadl_len =
		cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
	ioarcb->u.sis64_addr_data.data_ioadl_addr =
6674
		cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ata_ioadl.ioadl64));
6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688

	for_each_sg(qc->sg, sg, qc->n_elem, si) {
		ioadl64->flags = cpu_to_be32(ioadl_flags);
		ioadl64->data_len = cpu_to_be32(sg_dma_len(sg));
		ioadl64->address = cpu_to_be64(sg_dma_address(sg));

		last_ioadl64 = ioadl64;
		ioadl64++;
	}

	if (likely(last_ioadl64))
		last_ioadl64->flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
}

6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699
/**
 * ipr_build_ata_ioadl - Build an ATA scatter/gather list
 * @ipr_cmd:	ipr command struct
 * @qc:		ATA queued command
 *
 **/
static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
				struct ata_queued_cmd *qc)
{
	u32 ioadl_flags = 0;
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6700
	struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
J
Jeff Garzik 已提交
6701
	struct ipr_ioadl_desc *last_ioadl = NULL;
6702
	int len = qc->nbytes;
6703
	struct scatterlist *sg;
T
Tejun Heo 已提交
6704
	unsigned int si;
6705 6706 6707 6708 6709 6710 6711

	if (len == 0)
		return;

	if (qc->dma_dir == DMA_TO_DEVICE) {
		ioadl_flags = IPR_IOADL_FLAGS_WRITE;
		ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6712 6713
		ioarcb->data_transfer_length = cpu_to_be32(len);
		ioarcb->ioadl_len =
6714 6715 6716 6717 6718 6719 6720 6721
			cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
	} else if (qc->dma_dir == DMA_FROM_DEVICE) {
		ioadl_flags = IPR_IOADL_FLAGS_READ;
		ioarcb->read_data_transfer_length = cpu_to_be32(len);
		ioarcb->read_ioadl_len =
			cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
	}

T
Tejun Heo 已提交
6722
	for_each_sg(qc->sg, sg, qc->n_elem, si) {
6723 6724
		ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
		ioadl->address = cpu_to_be32(sg_dma_address(sg));
J
Jeff Garzik 已提交
6725 6726 6727

		last_ioadl = ioadl;
		ioadl++;
6728
	}
J
Jeff Garzik 已提交
6729 6730 6731

	if (likely(last_ioadl))
		last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
6732 6733
}

6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775
/**
 * ipr_qc_defer - Get a free ipr_cmd
 * @qc:	queued command
 *
 * Return value:
 *	0 if success
 **/
static int ipr_qc_defer(struct ata_queued_cmd *qc)
{
	struct ata_port *ap = qc->ap;
	struct ipr_sata_port *sata_port = ap->private_data;
	struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
	struct ipr_cmnd *ipr_cmd;
	struct ipr_hrr_queue *hrrq;
	int hrrq_id;

	hrrq_id = ipr_get_hrrq_index(ioa_cfg);
	hrrq = &ioa_cfg->hrrq[hrrq_id];

	qc->lldd_task = NULL;
	spin_lock(&hrrq->_lock);
	if (unlikely(hrrq->ioa_is_dead)) {
		spin_unlock(&hrrq->_lock);
		return 0;
	}

	if (unlikely(!hrrq->allow_cmds)) {
		spin_unlock(&hrrq->_lock);
		return ATA_DEFER_LINK;
	}

	ipr_cmd = __ipr_get_free_ipr_cmnd(hrrq);
	if (ipr_cmd == NULL) {
		spin_unlock(&hrrq->_lock);
		return ATA_DEFER_LINK;
	}

	qc->lldd_task = ipr_cmd;
	spin_unlock(&hrrq->_lock);
	return 0;
}

6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792
/**
 * ipr_qc_issue - Issue a SATA qc to a device
 * @qc:	queued command
 *
 * Return value:
 * 	0 if success
 **/
static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
{
	struct ata_port *ap = qc->ap;
	struct ipr_sata_port *sata_port = ap->private_data;
	struct ipr_resource_entry *res = sata_port->res;
	struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
	struct ipr_cmnd *ipr_cmd;
	struct ipr_ioarcb *ioarcb;
	struct ipr_ioarcb_ata_regs *regs;

6793 6794 6795 6796 6797
	if (qc->lldd_task == NULL)
		ipr_qc_defer(qc);

	ipr_cmd = qc->lldd_task;
	if (ipr_cmd == NULL)
6798
		return AC_ERR_SYSTEM;
6799

6800 6801 6802 6803 6804 6805 6806 6807 6808
	qc->lldd_task = NULL;
	spin_lock(&ipr_cmd->hrrq->_lock);
	if (unlikely(!ipr_cmd->hrrq->allow_cmds ||
			ipr_cmd->hrrq->ioa_is_dead)) {
		list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
		spin_unlock(&ipr_cmd->hrrq->_lock);
		return AC_ERR_SYSTEM;
	}

6809
	ipr_init_ipr_cmnd(ipr_cmd, ipr_lock_and_done);
6810 6811
	ioarcb = &ipr_cmd->ioarcb;

6812 6813 6814 6815 6816 6817 6818 6819
	if (ioa_cfg->sis64) {
		regs = &ipr_cmd->i.ata_ioadl.regs;
		ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
	} else
		regs = &ioarcb->u.add_data.u.regs;

	memset(regs, 0, sizeof(*regs));
	ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(*regs));
6820

6821
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
6822 6823
	ipr_cmd->qc = qc;
	ipr_cmd->done = ipr_sata_done;
6824
	ipr_cmd->ioarcb.res_handle = res->res_handle;
6825 6826 6827
	ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
	ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
	ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
6828
	ipr_cmd->dma_use_sg = qc->n_elem;
6829

6830 6831 6832 6833 6834
	if (ioa_cfg->sis64)
		ipr_build_ata_ioadl64(ipr_cmd, qc);
	else
		ipr_build_ata_ioadl(ipr_cmd, qc);

6835 6836 6837
	regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
	ipr_copy_sata_tf(regs, &qc->tf);
	memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
6838
	ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
6839 6840 6841 6842 6843 6844 6845 6846 6847 6848

	switch (qc->tf.protocol) {
	case ATA_PROT_NODATA:
	case ATA_PROT_PIO:
		break;

	case ATA_PROT_DMA:
		regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
		break;

6849 6850
	case ATAPI_PROT_PIO:
	case ATAPI_PROT_NODATA:
6851 6852 6853
		regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
		break;

6854
	case ATAPI_PROT_DMA:
6855 6856 6857 6858 6859 6860
		regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
		regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
		break;

	default:
		WARN_ON(1);
6861
		spin_unlock(&ipr_cmd->hrrq->_lock);
6862
		return AC_ERR_INVALID;
6863 6864
	}

6865
	ipr_send_command(ipr_cmd);
6866
	spin_unlock(&ipr_cmd->hrrq->_lock);
6867

6868 6869 6870
	return 0;
}

6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898
/**
 * ipr_qc_fill_rtf - Read result TF
 * @qc: ATA queued command
 *
 * Return value:
 * 	true
 **/
static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
{
	struct ipr_sata_port *sata_port = qc->ap->private_data;
	struct ipr_ioasa_gata *g = &sata_port->ioasa;
	struct ata_taskfile *tf = &qc->result_tf;

	tf->feature = g->error;
	tf->nsect = g->nsect;
	tf->lbal = g->lbal;
	tf->lbam = g->lbam;
	tf->lbah = g->lbah;
	tf->device = g->device;
	tf->command = g->status;
	tf->hob_nsect = g->hob_nsect;
	tf->hob_lbal = g->hob_lbal;
	tf->hob_lbam = g->hob_lbam;
	tf->hob_lbah = g->hob_lbah;

	return true;
}

6899 6900
static struct ata_port_operations ipr_sata_ops = {
	.phy_reset = ipr_ata_phy_reset,
6901
	.hardreset = ipr_sata_reset,
6902 6903
	.post_internal_cmd = ipr_ata_post_internal,
	.qc_prep = ata_noop_qc_prep,
6904
	.qc_defer = ipr_qc_defer,
6905
	.qc_issue = ipr_qc_issue,
6906
	.qc_fill_rtf = ipr_qc_fill_rtf,
6907 6908 6909 6910 6911
	.port_start = ata_sas_port_start,
	.port_stop = ata_sas_port_stop
};

static struct ata_port_info sata_port_info = {
6912 6913
	.flags		= ATA_FLAG_SATA | ATA_FLAG_PIO_DMA |
			  ATA_FLAG_SAS_HOST,
6914 6915 6916
	.pio_mask	= ATA_PIO4_ONLY,
	.mwdma_mask	= ATA_MWDMA2,
	.udma_mask	= ATA_UDMA6,
6917 6918 6919
	.port_ops	= &ipr_sata_ops
};

L
Linus Torvalds 已提交
6920 6921
#ifdef CONFIG_PPC_PSERIES
static const u16 ipr_blocked_processors[] = {
6922 6923 6924 6925 6926 6927 6928 6929
	PVR_NORTHSTAR,
	PVR_PULSAR,
	PVR_POWER4,
	PVR_ICESTAR,
	PVR_SSTAR,
	PVR_POWER4p,
	PVR_630,
	PVR_630p
L
Linus Torvalds 已提交
6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946
};

/**
 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
 * @ioa_cfg:	ioa cfg struct
 *
 * Adapters that use Gemstone revision < 3.1 do not work reliably on
 * certain pSeries hardware. This function determines if the given
 * adapter is in one of these confgurations or not.
 *
 * Return value:
 * 	1 if adapter is not supported / 0 if adapter is supported
 **/
static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
{
	int i;

6947
	if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
6948
		for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++) {
6949
			if (pvr_version_is(ipr_blocked_processors[i]))
6950
				return 1;
L
Linus Torvalds 已提交
6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971
		}
	}
	return 0;
}
#else
#define ipr_invalid_adapter(ioa_cfg) 0
#endif

/**
 * ipr_ioa_bringdown_done - IOA bring down completion.
 * @ipr_cmd:	ipr command struct
 *
 * This function processes the completion of an adapter bring down.
 * It wakes any reset sleepers.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6972
	int i;
L
Linus Torvalds 已提交
6973 6974

	ENTER;
6975 6976 6977 6978 6979 6980 6981
	if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa) {
		ipr_trace;
		spin_unlock_irq(ioa_cfg->host->host_lock);
		scsi_unblock_requests(ioa_cfg->host);
		spin_lock_irq(ioa_cfg->host->host_lock);
	}

L
Linus Torvalds 已提交
6982 6983
	ioa_cfg->in_reset_reload = 0;
	ioa_cfg->reset_retries = 0;
6984 6985 6986 6987 6988 6989 6990
	for (i = 0; i < ioa_cfg->hrrq_num; i++) {
		spin_lock(&ioa_cfg->hrrq[i]._lock);
		ioa_cfg->hrrq[i].ioa_is_dead = 1;
		spin_unlock(&ioa_cfg->hrrq[i]._lock);
	}
	wmb();

6991
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
L
Linus Torvalds 已提交
6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013
	wake_up_all(&ioa_cfg->reset_wait_q);
	LEAVE;

	return IPR_RC_JOB_RETURN;
}

/**
 * ipr_ioa_reset_done - IOA reset completion.
 * @ipr_cmd:	ipr command struct
 *
 * This function processes the completion of an adapter reset.
 * It schedules any necessary mid-layer add/removes and
 * wakes any reset sleepers.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_resource_entry *res;
	struct ipr_hostrcb *hostrcb, *temp;
7014
	int i = 0, j;
L
Linus Torvalds 已提交
7015 7016 7017

	ENTER;
	ioa_cfg->in_reset_reload = 0;
7018 7019 7020 7021 7022 7023
	for (j = 0; j < ioa_cfg->hrrq_num; j++) {
		spin_lock(&ioa_cfg->hrrq[j]._lock);
		ioa_cfg->hrrq[j].allow_cmds = 1;
		spin_unlock(&ioa_cfg->hrrq[j]._lock);
	}
	wmb();
L
Linus Torvalds 已提交
7024
	ioa_cfg->reset_cmd = NULL;
B
brking@us.ibm.com 已提交
7025
	ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
L
Linus Torvalds 已提交
7026 7027

	list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
7028
		if (res->add_to_ml || res->del_from_ml) {
L
Linus Torvalds 已提交
7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042
			ipr_trace;
			break;
		}
	}
	schedule_work(&ioa_cfg->work_q);

	list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
		list_del(&hostrcb->queue);
		if (i++ < IPR_NUM_LOG_HCAMS)
			ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
		else
			ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
	}

7043
	scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
L
Linus Torvalds 已提交
7044 7045 7046
	dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");

	ioa_cfg->reset_retries = 0;
7047
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
L
Linus Torvalds 已提交
7048 7049
	wake_up_all(&ioa_cfg->reset_wait_q);

7050
	spin_unlock(ioa_cfg->host->host_lock);
L
Linus Torvalds 已提交
7051
	scsi_unblock_requests(ioa_cfg->host);
7052
	spin_lock(ioa_cfg->host->host_lock);
L
Linus Torvalds 已提交
7053

7054
	if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds)
L
Linus Torvalds 已提交
7055 7056
		scsi_block_requests(ioa_cfg->host);

7057
	schedule_work(&ioa_cfg->work_q);
L
Linus Torvalds 已提交
7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084
	LEAVE;
	return IPR_RC_JOB_RETURN;
}

/**
 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
 * @supported_dev:	supported device struct
 * @vpids:			vendor product id struct
 *
 * Return value:
 * 	none
 **/
static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
				 struct ipr_std_inq_vpids *vpids)
{
	memset(supported_dev, 0, sizeof(struct ipr_supported_device));
	memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
	supported_dev->num_records = 1;
	supported_dev->data_length =
		cpu_to_be16(sizeof(struct ipr_supported_device));
	supported_dev->reserved = 0;
}

/**
 * ipr_set_supported_devs - Send Set Supported Devices for a device
 * @ipr_cmd:	ipr command struct
 *
7085
 * This function sends a Set Supported Devices to the adapter
L
Linus Torvalds 已提交
7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
	struct ipr_resource_entry *res = ipr_cmd->u.res;

	ipr_cmd->job_step = ipr_ioa_reset_done;

	list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
B
Brian King 已提交
7100
		if (!ipr_is_scsi_disk(res))
L
Linus Torvalds 已提交
7101 7102 7103
			continue;

		ipr_cmd->u.res = res;
7104
		ipr_set_sup_dev_dflt(supp_dev, &res->std_inq_data.vpids);
L
Linus Torvalds 已提交
7105 7106 7107 7108 7109 7110

		ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
		ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
		ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;

		ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
7111
		ioarcb->cmd_pkt.cdb[1] = IPR_SET_ALL_SUPPORTED_DEVICES;
L
Linus Torvalds 已提交
7112 7113 7114
		ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
		ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;

7115 7116 7117 7118 7119
		ipr_init_ioadl(ipr_cmd,
			       ioa_cfg->vpd_cbs_dma +
				 offsetof(struct ipr_misc_cbs, supp_dev),
			       sizeof(struct ipr_supported_device),
			       IPR_IOADL_FLAGS_WRITE_LAST);
L
Linus Torvalds 已提交
7120 7121 7122 7123

		ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
			   IPR_SET_SUP_DEVICE_TIMEOUT);

7124 7125
		if (!ioa_cfg->sis64)
			ipr_cmd->job_step = ipr_set_supported_devs;
7126
		LEAVE;
L
Linus Torvalds 已提交
7127 7128 7129
		return IPR_RC_JOB_RETURN;
	}

7130
	LEAVE;
L
Linus Torvalds 已提交
7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244
	return IPR_RC_JOB_CONTINUE;
}

/**
 * ipr_get_mode_page - Locate specified mode page
 * @mode_pages:	mode page buffer
 * @page_code:	page code to find
 * @len:		minimum required length for mode page
 *
 * Return value:
 * 	pointer to mode page / NULL on failure
 **/
static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
			       u32 page_code, u32 len)
{
	struct ipr_mode_page_hdr *mode_hdr;
	u32 page_length;
	u32 length;

	if (!mode_pages || (mode_pages->hdr.length == 0))
		return NULL;

	length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
	mode_hdr = (struct ipr_mode_page_hdr *)
		(mode_pages->data + mode_pages->hdr.block_desc_len);

	while (length) {
		if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
			if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
				return mode_hdr;
			break;
		} else {
			page_length = (sizeof(struct ipr_mode_page_hdr) +
				       mode_hdr->page_length);
			length -= page_length;
			mode_hdr = (struct ipr_mode_page_hdr *)
				((unsigned long)mode_hdr + page_length);
		}
	}
	return NULL;
}

/**
 * ipr_check_term_power - Check for term power errors
 * @ioa_cfg:	ioa config struct
 * @mode_pages:	IOAFP mode pages buffer
 *
 * Check the IOAFP's mode page 28 for term power errors
 *
 * Return value:
 * 	nothing
 **/
static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
				 struct ipr_mode_pages *mode_pages)
{
	int i;
	int entry_length;
	struct ipr_dev_bus_entry *bus;
	struct ipr_mode_page28 *mode_page;

	mode_page = ipr_get_mode_page(mode_pages, 0x28,
				      sizeof(struct ipr_mode_page28));

	entry_length = mode_page->entry_length;

	bus = mode_page->bus;

	for (i = 0; i < mode_page->num_entries; i++) {
		if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
			dev_err(&ioa_cfg->pdev->dev,
				"Term power is absent on scsi bus %d\n",
				bus->res_addr.bus);
		}

		bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
	}
}

/**
 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
 * @ioa_cfg:	ioa config struct
 *
 * Looks through the config table checking for SES devices. If
 * the SES device is in the SES table indicating a maximum SCSI
 * bus speed, the speed is limited for the bus.
 *
 * Return value:
 * 	none
 **/
static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
{
	u32 max_xfer_rate;
	int i;

	for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
		max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
						       ioa_cfg->bus_attr[i].bus_width);

		if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
			ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
	}
}

/**
 * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
 * @ioa_cfg:	ioa config struct
 * @mode_pages:	mode page 28 buffer
 *
 * Updates mode page 28 based on driver configuration
 *
 * Return value:
 * 	none
 **/
static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
7245
					  struct ipr_mode_pages *mode_pages)
L
Linus Torvalds 已提交
7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291
{
	int i, entry_length;
	struct ipr_dev_bus_entry *bus;
	struct ipr_bus_attributes *bus_attr;
	struct ipr_mode_page28 *mode_page;

	mode_page = ipr_get_mode_page(mode_pages, 0x28,
				      sizeof(struct ipr_mode_page28));

	entry_length = mode_page->entry_length;

	/* Loop for each device bus entry */
	for (i = 0, bus = mode_page->bus;
	     i < mode_page->num_entries;
	     i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
		if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
			dev_err(&ioa_cfg->pdev->dev,
				"Invalid resource address reported: 0x%08X\n",
				IPR_GET_PHYS_LOC(bus->res_addr));
			continue;
		}

		bus_attr = &ioa_cfg->bus_attr[i];
		bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
		bus->bus_width = bus_attr->bus_width;
		bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
		bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
		if (bus_attr->qas_enabled)
			bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
		else
			bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
	}
}

/**
 * ipr_build_mode_select - Build a mode select command
 * @ipr_cmd:	ipr command struct
 * @res_handle:	resource handle to send command to
 * @parm:		Byte 2 of Mode Sense command
 * @dma_addr:	DMA buffer address
 * @xfer_len:	data transfer length
 *
 * Return value:
 * 	none
 **/
static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
7292 7293
				  __be32 res_handle, u8 parm,
				  dma_addr_t dma_addr, u8 xfer_len)
L
Linus Torvalds 已提交
7294 7295 7296 7297 7298 7299 7300 7301 7302 7303
{
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;

	ioarcb->res_handle = res_handle;
	ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
	ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
	ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
	ioarcb->cmd_pkt.cdb[1] = parm;
	ioarcb->cmd_pkt.cdb[4] = xfer_len;

7304
	ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_WRITE_LAST);
L
Linus Torvalds 已提交
7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323
}

/**
 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
 * @ipr_cmd:	ipr command struct
 *
 * This function sets up the SCSI bus attributes and sends
 * a Mode Select for Page 28 to activate them.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
	int length;

	ENTER;
7324 7325 7326 7327 7328
	ipr_scsi_bus_speed_limit(ioa_cfg);
	ipr_check_term_power(ioa_cfg, mode_pages);
	ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
	length = mode_pages->hdr.length + 1;
	mode_pages->hdr.length = 0;
L
Linus Torvalds 已提交
7329 7330 7331 7332 7333

	ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
			      ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
			      length);

7334 7335 7336
	ipr_cmd->job_step = ipr_set_supported_devs;
	ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
				    struct ipr_resource_entry, queue);
L
Linus Torvalds 已提交
7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355
	ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

/**
 * ipr_build_mode_sense - Builds a mode sense command
 * @ipr_cmd:	ipr command struct
 * @res:		resource entry struct
 * @parm:		Byte 2 of mode sense command
 * @dma_addr:	DMA address of mode sense buffer
 * @xfer_len:	Size of DMA buffer
 *
 * Return value:
 * 	none
 **/
static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
				 __be32 res_handle,
7356
				 u8 parm, dma_addr_t dma_addr, u8 xfer_len)
L
Linus Torvalds 已提交
7357 7358 7359 7360 7361 7362 7363 7364 7365
{
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;

	ioarcb->res_handle = res_handle;
	ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
	ioarcb->cmd_pkt.cdb[2] = parm;
	ioarcb->cmd_pkt.cdb[4] = xfer_len;
	ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;

7366
	ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
L
Linus Torvalds 已提交
7367 7368
}

7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380
/**
 * ipr_reset_cmd_failed - Handle failure of IOA reset command
 * @ipr_cmd:	ipr command struct
 *
 * This function handles the failure of an IOA bringup command.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7381
	u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
7382 7383 7384 7385 7386 7387

	dev_err(&ioa_cfg->pdev->dev,
		"0x%02X failed with IOASC: 0x%08X\n",
		ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);

	ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
7388
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403
	return IPR_RC_JOB_RETURN;
}

/**
 * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
 * @ipr_cmd:	ipr command struct
 *
 * This function handles the failure of a Mode Sense to the IOAFP.
 * Some adapters do not handle all mode pages.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
{
7404
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7405
	u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
7406 7407

	if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
7408 7409 7410
		ipr_cmd->job_step = ipr_set_supported_devs;
		ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
					    struct ipr_resource_entry, queue);
7411 7412 7413 7414 7415 7416
		return IPR_RC_JOB_CONTINUE;
	}

	return ipr_reset_cmd_failed(ipr_cmd);
}

L
Linus Torvalds 已提交
7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437
/**
 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
 * @ipr_cmd:	ipr command struct
 *
 * This function send a Page 28 mode sense to the IOA to
 * retrieve SCSI bus attributes.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	ENTER;
	ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
			     0x28, ioa_cfg->vpd_cbs_dma +
			     offsetof(struct ipr_misc_cbs, mode_pages),
			     sizeof(struct ipr_mode_pages));

	ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
7438
	ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
L
Linus Torvalds 已提交
7439 7440 7441 7442 7443 7444 7445

	ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494
/**
 * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
 * @ipr_cmd:	ipr command struct
 *
 * This function enables dual IOA RAID support if possible.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
	struct ipr_mode_page24 *mode_page;
	int length;

	ENTER;
	mode_page = ipr_get_mode_page(mode_pages, 0x24,
				      sizeof(struct ipr_mode_page24));

	if (mode_page)
		mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;

	length = mode_pages->hdr.length + 1;
	mode_pages->hdr.length = 0;

	ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
			      ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
			      length);

	ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
	ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

/**
 * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
 * @ipr_cmd:	ipr command struct
 *
 * This function handles the failure of a Mode Sense to the IOAFP.
 * Some adapters do not handle all mode pages.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
{
7495
	u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533

	if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
		ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
		return IPR_RC_JOB_CONTINUE;
	}

	return ipr_reset_cmd_failed(ipr_cmd);
}

/**
 * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
 * @ipr_cmd:	ipr command struct
 *
 * This function send a mode sense to the IOA to retrieve
 * the IOA Advanced Function Control mode page.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	ENTER;
	ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
			     0x24, ioa_cfg->vpd_cbs_dma +
			     offsetof(struct ipr_misc_cbs, mode_pages),
			     sizeof(struct ipr_mode_pages));

	ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
	ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;

	ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

L
Linus Torvalds 已提交
7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549
/**
 * ipr_init_res_table - Initialize the resource table
 * @ipr_cmd:	ipr command struct
 *
 * This function looks through the existing resource table, comparing
 * it with the config table. This function will take care of old/new
 * devices and schedule adding/removing them from the mid-layer
 * as appropriate.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE
 **/
static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_resource_entry *res, *temp;
7550 7551
	struct ipr_config_table_entry_wrapper cfgtew;
	int entries, found, flag, i;
L
Linus Torvalds 已提交
7552 7553 7554
	LIST_HEAD(old_res);

	ENTER;
7555 7556 7557 7558 7559 7560
	if (ioa_cfg->sis64)
		flag = ioa_cfg->u.cfg_table64->hdr64.flags;
	else
		flag = ioa_cfg->u.cfg_table->hdr.flags;

	if (flag & IPR_UCODE_DOWNLOAD_REQ)
L
Linus Torvalds 已提交
7561 7562 7563 7564 7565
		dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");

	list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
		list_move_tail(&res->queue, &old_res);

7566
	if (ioa_cfg->sis64)
7567
		entries = be16_to_cpu(ioa_cfg->u.cfg_table64->hdr64.num_entries);
7568 7569 7570 7571 7572 7573 7574 7575
	else
		entries = ioa_cfg->u.cfg_table->hdr.num_entries;

	for (i = 0; i < entries; i++) {
		if (ioa_cfg->sis64)
			cfgtew.u.cfgte64 = &ioa_cfg->u.cfg_table64->dev[i];
		else
			cfgtew.u.cfgte = &ioa_cfg->u.cfg_table->dev[i];
L
Linus Torvalds 已提交
7576 7577 7578
		found = 0;

		list_for_each_entry_safe(res, temp, &old_res, queue) {
7579
			if (ipr_is_same_device(res, &cfgtew)) {
L
Linus Torvalds 已提交
7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595
				list_move_tail(&res->queue, &ioa_cfg->used_res_q);
				found = 1;
				break;
			}
		}

		if (!found) {
			if (list_empty(&ioa_cfg->free_res_q)) {
				dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
				break;
			}

			found = 1;
			res = list_entry(ioa_cfg->free_res_q.next,
					 struct ipr_resource_entry, queue);
			list_move_tail(&res->queue, &ioa_cfg->used_res_q);
7596
			ipr_init_res_entry(res, &cfgtew);
L
Linus Torvalds 已提交
7597
			res->add_to_ml = 1;
7598 7599
		} else if (res->sdev && (ipr_is_vset_device(res) || ipr_is_scsi_disk(res)))
			res->sdev->allow_restart = 1;
L
Linus Torvalds 已提交
7600 7601

		if (found)
7602
			ipr_update_res_entry(res, &cfgtew);
L
Linus Torvalds 已提交
7603 7604 7605 7606 7607
	}

	list_for_each_entry_safe(res, temp, &old_res, queue) {
		if (res->sdev) {
			res->del_from_ml = 1;
7608
			res->res_handle = IPR_INVALID_RES_HANDLE;
L
Linus Torvalds 已提交
7609 7610 7611 7612
			list_move_tail(&res->queue, &ioa_cfg->used_res_q);
		}
	}

7613 7614 7615 7616 7617
	list_for_each_entry_safe(res, temp, &old_res, queue) {
		ipr_clear_res_target(res);
		list_move_tail(&res->queue, &ioa_cfg->free_res_q);
	}

7618 7619 7620 7621
	if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
		ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
	else
		ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
L
Linus Torvalds 已提交
7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641

	LEAVE;
	return IPR_RC_JOB_CONTINUE;
}

/**
 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
 * @ipr_cmd:	ipr command struct
 *
 * This function sends a Query IOA Configuration command
 * to the adapter to retrieve the IOA configuration table.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
	struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
7642
	struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
L
Linus Torvalds 已提交
7643 7644

	ENTER;
7645 7646
	if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
		ioa_cfg->dual_raid = 1;
L
Linus Torvalds 已提交
7647 7648 7649 7650 7651 7652 7653
	dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
		 ucode_vpd->major_release, ucode_vpd->card_type,
		 ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
	ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
	ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);

	ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
7654
	ioarcb->cmd_pkt.cdb[6] = (ioa_cfg->cfg_table_size >> 16) & 0xff;
7655 7656
	ioarcb->cmd_pkt.cdb[7] = (ioa_cfg->cfg_table_size >> 8) & 0xff;
	ioarcb->cmd_pkt.cdb[8] = ioa_cfg->cfg_table_size & 0xff;
L
Linus Torvalds 已提交
7657

7658
	ipr_init_ioadl(ipr_cmd, ioa_cfg->cfg_table_dma, ioa_cfg->cfg_table_size,
7659
		       IPR_IOADL_FLAGS_READ_LAST);
L
Linus Torvalds 已提交
7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678

	ipr_cmd->job_step = ipr_init_res_table;

	ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

/**
 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
 * @ipr_cmd:	ipr command struct
 *
 * This utility function sends an inquiry to the adapter.
 *
 * Return value:
 * 	none
 **/
static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
7679
			      dma_addr_t dma_addr, u8 xfer_len)
L
Linus Torvalds 已提交
7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691
{
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;

	ENTER;
	ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
	ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);

	ioarcb->cmd_pkt.cdb[0] = INQUIRY;
	ioarcb->cmd_pkt.cdb[1] = flags;
	ioarcb->cmd_pkt.cdb[2] = page;
	ioarcb->cmd_pkt.cdb[4] = xfer_len;

7692
	ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
L
Linus Torvalds 已提交
7693 7694 7695 7696 7697

	ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
	LEAVE;
}

7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718
/**
 * ipr_inquiry_page_supported - Is the given inquiry page supported
 * @page0:		inquiry page 0 buffer
 * @page:		page code.
 *
 * This function determines if the specified inquiry page is supported.
 *
 * Return value:
 *	1 if page is supported / 0 if not
 **/
static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
{
	int i;

	for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
		if (page0->page[i] == page)
			return 1;

	return 0;
}

7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749
/**
 * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
 * @ipr_cmd:	ipr command struct
 *
 * This function sends a Page 0xD0 inquiry to the adapter
 * to retrieve adapter capabilities.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
	struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;

	ENTER;
	ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
	memset(cap, 0, sizeof(*cap));

	if (ipr_inquiry_page_supported(page0, 0xD0)) {
		ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
				  ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
				  sizeof(struct ipr_inquiry_cap));
		return IPR_RC_JOB_RETURN;
	}

	LEAVE;
	return IPR_RC_JOB_CONTINUE;
}

L
Linus Torvalds 已提交
7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760
/**
 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
 * @ipr_cmd:	ipr command struct
 *
 * This function sends a Page 3 inquiry to the adapter
 * to retrieve software VPD information.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
7761 7762 7763 7764 7765
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	ENTER;

7766
	ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786

	ipr_ioafp_inquiry(ipr_cmd, 1, 3,
			  ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
			  sizeof(struct ipr_inquiry_page3));

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

/**
 * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
 * @ipr_cmd:	ipr command struct
 *
 * This function sends a Page 0 inquiry to the adapter
 * to retrieve supported inquiry pages.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
L
Linus Torvalds 已提交
7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	char type[5];

	ENTER;

	/* Grab the type out of the VPD and store it away */
	memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
	type[4] = '\0';
	ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);

7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810
	if (ipr_invalid_adapter(ioa_cfg)) {
		dev_err(&ioa_cfg->pdev->dev,
			"Adapter not supported in this hardware configuration.\n");

		if (!ipr_testmode) {
			ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
			ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
			list_add_tail(&ipr_cmd->queue,
					&ioa_cfg->hrrq->hrrq_free_q);
			return IPR_RC_JOB_RETURN;
		}
	}

7811
	ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
L
Linus Torvalds 已提交
7812

7813 7814 7815
	ipr_ioafp_inquiry(ipr_cmd, 1, 0,
			  ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
			  sizeof(struct ipr_inquiry_page0));
L
Linus Torvalds 已提交
7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

/**
 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
 * @ipr_cmd:	ipr command struct
 *
 * This function sends a standard inquiry to the adapter.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	ENTER;
7835
	ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
L
Linus Torvalds 已提交
7836 7837 7838 7839 7840 7841 7842 7843 7844 7845

	ipr_ioafp_inquiry(ipr_cmd, 0, 0,
			  ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
			  sizeof(struct ipr_ioa_vpd));

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

/**
7846
 * ipr_ioafp_identify_hrrq - Send Identify Host RRQ.
L
Linus Torvalds 已提交
7847 7848 7849 7850 7851 7852 7853 7854
 * @ipr_cmd:	ipr command struct
 *
 * This function send an Identify Host Request Response Queue
 * command to establish the HRRQ with the adapter.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
7855
static int ipr_ioafp_identify_hrrq(struct ipr_cmnd *ipr_cmd)
L
Linus Torvalds 已提交
7856 7857 7858
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7859
	struct ipr_hrr_queue *hrrq;
L
Linus Torvalds 已提交
7860 7861

	ENTER;
7862
	ipr_cmd->job_step = ipr_ioafp_std_inquiry;
L
Linus Torvalds 已提交
7863 7864
	dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");

7865 7866
	if (ioa_cfg->identify_hrrq_index < ioa_cfg->hrrq_num) {
		hrrq = &ioa_cfg->hrrq[ioa_cfg->identify_hrrq_index];
L
Linus Torvalds 已提交
7867

7868 7869
		ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
		ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
L
Linus Torvalds 已提交
7870

7871 7872 7873
		ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
		if (ioa_cfg->sis64)
			ioarcb->cmd_pkt.cdb[1] = 0x1;
7874

7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893
		if (ioa_cfg->nvectors == 1)
			ioarcb->cmd_pkt.cdb[1] &= ~IPR_ID_HRRQ_SELE_ENABLE;
		else
			ioarcb->cmd_pkt.cdb[1] |= IPR_ID_HRRQ_SELE_ENABLE;

		ioarcb->cmd_pkt.cdb[2] =
			((u64) hrrq->host_rrq_dma >> 24) & 0xff;
		ioarcb->cmd_pkt.cdb[3] =
			((u64) hrrq->host_rrq_dma >> 16) & 0xff;
		ioarcb->cmd_pkt.cdb[4] =
			((u64) hrrq->host_rrq_dma >> 8) & 0xff;
		ioarcb->cmd_pkt.cdb[5] =
			((u64) hrrq->host_rrq_dma) & 0xff;
		ioarcb->cmd_pkt.cdb[7] =
			((sizeof(u32) * hrrq->size) >> 8) & 0xff;
		ioarcb->cmd_pkt.cdb[8] =
			(sizeof(u32) * hrrq->size) & 0xff;

		if (ioarcb->cmd_pkt.cdb[1] & IPR_ID_HRRQ_SELE_ENABLE)
7894 7895
			ioarcb->cmd_pkt.cdb[9] =
					ioa_cfg->identify_hrrq_index;
L
Linus Torvalds 已提交
7896

7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908
		if (ioa_cfg->sis64) {
			ioarcb->cmd_pkt.cdb[10] =
				((u64) hrrq->host_rrq_dma >> 56) & 0xff;
			ioarcb->cmd_pkt.cdb[11] =
				((u64) hrrq->host_rrq_dma >> 48) & 0xff;
			ioarcb->cmd_pkt.cdb[12] =
				((u64) hrrq->host_rrq_dma >> 40) & 0xff;
			ioarcb->cmd_pkt.cdb[13] =
				((u64) hrrq->host_rrq_dma >> 32) & 0xff;
		}

		if (ioarcb->cmd_pkt.cdb[1] & IPR_ID_HRRQ_SELE_ENABLE)
7909 7910
			ioarcb->cmd_pkt.cdb[14] =
					ioa_cfg->identify_hrrq_index;
7911 7912 7913 7914

		ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
			   IPR_INTERNAL_TIMEOUT);

7915 7916
		if (++ioa_cfg->identify_hrrq_index < ioa_cfg->hrrq_num)
			ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7917 7918 7919 7920 7921

		LEAVE;
		return IPR_RC_JOB_RETURN;
	}

L
Linus Torvalds 已提交
7922
	LEAVE;
7923
	return IPR_RC_JOB_CONTINUE;
L
Linus Torvalds 已提交
7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970
}

/**
 * ipr_reset_timer_done - Adapter reset timer function
 * @ipr_cmd:	ipr command struct
 *
 * Description: This function is used in adapter reset processing
 * for timing events. If the reset_cmd pointer in the IOA
 * config struct is not this adapter's we are doing nested
 * resets and fail_all_ops will take care of freeing the
 * command block.
 *
 * Return value:
 * 	none
 **/
static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	unsigned long lock_flags = 0;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);

	if (ioa_cfg->reset_cmd == ipr_cmd) {
		list_del(&ipr_cmd->queue);
		ipr_cmd->done(ipr_cmd);
	}

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
}

/**
 * ipr_reset_start_timer - Start a timer for adapter reset job
 * @ipr_cmd:	ipr command struct
 * @timeout:	timeout value
 *
 * Description: This function is used in adapter reset processing
 * for timing events. If the reset_cmd pointer in the IOA
 * config struct is not this adapter's we are doing nested
 * resets and fail_all_ops will take care of freeing the
 * command block.
 *
 * Return value:
 * 	none
 **/
static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
				  unsigned long timeout)
{
7971 7972 7973

	ENTER;
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
L
Linus Torvalds 已提交
7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990
	ipr_cmd->done = ipr_reset_ioa_job;

	ipr_cmd->timer.data = (unsigned long) ipr_cmd;
	ipr_cmd->timer.expires = jiffies + timeout;
	ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
	add_timer(&ipr_cmd->timer);
}

/**
 * ipr_init_ioa_mem - Initialize ioa_cfg control block
 * @ioa_cfg:	ioa cfg struct
 *
 * Return value:
 * 	nothing
 **/
static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
{
7991
	struct ipr_hrr_queue *hrrq;
L
Linus Torvalds 已提交
7992

7993
	for_each_hrrq(hrrq, ioa_cfg) {
7994
		spin_lock(&hrrq->_lock);
7995 7996 7997 7998 7999 8000 8001
		memset(hrrq->host_rrq, 0, sizeof(u32) * hrrq->size);

		/* Initialize Host RRQ pointers */
		hrrq->hrrq_start = hrrq->host_rrq;
		hrrq->hrrq_end = &hrrq->host_rrq[hrrq->size - 1];
		hrrq->hrrq_curr = hrrq->hrrq_start;
		hrrq->toggle_bit = 1;
8002
		spin_unlock(&hrrq->_lock);
8003
	}
8004
	wmb();
8005

8006 8007 8008 8009 8010
	ioa_cfg->identify_hrrq_index = 0;
	if (ioa_cfg->hrrq_num == 1)
		atomic_set(&ioa_cfg->hrrq_index, 0);
	else
		atomic_set(&ioa_cfg->hrrq_index, 1);
L
Linus Torvalds 已提交
8011 8012

	/* Zero out config table */
8013
	memset(ioa_cfg->u.cfg_table, 0, ioa_cfg->cfg_table_size);
L
Linus Torvalds 已提交
8014 8015
}

8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037
/**
 * ipr_reset_next_stage - Process IPL stage change based on feedback register.
 * @ipr_cmd:	ipr command struct
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_reset_next_stage(struct ipr_cmnd *ipr_cmd)
{
	unsigned long stage, stage_time;
	u32 feedback;
	volatile u32 int_reg;
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	u64 maskval = 0;

	feedback = readl(ioa_cfg->regs.init_feedback_reg);
	stage = feedback & IPR_IPL_INIT_STAGE_MASK;
	stage_time = feedback & IPR_IPL_INIT_STAGE_TIME_MASK;

	ipr_dbg("IPL stage = 0x%lx, IPL stage time = %ld\n", stage, stage_time);

	/* sanity check the stage_time value */
8038 8039 8040
	if (stage_time == 0)
		stage_time = IPR_IPL_INIT_DEFAULT_STAGE_TIME;
	else if (stage_time < IPR_IPL_INIT_MIN_STAGE_TIME)
8041 8042 8043 8044 8045 8046 8047 8048 8049 8050
		stage_time = IPR_IPL_INIT_MIN_STAGE_TIME;
	else if (stage_time > IPR_LONG_OPERATIONAL_TIMEOUT)
		stage_time = IPR_LONG_OPERATIONAL_TIMEOUT;

	if (stage == IPR_IPL_INIT_STAGE_UNKNOWN) {
		writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.set_interrupt_mask_reg);
		int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
		stage_time = ioa_cfg->transop_timeout;
		ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
	} else if (stage == IPR_IPL_INIT_STAGE_TRANSOP) {
8051 8052 8053 8054 8055 8056 8057 8058 8059
		int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
		if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
			ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
			maskval = IPR_PCII_IPL_STAGE_CHANGE;
			maskval = (maskval << 32) | IPR_PCII_IOA_TRANS_TO_OPER;
			writeq(maskval, ioa_cfg->regs.set_interrupt_mask_reg);
			int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
			return IPR_RC_JOB_CONTINUE;
		}
8060 8061 8062 8063 8064 8065 8066
	}

	ipr_cmd->timer.data = (unsigned long) ipr_cmd;
	ipr_cmd->timer.expires = jiffies + stage_time * HZ;
	ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
	ipr_cmd->done = ipr_reset_ioa_job;
	add_timer(&ipr_cmd->timer);
8067 8068

	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
8069 8070 8071 8072

	return IPR_RC_JOB_RETURN;
}

L
Linus Torvalds 已提交
8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086
/**
 * ipr_reset_enable_ioa - Enable the IOA following a reset.
 * @ipr_cmd:	ipr command struct
 *
 * This function reinitializes some control blocks and
 * enables destructive diagnostics on the adapter.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	volatile u32 int_reg;
8087
	volatile u64 maskval;
8088
	int i;
L
Linus Torvalds 已提交
8089 8090

	ENTER;
8091
	ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
L
Linus Torvalds 已提交
8092 8093
	ipr_init_ioa_mem(ioa_cfg);

8094 8095 8096 8097 8098 8099
	for (i = 0; i < ioa_cfg->hrrq_num; i++) {
		spin_lock(&ioa_cfg->hrrq[i]._lock);
		ioa_cfg->hrrq[i].allow_interrupts = 1;
		spin_unlock(&ioa_cfg->hrrq[i]._lock);
	}
	wmb();
8100 8101 8102 8103 8104 8105
	if (ioa_cfg->sis64) {
		/* Set the adapter to the correct endian mode. */
		writel(IPR_ENDIAN_SWAP_KEY, ioa_cfg->regs.endian_swap_reg);
		int_reg = readl(ioa_cfg->regs.endian_swap_reg);
	}

8106
	int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
L
Linus Torvalds 已提交
8107 8108 8109

	if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
		writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
8110
		       ioa_cfg->regs.clr_interrupt_mask_reg32);
L
Linus Torvalds 已提交
8111 8112 8113 8114 8115
		int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
		return IPR_RC_JOB_CONTINUE;
	}

	/* Enable destructive diagnostics on IOA */
8116 8117
	writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg32);

8118 8119 8120 8121 8122 8123
	if (ioa_cfg->sis64) {
		maskval = IPR_PCII_IPL_STAGE_CHANGE;
		maskval = (maskval << 32) | IPR_PCII_OPER_INTERRUPTS;
		writeq(maskval, ioa_cfg->regs.clr_interrupt_mask_reg);
	} else
		writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg32);
L
Linus Torvalds 已提交
8124 8125 8126 8127 8128

	int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);

	dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");

8129 8130 8131 8132 8133
	if (ioa_cfg->sis64) {
		ipr_cmd->job_step = ipr_reset_next_stage;
		return IPR_RC_JOB_CONTINUE;
	}

L
Linus Torvalds 已提交
8134
	ipr_cmd->timer.data = (unsigned long) ipr_cmd;
8135
	ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
L
Linus Torvalds 已提交
8136 8137 8138
	ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
	ipr_cmd->done = ipr_reset_ioa_job;
	add_timer(&ipr_cmd->timer);
8139
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
L
Linus Torvalds 已提交
8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

/**
 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
 * @ipr_cmd:	ipr command struct
 *
 * This function is invoked when an adapter dump has run out
 * of processing time.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE
 **/
static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	if (ioa_cfg->sdt_state == GET_DUMP)
8160 8161
		ioa_cfg->sdt_state = WAIT_FOR_DUMP;
	else if (ioa_cfg->sdt_state == READ_DUMP)
L
Linus Torvalds 已提交
8162 8163
		ioa_cfg->sdt_state = ABORT_DUMP;

8164
	ioa_cfg->dump_timeout = 1;
L
Linus Torvalds 已提交
8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201
	ipr_cmd->job_step = ipr_reset_alert;

	return IPR_RC_JOB_CONTINUE;
}

/**
 * ipr_unit_check_no_data - Log a unit check/no data error log
 * @ioa_cfg:		ioa config struct
 *
 * Logs an error indicating the adapter unit checked, but for some
 * reason, we were unable to fetch the unit check buffer.
 *
 * Return value:
 * 	nothing
 **/
static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
{
	ioa_cfg->errors_logged++;
	dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
}

/**
 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
 * @ioa_cfg:		ioa config struct
 *
 * Fetches the unit check buffer from the adapter by clocking the data
 * through the mailbox register.
 *
 * Return value:
 * 	nothing
 **/
static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
{
	unsigned long mailbox;
	struct ipr_hostrcb *hostrcb;
	struct ipr_uc_sdt sdt;
	int rc, length;
8202
	u32 ioasc;
L
Linus Torvalds 已提交
8203 8204 8205

	mailbox = readl(ioa_cfg->ioa_mailbox);

8206
	if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(mailbox)) {
L
Linus Torvalds 已提交
8207 8208 8209 8210 8211 8212 8213 8214
		ipr_unit_check_no_data(ioa_cfg);
		return;
	}

	memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
	rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
					(sizeof(struct ipr_uc_sdt)) / sizeof(__be32));

8215 8216 8217
	if (rc || !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY) ||
	    ((be32_to_cpu(sdt.hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
	    (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
L
Linus Torvalds 已提交
8218 8219 8220 8221 8222
		ipr_unit_check_no_data(ioa_cfg);
		return;
	}

	/* Find length of the first sdt entry (UC buffer) */
8223 8224 8225 8226 8227 8228
	if (be32_to_cpu(sdt.hdr.state) == IPR_FMT3_SDT_READY_TO_USE)
		length = be32_to_cpu(sdt.entry[0].end_token);
	else
		length = (be32_to_cpu(sdt.entry[0].end_token) -
			  be32_to_cpu(sdt.entry[0].start_token)) &
			  IPR_FMT2_MBX_ADDR_MASK;
L
Linus Torvalds 已提交
8229 8230 8231 8232 8233 8234 8235

	hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
			     struct ipr_hostrcb, queue);
	list_del(&hostrcb->queue);
	memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));

	rc = ipr_get_ldump_data_section(ioa_cfg,
8236
					be32_to_cpu(sdt.entry[0].start_token),
L
Linus Torvalds 已提交
8237 8238 8239
					(__be32 *)&hostrcb->hcam,
					min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));

8240
	if (!rc) {
L
Linus Torvalds 已提交
8241
		ipr_handle_log_data(ioa_cfg, hostrcb);
8242
		ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
8243 8244 8245 8246
		if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
		    ioa_cfg->sdt_state == GET_DUMP)
			ioa_cfg->sdt_state = WAIT_FOR_DUMP;
	} else
L
Linus Torvalds 已提交
8247 8248 8249 8250 8251
		ipr_unit_check_no_data(ioa_cfg);

	list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
}

8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274
/**
 * ipr_reset_get_unit_check_job - Call to get the unit check buffer.
 * @ipr_cmd:	ipr command struct
 *
 * Description: This function will call to get the unit check buffer.
 *
 * Return value:
 *	IPR_RC_JOB_RETURN
 **/
static int ipr_reset_get_unit_check_job(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	ENTER;
	ioa_cfg->ioa_unit_checked = 0;
	ipr_get_unit_check_buffer(ioa_cfg);
	ipr_cmd->job_step = ipr_reset_alert;
	ipr_reset_start_timer(ipr_cmd, 0);

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

L
Linus Torvalds 已提交
8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288
/**
 * ipr_reset_restore_cfg_space - Restore PCI config space.
 * @ipr_cmd:	ipr command struct
 *
 * Description: This function restores the saved PCI config space of
 * the adapter, fails all outstanding ops back to the callers, and
 * fetches the dump/unit check if applicable to this reset.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8289
	u32 int_reg;
L
Linus Torvalds 已提交
8290 8291

	ENTER;
8292
	ioa_cfg->pdev->state_saved = true;
8293
	pci_restore_state(ioa_cfg->pdev);
L
Linus Torvalds 已提交
8294 8295

	if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
8296
		ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
L
Linus Torvalds 已提交
8297 8298 8299 8300 8301
		return IPR_RC_JOB_CONTINUE;
	}

	ipr_fail_all_ops(ioa_cfg);

8302 8303 8304 8305 8306 8307
	if (ioa_cfg->sis64) {
		/* Set the adapter to the correct endian mode. */
		writel(IPR_ENDIAN_SWAP_KEY, ioa_cfg->regs.endian_swap_reg);
		int_reg = readl(ioa_cfg->regs.endian_swap_reg);
	}

L
Linus Torvalds 已提交
8308
	if (ioa_cfg->ioa_unit_checked) {
8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319
		if (ioa_cfg->sis64) {
			ipr_cmd->job_step = ipr_reset_get_unit_check_job;
			ipr_reset_start_timer(ipr_cmd, IPR_DUMP_DELAY_TIMEOUT);
			return IPR_RC_JOB_RETURN;
		} else {
			ioa_cfg->ioa_unit_checked = 0;
			ipr_get_unit_check_buffer(ioa_cfg);
			ipr_cmd->job_step = ipr_reset_alert;
			ipr_reset_start_timer(ipr_cmd, 0);
			return IPR_RC_JOB_RETURN;
		}
L
Linus Torvalds 已提交
8320 8321 8322 8323 8324 8325 8326 8327
	}

	if (ioa_cfg->in_ioa_bringdown) {
		ipr_cmd->job_step = ipr_ioa_bringdown_done;
	} else {
		ipr_cmd->job_step = ipr_reset_enable_ioa;

		if (GET_DUMP == ioa_cfg->sdt_state) {
8328
			ioa_cfg->sdt_state = READ_DUMP;
8329
			ioa_cfg->dump_timeout = 0;
8330 8331 8332 8333
			if (ioa_cfg->sis64)
				ipr_reset_start_timer(ipr_cmd, IPR_SIS64_DUMP_TIMEOUT);
			else
				ipr_reset_start_timer(ipr_cmd, IPR_SIS32_DUMP_TIMEOUT);
L
Linus Torvalds 已提交
8334 8335 8336 8337 8338 8339
			ipr_cmd->job_step = ipr_reset_wait_for_dump;
			schedule_work(&ioa_cfg->work_q);
			return IPR_RC_JOB_RETURN;
		}
	}

8340
	LEAVE;
L
Linus Torvalds 已提交
8341 8342 8343
	return IPR_RC_JOB_CONTINUE;
}

B
Brian King 已提交
8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354
/**
 * ipr_reset_bist_done - BIST has completed on the adapter.
 * @ipr_cmd:	ipr command struct
 *
 * Description: Unblock config space and resume the reset process.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE
 **/
static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
{
8355 8356
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

B
Brian King 已提交
8357
	ENTER;
8358 8359 8360
	if (ioa_cfg->cfg_locked)
		pci_cfg_access_unlock(ioa_cfg->pdev);
	ioa_cfg->cfg_locked = 0;
B
Brian King 已提交
8361 8362 8363 8364 8365
	ipr_cmd->job_step = ipr_reset_restore_cfg_space;
	LEAVE;
	return IPR_RC_JOB_CONTINUE;
}

L
Linus Torvalds 已提交
8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377
/**
 * ipr_reset_start_bist - Run BIST on the adapter.
 * @ipr_cmd:	ipr command struct
 *
 * Description: This function runs BIST on the adapter, then delays 2 seconds.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8378
	int rc = PCIBIOS_SUCCESSFUL;
L
Linus Torvalds 已提交
8379 8380

	ENTER;
8381 8382 8383 8384 8385 8386 8387
	if (ioa_cfg->ipr_chip->bist_method == IPR_MMIO)
		writel(IPR_UPROCI_SIS64_START_BIST,
		       ioa_cfg->regs.set_uproc_interrupt_reg32);
	else
		rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);

	if (rc == PCIBIOS_SUCCESSFUL) {
B
Brian King 已提交
8388
		ipr_cmd->job_step = ipr_reset_bist_done;
L
Linus Torvalds 已提交
8389 8390
		ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
		rc = IPR_RC_JOB_RETURN;
8391
	} else {
8392 8393 8394
		if (ioa_cfg->cfg_locked)
			pci_cfg_access_unlock(ipr_cmd->ioa_cfg->pdev);
		ioa_cfg->cfg_locked = 0;
8395 8396
		ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
		rc = IPR_RC_JOB_CONTINUE;
L
Linus Torvalds 已提交
8397 8398 8399 8400 8401 8402
	}

	LEAVE;
	return rc;
}

8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420
/**
 * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
 * @ipr_cmd:	ipr command struct
 *
 * Description: This clears PCI reset to the adapter and delays two seconds.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
{
	ENTER;
	ipr_cmd->job_step = ipr_reset_bist_done;
	ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
	LEAVE;
	return IPR_RC_JOB_RETURN;
}

B
Brian King 已提交
8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446
/**
 * ipr_reset_reset_work - Pulse a PCIe fundamental reset
 * @work:	work struct
 *
 * Description: This pulses warm reset to a slot.
 *
 **/
static void ipr_reset_reset_work(struct work_struct *work)
{
	struct ipr_cmnd *ipr_cmd = container_of(work, struct ipr_cmnd, work);
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct pci_dev *pdev = ioa_cfg->pdev;
	unsigned long lock_flags = 0;

	ENTER;
	pci_set_pcie_reset_state(pdev, pcie_warm_reset);
	msleep(jiffies_to_msecs(IPR_PCI_RESET_TIMEOUT));
	pci_set_pcie_reset_state(pdev, pcie_deassert_reset);

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	if (ioa_cfg->reset_cmd == ipr_cmd)
		ipr_reset_ioa_job(ipr_cmd);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	LEAVE;
}

8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460
/**
 * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
 * @ipr_cmd:	ipr command struct
 *
 * Description: This asserts PCI reset to the adapter.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	ENTER;
B
Brian King 已提交
8461 8462
	INIT_WORK(&ipr_cmd->work, ipr_reset_reset_work);
	queue_work(ioa_cfg->reset_work_q, &ipr_cmd->work);
8463 8464 8465 8466 8467
	ipr_cmd->job_step = ipr_reset_slot_reset_done;
	LEAVE;
	return IPR_RC_JOB_RETURN;
}

8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517
/**
 * ipr_reset_block_config_access_wait - Wait for permission to block config access
 * @ipr_cmd:	ipr command struct
 *
 * Description: This attempts to block config access to the IOA.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_reset_block_config_access_wait(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	int rc = IPR_RC_JOB_CONTINUE;

	if (pci_cfg_access_trylock(ioa_cfg->pdev)) {
		ioa_cfg->cfg_locked = 1;
		ipr_cmd->job_step = ioa_cfg->reset;
	} else {
		if (ipr_cmd->u.time_left) {
			rc = IPR_RC_JOB_RETURN;
			ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
			ipr_reset_start_timer(ipr_cmd,
					      IPR_CHECK_FOR_RESET_TIMEOUT);
		} else {
			ipr_cmd->job_step = ioa_cfg->reset;
			dev_err(&ioa_cfg->pdev->dev,
				"Timed out waiting to lock config access. Resetting anyway.\n");
		}
	}

	return rc;
}

/**
 * ipr_reset_block_config_access - Block config access to the IOA
 * @ipr_cmd:	ipr command struct
 *
 * Description: This attempts to block config access to the IOA
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE
 **/
static int ipr_reset_block_config_access(struct ipr_cmnd *ipr_cmd)
{
	ipr_cmd->ioa_cfg->cfg_locked = 0;
	ipr_cmd->job_step = ipr_reset_block_config_access_wait;
	ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
	return IPR_RC_JOB_CONTINUE;
}

L
Linus Torvalds 已提交
8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556
/**
 * ipr_reset_allowed - Query whether or not IOA can be reset
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 * 	0 if reset not allowed / non-zero if reset is allowed
 **/
static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
{
	volatile u32 temp_reg;

	temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
	return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
}

/**
 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
 * @ipr_cmd:	ipr command struct
 *
 * Description: This function waits for adapter permission to run BIST,
 * then runs BIST. If the adapter does not give permission after a
 * reasonable time, we will reset the adapter anyway. The impact of
 * resetting the adapter without warning the adapter is the risk of
 * losing the persistent error log on the adapter. If the adapter is
 * reset while it is writing to the flash on the adapter, the flash
 * segment will have bad ECC and be zeroed.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	int rc = IPR_RC_JOB_RETURN;

	if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
		ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
		ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
	} else {
8557
		ipr_cmd->job_step = ipr_reset_block_config_access;
L
Linus Torvalds 已提交
8558 8559 8560 8561 8562 8563 8564
		rc = IPR_RC_JOB_CONTINUE;
	}

	return rc;
}

/**
8565
 * ipr_reset_alert - Alert the adapter of a pending reset
L
Linus Torvalds 已提交
8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586
 * @ipr_cmd:	ipr command struct
 *
 * Description: This function alerts the adapter that it will be reset.
 * If memory space is not currently enabled, proceed directly
 * to running BIST on the adapter. The timer must always be started
 * so we guarantee we do not run BIST from ipr_isr.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN
 **/
static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	u16 cmd_reg;
	int rc;

	ENTER;
	rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);

	if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
		ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
8587
		writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg32);
L
Linus Torvalds 已提交
8588 8589
		ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
	} else {
8590
		ipr_cmd->job_step = ipr_reset_block_config_access;
L
Linus Torvalds 已提交
8591 8592 8593 8594 8595 8596 8597 8598 8599
	}

	ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
	ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

B
Brian King 已提交
8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715
/**
 * ipr_reset_quiesce_done - Complete IOA disconnect
 * @ipr_cmd:	ipr command struct
 *
 * Description: Freeze the adapter to complete quiesce processing
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE
 **/
static int ipr_reset_quiesce_done(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	ENTER;
	ipr_cmd->job_step = ipr_ioa_bringdown_done;
	ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
	LEAVE;
	return IPR_RC_JOB_CONTINUE;
}

/**
 * ipr_reset_cancel_hcam_done - Check for outstanding commands
 * @ipr_cmd:	ipr command struct
 *
 * Description: Ensure nothing is outstanding to the IOA and
 *			proceed with IOA disconnect. Otherwise reset the IOA.
 *
 * Return value:
 * 	IPR_RC_JOB_RETURN / IPR_RC_JOB_CONTINUE
 **/
static int ipr_reset_cancel_hcam_done(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_cmnd *loop_cmd;
	struct ipr_hrr_queue *hrrq;
	int rc = IPR_RC_JOB_CONTINUE;
	int count = 0;

	ENTER;
	ipr_cmd->job_step = ipr_reset_quiesce_done;

	for_each_hrrq(hrrq, ioa_cfg) {
		spin_lock(&hrrq->_lock);
		list_for_each_entry(loop_cmd, &hrrq->hrrq_pending_q, queue) {
			count++;
			ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
			list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
			rc = IPR_RC_JOB_RETURN;
			break;
		}
		spin_unlock(&hrrq->_lock);

		if (count)
			break;
	}

	LEAVE;
	return rc;
}

/**
 * ipr_reset_cancel_hcam - Cancel outstanding HCAMs
 * @ipr_cmd:	ipr command struct
 *
 * Description: Cancel any oustanding HCAMs to the IOA.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_reset_cancel_hcam(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	int rc = IPR_RC_JOB_CONTINUE;
	struct ipr_cmd_pkt *cmd_pkt;
	struct ipr_cmnd *hcam_cmd;
	struct ipr_hrr_queue *hrrq = &ioa_cfg->hrrq[IPR_INIT_HRRQ];

	ENTER;
	ipr_cmd->job_step = ipr_reset_cancel_hcam_done;

	if (!hrrq->ioa_is_dead) {
		if (!list_empty(&ioa_cfg->hostrcb_pending_q)) {
			list_for_each_entry(hcam_cmd, &hrrq->hrrq_pending_q, queue) {
				if (hcam_cmd->ioarcb.cmd_pkt.cdb[0] != IPR_HOST_CONTROLLED_ASYNC)
					continue;

				ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
				ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
				cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
				cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
				cmd_pkt->cdb[0] = IPR_CANCEL_REQUEST;
				cmd_pkt->cdb[1] = IPR_CANCEL_64BIT_IOARCB;
				cmd_pkt->cdb[10] = ((u64) hcam_cmd->dma_addr >> 56) & 0xff;
				cmd_pkt->cdb[11] = ((u64) hcam_cmd->dma_addr >> 48) & 0xff;
				cmd_pkt->cdb[12] = ((u64) hcam_cmd->dma_addr >> 40) & 0xff;
				cmd_pkt->cdb[13] = ((u64) hcam_cmd->dma_addr >> 32) & 0xff;
				cmd_pkt->cdb[2] = ((u64) hcam_cmd->dma_addr >> 24) & 0xff;
				cmd_pkt->cdb[3] = ((u64) hcam_cmd->dma_addr >> 16) & 0xff;
				cmd_pkt->cdb[4] = ((u64) hcam_cmd->dma_addr >> 8) & 0xff;
				cmd_pkt->cdb[5] = ((u64) hcam_cmd->dma_addr) & 0xff;

				ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
					   IPR_CANCEL_TIMEOUT);

				rc = IPR_RC_JOB_RETURN;
				ipr_cmd->job_step = ipr_reset_cancel_hcam;
				break;
			}
		}
	} else
		ipr_cmd->job_step = ipr_reset_alert;

	LEAVE;
	return rc;
}

L
Linus Torvalds 已提交
8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729
/**
 * ipr_reset_ucode_download_done - Microcode download completion
 * @ipr_cmd:	ipr command struct
 *
 * Description: This function unmaps the microcode download buffer.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE
 **/
static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;

A
Anton Blanchard 已提交
8730
	dma_unmap_sg(&ioa_cfg->pdev->dev, sglist->scatterlist,
L
Linus Torvalds 已提交
8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765
		     sglist->num_sg, DMA_TO_DEVICE);

	ipr_cmd->job_step = ipr_reset_alert;
	return IPR_RC_JOB_CONTINUE;
}

/**
 * ipr_reset_ucode_download - Download microcode to the adapter
 * @ipr_cmd:	ipr command struct
 *
 * Description: This function checks to see if it there is microcode
 * to download to the adapter. If there is, a download is performed.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;

	ENTER;
	ipr_cmd->job_step = ipr_reset_alert;

	if (!sglist)
		return IPR_RC_JOB_CONTINUE;

	ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
	ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
	ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
	ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
	ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
	ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
	ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;

8766 8767 8768 8769
	if (ioa_cfg->sis64)
		ipr_build_ucode_ioadl64(ipr_cmd, sglist);
	else
		ipr_build_ucode_ioadl(ipr_cmd, sglist);
L
Linus Torvalds 已提交
8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797
	ipr_cmd->job_step = ipr_reset_ucode_download_done;

	ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
		   IPR_WRITE_BUFFER_TIMEOUT);

	LEAVE;
	return IPR_RC_JOB_RETURN;
}

/**
 * ipr_reset_shutdown_ioa - Shutdown the adapter
 * @ipr_cmd:	ipr command struct
 *
 * Description: This function issues an adapter shutdown of the
 * specified type to the specified adapter as part of the
 * adapter reset job.
 *
 * Return value:
 * 	IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
 **/
static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
{
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
	unsigned long timeout;
	int rc = IPR_RC_JOB_CONTINUE;

	ENTER;
B
Brian King 已提交
8798 8799 8800
	if (shutdown_type == IPR_SHUTDOWN_QUIESCE)
		ipr_cmd->job_step = ipr_reset_cancel_hcam;
	else if (shutdown_type != IPR_SHUTDOWN_NONE &&
8801
			!ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead) {
L
Linus Torvalds 已提交
8802 8803 8804 8805 8806
		ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
		ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
		ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
		ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;

8807 8808
		if (shutdown_type == IPR_SHUTDOWN_NORMAL)
			timeout = IPR_SHUTDOWN_TIMEOUT;
L
Linus Torvalds 已提交
8809 8810
		else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
			timeout = IPR_INTERNAL_TIMEOUT;
8811 8812
		else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
			timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
L
Linus Torvalds 已提交
8813
		else
8814
			timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
L
Linus Torvalds 已提交
8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841

		ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);

		rc = IPR_RC_JOB_RETURN;
		ipr_cmd->job_step = ipr_reset_ucode_download;
	} else
		ipr_cmd->job_step = ipr_reset_alert;

	LEAVE;
	return rc;
}

/**
 * ipr_reset_ioa_job - Adapter reset job
 * @ipr_cmd:	ipr command struct
 *
 * Description: This function is the job router for the adapter reset job.
 *
 * Return value:
 * 	none
 **/
static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
{
	u32 rc, ioasc;
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;

	do {
8842
		ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
L
Linus Torvalds 已提交
8843 8844 8845 8846 8847 8848

		if (ioa_cfg->reset_cmd != ipr_cmd) {
			/*
			 * We are doing nested adapter resets and this is
			 * not the current reset job.
			 */
8849 8850
			list_add_tail(&ipr_cmd->queue,
					&ipr_cmd->hrrq->hrrq_free_q);
L
Linus Torvalds 已提交
8851 8852 8853 8854
			return;
		}

		if (IPR_IOASC_SENSE_KEY(ioasc)) {
8855 8856 8857
			rc = ipr_cmd->job_step_failed(ipr_cmd);
			if (rc == IPR_RC_JOB_RETURN)
				return;
L
Linus Torvalds 已提交
8858 8859 8860
		}

		ipr_reinit_ipr_cmnd(ipr_cmd);
8861
		ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
L
Linus Torvalds 已提交
8862
		rc = ipr_cmd->job_step(ipr_cmd);
8863
	} while (rc == IPR_RC_JOB_CONTINUE);
L
Linus Torvalds 已提交
8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884
}

/**
 * _ipr_initiate_ioa_reset - Initiate an adapter reset
 * @ioa_cfg:		ioa config struct
 * @job_step:		first job step of reset job
 * @shutdown_type:	shutdown type
 *
 * Description: This function will initiate the reset of the given adapter
 * starting at the selected job step.
 * If the caller needs to wait on the completion of the reset,
 * the caller must sleep on the reset_wait_q.
 *
 * Return value:
 * 	none
 **/
static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
				    int (*job_step) (struct ipr_cmnd *),
				    enum ipr_shutdown_type shutdown_type)
{
	struct ipr_cmnd *ipr_cmd;
8885
	int i;
L
Linus Torvalds 已提交
8886 8887

	ioa_cfg->in_reset_reload = 1;
8888 8889 8890 8891 8892 8893
	for (i = 0; i < ioa_cfg->hrrq_num; i++) {
		spin_lock(&ioa_cfg->hrrq[i]._lock);
		ioa_cfg->hrrq[i].allow_cmds = 0;
		spin_unlock(&ioa_cfg->hrrq[i]._lock);
	}
	wmb();
8894 8895
	if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa)
		scsi_block_requests(ioa_cfg->host);
L
Linus Torvalds 已提交
8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919

	ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
	ioa_cfg->reset_cmd = ipr_cmd;
	ipr_cmd->job_step = job_step;
	ipr_cmd->u.shutdown_type = shutdown_type;

	ipr_reset_ioa_job(ipr_cmd);
}

/**
 * ipr_initiate_ioa_reset - Initiate an adapter reset
 * @ioa_cfg:		ioa config struct
 * @shutdown_type:	shutdown type
 *
 * Description: This function will initiate the reset of the given adapter.
 * If the caller needs to wait on the completion of the reset,
 * the caller must sleep on the reset_wait_q.
 *
 * Return value:
 * 	none
 **/
static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
				   enum ipr_shutdown_type shutdown_type)
{
8920 8921 8922
	int i;

	if (ioa_cfg->hrrq[IPR_INIT_HRRQ].ioa_is_dead)
L
Linus Torvalds 已提交
8923 8924
		return;

8925 8926 8927 8928 8929 8930
	if (ioa_cfg->in_reset_reload) {
		if (ioa_cfg->sdt_state == GET_DUMP)
			ioa_cfg->sdt_state = WAIT_FOR_DUMP;
		else if (ioa_cfg->sdt_state == READ_DUMP)
			ioa_cfg->sdt_state = ABORT_DUMP;
	}
L
Linus Torvalds 已提交
8931 8932 8933 8934 8935 8936

	if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
		dev_err(&ioa_cfg->pdev->dev,
			"IOA taken offline - error recovery failed\n");

		ioa_cfg->reset_retries = 0;
8937 8938 8939 8940 8941 8942
		for (i = 0; i < ioa_cfg->hrrq_num; i++) {
			spin_lock(&ioa_cfg->hrrq[i]._lock);
			ioa_cfg->hrrq[i].ioa_is_dead = 1;
			spin_unlock(&ioa_cfg->hrrq[i]._lock);
		}
		wmb();
L
Linus Torvalds 已提交
8943 8944 8945 8946 8947 8948 8949

		if (ioa_cfg->in_ioa_bringdown) {
			ioa_cfg->reset_cmd = NULL;
			ioa_cfg->in_reset_reload = 0;
			ipr_fail_all_ops(ioa_cfg);
			wake_up_all(&ioa_cfg->reset_wait_q);

8950 8951 8952 8953 8954
			if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].removing_ioa) {
				spin_unlock_irq(ioa_cfg->host->host_lock);
				scsi_unblock_requests(ioa_cfg->host);
				spin_lock_irq(ioa_cfg->host->host_lock);
			}
L
Linus Torvalds 已提交
8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965
			return;
		} else {
			ioa_cfg->in_ioa_bringdown = 1;
			shutdown_type = IPR_SHUTDOWN_NONE;
		}
	}

	_ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
				shutdown_type);
}

8966 8967 8968 8969 8970 8971 8972 8973 8974 8975
/**
 * ipr_reset_freeze - Hold off all I/O activity
 * @ipr_cmd:	ipr command struct
 *
 * Description: If the PCI slot is frozen, hold off all I/O
 * activity; then, as soon as the slot is available again,
 * initiate an adapter reset.
 */
static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
{
8976 8977 8978
	struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
	int i;

8979
	/* Disallow new interrupts, avoid loop */
8980 8981 8982 8983 8984 8985
	for (i = 0; i < ioa_cfg->hrrq_num; i++) {
		spin_lock(&ioa_cfg->hrrq[i]._lock);
		ioa_cfg->hrrq[i].allow_interrupts = 0;
		spin_unlock(&ioa_cfg->hrrq[i]._lock);
	}
	wmb();
8986
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_pending_q);
8987 8988 8989 8990
	ipr_cmd->done = ipr_reset_ioa_job;
	return IPR_RC_JOB_RETURN;
}

B
Brian King 已提交
8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009
/**
 * ipr_pci_mmio_enabled - Called when MMIO has been re-enabled
 * @pdev:	PCI device struct
 *
 * Description: This routine is called to tell us that the MMIO
 * access to the IOA has been restored
 */
static pci_ers_result_t ipr_pci_mmio_enabled(struct pci_dev *pdev)
{
	unsigned long flags = 0;
	struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);

	spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
	if (!ioa_cfg->probe_done)
		pci_save_state(pdev);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
	return PCI_ERS_RESULT_NEED_RESET;
}

9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023
/**
 * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
 * @pdev:	PCI device struct
 *
 * Description: This routine is called to tell us that the PCI bus
 * is down. Can't do anything here, except put the device driver
 * into a holding pattern, waiting for the PCI bus to come back.
 */
static void ipr_pci_frozen(struct pci_dev *pdev)
{
	unsigned long flags = 0;
	struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);

	spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
B
Brian King 已提交
9024 9025
	if (ioa_cfg->probe_done)
		_ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
}

/**
 * ipr_pci_slot_reset - Called when PCI slot has been reset.
 * @pdev:	PCI device struct
 *
 * Description: This routine is called by the pci error recovery
 * code after the PCI slot has been reset, just before we
 * should resume normal operations.
 */
static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
{
	unsigned long flags = 0;
	struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);

	spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
B
Brian King 已提交
9043 9044 9045 9046 9047 9048 9049 9050
	if (ioa_cfg->probe_done) {
		if (ioa_cfg->needs_warm_reset)
			ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
		else
			_ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
						IPR_SHUTDOWN_NONE);
	} else
		wake_up_all(&ioa_cfg->eeh_wait_q);
9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
	return PCI_ERS_RESULT_RECOVERED;
}

/**
 * ipr_pci_perm_failure - Called when PCI slot is dead for good.
 * @pdev:	PCI device struct
 *
 * Description: This routine is called when the PCI bus has
 * permanently failed.
 */
static void ipr_pci_perm_failure(struct pci_dev *pdev)
{
	unsigned long flags = 0;
	struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
9066
	int i;
9067 9068

	spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
B
Brian King 已提交
9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082
	if (ioa_cfg->probe_done) {
		if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
			ioa_cfg->sdt_state = ABORT_DUMP;
		ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES - 1;
		ioa_cfg->in_ioa_bringdown = 1;
		for (i = 0; i < ioa_cfg->hrrq_num; i++) {
			spin_lock(&ioa_cfg->hrrq[i]._lock);
			ioa_cfg->hrrq[i].allow_cmds = 0;
			spin_unlock(&ioa_cfg->hrrq[i]._lock);
		}
		wmb();
		ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
	} else
		wake_up_all(&ioa_cfg->eeh_wait_q);
9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
}

/**
 * ipr_pci_error_detected - Called when a PCI error is detected.
 * @pdev:	PCI device struct
 * @state:	PCI channel state
 *
 * Description: Called when a PCI error is detected.
 *
 * Return value:
 * 	PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
 */
static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
					       pci_channel_state_t state)
{
	switch (state) {
	case pci_channel_io_frozen:
		ipr_pci_frozen(pdev);
B
Brian King 已提交
9102
		return PCI_ERS_RESULT_CAN_RECOVER;
9103 9104 9105 9106 9107 9108 9109 9110 9111 9112
	case pci_channel_io_perm_failure:
		ipr_pci_perm_failure(pdev);
		return PCI_ERS_RESULT_DISCONNECT;
		break;
	default:
		break;
	}
	return PCI_ERS_RESULT_NEED_RESET;
}

L
Linus Torvalds 已提交
9113 9114 9115 9116 9117 9118 9119 9120 9121
/**
 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
 * @ioa_cfg:	ioa cfg struct
 *
 * Description: This is the second phase of adapter intialization
 * This function takes care of initilizing the adapter to the point
 * where it can accept new commands.

 * Return value:
J
Joe Perches 已提交
9122
 * 	0 on success / -EIO on failure
L
Linus Torvalds 已提交
9123
 **/
9124
static int ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
L
Linus Torvalds 已提交
9125 9126 9127 9128 9129 9130 9131
{
	int rc = 0;
	unsigned long host_lock_flags = 0;

	ENTER;
	spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
	dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
B
Brian King 已提交
9132
	ioa_cfg->probe_done = 1;
9133 9134 9135 9136 9137 9138
	if (ioa_cfg->needs_hard_reset) {
		ioa_cfg->needs_hard_reset = 0;
		ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
	} else
		_ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
					IPR_SHUTDOWN_NONE);
L
Linus Torvalds 已提交
9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);

	LEAVE;
	return rc;
}

/**
 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 * 	none
 **/
static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
{
	int i;

9156 9157 9158 9159 9160 9161
	if (ioa_cfg->ipr_cmnd_list) {
		for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
			if (ioa_cfg->ipr_cmnd_list[i])
				dma_pool_free(ioa_cfg->ipr_cmd_pool,
					      ioa_cfg->ipr_cmnd_list[i],
					      ioa_cfg->ipr_cmnd_list_dma[i]);
L
Linus Torvalds 已提交
9162

9163 9164
			ioa_cfg->ipr_cmnd_list[i] = NULL;
		}
L
Linus Torvalds 已提交
9165 9166 9167
	}

	if (ioa_cfg->ipr_cmd_pool)
A
Anton Blanchard 已提交
9168
		dma_pool_destroy(ioa_cfg->ipr_cmd_pool);
L
Linus Torvalds 已提交
9169

9170 9171 9172 9173
	kfree(ioa_cfg->ipr_cmnd_list);
	kfree(ioa_cfg->ipr_cmnd_list_dma);
	ioa_cfg->ipr_cmnd_list = NULL;
	ioa_cfg->ipr_cmnd_list_dma = NULL;
L
Linus Torvalds 已提交
9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188
	ioa_cfg->ipr_cmd_pool = NULL;
}

/**
 * ipr_free_mem - Frees memory allocated for an adapter
 * @ioa_cfg:	ioa cfg struct
 *
 * Return value:
 * 	nothing
 **/
static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
{
	int i;

	kfree(ioa_cfg->res_entries);
A
Anton Blanchard 已提交
9189 9190
	dma_free_coherent(&ioa_cfg->pdev->dev, sizeof(struct ipr_misc_cbs),
			  ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
L
Linus Torvalds 已提交
9191
	ipr_free_cmd_blks(ioa_cfg);
9192 9193

	for (i = 0; i < ioa_cfg->hrrq_num; i++)
A
Anton Blanchard 已提交
9194 9195 9196 9197
		dma_free_coherent(&ioa_cfg->pdev->dev,
				  sizeof(u32) * ioa_cfg->hrrq[i].size,
				  ioa_cfg->hrrq[i].host_rrq,
				  ioa_cfg->hrrq[i].host_rrq_dma);
9198

A
Anton Blanchard 已提交
9199 9200
	dma_free_coherent(&ioa_cfg->pdev->dev, ioa_cfg->cfg_table_size,
			  ioa_cfg->u.cfg_table, ioa_cfg->cfg_table_dma);
L
Linus Torvalds 已提交
9201 9202

	for (i = 0; i < IPR_NUM_HCAMS; i++) {
A
Anton Blanchard 已提交
9203 9204 9205 9206
		dma_free_coherent(&ioa_cfg->pdev->dev,
				  sizeof(struct ipr_hostrcb),
				  ioa_cfg->hostrcb[i],
				  ioa_cfg->hostrcb_dma[i]);
L
Linus Torvalds 已提交
9207 9208 9209 9210 9211 9212 9213
	}

	ipr_free_dump(ioa_cfg);
	kfree(ioa_cfg->trace);
}

/**
B
Brian King 已提交
9214 9215
 * ipr_free_irqs - Free all allocated IRQs for the adapter.
 * @ioa_cfg:	ipr cfg struct
L
Linus Torvalds 已提交
9216
 *
B
Brian King 已提交
9217
 * This function frees all allocated IRQs for the
L
Linus Torvalds 已提交
9218 9219 9220 9221 9222
 * specified adapter.
 *
 * Return value:
 * 	none
 **/
B
Brian King 已提交
9223
static void ipr_free_irqs(struct ipr_ioa_cfg *ioa_cfg)
L
Linus Torvalds 已提交
9224 9225 9226
{
	struct pci_dev *pdev = ioa_cfg->pdev;

9227 9228 9229 9230 9231
	if (ioa_cfg->intr_flag == IPR_USE_MSI ||
	    ioa_cfg->intr_flag == IPR_USE_MSIX) {
		int i;
		for (i = 0; i < ioa_cfg->nvectors; i++)
			free_irq(ioa_cfg->vectors_info[i].vec,
B
Brian King 已提交
9232
				 &ioa_cfg->hrrq[i]);
9233 9234 9235
	} else
		free_irq(pdev->irq, &ioa_cfg->hrrq[0]);

9236
	if (ioa_cfg->intr_flag == IPR_USE_MSI) {
9237
		pci_disable_msi(pdev);
9238 9239
		ioa_cfg->intr_flag &= ~IPR_USE_MSI;
	} else if (ioa_cfg->intr_flag == IPR_USE_MSIX) {
9240
		pci_disable_msix(pdev);
9241 9242
		ioa_cfg->intr_flag &= ~IPR_USE_MSIX;
	}
B
Brian King 已提交
9243
}
9244

B
Brian King 已提交
9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257
/**
 * ipr_free_all_resources - Free all allocated resources for an adapter.
 * @ipr_cmd:	ipr command struct
 *
 * This function frees all allocated resources for the
 * specified adapter.
 *
 * Return value:
 * 	none
 **/
static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
{
	struct pci_dev *pdev = ioa_cfg->pdev;
9258

B
Brian King 已提交
9259 9260 9261 9262
	ENTER;
	ipr_free_irqs(ioa_cfg);
	if (ioa_cfg->reset_work_q)
		destroy_workqueue(ioa_cfg->reset_work_q);
L
Linus Torvalds 已提交
9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277
	iounmap(ioa_cfg->hdw_dma_regs);
	pci_release_regions(pdev);
	ipr_free_mem(ioa_cfg);
	scsi_host_put(ioa_cfg->host);
	pci_disable_device(pdev);
	LEAVE;
}

/**
 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 * 	0 on success / -ENOMEM on allocation failure
 **/
9278
static int ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
L
Linus Torvalds 已提交
9279 9280 9281 9282
{
	struct ipr_cmnd *ipr_cmd;
	struct ipr_ioarcb *ioarcb;
	dma_addr_t dma_addr;
9283
	int i, entries_each_hrrq, hrrq_id = 0;
L
Linus Torvalds 已提交
9284

A
Anton Blanchard 已提交
9285
	ioa_cfg->ipr_cmd_pool = dma_pool_create(IPR_NAME, &ioa_cfg->pdev->dev,
9286
						sizeof(struct ipr_cmnd), 512, 0);
L
Linus Torvalds 已提交
9287 9288 9289 9290

	if (!ioa_cfg->ipr_cmd_pool)
		return -ENOMEM;

9291 9292 9293 9294 9295 9296 9297 9298
	ioa_cfg->ipr_cmnd_list = kcalloc(IPR_NUM_CMD_BLKS, sizeof(struct ipr_cmnd *), GFP_KERNEL);
	ioa_cfg->ipr_cmnd_list_dma = kcalloc(IPR_NUM_CMD_BLKS, sizeof(dma_addr_t), GFP_KERNEL);

	if (!ioa_cfg->ipr_cmnd_list || !ioa_cfg->ipr_cmnd_list_dma) {
		ipr_free_cmd_blks(ioa_cfg);
		return -ENOMEM;
	}

9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333
	for (i = 0; i < ioa_cfg->hrrq_num; i++) {
		if (ioa_cfg->hrrq_num > 1) {
			if (i == 0) {
				entries_each_hrrq = IPR_NUM_INTERNAL_CMD_BLKS;
				ioa_cfg->hrrq[i].min_cmd_id = 0;
					ioa_cfg->hrrq[i].max_cmd_id =
						(entries_each_hrrq - 1);
			} else {
				entries_each_hrrq =
					IPR_NUM_BASE_CMD_BLKS/
					(ioa_cfg->hrrq_num - 1);
				ioa_cfg->hrrq[i].min_cmd_id =
					IPR_NUM_INTERNAL_CMD_BLKS +
					(i - 1) * entries_each_hrrq;
				ioa_cfg->hrrq[i].max_cmd_id =
					(IPR_NUM_INTERNAL_CMD_BLKS +
					i * entries_each_hrrq - 1);
			}
		} else {
			entries_each_hrrq = IPR_NUM_CMD_BLKS;
			ioa_cfg->hrrq[i].min_cmd_id = 0;
			ioa_cfg->hrrq[i].max_cmd_id = (entries_each_hrrq - 1);
		}
		ioa_cfg->hrrq[i].size = entries_each_hrrq;
	}

	BUG_ON(ioa_cfg->hrrq_num == 0);

	i = IPR_NUM_CMD_BLKS -
		ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].max_cmd_id - 1;
	if (i > 0) {
		ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].size += i;
		ioa_cfg->hrrq[ioa_cfg->hrrq_num - 1].max_cmd_id += i;
	}

L
Linus Torvalds 已提交
9334
	for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
A
Anton Blanchard 已提交
9335
		ipr_cmd = dma_pool_alloc(ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
L
Linus Torvalds 已提交
9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346

		if (!ipr_cmd) {
			ipr_free_cmd_blks(ioa_cfg);
			return -ENOMEM;
		}

		memset(ipr_cmd, 0, sizeof(*ipr_cmd));
		ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
		ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;

		ioarcb = &ipr_cmd->ioarcb;
9347 9348 9349 9350 9351 9352
		ipr_cmd->dma_addr = dma_addr;
		if (ioa_cfg->sis64)
			ioarcb->a.ioarcb_host_pci_addr64 = cpu_to_be64(dma_addr);
		else
			ioarcb->a.ioarcb_host_pci_addr = cpu_to_be32(dma_addr);

L
Linus Torvalds 已提交
9353
		ioarcb->host_response_handle = cpu_to_be32(i << 2);
9354 9355 9356 9357
		if (ioa_cfg->sis64) {
			ioarcb->u.sis64_addr_data.data_ioadl_addr =
				cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
			ioarcb->u.sis64_addr_data.ioasa_host_pci_addr =
9358
				cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, s.ioasa64));
9359 9360 9361 9362 9363
		} else {
			ioarcb->write_ioadl_addr =
				cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
			ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
			ioarcb->ioasa_host_pci_addr =
9364
				cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, s.ioasa));
9365
		}
L
Linus Torvalds 已提交
9366 9367 9368 9369 9370 9371
		ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
		ipr_cmd->cmd_index = i;
		ipr_cmd->ioa_cfg = ioa_cfg;
		ipr_cmd->sense_buffer_dma = dma_addr +
			offsetof(struct ipr_cmnd, sense_buffer);

9372 9373 9374 9375 9376
		ipr_cmd->ioarcb.cmd_pkt.hrrq_id = hrrq_id;
		ipr_cmd->hrrq = &ioa_cfg->hrrq[hrrq_id];
		list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
		if (i >= ioa_cfg->hrrq[hrrq_id].max_cmd_id)
			hrrq_id++;
L
Linus Torvalds 已提交
9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388
	}

	return 0;
}

/**
 * ipr_alloc_mem - Allocate memory for an adapter
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 * 	0 on success / non-zero for error
 **/
9389
static int ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
L
Linus Torvalds 已提交
9390 9391 9392 9393 9394
{
	struct pci_dev *pdev = ioa_cfg->pdev;
	int i, rc = -ENOMEM;

	ENTER;
9395
	ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
9396
				       ioa_cfg->max_devs_supported, GFP_KERNEL);
L
Linus Torvalds 已提交
9397 9398 9399 9400

	if (!ioa_cfg->res_entries)
		goto out;

9401
	for (i = 0; i < ioa_cfg->max_devs_supported; i++) {
L
Linus Torvalds 已提交
9402
		list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
9403 9404
		ioa_cfg->res_entries[i].ioa_cfg = ioa_cfg;
	}
L
Linus Torvalds 已提交
9405

A
Anton Blanchard 已提交
9406 9407 9408 9409
	ioa_cfg->vpd_cbs = dma_alloc_coherent(&pdev->dev,
					      sizeof(struct ipr_misc_cbs),
					      &ioa_cfg->vpd_cbs_dma,
					      GFP_KERNEL);
L
Linus Torvalds 已提交
9410 9411 9412 9413 9414 9415 9416

	if (!ioa_cfg->vpd_cbs)
		goto out_free_res_entries;

	if (ipr_alloc_cmd_blks(ioa_cfg))
		goto out_free_vpd_cbs;

9417
	for (i = 0; i < ioa_cfg->hrrq_num; i++) {
A
Anton Blanchard 已提交
9418
		ioa_cfg->hrrq[i].host_rrq = dma_alloc_coherent(&pdev->dev,
9419
					sizeof(u32) * ioa_cfg->hrrq[i].size,
A
Anton Blanchard 已提交
9420 9421
					&ioa_cfg->hrrq[i].host_rrq_dma,
					GFP_KERNEL);
9422 9423 9424

		if (!ioa_cfg->hrrq[i].host_rrq)  {
			while (--i > 0)
A
Anton Blanchard 已提交
9425
				dma_free_coherent(&pdev->dev,
9426 9427 9428 9429 9430 9431 9432
					sizeof(u32) * ioa_cfg->hrrq[i].size,
					ioa_cfg->hrrq[i].host_rrq,
					ioa_cfg->hrrq[i].host_rrq_dma);
			goto out_ipr_free_cmd_blocks;
		}
		ioa_cfg->hrrq[i].ioa_cfg = ioa_cfg;
	}
L
Linus Torvalds 已提交
9433

A
Anton Blanchard 已提交
9434 9435 9436 9437
	ioa_cfg->u.cfg_table = dma_alloc_coherent(&pdev->dev,
						  ioa_cfg->cfg_table_size,
						  &ioa_cfg->cfg_table_dma,
						  GFP_KERNEL);
L
Linus Torvalds 已提交
9438

9439
	if (!ioa_cfg->u.cfg_table)
L
Linus Torvalds 已提交
9440 9441 9442
		goto out_free_host_rrq;

	for (i = 0; i < IPR_NUM_HCAMS; i++) {
A
Anton Blanchard 已提交
9443 9444 9445 9446
		ioa_cfg->hostrcb[i] = dma_alloc_coherent(&pdev->dev,
							 sizeof(struct ipr_hostrcb),
							 &ioa_cfg->hostrcb_dma[i],
							 GFP_KERNEL);
L
Linus Torvalds 已提交
9447 9448 9449 9450 9451 9452

		if (!ioa_cfg->hostrcb[i])
			goto out_free_hostrcb_dma;

		ioa_cfg->hostrcb[i]->hostrcb_dma =
			ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
9453
		ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
L
Linus Torvalds 已提交
9454 9455 9456
		list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
	}

9457
	ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
L
Linus Torvalds 已提交
9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469
				 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);

	if (!ioa_cfg->trace)
		goto out_free_hostrcb_dma;

	rc = 0;
out:
	LEAVE;
	return rc;

out_free_hostrcb_dma:
	while (i-- > 0) {
A
Anton Blanchard 已提交
9470 9471 9472
		dma_free_coherent(&pdev->dev, sizeof(struct ipr_hostrcb),
				  ioa_cfg->hostrcb[i],
				  ioa_cfg->hostrcb_dma[i]);
L
Linus Torvalds 已提交
9473
	}
A
Anton Blanchard 已提交
9474 9475
	dma_free_coherent(&pdev->dev, ioa_cfg->cfg_table_size,
			  ioa_cfg->u.cfg_table, ioa_cfg->cfg_table_dma);
L
Linus Torvalds 已提交
9476
out_free_host_rrq:
9477
	for (i = 0; i < ioa_cfg->hrrq_num; i++) {
A
Anton Blanchard 已提交
9478 9479 9480 9481
		dma_free_coherent(&pdev->dev,
				  sizeof(u32) * ioa_cfg->hrrq[i].size,
				  ioa_cfg->hrrq[i].host_rrq,
				  ioa_cfg->hrrq[i].host_rrq_dma);
9482
	}
L
Linus Torvalds 已提交
9483 9484 9485
out_ipr_free_cmd_blocks:
	ipr_free_cmd_blks(ioa_cfg);
out_free_vpd_cbs:
A
Anton Blanchard 已提交
9486 9487
	dma_free_coherent(&pdev->dev, sizeof(struct ipr_misc_cbs),
			  ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
L
Linus Torvalds 已提交
9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499
out_free_res_entries:
	kfree(ioa_cfg->res_entries);
	goto out;
}

/**
 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 * 	none
 **/
9500
static void ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
L
Linus Torvalds 已提交
9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514
{
	int i;

	for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
		ioa_cfg->bus_attr[i].bus = i;
		ioa_cfg->bus_attr[i].qas_enabled = 0;
		ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
		if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
			ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
		else
			ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
	}
}

B
Brian King 已提交
9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556
/**
 * ipr_init_regs - Initialize IOA registers
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 *	none
 **/
static void ipr_init_regs(struct ipr_ioa_cfg *ioa_cfg)
{
	const struct ipr_interrupt_offsets *p;
	struct ipr_interrupts *t;
	void __iomem *base;

	p = &ioa_cfg->chip_cfg->regs;
	t = &ioa_cfg->regs;
	base = ioa_cfg->hdw_dma_regs;

	t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
	t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
	t->clr_interrupt_mask_reg32 = base + p->clr_interrupt_mask_reg32;
	t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
	t->sense_interrupt_mask_reg32 = base + p->sense_interrupt_mask_reg32;
	t->clr_interrupt_reg = base + p->clr_interrupt_reg;
	t->clr_interrupt_reg32 = base + p->clr_interrupt_reg32;
	t->sense_interrupt_reg = base + p->sense_interrupt_reg;
	t->sense_interrupt_reg32 = base + p->sense_interrupt_reg32;
	t->ioarrin_reg = base + p->ioarrin_reg;
	t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
	t->sense_uproc_interrupt_reg32 = base + p->sense_uproc_interrupt_reg32;
	t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
	t->set_uproc_interrupt_reg32 = base + p->set_uproc_interrupt_reg32;
	t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
	t->clr_uproc_interrupt_reg32 = base + p->clr_uproc_interrupt_reg32;

	if (ioa_cfg->sis64) {
		t->init_feedback_reg = base + p->init_feedback_reg;
		t->dump_addr_reg = base + p->dump_addr_reg;
		t->dump_data_reg = base + p->dump_data_reg;
		t->endian_swap_reg = base + p->endian_swap_reg;
	}
}

L
Linus Torvalds 已提交
9557 9558 9559 9560 9561 9562 9563 9564 9565
/**
 * ipr_init_ioa_cfg - Initialize IOA config struct
 * @ioa_cfg:	ioa config struct
 * @host:		scsi host struct
 * @pdev:		PCI dev struct
 *
 * Return value:
 * 	none
 **/
9566 9567
static void ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
			     struct Scsi_Host *host, struct pci_dev *pdev)
L
Linus Torvalds 已提交
9568
{
B
Brian King 已提交
9569
	int i;
L
Linus Torvalds 已提交
9570 9571 9572 9573

	ioa_cfg->host = host;
	ioa_cfg->pdev = pdev;
	ioa_cfg->log_level = ipr_log_level;
B
brking@us.ibm.com 已提交
9574
	ioa_cfg->doorbell = IPR_DOORBELL;
L
Linus Torvalds 已提交
9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585
	sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
	sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
	sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
	sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
	sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
	sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);

	INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
	INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
	INIT_LIST_HEAD(&ioa_cfg->free_res_q);
	INIT_LIST_HEAD(&ioa_cfg->used_res_q);
D
David Howells 已提交
9586
	INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
L
Linus Torvalds 已提交
9587
	init_waitqueue_head(&ioa_cfg->reset_wait_q);
9588
	init_waitqueue_head(&ioa_cfg->msi_wait_q);
B
Brian King 已提交
9589
	init_waitqueue_head(&ioa_cfg->eeh_wait_q);
L
Linus Torvalds 已提交
9590 9591 9592
	ioa_cfg->sdt_state = INACTIVE;

	ipr_initialize_bus_attr(ioa_cfg);
9593
	ioa_cfg->max_devs_supported = ipr_max_devs;
L
Linus Torvalds 已提交
9594

9595 9596 9597 9598 9599
	if (ioa_cfg->sis64) {
		host->max_id = IPR_MAX_SIS64_TARGETS_PER_BUS;
		host->max_lun = IPR_MAX_SIS64_LUNS_PER_TARGET;
		if (ipr_max_devs > IPR_MAX_SIS64_DEVS)
			ioa_cfg->max_devs_supported = IPR_MAX_SIS64_DEVS;
B
Brian King 已提交
9600 9601 9602
		ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr64)
					   + ((sizeof(struct ipr_config_table_entry64)
					       * ioa_cfg->max_devs_supported)));
9603 9604 9605 9606 9607
	} else {
		host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
		host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
		if (ipr_max_devs > IPR_MAX_PHYSICAL_DEVS)
			ioa_cfg->max_devs_supported = IPR_MAX_PHYSICAL_DEVS;
B
Brian King 已提交
9608 9609 9610
		ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr)
					   + ((sizeof(struct ipr_config_table_entry)
					       * ioa_cfg->max_devs_supported)));
9611
	}
B
Brian King 已提交
9612

9613
	host->max_channel = IPR_VSET_BUS;
L
Linus Torvalds 已提交
9614 9615
	host->unique_id = host->host_no;
	host->max_cmd_len = IPR_MAX_CDB_LEN;
9616
	host->can_queue = ioa_cfg->max_cmds;
L
Linus Torvalds 已提交
9617 9618
	pci_set_drvdata(pdev, ioa_cfg);

B
Brian King 已提交
9619 9620 9621 9622 9623 9624 9625 9626
	for (i = 0; i < ARRAY_SIZE(ioa_cfg->hrrq); i++) {
		INIT_LIST_HEAD(&ioa_cfg->hrrq[i].hrrq_free_q);
		INIT_LIST_HEAD(&ioa_cfg->hrrq[i].hrrq_pending_q);
		spin_lock_init(&ioa_cfg->hrrq[i]._lock);
		if (i == 0)
			ioa_cfg->hrrq[i].lock = ioa_cfg->host->host_lock;
		else
			ioa_cfg->hrrq[i].lock = &ioa_cfg->hrrq[i]._lock;
9627
	}
L
Linus Torvalds 已提交
9628 9629 9630
}

/**
9631
 * ipr_get_chip_info - Find adapter chip information
L
Linus Torvalds 已提交
9632 9633 9634
 * @dev_id:		PCI device id struct
 *
 * Return value:
9635
 * 	ptr to chip information on success / NULL on failure
L
Linus Torvalds 已提交
9636
 **/
9637
static const struct ipr_chip_t *
9638
ipr_get_chip_info(const struct pci_device_id *dev_id)
L
Linus Torvalds 已提交
9639 9640 9641 9642 9643 9644
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
		if (ipr_chip[i].vendor == dev_id->vendor &&
		    ipr_chip[i].device == dev_id->device)
9645
			return &ipr_chip[i];
L
Linus Torvalds 已提交
9646 9647 9648
	return NULL;
}

B
Brian King 已提交
9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668
/**
 * ipr_wait_for_pci_err_recovery - Wait for any PCI error recovery to complete
 *						during probe time
 * @ioa_cfg:	ioa config struct
 *
 * Return value:
 * 	None
 **/
static void ipr_wait_for_pci_err_recovery(struct ipr_ioa_cfg *ioa_cfg)
{
	struct pci_dev *pdev = ioa_cfg->pdev;

	if (pci_channel_offline(pdev)) {
		wait_event_timeout(ioa_cfg->eeh_wait_q,
				   !pci_channel_offline(pdev),
				   IPR_PCI_ERROR_RECOVERY_TIMEOUT);
		pci_restore_state(pdev);
	}
}

9669 9670 9671
static int ipr_enable_msix(struct ipr_ioa_cfg *ioa_cfg)
{
	struct msix_entry entries[IPR_MAX_MSIX_VECTORS];
9672
	int i, vectors;
9673 9674 9675 9676

	for (i = 0; i < ARRAY_SIZE(entries); ++i)
		entries[i].entry = i;

9677 9678 9679
	vectors = pci_enable_msix_range(ioa_cfg->pdev,
					entries, 1, ipr_number_of_msix);
	if (vectors < 0) {
B
Brian King 已提交
9680
		ipr_wait_for_pci_err_recovery(ioa_cfg);
9681
		return vectors;
9682 9683
	}

9684 9685 9686
	for (i = 0; i < vectors; i++)
		ioa_cfg->vectors_info[i].vec = entries[i].vector;
	ioa_cfg->nvectors = vectors;
9687

9688
	return 0;
9689 9690 9691 9692
}

static int ipr_enable_msi(struct ipr_ioa_cfg *ioa_cfg)
{
9693
	int i, vectors;
9694

9695 9696
	vectors = pci_enable_msi_range(ioa_cfg->pdev, 1, ipr_number_of_msix);
	if (vectors < 0) {
B
Brian King 已提交
9697
		ipr_wait_for_pci_err_recovery(ioa_cfg);
9698
		return vectors;
9699 9700
	}

9701 9702 9703
	for (i = 0; i < vectors; i++)
		ioa_cfg->vectors_info[i].vec = ioa_cfg->pdev->irq + i;
	ioa_cfg->nvectors = vectors;
9704

9705
	return 0;
9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739
}

static void name_msi_vectors(struct ipr_ioa_cfg *ioa_cfg)
{
	int vec_idx, n = sizeof(ioa_cfg->vectors_info[0].desc) - 1;

	for (vec_idx = 0; vec_idx < ioa_cfg->nvectors; vec_idx++) {
		snprintf(ioa_cfg->vectors_info[vec_idx].desc, n,
			 "host%d-%d", ioa_cfg->host->host_no, vec_idx);
		ioa_cfg->vectors_info[vec_idx].
			desc[strlen(ioa_cfg->vectors_info[vec_idx].desc)] = 0;
	}
}

static int ipr_request_other_msi_irqs(struct ipr_ioa_cfg *ioa_cfg)
{
	int i, rc;

	for (i = 1; i < ioa_cfg->nvectors; i++) {
		rc = request_irq(ioa_cfg->vectors_info[i].vec,
			ipr_isr_mhrrq,
			0,
			ioa_cfg->vectors_info[i].desc,
			&ioa_cfg->hrrq[i]);
		if (rc) {
			while (--i >= 0)
				free_irq(ioa_cfg->vectors_info[i].vec,
					&ioa_cfg->hrrq[i]);
			return rc;
		}
	}
	return 0;
}

9740 9741 9742 9743 9744 9745 9746 9747 9748 9749
/**
 * ipr_test_intr - Handle the interrupt generated in ipr_test_msi().
 * @pdev:		PCI device struct
 *
 * Description: Simply set the msi_received flag to 1 indicating that
 * Message Signaled Interrupts are supported.
 *
 * Return value:
 * 	0 on success / non-zero on failure
 **/
9750
static irqreturn_t ipr_test_intr(int irq, void *devp)
9751 9752 9753 9754 9755
{
	struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
	unsigned long lock_flags = 0;
	irqreturn_t rc = IRQ_HANDLED;

9756
	dev_info(&ioa_cfg->pdev->dev, "Received IRQ : %d\n", irq);
9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);

	ioa_cfg->msi_received = 1;
	wake_up(&ioa_cfg->msi_wait_q);

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	return rc;
}

/**
 * ipr_test_msi - Test for Message Signaled Interrupt (MSI) support.
 * @pdev:		PCI device struct
 *
9770
 * Description: The return value from pci_enable_msi_range() can not always be
9771 9772 9773 9774 9775 9776 9777
 * trusted.  This routine sets up and initiates a test interrupt to determine
 * if the interrupt is received via the ipr_test_intr() service routine.
 * If the tests fails, the driver will fall back to LSI.
 *
 * Return value:
 * 	0 on success / non-zero on failure
 **/
9778
static int ipr_test_msi(struct ipr_ioa_cfg *ioa_cfg, struct pci_dev *pdev)
9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789
{
	int rc;
	volatile u32 int_reg;
	unsigned long lock_flags = 0;

	ENTER;

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	init_waitqueue_head(&ioa_cfg->msi_wait_q);
	ioa_cfg->msi_received = 0;
	ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
9790
	writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.clr_interrupt_mask_reg32);
9791 9792 9793
	int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

9794 9795 9796 9797
	if (ioa_cfg->intr_flag == IPR_USE_MSIX)
		rc = request_irq(ioa_cfg->vectors_info[0].vec, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
	else
		rc = request_irq(pdev->irq, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
9798 9799 9800 9801 9802 9803
	if (rc) {
		dev_err(&pdev->dev, "Can not assign irq %d\n", pdev->irq);
		return rc;
	} else if (ipr_debug)
		dev_info(&pdev->dev, "IRQ assigned: %d\n", pdev->irq);

9804
	writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.sense_interrupt_reg32);
9805 9806
	int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
	wait_event_timeout(ioa_cfg->msi_wait_q, ioa_cfg->msi_received, HZ);
9807
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818
	ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);

	if (!ioa_cfg->msi_received) {
		/* MSI test failed */
		dev_info(&pdev->dev, "MSI test failed.  Falling back to LSI.\n");
		rc = -EOPNOTSUPP;
	} else if (ipr_debug)
		dev_info(&pdev->dev, "MSI test succeeded.\n");

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);

9819 9820 9821 9822
	if (ioa_cfg->intr_flag == IPR_USE_MSIX)
		free_irq(ioa_cfg->vectors_info[0].vec, ioa_cfg);
	else
		free_irq(pdev->irq, ioa_cfg);
9823 9824 9825 9826 9827 9828

	LEAVE;

	return rc;
}

9829
 /* ipr_probe_ioa - Allocates memory and does first stage of initialization
L
Linus Torvalds 已提交
9830 9831 9832 9833 9834 9835
 * @pdev:		PCI device struct
 * @dev_id:		PCI device id struct
 *
 * Return value:
 * 	0 on success / non-zero on failure
 **/
9836 9837
static int ipr_probe_ioa(struct pci_dev *pdev,
			 const struct pci_device_id *dev_id)
L
Linus Torvalds 已提交
9838 9839 9840 9841 9842
{
	struct ipr_ioa_cfg *ioa_cfg;
	struct Scsi_Host *host;
	unsigned long ipr_regs_pci;
	void __iomem *ipr_regs;
9843
	int rc = PCIBIOS_SUCCESSFUL;
9844
	volatile u32 mask, uproc, interrupts;
9845
	unsigned long lock_flags, driver_lock_flags;
L
Linus Torvalds 已提交
9846 9847 9848 9849 9850 9851 9852 9853 9854

	ENTER;

	dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
	host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));

	if (!host) {
		dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
		rc = -ENOMEM;
B
Brian King 已提交
9855
		goto out;
L
Linus Torvalds 已提交
9856 9857 9858 9859
	}

	ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
	memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
9860
	ata_host_init(&ioa_cfg->ata_host, &pdev->dev, &ipr_sata_ops);
L
Linus Torvalds 已提交
9861

9862
	ioa_cfg->ipr_chip = ipr_get_chip_info(dev_id);
L
Linus Torvalds 已提交
9863

9864
	if (!ioa_cfg->ipr_chip) {
L
Linus Torvalds 已提交
9865 9866 9867 9868 9869
		dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
			dev_id->vendor, dev_id->device);
		goto out_scsi_host_put;
	}

9870 9871
	/* set SIS 32 or SIS 64 */
	ioa_cfg->sis64 = ioa_cfg->ipr_chip->sis_type == IPR_SIS64 ? 1 : 0;
9872
	ioa_cfg->chip_cfg = ioa_cfg->ipr_chip->cfg;
9873
	ioa_cfg->clear_isr = ioa_cfg->chip_cfg->clear_isr;
9874
	ioa_cfg->max_cmds = ioa_cfg->chip_cfg->max_cmds;
9875

9876 9877 9878 9879 9880 9881 9882
	if (ipr_transop_timeout)
		ioa_cfg->transop_timeout = ipr_transop_timeout;
	else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
		ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
	else
		ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;

9883
	ioa_cfg->revid = pdev->revision;
9884

B
Brian King 已提交
9885 9886
	ipr_init_ioa_cfg(ioa_cfg, host, pdev);

L
Linus Torvalds 已提交
9887 9888 9889 9890 9891 9892 9893 9894 9895
	ipr_regs_pci = pci_resource_start(pdev, 0);

	rc = pci_request_regions(pdev, IPR_NAME);
	if (rc < 0) {
		dev_err(&pdev->dev,
			"Couldn't register memory range of registers\n");
		goto out_scsi_host_put;
	}

B
Brian King 已提交
9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910
	rc = pci_enable_device(pdev);

	if (rc || pci_channel_offline(pdev)) {
		if (pci_channel_offline(pdev)) {
			ipr_wait_for_pci_err_recovery(ioa_cfg);
			rc = pci_enable_device(pdev);
		}

		if (rc) {
			dev_err(&pdev->dev, "Cannot enable adapter\n");
			ipr_wait_for_pci_err_recovery(ioa_cfg);
			goto out_release_regions;
		}
	}

9911
	ipr_regs = pci_ioremap_bar(pdev, 0);
L
Linus Torvalds 已提交
9912 9913 9914 9915 9916

	if (!ipr_regs) {
		dev_err(&pdev->dev,
			"Couldn't map memory range of registers\n");
		rc = -ENOMEM;
B
Brian King 已提交
9917
		goto out_disable;
L
Linus Torvalds 已提交
9918 9919 9920 9921 9922 9923
	}

	ioa_cfg->hdw_dma_regs = ipr_regs;
	ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
	ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;

B
Brian King 已提交
9924
	ipr_init_regs(ioa_cfg);
L
Linus Torvalds 已提交
9925

9926
	if (ioa_cfg->sis64) {
A
Anton Blanchard 已提交
9927
		rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
9928
		if (rc < 0) {
A
Anton Blanchard 已提交
9929 9930 9931
			dev_dbg(&pdev->dev, "Failed to set 64 bit DMA mask\n");
			rc = dma_set_mask_and_coherent(&pdev->dev,
						       DMA_BIT_MASK(32));
9932 9933
		}
	} else
A
Anton Blanchard 已提交
9934
		rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
9935

L
Linus Torvalds 已提交
9936
	if (rc < 0) {
A
Anton Blanchard 已提交
9937
		dev_err(&pdev->dev, "Failed to set DMA mask\n");
L
Linus Torvalds 已提交
9938 9939 9940 9941 9942 9943 9944 9945
		goto cleanup_nomem;
	}

	rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
				   ioa_cfg->chip_cfg->cache_line_size);

	if (rc != PCIBIOS_SUCCESSFUL) {
		dev_err(&pdev->dev, "Write of cache line size failed\n");
B
Brian King 已提交
9946
		ipr_wait_for_pci_err_recovery(ioa_cfg);
L
Linus Torvalds 已提交
9947 9948 9949 9950
		rc = -EIO;
		goto cleanup_nomem;
	}

B
Brian King 已提交
9951 9952 9953 9954
	/* Issue MMIO read to ensure card is not in EEH */
	interrupts = readl(ioa_cfg->regs.sense_interrupt_reg);
	ipr_wait_for_pci_err_recovery(ioa_cfg);

9955 9956 9957 9958 9959 9960 9961
	if (ipr_number_of_msix > IPR_MAX_MSIX_VECTORS) {
		dev_err(&pdev->dev, "The max number of MSIX is %d\n",
			IPR_MAX_MSIX_VECTORS);
		ipr_number_of_msix = IPR_MAX_MSIX_VECTORS;
	}

	if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI &&
9962
			ipr_enable_msix(ioa_cfg) == 0)
9963 9964
		ioa_cfg->intr_flag = IPR_USE_MSIX;
	else if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI &&
9965
			ipr_enable_msi(ioa_cfg) == 0)
9966 9967 9968 9969 9970 9971 9972
		ioa_cfg->intr_flag = IPR_USE_MSI;
	else {
		ioa_cfg->intr_flag = IPR_USE_LSI;
		ioa_cfg->nvectors = 1;
		dev_info(&pdev->dev, "Cannot enable MSI.\n");
	}

B
Brian King 已提交
9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983
	pci_set_master(pdev);

	if (pci_channel_offline(pdev)) {
		ipr_wait_for_pci_err_recovery(ioa_cfg);
		pci_set_master(pdev);
		if (pci_channel_offline(pdev)) {
			rc = -EIO;
			goto out_msi_disable;
		}
	}

9984 9985
	if (ioa_cfg->intr_flag == IPR_USE_MSI ||
	    ioa_cfg->intr_flag == IPR_USE_MSIX) {
9986
		rc = ipr_test_msi(ioa_cfg, pdev);
9987
		if (rc == -EOPNOTSUPP) {
B
Brian King 已提交
9988
			ipr_wait_for_pci_err_recovery(ioa_cfg);
9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999
			if (ioa_cfg->intr_flag == IPR_USE_MSI) {
				ioa_cfg->intr_flag &= ~IPR_USE_MSI;
				pci_disable_msi(pdev);
			 } else if (ioa_cfg->intr_flag == IPR_USE_MSIX) {
				ioa_cfg->intr_flag &= ~IPR_USE_MSIX;
				pci_disable_msix(pdev);
			}

			ioa_cfg->intr_flag = IPR_USE_LSI;
			ioa_cfg->nvectors = 1;
		}
10000 10001
		else if (rc)
			goto out_msi_disable;
10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016
		else {
			if (ioa_cfg->intr_flag == IPR_USE_MSI)
				dev_info(&pdev->dev,
					"Request for %d MSIs succeeded with starting IRQ: %d\n",
					ioa_cfg->nvectors, pdev->irq);
			else if (ioa_cfg->intr_flag == IPR_USE_MSIX)
				dev_info(&pdev->dev,
					"Request for %d MSIXs succeeded.",
					ioa_cfg->nvectors);
		}
	}

	ioa_cfg->hrrq_num = min3(ioa_cfg->nvectors,
				(unsigned int)num_online_cpus(),
				(unsigned int)IPR_MAX_HRRQ_NUM);
10017

L
Linus Torvalds 已提交
10018
	if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
10019
		goto out_msi_disable;
L
Linus Torvalds 已提交
10020 10021

	if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
10022
		goto out_msi_disable;
L
Linus Torvalds 已提交
10023 10024 10025 10026 10027

	rc = ipr_alloc_mem(ioa_cfg);
	if (rc < 0) {
		dev_err(&pdev->dev,
			"Couldn't allocate enough memory for device driver!\n");
10028
		goto out_msi_disable;
L
Linus Torvalds 已提交
10029 10030
	}

B
Brian King 已提交
10031 10032 10033 10034 10035 10036 10037 10038 10039
	/* Save away PCI config space for use following IOA reset */
	rc = pci_save_state(pdev);

	if (rc != PCIBIOS_SUCCESSFUL) {
		dev_err(&pdev->dev, "Failed to save PCI config space\n");
		rc = -EIO;
		goto cleanup_nolog;
	}

10040 10041 10042 10043
	/*
	 * If HRRQ updated interrupt is not masked, or reset alert is set,
	 * the card is in an unknown state and needs a hard reset
	 */
10044 10045 10046
	mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
	interrupts = readl(ioa_cfg->regs.sense_interrupt_reg32);
	uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
10047 10048
	if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
		ioa_cfg->needs_hard_reset = 1;
10049
	if ((interrupts & IPR_PCII_ERROR_INTERRUPTS) || reset_devices)
10050 10051 10052
		ioa_cfg->needs_hard_reset = 1;
	if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
		ioa_cfg->ioa_unit_checked = 1;
10053

10054
	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
L
Linus Torvalds 已提交
10055
	ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
10056
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
L
Linus Torvalds 已提交
10057

10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071
	if (ioa_cfg->intr_flag == IPR_USE_MSI
			|| ioa_cfg->intr_flag == IPR_USE_MSIX) {
		name_msi_vectors(ioa_cfg);
		rc = request_irq(ioa_cfg->vectors_info[0].vec, ipr_isr,
			0,
			ioa_cfg->vectors_info[0].desc,
			&ioa_cfg->hrrq[0]);
		if (!rc)
			rc = ipr_request_other_msi_irqs(ioa_cfg);
	} else {
		rc = request_irq(pdev->irq, ipr_isr,
			 IRQF_SHARED,
			 IPR_NAME, &ioa_cfg->hrrq[0]);
	}
L
Linus Torvalds 已提交
10072 10073 10074 10075 10076 10077
	if (rc) {
		dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
			pdev->irq, rc);
		goto cleanup_nolog;
	}

10078 10079 10080 10081
	if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
	    (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
		ioa_cfg->needs_warm_reset = 1;
		ioa_cfg->reset = ipr_reset_slot_reset;
B
Brian King 已提交
10082 10083 10084 10085 10086 10087 10088 10089

		ioa_cfg->reset_work_q = alloc_ordered_workqueue("ipr_reset_%d",
								WQ_MEM_RECLAIM, host->host_no);

		if (!ioa_cfg->reset_work_q) {
			dev_err(&pdev->dev, "Couldn't register reset workqueue\n");
			goto out_free_irq;
		}
10090 10091 10092
	} else
		ioa_cfg->reset = ipr_reset_start_bist;

10093
	spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
L
Linus Torvalds 已提交
10094
	list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
10095
	spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
L
Linus Torvalds 已提交
10096 10097 10098 10099 10100

	LEAVE;
out:
	return rc;

B
Brian King 已提交
10101 10102
out_free_irq:
	ipr_free_irqs(ioa_cfg);
L
Linus Torvalds 已提交
10103 10104
cleanup_nolog:
	ipr_free_mem(ioa_cfg);
10105
out_msi_disable:
B
Brian King 已提交
10106
	ipr_wait_for_pci_err_recovery(ioa_cfg);
10107 10108 10109 10110
	if (ioa_cfg->intr_flag == IPR_USE_MSI)
		pci_disable_msi(pdev);
	else if (ioa_cfg->intr_flag == IPR_USE_MSIX)
		pci_disable_msix(pdev);
10111 10112
cleanup_nomem:
	iounmap(ipr_regs);
B
Brian King 已提交
10113 10114
out_disable:
	pci_disable_device(pdev);
L
Linus Torvalds 已提交
10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160
out_release_regions:
	pci_release_regions(pdev);
out_scsi_host_put:
	scsi_host_put(host);
	goto out;
}

/**
 * ipr_initiate_ioa_bringdown - Bring down an adapter
 * @ioa_cfg:		ioa config struct
 * @shutdown_type:	shutdown type
 *
 * Description: This function will initiate bringing down the adapter.
 * This consists of issuing an IOA shutdown to the adapter
 * to flush the cache, and running BIST.
 * If the caller needs to wait on the completion of the reset,
 * the caller must sleep on the reset_wait_q.
 *
 * Return value:
 * 	none
 **/
static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
				       enum ipr_shutdown_type shutdown_type)
{
	ENTER;
	if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
		ioa_cfg->sdt_state = ABORT_DUMP;
	ioa_cfg->reset_retries = 0;
	ioa_cfg->in_ioa_bringdown = 1;
	ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
	LEAVE;
}

/**
 * __ipr_remove - Remove a single adapter
 * @pdev:	pci device struct
 *
 * Adapter hot plug remove entry point.
 *
 * Return value:
 * 	none
 **/
static void __ipr_remove(struct pci_dev *pdev)
{
	unsigned long host_lock_flags = 0;
	struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
10161
	int i;
10162
	unsigned long driver_lock_flags;
L
Linus Torvalds 已提交
10163 10164 10165
	ENTER;

	spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
10166
	while (ioa_cfg->in_reset_reload) {
10167 10168 10169 10170 10171
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
		wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
		spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
	}

10172 10173 10174 10175 10176 10177
	for (i = 0; i < ioa_cfg->hrrq_num; i++) {
		spin_lock(&ioa_cfg->hrrq[i]._lock);
		ioa_cfg->hrrq[i].removing_ioa = 1;
		spin_unlock(&ioa_cfg->hrrq[i]._lock);
	}
	wmb();
L
Linus Torvalds 已提交
10178 10179 10180 10181
	ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);

	spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
	wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
10182
	flush_work(&ioa_cfg->work_q);
B
Brian King 已提交
10183 10184
	if (ioa_cfg->reset_work_q)
		flush_workqueue(ioa_cfg->reset_work_q);
10185
	INIT_LIST_HEAD(&ioa_cfg->used_res_q);
L
Linus Torvalds 已提交
10186 10187
	spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);

10188
	spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
L
Linus Torvalds 已提交
10189
	list_del(&ioa_cfg->queue);
10190
	spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
L
Linus Torvalds 已提交
10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209

	if (ioa_cfg->sdt_state == ABORT_DUMP)
		ioa_cfg->sdt_state = WAIT_FOR_DUMP;
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);

	ipr_free_all_resources(ioa_cfg);

	LEAVE;
}

/**
 * ipr_remove - IOA hot plug remove entry point
 * @pdev:	pci device struct
 *
 * Adapter hot plug remove entry point.
 *
 * Return value:
 * 	none
 **/
10210
static void ipr_remove(struct pci_dev *pdev)
L
Linus Torvalds 已提交
10211 10212 10213 10214 10215
{
	struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);

	ENTER;

10216
	ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
L
Linus Torvalds 已提交
10217
			      &ipr_trace_attr);
10218
	ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
L
Linus Torvalds 已提交
10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232
			     &ipr_dump_attr);
	scsi_remove_host(ioa_cfg->host);

	__ipr_remove(pdev);

	LEAVE;
}

/**
 * ipr_probe - Adapter hot plug add entry point
 *
 * Return value:
 * 	0 on success / non-zero on failure
 **/
10233
static int ipr_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
L
Linus Torvalds 已提交
10234 10235
{
	struct ipr_ioa_cfg *ioa_cfg;
10236
	int rc, i;
L
Linus Torvalds 已提交
10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257

	rc = ipr_probe_ioa(pdev, dev_id);

	if (rc)
		return rc;

	ioa_cfg = pci_get_drvdata(pdev);
	rc = ipr_probe_ioa_part2(ioa_cfg);

	if (rc) {
		__ipr_remove(pdev);
		return rc;
	}

	rc = scsi_add_host(ioa_cfg->host, &pdev->dev);

	if (rc) {
		__ipr_remove(pdev);
		return rc;
	}

10258
	rc = ipr_create_trace_file(&ioa_cfg->host->shost_dev.kobj,
L
Linus Torvalds 已提交
10259 10260 10261 10262 10263 10264 10265 10266
				   &ipr_trace_attr);

	if (rc) {
		scsi_remove_host(ioa_cfg->host);
		__ipr_remove(pdev);
		return rc;
	}

10267
	rc = ipr_create_dump_file(&ioa_cfg->host->shost_dev.kobj,
L
Linus Torvalds 已提交
10268 10269 10270
				   &ipr_dump_attr);

	if (rc) {
10271
		ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
L
Linus Torvalds 已提交
10272 10273 10274 10275 10276 10277 10278
				      &ipr_trace_attr);
		scsi_remove_host(ioa_cfg->host);
		__ipr_remove(pdev);
		return rc;
	}

	scsi_scan_host(ioa_cfg->host);
10279 10280
	ioa_cfg->iopoll_weight = ioa_cfg->chip_cfg->iopoll_weight;

10281
	if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
10282 10283 10284 10285 10286 10287 10288
		for (i = 1; i < ioa_cfg->hrrq_num; i++) {
			blk_iopoll_init(&ioa_cfg->hrrq[i].iopoll,
					ioa_cfg->iopoll_weight, ipr_iopoll);
			blk_iopoll_enable(&ioa_cfg->hrrq[i].iopoll);
		}
	}

L
Linus Torvalds 已提交
10289 10290 10291 10292 10293 10294
	schedule_work(&ioa_cfg->work_q);
	return 0;
}

/**
 * ipr_shutdown - Shutdown handler.
10295
 * @pdev:	pci device struct
L
Linus Torvalds 已提交
10296 10297 10298 10299 10300 10301 10302
 *
 * This function is invoked upon system shutdown/reboot. It will issue
 * an adapter shutdown to the adapter to flush the write cache.
 *
 * Return value:
 * 	none
 **/
10303
static void ipr_shutdown(struct pci_dev *pdev)
L
Linus Torvalds 已提交
10304
{
10305
	struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
L
Linus Torvalds 已提交
10306
	unsigned long lock_flags = 0;
B
Brian King 已提交
10307
	enum ipr_shutdown_type shutdown_type = IPR_SHUTDOWN_NORMAL;
10308
	int i;
L
Linus Torvalds 已提交
10309 10310

	spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
10311
	if (ioa_cfg->iopoll_weight && ioa_cfg->sis64 && ioa_cfg->nvectors > 1) {
10312 10313 10314 10315 10316
		ioa_cfg->iopoll_weight = 0;
		for (i = 1; i < ioa_cfg->hrrq_num; i++)
			blk_iopoll_disable(&ioa_cfg->hrrq[i].iopoll);
	}

10317
	while (ioa_cfg->in_reset_reload) {
10318 10319 10320 10321 10322
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
		wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
		spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
	}

B
Brian King 已提交
10323 10324 10325 10326
	if (ipr_fast_reboot && system_state == SYSTEM_RESTART && ioa_cfg->sis64)
		shutdown_type = IPR_SHUTDOWN_QUIESCE;

	ipr_initiate_ioa_bringdown(ioa_cfg, shutdown_type);
L
Linus Torvalds 已提交
10327 10328
	spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
	wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
B
Brian King 已提交
10329
	if (ipr_fast_reboot && system_state == SYSTEM_RESTART && ioa_cfg->sis64) {
B
Brian King 已提交
10330
		ipr_free_irqs(ioa_cfg);
B
Brian King 已提交
10331 10332
		pci_disable_device(ioa_cfg->pdev);
	}
L
Linus Torvalds 已提交
10333 10334
}

10335
static struct pci_device_id ipr_pci_table[] = {
L
Linus Torvalds 已提交
10336
	{ PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
10337
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
L
Linus Torvalds 已提交
10338
	{ PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
10339
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
L
Linus Torvalds 已提交
10340
	{ PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
10341
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
L
Linus Torvalds 已提交
10342
	{ PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
10343
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
L
Linus Torvalds 已提交
10344
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
10345
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
L
Linus Torvalds 已提交
10346
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
10347
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
L
Linus Torvalds 已提交
10348
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
10349
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
B
brking@us.ibm.com 已提交
10350
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
10351 10352
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
		IPR_USE_LONG_TRANSOP_TIMEOUT },
B
brking@us.ibm.com 已提交
10353
	{ PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
10354
	      PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
B
brking@us.ibm.com 已提交
10355
	{ PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
10356 10357
	      PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
	      IPR_USE_LONG_TRANSOP_TIMEOUT },
10358
	{ PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
10359 10360
	      PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
	      IPR_USE_LONG_TRANSOP_TIMEOUT },
B
brking@us.ibm.com 已提交
10361
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
10362
	      PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
B
brking@us.ibm.com 已提交
10363
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
10364 10365
	      PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
	      IPR_USE_LONG_TRANSOP_TIMEOUT},
10366
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
10367 10368
	      PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
	      IPR_USE_LONG_TRANSOP_TIMEOUT },
10369
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
10370 10371
	      PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
	      IPR_USE_LONG_TRANSOP_TIMEOUT },
10372 10373
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
	      PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
10374 10375
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
	      PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CC, 0, 0, 0 },
10376
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
10377
	      PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
10378
	      IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
L
Linus Torvalds 已提交
10379
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
10380
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
L
Linus Torvalds 已提交
10381
	{ PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
10382
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
B
brking@us.ibm.com 已提交
10383
	{ PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
10384 10385
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
		IPR_USE_LONG_TRANSOP_TIMEOUT },
10386
	{ PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
10387 10388
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
		IPR_USE_LONG_TRANSOP_TIMEOUT },
10389 10390 10391 10392 10393 10394
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B5, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B2, 0, 0, 0 },
10395 10396
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C0, 0, 0, 0 },
10397 10398
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C3, 0, 0, 0 },
10399 10400
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C4, 0, 0, 0 },
10401
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10402
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B4, 0, 0, 0 },
10403
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10404
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B1, 0, 0, 0 },
10405
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10406
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C6, 0, 0, 0 },
10407 10408 10409
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C8, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10410
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CE, 0, 0, 0 },
10411 10412 10413 10414 10415 10416 10417 10418
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D5, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D6, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D7, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D8, 0, 0, 0 },
10419 10420 10421
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57D9, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10422 10423
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57DA, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EB, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EC, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57ED, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EE, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57EF, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57F0, 0, 0, 0 },
10435 10436 10437 10438 10439 10440
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CCA, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CD2, 0, 0, 0 },
	{ PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
		PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2CCD, 0, 0, 0 },
L
Linus Torvalds 已提交
10441 10442 10443 10444
	{ }
};
MODULE_DEVICE_TABLE(pci, ipr_pci_table);

10445
static const struct pci_error_handlers ipr_err_handler = {
10446
	.error_detected = ipr_pci_error_detected,
B
Brian King 已提交
10447
	.mmio_enabled = ipr_pci_mmio_enabled,
10448 10449 10450
	.slot_reset = ipr_pci_slot_reset,
};

L
Linus Torvalds 已提交
10451 10452 10453 10454
static struct pci_driver ipr_driver = {
	.name = IPR_NAME,
	.id_table = ipr_pci_table,
	.probe = ipr_probe,
10455
	.remove = ipr_remove,
10456
	.shutdown = ipr_shutdown,
10457
	.err_handler = &ipr_err_handler,
L
Linus Torvalds 已提交
10458 10459
};

10460 10461 10462 10463 10464 10465 10466 10467
/**
 * ipr_halt_done - Shutdown prepare completion
 *
 * Return value:
 * 	none
 **/
static void ipr_halt_done(struct ipr_cmnd *ipr_cmd)
{
10468
	list_add_tail(&ipr_cmd->queue, &ipr_cmd->hrrq->hrrq_free_q);
10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480
}

/**
 * ipr_halt - Issue shutdown prepare to all adapters
 *
 * Return value:
 * 	NOTIFY_OK on success / NOTIFY_DONE on failure
 **/
static int ipr_halt(struct notifier_block *nb, ulong event, void *buf)
{
	struct ipr_cmnd *ipr_cmd;
	struct ipr_ioa_cfg *ioa_cfg;
10481
	unsigned long flags = 0, driver_lock_flags;
10482 10483 10484 10485

	if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
		return NOTIFY_DONE;

10486
	spin_lock_irqsave(&ipr_driver_lock, driver_lock_flags);
10487 10488 10489

	list_for_each_entry(ioa_cfg, &ipr_ioa_head, queue) {
		spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
B
Brian King 已提交
10490 10491
		if (!ioa_cfg->hrrq[IPR_INIT_HRRQ].allow_cmds ||
		    (ipr_fast_reboot && event == SYS_RESTART && ioa_cfg->sis64)) {
10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504
			spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
			continue;
		}

		ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
		ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
		ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
		ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
		ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;

		ipr_do_req(ipr_cmd, ipr_halt_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
		spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
	}
10505
	spin_unlock_irqrestore(&ipr_driver_lock, driver_lock_flags);
10506 10507 10508 10509 10510 10511 10512 10513

	return NOTIFY_OK;
}

static struct notifier_block ipr_notifier = {
	ipr_halt, NULL, 0
};

L
Linus Torvalds 已提交
10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524
/**
 * ipr_init - Module entry point
 *
 * Return value:
 * 	0 on success / negative value on failure
 **/
static int __init ipr_init(void)
{
	ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
		 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);

10525
	register_reboot_notifier(&ipr_notifier);
10526
	return pci_register_driver(&ipr_driver);
L
Linus Torvalds 已提交
10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538
}

/**
 * ipr_exit - Module unload
 *
 * Module unload entry point.
 *
 * Return value:
 * 	none
 **/
static void __exit ipr_exit(void)
{
10539
	unregister_reboot_notifier(&ipr_notifier);
L
Linus Torvalds 已提交
10540 10541 10542 10543 10544
	pci_unregister_driver(&ipr_driver);
}

module_init(ipr_init);
module_exit(ipr_exit);