[SCSI] ipr: Reduce queuecommand lock time
[deliverable/linux.git] / drivers / scsi / ipr.c
CommitLineData
1da177e4
LT
1/*
2 * ipr.c -- driver for IBM Power Linux RAID adapters
3 *
4 * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
5 *
6 * Copyright (C) 2003, 2004 IBM Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24/*
25 * Notes:
26 *
27 * This driver is used to control the following SCSI adapters:
28 *
29 * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
30 *
31 * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
32 * PCI-X Dual Channel Ultra 320 SCSI Adapter
33 * PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
34 * Embedded SCSI adapter on p615 and p655 systems
35 *
36 * Supported Hardware Features:
37 * - Ultra 320 SCSI controller
38 * - PCI-X host interface
39 * - Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
40 * - Non-Volatile Write Cache
41 * - Supports attachment of non-RAID disks, tape, and optical devices
42 * - RAID Levels 0, 5, 10
43 * - Hot spare
44 * - Background Parity Checking
45 * - Background Data Scrubbing
46 * - Ability to increase the capacity of an existing RAID 5 disk array
47 * by adding disks
48 *
49 * Driver Features:
50 * - Tagged command queuing
51 * - Adapter microcode download
52 * - PCI hot plug
53 * - SCSI device hot plug
54 *
55 */
56
1da177e4
LT
57#include <linux/fs.h>
58#include <linux/init.h>
59#include <linux/types.h>
60#include <linux/errno.h>
61#include <linux/kernel.h>
5a0e3ad6 62#include <linux/slab.h>
4d4dd706 63#include <linux/vmalloc.h>
1da177e4
LT
64#include <linux/ioport.h>
65#include <linux/delay.h>
66#include <linux/pci.h>
67#include <linux/wait.h>
68#include <linux/spinlock.h>
69#include <linux/sched.h>
70#include <linux/interrupt.h>
71#include <linux/blkdev.h>
72#include <linux/firmware.h>
73#include <linux/module.h>
74#include <linux/moduleparam.h>
35a39691 75#include <linux/libata.h>
0ce3a7e5 76#include <linux/hdreg.h>
f72919ec 77#include <linux/reboot.h>
3e7ebdfa 78#include <linux/stringify.h>
1da177e4
LT
79#include <asm/io.h>
80#include <asm/irq.h>
81#include <asm/processor.h>
82#include <scsi/scsi.h>
83#include <scsi/scsi_host.h>
84#include <scsi/scsi_tcq.h>
85#include <scsi/scsi_eh.h>
86#include <scsi/scsi_cmnd.h>
1da177e4
LT
87#include "ipr.h"
88
89/*
90 * Global Data
91 */
b7d68ca3 92static LIST_HEAD(ipr_ioa_head);
1da177e4
LT
93static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
94static unsigned int ipr_max_speed = 1;
95static int ipr_testmode = 0;
96static unsigned int ipr_fastfail = 0;
5469cb5b 97static unsigned int ipr_transop_timeout = 0;
d3c74871 98static unsigned int ipr_debug = 0;
3e7ebdfa 99static unsigned int ipr_max_devs = IPR_DEFAULT_SIS64_DEVS;
ac09c349 100static unsigned int ipr_dual_ioa_raid = 1;
1da177e4
LT
101static DEFINE_SPINLOCK(ipr_driver_lock);
102
103/* This table describes the differences between DMA controller chips */
104static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
60e7486b 105 { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
1da177e4 106 .mailbox = 0x0042C,
89aad428 107 .max_cmds = 100,
1da177e4 108 .cache_line_size = 0x20,
7dd21308 109 .clear_isr = 1,
1da177e4
LT
110 {
111 .set_interrupt_mask_reg = 0x0022C,
112 .clr_interrupt_mask_reg = 0x00230,
214777ba 113 .clr_interrupt_mask_reg32 = 0x00230,
1da177e4 114 .sense_interrupt_mask_reg = 0x0022C,
214777ba 115 .sense_interrupt_mask_reg32 = 0x0022C,
1da177e4 116 .clr_interrupt_reg = 0x00228,
214777ba 117 .clr_interrupt_reg32 = 0x00228,
1da177e4 118 .sense_interrupt_reg = 0x00224,
214777ba 119 .sense_interrupt_reg32 = 0x00224,
1da177e4
LT
120 .ioarrin_reg = 0x00404,
121 .sense_uproc_interrupt_reg = 0x00214,
214777ba 122 .sense_uproc_interrupt_reg32 = 0x00214,
1da177e4 123 .set_uproc_interrupt_reg = 0x00214,
214777ba
WB
124 .set_uproc_interrupt_reg32 = 0x00214,
125 .clr_uproc_interrupt_reg = 0x00218,
126 .clr_uproc_interrupt_reg32 = 0x00218
1da177e4
LT
127 }
128 },
129 { /* Snipe and Scamp */
130 .mailbox = 0x0052C,
89aad428 131 .max_cmds = 100,
1da177e4 132 .cache_line_size = 0x20,
7dd21308 133 .clear_isr = 1,
1da177e4
LT
134 {
135 .set_interrupt_mask_reg = 0x00288,
136 .clr_interrupt_mask_reg = 0x0028C,
214777ba 137 .clr_interrupt_mask_reg32 = 0x0028C,
1da177e4 138 .sense_interrupt_mask_reg = 0x00288,
214777ba 139 .sense_interrupt_mask_reg32 = 0x00288,
1da177e4 140 .clr_interrupt_reg = 0x00284,
214777ba 141 .clr_interrupt_reg32 = 0x00284,
1da177e4 142 .sense_interrupt_reg = 0x00280,
214777ba 143 .sense_interrupt_reg32 = 0x00280,
1da177e4
LT
144 .ioarrin_reg = 0x00504,
145 .sense_uproc_interrupt_reg = 0x00290,
214777ba 146 .sense_uproc_interrupt_reg32 = 0x00290,
1da177e4 147 .set_uproc_interrupt_reg = 0x00290,
214777ba
WB
148 .set_uproc_interrupt_reg32 = 0x00290,
149 .clr_uproc_interrupt_reg = 0x00294,
150 .clr_uproc_interrupt_reg32 = 0x00294
1da177e4
LT
151 }
152 },
a74c1639 153 { /* CRoC */
110def85 154 .mailbox = 0x00044,
89aad428 155 .max_cmds = 1000,
a74c1639 156 .cache_line_size = 0x20,
7dd21308 157 .clear_isr = 0,
a74c1639
WB
158 {
159 .set_interrupt_mask_reg = 0x00010,
160 .clr_interrupt_mask_reg = 0x00018,
214777ba 161 .clr_interrupt_mask_reg32 = 0x0001C,
a74c1639 162 .sense_interrupt_mask_reg = 0x00010,
214777ba 163 .sense_interrupt_mask_reg32 = 0x00014,
a74c1639 164 .clr_interrupt_reg = 0x00008,
214777ba 165 .clr_interrupt_reg32 = 0x0000C,
a74c1639 166 .sense_interrupt_reg = 0x00000,
214777ba 167 .sense_interrupt_reg32 = 0x00004,
a74c1639
WB
168 .ioarrin_reg = 0x00070,
169 .sense_uproc_interrupt_reg = 0x00020,
214777ba 170 .sense_uproc_interrupt_reg32 = 0x00024,
a74c1639 171 .set_uproc_interrupt_reg = 0x00020,
214777ba 172 .set_uproc_interrupt_reg32 = 0x00024,
dcbad00e 173 .clr_uproc_interrupt_reg = 0x00028,
214777ba
WB
174 .clr_uproc_interrupt_reg32 = 0x0002C,
175 .init_feedback_reg = 0x0005C,
dcbad00e 176 .dump_addr_reg = 0x00064,
8701f185
WB
177 .dump_data_reg = 0x00068,
178 .endian_swap_reg = 0x00084
a74c1639
WB
179 }
180 },
1da177e4
LT
181};
182
183static const struct ipr_chip_t ipr_chip[] = {
cb237ef7
WB
184 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
185 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
186 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
187 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
188 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, IPR_USE_MSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[0] },
189 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[1] },
190 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, IPR_USE_LSI, IPR_SIS32, IPR_PCI_CFG, &ipr_chip_cfg[1] },
191 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] },
cd9b3d04 192 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE, IPR_USE_MSI, IPR_SIS64, IPR_MMIO, &ipr_chip_cfg[2] }
1da177e4
LT
193};
194
195static int ipr_max_bus_speeds [] = {
196 IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
197};
198
199MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
200MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
201module_param_named(max_speed, ipr_max_speed, uint, 0);
202MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
203module_param_named(log_level, ipr_log_level, uint, 0);
204MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
205module_param_named(testmode, ipr_testmode, int, 0);
206MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
2cf22be0 207module_param_named(fastfail, ipr_fastfail, int, S_IRUGO | S_IWUSR);
1da177e4
LT
208MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
209module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
210MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
2cf22be0 211module_param_named(debug, ipr_debug, int, S_IRUGO | S_IWUSR);
d3c74871 212MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
ac09c349
BK
213module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
214MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
3e7ebdfa
WB
215module_param_named(max_devs, ipr_max_devs, int, 0);
216MODULE_PARM_DESC(max_devs, "Specify the maximum number of physical devices. "
217 "[Default=" __stringify(IPR_DEFAULT_SIS64_DEVS) "]");
1da177e4
LT
218MODULE_LICENSE("GPL");
219MODULE_VERSION(IPR_DRIVER_VERSION);
220
1da177e4
LT
221/* A constant array of IOASCs/URCs/Error Messages */
222static const
223struct ipr_error_table_t ipr_error_table[] = {
933916f3 224 {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
225 "8155: An unknown error was received"},
226 {0x00330000, 0, 0,
227 "Soft underlength error"},
228 {0x005A0000, 0, 0,
229 "Command to be cancelled not found"},
230 {0x00808000, 0, 0,
231 "Qualified success"},
933916f3 232 {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 233 "FFFE: Soft device bus error recovered by the IOA"},
933916f3 234 {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 235 "4101: Soft device bus fabric error"},
5aa3a333
WB
236 {0x01100100, 0, IPR_DEFAULT_LOG_LEVEL,
237 "FFFC: Logical block guard error recovered by the device"},
238 {0x01100300, 0, IPR_DEFAULT_LOG_LEVEL,
239 "FFFC: Logical block reference tag error recovered by the device"},
240 {0x01108300, 0, IPR_DEFAULT_LOG_LEVEL,
241 "4171: Recovered scatter list tag / sequence number error"},
242 {0x01109000, 0, IPR_DEFAULT_LOG_LEVEL,
243 "FF3D: Recovered logical block CRC error on IOA to Host transfer"},
244 {0x01109200, 0, IPR_DEFAULT_LOG_LEVEL,
245 "4171: Recovered logical block sequence number error on IOA to Host transfer"},
246 {0x0110A000, 0, IPR_DEFAULT_LOG_LEVEL,
247 "FFFD: Recovered logical block reference tag error detected by the IOA"},
248 {0x0110A100, 0, IPR_DEFAULT_LOG_LEVEL,
249 "FFFD: Logical block guard error recovered by the IOA"},
933916f3 250 {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 251 "FFF9: Device sector reassign successful"},
933916f3 252 {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 253 "FFF7: Media error recovered by device rewrite procedures"},
933916f3 254 {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 255 "7001: IOA sector reassignment successful"},
933916f3 256 {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 257 "FFF9: Soft media error. Sector reassignment recommended"},
933916f3 258 {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 259 "FFF7: Media error recovered by IOA rewrite procedures"},
933916f3 260 {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 261 "FF3D: Soft PCI bus error recovered by the IOA"},
933916f3 262 {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 263 "FFF6: Device hardware error recovered by the IOA"},
933916f3 264 {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 265 "FFF6: Device hardware error recovered by the device"},
933916f3 266 {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 267 "FF3D: Soft IOA error recovered by the IOA"},
933916f3 268 {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 269 "FFFA: Undefined device response recovered by the IOA"},
933916f3 270 {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 271 "FFF6: Device bus error, message or command phase"},
933916f3 272 {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
35a39691 273 "FFFE: Task Management Function failed"},
933916f3 274 {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 275 "FFF6: Failure prediction threshold exceeded"},
933916f3 276 {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
277 "8009: Impending cache battery pack failure"},
278 {0x02040400, 0, 0,
279 "34FF: Disk device format in progress"},
65f56475
BK
280 {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
281 "9070: IOA requested reset"},
1da177e4
LT
282 {0x023F0000, 0, 0,
283 "Synchronization required"},
284 {0x024E0000, 0, 0,
285 "No ready, IOA shutdown"},
286 {0x025A0000, 0, 0,
287 "Not ready, IOA has been shutdown"},
933916f3 288 {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
289 "3020: Storage subsystem configuration error"},
290 {0x03110B00, 0, 0,
291 "FFF5: Medium error, data unreadable, recommend reassign"},
292 {0x03110C00, 0, 0,
293 "7000: Medium error, data unreadable, do not reassign"},
933916f3 294 {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 295 "FFF3: Disk media format bad"},
933916f3 296 {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 297 "3002: Addressed device failed to respond to selection"},
933916f3 298 {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 299 "3100: Device bus error"},
933916f3 300 {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
301 "3109: IOA timed out a device command"},
302 {0x04088000, 0, 0,
303 "3120: SCSI bus is not operational"},
933916f3 304 {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 305 "4100: Hard device bus fabric error"},
5aa3a333
WB
306 {0x04100100, 0, IPR_DEFAULT_LOG_LEVEL,
307 "310C: Logical block guard error detected by the device"},
308 {0x04100300, 0, IPR_DEFAULT_LOG_LEVEL,
309 "310C: Logical block reference tag error detected by the device"},
310 {0x04108300, 1, IPR_DEFAULT_LOG_LEVEL,
311 "4170: Scatter list tag / sequence number error"},
312 {0x04109000, 1, IPR_DEFAULT_LOG_LEVEL,
313 "8150: Logical block CRC error on IOA to Host transfer"},
314 {0x04109200, 1, IPR_DEFAULT_LOG_LEVEL,
315 "4170: Logical block sequence number error on IOA to Host transfer"},
316 {0x0410A000, 0, IPR_DEFAULT_LOG_LEVEL,
317 "310D: Logical block reference tag error detected by the IOA"},
318 {0x0410A100, 0, IPR_DEFAULT_LOG_LEVEL,
319 "310D: Logical block guard error detected by the IOA"},
933916f3 320 {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 321 "9000: IOA reserved area data check"},
933916f3 322 {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 323 "9001: IOA reserved area invalid data pattern"},
933916f3 324 {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 325 "9002: IOA reserved area LRC error"},
5aa3a333
WB
326 {0x04118300, 1, IPR_DEFAULT_LOG_LEVEL,
327 "Hardware Error, IOA metadata access error"},
933916f3 328 {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 329 "102E: Out of alternate sectors for disk storage"},
933916f3 330 {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 331 "FFF4: Data transfer underlength error"},
933916f3 332 {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 333 "FFF4: Data transfer overlength error"},
933916f3 334 {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 335 "3400: Logical unit failure"},
933916f3 336 {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 337 "FFF4: Device microcode is corrupt"},
933916f3 338 {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
339 "8150: PCI bus error"},
340 {0x04430000, 1, 0,
341 "Unsupported device bus message received"},
933916f3 342 {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 343 "FFF4: Disk device problem"},
933916f3 344 {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 345 "8150: Permanent IOA failure"},
933916f3 346 {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 347 "3010: Disk device returned wrong response to IOA"},
933916f3 348 {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
349 "8151: IOA microcode error"},
350 {0x04448500, 0, 0,
351 "Device bus status error"},
933916f3 352 {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 353 "8157: IOA error requiring IOA reset to recover"},
35a39691
BK
354 {0x04448700, 0, 0,
355 "ATA device status error"},
1da177e4
LT
356 {0x04490000, 0, 0,
357 "Message reject received from the device"},
933916f3 358 {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 359 "8008: A permanent cache battery pack failure occurred"},
933916f3 360 {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 361 "9090: Disk unit has been modified after the last known status"},
933916f3 362 {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 363 "9081: IOA detected device error"},
933916f3 364 {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 365 "9082: IOA detected device error"},
933916f3 366 {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
1da177e4 367 "3110: Device bus error, message or command phase"},
933916f3 368 {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
35a39691 369 "3110: SAS Command / Task Management Function failed"},
933916f3 370 {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 371 "9091: Incorrect hardware configuration change has been detected"},
933916f3 372 {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
b0df54bb 373 "9073: Invalid multi-adapter configuration"},
933916f3 374 {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 375 "4010: Incorrect connection between cascaded expanders"},
933916f3 376 {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 377 "4020: Connections exceed IOA design limits"},
933916f3 378 {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 379 "4030: Incorrect multipath connection"},
933916f3 380 {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 381 "4110: Unsupported enclosure function"},
933916f3 382 {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
383 "FFF4: Command to logical unit failed"},
384 {0x05240000, 1, 0,
385 "Illegal request, invalid request type or request packet"},
386 {0x05250000, 0, 0,
387 "Illegal request, invalid resource handle"},
b0df54bb 388 {0x05258000, 0, 0,
389 "Illegal request, commands not allowed to this device"},
390 {0x05258100, 0, 0,
391 "Illegal request, command not allowed to a secondary adapter"},
5aa3a333
WB
392 {0x05258200, 0, 0,
393 "Illegal request, command not allowed to a non-optimized resource"},
1da177e4
LT
394 {0x05260000, 0, 0,
395 "Illegal request, invalid field in parameter list"},
396 {0x05260100, 0, 0,
397 "Illegal request, parameter not supported"},
398 {0x05260200, 0, 0,
399 "Illegal request, parameter value invalid"},
400 {0x052C0000, 0, 0,
401 "Illegal request, command sequence error"},
b0df54bb 402 {0x052C8000, 1, 0,
403 "Illegal request, dual adapter support not enabled"},
933916f3 404 {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 405 "9031: Array protection temporarily suspended, protection resuming"},
933916f3 406 {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 407 "9040: Array protection temporarily suspended, protection resuming"},
933916f3 408 {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 409 "3140: Device bus not ready to ready transition"},
933916f3 410 {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
411 "FFFB: SCSI bus was reset"},
412 {0x06290500, 0, 0,
413 "FFFE: SCSI bus transition to single ended"},
414 {0x06290600, 0, 0,
415 "FFFE: SCSI bus transition to LVD"},
933916f3 416 {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 417 "FFFB: SCSI bus was reset by another initiator"},
933916f3 418 {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 419 "3029: A device replacement has occurred"},
933916f3 420 {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 421 "9051: IOA cache data exists for a missing or failed device"},
933916f3 422 {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
b0df54bb 423 "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
933916f3 424 {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 425 "9025: Disk unit is not supported at its physical location"},
933916f3 426 {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 427 "3020: IOA detected a SCSI bus configuration error"},
933916f3 428 {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 429 "3150: SCSI bus configuration error"},
933916f3 430 {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
b0df54bb 431 "9074: Asymmetric advanced function disk configuration"},
933916f3 432 {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 433 "4040: Incomplete multipath connection between IOA and enclosure"},
933916f3 434 {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 435 "4041: Incomplete multipath connection between enclosure and device"},
933916f3 436 {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 437 "9075: Incomplete multipath connection between IOA and remote IOA"},
933916f3 438 {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 439 "9076: Configuration error, missing remote IOA"},
933916f3 440 {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 441 "4050: Enclosure does not support a required multipath function"},
b75424fc
WB
442 {0x06690000, 0, IPR_DEFAULT_LOG_LEVEL,
443 "4070: Logically bad block written on device"},
933916f3 444 {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 445 "9041: Array protection temporarily suspended"},
933916f3 446 {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 447 "9042: Corrupt array parity detected on specified device"},
933916f3 448 {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 449 "9030: Array no longer protected due to missing or failed disk unit"},
933916f3 450 {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
b0df54bb 451 "9071: Link operational transition"},
933916f3 452 {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
b0df54bb 453 "9072: Link not operational transition"},
933916f3 454 {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 455 "9032: Array exposed but still protected"},
e435340c
BK
456 {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
457 "70DD: Device forced failed by disrupt device command"},
933916f3 458 {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 459 "4061: Multipath redundancy level got better"},
933916f3 460 {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
896bbd21 461 "4060: Multipath redundancy level got worse"},
1da177e4
LT
462 {0x07270000, 0, 0,
463 "Failure due to other device"},
933916f3 464 {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 465 "9008: IOA does not support functions expected by devices"},
933916f3 466 {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 467 "9010: Cache data associated with attached devices cannot be found"},
933916f3 468 {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 469 "9011: Cache data belongs to devices other than those attached"},
933916f3 470 {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 471 "9020: Array missing 2 or more devices with only 1 device present"},
933916f3 472 {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 473 "9021: Array missing 2 or more devices with 2 or more devices present"},
933916f3 474 {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 475 "9022: Exposed array is missing a required device"},
933916f3 476 {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 477 "9023: Array member(s) not at required physical locations"},
933916f3 478 {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 479 "9024: Array not functional due to present hardware configuration"},
933916f3 480 {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 481 "9026: Array not functional due to present hardware configuration"},
933916f3 482 {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 483 "9027: Array is missing a device and parity is out of sync"},
933916f3 484 {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 485 "9028: Maximum number of arrays already exist"},
933916f3 486 {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 487 "9050: Required cache data cannot be located for a disk unit"},
933916f3 488 {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 489 "9052: Cache data exists for a device that has been modified"},
933916f3 490 {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 491 "9054: IOA resources not available due to previous problems"},
933916f3 492 {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 493 "9092: Disk unit requires initialization before use"},
933916f3 494 {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 495 "9029: Incorrect hardware configuration change has been detected"},
933916f3 496 {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 497 "9060: One or more disk pairs are missing from an array"},
933916f3 498 {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 499 "9061: One or more disks are missing from an array"},
933916f3 500 {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4 501 "9062: One or more disks are missing from an array"},
933916f3 502 {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
1da177e4
LT
503 "9063: Maximum number of functional arrays has been exceeded"},
504 {0x0B260000, 0, 0,
505 "Aborted command, invalid descriptor"},
506 {0x0B5A0000, 0, 0,
507 "Command terminated by host"}
508};
509
510static const struct ipr_ses_table_entry ipr_ses_table[] = {
511 { "2104-DL1 ", "XXXXXXXXXXXXXXXX", 80 },
512 { "2104-TL1 ", "XXXXXXXXXXXXXXXX", 80 },
513 { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
514 { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
515 { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
516 { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
517 { "2104-DU3 ", "XXXXXXXXXXXXXXXX", 160 },
518 { "2104-TU3 ", "XXXXXXXXXXXXXXXX", 160 },
519 { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
520 { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
521 { "St V1S2 ", "XXXXXXXXXXXXXXXX", 160 },
522 { "HSBPD4M PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
523 { "VSBPD1H U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
524};
525
526/*
527 * Function Prototypes
528 */
529static int ipr_reset_alert(struct ipr_cmnd *);
530static void ipr_process_ccn(struct ipr_cmnd *);
531static void ipr_process_error(struct ipr_cmnd *);
532static void ipr_reset_ioa_job(struct ipr_cmnd *);
533static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
534 enum ipr_shutdown_type);
535
536#ifdef CONFIG_SCSI_IPR_TRACE
537/**
538 * ipr_trc_hook - Add a trace entry to the driver trace
539 * @ipr_cmd: ipr command struct
540 * @type: trace type
541 * @add_data: additional data
542 *
543 * Return value:
544 * none
545 **/
546static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
547 u8 type, u32 add_data)
548{
549 struct ipr_trace_entry *trace_entry;
550 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
551
552 trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
553 trace_entry->time = jiffies;
554 trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
555 trace_entry->type = type;
a32c055f
WB
556 if (ipr_cmd->ioa_cfg->sis64)
557 trace_entry->ata_op_code = ipr_cmd->i.ata_ioadl.regs.command;
558 else
559 trace_entry->ata_op_code = ipr_cmd->ioarcb.u.add_data.u.regs.command;
35a39691 560 trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
1da177e4
LT
561 trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
562 trace_entry->u.add_data = add_data;
563}
564#else
565#define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
566#endif
567
568/**
569 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
570 * @ipr_cmd: ipr command struct
571 *
572 * Return value:
573 * none
574 **/
575static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
576{
577 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
96d21f00
WB
578 struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
579 struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
a32c055f 580 dma_addr_t dma_addr = ipr_cmd->dma_addr;
1da177e4
LT
581
582 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
a32c055f 583 ioarcb->data_transfer_length = 0;
1da177e4 584 ioarcb->read_data_transfer_length = 0;
a32c055f 585 ioarcb->ioadl_len = 0;
1da177e4 586 ioarcb->read_ioadl_len = 0;
a32c055f 587
96d21f00 588 if (ipr_cmd->ioa_cfg->sis64) {
a32c055f
WB
589 ioarcb->u.sis64_addr_data.data_ioadl_addr =
590 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
96d21f00
WB
591 ioasa64->u.gata.status = 0;
592 } else {
a32c055f
WB
593 ioarcb->write_ioadl_addr =
594 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
595 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
96d21f00 596 ioasa->u.gata.status = 0;
a32c055f
WB
597 }
598
96d21f00
WB
599 ioasa->hdr.ioasc = 0;
600 ioasa->hdr.residual_data_len = 0;
1da177e4 601 ipr_cmd->scsi_cmd = NULL;
35a39691 602 ipr_cmd->qc = NULL;
1da177e4
LT
603 ipr_cmd->sense_buffer[0] = 0;
604 ipr_cmd->dma_use_sg = 0;
605}
606
607/**
608 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
609 * @ipr_cmd: ipr command struct
610 *
611 * Return value:
612 * none
613 **/
614static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
615{
616 ipr_reinit_ipr_cmnd(ipr_cmd);
617 ipr_cmd->u.scratch = 0;
618 ipr_cmd->sibling = NULL;
619 init_timer(&ipr_cmd->timer);
620}
621
622/**
00bfef2c 623 * __ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
1da177e4
LT
624 * @ioa_cfg: ioa config struct
625 *
626 * Return value:
627 * pointer to ipr command struct
628 **/
629static
00bfef2c 630struct ipr_cmnd *__ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
1da177e4
LT
631{
632 struct ipr_cmnd *ipr_cmd;
633
634 ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
635 list_del(&ipr_cmd->queue);
1da177e4
LT
636
637 return ipr_cmd;
638}
639
00bfef2c
BK
640/**
641 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block and initialize it
642 * @ioa_cfg: ioa config struct
643 *
644 * Return value:
645 * pointer to ipr command struct
646 **/
647static
648struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
649{
650 struct ipr_cmnd *ipr_cmd = __ipr_get_free_ipr_cmnd(ioa_cfg);
651 ipr_init_ipr_cmnd(ipr_cmd);
652 return ipr_cmd;
653}
654
1da177e4
LT
655/**
656 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
657 * @ioa_cfg: ioa config struct
658 * @clr_ints: interrupts to clear
659 *
660 * This function masks all interrupts on the adapter, then clears the
661 * interrupts specified in the mask
662 *
663 * Return value:
664 * none
665 **/
666static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
667 u32 clr_ints)
668{
669 volatile u32 int_reg;
670
671 /* Stop new interrupts */
672 ioa_cfg->allow_interrupts = 0;
673
674 /* Set interrupt mask to stop all new interrupts */
214777ba
WB
675 if (ioa_cfg->sis64)
676 writeq(~0, ioa_cfg->regs.set_interrupt_mask_reg);
677 else
678 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
1da177e4
LT
679
680 /* Clear any pending interrupts */
214777ba
WB
681 if (ioa_cfg->sis64)
682 writel(~0, ioa_cfg->regs.clr_interrupt_reg);
683 writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg32);
1da177e4
LT
684 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
685}
686
687/**
688 * ipr_save_pcix_cmd_reg - Save PCI-X command register
689 * @ioa_cfg: ioa config struct
690 *
691 * Return value:
692 * 0 on success / -EIO on failure
693 **/
694static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
695{
696 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
697
7dce0e1c
BK
698 if (pcix_cmd_reg == 0)
699 return 0;
1da177e4
LT
700
701 if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
702 &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
703 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
704 return -EIO;
705 }
706
707 ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
708 return 0;
709}
710
711/**
712 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
713 * @ioa_cfg: ioa config struct
714 *
715 * Return value:
716 * 0 on success / -EIO on failure
717 **/
718static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
719{
720 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
721
722 if (pcix_cmd_reg) {
723 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
724 ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
725 dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
726 return -EIO;
727 }
1da177e4
LT
728 }
729
730 return 0;
731}
732
35a39691
BK
733/**
734 * ipr_sata_eh_done - done function for aborted SATA commands
735 * @ipr_cmd: ipr command struct
736 *
737 * This function is invoked for ops generated to SATA
738 * devices which are being aborted.
739 *
740 * Return value:
741 * none
742 **/
743static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
744{
745 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
746 struct ata_queued_cmd *qc = ipr_cmd->qc;
747 struct ipr_sata_port *sata_port = qc->ap->private_data;
748
749 qc->err_mask |= AC_ERR_OTHER;
750 sata_port->ioasa.status |= ATA_BUSY;
751 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
752 ata_qc_complete(qc);
753}
754
1da177e4
LT
755/**
756 * ipr_scsi_eh_done - mid-layer done function for aborted ops
757 * @ipr_cmd: ipr command struct
758 *
759 * This function is invoked by the interrupt handler for
760 * ops generated by the SCSI mid-layer which are being aborted.
761 *
762 * Return value:
763 * none
764 **/
765static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
766{
767 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
768 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
769
770 scsi_cmd->result |= (DID_ERROR << 16);
771
63015bc9 772 scsi_dma_unmap(ipr_cmd->scsi_cmd);
1da177e4
LT
773 scsi_cmd->scsi_done(scsi_cmd);
774 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
775}
776
777/**
778 * ipr_fail_all_ops - Fails all outstanding ops.
779 * @ioa_cfg: ioa config struct
780 *
781 * This function fails all outstanding ops.
782 *
783 * Return value:
784 * none
785 **/
786static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
787{
788 struct ipr_cmnd *ipr_cmd, *temp;
789
790 ENTER;
791 list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
792 list_del(&ipr_cmd->queue);
793
96d21f00
WB
794 ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
795 ipr_cmd->s.ioasa.hdr.ilid = cpu_to_be32(IPR_DRIVER_ILID);
1da177e4
LT
796
797 if (ipr_cmd->scsi_cmd)
798 ipr_cmd->done = ipr_scsi_eh_done;
35a39691
BK
799 else if (ipr_cmd->qc)
800 ipr_cmd->done = ipr_sata_eh_done;
1da177e4
LT
801
802 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
803 del_timer(&ipr_cmd->timer);
804 ipr_cmd->done(ipr_cmd);
805 }
806
807 LEAVE;
808}
809
a32c055f
WB
810/**
811 * ipr_send_command - Send driver initiated requests.
812 * @ipr_cmd: ipr command struct
813 *
814 * This function sends a command to the adapter using the correct write call.
815 * In the case of sis64, calculate the ioarcb size required. Then or in the
816 * appropriate bits.
817 *
818 * Return value:
819 * none
820 **/
821static void ipr_send_command(struct ipr_cmnd *ipr_cmd)
822{
823 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
824 dma_addr_t send_dma_addr = ipr_cmd->dma_addr;
825
826 if (ioa_cfg->sis64) {
827 /* The default size is 256 bytes */
828 send_dma_addr |= 0x1;
829
830 /* If the number of ioadls * size of ioadl > 128 bytes,
831 then use a 512 byte ioarcb */
832 if (ipr_cmd->dma_use_sg * sizeof(struct ipr_ioadl64_desc) > 128 )
833 send_dma_addr |= 0x4;
834 writeq(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
835 } else
836 writel(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
837}
838
1da177e4
LT
839/**
840 * ipr_do_req - Send driver initiated requests.
841 * @ipr_cmd: ipr command struct
842 * @done: done function
843 * @timeout_func: timeout function
844 * @timeout: timeout value
845 *
846 * This function sends the specified command to the adapter with the
847 * timeout given. The done function is invoked on command completion.
848 *
849 * Return value:
850 * none
851 **/
852static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
853 void (*done) (struct ipr_cmnd *),
854 void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
855{
856 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
857
858 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
859
860 ipr_cmd->done = done;
861
862 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
863 ipr_cmd->timer.expires = jiffies + timeout;
864 ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
865
866 add_timer(&ipr_cmd->timer);
867
868 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
869
a32c055f 870 ipr_send_command(ipr_cmd);
1da177e4
LT
871}
872
873/**
874 * ipr_internal_cmd_done - Op done function for an internally generated op.
875 * @ipr_cmd: ipr command struct
876 *
877 * This function is the op done function for an internally generated,
878 * blocking op. It simply wakes the sleeping thread.
879 *
880 * Return value:
881 * none
882 **/
883static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
884{
885 if (ipr_cmd->sibling)
886 ipr_cmd->sibling = NULL;
887 else
888 complete(&ipr_cmd->completion);
889}
890
a32c055f
WB
891/**
892 * ipr_init_ioadl - initialize the ioadl for the correct SIS type
893 * @ipr_cmd: ipr command struct
894 * @dma_addr: dma address
895 * @len: transfer length
896 * @flags: ioadl flag value
897 *
898 * This function initializes an ioadl in the case where there is only a single
899 * descriptor.
900 *
901 * Return value:
902 * nothing
903 **/
904static void ipr_init_ioadl(struct ipr_cmnd *ipr_cmd, dma_addr_t dma_addr,
905 u32 len, int flags)
906{
907 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
908 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
909
910 ipr_cmd->dma_use_sg = 1;
911
912 if (ipr_cmd->ioa_cfg->sis64) {
913 ioadl64->flags = cpu_to_be32(flags);
914 ioadl64->data_len = cpu_to_be32(len);
915 ioadl64->address = cpu_to_be64(dma_addr);
916
917 ipr_cmd->ioarcb.ioadl_len =
918 cpu_to_be32(sizeof(struct ipr_ioadl64_desc));
919 ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
920 } else {
921 ioadl->flags_and_data_len = cpu_to_be32(flags | len);
922 ioadl->address = cpu_to_be32(dma_addr);
923
924 if (flags == IPR_IOADL_FLAGS_READ_LAST) {
925 ipr_cmd->ioarcb.read_ioadl_len =
926 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
927 ipr_cmd->ioarcb.read_data_transfer_length = cpu_to_be32(len);
928 } else {
929 ipr_cmd->ioarcb.ioadl_len =
930 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
931 ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
932 }
933 }
934}
935
1da177e4
LT
936/**
937 * ipr_send_blocking_cmd - Send command and sleep on its completion.
938 * @ipr_cmd: ipr command struct
939 * @timeout_func: function to invoke if command times out
940 * @timeout: timeout
941 *
942 * Return value:
943 * none
944 **/
945static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
946 void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
947 u32 timeout)
948{
949 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
950
951 init_completion(&ipr_cmd->completion);
952 ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
953
954 spin_unlock_irq(ioa_cfg->host->host_lock);
955 wait_for_completion(&ipr_cmd->completion);
956 spin_lock_irq(ioa_cfg->host->host_lock);
957}
958
959/**
960 * ipr_send_hcam - Send an HCAM to the adapter.
961 * @ioa_cfg: ioa config struct
962 * @type: HCAM type
963 * @hostrcb: hostrcb struct
964 *
965 * This function will send a Host Controlled Async command to the adapter.
966 * If HCAMs are currently not allowed to be issued to the adapter, it will
967 * place the hostrcb on the free queue.
968 *
969 * Return value:
970 * none
971 **/
972static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
973 struct ipr_hostrcb *hostrcb)
974{
975 struct ipr_cmnd *ipr_cmd;
976 struct ipr_ioarcb *ioarcb;
977
978 if (ioa_cfg->allow_cmds) {
979 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
980 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
981 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
982
983 ipr_cmd->u.hostrcb = hostrcb;
984 ioarcb = &ipr_cmd->ioarcb;
985
986 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
987 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
988 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
989 ioarcb->cmd_pkt.cdb[1] = type;
990 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
991 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
992
a32c055f
WB
993 ipr_init_ioadl(ipr_cmd, hostrcb->hostrcb_dma,
994 sizeof(hostrcb->hcam), IPR_IOADL_FLAGS_READ_LAST);
1da177e4
LT
995
996 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
997 ipr_cmd->done = ipr_process_ccn;
998 else
999 ipr_cmd->done = ipr_process_error;
1000
1001 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
1002
a32c055f 1003 ipr_send_command(ipr_cmd);
1da177e4
LT
1004 } else {
1005 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
1006 }
1007}
1008
3e7ebdfa
WB
1009/**
1010 * ipr_update_ata_class - Update the ata class in the resource entry
1011 * @res: resource entry struct
1012 * @proto: cfgte device bus protocol value
1013 *
1014 * Return value:
1015 * none
1016 **/
1017static void ipr_update_ata_class(struct ipr_resource_entry *res, unsigned int proto)
1018{
1019 switch(proto) {
1020 case IPR_PROTO_SATA:
1021 case IPR_PROTO_SAS_STP:
1022 res->ata_class = ATA_DEV_ATA;
1023 break;
1024 case IPR_PROTO_SATA_ATAPI:
1025 case IPR_PROTO_SAS_STP_ATAPI:
1026 res->ata_class = ATA_DEV_ATAPI;
1027 break;
1028 default:
1029 res->ata_class = ATA_DEV_UNKNOWN;
1030 break;
1031 };
1032}
1033
1da177e4
LT
1034/**
1035 * ipr_init_res_entry - Initialize a resource entry struct.
1036 * @res: resource entry struct
3e7ebdfa 1037 * @cfgtew: config table entry wrapper struct
1da177e4
LT
1038 *
1039 * Return value:
1040 * none
1041 **/
3e7ebdfa
WB
1042static void ipr_init_res_entry(struct ipr_resource_entry *res,
1043 struct ipr_config_table_entry_wrapper *cfgtew)
1da177e4 1044{
3e7ebdfa
WB
1045 int found = 0;
1046 unsigned int proto;
1047 struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
1048 struct ipr_resource_entry *gscsi_res = NULL;
1049
ee0a90fa 1050 res->needs_sync_complete = 0;
1da177e4
LT
1051 res->in_erp = 0;
1052 res->add_to_ml = 0;
1053 res->del_from_ml = 0;
1054 res->resetting_device = 0;
1055 res->sdev = NULL;
35a39691 1056 res->sata_port = NULL;
3e7ebdfa
WB
1057
1058 if (ioa_cfg->sis64) {
1059 proto = cfgtew->u.cfgte64->proto;
1060 res->res_flags = cfgtew->u.cfgte64->res_flags;
1061 res->qmodel = IPR_QUEUEING_MODEL64(res);
438b0331 1062 res->type = cfgtew->u.cfgte64->res_type;
3e7ebdfa
WB
1063
1064 memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1065 sizeof(res->res_path));
1066
1067 res->bus = 0;
0cb992ed
WB
1068 memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1069 sizeof(res->dev_lun.scsi_lun));
3e7ebdfa
WB
1070 res->lun = scsilun_to_int(&res->dev_lun);
1071
1072 if (res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1073 list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue) {
1074 if (gscsi_res->dev_id == cfgtew->u.cfgte64->dev_id) {
1075 found = 1;
1076 res->target = gscsi_res->target;
1077 break;
1078 }
1079 }
1080 if (!found) {
1081 res->target = find_first_zero_bit(ioa_cfg->target_ids,
1082 ioa_cfg->max_devs_supported);
1083 set_bit(res->target, ioa_cfg->target_ids);
1084 }
3e7ebdfa
WB
1085 } else if (res->type == IPR_RES_TYPE_IOAFP) {
1086 res->bus = IPR_IOAFP_VIRTUAL_BUS;
1087 res->target = 0;
1088 } else if (res->type == IPR_RES_TYPE_ARRAY) {
1089 res->bus = IPR_ARRAY_VIRTUAL_BUS;
1090 res->target = find_first_zero_bit(ioa_cfg->array_ids,
1091 ioa_cfg->max_devs_supported);
1092 set_bit(res->target, ioa_cfg->array_ids);
1093 } else if (res->type == IPR_RES_TYPE_VOLUME_SET) {
1094 res->bus = IPR_VSET_VIRTUAL_BUS;
1095 res->target = find_first_zero_bit(ioa_cfg->vset_ids,
1096 ioa_cfg->max_devs_supported);
1097 set_bit(res->target, ioa_cfg->vset_ids);
1098 } else {
1099 res->target = find_first_zero_bit(ioa_cfg->target_ids,
1100 ioa_cfg->max_devs_supported);
1101 set_bit(res->target, ioa_cfg->target_ids);
1102 }
1103 } else {
1104 proto = cfgtew->u.cfgte->proto;
1105 res->qmodel = IPR_QUEUEING_MODEL(res);
1106 res->flags = cfgtew->u.cfgte->flags;
1107 if (res->flags & IPR_IS_IOA_RESOURCE)
1108 res->type = IPR_RES_TYPE_IOAFP;
1109 else
1110 res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1111
1112 res->bus = cfgtew->u.cfgte->res_addr.bus;
1113 res->target = cfgtew->u.cfgte->res_addr.target;
1114 res->lun = cfgtew->u.cfgte->res_addr.lun;
46d74563 1115 res->lun_wwn = get_unaligned_be64(cfgtew->u.cfgte->lun_wwn);
3e7ebdfa
WB
1116 }
1117
1118 ipr_update_ata_class(res, proto);
1119}
1120
1121/**
1122 * ipr_is_same_device - Determine if two devices are the same.
1123 * @res: resource entry struct
1124 * @cfgtew: config table entry wrapper struct
1125 *
1126 * Return value:
1127 * 1 if the devices are the same / 0 otherwise
1128 **/
1129static int ipr_is_same_device(struct ipr_resource_entry *res,
1130 struct ipr_config_table_entry_wrapper *cfgtew)
1131{
1132 if (res->ioa_cfg->sis64) {
1133 if (!memcmp(&res->dev_id, &cfgtew->u.cfgte64->dev_id,
1134 sizeof(cfgtew->u.cfgte64->dev_id)) &&
0cb992ed 1135 !memcmp(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
3e7ebdfa
WB
1136 sizeof(cfgtew->u.cfgte64->lun))) {
1137 return 1;
1138 }
1139 } else {
1140 if (res->bus == cfgtew->u.cfgte->res_addr.bus &&
1141 res->target == cfgtew->u.cfgte->res_addr.target &&
1142 res->lun == cfgtew->u.cfgte->res_addr.lun)
1143 return 1;
1144 }
1145
1146 return 0;
1147}
1148
1149/**
5adcbeb3 1150 * ipr_format_res_path - Format the resource path for printing.
3e7ebdfa
WB
1151 * @res_path: resource path
1152 * @buf: buffer
1153 *
1154 * Return value:
1155 * pointer to buffer
1156 **/
5adcbeb3 1157static char *ipr_format_res_path(u8 *res_path, char *buffer, int len)
3e7ebdfa
WB
1158{
1159 int i;
5adcbeb3 1160 char *p = buffer;
3e7ebdfa 1161
46d74563 1162 *p = '\0';
5adcbeb3
WB
1163 p += snprintf(p, buffer + len - p, "%02X", res_path[0]);
1164 for (i = 1; res_path[i] != 0xff && ((i * 3) < len); i++)
1165 p += snprintf(p, buffer + len - p, "-%02X", res_path[i]);
3e7ebdfa
WB
1166
1167 return buffer;
1168}
1169
1170/**
1171 * ipr_update_res_entry - Update the resource entry.
1172 * @res: resource entry struct
1173 * @cfgtew: config table entry wrapper struct
1174 *
1175 * Return value:
1176 * none
1177 **/
1178static void ipr_update_res_entry(struct ipr_resource_entry *res,
1179 struct ipr_config_table_entry_wrapper *cfgtew)
1180{
1181 char buffer[IPR_MAX_RES_PATH_LENGTH];
1182 unsigned int proto;
1183 int new_path = 0;
1184
1185 if (res->ioa_cfg->sis64) {
1186 res->flags = cfgtew->u.cfgte64->flags;
1187 res->res_flags = cfgtew->u.cfgte64->res_flags;
75576bb9 1188 res->type = cfgtew->u.cfgte64->res_type;
3e7ebdfa
WB
1189
1190 memcpy(&res->std_inq_data, &cfgtew->u.cfgte64->std_inq_data,
1191 sizeof(struct ipr_std_inq_data));
1192
1193 res->qmodel = IPR_QUEUEING_MODEL64(res);
1194 proto = cfgtew->u.cfgte64->proto;
1195 res->res_handle = cfgtew->u.cfgte64->res_handle;
1196 res->dev_id = cfgtew->u.cfgte64->dev_id;
1197
1198 memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
1199 sizeof(res->dev_lun.scsi_lun));
1200
1201 if (memcmp(res->res_path, &cfgtew->u.cfgte64->res_path,
1202 sizeof(res->res_path))) {
1203 memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
1204 sizeof(res->res_path));
1205 new_path = 1;
1206 }
1207
1208 if (res->sdev && new_path)
1209 sdev_printk(KERN_INFO, res->sdev, "Resource path: %s\n",
5adcbeb3
WB
1210 ipr_format_res_path(res->res_path, buffer,
1211 sizeof(buffer)));
3e7ebdfa
WB
1212 } else {
1213 res->flags = cfgtew->u.cfgte->flags;
1214 if (res->flags & IPR_IS_IOA_RESOURCE)
1215 res->type = IPR_RES_TYPE_IOAFP;
1216 else
1217 res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
1218
1219 memcpy(&res->std_inq_data, &cfgtew->u.cfgte->std_inq_data,
1220 sizeof(struct ipr_std_inq_data));
1221
1222 res->qmodel = IPR_QUEUEING_MODEL(res);
1223 proto = cfgtew->u.cfgte->proto;
1224 res->res_handle = cfgtew->u.cfgte->res_handle;
1225 }
1226
1227 ipr_update_ata_class(res, proto);
1228}
1229
1230/**
1231 * ipr_clear_res_target - Clear the bit in the bit map representing the target
1232 * for the resource.
1233 * @res: resource entry struct
1234 * @cfgtew: config table entry wrapper struct
1235 *
1236 * Return value:
1237 * none
1238 **/
1239static void ipr_clear_res_target(struct ipr_resource_entry *res)
1240{
1241 struct ipr_resource_entry *gscsi_res = NULL;
1242 struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
1243
1244 if (!ioa_cfg->sis64)
1245 return;
1246
1247 if (res->bus == IPR_ARRAY_VIRTUAL_BUS)
1248 clear_bit(res->target, ioa_cfg->array_ids);
1249 else if (res->bus == IPR_VSET_VIRTUAL_BUS)
1250 clear_bit(res->target, ioa_cfg->vset_ids);
1251 else if (res->bus == 0 && res->type == IPR_RES_TYPE_GENERIC_SCSI) {
1252 list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue)
1253 if (gscsi_res->dev_id == res->dev_id && gscsi_res != res)
1254 return;
1255 clear_bit(res->target, ioa_cfg->target_ids);
1256
1257 } else if (res->bus == 0)
1258 clear_bit(res->target, ioa_cfg->target_ids);
1da177e4
LT
1259}
1260
1261/**
1262 * ipr_handle_config_change - Handle a config change from the adapter
1263 * @ioa_cfg: ioa config struct
1264 * @hostrcb: hostrcb
1265 *
1266 * Return value:
1267 * none
1268 **/
1269static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
3e7ebdfa 1270 struct ipr_hostrcb *hostrcb)
1da177e4
LT
1271{
1272 struct ipr_resource_entry *res = NULL;
3e7ebdfa
WB
1273 struct ipr_config_table_entry_wrapper cfgtew;
1274 __be32 cc_res_handle;
1275
1da177e4
LT
1276 u32 is_ndn = 1;
1277
3e7ebdfa
WB
1278 if (ioa_cfg->sis64) {
1279 cfgtew.u.cfgte64 = &hostrcb->hcam.u.ccn.u.cfgte64;
1280 cc_res_handle = cfgtew.u.cfgte64->res_handle;
1281 } else {
1282 cfgtew.u.cfgte = &hostrcb->hcam.u.ccn.u.cfgte;
1283 cc_res_handle = cfgtew.u.cfgte->res_handle;
1284 }
1da177e4
LT
1285
1286 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3e7ebdfa 1287 if (res->res_handle == cc_res_handle) {
1da177e4
LT
1288 is_ndn = 0;
1289 break;
1290 }
1291 }
1292
1293 if (is_ndn) {
1294 if (list_empty(&ioa_cfg->free_res_q)) {
1295 ipr_send_hcam(ioa_cfg,
1296 IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
1297 hostrcb);
1298 return;
1299 }
1300
1301 res = list_entry(ioa_cfg->free_res_q.next,
1302 struct ipr_resource_entry, queue);
1303
1304 list_del(&res->queue);
3e7ebdfa 1305 ipr_init_res_entry(res, &cfgtew);
1da177e4
LT
1306 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
1307 }
1308
3e7ebdfa 1309 ipr_update_res_entry(res, &cfgtew);
1da177e4
LT
1310
1311 if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
1312 if (res->sdev) {
1da177e4 1313 res->del_from_ml = 1;
3e7ebdfa 1314 res->res_handle = IPR_INVALID_RES_HANDLE;
1da177e4
LT
1315 if (ioa_cfg->allow_ml_add_del)
1316 schedule_work(&ioa_cfg->work_q);
3e7ebdfa
WB
1317 } else {
1318 ipr_clear_res_target(res);
1da177e4 1319 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
3e7ebdfa 1320 }
5767a1c4 1321 } else if (!res->sdev || res->del_from_ml) {
1da177e4
LT
1322 res->add_to_ml = 1;
1323 if (ioa_cfg->allow_ml_add_del)
1324 schedule_work(&ioa_cfg->work_q);
1325 }
1326
1327 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1328}
1329
1330/**
1331 * ipr_process_ccn - Op done function for a CCN.
1332 * @ipr_cmd: ipr command struct
1333 *
1334 * This function is the op done function for a configuration
1335 * change notification host controlled async from the adapter.
1336 *
1337 * Return value:
1338 * none
1339 **/
1340static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
1341{
1342 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1343 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
96d21f00 1344 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
1345
1346 list_del(&hostrcb->queue);
1347 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1348
1349 if (ioasc) {
1350 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
1351 dev_err(&ioa_cfg->pdev->dev,
1352 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1353
1354 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
1355 } else {
1356 ipr_handle_config_change(ioa_cfg, hostrcb);
1357 }
1358}
1359
8cf093e2
BK
1360/**
1361 * strip_and_pad_whitespace - Strip and pad trailing whitespace.
1362 * @i: index into buffer
1363 * @buf: string to modify
1364 *
1365 * This function will strip all trailing whitespace, pad the end
1366 * of the string with a single space, and NULL terminate the string.
1367 *
1368 * Return value:
1369 * new length of string
1370 **/
1371static int strip_and_pad_whitespace(int i, char *buf)
1372{
1373 while (i && buf[i] == ' ')
1374 i--;
1375 buf[i+1] = ' ';
1376 buf[i+2] = '\0';
1377 return i + 2;
1378}
1379
1380/**
1381 * ipr_log_vpd_compact - Log the passed extended VPD compactly.
1382 * @prefix: string to print at start of printk
1383 * @hostrcb: hostrcb pointer
1384 * @vpd: vendor/product id/sn struct
1385 *
1386 * Return value:
1387 * none
1388 **/
1389static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1390 struct ipr_vpd *vpd)
1391{
1392 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
1393 int i = 0;
1394
1395 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1396 i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
1397
1398 memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
1399 i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
1400
1401 memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
1402 buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
1403
1404 ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
1405}
1406
1da177e4
LT
1407/**
1408 * ipr_log_vpd - Log the passed VPD to the error log.
cfc32139 1409 * @vpd: vendor/product id/sn struct
1da177e4
LT
1410 *
1411 * Return value:
1412 * none
1413 **/
cfc32139 1414static void ipr_log_vpd(struct ipr_vpd *vpd)
1da177e4
LT
1415{
1416 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
1417 + IPR_SERIAL_NUM_LEN];
1418
cfc32139 1419 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
1420 memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
1da177e4
LT
1421 IPR_PROD_ID_LEN);
1422 buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
1423 ipr_err("Vendor/Product ID: %s\n", buffer);
1424
cfc32139 1425 memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
1da177e4
LT
1426 buffer[IPR_SERIAL_NUM_LEN] = '\0';
1427 ipr_err(" Serial Number: %s\n", buffer);
1428}
1429
8cf093e2
BK
1430/**
1431 * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
1432 * @prefix: string to print at start of printk
1433 * @hostrcb: hostrcb pointer
1434 * @vpd: vendor/product id/sn/wwn struct
1435 *
1436 * Return value:
1437 * none
1438 **/
1439static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
1440 struct ipr_ext_vpd *vpd)
1441{
1442 ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
1443 ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
1444 be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
1445}
1446
ee0f05b8 1447/**
1448 * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
1449 * @vpd: vendor/product id/sn/wwn struct
1450 *
1451 * Return value:
1452 * none
1453 **/
1454static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
1455{
1456 ipr_log_vpd(&vpd->vpd);
1457 ipr_err(" WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
1458 be32_to_cpu(vpd->wwid[1]));
1459}
1460
1461/**
1462 * ipr_log_enhanced_cache_error - Log a cache error.
1463 * @ioa_cfg: ioa config struct
1464 * @hostrcb: hostrcb struct
1465 *
1466 * Return value:
1467 * none
1468 **/
1469static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1470 struct ipr_hostrcb *hostrcb)
1471{
4565e370
WB
1472 struct ipr_hostrcb_type_12_error *error;
1473
1474 if (ioa_cfg->sis64)
1475 error = &hostrcb->hcam.u.error64.u.type_12_error;
1476 else
1477 error = &hostrcb->hcam.u.error.u.type_12_error;
ee0f05b8 1478
1479 ipr_err("-----Current Configuration-----\n");
1480 ipr_err("Cache Directory Card Information:\n");
1481 ipr_log_ext_vpd(&error->ioa_vpd);
1482 ipr_err("Adapter Card Information:\n");
1483 ipr_log_ext_vpd(&error->cfc_vpd);
1484
1485 ipr_err("-----Expected Configuration-----\n");
1486 ipr_err("Cache Directory Card Information:\n");
1487 ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
1488 ipr_err("Adapter Card Information:\n");
1489 ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
1490
1491 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1492 be32_to_cpu(error->ioa_data[0]),
1493 be32_to_cpu(error->ioa_data[1]),
1494 be32_to_cpu(error->ioa_data[2]));
1495}
1496
1da177e4
LT
1497/**
1498 * ipr_log_cache_error - Log a cache error.
1499 * @ioa_cfg: ioa config struct
1500 * @hostrcb: hostrcb struct
1501 *
1502 * Return value:
1503 * none
1504 **/
1505static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
1506 struct ipr_hostrcb *hostrcb)
1507{
1508 struct ipr_hostrcb_type_02_error *error =
1509 &hostrcb->hcam.u.error.u.type_02_error;
1510
1511 ipr_err("-----Current Configuration-----\n");
1512 ipr_err("Cache Directory Card Information:\n");
cfc32139 1513 ipr_log_vpd(&error->ioa_vpd);
1da177e4 1514 ipr_err("Adapter Card Information:\n");
cfc32139 1515 ipr_log_vpd(&error->cfc_vpd);
1da177e4
LT
1516
1517 ipr_err("-----Expected Configuration-----\n");
1518 ipr_err("Cache Directory Card Information:\n");
cfc32139 1519 ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
1da177e4 1520 ipr_err("Adapter Card Information:\n");
cfc32139 1521 ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
1da177e4
LT
1522
1523 ipr_err("Additional IOA Data: %08X %08X %08X\n",
1524 be32_to_cpu(error->ioa_data[0]),
1525 be32_to_cpu(error->ioa_data[1]),
1526 be32_to_cpu(error->ioa_data[2]));
1527}
1528
ee0f05b8 1529/**
1530 * ipr_log_enhanced_config_error - Log a configuration error.
1531 * @ioa_cfg: ioa config struct
1532 * @hostrcb: hostrcb struct
1533 *
1534 * Return value:
1535 * none
1536 **/
1537static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
1538 struct ipr_hostrcb *hostrcb)
1539{
1540 int errors_logged, i;
1541 struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
1542 struct ipr_hostrcb_type_13_error *error;
1543
1544 error = &hostrcb->hcam.u.error.u.type_13_error;
1545 errors_logged = be32_to_cpu(error->errors_logged);
1546
1547 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1548 be32_to_cpu(error->errors_detected), errors_logged);
1549
1550 dev_entry = error->dev;
1551
1552 for (i = 0; i < errors_logged; i++, dev_entry++) {
1553 ipr_err_separator;
1554
1555 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
1556 ipr_log_ext_vpd(&dev_entry->vpd);
1557
1558 ipr_err("-----New Device Information-----\n");
1559 ipr_log_ext_vpd(&dev_entry->new_vpd);
1560
1561 ipr_err("Cache Directory Card Information:\n");
1562 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1563
1564 ipr_err("Adapter Card Information:\n");
1565 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1566 }
1567}
1568
4565e370
WB
1569/**
1570 * ipr_log_sis64_config_error - Log a device error.
1571 * @ioa_cfg: ioa config struct
1572 * @hostrcb: hostrcb struct
1573 *
1574 * Return value:
1575 * none
1576 **/
1577static void ipr_log_sis64_config_error(struct ipr_ioa_cfg *ioa_cfg,
1578 struct ipr_hostrcb *hostrcb)
1579{
1580 int errors_logged, i;
1581 struct ipr_hostrcb64_device_data_entry_enhanced *dev_entry;
1582 struct ipr_hostrcb_type_23_error *error;
1583 char buffer[IPR_MAX_RES_PATH_LENGTH];
1584
1585 error = &hostrcb->hcam.u.error64.u.type_23_error;
1586 errors_logged = be32_to_cpu(error->errors_logged);
1587
1588 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1589 be32_to_cpu(error->errors_detected), errors_logged);
1590
1591 dev_entry = error->dev;
1592
1593 for (i = 0; i < errors_logged; i++, dev_entry++) {
1594 ipr_err_separator;
1595
1596 ipr_err("Device %d : %s", i + 1,
5adcbeb3
WB
1597 ipr_format_res_path(dev_entry->res_path, buffer,
1598 sizeof(buffer)));
4565e370
WB
1599 ipr_log_ext_vpd(&dev_entry->vpd);
1600
1601 ipr_err("-----New Device Information-----\n");
1602 ipr_log_ext_vpd(&dev_entry->new_vpd);
1603
1604 ipr_err("Cache Directory Card Information:\n");
1605 ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
1606
1607 ipr_err("Adapter Card Information:\n");
1608 ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
1609 }
1610}
1611
1da177e4
LT
1612/**
1613 * ipr_log_config_error - Log a configuration error.
1614 * @ioa_cfg: ioa config struct
1615 * @hostrcb: hostrcb struct
1616 *
1617 * Return value:
1618 * none
1619 **/
1620static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
1621 struct ipr_hostrcb *hostrcb)
1622{
1623 int errors_logged, i;
1624 struct ipr_hostrcb_device_data_entry *dev_entry;
1625 struct ipr_hostrcb_type_03_error *error;
1626
1627 error = &hostrcb->hcam.u.error.u.type_03_error;
1628 errors_logged = be32_to_cpu(error->errors_logged);
1629
1630 ipr_err("Device Errors Detected/Logged: %d/%d\n",
1631 be32_to_cpu(error->errors_detected), errors_logged);
1632
cfc32139 1633 dev_entry = error->dev;
1da177e4
LT
1634
1635 for (i = 0; i < errors_logged; i++, dev_entry++) {
1636 ipr_err_separator;
1637
fa15b1f6 1638 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
cfc32139 1639 ipr_log_vpd(&dev_entry->vpd);
1da177e4
LT
1640
1641 ipr_err("-----New Device Information-----\n");
cfc32139 1642 ipr_log_vpd(&dev_entry->new_vpd);
1da177e4
LT
1643
1644 ipr_err("Cache Directory Card Information:\n");
cfc32139 1645 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
1da177e4
LT
1646
1647 ipr_err("Adapter Card Information:\n");
cfc32139 1648 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
1da177e4
LT
1649
1650 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1651 be32_to_cpu(dev_entry->ioa_data[0]),
1652 be32_to_cpu(dev_entry->ioa_data[1]),
1653 be32_to_cpu(dev_entry->ioa_data[2]),
1654 be32_to_cpu(dev_entry->ioa_data[3]),
1655 be32_to_cpu(dev_entry->ioa_data[4]));
1656 }
1657}
1658
ee0f05b8 1659/**
1660 * ipr_log_enhanced_array_error - Log an array configuration error.
1661 * @ioa_cfg: ioa config struct
1662 * @hostrcb: hostrcb struct
1663 *
1664 * Return value:
1665 * none
1666 **/
1667static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
1668 struct ipr_hostrcb *hostrcb)
1669{
1670 int i, num_entries;
1671 struct ipr_hostrcb_type_14_error *error;
1672 struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
1673 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1674
1675 error = &hostrcb->hcam.u.error.u.type_14_error;
1676
1677 ipr_err_separator;
1678
1679 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1680 error->protection_level,
1681 ioa_cfg->host->host_no,
1682 error->last_func_vset_res_addr.bus,
1683 error->last_func_vset_res_addr.target,
1684 error->last_func_vset_res_addr.lun);
1685
1686 ipr_err_separator;
1687
1688 array_entry = error->array_member;
1689 num_entries = min_t(u32, be32_to_cpu(error->num_entries),
7262026f 1690 ARRAY_SIZE(error->array_member));
ee0f05b8 1691
1692 for (i = 0; i < num_entries; i++, array_entry++) {
1693 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1694 continue;
1695
1696 if (be32_to_cpu(error->exposed_mode_adn) == i)
1697 ipr_err("Exposed Array Member %d:\n", i);
1698 else
1699 ipr_err("Array Member %d:\n", i);
1700
1701 ipr_log_ext_vpd(&array_entry->vpd);
1702 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1703 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1704 "Expected Location");
1705
1706 ipr_err_separator;
1707 }
1708}
1709
1da177e4
LT
1710/**
1711 * ipr_log_array_error - Log an array configuration error.
1712 * @ioa_cfg: ioa config struct
1713 * @hostrcb: hostrcb struct
1714 *
1715 * Return value:
1716 * none
1717 **/
1718static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1719 struct ipr_hostrcb *hostrcb)
1720{
1721 int i;
1722 struct ipr_hostrcb_type_04_error *error;
1723 struct ipr_hostrcb_array_data_entry *array_entry;
1724 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1725
1726 error = &hostrcb->hcam.u.error.u.type_04_error;
1727
1728 ipr_err_separator;
1729
1730 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1731 error->protection_level,
1732 ioa_cfg->host->host_no,
1733 error->last_func_vset_res_addr.bus,
1734 error->last_func_vset_res_addr.target,
1735 error->last_func_vset_res_addr.lun);
1736
1737 ipr_err_separator;
1738
1739 array_entry = error->array_member;
1740
1741 for (i = 0; i < 18; i++) {
cfc32139 1742 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1da177e4
LT
1743 continue;
1744
fa15b1f6 1745 if (be32_to_cpu(error->exposed_mode_adn) == i)
1da177e4 1746 ipr_err("Exposed Array Member %d:\n", i);
fa15b1f6 1747 else
1da177e4 1748 ipr_err("Array Member %d:\n", i);
1da177e4 1749
cfc32139 1750 ipr_log_vpd(&array_entry->vpd);
1da177e4 1751
fa15b1f6 1752 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1753 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1754 "Expected Location");
1da177e4
LT
1755
1756 ipr_err_separator;
1757
1758 if (i == 9)
1759 array_entry = error->array_member2;
1760 else
1761 array_entry++;
1762 }
1763}
1764
1765/**
b0df54bb 1766 * ipr_log_hex_data - Log additional hex IOA error data.
ac719aba 1767 * @ioa_cfg: ioa config struct
b0df54bb 1768 * @data: IOA error data
1769 * @len: data length
1da177e4
LT
1770 *
1771 * Return value:
1772 * none
1773 **/
ac719aba 1774static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
1da177e4
LT
1775{
1776 int i;
1da177e4 1777
b0df54bb 1778 if (len == 0)
1da177e4
LT
1779 return;
1780
ac719aba
BK
1781 if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
1782 len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
1783
b0df54bb 1784 for (i = 0; i < len / 4; i += 4) {
1da177e4 1785 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
b0df54bb 1786 be32_to_cpu(data[i]),
1787 be32_to_cpu(data[i+1]),
1788 be32_to_cpu(data[i+2]),
1789 be32_to_cpu(data[i+3]));
1da177e4
LT
1790 }
1791}
1792
ee0f05b8 1793/**
1794 * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
1795 * @ioa_cfg: ioa config struct
1796 * @hostrcb: hostrcb struct
1797 *
1798 * Return value:
1799 * none
1800 **/
1801static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1802 struct ipr_hostrcb *hostrcb)
1803{
1804 struct ipr_hostrcb_type_17_error *error;
1805
4565e370
WB
1806 if (ioa_cfg->sis64)
1807 error = &hostrcb->hcam.u.error64.u.type_17_error;
1808 else
1809 error = &hostrcb->hcam.u.error.u.type_17_error;
1810
ee0f05b8 1811 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
ca54cb8c 1812 strim(error->failure_reason);
ee0f05b8 1813
8cf093e2
BK
1814 ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1815 be32_to_cpu(hostrcb->hcam.u.error.prc));
1816 ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
ac719aba 1817 ipr_log_hex_data(ioa_cfg, error->data,
ee0f05b8 1818 be32_to_cpu(hostrcb->hcam.length) -
1819 (offsetof(struct ipr_hostrcb_error, u) +
1820 offsetof(struct ipr_hostrcb_type_17_error, data)));
1821}
1822
b0df54bb 1823/**
1824 * ipr_log_dual_ioa_error - Log a dual adapter error.
1825 * @ioa_cfg: ioa config struct
1826 * @hostrcb: hostrcb struct
1827 *
1828 * Return value:
1829 * none
1830 **/
1831static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1832 struct ipr_hostrcb *hostrcb)
1833{
1834 struct ipr_hostrcb_type_07_error *error;
1835
1836 error = &hostrcb->hcam.u.error.u.type_07_error;
1837 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
ca54cb8c 1838 strim(error->failure_reason);
b0df54bb 1839
8cf093e2
BK
1840 ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
1841 be32_to_cpu(hostrcb->hcam.u.error.prc));
1842 ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
ac719aba 1843 ipr_log_hex_data(ioa_cfg, error->data,
b0df54bb 1844 be32_to_cpu(hostrcb->hcam.length) -
1845 (offsetof(struct ipr_hostrcb_error, u) +
1846 offsetof(struct ipr_hostrcb_type_07_error, data)));
1847}
1848
49dc6a18
BK
1849static const struct {
1850 u8 active;
1851 char *desc;
1852} path_active_desc[] = {
1853 { IPR_PATH_NO_INFO, "Path" },
1854 { IPR_PATH_ACTIVE, "Active path" },
1855 { IPR_PATH_NOT_ACTIVE, "Inactive path" }
1856};
1857
1858static const struct {
1859 u8 state;
1860 char *desc;
1861} path_state_desc[] = {
1862 { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
1863 { IPR_PATH_HEALTHY, "is healthy" },
1864 { IPR_PATH_DEGRADED, "is degraded" },
1865 { IPR_PATH_FAILED, "is failed" }
1866};
1867
1868/**
1869 * ipr_log_fabric_path - Log a fabric path error
1870 * @hostrcb: hostrcb struct
1871 * @fabric: fabric descriptor
1872 *
1873 * Return value:
1874 * none
1875 **/
1876static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
1877 struct ipr_hostrcb_fabric_desc *fabric)
1878{
1879 int i, j;
1880 u8 path_state = fabric->path_state;
1881 u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1882 u8 state = path_state & IPR_PATH_STATE_MASK;
1883
1884 for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1885 if (path_active_desc[i].active != active)
1886 continue;
1887
1888 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1889 if (path_state_desc[j].state != state)
1890 continue;
1891
1892 if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
1893 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
1894 path_active_desc[i].desc, path_state_desc[j].desc,
1895 fabric->ioa_port);
1896 } else if (fabric->cascaded_expander == 0xff) {
1897 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
1898 path_active_desc[i].desc, path_state_desc[j].desc,
1899 fabric->ioa_port, fabric->phy);
1900 } else if (fabric->phy == 0xff) {
1901 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
1902 path_active_desc[i].desc, path_state_desc[j].desc,
1903 fabric->ioa_port, fabric->cascaded_expander);
1904 } else {
1905 ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
1906 path_active_desc[i].desc, path_state_desc[j].desc,
1907 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1908 }
1909 return;
1910 }
1911 }
1912
1913 ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
1914 fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
1915}
1916
4565e370
WB
1917/**
1918 * ipr_log64_fabric_path - Log a fabric path error
1919 * @hostrcb: hostrcb struct
1920 * @fabric: fabric descriptor
1921 *
1922 * Return value:
1923 * none
1924 **/
1925static void ipr_log64_fabric_path(struct ipr_hostrcb *hostrcb,
1926 struct ipr_hostrcb64_fabric_desc *fabric)
1927{
1928 int i, j;
1929 u8 path_state = fabric->path_state;
1930 u8 active = path_state & IPR_PATH_ACTIVE_MASK;
1931 u8 state = path_state & IPR_PATH_STATE_MASK;
1932 char buffer[IPR_MAX_RES_PATH_LENGTH];
1933
1934 for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
1935 if (path_active_desc[i].active != active)
1936 continue;
1937
1938 for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
1939 if (path_state_desc[j].state != state)
1940 continue;
1941
1942 ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s\n",
1943 path_active_desc[i].desc, path_state_desc[j].desc,
5adcbeb3
WB
1944 ipr_format_res_path(fabric->res_path, buffer,
1945 sizeof(buffer)));
4565e370
WB
1946 return;
1947 }
1948 }
1949
1950 ipr_err("Path state=%02X Resource Path=%s\n", path_state,
5adcbeb3 1951 ipr_format_res_path(fabric->res_path, buffer, sizeof(buffer)));
4565e370
WB
1952}
1953
49dc6a18
BK
1954static const struct {
1955 u8 type;
1956 char *desc;
1957} path_type_desc[] = {
1958 { IPR_PATH_CFG_IOA_PORT, "IOA port" },
1959 { IPR_PATH_CFG_EXP_PORT, "Expander port" },
1960 { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
1961 { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
1962};
1963
1964static const struct {
1965 u8 status;
1966 char *desc;
1967} path_status_desc[] = {
1968 { IPR_PATH_CFG_NO_PROB, "Functional" },
1969 { IPR_PATH_CFG_DEGRADED, "Degraded" },
1970 { IPR_PATH_CFG_FAILED, "Failed" },
1971 { IPR_PATH_CFG_SUSPECT, "Suspect" },
1972 { IPR_PATH_NOT_DETECTED, "Missing" },
1973 { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
1974};
1975
1976static const char *link_rate[] = {
1977 "unknown",
1978 "disabled",
1979 "phy reset problem",
1980 "spinup hold",
1981 "port selector",
1982 "unknown",
1983 "unknown",
1984 "unknown",
1985 "1.5Gbps",
1986 "3.0Gbps",
1987 "unknown",
1988 "unknown",
1989 "unknown",
1990 "unknown",
1991 "unknown",
1992 "unknown"
1993};
1994
1995/**
1996 * ipr_log_path_elem - Log a fabric path element.
1997 * @hostrcb: hostrcb struct
1998 * @cfg: fabric path element struct
1999 *
2000 * Return value:
2001 * none
2002 **/
2003static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
2004 struct ipr_hostrcb_config_element *cfg)
2005{
2006 int i, j;
2007 u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
2008 u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
2009
2010 if (type == IPR_PATH_CFG_NOT_EXIST)
2011 return;
2012
2013 for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
2014 if (path_type_desc[i].type != type)
2015 continue;
2016
2017 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
2018 if (path_status_desc[j].status != status)
2019 continue;
2020
2021 if (type == IPR_PATH_CFG_IOA_PORT) {
2022 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
2023 path_status_desc[j].desc, path_type_desc[i].desc,
2024 cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2025 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2026 } else {
2027 if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
2028 ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
2029 path_status_desc[j].desc, path_type_desc[i].desc,
2030 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2031 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2032 } else if (cfg->cascaded_expander == 0xff) {
2033 ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
2034 "WWN=%08X%08X\n", path_status_desc[j].desc,
2035 path_type_desc[i].desc, cfg->phy,
2036 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2037 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2038 } else if (cfg->phy == 0xff) {
2039 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
2040 "WWN=%08X%08X\n", path_status_desc[j].desc,
2041 path_type_desc[i].desc, cfg->cascaded_expander,
2042 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2043 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2044 } else {
2045 ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
2046 "WWN=%08X%08X\n", path_status_desc[j].desc,
2047 path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
2048 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2049 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2050 }
2051 }
2052 return;
2053 }
2054 }
2055
2056 ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
2057 "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
2058 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2059 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2060}
2061
4565e370
WB
2062/**
2063 * ipr_log64_path_elem - Log a fabric path element.
2064 * @hostrcb: hostrcb struct
2065 * @cfg: fabric path element struct
2066 *
2067 * Return value:
2068 * none
2069 **/
2070static void ipr_log64_path_elem(struct ipr_hostrcb *hostrcb,
2071 struct ipr_hostrcb64_config_element *cfg)
2072{
2073 int i, j;
2074 u8 desc_id = cfg->descriptor_id & IPR_DESCRIPTOR_MASK;
2075 u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
2076 u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
2077 char buffer[IPR_MAX_RES_PATH_LENGTH];
2078
2079 if (type == IPR_PATH_CFG_NOT_EXIST || desc_id != IPR_DESCRIPTOR_SIS64)
2080 return;
2081
2082 for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
2083 if (path_type_desc[i].type != type)
2084 continue;
2085
2086 for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
2087 if (path_status_desc[j].status != status)
2088 continue;
2089
2090 ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s, Link rate=%s, WWN=%08X%08X\n",
2091 path_status_desc[j].desc, path_type_desc[i].desc,
5adcbeb3
WB
2092 ipr_format_res_path(cfg->res_path, buffer,
2093 sizeof(buffer)),
4565e370
WB
2094 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2095 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2096 return;
2097 }
2098 }
2099 ipr_hcam_err(hostrcb, "Path element=%02X: Resource Path=%s, Link rate=%s "
2100 "WWN=%08X%08X\n", cfg->type_status,
5adcbeb3 2101 ipr_format_res_path(cfg->res_path, buffer, sizeof(buffer)),
4565e370
WB
2102 link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
2103 be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
2104}
2105
49dc6a18
BK
2106/**
2107 * ipr_log_fabric_error - Log a fabric error.
2108 * @ioa_cfg: ioa config struct
2109 * @hostrcb: hostrcb struct
2110 *
2111 * Return value:
2112 * none
2113 **/
2114static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
2115 struct ipr_hostrcb *hostrcb)
2116{
2117 struct ipr_hostrcb_type_20_error *error;
2118 struct ipr_hostrcb_fabric_desc *fabric;
2119 struct ipr_hostrcb_config_element *cfg;
2120 int i, add_len;
2121
2122 error = &hostrcb->hcam.u.error.u.type_20_error;
2123 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
2124 ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
2125
2126 add_len = be32_to_cpu(hostrcb->hcam.length) -
2127 (offsetof(struct ipr_hostrcb_error, u) +
2128 offsetof(struct ipr_hostrcb_type_20_error, desc));
2129
2130 for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
2131 ipr_log_fabric_path(hostrcb, fabric);
2132 for_each_fabric_cfg(fabric, cfg)
2133 ipr_log_path_elem(hostrcb, cfg);
2134
2135 add_len -= be16_to_cpu(fabric->length);
2136 fabric = (struct ipr_hostrcb_fabric_desc *)
2137 ((unsigned long)fabric + be16_to_cpu(fabric->length));
2138 }
2139
ac719aba 2140 ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
49dc6a18
BK
2141}
2142
4565e370
WB
2143/**
2144 * ipr_log_sis64_array_error - Log a sis64 array error.
2145 * @ioa_cfg: ioa config struct
2146 * @hostrcb: hostrcb struct
2147 *
2148 * Return value:
2149 * none
2150 **/
2151static void ipr_log_sis64_array_error(struct ipr_ioa_cfg *ioa_cfg,
2152 struct ipr_hostrcb *hostrcb)
2153{
2154 int i, num_entries;
2155 struct ipr_hostrcb_type_24_error *error;
2156 struct ipr_hostrcb64_array_data_entry *array_entry;
2157 char buffer[IPR_MAX_RES_PATH_LENGTH];
2158 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
2159
2160 error = &hostrcb->hcam.u.error64.u.type_24_error;
2161
2162 ipr_err_separator;
2163
2164 ipr_err("RAID %s Array Configuration: %s\n",
2165 error->protection_level,
5adcbeb3 2166 ipr_format_res_path(error->last_res_path, buffer, sizeof(buffer)));
4565e370
WB
2167
2168 ipr_err_separator;
2169
2170 array_entry = error->array_member;
7262026f
WB
2171 num_entries = min_t(u32, error->num_entries,
2172 ARRAY_SIZE(error->array_member));
4565e370
WB
2173
2174 for (i = 0; i < num_entries; i++, array_entry++) {
2175
2176 if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
2177 continue;
2178
2179 if (error->exposed_mode_adn == i)
2180 ipr_err("Exposed Array Member %d:\n", i);
2181 else
2182 ipr_err("Array Member %d:\n", i);
2183
2184 ipr_err("Array Member %d:\n", i);
2185 ipr_log_ext_vpd(&array_entry->vpd);
7262026f 2186 ipr_err("Current Location: %s\n",
5adcbeb3
WB
2187 ipr_format_res_path(array_entry->res_path, buffer,
2188 sizeof(buffer)));
7262026f 2189 ipr_err("Expected Location: %s\n",
5adcbeb3
WB
2190 ipr_format_res_path(array_entry->expected_res_path,
2191 buffer, sizeof(buffer)));
4565e370
WB
2192
2193 ipr_err_separator;
2194 }
2195}
2196
2197/**
2198 * ipr_log_sis64_fabric_error - Log a sis64 fabric error.
2199 * @ioa_cfg: ioa config struct
2200 * @hostrcb: hostrcb struct
2201 *
2202 * Return value:
2203 * none
2204 **/
2205static void ipr_log_sis64_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
2206 struct ipr_hostrcb *hostrcb)
2207{
2208 struct ipr_hostrcb_type_30_error *error;
2209 struct ipr_hostrcb64_fabric_desc *fabric;
2210 struct ipr_hostrcb64_config_element *cfg;
2211 int i, add_len;
2212
2213 error = &hostrcb->hcam.u.error64.u.type_30_error;
2214
2215 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
2216 ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
2217
2218 add_len = be32_to_cpu(hostrcb->hcam.length) -
2219 (offsetof(struct ipr_hostrcb64_error, u) +
2220 offsetof(struct ipr_hostrcb_type_30_error, desc));
2221
2222 for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
2223 ipr_log64_fabric_path(hostrcb, fabric);
2224 for_each_fabric_cfg(fabric, cfg)
2225 ipr_log64_path_elem(hostrcb, cfg);
2226
2227 add_len -= be16_to_cpu(fabric->length);
2228 fabric = (struct ipr_hostrcb64_fabric_desc *)
2229 ((unsigned long)fabric + be16_to_cpu(fabric->length));
2230 }
2231
2232 ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
2233}
2234
b0df54bb 2235/**
2236 * ipr_log_generic_error - Log an adapter error.
2237 * @ioa_cfg: ioa config struct
2238 * @hostrcb: hostrcb struct
2239 *
2240 * Return value:
2241 * none
2242 **/
2243static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
2244 struct ipr_hostrcb *hostrcb)
2245{
ac719aba 2246 ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
b0df54bb 2247 be32_to_cpu(hostrcb->hcam.length));
2248}
2249
1da177e4
LT
2250/**
2251 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
2252 * @ioasc: IOASC
2253 *
2254 * This function will return the index of into the ipr_error_table
2255 * for the specified IOASC. If the IOASC is not in the table,
2256 * 0 will be returned, which points to the entry used for unknown errors.
2257 *
2258 * Return value:
2259 * index into the ipr_error_table
2260 **/
2261static u32 ipr_get_error(u32 ioasc)
2262{
2263 int i;
2264
2265 for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
35a39691 2266 if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
1da177e4
LT
2267 return i;
2268
2269 return 0;
2270}
2271
2272/**
2273 * ipr_handle_log_data - Log an adapter error.
2274 * @ioa_cfg: ioa config struct
2275 * @hostrcb: hostrcb struct
2276 *
2277 * This function logs an adapter error to the system.
2278 *
2279 * Return value:
2280 * none
2281 **/
2282static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
2283 struct ipr_hostrcb *hostrcb)
2284{
2285 u32 ioasc;
2286 int error_index;
2287
2288 if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
2289 return;
2290
2291 if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
2292 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
2293
4565e370
WB
2294 if (ioa_cfg->sis64)
2295 ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
2296 else
2297 ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
1da177e4 2298
4565e370
WB
2299 if (!ioa_cfg->sis64 && (ioasc == IPR_IOASC_BUS_WAS_RESET ||
2300 ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER)) {
1da177e4
LT
2301 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
2302 scsi_report_bus_reset(ioa_cfg->host,
4565e370 2303 hostrcb->hcam.u.error.fd_res_addr.bus);
1da177e4
LT
2304 }
2305
2306 error_index = ipr_get_error(ioasc);
2307
2308 if (!ipr_error_table[error_index].log_hcam)
2309 return;
2310
49dc6a18 2311 ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
1da177e4
LT
2312
2313 /* Set indication we have logged an error */
2314 ioa_cfg->errors_logged++;
2315
933916f3 2316 if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
1da177e4 2317 return;
cf852037 2318 if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
2319 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
1da177e4
LT
2320
2321 switch (hostrcb->hcam.overlay_id) {
1da177e4
LT
2322 case IPR_HOST_RCB_OVERLAY_ID_2:
2323 ipr_log_cache_error(ioa_cfg, hostrcb);
2324 break;
2325 case IPR_HOST_RCB_OVERLAY_ID_3:
2326 ipr_log_config_error(ioa_cfg, hostrcb);
2327 break;
2328 case IPR_HOST_RCB_OVERLAY_ID_4:
2329 case IPR_HOST_RCB_OVERLAY_ID_6:
2330 ipr_log_array_error(ioa_cfg, hostrcb);
2331 break;
b0df54bb 2332 case IPR_HOST_RCB_OVERLAY_ID_7:
2333 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
2334 break;
ee0f05b8 2335 case IPR_HOST_RCB_OVERLAY_ID_12:
2336 ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
2337 break;
2338 case IPR_HOST_RCB_OVERLAY_ID_13:
2339 ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
2340 break;
2341 case IPR_HOST_RCB_OVERLAY_ID_14:
2342 case IPR_HOST_RCB_OVERLAY_ID_16:
2343 ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
2344 break;
2345 case IPR_HOST_RCB_OVERLAY_ID_17:
2346 ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
2347 break;
49dc6a18
BK
2348 case IPR_HOST_RCB_OVERLAY_ID_20:
2349 ipr_log_fabric_error(ioa_cfg, hostrcb);
2350 break;
4565e370
WB
2351 case IPR_HOST_RCB_OVERLAY_ID_23:
2352 ipr_log_sis64_config_error(ioa_cfg, hostrcb);
2353 break;
2354 case IPR_HOST_RCB_OVERLAY_ID_24:
2355 case IPR_HOST_RCB_OVERLAY_ID_26:
2356 ipr_log_sis64_array_error(ioa_cfg, hostrcb);
2357 break;
2358 case IPR_HOST_RCB_OVERLAY_ID_30:
2359 ipr_log_sis64_fabric_error(ioa_cfg, hostrcb);
2360 break;
cf852037 2361 case IPR_HOST_RCB_OVERLAY_ID_1:
1da177e4 2362 case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1da177e4 2363 default:
a9cfca96 2364 ipr_log_generic_error(ioa_cfg, hostrcb);
1da177e4
LT
2365 break;
2366 }
2367}
2368
2369/**
2370 * ipr_process_error - Op done function for an adapter error log.
2371 * @ipr_cmd: ipr command struct
2372 *
2373 * This function is the op done function for an error log host
2374 * controlled async from the adapter. It will log the error and
2375 * send the HCAM back to the adapter.
2376 *
2377 * Return value:
2378 * none
2379 **/
2380static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
2381{
2382 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2383 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
96d21f00 2384 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
4565e370
WB
2385 u32 fd_ioasc;
2386
2387 if (ioa_cfg->sis64)
2388 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
2389 else
2390 fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
1da177e4
LT
2391
2392 list_del(&hostrcb->queue);
2393 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
2394
2395 if (!ioasc) {
2396 ipr_handle_log_data(ioa_cfg, hostrcb);
65f56475
BK
2397 if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
2398 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
1da177e4
LT
2399 } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
2400 dev_err(&ioa_cfg->pdev->dev,
2401 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
2402 }
2403
2404 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
2405}
2406
2407/**
2408 * ipr_timeout - An internally generated op has timed out.
2409 * @ipr_cmd: ipr command struct
2410 *
2411 * This function blocks host requests and initiates an
2412 * adapter reset.
2413 *
2414 * Return value:
2415 * none
2416 **/
2417static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
2418{
2419 unsigned long lock_flags = 0;
2420 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2421
2422 ENTER;
2423 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2424
2425 ioa_cfg->errors_logged++;
2426 dev_err(&ioa_cfg->pdev->dev,
2427 "Adapter being reset due to command timeout.\n");
2428
2429 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2430 ioa_cfg->sdt_state = GET_DUMP;
2431
2432 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
2433 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2434
2435 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2436 LEAVE;
2437}
2438
2439/**
2440 * ipr_oper_timeout - Adapter timed out transitioning to operational
2441 * @ipr_cmd: ipr command struct
2442 *
2443 * This function blocks host requests and initiates an
2444 * adapter reset.
2445 *
2446 * Return value:
2447 * none
2448 **/
2449static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
2450{
2451 unsigned long lock_flags = 0;
2452 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2453
2454 ENTER;
2455 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2456
2457 ioa_cfg->errors_logged++;
2458 dev_err(&ioa_cfg->pdev->dev,
2459 "Adapter timed out transitioning to operational.\n");
2460
2461 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2462 ioa_cfg->sdt_state = GET_DUMP;
2463
2464 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
2465 if (ipr_fastfail)
2466 ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
2467 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2468 }
2469
2470 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2471 LEAVE;
2472}
2473
2474/**
2475 * ipr_reset_reload - Reset/Reload the IOA
2476 * @ioa_cfg: ioa config struct
2477 * @shutdown_type: shutdown type
2478 *
2479 * This function resets the adapter and re-initializes it.
2480 * This function assumes that all new host commands have been stopped.
2481 * Return value:
2482 * SUCCESS / FAILED
2483 **/
2484static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
2485 enum ipr_shutdown_type shutdown_type)
2486{
2487 if (!ioa_cfg->in_reset_reload)
2488 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
2489
2490 spin_unlock_irq(ioa_cfg->host->host_lock);
2491 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2492 spin_lock_irq(ioa_cfg->host->host_lock);
2493
2494 /* If we got hit with a host reset while we were already resetting
2495 the adapter for some reason, and the reset failed. */
2496 if (ioa_cfg->ioa_is_dead) {
2497 ipr_trace;
2498 return FAILED;
2499 }
2500
2501 return SUCCESS;
2502}
2503
2504/**
2505 * ipr_find_ses_entry - Find matching SES in SES table
2506 * @res: resource entry struct of SES
2507 *
2508 * Return value:
2509 * pointer to SES table entry / NULL on failure
2510 **/
2511static const struct ipr_ses_table_entry *
2512ipr_find_ses_entry(struct ipr_resource_entry *res)
2513{
2514 int i, j, matches;
3e7ebdfa 2515 struct ipr_std_inq_vpids *vpids;
1da177e4
LT
2516 const struct ipr_ses_table_entry *ste = ipr_ses_table;
2517
2518 for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
2519 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
2520 if (ste->compare_product_id_byte[j] == 'X') {
3e7ebdfa
WB
2521 vpids = &res->std_inq_data.vpids;
2522 if (vpids->product_id[j] == ste->product_id[j])
1da177e4
LT
2523 matches++;
2524 else
2525 break;
2526 } else
2527 matches++;
2528 }
2529
2530 if (matches == IPR_PROD_ID_LEN)
2531 return ste;
2532 }
2533
2534 return NULL;
2535}
2536
2537/**
2538 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
2539 * @ioa_cfg: ioa config struct
2540 * @bus: SCSI bus
2541 * @bus_width: bus width
2542 *
2543 * Return value:
2544 * SCSI bus speed in units of 100KHz, 1600 is 160 MHz
2545 * For a 2-byte wide SCSI bus, the maximum transfer speed is
2546 * twice the maximum transfer rate (e.g. for a wide enabled bus,
2547 * max 160MHz = max 320MB/sec).
2548 **/
2549static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
2550{
2551 struct ipr_resource_entry *res;
2552 const struct ipr_ses_table_entry *ste;
2553 u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
2554
2555 /* Loop through each config table entry in the config table buffer */
2556 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3e7ebdfa 2557 if (!(IPR_IS_SES_DEVICE(res->std_inq_data)))
1da177e4
LT
2558 continue;
2559
3e7ebdfa 2560 if (bus != res->bus)
1da177e4
LT
2561 continue;
2562
2563 if (!(ste = ipr_find_ses_entry(res)))
2564 continue;
2565
2566 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
2567 }
2568
2569 return max_xfer_rate;
2570}
2571
2572/**
2573 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
2574 * @ioa_cfg: ioa config struct
2575 * @max_delay: max delay in micro-seconds to wait
2576 *
2577 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
2578 *
2579 * Return value:
2580 * 0 on success / other on failure
2581 **/
2582static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
2583{
2584 volatile u32 pcii_reg;
2585 int delay = 1;
2586
2587 /* Read interrupt reg until IOA signals IO Debug Acknowledge */
2588 while (delay < max_delay) {
2589 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
2590
2591 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
2592 return 0;
2593
2594 /* udelay cannot be used if delay is more than a few milliseconds */
2595 if ((delay / 1000) > MAX_UDELAY_MS)
2596 mdelay(delay / 1000);
2597 else
2598 udelay(delay);
2599
2600 delay += delay;
2601 }
2602 return -EIO;
2603}
2604
dcbad00e
WB
2605/**
2606 * ipr_get_sis64_dump_data_section - Dump IOA memory
2607 * @ioa_cfg: ioa config struct
2608 * @start_addr: adapter address to dump
2609 * @dest: destination kernel buffer
2610 * @length_in_words: length to dump in 4 byte words
2611 *
2612 * Return value:
2613 * 0 on success
2614 **/
2615static int ipr_get_sis64_dump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2616 u32 start_addr,
2617 __be32 *dest, u32 length_in_words)
2618{
2619 int i;
2620
2621 for (i = 0; i < length_in_words; i++) {
2622 writel(start_addr+(i*4), ioa_cfg->regs.dump_addr_reg);
2623 *dest = cpu_to_be32(readl(ioa_cfg->regs.dump_data_reg));
2624 dest++;
2625 }
2626
2627 return 0;
2628}
2629
1da177e4
LT
2630/**
2631 * ipr_get_ldump_data_section - Dump IOA memory
2632 * @ioa_cfg: ioa config struct
2633 * @start_addr: adapter address to dump
2634 * @dest: destination kernel buffer
2635 * @length_in_words: length to dump in 4 byte words
2636 *
2637 * Return value:
2638 * 0 on success / -EIO on failure
2639 **/
2640static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
2641 u32 start_addr,
2642 __be32 *dest, u32 length_in_words)
2643{
2644 volatile u32 temp_pcii_reg;
2645 int i, delay = 0;
2646
dcbad00e
WB
2647 if (ioa_cfg->sis64)
2648 return ipr_get_sis64_dump_data_section(ioa_cfg, start_addr,
2649 dest, length_in_words);
2650
1da177e4
LT
2651 /* Write IOA interrupt reg starting LDUMP state */
2652 writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
214777ba 2653 ioa_cfg->regs.set_uproc_interrupt_reg32);
1da177e4
LT
2654
2655 /* Wait for IO debug acknowledge */
2656 if (ipr_wait_iodbg_ack(ioa_cfg,
2657 IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
2658 dev_err(&ioa_cfg->pdev->dev,
2659 "IOA dump long data transfer timeout\n");
2660 return -EIO;
2661 }
2662
2663 /* Signal LDUMP interlocked - clear IO debug ack */
2664 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2665 ioa_cfg->regs.clr_interrupt_reg);
2666
2667 /* Write Mailbox with starting address */
2668 writel(start_addr, ioa_cfg->ioa_mailbox);
2669
2670 /* Signal address valid - clear IOA Reset alert */
2671 writel(IPR_UPROCI_RESET_ALERT,
214777ba 2672 ioa_cfg->regs.clr_uproc_interrupt_reg32);
1da177e4
LT
2673
2674 for (i = 0; i < length_in_words; i++) {
2675 /* Wait for IO debug acknowledge */
2676 if (ipr_wait_iodbg_ack(ioa_cfg,
2677 IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
2678 dev_err(&ioa_cfg->pdev->dev,
2679 "IOA dump short data transfer timeout\n");
2680 return -EIO;
2681 }
2682
2683 /* Read data from mailbox and increment destination pointer */
2684 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
2685 dest++;
2686
2687 /* For all but the last word of data, signal data received */
2688 if (i < (length_in_words - 1)) {
2689 /* Signal dump data received - Clear IO debug Ack */
2690 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2691 ioa_cfg->regs.clr_interrupt_reg);
2692 }
2693 }
2694
2695 /* Signal end of block transfer. Set reset alert then clear IO debug ack */
2696 writel(IPR_UPROCI_RESET_ALERT,
214777ba 2697 ioa_cfg->regs.set_uproc_interrupt_reg32);
1da177e4
LT
2698
2699 writel(IPR_UPROCI_IO_DEBUG_ALERT,
214777ba 2700 ioa_cfg->regs.clr_uproc_interrupt_reg32);
1da177e4
LT
2701
2702 /* Signal dump data received - Clear IO debug Ack */
2703 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
2704 ioa_cfg->regs.clr_interrupt_reg);
2705
2706 /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
2707 while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
2708 temp_pcii_reg =
214777ba 2709 readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
1da177e4
LT
2710
2711 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
2712 return 0;
2713
2714 udelay(10);
2715 delay += 10;
2716 }
2717
2718 return 0;
2719}
2720
2721#ifdef CONFIG_SCSI_IPR_DUMP
2722/**
2723 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
2724 * @ioa_cfg: ioa config struct
2725 * @pci_address: adapter address
2726 * @length: length of data to copy
2727 *
2728 * Copy data from PCI adapter to kernel buffer.
2729 * Note: length MUST be a 4 byte multiple
2730 * Return value:
2731 * 0 on success / other on failure
2732 **/
2733static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
2734 unsigned long pci_address, u32 length)
2735{
2736 int bytes_copied = 0;
4d4dd706 2737 int cur_len, rc, rem_len, rem_page_len, max_dump_size;
1da177e4
LT
2738 __be32 *page;
2739 unsigned long lock_flags = 0;
2740 struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
2741
4d4dd706
KSS
2742 if (ioa_cfg->sis64)
2743 max_dump_size = IPR_FMT3_MAX_IOA_DUMP_SIZE;
2744 else
2745 max_dump_size = IPR_FMT2_MAX_IOA_DUMP_SIZE;
2746
1da177e4 2747 while (bytes_copied < length &&
4d4dd706 2748 (ioa_dump->hdr.len + bytes_copied) < max_dump_size) {
1da177e4
LT
2749 if (ioa_dump->page_offset >= PAGE_SIZE ||
2750 ioa_dump->page_offset == 0) {
2751 page = (__be32 *)__get_free_page(GFP_ATOMIC);
2752
2753 if (!page) {
2754 ipr_trace;
2755 return bytes_copied;
2756 }
2757
2758 ioa_dump->page_offset = 0;
2759 ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
2760 ioa_dump->next_page_index++;
2761 } else
2762 page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
2763
2764 rem_len = length - bytes_copied;
2765 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
2766 cur_len = min(rem_len, rem_page_len);
2767
2768 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2769 if (ioa_cfg->sdt_state == ABORT_DUMP) {
2770 rc = -EIO;
2771 } else {
2772 rc = ipr_get_ldump_data_section(ioa_cfg,
2773 pci_address + bytes_copied,
2774 &page[ioa_dump->page_offset / 4],
2775 (cur_len / sizeof(u32)));
2776 }
2777 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2778
2779 if (!rc) {
2780 ioa_dump->page_offset += cur_len;
2781 bytes_copied += cur_len;
2782 } else {
2783 ipr_trace;
2784 break;
2785 }
2786 schedule();
2787 }
2788
2789 return bytes_copied;
2790}
2791
2792/**
2793 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
2794 * @hdr: dump entry header struct
2795 *
2796 * Return value:
2797 * nothing
2798 **/
2799static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
2800{
2801 hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
2802 hdr->num_elems = 1;
2803 hdr->offset = sizeof(*hdr);
2804 hdr->status = IPR_DUMP_STATUS_SUCCESS;
2805}
2806
2807/**
2808 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
2809 * @ioa_cfg: ioa config struct
2810 * @driver_dump: driver dump struct
2811 *
2812 * Return value:
2813 * nothing
2814 **/
2815static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
2816 struct ipr_driver_dump *driver_dump)
2817{
2818 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2819
2820 ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
2821 driver_dump->ioa_type_entry.hdr.len =
2822 sizeof(struct ipr_dump_ioa_type_entry) -
2823 sizeof(struct ipr_dump_entry_header);
2824 driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2825 driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
2826 driver_dump->ioa_type_entry.type = ioa_cfg->type;
2827 driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
2828 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
2829 ucode_vpd->minor_release[1];
2830 driver_dump->hdr.num_entries++;
2831}
2832
2833/**
2834 * ipr_dump_version_data - Fill in the driver version in the dump.
2835 * @ioa_cfg: ioa config struct
2836 * @driver_dump: driver dump struct
2837 *
2838 * Return value:
2839 * nothing
2840 **/
2841static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
2842 struct ipr_driver_dump *driver_dump)
2843{
2844 ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
2845 driver_dump->version_entry.hdr.len =
2846 sizeof(struct ipr_dump_version_entry) -
2847 sizeof(struct ipr_dump_entry_header);
2848 driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2849 driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
2850 strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
2851 driver_dump->hdr.num_entries++;
2852}
2853
2854/**
2855 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
2856 * @ioa_cfg: ioa config struct
2857 * @driver_dump: driver dump struct
2858 *
2859 * Return value:
2860 * nothing
2861 **/
2862static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
2863 struct ipr_driver_dump *driver_dump)
2864{
2865 ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
2866 driver_dump->trace_entry.hdr.len =
2867 sizeof(struct ipr_dump_trace_entry) -
2868 sizeof(struct ipr_dump_entry_header);
2869 driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2870 driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
2871 memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
2872 driver_dump->hdr.num_entries++;
2873}
2874
2875/**
2876 * ipr_dump_location_data - Fill in the IOA location in the dump.
2877 * @ioa_cfg: ioa config struct
2878 * @driver_dump: driver dump struct
2879 *
2880 * Return value:
2881 * nothing
2882 **/
2883static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
2884 struct ipr_driver_dump *driver_dump)
2885{
2886 ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
2887 driver_dump->location_entry.hdr.len =
2888 sizeof(struct ipr_dump_location_entry) -
2889 sizeof(struct ipr_dump_entry_header);
2890 driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
2891 driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
71610f55 2892 strcpy(driver_dump->location_entry.location, dev_name(&ioa_cfg->pdev->dev));
1da177e4
LT
2893 driver_dump->hdr.num_entries++;
2894}
2895
2896/**
2897 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
2898 * @ioa_cfg: ioa config struct
2899 * @dump: dump struct
2900 *
2901 * Return value:
2902 * nothing
2903 **/
2904static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
2905{
2906 unsigned long start_addr, sdt_word;
2907 unsigned long lock_flags = 0;
2908 struct ipr_driver_dump *driver_dump = &dump->driver_dump;
2909 struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
4d4dd706
KSS
2910 u32 num_entries, max_num_entries, start_off, end_off;
2911 u32 max_dump_size, bytes_to_copy, bytes_copied, rc;
1da177e4 2912 struct ipr_sdt *sdt;
dcbad00e 2913 int valid = 1;
1da177e4
LT
2914 int i;
2915
2916 ENTER;
2917
2918 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2919
41e9a696 2920 if (ioa_cfg->sdt_state != READ_DUMP) {
1da177e4
LT
2921 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2922 return;
2923 }
2924
110def85
WB
2925 if (ioa_cfg->sis64) {
2926 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2927 ssleep(IPR_DUMP_DELAY_SECONDS);
2928 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2929 }
2930
1da177e4
LT
2931 start_addr = readl(ioa_cfg->ioa_mailbox);
2932
dcbad00e 2933 if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(start_addr)) {
1da177e4
LT
2934 dev_err(&ioa_cfg->pdev->dev,
2935 "Invalid dump table format: %lx\n", start_addr);
2936 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2937 return;
2938 }
2939
2940 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
2941
2942 driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
2943
2944 /* Initialize the overall dump header */
2945 driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
2946 driver_dump->hdr.num_entries = 1;
2947 driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
2948 driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
2949 driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
2950 driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
2951
2952 ipr_dump_version_data(ioa_cfg, driver_dump);
2953 ipr_dump_location_data(ioa_cfg, driver_dump);
2954 ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
2955 ipr_dump_trace_data(ioa_cfg, driver_dump);
2956
2957 /* Update dump_header */
2958 driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
2959
2960 /* IOA Dump entry */
2961 ipr_init_dump_entry_hdr(&ioa_dump->hdr);
1da177e4
LT
2962 ioa_dump->hdr.len = 0;
2963 ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
2964 ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
2965
2966 /* First entries in sdt are actually a list of dump addresses and
2967 lengths to gather the real dump data. sdt represents the pointer
2968 to the ioa generated dump table. Dump data will be extracted based
2969 on entries in this table */
2970 sdt = &ioa_dump->sdt;
2971
4d4dd706
KSS
2972 if (ioa_cfg->sis64) {
2973 max_num_entries = IPR_FMT3_NUM_SDT_ENTRIES;
2974 max_dump_size = IPR_FMT3_MAX_IOA_DUMP_SIZE;
2975 } else {
2976 max_num_entries = IPR_FMT2_NUM_SDT_ENTRIES;
2977 max_dump_size = IPR_FMT2_MAX_IOA_DUMP_SIZE;
2978 }
2979
2980 bytes_to_copy = offsetof(struct ipr_sdt, entry) +
2981 (max_num_entries * sizeof(struct ipr_sdt_entry));
1da177e4 2982 rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
4d4dd706 2983 bytes_to_copy / sizeof(__be32));
1da177e4
LT
2984
2985 /* Smart Dump table is ready to use and the first entry is valid */
dcbad00e
WB
2986 if (rc || ((be32_to_cpu(sdt->hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
2987 (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
1da177e4
LT
2988 dev_err(&ioa_cfg->pdev->dev,
2989 "Dump of IOA failed. Dump table not valid: %d, %X.\n",
2990 rc, be32_to_cpu(sdt->hdr.state));
2991 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
2992 ioa_cfg->sdt_state = DUMP_OBTAINED;
2993 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2994 return;
2995 }
2996
2997 num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
2998
4d4dd706
KSS
2999 if (num_entries > max_num_entries)
3000 num_entries = max_num_entries;
3001
3002 /* Update dump length to the actual data to be copied */
3003 dump->driver_dump.hdr.len += sizeof(struct ipr_sdt_header);
3004 if (ioa_cfg->sis64)
3005 dump->driver_dump.hdr.len += num_entries * sizeof(struct ipr_sdt_entry);
3006 else
3007 dump->driver_dump.hdr.len += max_num_entries * sizeof(struct ipr_sdt_entry);
1da177e4
LT
3008
3009 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3010
3011 for (i = 0; i < num_entries; i++) {
4d4dd706 3012 if (ioa_dump->hdr.len > max_dump_size) {
1da177e4
LT
3013 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
3014 break;
3015 }
3016
3017 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
dcbad00e
WB
3018 sdt_word = be32_to_cpu(sdt->entry[i].start_token);
3019 if (ioa_cfg->sis64)
3020 bytes_to_copy = be32_to_cpu(sdt->entry[i].end_token);
3021 else {
3022 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
3023 end_off = be32_to_cpu(sdt->entry[i].end_token);
3024
3025 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word)
3026 bytes_to_copy = end_off - start_off;
3027 else
3028 valid = 0;
3029 }
3030 if (valid) {
4d4dd706 3031 if (bytes_to_copy > max_dump_size) {
1da177e4
LT
3032 sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
3033 continue;
3034 }
3035
3036 /* Copy data from adapter to driver buffers */
3037 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
3038 bytes_to_copy);
3039
3040 ioa_dump->hdr.len += bytes_copied;
3041
3042 if (bytes_copied != bytes_to_copy) {
3043 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
3044 break;
3045 }
3046 }
3047 }
3048 }
3049
3050 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
3051
3052 /* Update dump_header */
3053 driver_dump->hdr.len += ioa_dump->hdr.len;
3054 wmb();
3055 ioa_cfg->sdt_state = DUMP_OBTAINED;
3056 LEAVE;
3057}
3058
3059#else
3060#define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
3061#endif
3062
3063/**
3064 * ipr_release_dump - Free adapter dump memory
3065 * @kref: kref struct
3066 *
3067 * Return value:
3068 * nothing
3069 **/
3070static void ipr_release_dump(struct kref *kref)
3071{
3072 struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
3073 struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
3074 unsigned long lock_flags = 0;
3075 int i;
3076
3077 ENTER;
3078 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3079 ioa_cfg->dump = NULL;
3080 ioa_cfg->sdt_state = INACTIVE;
3081 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3082
3083 for (i = 0; i < dump->ioa_dump.next_page_index; i++)
3084 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
3085
4d4dd706 3086 vfree(dump->ioa_dump.ioa_data);
1da177e4
LT
3087 kfree(dump);
3088 LEAVE;
3089}
3090
3091/**
3092 * ipr_worker_thread - Worker thread
c4028958 3093 * @work: ioa config struct
1da177e4
LT
3094 *
3095 * Called at task level from a work thread. This function takes care
3096 * of adding and removing device from the mid-layer as configuration
3097 * changes are detected by the adapter.
3098 *
3099 * Return value:
3100 * nothing
3101 **/
c4028958 3102static void ipr_worker_thread(struct work_struct *work)
1da177e4
LT
3103{
3104 unsigned long lock_flags;
3105 struct ipr_resource_entry *res;
3106 struct scsi_device *sdev;
3107 struct ipr_dump *dump;
c4028958
DH
3108 struct ipr_ioa_cfg *ioa_cfg =
3109 container_of(work, struct ipr_ioa_cfg, work_q);
1da177e4
LT
3110 u8 bus, target, lun;
3111 int did_work;
3112
3113 ENTER;
3114 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3115
41e9a696 3116 if (ioa_cfg->sdt_state == READ_DUMP) {
1da177e4
LT
3117 dump = ioa_cfg->dump;
3118 if (!dump) {
3119 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3120 return;
3121 }
3122 kref_get(&dump->kref);
3123 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3124 ipr_get_ioa_dump(ioa_cfg, dump);
3125 kref_put(&dump->kref, ipr_release_dump);
3126
3127 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4c647e90 3128 if (ioa_cfg->sdt_state == DUMP_OBTAINED && !ioa_cfg->dump_timeout)
1da177e4
LT
3129 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3130 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3131 return;
3132 }
3133
3134restart:
3135 do {
3136 did_work = 0;
3137 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
3138 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3139 return;
3140 }
3141
3142 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3143 if (res->del_from_ml && res->sdev) {
3144 did_work = 1;
3145 sdev = res->sdev;
3146 if (!scsi_device_get(sdev)) {
5767a1c4
KSS
3147 if (!res->add_to_ml)
3148 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
3149 else
3150 res->del_from_ml = 0;
1da177e4
LT
3151 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3152 scsi_remove_device(sdev);
3153 scsi_device_put(sdev);
3154 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3155 }
3156 break;
3157 }
3158 }
3159 } while(did_work);
3160
3161 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3162 if (res->add_to_ml) {
3e7ebdfa
WB
3163 bus = res->bus;
3164 target = res->target;
3165 lun = res->lun;
1121b794 3166 res->add_to_ml = 0;
1da177e4
LT
3167 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3168 scsi_add_device(ioa_cfg->host, bus, target, lun);
3169 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3170 goto restart;
3171 }
3172 }
3173
3174 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
ee959b00 3175 kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
1da177e4
LT
3176 LEAVE;
3177}
3178
3179#ifdef CONFIG_SCSI_IPR_TRACE
3180/**
3181 * ipr_read_trace - Dump the adapter trace
2c3c8bea 3182 * @filp: open sysfs file
1da177e4 3183 * @kobj: kobject struct
91a69029 3184 * @bin_attr: bin_attribute struct
1da177e4
LT
3185 * @buf: buffer
3186 * @off: offset
3187 * @count: buffer size
3188 *
3189 * Return value:
3190 * number of bytes printed to buffer
3191 **/
2c3c8bea 3192static ssize_t ipr_read_trace(struct file *filp, struct kobject *kobj,
91a69029
ZR
3193 struct bin_attribute *bin_attr,
3194 char *buf, loff_t off, size_t count)
1da177e4 3195{
ee959b00
TJ
3196 struct device *dev = container_of(kobj, struct device, kobj);
3197 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3198 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3199 unsigned long lock_flags = 0;
d777aaf3 3200 ssize_t ret;
1da177e4
LT
3201
3202 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
d777aaf3
AM
3203 ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
3204 IPR_TRACE_SIZE);
1da177e4 3205 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
d777aaf3
AM
3206
3207 return ret;
1da177e4
LT
3208}
3209
3210static struct bin_attribute ipr_trace_attr = {
3211 .attr = {
3212 .name = "trace",
3213 .mode = S_IRUGO,
3214 },
3215 .size = 0,
3216 .read = ipr_read_trace,
3217};
3218#endif
3219
3220/**
3221 * ipr_show_fw_version - Show the firmware version
ee959b00
TJ
3222 * @dev: class device struct
3223 * @buf: buffer
1da177e4
LT
3224 *
3225 * Return value:
3226 * number of bytes printed to buffer
3227 **/
ee959b00
TJ
3228static ssize_t ipr_show_fw_version(struct device *dev,
3229 struct device_attribute *attr, char *buf)
1da177e4 3230{
ee959b00 3231 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3232 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3233 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
3234 unsigned long lock_flags = 0;
3235 int len;
3236
3237 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3238 len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
3239 ucode_vpd->major_release, ucode_vpd->card_type,
3240 ucode_vpd->minor_release[0],
3241 ucode_vpd->minor_release[1]);
3242 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3243 return len;
3244}
3245
ee959b00 3246static struct device_attribute ipr_fw_version_attr = {
1da177e4
LT
3247 .attr = {
3248 .name = "fw_version",
3249 .mode = S_IRUGO,
3250 },
3251 .show = ipr_show_fw_version,
3252};
3253
3254/**
3255 * ipr_show_log_level - Show the adapter's error logging level
ee959b00
TJ
3256 * @dev: class device struct
3257 * @buf: buffer
1da177e4
LT
3258 *
3259 * Return value:
3260 * number of bytes printed to buffer
3261 **/
ee959b00
TJ
3262static ssize_t ipr_show_log_level(struct device *dev,
3263 struct device_attribute *attr, char *buf)
1da177e4 3264{
ee959b00 3265 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3266 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3267 unsigned long lock_flags = 0;
3268 int len;
3269
3270 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3271 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
3272 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3273 return len;
3274}
3275
3276/**
3277 * ipr_store_log_level - Change the adapter's error logging level
ee959b00
TJ
3278 * @dev: class device struct
3279 * @buf: buffer
1da177e4
LT
3280 *
3281 * Return value:
3282 * number of bytes printed to buffer
3283 **/
ee959b00
TJ
3284static ssize_t ipr_store_log_level(struct device *dev,
3285 struct device_attribute *attr,
1da177e4
LT
3286 const char *buf, size_t count)
3287{
ee959b00 3288 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3289 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3290 unsigned long lock_flags = 0;
3291
3292 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3293 ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
3294 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3295 return strlen(buf);
3296}
3297
ee959b00 3298static struct device_attribute ipr_log_level_attr = {
1da177e4
LT
3299 .attr = {
3300 .name = "log_level",
3301 .mode = S_IRUGO | S_IWUSR,
3302 },
3303 .show = ipr_show_log_level,
3304 .store = ipr_store_log_level
3305};
3306
3307/**
3308 * ipr_store_diagnostics - IOA Diagnostics interface
ee959b00
TJ
3309 * @dev: device struct
3310 * @buf: buffer
3311 * @count: buffer size
1da177e4
LT
3312 *
3313 * This function will reset the adapter and wait a reasonable
3314 * amount of time for any errors that the adapter might log.
3315 *
3316 * Return value:
3317 * count on success / other on failure
3318 **/
ee959b00
TJ
3319static ssize_t ipr_store_diagnostics(struct device *dev,
3320 struct device_attribute *attr,
1da177e4
LT
3321 const char *buf, size_t count)
3322{
ee959b00 3323 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3324 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3325 unsigned long lock_flags = 0;
3326 int rc = count;
3327
3328 if (!capable(CAP_SYS_ADMIN))
3329 return -EACCES;
3330
1da177e4 3331 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
970ea294
BK
3332 while(ioa_cfg->in_reset_reload) {
3333 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3334 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3335 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3336 }
3337
1da177e4
LT
3338 ioa_cfg->errors_logged = 0;
3339 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3340
3341 if (ioa_cfg->in_reset_reload) {
3342 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3343 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3344
3345 /* Wait for a second for any errors to be logged */
3346 msleep(1000);
3347 } else {
3348 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3349 return -EIO;
3350 }
3351
3352 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3353 if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
3354 rc = -EIO;
3355 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3356
3357 return rc;
3358}
3359
ee959b00 3360static struct device_attribute ipr_diagnostics_attr = {
1da177e4
LT
3361 .attr = {
3362 .name = "run_diagnostics",
3363 .mode = S_IWUSR,
3364 },
3365 .store = ipr_store_diagnostics
3366};
3367
f37eb54b 3368/**
3369 * ipr_show_adapter_state - Show the adapter's state
ee959b00
TJ
3370 * @class_dev: device struct
3371 * @buf: buffer
f37eb54b 3372 *
3373 * Return value:
3374 * number of bytes printed to buffer
3375 **/
ee959b00
TJ
3376static ssize_t ipr_show_adapter_state(struct device *dev,
3377 struct device_attribute *attr, char *buf)
f37eb54b 3378{
ee959b00 3379 struct Scsi_Host *shost = class_to_shost(dev);
f37eb54b 3380 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3381 unsigned long lock_flags = 0;
3382 int len;
3383
3384 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3385 if (ioa_cfg->ioa_is_dead)
3386 len = snprintf(buf, PAGE_SIZE, "offline\n");
3387 else
3388 len = snprintf(buf, PAGE_SIZE, "online\n");
3389 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3390 return len;
3391}
3392
3393/**
3394 * ipr_store_adapter_state - Change adapter state
ee959b00
TJ
3395 * @dev: device struct
3396 * @buf: buffer
3397 * @count: buffer size
f37eb54b 3398 *
3399 * This function will change the adapter's state.
3400 *
3401 * Return value:
3402 * count on success / other on failure
3403 **/
ee959b00
TJ
3404static ssize_t ipr_store_adapter_state(struct device *dev,
3405 struct device_attribute *attr,
f37eb54b 3406 const char *buf, size_t count)
3407{
ee959b00 3408 struct Scsi_Host *shost = class_to_shost(dev);
f37eb54b 3409 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3410 unsigned long lock_flags;
3411 int result = count;
3412
3413 if (!capable(CAP_SYS_ADMIN))
3414 return -EACCES;
3415
3416 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3417 if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
3418 ioa_cfg->ioa_is_dead = 0;
3419 ioa_cfg->reset_retries = 0;
3420 ioa_cfg->in_ioa_bringdown = 0;
3421 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3422 }
3423 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3424 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3425
3426 return result;
3427}
3428
ee959b00 3429static struct device_attribute ipr_ioa_state_attr = {
f37eb54b 3430 .attr = {
49dd0961 3431 .name = "online_state",
f37eb54b 3432 .mode = S_IRUGO | S_IWUSR,
3433 },
3434 .show = ipr_show_adapter_state,
3435 .store = ipr_store_adapter_state
3436};
3437
1da177e4
LT
3438/**
3439 * ipr_store_reset_adapter - Reset the adapter
ee959b00
TJ
3440 * @dev: device struct
3441 * @buf: buffer
3442 * @count: buffer size
1da177e4
LT
3443 *
3444 * This function will reset the adapter.
3445 *
3446 * Return value:
3447 * count on success / other on failure
3448 **/
ee959b00
TJ
3449static ssize_t ipr_store_reset_adapter(struct device *dev,
3450 struct device_attribute *attr,
1da177e4
LT
3451 const char *buf, size_t count)
3452{
ee959b00 3453 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3454 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3455 unsigned long lock_flags;
3456 int result = count;
3457
3458 if (!capable(CAP_SYS_ADMIN))
3459 return -EACCES;
3460
3461 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3462 if (!ioa_cfg->in_reset_reload)
3463 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3464 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3465 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3466
3467 return result;
3468}
3469
ee959b00 3470static struct device_attribute ipr_ioa_reset_attr = {
1da177e4
LT
3471 .attr = {
3472 .name = "reset_host",
3473 .mode = S_IWUSR,
3474 },
3475 .store = ipr_store_reset_adapter
3476};
3477
3478/**
3479 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
3480 * @buf_len: buffer length
3481 *
3482 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3483 * list to use for microcode download
3484 *
3485 * Return value:
3486 * pointer to sglist / NULL on failure
3487 **/
3488static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
3489{
3490 int sg_size, order, bsize_elem, num_elem, i, j;
3491 struct ipr_sglist *sglist;
3492 struct scatterlist *scatterlist;
3493 struct page *page;
3494
3495 /* Get the minimum size per scatter/gather element */
3496 sg_size = buf_len / (IPR_MAX_SGLIST - 1);
3497
3498 /* Get the actual size per element */
3499 order = get_order(sg_size);
3500
3501 /* Determine the actual number of bytes per element */
3502 bsize_elem = PAGE_SIZE * (1 << order);
3503
3504 /* Determine the actual number of sg entries needed */
3505 if (buf_len % bsize_elem)
3506 num_elem = (buf_len / bsize_elem) + 1;
3507 else
3508 num_elem = buf_len / bsize_elem;
3509
3510 /* Allocate a scatter/gather list for the DMA */
0bc42e35 3511 sglist = kzalloc(sizeof(struct ipr_sglist) +
1da177e4
LT
3512 (sizeof(struct scatterlist) * (num_elem - 1)),
3513 GFP_KERNEL);
3514
3515 if (sglist == NULL) {
3516 ipr_trace;
3517 return NULL;
3518 }
3519
1da177e4 3520 scatterlist = sglist->scatterlist;
45711f1a 3521 sg_init_table(scatterlist, num_elem);
1da177e4
LT
3522
3523 sglist->order = order;
3524 sglist->num_sg = num_elem;
3525
3526 /* Allocate a bunch of sg elements */
3527 for (i = 0; i < num_elem; i++) {
3528 page = alloc_pages(GFP_KERNEL, order);
3529 if (!page) {
3530 ipr_trace;
3531
3532 /* Free up what we already allocated */
3533 for (j = i - 1; j >= 0; j--)
45711f1a 3534 __free_pages(sg_page(&scatterlist[j]), order);
1da177e4
LT
3535 kfree(sglist);
3536 return NULL;
3537 }
3538
642f1490 3539 sg_set_page(&scatterlist[i], page, 0, 0);
1da177e4
LT
3540 }
3541
3542 return sglist;
3543}
3544
3545/**
3546 * ipr_free_ucode_buffer - Frees a microcode download buffer
3547 * @p_dnld: scatter/gather list pointer
3548 *
3549 * Free a DMA'able ucode download buffer previously allocated with
3550 * ipr_alloc_ucode_buffer
3551 *
3552 * Return value:
3553 * nothing
3554 **/
3555static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
3556{
3557 int i;
3558
3559 for (i = 0; i < sglist->num_sg; i++)
45711f1a 3560 __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
1da177e4
LT
3561
3562 kfree(sglist);
3563}
3564
3565/**
3566 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
3567 * @sglist: scatter/gather list pointer
3568 * @buffer: buffer pointer
3569 * @len: buffer length
3570 *
3571 * Copy a microcode image from a user buffer into a buffer allocated by
3572 * ipr_alloc_ucode_buffer
3573 *
3574 * Return value:
3575 * 0 on success / other on failure
3576 **/
3577static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
3578 u8 *buffer, u32 len)
3579{
3580 int bsize_elem, i, result = 0;
3581 struct scatterlist *scatterlist;
3582 void *kaddr;
3583
3584 /* Determine the actual number of bytes per element */
3585 bsize_elem = PAGE_SIZE * (1 << sglist->order);
3586
3587 scatterlist = sglist->scatterlist;
3588
3589 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
45711f1a
JA
3590 struct page *page = sg_page(&scatterlist[i]);
3591
3592 kaddr = kmap(page);
1da177e4 3593 memcpy(kaddr, buffer, bsize_elem);
45711f1a 3594 kunmap(page);
1da177e4
LT
3595
3596 scatterlist[i].length = bsize_elem;
3597
3598 if (result != 0) {
3599 ipr_trace;
3600 return result;
3601 }
3602 }
3603
3604 if (len % bsize_elem) {
45711f1a
JA
3605 struct page *page = sg_page(&scatterlist[i]);
3606
3607 kaddr = kmap(page);
1da177e4 3608 memcpy(kaddr, buffer, len % bsize_elem);
45711f1a 3609 kunmap(page);
1da177e4
LT
3610
3611 scatterlist[i].length = len % bsize_elem;
3612 }
3613
3614 sglist->buffer_len = len;
3615 return result;
3616}
3617
a32c055f
WB
3618/**
3619 * ipr_build_ucode_ioadl64 - Build a microcode download IOADL
3620 * @ipr_cmd: ipr command struct
3621 * @sglist: scatter/gather list
3622 *
3623 * Builds a microcode download IOA data list (IOADL).
3624 *
3625 **/
3626static void ipr_build_ucode_ioadl64(struct ipr_cmnd *ipr_cmd,
3627 struct ipr_sglist *sglist)
3628{
3629 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3630 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
3631 struct scatterlist *scatterlist = sglist->scatterlist;
3632 int i;
3633
3634 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
3635 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3636 ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3637
3638 ioarcb->ioadl_len =
3639 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
3640 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3641 ioadl64[i].flags = cpu_to_be32(IPR_IOADL_FLAGS_WRITE);
3642 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(&scatterlist[i]));
3643 ioadl64[i].address = cpu_to_be64(sg_dma_address(&scatterlist[i]));
3644 }
3645
3646 ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3647}
3648
1da177e4 3649/**
12baa420 3650 * ipr_build_ucode_ioadl - Build a microcode download IOADL
1da177e4
LT
3651 * @ipr_cmd: ipr command struct
3652 * @sglist: scatter/gather list
1da177e4 3653 *
12baa420 3654 * Builds a microcode download IOA data list (IOADL).
1da177e4 3655 *
1da177e4 3656 **/
12baa420 3657static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
3658 struct ipr_sglist *sglist)
1da177e4 3659{
1da177e4 3660 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
a32c055f 3661 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
1da177e4
LT
3662 struct scatterlist *scatterlist = sglist->scatterlist;
3663 int i;
3664
12baa420 3665 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
1da177e4 3666 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
a32c055f
WB
3667 ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
3668
3669 ioarcb->ioadl_len =
1da177e4
LT
3670 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3671
3672 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3673 ioadl[i].flags_and_data_len =
3674 cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
3675 ioadl[i].address =
3676 cpu_to_be32(sg_dma_address(&scatterlist[i]));
3677 }
3678
12baa420 3679 ioadl[i-1].flags_and_data_len |=
3680 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3681}
3682
3683/**
3684 * ipr_update_ioa_ucode - Update IOA's microcode
3685 * @ioa_cfg: ioa config struct
3686 * @sglist: scatter/gather list
3687 *
3688 * Initiate an adapter reset to update the IOA's microcode
3689 *
3690 * Return value:
3691 * 0 on success / -EIO on failure
3692 **/
3693static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
3694 struct ipr_sglist *sglist)
3695{
3696 unsigned long lock_flags;
3697
3698 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
970ea294
BK
3699 while(ioa_cfg->in_reset_reload) {
3700 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3701 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3702 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3703 }
12baa420 3704
3705 if (ioa_cfg->ucode_sglist) {
3706 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3707 dev_err(&ioa_cfg->pdev->dev,
3708 "Microcode download already in progress\n");
3709 return -EIO;
1da177e4 3710 }
12baa420 3711
3712 sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
3713 sglist->num_sg, DMA_TO_DEVICE);
3714
3715 if (!sglist->num_dma_sg) {
3716 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3717 dev_err(&ioa_cfg->pdev->dev,
3718 "Failed to map microcode download buffer!\n");
1da177e4
LT
3719 return -EIO;
3720 }
3721
12baa420 3722 ioa_cfg->ucode_sglist = sglist;
3723 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
3724 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3725 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
3726
3727 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3728 ioa_cfg->ucode_sglist = NULL;
3729 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1da177e4
LT
3730 return 0;
3731}
3732
3733/**
3734 * ipr_store_update_fw - Update the firmware on the adapter
ee959b00
TJ
3735 * @class_dev: device struct
3736 * @buf: buffer
3737 * @count: buffer size
1da177e4
LT
3738 *
3739 * This function will update the firmware on the adapter.
3740 *
3741 * Return value:
3742 * count on success / other on failure
3743 **/
ee959b00
TJ
3744static ssize_t ipr_store_update_fw(struct device *dev,
3745 struct device_attribute *attr,
3746 const char *buf, size_t count)
1da177e4 3747{
ee959b00 3748 struct Scsi_Host *shost = class_to_shost(dev);
1da177e4
LT
3749 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3750 struct ipr_ucode_image_header *image_hdr;
3751 const struct firmware *fw_entry;
3752 struct ipr_sglist *sglist;
1da177e4
LT
3753 char fname[100];
3754 char *src;
3755 int len, result, dnld_size;
3756
3757 if (!capable(CAP_SYS_ADMIN))
3758 return -EACCES;
3759
3760 len = snprintf(fname, 99, "%s", buf);
3761 fname[len-1] = '\0';
3762
3763 if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
3764 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
3765 return -EIO;
3766 }
3767
3768 image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
3769
1da177e4
LT
3770 src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
3771 dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
3772 sglist = ipr_alloc_ucode_buffer(dnld_size);
3773
3774 if (!sglist) {
3775 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
3776 release_firmware(fw_entry);
3777 return -ENOMEM;
3778 }
3779
3780 result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
3781
3782 if (result) {
3783 dev_err(&ioa_cfg->pdev->dev,
3784 "Microcode buffer copy to DMA buffer failed\n");
12baa420 3785 goto out;
1da177e4
LT
3786 }
3787
14ed9cc7
WB
3788 ipr_info("Updating microcode, please be patient. This may take up to 30 minutes.\n");
3789
12baa420 3790 result = ipr_update_ioa_ucode(ioa_cfg, sglist);
1da177e4 3791
12baa420 3792 if (!result)
3793 result = count;
3794out:
1da177e4
LT
3795 ipr_free_ucode_buffer(sglist);
3796 release_firmware(fw_entry);
12baa420 3797 return result;
1da177e4
LT
3798}
3799
ee959b00 3800static struct device_attribute ipr_update_fw_attr = {
1da177e4
LT
3801 .attr = {
3802 .name = "update_fw",
3803 .mode = S_IWUSR,
3804 },
3805 .store = ipr_store_update_fw
3806};
3807
75576bb9
WB
3808/**
3809 * ipr_show_fw_type - Show the adapter's firmware type.
3810 * @dev: class device struct
3811 * @buf: buffer
3812 *
3813 * Return value:
3814 * number of bytes printed to buffer
3815 **/
3816static ssize_t ipr_show_fw_type(struct device *dev,
3817 struct device_attribute *attr, char *buf)
3818{
3819 struct Scsi_Host *shost = class_to_shost(dev);
3820 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3821 unsigned long lock_flags = 0;
3822 int len;
3823
3824 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3825 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->sis64);
3826 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3827 return len;
3828}
3829
3830static struct device_attribute ipr_ioa_fw_type_attr = {
3831 .attr = {
3832 .name = "fw_type",
3833 .mode = S_IRUGO,
3834 },
3835 .show = ipr_show_fw_type
3836};
3837
ee959b00 3838static struct device_attribute *ipr_ioa_attrs[] = {
1da177e4
LT
3839 &ipr_fw_version_attr,
3840 &ipr_log_level_attr,
3841 &ipr_diagnostics_attr,
f37eb54b 3842 &ipr_ioa_state_attr,
1da177e4
LT
3843 &ipr_ioa_reset_attr,
3844 &ipr_update_fw_attr,
75576bb9 3845 &ipr_ioa_fw_type_attr,
1da177e4
LT
3846 NULL,
3847};
3848
3849#ifdef CONFIG_SCSI_IPR_DUMP
3850/**
3851 * ipr_read_dump - Dump the adapter
2c3c8bea 3852 * @filp: open sysfs file
1da177e4 3853 * @kobj: kobject struct
91a69029 3854 * @bin_attr: bin_attribute struct
1da177e4
LT
3855 * @buf: buffer
3856 * @off: offset
3857 * @count: buffer size
3858 *
3859 * Return value:
3860 * number of bytes printed to buffer
3861 **/
2c3c8bea 3862static ssize_t ipr_read_dump(struct file *filp, struct kobject *kobj,
91a69029
ZR
3863 struct bin_attribute *bin_attr,
3864 char *buf, loff_t off, size_t count)
1da177e4 3865{
ee959b00 3866 struct device *cdev = container_of(kobj, struct device, kobj);
1da177e4
LT
3867 struct Scsi_Host *shost = class_to_shost(cdev);
3868 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
3869 struct ipr_dump *dump;
3870 unsigned long lock_flags = 0;
3871 char *src;
4d4dd706 3872 int len, sdt_end;
1da177e4
LT
3873 size_t rc = count;
3874
3875 if (!capable(CAP_SYS_ADMIN))
3876 return -EACCES;
3877
3878 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3879 dump = ioa_cfg->dump;
3880
3881 if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
3882 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3883 return 0;
3884 }
3885 kref_get(&dump->kref);
3886 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3887
3888 if (off > dump->driver_dump.hdr.len) {
3889 kref_put(&dump->kref, ipr_release_dump);
3890 return 0;
3891 }
3892
3893 if (off + count > dump->driver_dump.hdr.len) {
3894 count = dump->driver_dump.hdr.len - off;
3895 rc = count;
3896 }
3897
3898 if (count && off < sizeof(dump->driver_dump)) {
3899 if (off + count > sizeof(dump->driver_dump))
3900 len = sizeof(dump->driver_dump) - off;
3901 else
3902 len = count;
3903 src = (u8 *)&dump->driver_dump + off;
3904 memcpy(buf, src, len);
3905 buf += len;
3906 off += len;
3907 count -= len;
3908 }
3909
3910 off -= sizeof(dump->driver_dump);
3911
4d4dd706
KSS
3912 if (ioa_cfg->sis64)
3913 sdt_end = offsetof(struct ipr_ioa_dump, sdt.entry) +
3914 (be32_to_cpu(dump->ioa_dump.sdt.hdr.num_entries_used) *
3915 sizeof(struct ipr_sdt_entry));
3916 else
3917 sdt_end = offsetof(struct ipr_ioa_dump, sdt.entry) +
3918 (IPR_FMT2_NUM_SDT_ENTRIES * sizeof(struct ipr_sdt_entry));
3919
3920 if (count && off < sdt_end) {
3921 if (off + count > sdt_end)
3922 len = sdt_end - off;
1da177e4
LT
3923 else
3924 len = count;
3925 src = (u8 *)&dump->ioa_dump + off;
3926 memcpy(buf, src, len);
3927 buf += len;
3928 off += len;
3929 count -= len;
3930 }
3931
4d4dd706 3932 off -= sdt_end;
1da177e4
LT
3933
3934 while (count) {
3935 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
3936 len = PAGE_ALIGN(off) - off;
3937 else
3938 len = count;
3939 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
3940 src += off & ~PAGE_MASK;
3941 memcpy(buf, src, len);
3942 buf += len;
3943 off += len;
3944 count -= len;
3945 }
3946
3947 kref_put(&dump->kref, ipr_release_dump);
3948 return rc;
3949}
3950
3951/**
3952 * ipr_alloc_dump - Prepare for adapter dump
3953 * @ioa_cfg: ioa config struct
3954 *
3955 * Return value:
3956 * 0 on success / other on failure
3957 **/
3958static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
3959{
3960 struct ipr_dump *dump;
4d4dd706 3961 __be32 **ioa_data;
1da177e4
LT
3962 unsigned long lock_flags = 0;
3963
0bc42e35 3964 dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
1da177e4
LT
3965
3966 if (!dump) {
3967 ipr_err("Dump memory allocation failed\n");
3968 return -ENOMEM;
3969 }
3970
4d4dd706
KSS
3971 if (ioa_cfg->sis64)
3972 ioa_data = vmalloc(IPR_FMT3_MAX_NUM_DUMP_PAGES * sizeof(__be32 *));
3973 else
3974 ioa_data = vmalloc(IPR_FMT2_MAX_NUM_DUMP_PAGES * sizeof(__be32 *));
3975
3976 if (!ioa_data) {
3977 ipr_err("Dump memory allocation failed\n");
3978 kfree(dump);
3979 return -ENOMEM;
3980 }
3981
3982 dump->ioa_dump.ioa_data = ioa_data;
3983
1da177e4
LT
3984 kref_init(&dump->kref);
3985 dump->ioa_cfg = ioa_cfg;
3986
3987 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3988
3989 if (INACTIVE != ioa_cfg->sdt_state) {
3990 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4d4dd706 3991 vfree(dump->ioa_dump.ioa_data);
1da177e4
LT
3992 kfree(dump);
3993 return 0;
3994 }
3995
3996 ioa_cfg->dump = dump;
3997 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
3998 if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
3999 ioa_cfg->dump_taken = 1;
4000 schedule_work(&ioa_cfg->work_q);
4001 }
4002 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4003
1da177e4
LT
4004 return 0;
4005}
4006
4007/**
4008 * ipr_free_dump - Free adapter dump memory
4009 * @ioa_cfg: ioa config struct
4010 *
4011 * Return value:
4012 * 0 on success / other on failure
4013 **/
4014static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
4015{
4016 struct ipr_dump *dump;
4017 unsigned long lock_flags = 0;
4018
4019 ENTER;
4020
4021 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4022 dump = ioa_cfg->dump;
4023 if (!dump) {
4024 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4025 return 0;
4026 }
4027
4028 ioa_cfg->dump = NULL;
4029 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4030
4031 kref_put(&dump->kref, ipr_release_dump);
4032
4033 LEAVE;
4034 return 0;
4035}
4036
4037/**
4038 * ipr_write_dump - Setup dump state of adapter
2c3c8bea 4039 * @filp: open sysfs file
1da177e4 4040 * @kobj: kobject struct
91a69029 4041 * @bin_attr: bin_attribute struct
1da177e4
LT
4042 * @buf: buffer
4043 * @off: offset
4044 * @count: buffer size
4045 *
4046 * Return value:
4047 * number of bytes printed to buffer
4048 **/
2c3c8bea 4049static ssize_t ipr_write_dump(struct file *filp, struct kobject *kobj,
91a69029
ZR
4050 struct bin_attribute *bin_attr,
4051 char *buf, loff_t off, size_t count)
1da177e4 4052{
ee959b00 4053 struct device *cdev = container_of(kobj, struct device, kobj);
1da177e4
LT
4054 struct Scsi_Host *shost = class_to_shost(cdev);
4055 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
4056 int rc;
4057
4058 if (!capable(CAP_SYS_ADMIN))
4059 return -EACCES;
4060
4061 if (buf[0] == '1')
4062 rc = ipr_alloc_dump(ioa_cfg);
4063 else if (buf[0] == '0')
4064 rc = ipr_free_dump(ioa_cfg);
4065 else
4066 return -EINVAL;
4067
4068 if (rc)
4069 return rc;
4070 else
4071 return count;
4072}
4073
4074static struct bin_attribute ipr_dump_attr = {
4075 .attr = {
4076 .name = "dump",
4077 .mode = S_IRUSR | S_IWUSR,
4078 },
4079 .size = 0,
4080 .read = ipr_read_dump,
4081 .write = ipr_write_dump
4082};
4083#else
4084static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
4085#endif
4086
4087/**
4088 * ipr_change_queue_depth - Change the device's queue depth
4089 * @sdev: scsi device struct
4090 * @qdepth: depth to set
e881a172 4091 * @reason: calling context
1da177e4
LT
4092 *
4093 * Return value:
4094 * actual depth set
4095 **/
e881a172
MC
4096static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth,
4097 int reason)
1da177e4 4098{
35a39691
BK
4099 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4100 struct ipr_resource_entry *res;
4101 unsigned long lock_flags = 0;
4102
e881a172
MC
4103 if (reason != SCSI_QDEPTH_DEFAULT)
4104 return -EOPNOTSUPP;
4105
35a39691
BK
4106 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4107 res = (struct ipr_resource_entry *)sdev->hostdata;
4108
4109 if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
4110 qdepth = IPR_MAX_CMD_PER_ATA_LUN;
4111 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4112
1da177e4
LT
4113 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
4114 return sdev->queue_depth;
4115}
4116
4117/**
4118 * ipr_change_queue_type - Change the device's queue type
4119 * @dsev: scsi device struct
4120 * @tag_type: type of tags to use
4121 *
4122 * Return value:
4123 * actual queue type set
4124 **/
4125static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
4126{
4127 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4128 struct ipr_resource_entry *res;
4129 unsigned long lock_flags = 0;
4130
4131 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4132 res = (struct ipr_resource_entry *)sdev->hostdata;
4133
4134 if (res) {
4135 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
4136 /*
4137 * We don't bother quiescing the device here since the
4138 * adapter firmware does it for us.
4139 */
4140 scsi_set_tag_type(sdev, tag_type);
4141
4142 if (tag_type)
4143 scsi_activate_tcq(sdev, sdev->queue_depth);
4144 else
4145 scsi_deactivate_tcq(sdev, sdev->queue_depth);
4146 } else
4147 tag_type = 0;
4148 } else
4149 tag_type = 0;
4150
4151 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4152 return tag_type;
4153}
4154
4155/**
4156 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
4157 * @dev: device struct
46d74563 4158 * @attr: device attribute structure
1da177e4
LT
4159 * @buf: buffer
4160 *
4161 * Return value:
4162 * number of bytes printed to buffer
4163 **/
10523b3b 4164static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
4165{
4166 struct scsi_device *sdev = to_scsi_device(dev);
4167 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4168 struct ipr_resource_entry *res;
4169 unsigned long lock_flags = 0;
4170 ssize_t len = -ENXIO;
4171
4172 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4173 res = (struct ipr_resource_entry *)sdev->hostdata;
4174 if (res)
3e7ebdfa 4175 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->res_handle);
1da177e4
LT
4176 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4177 return len;
4178}
4179
4180static struct device_attribute ipr_adapter_handle_attr = {
4181 .attr = {
4182 .name = "adapter_handle",
4183 .mode = S_IRUSR,
4184 },
4185 .show = ipr_show_adapter_handle
4186};
4187
3e7ebdfa 4188/**
5adcbeb3
WB
4189 * ipr_show_resource_path - Show the resource path or the resource address for
4190 * this device.
3e7ebdfa 4191 * @dev: device struct
46d74563 4192 * @attr: device attribute structure
3e7ebdfa
WB
4193 * @buf: buffer
4194 *
4195 * Return value:
4196 * number of bytes printed to buffer
4197 **/
4198static ssize_t ipr_show_resource_path(struct device *dev, struct device_attribute *attr, char *buf)
4199{
4200 struct scsi_device *sdev = to_scsi_device(dev);
4201 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4202 struct ipr_resource_entry *res;
4203 unsigned long lock_flags = 0;
4204 ssize_t len = -ENXIO;
4205 char buffer[IPR_MAX_RES_PATH_LENGTH];
4206
4207 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4208 res = (struct ipr_resource_entry *)sdev->hostdata;
5adcbeb3 4209 if (res && ioa_cfg->sis64)
3e7ebdfa 4210 len = snprintf(buf, PAGE_SIZE, "%s\n",
5adcbeb3
WB
4211 ipr_format_res_path(res->res_path, buffer,
4212 sizeof(buffer)));
4213 else if (res)
4214 len = snprintf(buf, PAGE_SIZE, "%d:%d:%d:%d\n", ioa_cfg->host->host_no,
4215 res->bus, res->target, res->lun);
4216
3e7ebdfa
WB
4217 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4218 return len;
4219}
4220
4221static struct device_attribute ipr_resource_path_attr = {
4222 .attr = {
4223 .name = "resource_path",
75576bb9 4224 .mode = S_IRUGO,
3e7ebdfa
WB
4225 },
4226 .show = ipr_show_resource_path
4227};
4228
46d74563
WB
4229/**
4230 * ipr_show_device_id - Show the device_id for this device.
4231 * @dev: device struct
4232 * @attr: device attribute structure
4233 * @buf: buffer
4234 *
4235 * Return value:
4236 * number of bytes printed to buffer
4237 **/
4238static ssize_t ipr_show_device_id(struct device *dev, struct device_attribute *attr, char *buf)
4239{
4240 struct scsi_device *sdev = to_scsi_device(dev);
4241 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4242 struct ipr_resource_entry *res;
4243 unsigned long lock_flags = 0;
4244 ssize_t len = -ENXIO;
4245
4246 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4247 res = (struct ipr_resource_entry *)sdev->hostdata;
4248 if (res && ioa_cfg->sis64)
4249 len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->dev_id);
4250 else if (res)
4251 len = snprintf(buf, PAGE_SIZE, "0x%llx\n", res->lun_wwn);
4252
4253 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4254 return len;
4255}
4256
4257static struct device_attribute ipr_device_id_attr = {
4258 .attr = {
4259 .name = "device_id",
4260 .mode = S_IRUGO,
4261 },
4262 .show = ipr_show_device_id
4263};
4264
75576bb9
WB
4265/**
4266 * ipr_show_resource_type - Show the resource type for this device.
4267 * @dev: device struct
46d74563 4268 * @attr: device attribute structure
75576bb9
WB
4269 * @buf: buffer
4270 *
4271 * Return value:
4272 * number of bytes printed to buffer
4273 **/
4274static ssize_t ipr_show_resource_type(struct device *dev, struct device_attribute *attr, char *buf)
4275{
4276 struct scsi_device *sdev = to_scsi_device(dev);
4277 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
4278 struct ipr_resource_entry *res;
4279 unsigned long lock_flags = 0;
4280 ssize_t len = -ENXIO;
4281
4282 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4283 res = (struct ipr_resource_entry *)sdev->hostdata;
4284
4285 if (res)
4286 len = snprintf(buf, PAGE_SIZE, "%x\n", res->type);
4287
4288 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4289 return len;
4290}
4291
4292static struct device_attribute ipr_resource_type_attr = {
4293 .attr = {
4294 .name = "resource_type",
4295 .mode = S_IRUGO,
4296 },
4297 .show = ipr_show_resource_type
4298};
4299
1da177e4
LT
4300static struct device_attribute *ipr_dev_attrs[] = {
4301 &ipr_adapter_handle_attr,
3e7ebdfa 4302 &ipr_resource_path_attr,
46d74563 4303 &ipr_device_id_attr,
75576bb9 4304 &ipr_resource_type_attr,
1da177e4
LT
4305 NULL,
4306};
4307
4308/**
4309 * ipr_biosparam - Return the HSC mapping
4310 * @sdev: scsi device struct
4311 * @block_device: block device pointer
4312 * @capacity: capacity of the device
4313 * @parm: Array containing returned HSC values.
4314 *
4315 * This function generates the HSC parms that fdisk uses.
4316 * We want to make sure we return something that places partitions
4317 * on 4k boundaries for best performance with the IOA.
4318 *
4319 * Return value:
4320 * 0 on success
4321 **/
4322static int ipr_biosparam(struct scsi_device *sdev,
4323 struct block_device *block_device,
4324 sector_t capacity, int *parm)
4325{
4326 int heads, sectors;
4327 sector_t cylinders;
4328
4329 heads = 128;
4330 sectors = 32;
4331
4332 cylinders = capacity;
4333 sector_div(cylinders, (128 * 32));
4334
4335 /* return result */
4336 parm[0] = heads;
4337 parm[1] = sectors;
4338 parm[2] = cylinders;
4339
4340 return 0;
4341}
4342
35a39691
BK
4343/**
4344 * ipr_find_starget - Find target based on bus/target.
4345 * @starget: scsi target struct
4346 *
4347 * Return value:
4348 * resource entry pointer if found / NULL if not found
4349 **/
4350static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
4351{
4352 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4353 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4354 struct ipr_resource_entry *res;
4355
4356 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3e7ebdfa 4357 if ((res->bus == starget->channel) &&
0ee1d714 4358 (res->target == starget->id)) {
35a39691
BK
4359 return res;
4360 }
4361 }
4362
4363 return NULL;
4364}
4365
4366static struct ata_port_info sata_port_info;
4367
4368/**
4369 * ipr_target_alloc - Prepare for commands to a SCSI target
4370 * @starget: scsi target struct
4371 *
4372 * If the device is a SATA device, this function allocates an
4373 * ATA port with libata, else it does nothing.
4374 *
4375 * Return value:
4376 * 0 on success / non-0 on failure
4377 **/
4378static int ipr_target_alloc(struct scsi_target *starget)
4379{
4380 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4381 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4382 struct ipr_sata_port *sata_port;
4383 struct ata_port *ap;
4384 struct ipr_resource_entry *res;
4385 unsigned long lock_flags;
4386
4387 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4388 res = ipr_find_starget(starget);
4389 starget->hostdata = NULL;
4390
4391 if (res && ipr_is_gata(res)) {
4392 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4393 sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
4394 if (!sata_port)
4395 return -ENOMEM;
4396
4397 ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
4398 if (ap) {
4399 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4400 sata_port->ioa_cfg = ioa_cfg;
4401 sata_port->ap = ap;
4402 sata_port->res = res;
4403
4404 res->sata_port = sata_port;
4405 ap->private_data = sata_port;
4406 starget->hostdata = sata_port;
4407 } else {
4408 kfree(sata_port);
4409 return -ENOMEM;
4410 }
4411 }
4412 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4413
4414 return 0;
4415}
4416
4417/**
4418 * ipr_target_destroy - Destroy a SCSI target
4419 * @starget: scsi target struct
4420 *
4421 * If the device was a SATA device, this function frees the libata
4422 * ATA port, else it does nothing.
4423 *
4424 **/
4425static void ipr_target_destroy(struct scsi_target *starget)
4426{
4427 struct ipr_sata_port *sata_port = starget->hostdata;
3e7ebdfa
WB
4428 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4429 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
4430
4431 if (ioa_cfg->sis64) {
0ee1d714
BK
4432 if (!ipr_find_starget(starget)) {
4433 if (starget->channel == IPR_ARRAY_VIRTUAL_BUS)
4434 clear_bit(starget->id, ioa_cfg->array_ids);
4435 else if (starget->channel == IPR_VSET_VIRTUAL_BUS)
4436 clear_bit(starget->id, ioa_cfg->vset_ids);
4437 else if (starget->channel == 0)
4438 clear_bit(starget->id, ioa_cfg->target_ids);
4439 }
3e7ebdfa 4440 }
35a39691
BK
4441
4442 if (sata_port) {
4443 starget->hostdata = NULL;
4444 ata_sas_port_destroy(sata_port->ap);
4445 kfree(sata_port);
4446 }
4447}
4448
4449/**
4450 * ipr_find_sdev - Find device based on bus/target/lun.
4451 * @sdev: scsi device struct
4452 *
4453 * Return value:
4454 * resource entry pointer if found / NULL if not found
4455 **/
4456static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
4457{
4458 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4459 struct ipr_resource_entry *res;
4460
4461 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3e7ebdfa
WB
4462 if ((res->bus == sdev->channel) &&
4463 (res->target == sdev->id) &&
4464 (res->lun == sdev->lun))
35a39691
BK
4465 return res;
4466 }
4467
4468 return NULL;
4469}
4470
1da177e4
LT
4471/**
4472 * ipr_slave_destroy - Unconfigure a SCSI device
4473 * @sdev: scsi device struct
4474 *
4475 * Return value:
4476 * nothing
4477 **/
4478static void ipr_slave_destroy(struct scsi_device *sdev)
4479{
4480 struct ipr_resource_entry *res;
4481 struct ipr_ioa_cfg *ioa_cfg;
4482 unsigned long lock_flags = 0;
4483
4484 ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4485
4486 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4487 res = (struct ipr_resource_entry *) sdev->hostdata;
4488 if (res) {
35a39691 4489 if (res->sata_port)
3e4ec344 4490 res->sata_port->ap->link.device[0].class = ATA_DEV_NONE;
1da177e4
LT
4491 sdev->hostdata = NULL;
4492 res->sdev = NULL;
35a39691 4493 res->sata_port = NULL;
1da177e4
LT
4494 }
4495 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4496}
4497
4498/**
4499 * ipr_slave_configure - Configure a SCSI device
4500 * @sdev: scsi device struct
4501 *
4502 * This function configures the specified scsi device.
4503 *
4504 * Return value:
4505 * 0 on success
4506 **/
4507static int ipr_slave_configure(struct scsi_device *sdev)
4508{
4509 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4510 struct ipr_resource_entry *res;
dd406ef8 4511 struct ata_port *ap = NULL;
1da177e4 4512 unsigned long lock_flags = 0;
3e7ebdfa 4513 char buffer[IPR_MAX_RES_PATH_LENGTH];
1da177e4
LT
4514
4515 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4516 res = sdev->hostdata;
4517 if (res) {
4518 if (ipr_is_af_dasd_device(res))
4519 sdev->type = TYPE_RAID;
0726ce26 4520 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
1da177e4 4521 sdev->scsi_level = 4;
0726ce26 4522 sdev->no_uld_attach = 1;
4523 }
1da177e4 4524 if (ipr_is_vset_device(res)) {
242f9dcb
JA
4525 blk_queue_rq_timeout(sdev->request_queue,
4526 IPR_VSET_RW_TIMEOUT);
086fa5ff 4527 blk_queue_max_hw_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
1da177e4 4528 }
dd406ef8
BK
4529 if (ipr_is_gata(res) && res->sata_port)
4530 ap = res->sata_port->ap;
4531 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4532
4533 if (ap) {
35a39691 4534 scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
dd406ef8
BK
4535 ata_sas_slave_configure(sdev, ap);
4536 } else
35a39691 4537 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
3e7ebdfa
WB
4538 if (ioa_cfg->sis64)
4539 sdev_printk(KERN_INFO, sdev, "Resource path: %s\n",
5adcbeb3
WB
4540 ipr_format_res_path(res->res_path, buffer,
4541 sizeof(buffer)));
dd406ef8 4542 return 0;
1da177e4
LT
4543 }
4544 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4545 return 0;
4546}
4547
35a39691
BK
4548/**
4549 * ipr_ata_slave_alloc - Prepare for commands to a SATA device
4550 * @sdev: scsi device struct
4551 *
4552 * This function initializes an ATA port so that future commands
4553 * sent through queuecommand will work.
4554 *
4555 * Return value:
4556 * 0 on success
4557 **/
4558static int ipr_ata_slave_alloc(struct scsi_device *sdev)
4559{
4560 struct ipr_sata_port *sata_port = NULL;
4561 int rc = -ENXIO;
4562
4563 ENTER;
4564 if (sdev->sdev_target)
4565 sata_port = sdev->sdev_target->hostdata;
b2024459 4566 if (sata_port) {
35a39691 4567 rc = ata_sas_port_init(sata_port->ap);
b2024459
DW
4568 if (rc == 0)
4569 rc = ata_sas_sync_probe(sata_port->ap);
4570 }
4571
35a39691
BK
4572 if (rc)
4573 ipr_slave_destroy(sdev);
4574
4575 LEAVE;
4576 return rc;
4577}
4578
1da177e4
LT
4579/**
4580 * ipr_slave_alloc - Prepare for commands to a device.
4581 * @sdev: scsi device struct
4582 *
4583 * This function saves a pointer to the resource entry
4584 * in the scsi device struct if the device exists. We
4585 * can then use this pointer in ipr_queuecommand when
4586 * handling new commands.
4587 *
4588 * Return value:
692aebfc 4589 * 0 on success / -ENXIO if device does not exist
1da177e4
LT
4590 **/
4591static int ipr_slave_alloc(struct scsi_device *sdev)
4592{
4593 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
4594 struct ipr_resource_entry *res;
4595 unsigned long lock_flags;
692aebfc 4596 int rc = -ENXIO;
1da177e4
LT
4597
4598 sdev->hostdata = NULL;
4599
4600 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4601
35a39691
BK
4602 res = ipr_find_sdev(sdev);
4603 if (res) {
4604 res->sdev = sdev;
4605 res->add_to_ml = 0;
4606 res->in_erp = 0;
4607 sdev->hostdata = res;
4608 if (!ipr_is_naca_model(res))
4609 res->needs_sync_complete = 1;
4610 rc = 0;
4611 if (ipr_is_gata(res)) {
4612 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4613 return ipr_ata_slave_alloc(sdev);
1da177e4
LT
4614 }
4615 }
4616
4617 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4618
692aebfc 4619 return rc;
1da177e4
LT
4620}
4621
4622/**
4623 * ipr_eh_host_reset - Reset the host adapter
4624 * @scsi_cmd: scsi command struct
4625 *
4626 * Return value:
4627 * SUCCESS / FAILED
4628 **/
df0ae249 4629static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
1da177e4
LT
4630{
4631 struct ipr_ioa_cfg *ioa_cfg;
4632 int rc;
4633
4634 ENTER;
4635 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
4636
a92fa25c
KSS
4637 if (!ioa_cfg->in_reset_reload) {
4638 dev_err(&ioa_cfg->pdev->dev,
4639 "Adapter being reset as a result of error recovery.\n");
1da177e4 4640
a92fa25c
KSS
4641 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
4642 ioa_cfg->sdt_state = GET_DUMP;
4643 }
1da177e4
LT
4644
4645 rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
4646
4647 LEAVE;
4648 return rc;
4649}
4650
df0ae249
JG
4651static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
4652{
4653 int rc;
4654
4655 spin_lock_irq(cmd->device->host->host_lock);
4656 rc = __ipr_eh_host_reset(cmd);
4657 spin_unlock_irq(cmd->device->host->host_lock);
4658
4659 return rc;
4660}
4661
c6513096
BK
4662/**
4663 * ipr_device_reset - Reset the device
4664 * @ioa_cfg: ioa config struct
4665 * @res: resource entry struct
4666 *
4667 * This function issues a device reset to the affected device.
4668 * If the device is a SCSI device, a LUN reset will be sent
4669 * to the device first. If that does not work, a target reset
35a39691
BK
4670 * will be sent. If the device is a SATA device, a PHY reset will
4671 * be sent.
c6513096
BK
4672 *
4673 * Return value:
4674 * 0 on success / non-zero on failure
4675 **/
4676static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
4677 struct ipr_resource_entry *res)
4678{
4679 struct ipr_cmnd *ipr_cmd;
4680 struct ipr_ioarcb *ioarcb;
4681 struct ipr_cmd_pkt *cmd_pkt;
35a39691 4682 struct ipr_ioarcb_ata_regs *regs;
c6513096
BK
4683 u32 ioasc;
4684
4685 ENTER;
4686 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4687 ioarcb = &ipr_cmd->ioarcb;
4688 cmd_pkt = &ioarcb->cmd_pkt;
a32c055f
WB
4689
4690 if (ipr_cmd->ioa_cfg->sis64) {
4691 regs = &ipr_cmd->i.ata_ioadl.regs;
4692 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
4693 } else
4694 regs = &ioarcb->u.add_data.u.regs;
c6513096 4695
3e7ebdfa 4696 ioarcb->res_handle = res->res_handle;
c6513096
BK
4697 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4698 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
35a39691
BK
4699 if (ipr_is_gata(res)) {
4700 cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
a32c055f 4701 ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(regs->flags));
35a39691
BK
4702 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
4703 }
c6513096
BK
4704
4705 ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
96d21f00 4706 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
c6513096 4707 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
96d21f00
WB
4708 if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET) {
4709 if (ipr_cmd->ioa_cfg->sis64)
4710 memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
4711 sizeof(struct ipr_ioasa_gata));
4712 else
4713 memcpy(&res->sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
4714 sizeof(struct ipr_ioasa_gata));
4715 }
c6513096
BK
4716
4717 LEAVE;
4718 return (IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0);
4719}
4720
35a39691
BK
4721/**
4722 * ipr_sata_reset - Reset the SATA port
cc0680a5 4723 * @link: SATA link to reset
35a39691
BK
4724 * @classes: class of the attached device
4725 *
cc0680a5 4726 * This function issues a SATA phy reset to the affected ATA link.
35a39691
BK
4727 *
4728 * Return value:
4729 * 0 on success / non-zero on failure
4730 **/
cc0680a5 4731static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
120bda35 4732 unsigned long deadline)
35a39691 4733{
cc0680a5 4734 struct ipr_sata_port *sata_port = link->ap->private_data;
35a39691
BK
4735 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
4736 struct ipr_resource_entry *res;
4737 unsigned long lock_flags = 0;
4738 int rc = -ENXIO;
4739
4740 ENTER;
4741 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
73d98ff0
BK
4742 while(ioa_cfg->in_reset_reload) {
4743 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4744 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
4745 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4746 }
4747
35a39691
BK
4748 res = sata_port->res;
4749 if (res) {
4750 rc = ipr_device_reset(ioa_cfg, res);
3e7ebdfa 4751 *classes = res->ata_class;
35a39691
BK
4752 }
4753
4754 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4755 LEAVE;
4756 return rc;
4757}
4758
1da177e4
LT
4759/**
4760 * ipr_eh_dev_reset - Reset the device
4761 * @scsi_cmd: scsi command struct
4762 *
4763 * This function issues a device reset to the affected device.
4764 * A LUN reset will be sent to the device first. If that does
4765 * not work, a target reset will be sent.
4766 *
4767 * Return value:
4768 * SUCCESS / FAILED
4769 **/
94d0e7b8 4770static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
1da177e4
LT
4771{
4772 struct ipr_cmnd *ipr_cmd;
4773 struct ipr_ioa_cfg *ioa_cfg;
4774 struct ipr_resource_entry *res;
35a39691
BK
4775 struct ata_port *ap;
4776 int rc = 0;
1da177e4
LT
4777
4778 ENTER;
4779 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
4780 res = scsi_cmd->device->hostdata;
4781
eeb88307 4782 if (!res)
1da177e4
LT
4783 return FAILED;
4784
4785 /*
4786 * If we are currently going through reset/reload, return failed. This will force the
4787 * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
4788 * reset to complete
4789 */
4790 if (ioa_cfg->in_reset_reload)
4791 return FAILED;
4792 if (ioa_cfg->ioa_is_dead)
4793 return FAILED;
4794
4795 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3e7ebdfa 4796 if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
1da177e4
LT
4797 if (ipr_cmd->scsi_cmd)
4798 ipr_cmd->done = ipr_scsi_eh_done;
24d6f2b5
BK
4799 if (ipr_cmd->qc)
4800 ipr_cmd->done = ipr_sata_eh_done;
7402ecef
BK
4801 if (ipr_cmd->qc && !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
4802 ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
4803 ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
4804 }
1da177e4
LT
4805 }
4806 }
4807
4808 res->resetting_device = 1;
fb3ed3cb 4809 scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
35a39691
BK
4810
4811 if (ipr_is_gata(res) && res->sata_port) {
4812 ap = res->sata_port->ap;
4813 spin_unlock_irq(scsi_cmd->device->host->host_lock);
a1efdaba 4814 ata_std_error_handler(ap);
35a39691 4815 spin_lock_irq(scsi_cmd->device->host->host_lock);
5af23d26
BK
4816
4817 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3e7ebdfa 4818 if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
5af23d26
BK
4819 rc = -EIO;
4820 break;
4821 }
4822 }
35a39691
BK
4823 } else
4824 rc = ipr_device_reset(ioa_cfg, res);
1da177e4
LT
4825 res->resetting_device = 0;
4826
1da177e4 4827 LEAVE;
c6513096 4828 return (rc ? FAILED : SUCCESS);
1da177e4
LT
4829}
4830
94d0e7b8
JG
4831static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
4832{
4833 int rc;
4834
4835 spin_lock_irq(cmd->device->host->host_lock);
4836 rc = __ipr_eh_dev_reset(cmd);
4837 spin_unlock_irq(cmd->device->host->host_lock);
4838
4839 return rc;
4840}
4841
1da177e4
LT
4842/**
4843 * ipr_bus_reset_done - Op done function for bus reset.
4844 * @ipr_cmd: ipr command struct
4845 *
4846 * This function is the op done function for a bus reset
4847 *
4848 * Return value:
4849 * none
4850 **/
4851static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
4852{
4853 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4854 struct ipr_resource_entry *res;
4855
4856 ENTER;
3e7ebdfa
WB
4857 if (!ioa_cfg->sis64)
4858 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4859 if (res->res_handle == ipr_cmd->ioarcb.res_handle) {
4860 scsi_report_bus_reset(ioa_cfg->host, res->bus);
4861 break;
4862 }
1da177e4 4863 }
1da177e4
LT
4864
4865 /*
4866 * If abort has not completed, indicate the reset has, else call the
4867 * abort's done function to wake the sleeping eh thread
4868 */
4869 if (ipr_cmd->sibling->sibling)
4870 ipr_cmd->sibling->sibling = NULL;
4871 else
4872 ipr_cmd->sibling->done(ipr_cmd->sibling);
4873
4874 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4875 LEAVE;
4876}
4877
4878/**
4879 * ipr_abort_timeout - An abort task has timed out
4880 * @ipr_cmd: ipr command struct
4881 *
4882 * This function handles when an abort task times out. If this
4883 * happens we issue a bus reset since we have resources tied
4884 * up that must be freed before returning to the midlayer.
4885 *
4886 * Return value:
4887 * none
4888 **/
4889static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
4890{
4891 struct ipr_cmnd *reset_cmd;
4892 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4893 struct ipr_cmd_pkt *cmd_pkt;
4894 unsigned long lock_flags = 0;
4895
4896 ENTER;
4897 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4898 if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
4899 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4900 return;
4901 }
4902
fb3ed3cb 4903 sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
1da177e4
LT
4904 reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4905 ipr_cmd->sibling = reset_cmd;
4906 reset_cmd->sibling = ipr_cmd;
4907 reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
4908 cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
4909 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4910 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
4911 cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
4912
4913 ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
4914 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4915 LEAVE;
4916}
4917
4918/**
4919 * ipr_cancel_op - Cancel specified op
4920 * @scsi_cmd: scsi command struct
4921 *
4922 * This function cancels specified op.
4923 *
4924 * Return value:
4925 * SUCCESS / FAILED
4926 **/
4927static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
4928{
4929 struct ipr_cmnd *ipr_cmd;
4930 struct ipr_ioa_cfg *ioa_cfg;
4931 struct ipr_resource_entry *res;
4932 struct ipr_cmd_pkt *cmd_pkt;
a92fa25c 4933 u32 ioasc, int_reg;
1da177e4
LT
4934 int op_found = 0;
4935
4936 ENTER;
4937 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4938 res = scsi_cmd->device->hostdata;
4939
8fa728a2
JG
4940 /* If we are currently going through reset/reload, return failed.
4941 * This will force the mid-layer to call ipr_eh_host_reset,
4942 * which will then go to sleep and wait for the reset to complete
4943 */
4944 if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
4945 return FAILED;
a92fa25c
KSS
4946 if (!res)
4947 return FAILED;
4948
4949 /*
4950 * If we are aborting a timed out op, chances are that the timeout was caused
4951 * by a still not detected EEH error. In such cases, reading a register will
4952 * trigger the EEH recovery infrastructure.
4953 */
4954 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4955
4956 if (!ipr_is_gscsi(res))
1da177e4
LT
4957 return FAILED;
4958
4959 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
4960 if (ipr_cmd->scsi_cmd == scsi_cmd) {
4961 ipr_cmd->done = ipr_scsi_eh_done;
4962 op_found = 1;
4963 break;
4964 }
4965 }
4966
4967 if (!op_found)
4968 return SUCCESS;
4969
4970 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3e7ebdfa 4971 ipr_cmd->ioarcb.res_handle = res->res_handle;
1da177e4
LT
4972 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
4973 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
4974 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
4975 ipr_cmd->u.sdev = scsi_cmd->device;
4976
fb3ed3cb
BK
4977 scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
4978 scsi_cmd->cmnd[0]);
1da177e4 4979 ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
96d21f00 4980 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
4981
4982 /*
4983 * If the abort task timed out and we sent a bus reset, we will get
4984 * one the following responses to the abort
4985 */
4986 if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
4987 ioasc = 0;
4988 ipr_trace;
4989 }
4990
4991 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
ee0a90fa 4992 if (!ipr_is_naca_model(res))
4993 res->needs_sync_complete = 1;
1da177e4
LT
4994
4995 LEAVE;
4996 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
4997}
4998
4999/**
5000 * ipr_eh_abort - Abort a single op
5001 * @scsi_cmd: scsi command struct
5002 *
5003 * Return value:
5004 * SUCCESS / FAILED
5005 **/
5006static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
5007{
8fa728a2
JG
5008 unsigned long flags;
5009 int rc;
1da177e4
LT
5010
5011 ENTER;
1da177e4 5012
8fa728a2
JG
5013 spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
5014 rc = ipr_cancel_op(scsi_cmd);
5015 spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
1da177e4
LT
5016
5017 LEAVE;
8fa728a2 5018 return rc;
1da177e4
LT
5019}
5020
5021/**
5022 * ipr_handle_other_interrupt - Handle "other" interrupts
5023 * @ioa_cfg: ioa config struct
634651fa 5024 * @int_reg: interrupt register
1da177e4
LT
5025 *
5026 * Return value:
5027 * IRQ_NONE / IRQ_HANDLED
5028 **/
634651fa 5029static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
630ad831 5030 u32 int_reg)
1da177e4
LT
5031{
5032 irqreturn_t rc = IRQ_HANDLED;
7dacb64f
WB
5033 u32 int_mask_reg;
5034
5035 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
5036 int_reg &= ~int_mask_reg;
5037
5038 /* If an interrupt on the adapter did not occur, ignore it.
5039 * Or in the case of SIS 64, check for a stage change interrupt.
5040 */
5041 if ((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0) {
5042 if (ioa_cfg->sis64) {
5043 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
5044 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
5045 if (int_reg & IPR_PCII_IPL_STAGE_CHANGE) {
5046
5047 /* clear stage change */
5048 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.clr_interrupt_reg);
5049 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
5050 list_del(&ioa_cfg->reset_cmd->queue);
5051 del_timer(&ioa_cfg->reset_cmd->timer);
5052 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
5053 return IRQ_HANDLED;
5054 }
5055 }
5056
5057 return IRQ_NONE;
5058 }
1da177e4
LT
5059
5060 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
5061 /* Mask the interrupt */
5062 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
5063
5064 /* Clear the interrupt */
5065 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
5066 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5067
5068 list_del(&ioa_cfg->reset_cmd->queue);
5069 del_timer(&ioa_cfg->reset_cmd->timer);
5070 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
7dacb64f 5071 } else if ((int_reg & IPR_PCII_HRRQ_UPDATED) == int_reg) {
7dd21308
BK
5072 if (ioa_cfg->clear_isr) {
5073 if (ipr_debug && printk_ratelimit())
5074 dev_err(&ioa_cfg->pdev->dev,
5075 "Spurious interrupt detected. 0x%08X\n", int_reg);
5076 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg32);
5077 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5078 return IRQ_NONE;
5079 }
1da177e4
LT
5080 } else {
5081 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
5082 ioa_cfg->ioa_unit_checked = 1;
5083 else
5084 dev_err(&ioa_cfg->pdev->dev,
5085 "Permanent IOA failure. 0x%08X\n", int_reg);
5086
5087 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
5088 ioa_cfg->sdt_state = GET_DUMP;
5089
5090 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
5091 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5092 }
5093
5094 return rc;
5095}
5096
3feeb89d
WB
5097/**
5098 * ipr_isr_eh - Interrupt service routine error handler
5099 * @ioa_cfg: ioa config struct
5100 * @msg: message to log
5101 *
5102 * Return value:
5103 * none
5104 **/
5105static void ipr_isr_eh(struct ipr_ioa_cfg *ioa_cfg, char *msg)
5106{
5107 ioa_cfg->errors_logged++;
5108 dev_err(&ioa_cfg->pdev->dev, "%s\n", msg);
5109
5110 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
5111 ioa_cfg->sdt_state = GET_DUMP;
5112
5113 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5114}
5115
1da177e4
LT
5116/**
5117 * ipr_isr - Interrupt service routine
5118 * @irq: irq number
5119 * @devp: pointer to ioa config struct
1da177e4
LT
5120 *
5121 * Return value:
5122 * IRQ_NONE / IRQ_HANDLED
5123 **/
7d12e780 5124static irqreturn_t ipr_isr(int irq, void *devp)
1da177e4
LT
5125{
5126 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
5127 unsigned long lock_flags = 0;
7dacb64f 5128 u32 int_reg = 0;
1da177e4
LT
5129 u32 ioasc;
5130 u16 cmd_index;
3feeb89d 5131 int num_hrrq = 0;
7dacb64f 5132 int irq_none = 0;
1da177e4
LT
5133 struct ipr_cmnd *ipr_cmd;
5134 irqreturn_t rc = IRQ_NONE;
5135
5136 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5137
5138 /* If interrupts are disabled, ignore the interrupt */
5139 if (!ioa_cfg->allow_interrupts) {
5140 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5141 return IRQ_NONE;
5142 }
5143
1da177e4
LT
5144 while (1) {
5145 ipr_cmd = NULL;
5146
5147 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
5148 ioa_cfg->toggle_bit) {
5149
5150 cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
5151 IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
5152
5153 if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
3feeb89d 5154 ipr_isr_eh(ioa_cfg, "Invalid response handle from IOA");
1da177e4
LT
5155 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5156 return IRQ_HANDLED;
5157 }
5158
5159 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
5160
96d21f00 5161 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
5162
5163 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
5164
5165 list_del(&ipr_cmd->queue);
5166 del_timer(&ipr_cmd->timer);
5167 ipr_cmd->done(ipr_cmd);
5168
5169 rc = IRQ_HANDLED;
5170
5171 if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
5172 ioa_cfg->hrrq_curr++;
5173 } else {
5174 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
5175 ioa_cfg->toggle_bit ^= 1u;
5176 }
5177 }
5178
7dd21308
BK
5179 if (ipr_cmd && !ioa_cfg->clear_isr)
5180 break;
5181
1da177e4
LT
5182 if (ipr_cmd != NULL) {
5183 /* Clear the PCI interrupt */
a5442ba4 5184 num_hrrq = 0;
3feeb89d 5185 do {
214777ba 5186 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg32);
7dacb64f 5187 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
3feeb89d
WB
5188 } while (int_reg & IPR_PCII_HRRQ_UPDATED &&
5189 num_hrrq++ < IPR_MAX_HRRQ_RETRIES);
5190
7dacb64f
WB
5191 } else if (rc == IRQ_NONE && irq_none == 0) {
5192 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
5193 irq_none++;
a5442ba4
WB
5194 } else if (num_hrrq == IPR_MAX_HRRQ_RETRIES &&
5195 int_reg & IPR_PCII_HRRQ_UPDATED) {
5196 ipr_isr_eh(ioa_cfg, "Error clearing HRRQ");
5197 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5198 return IRQ_HANDLED;
1da177e4
LT
5199 } else
5200 break;
5201 }
5202
5203 if (unlikely(rc == IRQ_NONE))
634651fa 5204 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
1da177e4
LT
5205
5206 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5207 return rc;
5208}
5209
a32c055f
WB
5210/**
5211 * ipr_build_ioadl64 - Build a scatter/gather list and map the buffer
5212 * @ioa_cfg: ioa config struct
5213 * @ipr_cmd: ipr command struct
5214 *
5215 * Return value:
5216 * 0 on success / -1 on failure
5217 **/
5218static int ipr_build_ioadl64(struct ipr_ioa_cfg *ioa_cfg,
5219 struct ipr_cmnd *ipr_cmd)
5220{
5221 int i, nseg;
5222 struct scatterlist *sg;
5223 u32 length;
5224 u32 ioadl_flags = 0;
5225 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5226 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
5227 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
5228
5229 length = scsi_bufflen(scsi_cmd);
5230 if (!length)
5231 return 0;
5232
5233 nseg = scsi_dma_map(scsi_cmd);
5234 if (nseg < 0) {
51f52a47
AB
5235 if (printk_ratelimit())
5236 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
a32c055f
WB
5237 return -1;
5238 }
5239
5240 ipr_cmd->dma_use_sg = nseg;
5241
438b0331 5242 ioarcb->data_transfer_length = cpu_to_be32(length);
b8803b1c
WB
5243 ioarcb->ioadl_len =
5244 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
438b0331 5245
a32c055f
WB
5246 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5247 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5248 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
5249 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE)
5250 ioadl_flags = IPR_IOADL_FLAGS_READ;
5251
5252 scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5253 ioadl64[i].flags = cpu_to_be32(ioadl_flags);
5254 ioadl64[i].data_len = cpu_to_be32(sg_dma_len(sg));
5255 ioadl64[i].address = cpu_to_be64(sg_dma_address(sg));
5256 }
5257
5258 ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5259 return 0;
5260}
5261
1da177e4
LT
5262/**
5263 * ipr_build_ioadl - Build a scatter/gather list and map the buffer
5264 * @ioa_cfg: ioa config struct
5265 * @ipr_cmd: ipr command struct
5266 *
5267 * Return value:
5268 * 0 on success / -1 on failure
5269 **/
5270static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
5271 struct ipr_cmnd *ipr_cmd)
5272{
63015bc9
FT
5273 int i, nseg;
5274 struct scatterlist *sg;
1da177e4
LT
5275 u32 length;
5276 u32 ioadl_flags = 0;
5277 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5278 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
a32c055f 5279 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
1da177e4 5280
63015bc9
FT
5281 length = scsi_bufflen(scsi_cmd);
5282 if (!length)
1da177e4
LT
5283 return 0;
5284
63015bc9
FT
5285 nseg = scsi_dma_map(scsi_cmd);
5286 if (nseg < 0) {
5287 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
5288 return -1;
5289 }
51b1c7e1 5290
63015bc9
FT
5291 ipr_cmd->dma_use_sg = nseg;
5292
5293 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
5294 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
5295 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
a32c055f
WB
5296 ioarcb->data_transfer_length = cpu_to_be32(length);
5297 ioarcb->ioadl_len =
63015bc9
FT
5298 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5299 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
5300 ioadl_flags = IPR_IOADL_FLAGS_READ;
5301 ioarcb->read_data_transfer_length = cpu_to_be32(length);
5302 ioarcb->read_ioadl_len =
5303 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
5304 }
1da177e4 5305
a32c055f
WB
5306 if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->u.add_data.u.ioadl)) {
5307 ioadl = ioarcb->u.add_data.u.ioadl;
5308 ioarcb->write_ioadl_addr = cpu_to_be32((ipr_cmd->dma_addr) +
5309 offsetof(struct ipr_ioarcb, u.add_data));
63015bc9
FT
5310 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5311 }
1da177e4 5312
63015bc9
FT
5313 scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
5314 ioadl[i].flags_and_data_len =
5315 cpu_to_be32(ioadl_flags | sg_dma_len(sg));
5316 ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
1da177e4
LT
5317 }
5318
63015bc9
FT
5319 ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
5320 return 0;
1da177e4
LT
5321}
5322
5323/**
5324 * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
5325 * @scsi_cmd: scsi command struct
5326 *
5327 * Return value:
5328 * task attributes
5329 **/
5330static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
5331{
5332 u8 tag[2];
5333 u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
5334
5335 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
5336 switch (tag[0]) {
5337 case MSG_SIMPLE_TAG:
5338 rc = IPR_FLAGS_LO_SIMPLE_TASK;
5339 break;
5340 case MSG_HEAD_TAG:
5341 rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
5342 break;
5343 case MSG_ORDERED_TAG:
5344 rc = IPR_FLAGS_LO_ORDERED_TASK;
5345 break;
5346 };
5347 }
5348
5349 return rc;
5350}
5351
5352/**
5353 * ipr_erp_done - Process completion of ERP for a device
5354 * @ipr_cmd: ipr command struct
5355 *
5356 * This function copies the sense buffer into the scsi_cmd
5357 * struct and pushes the scsi_done function.
5358 *
5359 * Return value:
5360 * nothing
5361 **/
5362static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
5363{
5364 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5365 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5366 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
96d21f00 5367 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
5368
5369 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5370 scsi_cmd->result |= (DID_ERROR << 16);
fb3ed3cb
BK
5371 scmd_printk(KERN_ERR, scsi_cmd,
5372 "Request Sense failed with IOASC: 0x%08X\n", ioasc);
1da177e4
LT
5373 } else {
5374 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
5375 SCSI_SENSE_BUFFERSIZE);
5376 }
5377
5378 if (res) {
ee0a90fa 5379 if (!ipr_is_naca_model(res))
5380 res->needs_sync_complete = 1;
1da177e4
LT
5381 res->in_erp = 0;
5382 }
63015bc9 5383 scsi_dma_unmap(ipr_cmd->scsi_cmd);
1da177e4
LT
5384 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5385 scsi_cmd->scsi_done(scsi_cmd);
5386}
5387
5388/**
5389 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
5390 * @ipr_cmd: ipr command struct
5391 *
5392 * Return value:
5393 * none
5394 **/
5395static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
5396{
51b1c7e1 5397 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
96d21f00 5398 struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
a32c055f 5399 dma_addr_t dma_addr = ipr_cmd->dma_addr;
1da177e4
LT
5400
5401 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
a32c055f 5402 ioarcb->data_transfer_length = 0;
1da177e4 5403 ioarcb->read_data_transfer_length = 0;
a32c055f 5404 ioarcb->ioadl_len = 0;
1da177e4 5405 ioarcb->read_ioadl_len = 0;
96d21f00
WB
5406 ioasa->hdr.ioasc = 0;
5407 ioasa->hdr.residual_data_len = 0;
a32c055f
WB
5408
5409 if (ipr_cmd->ioa_cfg->sis64)
5410 ioarcb->u.sis64_addr_data.data_ioadl_addr =
5411 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
5412 else {
5413 ioarcb->write_ioadl_addr =
5414 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
5415 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5416 }
1da177e4
LT
5417}
5418
5419/**
5420 * ipr_erp_request_sense - Send request sense to a device
5421 * @ipr_cmd: ipr command struct
5422 *
5423 * This function sends a request sense to a device as a result
5424 * of a check condition.
5425 *
5426 * Return value:
5427 * nothing
5428 **/
5429static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
5430{
5431 struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
96d21f00 5432 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
5433
5434 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
5435 ipr_erp_done(ipr_cmd);
5436 return;
5437 }
5438
5439 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5440
5441 cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
5442 cmd_pkt->cdb[0] = REQUEST_SENSE;
5443 cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
5444 cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
5445 cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5446 cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
5447
a32c055f
WB
5448 ipr_init_ioadl(ipr_cmd, ipr_cmd->sense_buffer_dma,
5449 SCSI_SENSE_BUFFERSIZE, IPR_IOADL_FLAGS_READ_LAST);
1da177e4
LT
5450
5451 ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
5452 IPR_REQUEST_SENSE_TIMEOUT * 2);
5453}
5454
5455/**
5456 * ipr_erp_cancel_all - Send cancel all to a device
5457 * @ipr_cmd: ipr command struct
5458 *
5459 * This function sends a cancel all to a device to clear the
5460 * queue. If we are running TCQ on the device, QERR is set to 1,
5461 * which means all outstanding ops have been dropped on the floor.
5462 * Cancel all will return them to us.
5463 *
5464 * Return value:
5465 * nothing
5466 **/
5467static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
5468{
5469 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5470 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
5471 struct ipr_cmd_pkt *cmd_pkt;
5472
5473 res->in_erp = 1;
5474
5475 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
5476
5477 if (!scsi_get_tag_type(scsi_cmd->device)) {
5478 ipr_erp_request_sense(ipr_cmd);
5479 return;
5480 }
5481
5482 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
5483 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
5484 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
5485
5486 ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
5487 IPR_CANCEL_ALL_TIMEOUT);
5488}
5489
5490/**
5491 * ipr_dump_ioasa - Dump contents of IOASA
5492 * @ioa_cfg: ioa config struct
5493 * @ipr_cmd: ipr command struct
fe964d0a 5494 * @res: resource entry struct
1da177e4
LT
5495 *
5496 * This function is invoked by the interrupt handler when ops
5497 * fail. It will log the IOASA if appropriate. Only called
5498 * for GPDD ops.
5499 *
5500 * Return value:
5501 * none
5502 **/
5503static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
fe964d0a 5504 struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
1da177e4
LT
5505{
5506 int i;
5507 u16 data_len;
b0692dd4 5508 u32 ioasc, fd_ioasc;
96d21f00 5509 struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
1da177e4
LT
5510 __be32 *ioasa_data = (__be32 *)ioasa;
5511 int error_index;
5512
96d21f00
WB
5513 ioasc = be32_to_cpu(ioasa->hdr.ioasc) & IPR_IOASC_IOASC_MASK;
5514 fd_ioasc = be32_to_cpu(ioasa->hdr.fd_ioasc) & IPR_IOASC_IOASC_MASK;
1da177e4
LT
5515
5516 if (0 == ioasc)
5517 return;
5518
5519 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
5520 return;
5521
b0692dd4
BK
5522 if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
5523 error_index = ipr_get_error(fd_ioasc);
5524 else
5525 error_index = ipr_get_error(ioasc);
1da177e4
LT
5526
5527 if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
5528 /* Don't log an error if the IOA already logged one */
96d21f00 5529 if (ioasa->hdr.ilid != 0)
1da177e4
LT
5530 return;
5531
cc9bd5d4
BK
5532 if (!ipr_is_gscsi(res))
5533 return;
5534
1da177e4
LT
5535 if (ipr_error_table[error_index].log_ioasa == 0)
5536 return;
5537 }
5538
fe964d0a 5539 ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
1da177e4 5540
96d21f00
WB
5541 data_len = be16_to_cpu(ioasa->hdr.ret_stat_len);
5542 if (ioa_cfg->sis64 && sizeof(struct ipr_ioasa64) < data_len)
5543 data_len = sizeof(struct ipr_ioasa64);
5544 else if (!ioa_cfg->sis64 && sizeof(struct ipr_ioasa) < data_len)
1da177e4 5545 data_len = sizeof(struct ipr_ioasa);
1da177e4
LT
5546
5547 ipr_err("IOASA Dump:\n");
5548
5549 for (i = 0; i < data_len / 4; i += 4) {
5550 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
5551 be32_to_cpu(ioasa_data[i]),
5552 be32_to_cpu(ioasa_data[i+1]),
5553 be32_to_cpu(ioasa_data[i+2]),
5554 be32_to_cpu(ioasa_data[i+3]));
5555 }
5556}
5557
5558/**
5559 * ipr_gen_sense - Generate SCSI sense data from an IOASA
5560 * @ioasa: IOASA
5561 * @sense_buf: sense data buffer
5562 *
5563 * Return value:
5564 * none
5565 **/
5566static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
5567{
5568 u32 failing_lba;
5569 u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
5570 struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
96d21f00
WB
5571 struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5572 u32 ioasc = be32_to_cpu(ioasa->hdr.ioasc);
1da177e4
LT
5573
5574 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
5575
5576 if (ioasc >= IPR_FIRST_DRIVER_IOASC)
5577 return;
5578
5579 ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
5580
5581 if (ipr_is_vset_device(res) &&
5582 ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
5583 ioasa->u.vset.failing_lba_hi != 0) {
5584 sense_buf[0] = 0x72;
5585 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
5586 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
5587 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
5588
5589 sense_buf[7] = 12;
5590 sense_buf[8] = 0;
5591 sense_buf[9] = 0x0A;
5592 sense_buf[10] = 0x80;
5593
5594 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
5595
5596 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
5597 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
5598 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
5599 sense_buf[15] = failing_lba & 0x000000ff;
5600
5601 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5602
5603 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
5604 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
5605 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
5606 sense_buf[19] = failing_lba & 0x000000ff;
5607 } else {
5608 sense_buf[0] = 0x70;
5609 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
5610 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
5611 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
5612
5613 /* Illegal request */
5614 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
96d21f00 5615 (be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
1da177e4
LT
5616 sense_buf[7] = 10; /* additional length */
5617
5618 /* IOARCB was in error */
5619 if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
5620 sense_buf[15] = 0xC0;
5621 else /* Parameter data was invalid */
5622 sense_buf[15] = 0x80;
5623
5624 sense_buf[16] =
5625 ((IPR_FIELD_POINTER_MASK &
96d21f00 5626 be32_to_cpu(ioasa->hdr.ioasc_specific)) >> 8) & 0xff;
1da177e4
LT
5627 sense_buf[17] =
5628 (IPR_FIELD_POINTER_MASK &
96d21f00 5629 be32_to_cpu(ioasa->hdr.ioasc_specific)) & 0xff;
1da177e4
LT
5630 } else {
5631 if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
5632 if (ipr_is_vset_device(res))
5633 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
5634 else
5635 failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
5636
5637 sense_buf[0] |= 0x80; /* Or in the Valid bit */
5638 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
5639 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
5640 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
5641 sense_buf[6] = failing_lba & 0x000000ff;
5642 }
5643
5644 sense_buf[7] = 6; /* additional length */
5645 }
5646 }
5647}
5648
ee0a90fa 5649/**
5650 * ipr_get_autosense - Copy autosense data to sense buffer
5651 * @ipr_cmd: ipr command struct
5652 *
5653 * This function copies the autosense buffer to the buffer
5654 * in the scsi_cmd, if there is autosense available.
5655 *
5656 * Return value:
5657 * 1 if autosense was available / 0 if not
5658 **/
5659static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
5660{
96d21f00
WB
5661 struct ipr_ioasa *ioasa = &ipr_cmd->s.ioasa;
5662 struct ipr_ioasa64 *ioasa64 = &ipr_cmd->s.ioasa64;
ee0a90fa 5663
96d21f00 5664 if ((be32_to_cpu(ioasa->hdr.ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
ee0a90fa 5665 return 0;
5666
96d21f00
WB
5667 if (ipr_cmd->ioa_cfg->sis64)
5668 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa64->auto_sense.data,
5669 min_t(u16, be16_to_cpu(ioasa64->auto_sense.auto_sense_len),
5670 SCSI_SENSE_BUFFERSIZE));
5671 else
5672 memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
5673 min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
5674 SCSI_SENSE_BUFFERSIZE));
ee0a90fa 5675 return 1;
5676}
5677
1da177e4
LT
5678/**
5679 * ipr_erp_start - Process an error response for a SCSI op
5680 * @ioa_cfg: ioa config struct
5681 * @ipr_cmd: ipr command struct
5682 *
5683 * This function determines whether or not to initiate ERP
5684 * on the affected device.
5685 *
5686 * Return value:
5687 * nothing
5688 **/
5689static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
5690 struct ipr_cmnd *ipr_cmd)
5691{
5692 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
5693 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
96d21f00 5694 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
8a048994 5695 u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
1da177e4
LT
5696
5697 if (!res) {
5698 ipr_scsi_eh_done(ipr_cmd);
5699 return;
5700 }
5701
8a048994 5702 if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
1da177e4
LT
5703 ipr_gen_sense(ipr_cmd);
5704
cc9bd5d4
BK
5705 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
5706
8a048994 5707 switch (masked_ioasc) {
1da177e4 5708 case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
ee0a90fa 5709 if (ipr_is_naca_model(res))
5710 scsi_cmd->result |= (DID_ABORT << 16);
5711 else
5712 scsi_cmd->result |= (DID_IMM_RETRY << 16);
1da177e4
LT
5713 break;
5714 case IPR_IOASC_IR_RESOURCE_HANDLE:
b0df54bb 5715 case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
1da177e4
LT
5716 scsi_cmd->result |= (DID_NO_CONNECT << 16);
5717 break;
5718 case IPR_IOASC_HW_SEL_TIMEOUT:
5719 scsi_cmd->result |= (DID_NO_CONNECT << 16);
ee0a90fa 5720 if (!ipr_is_naca_model(res))
5721 res->needs_sync_complete = 1;
1da177e4
LT
5722 break;
5723 case IPR_IOASC_SYNC_REQUIRED:
5724 if (!res->in_erp)
5725 res->needs_sync_complete = 1;
5726 scsi_cmd->result |= (DID_IMM_RETRY << 16);
5727 break;
5728 case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
b0df54bb 5729 case IPR_IOASA_IR_DUAL_IOA_DISABLED:
1da177e4
LT
5730 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
5731 break;
5732 case IPR_IOASC_BUS_WAS_RESET:
5733 case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
5734 /*
5735 * Report the bus reset and ask for a retry. The device
5736 * will give CC/UA the next command.
5737 */
5738 if (!res->resetting_device)
5739 scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
5740 scsi_cmd->result |= (DID_ERROR << 16);
ee0a90fa 5741 if (!ipr_is_naca_model(res))
5742 res->needs_sync_complete = 1;
1da177e4
LT
5743 break;
5744 case IPR_IOASC_HW_DEV_BUS_STATUS:
5745 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
5746 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
ee0a90fa 5747 if (!ipr_get_autosense(ipr_cmd)) {
5748 if (!ipr_is_naca_model(res)) {
5749 ipr_erp_cancel_all(ipr_cmd);
5750 return;
5751 }
5752 }
1da177e4 5753 }
ee0a90fa 5754 if (!ipr_is_naca_model(res))
5755 res->needs_sync_complete = 1;
1da177e4
LT
5756 break;
5757 case IPR_IOASC_NR_INIT_CMD_REQUIRED:
5758 break;
5759 default:
5b7304fb
BK
5760 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
5761 scsi_cmd->result |= (DID_ERROR << 16);
ee0a90fa 5762 if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
1da177e4
LT
5763 res->needs_sync_complete = 1;
5764 break;
5765 }
5766
63015bc9 5767 scsi_dma_unmap(ipr_cmd->scsi_cmd);
1da177e4
LT
5768 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5769 scsi_cmd->scsi_done(scsi_cmd);
5770}
5771
5772/**
5773 * ipr_scsi_done - mid-layer done function
5774 * @ipr_cmd: ipr command struct
5775 *
5776 * This function is invoked by the interrupt handler for
5777 * ops generated by the SCSI mid-layer
5778 *
5779 * Return value:
5780 * none
5781 **/
5782static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
5783{
5784 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5785 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
96d21f00 5786 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4 5787
96d21f00 5788 scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->s.ioasa.hdr.residual_data_len));
1da177e4
LT
5789
5790 if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
63015bc9 5791 scsi_dma_unmap(ipr_cmd->scsi_cmd);
1da177e4
LT
5792 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5793 scsi_cmd->scsi_done(scsi_cmd);
5794 } else
5795 ipr_erp_start(ioa_cfg, ipr_cmd);
5796}
5797
1da177e4
LT
5798/**
5799 * ipr_queuecommand - Queue a mid-layer request
00bfef2c 5800 * @shost: scsi host struct
1da177e4 5801 * @scsi_cmd: scsi command struct
1da177e4
LT
5802 *
5803 * This function queues a request generated by the mid-layer.
5804 *
5805 * Return value:
5806 * 0 on success
5807 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
5808 * SCSI_MLQUEUE_HOST_BUSY if host is busy
5809 **/
00bfef2c
BK
5810static int ipr_queuecommand(struct Scsi_Host *shost,
5811 struct scsi_cmnd *scsi_cmd)
1da177e4
LT
5812{
5813 struct ipr_ioa_cfg *ioa_cfg;
5814 struct ipr_resource_entry *res;
5815 struct ipr_ioarcb *ioarcb;
5816 struct ipr_cmnd *ipr_cmd;
00bfef2c 5817 unsigned long lock_flags;
1da177e4
LT
5818 int rc = 0;
5819
00bfef2c
BK
5820 ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
5821
5822 spin_lock_irqsave(shost->host_lock, lock_flags);
1da177e4 5823 scsi_cmd->result = (DID_OK << 16);
00bfef2c 5824 res = scsi_cmd->device->hostdata;
1da177e4
LT
5825
5826 /*
5827 * We are currently blocking all devices due to a host reset
5828 * We have told the host to stop giving us new requests, but
5829 * ERP ops don't count. FIXME
5830 */
00bfef2c
BK
5831 if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead)) {
5832 spin_unlock_irqrestore(shost->host_lock, lock_flags);
1da177e4 5833 return SCSI_MLQUEUE_HOST_BUSY;
00bfef2c 5834 }
1da177e4
LT
5835
5836 /*
5837 * FIXME - Create scsi_set_host_offline interface
5838 * and the ioa_is_dead check can be removed
5839 */
5840 if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
00bfef2c
BK
5841 spin_unlock_irqrestore(shost->host_lock, lock_flags);
5842 goto err_nodev;
1da177e4
LT
5843 }
5844
35a39691 5845 if (ipr_is_gata(res) && res->sata_port)
b27dcfb0 5846 return ata_sas_queuecmd(scsi_cmd, res->sata_port->ap);
35a39691 5847
00bfef2c
BK
5848 ipr_cmd = __ipr_get_free_ipr_cmnd(ioa_cfg);
5849 spin_unlock_irqrestore(shost->host_lock, lock_flags);
5850
5851 ipr_init_ipr_cmnd(ipr_cmd);
1da177e4 5852 ioarcb = &ipr_cmd->ioarcb;
1da177e4
LT
5853
5854 memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
5855 ipr_cmd->scsi_cmd = scsi_cmd;
1da177e4 5856 ipr_cmd->done = ipr_scsi_done;
1da177e4
LT
5857
5858 if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
5859 if (scsi_cmd->underflow == 0)
5860 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
5861
1da177e4 5862 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
ab6c10b1
WB
5863 if (ipr_is_gscsi(res))
5864 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
1da177e4
LT
5865 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
5866 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
5867 }
5868
5869 if (scsi_cmd->cmnd[0] >= 0xC0 &&
5870 (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
5871 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5872
a32c055f
WB
5873 if (likely(rc == 0)) {
5874 if (ioa_cfg->sis64)
5875 rc = ipr_build_ioadl64(ioa_cfg, ipr_cmd);
5876 else
5877 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
5878 }
1da177e4 5879
00bfef2c
BK
5880 spin_lock_irqsave(shost->host_lock, lock_flags);
5881 if (unlikely(rc || (!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))) {
5882 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5883 spin_unlock_irqrestore(shost->host_lock, lock_flags);
5884 if (!rc)
5885 scsi_dma_unmap(scsi_cmd);
a5fb407e 5886 return SCSI_MLQUEUE_HOST_BUSY;
1da177e4
LT
5887 }
5888
00bfef2c
BK
5889 if (unlikely(ioa_cfg->ioa_is_dead)) {
5890 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5891 spin_unlock_irqrestore(shost->host_lock, lock_flags);
5892 scsi_dma_unmap(scsi_cmd);
5893 goto err_nodev;
5894 }
5895
5896 ioarcb->res_handle = res->res_handle;
5897 if (res->needs_sync_complete) {
5898 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
5899 res->needs_sync_complete = 0;
5900 }
5901 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5902 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
a5fb407e 5903 ipr_send_command(ipr_cmd);
00bfef2c 5904 spin_unlock_irqrestore(shost->host_lock, lock_flags);
1da177e4 5905 return 0;
1da177e4 5906
00bfef2c
BK
5907err_nodev:
5908 spin_lock_irqsave(shost->host_lock, lock_flags);
5909 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
5910 scsi_cmd->result = (DID_NO_CONNECT << 16);
5911 scsi_cmd->scsi_done(scsi_cmd);
5912 spin_unlock_irqrestore(shost->host_lock, lock_flags);
5913 return 0;
5914}
f281233d 5915
35a39691
BK
5916/**
5917 * ipr_ioctl - IOCTL handler
5918 * @sdev: scsi device struct
5919 * @cmd: IOCTL cmd
5920 * @arg: IOCTL arg
5921 *
5922 * Return value:
5923 * 0 on success / other on failure
5924 **/
bd705f2d 5925static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
35a39691
BK
5926{
5927 struct ipr_resource_entry *res;
5928
5929 res = (struct ipr_resource_entry *)sdev->hostdata;
0ce3a7e5
BK
5930 if (res && ipr_is_gata(res)) {
5931 if (cmd == HDIO_GET_IDENTITY)
5932 return -ENOTTY;
94be9a58 5933 return ata_sas_scsi_ioctl(res->sata_port->ap, sdev, cmd, arg);
0ce3a7e5 5934 }
35a39691
BK
5935
5936 return -EINVAL;
5937}
5938
1da177e4
LT
5939/**
5940 * ipr_info - Get information about the card/driver
5941 * @scsi_host: scsi host struct
5942 *
5943 * Return value:
5944 * pointer to buffer with description string
5945 **/
5946static const char * ipr_ioa_info(struct Scsi_Host *host)
5947{
5948 static char buffer[512];
5949 struct ipr_ioa_cfg *ioa_cfg;
5950 unsigned long lock_flags = 0;
5951
5952 ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
5953
5954 spin_lock_irqsave(host->host_lock, lock_flags);
5955 sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
5956 spin_unlock_irqrestore(host->host_lock, lock_flags);
5957
5958 return buffer;
5959}
5960
5961static struct scsi_host_template driver_template = {
5962 .module = THIS_MODULE,
5963 .name = "IPR",
5964 .info = ipr_ioa_info,
35a39691 5965 .ioctl = ipr_ioctl,
1da177e4
LT
5966 .queuecommand = ipr_queuecommand,
5967 .eh_abort_handler = ipr_eh_abort,
5968 .eh_device_reset_handler = ipr_eh_dev_reset,
5969 .eh_host_reset_handler = ipr_eh_host_reset,
5970 .slave_alloc = ipr_slave_alloc,
5971 .slave_configure = ipr_slave_configure,
5972 .slave_destroy = ipr_slave_destroy,
35a39691
BK
5973 .target_alloc = ipr_target_alloc,
5974 .target_destroy = ipr_target_destroy,
1da177e4
LT
5975 .change_queue_depth = ipr_change_queue_depth,
5976 .change_queue_type = ipr_change_queue_type,
5977 .bios_param = ipr_biosparam,
5978 .can_queue = IPR_MAX_COMMANDS,
5979 .this_id = -1,
5980 .sg_tablesize = IPR_MAX_SGLIST,
5981 .max_sectors = IPR_IOA_MAX_SECTORS,
5982 .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
5983 .use_clustering = ENABLE_CLUSTERING,
5984 .shost_attrs = ipr_ioa_attrs,
5985 .sdev_attrs = ipr_dev_attrs,
5986 .proc_name = IPR_NAME
5987};
5988
35a39691
BK
5989/**
5990 * ipr_ata_phy_reset - libata phy_reset handler
5991 * @ap: ata port to reset
5992 *
5993 **/
5994static void ipr_ata_phy_reset(struct ata_port *ap)
5995{
5996 unsigned long flags;
5997 struct ipr_sata_port *sata_port = ap->private_data;
5998 struct ipr_resource_entry *res = sata_port->res;
5999 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6000 int rc;
6001
6002 ENTER;
6003 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6004 while(ioa_cfg->in_reset_reload) {
6005 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6006 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
6007 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6008 }
6009
6010 if (!ioa_cfg->allow_cmds)
6011 goto out_unlock;
6012
6013 rc = ipr_device_reset(ioa_cfg, res);
6014
6015 if (rc) {
3e4ec344 6016 ap->link.device[0].class = ATA_DEV_NONE;
35a39691
BK
6017 goto out_unlock;
6018 }
6019
3e7ebdfa
WB
6020 ap->link.device[0].class = res->ata_class;
6021 if (ap->link.device[0].class == ATA_DEV_UNKNOWN)
3e4ec344 6022 ap->link.device[0].class = ATA_DEV_NONE;
35a39691
BK
6023
6024out_unlock:
6025 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6026 LEAVE;
6027}
6028
6029/**
6030 * ipr_ata_post_internal - Cleanup after an internal command
6031 * @qc: ATA queued command
6032 *
6033 * Return value:
6034 * none
6035 **/
6036static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
6037{
6038 struct ipr_sata_port *sata_port = qc->ap->private_data;
6039 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6040 struct ipr_cmnd *ipr_cmd;
6041 unsigned long flags;
6042
6043 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
73d98ff0
BK
6044 while(ioa_cfg->in_reset_reload) {
6045 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6046 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
6047 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
6048 }
6049
35a39691
BK
6050 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
6051 if (ipr_cmd->qc == qc) {
6052 ipr_device_reset(ioa_cfg, sata_port->res);
6053 break;
6054 }
6055 }
6056 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
6057}
6058
35a39691
BK
6059/**
6060 * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
6061 * @regs: destination
6062 * @tf: source ATA taskfile
6063 *
6064 * Return value:
6065 * none
6066 **/
6067static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
6068 struct ata_taskfile *tf)
6069{
6070 regs->feature = tf->feature;
6071 regs->nsect = tf->nsect;
6072 regs->lbal = tf->lbal;
6073 regs->lbam = tf->lbam;
6074 regs->lbah = tf->lbah;
6075 regs->device = tf->device;
6076 regs->command = tf->command;
6077 regs->hob_feature = tf->hob_feature;
6078 regs->hob_nsect = tf->hob_nsect;
6079 regs->hob_lbal = tf->hob_lbal;
6080 regs->hob_lbam = tf->hob_lbam;
6081 regs->hob_lbah = tf->hob_lbah;
6082 regs->ctl = tf->ctl;
6083}
6084
6085/**
6086 * ipr_sata_done - done function for SATA commands
6087 * @ipr_cmd: ipr command struct
6088 *
6089 * This function is invoked by the interrupt handler for
6090 * ops generated by the SCSI mid-layer to SATA devices
6091 *
6092 * Return value:
6093 * none
6094 **/
6095static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
6096{
6097 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6098 struct ata_queued_cmd *qc = ipr_cmd->qc;
6099 struct ipr_sata_port *sata_port = qc->ap->private_data;
6100 struct ipr_resource_entry *res = sata_port->res;
96d21f00 6101 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
35a39691 6102
96d21f00
WB
6103 if (ipr_cmd->ioa_cfg->sis64)
6104 memcpy(&sata_port->ioasa, &ipr_cmd->s.ioasa64.u.gata,
6105 sizeof(struct ipr_ioasa_gata));
6106 else
6107 memcpy(&sata_port->ioasa, &ipr_cmd->s.ioasa.u.gata,
6108 sizeof(struct ipr_ioasa_gata));
35a39691
BK
6109 ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
6110
96d21f00 6111 if (be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
3e7ebdfa 6112 scsi_report_device_reset(ioa_cfg->host, res->bus, res->target);
35a39691
BK
6113
6114 if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
96d21f00 6115 qc->err_mask |= __ac_err_mask(sata_port->ioasa.status);
35a39691 6116 else
96d21f00 6117 qc->err_mask |= ac_err_mask(sata_port->ioasa.status);
35a39691
BK
6118 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6119 ata_qc_complete(qc);
6120}
6121
a32c055f
WB
6122/**
6123 * ipr_build_ata_ioadl64 - Build an ATA scatter/gather list
6124 * @ipr_cmd: ipr command struct
6125 * @qc: ATA queued command
6126 *
6127 **/
6128static void ipr_build_ata_ioadl64(struct ipr_cmnd *ipr_cmd,
6129 struct ata_queued_cmd *qc)
6130{
6131 u32 ioadl_flags = 0;
6132 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6133 struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
6134 struct ipr_ioadl64_desc *last_ioadl64 = NULL;
6135 int len = qc->nbytes;
6136 struct scatterlist *sg;
6137 unsigned int si;
6138 dma_addr_t dma_addr = ipr_cmd->dma_addr;
6139
6140 if (len == 0)
6141 return;
6142
6143 if (qc->dma_dir == DMA_TO_DEVICE) {
6144 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
6145 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6146 } else if (qc->dma_dir == DMA_FROM_DEVICE)
6147 ioadl_flags = IPR_IOADL_FLAGS_READ;
6148
6149 ioarcb->data_transfer_length = cpu_to_be32(len);
6150 ioarcb->ioadl_len =
6151 cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
6152 ioarcb->u.sis64_addr_data.data_ioadl_addr =
6153 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ata_ioadl));
6154
6155 for_each_sg(qc->sg, sg, qc->n_elem, si) {
6156 ioadl64->flags = cpu_to_be32(ioadl_flags);
6157 ioadl64->data_len = cpu_to_be32(sg_dma_len(sg));
6158 ioadl64->address = cpu_to_be64(sg_dma_address(sg));
6159
6160 last_ioadl64 = ioadl64;
6161 ioadl64++;
6162 }
6163
6164 if (likely(last_ioadl64))
6165 last_ioadl64->flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
6166}
6167
35a39691
BK
6168/**
6169 * ipr_build_ata_ioadl - Build an ATA scatter/gather list
6170 * @ipr_cmd: ipr command struct
6171 * @qc: ATA queued command
6172 *
6173 **/
6174static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
6175 struct ata_queued_cmd *qc)
6176{
6177 u32 ioadl_flags = 0;
6178 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
a32c055f 6179 struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
3be6cbd7 6180 struct ipr_ioadl_desc *last_ioadl = NULL;
dde20207 6181 int len = qc->nbytes;
35a39691 6182 struct scatterlist *sg;
ff2aeb1e 6183 unsigned int si;
35a39691
BK
6184
6185 if (len == 0)
6186 return;
6187
6188 if (qc->dma_dir == DMA_TO_DEVICE) {
6189 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
6190 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
a32c055f
WB
6191 ioarcb->data_transfer_length = cpu_to_be32(len);
6192 ioarcb->ioadl_len =
35a39691
BK
6193 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
6194 } else if (qc->dma_dir == DMA_FROM_DEVICE) {
6195 ioadl_flags = IPR_IOADL_FLAGS_READ;
6196 ioarcb->read_data_transfer_length = cpu_to_be32(len);
6197 ioarcb->read_ioadl_len =
6198 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
6199 }
6200
ff2aeb1e 6201 for_each_sg(qc->sg, sg, qc->n_elem, si) {
35a39691
BK
6202 ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
6203 ioadl->address = cpu_to_be32(sg_dma_address(sg));
3be6cbd7
JG
6204
6205 last_ioadl = ioadl;
6206 ioadl++;
35a39691 6207 }
3be6cbd7
JG
6208
6209 if (likely(last_ioadl))
6210 last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
35a39691
BK
6211}
6212
6213/**
6214 * ipr_qc_issue - Issue a SATA qc to a device
6215 * @qc: queued command
6216 *
6217 * Return value:
6218 * 0 if success
6219 **/
6220static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
6221{
6222 struct ata_port *ap = qc->ap;
6223 struct ipr_sata_port *sata_port = ap->private_data;
6224 struct ipr_resource_entry *res = sata_port->res;
6225 struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
6226 struct ipr_cmnd *ipr_cmd;
6227 struct ipr_ioarcb *ioarcb;
6228 struct ipr_ioarcb_ata_regs *regs;
6229
6230 if (unlikely(!ioa_cfg->allow_cmds || ioa_cfg->ioa_is_dead))
0feeed82 6231 return AC_ERR_SYSTEM;
35a39691
BK
6232
6233 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
6234 ioarcb = &ipr_cmd->ioarcb;
35a39691 6235
a32c055f
WB
6236 if (ioa_cfg->sis64) {
6237 regs = &ipr_cmd->i.ata_ioadl.regs;
6238 ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
6239 } else
6240 regs = &ioarcb->u.add_data.u.regs;
6241
6242 memset(regs, 0, sizeof(*regs));
6243 ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(*regs));
35a39691
BK
6244
6245 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
6246 ipr_cmd->qc = qc;
6247 ipr_cmd->done = ipr_sata_done;
3e7ebdfa 6248 ipr_cmd->ioarcb.res_handle = res->res_handle;
35a39691
BK
6249 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
6250 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
6251 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
dde20207 6252 ipr_cmd->dma_use_sg = qc->n_elem;
35a39691 6253
a32c055f
WB
6254 if (ioa_cfg->sis64)
6255 ipr_build_ata_ioadl64(ipr_cmd, qc);
6256 else
6257 ipr_build_ata_ioadl(ipr_cmd, qc);
6258
35a39691
BK
6259 regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
6260 ipr_copy_sata_tf(regs, &qc->tf);
6261 memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
3e7ebdfa 6262 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
35a39691
BK
6263
6264 switch (qc->tf.protocol) {
6265 case ATA_PROT_NODATA:
6266 case ATA_PROT_PIO:
6267 break;
6268
6269 case ATA_PROT_DMA:
6270 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
6271 break;
6272
0dc36888
TH
6273 case ATAPI_PROT_PIO:
6274 case ATAPI_PROT_NODATA:
35a39691
BK
6275 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
6276 break;
6277
0dc36888 6278 case ATAPI_PROT_DMA:
35a39691
BK
6279 regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
6280 regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
6281 break;
6282
6283 default:
6284 WARN_ON(1);
0feeed82 6285 return AC_ERR_INVALID;
35a39691
BK
6286 }
6287
a32c055f
WB
6288 ipr_send_command(ipr_cmd);
6289
35a39691
BK
6290 return 0;
6291}
6292
4c9bf4e7
TH
6293/**
6294 * ipr_qc_fill_rtf - Read result TF
6295 * @qc: ATA queued command
6296 *
6297 * Return value:
6298 * true
6299 **/
6300static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
6301{
6302 struct ipr_sata_port *sata_port = qc->ap->private_data;
6303 struct ipr_ioasa_gata *g = &sata_port->ioasa;
6304 struct ata_taskfile *tf = &qc->result_tf;
6305
6306 tf->feature = g->error;
6307 tf->nsect = g->nsect;
6308 tf->lbal = g->lbal;
6309 tf->lbam = g->lbam;
6310 tf->lbah = g->lbah;
6311 tf->device = g->device;
6312 tf->command = g->status;
6313 tf->hob_nsect = g->hob_nsect;
6314 tf->hob_lbal = g->hob_lbal;
6315 tf->hob_lbam = g->hob_lbam;
6316 tf->hob_lbah = g->hob_lbah;
6317 tf->ctl = g->alt_status;
6318
6319 return true;
6320}
6321
35a39691 6322static struct ata_port_operations ipr_sata_ops = {
35a39691 6323 .phy_reset = ipr_ata_phy_reset,
a1efdaba 6324 .hardreset = ipr_sata_reset,
35a39691 6325 .post_internal_cmd = ipr_ata_post_internal,
35a39691
BK
6326 .qc_prep = ata_noop_qc_prep,
6327 .qc_issue = ipr_qc_issue,
4c9bf4e7 6328 .qc_fill_rtf = ipr_qc_fill_rtf,
35a39691
BK
6329 .port_start = ata_sas_port_start,
6330 .port_stop = ata_sas_port_stop
6331};
6332
6333static struct ata_port_info sata_port_info = {
9cbe056f 6334 .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
0f2e0330
SS
6335 .pio_mask = ATA_PIO4_ONLY,
6336 .mwdma_mask = ATA_MWDMA2,
6337 .udma_mask = ATA_UDMA6,
35a39691
BK
6338 .port_ops = &ipr_sata_ops
6339};
6340
1da177e4
LT
6341#ifdef CONFIG_PPC_PSERIES
6342static const u16 ipr_blocked_processors[] = {
6343 PV_NORTHSTAR,
6344 PV_PULSAR,
6345 PV_POWER4,
6346 PV_ICESTAR,
6347 PV_SSTAR,
6348 PV_POWER4p,
6349 PV_630,
6350 PV_630p
6351};
6352
6353/**
6354 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
6355 * @ioa_cfg: ioa cfg struct
6356 *
6357 * Adapters that use Gemstone revision < 3.1 do not work reliably on
6358 * certain pSeries hardware. This function determines if the given
6359 * adapter is in one of these confgurations or not.
6360 *
6361 * Return value:
6362 * 1 if adapter is not supported / 0 if adapter is supported
6363 **/
6364static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
6365{
1da177e4
LT
6366 int i;
6367
44c10138
AK
6368 if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
6369 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
6370 if (__is_processor(ipr_blocked_processors[i]))
6371 return 1;
1da177e4
LT
6372 }
6373 }
6374 return 0;
6375}
6376#else
6377#define ipr_invalid_adapter(ioa_cfg) 0
6378#endif
6379
6380/**
6381 * ipr_ioa_bringdown_done - IOA bring down completion.
6382 * @ipr_cmd: ipr command struct
6383 *
6384 * This function processes the completion of an adapter bring down.
6385 * It wakes any reset sleepers.
6386 *
6387 * Return value:
6388 * IPR_RC_JOB_RETURN
6389 **/
6390static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
6391{
6392 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6393
6394 ENTER;
6395 ioa_cfg->in_reset_reload = 0;
6396 ioa_cfg->reset_retries = 0;
6397 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6398 wake_up_all(&ioa_cfg->reset_wait_q);
6399
6400 spin_unlock_irq(ioa_cfg->host->host_lock);
6401 scsi_unblock_requests(ioa_cfg->host);
6402 spin_lock_irq(ioa_cfg->host->host_lock);
6403 LEAVE;
6404
6405 return IPR_RC_JOB_RETURN;
6406}
6407
6408/**
6409 * ipr_ioa_reset_done - IOA reset completion.
6410 * @ipr_cmd: ipr command struct
6411 *
6412 * This function processes the completion of an adapter reset.
6413 * It schedules any necessary mid-layer add/removes and
6414 * wakes any reset sleepers.
6415 *
6416 * Return value:
6417 * IPR_RC_JOB_RETURN
6418 **/
6419static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
6420{
6421 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6422 struct ipr_resource_entry *res;
6423 struct ipr_hostrcb *hostrcb, *temp;
6424 int i = 0;
6425
6426 ENTER;
6427 ioa_cfg->in_reset_reload = 0;
6428 ioa_cfg->allow_cmds = 1;
6429 ioa_cfg->reset_cmd = NULL;
3d1d0da6 6430 ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
1da177e4
LT
6431
6432 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
6433 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
6434 ipr_trace;
6435 break;
6436 }
6437 }
6438 schedule_work(&ioa_cfg->work_q);
6439
6440 list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
6441 list_del(&hostrcb->queue);
6442 if (i++ < IPR_NUM_LOG_HCAMS)
6443 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
6444 else
6445 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
6446 }
6447
6bb04170 6448 scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
1da177e4
LT
6449 dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
6450
6451 ioa_cfg->reset_retries = 0;
6452 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6453 wake_up_all(&ioa_cfg->reset_wait_q);
6454
30237853 6455 spin_unlock(ioa_cfg->host->host_lock);
1da177e4 6456 scsi_unblock_requests(ioa_cfg->host);
30237853 6457 spin_lock(ioa_cfg->host->host_lock);
1da177e4
LT
6458
6459 if (!ioa_cfg->allow_cmds)
6460 scsi_block_requests(ioa_cfg->host);
6461
6462 LEAVE;
6463 return IPR_RC_JOB_RETURN;
6464}
6465
6466/**
6467 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
6468 * @supported_dev: supported device struct
6469 * @vpids: vendor product id struct
6470 *
6471 * Return value:
6472 * none
6473 **/
6474static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
6475 struct ipr_std_inq_vpids *vpids)
6476{
6477 memset(supported_dev, 0, sizeof(struct ipr_supported_device));
6478 memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
6479 supported_dev->num_records = 1;
6480 supported_dev->data_length =
6481 cpu_to_be16(sizeof(struct ipr_supported_device));
6482 supported_dev->reserved = 0;
6483}
6484
6485/**
6486 * ipr_set_supported_devs - Send Set Supported Devices for a device
6487 * @ipr_cmd: ipr command struct
6488 *
a32c055f 6489 * This function sends a Set Supported Devices to the adapter
1da177e4
LT
6490 *
6491 * Return value:
6492 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6493 **/
6494static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
6495{
6496 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6497 struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
1da177e4
LT
6498 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6499 struct ipr_resource_entry *res = ipr_cmd->u.res;
6500
6501 ipr_cmd->job_step = ipr_ioa_reset_done;
6502
6503 list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
e4fbf44e 6504 if (!ipr_is_scsi_disk(res))
1da177e4
LT
6505 continue;
6506
6507 ipr_cmd->u.res = res;
3e7ebdfa 6508 ipr_set_sup_dev_dflt(supp_dev, &res->std_inq_data.vpids);
1da177e4
LT
6509
6510 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
6511 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6512 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
6513
6514 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
3e7ebdfa 6515 ioarcb->cmd_pkt.cdb[1] = IPR_SET_ALL_SUPPORTED_DEVICES;
1da177e4
LT
6516 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
6517 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
6518
a32c055f
WB
6519 ipr_init_ioadl(ipr_cmd,
6520 ioa_cfg->vpd_cbs_dma +
6521 offsetof(struct ipr_misc_cbs, supp_dev),
6522 sizeof(struct ipr_supported_device),
6523 IPR_IOADL_FLAGS_WRITE_LAST);
1da177e4
LT
6524
6525 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
6526 IPR_SET_SUP_DEVICE_TIMEOUT);
6527
3e7ebdfa
WB
6528 if (!ioa_cfg->sis64)
6529 ipr_cmd->job_step = ipr_set_supported_devs;
1da177e4
LT
6530 return IPR_RC_JOB_RETURN;
6531 }
6532
6533 return IPR_RC_JOB_CONTINUE;
6534}
6535
6536/**
6537 * ipr_get_mode_page - Locate specified mode page
6538 * @mode_pages: mode page buffer
6539 * @page_code: page code to find
6540 * @len: minimum required length for mode page
6541 *
6542 * Return value:
6543 * pointer to mode page / NULL on failure
6544 **/
6545static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
6546 u32 page_code, u32 len)
6547{
6548 struct ipr_mode_page_hdr *mode_hdr;
6549 u32 page_length;
6550 u32 length;
6551
6552 if (!mode_pages || (mode_pages->hdr.length == 0))
6553 return NULL;
6554
6555 length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
6556 mode_hdr = (struct ipr_mode_page_hdr *)
6557 (mode_pages->data + mode_pages->hdr.block_desc_len);
6558
6559 while (length) {
6560 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
6561 if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
6562 return mode_hdr;
6563 break;
6564 } else {
6565 page_length = (sizeof(struct ipr_mode_page_hdr) +
6566 mode_hdr->page_length);
6567 length -= page_length;
6568 mode_hdr = (struct ipr_mode_page_hdr *)
6569 ((unsigned long)mode_hdr + page_length);
6570 }
6571 }
6572 return NULL;
6573}
6574
6575/**
6576 * ipr_check_term_power - Check for term power errors
6577 * @ioa_cfg: ioa config struct
6578 * @mode_pages: IOAFP mode pages buffer
6579 *
6580 * Check the IOAFP's mode page 28 for term power errors
6581 *
6582 * Return value:
6583 * nothing
6584 **/
6585static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
6586 struct ipr_mode_pages *mode_pages)
6587{
6588 int i;
6589 int entry_length;
6590 struct ipr_dev_bus_entry *bus;
6591 struct ipr_mode_page28 *mode_page;
6592
6593 mode_page = ipr_get_mode_page(mode_pages, 0x28,
6594 sizeof(struct ipr_mode_page28));
6595
6596 entry_length = mode_page->entry_length;
6597
6598 bus = mode_page->bus;
6599
6600 for (i = 0; i < mode_page->num_entries; i++) {
6601 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
6602 dev_err(&ioa_cfg->pdev->dev,
6603 "Term power is absent on scsi bus %d\n",
6604 bus->res_addr.bus);
6605 }
6606
6607 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
6608 }
6609}
6610
6611/**
6612 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
6613 * @ioa_cfg: ioa config struct
6614 *
6615 * Looks through the config table checking for SES devices. If
6616 * the SES device is in the SES table indicating a maximum SCSI
6617 * bus speed, the speed is limited for the bus.
6618 *
6619 * Return value:
6620 * none
6621 **/
6622static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
6623{
6624 u32 max_xfer_rate;
6625 int i;
6626
6627 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
6628 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
6629 ioa_cfg->bus_attr[i].bus_width);
6630
6631 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
6632 ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
6633 }
6634}
6635
6636/**
6637 * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
6638 * @ioa_cfg: ioa config struct
6639 * @mode_pages: mode page 28 buffer
6640 *
6641 * Updates mode page 28 based on driver configuration
6642 *
6643 * Return value:
6644 * none
6645 **/
6646static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
6647 struct ipr_mode_pages *mode_pages)
6648{
6649 int i, entry_length;
6650 struct ipr_dev_bus_entry *bus;
6651 struct ipr_bus_attributes *bus_attr;
6652 struct ipr_mode_page28 *mode_page;
6653
6654 mode_page = ipr_get_mode_page(mode_pages, 0x28,
6655 sizeof(struct ipr_mode_page28));
6656
6657 entry_length = mode_page->entry_length;
6658
6659 /* Loop for each device bus entry */
6660 for (i = 0, bus = mode_page->bus;
6661 i < mode_page->num_entries;
6662 i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
6663 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
6664 dev_err(&ioa_cfg->pdev->dev,
6665 "Invalid resource address reported: 0x%08X\n",
6666 IPR_GET_PHYS_LOC(bus->res_addr));
6667 continue;
6668 }
6669
6670 bus_attr = &ioa_cfg->bus_attr[i];
6671 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
6672 bus->bus_width = bus_attr->bus_width;
6673 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
6674 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
6675 if (bus_attr->qas_enabled)
6676 bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
6677 else
6678 bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
6679 }
6680}
6681
6682/**
6683 * ipr_build_mode_select - Build a mode select command
6684 * @ipr_cmd: ipr command struct
6685 * @res_handle: resource handle to send command to
6686 * @parm: Byte 2 of Mode Sense command
6687 * @dma_addr: DMA buffer address
6688 * @xfer_len: data transfer length
6689 *
6690 * Return value:
6691 * none
6692 **/
6693static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
a32c055f
WB
6694 __be32 res_handle, u8 parm,
6695 dma_addr_t dma_addr, u8 xfer_len)
1da177e4 6696{
1da177e4
LT
6697 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6698
6699 ioarcb->res_handle = res_handle;
6700 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6701 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
6702 ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
6703 ioarcb->cmd_pkt.cdb[1] = parm;
6704 ioarcb->cmd_pkt.cdb[4] = xfer_len;
6705
a32c055f 6706 ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_WRITE_LAST);
1da177e4
LT
6707}
6708
6709/**
6710 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
6711 * @ipr_cmd: ipr command struct
6712 *
6713 * This function sets up the SCSI bus attributes and sends
6714 * a Mode Select for Page 28 to activate them.
6715 *
6716 * Return value:
6717 * IPR_RC_JOB_RETURN
6718 **/
6719static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
6720{
6721 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6722 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
6723 int length;
6724
6725 ENTER;
4733804c
BK
6726 ipr_scsi_bus_speed_limit(ioa_cfg);
6727 ipr_check_term_power(ioa_cfg, mode_pages);
6728 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
6729 length = mode_pages->hdr.length + 1;
6730 mode_pages->hdr.length = 0;
1da177e4
LT
6731
6732 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
6733 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
6734 length);
6735
f72919ec
WB
6736 ipr_cmd->job_step = ipr_set_supported_devs;
6737 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
6738 struct ipr_resource_entry, queue);
1da177e4
LT
6739 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6740
6741 LEAVE;
6742 return IPR_RC_JOB_RETURN;
6743}
6744
6745/**
6746 * ipr_build_mode_sense - Builds a mode sense command
6747 * @ipr_cmd: ipr command struct
6748 * @res: resource entry struct
6749 * @parm: Byte 2 of mode sense command
6750 * @dma_addr: DMA address of mode sense buffer
6751 * @xfer_len: Size of DMA buffer
6752 *
6753 * Return value:
6754 * none
6755 **/
6756static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
6757 __be32 res_handle,
a32c055f 6758 u8 parm, dma_addr_t dma_addr, u8 xfer_len)
1da177e4 6759{
1da177e4
LT
6760 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
6761
6762 ioarcb->res_handle = res_handle;
6763 ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
6764 ioarcb->cmd_pkt.cdb[2] = parm;
6765 ioarcb->cmd_pkt.cdb[4] = xfer_len;
6766 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
6767
a32c055f 6768 ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
1da177e4
LT
6769}
6770
dfed823e 6771/**
6772 * ipr_reset_cmd_failed - Handle failure of IOA reset command
6773 * @ipr_cmd: ipr command struct
6774 *
6775 * This function handles the failure of an IOA bringup command.
6776 *
6777 * Return value:
6778 * IPR_RC_JOB_RETURN
6779 **/
6780static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
6781{
6782 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
96d21f00 6783 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
dfed823e 6784
6785 dev_err(&ioa_cfg->pdev->dev,
6786 "0x%02X failed with IOASC: 0x%08X\n",
6787 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
6788
6789 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
6790 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
6791 return IPR_RC_JOB_RETURN;
6792}
6793
6794/**
6795 * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
6796 * @ipr_cmd: ipr command struct
6797 *
6798 * This function handles the failure of a Mode Sense to the IOAFP.
6799 * Some adapters do not handle all mode pages.
6800 *
6801 * Return value:
6802 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6803 **/
6804static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
6805{
f72919ec 6806 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
96d21f00 6807 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
dfed823e 6808
6809 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
f72919ec
WB
6810 ipr_cmd->job_step = ipr_set_supported_devs;
6811 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
6812 struct ipr_resource_entry, queue);
dfed823e 6813 return IPR_RC_JOB_CONTINUE;
6814 }
6815
6816 return ipr_reset_cmd_failed(ipr_cmd);
6817}
6818
1da177e4
LT
6819/**
6820 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
6821 * @ipr_cmd: ipr command struct
6822 *
6823 * This function send a Page 28 mode sense to the IOA to
6824 * retrieve SCSI bus attributes.
6825 *
6826 * Return value:
6827 * IPR_RC_JOB_RETURN
6828 **/
6829static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
6830{
6831 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6832
6833 ENTER;
6834 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
6835 0x28, ioa_cfg->vpd_cbs_dma +
6836 offsetof(struct ipr_misc_cbs, mode_pages),
6837 sizeof(struct ipr_mode_pages));
6838
6839 ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
dfed823e 6840 ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
1da177e4
LT
6841
6842 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6843
6844 LEAVE;
6845 return IPR_RC_JOB_RETURN;
6846}
6847
ac09c349
BK
6848/**
6849 * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
6850 * @ipr_cmd: ipr command struct
6851 *
6852 * This function enables dual IOA RAID support if possible.
6853 *
6854 * Return value:
6855 * IPR_RC_JOB_RETURN
6856 **/
6857static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
6858{
6859 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6860 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
6861 struct ipr_mode_page24 *mode_page;
6862 int length;
6863
6864 ENTER;
6865 mode_page = ipr_get_mode_page(mode_pages, 0x24,
6866 sizeof(struct ipr_mode_page24));
6867
6868 if (mode_page)
6869 mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;
6870
6871 length = mode_pages->hdr.length + 1;
6872 mode_pages->hdr.length = 0;
6873
6874 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
6875 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
6876 length);
6877
6878 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
6879 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6880
6881 LEAVE;
6882 return IPR_RC_JOB_RETURN;
6883}
6884
6885/**
6886 * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
6887 * @ipr_cmd: ipr command struct
6888 *
6889 * This function handles the failure of a Mode Sense to the IOAFP.
6890 * Some adapters do not handle all mode pages.
6891 *
6892 * Return value:
6893 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
6894 **/
6895static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
6896{
96d21f00 6897 u32 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
ac09c349
BK
6898
6899 if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
6900 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
6901 return IPR_RC_JOB_CONTINUE;
6902 }
6903
6904 return ipr_reset_cmd_failed(ipr_cmd);
6905}
6906
6907/**
6908 * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
6909 * @ipr_cmd: ipr command struct
6910 *
6911 * This function send a mode sense to the IOA to retrieve
6912 * the IOA Advanced Function Control mode page.
6913 *
6914 * Return value:
6915 * IPR_RC_JOB_RETURN
6916 **/
6917static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
6918{
6919 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6920
6921 ENTER;
6922 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
6923 0x24, ioa_cfg->vpd_cbs_dma +
6924 offsetof(struct ipr_misc_cbs, mode_pages),
6925 sizeof(struct ipr_mode_pages));
6926
6927 ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
6928 ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;
6929
6930 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
6931
6932 LEAVE;
6933 return IPR_RC_JOB_RETURN;
6934}
6935
1da177e4
LT
6936/**
6937 * ipr_init_res_table - Initialize the resource table
6938 * @ipr_cmd: ipr command struct
6939 *
6940 * This function looks through the existing resource table, comparing
6941 * it with the config table. This function will take care of old/new
6942 * devices and schedule adding/removing them from the mid-layer
6943 * as appropriate.
6944 *
6945 * Return value:
6946 * IPR_RC_JOB_CONTINUE
6947 **/
6948static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
6949{
6950 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
6951 struct ipr_resource_entry *res, *temp;
3e7ebdfa
WB
6952 struct ipr_config_table_entry_wrapper cfgtew;
6953 int entries, found, flag, i;
1da177e4
LT
6954 LIST_HEAD(old_res);
6955
6956 ENTER;
3e7ebdfa
WB
6957 if (ioa_cfg->sis64)
6958 flag = ioa_cfg->u.cfg_table64->hdr64.flags;
6959 else
6960 flag = ioa_cfg->u.cfg_table->hdr.flags;
6961
6962 if (flag & IPR_UCODE_DOWNLOAD_REQ)
1da177e4
LT
6963 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
6964
6965 list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
6966 list_move_tail(&res->queue, &old_res);
6967
3e7ebdfa 6968 if (ioa_cfg->sis64)
438b0331 6969 entries = be16_to_cpu(ioa_cfg->u.cfg_table64->hdr64.num_entries);
3e7ebdfa
WB
6970 else
6971 entries = ioa_cfg->u.cfg_table->hdr.num_entries;
6972
6973 for (i = 0; i < entries; i++) {
6974 if (ioa_cfg->sis64)
6975 cfgtew.u.cfgte64 = &ioa_cfg->u.cfg_table64->dev[i];
6976 else
6977 cfgtew.u.cfgte = &ioa_cfg->u.cfg_table->dev[i];
1da177e4
LT
6978 found = 0;
6979
6980 list_for_each_entry_safe(res, temp, &old_res, queue) {
3e7ebdfa 6981 if (ipr_is_same_device(res, &cfgtew)) {
1da177e4
LT
6982 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
6983 found = 1;
6984 break;
6985 }
6986 }
6987
6988 if (!found) {
6989 if (list_empty(&ioa_cfg->free_res_q)) {
6990 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
6991 break;
6992 }
6993
6994 found = 1;
6995 res = list_entry(ioa_cfg->free_res_q.next,
6996 struct ipr_resource_entry, queue);
6997 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
3e7ebdfa 6998 ipr_init_res_entry(res, &cfgtew);
1da177e4 6999 res->add_to_ml = 1;
56115598
WB
7000 } else if (res->sdev && (ipr_is_vset_device(res) || ipr_is_scsi_disk(res)))
7001 res->sdev->allow_restart = 1;
1da177e4
LT
7002
7003 if (found)
3e7ebdfa 7004 ipr_update_res_entry(res, &cfgtew);
1da177e4
LT
7005 }
7006
7007 list_for_each_entry_safe(res, temp, &old_res, queue) {
7008 if (res->sdev) {
7009 res->del_from_ml = 1;
3e7ebdfa 7010 res->res_handle = IPR_INVALID_RES_HANDLE;
1da177e4 7011 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
1da177e4
LT
7012 }
7013 }
7014
3e7ebdfa
WB
7015 list_for_each_entry_safe(res, temp, &old_res, queue) {
7016 ipr_clear_res_target(res);
7017 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
7018 }
7019
ac09c349
BK
7020 if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
7021 ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
7022 else
7023 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
1da177e4
LT
7024
7025 LEAVE;
7026 return IPR_RC_JOB_CONTINUE;
7027}
7028
7029/**
7030 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
7031 * @ipr_cmd: ipr command struct
7032 *
7033 * This function sends a Query IOA Configuration command
7034 * to the adapter to retrieve the IOA configuration table.
7035 *
7036 * Return value:
7037 * IPR_RC_JOB_RETURN
7038 **/
7039static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
7040{
7041 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7042 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
1da177e4 7043 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
ac09c349 7044 struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
1da177e4
LT
7045
7046 ENTER;
ac09c349
BK
7047 if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
7048 ioa_cfg->dual_raid = 1;
1da177e4
LT
7049 dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
7050 ucode_vpd->major_release, ucode_vpd->card_type,
7051 ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
7052 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
7053 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7054
7055 ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
438b0331 7056 ioarcb->cmd_pkt.cdb[6] = (ioa_cfg->cfg_table_size >> 16) & 0xff;
3e7ebdfa
WB
7057 ioarcb->cmd_pkt.cdb[7] = (ioa_cfg->cfg_table_size >> 8) & 0xff;
7058 ioarcb->cmd_pkt.cdb[8] = ioa_cfg->cfg_table_size & 0xff;
1da177e4 7059
3e7ebdfa 7060 ipr_init_ioadl(ipr_cmd, ioa_cfg->cfg_table_dma, ioa_cfg->cfg_table_size,
a32c055f 7061 IPR_IOADL_FLAGS_READ_LAST);
1da177e4
LT
7062
7063 ipr_cmd->job_step = ipr_init_res_table;
7064
7065 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7066
7067 LEAVE;
7068 return IPR_RC_JOB_RETURN;
7069}
7070
7071/**
7072 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
7073 * @ipr_cmd: ipr command struct
7074 *
7075 * This utility function sends an inquiry to the adapter.
7076 *
7077 * Return value:
7078 * none
7079 **/
7080static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
a32c055f 7081 dma_addr_t dma_addr, u8 xfer_len)
1da177e4
LT
7082{
7083 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
1da177e4
LT
7084
7085 ENTER;
7086 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7087 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7088
7089 ioarcb->cmd_pkt.cdb[0] = INQUIRY;
7090 ioarcb->cmd_pkt.cdb[1] = flags;
7091 ioarcb->cmd_pkt.cdb[2] = page;
7092 ioarcb->cmd_pkt.cdb[4] = xfer_len;
7093
a32c055f 7094 ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
1da177e4
LT
7095
7096 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7097 LEAVE;
7098}
7099
62275040 7100/**
7101 * ipr_inquiry_page_supported - Is the given inquiry page supported
7102 * @page0: inquiry page 0 buffer
7103 * @page: page code.
7104 *
7105 * This function determines if the specified inquiry page is supported.
7106 *
7107 * Return value:
7108 * 1 if page is supported / 0 if not
7109 **/
7110static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
7111{
7112 int i;
7113
7114 for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
7115 if (page0->page[i] == page)
7116 return 1;
7117
7118 return 0;
7119}
7120
ac09c349
BK
7121/**
7122 * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
7123 * @ipr_cmd: ipr command struct
7124 *
7125 * This function sends a Page 0xD0 inquiry to the adapter
7126 * to retrieve adapter capabilities.
7127 *
7128 * Return value:
7129 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7130 **/
7131static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
7132{
7133 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7134 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
7135 struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
7136
7137 ENTER;
7138 ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
7139 memset(cap, 0, sizeof(*cap));
7140
7141 if (ipr_inquiry_page_supported(page0, 0xD0)) {
7142 ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
7143 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
7144 sizeof(struct ipr_inquiry_cap));
7145 return IPR_RC_JOB_RETURN;
7146 }
7147
7148 LEAVE;
7149 return IPR_RC_JOB_CONTINUE;
7150}
7151
1da177e4
LT
7152/**
7153 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
7154 * @ipr_cmd: ipr command struct
7155 *
7156 * This function sends a Page 3 inquiry to the adapter
7157 * to retrieve software VPD information.
7158 *
7159 * Return value:
7160 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7161 **/
7162static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
62275040 7163{
7164 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
62275040 7165
7166 ENTER;
7167
ac09c349 7168 ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
62275040 7169
7170 ipr_ioafp_inquiry(ipr_cmd, 1, 3,
7171 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
7172 sizeof(struct ipr_inquiry_page3));
7173
7174 LEAVE;
7175 return IPR_RC_JOB_RETURN;
7176}
7177
7178/**
7179 * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
7180 * @ipr_cmd: ipr command struct
7181 *
7182 * This function sends a Page 0 inquiry to the adapter
7183 * to retrieve supported inquiry pages.
7184 *
7185 * Return value:
7186 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7187 **/
7188static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
1da177e4
LT
7189{
7190 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7191 char type[5];
7192
7193 ENTER;
7194
7195 /* Grab the type out of the VPD and store it away */
7196 memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
7197 type[4] = '\0';
7198 ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
7199
62275040 7200 ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
1da177e4 7201
62275040 7202 ipr_ioafp_inquiry(ipr_cmd, 1, 0,
7203 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
7204 sizeof(struct ipr_inquiry_page0));
1da177e4
LT
7205
7206 LEAVE;
7207 return IPR_RC_JOB_RETURN;
7208}
7209
7210/**
7211 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
7212 * @ipr_cmd: ipr command struct
7213 *
7214 * This function sends a standard inquiry to the adapter.
7215 *
7216 * Return value:
7217 * IPR_RC_JOB_RETURN
7218 **/
7219static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
7220{
7221 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7222
7223 ENTER;
62275040 7224 ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
1da177e4
LT
7225
7226 ipr_ioafp_inquiry(ipr_cmd, 0, 0,
7227 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
7228 sizeof(struct ipr_ioa_vpd));
7229
7230 LEAVE;
7231 return IPR_RC_JOB_RETURN;
7232}
7233
7234/**
214777ba 7235 * ipr_ioafp_identify_hrrq - Send Identify Host RRQ.
1da177e4
LT
7236 * @ipr_cmd: ipr command struct
7237 *
7238 * This function send an Identify Host Request Response Queue
7239 * command to establish the HRRQ with the adapter.
7240 *
7241 * Return value:
7242 * IPR_RC_JOB_RETURN
7243 **/
214777ba 7244static int ipr_ioafp_identify_hrrq(struct ipr_cmnd *ipr_cmd)
1da177e4
LT
7245{
7246 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7247 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
7248
7249 ENTER;
7250 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
7251
7252 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
7253 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7254
7255 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
214777ba
WB
7256 if (ioa_cfg->sis64)
7257 ioarcb->cmd_pkt.cdb[1] = 0x1;
1da177e4 7258 ioarcb->cmd_pkt.cdb[2] =
214777ba 7259 ((u64) ioa_cfg->host_rrq_dma >> 24) & 0xff;
1da177e4 7260 ioarcb->cmd_pkt.cdb[3] =
214777ba 7261 ((u64) ioa_cfg->host_rrq_dma >> 16) & 0xff;
1da177e4 7262 ioarcb->cmd_pkt.cdb[4] =
214777ba 7263 ((u64) ioa_cfg->host_rrq_dma >> 8) & 0xff;
1da177e4 7264 ioarcb->cmd_pkt.cdb[5] =
214777ba 7265 ((u64) ioa_cfg->host_rrq_dma) & 0xff;
1da177e4
LT
7266 ioarcb->cmd_pkt.cdb[7] =
7267 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
7268 ioarcb->cmd_pkt.cdb[8] =
7269 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
7270
214777ba
WB
7271 if (ioa_cfg->sis64) {
7272 ioarcb->cmd_pkt.cdb[10] =
7273 ((u64) ioa_cfg->host_rrq_dma >> 56) & 0xff;
7274 ioarcb->cmd_pkt.cdb[11] =
7275 ((u64) ioa_cfg->host_rrq_dma >> 48) & 0xff;
7276 ioarcb->cmd_pkt.cdb[12] =
7277 ((u64) ioa_cfg->host_rrq_dma >> 40) & 0xff;
7278 ioarcb->cmd_pkt.cdb[13] =
7279 ((u64) ioa_cfg->host_rrq_dma >> 32) & 0xff;
7280 }
7281
1da177e4
LT
7282 ipr_cmd->job_step = ipr_ioafp_std_inquiry;
7283
7284 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
7285
7286 LEAVE;
7287 return IPR_RC_JOB_RETURN;
7288}
7289
7290/**
7291 * ipr_reset_timer_done - Adapter reset timer function
7292 * @ipr_cmd: ipr command struct
7293 *
7294 * Description: This function is used in adapter reset processing
7295 * for timing events. If the reset_cmd pointer in the IOA
7296 * config struct is not this adapter's we are doing nested
7297 * resets and fail_all_ops will take care of freeing the
7298 * command block.
7299 *
7300 * Return value:
7301 * none
7302 **/
7303static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
7304{
7305 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7306 unsigned long lock_flags = 0;
7307
7308 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
7309
7310 if (ioa_cfg->reset_cmd == ipr_cmd) {
7311 list_del(&ipr_cmd->queue);
7312 ipr_cmd->done(ipr_cmd);
7313 }
7314
7315 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
7316}
7317
7318/**
7319 * ipr_reset_start_timer - Start a timer for adapter reset job
7320 * @ipr_cmd: ipr command struct
7321 * @timeout: timeout value
7322 *
7323 * Description: This function is used in adapter reset processing
7324 * for timing events. If the reset_cmd pointer in the IOA
7325 * config struct is not this adapter's we are doing nested
7326 * resets and fail_all_ops will take care of freeing the
7327 * command block.
7328 *
7329 * Return value:
7330 * none
7331 **/
7332static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
7333 unsigned long timeout)
7334{
7335 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
7336 ipr_cmd->done = ipr_reset_ioa_job;
7337
7338 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
7339 ipr_cmd->timer.expires = jiffies + timeout;
7340 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
7341 add_timer(&ipr_cmd->timer);
7342}
7343
7344/**
7345 * ipr_init_ioa_mem - Initialize ioa_cfg control block
7346 * @ioa_cfg: ioa cfg struct
7347 *
7348 * Return value:
7349 * nothing
7350 **/
7351static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
7352{
7353 memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
7354
7355 /* Initialize Host RRQ pointers */
7356 ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
7357 ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
7358 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
7359 ioa_cfg->toggle_bit = 1;
7360
7361 /* Zero out config table */
3e7ebdfa 7362 memset(ioa_cfg->u.cfg_table, 0, ioa_cfg->cfg_table_size);
1da177e4
LT
7363}
7364
214777ba
WB
7365/**
7366 * ipr_reset_next_stage - Process IPL stage change based on feedback register.
7367 * @ipr_cmd: ipr command struct
7368 *
7369 * Return value:
7370 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7371 **/
7372static int ipr_reset_next_stage(struct ipr_cmnd *ipr_cmd)
7373{
7374 unsigned long stage, stage_time;
7375 u32 feedback;
7376 volatile u32 int_reg;
7377 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7378 u64 maskval = 0;
7379
7380 feedback = readl(ioa_cfg->regs.init_feedback_reg);
7381 stage = feedback & IPR_IPL_INIT_STAGE_MASK;
7382 stage_time = feedback & IPR_IPL_INIT_STAGE_TIME_MASK;
7383
7384 ipr_dbg("IPL stage = 0x%lx, IPL stage time = %ld\n", stage, stage_time);
7385
7386 /* sanity check the stage_time value */
438b0331
WB
7387 if (stage_time == 0)
7388 stage_time = IPR_IPL_INIT_DEFAULT_STAGE_TIME;
7389 else if (stage_time < IPR_IPL_INIT_MIN_STAGE_TIME)
214777ba
WB
7390 stage_time = IPR_IPL_INIT_MIN_STAGE_TIME;
7391 else if (stage_time > IPR_LONG_OPERATIONAL_TIMEOUT)
7392 stage_time = IPR_LONG_OPERATIONAL_TIMEOUT;
7393
7394 if (stage == IPR_IPL_INIT_STAGE_UNKNOWN) {
7395 writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.set_interrupt_mask_reg);
7396 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7397 stage_time = ioa_cfg->transop_timeout;
7398 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7399 } else if (stage == IPR_IPL_INIT_STAGE_TRANSOP) {
1df79ca4
WB
7400 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
7401 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
7402 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
7403 maskval = IPR_PCII_IPL_STAGE_CHANGE;
7404 maskval = (maskval << 32) | IPR_PCII_IOA_TRANS_TO_OPER;
7405 writeq(maskval, ioa_cfg->regs.set_interrupt_mask_reg);
7406 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7407 return IPR_RC_JOB_CONTINUE;
7408 }
214777ba
WB
7409 }
7410
7411 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
7412 ipr_cmd->timer.expires = jiffies + stage_time * HZ;
7413 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
7414 ipr_cmd->done = ipr_reset_ioa_job;
7415 add_timer(&ipr_cmd->timer);
7416 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
7417
7418 return IPR_RC_JOB_RETURN;
7419}
7420
1da177e4
LT
7421/**
7422 * ipr_reset_enable_ioa - Enable the IOA following a reset.
7423 * @ipr_cmd: ipr command struct
7424 *
7425 * This function reinitializes some control blocks and
7426 * enables destructive diagnostics on the adapter.
7427 *
7428 * Return value:
7429 * IPR_RC_JOB_RETURN
7430 **/
7431static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
7432{
7433 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7434 volatile u32 int_reg;
7be96900 7435 volatile u64 maskval;
1da177e4
LT
7436
7437 ENTER;
214777ba 7438 ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
1da177e4
LT
7439 ipr_init_ioa_mem(ioa_cfg);
7440
7441 ioa_cfg->allow_interrupts = 1;
8701f185
WB
7442 if (ioa_cfg->sis64) {
7443 /* Set the adapter to the correct endian mode. */
7444 writel(IPR_ENDIAN_SWAP_KEY, ioa_cfg->regs.endian_swap_reg);
7445 int_reg = readl(ioa_cfg->regs.endian_swap_reg);
7446 }
7447
7be96900 7448 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32);
1da177e4
LT
7449
7450 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
7451 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
214777ba 7452 ioa_cfg->regs.clr_interrupt_mask_reg32);
1da177e4
LT
7453 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7454 return IPR_RC_JOB_CONTINUE;
7455 }
7456
7457 /* Enable destructive diagnostics on IOA */
214777ba
WB
7458 writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg32);
7459
7be96900
WB
7460 if (ioa_cfg->sis64) {
7461 maskval = IPR_PCII_IPL_STAGE_CHANGE;
7462 maskval = (maskval << 32) | IPR_PCII_OPER_INTERRUPTS;
7463 writeq(maskval, ioa_cfg->regs.clr_interrupt_mask_reg);
7464 } else
7465 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg32);
1da177e4 7466
1da177e4
LT
7467 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
7468
7469 dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
7470
214777ba
WB
7471 if (ioa_cfg->sis64) {
7472 ipr_cmd->job_step = ipr_reset_next_stage;
7473 return IPR_RC_JOB_CONTINUE;
7474 }
7475
1da177e4 7476 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
5469cb5b 7477 ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
1da177e4
LT
7478 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
7479 ipr_cmd->done = ipr_reset_ioa_job;
7480 add_timer(&ipr_cmd->timer);
7481 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
7482
7483 LEAVE;
7484 return IPR_RC_JOB_RETURN;
7485}
7486
7487/**
7488 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
7489 * @ipr_cmd: ipr command struct
7490 *
7491 * This function is invoked when an adapter dump has run out
7492 * of processing time.
7493 *
7494 * Return value:
7495 * IPR_RC_JOB_CONTINUE
7496 **/
7497static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
7498{
7499 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7500
7501 if (ioa_cfg->sdt_state == GET_DUMP)
41e9a696
BK
7502 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7503 else if (ioa_cfg->sdt_state == READ_DUMP)
1da177e4
LT
7504 ioa_cfg->sdt_state = ABORT_DUMP;
7505
4c647e90 7506 ioa_cfg->dump_timeout = 1;
1da177e4
LT
7507 ipr_cmd->job_step = ipr_reset_alert;
7508
7509 return IPR_RC_JOB_CONTINUE;
7510}
7511
7512/**
7513 * ipr_unit_check_no_data - Log a unit check/no data error log
7514 * @ioa_cfg: ioa config struct
7515 *
7516 * Logs an error indicating the adapter unit checked, but for some
7517 * reason, we were unable to fetch the unit check buffer.
7518 *
7519 * Return value:
7520 * nothing
7521 **/
7522static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
7523{
7524 ioa_cfg->errors_logged++;
7525 dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
7526}
7527
7528/**
7529 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
7530 * @ioa_cfg: ioa config struct
7531 *
7532 * Fetches the unit check buffer from the adapter by clocking the data
7533 * through the mailbox register.
7534 *
7535 * Return value:
7536 * nothing
7537 **/
7538static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
7539{
7540 unsigned long mailbox;
7541 struct ipr_hostrcb *hostrcb;
7542 struct ipr_uc_sdt sdt;
7543 int rc, length;
65f56475 7544 u32 ioasc;
1da177e4
LT
7545
7546 mailbox = readl(ioa_cfg->ioa_mailbox);
7547
dcbad00e 7548 if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(mailbox)) {
1da177e4
LT
7549 ipr_unit_check_no_data(ioa_cfg);
7550 return;
7551 }
7552
7553 memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
7554 rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
7555 (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
7556
dcbad00e
WB
7557 if (rc || !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY) ||
7558 ((be32_to_cpu(sdt.hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
7559 (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
1da177e4
LT
7560 ipr_unit_check_no_data(ioa_cfg);
7561 return;
7562 }
7563
7564 /* Find length of the first sdt entry (UC buffer) */
dcbad00e
WB
7565 if (be32_to_cpu(sdt.hdr.state) == IPR_FMT3_SDT_READY_TO_USE)
7566 length = be32_to_cpu(sdt.entry[0].end_token);
7567 else
7568 length = (be32_to_cpu(sdt.entry[0].end_token) -
7569 be32_to_cpu(sdt.entry[0].start_token)) &
7570 IPR_FMT2_MBX_ADDR_MASK;
1da177e4
LT
7571
7572 hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
7573 struct ipr_hostrcb, queue);
7574 list_del(&hostrcb->queue);
7575 memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
7576
7577 rc = ipr_get_ldump_data_section(ioa_cfg,
dcbad00e 7578 be32_to_cpu(sdt.entry[0].start_token),
1da177e4
LT
7579 (__be32 *)&hostrcb->hcam,
7580 min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
7581
65f56475 7582 if (!rc) {
1da177e4 7583 ipr_handle_log_data(ioa_cfg, hostrcb);
4565e370 7584 ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
65f56475
BK
7585 if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
7586 ioa_cfg->sdt_state == GET_DUMP)
7587 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
7588 } else
1da177e4
LT
7589 ipr_unit_check_no_data(ioa_cfg);
7590
7591 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
7592}
7593
110def85
WB
7594/**
7595 * ipr_reset_get_unit_check_job - Call to get the unit check buffer.
7596 * @ipr_cmd: ipr command struct
7597 *
7598 * Description: This function will call to get the unit check buffer.
7599 *
7600 * Return value:
7601 * IPR_RC_JOB_RETURN
7602 **/
7603static int ipr_reset_get_unit_check_job(struct ipr_cmnd *ipr_cmd)
7604{
7605 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7606
7607 ENTER;
7608 ioa_cfg->ioa_unit_checked = 0;
7609 ipr_get_unit_check_buffer(ioa_cfg);
7610 ipr_cmd->job_step = ipr_reset_alert;
7611 ipr_reset_start_timer(ipr_cmd, 0);
7612
7613 LEAVE;
7614 return IPR_RC_JOB_RETURN;
7615}
7616
1da177e4
LT
7617/**
7618 * ipr_reset_restore_cfg_space - Restore PCI config space.
7619 * @ipr_cmd: ipr command struct
7620 *
7621 * Description: This function restores the saved PCI config space of
7622 * the adapter, fails all outstanding ops back to the callers, and
7623 * fetches the dump/unit check if applicable to this reset.
7624 *
7625 * Return value:
7626 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7627 **/
7628static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
7629{
7630 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
630ad831 7631 u32 int_reg;
1da177e4
LT
7632
7633 ENTER;
99c965dd 7634 ioa_cfg->pdev->state_saved = true;
1d3c16a8 7635 pci_restore_state(ioa_cfg->pdev);
1da177e4
LT
7636
7637 if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
96d21f00 7638 ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
1da177e4
LT
7639 return IPR_RC_JOB_CONTINUE;
7640 }
7641
7642 ipr_fail_all_ops(ioa_cfg);
7643
8701f185
WB
7644 if (ioa_cfg->sis64) {
7645 /* Set the adapter to the correct endian mode. */
7646 writel(IPR_ENDIAN_SWAP_KEY, ioa_cfg->regs.endian_swap_reg);
7647 int_reg = readl(ioa_cfg->regs.endian_swap_reg);
7648 }
7649
1da177e4 7650 if (ioa_cfg->ioa_unit_checked) {
110def85
WB
7651 if (ioa_cfg->sis64) {
7652 ipr_cmd->job_step = ipr_reset_get_unit_check_job;
7653 ipr_reset_start_timer(ipr_cmd, IPR_DUMP_DELAY_TIMEOUT);
7654 return IPR_RC_JOB_RETURN;
7655 } else {
7656 ioa_cfg->ioa_unit_checked = 0;
7657 ipr_get_unit_check_buffer(ioa_cfg);
7658 ipr_cmd->job_step = ipr_reset_alert;
7659 ipr_reset_start_timer(ipr_cmd, 0);
7660 return IPR_RC_JOB_RETURN;
7661 }
1da177e4
LT
7662 }
7663
7664 if (ioa_cfg->in_ioa_bringdown) {
7665 ipr_cmd->job_step = ipr_ioa_bringdown_done;
7666 } else {
7667 ipr_cmd->job_step = ipr_reset_enable_ioa;
7668
7669 if (GET_DUMP == ioa_cfg->sdt_state) {
41e9a696 7670 ioa_cfg->sdt_state = READ_DUMP;
4c647e90 7671 ioa_cfg->dump_timeout = 0;
4d4dd706
KSS
7672 if (ioa_cfg->sis64)
7673 ipr_reset_start_timer(ipr_cmd, IPR_SIS64_DUMP_TIMEOUT);
7674 else
7675 ipr_reset_start_timer(ipr_cmd, IPR_SIS32_DUMP_TIMEOUT);
1da177e4
LT
7676 ipr_cmd->job_step = ipr_reset_wait_for_dump;
7677 schedule_work(&ioa_cfg->work_q);
7678 return IPR_RC_JOB_RETURN;
7679 }
7680 }
7681
438b0331 7682 LEAVE;
1da177e4
LT
7683 return IPR_RC_JOB_CONTINUE;
7684}
7685
e619e1a7
BK
7686/**
7687 * ipr_reset_bist_done - BIST has completed on the adapter.
7688 * @ipr_cmd: ipr command struct
7689 *
7690 * Description: Unblock config space and resume the reset process.
7691 *
7692 * Return value:
7693 * IPR_RC_JOB_CONTINUE
7694 **/
7695static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
7696{
fb51ccbf
JK
7697 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7698
e619e1a7 7699 ENTER;
fb51ccbf
JK
7700 if (ioa_cfg->cfg_locked)
7701 pci_cfg_access_unlock(ioa_cfg->pdev);
7702 ioa_cfg->cfg_locked = 0;
e619e1a7
BK
7703 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
7704 LEAVE;
7705 return IPR_RC_JOB_CONTINUE;
7706}
7707
1da177e4
LT
7708/**
7709 * ipr_reset_start_bist - Run BIST on the adapter.
7710 * @ipr_cmd: ipr command struct
7711 *
7712 * Description: This function runs BIST on the adapter, then delays 2 seconds.
7713 *
7714 * Return value:
7715 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7716 **/
7717static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
7718{
7719 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
cb237ef7 7720 int rc = PCIBIOS_SUCCESSFUL;
1da177e4
LT
7721
7722 ENTER;
cb237ef7
WB
7723 if (ioa_cfg->ipr_chip->bist_method == IPR_MMIO)
7724 writel(IPR_UPROCI_SIS64_START_BIST,
7725 ioa_cfg->regs.set_uproc_interrupt_reg32);
7726 else
7727 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
7728
7729 if (rc == PCIBIOS_SUCCESSFUL) {
e619e1a7 7730 ipr_cmd->job_step = ipr_reset_bist_done;
1da177e4
LT
7731 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
7732 rc = IPR_RC_JOB_RETURN;
cb237ef7 7733 } else {
fb51ccbf
JK
7734 if (ioa_cfg->cfg_locked)
7735 pci_cfg_access_unlock(ipr_cmd->ioa_cfg->pdev);
7736 ioa_cfg->cfg_locked = 0;
cb237ef7
WB
7737 ipr_cmd->s.ioasa.hdr.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
7738 rc = IPR_RC_JOB_CONTINUE;
1da177e4
LT
7739 }
7740
7741 LEAVE;
7742 return rc;
7743}
7744
463fc696
BK
7745/**
7746 * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
7747 * @ipr_cmd: ipr command struct
7748 *
7749 * Description: This clears PCI reset to the adapter and delays two seconds.
7750 *
7751 * Return value:
7752 * IPR_RC_JOB_RETURN
7753 **/
7754static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
7755{
7756 ENTER;
7757 pci_set_pcie_reset_state(ipr_cmd->ioa_cfg->pdev, pcie_deassert_reset);
7758 ipr_cmd->job_step = ipr_reset_bist_done;
7759 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
7760 LEAVE;
7761 return IPR_RC_JOB_RETURN;
7762}
7763
7764/**
7765 * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
7766 * @ipr_cmd: ipr command struct
7767 *
7768 * Description: This asserts PCI reset to the adapter.
7769 *
7770 * Return value:
7771 * IPR_RC_JOB_RETURN
7772 **/
7773static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
7774{
7775 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7776 struct pci_dev *pdev = ioa_cfg->pdev;
7777
7778 ENTER;
463fc696
BK
7779 pci_set_pcie_reset_state(pdev, pcie_warm_reset);
7780 ipr_cmd->job_step = ipr_reset_slot_reset_done;
7781 ipr_reset_start_timer(ipr_cmd, IPR_PCI_RESET_TIMEOUT);
7782 LEAVE;
7783 return IPR_RC_JOB_RETURN;
7784}
7785
fb51ccbf
JK
7786/**
7787 * ipr_reset_block_config_access_wait - Wait for permission to block config access
7788 * @ipr_cmd: ipr command struct
7789 *
7790 * Description: This attempts to block config access to the IOA.
7791 *
7792 * Return value:
7793 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7794 **/
7795static int ipr_reset_block_config_access_wait(struct ipr_cmnd *ipr_cmd)
7796{
7797 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7798 int rc = IPR_RC_JOB_CONTINUE;
7799
7800 if (pci_cfg_access_trylock(ioa_cfg->pdev)) {
7801 ioa_cfg->cfg_locked = 1;
7802 ipr_cmd->job_step = ioa_cfg->reset;
7803 } else {
7804 if (ipr_cmd->u.time_left) {
7805 rc = IPR_RC_JOB_RETURN;
7806 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
7807 ipr_reset_start_timer(ipr_cmd,
7808 IPR_CHECK_FOR_RESET_TIMEOUT);
7809 } else {
7810 ipr_cmd->job_step = ioa_cfg->reset;
7811 dev_err(&ioa_cfg->pdev->dev,
7812 "Timed out waiting to lock config access. Resetting anyway.\n");
7813 }
7814 }
7815
7816 return rc;
7817}
7818
7819/**
7820 * ipr_reset_block_config_access - Block config access to the IOA
7821 * @ipr_cmd: ipr command struct
7822 *
7823 * Description: This attempts to block config access to the IOA
7824 *
7825 * Return value:
7826 * IPR_RC_JOB_CONTINUE
7827 **/
7828static int ipr_reset_block_config_access(struct ipr_cmnd *ipr_cmd)
7829{
7830 ipr_cmd->ioa_cfg->cfg_locked = 0;
7831 ipr_cmd->job_step = ipr_reset_block_config_access_wait;
7832 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
7833 return IPR_RC_JOB_CONTINUE;
7834}
7835
1da177e4
LT
7836/**
7837 * ipr_reset_allowed - Query whether or not IOA can be reset
7838 * @ioa_cfg: ioa config struct
7839 *
7840 * Return value:
7841 * 0 if reset not allowed / non-zero if reset is allowed
7842 **/
7843static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
7844{
7845 volatile u32 temp_reg;
7846
7847 temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
7848 return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
7849}
7850
7851/**
7852 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
7853 * @ipr_cmd: ipr command struct
7854 *
7855 * Description: This function waits for adapter permission to run BIST,
7856 * then runs BIST. If the adapter does not give permission after a
7857 * reasonable time, we will reset the adapter anyway. The impact of
7858 * resetting the adapter without warning the adapter is the risk of
7859 * losing the persistent error log on the adapter. If the adapter is
7860 * reset while it is writing to the flash on the adapter, the flash
7861 * segment will have bad ECC and be zeroed.
7862 *
7863 * Return value:
7864 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7865 **/
7866static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
7867{
7868 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7869 int rc = IPR_RC_JOB_RETURN;
7870
7871 if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
7872 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
7873 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
7874 } else {
fb51ccbf 7875 ipr_cmd->job_step = ipr_reset_block_config_access;
1da177e4
LT
7876 rc = IPR_RC_JOB_CONTINUE;
7877 }
7878
7879 return rc;
7880}
7881
7882/**
8701f185 7883 * ipr_reset_alert - Alert the adapter of a pending reset
1da177e4
LT
7884 * @ipr_cmd: ipr command struct
7885 *
7886 * Description: This function alerts the adapter that it will be reset.
7887 * If memory space is not currently enabled, proceed directly
7888 * to running BIST on the adapter. The timer must always be started
7889 * so we guarantee we do not run BIST from ipr_isr.
7890 *
7891 * Return value:
7892 * IPR_RC_JOB_RETURN
7893 **/
7894static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
7895{
7896 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7897 u16 cmd_reg;
7898 int rc;
7899
7900 ENTER;
7901 rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
7902
7903 if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
7904 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
214777ba 7905 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg32);
1da177e4
LT
7906 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
7907 } else {
fb51ccbf 7908 ipr_cmd->job_step = ipr_reset_block_config_access;
1da177e4
LT
7909 }
7910
7911 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
7912 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
7913
7914 LEAVE;
7915 return IPR_RC_JOB_RETURN;
7916}
7917
7918/**
7919 * ipr_reset_ucode_download_done - Microcode download completion
7920 * @ipr_cmd: ipr command struct
7921 *
7922 * Description: This function unmaps the microcode download buffer.
7923 *
7924 * Return value:
7925 * IPR_RC_JOB_CONTINUE
7926 **/
7927static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
7928{
7929 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7930 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
7931
7932 pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
7933 sglist->num_sg, DMA_TO_DEVICE);
7934
7935 ipr_cmd->job_step = ipr_reset_alert;
7936 return IPR_RC_JOB_CONTINUE;
7937}
7938
7939/**
7940 * ipr_reset_ucode_download - Download microcode to the adapter
7941 * @ipr_cmd: ipr command struct
7942 *
7943 * Description: This function checks to see if it there is microcode
7944 * to download to the adapter. If there is, a download is performed.
7945 *
7946 * Return value:
7947 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7948 **/
7949static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
7950{
7951 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7952 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
7953
7954 ENTER;
7955 ipr_cmd->job_step = ipr_reset_alert;
7956
7957 if (!sglist)
7958 return IPR_RC_JOB_CONTINUE;
7959
7960 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
7961 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
7962 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
7963 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
7964 ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
7965 ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
7966 ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
7967
a32c055f
WB
7968 if (ioa_cfg->sis64)
7969 ipr_build_ucode_ioadl64(ipr_cmd, sglist);
7970 else
7971 ipr_build_ucode_ioadl(ipr_cmd, sglist);
1da177e4
LT
7972 ipr_cmd->job_step = ipr_reset_ucode_download_done;
7973
7974 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
7975 IPR_WRITE_BUFFER_TIMEOUT);
7976
7977 LEAVE;
7978 return IPR_RC_JOB_RETURN;
7979}
7980
7981/**
7982 * ipr_reset_shutdown_ioa - Shutdown the adapter
7983 * @ipr_cmd: ipr command struct
7984 *
7985 * Description: This function issues an adapter shutdown of the
7986 * specified type to the specified adapter as part of the
7987 * adapter reset job.
7988 *
7989 * Return value:
7990 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
7991 **/
7992static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
7993{
7994 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
7995 enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
7996 unsigned long timeout;
7997 int rc = IPR_RC_JOB_CONTINUE;
7998
7999 ENTER;
8000 if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
8001 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
8002 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
8003 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
8004 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
8005
ac09c349
BK
8006 if (shutdown_type == IPR_SHUTDOWN_NORMAL)
8007 timeout = IPR_SHUTDOWN_TIMEOUT;
1da177e4
LT
8008 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
8009 timeout = IPR_INTERNAL_TIMEOUT;
ac09c349
BK
8010 else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
8011 timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
1da177e4 8012 else
ac09c349 8013 timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
1da177e4
LT
8014
8015 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
8016
8017 rc = IPR_RC_JOB_RETURN;
8018 ipr_cmd->job_step = ipr_reset_ucode_download;
8019 } else
8020 ipr_cmd->job_step = ipr_reset_alert;
8021
8022 LEAVE;
8023 return rc;
8024}
8025
8026/**
8027 * ipr_reset_ioa_job - Adapter reset job
8028 * @ipr_cmd: ipr command struct
8029 *
8030 * Description: This function is the job router for the adapter reset job.
8031 *
8032 * Return value:
8033 * none
8034 **/
8035static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
8036{
8037 u32 rc, ioasc;
1da177e4
LT
8038 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
8039
8040 do {
96d21f00 8041 ioasc = be32_to_cpu(ipr_cmd->s.ioasa.hdr.ioasc);
1da177e4
LT
8042
8043 if (ioa_cfg->reset_cmd != ipr_cmd) {
8044 /*
8045 * We are doing nested adapter resets and this is
8046 * not the current reset job.
8047 */
8048 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
8049 return;
8050 }
8051
8052 if (IPR_IOASC_SENSE_KEY(ioasc)) {
dfed823e 8053 rc = ipr_cmd->job_step_failed(ipr_cmd);
8054 if (rc == IPR_RC_JOB_RETURN)
8055 return;
1da177e4
LT
8056 }
8057
8058 ipr_reinit_ipr_cmnd(ipr_cmd);
dfed823e 8059 ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
1da177e4
LT
8060 rc = ipr_cmd->job_step(ipr_cmd);
8061 } while(rc == IPR_RC_JOB_CONTINUE);
8062}
8063
8064/**
8065 * _ipr_initiate_ioa_reset - Initiate an adapter reset
8066 * @ioa_cfg: ioa config struct
8067 * @job_step: first job step of reset job
8068 * @shutdown_type: shutdown type
8069 *
8070 * Description: This function will initiate the reset of the given adapter
8071 * starting at the selected job step.
8072 * If the caller needs to wait on the completion of the reset,
8073 * the caller must sleep on the reset_wait_q.
8074 *
8075 * Return value:
8076 * none
8077 **/
8078static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
8079 int (*job_step) (struct ipr_cmnd *),
8080 enum ipr_shutdown_type shutdown_type)
8081{
8082 struct ipr_cmnd *ipr_cmd;
8083
8084 ioa_cfg->in_reset_reload = 1;
8085 ioa_cfg->allow_cmds = 0;
8086 scsi_block_requests(ioa_cfg->host);
8087
8088 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
8089 ioa_cfg->reset_cmd = ipr_cmd;
8090 ipr_cmd->job_step = job_step;
8091 ipr_cmd->u.shutdown_type = shutdown_type;
8092
8093 ipr_reset_ioa_job(ipr_cmd);
8094}
8095
8096/**
8097 * ipr_initiate_ioa_reset - Initiate an adapter reset
8098 * @ioa_cfg: ioa config struct
8099 * @shutdown_type: shutdown type
8100 *
8101 * Description: This function will initiate the reset of the given adapter.
8102 * If the caller needs to wait on the completion of the reset,
8103 * the caller must sleep on the reset_wait_q.
8104 *
8105 * Return value:
8106 * none
8107 **/
8108static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
8109 enum ipr_shutdown_type shutdown_type)
8110{
8111 if (ioa_cfg->ioa_is_dead)
8112 return;
8113
41e9a696
BK
8114 if (ioa_cfg->in_reset_reload) {
8115 if (ioa_cfg->sdt_state == GET_DUMP)
8116 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
8117 else if (ioa_cfg->sdt_state == READ_DUMP)
8118 ioa_cfg->sdt_state = ABORT_DUMP;
8119 }
1da177e4
LT
8120
8121 if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
8122 dev_err(&ioa_cfg->pdev->dev,
8123 "IOA taken offline - error recovery failed\n");
8124
8125 ioa_cfg->reset_retries = 0;
8126 ioa_cfg->ioa_is_dead = 1;
8127
8128 if (ioa_cfg->in_ioa_bringdown) {
8129 ioa_cfg->reset_cmd = NULL;
8130 ioa_cfg->in_reset_reload = 0;
8131 ipr_fail_all_ops(ioa_cfg);
8132 wake_up_all(&ioa_cfg->reset_wait_q);
8133
8134 spin_unlock_irq(ioa_cfg->host->host_lock);
8135 scsi_unblock_requests(ioa_cfg->host);
8136 spin_lock_irq(ioa_cfg->host->host_lock);
8137 return;
8138 } else {
8139 ioa_cfg->in_ioa_bringdown = 1;
8140 shutdown_type = IPR_SHUTDOWN_NONE;
8141 }
8142 }
8143
8144 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
8145 shutdown_type);
8146}
8147
f8a88b19
LV
8148/**
8149 * ipr_reset_freeze - Hold off all I/O activity
8150 * @ipr_cmd: ipr command struct
8151 *
8152 * Description: If the PCI slot is frozen, hold off all I/O
8153 * activity; then, as soon as the slot is available again,
8154 * initiate an adapter reset.
8155 */
8156static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
8157{
8158 /* Disallow new interrupts, avoid loop */
8159 ipr_cmd->ioa_cfg->allow_interrupts = 0;
8160 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
8161 ipr_cmd->done = ipr_reset_ioa_job;
8162 return IPR_RC_JOB_RETURN;
8163}
8164
8165/**
8166 * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
8167 * @pdev: PCI device struct
8168 *
8169 * Description: This routine is called to tell us that the PCI bus
8170 * is down. Can't do anything here, except put the device driver
8171 * into a holding pattern, waiting for the PCI bus to come back.
8172 */
8173static void ipr_pci_frozen(struct pci_dev *pdev)
8174{
8175 unsigned long flags = 0;
8176 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8177
8178 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8179 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
8180 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8181}
8182
8183/**
8184 * ipr_pci_slot_reset - Called when PCI slot has been reset.
8185 * @pdev: PCI device struct
8186 *
8187 * Description: This routine is called by the pci error recovery
8188 * code after the PCI slot has been reset, just before we
8189 * should resume normal operations.
8190 */
8191static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
8192{
8193 unsigned long flags = 0;
8194 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8195
8196 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
463fc696
BK
8197 if (ioa_cfg->needs_warm_reset)
8198 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8199 else
8200 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
8201 IPR_SHUTDOWN_NONE);
f8a88b19
LV
8202 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8203 return PCI_ERS_RESULT_RECOVERED;
8204}
8205
8206/**
8207 * ipr_pci_perm_failure - Called when PCI slot is dead for good.
8208 * @pdev: PCI device struct
8209 *
8210 * Description: This routine is called when the PCI bus has
8211 * permanently failed.
8212 */
8213static void ipr_pci_perm_failure(struct pci_dev *pdev)
8214{
8215 unsigned long flags = 0;
8216 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
8217
8218 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
8219 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
8220 ioa_cfg->sdt_state = ABORT_DUMP;
8221 ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES;
8222 ioa_cfg->in_ioa_bringdown = 1;
6ff63896 8223 ioa_cfg->allow_cmds = 0;
f8a88b19
LV
8224 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8225 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
8226}
8227
8228/**
8229 * ipr_pci_error_detected - Called when a PCI error is detected.
8230 * @pdev: PCI device struct
8231 * @state: PCI channel state
8232 *
8233 * Description: Called when a PCI error is detected.
8234 *
8235 * Return value:
8236 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8237 */
8238static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
8239 pci_channel_state_t state)
8240{
8241 switch (state) {
8242 case pci_channel_io_frozen:
8243 ipr_pci_frozen(pdev);
8244 return PCI_ERS_RESULT_NEED_RESET;
8245 case pci_channel_io_perm_failure:
8246 ipr_pci_perm_failure(pdev);
8247 return PCI_ERS_RESULT_DISCONNECT;
8248 break;
8249 default:
8250 break;
8251 }
8252 return PCI_ERS_RESULT_NEED_RESET;
8253}
8254
1da177e4
LT
8255/**
8256 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
8257 * @ioa_cfg: ioa cfg struct
8258 *
8259 * Description: This is the second phase of adapter intialization
8260 * This function takes care of initilizing the adapter to the point
8261 * where it can accept new commands.
8262
8263 * Return value:
b1c11812 8264 * 0 on success / -EIO on failure
1da177e4
LT
8265 **/
8266static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
8267{
8268 int rc = 0;
8269 unsigned long host_lock_flags = 0;
8270
8271 ENTER;
8272 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8273 dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
ce155cce 8274 if (ioa_cfg->needs_hard_reset) {
8275 ioa_cfg->needs_hard_reset = 0;
8276 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
8277 } else
8278 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
8279 IPR_SHUTDOWN_NONE);
1da177e4
LT
8280
8281 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8282 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
8283 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
8284
8285 if (ioa_cfg->ioa_is_dead) {
8286 rc = -EIO;
8287 } else if (ipr_invalid_adapter(ioa_cfg)) {
8288 if (!ipr_testmode)
8289 rc = -EIO;
8290
8291 dev_err(&ioa_cfg->pdev->dev,
8292 "Adapter not supported in this hardware configuration.\n");
8293 }
8294
8295 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
8296
8297 LEAVE;
8298 return rc;
8299}
8300
8301/**
8302 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
8303 * @ioa_cfg: ioa config struct
8304 *
8305 * Return value:
8306 * none
8307 **/
8308static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
8309{
8310 int i;
8311
8312 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
8313 if (ioa_cfg->ipr_cmnd_list[i])
8314 pci_pool_free(ioa_cfg->ipr_cmd_pool,
8315 ioa_cfg->ipr_cmnd_list[i],
8316 ioa_cfg->ipr_cmnd_list_dma[i]);
8317
8318 ioa_cfg->ipr_cmnd_list[i] = NULL;
8319 }
8320
8321 if (ioa_cfg->ipr_cmd_pool)
8322 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
8323
89aad428
BK
8324 kfree(ioa_cfg->ipr_cmnd_list);
8325 kfree(ioa_cfg->ipr_cmnd_list_dma);
8326 ioa_cfg->ipr_cmnd_list = NULL;
8327 ioa_cfg->ipr_cmnd_list_dma = NULL;
1da177e4
LT
8328 ioa_cfg->ipr_cmd_pool = NULL;
8329}
8330
8331/**
8332 * ipr_free_mem - Frees memory allocated for an adapter
8333 * @ioa_cfg: ioa cfg struct
8334 *
8335 * Return value:
8336 * nothing
8337 **/
8338static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
8339{
8340 int i;
8341
8342 kfree(ioa_cfg->res_entries);
8343 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
8344 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
8345 ipr_free_cmd_blks(ioa_cfg);
8346 pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
8347 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
3e7ebdfa
WB
8348 pci_free_consistent(ioa_cfg->pdev, ioa_cfg->cfg_table_size,
8349 ioa_cfg->u.cfg_table,
1da177e4
LT
8350 ioa_cfg->cfg_table_dma);
8351
8352 for (i = 0; i < IPR_NUM_HCAMS; i++) {
8353 pci_free_consistent(ioa_cfg->pdev,
8354 sizeof(struct ipr_hostrcb),
8355 ioa_cfg->hostrcb[i],
8356 ioa_cfg->hostrcb_dma[i]);
8357 }
8358
8359 ipr_free_dump(ioa_cfg);
1da177e4
LT
8360 kfree(ioa_cfg->trace);
8361}
8362
8363/**
8364 * ipr_free_all_resources - Free all allocated resources for an adapter.
8365 * @ipr_cmd: ipr command struct
8366 *
8367 * This function frees all allocated resources for the
8368 * specified adapter.
8369 *
8370 * Return value:
8371 * none
8372 **/
8373static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
8374{
8375 struct pci_dev *pdev = ioa_cfg->pdev;
8376
8377 ENTER;
8378 free_irq(pdev->irq, ioa_cfg);
5a9ef25b 8379 pci_disable_msi(pdev);
1da177e4
LT
8380 iounmap(ioa_cfg->hdw_dma_regs);
8381 pci_release_regions(pdev);
8382 ipr_free_mem(ioa_cfg);
8383 scsi_host_put(ioa_cfg->host);
8384 pci_disable_device(pdev);
8385 LEAVE;
8386}
8387
8388/**
8389 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
8390 * @ioa_cfg: ioa config struct
8391 *
8392 * Return value:
8393 * 0 on success / -ENOMEM on allocation failure
8394 **/
8395static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
8396{
8397 struct ipr_cmnd *ipr_cmd;
8398 struct ipr_ioarcb *ioarcb;
8399 dma_addr_t dma_addr;
8400 int i;
8401
8402 ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
1bfff2f8 8403 sizeof(struct ipr_cmnd), 512, 0);
1da177e4
LT
8404
8405 if (!ioa_cfg->ipr_cmd_pool)
8406 return -ENOMEM;
8407
89aad428
BK
8408 ioa_cfg->ipr_cmnd_list = kcalloc(IPR_NUM_CMD_BLKS, sizeof(struct ipr_cmnd *), GFP_KERNEL);
8409 ioa_cfg->ipr_cmnd_list_dma = kcalloc(IPR_NUM_CMD_BLKS, sizeof(dma_addr_t), GFP_KERNEL);
8410
8411 if (!ioa_cfg->ipr_cmnd_list || !ioa_cfg->ipr_cmnd_list_dma) {
8412 ipr_free_cmd_blks(ioa_cfg);
8413 return -ENOMEM;
8414 }
8415
1da177e4 8416 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
e94b1766 8417 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
1da177e4
LT
8418
8419 if (!ipr_cmd) {
8420 ipr_free_cmd_blks(ioa_cfg);
8421 return -ENOMEM;
8422 }
8423
8424 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
8425 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
8426 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
8427
8428 ioarcb = &ipr_cmd->ioarcb;
a32c055f
WB
8429 ipr_cmd->dma_addr = dma_addr;
8430 if (ioa_cfg->sis64)
8431 ioarcb->a.ioarcb_host_pci_addr64 = cpu_to_be64(dma_addr);
8432 else
8433 ioarcb->a.ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
8434
1da177e4 8435 ioarcb->host_response_handle = cpu_to_be32(i << 2);
a32c055f
WB
8436 if (ioa_cfg->sis64) {
8437 ioarcb->u.sis64_addr_data.data_ioadl_addr =
8438 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
8439 ioarcb->u.sis64_addr_data.ioasa_host_pci_addr =
96d21f00 8440 cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, s.ioasa64));
a32c055f
WB
8441 } else {
8442 ioarcb->write_ioadl_addr =
8443 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
8444 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
8445 ioarcb->ioasa_host_pci_addr =
96d21f00 8446 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, s.ioasa));
a32c055f 8447 }
1da177e4
LT
8448 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
8449 ipr_cmd->cmd_index = i;
8450 ipr_cmd->ioa_cfg = ioa_cfg;
8451 ipr_cmd->sense_buffer_dma = dma_addr +
8452 offsetof(struct ipr_cmnd, sense_buffer);
8453
8454 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
8455 }
8456
8457 return 0;
8458}
8459
8460/**
8461 * ipr_alloc_mem - Allocate memory for an adapter
8462 * @ioa_cfg: ioa config struct
8463 *
8464 * Return value:
8465 * 0 on success / non-zero for error
8466 **/
8467static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
8468{
8469 struct pci_dev *pdev = ioa_cfg->pdev;
8470 int i, rc = -ENOMEM;
8471
8472 ENTER;
0bc42e35 8473 ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
3e7ebdfa 8474 ioa_cfg->max_devs_supported, GFP_KERNEL);
1da177e4
LT
8475
8476 if (!ioa_cfg->res_entries)
8477 goto out;
8478
3e7ebdfa
WB
8479 if (ioa_cfg->sis64) {
8480 ioa_cfg->target_ids = kzalloc(sizeof(unsigned long) *
8481 BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
8482 ioa_cfg->array_ids = kzalloc(sizeof(unsigned long) *
8483 BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
8484 ioa_cfg->vset_ids = kzalloc(sizeof(unsigned long) *
8485 BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
8486 }
8487
8488 for (i = 0; i < ioa_cfg->max_devs_supported; i++) {
1da177e4 8489 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
3e7ebdfa
WB
8490 ioa_cfg->res_entries[i].ioa_cfg = ioa_cfg;
8491 }
1da177e4
LT
8492
8493 ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
8494 sizeof(struct ipr_misc_cbs),
8495 &ioa_cfg->vpd_cbs_dma);
8496
8497 if (!ioa_cfg->vpd_cbs)
8498 goto out_free_res_entries;
8499
8500 if (ipr_alloc_cmd_blks(ioa_cfg))
8501 goto out_free_vpd_cbs;
8502
8503 ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
8504 sizeof(u32) * IPR_NUM_CMD_BLKS,
8505 &ioa_cfg->host_rrq_dma);
8506
8507 if (!ioa_cfg->host_rrq)
8508 goto out_ipr_free_cmd_blocks;
8509
3e7ebdfa
WB
8510 ioa_cfg->u.cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
8511 ioa_cfg->cfg_table_size,
8512 &ioa_cfg->cfg_table_dma);
1da177e4 8513
3e7ebdfa 8514 if (!ioa_cfg->u.cfg_table)
1da177e4
LT
8515 goto out_free_host_rrq;
8516
8517 for (i = 0; i < IPR_NUM_HCAMS; i++) {
8518 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
8519 sizeof(struct ipr_hostrcb),
8520 &ioa_cfg->hostrcb_dma[i]);
8521
8522 if (!ioa_cfg->hostrcb[i])
8523 goto out_free_hostrcb_dma;
8524
8525 ioa_cfg->hostrcb[i]->hostrcb_dma =
8526 ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
49dc6a18 8527 ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
1da177e4
LT
8528 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
8529 }
8530
0bc42e35 8531 ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
1da177e4
LT
8532 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
8533
8534 if (!ioa_cfg->trace)
8535 goto out_free_hostrcb_dma;
8536
1da177e4
LT
8537 rc = 0;
8538out:
8539 LEAVE;
8540 return rc;
8541
8542out_free_hostrcb_dma:
8543 while (i-- > 0) {
8544 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
8545 ioa_cfg->hostrcb[i],
8546 ioa_cfg->hostrcb_dma[i]);
8547 }
3e7ebdfa
WB
8548 pci_free_consistent(pdev, ioa_cfg->cfg_table_size,
8549 ioa_cfg->u.cfg_table,
8550 ioa_cfg->cfg_table_dma);
1da177e4
LT
8551out_free_host_rrq:
8552 pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
8553 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
8554out_ipr_free_cmd_blocks:
8555 ipr_free_cmd_blks(ioa_cfg);
8556out_free_vpd_cbs:
8557 pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
8558 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
8559out_free_res_entries:
8560 kfree(ioa_cfg->res_entries);
8561 goto out;
8562}
8563
8564/**
8565 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
8566 * @ioa_cfg: ioa config struct
8567 *
8568 * Return value:
8569 * none
8570 **/
8571static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
8572{
8573 int i;
8574
8575 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
8576 ioa_cfg->bus_attr[i].bus = i;
8577 ioa_cfg->bus_attr[i].qas_enabled = 0;
8578 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
8579 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
8580 ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
8581 else
8582 ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
8583 }
8584}
8585
8586/**
8587 * ipr_init_ioa_cfg - Initialize IOA config struct
8588 * @ioa_cfg: ioa config struct
8589 * @host: scsi host struct
8590 * @pdev: PCI dev struct
8591 *
8592 * Return value:
8593 * none
8594 **/
8595static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
8596 struct Scsi_Host *host, struct pci_dev *pdev)
8597{
8598 const struct ipr_interrupt_offsets *p;
8599 struct ipr_interrupts *t;
8600 void __iomem *base;
8601
8602 ioa_cfg->host = host;
8603 ioa_cfg->pdev = pdev;
8604 ioa_cfg->log_level = ipr_log_level;
3d1d0da6 8605 ioa_cfg->doorbell = IPR_DOORBELL;
1da177e4
LT
8606 sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
8607 sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
8608 sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
8609 sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
8610 sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
8611 sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
8612 sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
8613 sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
8614
8615 INIT_LIST_HEAD(&ioa_cfg->free_q);
8616 INIT_LIST_HEAD(&ioa_cfg->pending_q);
8617 INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
8618 INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
8619 INIT_LIST_HEAD(&ioa_cfg->free_res_q);
8620 INIT_LIST_HEAD(&ioa_cfg->used_res_q);
c4028958 8621 INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
1da177e4 8622 init_waitqueue_head(&ioa_cfg->reset_wait_q);
95fecd90 8623 init_waitqueue_head(&ioa_cfg->msi_wait_q);
1da177e4
LT
8624 ioa_cfg->sdt_state = INACTIVE;
8625
8626 ipr_initialize_bus_attr(ioa_cfg);
3e7ebdfa 8627 ioa_cfg->max_devs_supported = ipr_max_devs;
1da177e4 8628
3e7ebdfa
WB
8629 if (ioa_cfg->sis64) {
8630 host->max_id = IPR_MAX_SIS64_TARGETS_PER_BUS;
8631 host->max_lun = IPR_MAX_SIS64_LUNS_PER_TARGET;
8632 if (ipr_max_devs > IPR_MAX_SIS64_DEVS)
8633 ioa_cfg->max_devs_supported = IPR_MAX_SIS64_DEVS;
8634 } else {
8635 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
8636 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
8637 if (ipr_max_devs > IPR_MAX_PHYSICAL_DEVS)
8638 ioa_cfg->max_devs_supported = IPR_MAX_PHYSICAL_DEVS;
8639 }
1da177e4
LT
8640 host->max_channel = IPR_MAX_BUS_TO_SCAN;
8641 host->unique_id = host->host_no;
8642 host->max_cmd_len = IPR_MAX_CDB_LEN;
89aad428 8643 host->can_queue = ioa_cfg->max_cmds;
1da177e4
LT
8644 pci_set_drvdata(pdev, ioa_cfg);
8645
8646 p = &ioa_cfg->chip_cfg->regs;
8647 t = &ioa_cfg->regs;
8648 base = ioa_cfg->hdw_dma_regs;
8649
8650 t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
8651 t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
214777ba 8652 t->clr_interrupt_mask_reg32 = base + p->clr_interrupt_mask_reg32;
1da177e4 8653 t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
214777ba 8654 t->sense_interrupt_mask_reg32 = base + p->sense_interrupt_mask_reg32;
1da177e4 8655 t->clr_interrupt_reg = base + p->clr_interrupt_reg;
214777ba 8656 t->clr_interrupt_reg32 = base + p->clr_interrupt_reg32;
1da177e4 8657 t->sense_interrupt_reg = base + p->sense_interrupt_reg;
214777ba 8658 t->sense_interrupt_reg32 = base + p->sense_interrupt_reg32;
1da177e4
LT
8659 t->ioarrin_reg = base + p->ioarrin_reg;
8660 t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
214777ba 8661 t->sense_uproc_interrupt_reg32 = base + p->sense_uproc_interrupt_reg32;
1da177e4 8662 t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
214777ba 8663 t->set_uproc_interrupt_reg32 = base + p->set_uproc_interrupt_reg32;
1da177e4 8664 t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
214777ba 8665 t->clr_uproc_interrupt_reg32 = base + p->clr_uproc_interrupt_reg32;
dcbad00e
WB
8666
8667 if (ioa_cfg->sis64) {
214777ba 8668 t->init_feedback_reg = base + p->init_feedback_reg;
dcbad00e
WB
8669 t->dump_addr_reg = base + p->dump_addr_reg;
8670 t->dump_data_reg = base + p->dump_data_reg;
8701f185 8671 t->endian_swap_reg = base + p->endian_swap_reg;
dcbad00e 8672 }
1da177e4
LT
8673}
8674
8675/**
1be7bd82 8676 * ipr_get_chip_info - Find adapter chip information
1da177e4
LT
8677 * @dev_id: PCI device id struct
8678 *
8679 * Return value:
1be7bd82 8680 * ptr to chip information on success / NULL on failure
1da177e4 8681 **/
1be7bd82
WB
8682static const struct ipr_chip_t * __devinit
8683ipr_get_chip_info(const struct pci_device_id *dev_id)
1da177e4
LT
8684{
8685 int i;
8686
1da177e4
LT
8687 for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
8688 if (ipr_chip[i].vendor == dev_id->vendor &&
8689 ipr_chip[i].device == dev_id->device)
1be7bd82 8690 return &ipr_chip[i];
1da177e4
LT
8691 return NULL;
8692}
8693
95fecd90
WB
8694/**
8695 * ipr_test_intr - Handle the interrupt generated in ipr_test_msi().
8696 * @pdev: PCI device struct
8697 *
8698 * Description: Simply set the msi_received flag to 1 indicating that
8699 * Message Signaled Interrupts are supported.
8700 *
8701 * Return value:
8702 * 0 on success / non-zero on failure
8703 **/
8704static irqreturn_t __devinit ipr_test_intr(int irq, void *devp)
8705{
8706 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
8707 unsigned long lock_flags = 0;
8708 irqreturn_t rc = IRQ_HANDLED;
8709
8710 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8711
8712 ioa_cfg->msi_received = 1;
8713 wake_up(&ioa_cfg->msi_wait_q);
8714
8715 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8716 return rc;
8717}
8718
8719/**
8720 * ipr_test_msi - Test for Message Signaled Interrupt (MSI) support.
8721 * @pdev: PCI device struct
8722 *
8723 * Description: The return value from pci_enable_msi() can not always be
8724 * trusted. This routine sets up and initiates a test interrupt to determine
8725 * if the interrupt is received via the ipr_test_intr() service routine.
8726 * If the tests fails, the driver will fall back to LSI.
8727 *
8728 * Return value:
8729 * 0 on success / non-zero on failure
8730 **/
8731static int __devinit ipr_test_msi(struct ipr_ioa_cfg *ioa_cfg,
8732 struct pci_dev *pdev)
8733{
8734 int rc;
8735 volatile u32 int_reg;
8736 unsigned long lock_flags = 0;
8737
8738 ENTER;
8739
8740 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8741 init_waitqueue_head(&ioa_cfg->msi_wait_q);
8742 ioa_cfg->msi_received = 0;
8743 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
214777ba 8744 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.clr_interrupt_mask_reg32);
95fecd90
WB
8745 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
8746 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8747
8748 rc = request_irq(pdev->irq, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
8749 if (rc) {
8750 dev_err(&pdev->dev, "Can not assign irq %d\n", pdev->irq);
8751 return rc;
8752 } else if (ipr_debug)
8753 dev_info(&pdev->dev, "IRQ assigned: %d\n", pdev->irq);
8754
214777ba 8755 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.sense_interrupt_reg32);
95fecd90
WB
8756 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
8757 wait_event_timeout(ioa_cfg->msi_wait_q, ioa_cfg->msi_received, HZ);
8758 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
8759
8760 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
8761 if (!ioa_cfg->msi_received) {
8762 /* MSI test failed */
8763 dev_info(&pdev->dev, "MSI test failed. Falling back to LSI.\n");
8764 rc = -EOPNOTSUPP;
8765 } else if (ipr_debug)
8766 dev_info(&pdev->dev, "MSI test succeeded.\n");
8767
8768 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
8769
8770 free_irq(pdev->irq, ioa_cfg);
8771
8772 LEAVE;
8773
8774 return rc;
8775}
8776
1da177e4
LT
8777/**
8778 * ipr_probe_ioa - Allocates memory and does first stage of initialization
8779 * @pdev: PCI device struct
8780 * @dev_id: PCI device id struct
8781 *
8782 * Return value:
8783 * 0 on success / non-zero on failure
8784 **/
8785static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
8786 const struct pci_device_id *dev_id)
8787{
8788 struct ipr_ioa_cfg *ioa_cfg;
8789 struct Scsi_Host *host;
8790 unsigned long ipr_regs_pci;
8791 void __iomem *ipr_regs;
a2a65a3e 8792 int rc = PCIBIOS_SUCCESSFUL;
473b1e8e 8793 volatile u32 mask, uproc, interrupts;
1da177e4
LT
8794
8795 ENTER;
8796
8797 if ((rc = pci_enable_device(pdev))) {
8798 dev_err(&pdev->dev, "Cannot enable adapter\n");
8799 goto out;
8800 }
8801
8802 dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
8803
8804 host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
8805
8806 if (!host) {
8807 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
8808 rc = -ENOMEM;
8809 goto out_disable;
8810 }
8811
8812 ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
8813 memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
8d8e7d13 8814 ata_host_init(&ioa_cfg->ata_host, &pdev->dev, &ipr_sata_ops);
1da177e4 8815
1be7bd82 8816 ioa_cfg->ipr_chip = ipr_get_chip_info(dev_id);
1da177e4 8817
1be7bd82 8818 if (!ioa_cfg->ipr_chip) {
1da177e4
LT
8819 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
8820 dev_id->vendor, dev_id->device);
8821 goto out_scsi_host_put;
8822 }
8823
a32c055f
WB
8824 /* set SIS 32 or SIS 64 */
8825 ioa_cfg->sis64 = ioa_cfg->ipr_chip->sis_type == IPR_SIS64 ? 1 : 0;
1be7bd82 8826 ioa_cfg->chip_cfg = ioa_cfg->ipr_chip->cfg;
7dd21308 8827 ioa_cfg->clear_isr = ioa_cfg->chip_cfg->clear_isr;
89aad428 8828 ioa_cfg->max_cmds = ioa_cfg->chip_cfg->max_cmds;
1be7bd82 8829
5469cb5b
BK
8830 if (ipr_transop_timeout)
8831 ioa_cfg->transop_timeout = ipr_transop_timeout;
8832 else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
8833 ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
8834 else
8835 ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
8836
44c10138 8837 ioa_cfg->revid = pdev->revision;
463fc696 8838
1da177e4
LT
8839 ipr_regs_pci = pci_resource_start(pdev, 0);
8840
8841 rc = pci_request_regions(pdev, IPR_NAME);
8842 if (rc < 0) {
8843 dev_err(&pdev->dev,
8844 "Couldn't register memory range of registers\n");
8845 goto out_scsi_host_put;
8846 }
8847
25729a7f 8848 ipr_regs = pci_ioremap_bar(pdev, 0);
1da177e4
LT
8849
8850 if (!ipr_regs) {
8851 dev_err(&pdev->dev,
8852 "Couldn't map memory range of registers\n");
8853 rc = -ENOMEM;
8854 goto out_release_regions;
8855 }
8856
8857 ioa_cfg->hdw_dma_regs = ipr_regs;
8858 ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
8859 ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
8860
8861 ipr_init_ioa_cfg(ioa_cfg, host, pdev);
8862
8863 pci_set_master(pdev);
8864
a32c055f
WB
8865 if (ioa_cfg->sis64) {
8866 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
8867 if (rc < 0) {
8868 dev_dbg(&pdev->dev, "Failed to set 64 bit PCI DMA mask\n");
8869 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8870 }
8871
8872 } else
8873 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8874
1da177e4
LT
8875 if (rc < 0) {
8876 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
8877 goto cleanup_nomem;
8878 }
8879
8880 rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
8881 ioa_cfg->chip_cfg->cache_line_size);
8882
8883 if (rc != PCIBIOS_SUCCESSFUL) {
8884 dev_err(&pdev->dev, "Write of cache line size failed\n");
8885 rc = -EIO;
8886 goto cleanup_nomem;
8887 }
8888
95fecd90 8889 /* Enable MSI style interrupts if they are supported. */
1be7bd82 8890 if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI && !pci_enable_msi(pdev)) {
95fecd90
WB
8891 rc = ipr_test_msi(ioa_cfg, pdev);
8892 if (rc == -EOPNOTSUPP)
8893 pci_disable_msi(pdev);
8894 else if (rc)
8895 goto out_msi_disable;
8896 else
8897 dev_info(&pdev->dev, "MSI enabled with IRQ: %d\n", pdev->irq);
8898 } else if (ipr_debug)
8899 dev_info(&pdev->dev, "Cannot enable MSI.\n");
8900
1da177e4
LT
8901 /* Save away PCI config space for use following IOA reset */
8902 rc = pci_save_state(pdev);
8903
8904 if (rc != PCIBIOS_SUCCESSFUL) {
8905 dev_err(&pdev->dev, "Failed to save PCI config space\n");
8906 rc = -EIO;
f170c684 8907 goto out_msi_disable;
1da177e4
LT
8908 }
8909
8910 if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
f170c684 8911 goto out_msi_disable;
1da177e4
LT
8912
8913 if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
f170c684 8914 goto out_msi_disable;
1da177e4 8915
3e7ebdfa
WB
8916 if (ioa_cfg->sis64)
8917 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr64)
8918 + ((sizeof(struct ipr_config_table_entry64)
8919 * ioa_cfg->max_devs_supported)));
8920 else
8921 ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr)
8922 + ((sizeof(struct ipr_config_table_entry)
8923 * ioa_cfg->max_devs_supported)));
8924
1da177e4
LT
8925 rc = ipr_alloc_mem(ioa_cfg);
8926 if (rc < 0) {
8927 dev_err(&pdev->dev,
8928 "Couldn't allocate enough memory for device driver!\n");
f170c684 8929 goto out_msi_disable;
1da177e4
LT
8930 }
8931
ce155cce 8932 /*
8933 * If HRRQ updated interrupt is not masked, or reset alert is set,
8934 * the card is in an unknown state and needs a hard reset
8935 */
214777ba
WB
8936 mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
8937 interrupts = readl(ioa_cfg->regs.sense_interrupt_reg32);
8938 uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
ce155cce 8939 if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
8940 ioa_cfg->needs_hard_reset = 1;
5d7c20b7 8941 if ((interrupts & IPR_PCII_ERROR_INTERRUPTS) || reset_devices)
473b1e8e
BK
8942 ioa_cfg->needs_hard_reset = 1;
8943 if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
8944 ioa_cfg->ioa_unit_checked = 1;
ce155cce 8945
1da177e4 8946 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
95fecd90
WB
8947 rc = request_irq(pdev->irq, ipr_isr,
8948 ioa_cfg->msi_received ? 0 : IRQF_SHARED,
8949 IPR_NAME, ioa_cfg);
1da177e4
LT
8950
8951 if (rc) {
8952 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
8953 pdev->irq, rc);
8954 goto cleanup_nolog;
8955 }
8956
463fc696
BK
8957 if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
8958 (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
8959 ioa_cfg->needs_warm_reset = 1;
8960 ioa_cfg->reset = ipr_reset_slot_reset;
8961 } else
8962 ioa_cfg->reset = ipr_reset_start_bist;
8963
1da177e4
LT
8964 spin_lock(&ipr_driver_lock);
8965 list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
8966 spin_unlock(&ipr_driver_lock);
8967
8968 LEAVE;
8969out:
8970 return rc;
8971
8972cleanup_nolog:
8973 ipr_free_mem(ioa_cfg);
95fecd90
WB
8974out_msi_disable:
8975 pci_disable_msi(pdev);
f170c684
JL
8976cleanup_nomem:
8977 iounmap(ipr_regs);
1da177e4
LT
8978out_release_regions:
8979 pci_release_regions(pdev);
8980out_scsi_host_put:
8981 scsi_host_put(host);
8982out_disable:
8983 pci_disable_device(pdev);
8984 goto out;
8985}
8986
8987/**
8988 * ipr_scan_vsets - Scans for VSET devices
8989 * @ioa_cfg: ioa config struct
8990 *
8991 * Description: Since the VSET resources do not follow SAM in that we can have
8992 * sparse LUNs with no LUN 0, we have to scan for these ourselves.
8993 *
8994 * Return value:
8995 * none
8996 **/
8997static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
8998{
8999 int target, lun;
9000
9001 for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
9002 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
9003 scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
9004}
9005
9006/**
9007 * ipr_initiate_ioa_bringdown - Bring down an adapter
9008 * @ioa_cfg: ioa config struct
9009 * @shutdown_type: shutdown type
9010 *
9011 * Description: This function will initiate bringing down the adapter.
9012 * This consists of issuing an IOA shutdown to the adapter
9013 * to flush the cache, and running BIST.
9014 * If the caller needs to wait on the completion of the reset,
9015 * the caller must sleep on the reset_wait_q.
9016 *
9017 * Return value:
9018 * none
9019 **/
9020static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
9021 enum ipr_shutdown_type shutdown_type)
9022{
9023 ENTER;
9024 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
9025 ioa_cfg->sdt_state = ABORT_DUMP;
9026 ioa_cfg->reset_retries = 0;
9027 ioa_cfg->in_ioa_bringdown = 1;
9028 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
9029 LEAVE;
9030}
9031
9032/**
9033 * __ipr_remove - Remove a single adapter
9034 * @pdev: pci device struct
9035 *
9036 * Adapter hot plug remove entry point.
9037 *
9038 * Return value:
9039 * none
9040 **/
9041static void __ipr_remove(struct pci_dev *pdev)
9042{
9043 unsigned long host_lock_flags = 0;
9044 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
9045 ENTER;
9046
9047 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
970ea294
BK
9048 while(ioa_cfg->in_reset_reload) {
9049 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9050 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9051 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
9052 }
9053
1da177e4
LT
9054 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
9055
9056 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9057 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
a684b8da 9058 flush_work_sync(&ioa_cfg->work_q);
1da177e4
LT
9059 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
9060
9061 spin_lock(&ipr_driver_lock);
9062 list_del(&ioa_cfg->queue);
9063 spin_unlock(&ipr_driver_lock);
9064
9065 if (ioa_cfg->sdt_state == ABORT_DUMP)
9066 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
9067 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
9068
9069 ipr_free_all_resources(ioa_cfg);
9070
9071 LEAVE;
9072}
9073
9074/**
9075 * ipr_remove - IOA hot plug remove entry point
9076 * @pdev: pci device struct
9077 *
9078 * Adapter hot plug remove entry point.
9079 *
9080 * Return value:
9081 * none
9082 **/
f381642d 9083static void __devexit ipr_remove(struct pci_dev *pdev)
1da177e4
LT
9084{
9085 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
9086
9087 ENTER;
9088
ee959b00 9089 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
1da177e4 9090 &ipr_trace_attr);
ee959b00 9091 ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
1da177e4
LT
9092 &ipr_dump_attr);
9093 scsi_remove_host(ioa_cfg->host);
9094
9095 __ipr_remove(pdev);
9096
9097 LEAVE;
9098}
9099
9100/**
9101 * ipr_probe - Adapter hot plug add entry point
9102 *
9103 * Return value:
9104 * 0 on success / non-zero on failure
9105 **/
9106static int __devinit ipr_probe(struct pci_dev *pdev,
9107 const struct pci_device_id *dev_id)
9108{
9109 struct ipr_ioa_cfg *ioa_cfg;
9110 int rc;
9111
9112 rc = ipr_probe_ioa(pdev, dev_id);
9113
9114 if (rc)
9115 return rc;
9116
9117 ioa_cfg = pci_get_drvdata(pdev);
9118 rc = ipr_probe_ioa_part2(ioa_cfg);
9119
9120 if (rc) {
9121 __ipr_remove(pdev);
9122 return rc;
9123 }
9124
9125 rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
9126
9127 if (rc) {
9128 __ipr_remove(pdev);
9129 return rc;
9130 }
9131
ee959b00 9132 rc = ipr_create_trace_file(&ioa_cfg->host->shost_dev.kobj,
1da177e4
LT
9133 &ipr_trace_attr);
9134
9135 if (rc) {
9136 scsi_remove_host(ioa_cfg->host);
9137 __ipr_remove(pdev);
9138 return rc;
9139 }
9140
ee959b00 9141 rc = ipr_create_dump_file(&ioa_cfg->host->shost_dev.kobj,
1da177e4
LT
9142 &ipr_dump_attr);
9143
9144 if (rc) {
ee959b00 9145 ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
1da177e4
LT
9146 &ipr_trace_attr);
9147 scsi_remove_host(ioa_cfg->host);
9148 __ipr_remove(pdev);
9149 return rc;
9150 }
9151
9152 scsi_scan_host(ioa_cfg->host);
9153 ipr_scan_vsets(ioa_cfg);
9154 scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
9155 ioa_cfg->allow_ml_add_del = 1;
11cd8f12 9156 ioa_cfg->host->max_channel = IPR_VSET_BUS;
1da177e4
LT
9157 schedule_work(&ioa_cfg->work_q);
9158 return 0;
9159}
9160
9161/**
9162 * ipr_shutdown - Shutdown handler.
d18c3db5 9163 * @pdev: pci device struct
1da177e4
LT
9164 *
9165 * This function is invoked upon system shutdown/reboot. It will issue
9166 * an adapter shutdown to the adapter to flush the write cache.
9167 *
9168 * Return value:
9169 * none
9170 **/
d18c3db5 9171static void ipr_shutdown(struct pci_dev *pdev)
1da177e4 9172{
d18c3db5 9173 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
1da177e4
LT
9174 unsigned long lock_flags = 0;
9175
9176 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
970ea294
BK
9177 while(ioa_cfg->in_reset_reload) {
9178 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9179 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9180 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
9181 }
9182
1da177e4
LT
9183 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
9184 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
9185 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
9186}
9187
9188static struct pci_device_id ipr_pci_table[] __devinitdata = {
9189 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6d84c944 9190 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
1da177e4 9191 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6d84c944 9192 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
1da177e4 9193 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6d84c944 9194 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
1da177e4 9195 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6d84c944 9196 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
1da177e4 9197 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6d84c944 9198 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
1da177e4 9199 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6d84c944 9200 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
1da177e4 9201 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6d84c944 9202 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
86f51436 9203 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
5469cb5b
BK
9204 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
9205 IPR_USE_LONG_TRANSOP_TIMEOUT },
86f51436 9206 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
6d84c944 9207 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
86f51436 9208 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
22d2e402
BK
9209 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
9210 IPR_USE_LONG_TRANSOP_TIMEOUT },
60e7486b 9211 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
5469cb5b
BK
9212 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
9213 IPR_USE_LONG_TRANSOP_TIMEOUT },
86f51436 9214 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
6d84c944 9215 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
86f51436 9216 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
22d2e402
BK
9217 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
9218 IPR_USE_LONG_TRANSOP_TIMEOUT},
60e7486b 9219 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
5469cb5b
BK
9220 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
9221 IPR_USE_LONG_TRANSOP_TIMEOUT },
185eb31c 9222 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
22d2e402
BK
9223 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
9224 IPR_USE_LONG_TRANSOP_TIMEOUT },
185eb31c
BK
9225 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
9226 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
b0f56d3d
WB
9227 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
9228 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CC, 0, 0, 0 },
60e7486b 9229 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
5469cb5b 9230 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
463fc696 9231 IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
1da177e4 9232 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
6d84c944 9233 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
1da177e4 9234 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
6d84c944 9235 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
86f51436 9236 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
5469cb5b
BK
9237 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
9238 IPR_USE_LONG_TRANSOP_TIMEOUT },
60e7486b 9239 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
5469cb5b
BK
9240 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
9241 IPR_USE_LONG_TRANSOP_TIMEOUT },
d7b4627f
WB
9242 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9243 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B5, 0, 0, 0 },
9244 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9245 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0, 0 },
9246 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9247 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B2, 0, 0, 0 },
5a918353
WB
9248 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9249 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C3, 0, 0, 0 },
32622bde
WB
9250 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
9251 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C4, 0, 0, 0 },
cd9b3d04 9252 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
d7b4627f 9253 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B4, 0, 0, 0 },
cd9b3d04 9254 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
d7b4627f 9255 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B1, 0, 0, 0 },
cd9b3d04 9256 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
d7b4627f 9257 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C6, 0, 0, 0 },
cd9b3d04
WB
9258 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
9259 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C8, 0, 0, 0 },
9260 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROCODILE,
d7b4627f 9261 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CE, 0, 0, 0 },
1da177e4
LT
9262 { }
9263};
9264MODULE_DEVICE_TABLE(pci, ipr_pci_table);
9265
f8a88b19
LV
9266static struct pci_error_handlers ipr_err_handler = {
9267 .error_detected = ipr_pci_error_detected,
9268 .slot_reset = ipr_pci_slot_reset,
9269};
9270
1da177e4
LT
9271static struct pci_driver ipr_driver = {
9272 .name = IPR_NAME,
9273 .id_table = ipr_pci_table,
9274 .probe = ipr_probe,
f381642d 9275 .remove = __devexit_p(ipr_remove),
d18c3db5 9276 .shutdown = ipr_shutdown,
f8a88b19 9277 .err_handler = &ipr_err_handler,
1da177e4
LT
9278};
9279
f72919ec
WB
9280/**
9281 * ipr_halt_done - Shutdown prepare completion
9282 *
9283 * Return value:
9284 * none
9285 **/
9286static void ipr_halt_done(struct ipr_cmnd *ipr_cmd)
9287{
9288 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
9289
9290 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
9291}
9292
9293/**
9294 * ipr_halt - Issue shutdown prepare to all adapters
9295 *
9296 * Return value:
9297 * NOTIFY_OK on success / NOTIFY_DONE on failure
9298 **/
9299static int ipr_halt(struct notifier_block *nb, ulong event, void *buf)
9300{
9301 struct ipr_cmnd *ipr_cmd;
9302 struct ipr_ioa_cfg *ioa_cfg;
9303 unsigned long flags = 0;
9304
9305 if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
9306 return NOTIFY_DONE;
9307
9308 spin_lock(&ipr_driver_lock);
9309
9310 list_for_each_entry(ioa_cfg, &ipr_ioa_head, queue) {
9311 spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
9312 if (!ioa_cfg->allow_cmds) {
9313 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
9314 continue;
9315 }
9316
9317 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
9318 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
9319 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
9320 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
9321 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
9322
9323 ipr_do_req(ipr_cmd, ipr_halt_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
9324 spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
9325 }
9326 spin_unlock(&ipr_driver_lock);
9327
9328 return NOTIFY_OK;
9329}
9330
9331static struct notifier_block ipr_notifier = {
9332 ipr_halt, NULL, 0
9333};
9334
1da177e4
LT
9335/**
9336 * ipr_init - Module entry point
9337 *
9338 * Return value:
9339 * 0 on success / negative value on failure
9340 **/
9341static int __init ipr_init(void)
9342{
9343 ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
9344 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
9345
f72919ec 9346 register_reboot_notifier(&ipr_notifier);
dcbccbde 9347 return pci_register_driver(&ipr_driver);
1da177e4
LT
9348}
9349
9350/**
9351 * ipr_exit - Module unload
9352 *
9353 * Module unload entry point.
9354 *
9355 * Return value:
9356 * none
9357 **/
9358static void __exit ipr_exit(void)
9359{
f72919ec 9360 unregister_reboot_notifier(&ipr_notifier);
1da177e4
LT
9361 pci_unregister_driver(&ipr_driver);
9362}
9363
9364module_init(ipr_init);
9365module_exit(ipr_exit);
This page took 1.218985 seconds and 5 git commands to generate.