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 DEFINE_SPINLOCK(ipr_driver_lock);
94 /* This table describes the differences between DMA controller chips */
95 static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
96 { /* Gemstone and Citrine */
98 .cache_line_size = 0x20,
100 .set_interrupt_mask_reg = 0x0022C,
101 .clr_interrupt_mask_reg = 0x00230,
102 .sense_interrupt_mask_reg = 0x0022C,
103 .clr_interrupt_reg = 0x00228,
104 .sense_interrupt_reg = 0x00224,
105 .ioarrin_reg = 0x00404,
106 .sense_uproc_interrupt_reg = 0x00214,
107 .set_uproc_interrupt_reg = 0x00214,
108 .clr_uproc_interrupt_reg = 0x00218
113 .cache_line_size = 0x20,
115 .set_interrupt_mask_reg = 0x00288,
116 .clr_interrupt_mask_reg = 0x0028C,
117 .sense_interrupt_mask_reg = 0x00288,
118 .clr_interrupt_reg = 0x00284,
119 .sense_interrupt_reg = 0x00280,
120 .ioarrin_reg = 0x00504,
121 .sense_uproc_interrupt_reg = 0x00290,
122 .set_uproc_interrupt_reg = 0x00290,
123 .clr_uproc_interrupt_reg = 0x00294
128 static int ipr_max_bus_speeds [] = {
129 IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
132 MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
133 MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
134 module_param_named(max_speed, ipr_max_speed, uint, 0);
135 MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
136 module_param_named(log_level, ipr_log_level, uint, 0);
137 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
138 module_param_named(testmode, ipr_testmode, int, 0);
139 MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
140 MODULE_LICENSE("GPL");
141 MODULE_VERSION(IPR_DRIVER_VERSION);
143 static const char *ipr_gpdd_dev_end_states[] = {
145 "Terminated by host",
146 "Terminated by device reset",
147 "Terminated by bus reset",
149 "Command not started"
152 static const char *ipr_gpdd_dev_bus_phases[] = {
166 /* A constant array of IOASCs/URCs/Error Messages */
168 struct ipr_error_table_t ipr_error_table[] = {
170 "8155: An unknown error was received"},
172 "Soft underlength error"},
174 "Command to be cancelled not found"},
176 "Qualified success"},
178 "FFFE: Soft device bus error recovered by the IOA"},
180 "FFF9: Device sector reassign successful"},
182 "FFF7: Media error recovered by device rewrite procedures"},
184 "7001: IOA sector reassignment successful"},
186 "FFF9: Soft media error. Sector reassignment recommended"},
188 "FFF7: Media error recovered by IOA rewrite procedures"},
190 "FF3D: Soft PCI bus error recovered by the IOA"},
192 "FFF6: Device hardware error recovered by the IOA"},
194 "FFF6: Device hardware error recovered by the device"},
196 "FF3D: Soft IOA error recovered by the IOA"},
198 "FFFA: Undefined device response recovered by the IOA"},
200 "FFF6: Device bus error, message or command phase"},
202 "FFF6: Failure prediction threshold exceeded"},
204 "8009: Impending cache battery pack failure"},
206 "34FF: Disk device format in progress"},
208 "Synchronization required"},
210 "No ready, IOA shutdown"},
212 "Not ready, IOA has been shutdown"},
214 "3020: Storage subsystem configuration error"},
216 "FFF5: Medium error, data unreadable, recommend reassign"},
218 "7000: Medium error, data unreadable, do not reassign"},
220 "FFF3: Disk media format bad"},
222 "3002: Addressed device failed to respond to selection"},
224 "3100: Device bus error"},
226 "3109: IOA timed out a device command"},
228 "3120: SCSI bus is not operational"},
230 "9000: IOA reserved area data check"},
232 "9001: IOA reserved area invalid data pattern"},
234 "9002: IOA reserved area LRC error"},
236 "102E: Out of alternate sectors for disk storage"},
238 "FFF4: Data transfer underlength error"},
240 "FFF4: Data transfer overlength error"},
242 "3400: Logical unit failure"},
244 "FFF4: Device microcode is corrupt"},
246 "8150: PCI bus error"},
248 "Unsupported device bus message received"},
250 "FFF4: Disk device problem"},
252 "8150: Permanent IOA failure"},
254 "3010: Disk device returned wrong response to IOA"},
256 "8151: IOA microcode error"},
258 "Device bus status error"},
260 "8157: IOA error requiring IOA reset to recover"},
262 "Message reject received from the device"},
264 "8008: A permanent cache battery pack failure occurred"},
266 "9090: Disk unit has been modified after the last known status"},
268 "9081: IOA detected device error"},
270 "9082: IOA detected device error"},
272 "3110: Device bus error, message or command phase"},
274 "9091: Incorrect hardware configuration change has been detected"},
276 "FFF4: Command to logical unit failed"},
278 "Illegal request, invalid request type or request packet"},
280 "Illegal request, invalid resource handle"},
282 "Illegal request, invalid field in parameter list"},
284 "Illegal request, parameter not supported"},
286 "Illegal request, parameter value invalid"},
288 "Illegal request, command sequence error"},
290 "9031: Array protection temporarily suspended, protection resuming"},
292 "9040: Array protection temporarily suspended, protection resuming"},
294 "FFFB: SCSI bus was reset"},
296 "FFFE: SCSI bus transition to single ended"},
298 "FFFE: SCSI bus transition to LVD"},
300 "FFFB: SCSI bus was reset by another initiator"},
302 "3029: A device replacement has occurred"},
304 "9051: IOA cache data exists for a missing or failed device"},
306 "9025: Disk unit is not supported at its physical location"},
308 "3020: IOA detected a SCSI bus configuration error"},
310 "3150: SCSI bus configuration error"},
312 "9041: Array protection temporarily suspended"},
314 "9030: Array no longer protected due to missing or failed disk unit"},
316 "9042: Corrupt array parity detected on specified device"},
318 "Failure due to other device"},
320 "9008: IOA does not support functions expected by devices"},
322 "9010: Cache data associated with attached devices cannot be found"},
324 "9011: Cache data belongs to devices other than those attached"},
326 "9020: Array missing 2 or more devices with only 1 device present"},
328 "9021: Array missing 2 or more devices with 2 or more devices present"},
330 "9022: Exposed array is missing a required device"},
332 "9023: Array member(s) not at required physical locations"},
334 "9024: Array not functional due to present hardware configuration"},
336 "9026: Array not functional due to present hardware configuration"},
338 "9027: Array is missing a device and parity is out of sync"},
340 "9028: Maximum number of arrays already exist"},
342 "9050: Required cache data cannot be located for a disk unit"},
344 "9052: Cache data exists for a device that has been modified"},
346 "9054: IOA resources not available due to previous problems"},
348 "9092: Disk unit requires initialization before use"},
350 "9029: Incorrect hardware configuration change has been detected"},
352 "9060: One or more disk pairs are missing from an array"},
354 "9061: One or more disks are missing from an array"},
356 "9062: One or more disks are missing from an array"},
358 "9063: Maximum number of functional arrays has been exceeded"},
360 "Aborted command, invalid descriptor"},
362 "Command terminated by host"}
365 static const struct ipr_ses_table_entry ipr_ses_table[] = {
366 { "2104-DL1 ", "XXXXXXXXXXXXXXXX", 80 },
367 { "2104-TL1 ", "XXXXXXXXXXXXXXXX", 80 },
368 { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
369 { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
370 { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
371 { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
372 { "2104-DU3 ", "XXXXXXXXXXXXXXXX", 160 },
373 { "2104-TU3 ", "XXXXXXXXXXXXXXXX", 160 },
374 { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
375 { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
376 { "St V1S2 ", "XXXXXXXXXXXXXXXX", 160 },
377 { "HSBPD4M PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
378 { "VSBPD1H U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
382 * Function Prototypes
384 static int ipr_reset_alert(struct ipr_cmnd *);
385 static void ipr_process_ccn(struct ipr_cmnd *);
386 static void ipr_process_error(struct ipr_cmnd *);
387 static void ipr_reset_ioa_job(struct ipr_cmnd *);
388 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
389 enum ipr_shutdown_type);
391 #ifdef CONFIG_SCSI_IPR_TRACE
393 * ipr_trc_hook - Add a trace entry to the driver trace
394 * @ipr_cmd: ipr command struct
396 * @add_data: additional data
401 static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
402 u8 type, u32 add_data)
404 struct ipr_trace_entry *trace_entry;
405 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
407 trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
408 trace_entry->time = jiffies;
409 trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
410 trace_entry->type = type;
411 trace_entry->cmd_index = ipr_cmd->cmd_index;
412 trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
413 trace_entry->u.add_data = add_data;
416 #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
420 * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
421 * @ipr_cmd: ipr command struct
426 static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
428 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
429 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
431 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
432 ioarcb->write_data_transfer_length = 0;
433 ioarcb->read_data_transfer_length = 0;
434 ioarcb->write_ioadl_len = 0;
435 ioarcb->read_ioadl_len = 0;
437 ioasa->residual_data_len = 0;
439 ipr_cmd->scsi_cmd = NULL;
440 ipr_cmd->sense_buffer[0] = 0;
441 ipr_cmd->dma_use_sg = 0;
445 * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
446 * @ipr_cmd: ipr command struct
451 static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
453 ipr_reinit_ipr_cmnd(ipr_cmd);
454 ipr_cmd->u.scratch = 0;
455 ipr_cmd->sibling = NULL;
456 init_timer(&ipr_cmd->timer);
460 * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
461 * @ioa_cfg: ioa config struct
464 * pointer to ipr command struct
467 struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
469 struct ipr_cmnd *ipr_cmd;
471 ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
472 list_del(&ipr_cmd->queue);
473 ipr_init_ipr_cmnd(ipr_cmd);
479 * ipr_unmap_sglist - Unmap scatterlist if mapped
480 * @ioa_cfg: ioa config struct
481 * @ipr_cmd: ipr command struct
486 static void ipr_unmap_sglist(struct ipr_ioa_cfg *ioa_cfg,
487 struct ipr_cmnd *ipr_cmd)
489 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
491 if (ipr_cmd->dma_use_sg) {
492 if (scsi_cmd->use_sg > 0) {
493 pci_unmap_sg(ioa_cfg->pdev, scsi_cmd->request_buffer,
495 scsi_cmd->sc_data_direction);
497 pci_unmap_single(ioa_cfg->pdev, ipr_cmd->dma_handle,
498 scsi_cmd->request_bufflen,
499 scsi_cmd->sc_data_direction);
505 * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
506 * @ioa_cfg: ioa config struct
507 * @clr_ints: interrupts to clear
509 * This function masks all interrupts on the adapter, then clears the
510 * interrupts specified in the mask
515 static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
518 volatile u32 int_reg;
520 /* Stop new interrupts */
521 ioa_cfg->allow_interrupts = 0;
523 /* Set interrupt mask to stop all new interrupts */
524 writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
526 /* Clear any pending interrupts */
527 writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
528 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
532 * ipr_save_pcix_cmd_reg - Save PCI-X command register
533 * @ioa_cfg: ioa config struct
536 * 0 on success / -EIO on failure
538 static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
540 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
542 if (pcix_cmd_reg == 0) {
543 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
547 if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
548 &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
549 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
553 ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
558 * ipr_set_pcix_cmd_reg - Setup PCI-X command register
559 * @ioa_cfg: ioa config struct
562 * 0 on success / -EIO on failure
564 static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
566 int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
569 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
570 ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
571 dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
575 dev_err(&ioa_cfg->pdev->dev,
576 "Failed to setup PCI-X command register\n");
584 * ipr_scsi_eh_done - mid-layer done function for aborted ops
585 * @ipr_cmd: ipr command struct
587 * This function is invoked by the interrupt handler for
588 * ops generated by the SCSI mid-layer which are being aborted.
593 static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
595 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
596 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
598 scsi_cmd->result |= (DID_ERROR << 16);
600 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
601 scsi_cmd->scsi_done(scsi_cmd);
602 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
606 * ipr_fail_all_ops - Fails all outstanding ops.
607 * @ioa_cfg: ioa config struct
609 * This function fails all outstanding ops.
614 static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
616 struct ipr_cmnd *ipr_cmd, *temp;
619 list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
620 list_del(&ipr_cmd->queue);
622 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
623 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
625 if (ipr_cmd->scsi_cmd)
626 ipr_cmd->done = ipr_scsi_eh_done;
628 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
629 del_timer(&ipr_cmd->timer);
630 ipr_cmd->done(ipr_cmd);
637 * ipr_do_req - Send driver initiated requests.
638 * @ipr_cmd: ipr command struct
639 * @done: done function
640 * @timeout_func: timeout function
641 * @timeout: timeout value
643 * This function sends the specified command to the adapter with the
644 * timeout given. The done function is invoked on command completion.
649 static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
650 void (*done) (struct ipr_cmnd *),
651 void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
653 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
655 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
657 ipr_cmd->done = done;
659 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
660 ipr_cmd->timer.expires = jiffies + timeout;
661 ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
663 add_timer(&ipr_cmd->timer);
665 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
668 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
669 ioa_cfg->regs.ioarrin_reg);
673 * ipr_internal_cmd_done - Op done function for an internally generated op.
674 * @ipr_cmd: ipr command struct
676 * This function is the op done function for an internally generated,
677 * blocking op. It simply wakes the sleeping thread.
682 static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
684 if (ipr_cmd->sibling)
685 ipr_cmd->sibling = NULL;
687 complete(&ipr_cmd->completion);
691 * ipr_send_blocking_cmd - Send command and sleep on its completion.
692 * @ipr_cmd: ipr command struct
693 * @timeout_func: function to invoke if command times out
699 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
700 void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
703 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
705 init_completion(&ipr_cmd->completion);
706 ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
708 spin_unlock_irq(ioa_cfg->host->host_lock);
709 wait_for_completion(&ipr_cmd->completion);
710 spin_lock_irq(ioa_cfg->host->host_lock);
714 * ipr_send_hcam - Send an HCAM to the adapter.
715 * @ioa_cfg: ioa config struct
717 * @hostrcb: hostrcb struct
719 * This function will send a Host Controlled Async command to the adapter.
720 * If HCAMs are currently not allowed to be issued to the adapter, it will
721 * place the hostrcb on the free queue.
726 static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
727 struct ipr_hostrcb *hostrcb)
729 struct ipr_cmnd *ipr_cmd;
730 struct ipr_ioarcb *ioarcb;
732 if (ioa_cfg->allow_cmds) {
733 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
734 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
735 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
737 ipr_cmd->u.hostrcb = hostrcb;
738 ioarcb = &ipr_cmd->ioarcb;
740 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
741 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
742 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
743 ioarcb->cmd_pkt.cdb[1] = type;
744 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
745 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
747 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
748 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
749 ipr_cmd->ioadl[0].flags_and_data_len =
750 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
751 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
753 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
754 ipr_cmd->done = ipr_process_ccn;
756 ipr_cmd->done = ipr_process_error;
758 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
761 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
762 ioa_cfg->regs.ioarrin_reg);
764 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
769 * ipr_init_res_entry - Initialize a resource entry struct.
770 * @res: resource entry struct
775 static void ipr_init_res_entry(struct ipr_resource_entry *res)
777 res->needs_sync_complete = 1;
780 res->del_from_ml = 0;
781 res->resetting_device = 0;
783 res->qdepth = IPR_MAX_CMD_PER_LUN;
788 * ipr_handle_config_change - Handle a config change from the adapter
789 * @ioa_cfg: ioa config struct
795 static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
796 struct ipr_hostrcb *hostrcb)
798 struct ipr_resource_entry *res = NULL;
799 struct ipr_config_table_entry *cfgte;
802 cfgte = &hostrcb->hcam.u.ccn.cfgte;
804 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
805 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
806 sizeof(cfgte->res_addr))) {
813 if (list_empty(&ioa_cfg->free_res_q)) {
814 ipr_send_hcam(ioa_cfg,
815 IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
820 res = list_entry(ioa_cfg->free_res_q.next,
821 struct ipr_resource_entry, queue);
823 list_del(&res->queue);
824 ipr_init_res_entry(res);
825 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
828 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
830 if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
832 res->sdev->hostdata = NULL;
833 res->del_from_ml = 1;
834 if (ioa_cfg->allow_ml_add_del)
835 schedule_work(&ioa_cfg->work_q);
837 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
838 } else if (!res->sdev) {
840 if (ioa_cfg->allow_ml_add_del)
841 schedule_work(&ioa_cfg->work_q);
844 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
848 * ipr_process_ccn - Op done function for a CCN.
849 * @ipr_cmd: ipr command struct
851 * This function is the op done function for a configuration
852 * change notification host controlled async from the adapter.
857 static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
859 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
860 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
861 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
863 list_del(&hostrcb->queue);
864 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
867 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
868 dev_err(&ioa_cfg->pdev->dev,
869 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
871 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
873 ipr_handle_config_change(ioa_cfg, hostrcb);
878 * ipr_log_vpd - Log the passed VPD to the error log.
879 * @vpids: vendor/product id struct
880 * @serial_num: serial number string
885 static void ipr_log_vpd(struct ipr_std_inq_vpids *vpids, u8 *serial_num)
887 char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
888 + IPR_SERIAL_NUM_LEN];
890 memcpy(buffer, vpids->vendor_id, IPR_VENDOR_ID_LEN);
891 memcpy(buffer + IPR_VENDOR_ID_LEN, vpids->product_id,
893 buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
894 ipr_err("Vendor/Product ID: %s\n", buffer);
896 memcpy(buffer, serial_num, IPR_SERIAL_NUM_LEN);
897 buffer[IPR_SERIAL_NUM_LEN] = '\0';
898 ipr_err(" Serial Number: %s\n", buffer);
902 * ipr_log_cache_error - Log a cache error.
903 * @ioa_cfg: ioa config struct
904 * @hostrcb: hostrcb struct
909 static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
910 struct ipr_hostrcb *hostrcb)
912 struct ipr_hostrcb_type_02_error *error =
913 &hostrcb->hcam.u.error.u.type_02_error;
915 ipr_err("-----Current Configuration-----\n");
916 ipr_err("Cache Directory Card Information:\n");
917 ipr_log_vpd(&error->ioa_vpids, error->ioa_sn);
918 ipr_err("Adapter Card Information:\n");
919 ipr_log_vpd(&error->cfc_vpids, error->cfc_sn);
921 ipr_err("-----Expected Configuration-----\n");
922 ipr_err("Cache Directory Card Information:\n");
923 ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpids,
924 error->ioa_last_attached_to_cfc_sn);
925 ipr_err("Adapter Card Information:\n");
926 ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpids,
927 error->cfc_last_attached_to_ioa_sn);
929 ipr_err("Additional IOA Data: %08X %08X %08X\n",
930 be32_to_cpu(error->ioa_data[0]),
931 be32_to_cpu(error->ioa_data[1]),
932 be32_to_cpu(error->ioa_data[2]));
936 * ipr_log_config_error - Log a configuration error.
937 * @ioa_cfg: ioa config struct
938 * @hostrcb: hostrcb struct
943 static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
944 struct ipr_hostrcb *hostrcb)
946 int errors_logged, i;
947 struct ipr_hostrcb_device_data_entry *dev_entry;
948 struct ipr_hostrcb_type_03_error *error;
950 error = &hostrcb->hcam.u.error.u.type_03_error;
951 errors_logged = be32_to_cpu(error->errors_logged);
953 ipr_err("Device Errors Detected/Logged: %d/%d\n",
954 be32_to_cpu(error->errors_detected), errors_logged);
956 dev_entry = error->dev_entry;
958 for (i = 0; i < errors_logged; i++, dev_entry++) {
961 if (dev_entry->dev_res_addr.bus >= IPR_MAX_NUM_BUSES) {
962 ipr_err("Device %d: missing\n", i + 1);
964 ipr_err("Device %d: %d:%d:%d:%d\n", i + 1,
965 ioa_cfg->host->host_no, dev_entry->dev_res_addr.bus,
966 dev_entry->dev_res_addr.target, dev_entry->dev_res_addr.lun);
968 ipr_log_vpd(&dev_entry->dev_vpids, dev_entry->dev_sn);
970 ipr_err("-----New Device Information-----\n");
971 ipr_log_vpd(&dev_entry->new_dev_vpids, dev_entry->new_dev_sn);
973 ipr_err("Cache Directory Card Information:\n");
974 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpids,
975 dev_entry->ioa_last_with_dev_sn);
977 ipr_err("Adapter Card Information:\n");
978 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpids,
979 dev_entry->cfc_last_with_dev_sn);
981 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
982 be32_to_cpu(dev_entry->ioa_data[0]),
983 be32_to_cpu(dev_entry->ioa_data[1]),
984 be32_to_cpu(dev_entry->ioa_data[2]),
985 be32_to_cpu(dev_entry->ioa_data[3]),
986 be32_to_cpu(dev_entry->ioa_data[4]));
991 * ipr_log_array_error - Log an array configuration error.
992 * @ioa_cfg: ioa config struct
993 * @hostrcb: hostrcb struct
998 static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
999 struct ipr_hostrcb *hostrcb)
1002 struct ipr_hostrcb_type_04_error *error;
1003 struct ipr_hostrcb_array_data_entry *array_entry;
1004 u8 zero_sn[IPR_SERIAL_NUM_LEN];
1006 memset(zero_sn, '0', IPR_SERIAL_NUM_LEN);
1008 error = &hostrcb->hcam.u.error.u.type_04_error;
1012 ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1013 error->protection_level,
1014 ioa_cfg->host->host_no,
1015 error->last_func_vset_res_addr.bus,
1016 error->last_func_vset_res_addr.target,
1017 error->last_func_vset_res_addr.lun);
1021 array_entry = error->array_member;
1023 for (i = 0; i < 18; i++) {
1024 if (!memcmp(array_entry->serial_num, zero_sn, IPR_SERIAL_NUM_LEN))
1027 if (error->exposed_mode_adn == i) {
1028 ipr_err("Exposed Array Member %d:\n", i);
1030 ipr_err("Array Member %d:\n", i);
1033 ipr_log_vpd(&array_entry->vpids, array_entry->serial_num);
1035 if (array_entry->dev_res_addr.bus >= IPR_MAX_NUM_BUSES) {
1036 ipr_err("Current Location: unknown\n");
1038 ipr_err("Current Location: %d:%d:%d:%d\n",
1039 ioa_cfg->host->host_no,
1040 array_entry->dev_res_addr.bus,
1041 array_entry->dev_res_addr.target,
1042 array_entry->dev_res_addr.lun);
1045 if (array_entry->dev_res_addr.bus >= IPR_MAX_NUM_BUSES) {
1046 ipr_err("Expected Location: unknown\n");
1048 ipr_err("Expected Location: %d:%d:%d:%d\n",
1049 ioa_cfg->host->host_no,
1050 array_entry->expected_dev_res_addr.bus,
1051 array_entry->expected_dev_res_addr.target,
1052 array_entry->expected_dev_res_addr.lun);
1058 array_entry = error->array_member2;
1065 * ipr_log_generic_error - Log an adapter error.
1066 * @ioa_cfg: ioa config struct
1067 * @hostrcb: hostrcb struct
1072 static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1073 struct ipr_hostrcb *hostrcb)
1076 int ioa_data_len = be32_to_cpu(hostrcb->hcam.length);
1078 if (ioa_data_len == 0)
1081 ipr_err("IOA Error Data:\n");
1082 ipr_err("Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
1084 for (i = 0; i < ioa_data_len / 4; i += 4) {
1085 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1086 be32_to_cpu(hostrcb->hcam.u.raw.data[i]),
1087 be32_to_cpu(hostrcb->hcam.u.raw.data[i+1]),
1088 be32_to_cpu(hostrcb->hcam.u.raw.data[i+2]),
1089 be32_to_cpu(hostrcb->hcam.u.raw.data[i+3]));
1094 * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1097 * This function will return the index of into the ipr_error_table
1098 * for the specified IOASC. If the IOASC is not in the table,
1099 * 0 will be returned, which points to the entry used for unknown errors.
1102 * index into the ipr_error_table
1104 static u32 ipr_get_error(u32 ioasc)
1108 for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
1109 if (ipr_error_table[i].ioasc == ioasc)
1116 * ipr_handle_log_data - Log an adapter error.
1117 * @ioa_cfg: ioa config struct
1118 * @hostrcb: hostrcb struct
1120 * This function logs an adapter error to the system.
1125 static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1126 struct ipr_hostrcb *hostrcb)
1131 if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1134 if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1135 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1137 ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1139 if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1140 ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1141 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1142 scsi_report_bus_reset(ioa_cfg->host,
1143 hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1146 error_index = ipr_get_error(ioasc);
1148 if (!ipr_error_table[error_index].log_hcam)
1151 if (ipr_is_device(&hostrcb->hcam.u.error.failing_dev_res_addr)) {
1152 ipr_res_err(ioa_cfg, hostrcb->hcam.u.error.failing_dev_res_addr,
1153 "%s\n", ipr_error_table[error_index].error);
1155 dev_err(&ioa_cfg->pdev->dev, "%s\n",
1156 ipr_error_table[error_index].error);
1159 /* Set indication we have logged an error */
1160 ioa_cfg->errors_logged++;
1162 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
1165 switch (hostrcb->hcam.overlay_id) {
1166 case IPR_HOST_RCB_OVERLAY_ID_1:
1167 ipr_log_generic_error(ioa_cfg, hostrcb);
1169 case IPR_HOST_RCB_OVERLAY_ID_2:
1170 ipr_log_cache_error(ioa_cfg, hostrcb);
1172 case IPR_HOST_RCB_OVERLAY_ID_3:
1173 ipr_log_config_error(ioa_cfg, hostrcb);
1175 case IPR_HOST_RCB_OVERLAY_ID_4:
1176 case IPR_HOST_RCB_OVERLAY_ID_6:
1177 ipr_log_array_error(ioa_cfg, hostrcb);
1179 case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1180 ipr_log_generic_error(ioa_cfg, hostrcb);
1183 dev_err(&ioa_cfg->pdev->dev,
1184 "Unknown error received. Overlay ID: %d\n",
1185 hostrcb->hcam.overlay_id);
1191 * ipr_process_error - Op done function for an adapter error log.
1192 * @ipr_cmd: ipr command struct
1194 * This function is the op done function for an error log host
1195 * controlled async from the adapter. It will log the error and
1196 * send the HCAM back to the adapter.
1201 static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1203 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1204 struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1205 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
1207 list_del(&hostrcb->queue);
1208 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1211 ipr_handle_log_data(ioa_cfg, hostrcb);
1212 } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1213 dev_err(&ioa_cfg->pdev->dev,
1214 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1217 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1221 * ipr_timeout - An internally generated op has timed out.
1222 * @ipr_cmd: ipr command struct
1224 * This function blocks host requests and initiates an
1230 static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1232 unsigned long lock_flags = 0;
1233 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1236 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1238 ioa_cfg->errors_logged++;
1239 dev_err(&ioa_cfg->pdev->dev,
1240 "Adapter being reset due to command timeout.\n");
1242 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1243 ioa_cfg->sdt_state = GET_DUMP;
1245 if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1246 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1248 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1253 * ipr_reset_reload - Reset/Reload the IOA
1254 * @ioa_cfg: ioa config struct
1255 * @shutdown_type: shutdown type
1257 * This function resets the adapter and re-initializes it.
1258 * This function assumes that all new host commands have been stopped.
1262 static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1263 enum ipr_shutdown_type shutdown_type)
1265 if (!ioa_cfg->in_reset_reload)
1266 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1268 spin_unlock_irq(ioa_cfg->host->host_lock);
1269 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1270 spin_lock_irq(ioa_cfg->host->host_lock);
1272 /* If we got hit with a host reset while we were already resetting
1273 the adapter for some reason, and the reset failed. */
1274 if (ioa_cfg->ioa_is_dead) {
1283 * ipr_find_ses_entry - Find matching SES in SES table
1284 * @res: resource entry struct of SES
1287 * pointer to SES table entry / NULL on failure
1289 static const struct ipr_ses_table_entry *
1290 ipr_find_ses_entry(struct ipr_resource_entry *res)
1293 const struct ipr_ses_table_entry *ste = ipr_ses_table;
1295 for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1296 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1297 if (ste->compare_product_id_byte[j] == 'X') {
1298 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1306 if (matches == IPR_PROD_ID_LEN)
1314 * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1315 * @ioa_cfg: ioa config struct
1317 * @bus_width: bus width
1320 * SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1321 * For a 2-byte wide SCSI bus, the maximum transfer speed is
1322 * twice the maximum transfer rate (e.g. for a wide enabled bus,
1323 * max 160MHz = max 320MB/sec).
1325 static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1327 struct ipr_resource_entry *res;
1328 const struct ipr_ses_table_entry *ste;
1329 u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1331 /* Loop through each config table entry in the config table buffer */
1332 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1333 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1336 if (bus != res->cfgte.res_addr.bus)
1339 if (!(ste = ipr_find_ses_entry(res)))
1342 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1345 return max_xfer_rate;
1349 * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1350 * @ioa_cfg: ioa config struct
1351 * @max_delay: max delay in micro-seconds to wait
1353 * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1356 * 0 on success / other on failure
1358 static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1360 volatile u32 pcii_reg;
1363 /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1364 while (delay < max_delay) {
1365 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1367 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1370 /* udelay cannot be used if delay is more than a few milliseconds */
1371 if ((delay / 1000) > MAX_UDELAY_MS)
1372 mdelay(delay / 1000);
1382 * ipr_get_ldump_data_section - Dump IOA memory
1383 * @ioa_cfg: ioa config struct
1384 * @start_addr: adapter address to dump
1385 * @dest: destination kernel buffer
1386 * @length_in_words: length to dump in 4 byte words
1389 * 0 on success / -EIO on failure
1391 static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1393 u32 *dest, u32 length_in_words)
1395 volatile u32 temp_pcii_reg;
1398 /* Write IOA interrupt reg starting LDUMP state */
1399 writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1400 ioa_cfg->regs.set_uproc_interrupt_reg);
1402 /* Wait for IO debug acknowledge */
1403 if (ipr_wait_iodbg_ack(ioa_cfg,
1404 IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1405 dev_err(&ioa_cfg->pdev->dev,
1406 "IOA dump long data transfer timeout\n");
1410 /* Signal LDUMP interlocked - clear IO debug ack */
1411 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1412 ioa_cfg->regs.clr_interrupt_reg);
1414 /* Write Mailbox with starting address */
1415 writel(start_addr, ioa_cfg->ioa_mailbox);
1417 /* Signal address valid - clear IOA Reset alert */
1418 writel(IPR_UPROCI_RESET_ALERT,
1419 ioa_cfg->regs.clr_uproc_interrupt_reg);
1421 for (i = 0; i < length_in_words; i++) {
1422 /* Wait for IO debug acknowledge */
1423 if (ipr_wait_iodbg_ack(ioa_cfg,
1424 IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1425 dev_err(&ioa_cfg->pdev->dev,
1426 "IOA dump short data transfer timeout\n");
1430 /* Read data from mailbox and increment destination pointer */
1431 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1434 /* For all but the last word of data, signal data received */
1435 if (i < (length_in_words - 1)) {
1436 /* Signal dump data received - Clear IO debug Ack */
1437 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1438 ioa_cfg->regs.clr_interrupt_reg);
1442 /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1443 writel(IPR_UPROCI_RESET_ALERT,
1444 ioa_cfg->regs.set_uproc_interrupt_reg);
1446 writel(IPR_UPROCI_IO_DEBUG_ALERT,
1447 ioa_cfg->regs.clr_uproc_interrupt_reg);
1449 /* Signal dump data received - Clear IO debug Ack */
1450 writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1451 ioa_cfg->regs.clr_interrupt_reg);
1453 /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
1454 while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
1456 readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
1458 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
1468 #ifdef CONFIG_SCSI_IPR_DUMP
1470 * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
1471 * @ioa_cfg: ioa config struct
1472 * @pci_address: adapter address
1473 * @length: length of data to copy
1475 * Copy data from PCI adapter to kernel buffer.
1476 * Note: length MUST be a 4 byte multiple
1478 * 0 on success / other on failure
1480 static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
1481 unsigned long pci_address, u32 length)
1483 int bytes_copied = 0;
1484 int cur_len, rc, rem_len, rem_page_len;
1486 unsigned long lock_flags = 0;
1487 struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
1489 while (bytes_copied < length &&
1490 (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
1491 if (ioa_dump->page_offset >= PAGE_SIZE ||
1492 ioa_dump->page_offset == 0) {
1493 page = (u32 *)__get_free_page(GFP_ATOMIC);
1497 return bytes_copied;
1500 ioa_dump->page_offset = 0;
1501 ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
1502 ioa_dump->next_page_index++;
1504 page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
1506 rem_len = length - bytes_copied;
1507 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
1508 cur_len = min(rem_len, rem_page_len);
1510 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1511 if (ioa_cfg->sdt_state == ABORT_DUMP) {
1514 rc = ipr_get_ldump_data_section(ioa_cfg,
1515 pci_address + bytes_copied,
1516 &page[ioa_dump->page_offset / 4],
1517 (cur_len / sizeof(u32)));
1519 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1522 ioa_dump->page_offset += cur_len;
1523 bytes_copied += cur_len;
1531 return bytes_copied;
1535 * ipr_init_dump_entry_hdr - Initialize a dump entry header.
1536 * @hdr: dump entry header struct
1541 static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
1543 hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
1545 hdr->offset = sizeof(*hdr);
1546 hdr->status = IPR_DUMP_STATUS_SUCCESS;
1550 * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
1551 * @ioa_cfg: ioa config struct
1552 * @driver_dump: driver dump struct
1557 static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
1558 struct ipr_driver_dump *driver_dump)
1560 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
1562 ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
1563 driver_dump->ioa_type_entry.hdr.len =
1564 sizeof(struct ipr_dump_ioa_type_entry) -
1565 sizeof(struct ipr_dump_entry_header);
1566 driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1567 driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
1568 driver_dump->ioa_type_entry.type = ioa_cfg->type;
1569 driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
1570 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
1571 ucode_vpd->minor_release[1];
1572 driver_dump->hdr.num_entries++;
1576 * ipr_dump_version_data - Fill in the driver version in the dump.
1577 * @ioa_cfg: ioa config struct
1578 * @driver_dump: driver dump struct
1583 static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
1584 struct ipr_driver_dump *driver_dump)
1586 ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
1587 driver_dump->version_entry.hdr.len =
1588 sizeof(struct ipr_dump_version_entry) -
1589 sizeof(struct ipr_dump_entry_header);
1590 driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
1591 driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
1592 strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
1593 driver_dump->hdr.num_entries++;
1597 * ipr_dump_trace_data - Fill in the IOA trace in the dump.
1598 * @ioa_cfg: ioa config struct
1599 * @driver_dump: driver dump struct
1604 static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
1605 struct ipr_driver_dump *driver_dump)
1607 ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
1608 driver_dump->trace_entry.hdr.len =
1609 sizeof(struct ipr_dump_trace_entry) -
1610 sizeof(struct ipr_dump_entry_header);
1611 driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1612 driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
1613 memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
1614 driver_dump->hdr.num_entries++;
1618 * ipr_dump_location_data - Fill in the IOA location in the dump.
1619 * @ioa_cfg: ioa config struct
1620 * @driver_dump: driver dump struct
1625 static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
1626 struct ipr_driver_dump *driver_dump)
1628 ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
1629 driver_dump->location_entry.hdr.len =
1630 sizeof(struct ipr_dump_location_entry) -
1631 sizeof(struct ipr_dump_entry_header);
1632 driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
1633 driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
1634 strcpy(driver_dump->location_entry.location, ioa_cfg->pdev->dev.bus_id);
1635 driver_dump->hdr.num_entries++;
1639 * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
1640 * @ioa_cfg: ioa config struct
1641 * @dump: dump struct
1646 static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
1648 unsigned long start_addr, sdt_word;
1649 unsigned long lock_flags = 0;
1650 struct ipr_driver_dump *driver_dump = &dump->driver_dump;
1651 struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
1652 u32 num_entries, start_off, end_off;
1653 u32 bytes_to_copy, bytes_copied, rc;
1654 struct ipr_sdt *sdt;
1659 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1661 if (ioa_cfg->sdt_state != GET_DUMP) {
1662 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1666 start_addr = readl(ioa_cfg->ioa_mailbox);
1668 if (!ipr_sdt_is_fmt2(start_addr)) {
1669 dev_err(&ioa_cfg->pdev->dev,
1670 "Invalid dump table format: %lx\n", start_addr);
1671 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1675 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
1677 driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
1679 /* Initialize the overall dump header */
1680 driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
1681 driver_dump->hdr.num_entries = 1;
1682 driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
1683 driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
1684 driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
1685 driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
1687 ipr_dump_version_data(ioa_cfg, driver_dump);
1688 ipr_dump_location_data(ioa_cfg, driver_dump);
1689 ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
1690 ipr_dump_trace_data(ioa_cfg, driver_dump);
1692 /* Update dump_header */
1693 driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
1695 /* IOA Dump entry */
1696 ipr_init_dump_entry_hdr(&ioa_dump->hdr);
1697 ioa_dump->format = IPR_SDT_FMT2;
1698 ioa_dump->hdr.len = 0;
1699 ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1700 ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
1702 /* First entries in sdt are actually a list of dump addresses and
1703 lengths to gather the real dump data. sdt represents the pointer
1704 to the ioa generated dump table. Dump data will be extracted based
1705 on entries in this table */
1706 sdt = &ioa_dump->sdt;
1708 rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (u32 *)sdt,
1709 sizeof(struct ipr_sdt) / sizeof(u32));
1711 /* Smart Dump table is ready to use and the first entry is valid */
1712 if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
1713 dev_err(&ioa_cfg->pdev->dev,
1714 "Dump of IOA failed. Dump table not valid: %d, %X.\n",
1715 rc, be32_to_cpu(sdt->hdr.state));
1716 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
1717 ioa_cfg->sdt_state = DUMP_OBTAINED;
1718 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1722 num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
1724 if (num_entries > IPR_NUM_SDT_ENTRIES)
1725 num_entries = IPR_NUM_SDT_ENTRIES;
1727 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1729 for (i = 0; i < num_entries; i++) {
1730 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
1731 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1735 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
1736 sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
1737 start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
1738 end_off = be32_to_cpu(sdt->entry[i].end_offset);
1740 if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
1741 bytes_to_copy = end_off - start_off;
1742 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
1743 sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
1747 /* Copy data from adapter to driver buffers */
1748 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
1751 ioa_dump->hdr.len += bytes_copied;
1753 if (bytes_copied != bytes_to_copy) {
1754 driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1761 dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
1763 /* Update dump_header */
1764 driver_dump->hdr.len += ioa_dump->hdr.len;
1766 ioa_cfg->sdt_state = DUMP_OBTAINED;
1771 #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
1775 * ipr_release_dump - Free adapter dump memory
1776 * @kref: kref struct
1781 static void ipr_release_dump(struct kref *kref)
1783 struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
1784 struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
1785 unsigned long lock_flags = 0;
1789 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1790 ioa_cfg->dump = NULL;
1791 ioa_cfg->sdt_state = INACTIVE;
1792 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1794 for (i = 0; i < dump->ioa_dump.next_page_index; i++)
1795 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
1802 * ipr_worker_thread - Worker thread
1803 * @data: ioa config struct
1805 * Called at task level from a work thread. This function takes care
1806 * of adding and removing device from the mid-layer as configuration
1807 * changes are detected by the adapter.
1812 static void ipr_worker_thread(void *data)
1814 unsigned long lock_flags;
1815 struct ipr_resource_entry *res;
1816 struct scsi_device *sdev;
1817 struct ipr_dump *dump;
1818 struct ipr_ioa_cfg *ioa_cfg = data;
1819 u8 bus, target, lun;
1823 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1825 if (ioa_cfg->sdt_state == GET_DUMP) {
1826 dump = ioa_cfg->dump;
1828 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1831 kref_get(&dump->kref);
1832 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1833 ipr_get_ioa_dump(ioa_cfg, dump);
1834 kref_put(&dump->kref, ipr_release_dump);
1836 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1837 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
1838 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1839 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1846 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
1847 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1851 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1852 if (res->del_from_ml && res->sdev) {
1855 if (!scsi_device_get(sdev)) {
1857 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
1858 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1859 scsi_remove_device(sdev);
1860 scsi_device_put(sdev);
1861 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1868 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1869 if (res->add_to_ml) {
1870 bus = res->cfgte.res_addr.bus;
1871 target = res->cfgte.res_addr.target;
1872 lun = res->cfgte.res_addr.lun;
1873 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1874 scsi_add_device(ioa_cfg->host, bus, target, lun);
1875 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1880 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1884 #ifdef CONFIG_SCSI_IPR_TRACE
1886 * ipr_read_trace - Dump the adapter trace
1887 * @kobj: kobject struct
1890 * @count: buffer size
1893 * number of bytes printed to buffer
1895 static ssize_t ipr_read_trace(struct kobject *kobj, char *buf,
1896 loff_t off, size_t count)
1898 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
1899 struct Scsi_Host *shost = class_to_shost(cdev);
1900 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1901 unsigned long lock_flags = 0;
1902 int size = IPR_TRACE_SIZE;
1903 char *src = (char *)ioa_cfg->trace;
1907 if (off + count > size) {
1912 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1913 memcpy(buf, &src[off], count);
1914 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1918 static struct bin_attribute ipr_trace_attr = {
1924 .read = ipr_read_trace,
1929 * ipr_show_fw_version - Show the firmware version
1930 * @class_dev: class device struct
1934 * number of bytes printed to buffer
1936 static ssize_t ipr_show_fw_version(struct class_device *class_dev, char *buf)
1938 struct Scsi_Host *shost = class_to_shost(class_dev);
1939 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1940 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
1941 unsigned long lock_flags = 0;
1944 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1945 len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
1946 ucode_vpd->major_release, ucode_vpd->card_type,
1947 ucode_vpd->minor_release[0],
1948 ucode_vpd->minor_release[1]);
1949 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1953 static struct class_device_attribute ipr_fw_version_attr = {
1955 .name = "fw_version",
1958 .show = ipr_show_fw_version,
1962 * ipr_show_log_level - Show the adapter's error logging level
1963 * @class_dev: class device struct
1967 * number of bytes printed to buffer
1969 static ssize_t ipr_show_log_level(struct class_device *class_dev, char *buf)
1971 struct Scsi_Host *shost = class_to_shost(class_dev);
1972 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1973 unsigned long lock_flags = 0;
1976 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1977 len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
1978 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1983 * ipr_store_log_level - Change the adapter's error logging level
1984 * @class_dev: class device struct
1988 * number of bytes printed to buffer
1990 static ssize_t ipr_store_log_level(struct class_device *class_dev,
1991 const char *buf, size_t count)
1993 struct Scsi_Host *shost = class_to_shost(class_dev);
1994 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1995 unsigned long lock_flags = 0;
1997 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1998 ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
1999 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2003 static struct class_device_attribute ipr_log_level_attr = {
2005 .name = "log_level",
2006 .mode = S_IRUGO | S_IWUSR,
2008 .show = ipr_show_log_level,
2009 .store = ipr_store_log_level
2013 * ipr_store_diagnostics - IOA Diagnostics interface
2014 * @class_dev: class_device struct
2016 * @count: buffer size
2018 * This function will reset the adapter and wait a reasonable
2019 * amount of time for any errors that the adapter might log.
2022 * count on success / other on failure
2024 static ssize_t ipr_store_diagnostics(struct class_device *class_dev,
2025 const char *buf, size_t count)
2027 struct Scsi_Host *shost = class_to_shost(class_dev);
2028 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2029 unsigned long lock_flags = 0;
2032 if (!capable(CAP_SYS_ADMIN))
2035 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2036 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2037 ioa_cfg->errors_logged = 0;
2038 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2040 if (ioa_cfg->in_reset_reload) {
2041 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2042 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2044 /* Wait for a second for any errors to be logged */
2047 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2051 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2052 if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2054 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2059 static struct class_device_attribute ipr_diagnostics_attr = {
2061 .name = "run_diagnostics",
2064 .store = ipr_store_diagnostics
2068 * ipr_store_reset_adapter - Reset the adapter
2069 * @class_dev: class_device struct
2071 * @count: buffer size
2073 * This function will reset the adapter.
2076 * count on success / other on failure
2078 static ssize_t ipr_store_reset_adapter(struct class_device *class_dev,
2079 const char *buf, size_t count)
2081 struct Scsi_Host *shost = class_to_shost(class_dev);
2082 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2083 unsigned long lock_flags;
2086 if (!capable(CAP_SYS_ADMIN))
2089 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2090 if (!ioa_cfg->in_reset_reload)
2091 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2092 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2093 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2098 static struct class_device_attribute ipr_ioa_reset_attr = {
2100 .name = "reset_host",
2103 .store = ipr_store_reset_adapter
2107 * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2108 * @buf_len: buffer length
2110 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2111 * list to use for microcode download
2114 * pointer to sglist / NULL on failure
2116 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2118 int sg_size, order, bsize_elem, num_elem, i, j;
2119 struct ipr_sglist *sglist;
2120 struct scatterlist *scatterlist;
2123 /* Get the minimum size per scatter/gather element */
2124 sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2126 /* Get the actual size per element */
2127 order = get_order(sg_size);
2129 /* Determine the actual number of bytes per element */
2130 bsize_elem = PAGE_SIZE * (1 << order);
2132 /* Determine the actual number of sg entries needed */
2133 if (buf_len % bsize_elem)
2134 num_elem = (buf_len / bsize_elem) + 1;
2136 num_elem = buf_len / bsize_elem;
2138 /* Allocate a scatter/gather list for the DMA */
2139 sglist = kmalloc(sizeof(struct ipr_sglist) +
2140 (sizeof(struct scatterlist) * (num_elem - 1)),
2143 if (sglist == NULL) {
2148 memset(sglist, 0, sizeof(struct ipr_sglist) +
2149 (sizeof(struct scatterlist) * (num_elem - 1)));
2151 scatterlist = sglist->scatterlist;
2153 sglist->order = order;
2154 sglist->num_sg = num_elem;
2156 /* Allocate a bunch of sg elements */
2157 for (i = 0; i < num_elem; i++) {
2158 page = alloc_pages(GFP_KERNEL, order);
2162 /* Free up what we already allocated */
2163 for (j = i - 1; j >= 0; j--)
2164 __free_pages(scatterlist[j].page, order);
2169 scatterlist[i].page = page;
2176 * ipr_free_ucode_buffer - Frees a microcode download buffer
2177 * @p_dnld: scatter/gather list pointer
2179 * Free a DMA'able ucode download buffer previously allocated with
2180 * ipr_alloc_ucode_buffer
2185 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2189 for (i = 0; i < sglist->num_sg; i++)
2190 __free_pages(sglist->scatterlist[i].page, sglist->order);
2196 * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2197 * @sglist: scatter/gather list pointer
2198 * @buffer: buffer pointer
2199 * @len: buffer length
2201 * Copy a microcode image from a user buffer into a buffer allocated by
2202 * ipr_alloc_ucode_buffer
2205 * 0 on success / other on failure
2207 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2208 u8 *buffer, u32 len)
2210 int bsize_elem, i, result = 0;
2211 struct scatterlist *scatterlist;
2214 /* Determine the actual number of bytes per element */
2215 bsize_elem = PAGE_SIZE * (1 << sglist->order);
2217 scatterlist = sglist->scatterlist;
2219 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
2220 kaddr = kmap(scatterlist[i].page);
2221 memcpy(kaddr, buffer, bsize_elem);
2222 kunmap(scatterlist[i].page);
2224 scatterlist[i].length = bsize_elem;
2232 if (len % bsize_elem) {
2233 kaddr = kmap(scatterlist[i].page);
2234 memcpy(kaddr, buffer, len % bsize_elem);
2235 kunmap(scatterlist[i].page);
2237 scatterlist[i].length = len % bsize_elem;
2240 sglist->buffer_len = len;
2245 * ipr_map_ucode_buffer - Map a microcode download buffer
2246 * @ipr_cmd: ipr command struct
2247 * @sglist: scatter/gather list
2248 * @len: total length of download buffer
2250 * Maps a microcode download scatter/gather list for DMA and
2254 * 0 on success / -EIO on failure
2256 static int ipr_map_ucode_buffer(struct ipr_cmnd *ipr_cmd,
2257 struct ipr_sglist *sglist, int len)
2259 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2260 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2261 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2262 struct scatterlist *scatterlist = sglist->scatterlist;
2265 ipr_cmd->dma_use_sg = pci_map_sg(ioa_cfg->pdev, scatterlist,
2266 sglist->num_sg, DMA_TO_DEVICE);
2268 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
2269 ioarcb->write_data_transfer_length = cpu_to_be32(len);
2270 ioarcb->write_ioadl_len =
2271 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
2273 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
2274 ioadl[i].flags_and_data_len =
2275 cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
2277 cpu_to_be32(sg_dma_address(&scatterlist[i]));
2280 if (likely(ipr_cmd->dma_use_sg)) {
2281 ioadl[i-1].flags_and_data_len |=
2282 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
2285 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
2293 * ipr_store_update_fw - Update the firmware on the adapter
2294 * @class_dev: class_device struct
2296 * @count: buffer size
2298 * This function will update the firmware on the adapter.
2301 * count on success / other on failure
2303 static ssize_t ipr_store_update_fw(struct class_device *class_dev,
2304 const char *buf, size_t count)
2306 struct Scsi_Host *shost = class_to_shost(class_dev);
2307 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2308 struct ipr_ucode_image_header *image_hdr;
2309 const struct firmware *fw_entry;
2310 struct ipr_sglist *sglist;
2311 unsigned long lock_flags;
2314 int len, result, dnld_size;
2316 if (!capable(CAP_SYS_ADMIN))
2319 len = snprintf(fname, 99, "%s", buf);
2320 fname[len-1] = '\0';
2322 if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
2323 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
2327 image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
2329 if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
2330 (ioa_cfg->vpd_cbs->page3_data.card_type &&
2331 ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
2332 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
2333 release_firmware(fw_entry);
2337 src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
2338 dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
2339 sglist = ipr_alloc_ucode_buffer(dnld_size);
2342 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
2343 release_firmware(fw_entry);
2347 result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
2350 dev_err(&ioa_cfg->pdev->dev,
2351 "Microcode buffer copy to DMA buffer failed\n");
2352 ipr_free_ucode_buffer(sglist);
2353 release_firmware(fw_entry);
2357 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2359 if (ioa_cfg->ucode_sglist) {
2360 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2361 dev_err(&ioa_cfg->pdev->dev,
2362 "Microcode download already in progress\n");
2363 ipr_free_ucode_buffer(sglist);
2364 release_firmware(fw_entry);
2368 ioa_cfg->ucode_sglist = sglist;
2369 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2370 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2371 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2373 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2374 ioa_cfg->ucode_sglist = NULL;
2375 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2377 ipr_free_ucode_buffer(sglist);
2378 release_firmware(fw_entry);
2383 static struct class_device_attribute ipr_update_fw_attr = {
2385 .name = "update_fw",
2388 .store = ipr_store_update_fw
2391 static struct class_device_attribute *ipr_ioa_attrs[] = {
2392 &ipr_fw_version_attr,
2393 &ipr_log_level_attr,
2394 &ipr_diagnostics_attr,
2395 &ipr_ioa_reset_attr,
2396 &ipr_update_fw_attr,
2400 #ifdef CONFIG_SCSI_IPR_DUMP
2402 * ipr_read_dump - Dump the adapter
2403 * @kobj: kobject struct
2406 * @count: buffer size
2409 * number of bytes printed to buffer
2411 static ssize_t ipr_read_dump(struct kobject *kobj, char *buf,
2412 loff_t off, size_t count)
2414 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2415 struct Scsi_Host *shost = class_to_shost(cdev);
2416 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2417 struct ipr_dump *dump;
2418 unsigned long lock_flags = 0;
2423 if (!capable(CAP_SYS_ADMIN))
2426 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2427 dump = ioa_cfg->dump;
2429 if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
2430 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2433 kref_get(&dump->kref);
2434 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2436 if (off > dump->driver_dump.hdr.len) {
2437 kref_put(&dump->kref, ipr_release_dump);
2441 if (off + count > dump->driver_dump.hdr.len) {
2442 count = dump->driver_dump.hdr.len - off;
2446 if (count && off < sizeof(dump->driver_dump)) {
2447 if (off + count > sizeof(dump->driver_dump))
2448 len = sizeof(dump->driver_dump) - off;
2451 src = (u8 *)&dump->driver_dump + off;
2452 memcpy(buf, src, len);
2458 off -= sizeof(dump->driver_dump);
2460 if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
2461 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
2462 len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
2465 src = (u8 *)&dump->ioa_dump + off;
2466 memcpy(buf, src, len);
2472 off -= offsetof(struct ipr_ioa_dump, ioa_data);
2475 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
2476 len = PAGE_ALIGN(off) - off;
2479 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
2480 src += off & ~PAGE_MASK;
2481 memcpy(buf, src, len);
2487 kref_put(&dump->kref, ipr_release_dump);
2492 * ipr_alloc_dump - Prepare for adapter dump
2493 * @ioa_cfg: ioa config struct
2496 * 0 on success / other on failure
2498 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
2500 struct ipr_dump *dump;
2501 unsigned long lock_flags = 0;
2504 dump = kmalloc(sizeof(struct ipr_dump), GFP_KERNEL);
2507 ipr_err("Dump memory allocation failed\n");
2511 memset(dump, 0, sizeof(struct ipr_dump));
2512 kref_init(&dump->kref);
2513 dump->ioa_cfg = ioa_cfg;
2515 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2517 if (INACTIVE != ioa_cfg->sdt_state) {
2518 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2523 ioa_cfg->dump = dump;
2524 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
2525 if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
2526 ioa_cfg->dump_taken = 1;
2527 schedule_work(&ioa_cfg->work_q);
2529 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2536 * ipr_free_dump - Free adapter dump memory
2537 * @ioa_cfg: ioa config struct
2540 * 0 on success / other on failure
2542 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
2544 struct ipr_dump *dump;
2545 unsigned long lock_flags = 0;
2549 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2550 dump = ioa_cfg->dump;
2552 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2556 ioa_cfg->dump = NULL;
2557 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2559 kref_put(&dump->kref, ipr_release_dump);
2566 * ipr_write_dump - Setup dump state of adapter
2567 * @kobj: kobject struct
2570 * @count: buffer size
2573 * number of bytes printed to buffer
2575 static ssize_t ipr_write_dump(struct kobject *kobj, char *buf,
2576 loff_t off, size_t count)
2578 struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2579 struct Scsi_Host *shost = class_to_shost(cdev);
2580 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2583 if (!capable(CAP_SYS_ADMIN))
2587 rc = ipr_alloc_dump(ioa_cfg);
2588 else if (buf[0] == '0')
2589 rc = ipr_free_dump(ioa_cfg);
2599 static struct bin_attribute ipr_dump_attr = {
2602 .mode = S_IRUSR | S_IWUSR,
2605 .read = ipr_read_dump,
2606 .write = ipr_write_dump
2609 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
2613 * ipr_change_queue_depth - Change the device's queue depth
2614 * @sdev: scsi device struct
2615 * @qdepth: depth to set
2620 static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
2622 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2623 struct ipr_resource_entry *res;
2625 unsigned long lock_flags = 0;
2627 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2628 res = (struct ipr_resource_entry *)sdev->hostdata;
2630 res->qdepth = qdepth;
2632 if (ipr_is_gscsi(res) && res->tcq_active)
2633 tagged = MSG_ORDERED_TAG;
2636 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2637 scsi_adjust_queue_depth(sdev, tagged, qdepth);
2642 * ipr_show_tcq_enable - Show if the device is enabled for tcqing
2643 * @dev: device struct
2647 * number of bytes printed to buffer
2649 static ssize_t ipr_show_tcq_enable(struct device *dev, char *buf)
2651 struct scsi_device *sdev = to_scsi_device(dev);
2652 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2653 struct ipr_resource_entry *res;
2654 unsigned long lock_flags = 0;
2655 ssize_t len = -ENXIO;
2657 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2658 res = (struct ipr_resource_entry *)sdev->hostdata;
2660 len = snprintf(buf, PAGE_SIZE, "%d\n", res->tcq_active);
2661 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2666 * ipr_store_tcq_enable - Change the device's TCQing state
2667 * @dev: device struct
2671 * number of bytes printed to buffer
2673 static ssize_t ipr_store_tcq_enable(struct device *dev,
2674 const char *buf, size_t count)
2676 struct scsi_device *sdev = to_scsi_device(dev);
2677 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2678 struct ipr_resource_entry *res;
2679 unsigned long lock_flags = 0;
2680 int tcq_active = simple_strtoul(buf, NULL, 10);
2681 ssize_t len = -ENXIO;
2683 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2685 res = (struct ipr_resource_entry *)sdev->hostdata;
2688 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
2690 res->tcq_active = 1;
2691 scsi_activate_tcq(sdev, res->qdepth);
2693 res->tcq_active = 0;
2694 scsi_deactivate_tcq(sdev, res->qdepth);
2698 } else if (tcq_active) {
2703 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2707 static struct device_attribute ipr_tcqing_attr = {
2709 .name = "tcq_enable",
2710 .mode = S_IRUSR | S_IWUSR,
2712 .store = ipr_store_tcq_enable,
2713 .show = ipr_show_tcq_enable
2717 * ipr_show_adapter_handle - Show the adapter's resource handle for this device
2718 * @dev: device struct
2722 * number of bytes printed to buffer
2724 static ssize_t ipr_show_adapter_handle(struct device *dev, char *buf)
2726 struct scsi_device *sdev = to_scsi_device(dev);
2727 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2728 struct ipr_resource_entry *res;
2729 unsigned long lock_flags = 0;
2730 ssize_t len = -ENXIO;
2732 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2733 res = (struct ipr_resource_entry *)sdev->hostdata;
2735 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->cfgte.res_handle);
2736 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2740 static struct device_attribute ipr_adapter_handle_attr = {
2742 .name = "adapter_handle",
2745 .show = ipr_show_adapter_handle
2748 static struct device_attribute *ipr_dev_attrs[] = {
2750 &ipr_adapter_handle_attr,
2755 * ipr_biosparam - Return the HSC mapping
2756 * @sdev: scsi device struct
2757 * @block_device: block device pointer
2758 * @capacity: capacity of the device
2759 * @parm: Array containing returned HSC values.
2761 * This function generates the HSC parms that fdisk uses.
2762 * We want to make sure we return something that places partitions
2763 * on 4k boundaries for best performance with the IOA.
2768 static int ipr_biosparam(struct scsi_device *sdev,
2769 struct block_device *block_device,
2770 sector_t capacity, int *parm)
2778 cylinders = capacity;
2779 sector_div(cylinders, (128 * 32));
2784 parm[2] = cylinders;
2790 * ipr_slave_destroy - Unconfigure a SCSI device
2791 * @sdev: scsi device struct
2796 static void ipr_slave_destroy(struct scsi_device *sdev)
2798 struct ipr_resource_entry *res;
2799 struct ipr_ioa_cfg *ioa_cfg;
2800 unsigned long lock_flags = 0;
2802 ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2804 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2805 res = (struct ipr_resource_entry *) sdev->hostdata;
2807 sdev->hostdata = NULL;
2810 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2814 * ipr_slave_configure - Configure a SCSI device
2815 * @sdev: scsi device struct
2817 * This function configures the specified scsi device.
2822 static int ipr_slave_configure(struct scsi_device *sdev)
2824 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2825 struct ipr_resource_entry *res;
2826 unsigned long lock_flags = 0;
2828 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2829 res = sdev->hostdata;
2831 if (ipr_is_af_dasd_device(res))
2832 sdev->type = TYPE_RAID;
2833 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res))
2834 sdev->scsi_level = 4;
2835 if (ipr_is_vset_device(res))
2836 sdev->timeout = IPR_VSET_RW_TIMEOUT;
2837 if (IPR_IS_DASD_DEVICE(res->cfgte.std_inq_data))
2838 sdev->allow_restart = 1;
2839 scsi_adjust_queue_depth(sdev, 0, res->qdepth);
2841 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2846 * ipr_slave_alloc - Prepare for commands to a device.
2847 * @sdev: scsi device struct
2849 * This function saves a pointer to the resource entry
2850 * in the scsi device struct if the device exists. We
2851 * can then use this pointer in ipr_queuecommand when
2852 * handling new commands.
2857 static int ipr_slave_alloc(struct scsi_device *sdev)
2859 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2860 struct ipr_resource_entry *res;
2861 unsigned long lock_flags;
2863 sdev->hostdata = NULL;
2865 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2867 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2868 if ((res->cfgte.res_addr.bus == sdev->channel) &&
2869 (res->cfgte.res_addr.target == sdev->id) &&
2870 (res->cfgte.res_addr.lun == sdev->lun)) {
2874 sdev->hostdata = res;
2875 res->needs_sync_complete = 1;
2880 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2886 * ipr_eh_host_reset - Reset the host adapter
2887 * @scsi_cmd: scsi command struct
2892 static int ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
2894 struct ipr_ioa_cfg *ioa_cfg;
2898 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
2900 dev_err(&ioa_cfg->pdev->dev,
2901 "Adapter being reset as a result of error recovery.\n");
2903 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2904 ioa_cfg->sdt_state = GET_DUMP;
2906 rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
2913 * ipr_eh_dev_reset - Reset the device
2914 * @scsi_cmd: scsi command struct
2916 * This function issues a device reset to the affected device.
2917 * A LUN reset will be sent to the device first. If that does
2918 * not work, a target reset will be sent.
2923 static int ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
2925 struct ipr_cmnd *ipr_cmd;
2926 struct ipr_ioa_cfg *ioa_cfg;
2927 struct ipr_resource_entry *res;
2928 struct ipr_cmd_pkt *cmd_pkt;
2932 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
2933 res = scsi_cmd->device->hostdata;
2935 if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
2939 * If we are currently going through reset/reload, return failed. This will force the
2940 * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
2943 if (ioa_cfg->in_reset_reload)
2945 if (ioa_cfg->ioa_is_dead)
2948 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
2949 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
2950 if (ipr_cmd->scsi_cmd)
2951 ipr_cmd->done = ipr_scsi_eh_done;
2955 res->resetting_device = 1;
2957 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
2959 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
2960 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
2961 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
2962 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
2964 ipr_sdev_err(scsi_cmd->device, "Resetting device\n");
2965 ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
2967 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
2969 res->resetting_device = 0;
2971 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
2974 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
2978 * ipr_bus_reset_done - Op done function for bus reset.
2979 * @ipr_cmd: ipr command struct
2981 * This function is the op done function for a bus reset
2986 static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
2988 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2989 struct ipr_resource_entry *res;
2992 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2993 if (!memcmp(&res->cfgte.res_handle, &ipr_cmd->ioarcb.res_handle,
2994 sizeof(res->cfgte.res_handle))) {
2995 scsi_report_bus_reset(ioa_cfg->host, res->cfgte.res_addr.bus);
3001 * If abort has not completed, indicate the reset has, else call the
3002 * abort's done function to wake the sleeping eh thread
3004 if (ipr_cmd->sibling->sibling)
3005 ipr_cmd->sibling->sibling = NULL;
3007 ipr_cmd->sibling->done(ipr_cmd->sibling);
3009 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3014 * ipr_abort_timeout - An abort task has timed out
3015 * @ipr_cmd: ipr command struct
3017 * This function handles when an abort task times out. If this
3018 * happens we issue a bus reset since we have resources tied
3019 * up that must be freed before returning to the midlayer.
3024 static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
3026 struct ipr_cmnd *reset_cmd;
3027 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3028 struct ipr_cmd_pkt *cmd_pkt;
3029 unsigned long lock_flags = 0;
3032 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3033 if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
3034 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3038 ipr_sdev_err(ipr_cmd->u.sdev, "Abort timed out. Resetting bus\n");
3039 reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3040 ipr_cmd->sibling = reset_cmd;
3041 reset_cmd->sibling = ipr_cmd;
3042 reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
3043 cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
3044 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3045 cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
3046 cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
3048 ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3049 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3054 * ipr_cancel_op - Cancel specified op
3055 * @scsi_cmd: scsi command struct
3057 * This function cancels specified op.
3062 static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
3064 struct ipr_cmnd *ipr_cmd;
3065 struct ipr_ioa_cfg *ioa_cfg;
3066 struct ipr_resource_entry *res;
3067 struct ipr_cmd_pkt *cmd_pkt;
3072 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3073 res = scsi_cmd->device->hostdata;
3075 if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
3078 list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3079 if (ipr_cmd->scsi_cmd == scsi_cmd) {
3080 ipr_cmd->done = ipr_scsi_eh_done;
3089 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3090 ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
3091 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3092 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3093 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
3094 ipr_cmd->u.sdev = scsi_cmd->device;
3096 ipr_sdev_err(scsi_cmd->device, "Aborting command: %02X\n", scsi_cmd->cmnd[0]);
3097 ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
3098 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3101 * If the abort task timed out and we sent a bus reset, we will get
3102 * one the following responses to the abort
3104 if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
3109 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3110 res->needs_sync_complete = 1;
3113 return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
3117 * ipr_eh_abort - Abort a single op
3118 * @scsi_cmd: scsi command struct
3123 static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
3125 struct ipr_ioa_cfg *ioa_cfg;
3128 ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3130 /* If we are currently going through reset/reload, return failed. This will force the
3131 mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
3132 reset to complete */
3133 if (ioa_cfg->in_reset_reload)
3135 if (ioa_cfg->ioa_is_dead)
3137 if (!scsi_cmd->device->hostdata)
3141 return ipr_cancel_op(scsi_cmd);
3145 * ipr_handle_other_interrupt - Handle "other" interrupts
3146 * @ioa_cfg: ioa config struct
3147 * @int_reg: interrupt register
3150 * IRQ_NONE / IRQ_HANDLED
3152 static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
3153 volatile u32 int_reg)
3155 irqreturn_t rc = IRQ_HANDLED;
3157 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
3158 /* Mask the interrupt */
3159 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
3161 /* Clear the interrupt */
3162 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
3163 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
3165 list_del(&ioa_cfg->reset_cmd->queue);
3166 del_timer(&ioa_cfg->reset_cmd->timer);
3167 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
3169 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
3170 ioa_cfg->ioa_unit_checked = 1;
3172 dev_err(&ioa_cfg->pdev->dev,
3173 "Permanent IOA failure. 0x%08X\n", int_reg);
3175 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3176 ioa_cfg->sdt_state = GET_DUMP;
3178 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
3179 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3186 * ipr_isr - Interrupt service routine
3188 * @devp: pointer to ioa config struct
3189 * @regs: pt_regs struct
3192 * IRQ_NONE / IRQ_HANDLED
3194 static irqreturn_t ipr_isr(int irq, void *devp, struct pt_regs *regs)
3196 struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
3197 unsigned long lock_flags = 0;
3198 volatile u32 int_reg, int_mask_reg;
3201 struct ipr_cmnd *ipr_cmd;
3202 irqreturn_t rc = IRQ_NONE;
3204 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3206 /* If interrupts are disabled, ignore the interrupt */
3207 if (!ioa_cfg->allow_interrupts) {
3208 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3212 int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
3213 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
3215 /* If an interrupt on the adapter did not occur, ignore it */
3216 if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
3217 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3224 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
3225 ioa_cfg->toggle_bit) {
3227 cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
3228 IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
3230 if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
3231 ioa_cfg->errors_logged++;
3232 dev_err(&ioa_cfg->pdev->dev, "Invalid response handle from IOA\n");
3234 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3235 ioa_cfg->sdt_state = GET_DUMP;
3237 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3238 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3242 ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
3244 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3246 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
3248 list_del(&ipr_cmd->queue);
3249 del_timer(&ipr_cmd->timer);
3250 ipr_cmd->done(ipr_cmd);
3254 if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
3255 ioa_cfg->hrrq_curr++;
3257 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
3258 ioa_cfg->toggle_bit ^= 1u;
3262 if (ipr_cmd != NULL) {
3263 /* Clear the PCI interrupt */
3264 writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg);
3265 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
3270 if (unlikely(rc == IRQ_NONE))
3271 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
3273 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3278 * ipr_build_ioadl - Build a scatter/gather list and map the buffer
3279 * @ioa_cfg: ioa config struct
3280 * @ipr_cmd: ipr command struct
3283 * 0 on success / -1 on failure
3285 static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
3286 struct ipr_cmnd *ipr_cmd)
3289 struct scatterlist *sglist;
3291 u32 ioadl_flags = 0;
3292 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3293 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3294 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
3296 length = scsi_cmd->request_bufflen;
3301 if (scsi_cmd->use_sg) {
3302 ipr_cmd->dma_use_sg = pci_map_sg(ioa_cfg->pdev,
3303 scsi_cmd->request_buffer,
3305 scsi_cmd->sc_data_direction);
3307 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
3308 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
3309 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3310 ioarcb->write_data_transfer_length = cpu_to_be32(length);
3311 ioarcb->write_ioadl_len =
3312 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3313 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
3314 ioadl_flags = IPR_IOADL_FLAGS_READ;
3315 ioarcb->read_data_transfer_length = cpu_to_be32(length);
3316 ioarcb->read_ioadl_len =
3317 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3320 sglist = scsi_cmd->request_buffer;
3322 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3323 ioadl[i].flags_and_data_len =
3324 cpu_to_be32(ioadl_flags | sg_dma_len(&sglist[i]));
3326 cpu_to_be32(sg_dma_address(&sglist[i]));
3329 if (likely(ipr_cmd->dma_use_sg)) {
3330 ioadl[i-1].flags_and_data_len |=
3331 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3334 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
3336 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
3337 ioadl_flags = IPR_IOADL_FLAGS_WRITE;
3338 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3339 ioarcb->write_data_transfer_length = cpu_to_be32(length);
3340 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3341 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
3342 ioadl_flags = IPR_IOADL_FLAGS_READ;
3343 ioarcb->read_data_transfer_length = cpu_to_be32(length);
3344 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3347 ipr_cmd->dma_handle = pci_map_single(ioa_cfg->pdev,
3348 scsi_cmd->request_buffer, length,
3349 scsi_cmd->sc_data_direction);
3351 if (likely(!pci_dma_mapping_error(ipr_cmd->dma_handle))) {
3352 ipr_cmd->dma_use_sg = 1;
3353 ioadl[0].flags_and_data_len =
3354 cpu_to_be32(ioadl_flags | length | IPR_IOADL_FLAGS_LAST);
3355 ioadl[0].address = cpu_to_be32(ipr_cmd->dma_handle);
3358 dev_err(&ioa_cfg->pdev->dev, "pci_map_single failed!\n");
3365 * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
3366 * @scsi_cmd: scsi command struct
3371 static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
3374 u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
3376 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
3378 case MSG_SIMPLE_TAG:
3379 rc = IPR_FLAGS_LO_SIMPLE_TASK;
3382 rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
3384 case MSG_ORDERED_TAG:
3385 rc = IPR_FLAGS_LO_ORDERED_TASK;
3394 * ipr_erp_done - Process completion of ERP for a device
3395 * @ipr_cmd: ipr command struct
3397 * This function copies the sense buffer into the scsi_cmd
3398 * struct and pushes the scsi_done function.
3403 static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
3405 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3406 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3407 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3408 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3410 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3411 scsi_cmd->result |= (DID_ERROR << 16);
3412 ipr_sdev_err(scsi_cmd->device,
3413 "Request Sense failed with IOASC: 0x%08X\n", ioasc);
3415 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
3416 SCSI_SENSE_BUFFERSIZE);
3420 res->needs_sync_complete = 1;
3423 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3424 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3425 scsi_cmd->scsi_done(scsi_cmd);
3429 * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
3430 * @ipr_cmd: ipr command struct
3435 static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
3437 struct ipr_ioarcb *ioarcb;
3438 struct ipr_ioasa *ioasa;
3440 ioarcb = &ipr_cmd->ioarcb;
3441 ioasa = &ipr_cmd->ioasa;
3443 memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
3444 ioarcb->write_data_transfer_length = 0;
3445 ioarcb->read_data_transfer_length = 0;
3446 ioarcb->write_ioadl_len = 0;
3447 ioarcb->read_ioadl_len = 0;
3449 ioasa->residual_data_len = 0;
3453 * ipr_erp_request_sense - Send request sense to a device
3454 * @ipr_cmd: ipr command struct
3456 * This function sends a request sense to a device as a result
3457 * of a check condition.
3462 static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
3464 struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3465 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3467 if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3468 ipr_erp_done(ipr_cmd);
3472 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3474 cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
3475 cmd_pkt->cdb[0] = REQUEST_SENSE;
3476 cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
3477 cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
3478 cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
3479 cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
3481 ipr_cmd->ioadl[0].flags_and_data_len =
3482 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | SCSI_SENSE_BUFFERSIZE);
3483 ipr_cmd->ioadl[0].address =
3484 cpu_to_be32(ipr_cmd->sense_buffer_dma);
3486 ipr_cmd->ioarcb.read_ioadl_len =
3487 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3488 ipr_cmd->ioarcb.read_data_transfer_length =
3489 cpu_to_be32(SCSI_SENSE_BUFFERSIZE);
3491 ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
3492 IPR_REQUEST_SENSE_TIMEOUT * 2);
3496 * ipr_erp_cancel_all - Send cancel all to a device
3497 * @ipr_cmd: ipr command struct
3499 * This function sends a cancel all to a device to clear the
3500 * queue. If we are running TCQ on the device, QERR is set to 1,
3501 * which means all outstanding ops have been dropped on the floor.
3502 * Cancel all will return them to us.
3507 static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
3509 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3510 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3511 struct ipr_cmd_pkt *cmd_pkt;
3515 ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3517 if (!res->tcq_active) {
3518 ipr_erp_request_sense(ipr_cmd);
3522 cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3523 cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3524 cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
3526 ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
3527 IPR_CANCEL_ALL_TIMEOUT);
3531 * ipr_dump_ioasa - Dump contents of IOASA
3532 * @ioa_cfg: ioa config struct
3533 * @ipr_cmd: ipr command struct
3535 * This function is invoked by the interrupt handler when ops
3536 * fail. It will log the IOASA if appropriate. Only called
3542 static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
3543 struct ipr_cmnd *ipr_cmd)
3548 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
3549 u32 *ioasa_data = (u32 *)ioasa;
3552 ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
3557 if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
3560 error_index = ipr_get_error(ioasc);
3562 if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
3563 /* Don't log an error if the IOA already logged one */
3564 if (ioasa->ilid != 0)
3567 if (ipr_error_table[error_index].log_ioasa == 0)
3571 ipr_sdev_err(ipr_cmd->scsi_cmd->device, "%s\n",
3572 ipr_error_table[error_index].error);
3574 if ((ioasa->u.gpdd.end_state <= ARRAY_SIZE(ipr_gpdd_dev_end_states)) &&
3575 (ioasa->u.gpdd.bus_phase <= ARRAY_SIZE(ipr_gpdd_dev_bus_phases))) {
3576 ipr_sdev_err(ipr_cmd->scsi_cmd->device,
3577 "Device End state: %s Phase: %s\n",
3578 ipr_gpdd_dev_end_states[ioasa->u.gpdd.end_state],
3579 ipr_gpdd_dev_bus_phases[ioasa->u.gpdd.bus_phase]);
3582 if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
3583 data_len = sizeof(struct ipr_ioasa);
3585 data_len = be16_to_cpu(ioasa->ret_stat_len);
3587 ipr_err("IOASA Dump:\n");
3589 for (i = 0; i < data_len / 4; i += 4) {
3590 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
3591 be32_to_cpu(ioasa_data[i]),
3592 be32_to_cpu(ioasa_data[i+1]),
3593 be32_to_cpu(ioasa_data[i+2]),
3594 be32_to_cpu(ioasa_data[i+3]));
3599 * ipr_gen_sense - Generate SCSI sense data from an IOASA
3601 * @sense_buf: sense data buffer
3606 static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
3609 u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
3610 struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
3611 struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
3612 u32 ioasc = be32_to_cpu(ioasa->ioasc);
3614 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
3616 if (ioasc >= IPR_FIRST_DRIVER_IOASC)
3619 ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
3621 if (ipr_is_vset_device(res) &&
3622 ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
3623 ioasa->u.vset.failing_lba_hi != 0) {
3624 sense_buf[0] = 0x72;
3625 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
3626 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
3627 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
3631 sense_buf[9] = 0x0A;
3632 sense_buf[10] = 0x80;
3634 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
3636 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
3637 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
3638 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
3639 sense_buf[15] = failing_lba & 0x000000ff;
3641 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
3643 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
3644 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
3645 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
3646 sense_buf[19] = failing_lba & 0x000000ff;
3648 sense_buf[0] = 0x70;
3649 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
3650 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
3651 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
3653 /* Illegal request */
3654 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
3655 (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
3656 sense_buf[7] = 10; /* additional length */
3658 /* IOARCB was in error */
3659 if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
3660 sense_buf[15] = 0xC0;
3661 else /* Parameter data was invalid */
3662 sense_buf[15] = 0x80;
3665 ((IPR_FIELD_POINTER_MASK &
3666 be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
3668 (IPR_FIELD_POINTER_MASK &
3669 be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
3671 if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
3672 if (ipr_is_vset_device(res))
3673 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
3675 failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
3677 sense_buf[0] |= 0x80; /* Or in the Valid bit */
3678 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
3679 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
3680 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
3681 sense_buf[6] = failing_lba & 0x000000ff;
3684 sense_buf[7] = 6; /* additional length */
3690 * ipr_erp_start - Process an error response for a SCSI op
3691 * @ioa_cfg: ioa config struct
3692 * @ipr_cmd: ipr command struct
3694 * This function determines whether or not to initiate ERP
3695 * on the affected device.
3700 static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
3701 struct ipr_cmnd *ipr_cmd)
3703 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3704 struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3705 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3708 ipr_scsi_eh_done(ipr_cmd);
3712 if (ipr_is_gscsi(res))
3713 ipr_dump_ioasa(ioa_cfg, ipr_cmd);
3715 ipr_gen_sense(ipr_cmd);
3717 switch (ioasc & IPR_IOASC_IOASC_MASK) {
3718 case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
3719 scsi_cmd->result |= (DID_IMM_RETRY << 16);
3721 case IPR_IOASC_IR_RESOURCE_HANDLE:
3722 scsi_cmd->result |= (DID_NO_CONNECT << 16);
3724 case IPR_IOASC_HW_SEL_TIMEOUT:
3725 scsi_cmd->result |= (DID_NO_CONNECT << 16);
3726 res->needs_sync_complete = 1;
3728 case IPR_IOASC_SYNC_REQUIRED:
3730 res->needs_sync_complete = 1;
3731 scsi_cmd->result |= (DID_IMM_RETRY << 16);
3733 case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
3734 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
3736 case IPR_IOASC_BUS_WAS_RESET:
3737 case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
3739 * Report the bus reset and ask for a retry. The device
3740 * will give CC/UA the next command.
3742 if (!res->resetting_device)
3743 scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
3744 scsi_cmd->result |= (DID_ERROR << 16);
3745 res->needs_sync_complete = 1;
3747 case IPR_IOASC_HW_DEV_BUS_STATUS:
3748 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
3749 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
3750 ipr_erp_cancel_all(ipr_cmd);
3753 res->needs_sync_complete = 1;
3755 case IPR_IOASC_NR_INIT_CMD_REQUIRED:
3758 scsi_cmd->result |= (DID_ERROR << 16);
3759 if (!ipr_is_vset_device(res))
3760 res->needs_sync_complete = 1;
3764 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3765 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3766 scsi_cmd->scsi_done(scsi_cmd);
3770 * ipr_scsi_done - mid-layer done function
3771 * @ipr_cmd: ipr command struct
3773 * This function is invoked by the interrupt handler for
3774 * ops generated by the SCSI mid-layer
3779 static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
3781 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3782 struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3783 u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3785 scsi_cmd->resid = be32_to_cpu(ipr_cmd->ioasa.residual_data_len);
3787 if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
3788 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3789 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3790 scsi_cmd->scsi_done(scsi_cmd);
3792 ipr_erp_start(ioa_cfg, ipr_cmd);
3796 * ipr_save_ioafp_mode_select - Save adapters mode select data
3797 * @ioa_cfg: ioa config struct
3798 * @scsi_cmd: scsi command struct
3800 * This function saves mode select data for the adapter to
3801 * use following an adapter reset.
3804 * 0 on success / SCSI_MLQUEUE_HOST_BUSY on failure
3806 static int ipr_save_ioafp_mode_select(struct ipr_ioa_cfg *ioa_cfg,
3807 struct scsi_cmnd *scsi_cmd)
3809 if (!ioa_cfg->saved_mode_pages) {
3810 ioa_cfg->saved_mode_pages = kmalloc(sizeof(struct ipr_mode_pages),
3812 if (!ioa_cfg->saved_mode_pages) {
3813 dev_err(&ioa_cfg->pdev->dev,
3814 "IOA mode select buffer allocation failed\n");
3815 return SCSI_MLQUEUE_HOST_BUSY;
3819 memcpy(ioa_cfg->saved_mode_pages, scsi_cmd->buffer, scsi_cmd->cmnd[4]);
3820 ioa_cfg->saved_mode_page_len = scsi_cmd->cmnd[4];
3825 * ipr_queuecommand - Queue a mid-layer request
3826 * @scsi_cmd: scsi command struct
3827 * @done: done function
3829 * This function queues a request generated by the mid-layer.
3833 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3834 * SCSI_MLQUEUE_HOST_BUSY if host is busy
3836 static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
3837 void (*done) (struct scsi_cmnd *))
3839 struct ipr_ioa_cfg *ioa_cfg;
3840 struct ipr_resource_entry *res;
3841 struct ipr_ioarcb *ioarcb;
3842 struct ipr_cmnd *ipr_cmd;
3845 scsi_cmd->scsi_done = done;
3846 ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3847 res = scsi_cmd->device->hostdata;
3848 scsi_cmd->result = (DID_OK << 16);
3851 * We are currently blocking all devices due to a host reset
3852 * We have told the host to stop giving us new requests, but
3853 * ERP ops don't count. FIXME
3855 if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
3856 return SCSI_MLQUEUE_HOST_BUSY;
3859 * FIXME - Create scsi_set_host_offline interface
3860 * and the ioa_is_dead check can be removed
3862 if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
3863 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
3864 scsi_cmd->result = (DID_NO_CONNECT << 16);
3865 scsi_cmd->scsi_done(scsi_cmd);
3869 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3870 ioarcb = &ipr_cmd->ioarcb;
3871 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
3873 memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
3874 ipr_cmd->scsi_cmd = scsi_cmd;
3875 ioarcb->res_handle = res->cfgte.res_handle;
3876 ipr_cmd->done = ipr_scsi_done;
3877 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
3879 if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
3880 if (scsi_cmd->underflow == 0)
3881 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
3883 if (res->needs_sync_complete) {
3884 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
3885 res->needs_sync_complete = 0;
3888 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
3889 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
3890 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
3891 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
3894 if (scsi_cmd->cmnd[0] >= 0xC0 &&
3895 (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
3896 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
3898 if (ipr_is_ioa_resource(res) && scsi_cmd->cmnd[0] == MODE_SELECT)
3899 rc = ipr_save_ioafp_mode_select(ioa_cfg, scsi_cmd);
3901 if (likely(rc == 0))
3902 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
3904 if (likely(rc == 0)) {
3906 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
3907 ioa_cfg->regs.ioarrin_reg);
3909 list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3910 return SCSI_MLQUEUE_HOST_BUSY;
3917 * ipr_info - Get information about the card/driver
3918 * @scsi_host: scsi host struct
3921 * pointer to buffer with description string
3923 static const char * ipr_ioa_info(struct Scsi_Host *host)
3925 static char buffer[512];
3926 struct ipr_ioa_cfg *ioa_cfg;
3927 unsigned long lock_flags = 0;
3929 ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
3931 spin_lock_irqsave(host->host_lock, lock_flags);
3932 sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
3933 spin_unlock_irqrestore(host->host_lock, lock_flags);
3938 static struct scsi_host_template driver_template = {
3939 .module = THIS_MODULE,
3941 .info = ipr_ioa_info,
3942 .queuecommand = ipr_queuecommand,
3943 .eh_abort_handler = ipr_eh_abort,
3944 .eh_device_reset_handler = ipr_eh_dev_reset,
3945 .eh_host_reset_handler = ipr_eh_host_reset,
3946 .slave_alloc = ipr_slave_alloc,
3947 .slave_configure = ipr_slave_configure,
3948 .slave_destroy = ipr_slave_destroy,
3949 .change_queue_depth = ipr_change_queue_depth,
3950 .bios_param = ipr_biosparam,
3951 .can_queue = IPR_MAX_COMMANDS,
3953 .sg_tablesize = IPR_MAX_SGLIST,
3954 .max_sectors = IPR_MAX_SECTORS,
3955 .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
3956 .use_clustering = ENABLE_CLUSTERING,
3957 .shost_attrs = ipr_ioa_attrs,
3958 .sdev_attrs = ipr_dev_attrs,
3959 .proc_name = IPR_NAME
3962 #ifdef CONFIG_PPC_PSERIES
3963 static const u16 ipr_blocked_processors[] = {
3975 * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
3976 * @ioa_cfg: ioa cfg struct
3978 * Adapters that use Gemstone revision < 3.1 do not work reliably on
3979 * certain pSeries hardware. This function determines if the given
3980 * adapter is in one of these confgurations or not.
3983 * 1 if adapter is not supported / 0 if adapter is supported
3985 static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
3990 if (ioa_cfg->type == 0x5702) {
3991 if (pci_read_config_byte(ioa_cfg->pdev, PCI_REVISION_ID,
3992 &rev_id) == PCIBIOS_SUCCESSFUL) {
3994 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
3995 if (__is_processor(ipr_blocked_processors[i]))
4004 #define ipr_invalid_adapter(ioa_cfg) 0
4008 * ipr_ioa_bringdown_done - IOA bring down completion.
4009 * @ipr_cmd: ipr command struct
4011 * This function processes the completion of an adapter bring down.
4012 * It wakes any reset sleepers.
4017 static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
4019 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4022 ioa_cfg->in_reset_reload = 0;
4023 ioa_cfg->reset_retries = 0;
4024 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4025 wake_up_all(&ioa_cfg->reset_wait_q);
4027 spin_unlock_irq(ioa_cfg->host->host_lock);
4028 scsi_unblock_requests(ioa_cfg->host);
4029 spin_lock_irq(ioa_cfg->host->host_lock);
4032 return IPR_RC_JOB_RETURN;
4036 * ipr_ioa_reset_done - IOA reset completion.
4037 * @ipr_cmd: ipr command struct
4039 * This function processes the completion of an adapter reset.
4040 * It schedules any necessary mid-layer add/removes and
4041 * wakes any reset sleepers.
4046 static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
4048 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4049 struct ipr_resource_entry *res;
4050 struct ipr_hostrcb *hostrcb, *temp;
4054 ioa_cfg->in_reset_reload = 0;
4055 ioa_cfg->allow_cmds = 1;
4056 ioa_cfg->reset_cmd = NULL;
4058 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4059 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
4061 schedule_work(&ioa_cfg->work_q);
4066 list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
4067 list_del(&hostrcb->queue);
4068 if (i++ < IPR_NUM_LOG_HCAMS)
4069 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
4071 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
4074 dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
4076 ioa_cfg->reset_retries = 0;
4077 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4078 wake_up_all(&ioa_cfg->reset_wait_q);
4080 spin_unlock_irq(ioa_cfg->host->host_lock);
4081 scsi_unblock_requests(ioa_cfg->host);
4082 spin_lock_irq(ioa_cfg->host->host_lock);
4084 if (!ioa_cfg->allow_cmds)
4085 scsi_block_requests(ioa_cfg->host);
4088 return IPR_RC_JOB_RETURN;
4092 * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
4093 * @supported_dev: supported device struct
4094 * @vpids: vendor product id struct
4099 static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
4100 struct ipr_std_inq_vpids *vpids)
4102 memset(supported_dev, 0, sizeof(struct ipr_supported_device));
4103 memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
4104 supported_dev->num_records = 1;
4105 supported_dev->data_length =
4106 cpu_to_be16(sizeof(struct ipr_supported_device));
4107 supported_dev->reserved = 0;
4111 * ipr_set_supported_devs - Send Set Supported Devices for a device
4112 * @ipr_cmd: ipr command struct
4114 * This function send a Set Supported Devices to the adapter
4117 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4119 static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
4121 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4122 struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
4123 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4124 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4125 struct ipr_resource_entry *res = ipr_cmd->u.res;
4127 ipr_cmd->job_step = ipr_ioa_reset_done;
4129 list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
4130 if (!ipr_is_af_dasd_device(res))
4133 ipr_cmd->u.res = res;
4134 ipr_set_sup_dev_dflt(supp_dev, &res->cfgte.std_inq_data.vpids);
4136 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4137 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4138 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4140 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
4141 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
4142 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
4144 ioadl->flags_and_data_len = cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST |
4145 sizeof(struct ipr_supported_device));
4146 ioadl->address = cpu_to_be32(ioa_cfg->vpd_cbs_dma +
4147 offsetof(struct ipr_misc_cbs, supp_dev));
4148 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4149 ioarcb->write_data_transfer_length =
4150 cpu_to_be32(sizeof(struct ipr_supported_device));
4152 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
4153 IPR_SET_SUP_DEVICE_TIMEOUT);
4155 ipr_cmd->job_step = ipr_set_supported_devs;
4156 return IPR_RC_JOB_RETURN;
4159 return IPR_RC_JOB_CONTINUE;
4163 * ipr_get_mode_page - Locate specified mode page
4164 * @mode_pages: mode page buffer
4165 * @page_code: page code to find
4166 * @len: minimum required length for mode page
4169 * pointer to mode page / NULL on failure
4171 static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
4172 u32 page_code, u32 len)
4174 struct ipr_mode_page_hdr *mode_hdr;
4178 if (!mode_pages || (mode_pages->hdr.length == 0))
4181 length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
4182 mode_hdr = (struct ipr_mode_page_hdr *)
4183 (mode_pages->data + mode_pages->hdr.block_desc_len);
4186 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
4187 if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
4191 page_length = (sizeof(struct ipr_mode_page_hdr) +
4192 mode_hdr->page_length);
4193 length -= page_length;
4194 mode_hdr = (struct ipr_mode_page_hdr *)
4195 ((unsigned long)mode_hdr + page_length);
4202 * ipr_check_term_power - Check for term power errors
4203 * @ioa_cfg: ioa config struct
4204 * @mode_pages: IOAFP mode pages buffer
4206 * Check the IOAFP's mode page 28 for term power errors
4211 static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
4212 struct ipr_mode_pages *mode_pages)
4216 struct ipr_dev_bus_entry *bus;
4217 struct ipr_mode_page28 *mode_page;
4219 mode_page = ipr_get_mode_page(mode_pages, 0x28,
4220 sizeof(struct ipr_mode_page28));
4222 entry_length = mode_page->entry_length;
4224 bus = mode_page->bus;
4226 for (i = 0; i < mode_page->num_entries; i++) {
4227 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
4228 dev_err(&ioa_cfg->pdev->dev,
4229 "Term power is absent on scsi bus %d\n",
4233 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
4238 * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
4239 * @ioa_cfg: ioa config struct
4241 * Looks through the config table checking for SES devices. If
4242 * the SES device is in the SES table indicating a maximum SCSI
4243 * bus speed, the speed is limited for the bus.
4248 static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
4253 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
4254 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
4255 ioa_cfg->bus_attr[i].bus_width);
4257 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
4258 ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
4263 * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
4264 * @ioa_cfg: ioa config struct
4265 * @mode_pages: mode page 28 buffer
4267 * Updates mode page 28 based on driver configuration
4272 static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
4273 struct ipr_mode_pages *mode_pages)
4275 int i, entry_length;
4276 struct ipr_dev_bus_entry *bus;
4277 struct ipr_bus_attributes *bus_attr;
4278 struct ipr_mode_page28 *mode_page;
4280 mode_page = ipr_get_mode_page(mode_pages, 0x28,
4281 sizeof(struct ipr_mode_page28));
4283 entry_length = mode_page->entry_length;
4285 /* Loop for each device bus entry */
4286 for (i = 0, bus = mode_page->bus;
4287 i < mode_page->num_entries;
4288 i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
4289 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
4290 dev_err(&ioa_cfg->pdev->dev,
4291 "Invalid resource address reported: 0x%08X\n",
4292 IPR_GET_PHYS_LOC(bus->res_addr));
4296 bus_attr = &ioa_cfg->bus_attr[i];
4297 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
4298 bus->bus_width = bus_attr->bus_width;
4299 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
4300 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
4301 if (bus_attr->qas_enabled)
4302 bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
4304 bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
4309 * ipr_build_mode_select - Build a mode select command
4310 * @ipr_cmd: ipr command struct
4311 * @res_handle: resource handle to send command to
4312 * @parm: Byte 2 of Mode Sense command
4313 * @dma_addr: DMA buffer address
4314 * @xfer_len: data transfer length
4319 static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
4320 u32 res_handle, u8 parm, u32 dma_addr,
4323 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4324 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4326 ioarcb->res_handle = res_handle;
4327 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4328 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4329 ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
4330 ioarcb->cmd_pkt.cdb[1] = parm;
4331 ioarcb->cmd_pkt.cdb[4] = xfer_len;
4333 ioadl->flags_and_data_len =
4334 cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST | xfer_len);
4335 ioadl->address = cpu_to_be32(dma_addr);
4336 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4337 ioarcb->write_data_transfer_length = cpu_to_be32(xfer_len);
4341 * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
4342 * @ipr_cmd: ipr command struct
4344 * This function sets up the SCSI bus attributes and sends
4345 * a Mode Select for Page 28 to activate them.
4350 static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
4352 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4353 struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
4357 if (ioa_cfg->saved_mode_pages) {
4358 memcpy(mode_pages, ioa_cfg->saved_mode_pages,
4359 ioa_cfg->saved_mode_page_len);
4360 length = ioa_cfg->saved_mode_page_len;
4362 ipr_scsi_bus_speed_limit(ioa_cfg);
4363 ipr_check_term_power(ioa_cfg, mode_pages);
4364 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
4365 length = mode_pages->hdr.length + 1;
4366 mode_pages->hdr.length = 0;
4369 ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
4370 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
4373 ipr_cmd->job_step = ipr_set_supported_devs;
4374 ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
4375 struct ipr_resource_entry, queue);
4377 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4380 return IPR_RC_JOB_RETURN;
4384 * ipr_build_mode_sense - Builds a mode sense command
4385 * @ipr_cmd: ipr command struct
4386 * @res: resource entry struct
4387 * @parm: Byte 2 of mode sense command
4388 * @dma_addr: DMA address of mode sense buffer
4389 * @xfer_len: Size of DMA buffer
4394 static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
4396 u8 parm, u32 dma_addr, u8 xfer_len)
4398 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4399 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4401 ioarcb->res_handle = res_handle;
4402 ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
4403 ioarcb->cmd_pkt.cdb[2] = parm;
4404 ioarcb->cmd_pkt.cdb[4] = xfer_len;
4405 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4407 ioadl->flags_and_data_len =
4408 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
4409 ioadl->address = cpu_to_be32(dma_addr);
4410 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4411 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
4415 * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
4416 * @ipr_cmd: ipr command struct
4418 * This function send a Page 28 mode sense to the IOA to
4419 * retrieve SCSI bus attributes.
4424 static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
4426 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4429 ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
4430 0x28, ioa_cfg->vpd_cbs_dma +
4431 offsetof(struct ipr_misc_cbs, mode_pages),
4432 sizeof(struct ipr_mode_pages));
4434 ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
4436 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4439 return IPR_RC_JOB_RETURN;
4443 * ipr_init_res_table - Initialize the resource table
4444 * @ipr_cmd: ipr command struct
4446 * This function looks through the existing resource table, comparing
4447 * it with the config table. This function will take care of old/new
4448 * devices and schedule adding/removing them from the mid-layer
4452 * IPR_RC_JOB_CONTINUE
4454 static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
4456 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4457 struct ipr_resource_entry *res, *temp;
4458 struct ipr_config_table_entry *cfgte;
4463 if (ioa_cfg->cfg_table->hdr.flags & IPR_UCODE_DOWNLOAD_REQ)
4464 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
4466 list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
4467 list_move_tail(&res->queue, &old_res);
4469 for (i = 0; i < ioa_cfg->cfg_table->hdr.num_entries; i++) {
4470 cfgte = &ioa_cfg->cfg_table->dev[i];
4473 list_for_each_entry_safe(res, temp, &old_res, queue) {
4474 if (!memcmp(&res->cfgte.res_addr,
4475 &cfgte->res_addr, sizeof(cfgte->res_addr))) {
4476 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4483 if (list_empty(&ioa_cfg->free_res_q)) {
4484 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
4489 res = list_entry(ioa_cfg->free_res_q.next,
4490 struct ipr_resource_entry, queue);
4491 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4492 ipr_init_res_entry(res);
4497 memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
4500 list_for_each_entry_safe(res, temp, &old_res, queue) {
4502 res->del_from_ml = 1;
4503 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4505 list_move_tail(&res->queue, &ioa_cfg->free_res_q);
4509 ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
4512 return IPR_RC_JOB_CONTINUE;
4516 * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
4517 * @ipr_cmd: ipr command struct
4519 * This function sends a Query IOA Configuration command
4520 * to the adapter to retrieve the IOA configuration table.
4525 static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
4527 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4528 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4529 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4530 struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
4533 dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
4534 ucode_vpd->major_release, ucode_vpd->card_type,
4535 ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
4536 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4537 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4539 ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
4540 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_config_table) >> 8) & 0xff;
4541 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_config_table) & 0xff;
4543 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4544 ioarcb->read_data_transfer_length =
4545 cpu_to_be32(sizeof(struct ipr_config_table));
4547 ioadl->address = cpu_to_be32(ioa_cfg->cfg_table_dma);
4548 ioadl->flags_and_data_len =
4549 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(struct ipr_config_table));
4551 ipr_cmd->job_step = ipr_init_res_table;
4553 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4556 return IPR_RC_JOB_RETURN;
4560 * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
4561 * @ipr_cmd: ipr command struct
4563 * This utility function sends an inquiry to the adapter.
4568 static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
4569 u32 dma_addr, u8 xfer_len)
4571 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4572 struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4575 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4576 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4578 ioarcb->cmd_pkt.cdb[0] = INQUIRY;
4579 ioarcb->cmd_pkt.cdb[1] = flags;
4580 ioarcb->cmd_pkt.cdb[2] = page;
4581 ioarcb->cmd_pkt.cdb[4] = xfer_len;
4583 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4584 ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
4586 ioadl->address = cpu_to_be32(dma_addr);
4587 ioadl->flags_and_data_len =
4588 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
4590 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4595 * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
4596 * @ipr_cmd: ipr command struct
4598 * This function sends a Page 3 inquiry to the adapter
4599 * to retrieve software VPD information.
4602 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4604 static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
4606 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4611 /* Grab the type out of the VPD and store it away */
4612 memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
4614 ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
4616 ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
4618 ipr_ioafp_inquiry(ipr_cmd, 1, 3,
4619 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
4620 sizeof(struct ipr_inquiry_page3));
4623 return IPR_RC_JOB_RETURN;
4627 * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
4628 * @ipr_cmd: ipr command struct
4630 * This function sends a standard inquiry to the adapter.
4635 static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
4637 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4640 ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
4642 ipr_ioafp_inquiry(ipr_cmd, 0, 0,
4643 ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
4644 sizeof(struct ipr_ioa_vpd));
4647 return IPR_RC_JOB_RETURN;
4651 * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
4652 * @ipr_cmd: ipr command struct
4654 * This function send an Identify Host Request Response Queue
4655 * command to establish the HRRQ with the adapter.
4660 static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
4662 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4663 struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4666 dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
4668 ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
4669 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4671 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4672 ioarcb->cmd_pkt.cdb[2] =
4673 ((u32) ioa_cfg->host_rrq_dma >> 24) & 0xff;
4674 ioarcb->cmd_pkt.cdb[3] =
4675 ((u32) ioa_cfg->host_rrq_dma >> 16) & 0xff;
4676 ioarcb->cmd_pkt.cdb[4] =
4677 ((u32) ioa_cfg->host_rrq_dma >> 8) & 0xff;
4678 ioarcb->cmd_pkt.cdb[5] =
4679 ((u32) ioa_cfg->host_rrq_dma) & 0xff;
4680 ioarcb->cmd_pkt.cdb[7] =
4681 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
4682 ioarcb->cmd_pkt.cdb[8] =
4683 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
4685 ipr_cmd->job_step = ipr_ioafp_std_inquiry;
4687 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4690 return IPR_RC_JOB_RETURN;
4694 * ipr_reset_timer_done - Adapter reset timer function
4695 * @ipr_cmd: ipr command struct
4697 * Description: This function is used in adapter reset processing
4698 * for timing events. If the reset_cmd pointer in the IOA
4699 * config struct is not this adapter's we are doing nested
4700 * resets and fail_all_ops will take care of freeing the
4706 static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
4708 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4709 unsigned long lock_flags = 0;
4711 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4713 if (ioa_cfg->reset_cmd == ipr_cmd) {
4714 list_del(&ipr_cmd->queue);
4715 ipr_cmd->done(ipr_cmd);
4718 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4722 * ipr_reset_start_timer - Start a timer for adapter reset job
4723 * @ipr_cmd: ipr command struct
4724 * @timeout: timeout value
4726 * Description: This function is used in adapter reset processing
4727 * for timing events. If the reset_cmd pointer in the IOA
4728 * config struct is not this adapter's we are doing nested
4729 * resets and fail_all_ops will take care of freeing the
4735 static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
4736 unsigned long timeout)
4738 list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
4739 ipr_cmd->done = ipr_reset_ioa_job;
4741 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
4742 ipr_cmd->timer.expires = jiffies + timeout;
4743 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
4744 add_timer(&ipr_cmd->timer);
4748 * ipr_init_ioa_mem - Initialize ioa_cfg control block
4749 * @ioa_cfg: ioa cfg struct
4754 static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
4756 memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
4758 /* Initialize Host RRQ pointers */
4759 ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
4760 ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
4761 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
4762 ioa_cfg->toggle_bit = 1;
4764 /* Zero out config table */
4765 memset(ioa_cfg->cfg_table, 0, sizeof(struct ipr_config_table));
4769 * ipr_reset_enable_ioa - Enable the IOA following a reset.
4770 * @ipr_cmd: ipr command struct
4772 * This function reinitializes some control blocks and
4773 * enables destructive diagnostics on the adapter.
4778 static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
4780 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4781 volatile u32 int_reg;
4784 ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
4785 ipr_init_ioa_mem(ioa_cfg);
4787 ioa_cfg->allow_interrupts = 1;
4788 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4790 if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4791 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
4792 ioa_cfg->regs.clr_interrupt_mask_reg);
4793 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4794 return IPR_RC_JOB_CONTINUE;
4797 /* Enable destructive diagnostics on IOA */
4798 writel(IPR_DOORBELL, ioa_cfg->regs.set_uproc_interrupt_reg);
4800 writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
4801 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4803 dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
4805 ipr_cmd->timer.data = (unsigned long) ipr_cmd;
4806 ipr_cmd->timer.expires = jiffies + IPR_OPERATIONAL_TIMEOUT;
4807 ipr_cmd->timer.function = (void (*)(unsigned long))ipr_timeout;
4808 ipr_cmd->done = ipr_reset_ioa_job;
4809 add_timer(&ipr_cmd->timer);
4810 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
4813 return IPR_RC_JOB_RETURN;
4817 * ipr_reset_wait_for_dump - Wait for a dump to timeout.
4818 * @ipr_cmd: ipr command struct
4820 * This function is invoked when an adapter dump has run out
4821 * of processing time.
4824 * IPR_RC_JOB_CONTINUE
4826 static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
4828 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4830 if (ioa_cfg->sdt_state == GET_DUMP)
4831 ioa_cfg->sdt_state = ABORT_DUMP;
4833 ipr_cmd->job_step = ipr_reset_alert;
4835 return IPR_RC_JOB_CONTINUE;
4839 * ipr_unit_check_no_data - Log a unit check/no data error log
4840 * @ioa_cfg: ioa config struct
4842 * Logs an error indicating the adapter unit checked, but for some
4843 * reason, we were unable to fetch the unit check buffer.
4848 static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
4850 ioa_cfg->errors_logged++;
4851 dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
4855 * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
4856 * @ioa_cfg: ioa config struct
4858 * Fetches the unit check buffer from the adapter by clocking the data
4859 * through the mailbox register.
4864 static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
4866 unsigned long mailbox;
4867 struct ipr_hostrcb *hostrcb;
4868 struct ipr_uc_sdt sdt;
4871 mailbox = readl(ioa_cfg->ioa_mailbox);
4873 if (!ipr_sdt_is_fmt2(mailbox)) {
4874 ipr_unit_check_no_data(ioa_cfg);
4878 memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
4879 rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (u32 *) &sdt,
4880 (sizeof(struct ipr_uc_sdt)) / sizeof(u32));
4882 if (rc || (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE) ||
4883 !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY)) {
4884 ipr_unit_check_no_data(ioa_cfg);
4888 /* Find length of the first sdt entry (UC buffer) */
4889 length = (be32_to_cpu(sdt.entry[0].end_offset) -
4890 be32_to_cpu(sdt.entry[0].bar_str_offset)) & IPR_FMT2_MBX_ADDR_MASK;
4892 hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
4893 struct ipr_hostrcb, queue);
4894 list_del(&hostrcb->queue);
4895 memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
4897 rc = ipr_get_ldump_data_section(ioa_cfg,
4898 be32_to_cpu(sdt.entry[0].bar_str_offset),
4899 (u32 *)&hostrcb->hcam,
4900 min(length, (int)sizeof(hostrcb->hcam)) / sizeof(u32));
4903 ipr_handle_log_data(ioa_cfg, hostrcb);
4905 ipr_unit_check_no_data(ioa_cfg);
4907 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
4911 * ipr_reset_restore_cfg_space - Restore PCI config space.
4912 * @ipr_cmd: ipr command struct
4914 * Description: This function restores the saved PCI config space of
4915 * the adapter, fails all outstanding ops back to the callers, and
4916 * fetches the dump/unit check if applicable to this reset.
4919 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4921 static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
4923 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4927 rc = pci_restore_state(ioa_cfg->pdev);
4929 if (rc != PCIBIOS_SUCCESSFUL) {
4930 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4931 return IPR_RC_JOB_CONTINUE;
4934 if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
4935 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4936 return IPR_RC_JOB_CONTINUE;
4939 ipr_fail_all_ops(ioa_cfg);
4941 if (ioa_cfg->ioa_unit_checked) {
4942 ioa_cfg->ioa_unit_checked = 0;
4943 ipr_get_unit_check_buffer(ioa_cfg);
4944 ipr_cmd->job_step = ipr_reset_alert;
4945 ipr_reset_start_timer(ipr_cmd, 0);
4946 return IPR_RC_JOB_RETURN;
4949 if (ioa_cfg->in_ioa_bringdown) {
4950 ipr_cmd->job_step = ipr_ioa_bringdown_done;
4952 ipr_cmd->job_step = ipr_reset_enable_ioa;
4954 if (GET_DUMP == ioa_cfg->sdt_state) {
4955 ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
4956 ipr_cmd->job_step = ipr_reset_wait_for_dump;
4957 schedule_work(&ioa_cfg->work_q);
4958 return IPR_RC_JOB_RETURN;
4963 return IPR_RC_JOB_CONTINUE;
4967 * ipr_reset_start_bist - Run BIST on the adapter.
4968 * @ipr_cmd: ipr command struct
4970 * Description: This function runs BIST on the adapter, then delays 2 seconds.
4973 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4975 static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
4977 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4981 rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
4983 if (rc != PCIBIOS_SUCCESSFUL) {
4984 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4985 rc = IPR_RC_JOB_CONTINUE;
4987 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
4988 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
4989 rc = IPR_RC_JOB_RETURN;
4997 * ipr_reset_allowed - Query whether or not IOA can be reset
4998 * @ioa_cfg: ioa config struct
5001 * 0 if reset not allowed / non-zero if reset is allowed
5003 static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
5005 volatile u32 temp_reg;
5007 temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5008 return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
5012 * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
5013 * @ipr_cmd: ipr command struct
5015 * Description: This function waits for adapter permission to run BIST,
5016 * then runs BIST. If the adapter does not give permission after a
5017 * reasonable time, we will reset the adapter anyway. The impact of
5018 * resetting the adapter without warning the adapter is the risk of
5019 * losing the persistent error log on the adapter. If the adapter is
5020 * reset while it is writing to the flash on the adapter, the flash
5021 * segment will have bad ECC and be zeroed.
5024 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5026 static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
5028 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5029 int rc = IPR_RC_JOB_RETURN;
5031 if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
5032 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
5033 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5035 ipr_cmd->job_step = ipr_reset_start_bist;
5036 rc = IPR_RC_JOB_CONTINUE;
5043 * ipr_reset_alert_part2 - Alert the adapter of a pending reset
5044 * @ipr_cmd: ipr command struct
5046 * Description: This function alerts the adapter that it will be reset.
5047 * If memory space is not currently enabled, proceed directly
5048 * to running BIST on the adapter. The timer must always be started
5049 * so we guarantee we do not run BIST from ipr_isr.
5054 static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
5056 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5061 rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
5063 if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
5064 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
5065 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg);
5066 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
5068 ipr_cmd->job_step = ipr_reset_start_bist;
5071 ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
5072 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5075 return IPR_RC_JOB_RETURN;
5079 * ipr_reset_ucode_download_done - Microcode download completion
5080 * @ipr_cmd: ipr command struct
5082 * Description: This function unmaps the microcode download buffer.
5085 * IPR_RC_JOB_CONTINUE
5087 static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
5089 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5090 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5092 pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
5093 sglist->num_sg, DMA_TO_DEVICE);
5095 ipr_cmd->job_step = ipr_reset_alert;
5096 return IPR_RC_JOB_CONTINUE;
5100 * ipr_reset_ucode_download - Download microcode to the adapter
5101 * @ipr_cmd: ipr command struct
5103 * Description: This function checks to see if it there is microcode
5104 * to download to the adapter. If there is, a download is performed.
5107 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5109 static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
5111 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5112 struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5115 ipr_cmd->job_step = ipr_reset_alert;
5118 return IPR_RC_JOB_CONTINUE;
5120 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5121 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5122 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
5123 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
5124 ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
5125 ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
5126 ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
5128 if (ipr_map_ucode_buffer(ipr_cmd, sglist, sglist->buffer_len)) {
5129 dev_err(&ioa_cfg->pdev->dev,
5130 "Failed to map microcode download buffer\n");
5131 return IPR_RC_JOB_CONTINUE;
5134 ipr_cmd->job_step = ipr_reset_ucode_download_done;
5136 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
5137 IPR_WRITE_BUFFER_TIMEOUT);
5140 return IPR_RC_JOB_RETURN;
5144 * ipr_reset_shutdown_ioa - Shutdown the adapter
5145 * @ipr_cmd: ipr command struct
5147 * Description: This function issues an adapter shutdown of the
5148 * specified type to the specified adapter as part of the
5149 * adapter reset job.
5152 * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5154 static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
5156 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5157 enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
5158 unsigned long timeout;
5159 int rc = IPR_RC_JOB_CONTINUE;
5162 if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
5163 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5164 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5165 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
5166 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
5168 if (shutdown_type == IPR_SHUTDOWN_ABBREV)
5169 timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
5170 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
5171 timeout = IPR_INTERNAL_TIMEOUT;
5173 timeout = IPR_SHUTDOWN_TIMEOUT;
5175 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
5177 rc = IPR_RC_JOB_RETURN;
5178 ipr_cmd->job_step = ipr_reset_ucode_download;
5180 ipr_cmd->job_step = ipr_reset_alert;
5187 * ipr_reset_ioa_job - Adapter reset job
5188 * @ipr_cmd: ipr command struct
5190 * Description: This function is the job router for the adapter reset job.
5195 static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
5198 unsigned long scratch = ipr_cmd->u.scratch;
5199 struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5202 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5204 if (ioa_cfg->reset_cmd != ipr_cmd) {
5206 * We are doing nested adapter resets and this is
5207 * not the current reset job.
5209 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5213 if (IPR_IOASC_SENSE_KEY(ioasc)) {
5214 dev_err(&ioa_cfg->pdev->dev,
5215 "0x%02X failed with IOASC: 0x%08X\n",
5216 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
5218 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5219 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5223 ipr_reinit_ipr_cmnd(ipr_cmd);
5224 ipr_cmd->u.scratch = scratch;
5225 rc = ipr_cmd->job_step(ipr_cmd);
5226 } while(rc == IPR_RC_JOB_CONTINUE);
5230 * _ipr_initiate_ioa_reset - Initiate an adapter reset
5231 * @ioa_cfg: ioa config struct
5232 * @job_step: first job step of reset job
5233 * @shutdown_type: shutdown type
5235 * Description: This function will initiate the reset of the given adapter
5236 * starting at the selected job step.
5237 * If the caller needs to wait on the completion of the reset,
5238 * the caller must sleep on the reset_wait_q.
5243 static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5244 int (*job_step) (struct ipr_cmnd *),
5245 enum ipr_shutdown_type shutdown_type)
5247 struct ipr_cmnd *ipr_cmd;
5249 ioa_cfg->in_reset_reload = 1;
5250 ioa_cfg->allow_cmds = 0;
5251 scsi_block_requests(ioa_cfg->host);
5253 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5254 ioa_cfg->reset_cmd = ipr_cmd;
5255 ipr_cmd->job_step = job_step;
5256 ipr_cmd->u.shutdown_type = shutdown_type;
5258 ipr_reset_ioa_job(ipr_cmd);
5262 * ipr_initiate_ioa_reset - Initiate an adapter reset
5263 * @ioa_cfg: ioa config struct
5264 * @shutdown_type: shutdown type
5266 * Description: This function will initiate the reset of the given adapter.
5267 * If the caller needs to wait on the completion of the reset,
5268 * the caller must sleep on the reset_wait_q.
5273 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5274 enum ipr_shutdown_type shutdown_type)
5276 if (ioa_cfg->ioa_is_dead)
5279 if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
5280 ioa_cfg->sdt_state = ABORT_DUMP;
5282 if (ioa_cfg->reset_retries++ > IPR_NUM_RESET_RELOAD_RETRIES) {
5283 dev_err(&ioa_cfg->pdev->dev,
5284 "IOA taken offline - error recovery failed\n");
5286 ioa_cfg->reset_retries = 0;
5287 ioa_cfg->ioa_is_dead = 1;
5289 if (ioa_cfg->in_ioa_bringdown) {
5290 ioa_cfg->reset_cmd = NULL;
5291 ioa_cfg->in_reset_reload = 0;
5292 ipr_fail_all_ops(ioa_cfg);
5293 wake_up_all(&ioa_cfg->reset_wait_q);
5295 spin_unlock_irq(ioa_cfg->host->host_lock);
5296 scsi_unblock_requests(ioa_cfg->host);
5297 spin_lock_irq(ioa_cfg->host->host_lock);
5300 ioa_cfg->in_ioa_bringdown = 1;
5301 shutdown_type = IPR_SHUTDOWN_NONE;
5305 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
5310 * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
5311 * @ioa_cfg: ioa cfg struct
5313 * Description: This is the second phase of adapter intialization
5314 * This function takes care of initilizing the adapter to the point
5315 * where it can accept new commands.
5318 * 0 on sucess / -EIO on failure
5320 static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
5323 unsigned long host_lock_flags = 0;
5326 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5327 dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
5328 _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa, IPR_SHUTDOWN_NONE);
5330 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5331 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5332 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5334 if (ioa_cfg->ioa_is_dead) {
5336 } else if (ipr_invalid_adapter(ioa_cfg)) {
5340 dev_err(&ioa_cfg->pdev->dev,
5341 "Adapter not supported in this hardware configuration.\n");
5344 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5351 * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
5352 * @ioa_cfg: ioa config struct
5357 static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5361 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
5362 if (ioa_cfg->ipr_cmnd_list[i])
5363 pci_pool_free(ioa_cfg->ipr_cmd_pool,
5364 ioa_cfg->ipr_cmnd_list[i],
5365 ioa_cfg->ipr_cmnd_list_dma[i]);
5367 ioa_cfg->ipr_cmnd_list[i] = NULL;
5370 if (ioa_cfg->ipr_cmd_pool)
5371 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
5373 ioa_cfg->ipr_cmd_pool = NULL;
5377 * ipr_free_mem - Frees memory allocated for an adapter
5378 * @ioa_cfg: ioa cfg struct
5383 static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
5387 kfree(ioa_cfg->res_entries);
5388 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
5389 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5390 ipr_free_cmd_blks(ioa_cfg);
5391 pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5392 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5393 pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_config_table),
5395 ioa_cfg->cfg_table_dma);
5397 for (i = 0; i < IPR_NUM_HCAMS; i++) {
5398 pci_free_consistent(ioa_cfg->pdev,
5399 sizeof(struct ipr_hostrcb),
5400 ioa_cfg->hostrcb[i],
5401 ioa_cfg->hostrcb_dma[i]);
5404 ipr_free_dump(ioa_cfg);
5405 kfree(ioa_cfg->saved_mode_pages);
5406 kfree(ioa_cfg->trace);
5410 * ipr_free_all_resources - Free all allocated resources for an adapter.
5411 * @ipr_cmd: ipr command struct
5413 * This function frees all allocated resources for the
5414 * specified adapter.
5419 static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
5421 struct pci_dev *pdev = ioa_cfg->pdev;
5424 free_irq(pdev->irq, ioa_cfg);
5425 iounmap(ioa_cfg->hdw_dma_regs);
5426 pci_release_regions(pdev);
5427 ipr_free_mem(ioa_cfg);
5428 scsi_host_put(ioa_cfg->host);
5429 pci_disable_device(pdev);
5434 * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
5435 * @ioa_cfg: ioa config struct
5438 * 0 on success / -ENOMEM on allocation failure
5440 static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5442 struct ipr_cmnd *ipr_cmd;
5443 struct ipr_ioarcb *ioarcb;
5444 dma_addr_t dma_addr;
5447 ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
5448 sizeof(struct ipr_cmnd), 8, 0);
5450 if (!ioa_cfg->ipr_cmd_pool)
5453 for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
5454 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, SLAB_KERNEL, &dma_addr);
5457 ipr_free_cmd_blks(ioa_cfg);
5461 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
5462 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
5463 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
5465 ioarcb = &ipr_cmd->ioarcb;
5466 ioarcb->ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
5467 ioarcb->host_response_handle = cpu_to_be32(i << 2);
5468 ioarcb->write_ioadl_addr =
5469 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
5470 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5471 ioarcb->ioasa_host_pci_addr =
5472 cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
5473 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
5474 ipr_cmd->cmd_index = i;
5475 ipr_cmd->ioa_cfg = ioa_cfg;
5476 ipr_cmd->sense_buffer_dma = dma_addr +
5477 offsetof(struct ipr_cmnd, sense_buffer);
5479 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5486 * ipr_alloc_mem - Allocate memory for an adapter
5487 * @ioa_cfg: ioa config struct
5490 * 0 on success / non-zero for error
5492 static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
5494 struct pci_dev *pdev = ioa_cfg->pdev;
5495 int i, rc = -ENOMEM;
5498 ioa_cfg->res_entries = kmalloc(sizeof(struct ipr_resource_entry) *
5499 IPR_MAX_PHYSICAL_DEVS, GFP_KERNEL);
5501 if (!ioa_cfg->res_entries)
5504 memset(ioa_cfg->res_entries, 0,
5505 sizeof(struct ipr_resource_entry) * IPR_MAX_PHYSICAL_DEVS);
5507 for (i = 0; i < IPR_MAX_PHYSICAL_DEVS; i++)
5508 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
5510 ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
5511 sizeof(struct ipr_misc_cbs),
5512 &ioa_cfg->vpd_cbs_dma);
5514 if (!ioa_cfg->vpd_cbs)
5515 goto out_free_res_entries;
5517 if (ipr_alloc_cmd_blks(ioa_cfg))
5518 goto out_free_vpd_cbs;
5520 ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
5521 sizeof(u32) * IPR_NUM_CMD_BLKS,
5522 &ioa_cfg->host_rrq_dma);
5524 if (!ioa_cfg->host_rrq)
5525 goto out_ipr_free_cmd_blocks;
5527 ioa_cfg->cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
5528 sizeof(struct ipr_config_table),
5529 &ioa_cfg->cfg_table_dma);
5531 if (!ioa_cfg->cfg_table)
5532 goto out_free_host_rrq;
5534 for (i = 0; i < IPR_NUM_HCAMS; i++) {
5535 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
5536 sizeof(struct ipr_hostrcb),
5537 &ioa_cfg->hostrcb_dma[i]);
5539 if (!ioa_cfg->hostrcb[i])
5540 goto out_free_hostrcb_dma;
5542 ioa_cfg->hostrcb[i]->hostrcb_dma =
5543 ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
5544 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
5547 ioa_cfg->trace = kmalloc(sizeof(struct ipr_trace_entry) *
5548 IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
5550 if (!ioa_cfg->trace)
5551 goto out_free_hostrcb_dma;
5553 memset(ioa_cfg->trace, 0,
5554 sizeof(struct ipr_trace_entry) * IPR_NUM_TRACE_ENTRIES);
5561 out_free_hostrcb_dma:
5563 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
5564 ioa_cfg->hostrcb[i],
5565 ioa_cfg->hostrcb_dma[i]);
5567 pci_free_consistent(pdev, sizeof(struct ipr_config_table),
5568 ioa_cfg->cfg_table, ioa_cfg->cfg_table_dma);
5570 pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5571 ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5572 out_ipr_free_cmd_blocks:
5573 ipr_free_cmd_blks(ioa_cfg);
5575 pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
5576 ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5577 out_free_res_entries:
5578 kfree(ioa_cfg->res_entries);
5583 * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
5584 * @ioa_cfg: ioa config struct
5589 static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
5593 for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
5594 ioa_cfg->bus_attr[i].bus = i;
5595 ioa_cfg->bus_attr[i].qas_enabled = 0;
5596 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
5597 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
5598 ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
5600 ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
5605 * ipr_init_ioa_cfg - Initialize IOA config struct
5606 * @ioa_cfg: ioa config struct
5607 * @host: scsi host struct
5608 * @pdev: PCI dev struct
5613 static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
5614 struct Scsi_Host *host, struct pci_dev *pdev)
5616 const struct ipr_interrupt_offsets *p;
5617 struct ipr_interrupts *t;
5620 ioa_cfg->host = host;
5621 ioa_cfg->pdev = pdev;
5622 ioa_cfg->log_level = ipr_log_level;
5623 sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
5624 sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
5625 sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
5626 sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
5627 sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
5628 sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
5629 sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
5630 sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
5632 INIT_LIST_HEAD(&ioa_cfg->free_q);
5633 INIT_LIST_HEAD(&ioa_cfg->pending_q);
5634 INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
5635 INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
5636 INIT_LIST_HEAD(&ioa_cfg->free_res_q);
5637 INIT_LIST_HEAD(&ioa_cfg->used_res_q);
5638 INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread, ioa_cfg);
5639 init_waitqueue_head(&ioa_cfg->reset_wait_q);
5640 ioa_cfg->sdt_state = INACTIVE;
5642 ipr_initialize_bus_attr(ioa_cfg);
5644 host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
5645 host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
5646 host->max_channel = IPR_MAX_BUS_TO_SCAN;
5647 host->unique_id = host->host_no;
5648 host->max_cmd_len = IPR_MAX_CDB_LEN;
5649 pci_set_drvdata(pdev, ioa_cfg);
5651 p = &ioa_cfg->chip_cfg->regs;
5653 base = ioa_cfg->hdw_dma_regs;
5655 t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
5656 t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
5657 t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
5658 t->clr_interrupt_reg = base + p->clr_interrupt_reg;
5659 t->sense_interrupt_reg = base + p->sense_interrupt_reg;
5660 t->ioarrin_reg = base + p->ioarrin_reg;
5661 t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
5662 t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
5663 t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
5667 * ipr_probe_ioa - Allocates memory and does first stage of initialization
5668 * @pdev: PCI device struct
5669 * @dev_id: PCI device id struct
5672 * 0 on success / non-zero on failure
5674 static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
5675 const struct pci_device_id *dev_id)
5677 struct ipr_ioa_cfg *ioa_cfg;
5678 struct Scsi_Host *host;
5679 unsigned long ipr_regs_pci;
5680 void __iomem *ipr_regs;
5681 u32 rc = PCIBIOS_SUCCESSFUL;
5685 if ((rc = pci_enable_device(pdev))) {
5686 dev_err(&pdev->dev, "Cannot enable adapter\n");
5690 dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
5692 host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
5695 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
5700 ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
5701 memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
5703 ioa_cfg->chip_cfg = (const struct ipr_chip_cfg_t *)dev_id->driver_data;
5705 ipr_regs_pci = pci_resource_start(pdev, 0);
5707 rc = pci_request_regions(pdev, IPR_NAME);
5710 "Couldn't register memory range of registers\n");
5711 goto out_scsi_host_put;
5714 ipr_regs = ioremap(ipr_regs_pci, pci_resource_len(pdev, 0));
5718 "Couldn't map memory range of registers\n");
5720 goto out_release_regions;
5723 ioa_cfg->hdw_dma_regs = ipr_regs;
5724 ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
5725 ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
5727 ipr_init_ioa_cfg(ioa_cfg, host, pdev);
5729 pci_set_master(pdev);
5731 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5733 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
5737 rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
5738 ioa_cfg->chip_cfg->cache_line_size);
5740 if (rc != PCIBIOS_SUCCESSFUL) {
5741 dev_err(&pdev->dev, "Write of cache line size failed\n");
5746 /* Save away PCI config space for use following IOA reset */
5747 rc = pci_save_state(pdev);
5749 if (rc != PCIBIOS_SUCCESSFUL) {
5750 dev_err(&pdev->dev, "Failed to save PCI config space\n");
5755 if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
5758 if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
5761 rc = ipr_alloc_mem(ioa_cfg);
5764 "Couldn't allocate enough memory for device driver!\n");
5768 ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
5769 rc = request_irq(pdev->irq, ipr_isr, SA_SHIRQ, IPR_NAME, ioa_cfg);
5772 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
5777 spin_lock(&ipr_driver_lock);
5778 list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
5779 spin_unlock(&ipr_driver_lock);
5786 ipr_free_mem(ioa_cfg);
5789 out_release_regions:
5790 pci_release_regions(pdev);
5792 scsi_host_put(host);
5794 pci_disable_device(pdev);
5799 * ipr_scan_vsets - Scans for VSET devices
5800 * @ioa_cfg: ioa config struct
5802 * Description: Since the VSET resources do not follow SAM in that we can have
5803 * sparse LUNs with no LUN 0, we have to scan for these ourselves.
5808 static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
5812 for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
5813 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
5814 scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
5818 * ipr_initiate_ioa_bringdown - Bring down an adapter
5819 * @ioa_cfg: ioa config struct
5820 * @shutdown_type: shutdown type
5822 * Description: This function will initiate bringing down the adapter.
5823 * This consists of issuing an IOA shutdown to the adapter
5824 * to flush the cache, and running BIST.
5825 * If the caller needs to wait on the completion of the reset,
5826 * the caller must sleep on the reset_wait_q.
5831 static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
5832 enum ipr_shutdown_type shutdown_type)
5835 if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
5836 ioa_cfg->sdt_state = ABORT_DUMP;
5837 ioa_cfg->reset_retries = 0;
5838 ioa_cfg->in_ioa_bringdown = 1;
5839 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
5844 * __ipr_remove - Remove a single adapter
5845 * @pdev: pci device struct
5847 * Adapter hot plug remove entry point.
5852 static void __ipr_remove(struct pci_dev *pdev)
5854 unsigned long host_lock_flags = 0;
5855 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
5858 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5859 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
5861 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5862 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5863 spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5865 spin_lock(&ipr_driver_lock);
5866 list_del(&ioa_cfg->queue);
5867 spin_unlock(&ipr_driver_lock);
5869 if (ioa_cfg->sdt_state == ABORT_DUMP)
5870 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
5871 spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5873 ipr_free_all_resources(ioa_cfg);
5879 * ipr_remove - IOA hot plug remove entry point
5880 * @pdev: pci device struct
5882 * Adapter hot plug remove entry point.
5887 static void ipr_remove(struct pci_dev *pdev)
5889 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
5893 ioa_cfg->allow_cmds = 0;
5894 flush_scheduled_work();
5895 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5897 ipr_remove_dump_file(&ioa_cfg->host->shost_classdev.kobj,
5899 scsi_remove_host(ioa_cfg->host);
5907 * ipr_probe - Adapter hot plug add entry point
5910 * 0 on success / non-zero on failure
5912 static int __devinit ipr_probe(struct pci_dev *pdev,
5913 const struct pci_device_id *dev_id)
5915 struct ipr_ioa_cfg *ioa_cfg;
5918 rc = ipr_probe_ioa(pdev, dev_id);
5923 ioa_cfg = pci_get_drvdata(pdev);
5924 rc = ipr_probe_ioa_part2(ioa_cfg);
5931 rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
5938 rc = ipr_create_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5942 scsi_remove_host(ioa_cfg->host);
5947 rc = ipr_create_dump_file(&ioa_cfg->host->shost_classdev.kobj,
5951 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5953 scsi_remove_host(ioa_cfg->host);
5958 scsi_scan_host(ioa_cfg->host);
5959 ipr_scan_vsets(ioa_cfg);
5960 scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
5961 ioa_cfg->allow_ml_add_del = 1;
5962 schedule_work(&ioa_cfg->work_q);
5967 * ipr_shutdown - Shutdown handler.
5968 * @dev: device struct
5970 * This function is invoked upon system shutdown/reboot. It will issue
5971 * an adapter shutdown to the adapter to flush the write cache.
5976 static void ipr_shutdown(struct device *dev)
5978 struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(to_pci_dev(dev));
5979 unsigned long lock_flags = 0;
5981 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5982 ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
5983 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5984 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5987 static struct pci_device_id ipr_pci_table[] __devinitdata = {
5988 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
5989 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702,
5990 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
5991 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
5992 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E,
5993 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
5994 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
5995 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703,
5996 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
5997 { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
5998 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D,
5999 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6000 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6001 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B,
6002 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6003 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
6004 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780,
6005 0, 0, (kernel_ulong_t)&ipr_chip_cfg[1] },
6006 { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
6007 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_570F,
6008 0, 0, (kernel_ulong_t)&ipr_chip_cfg[1] },
6011 MODULE_DEVICE_TABLE(pci, ipr_pci_table);
6013 static struct pci_driver ipr_driver = {
6015 .id_table = ipr_pci_table,
6017 .remove = ipr_remove,
6019 .shutdown = ipr_shutdown,
6024 * ipr_init - Module entry point
6027 * 0 on success / negative value on failure
6029 static int __init ipr_init(void)
6031 ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
6032 IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
6034 return pci_module_init(&ipr_driver);
6038 * ipr_exit - Module unload
6040 * Module unload entry point.
6045 static void __exit ipr_exit(void)
6047 pci_unregister_driver(&ipr_driver);
6050 module_init(ipr_init);
6051 module_exit(ipr_exit);