kmsg_dump: Dump on crash_kexec as well
[linux-2.6.git] / drivers / scsi / pmcraid.c
blob34c6b896a91b98cf2328b251058c7aa82ce2829a
1 /*
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,
21 * USA
24 #include <linux/fs.h>
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>
42 #include <linux/io.h>
43 #include <asm/irq.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>
55 #include "pmcraid.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);
80 * Module parameters
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."
96 "(default: 0)");
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."
101 "(default: 0)");
103 /* chip specific constants for PMC MaxRAID controllers (same for
104 * 0x5220 and 0x8010
106 static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
108 .ioastatus = 0x0,
109 .ioarrin = 0x00040,
110 .mailbox = 0x7FC30,
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.
145 * Return value:
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;
152 u8 target, bus, lun;
153 unsigned long lock_flags;
154 int rc = -ENXIO;
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
160 * this lock
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)
169 continue;
170 bus = PMCRAID_VSET_BUS_ID;
171 lun = 0;
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);
176 } else {
177 continue;
180 if (bus == scsi_dev->channel &&
181 target == scsi_dev->id &&
182 lun == scsi_dev->lun) {
183 res = temp;
184 break;
188 if (res) {
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);
194 rc = 0;
196 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
197 return rc;
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
210 * Return value:
211 * 0 on success
213 static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
215 struct pmcraid_resource_entry *res = scsi_dev->hostdata;
217 if (!res)
218 return 0;
220 /* LLD exposes VSETs and Enclosure devices only */
221 if (RES_IS_GSCSI(res->cfg_entry) &&
222 scsi_dev->type != TYPE_ENCLOSURE)
223 return -ENXIO;
225 pmcraid_info("configuring %x:%x:%x:%x\n",
226 scsi_dev->host->unique_id,
227 scsi_dev->channel,
228 scsi_dev->id,
229 scsi_dev->lun);
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);
246 } else {
247 scsi_adjust_queue_depth(scsi_dev, 0,
248 scsi_dev->host->cmd_per_lun);
251 return 0;
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.
262 * Return value
263 * none
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;
271 if (res)
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
281 * @reason: calling context
283 * Return value
284 * actual depth set
286 static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth,
287 int reason)
289 if (reason != SCSI_QDEPTH_DEFAULT)
290 return -EOPNOTSUPP;
292 if (depth > PMCRAID_MAX_CMD_PER_LUN)
293 depth = PMCRAID_MAX_CMD_PER_LUN;
295 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), depth);
297 return scsi_dev->queue_depth;
301 * pmcraid_change_queue_type - Change the device's queue type
302 * @scsi_dev: scsi device struct
303 * @tag: type of tags to use
305 * Return value:
306 * actual queue type set
308 static int pmcraid_change_queue_type(struct scsi_device *scsi_dev, int tag)
310 struct pmcraid_resource_entry *res;
312 res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
314 if ((res) && scsi_dev->tagged_supported &&
315 (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
316 scsi_set_tag_type(scsi_dev, tag);
318 if (tag)
319 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
320 else
321 scsi_deactivate_tcq(scsi_dev, scsi_dev->queue_depth);
322 } else
323 tag = 0;
325 return tag;
330 * pmcraid_init_cmdblk - initializes a command block
332 * @cmd: pointer to struct pmcraid_cmd to be initialized
333 * @index: if >=0 first time initialization; otherwise reinitialization
335 * Return Value
336 * None
338 void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
340 struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
341 dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
343 if (index >= 0) {
344 /* first time initialization (called from probe) */
345 u32 ioasa_offset =
346 offsetof(struct pmcraid_control_block, ioasa);
348 cmd->index = index;
349 ioarcb->response_handle = cpu_to_le32(index << 2);
350 ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
351 ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
352 ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
353 } else {
354 /* re-initialization of various lengths, called once command is
355 * processed by IOA
357 memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
358 ioarcb->request_flags0 = 0;
359 ioarcb->request_flags1 = 0;
360 ioarcb->cmd_timeout = 0;
361 ioarcb->ioarcb_bus_addr &= (~0x1FULL);
362 ioarcb->ioadl_bus_addr = 0;
363 ioarcb->ioadl_length = 0;
364 ioarcb->data_transfer_length = 0;
365 ioarcb->add_cmd_param_length = 0;
366 ioarcb->add_cmd_param_offset = 0;
367 cmd->ioa_cb->ioasa.ioasc = 0;
368 cmd->ioa_cb->ioasa.residual_data_length = 0;
369 cmd->u.time_left = 0;
372 cmd->cmd_done = NULL;
373 cmd->scsi_cmd = NULL;
374 cmd->release = 0;
375 cmd->completion_req = 0;
376 cmd->dma_handle = 0;
377 init_timer(&cmd->timer);
381 * pmcraid_reinit_cmdblk - reinitialize a command block
383 * @cmd: pointer to struct pmcraid_cmd to be reinitialized
385 * Return Value
386 * None
388 static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
390 pmcraid_init_cmdblk(cmd, -1);
394 * pmcraid_get_free_cmd - get a free cmd block from command block pool
395 * @pinstance: adapter instance structure
397 * Return Value:
398 * returns pointer to cmd block or NULL if no blocks are available
400 static struct pmcraid_cmd *pmcraid_get_free_cmd(
401 struct pmcraid_instance *pinstance
404 struct pmcraid_cmd *cmd = NULL;
405 unsigned long lock_flags;
407 /* free cmd block list is protected by free_pool_lock */
408 spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
410 if (!list_empty(&pinstance->free_cmd_pool)) {
411 cmd = list_entry(pinstance->free_cmd_pool.next,
412 struct pmcraid_cmd, free_list);
413 list_del(&cmd->free_list);
415 spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
417 /* Initialize the command block before giving it the caller */
418 if (cmd != NULL)
419 pmcraid_reinit_cmdblk(cmd);
420 return cmd;
424 * pmcraid_return_cmd - return a completed command block back into free pool
425 * @cmd: pointer to the command block
427 * Return Value:
428 * nothing
430 void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
432 struct pmcraid_instance *pinstance = cmd->drv_inst;
433 unsigned long lock_flags;
435 spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
436 list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
437 spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
441 * pmcraid_read_interrupts - reads IOA interrupts
443 * @pinstance: pointer to adapter instance structure
445 * Return value
446 * interrupts read from IOA
448 static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
450 return ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
454 * pmcraid_disable_interrupts - Masks and clears all specified interrupts
456 * @pinstance: pointer to per adapter instance structure
457 * @intrs: interrupts to disable
459 * Return Value
460 * None
462 static void pmcraid_disable_interrupts(
463 struct pmcraid_instance *pinstance,
464 u32 intrs
467 u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
468 u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
470 iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
471 iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
472 iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_mask_reg);
473 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
477 * pmcraid_enable_interrupts - Enables specified interrupts
479 * @pinstance: pointer to per adapter instance structure
480 * @intr: interrupts to enable
482 * Return Value
483 * None
485 static void pmcraid_enable_interrupts(
486 struct pmcraid_instance *pinstance,
487 u32 intrs
490 u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
491 u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
493 iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
494 iowrite32(~intrs, pinstance->int_regs.ioa_host_interrupt_mask_reg);
495 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
497 pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
498 ioread32(pinstance->int_regs.global_interrupt_mask_reg),
499 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
503 * pmcraid_reset_type - Determine the required reset type
504 * @pinstance: pointer to adapter instance structure
506 * IOA requires hard reset if any of the following conditions is true.
507 * 1. If HRRQ valid interrupt is not masked
508 * 2. IOA reset alert doorbell is set
509 * 3. If there are any error interrupts
511 static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
513 u32 mask;
514 u32 intrs;
515 u32 alerts;
517 mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
518 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
519 alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
521 if ((mask & INTRS_HRRQ_VALID) == 0 ||
522 (alerts & DOORBELL_IOA_RESET_ALERT) ||
523 (intrs & PMCRAID_ERROR_INTERRUPTS)) {
524 pmcraid_info("IOA requires hard reset\n");
525 pinstance->ioa_hard_reset = 1;
528 /* If unit check is active, trigger the dump */
529 if (intrs & INTRS_IOA_UNIT_CHECK)
530 pinstance->ioa_unit_check = 1;
534 * pmcraid_bist_done - completion function for PCI BIST
535 * @cmd: pointer to reset command
536 * Return Value
537 * none
540 static void pmcraid_ioa_reset(struct pmcraid_cmd *);
542 static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
544 struct pmcraid_instance *pinstance = cmd->drv_inst;
545 unsigned long lock_flags;
546 int rc;
547 u16 pci_reg;
549 rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
551 /* If PCI config space can't be accessed wait for another two secs */
552 if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
553 cmd->u.time_left > 0) {
554 pmcraid_info("BIST not complete, waiting another 2 secs\n");
555 cmd->timer.expires = jiffies + cmd->u.time_left;
556 cmd->u.time_left = 0;
557 cmd->timer.data = (unsigned long)cmd;
558 cmd->timer.function =
559 (void (*)(unsigned long))pmcraid_bist_done;
560 add_timer(&cmd->timer);
561 } else {
562 cmd->u.time_left = 0;
563 pmcraid_info("BIST is complete, proceeding with reset\n");
564 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
565 pmcraid_ioa_reset(cmd);
566 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
571 * pmcraid_start_bist - starts BIST
572 * @cmd: pointer to reset cmd
573 * Return Value
574 * none
576 static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
578 struct pmcraid_instance *pinstance = cmd->drv_inst;
579 u32 doorbells, intrs;
581 /* proceed with bist and wait for 2 seconds */
582 iowrite32(DOORBELL_IOA_START_BIST,
583 pinstance->int_regs.host_ioa_interrupt_reg);
584 doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
585 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
586 pmcraid_info("doorbells after start bist: %x intrs: %x \n",
587 doorbells, intrs);
589 cmd->u.time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
590 cmd->timer.data = (unsigned long)cmd;
591 cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
592 cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
593 add_timer(&cmd->timer);
597 * pmcraid_reset_alert_done - completion routine for reset_alert
598 * @cmd: pointer to command block used in reset sequence
599 * Return value
600 * None
602 static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
604 struct pmcraid_instance *pinstance = cmd->drv_inst;
605 u32 status = ioread32(pinstance->ioa_status);
606 unsigned long lock_flags;
608 /* if the critical operation in progress bit is set or the wait times
609 * out, invoke reset engine to proceed with hard reset. If there is
610 * some more time to wait, restart the timer
612 if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
613 cmd->u.time_left <= 0) {
614 pmcraid_info("critical op is reset proceeding with reset\n");
615 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
616 pmcraid_ioa_reset(cmd);
617 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
618 } else {
619 pmcraid_info("critical op is not yet reset waiting again\n");
620 /* restart timer if some more time is available to wait */
621 cmd->u.time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
622 cmd->timer.data = (unsigned long)cmd;
623 cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
624 cmd->timer.function =
625 (void (*)(unsigned long))pmcraid_reset_alert_done;
626 add_timer(&cmd->timer);
631 * pmcraid_reset_alert - alerts IOA for a possible reset
632 * @cmd : command block to be used for reset sequence.
634 * Return Value
635 * returns 0 if pci config-space is accessible and RESET_DOORBELL is
636 * successfully written to IOA. Returns non-zero in case pci_config_space
637 * is not accessible
639 static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
641 struct pmcraid_instance *pinstance = cmd->drv_inst;
642 u32 doorbells;
643 int rc;
644 u16 pci_reg;
646 /* If we are able to access IOA PCI config space, alert IOA that we are
647 * going to reset it soon. This enables IOA to preserv persistent error
648 * data if any. In case memory space is not accessible, proceed with
649 * BIST or slot_reset
651 rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
652 if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
654 /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
655 * reset IOA doesn't generate any interrupts when CRITICAL
656 * OPERATION bit is reset. A timer is started to wait for this
657 * bit to be reset.
659 cmd->u.time_left = PMCRAID_RESET_TIMEOUT;
660 cmd->timer.data = (unsigned long)cmd;
661 cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
662 cmd->timer.function =
663 (void (*)(unsigned long))pmcraid_reset_alert_done;
664 add_timer(&cmd->timer);
666 iowrite32(DOORBELL_IOA_RESET_ALERT,
667 pinstance->int_regs.host_ioa_interrupt_reg);
668 doorbells =
669 ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
670 pmcraid_info("doorbells after reset alert: %x\n", doorbells);
671 } else {
672 pmcraid_info("PCI config is not accessible starting BIST\n");
673 pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
674 pmcraid_start_bist(cmd);
679 * pmcraid_timeout_handler - Timeout handler for internally generated ops
681 * @cmd : pointer to command structure, that got timedout
683 * This function blocks host requests and initiates an adapter reset.
685 * Return value:
686 * None
688 static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
690 struct pmcraid_instance *pinstance = cmd->drv_inst;
691 unsigned long lock_flags;
693 dev_info(&pinstance->pdev->dev,
694 "Adapter being reset due to command timeout.\n");
696 /* Command timeouts result in hard reset sequence. The command that got
697 * timed out may be the one used as part of reset sequence. In this
698 * case restart reset sequence using the same command block even if
699 * reset is in progress. Otherwise fail this command and get a free
700 * command block to restart the reset sequence.
702 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
703 if (!pinstance->ioa_reset_in_progress) {
704 pinstance->ioa_reset_attempts = 0;
705 cmd = pmcraid_get_free_cmd(pinstance);
707 /* If we are out of command blocks, just return here itself.
708 * Some other command's timeout handler can do the reset job
710 if (cmd == NULL) {
711 spin_unlock_irqrestore(pinstance->host->host_lock,
712 lock_flags);
713 pmcraid_err("no free cmnd block for timeout handler\n");
714 return;
717 pinstance->reset_cmd = cmd;
718 pinstance->ioa_reset_in_progress = 1;
719 } else {
720 pmcraid_info("reset is already in progress\n");
722 if (pinstance->reset_cmd != cmd) {
723 /* This command should have been given to IOA, this
724 * command will be completed by fail_outstanding_cmds
725 * anyway
727 pmcraid_err("cmd is pending but reset in progress\n");
730 /* If this command was being used as part of the reset
731 * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
732 * causes fail_outstanding_commands not to return the command
733 * block back to free pool
735 if (cmd == pinstance->reset_cmd)
736 cmd->cmd_done = pmcraid_ioa_reset;
740 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
741 scsi_block_requests(pinstance->host);
742 pmcraid_reset_alert(cmd);
743 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
747 * pmcraid_internal_done - completion routine for internally generated cmds
749 * @cmd: command that got response from IOA
751 * Return Value:
752 * none
754 static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
756 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
757 cmd->ioa_cb->ioarcb.cdb[0],
758 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
760 /* Some of the internal commands are sent with callers blocking for the
761 * response. Same will be indicated as part of cmd->completion_req
762 * field. Response path needs to wake up any waiters waiting for cmd
763 * completion if this flag is set.
765 if (cmd->completion_req) {
766 cmd->completion_req = 0;
767 complete(&cmd->wait_for_completion);
770 /* most of the internal commands are completed by caller itself, so
771 * no need to return the command block back to free pool until we are
772 * required to do so (e.g once done with initialization).
774 if (cmd->release) {
775 cmd->release = 0;
776 pmcraid_return_cmd(cmd);
781 * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
783 * @cmd: command that got response from IOA
785 * This routine is called after driver re-reads configuration table due to a
786 * lost CCN. It returns the command block back to free pool and schedules
787 * worker thread to add/delete devices into the system.
789 * Return Value:
790 * none
792 static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
794 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
795 cmd->ioa_cb->ioarcb.cdb[0],
796 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
798 if (cmd->release) {
799 cmd->release = 0;
800 pmcraid_return_cmd(cmd);
802 pmcraid_info("scheduling worker for config table reinitialization\n");
803 schedule_work(&cmd->drv_inst->worker_q);
807 * pmcraid_erp_done - Process completion of SCSI error response from device
808 * @cmd: pmcraid_command
810 * This function copies the sense buffer into the scsi_cmd struct and completes
811 * scsi_cmd by calling scsi_done function.
813 * Return value:
814 * none
816 static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
818 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
819 struct pmcraid_instance *pinstance = cmd->drv_inst;
820 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
822 if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
823 scsi_cmd->result |= (DID_ERROR << 16);
824 scmd_printk(KERN_INFO, scsi_cmd,
825 "command CDB[0] = %x failed with IOASC: 0x%08X\n",
826 cmd->ioa_cb->ioarcb.cdb[0], ioasc);
829 /* if we had allocated sense buffers for request sense, copy the sense
830 * release the buffers
832 if (cmd->sense_buffer != NULL) {
833 memcpy(scsi_cmd->sense_buffer,
834 cmd->sense_buffer,
835 SCSI_SENSE_BUFFERSIZE);
836 pci_free_consistent(pinstance->pdev,
837 SCSI_SENSE_BUFFERSIZE,
838 cmd->sense_buffer, cmd->sense_buffer_dma);
839 cmd->sense_buffer = NULL;
840 cmd->sense_buffer_dma = 0;
843 scsi_dma_unmap(scsi_cmd);
844 pmcraid_return_cmd(cmd);
845 scsi_cmd->scsi_done(scsi_cmd);
849 * pmcraid_fire_command - sends an IOA command to adapter
851 * This function adds the given block into pending command list
852 * and returns without waiting
854 * @cmd : command to be sent to the device
856 * Return Value
857 * None
859 static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
861 struct pmcraid_instance *pinstance = cmd->drv_inst;
862 unsigned long lock_flags;
864 /* Add this command block to pending cmd pool. We do this prior to
865 * writting IOARCB to ioarrin because IOA might complete the command
866 * by the time we are about to add it to the list. Response handler
867 * (isr/tasklet) looks for cmb block in the pending pending list.
869 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
870 list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
871 spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
872 atomic_inc(&pinstance->outstanding_cmds);
874 /* driver writes lower 32-bit value of IOARCB address only */
875 mb();
876 iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
877 pinstance->ioarrin);
881 * pmcraid_send_cmd - fires a command to IOA
883 * This function also sets up timeout function, and command completion
884 * function
886 * @cmd: pointer to the command block to be fired to IOA
887 * @cmd_done: command completion function, called once IOA responds
888 * @timeout: timeout to wait for this command completion
889 * @timeout_func: timeout handler
891 * Return value
892 * none
894 static void pmcraid_send_cmd(
895 struct pmcraid_cmd *cmd,
896 void (*cmd_done) (struct pmcraid_cmd *),
897 unsigned long timeout,
898 void (*timeout_func) (struct pmcraid_cmd *)
901 /* initialize done function */
902 cmd->cmd_done = cmd_done;
904 if (timeout_func) {
905 /* setup timeout handler */
906 cmd->timer.data = (unsigned long)cmd;
907 cmd->timer.expires = jiffies + timeout;
908 cmd->timer.function = (void (*)(unsigned long))timeout_func;
909 add_timer(&cmd->timer);
912 /* fire the command to IOA */
913 _pmcraid_fire_command(cmd);
917 * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
919 * @cmd: pointer to the command block used as part of reset sequence
921 * Return Value
922 * None
924 static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
926 pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
927 cmd->ioa_cb->ioarcb.cdb[0],
928 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
930 /* Note that commands sent during reset require next command to be sent
931 * to IOA. Hence reinit the done function as well as timeout function
933 pmcraid_reinit_cmdblk(cmd);
934 cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
935 cmd->ioa_cb->ioarcb.resource_handle =
936 cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
937 cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
938 cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
940 /* fire shutdown command to hardware. */
941 pmcraid_info("firing normal shutdown command (%d) to IOA\n",
942 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
944 pmcraid_send_cmd(cmd, pmcraid_ioa_reset,
945 PMCRAID_SHUTDOWN_TIMEOUT,
946 pmcraid_timeout_handler);
950 * pmcraid_identify_hrrq - registers host rrq buffers with IOA
951 * @cmd: pointer to command block to be used for identify hrrq
953 * Return Value
954 * 0 in case of success, otherwise non-zero failure code
957 static void pmcraid_querycfg(struct pmcraid_cmd *);
959 static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
961 struct pmcraid_instance *pinstance = cmd->drv_inst;
962 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
963 int index = 0;
964 __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
965 u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
967 pmcraid_reinit_cmdblk(cmd);
969 /* Initialize ioarcb */
970 ioarcb->request_type = REQ_TYPE_IOACMD;
971 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
973 /* initialize the hrrq number where IOA will respond to this command */
974 ioarcb->hrrq_id = index;
975 ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
976 ioarcb->cdb[1] = index;
978 /* IOA expects 64-bit pci address to be written in B.E format
979 * (i.e cdb[2]=MSByte..cdb[9]=LSB.
981 pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb => %llx:%llx\n",
982 hrrq_addr, ioarcb->ioarcb_bus_addr);
984 memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
985 memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
987 /* Subsequent commands require HRRQ identification to be successful.
988 * Note that this gets called even during reset from SCSI mid-layer
989 * or tasklet
991 pmcraid_send_cmd(cmd, pmcraid_querycfg,
992 PMCRAID_INTERNAL_TIMEOUT,
993 pmcraid_timeout_handler);
996 static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
997 static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
1000 * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
1002 * @cmd: initialized command block pointer
1004 * Return Value
1005 * none
1007 static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
1009 if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
1010 atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
1011 else
1012 atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
1014 pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
1018 * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
1020 * @pinstance: pointer to adapter instance structure
1021 * @type: HCAM type
1023 * Return Value
1024 * pointer to initialized pmcraid_cmd structure or NULL
1026 static struct pmcraid_cmd *pmcraid_init_hcam
1028 struct pmcraid_instance *pinstance,
1029 u8 type
1032 struct pmcraid_cmd *cmd;
1033 struct pmcraid_ioarcb *ioarcb;
1034 struct pmcraid_ioadl_desc *ioadl;
1035 struct pmcraid_hostrcb *hcam;
1036 void (*cmd_done) (struct pmcraid_cmd *);
1037 dma_addr_t dma;
1038 int rcb_size;
1040 cmd = pmcraid_get_free_cmd(pinstance);
1042 if (!cmd) {
1043 pmcraid_err("no free command blocks for hcam\n");
1044 return cmd;
1047 if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
1048 rcb_size = sizeof(struct pmcraid_hcam_ccn);
1049 cmd_done = pmcraid_process_ccn;
1050 dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
1051 hcam = &pinstance->ccn;
1052 } else {
1053 rcb_size = sizeof(struct pmcraid_hcam_ldn);
1054 cmd_done = pmcraid_process_ldn;
1055 dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
1056 hcam = &pinstance->ldn;
1059 /* initialize command pointer used for HCAM registration */
1060 hcam->cmd = cmd;
1062 ioarcb = &cmd->ioa_cb->ioarcb;
1063 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
1064 offsetof(struct pmcraid_ioarcb,
1065 add_data.u.ioadl[0]));
1066 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
1067 ioadl = ioarcb->add_data.u.ioadl;
1069 /* Initialize ioarcb */
1070 ioarcb->request_type = REQ_TYPE_HCAM;
1071 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
1072 ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
1073 ioarcb->cdb[1] = type;
1074 ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
1075 ioarcb->cdb[8] = (rcb_size) & 0xFF;
1077 ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
1079 ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
1080 ioadl[0].data_len = cpu_to_le32(rcb_size);
1081 ioadl[0].address = cpu_to_le32(dma);
1083 cmd->cmd_done = cmd_done;
1084 return cmd;
1088 * pmcraid_send_hcam - Send an HCAM to IOA
1089 * @pinstance: ioa config struct
1090 * @type: HCAM type
1092 * This function will send a Host Controlled Async command to IOA.
1094 * Return value:
1095 * none
1097 static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
1099 struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
1100 pmcraid_send_hcam_cmd(cmd);
1105 * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
1107 * @cmd: pointer to cmd that is used as cancelling command
1108 * @cmd_to_cancel: pointer to the command that needs to be cancelled
1110 static void pmcraid_prepare_cancel_cmd(
1111 struct pmcraid_cmd *cmd,
1112 struct pmcraid_cmd *cmd_to_cancel
1115 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
1116 __be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
1118 /* Get the resource handle to where the command to be aborted has been
1119 * sent.
1121 ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
1122 ioarcb->request_type = REQ_TYPE_IOACMD;
1123 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
1124 ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
1126 /* IOARCB address of the command to be cancelled is given in
1127 * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
1128 * IOARCB address are not masked.
1130 ioarcb_addr = cpu_to_be64(ioarcb_addr);
1131 memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
1135 * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
1137 * @cmd: command to be used as cancelling command
1138 * @type: HCAM type
1139 * @cmd_done: op done function for the cancelling command
1141 static void pmcraid_cancel_hcam(
1142 struct pmcraid_cmd *cmd,
1143 u8 type,
1144 void (*cmd_done) (struct pmcraid_cmd *)
1147 struct pmcraid_instance *pinstance;
1148 struct pmcraid_hostrcb *hcam;
1150 pinstance = cmd->drv_inst;
1151 hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
1152 &pinstance->ldn : &pinstance->ccn;
1154 /* prepare for cancelling previous hcam command. If the HCAM is
1155 * currently not pending with IOA, we would have hcam->cmd as non-null
1157 if (hcam->cmd == NULL)
1158 return;
1160 pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
1162 /* writing to IOARRIN must be protected by host_lock, as mid-layer
1163 * schedule queuecommand while we are doing this
1165 pmcraid_send_cmd(cmd, cmd_done,
1166 PMCRAID_INTERNAL_TIMEOUT,
1167 pmcraid_timeout_handler);
1171 * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
1173 * @cmd: command block to be used for cancelling the HCAM
1175 static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
1177 pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
1178 cmd->ioa_cb->ioarcb.cdb[0],
1179 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
1181 pmcraid_reinit_cmdblk(cmd);
1183 pmcraid_cancel_hcam(cmd,
1184 PMCRAID_HCAM_CODE_CONFIG_CHANGE,
1185 pmcraid_ioa_shutdown);
1189 * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
1191 * @cmd: command block to be used for cancelling the HCAM
1193 static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
1195 pmcraid_cancel_hcam(cmd,
1196 PMCRAID_HCAM_CODE_LOG_DATA,
1197 pmcraid_cancel_ccn);
1201 * pmcraid_expose_resource - check if the resource can be exposed to OS
1203 * @cfgte: pointer to configuration table entry of the resource
1205 * Return value:
1206 * true if resource can be added to midlayer, false(0) otherwise
1208 static int pmcraid_expose_resource(struct pmcraid_config_table_entry *cfgte)
1210 int retval = 0;
1212 if (cfgte->resource_type == RES_TYPE_VSET)
1213 retval = ((cfgte->unique_flags1 & 0xFF) < 0xFE);
1214 else if (cfgte->resource_type == RES_TYPE_GSCSI)
1215 retval = (RES_BUS(cfgte->resource_address) !=
1216 PMCRAID_VIRTUAL_ENCL_BUS_ID);
1217 return retval;
1220 /* attributes supported by pmcraid_event_family */
1221 enum {
1222 PMCRAID_AEN_ATTR_UNSPEC,
1223 PMCRAID_AEN_ATTR_EVENT,
1224 __PMCRAID_AEN_ATTR_MAX,
1226 #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
1228 /* commands supported by pmcraid_event_family */
1229 enum {
1230 PMCRAID_AEN_CMD_UNSPEC,
1231 PMCRAID_AEN_CMD_EVENT,
1232 __PMCRAID_AEN_CMD_MAX,
1234 #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
1236 static struct genl_family pmcraid_event_family = {
1237 .id = GENL_ID_GENERATE,
1238 .name = "pmcraid",
1239 .version = 1,
1240 .maxattr = PMCRAID_AEN_ATTR_MAX
1244 * pmcraid_netlink_init - registers pmcraid_event_family
1246 * Return value:
1247 * 0 if the pmcraid_event_family is successfully registered
1248 * with netlink generic, non-zero otherwise
1250 static int pmcraid_netlink_init(void)
1252 int result;
1254 result = genl_register_family(&pmcraid_event_family);
1256 if (result)
1257 return result;
1259 pmcraid_info("registered NETLINK GENERIC group: %d\n",
1260 pmcraid_event_family.id);
1262 return result;
1266 * pmcraid_netlink_release - unregisters pmcraid_event_family
1268 * Return value:
1269 * none
1271 static void pmcraid_netlink_release(void)
1273 genl_unregister_family(&pmcraid_event_family);
1277 * pmcraid_notify_aen - sends event msg to user space application
1278 * @pinstance: pointer to adapter instance structure
1279 * @type: HCAM type
1281 * Return value:
1282 * 0 if success, error value in case of any failure.
1284 static int pmcraid_notify_aen(struct pmcraid_instance *pinstance, u8 type)
1286 struct sk_buff *skb;
1287 struct pmcraid_aen_msg *aen_msg;
1288 void *msg_header;
1289 int data_size, total_size;
1290 int result;
1293 if (type == PMCRAID_HCAM_CODE_LOG_DATA) {
1294 aen_msg = pinstance->ldn.msg;
1295 data_size = pinstance->ldn.hcam->data_len;
1296 } else {
1297 aen_msg = pinstance->ccn.msg;
1298 data_size = pinstance->ccn.hcam->data_len;
1301 data_size += sizeof(struct pmcraid_hcam_hdr);
1302 aen_msg->hostno = (pinstance->host->unique_id << 16 |
1303 MINOR(pinstance->cdev.dev));
1304 aen_msg->length = data_size;
1305 data_size += sizeof(*aen_msg);
1307 total_size = nla_total_size(data_size);
1308 skb = genlmsg_new(total_size, GFP_ATOMIC);
1311 if (!skb) {
1312 pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
1313 total_size);
1314 return -ENOMEM;
1317 /* add the genetlink message header */
1318 msg_header = genlmsg_put(skb, 0, 0,
1319 &pmcraid_event_family, 0,
1320 PMCRAID_AEN_CMD_EVENT);
1321 if (!msg_header) {
1322 pmcraid_err("failed to copy command details\n");
1323 nlmsg_free(skb);
1324 return -ENOMEM;
1327 result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
1329 if (result) {
1330 pmcraid_err("failed to copy AEN attribute data \n");
1331 nlmsg_free(skb);
1332 return -EINVAL;
1335 /* send genetlink multicast message to notify appplications */
1336 result = genlmsg_end(skb, msg_header);
1338 if (result < 0) {
1339 pmcraid_err("genlmsg_end failed\n");
1340 nlmsg_free(skb);
1341 return result;
1344 result =
1345 genlmsg_multicast(skb, 0, pmcraid_event_family.id, GFP_ATOMIC);
1347 /* If there are no listeners, genlmsg_multicast may return non-zero
1348 * value.
1350 if (result)
1351 pmcraid_info("failed to send %s event message %x!\n",
1352 type == PMCRAID_HCAM_CODE_LOG_DATA ? "LDN" : "CCN",
1353 result);
1354 return result;
1358 * pmcraid_handle_config_change - Handle a config change from the adapter
1359 * @pinstance: pointer to per adapter instance structure
1361 * Return value:
1362 * none
1364 static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
1366 struct pmcraid_config_table_entry *cfg_entry;
1367 struct pmcraid_hcam_ccn *ccn_hcam;
1368 struct pmcraid_cmd *cmd;
1369 struct pmcraid_cmd *cfgcmd;
1370 struct pmcraid_resource_entry *res = NULL;
1371 u32 new_entry = 1;
1372 unsigned long lock_flags;
1373 unsigned long host_lock_flags;
1374 int rc;
1376 ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
1377 cfg_entry = &ccn_hcam->cfg_entry;
1379 pmcraid_info
1380 ("CCN(%x): %x type: %x lost: %x flags: %x res: %x:%x:%x:%x\n",
1381 pinstance->ccn.hcam->ilid,
1382 pinstance->ccn.hcam->op_code,
1383 pinstance->ccn.hcam->notification_type,
1384 pinstance->ccn.hcam->notification_lost,
1385 pinstance->ccn.hcam->flags,
1386 pinstance->host->unique_id,
1387 RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
1388 (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
1389 RES_BUS(cfg_entry->resource_address)),
1390 RES_IS_VSET(*cfg_entry) ? cfg_entry->unique_flags1 :
1391 RES_TARGET(cfg_entry->resource_address),
1392 RES_LUN(cfg_entry->resource_address));
1395 /* If this HCAM indicates a lost notification, read the config table */
1396 if (pinstance->ccn.hcam->notification_lost) {
1397 cfgcmd = pmcraid_get_free_cmd(pinstance);
1398 if (cfgcmd) {
1399 pmcraid_info("lost CCN, reading config table\b");
1400 pinstance->reinit_cfg_table = 1;
1401 pmcraid_querycfg(cfgcmd);
1402 } else {
1403 pmcraid_err("lost CCN, no free cmd for querycfg\n");
1405 goto out_notify_apps;
1408 /* If this resource is not going to be added to mid-layer, just notify
1409 * applications and return
1411 if (!pmcraid_expose_resource(cfg_entry))
1412 goto out_notify_apps;
1414 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
1415 list_for_each_entry(res, &pinstance->used_res_q, queue) {
1416 rc = memcmp(&res->cfg_entry.resource_address,
1417 &cfg_entry->resource_address,
1418 sizeof(cfg_entry->resource_address));
1419 if (!rc) {
1420 new_entry = 0;
1421 break;
1425 if (new_entry) {
1427 /* If there are more number of resources than what driver can
1428 * manage, do not notify the applications about the CCN. Just
1429 * ignore this notifications and re-register the same HCAM
1431 if (list_empty(&pinstance->free_res_q)) {
1432 spin_unlock_irqrestore(&pinstance->resource_lock,
1433 lock_flags);
1434 pmcraid_err("too many resources attached\n");
1435 spin_lock_irqsave(pinstance->host->host_lock,
1436 host_lock_flags);
1437 pmcraid_send_hcam(pinstance,
1438 PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1439 spin_unlock_irqrestore(pinstance->host->host_lock,
1440 host_lock_flags);
1441 return;
1444 res = list_entry(pinstance->free_res_q.next,
1445 struct pmcraid_resource_entry, queue);
1447 list_del(&res->queue);
1448 res->scsi_dev = NULL;
1449 res->reset_progress = 0;
1450 list_add_tail(&res->queue, &pinstance->used_res_q);
1453 memcpy(&res->cfg_entry, cfg_entry,
1454 sizeof(struct pmcraid_config_table_entry));
1456 if (pinstance->ccn.hcam->notification_type ==
1457 NOTIFICATION_TYPE_ENTRY_DELETED) {
1458 if (res->scsi_dev) {
1459 res->change_detected = RES_CHANGE_DEL;
1460 res->cfg_entry.resource_handle =
1461 PMCRAID_INVALID_RES_HANDLE;
1462 schedule_work(&pinstance->worker_q);
1463 } else {
1464 /* This may be one of the non-exposed resources */
1465 list_move_tail(&res->queue, &pinstance->free_res_q);
1467 } else if (!res->scsi_dev) {
1468 res->change_detected = RES_CHANGE_ADD;
1469 schedule_work(&pinstance->worker_q);
1471 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
1473 out_notify_apps:
1475 /* Notify configuration changes to registered applications.*/
1476 if (!pmcraid_disable_aen)
1477 pmcraid_notify_aen(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1479 cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1480 if (cmd)
1481 pmcraid_send_hcam_cmd(cmd);
1485 * pmcraid_get_error_info - return error string for an ioasc
1486 * @ioasc: ioasc code
1487 * Return Value
1488 * none
1490 static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
1492 int i;
1493 for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
1494 if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
1495 return &pmcraid_ioasc_error_table[i];
1497 return NULL;
1501 * pmcraid_ioasc_logger - log IOASC information based user-settings
1502 * @ioasc: ioasc code
1503 * @cmd: pointer to command that resulted in 'ioasc'
1505 void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
1507 struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
1509 if (error_info == NULL ||
1510 cmd->drv_inst->current_log_level < error_info->log_level)
1511 return;
1513 /* log the error string */
1514 pmcraid_err("cmd [%d] for resource %x failed with %x(%s)\n",
1515 cmd->ioa_cb->ioarcb.cdb[0],
1516 cmd->ioa_cb->ioarcb.resource_handle,
1517 le32_to_cpu(ioasc), error_info->error_string);
1521 * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
1523 * @pinstance: pointer to per adapter instance structure
1525 * Return value:
1526 * none
1528 static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
1530 struct pmcraid_hcam_ldn *hcam_ldn;
1531 u32 ioasc;
1533 hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
1535 pmcraid_info
1536 ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
1537 pinstance->ldn.hcam->ilid,
1538 pinstance->ldn.hcam->op_code,
1539 pinstance->ldn.hcam->notification_type,
1540 pinstance->ldn.hcam->notification_lost,
1541 pinstance->ldn.hcam->flags,
1542 pinstance->ldn.hcam->overlay_id);
1544 /* log only the errors, no need to log informational log entries */
1545 if (pinstance->ldn.hcam->notification_type !=
1546 NOTIFICATION_TYPE_ERROR_LOG)
1547 return;
1549 if (pinstance->ldn.hcam->notification_lost ==
1550 HOSTRCB_NOTIFICATIONS_LOST)
1551 dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
1553 ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
1555 if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
1556 ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
1557 dev_info(&pinstance->pdev->dev,
1558 "UnitAttention due to IOA Bus Reset\n");
1559 scsi_report_bus_reset(
1560 pinstance->host,
1561 RES_BUS(hcam_ldn->error_log.fd_ra));
1564 return;
1568 * pmcraid_process_ccn - Op done function for a CCN.
1569 * @cmd: pointer to command struct
1571 * This function is the op done function for a configuration
1572 * change notification
1574 * Return value:
1575 * none
1577 static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
1579 struct pmcraid_instance *pinstance = cmd->drv_inst;
1580 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1581 unsigned long lock_flags;
1583 pinstance->ccn.cmd = NULL;
1584 pmcraid_return_cmd(cmd);
1586 /* If driver initiated IOA reset happened while this hcam was pending
1587 * with IOA, or IOA bringdown sequence is in progress, no need to
1588 * re-register the hcam
1590 if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
1591 atomic_read(&pinstance->ccn.ignore) == 1) {
1592 return;
1593 } else if (ioasc) {
1594 dev_info(&pinstance->pdev->dev,
1595 "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
1596 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
1597 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1598 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
1599 } else {
1600 pmcraid_handle_config_change(pinstance);
1605 * pmcraid_process_ldn - op done function for an LDN
1606 * @cmd: pointer to command block
1608 * Return value
1609 * none
1611 static void pmcraid_initiate_reset(struct pmcraid_instance *);
1613 static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
1615 struct pmcraid_instance *pinstance = cmd->drv_inst;
1616 struct pmcraid_hcam_ldn *ldn_hcam =
1617 (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
1618 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1619 u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
1620 unsigned long lock_flags;
1622 /* return the command block back to freepool */
1623 pinstance->ldn.cmd = NULL;
1624 pmcraid_return_cmd(cmd);
1626 /* If driver initiated IOA reset happened while this hcam was pending
1627 * with IOA, no need to re-register the hcam as reset engine will do it
1628 * once reset sequence is complete
1630 if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
1631 atomic_read(&pinstance->ccn.ignore) == 1) {
1632 return;
1633 } else if (!ioasc) {
1634 pmcraid_handle_error_log(pinstance);
1635 if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
1636 spin_lock_irqsave(pinstance->host->host_lock,
1637 lock_flags);
1638 pmcraid_initiate_reset(pinstance);
1639 spin_unlock_irqrestore(pinstance->host->host_lock,
1640 lock_flags);
1641 return;
1643 } else {
1644 dev_info(&pinstance->pdev->dev,
1645 "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
1647 /* send netlink message for HCAM notification if enabled */
1648 if (!pmcraid_disable_aen)
1649 pmcraid_notify_aen(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1651 cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1652 if (cmd)
1653 pmcraid_send_hcam_cmd(cmd);
1657 * pmcraid_register_hcams - register HCAMs for CCN and LDN
1659 * @pinstance: pointer per adapter instance structure
1661 * Return Value
1662 * none
1664 static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
1666 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1667 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1671 * pmcraid_unregister_hcams - cancel HCAMs registered already
1672 * @cmd: pointer to command used as part of reset sequence
1674 static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
1676 struct pmcraid_instance *pinstance = cmd->drv_inst;
1678 /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
1679 * handling hcam response though it is not necessary. In order to
1680 * prevent this, set 'ignore', so that bring-down sequence doesn't
1681 * re-send any more hcams
1683 atomic_set(&pinstance->ccn.ignore, 1);
1684 atomic_set(&pinstance->ldn.ignore, 1);
1686 /* If adapter reset was forced as part of runtime reset sequence,
1687 * start the reset sequence.
1689 if (pinstance->force_ioa_reset && !pinstance->ioa_bringdown) {
1690 pinstance->force_ioa_reset = 0;
1691 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1692 pmcraid_reset_alert(cmd);
1693 return;
1696 /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
1697 * one after the other. So CCN cancellation will be triggered by
1698 * pmcraid_cancel_ldn itself.
1700 pmcraid_cancel_ldn(cmd);
1704 * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
1705 * @pinstance: pointer to adapter instance structure
1706 * Return Value
1707 * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
1709 static void pmcraid_reinit_buffers(struct pmcraid_instance *);
1711 static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
1713 u32 intrs;
1715 pmcraid_reinit_buffers(pinstance);
1716 intrs = pmcraid_read_interrupts(pinstance);
1718 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
1720 if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
1721 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
1722 pinstance->int_regs.ioa_host_interrupt_mask_reg);
1723 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
1724 pinstance->int_regs.ioa_host_interrupt_clr_reg);
1725 return 1;
1726 } else {
1727 return 0;
1732 * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
1733 * @cmd : pointer to reset command block
1735 * Return Value
1736 * none
1738 static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
1740 struct pmcraid_instance *pinstance = cmd->drv_inst;
1741 u32 int_reg;
1742 u32 doorbell;
1744 /* There will be an interrupt when Transition to Operational bit is
1745 * set so tasklet would execute next reset task. The timeout handler
1746 * would re-initiate a reset
1748 cmd->cmd_done = pmcraid_ioa_reset;
1749 cmd->timer.data = (unsigned long)cmd;
1750 cmd->timer.expires = jiffies +
1751 msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
1752 cmd->timer.function = (void (*)(unsigned long))pmcraid_timeout_handler;
1754 if (!timer_pending(&cmd->timer))
1755 add_timer(&cmd->timer);
1757 /* Enable destructive diagnostics on IOA if it is not yet in
1758 * operational state
1760 doorbell = DOORBELL_RUNTIME_RESET |
1761 DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
1763 iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
1764 int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
1765 pmcraid_info("Waiting for IOA to become operational %x:%x\n",
1766 ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
1767 int_reg);
1771 * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
1773 * @pinstance: pointer to adapter instance structure
1775 * Return Value
1776 * none
1778 static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
1780 pmcraid_info("%s is not yet implemented\n", __func__);
1784 * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
1785 * @pinstance: pointer to adapter instance structure
1787 * This function fails all outstanding ops. If they are submitted to IOA
1788 * already, it sends cancel all messages if IOA is still accepting IOARCBs,
1789 * otherwise just completes the commands and returns the cmd blocks to free
1790 * pool.
1792 * Return value:
1793 * none
1795 static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
1797 struct pmcraid_cmd *cmd, *temp;
1798 unsigned long lock_flags;
1800 /* pending command list is protected by pending_pool_lock. Its
1801 * traversal must be done as within this lock
1803 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
1804 list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
1805 free_list) {
1806 list_del(&cmd->free_list);
1807 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
1808 lock_flags);
1809 cmd->ioa_cb->ioasa.ioasc =
1810 cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
1811 cmd->ioa_cb->ioasa.ilid =
1812 cpu_to_be32(PMCRAID_DRIVER_ILID);
1814 /* In case the command timer is still running */
1815 del_timer(&cmd->timer);
1817 /* If this is an IO command, complete it by invoking scsi_done
1818 * function. If this is one of the internal commands other
1819 * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
1820 * complete it
1822 if (cmd->scsi_cmd) {
1824 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
1825 __le32 resp = cmd->ioa_cb->ioarcb.response_handle;
1827 scsi_cmd->result |= DID_ERROR << 16;
1829 scsi_dma_unmap(scsi_cmd);
1830 pmcraid_return_cmd(cmd);
1832 pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
1833 le32_to_cpu(resp) >> 2,
1834 cmd->ioa_cb->ioarcb.cdb[0],
1835 scsi_cmd->result);
1836 scsi_cmd->scsi_done(scsi_cmd);
1837 } else if (cmd->cmd_done == pmcraid_internal_done ||
1838 cmd->cmd_done == pmcraid_erp_done) {
1839 cmd->cmd_done(cmd);
1840 } else if (cmd->cmd_done != pmcraid_ioa_reset) {
1841 pmcraid_return_cmd(cmd);
1844 atomic_dec(&pinstance->outstanding_cmds);
1845 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
1848 spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
1852 * pmcraid_ioa_reset - Implementation of IOA reset logic
1854 * @cmd: pointer to the cmd block to be used for entire reset process
1856 * This function executes most of the steps required for IOA reset. This gets
1857 * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
1858 * 'eh_' thread. Access to variables used for controling the reset sequence is
1859 * synchronized using host lock. Various functions called during reset process
1860 * would make use of a single command block, pointer to which is also stored in
1861 * adapter instance structure.
1863 * Return Value
1864 * None
1866 static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
1868 struct pmcraid_instance *pinstance = cmd->drv_inst;
1869 u8 reset_complete = 0;
1871 pinstance->ioa_reset_in_progress = 1;
1873 if (pinstance->reset_cmd != cmd) {
1874 pmcraid_err("reset is called with different command block\n");
1875 pinstance->reset_cmd = cmd;
1878 pmcraid_info("reset_engine: state = %d, command = %p\n",
1879 pinstance->ioa_state, cmd);
1881 switch (pinstance->ioa_state) {
1883 case IOA_STATE_DEAD:
1884 /* If IOA is offline, whatever may be the reset reason, just
1885 * return. callers might be waiting on the reset wait_q, wake
1886 * up them
1888 pmcraid_err("IOA is offline no reset is possible\n");
1889 reset_complete = 1;
1890 break;
1892 case IOA_STATE_IN_BRINGDOWN:
1893 /* we enter here, once ioa shutdown command is processed by IOA
1894 * Alert IOA for a possible reset. If reset alert fails, IOA
1895 * goes through hard-reset
1897 pmcraid_disable_interrupts(pinstance, ~0);
1898 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1899 pmcraid_reset_alert(cmd);
1900 break;
1902 case IOA_STATE_UNKNOWN:
1903 /* We may be called during probe or resume. Some pre-processing
1904 * is required for prior to reset
1906 scsi_block_requests(pinstance->host);
1908 /* If asked to reset while IOA was processing responses or
1909 * there are any error responses then IOA may require
1910 * hard-reset.
1912 if (pinstance->ioa_hard_reset == 0) {
1913 if (ioread32(pinstance->ioa_status) &
1914 INTRS_TRANSITION_TO_OPERATIONAL) {
1915 pmcraid_info("sticky bit set, bring-up\n");
1916 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
1917 pmcraid_reinit_cmdblk(cmd);
1918 pmcraid_identify_hrrq(cmd);
1919 } else {
1920 pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
1921 pmcraid_soft_reset(cmd);
1923 } else {
1924 /* Alert IOA of a possible reset and wait for critical
1925 * operation in progress bit to reset
1927 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1928 pmcraid_reset_alert(cmd);
1930 break;
1932 case IOA_STATE_IN_RESET_ALERT:
1933 /* If critical operation in progress bit is reset or wait gets
1934 * timed out, reset proceeds with starting BIST on the IOA.
1935 * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
1936 * they are 3 or more, reset engine marks IOA dead and returns
1938 pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
1939 pmcraid_start_bist(cmd);
1940 break;
1942 case IOA_STATE_IN_HARD_RESET:
1943 pinstance->ioa_reset_attempts++;
1945 /* retry reset if we haven't reached maximum allowed limit */
1946 if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
1947 pinstance->ioa_reset_attempts = 0;
1948 pmcraid_err("IOA didn't respond marking it as dead\n");
1949 pinstance->ioa_state = IOA_STATE_DEAD;
1950 reset_complete = 1;
1951 break;
1954 /* Once either bist or pci reset is done, restore PCI config
1955 * space. If this fails, proceed with hard reset again
1958 if (pci_restore_state(pinstance->pdev)) {
1959 pmcraid_info("config-space error resetting again\n");
1960 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1961 pmcraid_reset_alert(cmd);
1962 break;
1965 /* fail all pending commands */
1966 pmcraid_fail_outstanding_cmds(pinstance);
1968 /* check if unit check is active, if so extract dump */
1969 if (pinstance->ioa_unit_check) {
1970 pmcraid_info("unit check is active\n");
1971 pinstance->ioa_unit_check = 0;
1972 pmcraid_get_dump(pinstance);
1973 pinstance->ioa_reset_attempts--;
1974 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1975 pmcraid_reset_alert(cmd);
1976 break;
1979 /* if the reset reason is to bring-down the ioa, we might be
1980 * done with the reset restore pci_config_space and complete
1981 * the reset
1983 if (pinstance->ioa_bringdown) {
1984 pmcraid_info("bringing down the adapter\n");
1985 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
1986 pinstance->ioa_bringdown = 0;
1987 pinstance->ioa_state = IOA_STATE_UNKNOWN;
1988 reset_complete = 1;
1989 } else {
1990 /* bring-up IOA, so proceed with soft reset
1991 * Reinitialize hrrq_buffers and their indices also
1992 * enable interrupts after a pci_restore_state
1994 if (pmcraid_reset_enable_ioa(pinstance)) {
1995 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
1996 pmcraid_info("bringing up the adapter\n");
1997 pmcraid_reinit_cmdblk(cmd);
1998 pmcraid_identify_hrrq(cmd);
1999 } else {
2000 pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
2001 pmcraid_soft_reset(cmd);
2004 break;
2006 case IOA_STATE_IN_SOFT_RESET:
2007 /* TRANSITION TO OPERATIONAL is on so start initialization
2008 * sequence
2010 pmcraid_info("In softreset proceeding with bring-up\n");
2011 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
2013 /* Initialization commands start with HRRQ identification. From
2014 * now on tasklet completes most of the commands as IOA is up
2015 * and intrs are enabled
2017 pmcraid_identify_hrrq(cmd);
2018 break;
2020 case IOA_STATE_IN_BRINGUP:
2021 /* we are done with bringing up of IOA, change the ioa_state to
2022 * operational and wake up any waiters
2024 pinstance->ioa_state = IOA_STATE_OPERATIONAL;
2025 reset_complete = 1;
2026 break;
2028 case IOA_STATE_OPERATIONAL:
2029 default:
2030 /* When IOA is operational and a reset is requested, check for
2031 * the reset reason. If reset is to bring down IOA, unregister
2032 * HCAMs and initiate shutdown; if adapter reset is forced then
2033 * restart reset sequence again
2035 if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
2036 pinstance->force_ioa_reset == 0) {
2037 reset_complete = 1;
2038 } else {
2039 if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
2040 pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
2041 pmcraid_reinit_cmdblk(cmd);
2042 pmcraid_unregister_hcams(cmd);
2044 break;
2047 /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
2048 * OPERATIONAL. Reset all control variables used during reset, wake up
2049 * any waiting threads and let the SCSI mid-layer send commands. Note
2050 * that host_lock must be held before invoking scsi_report_bus_reset.
2052 if (reset_complete) {
2053 pinstance->ioa_reset_in_progress = 0;
2054 pinstance->ioa_reset_attempts = 0;
2055 pinstance->reset_cmd = NULL;
2056 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2057 pinstance->ioa_bringdown = 0;
2058 pmcraid_return_cmd(cmd);
2060 /* If target state is to bring up the adapter, proceed with
2061 * hcam registration and resource exposure to mid-layer.
2063 if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
2064 pmcraid_register_hcams(pinstance);
2066 wake_up_all(&pinstance->reset_wait_q);
2069 return;
2073 * pmcraid_initiate_reset - initiates reset sequence. This is called from
2074 * ISR/tasklet during error interrupts including IOA unit check. If reset
2075 * is already in progress, it just returns, otherwise initiates IOA reset
2076 * to bring IOA up to operational state.
2078 * @pinstance: pointer to adapter instance structure
2080 * Return value
2081 * none
2083 static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
2085 struct pmcraid_cmd *cmd;
2087 /* If the reset is already in progress, just return, otherwise start
2088 * reset sequence and return
2090 if (!pinstance->ioa_reset_in_progress) {
2091 scsi_block_requests(pinstance->host);
2092 cmd = pmcraid_get_free_cmd(pinstance);
2094 if (cmd == NULL) {
2095 pmcraid_err("no cmnd blocks for initiate_reset\n");
2096 return;
2099 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2100 pinstance->reset_cmd = cmd;
2101 pinstance->force_ioa_reset = 1;
2102 pmcraid_ioa_reset(cmd);
2107 * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
2108 * or bringdown IOA
2109 * @pinstance: pointer adapter instance structure
2110 * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
2111 * @target_state: expected target state after reset
2113 * Note: This command initiates reset and waits for its completion. Hence this
2114 * should not be called from isr/timer/tasklet functions (timeout handlers,
2115 * error response handlers and interrupt handlers).
2117 * Return Value
2118 * 1 in case ioa_state is not target_state, 0 otherwise.
2120 static int pmcraid_reset_reload(
2121 struct pmcraid_instance *pinstance,
2122 u8 shutdown_type,
2123 u8 target_state
2126 struct pmcraid_cmd *reset_cmd = NULL;
2127 unsigned long lock_flags;
2128 int reset = 1;
2130 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2132 if (pinstance->ioa_reset_in_progress) {
2133 pmcraid_info("reset_reload: reset is already in progress\n");
2135 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2137 wait_event(pinstance->reset_wait_q,
2138 !pinstance->ioa_reset_in_progress);
2140 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2142 if (pinstance->ioa_state == IOA_STATE_DEAD) {
2143 spin_unlock_irqrestore(pinstance->host->host_lock,
2144 lock_flags);
2145 pmcraid_info("reset_reload: IOA is dead\n");
2146 return reset;
2147 } else if (pinstance->ioa_state == target_state) {
2148 reset = 0;
2152 if (reset) {
2153 pmcraid_info("reset_reload: proceeding with reset\n");
2154 scsi_block_requests(pinstance->host);
2155 reset_cmd = pmcraid_get_free_cmd(pinstance);
2157 if (reset_cmd == NULL) {
2158 pmcraid_err("no free cmnd for reset_reload\n");
2159 spin_unlock_irqrestore(pinstance->host->host_lock,
2160 lock_flags);
2161 return reset;
2164 if (shutdown_type == SHUTDOWN_NORMAL)
2165 pinstance->ioa_bringdown = 1;
2167 pinstance->ioa_shutdown_type = shutdown_type;
2168 pinstance->reset_cmd = reset_cmd;
2169 pinstance->force_ioa_reset = reset;
2170 pmcraid_info("reset_reload: initiating reset\n");
2171 pmcraid_ioa_reset(reset_cmd);
2172 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2173 pmcraid_info("reset_reload: waiting for reset to complete\n");
2174 wait_event(pinstance->reset_wait_q,
2175 !pinstance->ioa_reset_in_progress);
2177 pmcraid_info("reset_reload: reset is complete !! \n");
2178 scsi_unblock_requests(pinstance->host);
2179 if (pinstance->ioa_state == target_state)
2180 reset = 0;
2183 return reset;
2187 * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
2189 * @pinstance: pointer to adapter instance structure
2191 * Return Value
2192 * whatever is returned from pmcraid_reset_reload
2194 static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
2196 return pmcraid_reset_reload(pinstance,
2197 SHUTDOWN_NORMAL,
2198 IOA_STATE_UNKNOWN);
2202 * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
2204 * @pinstance: pointer to adapter instance structure
2206 * Return Value
2207 * whatever is returned from pmcraid_reset_reload
2209 static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
2211 return pmcraid_reset_reload(pinstance,
2212 SHUTDOWN_NONE,
2213 IOA_STATE_OPERATIONAL);
2217 * pmcraid_request_sense - Send request sense to a device
2218 * @cmd: pmcraid command struct
2220 * This function sends a request sense to a device as a result of a check
2221 * condition. This method re-uses the same command block that failed earlier.
2223 static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
2225 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2226 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
2228 /* allocate DMAable memory for sense buffers */
2229 cmd->sense_buffer = pci_alloc_consistent(cmd->drv_inst->pdev,
2230 SCSI_SENSE_BUFFERSIZE,
2231 &cmd->sense_buffer_dma);
2233 if (cmd->sense_buffer == NULL) {
2234 pmcraid_err
2235 ("couldn't allocate sense buffer for request sense\n");
2236 pmcraid_erp_done(cmd);
2237 return;
2240 /* re-use the command block */
2241 memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
2242 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
2243 ioarcb->request_flags0 = (SYNC_COMPLETE |
2244 NO_LINK_DESCS |
2245 INHIBIT_UL_CHECK);
2246 ioarcb->request_type = REQ_TYPE_SCSI;
2247 ioarcb->cdb[0] = REQUEST_SENSE;
2248 ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2250 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
2251 offsetof(struct pmcraid_ioarcb,
2252 add_data.u.ioadl[0]));
2253 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
2255 ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
2257 ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
2258 ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
2259 ioadl->flags = IOADL_FLAGS_LAST_DESC;
2261 /* request sense might be called as part of error response processing
2262 * which runs in tasklets context. It is possible that mid-layer might
2263 * schedule queuecommand during this time, hence, writting to IOARRIN
2264 * must be protect by host_lock
2266 pmcraid_send_cmd(cmd, pmcraid_erp_done,
2267 PMCRAID_REQUEST_SENSE_TIMEOUT,
2268 pmcraid_timeout_handler);
2272 * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
2273 * @cmd: command that failed
2274 * @sense: true if request_sense is required after cancel all
2276 * This function sends a cancel all to a device to clear the queue.
2278 static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, u32 sense)
2280 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2281 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2282 struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
2283 void (*cmd_done) (struct pmcraid_cmd *) = sense ? pmcraid_erp_done
2284 : pmcraid_request_sense;
2286 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
2287 ioarcb->request_flags0 = SYNC_OVERRIDE;
2288 ioarcb->request_type = REQ_TYPE_IOACMD;
2289 ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
2291 if (RES_IS_GSCSI(res->cfg_entry))
2292 ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
2294 ioarcb->ioadl_bus_addr = 0;
2295 ioarcb->ioadl_length = 0;
2296 ioarcb->data_transfer_length = 0;
2297 ioarcb->ioarcb_bus_addr &= (~0x1FULL);
2299 /* writing to IOARRIN must be protected by host_lock, as mid-layer
2300 * schedule queuecommand while we are doing this
2302 pmcraid_send_cmd(cmd, cmd_done,
2303 PMCRAID_REQUEST_SENSE_TIMEOUT,
2304 pmcraid_timeout_handler);
2308 * pmcraid_frame_auto_sense: frame fixed format sense information
2310 * @cmd: pointer to failing command block
2312 * Return value
2313 * none
2315 static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
2317 u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
2318 struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
2319 struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
2320 u32 ioasc = le32_to_cpu(ioasa->ioasc);
2321 u32 failing_lba = 0;
2323 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
2324 cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
2326 if (RES_IS_VSET(res->cfg_entry) &&
2327 ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
2328 ioasa->u.vset.failing_lba_hi != 0) {
2330 sense_buf[0] = 0x72;
2331 sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
2332 sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
2333 sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
2335 sense_buf[7] = 12;
2336 sense_buf[8] = 0;
2337 sense_buf[9] = 0x0A;
2338 sense_buf[10] = 0x80;
2340 failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
2342 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
2343 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
2344 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
2345 sense_buf[15] = failing_lba & 0x000000ff;
2347 failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
2349 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
2350 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
2351 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
2352 sense_buf[19] = failing_lba & 0x000000ff;
2353 } else {
2354 sense_buf[0] = 0x70;
2355 sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
2356 sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
2357 sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
2359 if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
2360 if (RES_IS_VSET(res->cfg_entry))
2361 failing_lba =
2362 le32_to_cpu(ioasa->u.
2363 vset.failing_lba_lo);
2364 sense_buf[0] |= 0x80;
2365 sense_buf[3] = (failing_lba >> 24) & 0xff;
2366 sense_buf[4] = (failing_lba >> 16) & 0xff;
2367 sense_buf[5] = (failing_lba >> 8) & 0xff;
2368 sense_buf[6] = failing_lba & 0xff;
2371 sense_buf[7] = 6; /* additional length */
2376 * pmcraid_error_handler - Error response handlers for a SCSI op
2377 * @cmd: pointer to pmcraid_cmd that has failed
2379 * This function determines whether or not to initiate ERP on the affected
2380 * device. This is called from a tasklet, which doesn't hold any locks.
2382 * Return value:
2383 * 0 it caller can complete the request, otherwise 1 where in error
2384 * handler itself completes the request and returns the command block
2385 * back to free-pool
2387 static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
2389 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2390 struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
2391 struct pmcraid_instance *pinstance = cmd->drv_inst;
2392 struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
2393 u32 ioasc = le32_to_cpu(ioasa->ioasc);
2394 u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
2395 u32 sense_copied = 0;
2397 if (!res) {
2398 pmcraid_info("resource pointer is NULL\n");
2399 return 0;
2402 /* If this was a SCSI read/write command keep count of errors */
2403 if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
2404 atomic_inc(&res->read_failures);
2405 else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
2406 atomic_inc(&res->write_failures);
2408 if (!RES_IS_GSCSI(res->cfg_entry) &&
2409 masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
2410 pmcraid_frame_auto_sense(cmd);
2413 /* Log IOASC/IOASA information based on user settings */
2414 pmcraid_ioasc_logger(ioasc, cmd);
2416 switch (masked_ioasc) {
2418 case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
2419 scsi_cmd->result |= (DID_ABORT << 16);
2420 break;
2422 case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
2423 case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
2424 scsi_cmd->result |= (DID_NO_CONNECT << 16);
2425 break;
2427 case PMCRAID_IOASC_NR_SYNC_REQUIRED:
2428 res->sync_reqd = 1;
2429 scsi_cmd->result |= (DID_IMM_RETRY << 16);
2430 break;
2432 case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
2433 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
2434 break;
2436 case PMCRAID_IOASC_UA_BUS_WAS_RESET:
2437 case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
2438 if (!res->reset_progress)
2439 scsi_report_bus_reset(pinstance->host,
2440 scsi_cmd->device->channel);
2441 scsi_cmd->result |= (DID_ERROR << 16);
2442 break;
2444 case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
2445 scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
2446 res->sync_reqd = 1;
2448 /* if check_condition is not active return with error otherwise
2449 * get/frame the sense buffer
2451 if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
2452 SAM_STAT_CHECK_CONDITION &&
2453 PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
2454 return 0;
2456 /* If we have auto sense data as part of IOASA pass it to
2457 * mid-layer
2459 if (ioasa->auto_sense_length != 0) {
2460 short sense_len = ioasa->auto_sense_length;
2461 int data_size = min_t(u16, le16_to_cpu(sense_len),
2462 SCSI_SENSE_BUFFERSIZE);
2464 memcpy(scsi_cmd->sense_buffer,
2465 ioasa->sense_data,
2466 data_size);
2467 sense_copied = 1;
2470 if (RES_IS_GSCSI(res->cfg_entry)) {
2471 pmcraid_cancel_all(cmd, sense_copied);
2472 } else if (sense_copied) {
2473 pmcraid_erp_done(cmd);
2474 return 0;
2475 } else {
2476 pmcraid_request_sense(cmd);
2479 return 1;
2481 case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
2482 break;
2484 default:
2485 if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
2486 scsi_cmd->result |= (DID_ERROR << 16);
2487 break;
2489 return 0;
2493 * pmcraid_reset_device - device reset handler functions
2495 * @scsi_cmd: scsi command struct
2496 * @modifier: reset modifier indicating the reset sequence to be performed
2498 * This function issues a device reset to the affected device.
2499 * A LUN reset will be sent to the device first. If that does
2500 * not work, a target reset will be sent.
2502 * Return value:
2503 * SUCCESS / FAILED
2505 static int pmcraid_reset_device(
2506 struct scsi_cmnd *scsi_cmd,
2507 unsigned long timeout,
2508 u8 modifier
2511 struct pmcraid_cmd *cmd;
2512 struct pmcraid_instance *pinstance;
2513 struct pmcraid_resource_entry *res;
2514 struct pmcraid_ioarcb *ioarcb;
2515 unsigned long lock_flags;
2516 u32 ioasc;
2518 pinstance =
2519 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
2520 res = scsi_cmd->device->hostdata;
2522 if (!res) {
2523 sdev_printk(KERN_ERR, scsi_cmd->device,
2524 "reset_device: NULL resource pointer\n");
2525 return FAILED;
2528 /* If adapter is currently going through reset/reload, return failed.
2529 * This will force the mid-layer to call _eh_bus/host reset, which
2530 * will then go to sleep and wait for the reset to complete
2532 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2533 if (pinstance->ioa_reset_in_progress ||
2534 pinstance->ioa_state == IOA_STATE_DEAD) {
2535 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2536 return FAILED;
2539 res->reset_progress = 1;
2540 pmcraid_info("Resetting %s resource with addr %x\n",
2541 ((modifier & RESET_DEVICE_LUN) ? "LUN" :
2542 ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
2543 le32_to_cpu(res->cfg_entry.resource_address));
2545 /* get a free cmd block */
2546 cmd = pmcraid_get_free_cmd(pinstance);
2548 if (cmd == NULL) {
2549 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2550 pmcraid_err("%s: no cmd blocks are available\n", __func__);
2551 return FAILED;
2554 ioarcb = &cmd->ioa_cb->ioarcb;
2555 ioarcb->resource_handle = res->cfg_entry.resource_handle;
2556 ioarcb->request_type = REQ_TYPE_IOACMD;
2557 ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
2559 /* Initialize reset modifier bits */
2560 if (modifier)
2561 modifier = ENABLE_RESET_MODIFIER | modifier;
2563 ioarcb->cdb[1] = modifier;
2565 init_completion(&cmd->wait_for_completion);
2566 cmd->completion_req = 1;
2568 pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
2569 cmd->ioa_cb->ioarcb.cdb[0],
2570 le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
2571 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
2573 pmcraid_send_cmd(cmd,
2574 pmcraid_internal_done,
2575 timeout,
2576 pmcraid_timeout_handler);
2578 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2580 /* RESET_DEVICE command completes after all pending IOARCBs are
2581 * completed. Once this command is completed, pmcraind_internal_done
2582 * will wake up the 'completion' queue.
2584 wait_for_completion(&cmd->wait_for_completion);
2586 /* complete the command here itself and return the command block
2587 * to free list
2589 pmcraid_return_cmd(cmd);
2590 res->reset_progress = 0;
2591 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
2593 /* set the return value based on the returned ioasc */
2594 return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
2598 * _pmcraid_io_done - helper for pmcraid_io_done function
2600 * @cmd: pointer to pmcraid command struct
2601 * @reslen: residual data length to be set in the ioasa
2602 * @ioasc: ioasc either returned by IOA or set by driver itself.
2604 * This function is invoked by pmcraid_io_done to complete mid-layer
2605 * scsi ops.
2607 * Return value:
2608 * 0 if caller is required to return it to free_pool. Returns 1 if
2609 * caller need not worry about freeing command block as error handler
2610 * will take care of that.
2613 static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
2615 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2616 int rc = 0;
2618 scsi_set_resid(scsi_cmd, reslen);
2620 pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
2621 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
2622 cmd->ioa_cb->ioarcb.cdb[0],
2623 ioasc, scsi_cmd->result);
2625 if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
2626 rc = pmcraid_error_handler(cmd);
2628 if (rc == 0) {
2629 scsi_dma_unmap(scsi_cmd);
2630 scsi_cmd->scsi_done(scsi_cmd);
2633 return rc;
2637 * pmcraid_io_done - SCSI completion function
2639 * @cmd: pointer to pmcraid command struct
2641 * This function is invoked by tasklet/mid-layer error handler to completing
2642 * the SCSI ops sent from mid-layer.
2644 * Return value
2645 * none
2648 static void pmcraid_io_done(struct pmcraid_cmd *cmd)
2650 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
2651 u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
2653 if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
2654 pmcraid_return_cmd(cmd);
2658 * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
2660 * @cmd: command block of the command to be aborted
2662 * Return Value:
2663 * returns pointer to command structure used as cancelling cmd
2665 static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
2667 struct pmcraid_cmd *cancel_cmd;
2668 struct pmcraid_instance *pinstance;
2669 struct pmcraid_resource_entry *res;
2671 pinstance = (struct pmcraid_instance *)cmd->drv_inst;
2672 res = cmd->scsi_cmd->device->hostdata;
2674 cancel_cmd = pmcraid_get_free_cmd(pinstance);
2676 if (cancel_cmd == NULL) {
2677 pmcraid_err("%s: no cmd blocks are available\n", __func__);
2678 return NULL;
2681 pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
2683 pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
2684 cmd->ioa_cb->ioarcb.cdb[0],
2685 cmd->ioa_cb->ioarcb.response_handle >> 2);
2687 init_completion(&cancel_cmd->wait_for_completion);
2688 cancel_cmd->completion_req = 1;
2690 pmcraid_info("command (%d) CDB[0] = %x for %x\n",
2691 le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
2692 cmd->ioa_cb->ioarcb.cdb[0],
2693 le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
2695 pmcraid_send_cmd(cancel_cmd,
2696 pmcraid_internal_done,
2697 PMCRAID_INTERNAL_TIMEOUT,
2698 pmcraid_timeout_handler);
2699 return cancel_cmd;
2703 * pmcraid_abort_complete - Waits for ABORT TASK completion
2705 * @cancel_cmd: command block use as cancelling command
2707 * Return Value:
2708 * returns SUCCESS if ABORT TASK has good completion
2709 * otherwise FAILED
2711 static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
2713 struct pmcraid_resource_entry *res;
2714 u32 ioasc;
2716 wait_for_completion(&cancel_cmd->wait_for_completion);
2717 res = cancel_cmd->u.res;
2718 cancel_cmd->u.res = NULL;
2719 ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
2721 /* If the abort task is not timed out we will get a Good completion
2722 * as sense_key, otherwise we may get one the following responses
2723 * due to subsquent bus reset or device reset. In case IOASC is
2724 * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
2726 if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
2727 ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
2728 if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
2729 res->sync_reqd = 1;
2730 ioasc = 0;
2733 /* complete the command here itself */
2734 pmcraid_return_cmd(cancel_cmd);
2735 return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
2739 * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
2741 * @scsi_cmd: scsi command struct given by mid-layer. When this is called
2742 * mid-layer ensures that no other commands are queued. This
2743 * never gets called under interrupt, but a separate eh thread.
2745 * Return value:
2746 * SUCCESS / FAILED
2748 static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
2750 struct pmcraid_instance *pinstance;
2751 struct pmcraid_cmd *cmd;
2752 struct pmcraid_resource_entry *res;
2753 unsigned long host_lock_flags;
2754 unsigned long pending_lock_flags;
2755 struct pmcraid_cmd *cancel_cmd = NULL;
2756 int cmd_found = 0;
2757 int rc = FAILED;
2759 pinstance =
2760 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
2762 scmd_printk(KERN_INFO, scsi_cmd,
2763 "I/O command timed out, aborting it.\n");
2765 res = scsi_cmd->device->hostdata;
2767 if (res == NULL)
2768 return rc;
2770 /* If we are currently going through reset/reload, return failed.
2771 * This will force the mid-layer to eventually call
2772 * pmcraid_eh_host_reset which will then go to sleep and wait for the
2773 * reset to complete
2775 spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
2777 if (pinstance->ioa_reset_in_progress ||
2778 pinstance->ioa_state == IOA_STATE_DEAD) {
2779 spin_unlock_irqrestore(pinstance->host->host_lock,
2780 host_lock_flags);
2781 return rc;
2784 /* loop over pending cmd list to find cmd corresponding to this
2785 * scsi_cmd. Note that this command might not have been completed
2786 * already. locking: all pending commands are protected with
2787 * pending_pool_lock.
2789 spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
2790 list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
2792 if (cmd->scsi_cmd == scsi_cmd) {
2793 cmd_found = 1;
2794 break;
2798 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
2799 pending_lock_flags);
2801 /* If the command to be aborted was given to IOA and still pending with
2802 * it, send ABORT_TASK to abort this and wait for its completion
2804 if (cmd_found)
2805 cancel_cmd = pmcraid_abort_cmd(cmd);
2807 spin_unlock_irqrestore(pinstance->host->host_lock,
2808 host_lock_flags);
2810 if (cancel_cmd) {
2811 cancel_cmd->u.res = cmd->scsi_cmd->device->hostdata;
2812 rc = pmcraid_abort_complete(cancel_cmd);
2815 return cmd_found ? rc : SUCCESS;
2819 * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
2821 * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
2823 * All these routines invokve pmcraid_reset_device with appropriate parameters.
2824 * Since these are called from mid-layer EH thread, no other IO will be queued
2825 * to the resource being reset. However, control path (IOCTL) may be active so
2826 * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
2827 * takes care by locking/unlocking host_lock.
2829 * Return value
2830 * SUCCESS or FAILED
2832 static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
2834 scmd_printk(KERN_INFO, scmd,
2835 "resetting device due to an I/O command timeout.\n");
2836 return pmcraid_reset_device(scmd,
2837 PMCRAID_INTERNAL_TIMEOUT,
2838 RESET_DEVICE_LUN);
2841 static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
2843 scmd_printk(KERN_INFO, scmd,
2844 "Doing bus reset due to an I/O command timeout.\n");
2845 return pmcraid_reset_device(scmd,
2846 PMCRAID_RESET_BUS_TIMEOUT,
2847 RESET_DEVICE_BUS);
2850 static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
2852 scmd_printk(KERN_INFO, scmd,
2853 "Doing target reset due to an I/O command timeout.\n");
2854 return pmcraid_reset_device(scmd,
2855 PMCRAID_INTERNAL_TIMEOUT,
2856 RESET_DEVICE_TARGET);
2860 * pmcraid_eh_host_reset_handler - adapter reset handler callback
2862 * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
2864 * Initiates adapter reset to bring it up to operational state
2866 * Return value
2867 * SUCCESS or FAILED
2869 static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
2871 unsigned long interval = 10000; /* 10 seconds interval */
2872 int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
2873 struct pmcraid_instance *pinstance =
2874 (struct pmcraid_instance *)(scmd->device->host->hostdata);
2877 /* wait for an additional 150 seconds just in case firmware could come
2878 * up and if it could complete all the pending commands excluding the
2879 * two HCAM (CCN and LDN).
2881 while (waits--) {
2882 if (atomic_read(&pinstance->outstanding_cmds) <=
2883 PMCRAID_MAX_HCAM_CMD)
2884 return SUCCESS;
2885 msleep(interval);
2888 dev_err(&pinstance->pdev->dev,
2889 "Adapter being reset due to an I/O command timeout.\n");
2890 return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
2894 * pmcraid_task_attributes - Translate SPI Q-Tags to task attributes
2895 * @scsi_cmd: scsi command struct
2897 * Return value
2898 * number of tags or 0 if the task is not tagged
2900 static u8 pmcraid_task_attributes(struct scsi_cmnd *scsi_cmd)
2902 char tag[2];
2903 u8 rc = 0;
2905 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
2906 switch (tag[0]) {
2907 case MSG_SIMPLE_TAG:
2908 rc = TASK_TAG_SIMPLE;
2909 break;
2910 case MSG_HEAD_TAG:
2911 rc = TASK_TAG_QUEUE_HEAD;
2912 break;
2913 case MSG_ORDERED_TAG:
2914 rc = TASK_TAG_ORDERED;
2915 break;
2919 return rc;
2924 * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
2925 * @cmd: pmcraid command struct
2926 * @sgcount: count of scatter-gather elements
2928 * Return value
2929 * returns pointer pmcraid_ioadl_desc, initialized to point to internal
2930 * or external IOADLs
2932 struct pmcraid_ioadl_desc *
2933 pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
2935 struct pmcraid_ioadl_desc *ioadl;
2936 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2937 int ioadl_count = 0;
2939 if (ioarcb->add_cmd_param_length)
2940 ioadl_count = DIV_ROUND_UP(ioarcb->add_cmd_param_length, 16);
2941 ioarcb->ioadl_length =
2942 sizeof(struct pmcraid_ioadl_desc) * sgcount;
2944 if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
2945 /* external ioadls start at offset 0x80 from control_block
2946 * structure, re-using 24 out of 27 ioadls part of IOARCB.
2947 * It is necessary to indicate to firmware that driver is
2948 * using ioadls to be treated as external to IOARCB.
2950 ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
2951 ioarcb->ioadl_bus_addr =
2952 cpu_to_le64((cmd->ioa_cb_bus_addr) +
2953 offsetof(struct pmcraid_ioarcb,
2954 add_data.u.ioadl[3]));
2955 ioadl = &ioarcb->add_data.u.ioadl[3];
2956 } else {
2957 ioarcb->ioadl_bus_addr =
2958 cpu_to_le64((cmd->ioa_cb_bus_addr) +
2959 offsetof(struct pmcraid_ioarcb,
2960 add_data.u.ioadl[ioadl_count]));
2962 ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
2963 ioarcb->ioarcb_bus_addr |=
2964 DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8);
2967 return ioadl;
2971 * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
2972 * @pinstance: pointer to adapter instance structure
2973 * @cmd: pmcraid command struct
2975 * This function is invoked by queuecommand entry point while sending a command
2976 * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
2978 * Return value:
2979 * 0 on success or -1 on failure
2981 static int pmcraid_build_ioadl(
2982 struct pmcraid_instance *pinstance,
2983 struct pmcraid_cmd *cmd
2986 int i, nseg;
2987 struct scatterlist *sglist;
2989 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2990 struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
2991 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
2993 u32 length = scsi_bufflen(scsi_cmd);
2995 if (!length)
2996 return 0;
2998 nseg = scsi_dma_map(scsi_cmd);
3000 if (nseg < 0) {
3001 scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
3002 return -1;
3003 } else if (nseg > PMCRAID_MAX_IOADLS) {
3004 scsi_dma_unmap(scsi_cmd);
3005 scmd_printk(KERN_ERR, scsi_cmd,
3006 "sg count is (%d) more than allowed!\n", nseg);
3007 return -1;
3010 /* Initialize IOARCB data transfer length fields */
3011 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
3012 ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
3014 ioarcb->request_flags0 |= NO_LINK_DESCS;
3015 ioarcb->data_transfer_length = cpu_to_le32(length);
3016 ioadl = pmcraid_init_ioadls(cmd, nseg);
3018 /* Initialize IOADL descriptor addresses */
3019 scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
3020 ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
3021 ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
3022 ioadl[i].flags = 0;
3024 /* setup last descriptor */
3025 ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
3027 return 0;
3031 * pmcraid_free_sglist - Frees an allocated SG buffer list
3032 * @sglist: scatter/gather list pointer
3034 * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
3036 * Return value:
3037 * none
3039 static void pmcraid_free_sglist(struct pmcraid_sglist *sglist)
3041 int i;
3043 for (i = 0; i < sglist->num_sg; i++)
3044 __free_pages(sg_page(&(sglist->scatterlist[i])),
3045 sglist->order);
3047 kfree(sglist);
3051 * pmcraid_alloc_sglist - Allocates memory for a SG list
3052 * @buflen: buffer length
3054 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3055 * list.
3057 * Return value
3058 * pointer to sglist / NULL on failure
3060 static struct pmcraid_sglist *pmcraid_alloc_sglist(int buflen)
3062 struct pmcraid_sglist *sglist;
3063 struct scatterlist *scatterlist;
3064 struct page *page;
3065 int num_elem, i, j;
3066 int sg_size;
3067 int order;
3068 int bsize_elem;
3070 sg_size = buflen / (PMCRAID_MAX_IOADLS - 1);
3071 order = (sg_size > 0) ? get_order(sg_size) : 0;
3072 bsize_elem = PAGE_SIZE * (1 << order);
3074 /* Determine the actual number of sg entries needed */
3075 if (buflen % bsize_elem)
3076 num_elem = (buflen / bsize_elem) + 1;
3077 else
3078 num_elem = buflen / bsize_elem;
3080 /* Allocate a scatter/gather list for the DMA */
3081 sglist = kzalloc(sizeof(struct pmcraid_sglist) +
3082 (sizeof(struct scatterlist) * (num_elem - 1)),
3083 GFP_KERNEL);
3085 if (sglist == NULL)
3086 return NULL;
3088 scatterlist = sglist->scatterlist;
3089 sg_init_table(scatterlist, num_elem);
3090 sglist->order = order;
3091 sglist->num_sg = num_elem;
3092 sg_size = buflen;
3094 for (i = 0; i < num_elem; i++) {
3095 page = alloc_pages(GFP_KERNEL|GFP_DMA, order);
3096 if (!page) {
3097 for (j = i - 1; j >= 0; j--)
3098 __free_pages(sg_page(&scatterlist[j]), order);
3099 kfree(sglist);
3100 return NULL;
3103 sg_set_page(&scatterlist[i], page,
3104 sg_size < bsize_elem ? sg_size : bsize_elem, 0);
3105 sg_size -= bsize_elem;
3108 return sglist;
3112 * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
3113 * @sglist: scatter/gather list pointer
3114 * @buffer: buffer pointer
3115 * @len: buffer length
3116 * @direction: data transfer direction
3118 * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
3120 * Return value:
3121 * 0 on success / other on failure
3123 static int pmcraid_copy_sglist(
3124 struct pmcraid_sglist *sglist,
3125 unsigned long buffer,
3126 u32 len,
3127 int direction
3130 struct scatterlist *scatterlist;
3131 void *kaddr;
3132 int bsize_elem;
3133 int i;
3134 int rc = 0;
3136 /* Determine the actual number of bytes per element */
3137 bsize_elem = PAGE_SIZE * (1 << sglist->order);
3139 scatterlist = sglist->scatterlist;
3141 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
3142 struct page *page = sg_page(&scatterlist[i]);
3144 kaddr = kmap(page);
3145 if (direction == DMA_TO_DEVICE)
3146 rc = __copy_from_user(kaddr,
3147 (void *)buffer,
3148 bsize_elem);
3149 else
3150 rc = __copy_to_user((void *)buffer, kaddr, bsize_elem);
3152 kunmap(page);
3154 if (rc) {
3155 pmcraid_err("failed to copy user data into sg list\n");
3156 return -EFAULT;
3159 scatterlist[i].length = bsize_elem;
3162 if (len % bsize_elem) {
3163 struct page *page = sg_page(&scatterlist[i]);
3165 kaddr = kmap(page);
3167 if (direction == DMA_TO_DEVICE)
3168 rc = __copy_from_user(kaddr,
3169 (void *)buffer,
3170 len % bsize_elem);
3171 else
3172 rc = __copy_to_user((void *)buffer,
3173 kaddr,
3174 len % bsize_elem);
3176 kunmap(page);
3178 scatterlist[i].length = len % bsize_elem;
3181 if (rc) {
3182 pmcraid_err("failed to copy user data into sg list\n");
3183 rc = -EFAULT;
3186 return rc;
3190 * pmcraid_queuecommand - Queue a mid-layer request
3191 * @scsi_cmd: scsi command struct
3192 * @done: done function
3194 * This function queues a request generated by the mid-layer. Midlayer calls
3195 * this routine within host->lock. Some of the functions called by queuecommand
3196 * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
3198 * Return value:
3199 * 0 on success
3200 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3201 * SCSI_MLQUEUE_HOST_BUSY if host is busy
3203 static int pmcraid_queuecommand(
3204 struct scsi_cmnd *scsi_cmd,
3205 void (*done) (struct scsi_cmnd *)
3208 struct pmcraid_instance *pinstance;
3209 struct pmcraid_resource_entry *res;
3210 struct pmcraid_ioarcb *ioarcb;
3211 struct pmcraid_cmd *cmd;
3212 int rc = 0;
3214 pinstance =
3215 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
3217 scsi_cmd->scsi_done = done;
3218 res = scsi_cmd->device->hostdata;
3219 scsi_cmd->result = (DID_OK << 16);
3221 /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
3222 * the command
3224 if (pinstance->ioa_state == IOA_STATE_DEAD) {
3225 pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
3226 scsi_cmd->result = (DID_NO_CONNECT << 16);
3227 scsi_cmd->scsi_done(scsi_cmd);
3228 return 0;
3231 /* If IOA reset is in progress, can't queue the commands */
3232 if (pinstance->ioa_reset_in_progress)
3233 return SCSI_MLQUEUE_HOST_BUSY;
3235 /* initialize the command and IOARCB to be sent to IOA */
3236 cmd = pmcraid_get_free_cmd(pinstance);
3238 if (cmd == NULL) {
3239 pmcraid_err("free command block is not available\n");
3240 return SCSI_MLQUEUE_HOST_BUSY;
3243 cmd->scsi_cmd = scsi_cmd;
3244 ioarcb = &(cmd->ioa_cb->ioarcb);
3245 memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
3246 ioarcb->resource_handle = res->cfg_entry.resource_handle;
3247 ioarcb->request_type = REQ_TYPE_SCSI;
3249 cmd->cmd_done = pmcraid_io_done;
3251 if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
3252 if (scsi_cmd->underflow == 0)
3253 ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
3255 if (res->sync_reqd) {
3256 ioarcb->request_flags0 |= SYNC_COMPLETE;
3257 res->sync_reqd = 0;
3260 ioarcb->request_flags0 |= NO_LINK_DESCS;
3261 ioarcb->request_flags1 |= pmcraid_task_attributes(scsi_cmd);
3263 if (RES_IS_GSCSI(res->cfg_entry))
3264 ioarcb->request_flags1 |= DELAY_AFTER_RESET;
3267 rc = pmcraid_build_ioadl(pinstance, cmd);
3269 pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
3270 le32_to_cpu(ioarcb->response_handle) >> 2,
3271 scsi_cmd->cmnd[0], pinstance->host->unique_id,
3272 RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
3273 PMCRAID_PHYS_BUS_ID,
3274 RES_IS_VSET(res->cfg_entry) ?
3275 res->cfg_entry.unique_flags1 :
3276 RES_TARGET(res->cfg_entry.resource_address),
3277 RES_LUN(res->cfg_entry.resource_address));
3279 if (likely(rc == 0)) {
3280 _pmcraid_fire_command(cmd);
3281 } else {
3282 pmcraid_err("queuecommand could not build ioadl\n");
3283 pmcraid_return_cmd(cmd);
3284 rc = SCSI_MLQUEUE_HOST_BUSY;
3287 return rc;
3291 * pmcraid_open -char node "open" entry, allowed only users with admin access
3293 static int pmcraid_chr_open(struct inode *inode, struct file *filep)
3295 struct pmcraid_instance *pinstance;
3297 if (!capable(CAP_SYS_ADMIN))
3298 return -EACCES;
3300 /* Populate adapter instance * pointer for use by ioctl */
3301 pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
3302 filep->private_data = pinstance;
3304 return 0;
3308 * pmcraid_release - char node "release" entry point
3310 static int pmcraid_chr_release(struct inode *inode, struct file *filep)
3312 struct pmcraid_instance *pinstance =
3313 ((struct pmcraid_instance *)filep->private_data);
3315 filep->private_data = NULL;
3316 fasync_helper(-1, filep, 0, &pinstance->aen_queue);
3318 return 0;
3322 * pmcraid_fasync - Async notifier registration from applications
3324 * This function adds the calling process to a driver global queue. When an
3325 * event occurs, SIGIO will be sent to all processes in this queue.
3327 static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
3329 struct pmcraid_instance *pinstance;
3330 int rc;
3332 pinstance = (struct pmcraid_instance *)filep->private_data;
3333 mutex_lock(&pinstance->aen_queue_lock);
3334 rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
3335 mutex_unlock(&pinstance->aen_queue_lock);
3337 return rc;
3342 * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
3343 * commands sent over IOCTL interface
3345 * @cmd : pointer to struct pmcraid_cmd
3346 * @buflen : length of the request buffer
3347 * @direction : data transfer direction
3349 * Return value
3350 * 0 on success, non-zero error code on failure
3352 static int pmcraid_build_passthrough_ioadls(
3353 struct pmcraid_cmd *cmd,
3354 int buflen,
3355 int direction
3358 struct pmcraid_sglist *sglist = NULL;
3359 struct scatterlist *sg = NULL;
3360 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
3361 struct pmcraid_ioadl_desc *ioadl;
3362 int i;
3364 sglist = pmcraid_alloc_sglist(buflen);
3366 if (!sglist) {
3367 pmcraid_err("can't allocate memory for passthrough SGls\n");
3368 return -ENOMEM;
3371 sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
3372 sglist->scatterlist,
3373 sglist->num_sg, direction);
3375 if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
3376 dev_err(&cmd->drv_inst->pdev->dev,
3377 "Failed to map passthrough buffer!\n");
3378 pmcraid_free_sglist(sglist);
3379 return -EIO;
3382 cmd->sglist = sglist;
3383 ioarcb->request_flags0 |= NO_LINK_DESCS;
3385 ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
3387 /* Initialize IOADL descriptor addresses */
3388 for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
3389 ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
3390 ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
3391 ioadl[i].flags = 0;
3394 /* setup the last descriptor */
3395 ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
3397 return 0;
3402 * pmcraid_release_passthrough_ioadls - release passthrough ioadls
3404 * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
3405 * @buflen: size of the request buffer
3406 * @direction: data transfer direction
3408 * Return value
3409 * 0 on success, non-zero error code on failure
3411 static void pmcraid_release_passthrough_ioadls(
3412 struct pmcraid_cmd *cmd,
3413 int buflen,
3414 int direction
3417 struct pmcraid_sglist *sglist = cmd->sglist;
3419 if (buflen > 0) {
3420 pci_unmap_sg(cmd->drv_inst->pdev,
3421 sglist->scatterlist,
3422 sglist->num_sg,
3423 direction);
3424 pmcraid_free_sglist(sglist);
3425 cmd->sglist = NULL;
3430 * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
3432 * @pinstance: pointer to adapter instance structure
3433 * @cmd: ioctl code
3434 * @arg: pointer to pmcraid_passthrough_buffer user buffer
3436 * Return value
3437 * 0 on success, non-zero error code on failure
3439 static long pmcraid_ioctl_passthrough(
3440 struct pmcraid_instance *pinstance,
3441 unsigned int ioctl_cmd,
3442 unsigned int buflen,
3443 unsigned long arg
3446 struct pmcraid_passthrough_ioctl_buffer *buffer;
3447 struct pmcraid_ioarcb *ioarcb;
3448 struct pmcraid_cmd *cmd;
3449 struct pmcraid_cmd *cancel_cmd;
3450 unsigned long request_buffer;
3451 unsigned long request_offset;
3452 unsigned long lock_flags;
3453 int request_size;
3454 int buffer_size;
3455 u8 access, direction;
3456 int rc = 0;
3458 /* If IOA reset is in progress, wait 10 secs for reset to complete */
3459 if (pinstance->ioa_reset_in_progress) {
3460 rc = wait_event_interruptible_timeout(
3461 pinstance->reset_wait_q,
3462 !pinstance->ioa_reset_in_progress,
3463 msecs_to_jiffies(10000));
3465 if (!rc)
3466 return -ETIMEDOUT;
3467 else if (rc < 0)
3468 return -ERESTARTSYS;
3471 /* If adapter is not in operational state, return error */
3472 if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
3473 pmcraid_err("IOA is not operational\n");
3474 return -ENOTTY;
3477 buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
3478 buffer = kmalloc(buffer_size, GFP_KERNEL);
3480 if (!buffer) {
3481 pmcraid_err("no memory for passthrough buffer\n");
3482 return -ENOMEM;
3485 request_offset =
3486 offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
3488 request_buffer = arg + request_offset;
3490 rc = __copy_from_user(buffer,
3491 (struct pmcraid_passthrough_ioctl_buffer *) arg,
3492 sizeof(struct pmcraid_passthrough_ioctl_buffer));
3493 if (rc) {
3494 pmcraid_err("ioctl: can't copy passthrough buffer\n");
3495 rc = -EFAULT;
3496 goto out_free_buffer;
3499 request_size = buffer->ioarcb.data_transfer_length;
3501 if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
3502 access = VERIFY_READ;
3503 direction = DMA_TO_DEVICE;
3504 } else {
3505 access = VERIFY_WRITE;
3506 direction = DMA_FROM_DEVICE;
3509 if (request_size > 0) {
3510 rc = access_ok(access, arg, request_offset + request_size);
3512 if (!rc) {
3513 rc = -EFAULT;
3514 goto out_free_buffer;
3518 /* check if we have any additional command parameters */
3519 if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
3520 rc = -EINVAL;
3521 goto out_free_buffer;
3524 cmd = pmcraid_get_free_cmd(pinstance);
3526 if (!cmd) {
3527 pmcraid_err("free command block is not available\n");
3528 rc = -ENOMEM;
3529 goto out_free_buffer;
3532 cmd->scsi_cmd = NULL;
3533 ioarcb = &(cmd->ioa_cb->ioarcb);
3535 /* Copy the user-provided IOARCB stuff field by field */
3536 ioarcb->resource_handle = buffer->ioarcb.resource_handle;
3537 ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
3538 ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
3539 ioarcb->request_type = buffer->ioarcb.request_type;
3540 ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
3541 ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
3542 memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
3544 if (buffer->ioarcb.add_cmd_param_length) {
3545 ioarcb->add_cmd_param_length =
3546 buffer->ioarcb.add_cmd_param_length;
3547 ioarcb->add_cmd_param_offset =
3548 buffer->ioarcb.add_cmd_param_offset;
3549 memcpy(ioarcb->add_data.u.add_cmd_params,
3550 buffer->ioarcb.add_data.u.add_cmd_params,
3551 buffer->ioarcb.add_cmd_param_length);
3554 if (request_size) {
3555 rc = pmcraid_build_passthrough_ioadls(cmd,
3556 request_size,
3557 direction);
3558 if (rc) {
3559 pmcraid_err("couldn't build passthrough ioadls\n");
3560 goto out_free_buffer;
3564 /* If data is being written into the device, copy the data from user
3565 * buffers
3567 if (direction == DMA_TO_DEVICE && request_size > 0) {
3568 rc = pmcraid_copy_sglist(cmd->sglist,
3569 request_buffer,
3570 request_size,
3571 direction);
3572 if (rc) {
3573 pmcraid_err("failed to copy user buffer\n");
3574 goto out_free_sglist;
3578 /* passthrough ioctl is a blocking command so, put the user to sleep
3579 * until timeout. Note that a timeout value of 0 means, do timeout.
3581 cmd->cmd_done = pmcraid_internal_done;
3582 init_completion(&cmd->wait_for_completion);
3583 cmd->completion_req = 1;
3585 pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
3586 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
3587 cmd->ioa_cb->ioarcb.cdb[0],
3588 le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
3590 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3591 _pmcraid_fire_command(cmd);
3592 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3594 /* If command timeout is specified put caller to wait till that time,
3595 * otherwise it would be blocking wait. If command gets timed out, it
3596 * will be aborted.
3598 if (buffer->ioarcb.cmd_timeout == 0) {
3599 wait_for_completion(&cmd->wait_for_completion);
3600 } else if (!wait_for_completion_timeout(
3601 &cmd->wait_for_completion,
3602 msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
3604 pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
3605 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
3606 cmd->ioa_cb->ioarcb.cdb[0]);
3608 rc = -ETIMEDOUT;
3609 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3610 cancel_cmd = pmcraid_abort_cmd(cmd);
3611 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3613 if (cancel_cmd) {
3614 wait_for_completion(&cancel_cmd->wait_for_completion);
3615 pmcraid_return_cmd(cancel_cmd);
3618 goto out_free_sglist;
3621 /* If the command failed for any reason, copy entire IOASA buffer and
3622 * return IOCTL success. If copying IOASA to user-buffer fails, return
3623 * EFAULT
3625 if (le32_to_cpu(cmd->ioa_cb->ioasa.ioasc)) {
3627 void *ioasa =
3628 (void *)(arg +
3629 offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
3631 pmcraid_info("command failed with %x\n",
3632 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
3633 if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
3634 sizeof(struct pmcraid_ioasa))) {
3635 pmcraid_err("failed to copy ioasa buffer to user\n");
3636 rc = -EFAULT;
3639 /* If the data transfer was from device, copy the data onto user
3640 * buffers
3642 else if (direction == DMA_FROM_DEVICE && request_size > 0) {
3643 rc = pmcraid_copy_sglist(cmd->sglist,
3644 request_buffer,
3645 request_size,
3646 direction);
3647 if (rc) {
3648 pmcraid_err("failed to copy user buffer\n");
3649 rc = -EFAULT;
3653 out_free_sglist:
3654 pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
3655 pmcraid_return_cmd(cmd);
3657 out_free_buffer:
3658 kfree(buffer);
3660 return rc;
3667 * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
3669 * @pinstance: pointer to adapter instance structure
3670 * @cmd: ioctl command passed in
3671 * @buflen: length of user_buffer
3672 * @user_buffer: user buffer pointer
3674 * Return Value
3675 * 0 in case of success, otherwise appropriate error code
3677 static long pmcraid_ioctl_driver(
3678 struct pmcraid_instance *pinstance,
3679 unsigned int cmd,
3680 unsigned int buflen,
3681 void __user *user_buffer
3684 int rc = -ENOSYS;
3686 if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
3687 pmcraid_err("ioctl_driver: access fault in request buffer \n");
3688 return -EFAULT;
3691 switch (cmd) {
3692 case PMCRAID_IOCTL_RESET_ADAPTER:
3693 pmcraid_reset_bringup(pinstance);
3694 rc = 0;
3695 break;
3697 default:
3698 break;
3701 return rc;
3705 * pmcraid_check_ioctl_buffer - check for proper access to user buffer
3707 * @cmd: ioctl command
3708 * @arg: user buffer
3709 * @hdr: pointer to kernel memory for pmcraid_ioctl_header
3711 * Return Value
3712 * negetive error code if there are access issues, otherwise zero.
3713 * Upon success, returns ioctl header copied out of user buffer.
3716 static int pmcraid_check_ioctl_buffer(
3717 int cmd,
3718 void __user *arg,
3719 struct pmcraid_ioctl_header *hdr
3722 int rc = 0;
3723 int access = VERIFY_READ;
3725 if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
3726 pmcraid_err("couldn't copy ioctl header from user buffer\n");
3727 return -EFAULT;
3730 /* check for valid driver signature */
3731 rc = memcmp(hdr->signature,
3732 PMCRAID_IOCTL_SIGNATURE,
3733 sizeof(hdr->signature));
3734 if (rc) {
3735 pmcraid_err("signature verification failed\n");
3736 return -EINVAL;
3739 /* buffer length can't be negetive */
3740 if (hdr->buffer_length < 0) {
3741 pmcraid_err("ioctl: invalid buffer length specified\n");
3742 return -EINVAL;
3745 /* check for appropriate buffer access */
3746 if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
3747 access = VERIFY_WRITE;
3749 rc = access_ok(access,
3750 (arg + sizeof(struct pmcraid_ioctl_header)),
3751 hdr->buffer_length);
3752 if (!rc) {
3753 pmcraid_err("access failed for user buffer of size %d\n",
3754 hdr->buffer_length);
3755 return -EFAULT;
3758 return 0;
3762 * pmcraid_ioctl - char node ioctl entry point
3764 static long pmcraid_chr_ioctl(
3765 struct file *filep,
3766 unsigned int cmd,
3767 unsigned long arg
3770 struct pmcraid_instance *pinstance = NULL;
3771 struct pmcraid_ioctl_header *hdr = NULL;
3772 int retval = -ENOTTY;
3774 hdr = kmalloc(GFP_KERNEL, sizeof(struct pmcraid_ioctl_header));
3776 if (!hdr) {
3777 pmcraid_err("faile to allocate memory for ioctl header\n");
3778 return -ENOMEM;
3781 retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
3783 if (retval) {
3784 pmcraid_info("chr_ioctl: header check failed\n");
3785 kfree(hdr);
3786 return retval;
3789 pinstance = (struct pmcraid_instance *)filep->private_data;
3791 if (!pinstance) {
3792 pmcraid_info("adapter instance is not found\n");
3793 kfree(hdr);
3794 return -ENOTTY;
3797 switch (_IOC_TYPE(cmd)) {
3799 case PMCRAID_PASSTHROUGH_IOCTL:
3800 /* If ioctl code is to download microcode, we need to block
3801 * mid-layer requests.
3803 if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
3804 scsi_block_requests(pinstance->host);
3806 retval = pmcraid_ioctl_passthrough(pinstance,
3807 cmd,
3808 hdr->buffer_length,
3809 arg);
3811 if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
3812 scsi_unblock_requests(pinstance->host);
3813 break;
3815 case PMCRAID_DRIVER_IOCTL:
3816 arg += sizeof(struct pmcraid_ioctl_header);
3817 retval = pmcraid_ioctl_driver(pinstance,
3818 cmd,
3819 hdr->buffer_length,
3820 (void __user *)arg);
3821 break;
3823 default:
3824 retval = -ENOTTY;
3825 break;
3828 kfree(hdr);
3830 return retval;
3834 * File operations structure for management interface
3836 static const struct file_operations pmcraid_fops = {
3837 .owner = THIS_MODULE,
3838 .open = pmcraid_chr_open,
3839 .release = pmcraid_chr_release,
3840 .fasync = pmcraid_chr_fasync,
3841 .unlocked_ioctl = pmcraid_chr_ioctl,
3842 #ifdef CONFIG_COMPAT
3843 .compat_ioctl = pmcraid_chr_ioctl,
3844 #endif
3851 * pmcraid_show_log_level - Display adapter's error logging level
3852 * @dev: class device struct
3853 * @buf: buffer
3855 * Return value:
3856 * number of bytes printed to buffer
3858 static ssize_t pmcraid_show_log_level(
3859 struct device *dev,
3860 struct device_attribute *attr,
3861 char *buf)
3863 struct Scsi_Host *shost = class_to_shost(dev);
3864 struct pmcraid_instance *pinstance =
3865 (struct pmcraid_instance *)shost->hostdata;
3866 return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
3870 * pmcraid_store_log_level - Change the adapter's error logging level
3871 * @dev: class device struct
3872 * @buf: buffer
3873 * @count: not used
3875 * Return value:
3876 * number of bytes printed to buffer
3878 static ssize_t pmcraid_store_log_level(
3879 struct device *dev,
3880 struct device_attribute *attr,
3881 const char *buf,
3882 size_t count
3885 struct Scsi_Host *shost;
3886 struct pmcraid_instance *pinstance;
3887 unsigned long val;
3889 if (strict_strtoul(buf, 10, &val))
3890 return -EINVAL;
3891 /* log-level should be from 0 to 2 */
3892 if (val > 2)
3893 return -EINVAL;
3895 shost = class_to_shost(dev);
3896 pinstance = (struct pmcraid_instance *)shost->hostdata;
3897 pinstance->current_log_level = val;
3899 return strlen(buf);
3902 static struct device_attribute pmcraid_log_level_attr = {
3903 .attr = {
3904 .name = "log_level",
3905 .mode = S_IRUGO | S_IWUSR,
3907 .show = pmcraid_show_log_level,
3908 .store = pmcraid_store_log_level,
3912 * pmcraid_show_drv_version - Display driver version
3913 * @dev: class device struct
3914 * @buf: buffer
3916 * Return value:
3917 * number of bytes printed to buffer
3919 static ssize_t pmcraid_show_drv_version(
3920 struct device *dev,
3921 struct device_attribute *attr,
3922 char *buf
3925 return snprintf(buf, PAGE_SIZE, "version: %s, build date: %s\n",
3926 PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
3929 static struct device_attribute pmcraid_driver_version_attr = {
3930 .attr = {
3931 .name = "drv_version",
3932 .mode = S_IRUGO,
3934 .show = pmcraid_show_drv_version,
3938 * pmcraid_show_io_adapter_id - Display driver assigned adapter id
3939 * @dev: class device struct
3940 * @buf: buffer
3942 * Return value:
3943 * number of bytes printed to buffer
3945 static ssize_t pmcraid_show_adapter_id(
3946 struct device *dev,
3947 struct device_attribute *attr,
3948 char *buf
3951 struct Scsi_Host *shost = class_to_shost(dev);
3952 struct pmcraid_instance *pinstance =
3953 (struct pmcraid_instance *)shost->hostdata;
3954 u32 adapter_id = (pinstance->pdev->bus->number << 8) |
3955 pinstance->pdev->devfn;
3956 u32 aen_group = pmcraid_event_family.id;
3958 return snprintf(buf, PAGE_SIZE,
3959 "adapter id: %d\nminor: %d\naen group: %d\n",
3960 adapter_id, MINOR(pinstance->cdev.dev), aen_group);
3963 static struct device_attribute pmcraid_adapter_id_attr = {
3964 .attr = {
3965 .name = "adapter_id",
3966 .mode = S_IRUGO | S_IWUSR,
3968 .show = pmcraid_show_adapter_id,
3971 static struct device_attribute *pmcraid_host_attrs[] = {
3972 &pmcraid_log_level_attr,
3973 &pmcraid_driver_version_attr,
3974 &pmcraid_adapter_id_attr,
3975 NULL,
3979 /* host template structure for pmcraid driver */
3980 static struct scsi_host_template pmcraid_host_template = {
3981 .module = THIS_MODULE,
3982 .name = PMCRAID_DRIVER_NAME,
3983 .queuecommand = pmcraid_queuecommand,
3984 .eh_abort_handler = pmcraid_eh_abort_handler,
3985 .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
3986 .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
3987 .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
3988 .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
3990 .slave_alloc = pmcraid_slave_alloc,
3991 .slave_configure = pmcraid_slave_configure,
3992 .slave_destroy = pmcraid_slave_destroy,
3993 .change_queue_depth = pmcraid_change_queue_depth,
3994 .change_queue_type = pmcraid_change_queue_type,
3995 .can_queue = PMCRAID_MAX_IO_CMD,
3996 .this_id = -1,
3997 .sg_tablesize = PMCRAID_MAX_IOADLS,
3998 .max_sectors = PMCRAID_IOA_MAX_SECTORS,
3999 .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
4000 .use_clustering = ENABLE_CLUSTERING,
4001 .shost_attrs = pmcraid_host_attrs,
4002 .proc_name = PMCRAID_DRIVER_NAME
4006 * pmcraid_isr_common - Common interrupt handler routine
4008 * @pinstance: pointer to adapter instance
4009 * @intrs: active interrupts (contents of ioa_host_interrupt register)
4010 * @hrrq_id: Host RRQ index
4012 * Return Value
4013 * none
4015 static void pmcraid_isr_common(
4016 struct pmcraid_instance *pinstance,
4017 u32 intrs,
4018 int hrrq_id
4021 u32 intrs_clear =
4022 (intrs & INTRS_CRITICAL_OP_IN_PROGRESS) ? intrs
4023 : INTRS_HRRQ_VALID;
4024 iowrite32(intrs_clear,
4025 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4026 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
4028 /* hrrq valid bit was set, schedule tasklet to handle the response */
4029 if (intrs_clear == INTRS_HRRQ_VALID)
4030 tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
4034 * pmcraid_isr - implements interrupt handling routine
4036 * @irq: interrupt vector number
4037 * @dev_id: pointer hrrq_vector
4039 * Return Value
4040 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4042 static irqreturn_t pmcraid_isr(int irq, void *dev_id)
4044 struct pmcraid_isr_param *hrrq_vector;
4045 struct pmcraid_instance *pinstance;
4046 unsigned long lock_flags;
4047 u32 intrs;
4049 /* In case of legacy interrupt mode where interrupts are shared across
4050 * isrs, it may be possible that the current interrupt is not from IOA
4052 if (!dev_id) {
4053 printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
4054 return IRQ_NONE;
4057 hrrq_vector = (struct pmcraid_isr_param *)dev_id;
4058 pinstance = hrrq_vector->drv_inst;
4060 /* Acquire the lock (currently host_lock) while processing interrupts.
4061 * This interval is small as most of the response processing is done by
4062 * tasklet without the lock.
4064 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
4065 intrs = pmcraid_read_interrupts(pinstance);
4067 if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0)) {
4068 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4069 return IRQ_NONE;
4072 /* Any error interrupts including unit_check, initiate IOA reset.
4073 * In case of unit check indicate to reset_sequence that IOA unit
4074 * checked and prepare for a dump during reset sequence
4076 if (intrs & PMCRAID_ERROR_INTERRUPTS) {
4078 if (intrs & INTRS_IOA_UNIT_CHECK)
4079 pinstance->ioa_unit_check = 1;
4081 iowrite32(intrs,
4082 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4083 pmcraid_err("ISR: error interrupts: %x initiating reset\n",
4084 intrs);
4085 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
4086 pmcraid_initiate_reset(pinstance);
4087 } else {
4088 pmcraid_isr_common(pinstance, intrs, hrrq_vector->hrrq_id);
4091 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4093 return IRQ_HANDLED;
4098 * pmcraid_worker_function - worker thread function
4100 * @workp: pointer to struct work queue
4102 * Return Value
4103 * None
4106 static void pmcraid_worker_function(struct work_struct *workp)
4108 struct pmcraid_instance *pinstance;
4109 struct pmcraid_resource_entry *res;
4110 struct pmcraid_resource_entry *temp;
4111 struct scsi_device *sdev;
4112 unsigned long lock_flags;
4113 unsigned long host_lock_flags;
4114 u8 bus, target, lun;
4116 pinstance = container_of(workp, struct pmcraid_instance, worker_q);
4117 /* add resources only after host is added into system */
4118 if (!atomic_read(&pinstance->expose_resources))
4119 return;
4121 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
4122 list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
4124 if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
4125 sdev = res->scsi_dev;
4127 /* host_lock must be held before calling
4128 * scsi_device_get
4130 spin_lock_irqsave(pinstance->host->host_lock,
4131 host_lock_flags);
4132 if (!scsi_device_get(sdev)) {
4133 spin_unlock_irqrestore(
4134 pinstance->host->host_lock,
4135 host_lock_flags);
4136 pmcraid_info("deleting %x from midlayer\n",
4137 res->cfg_entry.resource_address);
4138 list_move_tail(&res->queue,
4139 &pinstance->free_res_q);
4140 spin_unlock_irqrestore(
4141 &pinstance->resource_lock,
4142 lock_flags);
4143 scsi_remove_device(sdev);
4144 scsi_device_put(sdev);
4145 spin_lock_irqsave(&pinstance->resource_lock,
4146 lock_flags);
4147 res->change_detected = 0;
4148 } else {
4149 spin_unlock_irqrestore(
4150 pinstance->host->host_lock,
4151 host_lock_flags);
4156 list_for_each_entry(res, &pinstance->used_res_q, queue) {
4158 if (res->change_detected == RES_CHANGE_ADD) {
4160 if (!pmcraid_expose_resource(&res->cfg_entry))
4161 continue;
4163 if (RES_IS_VSET(res->cfg_entry)) {
4164 bus = PMCRAID_VSET_BUS_ID;
4165 target = res->cfg_entry.unique_flags1;
4166 lun = PMCRAID_VSET_LUN_ID;
4167 } else {
4168 bus = PMCRAID_PHYS_BUS_ID;
4169 target =
4170 RES_TARGET(
4171 res->cfg_entry.resource_address);
4172 lun = RES_LUN(res->cfg_entry.resource_address);
4175 res->change_detected = 0;
4176 spin_unlock_irqrestore(&pinstance->resource_lock,
4177 lock_flags);
4178 scsi_add_device(pinstance->host, bus, target, lun);
4179 spin_lock_irqsave(&pinstance->resource_lock,
4180 lock_flags);
4184 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
4188 * pmcraid_tasklet_function - Tasklet function
4190 * @instance: pointer to msix param structure
4192 * Return Value
4193 * None
4195 void pmcraid_tasklet_function(unsigned long instance)
4197 struct pmcraid_isr_param *hrrq_vector;
4198 struct pmcraid_instance *pinstance;
4199 unsigned long hrrq_lock_flags;
4200 unsigned long pending_lock_flags;
4201 unsigned long host_lock_flags;
4202 spinlock_t *lockp; /* hrrq buffer lock */
4203 int id;
4204 u32 intrs;
4205 __le32 resp;
4207 hrrq_vector = (struct pmcraid_isr_param *)instance;
4208 pinstance = hrrq_vector->drv_inst;
4209 id = hrrq_vector->hrrq_id;
4210 lockp = &(pinstance->hrrq_lock[id]);
4211 intrs = pmcraid_read_interrupts(pinstance);
4213 /* If interrupts was as part of the ioa initialization, clear and mask
4214 * it. Delete the timer and wakeup the reset engine to proceed with
4215 * reset sequence
4217 if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
4218 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
4219 pinstance->int_regs.ioa_host_interrupt_mask_reg);
4220 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
4221 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4223 if (pinstance->reset_cmd != NULL) {
4224 del_timer(&pinstance->reset_cmd->timer);
4225 spin_lock_irqsave(pinstance->host->host_lock,
4226 host_lock_flags);
4227 pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
4228 spin_unlock_irqrestore(pinstance->host->host_lock,
4229 host_lock_flags);
4231 return;
4234 /* loop through each of the commands responded by IOA. Each HRRQ buf is
4235 * protected by its own lock. Traversals must be done within this lock
4236 * as there may be multiple tasklets running on multiple CPUs. Note
4237 * that the lock is held just for picking up the response handle and
4238 * manipulating hrrq_curr/toggle_bit values.
4240 spin_lock_irqsave(lockp, hrrq_lock_flags);
4242 resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4244 while ((resp & HRRQ_TOGGLE_BIT) ==
4245 pinstance->host_toggle_bit[id]) {
4247 int cmd_index = resp >> 2;
4248 struct pmcraid_cmd *cmd = NULL;
4250 if (cmd_index < PMCRAID_MAX_CMD) {
4251 cmd = pinstance->cmd_list[cmd_index];
4252 } else {
4253 /* In case of invalid response handle, initiate IOA
4254 * reset sequence.
4256 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4258 pmcraid_err("Invalid response %d initiating reset\n",
4259 cmd_index);
4261 spin_lock_irqsave(pinstance->host->host_lock,
4262 host_lock_flags);
4263 pmcraid_initiate_reset(pinstance);
4264 spin_unlock_irqrestore(pinstance->host->host_lock,
4265 host_lock_flags);
4267 spin_lock_irqsave(lockp, hrrq_lock_flags);
4268 break;
4271 if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
4272 pinstance->hrrq_curr[id]++;
4273 } else {
4274 pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
4275 pinstance->host_toggle_bit[id] ^= 1u;
4278 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4280 spin_lock_irqsave(&pinstance->pending_pool_lock,
4281 pending_lock_flags);
4282 list_del(&cmd->free_list);
4283 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
4284 pending_lock_flags);
4285 del_timer(&cmd->timer);
4286 atomic_dec(&pinstance->outstanding_cmds);
4288 if (cmd->cmd_done == pmcraid_ioa_reset) {
4289 spin_lock_irqsave(pinstance->host->host_lock,
4290 host_lock_flags);
4291 cmd->cmd_done(cmd);
4292 spin_unlock_irqrestore(pinstance->host->host_lock,
4293 host_lock_flags);
4294 } else if (cmd->cmd_done != NULL) {
4295 cmd->cmd_done(cmd);
4297 /* loop over until we are done with all responses */
4298 spin_lock_irqsave(lockp, hrrq_lock_flags);
4299 resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4302 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4306 * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
4307 * @pinstance: pointer to adapter instance structure
4309 * This routine un-registers registered interrupt handler and
4310 * also frees irqs/vectors.
4312 * Retun Value
4313 * None
4315 static
4316 void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
4318 free_irq(pinstance->pdev->irq, &(pinstance->hrrq_vector[0]));
4322 * pmcraid_register_interrupt_handler - registers interrupt handler
4323 * @pinstance: pointer to per-adapter instance structure
4325 * Return Value
4326 * 0 on success, non-zero error code otherwise.
4328 static int
4329 pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
4331 struct pci_dev *pdev = pinstance->pdev;
4333 pinstance->hrrq_vector[0].hrrq_id = 0;
4334 pinstance->hrrq_vector[0].drv_inst = pinstance;
4335 pinstance->hrrq_vector[0].vector = 0;
4336 pinstance->num_hrrq = 1;
4337 return request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
4338 PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
4342 * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
4343 * @pinstance: per adapter instance structure pointer
4344 * @max_index: number of buffer blocks to release
4346 * Return Value
4347 * None
4349 static void
4350 pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
4352 int i;
4353 for (i = 0; i < max_index; i++) {
4354 kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
4355 pinstance->cmd_list[i] = NULL;
4357 kmem_cache_destroy(pinstance->cmd_cachep);
4358 pinstance->cmd_cachep = NULL;
4362 * pmcraid_release_control_blocks - releases buffers alloced for control blocks
4363 * @pinstance: pointer to per adapter instance structure
4364 * @max_index: number of buffers (from 0 onwards) to release
4366 * This function assumes that the command blocks for which control blocks are
4367 * linked are not released.
4369 * Return Value
4370 * None
4372 static void
4373 pmcraid_release_control_blocks(
4374 struct pmcraid_instance *pinstance,
4375 int max_index
4378 int i;
4380 if (pinstance->control_pool == NULL)
4381 return;
4383 for (i = 0; i < max_index; i++) {
4384 pci_pool_free(pinstance->control_pool,
4385 pinstance->cmd_list[i]->ioa_cb,
4386 pinstance->cmd_list[i]->ioa_cb_bus_addr);
4387 pinstance->cmd_list[i]->ioa_cb = NULL;
4388 pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
4390 pci_pool_destroy(pinstance->control_pool);
4391 pinstance->control_pool = NULL;
4395 * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
4396 * @pinstance - pointer to per adapter instance structure
4398 * Allocates memory for command blocks using kernel slab allocator.
4400 * Return Value
4401 * 0 in case of success; -ENOMEM in case of failure
4403 static int __devinit
4404 pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
4406 int i;
4408 sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
4409 pinstance->host->unique_id);
4412 pinstance->cmd_cachep = kmem_cache_create(
4413 pinstance->cmd_pool_name,
4414 sizeof(struct pmcraid_cmd), 0,
4415 SLAB_HWCACHE_ALIGN, NULL);
4416 if (!pinstance->cmd_cachep)
4417 return -ENOMEM;
4419 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4420 pinstance->cmd_list[i] =
4421 kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
4422 if (!pinstance->cmd_list[i]) {
4423 pmcraid_release_cmd_blocks(pinstance, i);
4424 return -ENOMEM;
4427 return 0;
4431 * pmcraid_allocate_control_blocks - allocates memory control blocks
4432 * @pinstance : pointer to per adapter instance structure
4434 * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
4435 * and IOASAs. This is called after command blocks are already allocated.
4437 * Return Value
4438 * 0 in case it can allocate all control blocks, otherwise -ENOMEM
4440 static int __devinit
4441 pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
4443 int i;
4445 sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
4446 pinstance->host->unique_id);
4448 pinstance->control_pool =
4449 pci_pool_create(pinstance->ctl_pool_name,
4450 pinstance->pdev,
4451 sizeof(struct pmcraid_control_block),
4452 PMCRAID_IOARCB_ALIGNMENT, 0);
4454 if (!pinstance->control_pool)
4455 return -ENOMEM;
4457 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4458 pinstance->cmd_list[i]->ioa_cb =
4459 pci_pool_alloc(
4460 pinstance->control_pool,
4461 GFP_KERNEL,
4462 &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
4464 if (!pinstance->cmd_list[i]->ioa_cb) {
4465 pmcraid_release_control_blocks(pinstance, i);
4466 return -ENOMEM;
4468 memset(pinstance->cmd_list[i]->ioa_cb, 0,
4469 sizeof(struct pmcraid_control_block));
4471 return 0;
4475 * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
4476 * @pinstance: pointer to per adapter instance structure
4477 * @maxindex: size of hrrq buffer pointer array
4479 * Return Value
4480 * None
4482 static void
4483 pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
4485 int i;
4486 for (i = 0; i < maxindex; i++) {
4488 pci_free_consistent(pinstance->pdev,
4489 HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
4490 pinstance->hrrq_start[i],
4491 pinstance->hrrq_start_bus_addr[i]);
4493 /* reset pointers and toggle bit to zeros */
4494 pinstance->hrrq_start[i] = NULL;
4495 pinstance->hrrq_start_bus_addr[i] = 0;
4496 pinstance->host_toggle_bit[i] = 0;
4501 * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
4502 * @pinstance: pointer to per adapter instance structure
4504 * Return value
4505 * 0 hrrq buffers are allocated, -ENOMEM otherwise.
4507 static int __devinit
4508 pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
4510 int i;
4511 int buf_count = PMCRAID_MAX_CMD / pinstance->num_hrrq;
4513 for (i = 0; i < pinstance->num_hrrq; i++) {
4514 int buffer_size = HRRQ_ENTRY_SIZE * buf_count;
4516 pinstance->hrrq_start[i] =
4517 pci_alloc_consistent(
4518 pinstance->pdev,
4519 buffer_size,
4520 &(pinstance->hrrq_start_bus_addr[i]));
4522 if (pinstance->hrrq_start[i] == 0) {
4523 pmcraid_err("could not allocate host rrq: %d\n", i);
4524 pmcraid_release_host_rrqs(pinstance, i);
4525 return -ENOMEM;
4528 memset(pinstance->hrrq_start[i], 0, buffer_size);
4529 pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4530 pinstance->hrrq_end[i] =
4531 pinstance->hrrq_start[i] + buf_count - 1;
4532 pinstance->host_toggle_bit[i] = 1;
4533 spin_lock_init(&pinstance->hrrq_lock[i]);
4535 return 0;
4539 * pmcraid_release_hcams - release HCAM buffers
4541 * @pinstance: pointer to per adapter instance structure
4543 * Return value
4544 * none
4546 static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
4548 if (pinstance->ccn.msg != NULL) {
4549 pci_free_consistent(pinstance->pdev,
4550 PMCRAID_AEN_HDR_SIZE +
4551 sizeof(struct pmcraid_hcam_ccn),
4552 pinstance->ccn.msg,
4553 pinstance->ccn.baddr);
4555 pinstance->ccn.msg = NULL;
4556 pinstance->ccn.hcam = NULL;
4557 pinstance->ccn.baddr = 0;
4560 if (pinstance->ldn.msg != NULL) {
4561 pci_free_consistent(pinstance->pdev,
4562 PMCRAID_AEN_HDR_SIZE +
4563 sizeof(struct pmcraid_hcam_ldn),
4564 pinstance->ldn.msg,
4565 pinstance->ldn.baddr);
4567 pinstance->ldn.msg = NULL;
4568 pinstance->ldn.hcam = NULL;
4569 pinstance->ldn.baddr = 0;
4574 * pmcraid_allocate_hcams - allocates HCAM buffers
4575 * @pinstance : pointer to per adapter instance structure
4577 * Return Value:
4578 * 0 in case of successful allocation, non-zero otherwise
4580 static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
4582 pinstance->ccn.msg = pci_alloc_consistent(
4583 pinstance->pdev,
4584 PMCRAID_AEN_HDR_SIZE +
4585 sizeof(struct pmcraid_hcam_ccn),
4586 &(pinstance->ccn.baddr));
4588 pinstance->ldn.msg = pci_alloc_consistent(
4589 pinstance->pdev,
4590 PMCRAID_AEN_HDR_SIZE +
4591 sizeof(struct pmcraid_hcam_ldn),
4592 &(pinstance->ldn.baddr));
4594 if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
4595 pmcraid_release_hcams(pinstance);
4596 } else {
4597 pinstance->ccn.hcam =
4598 (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
4599 pinstance->ldn.hcam =
4600 (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
4602 atomic_set(&pinstance->ccn.ignore, 0);
4603 atomic_set(&pinstance->ldn.ignore, 0);
4606 return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
4610 * pmcraid_release_config_buffers - release config.table buffers
4611 * @pinstance: pointer to per adapter instance structure
4613 * Return Value
4614 * none
4616 static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
4618 if (pinstance->cfg_table != NULL &&
4619 pinstance->cfg_table_bus_addr != 0) {
4620 pci_free_consistent(pinstance->pdev,
4621 sizeof(struct pmcraid_config_table),
4622 pinstance->cfg_table,
4623 pinstance->cfg_table_bus_addr);
4624 pinstance->cfg_table = NULL;
4625 pinstance->cfg_table_bus_addr = 0;
4628 if (pinstance->res_entries != NULL) {
4629 int i;
4631 for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4632 list_del(&pinstance->res_entries[i].queue);
4633 kfree(pinstance->res_entries);
4634 pinstance->res_entries = NULL;
4637 pmcraid_release_hcams(pinstance);
4641 * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
4642 * @pinstance : pointer to per adapter instance structure
4644 * Return Value
4645 * 0 for successful allocation, -ENOMEM for any failure
4647 static int __devinit
4648 pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
4650 int i;
4652 pinstance->res_entries =
4653 kzalloc(sizeof(struct pmcraid_resource_entry) *
4654 PMCRAID_MAX_RESOURCES, GFP_KERNEL);
4656 if (NULL == pinstance->res_entries) {
4657 pmcraid_err("failed to allocate memory for resource table\n");
4658 return -ENOMEM;
4661 for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4662 list_add_tail(&pinstance->res_entries[i].queue,
4663 &pinstance->free_res_q);
4665 pinstance->cfg_table =
4666 pci_alloc_consistent(pinstance->pdev,
4667 sizeof(struct pmcraid_config_table),
4668 &pinstance->cfg_table_bus_addr);
4670 if (NULL == pinstance->cfg_table) {
4671 pmcraid_err("couldn't alloc DMA memory for config table\n");
4672 pmcraid_release_config_buffers(pinstance);
4673 return -ENOMEM;
4676 if (pmcraid_allocate_hcams(pinstance)) {
4677 pmcraid_err("could not alloc DMA memory for HCAMS\n");
4678 pmcraid_release_config_buffers(pinstance);
4679 return -ENOMEM;
4682 return 0;
4686 * pmcraid_init_tasklets - registers tasklets for response handling
4688 * @pinstance: pointer adapter instance structure
4690 * Return value
4691 * none
4693 static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
4695 int i;
4696 for (i = 0; i < pinstance->num_hrrq; i++)
4697 tasklet_init(&pinstance->isr_tasklet[i],
4698 pmcraid_tasklet_function,
4699 (unsigned long)&pinstance->hrrq_vector[i]);
4703 * pmcraid_kill_tasklets - destroys tasklets registered for response handling
4705 * @pinstance: pointer to adapter instance structure
4707 * Return value
4708 * none
4710 static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
4712 int i;
4713 for (i = 0; i < pinstance->num_hrrq; i++)
4714 tasklet_kill(&pinstance->isr_tasklet[i]);
4718 * pmcraid_init_buffers - allocates memory and initializes various structures
4719 * @pinstance: pointer to per adapter instance structure
4721 * This routine pre-allocates memory based on the type of block as below:
4722 * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
4723 * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
4724 * config-table entries : DMAable memory using pci_alloc_consistent
4725 * HostRRQs : DMAable memory, using pci_alloc_consistent
4727 * Return Value
4728 * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
4730 static int __devinit pmcraid_init_buffers(struct pmcraid_instance *pinstance)
4732 int i;
4734 if (pmcraid_allocate_host_rrqs(pinstance)) {
4735 pmcraid_err("couldn't allocate memory for %d host rrqs\n",
4736 pinstance->num_hrrq);
4737 return -ENOMEM;
4740 if (pmcraid_allocate_config_buffers(pinstance)) {
4741 pmcraid_err("couldn't allocate memory for config buffers\n");
4742 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4743 return -ENOMEM;
4746 if (pmcraid_allocate_cmd_blocks(pinstance)) {
4747 pmcraid_err("couldn't allocate memory for cmd blocks \n");
4748 pmcraid_release_config_buffers(pinstance);
4749 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4750 return -ENOMEM;
4753 if (pmcraid_allocate_control_blocks(pinstance)) {
4754 pmcraid_err("couldn't allocate memory control blocks \n");
4755 pmcraid_release_config_buffers(pinstance);
4756 pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
4757 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4758 return -ENOMEM;
4761 /* Initialize all the command blocks and add them to free pool. No
4762 * need to lock (free_pool_lock) as this is done in initialization
4763 * itself
4765 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4766 struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
4767 pmcraid_init_cmdblk(cmdp, i);
4768 cmdp->drv_inst = pinstance;
4769 list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
4772 return 0;
4776 * pmcraid_reinit_buffers - resets various buffer pointers
4777 * @pinstance: pointer to adapter instance
4778 * Return value
4779 * none
4781 static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
4783 int i;
4784 int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
4786 for (i = 0; i < pinstance->num_hrrq; i++) {
4787 memset(pinstance->hrrq_start[i], 0, buffer_size);
4788 pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4789 pinstance->hrrq_end[i] =
4790 pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
4791 pinstance->host_toggle_bit[i] = 1;
4796 * pmcraid_init_instance - initialize per instance data structure
4797 * @pdev: pointer to pci device structure
4798 * @host: pointer to Scsi_Host structure
4799 * @mapped_pci_addr: memory mapped IOA configuration registers
4801 * Return Value
4802 * 0 on success, non-zero in case of any failure
4804 static int __devinit pmcraid_init_instance(
4805 struct pci_dev *pdev,
4806 struct Scsi_Host *host,
4807 void __iomem *mapped_pci_addr
4810 struct pmcraid_instance *pinstance =
4811 (struct pmcraid_instance *)host->hostdata;
4813 pinstance->host = host;
4814 pinstance->pdev = pdev;
4816 /* Initialize register addresses */
4817 pinstance->mapped_dma_addr = mapped_pci_addr;
4819 /* Initialize chip-specific details */
4821 struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
4822 struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
4824 pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
4826 pint_regs->ioa_host_interrupt_reg =
4827 mapped_pci_addr + chip_cfg->ioa_host_intr;
4828 pint_regs->ioa_host_interrupt_clr_reg =
4829 mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
4830 pint_regs->host_ioa_interrupt_reg =
4831 mapped_pci_addr + chip_cfg->host_ioa_intr;
4832 pint_regs->host_ioa_interrupt_clr_reg =
4833 mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
4835 /* Current version of firmware exposes interrupt mask set
4836 * and mask clr registers through memory mapped bar0.
4838 pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
4839 pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
4840 pint_regs->ioa_host_interrupt_mask_reg =
4841 mapped_pci_addr + chip_cfg->ioa_host_mask;
4842 pint_regs->ioa_host_interrupt_mask_clr_reg =
4843 mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
4844 pint_regs->global_interrupt_mask_reg =
4845 mapped_pci_addr + chip_cfg->global_intr_mask;
4848 pinstance->ioa_reset_attempts = 0;
4849 init_waitqueue_head(&pinstance->reset_wait_q);
4851 atomic_set(&pinstance->outstanding_cmds, 0);
4852 atomic_set(&pinstance->expose_resources, 0);
4854 INIT_LIST_HEAD(&pinstance->free_res_q);
4855 INIT_LIST_HEAD(&pinstance->used_res_q);
4856 INIT_LIST_HEAD(&pinstance->free_cmd_pool);
4857 INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
4859 spin_lock_init(&pinstance->free_pool_lock);
4860 spin_lock_init(&pinstance->pending_pool_lock);
4861 spin_lock_init(&pinstance->resource_lock);
4862 mutex_init(&pinstance->aen_queue_lock);
4864 /* Work-queue (Shared) for deferred processing error handling */
4865 INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
4867 /* Initialize the default log_level */
4868 pinstance->current_log_level = pmcraid_log_level;
4870 /* Setup variables required for reset engine */
4871 pinstance->ioa_state = IOA_STATE_UNKNOWN;
4872 pinstance->reset_cmd = NULL;
4873 return 0;
4877 * pmcraid_release_buffers - release per-adapter buffers allocated
4879 * @pinstance: pointer to adapter soft state
4881 * Return Value
4882 * none
4884 static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
4886 pmcraid_release_config_buffers(pinstance);
4887 pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
4888 pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
4889 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4894 * pmcraid_shutdown - shutdown adapter controller.
4895 * @pdev: pci device struct
4897 * Issues an adapter shutdown to the card waits for its completion
4899 * Return value
4900 * none
4902 static void pmcraid_shutdown(struct pci_dev *pdev)
4904 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
4905 pmcraid_reset_bringdown(pinstance);
4910 * pmcraid_get_minor - returns unused minor number from minor number bitmap
4912 static unsigned short pmcraid_get_minor(void)
4914 int minor;
4916 minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
4917 __set_bit(minor, pmcraid_minor);
4918 return minor;
4922 * pmcraid_release_minor - releases given minor back to minor number bitmap
4924 static void pmcraid_release_minor(unsigned short minor)
4926 __clear_bit(minor, pmcraid_minor);
4930 * pmcraid_setup_chrdev - allocates a minor number and registers a char device
4932 * @pinstance: pointer to adapter instance for which to register device
4934 * Return value
4935 * 0 in case of success, otherwise non-zero
4937 static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
4939 int minor;
4940 int error;
4942 minor = pmcraid_get_minor();
4943 cdev_init(&pinstance->cdev, &pmcraid_fops);
4944 pinstance->cdev.owner = THIS_MODULE;
4946 error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
4948 if (error)
4949 pmcraid_release_minor(minor);
4950 else
4951 device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
4952 NULL, "pmcsas%u", minor);
4953 return error;
4957 * pmcraid_release_chrdev - unregisters per-adapter management interface
4959 * @pinstance: pointer to adapter instance structure
4961 * Return value
4962 * none
4964 static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
4966 pmcraid_release_minor(MINOR(pinstance->cdev.dev));
4967 device_destroy(pmcraid_class,
4968 MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
4969 cdev_del(&pinstance->cdev);
4973 * pmcraid_remove - IOA hot plug remove entry point
4974 * @pdev: pci device struct
4976 * Return value
4977 * none
4979 static void __devexit pmcraid_remove(struct pci_dev *pdev)
4981 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
4983 /* remove the management interface (/dev file) for this device */
4984 pmcraid_release_chrdev(pinstance);
4986 /* remove host template from scsi midlayer */
4987 scsi_remove_host(pinstance->host);
4989 /* block requests from mid-layer */
4990 scsi_block_requests(pinstance->host);
4992 /* initiate shutdown adapter */
4993 pmcraid_shutdown(pdev);
4995 pmcraid_disable_interrupts(pinstance, ~0);
4996 flush_scheduled_work();
4998 pmcraid_kill_tasklets(pinstance);
4999 pmcraid_unregister_interrupt_handler(pinstance);
5000 pmcraid_release_buffers(pinstance);
5001 iounmap(pinstance->mapped_dma_addr);
5002 pci_release_regions(pdev);
5003 scsi_host_put(pinstance->host);
5004 pci_disable_device(pdev);
5006 return;
5009 #ifdef CONFIG_PM
5011 * pmcraid_suspend - driver suspend entry point for power management
5012 * @pdev: PCI device structure
5013 * @state: PCI power state to suspend routine
5015 * Return Value - 0 always
5017 static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
5019 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5021 pmcraid_shutdown(pdev);
5022 pmcraid_disable_interrupts(pinstance, ~0);
5023 pmcraid_kill_tasklets(pinstance);
5024 pci_set_drvdata(pinstance->pdev, pinstance);
5025 pmcraid_unregister_interrupt_handler(pinstance);
5026 pci_save_state(pdev);
5027 pci_disable_device(pdev);
5028 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5030 return 0;
5034 * pmcraid_resume - driver resume entry point PCI power management
5035 * @pdev: PCI device structure
5037 * Return Value - 0 in case of success. Error code in case of any failure
5039 static int pmcraid_resume(struct pci_dev *pdev)
5041 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5042 struct Scsi_Host *host = pinstance->host;
5043 int rc;
5044 int hrrqs;
5046 pci_set_power_state(pdev, PCI_D0);
5047 pci_enable_wake(pdev, PCI_D0, 0);
5048 pci_restore_state(pdev);
5050 rc = pci_enable_device(pdev);
5052 if (rc) {
5053 dev_err(&pdev->dev, "resume: Enable device failed\n");
5054 return rc;
5057 pci_set_master(pdev);
5059 if ((sizeof(dma_addr_t) == 4) ||
5060 pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5061 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5063 if (rc == 0)
5064 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5066 if (rc != 0) {
5067 dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
5068 goto disable_device;
5071 atomic_set(&pinstance->outstanding_cmds, 0);
5072 hrrqs = pinstance->num_hrrq;
5073 rc = pmcraid_register_interrupt_handler(pinstance);
5075 if (rc) {
5076 dev_err(&pdev->dev,
5077 "resume: couldn't register interrupt handlers\n");
5078 rc = -ENODEV;
5079 goto release_host;
5082 pmcraid_init_tasklets(pinstance);
5083 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5085 /* Start with hard reset sequence which brings up IOA to operational
5086 * state as well as completes the reset sequence.
5088 pinstance->ioa_hard_reset = 1;
5090 /* Start IOA firmware initialization and bring card to Operational
5091 * state.
5093 if (pmcraid_reset_bringup(pinstance)) {
5094 dev_err(&pdev->dev, "couldn't initialize IOA \n");
5095 rc = -ENODEV;
5096 goto release_tasklets;
5099 return 0;
5101 release_tasklets:
5102 pmcraid_kill_tasklets(pinstance);
5103 pmcraid_unregister_interrupt_handler(pinstance);
5105 release_host:
5106 scsi_host_put(host);
5108 disable_device:
5109 pci_disable_device(pdev);
5111 return rc;
5114 #else
5116 #define pmcraid_suspend NULL
5117 #define pmcraid_resume NULL
5119 #endif /* CONFIG_PM */
5122 * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
5123 * completion of the ioa reset
5124 * @cmd: pointer to reset command block
5126 static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
5128 struct pmcraid_instance *pinstance = cmd->drv_inst;
5129 unsigned long flags;
5131 spin_lock_irqsave(pinstance->host->host_lock, flags);
5132 pmcraid_ioa_reset(cmd);
5133 spin_unlock_irqrestore(pinstance->host->host_lock, flags);
5134 scsi_unblock_requests(pinstance->host);
5135 schedule_work(&pinstance->worker_q);
5139 * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
5141 * @cmd: pointer to pmcraid_cmd structure
5143 * Return Value
5144 * 0 for success or non-zero for failure cases
5146 static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
5148 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5149 void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
5151 pmcraid_reinit_cmdblk(cmd);
5153 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5154 ioarcb->request_type = REQ_TYPE_IOACMD;
5155 ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
5156 ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
5158 /* If this was called as part of resource table reinitialization due to
5159 * lost CCN, it is enough to return the command block back to free pool
5160 * as part of set_supported_devs completion function.
5162 if (cmd->drv_inst->reinit_cfg_table) {
5163 cmd->drv_inst->reinit_cfg_table = 0;
5164 cmd->release = 1;
5165 cmd_done = pmcraid_reinit_cfgtable_done;
5168 /* we will be done with the reset sequence after set supported devices,
5169 * setup the done function to return the command block back to free
5170 * pool
5172 pmcraid_send_cmd(cmd,
5173 cmd_done,
5174 PMCRAID_SET_SUP_DEV_TIMEOUT,
5175 pmcraid_timeout_handler);
5176 return;
5180 * pmcraid_init_res_table - Initialize the resource table
5181 * @cmd: pointer to pmcraid command struct
5183 * This function looks through the existing resource table, comparing
5184 * it with the config table. This function will take care of old/new
5185 * devices and schedule adding/removing them from the mid-layer
5186 * as appropriate.
5188 * Return value
5189 * None
5191 static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
5193 struct pmcraid_instance *pinstance = cmd->drv_inst;
5194 struct pmcraid_resource_entry *res, *temp;
5195 struct pmcraid_config_table_entry *cfgte;
5196 unsigned long lock_flags;
5197 int found, rc, i;
5198 LIST_HEAD(old_res);
5200 if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
5201 pmcraid_err("IOA requires microcode download\n");
5203 /* resource list is protected by pinstance->resource_lock.
5204 * init_res_table can be called from probe (user-thread) or runtime
5205 * reset (timer/tasklet)
5207 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
5209 list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
5210 list_move_tail(&res->queue, &old_res);
5212 for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
5213 cfgte = &pinstance->cfg_table->entries[i];
5215 if (!pmcraid_expose_resource(cfgte))
5216 continue;
5218 found = 0;
5220 /* If this entry was already detected and initialized */
5221 list_for_each_entry_safe(res, temp, &old_res, queue) {
5223 rc = memcmp(&res->cfg_entry.resource_address,
5224 &cfgte->resource_address,
5225 sizeof(cfgte->resource_address));
5226 if (!rc) {
5227 list_move_tail(&res->queue,
5228 &pinstance->used_res_q);
5229 found = 1;
5230 break;
5234 /* If this is new entry, initialize it and add it the queue */
5235 if (!found) {
5237 if (list_empty(&pinstance->free_res_q)) {
5238 pmcraid_err("Too many devices attached\n");
5239 break;
5242 found = 1;
5243 res = list_entry(pinstance->free_res_q.next,
5244 struct pmcraid_resource_entry, queue);
5246 res->scsi_dev = NULL;
5247 res->change_detected = RES_CHANGE_ADD;
5248 res->reset_progress = 0;
5249 list_move_tail(&res->queue, &pinstance->used_res_q);
5252 /* copy new configuration table entry details into driver
5253 * maintained resource entry
5255 if (found) {
5256 memcpy(&res->cfg_entry, cfgte,
5257 sizeof(struct pmcraid_config_table_entry));
5258 pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
5259 res->cfg_entry.resource_type,
5260 res->cfg_entry.unique_flags1,
5261 le32_to_cpu(res->cfg_entry.resource_address));
5265 /* Detect any deleted entries, mark them for deletion from mid-layer */
5266 list_for_each_entry_safe(res, temp, &old_res, queue) {
5268 if (res->scsi_dev) {
5269 res->change_detected = RES_CHANGE_DEL;
5270 res->cfg_entry.resource_handle =
5271 PMCRAID_INVALID_RES_HANDLE;
5272 list_move_tail(&res->queue, &pinstance->used_res_q);
5273 } else {
5274 list_move_tail(&res->queue, &pinstance->free_res_q);
5278 /* release the resource list lock */
5279 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
5280 pmcraid_set_supported_devs(cmd);
5284 * pmcraid_querycfg - Send a Query IOA Config to the adapter.
5285 * @cmd: pointer pmcraid_cmd struct
5287 * This function sends a Query IOA Configuration command to the adapter to
5288 * retrieve the IOA configuration table.
5290 * Return value:
5291 * none
5293 static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
5295 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5296 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
5297 struct pmcraid_instance *pinstance = cmd->drv_inst;
5298 int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
5300 ioarcb->request_type = REQ_TYPE_IOACMD;
5301 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5303 ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
5305 /* firmware requires 4-byte length field, specified in B.E format */
5306 memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
5308 /* Since entire config table can be described by single IOADL, it can
5309 * be part of IOARCB itself
5311 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
5312 offsetof(struct pmcraid_ioarcb,
5313 add_data.u.ioadl[0]));
5314 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
5315 ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
5317 ioarcb->request_flags0 |= NO_LINK_DESCS;
5318 ioarcb->data_transfer_length =
5319 cpu_to_le32(sizeof(struct pmcraid_config_table));
5321 ioadl = &(ioarcb->add_data.u.ioadl[0]);
5322 ioadl->flags = IOADL_FLAGS_LAST_DESC;
5323 ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
5324 ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
5326 pmcraid_send_cmd(cmd, pmcraid_init_res_table,
5327 PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
5332 * pmcraid_probe - PCI probe entry pointer for PMC MaxRaid controller driver
5333 * @pdev: pointer to pci device structure
5334 * @dev_id: pointer to device ids structure
5336 * Return Value
5337 * returns 0 if the device is claimed and successfully configured.
5338 * returns non-zero error code in case of any failure
5340 static int __devinit pmcraid_probe(
5341 struct pci_dev *pdev,
5342 const struct pci_device_id *dev_id
5345 struct pmcraid_instance *pinstance;
5346 struct Scsi_Host *host;
5347 void __iomem *mapped_pci_addr;
5348 int rc = PCIBIOS_SUCCESSFUL;
5350 if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
5351 pmcraid_err
5352 ("maximum number(%d) of supported adapters reached\n",
5353 atomic_read(&pmcraid_adapter_count));
5354 return -ENOMEM;
5357 atomic_inc(&pmcraid_adapter_count);
5358 rc = pci_enable_device(pdev);
5360 if (rc) {
5361 dev_err(&pdev->dev, "Cannot enable adapter\n");
5362 atomic_dec(&pmcraid_adapter_count);
5363 return rc;
5366 dev_info(&pdev->dev,
5367 "Found new IOA(%x:%x), Total IOA count: %d\n",
5368 pdev->vendor, pdev->device,
5369 atomic_read(&pmcraid_adapter_count));
5371 rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
5373 if (rc < 0) {
5374 dev_err(&pdev->dev,
5375 "Couldn't register memory range of registers\n");
5376 goto out_disable_device;
5379 mapped_pci_addr = pci_iomap(pdev, 0, 0);
5381 if (!mapped_pci_addr) {
5382 dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
5383 rc = -ENOMEM;
5384 goto out_release_regions;
5387 pci_set_master(pdev);
5389 /* Firmware requires the system bus address of IOARCB to be within
5390 * 32-bit addressable range though it has 64-bit IOARRIN register.
5391 * However, firmware supports 64-bit streaming DMA buffers, whereas
5392 * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
5393 * returns memory within 4GB (if not, change this logic), coherent
5394 * buffers are within firmware acceptible address ranges.
5396 if ((sizeof(dma_addr_t) == 4) ||
5397 pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5398 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5400 /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
5401 * bit mask for pci_alloc_consistent to return addresses within 4GB
5403 if (rc == 0)
5404 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5406 if (rc != 0) {
5407 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
5408 goto cleanup_nomem;
5411 host = scsi_host_alloc(&pmcraid_host_template,
5412 sizeof(struct pmcraid_instance));
5414 if (!host) {
5415 dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
5416 rc = -ENOMEM;
5417 goto cleanup_nomem;
5420 host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
5421 host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
5422 host->unique_id = host->host_no;
5423 host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
5424 host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
5426 /* zero out entire instance structure */
5427 pinstance = (struct pmcraid_instance *)host->hostdata;
5428 memset(pinstance, 0, sizeof(*pinstance));
5430 pinstance->chip_cfg =
5431 (struct pmcraid_chip_details *)(dev_id->driver_data);
5433 rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
5435 if (rc < 0) {
5436 dev_err(&pdev->dev, "failed to initialize adapter instance\n");
5437 goto out_scsi_host_put;
5440 pci_set_drvdata(pdev, pinstance);
5442 /* Save PCI config-space for use following the reset */
5443 rc = pci_save_state(pinstance->pdev);
5445 if (rc != 0) {
5446 dev_err(&pdev->dev, "Failed to save PCI config space\n");
5447 goto out_scsi_host_put;
5450 pmcraid_disable_interrupts(pinstance, ~0);
5452 rc = pmcraid_register_interrupt_handler(pinstance);
5454 if (rc) {
5455 dev_err(&pdev->dev, "couldn't register interrupt handler\n");
5456 goto out_scsi_host_put;
5459 pmcraid_init_tasklets(pinstance);
5461 /* allocate verious buffers used by LLD.*/
5462 rc = pmcraid_init_buffers(pinstance);
5464 if (rc) {
5465 pmcraid_err("couldn't allocate memory blocks\n");
5466 goto out_unregister_isr;
5469 /* check the reset type required */
5470 pmcraid_reset_type(pinstance);
5472 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5474 /* Start IOA firmware initialization and bring card to Operational
5475 * state.
5477 pmcraid_info("starting IOA initialization sequence\n");
5478 if (pmcraid_reset_bringup(pinstance)) {
5479 dev_err(&pdev->dev, "couldn't initialize IOA \n");
5480 rc = 1;
5481 goto out_release_bufs;
5484 /* Add adapter instance into mid-layer list */
5485 rc = scsi_add_host(pinstance->host, &pdev->dev);
5486 if (rc != 0) {
5487 pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
5488 goto out_release_bufs;
5491 scsi_scan_host(pinstance->host);
5493 rc = pmcraid_setup_chrdev(pinstance);
5495 if (rc != 0) {
5496 pmcraid_err("couldn't create mgmt interface, error: %x\n",
5497 rc);
5498 goto out_remove_host;
5501 /* Schedule worker thread to handle CCN and take care of adding and
5502 * removing devices to OS
5504 atomic_set(&pinstance->expose_resources, 1);
5505 schedule_work(&pinstance->worker_q);
5506 return rc;
5508 out_remove_host:
5509 scsi_remove_host(host);
5511 out_release_bufs:
5512 pmcraid_release_buffers(pinstance);
5514 out_unregister_isr:
5515 pmcraid_kill_tasklets(pinstance);
5516 pmcraid_unregister_interrupt_handler(pinstance);
5518 out_scsi_host_put:
5519 scsi_host_put(host);
5521 cleanup_nomem:
5522 iounmap(mapped_pci_addr);
5524 out_release_regions:
5525 pci_release_regions(pdev);
5527 out_disable_device:
5528 atomic_dec(&pmcraid_adapter_count);
5529 pci_set_drvdata(pdev, NULL);
5530 pci_disable_device(pdev);
5531 return -ENODEV;
5535 * PCI driver structure of pcmraid driver
5537 static struct pci_driver pmcraid_driver = {
5538 .name = PMCRAID_DRIVER_NAME,
5539 .id_table = pmcraid_pci_table,
5540 .probe = pmcraid_probe,
5541 .remove = pmcraid_remove,
5542 .suspend = pmcraid_suspend,
5543 .resume = pmcraid_resume,
5544 .shutdown = pmcraid_shutdown
5548 * pmcraid_init - module load entry point
5550 static int __init pmcraid_init(void)
5552 dev_t dev;
5553 int error;
5555 pmcraid_info("%s Device Driver version: %s %s\n",
5556 PMCRAID_DRIVER_NAME,
5557 PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
5559 error = alloc_chrdev_region(&dev, 0,
5560 PMCRAID_MAX_ADAPTERS,
5561 PMCRAID_DEVFILE);
5563 if (error) {
5564 pmcraid_err("failed to get a major number for adapters\n");
5565 goto out_init;
5568 pmcraid_major = MAJOR(dev);
5569 pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
5571 if (IS_ERR(pmcraid_class)) {
5572 error = PTR_ERR(pmcraid_class);
5573 pmcraid_err("failed to register with with sysfs, error = %x\n",
5574 error);
5575 goto out_unreg_chrdev;
5578 error = pmcraid_netlink_init();
5580 if (error)
5581 goto out_unreg_chrdev;
5583 error = pci_register_driver(&pmcraid_driver);
5585 if (error == 0)
5586 goto out_init;
5588 pmcraid_err("failed to register pmcraid driver, error = %x\n",
5589 error);
5590 class_destroy(pmcraid_class);
5591 pmcraid_netlink_release();
5593 out_unreg_chrdev:
5594 unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
5596 out_init:
5597 return error;
5601 * pmcraid_exit - module unload entry point
5603 static void __exit pmcraid_exit(void)
5605 pmcraid_netlink_release();
5606 class_destroy(pmcraid_class);
5607 unregister_chrdev_region(MKDEV(pmcraid_major, 0),
5608 PMCRAID_MAX_ADAPTERS);
5609 pci_unregister_driver(&pmcraid_driver);
5612 module_init(pmcraid_init);
5613 module_exit(pmcraid_exit);