[SCSI] ipr: Module parm to disable RAID 0 auto create
[linux-2.6.22.y-op.git] / drivers / scsi / ipr.c
bloba5df245c8c2c9b7fc499b2ba26a3ad0653e468d1
1 /*
2 * ipr.c -- driver for IBM Power Linux RAID adapters
4 * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
6 * Copyright (C) 2003, 2004 IBM Corporation
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.
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.
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
25 * Notes:
27 * This driver is used to control the following SCSI adapters:
29 * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
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
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
49 * Driver Features:
50 * - Tagged command queuing
51 * - Adapter microcode download
52 * - PCI hot plug
53 * - SCSI device hot plug
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"
86 * Global Data
88 static struct list_head ipr_ioa_head = LIST_HEAD_INIT(ipr_ioa_head);
89 static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
90 static unsigned int ipr_max_speed = 1;
91 static int ipr_testmode = 0;
92 static unsigned int ipr_fastfail = 0;
93 static unsigned int ipr_transop_timeout = IPR_OPERATIONAL_TIMEOUT;
94 static unsigned int ipr_enable_cache = 1;
95 static unsigned int ipr_debug = 0;
96 static int ipr_auto_create = 1;
97 static DEFINE_SPINLOCK(ipr_driver_lock);
99 /* This table describes the differences between DMA controller chips */
100 static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
101 { /* Gemstone and Citrine */
102 .mailbox = 0x0042C,
103 .cache_line_size = 0x20,
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
116 { /* Snipe and Scamp */
117 .mailbox = 0x0052C,
118 .cache_line_size = 0x20,
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
133 static 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] }
140 static int ipr_max_bus_speeds [] = {
141 IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
144 MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
145 MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
146 module_param_named(max_speed, ipr_max_speed, uint, 0);
147 MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
148 module_param_named(log_level, ipr_log_level, uint, 0);
149 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
150 module_param_named(testmode, ipr_testmode, int, 0);
151 MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
152 module_param_named(fastfail, ipr_fastfail, int, 0);
153 MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
154 module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
155 MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
156 module_param_named(enable_cache, ipr_enable_cache, int, 0);
157 MODULE_PARM_DESC(enable_cache, "Enable adapter's non-volatile write cache (default: 1)");
158 module_param_named(debug, ipr_debug, int, 0);
159 MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
160 module_param_named(auto_create, ipr_auto_create, int, 0);
161 MODULE_PARM_DESC(auto_create, "Auto-create single device RAID 0 arrays when initialized (default: 1)");
162 MODULE_LICENSE("GPL");
163 MODULE_VERSION(IPR_DRIVER_VERSION);
165 static 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"
174 static 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"
188 /* A constant array of IOASCs/URCs/Error Messages */
189 static const
190 struct 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"},
297 {0x04678000, 0, 1,
298 "9073: Invalid multi-adapter configuration"},
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"},
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"},
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"},
317 {0x052C8000, 1, 0,
318 "Illegal request, dual adapter support not enabled"},
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"},
335 {0x064C8100, 0, 1,
336 "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
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"},
343 {0x06678100, 0, 1,
344 "9074: Asymmetric advanced function disk configuration"},
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"},
351 {0x066B8000, 0, 1,
352 "9071: Link operational transition"},
353 {0x066B8100, 0, 1,
354 "9072: Link not operational transition"},
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"}
405 static 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 }
422 * Function Prototypes
424 static int ipr_reset_alert(struct ipr_cmnd *);
425 static void ipr_process_ccn(struct ipr_cmnd *);
426 static void ipr_process_error(struct ipr_cmnd *);
427 static void ipr_reset_ioa_job(struct ipr_cmnd *);
428 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
429 enum ipr_shutdown_type);
431 #ifdef CONFIG_SCSI_IPR_TRACE
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
438 * Return value:
439 * none
441 static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
442 u8 type, u32 add_data)
444 struct ipr_trace_entry *trace_entry;
445 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
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;
455 #else
456 #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
457 #endif
460 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
461 * @ipr_cmd: ipr command struct
463 * Return value:
464 * none
466 static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
468 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
469 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
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;
479 ipr_cmd->scsi_cmd = NULL;
480 ipr_cmd->sense_buffer[0] = 0;
481 ipr_cmd->dma_use_sg = 0;
485 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
486 * @ipr_cmd: ipr command struct
488 * Return value:
489 * none
491 static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
493 ipr_reinit_ipr_cmnd(ipr_cmd);
494 ipr_cmd->u.scratch = 0;
495 ipr_cmd->sibling = NULL;
496 init_timer(&ipr_cmd->timer);
500 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
501 * @ioa_cfg: ioa config struct
503 * Return value:
504 * pointer to ipr command struct
506 static
507 struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
509 struct ipr_cmnd *ipr_cmd;
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);
515 return ipr_cmd;
519 * ipr_unmap_sglist - Unmap scatterlist if mapped
520 * @ioa_cfg: ioa config struct
521 * @ipr_cmd: ipr command struct
523 * Return value:
524 * nothing
526 static void ipr_unmap_sglist(struct ipr_ioa_cfg *ioa_cfg,
527 struct ipr_cmnd *ipr_cmd)
529 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
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);
545 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
546 * @ioa_cfg: ioa config struct
547 * @clr_ints: interrupts to clear
549 * This function masks all interrupts on the adapter, then clears the
550 * interrupts specified in the mask
552 * Return value:
553 * none
555 static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
556 u32 clr_ints)
558 volatile u32 int_reg;
560 /* Stop new interrupts */
561 ioa_cfg->allow_interrupts = 0;
563 /* Set interrupt mask to stop all new interrupts */
564 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
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);
572 * ipr_save_pcix_cmd_reg - Save PCI-X command register
573 * @ioa_cfg: ioa config struct
575 * Return value:
576 * 0 on success / -EIO on failure
578 static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
580 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
582 if (pcix_cmd_reg == 0) {
583 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
584 return -EIO;
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;
593 ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
594 return 0;
598 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
599 * @ioa_cfg: ioa config struct
601 * Return value:
602 * 0 on success / -EIO on failure
604 static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
606 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
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;
614 } else {
615 dev_err(&ioa_cfg->pdev->dev,
616 "Failed to setup PCI-X command register\n");
617 return -EIO;
620 return 0;
624 * ipr_scsi_eh_done - mid-layer done function for aborted ops
625 * @ipr_cmd: ipr command struct
627 * This function is invoked by the interrupt handler for
628 * ops generated by the SCSI mid-layer which are being aborted.
630 * Return value:
631 * none
633 static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
635 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
636 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
638 scsi_cmd->result |= (DID_ERROR << 16);
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);
646 * ipr_fail_all_ops - Fails all outstanding ops.
647 * @ioa_cfg: ioa config struct
649 * This function fails all outstanding ops.
651 * Return value:
652 * none
654 static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
656 struct ipr_cmnd *ipr_cmd, *temp;
658 ENTER;
659 list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
660 list_del(&ipr_cmd->queue);
662 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
663 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
665 if (ipr_cmd->scsi_cmd)
666 ipr_cmd->done = ipr_scsi_eh_done;
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);
673 LEAVE;
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
683 * This function sends the specified command to the adapter with the
684 * timeout given. The done function is invoked on command completion.
686 * Return value:
687 * none
689 static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
690 void (*done) (struct ipr_cmnd *),
691 void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
693 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
695 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
697 ipr_cmd->done = done;
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;
703 add_timer(&ipr_cmd->timer);
705 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
707 mb();
708 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
709 ioa_cfg->regs.ioarrin_reg);
713 * ipr_internal_cmd_done - Op done function for an internally generated op.
714 * @ipr_cmd: ipr command struct
716 * This function is the op done function for an internally generated,
717 * blocking op. It simply wakes the sleeping thread.
719 * Return value:
720 * none
722 static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
724 if (ipr_cmd->sibling)
725 ipr_cmd->sibling = NULL;
726 else
727 complete(&ipr_cmd->completion);
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
736 * Return value:
737 * none
739 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
740 void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
741 u32 timeout)
743 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
745 init_completion(&ipr_cmd->completion);
746 ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
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);
754 * ipr_send_hcam - Send an HCAM to the adapter.
755 * @ioa_cfg: ioa config struct
756 * @type: HCAM type
757 * @hostrcb: hostrcb struct
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.
763 * Return value:
764 * none
766 static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
767 struct ipr_hostrcb *hostrcb)
769 struct ipr_cmnd *ipr_cmd;
770 struct ipr_ioarcb *ioarcb;
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);
777 ipr_cmd->u.hostrcb = hostrcb;
778 ioarcb = &ipr_cmd->ioarcb;
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;
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);
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;
798 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
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);
809 * ipr_init_res_entry - Initialize a resource entry struct.
810 * @res: resource entry struct
812 * Return value:
813 * none
815 static void ipr_init_res_entry(struct ipr_resource_entry *res)
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;
826 * ipr_handle_config_change - Handle a config change from the adapter
827 * @ioa_cfg: ioa config struct
828 * @hostrcb: hostrcb
830 * Return value:
831 * none
833 static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
834 struct ipr_hostrcb *hostrcb)
836 struct ipr_resource_entry *res = NULL;
837 struct ipr_config_table_entry *cfgte;
838 u32 is_ndn = 1;
840 cfgte = &hostrcb->hcam.u.ccn.cfgte;
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;
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;
858 res = list_entry(ioa_cfg->free_res_q.next,
859 struct ipr_resource_entry, queue);
861 list_del(&res->queue);
862 ipr_init_res_entry(res);
863 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
866 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
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);
882 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
886 * ipr_process_ccn - Op done function for a CCN.
887 * @ipr_cmd: ipr command struct
889 * This function is the op done function for a configuration
890 * change notification host controlled async from the adapter.
892 * Return value:
893 * none
895 static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
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);
901 list_del(&hostrcb->queue);
902 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
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);
909 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
910 } else {
911 ipr_handle_config_change(ioa_cfg, hostrcb);
916 * ipr_log_vpd - Log the passed VPD to the error log.
917 * @vpd: vendor/product id/sn struct
919 * Return value:
920 * none
922 static void ipr_log_vpd(struct ipr_vpd *vpd)
924 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
925 + IPR_SERIAL_NUM_LEN];
927 memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
928 memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
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);
933 memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
934 buffer[IPR_SERIAL_NUM_LEN] = '\0';
935 ipr_err(" Serial Number: %s\n", buffer);
939 * ipr_log_cache_error - Log a cache error.
940 * @ioa_cfg: ioa config struct
941 * @hostrcb: hostrcb struct
943 * Return value:
944 * none
946 static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
947 struct ipr_hostrcb *hostrcb)
949 struct ipr_hostrcb_type_02_error *error =
950 &hostrcb->hcam.u.error.u.type_02_error;
952 ipr_err("-----Current Configuration-----\n");
953 ipr_err("Cache Directory Card Information:\n");
954 ipr_log_vpd(&error->ioa_vpd);
955 ipr_err("Adapter Card Information:\n");
956 ipr_log_vpd(&error->cfc_vpd);
958 ipr_err("-----Expected Configuration-----\n");
959 ipr_err("Cache Directory Card Information:\n");
960 ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
961 ipr_err("Adapter Card Information:\n");
962 ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
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]));
971 * ipr_log_config_error - Log a configuration error.
972 * @ioa_cfg: ioa config struct
973 * @hostrcb: hostrcb struct
975 * Return value:
976 * none
978 static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
979 struct ipr_hostrcb *hostrcb)
981 int errors_logged, i;
982 struct ipr_hostrcb_device_data_entry *dev_entry;
983 struct ipr_hostrcb_type_03_error *error;
985 error = &hostrcb->hcam.u.error.u.type_03_error;
986 errors_logged = be32_to_cpu(error->errors_logged);
988 ipr_err("Device Errors Detected/Logged: %d/%d\n",
989 be32_to_cpu(error->errors_detected), errors_logged);
991 dev_entry = error->dev;
993 for (i = 0; i < errors_logged; i++, dev_entry++) {
994 ipr_err_separator;
996 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
997 ipr_log_vpd(&dev_entry->vpd);
999 ipr_err("-----New Device Information-----\n");
1000 ipr_log_vpd(&dev_entry->new_vpd);
1002 ipr_err("Cache Directory Card Information:\n");
1003 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
1005 ipr_err("Adapter Card Information:\n");
1006 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
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]));
1018 * ipr_log_array_error - Log an array configuration error.
1019 * @ioa_cfg: ioa config struct
1020 * @hostrcb: hostrcb struct
1022 * Return value:
1023 * none
1025 static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1026 struct ipr_hostrcb *hostrcb)
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' };
1033 error = &hostrcb->hcam.u.error.u.type_04_error;
1035 ipr_err_separator;
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);
1044 ipr_err_separator;
1046 array_entry = error->array_member;
1048 for (i = 0; i < 18; i++) {
1049 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1050 continue;
1052 if (be32_to_cpu(error->exposed_mode_adn) == i)
1053 ipr_err("Exposed Array Member %d:\n", i);
1054 else
1055 ipr_err("Array Member %d:\n", i);
1057 ipr_log_vpd(&array_entry->vpd);
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");
1063 ipr_err_separator;
1065 if (i == 9)
1066 array_entry = error->array_member2;
1067 else
1068 array_entry++;
1073 * ipr_log_hex_data - Log additional hex IOA error data.
1074 * @data: IOA error data
1075 * @len: data length
1077 * Return value:
1078 * none
1080 static void ipr_log_hex_data(u32 *data, int len)
1082 int i;
1084 if (len == 0)
1085 return;
1087 for (i = 0; i < len / 4; i += 4) {
1088 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
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]));
1097 * ipr_log_dual_ioa_error - Log a dual adapter error.
1098 * @ioa_cfg: ioa config struct
1099 * @hostrcb: hostrcb struct
1101 * Return value:
1102 * none
1104 static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
1105 struct ipr_hostrcb *hostrcb)
1107 struct ipr_hostrcb_type_07_error *error;
1109 error = &hostrcb->hcam.u.error.u.type_07_error;
1110 error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
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)));
1122 * ipr_log_generic_error - Log an adapter error.
1123 * @ioa_cfg: ioa config struct
1124 * @hostrcb: hostrcb struct
1126 * Return value:
1127 * none
1129 static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1130 struct ipr_hostrcb *hostrcb)
1132 ipr_log_hex_data(hostrcb->hcam.u.raw.data,
1133 be32_to_cpu(hostrcb->hcam.length));
1137 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1138 * @ioasc: IOASC
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.
1144 * Return value:
1145 * index into the ipr_error_table
1147 static u32 ipr_get_error(u32 ioasc)
1149 int i;
1151 for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
1152 if (ipr_error_table[i].ioasc == ioasc)
1153 return i;
1155 return 0;
1159 * ipr_handle_log_data - Log an adapter error.
1160 * @ioa_cfg: ioa config struct
1161 * @hostrcb: hostrcb struct
1163 * This function logs an adapter error to the system.
1165 * Return value:
1166 * none
1168 static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1169 struct ipr_hostrcb *hostrcb)
1171 u32 ioasc;
1172 int error_index;
1174 if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1175 return;
1177 if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1178 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1180 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
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);
1189 error_index = ipr_get_error(ioasc);
1191 if (!ipr_error_table[error_index].log_hcam)
1192 return;
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);
1202 /* Set indication we have logged an error */
1203 ioa_cfg->errors_logged++;
1205 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
1206 return;
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));
1210 switch (hostrcb->hcam.overlay_id) {
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;
1221 case IPR_HOST_RCB_OVERLAY_ID_7:
1222 ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
1223 break;
1224 case IPR_HOST_RCB_OVERLAY_ID_1:
1225 case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1226 default:
1227 ipr_log_generic_error(ioa_cfg, hostrcb);
1228 break;
1233 * ipr_process_error - Op done function for an adapter error log.
1234 * @ipr_cmd: ipr command struct
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.
1240 * Return value:
1241 * none
1243 static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
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);
1249 list_del(&hostrcb->queue);
1250 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
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);
1259 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1263 * ipr_timeout - An internally generated op has timed out.
1264 * @ipr_cmd: ipr command struct
1266 * This function blocks host requests and initiates an
1267 * adapter reset.
1269 * Return value:
1270 * none
1272 static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1274 unsigned long lock_flags = 0;
1275 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1277 ENTER;
1278 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1280 ioa_cfg->errors_logged++;
1281 dev_err(&ioa_cfg->pdev->dev,
1282 "Adapter being reset due to command timeout.\n");
1284 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1285 ioa_cfg->sdt_state = GET_DUMP;
1287 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1288 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1290 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1291 LEAVE;
1295 * ipr_oper_timeout - Adapter timed out transitioning to operational
1296 * @ipr_cmd: ipr command struct
1298 * This function blocks host requests and initiates an
1299 * adapter reset.
1301 * Return value:
1302 * none
1304 static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
1306 unsigned long lock_flags = 0;
1307 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1309 ENTER;
1310 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1312 ioa_cfg->errors_logged++;
1313 dev_err(&ioa_cfg->pdev->dev,
1314 "Adapter timed out transitioning to operational.\n");
1316 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1317 ioa_cfg->sdt_state = GET_DUMP;
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);
1325 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1326 LEAVE;
1330 * ipr_reset_reload - Reset/Reload the IOA
1331 * @ioa_cfg: ioa config struct
1332 * @shutdown_type: shutdown type
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
1339 static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1340 enum ipr_shutdown_type shutdown_type)
1342 if (!ioa_cfg->in_reset_reload)
1343 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
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);
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;
1356 return SUCCESS;
1360 * ipr_find_ses_entry - Find matching SES in SES table
1361 * @res: resource entry struct of SES
1363 * Return value:
1364 * pointer to SES table entry / NULL on failure
1366 static const struct ipr_ses_table_entry *
1367 ipr_find_ses_entry(struct ipr_resource_entry *res)
1369 int i, j, matches;
1370 const struct ipr_ses_table_entry *ste = ipr_ses_table;
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++;
1383 if (matches == IPR_PROD_ID_LEN)
1384 return ste;
1387 return NULL;
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
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).
1402 static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
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);
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;
1413 if (bus != res->cfgte.res_addr.bus)
1414 continue;
1416 if (!(ste = ipr_find_ses_entry(res)))
1417 continue;
1419 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1422 return max_xfer_rate;
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
1430 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1432 * Return value:
1433 * 0 on success / other on failure
1435 static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1437 volatile u32 pcii_reg;
1438 int delay = 1;
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);
1444 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1445 return 0;
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);
1453 delay += delay;
1455 return -EIO;
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
1465 * Return value:
1466 * 0 on success / -EIO on failure
1468 static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1469 u32 start_addr,
1470 __be32 *dest, u32 length_in_words)
1472 volatile u32 temp_pcii_reg;
1473 int i, delay = 0;
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);
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;
1487 /* Signal LDUMP interlocked - clear IO debug ack */
1488 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1489 ioa_cfg->regs.clr_interrupt_reg);
1491 /* Write Mailbox with starting address */
1492 writel(start_addr, ioa_cfg->ioa_mailbox);
1494 /* Signal address valid - clear IOA Reset alert */
1495 writel(IPR_UPROCI_RESET_ALERT,
1496 ioa_cfg->regs.clr_uproc_interrupt_reg);
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;
1507 /* Read data from mailbox and increment destination pointer */
1508 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1509 dest++;
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);
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);
1523 writel(IPR_UPROCI_IO_DEBUG_ALERT,
1524 ioa_cfg->regs.clr_uproc_interrupt_reg);
1526 /* Signal dump data received - Clear IO debug Ack */
1527 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1528 ioa_cfg->regs.clr_interrupt_reg);
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);
1535 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
1536 return 0;
1538 udelay(10);
1539 delay += 10;
1542 return 0;
1545 #ifdef CONFIG_SCSI_IPR_DUMP
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
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
1557 static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
1558 unsigned long pci_address, u32 length)
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;
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);
1572 if (!page) {
1573 ipr_trace;
1574 return bytes_copied;
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];
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);
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)));
1596 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1598 if (!rc) {
1599 ioa_dump->page_offset += cur_len;
1600 bytes_copied += cur_len;
1601 } else {
1602 ipr_trace;
1603 break;
1605 schedule();
1608 return bytes_copied;
1612 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
1613 * @hdr: dump entry header struct
1615 * Return value:
1616 * nothing
1618 static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
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;
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
1631 * Return value:
1632 * nothing
1634 static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
1635 struct ipr_driver_dump *driver_dump)
1637 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
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++;
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
1657 * Return value:
1658 * nothing
1660 static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
1661 struct ipr_driver_dump *driver_dump)
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++;
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
1678 * Return value:
1679 * nothing
1681 static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
1682 struct ipr_driver_dump *driver_dump)
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++;
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
1699 * Return value:
1700 * nothing
1702 static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
1703 struct ipr_driver_dump *driver_dump)
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++;
1716 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
1717 * @ioa_cfg: ioa config struct
1718 * @dump: dump struct
1720 * Return value:
1721 * nothing
1723 static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
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;
1734 ENTER;
1736 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1738 if (ioa_cfg->sdt_state != GET_DUMP) {
1739 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1740 return;
1743 start_addr = readl(ioa_cfg->ioa_mailbox);
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;
1752 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
1754 driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
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;
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);
1769 /* Update dump_header */
1770 driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
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;
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;
1785 rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
1786 sizeof(struct ipr_sdt) / sizeof(__be32));
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;
1799 num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
1801 if (num_entries > IPR_NUM_SDT_ENTRIES)
1802 num_entries = IPR_NUM_SDT_ENTRIES;
1804 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
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;
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);
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;
1824 /* Copy data from adapter to driver buffers */
1825 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
1826 bytes_to_copy);
1828 ioa_dump->hdr.len += bytes_copied;
1830 if (bytes_copied != bytes_to_copy) {
1831 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1832 break;
1838 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
1840 /* Update dump_header */
1841 driver_dump->hdr.len += ioa_dump->hdr.len;
1842 wmb();
1843 ioa_cfg->sdt_state = DUMP_OBTAINED;
1844 LEAVE;
1847 #else
1848 #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
1849 #endif
1852 * ipr_release_dump - Free adapter dump memory
1853 * @kref: kref struct
1855 * Return value:
1856 * nothing
1858 static void ipr_release_dump(struct kref *kref)
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;
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);
1871 for (i = 0; i < dump->ioa_dump.next_page_index; i++)
1872 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
1874 kfree(dump);
1875 LEAVE;
1879 * ipr_worker_thread - Worker thread
1880 * @data: ioa config struct
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.
1886 * Return value:
1887 * nothing
1889 static void ipr_worker_thread(void *data)
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;
1899 ENTER;
1900 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
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;
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);
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;
1920 restart:
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;
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);
1940 break;
1943 } while(did_work);
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;
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;
1962 #ifdef CONFIG_SCSI_IPR_TRACE
1964 * ipr_read_trace - Dump the adapter trace
1965 * @kobj: kobject struct
1966 * @buf: buffer
1967 * @off: offset
1968 * @count: buffer size
1970 * Return value:
1971 * number of bytes printed to buffer
1973 static ssize_t ipr_read_trace(struct kobject *kobj, char *buf,
1974 loff_t off, size_t count)
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;
1983 if (off > size)
1984 return 0;
1985 if (off + count > size) {
1986 size -= off;
1987 count = size;
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;
1996 static struct bin_attribute ipr_trace_attr = {
1997 .attr = {
1998 .name = "trace",
1999 .mode = S_IRUGO,
2001 .size = 0,
2002 .read = ipr_read_trace,
2004 #endif
2006 static 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" }
2016 * ipr_show_write_caching - Show the write caching attribute
2017 * @class_dev: class device struct
2018 * @buf: buffer
2020 * Return value:
2021 * number of bytes printed to buffer
2023 static ssize_t ipr_show_write_caching(struct class_device *class_dev, char *buf)
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;
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;
2037 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2038 return len;
2043 * ipr_store_write_caching - Enable/disable adapter write cache
2044 * @class_dev: class_device struct
2045 * @buf: buffer
2046 * @count: buffer size
2048 * This function will enable/disable adapter write cache.
2050 * Return value:
2051 * count on success / other on failure
2053 static ssize_t ipr_store_write_caching(struct class_device *class_dev,
2054 const char *buf, size_t count)
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;
2062 if (!capable(CAP_SYS_ADMIN))
2063 return -EACCES;
2064 if (ioa_cfg->cache_state == CACHE_NONE)
2065 return -EINVAL;
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;
2074 if (new_state != CACHE_DISABLED && new_state != CACHE_ENABLED)
2075 return -EINVAL;
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;
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);
2091 return count;
2094 static struct class_device_attribute ipr_ioa_cache_attr = {
2095 .attr = {
2096 .name = "write_cache",
2097 .mode = S_IRUGO | S_IWUSR,
2099 .show = ipr_show_write_caching,
2100 .store = ipr_store_write_caching
2104 * ipr_show_fw_version - Show the firmware version
2105 * @class_dev: class device struct
2106 * @buf: buffer
2108 * Return value:
2109 * number of bytes printed to buffer
2111 static ssize_t ipr_show_fw_version(struct class_device *class_dev, char *buf)
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;
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;
2128 static struct class_device_attribute ipr_fw_version_attr = {
2129 .attr = {
2130 .name = "fw_version",
2131 .mode = S_IRUGO,
2133 .show = ipr_show_fw_version,
2137 * ipr_show_log_level - Show the adapter's error logging level
2138 * @class_dev: class device struct
2139 * @buf: buffer
2141 * Return value:
2142 * number of bytes printed to buffer
2144 static ssize_t ipr_show_log_level(struct class_device *class_dev, char *buf)
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;
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;
2158 * ipr_store_log_level - Change the adapter's error logging level
2159 * @class_dev: class device struct
2160 * @buf: buffer
2162 * Return value:
2163 * number of bytes printed to buffer
2165 static ssize_t ipr_store_log_level(struct class_device *class_dev,
2166 const char *buf, size_t count)
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;
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);
2178 static struct class_device_attribute ipr_log_level_attr = {
2179 .attr = {
2180 .name = "log_level",
2181 .mode = S_IRUGO | S_IWUSR,
2183 .show = ipr_show_log_level,
2184 .store = ipr_store_log_level
2188 * ipr_store_diagnostics - IOA Diagnostics interface
2189 * @class_dev: class_device struct
2190 * @buf: buffer
2191 * @count: buffer size
2193 * This function will reset the adapter and wait a reasonable
2194 * amount of time for any errors that the adapter might log.
2196 * Return value:
2197 * count on success / other on failure
2199 static ssize_t ipr_store_diagnostics(struct class_device *class_dev,
2200 const char *buf, size_t count)
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;
2207 if (!capable(CAP_SYS_ADMIN))
2208 return -EACCES;
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);
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);
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;
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);
2231 return rc;
2234 static struct class_device_attribute ipr_diagnostics_attr = {
2235 .attr = {
2236 .name = "run_diagnostics",
2237 .mode = S_IWUSR,
2239 .store = ipr_store_diagnostics
2243 * ipr_show_adapter_state - Show the adapter's state
2244 * @class_dev: class device struct
2245 * @buf: buffer
2247 * Return value:
2248 * number of bytes printed to buffer
2250 static ssize_t ipr_show_adapter_state(struct class_device *class_dev, char *buf)
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;
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;
2267 * ipr_store_adapter_state - Change adapter state
2268 * @class_dev: class_device struct
2269 * @buf: buffer
2270 * @count: buffer size
2272 * This function will change the adapter's state.
2274 * Return value:
2275 * count on success / other on failure
2277 static ssize_t ipr_store_adapter_state(struct class_device *class_dev,
2278 const char *buf, size_t count)
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;
2285 if (!capable(CAP_SYS_ADMIN))
2286 return -EACCES;
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);
2295 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2296 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2298 return result;
2301 static struct class_device_attribute ipr_ioa_state_attr = {
2302 .attr = {
2303 .name = "state",
2304 .mode = S_IRUGO | S_IWUSR,
2306 .show = ipr_show_adapter_state,
2307 .store = ipr_store_adapter_state
2311 * ipr_store_reset_adapter - Reset the adapter
2312 * @class_dev: class_device struct
2313 * @buf: buffer
2314 * @count: buffer size
2316 * This function will reset the adapter.
2318 * Return value:
2319 * count on success / other on failure
2321 static ssize_t ipr_store_reset_adapter(struct class_device *class_dev,
2322 const char *buf, size_t count)
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;
2329 if (!capable(CAP_SYS_ADMIN))
2330 return -EACCES;
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);
2338 return result;
2341 static struct class_device_attribute ipr_ioa_reset_attr = {
2342 .attr = {
2343 .name = "reset_host",
2344 .mode = S_IWUSR,
2346 .store = ipr_store_reset_adapter
2350 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2351 * @buf_len: buffer length
2353 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2354 * list to use for microcode download
2356 * Return value:
2357 * pointer to sglist / NULL on failure
2359 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2361 int sg_size, order, bsize_elem, num_elem, i, j;
2362 struct ipr_sglist *sglist;
2363 struct scatterlist *scatterlist;
2364 struct page *page;
2366 /* Get the minimum size per scatter/gather element */
2367 sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2369 /* Get the actual size per element */
2370 order = get_order(sg_size);
2372 /* Determine the actual number of bytes per element */
2373 bsize_elem = PAGE_SIZE * (1 << order);
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;
2381 /* Allocate a scatter/gather list for the DMA */
2382 sglist = kzalloc(sizeof(struct ipr_sglist) +
2383 (sizeof(struct scatterlist) * (num_elem - 1)),
2384 GFP_KERNEL);
2386 if (sglist == NULL) {
2387 ipr_trace;
2388 return NULL;
2391 scatterlist = sglist->scatterlist;
2393 sglist->order = order;
2394 sglist->num_sg = num_elem;
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;
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;
2409 scatterlist[i].page = page;
2412 return sglist;
2416 * ipr_free_ucode_buffer - Frees a microcode download buffer
2417 * @p_dnld: scatter/gather list pointer
2419 * Free a DMA'able ucode download buffer previously allocated with
2420 * ipr_alloc_ucode_buffer
2422 * Return value:
2423 * nothing
2425 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2427 int i;
2429 for (i = 0; i < sglist->num_sg; i++)
2430 __free_pages(sglist->scatterlist[i].page, sglist->order);
2432 kfree(sglist);
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
2441 * Copy a microcode image from a user buffer into a buffer allocated by
2442 * ipr_alloc_ucode_buffer
2444 * Return value:
2445 * 0 on success / other on failure
2447 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2448 u8 *buffer, u32 len)
2450 int bsize_elem, i, result = 0;
2451 struct scatterlist *scatterlist;
2452 void *kaddr;
2454 /* Determine the actual number of bytes per element */
2455 bsize_elem = PAGE_SIZE * (1 << sglist->order);
2457 scatterlist = sglist->scatterlist;
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);
2464 scatterlist[i].length = bsize_elem;
2466 if (result != 0) {
2467 ipr_trace;
2468 return result;
2472 if (len % bsize_elem) {
2473 kaddr = kmap(scatterlist[i].page);
2474 memcpy(kaddr, buffer, len % bsize_elem);
2475 kunmap(scatterlist[i].page);
2477 scatterlist[i].length = len % bsize_elem;
2480 sglist->buffer_len = len;
2481 return result;
2485 * ipr_build_ucode_ioadl - Build a microcode download IOADL
2486 * @ipr_cmd: ipr command struct
2487 * @sglist: scatter/gather list
2489 * Builds a microcode download IOA data list (IOADL).
2492 static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
2493 struct ipr_sglist *sglist)
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;
2500 ipr_cmd->dma_use_sg = sglist->num_dma_sg;
2501 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
2502 ioarcb->write_data_transfer_length = cpu_to_be32(sglist->buffer_len);
2503 ioarcb->write_ioadl_len =
2504 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
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]));
2513 ioadl[i-1].flags_and_data_len |=
2514 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
2518 * ipr_update_ioa_ucode - Update IOA's microcode
2519 * @ioa_cfg: ioa config struct
2520 * @sglist: scatter/gather list
2522 * Initiate an adapter reset to update the IOA's microcode
2524 * Return value:
2525 * 0 on success / -EIO on failure
2527 static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
2528 struct ipr_sglist *sglist)
2530 unsigned long lock_flags;
2532 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
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;
2541 sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
2542 sglist->num_sg, DMA_TO_DEVICE);
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");
2548 return -EIO;
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);
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);
2559 return 0;
2563 * ipr_store_update_fw - Update the firmware on the adapter
2564 * @class_dev: class_device struct
2565 * @buf: buffer
2566 * @count: buffer size
2568 * This function will update the firmware on the adapter.
2570 * Return value:
2571 * count on success / other on failure
2573 static ssize_t ipr_store_update_fw(struct class_device *class_dev,
2574 const char *buf, size_t count)
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;
2581 char fname[100];
2582 char *src;
2583 int len, result, dnld_size;
2585 if (!capable(CAP_SYS_ADMIN))
2586 return -EACCES;
2588 len = snprintf(fname, 99, "%s", buf);
2589 fname[len-1] = '\0';
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;
2596 image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
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;
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);
2610 if (!sglist) {
2611 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
2612 release_firmware(fw_entry);
2613 return -ENOMEM;
2616 result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
2618 if (result) {
2619 dev_err(&ioa_cfg->pdev->dev,
2620 "Microcode buffer copy to DMA buffer failed\n");
2621 goto out;
2624 result = ipr_update_ioa_ucode(ioa_cfg, sglist);
2626 if (!result)
2627 result = count;
2628 out:
2629 ipr_free_ucode_buffer(sglist);
2630 release_firmware(fw_entry);
2631 return result;
2634 static struct class_device_attribute ipr_update_fw_attr = {
2635 .attr = {
2636 .name = "update_fw",
2637 .mode = S_IWUSR,
2639 .store = ipr_store_update_fw
2642 static struct class_device_attribute *ipr_ioa_attrs[] = {
2643 &ipr_fw_version_attr,
2644 &ipr_log_level_attr,
2645 &ipr_diagnostics_attr,
2646 &ipr_ioa_state_attr,
2647 &ipr_ioa_reset_attr,
2648 &ipr_update_fw_attr,
2649 &ipr_ioa_cache_attr,
2650 NULL,
2653 #ifdef CONFIG_SCSI_IPR_DUMP
2655 * ipr_read_dump - Dump the adapter
2656 * @kobj: kobject struct
2657 * @buf: buffer
2658 * @off: offset
2659 * @count: buffer size
2661 * Return value:
2662 * number of bytes printed to buffer
2664 static ssize_t ipr_read_dump(struct kobject *kobj, char *buf,
2665 loff_t off, size_t count)
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;
2676 if (!capable(CAP_SYS_ADMIN))
2677 return -EACCES;
2679 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2680 dump = ioa_cfg->dump;
2682 if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
2683 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2684 return 0;
2686 kref_get(&dump->kref);
2687 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2689 if (off > dump->driver_dump.hdr.len) {
2690 kref_put(&dump->kref, ipr_release_dump);
2691 return 0;
2694 if (off + count > dump->driver_dump.hdr.len) {
2695 count = dump->driver_dump.hdr.len - off;
2696 rc = count;
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;
2711 off -= sizeof(dump->driver_dump);
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;
2725 off -= offsetof(struct ipr_ioa_dump, ioa_data);
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;
2740 kref_put(&dump->kref, ipr_release_dump);
2741 return rc;
2745 * ipr_alloc_dump - Prepare for adapter dump
2746 * @ioa_cfg: ioa config struct
2748 * Return value:
2749 * 0 on success / other on failure
2751 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
2753 struct ipr_dump *dump;
2754 unsigned long lock_flags = 0;
2756 ENTER;
2757 dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
2759 if (!dump) {
2760 ipr_err("Dump memory allocation failed\n");
2761 return -ENOMEM;
2764 kref_init(&dump->kref);
2765 dump->ioa_cfg = ioa_cfg;
2767 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2769 if (INACTIVE != ioa_cfg->sdt_state) {
2770 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2771 kfree(dump);
2772 return 0;
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);
2781 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2783 LEAVE;
2784 return 0;
2788 * ipr_free_dump - Free adapter dump memory
2789 * @ioa_cfg: ioa config struct
2791 * Return value:
2792 * 0 on success / other on failure
2794 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
2796 struct ipr_dump *dump;
2797 unsigned long lock_flags = 0;
2799 ENTER;
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;
2808 ioa_cfg->dump = NULL;
2809 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2811 kref_put(&dump->kref, ipr_release_dump);
2813 LEAVE;
2814 return 0;
2818 * ipr_write_dump - Setup dump state of adapter
2819 * @kobj: kobject struct
2820 * @buf: buffer
2821 * @off: offset
2822 * @count: buffer size
2824 * Return value:
2825 * number of bytes printed to buffer
2827 static ssize_t ipr_write_dump(struct kobject *kobj, char *buf,
2828 loff_t off, size_t count)
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;
2835 if (!capable(CAP_SYS_ADMIN))
2836 return -EACCES;
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;
2845 if (rc)
2846 return rc;
2847 else
2848 return count;
2851 static struct bin_attribute ipr_dump_attr = {
2852 .attr = {
2853 .name = "dump",
2854 .mode = S_IRUSR | S_IWUSR,
2856 .size = 0,
2857 .read = ipr_read_dump,
2858 .write = ipr_write_dump
2860 #else
2861 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
2862 #endif
2865 * ipr_change_queue_depth - Change the device's queue depth
2866 * @sdev: scsi device struct
2867 * @qdepth: depth to set
2869 * Return value:
2870 * actual depth set
2872 static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
2874 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2875 return sdev->queue_depth;
2879 * ipr_change_queue_type - Change the device's queue type
2880 * @dsev: scsi device struct
2881 * @tag_type: type of tags to use
2883 * Return value:
2884 * actual queue type set
2886 static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
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;
2892 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2893 res = (struct ipr_resource_entry *)sdev->hostdata;
2895 if (res) {
2896 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
2898 * We don't bother quiescing the device here since the
2899 * adapter firmware does it for us.
2901 scsi_set_tag_type(sdev, tag_type);
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;
2912 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2913 return tag_type;
2917 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
2918 * @dev: device struct
2919 * @buf: buffer
2921 * Return value:
2922 * number of bytes printed to buffer
2924 static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
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;
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;
2940 static struct device_attribute ipr_adapter_handle_attr = {
2941 .attr = {
2942 .name = "adapter_handle",
2943 .mode = S_IRUSR,
2945 .show = ipr_show_adapter_handle
2948 static struct device_attribute *ipr_dev_attrs[] = {
2949 &ipr_adapter_handle_attr,
2950 NULL,
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.
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.
2964 * Return value:
2965 * 0 on success
2967 static int ipr_biosparam(struct scsi_device *sdev,
2968 struct block_device *block_device,
2969 sector_t capacity, int *parm)
2971 int heads, sectors;
2972 sector_t cylinders;
2974 heads = 128;
2975 sectors = 32;
2977 cylinders = capacity;
2978 sector_div(cylinders, (128 * 32));
2980 /* return result */
2981 parm[0] = heads;
2982 parm[1] = sectors;
2983 parm[2] = cylinders;
2985 return 0;
2989 * ipr_slave_destroy - Unconfigure a SCSI device
2990 * @sdev: scsi device struct
2992 * Return value:
2993 * nothing
2995 static void ipr_slave_destroy(struct scsi_device *sdev)
2997 struct ipr_resource_entry *res;
2998 struct ipr_ioa_cfg *ioa_cfg;
2999 unsigned long lock_flags = 0;
3001 ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
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;
3009 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3013 * ipr_slave_configure - Configure a SCSI device
3014 * @sdev: scsi device struct
3016 * This function configures the specified scsi device.
3018 * Return value:
3019 * 0 on success
3021 static int ipr_slave_configure(struct scsi_device *sdev)
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;
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;
3032 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
3033 sdev->scsi_level = 4;
3034 sdev->no_uld_attach = 1;
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);
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);
3044 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3045 return 0;
3049 * ipr_slave_alloc - Prepare for commands to a device.
3050 * @sdev: scsi device struct
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.
3057 * Return value:
3058 * 0 on success / -ENXIO if device does not exist
3060 static int ipr_slave_alloc(struct scsi_device *sdev)
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;
3065 int rc = -ENXIO;
3067 sdev->hostdata = NULL;
3069 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
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;
3080 rc = 0;
3081 break;
3085 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3087 return rc;
3091 * ipr_eh_host_reset - Reset the host adapter
3092 * @scsi_cmd: scsi command struct
3094 * Return value:
3095 * SUCCESS / FAILED
3097 static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
3099 struct ipr_ioa_cfg *ioa_cfg;
3100 int rc;
3102 ENTER;
3103 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3105 dev_err(&ioa_cfg->pdev->dev,
3106 "Adapter being reset as a result of error recovery.\n");
3108 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3109 ioa_cfg->sdt_state = GET_DUMP;
3111 rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
3113 LEAVE;
3114 return rc;
3117 static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
3119 int rc;
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);
3125 return rc;
3129 * ipr_eh_dev_reset - Reset the device
3130 * @scsi_cmd: scsi command struct
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.
3136 * Return value:
3137 * SUCCESS / FAILED
3139 static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
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;
3147 ENTER;
3148 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3149 res = scsi_cmd->device->hostdata;
3151 if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
3152 return FAILED;
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
3159 if (ioa_cfg->in_reset_reload)
3160 return FAILED;
3161 if (ioa_cfg->ioa_is_dead)
3162 return FAILED;
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;
3171 res->resetting_device = 1;
3173 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
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;
3180 ipr_sdev_err(scsi_cmd->device, "Resetting device\n");
3181 ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3183 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3185 res->resetting_device = 0;
3187 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3189 LEAVE;
3190 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
3193 static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
3195 int rc;
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);
3201 return rc;
3205 * ipr_bus_reset_done - Op done function for bus reset.
3206 * @ipr_cmd: ipr command struct
3208 * This function is the op done function for a bus reset
3210 * Return value:
3211 * none
3213 static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
3215 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3216 struct ipr_resource_entry *res;
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;
3228 * If abort has not completed, indicate the reset has, else call the
3229 * abort's done function to wake the sleeping eh thread
3231 if (ipr_cmd->sibling->sibling)
3232 ipr_cmd->sibling->sibling = NULL;
3233 else
3234 ipr_cmd->sibling->done(ipr_cmd->sibling);
3236 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3237 LEAVE;
3241 * ipr_abort_timeout - An abort task has timed out
3242 * @ipr_cmd: ipr command struct
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.
3248 * Return value:
3249 * none
3251 static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
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;
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;
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;
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;
3281 * ipr_cancel_op - Cancel specified op
3282 * @scsi_cmd: scsi command struct
3284 * This function cancels specified op.
3286 * Return value:
3287 * SUCCESS / FAILED
3289 static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
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;
3298 ENTER;
3299 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3300 res = scsi_cmd->device->hostdata;
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
3306 if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
3307 return FAILED;
3308 if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
3309 return FAILED;
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;
3319 if (!op_found)
3320 return SUCCESS;
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;
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);
3334 * If the abort task timed out and we sent a bus reset, we will get
3335 * one the following responses to the abort
3337 if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
3338 ioasc = 0;
3339 ipr_trace;
3342 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3343 res->needs_sync_complete = 1;
3345 LEAVE;
3346 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
3350 * ipr_eh_abort - Abort a single op
3351 * @scsi_cmd: scsi command struct
3353 * Return value:
3354 * SUCCESS / FAILED
3356 static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
3358 unsigned long flags;
3359 int rc;
3361 ENTER;
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);
3367 LEAVE;
3368 return rc;
3372 * ipr_handle_other_interrupt - Handle "other" interrupts
3373 * @ioa_cfg: ioa config struct
3374 * @int_reg: interrupt register
3376 * Return value:
3377 * IRQ_NONE / IRQ_HANDLED
3379 static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
3380 volatile u32 int_reg)
3382 irqreturn_t rc = IRQ_HANDLED;
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);
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);
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);
3402 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3403 ioa_cfg->sdt_state = GET_DUMP;
3405 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
3406 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3409 return rc;
3413 * ipr_isr - Interrupt service routine
3414 * @irq: irq number
3415 * @devp: pointer to ioa config struct
3416 * @regs: pt_regs struct
3418 * Return value:
3419 * IRQ_NONE / IRQ_HANDLED
3421 static irqreturn_t ipr_isr(int irq, void *devp, struct pt_regs *regs)
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;
3431 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
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;
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;
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;
3448 while (1) {
3449 ipr_cmd = NULL;
3451 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
3452 ioa_cfg->toggle_bit) {
3454 cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
3455 IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
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");
3461 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3462 ioa_cfg->sdt_state = GET_DUMP;
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;
3469 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
3471 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3473 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
3475 list_del(&ipr_cmd->queue);
3476 del_timer(&ipr_cmd->timer);
3477 ipr_cmd->done(ipr_cmd);
3479 rc = IRQ_HANDLED;
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;
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;
3497 if (unlikely(rc == IRQ_NONE))
3498 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
3500 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3501 return rc;
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
3509 * Return value:
3510 * 0 on success / -1 on failure
3512 static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
3513 struct ipr_cmnd *ipr_cmd)
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;
3523 length = scsi_cmd->request_bufflen;
3525 if (length == 0)
3526 return 0;
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);
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);
3547 sglist = scsi_cmd->request_buffer;
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]));
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));
3574 ipr_cmd->dma_handle = pci_map_single(ioa_cfg->pdev,
3575 scsi_cmd->request_buffer, length,
3576 scsi_cmd->sc_data_direction);
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");
3588 return -1;
3592 * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
3593 * @scsi_cmd: scsi command struct
3595 * Return value:
3596 * task attributes
3598 static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
3600 u8 tag[2];
3601 u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
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;
3617 return rc;
3621 * ipr_erp_done - Process completion of ERP for a device
3622 * @ipr_cmd: ipr command struct
3624 * This function copies the sense buffer into the scsi_cmd
3625 * struct and pushes the scsi_done function.
3627 * Return value:
3628 * nothing
3630 static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
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);
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);
3646 if (res) {
3647 res->needs_sync_complete = 1;
3648 res->in_erp = 0;
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);
3656 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
3657 * @ipr_cmd: ipr command struct
3659 * Return value:
3660 * none
3662 static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
3664 struct ipr_ioarcb *ioarcb;
3665 struct ipr_ioasa *ioasa;
3667 ioarcb = &ipr_cmd->ioarcb;
3668 ioasa = &ipr_cmd->ioasa;
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;
3680 * ipr_erp_request_sense - Send request sense to a device
3681 * @ipr_cmd: ipr command struct
3683 * This function sends a request sense to a device as a result
3684 * of a check condition.
3686 * Return value:
3687 * nothing
3689 static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
3691 struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3692 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3694 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3695 ipr_erp_done(ipr_cmd);
3696 return;
3699 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
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);
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);
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);
3718 ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
3719 IPR_REQUEST_SENSE_TIMEOUT * 2);
3723 * ipr_erp_cancel_all - Send cancel all to a device
3724 * @ipr_cmd: ipr command struct
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.
3731 * Return value:
3732 * nothing
3734 static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
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;
3740 res->in_erp = 1;
3742 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3744 if (!scsi_get_tag_type(scsi_cmd->device)) {
3745 ipr_erp_request_sense(ipr_cmd);
3746 return;
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;
3753 ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
3754 IPR_CANCEL_ALL_TIMEOUT);
3758 * ipr_dump_ioasa - Dump contents of IOASA
3759 * @ioa_cfg: ioa config struct
3760 * @ipr_cmd: ipr command struct
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.
3766 * Return value:
3767 * none
3769 static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
3770 struct ipr_cmnd *ipr_cmd)
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;
3779 ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
3781 if (0 == ioasc)
3782 return;
3784 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
3785 return;
3787 error_index = ipr_get_error(ioasc);
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;
3794 if (ipr_error_table[error_index].log_ioasa == 0)
3795 return;
3798 ipr_sdev_err(ipr_cmd->scsi_cmd->device, "%s\n",
3799 ipr_error_table[error_index].error);
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]);
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);
3814 ipr_err("IOASA Dump:\n");
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]));
3826 * ipr_gen_sense - Generate SCSI sense data from an IOASA
3827 * @ioasa: IOASA
3828 * @sense_buf: sense data buffer
3830 * Return value:
3831 * none
3833 static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
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);
3841 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
3843 if (ioasc >= IPR_FIRST_DRIVER_IOASC)
3844 return;
3846 ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
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);
3856 sense_buf[7] = 12;
3857 sense_buf[8] = 0;
3858 sense_buf[9] = 0x0A;
3859 sense_buf[10] = 0x80;
3861 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
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;
3868 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
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);
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 */
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;
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);
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;
3911 sense_buf[7] = 6; /* additional length */
3917 * ipr_erp_start - Process an error response for a SCSI op
3918 * @ioa_cfg: ioa config struct
3919 * @ipr_cmd: ipr command struct
3921 * This function determines whether or not to initiate ERP
3922 * on the affected device.
3924 * Return value:
3925 * nothing
3927 static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
3928 struct ipr_cmnd *ipr_cmd)
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);
3934 if (!res) {
3935 ipr_scsi_eh_done(ipr_cmd);
3936 return;
3939 if (ipr_is_gscsi(res))
3940 ipr_dump_ioasa(ioa_cfg, ipr_cmd);
3941 else
3942 ipr_gen_sense(ipr_cmd);
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:
3949 case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
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 */
3962 case IPR_IOASA_IR_DUAL_IOA_DISABLED:
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:
3968 * Report the bus reset and ask for a retry. The device
3969 * will give CC/UA the next command.
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;
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;
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);
3999 * ipr_scsi_done - mid-layer done function
4000 * @ipr_cmd: ipr command struct
4002 * This function is invoked by the interrupt handler for
4003 * ops generated by the SCSI mid-layer
4005 * Return value:
4006 * none
4008 static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
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);
4014 scsi_cmd->resid = be32_to_cpu(ipr_cmd->ioasa.residual_data_len);
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);
4025 * ipr_save_ioafp_mode_select - Save adapters mode select data
4026 * @ioa_cfg: ioa config struct
4027 * @scsi_cmd: scsi command struct
4029 * This function saves mode select data for the adapter to
4030 * use following an adapter reset.
4032 * Return value:
4033 * 0 on success / SCSI_MLQUEUE_HOST_BUSY on failure
4035 static int ipr_save_ioafp_mode_select(struct ipr_ioa_cfg *ioa_cfg,
4036 struct scsi_cmnd *scsi_cmd)
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;
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;
4054 * ipr_queuecommand - Queue a mid-layer request
4055 * @scsi_cmd: scsi command struct
4056 * @done: done function
4058 * This function queues a request generated by the mid-layer.
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
4065 static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
4066 void (*done) (struct scsi_cmnd *))
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;
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);
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
4084 if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
4085 return SCSI_MLQUEUE_HOST_BUSY;
4088 * FIXME - Create scsi_set_host_offline interface
4089 * and the ioa_is_dead check can be removed
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;
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);
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));
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;
4112 if (res->needs_sync_complete) {
4113 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
4114 res->needs_sync_complete = 0;
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);
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;
4127 if (ipr_is_ioa_resource(res) && scsi_cmd->cmnd[0] == MODE_SELECT)
4128 rc = ipr_save_ioafp_mode_select(ioa_cfg, scsi_cmd);
4130 if (likely(rc == 0))
4131 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
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;
4142 return 0;
4146 * ipr_info - Get information about the card/driver
4147 * @scsi_host: scsi host struct
4149 * Return value:
4150 * pointer to buffer with description string
4152 static const char * ipr_ioa_info(struct Scsi_Host *host)
4154 static char buffer[512];
4155 struct ipr_ioa_cfg *ioa_cfg;
4156 unsigned long lock_flags = 0;
4158 ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
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);
4164 return buffer;
4167 static 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
4192 #ifdef CONFIG_PPC_PSERIES
4193 static 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
4205 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
4206 * @ioa_cfg: ioa cfg struct
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.
4212 * Return value:
4213 * 1 if adapter is not supported / 0 if adapter is supported
4215 static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
4217 u8 rev_id;
4218 int i;
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;
4231 return 0;
4233 #else
4234 #define ipr_invalid_adapter(ioa_cfg) 0
4235 #endif
4238 * ipr_ioa_bringdown_done - IOA bring down completion.
4239 * @ipr_cmd: ipr command struct
4241 * This function processes the completion of an adapter bring down.
4242 * It wakes any reset sleepers.
4244 * Return value:
4245 * IPR_RC_JOB_RETURN
4247 static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
4249 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
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);
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;
4262 return IPR_RC_JOB_RETURN;
4266 * ipr_ioa_reset_done - IOA reset completion.
4267 * @ipr_cmd: ipr command struct
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.
4273 * Return value:
4274 * IPR_RC_JOB_RETURN
4276 static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
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;
4283 ENTER;
4284 ioa_cfg->in_reset_reload = 0;
4285 ioa_cfg->allow_cmds = 1;
4286 ioa_cfg->reset_cmd = NULL;
4287 ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
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;
4295 schedule_work(&ioa_cfg->work_q);
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);
4305 dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
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);
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);
4315 if (!ioa_cfg->allow_cmds)
4316 scsi_block_requests(ioa_cfg->host);
4318 LEAVE;
4319 return IPR_RC_JOB_RETURN;
4323 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
4324 * @supported_dev: supported device struct
4325 * @vpids: vendor product id struct
4327 * Return value:
4328 * none
4330 static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
4331 struct ipr_std_inq_vpids *vpids)
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;
4342 * ipr_set_supported_devs - Send Set Supported Devices for a device
4343 * @ipr_cmd: ipr command struct
4345 * This function send a Set Supported Devices to the adapter
4347 * Return value:
4348 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4350 static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
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;
4358 ipr_cmd->job_step = ipr_ioa_reset_done;
4360 list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
4361 if (!IPR_IS_DASD_DEVICE(res->cfgte.std_inq_data))
4362 continue;
4364 ipr_cmd->u.res = res;
4365 ipr_set_sup_dev_dflt(supp_dev, &res->cfgte.std_inq_data.vpids);
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;
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;
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));
4383 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
4384 IPR_SET_SUP_DEVICE_TIMEOUT);
4386 ipr_cmd->job_step = ipr_set_supported_devs;
4387 return IPR_RC_JOB_RETURN;
4390 return IPR_RC_JOB_CONTINUE;
4394 * ipr_setup_write_cache - Disable write cache if needed
4395 * @ipr_cmd: ipr command struct
4397 * This function sets up adapters write cache to desired setting
4399 * Return value:
4400 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4402 static int ipr_setup_write_cache(struct ipr_cmnd *ipr_cmd)
4404 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
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);
4410 if (ioa_cfg->cache_state != CACHE_DISABLED)
4411 return IPR_RC_JOB_CONTINUE;
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;
4418 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4420 return IPR_RC_JOB_RETURN;
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
4429 * Return value:
4430 * pointer to mode page / NULL on failure
4432 static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
4433 u32 page_code, u32 len)
4435 struct ipr_mode_page_hdr *mode_hdr;
4436 u32 page_length;
4437 u32 length;
4439 if (!mode_pages || (mode_pages->hdr.length == 0))
4440 return NULL;
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);
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);
4459 return NULL;
4463 * ipr_check_term_power - Check for term power errors
4464 * @ioa_cfg: ioa config struct
4465 * @mode_pages: IOAFP mode pages buffer
4467 * Check the IOAFP's mode page 28 for term power errors
4469 * Return value:
4470 * nothing
4472 static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
4473 struct ipr_mode_pages *mode_pages)
4475 int i;
4476 int entry_length;
4477 struct ipr_dev_bus_entry *bus;
4478 struct ipr_mode_page28 *mode_page;
4480 mode_page = ipr_get_mode_page(mode_pages, 0x28,
4481 sizeof(struct ipr_mode_page28));
4483 entry_length = mode_page->entry_length;
4485 bus = mode_page->bus;
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);
4494 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
4499 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
4500 * @ioa_cfg: ioa config struct
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.
4506 * Return value:
4507 * none
4509 static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
4511 u32 max_xfer_rate;
4512 int i;
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);
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;
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
4528 * Updates mode page 28 based on driver configuration
4530 * Return value:
4531 * none
4533 static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
4534 struct ipr_mode_pages *mode_pages)
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;
4541 mode_page = ipr_get_mode_page(mode_pages, 0x28,
4542 sizeof(struct ipr_mode_page28));
4544 entry_length = mode_page->entry_length;
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;
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;
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
4577 * Return value:
4578 * none
4580 static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
4581 __be32 res_handle, u8 parm, u32 dma_addr,
4582 u8 xfer_len)
4584 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4585 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
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;
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);
4602 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
4603 * @ipr_cmd: ipr command struct
4605 * This function sets up the SCSI bus attributes and sends
4606 * a Mode Select for Page 28 to activate them.
4608 * Return value:
4609 * IPR_RC_JOB_RETURN
4611 static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
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;
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;
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);
4634 ipr_cmd->job_step = ipr_setup_write_cache;
4635 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4637 LEAVE;
4638 return IPR_RC_JOB_RETURN;
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
4649 * Return value:
4650 * none
4652 static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
4653 __be32 res_handle,
4654 u8 parm, u32 dma_addr, u8 xfer_len)
4656 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4657 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
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;
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);
4673 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
4674 * @ipr_cmd: ipr command struct
4676 * This function send a Page 28 mode sense to the IOA to
4677 * retrieve SCSI bus attributes.
4679 * Return value:
4680 * IPR_RC_JOB_RETURN
4682 static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
4684 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
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));
4692 ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
4694 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4696 LEAVE;
4697 return IPR_RC_JOB_RETURN;
4701 * ipr_init_res_table - Initialize the resource table
4702 * @ipr_cmd: ipr command struct
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.
4709 * Return value:
4710 * IPR_RC_JOB_CONTINUE
4712 static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
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);
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");
4724 list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
4725 list_move_tail(&res->queue, &old_res);
4727 for (i = 0; i < ioa_cfg->cfg_table->hdr.num_entries; i++) {
4728 cfgte = &ioa_cfg->cfg_table->dev[i];
4729 found = 0;
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;
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;
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;
4754 if (found)
4755 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
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);
4768 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
4770 LEAVE;
4771 return IPR_RC_JOB_CONTINUE;
4775 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
4776 * @ipr_cmd: ipr command struct
4778 * This function sends a Query IOA Configuration command
4779 * to the adapter to retrieve the IOA configuration table.
4781 * Return value:
4782 * IPR_RC_JOB_RETURN
4784 static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
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;
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);
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;
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));
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));
4810 ipr_cmd->job_step = ipr_init_res_table;
4812 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4814 LEAVE;
4815 return IPR_RC_JOB_RETURN;
4819 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
4820 * @ipr_cmd: ipr command struct
4822 * This utility function sends an inquiry to the adapter.
4824 * Return value:
4825 * none
4827 static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
4828 u32 dma_addr, u8 xfer_len)
4830 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4831 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4833 ENTER;
4834 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4835 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
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;
4842 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4843 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
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);
4849 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4850 LEAVE;
4854 * ipr_inquiry_page_supported - Is the given inquiry page supported
4855 * @page0: inquiry page 0 buffer
4856 * @page: page code.
4858 * This function determines if the specified inquiry page is supported.
4860 * Return value:
4861 * 1 if page is supported / 0 if not
4863 static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
4865 int i;
4867 for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
4868 if (page0->page[i] == page)
4869 return 1;
4871 return 0;
4875 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
4876 * @ipr_cmd: ipr command struct
4878 * This function sends a Page 3 inquiry to the adapter
4879 * to retrieve software VPD information.
4881 * Return value:
4882 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4884 static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
4886 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4887 struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
4889 ENTER;
4891 if (!ipr_inquiry_page_supported(page0, 1))
4892 ioa_cfg->cache_state = CACHE_NONE;
4894 ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
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));
4900 LEAVE;
4901 return IPR_RC_JOB_RETURN;
4905 * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
4906 * @ipr_cmd: ipr command struct
4908 * This function sends a Page 0 inquiry to the adapter
4909 * to retrieve supported inquiry pages.
4911 * Return value:
4912 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4914 static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
4916 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4917 char type[5];
4919 ENTER;
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);
4926 ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
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));
4932 LEAVE;
4933 return IPR_RC_JOB_RETURN;
4937 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
4938 * @ipr_cmd: ipr command struct
4940 * This function sends a standard inquiry to the adapter.
4942 * Return value:
4943 * IPR_RC_JOB_RETURN
4945 static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
4947 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4949 ENTER;
4950 ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
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));
4956 LEAVE;
4957 return IPR_RC_JOB_RETURN;
4961 * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
4962 * @ipr_cmd: ipr command struct
4964 * This function send an Identify Host Request Response Queue
4965 * command to establish the HRRQ with the adapter.
4967 * Return value:
4968 * IPR_RC_JOB_RETURN
4970 static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
4972 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4973 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4975 ENTER;
4976 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
4978 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
4979 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
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;
4995 ipr_cmd->job_step = ipr_ioafp_std_inquiry;
4997 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4999 LEAVE;
5000 return IPR_RC_JOB_RETURN;
5004 * ipr_reset_timer_done - Adapter reset timer function
5005 * @ipr_cmd: ipr command struct
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.
5013 * Return value:
5014 * none
5016 static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
5018 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5019 unsigned long lock_flags = 0;
5021 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5023 if (ioa_cfg->reset_cmd == ipr_cmd) {
5024 list_del(&ipr_cmd->queue);
5025 ipr_cmd->done(ipr_cmd);
5028 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5032 * ipr_reset_start_timer - Start a timer for adapter reset job
5033 * @ipr_cmd: ipr command struct
5034 * @timeout: timeout value
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.
5042 * Return value:
5043 * none
5045 static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
5046 unsigned long timeout)
5048 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
5049 ipr_cmd->done = ipr_reset_ioa_job;
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);
5058 * ipr_init_ioa_mem - Initialize ioa_cfg control block
5059 * @ioa_cfg: ioa cfg struct
5061 * Return value:
5062 * nothing
5064 static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
5066 memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
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;
5074 /* Zero out config table */
5075 memset(ioa_cfg->cfg_table, 0, sizeof(struct ipr_config_table));
5079 * ipr_reset_enable_ioa - Enable the IOA following a reset.
5080 * @ipr_cmd: ipr command struct
5082 * This function reinitializes some control blocks and
5083 * enables destructive diagnostics on the adapter.
5085 * Return value:
5086 * IPR_RC_JOB_RETURN
5088 static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
5090 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5091 volatile u32 int_reg;
5093 ENTER;
5094 ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
5095 ipr_init_ioa_mem(ioa_cfg);
5097 ioa_cfg->allow_interrupts = 1;
5098 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
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;
5107 /* Enable destructive diagnostics on IOA */
5108 writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg);
5110 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
5111 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
5113 dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
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);
5122 LEAVE;
5123 return IPR_RC_JOB_RETURN;
5127 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
5128 * @ipr_cmd: ipr command struct
5130 * This function is invoked when an adapter dump has run out
5131 * of processing time.
5133 * Return value:
5134 * IPR_RC_JOB_CONTINUE
5136 static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
5138 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5140 if (ioa_cfg->sdt_state == GET_DUMP)
5141 ioa_cfg->sdt_state = ABORT_DUMP;
5143 ipr_cmd->job_step = ipr_reset_alert;
5145 return IPR_RC_JOB_CONTINUE;
5149 * ipr_unit_check_no_data - Log a unit check/no data error log
5150 * @ioa_cfg: ioa config struct
5152 * Logs an error indicating the adapter unit checked, but for some
5153 * reason, we were unable to fetch the unit check buffer.
5155 * Return value:
5156 * nothing
5158 static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
5160 ioa_cfg->errors_logged++;
5161 dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
5165 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
5166 * @ioa_cfg: ioa config struct
5168 * Fetches the unit check buffer from the adapter by clocking the data
5169 * through the mailbox register.
5171 * Return value:
5172 * nothing
5174 static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
5176 unsigned long mailbox;
5177 struct ipr_hostrcb *hostrcb;
5178 struct ipr_uc_sdt sdt;
5179 int rc, length;
5181 mailbox = readl(ioa_cfg->ioa_mailbox);
5183 if (!ipr_sdt_is_fmt2(mailbox)) {
5184 ipr_unit_check_no_data(ioa_cfg);
5185 return;
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));
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;
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;
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));
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));
5212 if (!rc)
5213 ipr_handle_log_data(ioa_cfg, hostrcb);
5214 else
5215 ipr_unit_check_no_data(ioa_cfg);
5217 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
5221 * ipr_reset_restore_cfg_space - Restore PCI config space.
5222 * @ipr_cmd: ipr command struct
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.
5228 * Return value:
5229 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5231 static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
5233 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5234 int rc;
5236 ENTER;
5237 pci_unblock_user_cfg_access(ioa_cfg->pdev);
5238 rc = pci_restore_state(ioa_cfg->pdev);
5240 if (rc != PCIBIOS_SUCCESSFUL) {
5241 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
5242 return IPR_RC_JOB_CONTINUE;
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;
5250 ipr_fail_all_ops(ioa_cfg);
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;
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;
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;
5273 ENTER;
5274 return IPR_RC_JOB_CONTINUE;
5278 * ipr_reset_start_bist - Run BIST on the adapter.
5279 * @ipr_cmd: ipr command struct
5281 * Description: This function runs BIST on the adapter, then delays 2 seconds.
5283 * Return value:
5284 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5286 static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
5288 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5289 int rc;
5291 ENTER;
5292 pci_block_user_cfg_access(ioa_cfg->pdev);
5293 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
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;
5304 LEAVE;
5305 return rc;
5309 * ipr_reset_allowed - Query whether or not IOA can be reset
5310 * @ioa_cfg: ioa config struct
5312 * Return value:
5313 * 0 if reset not allowed / non-zero if reset is allowed
5315 static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
5317 volatile u32 temp_reg;
5319 temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5320 return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
5324 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
5325 * @ipr_cmd: ipr command struct
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.
5335 * Return value:
5336 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5338 static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
5340 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5341 int rc = IPR_RC_JOB_RETURN;
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;
5351 return rc;
5355 * ipr_reset_alert_part2 - Alert the adapter of a pending reset
5356 * @ipr_cmd: ipr command struct
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.
5363 * Return value:
5364 * IPR_RC_JOB_RETURN
5366 static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
5368 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5369 u16 cmd_reg;
5370 int rc;
5372 ENTER;
5373 rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
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;
5383 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
5384 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5386 LEAVE;
5387 return IPR_RC_JOB_RETURN;
5391 * ipr_reset_ucode_download_done - Microcode download completion
5392 * @ipr_cmd: ipr command struct
5394 * Description: This function unmaps the microcode download buffer.
5396 * Return value:
5397 * IPR_RC_JOB_CONTINUE
5399 static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
5401 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5402 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5404 pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
5405 sglist->num_sg, DMA_TO_DEVICE);
5407 ipr_cmd->job_step = ipr_reset_alert;
5408 return IPR_RC_JOB_CONTINUE;
5412 * ipr_reset_ucode_download - Download microcode to the adapter
5413 * @ipr_cmd: ipr command struct
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.
5418 * Return value:
5419 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5421 static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
5423 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5424 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5426 ENTER;
5427 ipr_cmd->job_step = ipr_reset_alert;
5429 if (!sglist)
5430 return IPR_RC_JOB_CONTINUE;
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;
5440 ipr_build_ucode_ioadl(ipr_cmd, sglist);
5441 ipr_cmd->job_step = ipr_reset_ucode_download_done;
5443 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
5444 IPR_WRITE_BUFFER_TIMEOUT);
5446 LEAVE;
5447 return IPR_RC_JOB_RETURN;
5451 * ipr_reset_shutdown_ioa - Shutdown the adapter
5452 * @ipr_cmd: ipr command struct
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.
5458 * Return value:
5459 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5461 static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
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;
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;
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;
5482 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
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;
5489 LEAVE;
5490 return rc;
5494 * ipr_reset_ioa_job - Adapter reset job
5495 * @ipr_cmd: ipr command struct
5497 * Description: This function is the job router for the adapter reset job.
5499 * Return value:
5500 * none
5502 static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
5504 u32 rc, ioasc;
5505 unsigned long scratch = ipr_cmd->u.scratch;
5506 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5508 do {
5509 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5511 if (ioa_cfg->reset_cmd != ipr_cmd) {
5513 * We are doing nested adapter resets and this is
5514 * not the current reset job.
5516 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5517 return;
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);
5525 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5526 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5527 return;
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);
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
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.
5547 * Return value:
5548 * none
5550 static 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)
5554 struct ipr_cmnd *ipr_cmd;
5556 ioa_cfg->in_reset_reload = 1;
5557 ioa_cfg->allow_cmds = 0;
5558 scsi_block_requests(ioa_cfg->host);
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;
5565 ipr_reset_ioa_job(ipr_cmd);
5569 * ipr_initiate_ioa_reset - Initiate an adapter reset
5570 * @ioa_cfg: ioa config struct
5571 * @shutdown_type: shutdown type
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.
5577 * Return value:
5578 * none
5580 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5581 enum ipr_shutdown_type shutdown_type)
5583 if (ioa_cfg->ioa_is_dead)
5584 return;
5586 if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
5587 ioa_cfg->sdt_state = ABORT_DUMP;
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");
5593 ioa_cfg->reset_retries = 0;
5594 ioa_cfg->ioa_is_dead = 1;
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);
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;
5612 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
5613 shutdown_type);
5617 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
5618 * @ioa_cfg: ioa cfg struct
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.
5624 * Return value:
5625 * 0 on sucess / -EIO on failure
5627 static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
5629 int rc = 0;
5630 unsigned long host_lock_flags = 0;
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);
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);
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;
5647 dev_err(&ioa_cfg->pdev->dev,
5648 "Adapter not supported in this hardware configuration.\n");
5651 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5653 LEAVE;
5654 return rc;
5658 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
5659 * @ioa_cfg: ioa config struct
5661 * Return value:
5662 * none
5664 static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5666 int i;
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]);
5674 ioa_cfg->ipr_cmnd_list[i] = NULL;
5677 if (ioa_cfg->ipr_cmd_pool)
5678 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
5680 ioa_cfg->ipr_cmd_pool = NULL;
5684 * ipr_free_mem - Frees memory allocated for an adapter
5685 * @ioa_cfg: ioa cfg struct
5687 * Return value:
5688 * nothing
5690 static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
5692 int i;
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);
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]);
5711 ipr_free_dump(ioa_cfg);
5712 kfree(ioa_cfg->saved_mode_pages);
5713 kfree(ioa_cfg->trace);
5717 * ipr_free_all_resources - Free all allocated resources for an adapter.
5718 * @ipr_cmd: ipr command struct
5720 * This function frees all allocated resources for the
5721 * specified adapter.
5723 * Return value:
5724 * none
5726 static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
5728 struct pci_dev *pdev = ioa_cfg->pdev;
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;
5741 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
5742 * @ioa_cfg: ioa config struct
5744 * Return value:
5745 * 0 on success / -ENOMEM on allocation failure
5747 static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5749 struct ipr_cmnd *ipr_cmd;
5750 struct ipr_ioarcb *ioarcb;
5751 dma_addr_t dma_addr;
5752 int i;
5754 ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
5755 sizeof(struct ipr_cmnd), 8, 0);
5757 if (!ioa_cfg->ipr_cmd_pool)
5758 return -ENOMEM;
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);
5763 if (!ipr_cmd) {
5764 ipr_free_cmd_blks(ioa_cfg);
5765 return -ENOMEM;
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;
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);
5786 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5789 return 0;
5793 * ipr_alloc_mem - Allocate memory for an adapter
5794 * @ioa_cfg: ioa config struct
5796 * Return value:
5797 * 0 on success / non-zero for error
5799 static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
5801 struct pci_dev *pdev = ioa_cfg->pdev;
5802 int i, rc = -ENOMEM;
5804 ENTER;
5805 ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
5806 IPR_MAX_PHYSICAL_DEVS, GFP_KERNEL);
5808 if (!ioa_cfg->res_entries)
5809 goto out;
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);
5814 ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
5815 sizeof(struct ipr_misc_cbs),
5816 &ioa_cfg->vpd_cbs_dma);
5818 if (!ioa_cfg->vpd_cbs)
5819 goto out_free_res_entries;
5821 if (ipr_alloc_cmd_blks(ioa_cfg))
5822 goto out_free_vpd_cbs;
5824 ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
5825 sizeof(u32) * IPR_NUM_CMD_BLKS,
5826 &ioa_cfg->host_rrq_dma);
5828 if (!ioa_cfg->host_rrq)
5829 goto out_ipr_free_cmd_blocks;
5831 ioa_cfg->cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
5832 sizeof(struct ipr_config_table),
5833 &ioa_cfg->cfg_table_dma);
5835 if (!ioa_cfg->cfg_table)
5836 goto out_free_host_rrq;
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]);
5843 if (!ioa_cfg->hostrcb[i])
5844 goto out_free_hostrcb_dma;
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);
5851 ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
5852 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
5854 if (!ioa_cfg->trace)
5855 goto out_free_hostrcb_dma;
5857 rc = 0;
5858 out:
5859 LEAVE;
5860 return rc;
5862 out_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]);
5868 pci_free_consistent(pdev, sizeof(struct ipr_config_table),
5869 ioa_cfg->cfg_table, ioa_cfg->cfg_table_dma);
5870 out_free_host_rrq:
5871 pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5872 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5873 out_ipr_free_cmd_blocks:
5874 ipr_free_cmd_blks(ioa_cfg);
5875 out_free_vpd_cbs:
5876 pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
5877 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5878 out_free_res_entries:
5879 kfree(ioa_cfg->res_entries);
5880 goto out;
5884 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
5885 * @ioa_cfg: ioa config struct
5887 * Return value:
5888 * none
5890 static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
5892 int i;
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;
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
5911 * Return value:
5912 * none
5914 static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
5915 struct Scsi_Host *host, struct pci_dev *pdev)
5917 const struct ipr_interrupt_offsets *p;
5918 struct ipr_interrupts *t;
5919 void __iomem *base;
5921 ioa_cfg->host = host;
5922 ioa_cfg->pdev = pdev;
5923 ioa_cfg->log_level = ipr_log_level;
5924 ioa_cfg->doorbell = IPR_DOORBELL;
5925 if (!ipr_auto_create)
5926 ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
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);
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;
5945 if (ipr_enable_cache)
5946 ioa_cfg->cache_state = CACHE_ENABLED;
5947 else
5948 ioa_cfg->cache_state = CACHE_DISABLED;
5950 ipr_initialize_bus_attr(ioa_cfg);
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);
5959 p = &ioa_cfg->chip_cfg->regs;
5960 t = &ioa_cfg->regs;
5961 base = ioa_cfg->hdw_dma_regs;
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;
5975 * ipr_get_chip_cfg - Find adapter chip configuration
5976 * @dev_id: PCI device id struct
5978 * Return value:
5979 * ptr to chip config on success / NULL on failure
5981 static const struct ipr_chip_cfg_t * __devinit
5982 ipr_get_chip_cfg(const struct pci_device_id *dev_id)
5984 int i;
5986 if (dev_id->driver_data)
5987 return (const struct ipr_chip_cfg_t *)dev_id->driver_data;
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;
5997 * ipr_probe_ioa - Allocates memory and does first stage of initialization
5998 * @pdev: PCI device struct
5999 * @dev_id: PCI device id struct
6001 * Return value:
6002 * 0 on success / non-zero on failure
6004 static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
6005 const struct pci_device_id *dev_id)
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;
6013 ENTER;
6015 if ((rc = pci_enable_device(pdev))) {
6016 dev_err(&pdev->dev, "Cannot enable adapter\n");
6017 goto out;
6020 dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
6022 host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
6024 if (!host) {
6025 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
6026 rc = -ENOMEM;
6027 goto out_disable;
6030 ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
6031 memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
6033 ioa_cfg->chip_cfg = ipr_get_chip_cfg(dev_id);
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;
6041 ipr_regs_pci = pci_resource_start(pdev, 0);
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;
6050 ipr_regs = ioremap(ipr_regs_pci, pci_resource_len(pdev, 0));
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;
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;
6063 ipr_init_ioa_cfg(ioa_cfg, host, pdev);
6065 pci_set_master(pdev);
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;
6073 rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
6074 ioa_cfg->chip_cfg->cache_line_size);
6076 if (rc != PCIBIOS_SUCCESSFUL) {
6077 dev_err(&pdev->dev, "Write of cache line size failed\n");
6078 rc = -EIO;
6079 goto cleanup_nomem;
6082 /* Save away PCI config space for use following IOA reset */
6083 rc = pci_save_state(pdev);
6085 if (rc != PCIBIOS_SUCCESSFUL) {
6086 dev_err(&pdev->dev, "Failed to save PCI config space\n");
6087 rc = -EIO;
6088 goto cleanup_nomem;
6091 if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
6092 goto cleanup_nomem;
6094 if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
6095 goto cleanup_nomem;
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;
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);
6107 if (rc) {
6108 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
6109 pdev->irq, rc);
6110 goto cleanup_nolog;
6113 spin_lock(&ipr_driver_lock);
6114 list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
6115 spin_unlock(&ipr_driver_lock);
6117 LEAVE;
6118 out:
6119 return rc;
6121 cleanup_nolog:
6122 ipr_free_mem(ioa_cfg);
6123 cleanup_nomem:
6124 iounmap(ipr_regs);
6125 out_release_regions:
6126 pci_release_regions(pdev);
6127 out_scsi_host_put:
6128 scsi_host_put(host);
6129 out_disable:
6130 pci_disable_device(pdev);
6131 goto out;
6135 * ipr_scan_vsets - Scans for VSET devices
6136 * @ioa_cfg: ioa config struct
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.
6141 * Return value:
6142 * none
6144 static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
6146 int target, lun;
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);
6154 * ipr_initiate_ioa_bringdown - Bring down an adapter
6155 * @ioa_cfg: ioa config struct
6156 * @shutdown_type: shutdown type
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.
6164 * Return value:
6165 * none
6167 static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
6168 enum ipr_shutdown_type shutdown_type)
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;
6180 * __ipr_remove - Remove a single adapter
6181 * @pdev: pci device struct
6183 * Adapter hot plug remove entry point.
6185 * Return value:
6186 * none
6188 static void __ipr_remove(struct pci_dev *pdev)
6190 unsigned long host_lock_flags = 0;
6191 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6192 ENTER;
6194 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
6195 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
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);
6199 flush_scheduled_work();
6200 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
6202 spin_lock(&ipr_driver_lock);
6203 list_del(&ioa_cfg->queue);
6204 spin_unlock(&ipr_driver_lock);
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);
6210 ipr_free_all_resources(ioa_cfg);
6212 LEAVE;
6216 * ipr_remove - IOA hot plug remove entry point
6217 * @pdev: pci device struct
6219 * Adapter hot plug remove entry point.
6221 * Return value:
6222 * none
6224 static void ipr_remove(struct pci_dev *pdev)
6226 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6228 ENTER;
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);
6236 __ipr_remove(pdev);
6238 LEAVE;
6242 * ipr_probe - Adapter hot plug add entry point
6244 * Return value:
6245 * 0 on success / non-zero on failure
6247 static int __devinit ipr_probe(struct pci_dev *pdev,
6248 const struct pci_device_id *dev_id)
6250 struct ipr_ioa_cfg *ioa_cfg;
6251 int rc;
6253 rc = ipr_probe_ioa(pdev, dev_id);
6255 if (rc)
6256 return rc;
6258 ioa_cfg = pci_get_drvdata(pdev);
6259 rc = ipr_probe_ioa_part2(ioa_cfg);
6261 if (rc) {
6262 __ipr_remove(pdev);
6263 return rc;
6266 rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
6268 if (rc) {
6269 __ipr_remove(pdev);
6270 return rc;
6273 rc = ipr_create_trace_file(&ioa_cfg->host->shost_classdev.kobj,
6274 &ipr_trace_attr);
6276 if (rc) {
6277 scsi_remove_host(ioa_cfg->host);
6278 __ipr_remove(pdev);
6279 return rc;
6282 rc = ipr_create_dump_file(&ioa_cfg->host->shost_classdev.kobj,
6283 &ipr_dump_attr);
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;
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;
6297 ioa_cfg->host->max_channel = IPR_VSET_BUS;
6298 schedule_work(&ioa_cfg->work_q);
6299 return 0;
6303 * ipr_shutdown - Shutdown handler.
6304 * @pdev: pci device struct
6306 * This function is invoked upon system shutdown/reboot. It will issue
6307 * an adapter shutdown to the adapter to flush the write cache.
6309 * Return value:
6310 * none
6312 static void ipr_shutdown(struct pci_dev *pdev)
6314 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
6315 unsigned long lock_flags = 0;
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);
6323 static 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] },
6353 MODULE_DEVICE_TABLE(pci, ipr_pci_table);
6355 static struct pci_driver ipr_driver = {
6356 .name = IPR_NAME,
6357 .id_table = ipr_pci_table,
6358 .probe = ipr_probe,
6359 .remove = ipr_remove,
6360 .shutdown = ipr_shutdown,
6364 * ipr_init - Module entry point
6366 * Return value:
6367 * 0 on success / negative value on failure
6369 static int __init ipr_init(void)
6371 ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
6372 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
6374 return pci_module_init(&ipr_driver);
6378 * ipr_exit - Module unload
6380 * Module unload entry point.
6382 * Return value:
6383 * none
6385 static void __exit ipr_exit(void)
6387 pci_unregister_driver(&ipr_driver);
6390 module_init(ipr_init);
6391 module_exit(ipr_exit);