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 <scsi/scsi.h>
49 #include <scsi/scsi_host.h>
50 #include <scsi/scsi_device.h>
51 #include <scsi/scsi_tcq.h>
52 #include <scsi/scsi_eh.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsicam.h>
59 * Module configuration parameters
61 static unsigned int pmcraid_debug_log
;
62 static unsigned int pmcraid_disable_aen
;
63 static unsigned int pmcraid_log_level
= IOASC_LOG_LEVEL_MUST
;
64 static unsigned int pmcraid_enable_msix
;
67 * Data structures to support multiple adapters by the LLD.
68 * pmcraid_adapter_count - count of configured adapters
70 static atomic_t pmcraid_adapter_count
= ATOMIC_INIT(0);
73 * Supporting user-level control interface through IOCTL commands.
74 * pmcraid_major - major number to use
75 * pmcraid_minor - minor number(s) to use
77 static unsigned int pmcraid_major
;
78 static struct class *pmcraid_class
;
79 DECLARE_BITMAP(pmcraid_minor
, PMCRAID_MAX_ADAPTERS
);
84 MODULE_AUTHOR("Anil Ravindranath<anil_ravindranath@pmc-sierra.com>");
85 MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
86 MODULE_LICENSE("GPL");
87 MODULE_VERSION(PMCRAID_DRIVER_VERSION
);
89 module_param_named(log_level
, pmcraid_log_level
, uint
, (S_IRUGO
| S_IWUSR
));
90 MODULE_PARM_DESC(log_level
,
91 "Enables firmware error code logging, default :1 high-severity"
92 " errors, 2: all errors including high-severity errors,"
93 " 0: disables logging");
95 module_param_named(debug
, pmcraid_debug_log
, uint
, (S_IRUGO
| S_IWUSR
));
96 MODULE_PARM_DESC(debug
,
97 "Enable driver verbose message logging. Set 1 to enable."
100 module_param_named(disable_aen
, pmcraid_disable_aen
, uint
, (S_IRUGO
| S_IWUSR
));
101 MODULE_PARM_DESC(disable_aen
,
102 "Disable driver aen notifications to apps. Set 1 to disable."
105 /* chip specific constants for PMC MaxRAID controllers (same for
108 static struct pmcraid_chip_details pmcraid_chip_cfg
[] = {
113 .global_intr_mask
= 0x00034,
114 .ioa_host_intr
= 0x0009C,
115 .ioa_host_intr_clr
= 0x000A0,
116 .ioa_host_msix_intr
= 0x7FC40,
117 .ioa_host_mask
= 0x7FC28,
118 .ioa_host_mask_clr
= 0x7FC28,
119 .host_ioa_intr
= 0x00020,
120 .host_ioa_intr_clr
= 0x00020,
121 .transop_timeout
= 300
126 * PCI device ids supported by pmcraid driver
128 static struct pci_device_id pmcraid_pci_table
[] = {
129 { PCI_DEVICE(PCI_VENDOR_ID_PMC
, PCI_DEVICE_ID_PMC_MAXRAID
),
130 0, 0, (kernel_ulong_t
)&pmcraid_chip_cfg
[0]
135 MODULE_DEVICE_TABLE(pci
, pmcraid_pci_table
);
140 * pmcraid_slave_alloc - Prepare for commands to a device
141 * @scsi_dev: scsi device struct
143 * This function is called by mid-layer prior to sending any command to the new
144 * device. Stores resource entry details of the device in scsi_device struct.
145 * Queuecommand uses the resource handle and other details to fill up IOARCB
146 * while sending commands to the device.
149 * 0 on success / -ENXIO if device does not exist
151 static int pmcraid_slave_alloc(struct scsi_device
*scsi_dev
)
153 struct pmcraid_resource_entry
*temp
, *res
= NULL
;
154 struct pmcraid_instance
*pinstance
;
156 unsigned long lock_flags
;
160 pinstance
= shost_priv(scsi_dev
->host
);
162 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
164 /* Driver exposes VSET and GSCSI resources only; all other device types
165 * are not exposed. Resource list is synchronized using resource lock
166 * so any traversal or modifications to the list should be done inside
169 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
170 list_for_each_entry(temp
, &pinstance
->used_res_q
, queue
) {
172 /* do not expose VSETs with order-ids > MAX_VSET_TARGETS */
173 if (RES_IS_VSET(temp
->cfg_entry
)) {
174 if (fw_version
<= PMCRAID_FW_VERSION_1
)
175 target
= temp
->cfg_entry
.unique_flags1
;
177 target
= temp
->cfg_entry
.array_id
& 0xFF;
179 if (target
> PMCRAID_MAX_VSET_TARGETS
)
181 bus
= PMCRAID_VSET_BUS_ID
;
183 } else if (RES_IS_GSCSI(temp
->cfg_entry
)) {
184 target
= RES_TARGET(temp
->cfg_entry
.resource_address
);
185 bus
= PMCRAID_PHYS_BUS_ID
;
186 lun
= RES_LUN(temp
->cfg_entry
.resource_address
);
191 if (bus
== scsi_dev
->channel
&&
192 target
== scsi_dev
->id
&&
193 lun
== scsi_dev
->lun
) {
200 res
->scsi_dev
= scsi_dev
;
201 scsi_dev
->hostdata
= res
;
202 res
->change_detected
= 0;
203 atomic_set(&res
->read_failures
, 0);
204 atomic_set(&res
->write_failures
, 0);
207 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
212 * pmcraid_slave_configure - Configures a SCSI device
213 * @scsi_dev: scsi device struct
215 * This function is executed by SCSI mid layer just after a device is first
216 * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
217 * timeout value (default 30s) will be over-written to a higher value (60s)
218 * and max_sectors value will be over-written to 512. It also sets queue depth
219 * to host->cmd_per_lun value
224 static int pmcraid_slave_configure(struct scsi_device
*scsi_dev
)
226 struct pmcraid_resource_entry
*res
= scsi_dev
->hostdata
;
231 /* LLD exposes VSETs and Enclosure devices only */
232 if (RES_IS_GSCSI(res
->cfg_entry
) &&
233 scsi_dev
->type
!= TYPE_ENCLOSURE
)
236 pmcraid_info("configuring %x:%x:%x:%x\n",
237 scsi_dev
->host
->unique_id
,
242 if (RES_IS_GSCSI(res
->cfg_entry
)) {
243 scsi_dev
->allow_restart
= 1;
244 } else if (RES_IS_VSET(res
->cfg_entry
)) {
245 scsi_dev
->allow_restart
= 1;
246 blk_queue_rq_timeout(scsi_dev
->request_queue
,
247 PMCRAID_VSET_IO_TIMEOUT
);
248 blk_queue_max_hw_sectors(scsi_dev
->request_queue
,
249 PMCRAID_VSET_MAX_SECTORS
);
253 * We never want to report TCQ support for these types of devices.
255 if (!RES_IS_GSCSI(res
->cfg_entry
) && !RES_IS_VSET(res
->cfg_entry
))
256 scsi_dev
->tagged_supported
= 0;
262 * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
264 * @scsi_dev: scsi device struct
266 * This is called by mid-layer before removing a device. Pointer assignments
267 * done in pmcraid_slave_alloc will be reset to NULL here.
272 static void pmcraid_slave_destroy(struct scsi_device
*scsi_dev
)
274 struct pmcraid_resource_entry
*res
;
276 res
= (struct pmcraid_resource_entry
*)scsi_dev
->hostdata
;
279 res
->scsi_dev
= NULL
;
281 scsi_dev
->hostdata
= NULL
;
285 * pmcraid_change_queue_depth - Change the device's queue depth
286 * @scsi_dev: scsi device struct
287 * @depth: depth to set
292 static int pmcraid_change_queue_depth(struct scsi_device
*scsi_dev
, int depth
)
294 if (depth
> PMCRAID_MAX_CMD_PER_LUN
)
295 depth
= PMCRAID_MAX_CMD_PER_LUN
;
296 return scsi_change_queue_depth(scsi_dev
, depth
);
300 * pmcraid_init_cmdblk - initializes a command block
302 * @cmd: pointer to struct pmcraid_cmd to be initialized
303 * @index: if >=0 first time initialization; otherwise reinitialization
308 void pmcraid_init_cmdblk(struct pmcraid_cmd
*cmd
, int index
)
310 struct pmcraid_ioarcb
*ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
311 dma_addr_t dma_addr
= cmd
->ioa_cb_bus_addr
;
314 /* first time initialization (called from probe) */
316 offsetof(struct pmcraid_control_block
, ioasa
);
319 ioarcb
->response_handle
= cpu_to_le32(index
<< 2);
320 ioarcb
->ioarcb_bus_addr
= cpu_to_le64(dma_addr
);
321 ioarcb
->ioasa_bus_addr
= cpu_to_le64(dma_addr
+ ioasa_offset
);
322 ioarcb
->ioasa_len
= cpu_to_le16(sizeof(struct pmcraid_ioasa
));
324 /* re-initialization of various lengths, called once command is
327 memset(&cmd
->ioa_cb
->ioarcb
.cdb
, 0, PMCRAID_MAX_CDB_LEN
);
329 ioarcb
->request_flags0
= 0;
330 ioarcb
->request_flags1
= 0;
331 ioarcb
->cmd_timeout
= 0;
332 ioarcb
->ioarcb_bus_addr
&= (~0x1FULL
);
333 ioarcb
->ioadl_bus_addr
= 0;
334 ioarcb
->ioadl_length
= 0;
335 ioarcb
->data_transfer_length
= 0;
336 ioarcb
->add_cmd_param_length
= 0;
337 ioarcb
->add_cmd_param_offset
= 0;
338 cmd
->ioa_cb
->ioasa
.ioasc
= 0;
339 cmd
->ioa_cb
->ioasa
.residual_data_length
= 0;
343 cmd
->cmd_done
= NULL
;
344 cmd
->scsi_cmd
= NULL
;
346 cmd
->completion_req
= 0;
347 cmd
->sense_buffer
= 0;
348 cmd
->sense_buffer_dma
= 0;
350 init_timer(&cmd
->timer
);
354 * pmcraid_reinit_cmdblk - reinitialize a command block
356 * @cmd: pointer to struct pmcraid_cmd to be reinitialized
361 static void pmcraid_reinit_cmdblk(struct pmcraid_cmd
*cmd
)
363 pmcraid_init_cmdblk(cmd
, -1);
367 * pmcraid_get_free_cmd - get a free cmd block from command block pool
368 * @pinstance: adapter instance structure
371 * returns pointer to cmd block or NULL if no blocks are available
373 static struct pmcraid_cmd
*pmcraid_get_free_cmd(
374 struct pmcraid_instance
*pinstance
377 struct pmcraid_cmd
*cmd
= NULL
;
378 unsigned long lock_flags
;
380 /* free cmd block list is protected by free_pool_lock */
381 spin_lock_irqsave(&pinstance
->free_pool_lock
, lock_flags
);
383 if (!list_empty(&pinstance
->free_cmd_pool
)) {
384 cmd
= list_entry(pinstance
->free_cmd_pool
.next
,
385 struct pmcraid_cmd
, free_list
);
386 list_del(&cmd
->free_list
);
388 spin_unlock_irqrestore(&pinstance
->free_pool_lock
, lock_flags
);
390 /* Initialize the command block before giving it the caller */
392 pmcraid_reinit_cmdblk(cmd
);
397 * pmcraid_return_cmd - return a completed command block back into free pool
398 * @cmd: pointer to the command block
403 void pmcraid_return_cmd(struct pmcraid_cmd
*cmd
)
405 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
406 unsigned long lock_flags
;
408 spin_lock_irqsave(&pinstance
->free_pool_lock
, lock_flags
);
409 list_add_tail(&cmd
->free_list
, &pinstance
->free_cmd_pool
);
410 spin_unlock_irqrestore(&pinstance
->free_pool_lock
, lock_flags
);
414 * pmcraid_read_interrupts - reads IOA interrupts
416 * @pinstance: pointer to adapter instance structure
419 * interrupts read from IOA
421 static u32
pmcraid_read_interrupts(struct pmcraid_instance
*pinstance
)
423 return (pinstance
->interrupt_mode
) ?
424 ioread32(pinstance
->int_regs
.ioa_host_msix_interrupt_reg
) :
425 ioread32(pinstance
->int_regs
.ioa_host_interrupt_reg
);
429 * pmcraid_disable_interrupts - Masks and clears all specified interrupts
431 * @pinstance: pointer to per adapter instance structure
432 * @intrs: interrupts to disable
437 static void pmcraid_disable_interrupts(
438 struct pmcraid_instance
*pinstance
,
442 u32 gmask
= ioread32(pinstance
->int_regs
.global_interrupt_mask_reg
);
443 u32 nmask
= gmask
| GLOBAL_INTERRUPT_MASK
;
445 iowrite32(intrs
, pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
446 iowrite32(nmask
, pinstance
->int_regs
.global_interrupt_mask_reg
);
447 ioread32(pinstance
->int_regs
.global_interrupt_mask_reg
);
449 if (!pinstance
->interrupt_mode
) {
451 pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
452 ioread32(pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
457 * pmcraid_enable_interrupts - Enables specified interrupts
459 * @pinstance: pointer to per adapter instance structure
460 * @intr: interrupts to enable
465 static void pmcraid_enable_interrupts(
466 struct pmcraid_instance
*pinstance
,
470 u32 gmask
= ioread32(pinstance
->int_regs
.global_interrupt_mask_reg
);
471 u32 nmask
= gmask
& (~GLOBAL_INTERRUPT_MASK
);
473 iowrite32(nmask
, pinstance
->int_regs
.global_interrupt_mask_reg
);
475 if (!pinstance
->interrupt_mode
) {
477 pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
478 ioread32(pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
481 pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
482 ioread32(pinstance
->int_regs
.global_interrupt_mask_reg
),
483 ioread32(pinstance
->int_regs
.ioa_host_interrupt_mask_reg
));
487 * pmcraid_clr_trans_op - clear trans to op interrupt
489 * @pinstance: pointer to per adapter instance structure
494 static void pmcraid_clr_trans_op(
495 struct pmcraid_instance
*pinstance
498 unsigned long lock_flags
;
500 if (!pinstance
->interrupt_mode
) {
501 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL
,
502 pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
503 ioread32(pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
504 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL
,
505 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
506 ioread32(pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
509 if (pinstance
->reset_cmd
!= NULL
) {
510 del_timer(&pinstance
->reset_cmd
->timer
);
512 pinstance
->host
->host_lock
, lock_flags
);
513 pinstance
->reset_cmd
->cmd_done(pinstance
->reset_cmd
);
514 spin_unlock_irqrestore(
515 pinstance
->host
->host_lock
, lock_flags
);
520 * pmcraid_reset_type - Determine the required reset type
521 * @pinstance: pointer to adapter instance structure
523 * IOA requires hard reset if any of the following conditions is true.
524 * 1. If HRRQ valid interrupt is not masked
525 * 2. IOA reset alert doorbell is set
526 * 3. If there are any error interrupts
528 static void pmcraid_reset_type(struct pmcraid_instance
*pinstance
)
534 mask
= ioread32(pinstance
->int_regs
.ioa_host_interrupt_mask_reg
);
535 intrs
= ioread32(pinstance
->int_regs
.ioa_host_interrupt_reg
);
536 alerts
= ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
538 if ((mask
& INTRS_HRRQ_VALID
) == 0 ||
539 (alerts
& DOORBELL_IOA_RESET_ALERT
) ||
540 (intrs
& PMCRAID_ERROR_INTERRUPTS
)) {
541 pmcraid_info("IOA requires hard reset\n");
542 pinstance
->ioa_hard_reset
= 1;
545 /* If unit check is active, trigger the dump */
546 if (intrs
& INTRS_IOA_UNIT_CHECK
)
547 pinstance
->ioa_unit_check
= 1;
551 * pmcraid_bist_done - completion function for PCI BIST
552 * @cmd: pointer to reset command
557 static void pmcraid_ioa_reset(struct pmcraid_cmd
*);
559 static void pmcraid_bist_done(struct pmcraid_cmd
*cmd
)
561 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
562 unsigned long lock_flags
;
566 rc
= pci_read_config_word(pinstance
->pdev
, PCI_COMMAND
, &pci_reg
);
568 /* If PCI config space can't be accessed wait for another two secs */
569 if ((rc
!= PCIBIOS_SUCCESSFUL
|| (!(pci_reg
& PCI_COMMAND_MEMORY
))) &&
570 cmd
->time_left
> 0) {
571 pmcraid_info("BIST not complete, waiting another 2 secs\n");
572 cmd
->timer
.expires
= jiffies
+ cmd
->time_left
;
574 cmd
->timer
.data
= (unsigned long)cmd
;
575 cmd
->timer
.function
=
576 (void (*)(unsigned long))pmcraid_bist_done
;
577 add_timer(&cmd
->timer
);
580 pmcraid_info("BIST is complete, proceeding with reset\n");
581 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
582 pmcraid_ioa_reset(cmd
);
583 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
588 * pmcraid_start_bist - starts BIST
589 * @cmd: pointer to reset cmd
593 static void pmcraid_start_bist(struct pmcraid_cmd
*cmd
)
595 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
596 u32 doorbells
, intrs
;
598 /* proceed with bist and wait for 2 seconds */
599 iowrite32(DOORBELL_IOA_START_BIST
,
600 pinstance
->int_regs
.host_ioa_interrupt_reg
);
601 doorbells
= ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
602 intrs
= ioread32(pinstance
->int_regs
.ioa_host_interrupt_reg
);
603 pmcraid_info("doorbells after start bist: %x intrs: %x\n",
606 cmd
->time_left
= msecs_to_jiffies(PMCRAID_BIST_TIMEOUT
);
607 cmd
->timer
.data
= (unsigned long)cmd
;
608 cmd
->timer
.expires
= jiffies
+ msecs_to_jiffies(PMCRAID_BIST_TIMEOUT
);
609 cmd
->timer
.function
= (void (*)(unsigned long))pmcraid_bist_done
;
610 add_timer(&cmd
->timer
);
614 * pmcraid_reset_alert_done - completion routine for reset_alert
615 * @cmd: pointer to command block used in reset sequence
619 static void pmcraid_reset_alert_done(struct pmcraid_cmd
*cmd
)
621 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
622 u32 status
= ioread32(pinstance
->ioa_status
);
623 unsigned long lock_flags
;
625 /* if the critical operation in progress bit is set or the wait times
626 * out, invoke reset engine to proceed with hard reset. If there is
627 * some more time to wait, restart the timer
629 if (((status
& INTRS_CRITICAL_OP_IN_PROGRESS
) == 0) ||
630 cmd
->time_left
<= 0) {
631 pmcraid_info("critical op is reset proceeding with reset\n");
632 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
633 pmcraid_ioa_reset(cmd
);
634 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
636 pmcraid_info("critical op is not yet reset waiting again\n");
637 /* restart timer if some more time is available to wait */
638 cmd
->time_left
-= PMCRAID_CHECK_FOR_RESET_TIMEOUT
;
639 cmd
->timer
.data
= (unsigned long)cmd
;
640 cmd
->timer
.expires
= jiffies
+ PMCRAID_CHECK_FOR_RESET_TIMEOUT
;
641 cmd
->timer
.function
=
642 (void (*)(unsigned long))pmcraid_reset_alert_done
;
643 add_timer(&cmd
->timer
);
648 * pmcraid_reset_alert - alerts IOA for a possible reset
649 * @cmd : command block to be used for reset sequence.
652 * returns 0 if pci config-space is accessible and RESET_DOORBELL is
653 * successfully written to IOA. Returns non-zero in case pci_config_space
656 static void pmcraid_notify_ioastate(struct pmcraid_instance
*, u32
);
657 static void pmcraid_reset_alert(struct pmcraid_cmd
*cmd
)
659 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
664 /* If we are able to access IOA PCI config space, alert IOA that we are
665 * going to reset it soon. This enables IOA to preserv persistent error
666 * data if any. In case memory space is not accessible, proceed with
669 rc
= pci_read_config_word(pinstance
->pdev
, PCI_COMMAND
, &pci_reg
);
670 if ((rc
== PCIBIOS_SUCCESSFUL
) && (pci_reg
& PCI_COMMAND_MEMORY
)) {
672 /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
673 * reset IOA doesn't generate any interrupts when CRITICAL
674 * OPERATION bit is reset. A timer is started to wait for this
677 cmd
->time_left
= PMCRAID_RESET_TIMEOUT
;
678 cmd
->timer
.data
= (unsigned long)cmd
;
679 cmd
->timer
.expires
= jiffies
+ PMCRAID_CHECK_FOR_RESET_TIMEOUT
;
680 cmd
->timer
.function
=
681 (void (*)(unsigned long))pmcraid_reset_alert_done
;
682 add_timer(&cmd
->timer
);
684 iowrite32(DOORBELL_IOA_RESET_ALERT
,
685 pinstance
->int_regs
.host_ioa_interrupt_reg
);
687 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
688 pmcraid_info("doorbells after reset alert: %x\n", doorbells
);
690 pmcraid_info("PCI config is not accessible starting BIST\n");
691 pinstance
->ioa_state
= IOA_STATE_IN_HARD_RESET
;
692 pmcraid_start_bist(cmd
);
697 * pmcraid_timeout_handler - Timeout handler for internally generated ops
699 * @cmd : pointer to command structure, that got timedout
701 * This function blocks host requests and initiates an adapter reset.
706 static void pmcraid_timeout_handler(struct pmcraid_cmd
*cmd
)
708 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
709 unsigned long lock_flags
;
711 dev_info(&pinstance
->pdev
->dev
,
712 "Adapter being reset due to cmd(CDB[0] = %x) timeout\n",
713 cmd
->ioa_cb
->ioarcb
.cdb
[0]);
715 /* Command timeouts result in hard reset sequence. The command that got
716 * timed out may be the one used as part of reset sequence. In this
717 * case restart reset sequence using the same command block even if
718 * reset is in progress. Otherwise fail this command and get a free
719 * command block to restart the reset sequence.
721 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
722 if (!pinstance
->ioa_reset_in_progress
) {
723 pinstance
->ioa_reset_attempts
= 0;
724 cmd
= pmcraid_get_free_cmd(pinstance
);
726 /* If we are out of command blocks, just return here itself.
727 * Some other command's timeout handler can do the reset job
730 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
732 pmcraid_err("no free cmnd block for timeout handler\n");
736 pinstance
->reset_cmd
= cmd
;
737 pinstance
->ioa_reset_in_progress
= 1;
739 pmcraid_info("reset is already in progress\n");
741 if (pinstance
->reset_cmd
!= cmd
) {
742 /* This command should have been given to IOA, this
743 * command will be completed by fail_outstanding_cmds
746 pmcraid_err("cmd is pending but reset in progress\n");
749 /* If this command was being used as part of the reset
750 * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
751 * causes fail_outstanding_commands not to return the command
752 * block back to free pool
754 if (cmd
== pinstance
->reset_cmd
)
755 cmd
->cmd_done
= pmcraid_ioa_reset
;
758 /* Notify apps of important IOA bringup/bringdown sequences */
759 if (pinstance
->scn
.ioa_state
!= PMC_DEVICE_EVENT_RESET_START
&&
760 pinstance
->scn
.ioa_state
!= PMC_DEVICE_EVENT_SHUTDOWN_START
)
761 pmcraid_notify_ioastate(pinstance
,
762 PMC_DEVICE_EVENT_RESET_START
);
764 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
765 scsi_block_requests(pinstance
->host
);
766 pmcraid_reset_alert(cmd
);
767 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
771 * pmcraid_internal_done - completion routine for internally generated cmds
773 * @cmd: command that got response from IOA
778 static void pmcraid_internal_done(struct pmcraid_cmd
*cmd
)
780 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
781 cmd
->ioa_cb
->ioarcb
.cdb
[0],
782 le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
));
784 /* Some of the internal commands are sent with callers blocking for the
785 * response. Same will be indicated as part of cmd->completion_req
786 * field. Response path needs to wake up any waiters waiting for cmd
787 * completion if this flag is set.
789 if (cmd
->completion_req
) {
790 cmd
->completion_req
= 0;
791 complete(&cmd
->wait_for_completion
);
794 /* most of the internal commands are completed by caller itself, so
795 * no need to return the command block back to free pool until we are
796 * required to do so (e.g once done with initialization).
800 pmcraid_return_cmd(cmd
);
805 * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
807 * @cmd: command that got response from IOA
809 * This routine is called after driver re-reads configuration table due to a
810 * lost CCN. It returns the command block back to free pool and schedules
811 * worker thread to add/delete devices into the system.
816 static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd
*cmd
)
818 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
819 cmd
->ioa_cb
->ioarcb
.cdb
[0],
820 le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
));
824 pmcraid_return_cmd(cmd
);
826 pmcraid_info("scheduling worker for config table reinitialization\n");
827 schedule_work(&cmd
->drv_inst
->worker_q
);
831 * pmcraid_erp_done - Process completion of SCSI error response from device
832 * @cmd: pmcraid_command
834 * This function copies the sense buffer into the scsi_cmd struct and completes
835 * scsi_cmd by calling scsi_done function.
840 static void pmcraid_erp_done(struct pmcraid_cmd
*cmd
)
842 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
843 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
844 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
846 if (PMCRAID_IOASC_SENSE_KEY(ioasc
) > 0) {
847 scsi_cmd
->result
|= (DID_ERROR
<< 16);
848 scmd_printk(KERN_INFO
, scsi_cmd
,
849 "command CDB[0] = %x failed with IOASC: 0x%08X\n",
850 cmd
->ioa_cb
->ioarcb
.cdb
[0], ioasc
);
853 /* if we had allocated sense buffers for request sense, copy the sense
854 * release the buffers
856 if (cmd
->sense_buffer
!= NULL
) {
857 memcpy(scsi_cmd
->sense_buffer
,
859 SCSI_SENSE_BUFFERSIZE
);
860 pci_free_consistent(pinstance
->pdev
,
861 SCSI_SENSE_BUFFERSIZE
,
862 cmd
->sense_buffer
, cmd
->sense_buffer_dma
);
863 cmd
->sense_buffer
= NULL
;
864 cmd
->sense_buffer_dma
= 0;
867 scsi_dma_unmap(scsi_cmd
);
868 pmcraid_return_cmd(cmd
);
869 scsi_cmd
->scsi_done(scsi_cmd
);
873 * pmcraid_fire_command - sends an IOA command to adapter
875 * This function adds the given block into pending command list
876 * and returns without waiting
878 * @cmd : command to be sent to the device
883 static void _pmcraid_fire_command(struct pmcraid_cmd
*cmd
)
885 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
886 unsigned long lock_flags
;
888 /* Add this command block to pending cmd pool. We do this prior to
889 * writting IOARCB to ioarrin because IOA might complete the command
890 * by the time we are about to add it to the list. Response handler
891 * (isr/tasklet) looks for cmd block in the pending pending list.
893 spin_lock_irqsave(&pinstance
->pending_pool_lock
, lock_flags
);
894 list_add_tail(&cmd
->free_list
, &pinstance
->pending_cmd_pool
);
895 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
, lock_flags
);
896 atomic_inc(&pinstance
->outstanding_cmds
);
898 /* driver writes lower 32-bit value of IOARCB address only */
900 iowrite32(le32_to_cpu(cmd
->ioa_cb
->ioarcb
.ioarcb_bus_addr
),
905 * pmcraid_send_cmd - fires a command to IOA
907 * This function also sets up timeout function, and command completion
910 * @cmd: pointer to the command block to be fired to IOA
911 * @cmd_done: command completion function, called once IOA responds
912 * @timeout: timeout to wait for this command completion
913 * @timeout_func: timeout handler
918 static void pmcraid_send_cmd(
919 struct pmcraid_cmd
*cmd
,
920 void (*cmd_done
) (struct pmcraid_cmd
*),
921 unsigned long timeout
,
922 void (*timeout_func
) (struct pmcraid_cmd
*)
925 /* initialize done function */
926 cmd
->cmd_done
= cmd_done
;
929 /* setup timeout handler */
930 cmd
->timer
.data
= (unsigned long)cmd
;
931 cmd
->timer
.expires
= jiffies
+ timeout
;
932 cmd
->timer
.function
= (void (*)(unsigned long))timeout_func
;
933 add_timer(&cmd
->timer
);
936 /* fire the command to IOA */
937 _pmcraid_fire_command(cmd
);
941 * pmcraid_ioa_shutdown_done - completion function for IOA shutdown command
942 * @cmd: pointer to the command block used for sending IOA shutdown command
947 static void pmcraid_ioa_shutdown_done(struct pmcraid_cmd
*cmd
)
949 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
950 unsigned long lock_flags
;
952 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
953 pmcraid_ioa_reset(cmd
);
954 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
958 * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
960 * @cmd: pointer to the command block used as part of reset sequence
965 static void pmcraid_ioa_shutdown(struct pmcraid_cmd
*cmd
)
967 pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
968 cmd
->ioa_cb
->ioarcb
.cdb
[0],
969 le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
));
971 /* Note that commands sent during reset require next command to be sent
972 * to IOA. Hence reinit the done function as well as timeout function
974 pmcraid_reinit_cmdblk(cmd
);
975 cmd
->ioa_cb
->ioarcb
.request_type
= REQ_TYPE_IOACMD
;
976 cmd
->ioa_cb
->ioarcb
.resource_handle
=
977 cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
978 cmd
->ioa_cb
->ioarcb
.cdb
[0] = PMCRAID_IOA_SHUTDOWN
;
979 cmd
->ioa_cb
->ioarcb
.cdb
[1] = PMCRAID_SHUTDOWN_NORMAL
;
981 /* fire shutdown command to hardware. */
982 pmcraid_info("firing normal shutdown command (%d) to IOA\n",
983 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
));
985 pmcraid_notify_ioastate(cmd
->drv_inst
, PMC_DEVICE_EVENT_SHUTDOWN_START
);
987 pmcraid_send_cmd(cmd
, pmcraid_ioa_shutdown_done
,
988 PMCRAID_SHUTDOWN_TIMEOUT
,
989 pmcraid_timeout_handler
);
993 * pmcraid_get_fwversion_done - completion function for get_fwversion
995 * @cmd: pointer to command block used to send INQUIRY command
1000 static void pmcraid_querycfg(struct pmcraid_cmd
*);
1002 static void pmcraid_get_fwversion_done(struct pmcraid_cmd
*cmd
)
1004 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1005 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
1006 unsigned long lock_flags
;
1008 /* configuration table entry size depends on firmware version. If fw
1009 * version is not known, it is not possible to interpret IOA config
1013 pmcraid_err("IOA Inquiry failed with %x\n", ioasc
);
1014 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
1015 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
1016 pmcraid_reset_alert(cmd
);
1017 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
1019 pmcraid_querycfg(cmd
);
1024 * pmcraid_get_fwversion - reads firmware version information
1026 * @cmd: pointer to command block used to send INQUIRY command
1031 static void pmcraid_get_fwversion(struct pmcraid_cmd
*cmd
)
1033 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
1034 struct pmcraid_ioadl_desc
*ioadl
= ioarcb
->add_data
.u
.ioadl
;
1035 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1036 u16 data_size
= sizeof(struct pmcraid_inquiry_data
);
1038 pmcraid_reinit_cmdblk(cmd
);
1039 ioarcb
->request_type
= REQ_TYPE_SCSI
;
1040 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
1041 ioarcb
->cdb
[0] = INQUIRY
;
1043 ioarcb
->cdb
[2] = 0xD0;
1044 ioarcb
->cdb
[3] = (data_size
>> 8) & 0xFF;
1045 ioarcb
->cdb
[4] = data_size
& 0xFF;
1047 /* Since entire inquiry data it can be part of IOARCB itself
1049 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
1050 offsetof(struct pmcraid_ioarcb
,
1051 add_data
.u
.ioadl
[0]));
1052 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
1053 ioarcb
->ioarcb_bus_addr
&= ~(0x1FULL
);
1055 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
1056 ioarcb
->data_transfer_length
= cpu_to_le32(data_size
);
1057 ioadl
= &(ioarcb
->add_data
.u
.ioadl
[0]);
1058 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
1059 ioadl
->address
= cpu_to_le64(pinstance
->inq_data_baddr
);
1060 ioadl
->data_len
= cpu_to_le32(data_size
);
1062 pmcraid_send_cmd(cmd
, pmcraid_get_fwversion_done
,
1063 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
1067 * pmcraid_identify_hrrq - registers host rrq buffers with IOA
1068 * @cmd: pointer to command block to be used for identify hrrq
1073 static void pmcraid_identify_hrrq(struct pmcraid_cmd
*cmd
)
1075 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1076 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
1077 int index
= cmd
->hrrq_index
;
1078 __be64 hrrq_addr
= cpu_to_be64(pinstance
->hrrq_start_bus_addr
[index
]);
1079 u32 hrrq_size
= cpu_to_be32(sizeof(u32
) * PMCRAID_MAX_CMD
);
1080 void (*done_function
)(struct pmcraid_cmd
*);
1082 pmcraid_reinit_cmdblk(cmd
);
1083 cmd
->hrrq_index
= index
+ 1;
1085 if (cmd
->hrrq_index
< pinstance
->num_hrrq
) {
1086 done_function
= pmcraid_identify_hrrq
;
1088 cmd
->hrrq_index
= 0;
1089 done_function
= pmcraid_get_fwversion
;
1092 /* Initialize ioarcb */
1093 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
1094 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
1096 /* initialize the hrrq number where IOA will respond to this command */
1097 ioarcb
->hrrq_id
= index
;
1098 ioarcb
->cdb
[0] = PMCRAID_IDENTIFY_HRRQ
;
1099 ioarcb
->cdb
[1] = index
;
1101 /* IOA expects 64-bit pci address to be written in B.E format
1102 * (i.e cdb[2]=MSByte..cdb[9]=LSB.
1104 pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb:index => %llx:%llx:%x\n",
1105 hrrq_addr
, ioarcb
->ioarcb_bus_addr
, index
);
1107 memcpy(&(ioarcb
->cdb
[2]), &hrrq_addr
, sizeof(hrrq_addr
));
1108 memcpy(&(ioarcb
->cdb
[10]), &hrrq_size
, sizeof(hrrq_size
));
1110 /* Subsequent commands require HRRQ identification to be successful.
1111 * Note that this gets called even during reset from SCSI mid-layer
1114 pmcraid_send_cmd(cmd
, done_function
,
1115 PMCRAID_INTERNAL_TIMEOUT
,
1116 pmcraid_timeout_handler
);
1119 static void pmcraid_process_ccn(struct pmcraid_cmd
*cmd
);
1120 static void pmcraid_process_ldn(struct pmcraid_cmd
*cmd
);
1123 * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
1125 * @cmd: initialized command block pointer
1130 static void pmcraid_send_hcam_cmd(struct pmcraid_cmd
*cmd
)
1132 if (cmd
->ioa_cb
->ioarcb
.cdb
[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE
)
1133 atomic_set(&(cmd
->drv_inst
->ccn
.ignore
), 0);
1135 atomic_set(&(cmd
->drv_inst
->ldn
.ignore
), 0);
1137 pmcraid_send_cmd(cmd
, cmd
->cmd_done
, 0, NULL
);
1141 * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
1143 * @pinstance: pointer to adapter instance structure
1147 * pointer to initialized pmcraid_cmd structure or NULL
1149 static struct pmcraid_cmd
*pmcraid_init_hcam
1151 struct pmcraid_instance
*pinstance
,
1155 struct pmcraid_cmd
*cmd
;
1156 struct pmcraid_ioarcb
*ioarcb
;
1157 struct pmcraid_ioadl_desc
*ioadl
;
1158 struct pmcraid_hostrcb
*hcam
;
1159 void (*cmd_done
) (struct pmcraid_cmd
*);
1163 cmd
= pmcraid_get_free_cmd(pinstance
);
1166 pmcraid_err("no free command blocks for hcam\n");
1170 if (type
== PMCRAID_HCAM_CODE_CONFIG_CHANGE
) {
1171 rcb_size
= sizeof(struct pmcraid_hcam_ccn_ext
);
1172 cmd_done
= pmcraid_process_ccn
;
1173 dma
= pinstance
->ccn
.baddr
+ PMCRAID_AEN_HDR_SIZE
;
1174 hcam
= &pinstance
->ccn
;
1176 rcb_size
= sizeof(struct pmcraid_hcam_ldn
);
1177 cmd_done
= pmcraid_process_ldn
;
1178 dma
= pinstance
->ldn
.baddr
+ PMCRAID_AEN_HDR_SIZE
;
1179 hcam
= &pinstance
->ldn
;
1182 /* initialize command pointer used for HCAM registration */
1185 ioarcb
= &cmd
->ioa_cb
->ioarcb
;
1186 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
1187 offsetof(struct pmcraid_ioarcb
,
1188 add_data
.u
.ioadl
[0]));
1189 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
1190 ioadl
= ioarcb
->add_data
.u
.ioadl
;
1192 /* Initialize ioarcb */
1193 ioarcb
->request_type
= REQ_TYPE_HCAM
;
1194 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
1195 ioarcb
->cdb
[0] = PMCRAID_HOST_CONTROLLED_ASYNC
;
1196 ioarcb
->cdb
[1] = type
;
1197 ioarcb
->cdb
[7] = (rcb_size
>> 8) & 0xFF;
1198 ioarcb
->cdb
[8] = (rcb_size
) & 0xFF;
1200 ioarcb
->data_transfer_length
= cpu_to_le32(rcb_size
);
1202 ioadl
[0].flags
|= IOADL_FLAGS_READ_LAST
;
1203 ioadl
[0].data_len
= cpu_to_le32(rcb_size
);
1204 ioadl
[0].address
= cpu_to_le32(dma
);
1206 cmd
->cmd_done
= cmd_done
;
1211 * pmcraid_send_hcam - Send an HCAM to IOA
1212 * @pinstance: ioa config struct
1215 * This function will send a Host Controlled Async command to IOA.
1220 static void pmcraid_send_hcam(struct pmcraid_instance
*pinstance
, u8 type
)
1222 struct pmcraid_cmd
*cmd
= pmcraid_init_hcam(pinstance
, type
);
1223 pmcraid_send_hcam_cmd(cmd
);
1228 * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
1230 * @cmd: pointer to cmd that is used as cancelling command
1231 * @cmd_to_cancel: pointer to the command that needs to be cancelled
1233 static void pmcraid_prepare_cancel_cmd(
1234 struct pmcraid_cmd
*cmd
,
1235 struct pmcraid_cmd
*cmd_to_cancel
1238 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
1239 __be64 ioarcb_addr
= cmd_to_cancel
->ioa_cb
->ioarcb
.ioarcb_bus_addr
;
1241 /* Get the resource handle to where the command to be aborted has been
1244 ioarcb
->resource_handle
= cmd_to_cancel
->ioa_cb
->ioarcb
.resource_handle
;
1245 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
1246 memset(ioarcb
->cdb
, 0, PMCRAID_MAX_CDB_LEN
);
1247 ioarcb
->cdb
[0] = PMCRAID_ABORT_CMD
;
1249 /* IOARCB address of the command to be cancelled is given in
1250 * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
1251 * IOARCB address are not masked.
1253 ioarcb_addr
= cpu_to_be64(ioarcb_addr
);
1254 memcpy(&(ioarcb
->cdb
[2]), &ioarcb_addr
, sizeof(ioarcb_addr
));
1258 * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
1260 * @cmd: command to be used as cancelling command
1262 * @cmd_done: op done function for the cancelling command
1264 static void pmcraid_cancel_hcam(
1265 struct pmcraid_cmd
*cmd
,
1267 void (*cmd_done
) (struct pmcraid_cmd
*)
1270 struct pmcraid_instance
*pinstance
;
1271 struct pmcraid_hostrcb
*hcam
;
1273 pinstance
= cmd
->drv_inst
;
1274 hcam
= (type
== PMCRAID_HCAM_CODE_LOG_DATA
) ?
1275 &pinstance
->ldn
: &pinstance
->ccn
;
1277 /* prepare for cancelling previous hcam command. If the HCAM is
1278 * currently not pending with IOA, we would have hcam->cmd as non-null
1280 if (hcam
->cmd
== NULL
)
1283 pmcraid_prepare_cancel_cmd(cmd
, hcam
->cmd
);
1285 /* writing to IOARRIN must be protected by host_lock, as mid-layer
1286 * schedule queuecommand while we are doing this
1288 pmcraid_send_cmd(cmd
, cmd_done
,
1289 PMCRAID_INTERNAL_TIMEOUT
,
1290 pmcraid_timeout_handler
);
1294 * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
1296 * @cmd: command block to be used for cancelling the HCAM
1298 static void pmcraid_cancel_ccn(struct pmcraid_cmd
*cmd
)
1300 pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
1301 cmd
->ioa_cb
->ioarcb
.cdb
[0],
1302 le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
));
1304 pmcraid_reinit_cmdblk(cmd
);
1306 pmcraid_cancel_hcam(cmd
,
1307 PMCRAID_HCAM_CODE_CONFIG_CHANGE
,
1308 pmcraid_ioa_shutdown
);
1312 * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
1314 * @cmd: command block to be used for cancelling the HCAM
1316 static void pmcraid_cancel_ldn(struct pmcraid_cmd
*cmd
)
1318 pmcraid_cancel_hcam(cmd
,
1319 PMCRAID_HCAM_CODE_LOG_DATA
,
1320 pmcraid_cancel_ccn
);
1324 * pmcraid_expose_resource - check if the resource can be exposed to OS
1326 * @fw_version: firmware version code
1327 * @cfgte: pointer to configuration table entry of the resource
1330 * true if resource can be added to midlayer, false(0) otherwise
1332 static int pmcraid_expose_resource(u16 fw_version
,
1333 struct pmcraid_config_table_entry
*cfgte
)
1337 if (cfgte
->resource_type
== RES_TYPE_VSET
) {
1338 if (fw_version
<= PMCRAID_FW_VERSION_1
)
1339 retval
= ((cfgte
->unique_flags1
& 0x80) == 0);
1341 retval
= ((cfgte
->unique_flags0
& 0x80) == 0 &&
1342 (cfgte
->unique_flags1
& 0x80) == 0);
1344 } else if (cfgte
->resource_type
== RES_TYPE_GSCSI
)
1345 retval
= (RES_BUS(cfgte
->resource_address
) !=
1346 PMCRAID_VIRTUAL_ENCL_BUS_ID
);
1350 /* attributes supported by pmcraid_event_family */
1352 PMCRAID_AEN_ATTR_UNSPEC
,
1353 PMCRAID_AEN_ATTR_EVENT
,
1354 __PMCRAID_AEN_ATTR_MAX
,
1356 #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
1358 /* commands supported by pmcraid_event_family */
1360 PMCRAID_AEN_CMD_UNSPEC
,
1361 PMCRAID_AEN_CMD_EVENT
,
1362 __PMCRAID_AEN_CMD_MAX
,
1364 #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
1366 static struct genl_multicast_group pmcraid_mcgrps
[] = {
1367 { .name
= "events", /* not really used - see ID discussion below */ },
1370 static struct genl_family pmcraid_event_family
= {
1372 * Due to prior multicast group abuse (the code having assumed that
1373 * the family ID can be used as a multicast group ID) we need to
1374 * statically allocate a family (and thus group) ID.
1376 .id
= GENL_ID_PMCRAID
,
1379 .maxattr
= PMCRAID_AEN_ATTR_MAX
,
1380 .mcgrps
= pmcraid_mcgrps
,
1381 .n_mcgrps
= ARRAY_SIZE(pmcraid_mcgrps
),
1385 * pmcraid_netlink_init - registers pmcraid_event_family
1388 * 0 if the pmcraid_event_family is successfully registered
1389 * with netlink generic, non-zero otherwise
1391 static int pmcraid_netlink_init(void)
1395 result
= genl_register_family(&pmcraid_event_family
);
1400 pmcraid_info("registered NETLINK GENERIC group: %d\n",
1401 pmcraid_event_family
.id
);
1407 * pmcraid_netlink_release - unregisters pmcraid_event_family
1412 static void pmcraid_netlink_release(void)
1414 genl_unregister_family(&pmcraid_event_family
);
1418 * pmcraid_notify_aen - sends event msg to user space application
1419 * @pinstance: pointer to adapter instance structure
1423 * 0 if success, error value in case of any failure.
1425 static int pmcraid_notify_aen(
1426 struct pmcraid_instance
*pinstance
,
1427 struct pmcraid_aen_msg
*aen_msg
,
1431 struct sk_buff
*skb
;
1433 u32 total_size
, nla_genl_hdr_total_size
;
1436 aen_msg
->hostno
= (pinstance
->host
->unique_id
<< 16 |
1437 MINOR(pinstance
->cdev
.dev
));
1438 aen_msg
->length
= data_size
;
1440 data_size
+= sizeof(*aen_msg
);
1442 total_size
= nla_total_size(data_size
);
1443 /* Add GENL_HDR to total_size */
1444 nla_genl_hdr_total_size
=
1445 (total_size
+ (GENL_HDRLEN
+
1446 ((struct genl_family
*)&pmcraid_event_family
)->hdrsize
)
1448 skb
= genlmsg_new(nla_genl_hdr_total_size
, GFP_ATOMIC
);
1452 pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
1457 /* add the genetlink message header */
1458 msg_header
= genlmsg_put(skb
, 0, 0,
1459 &pmcraid_event_family
, 0,
1460 PMCRAID_AEN_CMD_EVENT
);
1462 pmcraid_err("failed to copy command details\n");
1467 result
= nla_put(skb
, PMCRAID_AEN_ATTR_EVENT
, data_size
, aen_msg
);
1470 pmcraid_err("failed to copy AEN attribute data\n");
1475 /* send genetlink multicast message to notify appplications */
1476 result
= genlmsg_end(skb
, msg_header
);
1479 pmcraid_err("genlmsg_end failed\n");
1484 result
= genlmsg_multicast(&pmcraid_event_family
, skb
,
1487 /* If there are no listeners, genlmsg_multicast may return non-zero
1491 pmcraid_info("error (%x) sending aen event message\n", result
);
1496 * pmcraid_notify_ccn - notifies about CCN event msg to user space
1497 * @pinstance: pointer adapter instance structure
1500 * 0 if success, error value in case of any failure
1502 static int pmcraid_notify_ccn(struct pmcraid_instance
*pinstance
)
1504 return pmcraid_notify_aen(pinstance
,
1506 pinstance
->ccn
.hcam
->data_len
+
1507 sizeof(struct pmcraid_hcam_hdr
));
1511 * pmcraid_notify_ldn - notifies about CCN event msg to user space
1512 * @pinstance: pointer adapter instance structure
1515 * 0 if success, error value in case of any failure
1517 static int pmcraid_notify_ldn(struct pmcraid_instance
*pinstance
)
1519 return pmcraid_notify_aen(pinstance
,
1521 pinstance
->ldn
.hcam
->data_len
+
1522 sizeof(struct pmcraid_hcam_hdr
));
1526 * pmcraid_notify_ioastate - sends IOA state event msg to user space
1527 * @pinstance: pointer adapter instance structure
1528 * @evt: controller state event to be sent
1531 * 0 if success, error value in case of any failure
1533 static void pmcraid_notify_ioastate(struct pmcraid_instance
*pinstance
, u32 evt
)
1535 pinstance
->scn
.ioa_state
= evt
;
1536 pmcraid_notify_aen(pinstance
,
1537 &pinstance
->scn
.msg
,
1542 * pmcraid_handle_config_change - Handle a config change from the adapter
1543 * @pinstance: pointer to per adapter instance structure
1549 static void pmcraid_handle_config_change(struct pmcraid_instance
*pinstance
)
1551 struct pmcraid_config_table_entry
*cfg_entry
;
1552 struct pmcraid_hcam_ccn
*ccn_hcam
;
1553 struct pmcraid_cmd
*cmd
;
1554 struct pmcraid_cmd
*cfgcmd
;
1555 struct pmcraid_resource_entry
*res
= NULL
;
1556 unsigned long lock_flags
;
1557 unsigned long host_lock_flags
;
1559 u32 hidden_entry
= 0;
1563 ccn_hcam
= (struct pmcraid_hcam_ccn
*)pinstance
->ccn
.hcam
;
1564 cfg_entry
= &ccn_hcam
->cfg_entry
;
1565 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
1567 pmcraid_info("CCN(%x): %x timestamp: %llx type: %x lost: %x flags: %x \
1568 res: %x:%x:%x:%x\n",
1569 pinstance
->ccn
.hcam
->ilid
,
1570 pinstance
->ccn
.hcam
->op_code
,
1571 ((pinstance
->ccn
.hcam
->timestamp1
) |
1572 ((pinstance
->ccn
.hcam
->timestamp2
& 0xffffffffLL
) << 32)),
1573 pinstance
->ccn
.hcam
->notification_type
,
1574 pinstance
->ccn
.hcam
->notification_lost
,
1575 pinstance
->ccn
.hcam
->flags
,
1576 pinstance
->host
->unique_id
,
1577 RES_IS_VSET(*cfg_entry
) ? PMCRAID_VSET_BUS_ID
:
1578 (RES_IS_GSCSI(*cfg_entry
) ? PMCRAID_PHYS_BUS_ID
:
1579 RES_BUS(cfg_entry
->resource_address
)),
1580 RES_IS_VSET(*cfg_entry
) ?
1581 (fw_version
<= PMCRAID_FW_VERSION_1
?
1582 cfg_entry
->unique_flags1
:
1583 cfg_entry
->array_id
& 0xFF) :
1584 RES_TARGET(cfg_entry
->resource_address
),
1585 RES_LUN(cfg_entry
->resource_address
));
1588 /* If this HCAM indicates a lost notification, read the config table */
1589 if (pinstance
->ccn
.hcam
->notification_lost
) {
1590 cfgcmd
= pmcraid_get_free_cmd(pinstance
);
1592 pmcraid_info("lost CCN, reading config table\b");
1593 pinstance
->reinit_cfg_table
= 1;
1594 pmcraid_querycfg(cfgcmd
);
1596 pmcraid_err("lost CCN, no free cmd for querycfg\n");
1598 goto out_notify_apps
;
1601 /* If this resource is not going to be added to mid-layer, just notify
1602 * applications and return. If this notification is about hiding a VSET
1603 * resource, check if it was exposed already.
1605 if (pinstance
->ccn
.hcam
->notification_type
==
1606 NOTIFICATION_TYPE_ENTRY_CHANGED
&&
1607 cfg_entry
->resource_type
== RES_TYPE_VSET
) {
1609 if (fw_version
<= PMCRAID_FW_VERSION_1
)
1610 hidden_entry
= (cfg_entry
->unique_flags1
& 0x80) != 0;
1612 hidden_entry
= (cfg_entry
->unique_flags1
& 0x80) != 0;
1614 } else if (!pmcraid_expose_resource(fw_version
, cfg_entry
)) {
1615 goto out_notify_apps
;
1618 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
1619 list_for_each_entry(res
, &pinstance
->used_res_q
, queue
) {
1620 rc
= memcmp(&res
->cfg_entry
.resource_address
,
1621 &cfg_entry
->resource_address
,
1622 sizeof(cfg_entry
->resource_address
));
1632 spin_unlock_irqrestore(&pinstance
->resource_lock
,
1634 goto out_notify_apps
;
1637 /* If there are more number of resources than what driver can
1638 * manage, do not notify the applications about the CCN. Just
1639 * ignore this notifications and re-register the same HCAM
1641 if (list_empty(&pinstance
->free_res_q
)) {
1642 spin_unlock_irqrestore(&pinstance
->resource_lock
,
1644 pmcraid_err("too many resources attached\n");
1645 spin_lock_irqsave(pinstance
->host
->host_lock
,
1647 pmcraid_send_hcam(pinstance
,
1648 PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1649 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
1654 res
= list_entry(pinstance
->free_res_q
.next
,
1655 struct pmcraid_resource_entry
, queue
);
1657 list_del(&res
->queue
);
1658 res
->scsi_dev
= NULL
;
1659 res
->reset_progress
= 0;
1660 list_add_tail(&res
->queue
, &pinstance
->used_res_q
);
1663 memcpy(&res
->cfg_entry
, cfg_entry
, pinstance
->config_table_entry_size
);
1665 if (pinstance
->ccn
.hcam
->notification_type
==
1666 NOTIFICATION_TYPE_ENTRY_DELETED
|| hidden_entry
) {
1667 if (res
->scsi_dev
) {
1668 if (fw_version
<= PMCRAID_FW_VERSION_1
)
1669 res
->cfg_entry
.unique_flags1
&= 0x7F;
1671 res
->cfg_entry
.array_id
&= 0xFF;
1672 res
->change_detected
= RES_CHANGE_DEL
;
1673 res
->cfg_entry
.resource_handle
=
1674 PMCRAID_INVALID_RES_HANDLE
;
1675 schedule_work(&pinstance
->worker_q
);
1677 /* This may be one of the non-exposed resources */
1678 list_move_tail(&res
->queue
, &pinstance
->free_res_q
);
1680 } else if (!res
->scsi_dev
) {
1681 res
->change_detected
= RES_CHANGE_ADD
;
1682 schedule_work(&pinstance
->worker_q
);
1684 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
1688 /* Notify configuration changes to registered applications.*/
1689 if (!pmcraid_disable_aen
)
1690 pmcraid_notify_ccn(pinstance
);
1692 cmd
= pmcraid_init_hcam(pinstance
, PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1694 pmcraid_send_hcam_cmd(cmd
);
1698 * pmcraid_get_error_info - return error string for an ioasc
1699 * @ioasc: ioasc code
1703 static struct pmcraid_ioasc_error
*pmcraid_get_error_info(u32 ioasc
)
1706 for (i
= 0; i
< ARRAY_SIZE(pmcraid_ioasc_error_table
); i
++) {
1707 if (pmcraid_ioasc_error_table
[i
].ioasc_code
== ioasc
)
1708 return &pmcraid_ioasc_error_table
[i
];
1714 * pmcraid_ioasc_logger - log IOASC information based user-settings
1715 * @ioasc: ioasc code
1716 * @cmd: pointer to command that resulted in 'ioasc'
1718 void pmcraid_ioasc_logger(u32 ioasc
, struct pmcraid_cmd
*cmd
)
1720 struct pmcraid_ioasc_error
*error_info
= pmcraid_get_error_info(ioasc
);
1722 if (error_info
== NULL
||
1723 cmd
->drv_inst
->current_log_level
< error_info
->log_level
)
1726 /* log the error string */
1727 pmcraid_err("cmd [%x] for resource %x failed with %x(%s)\n",
1728 cmd
->ioa_cb
->ioarcb
.cdb
[0],
1729 cmd
->ioa_cb
->ioarcb
.resource_handle
,
1730 le32_to_cpu(ioasc
), error_info
->error_string
);
1734 * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
1736 * @pinstance: pointer to per adapter instance structure
1741 static void pmcraid_handle_error_log(struct pmcraid_instance
*pinstance
)
1743 struct pmcraid_hcam_ldn
*hcam_ldn
;
1746 hcam_ldn
= (struct pmcraid_hcam_ldn
*)pinstance
->ldn
.hcam
;
1749 ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
1750 pinstance
->ldn
.hcam
->ilid
,
1751 pinstance
->ldn
.hcam
->op_code
,
1752 pinstance
->ldn
.hcam
->notification_type
,
1753 pinstance
->ldn
.hcam
->notification_lost
,
1754 pinstance
->ldn
.hcam
->flags
,
1755 pinstance
->ldn
.hcam
->overlay_id
);
1757 /* log only the errors, no need to log informational log entries */
1758 if (pinstance
->ldn
.hcam
->notification_type
!=
1759 NOTIFICATION_TYPE_ERROR_LOG
)
1762 if (pinstance
->ldn
.hcam
->notification_lost
==
1763 HOSTRCB_NOTIFICATIONS_LOST
)
1764 dev_info(&pinstance
->pdev
->dev
, "Error notifications lost\n");
1766 ioasc
= le32_to_cpu(hcam_ldn
->error_log
.fd_ioasc
);
1768 if (ioasc
== PMCRAID_IOASC_UA_BUS_WAS_RESET
||
1769 ioasc
== PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER
) {
1770 dev_info(&pinstance
->pdev
->dev
,
1771 "UnitAttention due to IOA Bus Reset\n");
1772 scsi_report_bus_reset(
1774 RES_BUS(hcam_ldn
->error_log
.fd_ra
));
1781 * pmcraid_process_ccn - Op done function for a CCN.
1782 * @cmd: pointer to command struct
1784 * This function is the op done function for a configuration
1785 * change notification
1790 static void pmcraid_process_ccn(struct pmcraid_cmd
*cmd
)
1792 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1793 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
1794 unsigned long lock_flags
;
1796 pinstance
->ccn
.cmd
= NULL
;
1797 pmcraid_return_cmd(cmd
);
1799 /* If driver initiated IOA reset happened while this hcam was pending
1800 * with IOA, or IOA bringdown sequence is in progress, no need to
1801 * re-register the hcam
1803 if (ioasc
== PMCRAID_IOASC_IOA_WAS_RESET
||
1804 atomic_read(&pinstance
->ccn
.ignore
) == 1) {
1807 dev_info(&pinstance
->pdev
->dev
,
1808 "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc
);
1809 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
1810 pmcraid_send_hcam(pinstance
, PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1811 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
1813 pmcraid_handle_config_change(pinstance
);
1818 * pmcraid_process_ldn - op done function for an LDN
1819 * @cmd: pointer to command block
1824 static void pmcraid_initiate_reset(struct pmcraid_instance
*);
1825 static void pmcraid_set_timestamp(struct pmcraid_cmd
*cmd
);
1827 static void pmcraid_process_ldn(struct pmcraid_cmd
*cmd
)
1829 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1830 struct pmcraid_hcam_ldn
*ldn_hcam
=
1831 (struct pmcraid_hcam_ldn
*)pinstance
->ldn
.hcam
;
1832 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
1833 u32 fd_ioasc
= le32_to_cpu(ldn_hcam
->error_log
.fd_ioasc
);
1834 unsigned long lock_flags
;
1836 /* return the command block back to freepool */
1837 pinstance
->ldn
.cmd
= NULL
;
1838 pmcraid_return_cmd(cmd
);
1840 /* If driver initiated IOA reset happened while this hcam was pending
1841 * with IOA, no need to re-register the hcam as reset engine will do it
1842 * once reset sequence is complete
1844 if (ioasc
== PMCRAID_IOASC_IOA_WAS_RESET
||
1845 atomic_read(&pinstance
->ccn
.ignore
) == 1) {
1847 } else if (!ioasc
) {
1848 pmcraid_handle_error_log(pinstance
);
1849 if (fd_ioasc
== PMCRAID_IOASC_NR_IOA_RESET_REQUIRED
) {
1850 spin_lock_irqsave(pinstance
->host
->host_lock
,
1852 pmcraid_initiate_reset(pinstance
);
1853 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
1857 if (fd_ioasc
== PMCRAID_IOASC_TIME_STAMP_OUT_OF_SYNC
) {
1858 pinstance
->timestamp_error
= 1;
1859 pmcraid_set_timestamp(cmd
);
1862 dev_info(&pinstance
->pdev
->dev
,
1863 "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc
);
1865 /* send netlink message for HCAM notification if enabled */
1866 if (!pmcraid_disable_aen
)
1867 pmcraid_notify_ldn(pinstance
);
1869 cmd
= pmcraid_init_hcam(pinstance
, PMCRAID_HCAM_CODE_LOG_DATA
);
1871 pmcraid_send_hcam_cmd(cmd
);
1875 * pmcraid_register_hcams - register HCAMs for CCN and LDN
1877 * @pinstance: pointer per adapter instance structure
1882 static void pmcraid_register_hcams(struct pmcraid_instance
*pinstance
)
1884 pmcraid_send_hcam(pinstance
, PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1885 pmcraid_send_hcam(pinstance
, PMCRAID_HCAM_CODE_LOG_DATA
);
1889 * pmcraid_unregister_hcams - cancel HCAMs registered already
1890 * @cmd: pointer to command used as part of reset sequence
1892 static void pmcraid_unregister_hcams(struct pmcraid_cmd
*cmd
)
1894 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1896 /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
1897 * handling hcam response though it is not necessary. In order to
1898 * prevent this, set 'ignore', so that bring-down sequence doesn't
1899 * re-send any more hcams
1901 atomic_set(&pinstance
->ccn
.ignore
, 1);
1902 atomic_set(&pinstance
->ldn
.ignore
, 1);
1904 /* If adapter reset was forced as part of runtime reset sequence,
1905 * start the reset sequence. Reset will be triggered even in case
1908 if ((pinstance
->force_ioa_reset
&& !pinstance
->ioa_bringdown
) ||
1909 pinstance
->ioa_unit_check
) {
1910 pinstance
->force_ioa_reset
= 0;
1911 pinstance
->ioa_unit_check
= 0;
1912 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
1913 pmcraid_reset_alert(cmd
);
1917 /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
1918 * one after the other. So CCN cancellation will be triggered by
1919 * pmcraid_cancel_ldn itself.
1921 pmcraid_cancel_ldn(cmd
);
1925 * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
1926 * @pinstance: pointer to adapter instance structure
1928 * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
1930 static void pmcraid_reinit_buffers(struct pmcraid_instance
*);
1932 static int pmcraid_reset_enable_ioa(struct pmcraid_instance
*pinstance
)
1936 pmcraid_reinit_buffers(pinstance
);
1937 intrs
= pmcraid_read_interrupts(pinstance
);
1939 pmcraid_enable_interrupts(pinstance
, PMCRAID_PCI_INTERRUPTS
);
1941 if (intrs
& INTRS_TRANSITION_TO_OPERATIONAL
) {
1942 if (!pinstance
->interrupt_mode
) {
1943 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL
,
1944 pinstance
->int_regs
.
1945 ioa_host_interrupt_mask_reg
);
1946 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL
,
1947 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
1956 * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
1957 * @cmd : pointer to reset command block
1962 static void pmcraid_soft_reset(struct pmcraid_cmd
*cmd
)
1964 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1968 /* There will be an interrupt when Transition to Operational bit is
1969 * set so tasklet would execute next reset task. The timeout handler
1970 * would re-initiate a reset
1972 cmd
->cmd_done
= pmcraid_ioa_reset
;
1973 cmd
->timer
.data
= (unsigned long)cmd
;
1974 cmd
->timer
.expires
= jiffies
+
1975 msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT
);
1976 cmd
->timer
.function
= (void (*)(unsigned long))pmcraid_timeout_handler
;
1978 if (!timer_pending(&cmd
->timer
))
1979 add_timer(&cmd
->timer
);
1981 /* Enable destructive diagnostics on IOA if it is not yet in
1984 doorbell
= DOORBELL_RUNTIME_RESET
|
1985 DOORBELL_ENABLE_DESTRUCTIVE_DIAGS
;
1987 /* Since we do RESET_ALERT and Start BIST we have to again write
1988 * MSIX Doorbell to indicate the interrupt mode
1990 if (pinstance
->interrupt_mode
) {
1991 iowrite32(DOORBELL_INTR_MODE_MSIX
,
1992 pinstance
->int_regs
.host_ioa_interrupt_reg
);
1993 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
1996 iowrite32(doorbell
, pinstance
->int_regs
.host_ioa_interrupt_reg
);
1997 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
),
1998 int_reg
= ioread32(pinstance
->int_regs
.ioa_host_interrupt_reg
);
2000 pmcraid_info("Waiting for IOA to become operational %x:%x\n",
2001 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
),
2006 * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
2008 * @pinstance: pointer to adapter instance structure
2013 static void pmcraid_get_dump(struct pmcraid_instance
*pinstance
)
2015 pmcraid_info("%s is not yet implemented\n", __func__
);
2019 * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
2020 * @pinstance: pointer to adapter instance structure
2022 * This function fails all outstanding ops. If they are submitted to IOA
2023 * already, it sends cancel all messages if IOA is still accepting IOARCBs,
2024 * otherwise just completes the commands and returns the cmd blocks to free
2030 static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance
*pinstance
)
2032 struct pmcraid_cmd
*cmd
, *temp
;
2033 unsigned long lock_flags
;
2035 /* pending command list is protected by pending_pool_lock. Its
2036 * traversal must be done as within this lock
2038 spin_lock_irqsave(&pinstance
->pending_pool_lock
, lock_flags
);
2039 list_for_each_entry_safe(cmd
, temp
, &pinstance
->pending_cmd_pool
,
2041 list_del(&cmd
->free_list
);
2042 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
,
2044 cmd
->ioa_cb
->ioasa
.ioasc
=
2045 cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET
);
2046 cmd
->ioa_cb
->ioasa
.ilid
=
2047 cpu_to_be32(PMCRAID_DRIVER_ILID
);
2049 /* In case the command timer is still running */
2050 del_timer(&cmd
->timer
);
2052 /* If this is an IO command, complete it by invoking scsi_done
2053 * function. If this is one of the internal commands other
2054 * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
2057 if (cmd
->scsi_cmd
) {
2059 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2060 __le32 resp
= cmd
->ioa_cb
->ioarcb
.response_handle
;
2062 scsi_cmd
->result
|= DID_ERROR
<< 16;
2064 scsi_dma_unmap(scsi_cmd
);
2065 pmcraid_return_cmd(cmd
);
2067 pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
2068 le32_to_cpu(resp
) >> 2,
2069 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2071 scsi_cmd
->scsi_done(scsi_cmd
);
2072 } else if (cmd
->cmd_done
== pmcraid_internal_done
||
2073 cmd
->cmd_done
== pmcraid_erp_done
) {
2075 } else if (cmd
->cmd_done
!= pmcraid_ioa_reset
&&
2076 cmd
->cmd_done
!= pmcraid_ioa_shutdown_done
) {
2077 pmcraid_return_cmd(cmd
);
2080 atomic_dec(&pinstance
->outstanding_cmds
);
2081 spin_lock_irqsave(&pinstance
->pending_pool_lock
, lock_flags
);
2084 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
, lock_flags
);
2088 * pmcraid_ioa_reset - Implementation of IOA reset logic
2090 * @cmd: pointer to the cmd block to be used for entire reset process
2092 * This function executes most of the steps required for IOA reset. This gets
2093 * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
2094 * 'eh_' thread. Access to variables used for controlling the reset sequence is
2095 * synchronized using host lock. Various functions called during reset process
2096 * would make use of a single command block, pointer to which is also stored in
2097 * adapter instance structure.
2102 static void pmcraid_ioa_reset(struct pmcraid_cmd
*cmd
)
2104 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
2105 u8 reset_complete
= 0;
2107 pinstance
->ioa_reset_in_progress
= 1;
2109 if (pinstance
->reset_cmd
!= cmd
) {
2110 pmcraid_err("reset is called with different command block\n");
2111 pinstance
->reset_cmd
= cmd
;
2114 pmcraid_info("reset_engine: state = %d, command = %p\n",
2115 pinstance
->ioa_state
, cmd
);
2117 switch (pinstance
->ioa_state
) {
2119 case IOA_STATE_DEAD
:
2120 /* If IOA is offline, whatever may be the reset reason, just
2121 * return. callers might be waiting on the reset wait_q, wake
2124 pmcraid_err("IOA is offline no reset is possible\n");
2128 case IOA_STATE_IN_BRINGDOWN
:
2129 /* we enter here, once ioa shutdown command is processed by IOA
2130 * Alert IOA for a possible reset. If reset alert fails, IOA
2131 * goes through hard-reset
2133 pmcraid_disable_interrupts(pinstance
, ~0);
2134 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
2135 pmcraid_reset_alert(cmd
);
2138 case IOA_STATE_UNKNOWN
:
2139 /* We may be called during probe or resume. Some pre-processing
2140 * is required for prior to reset
2142 scsi_block_requests(pinstance
->host
);
2144 /* If asked to reset while IOA was processing responses or
2145 * there are any error responses then IOA may require
2148 if (pinstance
->ioa_hard_reset
== 0) {
2149 if (ioread32(pinstance
->ioa_status
) &
2150 INTRS_TRANSITION_TO_OPERATIONAL
) {
2151 pmcraid_info("sticky bit set, bring-up\n");
2152 pinstance
->ioa_state
= IOA_STATE_IN_BRINGUP
;
2153 pmcraid_reinit_cmdblk(cmd
);
2154 pmcraid_identify_hrrq(cmd
);
2156 pinstance
->ioa_state
= IOA_STATE_IN_SOFT_RESET
;
2157 pmcraid_soft_reset(cmd
);
2160 /* Alert IOA of a possible reset and wait for critical
2161 * operation in progress bit to reset
2163 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
2164 pmcraid_reset_alert(cmd
);
2168 case IOA_STATE_IN_RESET_ALERT
:
2169 /* If critical operation in progress bit is reset or wait gets
2170 * timed out, reset proceeds with starting BIST on the IOA.
2171 * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
2172 * they are 3 or more, reset engine marks IOA dead and returns
2174 pinstance
->ioa_state
= IOA_STATE_IN_HARD_RESET
;
2175 pmcraid_start_bist(cmd
);
2178 case IOA_STATE_IN_HARD_RESET
:
2179 pinstance
->ioa_reset_attempts
++;
2181 /* retry reset if we haven't reached maximum allowed limit */
2182 if (pinstance
->ioa_reset_attempts
> PMCRAID_RESET_ATTEMPTS
) {
2183 pinstance
->ioa_reset_attempts
= 0;
2184 pmcraid_err("IOA didn't respond marking it as dead\n");
2185 pinstance
->ioa_state
= IOA_STATE_DEAD
;
2187 if (pinstance
->ioa_bringdown
)
2188 pmcraid_notify_ioastate(pinstance
,
2189 PMC_DEVICE_EVENT_SHUTDOWN_FAILED
);
2191 pmcraid_notify_ioastate(pinstance
,
2192 PMC_DEVICE_EVENT_RESET_FAILED
);
2197 /* Once either bist or pci reset is done, restore PCI config
2198 * space. If this fails, proceed with hard reset again
2200 pci_restore_state(pinstance
->pdev
);
2202 /* fail all pending commands */
2203 pmcraid_fail_outstanding_cmds(pinstance
);
2205 /* check if unit check is active, if so extract dump */
2206 if (pinstance
->ioa_unit_check
) {
2207 pmcraid_info("unit check is active\n");
2208 pinstance
->ioa_unit_check
= 0;
2209 pmcraid_get_dump(pinstance
);
2210 pinstance
->ioa_reset_attempts
--;
2211 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
2212 pmcraid_reset_alert(cmd
);
2216 /* if the reset reason is to bring-down the ioa, we might be
2217 * done with the reset restore pci_config_space and complete
2220 if (pinstance
->ioa_bringdown
) {
2221 pmcraid_info("bringing down the adapter\n");
2222 pinstance
->ioa_shutdown_type
= SHUTDOWN_NONE
;
2223 pinstance
->ioa_bringdown
= 0;
2224 pinstance
->ioa_state
= IOA_STATE_UNKNOWN
;
2225 pmcraid_notify_ioastate(pinstance
,
2226 PMC_DEVICE_EVENT_SHUTDOWN_SUCCESS
);
2229 /* bring-up IOA, so proceed with soft reset
2230 * Reinitialize hrrq_buffers and their indices also
2231 * enable interrupts after a pci_restore_state
2233 if (pmcraid_reset_enable_ioa(pinstance
)) {
2234 pinstance
->ioa_state
= IOA_STATE_IN_BRINGUP
;
2235 pmcraid_info("bringing up the adapter\n");
2236 pmcraid_reinit_cmdblk(cmd
);
2237 pmcraid_identify_hrrq(cmd
);
2239 pinstance
->ioa_state
= IOA_STATE_IN_SOFT_RESET
;
2240 pmcraid_soft_reset(cmd
);
2245 case IOA_STATE_IN_SOFT_RESET
:
2246 /* TRANSITION TO OPERATIONAL is on so start initialization
2249 pmcraid_info("In softreset proceeding with bring-up\n");
2250 pinstance
->ioa_state
= IOA_STATE_IN_BRINGUP
;
2252 /* Initialization commands start with HRRQ identification. From
2253 * now on tasklet completes most of the commands as IOA is up
2254 * and intrs are enabled
2256 pmcraid_identify_hrrq(cmd
);
2259 case IOA_STATE_IN_BRINGUP
:
2260 /* we are done with bringing up of IOA, change the ioa_state to
2261 * operational and wake up any waiters
2263 pinstance
->ioa_state
= IOA_STATE_OPERATIONAL
;
2267 case IOA_STATE_OPERATIONAL
:
2269 /* When IOA is operational and a reset is requested, check for
2270 * the reset reason. If reset is to bring down IOA, unregister
2271 * HCAMs and initiate shutdown; if adapter reset is forced then
2272 * restart reset sequence again
2274 if (pinstance
->ioa_shutdown_type
== SHUTDOWN_NONE
&&
2275 pinstance
->force_ioa_reset
== 0) {
2276 pmcraid_notify_ioastate(pinstance
,
2277 PMC_DEVICE_EVENT_RESET_SUCCESS
);
2280 if (pinstance
->ioa_shutdown_type
!= SHUTDOWN_NONE
)
2281 pinstance
->ioa_state
= IOA_STATE_IN_BRINGDOWN
;
2282 pmcraid_reinit_cmdblk(cmd
);
2283 pmcraid_unregister_hcams(cmd
);
2288 /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
2289 * OPERATIONAL. Reset all control variables used during reset, wake up
2290 * any waiting threads and let the SCSI mid-layer send commands. Note
2291 * that host_lock must be held before invoking scsi_report_bus_reset.
2293 if (reset_complete
) {
2294 pinstance
->ioa_reset_in_progress
= 0;
2295 pinstance
->ioa_reset_attempts
= 0;
2296 pinstance
->reset_cmd
= NULL
;
2297 pinstance
->ioa_shutdown_type
= SHUTDOWN_NONE
;
2298 pinstance
->ioa_bringdown
= 0;
2299 pmcraid_return_cmd(cmd
);
2301 /* If target state is to bring up the adapter, proceed with
2302 * hcam registration and resource exposure to mid-layer.
2304 if (pinstance
->ioa_state
== IOA_STATE_OPERATIONAL
)
2305 pmcraid_register_hcams(pinstance
);
2307 wake_up_all(&pinstance
->reset_wait_q
);
2314 * pmcraid_initiate_reset - initiates reset sequence. This is called from
2315 * ISR/tasklet during error interrupts including IOA unit check. If reset
2316 * is already in progress, it just returns, otherwise initiates IOA reset
2317 * to bring IOA up to operational state.
2319 * @pinstance: pointer to adapter instance structure
2324 static void pmcraid_initiate_reset(struct pmcraid_instance
*pinstance
)
2326 struct pmcraid_cmd
*cmd
;
2328 /* If the reset is already in progress, just return, otherwise start
2329 * reset sequence and return
2331 if (!pinstance
->ioa_reset_in_progress
) {
2332 scsi_block_requests(pinstance
->host
);
2333 cmd
= pmcraid_get_free_cmd(pinstance
);
2336 pmcraid_err("no cmnd blocks for initiate_reset\n");
2340 pinstance
->ioa_shutdown_type
= SHUTDOWN_NONE
;
2341 pinstance
->reset_cmd
= cmd
;
2342 pinstance
->force_ioa_reset
= 1;
2343 pmcraid_notify_ioastate(pinstance
,
2344 PMC_DEVICE_EVENT_RESET_START
);
2345 pmcraid_ioa_reset(cmd
);
2350 * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
2352 * @pinstance: pointer adapter instance structure
2353 * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
2354 * @target_state: expected target state after reset
2356 * Note: This command initiates reset and waits for its completion. Hence this
2357 * should not be called from isr/timer/tasklet functions (timeout handlers,
2358 * error response handlers and interrupt handlers).
2361 * 1 in case ioa_state is not target_state, 0 otherwise.
2363 static int pmcraid_reset_reload(
2364 struct pmcraid_instance
*pinstance
,
2369 struct pmcraid_cmd
*reset_cmd
= NULL
;
2370 unsigned long lock_flags
;
2373 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
2375 if (pinstance
->ioa_reset_in_progress
) {
2376 pmcraid_info("reset_reload: reset is already in progress\n");
2378 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2380 wait_event(pinstance
->reset_wait_q
,
2381 !pinstance
->ioa_reset_in_progress
);
2383 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
2385 if (pinstance
->ioa_state
== IOA_STATE_DEAD
) {
2386 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
2388 pmcraid_info("reset_reload: IOA is dead\n");
2390 } else if (pinstance
->ioa_state
== target_state
) {
2396 pmcraid_info("reset_reload: proceeding with reset\n");
2397 scsi_block_requests(pinstance
->host
);
2398 reset_cmd
= pmcraid_get_free_cmd(pinstance
);
2400 if (reset_cmd
== NULL
) {
2401 pmcraid_err("no free cmnd for reset_reload\n");
2402 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
2407 if (shutdown_type
== SHUTDOWN_NORMAL
)
2408 pinstance
->ioa_bringdown
= 1;
2410 pinstance
->ioa_shutdown_type
= shutdown_type
;
2411 pinstance
->reset_cmd
= reset_cmd
;
2412 pinstance
->force_ioa_reset
= reset
;
2413 pmcraid_info("reset_reload: initiating reset\n");
2414 pmcraid_ioa_reset(reset_cmd
);
2415 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2416 pmcraid_info("reset_reload: waiting for reset to complete\n");
2417 wait_event(pinstance
->reset_wait_q
,
2418 !pinstance
->ioa_reset_in_progress
);
2420 pmcraid_info("reset_reload: reset is complete !!\n");
2421 scsi_unblock_requests(pinstance
->host
);
2422 if (pinstance
->ioa_state
== target_state
)
2430 * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
2432 * @pinstance: pointer to adapter instance structure
2435 * whatever is returned from pmcraid_reset_reload
2437 static int pmcraid_reset_bringdown(struct pmcraid_instance
*pinstance
)
2439 return pmcraid_reset_reload(pinstance
,
2445 * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
2447 * @pinstance: pointer to adapter instance structure
2450 * whatever is returned from pmcraid_reset_reload
2452 static int pmcraid_reset_bringup(struct pmcraid_instance
*pinstance
)
2454 pmcraid_notify_ioastate(pinstance
, PMC_DEVICE_EVENT_RESET_START
);
2456 return pmcraid_reset_reload(pinstance
,
2458 IOA_STATE_OPERATIONAL
);
2462 * pmcraid_request_sense - Send request sense to a device
2463 * @cmd: pmcraid command struct
2465 * This function sends a request sense to a device as a result of a check
2466 * condition. This method re-uses the same command block that failed earlier.
2468 static void pmcraid_request_sense(struct pmcraid_cmd
*cmd
)
2470 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
2471 struct pmcraid_ioadl_desc
*ioadl
= ioarcb
->add_data
.u
.ioadl
;
2473 /* allocate DMAable memory for sense buffers */
2474 cmd
->sense_buffer
= pci_alloc_consistent(cmd
->drv_inst
->pdev
,
2475 SCSI_SENSE_BUFFERSIZE
,
2476 &cmd
->sense_buffer_dma
);
2478 if (cmd
->sense_buffer
== NULL
) {
2480 ("couldn't allocate sense buffer for request sense\n");
2481 pmcraid_erp_done(cmd
);
2485 /* re-use the command block */
2486 memset(&cmd
->ioa_cb
->ioasa
, 0, sizeof(struct pmcraid_ioasa
));
2487 memset(ioarcb
->cdb
, 0, PMCRAID_MAX_CDB_LEN
);
2488 ioarcb
->request_flags0
= (SYNC_COMPLETE
|
2491 ioarcb
->request_type
= REQ_TYPE_SCSI
;
2492 ioarcb
->cdb
[0] = REQUEST_SENSE
;
2493 ioarcb
->cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
2495 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
2496 offsetof(struct pmcraid_ioarcb
,
2497 add_data
.u
.ioadl
[0]));
2498 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
2500 ioarcb
->data_transfer_length
= cpu_to_le32(SCSI_SENSE_BUFFERSIZE
);
2502 ioadl
->address
= cpu_to_le64(cmd
->sense_buffer_dma
);
2503 ioadl
->data_len
= cpu_to_le32(SCSI_SENSE_BUFFERSIZE
);
2504 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
2506 /* request sense might be called as part of error response processing
2507 * which runs in tasklets context. It is possible that mid-layer might
2508 * schedule queuecommand during this time, hence, writting to IOARRIN
2509 * must be protect by host_lock
2511 pmcraid_send_cmd(cmd
, pmcraid_erp_done
,
2512 PMCRAID_REQUEST_SENSE_TIMEOUT
,
2513 pmcraid_timeout_handler
);
2517 * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
2518 * @cmd: command that failed
2519 * @sense: true if request_sense is required after cancel all
2521 * This function sends a cancel all to a device to clear the queue.
2523 static void pmcraid_cancel_all(struct pmcraid_cmd
*cmd
, u32 sense
)
2525 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2526 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
2527 struct pmcraid_resource_entry
*res
= scsi_cmd
->device
->hostdata
;
2528 void (*cmd_done
) (struct pmcraid_cmd
*) = sense
? pmcraid_erp_done
2529 : pmcraid_request_sense
;
2531 memset(ioarcb
->cdb
, 0, PMCRAID_MAX_CDB_LEN
);
2532 ioarcb
->request_flags0
= SYNC_OVERRIDE
;
2533 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
2534 ioarcb
->cdb
[0] = PMCRAID_CANCEL_ALL_REQUESTS
;
2536 if (RES_IS_GSCSI(res
->cfg_entry
))
2537 ioarcb
->cdb
[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL
;
2539 ioarcb
->ioadl_bus_addr
= 0;
2540 ioarcb
->ioadl_length
= 0;
2541 ioarcb
->data_transfer_length
= 0;
2542 ioarcb
->ioarcb_bus_addr
&= (~0x1FULL
);
2544 /* writing to IOARRIN must be protected by host_lock, as mid-layer
2545 * schedule queuecommand while we are doing this
2547 pmcraid_send_cmd(cmd
, cmd_done
,
2548 PMCRAID_REQUEST_SENSE_TIMEOUT
,
2549 pmcraid_timeout_handler
);
2553 * pmcraid_frame_auto_sense: frame fixed format sense information
2555 * @cmd: pointer to failing command block
2560 static void pmcraid_frame_auto_sense(struct pmcraid_cmd
*cmd
)
2562 u8
*sense_buf
= cmd
->scsi_cmd
->sense_buffer
;
2563 struct pmcraid_resource_entry
*res
= cmd
->scsi_cmd
->device
->hostdata
;
2564 struct pmcraid_ioasa
*ioasa
= &cmd
->ioa_cb
->ioasa
;
2565 u32 ioasc
= le32_to_cpu(ioasa
->ioasc
);
2566 u32 failing_lba
= 0;
2568 memset(sense_buf
, 0, SCSI_SENSE_BUFFERSIZE
);
2569 cmd
->scsi_cmd
->result
= SAM_STAT_CHECK_CONDITION
;
2571 if (RES_IS_VSET(res
->cfg_entry
) &&
2572 ioasc
== PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC
&&
2573 ioasa
->u
.vset
.failing_lba_hi
!= 0) {
2575 sense_buf
[0] = 0x72;
2576 sense_buf
[1] = PMCRAID_IOASC_SENSE_KEY(ioasc
);
2577 sense_buf
[2] = PMCRAID_IOASC_SENSE_CODE(ioasc
);
2578 sense_buf
[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc
);
2582 sense_buf
[9] = 0x0A;
2583 sense_buf
[10] = 0x80;
2585 failing_lba
= le32_to_cpu(ioasa
->u
.vset
.failing_lba_hi
);
2587 sense_buf
[12] = (failing_lba
& 0xff000000) >> 24;
2588 sense_buf
[13] = (failing_lba
& 0x00ff0000) >> 16;
2589 sense_buf
[14] = (failing_lba
& 0x0000ff00) >> 8;
2590 sense_buf
[15] = failing_lba
& 0x000000ff;
2592 failing_lba
= le32_to_cpu(ioasa
->u
.vset
.failing_lba_lo
);
2594 sense_buf
[16] = (failing_lba
& 0xff000000) >> 24;
2595 sense_buf
[17] = (failing_lba
& 0x00ff0000) >> 16;
2596 sense_buf
[18] = (failing_lba
& 0x0000ff00) >> 8;
2597 sense_buf
[19] = failing_lba
& 0x000000ff;
2599 sense_buf
[0] = 0x70;
2600 sense_buf
[2] = PMCRAID_IOASC_SENSE_KEY(ioasc
);
2601 sense_buf
[12] = PMCRAID_IOASC_SENSE_CODE(ioasc
);
2602 sense_buf
[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc
);
2604 if (ioasc
== PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC
) {
2605 if (RES_IS_VSET(res
->cfg_entry
))
2607 le32_to_cpu(ioasa
->u
.
2608 vset
.failing_lba_lo
);
2609 sense_buf
[0] |= 0x80;
2610 sense_buf
[3] = (failing_lba
>> 24) & 0xff;
2611 sense_buf
[4] = (failing_lba
>> 16) & 0xff;
2612 sense_buf
[5] = (failing_lba
>> 8) & 0xff;
2613 sense_buf
[6] = failing_lba
& 0xff;
2616 sense_buf
[7] = 6; /* additional length */
2621 * pmcraid_error_handler - Error response handlers for a SCSI op
2622 * @cmd: pointer to pmcraid_cmd that has failed
2624 * This function determines whether or not to initiate ERP on the affected
2625 * device. This is called from a tasklet, which doesn't hold any locks.
2628 * 0 it caller can complete the request, otherwise 1 where in error
2629 * handler itself completes the request and returns the command block
2632 static int pmcraid_error_handler(struct pmcraid_cmd
*cmd
)
2634 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2635 struct pmcraid_resource_entry
*res
= scsi_cmd
->device
->hostdata
;
2636 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
2637 struct pmcraid_ioasa
*ioasa
= &cmd
->ioa_cb
->ioasa
;
2638 u32 ioasc
= le32_to_cpu(ioasa
->ioasc
);
2639 u32 masked_ioasc
= ioasc
& PMCRAID_IOASC_SENSE_MASK
;
2640 u32 sense_copied
= 0;
2643 pmcraid_info("resource pointer is NULL\n");
2647 /* If this was a SCSI read/write command keep count of errors */
2648 if (SCSI_CMD_TYPE(scsi_cmd
->cmnd
[0]) == SCSI_READ_CMD
)
2649 atomic_inc(&res
->read_failures
);
2650 else if (SCSI_CMD_TYPE(scsi_cmd
->cmnd
[0]) == SCSI_WRITE_CMD
)
2651 atomic_inc(&res
->write_failures
);
2653 if (!RES_IS_GSCSI(res
->cfg_entry
) &&
2654 masked_ioasc
!= PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR
) {
2655 pmcraid_frame_auto_sense(cmd
);
2658 /* Log IOASC/IOASA information based on user settings */
2659 pmcraid_ioasc_logger(ioasc
, cmd
);
2661 switch (masked_ioasc
) {
2663 case PMCRAID_IOASC_AC_TERMINATED_BY_HOST
:
2664 scsi_cmd
->result
|= (DID_ABORT
<< 16);
2667 case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE
:
2668 case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE
:
2669 scsi_cmd
->result
|= (DID_NO_CONNECT
<< 16);
2672 case PMCRAID_IOASC_NR_SYNC_REQUIRED
:
2674 scsi_cmd
->result
|= (DID_IMM_RETRY
<< 16);
2677 case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC
:
2678 scsi_cmd
->result
|= (DID_PASSTHROUGH
<< 16);
2681 case PMCRAID_IOASC_UA_BUS_WAS_RESET
:
2682 case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER
:
2683 if (!res
->reset_progress
)
2684 scsi_report_bus_reset(pinstance
->host
,
2685 scsi_cmd
->device
->channel
);
2686 scsi_cmd
->result
|= (DID_ERROR
<< 16);
2689 case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR
:
2690 scsi_cmd
->result
|= PMCRAID_IOASC_SENSE_STATUS(ioasc
);
2693 /* if check_condition is not active return with error otherwise
2694 * get/frame the sense buffer
2696 if (PMCRAID_IOASC_SENSE_STATUS(ioasc
) !=
2697 SAM_STAT_CHECK_CONDITION
&&
2698 PMCRAID_IOASC_SENSE_STATUS(ioasc
) != SAM_STAT_ACA_ACTIVE
)
2701 /* If we have auto sense data as part of IOASA pass it to
2704 if (ioasa
->auto_sense_length
!= 0) {
2705 short sense_len
= ioasa
->auto_sense_length
;
2706 int data_size
= min_t(u16
, le16_to_cpu(sense_len
),
2707 SCSI_SENSE_BUFFERSIZE
);
2709 memcpy(scsi_cmd
->sense_buffer
,
2715 if (RES_IS_GSCSI(res
->cfg_entry
))
2716 pmcraid_cancel_all(cmd
, sense_copied
);
2717 else if (sense_copied
)
2718 pmcraid_erp_done(cmd
);
2720 pmcraid_request_sense(cmd
);
2724 case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED
:
2728 if (PMCRAID_IOASC_SENSE_KEY(ioasc
) > RECOVERED_ERROR
)
2729 scsi_cmd
->result
|= (DID_ERROR
<< 16);
2736 * pmcraid_reset_device - device reset handler functions
2738 * @scsi_cmd: scsi command struct
2739 * @modifier: reset modifier indicating the reset sequence to be performed
2741 * This function issues a device reset to the affected device.
2742 * A LUN reset will be sent to the device first. If that does
2743 * not work, a target reset will be sent.
2748 static int pmcraid_reset_device(
2749 struct scsi_cmnd
*scsi_cmd
,
2750 unsigned long timeout
,
2754 struct pmcraid_cmd
*cmd
;
2755 struct pmcraid_instance
*pinstance
;
2756 struct pmcraid_resource_entry
*res
;
2757 struct pmcraid_ioarcb
*ioarcb
;
2758 unsigned long lock_flags
;
2762 (struct pmcraid_instance
*)scsi_cmd
->device
->host
->hostdata
;
2763 res
= scsi_cmd
->device
->hostdata
;
2766 sdev_printk(KERN_ERR
, scsi_cmd
->device
,
2767 "reset_device: NULL resource pointer\n");
2771 /* If adapter is currently going through reset/reload, return failed.
2772 * This will force the mid-layer to call _eh_bus/host reset, which
2773 * will then go to sleep and wait for the reset to complete
2775 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
2776 if (pinstance
->ioa_reset_in_progress
||
2777 pinstance
->ioa_state
== IOA_STATE_DEAD
) {
2778 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2782 res
->reset_progress
= 1;
2783 pmcraid_info("Resetting %s resource with addr %x\n",
2784 ((modifier
& RESET_DEVICE_LUN
) ? "LUN" :
2785 ((modifier
& RESET_DEVICE_TARGET
) ? "TARGET" : "BUS")),
2786 le32_to_cpu(res
->cfg_entry
.resource_address
));
2788 /* get a free cmd block */
2789 cmd
= pmcraid_get_free_cmd(pinstance
);
2792 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2793 pmcraid_err("%s: no cmd blocks are available\n", __func__
);
2797 ioarcb
= &cmd
->ioa_cb
->ioarcb
;
2798 ioarcb
->resource_handle
= res
->cfg_entry
.resource_handle
;
2799 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
2800 ioarcb
->cdb
[0] = PMCRAID_RESET_DEVICE
;
2802 /* Initialize reset modifier bits */
2804 modifier
= ENABLE_RESET_MODIFIER
| modifier
;
2806 ioarcb
->cdb
[1] = modifier
;
2808 init_completion(&cmd
->wait_for_completion
);
2809 cmd
->completion_req
= 1;
2811 pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
2812 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2813 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.resource_handle
),
2814 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2);
2816 pmcraid_send_cmd(cmd
,
2817 pmcraid_internal_done
,
2819 pmcraid_timeout_handler
);
2821 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2823 /* RESET_DEVICE command completes after all pending IOARCBs are
2824 * completed. Once this command is completed, pmcraind_internal_done
2825 * will wake up the 'completion' queue.
2827 wait_for_completion(&cmd
->wait_for_completion
);
2829 /* complete the command here itself and return the command block
2832 pmcraid_return_cmd(cmd
);
2833 res
->reset_progress
= 0;
2834 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
2836 /* set the return value based on the returned ioasc */
2837 return PMCRAID_IOASC_SENSE_KEY(ioasc
) ? FAILED
: SUCCESS
;
2841 * _pmcraid_io_done - helper for pmcraid_io_done function
2843 * @cmd: pointer to pmcraid command struct
2844 * @reslen: residual data length to be set in the ioasa
2845 * @ioasc: ioasc either returned by IOA or set by driver itself.
2847 * This function is invoked by pmcraid_io_done to complete mid-layer
2851 * 0 if caller is required to return it to free_pool. Returns 1 if
2852 * caller need not worry about freeing command block as error handler
2853 * will take care of that.
2856 static int _pmcraid_io_done(struct pmcraid_cmd
*cmd
, int reslen
, int ioasc
)
2858 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2861 scsi_set_resid(scsi_cmd
, reslen
);
2863 pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
2864 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2,
2865 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2866 ioasc
, scsi_cmd
->result
);
2868 if (PMCRAID_IOASC_SENSE_KEY(ioasc
) != 0)
2869 rc
= pmcraid_error_handler(cmd
);
2872 scsi_dma_unmap(scsi_cmd
);
2873 scsi_cmd
->scsi_done(scsi_cmd
);
2880 * pmcraid_io_done - SCSI completion function
2882 * @cmd: pointer to pmcraid command struct
2884 * This function is invoked by tasklet/mid-layer error handler to completing
2885 * the SCSI ops sent from mid-layer.
2891 static void pmcraid_io_done(struct pmcraid_cmd
*cmd
)
2893 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
2894 u32 reslen
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.residual_data_length
);
2896 if (_pmcraid_io_done(cmd
, reslen
, ioasc
) == 0)
2897 pmcraid_return_cmd(cmd
);
2901 * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
2903 * @cmd: command block of the command to be aborted
2906 * returns pointer to command structure used as cancelling cmd
2908 static struct pmcraid_cmd
*pmcraid_abort_cmd(struct pmcraid_cmd
*cmd
)
2910 struct pmcraid_cmd
*cancel_cmd
;
2911 struct pmcraid_instance
*pinstance
;
2912 struct pmcraid_resource_entry
*res
;
2914 pinstance
= (struct pmcraid_instance
*)cmd
->drv_inst
;
2915 res
= cmd
->scsi_cmd
->device
->hostdata
;
2917 cancel_cmd
= pmcraid_get_free_cmd(pinstance
);
2919 if (cancel_cmd
== NULL
) {
2920 pmcraid_err("%s: no cmd blocks are available\n", __func__
);
2924 pmcraid_prepare_cancel_cmd(cancel_cmd
, cmd
);
2926 pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
2927 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2928 cmd
->ioa_cb
->ioarcb
.response_handle
>> 2);
2930 init_completion(&cancel_cmd
->wait_for_completion
);
2931 cancel_cmd
->completion_req
= 1;
2933 pmcraid_info("command (%d) CDB[0] = %x for %x\n",
2934 le32_to_cpu(cancel_cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2,
2935 cancel_cmd
->ioa_cb
->ioarcb
.cdb
[0],
2936 le32_to_cpu(cancel_cmd
->ioa_cb
->ioarcb
.resource_handle
));
2938 pmcraid_send_cmd(cancel_cmd
,
2939 pmcraid_internal_done
,
2940 PMCRAID_INTERNAL_TIMEOUT
,
2941 pmcraid_timeout_handler
);
2946 * pmcraid_abort_complete - Waits for ABORT TASK completion
2948 * @cancel_cmd: command block use as cancelling command
2951 * returns SUCCESS if ABORT TASK has good completion
2954 static int pmcraid_abort_complete(struct pmcraid_cmd
*cancel_cmd
)
2956 struct pmcraid_resource_entry
*res
;
2959 wait_for_completion(&cancel_cmd
->wait_for_completion
);
2960 res
= cancel_cmd
->res
;
2961 cancel_cmd
->res
= NULL
;
2962 ioasc
= le32_to_cpu(cancel_cmd
->ioa_cb
->ioasa
.ioasc
);
2964 /* If the abort task is not timed out we will get a Good completion
2965 * as sense_key, otherwise we may get one the following responses
2966 * due to subsequent bus reset or device reset. In case IOASC is
2967 * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
2969 if (ioasc
== PMCRAID_IOASC_UA_BUS_WAS_RESET
||
2970 ioasc
== PMCRAID_IOASC_NR_SYNC_REQUIRED
) {
2971 if (ioasc
== PMCRAID_IOASC_NR_SYNC_REQUIRED
)
2976 /* complete the command here itself */
2977 pmcraid_return_cmd(cancel_cmd
);
2978 return PMCRAID_IOASC_SENSE_KEY(ioasc
) ? FAILED
: SUCCESS
;
2982 * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
2984 * @scsi_cmd: scsi command struct given by mid-layer. When this is called
2985 * mid-layer ensures that no other commands are queued. This
2986 * never gets called under interrupt, but a separate eh thread.
2991 static int pmcraid_eh_abort_handler(struct scsi_cmnd
*scsi_cmd
)
2993 struct pmcraid_instance
*pinstance
;
2994 struct pmcraid_cmd
*cmd
;
2995 struct pmcraid_resource_entry
*res
;
2996 unsigned long host_lock_flags
;
2997 unsigned long pending_lock_flags
;
2998 struct pmcraid_cmd
*cancel_cmd
= NULL
;
3003 (struct pmcraid_instance
*)scsi_cmd
->device
->host
->hostdata
;
3005 scmd_printk(KERN_INFO
, scsi_cmd
,
3006 "I/O command timed out, aborting it.\n");
3008 res
= scsi_cmd
->device
->hostdata
;
3013 /* If we are currently going through reset/reload, return failed.
3014 * This will force the mid-layer to eventually call
3015 * pmcraid_eh_host_reset which will then go to sleep and wait for the
3018 spin_lock_irqsave(pinstance
->host
->host_lock
, host_lock_flags
);
3020 if (pinstance
->ioa_reset_in_progress
||
3021 pinstance
->ioa_state
== IOA_STATE_DEAD
) {
3022 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
3027 /* loop over pending cmd list to find cmd corresponding to this
3028 * scsi_cmd. Note that this command might not have been completed
3029 * already. locking: all pending commands are protected with
3030 * pending_pool_lock.
3032 spin_lock_irqsave(&pinstance
->pending_pool_lock
, pending_lock_flags
);
3033 list_for_each_entry(cmd
, &pinstance
->pending_cmd_pool
, free_list
) {
3035 if (cmd
->scsi_cmd
== scsi_cmd
) {
3041 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
,
3042 pending_lock_flags
);
3044 /* If the command to be aborted was given to IOA and still pending with
3045 * it, send ABORT_TASK to abort this and wait for its completion
3048 cancel_cmd
= pmcraid_abort_cmd(cmd
);
3050 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
3054 cancel_cmd
->res
= cmd
->scsi_cmd
->device
->hostdata
;
3055 rc
= pmcraid_abort_complete(cancel_cmd
);
3058 return cmd_found
? rc
: SUCCESS
;
3062 * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
3064 * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
3066 * All these routines invokve pmcraid_reset_device with appropriate parameters.
3067 * Since these are called from mid-layer EH thread, no other IO will be queued
3068 * to the resource being reset. However, control path (IOCTL) may be active so
3069 * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
3070 * takes care by locking/unlocking host_lock.
3075 static int pmcraid_eh_device_reset_handler(struct scsi_cmnd
*scmd
)
3077 scmd_printk(KERN_INFO
, scmd
,
3078 "resetting device due to an I/O command timeout.\n");
3079 return pmcraid_reset_device(scmd
,
3080 PMCRAID_INTERNAL_TIMEOUT
,
3084 static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd
*scmd
)
3086 scmd_printk(KERN_INFO
, scmd
,
3087 "Doing bus reset due to an I/O command timeout.\n");
3088 return pmcraid_reset_device(scmd
,
3089 PMCRAID_RESET_BUS_TIMEOUT
,
3093 static int pmcraid_eh_target_reset_handler(struct scsi_cmnd
*scmd
)
3095 scmd_printk(KERN_INFO
, scmd
,
3096 "Doing target reset due to an I/O command timeout.\n");
3097 return pmcraid_reset_device(scmd
,
3098 PMCRAID_INTERNAL_TIMEOUT
,
3099 RESET_DEVICE_TARGET
);
3103 * pmcraid_eh_host_reset_handler - adapter reset handler callback
3105 * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
3107 * Initiates adapter reset to bring it up to operational state
3112 static int pmcraid_eh_host_reset_handler(struct scsi_cmnd
*scmd
)
3114 unsigned long interval
= 10000; /* 10 seconds interval */
3115 int waits
= jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT
) / interval
;
3116 struct pmcraid_instance
*pinstance
=
3117 (struct pmcraid_instance
*)(scmd
->device
->host
->hostdata
);
3120 /* wait for an additional 150 seconds just in case firmware could come
3121 * up and if it could complete all the pending commands excluding the
3122 * two HCAM (CCN and LDN).
3125 if (atomic_read(&pinstance
->outstanding_cmds
) <=
3126 PMCRAID_MAX_HCAM_CMD
)
3131 dev_err(&pinstance
->pdev
->dev
,
3132 "Adapter being reset due to an I/O command timeout.\n");
3133 return pmcraid_reset_bringup(pinstance
) == 0 ? SUCCESS
: FAILED
;
3137 * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
3138 * @cmd: pmcraid command struct
3139 * @sgcount: count of scatter-gather elements
3142 * returns pointer pmcraid_ioadl_desc, initialized to point to internal
3143 * or external IOADLs
3145 struct pmcraid_ioadl_desc
*
3146 pmcraid_init_ioadls(struct pmcraid_cmd
*cmd
, int sgcount
)
3148 struct pmcraid_ioadl_desc
*ioadl
;
3149 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
3150 int ioadl_count
= 0;
3152 if (ioarcb
->add_cmd_param_length
)
3153 ioadl_count
= DIV_ROUND_UP(ioarcb
->add_cmd_param_length
, 16);
3154 ioarcb
->ioadl_length
=
3155 sizeof(struct pmcraid_ioadl_desc
) * sgcount
;
3157 if ((sgcount
+ ioadl_count
) > (ARRAY_SIZE(ioarcb
->add_data
.u
.ioadl
))) {
3158 /* external ioadls start at offset 0x80 from control_block
3159 * structure, re-using 24 out of 27 ioadls part of IOARCB.
3160 * It is necessary to indicate to firmware that driver is
3161 * using ioadls to be treated as external to IOARCB.
3163 ioarcb
->ioarcb_bus_addr
&= ~(0x1FULL
);
3164 ioarcb
->ioadl_bus_addr
=
3165 cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
3166 offsetof(struct pmcraid_ioarcb
,
3167 add_data
.u
.ioadl
[3]));
3168 ioadl
= &ioarcb
->add_data
.u
.ioadl
[3];
3170 ioarcb
->ioadl_bus_addr
=
3171 cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
3172 offsetof(struct pmcraid_ioarcb
,
3173 add_data
.u
.ioadl
[ioadl_count
]));
3175 ioadl
= &ioarcb
->add_data
.u
.ioadl
[ioadl_count
];
3176 ioarcb
->ioarcb_bus_addr
|=
3177 DIV_ROUND_CLOSEST(sgcount
+ ioadl_count
, 8);
3184 * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
3185 * @pinstance: pointer to adapter instance structure
3186 * @cmd: pmcraid command struct
3188 * This function is invoked by queuecommand entry point while sending a command
3189 * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
3192 * 0 on success or -1 on failure
3194 static int pmcraid_build_ioadl(
3195 struct pmcraid_instance
*pinstance
,
3196 struct pmcraid_cmd
*cmd
3200 struct scatterlist
*sglist
;
3202 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
3203 struct pmcraid_ioarcb
*ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
3204 struct pmcraid_ioadl_desc
*ioadl
= ioarcb
->add_data
.u
.ioadl
;
3206 u32 length
= scsi_bufflen(scsi_cmd
);
3211 nseg
= scsi_dma_map(scsi_cmd
);
3214 scmd_printk(KERN_ERR
, scsi_cmd
, "scsi_map_dma failed!\n");
3216 } else if (nseg
> PMCRAID_MAX_IOADLS
) {
3217 scsi_dma_unmap(scsi_cmd
);
3218 scmd_printk(KERN_ERR
, scsi_cmd
,
3219 "sg count is (%d) more than allowed!\n", nseg
);
3223 /* Initialize IOARCB data transfer length fields */
3224 if (scsi_cmd
->sc_data_direction
== DMA_TO_DEVICE
)
3225 ioarcb
->request_flags0
|= TRANSFER_DIR_WRITE
;
3227 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
3228 ioarcb
->data_transfer_length
= cpu_to_le32(length
);
3229 ioadl
= pmcraid_init_ioadls(cmd
, nseg
);
3231 /* Initialize IOADL descriptor addresses */
3232 scsi_for_each_sg(scsi_cmd
, sglist
, nseg
, i
) {
3233 ioadl
[i
].data_len
= cpu_to_le32(sg_dma_len(sglist
));
3234 ioadl
[i
].address
= cpu_to_le64(sg_dma_address(sglist
));
3237 /* setup last descriptor */
3238 ioadl
[i
- 1].flags
= IOADL_FLAGS_LAST_DESC
;
3244 * pmcraid_free_sglist - Frees an allocated SG buffer list
3245 * @sglist: scatter/gather list pointer
3247 * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
3252 static void pmcraid_free_sglist(struct pmcraid_sglist
*sglist
)
3256 for (i
= 0; i
< sglist
->num_sg
; i
++)
3257 __free_pages(sg_page(&(sglist
->scatterlist
[i
])),
3264 * pmcraid_alloc_sglist - Allocates memory for a SG list
3265 * @buflen: buffer length
3267 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3271 * pointer to sglist / NULL on failure
3273 static struct pmcraid_sglist
*pmcraid_alloc_sglist(int buflen
)
3275 struct pmcraid_sglist
*sglist
;
3276 struct scatterlist
*scatterlist
;
3283 sg_size
= buflen
/ (PMCRAID_MAX_IOADLS
- 1);
3284 order
= (sg_size
> 0) ? get_order(sg_size
) : 0;
3285 bsize_elem
= PAGE_SIZE
* (1 << order
);
3287 /* Determine the actual number of sg entries needed */
3288 if (buflen
% bsize_elem
)
3289 num_elem
= (buflen
/ bsize_elem
) + 1;
3291 num_elem
= buflen
/ bsize_elem
;
3293 /* Allocate a scatter/gather list for the DMA */
3294 sglist
= kzalloc(sizeof(struct pmcraid_sglist
) +
3295 (sizeof(struct scatterlist
) * (num_elem
- 1)),
3301 scatterlist
= sglist
->scatterlist
;
3302 sg_init_table(scatterlist
, num_elem
);
3303 sglist
->order
= order
;
3304 sglist
->num_sg
= num_elem
;
3307 for (i
= 0; i
< num_elem
; i
++) {
3308 page
= alloc_pages(GFP_KERNEL
|GFP_DMA
|__GFP_ZERO
, order
);
3310 for (j
= i
- 1; j
>= 0; j
--)
3311 __free_pages(sg_page(&scatterlist
[j
]), order
);
3316 sg_set_page(&scatterlist
[i
], page
,
3317 sg_size
< bsize_elem
? sg_size
: bsize_elem
, 0);
3318 sg_size
-= bsize_elem
;
3325 * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
3326 * @sglist: scatter/gather list pointer
3327 * @buffer: buffer pointer
3328 * @len: buffer length
3329 * @direction: data transfer direction
3331 * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
3334 * 0 on success / other on failure
3336 static int pmcraid_copy_sglist(
3337 struct pmcraid_sglist
*sglist
,
3338 unsigned long buffer
,
3343 struct scatterlist
*scatterlist
;
3349 /* Determine the actual number of bytes per element */
3350 bsize_elem
= PAGE_SIZE
* (1 << sglist
->order
);
3352 scatterlist
= sglist
->scatterlist
;
3354 for (i
= 0; i
< (len
/ bsize_elem
); i
++, buffer
+= bsize_elem
) {
3355 struct page
*page
= sg_page(&scatterlist
[i
]);
3358 if (direction
== DMA_TO_DEVICE
)
3359 rc
= __copy_from_user(kaddr
,
3363 rc
= __copy_to_user((void *)buffer
, kaddr
, bsize_elem
);
3368 pmcraid_err("failed to copy user data into sg list\n");
3372 scatterlist
[i
].length
= bsize_elem
;
3375 if (len
% bsize_elem
) {
3376 struct page
*page
= sg_page(&scatterlist
[i
]);
3380 if (direction
== DMA_TO_DEVICE
)
3381 rc
= __copy_from_user(kaddr
,
3385 rc
= __copy_to_user((void *)buffer
,
3391 scatterlist
[i
].length
= len
% bsize_elem
;
3395 pmcraid_err("failed to copy user data into sg list\n");
3403 * pmcraid_queuecommand - Queue a mid-layer request
3404 * @scsi_cmd: scsi command struct
3405 * @done: done function
3407 * This function queues a request generated by the mid-layer. Midlayer calls
3408 * this routine within host->lock. Some of the functions called by queuecommand
3409 * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
3413 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3414 * SCSI_MLQUEUE_HOST_BUSY if host is busy
3416 static int pmcraid_queuecommand_lck(
3417 struct scsi_cmnd
*scsi_cmd
,
3418 void (*done
) (struct scsi_cmnd
*)
3421 struct pmcraid_instance
*pinstance
;
3422 struct pmcraid_resource_entry
*res
;
3423 struct pmcraid_ioarcb
*ioarcb
;
3424 struct pmcraid_cmd
*cmd
;
3429 (struct pmcraid_instance
*)scsi_cmd
->device
->host
->hostdata
;
3430 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
3431 scsi_cmd
->scsi_done
= done
;
3432 res
= scsi_cmd
->device
->hostdata
;
3433 scsi_cmd
->result
= (DID_OK
<< 16);
3435 /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
3438 if (pinstance
->ioa_state
== IOA_STATE_DEAD
) {
3439 pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
3440 scsi_cmd
->result
= (DID_NO_CONNECT
<< 16);
3441 scsi_cmd
->scsi_done(scsi_cmd
);
3445 /* If IOA reset is in progress, can't queue the commands */
3446 if (pinstance
->ioa_reset_in_progress
)
3447 return SCSI_MLQUEUE_HOST_BUSY
;
3449 /* Firmware doesn't support SYNCHRONIZE_CACHE command (0x35), complete
3450 * the command here itself with success return
3452 if (scsi_cmd
->cmnd
[0] == SYNCHRONIZE_CACHE
) {
3453 pmcraid_info("SYNC_CACHE(0x35), completing in driver itself\n");
3454 scsi_cmd
->scsi_done(scsi_cmd
);
3458 /* initialize the command and IOARCB to be sent to IOA */
3459 cmd
= pmcraid_get_free_cmd(pinstance
);
3462 pmcraid_err("free command block is not available\n");
3463 return SCSI_MLQUEUE_HOST_BUSY
;
3466 cmd
->scsi_cmd
= scsi_cmd
;
3467 ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
3468 memcpy(ioarcb
->cdb
, scsi_cmd
->cmnd
, scsi_cmd
->cmd_len
);
3469 ioarcb
->resource_handle
= res
->cfg_entry
.resource_handle
;
3470 ioarcb
->request_type
= REQ_TYPE_SCSI
;
3472 /* set hrrq number where the IOA should respond to. Note that all cmds
3473 * generated internally uses hrrq_id 0, exception to this is the cmd
3474 * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
3475 * hrrq_id assigned here in queuecommand
3477 ioarcb
->hrrq_id
= atomic_add_return(1, &(pinstance
->last_message_id
)) %
3478 pinstance
->num_hrrq
;
3479 cmd
->cmd_done
= pmcraid_io_done
;
3481 if (RES_IS_GSCSI(res
->cfg_entry
) || RES_IS_VSET(res
->cfg_entry
)) {
3482 if (scsi_cmd
->underflow
== 0)
3483 ioarcb
->request_flags0
|= INHIBIT_UL_CHECK
;
3485 if (res
->sync_reqd
) {
3486 ioarcb
->request_flags0
|= SYNC_COMPLETE
;
3490 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
3492 if (scsi_cmd
->flags
& SCMD_TAGGED
)
3493 ioarcb
->request_flags1
|= TASK_TAG_SIMPLE
;
3495 if (RES_IS_GSCSI(res
->cfg_entry
))
3496 ioarcb
->request_flags1
|= DELAY_AFTER_RESET
;
3499 rc
= pmcraid_build_ioadl(pinstance
, cmd
);
3501 pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
3502 le32_to_cpu(ioarcb
->response_handle
) >> 2,
3503 scsi_cmd
->cmnd
[0], pinstance
->host
->unique_id
,
3504 RES_IS_VSET(res
->cfg_entry
) ? PMCRAID_VSET_BUS_ID
:
3505 PMCRAID_PHYS_BUS_ID
,
3506 RES_IS_VSET(res
->cfg_entry
) ?
3507 (fw_version
<= PMCRAID_FW_VERSION_1
?
3508 res
->cfg_entry
.unique_flags1
:
3509 res
->cfg_entry
.array_id
& 0xFF) :
3510 RES_TARGET(res
->cfg_entry
.resource_address
),
3511 RES_LUN(res
->cfg_entry
.resource_address
));
3513 if (likely(rc
== 0)) {
3514 _pmcraid_fire_command(cmd
);
3516 pmcraid_err("queuecommand could not build ioadl\n");
3517 pmcraid_return_cmd(cmd
);
3518 rc
= SCSI_MLQUEUE_HOST_BUSY
;
3524 static DEF_SCSI_QCMD(pmcraid_queuecommand
)
3527 * pmcraid_open -char node "open" entry, allowed only users with admin access
3529 static int pmcraid_chr_open(struct inode
*inode
, struct file
*filep
)
3531 struct pmcraid_instance
*pinstance
;
3533 if (!capable(CAP_SYS_ADMIN
))
3536 /* Populate adapter instance * pointer for use by ioctl */
3537 pinstance
= container_of(inode
->i_cdev
, struct pmcraid_instance
, cdev
);
3538 filep
->private_data
= pinstance
;
3544 * pmcraid_fasync - Async notifier registration from applications
3546 * This function adds the calling process to a driver global queue. When an
3547 * event occurs, SIGIO will be sent to all processes in this queue.
3549 static int pmcraid_chr_fasync(int fd
, struct file
*filep
, int mode
)
3551 struct pmcraid_instance
*pinstance
;
3554 pinstance
= filep
->private_data
;
3555 mutex_lock(&pinstance
->aen_queue_lock
);
3556 rc
= fasync_helper(fd
, filep
, mode
, &pinstance
->aen_queue
);
3557 mutex_unlock(&pinstance
->aen_queue_lock
);
3564 * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
3565 * commands sent over IOCTL interface
3567 * @cmd : pointer to struct pmcraid_cmd
3568 * @buflen : length of the request buffer
3569 * @direction : data transfer direction
3572 * 0 on success, non-zero error code on failure
3574 static int pmcraid_build_passthrough_ioadls(
3575 struct pmcraid_cmd
*cmd
,
3580 struct pmcraid_sglist
*sglist
= NULL
;
3581 struct scatterlist
*sg
= NULL
;
3582 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
3583 struct pmcraid_ioadl_desc
*ioadl
;
3586 sglist
= pmcraid_alloc_sglist(buflen
);
3589 pmcraid_err("can't allocate memory for passthrough SGls\n");
3593 sglist
->num_dma_sg
= pci_map_sg(cmd
->drv_inst
->pdev
,
3594 sglist
->scatterlist
,
3595 sglist
->num_sg
, direction
);
3597 if (!sglist
->num_dma_sg
|| sglist
->num_dma_sg
> PMCRAID_MAX_IOADLS
) {
3598 dev_err(&cmd
->drv_inst
->pdev
->dev
,
3599 "Failed to map passthrough buffer!\n");
3600 pmcraid_free_sglist(sglist
);
3604 cmd
->sglist
= sglist
;
3605 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
3607 ioadl
= pmcraid_init_ioadls(cmd
, sglist
->num_dma_sg
);
3609 /* Initialize IOADL descriptor addresses */
3610 for_each_sg(sglist
->scatterlist
, sg
, sglist
->num_dma_sg
, i
) {
3611 ioadl
[i
].data_len
= cpu_to_le32(sg_dma_len(sg
));
3612 ioadl
[i
].address
= cpu_to_le64(sg_dma_address(sg
));
3616 /* setup the last descriptor */
3617 ioadl
[i
- 1].flags
= IOADL_FLAGS_LAST_DESC
;
3624 * pmcraid_release_passthrough_ioadls - release passthrough ioadls
3626 * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
3627 * @buflen: size of the request buffer
3628 * @direction: data transfer direction
3631 * 0 on success, non-zero error code on failure
3633 static void pmcraid_release_passthrough_ioadls(
3634 struct pmcraid_cmd
*cmd
,
3639 struct pmcraid_sglist
*sglist
= cmd
->sglist
;
3642 pci_unmap_sg(cmd
->drv_inst
->pdev
,
3643 sglist
->scatterlist
,
3646 pmcraid_free_sglist(sglist
);
3652 * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
3654 * @pinstance: pointer to adapter instance structure
3656 * @arg: pointer to pmcraid_passthrough_buffer user buffer
3659 * 0 on success, non-zero error code on failure
3661 static long pmcraid_ioctl_passthrough(
3662 struct pmcraid_instance
*pinstance
,
3663 unsigned int ioctl_cmd
,
3664 unsigned int buflen
,
3668 struct pmcraid_passthrough_ioctl_buffer
*buffer
;
3669 struct pmcraid_ioarcb
*ioarcb
;
3670 struct pmcraid_cmd
*cmd
;
3671 struct pmcraid_cmd
*cancel_cmd
;
3672 unsigned long request_buffer
;
3673 unsigned long request_offset
;
3674 unsigned long lock_flags
;
3679 u8 access
, direction
;
3682 /* If IOA reset is in progress, wait 10 secs for reset to complete */
3683 if (pinstance
->ioa_reset_in_progress
) {
3684 rc
= wait_event_interruptible_timeout(
3685 pinstance
->reset_wait_q
,
3686 !pinstance
->ioa_reset_in_progress
,
3687 msecs_to_jiffies(10000));
3692 return -ERESTARTSYS
;
3695 /* If adapter is not in operational state, return error */
3696 if (pinstance
->ioa_state
!= IOA_STATE_OPERATIONAL
) {
3697 pmcraid_err("IOA is not operational\n");
3701 buffer_size
= sizeof(struct pmcraid_passthrough_ioctl_buffer
);
3702 buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
3705 pmcraid_err("no memory for passthrough buffer\n");
3710 offsetof(struct pmcraid_passthrough_ioctl_buffer
, request_buffer
);
3712 request_buffer
= arg
+ request_offset
;
3714 rc
= __copy_from_user(buffer
,
3715 (struct pmcraid_passthrough_ioctl_buffer
*) arg
,
3716 sizeof(struct pmcraid_passthrough_ioctl_buffer
));
3720 offsetof(struct pmcraid_passthrough_ioctl_buffer
, ioasa
));
3723 pmcraid_err("ioctl: can't copy passthrough buffer\n");
3725 goto out_free_buffer
;
3728 request_size
= buffer
->ioarcb
.data_transfer_length
;
3730 if (buffer
->ioarcb
.request_flags0
& TRANSFER_DIR_WRITE
) {
3731 access
= VERIFY_READ
;
3732 direction
= DMA_TO_DEVICE
;
3734 access
= VERIFY_WRITE
;
3735 direction
= DMA_FROM_DEVICE
;
3738 if (request_size
> 0) {
3739 rc
= access_ok(access
, arg
, request_offset
+ request_size
);
3743 goto out_free_buffer
;
3745 } else if (request_size
< 0) {
3747 goto out_free_buffer
;
3750 /* check if we have any additional command parameters */
3751 if (buffer
->ioarcb
.add_cmd_param_length
> PMCRAID_ADD_CMD_PARAM_LEN
) {
3753 goto out_free_buffer
;
3756 cmd
= pmcraid_get_free_cmd(pinstance
);
3759 pmcraid_err("free command block is not available\n");
3761 goto out_free_buffer
;
3764 cmd
->scsi_cmd
= NULL
;
3765 ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
3767 /* Copy the user-provided IOARCB stuff field by field */
3768 ioarcb
->resource_handle
= buffer
->ioarcb
.resource_handle
;
3769 ioarcb
->data_transfer_length
= buffer
->ioarcb
.data_transfer_length
;
3770 ioarcb
->cmd_timeout
= buffer
->ioarcb
.cmd_timeout
;
3771 ioarcb
->request_type
= buffer
->ioarcb
.request_type
;
3772 ioarcb
->request_flags0
= buffer
->ioarcb
.request_flags0
;
3773 ioarcb
->request_flags1
= buffer
->ioarcb
.request_flags1
;
3774 memcpy(ioarcb
->cdb
, buffer
->ioarcb
.cdb
, PMCRAID_MAX_CDB_LEN
);
3776 if (buffer
->ioarcb
.add_cmd_param_length
) {
3777 ioarcb
->add_cmd_param_length
=
3778 buffer
->ioarcb
.add_cmd_param_length
;
3779 ioarcb
->add_cmd_param_offset
=
3780 buffer
->ioarcb
.add_cmd_param_offset
;
3781 memcpy(ioarcb
->add_data
.u
.add_cmd_params
,
3782 buffer
->ioarcb
.add_data
.u
.add_cmd_params
,
3783 buffer
->ioarcb
.add_cmd_param_length
);
3786 /* set hrrq number where the IOA should respond to. Note that all cmds
3787 * generated internally uses hrrq_id 0, exception to this is the cmd
3788 * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
3789 * hrrq_id assigned here in queuecommand
3791 ioarcb
->hrrq_id
= atomic_add_return(1, &(pinstance
->last_message_id
)) %
3792 pinstance
->num_hrrq
;
3795 rc
= pmcraid_build_passthrough_ioadls(cmd
,
3799 pmcraid_err("couldn't build passthrough ioadls\n");
3800 goto out_free_buffer
;
3802 } else if (request_size
< 0) {
3804 goto out_free_buffer
;
3807 /* If data is being written into the device, copy the data from user
3810 if (direction
== DMA_TO_DEVICE
&& request_size
> 0) {
3811 rc
= pmcraid_copy_sglist(cmd
->sglist
,
3816 pmcraid_err("failed to copy user buffer\n");
3817 goto out_free_sglist
;
3821 /* passthrough ioctl is a blocking command so, put the user to sleep
3822 * until timeout. Note that a timeout value of 0 means, do timeout.
3824 cmd
->cmd_done
= pmcraid_internal_done
;
3825 init_completion(&cmd
->wait_for_completion
);
3826 cmd
->completion_req
= 1;
3828 pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
3829 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2,
3830 cmd
->ioa_cb
->ioarcb
.cdb
[0],
3831 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.resource_handle
));
3833 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
3834 _pmcraid_fire_command(cmd
);
3835 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
3837 /* NOTE ! Remove the below line once abort_task is implemented
3838 * in firmware. This line disables ioctl command timeout handling logic
3839 * similar to IO command timeout handling, making ioctl commands to wait
3840 * until the command completion regardless of timeout value specified in
3843 buffer
->ioarcb
.cmd_timeout
= 0;
3845 /* If command timeout is specified put caller to wait till that time,
3846 * otherwise it would be blocking wait. If command gets timed out, it
3849 if (buffer
->ioarcb
.cmd_timeout
== 0) {
3850 wait_for_completion(&cmd
->wait_for_completion
);
3851 } else if (!wait_for_completion_timeout(
3852 &cmd
->wait_for_completion
,
3853 msecs_to_jiffies(buffer
->ioarcb
.cmd_timeout
* 1000))) {
3855 pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
3856 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
>> 2),
3857 cmd
->ioa_cb
->ioarcb
.cdb
[0]);
3859 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
3860 cancel_cmd
= pmcraid_abort_cmd(cmd
);
3861 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
3864 wait_for_completion(&cancel_cmd
->wait_for_completion
);
3865 ioasc
= cancel_cmd
->ioa_cb
->ioasa
.ioasc
;
3866 pmcraid_return_cmd(cancel_cmd
);
3868 /* if abort task couldn't find the command i.e it got
3869 * completed prior to aborting, return good completion.
3870 * if command got aborted successfully or there was IOA
3871 * reset due to abort task itself getting timedout then
3874 if (ioasc
== PMCRAID_IOASC_IOA_WAS_RESET
||
3875 PMCRAID_IOASC_SENSE_KEY(ioasc
) == 0x00) {
3876 if (ioasc
!= PMCRAID_IOASC_GC_IOARCB_NOTFOUND
)
3878 goto out_handle_response
;
3882 /* no command block for abort task or abort task failed to abort
3883 * the IOARCB, then wait for 150 more seconds and initiate reset
3884 * sequence after timeout
3886 if (!wait_for_completion_timeout(
3887 &cmd
->wait_for_completion
,
3888 msecs_to_jiffies(150 * 1000))) {
3889 pmcraid_reset_bringup(cmd
->drv_inst
);
3894 out_handle_response
:
3895 /* copy entire IOASA buffer and return IOCTL success.
3896 * If copying IOASA to user-buffer fails, return
3899 if (copy_to_user(ioasa
, &cmd
->ioa_cb
->ioasa
,
3900 sizeof(struct pmcraid_ioasa
))) {
3901 pmcraid_err("failed to copy ioasa buffer to user\n");
3905 /* If the data transfer was from device, copy the data onto user
3908 else if (direction
== DMA_FROM_DEVICE
&& request_size
> 0) {
3909 rc
= pmcraid_copy_sglist(cmd
->sglist
,
3914 pmcraid_err("failed to copy user buffer\n");
3920 pmcraid_release_passthrough_ioadls(cmd
, request_size
, direction
);
3921 pmcraid_return_cmd(cmd
);
3933 * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
3935 * @pinstance: pointer to adapter instance structure
3936 * @cmd: ioctl command passed in
3937 * @buflen: length of user_buffer
3938 * @user_buffer: user buffer pointer
3941 * 0 in case of success, otherwise appropriate error code
3943 static long pmcraid_ioctl_driver(
3944 struct pmcraid_instance
*pinstance
,
3946 unsigned int buflen
,
3947 void __user
*user_buffer
3952 if (!access_ok(VERIFY_READ
, user_buffer
, _IOC_SIZE(cmd
))) {
3953 pmcraid_err("ioctl_driver: access fault in request buffer\n");
3958 case PMCRAID_IOCTL_RESET_ADAPTER
:
3959 pmcraid_reset_bringup(pinstance
);
3971 * pmcraid_check_ioctl_buffer - check for proper access to user buffer
3973 * @cmd: ioctl command
3975 * @hdr: pointer to kernel memory for pmcraid_ioctl_header
3978 * negetive error code if there are access issues, otherwise zero.
3979 * Upon success, returns ioctl header copied out of user buffer.
3982 static int pmcraid_check_ioctl_buffer(
3985 struct pmcraid_ioctl_header
*hdr
3989 int access
= VERIFY_READ
;
3991 if (copy_from_user(hdr
, arg
, sizeof(struct pmcraid_ioctl_header
))) {
3992 pmcraid_err("couldn't copy ioctl header from user buffer\n");
3996 /* check for valid driver signature */
3997 rc
= memcmp(hdr
->signature
,
3998 PMCRAID_IOCTL_SIGNATURE
,
3999 sizeof(hdr
->signature
));
4001 pmcraid_err("signature verification failed\n");
4005 /* check for appropriate buffer access */
4006 if ((_IOC_DIR(cmd
) & _IOC_READ
) == _IOC_READ
)
4007 access
= VERIFY_WRITE
;
4009 rc
= access_ok(access
,
4010 (arg
+ sizeof(struct pmcraid_ioctl_header
)),
4011 hdr
->buffer_length
);
4013 pmcraid_err("access failed for user buffer of size %d\n",
4014 hdr
->buffer_length
);
4022 * pmcraid_ioctl - char node ioctl entry point
4024 static long pmcraid_chr_ioctl(
4030 struct pmcraid_instance
*pinstance
= NULL
;
4031 struct pmcraid_ioctl_header
*hdr
= NULL
;
4032 int retval
= -ENOTTY
;
4034 hdr
= kmalloc(sizeof(struct pmcraid_ioctl_header
), GFP_KERNEL
);
4037 pmcraid_err("failed to allocate memory for ioctl header\n");
4041 retval
= pmcraid_check_ioctl_buffer(cmd
, (void *)arg
, hdr
);
4044 pmcraid_info("chr_ioctl: header check failed\n");
4049 pinstance
= filep
->private_data
;
4052 pmcraid_info("adapter instance is not found\n");
4057 switch (_IOC_TYPE(cmd
)) {
4059 case PMCRAID_PASSTHROUGH_IOCTL
:
4060 /* If ioctl code is to download microcode, we need to block
4061 * mid-layer requests.
4063 if (cmd
== PMCRAID_IOCTL_DOWNLOAD_MICROCODE
)
4064 scsi_block_requests(pinstance
->host
);
4066 retval
= pmcraid_ioctl_passthrough(pinstance
,
4071 if (cmd
== PMCRAID_IOCTL_DOWNLOAD_MICROCODE
)
4072 scsi_unblock_requests(pinstance
->host
);
4075 case PMCRAID_DRIVER_IOCTL
:
4076 arg
+= sizeof(struct pmcraid_ioctl_header
);
4077 retval
= pmcraid_ioctl_driver(pinstance
,
4080 (void __user
*)arg
);
4094 * File operations structure for management interface
4096 static const struct file_operations pmcraid_fops
= {
4097 .owner
= THIS_MODULE
,
4098 .open
= pmcraid_chr_open
,
4099 .fasync
= pmcraid_chr_fasync
,
4100 .unlocked_ioctl
= pmcraid_chr_ioctl
,
4101 #ifdef CONFIG_COMPAT
4102 .compat_ioctl
= pmcraid_chr_ioctl
,
4104 .llseek
= noop_llseek
,
4111 * pmcraid_show_log_level - Display adapter's error logging level
4112 * @dev: class device struct
4116 * number of bytes printed to buffer
4118 static ssize_t
pmcraid_show_log_level(
4120 struct device_attribute
*attr
,
4123 struct Scsi_Host
*shost
= class_to_shost(dev
);
4124 struct pmcraid_instance
*pinstance
=
4125 (struct pmcraid_instance
*)shost
->hostdata
;
4126 return snprintf(buf
, PAGE_SIZE
, "%d\n", pinstance
->current_log_level
);
4130 * pmcraid_store_log_level - Change the adapter's error logging level
4131 * @dev: class device struct
4136 * number of bytes printed to buffer
4138 static ssize_t
pmcraid_store_log_level(
4140 struct device_attribute
*attr
,
4145 struct Scsi_Host
*shost
;
4146 struct pmcraid_instance
*pinstance
;
4149 if (kstrtou8(buf
, 10, &val
))
4151 /* log-level should be from 0 to 2 */
4155 shost
= class_to_shost(dev
);
4156 pinstance
= (struct pmcraid_instance
*)shost
->hostdata
;
4157 pinstance
->current_log_level
= val
;
4162 static struct device_attribute pmcraid_log_level_attr
= {
4164 .name
= "log_level",
4165 .mode
= S_IRUGO
| S_IWUSR
,
4167 .show
= pmcraid_show_log_level
,
4168 .store
= pmcraid_store_log_level
,
4172 * pmcraid_show_drv_version - Display driver version
4173 * @dev: class device struct
4177 * number of bytes printed to buffer
4179 static ssize_t
pmcraid_show_drv_version(
4181 struct device_attribute
*attr
,
4185 return snprintf(buf
, PAGE_SIZE
, "version: %s\n",
4186 PMCRAID_DRIVER_VERSION
);
4189 static struct device_attribute pmcraid_driver_version_attr
= {
4191 .name
= "drv_version",
4194 .show
= pmcraid_show_drv_version
,
4198 * pmcraid_show_io_adapter_id - Display driver assigned adapter id
4199 * @dev: class device struct
4203 * number of bytes printed to buffer
4205 static ssize_t
pmcraid_show_adapter_id(
4207 struct device_attribute
*attr
,
4211 struct Scsi_Host
*shost
= class_to_shost(dev
);
4212 struct pmcraid_instance
*pinstance
=
4213 (struct pmcraid_instance
*)shost
->hostdata
;
4214 u32 adapter_id
= (pinstance
->pdev
->bus
->number
<< 8) |
4215 pinstance
->pdev
->devfn
;
4216 u32 aen_group
= pmcraid_event_family
.id
;
4218 return snprintf(buf
, PAGE_SIZE
,
4219 "adapter id: %d\nminor: %d\naen group: %d\n",
4220 adapter_id
, MINOR(pinstance
->cdev
.dev
), aen_group
);
4223 static struct device_attribute pmcraid_adapter_id_attr
= {
4225 .name
= "adapter_id",
4226 .mode
= S_IRUGO
| S_IWUSR
,
4228 .show
= pmcraid_show_adapter_id
,
4231 static struct device_attribute
*pmcraid_host_attrs
[] = {
4232 &pmcraid_log_level_attr
,
4233 &pmcraid_driver_version_attr
,
4234 &pmcraid_adapter_id_attr
,
4239 /* host template structure for pmcraid driver */
4240 static struct scsi_host_template pmcraid_host_template
= {
4241 .module
= THIS_MODULE
,
4242 .name
= PMCRAID_DRIVER_NAME
,
4243 .queuecommand
= pmcraid_queuecommand
,
4244 .eh_abort_handler
= pmcraid_eh_abort_handler
,
4245 .eh_bus_reset_handler
= pmcraid_eh_bus_reset_handler
,
4246 .eh_target_reset_handler
= pmcraid_eh_target_reset_handler
,
4247 .eh_device_reset_handler
= pmcraid_eh_device_reset_handler
,
4248 .eh_host_reset_handler
= pmcraid_eh_host_reset_handler
,
4250 .slave_alloc
= pmcraid_slave_alloc
,
4251 .slave_configure
= pmcraid_slave_configure
,
4252 .slave_destroy
= pmcraid_slave_destroy
,
4253 .change_queue_depth
= pmcraid_change_queue_depth
,
4254 .change_queue_type
= scsi_change_queue_type
,
4255 .can_queue
= PMCRAID_MAX_IO_CMD
,
4257 .sg_tablesize
= PMCRAID_MAX_IOADLS
,
4258 .max_sectors
= PMCRAID_IOA_MAX_SECTORS
,
4260 .cmd_per_lun
= PMCRAID_MAX_CMD_PER_LUN
,
4261 .use_clustering
= ENABLE_CLUSTERING
,
4262 .shost_attrs
= pmcraid_host_attrs
,
4263 .proc_name
= PMCRAID_DRIVER_NAME
,
4268 * pmcraid_isr_msix - implements MSI-X interrupt handling routine
4269 * @irq: interrupt vector number
4270 * @dev_id: pointer hrrq_vector
4273 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4276 static irqreturn_t
pmcraid_isr_msix(int irq
, void *dev_id
)
4278 struct pmcraid_isr_param
*hrrq_vector
;
4279 struct pmcraid_instance
*pinstance
;
4280 unsigned long lock_flags
;
4284 hrrq_vector
= (struct pmcraid_isr_param
*)dev_id
;
4285 hrrq_id
= hrrq_vector
->hrrq_id
;
4286 pinstance
= hrrq_vector
->drv_inst
;
4289 /* Read the interrupt */
4290 intrs_val
= pmcraid_read_interrupts(pinstance
);
4292 ((ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
)
4293 & DOORBELL_INTR_MSIX_CLR
) == 0)) {
4294 /* Any error interrupts including unit_check,
4295 * initiate IOA reset.In case of unit check indicate
4296 * to reset_sequence that IOA unit checked and prepare
4297 * for a dump during reset sequence
4299 if (intrs_val
& PMCRAID_ERROR_INTERRUPTS
) {
4300 if (intrs_val
& INTRS_IOA_UNIT_CHECK
)
4301 pinstance
->ioa_unit_check
= 1;
4303 pmcraid_err("ISR: error interrupts: %x \
4304 initiating reset\n", intrs_val
);
4305 spin_lock_irqsave(pinstance
->host
->host_lock
,
4307 pmcraid_initiate_reset(pinstance
);
4308 spin_unlock_irqrestore(
4309 pinstance
->host
->host_lock
,
4312 /* If interrupt was as part of the ioa initialization,
4313 * clear it. Delete the timer and wakeup the
4314 * reset engine to proceed with reset sequence
4316 if (intrs_val
& INTRS_TRANSITION_TO_OPERATIONAL
)
4317 pmcraid_clr_trans_op(pinstance
);
4319 /* Clear the interrupt register by writing
4320 * to host to ioa doorbell. Once done
4321 * FW will clear the interrupt.
4323 iowrite32(DOORBELL_INTR_MSIX_CLR
,
4324 pinstance
->int_regs
.host_ioa_interrupt_reg
);
4325 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
4331 tasklet_schedule(&(pinstance
->isr_tasklet
[hrrq_id
]));
4337 * pmcraid_isr - implements legacy interrupt handling routine
4339 * @irq: interrupt vector number
4340 * @dev_id: pointer hrrq_vector
4343 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4345 static irqreturn_t
pmcraid_isr(int irq
, void *dev_id
)
4347 struct pmcraid_isr_param
*hrrq_vector
;
4348 struct pmcraid_instance
*pinstance
;
4350 unsigned long lock_flags
;
4353 /* In case of legacy interrupt mode where interrupts are shared across
4354 * isrs, it may be possible that the current interrupt is not from IOA
4357 printk(KERN_INFO
"%s(): NULL host pointer\n", __func__
);
4360 hrrq_vector
= (struct pmcraid_isr_param
*)dev_id
;
4361 pinstance
= hrrq_vector
->drv_inst
;
4363 intrs
= pmcraid_read_interrupts(pinstance
);
4365 if (unlikely((intrs
& PMCRAID_PCI_INTERRUPTS
) == 0))
4368 /* Any error interrupts including unit_check, initiate IOA reset.
4369 * In case of unit check indicate to reset_sequence that IOA unit
4370 * checked and prepare for a dump during reset sequence
4372 if (intrs
& PMCRAID_ERROR_INTERRUPTS
) {
4374 if (intrs
& INTRS_IOA_UNIT_CHECK
)
4375 pinstance
->ioa_unit_check
= 1;
4378 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4379 pmcraid_err("ISR: error interrupts: %x initiating reset\n",
4382 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4383 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
4384 pmcraid_initiate_reset(pinstance
);
4385 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
4387 /* If interrupt was as part of the ioa initialization,
4388 * clear. Delete the timer and wakeup the
4389 * reset engine to proceed with reset sequence
4391 if (intrs
& INTRS_TRANSITION_TO_OPERATIONAL
) {
4392 pmcraid_clr_trans_op(pinstance
);
4395 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4397 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4400 &(pinstance
->isr_tasklet
[hrrq_id
]));
4409 * pmcraid_worker_function - worker thread function
4411 * @workp: pointer to struct work queue
4417 static void pmcraid_worker_function(struct work_struct
*workp
)
4419 struct pmcraid_instance
*pinstance
;
4420 struct pmcraid_resource_entry
*res
;
4421 struct pmcraid_resource_entry
*temp
;
4422 struct scsi_device
*sdev
;
4423 unsigned long lock_flags
;
4424 unsigned long host_lock_flags
;
4426 u8 bus
, target
, lun
;
4428 pinstance
= container_of(workp
, struct pmcraid_instance
, worker_q
);
4429 /* add resources only after host is added into system */
4430 if (!atomic_read(&pinstance
->expose_resources
))
4433 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
4435 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
4436 list_for_each_entry_safe(res
, temp
, &pinstance
->used_res_q
, queue
) {
4438 if (res
->change_detected
== RES_CHANGE_DEL
&& res
->scsi_dev
) {
4439 sdev
= res
->scsi_dev
;
4441 /* host_lock must be held before calling
4444 spin_lock_irqsave(pinstance
->host
->host_lock
,
4446 if (!scsi_device_get(sdev
)) {
4447 spin_unlock_irqrestore(
4448 pinstance
->host
->host_lock
,
4450 pmcraid_info("deleting %x from midlayer\n",
4451 res
->cfg_entry
.resource_address
);
4452 list_move_tail(&res
->queue
,
4453 &pinstance
->free_res_q
);
4454 spin_unlock_irqrestore(
4455 &pinstance
->resource_lock
,
4457 scsi_remove_device(sdev
);
4458 scsi_device_put(sdev
);
4459 spin_lock_irqsave(&pinstance
->resource_lock
,
4461 res
->change_detected
= 0;
4463 spin_unlock_irqrestore(
4464 pinstance
->host
->host_lock
,
4470 list_for_each_entry(res
, &pinstance
->used_res_q
, queue
) {
4472 if (res
->change_detected
== RES_CHANGE_ADD
) {
4474 if (!pmcraid_expose_resource(fw_version
,
4478 if (RES_IS_VSET(res
->cfg_entry
)) {
4479 bus
= PMCRAID_VSET_BUS_ID
;
4480 if (fw_version
<= PMCRAID_FW_VERSION_1
)
4481 target
= res
->cfg_entry
.unique_flags1
;
4483 target
= res
->cfg_entry
.array_id
& 0xFF;
4484 lun
= PMCRAID_VSET_LUN_ID
;
4486 bus
= PMCRAID_PHYS_BUS_ID
;
4489 res
->cfg_entry
.resource_address
);
4490 lun
= RES_LUN(res
->cfg_entry
.resource_address
);
4493 res
->change_detected
= 0;
4494 spin_unlock_irqrestore(&pinstance
->resource_lock
,
4496 scsi_add_device(pinstance
->host
, bus
, target
, lun
);
4497 spin_lock_irqsave(&pinstance
->resource_lock
,
4502 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
4506 * pmcraid_tasklet_function - Tasklet function
4508 * @instance: pointer to msix param structure
4513 static void pmcraid_tasklet_function(unsigned long instance
)
4515 struct pmcraid_isr_param
*hrrq_vector
;
4516 struct pmcraid_instance
*pinstance
;
4517 unsigned long hrrq_lock_flags
;
4518 unsigned long pending_lock_flags
;
4519 unsigned long host_lock_flags
;
4520 spinlock_t
*lockp
; /* hrrq buffer lock */
4524 hrrq_vector
= (struct pmcraid_isr_param
*)instance
;
4525 pinstance
= hrrq_vector
->drv_inst
;
4526 id
= hrrq_vector
->hrrq_id
;
4527 lockp
= &(pinstance
->hrrq_lock
[id
]);
4529 /* loop through each of the commands responded by IOA. Each HRRQ buf is
4530 * protected by its own lock. Traversals must be done within this lock
4531 * as there may be multiple tasklets running on multiple CPUs. Note
4532 * that the lock is held just for picking up the response handle and
4533 * manipulating hrrq_curr/toggle_bit values.
4535 spin_lock_irqsave(lockp
, hrrq_lock_flags
);
4537 resp
= le32_to_cpu(*(pinstance
->hrrq_curr
[id
]));
4539 while ((resp
& HRRQ_TOGGLE_BIT
) ==
4540 pinstance
->host_toggle_bit
[id
]) {
4542 int cmd_index
= resp
>> 2;
4543 struct pmcraid_cmd
*cmd
= NULL
;
4545 if (pinstance
->hrrq_curr
[id
] < pinstance
->hrrq_end
[id
]) {
4546 pinstance
->hrrq_curr
[id
]++;
4548 pinstance
->hrrq_curr
[id
] = pinstance
->hrrq_start
[id
];
4549 pinstance
->host_toggle_bit
[id
] ^= 1u;
4552 if (cmd_index
>= PMCRAID_MAX_CMD
) {
4553 /* In case of invalid response handle, log message */
4554 pmcraid_err("Invalid response handle %d\n", cmd_index
);
4555 resp
= le32_to_cpu(*(pinstance
->hrrq_curr
[id
]));
4559 cmd
= pinstance
->cmd_list
[cmd_index
];
4560 spin_unlock_irqrestore(lockp
, hrrq_lock_flags
);
4562 spin_lock_irqsave(&pinstance
->pending_pool_lock
,
4563 pending_lock_flags
);
4564 list_del(&cmd
->free_list
);
4565 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
,
4566 pending_lock_flags
);
4567 del_timer(&cmd
->timer
);
4568 atomic_dec(&pinstance
->outstanding_cmds
);
4570 if (cmd
->cmd_done
== pmcraid_ioa_reset
) {
4571 spin_lock_irqsave(pinstance
->host
->host_lock
,
4574 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
4576 } else if (cmd
->cmd_done
!= NULL
) {
4579 /* loop over until we are done with all responses */
4580 spin_lock_irqsave(lockp
, hrrq_lock_flags
);
4581 resp
= le32_to_cpu(*(pinstance
->hrrq_curr
[id
]));
4584 spin_unlock_irqrestore(lockp
, hrrq_lock_flags
);
4588 * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
4589 * @pinstance: pointer to adapter instance structure
4591 * This routine un-registers registered interrupt handler and
4592 * also frees irqs/vectors.
4598 void pmcraid_unregister_interrupt_handler(struct pmcraid_instance
*pinstance
)
4602 for (i
= 0; i
< pinstance
->num_hrrq
; i
++)
4603 free_irq(pinstance
->hrrq_vector
[i
].vector
,
4604 &(pinstance
->hrrq_vector
[i
]));
4606 if (pinstance
->interrupt_mode
) {
4607 pci_disable_msix(pinstance
->pdev
);
4608 pinstance
->interrupt_mode
= 0;
4613 * pmcraid_register_interrupt_handler - registers interrupt handler
4614 * @pinstance: pointer to per-adapter instance structure
4617 * 0 on success, non-zero error code otherwise.
4620 pmcraid_register_interrupt_handler(struct pmcraid_instance
*pinstance
)
4623 struct pci_dev
*pdev
= pinstance
->pdev
;
4625 if ((pmcraid_enable_msix
) &&
4626 (pci_find_capability(pdev
, PCI_CAP_ID_MSIX
))) {
4627 int num_hrrq
= PMCRAID_NUM_MSIX_VECTORS
;
4628 struct msix_entry entries
[PMCRAID_NUM_MSIX_VECTORS
];
4630 for (i
= 0; i
< PMCRAID_NUM_MSIX_VECTORS
; i
++)
4631 entries
[i
].entry
= i
;
4633 num_hrrq
= pci_enable_msix_range(pdev
, entries
, 1, num_hrrq
);
4635 goto pmcraid_isr_legacy
;
4637 for (i
= 0; i
< num_hrrq
; i
++) {
4638 pinstance
->hrrq_vector
[i
].hrrq_id
= i
;
4639 pinstance
->hrrq_vector
[i
].drv_inst
= pinstance
;
4640 pinstance
->hrrq_vector
[i
].vector
= entries
[i
].vector
;
4641 rc
= request_irq(pinstance
->hrrq_vector
[i
].vector
,
4642 pmcraid_isr_msix
, 0,
4643 PMCRAID_DRIVER_NAME
,
4644 &(pinstance
->hrrq_vector
[i
]));
4648 for (j
= 0; j
< i
; j
++)
4649 free_irq(entries
[j
].vector
,
4650 &(pinstance
->hrrq_vector
[j
]));
4651 pci_disable_msix(pdev
);
4652 goto pmcraid_isr_legacy
;
4656 pinstance
->num_hrrq
= num_hrrq
;
4657 pinstance
->interrupt_mode
= 1;
4658 iowrite32(DOORBELL_INTR_MODE_MSIX
,
4659 pinstance
->int_regs
.host_ioa_interrupt_reg
);
4660 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
4661 goto pmcraid_isr_out
;
4665 /* If MSI-X registration failed fallback to legacy mode, where
4666 * only one hrrq entry will be used
4668 pinstance
->hrrq_vector
[0].hrrq_id
= 0;
4669 pinstance
->hrrq_vector
[0].drv_inst
= pinstance
;
4670 pinstance
->hrrq_vector
[0].vector
= pdev
->irq
;
4671 pinstance
->num_hrrq
= 1;
4673 rc
= request_irq(pdev
->irq
, pmcraid_isr
, IRQF_SHARED
,
4674 PMCRAID_DRIVER_NAME
, &pinstance
->hrrq_vector
[0]);
4680 * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
4681 * @pinstance: per adapter instance structure pointer
4682 * @max_index: number of buffer blocks to release
4688 pmcraid_release_cmd_blocks(struct pmcraid_instance
*pinstance
, int max_index
)
4691 for (i
= 0; i
< max_index
; i
++) {
4692 kmem_cache_free(pinstance
->cmd_cachep
, pinstance
->cmd_list
[i
]);
4693 pinstance
->cmd_list
[i
] = NULL
;
4695 kmem_cache_destroy(pinstance
->cmd_cachep
);
4696 pinstance
->cmd_cachep
= NULL
;
4700 * pmcraid_release_control_blocks - releases buffers alloced for control blocks
4701 * @pinstance: pointer to per adapter instance structure
4702 * @max_index: number of buffers (from 0 onwards) to release
4704 * This function assumes that the command blocks for which control blocks are
4705 * linked are not released.
4711 pmcraid_release_control_blocks(
4712 struct pmcraid_instance
*pinstance
,
4718 if (pinstance
->control_pool
== NULL
)
4721 for (i
= 0; i
< max_index
; i
++) {
4722 pci_pool_free(pinstance
->control_pool
,
4723 pinstance
->cmd_list
[i
]->ioa_cb
,
4724 pinstance
->cmd_list
[i
]->ioa_cb_bus_addr
);
4725 pinstance
->cmd_list
[i
]->ioa_cb
= NULL
;
4726 pinstance
->cmd_list
[i
]->ioa_cb_bus_addr
= 0;
4728 pci_pool_destroy(pinstance
->control_pool
);
4729 pinstance
->control_pool
= NULL
;
4733 * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
4734 * @pinstance - pointer to per adapter instance structure
4736 * Allocates memory for command blocks using kernel slab allocator.
4739 * 0 in case of success; -ENOMEM in case of failure
4741 static int pmcraid_allocate_cmd_blocks(struct pmcraid_instance
*pinstance
)
4745 sprintf(pinstance
->cmd_pool_name
, "pmcraid_cmd_pool_%d",
4746 pinstance
->host
->unique_id
);
4749 pinstance
->cmd_cachep
= kmem_cache_create(
4750 pinstance
->cmd_pool_name
,
4751 sizeof(struct pmcraid_cmd
), 0,
4752 SLAB_HWCACHE_ALIGN
, NULL
);
4753 if (!pinstance
->cmd_cachep
)
4756 for (i
= 0; i
< PMCRAID_MAX_CMD
; i
++) {
4757 pinstance
->cmd_list
[i
] =
4758 kmem_cache_alloc(pinstance
->cmd_cachep
, GFP_KERNEL
);
4759 if (!pinstance
->cmd_list
[i
]) {
4760 pmcraid_release_cmd_blocks(pinstance
, i
);
4768 * pmcraid_allocate_control_blocks - allocates memory control blocks
4769 * @pinstance : pointer to per adapter instance structure
4771 * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
4772 * and IOASAs. This is called after command blocks are already allocated.
4775 * 0 in case it can allocate all control blocks, otherwise -ENOMEM
4777 static int pmcraid_allocate_control_blocks(struct pmcraid_instance
*pinstance
)
4781 sprintf(pinstance
->ctl_pool_name
, "pmcraid_control_pool_%d",
4782 pinstance
->host
->unique_id
);
4784 pinstance
->control_pool
=
4785 pci_pool_create(pinstance
->ctl_pool_name
,
4787 sizeof(struct pmcraid_control_block
),
4788 PMCRAID_IOARCB_ALIGNMENT
, 0);
4790 if (!pinstance
->control_pool
)
4793 for (i
= 0; i
< PMCRAID_MAX_CMD
; i
++) {
4794 pinstance
->cmd_list
[i
]->ioa_cb
=
4796 pinstance
->control_pool
,
4798 &(pinstance
->cmd_list
[i
]->ioa_cb_bus_addr
));
4800 if (!pinstance
->cmd_list
[i
]->ioa_cb
) {
4801 pmcraid_release_control_blocks(pinstance
, i
);
4804 memset(pinstance
->cmd_list
[i
]->ioa_cb
, 0,
4805 sizeof(struct pmcraid_control_block
));
4811 * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
4812 * @pinstance: pointer to per adapter instance structure
4813 * @maxindex: size of hrrq buffer pointer array
4819 pmcraid_release_host_rrqs(struct pmcraid_instance
*pinstance
, int maxindex
)
4822 for (i
= 0; i
< maxindex
; i
++) {
4824 pci_free_consistent(pinstance
->pdev
,
4825 HRRQ_ENTRY_SIZE
* PMCRAID_MAX_CMD
,
4826 pinstance
->hrrq_start
[i
],
4827 pinstance
->hrrq_start_bus_addr
[i
]);
4829 /* reset pointers and toggle bit to zeros */
4830 pinstance
->hrrq_start
[i
] = NULL
;
4831 pinstance
->hrrq_start_bus_addr
[i
] = 0;
4832 pinstance
->host_toggle_bit
[i
] = 0;
4837 * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
4838 * @pinstance: pointer to per adapter instance structure
4841 * 0 hrrq buffers are allocated, -ENOMEM otherwise.
4843 static int pmcraid_allocate_host_rrqs(struct pmcraid_instance
*pinstance
)
4847 buffer_size
= HRRQ_ENTRY_SIZE
* PMCRAID_MAX_CMD
;
4849 for (i
= 0; i
< pinstance
->num_hrrq
; i
++) {
4850 pinstance
->hrrq_start
[i
] =
4851 pci_alloc_consistent(
4854 &(pinstance
->hrrq_start_bus_addr
[i
]));
4856 if (pinstance
->hrrq_start
[i
] == 0) {
4857 pmcraid_err("pci_alloc failed for hrrq vector : %d\n",
4859 pmcraid_release_host_rrqs(pinstance
, i
);
4863 memset(pinstance
->hrrq_start
[i
], 0, buffer_size
);
4864 pinstance
->hrrq_curr
[i
] = pinstance
->hrrq_start
[i
];
4865 pinstance
->hrrq_end
[i
] =
4866 pinstance
->hrrq_start
[i
] + PMCRAID_MAX_CMD
- 1;
4867 pinstance
->host_toggle_bit
[i
] = 1;
4868 spin_lock_init(&pinstance
->hrrq_lock
[i
]);
4874 * pmcraid_release_hcams - release HCAM buffers
4876 * @pinstance: pointer to per adapter instance structure
4881 static void pmcraid_release_hcams(struct pmcraid_instance
*pinstance
)
4883 if (pinstance
->ccn
.msg
!= NULL
) {
4884 pci_free_consistent(pinstance
->pdev
,
4885 PMCRAID_AEN_HDR_SIZE
+
4886 sizeof(struct pmcraid_hcam_ccn_ext
),
4888 pinstance
->ccn
.baddr
);
4890 pinstance
->ccn
.msg
= NULL
;
4891 pinstance
->ccn
.hcam
= NULL
;
4892 pinstance
->ccn
.baddr
= 0;
4895 if (pinstance
->ldn
.msg
!= NULL
) {
4896 pci_free_consistent(pinstance
->pdev
,
4897 PMCRAID_AEN_HDR_SIZE
+
4898 sizeof(struct pmcraid_hcam_ldn
),
4900 pinstance
->ldn
.baddr
);
4902 pinstance
->ldn
.msg
= NULL
;
4903 pinstance
->ldn
.hcam
= NULL
;
4904 pinstance
->ldn
.baddr
= 0;
4909 * pmcraid_allocate_hcams - allocates HCAM buffers
4910 * @pinstance : pointer to per adapter instance structure
4913 * 0 in case of successful allocation, non-zero otherwise
4915 static int pmcraid_allocate_hcams(struct pmcraid_instance
*pinstance
)
4917 pinstance
->ccn
.msg
= pci_alloc_consistent(
4919 PMCRAID_AEN_HDR_SIZE
+
4920 sizeof(struct pmcraid_hcam_ccn_ext
),
4921 &(pinstance
->ccn
.baddr
));
4923 pinstance
->ldn
.msg
= pci_alloc_consistent(
4925 PMCRAID_AEN_HDR_SIZE
+
4926 sizeof(struct pmcraid_hcam_ldn
),
4927 &(pinstance
->ldn
.baddr
));
4929 if (pinstance
->ldn
.msg
== NULL
|| pinstance
->ccn
.msg
== NULL
) {
4930 pmcraid_release_hcams(pinstance
);
4932 pinstance
->ccn
.hcam
=
4933 (void *)pinstance
->ccn
.msg
+ PMCRAID_AEN_HDR_SIZE
;
4934 pinstance
->ldn
.hcam
=
4935 (void *)pinstance
->ldn
.msg
+ PMCRAID_AEN_HDR_SIZE
;
4937 atomic_set(&pinstance
->ccn
.ignore
, 0);
4938 atomic_set(&pinstance
->ldn
.ignore
, 0);
4941 return (pinstance
->ldn
.msg
== NULL
) ? -ENOMEM
: 0;
4945 * pmcraid_release_config_buffers - release config.table buffers
4946 * @pinstance: pointer to per adapter instance structure
4951 static void pmcraid_release_config_buffers(struct pmcraid_instance
*pinstance
)
4953 if (pinstance
->cfg_table
!= NULL
&&
4954 pinstance
->cfg_table_bus_addr
!= 0) {
4955 pci_free_consistent(pinstance
->pdev
,
4956 sizeof(struct pmcraid_config_table
),
4957 pinstance
->cfg_table
,
4958 pinstance
->cfg_table_bus_addr
);
4959 pinstance
->cfg_table
= NULL
;
4960 pinstance
->cfg_table_bus_addr
= 0;
4963 if (pinstance
->res_entries
!= NULL
) {
4966 for (i
= 0; i
< PMCRAID_MAX_RESOURCES
; i
++)
4967 list_del(&pinstance
->res_entries
[i
].queue
);
4968 kfree(pinstance
->res_entries
);
4969 pinstance
->res_entries
= NULL
;
4972 pmcraid_release_hcams(pinstance
);
4976 * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
4977 * @pinstance : pointer to per adapter instance structure
4980 * 0 for successful allocation, -ENOMEM for any failure
4982 static int pmcraid_allocate_config_buffers(struct pmcraid_instance
*pinstance
)
4986 pinstance
->res_entries
=
4987 kzalloc(sizeof(struct pmcraid_resource_entry
) *
4988 PMCRAID_MAX_RESOURCES
, GFP_KERNEL
);
4990 if (NULL
== pinstance
->res_entries
) {
4991 pmcraid_err("failed to allocate memory for resource table\n");
4995 for (i
= 0; i
< PMCRAID_MAX_RESOURCES
; i
++)
4996 list_add_tail(&pinstance
->res_entries
[i
].queue
,
4997 &pinstance
->free_res_q
);
4999 pinstance
->cfg_table
=
5000 pci_alloc_consistent(pinstance
->pdev
,
5001 sizeof(struct pmcraid_config_table
),
5002 &pinstance
->cfg_table_bus_addr
);
5004 if (NULL
== pinstance
->cfg_table
) {
5005 pmcraid_err("couldn't alloc DMA memory for config table\n");
5006 pmcraid_release_config_buffers(pinstance
);
5010 if (pmcraid_allocate_hcams(pinstance
)) {
5011 pmcraid_err("could not alloc DMA memory for HCAMS\n");
5012 pmcraid_release_config_buffers(pinstance
);
5020 * pmcraid_init_tasklets - registers tasklets for response handling
5022 * @pinstance: pointer adapter instance structure
5027 static void pmcraid_init_tasklets(struct pmcraid_instance
*pinstance
)
5030 for (i
= 0; i
< pinstance
->num_hrrq
; i
++)
5031 tasklet_init(&pinstance
->isr_tasklet
[i
],
5032 pmcraid_tasklet_function
,
5033 (unsigned long)&pinstance
->hrrq_vector
[i
]);
5037 * pmcraid_kill_tasklets - destroys tasklets registered for response handling
5039 * @pinstance: pointer to adapter instance structure
5044 static void pmcraid_kill_tasklets(struct pmcraid_instance
*pinstance
)
5047 for (i
= 0; i
< pinstance
->num_hrrq
; i
++)
5048 tasklet_kill(&pinstance
->isr_tasklet
[i
]);
5052 * pmcraid_release_buffers - release per-adapter buffers allocated
5054 * @pinstance: pointer to adapter soft state
5059 static void pmcraid_release_buffers(struct pmcraid_instance
*pinstance
)
5061 pmcraid_release_config_buffers(pinstance
);
5062 pmcraid_release_control_blocks(pinstance
, PMCRAID_MAX_CMD
);
5063 pmcraid_release_cmd_blocks(pinstance
, PMCRAID_MAX_CMD
);
5064 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
5066 if (pinstance
->inq_data
!= NULL
) {
5067 pci_free_consistent(pinstance
->pdev
,
5068 sizeof(struct pmcraid_inquiry_data
),
5069 pinstance
->inq_data
,
5070 pinstance
->inq_data_baddr
);
5072 pinstance
->inq_data
= NULL
;
5073 pinstance
->inq_data_baddr
= 0;
5076 if (pinstance
->timestamp_data
!= NULL
) {
5077 pci_free_consistent(pinstance
->pdev
,
5078 sizeof(struct pmcraid_timestamp_data
),
5079 pinstance
->timestamp_data
,
5080 pinstance
->timestamp_data_baddr
);
5082 pinstance
->timestamp_data
= NULL
;
5083 pinstance
->timestamp_data_baddr
= 0;
5088 * pmcraid_init_buffers - allocates memory and initializes various structures
5089 * @pinstance: pointer to per adapter instance structure
5091 * This routine pre-allocates memory based on the type of block as below:
5092 * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
5093 * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
5094 * config-table entries : DMAable memory using pci_alloc_consistent
5095 * HostRRQs : DMAable memory, using pci_alloc_consistent
5098 * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
5100 static int pmcraid_init_buffers(struct pmcraid_instance
*pinstance
)
5104 if (pmcraid_allocate_host_rrqs(pinstance
)) {
5105 pmcraid_err("couldn't allocate memory for %d host rrqs\n",
5106 pinstance
->num_hrrq
);
5110 if (pmcraid_allocate_config_buffers(pinstance
)) {
5111 pmcraid_err("couldn't allocate memory for config buffers\n");
5112 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
5116 if (pmcraid_allocate_cmd_blocks(pinstance
)) {
5117 pmcraid_err("couldn't allocate memory for cmd blocks\n");
5118 pmcraid_release_config_buffers(pinstance
);
5119 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
5123 if (pmcraid_allocate_control_blocks(pinstance
)) {
5124 pmcraid_err("couldn't allocate memory control blocks\n");
5125 pmcraid_release_config_buffers(pinstance
);
5126 pmcraid_release_cmd_blocks(pinstance
, PMCRAID_MAX_CMD
);
5127 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
5131 /* allocate DMAable memory for page D0 INQUIRY buffer */
5132 pinstance
->inq_data
= pci_alloc_consistent(
5134 sizeof(struct pmcraid_inquiry_data
),
5135 &pinstance
->inq_data_baddr
);
5137 if (pinstance
->inq_data
== NULL
) {
5138 pmcraid_err("couldn't allocate DMA memory for INQUIRY\n");
5139 pmcraid_release_buffers(pinstance
);
5143 /* allocate DMAable memory for set timestamp data buffer */
5144 pinstance
->timestamp_data
= pci_alloc_consistent(
5146 sizeof(struct pmcraid_timestamp_data
),
5147 &pinstance
->timestamp_data_baddr
);
5149 if (pinstance
->timestamp_data
== NULL
) {
5150 pmcraid_err("couldn't allocate DMA memory for \
5151 set time_stamp \n");
5152 pmcraid_release_buffers(pinstance
);
5157 /* Initialize all the command blocks and add them to free pool. No
5158 * need to lock (free_pool_lock) as this is done in initialization
5161 for (i
= 0; i
< PMCRAID_MAX_CMD
; i
++) {
5162 struct pmcraid_cmd
*cmdp
= pinstance
->cmd_list
[i
];
5163 pmcraid_init_cmdblk(cmdp
, i
);
5164 cmdp
->drv_inst
= pinstance
;
5165 list_add_tail(&cmdp
->free_list
, &pinstance
->free_cmd_pool
);
5172 * pmcraid_reinit_buffers - resets various buffer pointers
5173 * @pinstance: pointer to adapter instance
5177 static void pmcraid_reinit_buffers(struct pmcraid_instance
*pinstance
)
5180 int buffer_size
= HRRQ_ENTRY_SIZE
* PMCRAID_MAX_CMD
;
5182 for (i
= 0; i
< pinstance
->num_hrrq
; i
++) {
5183 memset(pinstance
->hrrq_start
[i
], 0, buffer_size
);
5184 pinstance
->hrrq_curr
[i
] = pinstance
->hrrq_start
[i
];
5185 pinstance
->hrrq_end
[i
] =
5186 pinstance
->hrrq_start
[i
] + PMCRAID_MAX_CMD
- 1;
5187 pinstance
->host_toggle_bit
[i
] = 1;
5192 * pmcraid_init_instance - initialize per instance data structure
5193 * @pdev: pointer to pci device structure
5194 * @host: pointer to Scsi_Host structure
5195 * @mapped_pci_addr: memory mapped IOA configuration registers
5198 * 0 on success, non-zero in case of any failure
5200 static int pmcraid_init_instance(struct pci_dev
*pdev
, struct Scsi_Host
*host
,
5201 void __iomem
*mapped_pci_addr
)
5203 struct pmcraid_instance
*pinstance
=
5204 (struct pmcraid_instance
*)host
->hostdata
;
5206 pinstance
->host
= host
;
5207 pinstance
->pdev
= pdev
;
5209 /* Initialize register addresses */
5210 pinstance
->mapped_dma_addr
= mapped_pci_addr
;
5212 /* Initialize chip-specific details */
5214 struct pmcraid_chip_details
*chip_cfg
= pinstance
->chip_cfg
;
5215 struct pmcraid_interrupts
*pint_regs
= &pinstance
->int_regs
;
5217 pinstance
->ioarrin
= mapped_pci_addr
+ chip_cfg
->ioarrin
;
5219 pint_regs
->ioa_host_interrupt_reg
=
5220 mapped_pci_addr
+ chip_cfg
->ioa_host_intr
;
5221 pint_regs
->ioa_host_interrupt_clr_reg
=
5222 mapped_pci_addr
+ chip_cfg
->ioa_host_intr_clr
;
5223 pint_regs
->ioa_host_msix_interrupt_reg
=
5224 mapped_pci_addr
+ chip_cfg
->ioa_host_msix_intr
;
5225 pint_regs
->host_ioa_interrupt_reg
=
5226 mapped_pci_addr
+ chip_cfg
->host_ioa_intr
;
5227 pint_regs
->host_ioa_interrupt_clr_reg
=
5228 mapped_pci_addr
+ chip_cfg
->host_ioa_intr_clr
;
5230 /* Current version of firmware exposes interrupt mask set
5231 * and mask clr registers through memory mapped bar0.
5233 pinstance
->mailbox
= mapped_pci_addr
+ chip_cfg
->mailbox
;
5234 pinstance
->ioa_status
= mapped_pci_addr
+ chip_cfg
->ioastatus
;
5235 pint_regs
->ioa_host_interrupt_mask_reg
=
5236 mapped_pci_addr
+ chip_cfg
->ioa_host_mask
;
5237 pint_regs
->ioa_host_interrupt_mask_clr_reg
=
5238 mapped_pci_addr
+ chip_cfg
->ioa_host_mask_clr
;
5239 pint_regs
->global_interrupt_mask_reg
=
5240 mapped_pci_addr
+ chip_cfg
->global_intr_mask
;
5243 pinstance
->ioa_reset_attempts
= 0;
5244 init_waitqueue_head(&pinstance
->reset_wait_q
);
5246 atomic_set(&pinstance
->outstanding_cmds
, 0);
5247 atomic_set(&pinstance
->last_message_id
, 0);
5248 atomic_set(&pinstance
->expose_resources
, 0);
5250 INIT_LIST_HEAD(&pinstance
->free_res_q
);
5251 INIT_LIST_HEAD(&pinstance
->used_res_q
);
5252 INIT_LIST_HEAD(&pinstance
->free_cmd_pool
);
5253 INIT_LIST_HEAD(&pinstance
->pending_cmd_pool
);
5255 spin_lock_init(&pinstance
->free_pool_lock
);
5256 spin_lock_init(&pinstance
->pending_pool_lock
);
5257 spin_lock_init(&pinstance
->resource_lock
);
5258 mutex_init(&pinstance
->aen_queue_lock
);
5260 /* Work-queue (Shared) for deferred processing error handling */
5261 INIT_WORK(&pinstance
->worker_q
, pmcraid_worker_function
);
5263 /* Initialize the default log_level */
5264 pinstance
->current_log_level
= pmcraid_log_level
;
5266 /* Setup variables required for reset engine */
5267 pinstance
->ioa_state
= IOA_STATE_UNKNOWN
;
5268 pinstance
->reset_cmd
= NULL
;
5273 * pmcraid_shutdown - shutdown adapter controller.
5274 * @pdev: pci device struct
5276 * Issues an adapter shutdown to the card waits for its completion
5281 static void pmcraid_shutdown(struct pci_dev
*pdev
)
5283 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5284 pmcraid_reset_bringdown(pinstance
);
5289 * pmcraid_get_minor - returns unused minor number from minor number bitmap
5291 static unsigned short pmcraid_get_minor(void)
5295 minor
= find_first_zero_bit(pmcraid_minor
, sizeof(pmcraid_minor
));
5296 __set_bit(minor
, pmcraid_minor
);
5301 * pmcraid_release_minor - releases given minor back to minor number bitmap
5303 static void pmcraid_release_minor(unsigned short minor
)
5305 __clear_bit(minor
, pmcraid_minor
);
5309 * pmcraid_setup_chrdev - allocates a minor number and registers a char device
5311 * @pinstance: pointer to adapter instance for which to register device
5314 * 0 in case of success, otherwise non-zero
5316 static int pmcraid_setup_chrdev(struct pmcraid_instance
*pinstance
)
5321 minor
= pmcraid_get_minor();
5322 cdev_init(&pinstance
->cdev
, &pmcraid_fops
);
5323 pinstance
->cdev
.owner
= THIS_MODULE
;
5325 error
= cdev_add(&pinstance
->cdev
, MKDEV(pmcraid_major
, minor
), 1);
5328 pmcraid_release_minor(minor
);
5330 device_create(pmcraid_class
, NULL
, MKDEV(pmcraid_major
, minor
),
5331 NULL
, "%s%u", PMCRAID_DEVFILE
, minor
);
5336 * pmcraid_release_chrdev - unregisters per-adapter management interface
5338 * @pinstance: pointer to adapter instance structure
5343 static void pmcraid_release_chrdev(struct pmcraid_instance
*pinstance
)
5345 pmcraid_release_minor(MINOR(pinstance
->cdev
.dev
));
5346 device_destroy(pmcraid_class
,
5347 MKDEV(pmcraid_major
, MINOR(pinstance
->cdev
.dev
)));
5348 cdev_del(&pinstance
->cdev
);
5352 * pmcraid_remove - IOA hot plug remove entry point
5353 * @pdev: pci device struct
5358 static void pmcraid_remove(struct pci_dev
*pdev
)
5360 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5362 /* remove the management interface (/dev file) for this device */
5363 pmcraid_release_chrdev(pinstance
);
5365 /* remove host template from scsi midlayer */
5366 scsi_remove_host(pinstance
->host
);
5368 /* block requests from mid-layer */
5369 scsi_block_requests(pinstance
->host
);
5371 /* initiate shutdown adapter */
5372 pmcraid_shutdown(pdev
);
5374 pmcraid_disable_interrupts(pinstance
, ~0);
5375 flush_work(&pinstance
->worker_q
);
5377 pmcraid_kill_tasklets(pinstance
);
5378 pmcraid_unregister_interrupt_handler(pinstance
);
5379 pmcraid_release_buffers(pinstance
);
5380 iounmap(pinstance
->mapped_dma_addr
);
5381 pci_release_regions(pdev
);
5382 scsi_host_put(pinstance
->host
);
5383 pci_disable_device(pdev
);
5390 * pmcraid_suspend - driver suspend entry point for power management
5391 * @pdev: PCI device structure
5392 * @state: PCI power state to suspend routine
5394 * Return Value - 0 always
5396 static int pmcraid_suspend(struct pci_dev
*pdev
, pm_message_t state
)
5398 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5400 pmcraid_shutdown(pdev
);
5401 pmcraid_disable_interrupts(pinstance
, ~0);
5402 pmcraid_kill_tasklets(pinstance
);
5403 pci_set_drvdata(pinstance
->pdev
, pinstance
);
5404 pmcraid_unregister_interrupt_handler(pinstance
);
5405 pci_save_state(pdev
);
5406 pci_disable_device(pdev
);
5407 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
5413 * pmcraid_resume - driver resume entry point PCI power management
5414 * @pdev: PCI device structure
5416 * Return Value - 0 in case of success. Error code in case of any failure
5418 static int pmcraid_resume(struct pci_dev
*pdev
)
5420 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5421 struct Scsi_Host
*host
= pinstance
->host
;
5424 pci_set_power_state(pdev
, PCI_D0
);
5425 pci_enable_wake(pdev
, PCI_D0
, 0);
5426 pci_restore_state(pdev
);
5428 rc
= pci_enable_device(pdev
);
5431 dev_err(&pdev
->dev
, "resume: Enable device failed\n");
5435 pci_set_master(pdev
);
5437 if ((sizeof(dma_addr_t
) == 4) ||
5438 pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)))
5439 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
5442 rc
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
5445 dev_err(&pdev
->dev
, "resume: Failed to set PCI DMA mask\n");
5446 goto disable_device
;
5449 pmcraid_disable_interrupts(pinstance
, ~0);
5450 atomic_set(&pinstance
->outstanding_cmds
, 0);
5451 rc
= pmcraid_register_interrupt_handler(pinstance
);
5455 "resume: couldn't register interrupt handlers\n");
5460 pmcraid_init_tasklets(pinstance
);
5461 pmcraid_enable_interrupts(pinstance
, PMCRAID_PCI_INTERRUPTS
);
5463 /* Start with hard reset sequence which brings up IOA to operational
5464 * state as well as completes the reset sequence.
5466 pinstance
->ioa_hard_reset
= 1;
5468 /* Start IOA firmware initialization and bring card to Operational
5471 if (pmcraid_reset_bringup(pinstance
)) {
5472 dev_err(&pdev
->dev
, "couldn't initialize IOA\n");
5474 goto release_tasklets
;
5480 pmcraid_disable_interrupts(pinstance
, ~0);
5481 pmcraid_kill_tasklets(pinstance
);
5482 pmcraid_unregister_interrupt_handler(pinstance
);
5485 scsi_host_put(host
);
5488 pci_disable_device(pdev
);
5495 #define pmcraid_suspend NULL
5496 #define pmcraid_resume NULL
5498 #endif /* CONFIG_PM */
5501 * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
5502 * completion of the ioa reset
5503 * @cmd: pointer to reset command block
5505 static void pmcraid_complete_ioa_reset(struct pmcraid_cmd
*cmd
)
5507 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5508 unsigned long flags
;
5510 spin_lock_irqsave(pinstance
->host
->host_lock
, flags
);
5511 pmcraid_ioa_reset(cmd
);
5512 spin_unlock_irqrestore(pinstance
->host
->host_lock
, flags
);
5513 scsi_unblock_requests(pinstance
->host
);
5514 schedule_work(&pinstance
->worker_q
);
5518 * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
5520 * @cmd: pointer to pmcraid_cmd structure
5523 * 0 for success or non-zero for failure cases
5525 static void pmcraid_set_supported_devs(struct pmcraid_cmd
*cmd
)
5527 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
5528 void (*cmd_done
) (struct pmcraid_cmd
*) = pmcraid_complete_ioa_reset
;
5530 pmcraid_reinit_cmdblk(cmd
);
5532 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
5533 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
5534 ioarcb
->cdb
[0] = PMCRAID_SET_SUPPORTED_DEVICES
;
5535 ioarcb
->cdb
[1] = ALL_DEVICES_SUPPORTED
;
5537 /* If this was called as part of resource table reinitialization due to
5538 * lost CCN, it is enough to return the command block back to free pool
5539 * as part of set_supported_devs completion function.
5541 if (cmd
->drv_inst
->reinit_cfg_table
) {
5542 cmd
->drv_inst
->reinit_cfg_table
= 0;
5544 cmd_done
= pmcraid_reinit_cfgtable_done
;
5547 /* we will be done with the reset sequence after set supported devices,
5548 * setup the done function to return the command block back to free
5551 pmcraid_send_cmd(cmd
,
5553 PMCRAID_SET_SUP_DEV_TIMEOUT
,
5554 pmcraid_timeout_handler
);
5559 * pmcraid_set_timestamp - set the timestamp to IOAFP
5561 * @cmd: pointer to pmcraid_cmd structure
5564 * 0 for success or non-zero for failure cases
5566 static void pmcraid_set_timestamp(struct pmcraid_cmd
*cmd
)
5568 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5569 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
5570 __be32 time_stamp_len
= cpu_to_be32(PMCRAID_TIMESTAMP_LEN
);
5571 struct pmcraid_ioadl_desc
*ioadl
= ioarcb
->add_data
.u
.ioadl
;
5576 do_gettimeofday(&tv
);
5577 timestamp
= tv
.tv_sec
* 1000;
5579 pinstance
->timestamp_data
->timestamp
[0] = (__u8
)(timestamp
);
5580 pinstance
->timestamp_data
->timestamp
[1] = (__u8
)((timestamp
) >> 8);
5581 pinstance
->timestamp_data
->timestamp
[2] = (__u8
)((timestamp
) >> 16);
5582 pinstance
->timestamp_data
->timestamp
[3] = (__u8
)((timestamp
) >> 24);
5583 pinstance
->timestamp_data
->timestamp
[4] = (__u8
)((timestamp
) >> 32);
5584 pinstance
->timestamp_data
->timestamp
[5] = (__u8
)((timestamp
) >> 40);
5586 pmcraid_reinit_cmdblk(cmd
);
5587 ioarcb
->request_type
= REQ_TYPE_SCSI
;
5588 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
5589 ioarcb
->cdb
[0] = PMCRAID_SCSI_SET_TIMESTAMP
;
5590 ioarcb
->cdb
[1] = PMCRAID_SCSI_SERVICE_ACTION
;
5591 memcpy(&(ioarcb
->cdb
[6]), &time_stamp_len
, sizeof(time_stamp_len
));
5593 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
5594 offsetof(struct pmcraid_ioarcb
,
5595 add_data
.u
.ioadl
[0]));
5596 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
5597 ioarcb
->ioarcb_bus_addr
&= ~(0x1FULL
);
5599 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
5600 ioarcb
->request_flags0
|= TRANSFER_DIR_WRITE
;
5601 ioarcb
->data_transfer_length
=
5602 cpu_to_le32(sizeof(struct pmcraid_timestamp_data
));
5603 ioadl
= &(ioarcb
->add_data
.u
.ioadl
[0]);
5604 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
5605 ioadl
->address
= cpu_to_le64(pinstance
->timestamp_data_baddr
);
5606 ioadl
->data_len
= cpu_to_le32(sizeof(struct pmcraid_timestamp_data
));
5608 if (!pinstance
->timestamp_error
) {
5609 pinstance
->timestamp_error
= 0;
5610 pmcraid_send_cmd(cmd
, pmcraid_set_supported_devs
,
5611 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
5613 pmcraid_send_cmd(cmd
, pmcraid_return_cmd
,
5614 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
5621 * pmcraid_init_res_table - Initialize the resource table
5622 * @cmd: pointer to pmcraid command struct
5624 * This function looks through the existing resource table, comparing
5625 * it with the config table. This function will take care of old/new
5626 * devices and schedule adding/removing them from the mid-layer
5632 static void pmcraid_init_res_table(struct pmcraid_cmd
*cmd
)
5634 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5635 struct pmcraid_resource_entry
*res
, *temp
;
5636 struct pmcraid_config_table_entry
*cfgte
;
5637 unsigned long lock_flags
;
5642 if (pinstance
->cfg_table
->flags
& MICROCODE_UPDATE_REQUIRED
)
5643 pmcraid_err("IOA requires microcode download\n");
5645 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
5647 /* resource list is protected by pinstance->resource_lock.
5648 * init_res_table can be called from probe (user-thread) or runtime
5649 * reset (timer/tasklet)
5651 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
5653 list_for_each_entry_safe(res
, temp
, &pinstance
->used_res_q
, queue
)
5654 list_move_tail(&res
->queue
, &old_res
);
5656 for (i
= 0; i
< pinstance
->cfg_table
->num_entries
; i
++) {
5657 if (be16_to_cpu(pinstance
->inq_data
->fw_version
) <=
5658 PMCRAID_FW_VERSION_1
)
5659 cfgte
= &pinstance
->cfg_table
->entries
[i
];
5661 cfgte
= (struct pmcraid_config_table_entry
*)
5662 &pinstance
->cfg_table
->entries_ext
[i
];
5664 if (!pmcraid_expose_resource(fw_version
, cfgte
))
5669 /* If this entry was already detected and initialized */
5670 list_for_each_entry_safe(res
, temp
, &old_res
, queue
) {
5672 rc
= memcmp(&res
->cfg_entry
.resource_address
,
5673 &cfgte
->resource_address
,
5674 sizeof(cfgte
->resource_address
));
5676 list_move_tail(&res
->queue
,
5677 &pinstance
->used_res_q
);
5683 /* If this is new entry, initialize it and add it the queue */
5686 if (list_empty(&pinstance
->free_res_q
)) {
5687 pmcraid_err("Too many devices attached\n");
5692 res
= list_entry(pinstance
->free_res_q
.next
,
5693 struct pmcraid_resource_entry
, queue
);
5695 res
->scsi_dev
= NULL
;
5696 res
->change_detected
= RES_CHANGE_ADD
;
5697 res
->reset_progress
= 0;
5698 list_move_tail(&res
->queue
, &pinstance
->used_res_q
);
5701 /* copy new configuration table entry details into driver
5702 * maintained resource entry
5705 memcpy(&res
->cfg_entry
, cfgte
,
5706 pinstance
->config_table_entry_size
);
5707 pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
5708 res
->cfg_entry
.resource_type
,
5709 (fw_version
<= PMCRAID_FW_VERSION_1
?
5710 res
->cfg_entry
.unique_flags1
:
5711 res
->cfg_entry
.array_id
& 0xFF),
5712 le32_to_cpu(res
->cfg_entry
.resource_address
));
5716 /* Detect any deleted entries, mark them for deletion from mid-layer */
5717 list_for_each_entry_safe(res
, temp
, &old_res
, queue
) {
5719 if (res
->scsi_dev
) {
5720 res
->change_detected
= RES_CHANGE_DEL
;
5721 res
->cfg_entry
.resource_handle
=
5722 PMCRAID_INVALID_RES_HANDLE
;
5723 list_move_tail(&res
->queue
, &pinstance
->used_res_q
);
5725 list_move_tail(&res
->queue
, &pinstance
->free_res_q
);
5729 /* release the resource list lock */
5730 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
5731 pmcraid_set_timestamp(cmd
);
5735 * pmcraid_querycfg - Send a Query IOA Config to the adapter.
5736 * @cmd: pointer pmcraid_cmd struct
5738 * This function sends a Query IOA Configuration command to the adapter to
5739 * retrieve the IOA configuration table.
5744 static void pmcraid_querycfg(struct pmcraid_cmd
*cmd
)
5746 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
5747 struct pmcraid_ioadl_desc
*ioadl
= ioarcb
->add_data
.u
.ioadl
;
5748 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5749 int cfg_table_size
= cpu_to_be32(sizeof(struct pmcraid_config_table
));
5751 if (be16_to_cpu(pinstance
->inq_data
->fw_version
) <=
5752 PMCRAID_FW_VERSION_1
)
5753 pinstance
->config_table_entry_size
=
5754 sizeof(struct pmcraid_config_table_entry
);
5756 pinstance
->config_table_entry_size
=
5757 sizeof(struct pmcraid_config_table_entry_ext
);
5759 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
5760 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
5762 ioarcb
->cdb
[0] = PMCRAID_QUERY_IOA_CONFIG
;
5764 /* firmware requires 4-byte length field, specified in B.E format */
5765 memcpy(&(ioarcb
->cdb
[10]), &cfg_table_size
, sizeof(cfg_table_size
));
5767 /* Since entire config table can be described by single IOADL, it can
5768 * be part of IOARCB itself
5770 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
5771 offsetof(struct pmcraid_ioarcb
,
5772 add_data
.u
.ioadl
[0]));
5773 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
5774 ioarcb
->ioarcb_bus_addr
&= ~(0x1FULL
);
5776 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
5777 ioarcb
->data_transfer_length
=
5778 cpu_to_le32(sizeof(struct pmcraid_config_table
));
5780 ioadl
= &(ioarcb
->add_data
.u
.ioadl
[0]);
5781 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
5782 ioadl
->address
= cpu_to_le64(pinstance
->cfg_table_bus_addr
);
5783 ioadl
->data_len
= cpu_to_le32(sizeof(struct pmcraid_config_table
));
5785 pmcraid_send_cmd(cmd
, pmcraid_init_res_table
,
5786 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
5791 * pmcraid_probe - PCI probe entry pointer for PMC MaxRAID controller driver
5792 * @pdev: pointer to pci device structure
5793 * @dev_id: pointer to device ids structure
5796 * returns 0 if the device is claimed and successfully configured.
5797 * returns non-zero error code in case of any failure
5799 static int pmcraid_probe(struct pci_dev
*pdev
,
5800 const struct pci_device_id
*dev_id
)
5802 struct pmcraid_instance
*pinstance
;
5803 struct Scsi_Host
*host
;
5804 void __iomem
*mapped_pci_addr
;
5805 int rc
= PCIBIOS_SUCCESSFUL
;
5807 if (atomic_read(&pmcraid_adapter_count
) >= PMCRAID_MAX_ADAPTERS
) {
5809 ("maximum number(%d) of supported adapters reached\n",
5810 atomic_read(&pmcraid_adapter_count
));
5814 atomic_inc(&pmcraid_adapter_count
);
5815 rc
= pci_enable_device(pdev
);
5818 dev_err(&pdev
->dev
, "Cannot enable adapter\n");
5819 atomic_dec(&pmcraid_adapter_count
);
5823 dev_info(&pdev
->dev
,
5824 "Found new IOA(%x:%x), Total IOA count: %d\n",
5825 pdev
->vendor
, pdev
->device
,
5826 atomic_read(&pmcraid_adapter_count
));
5828 rc
= pci_request_regions(pdev
, PMCRAID_DRIVER_NAME
);
5832 "Couldn't register memory range of registers\n");
5833 goto out_disable_device
;
5836 mapped_pci_addr
= pci_iomap(pdev
, 0, 0);
5838 if (!mapped_pci_addr
) {
5839 dev_err(&pdev
->dev
, "Couldn't map PCI registers memory\n");
5841 goto out_release_regions
;
5844 pci_set_master(pdev
);
5846 /* Firmware requires the system bus address of IOARCB to be within
5847 * 32-bit addressable range though it has 64-bit IOARRIN register.
5848 * However, firmware supports 64-bit streaming DMA buffers, whereas
5849 * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
5850 * returns memory within 4GB (if not, change this logic), coherent
5851 * buffers are within firmware acceptable address ranges.
5853 if ((sizeof(dma_addr_t
) == 4) ||
5854 pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)))
5855 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
5857 /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
5858 * bit mask for pci_alloc_consistent to return addresses within 4GB
5861 rc
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
5864 dev_err(&pdev
->dev
, "Failed to set PCI DMA mask\n");
5868 host
= scsi_host_alloc(&pmcraid_host_template
,
5869 sizeof(struct pmcraid_instance
));
5872 dev_err(&pdev
->dev
, "scsi_host_alloc failed!\n");
5877 host
->max_id
= PMCRAID_MAX_NUM_TARGETS_PER_BUS
;
5878 host
->max_lun
= PMCRAID_MAX_NUM_LUNS_PER_TARGET
;
5879 host
->unique_id
= host
->host_no
;
5880 host
->max_channel
= PMCRAID_MAX_BUS_TO_SCAN
;
5881 host
->max_cmd_len
= PMCRAID_MAX_CDB_LEN
;
5883 /* zero out entire instance structure */
5884 pinstance
= (struct pmcraid_instance
*)host
->hostdata
;
5885 memset(pinstance
, 0, sizeof(*pinstance
));
5887 pinstance
->chip_cfg
=
5888 (struct pmcraid_chip_details
*)(dev_id
->driver_data
);
5890 rc
= pmcraid_init_instance(pdev
, host
, mapped_pci_addr
);
5893 dev_err(&pdev
->dev
, "failed to initialize adapter instance\n");
5894 goto out_scsi_host_put
;
5897 pci_set_drvdata(pdev
, pinstance
);
5899 /* Save PCI config-space for use following the reset */
5900 rc
= pci_save_state(pinstance
->pdev
);
5903 dev_err(&pdev
->dev
, "Failed to save PCI config space\n");
5904 goto out_scsi_host_put
;
5907 pmcraid_disable_interrupts(pinstance
, ~0);
5909 rc
= pmcraid_register_interrupt_handler(pinstance
);
5912 dev_err(&pdev
->dev
, "couldn't register interrupt handler\n");
5913 goto out_scsi_host_put
;
5916 pmcraid_init_tasklets(pinstance
);
5918 /* allocate verious buffers used by LLD.*/
5919 rc
= pmcraid_init_buffers(pinstance
);
5922 pmcraid_err("couldn't allocate memory blocks\n");
5923 goto out_unregister_isr
;
5926 /* check the reset type required */
5927 pmcraid_reset_type(pinstance
);
5929 pmcraid_enable_interrupts(pinstance
, PMCRAID_PCI_INTERRUPTS
);
5931 /* Start IOA firmware initialization and bring card to Operational
5934 pmcraid_info("starting IOA initialization sequence\n");
5935 if (pmcraid_reset_bringup(pinstance
)) {
5936 dev_err(&pdev
->dev
, "couldn't initialize IOA\n");
5938 goto out_release_bufs
;
5941 /* Add adapter instance into mid-layer list */
5942 rc
= scsi_add_host(pinstance
->host
, &pdev
->dev
);
5944 pmcraid_err("couldn't add host into mid-layer: %d\n", rc
);
5945 goto out_release_bufs
;
5948 scsi_scan_host(pinstance
->host
);
5950 rc
= pmcraid_setup_chrdev(pinstance
);
5953 pmcraid_err("couldn't create mgmt interface, error: %x\n",
5955 goto out_remove_host
;
5958 /* Schedule worker thread to handle CCN and take care of adding and
5959 * removing devices to OS
5961 atomic_set(&pinstance
->expose_resources
, 1);
5962 schedule_work(&pinstance
->worker_q
);
5966 scsi_remove_host(host
);
5969 pmcraid_release_buffers(pinstance
);
5972 pmcraid_kill_tasklets(pinstance
);
5973 pmcraid_unregister_interrupt_handler(pinstance
);
5976 scsi_host_put(host
);
5979 iounmap(mapped_pci_addr
);
5981 out_release_regions
:
5982 pci_release_regions(pdev
);
5985 atomic_dec(&pmcraid_adapter_count
);
5986 pci_disable_device(pdev
);
5991 * PCI driver structure of pcmraid driver
5993 static struct pci_driver pmcraid_driver
= {
5994 .name
= PMCRAID_DRIVER_NAME
,
5995 .id_table
= pmcraid_pci_table
,
5996 .probe
= pmcraid_probe
,
5997 .remove
= pmcraid_remove
,
5998 .suspend
= pmcraid_suspend
,
5999 .resume
= pmcraid_resume
,
6000 .shutdown
= pmcraid_shutdown
6004 * pmcraid_init - module load entry point
6006 static int __init
pmcraid_init(void)
6011 pmcraid_info("%s Device Driver version: %s\n",
6012 PMCRAID_DRIVER_NAME
, PMCRAID_DRIVER_VERSION
);
6014 error
= alloc_chrdev_region(&dev
, 0,
6015 PMCRAID_MAX_ADAPTERS
,
6019 pmcraid_err("failed to get a major number for adapters\n");
6023 pmcraid_major
= MAJOR(dev
);
6024 pmcraid_class
= class_create(THIS_MODULE
, PMCRAID_DEVFILE
);
6026 if (IS_ERR(pmcraid_class
)) {
6027 error
= PTR_ERR(pmcraid_class
);
6028 pmcraid_err("failed to register with sysfs, error = %x\n",
6030 goto out_unreg_chrdev
;
6033 error
= pmcraid_netlink_init();
6036 goto out_unreg_chrdev
;
6038 error
= pci_register_driver(&pmcraid_driver
);
6043 pmcraid_err("failed to register pmcraid driver, error = %x\n",
6045 class_destroy(pmcraid_class
);
6046 pmcraid_netlink_release();
6049 unregister_chrdev_region(MKDEV(pmcraid_major
, 0), PMCRAID_MAX_ADAPTERS
);
6056 * pmcraid_exit - module unload entry point
6058 static void __exit
pmcraid_exit(void)
6060 pmcraid_netlink_release();
6061 unregister_chrdev_region(MKDEV(pmcraid_major
, 0),
6062 PMCRAID_MAX_ADAPTERS
);
6063 pci_unregister_driver(&pmcraid_driver
);
6064 class_destroy(pmcraid_class
);
6067 module_init(pmcraid_init
);
6068 module_exit(pmcraid_exit
);