2 * pmcraid.c -- driver for PMC Sierra MaxRAID controller adapters
4 * Written By: Anil Ravindranath<anil_ravindranath@pmc-sierra.com>
7 * Copyright (C) 2008, 2009 PMC Sierra Inc
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/errno.h>
29 #include <linux/kernel.h>
30 #include <linux/ioport.h>
31 #include <linux/delay.h>
32 #include <linux/pci.h>
33 #include <linux/wait.h>
34 #include <linux/spinlock.h>
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
37 #include <linux/blkdev.h>
38 #include <linux/firmware.h>
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
41 #include <linux/hdreg.h>
43 #include <linux/slab.h>
45 #include <asm/processor.h>
46 #include <linux/libata.h>
47 #include <linux/mutex.h>
48 #include <linux/ktime.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_host.h>
51 #include <scsi/scsi_device.h>
52 #include <scsi/scsi_tcq.h>
53 #include <scsi/scsi_eh.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsicam.h>
60 * Module configuration parameters
62 static unsigned int pmcraid_debug_log
;
63 static unsigned int pmcraid_disable_aen
;
64 static unsigned int pmcraid_log_level
= IOASC_LOG_LEVEL_MUST
;
65 static unsigned int pmcraid_enable_msix
;
68 * Data structures to support multiple adapters by the LLD.
69 * pmcraid_adapter_count - count of configured adapters
71 static atomic_t pmcraid_adapter_count
= ATOMIC_INIT(0);
74 * Supporting user-level control interface through IOCTL commands.
75 * pmcraid_major - major number to use
76 * pmcraid_minor - minor number(s) to use
78 static unsigned int pmcraid_major
;
79 static struct class *pmcraid_class
;
80 static DECLARE_BITMAP(pmcraid_minor
, PMCRAID_MAX_ADAPTERS
);
85 MODULE_AUTHOR("Anil Ravindranath<anil_ravindranath@pmc-sierra.com>");
86 MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(PMCRAID_DRIVER_VERSION
);
90 module_param_named(log_level
, pmcraid_log_level
, uint
, (S_IRUGO
| S_IWUSR
));
91 MODULE_PARM_DESC(log_level
,
92 "Enables firmware error code logging, default :1 high-severity"
93 " errors, 2: all errors including high-severity errors,"
94 " 0: disables logging");
96 module_param_named(debug
, pmcraid_debug_log
, uint
, (S_IRUGO
| S_IWUSR
));
97 MODULE_PARM_DESC(debug
,
98 "Enable driver verbose message logging. Set 1 to enable."
101 module_param_named(disable_aen
, pmcraid_disable_aen
, uint
, (S_IRUGO
| S_IWUSR
));
102 MODULE_PARM_DESC(disable_aen
,
103 "Disable driver aen notifications to apps. Set 1 to disable."
106 /* chip specific constants for PMC MaxRAID controllers (same for
109 static struct pmcraid_chip_details pmcraid_chip_cfg
[] = {
114 .global_intr_mask
= 0x00034,
115 .ioa_host_intr
= 0x0009C,
116 .ioa_host_intr_clr
= 0x000A0,
117 .ioa_host_msix_intr
= 0x7FC40,
118 .ioa_host_mask
= 0x7FC28,
119 .ioa_host_mask_clr
= 0x7FC28,
120 .host_ioa_intr
= 0x00020,
121 .host_ioa_intr_clr
= 0x00020,
122 .transop_timeout
= 300
127 * PCI device ids supported by pmcraid driver
129 static struct pci_device_id pmcraid_pci_table
[] = {
130 { PCI_DEVICE(PCI_VENDOR_ID_PMC
, PCI_DEVICE_ID_PMC_MAXRAID
),
131 0, 0, (kernel_ulong_t
)&pmcraid_chip_cfg
[0]
136 MODULE_DEVICE_TABLE(pci
, pmcraid_pci_table
);
141 * pmcraid_slave_alloc - Prepare for commands to a device
142 * @scsi_dev: scsi device struct
144 * This function is called by mid-layer prior to sending any command to the new
145 * device. Stores resource entry details of the device in scsi_device struct.
146 * Queuecommand uses the resource handle and other details to fill up IOARCB
147 * while sending commands to the device.
150 * 0 on success / -ENXIO if device does not exist
152 static int pmcraid_slave_alloc(struct scsi_device
*scsi_dev
)
154 struct pmcraid_resource_entry
*temp
, *res
= NULL
;
155 struct pmcraid_instance
*pinstance
;
157 unsigned long lock_flags
;
161 pinstance
= shost_priv(scsi_dev
->host
);
163 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
165 /* Driver exposes VSET and GSCSI resources only; all other device types
166 * are not exposed. Resource list is synchronized using resource lock
167 * so any traversal or modifications to the list should be done inside
170 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
171 list_for_each_entry(temp
, &pinstance
->used_res_q
, queue
) {
173 /* do not expose VSETs with order-ids > MAX_VSET_TARGETS */
174 if (RES_IS_VSET(temp
->cfg_entry
)) {
175 if (fw_version
<= PMCRAID_FW_VERSION_1
)
176 target
= temp
->cfg_entry
.unique_flags1
;
178 target
= le16_to_cpu(temp
->cfg_entry
.array_id
) & 0xFF;
180 if (target
> PMCRAID_MAX_VSET_TARGETS
)
182 bus
= PMCRAID_VSET_BUS_ID
;
184 } else if (RES_IS_GSCSI(temp
->cfg_entry
)) {
185 target
= RES_TARGET(temp
->cfg_entry
.resource_address
);
186 bus
= PMCRAID_PHYS_BUS_ID
;
187 lun
= RES_LUN(temp
->cfg_entry
.resource_address
);
192 if (bus
== scsi_dev
->channel
&&
193 target
== scsi_dev
->id
&&
194 lun
== scsi_dev
->lun
) {
201 res
->scsi_dev
= scsi_dev
;
202 scsi_dev
->hostdata
= res
;
203 res
->change_detected
= 0;
204 atomic_set(&res
->read_failures
, 0);
205 atomic_set(&res
->write_failures
, 0);
208 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
213 * pmcraid_slave_configure - Configures a SCSI device
214 * @scsi_dev: scsi device struct
216 * This function is executed by SCSI mid layer just after a device is first
217 * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
218 * timeout value (default 30s) will be over-written to a higher value (60s)
219 * and max_sectors value will be over-written to 512. It also sets queue depth
220 * to host->cmd_per_lun value
225 static int pmcraid_slave_configure(struct scsi_device
*scsi_dev
)
227 struct pmcraid_resource_entry
*res
= scsi_dev
->hostdata
;
232 /* LLD exposes VSETs and Enclosure devices only */
233 if (RES_IS_GSCSI(res
->cfg_entry
) &&
234 scsi_dev
->type
!= TYPE_ENCLOSURE
)
237 pmcraid_info("configuring %x:%x:%x:%x\n",
238 scsi_dev
->host
->unique_id
,
243 if (RES_IS_GSCSI(res
->cfg_entry
)) {
244 scsi_dev
->allow_restart
= 1;
245 } else if (RES_IS_VSET(res
->cfg_entry
)) {
246 scsi_dev
->allow_restart
= 1;
247 blk_queue_rq_timeout(scsi_dev
->request_queue
,
248 PMCRAID_VSET_IO_TIMEOUT
);
249 blk_queue_max_hw_sectors(scsi_dev
->request_queue
,
250 PMCRAID_VSET_MAX_SECTORS
);
254 * We never want to report TCQ support for these types of devices.
256 if (!RES_IS_GSCSI(res
->cfg_entry
) && !RES_IS_VSET(res
->cfg_entry
))
257 scsi_dev
->tagged_supported
= 0;
263 * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
265 * @scsi_dev: scsi device struct
267 * This is called by mid-layer before removing a device. Pointer assignments
268 * done in pmcraid_slave_alloc will be reset to NULL here.
273 static void pmcraid_slave_destroy(struct scsi_device
*scsi_dev
)
275 struct pmcraid_resource_entry
*res
;
277 res
= (struct pmcraid_resource_entry
*)scsi_dev
->hostdata
;
280 res
->scsi_dev
= NULL
;
282 scsi_dev
->hostdata
= NULL
;
286 * pmcraid_change_queue_depth - Change the device's queue depth
287 * @scsi_dev: scsi device struct
288 * @depth: depth to set
293 static int pmcraid_change_queue_depth(struct scsi_device
*scsi_dev
, int depth
)
295 if (depth
> PMCRAID_MAX_CMD_PER_LUN
)
296 depth
= PMCRAID_MAX_CMD_PER_LUN
;
297 return scsi_change_queue_depth(scsi_dev
, depth
);
301 * pmcraid_init_cmdblk - initializes a command block
303 * @cmd: pointer to struct pmcraid_cmd to be initialized
304 * @index: if >=0 first time initialization; otherwise reinitialization
309 static void pmcraid_init_cmdblk(struct pmcraid_cmd
*cmd
, int index
)
311 struct pmcraid_ioarcb
*ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
312 dma_addr_t dma_addr
= cmd
->ioa_cb_bus_addr
;
315 /* first time initialization (called from probe) */
317 offsetof(struct pmcraid_control_block
, ioasa
);
320 ioarcb
->response_handle
= cpu_to_le32(index
<< 2);
321 ioarcb
->ioarcb_bus_addr
= cpu_to_le64(dma_addr
);
322 ioarcb
->ioasa_bus_addr
= cpu_to_le64(dma_addr
+ ioasa_offset
);
323 ioarcb
->ioasa_len
= cpu_to_le16(sizeof(struct pmcraid_ioasa
));
325 /* re-initialization of various lengths, called once command is
328 memset(&cmd
->ioa_cb
->ioarcb
.cdb
, 0, PMCRAID_MAX_CDB_LEN
);
330 ioarcb
->request_flags0
= 0;
331 ioarcb
->request_flags1
= 0;
332 ioarcb
->cmd_timeout
= 0;
333 ioarcb
->ioarcb_bus_addr
&= cpu_to_le64(~0x1FULL
);
334 ioarcb
->ioadl_bus_addr
= 0;
335 ioarcb
->ioadl_length
= 0;
336 ioarcb
->data_transfer_length
= 0;
337 ioarcb
->add_cmd_param_length
= 0;
338 ioarcb
->add_cmd_param_offset
= 0;
339 cmd
->ioa_cb
->ioasa
.ioasc
= 0;
340 cmd
->ioa_cb
->ioasa
.residual_data_length
= 0;
344 cmd
->cmd_done
= NULL
;
345 cmd
->scsi_cmd
= NULL
;
347 cmd
->completion_req
= 0;
348 cmd
->sense_buffer
= NULL
;
349 cmd
->sense_buffer_dma
= 0;
351 timer_setup(&cmd
->timer
, NULL
, 0);
355 * pmcraid_reinit_cmdblk - reinitialize a command block
357 * @cmd: pointer to struct pmcraid_cmd to be reinitialized
362 static void pmcraid_reinit_cmdblk(struct pmcraid_cmd
*cmd
)
364 pmcraid_init_cmdblk(cmd
, -1);
368 * pmcraid_get_free_cmd - get a free cmd block from command block pool
369 * @pinstance: adapter instance structure
372 * returns pointer to cmd block or NULL if no blocks are available
374 static struct pmcraid_cmd
*pmcraid_get_free_cmd(
375 struct pmcraid_instance
*pinstance
378 struct pmcraid_cmd
*cmd
= NULL
;
379 unsigned long lock_flags
;
381 /* free cmd block list is protected by free_pool_lock */
382 spin_lock_irqsave(&pinstance
->free_pool_lock
, lock_flags
);
384 if (!list_empty(&pinstance
->free_cmd_pool
)) {
385 cmd
= list_entry(pinstance
->free_cmd_pool
.next
,
386 struct pmcraid_cmd
, free_list
);
387 list_del(&cmd
->free_list
);
389 spin_unlock_irqrestore(&pinstance
->free_pool_lock
, lock_flags
);
391 /* Initialize the command block before giving it the caller */
393 pmcraid_reinit_cmdblk(cmd
);
398 * pmcraid_return_cmd - return a completed command block back into free pool
399 * @cmd: pointer to the command block
404 static void pmcraid_return_cmd(struct pmcraid_cmd
*cmd
)
406 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
407 unsigned long lock_flags
;
409 spin_lock_irqsave(&pinstance
->free_pool_lock
, lock_flags
);
410 list_add_tail(&cmd
->free_list
, &pinstance
->free_cmd_pool
);
411 spin_unlock_irqrestore(&pinstance
->free_pool_lock
, lock_flags
);
415 * pmcraid_read_interrupts - reads IOA interrupts
417 * @pinstance: pointer to adapter instance structure
420 * interrupts read from IOA
422 static u32
pmcraid_read_interrupts(struct pmcraid_instance
*pinstance
)
424 return (pinstance
->interrupt_mode
) ?
425 ioread32(pinstance
->int_regs
.ioa_host_msix_interrupt_reg
) :
426 ioread32(pinstance
->int_regs
.ioa_host_interrupt_reg
);
430 * pmcraid_disable_interrupts - Masks and clears all specified interrupts
432 * @pinstance: pointer to per adapter instance structure
433 * @intrs: interrupts to disable
438 static void pmcraid_disable_interrupts(
439 struct pmcraid_instance
*pinstance
,
443 u32 gmask
= ioread32(pinstance
->int_regs
.global_interrupt_mask_reg
);
444 u32 nmask
= gmask
| GLOBAL_INTERRUPT_MASK
;
446 iowrite32(intrs
, pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
447 iowrite32(nmask
, pinstance
->int_regs
.global_interrupt_mask_reg
);
448 ioread32(pinstance
->int_regs
.global_interrupt_mask_reg
);
450 if (!pinstance
->interrupt_mode
) {
452 pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
453 ioread32(pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
458 * pmcraid_enable_interrupts - Enables specified interrupts
460 * @pinstance: pointer to per adapter instance structure
461 * @intr: interrupts to enable
466 static void pmcraid_enable_interrupts(
467 struct pmcraid_instance
*pinstance
,
471 u32 gmask
= ioread32(pinstance
->int_regs
.global_interrupt_mask_reg
);
472 u32 nmask
= gmask
& (~GLOBAL_INTERRUPT_MASK
);
474 iowrite32(nmask
, pinstance
->int_regs
.global_interrupt_mask_reg
);
476 if (!pinstance
->interrupt_mode
) {
478 pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
479 ioread32(pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
482 pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
483 ioread32(pinstance
->int_regs
.global_interrupt_mask_reg
),
484 ioread32(pinstance
->int_regs
.ioa_host_interrupt_mask_reg
));
488 * pmcraid_clr_trans_op - clear trans to op interrupt
490 * @pinstance: pointer to per adapter instance structure
495 static void pmcraid_clr_trans_op(
496 struct pmcraid_instance
*pinstance
499 unsigned long lock_flags
;
501 if (!pinstance
->interrupt_mode
) {
502 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL
,
503 pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
504 ioread32(pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
505 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL
,
506 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
507 ioread32(pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
510 if (pinstance
->reset_cmd
!= NULL
) {
511 del_timer(&pinstance
->reset_cmd
->timer
);
513 pinstance
->host
->host_lock
, lock_flags
);
514 pinstance
->reset_cmd
->cmd_done(pinstance
->reset_cmd
);
515 spin_unlock_irqrestore(
516 pinstance
->host
->host_lock
, lock_flags
);
521 * pmcraid_reset_type - Determine the required reset type
522 * @pinstance: pointer to adapter instance structure
524 * IOA requires hard reset if any of the following conditions is true.
525 * 1. If HRRQ valid interrupt is not masked
526 * 2. IOA reset alert doorbell is set
527 * 3. If there are any error interrupts
529 static void pmcraid_reset_type(struct pmcraid_instance
*pinstance
)
535 mask
= ioread32(pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
536 intrs
= ioread32(pinstance
->int_regs
.ioa_host_interrupt_reg
);
537 alerts
= ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
539 if ((mask
& INTRS_HRRQ_VALID
) == 0 ||
540 (alerts
& DOORBELL_IOA_RESET_ALERT
) ||
541 (intrs
& PMCRAID_ERROR_INTERRUPTS
)) {
542 pmcraid_info("IOA requires hard reset\n");
543 pinstance
->ioa_hard_reset
= 1;
546 /* If unit check is active, trigger the dump */
547 if (intrs
& INTRS_IOA_UNIT_CHECK
)
548 pinstance
->ioa_unit_check
= 1;
552 * pmcraid_bist_done - completion function for PCI BIST
553 * @cmd: pointer to reset command
558 static void pmcraid_ioa_reset(struct pmcraid_cmd
*);
560 static void pmcraid_bist_done(struct timer_list
*t
)
562 struct pmcraid_cmd
*cmd
= from_timer(cmd
, t
, timer
);
563 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
564 unsigned long lock_flags
;
568 rc
= pci_read_config_word(pinstance
->pdev
, PCI_COMMAND
, &pci_reg
);
570 /* If PCI config space can't be accessed wait for another two secs */
571 if ((rc
!= PCIBIOS_SUCCESSFUL
|| (!(pci_reg
& PCI_COMMAND_MEMORY
))) &&
572 cmd
->time_left
> 0) {
573 pmcraid_info("BIST not complete, waiting another 2 secs\n");
574 cmd
->timer
.expires
= jiffies
+ cmd
->time_left
;
576 add_timer(&cmd
->timer
);
579 pmcraid_info("BIST is complete, proceeding with reset\n");
580 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
581 pmcraid_ioa_reset(cmd
);
582 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
587 * pmcraid_start_bist - starts BIST
588 * @cmd: pointer to reset cmd
592 static void pmcraid_start_bist(struct pmcraid_cmd
*cmd
)
594 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
595 u32 doorbells
, intrs
;
597 /* proceed with bist and wait for 2 seconds */
598 iowrite32(DOORBELL_IOA_START_BIST
,
599 pinstance
->int_regs
.host_ioa_interrupt_reg
);
600 doorbells
= ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
601 intrs
= ioread32(pinstance
->int_regs
.ioa_host_interrupt_reg
);
602 pmcraid_info("doorbells after start bist: %x intrs: %x\n",
605 cmd
->time_left
= msecs_to_jiffies(PMCRAID_BIST_TIMEOUT
);
606 cmd
->timer
.expires
= jiffies
+ msecs_to_jiffies(PMCRAID_BIST_TIMEOUT
);
607 cmd
->timer
.function
= pmcraid_bist_done
;
608 add_timer(&cmd
->timer
);
612 * pmcraid_reset_alert_done - completion routine for reset_alert
613 * @cmd: pointer to command block used in reset sequence
617 static void pmcraid_reset_alert_done(struct timer_list
*t
)
619 struct pmcraid_cmd
*cmd
= from_timer(cmd
, t
, timer
);
620 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
621 u32 status
= ioread32(pinstance
->ioa_status
);
622 unsigned long lock_flags
;
624 /* if the critical operation in progress bit is set or the wait times
625 * out, invoke reset engine to proceed with hard reset. If there is
626 * some more time to wait, restart the timer
628 if (((status
& INTRS_CRITICAL_OP_IN_PROGRESS
) == 0) ||
629 cmd
->time_left
<= 0) {
630 pmcraid_info("critical op is reset proceeding with reset\n");
631 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
632 pmcraid_ioa_reset(cmd
);
633 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
635 pmcraid_info("critical op is not yet reset waiting again\n");
636 /* restart timer if some more time is available to wait */
637 cmd
->time_left
-= PMCRAID_CHECK_FOR_RESET_TIMEOUT
;
638 cmd
->timer
.expires
= jiffies
+ PMCRAID_CHECK_FOR_RESET_TIMEOUT
;
639 cmd
->timer
.function
= pmcraid_reset_alert_done
;
640 add_timer(&cmd
->timer
);
645 * pmcraid_reset_alert - alerts IOA for a possible reset
646 * @cmd : command block to be used for reset sequence.
649 * returns 0 if pci config-space is accessible and RESET_DOORBELL is
650 * successfully written to IOA. Returns non-zero in case pci_config_space
653 static void pmcraid_notify_ioastate(struct pmcraid_instance
*, u32
);
654 static void pmcraid_reset_alert(struct pmcraid_cmd
*cmd
)
656 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
661 /* If we are able to access IOA PCI config space, alert IOA that we are
662 * going to reset it soon. This enables IOA to preserv persistent error
663 * data if any. In case memory space is not accessible, proceed with
666 rc
= pci_read_config_word(pinstance
->pdev
, PCI_COMMAND
, &pci_reg
);
667 if ((rc
== PCIBIOS_SUCCESSFUL
) && (pci_reg
& PCI_COMMAND_MEMORY
)) {
669 /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
670 * reset IOA doesn't generate any interrupts when CRITICAL
671 * OPERATION bit is reset. A timer is started to wait for this
674 cmd
->time_left
= PMCRAID_RESET_TIMEOUT
;
675 cmd
->timer
.expires
= jiffies
+ PMCRAID_CHECK_FOR_RESET_TIMEOUT
;
676 cmd
->timer
.function
= pmcraid_reset_alert_done
;
677 add_timer(&cmd
->timer
);
679 iowrite32(DOORBELL_IOA_RESET_ALERT
,
680 pinstance
->int_regs
.host_ioa_interrupt_reg
);
682 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
683 pmcraid_info("doorbells after reset alert: %x\n", doorbells
);
685 pmcraid_info("PCI config is not accessible starting BIST\n");
686 pinstance
->ioa_state
= IOA_STATE_IN_HARD_RESET
;
687 pmcraid_start_bist(cmd
);
692 * pmcraid_timeout_handler - Timeout handler for internally generated ops
694 * @cmd : pointer to command structure, that got timedout
696 * This function blocks host requests and initiates an adapter reset.
701 static void pmcraid_timeout_handler(struct timer_list
*t
)
703 struct pmcraid_cmd
*cmd
= from_timer(cmd
, t
, timer
);
704 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
705 unsigned long lock_flags
;
707 dev_info(&pinstance
->pdev
->dev
,
708 "Adapter being reset due to cmd(CDB[0] = %x) timeout\n",
709 cmd
->ioa_cb
->ioarcb
.cdb
[0]);
711 /* Command timeouts result in hard reset sequence. The command that got
712 * timed out may be the one used as part of reset sequence. In this
713 * case restart reset sequence using the same command block even if
714 * reset is in progress. Otherwise fail this command and get a free
715 * command block to restart the reset sequence.
717 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
718 if (!pinstance
->ioa_reset_in_progress
) {
719 pinstance
->ioa_reset_attempts
= 0;
720 cmd
= pmcraid_get_free_cmd(pinstance
);
722 /* If we are out of command blocks, just return here itself.
723 * Some other command's timeout handler can do the reset job
726 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
728 pmcraid_err("no free cmnd block for timeout handler\n");
732 pinstance
->reset_cmd
= cmd
;
733 pinstance
->ioa_reset_in_progress
= 1;
735 pmcraid_info("reset is already in progress\n");
737 if (pinstance
->reset_cmd
!= cmd
) {
738 /* This command should have been given to IOA, this
739 * command will be completed by fail_outstanding_cmds
742 pmcraid_err("cmd is pending but reset in progress\n");
745 /* If this command was being used as part of the reset
746 * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
747 * causes fail_outstanding_commands not to return the command
748 * block back to free pool
750 if (cmd
== pinstance
->reset_cmd
)
751 cmd
->cmd_done
= pmcraid_ioa_reset
;
754 /* Notify apps of important IOA bringup/bringdown sequences */
755 if (pinstance
->scn
.ioa_state
!= PMC_DEVICE_EVENT_RESET_START
&&
756 pinstance
->scn
.ioa_state
!= PMC_DEVICE_EVENT_SHUTDOWN_START
)
757 pmcraid_notify_ioastate(pinstance
,
758 PMC_DEVICE_EVENT_RESET_START
);
760 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
761 scsi_block_requests(pinstance
->host
);
762 pmcraid_reset_alert(cmd
);
763 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
767 * pmcraid_internal_done - completion routine for internally generated cmds
769 * @cmd: command that got response from IOA
774 static void pmcraid_internal_done(struct pmcraid_cmd
*cmd
)
776 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
777 cmd
->ioa_cb
->ioarcb
.cdb
[0],
778 le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
));
780 /* Some of the internal commands are sent with callers blocking for the
781 * response. Same will be indicated as part of cmd->completion_req
782 * field. Response path needs to wake up any waiters waiting for cmd
783 * completion if this flag is set.
785 if (cmd
->completion_req
) {
786 cmd
->completion_req
= 0;
787 complete(&cmd
->wait_for_completion
);
790 /* most of the internal commands are completed by caller itself, so
791 * no need to return the command block back to free pool until we are
792 * required to do so (e.g once done with initialization).
796 pmcraid_return_cmd(cmd
);
801 * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
803 * @cmd: command that got response from IOA
805 * This routine is called after driver re-reads configuration table due to a
806 * lost CCN. It returns the command block back to free pool and schedules
807 * worker thread to add/delete devices into the system.
812 static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd
*cmd
)
814 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
815 cmd
->ioa_cb
->ioarcb
.cdb
[0],
816 le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
));
820 pmcraid_return_cmd(cmd
);
822 pmcraid_info("scheduling worker for config table reinitialization\n");
823 schedule_work(&cmd
->drv_inst
->worker_q
);
827 * pmcraid_erp_done - Process completion of SCSI error response from device
828 * @cmd: pmcraid_command
830 * This function copies the sense buffer into the scsi_cmd struct and completes
831 * scsi_cmd by calling scsi_done function.
836 static void pmcraid_erp_done(struct pmcraid_cmd
*cmd
)
838 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
839 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
840 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
842 if (PMCRAID_IOASC_SENSE_KEY(ioasc
) > 0) {
843 scsi_cmd
->result
|= (DID_ERROR
<< 16);
844 scmd_printk(KERN_INFO
, scsi_cmd
,
845 "command CDB[0] = %x failed with IOASC: 0x%08X\n",
846 cmd
->ioa_cb
->ioarcb
.cdb
[0], ioasc
);
849 if (cmd
->sense_buffer
) {
850 dma_unmap_single(&pinstance
->pdev
->dev
, cmd
->sense_buffer_dma
,
851 SCSI_SENSE_BUFFERSIZE
, DMA_FROM_DEVICE
);
852 cmd
->sense_buffer
= NULL
;
853 cmd
->sense_buffer_dma
= 0;
856 scsi_dma_unmap(scsi_cmd
);
857 pmcraid_return_cmd(cmd
);
858 scsi_cmd
->scsi_done(scsi_cmd
);
862 * pmcraid_fire_command - sends an IOA command to adapter
864 * This function adds the given block into pending command list
865 * and returns without waiting
867 * @cmd : command to be sent to the device
872 static void _pmcraid_fire_command(struct pmcraid_cmd
*cmd
)
874 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
875 unsigned long lock_flags
;
877 /* Add this command block to pending cmd pool. We do this prior to
878 * writting IOARCB to ioarrin because IOA might complete the command
879 * by the time we are about to add it to the list. Response handler
880 * (isr/tasklet) looks for cmd block in the pending pending list.
882 spin_lock_irqsave(&pinstance
->pending_pool_lock
, lock_flags
);
883 list_add_tail(&cmd
->free_list
, &pinstance
->pending_cmd_pool
);
884 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
, lock_flags
);
885 atomic_inc(&pinstance
->outstanding_cmds
);
887 /* driver writes lower 32-bit value of IOARCB address only */
889 iowrite32(le64_to_cpu(cmd
->ioa_cb
->ioarcb
.ioarcb_bus_addr
), pinstance
->ioarrin
);
893 * pmcraid_send_cmd - fires a command to IOA
895 * This function also sets up timeout function, and command completion
898 * @cmd: pointer to the command block to be fired to IOA
899 * @cmd_done: command completion function, called once IOA responds
900 * @timeout: timeout to wait for this command completion
901 * @timeout_func: timeout handler
906 static void pmcraid_send_cmd(
907 struct pmcraid_cmd
*cmd
,
908 void (*cmd_done
) (struct pmcraid_cmd
*),
909 unsigned long timeout
,
910 void (*timeout_func
) (struct timer_list
*)
913 /* initialize done function */
914 cmd
->cmd_done
= cmd_done
;
917 /* setup timeout handler */
918 cmd
->timer
.expires
= jiffies
+ timeout
;
919 cmd
->timer
.function
= timeout_func
;
920 add_timer(&cmd
->timer
);
923 /* fire the command to IOA */
924 _pmcraid_fire_command(cmd
);
928 * pmcraid_ioa_shutdown_done - completion function for IOA shutdown command
929 * @cmd: pointer to the command block used for sending IOA shutdown command
934 static void pmcraid_ioa_shutdown_done(struct pmcraid_cmd
*cmd
)
936 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
937 unsigned long lock_flags
;
939 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
940 pmcraid_ioa_reset(cmd
);
941 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
945 * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
947 * @cmd: pointer to the command block used as part of reset sequence
952 static void pmcraid_ioa_shutdown(struct pmcraid_cmd
*cmd
)
954 pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
955 cmd
->ioa_cb
->ioarcb
.cdb
[0],
956 le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
));
958 /* Note that commands sent during reset require next command to be sent
959 * to IOA. Hence reinit the done function as well as timeout function
961 pmcraid_reinit_cmdblk(cmd
);
962 cmd
->ioa_cb
->ioarcb
.request_type
= REQ_TYPE_IOACMD
;
963 cmd
->ioa_cb
->ioarcb
.resource_handle
=
964 cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
965 cmd
->ioa_cb
->ioarcb
.cdb
[0] = PMCRAID_IOA_SHUTDOWN
;
966 cmd
->ioa_cb
->ioarcb
.cdb
[1] = PMCRAID_SHUTDOWN_NORMAL
;
968 /* fire shutdown command to hardware. */
969 pmcraid_info("firing normal shutdown command (%d) to IOA\n",
970 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
));
972 pmcraid_notify_ioastate(cmd
->drv_inst
, PMC_DEVICE_EVENT_SHUTDOWN_START
);
974 pmcraid_send_cmd(cmd
, pmcraid_ioa_shutdown_done
,
975 PMCRAID_SHUTDOWN_TIMEOUT
,
976 pmcraid_timeout_handler
);
980 * pmcraid_get_fwversion_done - completion function for get_fwversion
982 * @cmd: pointer to command block used to send INQUIRY command
987 static void pmcraid_querycfg(struct pmcraid_cmd
*);
989 static void pmcraid_get_fwversion_done(struct pmcraid_cmd
*cmd
)
991 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
992 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
993 unsigned long lock_flags
;
995 /* configuration table entry size depends on firmware version. If fw
996 * version is not known, it is not possible to interpret IOA config
1000 pmcraid_err("IOA Inquiry failed with %x\n", ioasc
);
1001 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
1002 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
1003 pmcraid_reset_alert(cmd
);
1004 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
1006 pmcraid_querycfg(cmd
);
1011 * pmcraid_get_fwversion - reads firmware version information
1013 * @cmd: pointer to command block used to send INQUIRY command
1018 static void pmcraid_get_fwversion(struct pmcraid_cmd
*cmd
)
1020 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
1021 struct pmcraid_ioadl_desc
*ioadl
;
1022 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1023 u16 data_size
= sizeof(struct pmcraid_inquiry_data
);
1025 pmcraid_reinit_cmdblk(cmd
);
1026 ioarcb
->request_type
= REQ_TYPE_SCSI
;
1027 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
1028 ioarcb
->cdb
[0] = INQUIRY
;
1030 ioarcb
->cdb
[2] = 0xD0;
1031 ioarcb
->cdb
[3] = (data_size
>> 8) & 0xFF;
1032 ioarcb
->cdb
[4] = data_size
& 0xFF;
1034 /* Since entire inquiry data it can be part of IOARCB itself
1036 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
1037 offsetof(struct pmcraid_ioarcb
,
1038 add_data
.u
.ioadl
[0]));
1039 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
1040 ioarcb
->ioarcb_bus_addr
&= cpu_to_le64(~(0x1FULL
));
1042 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
1043 ioarcb
->data_transfer_length
= cpu_to_le32(data_size
);
1044 ioadl
= &(ioarcb
->add_data
.u
.ioadl
[0]);
1045 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
1046 ioadl
->address
= cpu_to_le64(pinstance
->inq_data_baddr
);
1047 ioadl
->data_len
= cpu_to_le32(data_size
);
1049 pmcraid_send_cmd(cmd
, pmcraid_get_fwversion_done
,
1050 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
1054 * pmcraid_identify_hrrq - registers host rrq buffers with IOA
1055 * @cmd: pointer to command block to be used for identify hrrq
1060 static void pmcraid_identify_hrrq(struct pmcraid_cmd
*cmd
)
1062 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1063 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
1064 int index
= cmd
->hrrq_index
;
1065 __be64 hrrq_addr
= cpu_to_be64(pinstance
->hrrq_start_bus_addr
[index
]);
1066 __be32 hrrq_size
= cpu_to_be32(sizeof(u32
) * PMCRAID_MAX_CMD
);
1067 void (*done_function
)(struct pmcraid_cmd
*);
1069 pmcraid_reinit_cmdblk(cmd
);
1070 cmd
->hrrq_index
= index
+ 1;
1072 if (cmd
->hrrq_index
< pinstance
->num_hrrq
) {
1073 done_function
= pmcraid_identify_hrrq
;
1075 cmd
->hrrq_index
= 0;
1076 done_function
= pmcraid_get_fwversion
;
1079 /* Initialize ioarcb */
1080 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
1081 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
1083 /* initialize the hrrq number where IOA will respond to this command */
1084 ioarcb
->hrrq_id
= index
;
1085 ioarcb
->cdb
[0] = PMCRAID_IDENTIFY_HRRQ
;
1086 ioarcb
->cdb
[1] = index
;
1088 /* IOA expects 64-bit pci address to be written in B.E format
1089 * (i.e cdb[2]=MSByte..cdb[9]=LSB.
1091 pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb:index => %llx:%llx:%x\n",
1092 hrrq_addr
, ioarcb
->ioarcb_bus_addr
, index
);
1094 memcpy(&(ioarcb
->cdb
[2]), &hrrq_addr
, sizeof(hrrq_addr
));
1095 memcpy(&(ioarcb
->cdb
[10]), &hrrq_size
, sizeof(hrrq_size
));
1097 /* Subsequent commands require HRRQ identification to be successful.
1098 * Note that this gets called even during reset from SCSI mid-layer
1101 pmcraid_send_cmd(cmd
, done_function
,
1102 PMCRAID_INTERNAL_TIMEOUT
,
1103 pmcraid_timeout_handler
);
1106 static void pmcraid_process_ccn(struct pmcraid_cmd
*cmd
);
1107 static void pmcraid_process_ldn(struct pmcraid_cmd
*cmd
);
1110 * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
1112 * @cmd: initialized command block pointer
1117 static void pmcraid_send_hcam_cmd(struct pmcraid_cmd
*cmd
)
1119 if (cmd
->ioa_cb
->ioarcb
.cdb
[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE
)
1120 atomic_set(&(cmd
->drv_inst
->ccn
.ignore
), 0);
1122 atomic_set(&(cmd
->drv_inst
->ldn
.ignore
), 0);
1124 pmcraid_send_cmd(cmd
, cmd
->cmd_done
, 0, NULL
);
1128 * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
1130 * @pinstance: pointer to adapter instance structure
1134 * pointer to initialized pmcraid_cmd structure or NULL
1136 static struct pmcraid_cmd
*pmcraid_init_hcam
1138 struct pmcraid_instance
*pinstance
,
1142 struct pmcraid_cmd
*cmd
;
1143 struct pmcraid_ioarcb
*ioarcb
;
1144 struct pmcraid_ioadl_desc
*ioadl
;
1145 struct pmcraid_hostrcb
*hcam
;
1146 void (*cmd_done
) (struct pmcraid_cmd
*);
1150 cmd
= pmcraid_get_free_cmd(pinstance
);
1153 pmcraid_err("no free command blocks for hcam\n");
1157 if (type
== PMCRAID_HCAM_CODE_CONFIG_CHANGE
) {
1158 rcb_size
= sizeof(struct pmcraid_hcam_ccn_ext
);
1159 cmd_done
= pmcraid_process_ccn
;
1160 dma
= pinstance
->ccn
.baddr
+ PMCRAID_AEN_HDR_SIZE
;
1161 hcam
= &pinstance
->ccn
;
1163 rcb_size
= sizeof(struct pmcraid_hcam_ldn
);
1164 cmd_done
= pmcraid_process_ldn
;
1165 dma
= pinstance
->ldn
.baddr
+ PMCRAID_AEN_HDR_SIZE
;
1166 hcam
= &pinstance
->ldn
;
1169 /* initialize command pointer used for HCAM registration */
1172 ioarcb
= &cmd
->ioa_cb
->ioarcb
;
1173 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
1174 offsetof(struct pmcraid_ioarcb
,
1175 add_data
.u
.ioadl
[0]));
1176 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
1177 ioadl
= ioarcb
->add_data
.u
.ioadl
;
1179 /* Initialize ioarcb */
1180 ioarcb
->request_type
= REQ_TYPE_HCAM
;
1181 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
1182 ioarcb
->cdb
[0] = PMCRAID_HOST_CONTROLLED_ASYNC
;
1183 ioarcb
->cdb
[1] = type
;
1184 ioarcb
->cdb
[7] = (rcb_size
>> 8) & 0xFF;
1185 ioarcb
->cdb
[8] = (rcb_size
) & 0xFF;
1187 ioarcb
->data_transfer_length
= cpu_to_le32(rcb_size
);
1189 ioadl
[0].flags
|= IOADL_FLAGS_READ_LAST
;
1190 ioadl
[0].data_len
= cpu_to_le32(rcb_size
);
1191 ioadl
[0].address
= cpu_to_le64(dma
);
1193 cmd
->cmd_done
= cmd_done
;
1198 * pmcraid_send_hcam - Send an HCAM to IOA
1199 * @pinstance: ioa config struct
1202 * This function will send a Host Controlled Async command to IOA.
1207 static void pmcraid_send_hcam(struct pmcraid_instance
*pinstance
, u8 type
)
1209 struct pmcraid_cmd
*cmd
= pmcraid_init_hcam(pinstance
, type
);
1210 pmcraid_send_hcam_cmd(cmd
);
1215 * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
1217 * @cmd: pointer to cmd that is used as cancelling command
1218 * @cmd_to_cancel: pointer to the command that needs to be cancelled
1220 static void pmcraid_prepare_cancel_cmd(
1221 struct pmcraid_cmd
*cmd
,
1222 struct pmcraid_cmd
*cmd_to_cancel
1225 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
1228 /* IOARCB address of the command to be cancelled is given in
1229 * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
1230 * IOARCB address are not masked.
1232 ioarcb_addr
= cpu_to_be64(le64_to_cpu(cmd_to_cancel
->ioa_cb
->ioarcb
.ioarcb_bus_addr
));
1234 /* Get the resource handle to where the command to be aborted has been
1237 ioarcb
->resource_handle
= cmd_to_cancel
->ioa_cb
->ioarcb
.resource_handle
;
1238 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
1239 memset(ioarcb
->cdb
, 0, PMCRAID_MAX_CDB_LEN
);
1240 ioarcb
->cdb
[0] = PMCRAID_ABORT_CMD
;
1242 memcpy(&(ioarcb
->cdb
[2]), &ioarcb_addr
, sizeof(ioarcb_addr
));
1246 * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
1248 * @cmd: command to be used as cancelling command
1250 * @cmd_done: op done function for the cancelling command
1252 static void pmcraid_cancel_hcam(
1253 struct pmcraid_cmd
*cmd
,
1255 void (*cmd_done
) (struct pmcraid_cmd
*)
1258 struct pmcraid_instance
*pinstance
;
1259 struct pmcraid_hostrcb
*hcam
;
1261 pinstance
= cmd
->drv_inst
;
1262 hcam
= (type
== PMCRAID_HCAM_CODE_LOG_DATA
) ?
1263 &pinstance
->ldn
: &pinstance
->ccn
;
1265 /* prepare for cancelling previous hcam command. If the HCAM is
1266 * currently not pending with IOA, we would have hcam->cmd as non-null
1268 if (hcam
->cmd
== NULL
)
1271 pmcraid_prepare_cancel_cmd(cmd
, hcam
->cmd
);
1273 /* writing to IOARRIN must be protected by host_lock, as mid-layer
1274 * schedule queuecommand while we are doing this
1276 pmcraid_send_cmd(cmd
, cmd_done
,
1277 PMCRAID_INTERNAL_TIMEOUT
,
1278 pmcraid_timeout_handler
);
1282 * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
1284 * @cmd: command block to be used for cancelling the HCAM
1286 static void pmcraid_cancel_ccn(struct pmcraid_cmd
*cmd
)
1288 pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
1289 cmd
->ioa_cb
->ioarcb
.cdb
[0],
1290 le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
));
1292 pmcraid_reinit_cmdblk(cmd
);
1294 pmcraid_cancel_hcam(cmd
,
1295 PMCRAID_HCAM_CODE_CONFIG_CHANGE
,
1296 pmcraid_ioa_shutdown
);
1300 * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
1302 * @cmd: command block to be used for cancelling the HCAM
1304 static void pmcraid_cancel_ldn(struct pmcraid_cmd
*cmd
)
1306 pmcraid_cancel_hcam(cmd
,
1307 PMCRAID_HCAM_CODE_LOG_DATA
,
1308 pmcraid_cancel_ccn
);
1312 * pmcraid_expose_resource - check if the resource can be exposed to OS
1314 * @fw_version: firmware version code
1315 * @cfgte: pointer to configuration table entry of the resource
1318 * true if resource can be added to midlayer, false(0) otherwise
1320 static int pmcraid_expose_resource(u16 fw_version
,
1321 struct pmcraid_config_table_entry
*cfgte
)
1325 if (cfgte
->resource_type
== RES_TYPE_VSET
) {
1326 if (fw_version
<= PMCRAID_FW_VERSION_1
)
1327 retval
= ((cfgte
->unique_flags1
& 0x80) == 0);
1329 retval
= ((cfgte
->unique_flags0
& 0x80) == 0 &&
1330 (cfgte
->unique_flags1
& 0x80) == 0);
1332 } else if (cfgte
->resource_type
== RES_TYPE_GSCSI
)
1333 retval
= (RES_BUS(cfgte
->resource_address
) !=
1334 PMCRAID_VIRTUAL_ENCL_BUS_ID
);
1338 /* attributes supported by pmcraid_event_family */
1340 PMCRAID_AEN_ATTR_UNSPEC
,
1341 PMCRAID_AEN_ATTR_EVENT
,
1342 __PMCRAID_AEN_ATTR_MAX
,
1344 #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
1346 /* commands supported by pmcraid_event_family */
1348 PMCRAID_AEN_CMD_UNSPEC
,
1349 PMCRAID_AEN_CMD_EVENT
,
1350 __PMCRAID_AEN_CMD_MAX
,
1352 #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
1354 static struct genl_multicast_group pmcraid_mcgrps
[] = {
1355 { .name
= "events", /* not really used - see ID discussion below */ },
1358 static struct genl_family pmcraid_event_family __ro_after_init
= {
1359 .module
= THIS_MODULE
,
1362 .maxattr
= PMCRAID_AEN_ATTR_MAX
,
1363 .mcgrps
= pmcraid_mcgrps
,
1364 .n_mcgrps
= ARRAY_SIZE(pmcraid_mcgrps
),
1368 * pmcraid_netlink_init - registers pmcraid_event_family
1371 * 0 if the pmcraid_event_family is successfully registered
1372 * with netlink generic, non-zero otherwise
1374 static int __init
pmcraid_netlink_init(void)
1378 result
= genl_register_family(&pmcraid_event_family
);
1383 pmcraid_info("registered NETLINK GENERIC group: %d\n",
1384 pmcraid_event_family
.id
);
1390 * pmcraid_netlink_release - unregisters pmcraid_event_family
1395 static void pmcraid_netlink_release(void)
1397 genl_unregister_family(&pmcraid_event_family
);
1401 * pmcraid_notify_aen - sends event msg to user space application
1402 * @pinstance: pointer to adapter instance structure
1406 * 0 if success, error value in case of any failure.
1408 static int pmcraid_notify_aen(
1409 struct pmcraid_instance
*pinstance
,
1410 struct pmcraid_aen_msg
*aen_msg
,
1414 struct sk_buff
*skb
;
1416 u32 total_size
, nla_genl_hdr_total_size
;
1419 aen_msg
->hostno
= (pinstance
->host
->unique_id
<< 16 |
1420 MINOR(pinstance
->cdev
.dev
));
1421 aen_msg
->length
= data_size
;
1423 data_size
+= sizeof(*aen_msg
);
1425 total_size
= nla_total_size(data_size
);
1426 /* Add GENL_HDR to total_size */
1427 nla_genl_hdr_total_size
=
1428 (total_size
+ (GENL_HDRLEN
+
1429 ((struct genl_family
*)&pmcraid_event_family
)->hdrsize
)
1431 skb
= genlmsg_new(nla_genl_hdr_total_size
, GFP_ATOMIC
);
1435 pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
1440 /* add the genetlink message header */
1441 msg_header
= genlmsg_put(skb
, 0, 0,
1442 &pmcraid_event_family
, 0,
1443 PMCRAID_AEN_CMD_EVENT
);
1445 pmcraid_err("failed to copy command details\n");
1450 result
= nla_put(skb
, PMCRAID_AEN_ATTR_EVENT
, data_size
, aen_msg
);
1453 pmcraid_err("failed to copy AEN attribute data\n");
1458 /* send genetlink multicast message to notify appplications */
1459 genlmsg_end(skb
, msg_header
);
1461 result
= genlmsg_multicast(&pmcraid_event_family
, skb
,
1464 /* If there are no listeners, genlmsg_multicast may return non-zero
1468 pmcraid_info("error (%x) sending aen event message\n", result
);
1473 * pmcraid_notify_ccn - notifies about CCN event msg to user space
1474 * @pinstance: pointer adapter instance structure
1477 * 0 if success, error value in case of any failure
1479 static int pmcraid_notify_ccn(struct pmcraid_instance
*pinstance
)
1481 return pmcraid_notify_aen(pinstance
,
1483 le32_to_cpu(pinstance
->ccn
.hcam
->data_len
) +
1484 sizeof(struct pmcraid_hcam_hdr
));
1488 * pmcraid_notify_ldn - notifies about CCN event msg to user space
1489 * @pinstance: pointer adapter instance structure
1492 * 0 if success, error value in case of any failure
1494 static int pmcraid_notify_ldn(struct pmcraid_instance
*pinstance
)
1496 return pmcraid_notify_aen(pinstance
,
1498 le32_to_cpu(pinstance
->ldn
.hcam
->data_len
) +
1499 sizeof(struct pmcraid_hcam_hdr
));
1503 * pmcraid_notify_ioastate - sends IOA state event msg to user space
1504 * @pinstance: pointer adapter instance structure
1505 * @evt: controller state event to be sent
1508 * 0 if success, error value in case of any failure
1510 static void pmcraid_notify_ioastate(struct pmcraid_instance
*pinstance
, u32 evt
)
1512 pinstance
->scn
.ioa_state
= evt
;
1513 pmcraid_notify_aen(pinstance
,
1514 &pinstance
->scn
.msg
,
1519 * pmcraid_handle_config_change - Handle a config change from the adapter
1520 * @pinstance: pointer to per adapter instance structure
1526 static void pmcraid_handle_config_change(struct pmcraid_instance
*pinstance
)
1528 struct pmcraid_config_table_entry
*cfg_entry
;
1529 struct pmcraid_hcam_ccn
*ccn_hcam
;
1530 struct pmcraid_cmd
*cmd
;
1531 struct pmcraid_cmd
*cfgcmd
;
1532 struct pmcraid_resource_entry
*res
= NULL
;
1533 unsigned long lock_flags
;
1534 unsigned long host_lock_flags
;
1536 u32 hidden_entry
= 0;
1540 ccn_hcam
= (struct pmcraid_hcam_ccn
*)pinstance
->ccn
.hcam
;
1541 cfg_entry
= &ccn_hcam
->cfg_entry
;
1542 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
1544 pmcraid_info("CCN(%x): %x timestamp: %llx type: %x lost: %x flags: %x \
1545 res: %x:%x:%x:%x\n",
1546 le32_to_cpu(pinstance
->ccn
.hcam
->ilid
),
1547 pinstance
->ccn
.hcam
->op_code
,
1548 (le32_to_cpu(pinstance
->ccn
.hcam
->timestamp1
) |
1549 ((le32_to_cpu(pinstance
->ccn
.hcam
->timestamp2
) & 0xffffffffLL
) << 32)),
1550 pinstance
->ccn
.hcam
->notification_type
,
1551 pinstance
->ccn
.hcam
->notification_lost
,
1552 pinstance
->ccn
.hcam
->flags
,
1553 pinstance
->host
->unique_id
,
1554 RES_IS_VSET(*cfg_entry
) ? PMCRAID_VSET_BUS_ID
:
1555 (RES_IS_GSCSI(*cfg_entry
) ? PMCRAID_PHYS_BUS_ID
:
1556 RES_BUS(cfg_entry
->resource_address
)),
1557 RES_IS_VSET(*cfg_entry
) ?
1558 (fw_version
<= PMCRAID_FW_VERSION_1
?
1559 cfg_entry
->unique_flags1
:
1560 le16_to_cpu(cfg_entry
->array_id
) & 0xFF) :
1561 RES_TARGET(cfg_entry
->resource_address
),
1562 RES_LUN(cfg_entry
->resource_address
));
1565 /* If this HCAM indicates a lost notification, read the config table */
1566 if (pinstance
->ccn
.hcam
->notification_lost
) {
1567 cfgcmd
= pmcraid_get_free_cmd(pinstance
);
1569 pmcraid_info("lost CCN, reading config table\b");
1570 pinstance
->reinit_cfg_table
= 1;
1571 pmcraid_querycfg(cfgcmd
);
1573 pmcraid_err("lost CCN, no free cmd for querycfg\n");
1575 goto out_notify_apps
;
1578 /* If this resource is not going to be added to mid-layer, just notify
1579 * applications and return. If this notification is about hiding a VSET
1580 * resource, check if it was exposed already.
1582 if (pinstance
->ccn
.hcam
->notification_type
==
1583 NOTIFICATION_TYPE_ENTRY_CHANGED
&&
1584 cfg_entry
->resource_type
== RES_TYPE_VSET
) {
1585 hidden_entry
= (cfg_entry
->unique_flags1
& 0x80) != 0;
1586 } else if (!pmcraid_expose_resource(fw_version
, cfg_entry
)) {
1587 goto out_notify_apps
;
1590 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
1591 list_for_each_entry(res
, &pinstance
->used_res_q
, queue
) {
1592 rc
= memcmp(&res
->cfg_entry
.resource_address
,
1593 &cfg_entry
->resource_address
,
1594 sizeof(cfg_entry
->resource_address
));
1604 spin_unlock_irqrestore(&pinstance
->resource_lock
,
1606 goto out_notify_apps
;
1609 /* If there are more number of resources than what driver can
1610 * manage, do not notify the applications about the CCN. Just
1611 * ignore this notifications and re-register the same HCAM
1613 if (list_empty(&pinstance
->free_res_q
)) {
1614 spin_unlock_irqrestore(&pinstance
->resource_lock
,
1616 pmcraid_err("too many resources attached\n");
1617 spin_lock_irqsave(pinstance
->host
->host_lock
,
1619 pmcraid_send_hcam(pinstance
,
1620 PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1621 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
1626 res
= list_entry(pinstance
->free_res_q
.next
,
1627 struct pmcraid_resource_entry
, queue
);
1629 list_del(&res
->queue
);
1630 res
->scsi_dev
= NULL
;
1631 res
->reset_progress
= 0;
1632 list_add_tail(&res
->queue
, &pinstance
->used_res_q
);
1635 memcpy(&res
->cfg_entry
, cfg_entry
, pinstance
->config_table_entry_size
);
1637 if (pinstance
->ccn
.hcam
->notification_type
==
1638 NOTIFICATION_TYPE_ENTRY_DELETED
|| hidden_entry
) {
1639 if (res
->scsi_dev
) {
1640 if (fw_version
<= PMCRAID_FW_VERSION_1
)
1641 res
->cfg_entry
.unique_flags1
&= 0x7F;
1643 res
->cfg_entry
.array_id
&= cpu_to_le16(0xFF);
1644 res
->change_detected
= RES_CHANGE_DEL
;
1645 res
->cfg_entry
.resource_handle
=
1646 PMCRAID_INVALID_RES_HANDLE
;
1647 schedule_work(&pinstance
->worker_q
);
1649 /* This may be one of the non-exposed resources */
1650 list_move_tail(&res
->queue
, &pinstance
->free_res_q
);
1652 } else if (!res
->scsi_dev
) {
1653 res
->change_detected
= RES_CHANGE_ADD
;
1654 schedule_work(&pinstance
->worker_q
);
1656 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
1660 /* Notify configuration changes to registered applications.*/
1661 if (!pmcraid_disable_aen
)
1662 pmcraid_notify_ccn(pinstance
);
1664 cmd
= pmcraid_init_hcam(pinstance
, PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1666 pmcraid_send_hcam_cmd(cmd
);
1670 * pmcraid_get_error_info - return error string for an ioasc
1671 * @ioasc: ioasc code
1675 static struct pmcraid_ioasc_error
*pmcraid_get_error_info(u32 ioasc
)
1678 for (i
= 0; i
< ARRAY_SIZE(pmcraid_ioasc_error_table
); i
++) {
1679 if (pmcraid_ioasc_error_table
[i
].ioasc_code
== ioasc
)
1680 return &pmcraid_ioasc_error_table
[i
];
1686 * pmcraid_ioasc_logger - log IOASC information based user-settings
1687 * @ioasc: ioasc code
1688 * @cmd: pointer to command that resulted in 'ioasc'
1690 static void pmcraid_ioasc_logger(u32 ioasc
, struct pmcraid_cmd
*cmd
)
1692 struct pmcraid_ioasc_error
*error_info
= pmcraid_get_error_info(ioasc
);
1694 if (error_info
== NULL
||
1695 cmd
->drv_inst
->current_log_level
< error_info
->log_level
)
1698 /* log the error string */
1699 pmcraid_err("cmd [%x] for resource %x failed with %x(%s)\n",
1700 cmd
->ioa_cb
->ioarcb
.cdb
[0],
1701 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.resource_handle
),
1702 ioasc
, error_info
->error_string
);
1706 * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
1708 * @pinstance: pointer to per adapter instance structure
1713 static void pmcraid_handle_error_log(struct pmcraid_instance
*pinstance
)
1715 struct pmcraid_hcam_ldn
*hcam_ldn
;
1718 hcam_ldn
= (struct pmcraid_hcam_ldn
*)pinstance
->ldn
.hcam
;
1721 ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
1722 pinstance
->ldn
.hcam
->ilid
,
1723 pinstance
->ldn
.hcam
->op_code
,
1724 pinstance
->ldn
.hcam
->notification_type
,
1725 pinstance
->ldn
.hcam
->notification_lost
,
1726 pinstance
->ldn
.hcam
->flags
,
1727 pinstance
->ldn
.hcam
->overlay_id
);
1729 /* log only the errors, no need to log informational log entries */
1730 if (pinstance
->ldn
.hcam
->notification_type
!=
1731 NOTIFICATION_TYPE_ERROR_LOG
)
1734 if (pinstance
->ldn
.hcam
->notification_lost
==
1735 HOSTRCB_NOTIFICATIONS_LOST
)
1736 dev_info(&pinstance
->pdev
->dev
, "Error notifications lost\n");
1738 ioasc
= le32_to_cpu(hcam_ldn
->error_log
.fd_ioasc
);
1740 if (ioasc
== PMCRAID_IOASC_UA_BUS_WAS_RESET
||
1741 ioasc
== PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER
) {
1742 dev_info(&pinstance
->pdev
->dev
,
1743 "UnitAttention due to IOA Bus Reset\n");
1744 scsi_report_bus_reset(
1746 RES_BUS(hcam_ldn
->error_log
.fd_ra
));
1753 * pmcraid_process_ccn - Op done function for a CCN.
1754 * @cmd: pointer to command struct
1756 * This function is the op done function for a configuration
1757 * change notification
1762 static void pmcraid_process_ccn(struct pmcraid_cmd
*cmd
)
1764 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1765 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
1766 unsigned long lock_flags
;
1768 pinstance
->ccn
.cmd
= NULL
;
1769 pmcraid_return_cmd(cmd
);
1771 /* If driver initiated IOA reset happened while this hcam was pending
1772 * with IOA, or IOA bringdown sequence is in progress, no need to
1773 * re-register the hcam
1775 if (ioasc
== PMCRAID_IOASC_IOA_WAS_RESET
||
1776 atomic_read(&pinstance
->ccn
.ignore
) == 1) {
1779 dev_info(&pinstance
->pdev
->dev
,
1780 "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc
);
1781 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
1782 pmcraid_send_hcam(pinstance
, PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1783 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
1785 pmcraid_handle_config_change(pinstance
);
1790 * pmcraid_process_ldn - op done function for an LDN
1791 * @cmd: pointer to command block
1796 static void pmcraid_initiate_reset(struct pmcraid_instance
*);
1797 static void pmcraid_set_timestamp(struct pmcraid_cmd
*cmd
);
1799 static void pmcraid_process_ldn(struct pmcraid_cmd
*cmd
)
1801 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1802 struct pmcraid_hcam_ldn
*ldn_hcam
=
1803 (struct pmcraid_hcam_ldn
*)pinstance
->ldn
.hcam
;
1804 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
1805 u32 fd_ioasc
= le32_to_cpu(ldn_hcam
->error_log
.fd_ioasc
);
1806 unsigned long lock_flags
;
1808 /* return the command block back to freepool */
1809 pinstance
->ldn
.cmd
= NULL
;
1810 pmcraid_return_cmd(cmd
);
1812 /* If driver initiated IOA reset happened while this hcam was pending
1813 * with IOA, no need to re-register the hcam as reset engine will do it
1814 * once reset sequence is complete
1816 if (ioasc
== PMCRAID_IOASC_IOA_WAS_RESET
||
1817 atomic_read(&pinstance
->ccn
.ignore
) == 1) {
1819 } else if (!ioasc
) {
1820 pmcraid_handle_error_log(pinstance
);
1821 if (fd_ioasc
== PMCRAID_IOASC_NR_IOA_RESET_REQUIRED
) {
1822 spin_lock_irqsave(pinstance
->host
->host_lock
,
1824 pmcraid_initiate_reset(pinstance
);
1825 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
1829 if (fd_ioasc
== PMCRAID_IOASC_TIME_STAMP_OUT_OF_SYNC
) {
1830 pinstance
->timestamp_error
= 1;
1831 pmcraid_set_timestamp(cmd
);
1834 dev_info(&pinstance
->pdev
->dev
,
1835 "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc
);
1837 /* send netlink message for HCAM notification if enabled */
1838 if (!pmcraid_disable_aen
)
1839 pmcraid_notify_ldn(pinstance
);
1841 cmd
= pmcraid_init_hcam(pinstance
, PMCRAID_HCAM_CODE_LOG_DATA
);
1843 pmcraid_send_hcam_cmd(cmd
);
1847 * pmcraid_register_hcams - register HCAMs for CCN and LDN
1849 * @pinstance: pointer per adapter instance structure
1854 static void pmcraid_register_hcams(struct pmcraid_instance
*pinstance
)
1856 pmcraid_send_hcam(pinstance
, PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1857 pmcraid_send_hcam(pinstance
, PMCRAID_HCAM_CODE_LOG_DATA
);
1861 * pmcraid_unregister_hcams - cancel HCAMs registered already
1862 * @cmd: pointer to command used as part of reset sequence
1864 static void pmcraid_unregister_hcams(struct pmcraid_cmd
*cmd
)
1866 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1868 /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
1869 * handling hcam response though it is not necessary. In order to
1870 * prevent this, set 'ignore', so that bring-down sequence doesn't
1871 * re-send any more hcams
1873 atomic_set(&pinstance
->ccn
.ignore
, 1);
1874 atomic_set(&pinstance
->ldn
.ignore
, 1);
1876 /* If adapter reset was forced as part of runtime reset sequence,
1877 * start the reset sequence. Reset will be triggered even in case
1880 if ((pinstance
->force_ioa_reset
&& !pinstance
->ioa_bringdown
) ||
1881 pinstance
->ioa_unit_check
) {
1882 pinstance
->force_ioa_reset
= 0;
1883 pinstance
->ioa_unit_check
= 0;
1884 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
1885 pmcraid_reset_alert(cmd
);
1889 /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
1890 * one after the other. So CCN cancellation will be triggered by
1891 * pmcraid_cancel_ldn itself.
1893 pmcraid_cancel_ldn(cmd
);
1897 * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
1898 * @pinstance: pointer to adapter instance structure
1900 * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
1902 static void pmcraid_reinit_buffers(struct pmcraid_instance
*);
1904 static int pmcraid_reset_enable_ioa(struct pmcraid_instance
*pinstance
)
1908 pmcraid_reinit_buffers(pinstance
);
1909 intrs
= pmcraid_read_interrupts(pinstance
);
1911 pmcraid_enable_interrupts(pinstance
, PMCRAID_PCI_INTERRUPTS
);
1913 if (intrs
& INTRS_TRANSITION_TO_OPERATIONAL
) {
1914 if (!pinstance
->interrupt_mode
) {
1915 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL
,
1916 pinstance
->int_regs
.
1917 ioa_host_interrupt_mask_reg
);
1918 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL
,
1919 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
1928 * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
1929 * @cmd : pointer to reset command block
1934 static void pmcraid_soft_reset(struct pmcraid_cmd
*cmd
)
1936 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1940 /* There will be an interrupt when Transition to Operational bit is
1941 * set so tasklet would execute next reset task. The timeout handler
1942 * would re-initiate a reset
1944 cmd
->cmd_done
= pmcraid_ioa_reset
;
1945 cmd
->timer
.expires
= jiffies
+
1946 msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT
);
1947 cmd
->timer
.function
= pmcraid_timeout_handler
;
1949 if (!timer_pending(&cmd
->timer
))
1950 add_timer(&cmd
->timer
);
1952 /* Enable destructive diagnostics on IOA if it is not yet in
1955 doorbell
= DOORBELL_RUNTIME_RESET
|
1956 DOORBELL_ENABLE_DESTRUCTIVE_DIAGS
;
1958 /* Since we do RESET_ALERT and Start BIST we have to again write
1959 * MSIX Doorbell to indicate the interrupt mode
1961 if (pinstance
->interrupt_mode
) {
1962 iowrite32(DOORBELL_INTR_MODE_MSIX
,
1963 pinstance
->int_regs
.host_ioa_interrupt_reg
);
1964 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
1967 iowrite32(doorbell
, pinstance
->int_regs
.host_ioa_interrupt_reg
);
1968 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
),
1969 int_reg
= ioread32(pinstance
->int_regs
.ioa_host_interrupt_reg
);
1971 pmcraid_info("Waiting for IOA to become operational %x:%x\n",
1972 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
),
1977 * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
1979 * @pinstance: pointer to adapter instance structure
1984 static void pmcraid_get_dump(struct pmcraid_instance
*pinstance
)
1986 pmcraid_info("%s is not yet implemented\n", __func__
);
1990 * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
1991 * @pinstance: pointer to adapter instance structure
1993 * This function fails all outstanding ops. If they are submitted to IOA
1994 * already, it sends cancel all messages if IOA is still accepting IOARCBs,
1995 * otherwise just completes the commands and returns the cmd blocks to free
2001 static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance
*pinstance
)
2003 struct pmcraid_cmd
*cmd
, *temp
;
2004 unsigned long lock_flags
;
2006 /* pending command list is protected by pending_pool_lock. Its
2007 * traversal must be done as within this lock
2009 spin_lock_irqsave(&pinstance
->pending_pool_lock
, lock_flags
);
2010 list_for_each_entry_safe(cmd
, temp
, &pinstance
->pending_cmd_pool
,
2012 list_del(&cmd
->free_list
);
2013 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
,
2015 cmd
->ioa_cb
->ioasa
.ioasc
=
2016 cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET
);
2017 cmd
->ioa_cb
->ioasa
.ilid
=
2018 cpu_to_le32(PMCRAID_DRIVER_ILID
);
2020 /* In case the command timer is still running */
2021 del_timer(&cmd
->timer
);
2023 /* If this is an IO command, complete it by invoking scsi_done
2024 * function. If this is one of the internal commands other
2025 * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
2028 if (cmd
->scsi_cmd
) {
2030 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2031 __le32 resp
= cmd
->ioa_cb
->ioarcb
.response_handle
;
2033 scsi_cmd
->result
|= DID_ERROR
<< 16;
2035 scsi_dma_unmap(scsi_cmd
);
2036 pmcraid_return_cmd(cmd
);
2038 pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
2039 le32_to_cpu(resp
) >> 2,
2040 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2042 scsi_cmd
->scsi_done(scsi_cmd
);
2043 } else if (cmd
->cmd_done
== pmcraid_internal_done
||
2044 cmd
->cmd_done
== pmcraid_erp_done
) {
2046 } else if (cmd
->cmd_done
!= pmcraid_ioa_reset
&&
2047 cmd
->cmd_done
!= pmcraid_ioa_shutdown_done
) {
2048 pmcraid_return_cmd(cmd
);
2051 atomic_dec(&pinstance
->outstanding_cmds
);
2052 spin_lock_irqsave(&pinstance
->pending_pool_lock
, lock_flags
);
2055 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
, lock_flags
);
2059 * pmcraid_ioa_reset - Implementation of IOA reset logic
2061 * @cmd: pointer to the cmd block to be used for entire reset process
2063 * This function executes most of the steps required for IOA reset. This gets
2064 * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
2065 * 'eh_' thread. Access to variables used for controlling the reset sequence is
2066 * synchronized using host lock. Various functions called during reset process
2067 * would make use of a single command block, pointer to which is also stored in
2068 * adapter instance structure.
2073 static void pmcraid_ioa_reset(struct pmcraid_cmd
*cmd
)
2075 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
2076 u8 reset_complete
= 0;
2078 pinstance
->ioa_reset_in_progress
= 1;
2080 if (pinstance
->reset_cmd
!= cmd
) {
2081 pmcraid_err("reset is called with different command block\n");
2082 pinstance
->reset_cmd
= cmd
;
2085 pmcraid_info("reset_engine: state = %d, command = %p\n",
2086 pinstance
->ioa_state
, cmd
);
2088 switch (pinstance
->ioa_state
) {
2090 case IOA_STATE_DEAD
:
2091 /* If IOA is offline, whatever may be the reset reason, just
2092 * return. callers might be waiting on the reset wait_q, wake
2095 pmcraid_err("IOA is offline no reset is possible\n");
2099 case IOA_STATE_IN_BRINGDOWN
:
2100 /* we enter here, once ioa shutdown command is processed by IOA
2101 * Alert IOA for a possible reset. If reset alert fails, IOA
2102 * goes through hard-reset
2104 pmcraid_disable_interrupts(pinstance
, ~0);
2105 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
2106 pmcraid_reset_alert(cmd
);
2109 case IOA_STATE_UNKNOWN
:
2110 /* We may be called during probe or resume. Some pre-processing
2111 * is required for prior to reset
2113 scsi_block_requests(pinstance
->host
);
2115 /* If asked to reset while IOA was processing responses or
2116 * there are any error responses then IOA may require
2119 if (pinstance
->ioa_hard_reset
== 0) {
2120 if (ioread32(pinstance
->ioa_status
) &
2121 INTRS_TRANSITION_TO_OPERATIONAL
) {
2122 pmcraid_info("sticky bit set, bring-up\n");
2123 pinstance
->ioa_state
= IOA_STATE_IN_BRINGUP
;
2124 pmcraid_reinit_cmdblk(cmd
);
2125 pmcraid_identify_hrrq(cmd
);
2127 pinstance
->ioa_state
= IOA_STATE_IN_SOFT_RESET
;
2128 pmcraid_soft_reset(cmd
);
2131 /* Alert IOA of a possible reset and wait for critical
2132 * operation in progress bit to reset
2134 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
2135 pmcraid_reset_alert(cmd
);
2139 case IOA_STATE_IN_RESET_ALERT
:
2140 /* If critical operation in progress bit is reset or wait gets
2141 * timed out, reset proceeds with starting BIST on the IOA.
2142 * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
2143 * they are 3 or more, reset engine marks IOA dead and returns
2145 pinstance
->ioa_state
= IOA_STATE_IN_HARD_RESET
;
2146 pmcraid_start_bist(cmd
);
2149 case IOA_STATE_IN_HARD_RESET
:
2150 pinstance
->ioa_reset_attempts
++;
2152 /* retry reset if we haven't reached maximum allowed limit */
2153 if (pinstance
->ioa_reset_attempts
> PMCRAID_RESET_ATTEMPTS
) {
2154 pinstance
->ioa_reset_attempts
= 0;
2155 pmcraid_err("IOA didn't respond marking it as dead\n");
2156 pinstance
->ioa_state
= IOA_STATE_DEAD
;
2158 if (pinstance
->ioa_bringdown
)
2159 pmcraid_notify_ioastate(pinstance
,
2160 PMC_DEVICE_EVENT_SHUTDOWN_FAILED
);
2162 pmcraid_notify_ioastate(pinstance
,
2163 PMC_DEVICE_EVENT_RESET_FAILED
);
2168 /* Once either bist or pci reset is done, restore PCI config
2169 * space. If this fails, proceed with hard reset again
2171 pci_restore_state(pinstance
->pdev
);
2173 /* fail all pending commands */
2174 pmcraid_fail_outstanding_cmds(pinstance
);
2176 /* check if unit check is active, if so extract dump */
2177 if (pinstance
->ioa_unit_check
) {
2178 pmcraid_info("unit check is active\n");
2179 pinstance
->ioa_unit_check
= 0;
2180 pmcraid_get_dump(pinstance
);
2181 pinstance
->ioa_reset_attempts
--;
2182 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
2183 pmcraid_reset_alert(cmd
);
2187 /* if the reset reason is to bring-down the ioa, we might be
2188 * done with the reset restore pci_config_space and complete
2191 if (pinstance
->ioa_bringdown
) {
2192 pmcraid_info("bringing down the adapter\n");
2193 pinstance
->ioa_shutdown_type
= SHUTDOWN_NONE
;
2194 pinstance
->ioa_bringdown
= 0;
2195 pinstance
->ioa_state
= IOA_STATE_UNKNOWN
;
2196 pmcraid_notify_ioastate(pinstance
,
2197 PMC_DEVICE_EVENT_SHUTDOWN_SUCCESS
);
2200 /* bring-up IOA, so proceed with soft reset
2201 * Reinitialize hrrq_buffers and their indices also
2202 * enable interrupts after a pci_restore_state
2204 if (pmcraid_reset_enable_ioa(pinstance
)) {
2205 pinstance
->ioa_state
= IOA_STATE_IN_BRINGUP
;
2206 pmcraid_info("bringing up the adapter\n");
2207 pmcraid_reinit_cmdblk(cmd
);
2208 pmcraid_identify_hrrq(cmd
);
2210 pinstance
->ioa_state
= IOA_STATE_IN_SOFT_RESET
;
2211 pmcraid_soft_reset(cmd
);
2216 case IOA_STATE_IN_SOFT_RESET
:
2217 /* TRANSITION TO OPERATIONAL is on so start initialization
2220 pmcraid_info("In softreset proceeding with bring-up\n");
2221 pinstance
->ioa_state
= IOA_STATE_IN_BRINGUP
;
2223 /* Initialization commands start with HRRQ identification. From
2224 * now on tasklet completes most of the commands as IOA is up
2225 * and intrs are enabled
2227 pmcraid_identify_hrrq(cmd
);
2230 case IOA_STATE_IN_BRINGUP
:
2231 /* we are done with bringing up of IOA, change the ioa_state to
2232 * operational and wake up any waiters
2234 pinstance
->ioa_state
= IOA_STATE_OPERATIONAL
;
2238 case IOA_STATE_OPERATIONAL
:
2240 /* When IOA is operational and a reset is requested, check for
2241 * the reset reason. If reset is to bring down IOA, unregister
2242 * HCAMs and initiate shutdown; if adapter reset is forced then
2243 * restart reset sequence again
2245 if (pinstance
->ioa_shutdown_type
== SHUTDOWN_NONE
&&
2246 pinstance
->force_ioa_reset
== 0) {
2247 pmcraid_notify_ioastate(pinstance
,
2248 PMC_DEVICE_EVENT_RESET_SUCCESS
);
2251 if (pinstance
->ioa_shutdown_type
!= SHUTDOWN_NONE
)
2252 pinstance
->ioa_state
= IOA_STATE_IN_BRINGDOWN
;
2253 pmcraid_reinit_cmdblk(cmd
);
2254 pmcraid_unregister_hcams(cmd
);
2259 /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
2260 * OPERATIONAL. Reset all control variables used during reset, wake up
2261 * any waiting threads and let the SCSI mid-layer send commands. Note
2262 * that host_lock must be held before invoking scsi_report_bus_reset.
2264 if (reset_complete
) {
2265 pinstance
->ioa_reset_in_progress
= 0;
2266 pinstance
->ioa_reset_attempts
= 0;
2267 pinstance
->reset_cmd
= NULL
;
2268 pinstance
->ioa_shutdown_type
= SHUTDOWN_NONE
;
2269 pinstance
->ioa_bringdown
= 0;
2270 pmcraid_return_cmd(cmd
);
2272 /* If target state is to bring up the adapter, proceed with
2273 * hcam registration and resource exposure to mid-layer.
2275 if (pinstance
->ioa_state
== IOA_STATE_OPERATIONAL
)
2276 pmcraid_register_hcams(pinstance
);
2278 wake_up_all(&pinstance
->reset_wait_q
);
2285 * pmcraid_initiate_reset - initiates reset sequence. This is called from
2286 * ISR/tasklet during error interrupts including IOA unit check. If reset
2287 * is already in progress, it just returns, otherwise initiates IOA reset
2288 * to bring IOA up to operational state.
2290 * @pinstance: pointer to adapter instance structure
2295 static void pmcraid_initiate_reset(struct pmcraid_instance
*pinstance
)
2297 struct pmcraid_cmd
*cmd
;
2299 /* If the reset is already in progress, just return, otherwise start
2300 * reset sequence and return
2302 if (!pinstance
->ioa_reset_in_progress
) {
2303 scsi_block_requests(pinstance
->host
);
2304 cmd
= pmcraid_get_free_cmd(pinstance
);
2307 pmcraid_err("no cmnd blocks for initiate_reset\n");
2311 pinstance
->ioa_shutdown_type
= SHUTDOWN_NONE
;
2312 pinstance
->reset_cmd
= cmd
;
2313 pinstance
->force_ioa_reset
= 1;
2314 pmcraid_notify_ioastate(pinstance
,
2315 PMC_DEVICE_EVENT_RESET_START
);
2316 pmcraid_ioa_reset(cmd
);
2321 * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
2323 * @pinstance: pointer adapter instance structure
2324 * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
2325 * @target_state: expected target state after reset
2327 * Note: This command initiates reset and waits for its completion. Hence this
2328 * should not be called from isr/timer/tasklet functions (timeout handlers,
2329 * error response handlers and interrupt handlers).
2332 * 1 in case ioa_state is not target_state, 0 otherwise.
2334 static int pmcraid_reset_reload(
2335 struct pmcraid_instance
*pinstance
,
2340 struct pmcraid_cmd
*reset_cmd
= NULL
;
2341 unsigned long lock_flags
;
2344 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
2346 if (pinstance
->ioa_reset_in_progress
) {
2347 pmcraid_info("reset_reload: reset is already in progress\n");
2349 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2351 wait_event(pinstance
->reset_wait_q
,
2352 !pinstance
->ioa_reset_in_progress
);
2354 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
2356 if (pinstance
->ioa_state
== IOA_STATE_DEAD
) {
2357 pmcraid_info("reset_reload: IOA is dead\n");
2361 if (pinstance
->ioa_state
== target_state
) {
2367 pmcraid_info("reset_reload: proceeding with reset\n");
2368 scsi_block_requests(pinstance
->host
);
2369 reset_cmd
= pmcraid_get_free_cmd(pinstance
);
2370 if (reset_cmd
== NULL
) {
2371 pmcraid_err("no free cmnd for reset_reload\n");
2375 if (shutdown_type
== SHUTDOWN_NORMAL
)
2376 pinstance
->ioa_bringdown
= 1;
2378 pinstance
->ioa_shutdown_type
= shutdown_type
;
2379 pinstance
->reset_cmd
= reset_cmd
;
2380 pinstance
->force_ioa_reset
= reset
;
2381 pmcraid_info("reset_reload: initiating reset\n");
2382 pmcraid_ioa_reset(reset_cmd
);
2383 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2384 pmcraid_info("reset_reload: waiting for reset to complete\n");
2385 wait_event(pinstance
->reset_wait_q
,
2386 !pinstance
->ioa_reset_in_progress
);
2388 pmcraid_info("reset_reload: reset is complete !!\n");
2389 scsi_unblock_requests(pinstance
->host
);
2390 return pinstance
->ioa_state
!= target_state
;
2393 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2398 * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
2400 * @pinstance: pointer to adapter instance structure
2403 * whatever is returned from pmcraid_reset_reload
2405 static int pmcraid_reset_bringdown(struct pmcraid_instance
*pinstance
)
2407 return pmcraid_reset_reload(pinstance
,
2413 * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
2415 * @pinstance: pointer to adapter instance structure
2418 * whatever is returned from pmcraid_reset_reload
2420 static int pmcraid_reset_bringup(struct pmcraid_instance
*pinstance
)
2422 pmcraid_notify_ioastate(pinstance
, PMC_DEVICE_EVENT_RESET_START
);
2424 return pmcraid_reset_reload(pinstance
,
2426 IOA_STATE_OPERATIONAL
);
2430 * pmcraid_request_sense - Send request sense to a device
2431 * @cmd: pmcraid command struct
2433 * This function sends a request sense to a device as a result of a check
2434 * condition. This method re-uses the same command block that failed earlier.
2436 static void pmcraid_request_sense(struct pmcraid_cmd
*cmd
)
2438 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
2439 struct pmcraid_ioadl_desc
*ioadl
= ioarcb
->add_data
.u
.ioadl
;
2440 struct device
*dev
= &cmd
->drv_inst
->pdev
->dev
;
2442 cmd
->sense_buffer
= cmd
->scsi_cmd
->sense_buffer
;
2443 cmd
->sense_buffer_dma
= dma_map_single(dev
, cmd
->sense_buffer
,
2444 SCSI_SENSE_BUFFERSIZE
, DMA_FROM_DEVICE
);
2445 if (dma_mapping_error(dev
, cmd
->sense_buffer_dma
)) {
2447 ("couldn't allocate sense buffer for request sense\n");
2448 pmcraid_erp_done(cmd
);
2452 /* re-use the command block */
2453 memset(&cmd
->ioa_cb
->ioasa
, 0, sizeof(struct pmcraid_ioasa
));
2454 memset(ioarcb
->cdb
, 0, PMCRAID_MAX_CDB_LEN
);
2455 ioarcb
->request_flags0
= (SYNC_COMPLETE
|
2458 ioarcb
->request_type
= REQ_TYPE_SCSI
;
2459 ioarcb
->cdb
[0] = REQUEST_SENSE
;
2460 ioarcb
->cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
2462 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
2463 offsetof(struct pmcraid_ioarcb
,
2464 add_data
.u
.ioadl
[0]));
2465 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
2467 ioarcb
->data_transfer_length
= cpu_to_le32(SCSI_SENSE_BUFFERSIZE
);
2469 ioadl
->address
= cpu_to_le64(cmd
->sense_buffer_dma
);
2470 ioadl
->data_len
= cpu_to_le32(SCSI_SENSE_BUFFERSIZE
);
2471 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
2473 /* request sense might be called as part of error response processing
2474 * which runs in tasklets context. It is possible that mid-layer might
2475 * schedule queuecommand during this time, hence, writting to IOARRIN
2476 * must be protect by host_lock
2478 pmcraid_send_cmd(cmd
, pmcraid_erp_done
,
2479 PMCRAID_REQUEST_SENSE_TIMEOUT
,
2480 pmcraid_timeout_handler
);
2484 * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
2485 * @cmd: command that failed
2486 * @need_sense: true if request_sense is required after cancel all
2488 * This function sends a cancel all to a device to clear the queue.
2490 static void pmcraid_cancel_all(struct pmcraid_cmd
*cmd
, bool need_sense
)
2492 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2493 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
2494 struct pmcraid_resource_entry
*res
= scsi_cmd
->device
->hostdata
;
2496 memset(ioarcb
->cdb
, 0, PMCRAID_MAX_CDB_LEN
);
2497 ioarcb
->request_flags0
= SYNC_OVERRIDE
;
2498 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
2499 ioarcb
->cdb
[0] = PMCRAID_CANCEL_ALL_REQUESTS
;
2501 if (RES_IS_GSCSI(res
->cfg_entry
))
2502 ioarcb
->cdb
[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL
;
2504 ioarcb
->ioadl_bus_addr
= 0;
2505 ioarcb
->ioadl_length
= 0;
2506 ioarcb
->data_transfer_length
= 0;
2507 ioarcb
->ioarcb_bus_addr
&= cpu_to_le64((~0x1FULL
));
2509 /* writing to IOARRIN must be protected by host_lock, as mid-layer
2510 * schedule queuecommand while we are doing this
2512 pmcraid_send_cmd(cmd
, need_sense
?
2513 pmcraid_erp_done
: pmcraid_request_sense
,
2514 PMCRAID_REQUEST_SENSE_TIMEOUT
,
2515 pmcraid_timeout_handler
);
2519 * pmcraid_frame_auto_sense: frame fixed format sense information
2521 * @cmd: pointer to failing command block
2526 static void pmcraid_frame_auto_sense(struct pmcraid_cmd
*cmd
)
2528 u8
*sense_buf
= cmd
->scsi_cmd
->sense_buffer
;
2529 struct pmcraid_resource_entry
*res
= cmd
->scsi_cmd
->device
->hostdata
;
2530 struct pmcraid_ioasa
*ioasa
= &cmd
->ioa_cb
->ioasa
;
2531 u32 ioasc
= le32_to_cpu(ioasa
->ioasc
);
2532 u32 failing_lba
= 0;
2534 memset(sense_buf
, 0, SCSI_SENSE_BUFFERSIZE
);
2535 cmd
->scsi_cmd
->result
= SAM_STAT_CHECK_CONDITION
;
2537 if (RES_IS_VSET(res
->cfg_entry
) &&
2538 ioasc
== PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC
&&
2539 ioasa
->u
.vset
.failing_lba_hi
!= 0) {
2541 sense_buf
[0] = 0x72;
2542 sense_buf
[1] = PMCRAID_IOASC_SENSE_KEY(ioasc
);
2543 sense_buf
[2] = PMCRAID_IOASC_SENSE_CODE(ioasc
);
2544 sense_buf
[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc
);
2548 sense_buf
[9] = 0x0A;
2549 sense_buf
[10] = 0x80;
2551 failing_lba
= le32_to_cpu(ioasa
->u
.vset
.failing_lba_hi
);
2553 sense_buf
[12] = (failing_lba
& 0xff000000) >> 24;
2554 sense_buf
[13] = (failing_lba
& 0x00ff0000) >> 16;
2555 sense_buf
[14] = (failing_lba
& 0x0000ff00) >> 8;
2556 sense_buf
[15] = failing_lba
& 0x000000ff;
2558 failing_lba
= le32_to_cpu(ioasa
->u
.vset
.failing_lba_lo
);
2560 sense_buf
[16] = (failing_lba
& 0xff000000) >> 24;
2561 sense_buf
[17] = (failing_lba
& 0x00ff0000) >> 16;
2562 sense_buf
[18] = (failing_lba
& 0x0000ff00) >> 8;
2563 sense_buf
[19] = failing_lba
& 0x000000ff;
2565 sense_buf
[0] = 0x70;
2566 sense_buf
[2] = PMCRAID_IOASC_SENSE_KEY(ioasc
);
2567 sense_buf
[12] = PMCRAID_IOASC_SENSE_CODE(ioasc
);
2568 sense_buf
[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc
);
2570 if (ioasc
== PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC
) {
2571 if (RES_IS_VSET(res
->cfg_entry
))
2573 le32_to_cpu(ioasa
->u
.
2574 vset
.failing_lba_lo
);
2575 sense_buf
[0] |= 0x80;
2576 sense_buf
[3] = (failing_lba
>> 24) & 0xff;
2577 sense_buf
[4] = (failing_lba
>> 16) & 0xff;
2578 sense_buf
[5] = (failing_lba
>> 8) & 0xff;
2579 sense_buf
[6] = failing_lba
& 0xff;
2582 sense_buf
[7] = 6; /* additional length */
2587 * pmcraid_error_handler - Error response handlers for a SCSI op
2588 * @cmd: pointer to pmcraid_cmd that has failed
2590 * This function determines whether or not to initiate ERP on the affected
2591 * device. This is called from a tasklet, which doesn't hold any locks.
2594 * 0 it caller can complete the request, otherwise 1 where in error
2595 * handler itself completes the request and returns the command block
2598 static int pmcraid_error_handler(struct pmcraid_cmd
*cmd
)
2600 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2601 struct pmcraid_resource_entry
*res
= scsi_cmd
->device
->hostdata
;
2602 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
2603 struct pmcraid_ioasa
*ioasa
= &cmd
->ioa_cb
->ioasa
;
2604 u32 ioasc
= le32_to_cpu(ioasa
->ioasc
);
2605 u32 masked_ioasc
= ioasc
& PMCRAID_IOASC_SENSE_MASK
;
2606 bool sense_copied
= false;
2609 pmcraid_info("resource pointer is NULL\n");
2613 /* If this was a SCSI read/write command keep count of errors */
2614 if (SCSI_CMD_TYPE(scsi_cmd
->cmnd
[0]) == SCSI_READ_CMD
)
2615 atomic_inc(&res
->read_failures
);
2616 else if (SCSI_CMD_TYPE(scsi_cmd
->cmnd
[0]) == SCSI_WRITE_CMD
)
2617 atomic_inc(&res
->write_failures
);
2619 if (!RES_IS_GSCSI(res
->cfg_entry
) &&
2620 masked_ioasc
!= PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR
) {
2621 pmcraid_frame_auto_sense(cmd
);
2624 /* Log IOASC/IOASA information based on user settings */
2625 pmcraid_ioasc_logger(ioasc
, cmd
);
2627 switch (masked_ioasc
) {
2629 case PMCRAID_IOASC_AC_TERMINATED_BY_HOST
:
2630 scsi_cmd
->result
|= (DID_ABORT
<< 16);
2633 case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE
:
2634 case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE
:
2635 scsi_cmd
->result
|= (DID_NO_CONNECT
<< 16);
2638 case PMCRAID_IOASC_NR_SYNC_REQUIRED
:
2640 scsi_cmd
->result
|= (DID_IMM_RETRY
<< 16);
2643 case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC
:
2644 scsi_cmd
->result
|= (DID_PASSTHROUGH
<< 16);
2647 case PMCRAID_IOASC_UA_BUS_WAS_RESET
:
2648 case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER
:
2649 if (!res
->reset_progress
)
2650 scsi_report_bus_reset(pinstance
->host
,
2651 scsi_cmd
->device
->channel
);
2652 scsi_cmd
->result
|= (DID_ERROR
<< 16);
2655 case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR
:
2656 scsi_cmd
->result
|= PMCRAID_IOASC_SENSE_STATUS(ioasc
);
2659 /* if check_condition is not active return with error otherwise
2660 * get/frame the sense buffer
2662 if (PMCRAID_IOASC_SENSE_STATUS(ioasc
) !=
2663 SAM_STAT_CHECK_CONDITION
&&
2664 PMCRAID_IOASC_SENSE_STATUS(ioasc
) != SAM_STAT_ACA_ACTIVE
)
2667 /* If we have auto sense data as part of IOASA pass it to
2670 if (ioasa
->auto_sense_length
!= 0) {
2671 short sense_len
= le16_to_cpu(ioasa
->auto_sense_length
);
2672 int data_size
= min_t(u16
, sense_len
,
2673 SCSI_SENSE_BUFFERSIZE
);
2675 memcpy(scsi_cmd
->sense_buffer
,
2678 sense_copied
= true;
2681 if (RES_IS_GSCSI(res
->cfg_entry
))
2682 pmcraid_cancel_all(cmd
, sense_copied
);
2683 else if (sense_copied
)
2684 pmcraid_erp_done(cmd
);
2686 pmcraid_request_sense(cmd
);
2690 case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED
:
2694 if (PMCRAID_IOASC_SENSE_KEY(ioasc
) > RECOVERED_ERROR
)
2695 scsi_cmd
->result
|= (DID_ERROR
<< 16);
2702 * pmcraid_reset_device - device reset handler functions
2704 * @scsi_cmd: scsi command struct
2705 * @modifier: reset modifier indicating the reset sequence to be performed
2707 * This function issues a device reset to the affected device.
2708 * A LUN reset will be sent to the device first. If that does
2709 * not work, a target reset will be sent.
2714 static int pmcraid_reset_device(
2715 struct scsi_cmnd
*scsi_cmd
,
2716 unsigned long timeout
,
2720 struct pmcraid_cmd
*cmd
;
2721 struct pmcraid_instance
*pinstance
;
2722 struct pmcraid_resource_entry
*res
;
2723 struct pmcraid_ioarcb
*ioarcb
;
2724 unsigned long lock_flags
;
2728 (struct pmcraid_instance
*)scsi_cmd
->device
->host
->hostdata
;
2729 res
= scsi_cmd
->device
->hostdata
;
2732 sdev_printk(KERN_ERR
, scsi_cmd
->device
,
2733 "reset_device: NULL resource pointer\n");
2737 /* If adapter is currently going through reset/reload, return failed.
2738 * This will force the mid-layer to call _eh_bus/host reset, which
2739 * will then go to sleep and wait for the reset to complete
2741 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
2742 if (pinstance
->ioa_reset_in_progress
||
2743 pinstance
->ioa_state
== IOA_STATE_DEAD
) {
2744 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2748 res
->reset_progress
= 1;
2749 pmcraid_info("Resetting %s resource with addr %x\n",
2750 ((modifier
& RESET_DEVICE_LUN
) ? "LUN" :
2751 ((modifier
& RESET_DEVICE_TARGET
) ? "TARGET" : "BUS")),
2752 le32_to_cpu(res
->cfg_entry
.resource_address
));
2754 /* get a free cmd block */
2755 cmd
= pmcraid_get_free_cmd(pinstance
);
2758 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2759 pmcraid_err("%s: no cmd blocks are available\n", __func__
);
2763 ioarcb
= &cmd
->ioa_cb
->ioarcb
;
2764 ioarcb
->resource_handle
= res
->cfg_entry
.resource_handle
;
2765 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
2766 ioarcb
->cdb
[0] = PMCRAID_RESET_DEVICE
;
2768 /* Initialize reset modifier bits */
2770 modifier
= ENABLE_RESET_MODIFIER
| modifier
;
2772 ioarcb
->cdb
[1] = modifier
;
2774 init_completion(&cmd
->wait_for_completion
);
2775 cmd
->completion_req
= 1;
2777 pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
2778 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2779 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.resource_handle
),
2780 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2);
2782 pmcraid_send_cmd(cmd
,
2783 pmcraid_internal_done
,
2785 pmcraid_timeout_handler
);
2787 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2789 /* RESET_DEVICE command completes after all pending IOARCBs are
2790 * completed. Once this command is completed, pmcraind_internal_done
2791 * will wake up the 'completion' queue.
2793 wait_for_completion(&cmd
->wait_for_completion
);
2795 /* complete the command here itself and return the command block
2798 pmcraid_return_cmd(cmd
);
2799 res
->reset_progress
= 0;
2800 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
2802 /* set the return value based on the returned ioasc */
2803 return PMCRAID_IOASC_SENSE_KEY(ioasc
) ? FAILED
: SUCCESS
;
2807 * _pmcraid_io_done - helper for pmcraid_io_done function
2809 * @cmd: pointer to pmcraid command struct
2810 * @reslen: residual data length to be set in the ioasa
2811 * @ioasc: ioasc either returned by IOA or set by driver itself.
2813 * This function is invoked by pmcraid_io_done to complete mid-layer
2817 * 0 if caller is required to return it to free_pool. Returns 1 if
2818 * caller need not worry about freeing command block as error handler
2819 * will take care of that.
2822 static int _pmcraid_io_done(struct pmcraid_cmd
*cmd
, int reslen
, int ioasc
)
2824 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2827 scsi_set_resid(scsi_cmd
, reslen
);
2829 pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
2830 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2,
2831 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2832 ioasc
, scsi_cmd
->result
);
2834 if (PMCRAID_IOASC_SENSE_KEY(ioasc
) != 0)
2835 rc
= pmcraid_error_handler(cmd
);
2838 scsi_dma_unmap(scsi_cmd
);
2839 scsi_cmd
->scsi_done(scsi_cmd
);
2846 * pmcraid_io_done - SCSI completion function
2848 * @cmd: pointer to pmcraid command struct
2850 * This function is invoked by tasklet/mid-layer error handler to completing
2851 * the SCSI ops sent from mid-layer.
2857 static void pmcraid_io_done(struct pmcraid_cmd
*cmd
)
2859 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
2860 u32 reslen
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.residual_data_length
);
2862 if (_pmcraid_io_done(cmd
, reslen
, ioasc
) == 0)
2863 pmcraid_return_cmd(cmd
);
2867 * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
2869 * @cmd: command block of the command to be aborted
2872 * returns pointer to command structure used as cancelling cmd
2874 static struct pmcraid_cmd
*pmcraid_abort_cmd(struct pmcraid_cmd
*cmd
)
2876 struct pmcraid_cmd
*cancel_cmd
;
2877 struct pmcraid_instance
*pinstance
;
2878 struct pmcraid_resource_entry
*res
;
2880 pinstance
= (struct pmcraid_instance
*)cmd
->drv_inst
;
2881 res
= cmd
->scsi_cmd
->device
->hostdata
;
2883 cancel_cmd
= pmcraid_get_free_cmd(pinstance
);
2885 if (cancel_cmd
== NULL
) {
2886 pmcraid_err("%s: no cmd blocks are available\n", __func__
);
2890 pmcraid_prepare_cancel_cmd(cancel_cmd
, cmd
);
2892 pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
2893 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2894 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2);
2896 init_completion(&cancel_cmd
->wait_for_completion
);
2897 cancel_cmd
->completion_req
= 1;
2899 pmcraid_info("command (%d) CDB[0] = %x for %x\n",
2900 le32_to_cpu(cancel_cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2,
2901 cancel_cmd
->ioa_cb
->ioarcb
.cdb
[0],
2902 le32_to_cpu(cancel_cmd
->ioa_cb
->ioarcb
.resource_handle
));
2904 pmcraid_send_cmd(cancel_cmd
,
2905 pmcraid_internal_done
,
2906 PMCRAID_INTERNAL_TIMEOUT
,
2907 pmcraid_timeout_handler
);
2912 * pmcraid_abort_complete - Waits for ABORT TASK completion
2914 * @cancel_cmd: command block use as cancelling command
2917 * returns SUCCESS if ABORT TASK has good completion
2920 static int pmcraid_abort_complete(struct pmcraid_cmd
*cancel_cmd
)
2922 struct pmcraid_resource_entry
*res
;
2925 wait_for_completion(&cancel_cmd
->wait_for_completion
);
2926 res
= cancel_cmd
->res
;
2927 cancel_cmd
->res
= NULL
;
2928 ioasc
= le32_to_cpu(cancel_cmd
->ioa_cb
->ioasa
.ioasc
);
2930 /* If the abort task is not timed out we will get a Good completion
2931 * as sense_key, otherwise we may get one the following responses
2932 * due to subsequent bus reset or device reset. In case IOASC is
2933 * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
2935 if (ioasc
== PMCRAID_IOASC_UA_BUS_WAS_RESET
||
2936 ioasc
== PMCRAID_IOASC_NR_SYNC_REQUIRED
) {
2937 if (ioasc
== PMCRAID_IOASC_NR_SYNC_REQUIRED
)
2942 /* complete the command here itself */
2943 pmcraid_return_cmd(cancel_cmd
);
2944 return PMCRAID_IOASC_SENSE_KEY(ioasc
) ? FAILED
: SUCCESS
;
2948 * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
2950 * @scsi_cmd: scsi command struct given by mid-layer. When this is called
2951 * mid-layer ensures that no other commands are queued. This
2952 * never gets called under interrupt, but a separate eh thread.
2957 static int pmcraid_eh_abort_handler(struct scsi_cmnd
*scsi_cmd
)
2959 struct pmcraid_instance
*pinstance
;
2960 struct pmcraid_cmd
*cmd
;
2961 struct pmcraid_resource_entry
*res
;
2962 unsigned long host_lock_flags
;
2963 unsigned long pending_lock_flags
;
2964 struct pmcraid_cmd
*cancel_cmd
= NULL
;
2969 (struct pmcraid_instance
*)scsi_cmd
->device
->host
->hostdata
;
2971 scmd_printk(KERN_INFO
, scsi_cmd
,
2972 "I/O command timed out, aborting it.\n");
2974 res
= scsi_cmd
->device
->hostdata
;
2979 /* If we are currently going through reset/reload, return failed.
2980 * This will force the mid-layer to eventually call
2981 * pmcraid_eh_host_reset which will then go to sleep and wait for the
2984 spin_lock_irqsave(pinstance
->host
->host_lock
, host_lock_flags
);
2986 if (pinstance
->ioa_reset_in_progress
||
2987 pinstance
->ioa_state
== IOA_STATE_DEAD
) {
2988 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
2993 /* loop over pending cmd list to find cmd corresponding to this
2994 * scsi_cmd. Note that this command might not have been completed
2995 * already. locking: all pending commands are protected with
2996 * pending_pool_lock.
2998 spin_lock_irqsave(&pinstance
->pending_pool_lock
, pending_lock_flags
);
2999 list_for_each_entry(cmd
, &pinstance
->pending_cmd_pool
, free_list
) {
3001 if (cmd
->scsi_cmd
== scsi_cmd
) {
3007 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
,
3008 pending_lock_flags
);
3010 /* If the command to be aborted was given to IOA and still pending with
3011 * it, send ABORT_TASK to abort this and wait for its completion
3014 cancel_cmd
= pmcraid_abort_cmd(cmd
);
3016 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
3020 cancel_cmd
->res
= cmd
->scsi_cmd
->device
->hostdata
;
3021 rc
= pmcraid_abort_complete(cancel_cmd
);
3024 return cmd_found
? rc
: SUCCESS
;
3028 * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
3030 * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
3032 * All these routines invokve pmcraid_reset_device with appropriate parameters.
3033 * Since these are called from mid-layer EH thread, no other IO will be queued
3034 * to the resource being reset. However, control path (IOCTL) may be active so
3035 * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
3036 * takes care by locking/unlocking host_lock.
3041 static int pmcraid_eh_device_reset_handler(struct scsi_cmnd
*scmd
)
3043 scmd_printk(KERN_INFO
, scmd
,
3044 "resetting device due to an I/O command timeout.\n");
3045 return pmcraid_reset_device(scmd
,
3046 PMCRAID_INTERNAL_TIMEOUT
,
3050 static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd
*scmd
)
3052 scmd_printk(KERN_INFO
, scmd
,
3053 "Doing bus reset due to an I/O command timeout.\n");
3054 return pmcraid_reset_device(scmd
,
3055 PMCRAID_RESET_BUS_TIMEOUT
,
3059 static int pmcraid_eh_target_reset_handler(struct scsi_cmnd
*scmd
)
3061 scmd_printk(KERN_INFO
, scmd
,
3062 "Doing target reset due to an I/O command timeout.\n");
3063 return pmcraid_reset_device(scmd
,
3064 PMCRAID_INTERNAL_TIMEOUT
,
3065 RESET_DEVICE_TARGET
);
3069 * pmcraid_eh_host_reset_handler - adapter reset handler callback
3071 * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
3073 * Initiates adapter reset to bring it up to operational state
3078 static int pmcraid_eh_host_reset_handler(struct scsi_cmnd
*scmd
)
3080 unsigned long interval
= 10000; /* 10 seconds interval */
3081 int waits
= jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT
) / interval
;
3082 struct pmcraid_instance
*pinstance
=
3083 (struct pmcraid_instance
*)(scmd
->device
->host
->hostdata
);
3086 /* wait for an additional 150 seconds just in case firmware could come
3087 * up and if it could complete all the pending commands excluding the
3088 * two HCAM (CCN and LDN).
3091 if (atomic_read(&pinstance
->outstanding_cmds
) <=
3092 PMCRAID_MAX_HCAM_CMD
)
3097 dev_err(&pinstance
->pdev
->dev
,
3098 "Adapter being reset due to an I/O command timeout.\n");
3099 return pmcraid_reset_bringup(pinstance
) == 0 ? SUCCESS
: FAILED
;
3103 * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
3104 * @cmd: pmcraid command struct
3105 * @sgcount: count of scatter-gather elements
3108 * returns pointer pmcraid_ioadl_desc, initialized to point to internal
3109 * or external IOADLs
3111 static struct pmcraid_ioadl_desc
*
3112 pmcraid_init_ioadls(struct pmcraid_cmd
*cmd
, int sgcount
)
3114 struct pmcraid_ioadl_desc
*ioadl
;
3115 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
3116 int ioadl_count
= 0;
3118 if (ioarcb
->add_cmd_param_length
)
3119 ioadl_count
= DIV_ROUND_UP(le16_to_cpu(ioarcb
->add_cmd_param_length
), 16);
3120 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
) * sgcount
);
3122 if ((sgcount
+ ioadl_count
) > (ARRAY_SIZE(ioarcb
->add_data
.u
.ioadl
))) {
3123 /* external ioadls start at offset 0x80 from control_block
3124 * structure, re-using 24 out of 27 ioadls part of IOARCB.
3125 * It is necessary to indicate to firmware that driver is
3126 * using ioadls to be treated as external to IOARCB.
3128 ioarcb
->ioarcb_bus_addr
&= cpu_to_le64(~(0x1FULL
));
3129 ioarcb
->ioadl_bus_addr
=
3130 cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
3131 offsetof(struct pmcraid_ioarcb
,
3132 add_data
.u
.ioadl
[3]));
3133 ioadl
= &ioarcb
->add_data
.u
.ioadl
[3];
3135 ioarcb
->ioadl_bus_addr
=
3136 cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
3137 offsetof(struct pmcraid_ioarcb
,
3138 add_data
.u
.ioadl
[ioadl_count
]));
3140 ioadl
= &ioarcb
->add_data
.u
.ioadl
[ioadl_count
];
3141 ioarcb
->ioarcb_bus_addr
|=
3142 cpu_to_le64(DIV_ROUND_CLOSEST(sgcount
+ ioadl_count
, 8));
3149 * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
3150 * @pinstance: pointer to adapter instance structure
3151 * @cmd: pmcraid command struct
3153 * This function is invoked by queuecommand entry point while sending a command
3154 * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
3157 * 0 on success or -1 on failure
3159 static int pmcraid_build_ioadl(
3160 struct pmcraid_instance
*pinstance
,
3161 struct pmcraid_cmd
*cmd
3165 struct scatterlist
*sglist
;
3167 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
3168 struct pmcraid_ioarcb
*ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
3169 struct pmcraid_ioadl_desc
*ioadl
;
3171 u32 length
= scsi_bufflen(scsi_cmd
);
3176 nseg
= scsi_dma_map(scsi_cmd
);
3179 scmd_printk(KERN_ERR
, scsi_cmd
, "scsi_map_dma failed!\n");
3181 } else if (nseg
> PMCRAID_MAX_IOADLS
) {
3182 scsi_dma_unmap(scsi_cmd
);
3183 scmd_printk(KERN_ERR
, scsi_cmd
,
3184 "sg count is (%d) more than allowed!\n", nseg
);
3188 /* Initialize IOARCB data transfer length fields */
3189 if (scsi_cmd
->sc_data_direction
== DMA_TO_DEVICE
)
3190 ioarcb
->request_flags0
|= TRANSFER_DIR_WRITE
;
3192 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
3193 ioarcb
->data_transfer_length
= cpu_to_le32(length
);
3194 ioadl
= pmcraid_init_ioadls(cmd
, nseg
);
3196 /* Initialize IOADL descriptor addresses */
3197 scsi_for_each_sg(scsi_cmd
, sglist
, nseg
, i
) {
3198 ioadl
[i
].data_len
= cpu_to_le32(sg_dma_len(sglist
));
3199 ioadl
[i
].address
= cpu_to_le64(sg_dma_address(sglist
));
3202 /* setup last descriptor */
3203 ioadl
[i
- 1].flags
= IOADL_FLAGS_LAST_DESC
;
3209 * pmcraid_free_sglist - Frees an allocated SG buffer list
3210 * @sglist: scatter/gather list pointer
3212 * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
3217 static void pmcraid_free_sglist(struct pmcraid_sglist
*sglist
)
3219 sgl_free_order(sglist
->scatterlist
, sglist
->order
);
3224 * pmcraid_alloc_sglist - Allocates memory for a SG list
3225 * @buflen: buffer length
3227 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3231 * pointer to sglist / NULL on failure
3233 static struct pmcraid_sglist
*pmcraid_alloc_sglist(int buflen
)
3235 struct pmcraid_sglist
*sglist
;
3239 sg_size
= buflen
/ (PMCRAID_MAX_IOADLS
- 1);
3240 order
= (sg_size
> 0) ? get_order(sg_size
) : 0;
3242 /* Allocate a scatter/gather list for the DMA */
3243 sglist
= kzalloc(sizeof(struct pmcraid_sglist
), GFP_KERNEL
);
3247 sglist
->order
= order
;
3248 sgl_alloc_order(buflen
, order
, false,
3249 GFP_KERNEL
| GFP_DMA
| __GFP_ZERO
, &sglist
->num_sg
);
3255 * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
3256 * @sglist: scatter/gather list pointer
3257 * @buffer: buffer pointer
3258 * @len: buffer length
3259 * @direction: data transfer direction
3261 * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
3264 * 0 on success / other on failure
3266 static int pmcraid_copy_sglist(
3267 struct pmcraid_sglist
*sglist
,
3268 void __user
*buffer
,
3273 struct scatterlist
*scatterlist
;
3279 /* Determine the actual number of bytes per element */
3280 bsize_elem
= PAGE_SIZE
* (1 << sglist
->order
);
3282 scatterlist
= sglist
->scatterlist
;
3284 for (i
= 0; i
< (len
/ bsize_elem
); i
++, buffer
+= bsize_elem
) {
3285 struct page
*page
= sg_page(&scatterlist
[i
]);
3288 if (direction
== DMA_TO_DEVICE
)
3289 rc
= copy_from_user(kaddr
, buffer
, bsize_elem
);
3291 rc
= copy_to_user(buffer
, kaddr
, bsize_elem
);
3296 pmcraid_err("failed to copy user data into sg list\n");
3300 scatterlist
[i
].length
= bsize_elem
;
3303 if (len
% bsize_elem
) {
3304 struct page
*page
= sg_page(&scatterlist
[i
]);
3308 if (direction
== DMA_TO_DEVICE
)
3309 rc
= copy_from_user(kaddr
, buffer
, len
% bsize_elem
);
3311 rc
= copy_to_user(buffer
, kaddr
, len
% bsize_elem
);
3315 scatterlist
[i
].length
= len
% bsize_elem
;
3319 pmcraid_err("failed to copy user data into sg list\n");
3327 * pmcraid_queuecommand - Queue a mid-layer request
3328 * @scsi_cmd: scsi command struct
3329 * @done: done function
3331 * This function queues a request generated by the mid-layer. Midlayer calls
3332 * this routine within host->lock. Some of the functions called by queuecommand
3333 * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
3337 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3338 * SCSI_MLQUEUE_HOST_BUSY if host is busy
3340 static int pmcraid_queuecommand_lck(
3341 struct scsi_cmnd
*scsi_cmd
,
3342 void (*done
) (struct scsi_cmnd
*)
3345 struct pmcraid_instance
*pinstance
;
3346 struct pmcraid_resource_entry
*res
;
3347 struct pmcraid_ioarcb
*ioarcb
;
3348 struct pmcraid_cmd
*cmd
;
3353 (struct pmcraid_instance
*)scsi_cmd
->device
->host
->hostdata
;
3354 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
3355 scsi_cmd
->scsi_done
= done
;
3356 res
= scsi_cmd
->device
->hostdata
;
3357 scsi_cmd
->result
= (DID_OK
<< 16);
3359 /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
3362 if (pinstance
->ioa_state
== IOA_STATE_DEAD
) {
3363 pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
3364 scsi_cmd
->result
= (DID_NO_CONNECT
<< 16);
3365 scsi_cmd
->scsi_done(scsi_cmd
);
3369 /* If IOA reset is in progress, can't queue the commands */
3370 if (pinstance
->ioa_reset_in_progress
)
3371 return SCSI_MLQUEUE_HOST_BUSY
;
3373 /* Firmware doesn't support SYNCHRONIZE_CACHE command (0x35), complete
3374 * the command here itself with success return
3376 if (scsi_cmd
->cmnd
[0] == SYNCHRONIZE_CACHE
) {
3377 pmcraid_info("SYNC_CACHE(0x35), completing in driver itself\n");
3378 scsi_cmd
->scsi_done(scsi_cmd
);
3382 /* initialize the command and IOARCB to be sent to IOA */
3383 cmd
= pmcraid_get_free_cmd(pinstance
);
3386 pmcraid_err("free command block is not available\n");
3387 return SCSI_MLQUEUE_HOST_BUSY
;
3390 cmd
->scsi_cmd
= scsi_cmd
;
3391 ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
3392 memcpy(ioarcb
->cdb
, scsi_cmd
->cmnd
, scsi_cmd
->cmd_len
);
3393 ioarcb
->resource_handle
= res
->cfg_entry
.resource_handle
;
3394 ioarcb
->request_type
= REQ_TYPE_SCSI
;
3396 /* set hrrq number where the IOA should respond to. Note that all cmds
3397 * generated internally uses hrrq_id 0, exception to this is the cmd
3398 * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
3399 * hrrq_id assigned here in queuecommand
3401 ioarcb
->hrrq_id
= atomic_add_return(1, &(pinstance
->last_message_id
)) %
3402 pinstance
->num_hrrq
;
3403 cmd
->cmd_done
= pmcraid_io_done
;
3405 if (RES_IS_GSCSI(res
->cfg_entry
) || RES_IS_VSET(res
->cfg_entry
)) {
3406 if (scsi_cmd
->underflow
== 0)
3407 ioarcb
->request_flags0
|= INHIBIT_UL_CHECK
;
3409 if (res
->sync_reqd
) {
3410 ioarcb
->request_flags0
|= SYNC_COMPLETE
;
3414 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
3416 if (scsi_cmd
->flags
& SCMD_TAGGED
)
3417 ioarcb
->request_flags1
|= TASK_TAG_SIMPLE
;
3419 if (RES_IS_GSCSI(res
->cfg_entry
))
3420 ioarcb
->request_flags1
|= DELAY_AFTER_RESET
;
3423 rc
= pmcraid_build_ioadl(pinstance
, cmd
);
3425 pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
3426 le32_to_cpu(ioarcb
->response_handle
) >> 2,
3427 scsi_cmd
->cmnd
[0], pinstance
->host
->unique_id
,
3428 RES_IS_VSET(res
->cfg_entry
) ? PMCRAID_VSET_BUS_ID
:
3429 PMCRAID_PHYS_BUS_ID
,
3430 RES_IS_VSET(res
->cfg_entry
) ?
3431 (fw_version
<= PMCRAID_FW_VERSION_1
?
3432 res
->cfg_entry
.unique_flags1
:
3433 le16_to_cpu(res
->cfg_entry
.array_id
) & 0xFF) :
3434 RES_TARGET(res
->cfg_entry
.resource_address
),
3435 RES_LUN(res
->cfg_entry
.resource_address
));
3437 if (likely(rc
== 0)) {
3438 _pmcraid_fire_command(cmd
);
3440 pmcraid_err("queuecommand could not build ioadl\n");
3441 pmcraid_return_cmd(cmd
);
3442 rc
= SCSI_MLQUEUE_HOST_BUSY
;
3448 static DEF_SCSI_QCMD(pmcraid_queuecommand
)
3451 * pmcraid_open -char node "open" entry, allowed only users with admin access
3453 static int pmcraid_chr_open(struct inode
*inode
, struct file
*filep
)
3455 struct pmcraid_instance
*pinstance
;
3457 if (!capable(CAP_SYS_ADMIN
))
3460 /* Populate adapter instance * pointer for use by ioctl */
3461 pinstance
= container_of(inode
->i_cdev
, struct pmcraid_instance
, cdev
);
3462 filep
->private_data
= pinstance
;
3468 * pmcraid_fasync - Async notifier registration from applications
3470 * This function adds the calling process to a driver global queue. When an
3471 * event occurs, SIGIO will be sent to all processes in this queue.
3473 static int pmcraid_chr_fasync(int fd
, struct file
*filep
, int mode
)
3475 struct pmcraid_instance
*pinstance
;
3478 pinstance
= filep
->private_data
;
3479 mutex_lock(&pinstance
->aen_queue_lock
);
3480 rc
= fasync_helper(fd
, filep
, mode
, &pinstance
->aen_queue
);
3481 mutex_unlock(&pinstance
->aen_queue_lock
);
3488 * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
3489 * commands sent over IOCTL interface
3491 * @cmd : pointer to struct pmcraid_cmd
3492 * @buflen : length of the request buffer
3493 * @direction : data transfer direction
3496 * 0 on success, non-zero error code on failure
3498 static int pmcraid_build_passthrough_ioadls(
3499 struct pmcraid_cmd
*cmd
,
3504 struct pmcraid_sglist
*sglist
= NULL
;
3505 struct scatterlist
*sg
= NULL
;
3506 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
3507 struct pmcraid_ioadl_desc
*ioadl
;
3510 sglist
= pmcraid_alloc_sglist(buflen
);
3513 pmcraid_err("can't allocate memory for passthrough SGls\n");
3517 sglist
->num_dma_sg
= dma_map_sg(&cmd
->drv_inst
->pdev
->dev
,
3518 sglist
->scatterlist
,
3519 sglist
->num_sg
, direction
);
3521 if (!sglist
->num_dma_sg
|| sglist
->num_dma_sg
> PMCRAID_MAX_IOADLS
) {
3522 dev_err(&cmd
->drv_inst
->pdev
->dev
,
3523 "Failed to map passthrough buffer!\n");
3524 pmcraid_free_sglist(sglist
);
3528 cmd
->sglist
= sglist
;
3529 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
3531 ioadl
= pmcraid_init_ioadls(cmd
, sglist
->num_dma_sg
);
3533 /* Initialize IOADL descriptor addresses */
3534 for_each_sg(sglist
->scatterlist
, sg
, sglist
->num_dma_sg
, i
) {
3535 ioadl
[i
].data_len
= cpu_to_le32(sg_dma_len(sg
));
3536 ioadl
[i
].address
= cpu_to_le64(sg_dma_address(sg
));
3540 /* setup the last descriptor */
3541 ioadl
[i
- 1].flags
= IOADL_FLAGS_LAST_DESC
;
3548 * pmcraid_release_passthrough_ioadls - release passthrough ioadls
3550 * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
3551 * @buflen: size of the request buffer
3552 * @direction: data transfer direction
3555 * 0 on success, non-zero error code on failure
3557 static void pmcraid_release_passthrough_ioadls(
3558 struct pmcraid_cmd
*cmd
,
3563 struct pmcraid_sglist
*sglist
= cmd
->sglist
;
3566 dma_unmap_sg(&cmd
->drv_inst
->pdev
->dev
,
3567 sglist
->scatterlist
,
3570 pmcraid_free_sglist(sglist
);
3576 * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
3578 * @pinstance: pointer to adapter instance structure
3580 * @arg: pointer to pmcraid_passthrough_buffer user buffer
3583 * 0 on success, non-zero error code on failure
3585 static long pmcraid_ioctl_passthrough(
3586 struct pmcraid_instance
*pinstance
,
3587 unsigned int ioctl_cmd
,
3588 unsigned int buflen
,
3592 struct pmcraid_passthrough_ioctl_buffer
*buffer
;
3593 struct pmcraid_ioarcb
*ioarcb
;
3594 struct pmcraid_cmd
*cmd
;
3595 struct pmcraid_cmd
*cancel_cmd
;
3596 void __user
*request_buffer
;
3597 unsigned long request_offset
;
3598 unsigned long lock_flags
;
3606 /* If IOA reset is in progress, wait 10 secs for reset to complete */
3607 if (pinstance
->ioa_reset_in_progress
) {
3608 rc
= wait_event_interruptible_timeout(
3609 pinstance
->reset_wait_q
,
3610 !pinstance
->ioa_reset_in_progress
,
3611 msecs_to_jiffies(10000));
3616 return -ERESTARTSYS
;
3619 /* If adapter is not in operational state, return error */
3620 if (pinstance
->ioa_state
!= IOA_STATE_OPERATIONAL
) {
3621 pmcraid_err("IOA is not operational\n");
3625 buffer_size
= sizeof(struct pmcraid_passthrough_ioctl_buffer
);
3626 buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
3629 pmcraid_err("no memory for passthrough buffer\n");
3634 offsetof(struct pmcraid_passthrough_ioctl_buffer
, request_buffer
);
3636 request_buffer
= arg
+ request_offset
;
3638 rc
= copy_from_user(buffer
, arg
,
3639 sizeof(struct pmcraid_passthrough_ioctl_buffer
));
3641 ioasa
= arg
+ offsetof(struct pmcraid_passthrough_ioctl_buffer
, ioasa
);
3644 pmcraid_err("ioctl: can't copy passthrough buffer\n");
3646 goto out_free_buffer
;
3649 request_size
= le32_to_cpu(buffer
->ioarcb
.data_transfer_length
);
3651 if (buffer
->ioarcb
.request_flags0
& TRANSFER_DIR_WRITE
) {
3652 direction
= DMA_TO_DEVICE
;
3654 direction
= DMA_FROM_DEVICE
;
3657 if (request_size
< 0) {
3659 goto out_free_buffer
;
3662 /* check if we have any additional command parameters */
3663 if (le16_to_cpu(buffer
->ioarcb
.add_cmd_param_length
)
3664 > PMCRAID_ADD_CMD_PARAM_LEN
) {
3666 goto out_free_buffer
;
3669 cmd
= pmcraid_get_free_cmd(pinstance
);
3672 pmcraid_err("free command block is not available\n");
3674 goto out_free_buffer
;
3677 cmd
->scsi_cmd
= NULL
;
3678 ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
3680 /* Copy the user-provided IOARCB stuff field by field */
3681 ioarcb
->resource_handle
= buffer
->ioarcb
.resource_handle
;
3682 ioarcb
->data_transfer_length
= buffer
->ioarcb
.data_transfer_length
;
3683 ioarcb
->cmd_timeout
= buffer
->ioarcb
.cmd_timeout
;
3684 ioarcb
->request_type
= buffer
->ioarcb
.request_type
;
3685 ioarcb
->request_flags0
= buffer
->ioarcb
.request_flags0
;
3686 ioarcb
->request_flags1
= buffer
->ioarcb
.request_flags1
;
3687 memcpy(ioarcb
->cdb
, buffer
->ioarcb
.cdb
, PMCRAID_MAX_CDB_LEN
);
3689 if (buffer
->ioarcb
.add_cmd_param_length
) {
3690 ioarcb
->add_cmd_param_length
=
3691 buffer
->ioarcb
.add_cmd_param_length
;
3692 ioarcb
->add_cmd_param_offset
=
3693 buffer
->ioarcb
.add_cmd_param_offset
;
3694 memcpy(ioarcb
->add_data
.u
.add_cmd_params
,
3695 buffer
->ioarcb
.add_data
.u
.add_cmd_params
,
3696 le16_to_cpu(buffer
->ioarcb
.add_cmd_param_length
));
3699 /* set hrrq number where the IOA should respond to. Note that all cmds
3700 * generated internally uses hrrq_id 0, exception to this is the cmd
3701 * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
3702 * hrrq_id assigned here in queuecommand
3704 ioarcb
->hrrq_id
= atomic_add_return(1, &(pinstance
->last_message_id
)) %
3705 pinstance
->num_hrrq
;
3708 rc
= pmcraid_build_passthrough_ioadls(cmd
,
3712 pmcraid_err("couldn't build passthrough ioadls\n");
3717 /* If data is being written into the device, copy the data from user
3720 if (direction
== DMA_TO_DEVICE
&& request_size
> 0) {
3721 rc
= pmcraid_copy_sglist(cmd
->sglist
,
3726 pmcraid_err("failed to copy user buffer\n");
3727 goto out_free_sglist
;
3731 /* passthrough ioctl is a blocking command so, put the user to sleep
3732 * until timeout. Note that a timeout value of 0 means, do timeout.
3734 cmd
->cmd_done
= pmcraid_internal_done
;
3735 init_completion(&cmd
->wait_for_completion
);
3736 cmd
->completion_req
= 1;
3738 pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
3739 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2,
3740 cmd
->ioa_cb
->ioarcb
.cdb
[0],
3741 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.resource_handle
));
3743 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
3744 _pmcraid_fire_command(cmd
);
3745 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
3747 /* NOTE ! Remove the below line once abort_task is implemented
3748 * in firmware. This line disables ioctl command timeout handling logic
3749 * similar to IO command timeout handling, making ioctl commands to wait
3750 * until the command completion regardless of timeout value specified in
3753 buffer
->ioarcb
.cmd_timeout
= 0;
3755 /* If command timeout is specified put caller to wait till that time,
3756 * otherwise it would be blocking wait. If command gets timed out, it
3759 if (buffer
->ioarcb
.cmd_timeout
== 0) {
3760 wait_for_completion(&cmd
->wait_for_completion
);
3761 } else if (!wait_for_completion_timeout(
3762 &cmd
->wait_for_completion
,
3763 msecs_to_jiffies(le16_to_cpu(buffer
->ioarcb
.cmd_timeout
) * 1000))) {
3765 pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
3766 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2,
3767 cmd
->ioa_cb
->ioarcb
.cdb
[0]);
3769 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
3770 cancel_cmd
= pmcraid_abort_cmd(cmd
);
3771 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
3774 wait_for_completion(&cancel_cmd
->wait_for_completion
);
3775 ioasc
= le32_to_cpu(cancel_cmd
->ioa_cb
->ioasa
.ioasc
);
3776 pmcraid_return_cmd(cancel_cmd
);
3778 /* if abort task couldn't find the command i.e it got
3779 * completed prior to aborting, return good completion.
3780 * if command got aborted successfully or there was IOA
3781 * reset due to abort task itself getting timedout then
3784 if (ioasc
== PMCRAID_IOASC_IOA_WAS_RESET
||
3785 PMCRAID_IOASC_SENSE_KEY(ioasc
) == 0x00) {
3786 if (ioasc
!= PMCRAID_IOASC_GC_IOARCB_NOTFOUND
)
3788 goto out_handle_response
;
3792 /* no command block for abort task or abort task failed to abort
3793 * the IOARCB, then wait for 150 more seconds and initiate reset
3794 * sequence after timeout
3796 if (!wait_for_completion_timeout(
3797 &cmd
->wait_for_completion
,
3798 msecs_to_jiffies(150 * 1000))) {
3799 pmcraid_reset_bringup(cmd
->drv_inst
);
3804 out_handle_response
:
3805 /* copy entire IOASA buffer and return IOCTL success.
3806 * If copying IOASA to user-buffer fails, return
3809 if (copy_to_user(ioasa
, &cmd
->ioa_cb
->ioasa
,
3810 sizeof(struct pmcraid_ioasa
))) {
3811 pmcraid_err("failed to copy ioasa buffer to user\n");
3815 /* If the data transfer was from device, copy the data onto user
3818 else if (direction
== DMA_FROM_DEVICE
&& request_size
> 0) {
3819 rc
= pmcraid_copy_sglist(cmd
->sglist
,
3824 pmcraid_err("failed to copy user buffer\n");
3830 pmcraid_release_passthrough_ioadls(cmd
, request_size
, direction
);
3833 pmcraid_return_cmd(cmd
);
3845 * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
3847 * @pinstance: pointer to adapter instance structure
3848 * @cmd: ioctl command passed in
3849 * @buflen: length of user_buffer
3850 * @user_buffer: user buffer pointer
3853 * 0 in case of success, otherwise appropriate error code
3855 static long pmcraid_ioctl_driver(
3856 struct pmcraid_instance
*pinstance
,
3858 unsigned int buflen
,
3859 void __user
*user_buffer
3865 case PMCRAID_IOCTL_RESET_ADAPTER
:
3866 pmcraid_reset_bringup(pinstance
);
3878 * pmcraid_check_ioctl_buffer - check for proper access to user buffer
3880 * @cmd: ioctl command
3882 * @hdr: pointer to kernel memory for pmcraid_ioctl_header
3885 * negetive error code if there are access issues, otherwise zero.
3886 * Upon success, returns ioctl header copied out of user buffer.
3889 static int pmcraid_check_ioctl_buffer(
3892 struct pmcraid_ioctl_header
*hdr
3897 if (copy_from_user(hdr
, arg
, sizeof(struct pmcraid_ioctl_header
))) {
3898 pmcraid_err("couldn't copy ioctl header from user buffer\n");
3902 /* check for valid driver signature */
3903 rc
= memcmp(hdr
->signature
,
3904 PMCRAID_IOCTL_SIGNATURE
,
3905 sizeof(hdr
->signature
));
3907 pmcraid_err("signature verification failed\n");
3915 * pmcraid_ioctl - char node ioctl entry point
3917 static long pmcraid_chr_ioctl(
3923 struct pmcraid_instance
*pinstance
= NULL
;
3924 struct pmcraid_ioctl_header
*hdr
= NULL
;
3925 void __user
*argp
= (void __user
*)arg
;
3926 int retval
= -ENOTTY
;
3928 hdr
= kmalloc(sizeof(struct pmcraid_ioctl_header
), GFP_KERNEL
);
3931 pmcraid_err("failed to allocate memory for ioctl header\n");
3935 retval
= pmcraid_check_ioctl_buffer(cmd
, argp
, hdr
);
3938 pmcraid_info("chr_ioctl: header check failed\n");
3943 pinstance
= filep
->private_data
;
3946 pmcraid_info("adapter instance is not found\n");
3951 switch (_IOC_TYPE(cmd
)) {
3953 case PMCRAID_PASSTHROUGH_IOCTL
:
3954 /* If ioctl code is to download microcode, we need to block
3955 * mid-layer requests.
3957 if (cmd
== PMCRAID_IOCTL_DOWNLOAD_MICROCODE
)
3958 scsi_block_requests(pinstance
->host
);
3960 retval
= pmcraid_ioctl_passthrough(pinstance
, cmd
,
3961 hdr
->buffer_length
, argp
);
3963 if (cmd
== PMCRAID_IOCTL_DOWNLOAD_MICROCODE
)
3964 scsi_unblock_requests(pinstance
->host
);
3967 case PMCRAID_DRIVER_IOCTL
:
3968 arg
+= sizeof(struct pmcraid_ioctl_header
);
3969 retval
= pmcraid_ioctl_driver(pinstance
, cmd
,
3970 hdr
->buffer_length
, argp
);
3984 * File operations structure for management interface
3986 static const struct file_operations pmcraid_fops
= {
3987 .owner
= THIS_MODULE
,
3988 .open
= pmcraid_chr_open
,
3989 .fasync
= pmcraid_chr_fasync
,
3990 .unlocked_ioctl
= pmcraid_chr_ioctl
,
3991 #ifdef CONFIG_COMPAT
3992 .compat_ioctl
= pmcraid_chr_ioctl
,
3994 .llseek
= noop_llseek
,
4001 * pmcraid_show_log_level - Display adapter's error logging level
4002 * @dev: class device struct
4006 * number of bytes printed to buffer
4008 static ssize_t
pmcraid_show_log_level(
4010 struct device_attribute
*attr
,
4013 struct Scsi_Host
*shost
= class_to_shost(dev
);
4014 struct pmcraid_instance
*pinstance
=
4015 (struct pmcraid_instance
*)shost
->hostdata
;
4016 return snprintf(buf
, PAGE_SIZE
, "%d\n", pinstance
->current_log_level
);
4020 * pmcraid_store_log_level - Change the adapter's error logging level
4021 * @dev: class device struct
4026 * number of bytes printed to buffer
4028 static ssize_t
pmcraid_store_log_level(
4030 struct device_attribute
*attr
,
4035 struct Scsi_Host
*shost
;
4036 struct pmcraid_instance
*pinstance
;
4039 if (kstrtou8(buf
, 10, &val
))
4041 /* log-level should be from 0 to 2 */
4045 shost
= class_to_shost(dev
);
4046 pinstance
= (struct pmcraid_instance
*)shost
->hostdata
;
4047 pinstance
->current_log_level
= val
;
4052 static struct device_attribute pmcraid_log_level_attr
= {
4054 .name
= "log_level",
4055 .mode
= S_IRUGO
| S_IWUSR
,
4057 .show
= pmcraid_show_log_level
,
4058 .store
= pmcraid_store_log_level
,
4062 * pmcraid_show_drv_version - Display driver version
4063 * @dev: class device struct
4067 * number of bytes printed to buffer
4069 static ssize_t
pmcraid_show_drv_version(
4071 struct device_attribute
*attr
,
4075 return snprintf(buf
, PAGE_SIZE
, "version: %s\n",
4076 PMCRAID_DRIVER_VERSION
);
4079 static struct device_attribute pmcraid_driver_version_attr
= {
4081 .name
= "drv_version",
4084 .show
= pmcraid_show_drv_version
,
4088 * pmcraid_show_io_adapter_id - Display driver assigned adapter id
4089 * @dev: class device struct
4093 * number of bytes printed to buffer
4095 static ssize_t
pmcraid_show_adapter_id(
4097 struct device_attribute
*attr
,
4101 struct Scsi_Host
*shost
= class_to_shost(dev
);
4102 struct pmcraid_instance
*pinstance
=
4103 (struct pmcraid_instance
*)shost
->hostdata
;
4104 u32 adapter_id
= (pinstance
->pdev
->bus
->number
<< 8) |
4105 pinstance
->pdev
->devfn
;
4106 u32 aen_group
= pmcraid_event_family
.id
;
4108 return snprintf(buf
, PAGE_SIZE
,
4109 "adapter id: %d\nminor: %d\naen group: %d\n",
4110 adapter_id
, MINOR(pinstance
->cdev
.dev
), aen_group
);
4113 static struct device_attribute pmcraid_adapter_id_attr
= {
4115 .name
= "adapter_id",
4118 .show
= pmcraid_show_adapter_id
,
4121 static struct device_attribute
*pmcraid_host_attrs
[] = {
4122 &pmcraid_log_level_attr
,
4123 &pmcraid_driver_version_attr
,
4124 &pmcraid_adapter_id_attr
,
4129 /* host template structure for pmcraid driver */
4130 static struct scsi_host_template pmcraid_host_template
= {
4131 .module
= THIS_MODULE
,
4132 .name
= PMCRAID_DRIVER_NAME
,
4133 .queuecommand
= pmcraid_queuecommand
,
4134 .eh_abort_handler
= pmcraid_eh_abort_handler
,
4135 .eh_bus_reset_handler
= pmcraid_eh_bus_reset_handler
,
4136 .eh_target_reset_handler
= pmcraid_eh_target_reset_handler
,
4137 .eh_device_reset_handler
= pmcraid_eh_device_reset_handler
,
4138 .eh_host_reset_handler
= pmcraid_eh_host_reset_handler
,
4140 .slave_alloc
= pmcraid_slave_alloc
,
4141 .slave_configure
= pmcraid_slave_configure
,
4142 .slave_destroy
= pmcraid_slave_destroy
,
4143 .change_queue_depth
= pmcraid_change_queue_depth
,
4144 .can_queue
= PMCRAID_MAX_IO_CMD
,
4146 .sg_tablesize
= PMCRAID_MAX_IOADLS
,
4147 .max_sectors
= PMCRAID_IOA_MAX_SECTORS
,
4149 .cmd_per_lun
= PMCRAID_MAX_CMD_PER_LUN
,
4150 .shost_attrs
= pmcraid_host_attrs
,
4151 .proc_name
= PMCRAID_DRIVER_NAME
,
4155 * pmcraid_isr_msix - implements MSI-X interrupt handling routine
4156 * @irq: interrupt vector number
4157 * @dev_id: pointer hrrq_vector
4160 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4163 static irqreturn_t
pmcraid_isr_msix(int irq
, void *dev_id
)
4165 struct pmcraid_isr_param
*hrrq_vector
;
4166 struct pmcraid_instance
*pinstance
;
4167 unsigned long lock_flags
;
4171 hrrq_vector
= (struct pmcraid_isr_param
*)dev_id
;
4172 hrrq_id
= hrrq_vector
->hrrq_id
;
4173 pinstance
= hrrq_vector
->drv_inst
;
4176 /* Read the interrupt */
4177 intrs_val
= pmcraid_read_interrupts(pinstance
);
4179 ((ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
)
4180 & DOORBELL_INTR_MSIX_CLR
) == 0)) {
4181 /* Any error interrupts including unit_check,
4182 * initiate IOA reset.In case of unit check indicate
4183 * to reset_sequence that IOA unit checked and prepare
4184 * for a dump during reset sequence
4186 if (intrs_val
& PMCRAID_ERROR_INTERRUPTS
) {
4187 if (intrs_val
& INTRS_IOA_UNIT_CHECK
)
4188 pinstance
->ioa_unit_check
= 1;
4190 pmcraid_err("ISR: error interrupts: %x \
4191 initiating reset\n", intrs_val
);
4192 spin_lock_irqsave(pinstance
->host
->host_lock
,
4194 pmcraid_initiate_reset(pinstance
);
4195 spin_unlock_irqrestore(
4196 pinstance
->host
->host_lock
,
4199 /* If interrupt was as part of the ioa initialization,
4200 * clear it. Delete the timer and wakeup the
4201 * reset engine to proceed with reset sequence
4203 if (intrs_val
& INTRS_TRANSITION_TO_OPERATIONAL
)
4204 pmcraid_clr_trans_op(pinstance
);
4206 /* Clear the interrupt register by writing
4207 * to host to ioa doorbell. Once done
4208 * FW will clear the interrupt.
4210 iowrite32(DOORBELL_INTR_MSIX_CLR
,
4211 pinstance
->int_regs
.host_ioa_interrupt_reg
);
4212 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
4218 tasklet_schedule(&(pinstance
->isr_tasklet
[hrrq_id
]));
4224 * pmcraid_isr - implements legacy interrupt handling routine
4226 * @irq: interrupt vector number
4227 * @dev_id: pointer hrrq_vector
4230 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4232 static irqreturn_t
pmcraid_isr(int irq
, void *dev_id
)
4234 struct pmcraid_isr_param
*hrrq_vector
;
4235 struct pmcraid_instance
*pinstance
;
4237 unsigned long lock_flags
;
4240 /* In case of legacy interrupt mode where interrupts are shared across
4241 * isrs, it may be possible that the current interrupt is not from IOA
4244 printk(KERN_INFO
"%s(): NULL host pointer\n", __func__
);
4247 hrrq_vector
= (struct pmcraid_isr_param
*)dev_id
;
4248 pinstance
= hrrq_vector
->drv_inst
;
4250 intrs
= pmcraid_read_interrupts(pinstance
);
4252 if (unlikely((intrs
& PMCRAID_PCI_INTERRUPTS
) == 0))
4255 /* Any error interrupts including unit_check, initiate IOA reset.
4256 * In case of unit check indicate to reset_sequence that IOA unit
4257 * checked and prepare for a dump during reset sequence
4259 if (intrs
& PMCRAID_ERROR_INTERRUPTS
) {
4261 if (intrs
& INTRS_IOA_UNIT_CHECK
)
4262 pinstance
->ioa_unit_check
= 1;
4265 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4266 pmcraid_err("ISR: error interrupts: %x initiating reset\n",
4269 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4270 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
4271 pmcraid_initiate_reset(pinstance
);
4272 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
4274 /* If interrupt was as part of the ioa initialization,
4275 * clear. Delete the timer and wakeup the
4276 * reset engine to proceed with reset sequence
4278 if (intrs
& INTRS_TRANSITION_TO_OPERATIONAL
) {
4279 pmcraid_clr_trans_op(pinstance
);
4282 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4284 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4287 &(pinstance
->isr_tasklet
[hrrq_id
]));
4296 * pmcraid_worker_function - worker thread function
4298 * @workp: pointer to struct work queue
4304 static void pmcraid_worker_function(struct work_struct
*workp
)
4306 struct pmcraid_instance
*pinstance
;
4307 struct pmcraid_resource_entry
*res
;
4308 struct pmcraid_resource_entry
*temp
;
4309 struct scsi_device
*sdev
;
4310 unsigned long lock_flags
;
4311 unsigned long host_lock_flags
;
4313 u8 bus
, target
, lun
;
4315 pinstance
= container_of(workp
, struct pmcraid_instance
, worker_q
);
4316 /* add resources only after host is added into system */
4317 if (!atomic_read(&pinstance
->expose_resources
))
4320 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
4322 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
4323 list_for_each_entry_safe(res
, temp
, &pinstance
->used_res_q
, queue
) {
4325 if (res
->change_detected
== RES_CHANGE_DEL
&& res
->scsi_dev
) {
4326 sdev
= res
->scsi_dev
;
4328 /* host_lock must be held before calling
4331 spin_lock_irqsave(pinstance
->host
->host_lock
,
4333 if (!scsi_device_get(sdev
)) {
4334 spin_unlock_irqrestore(
4335 pinstance
->host
->host_lock
,
4337 pmcraid_info("deleting %x from midlayer\n",
4338 res
->cfg_entry
.resource_address
);
4339 list_move_tail(&res
->queue
,
4340 &pinstance
->free_res_q
);
4341 spin_unlock_irqrestore(
4342 &pinstance
->resource_lock
,
4344 scsi_remove_device(sdev
);
4345 scsi_device_put(sdev
);
4346 spin_lock_irqsave(&pinstance
->resource_lock
,
4348 res
->change_detected
= 0;
4350 spin_unlock_irqrestore(
4351 pinstance
->host
->host_lock
,
4357 list_for_each_entry(res
, &pinstance
->used_res_q
, queue
) {
4359 if (res
->change_detected
== RES_CHANGE_ADD
) {
4361 if (!pmcraid_expose_resource(fw_version
,
4365 if (RES_IS_VSET(res
->cfg_entry
)) {
4366 bus
= PMCRAID_VSET_BUS_ID
;
4367 if (fw_version
<= PMCRAID_FW_VERSION_1
)
4368 target
= res
->cfg_entry
.unique_flags1
;
4370 target
= le16_to_cpu(res
->cfg_entry
.array_id
) & 0xFF;
4371 lun
= PMCRAID_VSET_LUN_ID
;
4373 bus
= PMCRAID_PHYS_BUS_ID
;
4376 res
->cfg_entry
.resource_address
);
4377 lun
= RES_LUN(res
->cfg_entry
.resource_address
);
4380 res
->change_detected
= 0;
4381 spin_unlock_irqrestore(&pinstance
->resource_lock
,
4383 scsi_add_device(pinstance
->host
, bus
, target
, lun
);
4384 spin_lock_irqsave(&pinstance
->resource_lock
,
4389 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
4393 * pmcraid_tasklet_function - Tasklet function
4395 * @instance: pointer to msix param structure
4400 static void pmcraid_tasklet_function(unsigned long instance
)
4402 struct pmcraid_isr_param
*hrrq_vector
;
4403 struct pmcraid_instance
*pinstance
;
4404 unsigned long hrrq_lock_flags
;
4405 unsigned long pending_lock_flags
;
4406 unsigned long host_lock_flags
;
4407 spinlock_t
*lockp
; /* hrrq buffer lock */
4411 hrrq_vector
= (struct pmcraid_isr_param
*)instance
;
4412 pinstance
= hrrq_vector
->drv_inst
;
4413 id
= hrrq_vector
->hrrq_id
;
4414 lockp
= &(pinstance
->hrrq_lock
[id
]);
4416 /* loop through each of the commands responded by IOA. Each HRRQ buf is
4417 * protected by its own lock. Traversals must be done within this lock
4418 * as there may be multiple tasklets running on multiple CPUs. Note
4419 * that the lock is held just for picking up the response handle and
4420 * manipulating hrrq_curr/toggle_bit values.
4422 spin_lock_irqsave(lockp
, hrrq_lock_flags
);
4424 resp
= le32_to_cpu(*(pinstance
->hrrq_curr
[id
]));
4426 while ((resp
& HRRQ_TOGGLE_BIT
) ==
4427 pinstance
->host_toggle_bit
[id
]) {
4429 int cmd_index
= resp
>> 2;
4430 struct pmcraid_cmd
*cmd
= NULL
;
4432 if (pinstance
->hrrq_curr
[id
] < pinstance
->hrrq_end
[id
]) {
4433 pinstance
->hrrq_curr
[id
]++;
4435 pinstance
->hrrq_curr
[id
] = pinstance
->hrrq_start
[id
];
4436 pinstance
->host_toggle_bit
[id
] ^= 1u;
4439 if (cmd_index
>= PMCRAID_MAX_CMD
) {
4440 /* In case of invalid response handle, log message */
4441 pmcraid_err("Invalid response handle %d\n", cmd_index
);
4442 resp
= le32_to_cpu(*(pinstance
->hrrq_curr
[id
]));
4446 cmd
= pinstance
->cmd_list
[cmd_index
];
4447 spin_unlock_irqrestore(lockp
, hrrq_lock_flags
);
4449 spin_lock_irqsave(&pinstance
->pending_pool_lock
,
4450 pending_lock_flags
);
4451 list_del(&cmd
->free_list
);
4452 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
,
4453 pending_lock_flags
);
4454 del_timer(&cmd
->timer
);
4455 atomic_dec(&pinstance
->outstanding_cmds
);
4457 if (cmd
->cmd_done
== pmcraid_ioa_reset
) {
4458 spin_lock_irqsave(pinstance
->host
->host_lock
,
4461 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
4463 } else if (cmd
->cmd_done
!= NULL
) {
4466 /* loop over until we are done with all responses */
4467 spin_lock_irqsave(lockp
, hrrq_lock_flags
);
4468 resp
= le32_to_cpu(*(pinstance
->hrrq_curr
[id
]));
4471 spin_unlock_irqrestore(lockp
, hrrq_lock_flags
);
4475 * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
4476 * @pinstance: pointer to adapter instance structure
4478 * This routine un-registers registered interrupt handler and
4479 * also frees irqs/vectors.
4485 void pmcraid_unregister_interrupt_handler(struct pmcraid_instance
*pinstance
)
4487 struct pci_dev
*pdev
= pinstance
->pdev
;
4490 for (i
= 0; i
< pinstance
->num_hrrq
; i
++)
4491 free_irq(pci_irq_vector(pdev
, i
), &pinstance
->hrrq_vector
[i
]);
4493 pinstance
->interrupt_mode
= 0;
4494 pci_free_irq_vectors(pdev
);
4498 * pmcraid_register_interrupt_handler - registers interrupt handler
4499 * @pinstance: pointer to per-adapter instance structure
4502 * 0 on success, non-zero error code otherwise.
4505 pmcraid_register_interrupt_handler(struct pmcraid_instance
*pinstance
)
4507 struct pci_dev
*pdev
= pinstance
->pdev
;
4508 unsigned int irq_flag
= PCI_IRQ_LEGACY
, flag
;
4509 int num_hrrq
, rc
, i
;
4512 if (pmcraid_enable_msix
)
4513 irq_flag
|= PCI_IRQ_MSIX
;
4515 num_hrrq
= pci_alloc_irq_vectors(pdev
, 1, PMCRAID_NUM_MSIX_VECTORS
,
4520 if (pdev
->msix_enabled
) {
4522 isr
= pmcraid_isr_msix
;
4528 for (i
= 0; i
< num_hrrq
; i
++) {
4529 struct pmcraid_isr_param
*vec
= &pinstance
->hrrq_vector
[i
];
4532 vec
->drv_inst
= pinstance
;
4533 rc
= request_irq(pci_irq_vector(pdev
, i
), isr
, flag
,
4534 PMCRAID_DRIVER_NAME
, vec
);
4539 pinstance
->num_hrrq
= num_hrrq
;
4540 if (pdev
->msix_enabled
) {
4541 pinstance
->interrupt_mode
= 1;
4542 iowrite32(DOORBELL_INTR_MODE_MSIX
,
4543 pinstance
->int_regs
.host_ioa_interrupt_reg
);
4544 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
4551 free_irq(pci_irq_vector(pdev
, i
), &pinstance
->hrrq_vector
[i
]);
4552 pci_free_irq_vectors(pdev
);
4557 * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
4558 * @pinstance: per adapter instance structure pointer
4559 * @max_index: number of buffer blocks to release
4565 pmcraid_release_cmd_blocks(struct pmcraid_instance
*pinstance
, int max_index
)
4568 for (i
= 0; i
< max_index
; i
++) {
4569 kmem_cache_free(pinstance
->cmd_cachep
, pinstance
->cmd_list
[i
]);
4570 pinstance
->cmd_list
[i
] = NULL
;
4572 kmem_cache_destroy(pinstance
->cmd_cachep
);
4573 pinstance
->cmd_cachep
= NULL
;
4577 * pmcraid_release_control_blocks - releases buffers alloced for control blocks
4578 * @pinstance: pointer to per adapter instance structure
4579 * @max_index: number of buffers (from 0 onwards) to release
4581 * This function assumes that the command blocks for which control blocks are
4582 * linked are not released.
4588 pmcraid_release_control_blocks(
4589 struct pmcraid_instance
*pinstance
,
4595 if (pinstance
->control_pool
== NULL
)
4598 for (i
= 0; i
< max_index
; i
++) {
4599 dma_pool_free(pinstance
->control_pool
,
4600 pinstance
->cmd_list
[i
]->ioa_cb
,
4601 pinstance
->cmd_list
[i
]->ioa_cb_bus_addr
);
4602 pinstance
->cmd_list
[i
]->ioa_cb
= NULL
;
4603 pinstance
->cmd_list
[i
]->ioa_cb_bus_addr
= 0;
4605 dma_pool_destroy(pinstance
->control_pool
);
4606 pinstance
->control_pool
= NULL
;
4610 * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
4611 * @pinstance - pointer to per adapter instance structure
4613 * Allocates memory for command blocks using kernel slab allocator.
4616 * 0 in case of success; -ENOMEM in case of failure
4618 static int pmcraid_allocate_cmd_blocks(struct pmcraid_instance
*pinstance
)
4622 sprintf(pinstance
->cmd_pool_name
, "pmcraid_cmd_pool_%d",
4623 pinstance
->host
->unique_id
);
4626 pinstance
->cmd_cachep
= kmem_cache_create(
4627 pinstance
->cmd_pool_name
,
4628 sizeof(struct pmcraid_cmd
), 0,
4629 SLAB_HWCACHE_ALIGN
, NULL
);
4630 if (!pinstance
->cmd_cachep
)
4633 for (i
= 0; i
< PMCRAID_MAX_CMD
; i
++) {
4634 pinstance
->cmd_list
[i
] =
4635 kmem_cache_alloc(pinstance
->cmd_cachep
, GFP_KERNEL
);
4636 if (!pinstance
->cmd_list
[i
]) {
4637 pmcraid_release_cmd_blocks(pinstance
, i
);
4645 * pmcraid_allocate_control_blocks - allocates memory control blocks
4646 * @pinstance : pointer to per adapter instance structure
4648 * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
4649 * and IOASAs. This is called after command blocks are already allocated.
4652 * 0 in case it can allocate all control blocks, otherwise -ENOMEM
4654 static int pmcraid_allocate_control_blocks(struct pmcraid_instance
*pinstance
)
4658 sprintf(pinstance
->ctl_pool_name
, "pmcraid_control_pool_%d",
4659 pinstance
->host
->unique_id
);
4661 pinstance
->control_pool
=
4662 dma_pool_create(pinstance
->ctl_pool_name
,
4663 &pinstance
->pdev
->dev
,
4664 sizeof(struct pmcraid_control_block
),
4665 PMCRAID_IOARCB_ALIGNMENT
, 0);
4667 if (!pinstance
->control_pool
)
4670 for (i
= 0; i
< PMCRAID_MAX_CMD
; i
++) {
4671 pinstance
->cmd_list
[i
]->ioa_cb
=
4673 pinstance
->control_pool
,
4675 &(pinstance
->cmd_list
[i
]->ioa_cb_bus_addr
));
4677 if (!pinstance
->cmd_list
[i
]->ioa_cb
) {
4678 pmcraid_release_control_blocks(pinstance
, i
);
4681 memset(pinstance
->cmd_list
[i
]->ioa_cb
, 0,
4682 sizeof(struct pmcraid_control_block
));
4688 * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
4689 * @pinstance: pointer to per adapter instance structure
4690 * @maxindex: size of hrrq buffer pointer array
4696 pmcraid_release_host_rrqs(struct pmcraid_instance
*pinstance
, int maxindex
)
4700 for (i
= 0; i
< maxindex
; i
++) {
4701 dma_free_coherent(&pinstance
->pdev
->dev
,
4702 HRRQ_ENTRY_SIZE
* PMCRAID_MAX_CMD
,
4703 pinstance
->hrrq_start
[i
],
4704 pinstance
->hrrq_start_bus_addr
[i
]);
4706 /* reset pointers and toggle bit to zeros */
4707 pinstance
->hrrq_start
[i
] = NULL
;
4708 pinstance
->hrrq_start_bus_addr
[i
] = 0;
4709 pinstance
->host_toggle_bit
[i
] = 0;
4714 * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
4715 * @pinstance: pointer to per adapter instance structure
4718 * 0 hrrq buffers are allocated, -ENOMEM otherwise.
4720 static int pmcraid_allocate_host_rrqs(struct pmcraid_instance
*pinstance
)
4724 buffer_size
= HRRQ_ENTRY_SIZE
* PMCRAID_MAX_CMD
;
4726 for (i
= 0; i
< pinstance
->num_hrrq
; i
++) {
4727 pinstance
->hrrq_start
[i
] =
4728 dma_alloc_coherent(&pinstance
->pdev
->dev
, buffer_size
,
4729 &pinstance
->hrrq_start_bus_addr
[i
],
4731 if (!pinstance
->hrrq_start
[i
]) {
4732 pmcraid_err("pci_alloc failed for hrrq vector : %d\n",
4734 pmcraid_release_host_rrqs(pinstance
, i
);
4738 memset(pinstance
->hrrq_start
[i
], 0, buffer_size
);
4739 pinstance
->hrrq_curr
[i
] = pinstance
->hrrq_start
[i
];
4740 pinstance
->hrrq_end
[i
] =
4741 pinstance
->hrrq_start
[i
] + PMCRAID_MAX_CMD
- 1;
4742 pinstance
->host_toggle_bit
[i
] = 1;
4743 spin_lock_init(&pinstance
->hrrq_lock
[i
]);
4749 * pmcraid_release_hcams - release HCAM buffers
4751 * @pinstance: pointer to per adapter instance structure
4756 static void pmcraid_release_hcams(struct pmcraid_instance
*pinstance
)
4758 if (pinstance
->ccn
.msg
!= NULL
) {
4759 dma_free_coherent(&pinstance
->pdev
->dev
,
4760 PMCRAID_AEN_HDR_SIZE
+
4761 sizeof(struct pmcraid_hcam_ccn_ext
),
4763 pinstance
->ccn
.baddr
);
4765 pinstance
->ccn
.msg
= NULL
;
4766 pinstance
->ccn
.hcam
= NULL
;
4767 pinstance
->ccn
.baddr
= 0;
4770 if (pinstance
->ldn
.msg
!= NULL
) {
4771 dma_free_coherent(&pinstance
->pdev
->dev
,
4772 PMCRAID_AEN_HDR_SIZE
+
4773 sizeof(struct pmcraid_hcam_ldn
),
4775 pinstance
->ldn
.baddr
);
4777 pinstance
->ldn
.msg
= NULL
;
4778 pinstance
->ldn
.hcam
= NULL
;
4779 pinstance
->ldn
.baddr
= 0;
4784 * pmcraid_allocate_hcams - allocates HCAM buffers
4785 * @pinstance : pointer to per adapter instance structure
4788 * 0 in case of successful allocation, non-zero otherwise
4790 static int pmcraid_allocate_hcams(struct pmcraid_instance
*pinstance
)
4792 pinstance
->ccn
.msg
= dma_alloc_coherent(&pinstance
->pdev
->dev
,
4793 PMCRAID_AEN_HDR_SIZE
+
4794 sizeof(struct pmcraid_hcam_ccn_ext
),
4795 &pinstance
->ccn
.baddr
, GFP_KERNEL
);
4797 pinstance
->ldn
.msg
= dma_alloc_coherent(&pinstance
->pdev
->dev
,
4798 PMCRAID_AEN_HDR_SIZE
+
4799 sizeof(struct pmcraid_hcam_ldn
),
4800 &pinstance
->ldn
.baddr
, GFP_KERNEL
);
4802 if (pinstance
->ldn
.msg
== NULL
|| pinstance
->ccn
.msg
== NULL
) {
4803 pmcraid_release_hcams(pinstance
);
4805 pinstance
->ccn
.hcam
=
4806 (void *)pinstance
->ccn
.msg
+ PMCRAID_AEN_HDR_SIZE
;
4807 pinstance
->ldn
.hcam
=
4808 (void *)pinstance
->ldn
.msg
+ PMCRAID_AEN_HDR_SIZE
;
4810 atomic_set(&pinstance
->ccn
.ignore
, 0);
4811 atomic_set(&pinstance
->ldn
.ignore
, 0);
4814 return (pinstance
->ldn
.msg
== NULL
) ? -ENOMEM
: 0;
4818 * pmcraid_release_config_buffers - release config.table buffers
4819 * @pinstance: pointer to per adapter instance structure
4824 static void pmcraid_release_config_buffers(struct pmcraid_instance
*pinstance
)
4826 if (pinstance
->cfg_table
!= NULL
&&
4827 pinstance
->cfg_table_bus_addr
!= 0) {
4828 dma_free_coherent(&pinstance
->pdev
->dev
,
4829 sizeof(struct pmcraid_config_table
),
4830 pinstance
->cfg_table
,
4831 pinstance
->cfg_table_bus_addr
);
4832 pinstance
->cfg_table
= NULL
;
4833 pinstance
->cfg_table_bus_addr
= 0;
4836 if (pinstance
->res_entries
!= NULL
) {
4839 for (i
= 0; i
< PMCRAID_MAX_RESOURCES
; i
++)
4840 list_del(&pinstance
->res_entries
[i
].queue
);
4841 kfree(pinstance
->res_entries
);
4842 pinstance
->res_entries
= NULL
;
4845 pmcraid_release_hcams(pinstance
);
4849 * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
4850 * @pinstance : pointer to per adapter instance structure
4853 * 0 for successful allocation, -ENOMEM for any failure
4855 static int pmcraid_allocate_config_buffers(struct pmcraid_instance
*pinstance
)
4859 pinstance
->res_entries
=
4860 kcalloc(PMCRAID_MAX_RESOURCES
,
4861 sizeof(struct pmcraid_resource_entry
),
4864 if (NULL
== pinstance
->res_entries
) {
4865 pmcraid_err("failed to allocate memory for resource table\n");
4869 for (i
= 0; i
< PMCRAID_MAX_RESOURCES
; i
++)
4870 list_add_tail(&pinstance
->res_entries
[i
].queue
,
4871 &pinstance
->free_res_q
);
4873 pinstance
->cfg_table
= dma_alloc_coherent(&pinstance
->pdev
->dev
,
4874 sizeof(struct pmcraid_config_table
),
4875 &pinstance
->cfg_table_bus_addr
,
4878 if (NULL
== pinstance
->cfg_table
) {
4879 pmcraid_err("couldn't alloc DMA memory for config table\n");
4880 pmcraid_release_config_buffers(pinstance
);
4884 if (pmcraid_allocate_hcams(pinstance
)) {
4885 pmcraid_err("could not alloc DMA memory for HCAMS\n");
4886 pmcraid_release_config_buffers(pinstance
);
4894 * pmcraid_init_tasklets - registers tasklets for response handling
4896 * @pinstance: pointer adapter instance structure
4901 static void pmcraid_init_tasklets(struct pmcraid_instance
*pinstance
)
4904 for (i
= 0; i
< pinstance
->num_hrrq
; i
++)
4905 tasklet_init(&pinstance
->isr_tasklet
[i
],
4906 pmcraid_tasklet_function
,
4907 (unsigned long)&pinstance
->hrrq_vector
[i
]);
4911 * pmcraid_kill_tasklets - destroys tasklets registered for response handling
4913 * @pinstance: pointer to adapter instance structure
4918 static void pmcraid_kill_tasklets(struct pmcraid_instance
*pinstance
)
4921 for (i
= 0; i
< pinstance
->num_hrrq
; i
++)
4922 tasklet_kill(&pinstance
->isr_tasklet
[i
]);
4926 * pmcraid_release_buffers - release per-adapter buffers allocated
4928 * @pinstance: pointer to adapter soft state
4933 static void pmcraid_release_buffers(struct pmcraid_instance
*pinstance
)
4935 pmcraid_release_config_buffers(pinstance
);
4936 pmcraid_release_control_blocks(pinstance
, PMCRAID_MAX_CMD
);
4937 pmcraid_release_cmd_blocks(pinstance
, PMCRAID_MAX_CMD
);
4938 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
4940 if (pinstance
->inq_data
!= NULL
) {
4941 dma_free_coherent(&pinstance
->pdev
->dev
,
4942 sizeof(struct pmcraid_inquiry_data
),
4943 pinstance
->inq_data
,
4944 pinstance
->inq_data_baddr
);
4946 pinstance
->inq_data
= NULL
;
4947 pinstance
->inq_data_baddr
= 0;
4950 if (pinstance
->timestamp_data
!= NULL
) {
4951 dma_free_coherent(&pinstance
->pdev
->dev
,
4952 sizeof(struct pmcraid_timestamp_data
),
4953 pinstance
->timestamp_data
,
4954 pinstance
->timestamp_data_baddr
);
4956 pinstance
->timestamp_data
= NULL
;
4957 pinstance
->timestamp_data_baddr
= 0;
4962 * pmcraid_init_buffers - allocates memory and initializes various structures
4963 * @pinstance: pointer to per adapter instance structure
4965 * This routine pre-allocates memory based on the type of block as below:
4966 * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
4967 * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
4968 * config-table entries : DMAable memory using dma_alloc_coherent
4969 * HostRRQs : DMAable memory, using dma_alloc_coherent
4972 * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
4974 static int pmcraid_init_buffers(struct pmcraid_instance
*pinstance
)
4978 if (pmcraid_allocate_host_rrqs(pinstance
)) {
4979 pmcraid_err("couldn't allocate memory for %d host rrqs\n",
4980 pinstance
->num_hrrq
);
4984 if (pmcraid_allocate_config_buffers(pinstance
)) {
4985 pmcraid_err("couldn't allocate memory for config buffers\n");
4986 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
4990 if (pmcraid_allocate_cmd_blocks(pinstance
)) {
4991 pmcraid_err("couldn't allocate memory for cmd blocks\n");
4992 pmcraid_release_config_buffers(pinstance
);
4993 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
4997 if (pmcraid_allocate_control_blocks(pinstance
)) {
4998 pmcraid_err("couldn't allocate memory control blocks\n");
4999 pmcraid_release_config_buffers(pinstance
);
5000 pmcraid_release_cmd_blocks(pinstance
, PMCRAID_MAX_CMD
);
5001 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
5005 /* allocate DMAable memory for page D0 INQUIRY buffer */
5006 pinstance
->inq_data
= dma_alloc_coherent(&pinstance
->pdev
->dev
,
5007 sizeof(struct pmcraid_inquiry_data
),
5008 &pinstance
->inq_data_baddr
, GFP_KERNEL
);
5009 if (pinstance
->inq_data
== NULL
) {
5010 pmcraid_err("couldn't allocate DMA memory for INQUIRY\n");
5011 pmcraid_release_buffers(pinstance
);
5015 /* allocate DMAable memory for set timestamp data buffer */
5016 pinstance
->timestamp_data
= dma_alloc_coherent(&pinstance
->pdev
->dev
,
5017 sizeof(struct pmcraid_timestamp_data
),
5018 &pinstance
->timestamp_data_baddr
,
5020 if (pinstance
->timestamp_data
== NULL
) {
5021 pmcraid_err("couldn't allocate DMA memory for \
5022 set time_stamp \n");
5023 pmcraid_release_buffers(pinstance
);
5028 /* Initialize all the command blocks and add them to free pool. No
5029 * need to lock (free_pool_lock) as this is done in initialization
5032 for (i
= 0; i
< PMCRAID_MAX_CMD
; i
++) {
5033 struct pmcraid_cmd
*cmdp
= pinstance
->cmd_list
[i
];
5034 pmcraid_init_cmdblk(cmdp
, i
);
5035 cmdp
->drv_inst
= pinstance
;
5036 list_add_tail(&cmdp
->free_list
, &pinstance
->free_cmd_pool
);
5043 * pmcraid_reinit_buffers - resets various buffer pointers
5044 * @pinstance: pointer to adapter instance
5048 static void pmcraid_reinit_buffers(struct pmcraid_instance
*pinstance
)
5051 int buffer_size
= HRRQ_ENTRY_SIZE
* PMCRAID_MAX_CMD
;
5053 for (i
= 0; i
< pinstance
->num_hrrq
; i
++) {
5054 memset(pinstance
->hrrq_start
[i
], 0, buffer_size
);
5055 pinstance
->hrrq_curr
[i
] = pinstance
->hrrq_start
[i
];
5056 pinstance
->hrrq_end
[i
] =
5057 pinstance
->hrrq_start
[i
] + PMCRAID_MAX_CMD
- 1;
5058 pinstance
->host_toggle_bit
[i
] = 1;
5063 * pmcraid_init_instance - initialize per instance data structure
5064 * @pdev: pointer to pci device structure
5065 * @host: pointer to Scsi_Host structure
5066 * @mapped_pci_addr: memory mapped IOA configuration registers
5069 * 0 on success, non-zero in case of any failure
5071 static int pmcraid_init_instance(struct pci_dev
*pdev
, struct Scsi_Host
*host
,
5072 void __iomem
*mapped_pci_addr
)
5074 struct pmcraid_instance
*pinstance
=
5075 (struct pmcraid_instance
*)host
->hostdata
;
5077 pinstance
->host
= host
;
5078 pinstance
->pdev
= pdev
;
5080 /* Initialize register addresses */
5081 pinstance
->mapped_dma_addr
= mapped_pci_addr
;
5083 /* Initialize chip-specific details */
5085 struct pmcraid_chip_details
*chip_cfg
= pinstance
->chip_cfg
;
5086 struct pmcraid_interrupts
*pint_regs
= &pinstance
->int_regs
;
5088 pinstance
->ioarrin
= mapped_pci_addr
+ chip_cfg
->ioarrin
;
5090 pint_regs
->ioa_host_interrupt_reg
=
5091 mapped_pci_addr
+ chip_cfg
->ioa_host_intr
;
5092 pint_regs
->ioa_host_interrupt_clr_reg
=
5093 mapped_pci_addr
+ chip_cfg
->ioa_host_intr_clr
;
5094 pint_regs
->ioa_host_msix_interrupt_reg
=
5095 mapped_pci_addr
+ chip_cfg
->ioa_host_msix_intr
;
5096 pint_regs
->host_ioa_interrupt_reg
=
5097 mapped_pci_addr
+ chip_cfg
->host_ioa_intr
;
5098 pint_regs
->host_ioa_interrupt_clr_reg
=
5099 mapped_pci_addr
+ chip_cfg
->host_ioa_intr_clr
;
5101 /* Current version of firmware exposes interrupt mask set
5102 * and mask clr registers through memory mapped bar0.
5104 pinstance
->mailbox
= mapped_pci_addr
+ chip_cfg
->mailbox
;
5105 pinstance
->ioa_status
= mapped_pci_addr
+ chip_cfg
->ioastatus
;
5106 pint_regs
->ioa_host_interrupt_mask_reg
=
5107 mapped_pci_addr
+ chip_cfg
->ioa_host_mask
;
5108 pint_regs
->ioa_host_interrupt_mask_clr_reg
=
5109 mapped_pci_addr
+ chip_cfg
->ioa_host_mask_clr
;
5110 pint_regs
->global_interrupt_mask_reg
=
5111 mapped_pci_addr
+ chip_cfg
->global_intr_mask
;
5114 pinstance
->ioa_reset_attempts
= 0;
5115 init_waitqueue_head(&pinstance
->reset_wait_q
);
5117 atomic_set(&pinstance
->outstanding_cmds
, 0);
5118 atomic_set(&pinstance
->last_message_id
, 0);
5119 atomic_set(&pinstance
->expose_resources
, 0);
5121 INIT_LIST_HEAD(&pinstance
->free_res_q
);
5122 INIT_LIST_HEAD(&pinstance
->used_res_q
);
5123 INIT_LIST_HEAD(&pinstance
->free_cmd_pool
);
5124 INIT_LIST_HEAD(&pinstance
->pending_cmd_pool
);
5126 spin_lock_init(&pinstance
->free_pool_lock
);
5127 spin_lock_init(&pinstance
->pending_pool_lock
);
5128 spin_lock_init(&pinstance
->resource_lock
);
5129 mutex_init(&pinstance
->aen_queue_lock
);
5131 /* Work-queue (Shared) for deferred processing error handling */
5132 INIT_WORK(&pinstance
->worker_q
, pmcraid_worker_function
);
5134 /* Initialize the default log_level */
5135 pinstance
->current_log_level
= pmcraid_log_level
;
5137 /* Setup variables required for reset engine */
5138 pinstance
->ioa_state
= IOA_STATE_UNKNOWN
;
5139 pinstance
->reset_cmd
= NULL
;
5144 * pmcraid_shutdown - shutdown adapter controller.
5145 * @pdev: pci device struct
5147 * Issues an adapter shutdown to the card waits for its completion
5152 static void pmcraid_shutdown(struct pci_dev
*pdev
)
5154 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5155 pmcraid_reset_bringdown(pinstance
);
5160 * pmcraid_get_minor - returns unused minor number from minor number bitmap
5162 static unsigned short pmcraid_get_minor(void)
5166 minor
= find_first_zero_bit(pmcraid_minor
, PMCRAID_MAX_ADAPTERS
);
5167 __set_bit(minor
, pmcraid_minor
);
5172 * pmcraid_release_minor - releases given minor back to minor number bitmap
5174 static void pmcraid_release_minor(unsigned short minor
)
5176 __clear_bit(minor
, pmcraid_minor
);
5180 * pmcraid_setup_chrdev - allocates a minor number and registers a char device
5182 * @pinstance: pointer to adapter instance for which to register device
5185 * 0 in case of success, otherwise non-zero
5187 static int pmcraid_setup_chrdev(struct pmcraid_instance
*pinstance
)
5192 minor
= pmcraid_get_minor();
5193 cdev_init(&pinstance
->cdev
, &pmcraid_fops
);
5194 pinstance
->cdev
.owner
= THIS_MODULE
;
5196 error
= cdev_add(&pinstance
->cdev
, MKDEV(pmcraid_major
, minor
), 1);
5199 pmcraid_release_minor(minor
);
5201 device_create(pmcraid_class
, NULL
, MKDEV(pmcraid_major
, minor
),
5202 NULL
, "%s%u", PMCRAID_DEVFILE
, minor
);
5207 * pmcraid_release_chrdev - unregisters per-adapter management interface
5209 * @pinstance: pointer to adapter instance structure
5214 static void pmcraid_release_chrdev(struct pmcraid_instance
*pinstance
)
5216 pmcraid_release_minor(MINOR(pinstance
->cdev
.dev
));
5217 device_destroy(pmcraid_class
,
5218 MKDEV(pmcraid_major
, MINOR(pinstance
->cdev
.dev
)));
5219 cdev_del(&pinstance
->cdev
);
5223 * pmcraid_remove - IOA hot plug remove entry point
5224 * @pdev: pci device struct
5229 static void pmcraid_remove(struct pci_dev
*pdev
)
5231 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5233 /* remove the management interface (/dev file) for this device */
5234 pmcraid_release_chrdev(pinstance
);
5236 /* remove host template from scsi midlayer */
5237 scsi_remove_host(pinstance
->host
);
5239 /* block requests from mid-layer */
5240 scsi_block_requests(pinstance
->host
);
5242 /* initiate shutdown adapter */
5243 pmcraid_shutdown(pdev
);
5245 pmcraid_disable_interrupts(pinstance
, ~0);
5246 flush_work(&pinstance
->worker_q
);
5248 pmcraid_kill_tasklets(pinstance
);
5249 pmcraid_unregister_interrupt_handler(pinstance
);
5250 pmcraid_release_buffers(pinstance
);
5251 iounmap(pinstance
->mapped_dma_addr
);
5252 pci_release_regions(pdev
);
5253 scsi_host_put(pinstance
->host
);
5254 pci_disable_device(pdev
);
5261 * pmcraid_suspend - driver suspend entry point for power management
5262 * @pdev: PCI device structure
5263 * @state: PCI power state to suspend routine
5265 * Return Value - 0 always
5267 static int pmcraid_suspend(struct pci_dev
*pdev
, pm_message_t state
)
5269 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5271 pmcraid_shutdown(pdev
);
5272 pmcraid_disable_interrupts(pinstance
, ~0);
5273 pmcraid_kill_tasklets(pinstance
);
5274 pci_set_drvdata(pinstance
->pdev
, pinstance
);
5275 pmcraid_unregister_interrupt_handler(pinstance
);
5276 pci_save_state(pdev
);
5277 pci_disable_device(pdev
);
5278 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
5284 * pmcraid_resume - driver resume entry point PCI power management
5285 * @pdev: PCI device structure
5287 * Return Value - 0 in case of success. Error code in case of any failure
5289 static int pmcraid_resume(struct pci_dev
*pdev
)
5291 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5292 struct Scsi_Host
*host
= pinstance
->host
;
5295 pci_set_power_state(pdev
, PCI_D0
);
5296 pci_enable_wake(pdev
, PCI_D0
, 0);
5297 pci_restore_state(pdev
);
5299 rc
= pci_enable_device(pdev
);
5302 dev_err(&pdev
->dev
, "resume: Enable device failed\n");
5306 pci_set_master(pdev
);
5308 if (sizeof(dma_addr_t
) == 4 ||
5309 dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(64)))
5310 rc
= dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(32));
5313 rc
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
5316 dev_err(&pdev
->dev
, "resume: Failed to set PCI DMA mask\n");
5317 goto disable_device
;
5320 pmcraid_disable_interrupts(pinstance
, ~0);
5321 atomic_set(&pinstance
->outstanding_cmds
, 0);
5322 rc
= pmcraid_register_interrupt_handler(pinstance
);
5326 "resume: couldn't register interrupt handlers\n");
5331 pmcraid_init_tasklets(pinstance
);
5332 pmcraid_enable_interrupts(pinstance
, PMCRAID_PCI_INTERRUPTS
);
5334 /* Start with hard reset sequence which brings up IOA to operational
5335 * state as well as completes the reset sequence.
5337 pinstance
->ioa_hard_reset
= 1;
5339 /* Start IOA firmware initialization and bring card to Operational
5342 if (pmcraid_reset_bringup(pinstance
)) {
5343 dev_err(&pdev
->dev
, "couldn't initialize IOA\n");
5345 goto release_tasklets
;
5351 pmcraid_disable_interrupts(pinstance
, ~0);
5352 pmcraid_kill_tasklets(pinstance
);
5353 pmcraid_unregister_interrupt_handler(pinstance
);
5356 scsi_host_put(host
);
5359 pci_disable_device(pdev
);
5366 #define pmcraid_suspend NULL
5367 #define pmcraid_resume NULL
5369 #endif /* CONFIG_PM */
5372 * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
5373 * completion of the ioa reset
5374 * @cmd: pointer to reset command block
5376 static void pmcraid_complete_ioa_reset(struct pmcraid_cmd
*cmd
)
5378 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5379 unsigned long flags
;
5381 spin_lock_irqsave(pinstance
->host
->host_lock
, flags
);
5382 pmcraid_ioa_reset(cmd
);
5383 spin_unlock_irqrestore(pinstance
->host
->host_lock
, flags
);
5384 scsi_unblock_requests(pinstance
->host
);
5385 schedule_work(&pinstance
->worker_q
);
5389 * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
5391 * @cmd: pointer to pmcraid_cmd structure
5394 * 0 for success or non-zero for failure cases
5396 static void pmcraid_set_supported_devs(struct pmcraid_cmd
*cmd
)
5398 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
5399 void (*cmd_done
) (struct pmcraid_cmd
*) = pmcraid_complete_ioa_reset
;
5401 pmcraid_reinit_cmdblk(cmd
);
5403 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
5404 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
5405 ioarcb
->cdb
[0] = PMCRAID_SET_SUPPORTED_DEVICES
;
5406 ioarcb
->cdb
[1] = ALL_DEVICES_SUPPORTED
;
5408 /* If this was called as part of resource table reinitialization due to
5409 * lost CCN, it is enough to return the command block back to free pool
5410 * as part of set_supported_devs completion function.
5412 if (cmd
->drv_inst
->reinit_cfg_table
) {
5413 cmd
->drv_inst
->reinit_cfg_table
= 0;
5415 cmd_done
= pmcraid_reinit_cfgtable_done
;
5418 /* we will be done with the reset sequence after set supported devices,
5419 * setup the done function to return the command block back to free
5422 pmcraid_send_cmd(cmd
,
5424 PMCRAID_SET_SUP_DEV_TIMEOUT
,
5425 pmcraid_timeout_handler
);
5430 * pmcraid_set_timestamp - set the timestamp to IOAFP
5432 * @cmd: pointer to pmcraid_cmd structure
5435 * 0 for success or non-zero for failure cases
5437 static void pmcraid_set_timestamp(struct pmcraid_cmd
*cmd
)
5439 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5440 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
5441 __be32 time_stamp_len
= cpu_to_be32(PMCRAID_TIMESTAMP_LEN
);
5442 struct pmcraid_ioadl_desc
*ioadl
;
5445 timestamp
= ktime_get_real_seconds() * 1000;
5447 pinstance
->timestamp_data
->timestamp
[0] = (__u8
)(timestamp
);
5448 pinstance
->timestamp_data
->timestamp
[1] = (__u8
)((timestamp
) >> 8);
5449 pinstance
->timestamp_data
->timestamp
[2] = (__u8
)((timestamp
) >> 16);
5450 pinstance
->timestamp_data
->timestamp
[3] = (__u8
)((timestamp
) >> 24);
5451 pinstance
->timestamp_data
->timestamp
[4] = (__u8
)((timestamp
) >> 32);
5452 pinstance
->timestamp_data
->timestamp
[5] = (__u8
)((timestamp
) >> 40);
5454 pmcraid_reinit_cmdblk(cmd
);
5455 ioarcb
->request_type
= REQ_TYPE_SCSI
;
5456 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
5457 ioarcb
->cdb
[0] = PMCRAID_SCSI_SET_TIMESTAMP
;
5458 ioarcb
->cdb
[1] = PMCRAID_SCSI_SERVICE_ACTION
;
5459 memcpy(&(ioarcb
->cdb
[6]), &time_stamp_len
, sizeof(time_stamp_len
));
5461 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
5462 offsetof(struct pmcraid_ioarcb
,
5463 add_data
.u
.ioadl
[0]));
5464 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
5465 ioarcb
->ioarcb_bus_addr
&= cpu_to_le64(~(0x1FULL
));
5467 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
5468 ioarcb
->request_flags0
|= TRANSFER_DIR_WRITE
;
5469 ioarcb
->data_transfer_length
=
5470 cpu_to_le32(sizeof(struct pmcraid_timestamp_data
));
5471 ioadl
= &(ioarcb
->add_data
.u
.ioadl
[0]);
5472 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
5473 ioadl
->address
= cpu_to_le64(pinstance
->timestamp_data_baddr
);
5474 ioadl
->data_len
= cpu_to_le32(sizeof(struct pmcraid_timestamp_data
));
5476 if (!pinstance
->timestamp_error
) {
5477 pinstance
->timestamp_error
= 0;
5478 pmcraid_send_cmd(cmd
, pmcraid_set_supported_devs
,
5479 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
5481 pmcraid_send_cmd(cmd
, pmcraid_return_cmd
,
5482 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
5489 * pmcraid_init_res_table - Initialize the resource table
5490 * @cmd: pointer to pmcraid command struct
5492 * This function looks through the existing resource table, comparing
5493 * it with the config table. This function will take care of old/new
5494 * devices and schedule adding/removing them from the mid-layer
5500 static void pmcraid_init_res_table(struct pmcraid_cmd
*cmd
)
5502 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5503 struct pmcraid_resource_entry
*res
, *temp
;
5504 struct pmcraid_config_table_entry
*cfgte
;
5505 unsigned long lock_flags
;
5510 if (pinstance
->cfg_table
->flags
& MICROCODE_UPDATE_REQUIRED
)
5511 pmcraid_err("IOA requires microcode download\n");
5513 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
5515 /* resource list is protected by pinstance->resource_lock.
5516 * init_res_table can be called from probe (user-thread) or runtime
5517 * reset (timer/tasklet)
5519 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
5521 list_for_each_entry_safe(res
, temp
, &pinstance
->used_res_q
, queue
)
5522 list_move_tail(&res
->queue
, &old_res
);
5524 for (i
= 0; i
< le16_to_cpu(pinstance
->cfg_table
->num_entries
); i
++) {
5525 if (be16_to_cpu(pinstance
->inq_data
->fw_version
) <=
5526 PMCRAID_FW_VERSION_1
)
5527 cfgte
= &pinstance
->cfg_table
->entries
[i
];
5529 cfgte
= (struct pmcraid_config_table_entry
*)
5530 &pinstance
->cfg_table
->entries_ext
[i
];
5532 if (!pmcraid_expose_resource(fw_version
, cfgte
))
5537 /* If this entry was already detected and initialized */
5538 list_for_each_entry_safe(res
, temp
, &old_res
, queue
) {
5540 rc
= memcmp(&res
->cfg_entry
.resource_address
,
5541 &cfgte
->resource_address
,
5542 sizeof(cfgte
->resource_address
));
5544 list_move_tail(&res
->queue
,
5545 &pinstance
->used_res_q
);
5551 /* If this is new entry, initialize it and add it the queue */
5554 if (list_empty(&pinstance
->free_res_q
)) {
5555 pmcraid_err("Too many devices attached\n");
5560 res
= list_entry(pinstance
->free_res_q
.next
,
5561 struct pmcraid_resource_entry
, queue
);
5563 res
->scsi_dev
= NULL
;
5564 res
->change_detected
= RES_CHANGE_ADD
;
5565 res
->reset_progress
= 0;
5566 list_move_tail(&res
->queue
, &pinstance
->used_res_q
);
5569 /* copy new configuration table entry details into driver
5570 * maintained resource entry
5573 memcpy(&res
->cfg_entry
, cfgte
,
5574 pinstance
->config_table_entry_size
);
5575 pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
5576 res
->cfg_entry
.resource_type
,
5577 (fw_version
<= PMCRAID_FW_VERSION_1
?
5578 res
->cfg_entry
.unique_flags1
:
5579 le16_to_cpu(res
->cfg_entry
.array_id
) & 0xFF),
5580 le32_to_cpu(res
->cfg_entry
.resource_address
));
5584 /* Detect any deleted entries, mark them for deletion from mid-layer */
5585 list_for_each_entry_safe(res
, temp
, &old_res
, queue
) {
5587 if (res
->scsi_dev
) {
5588 res
->change_detected
= RES_CHANGE_DEL
;
5589 res
->cfg_entry
.resource_handle
=
5590 PMCRAID_INVALID_RES_HANDLE
;
5591 list_move_tail(&res
->queue
, &pinstance
->used_res_q
);
5593 list_move_tail(&res
->queue
, &pinstance
->free_res_q
);
5597 /* release the resource list lock */
5598 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
5599 pmcraid_set_timestamp(cmd
);
5603 * pmcraid_querycfg - Send a Query IOA Config to the adapter.
5604 * @cmd: pointer pmcraid_cmd struct
5606 * This function sends a Query IOA Configuration command to the adapter to
5607 * retrieve the IOA configuration table.
5612 static void pmcraid_querycfg(struct pmcraid_cmd
*cmd
)
5614 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
5615 struct pmcraid_ioadl_desc
*ioadl
;
5616 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5617 __be32 cfg_table_size
= cpu_to_be32(sizeof(struct pmcraid_config_table
));
5619 if (be16_to_cpu(pinstance
->inq_data
->fw_version
) <=
5620 PMCRAID_FW_VERSION_1
)
5621 pinstance
->config_table_entry_size
=
5622 sizeof(struct pmcraid_config_table_entry
);
5624 pinstance
->config_table_entry_size
=
5625 sizeof(struct pmcraid_config_table_entry_ext
);
5627 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
5628 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
5630 ioarcb
->cdb
[0] = PMCRAID_QUERY_IOA_CONFIG
;
5632 /* firmware requires 4-byte length field, specified in B.E format */
5633 memcpy(&(ioarcb
->cdb
[10]), &cfg_table_size
, sizeof(cfg_table_size
));
5635 /* Since entire config table can be described by single IOADL, it can
5636 * be part of IOARCB itself
5638 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
5639 offsetof(struct pmcraid_ioarcb
,
5640 add_data
.u
.ioadl
[0]));
5641 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
5642 ioarcb
->ioarcb_bus_addr
&= cpu_to_le64(~0x1FULL
);
5644 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
5645 ioarcb
->data_transfer_length
=
5646 cpu_to_le32(sizeof(struct pmcraid_config_table
));
5648 ioadl
= &(ioarcb
->add_data
.u
.ioadl
[0]);
5649 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
5650 ioadl
->address
= cpu_to_le64(pinstance
->cfg_table_bus_addr
);
5651 ioadl
->data_len
= cpu_to_le32(sizeof(struct pmcraid_config_table
));
5653 pmcraid_send_cmd(cmd
, pmcraid_init_res_table
,
5654 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
5659 * pmcraid_probe - PCI probe entry pointer for PMC MaxRAID controller driver
5660 * @pdev: pointer to pci device structure
5661 * @dev_id: pointer to device ids structure
5664 * returns 0 if the device is claimed and successfully configured.
5665 * returns non-zero error code in case of any failure
5667 static int pmcraid_probe(struct pci_dev
*pdev
,
5668 const struct pci_device_id
*dev_id
)
5670 struct pmcraid_instance
*pinstance
;
5671 struct Scsi_Host
*host
;
5672 void __iomem
*mapped_pci_addr
;
5673 int rc
= PCIBIOS_SUCCESSFUL
;
5675 if (atomic_read(&pmcraid_adapter_count
) >= PMCRAID_MAX_ADAPTERS
) {
5677 ("maximum number(%d) of supported adapters reached\n",
5678 atomic_read(&pmcraid_adapter_count
));
5682 atomic_inc(&pmcraid_adapter_count
);
5683 rc
= pci_enable_device(pdev
);
5686 dev_err(&pdev
->dev
, "Cannot enable adapter\n");
5687 atomic_dec(&pmcraid_adapter_count
);
5691 dev_info(&pdev
->dev
,
5692 "Found new IOA(%x:%x), Total IOA count: %d\n",
5693 pdev
->vendor
, pdev
->device
,
5694 atomic_read(&pmcraid_adapter_count
));
5696 rc
= pci_request_regions(pdev
, PMCRAID_DRIVER_NAME
);
5700 "Couldn't register memory range of registers\n");
5701 goto out_disable_device
;
5704 mapped_pci_addr
= pci_iomap(pdev
, 0, 0);
5706 if (!mapped_pci_addr
) {
5707 dev_err(&pdev
->dev
, "Couldn't map PCI registers memory\n");
5709 goto out_release_regions
;
5712 pci_set_master(pdev
);
5714 /* Firmware requires the system bus address of IOARCB to be within
5715 * 32-bit addressable range though it has 64-bit IOARRIN register.
5716 * However, firmware supports 64-bit streaming DMA buffers, whereas
5717 * coherent buffers are to be 32-bit. Since dma_alloc_coherent always
5718 * returns memory within 4GB (if not, change this logic), coherent
5719 * buffers are within firmware acceptable address ranges.
5721 if (sizeof(dma_addr_t
) == 4 ||
5722 dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(64)))
5723 rc
= dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(32));
5725 /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
5726 * bit mask for dma_alloc_coherent to return addresses within 4GB
5729 rc
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
5732 dev_err(&pdev
->dev
, "Failed to set PCI DMA mask\n");
5736 host
= scsi_host_alloc(&pmcraid_host_template
,
5737 sizeof(struct pmcraid_instance
));
5740 dev_err(&pdev
->dev
, "scsi_host_alloc failed!\n");
5745 host
->max_id
= PMCRAID_MAX_NUM_TARGETS_PER_BUS
;
5746 host
->max_lun
= PMCRAID_MAX_NUM_LUNS_PER_TARGET
;
5747 host
->unique_id
= host
->host_no
;
5748 host
->max_channel
= PMCRAID_MAX_BUS_TO_SCAN
;
5749 host
->max_cmd_len
= PMCRAID_MAX_CDB_LEN
;
5751 /* zero out entire instance structure */
5752 pinstance
= (struct pmcraid_instance
*)host
->hostdata
;
5753 memset(pinstance
, 0, sizeof(*pinstance
));
5755 pinstance
->chip_cfg
=
5756 (struct pmcraid_chip_details
*)(dev_id
->driver_data
);
5758 rc
= pmcraid_init_instance(pdev
, host
, mapped_pci_addr
);
5761 dev_err(&pdev
->dev
, "failed to initialize adapter instance\n");
5762 goto out_scsi_host_put
;
5765 pci_set_drvdata(pdev
, pinstance
);
5767 /* Save PCI config-space for use following the reset */
5768 rc
= pci_save_state(pinstance
->pdev
);
5771 dev_err(&pdev
->dev
, "Failed to save PCI config space\n");
5772 goto out_scsi_host_put
;
5775 pmcraid_disable_interrupts(pinstance
, ~0);
5777 rc
= pmcraid_register_interrupt_handler(pinstance
);
5780 dev_err(&pdev
->dev
, "couldn't register interrupt handler\n");
5781 goto out_scsi_host_put
;
5784 pmcraid_init_tasklets(pinstance
);
5786 /* allocate verious buffers used by LLD.*/
5787 rc
= pmcraid_init_buffers(pinstance
);
5790 pmcraid_err("couldn't allocate memory blocks\n");
5791 goto out_unregister_isr
;
5794 /* check the reset type required */
5795 pmcraid_reset_type(pinstance
);
5797 pmcraid_enable_interrupts(pinstance
, PMCRAID_PCI_INTERRUPTS
);
5799 /* Start IOA firmware initialization and bring card to Operational
5802 pmcraid_info("starting IOA initialization sequence\n");
5803 if (pmcraid_reset_bringup(pinstance
)) {
5804 dev_err(&pdev
->dev
, "couldn't initialize IOA\n");
5806 goto out_release_bufs
;
5809 /* Add adapter instance into mid-layer list */
5810 rc
= scsi_add_host(pinstance
->host
, &pdev
->dev
);
5812 pmcraid_err("couldn't add host into mid-layer: %d\n", rc
);
5813 goto out_release_bufs
;
5816 scsi_scan_host(pinstance
->host
);
5818 rc
= pmcraid_setup_chrdev(pinstance
);
5821 pmcraid_err("couldn't create mgmt interface, error: %x\n",
5823 goto out_remove_host
;
5826 /* Schedule worker thread to handle CCN and take care of adding and
5827 * removing devices to OS
5829 atomic_set(&pinstance
->expose_resources
, 1);
5830 schedule_work(&pinstance
->worker_q
);
5834 scsi_remove_host(host
);
5837 pmcraid_release_buffers(pinstance
);
5840 pmcraid_kill_tasklets(pinstance
);
5841 pmcraid_unregister_interrupt_handler(pinstance
);
5844 scsi_host_put(host
);
5847 iounmap(mapped_pci_addr
);
5849 out_release_regions
:
5850 pci_release_regions(pdev
);
5853 atomic_dec(&pmcraid_adapter_count
);
5854 pci_disable_device(pdev
);
5859 * PCI driver structure of pcmraid driver
5861 static struct pci_driver pmcraid_driver
= {
5862 .name
= PMCRAID_DRIVER_NAME
,
5863 .id_table
= pmcraid_pci_table
,
5864 .probe
= pmcraid_probe
,
5865 .remove
= pmcraid_remove
,
5866 .suspend
= pmcraid_suspend
,
5867 .resume
= pmcraid_resume
,
5868 .shutdown
= pmcraid_shutdown
5872 * pmcraid_init - module load entry point
5874 static int __init
pmcraid_init(void)
5879 pmcraid_info("%s Device Driver version: %s\n",
5880 PMCRAID_DRIVER_NAME
, PMCRAID_DRIVER_VERSION
);
5882 error
= alloc_chrdev_region(&dev
, 0,
5883 PMCRAID_MAX_ADAPTERS
,
5887 pmcraid_err("failed to get a major number for adapters\n");
5891 pmcraid_major
= MAJOR(dev
);
5892 pmcraid_class
= class_create(THIS_MODULE
, PMCRAID_DEVFILE
);
5894 if (IS_ERR(pmcraid_class
)) {
5895 error
= PTR_ERR(pmcraid_class
);
5896 pmcraid_err("failed to register with sysfs, error = %x\n",
5898 goto out_unreg_chrdev
;
5901 error
= pmcraid_netlink_init();
5904 class_destroy(pmcraid_class
);
5905 goto out_unreg_chrdev
;
5908 error
= pci_register_driver(&pmcraid_driver
);
5913 pmcraid_err("failed to register pmcraid driver, error = %x\n",
5915 class_destroy(pmcraid_class
);
5916 pmcraid_netlink_release();
5919 unregister_chrdev_region(MKDEV(pmcraid_major
, 0), PMCRAID_MAX_ADAPTERS
);
5926 * pmcraid_exit - module unload entry point
5928 static void __exit
pmcraid_exit(void)
5930 pmcraid_netlink_release();
5931 unregister_chrdev_region(MKDEV(pmcraid_major
, 0),
5932 PMCRAID_MAX_ADAPTERS
);
5933 pci_unregister_driver(&pmcraid_driver
);
5934 class_destroy(pmcraid_class
);
5937 module_init(pmcraid_init
);
5938 module_exit(pmcraid_exit
);