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 genlmsg_end(skb
, msg_header
);
1478 result
= genlmsg_multicast(&pmcraid_event_family
, skb
,
1481 /* If there are no listeners, genlmsg_multicast may return non-zero
1485 pmcraid_info("error (%x) sending aen event message\n", result
);
1490 * pmcraid_notify_ccn - notifies about CCN event msg to user space
1491 * @pinstance: pointer adapter instance structure
1494 * 0 if success, error value in case of any failure
1496 static int pmcraid_notify_ccn(struct pmcraid_instance
*pinstance
)
1498 return pmcraid_notify_aen(pinstance
,
1500 pinstance
->ccn
.hcam
->data_len
+
1501 sizeof(struct pmcraid_hcam_hdr
));
1505 * pmcraid_notify_ldn - notifies about CCN event msg to user space
1506 * @pinstance: pointer adapter instance structure
1509 * 0 if success, error value in case of any failure
1511 static int pmcraid_notify_ldn(struct pmcraid_instance
*pinstance
)
1513 return pmcraid_notify_aen(pinstance
,
1515 pinstance
->ldn
.hcam
->data_len
+
1516 sizeof(struct pmcraid_hcam_hdr
));
1520 * pmcraid_notify_ioastate - sends IOA state event msg to user space
1521 * @pinstance: pointer adapter instance structure
1522 * @evt: controller state event to be sent
1525 * 0 if success, error value in case of any failure
1527 static void pmcraid_notify_ioastate(struct pmcraid_instance
*pinstance
, u32 evt
)
1529 pinstance
->scn
.ioa_state
= evt
;
1530 pmcraid_notify_aen(pinstance
,
1531 &pinstance
->scn
.msg
,
1536 * pmcraid_handle_config_change - Handle a config change from the adapter
1537 * @pinstance: pointer to per adapter instance structure
1543 static void pmcraid_handle_config_change(struct pmcraid_instance
*pinstance
)
1545 struct pmcraid_config_table_entry
*cfg_entry
;
1546 struct pmcraid_hcam_ccn
*ccn_hcam
;
1547 struct pmcraid_cmd
*cmd
;
1548 struct pmcraid_cmd
*cfgcmd
;
1549 struct pmcraid_resource_entry
*res
= NULL
;
1550 unsigned long lock_flags
;
1551 unsigned long host_lock_flags
;
1553 u32 hidden_entry
= 0;
1557 ccn_hcam
= (struct pmcraid_hcam_ccn
*)pinstance
->ccn
.hcam
;
1558 cfg_entry
= &ccn_hcam
->cfg_entry
;
1559 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
1561 pmcraid_info("CCN(%x): %x timestamp: %llx type: %x lost: %x flags: %x \
1562 res: %x:%x:%x:%x\n",
1563 pinstance
->ccn
.hcam
->ilid
,
1564 pinstance
->ccn
.hcam
->op_code
,
1565 ((pinstance
->ccn
.hcam
->timestamp1
) |
1566 ((pinstance
->ccn
.hcam
->timestamp2
& 0xffffffffLL
) << 32)),
1567 pinstance
->ccn
.hcam
->notification_type
,
1568 pinstance
->ccn
.hcam
->notification_lost
,
1569 pinstance
->ccn
.hcam
->flags
,
1570 pinstance
->host
->unique_id
,
1571 RES_IS_VSET(*cfg_entry
) ? PMCRAID_VSET_BUS_ID
:
1572 (RES_IS_GSCSI(*cfg_entry
) ? PMCRAID_PHYS_BUS_ID
:
1573 RES_BUS(cfg_entry
->resource_address
)),
1574 RES_IS_VSET(*cfg_entry
) ?
1575 (fw_version
<= PMCRAID_FW_VERSION_1
?
1576 cfg_entry
->unique_flags1
:
1577 cfg_entry
->array_id
& 0xFF) :
1578 RES_TARGET(cfg_entry
->resource_address
),
1579 RES_LUN(cfg_entry
->resource_address
));
1582 /* If this HCAM indicates a lost notification, read the config table */
1583 if (pinstance
->ccn
.hcam
->notification_lost
) {
1584 cfgcmd
= pmcraid_get_free_cmd(pinstance
);
1586 pmcraid_info("lost CCN, reading config table\b");
1587 pinstance
->reinit_cfg_table
= 1;
1588 pmcraid_querycfg(cfgcmd
);
1590 pmcraid_err("lost CCN, no free cmd for querycfg\n");
1592 goto out_notify_apps
;
1595 /* If this resource is not going to be added to mid-layer, just notify
1596 * applications and return. If this notification is about hiding a VSET
1597 * resource, check if it was exposed already.
1599 if (pinstance
->ccn
.hcam
->notification_type
==
1600 NOTIFICATION_TYPE_ENTRY_CHANGED
&&
1601 cfg_entry
->resource_type
== RES_TYPE_VSET
) {
1603 if (fw_version
<= PMCRAID_FW_VERSION_1
)
1604 hidden_entry
= (cfg_entry
->unique_flags1
& 0x80) != 0;
1606 hidden_entry
= (cfg_entry
->unique_flags1
& 0x80) != 0;
1608 } else if (!pmcraid_expose_resource(fw_version
, cfg_entry
)) {
1609 goto out_notify_apps
;
1612 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
1613 list_for_each_entry(res
, &pinstance
->used_res_q
, queue
) {
1614 rc
= memcmp(&res
->cfg_entry
.resource_address
,
1615 &cfg_entry
->resource_address
,
1616 sizeof(cfg_entry
->resource_address
));
1626 spin_unlock_irqrestore(&pinstance
->resource_lock
,
1628 goto out_notify_apps
;
1631 /* If there are more number of resources than what driver can
1632 * manage, do not notify the applications about the CCN. Just
1633 * ignore this notifications and re-register the same HCAM
1635 if (list_empty(&pinstance
->free_res_q
)) {
1636 spin_unlock_irqrestore(&pinstance
->resource_lock
,
1638 pmcraid_err("too many resources attached\n");
1639 spin_lock_irqsave(pinstance
->host
->host_lock
,
1641 pmcraid_send_hcam(pinstance
,
1642 PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1643 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
1648 res
= list_entry(pinstance
->free_res_q
.next
,
1649 struct pmcraid_resource_entry
, queue
);
1651 list_del(&res
->queue
);
1652 res
->scsi_dev
= NULL
;
1653 res
->reset_progress
= 0;
1654 list_add_tail(&res
->queue
, &pinstance
->used_res_q
);
1657 memcpy(&res
->cfg_entry
, cfg_entry
, pinstance
->config_table_entry_size
);
1659 if (pinstance
->ccn
.hcam
->notification_type
==
1660 NOTIFICATION_TYPE_ENTRY_DELETED
|| hidden_entry
) {
1661 if (res
->scsi_dev
) {
1662 if (fw_version
<= PMCRAID_FW_VERSION_1
)
1663 res
->cfg_entry
.unique_flags1
&= 0x7F;
1665 res
->cfg_entry
.array_id
&= 0xFF;
1666 res
->change_detected
= RES_CHANGE_DEL
;
1667 res
->cfg_entry
.resource_handle
=
1668 PMCRAID_INVALID_RES_HANDLE
;
1669 schedule_work(&pinstance
->worker_q
);
1671 /* This may be one of the non-exposed resources */
1672 list_move_tail(&res
->queue
, &pinstance
->free_res_q
);
1674 } else if (!res
->scsi_dev
) {
1675 res
->change_detected
= RES_CHANGE_ADD
;
1676 schedule_work(&pinstance
->worker_q
);
1678 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
1682 /* Notify configuration changes to registered applications.*/
1683 if (!pmcraid_disable_aen
)
1684 pmcraid_notify_ccn(pinstance
);
1686 cmd
= pmcraid_init_hcam(pinstance
, PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1688 pmcraid_send_hcam_cmd(cmd
);
1692 * pmcraid_get_error_info - return error string for an ioasc
1693 * @ioasc: ioasc code
1697 static struct pmcraid_ioasc_error
*pmcraid_get_error_info(u32 ioasc
)
1700 for (i
= 0; i
< ARRAY_SIZE(pmcraid_ioasc_error_table
); i
++) {
1701 if (pmcraid_ioasc_error_table
[i
].ioasc_code
== ioasc
)
1702 return &pmcraid_ioasc_error_table
[i
];
1708 * pmcraid_ioasc_logger - log IOASC information based user-settings
1709 * @ioasc: ioasc code
1710 * @cmd: pointer to command that resulted in 'ioasc'
1712 void pmcraid_ioasc_logger(u32 ioasc
, struct pmcraid_cmd
*cmd
)
1714 struct pmcraid_ioasc_error
*error_info
= pmcraid_get_error_info(ioasc
);
1716 if (error_info
== NULL
||
1717 cmd
->drv_inst
->current_log_level
< error_info
->log_level
)
1720 /* log the error string */
1721 pmcraid_err("cmd [%x] for resource %x failed with %x(%s)\n",
1722 cmd
->ioa_cb
->ioarcb
.cdb
[0],
1723 cmd
->ioa_cb
->ioarcb
.resource_handle
,
1724 le32_to_cpu(ioasc
), error_info
->error_string
);
1728 * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
1730 * @pinstance: pointer to per adapter instance structure
1735 static void pmcraid_handle_error_log(struct pmcraid_instance
*pinstance
)
1737 struct pmcraid_hcam_ldn
*hcam_ldn
;
1740 hcam_ldn
= (struct pmcraid_hcam_ldn
*)pinstance
->ldn
.hcam
;
1743 ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
1744 pinstance
->ldn
.hcam
->ilid
,
1745 pinstance
->ldn
.hcam
->op_code
,
1746 pinstance
->ldn
.hcam
->notification_type
,
1747 pinstance
->ldn
.hcam
->notification_lost
,
1748 pinstance
->ldn
.hcam
->flags
,
1749 pinstance
->ldn
.hcam
->overlay_id
);
1751 /* log only the errors, no need to log informational log entries */
1752 if (pinstance
->ldn
.hcam
->notification_type
!=
1753 NOTIFICATION_TYPE_ERROR_LOG
)
1756 if (pinstance
->ldn
.hcam
->notification_lost
==
1757 HOSTRCB_NOTIFICATIONS_LOST
)
1758 dev_info(&pinstance
->pdev
->dev
, "Error notifications lost\n");
1760 ioasc
= le32_to_cpu(hcam_ldn
->error_log
.fd_ioasc
);
1762 if (ioasc
== PMCRAID_IOASC_UA_BUS_WAS_RESET
||
1763 ioasc
== PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER
) {
1764 dev_info(&pinstance
->pdev
->dev
,
1765 "UnitAttention due to IOA Bus Reset\n");
1766 scsi_report_bus_reset(
1768 RES_BUS(hcam_ldn
->error_log
.fd_ra
));
1775 * pmcraid_process_ccn - Op done function for a CCN.
1776 * @cmd: pointer to command struct
1778 * This function is the op done function for a configuration
1779 * change notification
1784 static void pmcraid_process_ccn(struct pmcraid_cmd
*cmd
)
1786 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1787 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
1788 unsigned long lock_flags
;
1790 pinstance
->ccn
.cmd
= NULL
;
1791 pmcraid_return_cmd(cmd
);
1793 /* If driver initiated IOA reset happened while this hcam was pending
1794 * with IOA, or IOA bringdown sequence is in progress, no need to
1795 * re-register the hcam
1797 if (ioasc
== PMCRAID_IOASC_IOA_WAS_RESET
||
1798 atomic_read(&pinstance
->ccn
.ignore
) == 1) {
1801 dev_info(&pinstance
->pdev
->dev
,
1802 "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc
);
1803 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
1804 pmcraid_send_hcam(pinstance
, PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1805 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
1807 pmcraid_handle_config_change(pinstance
);
1812 * pmcraid_process_ldn - op done function for an LDN
1813 * @cmd: pointer to command block
1818 static void pmcraid_initiate_reset(struct pmcraid_instance
*);
1819 static void pmcraid_set_timestamp(struct pmcraid_cmd
*cmd
);
1821 static void pmcraid_process_ldn(struct pmcraid_cmd
*cmd
)
1823 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1824 struct pmcraid_hcam_ldn
*ldn_hcam
=
1825 (struct pmcraid_hcam_ldn
*)pinstance
->ldn
.hcam
;
1826 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
1827 u32 fd_ioasc
= le32_to_cpu(ldn_hcam
->error_log
.fd_ioasc
);
1828 unsigned long lock_flags
;
1830 /* return the command block back to freepool */
1831 pinstance
->ldn
.cmd
= NULL
;
1832 pmcraid_return_cmd(cmd
);
1834 /* If driver initiated IOA reset happened while this hcam was pending
1835 * with IOA, no need to re-register the hcam as reset engine will do it
1836 * once reset sequence is complete
1838 if (ioasc
== PMCRAID_IOASC_IOA_WAS_RESET
||
1839 atomic_read(&pinstance
->ccn
.ignore
) == 1) {
1841 } else if (!ioasc
) {
1842 pmcraid_handle_error_log(pinstance
);
1843 if (fd_ioasc
== PMCRAID_IOASC_NR_IOA_RESET_REQUIRED
) {
1844 spin_lock_irqsave(pinstance
->host
->host_lock
,
1846 pmcraid_initiate_reset(pinstance
);
1847 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
1851 if (fd_ioasc
== PMCRAID_IOASC_TIME_STAMP_OUT_OF_SYNC
) {
1852 pinstance
->timestamp_error
= 1;
1853 pmcraid_set_timestamp(cmd
);
1856 dev_info(&pinstance
->pdev
->dev
,
1857 "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc
);
1859 /* send netlink message for HCAM notification if enabled */
1860 if (!pmcraid_disable_aen
)
1861 pmcraid_notify_ldn(pinstance
);
1863 cmd
= pmcraid_init_hcam(pinstance
, PMCRAID_HCAM_CODE_LOG_DATA
);
1865 pmcraid_send_hcam_cmd(cmd
);
1869 * pmcraid_register_hcams - register HCAMs for CCN and LDN
1871 * @pinstance: pointer per adapter instance structure
1876 static void pmcraid_register_hcams(struct pmcraid_instance
*pinstance
)
1878 pmcraid_send_hcam(pinstance
, PMCRAID_HCAM_CODE_CONFIG_CHANGE
);
1879 pmcraid_send_hcam(pinstance
, PMCRAID_HCAM_CODE_LOG_DATA
);
1883 * pmcraid_unregister_hcams - cancel HCAMs registered already
1884 * @cmd: pointer to command used as part of reset sequence
1886 static void pmcraid_unregister_hcams(struct pmcraid_cmd
*cmd
)
1888 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1890 /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
1891 * handling hcam response though it is not necessary. In order to
1892 * prevent this, set 'ignore', so that bring-down sequence doesn't
1893 * re-send any more hcams
1895 atomic_set(&pinstance
->ccn
.ignore
, 1);
1896 atomic_set(&pinstance
->ldn
.ignore
, 1);
1898 /* If adapter reset was forced as part of runtime reset sequence,
1899 * start the reset sequence. Reset will be triggered even in case
1902 if ((pinstance
->force_ioa_reset
&& !pinstance
->ioa_bringdown
) ||
1903 pinstance
->ioa_unit_check
) {
1904 pinstance
->force_ioa_reset
= 0;
1905 pinstance
->ioa_unit_check
= 0;
1906 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
1907 pmcraid_reset_alert(cmd
);
1911 /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
1912 * one after the other. So CCN cancellation will be triggered by
1913 * pmcraid_cancel_ldn itself.
1915 pmcraid_cancel_ldn(cmd
);
1919 * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
1920 * @pinstance: pointer to adapter instance structure
1922 * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
1924 static void pmcraid_reinit_buffers(struct pmcraid_instance
*);
1926 static int pmcraid_reset_enable_ioa(struct pmcraid_instance
*pinstance
)
1930 pmcraid_reinit_buffers(pinstance
);
1931 intrs
= pmcraid_read_interrupts(pinstance
);
1933 pmcraid_enable_interrupts(pinstance
, PMCRAID_PCI_INTERRUPTS
);
1935 if (intrs
& INTRS_TRANSITION_TO_OPERATIONAL
) {
1936 if (!pinstance
->interrupt_mode
) {
1937 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL
,
1938 pinstance
->int_regs
.
1939 ioa_host_interrupt_mask_reg
);
1940 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL
,
1941 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
1950 * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
1951 * @cmd : pointer to reset command block
1956 static void pmcraid_soft_reset(struct pmcraid_cmd
*cmd
)
1958 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
1962 /* There will be an interrupt when Transition to Operational bit is
1963 * set so tasklet would execute next reset task. The timeout handler
1964 * would re-initiate a reset
1966 cmd
->cmd_done
= pmcraid_ioa_reset
;
1967 cmd
->timer
.data
= (unsigned long)cmd
;
1968 cmd
->timer
.expires
= jiffies
+
1969 msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT
);
1970 cmd
->timer
.function
= (void (*)(unsigned long))pmcraid_timeout_handler
;
1972 if (!timer_pending(&cmd
->timer
))
1973 add_timer(&cmd
->timer
);
1975 /* Enable destructive diagnostics on IOA if it is not yet in
1978 doorbell
= DOORBELL_RUNTIME_RESET
|
1979 DOORBELL_ENABLE_DESTRUCTIVE_DIAGS
;
1981 /* Since we do RESET_ALERT and Start BIST we have to again write
1982 * MSIX Doorbell to indicate the interrupt mode
1984 if (pinstance
->interrupt_mode
) {
1985 iowrite32(DOORBELL_INTR_MODE_MSIX
,
1986 pinstance
->int_regs
.host_ioa_interrupt_reg
);
1987 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
1990 iowrite32(doorbell
, pinstance
->int_regs
.host_ioa_interrupt_reg
);
1991 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
),
1992 int_reg
= ioread32(pinstance
->int_regs
.ioa_host_interrupt_reg
);
1994 pmcraid_info("Waiting for IOA to become operational %x:%x\n",
1995 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
),
2000 * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
2002 * @pinstance: pointer to adapter instance structure
2007 static void pmcraid_get_dump(struct pmcraid_instance
*pinstance
)
2009 pmcraid_info("%s is not yet implemented\n", __func__
);
2013 * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
2014 * @pinstance: pointer to adapter instance structure
2016 * This function fails all outstanding ops. If they are submitted to IOA
2017 * already, it sends cancel all messages if IOA is still accepting IOARCBs,
2018 * otherwise just completes the commands and returns the cmd blocks to free
2024 static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance
*pinstance
)
2026 struct pmcraid_cmd
*cmd
, *temp
;
2027 unsigned long lock_flags
;
2029 /* pending command list is protected by pending_pool_lock. Its
2030 * traversal must be done as within this lock
2032 spin_lock_irqsave(&pinstance
->pending_pool_lock
, lock_flags
);
2033 list_for_each_entry_safe(cmd
, temp
, &pinstance
->pending_cmd_pool
,
2035 list_del(&cmd
->free_list
);
2036 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
,
2038 cmd
->ioa_cb
->ioasa
.ioasc
=
2039 cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET
);
2040 cmd
->ioa_cb
->ioasa
.ilid
=
2041 cpu_to_be32(PMCRAID_DRIVER_ILID
);
2043 /* In case the command timer is still running */
2044 del_timer(&cmd
->timer
);
2046 /* If this is an IO command, complete it by invoking scsi_done
2047 * function. If this is one of the internal commands other
2048 * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
2051 if (cmd
->scsi_cmd
) {
2053 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2054 __le32 resp
= cmd
->ioa_cb
->ioarcb
.response_handle
;
2056 scsi_cmd
->result
|= DID_ERROR
<< 16;
2058 scsi_dma_unmap(scsi_cmd
);
2059 pmcraid_return_cmd(cmd
);
2061 pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
2062 le32_to_cpu(resp
) >> 2,
2063 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2065 scsi_cmd
->scsi_done(scsi_cmd
);
2066 } else if (cmd
->cmd_done
== pmcraid_internal_done
||
2067 cmd
->cmd_done
== pmcraid_erp_done
) {
2069 } else if (cmd
->cmd_done
!= pmcraid_ioa_reset
&&
2070 cmd
->cmd_done
!= pmcraid_ioa_shutdown_done
) {
2071 pmcraid_return_cmd(cmd
);
2074 atomic_dec(&pinstance
->outstanding_cmds
);
2075 spin_lock_irqsave(&pinstance
->pending_pool_lock
, lock_flags
);
2078 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
, lock_flags
);
2082 * pmcraid_ioa_reset - Implementation of IOA reset logic
2084 * @cmd: pointer to the cmd block to be used for entire reset process
2086 * This function executes most of the steps required for IOA reset. This gets
2087 * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
2088 * 'eh_' thread. Access to variables used for controlling the reset sequence is
2089 * synchronized using host lock. Various functions called during reset process
2090 * would make use of a single command block, pointer to which is also stored in
2091 * adapter instance structure.
2096 static void pmcraid_ioa_reset(struct pmcraid_cmd
*cmd
)
2098 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
2099 u8 reset_complete
= 0;
2101 pinstance
->ioa_reset_in_progress
= 1;
2103 if (pinstance
->reset_cmd
!= cmd
) {
2104 pmcraid_err("reset is called with different command block\n");
2105 pinstance
->reset_cmd
= cmd
;
2108 pmcraid_info("reset_engine: state = %d, command = %p\n",
2109 pinstance
->ioa_state
, cmd
);
2111 switch (pinstance
->ioa_state
) {
2113 case IOA_STATE_DEAD
:
2114 /* If IOA is offline, whatever may be the reset reason, just
2115 * return. callers might be waiting on the reset wait_q, wake
2118 pmcraid_err("IOA is offline no reset is possible\n");
2122 case IOA_STATE_IN_BRINGDOWN
:
2123 /* we enter here, once ioa shutdown command is processed by IOA
2124 * Alert IOA for a possible reset. If reset alert fails, IOA
2125 * goes through hard-reset
2127 pmcraid_disable_interrupts(pinstance
, ~0);
2128 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
2129 pmcraid_reset_alert(cmd
);
2132 case IOA_STATE_UNKNOWN
:
2133 /* We may be called during probe or resume. Some pre-processing
2134 * is required for prior to reset
2136 scsi_block_requests(pinstance
->host
);
2138 /* If asked to reset while IOA was processing responses or
2139 * there are any error responses then IOA may require
2142 if (pinstance
->ioa_hard_reset
== 0) {
2143 if (ioread32(pinstance
->ioa_status
) &
2144 INTRS_TRANSITION_TO_OPERATIONAL
) {
2145 pmcraid_info("sticky bit set, bring-up\n");
2146 pinstance
->ioa_state
= IOA_STATE_IN_BRINGUP
;
2147 pmcraid_reinit_cmdblk(cmd
);
2148 pmcraid_identify_hrrq(cmd
);
2150 pinstance
->ioa_state
= IOA_STATE_IN_SOFT_RESET
;
2151 pmcraid_soft_reset(cmd
);
2154 /* Alert IOA of a possible reset and wait for critical
2155 * operation in progress bit to reset
2157 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
2158 pmcraid_reset_alert(cmd
);
2162 case IOA_STATE_IN_RESET_ALERT
:
2163 /* If critical operation in progress bit is reset or wait gets
2164 * timed out, reset proceeds with starting BIST on the IOA.
2165 * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
2166 * they are 3 or more, reset engine marks IOA dead and returns
2168 pinstance
->ioa_state
= IOA_STATE_IN_HARD_RESET
;
2169 pmcraid_start_bist(cmd
);
2172 case IOA_STATE_IN_HARD_RESET
:
2173 pinstance
->ioa_reset_attempts
++;
2175 /* retry reset if we haven't reached maximum allowed limit */
2176 if (pinstance
->ioa_reset_attempts
> PMCRAID_RESET_ATTEMPTS
) {
2177 pinstance
->ioa_reset_attempts
= 0;
2178 pmcraid_err("IOA didn't respond marking it as dead\n");
2179 pinstance
->ioa_state
= IOA_STATE_DEAD
;
2181 if (pinstance
->ioa_bringdown
)
2182 pmcraid_notify_ioastate(pinstance
,
2183 PMC_DEVICE_EVENT_SHUTDOWN_FAILED
);
2185 pmcraid_notify_ioastate(pinstance
,
2186 PMC_DEVICE_EVENT_RESET_FAILED
);
2191 /* Once either bist or pci reset is done, restore PCI config
2192 * space. If this fails, proceed with hard reset again
2194 pci_restore_state(pinstance
->pdev
);
2196 /* fail all pending commands */
2197 pmcraid_fail_outstanding_cmds(pinstance
);
2199 /* check if unit check is active, if so extract dump */
2200 if (pinstance
->ioa_unit_check
) {
2201 pmcraid_info("unit check is active\n");
2202 pinstance
->ioa_unit_check
= 0;
2203 pmcraid_get_dump(pinstance
);
2204 pinstance
->ioa_reset_attempts
--;
2205 pinstance
->ioa_state
= IOA_STATE_IN_RESET_ALERT
;
2206 pmcraid_reset_alert(cmd
);
2210 /* if the reset reason is to bring-down the ioa, we might be
2211 * done with the reset restore pci_config_space and complete
2214 if (pinstance
->ioa_bringdown
) {
2215 pmcraid_info("bringing down the adapter\n");
2216 pinstance
->ioa_shutdown_type
= SHUTDOWN_NONE
;
2217 pinstance
->ioa_bringdown
= 0;
2218 pinstance
->ioa_state
= IOA_STATE_UNKNOWN
;
2219 pmcraid_notify_ioastate(pinstance
,
2220 PMC_DEVICE_EVENT_SHUTDOWN_SUCCESS
);
2223 /* bring-up IOA, so proceed with soft reset
2224 * Reinitialize hrrq_buffers and their indices also
2225 * enable interrupts after a pci_restore_state
2227 if (pmcraid_reset_enable_ioa(pinstance
)) {
2228 pinstance
->ioa_state
= IOA_STATE_IN_BRINGUP
;
2229 pmcraid_info("bringing up the adapter\n");
2230 pmcraid_reinit_cmdblk(cmd
);
2231 pmcraid_identify_hrrq(cmd
);
2233 pinstance
->ioa_state
= IOA_STATE_IN_SOFT_RESET
;
2234 pmcraid_soft_reset(cmd
);
2239 case IOA_STATE_IN_SOFT_RESET
:
2240 /* TRANSITION TO OPERATIONAL is on so start initialization
2243 pmcraid_info("In softreset proceeding with bring-up\n");
2244 pinstance
->ioa_state
= IOA_STATE_IN_BRINGUP
;
2246 /* Initialization commands start with HRRQ identification. From
2247 * now on tasklet completes most of the commands as IOA is up
2248 * and intrs are enabled
2250 pmcraid_identify_hrrq(cmd
);
2253 case IOA_STATE_IN_BRINGUP
:
2254 /* we are done with bringing up of IOA, change the ioa_state to
2255 * operational and wake up any waiters
2257 pinstance
->ioa_state
= IOA_STATE_OPERATIONAL
;
2261 case IOA_STATE_OPERATIONAL
:
2263 /* When IOA is operational and a reset is requested, check for
2264 * the reset reason. If reset is to bring down IOA, unregister
2265 * HCAMs and initiate shutdown; if adapter reset is forced then
2266 * restart reset sequence again
2268 if (pinstance
->ioa_shutdown_type
== SHUTDOWN_NONE
&&
2269 pinstance
->force_ioa_reset
== 0) {
2270 pmcraid_notify_ioastate(pinstance
,
2271 PMC_DEVICE_EVENT_RESET_SUCCESS
);
2274 if (pinstance
->ioa_shutdown_type
!= SHUTDOWN_NONE
)
2275 pinstance
->ioa_state
= IOA_STATE_IN_BRINGDOWN
;
2276 pmcraid_reinit_cmdblk(cmd
);
2277 pmcraid_unregister_hcams(cmd
);
2282 /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
2283 * OPERATIONAL. Reset all control variables used during reset, wake up
2284 * any waiting threads and let the SCSI mid-layer send commands. Note
2285 * that host_lock must be held before invoking scsi_report_bus_reset.
2287 if (reset_complete
) {
2288 pinstance
->ioa_reset_in_progress
= 0;
2289 pinstance
->ioa_reset_attempts
= 0;
2290 pinstance
->reset_cmd
= NULL
;
2291 pinstance
->ioa_shutdown_type
= SHUTDOWN_NONE
;
2292 pinstance
->ioa_bringdown
= 0;
2293 pmcraid_return_cmd(cmd
);
2295 /* If target state is to bring up the adapter, proceed with
2296 * hcam registration and resource exposure to mid-layer.
2298 if (pinstance
->ioa_state
== IOA_STATE_OPERATIONAL
)
2299 pmcraid_register_hcams(pinstance
);
2301 wake_up_all(&pinstance
->reset_wait_q
);
2308 * pmcraid_initiate_reset - initiates reset sequence. This is called from
2309 * ISR/tasklet during error interrupts including IOA unit check. If reset
2310 * is already in progress, it just returns, otherwise initiates IOA reset
2311 * to bring IOA up to operational state.
2313 * @pinstance: pointer to adapter instance structure
2318 static void pmcraid_initiate_reset(struct pmcraid_instance
*pinstance
)
2320 struct pmcraid_cmd
*cmd
;
2322 /* If the reset is already in progress, just return, otherwise start
2323 * reset sequence and return
2325 if (!pinstance
->ioa_reset_in_progress
) {
2326 scsi_block_requests(pinstance
->host
);
2327 cmd
= pmcraid_get_free_cmd(pinstance
);
2330 pmcraid_err("no cmnd blocks for initiate_reset\n");
2334 pinstance
->ioa_shutdown_type
= SHUTDOWN_NONE
;
2335 pinstance
->reset_cmd
= cmd
;
2336 pinstance
->force_ioa_reset
= 1;
2337 pmcraid_notify_ioastate(pinstance
,
2338 PMC_DEVICE_EVENT_RESET_START
);
2339 pmcraid_ioa_reset(cmd
);
2344 * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
2346 * @pinstance: pointer adapter instance structure
2347 * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
2348 * @target_state: expected target state after reset
2350 * Note: This command initiates reset and waits for its completion. Hence this
2351 * should not be called from isr/timer/tasklet functions (timeout handlers,
2352 * error response handlers and interrupt handlers).
2355 * 1 in case ioa_state is not target_state, 0 otherwise.
2357 static int pmcraid_reset_reload(
2358 struct pmcraid_instance
*pinstance
,
2363 struct pmcraid_cmd
*reset_cmd
= NULL
;
2364 unsigned long lock_flags
;
2367 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
2369 if (pinstance
->ioa_reset_in_progress
) {
2370 pmcraid_info("reset_reload: reset is already in progress\n");
2372 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2374 wait_event(pinstance
->reset_wait_q
,
2375 !pinstance
->ioa_reset_in_progress
);
2377 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
2379 if (pinstance
->ioa_state
== IOA_STATE_DEAD
) {
2380 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
2382 pmcraid_info("reset_reload: IOA is dead\n");
2384 } else if (pinstance
->ioa_state
== target_state
) {
2390 pmcraid_info("reset_reload: proceeding with reset\n");
2391 scsi_block_requests(pinstance
->host
);
2392 reset_cmd
= pmcraid_get_free_cmd(pinstance
);
2394 if (reset_cmd
== NULL
) {
2395 pmcraid_err("no free cmnd for reset_reload\n");
2396 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
2401 if (shutdown_type
== SHUTDOWN_NORMAL
)
2402 pinstance
->ioa_bringdown
= 1;
2404 pinstance
->ioa_shutdown_type
= shutdown_type
;
2405 pinstance
->reset_cmd
= reset_cmd
;
2406 pinstance
->force_ioa_reset
= reset
;
2407 pmcraid_info("reset_reload: initiating reset\n");
2408 pmcraid_ioa_reset(reset_cmd
);
2409 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2410 pmcraid_info("reset_reload: waiting for reset to complete\n");
2411 wait_event(pinstance
->reset_wait_q
,
2412 !pinstance
->ioa_reset_in_progress
);
2414 pmcraid_info("reset_reload: reset is complete !!\n");
2415 scsi_unblock_requests(pinstance
->host
);
2416 if (pinstance
->ioa_state
== target_state
)
2424 * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
2426 * @pinstance: pointer to adapter instance structure
2429 * whatever is returned from pmcraid_reset_reload
2431 static int pmcraid_reset_bringdown(struct pmcraid_instance
*pinstance
)
2433 return pmcraid_reset_reload(pinstance
,
2439 * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
2441 * @pinstance: pointer to adapter instance structure
2444 * whatever is returned from pmcraid_reset_reload
2446 static int pmcraid_reset_bringup(struct pmcraid_instance
*pinstance
)
2448 pmcraid_notify_ioastate(pinstance
, PMC_DEVICE_EVENT_RESET_START
);
2450 return pmcraid_reset_reload(pinstance
,
2452 IOA_STATE_OPERATIONAL
);
2456 * pmcraid_request_sense - Send request sense to a device
2457 * @cmd: pmcraid command struct
2459 * This function sends a request sense to a device as a result of a check
2460 * condition. This method re-uses the same command block that failed earlier.
2462 static void pmcraid_request_sense(struct pmcraid_cmd
*cmd
)
2464 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
2465 struct pmcraid_ioadl_desc
*ioadl
= ioarcb
->add_data
.u
.ioadl
;
2467 /* allocate DMAable memory for sense buffers */
2468 cmd
->sense_buffer
= pci_alloc_consistent(cmd
->drv_inst
->pdev
,
2469 SCSI_SENSE_BUFFERSIZE
,
2470 &cmd
->sense_buffer_dma
);
2472 if (cmd
->sense_buffer
== NULL
) {
2474 ("couldn't allocate sense buffer for request sense\n");
2475 pmcraid_erp_done(cmd
);
2479 /* re-use the command block */
2480 memset(&cmd
->ioa_cb
->ioasa
, 0, sizeof(struct pmcraid_ioasa
));
2481 memset(ioarcb
->cdb
, 0, PMCRAID_MAX_CDB_LEN
);
2482 ioarcb
->request_flags0
= (SYNC_COMPLETE
|
2485 ioarcb
->request_type
= REQ_TYPE_SCSI
;
2486 ioarcb
->cdb
[0] = REQUEST_SENSE
;
2487 ioarcb
->cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
2489 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
2490 offsetof(struct pmcraid_ioarcb
,
2491 add_data
.u
.ioadl
[0]));
2492 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
2494 ioarcb
->data_transfer_length
= cpu_to_le32(SCSI_SENSE_BUFFERSIZE
);
2496 ioadl
->address
= cpu_to_le64(cmd
->sense_buffer_dma
);
2497 ioadl
->data_len
= cpu_to_le32(SCSI_SENSE_BUFFERSIZE
);
2498 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
2500 /* request sense might be called as part of error response processing
2501 * which runs in tasklets context. It is possible that mid-layer might
2502 * schedule queuecommand during this time, hence, writting to IOARRIN
2503 * must be protect by host_lock
2505 pmcraid_send_cmd(cmd
, pmcraid_erp_done
,
2506 PMCRAID_REQUEST_SENSE_TIMEOUT
,
2507 pmcraid_timeout_handler
);
2511 * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
2512 * @cmd: command that failed
2513 * @sense: true if request_sense is required after cancel all
2515 * This function sends a cancel all to a device to clear the queue.
2517 static void pmcraid_cancel_all(struct pmcraid_cmd
*cmd
, u32 sense
)
2519 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2520 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
2521 struct pmcraid_resource_entry
*res
= scsi_cmd
->device
->hostdata
;
2522 void (*cmd_done
) (struct pmcraid_cmd
*) = sense
? pmcraid_erp_done
2523 : pmcraid_request_sense
;
2525 memset(ioarcb
->cdb
, 0, PMCRAID_MAX_CDB_LEN
);
2526 ioarcb
->request_flags0
= SYNC_OVERRIDE
;
2527 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
2528 ioarcb
->cdb
[0] = PMCRAID_CANCEL_ALL_REQUESTS
;
2530 if (RES_IS_GSCSI(res
->cfg_entry
))
2531 ioarcb
->cdb
[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL
;
2533 ioarcb
->ioadl_bus_addr
= 0;
2534 ioarcb
->ioadl_length
= 0;
2535 ioarcb
->data_transfer_length
= 0;
2536 ioarcb
->ioarcb_bus_addr
&= (~0x1FULL
);
2538 /* writing to IOARRIN must be protected by host_lock, as mid-layer
2539 * schedule queuecommand while we are doing this
2541 pmcraid_send_cmd(cmd
, cmd_done
,
2542 PMCRAID_REQUEST_SENSE_TIMEOUT
,
2543 pmcraid_timeout_handler
);
2547 * pmcraid_frame_auto_sense: frame fixed format sense information
2549 * @cmd: pointer to failing command block
2554 static void pmcraid_frame_auto_sense(struct pmcraid_cmd
*cmd
)
2556 u8
*sense_buf
= cmd
->scsi_cmd
->sense_buffer
;
2557 struct pmcraid_resource_entry
*res
= cmd
->scsi_cmd
->device
->hostdata
;
2558 struct pmcraid_ioasa
*ioasa
= &cmd
->ioa_cb
->ioasa
;
2559 u32 ioasc
= le32_to_cpu(ioasa
->ioasc
);
2560 u32 failing_lba
= 0;
2562 memset(sense_buf
, 0, SCSI_SENSE_BUFFERSIZE
);
2563 cmd
->scsi_cmd
->result
= SAM_STAT_CHECK_CONDITION
;
2565 if (RES_IS_VSET(res
->cfg_entry
) &&
2566 ioasc
== PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC
&&
2567 ioasa
->u
.vset
.failing_lba_hi
!= 0) {
2569 sense_buf
[0] = 0x72;
2570 sense_buf
[1] = PMCRAID_IOASC_SENSE_KEY(ioasc
);
2571 sense_buf
[2] = PMCRAID_IOASC_SENSE_CODE(ioasc
);
2572 sense_buf
[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc
);
2576 sense_buf
[9] = 0x0A;
2577 sense_buf
[10] = 0x80;
2579 failing_lba
= le32_to_cpu(ioasa
->u
.vset
.failing_lba_hi
);
2581 sense_buf
[12] = (failing_lba
& 0xff000000) >> 24;
2582 sense_buf
[13] = (failing_lba
& 0x00ff0000) >> 16;
2583 sense_buf
[14] = (failing_lba
& 0x0000ff00) >> 8;
2584 sense_buf
[15] = failing_lba
& 0x000000ff;
2586 failing_lba
= le32_to_cpu(ioasa
->u
.vset
.failing_lba_lo
);
2588 sense_buf
[16] = (failing_lba
& 0xff000000) >> 24;
2589 sense_buf
[17] = (failing_lba
& 0x00ff0000) >> 16;
2590 sense_buf
[18] = (failing_lba
& 0x0000ff00) >> 8;
2591 sense_buf
[19] = failing_lba
& 0x000000ff;
2593 sense_buf
[0] = 0x70;
2594 sense_buf
[2] = PMCRAID_IOASC_SENSE_KEY(ioasc
);
2595 sense_buf
[12] = PMCRAID_IOASC_SENSE_CODE(ioasc
);
2596 sense_buf
[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc
);
2598 if (ioasc
== PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC
) {
2599 if (RES_IS_VSET(res
->cfg_entry
))
2601 le32_to_cpu(ioasa
->u
.
2602 vset
.failing_lba_lo
);
2603 sense_buf
[0] |= 0x80;
2604 sense_buf
[3] = (failing_lba
>> 24) & 0xff;
2605 sense_buf
[4] = (failing_lba
>> 16) & 0xff;
2606 sense_buf
[5] = (failing_lba
>> 8) & 0xff;
2607 sense_buf
[6] = failing_lba
& 0xff;
2610 sense_buf
[7] = 6; /* additional length */
2615 * pmcraid_error_handler - Error response handlers for a SCSI op
2616 * @cmd: pointer to pmcraid_cmd that has failed
2618 * This function determines whether or not to initiate ERP on the affected
2619 * device. This is called from a tasklet, which doesn't hold any locks.
2622 * 0 it caller can complete the request, otherwise 1 where in error
2623 * handler itself completes the request and returns the command block
2626 static int pmcraid_error_handler(struct pmcraid_cmd
*cmd
)
2628 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2629 struct pmcraid_resource_entry
*res
= scsi_cmd
->device
->hostdata
;
2630 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
2631 struct pmcraid_ioasa
*ioasa
= &cmd
->ioa_cb
->ioasa
;
2632 u32 ioasc
= le32_to_cpu(ioasa
->ioasc
);
2633 u32 masked_ioasc
= ioasc
& PMCRAID_IOASC_SENSE_MASK
;
2634 u32 sense_copied
= 0;
2637 pmcraid_info("resource pointer is NULL\n");
2641 /* If this was a SCSI read/write command keep count of errors */
2642 if (SCSI_CMD_TYPE(scsi_cmd
->cmnd
[0]) == SCSI_READ_CMD
)
2643 atomic_inc(&res
->read_failures
);
2644 else if (SCSI_CMD_TYPE(scsi_cmd
->cmnd
[0]) == SCSI_WRITE_CMD
)
2645 atomic_inc(&res
->write_failures
);
2647 if (!RES_IS_GSCSI(res
->cfg_entry
) &&
2648 masked_ioasc
!= PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR
) {
2649 pmcraid_frame_auto_sense(cmd
);
2652 /* Log IOASC/IOASA information based on user settings */
2653 pmcraid_ioasc_logger(ioasc
, cmd
);
2655 switch (masked_ioasc
) {
2657 case PMCRAID_IOASC_AC_TERMINATED_BY_HOST
:
2658 scsi_cmd
->result
|= (DID_ABORT
<< 16);
2661 case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE
:
2662 case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE
:
2663 scsi_cmd
->result
|= (DID_NO_CONNECT
<< 16);
2666 case PMCRAID_IOASC_NR_SYNC_REQUIRED
:
2668 scsi_cmd
->result
|= (DID_IMM_RETRY
<< 16);
2671 case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC
:
2672 scsi_cmd
->result
|= (DID_PASSTHROUGH
<< 16);
2675 case PMCRAID_IOASC_UA_BUS_WAS_RESET
:
2676 case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER
:
2677 if (!res
->reset_progress
)
2678 scsi_report_bus_reset(pinstance
->host
,
2679 scsi_cmd
->device
->channel
);
2680 scsi_cmd
->result
|= (DID_ERROR
<< 16);
2683 case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR
:
2684 scsi_cmd
->result
|= PMCRAID_IOASC_SENSE_STATUS(ioasc
);
2687 /* if check_condition is not active return with error otherwise
2688 * get/frame the sense buffer
2690 if (PMCRAID_IOASC_SENSE_STATUS(ioasc
) !=
2691 SAM_STAT_CHECK_CONDITION
&&
2692 PMCRAID_IOASC_SENSE_STATUS(ioasc
) != SAM_STAT_ACA_ACTIVE
)
2695 /* If we have auto sense data as part of IOASA pass it to
2698 if (ioasa
->auto_sense_length
!= 0) {
2699 short sense_len
= ioasa
->auto_sense_length
;
2700 int data_size
= min_t(u16
, le16_to_cpu(sense_len
),
2701 SCSI_SENSE_BUFFERSIZE
);
2703 memcpy(scsi_cmd
->sense_buffer
,
2709 if (RES_IS_GSCSI(res
->cfg_entry
))
2710 pmcraid_cancel_all(cmd
, sense_copied
);
2711 else if (sense_copied
)
2712 pmcraid_erp_done(cmd
);
2714 pmcraid_request_sense(cmd
);
2718 case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED
:
2722 if (PMCRAID_IOASC_SENSE_KEY(ioasc
) > RECOVERED_ERROR
)
2723 scsi_cmd
->result
|= (DID_ERROR
<< 16);
2730 * pmcraid_reset_device - device reset handler functions
2732 * @scsi_cmd: scsi command struct
2733 * @modifier: reset modifier indicating the reset sequence to be performed
2735 * This function issues a device reset to the affected device.
2736 * A LUN reset will be sent to the device first. If that does
2737 * not work, a target reset will be sent.
2742 static int pmcraid_reset_device(
2743 struct scsi_cmnd
*scsi_cmd
,
2744 unsigned long timeout
,
2748 struct pmcraid_cmd
*cmd
;
2749 struct pmcraid_instance
*pinstance
;
2750 struct pmcraid_resource_entry
*res
;
2751 struct pmcraid_ioarcb
*ioarcb
;
2752 unsigned long lock_flags
;
2756 (struct pmcraid_instance
*)scsi_cmd
->device
->host
->hostdata
;
2757 res
= scsi_cmd
->device
->hostdata
;
2760 sdev_printk(KERN_ERR
, scsi_cmd
->device
,
2761 "reset_device: NULL resource pointer\n");
2765 /* If adapter is currently going through reset/reload, return failed.
2766 * This will force the mid-layer to call _eh_bus/host reset, which
2767 * will then go to sleep and wait for the reset to complete
2769 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
2770 if (pinstance
->ioa_reset_in_progress
||
2771 pinstance
->ioa_state
== IOA_STATE_DEAD
) {
2772 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2776 res
->reset_progress
= 1;
2777 pmcraid_info("Resetting %s resource with addr %x\n",
2778 ((modifier
& RESET_DEVICE_LUN
) ? "LUN" :
2779 ((modifier
& RESET_DEVICE_TARGET
) ? "TARGET" : "BUS")),
2780 le32_to_cpu(res
->cfg_entry
.resource_address
));
2782 /* get a free cmd block */
2783 cmd
= pmcraid_get_free_cmd(pinstance
);
2786 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2787 pmcraid_err("%s: no cmd blocks are available\n", __func__
);
2791 ioarcb
= &cmd
->ioa_cb
->ioarcb
;
2792 ioarcb
->resource_handle
= res
->cfg_entry
.resource_handle
;
2793 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
2794 ioarcb
->cdb
[0] = PMCRAID_RESET_DEVICE
;
2796 /* Initialize reset modifier bits */
2798 modifier
= ENABLE_RESET_MODIFIER
| modifier
;
2800 ioarcb
->cdb
[1] = modifier
;
2802 init_completion(&cmd
->wait_for_completion
);
2803 cmd
->completion_req
= 1;
2805 pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
2806 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2807 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.resource_handle
),
2808 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2);
2810 pmcraid_send_cmd(cmd
,
2811 pmcraid_internal_done
,
2813 pmcraid_timeout_handler
);
2815 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
2817 /* RESET_DEVICE command completes after all pending IOARCBs are
2818 * completed. Once this command is completed, pmcraind_internal_done
2819 * will wake up the 'completion' queue.
2821 wait_for_completion(&cmd
->wait_for_completion
);
2823 /* complete the command here itself and return the command block
2826 pmcraid_return_cmd(cmd
);
2827 res
->reset_progress
= 0;
2828 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
2830 /* set the return value based on the returned ioasc */
2831 return PMCRAID_IOASC_SENSE_KEY(ioasc
) ? FAILED
: SUCCESS
;
2835 * _pmcraid_io_done - helper for pmcraid_io_done function
2837 * @cmd: pointer to pmcraid command struct
2838 * @reslen: residual data length to be set in the ioasa
2839 * @ioasc: ioasc either returned by IOA or set by driver itself.
2841 * This function is invoked by pmcraid_io_done to complete mid-layer
2845 * 0 if caller is required to return it to free_pool. Returns 1 if
2846 * caller need not worry about freeing command block as error handler
2847 * will take care of that.
2850 static int _pmcraid_io_done(struct pmcraid_cmd
*cmd
, int reslen
, int ioasc
)
2852 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
2855 scsi_set_resid(scsi_cmd
, reslen
);
2857 pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
2858 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2,
2859 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2860 ioasc
, scsi_cmd
->result
);
2862 if (PMCRAID_IOASC_SENSE_KEY(ioasc
) != 0)
2863 rc
= pmcraid_error_handler(cmd
);
2866 scsi_dma_unmap(scsi_cmd
);
2867 scsi_cmd
->scsi_done(scsi_cmd
);
2874 * pmcraid_io_done - SCSI completion function
2876 * @cmd: pointer to pmcraid command struct
2878 * This function is invoked by tasklet/mid-layer error handler to completing
2879 * the SCSI ops sent from mid-layer.
2885 static void pmcraid_io_done(struct pmcraid_cmd
*cmd
)
2887 u32 ioasc
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.ioasc
);
2888 u32 reslen
= le32_to_cpu(cmd
->ioa_cb
->ioasa
.residual_data_length
);
2890 if (_pmcraid_io_done(cmd
, reslen
, ioasc
) == 0)
2891 pmcraid_return_cmd(cmd
);
2895 * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
2897 * @cmd: command block of the command to be aborted
2900 * returns pointer to command structure used as cancelling cmd
2902 static struct pmcraid_cmd
*pmcraid_abort_cmd(struct pmcraid_cmd
*cmd
)
2904 struct pmcraid_cmd
*cancel_cmd
;
2905 struct pmcraid_instance
*pinstance
;
2906 struct pmcraid_resource_entry
*res
;
2908 pinstance
= (struct pmcraid_instance
*)cmd
->drv_inst
;
2909 res
= cmd
->scsi_cmd
->device
->hostdata
;
2911 cancel_cmd
= pmcraid_get_free_cmd(pinstance
);
2913 if (cancel_cmd
== NULL
) {
2914 pmcraid_err("%s: no cmd blocks are available\n", __func__
);
2918 pmcraid_prepare_cancel_cmd(cancel_cmd
, cmd
);
2920 pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
2921 cmd
->ioa_cb
->ioarcb
.cdb
[0],
2922 cmd
->ioa_cb
->ioarcb
.response_handle
>> 2);
2924 init_completion(&cancel_cmd
->wait_for_completion
);
2925 cancel_cmd
->completion_req
= 1;
2927 pmcraid_info("command (%d) CDB[0] = %x for %x\n",
2928 le32_to_cpu(cancel_cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2,
2929 cancel_cmd
->ioa_cb
->ioarcb
.cdb
[0],
2930 le32_to_cpu(cancel_cmd
->ioa_cb
->ioarcb
.resource_handle
));
2932 pmcraid_send_cmd(cancel_cmd
,
2933 pmcraid_internal_done
,
2934 PMCRAID_INTERNAL_TIMEOUT
,
2935 pmcraid_timeout_handler
);
2940 * pmcraid_abort_complete - Waits for ABORT TASK completion
2942 * @cancel_cmd: command block use as cancelling command
2945 * returns SUCCESS if ABORT TASK has good completion
2948 static int pmcraid_abort_complete(struct pmcraid_cmd
*cancel_cmd
)
2950 struct pmcraid_resource_entry
*res
;
2953 wait_for_completion(&cancel_cmd
->wait_for_completion
);
2954 res
= cancel_cmd
->res
;
2955 cancel_cmd
->res
= NULL
;
2956 ioasc
= le32_to_cpu(cancel_cmd
->ioa_cb
->ioasa
.ioasc
);
2958 /* If the abort task is not timed out we will get a Good completion
2959 * as sense_key, otherwise we may get one the following responses
2960 * due to subsequent bus reset or device reset. In case IOASC is
2961 * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
2963 if (ioasc
== PMCRAID_IOASC_UA_BUS_WAS_RESET
||
2964 ioasc
== PMCRAID_IOASC_NR_SYNC_REQUIRED
) {
2965 if (ioasc
== PMCRAID_IOASC_NR_SYNC_REQUIRED
)
2970 /* complete the command here itself */
2971 pmcraid_return_cmd(cancel_cmd
);
2972 return PMCRAID_IOASC_SENSE_KEY(ioasc
) ? FAILED
: SUCCESS
;
2976 * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
2978 * @scsi_cmd: scsi command struct given by mid-layer. When this is called
2979 * mid-layer ensures that no other commands are queued. This
2980 * never gets called under interrupt, but a separate eh thread.
2985 static int pmcraid_eh_abort_handler(struct scsi_cmnd
*scsi_cmd
)
2987 struct pmcraid_instance
*pinstance
;
2988 struct pmcraid_cmd
*cmd
;
2989 struct pmcraid_resource_entry
*res
;
2990 unsigned long host_lock_flags
;
2991 unsigned long pending_lock_flags
;
2992 struct pmcraid_cmd
*cancel_cmd
= NULL
;
2997 (struct pmcraid_instance
*)scsi_cmd
->device
->host
->hostdata
;
2999 scmd_printk(KERN_INFO
, scsi_cmd
,
3000 "I/O command timed out, aborting it.\n");
3002 res
= scsi_cmd
->device
->hostdata
;
3007 /* If we are currently going through reset/reload, return failed.
3008 * This will force the mid-layer to eventually call
3009 * pmcraid_eh_host_reset which will then go to sleep and wait for the
3012 spin_lock_irqsave(pinstance
->host
->host_lock
, host_lock_flags
);
3014 if (pinstance
->ioa_reset_in_progress
||
3015 pinstance
->ioa_state
== IOA_STATE_DEAD
) {
3016 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
3021 /* loop over pending cmd list to find cmd corresponding to this
3022 * scsi_cmd. Note that this command might not have been completed
3023 * already. locking: all pending commands are protected with
3024 * pending_pool_lock.
3026 spin_lock_irqsave(&pinstance
->pending_pool_lock
, pending_lock_flags
);
3027 list_for_each_entry(cmd
, &pinstance
->pending_cmd_pool
, free_list
) {
3029 if (cmd
->scsi_cmd
== scsi_cmd
) {
3035 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
,
3036 pending_lock_flags
);
3038 /* If the command to be aborted was given to IOA and still pending with
3039 * it, send ABORT_TASK to abort this and wait for its completion
3042 cancel_cmd
= pmcraid_abort_cmd(cmd
);
3044 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
3048 cancel_cmd
->res
= cmd
->scsi_cmd
->device
->hostdata
;
3049 rc
= pmcraid_abort_complete(cancel_cmd
);
3052 return cmd_found
? rc
: SUCCESS
;
3056 * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
3058 * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
3060 * All these routines invokve pmcraid_reset_device with appropriate parameters.
3061 * Since these are called from mid-layer EH thread, no other IO will be queued
3062 * to the resource being reset. However, control path (IOCTL) may be active so
3063 * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
3064 * takes care by locking/unlocking host_lock.
3069 static int pmcraid_eh_device_reset_handler(struct scsi_cmnd
*scmd
)
3071 scmd_printk(KERN_INFO
, scmd
,
3072 "resetting device due to an I/O command timeout.\n");
3073 return pmcraid_reset_device(scmd
,
3074 PMCRAID_INTERNAL_TIMEOUT
,
3078 static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd
*scmd
)
3080 scmd_printk(KERN_INFO
, scmd
,
3081 "Doing bus reset due to an I/O command timeout.\n");
3082 return pmcraid_reset_device(scmd
,
3083 PMCRAID_RESET_BUS_TIMEOUT
,
3087 static int pmcraid_eh_target_reset_handler(struct scsi_cmnd
*scmd
)
3089 scmd_printk(KERN_INFO
, scmd
,
3090 "Doing target reset due to an I/O command timeout.\n");
3091 return pmcraid_reset_device(scmd
,
3092 PMCRAID_INTERNAL_TIMEOUT
,
3093 RESET_DEVICE_TARGET
);
3097 * pmcraid_eh_host_reset_handler - adapter reset handler callback
3099 * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
3101 * Initiates adapter reset to bring it up to operational state
3106 static int pmcraid_eh_host_reset_handler(struct scsi_cmnd
*scmd
)
3108 unsigned long interval
= 10000; /* 10 seconds interval */
3109 int waits
= jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT
) / interval
;
3110 struct pmcraid_instance
*pinstance
=
3111 (struct pmcraid_instance
*)(scmd
->device
->host
->hostdata
);
3114 /* wait for an additional 150 seconds just in case firmware could come
3115 * up and if it could complete all the pending commands excluding the
3116 * two HCAM (CCN and LDN).
3119 if (atomic_read(&pinstance
->outstanding_cmds
) <=
3120 PMCRAID_MAX_HCAM_CMD
)
3125 dev_err(&pinstance
->pdev
->dev
,
3126 "Adapter being reset due to an I/O command timeout.\n");
3127 return pmcraid_reset_bringup(pinstance
) == 0 ? SUCCESS
: FAILED
;
3131 * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
3132 * @cmd: pmcraid command struct
3133 * @sgcount: count of scatter-gather elements
3136 * returns pointer pmcraid_ioadl_desc, initialized to point to internal
3137 * or external IOADLs
3139 struct pmcraid_ioadl_desc
*
3140 pmcraid_init_ioadls(struct pmcraid_cmd
*cmd
, int sgcount
)
3142 struct pmcraid_ioadl_desc
*ioadl
;
3143 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
3144 int ioadl_count
= 0;
3146 if (ioarcb
->add_cmd_param_length
)
3147 ioadl_count
= DIV_ROUND_UP(ioarcb
->add_cmd_param_length
, 16);
3148 ioarcb
->ioadl_length
=
3149 sizeof(struct pmcraid_ioadl_desc
) * sgcount
;
3151 if ((sgcount
+ ioadl_count
) > (ARRAY_SIZE(ioarcb
->add_data
.u
.ioadl
))) {
3152 /* external ioadls start at offset 0x80 from control_block
3153 * structure, re-using 24 out of 27 ioadls part of IOARCB.
3154 * It is necessary to indicate to firmware that driver is
3155 * using ioadls to be treated as external to IOARCB.
3157 ioarcb
->ioarcb_bus_addr
&= ~(0x1FULL
);
3158 ioarcb
->ioadl_bus_addr
=
3159 cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
3160 offsetof(struct pmcraid_ioarcb
,
3161 add_data
.u
.ioadl
[3]));
3162 ioadl
= &ioarcb
->add_data
.u
.ioadl
[3];
3164 ioarcb
->ioadl_bus_addr
=
3165 cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
3166 offsetof(struct pmcraid_ioarcb
,
3167 add_data
.u
.ioadl
[ioadl_count
]));
3169 ioadl
= &ioarcb
->add_data
.u
.ioadl
[ioadl_count
];
3170 ioarcb
->ioarcb_bus_addr
|=
3171 DIV_ROUND_CLOSEST(sgcount
+ ioadl_count
, 8);
3178 * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
3179 * @pinstance: pointer to adapter instance structure
3180 * @cmd: pmcraid command struct
3182 * This function is invoked by queuecommand entry point while sending a command
3183 * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
3186 * 0 on success or -1 on failure
3188 static int pmcraid_build_ioadl(
3189 struct pmcraid_instance
*pinstance
,
3190 struct pmcraid_cmd
*cmd
3194 struct scatterlist
*sglist
;
3196 struct scsi_cmnd
*scsi_cmd
= cmd
->scsi_cmd
;
3197 struct pmcraid_ioarcb
*ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
3198 struct pmcraid_ioadl_desc
*ioadl
= ioarcb
->add_data
.u
.ioadl
;
3200 u32 length
= scsi_bufflen(scsi_cmd
);
3205 nseg
= scsi_dma_map(scsi_cmd
);
3208 scmd_printk(KERN_ERR
, scsi_cmd
, "scsi_map_dma failed!\n");
3210 } else if (nseg
> PMCRAID_MAX_IOADLS
) {
3211 scsi_dma_unmap(scsi_cmd
);
3212 scmd_printk(KERN_ERR
, scsi_cmd
,
3213 "sg count is (%d) more than allowed!\n", nseg
);
3217 /* Initialize IOARCB data transfer length fields */
3218 if (scsi_cmd
->sc_data_direction
== DMA_TO_DEVICE
)
3219 ioarcb
->request_flags0
|= TRANSFER_DIR_WRITE
;
3221 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
3222 ioarcb
->data_transfer_length
= cpu_to_le32(length
);
3223 ioadl
= pmcraid_init_ioadls(cmd
, nseg
);
3225 /* Initialize IOADL descriptor addresses */
3226 scsi_for_each_sg(scsi_cmd
, sglist
, nseg
, i
) {
3227 ioadl
[i
].data_len
= cpu_to_le32(sg_dma_len(sglist
));
3228 ioadl
[i
].address
= cpu_to_le64(sg_dma_address(sglist
));
3231 /* setup last descriptor */
3232 ioadl
[i
- 1].flags
= IOADL_FLAGS_LAST_DESC
;
3238 * pmcraid_free_sglist - Frees an allocated SG buffer list
3239 * @sglist: scatter/gather list pointer
3241 * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
3246 static void pmcraid_free_sglist(struct pmcraid_sglist
*sglist
)
3250 for (i
= 0; i
< sglist
->num_sg
; i
++)
3251 __free_pages(sg_page(&(sglist
->scatterlist
[i
])),
3258 * pmcraid_alloc_sglist - Allocates memory for a SG list
3259 * @buflen: buffer length
3261 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3265 * pointer to sglist / NULL on failure
3267 static struct pmcraid_sglist
*pmcraid_alloc_sglist(int buflen
)
3269 struct pmcraid_sglist
*sglist
;
3270 struct scatterlist
*scatterlist
;
3277 sg_size
= buflen
/ (PMCRAID_MAX_IOADLS
- 1);
3278 order
= (sg_size
> 0) ? get_order(sg_size
) : 0;
3279 bsize_elem
= PAGE_SIZE
* (1 << order
);
3281 /* Determine the actual number of sg entries needed */
3282 if (buflen
% bsize_elem
)
3283 num_elem
= (buflen
/ bsize_elem
) + 1;
3285 num_elem
= buflen
/ bsize_elem
;
3287 /* Allocate a scatter/gather list for the DMA */
3288 sglist
= kzalloc(sizeof(struct pmcraid_sglist
) +
3289 (sizeof(struct scatterlist
) * (num_elem
- 1)),
3295 scatterlist
= sglist
->scatterlist
;
3296 sg_init_table(scatterlist
, num_elem
);
3297 sglist
->order
= order
;
3298 sglist
->num_sg
= num_elem
;
3301 for (i
= 0; i
< num_elem
; i
++) {
3302 page
= alloc_pages(GFP_KERNEL
|GFP_DMA
|__GFP_ZERO
, order
);
3304 for (j
= i
- 1; j
>= 0; j
--)
3305 __free_pages(sg_page(&scatterlist
[j
]), order
);
3310 sg_set_page(&scatterlist
[i
], page
,
3311 sg_size
< bsize_elem
? sg_size
: bsize_elem
, 0);
3312 sg_size
-= bsize_elem
;
3319 * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
3320 * @sglist: scatter/gather list pointer
3321 * @buffer: buffer pointer
3322 * @len: buffer length
3323 * @direction: data transfer direction
3325 * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
3328 * 0 on success / other on failure
3330 static int pmcraid_copy_sglist(
3331 struct pmcraid_sglist
*sglist
,
3332 unsigned long buffer
,
3337 struct scatterlist
*scatterlist
;
3343 /* Determine the actual number of bytes per element */
3344 bsize_elem
= PAGE_SIZE
* (1 << sglist
->order
);
3346 scatterlist
= sglist
->scatterlist
;
3348 for (i
= 0; i
< (len
/ bsize_elem
); i
++, buffer
+= bsize_elem
) {
3349 struct page
*page
= sg_page(&scatterlist
[i
]);
3352 if (direction
== DMA_TO_DEVICE
)
3353 rc
= __copy_from_user(kaddr
,
3357 rc
= __copy_to_user((void *)buffer
, kaddr
, bsize_elem
);
3362 pmcraid_err("failed to copy user data into sg list\n");
3366 scatterlist
[i
].length
= bsize_elem
;
3369 if (len
% bsize_elem
) {
3370 struct page
*page
= sg_page(&scatterlist
[i
]);
3374 if (direction
== DMA_TO_DEVICE
)
3375 rc
= __copy_from_user(kaddr
,
3379 rc
= __copy_to_user((void *)buffer
,
3385 scatterlist
[i
].length
= len
% bsize_elem
;
3389 pmcraid_err("failed to copy user data into sg list\n");
3397 * pmcraid_queuecommand - Queue a mid-layer request
3398 * @scsi_cmd: scsi command struct
3399 * @done: done function
3401 * This function queues a request generated by the mid-layer. Midlayer calls
3402 * this routine within host->lock. Some of the functions called by queuecommand
3403 * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
3407 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3408 * SCSI_MLQUEUE_HOST_BUSY if host is busy
3410 static int pmcraid_queuecommand_lck(
3411 struct scsi_cmnd
*scsi_cmd
,
3412 void (*done
) (struct scsi_cmnd
*)
3415 struct pmcraid_instance
*pinstance
;
3416 struct pmcraid_resource_entry
*res
;
3417 struct pmcraid_ioarcb
*ioarcb
;
3418 struct pmcraid_cmd
*cmd
;
3423 (struct pmcraid_instance
*)scsi_cmd
->device
->host
->hostdata
;
3424 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
3425 scsi_cmd
->scsi_done
= done
;
3426 res
= scsi_cmd
->device
->hostdata
;
3427 scsi_cmd
->result
= (DID_OK
<< 16);
3429 /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
3432 if (pinstance
->ioa_state
== IOA_STATE_DEAD
) {
3433 pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
3434 scsi_cmd
->result
= (DID_NO_CONNECT
<< 16);
3435 scsi_cmd
->scsi_done(scsi_cmd
);
3439 /* If IOA reset is in progress, can't queue the commands */
3440 if (pinstance
->ioa_reset_in_progress
)
3441 return SCSI_MLQUEUE_HOST_BUSY
;
3443 /* Firmware doesn't support SYNCHRONIZE_CACHE command (0x35), complete
3444 * the command here itself with success return
3446 if (scsi_cmd
->cmnd
[0] == SYNCHRONIZE_CACHE
) {
3447 pmcraid_info("SYNC_CACHE(0x35), completing in driver itself\n");
3448 scsi_cmd
->scsi_done(scsi_cmd
);
3452 /* initialize the command and IOARCB to be sent to IOA */
3453 cmd
= pmcraid_get_free_cmd(pinstance
);
3456 pmcraid_err("free command block is not available\n");
3457 return SCSI_MLQUEUE_HOST_BUSY
;
3460 cmd
->scsi_cmd
= scsi_cmd
;
3461 ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
3462 memcpy(ioarcb
->cdb
, scsi_cmd
->cmnd
, scsi_cmd
->cmd_len
);
3463 ioarcb
->resource_handle
= res
->cfg_entry
.resource_handle
;
3464 ioarcb
->request_type
= REQ_TYPE_SCSI
;
3466 /* set hrrq number where the IOA should respond to. Note that all cmds
3467 * generated internally uses hrrq_id 0, exception to this is the cmd
3468 * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
3469 * hrrq_id assigned here in queuecommand
3471 ioarcb
->hrrq_id
= atomic_add_return(1, &(pinstance
->last_message_id
)) %
3472 pinstance
->num_hrrq
;
3473 cmd
->cmd_done
= pmcraid_io_done
;
3475 if (RES_IS_GSCSI(res
->cfg_entry
) || RES_IS_VSET(res
->cfg_entry
)) {
3476 if (scsi_cmd
->underflow
== 0)
3477 ioarcb
->request_flags0
|= INHIBIT_UL_CHECK
;
3479 if (res
->sync_reqd
) {
3480 ioarcb
->request_flags0
|= SYNC_COMPLETE
;
3484 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
3486 if (scsi_cmd
->flags
& SCMD_TAGGED
)
3487 ioarcb
->request_flags1
|= TASK_TAG_SIMPLE
;
3489 if (RES_IS_GSCSI(res
->cfg_entry
))
3490 ioarcb
->request_flags1
|= DELAY_AFTER_RESET
;
3493 rc
= pmcraid_build_ioadl(pinstance
, cmd
);
3495 pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
3496 le32_to_cpu(ioarcb
->response_handle
) >> 2,
3497 scsi_cmd
->cmnd
[0], pinstance
->host
->unique_id
,
3498 RES_IS_VSET(res
->cfg_entry
) ? PMCRAID_VSET_BUS_ID
:
3499 PMCRAID_PHYS_BUS_ID
,
3500 RES_IS_VSET(res
->cfg_entry
) ?
3501 (fw_version
<= PMCRAID_FW_VERSION_1
?
3502 res
->cfg_entry
.unique_flags1
:
3503 res
->cfg_entry
.array_id
& 0xFF) :
3504 RES_TARGET(res
->cfg_entry
.resource_address
),
3505 RES_LUN(res
->cfg_entry
.resource_address
));
3507 if (likely(rc
== 0)) {
3508 _pmcraid_fire_command(cmd
);
3510 pmcraid_err("queuecommand could not build ioadl\n");
3511 pmcraid_return_cmd(cmd
);
3512 rc
= SCSI_MLQUEUE_HOST_BUSY
;
3518 static DEF_SCSI_QCMD(pmcraid_queuecommand
)
3521 * pmcraid_open -char node "open" entry, allowed only users with admin access
3523 static int pmcraid_chr_open(struct inode
*inode
, struct file
*filep
)
3525 struct pmcraid_instance
*pinstance
;
3527 if (!capable(CAP_SYS_ADMIN
))
3530 /* Populate adapter instance * pointer for use by ioctl */
3531 pinstance
= container_of(inode
->i_cdev
, struct pmcraid_instance
, cdev
);
3532 filep
->private_data
= pinstance
;
3538 * pmcraid_fasync - Async notifier registration from applications
3540 * This function adds the calling process to a driver global queue. When an
3541 * event occurs, SIGIO will be sent to all processes in this queue.
3543 static int pmcraid_chr_fasync(int fd
, struct file
*filep
, int mode
)
3545 struct pmcraid_instance
*pinstance
;
3548 pinstance
= filep
->private_data
;
3549 mutex_lock(&pinstance
->aen_queue_lock
);
3550 rc
= fasync_helper(fd
, filep
, mode
, &pinstance
->aen_queue
);
3551 mutex_unlock(&pinstance
->aen_queue_lock
);
3558 * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
3559 * commands sent over IOCTL interface
3561 * @cmd : pointer to struct pmcraid_cmd
3562 * @buflen : length of the request buffer
3563 * @direction : data transfer direction
3566 * 0 on success, non-zero error code on failure
3568 static int pmcraid_build_passthrough_ioadls(
3569 struct pmcraid_cmd
*cmd
,
3574 struct pmcraid_sglist
*sglist
= NULL
;
3575 struct scatterlist
*sg
= NULL
;
3576 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
3577 struct pmcraid_ioadl_desc
*ioadl
;
3580 sglist
= pmcraid_alloc_sglist(buflen
);
3583 pmcraid_err("can't allocate memory for passthrough SGls\n");
3587 sglist
->num_dma_sg
= pci_map_sg(cmd
->drv_inst
->pdev
,
3588 sglist
->scatterlist
,
3589 sglist
->num_sg
, direction
);
3591 if (!sglist
->num_dma_sg
|| sglist
->num_dma_sg
> PMCRAID_MAX_IOADLS
) {
3592 dev_err(&cmd
->drv_inst
->pdev
->dev
,
3593 "Failed to map passthrough buffer!\n");
3594 pmcraid_free_sglist(sglist
);
3598 cmd
->sglist
= sglist
;
3599 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
3601 ioadl
= pmcraid_init_ioadls(cmd
, sglist
->num_dma_sg
);
3603 /* Initialize IOADL descriptor addresses */
3604 for_each_sg(sglist
->scatterlist
, sg
, sglist
->num_dma_sg
, i
) {
3605 ioadl
[i
].data_len
= cpu_to_le32(sg_dma_len(sg
));
3606 ioadl
[i
].address
= cpu_to_le64(sg_dma_address(sg
));
3610 /* setup the last descriptor */
3611 ioadl
[i
- 1].flags
= IOADL_FLAGS_LAST_DESC
;
3618 * pmcraid_release_passthrough_ioadls - release passthrough ioadls
3620 * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
3621 * @buflen: size of the request buffer
3622 * @direction: data transfer direction
3625 * 0 on success, non-zero error code on failure
3627 static void pmcraid_release_passthrough_ioadls(
3628 struct pmcraid_cmd
*cmd
,
3633 struct pmcraid_sglist
*sglist
= cmd
->sglist
;
3636 pci_unmap_sg(cmd
->drv_inst
->pdev
,
3637 sglist
->scatterlist
,
3640 pmcraid_free_sglist(sglist
);
3646 * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
3648 * @pinstance: pointer to adapter instance structure
3650 * @arg: pointer to pmcraid_passthrough_buffer user buffer
3653 * 0 on success, non-zero error code on failure
3655 static long pmcraid_ioctl_passthrough(
3656 struct pmcraid_instance
*pinstance
,
3657 unsigned int ioctl_cmd
,
3658 unsigned int buflen
,
3662 struct pmcraid_passthrough_ioctl_buffer
*buffer
;
3663 struct pmcraid_ioarcb
*ioarcb
;
3664 struct pmcraid_cmd
*cmd
;
3665 struct pmcraid_cmd
*cancel_cmd
;
3666 unsigned long request_buffer
;
3667 unsigned long request_offset
;
3668 unsigned long lock_flags
;
3673 u8 access
, direction
;
3676 /* If IOA reset is in progress, wait 10 secs for reset to complete */
3677 if (pinstance
->ioa_reset_in_progress
) {
3678 rc
= wait_event_interruptible_timeout(
3679 pinstance
->reset_wait_q
,
3680 !pinstance
->ioa_reset_in_progress
,
3681 msecs_to_jiffies(10000));
3686 return -ERESTARTSYS
;
3689 /* If adapter is not in operational state, return error */
3690 if (pinstance
->ioa_state
!= IOA_STATE_OPERATIONAL
) {
3691 pmcraid_err("IOA is not operational\n");
3695 buffer_size
= sizeof(struct pmcraid_passthrough_ioctl_buffer
);
3696 buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
3699 pmcraid_err("no memory for passthrough buffer\n");
3704 offsetof(struct pmcraid_passthrough_ioctl_buffer
, request_buffer
);
3706 request_buffer
= arg
+ request_offset
;
3708 rc
= __copy_from_user(buffer
,
3709 (struct pmcraid_passthrough_ioctl_buffer
*) arg
,
3710 sizeof(struct pmcraid_passthrough_ioctl_buffer
));
3714 offsetof(struct pmcraid_passthrough_ioctl_buffer
, ioasa
));
3717 pmcraid_err("ioctl: can't copy passthrough buffer\n");
3719 goto out_free_buffer
;
3722 request_size
= buffer
->ioarcb
.data_transfer_length
;
3724 if (buffer
->ioarcb
.request_flags0
& TRANSFER_DIR_WRITE
) {
3725 access
= VERIFY_READ
;
3726 direction
= DMA_TO_DEVICE
;
3728 access
= VERIFY_WRITE
;
3729 direction
= DMA_FROM_DEVICE
;
3732 if (request_size
> 0) {
3733 rc
= access_ok(access
, arg
, request_offset
+ request_size
);
3737 goto out_free_buffer
;
3739 } else if (request_size
< 0) {
3741 goto out_free_buffer
;
3744 /* check if we have any additional command parameters */
3745 if (buffer
->ioarcb
.add_cmd_param_length
> PMCRAID_ADD_CMD_PARAM_LEN
) {
3747 goto out_free_buffer
;
3750 cmd
= pmcraid_get_free_cmd(pinstance
);
3753 pmcraid_err("free command block is not available\n");
3755 goto out_free_buffer
;
3758 cmd
->scsi_cmd
= NULL
;
3759 ioarcb
= &(cmd
->ioa_cb
->ioarcb
);
3761 /* Copy the user-provided IOARCB stuff field by field */
3762 ioarcb
->resource_handle
= buffer
->ioarcb
.resource_handle
;
3763 ioarcb
->data_transfer_length
= buffer
->ioarcb
.data_transfer_length
;
3764 ioarcb
->cmd_timeout
= buffer
->ioarcb
.cmd_timeout
;
3765 ioarcb
->request_type
= buffer
->ioarcb
.request_type
;
3766 ioarcb
->request_flags0
= buffer
->ioarcb
.request_flags0
;
3767 ioarcb
->request_flags1
= buffer
->ioarcb
.request_flags1
;
3768 memcpy(ioarcb
->cdb
, buffer
->ioarcb
.cdb
, PMCRAID_MAX_CDB_LEN
);
3770 if (buffer
->ioarcb
.add_cmd_param_length
) {
3771 ioarcb
->add_cmd_param_length
=
3772 buffer
->ioarcb
.add_cmd_param_length
;
3773 ioarcb
->add_cmd_param_offset
=
3774 buffer
->ioarcb
.add_cmd_param_offset
;
3775 memcpy(ioarcb
->add_data
.u
.add_cmd_params
,
3776 buffer
->ioarcb
.add_data
.u
.add_cmd_params
,
3777 buffer
->ioarcb
.add_cmd_param_length
);
3780 /* set hrrq number where the IOA should respond to. Note that all cmds
3781 * generated internally uses hrrq_id 0, exception to this is the cmd
3782 * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
3783 * hrrq_id assigned here in queuecommand
3785 ioarcb
->hrrq_id
= atomic_add_return(1, &(pinstance
->last_message_id
)) %
3786 pinstance
->num_hrrq
;
3789 rc
= pmcraid_build_passthrough_ioadls(cmd
,
3793 pmcraid_err("couldn't build passthrough ioadls\n");
3794 goto out_free_buffer
;
3796 } else if (request_size
< 0) {
3798 goto out_free_buffer
;
3801 /* If data is being written into the device, copy the data from user
3804 if (direction
== DMA_TO_DEVICE
&& request_size
> 0) {
3805 rc
= pmcraid_copy_sglist(cmd
->sglist
,
3810 pmcraid_err("failed to copy user buffer\n");
3811 goto out_free_sglist
;
3815 /* passthrough ioctl is a blocking command so, put the user to sleep
3816 * until timeout. Note that a timeout value of 0 means, do timeout.
3818 cmd
->cmd_done
= pmcraid_internal_done
;
3819 init_completion(&cmd
->wait_for_completion
);
3820 cmd
->completion_req
= 1;
3822 pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
3823 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
) >> 2,
3824 cmd
->ioa_cb
->ioarcb
.cdb
[0],
3825 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.resource_handle
));
3827 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
3828 _pmcraid_fire_command(cmd
);
3829 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
3831 /* NOTE ! Remove the below line once abort_task is implemented
3832 * in firmware. This line disables ioctl command timeout handling logic
3833 * similar to IO command timeout handling, making ioctl commands to wait
3834 * until the command completion regardless of timeout value specified in
3837 buffer
->ioarcb
.cmd_timeout
= 0;
3839 /* If command timeout is specified put caller to wait till that time,
3840 * otherwise it would be blocking wait. If command gets timed out, it
3843 if (buffer
->ioarcb
.cmd_timeout
== 0) {
3844 wait_for_completion(&cmd
->wait_for_completion
);
3845 } else if (!wait_for_completion_timeout(
3846 &cmd
->wait_for_completion
,
3847 msecs_to_jiffies(buffer
->ioarcb
.cmd_timeout
* 1000))) {
3849 pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
3850 le32_to_cpu(cmd
->ioa_cb
->ioarcb
.response_handle
>> 2),
3851 cmd
->ioa_cb
->ioarcb
.cdb
[0]);
3853 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
3854 cancel_cmd
= pmcraid_abort_cmd(cmd
);
3855 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
3858 wait_for_completion(&cancel_cmd
->wait_for_completion
);
3859 ioasc
= cancel_cmd
->ioa_cb
->ioasa
.ioasc
;
3860 pmcraid_return_cmd(cancel_cmd
);
3862 /* if abort task couldn't find the command i.e it got
3863 * completed prior to aborting, return good completion.
3864 * if command got aborted successfully or there was IOA
3865 * reset due to abort task itself getting timedout then
3868 if (ioasc
== PMCRAID_IOASC_IOA_WAS_RESET
||
3869 PMCRAID_IOASC_SENSE_KEY(ioasc
) == 0x00) {
3870 if (ioasc
!= PMCRAID_IOASC_GC_IOARCB_NOTFOUND
)
3872 goto out_handle_response
;
3876 /* no command block for abort task or abort task failed to abort
3877 * the IOARCB, then wait for 150 more seconds and initiate reset
3878 * sequence after timeout
3880 if (!wait_for_completion_timeout(
3881 &cmd
->wait_for_completion
,
3882 msecs_to_jiffies(150 * 1000))) {
3883 pmcraid_reset_bringup(cmd
->drv_inst
);
3888 out_handle_response
:
3889 /* copy entire IOASA buffer and return IOCTL success.
3890 * If copying IOASA to user-buffer fails, return
3893 if (copy_to_user(ioasa
, &cmd
->ioa_cb
->ioasa
,
3894 sizeof(struct pmcraid_ioasa
))) {
3895 pmcraid_err("failed to copy ioasa buffer to user\n");
3899 /* If the data transfer was from device, copy the data onto user
3902 else if (direction
== DMA_FROM_DEVICE
&& request_size
> 0) {
3903 rc
= pmcraid_copy_sglist(cmd
->sglist
,
3908 pmcraid_err("failed to copy user buffer\n");
3914 pmcraid_release_passthrough_ioadls(cmd
, request_size
, direction
);
3915 pmcraid_return_cmd(cmd
);
3927 * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
3929 * @pinstance: pointer to adapter instance structure
3930 * @cmd: ioctl command passed in
3931 * @buflen: length of user_buffer
3932 * @user_buffer: user buffer pointer
3935 * 0 in case of success, otherwise appropriate error code
3937 static long pmcraid_ioctl_driver(
3938 struct pmcraid_instance
*pinstance
,
3940 unsigned int buflen
,
3941 void __user
*user_buffer
3946 if (!access_ok(VERIFY_READ
, user_buffer
, _IOC_SIZE(cmd
))) {
3947 pmcraid_err("ioctl_driver: access fault in request buffer\n");
3952 case PMCRAID_IOCTL_RESET_ADAPTER
:
3953 pmcraid_reset_bringup(pinstance
);
3965 * pmcraid_check_ioctl_buffer - check for proper access to user buffer
3967 * @cmd: ioctl command
3969 * @hdr: pointer to kernel memory for pmcraid_ioctl_header
3972 * negetive error code if there are access issues, otherwise zero.
3973 * Upon success, returns ioctl header copied out of user buffer.
3976 static int pmcraid_check_ioctl_buffer(
3979 struct pmcraid_ioctl_header
*hdr
3983 int access
= VERIFY_READ
;
3985 if (copy_from_user(hdr
, arg
, sizeof(struct pmcraid_ioctl_header
))) {
3986 pmcraid_err("couldn't copy ioctl header from user buffer\n");
3990 /* check for valid driver signature */
3991 rc
= memcmp(hdr
->signature
,
3992 PMCRAID_IOCTL_SIGNATURE
,
3993 sizeof(hdr
->signature
));
3995 pmcraid_err("signature verification failed\n");
3999 /* check for appropriate buffer access */
4000 if ((_IOC_DIR(cmd
) & _IOC_READ
) == _IOC_READ
)
4001 access
= VERIFY_WRITE
;
4003 rc
= access_ok(access
,
4004 (arg
+ sizeof(struct pmcraid_ioctl_header
)),
4005 hdr
->buffer_length
);
4007 pmcraid_err("access failed for user buffer of size %d\n",
4008 hdr
->buffer_length
);
4016 * pmcraid_ioctl - char node ioctl entry point
4018 static long pmcraid_chr_ioctl(
4024 struct pmcraid_instance
*pinstance
= NULL
;
4025 struct pmcraid_ioctl_header
*hdr
= NULL
;
4026 int retval
= -ENOTTY
;
4028 hdr
= kmalloc(sizeof(struct pmcraid_ioctl_header
), GFP_KERNEL
);
4031 pmcraid_err("failed to allocate memory for ioctl header\n");
4035 retval
= pmcraid_check_ioctl_buffer(cmd
, (void *)arg
, hdr
);
4038 pmcraid_info("chr_ioctl: header check failed\n");
4043 pinstance
= filep
->private_data
;
4046 pmcraid_info("adapter instance is not found\n");
4051 switch (_IOC_TYPE(cmd
)) {
4053 case PMCRAID_PASSTHROUGH_IOCTL
:
4054 /* If ioctl code is to download microcode, we need to block
4055 * mid-layer requests.
4057 if (cmd
== PMCRAID_IOCTL_DOWNLOAD_MICROCODE
)
4058 scsi_block_requests(pinstance
->host
);
4060 retval
= pmcraid_ioctl_passthrough(pinstance
,
4065 if (cmd
== PMCRAID_IOCTL_DOWNLOAD_MICROCODE
)
4066 scsi_unblock_requests(pinstance
->host
);
4069 case PMCRAID_DRIVER_IOCTL
:
4070 arg
+= sizeof(struct pmcraid_ioctl_header
);
4071 retval
= pmcraid_ioctl_driver(pinstance
,
4074 (void __user
*)arg
);
4088 * File operations structure for management interface
4090 static const struct file_operations pmcraid_fops
= {
4091 .owner
= THIS_MODULE
,
4092 .open
= pmcraid_chr_open
,
4093 .fasync
= pmcraid_chr_fasync
,
4094 .unlocked_ioctl
= pmcraid_chr_ioctl
,
4095 #ifdef CONFIG_COMPAT
4096 .compat_ioctl
= pmcraid_chr_ioctl
,
4098 .llseek
= noop_llseek
,
4105 * pmcraid_show_log_level - Display adapter's error logging level
4106 * @dev: class device struct
4110 * number of bytes printed to buffer
4112 static ssize_t
pmcraid_show_log_level(
4114 struct device_attribute
*attr
,
4117 struct Scsi_Host
*shost
= class_to_shost(dev
);
4118 struct pmcraid_instance
*pinstance
=
4119 (struct pmcraid_instance
*)shost
->hostdata
;
4120 return snprintf(buf
, PAGE_SIZE
, "%d\n", pinstance
->current_log_level
);
4124 * pmcraid_store_log_level - Change the adapter's error logging level
4125 * @dev: class device struct
4130 * number of bytes printed to buffer
4132 static ssize_t
pmcraid_store_log_level(
4134 struct device_attribute
*attr
,
4139 struct Scsi_Host
*shost
;
4140 struct pmcraid_instance
*pinstance
;
4143 if (kstrtou8(buf
, 10, &val
))
4145 /* log-level should be from 0 to 2 */
4149 shost
= class_to_shost(dev
);
4150 pinstance
= (struct pmcraid_instance
*)shost
->hostdata
;
4151 pinstance
->current_log_level
= val
;
4156 static struct device_attribute pmcraid_log_level_attr
= {
4158 .name
= "log_level",
4159 .mode
= S_IRUGO
| S_IWUSR
,
4161 .show
= pmcraid_show_log_level
,
4162 .store
= pmcraid_store_log_level
,
4166 * pmcraid_show_drv_version - Display driver version
4167 * @dev: class device struct
4171 * number of bytes printed to buffer
4173 static ssize_t
pmcraid_show_drv_version(
4175 struct device_attribute
*attr
,
4179 return snprintf(buf
, PAGE_SIZE
, "version: %s\n",
4180 PMCRAID_DRIVER_VERSION
);
4183 static struct device_attribute pmcraid_driver_version_attr
= {
4185 .name
= "drv_version",
4188 .show
= pmcraid_show_drv_version
,
4192 * pmcraid_show_io_adapter_id - Display driver assigned adapter id
4193 * @dev: class device struct
4197 * number of bytes printed to buffer
4199 static ssize_t
pmcraid_show_adapter_id(
4201 struct device_attribute
*attr
,
4205 struct Scsi_Host
*shost
= class_to_shost(dev
);
4206 struct pmcraid_instance
*pinstance
=
4207 (struct pmcraid_instance
*)shost
->hostdata
;
4208 u32 adapter_id
= (pinstance
->pdev
->bus
->number
<< 8) |
4209 pinstance
->pdev
->devfn
;
4210 u32 aen_group
= pmcraid_event_family
.id
;
4212 return snprintf(buf
, PAGE_SIZE
,
4213 "adapter id: %d\nminor: %d\naen group: %d\n",
4214 adapter_id
, MINOR(pinstance
->cdev
.dev
), aen_group
);
4217 static struct device_attribute pmcraid_adapter_id_attr
= {
4219 .name
= "adapter_id",
4222 .show
= pmcraid_show_adapter_id
,
4225 static struct device_attribute
*pmcraid_host_attrs
[] = {
4226 &pmcraid_log_level_attr
,
4227 &pmcraid_driver_version_attr
,
4228 &pmcraid_adapter_id_attr
,
4233 /* host template structure for pmcraid driver */
4234 static struct scsi_host_template pmcraid_host_template
= {
4235 .module
= THIS_MODULE
,
4236 .name
= PMCRAID_DRIVER_NAME
,
4237 .queuecommand
= pmcraid_queuecommand
,
4238 .eh_abort_handler
= pmcraid_eh_abort_handler
,
4239 .eh_bus_reset_handler
= pmcraid_eh_bus_reset_handler
,
4240 .eh_target_reset_handler
= pmcraid_eh_target_reset_handler
,
4241 .eh_device_reset_handler
= pmcraid_eh_device_reset_handler
,
4242 .eh_host_reset_handler
= pmcraid_eh_host_reset_handler
,
4244 .slave_alloc
= pmcraid_slave_alloc
,
4245 .slave_configure
= pmcraid_slave_configure
,
4246 .slave_destroy
= pmcraid_slave_destroy
,
4247 .change_queue_depth
= pmcraid_change_queue_depth
,
4248 .can_queue
= PMCRAID_MAX_IO_CMD
,
4250 .sg_tablesize
= PMCRAID_MAX_IOADLS
,
4251 .max_sectors
= PMCRAID_IOA_MAX_SECTORS
,
4253 .cmd_per_lun
= PMCRAID_MAX_CMD_PER_LUN
,
4254 .use_clustering
= ENABLE_CLUSTERING
,
4255 .shost_attrs
= pmcraid_host_attrs
,
4256 .proc_name
= PMCRAID_DRIVER_NAME
,
4260 * pmcraid_isr_msix - implements MSI-X interrupt handling routine
4261 * @irq: interrupt vector number
4262 * @dev_id: pointer hrrq_vector
4265 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4268 static irqreturn_t
pmcraid_isr_msix(int irq
, void *dev_id
)
4270 struct pmcraid_isr_param
*hrrq_vector
;
4271 struct pmcraid_instance
*pinstance
;
4272 unsigned long lock_flags
;
4276 hrrq_vector
= (struct pmcraid_isr_param
*)dev_id
;
4277 hrrq_id
= hrrq_vector
->hrrq_id
;
4278 pinstance
= hrrq_vector
->drv_inst
;
4281 /* Read the interrupt */
4282 intrs_val
= pmcraid_read_interrupts(pinstance
);
4284 ((ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
)
4285 & DOORBELL_INTR_MSIX_CLR
) == 0)) {
4286 /* Any error interrupts including unit_check,
4287 * initiate IOA reset.In case of unit check indicate
4288 * to reset_sequence that IOA unit checked and prepare
4289 * for a dump during reset sequence
4291 if (intrs_val
& PMCRAID_ERROR_INTERRUPTS
) {
4292 if (intrs_val
& INTRS_IOA_UNIT_CHECK
)
4293 pinstance
->ioa_unit_check
= 1;
4295 pmcraid_err("ISR: error interrupts: %x \
4296 initiating reset\n", intrs_val
);
4297 spin_lock_irqsave(pinstance
->host
->host_lock
,
4299 pmcraid_initiate_reset(pinstance
);
4300 spin_unlock_irqrestore(
4301 pinstance
->host
->host_lock
,
4304 /* If interrupt was as part of the ioa initialization,
4305 * clear it. Delete the timer and wakeup the
4306 * reset engine to proceed with reset sequence
4308 if (intrs_val
& INTRS_TRANSITION_TO_OPERATIONAL
)
4309 pmcraid_clr_trans_op(pinstance
);
4311 /* Clear the interrupt register by writing
4312 * to host to ioa doorbell. Once done
4313 * FW will clear the interrupt.
4315 iowrite32(DOORBELL_INTR_MSIX_CLR
,
4316 pinstance
->int_regs
.host_ioa_interrupt_reg
);
4317 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
4323 tasklet_schedule(&(pinstance
->isr_tasklet
[hrrq_id
]));
4329 * pmcraid_isr - implements legacy interrupt handling routine
4331 * @irq: interrupt vector number
4332 * @dev_id: pointer hrrq_vector
4335 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4337 static irqreturn_t
pmcraid_isr(int irq
, void *dev_id
)
4339 struct pmcraid_isr_param
*hrrq_vector
;
4340 struct pmcraid_instance
*pinstance
;
4342 unsigned long lock_flags
;
4345 /* In case of legacy interrupt mode where interrupts are shared across
4346 * isrs, it may be possible that the current interrupt is not from IOA
4349 printk(KERN_INFO
"%s(): NULL host pointer\n", __func__
);
4352 hrrq_vector
= (struct pmcraid_isr_param
*)dev_id
;
4353 pinstance
= hrrq_vector
->drv_inst
;
4355 intrs
= pmcraid_read_interrupts(pinstance
);
4357 if (unlikely((intrs
& PMCRAID_PCI_INTERRUPTS
) == 0))
4360 /* Any error interrupts including unit_check, initiate IOA reset.
4361 * In case of unit check indicate to reset_sequence that IOA unit
4362 * checked and prepare for a dump during reset sequence
4364 if (intrs
& PMCRAID_ERROR_INTERRUPTS
) {
4366 if (intrs
& INTRS_IOA_UNIT_CHECK
)
4367 pinstance
->ioa_unit_check
= 1;
4370 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4371 pmcraid_err("ISR: error interrupts: %x initiating reset\n",
4374 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4375 spin_lock_irqsave(pinstance
->host
->host_lock
, lock_flags
);
4376 pmcraid_initiate_reset(pinstance
);
4377 spin_unlock_irqrestore(pinstance
->host
->host_lock
, lock_flags
);
4379 /* If interrupt was as part of the ioa initialization,
4380 * clear. Delete the timer and wakeup the
4381 * reset engine to proceed with reset sequence
4383 if (intrs
& INTRS_TRANSITION_TO_OPERATIONAL
) {
4384 pmcraid_clr_trans_op(pinstance
);
4387 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4389 pinstance
->int_regs
.ioa_host_interrupt_clr_reg
);
4392 &(pinstance
->isr_tasklet
[hrrq_id
]));
4401 * pmcraid_worker_function - worker thread function
4403 * @workp: pointer to struct work queue
4409 static void pmcraid_worker_function(struct work_struct
*workp
)
4411 struct pmcraid_instance
*pinstance
;
4412 struct pmcraid_resource_entry
*res
;
4413 struct pmcraid_resource_entry
*temp
;
4414 struct scsi_device
*sdev
;
4415 unsigned long lock_flags
;
4416 unsigned long host_lock_flags
;
4418 u8 bus
, target
, lun
;
4420 pinstance
= container_of(workp
, struct pmcraid_instance
, worker_q
);
4421 /* add resources only after host is added into system */
4422 if (!atomic_read(&pinstance
->expose_resources
))
4425 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
4427 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
4428 list_for_each_entry_safe(res
, temp
, &pinstance
->used_res_q
, queue
) {
4430 if (res
->change_detected
== RES_CHANGE_DEL
&& res
->scsi_dev
) {
4431 sdev
= res
->scsi_dev
;
4433 /* host_lock must be held before calling
4436 spin_lock_irqsave(pinstance
->host
->host_lock
,
4438 if (!scsi_device_get(sdev
)) {
4439 spin_unlock_irqrestore(
4440 pinstance
->host
->host_lock
,
4442 pmcraid_info("deleting %x from midlayer\n",
4443 res
->cfg_entry
.resource_address
);
4444 list_move_tail(&res
->queue
,
4445 &pinstance
->free_res_q
);
4446 spin_unlock_irqrestore(
4447 &pinstance
->resource_lock
,
4449 scsi_remove_device(sdev
);
4450 scsi_device_put(sdev
);
4451 spin_lock_irqsave(&pinstance
->resource_lock
,
4453 res
->change_detected
= 0;
4455 spin_unlock_irqrestore(
4456 pinstance
->host
->host_lock
,
4462 list_for_each_entry(res
, &pinstance
->used_res_q
, queue
) {
4464 if (res
->change_detected
== RES_CHANGE_ADD
) {
4466 if (!pmcraid_expose_resource(fw_version
,
4470 if (RES_IS_VSET(res
->cfg_entry
)) {
4471 bus
= PMCRAID_VSET_BUS_ID
;
4472 if (fw_version
<= PMCRAID_FW_VERSION_1
)
4473 target
= res
->cfg_entry
.unique_flags1
;
4475 target
= res
->cfg_entry
.array_id
& 0xFF;
4476 lun
= PMCRAID_VSET_LUN_ID
;
4478 bus
= PMCRAID_PHYS_BUS_ID
;
4481 res
->cfg_entry
.resource_address
);
4482 lun
= RES_LUN(res
->cfg_entry
.resource_address
);
4485 res
->change_detected
= 0;
4486 spin_unlock_irqrestore(&pinstance
->resource_lock
,
4488 scsi_add_device(pinstance
->host
, bus
, target
, lun
);
4489 spin_lock_irqsave(&pinstance
->resource_lock
,
4494 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
4498 * pmcraid_tasklet_function - Tasklet function
4500 * @instance: pointer to msix param structure
4505 static void pmcraid_tasklet_function(unsigned long instance
)
4507 struct pmcraid_isr_param
*hrrq_vector
;
4508 struct pmcraid_instance
*pinstance
;
4509 unsigned long hrrq_lock_flags
;
4510 unsigned long pending_lock_flags
;
4511 unsigned long host_lock_flags
;
4512 spinlock_t
*lockp
; /* hrrq buffer lock */
4516 hrrq_vector
= (struct pmcraid_isr_param
*)instance
;
4517 pinstance
= hrrq_vector
->drv_inst
;
4518 id
= hrrq_vector
->hrrq_id
;
4519 lockp
= &(pinstance
->hrrq_lock
[id
]);
4521 /* loop through each of the commands responded by IOA. Each HRRQ buf is
4522 * protected by its own lock. Traversals must be done within this lock
4523 * as there may be multiple tasklets running on multiple CPUs. Note
4524 * that the lock is held just for picking up the response handle and
4525 * manipulating hrrq_curr/toggle_bit values.
4527 spin_lock_irqsave(lockp
, hrrq_lock_flags
);
4529 resp
= le32_to_cpu(*(pinstance
->hrrq_curr
[id
]));
4531 while ((resp
& HRRQ_TOGGLE_BIT
) ==
4532 pinstance
->host_toggle_bit
[id
]) {
4534 int cmd_index
= resp
>> 2;
4535 struct pmcraid_cmd
*cmd
= NULL
;
4537 if (pinstance
->hrrq_curr
[id
] < pinstance
->hrrq_end
[id
]) {
4538 pinstance
->hrrq_curr
[id
]++;
4540 pinstance
->hrrq_curr
[id
] = pinstance
->hrrq_start
[id
];
4541 pinstance
->host_toggle_bit
[id
] ^= 1u;
4544 if (cmd_index
>= PMCRAID_MAX_CMD
) {
4545 /* In case of invalid response handle, log message */
4546 pmcraid_err("Invalid response handle %d\n", cmd_index
);
4547 resp
= le32_to_cpu(*(pinstance
->hrrq_curr
[id
]));
4551 cmd
= pinstance
->cmd_list
[cmd_index
];
4552 spin_unlock_irqrestore(lockp
, hrrq_lock_flags
);
4554 spin_lock_irqsave(&pinstance
->pending_pool_lock
,
4555 pending_lock_flags
);
4556 list_del(&cmd
->free_list
);
4557 spin_unlock_irqrestore(&pinstance
->pending_pool_lock
,
4558 pending_lock_flags
);
4559 del_timer(&cmd
->timer
);
4560 atomic_dec(&pinstance
->outstanding_cmds
);
4562 if (cmd
->cmd_done
== pmcraid_ioa_reset
) {
4563 spin_lock_irqsave(pinstance
->host
->host_lock
,
4566 spin_unlock_irqrestore(pinstance
->host
->host_lock
,
4568 } else if (cmd
->cmd_done
!= NULL
) {
4571 /* loop over until we are done with all responses */
4572 spin_lock_irqsave(lockp
, hrrq_lock_flags
);
4573 resp
= le32_to_cpu(*(pinstance
->hrrq_curr
[id
]));
4576 spin_unlock_irqrestore(lockp
, hrrq_lock_flags
);
4580 * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
4581 * @pinstance: pointer to adapter instance structure
4583 * This routine un-registers registered interrupt handler and
4584 * also frees irqs/vectors.
4590 void pmcraid_unregister_interrupt_handler(struct pmcraid_instance
*pinstance
)
4594 for (i
= 0; i
< pinstance
->num_hrrq
; i
++)
4595 free_irq(pinstance
->hrrq_vector
[i
].vector
,
4596 &(pinstance
->hrrq_vector
[i
]));
4598 if (pinstance
->interrupt_mode
) {
4599 pci_disable_msix(pinstance
->pdev
);
4600 pinstance
->interrupt_mode
= 0;
4605 * pmcraid_register_interrupt_handler - registers interrupt handler
4606 * @pinstance: pointer to per-adapter instance structure
4609 * 0 on success, non-zero error code otherwise.
4612 pmcraid_register_interrupt_handler(struct pmcraid_instance
*pinstance
)
4615 struct pci_dev
*pdev
= pinstance
->pdev
;
4617 if ((pmcraid_enable_msix
) &&
4618 (pci_find_capability(pdev
, PCI_CAP_ID_MSIX
))) {
4619 int num_hrrq
= PMCRAID_NUM_MSIX_VECTORS
;
4620 struct msix_entry entries
[PMCRAID_NUM_MSIX_VECTORS
];
4622 for (i
= 0; i
< PMCRAID_NUM_MSIX_VECTORS
; i
++)
4623 entries
[i
].entry
= i
;
4625 num_hrrq
= pci_enable_msix_range(pdev
, entries
, 1, num_hrrq
);
4627 goto pmcraid_isr_legacy
;
4629 for (i
= 0; i
< num_hrrq
; i
++) {
4630 pinstance
->hrrq_vector
[i
].hrrq_id
= i
;
4631 pinstance
->hrrq_vector
[i
].drv_inst
= pinstance
;
4632 pinstance
->hrrq_vector
[i
].vector
= entries
[i
].vector
;
4633 rc
= request_irq(pinstance
->hrrq_vector
[i
].vector
,
4634 pmcraid_isr_msix
, 0,
4635 PMCRAID_DRIVER_NAME
,
4636 &(pinstance
->hrrq_vector
[i
]));
4640 for (j
= 0; j
< i
; j
++)
4641 free_irq(entries
[j
].vector
,
4642 &(pinstance
->hrrq_vector
[j
]));
4643 pci_disable_msix(pdev
);
4644 goto pmcraid_isr_legacy
;
4648 pinstance
->num_hrrq
= num_hrrq
;
4649 pinstance
->interrupt_mode
= 1;
4650 iowrite32(DOORBELL_INTR_MODE_MSIX
,
4651 pinstance
->int_regs
.host_ioa_interrupt_reg
);
4652 ioread32(pinstance
->int_regs
.host_ioa_interrupt_reg
);
4653 goto pmcraid_isr_out
;
4657 /* If MSI-X registration failed fallback to legacy mode, where
4658 * only one hrrq entry will be used
4660 pinstance
->hrrq_vector
[0].hrrq_id
= 0;
4661 pinstance
->hrrq_vector
[0].drv_inst
= pinstance
;
4662 pinstance
->hrrq_vector
[0].vector
= pdev
->irq
;
4663 pinstance
->num_hrrq
= 1;
4665 rc
= request_irq(pdev
->irq
, pmcraid_isr
, IRQF_SHARED
,
4666 PMCRAID_DRIVER_NAME
, &pinstance
->hrrq_vector
[0]);
4672 * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
4673 * @pinstance: per adapter instance structure pointer
4674 * @max_index: number of buffer blocks to release
4680 pmcraid_release_cmd_blocks(struct pmcraid_instance
*pinstance
, int max_index
)
4683 for (i
= 0; i
< max_index
; i
++) {
4684 kmem_cache_free(pinstance
->cmd_cachep
, pinstance
->cmd_list
[i
]);
4685 pinstance
->cmd_list
[i
] = NULL
;
4687 kmem_cache_destroy(pinstance
->cmd_cachep
);
4688 pinstance
->cmd_cachep
= NULL
;
4692 * pmcraid_release_control_blocks - releases buffers alloced for control blocks
4693 * @pinstance: pointer to per adapter instance structure
4694 * @max_index: number of buffers (from 0 onwards) to release
4696 * This function assumes that the command blocks for which control blocks are
4697 * linked are not released.
4703 pmcraid_release_control_blocks(
4704 struct pmcraid_instance
*pinstance
,
4710 if (pinstance
->control_pool
== NULL
)
4713 for (i
= 0; i
< max_index
; i
++) {
4714 pci_pool_free(pinstance
->control_pool
,
4715 pinstance
->cmd_list
[i
]->ioa_cb
,
4716 pinstance
->cmd_list
[i
]->ioa_cb_bus_addr
);
4717 pinstance
->cmd_list
[i
]->ioa_cb
= NULL
;
4718 pinstance
->cmd_list
[i
]->ioa_cb_bus_addr
= 0;
4720 pci_pool_destroy(pinstance
->control_pool
);
4721 pinstance
->control_pool
= NULL
;
4725 * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
4726 * @pinstance - pointer to per adapter instance structure
4728 * Allocates memory for command blocks using kernel slab allocator.
4731 * 0 in case of success; -ENOMEM in case of failure
4733 static int pmcraid_allocate_cmd_blocks(struct pmcraid_instance
*pinstance
)
4737 sprintf(pinstance
->cmd_pool_name
, "pmcraid_cmd_pool_%d",
4738 pinstance
->host
->unique_id
);
4741 pinstance
->cmd_cachep
= kmem_cache_create(
4742 pinstance
->cmd_pool_name
,
4743 sizeof(struct pmcraid_cmd
), 0,
4744 SLAB_HWCACHE_ALIGN
, NULL
);
4745 if (!pinstance
->cmd_cachep
)
4748 for (i
= 0; i
< PMCRAID_MAX_CMD
; i
++) {
4749 pinstance
->cmd_list
[i
] =
4750 kmem_cache_alloc(pinstance
->cmd_cachep
, GFP_KERNEL
);
4751 if (!pinstance
->cmd_list
[i
]) {
4752 pmcraid_release_cmd_blocks(pinstance
, i
);
4760 * pmcraid_allocate_control_blocks - allocates memory control blocks
4761 * @pinstance : pointer to per adapter instance structure
4763 * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
4764 * and IOASAs. This is called after command blocks are already allocated.
4767 * 0 in case it can allocate all control blocks, otherwise -ENOMEM
4769 static int pmcraid_allocate_control_blocks(struct pmcraid_instance
*pinstance
)
4773 sprintf(pinstance
->ctl_pool_name
, "pmcraid_control_pool_%d",
4774 pinstance
->host
->unique_id
);
4776 pinstance
->control_pool
=
4777 pci_pool_create(pinstance
->ctl_pool_name
,
4779 sizeof(struct pmcraid_control_block
),
4780 PMCRAID_IOARCB_ALIGNMENT
, 0);
4782 if (!pinstance
->control_pool
)
4785 for (i
= 0; i
< PMCRAID_MAX_CMD
; i
++) {
4786 pinstance
->cmd_list
[i
]->ioa_cb
=
4788 pinstance
->control_pool
,
4790 &(pinstance
->cmd_list
[i
]->ioa_cb_bus_addr
));
4792 if (!pinstance
->cmd_list
[i
]->ioa_cb
) {
4793 pmcraid_release_control_blocks(pinstance
, i
);
4796 memset(pinstance
->cmd_list
[i
]->ioa_cb
, 0,
4797 sizeof(struct pmcraid_control_block
));
4803 * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
4804 * @pinstance: pointer to per adapter instance structure
4805 * @maxindex: size of hrrq buffer pointer array
4811 pmcraid_release_host_rrqs(struct pmcraid_instance
*pinstance
, int maxindex
)
4814 for (i
= 0; i
< maxindex
; i
++) {
4816 pci_free_consistent(pinstance
->pdev
,
4817 HRRQ_ENTRY_SIZE
* PMCRAID_MAX_CMD
,
4818 pinstance
->hrrq_start
[i
],
4819 pinstance
->hrrq_start_bus_addr
[i
]);
4821 /* reset pointers and toggle bit to zeros */
4822 pinstance
->hrrq_start
[i
] = NULL
;
4823 pinstance
->hrrq_start_bus_addr
[i
] = 0;
4824 pinstance
->host_toggle_bit
[i
] = 0;
4829 * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
4830 * @pinstance: pointer to per adapter instance structure
4833 * 0 hrrq buffers are allocated, -ENOMEM otherwise.
4835 static int pmcraid_allocate_host_rrqs(struct pmcraid_instance
*pinstance
)
4839 buffer_size
= HRRQ_ENTRY_SIZE
* PMCRAID_MAX_CMD
;
4841 for (i
= 0; i
< pinstance
->num_hrrq
; i
++) {
4842 pinstance
->hrrq_start
[i
] =
4843 pci_alloc_consistent(
4846 &(pinstance
->hrrq_start_bus_addr
[i
]));
4848 if (pinstance
->hrrq_start
[i
] == 0) {
4849 pmcraid_err("pci_alloc failed for hrrq vector : %d\n",
4851 pmcraid_release_host_rrqs(pinstance
, i
);
4855 memset(pinstance
->hrrq_start
[i
], 0, buffer_size
);
4856 pinstance
->hrrq_curr
[i
] = pinstance
->hrrq_start
[i
];
4857 pinstance
->hrrq_end
[i
] =
4858 pinstance
->hrrq_start
[i
] + PMCRAID_MAX_CMD
- 1;
4859 pinstance
->host_toggle_bit
[i
] = 1;
4860 spin_lock_init(&pinstance
->hrrq_lock
[i
]);
4866 * pmcraid_release_hcams - release HCAM buffers
4868 * @pinstance: pointer to per adapter instance structure
4873 static void pmcraid_release_hcams(struct pmcraid_instance
*pinstance
)
4875 if (pinstance
->ccn
.msg
!= NULL
) {
4876 pci_free_consistent(pinstance
->pdev
,
4877 PMCRAID_AEN_HDR_SIZE
+
4878 sizeof(struct pmcraid_hcam_ccn_ext
),
4880 pinstance
->ccn
.baddr
);
4882 pinstance
->ccn
.msg
= NULL
;
4883 pinstance
->ccn
.hcam
= NULL
;
4884 pinstance
->ccn
.baddr
= 0;
4887 if (pinstance
->ldn
.msg
!= NULL
) {
4888 pci_free_consistent(pinstance
->pdev
,
4889 PMCRAID_AEN_HDR_SIZE
+
4890 sizeof(struct pmcraid_hcam_ldn
),
4892 pinstance
->ldn
.baddr
);
4894 pinstance
->ldn
.msg
= NULL
;
4895 pinstance
->ldn
.hcam
= NULL
;
4896 pinstance
->ldn
.baddr
= 0;
4901 * pmcraid_allocate_hcams - allocates HCAM buffers
4902 * @pinstance : pointer to per adapter instance structure
4905 * 0 in case of successful allocation, non-zero otherwise
4907 static int pmcraid_allocate_hcams(struct pmcraid_instance
*pinstance
)
4909 pinstance
->ccn
.msg
= pci_alloc_consistent(
4911 PMCRAID_AEN_HDR_SIZE
+
4912 sizeof(struct pmcraid_hcam_ccn_ext
),
4913 &(pinstance
->ccn
.baddr
));
4915 pinstance
->ldn
.msg
= pci_alloc_consistent(
4917 PMCRAID_AEN_HDR_SIZE
+
4918 sizeof(struct pmcraid_hcam_ldn
),
4919 &(pinstance
->ldn
.baddr
));
4921 if (pinstance
->ldn
.msg
== NULL
|| pinstance
->ccn
.msg
== NULL
) {
4922 pmcraid_release_hcams(pinstance
);
4924 pinstance
->ccn
.hcam
=
4925 (void *)pinstance
->ccn
.msg
+ PMCRAID_AEN_HDR_SIZE
;
4926 pinstance
->ldn
.hcam
=
4927 (void *)pinstance
->ldn
.msg
+ PMCRAID_AEN_HDR_SIZE
;
4929 atomic_set(&pinstance
->ccn
.ignore
, 0);
4930 atomic_set(&pinstance
->ldn
.ignore
, 0);
4933 return (pinstance
->ldn
.msg
== NULL
) ? -ENOMEM
: 0;
4937 * pmcraid_release_config_buffers - release config.table buffers
4938 * @pinstance: pointer to per adapter instance structure
4943 static void pmcraid_release_config_buffers(struct pmcraid_instance
*pinstance
)
4945 if (pinstance
->cfg_table
!= NULL
&&
4946 pinstance
->cfg_table_bus_addr
!= 0) {
4947 pci_free_consistent(pinstance
->pdev
,
4948 sizeof(struct pmcraid_config_table
),
4949 pinstance
->cfg_table
,
4950 pinstance
->cfg_table_bus_addr
);
4951 pinstance
->cfg_table
= NULL
;
4952 pinstance
->cfg_table_bus_addr
= 0;
4955 if (pinstance
->res_entries
!= NULL
) {
4958 for (i
= 0; i
< PMCRAID_MAX_RESOURCES
; i
++)
4959 list_del(&pinstance
->res_entries
[i
].queue
);
4960 kfree(pinstance
->res_entries
);
4961 pinstance
->res_entries
= NULL
;
4964 pmcraid_release_hcams(pinstance
);
4968 * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
4969 * @pinstance : pointer to per adapter instance structure
4972 * 0 for successful allocation, -ENOMEM for any failure
4974 static int pmcraid_allocate_config_buffers(struct pmcraid_instance
*pinstance
)
4978 pinstance
->res_entries
=
4979 kzalloc(sizeof(struct pmcraid_resource_entry
) *
4980 PMCRAID_MAX_RESOURCES
, GFP_KERNEL
);
4982 if (NULL
== pinstance
->res_entries
) {
4983 pmcraid_err("failed to allocate memory for resource table\n");
4987 for (i
= 0; i
< PMCRAID_MAX_RESOURCES
; i
++)
4988 list_add_tail(&pinstance
->res_entries
[i
].queue
,
4989 &pinstance
->free_res_q
);
4991 pinstance
->cfg_table
=
4992 pci_alloc_consistent(pinstance
->pdev
,
4993 sizeof(struct pmcraid_config_table
),
4994 &pinstance
->cfg_table_bus_addr
);
4996 if (NULL
== pinstance
->cfg_table
) {
4997 pmcraid_err("couldn't alloc DMA memory for config table\n");
4998 pmcraid_release_config_buffers(pinstance
);
5002 if (pmcraid_allocate_hcams(pinstance
)) {
5003 pmcraid_err("could not alloc DMA memory for HCAMS\n");
5004 pmcraid_release_config_buffers(pinstance
);
5012 * pmcraid_init_tasklets - registers tasklets for response handling
5014 * @pinstance: pointer adapter instance structure
5019 static void pmcraid_init_tasklets(struct pmcraid_instance
*pinstance
)
5022 for (i
= 0; i
< pinstance
->num_hrrq
; i
++)
5023 tasklet_init(&pinstance
->isr_tasklet
[i
],
5024 pmcraid_tasklet_function
,
5025 (unsigned long)&pinstance
->hrrq_vector
[i
]);
5029 * pmcraid_kill_tasklets - destroys tasklets registered for response handling
5031 * @pinstance: pointer to adapter instance structure
5036 static void pmcraid_kill_tasklets(struct pmcraid_instance
*pinstance
)
5039 for (i
= 0; i
< pinstance
->num_hrrq
; i
++)
5040 tasklet_kill(&pinstance
->isr_tasklet
[i
]);
5044 * pmcraid_release_buffers - release per-adapter buffers allocated
5046 * @pinstance: pointer to adapter soft state
5051 static void pmcraid_release_buffers(struct pmcraid_instance
*pinstance
)
5053 pmcraid_release_config_buffers(pinstance
);
5054 pmcraid_release_control_blocks(pinstance
, PMCRAID_MAX_CMD
);
5055 pmcraid_release_cmd_blocks(pinstance
, PMCRAID_MAX_CMD
);
5056 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
5058 if (pinstance
->inq_data
!= NULL
) {
5059 pci_free_consistent(pinstance
->pdev
,
5060 sizeof(struct pmcraid_inquiry_data
),
5061 pinstance
->inq_data
,
5062 pinstance
->inq_data_baddr
);
5064 pinstance
->inq_data
= NULL
;
5065 pinstance
->inq_data_baddr
= 0;
5068 if (pinstance
->timestamp_data
!= NULL
) {
5069 pci_free_consistent(pinstance
->pdev
,
5070 sizeof(struct pmcraid_timestamp_data
),
5071 pinstance
->timestamp_data
,
5072 pinstance
->timestamp_data_baddr
);
5074 pinstance
->timestamp_data
= NULL
;
5075 pinstance
->timestamp_data_baddr
= 0;
5080 * pmcraid_init_buffers - allocates memory and initializes various structures
5081 * @pinstance: pointer to per adapter instance structure
5083 * This routine pre-allocates memory based on the type of block as below:
5084 * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
5085 * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
5086 * config-table entries : DMAable memory using pci_alloc_consistent
5087 * HostRRQs : DMAable memory, using pci_alloc_consistent
5090 * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
5092 static int pmcraid_init_buffers(struct pmcraid_instance
*pinstance
)
5096 if (pmcraid_allocate_host_rrqs(pinstance
)) {
5097 pmcraid_err("couldn't allocate memory for %d host rrqs\n",
5098 pinstance
->num_hrrq
);
5102 if (pmcraid_allocate_config_buffers(pinstance
)) {
5103 pmcraid_err("couldn't allocate memory for config buffers\n");
5104 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
5108 if (pmcraid_allocate_cmd_blocks(pinstance
)) {
5109 pmcraid_err("couldn't allocate memory for cmd blocks\n");
5110 pmcraid_release_config_buffers(pinstance
);
5111 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
5115 if (pmcraid_allocate_control_blocks(pinstance
)) {
5116 pmcraid_err("couldn't allocate memory control blocks\n");
5117 pmcraid_release_config_buffers(pinstance
);
5118 pmcraid_release_cmd_blocks(pinstance
, PMCRAID_MAX_CMD
);
5119 pmcraid_release_host_rrqs(pinstance
, pinstance
->num_hrrq
);
5123 /* allocate DMAable memory for page D0 INQUIRY buffer */
5124 pinstance
->inq_data
= pci_alloc_consistent(
5126 sizeof(struct pmcraid_inquiry_data
),
5127 &pinstance
->inq_data_baddr
);
5129 if (pinstance
->inq_data
== NULL
) {
5130 pmcraid_err("couldn't allocate DMA memory for INQUIRY\n");
5131 pmcraid_release_buffers(pinstance
);
5135 /* allocate DMAable memory for set timestamp data buffer */
5136 pinstance
->timestamp_data
= pci_alloc_consistent(
5138 sizeof(struct pmcraid_timestamp_data
),
5139 &pinstance
->timestamp_data_baddr
);
5141 if (pinstance
->timestamp_data
== NULL
) {
5142 pmcraid_err("couldn't allocate DMA memory for \
5143 set time_stamp \n");
5144 pmcraid_release_buffers(pinstance
);
5149 /* Initialize all the command blocks and add them to free pool. No
5150 * need to lock (free_pool_lock) as this is done in initialization
5153 for (i
= 0; i
< PMCRAID_MAX_CMD
; i
++) {
5154 struct pmcraid_cmd
*cmdp
= pinstance
->cmd_list
[i
];
5155 pmcraid_init_cmdblk(cmdp
, i
);
5156 cmdp
->drv_inst
= pinstance
;
5157 list_add_tail(&cmdp
->free_list
, &pinstance
->free_cmd_pool
);
5164 * pmcraid_reinit_buffers - resets various buffer pointers
5165 * @pinstance: pointer to adapter instance
5169 static void pmcraid_reinit_buffers(struct pmcraid_instance
*pinstance
)
5172 int buffer_size
= HRRQ_ENTRY_SIZE
* PMCRAID_MAX_CMD
;
5174 for (i
= 0; i
< pinstance
->num_hrrq
; i
++) {
5175 memset(pinstance
->hrrq_start
[i
], 0, buffer_size
);
5176 pinstance
->hrrq_curr
[i
] = pinstance
->hrrq_start
[i
];
5177 pinstance
->hrrq_end
[i
] =
5178 pinstance
->hrrq_start
[i
] + PMCRAID_MAX_CMD
- 1;
5179 pinstance
->host_toggle_bit
[i
] = 1;
5184 * pmcraid_init_instance - initialize per instance data structure
5185 * @pdev: pointer to pci device structure
5186 * @host: pointer to Scsi_Host structure
5187 * @mapped_pci_addr: memory mapped IOA configuration registers
5190 * 0 on success, non-zero in case of any failure
5192 static int pmcraid_init_instance(struct pci_dev
*pdev
, struct Scsi_Host
*host
,
5193 void __iomem
*mapped_pci_addr
)
5195 struct pmcraid_instance
*pinstance
=
5196 (struct pmcraid_instance
*)host
->hostdata
;
5198 pinstance
->host
= host
;
5199 pinstance
->pdev
= pdev
;
5201 /* Initialize register addresses */
5202 pinstance
->mapped_dma_addr
= mapped_pci_addr
;
5204 /* Initialize chip-specific details */
5206 struct pmcraid_chip_details
*chip_cfg
= pinstance
->chip_cfg
;
5207 struct pmcraid_interrupts
*pint_regs
= &pinstance
->int_regs
;
5209 pinstance
->ioarrin
= mapped_pci_addr
+ chip_cfg
->ioarrin
;
5211 pint_regs
->ioa_host_interrupt_reg
=
5212 mapped_pci_addr
+ chip_cfg
->ioa_host_intr
;
5213 pint_regs
->ioa_host_interrupt_clr_reg
=
5214 mapped_pci_addr
+ chip_cfg
->ioa_host_intr_clr
;
5215 pint_regs
->ioa_host_msix_interrupt_reg
=
5216 mapped_pci_addr
+ chip_cfg
->ioa_host_msix_intr
;
5217 pint_regs
->host_ioa_interrupt_reg
=
5218 mapped_pci_addr
+ chip_cfg
->host_ioa_intr
;
5219 pint_regs
->host_ioa_interrupt_clr_reg
=
5220 mapped_pci_addr
+ chip_cfg
->host_ioa_intr_clr
;
5222 /* Current version of firmware exposes interrupt mask set
5223 * and mask clr registers through memory mapped bar0.
5225 pinstance
->mailbox
= mapped_pci_addr
+ chip_cfg
->mailbox
;
5226 pinstance
->ioa_status
= mapped_pci_addr
+ chip_cfg
->ioastatus
;
5227 pint_regs
->ioa_host_interrupt_mask_reg
=
5228 mapped_pci_addr
+ chip_cfg
->ioa_host_mask
;
5229 pint_regs
->ioa_host_interrupt_mask_clr_reg
=
5230 mapped_pci_addr
+ chip_cfg
->ioa_host_mask_clr
;
5231 pint_regs
->global_interrupt_mask_reg
=
5232 mapped_pci_addr
+ chip_cfg
->global_intr_mask
;
5235 pinstance
->ioa_reset_attempts
= 0;
5236 init_waitqueue_head(&pinstance
->reset_wait_q
);
5238 atomic_set(&pinstance
->outstanding_cmds
, 0);
5239 atomic_set(&pinstance
->last_message_id
, 0);
5240 atomic_set(&pinstance
->expose_resources
, 0);
5242 INIT_LIST_HEAD(&pinstance
->free_res_q
);
5243 INIT_LIST_HEAD(&pinstance
->used_res_q
);
5244 INIT_LIST_HEAD(&pinstance
->free_cmd_pool
);
5245 INIT_LIST_HEAD(&pinstance
->pending_cmd_pool
);
5247 spin_lock_init(&pinstance
->free_pool_lock
);
5248 spin_lock_init(&pinstance
->pending_pool_lock
);
5249 spin_lock_init(&pinstance
->resource_lock
);
5250 mutex_init(&pinstance
->aen_queue_lock
);
5252 /* Work-queue (Shared) for deferred processing error handling */
5253 INIT_WORK(&pinstance
->worker_q
, pmcraid_worker_function
);
5255 /* Initialize the default log_level */
5256 pinstance
->current_log_level
= pmcraid_log_level
;
5258 /* Setup variables required for reset engine */
5259 pinstance
->ioa_state
= IOA_STATE_UNKNOWN
;
5260 pinstance
->reset_cmd
= NULL
;
5265 * pmcraid_shutdown - shutdown adapter controller.
5266 * @pdev: pci device struct
5268 * Issues an adapter shutdown to the card waits for its completion
5273 static void pmcraid_shutdown(struct pci_dev
*pdev
)
5275 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5276 pmcraid_reset_bringdown(pinstance
);
5281 * pmcraid_get_minor - returns unused minor number from minor number bitmap
5283 static unsigned short pmcraid_get_minor(void)
5287 minor
= find_first_zero_bit(pmcraid_minor
, sizeof(pmcraid_minor
));
5288 __set_bit(minor
, pmcraid_minor
);
5293 * pmcraid_release_minor - releases given minor back to minor number bitmap
5295 static void pmcraid_release_minor(unsigned short minor
)
5297 __clear_bit(minor
, pmcraid_minor
);
5301 * pmcraid_setup_chrdev - allocates a minor number and registers a char device
5303 * @pinstance: pointer to adapter instance for which to register device
5306 * 0 in case of success, otherwise non-zero
5308 static int pmcraid_setup_chrdev(struct pmcraid_instance
*pinstance
)
5313 minor
= pmcraid_get_minor();
5314 cdev_init(&pinstance
->cdev
, &pmcraid_fops
);
5315 pinstance
->cdev
.owner
= THIS_MODULE
;
5317 error
= cdev_add(&pinstance
->cdev
, MKDEV(pmcraid_major
, minor
), 1);
5320 pmcraid_release_minor(minor
);
5322 device_create(pmcraid_class
, NULL
, MKDEV(pmcraid_major
, minor
),
5323 NULL
, "%s%u", PMCRAID_DEVFILE
, minor
);
5328 * pmcraid_release_chrdev - unregisters per-adapter management interface
5330 * @pinstance: pointer to adapter instance structure
5335 static void pmcraid_release_chrdev(struct pmcraid_instance
*pinstance
)
5337 pmcraid_release_minor(MINOR(pinstance
->cdev
.dev
));
5338 device_destroy(pmcraid_class
,
5339 MKDEV(pmcraid_major
, MINOR(pinstance
->cdev
.dev
)));
5340 cdev_del(&pinstance
->cdev
);
5344 * pmcraid_remove - IOA hot plug remove entry point
5345 * @pdev: pci device struct
5350 static void pmcraid_remove(struct pci_dev
*pdev
)
5352 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5354 /* remove the management interface (/dev file) for this device */
5355 pmcraid_release_chrdev(pinstance
);
5357 /* remove host template from scsi midlayer */
5358 scsi_remove_host(pinstance
->host
);
5360 /* block requests from mid-layer */
5361 scsi_block_requests(pinstance
->host
);
5363 /* initiate shutdown adapter */
5364 pmcraid_shutdown(pdev
);
5366 pmcraid_disable_interrupts(pinstance
, ~0);
5367 flush_work(&pinstance
->worker_q
);
5369 pmcraid_kill_tasklets(pinstance
);
5370 pmcraid_unregister_interrupt_handler(pinstance
);
5371 pmcraid_release_buffers(pinstance
);
5372 iounmap(pinstance
->mapped_dma_addr
);
5373 pci_release_regions(pdev
);
5374 scsi_host_put(pinstance
->host
);
5375 pci_disable_device(pdev
);
5382 * pmcraid_suspend - driver suspend entry point for power management
5383 * @pdev: PCI device structure
5384 * @state: PCI power state to suspend routine
5386 * Return Value - 0 always
5388 static int pmcraid_suspend(struct pci_dev
*pdev
, pm_message_t state
)
5390 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5392 pmcraid_shutdown(pdev
);
5393 pmcraid_disable_interrupts(pinstance
, ~0);
5394 pmcraid_kill_tasklets(pinstance
);
5395 pci_set_drvdata(pinstance
->pdev
, pinstance
);
5396 pmcraid_unregister_interrupt_handler(pinstance
);
5397 pci_save_state(pdev
);
5398 pci_disable_device(pdev
);
5399 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
5405 * pmcraid_resume - driver resume entry point PCI power management
5406 * @pdev: PCI device structure
5408 * Return Value - 0 in case of success. Error code in case of any failure
5410 static int pmcraid_resume(struct pci_dev
*pdev
)
5412 struct pmcraid_instance
*pinstance
= pci_get_drvdata(pdev
);
5413 struct Scsi_Host
*host
= pinstance
->host
;
5416 pci_set_power_state(pdev
, PCI_D0
);
5417 pci_enable_wake(pdev
, PCI_D0
, 0);
5418 pci_restore_state(pdev
);
5420 rc
= pci_enable_device(pdev
);
5423 dev_err(&pdev
->dev
, "resume: Enable device failed\n");
5427 pci_set_master(pdev
);
5429 if ((sizeof(dma_addr_t
) == 4) ||
5430 pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)))
5431 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
5434 rc
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
5437 dev_err(&pdev
->dev
, "resume: Failed to set PCI DMA mask\n");
5438 goto disable_device
;
5441 pmcraid_disable_interrupts(pinstance
, ~0);
5442 atomic_set(&pinstance
->outstanding_cmds
, 0);
5443 rc
= pmcraid_register_interrupt_handler(pinstance
);
5447 "resume: couldn't register interrupt handlers\n");
5452 pmcraid_init_tasklets(pinstance
);
5453 pmcraid_enable_interrupts(pinstance
, PMCRAID_PCI_INTERRUPTS
);
5455 /* Start with hard reset sequence which brings up IOA to operational
5456 * state as well as completes the reset sequence.
5458 pinstance
->ioa_hard_reset
= 1;
5460 /* Start IOA firmware initialization and bring card to Operational
5463 if (pmcraid_reset_bringup(pinstance
)) {
5464 dev_err(&pdev
->dev
, "couldn't initialize IOA\n");
5466 goto release_tasklets
;
5472 pmcraid_disable_interrupts(pinstance
, ~0);
5473 pmcraid_kill_tasklets(pinstance
);
5474 pmcraid_unregister_interrupt_handler(pinstance
);
5477 scsi_host_put(host
);
5480 pci_disable_device(pdev
);
5487 #define pmcraid_suspend NULL
5488 #define pmcraid_resume NULL
5490 #endif /* CONFIG_PM */
5493 * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
5494 * completion of the ioa reset
5495 * @cmd: pointer to reset command block
5497 static void pmcraid_complete_ioa_reset(struct pmcraid_cmd
*cmd
)
5499 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5500 unsigned long flags
;
5502 spin_lock_irqsave(pinstance
->host
->host_lock
, flags
);
5503 pmcraid_ioa_reset(cmd
);
5504 spin_unlock_irqrestore(pinstance
->host
->host_lock
, flags
);
5505 scsi_unblock_requests(pinstance
->host
);
5506 schedule_work(&pinstance
->worker_q
);
5510 * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
5512 * @cmd: pointer to pmcraid_cmd structure
5515 * 0 for success or non-zero for failure cases
5517 static void pmcraid_set_supported_devs(struct pmcraid_cmd
*cmd
)
5519 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
5520 void (*cmd_done
) (struct pmcraid_cmd
*) = pmcraid_complete_ioa_reset
;
5522 pmcraid_reinit_cmdblk(cmd
);
5524 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
5525 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
5526 ioarcb
->cdb
[0] = PMCRAID_SET_SUPPORTED_DEVICES
;
5527 ioarcb
->cdb
[1] = ALL_DEVICES_SUPPORTED
;
5529 /* If this was called as part of resource table reinitialization due to
5530 * lost CCN, it is enough to return the command block back to free pool
5531 * as part of set_supported_devs completion function.
5533 if (cmd
->drv_inst
->reinit_cfg_table
) {
5534 cmd
->drv_inst
->reinit_cfg_table
= 0;
5536 cmd_done
= pmcraid_reinit_cfgtable_done
;
5539 /* we will be done with the reset sequence after set supported devices,
5540 * setup the done function to return the command block back to free
5543 pmcraid_send_cmd(cmd
,
5545 PMCRAID_SET_SUP_DEV_TIMEOUT
,
5546 pmcraid_timeout_handler
);
5551 * pmcraid_set_timestamp - set the timestamp to IOAFP
5553 * @cmd: pointer to pmcraid_cmd structure
5556 * 0 for success or non-zero for failure cases
5558 static void pmcraid_set_timestamp(struct pmcraid_cmd
*cmd
)
5560 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5561 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
5562 __be32 time_stamp_len
= cpu_to_be32(PMCRAID_TIMESTAMP_LEN
);
5563 struct pmcraid_ioadl_desc
*ioadl
= ioarcb
->add_data
.u
.ioadl
;
5568 do_gettimeofday(&tv
);
5569 timestamp
= tv
.tv_sec
* 1000;
5571 pinstance
->timestamp_data
->timestamp
[0] = (__u8
)(timestamp
);
5572 pinstance
->timestamp_data
->timestamp
[1] = (__u8
)((timestamp
) >> 8);
5573 pinstance
->timestamp_data
->timestamp
[2] = (__u8
)((timestamp
) >> 16);
5574 pinstance
->timestamp_data
->timestamp
[3] = (__u8
)((timestamp
) >> 24);
5575 pinstance
->timestamp_data
->timestamp
[4] = (__u8
)((timestamp
) >> 32);
5576 pinstance
->timestamp_data
->timestamp
[5] = (__u8
)((timestamp
) >> 40);
5578 pmcraid_reinit_cmdblk(cmd
);
5579 ioarcb
->request_type
= REQ_TYPE_SCSI
;
5580 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
5581 ioarcb
->cdb
[0] = PMCRAID_SCSI_SET_TIMESTAMP
;
5582 ioarcb
->cdb
[1] = PMCRAID_SCSI_SERVICE_ACTION
;
5583 memcpy(&(ioarcb
->cdb
[6]), &time_stamp_len
, sizeof(time_stamp_len
));
5585 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
5586 offsetof(struct pmcraid_ioarcb
,
5587 add_data
.u
.ioadl
[0]));
5588 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
5589 ioarcb
->ioarcb_bus_addr
&= ~(0x1FULL
);
5591 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
5592 ioarcb
->request_flags0
|= TRANSFER_DIR_WRITE
;
5593 ioarcb
->data_transfer_length
=
5594 cpu_to_le32(sizeof(struct pmcraid_timestamp_data
));
5595 ioadl
= &(ioarcb
->add_data
.u
.ioadl
[0]);
5596 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
5597 ioadl
->address
= cpu_to_le64(pinstance
->timestamp_data_baddr
);
5598 ioadl
->data_len
= cpu_to_le32(sizeof(struct pmcraid_timestamp_data
));
5600 if (!pinstance
->timestamp_error
) {
5601 pinstance
->timestamp_error
= 0;
5602 pmcraid_send_cmd(cmd
, pmcraid_set_supported_devs
,
5603 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
5605 pmcraid_send_cmd(cmd
, pmcraid_return_cmd
,
5606 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
5613 * pmcraid_init_res_table - Initialize the resource table
5614 * @cmd: pointer to pmcraid command struct
5616 * This function looks through the existing resource table, comparing
5617 * it with the config table. This function will take care of old/new
5618 * devices and schedule adding/removing them from the mid-layer
5624 static void pmcraid_init_res_table(struct pmcraid_cmd
*cmd
)
5626 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5627 struct pmcraid_resource_entry
*res
, *temp
;
5628 struct pmcraid_config_table_entry
*cfgte
;
5629 unsigned long lock_flags
;
5634 if (pinstance
->cfg_table
->flags
& MICROCODE_UPDATE_REQUIRED
)
5635 pmcraid_err("IOA requires microcode download\n");
5637 fw_version
= be16_to_cpu(pinstance
->inq_data
->fw_version
);
5639 /* resource list is protected by pinstance->resource_lock.
5640 * init_res_table can be called from probe (user-thread) or runtime
5641 * reset (timer/tasklet)
5643 spin_lock_irqsave(&pinstance
->resource_lock
, lock_flags
);
5645 list_for_each_entry_safe(res
, temp
, &pinstance
->used_res_q
, queue
)
5646 list_move_tail(&res
->queue
, &old_res
);
5648 for (i
= 0; i
< pinstance
->cfg_table
->num_entries
; i
++) {
5649 if (be16_to_cpu(pinstance
->inq_data
->fw_version
) <=
5650 PMCRAID_FW_VERSION_1
)
5651 cfgte
= &pinstance
->cfg_table
->entries
[i
];
5653 cfgte
= (struct pmcraid_config_table_entry
*)
5654 &pinstance
->cfg_table
->entries_ext
[i
];
5656 if (!pmcraid_expose_resource(fw_version
, cfgte
))
5661 /* If this entry was already detected and initialized */
5662 list_for_each_entry_safe(res
, temp
, &old_res
, queue
) {
5664 rc
= memcmp(&res
->cfg_entry
.resource_address
,
5665 &cfgte
->resource_address
,
5666 sizeof(cfgte
->resource_address
));
5668 list_move_tail(&res
->queue
,
5669 &pinstance
->used_res_q
);
5675 /* If this is new entry, initialize it and add it the queue */
5678 if (list_empty(&pinstance
->free_res_q
)) {
5679 pmcraid_err("Too many devices attached\n");
5684 res
= list_entry(pinstance
->free_res_q
.next
,
5685 struct pmcraid_resource_entry
, queue
);
5687 res
->scsi_dev
= NULL
;
5688 res
->change_detected
= RES_CHANGE_ADD
;
5689 res
->reset_progress
= 0;
5690 list_move_tail(&res
->queue
, &pinstance
->used_res_q
);
5693 /* copy new configuration table entry details into driver
5694 * maintained resource entry
5697 memcpy(&res
->cfg_entry
, cfgte
,
5698 pinstance
->config_table_entry_size
);
5699 pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
5700 res
->cfg_entry
.resource_type
,
5701 (fw_version
<= PMCRAID_FW_VERSION_1
?
5702 res
->cfg_entry
.unique_flags1
:
5703 res
->cfg_entry
.array_id
& 0xFF),
5704 le32_to_cpu(res
->cfg_entry
.resource_address
));
5708 /* Detect any deleted entries, mark them for deletion from mid-layer */
5709 list_for_each_entry_safe(res
, temp
, &old_res
, queue
) {
5711 if (res
->scsi_dev
) {
5712 res
->change_detected
= RES_CHANGE_DEL
;
5713 res
->cfg_entry
.resource_handle
=
5714 PMCRAID_INVALID_RES_HANDLE
;
5715 list_move_tail(&res
->queue
, &pinstance
->used_res_q
);
5717 list_move_tail(&res
->queue
, &pinstance
->free_res_q
);
5721 /* release the resource list lock */
5722 spin_unlock_irqrestore(&pinstance
->resource_lock
, lock_flags
);
5723 pmcraid_set_timestamp(cmd
);
5727 * pmcraid_querycfg - Send a Query IOA Config to the adapter.
5728 * @cmd: pointer pmcraid_cmd struct
5730 * This function sends a Query IOA Configuration command to the adapter to
5731 * retrieve the IOA configuration table.
5736 static void pmcraid_querycfg(struct pmcraid_cmd
*cmd
)
5738 struct pmcraid_ioarcb
*ioarcb
= &cmd
->ioa_cb
->ioarcb
;
5739 struct pmcraid_ioadl_desc
*ioadl
= ioarcb
->add_data
.u
.ioadl
;
5740 struct pmcraid_instance
*pinstance
= cmd
->drv_inst
;
5741 int cfg_table_size
= cpu_to_be32(sizeof(struct pmcraid_config_table
));
5743 if (be16_to_cpu(pinstance
->inq_data
->fw_version
) <=
5744 PMCRAID_FW_VERSION_1
)
5745 pinstance
->config_table_entry_size
=
5746 sizeof(struct pmcraid_config_table_entry
);
5748 pinstance
->config_table_entry_size
=
5749 sizeof(struct pmcraid_config_table_entry_ext
);
5751 ioarcb
->request_type
= REQ_TYPE_IOACMD
;
5752 ioarcb
->resource_handle
= cpu_to_le32(PMCRAID_IOA_RES_HANDLE
);
5754 ioarcb
->cdb
[0] = PMCRAID_QUERY_IOA_CONFIG
;
5756 /* firmware requires 4-byte length field, specified in B.E format */
5757 memcpy(&(ioarcb
->cdb
[10]), &cfg_table_size
, sizeof(cfg_table_size
));
5759 /* Since entire config table can be described by single IOADL, it can
5760 * be part of IOARCB itself
5762 ioarcb
->ioadl_bus_addr
= cpu_to_le64((cmd
->ioa_cb_bus_addr
) +
5763 offsetof(struct pmcraid_ioarcb
,
5764 add_data
.u
.ioadl
[0]));
5765 ioarcb
->ioadl_length
= cpu_to_le32(sizeof(struct pmcraid_ioadl_desc
));
5766 ioarcb
->ioarcb_bus_addr
&= ~(0x1FULL
);
5768 ioarcb
->request_flags0
|= NO_LINK_DESCS
;
5769 ioarcb
->data_transfer_length
=
5770 cpu_to_le32(sizeof(struct pmcraid_config_table
));
5772 ioadl
= &(ioarcb
->add_data
.u
.ioadl
[0]);
5773 ioadl
->flags
= IOADL_FLAGS_LAST_DESC
;
5774 ioadl
->address
= cpu_to_le64(pinstance
->cfg_table_bus_addr
);
5775 ioadl
->data_len
= cpu_to_le32(sizeof(struct pmcraid_config_table
));
5777 pmcraid_send_cmd(cmd
, pmcraid_init_res_table
,
5778 PMCRAID_INTERNAL_TIMEOUT
, pmcraid_timeout_handler
);
5783 * pmcraid_probe - PCI probe entry pointer for PMC MaxRAID controller driver
5784 * @pdev: pointer to pci device structure
5785 * @dev_id: pointer to device ids structure
5788 * returns 0 if the device is claimed and successfully configured.
5789 * returns non-zero error code in case of any failure
5791 static int pmcraid_probe(struct pci_dev
*pdev
,
5792 const struct pci_device_id
*dev_id
)
5794 struct pmcraid_instance
*pinstance
;
5795 struct Scsi_Host
*host
;
5796 void __iomem
*mapped_pci_addr
;
5797 int rc
= PCIBIOS_SUCCESSFUL
;
5799 if (atomic_read(&pmcraid_adapter_count
) >= PMCRAID_MAX_ADAPTERS
) {
5801 ("maximum number(%d) of supported adapters reached\n",
5802 atomic_read(&pmcraid_adapter_count
));
5806 atomic_inc(&pmcraid_adapter_count
);
5807 rc
= pci_enable_device(pdev
);
5810 dev_err(&pdev
->dev
, "Cannot enable adapter\n");
5811 atomic_dec(&pmcraid_adapter_count
);
5815 dev_info(&pdev
->dev
,
5816 "Found new IOA(%x:%x), Total IOA count: %d\n",
5817 pdev
->vendor
, pdev
->device
,
5818 atomic_read(&pmcraid_adapter_count
));
5820 rc
= pci_request_regions(pdev
, PMCRAID_DRIVER_NAME
);
5824 "Couldn't register memory range of registers\n");
5825 goto out_disable_device
;
5828 mapped_pci_addr
= pci_iomap(pdev
, 0, 0);
5830 if (!mapped_pci_addr
) {
5831 dev_err(&pdev
->dev
, "Couldn't map PCI registers memory\n");
5833 goto out_release_regions
;
5836 pci_set_master(pdev
);
5838 /* Firmware requires the system bus address of IOARCB to be within
5839 * 32-bit addressable range though it has 64-bit IOARRIN register.
5840 * However, firmware supports 64-bit streaming DMA buffers, whereas
5841 * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
5842 * returns memory within 4GB (if not, change this logic), coherent
5843 * buffers are within firmware acceptable address ranges.
5845 if ((sizeof(dma_addr_t
) == 4) ||
5846 pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)))
5847 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
5849 /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
5850 * bit mask for pci_alloc_consistent to return addresses within 4GB
5853 rc
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
5856 dev_err(&pdev
->dev
, "Failed to set PCI DMA mask\n");
5860 host
= scsi_host_alloc(&pmcraid_host_template
,
5861 sizeof(struct pmcraid_instance
));
5864 dev_err(&pdev
->dev
, "scsi_host_alloc failed!\n");
5869 host
->max_id
= PMCRAID_MAX_NUM_TARGETS_PER_BUS
;
5870 host
->max_lun
= PMCRAID_MAX_NUM_LUNS_PER_TARGET
;
5871 host
->unique_id
= host
->host_no
;
5872 host
->max_channel
= PMCRAID_MAX_BUS_TO_SCAN
;
5873 host
->max_cmd_len
= PMCRAID_MAX_CDB_LEN
;
5875 /* zero out entire instance structure */
5876 pinstance
= (struct pmcraid_instance
*)host
->hostdata
;
5877 memset(pinstance
, 0, sizeof(*pinstance
));
5879 pinstance
->chip_cfg
=
5880 (struct pmcraid_chip_details
*)(dev_id
->driver_data
);
5882 rc
= pmcraid_init_instance(pdev
, host
, mapped_pci_addr
);
5885 dev_err(&pdev
->dev
, "failed to initialize adapter instance\n");
5886 goto out_scsi_host_put
;
5889 pci_set_drvdata(pdev
, pinstance
);
5891 /* Save PCI config-space for use following the reset */
5892 rc
= pci_save_state(pinstance
->pdev
);
5895 dev_err(&pdev
->dev
, "Failed to save PCI config space\n");
5896 goto out_scsi_host_put
;
5899 pmcraid_disable_interrupts(pinstance
, ~0);
5901 rc
= pmcraid_register_interrupt_handler(pinstance
);
5904 dev_err(&pdev
->dev
, "couldn't register interrupt handler\n");
5905 goto out_scsi_host_put
;
5908 pmcraid_init_tasklets(pinstance
);
5910 /* allocate verious buffers used by LLD.*/
5911 rc
= pmcraid_init_buffers(pinstance
);
5914 pmcraid_err("couldn't allocate memory blocks\n");
5915 goto out_unregister_isr
;
5918 /* check the reset type required */
5919 pmcraid_reset_type(pinstance
);
5921 pmcraid_enable_interrupts(pinstance
, PMCRAID_PCI_INTERRUPTS
);
5923 /* Start IOA firmware initialization and bring card to Operational
5926 pmcraid_info("starting IOA initialization sequence\n");
5927 if (pmcraid_reset_bringup(pinstance
)) {
5928 dev_err(&pdev
->dev
, "couldn't initialize IOA\n");
5930 goto out_release_bufs
;
5933 /* Add adapter instance into mid-layer list */
5934 rc
= scsi_add_host(pinstance
->host
, &pdev
->dev
);
5936 pmcraid_err("couldn't add host into mid-layer: %d\n", rc
);
5937 goto out_release_bufs
;
5940 scsi_scan_host(pinstance
->host
);
5942 rc
= pmcraid_setup_chrdev(pinstance
);
5945 pmcraid_err("couldn't create mgmt interface, error: %x\n",
5947 goto out_remove_host
;
5950 /* Schedule worker thread to handle CCN and take care of adding and
5951 * removing devices to OS
5953 atomic_set(&pinstance
->expose_resources
, 1);
5954 schedule_work(&pinstance
->worker_q
);
5958 scsi_remove_host(host
);
5961 pmcraid_release_buffers(pinstance
);
5964 pmcraid_kill_tasklets(pinstance
);
5965 pmcraid_unregister_interrupt_handler(pinstance
);
5968 scsi_host_put(host
);
5971 iounmap(mapped_pci_addr
);
5973 out_release_regions
:
5974 pci_release_regions(pdev
);
5977 atomic_dec(&pmcraid_adapter_count
);
5978 pci_disable_device(pdev
);
5983 * PCI driver structure of pcmraid driver
5985 static struct pci_driver pmcraid_driver
= {
5986 .name
= PMCRAID_DRIVER_NAME
,
5987 .id_table
= pmcraid_pci_table
,
5988 .probe
= pmcraid_probe
,
5989 .remove
= pmcraid_remove
,
5990 .suspend
= pmcraid_suspend
,
5991 .resume
= pmcraid_resume
,
5992 .shutdown
= pmcraid_shutdown
5996 * pmcraid_init - module load entry point
5998 static int __init
pmcraid_init(void)
6003 pmcraid_info("%s Device Driver version: %s\n",
6004 PMCRAID_DRIVER_NAME
, PMCRAID_DRIVER_VERSION
);
6006 error
= alloc_chrdev_region(&dev
, 0,
6007 PMCRAID_MAX_ADAPTERS
,
6011 pmcraid_err("failed to get a major number for adapters\n");
6015 pmcraid_major
= MAJOR(dev
);
6016 pmcraid_class
= class_create(THIS_MODULE
, PMCRAID_DEVFILE
);
6018 if (IS_ERR(pmcraid_class
)) {
6019 error
= PTR_ERR(pmcraid_class
);
6020 pmcraid_err("failed to register with sysfs, error = %x\n",
6022 goto out_unreg_chrdev
;
6025 error
= pmcraid_netlink_init();
6028 goto out_unreg_chrdev
;
6030 error
= pci_register_driver(&pmcraid_driver
);
6035 pmcraid_err("failed to register pmcraid driver, error = %x\n",
6037 class_destroy(pmcraid_class
);
6038 pmcraid_netlink_release();
6041 unregister_chrdev_region(MKDEV(pmcraid_major
, 0), PMCRAID_MAX_ADAPTERS
);
6048 * pmcraid_exit - module unload entry point
6050 static void __exit
pmcraid_exit(void)
6052 pmcraid_netlink_release();
6053 unregister_chrdev_region(MKDEV(pmcraid_major
, 0),
6054 PMCRAID_MAX_ADAPTERS
);
6055 pci_unregister_driver(&pmcraid_driver
);
6056 class_destroy(pmcraid_class
);
6059 module_init(pmcraid_init
);
6060 module_exit(pmcraid_exit
);