thinkpad-acpi: explain errors from acpi_install_notify_handler
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / pmcraid.c
blob9ab8c86fc39af61c9966a77a55d813e8ce6da456
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
282 * Return value
283 * actual depth set
285 static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth)
287 if (depth > PMCRAID_MAX_CMD_PER_LUN)
288 depth = PMCRAID_MAX_CMD_PER_LUN;
290 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), depth);
292 return scsi_dev->queue_depth;
296 * pmcraid_change_queue_type - Change the device's queue type
297 * @scsi_dev: scsi device struct
298 * @tag: type of tags to use
300 * Return value:
301 * actual queue type set
303 static int pmcraid_change_queue_type(struct scsi_device *scsi_dev, int tag)
305 struct pmcraid_resource_entry *res;
307 res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
309 if ((res) && scsi_dev->tagged_supported &&
310 (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
311 scsi_set_tag_type(scsi_dev, tag);
313 if (tag)
314 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
315 else
316 scsi_deactivate_tcq(scsi_dev, scsi_dev->queue_depth);
317 } else
318 tag = 0;
320 return tag;
325 * pmcraid_init_cmdblk - initializes a command block
327 * @cmd: pointer to struct pmcraid_cmd to be initialized
328 * @index: if >=0 first time initialization; otherwise reinitialization
330 * Return Value
331 * None
333 void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
335 struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
336 dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
338 if (index >= 0) {
339 /* first time initialization (called from probe) */
340 u32 ioasa_offset =
341 offsetof(struct pmcraid_control_block, ioasa);
343 cmd->index = index;
344 ioarcb->response_handle = cpu_to_le32(index << 2);
345 ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
346 ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
347 ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
348 } else {
349 /* re-initialization of various lengths, called once command is
350 * processed by IOA
352 memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
353 ioarcb->request_flags0 = 0;
354 ioarcb->request_flags1 = 0;
355 ioarcb->cmd_timeout = 0;
356 ioarcb->ioarcb_bus_addr &= (~0x1FULL);
357 ioarcb->ioadl_bus_addr = 0;
358 ioarcb->ioadl_length = 0;
359 ioarcb->data_transfer_length = 0;
360 ioarcb->add_cmd_param_length = 0;
361 ioarcb->add_cmd_param_offset = 0;
362 cmd->ioa_cb->ioasa.ioasc = 0;
363 cmd->ioa_cb->ioasa.residual_data_length = 0;
364 cmd->u.time_left = 0;
367 cmd->cmd_done = NULL;
368 cmd->scsi_cmd = NULL;
369 cmd->release = 0;
370 cmd->completion_req = 0;
371 cmd->dma_handle = 0;
372 init_timer(&cmd->timer);
376 * pmcraid_reinit_cmdblk - reinitialize a command block
378 * @cmd: pointer to struct pmcraid_cmd to be reinitialized
380 * Return Value
381 * None
383 static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
385 pmcraid_init_cmdblk(cmd, -1);
389 * pmcraid_get_free_cmd - get a free cmd block from command block pool
390 * @pinstance: adapter instance structure
392 * Return Value:
393 * returns pointer to cmd block or NULL if no blocks are available
395 static struct pmcraid_cmd *pmcraid_get_free_cmd(
396 struct pmcraid_instance *pinstance
399 struct pmcraid_cmd *cmd = NULL;
400 unsigned long lock_flags;
402 /* free cmd block list is protected by free_pool_lock */
403 spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
405 if (!list_empty(&pinstance->free_cmd_pool)) {
406 cmd = list_entry(pinstance->free_cmd_pool.next,
407 struct pmcraid_cmd, free_list);
408 list_del(&cmd->free_list);
410 spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
412 /* Initialize the command block before giving it the caller */
413 if (cmd != NULL)
414 pmcraid_reinit_cmdblk(cmd);
415 return cmd;
419 * pmcraid_return_cmd - return a completed command block back into free pool
420 * @cmd: pointer to the command block
422 * Return Value:
423 * nothing
425 void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
427 struct pmcraid_instance *pinstance = cmd->drv_inst;
428 unsigned long lock_flags;
430 spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
431 list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
432 spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
436 * pmcraid_read_interrupts - reads IOA interrupts
438 * @pinstance: pointer to adapter instance structure
440 * Return value
441 * interrupts read from IOA
443 static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
445 return ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
449 * pmcraid_disable_interrupts - Masks and clears all specified interrupts
451 * @pinstance: pointer to per adapter instance structure
452 * @intrs: interrupts to disable
454 * Return Value
455 * None
457 static void pmcraid_disable_interrupts(
458 struct pmcraid_instance *pinstance,
459 u32 intrs
462 u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
463 u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
465 iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
466 iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
467 iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_mask_reg);
468 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
472 * pmcraid_enable_interrupts - Enables specified interrupts
474 * @pinstance: pointer to per adapter instance structure
475 * @intr: interrupts to enable
477 * Return Value
478 * None
480 static void pmcraid_enable_interrupts(
481 struct pmcraid_instance *pinstance,
482 u32 intrs
485 u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
486 u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
488 iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
489 iowrite32(~intrs, pinstance->int_regs.ioa_host_interrupt_mask_reg);
490 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
492 pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
493 ioread32(pinstance->int_regs.global_interrupt_mask_reg),
494 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
498 * pmcraid_reset_type - Determine the required reset type
499 * @pinstance: pointer to adapter instance structure
501 * IOA requires hard reset if any of the following conditions is true.
502 * 1. If HRRQ valid interrupt is not masked
503 * 2. IOA reset alert doorbell is set
504 * 3. If there are any error interrupts
506 static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
508 u32 mask;
509 u32 intrs;
510 u32 alerts;
512 mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
513 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
514 alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
516 if ((mask & INTRS_HRRQ_VALID) == 0 ||
517 (alerts & DOORBELL_IOA_RESET_ALERT) ||
518 (intrs & PMCRAID_ERROR_INTERRUPTS)) {
519 pmcraid_info("IOA requires hard reset\n");
520 pinstance->ioa_hard_reset = 1;
523 /* If unit check is active, trigger the dump */
524 if (intrs & INTRS_IOA_UNIT_CHECK)
525 pinstance->ioa_unit_check = 1;
529 * pmcraid_bist_done - completion function for PCI BIST
530 * @cmd: pointer to reset command
531 * Return Value
532 * none
535 static void pmcraid_ioa_reset(struct pmcraid_cmd *);
537 static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
539 struct pmcraid_instance *pinstance = cmd->drv_inst;
540 unsigned long lock_flags;
541 int rc;
542 u16 pci_reg;
544 rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
546 /* If PCI config space can't be accessed wait for another two secs */
547 if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
548 cmd->u.time_left > 0) {
549 pmcraid_info("BIST not complete, waiting another 2 secs\n");
550 cmd->timer.expires = jiffies + cmd->u.time_left;
551 cmd->u.time_left = 0;
552 cmd->timer.data = (unsigned long)cmd;
553 cmd->timer.function =
554 (void (*)(unsigned long))pmcraid_bist_done;
555 add_timer(&cmd->timer);
556 } else {
557 cmd->u.time_left = 0;
558 pmcraid_info("BIST is complete, proceeding with reset\n");
559 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
560 pmcraid_ioa_reset(cmd);
561 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
566 * pmcraid_start_bist - starts BIST
567 * @cmd: pointer to reset cmd
568 * Return Value
569 * none
571 static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
573 struct pmcraid_instance *pinstance = cmd->drv_inst;
574 u32 doorbells, intrs;
576 /* proceed with bist and wait for 2 seconds */
577 iowrite32(DOORBELL_IOA_START_BIST,
578 pinstance->int_regs.host_ioa_interrupt_reg);
579 doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
580 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
581 pmcraid_info("doorbells after start bist: %x intrs: %x \n",
582 doorbells, intrs);
584 cmd->u.time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
585 cmd->timer.data = (unsigned long)cmd;
586 cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
587 cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
588 add_timer(&cmd->timer);
592 * pmcraid_reset_alert_done - completion routine for reset_alert
593 * @cmd: pointer to command block used in reset sequence
594 * Return value
595 * None
597 static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
599 struct pmcraid_instance *pinstance = cmd->drv_inst;
600 u32 status = ioread32(pinstance->ioa_status);
601 unsigned long lock_flags;
603 /* if the critical operation in progress bit is set or the wait times
604 * out, invoke reset engine to proceed with hard reset. If there is
605 * some more time to wait, restart the timer
607 if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
608 cmd->u.time_left <= 0) {
609 pmcraid_info("critical op is reset proceeding with reset\n");
610 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
611 pmcraid_ioa_reset(cmd);
612 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
613 } else {
614 pmcraid_info("critical op is not yet reset waiting again\n");
615 /* restart timer if some more time is available to wait */
616 cmd->u.time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
617 cmd->timer.data = (unsigned long)cmd;
618 cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
619 cmd->timer.function =
620 (void (*)(unsigned long))pmcraid_reset_alert_done;
621 add_timer(&cmd->timer);
626 * pmcraid_reset_alert - alerts IOA for a possible reset
627 * @cmd : command block to be used for reset sequence.
629 * Return Value
630 * returns 0 if pci config-space is accessible and RESET_DOORBELL is
631 * successfully written to IOA. Returns non-zero in case pci_config_space
632 * is not accessible
634 static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
636 struct pmcraid_instance *pinstance = cmd->drv_inst;
637 u32 doorbells;
638 int rc;
639 u16 pci_reg;
641 /* If we are able to access IOA PCI config space, alert IOA that we are
642 * going to reset it soon. This enables IOA to preserv persistent error
643 * data if any. In case memory space is not accessible, proceed with
644 * BIST or slot_reset
646 rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
647 if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
649 /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
650 * reset IOA doesn't generate any interrupts when CRITICAL
651 * OPERATION bit is reset. A timer is started to wait for this
652 * bit to be reset.
654 cmd->u.time_left = PMCRAID_RESET_TIMEOUT;
655 cmd->timer.data = (unsigned long)cmd;
656 cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
657 cmd->timer.function =
658 (void (*)(unsigned long))pmcraid_reset_alert_done;
659 add_timer(&cmd->timer);
661 iowrite32(DOORBELL_IOA_RESET_ALERT,
662 pinstance->int_regs.host_ioa_interrupt_reg);
663 doorbells =
664 ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
665 pmcraid_info("doorbells after reset alert: %x\n", doorbells);
666 } else {
667 pmcraid_info("PCI config is not accessible starting BIST\n");
668 pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
669 pmcraid_start_bist(cmd);
674 * pmcraid_timeout_handler - Timeout handler for internally generated ops
676 * @cmd : pointer to command structure, that got timedout
678 * This function blocks host requests and initiates an adapter reset.
680 * Return value:
681 * None
683 static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
685 struct pmcraid_instance *pinstance = cmd->drv_inst;
686 unsigned long lock_flags;
688 dev_info(&pinstance->pdev->dev,
689 "Adapter being reset due to command timeout.\n");
691 /* Command timeouts result in hard reset sequence. The command that got
692 * timed out may be the one used as part of reset sequence. In this
693 * case restart reset sequence using the same command block even if
694 * reset is in progress. Otherwise fail this command and get a free
695 * command block to restart the reset sequence.
697 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
698 if (!pinstance->ioa_reset_in_progress) {
699 pinstance->ioa_reset_attempts = 0;
700 cmd = pmcraid_get_free_cmd(pinstance);
702 /* If we are out of command blocks, just return here itself.
703 * Some other command's timeout handler can do the reset job
705 if (cmd == NULL) {
706 spin_unlock_irqrestore(pinstance->host->host_lock,
707 lock_flags);
708 pmcraid_err("no free cmnd block for timeout handler\n");
709 return;
712 pinstance->reset_cmd = cmd;
713 pinstance->ioa_reset_in_progress = 1;
714 } else {
715 pmcraid_info("reset is already in progress\n");
717 if (pinstance->reset_cmd != cmd) {
718 /* This command should have been given to IOA, this
719 * command will be completed by fail_outstanding_cmds
720 * anyway
722 pmcraid_err("cmd is pending but reset in progress\n");
725 /* If this command was being used as part of the reset
726 * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
727 * causes fail_outstanding_commands not to return the command
728 * block back to free pool
730 if (cmd == pinstance->reset_cmd)
731 cmd->cmd_done = pmcraid_ioa_reset;
735 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
736 scsi_block_requests(pinstance->host);
737 pmcraid_reset_alert(cmd);
738 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
742 * pmcraid_internal_done - completion routine for internally generated cmds
744 * @cmd: command that got response from IOA
746 * Return Value:
747 * none
749 static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
751 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
752 cmd->ioa_cb->ioarcb.cdb[0],
753 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
755 /* Some of the internal commands are sent with callers blocking for the
756 * response. Same will be indicated as part of cmd->completion_req
757 * field. Response path needs to wake up any waiters waiting for cmd
758 * completion if this flag is set.
760 if (cmd->completion_req) {
761 cmd->completion_req = 0;
762 complete(&cmd->wait_for_completion);
765 /* most of the internal commands are completed by caller itself, so
766 * no need to return the command block back to free pool until we are
767 * required to do so (e.g once done with initialization).
769 if (cmd->release) {
770 cmd->release = 0;
771 pmcraid_return_cmd(cmd);
776 * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
778 * @cmd: command that got response from IOA
780 * This routine is called after driver re-reads configuration table due to a
781 * lost CCN. It returns the command block back to free pool and schedules
782 * worker thread to add/delete devices into the system.
784 * Return Value:
785 * none
787 static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
789 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
790 cmd->ioa_cb->ioarcb.cdb[0],
791 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
793 if (cmd->release) {
794 cmd->release = 0;
795 pmcraid_return_cmd(cmd);
797 pmcraid_info("scheduling worker for config table reinitialization\n");
798 schedule_work(&cmd->drv_inst->worker_q);
802 * pmcraid_erp_done - Process completion of SCSI error response from device
803 * @cmd: pmcraid_command
805 * This function copies the sense buffer into the scsi_cmd struct and completes
806 * scsi_cmd by calling scsi_done function.
808 * Return value:
809 * none
811 static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
813 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
814 struct pmcraid_instance *pinstance = cmd->drv_inst;
815 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
817 if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
818 scsi_cmd->result |= (DID_ERROR << 16);
819 scmd_printk(KERN_INFO, scsi_cmd,
820 "command CDB[0] = %x failed with IOASC: 0x%08X\n",
821 cmd->ioa_cb->ioarcb.cdb[0], ioasc);
824 /* if we had allocated sense buffers for request sense, copy the sense
825 * release the buffers
827 if (cmd->sense_buffer != NULL) {
828 memcpy(scsi_cmd->sense_buffer,
829 cmd->sense_buffer,
830 SCSI_SENSE_BUFFERSIZE);
831 pci_free_consistent(pinstance->pdev,
832 SCSI_SENSE_BUFFERSIZE,
833 cmd->sense_buffer, cmd->sense_buffer_dma);
834 cmd->sense_buffer = NULL;
835 cmd->sense_buffer_dma = 0;
838 scsi_dma_unmap(scsi_cmd);
839 pmcraid_return_cmd(cmd);
840 scsi_cmd->scsi_done(scsi_cmd);
844 * pmcraid_fire_command - sends an IOA command to adapter
846 * This function adds the given block into pending command list
847 * and returns without waiting
849 * @cmd : command to be sent to the device
851 * Return Value
852 * None
854 static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
856 struct pmcraid_instance *pinstance = cmd->drv_inst;
857 unsigned long lock_flags;
859 /* Add this command block to pending cmd pool. We do this prior to
860 * writting IOARCB to ioarrin because IOA might complete the command
861 * by the time we are about to add it to the list. Response handler
862 * (isr/tasklet) looks for cmb block in the pending pending list.
864 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
865 list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
866 spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
867 atomic_inc(&pinstance->outstanding_cmds);
869 /* driver writes lower 32-bit value of IOARCB address only */
870 mb();
871 iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
872 pinstance->ioarrin);
876 * pmcraid_send_cmd - fires a command to IOA
878 * This function also sets up timeout function, and command completion
879 * function
881 * @cmd: pointer to the command block to be fired to IOA
882 * @cmd_done: command completion function, called once IOA responds
883 * @timeout: timeout to wait for this command completion
884 * @timeout_func: timeout handler
886 * Return value
887 * none
889 static void pmcraid_send_cmd(
890 struct pmcraid_cmd *cmd,
891 void (*cmd_done) (struct pmcraid_cmd *),
892 unsigned long timeout,
893 void (*timeout_func) (struct pmcraid_cmd *)
896 /* initialize done function */
897 cmd->cmd_done = cmd_done;
899 if (timeout_func) {
900 /* setup timeout handler */
901 cmd->timer.data = (unsigned long)cmd;
902 cmd->timer.expires = jiffies + timeout;
903 cmd->timer.function = (void (*)(unsigned long))timeout_func;
904 add_timer(&cmd->timer);
907 /* fire the command to IOA */
908 _pmcraid_fire_command(cmd);
912 * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
914 * @cmd: pointer to the command block used as part of reset sequence
916 * Return Value
917 * None
919 static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
921 pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
922 cmd->ioa_cb->ioarcb.cdb[0],
923 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
925 /* Note that commands sent during reset require next command to be sent
926 * to IOA. Hence reinit the done function as well as timeout function
928 pmcraid_reinit_cmdblk(cmd);
929 cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
930 cmd->ioa_cb->ioarcb.resource_handle =
931 cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
932 cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
933 cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
935 /* fire shutdown command to hardware. */
936 pmcraid_info("firing normal shutdown command (%d) to IOA\n",
937 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
939 pmcraid_send_cmd(cmd, pmcraid_ioa_reset,
940 PMCRAID_SHUTDOWN_TIMEOUT,
941 pmcraid_timeout_handler);
945 * pmcraid_identify_hrrq - registers host rrq buffers with IOA
946 * @cmd: pointer to command block to be used for identify hrrq
948 * Return Value
949 * 0 in case of success, otherwise non-zero failure code
952 static void pmcraid_querycfg(struct pmcraid_cmd *);
954 static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
956 struct pmcraid_instance *pinstance = cmd->drv_inst;
957 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
958 int index = 0;
959 __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
960 u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
962 pmcraid_reinit_cmdblk(cmd);
964 /* Initialize ioarcb */
965 ioarcb->request_type = REQ_TYPE_IOACMD;
966 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
968 /* initialize the hrrq number where IOA will respond to this command */
969 ioarcb->hrrq_id = index;
970 ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
971 ioarcb->cdb[1] = index;
973 /* IOA expects 64-bit pci address to be written in B.E format
974 * (i.e cdb[2]=MSByte..cdb[9]=LSB.
976 pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb => %llx:%llx\n",
977 hrrq_addr, ioarcb->ioarcb_bus_addr);
979 memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
980 memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
982 /* Subsequent commands require HRRQ identification to be successful.
983 * Note that this gets called even during reset from SCSI mid-layer
984 * or tasklet
986 pmcraid_send_cmd(cmd, pmcraid_querycfg,
987 PMCRAID_INTERNAL_TIMEOUT,
988 pmcraid_timeout_handler);
991 static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
992 static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
995 * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
997 * @cmd: initialized command block pointer
999 * Return Value
1000 * none
1002 static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
1004 if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
1005 atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
1006 else
1007 atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
1009 pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
1013 * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
1015 * @pinstance: pointer to adapter instance structure
1016 * @type: HCAM type
1018 * Return Value
1019 * pointer to initialized pmcraid_cmd structure or NULL
1021 static struct pmcraid_cmd *pmcraid_init_hcam
1023 struct pmcraid_instance *pinstance,
1024 u8 type
1027 struct pmcraid_cmd *cmd;
1028 struct pmcraid_ioarcb *ioarcb;
1029 struct pmcraid_ioadl_desc *ioadl;
1030 struct pmcraid_hostrcb *hcam;
1031 void (*cmd_done) (struct pmcraid_cmd *);
1032 dma_addr_t dma;
1033 int rcb_size;
1035 cmd = pmcraid_get_free_cmd(pinstance);
1037 if (!cmd) {
1038 pmcraid_err("no free command blocks for hcam\n");
1039 return cmd;
1042 if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
1043 rcb_size = sizeof(struct pmcraid_hcam_ccn);
1044 cmd_done = pmcraid_process_ccn;
1045 dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
1046 hcam = &pinstance->ccn;
1047 } else {
1048 rcb_size = sizeof(struct pmcraid_hcam_ldn);
1049 cmd_done = pmcraid_process_ldn;
1050 dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
1051 hcam = &pinstance->ldn;
1054 /* initialize command pointer used for HCAM registration */
1055 hcam->cmd = cmd;
1057 ioarcb = &cmd->ioa_cb->ioarcb;
1058 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
1059 offsetof(struct pmcraid_ioarcb,
1060 add_data.u.ioadl[0]));
1061 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
1062 ioadl = ioarcb->add_data.u.ioadl;
1064 /* Initialize ioarcb */
1065 ioarcb->request_type = REQ_TYPE_HCAM;
1066 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
1067 ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
1068 ioarcb->cdb[1] = type;
1069 ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
1070 ioarcb->cdb[8] = (rcb_size) & 0xFF;
1072 ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
1074 ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
1075 ioadl[0].data_len = cpu_to_le32(rcb_size);
1076 ioadl[0].address = cpu_to_le32(dma);
1078 cmd->cmd_done = cmd_done;
1079 return cmd;
1083 * pmcraid_send_hcam - Send an HCAM to IOA
1084 * @pinstance: ioa config struct
1085 * @type: HCAM type
1087 * This function will send a Host Controlled Async command to IOA.
1089 * Return value:
1090 * none
1092 static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
1094 struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
1095 pmcraid_send_hcam_cmd(cmd);
1100 * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
1102 * @cmd: pointer to cmd that is used as cancelling command
1103 * @cmd_to_cancel: pointer to the command that needs to be cancelled
1105 static void pmcraid_prepare_cancel_cmd(
1106 struct pmcraid_cmd *cmd,
1107 struct pmcraid_cmd *cmd_to_cancel
1110 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
1111 __be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
1113 /* Get the resource handle to where the command to be aborted has been
1114 * sent.
1116 ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
1117 ioarcb->request_type = REQ_TYPE_IOACMD;
1118 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
1119 ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
1121 /* IOARCB address of the command to be cancelled is given in
1122 * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
1123 * IOARCB address are not masked.
1125 ioarcb_addr = cpu_to_be64(ioarcb_addr);
1126 memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
1130 * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
1132 * @cmd: command to be used as cancelling command
1133 * @type: HCAM type
1134 * @cmd_done: op done function for the cancelling command
1136 static void pmcraid_cancel_hcam(
1137 struct pmcraid_cmd *cmd,
1138 u8 type,
1139 void (*cmd_done) (struct pmcraid_cmd *)
1142 struct pmcraid_instance *pinstance;
1143 struct pmcraid_hostrcb *hcam;
1145 pinstance = cmd->drv_inst;
1146 hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
1147 &pinstance->ldn : &pinstance->ccn;
1149 /* prepare for cancelling previous hcam command. If the HCAM is
1150 * currently not pending with IOA, we would have hcam->cmd as non-null
1152 if (hcam->cmd == NULL)
1153 return;
1155 pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
1157 /* writing to IOARRIN must be protected by host_lock, as mid-layer
1158 * schedule queuecommand while we are doing this
1160 pmcraid_send_cmd(cmd, cmd_done,
1161 PMCRAID_INTERNAL_TIMEOUT,
1162 pmcraid_timeout_handler);
1166 * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
1168 * @cmd: command block to be used for cancelling the HCAM
1170 static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
1172 pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
1173 cmd->ioa_cb->ioarcb.cdb[0],
1174 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
1176 pmcraid_reinit_cmdblk(cmd);
1178 pmcraid_cancel_hcam(cmd,
1179 PMCRAID_HCAM_CODE_CONFIG_CHANGE,
1180 pmcraid_ioa_shutdown);
1184 * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
1186 * @cmd: command block to be used for cancelling the HCAM
1188 static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
1190 pmcraid_cancel_hcam(cmd,
1191 PMCRAID_HCAM_CODE_LOG_DATA,
1192 pmcraid_cancel_ccn);
1196 * pmcraid_expose_resource - check if the resource can be exposed to OS
1198 * @cfgte: pointer to configuration table entry of the resource
1200 * Return value:
1201 * true if resource can be added to midlayer, false(0) otherwise
1203 static int pmcraid_expose_resource(struct pmcraid_config_table_entry *cfgte)
1205 int retval = 0;
1207 if (cfgte->resource_type == RES_TYPE_VSET)
1208 retval = ((cfgte->unique_flags1 & 0xFF) < 0xFE);
1209 else if (cfgte->resource_type == RES_TYPE_GSCSI)
1210 retval = (RES_BUS(cfgte->resource_address) !=
1211 PMCRAID_VIRTUAL_ENCL_BUS_ID);
1212 return retval;
1215 /* attributes supported by pmcraid_event_family */
1216 enum {
1217 PMCRAID_AEN_ATTR_UNSPEC,
1218 PMCRAID_AEN_ATTR_EVENT,
1219 __PMCRAID_AEN_ATTR_MAX,
1221 #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
1223 /* commands supported by pmcraid_event_family */
1224 enum {
1225 PMCRAID_AEN_CMD_UNSPEC,
1226 PMCRAID_AEN_CMD_EVENT,
1227 __PMCRAID_AEN_CMD_MAX,
1229 #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
1231 static struct genl_family pmcraid_event_family = {
1232 .id = GENL_ID_GENERATE,
1233 .name = "pmcraid",
1234 .version = 1,
1235 .maxattr = PMCRAID_AEN_ATTR_MAX
1239 * pmcraid_netlink_init - registers pmcraid_event_family
1241 * Return value:
1242 * 0 if the pmcraid_event_family is successfully registered
1243 * with netlink generic, non-zero otherwise
1245 static int pmcraid_netlink_init(void)
1247 int result;
1249 result = genl_register_family(&pmcraid_event_family);
1251 if (result)
1252 return result;
1254 pmcraid_info("registered NETLINK GENERIC group: %d\n",
1255 pmcraid_event_family.id);
1257 return result;
1261 * pmcraid_netlink_release - unregisters pmcraid_event_family
1263 * Return value:
1264 * none
1266 static void pmcraid_netlink_release(void)
1268 genl_unregister_family(&pmcraid_event_family);
1272 * pmcraid_notify_aen - sends event msg to user space application
1273 * @pinstance: pointer to adapter instance structure
1274 * @type: HCAM type
1276 * Return value:
1277 * 0 if success, error value in case of any failure.
1279 static int pmcraid_notify_aen(struct pmcraid_instance *pinstance, u8 type)
1281 struct sk_buff *skb;
1282 struct pmcraid_aen_msg *aen_msg;
1283 void *msg_header;
1284 int data_size, total_size;
1285 int result;
1288 if (type == PMCRAID_HCAM_CODE_LOG_DATA) {
1289 aen_msg = pinstance->ldn.msg;
1290 data_size = pinstance->ldn.hcam->data_len;
1291 } else {
1292 aen_msg = pinstance->ccn.msg;
1293 data_size = pinstance->ccn.hcam->data_len;
1296 data_size += sizeof(struct pmcraid_hcam_hdr);
1297 aen_msg->hostno = (pinstance->host->unique_id << 16 |
1298 MINOR(pinstance->cdev.dev));
1299 aen_msg->length = data_size;
1300 data_size += sizeof(*aen_msg);
1302 total_size = nla_total_size(data_size);
1303 skb = genlmsg_new(total_size, GFP_ATOMIC);
1306 if (!skb) {
1307 pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
1308 total_size);
1309 return -ENOMEM;
1312 /* add the genetlink message header */
1313 msg_header = genlmsg_put(skb, 0, 0,
1314 &pmcraid_event_family, 0,
1315 PMCRAID_AEN_CMD_EVENT);
1316 if (!msg_header) {
1317 pmcraid_err("failed to copy command details\n");
1318 nlmsg_free(skb);
1319 return -ENOMEM;
1322 result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
1324 if (result) {
1325 pmcraid_err("failed to copy AEN attribute data \n");
1326 nlmsg_free(skb);
1327 return -EINVAL;
1330 /* send genetlink multicast message to notify appplications */
1331 result = genlmsg_end(skb, msg_header);
1333 if (result < 0) {
1334 pmcraid_err("genlmsg_end failed\n");
1335 nlmsg_free(skb);
1336 return result;
1339 result =
1340 genlmsg_multicast(skb, 0, pmcraid_event_family.id, GFP_ATOMIC);
1342 /* If there are no listeners, genlmsg_multicast may return non-zero
1343 * value.
1345 if (result)
1346 pmcraid_info("failed to send %s event message %x!\n",
1347 type == PMCRAID_HCAM_CODE_LOG_DATA ? "LDN" : "CCN",
1348 result);
1349 return result;
1353 * pmcraid_handle_config_change - Handle a config change from the adapter
1354 * @pinstance: pointer to per adapter instance structure
1356 * Return value:
1357 * none
1359 static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
1361 struct pmcraid_config_table_entry *cfg_entry;
1362 struct pmcraid_hcam_ccn *ccn_hcam;
1363 struct pmcraid_cmd *cmd;
1364 struct pmcraid_cmd *cfgcmd;
1365 struct pmcraid_resource_entry *res = NULL;
1366 u32 new_entry = 1;
1367 unsigned long lock_flags;
1368 unsigned long host_lock_flags;
1369 int rc;
1371 ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
1372 cfg_entry = &ccn_hcam->cfg_entry;
1374 pmcraid_info
1375 ("CCN(%x): %x type: %x lost: %x flags: %x res: %x:%x:%x:%x\n",
1376 pinstance->ccn.hcam->ilid,
1377 pinstance->ccn.hcam->op_code,
1378 pinstance->ccn.hcam->notification_type,
1379 pinstance->ccn.hcam->notification_lost,
1380 pinstance->ccn.hcam->flags,
1381 pinstance->host->unique_id,
1382 RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
1383 (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
1384 RES_BUS(cfg_entry->resource_address)),
1385 RES_IS_VSET(*cfg_entry) ? cfg_entry->unique_flags1 :
1386 RES_TARGET(cfg_entry->resource_address),
1387 RES_LUN(cfg_entry->resource_address));
1390 /* If this HCAM indicates a lost notification, read the config table */
1391 if (pinstance->ccn.hcam->notification_lost) {
1392 cfgcmd = pmcraid_get_free_cmd(pinstance);
1393 if (cfgcmd) {
1394 pmcraid_info("lost CCN, reading config table\b");
1395 pinstance->reinit_cfg_table = 1;
1396 pmcraid_querycfg(cfgcmd);
1397 } else {
1398 pmcraid_err("lost CCN, no free cmd for querycfg\n");
1400 goto out_notify_apps;
1403 /* If this resource is not going to be added to mid-layer, just notify
1404 * applications and return
1406 if (!pmcraid_expose_resource(cfg_entry))
1407 goto out_notify_apps;
1409 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
1410 list_for_each_entry(res, &pinstance->used_res_q, queue) {
1411 rc = memcmp(&res->cfg_entry.resource_address,
1412 &cfg_entry->resource_address,
1413 sizeof(cfg_entry->resource_address));
1414 if (!rc) {
1415 new_entry = 0;
1416 break;
1420 if (new_entry) {
1422 /* If there are more number of resources than what driver can
1423 * manage, do not notify the applications about the CCN. Just
1424 * ignore this notifications and re-register the same HCAM
1426 if (list_empty(&pinstance->free_res_q)) {
1427 spin_unlock_irqrestore(&pinstance->resource_lock,
1428 lock_flags);
1429 pmcraid_err("too many resources attached\n");
1430 spin_lock_irqsave(pinstance->host->host_lock,
1431 host_lock_flags);
1432 pmcraid_send_hcam(pinstance,
1433 PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1434 spin_unlock_irqrestore(pinstance->host->host_lock,
1435 host_lock_flags);
1436 return;
1439 res = list_entry(pinstance->free_res_q.next,
1440 struct pmcraid_resource_entry, queue);
1442 list_del(&res->queue);
1443 res->scsi_dev = NULL;
1444 res->reset_progress = 0;
1445 list_add_tail(&res->queue, &pinstance->used_res_q);
1448 memcpy(&res->cfg_entry, cfg_entry,
1449 sizeof(struct pmcraid_config_table_entry));
1451 if (pinstance->ccn.hcam->notification_type ==
1452 NOTIFICATION_TYPE_ENTRY_DELETED) {
1453 if (res->scsi_dev) {
1454 res->change_detected = RES_CHANGE_DEL;
1455 res->cfg_entry.resource_handle =
1456 PMCRAID_INVALID_RES_HANDLE;
1457 schedule_work(&pinstance->worker_q);
1458 } else {
1459 /* This may be one of the non-exposed resources */
1460 list_move_tail(&res->queue, &pinstance->free_res_q);
1462 } else if (!res->scsi_dev) {
1463 res->change_detected = RES_CHANGE_ADD;
1464 schedule_work(&pinstance->worker_q);
1466 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
1468 out_notify_apps:
1470 /* Notify configuration changes to registered applications.*/
1471 if (!pmcraid_disable_aen)
1472 pmcraid_notify_aen(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1474 cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1475 if (cmd)
1476 pmcraid_send_hcam_cmd(cmd);
1480 * pmcraid_get_error_info - return error string for an ioasc
1481 * @ioasc: ioasc code
1482 * Return Value
1483 * none
1485 static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
1487 int i;
1488 for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
1489 if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
1490 return &pmcraid_ioasc_error_table[i];
1492 return NULL;
1496 * pmcraid_ioasc_logger - log IOASC information based user-settings
1497 * @ioasc: ioasc code
1498 * @cmd: pointer to command that resulted in 'ioasc'
1500 void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
1502 struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
1504 if (error_info == NULL ||
1505 cmd->drv_inst->current_log_level < error_info->log_level)
1506 return;
1508 /* log the error string */
1509 pmcraid_err("cmd [%d] for resource %x failed with %x(%s)\n",
1510 cmd->ioa_cb->ioarcb.cdb[0],
1511 cmd->ioa_cb->ioarcb.resource_handle,
1512 le32_to_cpu(ioasc), error_info->error_string);
1516 * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
1518 * @pinstance: pointer to per adapter instance structure
1520 * Return value:
1521 * none
1523 static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
1525 struct pmcraid_hcam_ldn *hcam_ldn;
1526 u32 ioasc;
1528 hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
1530 pmcraid_info
1531 ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
1532 pinstance->ldn.hcam->ilid,
1533 pinstance->ldn.hcam->op_code,
1534 pinstance->ldn.hcam->notification_type,
1535 pinstance->ldn.hcam->notification_lost,
1536 pinstance->ldn.hcam->flags,
1537 pinstance->ldn.hcam->overlay_id);
1539 /* log only the errors, no need to log informational log entries */
1540 if (pinstance->ldn.hcam->notification_type !=
1541 NOTIFICATION_TYPE_ERROR_LOG)
1542 return;
1544 if (pinstance->ldn.hcam->notification_lost ==
1545 HOSTRCB_NOTIFICATIONS_LOST)
1546 dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
1548 ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
1550 if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
1551 ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
1552 dev_info(&pinstance->pdev->dev,
1553 "UnitAttention due to IOA Bus Reset\n");
1554 scsi_report_bus_reset(
1555 pinstance->host,
1556 RES_BUS(hcam_ldn->error_log.fd_ra));
1559 return;
1563 * pmcraid_process_ccn - Op done function for a CCN.
1564 * @cmd: pointer to command struct
1566 * This function is the op done function for a configuration
1567 * change notification
1569 * Return value:
1570 * none
1572 static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
1574 struct pmcraid_instance *pinstance = cmd->drv_inst;
1575 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1576 unsigned long lock_flags;
1578 pinstance->ccn.cmd = NULL;
1579 pmcraid_return_cmd(cmd);
1581 /* If driver initiated IOA reset happened while this hcam was pending
1582 * with IOA, or IOA bringdown sequence is in progress, no need to
1583 * re-register the hcam
1585 if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
1586 atomic_read(&pinstance->ccn.ignore) == 1) {
1587 return;
1588 } else if (ioasc) {
1589 dev_info(&pinstance->pdev->dev,
1590 "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
1591 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
1592 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1593 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
1594 } else {
1595 pmcraid_handle_config_change(pinstance);
1600 * pmcraid_process_ldn - op done function for an LDN
1601 * @cmd: pointer to command block
1603 * Return value
1604 * none
1606 static void pmcraid_initiate_reset(struct pmcraid_instance *);
1608 static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
1610 struct pmcraid_instance *pinstance = cmd->drv_inst;
1611 struct pmcraid_hcam_ldn *ldn_hcam =
1612 (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
1613 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1614 u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
1615 unsigned long lock_flags;
1617 /* return the command block back to freepool */
1618 pinstance->ldn.cmd = NULL;
1619 pmcraid_return_cmd(cmd);
1621 /* If driver initiated IOA reset happened while this hcam was pending
1622 * with IOA, no need to re-register the hcam as reset engine will do it
1623 * once reset sequence is complete
1625 if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
1626 atomic_read(&pinstance->ccn.ignore) == 1) {
1627 return;
1628 } else if (!ioasc) {
1629 pmcraid_handle_error_log(pinstance);
1630 if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
1631 spin_lock_irqsave(pinstance->host->host_lock,
1632 lock_flags);
1633 pmcraid_initiate_reset(pinstance);
1634 spin_unlock_irqrestore(pinstance->host->host_lock,
1635 lock_flags);
1636 return;
1638 } else {
1639 dev_info(&pinstance->pdev->dev,
1640 "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
1642 /* send netlink message for HCAM notification if enabled */
1643 if (!pmcraid_disable_aen)
1644 pmcraid_notify_aen(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1646 cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1647 if (cmd)
1648 pmcraid_send_hcam_cmd(cmd);
1652 * pmcraid_register_hcams - register HCAMs for CCN and LDN
1654 * @pinstance: pointer per adapter instance structure
1656 * Return Value
1657 * none
1659 static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
1661 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1662 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1666 * pmcraid_unregister_hcams - cancel HCAMs registered already
1667 * @cmd: pointer to command used as part of reset sequence
1669 static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
1671 struct pmcraid_instance *pinstance = cmd->drv_inst;
1673 /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
1674 * handling hcam response though it is not necessary. In order to
1675 * prevent this, set 'ignore', so that bring-down sequence doesn't
1676 * re-send any more hcams
1678 atomic_set(&pinstance->ccn.ignore, 1);
1679 atomic_set(&pinstance->ldn.ignore, 1);
1681 /* If adapter reset was forced as part of runtime reset sequence,
1682 * start the reset sequence.
1684 if (pinstance->force_ioa_reset && !pinstance->ioa_bringdown) {
1685 pinstance->force_ioa_reset = 0;
1686 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1687 pmcraid_reset_alert(cmd);
1688 return;
1691 /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
1692 * one after the other. So CCN cancellation will be triggered by
1693 * pmcraid_cancel_ldn itself.
1695 pmcraid_cancel_ldn(cmd);
1699 * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
1700 * @pinstance: pointer to adapter instance structure
1701 * Return Value
1702 * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
1704 static void pmcraid_reinit_buffers(struct pmcraid_instance *);
1706 static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
1708 u32 intrs;
1710 pmcraid_reinit_buffers(pinstance);
1711 intrs = pmcraid_read_interrupts(pinstance);
1713 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
1715 if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
1716 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
1717 pinstance->int_regs.ioa_host_interrupt_mask_reg);
1718 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
1719 pinstance->int_regs.ioa_host_interrupt_clr_reg);
1720 return 1;
1721 } else {
1722 return 0;
1727 * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
1728 * @cmd : pointer to reset command block
1730 * Return Value
1731 * none
1733 static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
1735 struct pmcraid_instance *pinstance = cmd->drv_inst;
1736 u32 int_reg;
1737 u32 doorbell;
1739 /* There will be an interrupt when Transition to Operational bit is
1740 * set so tasklet would execute next reset task. The timeout handler
1741 * would re-initiate a reset
1743 cmd->cmd_done = pmcraid_ioa_reset;
1744 cmd->timer.data = (unsigned long)cmd;
1745 cmd->timer.expires = jiffies +
1746 msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
1747 cmd->timer.function = (void (*)(unsigned long))pmcraid_timeout_handler;
1749 if (!timer_pending(&cmd->timer))
1750 add_timer(&cmd->timer);
1752 /* Enable destructive diagnostics on IOA if it is not yet in
1753 * operational state
1755 doorbell = DOORBELL_RUNTIME_RESET |
1756 DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
1758 iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
1759 int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
1760 pmcraid_info("Waiting for IOA to become operational %x:%x\n",
1761 ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
1762 int_reg);
1766 * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
1768 * @pinstance: pointer to adapter instance structure
1770 * Return Value
1771 * none
1773 static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
1775 pmcraid_info("%s is not yet implemented\n", __func__);
1779 * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
1780 * @pinstance: pointer to adapter instance structure
1782 * This function fails all outstanding ops. If they are submitted to IOA
1783 * already, it sends cancel all messages if IOA is still accepting IOARCBs,
1784 * otherwise just completes the commands and returns the cmd blocks to free
1785 * pool.
1787 * Return value:
1788 * none
1790 static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
1792 struct pmcraid_cmd *cmd, *temp;
1793 unsigned long lock_flags;
1795 /* pending command list is protected by pending_pool_lock. Its
1796 * traversal must be done as within this lock
1798 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
1799 list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
1800 free_list) {
1801 list_del(&cmd->free_list);
1802 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
1803 lock_flags);
1804 cmd->ioa_cb->ioasa.ioasc =
1805 cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
1806 cmd->ioa_cb->ioasa.ilid =
1807 cpu_to_be32(PMCRAID_DRIVER_ILID);
1809 /* In case the command timer is still running */
1810 del_timer(&cmd->timer);
1812 /* If this is an IO command, complete it by invoking scsi_done
1813 * function. If this is one of the internal commands other
1814 * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
1815 * complete it
1817 if (cmd->scsi_cmd) {
1819 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
1820 __le32 resp = cmd->ioa_cb->ioarcb.response_handle;
1822 scsi_cmd->result |= DID_ERROR << 16;
1824 scsi_dma_unmap(scsi_cmd);
1825 pmcraid_return_cmd(cmd);
1827 pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
1828 le32_to_cpu(resp) >> 2,
1829 cmd->ioa_cb->ioarcb.cdb[0],
1830 scsi_cmd->result);
1831 scsi_cmd->scsi_done(scsi_cmd);
1832 } else if (cmd->cmd_done == pmcraid_internal_done ||
1833 cmd->cmd_done == pmcraid_erp_done) {
1834 cmd->cmd_done(cmd);
1835 } else if (cmd->cmd_done != pmcraid_ioa_reset) {
1836 pmcraid_return_cmd(cmd);
1839 atomic_dec(&pinstance->outstanding_cmds);
1840 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
1843 spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
1847 * pmcraid_ioa_reset - Implementation of IOA reset logic
1849 * @cmd: pointer to the cmd block to be used for entire reset process
1851 * This function executes most of the steps required for IOA reset. This gets
1852 * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
1853 * 'eh_' thread. Access to variables used for controling the reset sequence is
1854 * synchronized using host lock. Various functions called during reset process
1855 * would make use of a single command block, pointer to which is also stored in
1856 * adapter instance structure.
1858 * Return Value
1859 * None
1861 static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
1863 struct pmcraid_instance *pinstance = cmd->drv_inst;
1864 u8 reset_complete = 0;
1866 pinstance->ioa_reset_in_progress = 1;
1868 if (pinstance->reset_cmd != cmd) {
1869 pmcraid_err("reset is called with different command block\n");
1870 pinstance->reset_cmd = cmd;
1873 pmcraid_info("reset_engine: state = %d, command = %p\n",
1874 pinstance->ioa_state, cmd);
1876 switch (pinstance->ioa_state) {
1878 case IOA_STATE_DEAD:
1879 /* If IOA is offline, whatever may be the reset reason, just
1880 * return. callers might be waiting on the reset wait_q, wake
1881 * up them
1883 pmcraid_err("IOA is offline no reset is possible\n");
1884 reset_complete = 1;
1885 break;
1887 case IOA_STATE_IN_BRINGDOWN:
1888 /* we enter here, once ioa shutdown command is processed by IOA
1889 * Alert IOA for a possible reset. If reset alert fails, IOA
1890 * goes through hard-reset
1892 pmcraid_disable_interrupts(pinstance, ~0);
1893 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1894 pmcraid_reset_alert(cmd);
1895 break;
1897 case IOA_STATE_UNKNOWN:
1898 /* We may be called during probe or resume. Some pre-processing
1899 * is required for prior to reset
1901 scsi_block_requests(pinstance->host);
1903 /* If asked to reset while IOA was processing responses or
1904 * there are any error responses then IOA may require
1905 * hard-reset.
1907 if (pinstance->ioa_hard_reset == 0) {
1908 if (ioread32(pinstance->ioa_status) &
1909 INTRS_TRANSITION_TO_OPERATIONAL) {
1910 pmcraid_info("sticky bit set, bring-up\n");
1911 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
1912 pmcraid_reinit_cmdblk(cmd);
1913 pmcraid_identify_hrrq(cmd);
1914 } else {
1915 pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
1916 pmcraid_soft_reset(cmd);
1918 } else {
1919 /* Alert IOA of a possible reset and wait for critical
1920 * operation in progress bit to reset
1922 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1923 pmcraid_reset_alert(cmd);
1925 break;
1927 case IOA_STATE_IN_RESET_ALERT:
1928 /* If critical operation in progress bit is reset or wait gets
1929 * timed out, reset proceeds with starting BIST on the IOA.
1930 * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
1931 * they are 3 or more, reset engine marks IOA dead and returns
1933 pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
1934 pmcraid_start_bist(cmd);
1935 break;
1937 case IOA_STATE_IN_HARD_RESET:
1938 pinstance->ioa_reset_attempts++;
1940 /* retry reset if we haven't reached maximum allowed limit */
1941 if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
1942 pinstance->ioa_reset_attempts = 0;
1943 pmcraid_err("IOA didn't respond marking it as dead\n");
1944 pinstance->ioa_state = IOA_STATE_DEAD;
1945 reset_complete = 1;
1946 break;
1949 /* Once either bist or pci reset is done, restore PCI config
1950 * space. If this fails, proceed with hard reset again
1953 if (pci_restore_state(pinstance->pdev)) {
1954 pmcraid_info("config-space error resetting again\n");
1955 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1956 pmcraid_reset_alert(cmd);
1957 break;
1960 /* fail all pending commands */
1961 pmcraid_fail_outstanding_cmds(pinstance);
1963 /* check if unit check is active, if so extract dump */
1964 if (pinstance->ioa_unit_check) {
1965 pmcraid_info("unit check is active\n");
1966 pinstance->ioa_unit_check = 0;
1967 pmcraid_get_dump(pinstance);
1968 pinstance->ioa_reset_attempts--;
1969 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1970 pmcraid_reset_alert(cmd);
1971 break;
1974 /* if the reset reason is to bring-down the ioa, we might be
1975 * done with the reset restore pci_config_space and complete
1976 * the reset
1978 if (pinstance->ioa_bringdown) {
1979 pmcraid_info("bringing down the adapter\n");
1980 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
1981 pinstance->ioa_bringdown = 0;
1982 pinstance->ioa_state = IOA_STATE_UNKNOWN;
1983 reset_complete = 1;
1984 } else {
1985 /* bring-up IOA, so proceed with soft reset
1986 * Reinitialize hrrq_buffers and their indices also
1987 * enable interrupts after a pci_restore_state
1989 if (pmcraid_reset_enable_ioa(pinstance)) {
1990 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
1991 pmcraid_info("bringing up the adapter\n");
1992 pmcraid_reinit_cmdblk(cmd);
1993 pmcraid_identify_hrrq(cmd);
1994 } else {
1995 pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
1996 pmcraid_soft_reset(cmd);
1999 break;
2001 case IOA_STATE_IN_SOFT_RESET:
2002 /* TRANSITION TO OPERATIONAL is on so start initialization
2003 * sequence
2005 pmcraid_info("In softreset proceeding with bring-up\n");
2006 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
2008 /* Initialization commands start with HRRQ identification. From
2009 * now on tasklet completes most of the commands as IOA is up
2010 * and intrs are enabled
2012 pmcraid_identify_hrrq(cmd);
2013 break;
2015 case IOA_STATE_IN_BRINGUP:
2016 /* we are done with bringing up of IOA, change the ioa_state to
2017 * operational and wake up any waiters
2019 pinstance->ioa_state = IOA_STATE_OPERATIONAL;
2020 reset_complete = 1;
2021 break;
2023 case IOA_STATE_OPERATIONAL:
2024 default:
2025 /* When IOA is operational and a reset is requested, check for
2026 * the reset reason. If reset is to bring down IOA, unregister
2027 * HCAMs and initiate shutdown; if adapter reset is forced then
2028 * restart reset sequence again
2030 if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
2031 pinstance->force_ioa_reset == 0) {
2032 reset_complete = 1;
2033 } else {
2034 if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
2035 pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
2036 pmcraid_reinit_cmdblk(cmd);
2037 pmcraid_unregister_hcams(cmd);
2039 break;
2042 /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
2043 * OPERATIONAL. Reset all control variables used during reset, wake up
2044 * any waiting threads and let the SCSI mid-layer send commands. Note
2045 * that host_lock must be held before invoking scsi_report_bus_reset.
2047 if (reset_complete) {
2048 pinstance->ioa_reset_in_progress = 0;
2049 pinstance->ioa_reset_attempts = 0;
2050 pinstance->reset_cmd = NULL;
2051 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2052 pinstance->ioa_bringdown = 0;
2053 pmcraid_return_cmd(cmd);
2055 /* If target state is to bring up the adapter, proceed with
2056 * hcam registration and resource exposure to mid-layer.
2058 if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
2059 pmcraid_register_hcams(pinstance);
2061 wake_up_all(&pinstance->reset_wait_q);
2064 return;
2068 * pmcraid_initiate_reset - initiates reset sequence. This is called from
2069 * ISR/tasklet during error interrupts including IOA unit check. If reset
2070 * is already in progress, it just returns, otherwise initiates IOA reset
2071 * to bring IOA up to operational state.
2073 * @pinstance: pointer to adapter instance structure
2075 * Return value
2076 * none
2078 static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
2080 struct pmcraid_cmd *cmd;
2082 /* If the reset is already in progress, just return, otherwise start
2083 * reset sequence and return
2085 if (!pinstance->ioa_reset_in_progress) {
2086 scsi_block_requests(pinstance->host);
2087 cmd = pmcraid_get_free_cmd(pinstance);
2089 if (cmd == NULL) {
2090 pmcraid_err("no cmnd blocks for initiate_reset\n");
2091 return;
2094 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2095 pinstance->reset_cmd = cmd;
2096 pinstance->force_ioa_reset = 1;
2097 pmcraid_ioa_reset(cmd);
2102 * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
2103 * or bringdown IOA
2104 * @pinstance: pointer adapter instance structure
2105 * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
2106 * @target_state: expected target state after reset
2108 * Note: This command initiates reset and waits for its completion. Hence this
2109 * should not be called from isr/timer/tasklet functions (timeout handlers,
2110 * error response handlers and interrupt handlers).
2112 * Return Value
2113 * 1 in case ioa_state is not target_state, 0 otherwise.
2115 static int pmcraid_reset_reload(
2116 struct pmcraid_instance *pinstance,
2117 u8 shutdown_type,
2118 u8 target_state
2121 struct pmcraid_cmd *reset_cmd = NULL;
2122 unsigned long lock_flags;
2123 int reset = 1;
2125 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2127 if (pinstance->ioa_reset_in_progress) {
2128 pmcraid_info("reset_reload: reset is already in progress\n");
2130 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2132 wait_event(pinstance->reset_wait_q,
2133 !pinstance->ioa_reset_in_progress);
2135 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2137 if (pinstance->ioa_state == IOA_STATE_DEAD) {
2138 spin_unlock_irqrestore(pinstance->host->host_lock,
2139 lock_flags);
2140 pmcraid_info("reset_reload: IOA is dead\n");
2141 return reset;
2142 } else if (pinstance->ioa_state == target_state) {
2143 reset = 0;
2147 if (reset) {
2148 pmcraid_info("reset_reload: proceeding with reset\n");
2149 scsi_block_requests(pinstance->host);
2150 reset_cmd = pmcraid_get_free_cmd(pinstance);
2152 if (reset_cmd == NULL) {
2153 pmcraid_err("no free cmnd for reset_reload\n");
2154 spin_unlock_irqrestore(pinstance->host->host_lock,
2155 lock_flags);
2156 return reset;
2159 if (shutdown_type == SHUTDOWN_NORMAL)
2160 pinstance->ioa_bringdown = 1;
2162 pinstance->ioa_shutdown_type = shutdown_type;
2163 pinstance->reset_cmd = reset_cmd;
2164 pinstance->force_ioa_reset = reset;
2165 pmcraid_info("reset_reload: initiating reset\n");
2166 pmcraid_ioa_reset(reset_cmd);
2167 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2168 pmcraid_info("reset_reload: waiting for reset to complete\n");
2169 wait_event(pinstance->reset_wait_q,
2170 !pinstance->ioa_reset_in_progress);
2172 pmcraid_info("reset_reload: reset is complete !! \n");
2173 scsi_unblock_requests(pinstance->host);
2174 if (pinstance->ioa_state == target_state)
2175 reset = 0;
2178 return reset;
2182 * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
2184 * @pinstance: pointer to adapter instance structure
2186 * Return Value
2187 * whatever is returned from pmcraid_reset_reload
2189 static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
2191 return pmcraid_reset_reload(pinstance,
2192 SHUTDOWN_NORMAL,
2193 IOA_STATE_UNKNOWN);
2197 * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
2199 * @pinstance: pointer to adapter instance structure
2201 * Return Value
2202 * whatever is returned from pmcraid_reset_reload
2204 static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
2206 return pmcraid_reset_reload(pinstance,
2207 SHUTDOWN_NONE,
2208 IOA_STATE_OPERATIONAL);
2212 * pmcraid_request_sense - Send request sense to a device
2213 * @cmd: pmcraid command struct
2215 * This function sends a request sense to a device as a result of a check
2216 * condition. This method re-uses the same command block that failed earlier.
2218 static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
2220 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2221 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
2223 /* allocate DMAable memory for sense buffers */
2224 cmd->sense_buffer = pci_alloc_consistent(cmd->drv_inst->pdev,
2225 SCSI_SENSE_BUFFERSIZE,
2226 &cmd->sense_buffer_dma);
2228 if (cmd->sense_buffer == NULL) {
2229 pmcraid_err
2230 ("couldn't allocate sense buffer for request sense\n");
2231 pmcraid_erp_done(cmd);
2232 return;
2235 /* re-use the command block */
2236 memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
2237 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
2238 ioarcb->request_flags0 = (SYNC_COMPLETE |
2239 NO_LINK_DESCS |
2240 INHIBIT_UL_CHECK);
2241 ioarcb->request_type = REQ_TYPE_SCSI;
2242 ioarcb->cdb[0] = REQUEST_SENSE;
2243 ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2245 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
2246 offsetof(struct pmcraid_ioarcb,
2247 add_data.u.ioadl[0]));
2248 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
2250 ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
2252 ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
2253 ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
2254 ioadl->flags = IOADL_FLAGS_LAST_DESC;
2256 /* request sense might be called as part of error response processing
2257 * which runs in tasklets context. It is possible that mid-layer might
2258 * schedule queuecommand during this time, hence, writting to IOARRIN
2259 * must be protect by host_lock
2261 pmcraid_send_cmd(cmd, pmcraid_erp_done,
2262 PMCRAID_REQUEST_SENSE_TIMEOUT,
2263 pmcraid_timeout_handler);
2267 * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
2268 * @cmd: command that failed
2269 * @sense: true if request_sense is required after cancel all
2271 * This function sends a cancel all to a device to clear the queue.
2273 static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, u32 sense)
2275 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2276 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2277 struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
2278 void (*cmd_done) (struct pmcraid_cmd *) = sense ? pmcraid_erp_done
2279 : pmcraid_request_sense;
2281 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
2282 ioarcb->request_flags0 = SYNC_OVERRIDE;
2283 ioarcb->request_type = REQ_TYPE_IOACMD;
2284 ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
2286 if (RES_IS_GSCSI(res->cfg_entry))
2287 ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
2289 ioarcb->ioadl_bus_addr = 0;
2290 ioarcb->ioadl_length = 0;
2291 ioarcb->data_transfer_length = 0;
2292 ioarcb->ioarcb_bus_addr &= (~0x1FULL);
2294 /* writing to IOARRIN must be protected by host_lock, as mid-layer
2295 * schedule queuecommand while we are doing this
2297 pmcraid_send_cmd(cmd, cmd_done,
2298 PMCRAID_REQUEST_SENSE_TIMEOUT,
2299 pmcraid_timeout_handler);
2303 * pmcraid_frame_auto_sense: frame fixed format sense information
2305 * @cmd: pointer to failing command block
2307 * Return value
2308 * none
2310 static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
2312 u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
2313 struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
2314 struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
2315 u32 ioasc = le32_to_cpu(ioasa->ioasc);
2316 u32 failing_lba = 0;
2318 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
2319 cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
2321 if (RES_IS_VSET(res->cfg_entry) &&
2322 ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
2323 ioasa->u.vset.failing_lba_hi != 0) {
2325 sense_buf[0] = 0x72;
2326 sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
2327 sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
2328 sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
2330 sense_buf[7] = 12;
2331 sense_buf[8] = 0;
2332 sense_buf[9] = 0x0A;
2333 sense_buf[10] = 0x80;
2335 failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
2337 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
2338 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
2339 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
2340 sense_buf[15] = failing_lba & 0x000000ff;
2342 failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
2344 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
2345 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
2346 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
2347 sense_buf[19] = failing_lba & 0x000000ff;
2348 } else {
2349 sense_buf[0] = 0x70;
2350 sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
2351 sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
2352 sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
2354 if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
2355 if (RES_IS_VSET(res->cfg_entry))
2356 failing_lba =
2357 le32_to_cpu(ioasa->u.
2358 vset.failing_lba_lo);
2359 sense_buf[0] |= 0x80;
2360 sense_buf[3] = (failing_lba >> 24) & 0xff;
2361 sense_buf[4] = (failing_lba >> 16) & 0xff;
2362 sense_buf[5] = (failing_lba >> 8) & 0xff;
2363 sense_buf[6] = failing_lba & 0xff;
2366 sense_buf[7] = 6; /* additional length */
2371 * pmcraid_error_handler - Error response handlers for a SCSI op
2372 * @cmd: pointer to pmcraid_cmd that has failed
2374 * This function determines whether or not to initiate ERP on the affected
2375 * device. This is called from a tasklet, which doesn't hold any locks.
2377 * Return value:
2378 * 0 it caller can complete the request, otherwise 1 where in error
2379 * handler itself completes the request and returns the command block
2380 * back to free-pool
2382 static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
2384 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2385 struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
2386 struct pmcraid_instance *pinstance = cmd->drv_inst;
2387 struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
2388 u32 ioasc = le32_to_cpu(ioasa->ioasc);
2389 u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
2390 u32 sense_copied = 0;
2392 if (!res) {
2393 pmcraid_info("resource pointer is NULL\n");
2394 return 0;
2397 /* If this was a SCSI read/write command keep count of errors */
2398 if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
2399 atomic_inc(&res->read_failures);
2400 else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
2401 atomic_inc(&res->write_failures);
2403 if (!RES_IS_GSCSI(res->cfg_entry) &&
2404 masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
2405 pmcraid_frame_auto_sense(cmd);
2408 /* Log IOASC/IOASA information based on user settings */
2409 pmcraid_ioasc_logger(ioasc, cmd);
2411 switch (masked_ioasc) {
2413 case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
2414 scsi_cmd->result |= (DID_ABORT << 16);
2415 break;
2417 case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
2418 case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
2419 scsi_cmd->result |= (DID_NO_CONNECT << 16);
2420 break;
2422 case PMCRAID_IOASC_NR_SYNC_REQUIRED:
2423 res->sync_reqd = 1;
2424 scsi_cmd->result |= (DID_IMM_RETRY << 16);
2425 break;
2427 case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
2428 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
2429 break;
2431 case PMCRAID_IOASC_UA_BUS_WAS_RESET:
2432 case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
2433 if (!res->reset_progress)
2434 scsi_report_bus_reset(pinstance->host,
2435 scsi_cmd->device->channel);
2436 scsi_cmd->result |= (DID_ERROR << 16);
2437 break;
2439 case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
2440 scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
2441 res->sync_reqd = 1;
2443 /* if check_condition is not active return with error otherwise
2444 * get/frame the sense buffer
2446 if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
2447 SAM_STAT_CHECK_CONDITION &&
2448 PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
2449 return 0;
2451 /* If we have auto sense data as part of IOASA pass it to
2452 * mid-layer
2454 if (ioasa->auto_sense_length != 0) {
2455 short sense_len = ioasa->auto_sense_length;
2456 int data_size = min_t(u16, le16_to_cpu(sense_len),
2457 SCSI_SENSE_BUFFERSIZE);
2459 memcpy(scsi_cmd->sense_buffer,
2460 ioasa->sense_data,
2461 data_size);
2462 sense_copied = 1;
2465 if (RES_IS_GSCSI(res->cfg_entry)) {
2466 pmcraid_cancel_all(cmd, sense_copied);
2467 } else if (sense_copied) {
2468 pmcraid_erp_done(cmd);
2469 return 0;
2470 } else {
2471 pmcraid_request_sense(cmd);
2474 return 1;
2476 case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
2477 break;
2479 default:
2480 if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
2481 scsi_cmd->result |= (DID_ERROR << 16);
2482 break;
2484 return 0;
2488 * pmcraid_reset_device - device reset handler functions
2490 * @scsi_cmd: scsi command struct
2491 * @modifier: reset modifier indicating the reset sequence to be performed
2493 * This function issues a device reset to the affected device.
2494 * A LUN reset will be sent to the device first. If that does
2495 * not work, a target reset will be sent.
2497 * Return value:
2498 * SUCCESS / FAILED
2500 static int pmcraid_reset_device(
2501 struct scsi_cmnd *scsi_cmd,
2502 unsigned long timeout,
2503 u8 modifier
2506 struct pmcraid_cmd *cmd;
2507 struct pmcraid_instance *pinstance;
2508 struct pmcraid_resource_entry *res;
2509 struct pmcraid_ioarcb *ioarcb;
2510 unsigned long lock_flags;
2511 u32 ioasc;
2513 pinstance =
2514 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
2515 res = scsi_cmd->device->hostdata;
2517 if (!res) {
2518 sdev_printk(KERN_ERR, scsi_cmd->device,
2519 "reset_device: NULL resource pointer\n");
2520 return FAILED;
2523 /* If adapter is currently going through reset/reload, return failed.
2524 * This will force the mid-layer to call _eh_bus/host reset, which
2525 * will then go to sleep and wait for the reset to complete
2527 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2528 if (pinstance->ioa_reset_in_progress ||
2529 pinstance->ioa_state == IOA_STATE_DEAD) {
2530 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2531 return FAILED;
2534 res->reset_progress = 1;
2535 pmcraid_info("Resetting %s resource with addr %x\n",
2536 ((modifier & RESET_DEVICE_LUN) ? "LUN" :
2537 ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
2538 le32_to_cpu(res->cfg_entry.resource_address));
2540 /* get a free cmd block */
2541 cmd = pmcraid_get_free_cmd(pinstance);
2543 if (cmd == NULL) {
2544 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2545 pmcraid_err("%s: no cmd blocks are available\n", __func__);
2546 return FAILED;
2549 ioarcb = &cmd->ioa_cb->ioarcb;
2550 ioarcb->resource_handle = res->cfg_entry.resource_handle;
2551 ioarcb->request_type = REQ_TYPE_IOACMD;
2552 ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
2554 /* Initialize reset modifier bits */
2555 if (modifier)
2556 modifier = ENABLE_RESET_MODIFIER | modifier;
2558 ioarcb->cdb[1] = modifier;
2560 init_completion(&cmd->wait_for_completion);
2561 cmd->completion_req = 1;
2563 pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
2564 cmd->ioa_cb->ioarcb.cdb[0],
2565 le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
2566 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
2568 pmcraid_send_cmd(cmd,
2569 pmcraid_internal_done,
2570 timeout,
2571 pmcraid_timeout_handler);
2573 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2575 /* RESET_DEVICE command completes after all pending IOARCBs are
2576 * completed. Once this command is completed, pmcraind_internal_done
2577 * will wake up the 'completion' queue.
2579 wait_for_completion(&cmd->wait_for_completion);
2581 /* complete the command here itself and return the command block
2582 * to free list
2584 pmcraid_return_cmd(cmd);
2585 res->reset_progress = 0;
2586 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
2588 /* set the return value based on the returned ioasc */
2589 return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
2593 * _pmcraid_io_done - helper for pmcraid_io_done function
2595 * @cmd: pointer to pmcraid command struct
2596 * @reslen: residual data length to be set in the ioasa
2597 * @ioasc: ioasc either returned by IOA or set by driver itself.
2599 * This function is invoked by pmcraid_io_done to complete mid-layer
2600 * scsi ops.
2602 * Return value:
2603 * 0 if caller is required to return it to free_pool. Returns 1 if
2604 * caller need not worry about freeing command block as error handler
2605 * will take care of that.
2608 static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
2610 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2611 int rc = 0;
2613 scsi_set_resid(scsi_cmd, reslen);
2615 pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
2616 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
2617 cmd->ioa_cb->ioarcb.cdb[0],
2618 ioasc, scsi_cmd->result);
2620 if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
2621 rc = pmcraid_error_handler(cmd);
2623 if (rc == 0) {
2624 scsi_dma_unmap(scsi_cmd);
2625 scsi_cmd->scsi_done(scsi_cmd);
2628 return rc;
2632 * pmcraid_io_done - SCSI completion function
2634 * @cmd: pointer to pmcraid command struct
2636 * This function is invoked by tasklet/mid-layer error handler to completing
2637 * the SCSI ops sent from mid-layer.
2639 * Return value
2640 * none
2643 static void pmcraid_io_done(struct pmcraid_cmd *cmd)
2645 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
2646 u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
2648 if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
2649 pmcraid_return_cmd(cmd);
2653 * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
2655 * @cmd: command block of the command to be aborted
2657 * Return Value:
2658 * returns pointer to command structure used as cancelling cmd
2660 static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
2662 struct pmcraid_cmd *cancel_cmd;
2663 struct pmcraid_instance *pinstance;
2664 struct pmcraid_resource_entry *res;
2666 pinstance = (struct pmcraid_instance *)cmd->drv_inst;
2667 res = cmd->scsi_cmd->device->hostdata;
2669 cancel_cmd = pmcraid_get_free_cmd(pinstance);
2671 if (cancel_cmd == NULL) {
2672 pmcraid_err("%s: no cmd blocks are available\n", __func__);
2673 return NULL;
2676 pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
2678 pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
2679 cmd->ioa_cb->ioarcb.cdb[0],
2680 cmd->ioa_cb->ioarcb.response_handle >> 2);
2682 init_completion(&cancel_cmd->wait_for_completion);
2683 cancel_cmd->completion_req = 1;
2685 pmcraid_info("command (%d) CDB[0] = %x for %x\n",
2686 le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
2687 cmd->ioa_cb->ioarcb.cdb[0],
2688 le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
2690 pmcraid_send_cmd(cancel_cmd,
2691 pmcraid_internal_done,
2692 PMCRAID_INTERNAL_TIMEOUT,
2693 pmcraid_timeout_handler);
2694 return cancel_cmd;
2698 * pmcraid_abort_complete - Waits for ABORT TASK completion
2700 * @cancel_cmd: command block use as cancelling command
2702 * Return Value:
2703 * returns SUCCESS if ABORT TASK has good completion
2704 * otherwise FAILED
2706 static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
2708 struct pmcraid_resource_entry *res;
2709 u32 ioasc;
2711 wait_for_completion(&cancel_cmd->wait_for_completion);
2712 res = cancel_cmd->u.res;
2713 cancel_cmd->u.res = NULL;
2714 ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
2716 /* If the abort task is not timed out we will get a Good completion
2717 * as sense_key, otherwise we may get one the following responses
2718 * due to subsquent bus reset or device reset. In case IOASC is
2719 * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
2721 if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
2722 ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
2723 if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
2724 res->sync_reqd = 1;
2725 ioasc = 0;
2728 /* complete the command here itself */
2729 pmcraid_return_cmd(cancel_cmd);
2730 return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
2734 * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
2736 * @scsi_cmd: scsi command struct given by mid-layer. When this is called
2737 * mid-layer ensures that no other commands are queued. This
2738 * never gets called under interrupt, but a separate eh thread.
2740 * Return value:
2741 * SUCCESS / FAILED
2743 static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
2745 struct pmcraid_instance *pinstance;
2746 struct pmcraid_cmd *cmd;
2747 struct pmcraid_resource_entry *res;
2748 unsigned long host_lock_flags;
2749 unsigned long pending_lock_flags;
2750 struct pmcraid_cmd *cancel_cmd = NULL;
2751 int cmd_found = 0;
2752 int rc = FAILED;
2754 pinstance =
2755 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
2757 scmd_printk(KERN_INFO, scsi_cmd,
2758 "I/O command timed out, aborting it.\n");
2760 res = scsi_cmd->device->hostdata;
2762 if (res == NULL)
2763 return rc;
2765 /* If we are currently going through reset/reload, return failed.
2766 * This will force the mid-layer to eventually call
2767 * pmcraid_eh_host_reset which will then go to sleep and wait for the
2768 * reset to complete
2770 spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
2772 if (pinstance->ioa_reset_in_progress ||
2773 pinstance->ioa_state == IOA_STATE_DEAD) {
2774 spin_unlock_irqrestore(pinstance->host->host_lock,
2775 host_lock_flags);
2776 return rc;
2779 /* loop over pending cmd list to find cmd corresponding to this
2780 * scsi_cmd. Note that this command might not have been completed
2781 * already. locking: all pending commands are protected with
2782 * pending_pool_lock.
2784 spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
2785 list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
2787 if (cmd->scsi_cmd == scsi_cmd) {
2788 cmd_found = 1;
2789 break;
2793 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
2794 pending_lock_flags);
2796 /* If the command to be aborted was given to IOA and still pending with
2797 * it, send ABORT_TASK to abort this and wait for its completion
2799 if (cmd_found)
2800 cancel_cmd = pmcraid_abort_cmd(cmd);
2802 spin_unlock_irqrestore(pinstance->host->host_lock,
2803 host_lock_flags);
2805 if (cancel_cmd) {
2806 cancel_cmd->u.res = cmd->scsi_cmd->device->hostdata;
2807 rc = pmcraid_abort_complete(cancel_cmd);
2810 return cmd_found ? rc : SUCCESS;
2814 * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
2816 * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
2818 * All these routines invokve pmcraid_reset_device with appropriate parameters.
2819 * Since these are called from mid-layer EH thread, no other IO will be queued
2820 * to the resource being reset. However, control path (IOCTL) may be active so
2821 * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
2822 * takes care by locking/unlocking host_lock.
2824 * Return value
2825 * SUCCESS or FAILED
2827 static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
2829 scmd_printk(KERN_INFO, scmd,
2830 "resetting device due to an I/O command timeout.\n");
2831 return pmcraid_reset_device(scmd,
2832 PMCRAID_INTERNAL_TIMEOUT,
2833 RESET_DEVICE_LUN);
2836 static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
2838 scmd_printk(KERN_INFO, scmd,
2839 "Doing bus reset due to an I/O command timeout.\n");
2840 return pmcraid_reset_device(scmd,
2841 PMCRAID_RESET_BUS_TIMEOUT,
2842 RESET_DEVICE_BUS);
2845 static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
2847 scmd_printk(KERN_INFO, scmd,
2848 "Doing target reset due to an I/O command timeout.\n");
2849 return pmcraid_reset_device(scmd,
2850 PMCRAID_INTERNAL_TIMEOUT,
2851 RESET_DEVICE_TARGET);
2855 * pmcraid_eh_host_reset_handler - adapter reset handler callback
2857 * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
2859 * Initiates adapter reset to bring it up to operational state
2861 * Return value
2862 * SUCCESS or FAILED
2864 static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
2866 unsigned long interval = 10000; /* 10 seconds interval */
2867 int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
2868 struct pmcraid_instance *pinstance =
2869 (struct pmcraid_instance *)(scmd->device->host->hostdata);
2872 /* wait for an additional 150 seconds just in case firmware could come
2873 * up and if it could complete all the pending commands excluding the
2874 * two HCAM (CCN and LDN).
2876 while (waits--) {
2877 if (atomic_read(&pinstance->outstanding_cmds) <=
2878 PMCRAID_MAX_HCAM_CMD)
2879 return SUCCESS;
2880 msleep(interval);
2883 dev_err(&pinstance->pdev->dev,
2884 "Adapter being reset due to an I/O command timeout.\n");
2885 return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
2889 * pmcraid_task_attributes - Translate SPI Q-Tags to task attributes
2890 * @scsi_cmd: scsi command struct
2892 * Return value
2893 * number of tags or 0 if the task is not tagged
2895 static u8 pmcraid_task_attributes(struct scsi_cmnd *scsi_cmd)
2897 char tag[2];
2898 u8 rc = 0;
2900 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
2901 switch (tag[0]) {
2902 case MSG_SIMPLE_TAG:
2903 rc = TASK_TAG_SIMPLE;
2904 break;
2905 case MSG_HEAD_TAG:
2906 rc = TASK_TAG_QUEUE_HEAD;
2907 break;
2908 case MSG_ORDERED_TAG:
2909 rc = TASK_TAG_ORDERED;
2910 break;
2914 return rc;
2919 * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
2920 * @cmd: pmcraid command struct
2921 * @sgcount: count of scatter-gather elements
2923 * Return value
2924 * returns pointer pmcraid_ioadl_desc, initialized to point to internal
2925 * or external IOADLs
2927 struct pmcraid_ioadl_desc *
2928 pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
2930 struct pmcraid_ioadl_desc *ioadl;
2931 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2932 int ioadl_count = 0;
2934 if (ioarcb->add_cmd_param_length)
2935 ioadl_count = DIV_ROUND_UP(ioarcb->add_cmd_param_length, 16);
2936 ioarcb->ioadl_length =
2937 sizeof(struct pmcraid_ioadl_desc) * sgcount;
2939 if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
2940 /* external ioadls start at offset 0x80 from control_block
2941 * structure, re-using 24 out of 27 ioadls part of IOARCB.
2942 * It is necessary to indicate to firmware that driver is
2943 * using ioadls to be treated as external to IOARCB.
2945 ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
2946 ioarcb->ioadl_bus_addr =
2947 cpu_to_le64((cmd->ioa_cb_bus_addr) +
2948 offsetof(struct pmcraid_ioarcb,
2949 add_data.u.ioadl[3]));
2950 ioadl = &ioarcb->add_data.u.ioadl[3];
2951 } else {
2952 ioarcb->ioadl_bus_addr =
2953 cpu_to_le64((cmd->ioa_cb_bus_addr) +
2954 offsetof(struct pmcraid_ioarcb,
2955 add_data.u.ioadl[ioadl_count]));
2957 ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
2958 ioarcb->ioarcb_bus_addr |=
2959 DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8);
2962 return ioadl;
2966 * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
2967 * @pinstance: pointer to adapter instance structure
2968 * @cmd: pmcraid command struct
2970 * This function is invoked by queuecommand entry point while sending a command
2971 * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
2973 * Return value:
2974 * 0 on success or -1 on failure
2976 static int pmcraid_build_ioadl(
2977 struct pmcraid_instance *pinstance,
2978 struct pmcraid_cmd *cmd
2981 int i, nseg;
2982 struct scatterlist *sglist;
2984 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2985 struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
2986 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
2988 u32 length = scsi_bufflen(scsi_cmd);
2990 if (!length)
2991 return 0;
2993 nseg = scsi_dma_map(scsi_cmd);
2995 if (nseg < 0) {
2996 scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
2997 return -1;
2998 } else if (nseg > PMCRAID_MAX_IOADLS) {
2999 scsi_dma_unmap(scsi_cmd);
3000 scmd_printk(KERN_ERR, scsi_cmd,
3001 "sg count is (%d) more than allowed!\n", nseg);
3002 return -1;
3005 /* Initialize IOARCB data transfer length fields */
3006 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
3007 ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
3009 ioarcb->request_flags0 |= NO_LINK_DESCS;
3010 ioarcb->data_transfer_length = cpu_to_le32(length);
3011 ioadl = pmcraid_init_ioadls(cmd, nseg);
3013 /* Initialize IOADL descriptor addresses */
3014 scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
3015 ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
3016 ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
3017 ioadl[i].flags = 0;
3019 /* setup last descriptor */
3020 ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
3022 return 0;
3026 * pmcraid_free_sglist - Frees an allocated SG buffer list
3027 * @sglist: scatter/gather list pointer
3029 * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
3031 * Return value:
3032 * none
3034 static void pmcraid_free_sglist(struct pmcraid_sglist *sglist)
3036 int i;
3038 for (i = 0; i < sglist->num_sg; i++)
3039 __free_pages(sg_page(&(sglist->scatterlist[i])),
3040 sglist->order);
3042 kfree(sglist);
3046 * pmcraid_alloc_sglist - Allocates memory for a SG list
3047 * @buflen: buffer length
3049 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3050 * list.
3052 * Return value
3053 * pointer to sglist / NULL on failure
3055 static struct pmcraid_sglist *pmcraid_alloc_sglist(int buflen)
3057 struct pmcraid_sglist *sglist;
3058 struct scatterlist *scatterlist;
3059 struct page *page;
3060 int num_elem, i, j;
3061 int sg_size;
3062 int order;
3063 int bsize_elem;
3065 sg_size = buflen / (PMCRAID_MAX_IOADLS - 1);
3066 order = (sg_size > 0) ? get_order(sg_size) : 0;
3067 bsize_elem = PAGE_SIZE * (1 << order);
3069 /* Determine the actual number of sg entries needed */
3070 if (buflen % bsize_elem)
3071 num_elem = (buflen / bsize_elem) + 1;
3072 else
3073 num_elem = buflen / bsize_elem;
3075 /* Allocate a scatter/gather list for the DMA */
3076 sglist = kzalloc(sizeof(struct pmcraid_sglist) +
3077 (sizeof(struct scatterlist) * (num_elem - 1)),
3078 GFP_KERNEL);
3080 if (sglist == NULL)
3081 return NULL;
3083 scatterlist = sglist->scatterlist;
3084 sg_init_table(scatterlist, num_elem);
3085 sglist->order = order;
3086 sglist->num_sg = num_elem;
3087 sg_size = buflen;
3089 for (i = 0; i < num_elem; i++) {
3090 page = alloc_pages(GFP_KERNEL|GFP_DMA, order);
3091 if (!page) {
3092 for (j = i - 1; j >= 0; j--)
3093 __free_pages(sg_page(&scatterlist[j]), order);
3094 kfree(sglist);
3095 return NULL;
3098 sg_set_page(&scatterlist[i], page,
3099 sg_size < bsize_elem ? sg_size : bsize_elem, 0);
3100 sg_size -= bsize_elem;
3103 return sglist;
3107 * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
3108 * @sglist: scatter/gather list pointer
3109 * @buffer: buffer pointer
3110 * @len: buffer length
3111 * @direction: data transfer direction
3113 * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
3115 * Return value:
3116 * 0 on success / other on failure
3118 static int pmcraid_copy_sglist(
3119 struct pmcraid_sglist *sglist,
3120 unsigned long buffer,
3121 u32 len,
3122 int direction
3125 struct scatterlist *scatterlist;
3126 void *kaddr;
3127 int bsize_elem;
3128 int i;
3129 int rc = 0;
3131 /* Determine the actual number of bytes per element */
3132 bsize_elem = PAGE_SIZE * (1 << sglist->order);
3134 scatterlist = sglist->scatterlist;
3136 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
3137 struct page *page = sg_page(&scatterlist[i]);
3139 kaddr = kmap(page);
3140 if (direction == DMA_TO_DEVICE)
3141 rc = __copy_from_user(kaddr,
3142 (void *)buffer,
3143 bsize_elem);
3144 else
3145 rc = __copy_to_user((void *)buffer, kaddr, bsize_elem);
3147 kunmap(page);
3149 if (rc) {
3150 pmcraid_err("failed to copy user data into sg list\n");
3151 return -EFAULT;
3154 scatterlist[i].length = bsize_elem;
3157 if (len % bsize_elem) {
3158 struct page *page = sg_page(&scatterlist[i]);
3160 kaddr = kmap(page);
3162 if (direction == DMA_TO_DEVICE)
3163 rc = __copy_from_user(kaddr,
3164 (void *)buffer,
3165 len % bsize_elem);
3166 else
3167 rc = __copy_to_user((void *)buffer,
3168 kaddr,
3169 len % bsize_elem);
3171 kunmap(page);
3173 scatterlist[i].length = len % bsize_elem;
3176 if (rc) {
3177 pmcraid_err("failed to copy user data into sg list\n");
3178 rc = -EFAULT;
3181 return rc;
3185 * pmcraid_queuecommand - Queue a mid-layer request
3186 * @scsi_cmd: scsi command struct
3187 * @done: done function
3189 * This function queues a request generated by the mid-layer. Midlayer calls
3190 * this routine within host->lock. Some of the functions called by queuecommand
3191 * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
3193 * Return value:
3194 * 0 on success
3195 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3196 * SCSI_MLQUEUE_HOST_BUSY if host is busy
3198 static int pmcraid_queuecommand(
3199 struct scsi_cmnd *scsi_cmd,
3200 void (*done) (struct scsi_cmnd *)
3203 struct pmcraid_instance *pinstance;
3204 struct pmcraid_resource_entry *res;
3205 struct pmcraid_ioarcb *ioarcb;
3206 struct pmcraid_cmd *cmd;
3207 int rc = 0;
3209 pinstance =
3210 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
3212 scsi_cmd->scsi_done = done;
3213 res = scsi_cmd->device->hostdata;
3214 scsi_cmd->result = (DID_OK << 16);
3216 /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
3217 * the command
3219 if (pinstance->ioa_state == IOA_STATE_DEAD) {
3220 pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
3221 scsi_cmd->result = (DID_NO_CONNECT << 16);
3222 scsi_cmd->scsi_done(scsi_cmd);
3223 return 0;
3226 /* If IOA reset is in progress, can't queue the commands */
3227 if (pinstance->ioa_reset_in_progress)
3228 return SCSI_MLQUEUE_HOST_BUSY;
3230 /* initialize the command and IOARCB to be sent to IOA */
3231 cmd = pmcraid_get_free_cmd(pinstance);
3233 if (cmd == NULL) {
3234 pmcraid_err("free command block is not available\n");
3235 return SCSI_MLQUEUE_HOST_BUSY;
3238 cmd->scsi_cmd = scsi_cmd;
3239 ioarcb = &(cmd->ioa_cb->ioarcb);
3240 memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
3241 ioarcb->resource_handle = res->cfg_entry.resource_handle;
3242 ioarcb->request_type = REQ_TYPE_SCSI;
3244 cmd->cmd_done = pmcraid_io_done;
3246 if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
3247 if (scsi_cmd->underflow == 0)
3248 ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
3250 if (res->sync_reqd) {
3251 ioarcb->request_flags0 |= SYNC_COMPLETE;
3252 res->sync_reqd = 0;
3255 ioarcb->request_flags0 |= NO_LINK_DESCS;
3256 ioarcb->request_flags1 |= pmcraid_task_attributes(scsi_cmd);
3258 if (RES_IS_GSCSI(res->cfg_entry))
3259 ioarcb->request_flags1 |= DELAY_AFTER_RESET;
3262 rc = pmcraid_build_ioadl(pinstance, cmd);
3264 pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
3265 le32_to_cpu(ioarcb->response_handle) >> 2,
3266 scsi_cmd->cmnd[0], pinstance->host->unique_id,
3267 RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
3268 PMCRAID_PHYS_BUS_ID,
3269 RES_IS_VSET(res->cfg_entry) ?
3270 res->cfg_entry.unique_flags1 :
3271 RES_TARGET(res->cfg_entry.resource_address),
3272 RES_LUN(res->cfg_entry.resource_address));
3274 if (likely(rc == 0)) {
3275 _pmcraid_fire_command(cmd);
3276 } else {
3277 pmcraid_err("queuecommand could not build ioadl\n");
3278 pmcraid_return_cmd(cmd);
3279 rc = SCSI_MLQUEUE_HOST_BUSY;
3282 return rc;
3286 * pmcraid_open -char node "open" entry, allowed only users with admin access
3288 static int pmcraid_chr_open(struct inode *inode, struct file *filep)
3290 struct pmcraid_instance *pinstance;
3292 if (!capable(CAP_SYS_ADMIN))
3293 return -EACCES;
3295 /* Populate adapter instance * pointer for use by ioctl */
3296 pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
3297 filep->private_data = pinstance;
3299 return 0;
3303 * pmcraid_release - char node "release" entry point
3305 static int pmcraid_chr_release(struct inode *inode, struct file *filep)
3307 struct pmcraid_instance *pinstance =
3308 ((struct pmcraid_instance *)filep->private_data);
3310 filep->private_data = NULL;
3311 fasync_helper(-1, filep, 0, &pinstance->aen_queue);
3313 return 0;
3317 * pmcraid_fasync - Async notifier registration from applications
3319 * This function adds the calling process to a driver global queue. When an
3320 * event occurs, SIGIO will be sent to all processes in this queue.
3322 static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
3324 struct pmcraid_instance *pinstance;
3325 int rc;
3327 pinstance = (struct pmcraid_instance *)filep->private_data;
3328 mutex_lock(&pinstance->aen_queue_lock);
3329 rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
3330 mutex_unlock(&pinstance->aen_queue_lock);
3332 return rc;
3337 * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
3338 * commands sent over IOCTL interface
3340 * @cmd : pointer to struct pmcraid_cmd
3341 * @buflen : length of the request buffer
3342 * @direction : data transfer direction
3344 * Return value
3345 * 0 on sucess, non-zero error code on failure
3347 static int pmcraid_build_passthrough_ioadls(
3348 struct pmcraid_cmd *cmd,
3349 int buflen,
3350 int direction
3353 struct pmcraid_sglist *sglist = NULL;
3354 struct scatterlist *sg = NULL;
3355 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
3356 struct pmcraid_ioadl_desc *ioadl;
3357 int i;
3359 sglist = pmcraid_alloc_sglist(buflen);
3361 if (!sglist) {
3362 pmcraid_err("can't allocate memory for passthrough SGls\n");
3363 return -ENOMEM;
3366 sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
3367 sglist->scatterlist,
3368 sglist->num_sg, direction);
3370 if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
3371 dev_err(&cmd->drv_inst->pdev->dev,
3372 "Failed to map passthrough buffer!\n");
3373 pmcraid_free_sglist(sglist);
3374 return -EIO;
3377 cmd->sglist = sglist;
3378 ioarcb->request_flags0 |= NO_LINK_DESCS;
3380 ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
3382 /* Initialize IOADL descriptor addresses */
3383 for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
3384 ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
3385 ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
3386 ioadl[i].flags = 0;
3389 /* setup the last descriptor */
3390 ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
3392 return 0;
3397 * pmcraid_release_passthrough_ioadls - release passthrough ioadls
3399 * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
3400 * @buflen: size of the request buffer
3401 * @direction: data transfer direction
3403 * Return value
3404 * 0 on sucess, non-zero error code on failure
3406 static void pmcraid_release_passthrough_ioadls(
3407 struct pmcraid_cmd *cmd,
3408 int buflen,
3409 int direction
3412 struct pmcraid_sglist *sglist = cmd->sglist;
3414 if (buflen > 0) {
3415 pci_unmap_sg(cmd->drv_inst->pdev,
3416 sglist->scatterlist,
3417 sglist->num_sg,
3418 direction);
3419 pmcraid_free_sglist(sglist);
3420 cmd->sglist = NULL;
3425 * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
3427 * @pinstance: pointer to adapter instance structure
3428 * @cmd: ioctl code
3429 * @arg: pointer to pmcraid_passthrough_buffer user buffer
3431 * Return value
3432 * 0 on sucess, non-zero error code on failure
3434 static long pmcraid_ioctl_passthrough(
3435 struct pmcraid_instance *pinstance,
3436 unsigned int ioctl_cmd,
3437 unsigned int buflen,
3438 unsigned long arg
3441 struct pmcraid_passthrough_ioctl_buffer *buffer;
3442 struct pmcraid_ioarcb *ioarcb;
3443 struct pmcraid_cmd *cmd;
3444 struct pmcraid_cmd *cancel_cmd;
3445 unsigned long request_buffer;
3446 unsigned long request_offset;
3447 unsigned long lock_flags;
3448 int request_size;
3449 int buffer_size;
3450 u8 access, direction;
3451 int rc = 0;
3453 /* If IOA reset is in progress, wait 10 secs for reset to complete */
3454 if (pinstance->ioa_reset_in_progress) {
3455 rc = wait_event_interruptible_timeout(
3456 pinstance->reset_wait_q,
3457 !pinstance->ioa_reset_in_progress,
3458 msecs_to_jiffies(10000));
3460 if (!rc)
3461 return -ETIMEDOUT;
3462 else if (rc < 0)
3463 return -ERESTARTSYS;
3466 /* If adapter is not in operational state, return error */
3467 if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
3468 pmcraid_err("IOA is not operational\n");
3469 return -ENOTTY;
3472 buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
3473 buffer = kmalloc(buffer_size, GFP_KERNEL);
3475 if (!buffer) {
3476 pmcraid_err("no memory for passthrough buffer\n");
3477 return -ENOMEM;
3480 request_offset =
3481 offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
3483 request_buffer = arg + request_offset;
3485 rc = __copy_from_user(buffer,
3486 (struct pmcraid_passthrough_ioctl_buffer *) arg,
3487 sizeof(struct pmcraid_passthrough_ioctl_buffer));
3488 if (rc) {
3489 pmcraid_err("ioctl: can't copy passthrough buffer\n");
3490 rc = -EFAULT;
3491 goto out_free_buffer;
3494 request_size = buffer->ioarcb.data_transfer_length;
3496 if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
3497 access = VERIFY_READ;
3498 direction = DMA_TO_DEVICE;
3499 } else {
3500 access = VERIFY_WRITE;
3501 direction = DMA_FROM_DEVICE;
3504 if (request_size > 0) {
3505 rc = access_ok(access, arg, request_offset + request_size);
3507 if (!rc) {
3508 rc = -EFAULT;
3509 goto out_free_buffer;
3511 } else if (request_size < 0) {
3512 rc = -EINVAL;
3513 goto out_free_buffer;
3516 /* check if we have any additional command parameters */
3517 if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
3518 rc = -EINVAL;
3519 goto out_free_buffer;
3522 cmd = pmcraid_get_free_cmd(pinstance);
3524 if (!cmd) {
3525 pmcraid_err("free command block is not available\n");
3526 rc = -ENOMEM;
3527 goto out_free_buffer;
3530 cmd->scsi_cmd = NULL;
3531 ioarcb = &(cmd->ioa_cb->ioarcb);
3533 /* Copy the user-provided IOARCB stuff field by field */
3534 ioarcb->resource_handle = buffer->ioarcb.resource_handle;
3535 ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
3536 ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
3537 ioarcb->request_type = buffer->ioarcb.request_type;
3538 ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
3539 ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
3540 memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
3542 if (buffer->ioarcb.add_cmd_param_length) {
3543 ioarcb->add_cmd_param_length =
3544 buffer->ioarcb.add_cmd_param_length;
3545 ioarcb->add_cmd_param_offset =
3546 buffer->ioarcb.add_cmd_param_offset;
3547 memcpy(ioarcb->add_data.u.add_cmd_params,
3548 buffer->ioarcb.add_data.u.add_cmd_params,
3549 buffer->ioarcb.add_cmd_param_length);
3552 if (request_size) {
3553 rc = pmcraid_build_passthrough_ioadls(cmd,
3554 request_size,
3555 direction);
3556 if (rc) {
3557 pmcraid_err("couldn't build passthrough ioadls\n");
3558 goto out_free_buffer;
3560 } else if (request_size < 0) {
3561 rc = -EINVAL;
3562 goto out_free_buffer;
3565 /* If data is being written into the device, copy the data from user
3566 * buffers
3568 if (direction == DMA_TO_DEVICE && request_size > 0) {
3569 rc = pmcraid_copy_sglist(cmd->sglist,
3570 request_buffer,
3571 request_size,
3572 direction);
3573 if (rc) {
3574 pmcraid_err("failed to copy user buffer\n");
3575 goto out_free_sglist;
3579 /* passthrough ioctl is a blocking command so, put the user to sleep
3580 * until timeout. Note that a timeout value of 0 means, do timeout.
3582 cmd->cmd_done = pmcraid_internal_done;
3583 init_completion(&cmd->wait_for_completion);
3584 cmd->completion_req = 1;
3586 pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
3587 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
3588 cmd->ioa_cb->ioarcb.cdb[0],
3589 le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
3591 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3592 _pmcraid_fire_command(cmd);
3593 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3595 /* If command timeout is specified put caller to wait till that time,
3596 * otherwise it would be blocking wait. If command gets timed out, it
3597 * will be aborted.
3599 if (buffer->ioarcb.cmd_timeout == 0) {
3600 wait_for_completion(&cmd->wait_for_completion);
3601 } else if (!wait_for_completion_timeout(
3602 &cmd->wait_for_completion,
3603 msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
3605 pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
3606 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
3607 cmd->ioa_cb->ioarcb.cdb[0]);
3609 rc = -ETIMEDOUT;
3610 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3611 cancel_cmd = pmcraid_abort_cmd(cmd);
3612 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3614 if (cancel_cmd) {
3615 wait_for_completion(&cancel_cmd->wait_for_completion);
3616 pmcraid_return_cmd(cancel_cmd);
3619 goto out_free_sglist;
3622 /* If the command failed for any reason, copy entire IOASA buffer and
3623 * return IOCTL success. If copying IOASA to user-buffer fails, return
3624 * EFAULT
3626 if (le32_to_cpu(cmd->ioa_cb->ioasa.ioasc)) {
3628 void *ioasa =
3629 (void *)(arg +
3630 offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
3632 pmcraid_info("command failed with %x\n",
3633 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
3634 if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
3635 sizeof(struct pmcraid_ioasa))) {
3636 pmcraid_err("failed to copy ioasa buffer to user\n");
3637 rc = -EFAULT;
3640 /* If the data transfer was from device, copy the data onto user
3641 * buffers
3643 else if (direction == DMA_FROM_DEVICE && request_size > 0) {
3644 rc = pmcraid_copy_sglist(cmd->sglist,
3645 request_buffer,
3646 request_size,
3647 direction);
3648 if (rc) {
3649 pmcraid_err("failed to copy user buffer\n");
3650 rc = -EFAULT;
3654 out_free_sglist:
3655 pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
3656 pmcraid_return_cmd(cmd);
3658 out_free_buffer:
3659 kfree(buffer);
3661 return rc;
3668 * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
3670 * @pinstance: pointer to adapter instance structure
3671 * @cmd: ioctl command passed in
3672 * @buflen: length of user_buffer
3673 * @user_buffer: user buffer pointer
3675 * Return Value
3676 * 0 in case of success, otherwise appropriate error code
3678 static long pmcraid_ioctl_driver(
3679 struct pmcraid_instance *pinstance,
3680 unsigned int cmd,
3681 unsigned int buflen,
3682 void __user *user_buffer
3685 int rc = -ENOSYS;
3687 if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
3688 pmcraid_err("ioctl_driver: access fault in request buffer \n");
3689 return -EFAULT;
3692 switch (cmd) {
3693 case PMCRAID_IOCTL_RESET_ADAPTER:
3694 pmcraid_reset_bringup(pinstance);
3695 rc = 0;
3696 break;
3698 default:
3699 break;
3702 return rc;
3706 * pmcraid_check_ioctl_buffer - check for proper access to user buffer
3708 * @cmd: ioctl command
3709 * @arg: user buffer
3710 * @hdr: pointer to kernel memory for pmcraid_ioctl_header
3712 * Return Value
3713 * negetive error code if there are access issues, otherwise zero.
3714 * Upon success, returns ioctl header copied out of user buffer.
3717 static int pmcraid_check_ioctl_buffer(
3718 int cmd,
3719 void __user *arg,
3720 struct pmcraid_ioctl_header *hdr
3723 int rc = 0;
3724 int access = VERIFY_READ;
3726 if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
3727 pmcraid_err("couldn't copy ioctl header from user buffer\n");
3728 return -EFAULT;
3731 /* check for valid driver signature */
3732 rc = memcmp(hdr->signature,
3733 PMCRAID_IOCTL_SIGNATURE,
3734 sizeof(hdr->signature));
3735 if (rc) {
3736 pmcraid_err("signature verification failed\n");
3737 return -EINVAL;
3740 /* buffer length can't be negetive */
3741 if (hdr->buffer_length < 0) {
3742 pmcraid_err("ioctl: invalid buffer length specified\n");
3743 return -EINVAL;
3746 /* check for appropriate buffer access */
3747 if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
3748 access = VERIFY_WRITE;
3750 rc = access_ok(access,
3751 (arg + sizeof(struct pmcraid_ioctl_header)),
3752 hdr->buffer_length);
3753 if (!rc) {
3754 pmcraid_err("access failed for user buffer of size %d\n",
3755 hdr->buffer_length);
3756 return -EFAULT;
3759 return 0;
3763 * pmcraid_ioctl - char node ioctl entry point
3765 static long pmcraid_chr_ioctl(
3766 struct file *filep,
3767 unsigned int cmd,
3768 unsigned long arg
3771 struct pmcraid_instance *pinstance = NULL;
3772 struct pmcraid_ioctl_header *hdr = NULL;
3773 int retval = -ENOTTY;
3775 hdr = kmalloc(GFP_KERNEL, sizeof(struct pmcraid_ioctl_header));
3777 if (!hdr) {
3778 pmcraid_err("faile to allocate memory for ioctl header\n");
3779 return -ENOMEM;
3782 retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
3784 if (retval) {
3785 pmcraid_info("chr_ioctl: header check failed\n");
3786 kfree(hdr);
3787 return retval;
3790 pinstance = (struct pmcraid_instance *)filep->private_data;
3792 if (!pinstance) {
3793 pmcraid_info("adapter instance is not found\n");
3794 kfree(hdr);
3795 return -ENOTTY;
3798 switch (_IOC_TYPE(cmd)) {
3800 case PMCRAID_PASSTHROUGH_IOCTL:
3801 /* If ioctl code is to download microcode, we need to block
3802 * mid-layer requests.
3804 if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
3805 scsi_block_requests(pinstance->host);
3807 retval = pmcraid_ioctl_passthrough(pinstance,
3808 cmd,
3809 hdr->buffer_length,
3810 arg);
3812 if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
3813 scsi_unblock_requests(pinstance->host);
3814 break;
3816 case PMCRAID_DRIVER_IOCTL:
3817 arg += sizeof(struct pmcraid_ioctl_header);
3818 retval = pmcraid_ioctl_driver(pinstance,
3819 cmd,
3820 hdr->buffer_length,
3821 (void __user *)arg);
3822 break;
3824 default:
3825 retval = -ENOTTY;
3826 break;
3829 kfree(hdr);
3831 return retval;
3835 * File operations structure for management interface
3837 static const struct file_operations pmcraid_fops = {
3838 .owner = THIS_MODULE,
3839 .open = pmcraid_chr_open,
3840 .release = pmcraid_chr_release,
3841 .fasync = pmcraid_chr_fasync,
3842 .unlocked_ioctl = pmcraid_chr_ioctl,
3843 #ifdef CONFIG_COMPAT
3844 .compat_ioctl = pmcraid_chr_ioctl,
3845 #endif
3852 * pmcraid_show_log_level - Display adapter's error logging level
3853 * @dev: class device struct
3854 * @buf: buffer
3856 * Return value:
3857 * number of bytes printed to buffer
3859 static ssize_t pmcraid_show_log_level(
3860 struct device *dev,
3861 struct device_attribute *attr,
3862 char *buf)
3864 struct Scsi_Host *shost = class_to_shost(dev);
3865 struct pmcraid_instance *pinstance =
3866 (struct pmcraid_instance *)shost->hostdata;
3867 return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
3871 * pmcraid_store_log_level - Change the adapter's error logging level
3872 * @dev: class device struct
3873 * @buf: buffer
3874 * @count: not used
3876 * Return value:
3877 * number of bytes printed to buffer
3879 static ssize_t pmcraid_store_log_level(
3880 struct device *dev,
3881 struct device_attribute *attr,
3882 const char *buf,
3883 size_t count
3886 struct Scsi_Host *shost;
3887 struct pmcraid_instance *pinstance;
3888 unsigned long val;
3890 if (strict_strtoul(buf, 10, &val))
3891 return -EINVAL;
3892 /* log-level should be from 0 to 2 */
3893 if (val > 2)
3894 return -EINVAL;
3896 shost = class_to_shost(dev);
3897 pinstance = (struct pmcraid_instance *)shost->hostdata;
3898 pinstance->current_log_level = val;
3900 return strlen(buf);
3903 static struct device_attribute pmcraid_log_level_attr = {
3904 .attr = {
3905 .name = "log_level",
3906 .mode = S_IRUGO | S_IWUSR,
3908 .show = pmcraid_show_log_level,
3909 .store = pmcraid_store_log_level,
3913 * pmcraid_show_drv_version - Display driver version
3914 * @dev: class device struct
3915 * @buf: buffer
3917 * Return value:
3918 * number of bytes printed to buffer
3920 static ssize_t pmcraid_show_drv_version(
3921 struct device *dev,
3922 struct device_attribute *attr,
3923 char *buf
3926 return snprintf(buf, PAGE_SIZE, "version: %s, build date: %s\n",
3927 PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
3930 static struct device_attribute pmcraid_driver_version_attr = {
3931 .attr = {
3932 .name = "drv_version",
3933 .mode = S_IRUGO,
3935 .show = pmcraid_show_drv_version,
3939 * pmcraid_show_io_adapter_id - Display driver assigned adapter id
3940 * @dev: class device struct
3941 * @buf: buffer
3943 * Return value:
3944 * number of bytes printed to buffer
3946 static ssize_t pmcraid_show_adapter_id(
3947 struct device *dev,
3948 struct device_attribute *attr,
3949 char *buf
3952 struct Scsi_Host *shost = class_to_shost(dev);
3953 struct pmcraid_instance *pinstance =
3954 (struct pmcraid_instance *)shost->hostdata;
3955 u32 adapter_id = (pinstance->pdev->bus->number << 8) |
3956 pinstance->pdev->devfn;
3957 u32 aen_group = pmcraid_event_family.id;
3959 return snprintf(buf, PAGE_SIZE,
3960 "adapter id: %d\nminor: %d\naen group: %d\n",
3961 adapter_id, MINOR(pinstance->cdev.dev), aen_group);
3964 static struct device_attribute pmcraid_adapter_id_attr = {
3965 .attr = {
3966 .name = "adapter_id",
3967 .mode = S_IRUGO | S_IWUSR,
3969 .show = pmcraid_show_adapter_id,
3972 static struct device_attribute *pmcraid_host_attrs[] = {
3973 &pmcraid_log_level_attr,
3974 &pmcraid_driver_version_attr,
3975 &pmcraid_adapter_id_attr,
3976 NULL,
3980 /* host template structure for pmcraid driver */
3981 static struct scsi_host_template pmcraid_host_template = {
3982 .module = THIS_MODULE,
3983 .name = PMCRAID_DRIVER_NAME,
3984 .queuecommand = pmcraid_queuecommand,
3985 .eh_abort_handler = pmcraid_eh_abort_handler,
3986 .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
3987 .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
3988 .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
3989 .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
3991 .slave_alloc = pmcraid_slave_alloc,
3992 .slave_configure = pmcraid_slave_configure,
3993 .slave_destroy = pmcraid_slave_destroy,
3994 .change_queue_depth = pmcraid_change_queue_depth,
3995 .change_queue_type = pmcraid_change_queue_type,
3996 .can_queue = PMCRAID_MAX_IO_CMD,
3997 .this_id = -1,
3998 .sg_tablesize = PMCRAID_MAX_IOADLS,
3999 .max_sectors = PMCRAID_IOA_MAX_SECTORS,
4000 .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
4001 .use_clustering = ENABLE_CLUSTERING,
4002 .shost_attrs = pmcraid_host_attrs,
4003 .proc_name = PMCRAID_DRIVER_NAME
4007 * pmcraid_isr_common - Common interrupt handler routine
4009 * @pinstance: pointer to adapter instance
4010 * @intrs: active interrupts (contents of ioa_host_interrupt register)
4011 * @hrrq_id: Host RRQ index
4013 * Return Value
4014 * none
4016 static void pmcraid_isr_common(
4017 struct pmcraid_instance *pinstance,
4018 u32 intrs,
4019 int hrrq_id
4022 u32 intrs_clear =
4023 (intrs & INTRS_CRITICAL_OP_IN_PROGRESS) ? intrs
4024 : INTRS_HRRQ_VALID;
4025 iowrite32(intrs_clear,
4026 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4027 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
4029 /* hrrq valid bit was set, schedule tasklet to handle the response */
4030 if (intrs_clear == INTRS_HRRQ_VALID)
4031 tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
4035 * pmcraid_isr - implements interrupt handling routine
4037 * @irq: interrupt vector number
4038 * @dev_id: pointer hrrq_vector
4040 * Return Value
4041 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4043 static irqreturn_t pmcraid_isr(int irq, void *dev_id)
4045 struct pmcraid_isr_param *hrrq_vector;
4046 struct pmcraid_instance *pinstance;
4047 unsigned long lock_flags;
4048 u32 intrs;
4050 /* In case of legacy interrupt mode where interrupts are shared across
4051 * isrs, it may be possible that the current interrupt is not from IOA
4053 if (!dev_id) {
4054 printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
4055 return IRQ_NONE;
4058 hrrq_vector = (struct pmcraid_isr_param *)dev_id;
4059 pinstance = hrrq_vector->drv_inst;
4061 /* Acquire the lock (currently host_lock) while processing interrupts.
4062 * This interval is small as most of the response processing is done by
4063 * tasklet without the lock.
4065 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
4066 intrs = pmcraid_read_interrupts(pinstance);
4068 if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0)) {
4069 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4070 return IRQ_NONE;
4073 /* Any error interrupts including unit_check, initiate IOA reset.
4074 * In case of unit check indicate to reset_sequence that IOA unit
4075 * checked and prepare for a dump during reset sequence
4077 if (intrs & PMCRAID_ERROR_INTERRUPTS) {
4079 if (intrs & INTRS_IOA_UNIT_CHECK)
4080 pinstance->ioa_unit_check = 1;
4082 iowrite32(intrs,
4083 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4084 pmcraid_err("ISR: error interrupts: %x initiating reset\n",
4085 intrs);
4086 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
4087 pmcraid_initiate_reset(pinstance);
4088 } else {
4089 pmcraid_isr_common(pinstance, intrs, hrrq_vector->hrrq_id);
4092 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4094 return IRQ_HANDLED;
4099 * pmcraid_worker_function - worker thread function
4101 * @workp: pointer to struct work queue
4103 * Return Value
4104 * None
4107 static void pmcraid_worker_function(struct work_struct *workp)
4109 struct pmcraid_instance *pinstance;
4110 struct pmcraid_resource_entry *res;
4111 struct pmcraid_resource_entry *temp;
4112 struct scsi_device *sdev;
4113 unsigned long lock_flags;
4114 unsigned long host_lock_flags;
4115 u8 bus, target, lun;
4117 pinstance = container_of(workp, struct pmcraid_instance, worker_q);
4118 /* add resources only after host is added into system */
4119 if (!atomic_read(&pinstance->expose_resources))
4120 return;
4122 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
4123 list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
4125 if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
4126 sdev = res->scsi_dev;
4128 /* host_lock must be held before calling
4129 * scsi_device_get
4131 spin_lock_irqsave(pinstance->host->host_lock,
4132 host_lock_flags);
4133 if (!scsi_device_get(sdev)) {
4134 spin_unlock_irqrestore(
4135 pinstance->host->host_lock,
4136 host_lock_flags);
4137 pmcraid_info("deleting %x from midlayer\n",
4138 res->cfg_entry.resource_address);
4139 list_move_tail(&res->queue,
4140 &pinstance->free_res_q);
4141 spin_unlock_irqrestore(
4142 &pinstance->resource_lock,
4143 lock_flags);
4144 scsi_remove_device(sdev);
4145 scsi_device_put(sdev);
4146 spin_lock_irqsave(&pinstance->resource_lock,
4147 lock_flags);
4148 res->change_detected = 0;
4149 } else {
4150 spin_unlock_irqrestore(
4151 pinstance->host->host_lock,
4152 host_lock_flags);
4157 list_for_each_entry(res, &pinstance->used_res_q, queue) {
4159 if (res->change_detected == RES_CHANGE_ADD) {
4161 if (!pmcraid_expose_resource(&res->cfg_entry))
4162 continue;
4164 if (RES_IS_VSET(res->cfg_entry)) {
4165 bus = PMCRAID_VSET_BUS_ID;
4166 target = res->cfg_entry.unique_flags1;
4167 lun = PMCRAID_VSET_LUN_ID;
4168 } else {
4169 bus = PMCRAID_PHYS_BUS_ID;
4170 target =
4171 RES_TARGET(
4172 res->cfg_entry.resource_address);
4173 lun = RES_LUN(res->cfg_entry.resource_address);
4176 res->change_detected = 0;
4177 spin_unlock_irqrestore(&pinstance->resource_lock,
4178 lock_flags);
4179 scsi_add_device(pinstance->host, bus, target, lun);
4180 spin_lock_irqsave(&pinstance->resource_lock,
4181 lock_flags);
4185 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
4189 * pmcraid_tasklet_function - Tasklet function
4191 * @instance: pointer to msix param structure
4193 * Return Value
4194 * None
4196 void pmcraid_tasklet_function(unsigned long instance)
4198 struct pmcraid_isr_param *hrrq_vector;
4199 struct pmcraid_instance *pinstance;
4200 unsigned long hrrq_lock_flags;
4201 unsigned long pending_lock_flags;
4202 unsigned long host_lock_flags;
4203 spinlock_t *lockp; /* hrrq buffer lock */
4204 int id;
4205 u32 intrs;
4206 __le32 resp;
4208 hrrq_vector = (struct pmcraid_isr_param *)instance;
4209 pinstance = hrrq_vector->drv_inst;
4210 id = hrrq_vector->hrrq_id;
4211 lockp = &(pinstance->hrrq_lock[id]);
4212 intrs = pmcraid_read_interrupts(pinstance);
4214 /* If interrupts was as part of the ioa initialization, clear and mask
4215 * it. Delete the timer and wakeup the reset engine to proceed with
4216 * reset sequence
4218 if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
4219 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
4220 pinstance->int_regs.ioa_host_interrupt_mask_reg);
4221 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
4222 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4224 if (pinstance->reset_cmd != NULL) {
4225 del_timer(&pinstance->reset_cmd->timer);
4226 spin_lock_irqsave(pinstance->host->host_lock,
4227 host_lock_flags);
4228 pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
4229 spin_unlock_irqrestore(pinstance->host->host_lock,
4230 host_lock_flags);
4232 return;
4235 /* loop through each of the commands responded by IOA. Each HRRQ buf is
4236 * protected by its own lock. Traversals must be done within this lock
4237 * as there may be multiple tasklets running on multiple CPUs. Note
4238 * that the lock is held just for picking up the response handle and
4239 * manipulating hrrq_curr/toggle_bit values.
4241 spin_lock_irqsave(lockp, hrrq_lock_flags);
4243 resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4245 while ((resp & HRRQ_TOGGLE_BIT) ==
4246 pinstance->host_toggle_bit[id]) {
4248 int cmd_index = resp >> 2;
4249 struct pmcraid_cmd *cmd = NULL;
4251 if (cmd_index < PMCRAID_MAX_CMD) {
4252 cmd = pinstance->cmd_list[cmd_index];
4253 } else {
4254 /* In case of invalid response handle, initiate IOA
4255 * reset sequence.
4257 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4259 pmcraid_err("Invalid response %d initiating reset\n",
4260 cmd_index);
4262 spin_lock_irqsave(pinstance->host->host_lock,
4263 host_lock_flags);
4264 pmcraid_initiate_reset(pinstance);
4265 spin_unlock_irqrestore(pinstance->host->host_lock,
4266 host_lock_flags);
4268 spin_lock_irqsave(lockp, hrrq_lock_flags);
4269 break;
4272 if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
4273 pinstance->hrrq_curr[id]++;
4274 } else {
4275 pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
4276 pinstance->host_toggle_bit[id] ^= 1u;
4279 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4281 spin_lock_irqsave(&pinstance->pending_pool_lock,
4282 pending_lock_flags);
4283 list_del(&cmd->free_list);
4284 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
4285 pending_lock_flags);
4286 del_timer(&cmd->timer);
4287 atomic_dec(&pinstance->outstanding_cmds);
4289 if (cmd->cmd_done == pmcraid_ioa_reset) {
4290 spin_lock_irqsave(pinstance->host->host_lock,
4291 host_lock_flags);
4292 cmd->cmd_done(cmd);
4293 spin_unlock_irqrestore(pinstance->host->host_lock,
4294 host_lock_flags);
4295 } else if (cmd->cmd_done != NULL) {
4296 cmd->cmd_done(cmd);
4298 /* loop over until we are done with all responses */
4299 spin_lock_irqsave(lockp, hrrq_lock_flags);
4300 resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4303 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4307 * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
4308 * @pinstance: pointer to adapter instance structure
4310 * This routine un-registers registered interrupt handler and
4311 * also frees irqs/vectors.
4313 * Retun Value
4314 * None
4316 static
4317 void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
4319 free_irq(pinstance->pdev->irq, &(pinstance->hrrq_vector[0]));
4323 * pmcraid_register_interrupt_handler - registers interrupt handler
4324 * @pinstance: pointer to per-adapter instance structure
4326 * Return Value
4327 * 0 on success, non-zero error code otherwise.
4329 static int
4330 pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
4332 struct pci_dev *pdev = pinstance->pdev;
4334 pinstance->hrrq_vector[0].hrrq_id = 0;
4335 pinstance->hrrq_vector[0].drv_inst = pinstance;
4336 pinstance->hrrq_vector[0].vector = 0;
4337 pinstance->num_hrrq = 1;
4338 return request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
4339 PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
4343 * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
4344 * @pinstance: per adapter instance structure pointer
4345 * @max_index: number of buffer blocks to release
4347 * Return Value
4348 * None
4350 static void
4351 pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
4353 int i;
4354 for (i = 0; i < max_index; i++) {
4355 kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
4356 pinstance->cmd_list[i] = NULL;
4358 kmem_cache_destroy(pinstance->cmd_cachep);
4359 pinstance->cmd_cachep = NULL;
4363 * pmcraid_release_control_blocks - releases buffers alloced for control blocks
4364 * @pinstance: pointer to per adapter instance structure
4365 * @max_index: number of buffers (from 0 onwards) to release
4367 * This function assumes that the command blocks for which control blocks are
4368 * linked are not released.
4370 * Return Value
4371 * None
4373 static void
4374 pmcraid_release_control_blocks(
4375 struct pmcraid_instance *pinstance,
4376 int max_index
4379 int i;
4381 if (pinstance->control_pool == NULL)
4382 return;
4384 for (i = 0; i < max_index; i++) {
4385 pci_pool_free(pinstance->control_pool,
4386 pinstance->cmd_list[i]->ioa_cb,
4387 pinstance->cmd_list[i]->ioa_cb_bus_addr);
4388 pinstance->cmd_list[i]->ioa_cb = NULL;
4389 pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
4391 pci_pool_destroy(pinstance->control_pool);
4392 pinstance->control_pool = NULL;
4396 * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
4397 * @pinstance - pointer to per adapter instance structure
4399 * Allocates memory for command blocks using kernel slab allocator.
4401 * Return Value
4402 * 0 in case of success; -ENOMEM in case of failure
4404 static int __devinit
4405 pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
4407 int i;
4409 sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
4410 pinstance->host->unique_id);
4413 pinstance->cmd_cachep = kmem_cache_create(
4414 pinstance->cmd_pool_name,
4415 sizeof(struct pmcraid_cmd), 0,
4416 SLAB_HWCACHE_ALIGN, NULL);
4417 if (!pinstance->cmd_cachep)
4418 return -ENOMEM;
4420 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4421 pinstance->cmd_list[i] =
4422 kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
4423 if (!pinstance->cmd_list[i]) {
4424 pmcraid_release_cmd_blocks(pinstance, i);
4425 return -ENOMEM;
4428 return 0;
4432 * pmcraid_allocate_control_blocks - allocates memory control blocks
4433 * @pinstance : pointer to per adapter instance structure
4435 * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
4436 * and IOASAs. This is called after command blocks are already allocated.
4438 * Return Value
4439 * 0 in case it can allocate all control blocks, otherwise -ENOMEM
4441 static int __devinit
4442 pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
4444 int i;
4446 sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
4447 pinstance->host->unique_id);
4449 pinstance->control_pool =
4450 pci_pool_create(pinstance->ctl_pool_name,
4451 pinstance->pdev,
4452 sizeof(struct pmcraid_control_block),
4453 PMCRAID_IOARCB_ALIGNMENT, 0);
4455 if (!pinstance->control_pool)
4456 return -ENOMEM;
4458 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4459 pinstance->cmd_list[i]->ioa_cb =
4460 pci_pool_alloc(
4461 pinstance->control_pool,
4462 GFP_KERNEL,
4463 &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
4465 if (!pinstance->cmd_list[i]->ioa_cb) {
4466 pmcraid_release_control_blocks(pinstance, i);
4467 return -ENOMEM;
4469 memset(pinstance->cmd_list[i]->ioa_cb, 0,
4470 sizeof(struct pmcraid_control_block));
4472 return 0;
4476 * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
4477 * @pinstance: pointer to per adapter instance structure
4478 * @maxindex: size of hrrq buffer pointer array
4480 * Return Value
4481 * None
4483 static void
4484 pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
4486 int i;
4487 for (i = 0; i < maxindex; i++) {
4489 pci_free_consistent(pinstance->pdev,
4490 HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
4491 pinstance->hrrq_start[i],
4492 pinstance->hrrq_start_bus_addr[i]);
4494 /* reset pointers and toggle bit to zeros */
4495 pinstance->hrrq_start[i] = NULL;
4496 pinstance->hrrq_start_bus_addr[i] = 0;
4497 pinstance->host_toggle_bit[i] = 0;
4502 * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
4503 * @pinstance: pointer to per adapter instance structure
4505 * Return value
4506 * 0 hrrq buffers are allocated, -ENOMEM otherwise.
4508 static int __devinit
4509 pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
4511 int i;
4512 int buf_count = PMCRAID_MAX_CMD / pinstance->num_hrrq;
4514 for (i = 0; i < pinstance->num_hrrq; i++) {
4515 int buffer_size = HRRQ_ENTRY_SIZE * buf_count;
4517 pinstance->hrrq_start[i] =
4518 pci_alloc_consistent(
4519 pinstance->pdev,
4520 buffer_size,
4521 &(pinstance->hrrq_start_bus_addr[i]));
4523 if (pinstance->hrrq_start[i] == 0) {
4524 pmcraid_err("could not allocate host rrq: %d\n", i);
4525 pmcraid_release_host_rrqs(pinstance, i);
4526 return -ENOMEM;
4529 memset(pinstance->hrrq_start[i], 0, buffer_size);
4530 pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4531 pinstance->hrrq_end[i] =
4532 pinstance->hrrq_start[i] + buf_count - 1;
4533 pinstance->host_toggle_bit[i] = 1;
4534 spin_lock_init(&pinstance->hrrq_lock[i]);
4536 return 0;
4540 * pmcraid_release_hcams - release HCAM buffers
4542 * @pinstance: pointer to per adapter instance structure
4544 * Return value
4545 * none
4547 static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
4549 if (pinstance->ccn.msg != NULL) {
4550 pci_free_consistent(pinstance->pdev,
4551 PMCRAID_AEN_HDR_SIZE +
4552 sizeof(struct pmcraid_hcam_ccn),
4553 pinstance->ccn.msg,
4554 pinstance->ccn.baddr);
4556 pinstance->ccn.msg = NULL;
4557 pinstance->ccn.hcam = NULL;
4558 pinstance->ccn.baddr = 0;
4561 if (pinstance->ldn.msg != NULL) {
4562 pci_free_consistent(pinstance->pdev,
4563 PMCRAID_AEN_HDR_SIZE +
4564 sizeof(struct pmcraid_hcam_ldn),
4565 pinstance->ldn.msg,
4566 pinstance->ldn.baddr);
4568 pinstance->ldn.msg = NULL;
4569 pinstance->ldn.hcam = NULL;
4570 pinstance->ldn.baddr = 0;
4575 * pmcraid_allocate_hcams - allocates HCAM buffers
4576 * @pinstance : pointer to per adapter instance structure
4578 * Return Value:
4579 * 0 in case of successful allocation, non-zero otherwise
4581 static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
4583 pinstance->ccn.msg = pci_alloc_consistent(
4584 pinstance->pdev,
4585 PMCRAID_AEN_HDR_SIZE +
4586 sizeof(struct pmcraid_hcam_ccn),
4587 &(pinstance->ccn.baddr));
4589 pinstance->ldn.msg = pci_alloc_consistent(
4590 pinstance->pdev,
4591 PMCRAID_AEN_HDR_SIZE +
4592 sizeof(struct pmcraid_hcam_ldn),
4593 &(pinstance->ldn.baddr));
4595 if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
4596 pmcraid_release_hcams(pinstance);
4597 } else {
4598 pinstance->ccn.hcam =
4599 (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
4600 pinstance->ldn.hcam =
4601 (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
4603 atomic_set(&pinstance->ccn.ignore, 0);
4604 atomic_set(&pinstance->ldn.ignore, 0);
4607 return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
4611 * pmcraid_release_config_buffers - release config.table buffers
4612 * @pinstance: pointer to per adapter instance structure
4614 * Return Value
4615 * none
4617 static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
4619 if (pinstance->cfg_table != NULL &&
4620 pinstance->cfg_table_bus_addr != 0) {
4621 pci_free_consistent(pinstance->pdev,
4622 sizeof(struct pmcraid_config_table),
4623 pinstance->cfg_table,
4624 pinstance->cfg_table_bus_addr);
4625 pinstance->cfg_table = NULL;
4626 pinstance->cfg_table_bus_addr = 0;
4629 if (pinstance->res_entries != NULL) {
4630 int i;
4632 for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4633 list_del(&pinstance->res_entries[i].queue);
4634 kfree(pinstance->res_entries);
4635 pinstance->res_entries = NULL;
4638 pmcraid_release_hcams(pinstance);
4642 * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
4643 * @pinstance : pointer to per adapter instance structure
4645 * Return Value
4646 * 0 for successful allocation, -ENOMEM for any failure
4648 static int __devinit
4649 pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
4651 int i;
4653 pinstance->res_entries =
4654 kzalloc(sizeof(struct pmcraid_resource_entry) *
4655 PMCRAID_MAX_RESOURCES, GFP_KERNEL);
4657 if (NULL == pinstance->res_entries) {
4658 pmcraid_err("failed to allocate memory for resource table\n");
4659 return -ENOMEM;
4662 for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4663 list_add_tail(&pinstance->res_entries[i].queue,
4664 &pinstance->free_res_q);
4666 pinstance->cfg_table =
4667 pci_alloc_consistent(pinstance->pdev,
4668 sizeof(struct pmcraid_config_table),
4669 &pinstance->cfg_table_bus_addr);
4671 if (NULL == pinstance->cfg_table) {
4672 pmcraid_err("couldn't alloc DMA memory for config table\n");
4673 pmcraid_release_config_buffers(pinstance);
4674 return -ENOMEM;
4677 if (pmcraid_allocate_hcams(pinstance)) {
4678 pmcraid_err("could not alloc DMA memory for HCAMS\n");
4679 pmcraid_release_config_buffers(pinstance);
4680 return -ENOMEM;
4683 return 0;
4687 * pmcraid_init_tasklets - registers tasklets for response handling
4689 * @pinstance: pointer adapter instance structure
4691 * Return value
4692 * none
4694 static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
4696 int i;
4697 for (i = 0; i < pinstance->num_hrrq; i++)
4698 tasklet_init(&pinstance->isr_tasklet[i],
4699 pmcraid_tasklet_function,
4700 (unsigned long)&pinstance->hrrq_vector[i]);
4704 * pmcraid_kill_tasklets - destroys tasklets registered for response handling
4706 * @pinstance: pointer to adapter instance structure
4708 * Return value
4709 * none
4711 static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
4713 int i;
4714 for (i = 0; i < pinstance->num_hrrq; i++)
4715 tasklet_kill(&pinstance->isr_tasklet[i]);
4719 * pmcraid_init_buffers - allocates memory and initializes various structures
4720 * @pinstance: pointer to per adapter instance structure
4722 * This routine pre-allocates memory based on the type of block as below:
4723 * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
4724 * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
4725 * config-table entries : DMAable memory using pci_alloc_consistent
4726 * HostRRQs : DMAable memory, using pci_alloc_consistent
4728 * Return Value
4729 * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
4731 static int __devinit pmcraid_init_buffers(struct pmcraid_instance *pinstance)
4733 int i;
4735 if (pmcraid_allocate_host_rrqs(pinstance)) {
4736 pmcraid_err("couldn't allocate memory for %d host rrqs\n",
4737 pinstance->num_hrrq);
4738 return -ENOMEM;
4741 if (pmcraid_allocate_config_buffers(pinstance)) {
4742 pmcraid_err("couldn't allocate memory for config buffers\n");
4743 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4744 return -ENOMEM;
4747 if (pmcraid_allocate_cmd_blocks(pinstance)) {
4748 pmcraid_err("couldn't allocate memory for cmd blocks \n");
4749 pmcraid_release_config_buffers(pinstance);
4750 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4751 return -ENOMEM;
4754 if (pmcraid_allocate_control_blocks(pinstance)) {
4755 pmcraid_err("couldn't allocate memory control blocks \n");
4756 pmcraid_release_config_buffers(pinstance);
4757 pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
4758 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4759 return -ENOMEM;
4762 /* Initialize all the command blocks and add them to free pool. No
4763 * need to lock (free_pool_lock) as this is done in initialization
4764 * itself
4766 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4767 struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
4768 pmcraid_init_cmdblk(cmdp, i);
4769 cmdp->drv_inst = pinstance;
4770 list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
4773 return 0;
4777 * pmcraid_reinit_buffers - resets various buffer pointers
4778 * @pinstance: pointer to adapter instance
4779 * Return value
4780 * none
4782 static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
4784 int i;
4785 int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
4787 for (i = 0; i < pinstance->num_hrrq; i++) {
4788 memset(pinstance->hrrq_start[i], 0, buffer_size);
4789 pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4790 pinstance->hrrq_end[i] =
4791 pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
4792 pinstance->host_toggle_bit[i] = 1;
4797 * pmcraid_init_instance - initialize per instance data structure
4798 * @pdev: pointer to pci device structure
4799 * @host: pointer to Scsi_Host structure
4800 * @mapped_pci_addr: memory mapped IOA configuration registers
4802 * Return Value
4803 * 0 on success, non-zero in case of any failure
4805 static int __devinit pmcraid_init_instance(
4806 struct pci_dev *pdev,
4807 struct Scsi_Host *host,
4808 void __iomem *mapped_pci_addr
4811 struct pmcraid_instance *pinstance =
4812 (struct pmcraid_instance *)host->hostdata;
4814 pinstance->host = host;
4815 pinstance->pdev = pdev;
4817 /* Initialize register addresses */
4818 pinstance->mapped_dma_addr = mapped_pci_addr;
4820 /* Initialize chip-specific details */
4822 struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
4823 struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
4825 pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
4827 pint_regs->ioa_host_interrupt_reg =
4828 mapped_pci_addr + chip_cfg->ioa_host_intr;
4829 pint_regs->ioa_host_interrupt_clr_reg =
4830 mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
4831 pint_regs->host_ioa_interrupt_reg =
4832 mapped_pci_addr + chip_cfg->host_ioa_intr;
4833 pint_regs->host_ioa_interrupt_clr_reg =
4834 mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
4836 /* Current version of firmware exposes interrupt mask set
4837 * and mask clr registers through memory mapped bar0.
4839 pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
4840 pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
4841 pint_regs->ioa_host_interrupt_mask_reg =
4842 mapped_pci_addr + chip_cfg->ioa_host_mask;
4843 pint_regs->ioa_host_interrupt_mask_clr_reg =
4844 mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
4845 pint_regs->global_interrupt_mask_reg =
4846 mapped_pci_addr + chip_cfg->global_intr_mask;
4849 pinstance->ioa_reset_attempts = 0;
4850 init_waitqueue_head(&pinstance->reset_wait_q);
4852 atomic_set(&pinstance->outstanding_cmds, 0);
4853 atomic_set(&pinstance->expose_resources, 0);
4855 INIT_LIST_HEAD(&pinstance->free_res_q);
4856 INIT_LIST_HEAD(&pinstance->used_res_q);
4857 INIT_LIST_HEAD(&pinstance->free_cmd_pool);
4858 INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
4860 spin_lock_init(&pinstance->free_pool_lock);
4861 spin_lock_init(&pinstance->pending_pool_lock);
4862 spin_lock_init(&pinstance->resource_lock);
4863 mutex_init(&pinstance->aen_queue_lock);
4865 /* Work-queue (Shared) for deferred processing error handling */
4866 INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
4868 /* Initialize the default log_level */
4869 pinstance->current_log_level = pmcraid_log_level;
4871 /* Setup variables required for reset engine */
4872 pinstance->ioa_state = IOA_STATE_UNKNOWN;
4873 pinstance->reset_cmd = NULL;
4874 return 0;
4878 * pmcraid_release_buffers - release per-adapter buffers allocated
4880 * @pinstance: pointer to adapter soft state
4882 * Return Value
4883 * none
4885 static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
4887 pmcraid_release_config_buffers(pinstance);
4888 pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
4889 pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
4890 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4895 * pmcraid_shutdown - shutdown adapter controller.
4896 * @pdev: pci device struct
4898 * Issues an adapter shutdown to the card waits for its completion
4900 * Return value
4901 * none
4903 static void pmcraid_shutdown(struct pci_dev *pdev)
4905 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
4906 pmcraid_reset_bringdown(pinstance);
4911 * pmcraid_get_minor - returns unused minor number from minor number bitmap
4913 static unsigned short pmcraid_get_minor(void)
4915 int minor;
4917 minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
4918 __set_bit(minor, pmcraid_minor);
4919 return minor;
4923 * pmcraid_release_minor - releases given minor back to minor number bitmap
4925 static void pmcraid_release_minor(unsigned short minor)
4927 __clear_bit(minor, pmcraid_minor);
4931 * pmcraid_setup_chrdev - allocates a minor number and registers a char device
4933 * @pinstance: pointer to adapter instance for which to register device
4935 * Return value
4936 * 0 in case of success, otherwise non-zero
4938 static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
4940 int minor;
4941 int error;
4943 minor = pmcraid_get_minor();
4944 cdev_init(&pinstance->cdev, &pmcraid_fops);
4945 pinstance->cdev.owner = THIS_MODULE;
4947 error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
4949 if (error)
4950 pmcraid_release_minor(minor);
4951 else
4952 device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
4953 NULL, "pmcsas%u", minor);
4954 return error;
4958 * pmcraid_release_chrdev - unregisters per-adapter management interface
4960 * @pinstance: pointer to adapter instance structure
4962 * Return value
4963 * none
4965 static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
4967 pmcraid_release_minor(MINOR(pinstance->cdev.dev));
4968 device_destroy(pmcraid_class,
4969 MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
4970 cdev_del(&pinstance->cdev);
4974 * pmcraid_remove - IOA hot plug remove entry point
4975 * @pdev: pci device struct
4977 * Return value
4978 * none
4980 static void __devexit pmcraid_remove(struct pci_dev *pdev)
4982 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
4984 /* remove the management interface (/dev file) for this device */
4985 pmcraid_release_chrdev(pinstance);
4987 /* remove host template from scsi midlayer */
4988 scsi_remove_host(pinstance->host);
4990 /* block requests from mid-layer */
4991 scsi_block_requests(pinstance->host);
4993 /* initiate shutdown adapter */
4994 pmcraid_shutdown(pdev);
4996 pmcraid_disable_interrupts(pinstance, ~0);
4997 flush_scheduled_work();
4999 pmcraid_kill_tasklets(pinstance);
5000 pmcraid_unregister_interrupt_handler(pinstance);
5001 pmcraid_release_buffers(pinstance);
5002 iounmap(pinstance->mapped_dma_addr);
5003 pci_release_regions(pdev);
5004 scsi_host_put(pinstance->host);
5005 pci_disable_device(pdev);
5007 return;
5010 #ifdef CONFIG_PM
5012 * pmcraid_suspend - driver suspend entry point for power management
5013 * @pdev: PCI device structure
5014 * @state: PCI power state to suspend routine
5016 * Return Value - 0 always
5018 static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
5020 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5022 pmcraid_shutdown(pdev);
5023 pmcraid_disable_interrupts(pinstance, ~0);
5024 pmcraid_kill_tasklets(pinstance);
5025 pci_set_drvdata(pinstance->pdev, pinstance);
5026 pmcraid_unregister_interrupt_handler(pinstance);
5027 pci_save_state(pdev);
5028 pci_disable_device(pdev);
5029 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5031 return 0;
5035 * pmcraid_resume - driver resume entry point PCI power management
5036 * @pdev: PCI device structure
5038 * Return Value - 0 in case of success. Error code in case of any failure
5040 static int pmcraid_resume(struct pci_dev *pdev)
5042 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5043 struct Scsi_Host *host = pinstance->host;
5044 int rc;
5045 int hrrqs;
5047 pci_set_power_state(pdev, PCI_D0);
5048 pci_enable_wake(pdev, PCI_D0, 0);
5049 pci_restore_state(pdev);
5051 rc = pci_enable_device(pdev);
5053 if (rc) {
5054 dev_err(&pdev->dev, "resume: Enable device failed\n");
5055 return rc;
5058 pci_set_master(pdev);
5060 if ((sizeof(dma_addr_t) == 4) ||
5061 pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5062 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5064 if (rc == 0)
5065 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5067 if (rc != 0) {
5068 dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
5069 goto disable_device;
5072 atomic_set(&pinstance->outstanding_cmds, 0);
5073 hrrqs = pinstance->num_hrrq;
5074 rc = pmcraid_register_interrupt_handler(pinstance);
5076 if (rc) {
5077 dev_err(&pdev->dev,
5078 "resume: couldn't register interrupt handlers\n");
5079 rc = -ENODEV;
5080 goto release_host;
5083 pmcraid_init_tasklets(pinstance);
5084 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5086 /* Start with hard reset sequence which brings up IOA to operational
5087 * state as well as completes the reset sequence.
5089 pinstance->ioa_hard_reset = 1;
5091 /* Start IOA firmware initialization and bring card to Operational
5092 * state.
5094 if (pmcraid_reset_bringup(pinstance)) {
5095 dev_err(&pdev->dev, "couldn't initialize IOA \n");
5096 rc = -ENODEV;
5097 goto release_tasklets;
5100 return 0;
5102 release_tasklets:
5103 pmcraid_kill_tasklets(pinstance);
5104 pmcraid_unregister_interrupt_handler(pinstance);
5106 release_host:
5107 scsi_host_put(host);
5109 disable_device:
5110 pci_disable_device(pdev);
5112 return rc;
5115 #else
5117 #define pmcraid_suspend NULL
5118 #define pmcraid_resume NULL
5120 #endif /* CONFIG_PM */
5123 * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
5124 * completion of the ioa reset
5125 * @cmd: pointer to reset command block
5127 static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
5129 struct pmcraid_instance *pinstance = cmd->drv_inst;
5130 unsigned long flags;
5132 spin_lock_irqsave(pinstance->host->host_lock, flags);
5133 pmcraid_ioa_reset(cmd);
5134 spin_unlock_irqrestore(pinstance->host->host_lock, flags);
5135 scsi_unblock_requests(pinstance->host);
5136 schedule_work(&pinstance->worker_q);
5140 * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
5142 * @cmd: pointer to pmcraid_cmd structure
5144 * Return Value
5145 * 0 for success or non-zero for failure cases
5147 static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
5149 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5150 void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
5152 pmcraid_reinit_cmdblk(cmd);
5154 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5155 ioarcb->request_type = REQ_TYPE_IOACMD;
5156 ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
5157 ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
5159 /* If this was called as part of resource table reinitialization due to
5160 * lost CCN, it is enough to return the command block back to free pool
5161 * as part of set_supported_devs completion function.
5163 if (cmd->drv_inst->reinit_cfg_table) {
5164 cmd->drv_inst->reinit_cfg_table = 0;
5165 cmd->release = 1;
5166 cmd_done = pmcraid_reinit_cfgtable_done;
5169 /* we will be done with the reset sequence after set supported devices,
5170 * setup the done function to return the command block back to free
5171 * pool
5173 pmcraid_send_cmd(cmd,
5174 cmd_done,
5175 PMCRAID_SET_SUP_DEV_TIMEOUT,
5176 pmcraid_timeout_handler);
5177 return;
5181 * pmcraid_init_res_table - Initialize the resource table
5182 * @cmd: pointer to pmcraid command struct
5184 * This function looks through the existing resource table, comparing
5185 * it with the config table. This function will take care of old/new
5186 * devices and schedule adding/removing them from the mid-layer
5187 * as appropriate.
5189 * Return value
5190 * None
5192 static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
5194 struct pmcraid_instance *pinstance = cmd->drv_inst;
5195 struct pmcraid_resource_entry *res, *temp;
5196 struct pmcraid_config_table_entry *cfgte;
5197 unsigned long lock_flags;
5198 int found, rc, i;
5199 LIST_HEAD(old_res);
5201 if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
5202 pmcraid_err("IOA requires microcode download\n");
5204 /* resource list is protected by pinstance->resource_lock.
5205 * init_res_table can be called from probe (user-thread) or runtime
5206 * reset (timer/tasklet)
5208 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
5210 list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
5211 list_move_tail(&res->queue, &old_res);
5213 for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
5214 cfgte = &pinstance->cfg_table->entries[i];
5216 if (!pmcraid_expose_resource(cfgte))
5217 continue;
5219 found = 0;
5221 /* If this entry was already detected and initialized */
5222 list_for_each_entry_safe(res, temp, &old_res, queue) {
5224 rc = memcmp(&res->cfg_entry.resource_address,
5225 &cfgte->resource_address,
5226 sizeof(cfgte->resource_address));
5227 if (!rc) {
5228 list_move_tail(&res->queue,
5229 &pinstance->used_res_q);
5230 found = 1;
5231 break;
5235 /* If this is new entry, initialize it and add it the queue */
5236 if (!found) {
5238 if (list_empty(&pinstance->free_res_q)) {
5239 pmcraid_err("Too many devices attached\n");
5240 break;
5243 found = 1;
5244 res = list_entry(pinstance->free_res_q.next,
5245 struct pmcraid_resource_entry, queue);
5247 res->scsi_dev = NULL;
5248 res->change_detected = RES_CHANGE_ADD;
5249 res->reset_progress = 0;
5250 list_move_tail(&res->queue, &pinstance->used_res_q);
5253 /* copy new configuration table entry details into driver
5254 * maintained resource entry
5256 if (found) {
5257 memcpy(&res->cfg_entry, cfgte,
5258 sizeof(struct pmcraid_config_table_entry));
5259 pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
5260 res->cfg_entry.resource_type,
5261 res->cfg_entry.unique_flags1,
5262 le32_to_cpu(res->cfg_entry.resource_address));
5266 /* Detect any deleted entries, mark them for deletion from mid-layer */
5267 list_for_each_entry_safe(res, temp, &old_res, queue) {
5269 if (res->scsi_dev) {
5270 res->change_detected = RES_CHANGE_DEL;
5271 res->cfg_entry.resource_handle =
5272 PMCRAID_INVALID_RES_HANDLE;
5273 list_move_tail(&res->queue, &pinstance->used_res_q);
5274 } else {
5275 list_move_tail(&res->queue, &pinstance->free_res_q);
5279 /* release the resource list lock */
5280 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
5281 pmcraid_set_supported_devs(cmd);
5285 * pmcraid_querycfg - Send a Query IOA Config to the adapter.
5286 * @cmd: pointer pmcraid_cmd struct
5288 * This function sends a Query IOA Configuration command to the adapter to
5289 * retrieve the IOA configuration table.
5291 * Return value:
5292 * none
5294 static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
5296 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5297 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
5298 struct pmcraid_instance *pinstance = cmd->drv_inst;
5299 int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
5301 ioarcb->request_type = REQ_TYPE_IOACMD;
5302 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5304 ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
5306 /* firmware requires 4-byte length field, specified in B.E format */
5307 memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
5309 /* Since entire config table can be described by single IOADL, it can
5310 * be part of IOARCB itself
5312 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
5313 offsetof(struct pmcraid_ioarcb,
5314 add_data.u.ioadl[0]));
5315 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
5316 ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
5318 ioarcb->request_flags0 |= NO_LINK_DESCS;
5319 ioarcb->data_transfer_length =
5320 cpu_to_le32(sizeof(struct pmcraid_config_table));
5322 ioadl = &(ioarcb->add_data.u.ioadl[0]);
5323 ioadl->flags = IOADL_FLAGS_LAST_DESC;
5324 ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
5325 ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
5327 pmcraid_send_cmd(cmd, pmcraid_init_res_table,
5328 PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
5333 * pmcraid_probe - PCI probe entry pointer for PMC MaxRaid controller driver
5334 * @pdev: pointer to pci device structure
5335 * @dev_id: pointer to device ids structure
5337 * Return Value
5338 * returns 0 if the device is claimed and successfully configured.
5339 * returns non-zero error code in case of any failure
5341 static int __devinit pmcraid_probe(
5342 struct pci_dev *pdev,
5343 const struct pci_device_id *dev_id
5346 struct pmcraid_instance *pinstance;
5347 struct Scsi_Host *host;
5348 void __iomem *mapped_pci_addr;
5349 int rc = PCIBIOS_SUCCESSFUL;
5351 if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
5352 pmcraid_err
5353 ("maximum number(%d) of supported adapters reached\n",
5354 atomic_read(&pmcraid_adapter_count));
5355 return -ENOMEM;
5358 atomic_inc(&pmcraid_adapter_count);
5359 rc = pci_enable_device(pdev);
5361 if (rc) {
5362 dev_err(&pdev->dev, "Cannot enable adapter\n");
5363 atomic_dec(&pmcraid_adapter_count);
5364 return rc;
5367 dev_info(&pdev->dev,
5368 "Found new IOA(%x:%x), Total IOA count: %d\n",
5369 pdev->vendor, pdev->device,
5370 atomic_read(&pmcraid_adapter_count));
5372 rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
5374 if (rc < 0) {
5375 dev_err(&pdev->dev,
5376 "Couldn't register memory range of registers\n");
5377 goto out_disable_device;
5380 mapped_pci_addr = pci_iomap(pdev, 0, 0);
5382 if (!mapped_pci_addr) {
5383 dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
5384 rc = -ENOMEM;
5385 goto out_release_regions;
5388 pci_set_master(pdev);
5390 /* Firmware requires the system bus address of IOARCB to be within
5391 * 32-bit addressable range though it has 64-bit IOARRIN register.
5392 * However, firmware supports 64-bit streaming DMA buffers, whereas
5393 * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
5394 * returns memory within 4GB (if not, change this logic), coherent
5395 * buffers are within firmware acceptible address ranges.
5397 if ((sizeof(dma_addr_t) == 4) ||
5398 pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5399 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5401 /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
5402 * bit mask for pci_alloc_consistent to return addresses within 4GB
5404 if (rc == 0)
5405 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5407 if (rc != 0) {
5408 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
5409 goto cleanup_nomem;
5412 host = scsi_host_alloc(&pmcraid_host_template,
5413 sizeof(struct pmcraid_instance));
5415 if (!host) {
5416 dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
5417 rc = -ENOMEM;
5418 goto cleanup_nomem;
5421 host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
5422 host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
5423 host->unique_id = host->host_no;
5424 host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
5425 host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
5427 /* zero out entire instance structure */
5428 pinstance = (struct pmcraid_instance *)host->hostdata;
5429 memset(pinstance, 0, sizeof(*pinstance));
5431 pinstance->chip_cfg =
5432 (struct pmcraid_chip_details *)(dev_id->driver_data);
5434 rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
5436 if (rc < 0) {
5437 dev_err(&pdev->dev, "failed to initialize adapter instance\n");
5438 goto out_scsi_host_put;
5441 pci_set_drvdata(pdev, pinstance);
5443 /* Save PCI config-space for use following the reset */
5444 rc = pci_save_state(pinstance->pdev);
5446 if (rc != 0) {
5447 dev_err(&pdev->dev, "Failed to save PCI config space\n");
5448 goto out_scsi_host_put;
5451 pmcraid_disable_interrupts(pinstance, ~0);
5453 rc = pmcraid_register_interrupt_handler(pinstance);
5455 if (rc) {
5456 dev_err(&pdev->dev, "couldn't register interrupt handler\n");
5457 goto out_scsi_host_put;
5460 pmcraid_init_tasklets(pinstance);
5462 /* allocate verious buffers used by LLD.*/
5463 rc = pmcraid_init_buffers(pinstance);
5465 if (rc) {
5466 pmcraid_err("couldn't allocate memory blocks\n");
5467 goto out_unregister_isr;
5470 /* check the reset type required */
5471 pmcraid_reset_type(pinstance);
5473 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5475 /* Start IOA firmware initialization and bring card to Operational
5476 * state.
5478 pmcraid_info("starting IOA initialization sequence\n");
5479 if (pmcraid_reset_bringup(pinstance)) {
5480 dev_err(&pdev->dev, "couldn't initialize IOA \n");
5481 rc = 1;
5482 goto out_release_bufs;
5485 /* Add adapter instance into mid-layer list */
5486 rc = scsi_add_host(pinstance->host, &pdev->dev);
5487 if (rc != 0) {
5488 pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
5489 goto out_release_bufs;
5492 scsi_scan_host(pinstance->host);
5494 rc = pmcraid_setup_chrdev(pinstance);
5496 if (rc != 0) {
5497 pmcraid_err("couldn't create mgmt interface, error: %x\n",
5498 rc);
5499 goto out_remove_host;
5502 /* Schedule worker thread to handle CCN and take care of adding and
5503 * removing devices to OS
5505 atomic_set(&pinstance->expose_resources, 1);
5506 schedule_work(&pinstance->worker_q);
5507 return rc;
5509 out_remove_host:
5510 scsi_remove_host(host);
5512 out_release_bufs:
5513 pmcraid_release_buffers(pinstance);
5515 out_unregister_isr:
5516 pmcraid_kill_tasklets(pinstance);
5517 pmcraid_unregister_interrupt_handler(pinstance);
5519 out_scsi_host_put:
5520 scsi_host_put(host);
5522 cleanup_nomem:
5523 iounmap(mapped_pci_addr);
5525 out_release_regions:
5526 pci_release_regions(pdev);
5528 out_disable_device:
5529 atomic_dec(&pmcraid_adapter_count);
5530 pci_set_drvdata(pdev, NULL);
5531 pci_disable_device(pdev);
5532 return -ENODEV;
5536 * PCI driver structure of pcmraid driver
5538 static struct pci_driver pmcraid_driver = {
5539 .name = PMCRAID_DRIVER_NAME,
5540 .id_table = pmcraid_pci_table,
5541 .probe = pmcraid_probe,
5542 .remove = pmcraid_remove,
5543 .suspend = pmcraid_suspend,
5544 .resume = pmcraid_resume,
5545 .shutdown = pmcraid_shutdown
5549 * pmcraid_init - module load entry point
5551 static int __init pmcraid_init(void)
5553 dev_t dev;
5554 int error;
5556 pmcraid_info("%s Device Driver version: %s %s\n",
5557 PMCRAID_DRIVER_NAME,
5558 PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
5560 error = alloc_chrdev_region(&dev, 0,
5561 PMCRAID_MAX_ADAPTERS,
5562 PMCRAID_DEVFILE);
5564 if (error) {
5565 pmcraid_err("failed to get a major number for adapters\n");
5566 goto out_init;
5569 pmcraid_major = MAJOR(dev);
5570 pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
5572 if (IS_ERR(pmcraid_class)) {
5573 error = PTR_ERR(pmcraid_class);
5574 pmcraid_err("failed to register with with sysfs, error = %x\n",
5575 error);
5576 goto out_unreg_chrdev;
5579 error = pmcraid_netlink_init();
5581 if (error)
5582 goto out_unreg_chrdev;
5584 error = pci_register_driver(&pmcraid_driver);
5586 if (error == 0)
5587 goto out_init;
5589 pmcraid_err("failed to register pmcraid driver, error = %x\n",
5590 error);
5591 class_destroy(pmcraid_class);
5592 pmcraid_netlink_release();
5594 out_unreg_chrdev:
5595 unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
5597 out_init:
5598 return error;
5602 * pmcraid_exit - module unload entry point
5604 static void __exit pmcraid_exit(void)
5606 pmcraid_netlink_release();
5607 class_destroy(pmcraid_class);
5608 unregister_chrdev_region(MKDEV(pmcraid_major, 0),
5609 PMCRAID_MAX_ADAPTERS);
5610 pci_unregister_driver(&pmcraid_driver);
5613 module_init(pmcraid_init);
5614 module_exit(pmcraid_exit);