ath9k: remove unneeded calculation of minimal calibration power
[linux-2.6/next.git] / drivers / scsi / pmcraid.c
blob53aefffbaead2027c09d2da7925fe3c0afecb607
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 /* buffer length can't be negetive */
3755 if (hdr->buffer_length < 0) {
3756 pmcraid_err("ioctl: invalid buffer length specified\n");
3757 return -EINVAL;
3760 /* check for appropriate buffer access */
3761 if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
3762 access = VERIFY_WRITE;
3764 rc = access_ok(access,
3765 (arg + sizeof(struct pmcraid_ioctl_header)),
3766 hdr->buffer_length);
3767 if (!rc) {
3768 pmcraid_err("access failed for user buffer of size %d\n",
3769 hdr->buffer_length);
3770 return -EFAULT;
3773 return 0;
3777 * pmcraid_ioctl - char node ioctl entry point
3779 static long pmcraid_chr_ioctl(
3780 struct file *filep,
3781 unsigned int cmd,
3782 unsigned long arg
3785 struct pmcraid_instance *pinstance = NULL;
3786 struct pmcraid_ioctl_header *hdr = NULL;
3787 int retval = -ENOTTY;
3789 hdr = kmalloc(GFP_KERNEL, sizeof(struct pmcraid_ioctl_header));
3791 if (!hdr) {
3792 pmcraid_err("faile to allocate memory for ioctl header\n");
3793 return -ENOMEM;
3796 retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
3798 if (retval) {
3799 pmcraid_info("chr_ioctl: header check failed\n");
3800 kfree(hdr);
3801 return retval;
3804 pinstance = (struct pmcraid_instance *)filep->private_data;
3806 if (!pinstance) {
3807 pmcraid_info("adapter instance is not found\n");
3808 kfree(hdr);
3809 return -ENOTTY;
3812 switch (_IOC_TYPE(cmd)) {
3814 case PMCRAID_PASSTHROUGH_IOCTL:
3815 /* If ioctl code is to download microcode, we need to block
3816 * mid-layer requests.
3818 if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
3819 scsi_block_requests(pinstance->host);
3821 retval = pmcraid_ioctl_passthrough(pinstance,
3822 cmd,
3823 hdr->buffer_length,
3824 arg);
3826 if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
3827 scsi_unblock_requests(pinstance->host);
3828 break;
3830 case PMCRAID_DRIVER_IOCTL:
3831 arg += sizeof(struct pmcraid_ioctl_header);
3832 retval = pmcraid_ioctl_driver(pinstance,
3833 cmd,
3834 hdr->buffer_length,
3835 (void __user *)arg);
3836 break;
3838 default:
3839 retval = -ENOTTY;
3840 break;
3843 kfree(hdr);
3845 return retval;
3849 * File operations structure for management interface
3851 static const struct file_operations pmcraid_fops = {
3852 .owner = THIS_MODULE,
3853 .open = pmcraid_chr_open,
3854 .release = pmcraid_chr_release,
3855 .fasync = pmcraid_chr_fasync,
3856 .unlocked_ioctl = pmcraid_chr_ioctl,
3857 #ifdef CONFIG_COMPAT
3858 .compat_ioctl = pmcraid_chr_ioctl,
3859 #endif
3866 * pmcraid_show_log_level - Display adapter's error logging level
3867 * @dev: class device struct
3868 * @buf: buffer
3870 * Return value:
3871 * number of bytes printed to buffer
3873 static ssize_t pmcraid_show_log_level(
3874 struct device *dev,
3875 struct device_attribute *attr,
3876 char *buf)
3878 struct Scsi_Host *shost = class_to_shost(dev);
3879 struct pmcraid_instance *pinstance =
3880 (struct pmcraid_instance *)shost->hostdata;
3881 return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
3885 * pmcraid_store_log_level - Change the adapter's error logging level
3886 * @dev: class device struct
3887 * @buf: buffer
3888 * @count: not used
3890 * Return value:
3891 * number of bytes printed to buffer
3893 static ssize_t pmcraid_store_log_level(
3894 struct device *dev,
3895 struct device_attribute *attr,
3896 const char *buf,
3897 size_t count
3900 struct Scsi_Host *shost;
3901 struct pmcraid_instance *pinstance;
3902 unsigned long val;
3904 if (strict_strtoul(buf, 10, &val))
3905 return -EINVAL;
3906 /* log-level should be from 0 to 2 */
3907 if (val > 2)
3908 return -EINVAL;
3910 shost = class_to_shost(dev);
3911 pinstance = (struct pmcraid_instance *)shost->hostdata;
3912 pinstance->current_log_level = val;
3914 return strlen(buf);
3917 static struct device_attribute pmcraid_log_level_attr = {
3918 .attr = {
3919 .name = "log_level",
3920 .mode = S_IRUGO | S_IWUSR,
3922 .show = pmcraid_show_log_level,
3923 .store = pmcraid_store_log_level,
3927 * pmcraid_show_drv_version - Display driver version
3928 * @dev: class device struct
3929 * @buf: buffer
3931 * Return value:
3932 * number of bytes printed to buffer
3934 static ssize_t pmcraid_show_drv_version(
3935 struct device *dev,
3936 struct device_attribute *attr,
3937 char *buf
3940 return snprintf(buf, PAGE_SIZE, "version: %s, build date: %s\n",
3941 PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
3944 static struct device_attribute pmcraid_driver_version_attr = {
3945 .attr = {
3946 .name = "drv_version",
3947 .mode = S_IRUGO,
3949 .show = pmcraid_show_drv_version,
3953 * pmcraid_show_io_adapter_id - Display driver assigned adapter id
3954 * @dev: class device struct
3955 * @buf: buffer
3957 * Return value:
3958 * number of bytes printed to buffer
3960 static ssize_t pmcraid_show_adapter_id(
3961 struct device *dev,
3962 struct device_attribute *attr,
3963 char *buf
3966 struct Scsi_Host *shost = class_to_shost(dev);
3967 struct pmcraid_instance *pinstance =
3968 (struct pmcraid_instance *)shost->hostdata;
3969 u32 adapter_id = (pinstance->pdev->bus->number << 8) |
3970 pinstance->pdev->devfn;
3971 u32 aen_group = pmcraid_event_family.id;
3973 return snprintf(buf, PAGE_SIZE,
3974 "adapter id: %d\nminor: %d\naen group: %d\n",
3975 adapter_id, MINOR(pinstance->cdev.dev), aen_group);
3978 static struct device_attribute pmcraid_adapter_id_attr = {
3979 .attr = {
3980 .name = "adapter_id",
3981 .mode = S_IRUGO | S_IWUSR,
3983 .show = pmcraid_show_adapter_id,
3986 static struct device_attribute *pmcraid_host_attrs[] = {
3987 &pmcraid_log_level_attr,
3988 &pmcraid_driver_version_attr,
3989 &pmcraid_adapter_id_attr,
3990 NULL,
3994 /* host template structure for pmcraid driver */
3995 static struct scsi_host_template pmcraid_host_template = {
3996 .module = THIS_MODULE,
3997 .name = PMCRAID_DRIVER_NAME,
3998 .queuecommand = pmcraid_queuecommand,
3999 .eh_abort_handler = pmcraid_eh_abort_handler,
4000 .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
4001 .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
4002 .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
4003 .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
4005 .slave_alloc = pmcraid_slave_alloc,
4006 .slave_configure = pmcraid_slave_configure,
4007 .slave_destroy = pmcraid_slave_destroy,
4008 .change_queue_depth = pmcraid_change_queue_depth,
4009 .change_queue_type = pmcraid_change_queue_type,
4010 .can_queue = PMCRAID_MAX_IO_CMD,
4011 .this_id = -1,
4012 .sg_tablesize = PMCRAID_MAX_IOADLS,
4013 .max_sectors = PMCRAID_IOA_MAX_SECTORS,
4014 .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
4015 .use_clustering = ENABLE_CLUSTERING,
4016 .shost_attrs = pmcraid_host_attrs,
4017 .proc_name = PMCRAID_DRIVER_NAME
4021 * pmcraid_isr_common - Common interrupt handler routine
4023 * @pinstance: pointer to adapter instance
4024 * @intrs: active interrupts (contents of ioa_host_interrupt register)
4025 * @hrrq_id: Host RRQ index
4027 * Return Value
4028 * none
4030 static void pmcraid_isr_common(
4031 struct pmcraid_instance *pinstance,
4032 u32 intrs,
4033 int hrrq_id
4036 u32 intrs_clear =
4037 (intrs & INTRS_CRITICAL_OP_IN_PROGRESS) ? intrs
4038 : INTRS_HRRQ_VALID;
4039 iowrite32(intrs_clear,
4040 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4041 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
4043 /* hrrq valid bit was set, schedule tasklet to handle the response */
4044 if (intrs_clear == INTRS_HRRQ_VALID)
4045 tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
4049 * pmcraid_isr - implements interrupt handling routine
4051 * @irq: interrupt vector number
4052 * @dev_id: pointer hrrq_vector
4054 * Return Value
4055 * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4057 static irqreturn_t pmcraid_isr(int irq, void *dev_id)
4059 struct pmcraid_isr_param *hrrq_vector;
4060 struct pmcraid_instance *pinstance;
4061 unsigned long lock_flags;
4062 u32 intrs;
4064 /* In case of legacy interrupt mode where interrupts are shared across
4065 * isrs, it may be possible that the current interrupt is not from IOA
4067 if (!dev_id) {
4068 printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
4069 return IRQ_NONE;
4072 hrrq_vector = (struct pmcraid_isr_param *)dev_id;
4073 pinstance = hrrq_vector->drv_inst;
4075 /* Acquire the lock (currently host_lock) while processing interrupts.
4076 * This interval is small as most of the response processing is done by
4077 * tasklet without the lock.
4079 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
4080 intrs = pmcraid_read_interrupts(pinstance);
4082 if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0)) {
4083 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4084 return IRQ_NONE;
4087 /* Any error interrupts including unit_check, initiate IOA reset.
4088 * In case of unit check indicate to reset_sequence that IOA unit
4089 * checked and prepare for a dump during reset sequence
4091 if (intrs & PMCRAID_ERROR_INTERRUPTS) {
4093 if (intrs & INTRS_IOA_UNIT_CHECK)
4094 pinstance->ioa_unit_check = 1;
4096 iowrite32(intrs,
4097 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4098 pmcraid_err("ISR: error interrupts: %x initiating reset\n",
4099 intrs);
4100 intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
4101 pmcraid_initiate_reset(pinstance);
4102 } else {
4103 pmcraid_isr_common(pinstance, intrs, hrrq_vector->hrrq_id);
4106 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4108 return IRQ_HANDLED;
4113 * pmcraid_worker_function - worker thread function
4115 * @workp: pointer to struct work queue
4117 * Return Value
4118 * None
4121 static void pmcraid_worker_function(struct work_struct *workp)
4123 struct pmcraid_instance *pinstance;
4124 struct pmcraid_resource_entry *res;
4125 struct pmcraid_resource_entry *temp;
4126 struct scsi_device *sdev;
4127 unsigned long lock_flags;
4128 unsigned long host_lock_flags;
4129 u8 bus, target, lun;
4131 pinstance = container_of(workp, struct pmcraid_instance, worker_q);
4132 /* add resources only after host is added into system */
4133 if (!atomic_read(&pinstance->expose_resources))
4134 return;
4136 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
4137 list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
4139 if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
4140 sdev = res->scsi_dev;
4142 /* host_lock must be held before calling
4143 * scsi_device_get
4145 spin_lock_irqsave(pinstance->host->host_lock,
4146 host_lock_flags);
4147 if (!scsi_device_get(sdev)) {
4148 spin_unlock_irqrestore(
4149 pinstance->host->host_lock,
4150 host_lock_flags);
4151 pmcraid_info("deleting %x from midlayer\n",
4152 res->cfg_entry.resource_address);
4153 list_move_tail(&res->queue,
4154 &pinstance->free_res_q);
4155 spin_unlock_irqrestore(
4156 &pinstance->resource_lock,
4157 lock_flags);
4158 scsi_remove_device(sdev);
4159 scsi_device_put(sdev);
4160 spin_lock_irqsave(&pinstance->resource_lock,
4161 lock_flags);
4162 res->change_detected = 0;
4163 } else {
4164 spin_unlock_irqrestore(
4165 pinstance->host->host_lock,
4166 host_lock_flags);
4171 list_for_each_entry(res, &pinstance->used_res_q, queue) {
4173 if (res->change_detected == RES_CHANGE_ADD) {
4175 if (!pmcraid_expose_resource(&res->cfg_entry))
4176 continue;
4178 if (RES_IS_VSET(res->cfg_entry)) {
4179 bus = PMCRAID_VSET_BUS_ID;
4180 target = res->cfg_entry.unique_flags1;
4181 lun = PMCRAID_VSET_LUN_ID;
4182 } else {
4183 bus = PMCRAID_PHYS_BUS_ID;
4184 target =
4185 RES_TARGET(
4186 res->cfg_entry.resource_address);
4187 lun = RES_LUN(res->cfg_entry.resource_address);
4190 res->change_detected = 0;
4191 spin_unlock_irqrestore(&pinstance->resource_lock,
4192 lock_flags);
4193 scsi_add_device(pinstance->host, bus, target, lun);
4194 spin_lock_irqsave(&pinstance->resource_lock,
4195 lock_flags);
4199 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
4203 * pmcraid_tasklet_function - Tasklet function
4205 * @instance: pointer to msix param structure
4207 * Return Value
4208 * None
4210 void pmcraid_tasklet_function(unsigned long instance)
4212 struct pmcraid_isr_param *hrrq_vector;
4213 struct pmcraid_instance *pinstance;
4214 unsigned long hrrq_lock_flags;
4215 unsigned long pending_lock_flags;
4216 unsigned long host_lock_flags;
4217 spinlock_t *lockp; /* hrrq buffer lock */
4218 int id;
4219 u32 intrs;
4220 __le32 resp;
4222 hrrq_vector = (struct pmcraid_isr_param *)instance;
4223 pinstance = hrrq_vector->drv_inst;
4224 id = hrrq_vector->hrrq_id;
4225 lockp = &(pinstance->hrrq_lock[id]);
4226 intrs = pmcraid_read_interrupts(pinstance);
4228 /* If interrupts was as part of the ioa initialization, clear and mask
4229 * it. Delete the timer and wakeup the reset engine to proceed with
4230 * reset sequence
4232 if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
4233 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
4234 pinstance->int_regs.ioa_host_interrupt_mask_reg);
4235 iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
4236 pinstance->int_regs.ioa_host_interrupt_clr_reg);
4238 if (pinstance->reset_cmd != NULL) {
4239 del_timer(&pinstance->reset_cmd->timer);
4240 spin_lock_irqsave(pinstance->host->host_lock,
4241 host_lock_flags);
4242 pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
4243 spin_unlock_irqrestore(pinstance->host->host_lock,
4244 host_lock_flags);
4246 return;
4249 /* loop through each of the commands responded by IOA. Each HRRQ buf is
4250 * protected by its own lock. Traversals must be done within this lock
4251 * as there may be multiple tasklets running on multiple CPUs. Note
4252 * that the lock is held just for picking up the response handle and
4253 * manipulating hrrq_curr/toggle_bit values.
4255 spin_lock_irqsave(lockp, hrrq_lock_flags);
4257 resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4259 while ((resp & HRRQ_TOGGLE_BIT) ==
4260 pinstance->host_toggle_bit[id]) {
4262 int cmd_index = resp >> 2;
4263 struct pmcraid_cmd *cmd = NULL;
4265 if (cmd_index < PMCRAID_MAX_CMD) {
4266 cmd = pinstance->cmd_list[cmd_index];
4267 } else {
4268 /* In case of invalid response handle, initiate IOA
4269 * reset sequence.
4271 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4273 pmcraid_err("Invalid response %d initiating reset\n",
4274 cmd_index);
4276 spin_lock_irqsave(pinstance->host->host_lock,
4277 host_lock_flags);
4278 pmcraid_initiate_reset(pinstance);
4279 spin_unlock_irqrestore(pinstance->host->host_lock,
4280 host_lock_flags);
4282 spin_lock_irqsave(lockp, hrrq_lock_flags);
4283 break;
4286 if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
4287 pinstance->hrrq_curr[id]++;
4288 } else {
4289 pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
4290 pinstance->host_toggle_bit[id] ^= 1u;
4293 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4295 spin_lock_irqsave(&pinstance->pending_pool_lock,
4296 pending_lock_flags);
4297 list_del(&cmd->free_list);
4298 spin_unlock_irqrestore(&pinstance->pending_pool_lock,
4299 pending_lock_flags);
4300 del_timer(&cmd->timer);
4301 atomic_dec(&pinstance->outstanding_cmds);
4303 if (cmd->cmd_done == pmcraid_ioa_reset) {
4304 spin_lock_irqsave(pinstance->host->host_lock,
4305 host_lock_flags);
4306 cmd->cmd_done(cmd);
4307 spin_unlock_irqrestore(pinstance->host->host_lock,
4308 host_lock_flags);
4309 } else if (cmd->cmd_done != NULL) {
4310 cmd->cmd_done(cmd);
4312 /* loop over until we are done with all responses */
4313 spin_lock_irqsave(lockp, hrrq_lock_flags);
4314 resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4317 spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4321 * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
4322 * @pinstance: pointer to adapter instance structure
4324 * This routine un-registers registered interrupt handler and
4325 * also frees irqs/vectors.
4327 * Retun Value
4328 * None
4330 static
4331 void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
4333 free_irq(pinstance->pdev->irq, &(pinstance->hrrq_vector[0]));
4337 * pmcraid_register_interrupt_handler - registers interrupt handler
4338 * @pinstance: pointer to per-adapter instance structure
4340 * Return Value
4341 * 0 on success, non-zero error code otherwise.
4343 static int
4344 pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
4346 struct pci_dev *pdev = pinstance->pdev;
4348 pinstance->hrrq_vector[0].hrrq_id = 0;
4349 pinstance->hrrq_vector[0].drv_inst = pinstance;
4350 pinstance->hrrq_vector[0].vector = 0;
4351 pinstance->num_hrrq = 1;
4352 return request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
4353 PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
4357 * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
4358 * @pinstance: per adapter instance structure pointer
4359 * @max_index: number of buffer blocks to release
4361 * Return Value
4362 * None
4364 static void
4365 pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
4367 int i;
4368 for (i = 0; i < max_index; i++) {
4369 kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
4370 pinstance->cmd_list[i] = NULL;
4372 kmem_cache_destroy(pinstance->cmd_cachep);
4373 pinstance->cmd_cachep = NULL;
4377 * pmcraid_release_control_blocks - releases buffers alloced for control blocks
4378 * @pinstance: pointer to per adapter instance structure
4379 * @max_index: number of buffers (from 0 onwards) to release
4381 * This function assumes that the command blocks for which control blocks are
4382 * linked are not released.
4384 * Return Value
4385 * None
4387 static void
4388 pmcraid_release_control_blocks(
4389 struct pmcraid_instance *pinstance,
4390 int max_index
4393 int i;
4395 if (pinstance->control_pool == NULL)
4396 return;
4398 for (i = 0; i < max_index; i++) {
4399 pci_pool_free(pinstance->control_pool,
4400 pinstance->cmd_list[i]->ioa_cb,
4401 pinstance->cmd_list[i]->ioa_cb_bus_addr);
4402 pinstance->cmd_list[i]->ioa_cb = NULL;
4403 pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
4405 pci_pool_destroy(pinstance->control_pool);
4406 pinstance->control_pool = NULL;
4410 * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
4411 * @pinstance - pointer to per adapter instance structure
4413 * Allocates memory for command blocks using kernel slab allocator.
4415 * Return Value
4416 * 0 in case of success; -ENOMEM in case of failure
4418 static int __devinit
4419 pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
4421 int i;
4423 sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
4424 pinstance->host->unique_id);
4427 pinstance->cmd_cachep = kmem_cache_create(
4428 pinstance->cmd_pool_name,
4429 sizeof(struct pmcraid_cmd), 0,
4430 SLAB_HWCACHE_ALIGN, NULL);
4431 if (!pinstance->cmd_cachep)
4432 return -ENOMEM;
4434 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4435 pinstance->cmd_list[i] =
4436 kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
4437 if (!pinstance->cmd_list[i]) {
4438 pmcraid_release_cmd_blocks(pinstance, i);
4439 return -ENOMEM;
4442 return 0;
4446 * pmcraid_allocate_control_blocks - allocates memory control blocks
4447 * @pinstance : pointer to per adapter instance structure
4449 * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
4450 * and IOASAs. This is called after command blocks are already allocated.
4452 * Return Value
4453 * 0 in case it can allocate all control blocks, otherwise -ENOMEM
4455 static int __devinit
4456 pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
4458 int i;
4460 sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
4461 pinstance->host->unique_id);
4463 pinstance->control_pool =
4464 pci_pool_create(pinstance->ctl_pool_name,
4465 pinstance->pdev,
4466 sizeof(struct pmcraid_control_block),
4467 PMCRAID_IOARCB_ALIGNMENT, 0);
4469 if (!pinstance->control_pool)
4470 return -ENOMEM;
4472 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4473 pinstance->cmd_list[i]->ioa_cb =
4474 pci_pool_alloc(
4475 pinstance->control_pool,
4476 GFP_KERNEL,
4477 &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
4479 if (!pinstance->cmd_list[i]->ioa_cb) {
4480 pmcraid_release_control_blocks(pinstance, i);
4481 return -ENOMEM;
4483 memset(pinstance->cmd_list[i]->ioa_cb, 0,
4484 sizeof(struct pmcraid_control_block));
4486 return 0;
4490 * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
4491 * @pinstance: pointer to per adapter instance structure
4492 * @maxindex: size of hrrq buffer pointer array
4494 * Return Value
4495 * None
4497 static void
4498 pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
4500 int i;
4501 for (i = 0; i < maxindex; i++) {
4503 pci_free_consistent(pinstance->pdev,
4504 HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
4505 pinstance->hrrq_start[i],
4506 pinstance->hrrq_start_bus_addr[i]);
4508 /* reset pointers and toggle bit to zeros */
4509 pinstance->hrrq_start[i] = NULL;
4510 pinstance->hrrq_start_bus_addr[i] = 0;
4511 pinstance->host_toggle_bit[i] = 0;
4516 * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
4517 * @pinstance: pointer to per adapter instance structure
4519 * Return value
4520 * 0 hrrq buffers are allocated, -ENOMEM otherwise.
4522 static int __devinit
4523 pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
4525 int i;
4526 int buf_count = PMCRAID_MAX_CMD / pinstance->num_hrrq;
4528 for (i = 0; i < pinstance->num_hrrq; i++) {
4529 int buffer_size = HRRQ_ENTRY_SIZE * buf_count;
4531 pinstance->hrrq_start[i] =
4532 pci_alloc_consistent(
4533 pinstance->pdev,
4534 buffer_size,
4535 &(pinstance->hrrq_start_bus_addr[i]));
4537 if (pinstance->hrrq_start[i] == 0) {
4538 pmcraid_err("could not allocate host rrq: %d\n", i);
4539 pmcraid_release_host_rrqs(pinstance, i);
4540 return -ENOMEM;
4543 memset(pinstance->hrrq_start[i], 0, buffer_size);
4544 pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4545 pinstance->hrrq_end[i] =
4546 pinstance->hrrq_start[i] + buf_count - 1;
4547 pinstance->host_toggle_bit[i] = 1;
4548 spin_lock_init(&pinstance->hrrq_lock[i]);
4550 return 0;
4554 * pmcraid_release_hcams - release HCAM buffers
4556 * @pinstance: pointer to per adapter instance structure
4558 * Return value
4559 * none
4561 static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
4563 if (pinstance->ccn.msg != NULL) {
4564 pci_free_consistent(pinstance->pdev,
4565 PMCRAID_AEN_HDR_SIZE +
4566 sizeof(struct pmcraid_hcam_ccn),
4567 pinstance->ccn.msg,
4568 pinstance->ccn.baddr);
4570 pinstance->ccn.msg = NULL;
4571 pinstance->ccn.hcam = NULL;
4572 pinstance->ccn.baddr = 0;
4575 if (pinstance->ldn.msg != NULL) {
4576 pci_free_consistent(pinstance->pdev,
4577 PMCRAID_AEN_HDR_SIZE +
4578 sizeof(struct pmcraid_hcam_ldn),
4579 pinstance->ldn.msg,
4580 pinstance->ldn.baddr);
4582 pinstance->ldn.msg = NULL;
4583 pinstance->ldn.hcam = NULL;
4584 pinstance->ldn.baddr = 0;
4589 * pmcraid_allocate_hcams - allocates HCAM buffers
4590 * @pinstance : pointer to per adapter instance structure
4592 * Return Value:
4593 * 0 in case of successful allocation, non-zero otherwise
4595 static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
4597 pinstance->ccn.msg = pci_alloc_consistent(
4598 pinstance->pdev,
4599 PMCRAID_AEN_HDR_SIZE +
4600 sizeof(struct pmcraid_hcam_ccn),
4601 &(pinstance->ccn.baddr));
4603 pinstance->ldn.msg = pci_alloc_consistent(
4604 pinstance->pdev,
4605 PMCRAID_AEN_HDR_SIZE +
4606 sizeof(struct pmcraid_hcam_ldn),
4607 &(pinstance->ldn.baddr));
4609 if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
4610 pmcraid_release_hcams(pinstance);
4611 } else {
4612 pinstance->ccn.hcam =
4613 (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
4614 pinstance->ldn.hcam =
4615 (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
4617 atomic_set(&pinstance->ccn.ignore, 0);
4618 atomic_set(&pinstance->ldn.ignore, 0);
4621 return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
4625 * pmcraid_release_config_buffers - release config.table buffers
4626 * @pinstance: pointer to per adapter instance structure
4628 * Return Value
4629 * none
4631 static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
4633 if (pinstance->cfg_table != NULL &&
4634 pinstance->cfg_table_bus_addr != 0) {
4635 pci_free_consistent(pinstance->pdev,
4636 sizeof(struct pmcraid_config_table),
4637 pinstance->cfg_table,
4638 pinstance->cfg_table_bus_addr);
4639 pinstance->cfg_table = NULL;
4640 pinstance->cfg_table_bus_addr = 0;
4643 if (pinstance->res_entries != NULL) {
4644 int i;
4646 for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4647 list_del(&pinstance->res_entries[i].queue);
4648 kfree(pinstance->res_entries);
4649 pinstance->res_entries = NULL;
4652 pmcraid_release_hcams(pinstance);
4656 * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
4657 * @pinstance : pointer to per adapter instance structure
4659 * Return Value
4660 * 0 for successful allocation, -ENOMEM for any failure
4662 static int __devinit
4663 pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
4665 int i;
4667 pinstance->res_entries =
4668 kzalloc(sizeof(struct pmcraid_resource_entry) *
4669 PMCRAID_MAX_RESOURCES, GFP_KERNEL);
4671 if (NULL == pinstance->res_entries) {
4672 pmcraid_err("failed to allocate memory for resource table\n");
4673 return -ENOMEM;
4676 for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4677 list_add_tail(&pinstance->res_entries[i].queue,
4678 &pinstance->free_res_q);
4680 pinstance->cfg_table =
4681 pci_alloc_consistent(pinstance->pdev,
4682 sizeof(struct pmcraid_config_table),
4683 &pinstance->cfg_table_bus_addr);
4685 if (NULL == pinstance->cfg_table) {
4686 pmcraid_err("couldn't alloc DMA memory for config table\n");
4687 pmcraid_release_config_buffers(pinstance);
4688 return -ENOMEM;
4691 if (pmcraid_allocate_hcams(pinstance)) {
4692 pmcraid_err("could not alloc DMA memory for HCAMS\n");
4693 pmcraid_release_config_buffers(pinstance);
4694 return -ENOMEM;
4697 return 0;
4701 * pmcraid_init_tasklets - registers tasklets for response handling
4703 * @pinstance: pointer adapter instance structure
4705 * Return value
4706 * none
4708 static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
4710 int i;
4711 for (i = 0; i < pinstance->num_hrrq; i++)
4712 tasklet_init(&pinstance->isr_tasklet[i],
4713 pmcraid_tasklet_function,
4714 (unsigned long)&pinstance->hrrq_vector[i]);
4718 * pmcraid_kill_tasklets - destroys tasklets registered for response handling
4720 * @pinstance: pointer to adapter instance structure
4722 * Return value
4723 * none
4725 static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
4727 int i;
4728 for (i = 0; i < pinstance->num_hrrq; i++)
4729 tasklet_kill(&pinstance->isr_tasklet[i]);
4733 * pmcraid_init_buffers - allocates memory and initializes various structures
4734 * @pinstance: pointer to per adapter instance structure
4736 * This routine pre-allocates memory based on the type of block as below:
4737 * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
4738 * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
4739 * config-table entries : DMAable memory using pci_alloc_consistent
4740 * HostRRQs : DMAable memory, using pci_alloc_consistent
4742 * Return Value
4743 * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
4745 static int __devinit pmcraid_init_buffers(struct pmcraid_instance *pinstance)
4747 int i;
4749 if (pmcraid_allocate_host_rrqs(pinstance)) {
4750 pmcraid_err("couldn't allocate memory for %d host rrqs\n",
4751 pinstance->num_hrrq);
4752 return -ENOMEM;
4755 if (pmcraid_allocate_config_buffers(pinstance)) {
4756 pmcraid_err("couldn't allocate memory for config buffers\n");
4757 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4758 return -ENOMEM;
4761 if (pmcraid_allocate_cmd_blocks(pinstance)) {
4762 pmcraid_err("couldn't allocate memory for cmd blocks \n");
4763 pmcraid_release_config_buffers(pinstance);
4764 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4765 return -ENOMEM;
4768 if (pmcraid_allocate_control_blocks(pinstance)) {
4769 pmcraid_err("couldn't allocate memory control blocks \n");
4770 pmcraid_release_config_buffers(pinstance);
4771 pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
4772 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4773 return -ENOMEM;
4776 /* Initialize all the command blocks and add them to free pool. No
4777 * need to lock (free_pool_lock) as this is done in initialization
4778 * itself
4780 for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4781 struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
4782 pmcraid_init_cmdblk(cmdp, i);
4783 cmdp->drv_inst = pinstance;
4784 list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
4787 return 0;
4791 * pmcraid_reinit_buffers - resets various buffer pointers
4792 * @pinstance: pointer to adapter instance
4793 * Return value
4794 * none
4796 static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
4798 int i;
4799 int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
4801 for (i = 0; i < pinstance->num_hrrq; i++) {
4802 memset(pinstance->hrrq_start[i], 0, buffer_size);
4803 pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4804 pinstance->hrrq_end[i] =
4805 pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
4806 pinstance->host_toggle_bit[i] = 1;
4811 * pmcraid_init_instance - initialize per instance data structure
4812 * @pdev: pointer to pci device structure
4813 * @host: pointer to Scsi_Host structure
4814 * @mapped_pci_addr: memory mapped IOA configuration registers
4816 * Return Value
4817 * 0 on success, non-zero in case of any failure
4819 static int __devinit pmcraid_init_instance(
4820 struct pci_dev *pdev,
4821 struct Scsi_Host *host,
4822 void __iomem *mapped_pci_addr
4825 struct pmcraid_instance *pinstance =
4826 (struct pmcraid_instance *)host->hostdata;
4828 pinstance->host = host;
4829 pinstance->pdev = pdev;
4831 /* Initialize register addresses */
4832 pinstance->mapped_dma_addr = mapped_pci_addr;
4834 /* Initialize chip-specific details */
4836 struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
4837 struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
4839 pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
4841 pint_regs->ioa_host_interrupt_reg =
4842 mapped_pci_addr + chip_cfg->ioa_host_intr;
4843 pint_regs->ioa_host_interrupt_clr_reg =
4844 mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
4845 pint_regs->host_ioa_interrupt_reg =
4846 mapped_pci_addr + chip_cfg->host_ioa_intr;
4847 pint_regs->host_ioa_interrupt_clr_reg =
4848 mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
4850 /* Current version of firmware exposes interrupt mask set
4851 * and mask clr registers through memory mapped bar0.
4853 pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
4854 pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
4855 pint_regs->ioa_host_interrupt_mask_reg =
4856 mapped_pci_addr + chip_cfg->ioa_host_mask;
4857 pint_regs->ioa_host_interrupt_mask_clr_reg =
4858 mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
4859 pint_regs->global_interrupt_mask_reg =
4860 mapped_pci_addr + chip_cfg->global_intr_mask;
4863 pinstance->ioa_reset_attempts = 0;
4864 init_waitqueue_head(&pinstance->reset_wait_q);
4866 atomic_set(&pinstance->outstanding_cmds, 0);
4867 atomic_set(&pinstance->expose_resources, 0);
4869 INIT_LIST_HEAD(&pinstance->free_res_q);
4870 INIT_LIST_HEAD(&pinstance->used_res_q);
4871 INIT_LIST_HEAD(&pinstance->free_cmd_pool);
4872 INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
4874 spin_lock_init(&pinstance->free_pool_lock);
4875 spin_lock_init(&pinstance->pending_pool_lock);
4876 spin_lock_init(&pinstance->resource_lock);
4877 mutex_init(&pinstance->aen_queue_lock);
4879 /* Work-queue (Shared) for deferred processing error handling */
4880 INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
4882 /* Initialize the default log_level */
4883 pinstance->current_log_level = pmcraid_log_level;
4885 /* Setup variables required for reset engine */
4886 pinstance->ioa_state = IOA_STATE_UNKNOWN;
4887 pinstance->reset_cmd = NULL;
4888 return 0;
4892 * pmcraid_release_buffers - release per-adapter buffers allocated
4894 * @pinstance: pointer to adapter soft state
4896 * Return Value
4897 * none
4899 static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
4901 pmcraid_release_config_buffers(pinstance);
4902 pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
4903 pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
4904 pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
4909 * pmcraid_shutdown - shutdown adapter controller.
4910 * @pdev: pci device struct
4912 * Issues an adapter shutdown to the card waits for its completion
4914 * Return value
4915 * none
4917 static void pmcraid_shutdown(struct pci_dev *pdev)
4919 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
4920 pmcraid_reset_bringdown(pinstance);
4925 * pmcraid_get_minor - returns unused minor number from minor number bitmap
4927 static unsigned short pmcraid_get_minor(void)
4929 int minor;
4931 minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
4932 __set_bit(minor, pmcraid_minor);
4933 return minor;
4937 * pmcraid_release_minor - releases given minor back to minor number bitmap
4939 static void pmcraid_release_minor(unsigned short minor)
4941 __clear_bit(minor, pmcraid_minor);
4945 * pmcraid_setup_chrdev - allocates a minor number and registers a char device
4947 * @pinstance: pointer to adapter instance for which to register device
4949 * Return value
4950 * 0 in case of success, otherwise non-zero
4952 static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
4954 int minor;
4955 int error;
4957 minor = pmcraid_get_minor();
4958 cdev_init(&pinstance->cdev, &pmcraid_fops);
4959 pinstance->cdev.owner = THIS_MODULE;
4961 error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
4963 if (error)
4964 pmcraid_release_minor(minor);
4965 else
4966 device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
4967 NULL, "pmcsas%u", minor);
4968 return error;
4972 * pmcraid_release_chrdev - unregisters per-adapter management interface
4974 * @pinstance: pointer to adapter instance structure
4976 * Return value
4977 * none
4979 static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
4981 pmcraid_release_minor(MINOR(pinstance->cdev.dev));
4982 device_destroy(pmcraid_class,
4983 MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
4984 cdev_del(&pinstance->cdev);
4988 * pmcraid_remove - IOA hot plug remove entry point
4989 * @pdev: pci device struct
4991 * Return value
4992 * none
4994 static void __devexit pmcraid_remove(struct pci_dev *pdev)
4996 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
4998 /* remove the management interface (/dev file) for this device */
4999 pmcraid_release_chrdev(pinstance);
5001 /* remove host template from scsi midlayer */
5002 scsi_remove_host(pinstance->host);
5004 /* block requests from mid-layer */
5005 scsi_block_requests(pinstance->host);
5007 /* initiate shutdown adapter */
5008 pmcraid_shutdown(pdev);
5010 pmcraid_disable_interrupts(pinstance, ~0);
5011 flush_scheduled_work();
5013 pmcraid_kill_tasklets(pinstance);
5014 pmcraid_unregister_interrupt_handler(pinstance);
5015 pmcraid_release_buffers(pinstance);
5016 iounmap(pinstance->mapped_dma_addr);
5017 pci_release_regions(pdev);
5018 scsi_host_put(pinstance->host);
5019 pci_disable_device(pdev);
5021 return;
5024 #ifdef CONFIG_PM
5026 * pmcraid_suspend - driver suspend entry point for power management
5027 * @pdev: PCI device structure
5028 * @state: PCI power state to suspend routine
5030 * Return Value - 0 always
5032 static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
5034 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5036 pmcraid_shutdown(pdev);
5037 pmcraid_disable_interrupts(pinstance, ~0);
5038 pmcraid_kill_tasklets(pinstance);
5039 pci_set_drvdata(pinstance->pdev, pinstance);
5040 pmcraid_unregister_interrupt_handler(pinstance);
5041 pci_save_state(pdev);
5042 pci_disable_device(pdev);
5043 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5045 return 0;
5049 * pmcraid_resume - driver resume entry point PCI power management
5050 * @pdev: PCI device structure
5052 * Return Value - 0 in case of success. Error code in case of any failure
5054 static int pmcraid_resume(struct pci_dev *pdev)
5056 struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5057 struct Scsi_Host *host = pinstance->host;
5058 int rc;
5059 int hrrqs;
5061 pci_set_power_state(pdev, PCI_D0);
5062 pci_enable_wake(pdev, PCI_D0, 0);
5063 pci_restore_state(pdev);
5065 rc = pci_enable_device(pdev);
5067 if (rc) {
5068 dev_err(&pdev->dev, "resume: Enable device failed\n");
5069 return rc;
5072 pci_set_master(pdev);
5074 if ((sizeof(dma_addr_t) == 4) ||
5075 pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5076 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5078 if (rc == 0)
5079 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5081 if (rc != 0) {
5082 dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
5083 goto disable_device;
5086 atomic_set(&pinstance->outstanding_cmds, 0);
5087 hrrqs = pinstance->num_hrrq;
5088 rc = pmcraid_register_interrupt_handler(pinstance);
5090 if (rc) {
5091 dev_err(&pdev->dev,
5092 "resume: couldn't register interrupt handlers\n");
5093 rc = -ENODEV;
5094 goto release_host;
5097 pmcraid_init_tasklets(pinstance);
5098 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5100 /* Start with hard reset sequence which brings up IOA to operational
5101 * state as well as completes the reset sequence.
5103 pinstance->ioa_hard_reset = 1;
5105 /* Start IOA firmware initialization and bring card to Operational
5106 * state.
5108 if (pmcraid_reset_bringup(pinstance)) {
5109 dev_err(&pdev->dev, "couldn't initialize IOA \n");
5110 rc = -ENODEV;
5111 goto release_tasklets;
5114 return 0;
5116 release_tasklets:
5117 pmcraid_kill_tasklets(pinstance);
5118 pmcraid_unregister_interrupt_handler(pinstance);
5120 release_host:
5121 scsi_host_put(host);
5123 disable_device:
5124 pci_disable_device(pdev);
5126 return rc;
5129 #else
5131 #define pmcraid_suspend NULL
5132 #define pmcraid_resume NULL
5134 #endif /* CONFIG_PM */
5137 * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
5138 * completion of the ioa reset
5139 * @cmd: pointer to reset command block
5141 static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
5143 struct pmcraid_instance *pinstance = cmd->drv_inst;
5144 unsigned long flags;
5146 spin_lock_irqsave(pinstance->host->host_lock, flags);
5147 pmcraid_ioa_reset(cmd);
5148 spin_unlock_irqrestore(pinstance->host->host_lock, flags);
5149 scsi_unblock_requests(pinstance->host);
5150 schedule_work(&pinstance->worker_q);
5154 * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
5156 * @cmd: pointer to pmcraid_cmd structure
5158 * Return Value
5159 * 0 for success or non-zero for failure cases
5161 static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
5163 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5164 void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
5166 pmcraid_reinit_cmdblk(cmd);
5168 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5169 ioarcb->request_type = REQ_TYPE_IOACMD;
5170 ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
5171 ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
5173 /* If this was called as part of resource table reinitialization due to
5174 * lost CCN, it is enough to return the command block back to free pool
5175 * as part of set_supported_devs completion function.
5177 if (cmd->drv_inst->reinit_cfg_table) {
5178 cmd->drv_inst->reinit_cfg_table = 0;
5179 cmd->release = 1;
5180 cmd_done = pmcraid_reinit_cfgtable_done;
5183 /* we will be done with the reset sequence after set supported devices,
5184 * setup the done function to return the command block back to free
5185 * pool
5187 pmcraid_send_cmd(cmd,
5188 cmd_done,
5189 PMCRAID_SET_SUP_DEV_TIMEOUT,
5190 pmcraid_timeout_handler);
5191 return;
5195 * pmcraid_init_res_table - Initialize the resource table
5196 * @cmd: pointer to pmcraid command struct
5198 * This function looks through the existing resource table, comparing
5199 * it with the config table. This function will take care of old/new
5200 * devices and schedule adding/removing them from the mid-layer
5201 * as appropriate.
5203 * Return value
5204 * None
5206 static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
5208 struct pmcraid_instance *pinstance = cmd->drv_inst;
5209 struct pmcraid_resource_entry *res, *temp;
5210 struct pmcraid_config_table_entry *cfgte;
5211 unsigned long lock_flags;
5212 int found, rc, i;
5213 LIST_HEAD(old_res);
5215 if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
5216 pmcraid_err("IOA requires microcode download\n");
5218 /* resource list is protected by pinstance->resource_lock.
5219 * init_res_table can be called from probe (user-thread) or runtime
5220 * reset (timer/tasklet)
5222 spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
5224 list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
5225 list_move_tail(&res->queue, &old_res);
5227 for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
5228 cfgte = &pinstance->cfg_table->entries[i];
5230 if (!pmcraid_expose_resource(cfgte))
5231 continue;
5233 found = 0;
5235 /* If this entry was already detected and initialized */
5236 list_for_each_entry_safe(res, temp, &old_res, queue) {
5238 rc = memcmp(&res->cfg_entry.resource_address,
5239 &cfgte->resource_address,
5240 sizeof(cfgte->resource_address));
5241 if (!rc) {
5242 list_move_tail(&res->queue,
5243 &pinstance->used_res_q);
5244 found = 1;
5245 break;
5249 /* If this is new entry, initialize it and add it the queue */
5250 if (!found) {
5252 if (list_empty(&pinstance->free_res_q)) {
5253 pmcraid_err("Too many devices attached\n");
5254 break;
5257 found = 1;
5258 res = list_entry(pinstance->free_res_q.next,
5259 struct pmcraid_resource_entry, queue);
5261 res->scsi_dev = NULL;
5262 res->change_detected = RES_CHANGE_ADD;
5263 res->reset_progress = 0;
5264 list_move_tail(&res->queue, &pinstance->used_res_q);
5267 /* copy new configuration table entry details into driver
5268 * maintained resource entry
5270 if (found) {
5271 memcpy(&res->cfg_entry, cfgte,
5272 sizeof(struct pmcraid_config_table_entry));
5273 pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
5274 res->cfg_entry.resource_type,
5275 res->cfg_entry.unique_flags1,
5276 le32_to_cpu(res->cfg_entry.resource_address));
5280 /* Detect any deleted entries, mark them for deletion from mid-layer */
5281 list_for_each_entry_safe(res, temp, &old_res, queue) {
5283 if (res->scsi_dev) {
5284 res->change_detected = RES_CHANGE_DEL;
5285 res->cfg_entry.resource_handle =
5286 PMCRAID_INVALID_RES_HANDLE;
5287 list_move_tail(&res->queue, &pinstance->used_res_q);
5288 } else {
5289 list_move_tail(&res->queue, &pinstance->free_res_q);
5293 /* release the resource list lock */
5294 spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
5295 pmcraid_set_supported_devs(cmd);
5299 * pmcraid_querycfg - Send a Query IOA Config to the adapter.
5300 * @cmd: pointer pmcraid_cmd struct
5302 * This function sends a Query IOA Configuration command to the adapter to
5303 * retrieve the IOA configuration table.
5305 * Return value:
5306 * none
5308 static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
5310 struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5311 struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
5312 struct pmcraid_instance *pinstance = cmd->drv_inst;
5313 int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
5315 ioarcb->request_type = REQ_TYPE_IOACMD;
5316 ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5318 ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
5320 /* firmware requires 4-byte length field, specified in B.E format */
5321 memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
5323 /* Since entire config table can be described by single IOADL, it can
5324 * be part of IOARCB itself
5326 ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
5327 offsetof(struct pmcraid_ioarcb,
5328 add_data.u.ioadl[0]));
5329 ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
5330 ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
5332 ioarcb->request_flags0 |= NO_LINK_DESCS;
5333 ioarcb->data_transfer_length =
5334 cpu_to_le32(sizeof(struct pmcraid_config_table));
5336 ioadl = &(ioarcb->add_data.u.ioadl[0]);
5337 ioadl->flags = IOADL_FLAGS_LAST_DESC;
5338 ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
5339 ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
5341 pmcraid_send_cmd(cmd, pmcraid_init_res_table,
5342 PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
5347 * pmcraid_probe - PCI probe entry pointer for PMC MaxRaid controller driver
5348 * @pdev: pointer to pci device structure
5349 * @dev_id: pointer to device ids structure
5351 * Return Value
5352 * returns 0 if the device is claimed and successfully configured.
5353 * returns non-zero error code in case of any failure
5355 static int __devinit pmcraid_probe(
5356 struct pci_dev *pdev,
5357 const struct pci_device_id *dev_id
5360 struct pmcraid_instance *pinstance;
5361 struct Scsi_Host *host;
5362 void __iomem *mapped_pci_addr;
5363 int rc = PCIBIOS_SUCCESSFUL;
5365 if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
5366 pmcraid_err
5367 ("maximum number(%d) of supported adapters reached\n",
5368 atomic_read(&pmcraid_adapter_count));
5369 return -ENOMEM;
5372 atomic_inc(&pmcraid_adapter_count);
5373 rc = pci_enable_device(pdev);
5375 if (rc) {
5376 dev_err(&pdev->dev, "Cannot enable adapter\n");
5377 atomic_dec(&pmcraid_adapter_count);
5378 return rc;
5381 dev_info(&pdev->dev,
5382 "Found new IOA(%x:%x), Total IOA count: %d\n",
5383 pdev->vendor, pdev->device,
5384 atomic_read(&pmcraid_adapter_count));
5386 rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
5388 if (rc < 0) {
5389 dev_err(&pdev->dev,
5390 "Couldn't register memory range of registers\n");
5391 goto out_disable_device;
5394 mapped_pci_addr = pci_iomap(pdev, 0, 0);
5396 if (!mapped_pci_addr) {
5397 dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
5398 rc = -ENOMEM;
5399 goto out_release_regions;
5402 pci_set_master(pdev);
5404 /* Firmware requires the system bus address of IOARCB to be within
5405 * 32-bit addressable range though it has 64-bit IOARRIN register.
5406 * However, firmware supports 64-bit streaming DMA buffers, whereas
5407 * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
5408 * returns memory within 4GB (if not, change this logic), coherent
5409 * buffers are within firmware acceptible address ranges.
5411 if ((sizeof(dma_addr_t) == 4) ||
5412 pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5413 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5415 /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
5416 * bit mask for pci_alloc_consistent to return addresses within 4GB
5418 if (rc == 0)
5419 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5421 if (rc != 0) {
5422 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
5423 goto cleanup_nomem;
5426 host = scsi_host_alloc(&pmcraid_host_template,
5427 sizeof(struct pmcraid_instance));
5429 if (!host) {
5430 dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
5431 rc = -ENOMEM;
5432 goto cleanup_nomem;
5435 host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
5436 host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
5437 host->unique_id = host->host_no;
5438 host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
5439 host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
5441 /* zero out entire instance structure */
5442 pinstance = (struct pmcraid_instance *)host->hostdata;
5443 memset(pinstance, 0, sizeof(*pinstance));
5445 pinstance->chip_cfg =
5446 (struct pmcraid_chip_details *)(dev_id->driver_data);
5448 rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
5450 if (rc < 0) {
5451 dev_err(&pdev->dev, "failed to initialize adapter instance\n");
5452 goto out_scsi_host_put;
5455 pci_set_drvdata(pdev, pinstance);
5457 /* Save PCI config-space for use following the reset */
5458 rc = pci_save_state(pinstance->pdev);
5460 if (rc != 0) {
5461 dev_err(&pdev->dev, "Failed to save PCI config space\n");
5462 goto out_scsi_host_put;
5465 pmcraid_disable_interrupts(pinstance, ~0);
5467 rc = pmcraid_register_interrupt_handler(pinstance);
5469 if (rc) {
5470 dev_err(&pdev->dev, "couldn't register interrupt handler\n");
5471 goto out_scsi_host_put;
5474 pmcraid_init_tasklets(pinstance);
5476 /* allocate verious buffers used by LLD.*/
5477 rc = pmcraid_init_buffers(pinstance);
5479 if (rc) {
5480 pmcraid_err("couldn't allocate memory blocks\n");
5481 goto out_unregister_isr;
5484 /* check the reset type required */
5485 pmcraid_reset_type(pinstance);
5487 pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5489 /* Start IOA firmware initialization and bring card to Operational
5490 * state.
5492 pmcraid_info("starting IOA initialization sequence\n");
5493 if (pmcraid_reset_bringup(pinstance)) {
5494 dev_err(&pdev->dev, "couldn't initialize IOA \n");
5495 rc = 1;
5496 goto out_release_bufs;
5499 /* Add adapter instance into mid-layer list */
5500 rc = scsi_add_host(pinstance->host, &pdev->dev);
5501 if (rc != 0) {
5502 pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
5503 goto out_release_bufs;
5506 scsi_scan_host(pinstance->host);
5508 rc = pmcraid_setup_chrdev(pinstance);
5510 if (rc != 0) {
5511 pmcraid_err("couldn't create mgmt interface, error: %x\n",
5512 rc);
5513 goto out_remove_host;
5516 /* Schedule worker thread to handle CCN and take care of adding and
5517 * removing devices to OS
5519 atomic_set(&pinstance->expose_resources, 1);
5520 schedule_work(&pinstance->worker_q);
5521 return rc;
5523 out_remove_host:
5524 scsi_remove_host(host);
5526 out_release_bufs:
5527 pmcraid_release_buffers(pinstance);
5529 out_unregister_isr:
5530 pmcraid_kill_tasklets(pinstance);
5531 pmcraid_unregister_interrupt_handler(pinstance);
5533 out_scsi_host_put:
5534 scsi_host_put(host);
5536 cleanup_nomem:
5537 iounmap(mapped_pci_addr);
5539 out_release_regions:
5540 pci_release_regions(pdev);
5542 out_disable_device:
5543 atomic_dec(&pmcraid_adapter_count);
5544 pci_set_drvdata(pdev, NULL);
5545 pci_disable_device(pdev);
5546 return -ENODEV;
5550 * PCI driver structure of pcmraid driver
5552 static struct pci_driver pmcraid_driver = {
5553 .name = PMCRAID_DRIVER_NAME,
5554 .id_table = pmcraid_pci_table,
5555 .probe = pmcraid_probe,
5556 .remove = pmcraid_remove,
5557 .suspend = pmcraid_suspend,
5558 .resume = pmcraid_resume,
5559 .shutdown = pmcraid_shutdown
5563 * pmcraid_init - module load entry point
5565 static int __init pmcraid_init(void)
5567 dev_t dev;
5568 int error;
5570 pmcraid_info("%s Device Driver version: %s %s\n",
5571 PMCRAID_DRIVER_NAME,
5572 PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
5574 error = alloc_chrdev_region(&dev, 0,
5575 PMCRAID_MAX_ADAPTERS,
5576 PMCRAID_DEVFILE);
5578 if (error) {
5579 pmcraid_err("failed to get a major number for adapters\n");
5580 goto out_init;
5583 pmcraid_major = MAJOR(dev);
5584 pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
5586 if (IS_ERR(pmcraid_class)) {
5587 error = PTR_ERR(pmcraid_class);
5588 pmcraid_err("failed to register with with sysfs, error = %x\n",
5589 error);
5590 goto out_unreg_chrdev;
5593 error = pmcraid_netlink_init();
5595 if (error)
5596 goto out_unreg_chrdev;
5598 error = pci_register_driver(&pmcraid_driver);
5600 if (error == 0)
5601 goto out_init;
5603 pmcraid_err("failed to register pmcraid driver, error = %x\n",
5604 error);
5605 class_destroy(pmcraid_class);
5606 pmcraid_netlink_release();
5608 out_unreg_chrdev:
5609 unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
5611 out_init:
5612 return error;
5616 * pmcraid_exit - module unload entry point
5618 static void __exit pmcraid_exit(void)
5620 pmcraid_netlink_release();
5621 class_destroy(pmcraid_class);
5622 unregister_chrdev_region(MKDEV(pmcraid_major, 0),
5623 PMCRAID_MAX_ADAPTERS);
5624 pci_unregister_driver(&pmcraid_driver);
5627 module_init(pmcraid_init);
5628 module_exit(pmcraid_exit);