[SCSI] ipr: Handle device autosense
[GitHub/mt8127/android_kernel_alcatel_ttab.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
57#include <linux/config.h>
58#include <linux/fs.h>
59#include <linux/init.h>
60#include <linux/types.h>
61#include <linux/errno.h>
62#include <linux/kernel.h>
63#include <linux/ioport.h>
64#include <linux/delay.h>
65#include <linux/pci.h>
66#include <linux/wait.h>
67#include <linux/spinlock.h>
68#include <linux/sched.h>
69#include <linux/interrupt.h>
70#include <linux/blkdev.h>
71#include <linux/firmware.h>
72#include <linux/module.h>
73#include <linux/moduleparam.h>
74#include <asm/io.h>
75#include <asm/irq.h>
76#include <asm/processor.h>
77#include <scsi/scsi.h>
78#include <scsi/scsi_host.h>
79#include <scsi/scsi_tcq.h>
80#include <scsi/scsi_eh.h>
81#include <scsi/scsi_cmnd.h>
82#include <scsi/scsi_request.h>
83#include "ipr.h"
84
85/*
86 * Global Data
87 */
88static struct list_head ipr_ioa_head = LIST_HEAD_INIT(ipr_ioa_head);
89static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
90static unsigned int ipr_max_speed = 1;
91static int ipr_testmode = 0;
92static unsigned int ipr_fastfail = 0;
93static unsigned int ipr_transop_timeout = IPR_OPERATIONAL_TIMEOUT;
62275040 94static unsigned int ipr_enable_cache = 1;
d3c74871 95static unsigned int ipr_debug = 0;
32d29776 96static int ipr_auto_create = 1;
1da177e4
LT
97static DEFINE_SPINLOCK(ipr_driver_lock);
98
99/* This table describes the differences between DMA controller chips */
100static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
101 { /* Gemstone and Citrine */
102 .mailbox = 0x0042C,
103 .cache_line_size = 0x20,
104 {
105 .set_interrupt_mask_reg = 0x0022C,
106 .clr_interrupt_mask_reg = 0x00230,
107 .sense_interrupt_mask_reg = 0x0022C,
108 .clr_interrupt_reg = 0x00228,
109 .sense_interrupt_reg = 0x00224,
110 .ioarrin_reg = 0x00404,
111 .sense_uproc_interrupt_reg = 0x00214,
112 .set_uproc_interrupt_reg = 0x00214,
113 .clr_uproc_interrupt_reg = 0x00218
114 }
115 },
116 { /* Snipe and Scamp */
117 .mailbox = 0x0052C,
118 .cache_line_size = 0x20,
119 {
120 .set_interrupt_mask_reg = 0x00288,
121 .clr_interrupt_mask_reg = 0x0028C,
122 .sense_interrupt_mask_reg = 0x00288,
123 .clr_interrupt_reg = 0x00284,
124 .sense_interrupt_reg = 0x00280,
125 .ioarrin_reg = 0x00504,
126 .sense_uproc_interrupt_reg = 0x00290,
127 .set_uproc_interrupt_reg = 0x00290,
128 .clr_uproc_interrupt_reg = 0x00294
129 }
130 },
131};
132
133static const struct ipr_chip_t ipr_chip[] = {
134 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, &ipr_chip_cfg[0] },
135 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, &ipr_chip_cfg[0] },
136 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, &ipr_chip_cfg[1] },
137 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, &ipr_chip_cfg[1] }
138};
139
140static int ipr_max_bus_speeds [] = {
141 IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
142};
143
144MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
145MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
146module_param_named(max_speed, ipr_max_speed, uint, 0);
147MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
148module_param_named(log_level, ipr_log_level, uint, 0);
149MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
150module_param_named(testmode, ipr_testmode, int, 0);
151MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
152module_param_named(fastfail, ipr_fastfail, int, 0);
153MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
154module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
155MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
62275040
BK
156module_param_named(enable_cache, ipr_enable_cache, int, 0);
157MODULE_PARM_DESC(enable_cache, "Enable adapter's non-volatile write cache (default: 1)");
d3c74871
BK
158module_param_named(debug, ipr_debug, int, 0);
159MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
32d29776
BK
160module_param_named(auto_create, ipr_auto_create, int, 0);
161MODULE_PARM_DESC(auto_create, "Auto-create single device RAID 0 arrays when initialized (default: 1)");
1da177e4
LT
162MODULE_LICENSE("GPL");
163MODULE_VERSION(IPR_DRIVER_VERSION);
164
165static const char *ipr_gpdd_dev_end_states[] = {
166 "Command complete",
167 "Terminated by host",
168 "Terminated by device reset",
169 "Terminated by bus reset",
170 "Unknown",
171 "Command not started"
172};
173
174static const char *ipr_gpdd_dev_bus_phases[] = {
175 "Bus free",
176 "Arbitration",
177 "Selection",
178 "Message out",
179 "Command",
180 "Message in",
181 "Data out",
182 "Data in",
183 "Status",
184 "Reselection",
185 "Unknown"
186};
187
188/* A constant array of IOASCs/URCs/Error Messages */
189static const
190struct ipr_error_table_t ipr_error_table[] = {
191 {0x00000000, 1, 1,
192 "8155: An unknown error was received"},
193 {0x00330000, 0, 0,
194 "Soft underlength error"},
195 {0x005A0000, 0, 0,
196 "Command to be cancelled not found"},
197 {0x00808000, 0, 0,
198 "Qualified success"},
199 {0x01080000, 1, 1,
200 "FFFE: Soft device bus error recovered by the IOA"},
201 {0x01170600, 0, 1,
202 "FFF9: Device sector reassign successful"},
203 {0x01170900, 0, 1,
204 "FFF7: Media error recovered by device rewrite procedures"},
205 {0x01180200, 0, 1,
206 "7001: IOA sector reassignment successful"},
207 {0x01180500, 0, 1,
208 "FFF9: Soft media error. Sector reassignment recommended"},
209 {0x01180600, 0, 1,
210 "FFF7: Media error recovered by IOA rewrite procedures"},
211 {0x01418000, 0, 1,
212 "FF3D: Soft PCI bus error recovered by the IOA"},
213 {0x01440000, 1, 1,
214 "FFF6: Device hardware error recovered by the IOA"},
215 {0x01448100, 0, 1,
216 "FFF6: Device hardware error recovered by the device"},
217 {0x01448200, 1, 1,
218 "FF3D: Soft IOA error recovered by the IOA"},
219 {0x01448300, 0, 1,
220 "FFFA: Undefined device response recovered by the IOA"},
221 {0x014A0000, 1, 1,
222 "FFF6: Device bus error, message or command phase"},
223 {0x015D0000, 0, 1,
224 "FFF6: Failure prediction threshold exceeded"},
225 {0x015D9200, 0, 1,
226 "8009: Impending cache battery pack failure"},
227 {0x02040400, 0, 0,
228 "34FF: Disk device format in progress"},
229 {0x023F0000, 0, 0,
230 "Synchronization required"},
231 {0x024E0000, 0, 0,
232 "No ready, IOA shutdown"},
233 {0x025A0000, 0, 0,
234 "Not ready, IOA has been shutdown"},
235 {0x02670100, 0, 1,
236 "3020: Storage subsystem configuration error"},
237 {0x03110B00, 0, 0,
238 "FFF5: Medium error, data unreadable, recommend reassign"},
239 {0x03110C00, 0, 0,
240 "7000: Medium error, data unreadable, do not reassign"},
241 {0x03310000, 0, 1,
242 "FFF3: Disk media format bad"},
243 {0x04050000, 0, 1,
244 "3002: Addressed device failed to respond to selection"},
245 {0x04080000, 1, 1,
246 "3100: Device bus error"},
247 {0x04080100, 0, 1,
248 "3109: IOA timed out a device command"},
249 {0x04088000, 0, 0,
250 "3120: SCSI bus is not operational"},
251 {0x04118000, 0, 1,
252 "9000: IOA reserved area data check"},
253 {0x04118100, 0, 1,
254 "9001: IOA reserved area invalid data pattern"},
255 {0x04118200, 0, 1,
256 "9002: IOA reserved area LRC error"},
257 {0x04320000, 0, 1,
258 "102E: Out of alternate sectors for disk storage"},
259 {0x04330000, 1, 1,
260 "FFF4: Data transfer underlength error"},
261 {0x04338000, 1, 1,
262 "FFF4: Data transfer overlength error"},
263 {0x043E0100, 0, 1,
264 "3400: Logical unit failure"},
265 {0x04408500, 0, 1,
266 "FFF4: Device microcode is corrupt"},
267 {0x04418000, 1, 1,
268 "8150: PCI bus error"},
269 {0x04430000, 1, 0,
270 "Unsupported device bus message received"},
271 {0x04440000, 1, 1,
272 "FFF4: Disk device problem"},
273 {0x04448200, 1, 1,
274 "8150: Permanent IOA failure"},
275 {0x04448300, 0, 1,
276 "3010: Disk device returned wrong response to IOA"},
277 {0x04448400, 0, 1,
278 "8151: IOA microcode error"},
279 {0x04448500, 0, 0,
280 "Device bus status error"},
281 {0x04448600, 0, 1,
282 "8157: IOA error requiring IOA reset to recover"},
283 {0x04490000, 0, 0,
284 "Message reject received from the device"},
285 {0x04449200, 0, 1,
286 "8008: A permanent cache battery pack failure occurred"},
287 {0x0444A000, 0, 1,
288 "9090: Disk unit has been modified after the last known status"},
289 {0x0444A200, 0, 1,
290 "9081: IOA detected device error"},
291 {0x0444A300, 0, 1,
292 "9082: IOA detected device error"},
293 {0x044A0000, 1, 1,
294 "3110: Device bus error, message or command phase"},
295 {0x04670400, 0, 1,
296 "9091: Incorrect hardware configuration change has been detected"},
b0df54bb
BK
297 {0x04678000, 0, 1,
298 "9073: Invalid multi-adapter configuration"},
1da177e4
LT
299 {0x046E0000, 0, 1,
300 "FFF4: Command to logical unit failed"},
301 {0x05240000, 1, 0,
302 "Illegal request, invalid request type or request packet"},
303 {0x05250000, 0, 0,
304 "Illegal request, invalid resource handle"},
b0df54bb
BK
305 {0x05258000, 0, 0,
306 "Illegal request, commands not allowed to this device"},
307 {0x05258100, 0, 0,
308 "Illegal request, command not allowed to a secondary adapter"},
1da177e4
LT
309 {0x05260000, 0, 0,
310 "Illegal request, invalid field in parameter list"},
311 {0x05260100, 0, 0,
312 "Illegal request, parameter not supported"},
313 {0x05260200, 0, 0,
314 "Illegal request, parameter value invalid"},
315 {0x052C0000, 0, 0,
316 "Illegal request, command sequence error"},
b0df54bb
BK
317 {0x052C8000, 1, 0,
318 "Illegal request, dual adapter support not enabled"},
1da177e4
LT
319 {0x06040500, 0, 1,
320 "9031: Array protection temporarily suspended, protection resuming"},
321 {0x06040600, 0, 1,
322 "9040: Array protection temporarily suspended, protection resuming"},
323 {0x06290000, 0, 1,
324 "FFFB: SCSI bus was reset"},
325 {0x06290500, 0, 0,
326 "FFFE: SCSI bus transition to single ended"},
327 {0x06290600, 0, 0,
328 "FFFE: SCSI bus transition to LVD"},
329 {0x06298000, 0, 1,
330 "FFFB: SCSI bus was reset by another initiator"},
331 {0x063F0300, 0, 1,
332 "3029: A device replacement has occurred"},
333 {0x064C8000, 0, 1,
334 "9051: IOA cache data exists for a missing or failed device"},
b0df54bb
BK
335 {0x064C8100, 0, 1,
336 "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
1da177e4
LT
337 {0x06670100, 0, 1,
338 "9025: Disk unit is not supported at its physical location"},
339 {0x06670600, 0, 1,
340 "3020: IOA detected a SCSI bus configuration error"},
341 {0x06678000, 0, 1,
342 "3150: SCSI bus configuration error"},
b0df54bb
BK
343 {0x06678100, 0, 1,
344 "9074: Asymmetric advanced function disk configuration"},
1da177e4
LT
345 {0x06690200, 0, 1,
346 "9041: Array protection temporarily suspended"},
347 {0x06698200, 0, 1,
348 "9042: Corrupt array parity detected on specified device"},
349 {0x066B0200, 0, 1,
350 "9030: Array no longer protected due to missing or failed disk unit"},
b0df54bb
BK
351 {0x066B8000, 0, 1,
352 "9071: Link operational transition"},
353 {0x066B8100, 0, 1,
354 "9072: Link not operational transition"},
1da177e4
LT
355 {0x066B8200, 0, 1,
356 "9032: Array exposed but still protected"},
357 {0x07270000, 0, 0,
358 "Failure due to other device"},
359 {0x07278000, 0, 1,
360 "9008: IOA does not support functions expected by devices"},
361 {0x07278100, 0, 1,
362 "9010: Cache data associated with attached devices cannot be found"},
363 {0x07278200, 0, 1,
364 "9011: Cache data belongs to devices other than those attached"},
365 {0x07278400, 0, 1,
366 "9020: Array missing 2 or more devices with only 1 device present"},
367 {0x07278500, 0, 1,
368 "9021: Array missing 2 or more devices with 2 or more devices present"},
369 {0x07278600, 0, 1,
370 "9022: Exposed array is missing a required device"},
371 {0x07278700, 0, 1,
372 "9023: Array member(s) not at required physical locations"},
373 {0x07278800, 0, 1,
374 "9024: Array not functional due to present hardware configuration"},
375 {0x07278900, 0, 1,
376 "9026: Array not functional due to present hardware configuration"},
377 {0x07278A00, 0, 1,
378 "9027: Array is missing a device and parity is out of sync"},
379 {0x07278B00, 0, 1,
380 "9028: Maximum number of arrays already exist"},
381 {0x07278C00, 0, 1,
382 "9050: Required cache data cannot be located for a disk unit"},
383 {0x07278D00, 0, 1,
384 "9052: Cache data exists for a device that has been modified"},
385 {0x07278F00, 0, 1,
386 "9054: IOA resources not available due to previous problems"},
387 {0x07279100, 0, 1,
388 "9092: Disk unit requires initialization before use"},
389 {0x07279200, 0, 1,
390 "9029: Incorrect hardware configuration change has been detected"},
391 {0x07279600, 0, 1,
392 "9060: One or more disk pairs are missing from an array"},
393 {0x07279700, 0, 1,
394 "9061: One or more disks are missing from an array"},
395 {0x07279800, 0, 1,
396 "9062: One or more disks are missing from an array"},
397 {0x07279900, 0, 1,
398 "9063: Maximum number of functional arrays has been exceeded"},
399 {0x0B260000, 0, 0,
400 "Aborted command, invalid descriptor"},
401 {0x0B5A0000, 0, 0,
402 "Command terminated by host"}
403};
404
405static const struct ipr_ses_table_entry ipr_ses_table[] = {
406 { "2104-DL1 ", "XXXXXXXXXXXXXXXX", 80 },
407 { "2104-TL1 ", "XXXXXXXXXXXXXXXX", 80 },
408 { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
409 { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
410 { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
411 { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
412 { "2104-DU3 ", "XXXXXXXXXXXXXXXX", 160 },
413 { "2104-TU3 ", "XXXXXXXXXXXXXXXX", 160 },
414 { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
415 { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
416 { "St V1S2 ", "XXXXXXXXXXXXXXXX", 160 },
417 { "HSBPD4M PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
418 { "VSBPD1H U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
419};
420
421/*
422 * Function Prototypes
423 */
424static int ipr_reset_alert(struct ipr_cmnd *);
425static void ipr_process_ccn(struct ipr_cmnd *);
426static void ipr_process_error(struct ipr_cmnd *);
427static void ipr_reset_ioa_job(struct ipr_cmnd *);
428static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
429 enum ipr_shutdown_type);
430
431#ifdef CONFIG_SCSI_IPR_TRACE
432/**
433 * ipr_trc_hook - Add a trace entry to the driver trace
434 * @ipr_cmd: ipr command struct
435 * @type: trace type
436 * @add_data: additional data
437 *
438 * Return value:
439 * none
440 **/
441static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
442 u8 type, u32 add_data)
443{
444 struct ipr_trace_entry *trace_entry;
445 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
446
447 trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
448 trace_entry->time = jiffies;
449 trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
450 trace_entry->type = type;
451 trace_entry->cmd_index = ipr_cmd->cmd_index;
452 trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
453 trace_entry->u.add_data = add_data;
454}
455#else
456#define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
457#endif
458
459/**
460 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
461 * @ipr_cmd: ipr command struct
462 *
463 * Return value:
464 * none
465 **/
466static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
467{
468 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
469 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
470
471 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
472 ioarcb->write_data_transfer_length = 0;
473 ioarcb->read_data_transfer_length = 0;
474 ioarcb->write_ioadl_len = 0;
475 ioarcb->read_ioadl_len = 0;
476 ioasa->ioasc = 0;
477 ioasa->residual_data_len = 0;
478
479 ipr_cmd->scsi_cmd = NULL;
480 ipr_cmd->sense_buffer[0] = 0;
481 ipr_cmd->dma_use_sg = 0;
482}
483
484/**
485 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
486 * @ipr_cmd: ipr command struct
487 *
488 * Return value:
489 * none
490 **/
491static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
492{
493 ipr_reinit_ipr_cmnd(ipr_cmd);
494 ipr_cmd->u.scratch = 0;
495 ipr_cmd->sibling = NULL;
496 init_timer(&ipr_cmd->timer);
497}
498
499/**
500 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
501 * @ioa_cfg: ioa config struct
502 *
503 * Return value:
504 * pointer to ipr command struct
505 **/
506static
507struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
508{
509 struct ipr_cmnd *ipr_cmd;
510
511 ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
512 list_del(&ipr_cmd->queue);
513 ipr_init_ipr_cmnd(ipr_cmd);
514
515 return ipr_cmd;
516}
517
518/**
519 * ipr_unmap_sglist - Unmap scatterlist if mapped
520 * @ioa_cfg: ioa config struct
521 * @ipr_cmd: ipr command struct
522 *
523 * Return value:
524 * nothing
525 **/
526static void ipr_unmap_sglist(struct ipr_ioa_cfg *ioa_cfg,
527 struct ipr_cmnd *ipr_cmd)
528{
529 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
530
531 if (ipr_cmd->dma_use_sg) {
532 if (scsi_cmd->use_sg > 0) {
533 pci_unmap_sg(ioa_cfg->pdev, scsi_cmd->request_buffer,
534 scsi_cmd->use_sg,
535 scsi_cmd->sc_data_direction);
536 } else {
537 pci_unmap_single(ioa_cfg->pdev, ipr_cmd->dma_handle,
538 scsi_cmd->request_bufflen,
539 scsi_cmd->sc_data_direction);
540 }
541 }
542}
543
544/**
545 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
546 * @ioa_cfg: ioa config struct
547 * @clr_ints: interrupts to clear
548 *
549 * This function masks all interrupts on the adapter, then clears the
550 * interrupts specified in the mask
551 *
552 * Return value:
553 * none
554 **/
555static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
556 u32 clr_ints)
557{
558 volatile u32 int_reg;
559
560 /* Stop new interrupts */
561 ioa_cfg->allow_interrupts = 0;
562
563 /* Set interrupt mask to stop all new interrupts */
564 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
565
566 /* Clear any pending interrupts */
567 writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
568 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
569}
570
571/**
572 * ipr_save_pcix_cmd_reg - Save PCI-X command register
573 * @ioa_cfg: ioa config struct
574 *
575 * Return value:
576 * 0 on success / -EIO on failure
577 **/
578static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
579{
580 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
581
582 if (pcix_cmd_reg == 0) {
583 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
584 return -EIO;
585 }
586
587 if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
588 &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
589 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
590 return -EIO;
591 }
592
593 ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
594 return 0;
595}
596
597/**
598 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
599 * @ioa_cfg: ioa config struct
600 *
601 * Return value:
602 * 0 on success / -EIO on failure
603 **/
604static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
605{
606 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
607
608 if (pcix_cmd_reg) {
609 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
610 ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
611 dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
612 return -EIO;
613 }
614 } else {
615 dev_err(&ioa_cfg->pdev->dev,
616 "Failed to setup PCI-X command register\n");
617 return -EIO;
618 }
619
620 return 0;
621}
622
623/**
624 * ipr_scsi_eh_done - mid-layer done function for aborted ops
625 * @ipr_cmd: ipr command struct
626 *
627 * This function is invoked by the interrupt handler for
628 * ops generated by the SCSI mid-layer which are being aborted.
629 *
630 * Return value:
631 * none
632 **/
633static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
634{
635 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
636 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
637
638 scsi_cmd->result |= (DID_ERROR << 16);
639
640 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
641 scsi_cmd->scsi_done(scsi_cmd);
642 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
643}
644
645/**
646 * ipr_fail_all_ops - Fails all outstanding ops.
647 * @ioa_cfg: ioa config struct
648 *
649 * This function fails all outstanding ops.
650 *
651 * Return value:
652 * none
653 **/
654static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
655{
656 struct ipr_cmnd *ipr_cmd, *temp;
657
658 ENTER;
659 list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
660 list_del(&ipr_cmd->queue);
661
662 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
663 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
664
665 if (ipr_cmd->scsi_cmd)
666 ipr_cmd->done = ipr_scsi_eh_done;
667
668 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
669 del_timer(&ipr_cmd->timer);
670 ipr_cmd->done(ipr_cmd);
671 }
672
673 LEAVE;
674}
675
676/**
677 * ipr_do_req - Send driver initiated requests.
678 * @ipr_cmd: ipr command struct
679 * @done: done function
680 * @timeout_func: timeout function
681 * @timeout: timeout value
682 *
683 * This function sends the specified command to the adapter with the
684 * timeout given. The done function is invoked on command completion.
685 *
686 * Return value:
687 * none
688 **/
689static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
690 void (*done) (struct ipr_cmnd *),
691 void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
692{
693 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
694
695 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
696
697 ipr_cmd->done = done;
698
699 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
700 ipr_cmd->timer.expires = jiffies + timeout;
701 ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
702
703 add_timer(&ipr_cmd->timer);
704
705 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
706
707 mb();
708 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
709 ioa_cfg->regs.ioarrin_reg);
710}
711
712/**
713 * ipr_internal_cmd_done - Op done function for an internally generated op.
714 * @ipr_cmd: ipr command struct
715 *
716 * This function is the op done function for an internally generated,
717 * blocking op. It simply wakes the sleeping thread.
718 *
719 * Return value:
720 * none
721 **/
722static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
723{
724 if (ipr_cmd->sibling)
725 ipr_cmd->sibling = NULL;
726 else
727 complete(&ipr_cmd->completion);
728}
729
730/**
731 * ipr_send_blocking_cmd - Send command and sleep on its completion.
732 * @ipr_cmd: ipr command struct
733 * @timeout_func: function to invoke if command times out
734 * @timeout: timeout
735 *
736 * Return value:
737 * none
738 **/
739static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
740 void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
741 u32 timeout)
742{
743 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
744
745 init_completion(&ipr_cmd->completion);
746 ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
747
748 spin_unlock_irq(ioa_cfg->host->host_lock);
749 wait_for_completion(&ipr_cmd->completion);
750 spin_lock_irq(ioa_cfg->host->host_lock);
751}
752
753/**
754 * ipr_send_hcam - Send an HCAM to the adapter.
755 * @ioa_cfg: ioa config struct
756 * @type: HCAM type
757 * @hostrcb: hostrcb struct
758 *
759 * This function will send a Host Controlled Async command to the adapter.
760 * If HCAMs are currently not allowed to be issued to the adapter, it will
761 * place the hostrcb on the free queue.
762 *
763 * Return value:
764 * none
765 **/
766static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
767 struct ipr_hostrcb *hostrcb)
768{
769 struct ipr_cmnd *ipr_cmd;
770 struct ipr_ioarcb *ioarcb;
771
772 if (ioa_cfg->allow_cmds) {
773 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
774 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
775 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
776
777 ipr_cmd->u.hostrcb = hostrcb;
778 ioarcb = &ipr_cmd->ioarcb;
779
780 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
781 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
782 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
783 ioarcb->cmd_pkt.cdb[1] = type;
784 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
785 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
786
787 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
788 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
789 ipr_cmd->ioadl[0].flags_and_data_len =
790 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
791 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
792
793 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
794 ipr_cmd->done = ipr_process_ccn;
795 else
796 ipr_cmd->done = ipr_process_error;
797
798 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
799
800 mb();
801 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
802 ioa_cfg->regs.ioarrin_reg);
803 } else {
804 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
805 }
806}
807
808/**
809 * ipr_init_res_entry - Initialize a resource entry struct.
810 * @res: resource entry struct
811 *
812 * Return value:
813 * none
814 **/
815static void ipr_init_res_entry(struct ipr_resource_entry *res)
816{
817 res->needs_sync_complete = 1;
818 res->in_erp = 0;
819 res->add_to_ml = 0;
820 res->del_from_ml = 0;
821 res->resetting_device = 0;
822 res->sdev = NULL;
823}
824
825/**
826 * ipr_handle_config_change - Handle a config change from the adapter
827 * @ioa_cfg: ioa config struct
828 * @hostrcb: hostrcb
829 *
830 * Return value:
831 * none
832 **/
833static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
834 struct ipr_hostrcb *hostrcb)
835{
836 struct ipr_resource_entry *res = NULL;
837 struct ipr_config_table_entry *cfgte;
838 u32 is_ndn = 1;
839
840 cfgte = &hostrcb->hcam.u.ccn.cfgte;
841
842 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
843 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
844 sizeof(cfgte->res_addr))) {
845 is_ndn = 0;
846 break;
847 }
848 }
849
850 if (is_ndn) {
851 if (list_empty(&ioa_cfg->free_res_q)) {
852 ipr_send_hcam(ioa_cfg,
853 IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
854 hostrcb);
855 return;
856 }
857
858 res = list_entry(ioa_cfg->free_res_q.next,
859 struct ipr_resource_entry, queue);
860
861 list_del(&res->queue);
862 ipr_init_res_entry(res);
863 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
864 }
865
866 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
867
868 if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
869 if (res->sdev) {
870 res->sdev->hostdata = NULL;
871 res->del_from_ml = 1;
872 if (ioa_cfg->allow_ml_add_del)
873 schedule_work(&ioa_cfg->work_q);
874 } else
875 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
876 } else if (!res->sdev) {
877 res->add_to_ml = 1;
878 if (ioa_cfg->allow_ml_add_del)
879 schedule_work(&ioa_cfg->work_q);
880 }
881
882 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
883}
884
885/**
886 * ipr_process_ccn - Op done function for a CCN.
887 * @ipr_cmd: ipr command struct
888 *
889 * This function is the op done function for a configuration
890 * change notification host controlled async from the adapter.
891 *
892 * Return value:
893 * none
894 **/
895static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
896{
897 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
898 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
899 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
900
901 list_del(&hostrcb->queue);
902 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
903
904 if (ioasc) {
905 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
906 dev_err(&ioa_cfg->pdev->dev,
907 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
908
909 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
910 } else {
911 ipr_handle_config_change(ioa_cfg, hostrcb);
912 }
913}
914
915/**
916 * ipr_log_vpd - Log the passed VPD to the error log.
cfc32139 917 * @vpd: vendor/product id/sn struct
1da177e4
LT
918 *
919 * Return value:
920 * none
921 **/
cfc32139 922static void ipr_log_vpd(struct ipr_vpd *vpd)
1da177e4
LT
923{
924 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
925 + IPR_SERIAL_NUM_LEN];
926
cfc32139
BK
927 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
928 memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
1da177e4
LT
929 IPR_PROD_ID_LEN);
930 buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
931 ipr_err("Vendor/Product ID: %s\n", buffer);
932
cfc32139 933 memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
1da177e4
LT
934 buffer[IPR_SERIAL_NUM_LEN] = '\0';
935 ipr_err(" Serial Number: %s\n", buffer);
936}
937
938/**
939 * ipr_log_cache_error - Log a cache error.
940 * @ioa_cfg: ioa config struct
941 * @hostrcb: hostrcb struct
942 *
943 * Return value:
944 * none
945 **/
946static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
947 struct ipr_hostrcb *hostrcb)
948{
949 struct ipr_hostrcb_type_02_error *error =
950 &hostrcb->hcam.u.error.u.type_02_error;
951
952 ipr_err("-----Current Configuration-----\n");
953 ipr_err("Cache Directory Card Information:\n");
cfc32139 954 ipr_log_vpd(&error->ioa_vpd);
1da177e4 955 ipr_err("Adapter Card Information:\n");
cfc32139 956 ipr_log_vpd(&error->cfc_vpd);
1da177e4
LT
957
958 ipr_err("-----Expected Configuration-----\n");
959 ipr_err("Cache Directory Card Information:\n");
cfc32139 960 ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
1da177e4 961 ipr_err("Adapter Card Information:\n");
cfc32139 962 ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
1da177e4
LT
963
964 ipr_err("Additional IOA Data: %08X %08X %08X\n",
965 be32_to_cpu(error->ioa_data[0]),
966 be32_to_cpu(error->ioa_data[1]),
967 be32_to_cpu(error->ioa_data[2]));
968}
969
970/**
971 * ipr_log_config_error - Log a configuration error.
972 * @ioa_cfg: ioa config struct
973 * @hostrcb: hostrcb struct
974 *
975 * Return value:
976 * none
977 **/
978static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
979 struct ipr_hostrcb *hostrcb)
980{
981 int errors_logged, i;
982 struct ipr_hostrcb_device_data_entry *dev_entry;
983 struct ipr_hostrcb_type_03_error *error;
984
985 error = &hostrcb->hcam.u.error.u.type_03_error;
986 errors_logged = be32_to_cpu(error->errors_logged);
987
988 ipr_err("Device Errors Detected/Logged: %d/%d\n",
989 be32_to_cpu(error->errors_detected), errors_logged);
990
cfc32139 991 dev_entry = error->dev;
1da177e4
LT
992
993 for (i = 0; i < errors_logged; i++, dev_entry++) {
994 ipr_err_separator;
995
fa15b1f6 996 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
cfc32139 997 ipr_log_vpd(&dev_entry->vpd);
1da177e4
LT
998
999 ipr_err("-----New Device Information-----\n");
cfc32139 1000 ipr_log_vpd(&dev_entry->new_vpd);
1da177e4
LT
1001
1002 ipr_err("Cache Directory Card Information:\n");
cfc32139 1003 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
1da177e4
LT
1004
1005 ipr_err("Adapter Card Information:\n");
cfc32139 1006 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
1da177e4
LT
1007
1008 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
1009 be32_to_cpu(dev_entry->ioa_data[0]),
1010 be32_to_cpu(dev_entry->ioa_data[1]),
1011 be32_to_cpu(dev_entry->ioa_data[2]),
1012 be32_to_cpu(dev_entry->ioa_data[3]),
1013 be32_to_cpu(dev_entry->ioa_data[4]));
1014 }
1015}
1016
1017/**
1018 * ipr_log_array_error - Log an array configuration error.
1019 * @ioa_cfg: ioa config struct
1020 * @hostrcb: hostrcb struct
1021 *
1022 * Return value:
1023 * none
1024 **/
1025static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1026 struct ipr_hostrcb *hostrcb)
1027{
1028 int i;
1029 struct ipr_hostrcb_type_04_error *error;
1030 struct ipr_hostrcb_array_data_entry *array_entry;
1031 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1032
1033 error = &hostrcb->hcam.u.error.u.type_04_error;
1034
1035 ipr_err_separator;
1036
1037 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1038 error->protection_level,
1039 ioa_cfg->host->host_no,
1040 error->last_func_vset_res_addr.bus,
1041 error->last_func_vset_res_addr.target,
1042 error->last_func_vset_res_addr.lun);
1043
1044 ipr_err_separator;
1045
1046 array_entry = error->array_member;
1047
1048 for (i = 0; i < 18; i++) {
cfc32139 1049 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1da177e4
LT
1050 continue;
1051
fa15b1f6 1052 if (be32_to_cpu(error->exposed_mode_adn) == i)
1da177e4 1053 ipr_err("Exposed Array Member %d:\n", i);
fa15b1f6 1054 else
1da177e4 1055 ipr_err("Array Member %d:\n", i);
1da177e4 1056
cfc32139 1057 ipr_log_vpd(&array_entry->vpd);
1da177e4 1058
fa15b1f6
BK
1059 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1060 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1061 "Expected Location");
1da177e4
LT
1062
1063 ipr_err_separator;
1064
1065 if (i == 9)
1066 array_entry = error->array_member2;
1067 else
1068 array_entry++;
1069 }
1070}
1071
1072/**
b0df54bb
BK
1073 * ipr_log_hex_data - Log additional hex IOA error data.
1074 * @data: IOA error data
1075 * @len: data length
1da177e4
LT
1076 *
1077 * Return value:
1078 * none
1079 **/
b0df54bb 1080static void ipr_log_hex_data(u32 *data, int len)
1da177e4
LT
1081{
1082 int i;
1da177e4 1083
b0df54bb 1084 if (len == 0)
1da177e4
LT
1085 return;
1086
b0df54bb 1087 for (i = 0; i < len / 4; i += 4) {
1da177e4 1088 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
b0df54bb
BK
1089 be32_to_cpu(data[i]),
1090 be32_to_cpu(data[i+1]),
1091 be32_to_cpu(data[i+2]),
1092 be32_to_cpu(data[i+3]));
1da177e4
LT
1093 }
1094}
1095
b0df54bb
BK
1096/**
1097 * ipr_log_dual_ioa_error - Log a dual adapter error.
1098 * @ioa_cfg: ioa config struct
1099 * @hostrcb: hostrcb struct
1100 *
1101 * Return value:
1102 * none
1103 **/
1104static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1105 struct ipr_hostrcb *hostrcb)
1106{
1107 struct ipr_hostrcb_type_07_error *error;
1108
1109 error = &hostrcb->hcam.u.error.u.type_07_error;
1110 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
1111
1112 ipr_err("%s\n", error->failure_reason);
1113 ipr_err("Remote Adapter VPD:\n");
1114 ipr_log_vpd(&error->vpd);
1115 ipr_log_hex_data(error->data,
1116 be32_to_cpu(hostrcb->hcam.length) -
1117 (offsetof(struct ipr_hostrcb_error, u) +
1118 offsetof(struct ipr_hostrcb_type_07_error, data)));
1119}
1120
1121/**
1122 * ipr_log_generic_error - Log an adapter error.
1123 * @ioa_cfg: ioa config struct
1124 * @hostrcb: hostrcb struct
1125 *
1126 * Return value:
1127 * none
1128 **/
1129static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1130 struct ipr_hostrcb *hostrcb)
1131{
1132 ipr_log_hex_data(hostrcb->hcam.u.raw.data,
1133 be32_to_cpu(hostrcb->hcam.length));
1134}
1135
1da177e4
LT
1136/**
1137 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1138 * @ioasc: IOASC
1139 *
1140 * This function will return the index of into the ipr_error_table
1141 * for the specified IOASC. If the IOASC is not in the table,
1142 * 0 will be returned, which points to the entry used for unknown errors.
1143 *
1144 * Return value:
1145 * index into the ipr_error_table
1146 **/
1147static u32 ipr_get_error(u32 ioasc)
1148{
1149 int i;
1150
1151 for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
1152 if (ipr_error_table[i].ioasc == ioasc)
1153 return i;
1154
1155 return 0;
1156}
1157
1158/**
1159 * ipr_handle_log_data - Log an adapter error.
1160 * @ioa_cfg: ioa config struct
1161 * @hostrcb: hostrcb struct
1162 *
1163 * This function logs an adapter error to the system.
1164 *
1165 * Return value:
1166 * none
1167 **/
1168static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1169 struct ipr_hostrcb *hostrcb)
1170{
1171 u32 ioasc;
1172 int error_index;
1173
1174 if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1175 return;
1176
1177 if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1178 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1179
1180 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1181
1182 if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1183 ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1184 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1185 scsi_report_bus_reset(ioa_cfg->host,
1186 hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1187 }
1188
1189 error_index = ipr_get_error(ioasc);
1190
1191 if (!ipr_error_table[error_index].log_hcam)
1192 return;
1193
1194 if (ipr_is_device(&hostrcb->hcam.u.error.failing_dev_res_addr)) {
1195 ipr_res_err(ioa_cfg, hostrcb->hcam.u.error.failing_dev_res_addr,
1196 "%s\n", ipr_error_table[error_index].error);
1197 } else {
1198 dev_err(&ioa_cfg->pdev->dev, "%s\n",
1199 ipr_error_table[error_index].error);
1200 }
1201
1202 /* Set indication we have logged an error */
1203 ioa_cfg->errors_logged++;
1204
1205 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
1206 return;
cf852037
BK
1207 if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
1208 hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
1da177e4
LT
1209
1210 switch (hostrcb->hcam.overlay_id) {
1da177e4
LT
1211 case IPR_HOST_RCB_OVERLAY_ID_2:
1212 ipr_log_cache_error(ioa_cfg, hostrcb);
1213 break;
1214 case IPR_HOST_RCB_OVERLAY_ID_3:
1215 ipr_log_config_error(ioa_cfg, hostrcb);
1216 break;
1217 case IPR_HOST_RCB_OVERLAY_ID_4:
1218 case IPR_HOST_RCB_OVERLAY_ID_6:
1219 ipr_log_array_error(ioa_cfg, hostrcb);
1220 break;
b0df54bb
BK
1221 case IPR_HOST_RCB_OVERLAY_ID_7:
1222 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
1223 break;
cf852037 1224 case IPR_HOST_RCB_OVERLAY_ID_1:
1da177e4 1225 case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1da177e4 1226 default:
a9cfca96 1227 ipr_log_generic_error(ioa_cfg, hostrcb);
1da177e4
LT
1228 break;
1229 }
1230}
1231
1232/**
1233 * ipr_process_error - Op done function for an adapter error log.
1234 * @ipr_cmd: ipr command struct
1235 *
1236 * This function is the op done function for an error log host
1237 * controlled async from the adapter. It will log the error and
1238 * send the HCAM back to the adapter.
1239 *
1240 * Return value:
1241 * none
1242 **/
1243static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1244{
1245 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1246 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1247 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
1248
1249 list_del(&hostrcb->queue);
1250 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1251
1252 if (!ioasc) {
1253 ipr_handle_log_data(ioa_cfg, hostrcb);
1254 } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1255 dev_err(&ioa_cfg->pdev->dev,
1256 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1257 }
1258
1259 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1260}
1261
1262/**
1263 * ipr_timeout - An internally generated op has timed out.
1264 * @ipr_cmd: ipr command struct
1265 *
1266 * This function blocks host requests and initiates an
1267 * adapter reset.
1268 *
1269 * Return value:
1270 * none
1271 **/
1272static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1273{
1274 unsigned long lock_flags = 0;
1275 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1276
1277 ENTER;
1278 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1279
1280 ioa_cfg->errors_logged++;
1281 dev_err(&ioa_cfg->pdev->dev,
1282 "Adapter being reset due to command timeout.\n");
1283
1284 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1285 ioa_cfg->sdt_state = GET_DUMP;
1286
1287 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1288 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1289
1290 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1291 LEAVE;
1292}
1293
1294/**
1295 * ipr_oper_timeout - Adapter timed out transitioning to operational
1296 * @ipr_cmd: ipr command struct
1297 *
1298 * This function blocks host requests and initiates an
1299 * adapter reset.
1300 *
1301 * Return value:
1302 * none
1303 **/
1304static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
1305{
1306 unsigned long lock_flags = 0;
1307 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1308
1309 ENTER;
1310 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1311
1312 ioa_cfg->errors_logged++;
1313 dev_err(&ioa_cfg->pdev->dev,
1314 "Adapter timed out transitioning to operational.\n");
1315
1316 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1317 ioa_cfg->sdt_state = GET_DUMP;
1318
1319 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
1320 if (ipr_fastfail)
1321 ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
1322 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1323 }
1324
1325 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1326 LEAVE;
1327}
1328
1329/**
1330 * ipr_reset_reload - Reset/Reload the IOA
1331 * @ioa_cfg: ioa config struct
1332 * @shutdown_type: shutdown type
1333 *
1334 * This function resets the adapter and re-initializes it.
1335 * This function assumes that all new host commands have been stopped.
1336 * Return value:
1337 * SUCCESS / FAILED
1338 **/
1339static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1340 enum ipr_shutdown_type shutdown_type)
1341{
1342 if (!ioa_cfg->in_reset_reload)
1343 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1344
1345 spin_unlock_irq(ioa_cfg->host->host_lock);
1346 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1347 spin_lock_irq(ioa_cfg->host->host_lock);
1348
1349 /* If we got hit with a host reset while we were already resetting
1350 the adapter for some reason, and the reset failed. */
1351 if (ioa_cfg->ioa_is_dead) {
1352 ipr_trace;
1353 return FAILED;
1354 }
1355
1356 return SUCCESS;
1357}
1358
1359/**
1360 * ipr_find_ses_entry - Find matching SES in SES table
1361 * @res: resource entry struct of SES
1362 *
1363 * Return value:
1364 * pointer to SES table entry / NULL on failure
1365 **/
1366static const struct ipr_ses_table_entry *
1367ipr_find_ses_entry(struct ipr_resource_entry *res)
1368{
1369 int i, j, matches;
1370 const struct ipr_ses_table_entry *ste = ipr_ses_table;
1371
1372 for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1373 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1374 if (ste->compare_product_id_byte[j] == 'X') {
1375 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1376 matches++;
1377 else
1378 break;
1379 } else
1380 matches++;
1381 }
1382
1383 if (matches == IPR_PROD_ID_LEN)
1384 return ste;
1385 }
1386
1387 return NULL;
1388}
1389
1390/**
1391 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1392 * @ioa_cfg: ioa config struct
1393 * @bus: SCSI bus
1394 * @bus_width: bus width
1395 *
1396 * Return value:
1397 * SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1398 * For a 2-byte wide SCSI bus, the maximum transfer speed is
1399 * twice the maximum transfer rate (e.g. for a wide enabled bus,
1400 * max 160MHz = max 320MB/sec).
1401 **/
1402static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1403{
1404 struct ipr_resource_entry *res;
1405 const struct ipr_ses_table_entry *ste;
1406 u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1407
1408 /* Loop through each config table entry in the config table buffer */
1409 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1410 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1411 continue;
1412
1413 if (bus != res->cfgte.res_addr.bus)
1414 continue;
1415
1416 if (!(ste = ipr_find_ses_entry(res)))
1417 continue;
1418
1419 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1420 }
1421
1422 return max_xfer_rate;
1423}
1424
1425/**
1426 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1427 * @ioa_cfg: ioa config struct
1428 * @max_delay: max delay in micro-seconds to wait
1429 *
1430 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1431 *
1432 * Return value:
1433 * 0 on success / other on failure
1434 **/
1435static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1436{
1437 volatile u32 pcii_reg;
1438 int delay = 1;
1439
1440 /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1441 while (delay < max_delay) {
1442 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1443
1444 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1445 return 0;
1446
1447 /* udelay cannot be used if delay is more than a few milliseconds */
1448 if ((delay / 1000) > MAX_UDELAY_MS)
1449 mdelay(delay / 1000);
1450 else
1451 udelay(delay);
1452
1453 delay += delay;
1454 }
1455 return -EIO;
1456}
1457
1458/**
1459 * ipr_get_ldump_data_section - Dump IOA memory
1460 * @ioa_cfg: ioa config struct
1461 * @start_addr: adapter address to dump
1462 * @dest: destination kernel buffer
1463 * @length_in_words: length to dump in 4 byte words
1464 *
1465 * Return value:
1466 * 0 on success / -EIO on failure
1467 **/
1468static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1469 u32 start_addr,
1470 __be32 *dest, u32 length_in_words)
1471{
1472 volatile u32 temp_pcii_reg;
1473 int i, delay = 0;
1474
1475 /* Write IOA interrupt reg starting LDUMP state */
1476 writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1477 ioa_cfg->regs.set_uproc_interrupt_reg);
1478
1479 /* Wait for IO debug acknowledge */
1480 if (ipr_wait_iodbg_ack(ioa_cfg,
1481 IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1482 dev_err(&ioa_cfg->pdev->dev,
1483 "IOA dump long data transfer timeout\n");
1484 return -EIO;
1485 }
1486
1487 /* Signal LDUMP interlocked - clear IO debug ack */
1488 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1489 ioa_cfg->regs.clr_interrupt_reg);
1490
1491 /* Write Mailbox with starting address */
1492 writel(start_addr, ioa_cfg->ioa_mailbox);
1493
1494 /* Signal address valid - clear IOA Reset alert */
1495 writel(IPR_UPROCI_RESET_ALERT,
1496 ioa_cfg->regs.clr_uproc_interrupt_reg);
1497
1498 for (i = 0; i < length_in_words; i++) {
1499 /* Wait for IO debug acknowledge */
1500 if (ipr_wait_iodbg_ack(ioa_cfg,
1501 IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1502 dev_err(&ioa_cfg->pdev->dev,
1503 "IOA dump short data transfer timeout\n");
1504 return -EIO;
1505 }
1506
1507 /* Read data from mailbox and increment destination pointer */
1508 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1509 dest++;
1510
1511 /* For all but the last word of data, signal data received */
1512 if (i < (length_in_words - 1)) {
1513 /* Signal dump data received - Clear IO debug Ack */
1514 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1515 ioa_cfg->regs.clr_interrupt_reg);
1516 }
1517 }
1518
1519 /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1520 writel(IPR_UPROCI_RESET_ALERT,
1521 ioa_cfg->regs.set_uproc_interrupt_reg);
1522
1523 writel(IPR_UPROCI_IO_DEBUG_ALERT,
1524 ioa_cfg->regs.clr_uproc_interrupt_reg);
1525
1526 /* Signal dump data received - Clear IO debug Ack */
1527 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1528 ioa_cfg->regs.clr_interrupt_reg);
1529
1530 /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
1531 while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
1532 temp_pcii_reg =
1533 readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
1534
1535 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
1536 return 0;
1537
1538 udelay(10);
1539 delay += 10;
1540 }
1541
1542 return 0;
1543}
1544
1545#ifdef CONFIG_SCSI_IPR_DUMP
1546/**
1547 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
1548 * @ioa_cfg: ioa config struct
1549 * @pci_address: adapter address
1550 * @length: length of data to copy
1551 *
1552 * Copy data from PCI adapter to kernel buffer.
1553 * Note: length MUST be a 4 byte multiple
1554 * Return value:
1555 * 0 on success / other on failure
1556 **/
1557static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
1558 unsigned long pci_address, u32 length)
1559{
1560 int bytes_copied = 0;
1561 int cur_len, rc, rem_len, rem_page_len;
1562 __be32 *page;
1563 unsigned long lock_flags = 0;
1564 struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
1565
1566 while (bytes_copied < length &&
1567 (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
1568 if (ioa_dump->page_offset >= PAGE_SIZE ||
1569 ioa_dump->page_offset == 0) {
1570 page = (__be32 *)__get_free_page(GFP_ATOMIC);
1571
1572 if (!page) {
1573 ipr_trace;
1574 return bytes_copied;
1575 }
1576
1577 ioa_dump->page_offset = 0;
1578 ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
1579 ioa_dump->next_page_index++;
1580 } else
1581 page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
1582
1583 rem_len = length - bytes_copied;
1584 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
1585 cur_len = min(rem_len, rem_page_len);
1586
1587 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1588 if (ioa_cfg->sdt_state == ABORT_DUMP) {
1589 rc = -EIO;
1590 } else {
1591 rc = ipr_get_ldump_data_section(ioa_cfg,
1592 pci_address + bytes_copied,
1593 &page[ioa_dump->page_offset / 4],
1594 (cur_len / sizeof(u32)));
1595 }
1596 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1597
1598 if (!rc) {
1599 ioa_dump->page_offset += cur_len;
1600 bytes_copied += cur_len;
1601 } else {
1602 ipr_trace;
1603 break;
1604 }
1605 schedule();
1606 }
1607
1608 return bytes_copied;
1609}
1610
1611/**
1612 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
1613 * @hdr: dump entry header struct
1614 *
1615 * Return value:
1616 * nothing
1617 **/
1618static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
1619{
1620 hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
1621 hdr->num_elems = 1;
1622 hdr->offset = sizeof(*hdr);
1623 hdr->status = IPR_DUMP_STATUS_SUCCESS;
1624}
1625
1626/**
1627 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
1628 * @ioa_cfg: ioa config struct
1629 * @driver_dump: driver dump struct
1630 *
1631 * Return value:
1632 * nothing
1633 **/
1634static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
1635 struct ipr_driver_dump *driver_dump)
1636{
1637 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
1638
1639 ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
1640 driver_dump->ioa_type_entry.hdr.len =
1641 sizeof(struct ipr_dump_ioa_type_entry) -
1642 sizeof(struct ipr_dump_entry_header);
1643 driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1644 driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
1645 driver_dump->ioa_type_entry.type = ioa_cfg->type;
1646 driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
1647 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
1648 ucode_vpd->minor_release[1];
1649 driver_dump->hdr.num_entries++;
1650}
1651
1652/**
1653 * ipr_dump_version_data - Fill in the driver version in the dump.
1654 * @ioa_cfg: ioa config struct
1655 * @driver_dump: driver dump struct
1656 *
1657 * Return value:
1658 * nothing
1659 **/
1660static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
1661 struct ipr_driver_dump *driver_dump)
1662{
1663 ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
1664 driver_dump->version_entry.hdr.len =
1665 sizeof(struct ipr_dump_version_entry) -
1666 sizeof(struct ipr_dump_entry_header);
1667 driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
1668 driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
1669 strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
1670 driver_dump->hdr.num_entries++;
1671}
1672
1673/**
1674 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
1675 * @ioa_cfg: ioa config struct
1676 * @driver_dump: driver dump struct
1677 *
1678 * Return value:
1679 * nothing
1680 **/
1681static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
1682 struct ipr_driver_dump *driver_dump)
1683{
1684 ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
1685 driver_dump->trace_entry.hdr.len =
1686 sizeof(struct ipr_dump_trace_entry) -
1687 sizeof(struct ipr_dump_entry_header);
1688 driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1689 driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
1690 memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
1691 driver_dump->hdr.num_entries++;
1692}
1693
1694/**
1695 * ipr_dump_location_data - Fill in the IOA location in the dump.
1696 * @ioa_cfg: ioa config struct
1697 * @driver_dump: driver dump struct
1698 *
1699 * Return value:
1700 * nothing
1701 **/
1702static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
1703 struct ipr_driver_dump *driver_dump)
1704{
1705 ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
1706 driver_dump->location_entry.hdr.len =
1707 sizeof(struct ipr_dump_location_entry) -
1708 sizeof(struct ipr_dump_entry_header);
1709 driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
1710 driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
1711 strcpy(driver_dump->location_entry.location, ioa_cfg->pdev->dev.bus_id);
1712 driver_dump->hdr.num_entries++;
1713}
1714
1715/**
1716 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
1717 * @ioa_cfg: ioa config struct
1718 * @dump: dump struct
1719 *
1720 * Return value:
1721 * nothing
1722 **/
1723static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
1724{
1725 unsigned long start_addr, sdt_word;
1726 unsigned long lock_flags = 0;
1727 struct ipr_driver_dump *driver_dump = &dump->driver_dump;
1728 struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
1729 u32 num_entries, start_off, end_off;
1730 u32 bytes_to_copy, bytes_copied, rc;
1731 struct ipr_sdt *sdt;
1732 int i;
1733
1734 ENTER;
1735
1736 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1737
1738 if (ioa_cfg->sdt_state != GET_DUMP) {
1739 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1740 return;
1741 }
1742
1743 start_addr = readl(ioa_cfg->ioa_mailbox);
1744
1745 if (!ipr_sdt_is_fmt2(start_addr)) {
1746 dev_err(&ioa_cfg->pdev->dev,
1747 "Invalid dump table format: %lx\n", start_addr);
1748 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1749 return;
1750 }
1751
1752 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
1753
1754 driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
1755
1756 /* Initialize the overall dump header */
1757 driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
1758 driver_dump->hdr.num_entries = 1;
1759 driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
1760 driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
1761 driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
1762 driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
1763
1764 ipr_dump_version_data(ioa_cfg, driver_dump);
1765 ipr_dump_location_data(ioa_cfg, driver_dump);
1766 ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
1767 ipr_dump_trace_data(ioa_cfg, driver_dump);
1768
1769 /* Update dump_header */
1770 driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
1771
1772 /* IOA Dump entry */
1773 ipr_init_dump_entry_hdr(&ioa_dump->hdr);
1774 ioa_dump->format = IPR_SDT_FMT2;
1775 ioa_dump->hdr.len = 0;
1776 ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1777 ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
1778
1779 /* First entries in sdt are actually a list of dump addresses and
1780 lengths to gather the real dump data. sdt represents the pointer
1781 to the ioa generated dump table. Dump data will be extracted based
1782 on entries in this table */
1783 sdt = &ioa_dump->sdt;
1784
1785 rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
1786 sizeof(struct ipr_sdt) / sizeof(__be32));
1787
1788 /* Smart Dump table is ready to use and the first entry is valid */
1789 if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
1790 dev_err(&ioa_cfg->pdev->dev,
1791 "Dump of IOA failed. Dump table not valid: %d, %X.\n",
1792 rc, be32_to_cpu(sdt->hdr.state));
1793 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
1794 ioa_cfg->sdt_state = DUMP_OBTAINED;
1795 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1796 return;
1797 }
1798
1799 num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
1800
1801 if (num_entries > IPR_NUM_SDT_ENTRIES)
1802 num_entries = IPR_NUM_SDT_ENTRIES;
1803
1804 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1805
1806 for (i = 0; i < num_entries; i++) {
1807 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
1808 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1809 break;
1810 }
1811
1812 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
1813 sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
1814 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
1815 end_off = be32_to_cpu(sdt->entry[i].end_offset);
1816
1817 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
1818 bytes_to_copy = end_off - start_off;
1819 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
1820 sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
1821 continue;
1822 }
1823
1824 /* Copy data from adapter to driver buffers */
1825 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
1826 bytes_to_copy);
1827
1828 ioa_dump->hdr.len += bytes_copied;
1829
1830 if (bytes_copied != bytes_to_copy) {
1831 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1832 break;
1833 }
1834 }
1835 }
1836 }
1837
1838 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
1839
1840 /* Update dump_header */
1841 driver_dump->hdr.len += ioa_dump->hdr.len;
1842 wmb();
1843 ioa_cfg->sdt_state = DUMP_OBTAINED;
1844 LEAVE;
1845}
1846
1847#else
1848#define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
1849#endif
1850
1851/**
1852 * ipr_release_dump - Free adapter dump memory
1853 * @kref: kref struct
1854 *
1855 * Return value:
1856 * nothing
1857 **/
1858static void ipr_release_dump(struct kref *kref)
1859{
1860 struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
1861 struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
1862 unsigned long lock_flags = 0;
1863 int i;
1864
1865 ENTER;
1866 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1867 ioa_cfg->dump = NULL;
1868 ioa_cfg->sdt_state = INACTIVE;
1869 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1870
1871 for (i = 0; i < dump->ioa_dump.next_page_index; i++)
1872 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
1873
1874 kfree(dump);
1875 LEAVE;
1876}
1877
1878/**
1879 * ipr_worker_thread - Worker thread
1880 * @data: ioa config struct
1881 *
1882 * Called at task level from a work thread. This function takes care
1883 * of adding and removing device from the mid-layer as configuration
1884 * changes are detected by the adapter.
1885 *
1886 * Return value:
1887 * nothing
1888 **/
1889static void ipr_worker_thread(void *data)
1890{
1891 unsigned long lock_flags;
1892 struct ipr_resource_entry *res;
1893 struct scsi_device *sdev;
1894 struct ipr_dump *dump;
1895 struct ipr_ioa_cfg *ioa_cfg = data;
1896 u8 bus, target, lun;
1897 int did_work;
1898
1899 ENTER;
1900 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1901
1902 if (ioa_cfg->sdt_state == GET_DUMP) {
1903 dump = ioa_cfg->dump;
1904 if (!dump) {
1905 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1906 return;
1907 }
1908 kref_get(&dump->kref);
1909 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1910 ipr_get_ioa_dump(ioa_cfg, dump);
1911 kref_put(&dump->kref, ipr_release_dump);
1912
1913 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1914 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
1915 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1916 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1917 return;
1918 }
1919
1920restart:
1921 do {
1922 did_work = 0;
1923 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
1924 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1925 return;
1926 }
1927
1928 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1929 if (res->del_from_ml && res->sdev) {
1930 did_work = 1;
1931 sdev = res->sdev;
1932 if (!scsi_device_get(sdev)) {
1933 res->sdev = NULL;
1934 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
1935 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1936 scsi_remove_device(sdev);
1937 scsi_device_put(sdev);
1938 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1939 }
1940 break;
1941 }
1942 }
1943 } while(did_work);
1944
1945 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1946 if (res->add_to_ml) {
1947 bus = res->cfgte.res_addr.bus;
1948 target = res->cfgte.res_addr.target;
1949 lun = res->cfgte.res_addr.lun;
1950 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1951 scsi_add_device(ioa_cfg->host, bus, target, lun);
1952 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1953 goto restart;
1954 }
1955 }
1956
1957 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1958 kobject_uevent(&ioa_cfg->host->shost_classdev.kobj, KOBJ_CHANGE, NULL);
1959 LEAVE;
1960}
1961
1962#ifdef CONFIG_SCSI_IPR_TRACE
1963/**
1964 * ipr_read_trace - Dump the adapter trace
1965 * @kobj: kobject struct
1966 * @buf: buffer
1967 * @off: offset
1968 * @count: buffer size
1969 *
1970 * Return value:
1971 * number of bytes printed to buffer
1972 **/
1973static ssize_t ipr_read_trace(struct kobject *kobj, char *buf,
1974 loff_t off, size_t count)
1975{
1976 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
1977 struct Scsi_Host *shost = class_to_shost(cdev);
1978 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1979 unsigned long lock_flags = 0;
1980 int size = IPR_TRACE_SIZE;
1981 char *src = (char *)ioa_cfg->trace;
1982
1983 if (off > size)
1984 return 0;
1985 if (off + count > size) {
1986 size -= off;
1987 count = size;
1988 }
1989
1990 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1991 memcpy(buf, &src[off], count);
1992 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1993 return count;
1994}
1995
1996static struct bin_attribute ipr_trace_attr = {
1997 .attr = {
1998 .name = "trace",
1999 .mode = S_IRUGO,
2000 },
2001 .size = 0,
2002 .read = ipr_read_trace,
2003};
2004#endif
2005
62275040
BK
2006static const struct {
2007 enum ipr_cache_state state;
2008 char *name;
2009} cache_state [] = {
2010 { CACHE_NONE, "none" },
2011 { CACHE_DISABLED, "disabled" },
2012 { CACHE_ENABLED, "enabled" }
2013};
2014
2015/**
2016 * ipr_show_write_caching - Show the write caching attribute
2017 * @class_dev: class device struct
2018 * @buf: buffer
2019 *
2020 * Return value:
2021 * number of bytes printed to buffer
2022 **/
2023static ssize_t ipr_show_write_caching(struct class_device *class_dev, char *buf)
2024{
2025 struct Scsi_Host *shost = class_to_shost(class_dev);
2026 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2027 unsigned long lock_flags = 0;
2028 int i, len = 0;
2029
2030 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2031 for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2032 if (cache_state[i].state == ioa_cfg->cache_state) {
2033 len = snprintf(buf, PAGE_SIZE, "%s\n", cache_state[i].name);
2034 break;
2035 }
2036 }
2037 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2038 return len;
2039}
2040
2041
2042/**
2043 * ipr_store_write_caching - Enable/disable adapter write cache
2044 * @class_dev: class_device struct
2045 * @buf: buffer
2046 * @count: buffer size
2047 *
2048 * This function will enable/disable adapter write cache.
2049 *
2050 * Return value:
2051 * count on success / other on failure
2052 **/
2053static ssize_t ipr_store_write_caching(struct class_device *class_dev,
2054 const char *buf, size_t count)
2055{
2056 struct Scsi_Host *shost = class_to_shost(class_dev);
2057 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2058 unsigned long lock_flags = 0;
2059 enum ipr_cache_state new_state = CACHE_INVALID;
2060 int i;
2061
2062 if (!capable(CAP_SYS_ADMIN))
2063 return -EACCES;
2064 if (ioa_cfg->cache_state == CACHE_NONE)
2065 return -EINVAL;
2066
2067 for (i = 0; i < ARRAY_SIZE(cache_state); i++) {
2068 if (!strncmp(cache_state[i].name, buf, strlen(cache_state[i].name))) {
2069 new_state = cache_state[i].state;
2070 break;
2071 }
2072 }
2073
2074 if (new_state != CACHE_DISABLED && new_state != CACHE_ENABLED)
2075 return -EINVAL;
2076
2077 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2078 if (ioa_cfg->cache_state == new_state) {
2079 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2080 return count;
2081 }
2082
2083 ioa_cfg->cache_state = new_state;
2084 dev_info(&ioa_cfg->pdev->dev, "%s adapter write cache.\n",
2085 new_state == CACHE_ENABLED ? "Enabling" : "Disabling");
2086 if (!ioa_cfg->in_reset_reload)
2087 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2088 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2089 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2090
2091 return count;
2092}
2093
2094static struct class_device_attribute ipr_ioa_cache_attr = {
2095 .attr = {
2096 .name = "write_cache",
2097 .mode = S_IRUGO | S_IWUSR,
2098 },
2099 .show = ipr_show_write_caching,
2100 .store = ipr_store_write_caching
2101};
2102
1da177e4
LT
2103/**
2104 * ipr_show_fw_version - Show the firmware version
2105 * @class_dev: class device struct
2106 * @buf: buffer
2107 *
2108 * Return value:
2109 * number of bytes printed to buffer
2110 **/
2111static ssize_t ipr_show_fw_version(struct class_device *class_dev, char *buf)
2112{
2113 struct Scsi_Host *shost = class_to_shost(class_dev);
2114 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2115 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
2116 unsigned long lock_flags = 0;
2117 int len;
2118
2119 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2120 len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
2121 ucode_vpd->major_release, ucode_vpd->card_type,
2122 ucode_vpd->minor_release[0],
2123 ucode_vpd->minor_release[1]);
2124 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2125 return len;
2126}
2127
2128static struct class_device_attribute ipr_fw_version_attr = {
2129 .attr = {
2130 .name = "fw_version",
2131 .mode = S_IRUGO,
2132 },
2133 .show = ipr_show_fw_version,
2134};
2135
2136/**
2137 * ipr_show_log_level - Show the adapter's error logging level
2138 * @class_dev: class device struct
2139 * @buf: buffer
2140 *
2141 * Return value:
2142 * number of bytes printed to buffer
2143 **/
2144static ssize_t ipr_show_log_level(struct class_device *class_dev, char *buf)
2145{
2146 struct Scsi_Host *shost = class_to_shost(class_dev);
2147 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2148 unsigned long lock_flags = 0;
2149 int len;
2150
2151 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2152 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
2153 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2154 return len;
2155}
2156
2157/**
2158 * ipr_store_log_level - Change the adapter's error logging level
2159 * @class_dev: class device struct
2160 * @buf: buffer
2161 *
2162 * Return value:
2163 * number of bytes printed to buffer
2164 **/
2165static ssize_t ipr_store_log_level(struct class_device *class_dev,
2166 const char *buf, size_t count)
2167{
2168 struct Scsi_Host *shost = class_to_shost(class_dev);
2169 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2170 unsigned long lock_flags = 0;
2171
2172 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2173 ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
2174 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2175 return strlen(buf);
2176}
2177
2178static struct class_device_attribute ipr_log_level_attr = {
2179 .attr = {
2180 .name = "log_level",
2181 .mode = S_IRUGO | S_IWUSR,
2182 },
2183 .show = ipr_show_log_level,
2184 .store = ipr_store_log_level
2185};
2186
2187/**
2188 * ipr_store_diagnostics - IOA Diagnostics interface
2189 * @class_dev: class_device struct
2190 * @buf: buffer
2191 * @count: buffer size
2192 *
2193 * This function will reset the adapter and wait a reasonable
2194 * amount of time for any errors that the adapter might log.
2195 *
2196 * Return value:
2197 * count on success / other on failure
2198 **/
2199static ssize_t ipr_store_diagnostics(struct class_device *class_dev,
2200 const char *buf, size_t count)
2201{
2202 struct Scsi_Host *shost = class_to_shost(class_dev);
2203 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2204 unsigned long lock_flags = 0;
2205 int rc = count;
2206
2207 if (!capable(CAP_SYS_ADMIN))
2208 return -EACCES;
2209
2210 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2211 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2212 ioa_cfg->errors_logged = 0;
2213 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2214
2215 if (ioa_cfg->in_reset_reload) {
2216 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2217 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2218
2219 /* Wait for a second for any errors to be logged */
2220 msleep(1000);
2221 } else {
2222 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2223 return -EIO;
2224 }
2225
2226 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2227 if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2228 rc = -EIO;
2229 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2230
2231 return rc;
2232}
2233
2234static struct class_device_attribute ipr_diagnostics_attr = {
2235 .attr = {
2236 .name = "run_diagnostics",
2237 .mode = S_IWUSR,
2238 },
2239 .store = ipr_store_diagnostics
2240};
2241
f37eb54b
BK
2242/**
2243 * ipr_show_adapter_state - Show the adapter's state
2244 * @class_dev: class device struct
2245 * @buf: buffer
2246 *
2247 * Return value:
2248 * number of bytes printed to buffer
2249 **/
2250static ssize_t ipr_show_adapter_state(struct class_device *class_dev, char *buf)
2251{
2252 struct Scsi_Host *shost = class_to_shost(class_dev);
2253 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2254 unsigned long lock_flags = 0;
2255 int len;
2256
2257 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2258 if (ioa_cfg->ioa_is_dead)
2259 len = snprintf(buf, PAGE_SIZE, "offline\n");
2260 else
2261 len = snprintf(buf, PAGE_SIZE, "online\n");
2262 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2263 return len;
2264}
2265
2266/**
2267 * ipr_store_adapter_state - Change adapter state
2268 * @class_dev: class_device struct
2269 * @buf: buffer
2270 * @count: buffer size
2271 *
2272 * This function will change the adapter's state.
2273 *
2274 * Return value:
2275 * count on success / other on failure
2276 **/
2277static ssize_t ipr_store_adapter_state(struct class_device *class_dev,
2278 const char *buf, size_t count)
2279{
2280 struct Scsi_Host *shost = class_to_shost(class_dev);
2281 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2282 unsigned long lock_flags;
2283 int result = count;
2284
2285 if (!capable(CAP_SYS_ADMIN))
2286 return -EACCES;
2287
2288 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2289 if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
2290 ioa_cfg->ioa_is_dead = 0;
2291 ioa_cfg->reset_retries = 0;
2292 ioa_cfg->in_ioa_bringdown = 0;
2293 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
2294 }
2295 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2296 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2297
2298 return result;
2299}
2300
2301static struct class_device_attribute ipr_ioa_state_attr = {
2302 .attr = {
2303 .name = "state",
2304 .mode = S_IRUGO | S_IWUSR,
2305 },
2306 .show = ipr_show_adapter_state,
2307 .store = ipr_store_adapter_state
2308};
2309
1da177e4
LT
2310/**
2311 * ipr_store_reset_adapter - Reset the adapter
2312 * @class_dev: class_device struct
2313 * @buf: buffer
2314 * @count: buffer size
2315 *
2316 * This function will reset the adapter.
2317 *
2318 * Return value:
2319 * count on success / other on failure
2320 **/
2321static ssize_t ipr_store_reset_adapter(struct class_device *class_dev,
2322 const char *buf, size_t count)
2323{
2324 struct Scsi_Host *shost = class_to_shost(class_dev);
2325 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2326 unsigned long lock_flags;
2327 int result = count;
2328
2329 if (!capable(CAP_SYS_ADMIN))
2330 return -EACCES;
2331
2332 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2333 if (!ioa_cfg->in_reset_reload)
2334 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2335 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2336 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2337
2338 return result;
2339}
2340
2341static struct class_device_attribute ipr_ioa_reset_attr = {
2342 .attr = {
2343 .name = "reset_host",
2344 .mode = S_IWUSR,
2345 },
2346 .store = ipr_store_reset_adapter
2347};
2348
2349/**
2350 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2351 * @buf_len: buffer length
2352 *
2353 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2354 * list to use for microcode download
2355 *
2356 * Return value:
2357 * pointer to sglist / NULL on failure
2358 **/
2359static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2360{
2361 int sg_size, order, bsize_elem, num_elem, i, j;
2362 struct ipr_sglist *sglist;
2363 struct scatterlist *scatterlist;
2364 struct page *page;
2365
2366 /* Get the minimum size per scatter/gather element */
2367 sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2368
2369 /* Get the actual size per element */
2370 order = get_order(sg_size);
2371
2372 /* Determine the actual number of bytes per element */
2373 bsize_elem = PAGE_SIZE * (1 << order);
2374
2375 /* Determine the actual number of sg entries needed */
2376 if (buf_len % bsize_elem)
2377 num_elem = (buf_len / bsize_elem) + 1;
2378 else
2379 num_elem = buf_len / bsize_elem;
2380
2381 /* Allocate a scatter/gather list for the DMA */
0bc42e35 2382 sglist = kzalloc(sizeof(struct ipr_sglist) +
1da177e4
LT
2383 (sizeof(struct scatterlist) * (num_elem - 1)),
2384 GFP_KERNEL);
2385
2386 if (sglist == NULL) {
2387 ipr_trace;
2388 return NULL;
2389 }
2390
1da177e4
LT
2391 scatterlist = sglist->scatterlist;
2392
2393 sglist->order = order;
2394 sglist->num_sg = num_elem;
2395
2396 /* Allocate a bunch of sg elements */
2397 for (i = 0; i < num_elem; i++) {
2398 page = alloc_pages(GFP_KERNEL, order);
2399 if (!page) {
2400 ipr_trace;
2401
2402 /* Free up what we already allocated */
2403 for (j = i - 1; j >= 0; j--)
2404 __free_pages(scatterlist[j].page, order);
2405 kfree(sglist);
2406 return NULL;
2407 }
2408
2409 scatterlist[i].page = page;
2410 }
2411
2412 return sglist;
2413}
2414
2415/**
2416 * ipr_free_ucode_buffer - Frees a microcode download buffer
2417 * @p_dnld: scatter/gather list pointer
2418 *
2419 * Free a DMA'able ucode download buffer previously allocated with
2420 * ipr_alloc_ucode_buffer
2421 *
2422 * Return value:
2423 * nothing
2424 **/
2425static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2426{
2427 int i;
2428
2429 for (i = 0; i < sglist->num_sg; i++)
2430 __free_pages(sglist->scatterlist[i].page, sglist->order);
2431
2432 kfree(sglist);
2433}
2434
2435/**
2436 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2437 * @sglist: scatter/gather list pointer
2438 * @buffer: buffer pointer
2439 * @len: buffer length
2440 *
2441 * Copy a microcode image from a user buffer into a buffer allocated by
2442 * ipr_alloc_ucode_buffer
2443 *
2444 * Return value:
2445 * 0 on success / other on failure
2446 **/
2447static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2448 u8 *buffer, u32 len)
2449{
2450 int bsize_elem, i, result = 0;
2451 struct scatterlist *scatterlist;
2452 void *kaddr;
2453
2454 /* Determine the actual number of bytes per element */
2455 bsize_elem = PAGE_SIZE * (1 << sglist->order);
2456
2457 scatterlist = sglist->scatterlist;
2458
2459 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
2460 kaddr = kmap(scatterlist[i].page);
2461 memcpy(kaddr, buffer, bsize_elem);
2462 kunmap(scatterlist[i].page);
2463
2464 scatterlist[i].length = bsize_elem;
2465
2466 if (result != 0) {
2467 ipr_trace;
2468 return result;
2469 }
2470 }
2471
2472 if (len % bsize_elem) {
2473 kaddr = kmap(scatterlist[i].page);
2474 memcpy(kaddr, buffer, len % bsize_elem);
2475 kunmap(scatterlist[i].page);
2476
2477 scatterlist[i].length = len % bsize_elem;
2478 }
2479
2480 sglist->buffer_len = len;
2481 return result;
2482}
2483
2484/**
12baa420 2485 * ipr_build_ucode_ioadl - Build a microcode download IOADL
1da177e4
LT
2486 * @ipr_cmd: ipr command struct
2487 * @sglist: scatter/gather list
1da177e4 2488 *
12baa420 2489 * Builds a microcode download IOA data list (IOADL).
1da177e4 2490 *
1da177e4 2491 **/
12baa420
BK
2492static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
2493 struct ipr_sglist *sglist)
1da177e4 2494{
1da177e4
LT
2495 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2496 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2497 struct scatterlist *scatterlist = sglist->scatterlist;
2498 int i;
2499
12baa420 2500 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
1da177e4 2501 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
12baa420 2502 ioarcb->write_data_transfer_length = cpu_to_be32(sglist->buffer_len);
1da177e4
LT
2503 ioarcb->write_ioadl_len =
2504 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
2505
2506 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
2507 ioadl[i].flags_and_data_len =
2508 cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
2509 ioadl[i].address =
2510 cpu_to_be32(sg_dma_address(&scatterlist[i]));
2511 }
2512
12baa420
BK
2513 ioadl[i-1].flags_and_data_len |=
2514 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
2515}
2516
2517/**
2518 * ipr_update_ioa_ucode - Update IOA's microcode
2519 * @ioa_cfg: ioa config struct
2520 * @sglist: scatter/gather list
2521 *
2522 * Initiate an adapter reset to update the IOA's microcode
2523 *
2524 * Return value:
2525 * 0 on success / -EIO on failure
2526 **/
2527static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
2528 struct ipr_sglist *sglist)
2529{
2530 unsigned long lock_flags;
2531
2532 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2533
2534 if (ioa_cfg->ucode_sglist) {
2535 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2536 dev_err(&ioa_cfg->pdev->dev,
2537 "Microcode download already in progress\n");
2538 return -EIO;
1da177e4 2539 }
12baa420
BK
2540
2541 sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
2542 sglist->num_sg, DMA_TO_DEVICE);
2543
2544 if (!sglist->num_dma_sg) {
2545 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2546 dev_err(&ioa_cfg->pdev->dev,
2547 "Failed to map microcode download buffer!\n");
1da177e4
LT
2548 return -EIO;
2549 }
2550
12baa420
BK
2551 ioa_cfg->ucode_sglist = sglist;
2552 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2553 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2554 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2555
2556 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2557 ioa_cfg->ucode_sglist = NULL;
2558 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1da177e4
LT
2559 return 0;
2560}
2561
2562/**
2563 * ipr_store_update_fw - Update the firmware on the adapter
2564 * @class_dev: class_device struct
2565 * @buf: buffer
2566 * @count: buffer size
2567 *
2568 * This function will update the firmware on the adapter.
2569 *
2570 * Return value:
2571 * count on success / other on failure
2572 **/
2573static ssize_t ipr_store_update_fw(struct class_device *class_dev,
2574 const char *buf, size_t count)
2575{
2576 struct Scsi_Host *shost = class_to_shost(class_dev);
2577 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2578 struct ipr_ucode_image_header *image_hdr;
2579 const struct firmware *fw_entry;
2580 struct ipr_sglist *sglist;
1da177e4
LT
2581 char fname[100];
2582 char *src;
2583 int len, result, dnld_size;
2584
2585 if (!capable(CAP_SYS_ADMIN))
2586 return -EACCES;
2587
2588 len = snprintf(fname, 99, "%s", buf);
2589 fname[len-1] = '\0';
2590
2591 if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
2592 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
2593 return -EIO;
2594 }
2595
2596 image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
2597
2598 if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
2599 (ioa_cfg->vpd_cbs->page3_data.card_type &&
2600 ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
2601 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
2602 release_firmware(fw_entry);
2603 return -EINVAL;
2604 }
2605
2606 src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
2607 dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
2608 sglist = ipr_alloc_ucode_buffer(dnld_size);
2609
2610 if (!sglist) {
2611 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
2612 release_firmware(fw_entry);
2613 return -ENOMEM;
2614 }
2615
2616 result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
2617
2618 if (result) {
2619 dev_err(&ioa_cfg->pdev->dev,
2620 "Microcode buffer copy to DMA buffer failed\n");
12baa420 2621 goto out;
1da177e4
LT
2622 }
2623
12baa420 2624 result = ipr_update_ioa_ucode(ioa_cfg, sglist);
1da177e4 2625
12baa420
BK
2626 if (!result)
2627 result = count;
2628out:
1da177e4
LT
2629 ipr_free_ucode_buffer(sglist);
2630 release_firmware(fw_entry);
12baa420 2631 return result;
1da177e4
LT
2632}
2633
2634static struct class_device_attribute ipr_update_fw_attr = {
2635 .attr = {
2636 .name = "update_fw",
2637 .mode = S_IWUSR,
2638 },
2639 .store = ipr_store_update_fw
2640};
2641
2642static struct class_device_attribute *ipr_ioa_attrs[] = {
2643 &ipr_fw_version_attr,
2644 &ipr_log_level_attr,
2645 &ipr_diagnostics_attr,
f37eb54b 2646 &ipr_ioa_state_attr,
1da177e4
LT
2647 &ipr_ioa_reset_attr,
2648 &ipr_update_fw_attr,
62275040 2649 &ipr_ioa_cache_attr,
1da177e4
LT
2650 NULL,
2651};
2652
2653#ifdef CONFIG_SCSI_IPR_DUMP
2654/**
2655 * ipr_read_dump - Dump the adapter
2656 * @kobj: kobject struct
2657 * @buf: buffer
2658 * @off: offset
2659 * @count: buffer size
2660 *
2661 * Return value:
2662 * number of bytes printed to buffer
2663 **/
2664static ssize_t ipr_read_dump(struct kobject *kobj, char *buf,
2665 loff_t off, size_t count)
2666{
2667 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2668 struct Scsi_Host *shost = class_to_shost(cdev);
2669 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2670 struct ipr_dump *dump;
2671 unsigned long lock_flags = 0;
2672 char *src;
2673 int len;
2674 size_t rc = count;
2675
2676 if (!capable(CAP_SYS_ADMIN))
2677 return -EACCES;
2678
2679 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2680 dump = ioa_cfg->dump;
2681
2682 if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
2683 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2684 return 0;
2685 }
2686 kref_get(&dump->kref);
2687 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2688
2689 if (off > dump->driver_dump.hdr.len) {
2690 kref_put(&dump->kref, ipr_release_dump);
2691 return 0;
2692 }
2693
2694 if (off + count > dump->driver_dump.hdr.len) {
2695 count = dump->driver_dump.hdr.len - off;
2696 rc = count;
2697 }
2698
2699 if (count && off < sizeof(dump->driver_dump)) {
2700 if (off + count > sizeof(dump->driver_dump))
2701 len = sizeof(dump->driver_dump) - off;
2702 else
2703 len = count;
2704 src = (u8 *)&dump->driver_dump + off;
2705 memcpy(buf, src, len);
2706 buf += len;
2707 off += len;
2708 count -= len;
2709 }
2710
2711 off -= sizeof(dump->driver_dump);
2712
2713 if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
2714 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
2715 len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
2716 else
2717 len = count;
2718 src = (u8 *)&dump->ioa_dump + off;
2719 memcpy(buf, src, len);
2720 buf += len;
2721 off += len;
2722 count -= len;
2723 }
2724
2725 off -= offsetof(struct ipr_ioa_dump, ioa_data);
2726
2727 while (count) {
2728 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
2729 len = PAGE_ALIGN(off) - off;
2730 else
2731 len = count;
2732 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
2733 src += off & ~PAGE_MASK;
2734 memcpy(buf, src, len);
2735 buf += len;
2736 off += len;
2737 count -= len;
2738 }
2739
2740 kref_put(&dump->kref, ipr_release_dump);
2741 return rc;
2742}
2743
2744/**
2745 * ipr_alloc_dump - Prepare for adapter dump
2746 * @ioa_cfg: ioa config struct
2747 *
2748 * Return value:
2749 * 0 on success / other on failure
2750 **/
2751static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
2752{
2753 struct ipr_dump *dump;
2754 unsigned long lock_flags = 0;
2755
2756 ENTER;
0bc42e35 2757 dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
1da177e4
LT
2758
2759 if (!dump) {
2760 ipr_err("Dump memory allocation failed\n");
2761 return -ENOMEM;
2762 }
2763
1da177e4
LT
2764 kref_init(&dump->kref);
2765 dump->ioa_cfg = ioa_cfg;
2766
2767 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2768
2769 if (INACTIVE != ioa_cfg->sdt_state) {
2770 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2771 kfree(dump);
2772 return 0;
2773 }
2774
2775 ioa_cfg->dump = dump;
2776 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
2777 if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
2778 ioa_cfg->dump_taken = 1;
2779 schedule_work(&ioa_cfg->work_q);
2780 }
2781 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2782
2783 LEAVE;
2784 return 0;
2785}
2786
2787/**
2788 * ipr_free_dump - Free adapter dump memory
2789 * @ioa_cfg: ioa config struct
2790 *
2791 * Return value:
2792 * 0 on success / other on failure
2793 **/
2794static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
2795{
2796 struct ipr_dump *dump;
2797 unsigned long lock_flags = 0;
2798
2799 ENTER;
2800
2801 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2802 dump = ioa_cfg->dump;
2803 if (!dump) {
2804 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2805 return 0;
2806 }
2807
2808 ioa_cfg->dump = NULL;
2809 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2810
2811 kref_put(&dump->kref, ipr_release_dump);
2812
2813 LEAVE;
2814 return 0;
2815}
2816
2817/**
2818 * ipr_write_dump - Setup dump state of adapter
2819 * @kobj: kobject struct
2820 * @buf: buffer
2821 * @off: offset
2822 * @count: buffer size
2823 *
2824 * Return value:
2825 * number of bytes printed to buffer
2826 **/
2827static ssize_t ipr_write_dump(struct kobject *kobj, char *buf,
2828 loff_t off, size_t count)
2829{
2830 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2831 struct Scsi_Host *shost = class_to_shost(cdev);
2832 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2833 int rc;
2834
2835 if (!capable(CAP_SYS_ADMIN))
2836 return -EACCES;
2837
2838 if (buf[0] == '1')
2839 rc = ipr_alloc_dump(ioa_cfg);
2840 else if (buf[0] == '0')
2841 rc = ipr_free_dump(ioa_cfg);
2842 else
2843 return -EINVAL;
2844
2845 if (rc)
2846 return rc;
2847 else
2848 return count;
2849}
2850
2851static struct bin_attribute ipr_dump_attr = {
2852 .attr = {
2853 .name = "dump",
2854 .mode = S_IRUSR | S_IWUSR,
2855 },
2856 .size = 0,
2857 .read = ipr_read_dump,
2858 .write = ipr_write_dump
2859};
2860#else
2861static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
2862#endif
2863
2864/**
2865 * ipr_change_queue_depth - Change the device's queue depth
2866 * @sdev: scsi device struct
2867 * @qdepth: depth to set
2868 *
2869 * Return value:
2870 * actual depth set
2871 **/
2872static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
2873{
2874 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2875 return sdev->queue_depth;
2876}
2877
2878/**
2879 * ipr_change_queue_type - Change the device's queue type
2880 * @dsev: scsi device struct
2881 * @tag_type: type of tags to use
2882 *
2883 * Return value:
2884 * actual queue type set
2885 **/
2886static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
2887{
2888 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2889 struct ipr_resource_entry *res;
2890 unsigned long lock_flags = 0;
2891
2892 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2893 res = (struct ipr_resource_entry *)sdev->hostdata;
2894
2895 if (res) {
2896 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
2897 /*
2898 * We don't bother quiescing the device here since the
2899 * adapter firmware does it for us.
2900 */
2901 scsi_set_tag_type(sdev, tag_type);
2902
2903 if (tag_type)
2904 scsi_activate_tcq(sdev, sdev->queue_depth);
2905 else
2906 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2907 } else
2908 tag_type = 0;
2909 } else
2910 tag_type = 0;
2911
2912 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2913 return tag_type;
2914}
2915
2916/**
2917 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
2918 * @dev: device struct
2919 * @buf: buffer
2920 *
2921 * Return value:
2922 * number of bytes printed to buffer
2923 **/
10523b3b 2924static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
2925{
2926 struct scsi_device *sdev = to_scsi_device(dev);
2927 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2928 struct ipr_resource_entry *res;
2929 unsigned long lock_flags = 0;
2930 ssize_t len = -ENXIO;
2931
2932 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2933 res = (struct ipr_resource_entry *)sdev->hostdata;
2934 if (res)
2935 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->cfgte.res_handle);
2936 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2937 return len;
2938}
2939
2940static struct device_attribute ipr_adapter_handle_attr = {
2941 .attr = {
2942 .name = "adapter_handle",
2943 .mode = S_IRUSR,
2944 },
2945 .show = ipr_show_adapter_handle
2946};
2947
2948static struct device_attribute *ipr_dev_attrs[] = {
2949 &ipr_adapter_handle_attr,
2950 NULL,
2951};
2952
2953/**
2954 * ipr_biosparam - Return the HSC mapping
2955 * @sdev: scsi device struct
2956 * @block_device: block device pointer
2957 * @capacity: capacity of the device
2958 * @parm: Array containing returned HSC values.
2959 *
2960 * This function generates the HSC parms that fdisk uses.
2961 * We want to make sure we return something that places partitions
2962 * on 4k boundaries for best performance with the IOA.
2963 *
2964 * Return value:
2965 * 0 on success
2966 **/
2967static int ipr_biosparam(struct scsi_device *sdev,
2968 struct block_device *block_device,
2969 sector_t capacity, int *parm)
2970{
2971 int heads, sectors;
2972 sector_t cylinders;
2973
2974 heads = 128;
2975 sectors = 32;
2976
2977 cylinders = capacity;
2978 sector_div(cylinders, (128 * 32));
2979
2980 /* return result */
2981 parm[0] = heads;
2982 parm[1] = sectors;
2983 parm[2] = cylinders;
2984
2985 return 0;
2986}
2987
2988/**
2989 * ipr_slave_destroy - Unconfigure a SCSI device
2990 * @sdev: scsi device struct
2991 *
2992 * Return value:
2993 * nothing
2994 **/
2995static void ipr_slave_destroy(struct scsi_device *sdev)
2996{
2997 struct ipr_resource_entry *res;
2998 struct ipr_ioa_cfg *ioa_cfg;
2999 unsigned long lock_flags = 0;
3000
3001 ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3002
3003 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3004 res = (struct ipr_resource_entry *) sdev->hostdata;
3005 if (res) {
3006 sdev->hostdata = NULL;
3007 res->sdev = NULL;
3008 }
3009 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3010}
3011
3012/**
3013 * ipr_slave_configure - Configure a SCSI device
3014 * @sdev: scsi device struct
3015 *
3016 * This function configures the specified scsi device.
3017 *
3018 * Return value:
3019 * 0 on success
3020 **/
3021static int ipr_slave_configure(struct scsi_device *sdev)
3022{
3023 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3024 struct ipr_resource_entry *res;
3025 unsigned long lock_flags = 0;
3026
3027 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3028 res = sdev->hostdata;
3029 if (res) {
3030 if (ipr_is_af_dasd_device(res))
3031 sdev->type = TYPE_RAID;
0726ce26 3032 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
1da177e4 3033 sdev->scsi_level = 4;
0726ce26
BK
3034 sdev->no_uld_attach = 1;
3035 }
1da177e4
LT
3036 if (ipr_is_vset_device(res)) {
3037 sdev->timeout = IPR_VSET_RW_TIMEOUT;
3038 blk_queue_max_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
3039 }
3040 if (IPR_IS_DASD_DEVICE(res->cfgte.std_inq_data))
3041 sdev->allow_restart = 1;
3042 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
3043 }
3044 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3045 return 0;
3046}
3047
3048/**
3049 * ipr_slave_alloc - Prepare for commands to a device.
3050 * @sdev: scsi device struct
3051 *
3052 * This function saves a pointer to the resource entry
3053 * in the scsi device struct if the device exists. We
3054 * can then use this pointer in ipr_queuecommand when
3055 * handling new commands.
3056 *
3057 * Return value:
692aebfc 3058 * 0 on success / -ENXIO if device does not exist
1da177e4
LT
3059 **/
3060static int ipr_slave_alloc(struct scsi_device *sdev)
3061{
3062 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
3063 struct ipr_resource_entry *res;
3064 unsigned long lock_flags;
692aebfc 3065 int rc = -ENXIO;
1da177e4
LT
3066
3067 sdev->hostdata = NULL;
3068
3069 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3070
3071 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3072 if ((res->cfgte.res_addr.bus == sdev->channel) &&
3073 (res->cfgte.res_addr.target == sdev->id) &&
3074 (res->cfgte.res_addr.lun == sdev->lun)) {
3075 res->sdev = sdev;
3076 res->add_to_ml = 0;
3077 res->in_erp = 0;
3078 sdev->hostdata = res;
3079 res->needs_sync_complete = 1;
692aebfc 3080 rc = 0;
1da177e4
LT
3081 break;
3082 }
3083 }
3084
3085 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3086
692aebfc 3087 return rc;
1da177e4
LT
3088}
3089
3090/**
3091 * ipr_eh_host_reset - Reset the host adapter
3092 * @scsi_cmd: scsi command struct
3093 *
3094 * Return value:
3095 * SUCCESS / FAILED
3096 **/
df0ae249 3097static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
1da177e4
LT
3098{
3099 struct ipr_ioa_cfg *ioa_cfg;
3100 int rc;
3101
3102 ENTER;
3103 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3104
3105 dev_err(&ioa_cfg->pdev->dev,
3106 "Adapter being reset as a result of error recovery.\n");
3107
3108 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3109 ioa_cfg->sdt_state = GET_DUMP;
3110
3111 rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
3112
3113 LEAVE;
3114 return rc;
3115}
3116
df0ae249
JG
3117static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
3118{
3119 int rc;
3120
3121 spin_lock_irq(cmd->device->host->host_lock);
3122 rc = __ipr_eh_host_reset(cmd);
3123 spin_unlock_irq(cmd->device->host->host_lock);
3124
3125 return rc;
3126}
3127
1da177e4
LT
3128/**
3129 * ipr_eh_dev_reset - Reset the device
3130 * @scsi_cmd: scsi command struct
3131 *
3132 * This function issues a device reset to the affected device.
3133 * A LUN reset will be sent to the device first. If that does
3134 * not work, a target reset will be sent.
3135 *
3136 * Return value:
3137 * SUCCESS / FAILED
3138 **/
94d0e7b8 3139static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
1da177e4
LT
3140{
3141 struct ipr_cmnd *ipr_cmd;
3142 struct ipr_ioa_cfg *ioa_cfg;
3143 struct ipr_resource_entry *res;
3144 struct ipr_cmd_pkt *cmd_pkt;
3145 u32 ioasc;
3146
3147 ENTER;
3148 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3149 res = scsi_cmd->device->hostdata;
3150
3151 if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
3152 return FAILED;
3153
3154 /*
3155 * If we are currently going through reset/reload, return failed. This will force the
3156 * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
3157 * reset to complete
3158 */
3159 if (ioa_cfg->in_reset_reload)
3160 return FAILED;
3161 if (ioa_cfg->ioa_is_dead)
3162 return FAILED;
3163
3164 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3165 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
3166 if (ipr_cmd->scsi_cmd)
3167 ipr_cmd->done = ipr_scsi_eh_done;
3168 }
3169 }
3170
3171 res->resetting_device = 1;
3172
3173 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3174
3175 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
3176 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3177 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3178 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
3179
3180 ipr_sdev_err(scsi_cmd->device, "Resetting device\n");
3181 ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3182
3183 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3184
3185 res->resetting_device = 0;
3186
3187 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3188
3189 LEAVE;
3190 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
3191}
3192
94d0e7b8
JG
3193static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
3194{
3195 int rc;
3196
3197 spin_lock_irq(cmd->device->host->host_lock);
3198 rc = __ipr_eh_dev_reset(cmd);
3199 spin_unlock_irq(cmd->device->host->host_lock);
3200
3201 return rc;
3202}
3203
1da177e4
LT
3204/**
3205 * ipr_bus_reset_done - Op done function for bus reset.
3206 * @ipr_cmd: ipr command struct
3207 *
3208 * This function is the op done function for a bus reset
3209 *
3210 * Return value:
3211 * none
3212 **/
3213static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
3214{
3215 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3216 struct ipr_resource_entry *res;
3217
3218 ENTER;
3219 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3220 if (!memcmp(&res->cfgte.res_handle, &ipr_cmd->ioarcb.res_handle,
3221 sizeof(res->cfgte.res_handle))) {
3222 scsi_report_bus_reset(ioa_cfg->host, res->cfgte.res_addr.bus);
3223 break;
3224 }
3225 }
3226
3227 /*
3228 * If abort has not completed, indicate the reset has, else call the
3229 * abort's done function to wake the sleeping eh thread
3230 */
3231 if (ipr_cmd->sibling->sibling)
3232 ipr_cmd->sibling->sibling = NULL;
3233 else
3234 ipr_cmd->sibling->done(ipr_cmd->sibling);
3235
3236 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3237 LEAVE;
3238}
3239
3240/**
3241 * ipr_abort_timeout - An abort task has timed out
3242 * @ipr_cmd: ipr command struct
3243 *
3244 * This function handles when an abort task times out. If this
3245 * happens we issue a bus reset since we have resources tied
3246 * up that must be freed before returning to the midlayer.
3247 *
3248 * Return value:
3249 * none
3250 **/
3251static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
3252{
3253 struct ipr_cmnd *reset_cmd;
3254 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3255 struct ipr_cmd_pkt *cmd_pkt;
3256 unsigned long lock_flags = 0;
3257
3258 ENTER;
3259 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3260 if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
3261 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3262 return;
3263 }
3264
3265 ipr_sdev_err(ipr_cmd->u.sdev, "Abort timed out. Resetting bus\n");
3266 reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3267 ipr_cmd->sibling = reset_cmd;
3268 reset_cmd->sibling = ipr_cmd;
3269 reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
3270 cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
3271 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3272 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
3273 cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
3274
3275 ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3276 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3277 LEAVE;
3278}
3279
3280/**
3281 * ipr_cancel_op - Cancel specified op
3282 * @scsi_cmd: scsi command struct
3283 *
3284 * This function cancels specified op.
3285 *
3286 * Return value:
3287 * SUCCESS / FAILED
3288 **/
3289static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
3290{
3291 struct ipr_cmnd *ipr_cmd;
3292 struct ipr_ioa_cfg *ioa_cfg;
3293 struct ipr_resource_entry *res;
3294 struct ipr_cmd_pkt *cmd_pkt;
3295 u32 ioasc;
3296 int op_found = 0;
3297
3298 ENTER;
3299 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3300 res = scsi_cmd->device->hostdata;
3301
8fa728a2
JG
3302 /* If we are currently going through reset/reload, return failed.
3303 * This will force the mid-layer to call ipr_eh_host_reset,
3304 * which will then go to sleep and wait for the reset to complete
3305 */
3306 if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
3307 return FAILED;
1da177e4
LT
3308 if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
3309 return FAILED;
3310
3311 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3312 if (ipr_cmd->scsi_cmd == scsi_cmd) {
3313 ipr_cmd->done = ipr_scsi_eh_done;
3314 op_found = 1;
3315 break;
3316 }
3317 }
3318
3319 if (!op_found)
3320 return SUCCESS;
3321
3322 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3323 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
3324 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3325 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3326 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
3327 ipr_cmd->u.sdev = scsi_cmd->device;
3328
3329 ipr_sdev_err(scsi_cmd->device, "Aborting command: %02X\n", scsi_cmd->cmnd[0]);
3330 ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
3331 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3332
3333 /*
3334 * If the abort task timed out and we sent a bus reset, we will get
3335 * one the following responses to the abort
3336 */
3337 if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
3338 ioasc = 0;
3339 ipr_trace;
3340 }
3341
3342 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3343 res->needs_sync_complete = 1;
3344
3345 LEAVE;
3346 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
3347}
3348
3349/**
3350 * ipr_eh_abort - Abort a single op
3351 * @scsi_cmd: scsi command struct
3352 *
3353 * Return value:
3354 * SUCCESS / FAILED
3355 **/
3356static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
3357{
8fa728a2
JG
3358 unsigned long flags;
3359 int rc;
1da177e4
LT
3360
3361 ENTER;
1da177e4 3362
8fa728a2
JG
3363 spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
3364 rc = ipr_cancel_op(scsi_cmd);
3365 spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
1da177e4
LT
3366
3367 LEAVE;
8fa728a2 3368 return rc;
1da177e4
LT
3369}
3370
3371/**
3372 * ipr_handle_other_interrupt - Handle "other" interrupts
3373 * @ioa_cfg: ioa config struct
3374 * @int_reg: interrupt register
3375 *
3376 * Return value:
3377 * IRQ_NONE / IRQ_HANDLED
3378 **/
3379static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
3380 volatile u32 int_reg)
3381{
3382 irqreturn_t rc = IRQ_HANDLED;
3383
3384 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
3385 /* Mask the interrupt */
3386 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
3387
3388 /* Clear the interrupt */
3389 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
3390 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
3391
3392 list_del(&ioa_cfg->reset_cmd->queue);
3393 del_timer(&ioa_cfg->reset_cmd->timer);
3394 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
3395 } else {
3396 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
3397 ioa_cfg->ioa_unit_checked = 1;
3398 else
3399 dev_err(&ioa_cfg->pdev->dev,
3400 "Permanent IOA failure. 0x%08X\n", int_reg);
3401
3402 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3403 ioa_cfg->sdt_state = GET_DUMP;
3404
3405 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
3406 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3407 }
3408
3409 return rc;
3410}
3411
3412/**
3413 * ipr_isr - Interrupt service routine
3414 * @irq: irq number
3415 * @devp: pointer to ioa config struct
3416 * @regs: pt_regs struct
3417 *
3418 * Return value:
3419 * IRQ_NONE / IRQ_HANDLED
3420 **/
3421static irqreturn_t ipr_isr(int irq, void *devp, struct pt_regs *regs)
3422{
3423 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
3424 unsigned long lock_flags = 0;
3425 volatile u32 int_reg, int_mask_reg;
3426 u32 ioasc;
3427 u16 cmd_index;
3428 struct ipr_cmnd *ipr_cmd;
3429 irqreturn_t rc = IRQ_NONE;
3430
3431 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3432
3433 /* If interrupts are disabled, ignore the interrupt */
3434 if (!ioa_cfg->allow_interrupts) {
3435 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3436 return IRQ_NONE;
3437 }
3438
3439 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
3440 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
3441
3442 /* If an interrupt on the adapter did not occur, ignore it */
3443 if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
3444 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3445 return IRQ_NONE;
3446 }
3447
3448 while (1) {
3449 ipr_cmd = NULL;
3450
3451 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
3452 ioa_cfg->toggle_bit) {
3453
3454 cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
3455 IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
3456
3457 if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
3458 ioa_cfg->errors_logged++;
3459 dev_err(&ioa_cfg->pdev->dev, "Invalid response handle from IOA\n");
3460
3461 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3462 ioa_cfg->sdt_state = GET_DUMP;
3463
3464 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3465 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3466 return IRQ_HANDLED;
3467 }
3468
3469 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
3470
3471 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3472
3473 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
3474
3475 list_del(&ipr_cmd->queue);
3476 del_timer(&ipr_cmd->timer);
3477 ipr_cmd->done(ipr_cmd);
3478
3479 rc = IRQ_HANDLED;
3480
3481 if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
3482 ioa_cfg->hrrq_curr++;
3483 } else {
3484 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
3485 ioa_cfg->toggle_bit ^= 1u;
3486 }
3487 }
3488
3489 if (ipr_cmd != NULL) {
3490 /* Clear the PCI interrupt */
3491 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg);
3492 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
3493 } else
3494 break;
3495 }
3496
3497 if (unlikely(rc == IRQ_NONE))
3498 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
3499
3500 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3501 return rc;
3502}
3503
3504/**
3505 * ipr_build_ioadl - Build a scatter/gather list and map the buffer
3506 * @ioa_cfg: ioa config struct
3507 * @ipr_cmd: ipr command struct
3508 *
3509 * Return value:
3510 * 0 on success / -1 on failure
3511 **/
3512static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
3513 struct ipr_cmnd *ipr_cmd)
3514{
3515 int i;
3516 struct scatterlist *sglist;
3517 u32 length;
3518 u32 ioadl_flags = 0;
3519 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3520 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3521 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
3522
3523 length = scsi_cmd->request_bufflen;
3524
3525 if (length == 0)
3526 return 0;
3527
3528 if (scsi_cmd->use_sg) {
3529 ipr_cmd->dma_use_sg = pci_map_sg(ioa_cfg->pdev,
3530 scsi_cmd->request_buffer,
3531 scsi_cmd->use_sg,
3532 scsi_cmd->sc_data_direction);
3533
3534 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
3535 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
3536 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3537 ioarcb->write_data_transfer_length = cpu_to_be32(length);
3538 ioarcb->write_ioadl_len =
3539 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3540 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
3541 ioadl_flags = IPR_IOADL_FLAGS_READ;
3542 ioarcb->read_data_transfer_length = cpu_to_be32(length);
3543 ioarcb->read_ioadl_len =
3544 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3545 }
3546
3547 sglist = scsi_cmd->request_buffer;
3548
3549 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3550 ioadl[i].flags_and_data_len =
3551 cpu_to_be32(ioadl_flags | sg_dma_len(&sglist[i]));
3552 ioadl[i].address =
3553 cpu_to_be32(sg_dma_address(&sglist[i]));
3554 }
3555
3556 if (likely(ipr_cmd->dma_use_sg)) {
3557 ioadl[i-1].flags_and_data_len |=
3558 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3559 return 0;
3560 } else
3561 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
3562 } else {
3563 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
3564 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
3565 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3566 ioarcb->write_data_transfer_length = cpu_to_be32(length);
3567 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3568 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
3569 ioadl_flags = IPR_IOADL_FLAGS_READ;
3570 ioarcb->read_data_transfer_length = cpu_to_be32(length);
3571 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3572 }
3573
3574 ipr_cmd->dma_handle = pci_map_single(ioa_cfg->pdev,
3575 scsi_cmd->request_buffer, length,
3576 scsi_cmd->sc_data_direction);
3577
3578 if (likely(!pci_dma_mapping_error(ipr_cmd->dma_handle))) {
3579 ipr_cmd->dma_use_sg = 1;
3580 ioadl[0].flags_and_data_len =
3581 cpu_to_be32(ioadl_flags | length | IPR_IOADL_FLAGS_LAST);
3582 ioadl[0].address = cpu_to_be32(ipr_cmd->dma_handle);
3583 return 0;
3584 } else
3585 dev_err(&ioa_cfg->pdev->dev, "pci_map_single failed!\n");
3586 }
3587
3588 return -1;
3589}
3590
3591/**
3592 * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
3593 * @scsi_cmd: scsi command struct
3594 *
3595 * Return value:
3596 * task attributes
3597 **/
3598static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
3599{
3600 u8 tag[2];
3601 u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
3602
3603 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
3604 switch (tag[0]) {
3605 case MSG_SIMPLE_TAG:
3606 rc = IPR_FLAGS_LO_SIMPLE_TASK;
3607 break;
3608 case MSG_HEAD_TAG:
3609 rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
3610 break;
3611 case MSG_ORDERED_TAG:
3612 rc = IPR_FLAGS_LO_ORDERED_TASK;
3613 break;
3614 };
3615 }
3616
3617 return rc;
3618}
3619
3620/**
3621 * ipr_erp_done - Process completion of ERP for a device
3622 * @ipr_cmd: ipr command struct
3623 *
3624 * This function copies the sense buffer into the scsi_cmd
3625 * struct and pushes the scsi_done function.
3626 *
3627 * Return value:
3628 * nothing
3629 **/
3630static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
3631{
3632 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3633 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3634 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3635 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3636
3637 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3638 scsi_cmd->result |= (DID_ERROR << 16);
3639 ipr_sdev_err(scsi_cmd->device,
3640 "Request Sense failed with IOASC: 0x%08X\n", ioasc);
3641 } else {
3642 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
3643 SCSI_SENSE_BUFFERSIZE);
3644 }
3645
3646 if (res) {
3647 res->needs_sync_complete = 1;
3648 res->in_erp = 0;
3649 }
3650 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3651 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3652 scsi_cmd->scsi_done(scsi_cmd);
3653}
3654
3655/**
3656 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
3657 * @ipr_cmd: ipr command struct
3658 *
3659 * Return value:
3660 * none
3661 **/
3662static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
3663{
3664 struct ipr_ioarcb *ioarcb;
3665 struct ipr_ioasa *ioasa;
3666
3667 ioarcb = &ipr_cmd->ioarcb;
3668 ioasa = &ipr_cmd->ioasa;
3669
3670 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
3671 ioarcb->write_data_transfer_length = 0;
3672 ioarcb->read_data_transfer_length = 0;
3673 ioarcb->write_ioadl_len = 0;
3674 ioarcb->read_ioadl_len = 0;
3675 ioasa->ioasc = 0;
3676 ioasa->residual_data_len = 0;
3677}
3678
3679/**
3680 * ipr_erp_request_sense - Send request sense to a device
3681 * @ipr_cmd: ipr command struct
3682 *
3683 * This function sends a request sense to a device as a result
3684 * of a check condition.
3685 *
3686 * Return value:
3687 * nothing
3688 **/
3689static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
3690{
3691 struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3692 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3693
3694 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3695 ipr_erp_done(ipr_cmd);
3696 return;
3697 }
3698
3699 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3700
3701 cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
3702 cmd_pkt->cdb[0] = REQUEST_SENSE;
3703 cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
3704 cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
3705 cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
3706 cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
3707
3708 ipr_cmd->ioadl[0].flags_and_data_len =
3709 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | SCSI_SENSE_BUFFERSIZE);
3710 ipr_cmd->ioadl[0].address =
3711 cpu_to_be32(ipr_cmd->sense_buffer_dma);
3712
3713 ipr_cmd->ioarcb.read_ioadl_len =
3714 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3715 ipr_cmd->ioarcb.read_data_transfer_length =
3716 cpu_to_be32(SCSI_SENSE_BUFFERSIZE);
3717
3718 ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
3719 IPR_REQUEST_SENSE_TIMEOUT * 2);
3720}
3721
3722/**
3723 * ipr_erp_cancel_all - Send cancel all to a device
3724 * @ipr_cmd: ipr command struct
3725 *
3726 * This function sends a cancel all to a device to clear the
3727 * queue. If we are running TCQ on the device, QERR is set to 1,
3728 * which means all outstanding ops have been dropped on the floor.
3729 * Cancel all will return them to us.
3730 *
3731 * Return value:
3732 * nothing
3733 **/
3734static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
3735{
3736 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3737 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3738 struct ipr_cmd_pkt *cmd_pkt;
3739
3740 res->in_erp = 1;
3741
3742 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3743
3744 if (!scsi_get_tag_type(scsi_cmd->device)) {
3745 ipr_erp_request_sense(ipr_cmd);
3746 return;
3747 }
3748
3749 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3750 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3751 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
3752
3753 ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
3754 IPR_CANCEL_ALL_TIMEOUT);
3755}
3756
3757/**
3758 * ipr_dump_ioasa - Dump contents of IOASA
3759 * @ioa_cfg: ioa config struct
3760 * @ipr_cmd: ipr command struct
3761 *
3762 * This function is invoked by the interrupt handler when ops
3763 * fail. It will log the IOASA if appropriate. Only called
3764 * for GPDD ops.
3765 *
3766 * Return value:
3767 * none
3768 **/
3769static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
3770 struct ipr_cmnd *ipr_cmd)
3771{
3772 int i;
3773 u16 data_len;
3774 u32 ioasc;
3775 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
3776 __be32 *ioasa_data = (__be32 *)ioasa;
3777 int error_index;
3778
3779 ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
3780
3781 if (0 == ioasc)
3782 return;
3783
3784 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
3785 return;
3786
3787 error_index = ipr_get_error(ioasc);
3788
3789 if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
3790 /* Don't log an error if the IOA already logged one */
3791 if (ioasa->ilid != 0)
3792 return;
3793
3794 if (ipr_error_table[error_index].log_ioasa == 0)
3795 return;
3796 }
3797
3798 ipr_sdev_err(ipr_cmd->scsi_cmd->device, "%s\n",
3799 ipr_error_table[error_index].error);
3800
3801 if ((ioasa->u.gpdd.end_state <= ARRAY_SIZE(ipr_gpdd_dev_end_states)) &&
3802 (ioasa->u.gpdd.bus_phase <= ARRAY_SIZE(ipr_gpdd_dev_bus_phases))) {
3803 ipr_sdev_err(ipr_cmd->scsi_cmd->device,
3804 "Device End state: %s Phase: %s\n",
3805 ipr_gpdd_dev_end_states[ioasa->u.gpdd.end_state],
3806 ipr_gpdd_dev_bus_phases[ioasa->u.gpdd.bus_phase]);
3807 }
3808
3809 if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
3810 data_len = sizeof(struct ipr_ioasa);
3811 else
3812 data_len = be16_to_cpu(ioasa->ret_stat_len);
3813
3814 ipr_err("IOASA Dump:\n");
3815
3816 for (i = 0; i < data_len / 4; i += 4) {
3817 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
3818 be32_to_cpu(ioasa_data[i]),
3819 be32_to_cpu(ioasa_data[i+1]),
3820 be32_to_cpu(ioasa_data[i+2]),
3821 be32_to_cpu(ioasa_data[i+3]));
3822 }
3823}
3824
3825/**
3826 * ipr_gen_sense - Generate SCSI sense data from an IOASA
3827 * @ioasa: IOASA
3828 * @sense_buf: sense data buffer
3829 *
3830 * Return value:
3831 * none
3832 **/
3833static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
3834{
3835 u32 failing_lba;
3836 u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
3837 struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
3838 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
3839 u32 ioasc = be32_to_cpu(ioasa->ioasc);
3840
3841 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
3842
3843 if (ioasc >= IPR_FIRST_DRIVER_IOASC)
3844 return;
3845
3846 ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
3847
3848 if (ipr_is_vset_device(res) &&
3849 ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
3850 ioasa->u.vset.failing_lba_hi != 0) {
3851 sense_buf[0] = 0x72;
3852 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
3853 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
3854 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
3855
3856 sense_buf[7] = 12;
3857 sense_buf[8] = 0;
3858 sense_buf[9] = 0x0A;
3859 sense_buf[10] = 0x80;
3860
3861 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
3862
3863 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
3864 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
3865 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
3866 sense_buf[15] = failing_lba & 0x000000ff;
3867
3868 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
3869
3870 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
3871 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
3872 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
3873 sense_buf[19] = failing_lba & 0x000000ff;
3874 } else {
3875 sense_buf[0] = 0x70;
3876 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
3877 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
3878 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
3879
3880 /* Illegal request */
3881 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
3882 (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
3883 sense_buf[7] = 10; /* additional length */
3884
3885 /* IOARCB was in error */
3886 if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
3887 sense_buf[15] = 0xC0;
3888 else /* Parameter data was invalid */
3889 sense_buf[15] = 0x80;
3890
3891 sense_buf[16] =
3892 ((IPR_FIELD_POINTER_MASK &
3893 be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
3894 sense_buf[17] =
3895 (IPR_FIELD_POINTER_MASK &
3896 be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
3897 } else {
3898 if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
3899 if (ipr_is_vset_device(res))
3900 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
3901 else
3902 failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
3903
3904 sense_buf[0] |= 0x80; /* Or in the Valid bit */
3905 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
3906 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
3907 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
3908 sense_buf[6] = failing_lba & 0x000000ff;
3909 }
3910
3911 sense_buf[7] = 6; /* additional length */
3912 }
3913 }
3914}
3915
3916/**
3917 * ipr_erp_start - Process an error response for a SCSI op
3918 * @ioa_cfg: ioa config struct
3919 * @ipr_cmd: ipr command struct
3920 *
3921 * This function determines whether or not to initiate ERP
3922 * on the affected device.
3923 *
3924 * Return value:
3925 * nothing
3926 **/
3927static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
3928 struct ipr_cmnd *ipr_cmd)
3929{
3930 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3931 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3932 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3933
3934 if (!res) {
3935 ipr_scsi_eh_done(ipr_cmd);
3936 return;
3937 }
3938
3939 if (ipr_is_gscsi(res))
3940 ipr_dump_ioasa(ioa_cfg, ipr_cmd);
3941 else
3942 ipr_gen_sense(ipr_cmd);
3943
3944 switch (ioasc & IPR_IOASC_IOASC_MASK) {
3945 case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
3946 scsi_cmd->result |= (DID_IMM_RETRY << 16);
3947 break;
3948 case IPR_IOASC_IR_RESOURCE_HANDLE:
b0df54bb 3949 case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
1da177e4
LT
3950 scsi_cmd->result |= (DID_NO_CONNECT << 16);
3951 break;
3952 case IPR_IOASC_HW_SEL_TIMEOUT:
3953 scsi_cmd->result |= (DID_NO_CONNECT << 16);
3954 res->needs_sync_complete = 1;
3955 break;
3956 case IPR_IOASC_SYNC_REQUIRED:
3957 if (!res->in_erp)
3958 res->needs_sync_complete = 1;
3959 scsi_cmd->result |= (DID_IMM_RETRY << 16);
3960 break;
3961 case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
b0df54bb 3962 case IPR_IOASA_IR_DUAL_IOA_DISABLED:
1da177e4
LT
3963 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
3964 break;
3965 case IPR_IOASC_BUS_WAS_RESET:
3966 case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
3967 /*
3968 * Report the bus reset and ask for a retry. The device
3969 * will give CC/UA the next command.
3970 */
3971 if (!res->resetting_device)
3972 scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
3973 scsi_cmd->result |= (DID_ERROR << 16);
3974 res->needs_sync_complete = 1;
3975 break;
3976 case IPR_IOASC_HW_DEV_BUS_STATUS:
3977 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
3978 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
3979 ipr_erp_cancel_all(ipr_cmd);
3980 return;
3981 }
3982 res->needs_sync_complete = 1;
3983 break;
3984 case IPR_IOASC_NR_INIT_CMD_REQUIRED:
3985 break;
3986 default:
3987 scsi_cmd->result |= (DID_ERROR << 16);
3988 if (!ipr_is_vset_device(res))
3989 res->needs_sync_complete = 1;
3990 break;
3991 }
3992
3993 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3994 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3995 scsi_cmd->scsi_done(scsi_cmd);
3996}
3997
3998/**
3999 * ipr_scsi_done - mid-layer done function
4000 * @ipr_cmd: ipr command struct
4001 *
4002 * This function is invoked by the interrupt handler for
4003 * ops generated by the SCSI mid-layer
4004 *
4005 * Return value:
4006 * none
4007 **/
4008static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
4009{
4010 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4011 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
4012 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
4013
4014 scsi_cmd->resid = be32_to_cpu(ipr_cmd->ioasa.residual_data_len);
4015
4016 if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
4017 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
4018 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4019 scsi_cmd->scsi_done(scsi_cmd);
4020 } else
4021 ipr_erp_start(ioa_cfg, ipr_cmd);
4022}
4023
4024/**
4025 * ipr_save_ioafp_mode_select - Save adapters mode select data
4026 * @ioa_cfg: ioa config struct
4027 * @scsi_cmd: scsi command struct
4028 *
4029 * This function saves mode select data for the adapter to
4030 * use following an adapter reset.
4031 *
4032 * Return value:
4033 * 0 on success / SCSI_MLQUEUE_HOST_BUSY on failure
4034 **/
4035static int ipr_save_ioafp_mode_select(struct ipr_ioa_cfg *ioa_cfg,
4036 struct scsi_cmnd *scsi_cmd)
4037{
4038 if (!ioa_cfg->saved_mode_pages) {
4039 ioa_cfg->saved_mode_pages = kmalloc(sizeof(struct ipr_mode_pages),
4040 GFP_ATOMIC);
4041 if (!ioa_cfg->saved_mode_pages) {
4042 dev_err(&ioa_cfg->pdev->dev,
4043 "IOA mode select buffer allocation failed\n");
4044 return SCSI_MLQUEUE_HOST_BUSY;
4045 }
4046 }
4047
4048 memcpy(ioa_cfg->saved_mode_pages, scsi_cmd->buffer, scsi_cmd->cmnd[4]);
4049 ioa_cfg->saved_mode_page_len = scsi_cmd->cmnd[4];
4050 return 0;
4051}
4052
4053/**
4054 * ipr_queuecommand - Queue a mid-layer request
4055 * @scsi_cmd: scsi command struct
4056 * @done: done function
4057 *
4058 * This function queues a request generated by the mid-layer.
4059 *
4060 * Return value:
4061 * 0 on success
4062 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
4063 * SCSI_MLQUEUE_HOST_BUSY if host is busy
4064 **/
4065static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
4066 void (*done) (struct scsi_cmnd *))
4067{
4068 struct ipr_ioa_cfg *ioa_cfg;
4069 struct ipr_resource_entry *res;
4070 struct ipr_ioarcb *ioarcb;
4071 struct ipr_cmnd *ipr_cmd;
4072 int rc = 0;
4073
4074 scsi_cmd->scsi_done = done;
4075 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
4076 res = scsi_cmd->device->hostdata;
4077 scsi_cmd->result = (DID_OK << 16);
4078
4079 /*
4080 * We are currently blocking all devices due to a host reset
4081 * We have told the host to stop giving us new requests, but
4082 * ERP ops don't count. FIXME
4083 */
4084 if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
4085 return SCSI_MLQUEUE_HOST_BUSY;
4086
4087 /*
4088 * FIXME - Create scsi_set_host_offline interface
4089 * and the ioa_is_dead check can be removed
4090 */
4091 if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
4092 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
4093 scsi_cmd->result = (DID_NO_CONNECT << 16);
4094 scsi_cmd->scsi_done(scsi_cmd);
4095 return 0;
4096 }
4097
4098 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
4099 ioarcb = &ipr_cmd->ioarcb;
4100 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
4101
4102 memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
4103 ipr_cmd->scsi_cmd = scsi_cmd;
4104 ioarcb->res_handle = res->cfgte.res_handle;
4105 ipr_cmd->done = ipr_scsi_done;
4106 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
4107
4108 if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
4109 if (scsi_cmd->underflow == 0)
4110 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
4111
4112 if (res->needs_sync_complete) {
4113 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
4114 res->needs_sync_complete = 0;
4115 }
4116
4117 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
4118 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
4119 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
4120 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
4121 }
4122
4123 if (scsi_cmd->cmnd[0] >= 0xC0 &&
4124 (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
4125 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4126
4127 if (ipr_is_ioa_resource(res) && scsi_cmd->cmnd[0] == MODE_SELECT)
4128 rc = ipr_save_ioafp_mode_select(ioa_cfg, scsi_cmd);
4129
4130 if (likely(rc == 0))
4131 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
4132
4133 if (likely(rc == 0)) {
4134 mb();
4135 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
4136 ioa_cfg->regs.ioarrin_reg);
4137 } else {
4138 list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4139 return SCSI_MLQUEUE_HOST_BUSY;
4140 }
4141
4142 return 0;
4143}
4144
4145/**
4146 * ipr_info - Get information about the card/driver
4147 * @scsi_host: scsi host struct
4148 *
4149 * Return value:
4150 * pointer to buffer with description string
4151 **/
4152static const char * ipr_ioa_info(struct Scsi_Host *host)
4153{
4154 static char buffer[512];
4155 struct ipr_ioa_cfg *ioa_cfg;
4156 unsigned long lock_flags = 0;
4157
4158 ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
4159
4160 spin_lock_irqsave(host->host_lock, lock_flags);
4161 sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
4162 spin_unlock_irqrestore(host->host_lock, lock_flags);
4163
4164 return buffer;
4165}
4166
4167static struct scsi_host_template driver_template = {
4168 .module = THIS_MODULE,
4169 .name = "IPR",
4170 .info = ipr_ioa_info,
4171 .queuecommand = ipr_queuecommand,
4172 .eh_abort_handler = ipr_eh_abort,
4173 .eh_device_reset_handler = ipr_eh_dev_reset,
4174 .eh_host_reset_handler = ipr_eh_host_reset,
4175 .slave_alloc = ipr_slave_alloc,
4176 .slave_configure = ipr_slave_configure,
4177 .slave_destroy = ipr_slave_destroy,
4178 .change_queue_depth = ipr_change_queue_depth,
4179 .change_queue_type = ipr_change_queue_type,
4180 .bios_param = ipr_biosparam,
4181 .can_queue = IPR_MAX_COMMANDS,
4182 .this_id = -1,
4183 .sg_tablesize = IPR_MAX_SGLIST,
4184 .max_sectors = IPR_IOA_MAX_SECTORS,
4185 .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
4186 .use_clustering = ENABLE_CLUSTERING,
4187 .shost_attrs = ipr_ioa_attrs,
4188 .sdev_attrs = ipr_dev_attrs,
4189 .proc_name = IPR_NAME
4190};
4191
4192#ifdef CONFIG_PPC_PSERIES
4193static const u16 ipr_blocked_processors[] = {
4194 PV_NORTHSTAR,
4195 PV_PULSAR,
4196 PV_POWER4,
4197 PV_ICESTAR,
4198 PV_SSTAR,
4199 PV_POWER4p,
4200 PV_630,
4201 PV_630p
4202};
4203
4204/**
4205 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
4206 * @ioa_cfg: ioa cfg struct
4207 *
4208 * Adapters that use Gemstone revision < 3.1 do not work reliably on
4209 * certain pSeries hardware. This function determines if the given
4210 * adapter is in one of these confgurations or not.
4211 *
4212 * Return value:
4213 * 1 if adapter is not supported / 0 if adapter is supported
4214 **/
4215static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
4216{
4217 u8 rev_id;
4218 int i;
4219
4220 if (ioa_cfg->type == 0x5702) {
4221 if (pci_read_config_byte(ioa_cfg->pdev, PCI_REVISION_ID,
4222 &rev_id) == PCIBIOS_SUCCESSFUL) {
4223 if (rev_id < 4) {
4224 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
4225 if (__is_processor(ipr_blocked_processors[i]))
4226 return 1;
4227 }
4228 }
4229 }
4230 }
4231 return 0;
4232}
4233#else
4234#define ipr_invalid_adapter(ioa_cfg) 0
4235#endif
4236
4237/**
4238 * ipr_ioa_bringdown_done - IOA bring down completion.
4239 * @ipr_cmd: ipr command struct
4240 *
4241 * This function processes the completion of an adapter bring down.
4242 * It wakes any reset sleepers.
4243 *
4244 * Return value:
4245 * IPR_RC_JOB_RETURN
4246 **/
4247static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
4248{
4249 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4250
4251 ENTER;
4252 ioa_cfg->in_reset_reload = 0;
4253 ioa_cfg->reset_retries = 0;
4254 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4255 wake_up_all(&ioa_cfg->reset_wait_q);
4256
4257 spin_unlock_irq(ioa_cfg->host->host_lock);
4258 scsi_unblock_requests(ioa_cfg->host);
4259 spin_lock_irq(ioa_cfg->host->host_lock);
4260 LEAVE;
4261
4262 return IPR_RC_JOB_RETURN;
4263}
4264
4265/**
4266 * ipr_ioa_reset_done - IOA reset completion.
4267 * @ipr_cmd: ipr command struct
4268 *
4269 * This function processes the completion of an adapter reset.
4270 * It schedules any necessary mid-layer add/removes and
4271 * wakes any reset sleepers.
4272 *
4273 * Return value:
4274 * IPR_RC_JOB_RETURN
4275 **/
4276static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
4277{
4278 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4279 struct ipr_resource_entry *res;
4280 struct ipr_hostrcb *hostrcb, *temp;
4281 int i = 0;
4282
4283 ENTER;
4284 ioa_cfg->in_reset_reload = 0;
4285 ioa_cfg->allow_cmds = 1;
4286 ioa_cfg->reset_cmd = NULL;
3d1d0da6 4287 ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
1da177e4
LT
4288
4289 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4290 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
4291 ipr_trace;
4292 break;
4293 }
4294 }
4295 schedule_work(&ioa_cfg->work_q);
4296
4297 list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
4298 list_del(&hostrcb->queue);
4299 if (i++ < IPR_NUM_LOG_HCAMS)
4300 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
4301 else
4302 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
4303 }
4304
4305 dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
4306
4307 ioa_cfg->reset_retries = 0;
4308 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4309 wake_up_all(&ioa_cfg->reset_wait_q);
4310
4311 spin_unlock_irq(ioa_cfg->host->host_lock);
4312 scsi_unblock_requests(ioa_cfg->host);
4313 spin_lock_irq(ioa_cfg->host->host_lock);
4314
4315 if (!ioa_cfg->allow_cmds)
4316 scsi_block_requests(ioa_cfg->host);
4317
4318 LEAVE;
4319 return IPR_RC_JOB_RETURN;
4320}
4321
4322/**
4323 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
4324 * @supported_dev: supported device struct
4325 * @vpids: vendor product id struct
4326 *
4327 * Return value:
4328 * none
4329 **/
4330static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
4331 struct ipr_std_inq_vpids *vpids)
4332{
4333 memset(supported_dev, 0, sizeof(struct ipr_supported_device));
4334 memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
4335 supported_dev->num_records = 1;
4336 supported_dev->data_length =
4337 cpu_to_be16(sizeof(struct ipr_supported_device));
4338 supported_dev->reserved = 0;
4339}
4340
4341/**
4342 * ipr_set_supported_devs - Send Set Supported Devices for a device
4343 * @ipr_cmd: ipr command struct
4344 *
4345 * This function send a Set Supported Devices to the adapter
4346 *
4347 * Return value:
4348 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4349 **/
4350static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
4351{
4352 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4353 struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
4354 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4355 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4356 struct ipr_resource_entry *res = ipr_cmd->u.res;
4357
4358 ipr_cmd->job_step = ipr_ioa_reset_done;
4359
4360 list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
d0ad6f50 4361 if (!IPR_IS_DASD_DEVICE(res->cfgte.std_inq_data))
1da177e4
LT
4362 continue;
4363
4364 ipr_cmd->u.res = res;
4365 ipr_set_sup_dev_dflt(supp_dev, &res->cfgte.std_inq_data.vpids);
4366
4367 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4368 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4369 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4370
4371 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
4372 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
4373 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
4374
4375 ioadl->flags_and_data_len = cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST |
4376 sizeof(struct ipr_supported_device));
4377 ioadl->address = cpu_to_be32(ioa_cfg->vpd_cbs_dma +
4378 offsetof(struct ipr_misc_cbs, supp_dev));
4379 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4380 ioarcb->write_data_transfer_length =
4381 cpu_to_be32(sizeof(struct ipr_supported_device));
4382
4383 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
4384 IPR_SET_SUP_DEVICE_TIMEOUT);
4385
4386 ipr_cmd->job_step = ipr_set_supported_devs;
4387 return IPR_RC_JOB_RETURN;
4388 }
4389
4390 return IPR_RC_JOB_CONTINUE;
4391}
4392
62275040
BK
4393/**
4394 * ipr_setup_write_cache - Disable write cache if needed
4395 * @ipr_cmd: ipr command struct
4396 *
4397 * This function sets up adapters write cache to desired setting
4398 *
4399 * Return value:
4400 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4401 **/
4402static int ipr_setup_write_cache(struct ipr_cmnd *ipr_cmd)
4403{
4404 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4405
4406 ipr_cmd->job_step = ipr_set_supported_devs;
4407 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
4408 struct ipr_resource_entry, queue);
4409
4410 if (ioa_cfg->cache_state != CACHE_DISABLED)
4411 return IPR_RC_JOB_CONTINUE;
4412
4413 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4414 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4415 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
4416 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
4417
4418 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4419
4420 return IPR_RC_JOB_RETURN;
4421}
4422
1da177e4
LT
4423/**
4424 * ipr_get_mode_page - Locate specified mode page
4425 * @mode_pages: mode page buffer
4426 * @page_code: page code to find
4427 * @len: minimum required length for mode page
4428 *
4429 * Return value:
4430 * pointer to mode page / NULL on failure
4431 **/
4432static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
4433 u32 page_code, u32 len)
4434{
4435 struct ipr_mode_page_hdr *mode_hdr;
4436 u32 page_length;
4437 u32 length;
4438
4439 if (!mode_pages || (mode_pages->hdr.length == 0))
4440 return NULL;
4441
4442 length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
4443 mode_hdr = (struct ipr_mode_page_hdr *)
4444 (mode_pages->data + mode_pages->hdr.block_desc_len);
4445
4446 while (length) {
4447 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
4448 if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
4449 return mode_hdr;
4450 break;
4451 } else {
4452 page_length = (sizeof(struct ipr_mode_page_hdr) +
4453 mode_hdr->page_length);
4454 length -= page_length;
4455 mode_hdr = (struct ipr_mode_page_hdr *)
4456 ((unsigned long)mode_hdr + page_length);
4457 }
4458 }
4459 return NULL;
4460}
4461
4462/**
4463 * ipr_check_term_power - Check for term power errors
4464 * @ioa_cfg: ioa config struct
4465 * @mode_pages: IOAFP mode pages buffer
4466 *
4467 * Check the IOAFP's mode page 28 for term power errors
4468 *
4469 * Return value:
4470 * nothing
4471 **/
4472static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
4473 struct ipr_mode_pages *mode_pages)
4474{
4475 int i;
4476 int entry_length;
4477 struct ipr_dev_bus_entry *bus;
4478 struct ipr_mode_page28 *mode_page;
4479
4480 mode_page = ipr_get_mode_page(mode_pages, 0x28,
4481 sizeof(struct ipr_mode_page28));
4482
4483 entry_length = mode_page->entry_length;
4484
4485 bus = mode_page->bus;
4486
4487 for (i = 0; i < mode_page->num_entries; i++) {
4488 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
4489 dev_err(&ioa_cfg->pdev->dev,
4490 "Term power is absent on scsi bus %d\n",
4491 bus->res_addr.bus);
4492 }
4493
4494 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
4495 }
4496}
4497
4498/**
4499 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
4500 * @ioa_cfg: ioa config struct
4501 *
4502 * Looks through the config table checking for SES devices. If
4503 * the SES device is in the SES table indicating a maximum SCSI
4504 * bus speed, the speed is limited for the bus.
4505 *
4506 * Return value:
4507 * none
4508 **/
4509static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
4510{
4511 u32 max_xfer_rate;
4512 int i;
4513
4514 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
4515 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
4516 ioa_cfg->bus_attr[i].bus_width);
4517
4518 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
4519 ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
4520 }
4521}
4522
4523/**
4524 * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
4525 * @ioa_cfg: ioa config struct
4526 * @mode_pages: mode page 28 buffer
4527 *
4528 * Updates mode page 28 based on driver configuration
4529 *
4530 * Return value:
4531 * none
4532 **/
4533static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
4534 struct ipr_mode_pages *mode_pages)
4535{
4536 int i, entry_length;
4537 struct ipr_dev_bus_entry *bus;
4538 struct ipr_bus_attributes *bus_attr;
4539 struct ipr_mode_page28 *mode_page;
4540
4541 mode_page = ipr_get_mode_page(mode_pages, 0x28,
4542 sizeof(struct ipr_mode_page28));
4543
4544 entry_length = mode_page->entry_length;
4545
4546 /* Loop for each device bus entry */
4547 for (i = 0, bus = mode_page->bus;
4548 i < mode_page->num_entries;
4549 i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
4550 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
4551 dev_err(&ioa_cfg->pdev->dev,
4552 "Invalid resource address reported: 0x%08X\n",
4553 IPR_GET_PHYS_LOC(bus->res_addr));
4554 continue;
4555 }
4556
4557 bus_attr = &ioa_cfg->bus_attr[i];
4558 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
4559 bus->bus_width = bus_attr->bus_width;
4560 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
4561 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
4562 if (bus_attr->qas_enabled)
4563 bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
4564 else
4565 bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
4566 }
4567}
4568
4569/**
4570 * ipr_build_mode_select - Build a mode select command
4571 * @ipr_cmd: ipr command struct
4572 * @res_handle: resource handle to send command to
4573 * @parm: Byte 2 of Mode Sense command
4574 * @dma_addr: DMA buffer address
4575 * @xfer_len: data transfer length
4576 *
4577 * Return value:
4578 * none
4579 **/
4580static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
4581 __be32 res_handle, u8 parm, u32 dma_addr,
4582 u8 xfer_len)
4583{
4584 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4585 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4586
4587 ioarcb->res_handle = res_handle;
4588 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4589 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4590 ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
4591 ioarcb->cmd_pkt.cdb[1] = parm;
4592 ioarcb->cmd_pkt.cdb[4] = xfer_len;
4593
4594 ioadl->flags_and_data_len =
4595 cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST | xfer_len);
4596 ioadl->address = cpu_to_be32(dma_addr);
4597 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4598 ioarcb->write_data_transfer_length = cpu_to_be32(xfer_len);
4599}
4600
4601/**
4602 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
4603 * @ipr_cmd: ipr command struct
4604 *
4605 * This function sets up the SCSI bus attributes and sends
4606 * a Mode Select for Page 28 to activate them.
4607 *
4608 * Return value:
4609 * IPR_RC_JOB_RETURN
4610 **/
4611static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
4612{
4613 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4614 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
4615 int length;
4616
4617 ENTER;
4618 if (ioa_cfg->saved_mode_pages) {
4619 memcpy(mode_pages, ioa_cfg->saved_mode_pages,
4620 ioa_cfg->saved_mode_page_len);
4621 length = ioa_cfg->saved_mode_page_len;
4622 } else {
4623 ipr_scsi_bus_speed_limit(ioa_cfg);
4624 ipr_check_term_power(ioa_cfg, mode_pages);
4625 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
4626 length = mode_pages->hdr.length + 1;
4627 mode_pages->hdr.length = 0;
4628 }
4629
4630 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
4631 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
4632 length);
4633
62275040 4634 ipr_cmd->job_step = ipr_setup_write_cache;
1da177e4
LT
4635 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4636
4637 LEAVE;
4638 return IPR_RC_JOB_RETURN;
4639}
4640
4641/**
4642 * ipr_build_mode_sense - Builds a mode sense command
4643 * @ipr_cmd: ipr command struct
4644 * @res: resource entry struct
4645 * @parm: Byte 2 of mode sense command
4646 * @dma_addr: DMA address of mode sense buffer
4647 * @xfer_len: Size of DMA buffer
4648 *
4649 * Return value:
4650 * none
4651 **/
4652static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
4653 __be32 res_handle,
4654 u8 parm, u32 dma_addr, u8 xfer_len)
4655{
4656 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4657 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4658
4659 ioarcb->res_handle = res_handle;
4660 ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
4661 ioarcb->cmd_pkt.cdb[2] = parm;
4662 ioarcb->cmd_pkt.cdb[4] = xfer_len;
4663 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4664
4665 ioadl->flags_and_data_len =
4666 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
4667 ioadl->address = cpu_to_be32(dma_addr);
4668 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4669 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
4670}
4671
4672/**
4673 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
4674 * @ipr_cmd: ipr command struct
4675 *
4676 * This function send a Page 28 mode sense to the IOA to
4677 * retrieve SCSI bus attributes.
4678 *
4679 * Return value:
4680 * IPR_RC_JOB_RETURN
4681 **/
4682static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
4683{
4684 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4685
4686 ENTER;
4687 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
4688 0x28, ioa_cfg->vpd_cbs_dma +
4689 offsetof(struct ipr_misc_cbs, mode_pages),
4690 sizeof(struct ipr_mode_pages));
4691
4692 ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
4693
4694 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4695
4696 LEAVE;
4697 return IPR_RC_JOB_RETURN;
4698}
4699
4700/**
4701 * ipr_init_res_table - Initialize the resource table
4702 * @ipr_cmd: ipr command struct
4703 *
4704 * This function looks through the existing resource table, comparing
4705 * it with the config table. This function will take care of old/new
4706 * devices and schedule adding/removing them from the mid-layer
4707 * as appropriate.
4708 *
4709 * Return value:
4710 * IPR_RC_JOB_CONTINUE
4711 **/
4712static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
4713{
4714 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4715 struct ipr_resource_entry *res, *temp;
4716 struct ipr_config_table_entry *cfgte;
4717 int found, i;
4718 LIST_HEAD(old_res);
4719
4720 ENTER;
4721 if (ioa_cfg->cfg_table->hdr.flags & IPR_UCODE_DOWNLOAD_REQ)
4722 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
4723
4724 list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
4725 list_move_tail(&res->queue, &old_res);
4726
4727 for (i = 0; i < ioa_cfg->cfg_table->hdr.num_entries; i++) {
4728 cfgte = &ioa_cfg->cfg_table->dev[i];
4729 found = 0;
4730
4731 list_for_each_entry_safe(res, temp, &old_res, queue) {
4732 if (!memcmp(&res->cfgte.res_addr,
4733 &cfgte->res_addr, sizeof(cfgte->res_addr))) {
4734 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4735 found = 1;
4736 break;
4737 }
4738 }
4739
4740 if (!found) {
4741 if (list_empty(&ioa_cfg->free_res_q)) {
4742 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
4743 break;
4744 }
4745
4746 found = 1;
4747 res = list_entry(ioa_cfg->free_res_q.next,
4748 struct ipr_resource_entry, queue);
4749 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4750 ipr_init_res_entry(res);
4751 res->add_to_ml = 1;
4752 }
4753
4754 if (found)
4755 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
4756 }
4757
4758 list_for_each_entry_safe(res, temp, &old_res, queue) {
4759 if (res->sdev) {
4760 res->del_from_ml = 1;
4761 res->sdev->hostdata = NULL;
4762 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4763 } else {
4764 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
4765 }
4766 }
4767
4768 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
4769
4770 LEAVE;
4771 return IPR_RC_JOB_CONTINUE;
4772}
4773
4774/**
4775 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
4776 * @ipr_cmd: ipr command struct
4777 *
4778 * This function sends a Query IOA Configuration command
4779 * to the adapter to retrieve the IOA configuration table.
4780 *
4781 * Return value:
4782 * IPR_RC_JOB_RETURN
4783 **/
4784static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
4785{
4786 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4787 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4788 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4789 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
4790
4791 ENTER;
4792 dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
4793 ucode_vpd->major_release, ucode_vpd->card_type,
4794 ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
4795 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4796 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4797
4798 ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
4799 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_config_table) >> 8) & 0xff;
4800 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_config_table) & 0xff;
4801
4802 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4803 ioarcb->read_data_transfer_length =
4804 cpu_to_be32(sizeof(struct ipr_config_table));
4805
4806 ioadl->address = cpu_to_be32(ioa_cfg->cfg_table_dma);
4807 ioadl->flags_and_data_len =
4808 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(struct ipr_config_table));
4809
4810 ipr_cmd->job_step = ipr_init_res_table;
4811
4812 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4813
4814 LEAVE;
4815 return IPR_RC_JOB_RETURN;
4816}
4817
4818/**
4819 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
4820 * @ipr_cmd: ipr command struct
4821 *
4822 * This utility function sends an inquiry to the adapter.
4823 *
4824 * Return value:
4825 * none
4826 **/
4827static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
4828 u32 dma_addr, u8 xfer_len)
4829{
4830 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4831 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4832
4833 ENTER;
4834 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4835 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4836
4837 ioarcb->cmd_pkt.cdb[0] = INQUIRY;
4838 ioarcb->cmd_pkt.cdb[1] = flags;
4839 ioarcb->cmd_pkt.cdb[2] = page;
4840 ioarcb->cmd_pkt.cdb[4] = xfer_len;
4841
4842 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4843 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
4844
4845 ioadl->address = cpu_to_be32(dma_addr);
4846 ioadl->flags_and_data_len =
4847 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
4848
4849 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4850 LEAVE;
4851}
4852
62275040
BK
4853/**
4854 * ipr_inquiry_page_supported - Is the given inquiry page supported
4855 * @page0: inquiry page 0 buffer
4856 * @page: page code.
4857 *
4858 * This function determines if the specified inquiry page is supported.
4859 *
4860 * Return value:
4861 * 1 if page is supported / 0 if not
4862 **/
4863static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
4864{
4865 int i;
4866
4867 for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
4868 if (page0->page[i] == page)
4869 return 1;
4870
4871 return 0;
4872}
4873
1da177e4
LT
4874/**
4875 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
4876 * @ipr_cmd: ipr command struct
4877 *
4878 * This function sends a Page 3 inquiry to the adapter
4879 * to retrieve software VPD information.
4880 *
4881 * Return value:
4882 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4883 **/
4884static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
62275040
BK
4885{
4886 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4887 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
4888
4889 ENTER;
4890
4891 if (!ipr_inquiry_page_supported(page0, 1))
4892 ioa_cfg->cache_state = CACHE_NONE;
4893
4894 ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
4895
4896 ipr_ioafp_inquiry(ipr_cmd, 1, 3,
4897 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
4898 sizeof(struct ipr_inquiry_page3));
4899
4900 LEAVE;
4901 return IPR_RC_JOB_RETURN;
4902}
4903
4904/**
4905 * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
4906 * @ipr_cmd: ipr command struct
4907 *
4908 * This function sends a Page 0 inquiry to the adapter
4909 * to retrieve supported inquiry pages.
4910 *
4911 * Return value:
4912 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4913 **/
4914static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
1da177e4
LT
4915{
4916 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4917 char type[5];
4918
4919 ENTER;
4920
4921 /* Grab the type out of the VPD and store it away */
4922 memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
4923 type[4] = '\0';
4924 ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
4925
62275040 4926 ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
1da177e4 4927
62275040
BK
4928 ipr_ioafp_inquiry(ipr_cmd, 1, 0,
4929 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
4930 sizeof(struct ipr_inquiry_page0));
1da177e4
LT
4931
4932 LEAVE;
4933 return IPR_RC_JOB_RETURN;
4934}
4935
4936/**
4937 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
4938 * @ipr_cmd: ipr command struct
4939 *
4940 * This function sends a standard inquiry to the adapter.
4941 *
4942 * Return value:
4943 * IPR_RC_JOB_RETURN
4944 **/
4945static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
4946{
4947 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4948
4949 ENTER;
62275040 4950 ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
1da177e4
LT
4951
4952 ipr_ioafp_inquiry(ipr_cmd, 0, 0,
4953 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
4954 sizeof(struct ipr_ioa_vpd));
4955
4956 LEAVE;
4957 return IPR_RC_JOB_RETURN;
4958}
4959
4960/**
4961 * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
4962 * @ipr_cmd: ipr command struct
4963 *
4964 * This function send an Identify Host Request Response Queue
4965 * command to establish the HRRQ with the adapter.
4966 *
4967 * Return value:
4968 * IPR_RC_JOB_RETURN
4969 **/
4970static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
4971{
4972 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4973 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4974
4975 ENTER;
4976 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
4977
4978 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
4979 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4980
4981 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4982 ioarcb->cmd_pkt.cdb[2] =
4983 ((u32) ioa_cfg->host_rrq_dma >> 24) & 0xff;
4984 ioarcb->cmd_pkt.cdb[3] =
4985 ((u32) ioa_cfg->host_rrq_dma >> 16) & 0xff;
4986 ioarcb->cmd_pkt.cdb[4] =
4987 ((u32) ioa_cfg->host_rrq_dma >> 8) & 0xff;
4988 ioarcb->cmd_pkt.cdb[5] =
4989 ((u32) ioa_cfg->host_rrq_dma) & 0xff;
4990 ioarcb->cmd_pkt.cdb[7] =
4991 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
4992 ioarcb->cmd_pkt.cdb[8] =
4993 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
4994
4995 ipr_cmd->job_step = ipr_ioafp_std_inquiry;
4996
4997 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4998
4999 LEAVE;
5000 return IPR_RC_JOB_RETURN;
5001}
5002
5003/**
5004 * ipr_reset_timer_done - Adapter reset timer function
5005 * @ipr_cmd: ipr command struct
5006 *
5007 * Description: This function is used in adapter reset processing
5008 * for timing events. If the reset_cmd pointer in the IOA
5009 * config struct is not this adapter's we are doing nested
5010 * resets and fail_all_ops will take care of freeing the
5011 * command block.
5012 *
5013 * Return value:
5014 * none
5015 **/
5016static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
5017{
5018 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5019 unsigned long lock_flags = 0;
5020
5021 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5022
5023 if (ioa_cfg->reset_cmd == ipr_cmd) {
5024 list_del(&ipr_cmd->queue);
5025 ipr_cmd->done(ipr_cmd);
5026 }
5027
5028 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5029}
5030
5031/**
5032 * ipr_reset_start_timer - Start a timer for adapter reset job
5033 * @ipr_cmd: ipr command struct
5034 * @timeout: timeout value
5035 *
5036 * Description: This function is used in adapter reset processing
5037 * for timing events. If the reset_cmd pointer in the IOA
5038 * config struct is not this adapter's we are doing nested
5039 * resets and fail_all_ops will take care of freeing the
5040 * command block.
5041 *
5042 * Return value:
5043 * none
5044 **/
5045static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
5046 unsigned long timeout)
5047{
5048 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
5049 ipr_cmd->done = ipr_reset_ioa_job;
5050
5051 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
5052 ipr_cmd->timer.expires = jiffies + timeout;
5053 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
5054 add_timer(&ipr_cmd->timer);
5055}
5056
5057/**
5058 * ipr_init_ioa_mem - Initialize ioa_cfg control block
5059 * @ioa_cfg: ioa cfg struct
5060 *
5061 * Return value:
5062 * nothing
5063 **/
5064static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
5065{
5066 memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
5067
5068 /* Initialize Host RRQ pointers */
5069 ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
5070 ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
5071 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
5072 ioa_cfg->toggle_bit = 1;
5073
5074 /* Zero out config table */
5075 memset(ioa_cfg->cfg_table, 0, sizeof(struct ipr_config_table));
5076}
5077
5078/**
5079 * ipr_reset_enable_ioa - Enable the IOA following a reset.
5080 * @ipr_cmd: ipr command struct
5081 *
5082 * This function reinitializes some control blocks and
5083 * enables destructive diagnostics on the adapter.
5084 *
5085 * Return value:
5086 * IPR_RC_JOB_RETURN
5087 **/
5088static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
5089{
5090 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5091 volatile u32 int_reg;
5092
5093 ENTER;
5094 ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
5095 ipr_init_ioa_mem(ioa_cfg);
5096
5097 ioa_cfg->allow_interrupts = 1;
5098 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5099
5100 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
5101 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
5102 ioa_cfg->regs.clr_interrupt_mask_reg);
5103 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
5104 return IPR_RC_JOB_CONTINUE;
5105 }
5106
5107 /* Enable destructive diagnostics on IOA */
3d1d0da6 5108 writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg);
1da177e4
LT
5109
5110 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
5111 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
5112
5113 dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
5114
5115 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
5116 ipr_cmd->timer.expires = jiffies + (ipr_transop_timeout * HZ);
5117 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
5118 ipr_cmd->done = ipr_reset_ioa_job;
5119 add_timer(&ipr_cmd->timer);
5120 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
5121
5122 LEAVE;
5123 return IPR_RC_JOB_RETURN;
5124}
5125
5126/**
5127 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
5128 * @ipr_cmd: ipr command struct
5129 *
5130 * This function is invoked when an adapter dump has run out
5131 * of processing time.
5132 *
5133 * Return value:
5134 * IPR_RC_JOB_CONTINUE
5135 **/
5136static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
5137{
5138 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5139
5140 if (ioa_cfg->sdt_state == GET_DUMP)
5141 ioa_cfg->sdt_state = ABORT_DUMP;
5142
5143 ipr_cmd->job_step = ipr_reset_alert;
5144
5145 return IPR_RC_JOB_CONTINUE;
5146}
5147
5148/**
5149 * ipr_unit_check_no_data - Log a unit check/no data error log
5150 * @ioa_cfg: ioa config struct
5151 *
5152 * Logs an error indicating the adapter unit checked, but for some
5153 * reason, we were unable to fetch the unit check buffer.
5154 *
5155 * Return value:
5156 * nothing
5157 **/
5158static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
5159{
5160 ioa_cfg->errors_logged++;
5161 dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
5162}
5163
5164/**
5165 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
5166 * @ioa_cfg: ioa config struct
5167 *
5168 * Fetches the unit check buffer from the adapter by clocking the data
5169 * through the mailbox register.
5170 *
5171 * Return value:
5172 * nothing
5173 **/
5174static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
5175{
5176 unsigned long mailbox;
5177 struct ipr_hostrcb *hostrcb;
5178 struct ipr_uc_sdt sdt;
5179 int rc, length;
5180
5181 mailbox = readl(ioa_cfg->ioa_mailbox);
5182
5183 if (!ipr_sdt_is_fmt2(mailbox)) {
5184 ipr_unit_check_no_data(ioa_cfg);
5185 return;
5186 }
5187
5188 memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
5189 rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
5190 (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
5191
5192 if (rc || (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE) ||
5193 !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY)) {
5194 ipr_unit_check_no_data(ioa_cfg);
5195 return;
5196 }
5197
5198 /* Find length of the first sdt entry (UC buffer) */
5199 length = (be32_to_cpu(sdt.entry[0].end_offset) -
5200 be32_to_cpu(sdt.entry[0].bar_str_offset)) & IPR_FMT2_MBX_ADDR_MASK;
5201
5202 hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
5203 struct ipr_hostrcb, queue);
5204 list_del(&hostrcb->queue);
5205 memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
5206
5207 rc = ipr_get_ldump_data_section(ioa_cfg,
5208 be32_to_cpu(sdt.entry[0].bar_str_offset),
5209 (__be32 *)&hostrcb->hcam,
5210 min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
5211
5212 if (!rc)
5213 ipr_handle_log_data(ioa_cfg, hostrcb);
5214 else
5215 ipr_unit_check_no_data(ioa_cfg);
5216
5217 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
5218}
5219
5220/**
5221 * ipr_reset_restore_cfg_space - Restore PCI config space.
5222 * @ipr_cmd: ipr command struct
5223 *
5224 * Description: This function restores the saved PCI config space of
5225 * the adapter, fails all outstanding ops back to the callers, and
5226 * fetches the dump/unit check if applicable to this reset.
5227 *
5228 * Return value:
5229 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5230 **/
5231static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
5232{
5233 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5234 int rc;
5235
5236 ENTER;
b30197d2 5237 pci_unblock_user_cfg_access(ioa_cfg->pdev);
1da177e4
LT
5238 rc = pci_restore_state(ioa_cfg->pdev);
5239
5240 if (rc != PCIBIOS_SUCCESSFUL) {
5241 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
5242 return IPR_RC_JOB_CONTINUE;
5243 }
5244
5245 if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
5246 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
5247 return IPR_RC_JOB_CONTINUE;
5248 }
5249
5250 ipr_fail_all_ops(ioa_cfg);
5251
5252 if (ioa_cfg->ioa_unit_checked) {
5253 ioa_cfg->ioa_unit_checked = 0;
5254 ipr_get_unit_check_buffer(ioa_cfg);
5255 ipr_cmd->job_step = ipr_reset_alert;
5256 ipr_reset_start_timer(ipr_cmd, 0);
5257 return IPR_RC_JOB_RETURN;
5258 }
5259
5260 if (ioa_cfg->in_ioa_bringdown) {
5261 ipr_cmd->job_step = ipr_ioa_bringdown_done;
5262 } else {
5263 ipr_cmd->job_step = ipr_reset_enable_ioa;
5264
5265 if (GET_DUMP == ioa_cfg->sdt_state) {
5266 ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
5267 ipr_cmd->job_step = ipr_reset_wait_for_dump;
5268 schedule_work(&ioa_cfg->work_q);
5269 return IPR_RC_JOB_RETURN;
5270 }
5271 }
5272
5273 ENTER;
5274 return IPR_RC_JOB_CONTINUE;
5275}
5276
5277/**
5278 * ipr_reset_start_bist - Run BIST on the adapter.
5279 * @ipr_cmd: ipr command struct
5280 *
5281 * Description: This function runs BIST on the adapter, then delays 2 seconds.
5282 *
5283 * Return value:
5284 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5285 **/
5286static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
5287{
5288 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5289 int rc;
5290
5291 ENTER;
b30197d2 5292 pci_block_user_cfg_access(ioa_cfg->pdev);
1da177e4
LT
5293 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
5294
5295 if (rc != PCIBIOS_SUCCESSFUL) {
5296 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
5297 rc = IPR_RC_JOB_CONTINUE;
5298 } else {
5299 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
5300 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
5301 rc = IPR_RC_JOB_RETURN;
5302 }
5303
5304 LEAVE;
5305 return rc;
5306}
5307
5308/**
5309 * ipr_reset_allowed - Query whether or not IOA can be reset
5310 * @ioa_cfg: ioa config struct
5311 *
5312 * Return value:
5313 * 0 if reset not allowed / non-zero if reset is allowed
5314 **/
5315static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
5316{
5317 volatile u32 temp_reg;
5318
5319 temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5320 return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
5321}
5322
5323/**
5324 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
5325 * @ipr_cmd: ipr command struct
5326 *
5327 * Description: This function waits for adapter permission to run BIST,
5328 * then runs BIST. If the adapter does not give permission after a
5329 * reasonable time, we will reset the adapter anyway. The impact of
5330 * resetting the adapter without warning the adapter is the risk of
5331 * losing the persistent error log on the adapter. If the adapter is
5332 * reset while it is writing to the flash on the adapter, the flash
5333 * segment will have bad ECC and be zeroed.
5334 *
5335 * Return value:
5336 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5337 **/
5338static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
5339{
5340 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5341 int rc = IPR_RC_JOB_RETURN;
5342
5343 if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
5344 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
5345 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5346 } else {
5347 ipr_cmd->job_step = ipr_reset_start_bist;
5348 rc = IPR_RC_JOB_CONTINUE;
5349 }
5350
5351 return rc;
5352}
5353
5354/**
5355 * ipr_reset_alert_part2 - Alert the adapter of a pending reset
5356 * @ipr_cmd: ipr command struct
5357 *
5358 * Description: This function alerts the adapter that it will be reset.
5359 * If memory space is not currently enabled, proceed directly
5360 * to running BIST on the adapter. The timer must always be started
5361 * so we guarantee we do not run BIST from ipr_isr.
5362 *
5363 * Return value:
5364 * IPR_RC_JOB_RETURN
5365 **/
5366static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
5367{
5368 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5369 u16 cmd_reg;
5370 int rc;
5371
5372 ENTER;
5373 rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
5374
5375 if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
5376 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
5377 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg);
5378 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
5379 } else {
5380 ipr_cmd->job_step = ipr_reset_start_bist;
5381 }
5382
5383 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
5384 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5385
5386 LEAVE;
5387 return IPR_RC_JOB_RETURN;
5388}
5389
5390/**
5391 * ipr_reset_ucode_download_done - Microcode download completion
5392 * @ipr_cmd: ipr command struct
5393 *
5394 * Description: This function unmaps the microcode download buffer.
5395 *
5396 * Return value:
5397 * IPR_RC_JOB_CONTINUE
5398 **/
5399static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
5400{
5401 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5402 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5403
5404 pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
5405 sglist->num_sg, DMA_TO_DEVICE);
5406
5407 ipr_cmd->job_step = ipr_reset_alert;
5408 return IPR_RC_JOB_CONTINUE;
5409}
5410
5411/**
5412 * ipr_reset_ucode_download - Download microcode to the adapter
5413 * @ipr_cmd: ipr command struct
5414 *
5415 * Description: This function checks to see if it there is microcode
5416 * to download to the adapter. If there is, a download is performed.
5417 *
5418 * Return value:
5419 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5420 **/
5421static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
5422{
5423 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5424 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5425
5426 ENTER;
5427 ipr_cmd->job_step = ipr_reset_alert;
5428
5429 if (!sglist)
5430 return IPR_RC_JOB_CONTINUE;
5431
5432 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5433 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5434 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
5435 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
5436 ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
5437 ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
5438 ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
5439
12baa420 5440 ipr_build_ucode_ioadl(ipr_cmd, sglist);
1da177e4
LT
5441 ipr_cmd->job_step = ipr_reset_ucode_download_done;
5442
5443 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
5444 IPR_WRITE_BUFFER_TIMEOUT);
5445
5446 LEAVE;
5447 return IPR_RC_JOB_RETURN;
5448}
5449
5450/**
5451 * ipr_reset_shutdown_ioa - Shutdown the adapter
5452 * @ipr_cmd: ipr command struct
5453 *
5454 * Description: This function issues an adapter shutdown of the
5455 * specified type to the specified adapter as part of the
5456 * adapter reset job.
5457 *
5458 * Return value:
5459 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5460 **/
5461static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
5462{
5463 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5464 enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
5465 unsigned long timeout;
5466 int rc = IPR_RC_JOB_CONTINUE;
5467
5468 ENTER;
5469 if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
5470 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5471 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5472 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
5473 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
5474
5475 if (shutdown_type == IPR_SHUTDOWN_ABBREV)
5476 timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
5477 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
5478 timeout = IPR_INTERNAL_TIMEOUT;
5479 else
5480 timeout = IPR_SHUTDOWN_TIMEOUT;
5481
5482 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
5483
5484 rc = IPR_RC_JOB_RETURN;
5485 ipr_cmd->job_step = ipr_reset_ucode_download;
5486 } else
5487 ipr_cmd->job_step = ipr_reset_alert;
5488
5489 LEAVE;
5490 return rc;
5491}
5492
5493/**
5494 * ipr_reset_ioa_job - Adapter reset job
5495 * @ipr_cmd: ipr command struct
5496 *
5497 * Description: This function is the job router for the adapter reset job.
5498 *
5499 * Return value:
5500 * none
5501 **/
5502static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
5503{
5504 u32 rc, ioasc;
5505 unsigned long scratch = ipr_cmd->u.scratch;
5506 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5507
5508 do {
5509 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5510
5511 if (ioa_cfg->reset_cmd != ipr_cmd) {
5512 /*
5513 * We are doing nested adapter resets and this is
5514 * not the current reset job.
5515 */
5516 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5517 return;
5518 }
5519
5520 if (IPR_IOASC_SENSE_KEY(ioasc)) {
5521 dev_err(&ioa_cfg->pdev->dev,
5522 "0x%02X failed with IOASC: 0x%08X\n",
5523 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
5524
5525 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5526 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5527 return;
5528 }
5529
5530 ipr_reinit_ipr_cmnd(ipr_cmd);
5531 ipr_cmd->u.scratch = scratch;
5532 rc = ipr_cmd->job_step(ipr_cmd);
5533 } while(rc == IPR_RC_JOB_CONTINUE);
5534}
5535
5536/**
5537 * _ipr_initiate_ioa_reset - Initiate an adapter reset
5538 * @ioa_cfg: ioa config struct
5539 * @job_step: first job step of reset job
5540 * @shutdown_type: shutdown type
5541 *
5542 * Description: This function will initiate the reset of the given adapter
5543 * starting at the selected job step.
5544 * If the caller needs to wait on the completion of the reset,
5545 * the caller must sleep on the reset_wait_q.
5546 *
5547 * Return value:
5548 * none
5549 **/
5550static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5551 int (*job_step) (struct ipr_cmnd *),
5552 enum ipr_shutdown_type shutdown_type)
5553{
5554 struct ipr_cmnd *ipr_cmd;
5555
5556 ioa_cfg->in_reset_reload = 1;
5557 ioa_cfg->allow_cmds = 0;
5558 scsi_block_requests(ioa_cfg->host);
5559
5560 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5561 ioa_cfg->reset_cmd = ipr_cmd;
5562 ipr_cmd->job_step = job_step;
5563 ipr_cmd->u.shutdown_type = shutdown_type;
5564
5565 ipr_reset_ioa_job(ipr_cmd);
5566}
5567
5568/**
5569 * ipr_initiate_ioa_reset - Initiate an adapter reset
5570 * @ioa_cfg: ioa config struct
5571 * @shutdown_type: shutdown type
5572 *
5573 * Description: This function will initiate the reset of the given adapter.
5574 * If the caller needs to wait on the completion of the reset,
5575 * the caller must sleep on the reset_wait_q.
5576 *
5577 * Return value:
5578 * none
5579 **/
5580static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5581 enum ipr_shutdown_type shutdown_type)
5582{
5583 if (ioa_cfg->ioa_is_dead)
5584 return;
5585
5586 if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
5587 ioa_cfg->sdt_state = ABORT_DUMP;
5588
5589 if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
5590 dev_err(&ioa_cfg->pdev->dev,
5591 "IOA taken offline - error recovery failed\n");
5592
5593 ioa_cfg->reset_retries = 0;
5594 ioa_cfg->ioa_is_dead = 1;
5595
5596 if (ioa_cfg->in_ioa_bringdown) {
5597 ioa_cfg->reset_cmd = NULL;
5598 ioa_cfg->in_reset_reload = 0;
5599 ipr_fail_all_ops(ioa_cfg);
5600 wake_up_all(&ioa_cfg->reset_wait_q);
5601
5602 spin_unlock_irq(ioa_cfg->host->host_lock);
5603 scsi_unblock_requests(ioa_cfg->host);
5604 spin_lock_irq(ioa_cfg->host->host_lock);
5605 return;
5606 } else {
5607 ioa_cfg->in_ioa_bringdown = 1;
5608 shutdown_type = IPR_SHUTDOWN_NONE;
5609 }
5610 }
5611
5612 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
5613 shutdown_type);
5614}
5615
5616/**
5617 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
5618 * @ioa_cfg: ioa cfg struct
5619 *
5620 * Description: This is the second phase of adapter intialization
5621 * This function takes care of initilizing the adapter to the point
5622 * where it can accept new commands.
5623
5624 * Return value:
5625 * 0 on sucess / -EIO on failure
5626 **/
5627static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
5628{
5629 int rc = 0;
5630 unsigned long host_lock_flags = 0;
5631
5632 ENTER;
5633 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5634 dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
5635 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa, IPR_SHUTDOWN_NONE);
5636
5637 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5638 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5639 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5640
5641 if (ioa_cfg->ioa_is_dead) {
5642 rc = -EIO;
5643 } else if (ipr_invalid_adapter(ioa_cfg)) {
5644 if (!ipr_testmode)
5645 rc = -EIO;
5646
5647 dev_err(&ioa_cfg->pdev->dev,
5648 "Adapter not supported in this hardware configuration.\n");
5649 }
5650
5651 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5652
5653 LEAVE;
5654 return rc;
5655}
5656
5657/**
5658 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
5659 * @ioa_cfg: ioa config struct
5660 *
5661 * Return value:
5662 * none
5663 **/
5664static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5665{
5666 int i;
5667
5668 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
5669 if (ioa_cfg->ipr_cmnd_list[i])
5670 pci_pool_free(ioa_cfg->ipr_cmd_pool,
5671 ioa_cfg->ipr_cmnd_list[i],
5672 ioa_cfg->ipr_cmnd_list_dma[i]);
5673
5674 ioa_cfg->ipr_cmnd_list[i] = NULL;
5675 }
5676
5677 if (ioa_cfg->ipr_cmd_pool)
5678 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
5679
5680 ioa_cfg->ipr_cmd_pool = NULL;
5681}
5682
5683/**
5684 * ipr_free_mem - Frees memory allocated for an adapter
5685 * @ioa_cfg: ioa cfg struct
5686 *
5687 * Return value:
5688 * nothing
5689 **/
5690static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
5691{
5692 int i;
5693
5694 kfree(ioa_cfg->res_entries);
5695 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
5696 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5697 ipr_free_cmd_blks(ioa_cfg);
5698 pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5699 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5700 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_config_table),
5701 ioa_cfg->cfg_table,
5702 ioa_cfg->cfg_table_dma);
5703
5704 for (i = 0; i < IPR_NUM_HCAMS; i++) {
5705 pci_free_consistent(ioa_cfg->pdev,
5706 sizeof(struct ipr_hostrcb),
5707 ioa_cfg->hostrcb[i],
5708 ioa_cfg->hostrcb_dma[i]);
5709 }
5710
5711 ipr_free_dump(ioa_cfg);
5712 kfree(ioa_cfg->saved_mode_pages);
5713 kfree(ioa_cfg->trace);
5714}
5715
5716/**
5717 * ipr_free_all_resources - Free all allocated resources for an adapter.
5718 * @ipr_cmd: ipr command struct
5719 *
5720 * This function frees all allocated resources for the
5721 * specified adapter.
5722 *
5723 * Return value:
5724 * none
5725 **/
5726static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
5727{
5728 struct pci_dev *pdev = ioa_cfg->pdev;
5729
5730 ENTER;
5731 free_irq(pdev->irq, ioa_cfg);
5732 iounmap(ioa_cfg->hdw_dma_regs);
5733 pci_release_regions(pdev);
5734 ipr_free_mem(ioa_cfg);
5735 scsi_host_put(ioa_cfg->host);
5736 pci_disable_device(pdev);
5737 LEAVE;
5738}
5739
5740/**
5741 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
5742 * @ioa_cfg: ioa config struct
5743 *
5744 * Return value:
5745 * 0 on success / -ENOMEM on allocation failure
5746 **/
5747static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5748{
5749 struct ipr_cmnd *ipr_cmd;
5750 struct ipr_ioarcb *ioarcb;
5751 dma_addr_t dma_addr;
5752 int i;
5753
5754 ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
5755 sizeof(struct ipr_cmnd), 8, 0);
5756
5757 if (!ioa_cfg->ipr_cmd_pool)
5758 return -ENOMEM;
5759
5760 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
5761 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, SLAB_KERNEL, &dma_addr);
5762
5763 if (!ipr_cmd) {
5764 ipr_free_cmd_blks(ioa_cfg);
5765 return -ENOMEM;
5766 }
5767
5768 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
5769 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
5770 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
5771
5772 ioarcb = &ipr_cmd->ioarcb;
5773 ioarcb->ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
5774 ioarcb->host_response_handle = cpu_to_be32(i << 2);
5775 ioarcb->write_ioadl_addr =
5776 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
5777 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5778 ioarcb->ioasa_host_pci_addr =
5779 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
5780 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
5781 ipr_cmd->cmd_index = i;
5782 ipr_cmd->ioa_cfg = ioa_cfg;
5783 ipr_cmd->sense_buffer_dma = dma_addr +
5784 offsetof(struct ipr_cmnd, sense_buffer);
5785
5786 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5787 }
5788
5789 return 0;
5790}
5791
5792/**
5793 * ipr_alloc_mem - Allocate memory for an adapter
5794 * @ioa_cfg: ioa config struct
5795 *
5796 * Return value:
5797 * 0 on success / non-zero for error
5798 **/
5799static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
5800{
5801 struct pci_dev *pdev = ioa_cfg->pdev;
5802 int i, rc = -ENOMEM;
5803
5804 ENTER;
0bc42e35 5805 ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
1da177e4
LT
5806 IPR_MAX_PHYSICAL_DEVS, GFP_KERNEL);
5807
5808 if (!ioa_cfg->res_entries)
5809 goto out;
5810
1da177e4
LT
5811 for (i = 0; i < IPR_MAX_PHYSICAL_DEVS; i++)
5812 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
5813
5814 ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
5815 sizeof(struct ipr_misc_cbs),
5816 &ioa_cfg->vpd_cbs_dma);
5817
5818 if (!ioa_cfg->vpd_cbs)
5819 goto out_free_res_entries;
5820
5821 if (ipr_alloc_cmd_blks(ioa_cfg))
5822 goto out_free_vpd_cbs;
5823
5824 ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
5825 sizeof(u32) * IPR_NUM_CMD_BLKS,
5826 &ioa_cfg->host_rrq_dma);
5827
5828 if (!ioa_cfg->host_rrq)
5829 goto out_ipr_free_cmd_blocks;
5830
5831 ioa_cfg->cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
5832 sizeof(struct ipr_config_table),
5833 &ioa_cfg->cfg_table_dma);
5834
5835 if (!ioa_cfg->cfg_table)
5836 goto out_free_host_rrq;
5837
5838 for (i = 0; i < IPR_NUM_HCAMS; i++) {
5839 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
5840 sizeof(struct ipr_hostrcb),
5841 &ioa_cfg->hostrcb_dma[i]);
5842
5843 if (!ioa_cfg->hostrcb[i])
5844 goto out_free_hostrcb_dma;
5845
5846 ioa_cfg->hostrcb[i]->hostrcb_dma =
5847 ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
5848 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
5849 }
5850
0bc42e35 5851 ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
1da177e4
LT
5852 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
5853
5854 if (!ioa_cfg->trace)
5855 goto out_free_hostrcb_dma;
5856
1da177e4
LT
5857 rc = 0;
5858out:
5859 LEAVE;
5860 return rc;
5861
5862out_free_hostrcb_dma:
5863 while (i-- > 0) {
5864 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
5865 ioa_cfg->hostrcb[i],
5866 ioa_cfg->hostrcb_dma[i]);
5867 }
5868 pci_free_consistent(pdev, sizeof(struct ipr_config_table),
5869 ioa_cfg->cfg_table, ioa_cfg->cfg_table_dma);
5870out_free_host_rrq:
5871 pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5872 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5873out_ipr_free_cmd_blocks:
5874 ipr_free_cmd_blks(ioa_cfg);
5875out_free_vpd_cbs:
5876 pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
5877 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5878out_free_res_entries:
5879 kfree(ioa_cfg->res_entries);
5880 goto out;
5881}
5882
5883/**
5884 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
5885 * @ioa_cfg: ioa config struct
5886 *
5887 * Return value:
5888 * none
5889 **/
5890static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
5891{
5892 int i;
5893
5894 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
5895 ioa_cfg->bus_attr[i].bus = i;
5896 ioa_cfg->bus_attr[i].qas_enabled = 0;
5897 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
5898 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
5899 ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
5900 else
5901 ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
5902 }
5903}
5904
5905/**
5906 * ipr_init_ioa_cfg - Initialize IOA config struct
5907 * @ioa_cfg: ioa config struct
5908 * @host: scsi host struct
5909 * @pdev: PCI dev struct
5910 *
5911 * Return value:
5912 * none
5913 **/
5914static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
5915 struct Scsi_Host *host, struct pci_dev *pdev)
5916{
5917 const struct ipr_interrupt_offsets *p;
5918 struct ipr_interrupts *t;
5919 void __iomem *base;
5920
5921 ioa_cfg->host = host;
5922 ioa_cfg->pdev = pdev;
5923 ioa_cfg->log_level = ipr_log_level;
3d1d0da6 5924 ioa_cfg->doorbell = IPR_DOORBELL;
32d29776
BK
5925 if (!ipr_auto_create)
5926 ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
1da177e4
LT
5927 sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
5928 sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
5929 sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
5930 sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
5931 sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
5932 sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
5933 sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
5934 sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
5935
5936 INIT_LIST_HEAD(&ioa_cfg->free_q);
5937 INIT_LIST_HEAD(&ioa_cfg->pending_q);
5938 INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
5939 INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
5940 INIT_LIST_HEAD(&ioa_cfg->free_res_q);
5941 INIT_LIST_HEAD(&ioa_cfg->used_res_q);
5942 INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread, ioa_cfg);
5943 init_waitqueue_head(&ioa_cfg->reset_wait_q);
5944 ioa_cfg->sdt_state = INACTIVE;
62275040
BK
5945 if (ipr_enable_cache)
5946 ioa_cfg->cache_state = CACHE_ENABLED;
5947 else
5948 ioa_cfg->cache_state = CACHE_DISABLED;
1da177e4
LT
5949
5950 ipr_initialize_bus_attr(ioa_cfg);
5951
5952 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
5953 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
5954 host->max_channel = IPR_MAX_BUS_TO_SCAN;
5955 host->unique_id = host->host_no;
5956 host->max_cmd_len = IPR_MAX_CDB_LEN;
5957 pci_set_drvdata(pdev, ioa_cfg);
5958
5959 p = &ioa_cfg->chip_cfg->regs;
5960 t = &ioa_cfg->regs;
5961 base = ioa_cfg->hdw_dma_regs;
5962
5963 t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
5964 t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
5965 t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
5966 t->clr_interrupt_reg = base + p->clr_interrupt_reg;
5967 t->sense_interrupt_reg = base + p->sense_interrupt_reg;
5968 t->ioarrin_reg = base + p->ioarrin_reg;
5969 t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
5970 t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
5971 t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
5972}
5973
5974/**
5975 * ipr_get_chip_cfg - Find adapter chip configuration
5976 * @dev_id: PCI device id struct
5977 *
5978 * Return value:
5979 * ptr to chip config on success / NULL on failure
5980 **/
5981static const struct ipr_chip_cfg_t * __devinit
5982ipr_get_chip_cfg(const struct pci_device_id *dev_id)
5983{
5984 int i;
5985
5986 if (dev_id->driver_data)
5987 return (const struct ipr_chip_cfg_t *)dev_id->driver_data;
5988
5989 for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
5990 if (ipr_chip[i].vendor == dev_id->vendor &&
5991 ipr_chip[i].device == dev_id->device)
5992 return ipr_chip[i].cfg;
5993 return NULL;
5994}
5995
5996/**
5997 * ipr_probe_ioa - Allocates memory and does first stage of initialization
5998 * @pdev: PCI device struct
5999 * @dev_id: PCI device id struct
6000 *
6001 * Return value:
6002 * 0 on success / non-zero on failure
6003 **/
6004static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
6005 const struct pci_device_id *dev_id)
6006{
6007 struct ipr_ioa_cfg *ioa_cfg;
6008 struct Scsi_Host *host;
6009 unsigned long ipr_regs_pci;
6010 void __iomem *ipr_regs;
6011 u32 rc = PCIBIOS_SUCCESSFUL;
6012
6013 ENTER;
6014
6015 if ((rc = pci_enable_device(pdev))) {
6016 dev_err(&pdev->dev, "Cannot enable adapter\n");
6017 goto out;
6018 }
6019
6020 dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
6021
6022 host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
6023
6024 if (!host) {
6025 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
6026 rc = -ENOMEM;
6027 goto out_disable;
6028 }
6029
6030 ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
6031 memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
6032
6033 ioa_cfg->chip_cfg = ipr_get_chip_cfg(dev_id);
6034
6035 if (!ioa_cfg->chip_cfg) {
6036 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
6037 dev_id->vendor, dev_id->device);
6038 goto out_scsi_host_put;
6039 }
6040
6041 ipr_regs_pci = pci_resource_start(pdev, 0);
6042
6043 rc = pci_request_regions(pdev, IPR_NAME);
6044 if (rc < 0) {
6045 dev_err(&pdev->dev,
6046 "Couldn't register memory range of registers\n");
6047 goto out_scsi_host_put;
6048 }
6049
6050 ipr_regs = ioremap(ipr_regs_pci, pci_resource_len(pdev, 0));
6051
6052 if (!ipr_regs) {
6053 dev_err(&pdev->dev,
6054 "Couldn't map memory range of registers\n");
6055 rc = -ENOMEM;
6056 goto out_release_regions;
6057 }
6058
6059 ioa_cfg->hdw_dma_regs = ipr_regs;
6060 ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
6061 ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
6062
6063 ipr_init_ioa_cfg(ioa_cfg, host, pdev);
6064
6065 pci_set_master(pdev);
6066
6067 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
6068 if (rc < 0) {
6069 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
6070 goto cleanup_nomem;
6071 }
6072
6073 rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
6074 ioa_cfg->chip_cfg->cache_line_size);
6075
6076 if (rc != PCIBIOS_SUCCESSFUL) {
6077 dev_err(&pdev->dev, "Write of cache line size failed\n");
6078 rc = -EIO;
6079 goto cleanup_nomem;
6080 }
6081
6082 /* Save away PCI config space for use following IOA reset */
6083 rc = pci_save_state(pdev);
6084
6085 if (rc != PCIBIOS_SUCCESSFUL) {
6086 dev_err(&pdev->dev, "Failed to save PCI config space\n");
6087 rc = -EIO;
6088 goto cleanup_nomem;
6089 }
6090
6091 if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
6092 goto cleanup_nomem;
6093
6094 if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
6095 goto cleanup_nomem;
6096
6097 rc = ipr_alloc_mem(ioa_cfg);
6098 if (rc < 0) {
6099 dev_err(&pdev->dev,
6100 "Couldn't allocate enough memory for device driver!\n");
6101 goto cleanup_nomem;
6102 }
6103
6104 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
6105 rc = request_irq(pdev->irq, ipr_isr, SA_SHIRQ, IPR_NAME, ioa_cfg);
6106
6107 if (rc) {
6108 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
6109 pdev->irq, rc);
6110 goto cleanup_nolog;
6111 }
6112
6113 spin_lock(&ipr_driver_lock);
6114 list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
6115 spin_unlock(&ipr_driver_lock);
6116
6117 LEAVE;
6118out:
6119 return rc;
6120
6121cleanup_nolog:
6122 ipr_free_mem(ioa_cfg);
6123cleanup_nomem:
6124 iounmap(ipr_regs);
6125out_release_regions:
6126 pci_release_regions(pdev);
6127out_scsi_host_put:
6128 scsi_host_put(host);
6129out_disable:
6130 pci_disable_device(pdev);
6131 goto out;
6132}
6133
6134/**
6135 * ipr_scan_vsets - Scans for VSET devices
6136 * @ioa_cfg: ioa config struct
6137 *
6138 * Description: Since the VSET resources do not follow SAM in that we can have
6139 * sparse LUNs with no LUN 0, we have to scan for these ourselves.
6140 *
6141 * Return value:
6142 * none
6143 **/
6144static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
6145{
6146 int target, lun;
6147
6148 for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
6149 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
6150 scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
6151}
6152
6153/**
6154 * ipr_initiate_ioa_bringdown - Bring down an adapter
6155 * @ioa_cfg: ioa config struct
6156 * @shutdown_type: shutdown type
6157 *
6158 * Description: This function will initiate bringing down the adapter.
6159 * This consists of issuing an IOA shutdown to the adapter
6160 * to flush the cache, and running BIST.
6161 * If the caller needs to wait on the completion of the reset,
6162 * the caller must sleep on the reset_wait_q.
6163 *
6164 * Return value:
6165 * none
6166 **/
6167static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
6168 enum ipr_shutdown_type shutdown_type)
6169{
6170 ENTER;
6171 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
6172 ioa_cfg->sdt_state = ABORT_DUMP;
6173 ioa_cfg->reset_retries = 0;
6174 ioa_cfg->in_ioa_bringdown = 1;
6175 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
6176 LEAVE;
6177}
6178
6179/**
6180 * __ipr_remove - Remove a single adapter
6181 * @pdev: pci device struct
6182 *
6183 * Adapter hot plug remove entry point.
6184 *
6185 * Return value:
6186 * none
6187 **/
6188static void __ipr_remove(struct pci_dev *pdev)
6189{
6190 unsigned long host_lock_flags = 0;
6191 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6192 ENTER;
6193
6194 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
6195 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
6196
6197 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
6198 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5cbf5eae 6199 flush_scheduled_work();
1da177e4
LT
6200 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
6201
6202 spin_lock(&ipr_driver_lock);
6203 list_del(&ioa_cfg->queue);
6204 spin_unlock(&ipr_driver_lock);
6205
6206 if (ioa_cfg->sdt_state == ABORT_DUMP)
6207 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
6208 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
6209
6210 ipr_free_all_resources(ioa_cfg);
6211
6212 LEAVE;
6213}
6214
6215/**
6216 * ipr_remove - IOA hot plug remove entry point
6217 * @pdev: pci device struct
6218 *
6219 * Adapter hot plug remove entry point.
6220 *
6221 * Return value:
6222 * none
6223 **/
6224static void ipr_remove(struct pci_dev *pdev)
6225{
6226 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6227
6228 ENTER;
6229
1da177e4
LT
6230 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
6231 &ipr_trace_attr);
6232 ipr_remove_dump_file(&ioa_cfg->host->shost_classdev.kobj,
6233 &ipr_dump_attr);
6234 scsi_remove_host(ioa_cfg->host);
6235
6236 __ipr_remove(pdev);
6237
6238 LEAVE;
6239}
6240
6241/**
6242 * ipr_probe - Adapter hot plug add entry point
6243 *
6244 * Return value:
6245 * 0 on success / non-zero on failure
6246 **/
6247static int __devinit ipr_probe(struct pci_dev *pdev,
6248 const struct pci_device_id *dev_id)
6249{
6250 struct ipr_ioa_cfg *ioa_cfg;
6251 int rc;
6252
6253 rc = ipr_probe_ioa(pdev, dev_id);
6254
6255 if (rc)
6256 return rc;
6257
6258 ioa_cfg = pci_get_drvdata(pdev);
6259 rc = ipr_probe_ioa_part2(ioa_cfg);
6260
6261 if (rc) {
6262 __ipr_remove(pdev);
6263 return rc;
6264 }
6265
6266 rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
6267
6268 if (rc) {
6269 __ipr_remove(pdev);
6270 return rc;
6271 }
6272
6273 rc = ipr_create_trace_file(&ioa_cfg->host->shost_classdev.kobj,
6274 &ipr_trace_attr);
6275
6276 if (rc) {
6277 scsi_remove_host(ioa_cfg->host);
6278 __ipr_remove(pdev);
6279 return rc;
6280 }
6281
6282 rc = ipr_create_dump_file(&ioa_cfg->host->shost_classdev.kobj,
6283 &ipr_dump_attr);
6284
6285 if (rc) {
6286 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
6287 &ipr_trace_attr);
6288 scsi_remove_host(ioa_cfg->host);
6289 __ipr_remove(pdev);
6290 return rc;
6291 }
6292
6293 scsi_scan_host(ioa_cfg->host);
6294 ipr_scan_vsets(ioa_cfg);
6295 scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
6296 ioa_cfg->allow_ml_add_del = 1;
11cd8f12 6297 ioa_cfg->host->max_channel = IPR_VSET_BUS;
1da177e4
LT
6298 schedule_work(&ioa_cfg->work_q);
6299 return 0;
6300}
6301
6302/**
6303 * ipr_shutdown - Shutdown handler.
d18c3db5 6304 * @pdev: pci device struct
1da177e4
LT
6305 *
6306 * This function is invoked upon system shutdown/reboot. It will issue
6307 * an adapter shutdown to the adapter to flush the write cache.
6308 *
6309 * Return value:
6310 * none
6311 **/
d18c3db5 6312static void ipr_shutdown(struct pci_dev *pdev)
1da177e4 6313{
d18c3db5 6314 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
1da177e4
LT
6315 unsigned long lock_flags = 0;
6316
6317 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6318 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
6319 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6320 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
6321}
6322
6323static struct pci_device_id ipr_pci_table[] __devinitdata = {
6324 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6325 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702,
6326 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6327 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6328 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703,
6329 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6330 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6331 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D,
6332 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6333 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6334 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E,
6335 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6336 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6337 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B,
6338 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6339 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6340 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E,
6341 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6342 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6343 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A,
6344 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6345 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
6346 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780,
6347 0, 0, (kernel_ulong_t)&ipr_chip_cfg[1] },
6348 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
6349 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E,
6350 0, 0, (kernel_ulong_t)&ipr_chip_cfg[1] },
6351 { }
6352};
6353MODULE_DEVICE_TABLE(pci, ipr_pci_table);
6354
6355static struct pci_driver ipr_driver = {
6356 .name = IPR_NAME,
6357 .id_table = ipr_pci_table,
6358 .probe = ipr_probe,
6359 .remove = ipr_remove,
d18c3db5 6360 .shutdown = ipr_shutdown,
1da177e4
LT
6361};
6362
6363/**
6364 * ipr_init - Module entry point
6365 *
6366 * Return value:
6367 * 0 on success / negative value on failure
6368 **/
6369static int __init ipr_init(void)
6370{
6371 ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
6372 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
6373
6374 return pci_module_init(&ipr_driver);
6375}
6376
6377/**
6378 * ipr_exit - Module unload
6379 *
6380 * Module unload entry point.
6381 *
6382 * Return value:
6383 * none
6384 **/
6385static void __exit ipr_exit(void)
6386{
6387 pci_unregister_driver(&ipr_driver);
6388}
6389
6390module_init(ipr_init);
6391module_exit(ipr_exit);