rtc-m41t80: use nonseekable_open()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / pmcraid.c
blobc44e4ab4e938db7a3d370d9406a801f324bc7132
1 /*
2 * pmcraid.c -- driver for PMC Sierra MaxRAID controller adapters
4 * Written By: Anil Ravindranath<anil_ravindranath@pmc-sierra.com>
5 * PMC-Sierra Inc
7 * Copyright (C) 2008, 2009 PMC Sierra Inc
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
22 * USA
25 #include <linux/fs.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/errno.h>
29 #include <linux/kernel.h>
30 #include <linux/ioport.h>
31 #include <linux/delay.h>
32 #include <linux/pci.h>
33 #include <linux/wait.h>
34 #include <linux/spinlock.h>
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
37 #include <linux/blkdev.h>
38 #include <linux/firmware.h>
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
41 #include <linux/hdreg.h>
42 #include <linux/version.h>
43 #include <linux/io.h>
44 #include <linux/slab.h>
45 #include <asm/irq.h>
46 #include <asm/processor.h>
47 #include <linux/libata.h>
48 #include <linux/mutex.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_host.h>
51 #include <scsi/scsi_device.h>
52 #include <scsi/scsi_tcq.h>
53 #include <scsi/scsi_eh.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsicam.h>
57 #include "pmcraid.h"
60 * Module configuration parameters
62 static unsigned int pmcraid_debug_log;
63 static unsigned int pmcraid_disable_aen;
64 static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
67 * Data structures to support multiple adapters by the LLD.
68 * pmcraid_adapter_count - count of configured adapters
70 static atomic_t pmcraid_adapter_count = ATOMIC_INIT(0);
73 * Supporting user-level control interface through IOCTL commands.
74 * pmcraid_major - major number to use
75 * pmcraid_minor - minor number(s) to use
77 static unsigned int pmcraid_major;
78 static struct class *pmcraid_class;
79 DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
82 * Module parameters
84 MODULE_AUTHOR("Anil Ravindranath<anil_ravindranath@pmc-sierra.com>");
85 MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
86 MODULE_LICENSE("GPL");
87 MODULE_VERSION(PMCRAID_DRIVER_VERSION);
89 module_param_named(log_level, pmcraid_log_level, uint, (S_IRUGO | S_IWUSR));
90 MODULE_PARM_DESC(log_level,
91 "Enables firmware error code logging, default :1 high-severity"
92 " errors, 2: all errors including high-severity errors,"
93 " 0: disables logging");
95 module_param_named(debug, pmcraid_debug_log, uint, (S_IRUGO | S_IWUSR));
96 MODULE_PARM_DESC(debug,
97 "Enable driver verbose message logging. Set 1 to enable."
98 "(default: 0)");
100 module_param_named(disable_aen, pmcraid_disable_aen, uint, (S_IRUGO | S_IWUSR));
101 MODULE_PARM_DESC(disable_aen,
102 "Disable driver aen notifications to apps. Set 1 to disable."
103 "(default: 0)");
105 /* chip specific constants for PMC MaxRAID controllers (same for
106 * 0x5220 and 0x8010
108 static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
110 .ioastatus = 0x0,
111 .ioarrin = 0x00040,
112 .mailbox = 0x7FC30,
113 .global_intr_mask = 0x00034,
114 .ioa_host_intr = 0x0009C,
115 .ioa_host_intr_clr = 0x000A0,
116 .ioa_host_mask = 0x7FC28,
117 .ioa_host_mask_clr = 0x7FC28,
118 .host_ioa_intr = 0x00020,
119 .host_ioa_intr_clr = 0x00020,
120 .transop_timeout = 300
125 * PCI device ids supported by pmcraid driver
127 static struct pci_device_id pmcraid_pci_table[] __devinitdata = {
128 { PCI_DEVICE(PCI_VENDOR_ID_PMC, PCI_DEVICE_ID_PMC_MAXRAID),
129 0, 0, (kernel_ulong_t)&pmcraid_chip_cfg[0]
134 MODULE_DEVICE_TABLE(pci, pmcraid_pci_table);
139 * pmcraid_slave_alloc - Prepare for commands to a device
140 * @scsi_dev: scsi device struct
142 * This function is called by mid-layer prior to sending any command to the new
143 * device. Stores resource entry details of the device in scsi_device struct.
144 * Queuecommand uses the resource handle and other details to fill up IOARCB
145 * while sending commands to the device.
147 * Return value:
148 * 0 on success / -ENXIO if device does not exist
150 static int pmcraid_slave_alloc(struct scsi_device *scsi_dev)
152 struct pmcraid_resource_entry *temp, *res = NULL;
153 struct pmcraid_instance *pinstance;
154 u8 target, bus, lun;
155 unsigned long lock_flags;
156 int rc = -ENXIO;
157 pinstance = shost_priv(scsi_dev->host);
159 /* Driver exposes VSET and GSCSI resources only; all other device types
160 * are not exposed. Resource list is synchronized using resource lock
161 * so any traversal or modifications to the list should be done inside
162 * this lock
164 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
165 list_for_each_entry(temp, &pinstance->used_res_q, queue) {
167 /* do not expose VSETs with order-ids > MAX_VSET_TARGETS */
168 if (RES_IS_VSET(temp->cfg_entry)) {
169 target = temp->cfg_entry.unique_flags1;
170 if (target > PMCRAID_MAX_VSET_TARGETS)
171 continue;
172 bus = PMCRAID_VSET_BUS_ID;
173 lun = 0;
174 } else if (RES_IS_GSCSI(temp->cfg_entry)) {
175 target = RES_TARGET(temp->cfg_entry.resource_address);
176 bus = PMCRAID_PHYS_BUS_ID;
177 lun = RES_LUN(temp->cfg_entry.resource_address);
178 } else {
179 continue;
182 if (bus == scsi_dev->channel &&
183 target == scsi_dev->id &&
184 lun == scsi_dev->lun) {
185 res = temp;
186 break;
190 if (res) {
191 res->scsi_dev = scsi_dev;
192 scsi_dev->hostdata = res;
193 res->change_detected = 0;
194 atomic_set(&res->read_failures, 0);
195 atomic_set(&res->write_failures, 0);
196 rc = 0;
198 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
199 return rc;
203 * pmcraid_slave_configure - Configures a SCSI device
204 * @scsi_dev: scsi device struct
206 * This fucntion is executed by SCSI mid layer just after a device is first
207 * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
208 * timeout value (default 30s) will be over-written to a higher value (60s)
209 * and max_sectors value will be over-written to 512. It also sets queue depth
210 * to host->cmd_per_lun value
212 * Return value:
213 * 0 on success
215 static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
217 struct pmcraid_resource_entry *res = scsi_dev->hostdata;
219 if (!res)
220 return 0;
222 /* LLD exposes VSETs and Enclosure devices only */
223 if (RES_IS_GSCSI(res->cfg_entry) &&
224 scsi_dev->type != TYPE_ENCLOSURE)
225 return -ENXIO;
227 pmcraid_info("configuring %x:%x:%x:%x\n",
228 scsi_dev->host->unique_id,
229 scsi_dev->channel,
230 scsi_dev->id,
231 scsi_dev->lun);
233 if (RES_IS_GSCSI(res->cfg_entry)) {
234 scsi_dev->allow_restart = 1;
235 } else if (RES_IS_VSET(res->cfg_entry)) {
236 scsi_dev->allow_restart = 1;
237 blk_queue_rq_timeout(scsi_dev->request_queue,
238 PMCRAID_VSET_IO_TIMEOUT);
239 blk_queue_max_hw_sectors(scsi_dev->request_queue,
240 PMCRAID_VSET_MAX_SECTORS);
243 if (scsi_dev->tagged_supported &&
244 (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
245 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
246 scsi_adjust_queue_depth(scsi_dev, MSG_SIMPLE_TAG,
247 scsi_dev->host->cmd_per_lun);
248 } else {
249 scsi_adjust_queue_depth(scsi_dev, 0,
250 scsi_dev->host->cmd_per_lun);
253 return 0;
257 * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
259 * @scsi_dev: scsi device struct
261 * This is called by mid-layer before removing a device. Pointer assignments
262 * done in pmcraid_slave_alloc will be reset to NULL here.
264 * Return value
265 * none
267 static void pmcraid_slave_destroy(struct scsi_device *scsi_dev)
269 struct pmcraid_resource_entry *res;
271 res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
273 if (res)
274 res->scsi_dev = NULL;
276 scsi_dev->hostdata = NULL;
280 * pmcraid_change_queue_depth - Change the device's queue depth
281 * @scsi_dev: scsi device struct
282 * @depth: depth to set
283 * @reason: calling context
285 * Return value
286 * actual depth set
288 static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth,
289 int reason)
291 if (reason != SCSI_QDEPTH_DEFAULT)
292 return -EOPNOTSUPP;
294 if (depth > PMCRAID_MAX_CMD_PER_LUN)
295 depth = PMCRAID_MAX_CMD_PER_LUN;
297 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), depth);
299 return scsi_dev->queue_depth;
303 * pmcraid_change_queue_type - Change the device's queue type
304 * @scsi_dev: scsi device struct
305 * @tag: type of tags to use
307 * Return value:
308 * actual queue type set
310 static int pmcraid_change_queue_type(struct scsi_device *scsi_dev, int tag)
312 struct pmcraid_resource_entry *res;
314 res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
316 if ((res) && scsi_dev->tagged_supported &&
317 (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
318 scsi_set_tag_type(scsi_dev, tag);
320 if (tag)
321 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
322 else
323 scsi_deactivate_tcq(scsi_dev, scsi_dev->queue_depth);
324 } else
325 tag = 0;
327 return tag;
332 * pmcraid_init_cmdblk - initializes a command block
334 * @cmd: pointer to struct pmcraid_cmd to be initialized
335 * @index: if >=0 first time initialization; otherwise reinitialization
337 * Return Value
338 * None
340 void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
342 struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
343 dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
345 if (index >= 0) {
346 /* first time initialization (called from probe) */
347 u32 ioasa_offset =
348 offsetof(struct pmcraid_control_block, ioasa);
350 cmd->index = index;
351 ioarcb->response_handle = cpu_to_le32(index << 2);
352 ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
353 ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
354 ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
355 } else {
356 /* re-initialization of various lengths, called once command is
357 * processed by IOA
359 memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
360 ioarcb->request_flags0 = 0;
361 ioarcb->request_flags1 = 0;
362 ioarcb->cmd_timeout = 0;
363 ioarcb->ioarcb_bus_addr &= (~0x1FULL);
364 ioarcb->ioadl_bus_addr = 0;
365 ioarcb->ioadl_length = 0;
366 ioarcb->data_transfer_length = 0;
367 ioarcb->add_cmd_param_length = 0;
368 ioarcb->add_cmd_param_offset = 0;
369 cmd->ioa_cb->ioasa.ioasc = 0;
370 cmd->ioa_cb->ioasa.residual_data_length = 0;
371 cmd->u.time_left = 0;
374 cmd->cmd_done = NULL;
375 cmd->scsi_cmd = NULL;
376 cmd->release = 0;
377 cmd->completion_req = 0;
378 cmd->dma_handle = 0;
379 init_timer(&cmd->timer);
383 * pmcraid_reinit_cmdblk - reinitialize a command block
385 * @cmd: pointer to struct pmcraid_cmd to be reinitialized
387 * Return Value
388 * None
390 static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
392 pmcraid_init_cmdblk(cmd, -1);
396 * pmcraid_get_free_cmd - get a free cmd block from command block pool
397 * @pinstance: adapter instance structure
399 * Return Value:
400 * returns pointer to cmd block or NULL if no blocks are available
402 static struct pmcraid_cmd *pmcraid_get_free_cmd(
403 struct pmcraid_instance *pinstance
406 struct pmcraid_cmd *cmd = NULL;
407 unsigned long lock_flags;
409 /* free cmd block list is protected by free_pool_lock */
410 spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
412 if (!list_empty(&pinstance->free_cmd_pool)) {
413 cmd = list_entry(pinstance->free_cmd_pool.next,
414 struct pmcraid_cmd, free_list);
415 list_del(&cmd->free_list);
417 spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
419 /* Initialize the command block before giving it the caller */
420 if (cmd != NULL)
421 pmcraid_reinit_cmdblk(cmd);
422 return cmd;
426 * pmcraid_return_cmd - return a completed command block back into free pool
427 * @cmd: pointer to the command block
429 * Return Value:
430 * nothing
432 void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
434 struct pmcraid_instance *pinstance = cmd->drv_inst;
435 unsigned long lock_flags;
437 spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
438 list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
439 spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
443 * pmcraid_read_interrupts - reads IOA interrupts
445 * @pinstance: pointer to adapter instance structure
447 * Return value
448 * interrupts read from IOA
450 static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
452 return ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
456 * pmcraid_disable_interrupts - Masks and clears all specified interrupts
458 * @pinstance: pointer to per adapter instance structure
459 * @intrs: interrupts to disable
461 * Return Value
462 * None
464 static void pmcraid_disable_interrupts(
465 struct pmcraid_instance *pinstance,
466 u32 intrs
469 u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
470 u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
472 iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
473 iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
474 iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_mask_reg);
475 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
479 * pmcraid_enable_interrupts - Enables specified interrupts
481 * @pinstance: pointer to per adapter instance structure
482 * @intr: interrupts to enable
484 * Return Value
485 * None
487 static void pmcraid_enable_interrupts(
488 struct pmcraid_instance *pinstance,
489 u32 intrs
492 u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
493 u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
495 iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
496 iowrite32(~intrs, pinstance->int_regs.ioa_host_interrupt_mask_reg);
497 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
499 pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
500 ioread32(pinstance->int_regs.global_interrupt_mask_reg),
501 ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
505 * pmcraid_reset_type - Determine the required reset type
506 * @pinstance: pointer to adapter instance structure
508 * IOA requires hard reset if any of the following conditions is true.
509 * 1. If HRRQ valid interrupt is not masked
510 * 2. IOA reset alert doorbell is set
511 * 3. If there are any error interrupts
513 static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
515 u32 mask;
516 u32 intrs;
517 u32 alerts;
519 mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
520 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
521 alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
523 if ((mask & INTRS_HRRQ_VALID) == 0 ||
524 (alerts & DOORBELL_IOA_RESET_ALERT) ||
525 (intrs & PMCRAID_ERROR_INTERRUPTS)) {
526 pmcraid_info("IOA requires hard reset\n");
527 pinstance->ioa_hard_reset = 1;
530 /* If unit check is active, trigger the dump */
531 if (intrs & INTRS_IOA_UNIT_CHECK)
532 pinstance->ioa_unit_check = 1;
536 * pmcraid_bist_done - completion function for PCI BIST
537 * @cmd: pointer to reset command
538 * Return Value
539 * none
542 static void pmcraid_ioa_reset(struct pmcraid_cmd *);
544 static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
546 struct pmcraid_instance *pinstance = cmd->drv_inst;
547 unsigned long lock_flags;
548 int rc;
549 u16 pci_reg;
551 rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
553 /* If PCI config space can't be accessed wait for another two secs */
554 if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
555 cmd->u.time_left > 0) {
556 pmcraid_info("BIST not complete, waiting another 2 secs\n");
557 cmd->timer.expires = jiffies + cmd->u.time_left;
558 cmd->u.time_left = 0;
559 cmd->timer.data = (unsigned long)cmd;
560 cmd->timer.function =
561 (void (*)(unsigned long))pmcraid_bist_done;
562 add_timer(&cmd->timer);
563 } else {
564 cmd->u.time_left = 0;
565 pmcraid_info("BIST is complete, proceeding with reset\n");
566 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
567 pmcraid_ioa_reset(cmd);
568 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
573 * pmcraid_start_bist - starts BIST
574 * @cmd: pointer to reset cmd
575 * Return Value
576 * none
578 static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
580 struct pmcraid_instance *pinstance = cmd->drv_inst;
581 u32 doorbells, intrs;
583 /* proceed with bist and wait for 2 seconds */
584 iowrite32(DOORBELL_IOA_START_BIST,
585 pinstance->int_regs.host_ioa_interrupt_reg);
586 doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
587 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
588 pmcraid_info("doorbells after start bist: %x intrs: %x \n",
589 doorbells, intrs);
591 cmd->u.time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
592 cmd->timer.data = (unsigned long)cmd;
593 cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
594 cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
595 add_timer(&cmd->timer);
599 * pmcraid_reset_alert_done - completion routine for reset_alert
600 * @cmd: pointer to command block used in reset sequence
601 * Return value
602 * None
604 static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
606 struct pmcraid_instance *pinstance = cmd->drv_inst;
607 u32 status = ioread32(pinstance->ioa_status);
608 unsigned long lock_flags;
610 /* if the critical operation in progress bit is set or the wait times
611 * out, invoke reset engine to proceed with hard reset. If there is
612 * some more time to wait, restart the timer
614 if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
615 cmd->u.time_left <= 0) {
616 pmcraid_info("critical op is reset proceeding with reset\n");
617 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
618 pmcraid_ioa_reset(cmd);
619 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
620 } else {
621 pmcraid_info("critical op is not yet reset waiting again\n");
622 /* restart timer if some more time is available to wait */
623 cmd->u.time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
624 cmd->timer.data = (unsigned long)cmd;
625 cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
626 cmd->timer.function =
627 (void (*)(unsigned long))pmcraid_reset_alert_done;
628 add_timer(&cmd->timer);
633 * pmcraid_reset_alert - alerts IOA for a possible reset
634 * @cmd : command block to be used for reset sequence.
636 * Return Value
637 * returns 0 if pci config-space is accessible and RESET_DOORBELL is
638 * successfully written to IOA. Returns non-zero in case pci_config_space
639 * is not accessible
641 static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
643 struct pmcraid_instance *pinstance = cmd->drv_inst;
644 u32 doorbells;
645 int rc;
646 u16 pci_reg;
648 /* If we are able to access IOA PCI config space, alert IOA that we are
649 * going to reset it soon. This enables IOA to preserv persistent error
650 * data if any. In case memory space is not accessible, proceed with
651 * BIST or slot_reset
653 rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
654 if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
656 /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
657 * reset IOA doesn't generate any interrupts when CRITICAL
658 * OPERATION bit is reset. A timer is started to wait for this
659 * bit to be reset.
661 cmd->u.time_left = PMCRAID_RESET_TIMEOUT;
662 cmd->timer.data = (unsigned long)cmd;
663 cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
664 cmd->timer.function =
665 (void (*)(unsigned long))pmcraid_reset_alert_done;
666 add_timer(&cmd->timer);
668 iowrite32(DOORBELL_IOA_RESET_ALERT,
669 pinstance->int_regs.host_ioa_interrupt_reg);
670 doorbells =
671 ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
672 pmcraid_info("doorbells after reset alert: %x\n", doorbells);
673 } else {
674 pmcraid_info("PCI config is not accessible starting BIST\n");
675 pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
676 pmcraid_start_bist(cmd);
681 * pmcraid_timeout_handler - Timeout handler for internally generated ops
683 * @cmd : pointer to command structure, that got timedout
685 * This function blocks host requests and initiates an adapter reset.
687 * Return value:
688 * None
690 static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
692 struct pmcraid_instance *pinstance = cmd->drv_inst;
693 unsigned long lock_flags;
695 dev_info(&pinstance->pdev->dev,
696 "Adapter being reset due to command timeout.\n");
698 /* Command timeouts result in hard reset sequence. The command that got
699 * timed out may be the one used as part of reset sequence. In this
700 * case restart reset sequence using the same command block even if
701 * reset is in progress. Otherwise fail this command and get a free
702 * command block to restart the reset sequence.
704 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
705 if (!pinstance->ioa_reset_in_progress) {
706 pinstance->ioa_reset_attempts = 0;
707 cmd = pmcraid_get_free_cmd(pinstance);
709 /* If we are out of command blocks, just return here itself.
710 * Some other command's timeout handler can do the reset job
712 if (cmd == NULL) {
713 spin_unlock_irqrestore(pinstance->host->host_lock,
714 lock_flags);
715 pmcraid_err("no free cmnd block for timeout handler\n");
716 return;
719 pinstance->reset_cmd = cmd;
720 pinstance->ioa_reset_in_progress = 1;
721 } else {
722 pmcraid_info("reset is already in progress\n");
724 if (pinstance->reset_cmd != cmd) {
725 /* This command should have been given to IOA, this
726 * command will be completed by fail_outstanding_cmds
727 * anyway
729 pmcraid_err("cmd is pending but reset in progress\n");
732 /* If this command was being used as part of the reset
733 * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
734 * causes fail_outstanding_commands not to return the command
735 * block back to free pool
737 if (cmd == pinstance->reset_cmd)
738 cmd->cmd_done = pmcraid_ioa_reset;
742 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
743 scsi_block_requests(pinstance->host);
744 pmcraid_reset_alert(cmd);
745 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
749 * pmcraid_internal_done - completion routine for internally generated cmds
751 * @cmd: command that got response from IOA
753 * Return Value:
754 * none
756 static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
758 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
759 cmd->ioa_cb->ioarcb.cdb[0],
760 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
762 /* Some of the internal commands are sent with callers blocking for the
763 * response. Same will be indicated as part of cmd->completion_req
764 * field. Response path needs to wake up any waiters waiting for cmd
765 * completion if this flag is set.
767 if (cmd->completion_req) {
768 cmd->completion_req = 0;
769 complete(&cmd->wait_for_completion);
772 /* most of the internal commands are completed by caller itself, so
773 * no need to return the command block back to free pool until we are
774 * required to do so (e.g once done with initialization).
776 if (cmd->release) {
777 cmd->release = 0;
778 pmcraid_return_cmd(cmd);
783 * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
785 * @cmd: command that got response from IOA
787 * This routine is called after driver re-reads configuration table due to a
788 * lost CCN. It returns the command block back to free pool and schedules
789 * worker thread to add/delete devices into the system.
791 * Return Value:
792 * none
794 static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
796 pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
797 cmd->ioa_cb->ioarcb.cdb[0],
798 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
800 if (cmd->release) {
801 cmd->release = 0;
802 pmcraid_return_cmd(cmd);
804 pmcraid_info("scheduling worker for config table reinitialization\n");
805 schedule_work(&cmd->drv_inst->worker_q);
809 * pmcraid_erp_done - Process completion of SCSI error response from device
810 * @cmd: pmcraid_command
812 * This function copies the sense buffer into the scsi_cmd struct and completes
813 * scsi_cmd by calling scsi_done function.
815 * Return value:
816 * none
818 static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
820 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
821 struct pmcraid_instance *pinstance = cmd->drv_inst;
822 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
824 if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
825 scsi_cmd->result |= (DID_ERROR << 16);
826 scmd_printk(KERN_INFO, scsi_cmd,
827 "command CDB[0] = %x failed with IOASC: 0x%08X\n",
828 cmd->ioa_cb->ioarcb.cdb[0], ioasc);
831 /* if we had allocated sense buffers for request sense, copy the sense
832 * release the buffers
834 if (cmd->sense_buffer != NULL) {
835 memcpy(scsi_cmd->sense_buffer,
836 cmd->sense_buffer,
837 SCSI_SENSE_BUFFERSIZE);
838 pci_free_consistent(pinstance->pdev,
839 SCSI_SENSE_BUFFERSIZE,
840 cmd->sense_buffer, cmd->sense_buffer_dma);
841 cmd->sense_buffer = NULL;
842 cmd->sense_buffer_dma = 0;
845 scsi_dma_unmap(scsi_cmd);
846 pmcraid_return_cmd(cmd);
847 scsi_cmd->scsi_done(scsi_cmd);
851 * pmcraid_fire_command - sends an IOA command to adapter
853 * This function adds the given block into pending command list
854 * and returns without waiting
856 * @cmd : command to be sent to the device
858 * Return Value
859 * None
861 static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
863 struct pmcraid_instance *pinstance = cmd->drv_inst;
864 unsigned long lock_flags;
866 /* Add this command block to pending cmd pool. We do this prior to
867 * writting IOARCB to ioarrin because IOA might complete the command
868 * by the time we are about to add it to the list. Response handler
869 * (isr/tasklet) looks for cmb block in the pending pending list.
871 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
872 list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
873 spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
874 atomic_inc(&pinstance->outstanding_cmds);
876 /* driver writes lower 32-bit value of IOARCB address only */
877 mb();
878 iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
879 pinstance->ioarrin);
883 * pmcraid_send_cmd - fires a command to IOA
885 * This function also sets up timeout function, and command completion
886 * function
888 * @cmd: pointer to the command block to be fired to IOA
889 * @cmd_done: command completion function, called once IOA responds
890 * @timeout: timeout to wait for this command completion
891 * @timeout_func: timeout handler
893 * Return value
894 * none
896 static void pmcraid_send_cmd(
897 struct pmcraid_cmd *cmd,
898 void (*cmd_done) (struct pmcraid_cmd *),
899 unsigned long timeout,
900 void (*timeout_func) (struct pmcraid_cmd *)
903 /* initialize done function */
904 cmd->cmd_done = cmd_done;
906 if (timeout_func) {
907 /* setup timeout handler */
908 cmd->timer.data = (unsigned long)cmd;
909 cmd->timer.expires = jiffies + timeout;
910 cmd->timer.function = (void (*)(unsigned long))timeout_func;
911 add_timer(&cmd->timer);
914 /* fire the command to IOA */
915 _pmcraid_fire_command(cmd);
919 * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
921 * @cmd: pointer to the command block used as part of reset sequence
923 * Return Value
924 * None
926 static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
928 pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
929 cmd->ioa_cb->ioarcb.cdb[0],
930 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
932 /* Note that commands sent during reset require next command to be sent
933 * to IOA. Hence reinit the done function as well as timeout function
935 pmcraid_reinit_cmdblk(cmd);
936 cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
937 cmd->ioa_cb->ioarcb.resource_handle =
938 cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
939 cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
940 cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
942 /* fire shutdown command to hardware. */
943 pmcraid_info("firing normal shutdown command (%d) to IOA\n",
944 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
946 pmcraid_send_cmd(cmd, pmcraid_ioa_reset,
947 PMCRAID_SHUTDOWN_TIMEOUT,
948 pmcraid_timeout_handler);
952 * pmcraid_identify_hrrq - registers host rrq buffers with IOA
953 * @cmd: pointer to command block to be used for identify hrrq
955 * Return Value
956 * 0 in case of success, otherwise non-zero failure code
959 static void pmcraid_querycfg(struct pmcraid_cmd *);
961 static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
963 struct pmcraid_instance *pinstance = cmd->drv_inst;
964 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
965 int index = 0;
966 __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
967 u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
969 pmcraid_reinit_cmdblk(cmd);
971 /* Initialize ioarcb */
972 ioarcb->request_type = REQ_TYPE_IOACMD;
973 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
975 /* initialize the hrrq number where IOA will respond to this command */
976 ioarcb->hrrq_id = index;
977 ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
978 ioarcb->cdb[1] = index;
980 /* IOA expects 64-bit pci address to be written in B.E format
981 * (i.e cdb[2]=MSByte..cdb[9]=LSB.
983 pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb => %llx:%llx\n",
984 hrrq_addr, ioarcb->ioarcb_bus_addr);
986 memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
987 memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
989 /* Subsequent commands require HRRQ identification to be successful.
990 * Note that this gets called even during reset from SCSI mid-layer
991 * or tasklet
993 pmcraid_send_cmd(cmd, pmcraid_querycfg,
994 PMCRAID_INTERNAL_TIMEOUT,
995 pmcraid_timeout_handler);
998 static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
999 static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
1002 * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
1004 * @cmd: initialized command block pointer
1006 * Return Value
1007 * none
1009 static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
1011 if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
1012 atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
1013 else
1014 atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
1016 pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
1020 * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
1022 * @pinstance: pointer to adapter instance structure
1023 * @type: HCAM type
1025 * Return Value
1026 * pointer to initialized pmcraid_cmd structure or NULL
1028 static struct pmcraid_cmd *pmcraid_init_hcam
1030 struct pmcraid_instance *pinstance,
1031 u8 type
1034 struct pmcraid_cmd *cmd;
1035 struct pmcraid_ioarcb *ioarcb;
1036 struct pmcraid_ioadl_desc *ioadl;
1037 struct pmcraid_hostrcb *hcam;
1038 void (*cmd_done) (struct pmcraid_cmd *);
1039 dma_addr_t dma;
1040 int rcb_size;
1042 cmd = pmcraid_get_free_cmd(pinstance);
1044 if (!cmd) {
1045 pmcraid_err("no free command blocks for hcam\n");
1046 return cmd;
1049 if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
1050 rcb_size = sizeof(struct pmcraid_hcam_ccn);
1051 cmd_done = pmcraid_process_ccn;
1052 dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
1053 hcam = &pinstance->ccn;
1054 } else {
1055 rcb_size = sizeof(struct pmcraid_hcam_ldn);
1056 cmd_done = pmcraid_process_ldn;
1057 dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
1058 hcam = &pinstance->ldn;
1061 /* initialize command pointer used for HCAM registration */
1062 hcam->cmd = cmd;
1064 ioarcb = &cmd->ioa_cb->ioarcb;
1065 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
1066 offsetof(struct pmcraid_ioarcb,
1067 add_data.u.ioadl[0]));
1068 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
1069 ioadl = ioarcb->add_data.u.ioadl;
1071 /* Initialize ioarcb */
1072 ioarcb->request_type = REQ_TYPE_HCAM;
1073 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
1074 ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
1075 ioarcb->cdb[1] = type;
1076 ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
1077 ioarcb->cdb[8] = (rcb_size) & 0xFF;
1079 ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
1081 ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
1082 ioadl[0].data_len = cpu_to_le32(rcb_size);
1083 ioadl[0].address = cpu_to_le32(dma);
1085 cmd->cmd_done = cmd_done;
1086 return cmd;
1090 * pmcraid_send_hcam - Send an HCAM to IOA
1091 * @pinstance: ioa config struct
1092 * @type: HCAM type
1094 * This function will send a Host Controlled Async command to IOA.
1096 * Return value:
1097 * none
1099 static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
1101 struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
1102 pmcraid_send_hcam_cmd(cmd);
1107 * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
1109 * @cmd: pointer to cmd that is used as cancelling command
1110 * @cmd_to_cancel: pointer to the command that needs to be cancelled
1112 static void pmcraid_prepare_cancel_cmd(
1113 struct pmcraid_cmd *cmd,
1114 struct pmcraid_cmd *cmd_to_cancel
1117 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
1118 __be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
1120 /* Get the resource handle to where the command to be aborted has been
1121 * sent.
1123 ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
1124 ioarcb->request_type = REQ_TYPE_IOACMD;
1125 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
1126 ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
1128 /* IOARCB address of the command to be cancelled is given in
1129 * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
1130 * IOARCB address are not masked.
1132 ioarcb_addr = cpu_to_be64(ioarcb_addr);
1133 memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
1137 * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
1139 * @cmd: command to be used as cancelling command
1140 * @type: HCAM type
1141 * @cmd_done: op done function for the cancelling command
1143 static void pmcraid_cancel_hcam(
1144 struct pmcraid_cmd *cmd,
1145 u8 type,
1146 void (*cmd_done) (struct pmcraid_cmd *)
1149 struct pmcraid_instance *pinstance;
1150 struct pmcraid_hostrcb *hcam;
1152 pinstance = cmd->drv_inst;
1153 hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
1154 &pinstance->ldn : &pinstance->ccn;
1156 /* prepare for cancelling previous hcam command. If the HCAM is
1157 * currently not pending with IOA, we would have hcam->cmd as non-null
1159 if (hcam->cmd == NULL)
1160 return;
1162 pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
1164 /* writing to IOARRIN must be protected by host_lock, as mid-layer
1165 * schedule queuecommand while we are doing this
1167 pmcraid_send_cmd(cmd, cmd_done,
1168 PMCRAID_INTERNAL_TIMEOUT,
1169 pmcraid_timeout_handler);
1173 * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
1175 * @cmd: command block to be used for cancelling the HCAM
1177 static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
1179 pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
1180 cmd->ioa_cb->ioarcb.cdb[0],
1181 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
1183 pmcraid_reinit_cmdblk(cmd);
1185 pmcraid_cancel_hcam(cmd,
1186 PMCRAID_HCAM_CODE_CONFIG_CHANGE,
1187 pmcraid_ioa_shutdown);
1191 * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
1193 * @cmd: command block to be used for cancelling the HCAM
1195 static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
1197 pmcraid_cancel_hcam(cmd,
1198 PMCRAID_HCAM_CODE_LOG_DATA,
1199 pmcraid_cancel_ccn);
1203 * pmcraid_expose_resource - check if the resource can be exposed to OS
1205 * @cfgte: pointer to configuration table entry of the resource
1207 * Return value:
1208 * true if resource can be added to midlayer, false(0) otherwise
1210 static int pmcraid_expose_resource(struct pmcraid_config_table_entry *cfgte)
1212 int retval = 0;
1214 if (cfgte->resource_type == RES_TYPE_VSET)
1215 retval = ((cfgte->unique_flags1 & 0x80) == 0);
1216 else if (cfgte->resource_type == RES_TYPE_GSCSI)
1217 retval = (RES_BUS(cfgte->resource_address) !=
1218 PMCRAID_VIRTUAL_ENCL_BUS_ID);
1219 return retval;
1222 /* attributes supported by pmcraid_event_family */
1223 enum {
1224 PMCRAID_AEN_ATTR_UNSPEC,
1225 PMCRAID_AEN_ATTR_EVENT,
1226 __PMCRAID_AEN_ATTR_MAX,
1228 #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
1230 /* commands supported by pmcraid_event_family */
1231 enum {
1232 PMCRAID_AEN_CMD_UNSPEC,
1233 PMCRAID_AEN_CMD_EVENT,
1234 __PMCRAID_AEN_CMD_MAX,
1236 #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
1238 static struct genl_family pmcraid_event_family = {
1239 .id = GENL_ID_GENERATE,
1240 .name = "pmcraid",
1241 .version = 1,
1242 .maxattr = PMCRAID_AEN_ATTR_MAX
1246 * pmcraid_netlink_init - registers pmcraid_event_family
1248 * Return value:
1249 * 0 if the pmcraid_event_family is successfully registered
1250 * with netlink generic, non-zero otherwise
1252 static int pmcraid_netlink_init(void)
1254 int result;
1256 result = genl_register_family(&pmcraid_event_family);
1258 if (result)
1259 return result;
1261 pmcraid_info("registered NETLINK GENERIC group: %d\n",
1262 pmcraid_event_family.id);
1264 return result;
1268 * pmcraid_netlink_release - unregisters pmcraid_event_family
1270 * Return value:
1271 * none
1273 static void pmcraid_netlink_release(void)
1275 genl_unregister_family(&pmcraid_event_family);
1279 * pmcraid_notify_aen - sends event msg to user space application
1280 * @pinstance: pointer to adapter instance structure
1281 * @type: HCAM type
1283 * Return value:
1284 * 0 if success, error value in case of any failure.
1286 static int pmcraid_notify_aen(struct pmcraid_instance *pinstance, u8 type)
1288 struct sk_buff *skb;
1289 struct pmcraid_aen_msg *aen_msg;
1290 void *msg_header;
1291 int data_size, total_size;
1292 int result;
1295 if (type == PMCRAID_HCAM_CODE_LOG_DATA) {
1296 aen_msg = pinstance->ldn.msg;
1297 data_size = pinstance->ldn.hcam->data_len;
1298 } else {
1299 aen_msg = pinstance->ccn.msg;
1300 data_size = pinstance->ccn.hcam->data_len;
1303 data_size += sizeof(struct pmcraid_hcam_hdr);
1304 aen_msg->hostno = (pinstance->host->unique_id << 16 |
1305 MINOR(pinstance->cdev.dev));
1306 aen_msg->length = data_size;
1307 data_size += sizeof(*aen_msg);
1309 total_size = nla_total_size(data_size);
1310 skb = genlmsg_new(total_size, GFP_ATOMIC);
1313 if (!skb) {
1314 pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
1315 total_size);
1316 return -ENOMEM;
1319 /* add the genetlink message header */
1320 msg_header = genlmsg_put(skb, 0, 0,
1321 &pmcraid_event_family, 0,
1322 PMCRAID_AEN_CMD_EVENT);
1323 if (!msg_header) {
1324 pmcraid_err("failed to copy command details\n");
1325 nlmsg_free(skb);
1326 return -ENOMEM;
1329 result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
1331 if (result) {
1332 pmcraid_err("failed to copy AEN attribute data \n");
1333 nlmsg_free(skb);
1334 return -EINVAL;
1337 /* send genetlink multicast message to notify appplications */
1338 result = genlmsg_end(skb, msg_header);
1340 if (result < 0) {
1341 pmcraid_err("genlmsg_end failed\n");
1342 nlmsg_free(skb);
1343 return result;
1346 result =
1347 genlmsg_multicast(skb, 0, pmcraid_event_family.id, GFP_ATOMIC);
1349 /* If there are no listeners, genlmsg_multicast may return non-zero
1350 * value.
1352 if (result)
1353 pmcraid_info("failed to send %s event message %x!\n",
1354 type == PMCRAID_HCAM_CODE_LOG_DATA ? "LDN" : "CCN",
1355 result);
1356 return result;
1360 * pmcraid_handle_config_change - Handle a config change from the adapter
1361 * @pinstance: pointer to per adapter instance structure
1363 * Return value:
1364 * none
1367 static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
1369 struct pmcraid_config_table_entry *cfg_entry;
1370 struct pmcraid_hcam_ccn *ccn_hcam;
1371 struct pmcraid_cmd *cmd;
1372 struct pmcraid_cmd *cfgcmd;
1373 struct pmcraid_resource_entry *res = NULL;
1374 unsigned long lock_flags;
1375 unsigned long host_lock_flags;
1376 u32 new_entry = 1;
1377 u32 hidden_entry = 0;
1378 int rc;
1380 ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
1381 cfg_entry = &ccn_hcam->cfg_entry;
1383 pmcraid_info
1384 ("CCN(%x): %x type: %x lost: %x flags: %x res: %x:%x:%x:%x\n",
1385 pinstance->ccn.hcam->ilid,
1386 pinstance->ccn.hcam->op_code,
1387 pinstance->ccn.hcam->notification_type,
1388 pinstance->ccn.hcam->notification_lost,
1389 pinstance->ccn.hcam->flags,
1390 pinstance->host->unique_id,
1391 RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
1392 (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
1393 RES_BUS(cfg_entry->resource_address)),
1394 RES_IS_VSET(*cfg_entry) ? cfg_entry->unique_flags1 :
1395 RES_TARGET(cfg_entry->resource_address),
1396 RES_LUN(cfg_entry->resource_address));
1399 /* If this HCAM indicates a lost notification, read the config table */
1400 if (pinstance->ccn.hcam->notification_lost) {
1401 cfgcmd = pmcraid_get_free_cmd(pinstance);
1402 if (cfgcmd) {
1403 pmcraid_info("lost CCN, reading config table\b");
1404 pinstance->reinit_cfg_table = 1;
1405 pmcraid_querycfg(cfgcmd);
1406 } else {
1407 pmcraid_err("lost CCN, no free cmd for querycfg\n");
1409 goto out_notify_apps;
1412 /* If this resource is not going to be added to mid-layer, just notify
1413 * applications and return. If this notification is about hiding a VSET
1414 * resource, check if it was exposed already.
1416 if (pinstance->ccn.hcam->notification_type ==
1417 NOTIFICATION_TYPE_ENTRY_CHANGED &&
1418 cfg_entry->resource_type == RES_TYPE_VSET &&
1419 cfg_entry->unique_flags1 & 0x80) {
1420 hidden_entry = 1;
1421 } else if (!pmcraid_expose_resource(cfg_entry))
1422 goto out_notify_apps;
1424 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
1425 list_for_each_entry(res, &pinstance->used_res_q, queue) {
1426 rc = memcmp(&res->cfg_entry.resource_address,
1427 &cfg_entry->resource_address,
1428 sizeof(cfg_entry->resource_address));
1429 if (!rc) {
1430 new_entry = 0;
1431 break;
1435 if (new_entry) {
1437 if (hidden_entry) {
1438 spin_unlock_irqrestore(&pinstance->resource_lock,
1439 lock_flags);
1440 goto out_notify_apps;
1443 /* If there are more number of resources than what driver can
1444 * manage, do not notify the applications about the CCN. Just
1445 * ignore this notifications and re-register the same HCAM
1447 if (list_empty(&pinstance->free_res_q)) {
1448 spin_unlock_irqrestore(&pinstance->resource_lock,
1449 lock_flags);
1450 pmcraid_err("too many resources attached\n");
1451 spin_lock_irqsave(pinstance->host->host_lock,
1452 host_lock_flags);
1453 pmcraid_send_hcam(pinstance,
1454 PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1455 spin_unlock_irqrestore(pinstance->host->host_lock,
1456 host_lock_flags);
1457 return;
1460 res = list_entry(pinstance->free_res_q.next,
1461 struct pmcraid_resource_entry, queue);
1463 list_del(&res->queue);
1464 res->scsi_dev = NULL;
1465 res->reset_progress = 0;
1466 list_add_tail(&res->queue, &pinstance->used_res_q);
1469 memcpy(&res->cfg_entry, cfg_entry,
1470 sizeof(struct pmcraid_config_table_entry));
1472 if (pinstance->ccn.hcam->notification_type ==
1473 NOTIFICATION_TYPE_ENTRY_DELETED || hidden_entry) {
1474 if (res->scsi_dev) {
1475 res->cfg_entry.unique_flags1 &= 0x7F;
1476 res->change_detected = RES_CHANGE_DEL;
1477 res->cfg_entry.resource_handle =
1478 PMCRAID_INVALID_RES_HANDLE;
1479 schedule_work(&pinstance->worker_q);
1480 } else {
1481 /* This may be one of the non-exposed resources */
1482 list_move_tail(&res->queue, &pinstance->free_res_q);
1484 } else if (!res->scsi_dev) {
1485 res->change_detected = RES_CHANGE_ADD;
1486 schedule_work(&pinstance->worker_q);
1488 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
1490 out_notify_apps:
1492 /* Notify configuration changes to registered applications.*/
1493 if (!pmcraid_disable_aen)
1494 pmcraid_notify_aen(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1496 cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1497 if (cmd)
1498 pmcraid_send_hcam_cmd(cmd);
1502 * pmcraid_get_error_info - return error string for an ioasc
1503 * @ioasc: ioasc code
1504 * Return Value
1505 * none
1507 static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
1509 int i;
1510 for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
1511 if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
1512 return &pmcraid_ioasc_error_table[i];
1514 return NULL;
1518 * pmcraid_ioasc_logger - log IOASC information based user-settings
1519 * @ioasc: ioasc code
1520 * @cmd: pointer to command that resulted in 'ioasc'
1522 void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
1524 struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
1526 if (error_info == NULL ||
1527 cmd->drv_inst->current_log_level < error_info->log_level)
1528 return;
1530 /* log the error string */
1531 pmcraid_err("cmd [%d] for resource %x failed with %x(%s)\n",
1532 cmd->ioa_cb->ioarcb.cdb[0],
1533 cmd->ioa_cb->ioarcb.resource_handle,
1534 le32_to_cpu(ioasc), error_info->error_string);
1538 * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
1540 * @pinstance: pointer to per adapter instance structure
1542 * Return value:
1543 * none
1545 static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
1547 struct pmcraid_hcam_ldn *hcam_ldn;
1548 u32 ioasc;
1550 hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
1552 pmcraid_info
1553 ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
1554 pinstance->ldn.hcam->ilid,
1555 pinstance->ldn.hcam->op_code,
1556 pinstance->ldn.hcam->notification_type,
1557 pinstance->ldn.hcam->notification_lost,
1558 pinstance->ldn.hcam->flags,
1559 pinstance->ldn.hcam->overlay_id);
1561 /* log only the errors, no need to log informational log entries */
1562 if (pinstance->ldn.hcam->notification_type !=
1563 NOTIFICATION_TYPE_ERROR_LOG)
1564 return;
1566 if (pinstance->ldn.hcam->notification_lost ==
1567 HOSTRCB_NOTIFICATIONS_LOST)
1568 dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
1570 ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
1572 if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
1573 ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
1574 dev_info(&pinstance->pdev->dev,
1575 "UnitAttention due to IOA Bus Reset\n");
1576 scsi_report_bus_reset(
1577 pinstance->host,
1578 RES_BUS(hcam_ldn->error_log.fd_ra));
1581 return;
1585 * pmcraid_process_ccn - Op done function for a CCN.
1586 * @cmd: pointer to command struct
1588 * This function is the op done function for a configuration
1589 * change notification
1591 * Return value:
1592 * none
1594 static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
1596 struct pmcraid_instance *pinstance = cmd->drv_inst;
1597 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1598 unsigned long lock_flags;
1600 pinstance->ccn.cmd = NULL;
1601 pmcraid_return_cmd(cmd);
1603 /* If driver initiated IOA reset happened while this hcam was pending
1604 * with IOA, or IOA bringdown sequence is in progress, no need to
1605 * re-register the hcam
1607 if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
1608 atomic_read(&pinstance->ccn.ignore) == 1) {
1609 return;
1610 } else if (ioasc) {
1611 dev_info(&pinstance->pdev->dev,
1612 "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
1613 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
1614 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1615 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
1616 } else {
1617 pmcraid_handle_config_change(pinstance);
1622 * pmcraid_process_ldn - op done function for an LDN
1623 * @cmd: pointer to command block
1625 * Return value
1626 * none
1628 static void pmcraid_initiate_reset(struct pmcraid_instance *);
1630 static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
1632 struct pmcraid_instance *pinstance = cmd->drv_inst;
1633 struct pmcraid_hcam_ldn *ldn_hcam =
1634 (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
1635 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1636 u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
1637 unsigned long lock_flags;
1639 /* return the command block back to freepool */
1640 pinstance->ldn.cmd = NULL;
1641 pmcraid_return_cmd(cmd);
1643 /* If driver initiated IOA reset happened while this hcam was pending
1644 * with IOA, no need to re-register the hcam as reset engine will do it
1645 * once reset sequence is complete
1647 if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
1648 atomic_read(&pinstance->ccn.ignore) == 1) {
1649 return;
1650 } else if (!ioasc) {
1651 pmcraid_handle_error_log(pinstance);
1652 if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
1653 spin_lock_irqsave(pinstance->host->host_lock,
1654 lock_flags);
1655 pmcraid_initiate_reset(pinstance);
1656 spin_unlock_irqrestore(pinstance->host->host_lock,
1657 lock_flags);
1658 return;
1660 } else {
1661 dev_info(&pinstance->pdev->dev,
1662 "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
1664 /* send netlink message for HCAM notification if enabled */
1665 if (!pmcraid_disable_aen)
1666 pmcraid_notify_aen(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1668 cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1669 if (cmd)
1670 pmcraid_send_hcam_cmd(cmd);
1674 * pmcraid_register_hcams - register HCAMs for CCN and LDN
1676 * @pinstance: pointer per adapter instance structure
1678 * Return Value
1679 * none
1681 static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
1683 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1684 pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1688 * pmcraid_unregister_hcams - cancel HCAMs registered already
1689 * @cmd: pointer to command used as part of reset sequence
1691 static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
1693 struct pmcraid_instance *pinstance = cmd->drv_inst;
1695 /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
1696 * handling hcam response though it is not necessary. In order to
1697 * prevent this, set 'ignore', so that bring-down sequence doesn't
1698 * re-send any more hcams
1700 atomic_set(&pinstance->ccn.ignore, 1);
1701 atomic_set(&pinstance->ldn.ignore, 1);
1703 /* If adapter reset was forced as part of runtime reset sequence,
1704 * start the reset sequence.
1706 if (pinstance->force_ioa_reset && !pinstance->ioa_bringdown) {
1707 pinstance->force_ioa_reset = 0;
1708 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1709 pmcraid_reset_alert(cmd);
1710 return;
1713 /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
1714 * one after the other. So CCN cancellation will be triggered by
1715 * pmcraid_cancel_ldn itself.
1717 pmcraid_cancel_ldn(cmd);
1721 * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
1722 * @pinstance: pointer to adapter instance structure
1723 * Return Value
1724 * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
1726 static void pmcraid_reinit_buffers(struct pmcraid_instance *);
1728 static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
1730 u32 intrs;
1732 pmcraid_reinit_buffers(pinstance);
1733 intrs = pmcraid_read_interrupts(pinstance);
1735 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
1737 if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
1738 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
1739 pinstance->int_regs.ioa_host_interrupt_mask_reg);
1740 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
1741 pinstance->int_regs.ioa_host_interrupt_clr_reg);
1742 return 1;
1743 } else {
1744 return 0;
1749 * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
1750 * @cmd : pointer to reset command block
1752 * Return Value
1753 * none
1755 static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
1757 struct pmcraid_instance *pinstance = cmd->drv_inst;
1758 u32 int_reg;
1759 u32 doorbell;
1761 /* There will be an interrupt when Transition to Operational bit is
1762 * set so tasklet would execute next reset task. The timeout handler
1763 * would re-initiate a reset
1765 cmd->cmd_done = pmcraid_ioa_reset;
1766 cmd->timer.data = (unsigned long)cmd;
1767 cmd->timer.expires = jiffies +
1768 msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
1769 cmd->timer.function = (void (*)(unsigned long))pmcraid_timeout_handler;
1771 if (!timer_pending(&cmd->timer))
1772 add_timer(&cmd->timer);
1774 /* Enable destructive diagnostics on IOA if it is not yet in
1775 * operational state
1777 doorbell = DOORBELL_RUNTIME_RESET |
1778 DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
1780 iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
1781 int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
1782 pmcraid_info("Waiting for IOA to become operational %x:%x\n",
1783 ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
1784 int_reg);
1788 * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
1790 * @pinstance: pointer to adapter instance structure
1792 * Return Value
1793 * none
1795 static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
1797 pmcraid_info("%s is not yet implemented\n", __func__);
1801 * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
1802 * @pinstance: pointer to adapter instance structure
1804 * This function fails all outstanding ops. If they are submitted to IOA
1805 * already, it sends cancel all messages if IOA is still accepting IOARCBs,
1806 * otherwise just completes the commands and returns the cmd blocks to free
1807 * pool.
1809 * Return value:
1810 * none
1812 static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
1814 struct pmcraid_cmd *cmd, *temp;
1815 unsigned long lock_flags;
1817 /* pending command list is protected by pending_pool_lock. Its
1818 * traversal must be done as within this lock
1820 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
1821 list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
1822 free_list) {
1823 list_del(&cmd->free_list);
1824 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
1825 lock_flags);
1826 cmd->ioa_cb->ioasa.ioasc =
1827 cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
1828 cmd->ioa_cb->ioasa.ilid =
1829 cpu_to_be32(PMCRAID_DRIVER_ILID);
1831 /* In case the command timer is still running */
1832 del_timer(&cmd->timer);
1834 /* If this is an IO command, complete it by invoking scsi_done
1835 * function. If this is one of the internal commands other
1836 * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
1837 * complete it
1839 if (cmd->scsi_cmd) {
1841 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
1842 __le32 resp = cmd->ioa_cb->ioarcb.response_handle;
1844 scsi_cmd->result |= DID_ERROR << 16;
1846 scsi_dma_unmap(scsi_cmd);
1847 pmcraid_return_cmd(cmd);
1849 pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
1850 le32_to_cpu(resp) >> 2,
1851 cmd->ioa_cb->ioarcb.cdb[0],
1852 scsi_cmd->result);
1853 scsi_cmd->scsi_done(scsi_cmd);
1854 } else if (cmd->cmd_done == pmcraid_internal_done ||
1855 cmd->cmd_done == pmcraid_erp_done) {
1856 cmd->cmd_done(cmd);
1857 } else if (cmd->cmd_done != pmcraid_ioa_reset) {
1858 pmcraid_return_cmd(cmd);
1861 atomic_dec(&pinstance->outstanding_cmds);
1862 spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
1865 spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
1869 * pmcraid_ioa_reset - Implementation of IOA reset logic
1871 * @cmd: pointer to the cmd block to be used for entire reset process
1873 * This function executes most of the steps required for IOA reset. This gets
1874 * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
1875 * 'eh_' thread. Access to variables used for controling the reset sequence is
1876 * synchronized using host lock. Various functions called during reset process
1877 * would make use of a single command block, pointer to which is also stored in
1878 * adapter instance structure.
1880 * Return Value
1881 * None
1883 static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
1885 struct pmcraid_instance *pinstance = cmd->drv_inst;
1886 u8 reset_complete = 0;
1888 pinstance->ioa_reset_in_progress = 1;
1890 if (pinstance->reset_cmd != cmd) {
1891 pmcraid_err("reset is called with different command block\n");
1892 pinstance->reset_cmd = cmd;
1895 pmcraid_info("reset_engine: state = %d, command = %p\n",
1896 pinstance->ioa_state, cmd);
1898 switch (pinstance->ioa_state) {
1900 case IOA_STATE_DEAD:
1901 /* If IOA is offline, whatever may be the reset reason, just
1902 * return. callers might be waiting on the reset wait_q, wake
1903 * up them
1905 pmcraid_err("IOA is offline no reset is possible\n");
1906 reset_complete = 1;
1907 break;
1909 case IOA_STATE_IN_BRINGDOWN:
1910 /* we enter here, once ioa shutdown command is processed by IOA
1911 * Alert IOA for a possible reset. If reset alert fails, IOA
1912 * goes through hard-reset
1914 pmcraid_disable_interrupts(pinstance, ~0);
1915 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1916 pmcraid_reset_alert(cmd);
1917 break;
1919 case IOA_STATE_UNKNOWN:
1920 /* We may be called during probe or resume. Some pre-processing
1921 * is required for prior to reset
1923 scsi_block_requests(pinstance->host);
1925 /* If asked to reset while IOA was processing responses or
1926 * there are any error responses then IOA may require
1927 * hard-reset.
1929 if (pinstance->ioa_hard_reset == 0) {
1930 if (ioread32(pinstance->ioa_status) &
1931 INTRS_TRANSITION_TO_OPERATIONAL) {
1932 pmcraid_info("sticky bit set, bring-up\n");
1933 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
1934 pmcraid_reinit_cmdblk(cmd);
1935 pmcraid_identify_hrrq(cmd);
1936 } else {
1937 pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
1938 pmcraid_soft_reset(cmd);
1940 } else {
1941 /* Alert IOA of a possible reset and wait for critical
1942 * operation in progress bit to reset
1944 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1945 pmcraid_reset_alert(cmd);
1947 break;
1949 case IOA_STATE_IN_RESET_ALERT:
1950 /* If critical operation in progress bit is reset or wait gets
1951 * timed out, reset proceeds with starting BIST on the IOA.
1952 * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
1953 * they are 3 or more, reset engine marks IOA dead and returns
1955 pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
1956 pmcraid_start_bist(cmd);
1957 break;
1959 case IOA_STATE_IN_HARD_RESET:
1960 pinstance->ioa_reset_attempts++;
1962 /* retry reset if we haven't reached maximum allowed limit */
1963 if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
1964 pinstance->ioa_reset_attempts = 0;
1965 pmcraid_err("IOA didn't respond marking it as dead\n");
1966 pinstance->ioa_state = IOA_STATE_DEAD;
1967 reset_complete = 1;
1968 break;
1971 /* Once either bist or pci reset is done, restore PCI config
1972 * space. If this fails, proceed with hard reset again
1975 if (pci_restore_state(pinstance->pdev)) {
1976 pmcraid_info("config-space error resetting again\n");
1977 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1978 pmcraid_reset_alert(cmd);
1979 break;
1982 /* fail all pending commands */
1983 pmcraid_fail_outstanding_cmds(pinstance);
1985 /* check if unit check is active, if so extract dump */
1986 if (pinstance->ioa_unit_check) {
1987 pmcraid_info("unit check is active\n");
1988 pinstance->ioa_unit_check = 0;
1989 pmcraid_get_dump(pinstance);
1990 pinstance->ioa_reset_attempts--;
1991 pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1992 pmcraid_reset_alert(cmd);
1993 break;
1996 /* if the reset reason is to bring-down the ioa, we might be
1997 * done with the reset restore pci_config_space and complete
1998 * the reset
2000 if (pinstance->ioa_bringdown) {
2001 pmcraid_info("bringing down the adapter\n");
2002 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2003 pinstance->ioa_bringdown = 0;
2004 pinstance->ioa_state = IOA_STATE_UNKNOWN;
2005 reset_complete = 1;
2006 } else {
2007 /* bring-up IOA, so proceed with soft reset
2008 * Reinitialize hrrq_buffers and their indices also
2009 * enable interrupts after a pci_restore_state
2011 if (pmcraid_reset_enable_ioa(pinstance)) {
2012 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
2013 pmcraid_info("bringing up the adapter\n");
2014 pmcraid_reinit_cmdblk(cmd);
2015 pmcraid_identify_hrrq(cmd);
2016 } else {
2017 pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
2018 pmcraid_soft_reset(cmd);
2021 break;
2023 case IOA_STATE_IN_SOFT_RESET:
2024 /* TRANSITION TO OPERATIONAL is on so start initialization
2025 * sequence
2027 pmcraid_info("In softreset proceeding with bring-up\n");
2028 pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
2030 /* Initialization commands start with HRRQ identification. From
2031 * now on tasklet completes most of the commands as IOA is up
2032 * and intrs are enabled
2034 pmcraid_identify_hrrq(cmd);
2035 break;
2037 case IOA_STATE_IN_BRINGUP:
2038 /* we are done with bringing up of IOA, change the ioa_state to
2039 * operational and wake up any waiters
2041 pinstance->ioa_state = IOA_STATE_OPERATIONAL;
2042 reset_complete = 1;
2043 break;
2045 case IOA_STATE_OPERATIONAL:
2046 default:
2047 /* When IOA is operational and a reset is requested, check for
2048 * the reset reason. If reset is to bring down IOA, unregister
2049 * HCAMs and initiate shutdown; if adapter reset is forced then
2050 * restart reset sequence again
2052 if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
2053 pinstance->force_ioa_reset == 0) {
2054 reset_complete = 1;
2055 } else {
2056 if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
2057 pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
2058 pmcraid_reinit_cmdblk(cmd);
2059 pmcraid_unregister_hcams(cmd);
2061 break;
2064 /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
2065 * OPERATIONAL. Reset all control variables used during reset, wake up
2066 * any waiting threads and let the SCSI mid-layer send commands. Note
2067 * that host_lock must be held before invoking scsi_report_bus_reset.
2069 if (reset_complete) {
2070 pinstance->ioa_reset_in_progress = 0;
2071 pinstance->ioa_reset_attempts = 0;
2072 pinstance->reset_cmd = NULL;
2073 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2074 pinstance->ioa_bringdown = 0;
2075 pmcraid_return_cmd(cmd);
2077 /* If target state is to bring up the adapter, proceed with
2078 * hcam registration and resource exposure to mid-layer.
2080 if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
2081 pmcraid_register_hcams(pinstance);
2083 wake_up_all(&pinstance->reset_wait_q);
2086 return;
2090 * pmcraid_initiate_reset - initiates reset sequence. This is called from
2091 * ISR/tasklet during error interrupts including IOA unit check. If reset
2092 * is already in progress, it just returns, otherwise initiates IOA reset
2093 * to bring IOA up to operational state.
2095 * @pinstance: pointer to adapter instance structure
2097 * Return value
2098 * none
2100 static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
2102 struct pmcraid_cmd *cmd;
2104 /* If the reset is already in progress, just return, otherwise start
2105 * reset sequence and return
2107 if (!pinstance->ioa_reset_in_progress) {
2108 scsi_block_requests(pinstance->host);
2109 cmd = pmcraid_get_free_cmd(pinstance);
2111 if (cmd == NULL) {
2112 pmcraid_err("no cmnd blocks for initiate_reset\n");
2113 return;
2116 pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2117 pinstance->reset_cmd = cmd;
2118 pinstance->force_ioa_reset = 1;
2119 pmcraid_ioa_reset(cmd);
2124 * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
2125 * or bringdown IOA
2126 * @pinstance: pointer adapter instance structure
2127 * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
2128 * @target_state: expected target state after reset
2130 * Note: This command initiates reset and waits for its completion. Hence this
2131 * should not be called from isr/timer/tasklet functions (timeout handlers,
2132 * error response handlers and interrupt handlers).
2134 * Return Value
2135 * 1 in case ioa_state is not target_state, 0 otherwise.
2137 static int pmcraid_reset_reload(
2138 struct pmcraid_instance *pinstance,
2139 u8 shutdown_type,
2140 u8 target_state
2143 struct pmcraid_cmd *reset_cmd = NULL;
2144 unsigned long lock_flags;
2145 int reset = 1;
2147 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2149 if (pinstance->ioa_reset_in_progress) {
2150 pmcraid_info("reset_reload: reset is already in progress\n");
2152 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2154 wait_event(pinstance->reset_wait_q,
2155 !pinstance->ioa_reset_in_progress);
2157 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2159 if (pinstance->ioa_state == IOA_STATE_DEAD) {
2160 spin_unlock_irqrestore(pinstance->host->host_lock,
2161 lock_flags);
2162 pmcraid_info("reset_reload: IOA is dead\n");
2163 return reset;
2164 } else if (pinstance->ioa_state == target_state) {
2165 reset = 0;
2169 if (reset) {
2170 pmcraid_info("reset_reload: proceeding with reset\n");
2171 scsi_block_requests(pinstance->host);
2172 reset_cmd = pmcraid_get_free_cmd(pinstance);
2174 if (reset_cmd == NULL) {
2175 pmcraid_err("no free cmnd for reset_reload\n");
2176 spin_unlock_irqrestore(pinstance->host->host_lock,
2177 lock_flags);
2178 return reset;
2181 if (shutdown_type == SHUTDOWN_NORMAL)
2182 pinstance->ioa_bringdown = 1;
2184 pinstance->ioa_shutdown_type = shutdown_type;
2185 pinstance->reset_cmd = reset_cmd;
2186 pinstance->force_ioa_reset = reset;
2187 pmcraid_info("reset_reload: initiating reset\n");
2188 pmcraid_ioa_reset(reset_cmd);
2189 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2190 pmcraid_info("reset_reload: waiting for reset to complete\n");
2191 wait_event(pinstance->reset_wait_q,
2192 !pinstance->ioa_reset_in_progress);
2194 pmcraid_info("reset_reload: reset is complete !! \n");
2195 scsi_unblock_requests(pinstance->host);
2196 if (pinstance->ioa_state == target_state)
2197 reset = 0;
2200 return reset;
2204 * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
2206 * @pinstance: pointer to adapter instance structure
2208 * Return Value
2209 * whatever is returned from pmcraid_reset_reload
2211 static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
2213 return pmcraid_reset_reload(pinstance,
2214 SHUTDOWN_NORMAL,
2215 IOA_STATE_UNKNOWN);
2219 * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
2221 * @pinstance: pointer to adapter instance structure
2223 * Return Value
2224 * whatever is returned from pmcraid_reset_reload
2226 static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
2228 return pmcraid_reset_reload(pinstance,
2229 SHUTDOWN_NONE,
2230 IOA_STATE_OPERATIONAL);
2234 * pmcraid_request_sense - Send request sense to a device
2235 * @cmd: pmcraid command struct
2237 * This function sends a request sense to a device as a result of a check
2238 * condition. This method re-uses the same command block that failed earlier.
2240 static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
2242 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2243 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
2245 /* allocate DMAable memory for sense buffers */
2246 cmd->sense_buffer = pci_alloc_consistent(cmd->drv_inst->pdev,
2247 SCSI_SENSE_BUFFERSIZE,
2248 &cmd->sense_buffer_dma);
2250 if (cmd->sense_buffer == NULL) {
2251 pmcraid_err
2252 ("couldn't allocate sense buffer for request sense\n");
2253 pmcraid_erp_done(cmd);
2254 return;
2257 /* re-use the command block */
2258 memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
2259 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
2260 ioarcb->request_flags0 = (SYNC_COMPLETE |
2261 NO_LINK_DESCS |
2262 INHIBIT_UL_CHECK);
2263 ioarcb->request_type = REQ_TYPE_SCSI;
2264 ioarcb->cdb[0] = REQUEST_SENSE;
2265 ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2267 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
2268 offsetof(struct pmcraid_ioarcb,
2269 add_data.u.ioadl[0]));
2270 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
2272 ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
2274 ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
2275 ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
2276 ioadl->flags = IOADL_FLAGS_LAST_DESC;
2278 /* request sense might be called as part of error response processing
2279 * which runs in tasklets context. It is possible that mid-layer might
2280 * schedule queuecommand during this time, hence, writting to IOARRIN
2281 * must be protect by host_lock
2283 pmcraid_send_cmd(cmd, pmcraid_erp_done,
2284 PMCRAID_REQUEST_SENSE_TIMEOUT,
2285 pmcraid_timeout_handler);
2289 * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
2290 * @cmd: command that failed
2291 * @sense: true if request_sense is required after cancel all
2293 * This function sends a cancel all to a device to clear the queue.
2295 static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, u32 sense)
2297 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2298 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2299 struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
2300 void (*cmd_done) (struct pmcraid_cmd *) = sense ? pmcraid_erp_done
2301 : pmcraid_request_sense;
2303 memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
2304 ioarcb->request_flags0 = SYNC_OVERRIDE;
2305 ioarcb->request_type = REQ_TYPE_IOACMD;
2306 ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
2308 if (RES_IS_GSCSI(res->cfg_entry))
2309 ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
2311 ioarcb->ioadl_bus_addr = 0;
2312 ioarcb->ioadl_length = 0;
2313 ioarcb->data_transfer_length = 0;
2314 ioarcb->ioarcb_bus_addr &= (~0x1FULL);
2316 /* writing to IOARRIN must be protected by host_lock, as mid-layer
2317 * schedule queuecommand while we are doing this
2319 pmcraid_send_cmd(cmd, cmd_done,
2320 PMCRAID_REQUEST_SENSE_TIMEOUT,
2321 pmcraid_timeout_handler);
2325 * pmcraid_frame_auto_sense: frame fixed format sense information
2327 * @cmd: pointer to failing command block
2329 * Return value
2330 * none
2332 static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
2334 u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
2335 struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
2336 struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
2337 u32 ioasc = le32_to_cpu(ioasa->ioasc);
2338 u32 failing_lba = 0;
2340 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
2341 cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
2343 if (RES_IS_VSET(res->cfg_entry) &&
2344 ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
2345 ioasa->u.vset.failing_lba_hi != 0) {
2347 sense_buf[0] = 0x72;
2348 sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
2349 sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
2350 sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
2352 sense_buf[7] = 12;
2353 sense_buf[8] = 0;
2354 sense_buf[9] = 0x0A;
2355 sense_buf[10] = 0x80;
2357 failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
2359 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
2360 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
2361 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
2362 sense_buf[15] = failing_lba & 0x000000ff;
2364 failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
2366 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
2367 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
2368 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
2369 sense_buf[19] = failing_lba & 0x000000ff;
2370 } else {
2371 sense_buf[0] = 0x70;
2372 sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
2373 sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
2374 sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
2376 if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
2377 if (RES_IS_VSET(res->cfg_entry))
2378 failing_lba =
2379 le32_to_cpu(ioasa->u.
2380 vset.failing_lba_lo);
2381 sense_buf[0] |= 0x80;
2382 sense_buf[3] = (failing_lba >> 24) & 0xff;
2383 sense_buf[4] = (failing_lba >> 16) & 0xff;
2384 sense_buf[5] = (failing_lba >> 8) & 0xff;
2385 sense_buf[6] = failing_lba & 0xff;
2388 sense_buf[7] = 6; /* additional length */
2393 * pmcraid_error_handler - Error response handlers for a SCSI op
2394 * @cmd: pointer to pmcraid_cmd that has failed
2396 * This function determines whether or not to initiate ERP on the affected
2397 * device. This is called from a tasklet, which doesn't hold any locks.
2399 * Return value:
2400 * 0 it caller can complete the request, otherwise 1 where in error
2401 * handler itself completes the request and returns the command block
2402 * back to free-pool
2404 static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
2406 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2407 struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
2408 struct pmcraid_instance *pinstance = cmd->drv_inst;
2409 struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
2410 u32 ioasc = le32_to_cpu(ioasa->ioasc);
2411 u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
2412 u32 sense_copied = 0;
2414 if (!res) {
2415 pmcraid_info("resource pointer is NULL\n");
2416 return 0;
2419 /* If this was a SCSI read/write command keep count of errors */
2420 if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
2421 atomic_inc(&res->read_failures);
2422 else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
2423 atomic_inc(&res->write_failures);
2425 if (!RES_IS_GSCSI(res->cfg_entry) &&
2426 masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
2427 pmcraid_frame_auto_sense(cmd);
2430 /* Log IOASC/IOASA information based on user settings */
2431 pmcraid_ioasc_logger(ioasc, cmd);
2433 switch (masked_ioasc) {
2435 case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
2436 scsi_cmd->result |= (DID_ABORT << 16);
2437 break;
2439 case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
2440 case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
2441 scsi_cmd->result |= (DID_NO_CONNECT << 16);
2442 break;
2444 case PMCRAID_IOASC_NR_SYNC_REQUIRED:
2445 res->sync_reqd = 1;
2446 scsi_cmd->result |= (DID_IMM_RETRY << 16);
2447 break;
2449 case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
2450 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
2451 break;
2453 case PMCRAID_IOASC_UA_BUS_WAS_RESET:
2454 case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
2455 if (!res->reset_progress)
2456 scsi_report_bus_reset(pinstance->host,
2457 scsi_cmd->device->channel);
2458 scsi_cmd->result |= (DID_ERROR << 16);
2459 break;
2461 case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
2462 scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
2463 res->sync_reqd = 1;
2465 /* if check_condition is not active return with error otherwise
2466 * get/frame the sense buffer
2468 if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
2469 SAM_STAT_CHECK_CONDITION &&
2470 PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
2471 return 0;
2473 /* If we have auto sense data as part of IOASA pass it to
2474 * mid-layer
2476 if (ioasa->auto_sense_length != 0) {
2477 short sense_len = ioasa->auto_sense_length;
2478 int data_size = min_t(u16, le16_to_cpu(sense_len),
2479 SCSI_SENSE_BUFFERSIZE);
2481 memcpy(scsi_cmd->sense_buffer,
2482 ioasa->sense_data,
2483 data_size);
2484 sense_copied = 1;
2487 if (RES_IS_GSCSI(res->cfg_entry))
2488 pmcraid_cancel_all(cmd, sense_copied);
2489 else if (sense_copied)
2490 pmcraid_erp_done(cmd);
2491 else
2492 pmcraid_request_sense(cmd);
2494 return 1;
2496 case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
2497 break;
2499 default:
2500 if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
2501 scsi_cmd->result |= (DID_ERROR << 16);
2502 break;
2504 return 0;
2508 * pmcraid_reset_device - device reset handler functions
2510 * @scsi_cmd: scsi command struct
2511 * @modifier: reset modifier indicating the reset sequence to be performed
2513 * This function issues a device reset to the affected device.
2514 * A LUN reset will be sent to the device first. If that does
2515 * not work, a target reset will be sent.
2517 * Return value:
2518 * SUCCESS / FAILED
2520 static int pmcraid_reset_device(
2521 struct scsi_cmnd *scsi_cmd,
2522 unsigned long timeout,
2523 u8 modifier
2526 struct pmcraid_cmd *cmd;
2527 struct pmcraid_instance *pinstance;
2528 struct pmcraid_resource_entry *res;
2529 struct pmcraid_ioarcb *ioarcb;
2530 unsigned long lock_flags;
2531 u32 ioasc;
2533 pinstance =
2534 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
2535 res = scsi_cmd->device->hostdata;
2537 if (!res) {
2538 sdev_printk(KERN_ERR, scsi_cmd->device,
2539 "reset_device: NULL resource pointer\n");
2540 return FAILED;
2543 /* If adapter is currently going through reset/reload, return failed.
2544 * This will force the mid-layer to call _eh_bus/host reset, which
2545 * will then go to sleep and wait for the reset to complete
2547 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2548 if (pinstance->ioa_reset_in_progress ||
2549 pinstance->ioa_state == IOA_STATE_DEAD) {
2550 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2551 return FAILED;
2554 res->reset_progress = 1;
2555 pmcraid_info("Resetting %s resource with addr %x\n",
2556 ((modifier & RESET_DEVICE_LUN) ? "LUN" :
2557 ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
2558 le32_to_cpu(res->cfg_entry.resource_address));
2560 /* get a free cmd block */
2561 cmd = pmcraid_get_free_cmd(pinstance);
2563 if (cmd == NULL) {
2564 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2565 pmcraid_err("%s: no cmd blocks are available\n", __func__);
2566 return FAILED;
2569 ioarcb = &cmd->ioa_cb->ioarcb;
2570 ioarcb->resource_handle = res->cfg_entry.resource_handle;
2571 ioarcb->request_type = REQ_TYPE_IOACMD;
2572 ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
2574 /* Initialize reset modifier bits */
2575 if (modifier)
2576 modifier = ENABLE_RESET_MODIFIER | modifier;
2578 ioarcb->cdb[1] = modifier;
2580 init_completion(&cmd->wait_for_completion);
2581 cmd->completion_req = 1;
2583 pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
2584 cmd->ioa_cb->ioarcb.cdb[0],
2585 le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
2586 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
2588 pmcraid_send_cmd(cmd,
2589 pmcraid_internal_done,
2590 timeout,
2591 pmcraid_timeout_handler);
2593 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2595 /* RESET_DEVICE command completes after all pending IOARCBs are
2596 * completed. Once this command is completed, pmcraind_internal_done
2597 * will wake up the 'completion' queue.
2599 wait_for_completion(&cmd->wait_for_completion);
2601 /* complete the command here itself and return the command block
2602 * to free list
2604 pmcraid_return_cmd(cmd);
2605 res->reset_progress = 0;
2606 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
2608 /* set the return value based on the returned ioasc */
2609 return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
2613 * _pmcraid_io_done - helper for pmcraid_io_done function
2615 * @cmd: pointer to pmcraid command struct
2616 * @reslen: residual data length to be set in the ioasa
2617 * @ioasc: ioasc either returned by IOA or set by driver itself.
2619 * This function is invoked by pmcraid_io_done to complete mid-layer
2620 * scsi ops.
2622 * Return value:
2623 * 0 if caller is required to return it to free_pool. Returns 1 if
2624 * caller need not worry about freeing command block as error handler
2625 * will take care of that.
2628 static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
2630 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2631 int rc = 0;
2633 scsi_set_resid(scsi_cmd, reslen);
2635 pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
2636 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
2637 cmd->ioa_cb->ioarcb.cdb[0],
2638 ioasc, scsi_cmd->result);
2640 if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
2641 rc = pmcraid_error_handler(cmd);
2643 if (rc == 0) {
2644 scsi_dma_unmap(scsi_cmd);
2645 scsi_cmd->scsi_done(scsi_cmd);
2648 return rc;
2652 * pmcraid_io_done - SCSI completion function
2654 * @cmd: pointer to pmcraid command struct
2656 * This function is invoked by tasklet/mid-layer error handler to completing
2657 * the SCSI ops sent from mid-layer.
2659 * Return value
2660 * none
2663 static void pmcraid_io_done(struct pmcraid_cmd *cmd)
2665 u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
2666 u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
2668 if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
2669 pmcraid_return_cmd(cmd);
2673 * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
2675 * @cmd: command block of the command to be aborted
2677 * Return Value:
2678 * returns pointer to command structure used as cancelling cmd
2680 static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
2682 struct pmcraid_cmd *cancel_cmd;
2683 struct pmcraid_instance *pinstance;
2684 struct pmcraid_resource_entry *res;
2686 pinstance = (struct pmcraid_instance *)cmd->drv_inst;
2687 res = cmd->scsi_cmd->device->hostdata;
2689 cancel_cmd = pmcraid_get_free_cmd(pinstance);
2691 if (cancel_cmd == NULL) {
2692 pmcraid_err("%s: no cmd blocks are available\n", __func__);
2693 return NULL;
2696 pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
2698 pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
2699 cmd->ioa_cb->ioarcb.cdb[0],
2700 cmd->ioa_cb->ioarcb.response_handle >> 2);
2702 init_completion(&cancel_cmd->wait_for_completion);
2703 cancel_cmd->completion_req = 1;
2705 pmcraid_info("command (%d) CDB[0] = %x for %x\n",
2706 le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
2707 cmd->ioa_cb->ioarcb.cdb[0],
2708 le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
2710 pmcraid_send_cmd(cancel_cmd,
2711 pmcraid_internal_done,
2712 PMCRAID_INTERNAL_TIMEOUT,
2713 pmcraid_timeout_handler);
2714 return cancel_cmd;
2718 * pmcraid_abort_complete - Waits for ABORT TASK completion
2720 * @cancel_cmd: command block use as cancelling command
2722 * Return Value:
2723 * returns SUCCESS if ABORT TASK has good completion
2724 * otherwise FAILED
2726 static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
2728 struct pmcraid_resource_entry *res;
2729 u32 ioasc;
2731 wait_for_completion(&cancel_cmd->wait_for_completion);
2732 res = cancel_cmd->u.res;
2733 cancel_cmd->u.res = NULL;
2734 ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
2736 /* If the abort task is not timed out we will get a Good completion
2737 * as sense_key, otherwise we may get one the following responses
2738 * due to subsquent bus reset or device reset. In case IOASC is
2739 * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
2741 if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
2742 ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
2743 if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
2744 res->sync_reqd = 1;
2745 ioasc = 0;
2748 /* complete the command here itself */
2749 pmcraid_return_cmd(cancel_cmd);
2750 return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
2754 * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
2756 * @scsi_cmd: scsi command struct given by mid-layer. When this is called
2757 * mid-layer ensures that no other commands are queued. This
2758 * never gets called under interrupt, but a separate eh thread.
2760 * Return value:
2761 * SUCCESS / FAILED
2763 static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
2765 struct pmcraid_instance *pinstance;
2766 struct pmcraid_cmd *cmd;
2767 struct pmcraid_resource_entry *res;
2768 unsigned long host_lock_flags;
2769 unsigned long pending_lock_flags;
2770 struct pmcraid_cmd *cancel_cmd = NULL;
2771 int cmd_found = 0;
2772 int rc = FAILED;
2774 pinstance =
2775 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
2777 scmd_printk(KERN_INFO, scsi_cmd,
2778 "I/O command timed out, aborting it.\n");
2780 res = scsi_cmd->device->hostdata;
2782 if (res == NULL)
2783 return rc;
2785 /* If we are currently going through reset/reload, return failed.
2786 * This will force the mid-layer to eventually call
2787 * pmcraid_eh_host_reset which will then go to sleep and wait for the
2788 * reset to complete
2790 spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
2792 if (pinstance->ioa_reset_in_progress ||
2793 pinstance->ioa_state == IOA_STATE_DEAD) {
2794 spin_unlock_irqrestore(pinstance->host->host_lock,
2795 host_lock_flags);
2796 return rc;
2799 /* loop over pending cmd list to find cmd corresponding to this
2800 * scsi_cmd. Note that this command might not have been completed
2801 * already. locking: all pending commands are protected with
2802 * pending_pool_lock.
2804 spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
2805 list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
2807 if (cmd->scsi_cmd == scsi_cmd) {
2808 cmd_found = 1;
2809 break;
2813 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
2814 pending_lock_flags);
2816 /* If the command to be aborted was given to IOA and still pending with
2817 * it, send ABORT_TASK to abort this and wait for its completion
2819 if (cmd_found)
2820 cancel_cmd = pmcraid_abort_cmd(cmd);
2822 spin_unlock_irqrestore(pinstance->host->host_lock,
2823 host_lock_flags);
2825 if (cancel_cmd) {
2826 cancel_cmd->u.res = cmd->scsi_cmd->device->hostdata;
2827 rc = pmcraid_abort_complete(cancel_cmd);
2830 return cmd_found ? rc : SUCCESS;
2834 * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
2836 * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
2838 * All these routines invokve pmcraid_reset_device with appropriate parameters.
2839 * Since these are called from mid-layer EH thread, no other IO will be queued
2840 * to the resource being reset. However, control path (IOCTL) may be active so
2841 * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
2842 * takes care by locking/unlocking host_lock.
2844 * Return value
2845 * SUCCESS or FAILED
2847 static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
2849 scmd_printk(KERN_INFO, scmd,
2850 "resetting device due to an I/O command timeout.\n");
2851 return pmcraid_reset_device(scmd,
2852 PMCRAID_INTERNAL_TIMEOUT,
2853 RESET_DEVICE_LUN);
2856 static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
2858 scmd_printk(KERN_INFO, scmd,
2859 "Doing bus reset due to an I/O command timeout.\n");
2860 return pmcraid_reset_device(scmd,
2861 PMCRAID_RESET_BUS_TIMEOUT,
2862 RESET_DEVICE_BUS);
2865 static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
2867 scmd_printk(KERN_INFO, scmd,
2868 "Doing target reset due to an I/O command timeout.\n");
2869 return pmcraid_reset_device(scmd,
2870 PMCRAID_INTERNAL_TIMEOUT,
2871 RESET_DEVICE_TARGET);
2875 * pmcraid_eh_host_reset_handler - adapter reset handler callback
2877 * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
2879 * Initiates adapter reset to bring it up to operational state
2881 * Return value
2882 * SUCCESS or FAILED
2884 static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
2886 unsigned long interval = 10000; /* 10 seconds interval */
2887 int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
2888 struct pmcraid_instance *pinstance =
2889 (struct pmcraid_instance *)(scmd->device->host->hostdata);
2892 /* wait for an additional 150 seconds just in case firmware could come
2893 * up and if it could complete all the pending commands excluding the
2894 * two HCAM (CCN and LDN).
2896 while (waits--) {
2897 if (atomic_read(&pinstance->outstanding_cmds) <=
2898 PMCRAID_MAX_HCAM_CMD)
2899 return SUCCESS;
2900 msleep(interval);
2903 dev_err(&pinstance->pdev->dev,
2904 "Adapter being reset due to an I/O command timeout.\n");
2905 return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
2909 * pmcraid_task_attributes - Translate SPI Q-Tags to task attributes
2910 * @scsi_cmd: scsi command struct
2912 * Return value
2913 * number of tags or 0 if the task is not tagged
2915 static u8 pmcraid_task_attributes(struct scsi_cmnd *scsi_cmd)
2917 char tag[2];
2918 u8 rc = 0;
2920 if (scsi_populate_tag_msg(scsi_cmd, tag)) {
2921 switch (tag[0]) {
2922 case MSG_SIMPLE_TAG:
2923 rc = TASK_TAG_SIMPLE;
2924 break;
2925 case MSG_HEAD_TAG:
2926 rc = TASK_TAG_QUEUE_HEAD;
2927 break;
2928 case MSG_ORDERED_TAG:
2929 rc = TASK_TAG_ORDERED;
2930 break;
2934 return rc;
2939 * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
2940 * @cmd: pmcraid command struct
2941 * @sgcount: count of scatter-gather elements
2943 * Return value
2944 * returns pointer pmcraid_ioadl_desc, initialized to point to internal
2945 * or external IOADLs
2947 struct pmcraid_ioadl_desc *
2948 pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
2950 struct pmcraid_ioadl_desc *ioadl;
2951 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2952 int ioadl_count = 0;
2954 if (ioarcb->add_cmd_param_length)
2955 ioadl_count = DIV_ROUND_UP(ioarcb->add_cmd_param_length, 16);
2956 ioarcb->ioadl_length =
2957 sizeof(struct pmcraid_ioadl_desc) * sgcount;
2959 if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
2960 /* external ioadls start at offset 0x80 from control_block
2961 * structure, re-using 24 out of 27 ioadls part of IOARCB.
2962 * It is necessary to indicate to firmware that driver is
2963 * using ioadls to be treated as external to IOARCB.
2965 ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
2966 ioarcb->ioadl_bus_addr =
2967 cpu_to_le64((cmd->ioa_cb_bus_addr) +
2968 offsetof(struct pmcraid_ioarcb,
2969 add_data.u.ioadl[3]));
2970 ioadl = &ioarcb->add_data.u.ioadl[3];
2971 } else {
2972 ioarcb->ioadl_bus_addr =
2973 cpu_to_le64((cmd->ioa_cb_bus_addr) +
2974 offsetof(struct pmcraid_ioarcb,
2975 add_data.u.ioadl[ioadl_count]));
2977 ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
2978 ioarcb->ioarcb_bus_addr |=
2979 DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8);
2982 return ioadl;
2986 * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
2987 * @pinstance: pointer to adapter instance structure
2988 * @cmd: pmcraid command struct
2990 * This function is invoked by queuecommand entry point while sending a command
2991 * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
2993 * Return value:
2994 * 0 on success or -1 on failure
2996 static int pmcraid_build_ioadl(
2997 struct pmcraid_instance *pinstance,
2998 struct pmcraid_cmd *cmd
3001 int i, nseg;
3002 struct scatterlist *sglist;
3004 struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
3005 struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
3006 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
3008 u32 length = scsi_bufflen(scsi_cmd);
3010 if (!length)
3011 return 0;
3013 nseg = scsi_dma_map(scsi_cmd);
3015 if (nseg < 0) {
3016 scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
3017 return -1;
3018 } else if (nseg > PMCRAID_MAX_IOADLS) {
3019 scsi_dma_unmap(scsi_cmd);
3020 scmd_printk(KERN_ERR, scsi_cmd,
3021 "sg count is (%d) more than allowed!\n", nseg);
3022 return -1;
3025 /* Initialize IOARCB data transfer length fields */
3026 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
3027 ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
3029 ioarcb->request_flags0 |= NO_LINK_DESCS;
3030 ioarcb->data_transfer_length = cpu_to_le32(length);
3031 ioadl = pmcraid_init_ioadls(cmd, nseg);
3033 /* Initialize IOADL descriptor addresses */
3034 scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
3035 ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
3036 ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
3037 ioadl[i].flags = 0;
3039 /* setup last descriptor */
3040 ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
3042 return 0;
3046 * pmcraid_free_sglist - Frees an allocated SG buffer list
3047 * @sglist: scatter/gather list pointer
3049 * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
3051 * Return value:
3052 * none
3054 static void pmcraid_free_sglist(struct pmcraid_sglist *sglist)
3056 int i;
3058 for (i = 0; i < sglist->num_sg; i++)
3059 __free_pages(sg_page(&(sglist->scatterlist[i])),
3060 sglist->order);
3062 kfree(sglist);
3066 * pmcraid_alloc_sglist - Allocates memory for a SG list
3067 * @buflen: buffer length
3069 * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3070 * list.
3072 * Return value
3073 * pointer to sglist / NULL on failure
3075 static struct pmcraid_sglist *pmcraid_alloc_sglist(int buflen)
3077 struct pmcraid_sglist *sglist;
3078 struct scatterlist *scatterlist;
3079 struct page *page;
3080 int num_elem, i, j;
3081 int sg_size;
3082 int order;
3083 int bsize_elem;
3085 sg_size = buflen / (PMCRAID_MAX_IOADLS - 1);
3086 order = (sg_size > 0) ? get_order(sg_size) : 0;
3087 bsize_elem = PAGE_SIZE * (1 << order);
3089 /* Determine the actual number of sg entries needed */
3090 if (buflen % bsize_elem)
3091 num_elem = (buflen / bsize_elem) + 1;
3092 else
3093 num_elem = buflen / bsize_elem;
3095 /* Allocate a scatter/gather list for the DMA */
3096 sglist = kzalloc(sizeof(struct pmcraid_sglist) +
3097 (sizeof(struct scatterlist) * (num_elem - 1)),
3098 GFP_KERNEL);
3100 if (sglist == NULL)
3101 return NULL;
3103 scatterlist = sglist->scatterlist;
3104 sg_init_table(scatterlist, num_elem);
3105 sglist->order = order;
3106 sglist->num_sg = num_elem;
3107 sg_size = buflen;
3109 for (i = 0; i < num_elem; i++) {
3110 page = alloc_pages(GFP_KERNEL|GFP_DMA, order);
3111 if (!page) {
3112 for (j = i - 1; j >= 0; j--)
3113 __free_pages(sg_page(&scatterlist[j]), order);
3114 kfree(sglist);
3115 return NULL;
3118 sg_set_page(&scatterlist[i], page,
3119 sg_size < bsize_elem ? sg_size : bsize_elem, 0);
3120 sg_size -= bsize_elem;
3123 return sglist;
3127 * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
3128 * @sglist: scatter/gather list pointer
3129 * @buffer: buffer pointer
3130 * @len: buffer length
3131 * @direction: data transfer direction
3133 * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
3135 * Return value:
3136 * 0 on success / other on failure
3138 static int pmcraid_copy_sglist(
3139 struct pmcraid_sglist *sglist,
3140 unsigned long buffer,
3141 u32 len,
3142 int direction
3145 struct scatterlist *scatterlist;
3146 void *kaddr;
3147 int bsize_elem;
3148 int i;
3149 int rc = 0;
3151 /* Determine the actual number of bytes per element */
3152 bsize_elem = PAGE_SIZE * (1 << sglist->order);
3154 scatterlist = sglist->scatterlist;
3156 for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
3157 struct page *page = sg_page(&scatterlist[i]);
3159 kaddr = kmap(page);
3160 if (direction == DMA_TO_DEVICE)
3161 rc = __copy_from_user(kaddr,
3162 (void *)buffer,
3163 bsize_elem);
3164 else
3165 rc = __copy_to_user((void *)buffer, kaddr, bsize_elem);
3167 kunmap(page);
3169 if (rc) {
3170 pmcraid_err("failed to copy user data into sg list\n");
3171 return -EFAULT;
3174 scatterlist[i].length = bsize_elem;
3177 if (len % bsize_elem) {
3178 struct page *page = sg_page(&scatterlist[i]);
3180 kaddr = kmap(page);
3182 if (direction == DMA_TO_DEVICE)
3183 rc = __copy_from_user(kaddr,
3184 (void *)buffer,
3185 len % bsize_elem);
3186 else
3187 rc = __copy_to_user((void *)buffer,
3188 kaddr,
3189 len % bsize_elem);
3191 kunmap(page);
3193 scatterlist[i].length = len % bsize_elem;
3196 if (rc) {
3197 pmcraid_err("failed to copy user data into sg list\n");
3198 rc = -EFAULT;
3201 return rc;
3205 * pmcraid_queuecommand - Queue a mid-layer request
3206 * @scsi_cmd: scsi command struct
3207 * @done: done function
3209 * This function queues a request generated by the mid-layer. Midlayer calls
3210 * this routine within host->lock. Some of the functions called by queuecommand
3211 * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
3213 * Return value:
3214 * 0 on success
3215 * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3216 * SCSI_MLQUEUE_HOST_BUSY if host is busy
3218 static int pmcraid_queuecommand(
3219 struct scsi_cmnd *scsi_cmd,
3220 void (*done) (struct scsi_cmnd *)
3223 struct pmcraid_instance *pinstance;
3224 struct pmcraid_resource_entry *res;
3225 struct pmcraid_ioarcb *ioarcb;
3226 struct pmcraid_cmd *cmd;
3227 int rc = 0;
3229 pinstance =
3230 (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
3232 scsi_cmd->scsi_done = done;
3233 res = scsi_cmd->device->hostdata;
3234 scsi_cmd->result = (DID_OK << 16);
3236 /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
3237 * the command
3239 if (pinstance->ioa_state == IOA_STATE_DEAD) {
3240 pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
3241 scsi_cmd->result = (DID_NO_CONNECT << 16);
3242 scsi_cmd->scsi_done(scsi_cmd);
3243 return 0;
3246 /* If IOA reset is in progress, can't queue the commands */
3247 if (pinstance->ioa_reset_in_progress)
3248 return SCSI_MLQUEUE_HOST_BUSY;
3250 /* initialize the command and IOARCB to be sent to IOA */
3251 cmd = pmcraid_get_free_cmd(pinstance);
3253 if (cmd == NULL) {
3254 pmcraid_err("free command block is not available\n");
3255 return SCSI_MLQUEUE_HOST_BUSY;
3258 cmd->scsi_cmd = scsi_cmd;
3259 ioarcb = &(cmd->ioa_cb->ioarcb);
3260 memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
3261 ioarcb->resource_handle = res->cfg_entry.resource_handle;
3262 ioarcb->request_type = REQ_TYPE_SCSI;
3264 cmd->cmd_done = pmcraid_io_done;
3266 if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
3267 if (scsi_cmd->underflow == 0)
3268 ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
3270 if (res->sync_reqd) {
3271 ioarcb->request_flags0 |= SYNC_COMPLETE;
3272 res->sync_reqd = 0;
3275 ioarcb->request_flags0 |= NO_LINK_DESCS;
3276 ioarcb->request_flags1 |= pmcraid_task_attributes(scsi_cmd);
3278 if (RES_IS_GSCSI(res->cfg_entry))
3279 ioarcb->request_flags1 |= DELAY_AFTER_RESET;
3282 rc = pmcraid_build_ioadl(pinstance, cmd);
3284 pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
3285 le32_to_cpu(ioarcb->response_handle) >> 2,
3286 scsi_cmd->cmnd[0], pinstance->host->unique_id,
3287 RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
3288 PMCRAID_PHYS_BUS_ID,
3289 RES_IS_VSET(res->cfg_entry) ?
3290 res->cfg_entry.unique_flags1 :
3291 RES_TARGET(res->cfg_entry.resource_address),
3292 RES_LUN(res->cfg_entry.resource_address));
3294 if (likely(rc == 0)) {
3295 _pmcraid_fire_command(cmd);
3296 } else {
3297 pmcraid_err("queuecommand could not build ioadl\n");
3298 pmcraid_return_cmd(cmd);
3299 rc = SCSI_MLQUEUE_HOST_BUSY;
3302 return rc;
3306 * pmcraid_open -char node "open" entry, allowed only users with admin access
3308 static int pmcraid_chr_open(struct inode *inode, struct file *filep)
3310 struct pmcraid_instance *pinstance;
3312 if (!capable(CAP_SYS_ADMIN))
3313 return -EACCES;
3315 /* Populate adapter instance * pointer for use by ioctl */
3316 pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
3317 filep->private_data = pinstance;
3319 return 0;
3323 * pmcraid_release - char node "release" entry point
3325 static int pmcraid_chr_release(struct inode *inode, struct file *filep)
3327 struct pmcraid_instance *pinstance =
3328 ((struct pmcraid_instance *)filep->private_data);
3330 filep->private_data = NULL;
3331 fasync_helper(-1, filep, 0, &pinstance->aen_queue);
3333 return 0;
3337 * pmcraid_fasync - Async notifier registration from applications
3339 * This function adds the calling process to a driver global queue. When an
3340 * event occurs, SIGIO will be sent to all processes in this queue.
3342 static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
3344 struct pmcraid_instance *pinstance;
3345 int rc;
3347 pinstance = (struct pmcraid_instance *)filep->private_data;
3348 mutex_lock(&pinstance->aen_queue_lock);
3349 rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
3350 mutex_unlock(&pinstance->aen_queue_lock);
3352 return rc;
3357 * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
3358 * commands sent over IOCTL interface
3360 * @cmd : pointer to struct pmcraid_cmd
3361 * @buflen : length of the request buffer
3362 * @direction : data transfer direction
3364 * Return value
3365 * 0 on success, non-zero error code on failure
3367 static int pmcraid_build_passthrough_ioadls(
3368 struct pmcraid_cmd *cmd,
3369 int buflen,
3370 int direction
3373 struct pmcraid_sglist *sglist = NULL;
3374 struct scatterlist *sg = NULL;
3375 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
3376 struct pmcraid_ioadl_desc *ioadl;
3377 int i;
3379 sglist = pmcraid_alloc_sglist(buflen);
3381 if (!sglist) {
3382 pmcraid_err("can't allocate memory for passthrough SGls\n");
3383 return -ENOMEM;
3386 sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
3387 sglist->scatterlist,
3388 sglist->num_sg, direction);
3390 if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
3391 dev_err(&cmd->drv_inst->pdev->dev,
3392 "Failed to map passthrough buffer!\n");
3393 pmcraid_free_sglist(sglist);
3394 return -EIO;
3397 cmd->sglist = sglist;
3398 ioarcb->request_flags0 |= NO_LINK_DESCS;
3400 ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
3402 /* Initialize IOADL descriptor addresses */
3403 for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
3404 ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
3405 ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
3406 ioadl[i].flags = 0;
3409 /* setup the last descriptor */
3410 ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
3412 return 0;
3417 * pmcraid_release_passthrough_ioadls - release passthrough ioadls
3419 * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
3420 * @buflen: size of the request buffer
3421 * @direction: data transfer direction
3423 * Return value
3424 * 0 on success, non-zero error code on failure
3426 static void pmcraid_release_passthrough_ioadls(
3427 struct pmcraid_cmd *cmd,
3428 int buflen,
3429 int direction
3432 struct pmcraid_sglist *sglist = cmd->sglist;
3434 if (buflen > 0) {
3435 pci_unmap_sg(cmd->drv_inst->pdev,
3436 sglist->scatterlist,
3437 sglist->num_sg,
3438 direction);
3439 pmcraid_free_sglist(sglist);
3440 cmd->sglist = NULL;
3445 * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
3447 * @pinstance: pointer to adapter instance structure
3448 * @cmd: ioctl code
3449 * @arg: pointer to pmcraid_passthrough_buffer user buffer
3451 * Return value
3452 * 0 on success, non-zero error code on failure
3454 static long pmcraid_ioctl_passthrough(
3455 struct pmcraid_instance *pinstance,
3456 unsigned int ioctl_cmd,
3457 unsigned int buflen,
3458 unsigned long arg
3461 struct pmcraid_passthrough_ioctl_buffer *buffer;
3462 struct pmcraid_ioarcb *ioarcb;
3463 struct pmcraid_cmd *cmd;
3464 struct pmcraid_cmd *cancel_cmd;
3465 unsigned long request_buffer;
3466 unsigned long request_offset;
3467 unsigned long lock_flags;
3468 int request_size;
3469 int buffer_size;
3470 u8 access, direction;
3471 int rc = 0;
3473 /* If IOA reset is in progress, wait 10 secs for reset to complete */
3474 if (pinstance->ioa_reset_in_progress) {
3475 rc = wait_event_interruptible_timeout(
3476 pinstance->reset_wait_q,
3477 !pinstance->ioa_reset_in_progress,
3478 msecs_to_jiffies(10000));
3480 if (!rc)
3481 return -ETIMEDOUT;
3482 else if (rc < 0)
3483 return -ERESTARTSYS;
3486 /* If adapter is not in operational state, return error */
3487 if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
3488 pmcraid_err("IOA is not operational\n");
3489 return -ENOTTY;
3492 buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
3493 buffer = kmalloc(buffer_size, GFP_KERNEL);
3495 if (!buffer) {
3496 pmcraid_err("no memory for passthrough buffer\n");
3497 return -ENOMEM;
3500 request_offset =
3501 offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
3503 request_buffer = arg + request_offset;
3505 rc = __copy_from_user(buffer,
3506 (struct pmcraid_passthrough_ioctl_buffer *) arg,
3507 sizeof(struct pmcraid_passthrough_ioctl_buffer));
3508 if (rc) {
3509 pmcraid_err("ioctl: can't copy passthrough buffer\n");
3510 rc = -EFAULT;
3511 goto out_free_buffer;
3514 request_size = buffer->ioarcb.data_transfer_length;
3516 if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
3517 access = VERIFY_READ;
3518 direction = DMA_TO_DEVICE;
3519 } else {
3520 access = VERIFY_WRITE;
3521 direction = DMA_FROM_DEVICE;
3524 if (request_size > 0) {
3525 rc = access_ok(access, arg, request_offset + request_size);
3527 if (!rc) {
3528 rc = -EFAULT;
3529 goto out_free_buffer;
3533 /* check if we have any additional command parameters */
3534 if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
3535 rc = -EINVAL;
3536 goto out_free_buffer;
3539 cmd = pmcraid_get_free_cmd(pinstance);
3541 if (!cmd) {
3542 pmcraid_err("free command block is not available\n");
3543 rc = -ENOMEM;
3544 goto out_free_buffer;
3547 cmd->scsi_cmd = NULL;
3548 ioarcb = &(cmd->ioa_cb->ioarcb);
3550 /* Copy the user-provided IOARCB stuff field by field */
3551 ioarcb->resource_handle = buffer->ioarcb.resource_handle;
3552 ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
3553 ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
3554 ioarcb->request_type = buffer->ioarcb.request_type;
3555 ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
3556 ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
3557 memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
3559 if (buffer->ioarcb.add_cmd_param_length) {
3560 ioarcb->add_cmd_param_length =
3561 buffer->ioarcb.add_cmd_param_length;
3562 ioarcb->add_cmd_param_offset =
3563 buffer->ioarcb.add_cmd_param_offset;
3564 memcpy(ioarcb->add_data.u.add_cmd_params,
3565 buffer->ioarcb.add_data.u.add_cmd_params,
3566 buffer->ioarcb.add_cmd_param_length);
3569 if (request_size) {
3570 rc = pmcraid_build_passthrough_ioadls(cmd,
3571 request_size,
3572 direction);
3573 if (rc) {
3574 pmcraid_err("couldn't build passthrough ioadls\n");
3575 goto out_free_buffer;
3579 /* If data is being written into the device, copy the data from user
3580 * buffers
3582 if (direction == DMA_TO_DEVICE && request_size > 0) {
3583 rc = pmcraid_copy_sglist(cmd->sglist,
3584 request_buffer,
3585 request_size,
3586 direction);
3587 if (rc) {
3588 pmcraid_err("failed to copy user buffer\n");
3589 goto out_free_sglist;
3593 /* passthrough ioctl is a blocking command so, put the user to sleep
3594 * until timeout. Note that a timeout value of 0 means, do timeout.
3596 cmd->cmd_done = pmcraid_internal_done;
3597 init_completion(&cmd->wait_for_completion);
3598 cmd->completion_req = 1;
3600 pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
3601 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
3602 cmd->ioa_cb->ioarcb.cdb[0],
3603 le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
3605 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3606 _pmcraid_fire_command(cmd);
3607 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3609 /* If command timeout is specified put caller to wait till that time,
3610 * otherwise it would be blocking wait. If command gets timed out, it
3611 * will be aborted.
3613 if (buffer->ioarcb.cmd_timeout == 0) {
3614 wait_for_completion(&cmd->wait_for_completion);
3615 } else if (!wait_for_completion_timeout(
3616 &cmd->wait_for_completion,
3617 msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
3619 pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
3620 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
3621 cmd->ioa_cb->ioarcb.cdb[0]);
3623 rc = -ETIMEDOUT;
3624 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3625 cancel_cmd = pmcraid_abort_cmd(cmd);
3626 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3628 if (cancel_cmd) {
3629 wait_for_completion(&cancel_cmd->wait_for_completion);
3630 pmcraid_return_cmd(cancel_cmd);
3633 goto out_free_sglist;
3636 /* If the command failed for any reason, copy entire IOASA buffer and
3637 * return IOCTL success. If copying IOASA to user-buffer fails, return
3638 * EFAULT
3640 if (le32_to_cpu(cmd->ioa_cb->ioasa.ioasc)) {
3642 void *ioasa =
3643 (void *)(arg +
3644 offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
3646 pmcraid_info("command failed with %x\n",
3647 le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
3648 if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
3649 sizeof(struct pmcraid_ioasa))) {
3650 pmcraid_err("failed to copy ioasa buffer to user\n");
3651 rc = -EFAULT;
3654 /* If the data transfer was from device, copy the data onto user
3655 * buffers
3657 else if (direction == DMA_FROM_DEVICE && request_size > 0) {
3658 rc = pmcraid_copy_sglist(cmd->sglist,
3659 request_buffer,
3660 request_size,
3661 direction);
3662 if (rc) {
3663 pmcraid_err("failed to copy user buffer\n");
3664 rc = -EFAULT;
3668 out_free_sglist:
3669 pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
3670 pmcraid_return_cmd(cmd);
3672 out_free_buffer:
3673 kfree(buffer);
3675 return rc;
3682 * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
3684 * @pinstance: pointer to adapter instance structure
3685 * @cmd: ioctl command passed in
3686 * @buflen: length of user_buffer
3687 * @user_buffer: user buffer pointer
3689 * Return Value
3690 * 0 in case of success, otherwise appropriate error code
3692 static long pmcraid_ioctl_driver(
3693 struct pmcraid_instance *pinstance,
3694 unsigned int cmd,
3695 unsigned int buflen,
3696 void __user *user_buffer
3699 int rc = -ENOSYS;
3701 if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
3702 pmcraid_err("ioctl_driver: access fault in request buffer \n");
3703 return -EFAULT;
3706 switch (cmd) {
3707 case PMCRAID_IOCTL_RESET_ADAPTER:
3708 pmcraid_reset_bringup(pinstance);
3709 rc = 0;
3710 break;
3712 default:
3713 break;
3716 return rc;
3720 * pmcraid_check_ioctl_buffer - check for proper access to user buffer
3722 * @cmd: ioctl command
3723 * @arg: user buffer
3724 * @hdr: pointer to kernel memory for pmcraid_ioctl_header
3726 * Return Value
3727 * negetive error code if there are access issues, otherwise zero.
3728 * Upon success, returns ioctl header copied out of user buffer.
3731 static int pmcraid_check_ioctl_buffer(
3732 int cmd,
3733 void __user *arg,
3734 struct pmcraid_ioctl_header *hdr
3737 int rc = 0;
3738 int access = VERIFY_READ;
3740 if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
3741 pmcraid_err("couldn't copy ioctl header from user buffer\n");
3742 return -EFAULT;
3745 /* check for valid driver signature */
3746 rc = memcmp(hdr->signature,
3747 PMCRAID_IOCTL_SIGNATURE,
3748 sizeof(hdr->signature));
3749 if (rc) {
3750 pmcraid_err("signature verification failed\n");
3751 return -EINVAL;
3754 /* check for appropriate buffer access */
3755 if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
3756 access = VERIFY_WRITE;
3758 rc = access_ok(access,
3759 (arg + sizeof(struct pmcraid_ioctl_header)),
3760 hdr->buffer_length);
3761 if (!rc) {
3762 pmcraid_err("access failed for user buffer of size %d\n",
3763 hdr->buffer_length);
3764 return -EFAULT;
3767 return 0;
3771 * pmcraid_ioctl - char node ioctl entry point
3773 static long pmcraid_chr_ioctl(
3774 struct file *filep,
3775 unsigned int cmd,
3776 unsigned long arg
3779 struct pmcraid_instance *pinstance = NULL;
3780 struct pmcraid_ioctl_header *hdr = NULL;
3781 int retval = -ENOTTY;
3783 hdr = kmalloc(GFP_KERNEL, sizeof(struct pmcraid_ioctl_header));
3785 if (!hdr) {
3786 pmcraid_err("faile to allocate memory for ioctl header\n");
3787 return -ENOMEM;
3790 retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
3792 if (retval) {
3793 pmcraid_info("chr_ioctl: header check failed\n");
3794 kfree(hdr);
3795 return retval;
3798 pinstance = (struct pmcraid_instance *)filep->private_data;
3800 if (!pinstance) {
3801 pmcraid_info("adapter instance is not found\n");
3802 kfree(hdr);
3803 return -ENOTTY;
3806 switch (_IOC_TYPE(cmd)) {
3808 case PMCRAID_PASSTHROUGH_IOCTL:
3809 /* If ioctl code is to download microcode, we need to block
3810 * mid-layer requests.
3812 if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
3813 scsi_block_requests(pinstance->host);
3815 retval = pmcraid_ioctl_passthrough(pinstance,
3816 cmd,
3817 hdr->buffer_length,
3818 arg);
3820 if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
3821 scsi_unblock_requests(pinstance->host);
3822 break;
3824 case PMCRAID_DRIVER_IOCTL:
3825 arg += sizeof(struct pmcraid_ioctl_header);
3826 retval = pmcraid_ioctl_driver(pinstance,
3827 cmd,
3828 hdr->buffer_length,
3829 (void __user *)arg);
3830 break;
3832 default:
3833 retval = -ENOTTY;
3834 break;
3837 kfree(hdr);
3839 return retval;
3843 * File operations structure for management interface
3845 static const struct file_operations pmcraid_fops = {
3846 .owner = THIS_MODULE,
3847 .open = pmcraid_chr_open,
3848 .release = pmcraid_chr_release,
3849 .fasync = pmcraid_chr_fasync,
3850 .unlocked_ioctl = pmcraid_chr_ioctl,
3851 #ifdef CONFIG_COMPAT
3852 .compat_ioctl = pmcraid_chr_ioctl,
3853 #endif
3860 * pmcraid_show_log_level - Display adapter's error logging level
3861 * @dev: class device struct
3862 * @buf: buffer
3864 * Return value:
3865 * number of bytes printed to buffer
3867 static ssize_t pmcraid_show_log_level(
3868 struct device *dev,
3869 struct device_attribute *attr,
3870 char *buf)
3872 struct Scsi_Host *shost = class_to_shost(dev);
3873 struct pmcraid_instance *pinstance =
3874 (struct pmcraid_instance *)shost->hostdata;
3875 return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
3879 * pmcraid_store_log_level - Change the adapter's error logging level
3880 * @dev: class device struct
3881 * @buf: buffer
3882 * @count: not used
3884 * Return value:
3885 * number of bytes printed to buffer
3887 static ssize_t pmcraid_store_log_level(
3888 struct device *dev,
3889 struct device_attribute *attr,
3890 const char *buf,
3891 size_t count
3894 struct Scsi_Host *shost;
3895 struct pmcraid_instance *pinstance;
3896 unsigned long val;
3898 if (strict_strtoul(buf, 10, &val))
3899 return -EINVAL;
3900 /* log-level should be from 0 to 2 */
3901 if (val > 2)
3902 return -EINVAL;
3904 shost = class_to_shost(dev);
3905 pinstance = (struct pmcraid_instance *)shost->hostdata;
3906 pinstance->current_log_level = val;
3908 return strlen(buf);
3911 static struct device_attribute pmcraid_log_level_attr = {
3912 .attr = {
3913 .name = "log_level",
3914 .mode = S_IRUGO | S_IWUSR,
3916 .show = pmcraid_show_log_level,
3917 .store = pmcraid_store_log_level,
3921 * pmcraid_show_drv_version - Display driver version
3922 * @dev: class device struct
3923 * @buf: buffer
3925 * Return value:
3926 * number of bytes printed to buffer
3928 static ssize_t pmcraid_show_drv_version(
3929 struct device *dev,
3930 struct device_attribute *attr,
3931 char *buf
3934 return snprintf(buf, PAGE_SIZE, "version: %s, build date: %s\n",
3935 PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
3938 static struct device_attribute pmcraid_driver_version_attr = {
3939 .attr = {
3940 .name = "drv_version",
3941 .mode = S_IRUGO,
3943 .show = pmcraid_show_drv_version,
3947 * pmcraid_show_io_adapter_id - Display driver assigned adapter id
3948 * @dev: class device struct
3949 * @buf: buffer
3951 * Return value:
3952 * number of bytes printed to buffer
3954 static ssize_t pmcraid_show_adapter_id(
3955 struct device *dev,
3956 struct device_attribute *attr,
3957 char *buf
3960 struct Scsi_Host *shost = class_to_shost(dev);
3961 struct pmcraid_instance *pinstance =
3962 (struct pmcraid_instance *)shost->hostdata;
3963 u32 adapter_id = (pinstance->pdev->bus->number << 8) |
3964 pinstance->pdev->devfn;
3965 u32 aen_group = pmcraid_event_family.id;
3967 return snprintf(buf, PAGE_SIZE,
3968 "adapter id: %d\nminor: %d\naen group: %d\n",
3969 adapter_id, MINOR(pinstance->cdev.dev), aen_group);
3972 static struct device_attribute pmcraid_adapter_id_attr = {
3973 .attr = {
3974 .name = "adapter_id",
3975 .mode = S_IRUGO | S_IWUSR,
3977 .show = pmcraid_show_adapter_id,
3980 static struct device_attribute *pmcraid_host_attrs[] = {
3981 &pmcraid_log_level_attr,
3982 &pmcraid_driver_version_attr,
3983 &pmcraid_adapter_id_attr,
3984 NULL,
3988 /* host template structure for pmcraid driver */
3989 static struct scsi_host_template pmcraid_host_template = {
3990 .module = THIS_MODULE,
3991 .name = PMCRAID_DRIVER_NAME,
3992 .queuecommand = pmcraid_queuecommand,
3993 .eh_abort_handler = pmcraid_eh_abort_handler,
3994 .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
3995 .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
3996 .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
3997 .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
3999 .slave_alloc = pmcraid_slave_alloc,
4000 .slave_configure = pmcraid_slave_configure,
4001 .slave_destroy = pmcraid_slave_destroy,
4002 .change_queue_depth = pmcraid_change_queue_depth,
4003 .change_queue_type = pmcraid_change_queue_type,
4004 .can_queue = PMCRAID_MAX_IO_CMD,
4005 .this_id = -1,
4006 .sg_tablesize = PMCRAID_MAX_IOADLS,
4007 .max_sectors = PMCRAID_IOA_MAX_SECTORS,
4008 .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
4009 .use_clustering = ENABLE_CLUSTERING,
4010 .shost_attrs = pmcraid_host_attrs,
4011 .proc_name = PMCRAID_DRIVER_NAME
4015 * pmcraid_isr_common - Common interrupt handler routine
4017 * @pinstance: pointer to adapter instance
4018 * @intrs: active interrupts (contents of ioa_host_interrupt register)
4019 * @hrrq_id: Host RRQ index
4021 * Return Value
4022 * none
4024 static void pmcraid_isr_common(
4025 struct pmcraid_instance *pinstance,
4026 u32 intrs,
4027 int hrrq_id
4030 u32 intrs_clear =
4031 (intrs & INTRS_CRITICAL_OP_IN_PROGRESS) ? intrs
4032 : INTRS_HRRQ_VALID;
4033 iowrite32(intrs_clear,
4034 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4035 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
4037 /* hrrq valid bit was set, schedule tasklet to handle the response */
4038 if (intrs_clear == INTRS_HRRQ_VALID)
4039 tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
4043 * pmcraid_isr - implements interrupt handling routine
4045 * @irq: interrupt vector number
4046 * @dev_id: pointer hrrq_vector
4048 * Return Value
4049 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4051 static irqreturn_t pmcraid_isr(int irq, void *dev_id)
4053 struct pmcraid_isr_param *hrrq_vector;
4054 struct pmcraid_instance *pinstance;
4055 unsigned long lock_flags;
4056 u32 intrs;
4058 /* In case of legacy interrupt mode where interrupts are shared across
4059 * isrs, it may be possible that the current interrupt is not from IOA
4061 if (!dev_id) {
4062 printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
4063 return IRQ_NONE;
4066 hrrq_vector = (struct pmcraid_isr_param *)dev_id;
4067 pinstance = hrrq_vector->drv_inst;
4069 /* Acquire the lock (currently host_lock) while processing interrupts.
4070 * This interval is small as most of the response processing is done by
4071 * tasklet without the lock.
4073 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
4074 intrs = pmcraid_read_interrupts(pinstance);
4076 if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0)) {
4077 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4078 return IRQ_NONE;
4081 /* Any error interrupts including unit_check, initiate IOA reset.
4082 * In case of unit check indicate to reset_sequence that IOA unit
4083 * checked and prepare for a dump during reset sequence
4085 if (intrs & PMCRAID_ERROR_INTERRUPTS) {
4087 if (intrs & INTRS_IOA_UNIT_CHECK)
4088 pinstance->ioa_unit_check = 1;
4090 iowrite32(intrs,
4091 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4092 pmcraid_err("ISR: error interrupts: %x initiating reset\n",
4093 intrs);
4094 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
4095 pmcraid_initiate_reset(pinstance);
4096 } else {
4097 pmcraid_isr_common(pinstance, intrs, hrrq_vector->hrrq_id);
4100 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4102 return IRQ_HANDLED;
4107 * pmcraid_worker_function - worker thread function
4109 * @workp: pointer to struct work queue
4111 * Return Value
4112 * None
4115 static void pmcraid_worker_function(struct work_struct *workp)
4117 struct pmcraid_instance *pinstance;
4118 struct pmcraid_resource_entry *res;
4119 struct pmcraid_resource_entry *temp;
4120 struct scsi_device *sdev;
4121 unsigned long lock_flags;
4122 unsigned long host_lock_flags;
4123 u8 bus, target, lun;
4125 pinstance = container_of(workp, struct pmcraid_instance, worker_q);
4126 /* add resources only after host is added into system */
4127 if (!atomic_read(&pinstance->expose_resources))
4128 return;
4130 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
4131 list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
4133 if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
4134 sdev = res->scsi_dev;
4136 /* host_lock must be held before calling
4137 * scsi_device_get
4139 spin_lock_irqsave(pinstance->host->host_lock,
4140 host_lock_flags);
4141 if (!scsi_device_get(sdev)) {
4142 spin_unlock_irqrestore(
4143 pinstance->host->host_lock,
4144 host_lock_flags);
4145 pmcraid_info("deleting %x from midlayer\n",
4146 res->cfg_entry.resource_address);
4147 list_move_tail(&res->queue,
4148 &pinstance->free_res_q);
4149 spin_unlock_irqrestore(
4150 &pinstance->resource_lock,
4151 lock_flags);
4152 scsi_remove_device(sdev);
4153 scsi_device_put(sdev);
4154 spin_lock_irqsave(&pinstance->resource_lock,
4155 lock_flags);
4156 res->change_detected = 0;
4157 } else {
4158 spin_unlock_irqrestore(
4159 pinstance->host->host_lock,
4160 host_lock_flags);
4165 list_for_each_entry(res, &pinstance->used_res_q, queue) {
4167 if (res->change_detected == RES_CHANGE_ADD) {
4169 if (!pmcraid_expose_resource(&res->cfg_entry))
4170 continue;
4172 if (RES_IS_VSET(res->cfg_entry)) {
4173 bus = PMCRAID_VSET_BUS_ID;
4174 target = res->cfg_entry.unique_flags1;
4175 lun = PMCRAID_VSET_LUN_ID;
4176 } else {
4177 bus = PMCRAID_PHYS_BUS_ID;
4178 target =
4179 RES_TARGET(
4180 res->cfg_entry.resource_address);
4181 lun = RES_LUN(res->cfg_entry.resource_address);
4184 res->change_detected = 0;
4185 spin_unlock_irqrestore(&pinstance->resource_lock,
4186 lock_flags);
4187 scsi_add_device(pinstance->host, bus, target, lun);
4188 spin_lock_irqsave(&pinstance->resource_lock,
4189 lock_flags);
4193 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
4197 * pmcraid_tasklet_function - Tasklet function
4199 * @instance: pointer to msix param structure
4201 * Return Value
4202 * None
4204 void pmcraid_tasklet_function(unsigned long instance)
4206 struct pmcraid_isr_param *hrrq_vector;
4207 struct pmcraid_instance *pinstance;
4208 unsigned long hrrq_lock_flags;
4209 unsigned long pending_lock_flags;
4210 unsigned long host_lock_flags;
4211 spinlock_t *lockp; /* hrrq buffer lock */
4212 int id;
4213 u32 intrs;
4214 __le32 resp;
4216 hrrq_vector = (struct pmcraid_isr_param *)instance;
4217 pinstance = hrrq_vector->drv_inst;
4218 id = hrrq_vector->hrrq_id;
4219 lockp = &(pinstance->hrrq_lock[id]);
4220 intrs = pmcraid_read_interrupts(pinstance);
4222 /* If interrupts was as part of the ioa initialization, clear and mask
4223 * it. Delete the timer and wakeup the reset engine to proceed with
4224 * reset sequence
4226 if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
4227 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
4228 pinstance->int_regs.ioa_host_interrupt_mask_reg);
4229 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
4230 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4232 if (pinstance->reset_cmd != NULL) {
4233 del_timer(&pinstance->reset_cmd->timer);
4234 spin_lock_irqsave(pinstance->host->host_lock,
4235 host_lock_flags);
4236 pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
4237 spin_unlock_irqrestore(pinstance->host->host_lock,
4238 host_lock_flags);
4240 return;
4243 /* loop through each of the commands responded by IOA. Each HRRQ buf is
4244 * protected by its own lock. Traversals must be done within this lock
4245 * as there may be multiple tasklets running on multiple CPUs. Note
4246 * that the lock is held just for picking up the response handle and
4247 * manipulating hrrq_curr/toggle_bit values.
4249 spin_lock_irqsave(lockp, hrrq_lock_flags);
4251 resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4253 while ((resp & HRRQ_TOGGLE_BIT) ==
4254 pinstance->host_toggle_bit[id]) {
4256 int cmd_index = resp >> 2;
4257 struct pmcraid_cmd *cmd = NULL;
4259 if (cmd_index < PMCRAID_MAX_CMD) {
4260 cmd = pinstance->cmd_list[cmd_index];
4261 } else {
4262 /* In case of invalid response handle, initiate IOA
4263 * reset sequence.
4265 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4267 pmcraid_err("Invalid response %d initiating reset\n",
4268 cmd_index);
4270 spin_lock_irqsave(pinstance->host->host_lock,
4271 host_lock_flags);
4272 pmcraid_initiate_reset(pinstance);
4273 spin_unlock_irqrestore(pinstance->host->host_lock,
4274 host_lock_flags);
4276 spin_lock_irqsave(lockp, hrrq_lock_flags);
4277 break;
4280 if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
4281 pinstance->hrrq_curr[id]++;
4282 } else {
4283 pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
4284 pinstance->host_toggle_bit[id] ^= 1u;
4287 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4289 spin_lock_irqsave(&pinstance->pending_pool_lock,
4290 pending_lock_flags);
4291 list_del(&cmd->free_list);
4292 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
4293 pending_lock_flags);
4294 del_timer(&cmd->timer);
4295 atomic_dec(&pinstance->outstanding_cmds);
4297 if (cmd->cmd_done == pmcraid_ioa_reset) {
4298 spin_lock_irqsave(pinstance->host->host_lock,
4299 host_lock_flags);
4300 cmd->cmd_done(cmd);
4301 spin_unlock_irqrestore(pinstance->host->host_lock,
4302 host_lock_flags);
4303 } else if (cmd->cmd_done != NULL) {
4304 cmd->cmd_done(cmd);
4306 /* loop over until we are done with all responses */
4307 spin_lock_irqsave(lockp, hrrq_lock_flags);
4308 resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4311 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4315 * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
4316 * @pinstance: pointer to adapter instance structure
4318 * This routine un-registers registered interrupt handler and
4319 * also frees irqs/vectors.
4321 * Retun Value
4322 * None
4324 static
4325 void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
4327 free_irq(pinstance->pdev->irq, &(pinstance->hrrq_vector[0]));
4331 * pmcraid_register_interrupt_handler - registers interrupt handler
4332 * @pinstance: pointer to per-adapter instance structure
4334 * Return Value
4335 * 0 on success, non-zero error code otherwise.
4337 static int
4338 pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
4340 struct pci_dev *pdev = pinstance->pdev;
4342 pinstance->hrrq_vector[0].hrrq_id = 0;
4343 pinstance->hrrq_vector[0].drv_inst = pinstance;
4344 pinstance->hrrq_vector[0].vector = 0;
4345 pinstance->num_hrrq = 1;
4346 return request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
4347 PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
4351 * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
4352 * @pinstance: per adapter instance structure pointer
4353 * @max_index: number of buffer blocks to release
4355 * Return Value
4356 * None
4358 static void
4359 pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
4361 int i;
4362 for (i = 0; i < max_index; i++) {
4363 kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
4364 pinstance->cmd_list[i] = NULL;
4366 kmem_cache_destroy(pinstance->cmd_cachep);
4367 pinstance->cmd_cachep = NULL;
4371 * pmcraid_release_control_blocks - releases buffers alloced for control blocks
4372 * @pinstance: pointer to per adapter instance structure
4373 * @max_index: number of buffers (from 0 onwards) to release
4375 * This function assumes that the command blocks for which control blocks are
4376 * linked are not released.
4378 * Return Value
4379 * None
4381 static void
4382 pmcraid_release_control_blocks(
4383 struct pmcraid_instance *pinstance,
4384 int max_index
4387 int i;
4389 if (pinstance->control_pool == NULL)
4390 return;
4392 for (i = 0; i < max_index; i++) {
4393 pci_pool_free(pinstance->control_pool,
4394 pinstance->cmd_list[i]->ioa_cb,
4395 pinstance->cmd_list[i]->ioa_cb_bus_addr);
4396 pinstance->cmd_list[i]->ioa_cb = NULL;
4397 pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
4399 pci_pool_destroy(pinstance->control_pool);
4400 pinstance->control_pool = NULL;
4404 * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
4405 * @pinstance - pointer to per adapter instance structure
4407 * Allocates memory for command blocks using kernel slab allocator.
4409 * Return Value
4410 * 0 in case of success; -ENOMEM in case of failure
4412 static int __devinit
4413 pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
4415 int i;
4417 sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
4418 pinstance->host->unique_id);
4421 pinstance->cmd_cachep = kmem_cache_create(
4422 pinstance->cmd_pool_name,
4423 sizeof(struct pmcraid_cmd), 0,
4424 SLAB_HWCACHE_ALIGN, NULL);
4425 if (!pinstance->cmd_cachep)
4426 return -ENOMEM;
4428 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4429 pinstance->cmd_list[i] =
4430 kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
4431 if (!pinstance->cmd_list[i]) {
4432 pmcraid_release_cmd_blocks(pinstance, i);
4433 return -ENOMEM;
4436 return 0;
4440 * pmcraid_allocate_control_blocks - allocates memory control blocks
4441 * @pinstance : pointer to per adapter instance structure
4443 * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
4444 * and IOASAs. This is called after command blocks are already allocated.
4446 * Return Value
4447 * 0 in case it can allocate all control blocks, otherwise -ENOMEM
4449 static int __devinit
4450 pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
4452 int i;
4454 sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
4455 pinstance->host->unique_id);
4457 pinstance->control_pool =
4458 pci_pool_create(pinstance->ctl_pool_name,
4459 pinstance->pdev,
4460 sizeof(struct pmcraid_control_block),
4461 PMCRAID_IOARCB_ALIGNMENT, 0);
4463 if (!pinstance->control_pool)
4464 return -ENOMEM;
4466 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4467 pinstance->cmd_list[i]->ioa_cb =
4468 pci_pool_alloc(
4469 pinstance->control_pool,
4470 GFP_KERNEL,
4471 &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
4473 if (!pinstance->cmd_list[i]->ioa_cb) {
4474 pmcraid_release_control_blocks(pinstance, i);
4475 return -ENOMEM;
4477 memset(pinstance->cmd_list[i]->ioa_cb, 0,
4478 sizeof(struct pmcraid_control_block));
4480 return 0;
4484 * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
4485 * @pinstance: pointer to per adapter instance structure
4486 * @maxindex: size of hrrq buffer pointer array
4488 * Return Value
4489 * None
4491 static void
4492 pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
4494 int i;
4495 for (i = 0; i < maxindex; i++) {
4497 pci_free_consistent(pinstance->pdev,
4498 HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
4499 pinstance->hrrq_start[i],
4500 pinstance->hrrq_start_bus_addr[i]);
4502 /* reset pointers and toggle bit to zeros */
4503 pinstance->hrrq_start[i] = NULL;
4504 pinstance->hrrq_start_bus_addr[i] = 0;
4505 pinstance->host_toggle_bit[i] = 0;
4510 * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
4511 * @pinstance: pointer to per adapter instance structure
4513 * Return value
4514 * 0 hrrq buffers are allocated, -ENOMEM otherwise.
4516 static int __devinit
4517 pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
4519 int i;
4520 int buf_count = PMCRAID_MAX_CMD / pinstance->num_hrrq;
4522 for (i = 0; i < pinstance->num_hrrq; i++) {
4523 int buffer_size = HRRQ_ENTRY_SIZE * buf_count;
4525 pinstance->hrrq_start[i] =
4526 pci_alloc_consistent(
4527 pinstance->pdev,
4528 buffer_size,
4529 &(pinstance->hrrq_start_bus_addr[i]));
4531 if (pinstance->hrrq_start[i] == 0) {
4532 pmcraid_err("could not allocate host rrq: %d\n", i);
4533 pmcraid_release_host_rrqs(pinstance, i);
4534 return -ENOMEM;
4537 memset(pinstance->hrrq_start[i], 0, buffer_size);
4538 pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4539 pinstance->hrrq_end[i] =
4540 pinstance->hrrq_start[i] + buf_count - 1;
4541 pinstance->host_toggle_bit[i] = 1;
4542 spin_lock_init(&pinstance->hrrq_lock[i]);
4544 return 0;
4548 * pmcraid_release_hcams - release HCAM buffers
4550 * @pinstance: pointer to per adapter instance structure
4552 * Return value
4553 * none
4555 static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
4557 if (pinstance->ccn.msg != NULL) {
4558 pci_free_consistent(pinstance->pdev,
4559 PMCRAID_AEN_HDR_SIZE +
4560 sizeof(struct pmcraid_hcam_ccn),
4561 pinstance->ccn.msg,
4562 pinstance->ccn.baddr);
4564 pinstance->ccn.msg = NULL;
4565 pinstance->ccn.hcam = NULL;
4566 pinstance->ccn.baddr = 0;
4569 if (pinstance->ldn.msg != NULL) {
4570 pci_free_consistent(pinstance->pdev,
4571 PMCRAID_AEN_HDR_SIZE +
4572 sizeof(struct pmcraid_hcam_ldn),
4573 pinstance->ldn.msg,
4574 pinstance->ldn.baddr);
4576 pinstance->ldn.msg = NULL;
4577 pinstance->ldn.hcam = NULL;
4578 pinstance->ldn.baddr = 0;
4583 * pmcraid_allocate_hcams - allocates HCAM buffers
4584 * @pinstance : pointer to per adapter instance structure
4586 * Return Value:
4587 * 0 in case of successful allocation, non-zero otherwise
4589 static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
4591 pinstance->ccn.msg = pci_alloc_consistent(
4592 pinstance->pdev,
4593 PMCRAID_AEN_HDR_SIZE +
4594 sizeof(struct pmcraid_hcam_ccn),
4595 &(pinstance->ccn.baddr));
4597 pinstance->ldn.msg = pci_alloc_consistent(
4598 pinstance->pdev,
4599 PMCRAID_AEN_HDR_SIZE +
4600 sizeof(struct pmcraid_hcam_ldn),
4601 &(pinstance->ldn.baddr));
4603 if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
4604 pmcraid_release_hcams(pinstance);
4605 } else {
4606 pinstance->ccn.hcam =
4607 (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
4608 pinstance->ldn.hcam =
4609 (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
4611 atomic_set(&pinstance->ccn.ignore, 0);
4612 atomic_set(&pinstance->ldn.ignore, 0);
4615 return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
4619 * pmcraid_release_config_buffers - release config.table buffers
4620 * @pinstance: pointer to per adapter instance structure
4622 * Return Value
4623 * none
4625 static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
4627 if (pinstance->cfg_table != NULL &&
4628 pinstance->cfg_table_bus_addr != 0) {
4629 pci_free_consistent(pinstance->pdev,
4630 sizeof(struct pmcraid_config_table),
4631 pinstance->cfg_table,
4632 pinstance->cfg_table_bus_addr);
4633 pinstance->cfg_table = NULL;
4634 pinstance->cfg_table_bus_addr = 0;
4637 if (pinstance->res_entries != NULL) {
4638 int i;
4640 for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4641 list_del(&pinstance->res_entries[i].queue);
4642 kfree(pinstance->res_entries);
4643 pinstance->res_entries = NULL;
4646 pmcraid_release_hcams(pinstance);
4650 * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
4651 * @pinstance : pointer to per adapter instance structure
4653 * Return Value
4654 * 0 for successful allocation, -ENOMEM for any failure
4656 static int __devinit
4657 pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
4659 int i;
4661 pinstance->res_entries =
4662 kzalloc(sizeof(struct pmcraid_resource_entry) *
4663 PMCRAID_MAX_RESOURCES, GFP_KERNEL);
4665 if (NULL == pinstance->res_entries) {
4666 pmcraid_err("failed to allocate memory for resource table\n");
4667 return -ENOMEM;
4670 for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4671 list_add_tail(&pinstance->res_entries[i].queue,
4672 &pinstance->free_res_q);
4674 pinstance->cfg_table =
4675 pci_alloc_consistent(pinstance->pdev,
4676 sizeof(struct pmcraid_config_table),
4677 &pinstance->cfg_table_bus_addr);
4679 if (NULL == pinstance->cfg_table) {
4680 pmcraid_err("couldn't alloc DMA memory for config table\n");
4681 pmcraid_release_config_buffers(pinstance);
4682 return -ENOMEM;
4685 if (pmcraid_allocate_hcams(pinstance)) {
4686 pmcraid_err("could not alloc DMA memory for HCAMS\n");
4687 pmcraid_release_config_buffers(pinstance);
4688 return -ENOMEM;
4691 return 0;
4695 * pmcraid_init_tasklets - registers tasklets for response handling
4697 * @pinstance: pointer adapter instance structure
4699 * Return value
4700 * none
4702 static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
4704 int i;
4705 for (i = 0; i < pinstance->num_hrrq; i++)
4706 tasklet_init(&pinstance->isr_tasklet[i],
4707 pmcraid_tasklet_function,
4708 (unsigned long)&pinstance->hrrq_vector[i]);
4712 * pmcraid_kill_tasklets - destroys tasklets registered for response handling
4714 * @pinstance: pointer to adapter instance structure
4716 * Return value
4717 * none
4719 static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
4721 int i;
4722 for (i = 0; i < pinstance->num_hrrq; i++)
4723 tasklet_kill(&pinstance->isr_tasklet[i]);
4727 * pmcraid_init_buffers - allocates memory and initializes various structures
4728 * @pinstance: pointer to per adapter instance structure
4730 * This routine pre-allocates memory based on the type of block as below:
4731 * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
4732 * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
4733 * config-table entries : DMAable memory using pci_alloc_consistent
4734 * HostRRQs : DMAable memory, using pci_alloc_consistent
4736 * Return Value
4737 * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
4739 static int __devinit pmcraid_init_buffers(struct pmcraid_instance *pinstance)
4741 int i;
4743 if (pmcraid_allocate_host_rrqs(pinstance)) {
4744 pmcraid_err("couldn't allocate memory for %d host rrqs\n",
4745 pinstance->num_hrrq);
4746 return -ENOMEM;
4749 if (pmcraid_allocate_config_buffers(pinstance)) {
4750 pmcraid_err("couldn't allocate memory for config buffers\n");
4751 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4752 return -ENOMEM;
4755 if (pmcraid_allocate_cmd_blocks(pinstance)) {
4756 pmcraid_err("couldn't allocate memory for cmd blocks \n");
4757 pmcraid_release_config_buffers(pinstance);
4758 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4759 return -ENOMEM;
4762 if (pmcraid_allocate_control_blocks(pinstance)) {
4763 pmcraid_err("couldn't allocate memory control blocks \n");
4764 pmcraid_release_config_buffers(pinstance);
4765 pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
4766 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4767 return -ENOMEM;
4770 /* Initialize all the command blocks and add them to free pool. No
4771 * need to lock (free_pool_lock) as this is done in initialization
4772 * itself
4774 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4775 struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
4776 pmcraid_init_cmdblk(cmdp, i);
4777 cmdp->drv_inst = pinstance;
4778 list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
4781 return 0;
4785 * pmcraid_reinit_buffers - resets various buffer pointers
4786 * @pinstance: pointer to adapter instance
4787 * Return value
4788 * none
4790 static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
4792 int i;
4793 int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
4795 for (i = 0; i < pinstance->num_hrrq; i++) {
4796 memset(pinstance->hrrq_start[i], 0, buffer_size);
4797 pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4798 pinstance->hrrq_end[i] =
4799 pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
4800 pinstance->host_toggle_bit[i] = 1;
4805 * pmcraid_init_instance - initialize per instance data structure
4806 * @pdev: pointer to pci device structure
4807 * @host: pointer to Scsi_Host structure
4808 * @mapped_pci_addr: memory mapped IOA configuration registers
4810 * Return Value
4811 * 0 on success, non-zero in case of any failure
4813 static int __devinit pmcraid_init_instance(
4814 struct pci_dev *pdev,
4815 struct Scsi_Host *host,
4816 void __iomem *mapped_pci_addr
4819 struct pmcraid_instance *pinstance =
4820 (struct pmcraid_instance *)host->hostdata;
4822 pinstance->host = host;
4823 pinstance->pdev = pdev;
4825 /* Initialize register addresses */
4826 pinstance->mapped_dma_addr = mapped_pci_addr;
4828 /* Initialize chip-specific details */
4830 struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
4831 struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
4833 pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
4835 pint_regs->ioa_host_interrupt_reg =
4836 mapped_pci_addr + chip_cfg->ioa_host_intr;
4837 pint_regs->ioa_host_interrupt_clr_reg =
4838 mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
4839 pint_regs->host_ioa_interrupt_reg =
4840 mapped_pci_addr + chip_cfg->host_ioa_intr;
4841 pint_regs->host_ioa_interrupt_clr_reg =
4842 mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
4844 /* Current version of firmware exposes interrupt mask set
4845 * and mask clr registers through memory mapped bar0.
4847 pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
4848 pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
4849 pint_regs->ioa_host_interrupt_mask_reg =
4850 mapped_pci_addr + chip_cfg->ioa_host_mask;
4851 pint_regs->ioa_host_interrupt_mask_clr_reg =
4852 mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
4853 pint_regs->global_interrupt_mask_reg =
4854 mapped_pci_addr + chip_cfg->global_intr_mask;
4857 pinstance->ioa_reset_attempts = 0;
4858 init_waitqueue_head(&pinstance->reset_wait_q);
4860 atomic_set(&pinstance->outstanding_cmds, 0);
4861 atomic_set(&pinstance->expose_resources, 0);
4863 INIT_LIST_HEAD(&pinstance->free_res_q);
4864 INIT_LIST_HEAD(&pinstance->used_res_q);
4865 INIT_LIST_HEAD(&pinstance->free_cmd_pool);
4866 INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
4868 spin_lock_init(&pinstance->free_pool_lock);
4869 spin_lock_init(&pinstance->pending_pool_lock);
4870 spin_lock_init(&pinstance->resource_lock);
4871 mutex_init(&pinstance->aen_queue_lock);
4873 /* Work-queue (Shared) for deferred processing error handling */
4874 INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
4876 /* Initialize the default log_level */
4877 pinstance->current_log_level = pmcraid_log_level;
4879 /* Setup variables required for reset engine */
4880 pinstance->ioa_state = IOA_STATE_UNKNOWN;
4881 pinstance->reset_cmd = NULL;
4882 return 0;
4886 * pmcraid_release_buffers - release per-adapter buffers allocated
4888 * @pinstance: pointer to adapter soft state
4890 * Return Value
4891 * none
4893 static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
4895 pmcraid_release_config_buffers(pinstance);
4896 pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
4897 pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
4898 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4903 * pmcraid_shutdown - shutdown adapter controller.
4904 * @pdev: pci device struct
4906 * Issues an adapter shutdown to the card waits for its completion
4908 * Return value
4909 * none
4911 static void pmcraid_shutdown(struct pci_dev *pdev)
4913 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
4914 pmcraid_reset_bringdown(pinstance);
4919 * pmcraid_get_minor - returns unused minor number from minor number bitmap
4921 static unsigned short pmcraid_get_minor(void)
4923 int minor;
4925 minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
4926 __set_bit(minor, pmcraid_minor);
4927 return minor;
4931 * pmcraid_release_minor - releases given minor back to minor number bitmap
4933 static void pmcraid_release_minor(unsigned short minor)
4935 __clear_bit(minor, pmcraid_minor);
4939 * pmcraid_setup_chrdev - allocates a minor number and registers a char device
4941 * @pinstance: pointer to adapter instance for which to register device
4943 * Return value
4944 * 0 in case of success, otherwise non-zero
4946 static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
4948 int minor;
4949 int error;
4951 minor = pmcraid_get_minor();
4952 cdev_init(&pinstance->cdev, &pmcraid_fops);
4953 pinstance->cdev.owner = THIS_MODULE;
4955 error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
4957 if (error)
4958 pmcraid_release_minor(minor);
4959 else
4960 device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
4961 NULL, "pmcsas%u", minor);
4962 return error;
4966 * pmcraid_release_chrdev - unregisters per-adapter management interface
4968 * @pinstance: pointer to adapter instance structure
4970 * Return value
4971 * none
4973 static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
4975 pmcraid_release_minor(MINOR(pinstance->cdev.dev));
4976 device_destroy(pmcraid_class,
4977 MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
4978 cdev_del(&pinstance->cdev);
4982 * pmcraid_remove - IOA hot plug remove entry point
4983 * @pdev: pci device struct
4985 * Return value
4986 * none
4988 static void __devexit pmcraid_remove(struct pci_dev *pdev)
4990 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
4992 /* remove the management interface (/dev file) for this device */
4993 pmcraid_release_chrdev(pinstance);
4995 /* remove host template from scsi midlayer */
4996 scsi_remove_host(pinstance->host);
4998 /* block requests from mid-layer */
4999 scsi_block_requests(pinstance->host);
5001 /* initiate shutdown adapter */
5002 pmcraid_shutdown(pdev);
5004 pmcraid_disable_interrupts(pinstance, ~0);
5005 flush_scheduled_work();
5007 pmcraid_kill_tasklets(pinstance);
5008 pmcraid_unregister_interrupt_handler(pinstance);
5009 pmcraid_release_buffers(pinstance);
5010 iounmap(pinstance->mapped_dma_addr);
5011 pci_release_regions(pdev);
5012 scsi_host_put(pinstance->host);
5013 pci_disable_device(pdev);
5015 return;
5018 #ifdef CONFIG_PM
5020 * pmcraid_suspend - driver suspend entry point for power management
5021 * @pdev: PCI device structure
5022 * @state: PCI power state to suspend routine
5024 * Return Value - 0 always
5026 static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
5028 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5030 pmcraid_shutdown(pdev);
5031 pmcraid_disable_interrupts(pinstance, ~0);
5032 pmcraid_kill_tasklets(pinstance);
5033 pci_set_drvdata(pinstance->pdev, pinstance);
5034 pmcraid_unregister_interrupt_handler(pinstance);
5035 pci_save_state(pdev);
5036 pci_disable_device(pdev);
5037 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5039 return 0;
5043 * pmcraid_resume - driver resume entry point PCI power management
5044 * @pdev: PCI device structure
5046 * Return Value - 0 in case of success. Error code in case of any failure
5048 static int pmcraid_resume(struct pci_dev *pdev)
5050 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5051 struct Scsi_Host *host = pinstance->host;
5052 int rc;
5053 int hrrqs;
5055 pci_set_power_state(pdev, PCI_D0);
5056 pci_enable_wake(pdev, PCI_D0, 0);
5057 pci_restore_state(pdev);
5059 rc = pci_enable_device(pdev);
5061 if (rc) {
5062 dev_err(&pdev->dev, "resume: Enable device failed\n");
5063 return rc;
5066 pci_set_master(pdev);
5068 if ((sizeof(dma_addr_t) == 4) ||
5069 pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5070 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5072 if (rc == 0)
5073 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5075 if (rc != 0) {
5076 dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
5077 goto disable_device;
5080 atomic_set(&pinstance->outstanding_cmds, 0);
5081 hrrqs = pinstance->num_hrrq;
5082 rc = pmcraid_register_interrupt_handler(pinstance);
5084 if (rc) {
5085 dev_err(&pdev->dev,
5086 "resume: couldn't register interrupt handlers\n");
5087 rc = -ENODEV;
5088 goto release_host;
5091 pmcraid_init_tasklets(pinstance);
5092 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5094 /* Start with hard reset sequence which brings up IOA to operational
5095 * state as well as completes the reset sequence.
5097 pinstance->ioa_hard_reset = 1;
5099 /* Start IOA firmware initialization and bring card to Operational
5100 * state.
5102 if (pmcraid_reset_bringup(pinstance)) {
5103 dev_err(&pdev->dev, "couldn't initialize IOA \n");
5104 rc = -ENODEV;
5105 goto release_tasklets;
5108 return 0;
5110 release_tasklets:
5111 pmcraid_kill_tasklets(pinstance);
5112 pmcraid_unregister_interrupt_handler(pinstance);
5114 release_host:
5115 scsi_host_put(host);
5117 disable_device:
5118 pci_disable_device(pdev);
5120 return rc;
5123 #else
5125 #define pmcraid_suspend NULL
5126 #define pmcraid_resume NULL
5128 #endif /* CONFIG_PM */
5131 * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
5132 * completion of the ioa reset
5133 * @cmd: pointer to reset command block
5135 static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
5137 struct pmcraid_instance *pinstance = cmd->drv_inst;
5138 unsigned long flags;
5140 spin_lock_irqsave(pinstance->host->host_lock, flags);
5141 pmcraid_ioa_reset(cmd);
5142 spin_unlock_irqrestore(pinstance->host->host_lock, flags);
5143 scsi_unblock_requests(pinstance->host);
5144 schedule_work(&pinstance->worker_q);
5148 * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
5150 * @cmd: pointer to pmcraid_cmd structure
5152 * Return Value
5153 * 0 for success or non-zero for failure cases
5155 static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
5157 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5158 void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
5160 pmcraid_reinit_cmdblk(cmd);
5162 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5163 ioarcb->request_type = REQ_TYPE_IOACMD;
5164 ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
5165 ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
5167 /* If this was called as part of resource table reinitialization due to
5168 * lost CCN, it is enough to return the command block back to free pool
5169 * as part of set_supported_devs completion function.
5171 if (cmd->drv_inst->reinit_cfg_table) {
5172 cmd->drv_inst->reinit_cfg_table = 0;
5173 cmd->release = 1;
5174 cmd_done = pmcraid_reinit_cfgtable_done;
5177 /* we will be done with the reset sequence after set supported devices,
5178 * setup the done function to return the command block back to free
5179 * pool
5181 pmcraid_send_cmd(cmd,
5182 cmd_done,
5183 PMCRAID_SET_SUP_DEV_TIMEOUT,
5184 pmcraid_timeout_handler);
5185 return;
5189 * pmcraid_init_res_table - Initialize the resource table
5190 * @cmd: pointer to pmcraid command struct
5192 * This function looks through the existing resource table, comparing
5193 * it with the config table. This function will take care of old/new
5194 * devices and schedule adding/removing them from the mid-layer
5195 * as appropriate.
5197 * Return value
5198 * None
5200 static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
5202 struct pmcraid_instance *pinstance = cmd->drv_inst;
5203 struct pmcraid_resource_entry *res, *temp;
5204 struct pmcraid_config_table_entry *cfgte;
5205 unsigned long lock_flags;
5206 int found, rc, i;
5207 LIST_HEAD(old_res);
5209 if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
5210 pmcraid_err("IOA requires microcode download\n");
5212 /* resource list is protected by pinstance->resource_lock.
5213 * init_res_table can be called from probe (user-thread) or runtime
5214 * reset (timer/tasklet)
5216 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
5218 list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
5219 list_move_tail(&res->queue, &old_res);
5221 for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
5222 cfgte = &pinstance->cfg_table->entries[i];
5224 if (!pmcraid_expose_resource(cfgte))
5225 continue;
5227 found = 0;
5229 /* If this entry was already detected and initialized */
5230 list_for_each_entry_safe(res, temp, &old_res, queue) {
5232 rc = memcmp(&res->cfg_entry.resource_address,
5233 &cfgte->resource_address,
5234 sizeof(cfgte->resource_address));
5235 if (!rc) {
5236 list_move_tail(&res->queue,
5237 &pinstance->used_res_q);
5238 found = 1;
5239 break;
5243 /* If this is new entry, initialize it and add it the queue */
5244 if (!found) {
5246 if (list_empty(&pinstance->free_res_q)) {
5247 pmcraid_err("Too many devices attached\n");
5248 break;
5251 found = 1;
5252 res = list_entry(pinstance->free_res_q.next,
5253 struct pmcraid_resource_entry, queue);
5255 res->scsi_dev = NULL;
5256 res->change_detected = RES_CHANGE_ADD;
5257 res->reset_progress = 0;
5258 list_move_tail(&res->queue, &pinstance->used_res_q);
5261 /* copy new configuration table entry details into driver
5262 * maintained resource entry
5264 if (found) {
5265 memcpy(&res->cfg_entry, cfgte,
5266 sizeof(struct pmcraid_config_table_entry));
5267 pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
5268 res->cfg_entry.resource_type,
5269 res->cfg_entry.unique_flags1,
5270 le32_to_cpu(res->cfg_entry.resource_address));
5274 /* Detect any deleted entries, mark them for deletion from mid-layer */
5275 list_for_each_entry_safe(res, temp, &old_res, queue) {
5277 if (res->scsi_dev) {
5278 res->change_detected = RES_CHANGE_DEL;
5279 res->cfg_entry.resource_handle =
5280 PMCRAID_INVALID_RES_HANDLE;
5281 list_move_tail(&res->queue, &pinstance->used_res_q);
5282 } else {
5283 list_move_tail(&res->queue, &pinstance->free_res_q);
5287 /* release the resource list lock */
5288 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
5289 pmcraid_set_supported_devs(cmd);
5293 * pmcraid_querycfg - Send a Query IOA Config to the adapter.
5294 * @cmd: pointer pmcraid_cmd struct
5296 * This function sends a Query IOA Configuration command to the adapter to
5297 * retrieve the IOA configuration table.
5299 * Return value:
5300 * none
5302 static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
5304 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5305 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
5306 struct pmcraid_instance *pinstance = cmd->drv_inst;
5307 int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
5309 ioarcb->request_type = REQ_TYPE_IOACMD;
5310 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5312 ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
5314 /* firmware requires 4-byte length field, specified in B.E format */
5315 memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
5317 /* Since entire config table can be described by single IOADL, it can
5318 * be part of IOARCB itself
5320 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
5321 offsetof(struct pmcraid_ioarcb,
5322 add_data.u.ioadl[0]));
5323 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
5324 ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
5326 ioarcb->request_flags0 |= NO_LINK_DESCS;
5327 ioarcb->data_transfer_length =
5328 cpu_to_le32(sizeof(struct pmcraid_config_table));
5330 ioadl = &(ioarcb->add_data.u.ioadl[0]);
5331 ioadl->flags = IOADL_FLAGS_LAST_DESC;
5332 ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
5333 ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
5335 pmcraid_send_cmd(cmd, pmcraid_init_res_table,
5336 PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
5341 * pmcraid_probe - PCI probe entry pointer for PMC MaxRaid controller driver
5342 * @pdev: pointer to pci device structure
5343 * @dev_id: pointer to device ids structure
5345 * Return Value
5346 * returns 0 if the device is claimed and successfully configured.
5347 * returns non-zero error code in case of any failure
5349 static int __devinit pmcraid_probe(
5350 struct pci_dev *pdev,
5351 const struct pci_device_id *dev_id
5354 struct pmcraid_instance *pinstance;
5355 struct Scsi_Host *host;
5356 void __iomem *mapped_pci_addr;
5357 int rc = PCIBIOS_SUCCESSFUL;
5359 if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
5360 pmcraid_err
5361 ("maximum number(%d) of supported adapters reached\n",
5362 atomic_read(&pmcraid_adapter_count));
5363 return -ENOMEM;
5366 atomic_inc(&pmcraid_adapter_count);
5367 rc = pci_enable_device(pdev);
5369 if (rc) {
5370 dev_err(&pdev->dev, "Cannot enable adapter\n");
5371 atomic_dec(&pmcraid_adapter_count);
5372 return rc;
5375 dev_info(&pdev->dev,
5376 "Found new IOA(%x:%x), Total IOA count: %d\n",
5377 pdev->vendor, pdev->device,
5378 atomic_read(&pmcraid_adapter_count));
5380 rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
5382 if (rc < 0) {
5383 dev_err(&pdev->dev,
5384 "Couldn't register memory range of registers\n");
5385 goto out_disable_device;
5388 mapped_pci_addr = pci_iomap(pdev, 0, 0);
5390 if (!mapped_pci_addr) {
5391 dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
5392 rc = -ENOMEM;
5393 goto out_release_regions;
5396 pci_set_master(pdev);
5398 /* Firmware requires the system bus address of IOARCB to be within
5399 * 32-bit addressable range though it has 64-bit IOARRIN register.
5400 * However, firmware supports 64-bit streaming DMA buffers, whereas
5401 * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
5402 * returns memory within 4GB (if not, change this logic), coherent
5403 * buffers are within firmware acceptible address ranges.
5405 if ((sizeof(dma_addr_t) == 4) ||
5406 pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5407 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5409 /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
5410 * bit mask for pci_alloc_consistent to return addresses within 4GB
5412 if (rc == 0)
5413 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5415 if (rc != 0) {
5416 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
5417 goto cleanup_nomem;
5420 host = scsi_host_alloc(&pmcraid_host_template,
5421 sizeof(struct pmcraid_instance));
5423 if (!host) {
5424 dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
5425 rc = -ENOMEM;
5426 goto cleanup_nomem;
5429 host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
5430 host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
5431 host->unique_id = host->host_no;
5432 host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
5433 host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
5435 /* zero out entire instance structure */
5436 pinstance = (struct pmcraid_instance *)host->hostdata;
5437 memset(pinstance, 0, sizeof(*pinstance));
5439 pinstance->chip_cfg =
5440 (struct pmcraid_chip_details *)(dev_id->driver_data);
5442 rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
5444 if (rc < 0) {
5445 dev_err(&pdev->dev, "failed to initialize adapter instance\n");
5446 goto out_scsi_host_put;
5449 pci_set_drvdata(pdev, pinstance);
5451 /* Save PCI config-space for use following the reset */
5452 rc = pci_save_state(pinstance->pdev);
5454 if (rc != 0) {
5455 dev_err(&pdev->dev, "Failed to save PCI config space\n");
5456 goto out_scsi_host_put;
5459 pmcraid_disable_interrupts(pinstance, ~0);
5461 rc = pmcraid_register_interrupt_handler(pinstance);
5463 if (rc) {
5464 dev_err(&pdev->dev, "couldn't register interrupt handler\n");
5465 goto out_scsi_host_put;
5468 pmcraid_init_tasklets(pinstance);
5470 /* allocate verious buffers used by LLD.*/
5471 rc = pmcraid_init_buffers(pinstance);
5473 if (rc) {
5474 pmcraid_err("couldn't allocate memory blocks\n");
5475 goto out_unregister_isr;
5478 /* check the reset type required */
5479 pmcraid_reset_type(pinstance);
5481 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5483 /* Start IOA firmware initialization and bring card to Operational
5484 * state.
5486 pmcraid_info("starting IOA initialization sequence\n");
5487 if (pmcraid_reset_bringup(pinstance)) {
5488 dev_err(&pdev->dev, "couldn't initialize IOA \n");
5489 rc = 1;
5490 goto out_release_bufs;
5493 /* Add adapter instance into mid-layer list */
5494 rc = scsi_add_host(pinstance->host, &pdev->dev);
5495 if (rc != 0) {
5496 pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
5497 goto out_release_bufs;
5500 scsi_scan_host(pinstance->host);
5502 rc = pmcraid_setup_chrdev(pinstance);
5504 if (rc != 0) {
5505 pmcraid_err("couldn't create mgmt interface, error: %x\n",
5506 rc);
5507 goto out_remove_host;
5510 /* Schedule worker thread to handle CCN and take care of adding and
5511 * removing devices to OS
5513 atomic_set(&pinstance->expose_resources, 1);
5514 schedule_work(&pinstance->worker_q);
5515 return rc;
5517 out_remove_host:
5518 scsi_remove_host(host);
5520 out_release_bufs:
5521 pmcraid_release_buffers(pinstance);
5523 out_unregister_isr:
5524 pmcraid_kill_tasklets(pinstance);
5525 pmcraid_unregister_interrupt_handler(pinstance);
5527 out_scsi_host_put:
5528 scsi_host_put(host);
5530 cleanup_nomem:
5531 iounmap(mapped_pci_addr);
5533 out_release_regions:
5534 pci_release_regions(pdev);
5536 out_disable_device:
5537 atomic_dec(&pmcraid_adapter_count);
5538 pci_set_drvdata(pdev, NULL);
5539 pci_disable_device(pdev);
5540 return -ENODEV;
5544 * PCI driver structure of pcmraid driver
5546 static struct pci_driver pmcraid_driver = {
5547 .name = PMCRAID_DRIVER_NAME,
5548 .id_table = pmcraid_pci_table,
5549 .probe = pmcraid_probe,
5550 .remove = pmcraid_remove,
5551 .suspend = pmcraid_suspend,
5552 .resume = pmcraid_resume,
5553 .shutdown = pmcraid_shutdown
5557 * pmcraid_init - module load entry point
5559 static int __init pmcraid_init(void)
5561 dev_t dev;
5562 int error;
5564 pmcraid_info("%s Device Driver version: %s %s\n",
5565 PMCRAID_DRIVER_NAME,
5566 PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
5568 error = alloc_chrdev_region(&dev, 0,
5569 PMCRAID_MAX_ADAPTERS,
5570 PMCRAID_DEVFILE);
5572 if (error) {
5573 pmcraid_err("failed to get a major number for adapters\n");
5574 goto out_init;
5577 pmcraid_major = MAJOR(dev);
5578 pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
5580 if (IS_ERR(pmcraid_class)) {
5581 error = PTR_ERR(pmcraid_class);
5582 pmcraid_err("failed to register with with sysfs, error = %x\n",
5583 error);
5584 goto out_unreg_chrdev;
5587 error = pmcraid_netlink_init();
5589 if (error)
5590 goto out_unreg_chrdev;
5592 error = pci_register_driver(&pmcraid_driver);
5594 if (error == 0)
5595 goto out_init;
5597 pmcraid_err("failed to register pmcraid driver, error = %x\n",
5598 error);
5599 class_destroy(pmcraid_class);
5600 pmcraid_netlink_release();
5602 out_unreg_chrdev:
5603 unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
5605 out_init:
5606 return error;
5610 * pmcraid_exit - module unload entry point
5612 static void __exit pmcraid_exit(void)
5614 pmcraid_netlink_release();
5615 class_destroy(pmcraid_class);
5616 unregister_chrdev_region(MKDEV(pmcraid_major, 0),
5617 PMCRAID_MAX_ADAPTERS);
5618 pci_unregister_driver(&pmcraid_driver);
5621 module_init(pmcraid_init);
5622 module_exit(pmcraid_exit);