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
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
44 * - Background Parity Checking
45 * - Background Data Scrubbing
46 * - Ability to increase the capacity of an existing RAID 5 disk array
50 * - Tagged command queuing
51 * - Adapter microcode download
53 * - SCSI device hot plug
57 #include <linux/config.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>
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>
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 DEFINE_SPINLOCK(ipr_driver_lock);
96 /* This table describes the differences between DMA controller chips */
97 static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
98 { /* Gemstone and Citrine */
100 .cache_line_size = 0x20,
102 .set_interrupt_mask_reg = 0x0022C,
103 .clr_interrupt_mask_reg = 0x00230,
104 .sense_interrupt_mask_reg = 0x0022C,
105 .clr_interrupt_reg = 0x00228,
106 .sense_interrupt_reg = 0x00224,
107 .ioarrin_reg = 0x00404,
108 .sense_uproc_interrupt_reg = 0x00214,
109 .set_uproc_interrupt_reg = 0x00214,
110 .clr_uproc_interrupt_reg = 0x00218
113 { /* Snipe and Scamp */
115 .cache_line_size = 0x20,
117 .set_interrupt_mask_reg = 0x00288,
118 .clr_interrupt_mask_reg = 0x0028C,
119 .sense_interrupt_mask_reg = 0x00288,
120 .clr_interrupt_reg = 0x00284,
121 .sense_interrupt_reg = 0x00280,
122 .ioarrin_reg = 0x00504,
123 .sense_uproc_interrupt_reg = 0x00290,
124 .set_uproc_interrupt_reg = 0x00290,
125 .clr_uproc_interrupt_reg = 0x00294
130 static const struct ipr_chip_t ipr_chip[] = {
131 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, &ipr_chip_cfg[0] },
132 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, &ipr_chip_cfg[0] },
133 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, &ipr_chip_cfg[1] },
134 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, &ipr_chip_cfg[1] }
137 static int ipr_max_bus_speeds [] = {
138 IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
141 MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
142 MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
143 module_param_named(max_speed, ipr_max_speed, uint, 0);
144 MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
145 module_param_named(log_level, ipr_log_level, uint, 0);
146 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
147 module_param_named(testmode, ipr_testmode, int, 0);
148 MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
149 module_param_named(fastfail, ipr_fastfail, int, 0);
150 MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
151 module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
152 MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
153 MODULE_LICENSE("GPL");
154 MODULE_VERSION(IPR_DRIVER_VERSION);
156 static const char *ipr_gpdd_dev_end_states[] = {
158 "Terminated by host",
159 "Terminated by device reset",
160 "Terminated by bus reset",
162 "Command not started"
165 static const char *ipr_gpdd_dev_bus_phases[] = {
179 /* A constant array of IOASCs/URCs/Error Messages */
181 struct ipr_error_table_t ipr_error_table[] = {
183 "8155: An unknown error was received"},
185 "Soft underlength error"},
187 "Command to be cancelled not found"},
189 "Qualified success"},
191 "FFFE: Soft device bus error recovered by the IOA"},
193 "FFF9: Device sector reassign successful"},
195 "FFF7: Media error recovered by device rewrite procedures"},
197 "7001: IOA sector reassignment successful"},
199 "FFF9: Soft media error. Sector reassignment recommended"},
201 "FFF7: Media error recovered by IOA rewrite procedures"},
203 "FF3D: Soft PCI bus error recovered by the IOA"},
205 "FFF6: Device hardware error recovered by the IOA"},
207 "FFF6: Device hardware error recovered by the device"},
209 "FF3D: Soft IOA error recovered by the IOA"},
211 "FFFA: Undefined device response recovered by the IOA"},
213 "FFF6: Device bus error, message or command phase"},
215 "FFF6: Failure prediction threshold exceeded"},
217 "8009: Impending cache battery pack failure"},
219 "34FF: Disk device format in progress"},
221 "Synchronization required"},
223 "No ready, IOA shutdown"},
225 "Not ready, IOA has been shutdown"},
227 "3020: Storage subsystem configuration error"},
229 "FFF5: Medium error, data unreadable, recommend reassign"},
231 "7000: Medium error, data unreadable, do not reassign"},
233 "FFF3: Disk media format bad"},
235 "3002: Addressed device failed to respond to selection"},
237 "3100: Device bus error"},
239 "3109: IOA timed out a device command"},
241 "3120: SCSI bus is not operational"},
243 "9000: IOA reserved area data check"},
245 "9001: IOA reserved area invalid data pattern"},
247 "9002: IOA reserved area LRC error"},
249 "102E: Out of alternate sectors for disk storage"},
251 "FFF4: Data transfer underlength error"},
253 "FFF4: Data transfer overlength error"},
255 "3400: Logical unit failure"},
257 "FFF4: Device microcode is corrupt"},
259 "8150: PCI bus error"},
261 "Unsupported device bus message received"},
263 "FFF4: Disk device problem"},
265 "8150: Permanent IOA failure"},
267 "3010: Disk device returned wrong response to IOA"},
269 "8151: IOA microcode error"},
271 "Device bus status error"},
273 "8157: IOA error requiring IOA reset to recover"},
275 "Message reject received from the device"},
277 "8008: A permanent cache battery pack failure occurred"},
279 "9090: Disk unit has been modified after the last known status"},
281 "9081: IOA detected device error"},
283 "9082: IOA detected device error"},
285 "3110: Device bus error, message or command phase"},
287 "9091: Incorrect hardware configuration change has been detected"},
289 "FFF4: Command to logical unit failed"},
291 "Illegal request, invalid request type or request packet"},
293 "Illegal request, invalid resource handle"},
295 "Illegal request, invalid field in parameter list"},
297 "Illegal request, parameter not supported"},
299 "Illegal request, parameter value invalid"},
301 "Illegal request, command sequence error"},
303 "9031: Array protection temporarily suspended, protection resuming"},
305 "9040: Array protection temporarily suspended, protection resuming"},
307 "FFFB: SCSI bus was reset"},
309 "FFFE: SCSI bus transition to single ended"},
311 "FFFE: SCSI bus transition to LVD"},
313 "FFFB: SCSI bus was reset by another initiator"},
315 "3029: A device replacement has occurred"},
317 "9051: IOA cache data exists for a missing or failed device"},
319 "9025: Disk unit is not supported at its physical location"},
321 "3020: IOA detected a SCSI bus configuration error"},
323 "3150: SCSI bus configuration error"},
325 "9041: Array protection temporarily suspended"},
327 "9042: Corrupt array parity detected on specified device"},
329 "9030: Array no longer protected due to missing or failed disk unit"},
331 "9032: Array exposed but still protected"},
333 "Failure due to other device"},
335 "9008: IOA does not support functions expected by devices"},
337 "9010: Cache data associated with attached devices cannot be found"},
339 "9011: Cache data belongs to devices other than those attached"},
341 "9020: Array missing 2 or more devices with only 1 device present"},
343 "9021: Array missing 2 or more devices with 2 or more devices present"},
345 "9022: Exposed array is missing a required device"},
347 "9023: Array member(s) not at required physical locations"},
349 "9024: Array not functional due to present hardware configuration"},
351 "9026: Array not functional due to present hardware configuration"},
353 "9027: Array is missing a device and parity is out of sync"},
355 "9028: Maximum number of arrays already exist"},
357 "9050: Required cache data cannot be located for a disk unit"},
359 "9052: Cache data exists for a device that has been modified"},
361 "9054: IOA resources not available due to previous problems"},
363 "9092: Disk unit requires initialization before use"},
365 "9029: Incorrect hardware configuration change has been detected"},
367 "9060: One or more disk pairs are missing from an array"},
369 "9061: One or more disks are missing from an array"},
371 "9062: One or more disks are missing from an array"},
373 "9063: Maximum number of functional arrays has been exceeded"},
375 "Aborted command, invalid descriptor"},
377 "Command terminated by host"}
380 static const struct ipr_ses_table_entry ipr_ses_table[] = {
381 { "2104-DL1 ", "XXXXXXXXXXXXXXXX", 80 },
382 { "2104-TL1 ", "XXXXXXXXXXXXXXXX", 80 },
383 { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
384 { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
385 { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
386 { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
387 { "2104-DU3 ", "XXXXXXXXXXXXXXXX", 160 },
388 { "2104-TU3 ", "XXXXXXXXXXXXXXXX", 160 },
389 { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
390 { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
391 { "St V1S2 ", "XXXXXXXXXXXXXXXX", 160 },
392 { "HSBPD4M PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
393 { "VSBPD1H U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
397 * Function Prototypes
399 static int ipr_reset_alert(struct ipr_cmnd *);
400 static void ipr_process_ccn(struct ipr_cmnd *);
401 static void ipr_process_error(struct ipr_cmnd *);
402 static void ipr_reset_ioa_job(struct ipr_cmnd *);
403 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
404 enum ipr_shutdown_type);
406 #ifdef CONFIG_SCSI_IPR_TRACE
408 * ipr_trc_hook - Add a trace entry to the driver trace
409 * @ipr_cmd: ipr command struct
411 * @add_data: additional data
416 static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
417 u8 type, u32 add_data)
419 struct ipr_trace_entry *trace_entry;
420 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
422 trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
423 trace_entry->time = jiffies;
424 trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
425 trace_entry->type = type;
426 trace_entry->cmd_index = ipr_cmd->cmd_index;
427 trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
428 trace_entry->u.add_data = add_data;
431 #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
435 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
436 * @ipr_cmd: ipr command struct
441 static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
443 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
444 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
446 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
447 ioarcb->write_data_transfer_length = 0;
448 ioarcb->read_data_transfer_length = 0;
449 ioarcb->write_ioadl_len = 0;
450 ioarcb->read_ioadl_len = 0;
452 ioasa->residual_data_len = 0;
454 ipr_cmd->scsi_cmd = NULL;
455 ipr_cmd->sense_buffer[0] = 0;
456 ipr_cmd->dma_use_sg = 0;
460 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
461 * @ipr_cmd: ipr command struct
466 static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
468 ipr_reinit_ipr_cmnd(ipr_cmd);
469 ipr_cmd->u.scratch = 0;
470 ipr_cmd->sibling = NULL;
471 init_timer(&ipr_cmd->timer);
475 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
476 * @ioa_cfg: ioa config struct
479 * pointer to ipr command struct
482 struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
484 struct ipr_cmnd *ipr_cmd;
486 ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
487 list_del(&ipr_cmd->queue);
488 ipr_init_ipr_cmnd(ipr_cmd);
494 * ipr_unmap_sglist - Unmap scatterlist if mapped
495 * @ioa_cfg: ioa config struct
496 * @ipr_cmd: ipr command struct
501 static void ipr_unmap_sglist(struct ipr_ioa_cfg *ioa_cfg,
502 struct ipr_cmnd *ipr_cmd)
504 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
506 if (ipr_cmd->dma_use_sg) {
507 if (scsi_cmd->use_sg > 0) {
508 pci_unmap_sg(ioa_cfg->pdev, scsi_cmd->request_buffer,
510 scsi_cmd->sc_data_direction);
512 pci_unmap_single(ioa_cfg->pdev, ipr_cmd->dma_handle,
513 scsi_cmd->request_bufflen,
514 scsi_cmd->sc_data_direction);
520 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
521 * @ioa_cfg: ioa config struct
522 * @clr_ints: interrupts to clear
524 * This function masks all interrupts on the adapter, then clears the
525 * interrupts specified in the mask
530 static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
533 volatile u32 int_reg;
535 /* Stop new interrupts */
536 ioa_cfg->allow_interrupts = 0;
538 /* Set interrupt mask to stop all new interrupts */
539 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
541 /* Clear any pending interrupts */
542 writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
543 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
547 * ipr_save_pcix_cmd_reg - Save PCI-X command register
548 * @ioa_cfg: ioa config struct
551 * 0 on success / -EIO on failure
553 static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
555 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
557 if (pcix_cmd_reg == 0) {
558 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
562 if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
563 &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
564 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
568 ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
573 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
574 * @ioa_cfg: ioa config struct
577 * 0 on success / -EIO on failure
579 static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
581 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
584 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
585 ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
586 dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
590 dev_err(&ioa_cfg->pdev->dev,
591 "Failed to setup PCI-X command register\n");
599 * ipr_scsi_eh_done - mid-layer done function for aborted ops
600 * @ipr_cmd: ipr command struct
602 * This function is invoked by the interrupt handler for
603 * ops generated by the SCSI mid-layer which are being aborted.
608 static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
610 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
611 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
613 scsi_cmd->result |= (DID_ERROR << 16);
615 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
616 scsi_cmd->scsi_done(scsi_cmd);
617 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
621 * ipr_fail_all_ops - Fails all outstanding ops.
622 * @ioa_cfg: ioa config struct
624 * This function fails all outstanding ops.
629 static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
631 struct ipr_cmnd *ipr_cmd, *temp;
634 list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
635 list_del(&ipr_cmd->queue);
637 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
638 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
640 if (ipr_cmd->scsi_cmd)
641 ipr_cmd->done = ipr_scsi_eh_done;
643 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
644 del_timer(&ipr_cmd->timer);
645 ipr_cmd->done(ipr_cmd);
652 * ipr_do_req - Send driver initiated requests.
653 * @ipr_cmd: ipr command struct
654 * @done: done function
655 * @timeout_func: timeout function
656 * @timeout: timeout value
658 * This function sends the specified command to the adapter with the
659 * timeout given. The done function is invoked on command completion.
664 static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
665 void (*done) (struct ipr_cmnd *),
666 void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
668 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
670 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
672 ipr_cmd->done = done;
674 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
675 ipr_cmd->timer.expires = jiffies + timeout;
676 ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
678 add_timer(&ipr_cmd->timer);
680 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
683 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
684 ioa_cfg->regs.ioarrin_reg);
688 * ipr_internal_cmd_done - Op done function for an internally generated op.
689 * @ipr_cmd: ipr command struct
691 * This function is the op done function for an internally generated,
692 * blocking op. It simply wakes the sleeping thread.
697 static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
699 if (ipr_cmd->sibling)
700 ipr_cmd->sibling = NULL;
702 complete(&ipr_cmd->completion);
706 * ipr_send_blocking_cmd - Send command and sleep on its completion.
707 * @ipr_cmd: ipr command struct
708 * @timeout_func: function to invoke if command times out
714 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
715 void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
718 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
720 init_completion(&ipr_cmd->completion);
721 ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
723 spin_unlock_irq(ioa_cfg->host->host_lock);
724 wait_for_completion(&ipr_cmd->completion);
725 spin_lock_irq(ioa_cfg->host->host_lock);
729 * ipr_send_hcam - Send an HCAM to the adapter.
730 * @ioa_cfg: ioa config struct
732 * @hostrcb: hostrcb struct
734 * This function will send a Host Controlled Async command to the adapter.
735 * If HCAMs are currently not allowed to be issued to the adapter, it will
736 * place the hostrcb on the free queue.
741 static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
742 struct ipr_hostrcb *hostrcb)
744 struct ipr_cmnd *ipr_cmd;
745 struct ipr_ioarcb *ioarcb;
747 if (ioa_cfg->allow_cmds) {
748 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
749 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
750 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
752 ipr_cmd->u.hostrcb = hostrcb;
753 ioarcb = &ipr_cmd->ioarcb;
755 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
756 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
757 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
758 ioarcb->cmd_pkt.cdb[1] = type;
759 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
760 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
762 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
763 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
764 ipr_cmd->ioadl[0].flags_and_data_len =
765 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
766 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
768 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
769 ipr_cmd->done = ipr_process_ccn;
771 ipr_cmd->done = ipr_process_error;
773 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
776 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
777 ioa_cfg->regs.ioarrin_reg);
779 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
784 * ipr_init_res_entry - Initialize a resource entry struct.
785 * @res: resource entry struct
790 static void ipr_init_res_entry(struct ipr_resource_entry *res)
792 res->needs_sync_complete = 1;
795 res->del_from_ml = 0;
796 res->resetting_device = 0;
801 * ipr_handle_config_change - Handle a config change from the adapter
802 * @ioa_cfg: ioa config struct
808 static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
809 struct ipr_hostrcb *hostrcb)
811 struct ipr_resource_entry *res = NULL;
812 struct ipr_config_table_entry *cfgte;
815 cfgte = &hostrcb->hcam.u.ccn.cfgte;
817 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
818 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
819 sizeof(cfgte->res_addr))) {
826 if (list_empty(&ioa_cfg->free_res_q)) {
827 ipr_send_hcam(ioa_cfg,
828 IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
833 res = list_entry(ioa_cfg->free_res_q.next,
834 struct ipr_resource_entry, queue);
836 list_del(&res->queue);
837 ipr_init_res_entry(res);
838 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
841 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
843 if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
845 res->sdev->hostdata = NULL;
846 res->del_from_ml = 1;
847 if (ioa_cfg->allow_ml_add_del)
848 schedule_work(&ioa_cfg->work_q);
850 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
851 } else if (!res->sdev) {
853 if (ioa_cfg->allow_ml_add_del)
854 schedule_work(&ioa_cfg->work_q);
857 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
861 * ipr_process_ccn - Op done function for a CCN.
862 * @ipr_cmd: ipr command struct
864 * This function is the op done function for a configuration
865 * change notification host controlled async from the adapter.
870 static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
872 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
873 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
874 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
876 list_del(&hostrcb->queue);
877 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
880 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
881 dev_err(&ioa_cfg->pdev->dev,
882 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
884 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
886 ipr_handle_config_change(ioa_cfg, hostrcb);
891 * ipr_log_vpd - Log the passed VPD to the error log.
892 * @vpids: vendor/product id struct
893 * @serial_num: serial number string
898 static void ipr_log_vpd(struct ipr_std_inq_vpids *vpids, u8 *serial_num)
900 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
901 + IPR_SERIAL_NUM_LEN];
903 memcpy(buffer, vpids->vendor_id, IPR_VENDOR_ID_LEN);
904 memcpy(buffer + IPR_VENDOR_ID_LEN, vpids->product_id,
906 buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
907 ipr_err("Vendor/Product ID: %s\n", buffer);
909 memcpy(buffer, serial_num, IPR_SERIAL_NUM_LEN);
910 buffer[IPR_SERIAL_NUM_LEN] = '\0';
911 ipr_err(" Serial Number: %s\n", buffer);
915 * ipr_log_cache_error - Log a cache error.
916 * @ioa_cfg: ioa config struct
917 * @hostrcb: hostrcb struct
922 static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
923 struct ipr_hostrcb *hostrcb)
925 struct ipr_hostrcb_type_02_error *error =
926 &hostrcb->hcam.u.error.u.type_02_error;
928 ipr_err("-----Current Configuration-----\n");
929 ipr_err("Cache Directory Card Information:\n");
930 ipr_log_vpd(&error->ioa_vpids, error->ioa_sn);
931 ipr_err("Adapter Card Information:\n");
932 ipr_log_vpd(&error->cfc_vpids, error->cfc_sn);
934 ipr_err("-----Expected Configuration-----\n");
935 ipr_err("Cache Directory Card Information:\n");
936 ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpids,
937 error->ioa_last_attached_to_cfc_sn);
938 ipr_err("Adapter Card Information:\n");
939 ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpids,
940 error->cfc_last_attached_to_ioa_sn);
942 ipr_err("Additional IOA Data: %08X %08X %08X\n",
943 be32_to_cpu(error->ioa_data[0]),
944 be32_to_cpu(error->ioa_data[1]),
945 be32_to_cpu(error->ioa_data[2]));
949 * ipr_log_config_error - Log a configuration error.
950 * @ioa_cfg: ioa config struct
951 * @hostrcb: hostrcb struct
956 static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
957 struct ipr_hostrcb *hostrcb)
959 int errors_logged, i;
960 struct ipr_hostrcb_device_data_entry *dev_entry;
961 struct ipr_hostrcb_type_03_error *error;
963 error = &hostrcb->hcam.u.error.u.type_03_error;
964 errors_logged = be32_to_cpu(error->errors_logged);
966 ipr_err("Device Errors Detected/Logged: %d/%d\n",
967 be32_to_cpu(error->errors_detected), errors_logged);
969 dev_entry = error->dev_entry;
971 for (i = 0; i < errors_logged; i++, dev_entry++) {
974 if (dev_entry->dev_res_addr.bus >= IPR_MAX_NUM_BUSES) {
975 ipr_err("Device %d: missing\n", i + 1);
977 ipr_err("Device %d: %d:%d:%d:%d\n", i + 1,
978 ioa_cfg->host->host_no, dev_entry->dev_res_addr.bus,
979 dev_entry->dev_res_addr.target, dev_entry->dev_res_addr.lun);
981 ipr_log_vpd(&dev_entry->dev_vpids, dev_entry->dev_sn);
983 ipr_err("-----New Device Information-----\n");
984 ipr_log_vpd(&dev_entry->new_dev_vpids, dev_entry->new_dev_sn);
986 ipr_err("Cache Directory Card Information:\n");
987 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpids,
988 dev_entry->ioa_last_with_dev_sn);
990 ipr_err("Adapter Card Information:\n");
991 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpids,
992 dev_entry->cfc_last_with_dev_sn);
994 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
995 be32_to_cpu(dev_entry->ioa_data[0]),
996 be32_to_cpu(dev_entry->ioa_data[1]),
997 be32_to_cpu(dev_entry->ioa_data[2]),
998 be32_to_cpu(dev_entry->ioa_data[3]),
999 be32_to_cpu(dev_entry->ioa_data[4]));
1004 * ipr_log_array_error - Log an array configuration error.
1005 * @ioa_cfg: ioa config struct
1006 * @hostrcb: hostrcb struct
1011 static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1012 struct ipr_hostrcb *hostrcb)
1015 struct ipr_hostrcb_type_04_error *error;
1016 struct ipr_hostrcb_array_data_entry *array_entry;
1017 const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1019 error = &hostrcb->hcam.u.error.u.type_04_error;
1023 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1024 error->protection_level,
1025 ioa_cfg->host->host_no,
1026 error->last_func_vset_res_addr.bus,
1027 error->last_func_vset_res_addr.target,
1028 error->last_func_vset_res_addr.lun);
1032 array_entry = error->array_member;
1034 for (i = 0; i < 18; i++) {
1035 if (!memcmp(array_entry->serial_num, zero_sn, IPR_SERIAL_NUM_LEN))
1038 if (be32_to_cpu(error->exposed_mode_adn) == i) {
1039 ipr_err("Exposed Array Member %d:\n", i);
1041 ipr_err("Array Member %d:\n", i);
1044 ipr_log_vpd(&array_entry->vpids, array_entry->serial_num);
1046 if (array_entry->dev_res_addr.bus >= IPR_MAX_NUM_BUSES) {
1047 ipr_err("Current Location: unknown\n");
1049 ipr_err("Current Location: %d:%d:%d:%d\n",
1050 ioa_cfg->host->host_no,
1051 array_entry->dev_res_addr.bus,
1052 array_entry->dev_res_addr.target,
1053 array_entry->dev_res_addr.lun);
1056 if (array_entry->dev_res_addr.bus >= IPR_MAX_NUM_BUSES) {
1057 ipr_err("Expected Location: unknown\n");
1059 ipr_err("Expected Location: %d:%d:%d:%d\n",
1060 ioa_cfg->host->host_no,
1061 array_entry->expected_dev_res_addr.bus,
1062 array_entry->expected_dev_res_addr.target,
1063 array_entry->expected_dev_res_addr.lun);
1069 array_entry = error->array_member2;
1076 * ipr_log_generic_error - Log an adapter error.
1077 * @ioa_cfg: ioa config struct
1078 * @hostrcb: hostrcb struct
1083 static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1084 struct ipr_hostrcb *hostrcb)
1087 int ioa_data_len = be32_to_cpu(hostrcb->hcam.length);
1089 if (ioa_data_len == 0)
1092 ipr_err("IOA Error Data:\n");
1093 ipr_err("Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
1095 for (i = 0; i < ioa_data_len / 4; i += 4) {
1096 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1097 be32_to_cpu(hostrcb->hcam.u.raw.data[i]),
1098 be32_to_cpu(hostrcb->hcam.u.raw.data[i+1]),
1099 be32_to_cpu(hostrcb->hcam.u.raw.data[i+2]),
1100 be32_to_cpu(hostrcb->hcam.u.raw.data[i+3]));
1105 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1108 * This function will return the index of into the ipr_error_table
1109 * for the specified IOASC. If the IOASC is not in the table,
1110 * 0 will be returned, which points to the entry used for unknown errors.
1113 * index into the ipr_error_table
1115 static u32 ipr_get_error(u32 ioasc)
1119 for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
1120 if (ipr_error_table[i].ioasc == ioasc)
1127 * ipr_handle_log_data - Log an adapter error.
1128 * @ioa_cfg: ioa config struct
1129 * @hostrcb: hostrcb struct
1131 * This function logs an adapter error to the system.
1136 static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1137 struct ipr_hostrcb *hostrcb)
1142 if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1145 if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1146 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1148 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1150 if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1151 ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1152 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1153 scsi_report_bus_reset(ioa_cfg->host,
1154 hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1157 error_index = ipr_get_error(ioasc);
1159 if (!ipr_error_table[error_index].log_hcam)
1162 if (ipr_is_device(&hostrcb->hcam.u.error.failing_dev_res_addr)) {
1163 ipr_res_err(ioa_cfg, hostrcb->hcam.u.error.failing_dev_res_addr,
1164 "%s\n", ipr_error_table[error_index].error);
1166 dev_err(&ioa_cfg->pdev->dev, "%s\n",
1167 ipr_error_table[error_index].error);
1170 /* Set indication we have logged an error */
1171 ioa_cfg->errors_logged++;
1173 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
1176 switch (hostrcb->hcam.overlay_id) {
1177 case IPR_HOST_RCB_OVERLAY_ID_1:
1178 ipr_log_generic_error(ioa_cfg, hostrcb);
1180 case IPR_HOST_RCB_OVERLAY_ID_2:
1181 ipr_log_cache_error(ioa_cfg, hostrcb);
1183 case IPR_HOST_RCB_OVERLAY_ID_3:
1184 ipr_log_config_error(ioa_cfg, hostrcb);
1186 case IPR_HOST_RCB_OVERLAY_ID_4:
1187 case IPR_HOST_RCB_OVERLAY_ID_6:
1188 ipr_log_array_error(ioa_cfg, hostrcb);
1190 case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1191 ipr_log_generic_error(ioa_cfg, hostrcb);
1194 dev_err(&ioa_cfg->pdev->dev,
1195 "Unknown error received. Overlay ID: %d\n",
1196 hostrcb->hcam.overlay_id);
1202 * ipr_process_error - Op done function for an adapter error log.
1203 * @ipr_cmd: ipr command struct
1205 * This function is the op done function for an error log host
1206 * controlled async from the adapter. It will log the error and
1207 * send the HCAM back to the adapter.
1212 static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1214 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1215 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1216 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
1218 list_del(&hostrcb->queue);
1219 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1222 ipr_handle_log_data(ioa_cfg, hostrcb);
1223 } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1224 dev_err(&ioa_cfg->pdev->dev,
1225 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1228 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1232 * ipr_timeout - An internally generated op has timed out.
1233 * @ipr_cmd: ipr command struct
1235 * This function blocks host requests and initiates an
1241 static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1243 unsigned long lock_flags = 0;
1244 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1247 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1249 ioa_cfg->errors_logged++;
1250 dev_err(&ioa_cfg->pdev->dev,
1251 "Adapter being reset due to command timeout.\n");
1253 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1254 ioa_cfg->sdt_state = GET_DUMP;
1256 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1257 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1259 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1264 * ipr_oper_timeout - Adapter timed out transitioning to operational
1265 * @ipr_cmd: ipr command struct
1267 * This function blocks host requests and initiates an
1273 static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
1275 unsigned long lock_flags = 0;
1276 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1279 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1281 ioa_cfg->errors_logged++;
1282 dev_err(&ioa_cfg->pdev->dev,
1283 "Adapter timed out transitioning to operational.\n");
1285 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1286 ioa_cfg->sdt_state = GET_DUMP;
1288 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
1290 ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
1291 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1294 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1299 * ipr_reset_reload - Reset/Reload the IOA
1300 * @ioa_cfg: ioa config struct
1301 * @shutdown_type: shutdown type
1303 * This function resets the adapter and re-initializes it.
1304 * This function assumes that all new host commands have been stopped.
1308 static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1309 enum ipr_shutdown_type shutdown_type)
1311 if (!ioa_cfg->in_reset_reload)
1312 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1314 spin_unlock_irq(ioa_cfg->host->host_lock);
1315 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1316 spin_lock_irq(ioa_cfg->host->host_lock);
1318 /* If we got hit with a host reset while we were already resetting
1319 the adapter for some reason, and the reset failed. */
1320 if (ioa_cfg->ioa_is_dead) {
1329 * ipr_find_ses_entry - Find matching SES in SES table
1330 * @res: resource entry struct of SES
1333 * pointer to SES table entry / NULL on failure
1335 static const struct ipr_ses_table_entry *
1336 ipr_find_ses_entry(struct ipr_resource_entry *res)
1339 const struct ipr_ses_table_entry *ste = ipr_ses_table;
1341 for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1342 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1343 if (ste->compare_product_id_byte[j] == 'X') {
1344 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1352 if (matches == IPR_PROD_ID_LEN)
1360 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1361 * @ioa_cfg: ioa config struct
1363 * @bus_width: bus width
1366 * SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1367 * For a 2-byte wide SCSI bus, the maximum transfer speed is
1368 * twice the maximum transfer rate (e.g. for a wide enabled bus,
1369 * max 160MHz = max 320MB/sec).
1371 static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1373 struct ipr_resource_entry *res;
1374 const struct ipr_ses_table_entry *ste;
1375 u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1377 /* Loop through each config table entry in the config table buffer */
1378 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1379 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1382 if (bus != res->cfgte.res_addr.bus)
1385 if (!(ste = ipr_find_ses_entry(res)))
1388 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1391 return max_xfer_rate;
1395 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1396 * @ioa_cfg: ioa config struct
1397 * @max_delay: max delay in micro-seconds to wait
1399 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1402 * 0 on success / other on failure
1404 static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1406 volatile u32 pcii_reg;
1409 /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1410 while (delay < max_delay) {
1411 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1413 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1416 /* udelay cannot be used if delay is more than a few milliseconds */
1417 if ((delay / 1000) > MAX_UDELAY_MS)
1418 mdelay(delay / 1000);
1428 * ipr_get_ldump_data_section - Dump IOA memory
1429 * @ioa_cfg: ioa config struct
1430 * @start_addr: adapter address to dump
1431 * @dest: destination kernel buffer
1432 * @length_in_words: length to dump in 4 byte words
1435 * 0 on success / -EIO on failure
1437 static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1439 __be32 *dest, u32 length_in_words)
1441 volatile u32 temp_pcii_reg;
1444 /* Write IOA interrupt reg starting LDUMP state */
1445 writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1446 ioa_cfg->regs.set_uproc_interrupt_reg);
1448 /* Wait for IO debug acknowledge */
1449 if (ipr_wait_iodbg_ack(ioa_cfg,
1450 IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1451 dev_err(&ioa_cfg->pdev->dev,
1452 "IOA dump long data transfer timeout\n");
1456 /* Signal LDUMP interlocked - clear IO debug ack */
1457 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1458 ioa_cfg->regs.clr_interrupt_reg);
1460 /* Write Mailbox with starting address */
1461 writel(start_addr, ioa_cfg->ioa_mailbox);
1463 /* Signal address valid - clear IOA Reset alert */
1464 writel(IPR_UPROCI_RESET_ALERT,
1465 ioa_cfg->regs.clr_uproc_interrupt_reg);
1467 for (i = 0; i < length_in_words; i++) {
1468 /* Wait for IO debug acknowledge */
1469 if (ipr_wait_iodbg_ack(ioa_cfg,
1470 IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1471 dev_err(&ioa_cfg->pdev->dev,
1472 "IOA dump short data transfer timeout\n");
1476 /* Read data from mailbox and increment destination pointer */
1477 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1480 /* For all but the last word of data, signal data received */
1481 if (i < (length_in_words - 1)) {
1482 /* Signal dump data received - Clear IO debug Ack */
1483 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1484 ioa_cfg->regs.clr_interrupt_reg);
1488 /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1489 writel(IPR_UPROCI_RESET_ALERT,
1490 ioa_cfg->regs.set_uproc_interrupt_reg);
1492 writel(IPR_UPROCI_IO_DEBUG_ALERT,
1493 ioa_cfg->regs.clr_uproc_interrupt_reg);
1495 /* Signal dump data received - Clear IO debug Ack */
1496 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1497 ioa_cfg->regs.clr_interrupt_reg);
1499 /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
1500 while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
1502 readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
1504 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
1514 #ifdef CONFIG_SCSI_IPR_DUMP
1516 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
1517 * @ioa_cfg: ioa config struct
1518 * @pci_address: adapter address
1519 * @length: length of data to copy
1521 * Copy data from PCI adapter to kernel buffer.
1522 * Note: length MUST be a 4 byte multiple
1524 * 0 on success / other on failure
1526 static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
1527 unsigned long pci_address, u32 length)
1529 int bytes_copied = 0;
1530 int cur_len, rc, rem_len, rem_page_len;
1532 unsigned long lock_flags = 0;
1533 struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
1535 while (bytes_copied < length &&
1536 (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
1537 if (ioa_dump->page_offset >= PAGE_SIZE ||
1538 ioa_dump->page_offset == 0) {
1539 page = (__be32 *)__get_free_page(GFP_ATOMIC);
1543 return bytes_copied;
1546 ioa_dump->page_offset = 0;
1547 ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
1548 ioa_dump->next_page_index++;
1550 page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
1552 rem_len = length - bytes_copied;
1553 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
1554 cur_len = min(rem_len, rem_page_len);
1556 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1557 if (ioa_cfg->sdt_state == ABORT_DUMP) {
1560 rc = ipr_get_ldump_data_section(ioa_cfg,
1561 pci_address + bytes_copied,
1562 &page[ioa_dump->page_offset / 4],
1563 (cur_len / sizeof(u32)));
1565 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1568 ioa_dump->page_offset += cur_len;
1569 bytes_copied += cur_len;
1577 return bytes_copied;
1581 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
1582 * @hdr: dump entry header struct
1587 static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
1589 hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
1591 hdr->offset = sizeof(*hdr);
1592 hdr->status = IPR_DUMP_STATUS_SUCCESS;
1596 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
1597 * @ioa_cfg: ioa config struct
1598 * @driver_dump: driver dump struct
1603 static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
1604 struct ipr_driver_dump *driver_dump)
1606 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
1608 ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
1609 driver_dump->ioa_type_entry.hdr.len =
1610 sizeof(struct ipr_dump_ioa_type_entry) -
1611 sizeof(struct ipr_dump_entry_header);
1612 driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1613 driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
1614 driver_dump->ioa_type_entry.type = ioa_cfg->type;
1615 driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
1616 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
1617 ucode_vpd->minor_release[1];
1618 driver_dump->hdr.num_entries++;
1622 * ipr_dump_version_data - Fill in the driver version in the dump.
1623 * @ioa_cfg: ioa config struct
1624 * @driver_dump: driver dump struct
1629 static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
1630 struct ipr_driver_dump *driver_dump)
1632 ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
1633 driver_dump->version_entry.hdr.len =
1634 sizeof(struct ipr_dump_version_entry) -
1635 sizeof(struct ipr_dump_entry_header);
1636 driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
1637 driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
1638 strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
1639 driver_dump->hdr.num_entries++;
1643 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
1644 * @ioa_cfg: ioa config struct
1645 * @driver_dump: driver dump struct
1650 static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
1651 struct ipr_driver_dump *driver_dump)
1653 ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
1654 driver_dump->trace_entry.hdr.len =
1655 sizeof(struct ipr_dump_trace_entry) -
1656 sizeof(struct ipr_dump_entry_header);
1657 driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1658 driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
1659 memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
1660 driver_dump->hdr.num_entries++;
1664 * ipr_dump_location_data - Fill in the IOA location in the dump.
1665 * @ioa_cfg: ioa config struct
1666 * @driver_dump: driver dump struct
1671 static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
1672 struct ipr_driver_dump *driver_dump)
1674 ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
1675 driver_dump->location_entry.hdr.len =
1676 sizeof(struct ipr_dump_location_entry) -
1677 sizeof(struct ipr_dump_entry_header);
1678 driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
1679 driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
1680 strcpy(driver_dump->location_entry.location, ioa_cfg->pdev->dev.bus_id);
1681 driver_dump->hdr.num_entries++;
1685 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
1686 * @ioa_cfg: ioa config struct
1687 * @dump: dump struct
1692 static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
1694 unsigned long start_addr, sdt_word;
1695 unsigned long lock_flags = 0;
1696 struct ipr_driver_dump *driver_dump = &dump->driver_dump;
1697 struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
1698 u32 num_entries, start_off, end_off;
1699 u32 bytes_to_copy, bytes_copied, rc;
1700 struct ipr_sdt *sdt;
1705 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1707 if (ioa_cfg->sdt_state != GET_DUMP) {
1708 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1712 start_addr = readl(ioa_cfg->ioa_mailbox);
1714 if (!ipr_sdt_is_fmt2(start_addr)) {
1715 dev_err(&ioa_cfg->pdev->dev,
1716 "Invalid dump table format: %lx\n", start_addr);
1717 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1721 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
1723 driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
1725 /* Initialize the overall dump header */
1726 driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
1727 driver_dump->hdr.num_entries = 1;
1728 driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
1729 driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
1730 driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
1731 driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
1733 ipr_dump_version_data(ioa_cfg, driver_dump);
1734 ipr_dump_location_data(ioa_cfg, driver_dump);
1735 ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
1736 ipr_dump_trace_data(ioa_cfg, driver_dump);
1738 /* Update dump_header */
1739 driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
1741 /* IOA Dump entry */
1742 ipr_init_dump_entry_hdr(&ioa_dump->hdr);
1743 ioa_dump->format = IPR_SDT_FMT2;
1744 ioa_dump->hdr.len = 0;
1745 ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1746 ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
1748 /* First entries in sdt are actually a list of dump addresses and
1749 lengths to gather the real dump data. sdt represents the pointer
1750 to the ioa generated dump table. Dump data will be extracted based
1751 on entries in this table */
1752 sdt = &ioa_dump->sdt;
1754 rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
1755 sizeof(struct ipr_sdt) / sizeof(__be32));
1757 /* Smart Dump table is ready to use and the first entry is valid */
1758 if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
1759 dev_err(&ioa_cfg->pdev->dev,
1760 "Dump of IOA failed. Dump table not valid: %d, %X.\n",
1761 rc, be32_to_cpu(sdt->hdr.state));
1762 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
1763 ioa_cfg->sdt_state = DUMP_OBTAINED;
1764 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1768 num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
1770 if (num_entries > IPR_NUM_SDT_ENTRIES)
1771 num_entries = IPR_NUM_SDT_ENTRIES;
1773 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1775 for (i = 0; i < num_entries; i++) {
1776 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
1777 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1781 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
1782 sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
1783 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
1784 end_off = be32_to_cpu(sdt->entry[i].end_offset);
1786 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
1787 bytes_to_copy = end_off - start_off;
1788 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
1789 sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
1793 /* Copy data from adapter to driver buffers */
1794 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
1797 ioa_dump->hdr.len += bytes_copied;
1799 if (bytes_copied != bytes_to_copy) {
1800 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1807 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
1809 /* Update dump_header */
1810 driver_dump->hdr.len += ioa_dump->hdr.len;
1812 ioa_cfg->sdt_state = DUMP_OBTAINED;
1817 #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
1821 * ipr_release_dump - Free adapter dump memory
1822 * @kref: kref struct
1827 static void ipr_release_dump(struct kref *kref)
1829 struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
1830 struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
1831 unsigned long lock_flags = 0;
1835 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1836 ioa_cfg->dump = NULL;
1837 ioa_cfg->sdt_state = INACTIVE;
1838 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1840 for (i = 0; i < dump->ioa_dump.next_page_index; i++)
1841 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
1848 * ipr_worker_thread - Worker thread
1849 * @data: ioa config struct
1851 * Called at task level from a work thread. This function takes care
1852 * of adding and removing device from the mid-layer as configuration
1853 * changes are detected by the adapter.
1858 static void ipr_worker_thread(void *data)
1860 unsigned long lock_flags;
1861 struct ipr_resource_entry *res;
1862 struct scsi_device *sdev;
1863 struct ipr_dump *dump;
1864 struct ipr_ioa_cfg *ioa_cfg = data;
1865 u8 bus, target, lun;
1869 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1871 if (ioa_cfg->sdt_state == GET_DUMP) {
1872 dump = ioa_cfg->dump;
1874 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1877 kref_get(&dump->kref);
1878 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1879 ipr_get_ioa_dump(ioa_cfg, dump);
1880 kref_put(&dump->kref, ipr_release_dump);
1882 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1883 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
1884 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1885 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1892 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
1893 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1897 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1898 if (res->del_from_ml && res->sdev) {
1901 if (!scsi_device_get(sdev)) {
1903 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
1904 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1905 scsi_remove_device(sdev);
1906 scsi_device_put(sdev);
1907 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1914 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1915 if (res->add_to_ml) {
1916 bus = res->cfgte.res_addr.bus;
1917 target = res->cfgte.res_addr.target;
1918 lun = res->cfgte.res_addr.lun;
1919 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1920 scsi_add_device(ioa_cfg->host, bus, target, lun);
1921 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1926 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1927 kobject_uevent(&ioa_cfg->host->shost_classdev.kobj, KOBJ_CHANGE, NULL);
1931 #ifdef CONFIG_SCSI_IPR_TRACE
1933 * ipr_read_trace - Dump the adapter trace
1934 * @kobj: kobject struct
1937 * @count: buffer size
1940 * number of bytes printed to buffer
1942 static ssize_t ipr_read_trace(struct kobject *kobj, char *buf,
1943 loff_t off, size_t count)
1945 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
1946 struct Scsi_Host *shost = class_to_shost(cdev);
1947 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1948 unsigned long lock_flags = 0;
1949 int size = IPR_TRACE_SIZE;
1950 char *src = (char *)ioa_cfg->trace;
1954 if (off + count > size) {
1959 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1960 memcpy(buf, &src[off], count);
1961 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1965 static struct bin_attribute ipr_trace_attr = {
1971 .read = ipr_read_trace,
1976 * ipr_show_fw_version - Show the firmware version
1977 * @class_dev: class device struct
1981 * number of bytes printed to buffer
1983 static ssize_t ipr_show_fw_version(struct class_device *class_dev, char *buf)
1985 struct Scsi_Host *shost = class_to_shost(class_dev);
1986 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1987 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
1988 unsigned long lock_flags = 0;
1991 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1992 len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
1993 ucode_vpd->major_release, ucode_vpd->card_type,
1994 ucode_vpd->minor_release[0],
1995 ucode_vpd->minor_release[1]);
1996 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2000 static struct class_device_attribute ipr_fw_version_attr = {
2002 .name = "fw_version",
2005 .show = ipr_show_fw_version,
2009 * ipr_show_log_level - Show the adapter's error logging level
2010 * @class_dev: class device struct
2014 * number of bytes printed to buffer
2016 static ssize_t ipr_show_log_level(struct class_device *class_dev, char *buf)
2018 struct Scsi_Host *shost = class_to_shost(class_dev);
2019 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2020 unsigned long lock_flags = 0;
2023 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2024 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
2025 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2030 * ipr_store_log_level - Change the adapter's error logging level
2031 * @class_dev: class device struct
2035 * number of bytes printed to buffer
2037 static ssize_t ipr_store_log_level(struct class_device *class_dev,
2038 const char *buf, size_t count)
2040 struct Scsi_Host *shost = class_to_shost(class_dev);
2041 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2042 unsigned long lock_flags = 0;
2044 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2045 ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
2046 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2050 static struct class_device_attribute ipr_log_level_attr = {
2052 .name = "log_level",
2053 .mode = S_IRUGO | S_IWUSR,
2055 .show = ipr_show_log_level,
2056 .store = ipr_store_log_level
2060 * ipr_store_diagnostics - IOA Diagnostics interface
2061 * @class_dev: class_device struct
2063 * @count: buffer size
2065 * This function will reset the adapter and wait a reasonable
2066 * amount of time for any errors that the adapter might log.
2069 * count on success / other on failure
2071 static ssize_t ipr_store_diagnostics(struct class_device *class_dev,
2072 const char *buf, size_t count)
2074 struct Scsi_Host *shost = class_to_shost(class_dev);
2075 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2076 unsigned long lock_flags = 0;
2079 if (!capable(CAP_SYS_ADMIN))
2082 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2083 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2084 ioa_cfg->errors_logged = 0;
2085 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2087 if (ioa_cfg->in_reset_reload) {
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 /* Wait for a second for any errors to be logged */
2094 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2098 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2099 if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2101 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2106 static struct class_device_attribute ipr_diagnostics_attr = {
2108 .name = "run_diagnostics",
2111 .store = ipr_store_diagnostics
2115 * ipr_store_reset_adapter - Reset the adapter
2116 * @class_dev: class_device struct
2118 * @count: buffer size
2120 * This function will reset the adapter.
2123 * count on success / other on failure
2125 static ssize_t ipr_store_reset_adapter(struct class_device *class_dev,
2126 const char *buf, size_t count)
2128 struct Scsi_Host *shost = class_to_shost(class_dev);
2129 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2130 unsigned long lock_flags;
2133 if (!capable(CAP_SYS_ADMIN))
2136 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2137 if (!ioa_cfg->in_reset_reload)
2138 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2139 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2140 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2145 static struct class_device_attribute ipr_ioa_reset_attr = {
2147 .name = "reset_host",
2150 .store = ipr_store_reset_adapter
2154 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2155 * @buf_len: buffer length
2157 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2158 * list to use for microcode download
2161 * pointer to sglist / NULL on failure
2163 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2165 int sg_size, order, bsize_elem, num_elem, i, j;
2166 struct ipr_sglist *sglist;
2167 struct scatterlist *scatterlist;
2170 /* Get the minimum size per scatter/gather element */
2171 sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2173 /* Get the actual size per element */
2174 order = get_order(sg_size);
2176 /* Determine the actual number of bytes per element */
2177 bsize_elem = PAGE_SIZE * (1 << order);
2179 /* Determine the actual number of sg entries needed */
2180 if (buf_len % bsize_elem)
2181 num_elem = (buf_len / bsize_elem) + 1;
2183 num_elem = buf_len / bsize_elem;
2185 /* Allocate a scatter/gather list for the DMA */
2186 sglist = kmalloc(sizeof(struct ipr_sglist) +
2187 (sizeof(struct scatterlist) * (num_elem - 1)),
2190 if (sglist == NULL) {
2195 memset(sglist, 0, sizeof(struct ipr_sglist) +
2196 (sizeof(struct scatterlist) * (num_elem - 1)));
2198 scatterlist = sglist->scatterlist;
2200 sglist->order = order;
2201 sglist->num_sg = num_elem;
2203 /* Allocate a bunch of sg elements */
2204 for (i = 0; i < num_elem; i++) {
2205 page = alloc_pages(GFP_KERNEL, order);
2209 /* Free up what we already allocated */
2210 for (j = i - 1; j >= 0; j--)
2211 __free_pages(scatterlist[j].page, order);
2216 scatterlist[i].page = page;
2223 * ipr_free_ucode_buffer - Frees a microcode download buffer
2224 * @p_dnld: scatter/gather list pointer
2226 * Free a DMA'able ucode download buffer previously allocated with
2227 * ipr_alloc_ucode_buffer
2232 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2236 for (i = 0; i < sglist->num_sg; i++)
2237 __free_pages(sglist->scatterlist[i].page, sglist->order);
2243 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2244 * @sglist: scatter/gather list pointer
2245 * @buffer: buffer pointer
2246 * @len: buffer length
2248 * Copy a microcode image from a user buffer into a buffer allocated by
2249 * ipr_alloc_ucode_buffer
2252 * 0 on success / other on failure
2254 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2255 u8 *buffer, u32 len)
2257 int bsize_elem, i, result = 0;
2258 struct scatterlist *scatterlist;
2261 /* Determine the actual number of bytes per element */
2262 bsize_elem = PAGE_SIZE * (1 << sglist->order);
2264 scatterlist = sglist->scatterlist;
2266 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
2267 kaddr = kmap(scatterlist[i].page);
2268 memcpy(kaddr, buffer, bsize_elem);
2269 kunmap(scatterlist[i].page);
2271 scatterlist[i].length = bsize_elem;
2279 if (len % bsize_elem) {
2280 kaddr = kmap(scatterlist[i].page);
2281 memcpy(kaddr, buffer, len % bsize_elem);
2282 kunmap(scatterlist[i].page);
2284 scatterlist[i].length = len % bsize_elem;
2287 sglist->buffer_len = len;
2292 * ipr_map_ucode_buffer - Map a microcode download buffer
2293 * @ipr_cmd: ipr command struct
2294 * @sglist: scatter/gather list
2295 * @len: total length of download buffer
2297 * Maps a microcode download scatter/gather list for DMA and
2301 * 0 on success / -EIO on failure
2303 static int ipr_map_ucode_buffer(struct ipr_cmnd *ipr_cmd,
2304 struct ipr_sglist *sglist, int len)
2306 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2307 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2308 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2309 struct scatterlist *scatterlist = sglist->scatterlist;
2312 ipr_cmd->dma_use_sg = pci_map_sg(ioa_cfg->pdev, scatterlist,
2313 sglist->num_sg, DMA_TO_DEVICE);
2315 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
2316 ioarcb->write_data_transfer_length = cpu_to_be32(len);
2317 ioarcb->write_ioadl_len =
2318 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
2320 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
2321 ioadl[i].flags_and_data_len =
2322 cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
2324 cpu_to_be32(sg_dma_address(&scatterlist[i]));
2327 if (likely(ipr_cmd->dma_use_sg)) {
2328 ioadl[i-1].flags_and_data_len |=
2329 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
2332 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
2340 * ipr_store_update_fw - Update the firmware on the adapter
2341 * @class_dev: class_device struct
2343 * @count: buffer size
2345 * This function will update the firmware on the adapter.
2348 * count on success / other on failure
2350 static ssize_t ipr_store_update_fw(struct class_device *class_dev,
2351 const char *buf, size_t count)
2353 struct Scsi_Host *shost = class_to_shost(class_dev);
2354 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2355 struct ipr_ucode_image_header *image_hdr;
2356 const struct firmware *fw_entry;
2357 struct ipr_sglist *sglist;
2358 unsigned long lock_flags;
2361 int len, result, dnld_size;
2363 if (!capable(CAP_SYS_ADMIN))
2366 len = snprintf(fname, 99, "%s", buf);
2367 fname[len-1] = '\0';
2369 if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
2370 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
2374 image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
2376 if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
2377 (ioa_cfg->vpd_cbs->page3_data.card_type &&
2378 ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
2379 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
2380 release_firmware(fw_entry);
2384 src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
2385 dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
2386 sglist = ipr_alloc_ucode_buffer(dnld_size);
2389 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
2390 release_firmware(fw_entry);
2394 result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
2397 dev_err(&ioa_cfg->pdev->dev,
2398 "Microcode buffer copy to DMA buffer failed\n");
2399 ipr_free_ucode_buffer(sglist);
2400 release_firmware(fw_entry);
2404 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2406 if (ioa_cfg->ucode_sglist) {
2407 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2408 dev_err(&ioa_cfg->pdev->dev,
2409 "Microcode download already in progress\n");
2410 ipr_free_ucode_buffer(sglist);
2411 release_firmware(fw_entry);
2415 ioa_cfg->ucode_sglist = sglist;
2416 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2417 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2418 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2420 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2421 ioa_cfg->ucode_sglist = NULL;
2422 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2424 ipr_free_ucode_buffer(sglist);
2425 release_firmware(fw_entry);
2430 static struct class_device_attribute ipr_update_fw_attr = {
2432 .name = "update_fw",
2435 .store = ipr_store_update_fw
2438 static struct class_device_attribute *ipr_ioa_attrs[] = {
2439 &ipr_fw_version_attr,
2440 &ipr_log_level_attr,
2441 &ipr_diagnostics_attr,
2442 &ipr_ioa_reset_attr,
2443 &ipr_update_fw_attr,
2447 #ifdef CONFIG_SCSI_IPR_DUMP
2449 * ipr_read_dump - Dump the adapter
2450 * @kobj: kobject struct
2453 * @count: buffer size
2456 * number of bytes printed to buffer
2458 static ssize_t ipr_read_dump(struct kobject *kobj, char *buf,
2459 loff_t off, size_t count)
2461 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2462 struct Scsi_Host *shost = class_to_shost(cdev);
2463 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2464 struct ipr_dump *dump;
2465 unsigned long lock_flags = 0;
2470 if (!capable(CAP_SYS_ADMIN))
2473 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2474 dump = ioa_cfg->dump;
2476 if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
2477 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2480 kref_get(&dump->kref);
2481 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2483 if (off > dump->driver_dump.hdr.len) {
2484 kref_put(&dump->kref, ipr_release_dump);
2488 if (off + count > dump->driver_dump.hdr.len) {
2489 count = dump->driver_dump.hdr.len - off;
2493 if (count && off < sizeof(dump->driver_dump)) {
2494 if (off + count > sizeof(dump->driver_dump))
2495 len = sizeof(dump->driver_dump) - off;
2498 src = (u8 *)&dump->driver_dump + off;
2499 memcpy(buf, src, len);
2505 off -= sizeof(dump->driver_dump);
2507 if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
2508 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
2509 len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
2512 src = (u8 *)&dump->ioa_dump + off;
2513 memcpy(buf, src, len);
2519 off -= offsetof(struct ipr_ioa_dump, ioa_data);
2522 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
2523 len = PAGE_ALIGN(off) - off;
2526 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
2527 src += off & ~PAGE_MASK;
2528 memcpy(buf, src, len);
2534 kref_put(&dump->kref, ipr_release_dump);
2539 * ipr_alloc_dump - Prepare for adapter dump
2540 * @ioa_cfg: ioa config struct
2543 * 0 on success / other on failure
2545 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
2547 struct ipr_dump *dump;
2548 unsigned long lock_flags = 0;
2551 dump = kmalloc(sizeof(struct ipr_dump), GFP_KERNEL);
2554 ipr_err("Dump memory allocation failed\n");
2558 memset(dump, 0, sizeof(struct ipr_dump));
2559 kref_init(&dump->kref);
2560 dump->ioa_cfg = ioa_cfg;
2562 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2564 if (INACTIVE != ioa_cfg->sdt_state) {
2565 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2570 ioa_cfg->dump = dump;
2571 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
2572 if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
2573 ioa_cfg->dump_taken = 1;
2574 schedule_work(&ioa_cfg->work_q);
2576 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2583 * ipr_free_dump - Free adapter dump memory
2584 * @ioa_cfg: ioa config struct
2587 * 0 on success / other on failure
2589 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
2591 struct ipr_dump *dump;
2592 unsigned long lock_flags = 0;
2596 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2597 dump = ioa_cfg->dump;
2599 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2603 ioa_cfg->dump = NULL;
2604 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2606 kref_put(&dump->kref, ipr_release_dump);
2613 * ipr_write_dump - Setup dump state of adapter
2614 * @kobj: kobject struct
2617 * @count: buffer size
2620 * number of bytes printed to buffer
2622 static ssize_t ipr_write_dump(struct kobject *kobj, char *buf,
2623 loff_t off, size_t count)
2625 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2626 struct Scsi_Host *shost = class_to_shost(cdev);
2627 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2630 if (!capable(CAP_SYS_ADMIN))
2634 rc = ipr_alloc_dump(ioa_cfg);
2635 else if (buf[0] == '0')
2636 rc = ipr_free_dump(ioa_cfg);
2646 static struct bin_attribute ipr_dump_attr = {
2649 .mode = S_IRUSR | S_IWUSR,
2652 .read = ipr_read_dump,
2653 .write = ipr_write_dump
2656 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
2660 * ipr_change_queue_depth - Change the device's queue depth
2661 * @sdev: scsi device struct
2662 * @qdepth: depth to set
2667 static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
2669 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2670 return sdev->queue_depth;
2674 * ipr_change_queue_type - Change the device's queue type
2675 * @dsev: scsi device struct
2676 * @tag_type: type of tags to use
2679 * actual queue type set
2681 static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
2683 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2684 struct ipr_resource_entry *res;
2685 unsigned long lock_flags = 0;
2687 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2688 res = (struct ipr_resource_entry *)sdev->hostdata;
2691 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
2693 * We don't bother quiescing the device here since the
2694 * adapter firmware does it for us.
2696 scsi_set_tag_type(sdev, tag_type);
2699 scsi_activate_tcq(sdev, sdev->queue_depth);
2701 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2707 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2712 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
2713 * @dev: device struct
2717 * number of bytes printed to buffer
2719 static ssize_t ipr_show_adapter_handle(struct device *dev, char *buf)
2721 struct scsi_device *sdev = to_scsi_device(dev);
2722 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2723 struct ipr_resource_entry *res;
2724 unsigned long lock_flags = 0;
2725 ssize_t len = -ENXIO;
2727 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2728 res = (struct ipr_resource_entry *)sdev->hostdata;
2730 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->cfgte.res_handle);
2731 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2735 static struct device_attribute ipr_adapter_handle_attr = {
2737 .name = "adapter_handle",
2740 .show = ipr_show_adapter_handle
2743 static struct device_attribute *ipr_dev_attrs[] = {
2744 &ipr_adapter_handle_attr,
2749 * ipr_biosparam - Return the HSC mapping
2750 * @sdev: scsi device struct
2751 * @block_device: block device pointer
2752 * @capacity: capacity of the device
2753 * @parm: Array containing returned HSC values.
2755 * This function generates the HSC parms that fdisk uses.
2756 * We want to make sure we return something that places partitions
2757 * on 4k boundaries for best performance with the IOA.
2762 static int ipr_biosparam(struct scsi_device *sdev,
2763 struct block_device *block_device,
2764 sector_t capacity, int *parm)
2772 cylinders = capacity;
2773 sector_div(cylinders, (128 * 32));
2778 parm[2] = cylinders;
2784 * ipr_slave_destroy - Unconfigure a SCSI device
2785 * @sdev: scsi device struct
2790 static void ipr_slave_destroy(struct scsi_device *sdev)
2792 struct ipr_resource_entry *res;
2793 struct ipr_ioa_cfg *ioa_cfg;
2794 unsigned long lock_flags = 0;
2796 ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2798 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2799 res = (struct ipr_resource_entry *) sdev->hostdata;
2801 sdev->hostdata = NULL;
2804 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2808 * ipr_slave_configure - Configure a SCSI device
2809 * @sdev: scsi device struct
2811 * This function configures the specified scsi device.
2816 static int ipr_slave_configure(struct scsi_device *sdev)
2818 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2819 struct ipr_resource_entry *res;
2820 unsigned long lock_flags = 0;
2822 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2823 res = sdev->hostdata;
2825 if (ipr_is_af_dasd_device(res))
2826 sdev->type = TYPE_RAID;
2827 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res))
2828 sdev->scsi_level = 4;
2829 if (ipr_is_vset_device(res)) {
2830 sdev->timeout = IPR_VSET_RW_TIMEOUT;
2831 blk_queue_max_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
2833 if (IPR_IS_DASD_DEVICE(res->cfgte.std_inq_data))
2834 sdev->allow_restart = 1;
2835 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
2837 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2842 * ipr_slave_alloc - Prepare for commands to a device.
2843 * @sdev: scsi device struct
2845 * This function saves a pointer to the resource entry
2846 * in the scsi device struct if the device exists. We
2847 * can then use this pointer in ipr_queuecommand when
2848 * handling new commands.
2853 static int ipr_slave_alloc(struct scsi_device *sdev)
2855 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2856 struct ipr_resource_entry *res;
2857 unsigned long lock_flags;
2859 sdev->hostdata = NULL;
2861 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2863 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2864 if ((res->cfgte.res_addr.bus == sdev->channel) &&
2865 (res->cfgte.res_addr.target == sdev->id) &&
2866 (res->cfgte.res_addr.lun == sdev->lun)) {
2870 sdev->hostdata = res;
2871 res->needs_sync_complete = 1;
2876 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2882 * ipr_eh_host_reset - Reset the host adapter
2883 * @scsi_cmd: scsi command struct
2888 static int ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
2890 struct ipr_ioa_cfg *ioa_cfg;
2894 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
2896 dev_err(&ioa_cfg->pdev->dev,
2897 "Adapter being reset as a result of error recovery.\n");
2899 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2900 ioa_cfg->sdt_state = GET_DUMP;
2902 rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
2909 * ipr_eh_dev_reset - Reset the device
2910 * @scsi_cmd: scsi command struct
2912 * This function issues a device reset to the affected device.
2913 * A LUN reset will be sent to the device first. If that does
2914 * not work, a target reset will be sent.
2919 static int ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
2921 struct ipr_cmnd *ipr_cmd;
2922 struct ipr_ioa_cfg *ioa_cfg;
2923 struct ipr_resource_entry *res;
2924 struct ipr_cmd_pkt *cmd_pkt;
2928 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
2929 res = scsi_cmd->device->hostdata;
2931 if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
2935 * If we are currently going through reset/reload, return failed. This will force the
2936 * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
2939 if (ioa_cfg->in_reset_reload)
2941 if (ioa_cfg->ioa_is_dead)
2944 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
2945 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
2946 if (ipr_cmd->scsi_cmd)
2947 ipr_cmd->done = ipr_scsi_eh_done;
2951 res->resetting_device = 1;
2953 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
2955 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
2956 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
2957 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
2958 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
2960 ipr_sdev_err(scsi_cmd->device, "Resetting device\n");
2961 ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
2963 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
2965 res->resetting_device = 0;
2967 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
2970 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
2974 * ipr_bus_reset_done - Op done function for bus reset.
2975 * @ipr_cmd: ipr command struct
2977 * This function is the op done function for a bus reset
2982 static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
2984 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2985 struct ipr_resource_entry *res;
2988 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2989 if (!memcmp(&res->cfgte.res_handle, &ipr_cmd->ioarcb.res_handle,
2990 sizeof(res->cfgte.res_handle))) {
2991 scsi_report_bus_reset(ioa_cfg->host, res->cfgte.res_addr.bus);
2997 * If abort has not completed, indicate the reset has, else call the
2998 * abort's done function to wake the sleeping eh thread
3000 if (ipr_cmd->sibling->sibling)
3001 ipr_cmd->sibling->sibling = NULL;
3003 ipr_cmd->sibling->done(ipr_cmd->sibling);
3005 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3010 * ipr_abort_timeout - An abort task has timed out
3011 * @ipr_cmd: ipr command struct
3013 * This function handles when an abort task times out. If this
3014 * happens we issue a bus reset since we have resources tied
3015 * up that must be freed before returning to the midlayer.
3020 static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
3022 struct ipr_cmnd *reset_cmd;
3023 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3024 struct ipr_cmd_pkt *cmd_pkt;
3025 unsigned long lock_flags = 0;
3028 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3029 if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
3030 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3034 ipr_sdev_err(ipr_cmd->u.sdev, "Abort timed out. Resetting bus\n");
3035 reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3036 ipr_cmd->sibling = reset_cmd;
3037 reset_cmd->sibling = ipr_cmd;
3038 reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
3039 cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
3040 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3041 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
3042 cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
3044 ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3045 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3050 * ipr_cancel_op - Cancel specified op
3051 * @scsi_cmd: scsi command struct
3053 * This function cancels specified op.
3058 static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
3060 struct ipr_cmnd *ipr_cmd;
3061 struct ipr_ioa_cfg *ioa_cfg;
3062 struct ipr_resource_entry *res;
3063 struct ipr_cmd_pkt *cmd_pkt;
3068 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3069 res = scsi_cmd->device->hostdata;
3071 if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
3074 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3075 if (ipr_cmd->scsi_cmd == scsi_cmd) {
3076 ipr_cmd->done = ipr_scsi_eh_done;
3085 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3086 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
3087 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3088 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3089 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
3090 ipr_cmd->u.sdev = scsi_cmd->device;
3092 ipr_sdev_err(scsi_cmd->device, "Aborting command: %02X\n", scsi_cmd->cmnd[0]);
3093 ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
3094 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3097 * If the abort task timed out and we sent a bus reset, we will get
3098 * one the following responses to the abort
3100 if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
3105 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3106 res->needs_sync_complete = 1;
3109 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
3113 * ipr_eh_abort - Abort a single op
3114 * @scsi_cmd: scsi command struct
3119 static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
3121 struct ipr_ioa_cfg *ioa_cfg;
3124 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3126 /* If we are currently going through reset/reload, return failed. This will force the
3127 mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
3128 reset to complete */
3129 if (ioa_cfg->in_reset_reload)
3131 if (ioa_cfg->ioa_is_dead)
3133 if (!scsi_cmd->device->hostdata)
3137 return ipr_cancel_op(scsi_cmd);
3141 * ipr_handle_other_interrupt - Handle "other" interrupts
3142 * @ioa_cfg: ioa config struct
3143 * @int_reg: interrupt register
3146 * IRQ_NONE / IRQ_HANDLED
3148 static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
3149 volatile u32 int_reg)
3151 irqreturn_t rc = IRQ_HANDLED;
3153 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
3154 /* Mask the interrupt */
3155 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
3157 /* Clear the interrupt */
3158 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
3159 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
3161 list_del(&ioa_cfg->reset_cmd->queue);
3162 del_timer(&ioa_cfg->reset_cmd->timer);
3163 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
3165 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
3166 ioa_cfg->ioa_unit_checked = 1;
3168 dev_err(&ioa_cfg->pdev->dev,
3169 "Permanent IOA failure. 0x%08X\n", int_reg);
3171 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3172 ioa_cfg->sdt_state = GET_DUMP;
3174 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
3175 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3182 * ipr_isr - Interrupt service routine
3184 * @devp: pointer to ioa config struct
3185 * @regs: pt_regs struct
3188 * IRQ_NONE / IRQ_HANDLED
3190 static irqreturn_t ipr_isr(int irq, void *devp, struct pt_regs *regs)
3192 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
3193 unsigned long lock_flags = 0;
3194 volatile u32 int_reg, int_mask_reg;
3197 struct ipr_cmnd *ipr_cmd;
3198 irqreturn_t rc = IRQ_NONE;
3200 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3202 /* If interrupts are disabled, ignore the interrupt */
3203 if (!ioa_cfg->allow_interrupts) {
3204 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3208 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
3209 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
3211 /* If an interrupt on the adapter did not occur, ignore it */
3212 if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
3213 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3220 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
3221 ioa_cfg->toggle_bit) {
3223 cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
3224 IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
3226 if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
3227 ioa_cfg->errors_logged++;
3228 dev_err(&ioa_cfg->pdev->dev, "Invalid response handle from IOA\n");
3230 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3231 ioa_cfg->sdt_state = GET_DUMP;
3233 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3234 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3238 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
3240 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3242 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
3244 list_del(&ipr_cmd->queue);
3245 del_timer(&ipr_cmd->timer);
3246 ipr_cmd->done(ipr_cmd);
3250 if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
3251 ioa_cfg->hrrq_curr++;
3253 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
3254 ioa_cfg->toggle_bit ^= 1u;
3258 if (ipr_cmd != NULL) {
3259 /* Clear the PCI interrupt */
3260 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg);
3261 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
3266 if (unlikely(rc == IRQ_NONE))
3267 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
3269 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3274 * ipr_build_ioadl - Build a scatter/gather list and map the buffer
3275 * @ioa_cfg: ioa config struct
3276 * @ipr_cmd: ipr command struct
3279 * 0 on success / -1 on failure
3281 static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
3282 struct ipr_cmnd *ipr_cmd)
3285 struct scatterlist *sglist;
3287 u32 ioadl_flags = 0;
3288 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3289 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3290 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
3292 length = scsi_cmd->request_bufflen;
3297 if (scsi_cmd->use_sg) {
3298 ipr_cmd->dma_use_sg = pci_map_sg(ioa_cfg->pdev,
3299 scsi_cmd->request_buffer,
3301 scsi_cmd->sc_data_direction);
3303 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
3304 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
3305 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3306 ioarcb->write_data_transfer_length = cpu_to_be32(length);
3307 ioarcb->write_ioadl_len =
3308 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3309 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
3310 ioadl_flags = IPR_IOADL_FLAGS_READ;
3311 ioarcb->read_data_transfer_length = cpu_to_be32(length);
3312 ioarcb->read_ioadl_len =
3313 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3316 sglist = scsi_cmd->request_buffer;
3318 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3319 ioadl[i].flags_and_data_len =
3320 cpu_to_be32(ioadl_flags | sg_dma_len(&sglist[i]));
3322 cpu_to_be32(sg_dma_address(&sglist[i]));
3325 if (likely(ipr_cmd->dma_use_sg)) {
3326 ioadl[i-1].flags_and_data_len |=
3327 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3330 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
3332 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
3333 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
3334 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3335 ioarcb->write_data_transfer_length = cpu_to_be32(length);
3336 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3337 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
3338 ioadl_flags = IPR_IOADL_FLAGS_READ;
3339 ioarcb->read_data_transfer_length = cpu_to_be32(length);
3340 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3343 ipr_cmd->dma_handle = pci_map_single(ioa_cfg->pdev,
3344 scsi_cmd->request_buffer, length,
3345 scsi_cmd->sc_data_direction);
3347 if (likely(!pci_dma_mapping_error(ipr_cmd->dma_handle))) {
3348 ipr_cmd->dma_use_sg = 1;
3349 ioadl[0].flags_and_data_len =
3350 cpu_to_be32(ioadl_flags | length | IPR_IOADL_FLAGS_LAST);
3351 ioadl[0].address = cpu_to_be32(ipr_cmd->dma_handle);
3354 dev_err(&ioa_cfg->pdev->dev, "pci_map_single failed!\n");
3361 * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
3362 * @scsi_cmd: scsi command struct
3367 static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
3370 u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
3372 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
3374 case MSG_SIMPLE_TAG:
3375 rc = IPR_FLAGS_LO_SIMPLE_TASK;
3378 rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
3380 case MSG_ORDERED_TAG:
3381 rc = IPR_FLAGS_LO_ORDERED_TASK;
3390 * ipr_erp_done - Process completion of ERP for a device
3391 * @ipr_cmd: ipr command struct
3393 * This function copies the sense buffer into the scsi_cmd
3394 * struct and pushes the scsi_done function.
3399 static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
3401 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3402 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3403 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3404 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3406 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3407 scsi_cmd->result |= (DID_ERROR << 16);
3408 ipr_sdev_err(scsi_cmd->device,
3409 "Request Sense failed with IOASC: 0x%08X\n", ioasc);
3411 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
3412 SCSI_SENSE_BUFFERSIZE);
3416 res->needs_sync_complete = 1;
3419 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3420 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3421 scsi_cmd->scsi_done(scsi_cmd);
3425 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
3426 * @ipr_cmd: ipr command struct
3431 static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
3433 struct ipr_ioarcb *ioarcb;
3434 struct ipr_ioasa *ioasa;
3436 ioarcb = &ipr_cmd->ioarcb;
3437 ioasa = &ipr_cmd->ioasa;
3439 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
3440 ioarcb->write_data_transfer_length = 0;
3441 ioarcb->read_data_transfer_length = 0;
3442 ioarcb->write_ioadl_len = 0;
3443 ioarcb->read_ioadl_len = 0;
3445 ioasa->residual_data_len = 0;
3449 * ipr_erp_request_sense - Send request sense to a device
3450 * @ipr_cmd: ipr command struct
3452 * This function sends a request sense to a device as a result
3453 * of a check condition.
3458 static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
3460 struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3461 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3463 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3464 ipr_erp_done(ipr_cmd);
3468 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3470 cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
3471 cmd_pkt->cdb[0] = REQUEST_SENSE;
3472 cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
3473 cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
3474 cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
3475 cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
3477 ipr_cmd->ioadl[0].flags_and_data_len =
3478 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | SCSI_SENSE_BUFFERSIZE);
3479 ipr_cmd->ioadl[0].address =
3480 cpu_to_be32(ipr_cmd->sense_buffer_dma);
3482 ipr_cmd->ioarcb.read_ioadl_len =
3483 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3484 ipr_cmd->ioarcb.read_data_transfer_length =
3485 cpu_to_be32(SCSI_SENSE_BUFFERSIZE);
3487 ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
3488 IPR_REQUEST_SENSE_TIMEOUT * 2);
3492 * ipr_erp_cancel_all - Send cancel all to a device
3493 * @ipr_cmd: ipr command struct
3495 * This function sends a cancel all to a device to clear the
3496 * queue. If we are running TCQ on the device, QERR is set to 1,
3497 * which means all outstanding ops have been dropped on the floor.
3498 * Cancel all will return them to us.
3503 static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
3505 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3506 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3507 struct ipr_cmd_pkt *cmd_pkt;
3511 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3513 if (!scsi_get_tag_type(scsi_cmd->device)) {
3514 ipr_erp_request_sense(ipr_cmd);
3518 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3519 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3520 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
3522 ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
3523 IPR_CANCEL_ALL_TIMEOUT);
3527 * ipr_dump_ioasa - Dump contents of IOASA
3528 * @ioa_cfg: ioa config struct
3529 * @ipr_cmd: ipr command struct
3531 * This function is invoked by the interrupt handler when ops
3532 * fail. It will log the IOASA if appropriate. Only called
3538 static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
3539 struct ipr_cmnd *ipr_cmd)
3544 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
3545 __be32 *ioasa_data = (__be32 *)ioasa;
3548 ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
3553 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
3556 error_index = ipr_get_error(ioasc);
3558 if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
3559 /* Don't log an error if the IOA already logged one */
3560 if (ioasa->ilid != 0)
3563 if (ipr_error_table[error_index].log_ioasa == 0)
3567 ipr_sdev_err(ipr_cmd->scsi_cmd->device, "%s\n",
3568 ipr_error_table[error_index].error);
3570 if ((ioasa->u.gpdd.end_state <= ARRAY_SIZE(ipr_gpdd_dev_end_states)) &&
3571 (ioasa->u.gpdd.bus_phase <= ARRAY_SIZE(ipr_gpdd_dev_bus_phases))) {
3572 ipr_sdev_err(ipr_cmd->scsi_cmd->device,
3573 "Device End state: %s Phase: %s\n",
3574 ipr_gpdd_dev_end_states[ioasa->u.gpdd.end_state],
3575 ipr_gpdd_dev_bus_phases[ioasa->u.gpdd.bus_phase]);
3578 if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
3579 data_len = sizeof(struct ipr_ioasa);
3581 data_len = be16_to_cpu(ioasa->ret_stat_len);
3583 ipr_err("IOASA Dump:\n");
3585 for (i = 0; i < data_len / 4; i += 4) {
3586 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
3587 be32_to_cpu(ioasa_data[i]),
3588 be32_to_cpu(ioasa_data[i+1]),
3589 be32_to_cpu(ioasa_data[i+2]),
3590 be32_to_cpu(ioasa_data[i+3]));
3595 * ipr_gen_sense - Generate SCSI sense data from an IOASA
3597 * @sense_buf: sense data buffer
3602 static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
3605 u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
3606 struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
3607 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
3608 u32 ioasc = be32_to_cpu(ioasa->ioasc);
3610 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
3612 if (ioasc >= IPR_FIRST_DRIVER_IOASC)
3615 ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
3617 if (ipr_is_vset_device(res) &&
3618 ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
3619 ioasa->u.vset.failing_lba_hi != 0) {
3620 sense_buf[0] = 0x72;
3621 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
3622 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
3623 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
3627 sense_buf[9] = 0x0A;
3628 sense_buf[10] = 0x80;
3630 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
3632 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
3633 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
3634 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
3635 sense_buf[15] = failing_lba & 0x000000ff;
3637 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
3639 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
3640 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
3641 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
3642 sense_buf[19] = failing_lba & 0x000000ff;
3644 sense_buf[0] = 0x70;
3645 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
3646 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
3647 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
3649 /* Illegal request */
3650 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
3651 (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
3652 sense_buf[7] = 10; /* additional length */
3654 /* IOARCB was in error */
3655 if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
3656 sense_buf[15] = 0xC0;
3657 else /* Parameter data was invalid */
3658 sense_buf[15] = 0x80;
3661 ((IPR_FIELD_POINTER_MASK &
3662 be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
3664 (IPR_FIELD_POINTER_MASK &
3665 be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
3667 if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
3668 if (ipr_is_vset_device(res))
3669 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
3671 failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
3673 sense_buf[0] |= 0x80; /* Or in the Valid bit */
3674 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
3675 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
3676 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
3677 sense_buf[6] = failing_lba & 0x000000ff;
3680 sense_buf[7] = 6; /* additional length */
3686 * ipr_erp_start - Process an error response for a SCSI op
3687 * @ioa_cfg: ioa config struct
3688 * @ipr_cmd: ipr command struct
3690 * This function determines whether or not to initiate ERP
3691 * on the affected device.
3696 static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
3697 struct ipr_cmnd *ipr_cmd)
3699 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3700 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3701 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3704 ipr_scsi_eh_done(ipr_cmd);
3708 if (ipr_is_gscsi(res))
3709 ipr_dump_ioasa(ioa_cfg, ipr_cmd);
3711 ipr_gen_sense(ipr_cmd);
3713 switch (ioasc & IPR_IOASC_IOASC_MASK) {
3714 case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
3715 scsi_cmd->result |= (DID_IMM_RETRY << 16);
3717 case IPR_IOASC_IR_RESOURCE_HANDLE:
3718 scsi_cmd->result |= (DID_NO_CONNECT << 16);
3720 case IPR_IOASC_HW_SEL_TIMEOUT:
3721 scsi_cmd->result |= (DID_NO_CONNECT << 16);
3722 res->needs_sync_complete = 1;
3724 case IPR_IOASC_SYNC_REQUIRED:
3726 res->needs_sync_complete = 1;
3727 scsi_cmd->result |= (DID_IMM_RETRY << 16);
3729 case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
3730 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
3732 case IPR_IOASC_BUS_WAS_RESET:
3733 case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
3735 * Report the bus reset and ask for a retry. The device
3736 * will give CC/UA the next command.
3738 if (!res->resetting_device)
3739 scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
3740 scsi_cmd->result |= (DID_ERROR << 16);
3741 res->needs_sync_complete = 1;
3743 case IPR_IOASC_HW_DEV_BUS_STATUS:
3744 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
3745 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
3746 ipr_erp_cancel_all(ipr_cmd);
3749 res->needs_sync_complete = 1;
3751 case IPR_IOASC_NR_INIT_CMD_REQUIRED:
3754 scsi_cmd->result |= (DID_ERROR << 16);
3755 if (!ipr_is_vset_device(res))
3756 res->needs_sync_complete = 1;
3760 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3761 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3762 scsi_cmd->scsi_done(scsi_cmd);
3766 * ipr_scsi_done - mid-layer done function
3767 * @ipr_cmd: ipr command struct
3769 * This function is invoked by the interrupt handler for
3770 * ops generated by the SCSI mid-layer
3775 static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
3777 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3778 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3779 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3781 scsi_cmd->resid = be32_to_cpu(ipr_cmd->ioasa.residual_data_len);
3783 if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
3784 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3785 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3786 scsi_cmd->scsi_done(scsi_cmd);
3788 ipr_erp_start(ioa_cfg, ipr_cmd);
3792 * ipr_save_ioafp_mode_select - Save adapters mode select data
3793 * @ioa_cfg: ioa config struct
3794 * @scsi_cmd: scsi command struct
3796 * This function saves mode select data for the adapter to
3797 * use following an adapter reset.
3800 * 0 on success / SCSI_MLQUEUE_HOST_BUSY on failure
3802 static int ipr_save_ioafp_mode_select(struct ipr_ioa_cfg *ioa_cfg,
3803 struct scsi_cmnd *scsi_cmd)
3805 if (!ioa_cfg->saved_mode_pages) {
3806 ioa_cfg->saved_mode_pages = kmalloc(sizeof(struct ipr_mode_pages),
3808 if (!ioa_cfg->saved_mode_pages) {
3809 dev_err(&ioa_cfg->pdev->dev,
3810 "IOA mode select buffer allocation failed\n");
3811 return SCSI_MLQUEUE_HOST_BUSY;
3815 memcpy(ioa_cfg->saved_mode_pages, scsi_cmd->buffer, scsi_cmd->cmnd[4]);
3816 ioa_cfg->saved_mode_page_len = scsi_cmd->cmnd[4];
3821 * ipr_queuecommand - Queue a mid-layer request
3822 * @scsi_cmd: scsi command struct
3823 * @done: done function
3825 * This function queues a request generated by the mid-layer.
3829 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3830 * SCSI_MLQUEUE_HOST_BUSY if host is busy
3832 static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
3833 void (*done) (struct scsi_cmnd *))
3835 struct ipr_ioa_cfg *ioa_cfg;
3836 struct ipr_resource_entry *res;
3837 struct ipr_ioarcb *ioarcb;
3838 struct ipr_cmnd *ipr_cmd;
3841 scsi_cmd->scsi_done = done;
3842 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3843 res = scsi_cmd->device->hostdata;
3844 scsi_cmd->result = (DID_OK << 16);
3847 * We are currently blocking all devices due to a host reset
3848 * We have told the host to stop giving us new requests, but
3849 * ERP ops don't count. FIXME
3851 if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
3852 return SCSI_MLQUEUE_HOST_BUSY;
3855 * FIXME - Create scsi_set_host_offline interface
3856 * and the ioa_is_dead check can be removed
3858 if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
3859 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
3860 scsi_cmd->result = (DID_NO_CONNECT << 16);
3861 scsi_cmd->scsi_done(scsi_cmd);
3865 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3866 ioarcb = &ipr_cmd->ioarcb;
3867 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
3869 memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
3870 ipr_cmd->scsi_cmd = scsi_cmd;
3871 ioarcb->res_handle = res->cfgte.res_handle;
3872 ipr_cmd->done = ipr_scsi_done;
3873 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
3875 if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
3876 if (scsi_cmd->underflow == 0)
3877 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
3879 if (res->needs_sync_complete) {
3880 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
3881 res->needs_sync_complete = 0;
3884 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
3885 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
3886 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
3887 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
3890 if (scsi_cmd->cmnd[0] >= 0xC0 &&
3891 (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
3892 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
3894 if (ipr_is_ioa_resource(res) && scsi_cmd->cmnd[0] == MODE_SELECT)
3895 rc = ipr_save_ioafp_mode_select(ioa_cfg, scsi_cmd);
3897 if (likely(rc == 0))
3898 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
3900 if (likely(rc == 0)) {
3902 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
3903 ioa_cfg->regs.ioarrin_reg);
3905 list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3906 return SCSI_MLQUEUE_HOST_BUSY;
3913 * ipr_info - Get information about the card/driver
3914 * @scsi_host: scsi host struct
3917 * pointer to buffer with description string
3919 static const char * ipr_ioa_info(struct Scsi_Host *host)
3921 static char buffer[512];
3922 struct ipr_ioa_cfg *ioa_cfg;
3923 unsigned long lock_flags = 0;
3925 ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
3927 spin_lock_irqsave(host->host_lock, lock_flags);
3928 sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
3929 spin_unlock_irqrestore(host->host_lock, lock_flags);
3934 static struct scsi_host_template driver_template = {
3935 .module = THIS_MODULE,
3937 .info = ipr_ioa_info,
3938 .queuecommand = ipr_queuecommand,
3939 .eh_abort_handler = ipr_eh_abort,
3940 .eh_device_reset_handler = ipr_eh_dev_reset,
3941 .eh_host_reset_handler = ipr_eh_host_reset,
3942 .slave_alloc = ipr_slave_alloc,
3943 .slave_configure = ipr_slave_configure,
3944 .slave_destroy = ipr_slave_destroy,
3945 .change_queue_depth = ipr_change_queue_depth,
3946 .change_queue_type = ipr_change_queue_type,
3947 .bios_param = ipr_biosparam,
3948 .can_queue = IPR_MAX_COMMANDS,
3950 .sg_tablesize = IPR_MAX_SGLIST,
3951 .max_sectors = IPR_IOA_MAX_SECTORS,
3952 .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
3953 .use_clustering = ENABLE_CLUSTERING,
3954 .shost_attrs = ipr_ioa_attrs,
3955 .sdev_attrs = ipr_dev_attrs,
3956 .proc_name = IPR_NAME
3959 #ifdef CONFIG_PPC_PSERIES
3960 static const u16 ipr_blocked_processors[] = {
3972 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
3973 * @ioa_cfg: ioa cfg struct
3975 * Adapters that use Gemstone revision < 3.1 do not work reliably on
3976 * certain pSeries hardware. This function determines if the given
3977 * adapter is in one of these confgurations or not.
3980 * 1 if adapter is not supported / 0 if adapter is supported
3982 static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
3987 if (ioa_cfg->type == 0x5702) {
3988 if (pci_read_config_byte(ioa_cfg->pdev, PCI_REVISION_ID,
3989 &rev_id) == PCIBIOS_SUCCESSFUL) {
3991 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
3992 if (__is_processor(ipr_blocked_processors[i]))
4001 #define ipr_invalid_adapter(ioa_cfg) 0
4005 * ipr_ioa_bringdown_done - IOA bring down completion.
4006 * @ipr_cmd: ipr command struct
4008 * This function processes the completion of an adapter bring down.
4009 * It wakes any reset sleepers.
4014 static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
4016 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4019 ioa_cfg->in_reset_reload = 0;
4020 ioa_cfg->reset_retries = 0;
4021 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4022 wake_up_all(&ioa_cfg->reset_wait_q);
4024 spin_unlock_irq(ioa_cfg->host->host_lock);
4025 scsi_unblock_requests(ioa_cfg->host);
4026 spin_lock_irq(ioa_cfg->host->host_lock);
4029 return IPR_RC_JOB_RETURN;
4033 * ipr_ioa_reset_done - IOA reset completion.
4034 * @ipr_cmd: ipr command struct
4036 * This function processes the completion of an adapter reset.
4037 * It schedules any necessary mid-layer add/removes and
4038 * wakes any reset sleepers.
4043 static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
4045 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4046 struct ipr_resource_entry *res;
4047 struct ipr_hostrcb *hostrcb, *temp;
4051 ioa_cfg->in_reset_reload = 0;
4052 ioa_cfg->allow_cmds = 1;
4053 ioa_cfg->reset_cmd = NULL;
4055 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4056 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
4061 schedule_work(&ioa_cfg->work_q);
4063 list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
4064 list_del(&hostrcb->queue);
4065 if (i++ < IPR_NUM_LOG_HCAMS)
4066 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
4068 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
4071 dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
4073 ioa_cfg->reset_retries = 0;
4074 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4075 wake_up_all(&ioa_cfg->reset_wait_q);
4077 spin_unlock_irq(ioa_cfg->host->host_lock);
4078 scsi_unblock_requests(ioa_cfg->host);
4079 spin_lock_irq(ioa_cfg->host->host_lock);
4081 if (!ioa_cfg->allow_cmds)
4082 scsi_block_requests(ioa_cfg->host);
4085 return IPR_RC_JOB_RETURN;
4089 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
4090 * @supported_dev: supported device struct
4091 * @vpids: vendor product id struct
4096 static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
4097 struct ipr_std_inq_vpids *vpids)
4099 memset(supported_dev, 0, sizeof(struct ipr_supported_device));
4100 memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
4101 supported_dev->num_records = 1;
4102 supported_dev->data_length =
4103 cpu_to_be16(sizeof(struct ipr_supported_device));
4104 supported_dev->reserved = 0;
4108 * ipr_set_supported_devs - Send Set Supported Devices for a device
4109 * @ipr_cmd: ipr command struct
4111 * This function send a Set Supported Devices to the adapter
4114 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4116 static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
4118 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4119 struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
4120 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4121 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4122 struct ipr_resource_entry *res = ipr_cmd->u.res;
4124 ipr_cmd->job_step = ipr_ioa_reset_done;
4126 list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
4127 if (!ipr_is_af_dasd_device(res))
4130 ipr_cmd->u.res = res;
4131 ipr_set_sup_dev_dflt(supp_dev, &res->cfgte.std_inq_data.vpids);
4133 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4134 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4135 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4137 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
4138 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
4139 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
4141 ioadl->flags_and_data_len = cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST |
4142 sizeof(struct ipr_supported_device));
4143 ioadl->address = cpu_to_be32(ioa_cfg->vpd_cbs_dma +
4144 offsetof(struct ipr_misc_cbs, supp_dev));
4145 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4146 ioarcb->write_data_transfer_length =
4147 cpu_to_be32(sizeof(struct ipr_supported_device));
4149 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
4150 IPR_SET_SUP_DEVICE_TIMEOUT);
4152 ipr_cmd->job_step = ipr_set_supported_devs;
4153 return IPR_RC_JOB_RETURN;
4156 return IPR_RC_JOB_CONTINUE;
4160 * ipr_get_mode_page - Locate specified mode page
4161 * @mode_pages: mode page buffer
4162 * @page_code: page code to find
4163 * @len: minimum required length for mode page
4166 * pointer to mode page / NULL on failure
4168 static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
4169 u32 page_code, u32 len)
4171 struct ipr_mode_page_hdr *mode_hdr;
4175 if (!mode_pages || (mode_pages->hdr.length == 0))
4178 length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
4179 mode_hdr = (struct ipr_mode_page_hdr *)
4180 (mode_pages->data + mode_pages->hdr.block_desc_len);
4183 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
4184 if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
4188 page_length = (sizeof(struct ipr_mode_page_hdr) +
4189 mode_hdr->page_length);
4190 length -= page_length;
4191 mode_hdr = (struct ipr_mode_page_hdr *)
4192 ((unsigned long)mode_hdr + page_length);
4199 * ipr_check_term_power - Check for term power errors
4200 * @ioa_cfg: ioa config struct
4201 * @mode_pages: IOAFP mode pages buffer
4203 * Check the IOAFP's mode page 28 for term power errors
4208 static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
4209 struct ipr_mode_pages *mode_pages)
4213 struct ipr_dev_bus_entry *bus;
4214 struct ipr_mode_page28 *mode_page;
4216 mode_page = ipr_get_mode_page(mode_pages, 0x28,
4217 sizeof(struct ipr_mode_page28));
4219 entry_length = mode_page->entry_length;
4221 bus = mode_page->bus;
4223 for (i = 0; i < mode_page->num_entries; i++) {
4224 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
4225 dev_err(&ioa_cfg->pdev->dev,
4226 "Term power is absent on scsi bus %d\n",
4230 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
4235 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
4236 * @ioa_cfg: ioa config struct
4238 * Looks through the config table checking for SES devices. If
4239 * the SES device is in the SES table indicating a maximum SCSI
4240 * bus speed, the speed is limited for the bus.
4245 static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
4250 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
4251 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
4252 ioa_cfg->bus_attr[i].bus_width);
4254 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
4255 ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
4260 * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
4261 * @ioa_cfg: ioa config struct
4262 * @mode_pages: mode page 28 buffer
4264 * Updates mode page 28 based on driver configuration
4269 static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
4270 struct ipr_mode_pages *mode_pages)
4272 int i, entry_length;
4273 struct ipr_dev_bus_entry *bus;
4274 struct ipr_bus_attributes *bus_attr;
4275 struct ipr_mode_page28 *mode_page;
4277 mode_page = ipr_get_mode_page(mode_pages, 0x28,
4278 sizeof(struct ipr_mode_page28));
4280 entry_length = mode_page->entry_length;
4282 /* Loop for each device bus entry */
4283 for (i = 0, bus = mode_page->bus;
4284 i < mode_page->num_entries;
4285 i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
4286 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
4287 dev_err(&ioa_cfg->pdev->dev,
4288 "Invalid resource address reported: 0x%08X\n",
4289 IPR_GET_PHYS_LOC(bus->res_addr));
4293 bus_attr = &ioa_cfg->bus_attr[i];
4294 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
4295 bus->bus_width = bus_attr->bus_width;
4296 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
4297 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
4298 if (bus_attr->qas_enabled)
4299 bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
4301 bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
4306 * ipr_build_mode_select - Build a mode select command
4307 * @ipr_cmd: ipr command struct
4308 * @res_handle: resource handle to send command to
4309 * @parm: Byte 2 of Mode Sense command
4310 * @dma_addr: DMA buffer address
4311 * @xfer_len: data transfer length
4316 static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
4317 __be32 res_handle, u8 parm, u32 dma_addr,
4320 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4321 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4323 ioarcb->res_handle = res_handle;
4324 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4325 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4326 ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
4327 ioarcb->cmd_pkt.cdb[1] = parm;
4328 ioarcb->cmd_pkt.cdb[4] = xfer_len;
4330 ioadl->flags_and_data_len =
4331 cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST | xfer_len);
4332 ioadl->address = cpu_to_be32(dma_addr);
4333 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4334 ioarcb->write_data_transfer_length = cpu_to_be32(xfer_len);
4338 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
4339 * @ipr_cmd: ipr command struct
4341 * This function sets up the SCSI bus attributes and sends
4342 * a Mode Select for Page 28 to activate them.
4347 static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
4349 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4350 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
4354 if (ioa_cfg->saved_mode_pages) {
4355 memcpy(mode_pages, ioa_cfg->saved_mode_pages,
4356 ioa_cfg->saved_mode_page_len);
4357 length = ioa_cfg->saved_mode_page_len;
4359 ipr_scsi_bus_speed_limit(ioa_cfg);
4360 ipr_check_term_power(ioa_cfg, mode_pages);
4361 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
4362 length = mode_pages->hdr.length + 1;
4363 mode_pages->hdr.length = 0;
4366 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
4367 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
4370 ipr_cmd->job_step = ipr_set_supported_devs;
4371 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
4372 struct ipr_resource_entry, queue);
4374 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4377 return IPR_RC_JOB_RETURN;
4381 * ipr_build_mode_sense - Builds a mode sense command
4382 * @ipr_cmd: ipr command struct
4383 * @res: resource entry struct
4384 * @parm: Byte 2 of mode sense command
4385 * @dma_addr: DMA address of mode sense buffer
4386 * @xfer_len: Size of DMA buffer
4391 static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
4393 u8 parm, u32 dma_addr, u8 xfer_len)
4395 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4396 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4398 ioarcb->res_handle = res_handle;
4399 ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
4400 ioarcb->cmd_pkt.cdb[2] = parm;
4401 ioarcb->cmd_pkt.cdb[4] = xfer_len;
4402 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4404 ioadl->flags_and_data_len =
4405 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
4406 ioadl->address = cpu_to_be32(dma_addr);
4407 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4408 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
4412 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
4413 * @ipr_cmd: ipr command struct
4415 * This function send a Page 28 mode sense to the IOA to
4416 * retrieve SCSI bus attributes.
4421 static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
4423 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4426 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
4427 0x28, ioa_cfg->vpd_cbs_dma +
4428 offsetof(struct ipr_misc_cbs, mode_pages),
4429 sizeof(struct ipr_mode_pages));
4431 ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
4433 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4436 return IPR_RC_JOB_RETURN;
4440 * ipr_init_res_table - Initialize the resource table
4441 * @ipr_cmd: ipr command struct
4443 * This function looks through the existing resource table, comparing
4444 * it with the config table. This function will take care of old/new
4445 * devices and schedule adding/removing them from the mid-layer
4449 * IPR_RC_JOB_CONTINUE
4451 static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
4453 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4454 struct ipr_resource_entry *res, *temp;
4455 struct ipr_config_table_entry *cfgte;
4460 if (ioa_cfg->cfg_table->hdr.flags & IPR_UCODE_DOWNLOAD_REQ)
4461 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
4463 list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
4464 list_move_tail(&res->queue, &old_res);
4466 for (i = 0; i < ioa_cfg->cfg_table->hdr.num_entries; i++) {
4467 cfgte = &ioa_cfg->cfg_table->dev[i];
4470 list_for_each_entry_safe(res, temp, &old_res, queue) {
4471 if (!memcmp(&res->cfgte.res_addr,
4472 &cfgte->res_addr, sizeof(cfgte->res_addr))) {
4473 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4480 if (list_empty(&ioa_cfg->free_res_q)) {
4481 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
4486 res = list_entry(ioa_cfg->free_res_q.next,
4487 struct ipr_resource_entry, queue);
4488 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4489 ipr_init_res_entry(res);
4494 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
4497 list_for_each_entry_safe(res, temp, &old_res, queue) {
4499 res->del_from_ml = 1;
4500 res->sdev->hostdata = NULL;
4501 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4503 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
4507 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
4510 return IPR_RC_JOB_CONTINUE;
4514 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
4515 * @ipr_cmd: ipr command struct
4517 * This function sends a Query IOA Configuration command
4518 * to the adapter to retrieve the IOA configuration table.
4523 static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
4525 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4526 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4527 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4528 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
4531 dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
4532 ucode_vpd->major_release, ucode_vpd->card_type,
4533 ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
4534 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4535 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4537 ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
4538 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_config_table) >> 8) & 0xff;
4539 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_config_table) & 0xff;
4541 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4542 ioarcb->read_data_transfer_length =
4543 cpu_to_be32(sizeof(struct ipr_config_table));
4545 ioadl->address = cpu_to_be32(ioa_cfg->cfg_table_dma);
4546 ioadl->flags_and_data_len =
4547 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(struct ipr_config_table));
4549 ipr_cmd->job_step = ipr_init_res_table;
4551 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4554 return IPR_RC_JOB_RETURN;
4558 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
4559 * @ipr_cmd: ipr command struct
4561 * This utility function sends an inquiry to the adapter.
4566 static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
4567 u32 dma_addr, u8 xfer_len)
4569 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4570 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4573 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4574 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4576 ioarcb->cmd_pkt.cdb[0] = INQUIRY;
4577 ioarcb->cmd_pkt.cdb[1] = flags;
4578 ioarcb->cmd_pkt.cdb[2] = page;
4579 ioarcb->cmd_pkt.cdb[4] = xfer_len;
4581 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4582 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
4584 ioadl->address = cpu_to_be32(dma_addr);
4585 ioadl->flags_and_data_len =
4586 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
4588 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4593 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
4594 * @ipr_cmd: ipr command struct
4596 * This function sends a Page 3 inquiry to the adapter
4597 * to retrieve software VPD information.
4600 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4602 static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
4604 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4609 /* Grab the type out of the VPD and store it away */
4610 memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
4612 ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
4614 ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
4616 ipr_ioafp_inquiry(ipr_cmd, 1, 3,
4617 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
4618 sizeof(struct ipr_inquiry_page3));
4621 return IPR_RC_JOB_RETURN;
4625 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
4626 * @ipr_cmd: ipr command struct
4628 * This function sends a standard inquiry to the adapter.
4633 static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
4635 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4638 ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
4640 ipr_ioafp_inquiry(ipr_cmd, 0, 0,
4641 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
4642 sizeof(struct ipr_ioa_vpd));
4645 return IPR_RC_JOB_RETURN;
4649 * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
4650 * @ipr_cmd: ipr command struct
4652 * This function send an Identify Host Request Response Queue
4653 * command to establish the HRRQ with the adapter.
4658 static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
4660 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4661 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4664 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
4666 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
4667 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4669 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4670 ioarcb->cmd_pkt.cdb[2] =
4671 ((u32) ioa_cfg->host_rrq_dma >> 24) & 0xff;
4672 ioarcb->cmd_pkt.cdb[3] =
4673 ((u32) ioa_cfg->host_rrq_dma >> 16) & 0xff;
4674 ioarcb->cmd_pkt.cdb[4] =
4675 ((u32) ioa_cfg->host_rrq_dma >> 8) & 0xff;
4676 ioarcb->cmd_pkt.cdb[5] =
4677 ((u32) ioa_cfg->host_rrq_dma) & 0xff;
4678 ioarcb->cmd_pkt.cdb[7] =
4679 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
4680 ioarcb->cmd_pkt.cdb[8] =
4681 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
4683 ipr_cmd->job_step = ipr_ioafp_std_inquiry;
4685 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4688 return IPR_RC_JOB_RETURN;
4692 * ipr_reset_timer_done - Adapter reset timer function
4693 * @ipr_cmd: ipr command struct
4695 * Description: This function is used in adapter reset processing
4696 * for timing events. If the reset_cmd pointer in the IOA
4697 * config struct is not this adapter's we are doing nested
4698 * resets and fail_all_ops will take care of freeing the
4704 static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
4706 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4707 unsigned long lock_flags = 0;
4709 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4711 if (ioa_cfg->reset_cmd == ipr_cmd) {
4712 list_del(&ipr_cmd->queue);
4713 ipr_cmd->done(ipr_cmd);
4716 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4720 * ipr_reset_start_timer - Start a timer for adapter reset job
4721 * @ipr_cmd: ipr command struct
4722 * @timeout: timeout value
4724 * Description: This function is used in adapter reset processing
4725 * for timing events. If the reset_cmd pointer in the IOA
4726 * config struct is not this adapter's we are doing nested
4727 * resets and fail_all_ops will take care of freeing the
4733 static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
4734 unsigned long timeout)
4736 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
4737 ipr_cmd->done = ipr_reset_ioa_job;
4739 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
4740 ipr_cmd->timer.expires = jiffies + timeout;
4741 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
4742 add_timer(&ipr_cmd->timer);
4746 * ipr_init_ioa_mem - Initialize ioa_cfg control block
4747 * @ioa_cfg: ioa cfg struct
4752 static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
4754 memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
4756 /* Initialize Host RRQ pointers */
4757 ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
4758 ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
4759 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
4760 ioa_cfg->toggle_bit = 1;
4762 /* Zero out config table */
4763 memset(ioa_cfg->cfg_table, 0, sizeof(struct ipr_config_table));
4767 * ipr_reset_enable_ioa - Enable the IOA following a reset.
4768 * @ipr_cmd: ipr command struct
4770 * This function reinitializes some control blocks and
4771 * enables destructive diagnostics on the adapter.
4776 static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
4778 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4779 volatile u32 int_reg;
4782 ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
4783 ipr_init_ioa_mem(ioa_cfg);
4785 ioa_cfg->allow_interrupts = 1;
4786 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4788 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4789 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
4790 ioa_cfg->regs.clr_interrupt_mask_reg);
4791 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4792 return IPR_RC_JOB_CONTINUE;
4795 /* Enable destructive diagnostics on IOA */
4796 writel(IPR_DOORBELL, ioa_cfg->regs.set_uproc_interrupt_reg);
4798 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
4799 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4801 dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
4803 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
4804 ipr_cmd->timer.expires = jiffies + (ipr_transop_timeout * HZ);
4805 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
4806 ipr_cmd->done = ipr_reset_ioa_job;
4807 add_timer(&ipr_cmd->timer);
4808 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
4811 return IPR_RC_JOB_RETURN;
4815 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
4816 * @ipr_cmd: ipr command struct
4818 * This function is invoked when an adapter dump has run out
4819 * of processing time.
4822 * IPR_RC_JOB_CONTINUE
4824 static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
4826 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4828 if (ioa_cfg->sdt_state == GET_DUMP)
4829 ioa_cfg->sdt_state = ABORT_DUMP;
4831 ipr_cmd->job_step = ipr_reset_alert;
4833 return IPR_RC_JOB_CONTINUE;
4837 * ipr_unit_check_no_data - Log a unit check/no data error log
4838 * @ioa_cfg: ioa config struct
4840 * Logs an error indicating the adapter unit checked, but for some
4841 * reason, we were unable to fetch the unit check buffer.
4846 static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
4848 ioa_cfg->errors_logged++;
4849 dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
4853 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
4854 * @ioa_cfg: ioa config struct
4856 * Fetches the unit check buffer from the adapter by clocking the data
4857 * through the mailbox register.
4862 static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
4864 unsigned long mailbox;
4865 struct ipr_hostrcb *hostrcb;
4866 struct ipr_uc_sdt sdt;
4869 mailbox = readl(ioa_cfg->ioa_mailbox);
4871 if (!ipr_sdt_is_fmt2(mailbox)) {
4872 ipr_unit_check_no_data(ioa_cfg);
4876 memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
4877 rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
4878 (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
4880 if (rc || (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE) ||
4881 !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY)) {
4882 ipr_unit_check_no_data(ioa_cfg);
4886 /* Find length of the first sdt entry (UC buffer) */
4887 length = (be32_to_cpu(sdt.entry[0].end_offset) -
4888 be32_to_cpu(sdt.entry[0].bar_str_offset)) & IPR_FMT2_MBX_ADDR_MASK;
4890 hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
4891 struct ipr_hostrcb, queue);
4892 list_del(&hostrcb->queue);
4893 memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
4895 rc = ipr_get_ldump_data_section(ioa_cfg,
4896 be32_to_cpu(sdt.entry[0].bar_str_offset),
4897 (__be32 *)&hostrcb->hcam,
4898 min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
4901 ipr_handle_log_data(ioa_cfg, hostrcb);
4903 ipr_unit_check_no_data(ioa_cfg);
4905 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
4909 * ipr_reset_restore_cfg_space - Restore PCI config space.
4910 * @ipr_cmd: ipr command struct
4912 * Description: This function restores the saved PCI config space of
4913 * the adapter, fails all outstanding ops back to the callers, and
4914 * fetches the dump/unit check if applicable to this reset.
4917 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4919 static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
4921 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4925 rc = pci_restore_state(ioa_cfg->pdev);
4927 if (rc != PCIBIOS_SUCCESSFUL) {
4928 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4929 return IPR_RC_JOB_CONTINUE;
4932 if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
4933 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4934 return IPR_RC_JOB_CONTINUE;
4937 ipr_fail_all_ops(ioa_cfg);
4939 if (ioa_cfg->ioa_unit_checked) {
4940 ioa_cfg->ioa_unit_checked = 0;
4941 ipr_get_unit_check_buffer(ioa_cfg);
4942 ipr_cmd->job_step = ipr_reset_alert;
4943 ipr_reset_start_timer(ipr_cmd, 0);
4944 return IPR_RC_JOB_RETURN;
4947 if (ioa_cfg->in_ioa_bringdown) {
4948 ipr_cmd->job_step = ipr_ioa_bringdown_done;
4950 ipr_cmd->job_step = ipr_reset_enable_ioa;
4952 if (GET_DUMP == ioa_cfg->sdt_state) {
4953 ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
4954 ipr_cmd->job_step = ipr_reset_wait_for_dump;
4955 schedule_work(&ioa_cfg->work_q);
4956 return IPR_RC_JOB_RETURN;
4961 return IPR_RC_JOB_CONTINUE;
4965 * ipr_reset_start_bist - Run BIST on the adapter.
4966 * @ipr_cmd: ipr command struct
4968 * Description: This function runs BIST on the adapter, then delays 2 seconds.
4971 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4973 static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
4975 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4979 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
4981 if (rc != PCIBIOS_SUCCESSFUL) {
4982 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4983 rc = IPR_RC_JOB_CONTINUE;
4985 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
4986 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
4987 rc = IPR_RC_JOB_RETURN;
4995 * ipr_reset_allowed - Query whether or not IOA can be reset
4996 * @ioa_cfg: ioa config struct
4999 * 0 if reset not allowed / non-zero if reset is allowed
5001 static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
5003 volatile u32 temp_reg;
5005 temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5006 return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
5010 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
5011 * @ipr_cmd: ipr command struct
5013 * Description: This function waits for adapter permission to run BIST,
5014 * then runs BIST. If the adapter does not give permission after a
5015 * reasonable time, we will reset the adapter anyway. The impact of
5016 * resetting the adapter without warning the adapter is the risk of
5017 * losing the persistent error log on the adapter. If the adapter is
5018 * reset while it is writing to the flash on the adapter, the flash
5019 * segment will have bad ECC and be zeroed.
5022 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5024 static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
5026 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5027 int rc = IPR_RC_JOB_RETURN;
5029 if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
5030 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
5031 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5033 ipr_cmd->job_step = ipr_reset_start_bist;
5034 rc = IPR_RC_JOB_CONTINUE;
5041 * ipr_reset_alert_part2 - Alert the adapter of a pending reset
5042 * @ipr_cmd: ipr command struct
5044 * Description: This function alerts the adapter that it will be reset.
5045 * If memory space is not currently enabled, proceed directly
5046 * to running BIST on the adapter. The timer must always be started
5047 * so we guarantee we do not run BIST from ipr_isr.
5052 static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
5054 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5059 rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
5061 if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
5062 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
5063 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg);
5064 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
5066 ipr_cmd->job_step = ipr_reset_start_bist;
5069 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
5070 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5073 return IPR_RC_JOB_RETURN;
5077 * ipr_reset_ucode_download_done - Microcode download completion
5078 * @ipr_cmd: ipr command struct
5080 * Description: This function unmaps the microcode download buffer.
5083 * IPR_RC_JOB_CONTINUE
5085 static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
5087 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5088 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5090 pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
5091 sglist->num_sg, DMA_TO_DEVICE);
5093 ipr_cmd->job_step = ipr_reset_alert;
5094 return IPR_RC_JOB_CONTINUE;
5098 * ipr_reset_ucode_download - Download microcode to the adapter
5099 * @ipr_cmd: ipr command struct
5101 * Description: This function checks to see if it there is microcode
5102 * to download to the adapter. If there is, a download is performed.
5105 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5107 static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
5109 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5110 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5113 ipr_cmd->job_step = ipr_reset_alert;
5116 return IPR_RC_JOB_CONTINUE;
5118 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5119 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5120 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
5121 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
5122 ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
5123 ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
5124 ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
5126 if (ipr_map_ucode_buffer(ipr_cmd, sglist, sglist->buffer_len)) {
5127 dev_err(&ioa_cfg->pdev->dev,
5128 "Failed to map microcode download buffer\n");
5129 return IPR_RC_JOB_CONTINUE;
5132 ipr_cmd->job_step = ipr_reset_ucode_download_done;
5134 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
5135 IPR_WRITE_BUFFER_TIMEOUT);
5138 return IPR_RC_JOB_RETURN;
5142 * ipr_reset_shutdown_ioa - Shutdown the adapter
5143 * @ipr_cmd: ipr command struct
5145 * Description: This function issues an adapter shutdown of the
5146 * specified type to the specified adapter as part of the
5147 * adapter reset job.
5150 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5152 static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
5154 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5155 enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
5156 unsigned long timeout;
5157 int rc = IPR_RC_JOB_CONTINUE;
5160 if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
5161 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5162 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5163 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
5164 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
5166 if (shutdown_type == IPR_SHUTDOWN_ABBREV)
5167 timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
5168 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
5169 timeout = IPR_INTERNAL_TIMEOUT;
5171 timeout = IPR_SHUTDOWN_TIMEOUT;
5173 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
5175 rc = IPR_RC_JOB_RETURN;
5176 ipr_cmd->job_step = ipr_reset_ucode_download;
5178 ipr_cmd->job_step = ipr_reset_alert;
5185 * ipr_reset_ioa_job - Adapter reset job
5186 * @ipr_cmd: ipr command struct
5188 * Description: This function is the job router for the adapter reset job.
5193 static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
5196 unsigned long scratch = ipr_cmd->u.scratch;
5197 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5200 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5202 if (ioa_cfg->reset_cmd != ipr_cmd) {
5204 * We are doing nested adapter resets and this is
5205 * not the current reset job.
5207 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5211 if (IPR_IOASC_SENSE_KEY(ioasc)) {
5212 dev_err(&ioa_cfg->pdev->dev,
5213 "0x%02X failed with IOASC: 0x%08X\n",
5214 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
5216 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5217 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5221 ipr_reinit_ipr_cmnd(ipr_cmd);
5222 ipr_cmd->u.scratch = scratch;
5223 rc = ipr_cmd->job_step(ipr_cmd);
5224 } while(rc == IPR_RC_JOB_CONTINUE);
5228 * _ipr_initiate_ioa_reset - Initiate an adapter reset
5229 * @ioa_cfg: ioa config struct
5230 * @job_step: first job step of reset job
5231 * @shutdown_type: shutdown type
5233 * Description: This function will initiate the reset of the given adapter
5234 * starting at the selected job step.
5235 * If the caller needs to wait on the completion of the reset,
5236 * the caller must sleep on the reset_wait_q.
5241 static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5242 int (*job_step) (struct ipr_cmnd *),
5243 enum ipr_shutdown_type shutdown_type)
5245 struct ipr_cmnd *ipr_cmd;
5247 ioa_cfg->in_reset_reload = 1;
5248 ioa_cfg->allow_cmds = 0;
5249 scsi_block_requests(ioa_cfg->host);
5251 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5252 ioa_cfg->reset_cmd = ipr_cmd;
5253 ipr_cmd->job_step = job_step;
5254 ipr_cmd->u.shutdown_type = shutdown_type;
5256 ipr_reset_ioa_job(ipr_cmd);
5260 * ipr_initiate_ioa_reset - Initiate an adapter reset
5261 * @ioa_cfg: ioa config struct
5262 * @shutdown_type: shutdown type
5264 * Description: This function will initiate the reset of the given adapter.
5265 * If the caller needs to wait on the completion of the reset,
5266 * the caller must sleep on the reset_wait_q.
5271 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5272 enum ipr_shutdown_type shutdown_type)
5274 if (ioa_cfg->ioa_is_dead)
5277 if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
5278 ioa_cfg->sdt_state = ABORT_DUMP;
5280 if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
5281 dev_err(&ioa_cfg->pdev->dev,
5282 "IOA taken offline - error recovery failed\n");
5284 ioa_cfg->reset_retries = 0;
5285 ioa_cfg->ioa_is_dead = 1;
5287 if (ioa_cfg->in_ioa_bringdown) {
5288 ioa_cfg->reset_cmd = NULL;
5289 ioa_cfg->in_reset_reload = 0;
5290 ipr_fail_all_ops(ioa_cfg);
5291 wake_up_all(&ioa_cfg->reset_wait_q);
5293 spin_unlock_irq(ioa_cfg->host->host_lock);
5294 scsi_unblock_requests(ioa_cfg->host);
5295 spin_lock_irq(ioa_cfg->host->host_lock);
5298 ioa_cfg->in_ioa_bringdown = 1;
5299 shutdown_type = IPR_SHUTDOWN_NONE;
5303 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
5308 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
5309 * @ioa_cfg: ioa cfg struct
5311 * Description: This is the second phase of adapter intialization
5312 * This function takes care of initilizing the adapter to the point
5313 * where it can accept new commands.
5316 * 0 on sucess / -EIO on failure
5318 static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
5321 unsigned long host_lock_flags = 0;
5324 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5325 dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
5326 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa, IPR_SHUTDOWN_NONE);
5328 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5329 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5330 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5332 if (ioa_cfg->ioa_is_dead) {
5334 } else if (ipr_invalid_adapter(ioa_cfg)) {
5338 dev_err(&ioa_cfg->pdev->dev,
5339 "Adapter not supported in this hardware configuration.\n");
5342 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5349 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
5350 * @ioa_cfg: ioa config struct
5355 static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5359 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
5360 if (ioa_cfg->ipr_cmnd_list[i])
5361 pci_pool_free(ioa_cfg->ipr_cmd_pool,
5362 ioa_cfg->ipr_cmnd_list[i],
5363 ioa_cfg->ipr_cmnd_list_dma[i]);
5365 ioa_cfg->ipr_cmnd_list[i] = NULL;
5368 if (ioa_cfg->ipr_cmd_pool)
5369 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
5371 ioa_cfg->ipr_cmd_pool = NULL;
5375 * ipr_free_mem - Frees memory allocated for an adapter
5376 * @ioa_cfg: ioa cfg struct
5381 static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
5385 kfree(ioa_cfg->res_entries);
5386 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
5387 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5388 ipr_free_cmd_blks(ioa_cfg);
5389 pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5390 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5391 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_config_table),
5393 ioa_cfg->cfg_table_dma);
5395 for (i = 0; i < IPR_NUM_HCAMS; i++) {
5396 pci_free_consistent(ioa_cfg->pdev,
5397 sizeof(struct ipr_hostrcb),
5398 ioa_cfg->hostrcb[i],
5399 ioa_cfg->hostrcb_dma[i]);
5402 ipr_free_dump(ioa_cfg);
5403 kfree(ioa_cfg->saved_mode_pages);
5404 kfree(ioa_cfg->trace);
5408 * ipr_free_all_resources - Free all allocated resources for an adapter.
5409 * @ipr_cmd: ipr command struct
5411 * This function frees all allocated resources for the
5412 * specified adapter.
5417 static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
5419 struct pci_dev *pdev = ioa_cfg->pdev;
5422 free_irq(pdev->irq, ioa_cfg);
5423 iounmap(ioa_cfg->hdw_dma_regs);
5424 pci_release_regions(pdev);
5425 ipr_free_mem(ioa_cfg);
5426 scsi_host_put(ioa_cfg->host);
5427 pci_disable_device(pdev);
5432 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
5433 * @ioa_cfg: ioa config struct
5436 * 0 on success / -ENOMEM on allocation failure
5438 static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5440 struct ipr_cmnd *ipr_cmd;
5441 struct ipr_ioarcb *ioarcb;
5442 dma_addr_t dma_addr;
5445 ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
5446 sizeof(struct ipr_cmnd), 8, 0);
5448 if (!ioa_cfg->ipr_cmd_pool)
5451 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
5452 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, SLAB_KERNEL, &dma_addr);
5455 ipr_free_cmd_blks(ioa_cfg);
5459 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
5460 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
5461 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
5463 ioarcb = &ipr_cmd->ioarcb;
5464 ioarcb->ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
5465 ioarcb->host_response_handle = cpu_to_be32(i << 2);
5466 ioarcb->write_ioadl_addr =
5467 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
5468 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5469 ioarcb->ioasa_host_pci_addr =
5470 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
5471 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
5472 ipr_cmd->cmd_index = i;
5473 ipr_cmd->ioa_cfg = ioa_cfg;
5474 ipr_cmd->sense_buffer_dma = dma_addr +
5475 offsetof(struct ipr_cmnd, sense_buffer);
5477 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5484 * ipr_alloc_mem - Allocate memory for an adapter
5485 * @ioa_cfg: ioa config struct
5488 * 0 on success / non-zero for error
5490 static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
5492 struct pci_dev *pdev = ioa_cfg->pdev;
5493 int i, rc = -ENOMEM;
5496 ioa_cfg->res_entries = kmalloc(sizeof(struct ipr_resource_entry) *
5497 IPR_MAX_PHYSICAL_DEVS, GFP_KERNEL);
5499 if (!ioa_cfg->res_entries)
5502 memset(ioa_cfg->res_entries, 0,
5503 sizeof(struct ipr_resource_entry) * IPR_MAX_PHYSICAL_DEVS);
5505 for (i = 0; i < IPR_MAX_PHYSICAL_DEVS; i++)
5506 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
5508 ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
5509 sizeof(struct ipr_misc_cbs),
5510 &ioa_cfg->vpd_cbs_dma);
5512 if (!ioa_cfg->vpd_cbs)
5513 goto out_free_res_entries;
5515 if (ipr_alloc_cmd_blks(ioa_cfg))
5516 goto out_free_vpd_cbs;
5518 ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
5519 sizeof(u32) * IPR_NUM_CMD_BLKS,
5520 &ioa_cfg->host_rrq_dma);
5522 if (!ioa_cfg->host_rrq)
5523 goto out_ipr_free_cmd_blocks;
5525 ioa_cfg->cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
5526 sizeof(struct ipr_config_table),
5527 &ioa_cfg->cfg_table_dma);
5529 if (!ioa_cfg->cfg_table)
5530 goto out_free_host_rrq;
5532 for (i = 0; i < IPR_NUM_HCAMS; i++) {
5533 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
5534 sizeof(struct ipr_hostrcb),
5535 &ioa_cfg->hostrcb_dma[i]);
5537 if (!ioa_cfg->hostrcb[i])
5538 goto out_free_hostrcb_dma;
5540 ioa_cfg->hostrcb[i]->hostrcb_dma =
5541 ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
5542 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
5545 ioa_cfg->trace = kmalloc(sizeof(struct ipr_trace_entry) *
5546 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
5548 if (!ioa_cfg->trace)
5549 goto out_free_hostrcb_dma;
5551 memset(ioa_cfg->trace, 0,
5552 sizeof(struct ipr_trace_entry) * IPR_NUM_TRACE_ENTRIES);
5559 out_free_hostrcb_dma:
5561 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
5562 ioa_cfg->hostrcb[i],
5563 ioa_cfg->hostrcb_dma[i]);
5565 pci_free_consistent(pdev, sizeof(struct ipr_config_table),
5566 ioa_cfg->cfg_table, ioa_cfg->cfg_table_dma);
5568 pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5569 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5570 out_ipr_free_cmd_blocks:
5571 ipr_free_cmd_blks(ioa_cfg);
5573 pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
5574 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5575 out_free_res_entries:
5576 kfree(ioa_cfg->res_entries);
5581 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
5582 * @ioa_cfg: ioa config struct
5587 static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
5591 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
5592 ioa_cfg->bus_attr[i].bus = i;
5593 ioa_cfg->bus_attr[i].qas_enabled = 0;
5594 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
5595 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
5596 ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
5598 ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
5603 * ipr_init_ioa_cfg - Initialize IOA config struct
5604 * @ioa_cfg: ioa config struct
5605 * @host: scsi host struct
5606 * @pdev: PCI dev struct
5611 static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
5612 struct Scsi_Host *host, struct pci_dev *pdev)
5614 const struct ipr_interrupt_offsets *p;
5615 struct ipr_interrupts *t;
5618 ioa_cfg->host = host;
5619 ioa_cfg->pdev = pdev;
5620 ioa_cfg->log_level = ipr_log_level;
5621 sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
5622 sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
5623 sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
5624 sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
5625 sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
5626 sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
5627 sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
5628 sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
5630 INIT_LIST_HEAD(&ioa_cfg->free_q);
5631 INIT_LIST_HEAD(&ioa_cfg->pending_q);
5632 INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
5633 INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
5634 INIT_LIST_HEAD(&ioa_cfg->free_res_q);
5635 INIT_LIST_HEAD(&ioa_cfg->used_res_q);
5636 INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread, ioa_cfg);
5637 init_waitqueue_head(&ioa_cfg->reset_wait_q);
5638 ioa_cfg->sdt_state = INACTIVE;
5640 ipr_initialize_bus_attr(ioa_cfg);
5642 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
5643 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
5644 host->max_channel = IPR_MAX_BUS_TO_SCAN;
5645 host->unique_id = host->host_no;
5646 host->max_cmd_len = IPR_MAX_CDB_LEN;
5647 pci_set_drvdata(pdev, ioa_cfg);
5649 p = &ioa_cfg->chip_cfg->regs;
5651 base = ioa_cfg->hdw_dma_regs;
5653 t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
5654 t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
5655 t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
5656 t->clr_interrupt_reg = base + p->clr_interrupt_reg;
5657 t->sense_interrupt_reg = base + p->sense_interrupt_reg;
5658 t->ioarrin_reg = base + p->ioarrin_reg;
5659 t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
5660 t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
5661 t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
5665 * ipr_get_chip_cfg - Find adapter chip configuration
5666 * @dev_id: PCI device id struct
5669 * ptr to chip config on success / NULL on failure
5671 static const struct ipr_chip_cfg_t * __devinit
5672 ipr_get_chip_cfg(const struct pci_device_id *dev_id)
5676 if (dev_id->driver_data)
5677 return (const struct ipr_chip_cfg_t *)dev_id->driver_data;
5679 for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
5680 if (ipr_chip[i].vendor == dev_id->vendor &&
5681 ipr_chip[i].device == dev_id->device)
5682 return ipr_chip[i].cfg;
5687 * ipr_probe_ioa - Allocates memory and does first stage of initialization
5688 * @pdev: PCI device struct
5689 * @dev_id: PCI device id struct
5692 * 0 on success / non-zero on failure
5694 static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
5695 const struct pci_device_id *dev_id)
5697 struct ipr_ioa_cfg *ioa_cfg;
5698 struct Scsi_Host *host;
5699 unsigned long ipr_regs_pci;
5700 void __iomem *ipr_regs;
5701 u32 rc = PCIBIOS_SUCCESSFUL;
5705 if ((rc = pci_enable_device(pdev))) {
5706 dev_err(&pdev->dev, "Cannot enable adapter\n");
5710 dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
5712 host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
5715 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
5720 ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
5721 memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
5723 ioa_cfg->chip_cfg = ipr_get_chip_cfg(dev_id);
5725 if (!ioa_cfg->chip_cfg) {
5726 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
5727 dev_id->vendor, dev_id->device);
5728 goto out_scsi_host_put;
5731 ipr_regs_pci = pci_resource_start(pdev, 0);
5733 rc = pci_request_regions(pdev, IPR_NAME);
5736 "Couldn't register memory range of registers\n");
5737 goto out_scsi_host_put;
5740 ipr_regs = ioremap(ipr_regs_pci, pci_resource_len(pdev, 0));
5744 "Couldn't map memory range of registers\n");
5746 goto out_release_regions;
5749 ioa_cfg->hdw_dma_regs = ipr_regs;
5750 ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
5751 ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
5753 ipr_init_ioa_cfg(ioa_cfg, host, pdev);
5755 pci_set_master(pdev);
5757 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5759 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
5763 rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
5764 ioa_cfg->chip_cfg->cache_line_size);
5766 if (rc != PCIBIOS_SUCCESSFUL) {
5767 dev_err(&pdev->dev, "Write of cache line size failed\n");
5772 /* Save away PCI config space for use following IOA reset */
5773 rc = pci_save_state(pdev);
5775 if (rc != PCIBIOS_SUCCESSFUL) {
5776 dev_err(&pdev->dev, "Failed to save PCI config space\n");
5781 if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
5784 if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
5787 rc = ipr_alloc_mem(ioa_cfg);
5790 "Couldn't allocate enough memory for device driver!\n");
5794 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
5795 rc = request_irq(pdev->irq, ipr_isr, SA_SHIRQ, IPR_NAME, ioa_cfg);
5798 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
5803 spin_lock(&ipr_driver_lock);
5804 list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
5805 spin_unlock(&ipr_driver_lock);
5812 ipr_free_mem(ioa_cfg);
5815 out_release_regions:
5816 pci_release_regions(pdev);
5818 scsi_host_put(host);
5820 pci_disable_device(pdev);
5825 * ipr_scan_vsets - Scans for VSET devices
5826 * @ioa_cfg: ioa config struct
5828 * Description: Since the VSET resources do not follow SAM in that we can have
5829 * sparse LUNs with no LUN 0, we have to scan for these ourselves.
5834 static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
5838 for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
5839 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
5840 scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
5844 * ipr_initiate_ioa_bringdown - Bring down an adapter
5845 * @ioa_cfg: ioa config struct
5846 * @shutdown_type: shutdown type
5848 * Description: This function will initiate bringing down the adapter.
5849 * This consists of issuing an IOA shutdown to the adapter
5850 * to flush the cache, and running BIST.
5851 * If the caller needs to wait on the completion of the reset,
5852 * the caller must sleep on the reset_wait_q.
5857 static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
5858 enum ipr_shutdown_type shutdown_type)
5861 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
5862 ioa_cfg->sdt_state = ABORT_DUMP;
5863 ioa_cfg->reset_retries = 0;
5864 ioa_cfg->in_ioa_bringdown = 1;
5865 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
5870 * __ipr_remove - Remove a single adapter
5871 * @pdev: pci device struct
5873 * Adapter hot plug remove entry point.
5878 static void __ipr_remove(struct pci_dev *pdev)
5880 unsigned long host_lock_flags = 0;
5881 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
5884 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5885 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
5887 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5888 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5889 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5891 spin_lock(&ipr_driver_lock);
5892 list_del(&ioa_cfg->queue);
5893 spin_unlock(&ipr_driver_lock);
5895 if (ioa_cfg->sdt_state == ABORT_DUMP)
5896 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
5897 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5899 ipr_free_all_resources(ioa_cfg);
5905 * ipr_remove - IOA hot plug remove entry point
5906 * @pdev: pci device struct
5908 * Adapter hot plug remove entry point.
5913 static void ipr_remove(struct pci_dev *pdev)
5915 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
5919 ioa_cfg->allow_cmds = 0;
5920 flush_scheduled_work();
5921 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5923 ipr_remove_dump_file(&ioa_cfg->host->shost_classdev.kobj,
5925 scsi_remove_host(ioa_cfg->host);
5933 * ipr_probe - Adapter hot plug add entry point
5936 * 0 on success / non-zero on failure
5938 static int __devinit ipr_probe(struct pci_dev *pdev,
5939 const struct pci_device_id *dev_id)
5941 struct ipr_ioa_cfg *ioa_cfg;
5944 rc = ipr_probe_ioa(pdev, dev_id);
5949 ioa_cfg = pci_get_drvdata(pdev);
5950 rc = ipr_probe_ioa_part2(ioa_cfg);
5957 rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
5964 rc = ipr_create_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5968 scsi_remove_host(ioa_cfg->host);
5973 rc = ipr_create_dump_file(&ioa_cfg->host->shost_classdev.kobj,
5977 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5979 scsi_remove_host(ioa_cfg->host);
5984 scsi_scan_host(ioa_cfg->host);
5985 ipr_scan_vsets(ioa_cfg);
5986 scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
5987 ioa_cfg->allow_ml_add_del = 1;
5988 schedule_work(&ioa_cfg->work_q);
5993 * ipr_shutdown - Shutdown handler.
5994 * @dev: device struct
5996 * This function is invoked upon system shutdown/reboot. It will issue
5997 * an adapter shutdown to the adapter to flush the write cache.
6002 static void ipr_shutdown(struct device *dev)
6004 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(to_pci_dev(dev));
6005 unsigned long lock_flags = 0;
6007 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6008 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
6009 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6010 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
6013 static struct pci_device_id ipr_pci_table[] __devinitdata = {
6014 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6015 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702,
6016 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6017 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6018 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703,
6019 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6020 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6021 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D,
6022 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6023 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6024 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E,
6025 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6026 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6027 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B,
6028 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6029 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6030 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E,
6031 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6032 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6033 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A,
6034 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6035 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
6036 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780,
6037 0, 0, (kernel_ulong_t)&ipr_chip_cfg[1] },
6038 { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
6039 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E,
6040 0, 0, (kernel_ulong_t)&ipr_chip_cfg[1] },
6043 MODULE_DEVICE_TABLE(pci, ipr_pci_table);
6045 static struct pci_driver ipr_driver = {
6047 .id_table = ipr_pci_table,
6049 .remove = ipr_remove,
6051 .shutdown = ipr_shutdown,
6056 * ipr_init - Module entry point
6059 * 0 on success / negative value on failure
6061 static int __init ipr_init(void)
6063 ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
6064 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
6066 return pci_module_init(&ipr_driver);
6070 * ipr_exit - Module unload
6072 * Module unload entry point.
6077 static void __exit ipr_exit(void)
6079 pci_unregister_driver(&ipr_driver);
6082 module_init(ipr_init);
6083 module_exit(ipr_exit);