2 * pmcraid.c -- driver for PMC Sierra MaxRAID controller adapters
4 * Written By: PMC Sierra Corporation
6 * Copyright (C) 2008, 2009 PMC Sierra Inc
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/ioport.h>
30 #include <linux/delay.h>
31 #include <linux/pci.h>
32 #include <linux/wait.h>
33 #include <linux/spinlock.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/blkdev.h>
37 #include <linux/firmware.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/hdreg.h>
41 #include <linux/version.h>
44 #include <asm/processor.h>
45 #include <linux/libata.h>
46 #include <linux/mutex.h>
47 #include <scsi/scsi.h>
48 #include <scsi/scsi_host.h>
49 #include <scsi/scsi_device.h>
50 #include <scsi/scsi_tcq.h>
51 #include <scsi/scsi_eh.h>
52 #include <scsi/scsi_cmnd.h>
53 #include <scsi/scsicam.h>
58 * Module configuration parameters
60 static unsigned int pmcraid_debug_log;
61 static unsigned int pmcraid_disable_aen;
62 static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
65 * Data structures to support multiple adapters by the LLD.
66 * pmcraid_adapter_count - count of configured adapters
68 static atomic_t pmcraid_adapter_count = ATOMIC_INIT(0);
71 * Supporting user-level control interface through IOCTL commands.
72 * pmcraid_major - major number to use
73 * pmcraid_minor - minor number(s) to use
75 static unsigned int pmcraid_major;
76 static struct class *pmcraid_class;
77 DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
82 MODULE_AUTHOR("PMC Sierra Corporation, anil_ravindranath@pmc-sierra.com");
83 MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
84 MODULE_LICENSE("GPL");
85 MODULE_VERSION(PMCRAID_DRIVER_VERSION);
87 module_param_named(log_level, pmcraid_log_level, uint, (S_IRUGO | S_IWUSR));
88 MODULE_PARM_DESC(log_level,
89 "Enables firmware error code logging, default :1 high-severity"
90 " errors, 2: all errors including high-severity errors,"
91 " 0: disables logging");
93 module_param_named(debug, pmcraid_debug_log, uint, (S_IRUGO | S_IWUSR));
94 MODULE_PARM_DESC(debug,
95 "Enable driver verbose message logging. Set 1 to enable."
98 module_param_named(disable_aen, pmcraid_disable_aen, uint, (S_IRUGO | S_IWUSR));
99 MODULE_PARM_DESC(disable_aen,
100 "Disable driver aen notifications to apps. Set 1 to disable."
103 /* chip specific constants for PMC MaxRAID controllers (same for
106 static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
111 .global_intr_mask = 0x00034,
112 .ioa_host_intr = 0x0009C,
113 .ioa_host_intr_clr = 0x000A0,
114 .ioa_host_mask = 0x7FC28,
115 .ioa_host_mask_clr = 0x7FC28,
116 .host_ioa_intr = 0x00020,
117 .host_ioa_intr_clr = 0x00020,
118 .transop_timeout = 300
123 * PCI device ids supported by pmcraid driver
125 static struct pci_device_id pmcraid_pci_table[] __devinitdata = {
126 { PCI_DEVICE(PCI_VENDOR_ID_PMC, PCI_DEVICE_ID_PMC_MAXRAID),
127 0, 0, (kernel_ulong_t)&pmcraid_chip_cfg[0]
132 MODULE_DEVICE_TABLE(pci, pmcraid_pci_table);
137 * pmcraid_slave_alloc - Prepare for commands to a device
138 * @scsi_dev: scsi device struct
140 * This function is called by mid-layer prior to sending any command to the new
141 * device. Stores resource entry details of the device in scsi_device struct.
142 * Queuecommand uses the resource handle and other details to fill up IOARCB
143 * while sending commands to the device.
146 * 0 on success / -ENXIO if device does not exist
148 static int pmcraid_slave_alloc(struct scsi_device *scsi_dev)
150 struct pmcraid_resource_entry *temp, *res = NULL;
151 struct pmcraid_instance *pinstance;
153 unsigned long lock_flags;
155 pinstance = shost_priv(scsi_dev->host);
157 /* Driver exposes VSET and GSCSI resources only; all other device types
158 * are not exposed. Resource list is synchronized using resource lock
159 * so any traversal or modifications to the list should be done inside
162 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
163 list_for_each_entry(temp, &pinstance->used_res_q, queue) {
165 /* do not expose VSETs with order-ids >= 240 */
166 if (RES_IS_VSET(temp->cfg_entry)) {
167 target = temp->cfg_entry.unique_flags1;
168 if (target >= PMCRAID_MAX_VSET_TARGETS)
170 bus = PMCRAID_VSET_BUS_ID;
172 } else if (RES_IS_GSCSI(temp->cfg_entry)) {
173 target = RES_TARGET(temp->cfg_entry.resource_address);
174 bus = PMCRAID_PHYS_BUS_ID;
175 lun = RES_LUN(temp->cfg_entry.resource_address);
180 if (bus == scsi_dev->channel &&
181 target == scsi_dev->id &&
182 lun == scsi_dev->lun) {
189 res->scsi_dev = scsi_dev;
190 scsi_dev->hostdata = res;
191 res->change_detected = 0;
192 atomic_set(&res->read_failures, 0);
193 atomic_set(&res->write_failures, 0);
196 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
201 * pmcraid_slave_configure - Configures a SCSI device
202 * @scsi_dev: scsi device struct
204 * This fucntion is executed by SCSI mid layer just after a device is first
205 * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
206 * timeout value (default 30s) will be over-written to a higher value (60s)
207 * and max_sectors value will be over-written to 512. It also sets queue depth
208 * to host->cmd_per_lun value
213 static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
215 struct pmcraid_resource_entry *res = scsi_dev->hostdata;
220 /* LLD exposes VSETs and Enclosure devices only */
221 if (RES_IS_GSCSI(res->cfg_entry) &&
222 scsi_dev->type != TYPE_ENCLOSURE)
225 pmcraid_info("configuring %x:%x:%x:%x\n",
226 scsi_dev->host->unique_id,
231 if (RES_IS_GSCSI(res->cfg_entry)) {
232 scsi_dev->allow_restart = 1;
233 } else if (RES_IS_VSET(res->cfg_entry)) {
234 scsi_dev->allow_restart = 1;
235 blk_queue_rq_timeout(scsi_dev->request_queue,
236 PMCRAID_VSET_IO_TIMEOUT);
237 blk_queue_max_sectors(scsi_dev->request_queue,
238 PMCRAID_VSET_MAX_SECTORS);
241 if (scsi_dev->tagged_supported &&
242 (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
243 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
244 scsi_adjust_queue_depth(scsi_dev, MSG_SIMPLE_TAG,
245 scsi_dev->host->cmd_per_lun);
247 scsi_adjust_queue_depth(scsi_dev, 0,
248 scsi_dev->host->cmd_per_lun);
255 * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
257 * @scsi_dev: scsi device struct
259 * This is called by mid-layer before removing a device. Pointer assignments
260 * done in pmcraid_slave_alloc will be reset to NULL here.
265 static void pmcraid_slave_destroy(struct scsi_device *scsi_dev)
267 struct pmcraid_resource_entry *res;
269 res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
272 res->scsi_dev = NULL;
274 scsi_dev->hostdata = NULL;
278 * pmcraid_change_queue_depth - Change the device's queue depth
279 * @scsi_dev: scsi device struct
280 * @depth: depth to set
285 static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth)
287 if (depth > PMCRAID_MAX_CMD_PER_LUN)
288 depth = PMCRAID_MAX_CMD_PER_LUN;
290 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), depth);
292 return scsi_dev->queue_depth;
296 * pmcraid_change_queue_type - Change the device's queue type
297 * @scsi_dev: scsi device struct
298 * @tag: type of tags to use
301 * actual queue type set
303 static int pmcraid_change_queue_type(struct scsi_device *scsi_dev, int tag)
305 struct pmcraid_resource_entry *res;
307 res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
309 if ((res) && scsi_dev->tagged_supported &&
310 (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
311 scsi_set_tag_type(scsi_dev, tag);
314 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
316 scsi_deactivate_tcq(scsi_dev, scsi_dev->queue_depth);
325 * pmcraid_init_cmdblk - initializes a command block
327 * @cmd: pointer to struct pmcraid_cmd to be initialized
328 * @index: if >=0 first time initialization; otherwise reinitialization
333 void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
335 struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
336 dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
339 /* first time initialization (called from probe) */
341 offsetof(struct pmcraid_control_block, ioasa);
344 ioarcb->response_handle = cpu_to_le32(index << 2);
345 ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
346 ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
347 ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
349 /* re-initialization of various lengths, called once command is
352 memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
353 ioarcb->request_flags0 = 0;
354 ioarcb->request_flags1 = 0;
355 ioarcb->cmd_timeout = 0;
356 ioarcb->ioarcb_bus_addr &= (~0x1FULL);
357 ioarcb->ioadl_bus_addr = 0;
358 ioarcb->ioadl_length = 0;
359 ioarcb->data_transfer_length = 0;
360 ioarcb->add_cmd_param_length = 0;
361 ioarcb->add_cmd_param_offset = 0;
362 cmd->ioa_cb->ioasa.ioasc = 0;
363 cmd->ioa_cb->ioasa.residual_data_length = 0;
364 cmd->u.time_left = 0;
367 cmd->cmd_done = NULL;
368 cmd->scsi_cmd = NULL;
370 cmd->completion_req = 0;
372 init_timer(&cmd->timer);
376 * pmcraid_reinit_cmdblk - reinitialize a command block
378 * @cmd: pointer to struct pmcraid_cmd to be reinitialized
383 static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
385 pmcraid_init_cmdblk(cmd, -1);
389 * pmcraid_get_free_cmd - get a free cmd block from command block pool
390 * @pinstance: adapter instance structure
393 * returns pointer to cmd block or NULL if no blocks are available
395 static struct pmcraid_cmd *pmcraid_get_free_cmd(
396 struct pmcraid_instance *pinstance
399 struct pmcraid_cmd *cmd = NULL;
400 unsigned long lock_flags;
402 /* free cmd block list is protected by free_pool_lock */
403 spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
405 if (!list_empty(&pinstance->free_cmd_pool)) {
406 cmd = list_entry(pinstance->free_cmd_pool.next,
407 struct pmcraid_cmd, free_list);
408 list_del(&cmd->free_list);
410 spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
412 /* Initialize the command block before giving it the caller */
414 pmcraid_reinit_cmdblk(cmd);
419 * pmcraid_return_cmd - return a completed command block back into free pool
420 * @cmd: pointer to the command block
425 void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
427 struct pmcraid_instance *pinstance = cmd->drv_inst;
428 unsigned long lock_flags;
430 spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
431 list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
432 spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
436 * pmcraid_read_interrupts - reads IOA interrupts
438 * @pinstance: pointer to adapter instance structure
441 * interrupts read from IOA
443 static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
445 return ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
449 * pmcraid_disable_interrupts - Masks and clears all specified interrupts
451 * @pinstance: pointer to per adapter instance structure
452 * @intrs: interrupts to disable
457 static void pmcraid_disable_interrupts(
458 struct pmcraid_instance *pinstance,
462 u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
463 u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
465 iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
466 iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
467 iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_mask_reg);
468 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
472 * pmcraid_enable_interrupts - Enables specified interrupts
474 * @pinstance: pointer to per adapter instance structure
475 * @intr: interrupts to enable
480 static void pmcraid_enable_interrupts(
481 struct pmcraid_instance *pinstance,
485 u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
486 u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
488 iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
489 iowrite32(~intrs, pinstance->int_regs.ioa_host_interrupt_mask_reg);
490 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
492 pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
493 ioread32(pinstance->int_regs.global_interrupt_mask_reg),
494 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
498 * pmcraid_reset_type - Determine the required reset type
499 * @pinstance: pointer to adapter instance structure
501 * IOA requires hard reset if any of the following conditions is true.
502 * 1. If HRRQ valid interrupt is not masked
503 * 2. IOA reset alert doorbell is set
504 * 3. If there are any error interrupts
506 static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
512 mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
513 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
514 alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
516 if ((mask & INTRS_HRRQ_VALID) == 0 ||
517 (alerts & DOORBELL_IOA_RESET_ALERT) ||
518 (intrs & PMCRAID_ERROR_INTERRUPTS)) {
519 pmcraid_info("IOA requires hard reset\n");
520 pinstance->ioa_hard_reset = 1;
523 /* If unit check is active, trigger the dump */
524 if (intrs & INTRS_IOA_UNIT_CHECK)
525 pinstance->ioa_unit_check = 1;
529 * pmcraid_bist_done - completion function for PCI BIST
530 * @cmd: pointer to reset command
535 static void pmcraid_ioa_reset(struct pmcraid_cmd *);
537 static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
539 struct pmcraid_instance *pinstance = cmd->drv_inst;
540 unsigned long lock_flags;
544 rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
546 /* If PCI config space can't be accessed wait for another two secs */
547 if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
548 cmd->u.time_left > 0) {
549 pmcraid_info("BIST not complete, waiting another 2 secs\n");
550 cmd->timer.expires = jiffies + cmd->u.time_left;
551 cmd->u.time_left = 0;
552 cmd->timer.data = (unsigned long)cmd;
553 cmd->timer.function =
554 (void (*)(unsigned long))pmcraid_bist_done;
555 add_timer(&cmd->timer);
557 cmd->u.time_left = 0;
558 pmcraid_info("BIST is complete, proceeding with reset\n");
559 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
560 pmcraid_ioa_reset(cmd);
561 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
566 * pmcraid_start_bist - starts BIST
567 * @cmd: pointer to reset cmd
571 static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
573 struct pmcraid_instance *pinstance = cmd->drv_inst;
574 u32 doorbells, intrs;
576 /* proceed with bist and wait for 2 seconds */
577 iowrite32(DOORBELL_IOA_START_BIST,
578 pinstance->int_regs.host_ioa_interrupt_reg);
579 doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
580 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
581 pmcraid_info("doorbells after start bist: %x intrs: %x \n",
584 cmd->u.time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
585 cmd->timer.data = (unsigned long)cmd;
586 cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
587 cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
588 add_timer(&cmd->timer);
592 * pmcraid_reset_alert_done - completion routine for reset_alert
593 * @cmd: pointer to command block used in reset sequence
597 static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
599 struct pmcraid_instance *pinstance = cmd->drv_inst;
600 u32 status = ioread32(pinstance->ioa_status);
601 unsigned long lock_flags;
603 /* if the critical operation in progress bit is set or the wait times
604 * out, invoke reset engine to proceed with hard reset. If there is
605 * some more time to wait, restart the timer
607 if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
608 cmd->u.time_left <= 0) {
609 pmcraid_info("critical op is reset proceeding with reset\n");
610 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
611 pmcraid_ioa_reset(cmd);
612 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
614 pmcraid_info("critical op is not yet reset waiting again\n");
615 /* restart timer if some more time is available to wait */
616 cmd->u.time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
617 cmd->timer.data = (unsigned long)cmd;
618 cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
619 cmd->timer.function =
620 (void (*)(unsigned long))pmcraid_reset_alert_done;
621 add_timer(&cmd->timer);
626 * pmcraid_reset_alert - alerts IOA for a possible reset
627 * @cmd : command block to be used for reset sequence.
630 * returns 0 if pci config-space is accessible and RESET_DOORBELL is
631 * successfully written to IOA. Returns non-zero in case pci_config_space
634 static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
636 struct pmcraid_instance *pinstance = cmd->drv_inst;
641 /* If we are able to access IOA PCI config space, alert IOA that we are
642 * going to reset it soon. This enables IOA to preserv persistent error
643 * data if any. In case memory space is not accessible, proceed with
646 rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
647 if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
649 /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
650 * reset IOA doesn't generate any interrupts when CRITICAL
651 * OPERATION bit is reset. A timer is started to wait for this
654 cmd->u.time_left = PMCRAID_RESET_TIMEOUT;
655 cmd->timer.data = (unsigned long)cmd;
656 cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
657 cmd->timer.function =
658 (void (*)(unsigned long))pmcraid_reset_alert_done;
659 add_timer(&cmd->timer);
661 iowrite32(DOORBELL_IOA_RESET_ALERT,
662 pinstance->int_regs.host_ioa_interrupt_reg);
664 ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
665 pmcraid_info("doorbells after reset alert: %x\n", doorbells);
667 pmcraid_info("PCI config is not accessible starting BIST\n");
668 pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
669 pmcraid_start_bist(cmd);
674 * pmcraid_timeout_handler - Timeout handler for internally generated ops
676 * @cmd : pointer to command structure, that got timedout
678 * This function blocks host requests and initiates an adapter reset.
683 static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
685 struct pmcraid_instance *pinstance = cmd->drv_inst;
686 unsigned long lock_flags;
688 dev_info(&pinstance->pdev->dev,
689 "Adapter being reset due to command timeout.\n");
691 /* Command timeouts result in hard reset sequence. The command that got
692 * timed out may be the one used as part of reset sequence. In this
693 * case restart reset sequence using the same command block even if
694 * reset is in progress. Otherwise fail this command and get a free
695 * command block to restart the reset sequence.
697 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
698 if (!pinstance->ioa_reset_in_progress) {
699 pinstance->ioa_reset_attempts = 0;
700 cmd = pmcraid_get_free_cmd(pinstance);
702 /* If we are out of command blocks, just return here itself.
703 * Some other command's timeout handler can do the reset job
706 spin_unlock_irqrestore(pinstance->host->host_lock,
708 pmcraid_err("no free cmnd block for timeout handler\n");
712 pinstance->reset_cmd = cmd;
713 pinstance->ioa_reset_in_progress = 1;
715 pmcraid_info("reset is already in progress\n");
717 if (pinstance->reset_cmd != cmd) {
718 /* This command should have been given to IOA, this
719 * command will be completed by fail_outstanding_cmds
722 pmcraid_err("cmd is pending but reset in progress\n");
725 /* If this command was being used as part of the reset
726 * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
727 * causes fail_outstanding_commands not to return the command
728 * block back to free pool
730 if (cmd == pinstance->reset_cmd)
731 cmd->cmd_done = pmcraid_ioa_reset;
735 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
736 scsi_block_requests(pinstance->host);
737 pmcraid_reset_alert(cmd);
738 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
742 * pmcraid_internal_done - completion routine for internally generated cmds
744 * @cmd: command that got response from IOA
749 static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
751 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
752 cmd->ioa_cb->ioarcb.cdb[0],
753 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
755 /* Some of the internal commands are sent with callers blocking for the
756 * response. Same will be indicated as part of cmd->completion_req
757 * field. Response path needs to wake up any waiters waiting for cmd
758 * completion if this flag is set.
760 if (cmd->completion_req) {
761 cmd->completion_req = 0;
762 complete(&cmd->wait_for_completion);
765 /* most of the internal commands are completed by caller itself, so
766 * no need to return the command block back to free pool until we are
767 * required to do so (e.g once done with initialization).
771 pmcraid_return_cmd(cmd);
776 * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
778 * @cmd: command that got response from IOA
780 * This routine is called after driver re-reads configuration table due to a
781 * lost CCN. It returns the command block back to free pool and schedules
782 * worker thread to add/delete devices into the system.
787 static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
789 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
790 cmd->ioa_cb->ioarcb.cdb[0],
791 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
795 pmcraid_return_cmd(cmd);
797 pmcraid_info("scheduling worker for config table reinitialization\n");
798 schedule_work(&cmd->drv_inst->worker_q);
802 * pmcraid_erp_done - Process completion of SCSI error response from device
803 * @cmd: pmcraid_command
805 * This function copies the sense buffer into the scsi_cmd struct and completes
806 * scsi_cmd by calling scsi_done function.
811 static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
813 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
814 struct pmcraid_instance *pinstance = cmd->drv_inst;
815 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
817 if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
818 scsi_cmd->result |= (DID_ERROR << 16);
819 scmd_printk(KERN_INFO, scsi_cmd,
820 "command CDB[0] = %x failed with IOASC: 0x%08X\n",
821 cmd->ioa_cb->ioarcb.cdb[0], ioasc);
824 /* if we had allocated sense buffers for request sense, copy the sense
825 * release the buffers
827 if (cmd->sense_buffer != NULL) {
828 memcpy(scsi_cmd->sense_buffer,
830 SCSI_SENSE_BUFFERSIZE);
831 pci_free_consistent(pinstance->pdev,
832 SCSI_SENSE_BUFFERSIZE,
833 cmd->sense_buffer, cmd->sense_buffer_dma);
834 cmd->sense_buffer = NULL;
835 cmd->sense_buffer_dma = 0;
838 scsi_dma_unmap(scsi_cmd);
839 pmcraid_return_cmd(cmd);
840 scsi_cmd->scsi_done(scsi_cmd);
844 * pmcraid_fire_command - sends an IOA command to adapter
846 * This function adds the given block into pending command list
847 * and returns without waiting
849 * @cmd : command to be sent to the device
854 static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
856 struct pmcraid_instance *pinstance = cmd->drv_inst;
857 unsigned long lock_flags;
859 /* Add this command block to pending cmd pool. We do this prior to
860 * writting IOARCB to ioarrin because IOA might complete the command
861 * by the time we are about to add it to the list. Response handler
862 * (isr/tasklet) looks for cmb block in the pending pending list.
864 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
865 list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
866 spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
867 atomic_inc(&pinstance->outstanding_cmds);
869 /* driver writes lower 32-bit value of IOARCB address only */
871 iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
876 * pmcraid_send_cmd - fires a command to IOA
878 * This function also sets up timeout function, and command completion
881 * @cmd: pointer to the command block to be fired to IOA
882 * @cmd_done: command completion function, called once IOA responds
883 * @timeout: timeout to wait for this command completion
884 * @timeout_func: timeout handler
889 static void pmcraid_send_cmd(
890 struct pmcraid_cmd *cmd,
891 void (*cmd_done) (struct pmcraid_cmd *),
892 unsigned long timeout,
893 void (*timeout_func) (struct pmcraid_cmd *)
896 /* initialize done function */
897 cmd->cmd_done = cmd_done;
900 /* setup timeout handler */
901 cmd->timer.data = (unsigned long)cmd;
902 cmd->timer.expires = jiffies + timeout;
903 cmd->timer.function = (void (*)(unsigned long))timeout_func;
904 add_timer(&cmd->timer);
907 /* fire the command to IOA */
908 _pmcraid_fire_command(cmd);
912 * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
914 * @cmd: pointer to the command block used as part of reset sequence
919 static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
921 pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
922 cmd->ioa_cb->ioarcb.cdb[0],
923 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
925 /* Note that commands sent during reset require next command to be sent
926 * to IOA. Hence reinit the done function as well as timeout function
928 pmcraid_reinit_cmdblk(cmd);
929 cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
930 cmd->ioa_cb->ioarcb.resource_handle =
931 cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
932 cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
933 cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
935 /* fire shutdown command to hardware. */
936 pmcraid_info("firing normal shutdown command (%d) to IOA\n",
937 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
939 pmcraid_send_cmd(cmd, pmcraid_ioa_reset,
940 PMCRAID_SHUTDOWN_TIMEOUT,
941 pmcraid_timeout_handler);
945 * pmcraid_identify_hrrq - registers host rrq buffers with IOA
946 * @cmd: pointer to command block to be used for identify hrrq
949 * 0 in case of success, otherwise non-zero failure code
952 static void pmcraid_querycfg(struct pmcraid_cmd *);
954 static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
956 struct pmcraid_instance *pinstance = cmd->drv_inst;
957 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
959 __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
960 u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
962 pmcraid_reinit_cmdblk(cmd);
964 /* Initialize ioarcb */
965 ioarcb->request_type = REQ_TYPE_IOACMD;
966 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
968 /* initialize the hrrq number where IOA will respond to this command */
969 ioarcb->hrrq_id = index;
970 ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
971 ioarcb->cdb[1] = index;
973 /* IOA expects 64-bit pci address to be written in B.E format
974 * (i.e cdb[2]=MSByte..cdb[9]=LSB.
976 pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb => %llx:%llx\n",
977 hrrq_addr, ioarcb->ioarcb_bus_addr);
979 memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
980 memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
982 /* Subsequent commands require HRRQ identification to be successful.
983 * Note that this gets called even during reset from SCSI mid-layer
986 pmcraid_send_cmd(cmd, pmcraid_querycfg,
987 PMCRAID_INTERNAL_TIMEOUT,
988 pmcraid_timeout_handler);
991 static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
992 static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
995 * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
997 * @cmd: initialized command block pointer
1002 static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
1004 if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
1005 atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
1007 atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
1009 pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
1013 * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
1015 * @pinstance: pointer to adapter instance structure
1019 * pointer to initialized pmcraid_cmd structure or NULL
1021 static struct pmcraid_cmd *pmcraid_init_hcam
1023 struct pmcraid_instance *pinstance,
1027 struct pmcraid_cmd *cmd;
1028 struct pmcraid_ioarcb *ioarcb;
1029 struct pmcraid_ioadl_desc *ioadl;
1030 struct pmcraid_hostrcb *hcam;
1031 void (*cmd_done) (struct pmcraid_cmd *);
1035 cmd = pmcraid_get_free_cmd(pinstance);
1038 pmcraid_err("no free command blocks for hcam\n");
1042 if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
1043 rcb_size = sizeof(struct pmcraid_hcam_ccn);
1044 cmd_done = pmcraid_process_ccn;
1045 dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
1046 hcam = &pinstance->ccn;
1048 rcb_size = sizeof(struct pmcraid_hcam_ldn);
1049 cmd_done = pmcraid_process_ldn;
1050 dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
1051 hcam = &pinstance->ldn;
1054 /* initialize command pointer used for HCAM registration */
1057 ioarcb = &cmd->ioa_cb->ioarcb;
1058 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
1059 offsetof(struct pmcraid_ioarcb,
1060 add_data.u.ioadl[0]));
1061 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
1062 ioadl = ioarcb->add_data.u.ioadl;
1064 /* Initialize ioarcb */
1065 ioarcb->request_type = REQ_TYPE_HCAM;
1066 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
1067 ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
1068 ioarcb->cdb[1] = type;
1069 ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
1070 ioarcb->cdb[8] = (rcb_size) & 0xFF;
1072 ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
1074 ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
1075 ioadl[0].data_len = cpu_to_le32(rcb_size);
1076 ioadl[0].address = cpu_to_le32(dma);
1078 cmd->cmd_done = cmd_done;
1083 * pmcraid_send_hcam - Send an HCAM to IOA
1084 * @pinstance: ioa config struct
1087 * This function will send a Host Controlled Async command to IOA.
1092 static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
1094 struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
1095 pmcraid_send_hcam_cmd(cmd);
1100 * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
1102 * @cmd: pointer to cmd that is used as cancelling command
1103 * @cmd_to_cancel: pointer to the command that needs to be cancelled
1105 static void pmcraid_prepare_cancel_cmd(
1106 struct pmcraid_cmd *cmd,
1107 struct pmcraid_cmd *cmd_to_cancel
1110 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
1111 __be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
1113 /* Get the resource handle to where the command to be aborted has been
1116 ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
1117 ioarcb->request_type = REQ_TYPE_IOACMD;
1118 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
1119 ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
1121 /* IOARCB address of the command to be cancelled is given in
1122 * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
1123 * IOARCB address are not masked.
1125 ioarcb_addr = cpu_to_be64(ioarcb_addr);
1126 memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
1130 * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
1132 * @cmd: command to be used as cancelling command
1134 * @cmd_done: op done function for the cancelling command
1136 static void pmcraid_cancel_hcam(
1137 struct pmcraid_cmd *cmd,
1139 void (*cmd_done) (struct pmcraid_cmd *)
1142 struct pmcraid_instance *pinstance;
1143 struct pmcraid_hostrcb *hcam;
1145 pinstance = cmd->drv_inst;
1146 hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
1147 &pinstance->ldn : &pinstance->ccn;
1149 /* prepare for cancelling previous hcam command. If the HCAM is
1150 * currently not pending with IOA, we would have hcam->cmd as non-null
1152 if (hcam->cmd == NULL)
1155 pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
1157 /* writing to IOARRIN must be protected by host_lock, as mid-layer
1158 * schedule queuecommand while we are doing this
1160 pmcraid_send_cmd(cmd, cmd_done,
1161 PMCRAID_INTERNAL_TIMEOUT,
1162 pmcraid_timeout_handler);
1166 * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
1168 * @cmd: command block to be used for cancelling the HCAM
1170 static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
1172 pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
1173 cmd->ioa_cb->ioarcb.cdb[0],
1174 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
1176 pmcraid_reinit_cmdblk(cmd);
1178 pmcraid_cancel_hcam(cmd,
1179 PMCRAID_HCAM_CODE_CONFIG_CHANGE,
1180 pmcraid_ioa_shutdown);
1184 * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
1186 * @cmd: command block to be used for cancelling the HCAM
1188 static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
1190 pmcraid_cancel_hcam(cmd,
1191 PMCRAID_HCAM_CODE_LOG_DATA,
1192 pmcraid_cancel_ccn);
1196 * pmcraid_expose_resource - check if the resource can be exposed to OS
1198 * @cfgte: pointer to configuration table entry of the resource
1201 * true if resource can be added to midlayer, false(0) otherwise
1203 static int pmcraid_expose_resource(struct pmcraid_config_table_entry *cfgte)
1207 if (cfgte->resource_type == RES_TYPE_VSET)
1208 retval = ((cfgte->unique_flags1 & 0xFF) < 0xFE);
1209 else if (cfgte->resource_type == RES_TYPE_GSCSI)
1210 retval = (RES_BUS(cfgte->resource_address) !=
1211 PMCRAID_VIRTUAL_ENCL_BUS_ID);
1215 /* attributes supported by pmcraid_event_family */
1217 PMCRAID_AEN_ATTR_UNSPEC,
1218 PMCRAID_AEN_ATTR_EVENT,
1219 __PMCRAID_AEN_ATTR_MAX,
1221 #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
1223 /* commands supported by pmcraid_event_family */
1225 PMCRAID_AEN_CMD_UNSPEC,
1226 PMCRAID_AEN_CMD_EVENT,
1227 __PMCRAID_AEN_CMD_MAX,
1229 #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
1231 static struct genl_family pmcraid_event_family = {
1232 .id = GENL_ID_GENERATE,
1235 .maxattr = PMCRAID_AEN_ATTR_MAX
1239 * pmcraid_netlink_init - registers pmcraid_event_family
1242 * 0 if the pmcraid_event_family is successfully registered
1243 * with netlink generic, non-zero otherwise
1245 static int pmcraid_netlink_init(void)
1249 result = genl_register_family(&pmcraid_event_family);
1254 pmcraid_info("registered NETLINK GENERIC group: %d\n",
1255 pmcraid_event_family.id);
1261 * pmcraid_netlink_release - unregisters pmcraid_event_family
1266 static void pmcraid_netlink_release(void)
1268 genl_unregister_family(&pmcraid_event_family);
1272 * pmcraid_notify_aen - sends event msg to user space application
1273 * @pinstance: pointer to adapter instance structure
1277 * 0 if success, error value in case of any failure.
1279 static int pmcraid_notify_aen(struct pmcraid_instance *pinstance, u8 type)
1281 struct sk_buff *skb;
1282 struct pmcraid_aen_msg *aen_msg;
1284 int data_size, total_size;
1288 if (type == PMCRAID_HCAM_CODE_LOG_DATA) {
1289 aen_msg = pinstance->ldn.msg;
1290 data_size = pinstance->ldn.hcam->data_len;
1292 aen_msg = pinstance->ccn.msg;
1293 data_size = pinstance->ccn.hcam->data_len;
1296 data_size += sizeof(struct pmcraid_hcam_hdr);
1297 aen_msg->hostno = (pinstance->host->unique_id << 16 |
1298 MINOR(pinstance->cdev.dev));
1299 aen_msg->length = data_size;
1300 data_size += sizeof(*aen_msg);
1302 total_size = nla_total_size(data_size);
1303 skb = genlmsg_new(total_size, GFP_ATOMIC);
1307 pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
1312 /* add the genetlink message header */
1313 msg_header = genlmsg_put(skb, 0, 0,
1314 &pmcraid_event_family, 0,
1315 PMCRAID_AEN_CMD_EVENT);
1317 pmcraid_err("failed to copy command details\n");
1322 result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
1325 pmcraid_err("failed to copy AEN attribute data \n");
1330 /* send genetlink multicast message to notify appplications */
1331 result = genlmsg_end(skb, msg_header);
1334 pmcraid_err("genlmsg_end failed\n");
1340 genlmsg_multicast(skb, 0, pmcraid_event_family.id, GFP_ATOMIC);
1342 /* If there are no listeners, genlmsg_multicast may return non-zero
1346 pmcraid_info("failed to send %s event message %x!\n",
1347 type == PMCRAID_HCAM_CODE_LOG_DATA ? "LDN" : "CCN",
1353 * pmcraid_handle_config_change - Handle a config change from the adapter
1354 * @pinstance: pointer to per adapter instance structure
1359 static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
1361 struct pmcraid_config_table_entry *cfg_entry;
1362 struct pmcraid_hcam_ccn *ccn_hcam;
1363 struct pmcraid_cmd *cmd;
1364 struct pmcraid_cmd *cfgcmd;
1365 struct pmcraid_resource_entry *res = NULL;
1367 unsigned long lock_flags;
1368 unsigned long host_lock_flags;
1371 ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
1372 cfg_entry = &ccn_hcam->cfg_entry;
1375 ("CCN(%x): %x type: %x lost: %x flags: %x res: %x:%x:%x:%x\n",
1376 pinstance->ccn.hcam->ilid,
1377 pinstance->ccn.hcam->op_code,
1378 pinstance->ccn.hcam->notification_type,
1379 pinstance->ccn.hcam->notification_lost,
1380 pinstance->ccn.hcam->flags,
1381 pinstance->host->unique_id,
1382 RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
1383 (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
1384 RES_BUS(cfg_entry->resource_address)),
1385 RES_IS_VSET(*cfg_entry) ? cfg_entry->unique_flags1 :
1386 RES_TARGET(cfg_entry->resource_address),
1387 RES_LUN(cfg_entry->resource_address));
1390 /* If this HCAM indicates a lost notification, read the config table */
1391 if (pinstance->ccn.hcam->notification_lost) {
1392 cfgcmd = pmcraid_get_free_cmd(pinstance);
1394 pmcraid_info("lost CCN, reading config table\b");
1395 pinstance->reinit_cfg_table = 1;
1396 pmcraid_querycfg(cfgcmd);
1398 pmcraid_err("lost CCN, no free cmd for querycfg\n");
1400 goto out_notify_apps;
1403 /* If this resource is not going to be added to mid-layer, just notify
1404 * applications and return
1406 if (!pmcraid_expose_resource(cfg_entry))
1407 goto out_notify_apps;
1409 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
1410 list_for_each_entry(res, &pinstance->used_res_q, queue) {
1411 rc = memcmp(&res->cfg_entry.resource_address,
1412 &cfg_entry->resource_address,
1413 sizeof(cfg_entry->resource_address));
1422 /* If there are more number of resources than what driver can
1423 * manage, do not notify the applications about the CCN. Just
1424 * ignore this notifications and re-register the same HCAM
1426 if (list_empty(&pinstance->free_res_q)) {
1427 spin_unlock_irqrestore(&pinstance->resource_lock,
1429 pmcraid_err("too many resources attached\n");
1430 spin_lock_irqsave(pinstance->host->host_lock,
1432 pmcraid_send_hcam(pinstance,
1433 PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1434 spin_unlock_irqrestore(pinstance->host->host_lock,
1439 res = list_entry(pinstance->free_res_q.next,
1440 struct pmcraid_resource_entry, queue);
1442 list_del(&res->queue);
1443 res->scsi_dev = NULL;
1444 res->reset_progress = 0;
1445 list_add_tail(&res->queue, &pinstance->used_res_q);
1448 memcpy(&res->cfg_entry, cfg_entry,
1449 sizeof(struct pmcraid_config_table_entry));
1451 if (pinstance->ccn.hcam->notification_type ==
1452 NOTIFICATION_TYPE_ENTRY_DELETED) {
1453 if (res->scsi_dev) {
1454 res->change_detected = RES_CHANGE_DEL;
1455 res->cfg_entry.resource_handle =
1456 PMCRAID_INVALID_RES_HANDLE;
1457 schedule_work(&pinstance->worker_q);
1459 /* This may be one of the non-exposed resources */
1460 list_move_tail(&res->queue, &pinstance->free_res_q);
1462 } else if (!res->scsi_dev) {
1463 res->change_detected = RES_CHANGE_ADD;
1464 schedule_work(&pinstance->worker_q);
1466 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
1470 /* Notify configuration changes to registered applications.*/
1471 if (!pmcraid_disable_aen)
1472 pmcraid_notify_aen(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1474 cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1476 pmcraid_send_hcam_cmd(cmd);
1480 * pmcraid_get_error_info - return error string for an ioasc
1481 * @ioasc: ioasc code
1485 static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
1488 for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
1489 if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
1490 return &pmcraid_ioasc_error_table[i];
1496 * pmcraid_ioasc_logger - log IOASC information based user-settings
1497 * @ioasc: ioasc code
1498 * @cmd: pointer to command that resulted in 'ioasc'
1500 void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
1502 struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
1504 if (error_info == NULL ||
1505 cmd->drv_inst->current_log_level < error_info->log_level)
1508 /* log the error string */
1509 pmcraid_err("cmd [%d] for resource %x failed with %x(%s)\n",
1510 cmd->ioa_cb->ioarcb.cdb[0],
1511 cmd->ioa_cb->ioarcb.resource_handle,
1512 le32_to_cpu(ioasc), error_info->error_string);
1516 * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
1518 * @pinstance: pointer to per adapter instance structure
1523 static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
1525 struct pmcraid_hcam_ldn *hcam_ldn;
1528 hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
1531 ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
1532 pinstance->ldn.hcam->ilid,
1533 pinstance->ldn.hcam->op_code,
1534 pinstance->ldn.hcam->notification_type,
1535 pinstance->ldn.hcam->notification_lost,
1536 pinstance->ldn.hcam->flags,
1537 pinstance->ldn.hcam->overlay_id);
1539 /* log only the errors, no need to log informational log entries */
1540 if (pinstance->ldn.hcam->notification_type !=
1541 NOTIFICATION_TYPE_ERROR_LOG)
1544 if (pinstance->ldn.hcam->notification_lost ==
1545 HOSTRCB_NOTIFICATIONS_LOST)
1546 dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
1548 ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
1550 if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
1551 ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
1552 dev_info(&pinstance->pdev->dev,
1553 "UnitAttention due to IOA Bus Reset\n");
1554 scsi_report_bus_reset(
1556 RES_BUS(hcam_ldn->error_log.fd_ra));
1563 * pmcraid_process_ccn - Op done function for a CCN.
1564 * @cmd: pointer to command struct
1566 * This function is the op done function for a configuration
1567 * change notification
1572 static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
1574 struct pmcraid_instance *pinstance = cmd->drv_inst;
1575 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1576 unsigned long lock_flags;
1578 pinstance->ccn.cmd = NULL;
1579 pmcraid_return_cmd(cmd);
1581 /* If driver initiated IOA reset happened while this hcam was pending
1582 * with IOA, or IOA bringdown sequence is in progress, no need to
1583 * re-register the hcam
1585 if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
1586 atomic_read(&pinstance->ccn.ignore) == 1) {
1589 dev_info(&pinstance->pdev->dev,
1590 "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
1591 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
1592 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1593 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
1595 pmcraid_handle_config_change(pinstance);
1600 * pmcraid_process_ldn - op done function for an LDN
1601 * @cmd: pointer to command block
1606 static void pmcraid_initiate_reset(struct pmcraid_instance *);
1608 static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
1610 struct pmcraid_instance *pinstance = cmd->drv_inst;
1611 struct pmcraid_hcam_ldn *ldn_hcam =
1612 (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
1613 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1614 u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
1615 unsigned long lock_flags;
1617 /* return the command block back to freepool */
1618 pinstance->ldn.cmd = NULL;
1619 pmcraid_return_cmd(cmd);
1621 /* If driver initiated IOA reset happened while this hcam was pending
1622 * with IOA, no need to re-register the hcam as reset engine will do it
1623 * once reset sequence is complete
1625 if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
1626 atomic_read(&pinstance->ccn.ignore) == 1) {
1628 } else if (!ioasc) {
1629 pmcraid_handle_error_log(pinstance);
1630 if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
1631 spin_lock_irqsave(pinstance->host->host_lock,
1633 pmcraid_initiate_reset(pinstance);
1634 spin_unlock_irqrestore(pinstance->host->host_lock,
1639 dev_info(&pinstance->pdev->dev,
1640 "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
1642 /* send netlink message for HCAM notification if enabled */
1643 if (!pmcraid_disable_aen)
1644 pmcraid_notify_aen(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1646 cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1648 pmcraid_send_hcam_cmd(cmd);
1652 * pmcraid_register_hcams - register HCAMs for CCN and LDN
1654 * @pinstance: pointer per adapter instance structure
1659 static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
1661 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1662 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1666 * pmcraid_unregister_hcams - cancel HCAMs registered already
1667 * @cmd: pointer to command used as part of reset sequence
1669 static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
1671 struct pmcraid_instance *pinstance = cmd->drv_inst;
1673 /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
1674 * handling hcam response though it is not necessary. In order to
1675 * prevent this, set 'ignore', so that bring-down sequence doesn't
1676 * re-send any more hcams
1678 atomic_set(&pinstance->ccn.ignore, 1);
1679 atomic_set(&pinstance->ldn.ignore, 1);
1681 /* If adapter reset was forced as part of runtime reset sequence,
1682 * start the reset sequence.
1684 if (pinstance->force_ioa_reset && !pinstance->ioa_bringdown) {
1685 pinstance->force_ioa_reset = 0;
1686 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1687 pmcraid_reset_alert(cmd);
1691 /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
1692 * one after the other. So CCN cancellation will be triggered by
1693 * pmcraid_cancel_ldn itself.
1695 pmcraid_cancel_ldn(cmd);
1699 * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
1700 * @pinstance: pointer to adapter instance structure
1702 * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
1704 static void pmcraid_reinit_buffers(struct pmcraid_instance *);
1706 static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
1710 pmcraid_reinit_buffers(pinstance);
1711 intrs = pmcraid_read_interrupts(pinstance);
1713 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
1715 if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
1716 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
1717 pinstance->int_regs.ioa_host_interrupt_mask_reg);
1718 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
1719 pinstance->int_regs.ioa_host_interrupt_clr_reg);
1727 * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
1728 * @cmd : pointer to reset command block
1733 static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
1735 struct pmcraid_instance *pinstance = cmd->drv_inst;
1739 /* There will be an interrupt when Transition to Operational bit is
1740 * set so tasklet would execute next reset task. The timeout handler
1741 * would re-initiate a reset
1743 cmd->cmd_done = pmcraid_ioa_reset;
1744 cmd->timer.data = (unsigned long)cmd;
1745 cmd->timer.expires = jiffies +
1746 msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
1747 cmd->timer.function = (void (*)(unsigned long))pmcraid_timeout_handler;
1749 if (!timer_pending(&cmd->timer))
1750 add_timer(&cmd->timer);
1752 /* Enable destructive diagnostics on IOA if it is not yet in
1755 doorbell = DOORBELL_RUNTIME_RESET |
1756 DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
1758 iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
1759 int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
1760 pmcraid_info("Waiting for IOA to become operational %x:%x\n",
1761 ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
1766 * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
1768 * @pinstance: pointer to adapter instance structure
1773 static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
1775 pmcraid_info("%s is not yet implemented\n", __func__);
1779 * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
1780 * @pinstance: pointer to adapter instance structure
1782 * This function fails all outstanding ops. If they are submitted to IOA
1783 * already, it sends cancel all messages if IOA is still accepting IOARCBs,
1784 * otherwise just completes the commands and returns the cmd blocks to free
1790 static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
1792 struct pmcraid_cmd *cmd, *temp;
1793 unsigned long lock_flags;
1795 /* pending command list is protected by pending_pool_lock. Its
1796 * traversal must be done as within this lock
1798 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
1799 list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
1801 list_del(&cmd->free_list);
1802 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
1804 cmd->ioa_cb->ioasa.ioasc =
1805 cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
1806 cmd->ioa_cb->ioasa.ilid =
1807 cpu_to_be32(PMCRAID_DRIVER_ILID);
1809 /* In case the command timer is still running */
1810 del_timer(&cmd->timer);
1812 /* If this is an IO command, complete it by invoking scsi_done
1813 * function. If this is one of the internal commands other
1814 * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
1817 if (cmd->scsi_cmd) {
1819 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
1820 __le32 resp = cmd->ioa_cb->ioarcb.response_handle;
1822 scsi_cmd->result |= DID_ERROR << 16;
1824 scsi_dma_unmap(scsi_cmd);
1825 pmcraid_return_cmd(cmd);
1827 pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
1828 le32_to_cpu(resp) >> 2,
1829 cmd->ioa_cb->ioarcb.cdb[0],
1831 scsi_cmd->scsi_done(scsi_cmd);
1832 } else if (cmd->cmd_done == pmcraid_internal_done ||
1833 cmd->cmd_done == pmcraid_erp_done) {
1835 } else if (cmd->cmd_done != pmcraid_ioa_reset) {
1836 pmcraid_return_cmd(cmd);
1839 atomic_dec(&pinstance->outstanding_cmds);
1840 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
1843 spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
1847 * pmcraid_ioa_reset - Implementation of IOA reset logic
1849 * @cmd: pointer to the cmd block to be used for entire reset process
1851 * This function executes most of the steps required for IOA reset. This gets
1852 * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
1853 * 'eh_' thread. Access to variables used for controling the reset sequence is
1854 * synchronized using host lock. Various functions called during reset process
1855 * would make use of a single command block, pointer to which is also stored in
1856 * adapter instance structure.
1861 static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
1863 struct pmcraid_instance *pinstance = cmd->drv_inst;
1864 u8 reset_complete = 0;
1866 pinstance->ioa_reset_in_progress = 1;
1868 if (pinstance->reset_cmd != cmd) {
1869 pmcraid_err("reset is called with different command block\n");
1870 pinstance->reset_cmd = cmd;
1873 pmcraid_info("reset_engine: state = %d, command = %p\n",
1874 pinstance->ioa_state, cmd);
1876 switch (pinstance->ioa_state) {
1878 case IOA_STATE_DEAD:
1879 /* If IOA is offline, whatever may be the reset reason, just
1880 * return. callers might be waiting on the reset wait_q, wake
1883 pmcraid_err("IOA is offline no reset is possible\n");
1887 case IOA_STATE_IN_BRINGDOWN:
1888 /* we enter here, once ioa shutdown command is processed by IOA
1889 * Alert IOA for a possible reset. If reset alert fails, IOA
1890 * goes through hard-reset
1892 pmcraid_disable_interrupts(pinstance, ~0);
1893 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1894 pmcraid_reset_alert(cmd);
1897 case IOA_STATE_UNKNOWN:
1898 /* We may be called during probe or resume. Some pre-processing
1899 * is required for prior to reset
1901 scsi_block_requests(pinstance->host);
1903 /* If asked to reset while IOA was processing responses or
1904 * there are any error responses then IOA may require
1907 if (pinstance->ioa_hard_reset == 0) {
1908 if (ioread32(pinstance->ioa_status) &
1909 INTRS_TRANSITION_TO_OPERATIONAL) {
1910 pmcraid_info("sticky bit set, bring-up\n");
1911 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
1912 pmcraid_reinit_cmdblk(cmd);
1913 pmcraid_identify_hrrq(cmd);
1915 pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
1916 pmcraid_soft_reset(cmd);
1919 /* Alert IOA of a possible reset and wait for critical
1920 * operation in progress bit to reset
1922 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1923 pmcraid_reset_alert(cmd);
1927 case IOA_STATE_IN_RESET_ALERT:
1928 /* If critical operation in progress bit is reset or wait gets
1929 * timed out, reset proceeds with starting BIST on the IOA.
1930 * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
1931 * they are 3 or more, reset engine marks IOA dead and returns
1933 pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
1934 pmcraid_start_bist(cmd);
1937 case IOA_STATE_IN_HARD_RESET:
1938 pinstance->ioa_reset_attempts++;
1940 /* retry reset if we haven't reached maximum allowed limit */
1941 if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
1942 pinstance->ioa_reset_attempts = 0;
1943 pmcraid_err("IOA didn't respond marking it as dead\n");
1944 pinstance->ioa_state = IOA_STATE_DEAD;
1949 /* Once either bist or pci reset is done, restore PCI config
1950 * space. If this fails, proceed with hard reset again
1953 if (pci_restore_state(pinstance->pdev)) {
1954 pmcraid_info("config-space error resetting again\n");
1955 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1956 pmcraid_reset_alert(cmd);
1960 /* fail all pending commands */
1961 pmcraid_fail_outstanding_cmds(pinstance);
1963 /* check if unit check is active, if so extract dump */
1964 if (pinstance->ioa_unit_check) {
1965 pmcraid_info("unit check is active\n");
1966 pinstance->ioa_unit_check = 0;
1967 pmcraid_get_dump(pinstance);
1968 pinstance->ioa_reset_attempts--;
1969 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1970 pmcraid_reset_alert(cmd);
1974 /* if the reset reason is to bring-down the ioa, we might be
1975 * done with the reset restore pci_config_space and complete
1978 if (pinstance->ioa_bringdown) {
1979 pmcraid_info("bringing down the adapter\n");
1980 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
1981 pinstance->ioa_bringdown = 0;
1982 pinstance->ioa_state = IOA_STATE_UNKNOWN;
1985 /* bring-up IOA, so proceed with soft reset
1986 * Reinitialize hrrq_buffers and their indices also
1987 * enable interrupts after a pci_restore_state
1989 if (pmcraid_reset_enable_ioa(pinstance)) {
1990 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
1991 pmcraid_info("bringing up the adapter\n");
1992 pmcraid_reinit_cmdblk(cmd);
1993 pmcraid_identify_hrrq(cmd);
1995 pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
1996 pmcraid_soft_reset(cmd);
2001 case IOA_STATE_IN_SOFT_RESET:
2002 /* TRANSITION TO OPERATIONAL is on so start initialization
2005 pmcraid_info("In softreset proceeding with bring-up\n");
2006 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
2008 /* Initialization commands start with HRRQ identification. From
2009 * now on tasklet completes most of the commands as IOA is up
2010 * and intrs are enabled
2012 pmcraid_identify_hrrq(cmd);
2015 case IOA_STATE_IN_BRINGUP:
2016 /* we are done with bringing up of IOA, change the ioa_state to
2017 * operational and wake up any waiters
2019 pinstance->ioa_state = IOA_STATE_OPERATIONAL;
2023 case IOA_STATE_OPERATIONAL:
2025 /* When IOA is operational and a reset is requested, check for
2026 * the reset reason. If reset is to bring down IOA, unregister
2027 * HCAMs and initiate shutdown; if adapter reset is forced then
2028 * restart reset sequence again
2030 if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
2031 pinstance->force_ioa_reset == 0) {
2034 if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
2035 pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
2036 pmcraid_reinit_cmdblk(cmd);
2037 pmcraid_unregister_hcams(cmd);
2042 /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
2043 * OPERATIONAL. Reset all control variables used during reset, wake up
2044 * any waiting threads and let the SCSI mid-layer send commands. Note
2045 * that host_lock must be held before invoking scsi_report_bus_reset.
2047 if (reset_complete) {
2048 pinstance->ioa_reset_in_progress = 0;
2049 pinstance->ioa_reset_attempts = 0;
2050 pinstance->reset_cmd = NULL;
2051 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2052 pinstance->ioa_bringdown = 0;
2053 pmcraid_return_cmd(cmd);
2055 /* If target state is to bring up the adapter, proceed with
2056 * hcam registration and resource exposure to mid-layer.
2058 if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
2059 pmcraid_register_hcams(pinstance);
2061 wake_up_all(&pinstance->reset_wait_q);
2068 * pmcraid_initiate_reset - initiates reset sequence. This is called from
2069 * ISR/tasklet during error interrupts including IOA unit check. If reset
2070 * is already in progress, it just returns, otherwise initiates IOA reset
2071 * to bring IOA up to operational state.
2073 * @pinstance: pointer to adapter instance structure
2078 static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
2080 struct pmcraid_cmd *cmd;
2082 /* If the reset is already in progress, just return, otherwise start
2083 * reset sequence and return
2085 if (!pinstance->ioa_reset_in_progress) {
2086 scsi_block_requests(pinstance->host);
2087 cmd = pmcraid_get_free_cmd(pinstance);
2090 pmcraid_err("no cmnd blocks for initiate_reset\n");
2094 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2095 pinstance->reset_cmd = cmd;
2096 pinstance->force_ioa_reset = 1;
2097 pmcraid_ioa_reset(cmd);
2102 * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
2104 * @pinstance: pointer adapter instance structure
2105 * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
2106 * @target_state: expected target state after reset
2108 * Note: This command initiates reset and waits for its completion. Hence this
2109 * should not be called from isr/timer/tasklet functions (timeout handlers,
2110 * error response handlers and interrupt handlers).
2113 * 1 in case ioa_state is not target_state, 0 otherwise.
2115 static int pmcraid_reset_reload(
2116 struct pmcraid_instance *pinstance,
2121 struct pmcraid_cmd *reset_cmd = NULL;
2122 unsigned long lock_flags;
2125 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2127 if (pinstance->ioa_reset_in_progress) {
2128 pmcraid_info("reset_reload: reset is already in progress\n");
2130 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2132 wait_event(pinstance->reset_wait_q,
2133 !pinstance->ioa_reset_in_progress);
2135 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2137 if (pinstance->ioa_state == IOA_STATE_DEAD) {
2138 spin_unlock_irqrestore(pinstance->host->host_lock,
2140 pmcraid_info("reset_reload: IOA is dead\n");
2142 } else if (pinstance->ioa_state == target_state) {
2148 pmcraid_info("reset_reload: proceeding with reset\n");
2149 scsi_block_requests(pinstance->host);
2150 reset_cmd = pmcraid_get_free_cmd(pinstance);
2152 if (reset_cmd == NULL) {
2153 pmcraid_err("no free cmnd for reset_reload\n");
2154 spin_unlock_irqrestore(pinstance->host->host_lock,
2159 if (shutdown_type == SHUTDOWN_NORMAL)
2160 pinstance->ioa_bringdown = 1;
2162 pinstance->ioa_shutdown_type = shutdown_type;
2163 pinstance->reset_cmd = reset_cmd;
2164 pinstance->force_ioa_reset = reset;
2165 pmcraid_info("reset_reload: initiating reset\n");
2166 pmcraid_ioa_reset(reset_cmd);
2167 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2168 pmcraid_info("reset_reload: waiting for reset to complete\n");
2169 wait_event(pinstance->reset_wait_q,
2170 !pinstance->ioa_reset_in_progress);
2172 pmcraid_info("reset_reload: reset is complete !! \n");
2173 scsi_unblock_requests(pinstance->host);
2174 if (pinstance->ioa_state == target_state)
2182 * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
2184 * @pinstance: pointer to adapter instance structure
2187 * whatever is returned from pmcraid_reset_reload
2189 static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
2191 return pmcraid_reset_reload(pinstance,
2197 * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
2199 * @pinstance: pointer to adapter instance structure
2202 * whatever is returned from pmcraid_reset_reload
2204 static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
2206 return pmcraid_reset_reload(pinstance,
2208 IOA_STATE_OPERATIONAL);
2212 * pmcraid_request_sense - Send request sense to a device
2213 * @cmd: pmcraid command struct
2215 * This function sends a request sense to a device as a result of a check
2216 * condition. This method re-uses the same command block that failed earlier.
2218 static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
2220 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2221 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
2223 /* allocate DMAable memory for sense buffers */
2224 cmd->sense_buffer = pci_alloc_consistent(cmd->drv_inst->pdev,
2225 SCSI_SENSE_BUFFERSIZE,
2226 &cmd->sense_buffer_dma);
2228 if (cmd->sense_buffer == NULL) {
2230 ("couldn't allocate sense buffer for request sense\n");
2231 pmcraid_erp_done(cmd);
2235 /* re-use the command block */
2236 memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
2237 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
2238 ioarcb->request_flags0 = (SYNC_COMPLETE |
2241 ioarcb->request_type = REQ_TYPE_SCSI;
2242 ioarcb->cdb[0] = REQUEST_SENSE;
2243 ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2245 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
2246 offsetof(struct pmcraid_ioarcb,
2247 add_data.u.ioadl[0]));
2248 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
2250 ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
2252 ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
2253 ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
2254 ioadl->flags = IOADL_FLAGS_LAST_DESC;
2256 /* request sense might be called as part of error response processing
2257 * which runs in tasklets context. It is possible that mid-layer might
2258 * schedule queuecommand during this time, hence, writting to IOARRIN
2259 * must be protect by host_lock
2261 pmcraid_send_cmd(cmd, pmcraid_erp_done,
2262 PMCRAID_REQUEST_SENSE_TIMEOUT,
2263 pmcraid_timeout_handler);
2267 * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
2268 * @cmd: command that failed
2269 * @sense: true if request_sense is required after cancel all
2271 * This function sends a cancel all to a device to clear the queue.
2273 static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, u32 sense)
2275 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2276 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2277 struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
2278 void (*cmd_done) (struct pmcraid_cmd *) = sense ? pmcraid_erp_done
2279 : pmcraid_request_sense;
2281 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
2282 ioarcb->request_flags0 = SYNC_OVERRIDE;
2283 ioarcb->request_type = REQ_TYPE_IOACMD;
2284 ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
2286 if (RES_IS_GSCSI(res->cfg_entry))
2287 ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
2289 ioarcb->ioadl_bus_addr = 0;
2290 ioarcb->ioadl_length = 0;
2291 ioarcb->data_transfer_length = 0;
2292 ioarcb->ioarcb_bus_addr &= (~0x1FULL);
2294 /* writing to IOARRIN must be protected by host_lock, as mid-layer
2295 * schedule queuecommand while we are doing this
2297 pmcraid_send_cmd(cmd, cmd_done,
2298 PMCRAID_REQUEST_SENSE_TIMEOUT,
2299 pmcraid_timeout_handler);
2303 * pmcraid_frame_auto_sense: frame fixed format sense information
2305 * @cmd: pointer to failing command block
2310 static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
2312 u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
2313 struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
2314 struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
2315 u32 ioasc = le32_to_cpu(ioasa->ioasc);
2316 u32 failing_lba = 0;
2318 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
2319 cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
2321 if (RES_IS_VSET(res->cfg_entry) &&
2322 ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
2323 ioasa->u.vset.failing_lba_hi != 0) {
2325 sense_buf[0] = 0x72;
2326 sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
2327 sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
2328 sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
2332 sense_buf[9] = 0x0A;
2333 sense_buf[10] = 0x80;
2335 failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
2337 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
2338 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
2339 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
2340 sense_buf[15] = failing_lba & 0x000000ff;
2342 failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
2344 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
2345 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
2346 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
2347 sense_buf[19] = failing_lba & 0x000000ff;
2349 sense_buf[0] = 0x70;
2350 sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
2351 sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
2352 sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
2354 if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
2355 if (RES_IS_VSET(res->cfg_entry))
2357 le32_to_cpu(ioasa->u.
2358 vset.failing_lba_lo);
2359 sense_buf[0] |= 0x80;
2360 sense_buf[3] = (failing_lba >> 24) & 0xff;
2361 sense_buf[4] = (failing_lba >> 16) & 0xff;
2362 sense_buf[5] = (failing_lba >> 8) & 0xff;
2363 sense_buf[6] = failing_lba & 0xff;
2366 sense_buf[7] = 6; /* additional length */
2371 * pmcraid_error_handler - Error response handlers for a SCSI op
2372 * @cmd: pointer to pmcraid_cmd that has failed
2374 * This function determines whether or not to initiate ERP on the affected
2375 * device. This is called from a tasklet, which doesn't hold any locks.
2378 * 0 it caller can complete the request, otherwise 1 where in error
2379 * handler itself completes the request and returns the command block
2382 static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
2384 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2385 struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
2386 struct pmcraid_instance *pinstance = cmd->drv_inst;
2387 struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
2388 u32 ioasc = le32_to_cpu(ioasa->ioasc);
2389 u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
2390 u32 sense_copied = 0;
2393 pmcraid_info("resource pointer is NULL\n");
2397 /* If this was a SCSI read/write command keep count of errors */
2398 if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
2399 atomic_inc(&res->read_failures);
2400 else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
2401 atomic_inc(&res->write_failures);
2403 if (!RES_IS_GSCSI(res->cfg_entry) &&
2404 masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
2405 pmcraid_frame_auto_sense(cmd);
2408 /* Log IOASC/IOASA information based on user settings */
2409 pmcraid_ioasc_logger(ioasc, cmd);
2411 switch (masked_ioasc) {
2413 case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
2414 scsi_cmd->result |= (DID_ABORT << 16);
2417 case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
2418 case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
2419 scsi_cmd->result |= (DID_NO_CONNECT << 16);
2422 case PMCRAID_IOASC_NR_SYNC_REQUIRED:
2424 scsi_cmd->result |= (DID_IMM_RETRY << 16);
2427 case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
2428 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
2431 case PMCRAID_IOASC_UA_BUS_WAS_RESET:
2432 case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
2433 if (!res->reset_progress)
2434 scsi_report_bus_reset(pinstance->host,
2435 scsi_cmd->device->channel);
2436 scsi_cmd->result |= (DID_ERROR << 16);
2439 case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
2440 scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
2443 /* if check_condition is not active return with error otherwise
2444 * get/frame the sense buffer
2446 if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
2447 SAM_STAT_CHECK_CONDITION &&
2448 PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
2451 /* If we have auto sense data as part of IOASA pass it to
2454 if (ioasa->auto_sense_length != 0) {
2455 short sense_len = ioasa->auto_sense_length;
2456 int data_size = min_t(u16, le16_to_cpu(sense_len),
2457 SCSI_SENSE_BUFFERSIZE);
2459 memcpy(scsi_cmd->sense_buffer,
2465 if (RES_IS_GSCSI(res->cfg_entry)) {
2466 pmcraid_cancel_all(cmd, sense_copied);
2467 } else if (sense_copied) {
2468 pmcraid_erp_done(cmd);
2471 pmcraid_request_sense(cmd);
2476 case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
2480 if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
2481 scsi_cmd->result |= (DID_ERROR << 16);
2488 * pmcraid_reset_device - device reset handler functions
2490 * @scsi_cmd: scsi command struct
2491 * @modifier: reset modifier indicating the reset sequence to be performed
2493 * This function issues a device reset to the affected device.
2494 * A LUN reset will be sent to the device first. If that does
2495 * not work, a target reset will be sent.
2500 static int pmcraid_reset_device(
2501 struct scsi_cmnd *scsi_cmd,
2502 unsigned long timeout,
2506 struct pmcraid_cmd *cmd;
2507 struct pmcraid_instance *pinstance;
2508 struct pmcraid_resource_entry *res;
2509 struct pmcraid_ioarcb *ioarcb;
2510 unsigned long lock_flags;
2514 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
2515 res = scsi_cmd->device->hostdata;
2518 sdev_printk(KERN_ERR, scsi_cmd->device,
2519 "reset_device: NULL resource pointer\n");
2523 /* If adapter is currently going through reset/reload, return failed.
2524 * This will force the mid-layer to call _eh_bus/host reset, which
2525 * will then go to sleep and wait for the reset to complete
2527 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2528 if (pinstance->ioa_reset_in_progress ||
2529 pinstance->ioa_state == IOA_STATE_DEAD) {
2530 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2534 res->reset_progress = 1;
2535 pmcraid_info("Resetting %s resource with addr %x\n",
2536 ((modifier & RESET_DEVICE_LUN) ? "LUN" :
2537 ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
2538 le32_to_cpu(res->cfg_entry.resource_address));
2540 /* get a free cmd block */
2541 cmd = pmcraid_get_free_cmd(pinstance);
2544 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2545 pmcraid_err("%s: no cmd blocks are available\n", __func__);
2549 ioarcb = &cmd->ioa_cb->ioarcb;
2550 ioarcb->resource_handle = res->cfg_entry.resource_handle;
2551 ioarcb->request_type = REQ_TYPE_IOACMD;
2552 ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
2554 /* Initialize reset modifier bits */
2556 modifier = ENABLE_RESET_MODIFIER | modifier;
2558 ioarcb->cdb[1] = modifier;
2560 init_completion(&cmd->wait_for_completion);
2561 cmd->completion_req = 1;
2563 pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
2564 cmd->ioa_cb->ioarcb.cdb[0],
2565 le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
2566 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
2568 pmcraid_send_cmd(cmd,
2569 pmcraid_internal_done,
2571 pmcraid_timeout_handler);
2573 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2575 /* RESET_DEVICE command completes after all pending IOARCBs are
2576 * completed. Once this command is completed, pmcraind_internal_done
2577 * will wake up the 'completion' queue.
2579 wait_for_completion(&cmd->wait_for_completion);
2581 /* complete the command here itself and return the command block
2584 pmcraid_return_cmd(cmd);
2585 res->reset_progress = 0;
2586 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
2588 /* set the return value based on the returned ioasc */
2589 return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
2593 * _pmcraid_io_done - helper for pmcraid_io_done function
2595 * @cmd: pointer to pmcraid command struct
2596 * @reslen: residual data length to be set in the ioasa
2597 * @ioasc: ioasc either returned by IOA or set by driver itself.
2599 * This function is invoked by pmcraid_io_done to complete mid-layer
2603 * 0 if caller is required to return it to free_pool. Returns 1 if
2604 * caller need not worry about freeing command block as error handler
2605 * will take care of that.
2608 static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
2610 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2613 scsi_set_resid(scsi_cmd, reslen);
2615 pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
2616 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
2617 cmd->ioa_cb->ioarcb.cdb[0],
2618 ioasc, scsi_cmd->result);
2620 if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
2621 rc = pmcraid_error_handler(cmd);
2624 scsi_dma_unmap(scsi_cmd);
2625 scsi_cmd->scsi_done(scsi_cmd);
2632 * pmcraid_io_done - SCSI completion function
2634 * @cmd: pointer to pmcraid command struct
2636 * This function is invoked by tasklet/mid-layer error handler to completing
2637 * the SCSI ops sent from mid-layer.
2643 static void pmcraid_io_done(struct pmcraid_cmd *cmd)
2645 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
2646 u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
2648 if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
2649 pmcraid_return_cmd(cmd);
2653 * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
2655 * @cmd: command block of the command to be aborted
2658 * returns pointer to command structure used as cancelling cmd
2660 static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
2662 struct pmcraid_cmd *cancel_cmd;
2663 struct pmcraid_instance *pinstance;
2664 struct pmcraid_resource_entry *res;
2666 pinstance = (struct pmcraid_instance *)cmd->drv_inst;
2667 res = cmd->scsi_cmd->device->hostdata;
2669 cancel_cmd = pmcraid_get_free_cmd(pinstance);
2671 if (cancel_cmd == NULL) {
2672 pmcraid_err("%s: no cmd blocks are available\n", __func__);
2676 pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
2678 pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
2679 cmd->ioa_cb->ioarcb.cdb[0],
2680 cmd->ioa_cb->ioarcb.response_handle >> 2);
2682 init_completion(&cancel_cmd->wait_for_completion);
2683 cancel_cmd->completion_req = 1;
2685 pmcraid_info("command (%d) CDB[0] = %x for %x\n",
2686 le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
2687 cmd->ioa_cb->ioarcb.cdb[0],
2688 le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
2690 pmcraid_send_cmd(cancel_cmd,
2691 pmcraid_internal_done,
2692 PMCRAID_INTERNAL_TIMEOUT,
2693 pmcraid_timeout_handler);
2698 * pmcraid_abort_complete - Waits for ABORT TASK completion
2700 * @cancel_cmd: command block use as cancelling command
2703 * returns SUCCESS if ABORT TASK has good completion
2706 static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
2708 struct pmcraid_resource_entry *res;
2711 wait_for_completion(&cancel_cmd->wait_for_completion);
2712 res = cancel_cmd->u.res;
2713 cancel_cmd->u.res = NULL;
2714 ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
2716 /* If the abort task is not timed out we will get a Good completion
2717 * as sense_key, otherwise we may get one the following responses
2718 * due to subsquent bus reset or device reset. In case IOASC is
2719 * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
2721 if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
2722 ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
2723 if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
2728 /* complete the command here itself */
2729 pmcraid_return_cmd(cancel_cmd);
2730 return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
2734 * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
2736 * @scsi_cmd: scsi command struct given by mid-layer. When this is called
2737 * mid-layer ensures that no other commands are queued. This
2738 * never gets called under interrupt, but a separate eh thread.
2743 static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
2745 struct pmcraid_instance *pinstance;
2746 struct pmcraid_cmd *cmd;
2747 struct pmcraid_resource_entry *res;
2748 unsigned long host_lock_flags;
2749 unsigned long pending_lock_flags;
2750 struct pmcraid_cmd *cancel_cmd = NULL;
2755 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
2757 scmd_printk(KERN_INFO, scsi_cmd,
2758 "I/O command timed out, aborting it.\n");
2760 res = scsi_cmd->device->hostdata;
2765 /* If we are currently going through reset/reload, return failed.
2766 * This will force the mid-layer to eventually call
2767 * pmcraid_eh_host_reset which will then go to sleep and wait for the
2770 spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
2772 if (pinstance->ioa_reset_in_progress ||
2773 pinstance->ioa_state == IOA_STATE_DEAD) {
2774 spin_unlock_irqrestore(pinstance->host->host_lock,
2779 /* loop over pending cmd list to find cmd corresponding to this
2780 * scsi_cmd. Note that this command might not have been completed
2781 * already. locking: all pending commands are protected with
2782 * pending_pool_lock.
2784 spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
2785 list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
2787 if (cmd->scsi_cmd == scsi_cmd) {
2793 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
2794 pending_lock_flags);
2796 /* If the command to be aborted was given to IOA and still pending with
2797 * it, send ABORT_TASK to abort this and wait for its completion
2800 cancel_cmd = pmcraid_abort_cmd(cmd);
2802 spin_unlock_irqrestore(pinstance->host->host_lock,
2806 cancel_cmd->u.res = cmd->scsi_cmd->device->hostdata;
2807 rc = pmcraid_abort_complete(cancel_cmd);
2810 return cmd_found ? rc : SUCCESS;
2814 * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
2816 * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
2818 * All these routines invokve pmcraid_reset_device with appropriate parameters.
2819 * Since these are called from mid-layer EH thread, no other IO will be queued
2820 * to the resource being reset. However, control path (IOCTL) may be active so
2821 * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
2822 * takes care by locking/unlocking host_lock.
2827 static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
2829 scmd_printk(KERN_INFO, scmd,
2830 "resetting device due to an I/O command timeout.\n");
2831 return pmcraid_reset_device(scmd,
2832 PMCRAID_INTERNAL_TIMEOUT,
2836 static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
2838 scmd_printk(KERN_INFO, scmd,
2839 "Doing bus reset due to an I/O command timeout.\n");
2840 return pmcraid_reset_device(scmd,
2841 PMCRAID_RESET_BUS_TIMEOUT,
2845 static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
2847 scmd_printk(KERN_INFO, scmd,
2848 "Doing target reset due to an I/O command timeout.\n");
2849 return pmcraid_reset_device(scmd,
2850 PMCRAID_INTERNAL_TIMEOUT,
2851 RESET_DEVICE_TARGET);
2855 * pmcraid_eh_host_reset_handler - adapter reset handler callback
2857 * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
2859 * Initiates adapter reset to bring it up to operational state
2864 static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
2866 unsigned long interval = 10000; /* 10 seconds interval */
2867 int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
2868 struct pmcraid_instance *pinstance =
2869 (struct pmcraid_instance *)(scmd->device->host->hostdata);
2872 /* wait for an additional 150 seconds just in case firmware could come
2873 * up and if it could complete all the pending commands excluding the
2874 * two HCAM (CCN and LDN).
2877 if (atomic_read(&pinstance->outstanding_cmds) <=
2878 PMCRAID_MAX_HCAM_CMD)
2883 dev_err(&pinstance->pdev->dev,
2884 "Adapter being reset due to an I/O command timeout.\n");
2885 return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
2889 * pmcraid_task_attributes - Translate SPI Q-Tags to task attributes
2890 * @scsi_cmd: scsi command struct
2893 * number of tags or 0 if the task is not tagged
2895 static u8 pmcraid_task_attributes(struct scsi_cmnd *scsi_cmd)
2900 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
2902 case MSG_SIMPLE_TAG:
2903 rc = TASK_TAG_SIMPLE;
2906 rc = TASK_TAG_QUEUE_HEAD;
2908 case MSG_ORDERED_TAG:
2909 rc = TASK_TAG_ORDERED;
2919 * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
2920 * @cmd: pmcraid command struct
2921 * @sgcount: count of scatter-gather elements
2924 * returns pointer pmcraid_ioadl_desc, initialized to point to internal
2925 * or external IOADLs
2927 struct pmcraid_ioadl_desc *
2928 pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
2930 struct pmcraid_ioadl_desc *ioadl;
2931 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2932 int ioadl_count = 0;
2934 if (ioarcb->add_cmd_param_length)
2935 ioadl_count = DIV_ROUND_UP(ioarcb->add_cmd_param_length, 16);
2936 ioarcb->ioadl_length =
2937 sizeof(struct pmcraid_ioadl_desc) * sgcount;
2939 if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
2940 /* external ioadls start at offset 0x80 from control_block
2941 * structure, re-using 24 out of 27 ioadls part of IOARCB.
2942 * It is necessary to indicate to firmware that driver is
2943 * using ioadls to be treated as external to IOARCB.
2945 ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
2946 ioarcb->ioadl_bus_addr =
2947 cpu_to_le64((cmd->ioa_cb_bus_addr) +
2948 offsetof(struct pmcraid_ioarcb,
2949 add_data.u.ioadl[3]));
2950 ioadl = &ioarcb->add_data.u.ioadl[3];
2952 ioarcb->ioadl_bus_addr =
2953 cpu_to_le64((cmd->ioa_cb_bus_addr) +
2954 offsetof(struct pmcraid_ioarcb,
2955 add_data.u.ioadl[ioadl_count]));
2957 ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
2958 ioarcb->ioarcb_bus_addr |=
2959 DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8);
2966 * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
2967 * @pinstance: pointer to adapter instance structure
2968 * @cmd: pmcraid command struct
2970 * This function is invoked by queuecommand entry point while sending a command
2971 * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
2974 * 0 on success or -1 on failure
2976 static int pmcraid_build_ioadl(
2977 struct pmcraid_instance *pinstance,
2978 struct pmcraid_cmd *cmd
2982 struct scatterlist *sglist;
2984 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2985 struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
2986 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
2988 u32 length = scsi_bufflen(scsi_cmd);
2993 nseg = scsi_dma_map(scsi_cmd);
2996 scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
2998 } else if (nseg > PMCRAID_MAX_IOADLS) {
2999 scsi_dma_unmap(scsi_cmd);
3000 scmd_printk(KERN_ERR, scsi_cmd,
3001 "sg count is (%d) more than allowed!\n", nseg);
3005 /* Initialize IOARCB data transfer length fields */
3006 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
3007 ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
3009 ioarcb->request_flags0 |= NO_LINK_DESCS;
3010 ioarcb->data_transfer_length = cpu_to_le32(length);
3011 ioadl = pmcraid_init_ioadls(cmd, nseg);
3013 /* Initialize IOADL descriptor addresses */
3014 scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
3015 ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
3016 ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
3019 /* setup last descriptor */
3020 ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
3026 * pmcraid_free_sglist - Frees an allocated SG buffer list
3027 * @sglist: scatter/gather list pointer
3029 * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
3034 static void pmcraid_free_sglist(struct pmcraid_sglist *sglist)
3038 for (i = 0; i < sglist->num_sg; i++)
3039 __free_pages(sg_page(&(sglist->scatterlist[i])),
3046 * pmcraid_alloc_sglist - Allocates memory for a SG list
3047 * @buflen: buffer length
3049 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3053 * pointer to sglist / NULL on failure
3055 static struct pmcraid_sglist *pmcraid_alloc_sglist(int buflen)
3057 struct pmcraid_sglist *sglist;
3058 struct scatterlist *scatterlist;
3065 sg_size = buflen / (PMCRAID_MAX_IOADLS - 1);
3066 order = (sg_size > 0) ? get_order(sg_size) : 0;
3067 bsize_elem = PAGE_SIZE * (1 << order);
3069 /* Determine the actual number of sg entries needed */
3070 if (buflen % bsize_elem)
3071 num_elem = (buflen / bsize_elem) + 1;
3073 num_elem = buflen / bsize_elem;
3075 /* Allocate a scatter/gather list for the DMA */
3076 sglist = kzalloc(sizeof(struct pmcraid_sglist) +
3077 (sizeof(struct scatterlist) * (num_elem - 1)),
3083 scatterlist = sglist->scatterlist;
3084 sg_init_table(scatterlist, num_elem);
3085 sglist->order = order;
3086 sglist->num_sg = num_elem;
3089 for (i = 0; i < num_elem; i++) {
3090 page = alloc_pages(GFP_KERNEL|GFP_DMA, order);
3092 for (j = i - 1; j >= 0; j--)
3093 __free_pages(sg_page(&scatterlist[j]), order);
3098 sg_set_page(&scatterlist[i], page,
3099 sg_size < bsize_elem ? sg_size : bsize_elem, 0);
3100 sg_size -= bsize_elem;
3107 * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
3108 * @sglist: scatter/gather list pointer
3109 * @buffer: buffer pointer
3110 * @len: buffer length
3111 * @direction: data transfer direction
3113 * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
3116 * 0 on success / other on failure
3118 static int pmcraid_copy_sglist(
3119 struct pmcraid_sglist *sglist,
3120 unsigned long buffer,
3125 struct scatterlist *scatterlist;
3131 /* Determine the actual number of bytes per element */
3132 bsize_elem = PAGE_SIZE * (1 << sglist->order);
3134 scatterlist = sglist->scatterlist;
3136 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
3137 struct page *page = sg_page(&scatterlist[i]);
3140 if (direction == DMA_TO_DEVICE)
3141 rc = __copy_from_user(kaddr,
3145 rc = __copy_to_user((void *)buffer, kaddr, bsize_elem);
3150 pmcraid_err("failed to copy user data into sg list\n");
3154 scatterlist[i].length = bsize_elem;
3157 if (len % bsize_elem) {
3158 struct page *page = sg_page(&scatterlist[i]);
3162 if (direction == DMA_TO_DEVICE)
3163 rc = __copy_from_user(kaddr,
3167 rc = __copy_to_user((void *)buffer,
3173 scatterlist[i].length = len % bsize_elem;
3177 pmcraid_err("failed to copy user data into sg list\n");
3185 * pmcraid_queuecommand - Queue a mid-layer request
3186 * @scsi_cmd: scsi command struct
3187 * @done: done function
3189 * This function queues a request generated by the mid-layer. Midlayer calls
3190 * this routine within host->lock. Some of the functions called by queuecommand
3191 * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
3195 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3196 * SCSI_MLQUEUE_HOST_BUSY if host is busy
3198 static int pmcraid_queuecommand(
3199 struct scsi_cmnd *scsi_cmd,
3200 void (*done) (struct scsi_cmnd *)
3203 struct pmcraid_instance *pinstance;
3204 struct pmcraid_resource_entry *res;
3205 struct pmcraid_ioarcb *ioarcb;
3206 struct pmcraid_cmd *cmd;
3210 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
3212 scsi_cmd->scsi_done = done;
3213 res = scsi_cmd->device->hostdata;
3214 scsi_cmd->result = (DID_OK << 16);
3216 /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
3219 if (pinstance->ioa_state == IOA_STATE_DEAD) {
3220 pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
3221 scsi_cmd->result = (DID_NO_CONNECT << 16);
3222 scsi_cmd->scsi_done(scsi_cmd);
3226 /* If IOA reset is in progress, can't queue the commands */
3227 if (pinstance->ioa_reset_in_progress)
3228 return SCSI_MLQUEUE_HOST_BUSY;
3230 /* initialize the command and IOARCB to be sent to IOA */
3231 cmd = pmcraid_get_free_cmd(pinstance);
3234 pmcraid_err("free command block is not available\n");
3235 return SCSI_MLQUEUE_HOST_BUSY;
3238 cmd->scsi_cmd = scsi_cmd;
3239 ioarcb = &(cmd->ioa_cb->ioarcb);
3240 memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
3241 ioarcb->resource_handle = res->cfg_entry.resource_handle;
3242 ioarcb->request_type = REQ_TYPE_SCSI;
3244 cmd->cmd_done = pmcraid_io_done;
3246 if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
3247 if (scsi_cmd->underflow == 0)
3248 ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
3250 if (res->sync_reqd) {
3251 ioarcb->request_flags0 |= SYNC_COMPLETE;
3255 ioarcb->request_flags0 |= NO_LINK_DESCS;
3256 ioarcb->request_flags1 |= pmcraid_task_attributes(scsi_cmd);
3258 if (RES_IS_GSCSI(res->cfg_entry))
3259 ioarcb->request_flags1 |= DELAY_AFTER_RESET;
3262 rc = pmcraid_build_ioadl(pinstance, cmd);
3264 pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
3265 le32_to_cpu(ioarcb->response_handle) >> 2,
3266 scsi_cmd->cmnd[0], pinstance->host->unique_id,
3267 RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
3268 PMCRAID_PHYS_BUS_ID,
3269 RES_IS_VSET(res->cfg_entry) ?
3270 res->cfg_entry.unique_flags1 :
3271 RES_TARGET(res->cfg_entry.resource_address),
3272 RES_LUN(res->cfg_entry.resource_address));
3274 if (likely(rc == 0)) {
3275 _pmcraid_fire_command(cmd);
3277 pmcraid_err("queuecommand could not build ioadl\n");
3278 pmcraid_return_cmd(cmd);
3279 rc = SCSI_MLQUEUE_HOST_BUSY;
3286 * pmcraid_open -char node "open" entry, allowed only users with admin access
3288 static int pmcraid_chr_open(struct inode *inode, struct file *filep)
3290 struct pmcraid_instance *pinstance;
3292 if (!capable(CAP_SYS_ADMIN))
3295 /* Populate adapter instance * pointer for use by ioctl */
3296 pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
3297 filep->private_data = pinstance;
3303 * pmcraid_release - char node "release" entry point
3305 static int pmcraid_chr_release(struct inode *inode, struct file *filep)
3307 struct pmcraid_instance *pinstance =
3308 ((struct pmcraid_instance *)filep->private_data);
3310 filep->private_data = NULL;
3311 fasync_helper(-1, filep, 0, &pinstance->aen_queue);
3317 * pmcraid_fasync - Async notifier registration from applications
3319 * This function adds the calling process to a driver global queue. When an
3320 * event occurs, SIGIO will be sent to all processes in this queue.
3322 static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
3324 struct pmcraid_instance *pinstance;
3327 pinstance = (struct pmcraid_instance *)filep->private_data;
3328 mutex_lock(&pinstance->aen_queue_lock);
3329 rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
3330 mutex_unlock(&pinstance->aen_queue_lock);
3337 * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
3338 * commands sent over IOCTL interface
3340 * @cmd : pointer to struct pmcraid_cmd
3341 * @buflen : length of the request buffer
3342 * @direction : data transfer direction
3345 * 0 on sucess, non-zero error code on failure
3347 static int pmcraid_build_passthrough_ioadls(
3348 struct pmcraid_cmd *cmd,
3353 struct pmcraid_sglist *sglist = NULL;
3354 struct scatterlist *sg = NULL;
3355 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
3356 struct pmcraid_ioadl_desc *ioadl;
3359 sglist = pmcraid_alloc_sglist(buflen);
3362 pmcraid_err("can't allocate memory for passthrough SGls\n");
3366 sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
3367 sglist->scatterlist,
3368 sglist->num_sg, direction);
3370 if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
3371 dev_err(&cmd->drv_inst->pdev->dev,
3372 "Failed to map passthrough buffer!\n");
3373 pmcraid_free_sglist(sglist);
3377 cmd->sglist = sglist;
3378 ioarcb->request_flags0 |= NO_LINK_DESCS;
3380 ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
3382 /* Initialize IOADL descriptor addresses */
3383 for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
3384 ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
3385 ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
3389 /* setup the last descriptor */
3390 ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
3397 * pmcraid_release_passthrough_ioadls - release passthrough ioadls
3399 * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
3400 * @buflen: size of the request buffer
3401 * @direction: data transfer direction
3404 * 0 on sucess, non-zero error code on failure
3406 static void pmcraid_release_passthrough_ioadls(
3407 struct pmcraid_cmd *cmd,
3412 struct pmcraid_sglist *sglist = cmd->sglist;
3415 pci_unmap_sg(cmd->drv_inst->pdev,
3416 sglist->scatterlist,
3419 pmcraid_free_sglist(sglist);
3425 * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
3427 * @pinstance: pointer to adapter instance structure
3429 * @arg: pointer to pmcraid_passthrough_buffer user buffer
3432 * 0 on sucess, non-zero error code on failure
3434 static long pmcraid_ioctl_passthrough(
3435 struct pmcraid_instance *pinstance,
3436 unsigned int ioctl_cmd,
3437 unsigned int buflen,
3441 struct pmcraid_passthrough_ioctl_buffer *buffer;
3442 struct pmcraid_ioarcb *ioarcb;
3443 struct pmcraid_cmd *cmd;
3444 struct pmcraid_cmd *cancel_cmd;
3445 unsigned long request_buffer;
3446 unsigned long request_offset;
3447 unsigned long lock_flags;
3450 u8 access, direction;
3453 /* If IOA reset is in progress, wait 10 secs for reset to complete */
3454 if (pinstance->ioa_reset_in_progress) {
3455 rc = wait_event_interruptible_timeout(
3456 pinstance->reset_wait_q,
3457 !pinstance->ioa_reset_in_progress,
3458 msecs_to_jiffies(10000));
3463 return -ERESTARTSYS;
3466 /* If adapter is not in operational state, return error */
3467 if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
3468 pmcraid_err("IOA is not operational\n");
3472 buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
3473 buffer = kmalloc(buffer_size, GFP_KERNEL);
3476 pmcraid_err("no memory for passthrough buffer\n");
3481 offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
3483 request_buffer = arg + request_offset;
3485 rc = __copy_from_user(buffer,
3486 (struct pmcraid_passthrough_ioctl_buffer *) arg,
3487 sizeof(struct pmcraid_passthrough_ioctl_buffer));
3489 pmcraid_err("ioctl: can't copy passthrough buffer\n");
3491 goto out_free_buffer;
3494 request_size = buffer->ioarcb.data_transfer_length;
3496 if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
3497 access = VERIFY_READ;
3498 direction = DMA_TO_DEVICE;
3500 access = VERIFY_WRITE;
3501 direction = DMA_FROM_DEVICE;
3504 if (request_size > 0) {
3505 rc = access_ok(access, arg, request_offset + request_size);
3509 goto out_free_buffer;
3511 } else if (request_size < 0) {
3513 goto out_free_buffer;
3516 /* check if we have any additional command parameters */
3517 if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
3519 goto out_free_buffer;
3522 cmd = pmcraid_get_free_cmd(pinstance);
3525 pmcraid_err("free command block is not available\n");
3527 goto out_free_buffer;
3530 cmd->scsi_cmd = NULL;
3531 ioarcb = &(cmd->ioa_cb->ioarcb);
3533 /* Copy the user-provided IOARCB stuff field by field */
3534 ioarcb->resource_handle = buffer->ioarcb.resource_handle;
3535 ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
3536 ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
3537 ioarcb->request_type = buffer->ioarcb.request_type;
3538 ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
3539 ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
3540 memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
3542 if (buffer->ioarcb.add_cmd_param_length) {
3543 ioarcb->add_cmd_param_length =
3544 buffer->ioarcb.add_cmd_param_length;
3545 ioarcb->add_cmd_param_offset =
3546 buffer->ioarcb.add_cmd_param_offset;
3547 memcpy(ioarcb->add_data.u.add_cmd_params,
3548 buffer->ioarcb.add_data.u.add_cmd_params,
3549 buffer->ioarcb.add_cmd_param_length);
3553 rc = pmcraid_build_passthrough_ioadls(cmd,
3557 pmcraid_err("couldn't build passthrough ioadls\n");
3558 goto out_free_buffer;
3562 /* If data is being written into the device, copy the data from user
3565 if (direction == DMA_TO_DEVICE && request_size > 0) {
3566 rc = pmcraid_copy_sglist(cmd->sglist,
3571 pmcraid_err("failed to copy user buffer\n");
3572 goto out_free_sglist;
3576 /* passthrough ioctl is a blocking command so, put the user to sleep
3577 * until timeout. Note that a timeout value of 0 means, do timeout.
3579 cmd->cmd_done = pmcraid_internal_done;
3580 init_completion(&cmd->wait_for_completion);
3581 cmd->completion_req = 1;
3583 pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
3584 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
3585 cmd->ioa_cb->ioarcb.cdb[0],
3586 le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
3588 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3589 _pmcraid_fire_command(cmd);
3590 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3592 /* If command timeout is specified put caller to wait till that time,
3593 * otherwise it would be blocking wait. If command gets timed out, it
3596 if (buffer->ioarcb.cmd_timeout == 0) {
3597 wait_for_completion(&cmd->wait_for_completion);
3598 } else if (!wait_for_completion_timeout(
3599 &cmd->wait_for_completion,
3600 msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
3602 pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
3603 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
3604 cmd->ioa_cb->ioarcb.cdb[0]);
3607 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3608 cancel_cmd = pmcraid_abort_cmd(cmd);
3609 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3612 wait_for_completion(&cancel_cmd->wait_for_completion);
3613 pmcraid_return_cmd(cancel_cmd);
3616 goto out_free_sglist;
3619 /* If the command failed for any reason, copy entire IOASA buffer and
3620 * return IOCTL success. If copying IOASA to user-buffer fails, return
3623 if (le32_to_cpu(cmd->ioa_cb->ioasa.ioasc)) {
3627 offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
3629 pmcraid_info("command failed with %x\n",
3630 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
3631 if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
3632 sizeof(struct pmcraid_ioasa))) {
3633 pmcraid_err("failed to copy ioasa buffer to user\n");
3637 /* If the data transfer was from device, copy the data onto user
3640 else if (direction == DMA_FROM_DEVICE && request_size > 0) {
3641 rc = pmcraid_copy_sglist(cmd->sglist,
3646 pmcraid_err("failed to copy user buffer\n");
3652 pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
3653 pmcraid_return_cmd(cmd);
3665 * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
3667 * @pinstance: pointer to adapter instance structure
3668 * @cmd: ioctl command passed in
3669 * @buflen: length of user_buffer
3670 * @user_buffer: user buffer pointer
3673 * 0 in case of success, otherwise appropriate error code
3675 static long pmcraid_ioctl_driver(
3676 struct pmcraid_instance *pinstance,
3678 unsigned int buflen,
3679 void __user *user_buffer
3684 if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
3685 pmcraid_err("ioctl_driver: access fault in request buffer \n");
3690 case PMCRAID_IOCTL_RESET_ADAPTER:
3691 pmcraid_reset_bringup(pinstance);
3703 * pmcraid_check_ioctl_buffer - check for proper access to user buffer
3705 * @cmd: ioctl command
3707 * @hdr: pointer to kernel memory for pmcraid_ioctl_header
3710 * negetive error code if there are access issues, otherwise zero.
3711 * Upon success, returns ioctl header copied out of user buffer.
3714 static int pmcraid_check_ioctl_buffer(
3717 struct pmcraid_ioctl_header *hdr
3721 int access = VERIFY_READ;
3723 if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
3724 pmcraid_err("couldn't copy ioctl header from user buffer\n");
3728 /* check for valid driver signature */
3729 rc = memcmp(hdr->signature,
3730 PMCRAID_IOCTL_SIGNATURE,
3731 sizeof(hdr->signature));
3733 pmcraid_err("signature verification failed\n");
3737 /* buffer length can't be negetive */
3738 if (hdr->buffer_length < 0) {
3739 pmcraid_err("ioctl: invalid buffer length specified\n");
3743 /* check for appropriate buffer access */
3744 if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
3745 access = VERIFY_WRITE;
3747 rc = access_ok(access,
3748 (arg + sizeof(struct pmcraid_ioctl_header)),
3749 hdr->buffer_length);
3751 pmcraid_err("access failed for user buffer of size %d\n",
3752 hdr->buffer_length);
3760 * pmcraid_ioctl - char node ioctl entry point
3762 static long pmcraid_chr_ioctl(
3768 struct pmcraid_instance *pinstance = NULL;
3769 struct pmcraid_ioctl_header *hdr = NULL;
3770 int retval = -ENOTTY;
3772 hdr = kmalloc(GFP_KERNEL, sizeof(struct pmcraid_ioctl_header));
3775 pmcraid_err("faile to allocate memory for ioctl header\n");
3779 retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
3782 pmcraid_info("chr_ioctl: header check failed\n");
3787 pinstance = (struct pmcraid_instance *)filep->private_data;
3790 pmcraid_info("adapter instance is not found\n");
3795 switch (_IOC_TYPE(cmd)) {
3797 case PMCRAID_PASSTHROUGH_IOCTL:
3798 /* If ioctl code is to download microcode, we need to block
3799 * mid-layer requests.
3801 if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
3802 scsi_block_requests(pinstance->host);
3804 retval = pmcraid_ioctl_passthrough(pinstance,
3809 if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
3810 scsi_unblock_requests(pinstance->host);
3813 case PMCRAID_DRIVER_IOCTL:
3814 arg += sizeof(struct pmcraid_ioctl_header);
3815 retval = pmcraid_ioctl_driver(pinstance,
3818 (void __user *)arg);
3832 * File operations structure for management interface
3834 static const struct file_operations pmcraid_fops = {
3835 .owner = THIS_MODULE,
3836 .open = pmcraid_chr_open,
3837 .release = pmcraid_chr_release,
3838 .fasync = pmcraid_chr_fasync,
3839 .unlocked_ioctl = pmcraid_chr_ioctl,
3840 #ifdef CONFIG_COMPAT
3841 .compat_ioctl = pmcraid_chr_ioctl,
3849 * pmcraid_show_log_level - Display adapter's error logging level
3850 * @dev: class device struct
3854 * number of bytes printed to buffer
3856 static ssize_t pmcraid_show_log_level(
3858 struct device_attribute *attr,
3861 struct Scsi_Host *shost = class_to_shost(dev);
3862 struct pmcraid_instance *pinstance =
3863 (struct pmcraid_instance *)shost->hostdata;
3864 return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
3868 * pmcraid_store_log_level - Change the adapter's error logging level
3869 * @dev: class device struct
3874 * number of bytes printed to buffer
3876 static ssize_t pmcraid_store_log_level(
3878 struct device_attribute *attr,
3883 struct Scsi_Host *shost;
3884 struct pmcraid_instance *pinstance;
3887 if (strict_strtoul(buf, 10, &val))
3889 /* log-level should be from 0 to 2 */
3893 shost = class_to_shost(dev);
3894 pinstance = (struct pmcraid_instance *)shost->hostdata;
3895 pinstance->current_log_level = val;
3900 static struct device_attribute pmcraid_log_level_attr = {
3902 .name = "log_level",
3903 .mode = S_IRUGO | S_IWUSR,
3905 .show = pmcraid_show_log_level,
3906 .store = pmcraid_store_log_level,
3910 * pmcraid_show_drv_version - Display driver version
3911 * @dev: class device struct
3915 * number of bytes printed to buffer
3917 static ssize_t pmcraid_show_drv_version(
3919 struct device_attribute *attr,
3923 return snprintf(buf, PAGE_SIZE, "version: %s, build date: %s\n",
3924 PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
3927 static struct device_attribute pmcraid_driver_version_attr = {
3929 .name = "drv_version",
3932 .show = pmcraid_show_drv_version,
3936 * pmcraid_show_io_adapter_id - Display driver assigned adapter id
3937 * @dev: class device struct
3941 * number of bytes printed to buffer
3943 static ssize_t pmcraid_show_adapter_id(
3945 struct device_attribute *attr,
3949 struct Scsi_Host *shost = class_to_shost(dev);
3950 struct pmcraid_instance *pinstance =
3951 (struct pmcraid_instance *)shost->hostdata;
3952 u32 adapter_id = (pinstance->pdev->bus->number << 8) |
3953 pinstance->pdev->devfn;
3954 u32 aen_group = pmcraid_event_family.id;
3956 return snprintf(buf, PAGE_SIZE,
3957 "adapter id: %d\nminor: %d\naen group: %d\n",
3958 adapter_id, MINOR(pinstance->cdev.dev), aen_group);
3961 static struct device_attribute pmcraid_adapter_id_attr = {
3963 .name = "adapter_id",
3964 .mode = S_IRUGO | S_IWUSR,
3966 .show = pmcraid_show_adapter_id,
3969 static struct device_attribute *pmcraid_host_attrs[] = {
3970 &pmcraid_log_level_attr,
3971 &pmcraid_driver_version_attr,
3972 &pmcraid_adapter_id_attr,
3977 /* host template structure for pmcraid driver */
3978 static struct scsi_host_template pmcraid_host_template = {
3979 .module = THIS_MODULE,
3980 .name = PMCRAID_DRIVER_NAME,
3981 .queuecommand = pmcraid_queuecommand,
3982 .eh_abort_handler = pmcraid_eh_abort_handler,
3983 .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
3984 .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
3985 .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
3986 .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
3988 .slave_alloc = pmcraid_slave_alloc,
3989 .slave_configure = pmcraid_slave_configure,
3990 .slave_destroy = pmcraid_slave_destroy,
3991 .change_queue_depth = pmcraid_change_queue_depth,
3992 .change_queue_type = pmcraid_change_queue_type,
3993 .can_queue = PMCRAID_MAX_IO_CMD,
3995 .sg_tablesize = PMCRAID_MAX_IOADLS,
3996 .max_sectors = PMCRAID_IOA_MAX_SECTORS,
3997 .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
3998 .use_clustering = ENABLE_CLUSTERING,
3999 .shost_attrs = pmcraid_host_attrs,
4000 .proc_name = PMCRAID_DRIVER_NAME
4004 * pmcraid_isr_common - Common interrupt handler routine
4006 * @pinstance: pointer to adapter instance
4007 * @intrs: active interrupts (contents of ioa_host_interrupt register)
4008 * @hrrq_id: Host RRQ index
4013 static void pmcraid_isr_common(
4014 struct pmcraid_instance *pinstance,
4020 (intrs & INTRS_CRITICAL_OP_IN_PROGRESS) ? intrs
4022 iowrite32(intrs_clear,
4023 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4024 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
4026 /* hrrq valid bit was set, schedule tasklet to handle the response */
4027 if (intrs_clear == INTRS_HRRQ_VALID)
4028 tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
4032 * pmcraid_isr - implements interrupt handling routine
4034 * @irq: interrupt vector number
4035 * @dev_id: pointer hrrq_vector
4038 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4040 static irqreturn_t pmcraid_isr(int irq, void *dev_id)
4042 struct pmcraid_isr_param *hrrq_vector;
4043 struct pmcraid_instance *pinstance;
4044 unsigned long lock_flags;
4047 /* In case of legacy interrupt mode where interrupts are shared across
4048 * isrs, it may be possible that the current interrupt is not from IOA
4051 printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
4055 hrrq_vector = (struct pmcraid_isr_param *)dev_id;
4056 pinstance = hrrq_vector->drv_inst;
4058 /* Acquire the lock (currently host_lock) while processing interrupts.
4059 * This interval is small as most of the response processing is done by
4060 * tasklet without the lock.
4062 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
4063 intrs = pmcraid_read_interrupts(pinstance);
4065 if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0)) {
4066 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4070 /* Any error interrupts including unit_check, initiate IOA reset.
4071 * In case of unit check indicate to reset_sequence that IOA unit
4072 * checked and prepare for a dump during reset sequence
4074 if (intrs & PMCRAID_ERROR_INTERRUPTS) {
4076 if (intrs & INTRS_IOA_UNIT_CHECK)
4077 pinstance->ioa_unit_check = 1;
4080 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4081 pmcraid_err("ISR: error interrupts: %x initiating reset\n",
4083 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
4084 pmcraid_initiate_reset(pinstance);
4086 pmcraid_isr_common(pinstance, intrs, hrrq_vector->hrrq_id);
4089 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4096 * pmcraid_worker_function - worker thread function
4098 * @workp: pointer to struct work queue
4104 static void pmcraid_worker_function(struct work_struct *workp)
4106 struct pmcraid_instance *pinstance;
4107 struct pmcraid_resource_entry *res;
4108 struct pmcraid_resource_entry *temp;
4109 struct scsi_device *sdev;
4110 unsigned long lock_flags;
4111 unsigned long host_lock_flags;
4112 u8 bus, target, lun;
4114 pinstance = container_of(workp, struct pmcraid_instance, worker_q);
4115 /* add resources only after host is added into system */
4116 if (!atomic_read(&pinstance->expose_resources))
4119 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
4120 list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
4122 if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
4123 sdev = res->scsi_dev;
4125 /* host_lock must be held before calling
4128 spin_lock_irqsave(pinstance->host->host_lock,
4130 if (!scsi_device_get(sdev)) {
4131 spin_unlock_irqrestore(
4132 pinstance->host->host_lock,
4134 pmcraid_info("deleting %x from midlayer\n",
4135 res->cfg_entry.resource_address);
4136 list_move_tail(&res->queue,
4137 &pinstance->free_res_q);
4138 spin_unlock_irqrestore(
4139 &pinstance->resource_lock,
4141 scsi_remove_device(sdev);
4142 scsi_device_put(sdev);
4143 spin_lock_irqsave(&pinstance->resource_lock,
4145 res->change_detected = 0;
4147 spin_unlock_irqrestore(
4148 pinstance->host->host_lock,
4154 list_for_each_entry(res, &pinstance->used_res_q, queue) {
4156 if (res->change_detected == RES_CHANGE_ADD) {
4158 if (!pmcraid_expose_resource(&res->cfg_entry))
4161 if (RES_IS_VSET(res->cfg_entry)) {
4162 bus = PMCRAID_VSET_BUS_ID;
4163 target = res->cfg_entry.unique_flags1;
4164 lun = PMCRAID_VSET_LUN_ID;
4166 bus = PMCRAID_PHYS_BUS_ID;
4169 res->cfg_entry.resource_address);
4170 lun = RES_LUN(res->cfg_entry.resource_address);
4173 res->change_detected = 0;
4174 spin_unlock_irqrestore(&pinstance->resource_lock,
4176 scsi_add_device(pinstance->host, bus, target, lun);
4177 spin_lock_irqsave(&pinstance->resource_lock,
4182 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
4186 * pmcraid_tasklet_function - Tasklet function
4188 * @instance: pointer to msix param structure
4193 void pmcraid_tasklet_function(unsigned long instance)
4195 struct pmcraid_isr_param *hrrq_vector;
4196 struct pmcraid_instance *pinstance;
4197 unsigned long hrrq_lock_flags;
4198 unsigned long pending_lock_flags;
4199 unsigned long host_lock_flags;
4200 spinlock_t *lockp; /* hrrq buffer lock */
4205 hrrq_vector = (struct pmcraid_isr_param *)instance;
4206 pinstance = hrrq_vector->drv_inst;
4207 id = hrrq_vector->hrrq_id;
4208 lockp = &(pinstance->hrrq_lock[id]);
4209 intrs = pmcraid_read_interrupts(pinstance);
4211 /* If interrupts was as part of the ioa initialization, clear and mask
4212 * it. Delete the timer and wakeup the reset engine to proceed with
4215 if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
4216 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
4217 pinstance->int_regs.ioa_host_interrupt_mask_reg);
4218 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
4219 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4221 if (pinstance->reset_cmd != NULL) {
4222 del_timer(&pinstance->reset_cmd->timer);
4223 spin_lock_irqsave(pinstance->host->host_lock,
4225 pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
4226 spin_unlock_irqrestore(pinstance->host->host_lock,
4232 /* loop through each of the commands responded by IOA. Each HRRQ buf is
4233 * protected by its own lock. Traversals must be done within this lock
4234 * as there may be multiple tasklets running on multiple CPUs. Note
4235 * that the lock is held just for picking up the response handle and
4236 * manipulating hrrq_curr/toggle_bit values.
4238 spin_lock_irqsave(lockp, hrrq_lock_flags);
4240 resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4242 while ((resp & HRRQ_TOGGLE_BIT) ==
4243 pinstance->host_toggle_bit[id]) {
4245 int cmd_index = resp >> 2;
4246 struct pmcraid_cmd *cmd = NULL;
4248 if (cmd_index < PMCRAID_MAX_CMD) {
4249 cmd = pinstance->cmd_list[cmd_index];
4251 /* In case of invalid response handle, initiate IOA
4254 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4256 pmcraid_err("Invalid response %d initiating reset\n",
4259 spin_lock_irqsave(pinstance->host->host_lock,
4261 pmcraid_initiate_reset(pinstance);
4262 spin_unlock_irqrestore(pinstance->host->host_lock,
4265 spin_lock_irqsave(lockp, hrrq_lock_flags);
4269 if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
4270 pinstance->hrrq_curr[id]++;
4272 pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
4273 pinstance->host_toggle_bit[id] ^= 1u;
4276 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4278 spin_lock_irqsave(&pinstance->pending_pool_lock,
4279 pending_lock_flags);
4280 list_del(&cmd->free_list);
4281 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
4282 pending_lock_flags);
4283 del_timer(&cmd->timer);
4284 atomic_dec(&pinstance->outstanding_cmds);
4286 if (cmd->cmd_done == pmcraid_ioa_reset) {
4287 spin_lock_irqsave(pinstance->host->host_lock,
4290 spin_unlock_irqrestore(pinstance->host->host_lock,
4292 } else if (cmd->cmd_done != NULL) {
4295 /* loop over until we are done with all responses */
4296 spin_lock_irqsave(lockp, hrrq_lock_flags);
4297 resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4300 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4304 * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
4305 * @pinstance: pointer to adapter instance structure
4307 * This routine un-registers registered interrupt handler and
4308 * also frees irqs/vectors.
4314 void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
4316 free_irq(pinstance->pdev->irq, &(pinstance->hrrq_vector[0]));
4320 * pmcraid_register_interrupt_handler - registers interrupt handler
4321 * @pinstance: pointer to per-adapter instance structure
4324 * 0 on success, non-zero error code otherwise.
4327 pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
4329 struct pci_dev *pdev = pinstance->pdev;
4331 pinstance->hrrq_vector[0].hrrq_id = 0;
4332 pinstance->hrrq_vector[0].drv_inst = pinstance;
4333 pinstance->hrrq_vector[0].vector = 0;
4334 pinstance->num_hrrq = 1;
4335 return request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
4336 PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
4340 * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
4341 * @pinstance: per adapter instance structure pointer
4342 * @max_index: number of buffer blocks to release
4348 pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
4351 for (i = 0; i < max_index; i++) {
4352 kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
4353 pinstance->cmd_list[i] = NULL;
4355 kmem_cache_destroy(pinstance->cmd_cachep);
4356 pinstance->cmd_cachep = NULL;
4360 * pmcraid_release_control_blocks - releases buffers alloced for control blocks
4361 * @pinstance: pointer to per adapter instance structure
4362 * @max_index: number of buffers (from 0 onwards) to release
4364 * This function assumes that the command blocks for which control blocks are
4365 * linked are not released.
4371 pmcraid_release_control_blocks(
4372 struct pmcraid_instance *pinstance,
4378 if (pinstance->control_pool == NULL)
4381 for (i = 0; i < max_index; i++) {
4382 pci_pool_free(pinstance->control_pool,
4383 pinstance->cmd_list[i]->ioa_cb,
4384 pinstance->cmd_list[i]->ioa_cb_bus_addr);
4385 pinstance->cmd_list[i]->ioa_cb = NULL;
4386 pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
4388 pci_pool_destroy(pinstance->control_pool);
4389 pinstance->control_pool = NULL;
4393 * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
4394 * @pinstance - pointer to per adapter instance structure
4396 * Allocates memory for command blocks using kernel slab allocator.
4399 * 0 in case of success; -ENOMEM in case of failure
4401 static int __devinit
4402 pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
4406 sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
4407 pinstance->host->unique_id);
4410 pinstance->cmd_cachep = kmem_cache_create(
4411 pinstance->cmd_pool_name,
4412 sizeof(struct pmcraid_cmd), 0,
4413 SLAB_HWCACHE_ALIGN, NULL);
4414 if (!pinstance->cmd_cachep)
4417 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4418 pinstance->cmd_list[i] =
4419 kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
4420 if (!pinstance->cmd_list[i]) {
4421 pmcraid_release_cmd_blocks(pinstance, i);
4429 * pmcraid_allocate_control_blocks - allocates memory control blocks
4430 * @pinstance : pointer to per adapter instance structure
4432 * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
4433 * and IOASAs. This is called after command blocks are already allocated.
4436 * 0 in case it can allocate all control blocks, otherwise -ENOMEM
4438 static int __devinit
4439 pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
4443 sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
4444 pinstance->host->unique_id);
4446 pinstance->control_pool =
4447 pci_pool_create(pinstance->ctl_pool_name,
4449 sizeof(struct pmcraid_control_block),
4450 PMCRAID_IOARCB_ALIGNMENT, 0);
4452 if (!pinstance->control_pool)
4455 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4456 pinstance->cmd_list[i]->ioa_cb =
4458 pinstance->control_pool,
4460 &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
4462 if (!pinstance->cmd_list[i]->ioa_cb) {
4463 pmcraid_release_control_blocks(pinstance, i);
4466 memset(pinstance->cmd_list[i]->ioa_cb, 0,
4467 sizeof(struct pmcraid_control_block));
4473 * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
4474 * @pinstance: pointer to per adapter instance structure
4475 * @maxindex: size of hrrq buffer pointer array
4481 pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
4484 for (i = 0; i < maxindex; i++) {
4486 pci_free_consistent(pinstance->pdev,
4487 HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
4488 pinstance->hrrq_start[i],
4489 pinstance->hrrq_start_bus_addr[i]);
4491 /* reset pointers and toggle bit to zeros */
4492 pinstance->hrrq_start[i] = NULL;
4493 pinstance->hrrq_start_bus_addr[i] = 0;
4494 pinstance->host_toggle_bit[i] = 0;
4499 * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
4500 * @pinstance: pointer to per adapter instance structure
4503 * 0 hrrq buffers are allocated, -ENOMEM otherwise.
4505 static int __devinit
4506 pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
4509 int buf_count = PMCRAID_MAX_CMD / pinstance->num_hrrq;
4511 for (i = 0; i < pinstance->num_hrrq; i++) {
4512 int buffer_size = HRRQ_ENTRY_SIZE * buf_count;
4514 pinstance->hrrq_start[i] =
4515 pci_alloc_consistent(
4518 &(pinstance->hrrq_start_bus_addr[i]));
4520 if (pinstance->hrrq_start[i] == 0) {
4521 pmcraid_err("could not allocate host rrq: %d\n", i);
4522 pmcraid_release_host_rrqs(pinstance, i);
4526 memset(pinstance->hrrq_start[i], 0, buffer_size);
4527 pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4528 pinstance->hrrq_end[i] =
4529 pinstance->hrrq_start[i] + buf_count - 1;
4530 pinstance->host_toggle_bit[i] = 1;
4531 spin_lock_init(&pinstance->hrrq_lock[i]);
4537 * pmcraid_release_hcams - release HCAM buffers
4539 * @pinstance: pointer to per adapter instance structure
4544 static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
4546 if (pinstance->ccn.msg != NULL) {
4547 pci_free_consistent(pinstance->pdev,
4548 PMCRAID_AEN_HDR_SIZE +
4549 sizeof(struct pmcraid_hcam_ccn),
4551 pinstance->ccn.baddr);
4553 pinstance->ccn.msg = NULL;
4554 pinstance->ccn.hcam = NULL;
4555 pinstance->ccn.baddr = 0;
4558 if (pinstance->ldn.msg != NULL) {
4559 pci_free_consistent(pinstance->pdev,
4560 PMCRAID_AEN_HDR_SIZE +
4561 sizeof(struct pmcraid_hcam_ldn),
4563 pinstance->ldn.baddr);
4565 pinstance->ldn.msg = NULL;
4566 pinstance->ldn.hcam = NULL;
4567 pinstance->ldn.baddr = 0;
4572 * pmcraid_allocate_hcams - allocates HCAM buffers
4573 * @pinstance : pointer to per adapter instance structure
4576 * 0 in case of successful allocation, non-zero otherwise
4578 static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
4580 pinstance->ccn.msg = pci_alloc_consistent(
4582 PMCRAID_AEN_HDR_SIZE +
4583 sizeof(struct pmcraid_hcam_ccn),
4584 &(pinstance->ccn.baddr));
4586 pinstance->ldn.msg = pci_alloc_consistent(
4588 PMCRAID_AEN_HDR_SIZE +
4589 sizeof(struct pmcraid_hcam_ldn),
4590 &(pinstance->ldn.baddr));
4592 if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
4593 pmcraid_release_hcams(pinstance);
4595 pinstance->ccn.hcam =
4596 (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
4597 pinstance->ldn.hcam =
4598 (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
4600 atomic_set(&pinstance->ccn.ignore, 0);
4601 atomic_set(&pinstance->ldn.ignore, 0);
4604 return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
4608 * pmcraid_release_config_buffers - release config.table buffers
4609 * @pinstance: pointer to per adapter instance structure
4614 static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
4616 if (pinstance->cfg_table != NULL &&
4617 pinstance->cfg_table_bus_addr != 0) {
4618 pci_free_consistent(pinstance->pdev,
4619 sizeof(struct pmcraid_config_table),
4620 pinstance->cfg_table,
4621 pinstance->cfg_table_bus_addr);
4622 pinstance->cfg_table = NULL;
4623 pinstance->cfg_table_bus_addr = 0;
4626 if (pinstance->res_entries != NULL) {
4629 for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4630 list_del(&pinstance->res_entries[i].queue);
4631 kfree(pinstance->res_entries);
4632 pinstance->res_entries = NULL;
4635 pmcraid_release_hcams(pinstance);
4639 * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
4640 * @pinstance : pointer to per adapter instance structure
4643 * 0 for successful allocation, -ENOMEM for any failure
4645 static int __devinit
4646 pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
4650 pinstance->res_entries =
4651 kzalloc(sizeof(struct pmcraid_resource_entry) *
4652 PMCRAID_MAX_RESOURCES, GFP_KERNEL);
4654 if (NULL == pinstance->res_entries) {
4655 pmcraid_err("failed to allocate memory for resource table\n");
4659 for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4660 list_add_tail(&pinstance->res_entries[i].queue,
4661 &pinstance->free_res_q);
4663 pinstance->cfg_table =
4664 pci_alloc_consistent(pinstance->pdev,
4665 sizeof(struct pmcraid_config_table),
4666 &pinstance->cfg_table_bus_addr);
4668 if (NULL == pinstance->cfg_table) {
4669 pmcraid_err("couldn't alloc DMA memory for config table\n");
4670 pmcraid_release_config_buffers(pinstance);
4674 if (pmcraid_allocate_hcams(pinstance)) {
4675 pmcraid_err("could not alloc DMA memory for HCAMS\n");
4676 pmcraid_release_config_buffers(pinstance);
4684 * pmcraid_init_tasklets - registers tasklets for response handling
4686 * @pinstance: pointer adapter instance structure
4691 static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
4694 for (i = 0; i < pinstance->num_hrrq; i++)
4695 tasklet_init(&pinstance->isr_tasklet[i],
4696 pmcraid_tasklet_function,
4697 (unsigned long)&pinstance->hrrq_vector[i]);
4701 * pmcraid_kill_tasklets - destroys tasklets registered for response handling
4703 * @pinstance: pointer to adapter instance structure
4708 static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
4711 for (i = 0; i < pinstance->num_hrrq; i++)
4712 tasklet_kill(&pinstance->isr_tasklet[i]);
4716 * pmcraid_init_buffers - allocates memory and initializes various structures
4717 * @pinstance: pointer to per adapter instance structure
4719 * This routine pre-allocates memory based on the type of block as below:
4720 * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
4721 * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
4722 * config-table entries : DMAable memory using pci_alloc_consistent
4723 * HostRRQs : DMAable memory, using pci_alloc_consistent
4726 * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
4728 static int __devinit pmcraid_init_buffers(struct pmcraid_instance *pinstance)
4732 if (pmcraid_allocate_host_rrqs(pinstance)) {
4733 pmcraid_err("couldn't allocate memory for %d host rrqs\n",
4734 pinstance->num_hrrq);
4738 if (pmcraid_allocate_config_buffers(pinstance)) {
4739 pmcraid_err("couldn't allocate memory for config buffers\n");
4740 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4744 if (pmcraid_allocate_cmd_blocks(pinstance)) {
4745 pmcraid_err("couldn't allocate memory for cmd blocks \n");
4746 pmcraid_release_config_buffers(pinstance);
4747 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4751 if (pmcraid_allocate_control_blocks(pinstance)) {
4752 pmcraid_err("couldn't allocate memory control blocks \n");
4753 pmcraid_release_config_buffers(pinstance);
4754 pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
4755 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4759 /* Initialize all the command blocks and add them to free pool. No
4760 * need to lock (free_pool_lock) as this is done in initialization
4763 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4764 struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
4765 pmcraid_init_cmdblk(cmdp, i);
4766 cmdp->drv_inst = pinstance;
4767 list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
4774 * pmcraid_reinit_buffers - resets various buffer pointers
4775 * @pinstance: pointer to adapter instance
4779 static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
4782 int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
4784 for (i = 0; i < pinstance->num_hrrq; i++) {
4785 memset(pinstance->hrrq_start[i], 0, buffer_size);
4786 pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4787 pinstance->hrrq_end[i] =
4788 pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
4789 pinstance->host_toggle_bit[i] = 1;
4794 * pmcraid_init_instance - initialize per instance data structure
4795 * @pdev: pointer to pci device structure
4796 * @host: pointer to Scsi_Host structure
4797 * @mapped_pci_addr: memory mapped IOA configuration registers
4800 * 0 on success, non-zero in case of any failure
4802 static int __devinit pmcraid_init_instance(
4803 struct pci_dev *pdev,
4804 struct Scsi_Host *host,
4805 void __iomem *mapped_pci_addr
4808 struct pmcraid_instance *pinstance =
4809 (struct pmcraid_instance *)host->hostdata;
4811 pinstance->host = host;
4812 pinstance->pdev = pdev;
4814 /* Initialize register addresses */
4815 pinstance->mapped_dma_addr = mapped_pci_addr;
4817 /* Initialize chip-specific details */
4819 struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
4820 struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
4822 pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
4824 pint_regs->ioa_host_interrupt_reg =
4825 mapped_pci_addr + chip_cfg->ioa_host_intr;
4826 pint_regs->ioa_host_interrupt_clr_reg =
4827 mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
4828 pint_regs->host_ioa_interrupt_reg =
4829 mapped_pci_addr + chip_cfg->host_ioa_intr;
4830 pint_regs->host_ioa_interrupt_clr_reg =
4831 mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
4833 /* Current version of firmware exposes interrupt mask set
4834 * and mask clr registers through memory mapped bar0.
4836 pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
4837 pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
4838 pint_regs->ioa_host_interrupt_mask_reg =
4839 mapped_pci_addr + chip_cfg->ioa_host_mask;
4840 pint_regs->ioa_host_interrupt_mask_clr_reg =
4841 mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
4842 pint_regs->global_interrupt_mask_reg =
4843 mapped_pci_addr + chip_cfg->global_intr_mask;
4846 pinstance->ioa_reset_attempts = 0;
4847 init_waitqueue_head(&pinstance->reset_wait_q);
4849 atomic_set(&pinstance->outstanding_cmds, 0);
4850 atomic_set(&pinstance->expose_resources, 0);
4852 INIT_LIST_HEAD(&pinstance->free_res_q);
4853 INIT_LIST_HEAD(&pinstance->used_res_q);
4854 INIT_LIST_HEAD(&pinstance->free_cmd_pool);
4855 INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
4857 spin_lock_init(&pinstance->free_pool_lock);
4858 spin_lock_init(&pinstance->pending_pool_lock);
4859 spin_lock_init(&pinstance->resource_lock);
4860 mutex_init(&pinstance->aen_queue_lock);
4862 /* Work-queue (Shared) for deferred processing error handling */
4863 INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
4865 /* Initialize the default log_level */
4866 pinstance->current_log_level = pmcraid_log_level;
4868 /* Setup variables required for reset engine */
4869 pinstance->ioa_state = IOA_STATE_UNKNOWN;
4870 pinstance->reset_cmd = NULL;
4875 * pmcraid_release_buffers - release per-adapter buffers allocated
4877 * @pinstance: pointer to adapter soft state
4882 static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
4884 pmcraid_release_config_buffers(pinstance);
4885 pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
4886 pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
4887 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4892 * pmcraid_shutdown - shutdown adapter controller.
4893 * @pdev: pci device struct
4895 * Issues an adapter shutdown to the card waits for its completion
4900 static void pmcraid_shutdown(struct pci_dev *pdev)
4902 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
4903 pmcraid_reset_bringdown(pinstance);
4908 * pmcraid_get_minor - returns unused minor number from minor number bitmap
4910 static unsigned short pmcraid_get_minor(void)
4914 minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
4915 __set_bit(minor, pmcraid_minor);
4920 * pmcraid_release_minor - releases given minor back to minor number bitmap
4922 static void pmcraid_release_minor(unsigned short minor)
4924 __clear_bit(minor, pmcraid_minor);
4928 * pmcraid_setup_chrdev - allocates a minor number and registers a char device
4930 * @pinstance: pointer to adapter instance for which to register device
4933 * 0 in case of success, otherwise non-zero
4935 static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
4940 minor = pmcraid_get_minor();
4941 cdev_init(&pinstance->cdev, &pmcraid_fops);
4942 pinstance->cdev.owner = THIS_MODULE;
4944 error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
4947 pmcraid_release_minor(minor);
4949 device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
4950 NULL, "pmcsas%u", minor);
4955 * pmcraid_release_chrdev - unregisters per-adapter management interface
4957 * @pinstance: pointer to adapter instance structure
4962 static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
4964 pmcraid_release_minor(MINOR(pinstance->cdev.dev));
4965 device_destroy(pmcraid_class,
4966 MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
4967 cdev_del(&pinstance->cdev);
4971 * pmcraid_remove - IOA hot plug remove entry point
4972 * @pdev: pci device struct
4977 static void __devexit pmcraid_remove(struct pci_dev *pdev)
4979 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
4981 /* remove the management interface (/dev file) for this device */
4982 pmcraid_release_chrdev(pinstance);
4984 /* remove host template from scsi midlayer */
4985 scsi_remove_host(pinstance->host);
4987 /* block requests from mid-layer */
4988 scsi_block_requests(pinstance->host);
4990 /* initiate shutdown adapter */
4991 pmcraid_shutdown(pdev);
4993 pmcraid_disable_interrupts(pinstance, ~0);
4994 flush_scheduled_work();
4996 pmcraid_kill_tasklets(pinstance);
4997 pmcraid_unregister_interrupt_handler(pinstance);
4998 pmcraid_release_buffers(pinstance);
4999 iounmap(pinstance->mapped_dma_addr);
5000 pci_release_regions(pdev);
5001 scsi_host_put(pinstance->host);
5002 pci_disable_device(pdev);
5009 * pmcraid_suspend - driver suspend entry point for power management
5010 * @pdev: PCI device structure
5011 * @state: PCI power state to suspend routine
5013 * Return Value - 0 always
5015 static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
5017 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5019 pmcraid_shutdown(pdev);
5020 pmcraid_disable_interrupts(pinstance, ~0);
5021 pmcraid_kill_tasklets(pinstance);
5022 pci_set_drvdata(pinstance->pdev, pinstance);
5023 pmcraid_unregister_interrupt_handler(pinstance);
5024 pci_save_state(pdev);
5025 pci_disable_device(pdev);
5026 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5032 * pmcraid_resume - driver resume entry point PCI power management
5033 * @pdev: PCI device structure
5035 * Return Value - 0 in case of success. Error code in case of any failure
5037 static int pmcraid_resume(struct pci_dev *pdev)
5039 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5040 struct Scsi_Host *host = pinstance->host;
5044 pci_set_power_state(pdev, PCI_D0);
5045 pci_enable_wake(pdev, PCI_D0, 0);
5046 pci_restore_state(pdev);
5048 rc = pci_enable_device(pdev);
5051 dev_err(&pdev->dev, "resume: Enable device failed\n");
5055 pci_set_master(pdev);
5057 if ((sizeof(dma_addr_t) == 4) ||
5058 pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5059 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5062 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5065 dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
5066 goto disable_device;
5069 atomic_set(&pinstance->outstanding_cmds, 0);
5070 hrrqs = pinstance->num_hrrq;
5071 rc = pmcraid_register_interrupt_handler(pinstance);
5075 "resume: couldn't register interrupt handlers\n");
5080 pmcraid_init_tasklets(pinstance);
5081 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5083 /* Start with hard reset sequence which brings up IOA to operational
5084 * state as well as completes the reset sequence.
5086 pinstance->ioa_hard_reset = 1;
5088 /* Start IOA firmware initialization and bring card to Operational
5091 if (pmcraid_reset_bringup(pinstance)) {
5092 dev_err(&pdev->dev, "couldn't initialize IOA \n");
5094 goto release_tasklets;
5100 pmcraid_kill_tasklets(pinstance);
5101 pmcraid_unregister_interrupt_handler(pinstance);
5104 scsi_host_put(host);
5107 pci_disable_device(pdev);
5114 #define pmcraid_suspend NULL
5115 #define pmcraid_resume NULL
5117 #endif /* CONFIG_PM */
5120 * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
5121 * completion of the ioa reset
5122 * @cmd: pointer to reset command block
5124 static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
5126 struct pmcraid_instance *pinstance = cmd->drv_inst;
5127 unsigned long flags;
5129 spin_lock_irqsave(pinstance->host->host_lock, flags);
5130 pmcraid_ioa_reset(cmd);
5131 spin_unlock_irqrestore(pinstance->host->host_lock, flags);
5132 scsi_unblock_requests(pinstance->host);
5133 schedule_work(&pinstance->worker_q);
5137 * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
5139 * @cmd: pointer to pmcraid_cmd structure
5142 * 0 for success or non-zero for failure cases
5144 static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
5146 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5147 void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
5149 pmcraid_reinit_cmdblk(cmd);
5151 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5152 ioarcb->request_type = REQ_TYPE_IOACMD;
5153 ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
5154 ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
5156 /* If this was called as part of resource table reinitialization due to
5157 * lost CCN, it is enough to return the command block back to free pool
5158 * as part of set_supported_devs completion function.
5160 if (cmd->drv_inst->reinit_cfg_table) {
5161 cmd->drv_inst->reinit_cfg_table = 0;
5163 cmd_done = pmcraid_reinit_cfgtable_done;
5166 /* we will be done with the reset sequence after set supported devices,
5167 * setup the done function to return the command block back to free
5170 pmcraid_send_cmd(cmd,
5172 PMCRAID_SET_SUP_DEV_TIMEOUT,
5173 pmcraid_timeout_handler);
5178 * pmcraid_init_res_table - Initialize the resource table
5179 * @cmd: pointer to pmcraid command struct
5181 * This function looks through the existing resource table, comparing
5182 * it with the config table. This function will take care of old/new
5183 * devices and schedule adding/removing them from the mid-layer
5189 static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
5191 struct pmcraid_instance *pinstance = cmd->drv_inst;
5192 struct pmcraid_resource_entry *res, *temp;
5193 struct pmcraid_config_table_entry *cfgte;
5194 unsigned long lock_flags;
5198 if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
5199 pmcraid_err("IOA requires microcode download\n");
5201 /* resource list is protected by pinstance->resource_lock.
5202 * init_res_table can be called from probe (user-thread) or runtime
5203 * reset (timer/tasklet)
5205 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
5207 list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
5208 list_move_tail(&res->queue, &old_res);
5210 for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
5211 cfgte = &pinstance->cfg_table->entries[i];
5213 if (!pmcraid_expose_resource(cfgte))
5218 /* If this entry was already detected and initialized */
5219 list_for_each_entry_safe(res, temp, &old_res, queue) {
5221 rc = memcmp(&res->cfg_entry.resource_address,
5222 &cfgte->resource_address,
5223 sizeof(cfgte->resource_address));
5225 list_move_tail(&res->queue,
5226 &pinstance->used_res_q);
5232 /* If this is new entry, initialize it and add it the queue */
5235 if (list_empty(&pinstance->free_res_q)) {
5236 pmcraid_err("Too many devices attached\n");
5241 res = list_entry(pinstance->free_res_q.next,
5242 struct pmcraid_resource_entry, queue);
5244 res->scsi_dev = NULL;
5245 res->change_detected = RES_CHANGE_ADD;
5246 res->reset_progress = 0;
5247 list_move_tail(&res->queue, &pinstance->used_res_q);
5250 /* copy new configuration table entry details into driver
5251 * maintained resource entry
5254 memcpy(&res->cfg_entry, cfgte,
5255 sizeof(struct pmcraid_config_table_entry));
5256 pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
5257 res->cfg_entry.resource_type,
5258 res->cfg_entry.unique_flags1,
5259 le32_to_cpu(res->cfg_entry.resource_address));
5263 /* Detect any deleted entries, mark them for deletion from mid-layer */
5264 list_for_each_entry_safe(res, temp, &old_res, queue) {
5266 if (res->scsi_dev) {
5267 res->change_detected = RES_CHANGE_DEL;
5268 res->cfg_entry.resource_handle =
5269 PMCRAID_INVALID_RES_HANDLE;
5270 list_move_tail(&res->queue, &pinstance->used_res_q);
5272 list_move_tail(&res->queue, &pinstance->free_res_q);
5276 /* release the resource list lock */
5277 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
5278 pmcraid_set_supported_devs(cmd);
5282 * pmcraid_querycfg - Send a Query IOA Config to the adapter.
5283 * @cmd: pointer pmcraid_cmd struct
5285 * This function sends a Query IOA Configuration command to the adapter to
5286 * retrieve the IOA configuration table.
5291 static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
5293 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5294 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
5295 struct pmcraid_instance *pinstance = cmd->drv_inst;
5296 int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
5298 ioarcb->request_type = REQ_TYPE_IOACMD;
5299 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5301 ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
5303 /* firmware requires 4-byte length field, specified in B.E format */
5304 memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
5306 /* Since entire config table can be described by single IOADL, it can
5307 * be part of IOARCB itself
5309 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
5310 offsetof(struct pmcraid_ioarcb,
5311 add_data.u.ioadl[0]));
5312 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
5313 ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
5315 ioarcb->request_flags0 |= NO_LINK_DESCS;
5316 ioarcb->data_transfer_length =
5317 cpu_to_le32(sizeof(struct pmcraid_config_table));
5319 ioadl = &(ioarcb->add_data.u.ioadl[0]);
5320 ioadl->flags = IOADL_FLAGS_LAST_DESC;
5321 ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
5322 ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
5324 pmcraid_send_cmd(cmd, pmcraid_init_res_table,
5325 PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
5330 * pmcraid_probe - PCI probe entry pointer for PMC MaxRaid controller driver
5331 * @pdev: pointer to pci device structure
5332 * @dev_id: pointer to device ids structure
5335 * returns 0 if the device is claimed and successfully configured.
5336 * returns non-zero error code in case of any failure
5338 static int __devinit pmcraid_probe(
5339 struct pci_dev *pdev,
5340 const struct pci_device_id *dev_id
5343 struct pmcraid_instance *pinstance;
5344 struct Scsi_Host *host;
5345 void __iomem *mapped_pci_addr;
5346 int rc = PCIBIOS_SUCCESSFUL;
5348 if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
5350 ("maximum number(%d) of supported adapters reached\n",
5351 atomic_read(&pmcraid_adapter_count));
5355 atomic_inc(&pmcraid_adapter_count);
5356 rc = pci_enable_device(pdev);
5359 dev_err(&pdev->dev, "Cannot enable adapter\n");
5360 atomic_dec(&pmcraid_adapter_count);
5364 dev_info(&pdev->dev,
5365 "Found new IOA(%x:%x), Total IOA count: %d\n",
5366 pdev->vendor, pdev->device,
5367 atomic_read(&pmcraid_adapter_count));
5369 rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
5373 "Couldn't register memory range of registers\n");
5374 goto out_disable_device;
5377 mapped_pci_addr = pci_iomap(pdev, 0, 0);
5379 if (!mapped_pci_addr) {
5380 dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
5382 goto out_release_regions;
5385 pci_set_master(pdev);
5387 /* Firmware requires the system bus address of IOARCB to be within
5388 * 32-bit addressable range though it has 64-bit IOARRIN register.
5389 * However, firmware supports 64-bit streaming DMA buffers, whereas
5390 * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
5391 * returns memory within 4GB (if not, change this logic), coherent
5392 * buffers are within firmware acceptible address ranges.
5394 if ((sizeof(dma_addr_t) == 4) ||
5395 pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5396 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5398 /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
5399 * bit mask for pci_alloc_consistent to return addresses within 4GB
5402 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5405 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
5409 host = scsi_host_alloc(&pmcraid_host_template,
5410 sizeof(struct pmcraid_instance));
5413 dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
5418 host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
5419 host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
5420 host->unique_id = host->host_no;
5421 host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
5422 host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
5424 /* zero out entire instance structure */
5425 pinstance = (struct pmcraid_instance *)host->hostdata;
5426 memset(pinstance, 0, sizeof(*pinstance));
5428 pinstance->chip_cfg =
5429 (struct pmcraid_chip_details *)(dev_id->driver_data);
5431 rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
5434 dev_err(&pdev->dev, "failed to initialize adapter instance\n");
5435 goto out_scsi_host_put;
5438 pci_set_drvdata(pdev, pinstance);
5440 /* Save PCI config-space for use following the reset */
5441 rc = pci_save_state(pinstance->pdev);
5444 dev_err(&pdev->dev, "Failed to save PCI config space\n");
5445 goto out_scsi_host_put;
5448 pmcraid_disable_interrupts(pinstance, ~0);
5450 rc = pmcraid_register_interrupt_handler(pinstance);
5453 dev_err(&pdev->dev, "couldn't register interrupt handler\n");
5454 goto out_scsi_host_put;
5457 pmcraid_init_tasklets(pinstance);
5459 /* allocate verious buffers used by LLD.*/
5460 rc = pmcraid_init_buffers(pinstance);
5463 pmcraid_err("couldn't allocate memory blocks\n");
5464 goto out_unregister_isr;
5467 /* check the reset type required */
5468 pmcraid_reset_type(pinstance);
5470 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5472 /* Start IOA firmware initialization and bring card to Operational
5475 pmcraid_info("starting IOA initialization sequence\n");
5476 if (pmcraid_reset_bringup(pinstance)) {
5477 dev_err(&pdev->dev, "couldn't initialize IOA \n");
5479 goto out_release_bufs;
5482 /* Add adapter instance into mid-layer list */
5483 rc = scsi_add_host(pinstance->host, &pdev->dev);
5485 pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
5486 goto out_release_bufs;
5489 scsi_scan_host(pinstance->host);
5491 rc = pmcraid_setup_chrdev(pinstance);
5494 pmcraid_err("couldn't create mgmt interface, error: %x\n",
5496 goto out_remove_host;
5499 /* Schedule worker thread to handle CCN and take care of adding and
5500 * removing devices to OS
5502 atomic_set(&pinstance->expose_resources, 1);
5503 schedule_work(&pinstance->worker_q);
5507 scsi_remove_host(host);
5510 pmcraid_release_buffers(pinstance);
5513 pmcraid_kill_tasklets(pinstance);
5514 pmcraid_unregister_interrupt_handler(pinstance);
5517 scsi_host_put(host);
5520 iounmap(mapped_pci_addr);
5522 out_release_regions:
5523 pci_release_regions(pdev);
5526 atomic_dec(&pmcraid_adapter_count);
5527 pci_set_drvdata(pdev, NULL);
5528 pci_disable_device(pdev);
5533 * PCI driver structure of pcmraid driver
5535 static struct pci_driver pmcraid_driver = {
5536 .name = PMCRAID_DRIVER_NAME,
5537 .id_table = pmcraid_pci_table,
5538 .probe = pmcraid_probe,
5539 .remove = pmcraid_remove,
5540 .suspend = pmcraid_suspend,
5541 .resume = pmcraid_resume,
5542 .shutdown = pmcraid_shutdown
5546 * pmcraid_init - module load entry point
5548 static int __init pmcraid_init(void)
5553 pmcraid_info("%s Device Driver version: %s %s\n",
5554 PMCRAID_DRIVER_NAME,
5555 PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
5557 error = alloc_chrdev_region(&dev, 0,
5558 PMCRAID_MAX_ADAPTERS,
5562 pmcraid_err("failed to get a major number for adapters\n");
5566 pmcraid_major = MAJOR(dev);
5567 pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
5569 if (IS_ERR(pmcraid_class)) {
5570 error = PTR_ERR(pmcraid_class);
5571 pmcraid_err("failed to register with with sysfs, error = %x\n",
5573 goto out_unreg_chrdev;
5576 error = pmcraid_netlink_init();
5579 goto out_unreg_chrdev;
5581 error = pci_register_driver(&pmcraid_driver);
5586 pmcraid_err("failed to register pmcraid driver, error = %x\n",
5588 class_destroy(pmcraid_class);
5589 pmcraid_netlink_release();
5592 unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
5599 * pmcraid_exit - module unload entry point
5601 static void __exit pmcraid_exit(void)
5603 pmcraid_netlink_release();
5604 class_destroy(pmcraid_class);
5605 unregister_chrdev_region(MKDEV(pmcraid_major, 0),
5606 PMCRAID_MAX_ADAPTERS);
5607 pci_unregister_driver(&pmcraid_driver);
5610 module_init(pmcraid_init);
5611 module_exit(pmcraid_exit);