xref: /linux/drivers/scsi/pmcraid.c (revision 957e3facd147510f2cf8780e38606f1d707f0e33)
1 /*
2  * pmcraid.c -- driver for PMC Sierra MaxRAID controller adapters
3  *
4  * Written By: Anil Ravindranath<anil_ravindranath@pmc-sierra.com>
5  *             PMC-Sierra Inc
6  *
7  * Copyright (C) 2008, 2009 PMC Sierra Inc
8  *
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.
13  *
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.
18  *
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
23  *
24  */
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/io.h>
43 #include <linux/slab.h>
44 #include <asm/irq.h>
45 #include <asm/processor.h>
46 #include <linux/libata.h>
47 #include <linux/mutex.h>
48 #include <scsi/scsi.h>
49 #include <scsi/scsi_host.h>
50 #include <scsi/scsi_device.h>
51 #include <scsi/scsi_tcq.h>
52 #include <scsi/scsi_eh.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsicam.h>
55 
56 #include "pmcraid.h"
57 
58 /*
59  *   Module configuration parameters
60  */
61 static unsigned int pmcraid_debug_log;
62 static unsigned int pmcraid_disable_aen;
63 static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
64 static unsigned int pmcraid_enable_msix;
65 
66 /*
67  * Data structures to support multiple adapters by the LLD.
68  * pmcraid_adapter_count - count of configured adapters
69  */
70 static atomic_t pmcraid_adapter_count = ATOMIC_INIT(0);
71 
72 /*
73  * Supporting user-level control interface through IOCTL commands.
74  * pmcraid_major - major number to use
75  * pmcraid_minor - minor number(s) to use
76  */
77 static unsigned int pmcraid_major;
78 static struct class *pmcraid_class;
79 DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
80 
81 /*
82  * Module parameters
83  */
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);
88 
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");
94 
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)");
99 
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)");
104 
105 /* chip specific constants for PMC MaxRAID controllers (same for
106  * 0x5220 and 0x8010
107  */
108 static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
109 	{
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_msix_intr = 0x7FC40,
117 	 .ioa_host_mask = 0x7FC28,
118 	 .ioa_host_mask_clr = 0x7FC28,
119 	 .host_ioa_intr = 0x00020,
120 	 .host_ioa_intr_clr = 0x00020,
121 	 .transop_timeout = 300
122 	 }
123 };
124 
125 /*
126  * PCI device ids supported by pmcraid driver
127  */
128 static struct pci_device_id pmcraid_pci_table[] = {
129 	{ PCI_DEVICE(PCI_VENDOR_ID_PMC, PCI_DEVICE_ID_PMC_MAXRAID),
130 	  0, 0, (kernel_ulong_t)&pmcraid_chip_cfg[0]
131 	},
132 	{}
133 };
134 
135 MODULE_DEVICE_TABLE(pci, pmcraid_pci_table);
136 
137 
138 
139 /**
140  * pmcraid_slave_alloc - Prepare for commands to a device
141  * @scsi_dev: scsi device struct
142  *
143  * This function is called by mid-layer prior to sending any command to the new
144  * device. Stores resource entry details of the device in scsi_device struct.
145  * Queuecommand uses the resource handle and other details to fill up IOARCB
146  * while sending commands to the device.
147  *
148  * Return value:
149  *	  0 on success / -ENXIO if device does not exist
150  */
151 static int pmcraid_slave_alloc(struct scsi_device *scsi_dev)
152 {
153 	struct pmcraid_resource_entry *temp, *res = NULL;
154 	struct pmcraid_instance *pinstance;
155 	u8 target, bus, lun;
156 	unsigned long lock_flags;
157 	int rc = -ENXIO;
158 	u16 fw_version;
159 
160 	pinstance = shost_priv(scsi_dev->host);
161 
162 	fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
163 
164 	/* Driver exposes VSET and GSCSI resources only; all other device types
165 	 * are not exposed. Resource list is synchronized using resource lock
166 	 * so any traversal or modifications to the list should be done inside
167 	 * this lock
168 	 */
169 	spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
170 	list_for_each_entry(temp, &pinstance->used_res_q, queue) {
171 
172 		/* do not expose VSETs with order-ids > MAX_VSET_TARGETS */
173 		if (RES_IS_VSET(temp->cfg_entry)) {
174 			if (fw_version <= PMCRAID_FW_VERSION_1)
175 				target = temp->cfg_entry.unique_flags1;
176 			else
177 				target = temp->cfg_entry.array_id & 0xFF;
178 
179 			if (target > PMCRAID_MAX_VSET_TARGETS)
180 				continue;
181 			bus = PMCRAID_VSET_BUS_ID;
182 			lun = 0;
183 		} else if (RES_IS_GSCSI(temp->cfg_entry)) {
184 			target = RES_TARGET(temp->cfg_entry.resource_address);
185 			bus = PMCRAID_PHYS_BUS_ID;
186 			lun = RES_LUN(temp->cfg_entry.resource_address);
187 		} else {
188 			continue;
189 		}
190 
191 		if (bus == scsi_dev->channel &&
192 		    target == scsi_dev->id &&
193 		    lun == scsi_dev->lun) {
194 			res = temp;
195 			break;
196 		}
197 	}
198 
199 	if (res) {
200 		res->scsi_dev = scsi_dev;
201 		scsi_dev->hostdata = res;
202 		res->change_detected = 0;
203 		atomic_set(&res->read_failures, 0);
204 		atomic_set(&res->write_failures, 0);
205 		rc = 0;
206 	}
207 	spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
208 	return rc;
209 }
210 
211 /**
212  * pmcraid_slave_configure - Configures a SCSI device
213  * @scsi_dev: scsi device struct
214  *
215  * This function is executed by SCSI mid layer just after a device is first
216  * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
217  * timeout value (default 30s) will be over-written to a higher value (60s)
218  * and max_sectors value will be over-written to 512. It also sets queue depth
219  * to host->cmd_per_lun value
220  *
221  * Return value:
222  *	  0 on success
223  */
224 static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
225 {
226 	struct pmcraid_resource_entry *res = scsi_dev->hostdata;
227 
228 	if (!res)
229 		return 0;
230 
231 	/* LLD exposes VSETs and Enclosure devices only */
232 	if (RES_IS_GSCSI(res->cfg_entry) &&
233 	    scsi_dev->type != TYPE_ENCLOSURE)
234 		return -ENXIO;
235 
236 	pmcraid_info("configuring %x:%x:%x:%x\n",
237 		     scsi_dev->host->unique_id,
238 		     scsi_dev->channel,
239 		     scsi_dev->id,
240 		     (u8)scsi_dev->lun);
241 
242 	if (RES_IS_GSCSI(res->cfg_entry)) {
243 		scsi_dev->allow_restart = 1;
244 	} else if (RES_IS_VSET(res->cfg_entry)) {
245 		scsi_dev->allow_restart = 1;
246 		blk_queue_rq_timeout(scsi_dev->request_queue,
247 				     PMCRAID_VSET_IO_TIMEOUT);
248 		blk_queue_max_hw_sectors(scsi_dev->request_queue,
249 				      PMCRAID_VSET_MAX_SECTORS);
250 	}
251 
252 	/*
253 	 * We never want to report TCQ support for these types of devices.
254 	 */
255 	if (!RES_IS_GSCSI(res->cfg_entry) && !RES_IS_VSET(res->cfg_entry))
256 		scsi_dev->tagged_supported = 0;
257 
258 	return 0;
259 }
260 
261 /**
262  * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
263  *
264  * @scsi_dev: scsi device struct
265  *
266  * This is called by mid-layer before removing a device. Pointer assignments
267  * done in pmcraid_slave_alloc will be reset to NULL here.
268  *
269  * Return value
270  *   none
271  */
272 static void pmcraid_slave_destroy(struct scsi_device *scsi_dev)
273 {
274 	struct pmcraid_resource_entry *res;
275 
276 	res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
277 
278 	if (res)
279 		res->scsi_dev = NULL;
280 
281 	scsi_dev->hostdata = NULL;
282 }
283 
284 /**
285  * pmcraid_change_queue_depth - Change the device's queue depth
286  * @scsi_dev: scsi device struct
287  * @depth: depth to set
288  *
289  * Return value
290  *	actual depth set
291  */
292 static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth)
293 {
294 	if (depth > PMCRAID_MAX_CMD_PER_LUN)
295 		depth = PMCRAID_MAX_CMD_PER_LUN;
296 	return scsi_change_queue_depth(scsi_dev, depth);
297 }
298 
299 /**
300  * pmcraid_init_cmdblk - initializes a command block
301  *
302  * @cmd: pointer to struct pmcraid_cmd to be initialized
303  * @index: if >=0 first time initialization; otherwise reinitialization
304  *
305  * Return Value
306  *	 None
307  */
308 void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
309 {
310 	struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
311 	dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
312 
313 	if (index >= 0) {
314 		/* first time initialization (called from  probe) */
315 		u32 ioasa_offset =
316 			offsetof(struct pmcraid_control_block, ioasa);
317 
318 		cmd->index = index;
319 		ioarcb->response_handle = cpu_to_le32(index << 2);
320 		ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
321 		ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
322 		ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
323 	} else {
324 		/* re-initialization of various lengths, called once command is
325 		 * processed by IOA
326 		 */
327 		memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
328 		ioarcb->hrrq_id = 0;
329 		ioarcb->request_flags0 = 0;
330 		ioarcb->request_flags1 = 0;
331 		ioarcb->cmd_timeout = 0;
332 		ioarcb->ioarcb_bus_addr &= (~0x1FULL);
333 		ioarcb->ioadl_bus_addr = 0;
334 		ioarcb->ioadl_length = 0;
335 		ioarcb->data_transfer_length = 0;
336 		ioarcb->add_cmd_param_length = 0;
337 		ioarcb->add_cmd_param_offset = 0;
338 		cmd->ioa_cb->ioasa.ioasc = 0;
339 		cmd->ioa_cb->ioasa.residual_data_length = 0;
340 		cmd->time_left = 0;
341 	}
342 
343 	cmd->cmd_done = NULL;
344 	cmd->scsi_cmd = NULL;
345 	cmd->release = 0;
346 	cmd->completion_req = 0;
347 	cmd->sense_buffer = 0;
348 	cmd->sense_buffer_dma = 0;
349 	cmd->dma_handle = 0;
350 	init_timer(&cmd->timer);
351 }
352 
353 /**
354  * pmcraid_reinit_cmdblk - reinitialize a command block
355  *
356  * @cmd: pointer to struct pmcraid_cmd to be reinitialized
357  *
358  * Return Value
359  *	 None
360  */
361 static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
362 {
363 	pmcraid_init_cmdblk(cmd, -1);
364 }
365 
366 /**
367  * pmcraid_get_free_cmd - get a free cmd block from command block pool
368  * @pinstance: adapter instance structure
369  *
370  * Return Value:
371  *	returns pointer to cmd block or NULL if no blocks are available
372  */
373 static struct pmcraid_cmd *pmcraid_get_free_cmd(
374 	struct pmcraid_instance *pinstance
375 )
376 {
377 	struct pmcraid_cmd *cmd = NULL;
378 	unsigned long lock_flags;
379 
380 	/* free cmd block list is protected by free_pool_lock */
381 	spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
382 
383 	if (!list_empty(&pinstance->free_cmd_pool)) {
384 		cmd = list_entry(pinstance->free_cmd_pool.next,
385 				 struct pmcraid_cmd, free_list);
386 		list_del(&cmd->free_list);
387 	}
388 	spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
389 
390 	/* Initialize the command block before giving it the caller */
391 	if (cmd != NULL)
392 		pmcraid_reinit_cmdblk(cmd);
393 	return cmd;
394 }
395 
396 /**
397  * pmcraid_return_cmd - return a completed command block back into free pool
398  * @cmd: pointer to the command block
399  *
400  * Return Value:
401  *	nothing
402  */
403 void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
404 {
405 	struct pmcraid_instance *pinstance = cmd->drv_inst;
406 	unsigned long lock_flags;
407 
408 	spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
409 	list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
410 	spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
411 }
412 
413 /**
414  * pmcraid_read_interrupts -  reads IOA interrupts
415  *
416  * @pinstance: pointer to adapter instance structure
417  *
418  * Return value
419  *	 interrupts read from IOA
420  */
421 static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
422 {
423 	return (pinstance->interrupt_mode) ?
424 		ioread32(pinstance->int_regs.ioa_host_msix_interrupt_reg) :
425 		ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
426 }
427 
428 /**
429  * pmcraid_disable_interrupts - Masks and clears all specified interrupts
430  *
431  * @pinstance: pointer to per adapter instance structure
432  * @intrs: interrupts to disable
433  *
434  * Return Value
435  *	 None
436  */
437 static void pmcraid_disable_interrupts(
438 	struct pmcraid_instance *pinstance,
439 	u32 intrs
440 )
441 {
442 	u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
443 	u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
444 
445 	iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
446 	iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
447 	ioread32(pinstance->int_regs.global_interrupt_mask_reg);
448 
449 	if (!pinstance->interrupt_mode) {
450 		iowrite32(intrs,
451 			pinstance->int_regs.ioa_host_interrupt_mask_reg);
452 		ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
453 	}
454 }
455 
456 /**
457  * pmcraid_enable_interrupts - Enables specified interrupts
458  *
459  * @pinstance: pointer to per adapter instance structure
460  * @intr: interrupts to enable
461  *
462  * Return Value
463  *	 None
464  */
465 static void pmcraid_enable_interrupts(
466 	struct pmcraid_instance *pinstance,
467 	u32 intrs
468 )
469 {
470 	u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
471 	u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
472 
473 	iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
474 
475 	if (!pinstance->interrupt_mode) {
476 		iowrite32(~intrs,
477 			 pinstance->int_regs.ioa_host_interrupt_mask_reg);
478 		ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
479 	}
480 
481 	pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
482 		ioread32(pinstance->int_regs.global_interrupt_mask_reg),
483 		ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
484 }
485 
486 /**
487  * pmcraid_clr_trans_op - clear trans to op interrupt
488  *
489  * @pinstance: pointer to per adapter instance structure
490  *
491  * Return Value
492  *	 None
493  */
494 static void pmcraid_clr_trans_op(
495 	struct pmcraid_instance *pinstance
496 )
497 {
498 	unsigned long lock_flags;
499 
500 	if (!pinstance->interrupt_mode) {
501 		iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
502 			pinstance->int_regs.ioa_host_interrupt_mask_reg);
503 		ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
504 		iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
505 			pinstance->int_regs.ioa_host_interrupt_clr_reg);
506 		ioread32(pinstance->int_regs.ioa_host_interrupt_clr_reg);
507 	}
508 
509 	if (pinstance->reset_cmd != NULL) {
510 		del_timer(&pinstance->reset_cmd->timer);
511 		spin_lock_irqsave(
512 			pinstance->host->host_lock, lock_flags);
513 		pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
514 		spin_unlock_irqrestore(
515 			pinstance->host->host_lock, lock_flags);
516 	}
517 }
518 
519 /**
520  * pmcraid_reset_type - Determine the required reset type
521  * @pinstance: pointer to adapter instance structure
522  *
523  * IOA requires hard reset if any of the following conditions is true.
524  * 1. If HRRQ valid interrupt is not masked
525  * 2. IOA reset alert doorbell is set
526  * 3. If there are any error interrupts
527  */
528 static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
529 {
530 	u32 mask;
531 	u32 intrs;
532 	u32 alerts;
533 
534 	mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
535 	intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
536 	alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
537 
538 	if ((mask & INTRS_HRRQ_VALID) == 0 ||
539 	    (alerts & DOORBELL_IOA_RESET_ALERT) ||
540 	    (intrs & PMCRAID_ERROR_INTERRUPTS)) {
541 		pmcraid_info("IOA requires hard reset\n");
542 		pinstance->ioa_hard_reset = 1;
543 	}
544 
545 	/* If unit check is active, trigger the dump */
546 	if (intrs & INTRS_IOA_UNIT_CHECK)
547 		pinstance->ioa_unit_check = 1;
548 }
549 
550 /**
551  * pmcraid_bist_done - completion function for PCI BIST
552  * @cmd: pointer to reset command
553  * Return Value
554  *	none
555  */
556 
557 static void pmcraid_ioa_reset(struct pmcraid_cmd *);
558 
559 static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
560 {
561 	struct pmcraid_instance *pinstance = cmd->drv_inst;
562 	unsigned long lock_flags;
563 	int rc;
564 	u16 pci_reg;
565 
566 	rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
567 
568 	/* If PCI config space can't be accessed wait for another two secs */
569 	if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
570 	    cmd->time_left > 0) {
571 		pmcraid_info("BIST not complete, waiting another 2 secs\n");
572 		cmd->timer.expires = jiffies + cmd->time_left;
573 		cmd->time_left = 0;
574 		cmd->timer.data = (unsigned long)cmd;
575 		cmd->timer.function =
576 			(void (*)(unsigned long))pmcraid_bist_done;
577 		add_timer(&cmd->timer);
578 	} else {
579 		cmd->time_left = 0;
580 		pmcraid_info("BIST is complete, proceeding with reset\n");
581 		spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
582 		pmcraid_ioa_reset(cmd);
583 		spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
584 	}
585 }
586 
587 /**
588  * pmcraid_start_bist - starts BIST
589  * @cmd: pointer to reset cmd
590  * Return Value
591  *   none
592  */
593 static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
594 {
595 	struct pmcraid_instance *pinstance = cmd->drv_inst;
596 	u32 doorbells, intrs;
597 
598 	/* proceed with bist and wait for 2 seconds */
599 	iowrite32(DOORBELL_IOA_START_BIST,
600 		pinstance->int_regs.host_ioa_interrupt_reg);
601 	doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
602 	intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
603 	pmcraid_info("doorbells after start bist: %x intrs: %x\n",
604 		      doorbells, intrs);
605 
606 	cmd->time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
607 	cmd->timer.data = (unsigned long)cmd;
608 	cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
609 	cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
610 	add_timer(&cmd->timer);
611 }
612 
613 /**
614  * pmcraid_reset_alert_done - completion routine for reset_alert
615  * @cmd: pointer to command block used in reset sequence
616  * Return value
617  *  None
618  */
619 static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
620 {
621 	struct pmcraid_instance *pinstance = cmd->drv_inst;
622 	u32 status = ioread32(pinstance->ioa_status);
623 	unsigned long lock_flags;
624 
625 	/* if the critical operation in progress bit is set or the wait times
626 	 * out, invoke reset engine to proceed with hard reset. If there is
627 	 * some more time to wait, restart the timer
628 	 */
629 	if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
630 	    cmd->time_left <= 0) {
631 		pmcraid_info("critical op is reset proceeding with reset\n");
632 		spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
633 		pmcraid_ioa_reset(cmd);
634 		spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
635 	} else {
636 		pmcraid_info("critical op is not yet reset waiting again\n");
637 		/* restart timer if some more time is available to wait */
638 		cmd->time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
639 		cmd->timer.data = (unsigned long)cmd;
640 		cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
641 		cmd->timer.function =
642 			(void (*)(unsigned long))pmcraid_reset_alert_done;
643 		add_timer(&cmd->timer);
644 	}
645 }
646 
647 /**
648  * pmcraid_reset_alert - alerts IOA for a possible reset
649  * @cmd : command block to be used for reset sequence.
650  *
651  * Return Value
652  *	returns 0 if pci config-space is accessible and RESET_DOORBELL is
653  *	successfully written to IOA. Returns non-zero in case pci_config_space
654  *	is not accessible
655  */
656 static void pmcraid_notify_ioastate(struct pmcraid_instance *, u32);
657 static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
658 {
659 	struct pmcraid_instance *pinstance = cmd->drv_inst;
660 	u32 doorbells;
661 	int rc;
662 	u16 pci_reg;
663 
664 	/* If we are able to access IOA PCI config space, alert IOA that we are
665 	 * going to reset it soon. This enables IOA to preserv persistent error
666 	 * data if any. In case memory space is not accessible, proceed with
667 	 * BIST or slot_reset
668 	 */
669 	rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
670 	if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
671 
672 		/* wait for IOA permission i.e until CRITICAL_OPERATION bit is
673 		 * reset IOA doesn't generate any interrupts when CRITICAL
674 		 * OPERATION bit is reset. A timer is started to wait for this
675 		 * bit to be reset.
676 		 */
677 		cmd->time_left = PMCRAID_RESET_TIMEOUT;
678 		cmd->timer.data = (unsigned long)cmd;
679 		cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
680 		cmd->timer.function =
681 			(void (*)(unsigned long))pmcraid_reset_alert_done;
682 		add_timer(&cmd->timer);
683 
684 		iowrite32(DOORBELL_IOA_RESET_ALERT,
685 			pinstance->int_regs.host_ioa_interrupt_reg);
686 		doorbells =
687 			ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
688 		pmcraid_info("doorbells after reset alert: %x\n", doorbells);
689 	} else {
690 		pmcraid_info("PCI config is not accessible starting BIST\n");
691 		pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
692 		pmcraid_start_bist(cmd);
693 	}
694 }
695 
696 /**
697  * pmcraid_timeout_handler -  Timeout handler for internally generated ops
698  *
699  * @cmd : pointer to command structure, that got timedout
700  *
701  * This function blocks host requests and initiates an adapter reset.
702  *
703  * Return value:
704  *   None
705  */
706 static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
707 {
708 	struct pmcraid_instance *pinstance = cmd->drv_inst;
709 	unsigned long lock_flags;
710 
711 	dev_info(&pinstance->pdev->dev,
712 		"Adapter being reset due to cmd(CDB[0] = %x) timeout\n",
713 		cmd->ioa_cb->ioarcb.cdb[0]);
714 
715 	/* Command timeouts result in hard reset sequence. The command that got
716 	 * timed out may be the one used as part of reset sequence. In this
717 	 * case restart reset sequence using the same command block even if
718 	 * reset is in progress. Otherwise fail this command and get a free
719 	 * command block to restart the reset sequence.
720 	 */
721 	spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
722 	if (!pinstance->ioa_reset_in_progress) {
723 		pinstance->ioa_reset_attempts = 0;
724 		cmd = pmcraid_get_free_cmd(pinstance);
725 
726 		/* If we are out of command blocks, just return here itself.
727 		 * Some other command's timeout handler can do the reset job
728 		 */
729 		if (cmd == NULL) {
730 			spin_unlock_irqrestore(pinstance->host->host_lock,
731 					       lock_flags);
732 			pmcraid_err("no free cmnd block for timeout handler\n");
733 			return;
734 		}
735 
736 		pinstance->reset_cmd = cmd;
737 		pinstance->ioa_reset_in_progress = 1;
738 	} else {
739 		pmcraid_info("reset is already in progress\n");
740 
741 		if (pinstance->reset_cmd != cmd) {
742 			/* This command should have been given to IOA, this
743 			 * command will be completed by fail_outstanding_cmds
744 			 * anyway
745 			 */
746 			pmcraid_err("cmd is pending but reset in progress\n");
747 		}
748 
749 		/* If this command was being used as part of the reset
750 		 * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
751 		 * causes fail_outstanding_commands not to return the command
752 		 * block back to free pool
753 		 */
754 		if (cmd == pinstance->reset_cmd)
755 			cmd->cmd_done = pmcraid_ioa_reset;
756 	}
757 
758 	/* Notify apps of important IOA bringup/bringdown sequences */
759 	if (pinstance->scn.ioa_state != PMC_DEVICE_EVENT_RESET_START &&
760 	    pinstance->scn.ioa_state != PMC_DEVICE_EVENT_SHUTDOWN_START)
761 		pmcraid_notify_ioastate(pinstance,
762 					PMC_DEVICE_EVENT_RESET_START);
763 
764 	pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
765 	scsi_block_requests(pinstance->host);
766 	pmcraid_reset_alert(cmd);
767 	spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
768 }
769 
770 /**
771  * pmcraid_internal_done - completion routine for internally generated cmds
772  *
773  * @cmd: command that got response from IOA
774  *
775  * Return Value:
776  *	 none
777  */
778 static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
779 {
780 	pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
781 		     cmd->ioa_cb->ioarcb.cdb[0],
782 		     le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
783 
784 	/* Some of the internal commands are sent with callers blocking for the
785 	 * response. Same will be indicated as part of cmd->completion_req
786 	 * field. Response path needs to wake up any waiters waiting for cmd
787 	 * completion if this flag is set.
788 	 */
789 	if (cmd->completion_req) {
790 		cmd->completion_req = 0;
791 		complete(&cmd->wait_for_completion);
792 	}
793 
794 	/* most of the internal commands are completed by caller itself, so
795 	 * no need to return the command block back to free pool until we are
796 	 * required to do so (e.g once done with initialization).
797 	 */
798 	if (cmd->release) {
799 		cmd->release = 0;
800 		pmcraid_return_cmd(cmd);
801 	}
802 }
803 
804 /**
805  * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
806  *
807  * @cmd: command that got response from IOA
808  *
809  * This routine is called after driver re-reads configuration table due to a
810  * lost CCN. It returns the command block back to free pool and schedules
811  * worker thread to add/delete devices into the system.
812  *
813  * Return Value:
814  *	 none
815  */
816 static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
817 {
818 	pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
819 		     cmd->ioa_cb->ioarcb.cdb[0],
820 		     le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
821 
822 	if (cmd->release) {
823 		cmd->release = 0;
824 		pmcraid_return_cmd(cmd);
825 	}
826 	pmcraid_info("scheduling worker for config table reinitialization\n");
827 	schedule_work(&cmd->drv_inst->worker_q);
828 }
829 
830 /**
831  * pmcraid_erp_done - Process completion of SCSI error response from device
832  * @cmd: pmcraid_command
833  *
834  * This function copies the sense buffer into the scsi_cmd struct and completes
835  * scsi_cmd by calling scsi_done function.
836  *
837  * Return value:
838  *  none
839  */
840 static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
841 {
842 	struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
843 	struct pmcraid_instance *pinstance = cmd->drv_inst;
844 	u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
845 
846 	if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
847 		scsi_cmd->result |= (DID_ERROR << 16);
848 		scmd_printk(KERN_INFO, scsi_cmd,
849 			    "command CDB[0] = %x failed with IOASC: 0x%08X\n",
850 			    cmd->ioa_cb->ioarcb.cdb[0], ioasc);
851 	}
852 
853 	/* if we had allocated sense buffers for request sense, copy the sense
854 	 * release the buffers
855 	 */
856 	if (cmd->sense_buffer != NULL) {
857 		memcpy(scsi_cmd->sense_buffer,
858 		       cmd->sense_buffer,
859 		       SCSI_SENSE_BUFFERSIZE);
860 		pci_free_consistent(pinstance->pdev,
861 				    SCSI_SENSE_BUFFERSIZE,
862 				    cmd->sense_buffer, cmd->sense_buffer_dma);
863 		cmd->sense_buffer = NULL;
864 		cmd->sense_buffer_dma = 0;
865 	}
866 
867 	scsi_dma_unmap(scsi_cmd);
868 	pmcraid_return_cmd(cmd);
869 	scsi_cmd->scsi_done(scsi_cmd);
870 }
871 
872 /**
873  * pmcraid_fire_command - sends an IOA command to adapter
874  *
875  * This function adds the given block into pending command list
876  * and returns without waiting
877  *
878  * @cmd : command to be sent to the device
879  *
880  * Return Value
881  *	None
882  */
883 static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
884 {
885 	struct pmcraid_instance *pinstance = cmd->drv_inst;
886 	unsigned long lock_flags;
887 
888 	/* Add this command block to pending cmd pool. We do this prior to
889 	 * writting IOARCB to ioarrin because IOA might complete the command
890 	 * by the time we are about to add it to the list. Response handler
891 	 * (isr/tasklet) looks for cmd block in the pending pending list.
892 	 */
893 	spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
894 	list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
895 	spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
896 	atomic_inc(&pinstance->outstanding_cmds);
897 
898 	/* driver writes lower 32-bit value of IOARCB address only */
899 	mb();
900 	iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
901 		  pinstance->ioarrin);
902 }
903 
904 /**
905  * pmcraid_send_cmd - fires a command to IOA
906  *
907  * This function also sets up timeout function, and command completion
908  * function
909  *
910  * @cmd: pointer to the command block to be fired to IOA
911  * @cmd_done: command completion function, called once IOA responds
912  * @timeout: timeout to wait for this command completion
913  * @timeout_func: timeout handler
914  *
915  * Return value
916  *   none
917  */
918 static void pmcraid_send_cmd(
919 	struct pmcraid_cmd *cmd,
920 	void (*cmd_done) (struct pmcraid_cmd *),
921 	unsigned long timeout,
922 	void (*timeout_func) (struct pmcraid_cmd *)
923 )
924 {
925 	/* initialize done function */
926 	cmd->cmd_done = cmd_done;
927 
928 	if (timeout_func) {
929 		/* setup timeout handler */
930 		cmd->timer.data = (unsigned long)cmd;
931 		cmd->timer.expires = jiffies + timeout;
932 		cmd->timer.function = (void (*)(unsigned long))timeout_func;
933 		add_timer(&cmd->timer);
934 	}
935 
936 	/* fire the command to IOA */
937 	_pmcraid_fire_command(cmd);
938 }
939 
940 /**
941  * pmcraid_ioa_shutdown_done - completion function for IOA shutdown command
942  * @cmd: pointer to the command block used for sending IOA shutdown command
943  *
944  * Return value
945  *  None
946  */
947 static void pmcraid_ioa_shutdown_done(struct pmcraid_cmd *cmd)
948 {
949 	struct pmcraid_instance *pinstance = cmd->drv_inst;
950 	unsigned long lock_flags;
951 
952 	spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
953 	pmcraid_ioa_reset(cmd);
954 	spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
955 }
956 
957 /**
958  * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
959  *
960  * @cmd: pointer to the command block used as part of reset sequence
961  *
962  * Return Value
963  *  None
964  */
965 static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
966 {
967 	pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
968 		     cmd->ioa_cb->ioarcb.cdb[0],
969 		     le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
970 
971 	/* Note that commands sent during reset require next command to be sent
972 	 * to IOA. Hence reinit the done function as well as timeout function
973 	 */
974 	pmcraid_reinit_cmdblk(cmd);
975 	cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
976 	cmd->ioa_cb->ioarcb.resource_handle =
977 		cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
978 	cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
979 	cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
980 
981 	/* fire shutdown command to hardware. */
982 	pmcraid_info("firing normal shutdown command (%d) to IOA\n",
983 		     le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
984 
985 	pmcraid_notify_ioastate(cmd->drv_inst, PMC_DEVICE_EVENT_SHUTDOWN_START);
986 
987 	pmcraid_send_cmd(cmd, pmcraid_ioa_shutdown_done,
988 			 PMCRAID_SHUTDOWN_TIMEOUT,
989 			 pmcraid_timeout_handler);
990 }
991 
992 /**
993  * pmcraid_get_fwversion_done - completion function for get_fwversion
994  *
995  * @cmd: pointer to command block used to send INQUIRY command
996  *
997  * Return Value
998  *	none
999  */
1000 static void pmcraid_querycfg(struct pmcraid_cmd *);
1001 
1002 static void pmcraid_get_fwversion_done(struct pmcraid_cmd *cmd)
1003 {
1004 	struct pmcraid_instance *pinstance = cmd->drv_inst;
1005 	u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1006 	unsigned long lock_flags;
1007 
1008 	/* configuration table entry size depends on firmware version. If fw
1009 	 * version is not known, it is not possible to interpret IOA config
1010 	 * table
1011 	 */
1012 	if (ioasc) {
1013 		pmcraid_err("IOA Inquiry failed with %x\n", ioasc);
1014 		spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
1015 		pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1016 		pmcraid_reset_alert(cmd);
1017 		spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
1018 	} else  {
1019 		pmcraid_querycfg(cmd);
1020 	}
1021 }
1022 
1023 /**
1024  * pmcraid_get_fwversion - reads firmware version information
1025  *
1026  * @cmd: pointer to command block used to send INQUIRY command
1027  *
1028  * Return Value
1029  *	none
1030  */
1031 static void pmcraid_get_fwversion(struct pmcraid_cmd *cmd)
1032 {
1033 	struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
1034 	struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
1035 	struct pmcraid_instance *pinstance = cmd->drv_inst;
1036 	u16 data_size = sizeof(struct pmcraid_inquiry_data);
1037 
1038 	pmcraid_reinit_cmdblk(cmd);
1039 	ioarcb->request_type = REQ_TYPE_SCSI;
1040 	ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
1041 	ioarcb->cdb[0] = INQUIRY;
1042 	ioarcb->cdb[1] = 1;
1043 	ioarcb->cdb[2] = 0xD0;
1044 	ioarcb->cdb[3] = (data_size >> 8) & 0xFF;
1045 	ioarcb->cdb[4] = data_size & 0xFF;
1046 
1047 	/* Since entire inquiry data it can be part of IOARCB itself
1048 	 */
1049 	ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
1050 					offsetof(struct pmcraid_ioarcb,
1051 						add_data.u.ioadl[0]));
1052 	ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
1053 	ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
1054 
1055 	ioarcb->request_flags0 |= NO_LINK_DESCS;
1056 	ioarcb->data_transfer_length = cpu_to_le32(data_size);
1057 	ioadl = &(ioarcb->add_data.u.ioadl[0]);
1058 	ioadl->flags = IOADL_FLAGS_LAST_DESC;
1059 	ioadl->address = cpu_to_le64(pinstance->inq_data_baddr);
1060 	ioadl->data_len = cpu_to_le32(data_size);
1061 
1062 	pmcraid_send_cmd(cmd, pmcraid_get_fwversion_done,
1063 			 PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
1064 }
1065 
1066 /**
1067  * pmcraid_identify_hrrq - registers host rrq buffers with IOA
1068  * @cmd: pointer to command block to be used for identify hrrq
1069  *
1070  * Return Value
1071  *	 none
1072  */
1073 static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
1074 {
1075 	struct pmcraid_instance *pinstance = cmd->drv_inst;
1076 	struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
1077 	int index = cmd->hrrq_index;
1078 	__be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
1079 	u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
1080 	void (*done_function)(struct pmcraid_cmd *);
1081 
1082 	pmcraid_reinit_cmdblk(cmd);
1083 	cmd->hrrq_index = index + 1;
1084 
1085 	if (cmd->hrrq_index < pinstance->num_hrrq) {
1086 		done_function = pmcraid_identify_hrrq;
1087 	} else {
1088 		cmd->hrrq_index = 0;
1089 		done_function = pmcraid_get_fwversion;
1090 	}
1091 
1092 	/* Initialize ioarcb */
1093 	ioarcb->request_type = REQ_TYPE_IOACMD;
1094 	ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
1095 
1096 	/* initialize the hrrq number where IOA will respond to this command */
1097 	ioarcb->hrrq_id = index;
1098 	ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
1099 	ioarcb->cdb[1] = index;
1100 
1101 	/* IOA expects 64-bit pci address to be written in B.E format
1102 	 * (i.e cdb[2]=MSByte..cdb[9]=LSB.
1103 	 */
1104 	pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb:index => %llx:%llx:%x\n",
1105 		     hrrq_addr, ioarcb->ioarcb_bus_addr, index);
1106 
1107 	memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
1108 	memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
1109 
1110 	/* Subsequent commands require HRRQ identification to be successful.
1111 	 * Note that this gets called even during reset from SCSI mid-layer
1112 	 * or tasklet
1113 	 */
1114 	pmcraid_send_cmd(cmd, done_function,
1115 			 PMCRAID_INTERNAL_TIMEOUT,
1116 			 pmcraid_timeout_handler);
1117 }
1118 
1119 static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
1120 static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
1121 
1122 /**
1123  * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
1124  *
1125  * @cmd: initialized command block pointer
1126  *
1127  * Return Value
1128  *   none
1129  */
1130 static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
1131 {
1132 	if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
1133 		atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
1134 	else
1135 		atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
1136 
1137 	pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
1138 }
1139 
1140 /**
1141  * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
1142  *
1143  * @pinstance: pointer to adapter instance structure
1144  * @type: HCAM type
1145  *
1146  * Return Value
1147  *   pointer to initialized pmcraid_cmd structure or NULL
1148  */
1149 static struct pmcraid_cmd *pmcraid_init_hcam
1150 (
1151 	struct pmcraid_instance *pinstance,
1152 	u8 type
1153 )
1154 {
1155 	struct pmcraid_cmd *cmd;
1156 	struct pmcraid_ioarcb *ioarcb;
1157 	struct pmcraid_ioadl_desc *ioadl;
1158 	struct pmcraid_hostrcb *hcam;
1159 	void (*cmd_done) (struct pmcraid_cmd *);
1160 	dma_addr_t dma;
1161 	int rcb_size;
1162 
1163 	cmd = pmcraid_get_free_cmd(pinstance);
1164 
1165 	if (!cmd) {
1166 		pmcraid_err("no free command blocks for hcam\n");
1167 		return cmd;
1168 	}
1169 
1170 	if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
1171 		rcb_size = sizeof(struct pmcraid_hcam_ccn_ext);
1172 		cmd_done = pmcraid_process_ccn;
1173 		dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
1174 		hcam = &pinstance->ccn;
1175 	} else {
1176 		rcb_size = sizeof(struct pmcraid_hcam_ldn);
1177 		cmd_done = pmcraid_process_ldn;
1178 		dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
1179 		hcam = &pinstance->ldn;
1180 	}
1181 
1182 	/* initialize command pointer used for HCAM registration */
1183 	hcam->cmd = cmd;
1184 
1185 	ioarcb = &cmd->ioa_cb->ioarcb;
1186 	ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
1187 					offsetof(struct pmcraid_ioarcb,
1188 						add_data.u.ioadl[0]));
1189 	ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
1190 	ioadl = ioarcb->add_data.u.ioadl;
1191 
1192 	/* Initialize ioarcb */
1193 	ioarcb->request_type = REQ_TYPE_HCAM;
1194 	ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
1195 	ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
1196 	ioarcb->cdb[1] = type;
1197 	ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
1198 	ioarcb->cdb[8] = (rcb_size) & 0xFF;
1199 
1200 	ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
1201 
1202 	ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
1203 	ioadl[0].data_len = cpu_to_le32(rcb_size);
1204 	ioadl[0].address = cpu_to_le32(dma);
1205 
1206 	cmd->cmd_done = cmd_done;
1207 	return cmd;
1208 }
1209 
1210 /**
1211  * pmcraid_send_hcam - Send an HCAM to IOA
1212  * @pinstance: ioa config struct
1213  * @type: HCAM type
1214  *
1215  * This function will send a Host Controlled Async command to IOA.
1216  *
1217  * Return value:
1218  *	none
1219  */
1220 static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
1221 {
1222 	struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
1223 	pmcraid_send_hcam_cmd(cmd);
1224 }
1225 
1226 
1227 /**
1228  * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
1229  *
1230  * @cmd: pointer to cmd that is used as cancelling command
1231  * @cmd_to_cancel: pointer to the command that needs to be cancelled
1232  */
1233 static void pmcraid_prepare_cancel_cmd(
1234 	struct pmcraid_cmd *cmd,
1235 	struct pmcraid_cmd *cmd_to_cancel
1236 )
1237 {
1238 	struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
1239 	__be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
1240 
1241 	/* Get the resource handle to where the command to be aborted has been
1242 	 * sent.
1243 	 */
1244 	ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
1245 	ioarcb->request_type = REQ_TYPE_IOACMD;
1246 	memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
1247 	ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
1248 
1249 	/* IOARCB address of the command to be cancelled is given in
1250 	 * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
1251 	 * IOARCB address are not masked.
1252 	 */
1253 	ioarcb_addr = cpu_to_be64(ioarcb_addr);
1254 	memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
1255 }
1256 
1257 /**
1258  * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
1259  *
1260  * @cmd: command to be used as cancelling command
1261  * @type: HCAM type
1262  * @cmd_done: op done function for the cancelling command
1263  */
1264 static void pmcraid_cancel_hcam(
1265 	struct pmcraid_cmd *cmd,
1266 	u8 type,
1267 	void (*cmd_done) (struct pmcraid_cmd *)
1268 )
1269 {
1270 	struct pmcraid_instance *pinstance;
1271 	struct pmcraid_hostrcb  *hcam;
1272 
1273 	pinstance = cmd->drv_inst;
1274 	hcam =  (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
1275 		&pinstance->ldn : &pinstance->ccn;
1276 
1277 	/* prepare for cancelling previous hcam command. If the HCAM is
1278 	 * currently not pending with IOA, we would have hcam->cmd as non-null
1279 	 */
1280 	if (hcam->cmd == NULL)
1281 		return;
1282 
1283 	pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
1284 
1285 	/* writing to IOARRIN must be protected by host_lock, as mid-layer
1286 	 * schedule queuecommand while we are doing this
1287 	 */
1288 	pmcraid_send_cmd(cmd, cmd_done,
1289 			 PMCRAID_INTERNAL_TIMEOUT,
1290 			 pmcraid_timeout_handler);
1291 }
1292 
1293 /**
1294  * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
1295  *
1296  * @cmd: command block to be used for cancelling the HCAM
1297  */
1298 static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
1299 {
1300 	pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
1301 		     cmd->ioa_cb->ioarcb.cdb[0],
1302 		     le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
1303 
1304 	pmcraid_reinit_cmdblk(cmd);
1305 
1306 	pmcraid_cancel_hcam(cmd,
1307 			    PMCRAID_HCAM_CODE_CONFIG_CHANGE,
1308 			    pmcraid_ioa_shutdown);
1309 }
1310 
1311 /**
1312  * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
1313  *
1314  * @cmd: command block to be used for cancelling the HCAM
1315  */
1316 static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
1317 {
1318 	pmcraid_cancel_hcam(cmd,
1319 			    PMCRAID_HCAM_CODE_LOG_DATA,
1320 			    pmcraid_cancel_ccn);
1321 }
1322 
1323 /**
1324  * pmcraid_expose_resource - check if the resource can be exposed to OS
1325  *
1326  * @fw_version: firmware version code
1327  * @cfgte: pointer to configuration table entry of the resource
1328  *
1329  * Return value:
1330  *	true if resource can be added to midlayer, false(0) otherwise
1331  */
1332 static int pmcraid_expose_resource(u16 fw_version,
1333 				   struct pmcraid_config_table_entry *cfgte)
1334 {
1335 	int retval = 0;
1336 
1337 	if (cfgte->resource_type == RES_TYPE_VSET) {
1338 		if (fw_version <= PMCRAID_FW_VERSION_1)
1339 			retval = ((cfgte->unique_flags1 & 0x80) == 0);
1340 		else
1341 			retval = ((cfgte->unique_flags0 & 0x80) == 0 &&
1342 				  (cfgte->unique_flags1 & 0x80) == 0);
1343 
1344 	} else if (cfgte->resource_type == RES_TYPE_GSCSI)
1345 		retval = (RES_BUS(cfgte->resource_address) !=
1346 				PMCRAID_VIRTUAL_ENCL_BUS_ID);
1347 	return retval;
1348 }
1349 
1350 /* attributes supported by pmcraid_event_family */
1351 enum {
1352 	PMCRAID_AEN_ATTR_UNSPEC,
1353 	PMCRAID_AEN_ATTR_EVENT,
1354 	__PMCRAID_AEN_ATTR_MAX,
1355 };
1356 #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
1357 
1358 /* commands supported by pmcraid_event_family */
1359 enum {
1360 	PMCRAID_AEN_CMD_UNSPEC,
1361 	PMCRAID_AEN_CMD_EVENT,
1362 	__PMCRAID_AEN_CMD_MAX,
1363 };
1364 #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
1365 
1366 static struct genl_multicast_group pmcraid_mcgrps[] = {
1367 	{ .name = "events", /* not really used - see ID discussion below */ },
1368 };
1369 
1370 static struct genl_family pmcraid_event_family = {
1371 	/*
1372 	 * Due to prior multicast group abuse (the code having assumed that
1373 	 * the family ID can be used as a multicast group ID) we need to
1374 	 * statically allocate a family (and thus group) ID.
1375 	 */
1376 	.id = GENL_ID_PMCRAID,
1377 	.name = "pmcraid",
1378 	.version = 1,
1379 	.maxattr = PMCRAID_AEN_ATTR_MAX,
1380 	.mcgrps = pmcraid_mcgrps,
1381 	.n_mcgrps = ARRAY_SIZE(pmcraid_mcgrps),
1382 };
1383 
1384 /**
1385  * pmcraid_netlink_init - registers pmcraid_event_family
1386  *
1387  * Return value:
1388  *	0 if the pmcraid_event_family is successfully registered
1389  *	with netlink generic, non-zero otherwise
1390  */
1391 static int pmcraid_netlink_init(void)
1392 {
1393 	int result;
1394 
1395 	result = genl_register_family(&pmcraid_event_family);
1396 
1397 	if (result)
1398 		return result;
1399 
1400 	pmcraid_info("registered NETLINK GENERIC group: %d\n",
1401 		     pmcraid_event_family.id);
1402 
1403 	return result;
1404 }
1405 
1406 /**
1407  * pmcraid_netlink_release - unregisters pmcraid_event_family
1408  *
1409  * Return value:
1410  *	none
1411  */
1412 static void pmcraid_netlink_release(void)
1413 {
1414 	genl_unregister_family(&pmcraid_event_family);
1415 }
1416 
1417 /**
1418  * pmcraid_notify_aen - sends event msg to user space application
1419  * @pinstance: pointer to adapter instance structure
1420  * @type: HCAM type
1421  *
1422  * Return value:
1423  *	0 if success, error value in case of any failure.
1424  */
1425 static int pmcraid_notify_aen(
1426 	struct pmcraid_instance *pinstance,
1427 	struct pmcraid_aen_msg  *aen_msg,
1428 	u32    data_size
1429 )
1430 {
1431 	struct sk_buff *skb;
1432 	void *msg_header;
1433 	u32  total_size, nla_genl_hdr_total_size;
1434 	int result;
1435 
1436 	aen_msg->hostno = (pinstance->host->unique_id << 16 |
1437 			   MINOR(pinstance->cdev.dev));
1438 	aen_msg->length = data_size;
1439 
1440 	data_size += sizeof(*aen_msg);
1441 
1442 	total_size = nla_total_size(data_size);
1443 	/* Add GENL_HDR to total_size */
1444 	nla_genl_hdr_total_size =
1445 		(total_size + (GENL_HDRLEN +
1446 		((struct genl_family *)&pmcraid_event_family)->hdrsize)
1447 		 + NLMSG_HDRLEN);
1448 	skb = genlmsg_new(nla_genl_hdr_total_size, GFP_ATOMIC);
1449 
1450 
1451 	if (!skb) {
1452 		pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
1453 			     total_size);
1454 		return -ENOMEM;
1455 	}
1456 
1457 	/* add the genetlink message header */
1458 	msg_header = genlmsg_put(skb, 0, 0,
1459 				 &pmcraid_event_family, 0,
1460 				 PMCRAID_AEN_CMD_EVENT);
1461 	if (!msg_header) {
1462 		pmcraid_err("failed to copy command details\n");
1463 		nlmsg_free(skb);
1464 		return -ENOMEM;
1465 	}
1466 
1467 	result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
1468 
1469 	if (result) {
1470 		pmcraid_err("failed to copy AEN attribute data\n");
1471 		nlmsg_free(skb);
1472 		return -EINVAL;
1473 	}
1474 
1475 	/* send genetlink multicast message to notify appplications */
1476 	result = genlmsg_end(skb, msg_header);
1477 
1478 	if (result < 0) {
1479 		pmcraid_err("genlmsg_end failed\n");
1480 		nlmsg_free(skb);
1481 		return result;
1482 	}
1483 
1484 	result = genlmsg_multicast(&pmcraid_event_family, skb,
1485 				   0, 0, GFP_ATOMIC);
1486 
1487 	/* If there are no listeners, genlmsg_multicast may return non-zero
1488 	 * value.
1489 	 */
1490 	if (result)
1491 		pmcraid_info("error (%x) sending aen event message\n", result);
1492 	return result;
1493 }
1494 
1495 /**
1496  * pmcraid_notify_ccn - notifies about CCN event msg to user space
1497  * @pinstance: pointer adapter instance structure
1498  *
1499  * Return value:
1500  *	0 if success, error value in case of any failure
1501  */
1502 static int pmcraid_notify_ccn(struct pmcraid_instance *pinstance)
1503 {
1504 	return pmcraid_notify_aen(pinstance,
1505 				pinstance->ccn.msg,
1506 				pinstance->ccn.hcam->data_len +
1507 				sizeof(struct pmcraid_hcam_hdr));
1508 }
1509 
1510 /**
1511  * pmcraid_notify_ldn - notifies about CCN event msg to user space
1512  * @pinstance: pointer adapter instance structure
1513  *
1514  * Return value:
1515  *	0 if success, error value in case of any failure
1516  */
1517 static int pmcraid_notify_ldn(struct pmcraid_instance *pinstance)
1518 {
1519 	return pmcraid_notify_aen(pinstance,
1520 				pinstance->ldn.msg,
1521 				pinstance->ldn.hcam->data_len +
1522 				sizeof(struct pmcraid_hcam_hdr));
1523 }
1524 
1525 /**
1526  * pmcraid_notify_ioastate - sends IOA state event msg to user space
1527  * @pinstance: pointer adapter instance structure
1528  * @evt: controller state event to be sent
1529  *
1530  * Return value:
1531  *	0 if success, error value in case of any failure
1532  */
1533 static void pmcraid_notify_ioastate(struct pmcraid_instance *pinstance, u32 evt)
1534 {
1535 	pinstance->scn.ioa_state = evt;
1536 	pmcraid_notify_aen(pinstance,
1537 			  &pinstance->scn.msg,
1538 			  sizeof(u32));
1539 }
1540 
1541 /**
1542  * pmcraid_handle_config_change - Handle a config change from the adapter
1543  * @pinstance: pointer to per adapter instance structure
1544  *
1545  * Return value:
1546  *  none
1547  */
1548 
1549 static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
1550 {
1551 	struct pmcraid_config_table_entry *cfg_entry;
1552 	struct pmcraid_hcam_ccn *ccn_hcam;
1553 	struct pmcraid_cmd *cmd;
1554 	struct pmcraid_cmd *cfgcmd;
1555 	struct pmcraid_resource_entry *res = NULL;
1556 	unsigned long lock_flags;
1557 	unsigned long host_lock_flags;
1558 	u32 new_entry = 1;
1559 	u32 hidden_entry = 0;
1560 	u16 fw_version;
1561 	int rc;
1562 
1563 	ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
1564 	cfg_entry = &ccn_hcam->cfg_entry;
1565 	fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
1566 
1567 	pmcraid_info("CCN(%x): %x timestamp: %llx type: %x lost: %x flags: %x \
1568 		 res: %x:%x:%x:%x\n",
1569 		 pinstance->ccn.hcam->ilid,
1570 		 pinstance->ccn.hcam->op_code,
1571 		((pinstance->ccn.hcam->timestamp1) |
1572 		((pinstance->ccn.hcam->timestamp2 & 0xffffffffLL) << 32)),
1573 		 pinstance->ccn.hcam->notification_type,
1574 		 pinstance->ccn.hcam->notification_lost,
1575 		 pinstance->ccn.hcam->flags,
1576 		 pinstance->host->unique_id,
1577 		 RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
1578 		 (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
1579 			RES_BUS(cfg_entry->resource_address)),
1580 		 RES_IS_VSET(*cfg_entry) ?
1581 			(fw_version <= PMCRAID_FW_VERSION_1 ?
1582 				cfg_entry->unique_flags1 :
1583 					cfg_entry->array_id & 0xFF) :
1584 			RES_TARGET(cfg_entry->resource_address),
1585 		 RES_LUN(cfg_entry->resource_address));
1586 
1587 
1588 	/* If this HCAM indicates a lost notification, read the config table */
1589 	if (pinstance->ccn.hcam->notification_lost) {
1590 		cfgcmd = pmcraid_get_free_cmd(pinstance);
1591 		if (cfgcmd) {
1592 			pmcraid_info("lost CCN, reading config table\b");
1593 			pinstance->reinit_cfg_table = 1;
1594 			pmcraid_querycfg(cfgcmd);
1595 		} else {
1596 			pmcraid_err("lost CCN, no free cmd for querycfg\n");
1597 		}
1598 		goto out_notify_apps;
1599 	}
1600 
1601 	/* If this resource is not going to be added to mid-layer, just notify
1602 	 * applications and return. If this notification is about hiding a VSET
1603 	 * resource, check if it was exposed already.
1604 	 */
1605 	if (pinstance->ccn.hcam->notification_type ==
1606 	    NOTIFICATION_TYPE_ENTRY_CHANGED &&
1607 	    cfg_entry->resource_type == RES_TYPE_VSET) {
1608 
1609 		if (fw_version <= PMCRAID_FW_VERSION_1)
1610 			hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
1611 		else
1612 			hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
1613 
1614 	} else if (!pmcraid_expose_resource(fw_version, cfg_entry)) {
1615 		goto out_notify_apps;
1616 	}
1617 
1618 	spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
1619 	list_for_each_entry(res, &pinstance->used_res_q, queue) {
1620 		rc = memcmp(&res->cfg_entry.resource_address,
1621 			    &cfg_entry->resource_address,
1622 			    sizeof(cfg_entry->resource_address));
1623 		if (!rc) {
1624 			new_entry = 0;
1625 			break;
1626 		}
1627 	}
1628 
1629 	if (new_entry) {
1630 
1631 		if (hidden_entry) {
1632 			spin_unlock_irqrestore(&pinstance->resource_lock,
1633 						lock_flags);
1634 			goto out_notify_apps;
1635 		}
1636 
1637 		/* If there are more number of resources than what driver can
1638 		 * manage, do not notify the applications about the CCN. Just
1639 		 * ignore this notifications and re-register the same HCAM
1640 		 */
1641 		if (list_empty(&pinstance->free_res_q)) {
1642 			spin_unlock_irqrestore(&pinstance->resource_lock,
1643 						lock_flags);
1644 			pmcraid_err("too many resources attached\n");
1645 			spin_lock_irqsave(pinstance->host->host_lock,
1646 					  host_lock_flags);
1647 			pmcraid_send_hcam(pinstance,
1648 					  PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1649 			spin_unlock_irqrestore(pinstance->host->host_lock,
1650 					       host_lock_flags);
1651 			return;
1652 		}
1653 
1654 		res = list_entry(pinstance->free_res_q.next,
1655 				 struct pmcraid_resource_entry, queue);
1656 
1657 		list_del(&res->queue);
1658 		res->scsi_dev = NULL;
1659 		res->reset_progress = 0;
1660 		list_add_tail(&res->queue, &pinstance->used_res_q);
1661 	}
1662 
1663 	memcpy(&res->cfg_entry, cfg_entry, pinstance->config_table_entry_size);
1664 
1665 	if (pinstance->ccn.hcam->notification_type ==
1666 	    NOTIFICATION_TYPE_ENTRY_DELETED || hidden_entry) {
1667 		if (res->scsi_dev) {
1668 			if (fw_version <= PMCRAID_FW_VERSION_1)
1669 				res->cfg_entry.unique_flags1 &= 0x7F;
1670 			else
1671 				res->cfg_entry.array_id &= 0xFF;
1672 			res->change_detected = RES_CHANGE_DEL;
1673 			res->cfg_entry.resource_handle =
1674 				PMCRAID_INVALID_RES_HANDLE;
1675 			schedule_work(&pinstance->worker_q);
1676 		} else {
1677 			/* This may be one of the non-exposed resources */
1678 			list_move_tail(&res->queue, &pinstance->free_res_q);
1679 		}
1680 	} else if (!res->scsi_dev) {
1681 		res->change_detected = RES_CHANGE_ADD;
1682 		schedule_work(&pinstance->worker_q);
1683 	}
1684 	spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
1685 
1686 out_notify_apps:
1687 
1688 	/* Notify configuration changes to registered applications.*/
1689 	if (!pmcraid_disable_aen)
1690 		pmcraid_notify_ccn(pinstance);
1691 
1692 	cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1693 	if (cmd)
1694 		pmcraid_send_hcam_cmd(cmd);
1695 }
1696 
1697 /**
1698  * pmcraid_get_error_info - return error string for an ioasc
1699  * @ioasc: ioasc code
1700  * Return Value
1701  *	 none
1702  */
1703 static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
1704 {
1705 	int i;
1706 	for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
1707 		if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
1708 			return &pmcraid_ioasc_error_table[i];
1709 	}
1710 	return NULL;
1711 }
1712 
1713 /**
1714  * pmcraid_ioasc_logger - log IOASC information based user-settings
1715  * @ioasc: ioasc code
1716  * @cmd: pointer to command that resulted in 'ioasc'
1717  */
1718 void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
1719 {
1720 	struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
1721 
1722 	if (error_info == NULL ||
1723 		cmd->drv_inst->current_log_level < error_info->log_level)
1724 		return;
1725 
1726 	/* log the error string */
1727 	pmcraid_err("cmd [%x] for resource %x failed with %x(%s)\n",
1728 		cmd->ioa_cb->ioarcb.cdb[0],
1729 		cmd->ioa_cb->ioarcb.resource_handle,
1730 		le32_to_cpu(ioasc), error_info->error_string);
1731 }
1732 
1733 /**
1734  * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
1735  *
1736  * @pinstance: pointer to per adapter instance structure
1737  *
1738  * Return value:
1739  *  none
1740  */
1741 static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
1742 {
1743 	struct pmcraid_hcam_ldn *hcam_ldn;
1744 	u32 ioasc;
1745 
1746 	hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
1747 
1748 	pmcraid_info
1749 		("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
1750 		 pinstance->ldn.hcam->ilid,
1751 		 pinstance->ldn.hcam->op_code,
1752 		 pinstance->ldn.hcam->notification_type,
1753 		 pinstance->ldn.hcam->notification_lost,
1754 		 pinstance->ldn.hcam->flags,
1755 		 pinstance->ldn.hcam->overlay_id);
1756 
1757 	/* log only the errors, no need to log informational log entries */
1758 	if (pinstance->ldn.hcam->notification_type !=
1759 	    NOTIFICATION_TYPE_ERROR_LOG)
1760 		return;
1761 
1762 	if (pinstance->ldn.hcam->notification_lost ==
1763 	    HOSTRCB_NOTIFICATIONS_LOST)
1764 		dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
1765 
1766 	ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
1767 
1768 	if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
1769 		ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
1770 		dev_info(&pinstance->pdev->dev,
1771 			"UnitAttention due to IOA Bus Reset\n");
1772 		scsi_report_bus_reset(
1773 			pinstance->host,
1774 			RES_BUS(hcam_ldn->error_log.fd_ra));
1775 	}
1776 
1777 	return;
1778 }
1779 
1780 /**
1781  * pmcraid_process_ccn - Op done function for a CCN.
1782  * @cmd: pointer to command struct
1783  *
1784  * This function is the op done function for a configuration
1785  * change notification
1786  *
1787  * Return value:
1788  * none
1789  */
1790 static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
1791 {
1792 	struct pmcraid_instance *pinstance = cmd->drv_inst;
1793 	u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1794 	unsigned long lock_flags;
1795 
1796 	pinstance->ccn.cmd = NULL;
1797 	pmcraid_return_cmd(cmd);
1798 
1799 	/* If driver initiated IOA reset happened while this hcam was pending
1800 	 * with IOA, or IOA bringdown sequence is in progress, no need to
1801 	 * re-register the hcam
1802 	 */
1803 	if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
1804 	    atomic_read(&pinstance->ccn.ignore) == 1) {
1805 		return;
1806 	} else if (ioasc) {
1807 		dev_info(&pinstance->pdev->dev,
1808 			"Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
1809 		spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
1810 		pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1811 		spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
1812 	} else {
1813 		pmcraid_handle_config_change(pinstance);
1814 	}
1815 }
1816 
1817 /**
1818  * pmcraid_process_ldn - op done function for an LDN
1819  * @cmd: pointer to command block
1820  *
1821  * Return value
1822  *   none
1823  */
1824 static void pmcraid_initiate_reset(struct pmcraid_instance *);
1825 static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd);
1826 
1827 static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
1828 {
1829 	struct pmcraid_instance *pinstance = cmd->drv_inst;
1830 	struct pmcraid_hcam_ldn *ldn_hcam =
1831 			(struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
1832 	u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
1833 	u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
1834 	unsigned long lock_flags;
1835 
1836 	/* return the command block back to freepool */
1837 	pinstance->ldn.cmd = NULL;
1838 	pmcraid_return_cmd(cmd);
1839 
1840 	/* If driver initiated IOA reset happened while this hcam was pending
1841 	 * with IOA, no need to re-register the hcam as reset engine will do it
1842 	 * once reset sequence is complete
1843 	 */
1844 	if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
1845 	    atomic_read(&pinstance->ccn.ignore) == 1) {
1846 		return;
1847 	} else if (!ioasc) {
1848 		pmcraid_handle_error_log(pinstance);
1849 		if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
1850 			spin_lock_irqsave(pinstance->host->host_lock,
1851 					  lock_flags);
1852 			pmcraid_initiate_reset(pinstance);
1853 			spin_unlock_irqrestore(pinstance->host->host_lock,
1854 					       lock_flags);
1855 			return;
1856 		}
1857 		if (fd_ioasc == PMCRAID_IOASC_TIME_STAMP_OUT_OF_SYNC) {
1858 			pinstance->timestamp_error = 1;
1859 			pmcraid_set_timestamp(cmd);
1860 		}
1861 	} else {
1862 		dev_info(&pinstance->pdev->dev,
1863 			"Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
1864 	}
1865 	/* send netlink message for HCAM notification if enabled */
1866 	if (!pmcraid_disable_aen)
1867 		pmcraid_notify_ldn(pinstance);
1868 
1869 	cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1870 	if (cmd)
1871 		pmcraid_send_hcam_cmd(cmd);
1872 }
1873 
1874 /**
1875  * pmcraid_register_hcams - register HCAMs for CCN and LDN
1876  *
1877  * @pinstance: pointer per adapter instance structure
1878  *
1879  * Return Value
1880  *   none
1881  */
1882 static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
1883 {
1884 	pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
1885 	pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
1886 }
1887 
1888 /**
1889  * pmcraid_unregister_hcams - cancel HCAMs registered already
1890  * @cmd: pointer to command used as part of reset sequence
1891  */
1892 static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
1893 {
1894 	struct pmcraid_instance *pinstance = cmd->drv_inst;
1895 
1896 	/* During IOA bringdown, HCAM gets fired and tasklet proceeds with
1897 	 * handling hcam response though it is not necessary. In order to
1898 	 * prevent this, set 'ignore', so that bring-down sequence doesn't
1899 	 * re-send any more hcams
1900 	 */
1901 	atomic_set(&pinstance->ccn.ignore, 1);
1902 	atomic_set(&pinstance->ldn.ignore, 1);
1903 
1904 	/* If adapter reset was forced as part of runtime reset sequence,
1905 	 * start the reset sequence. Reset will be triggered even in case
1906 	 * IOA unit_check.
1907 	 */
1908 	if ((pinstance->force_ioa_reset && !pinstance->ioa_bringdown) ||
1909 	     pinstance->ioa_unit_check) {
1910 		pinstance->force_ioa_reset = 0;
1911 		pinstance->ioa_unit_check = 0;
1912 		pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
1913 		pmcraid_reset_alert(cmd);
1914 		return;
1915 	}
1916 
1917 	/* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
1918 	 * one after the other. So CCN cancellation will be triggered by
1919 	 * pmcraid_cancel_ldn itself.
1920 	 */
1921 	pmcraid_cancel_ldn(cmd);
1922 }
1923 
1924 /**
1925  * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
1926  * @pinstance: pointer to adapter instance structure
1927  * Return Value
1928  *  1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
1929  */
1930 static void pmcraid_reinit_buffers(struct pmcraid_instance *);
1931 
1932 static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
1933 {
1934 	u32 intrs;
1935 
1936 	pmcraid_reinit_buffers(pinstance);
1937 	intrs = pmcraid_read_interrupts(pinstance);
1938 
1939 	pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
1940 
1941 	if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
1942 		if (!pinstance->interrupt_mode) {
1943 			iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
1944 				pinstance->int_regs.
1945 				ioa_host_interrupt_mask_reg);
1946 			iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
1947 				pinstance->int_regs.ioa_host_interrupt_clr_reg);
1948 		}
1949 		return 1;
1950 	} else {
1951 		return 0;
1952 	}
1953 }
1954 
1955 /**
1956  * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
1957  * @cmd : pointer to reset command block
1958  *
1959  * Return Value
1960  *	none
1961  */
1962 static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
1963 {
1964 	struct pmcraid_instance *pinstance = cmd->drv_inst;
1965 	u32 int_reg;
1966 	u32 doorbell;
1967 
1968 	/* There will be an interrupt when Transition to Operational bit is
1969 	 * set so tasklet would execute next reset task. The timeout handler
1970 	 * would re-initiate a reset
1971 	 */
1972 	cmd->cmd_done = pmcraid_ioa_reset;
1973 	cmd->timer.data = (unsigned long)cmd;
1974 	cmd->timer.expires = jiffies +
1975 			     msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
1976 	cmd->timer.function = (void (*)(unsigned long))pmcraid_timeout_handler;
1977 
1978 	if (!timer_pending(&cmd->timer))
1979 		add_timer(&cmd->timer);
1980 
1981 	/* Enable destructive diagnostics on IOA if it is not yet in
1982 	 * operational state
1983 	 */
1984 	doorbell = DOORBELL_RUNTIME_RESET |
1985 		   DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
1986 
1987 	/* Since we do RESET_ALERT and Start BIST we have to again write
1988 	 * MSIX Doorbell to indicate the interrupt mode
1989 	 */
1990 	if (pinstance->interrupt_mode) {
1991 		iowrite32(DOORBELL_INTR_MODE_MSIX,
1992 			  pinstance->int_regs.host_ioa_interrupt_reg);
1993 		ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
1994 	}
1995 
1996 	iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
1997 	ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
1998 	int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
1999 
2000 	pmcraid_info("Waiting for IOA to become operational %x:%x\n",
2001 		     ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
2002 		     int_reg);
2003 }
2004 
2005 /**
2006  * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
2007  *
2008  * @pinstance: pointer to adapter instance structure
2009  *
2010  * Return Value
2011  *	none
2012  */
2013 static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
2014 {
2015 	pmcraid_info("%s is not yet implemented\n", __func__);
2016 }
2017 
2018 /**
2019  * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
2020  * @pinstance: pointer to adapter instance structure
2021  *
2022  * This function fails all outstanding ops. If they are submitted to IOA
2023  * already, it sends cancel all messages if IOA is still accepting IOARCBs,
2024  * otherwise just completes the commands and returns the cmd blocks to free
2025  * pool.
2026  *
2027  * Return value:
2028  *	 none
2029  */
2030 static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
2031 {
2032 	struct pmcraid_cmd *cmd, *temp;
2033 	unsigned long lock_flags;
2034 
2035 	/* pending command list is protected by pending_pool_lock. Its
2036 	 * traversal must be done as within this lock
2037 	 */
2038 	spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
2039 	list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
2040 				 free_list) {
2041 		list_del(&cmd->free_list);
2042 		spin_unlock_irqrestore(&pinstance->pending_pool_lock,
2043 					lock_flags);
2044 		cmd->ioa_cb->ioasa.ioasc =
2045 			cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
2046 		cmd->ioa_cb->ioasa.ilid =
2047 			cpu_to_be32(PMCRAID_DRIVER_ILID);
2048 
2049 		/* In case the command timer is still running */
2050 		del_timer(&cmd->timer);
2051 
2052 		/* If this is an IO command, complete it by invoking scsi_done
2053 		 * function. If this is one of the internal commands other
2054 		 * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
2055 		 * complete it
2056 		 */
2057 		if (cmd->scsi_cmd) {
2058 
2059 			struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2060 			__le32 resp = cmd->ioa_cb->ioarcb.response_handle;
2061 
2062 			scsi_cmd->result |= DID_ERROR << 16;
2063 
2064 			scsi_dma_unmap(scsi_cmd);
2065 			pmcraid_return_cmd(cmd);
2066 
2067 			pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
2068 				     le32_to_cpu(resp) >> 2,
2069 				     cmd->ioa_cb->ioarcb.cdb[0],
2070 				     scsi_cmd->result);
2071 			scsi_cmd->scsi_done(scsi_cmd);
2072 		} else if (cmd->cmd_done == pmcraid_internal_done ||
2073 			   cmd->cmd_done == pmcraid_erp_done) {
2074 			cmd->cmd_done(cmd);
2075 		} else if (cmd->cmd_done != pmcraid_ioa_reset &&
2076 			   cmd->cmd_done != pmcraid_ioa_shutdown_done) {
2077 			pmcraid_return_cmd(cmd);
2078 		}
2079 
2080 		atomic_dec(&pinstance->outstanding_cmds);
2081 		spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
2082 	}
2083 
2084 	spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
2085 }
2086 
2087 /**
2088  * pmcraid_ioa_reset - Implementation of IOA reset logic
2089  *
2090  * @cmd: pointer to the cmd block to be used for entire reset process
2091  *
2092  * This function executes most of the steps required for IOA reset. This gets
2093  * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
2094  * 'eh_' thread. Access to variables used for controlling the reset sequence is
2095  * synchronized using host lock. Various functions called during reset process
2096  * would make use of a single command block, pointer to which is also stored in
2097  * adapter instance structure.
2098  *
2099  * Return Value
2100  *	 None
2101  */
2102 static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
2103 {
2104 	struct pmcraid_instance *pinstance = cmd->drv_inst;
2105 	u8 reset_complete = 0;
2106 
2107 	pinstance->ioa_reset_in_progress = 1;
2108 
2109 	if (pinstance->reset_cmd != cmd) {
2110 		pmcraid_err("reset is called with different command block\n");
2111 		pinstance->reset_cmd = cmd;
2112 	}
2113 
2114 	pmcraid_info("reset_engine: state = %d, command = %p\n",
2115 		      pinstance->ioa_state, cmd);
2116 
2117 	switch (pinstance->ioa_state) {
2118 
2119 	case IOA_STATE_DEAD:
2120 		/* If IOA is offline, whatever may be the reset reason, just
2121 		 * return. callers might be waiting on the reset wait_q, wake
2122 		 * up them
2123 		 */
2124 		pmcraid_err("IOA is offline no reset is possible\n");
2125 		reset_complete = 1;
2126 		break;
2127 
2128 	case IOA_STATE_IN_BRINGDOWN:
2129 		/* we enter here, once ioa shutdown command is processed by IOA
2130 		 * Alert IOA for a possible reset. If reset alert fails, IOA
2131 		 * goes through hard-reset
2132 		 */
2133 		pmcraid_disable_interrupts(pinstance, ~0);
2134 		pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
2135 		pmcraid_reset_alert(cmd);
2136 		break;
2137 
2138 	case IOA_STATE_UNKNOWN:
2139 		/* We may be called during probe or resume. Some pre-processing
2140 		 * is required for prior to reset
2141 		 */
2142 		scsi_block_requests(pinstance->host);
2143 
2144 		/* If asked to reset while IOA was processing responses or
2145 		 * there are any error responses then IOA may require
2146 		 * hard-reset.
2147 		 */
2148 		if (pinstance->ioa_hard_reset == 0) {
2149 			if (ioread32(pinstance->ioa_status) &
2150 			    INTRS_TRANSITION_TO_OPERATIONAL) {
2151 				pmcraid_info("sticky bit set, bring-up\n");
2152 				pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
2153 				pmcraid_reinit_cmdblk(cmd);
2154 				pmcraid_identify_hrrq(cmd);
2155 			} else {
2156 				pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
2157 				pmcraid_soft_reset(cmd);
2158 			}
2159 		} else {
2160 			/* Alert IOA of a possible reset and wait for critical
2161 			 * operation in progress bit to reset
2162 			 */
2163 			pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
2164 			pmcraid_reset_alert(cmd);
2165 		}
2166 		break;
2167 
2168 	case IOA_STATE_IN_RESET_ALERT:
2169 		/* If critical operation in progress bit is reset or wait gets
2170 		 * timed out, reset proceeds with starting BIST on the IOA.
2171 		 * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
2172 		 * they are 3 or more, reset engine marks IOA dead and returns
2173 		 */
2174 		pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
2175 		pmcraid_start_bist(cmd);
2176 		break;
2177 
2178 	case IOA_STATE_IN_HARD_RESET:
2179 		pinstance->ioa_reset_attempts++;
2180 
2181 		/* retry reset if we haven't reached maximum allowed limit */
2182 		if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
2183 			pinstance->ioa_reset_attempts = 0;
2184 			pmcraid_err("IOA didn't respond marking it as dead\n");
2185 			pinstance->ioa_state = IOA_STATE_DEAD;
2186 
2187 			if (pinstance->ioa_bringdown)
2188 				pmcraid_notify_ioastate(pinstance,
2189 					PMC_DEVICE_EVENT_SHUTDOWN_FAILED);
2190 			else
2191 				pmcraid_notify_ioastate(pinstance,
2192 						PMC_DEVICE_EVENT_RESET_FAILED);
2193 			reset_complete = 1;
2194 			break;
2195 		}
2196 
2197 		/* Once either bist or pci reset is done, restore PCI config
2198 		 * space. If this fails, proceed with hard reset again
2199 		 */
2200 		pci_restore_state(pinstance->pdev);
2201 
2202 		/* fail all pending commands */
2203 		pmcraid_fail_outstanding_cmds(pinstance);
2204 
2205 		/* check if unit check is active, if so extract dump */
2206 		if (pinstance->ioa_unit_check) {
2207 			pmcraid_info("unit check is active\n");
2208 			pinstance->ioa_unit_check = 0;
2209 			pmcraid_get_dump(pinstance);
2210 			pinstance->ioa_reset_attempts--;
2211 			pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
2212 			pmcraid_reset_alert(cmd);
2213 			break;
2214 		}
2215 
2216 		/* if the reset reason is to bring-down the ioa, we might be
2217 		 * done with the reset restore pci_config_space and complete
2218 		 * the reset
2219 		 */
2220 		if (pinstance->ioa_bringdown) {
2221 			pmcraid_info("bringing down the adapter\n");
2222 			pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2223 			pinstance->ioa_bringdown = 0;
2224 			pinstance->ioa_state = IOA_STATE_UNKNOWN;
2225 			pmcraid_notify_ioastate(pinstance,
2226 					PMC_DEVICE_EVENT_SHUTDOWN_SUCCESS);
2227 			reset_complete = 1;
2228 		} else {
2229 			/* bring-up IOA, so proceed with soft reset
2230 			 * Reinitialize hrrq_buffers and their indices also
2231 			 * enable interrupts after a pci_restore_state
2232 			 */
2233 			if (pmcraid_reset_enable_ioa(pinstance)) {
2234 				pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
2235 				pmcraid_info("bringing up the adapter\n");
2236 				pmcraid_reinit_cmdblk(cmd);
2237 				pmcraid_identify_hrrq(cmd);
2238 			} else {
2239 				pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
2240 				pmcraid_soft_reset(cmd);
2241 			}
2242 		}
2243 		break;
2244 
2245 	case IOA_STATE_IN_SOFT_RESET:
2246 		/* TRANSITION TO OPERATIONAL is on so start initialization
2247 		 * sequence
2248 		 */
2249 		pmcraid_info("In softreset proceeding with bring-up\n");
2250 		pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
2251 
2252 		/* Initialization commands start with HRRQ identification. From
2253 		 * now on tasklet completes most of the commands as IOA is up
2254 		 * and intrs are enabled
2255 		 */
2256 		pmcraid_identify_hrrq(cmd);
2257 		break;
2258 
2259 	case IOA_STATE_IN_BRINGUP:
2260 		/* we are done with bringing up of IOA, change the ioa_state to
2261 		 * operational and wake up any waiters
2262 		 */
2263 		pinstance->ioa_state = IOA_STATE_OPERATIONAL;
2264 		reset_complete = 1;
2265 		break;
2266 
2267 	case IOA_STATE_OPERATIONAL:
2268 	default:
2269 		/* When IOA is operational and a reset is requested, check for
2270 		 * the reset reason. If reset is to bring down IOA, unregister
2271 		 * HCAMs and initiate shutdown; if adapter reset is forced then
2272 		 * restart reset sequence again
2273 		 */
2274 		if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
2275 		    pinstance->force_ioa_reset == 0) {
2276 			pmcraid_notify_ioastate(pinstance,
2277 						PMC_DEVICE_EVENT_RESET_SUCCESS);
2278 			reset_complete = 1;
2279 		} else {
2280 			if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
2281 				pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
2282 			pmcraid_reinit_cmdblk(cmd);
2283 			pmcraid_unregister_hcams(cmd);
2284 		}
2285 		break;
2286 	}
2287 
2288 	/* reset will be completed if ioa_state is either DEAD or UNKNOWN or
2289 	 * OPERATIONAL. Reset all control variables used during reset, wake up
2290 	 * any waiting threads and let the SCSI mid-layer send commands. Note
2291 	 * that host_lock must be held before invoking scsi_report_bus_reset.
2292 	 */
2293 	if (reset_complete) {
2294 		pinstance->ioa_reset_in_progress = 0;
2295 		pinstance->ioa_reset_attempts = 0;
2296 		pinstance->reset_cmd = NULL;
2297 		pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2298 		pinstance->ioa_bringdown = 0;
2299 		pmcraid_return_cmd(cmd);
2300 
2301 		/* If target state is to bring up the adapter, proceed with
2302 		 * hcam registration and resource exposure to mid-layer.
2303 		 */
2304 		if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
2305 			pmcraid_register_hcams(pinstance);
2306 
2307 		wake_up_all(&pinstance->reset_wait_q);
2308 	}
2309 
2310 	return;
2311 }
2312 
2313 /**
2314  * pmcraid_initiate_reset - initiates reset sequence. This is called from
2315  * ISR/tasklet during error interrupts including IOA unit check. If reset
2316  * is already in progress, it just returns, otherwise initiates IOA reset
2317  * to bring IOA up to operational state.
2318  *
2319  * @pinstance: pointer to adapter instance structure
2320  *
2321  * Return value
2322  *	 none
2323  */
2324 static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
2325 {
2326 	struct pmcraid_cmd *cmd;
2327 
2328 	/* If the reset is already in progress, just return, otherwise start
2329 	 * reset sequence and return
2330 	 */
2331 	if (!pinstance->ioa_reset_in_progress) {
2332 		scsi_block_requests(pinstance->host);
2333 		cmd = pmcraid_get_free_cmd(pinstance);
2334 
2335 		if (cmd == NULL) {
2336 			pmcraid_err("no cmnd blocks for initiate_reset\n");
2337 			return;
2338 		}
2339 
2340 		pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
2341 		pinstance->reset_cmd = cmd;
2342 		pinstance->force_ioa_reset = 1;
2343 		pmcraid_notify_ioastate(pinstance,
2344 					PMC_DEVICE_EVENT_RESET_START);
2345 		pmcraid_ioa_reset(cmd);
2346 	}
2347 }
2348 
2349 /**
2350  * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
2351  *			  or bringdown IOA
2352  * @pinstance: pointer adapter instance structure
2353  * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
2354  * @target_state: expected target state after reset
2355  *
2356  * Note: This command initiates reset and waits for its completion. Hence this
2357  * should not be called from isr/timer/tasklet functions (timeout handlers,
2358  * error response handlers and interrupt handlers).
2359  *
2360  * Return Value
2361  *	 1 in case ioa_state is not target_state, 0 otherwise.
2362  */
2363 static int pmcraid_reset_reload(
2364 	struct pmcraid_instance *pinstance,
2365 	u8 shutdown_type,
2366 	u8 target_state
2367 )
2368 {
2369 	struct pmcraid_cmd *reset_cmd = NULL;
2370 	unsigned long lock_flags;
2371 	int reset = 1;
2372 
2373 	spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2374 
2375 	if (pinstance->ioa_reset_in_progress) {
2376 		pmcraid_info("reset_reload: reset is already in progress\n");
2377 
2378 		spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2379 
2380 		wait_event(pinstance->reset_wait_q,
2381 			   !pinstance->ioa_reset_in_progress);
2382 
2383 		spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2384 
2385 		if (pinstance->ioa_state == IOA_STATE_DEAD) {
2386 			spin_unlock_irqrestore(pinstance->host->host_lock,
2387 					       lock_flags);
2388 			pmcraid_info("reset_reload: IOA is dead\n");
2389 			return reset;
2390 		} else if (pinstance->ioa_state == target_state) {
2391 			reset = 0;
2392 		}
2393 	}
2394 
2395 	if (reset) {
2396 		pmcraid_info("reset_reload: proceeding with reset\n");
2397 		scsi_block_requests(pinstance->host);
2398 		reset_cmd = pmcraid_get_free_cmd(pinstance);
2399 
2400 		if (reset_cmd == NULL) {
2401 			pmcraid_err("no free cmnd for reset_reload\n");
2402 			spin_unlock_irqrestore(pinstance->host->host_lock,
2403 					       lock_flags);
2404 			return reset;
2405 		}
2406 
2407 		if (shutdown_type == SHUTDOWN_NORMAL)
2408 			pinstance->ioa_bringdown = 1;
2409 
2410 		pinstance->ioa_shutdown_type = shutdown_type;
2411 		pinstance->reset_cmd = reset_cmd;
2412 		pinstance->force_ioa_reset = reset;
2413 		pmcraid_info("reset_reload: initiating reset\n");
2414 		pmcraid_ioa_reset(reset_cmd);
2415 		spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2416 		pmcraid_info("reset_reload: waiting for reset to complete\n");
2417 		wait_event(pinstance->reset_wait_q,
2418 			   !pinstance->ioa_reset_in_progress);
2419 
2420 		pmcraid_info("reset_reload: reset is complete !!\n");
2421 		scsi_unblock_requests(pinstance->host);
2422 		if (pinstance->ioa_state == target_state)
2423 			reset = 0;
2424 	}
2425 
2426 	return reset;
2427 }
2428 
2429 /**
2430  * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
2431  *
2432  * @pinstance: pointer to adapter instance structure
2433  *
2434  * Return Value
2435  *	 whatever is returned from pmcraid_reset_reload
2436  */
2437 static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
2438 {
2439 	return pmcraid_reset_reload(pinstance,
2440 				    SHUTDOWN_NORMAL,
2441 				    IOA_STATE_UNKNOWN);
2442 }
2443 
2444 /**
2445  * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
2446  *
2447  * @pinstance: pointer to adapter instance structure
2448  *
2449  * Return Value
2450  *	 whatever is returned from pmcraid_reset_reload
2451  */
2452 static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
2453 {
2454 	pmcraid_notify_ioastate(pinstance, PMC_DEVICE_EVENT_RESET_START);
2455 
2456 	return pmcraid_reset_reload(pinstance,
2457 				    SHUTDOWN_NONE,
2458 				    IOA_STATE_OPERATIONAL);
2459 }
2460 
2461 /**
2462  * pmcraid_request_sense - Send request sense to a device
2463  * @cmd: pmcraid command struct
2464  *
2465  * This function sends a request sense to a device as a result of a check
2466  * condition. This method re-uses the same command block that failed earlier.
2467  */
2468 static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
2469 {
2470 	struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2471 	struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
2472 
2473 	/* allocate DMAable memory for sense buffers */
2474 	cmd->sense_buffer = pci_alloc_consistent(cmd->drv_inst->pdev,
2475 						 SCSI_SENSE_BUFFERSIZE,
2476 						 &cmd->sense_buffer_dma);
2477 
2478 	if (cmd->sense_buffer == NULL) {
2479 		pmcraid_err
2480 			("couldn't allocate sense buffer for request sense\n");
2481 		pmcraid_erp_done(cmd);
2482 		return;
2483 	}
2484 
2485 	/* re-use the command block */
2486 	memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
2487 	memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
2488 	ioarcb->request_flags0 = (SYNC_COMPLETE |
2489 				  NO_LINK_DESCS |
2490 				  INHIBIT_UL_CHECK);
2491 	ioarcb->request_type = REQ_TYPE_SCSI;
2492 	ioarcb->cdb[0] = REQUEST_SENSE;
2493 	ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2494 
2495 	ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
2496 					offsetof(struct pmcraid_ioarcb,
2497 						add_data.u.ioadl[0]));
2498 	ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
2499 
2500 	ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
2501 
2502 	ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
2503 	ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
2504 	ioadl->flags = IOADL_FLAGS_LAST_DESC;
2505 
2506 	/* request sense might be called as part of error response processing
2507 	 * which runs in tasklets context. It is possible that mid-layer might
2508 	 * schedule queuecommand during this time, hence, writting to IOARRIN
2509 	 * must be protect by host_lock
2510 	 */
2511 	pmcraid_send_cmd(cmd, pmcraid_erp_done,
2512 			 PMCRAID_REQUEST_SENSE_TIMEOUT,
2513 			 pmcraid_timeout_handler);
2514 }
2515 
2516 /**
2517  * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
2518  * @cmd: command that failed
2519  * @sense: true if request_sense is required after cancel all
2520  *
2521  * This function sends a cancel all to a device to clear the queue.
2522  */
2523 static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, u32 sense)
2524 {
2525 	struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2526 	struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
2527 	struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
2528 	void (*cmd_done) (struct pmcraid_cmd *) = sense ? pmcraid_erp_done
2529 							: pmcraid_request_sense;
2530 
2531 	memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
2532 	ioarcb->request_flags0 = SYNC_OVERRIDE;
2533 	ioarcb->request_type = REQ_TYPE_IOACMD;
2534 	ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
2535 
2536 	if (RES_IS_GSCSI(res->cfg_entry))
2537 		ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
2538 
2539 	ioarcb->ioadl_bus_addr = 0;
2540 	ioarcb->ioadl_length = 0;
2541 	ioarcb->data_transfer_length = 0;
2542 	ioarcb->ioarcb_bus_addr &= (~0x1FULL);
2543 
2544 	/* writing to IOARRIN must be protected by host_lock, as mid-layer
2545 	 * schedule queuecommand while we are doing this
2546 	 */
2547 	pmcraid_send_cmd(cmd, cmd_done,
2548 			 PMCRAID_REQUEST_SENSE_TIMEOUT,
2549 			 pmcraid_timeout_handler);
2550 }
2551 
2552 /**
2553  * pmcraid_frame_auto_sense: frame fixed format sense information
2554  *
2555  * @cmd: pointer to failing command block
2556  *
2557  * Return value
2558  *  none
2559  */
2560 static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
2561 {
2562 	u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
2563 	struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
2564 	struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
2565 	u32 ioasc = le32_to_cpu(ioasa->ioasc);
2566 	u32 failing_lba = 0;
2567 
2568 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
2569 	cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
2570 
2571 	if (RES_IS_VSET(res->cfg_entry) &&
2572 	    ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
2573 	    ioasa->u.vset.failing_lba_hi != 0) {
2574 
2575 		sense_buf[0] = 0x72;
2576 		sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
2577 		sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
2578 		sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
2579 
2580 		sense_buf[7] = 12;
2581 		sense_buf[8] = 0;
2582 		sense_buf[9] = 0x0A;
2583 		sense_buf[10] = 0x80;
2584 
2585 		failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
2586 
2587 		sense_buf[12] = (failing_lba & 0xff000000) >> 24;
2588 		sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
2589 		sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
2590 		sense_buf[15] = failing_lba & 0x000000ff;
2591 
2592 		failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
2593 
2594 		sense_buf[16] = (failing_lba & 0xff000000) >> 24;
2595 		sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
2596 		sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
2597 		sense_buf[19] = failing_lba & 0x000000ff;
2598 	} else {
2599 		sense_buf[0] = 0x70;
2600 		sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
2601 		sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
2602 		sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
2603 
2604 		if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
2605 			if (RES_IS_VSET(res->cfg_entry))
2606 				failing_lba =
2607 					le32_to_cpu(ioasa->u.
2608 						 vset.failing_lba_lo);
2609 			sense_buf[0] |= 0x80;
2610 			sense_buf[3] = (failing_lba >> 24) & 0xff;
2611 			sense_buf[4] = (failing_lba >> 16) & 0xff;
2612 			sense_buf[5] = (failing_lba >> 8) & 0xff;
2613 			sense_buf[6] = failing_lba & 0xff;
2614 		}
2615 
2616 		sense_buf[7] = 6; /* additional length */
2617 	}
2618 }
2619 
2620 /**
2621  * pmcraid_error_handler - Error response handlers for a SCSI op
2622  * @cmd: pointer to pmcraid_cmd that has failed
2623  *
2624  * This function determines whether or not to initiate ERP on the affected
2625  * device. This is called from a tasklet, which doesn't hold any locks.
2626  *
2627  * Return value:
2628  *	 0 it caller can complete the request, otherwise 1 where in error
2629  *	 handler itself completes the request and returns the command block
2630  *	 back to free-pool
2631  */
2632 static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
2633 {
2634 	struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2635 	struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
2636 	struct pmcraid_instance *pinstance = cmd->drv_inst;
2637 	struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
2638 	u32 ioasc = le32_to_cpu(ioasa->ioasc);
2639 	u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
2640 	u32 sense_copied = 0;
2641 
2642 	if (!res) {
2643 		pmcraid_info("resource pointer is NULL\n");
2644 		return 0;
2645 	}
2646 
2647 	/* If this was a SCSI read/write command keep count of errors */
2648 	if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
2649 		atomic_inc(&res->read_failures);
2650 	else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
2651 		atomic_inc(&res->write_failures);
2652 
2653 	if (!RES_IS_GSCSI(res->cfg_entry) &&
2654 		masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
2655 		pmcraid_frame_auto_sense(cmd);
2656 	}
2657 
2658 	/* Log IOASC/IOASA information based on user settings */
2659 	pmcraid_ioasc_logger(ioasc, cmd);
2660 
2661 	switch (masked_ioasc) {
2662 
2663 	case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
2664 		scsi_cmd->result |= (DID_ABORT << 16);
2665 		break;
2666 
2667 	case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
2668 	case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
2669 		scsi_cmd->result |= (DID_NO_CONNECT << 16);
2670 		break;
2671 
2672 	case PMCRAID_IOASC_NR_SYNC_REQUIRED:
2673 		res->sync_reqd = 1;
2674 		scsi_cmd->result |= (DID_IMM_RETRY << 16);
2675 		break;
2676 
2677 	case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
2678 		scsi_cmd->result |= (DID_PASSTHROUGH << 16);
2679 		break;
2680 
2681 	case PMCRAID_IOASC_UA_BUS_WAS_RESET:
2682 	case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
2683 		if (!res->reset_progress)
2684 			scsi_report_bus_reset(pinstance->host,
2685 					      scsi_cmd->device->channel);
2686 		scsi_cmd->result |= (DID_ERROR << 16);
2687 		break;
2688 
2689 	case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
2690 		scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
2691 		res->sync_reqd = 1;
2692 
2693 		/* if check_condition is not active return with error otherwise
2694 		 * get/frame the sense buffer
2695 		 */
2696 		if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
2697 		    SAM_STAT_CHECK_CONDITION &&
2698 		    PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
2699 			return 0;
2700 
2701 		/* If we have auto sense data as part of IOASA pass it to
2702 		 * mid-layer
2703 		 */
2704 		if (ioasa->auto_sense_length != 0) {
2705 			short sense_len = ioasa->auto_sense_length;
2706 			int data_size = min_t(u16, le16_to_cpu(sense_len),
2707 					      SCSI_SENSE_BUFFERSIZE);
2708 
2709 			memcpy(scsi_cmd->sense_buffer,
2710 			       ioasa->sense_data,
2711 			       data_size);
2712 			sense_copied = 1;
2713 		}
2714 
2715 		if (RES_IS_GSCSI(res->cfg_entry))
2716 			pmcraid_cancel_all(cmd, sense_copied);
2717 		else if (sense_copied)
2718 			pmcraid_erp_done(cmd);
2719 		else
2720 			pmcraid_request_sense(cmd);
2721 
2722 		return 1;
2723 
2724 	case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
2725 		break;
2726 
2727 	default:
2728 		if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
2729 			scsi_cmd->result |= (DID_ERROR << 16);
2730 		break;
2731 	}
2732 	return 0;
2733 }
2734 
2735 /**
2736  * pmcraid_reset_device - device reset handler functions
2737  *
2738  * @scsi_cmd: scsi command struct
2739  * @modifier: reset modifier indicating the reset sequence to be performed
2740  *
2741  * This function issues a device reset to the affected device.
2742  * A LUN reset will be sent to the device first. If that does
2743  * not work, a target reset will be sent.
2744  *
2745  * Return value:
2746  *	SUCCESS / FAILED
2747  */
2748 static int pmcraid_reset_device(
2749 	struct scsi_cmnd *scsi_cmd,
2750 	unsigned long timeout,
2751 	u8 modifier
2752 )
2753 {
2754 	struct pmcraid_cmd *cmd;
2755 	struct pmcraid_instance *pinstance;
2756 	struct pmcraid_resource_entry *res;
2757 	struct pmcraid_ioarcb *ioarcb;
2758 	unsigned long lock_flags;
2759 	u32 ioasc;
2760 
2761 	pinstance =
2762 		(struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
2763 	res = scsi_cmd->device->hostdata;
2764 
2765 	if (!res) {
2766 		sdev_printk(KERN_ERR, scsi_cmd->device,
2767 			    "reset_device: NULL resource pointer\n");
2768 		return FAILED;
2769 	}
2770 
2771 	/* If adapter is currently going through reset/reload, return failed.
2772 	 * This will force the mid-layer to call _eh_bus/host reset, which
2773 	 * will then go to sleep and wait for the reset to complete
2774 	 */
2775 	spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
2776 	if (pinstance->ioa_reset_in_progress ||
2777 	    pinstance->ioa_state == IOA_STATE_DEAD) {
2778 		spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2779 		return FAILED;
2780 	}
2781 
2782 	res->reset_progress = 1;
2783 	pmcraid_info("Resetting %s resource with addr %x\n",
2784 		     ((modifier & RESET_DEVICE_LUN) ? "LUN" :
2785 		     ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
2786 		     le32_to_cpu(res->cfg_entry.resource_address));
2787 
2788 	/* get a free cmd block */
2789 	cmd = pmcraid_get_free_cmd(pinstance);
2790 
2791 	if (cmd == NULL) {
2792 		spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2793 		pmcraid_err("%s: no cmd blocks are available\n", __func__);
2794 		return FAILED;
2795 	}
2796 
2797 	ioarcb = &cmd->ioa_cb->ioarcb;
2798 	ioarcb->resource_handle = res->cfg_entry.resource_handle;
2799 	ioarcb->request_type = REQ_TYPE_IOACMD;
2800 	ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
2801 
2802 	/* Initialize reset modifier bits */
2803 	if (modifier)
2804 		modifier = ENABLE_RESET_MODIFIER | modifier;
2805 
2806 	ioarcb->cdb[1] = modifier;
2807 
2808 	init_completion(&cmd->wait_for_completion);
2809 	cmd->completion_req = 1;
2810 
2811 	pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
2812 		     cmd->ioa_cb->ioarcb.cdb[0],
2813 		     le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
2814 		     le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
2815 
2816 	pmcraid_send_cmd(cmd,
2817 			 pmcraid_internal_done,
2818 			 timeout,
2819 			 pmcraid_timeout_handler);
2820 
2821 	spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
2822 
2823 	/* RESET_DEVICE command completes after all pending IOARCBs are
2824 	 * completed. Once this command is completed, pmcraind_internal_done
2825 	 * will wake up the 'completion' queue.
2826 	 */
2827 	wait_for_completion(&cmd->wait_for_completion);
2828 
2829 	/* complete the command here itself and return the command block
2830 	 * to free list
2831 	 */
2832 	pmcraid_return_cmd(cmd);
2833 	res->reset_progress = 0;
2834 	ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
2835 
2836 	/* set the return value based on the returned ioasc */
2837 	return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
2838 }
2839 
2840 /**
2841  * _pmcraid_io_done - helper for pmcraid_io_done function
2842  *
2843  * @cmd: pointer to pmcraid command struct
2844  * @reslen: residual data length to be set in the ioasa
2845  * @ioasc: ioasc either returned by IOA or set by driver itself.
2846  *
2847  * This function is invoked by pmcraid_io_done to complete mid-layer
2848  * scsi ops.
2849  *
2850  * Return value:
2851  *	  0 if caller is required to return it to free_pool. Returns 1 if
2852  *	  caller need not worry about freeing command block as error handler
2853  *	  will take care of that.
2854  */
2855 
2856 static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
2857 {
2858 	struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
2859 	int rc = 0;
2860 
2861 	scsi_set_resid(scsi_cmd, reslen);
2862 
2863 	pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
2864 		le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
2865 		cmd->ioa_cb->ioarcb.cdb[0],
2866 		ioasc, scsi_cmd->result);
2867 
2868 	if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
2869 		rc = pmcraid_error_handler(cmd);
2870 
2871 	if (rc == 0) {
2872 		scsi_dma_unmap(scsi_cmd);
2873 		scsi_cmd->scsi_done(scsi_cmd);
2874 	}
2875 
2876 	return rc;
2877 }
2878 
2879 /**
2880  * pmcraid_io_done - SCSI completion function
2881  *
2882  * @cmd: pointer to pmcraid command struct
2883  *
2884  * This function is invoked by tasklet/mid-layer error handler to completing
2885  * the SCSI ops sent from mid-layer.
2886  *
2887  * Return value
2888  *	  none
2889  */
2890 
2891 static void pmcraid_io_done(struct pmcraid_cmd *cmd)
2892 {
2893 	u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
2894 	u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
2895 
2896 	if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
2897 		pmcraid_return_cmd(cmd);
2898 }
2899 
2900 /**
2901  * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
2902  *
2903  * @cmd: command block of the command to be aborted
2904  *
2905  * Return Value:
2906  *	 returns pointer to command structure used as cancelling cmd
2907  */
2908 static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
2909 {
2910 	struct pmcraid_cmd *cancel_cmd;
2911 	struct pmcraid_instance *pinstance;
2912 	struct pmcraid_resource_entry *res;
2913 
2914 	pinstance = (struct pmcraid_instance *)cmd->drv_inst;
2915 	res = cmd->scsi_cmd->device->hostdata;
2916 
2917 	cancel_cmd = pmcraid_get_free_cmd(pinstance);
2918 
2919 	if (cancel_cmd == NULL) {
2920 		pmcraid_err("%s: no cmd blocks are available\n", __func__);
2921 		return NULL;
2922 	}
2923 
2924 	pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
2925 
2926 	pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
2927 		cmd->ioa_cb->ioarcb.cdb[0],
2928 		cmd->ioa_cb->ioarcb.response_handle >> 2);
2929 
2930 	init_completion(&cancel_cmd->wait_for_completion);
2931 	cancel_cmd->completion_req = 1;
2932 
2933 	pmcraid_info("command (%d) CDB[0] = %x for %x\n",
2934 		le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
2935 		cancel_cmd->ioa_cb->ioarcb.cdb[0],
2936 		le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
2937 
2938 	pmcraid_send_cmd(cancel_cmd,
2939 			 pmcraid_internal_done,
2940 			 PMCRAID_INTERNAL_TIMEOUT,
2941 			 pmcraid_timeout_handler);
2942 	return cancel_cmd;
2943 }
2944 
2945 /**
2946  * pmcraid_abort_complete - Waits for ABORT TASK completion
2947  *
2948  * @cancel_cmd: command block use as cancelling command
2949  *
2950  * Return Value:
2951  *	 returns SUCCESS if ABORT TASK has good completion
2952  *	 otherwise FAILED
2953  */
2954 static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
2955 {
2956 	struct pmcraid_resource_entry *res;
2957 	u32 ioasc;
2958 
2959 	wait_for_completion(&cancel_cmd->wait_for_completion);
2960 	res = cancel_cmd->res;
2961 	cancel_cmd->res = NULL;
2962 	ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
2963 
2964 	/* If the abort task is not timed out we will get a Good completion
2965 	 * as sense_key, otherwise we may get one the following responses
2966 	 * due to subsequent bus reset or device reset. In case IOASC is
2967 	 * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
2968 	 */
2969 	if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
2970 	    ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
2971 		if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
2972 			res->sync_reqd = 1;
2973 		ioasc = 0;
2974 	}
2975 
2976 	/* complete the command here itself */
2977 	pmcraid_return_cmd(cancel_cmd);
2978 	return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
2979 }
2980 
2981 /**
2982  * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
2983  *
2984  * @scsi_cmd:   scsi command struct given by mid-layer. When this is called
2985  *		mid-layer ensures that no other commands are queued. This
2986  *		never gets called under interrupt, but a separate eh thread.
2987  *
2988  * Return value:
2989  *	 SUCCESS / FAILED
2990  */
2991 static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
2992 {
2993 	struct pmcraid_instance *pinstance;
2994 	struct pmcraid_cmd *cmd;
2995 	struct pmcraid_resource_entry *res;
2996 	unsigned long host_lock_flags;
2997 	unsigned long pending_lock_flags;
2998 	struct pmcraid_cmd *cancel_cmd = NULL;
2999 	int cmd_found = 0;
3000 	int rc = FAILED;
3001 
3002 	pinstance =
3003 		(struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
3004 
3005 	scmd_printk(KERN_INFO, scsi_cmd,
3006 		    "I/O command timed out, aborting it.\n");
3007 
3008 	res = scsi_cmd->device->hostdata;
3009 
3010 	if (res == NULL)
3011 		return rc;
3012 
3013 	/* If we are currently going through reset/reload, return failed.
3014 	 * This will force the mid-layer to eventually call
3015 	 * pmcraid_eh_host_reset which will then go to sleep and wait for the
3016 	 * reset to complete
3017 	 */
3018 	spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
3019 
3020 	if (pinstance->ioa_reset_in_progress ||
3021 	    pinstance->ioa_state == IOA_STATE_DEAD) {
3022 		spin_unlock_irqrestore(pinstance->host->host_lock,
3023 				       host_lock_flags);
3024 		return rc;
3025 	}
3026 
3027 	/* loop over pending cmd list to find cmd corresponding to this
3028 	 * scsi_cmd. Note that this command might not have been completed
3029 	 * already. locking: all pending commands are protected with
3030 	 * pending_pool_lock.
3031 	 */
3032 	spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
3033 	list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
3034 
3035 		if (cmd->scsi_cmd == scsi_cmd) {
3036 			cmd_found = 1;
3037 			break;
3038 		}
3039 	}
3040 
3041 	spin_unlock_irqrestore(&pinstance->pending_pool_lock,
3042 				pending_lock_flags);
3043 
3044 	/* If the command to be aborted was given to IOA and still pending with
3045 	 * it, send ABORT_TASK to abort this and wait for its completion
3046 	 */
3047 	if (cmd_found)
3048 		cancel_cmd = pmcraid_abort_cmd(cmd);
3049 
3050 	spin_unlock_irqrestore(pinstance->host->host_lock,
3051 			       host_lock_flags);
3052 
3053 	if (cancel_cmd) {
3054 		cancel_cmd->res = cmd->scsi_cmd->device->hostdata;
3055 		rc = pmcraid_abort_complete(cancel_cmd);
3056 	}
3057 
3058 	return cmd_found ? rc : SUCCESS;
3059 }
3060 
3061 /**
3062  * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
3063  *
3064  * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
3065  *
3066  * All these routines invokve pmcraid_reset_device with appropriate parameters.
3067  * Since these are called from mid-layer EH thread, no other IO will be queued
3068  * to the resource being reset. However, control path (IOCTL) may be active so
3069  * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
3070  * takes care by locking/unlocking host_lock.
3071  *
3072  * Return value
3073  *	SUCCESS or FAILED
3074  */
3075 static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
3076 {
3077 	scmd_printk(KERN_INFO, scmd,
3078 		    "resetting device due to an I/O command timeout.\n");
3079 	return pmcraid_reset_device(scmd,
3080 				    PMCRAID_INTERNAL_TIMEOUT,
3081 				    RESET_DEVICE_LUN);
3082 }
3083 
3084 static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
3085 {
3086 	scmd_printk(KERN_INFO, scmd,
3087 		    "Doing bus reset due to an I/O command timeout.\n");
3088 	return pmcraid_reset_device(scmd,
3089 				    PMCRAID_RESET_BUS_TIMEOUT,
3090 				    RESET_DEVICE_BUS);
3091 }
3092 
3093 static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
3094 {
3095 	scmd_printk(KERN_INFO, scmd,
3096 		    "Doing target reset due to an I/O command timeout.\n");
3097 	return pmcraid_reset_device(scmd,
3098 				    PMCRAID_INTERNAL_TIMEOUT,
3099 				    RESET_DEVICE_TARGET);
3100 }
3101 
3102 /**
3103  * pmcraid_eh_host_reset_handler - adapter reset handler callback
3104  *
3105  * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
3106  *
3107  * Initiates adapter reset to bring it up to operational state
3108  *
3109  * Return value
3110  *	SUCCESS or FAILED
3111  */
3112 static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
3113 {
3114 	unsigned long interval = 10000; /* 10 seconds interval */
3115 	int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
3116 	struct pmcraid_instance *pinstance =
3117 		(struct pmcraid_instance *)(scmd->device->host->hostdata);
3118 
3119 
3120 	/* wait for an additional 150 seconds just in case firmware could come
3121 	 * up and if it could complete all the pending commands excluding the
3122 	 * two HCAM (CCN and LDN).
3123 	 */
3124 	while (waits--) {
3125 		if (atomic_read(&pinstance->outstanding_cmds) <=
3126 		    PMCRAID_MAX_HCAM_CMD)
3127 			return SUCCESS;
3128 		msleep(interval);
3129 	}
3130 
3131 	dev_err(&pinstance->pdev->dev,
3132 		"Adapter being reset due to an I/O command timeout.\n");
3133 	return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
3134 }
3135 
3136 /**
3137  * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
3138  * @cmd: pmcraid command struct
3139  * @sgcount: count of scatter-gather elements
3140  *
3141  * Return value
3142  *   returns pointer pmcraid_ioadl_desc, initialized to point to internal
3143  *   or external IOADLs
3144  */
3145 struct pmcraid_ioadl_desc *
3146 pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
3147 {
3148 	struct pmcraid_ioadl_desc *ioadl;
3149 	struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
3150 	int ioadl_count = 0;
3151 
3152 	if (ioarcb->add_cmd_param_length)
3153 		ioadl_count = DIV_ROUND_UP(ioarcb->add_cmd_param_length, 16);
3154 	ioarcb->ioadl_length =
3155 		sizeof(struct pmcraid_ioadl_desc) * sgcount;
3156 
3157 	if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
3158 		/* external ioadls start at offset 0x80 from control_block
3159 		 * structure, re-using 24 out of 27 ioadls part of IOARCB.
3160 		 * It is necessary to indicate to firmware that driver is
3161 		 * using ioadls to be treated as external to IOARCB.
3162 		 */
3163 		ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
3164 		ioarcb->ioadl_bus_addr =
3165 			cpu_to_le64((cmd->ioa_cb_bus_addr) +
3166 				offsetof(struct pmcraid_ioarcb,
3167 					add_data.u.ioadl[3]));
3168 		ioadl = &ioarcb->add_data.u.ioadl[3];
3169 	} else {
3170 		ioarcb->ioadl_bus_addr =
3171 			cpu_to_le64((cmd->ioa_cb_bus_addr) +
3172 				offsetof(struct pmcraid_ioarcb,
3173 					add_data.u.ioadl[ioadl_count]));
3174 
3175 		ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
3176 		ioarcb->ioarcb_bus_addr |=
3177 				DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8);
3178 	}
3179 
3180 	return ioadl;
3181 }
3182 
3183 /**
3184  * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
3185  * @pinstance: pointer to adapter instance structure
3186  * @cmd: pmcraid command struct
3187  *
3188  * This function is invoked by queuecommand entry point while sending a command
3189  * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
3190  *
3191  * Return value:
3192  *	0 on success or -1 on failure
3193  */
3194 static int pmcraid_build_ioadl(
3195 	struct pmcraid_instance *pinstance,
3196 	struct pmcraid_cmd *cmd
3197 )
3198 {
3199 	int i, nseg;
3200 	struct scatterlist *sglist;
3201 
3202 	struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
3203 	struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
3204 	struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
3205 
3206 	u32 length = scsi_bufflen(scsi_cmd);
3207 
3208 	if (!length)
3209 		return 0;
3210 
3211 	nseg = scsi_dma_map(scsi_cmd);
3212 
3213 	if (nseg < 0) {
3214 		scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
3215 		return -1;
3216 	} else if (nseg > PMCRAID_MAX_IOADLS) {
3217 		scsi_dma_unmap(scsi_cmd);
3218 		scmd_printk(KERN_ERR, scsi_cmd,
3219 			"sg count is (%d) more than allowed!\n", nseg);
3220 		return -1;
3221 	}
3222 
3223 	/* Initialize IOARCB data transfer length fields */
3224 	if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
3225 		ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
3226 
3227 	ioarcb->request_flags0 |= NO_LINK_DESCS;
3228 	ioarcb->data_transfer_length = cpu_to_le32(length);
3229 	ioadl = pmcraid_init_ioadls(cmd, nseg);
3230 
3231 	/* Initialize IOADL descriptor addresses */
3232 	scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
3233 		ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
3234 		ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
3235 		ioadl[i].flags = 0;
3236 	}
3237 	/* setup last descriptor */
3238 	ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
3239 
3240 	return 0;
3241 }
3242 
3243 /**
3244  * pmcraid_free_sglist - Frees an allocated SG buffer list
3245  * @sglist: scatter/gather list pointer
3246  *
3247  * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
3248  *
3249  * Return value:
3250  *	none
3251  */
3252 static void pmcraid_free_sglist(struct pmcraid_sglist *sglist)
3253 {
3254 	int i;
3255 
3256 	for (i = 0; i < sglist->num_sg; i++)
3257 		__free_pages(sg_page(&(sglist->scatterlist[i])),
3258 			     sglist->order);
3259 
3260 	kfree(sglist);
3261 }
3262 
3263 /**
3264  * pmcraid_alloc_sglist - Allocates memory for a SG list
3265  * @buflen: buffer length
3266  *
3267  * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
3268  * list.
3269  *
3270  * Return value
3271  *	pointer to sglist / NULL on failure
3272  */
3273 static struct pmcraid_sglist *pmcraid_alloc_sglist(int buflen)
3274 {
3275 	struct pmcraid_sglist *sglist;
3276 	struct scatterlist *scatterlist;
3277 	struct page *page;
3278 	int num_elem, i, j;
3279 	int sg_size;
3280 	int order;
3281 	int bsize_elem;
3282 
3283 	sg_size = buflen / (PMCRAID_MAX_IOADLS - 1);
3284 	order = (sg_size > 0) ? get_order(sg_size) : 0;
3285 	bsize_elem = PAGE_SIZE * (1 << order);
3286 
3287 	/* Determine the actual number of sg entries needed */
3288 	if (buflen % bsize_elem)
3289 		num_elem = (buflen / bsize_elem) + 1;
3290 	else
3291 		num_elem = buflen / bsize_elem;
3292 
3293 	/* Allocate a scatter/gather list for the DMA */
3294 	sglist = kzalloc(sizeof(struct pmcraid_sglist) +
3295 			 (sizeof(struct scatterlist) * (num_elem - 1)),
3296 			 GFP_KERNEL);
3297 
3298 	if (sglist == NULL)
3299 		return NULL;
3300 
3301 	scatterlist = sglist->scatterlist;
3302 	sg_init_table(scatterlist, num_elem);
3303 	sglist->order = order;
3304 	sglist->num_sg = num_elem;
3305 	sg_size = buflen;
3306 
3307 	for (i = 0; i < num_elem; i++) {
3308 		page = alloc_pages(GFP_KERNEL|GFP_DMA|__GFP_ZERO, order);
3309 		if (!page) {
3310 			for (j = i - 1; j >= 0; j--)
3311 				__free_pages(sg_page(&scatterlist[j]), order);
3312 			kfree(sglist);
3313 			return NULL;
3314 		}
3315 
3316 		sg_set_page(&scatterlist[i], page,
3317 			sg_size < bsize_elem ? sg_size : bsize_elem, 0);
3318 		sg_size -= bsize_elem;
3319 	}
3320 
3321 	return sglist;
3322 }
3323 
3324 /**
3325  * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
3326  * @sglist: scatter/gather list pointer
3327  * @buffer: buffer pointer
3328  * @len: buffer length
3329  * @direction: data transfer direction
3330  *
3331  * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
3332  *
3333  * Return value:
3334  * 0 on success / other on failure
3335  */
3336 static int pmcraid_copy_sglist(
3337 	struct pmcraid_sglist *sglist,
3338 	unsigned long buffer,
3339 	u32 len,
3340 	int direction
3341 )
3342 {
3343 	struct scatterlist *scatterlist;
3344 	void *kaddr;
3345 	int bsize_elem;
3346 	int i;
3347 	int rc = 0;
3348 
3349 	/* Determine the actual number of bytes per element */
3350 	bsize_elem = PAGE_SIZE * (1 << sglist->order);
3351 
3352 	scatterlist = sglist->scatterlist;
3353 
3354 	for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
3355 		struct page *page = sg_page(&scatterlist[i]);
3356 
3357 		kaddr = kmap(page);
3358 		if (direction == DMA_TO_DEVICE)
3359 			rc = __copy_from_user(kaddr,
3360 					      (void *)buffer,
3361 					      bsize_elem);
3362 		else
3363 			rc = __copy_to_user((void *)buffer, kaddr, bsize_elem);
3364 
3365 		kunmap(page);
3366 
3367 		if (rc) {
3368 			pmcraid_err("failed to copy user data into sg list\n");
3369 			return -EFAULT;
3370 		}
3371 
3372 		scatterlist[i].length = bsize_elem;
3373 	}
3374 
3375 	if (len % bsize_elem) {
3376 		struct page *page = sg_page(&scatterlist[i]);
3377 
3378 		kaddr = kmap(page);
3379 
3380 		if (direction == DMA_TO_DEVICE)
3381 			rc = __copy_from_user(kaddr,
3382 					      (void *)buffer,
3383 					      len % bsize_elem);
3384 		else
3385 			rc = __copy_to_user((void *)buffer,
3386 					    kaddr,
3387 					    len % bsize_elem);
3388 
3389 		kunmap(page);
3390 
3391 		scatterlist[i].length = len % bsize_elem;
3392 	}
3393 
3394 	if (rc) {
3395 		pmcraid_err("failed to copy user data into sg list\n");
3396 		rc = -EFAULT;
3397 	}
3398 
3399 	return rc;
3400 }
3401 
3402 /**
3403  * pmcraid_queuecommand - Queue a mid-layer request
3404  * @scsi_cmd: scsi command struct
3405  * @done: done function
3406  *
3407  * This function queues a request generated by the mid-layer. Midlayer calls
3408  * this routine within host->lock. Some of the functions called by queuecommand
3409  * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
3410  *
3411  * Return value:
3412  *	  0 on success
3413  *	  SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3414  *	  SCSI_MLQUEUE_HOST_BUSY if host is busy
3415  */
3416 static int pmcraid_queuecommand_lck(
3417 	struct scsi_cmnd *scsi_cmd,
3418 	void (*done) (struct scsi_cmnd *)
3419 )
3420 {
3421 	struct pmcraid_instance *pinstance;
3422 	struct pmcraid_resource_entry *res;
3423 	struct pmcraid_ioarcb *ioarcb;
3424 	struct pmcraid_cmd *cmd;
3425 	u32 fw_version;
3426 	int rc = 0;
3427 
3428 	pinstance =
3429 		(struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
3430 	fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
3431 	scsi_cmd->scsi_done = done;
3432 	res = scsi_cmd->device->hostdata;
3433 	scsi_cmd->result = (DID_OK << 16);
3434 
3435 	/* if adapter is marked as dead, set result to DID_NO_CONNECT complete
3436 	 * the command
3437 	 */
3438 	if (pinstance->ioa_state == IOA_STATE_DEAD) {
3439 		pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
3440 		scsi_cmd->result = (DID_NO_CONNECT << 16);
3441 		scsi_cmd->scsi_done(scsi_cmd);
3442 		return 0;
3443 	}
3444 
3445 	/* If IOA reset is in progress, can't queue the commands */
3446 	if (pinstance->ioa_reset_in_progress)
3447 		return SCSI_MLQUEUE_HOST_BUSY;
3448 
3449 	/* Firmware doesn't support SYNCHRONIZE_CACHE command (0x35), complete
3450 	 * the command here itself with success return
3451 	 */
3452 	if (scsi_cmd->cmnd[0] == SYNCHRONIZE_CACHE) {
3453 		pmcraid_info("SYNC_CACHE(0x35), completing in driver itself\n");
3454 		scsi_cmd->scsi_done(scsi_cmd);
3455 		return 0;
3456 	}
3457 
3458 	/* initialize the command and IOARCB to be sent to IOA */
3459 	cmd = pmcraid_get_free_cmd(pinstance);
3460 
3461 	if (cmd == NULL) {
3462 		pmcraid_err("free command block is not available\n");
3463 		return SCSI_MLQUEUE_HOST_BUSY;
3464 	}
3465 
3466 	cmd->scsi_cmd = scsi_cmd;
3467 	ioarcb = &(cmd->ioa_cb->ioarcb);
3468 	memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
3469 	ioarcb->resource_handle = res->cfg_entry.resource_handle;
3470 	ioarcb->request_type = REQ_TYPE_SCSI;
3471 
3472 	/* set hrrq number where the IOA should respond to. Note that all cmds
3473 	 * generated internally uses hrrq_id 0, exception to this is the cmd
3474 	 * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
3475 	 * hrrq_id assigned here in queuecommand
3476 	 */
3477 	ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
3478 			  pinstance->num_hrrq;
3479 	cmd->cmd_done = pmcraid_io_done;
3480 
3481 	if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
3482 		if (scsi_cmd->underflow == 0)
3483 			ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
3484 
3485 		if (res->sync_reqd) {
3486 			ioarcb->request_flags0 |= SYNC_COMPLETE;
3487 			res->sync_reqd = 0;
3488 		}
3489 
3490 		ioarcb->request_flags0 |= NO_LINK_DESCS;
3491 
3492 		if (scsi_cmd->flags & SCMD_TAGGED)
3493 			ioarcb->request_flags1 |= TASK_TAG_SIMPLE;
3494 
3495 		if (RES_IS_GSCSI(res->cfg_entry))
3496 			ioarcb->request_flags1 |= DELAY_AFTER_RESET;
3497 	}
3498 
3499 	rc = pmcraid_build_ioadl(pinstance, cmd);
3500 
3501 	pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
3502 		     le32_to_cpu(ioarcb->response_handle) >> 2,
3503 		     scsi_cmd->cmnd[0], pinstance->host->unique_id,
3504 		     RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
3505 			PMCRAID_PHYS_BUS_ID,
3506 		     RES_IS_VSET(res->cfg_entry) ?
3507 			(fw_version <= PMCRAID_FW_VERSION_1 ?
3508 				res->cfg_entry.unique_flags1 :
3509 					res->cfg_entry.array_id & 0xFF) :
3510 			RES_TARGET(res->cfg_entry.resource_address),
3511 		     RES_LUN(res->cfg_entry.resource_address));
3512 
3513 	if (likely(rc == 0)) {
3514 		_pmcraid_fire_command(cmd);
3515 	} else {
3516 		pmcraid_err("queuecommand could not build ioadl\n");
3517 		pmcraid_return_cmd(cmd);
3518 		rc = SCSI_MLQUEUE_HOST_BUSY;
3519 	}
3520 
3521 	return rc;
3522 }
3523 
3524 static DEF_SCSI_QCMD(pmcraid_queuecommand)
3525 
3526 /**
3527  * pmcraid_open -char node "open" entry, allowed only users with admin access
3528  */
3529 static int pmcraid_chr_open(struct inode *inode, struct file *filep)
3530 {
3531 	struct pmcraid_instance *pinstance;
3532 
3533 	if (!capable(CAP_SYS_ADMIN))
3534 		return -EACCES;
3535 
3536 	/* Populate adapter instance * pointer for use by ioctl */
3537 	pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
3538 	filep->private_data = pinstance;
3539 
3540 	return 0;
3541 }
3542 
3543 /**
3544  * pmcraid_fasync - Async notifier registration from applications
3545  *
3546  * This function adds the calling process to a driver global queue. When an
3547  * event occurs, SIGIO will be sent to all processes in this queue.
3548  */
3549 static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
3550 {
3551 	struct pmcraid_instance *pinstance;
3552 	int rc;
3553 
3554 	pinstance = filep->private_data;
3555 	mutex_lock(&pinstance->aen_queue_lock);
3556 	rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
3557 	mutex_unlock(&pinstance->aen_queue_lock);
3558 
3559 	return rc;
3560 }
3561 
3562 
3563 /**
3564  * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
3565  * commands sent over IOCTL interface
3566  *
3567  * @cmd       : pointer to struct pmcraid_cmd
3568  * @buflen    : length of the request buffer
3569  * @direction : data transfer direction
3570  *
3571  * Return value
3572  *  0 on success, non-zero error code on failure
3573  */
3574 static int pmcraid_build_passthrough_ioadls(
3575 	struct pmcraid_cmd *cmd,
3576 	int buflen,
3577 	int direction
3578 )
3579 {
3580 	struct pmcraid_sglist *sglist = NULL;
3581 	struct scatterlist *sg = NULL;
3582 	struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
3583 	struct pmcraid_ioadl_desc *ioadl;
3584 	int i;
3585 
3586 	sglist = pmcraid_alloc_sglist(buflen);
3587 
3588 	if (!sglist) {
3589 		pmcraid_err("can't allocate memory for passthrough SGls\n");
3590 		return -ENOMEM;
3591 	}
3592 
3593 	sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
3594 					sglist->scatterlist,
3595 					sglist->num_sg, direction);
3596 
3597 	if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
3598 		dev_err(&cmd->drv_inst->pdev->dev,
3599 			"Failed to map passthrough buffer!\n");
3600 		pmcraid_free_sglist(sglist);
3601 		return -EIO;
3602 	}
3603 
3604 	cmd->sglist = sglist;
3605 	ioarcb->request_flags0 |= NO_LINK_DESCS;
3606 
3607 	ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
3608 
3609 	/* Initialize IOADL descriptor addresses */
3610 	for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
3611 		ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
3612 		ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
3613 		ioadl[i].flags = 0;
3614 	}
3615 
3616 	/* setup the last descriptor */
3617 	ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
3618 
3619 	return 0;
3620 }
3621 
3622 
3623 /**
3624  * pmcraid_release_passthrough_ioadls - release passthrough ioadls
3625  *
3626  * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
3627  * @buflen: size of the request buffer
3628  * @direction: data transfer direction
3629  *
3630  * Return value
3631  *  0 on success, non-zero error code on failure
3632  */
3633 static void pmcraid_release_passthrough_ioadls(
3634 	struct pmcraid_cmd *cmd,
3635 	int buflen,
3636 	int direction
3637 )
3638 {
3639 	struct pmcraid_sglist *sglist = cmd->sglist;
3640 
3641 	if (buflen > 0) {
3642 		pci_unmap_sg(cmd->drv_inst->pdev,
3643 			     sglist->scatterlist,
3644 			     sglist->num_sg,
3645 			     direction);
3646 		pmcraid_free_sglist(sglist);
3647 		cmd->sglist = NULL;
3648 	}
3649 }
3650 
3651 /**
3652  * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
3653  *
3654  * @pinstance: pointer to adapter instance structure
3655  * @cmd: ioctl code
3656  * @arg: pointer to pmcraid_passthrough_buffer user buffer
3657  *
3658  * Return value
3659  *  0 on success, non-zero error code on failure
3660  */
3661 static long pmcraid_ioctl_passthrough(
3662 	struct pmcraid_instance *pinstance,
3663 	unsigned int ioctl_cmd,
3664 	unsigned int buflen,
3665 	unsigned long arg
3666 )
3667 {
3668 	struct pmcraid_passthrough_ioctl_buffer *buffer;
3669 	struct pmcraid_ioarcb *ioarcb;
3670 	struct pmcraid_cmd *cmd;
3671 	struct pmcraid_cmd *cancel_cmd;
3672 	unsigned long request_buffer;
3673 	unsigned long request_offset;
3674 	unsigned long lock_flags;
3675 	void *ioasa;
3676 	u32 ioasc;
3677 	int request_size;
3678 	int buffer_size;
3679 	u8 access, direction;
3680 	int rc = 0;
3681 
3682 	/* If IOA reset is in progress, wait 10 secs for reset to complete */
3683 	if (pinstance->ioa_reset_in_progress) {
3684 		rc = wait_event_interruptible_timeout(
3685 				pinstance->reset_wait_q,
3686 				!pinstance->ioa_reset_in_progress,
3687 				msecs_to_jiffies(10000));
3688 
3689 		if (!rc)
3690 			return -ETIMEDOUT;
3691 		else if (rc < 0)
3692 			return -ERESTARTSYS;
3693 	}
3694 
3695 	/* If adapter is not in operational state, return error */
3696 	if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
3697 		pmcraid_err("IOA is not operational\n");
3698 		return -ENOTTY;
3699 	}
3700 
3701 	buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
3702 	buffer = kmalloc(buffer_size, GFP_KERNEL);
3703 
3704 	if (!buffer) {
3705 		pmcraid_err("no memory for passthrough buffer\n");
3706 		return -ENOMEM;
3707 	}
3708 
3709 	request_offset =
3710 	    offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
3711 
3712 	request_buffer = arg + request_offset;
3713 
3714 	rc = __copy_from_user(buffer,
3715 			     (struct pmcraid_passthrough_ioctl_buffer *) arg,
3716 			     sizeof(struct pmcraid_passthrough_ioctl_buffer));
3717 
3718 	ioasa =
3719 	(void *)(arg +
3720 		offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
3721 
3722 	if (rc) {
3723 		pmcraid_err("ioctl: can't copy passthrough buffer\n");
3724 		rc = -EFAULT;
3725 		goto out_free_buffer;
3726 	}
3727 
3728 	request_size = buffer->ioarcb.data_transfer_length;
3729 
3730 	if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
3731 		access = VERIFY_READ;
3732 		direction = DMA_TO_DEVICE;
3733 	} else {
3734 		access = VERIFY_WRITE;
3735 		direction = DMA_FROM_DEVICE;
3736 	}
3737 
3738 	if (request_size > 0) {
3739 		rc = access_ok(access, arg, request_offset + request_size);
3740 
3741 		if (!rc) {
3742 			rc = -EFAULT;
3743 			goto out_free_buffer;
3744 		}
3745 	} else if (request_size < 0) {
3746 		rc = -EINVAL;
3747 		goto out_free_buffer;
3748 	}
3749 
3750 	/* check if we have any additional command parameters */
3751 	if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
3752 		rc = -EINVAL;
3753 		goto out_free_buffer;
3754 	}
3755 
3756 	cmd = pmcraid_get_free_cmd(pinstance);
3757 
3758 	if (!cmd) {
3759 		pmcraid_err("free command block is not available\n");
3760 		rc = -ENOMEM;
3761 		goto out_free_buffer;
3762 	}
3763 
3764 	cmd->scsi_cmd = NULL;
3765 	ioarcb = &(cmd->ioa_cb->ioarcb);
3766 
3767 	/* Copy the user-provided IOARCB stuff field by field */
3768 	ioarcb->resource_handle = buffer->ioarcb.resource_handle;
3769 	ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
3770 	ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
3771 	ioarcb->request_type = buffer->ioarcb.request_type;
3772 	ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
3773 	ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
3774 	memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
3775 
3776 	if (buffer->ioarcb.add_cmd_param_length) {
3777 		ioarcb->add_cmd_param_length =
3778 			buffer->ioarcb.add_cmd_param_length;
3779 		ioarcb->add_cmd_param_offset =
3780 			buffer->ioarcb.add_cmd_param_offset;
3781 		memcpy(ioarcb->add_data.u.add_cmd_params,
3782 			buffer->ioarcb.add_data.u.add_cmd_params,
3783 			buffer->ioarcb.add_cmd_param_length);
3784 	}
3785 
3786 	/* set hrrq number where the IOA should respond to. Note that all cmds
3787 	 * generated internally uses hrrq_id 0, exception to this is the cmd
3788 	 * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
3789 	 * hrrq_id assigned here in queuecommand
3790 	 */
3791 	ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
3792 			  pinstance->num_hrrq;
3793 
3794 	if (request_size) {
3795 		rc = pmcraid_build_passthrough_ioadls(cmd,
3796 						      request_size,
3797 						      direction);
3798 		if (rc) {
3799 			pmcraid_err("couldn't build passthrough ioadls\n");
3800 			goto out_free_buffer;
3801 		}
3802 	} else if (request_size < 0) {
3803 		rc = -EINVAL;
3804 		goto out_free_buffer;
3805 	}
3806 
3807 	/* If data is being written into the device, copy the data from user
3808 	 * buffers
3809 	 */
3810 	if (direction == DMA_TO_DEVICE && request_size > 0) {
3811 		rc = pmcraid_copy_sglist(cmd->sglist,
3812 					 request_buffer,
3813 					 request_size,
3814 					 direction);
3815 		if (rc) {
3816 			pmcraid_err("failed to copy user buffer\n");
3817 			goto out_free_sglist;
3818 		}
3819 	}
3820 
3821 	/* passthrough ioctl is a blocking command so, put the user to sleep
3822 	 * until timeout. Note that a timeout value of 0 means, do timeout.
3823 	 */
3824 	cmd->cmd_done = pmcraid_internal_done;
3825 	init_completion(&cmd->wait_for_completion);
3826 	cmd->completion_req = 1;
3827 
3828 	pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
3829 		     le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
3830 		     cmd->ioa_cb->ioarcb.cdb[0],
3831 		     le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
3832 
3833 	spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3834 	_pmcraid_fire_command(cmd);
3835 	spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3836 
3837 	/* NOTE ! Remove the below line once abort_task is implemented
3838 	 * in firmware. This line disables ioctl command timeout handling logic
3839 	 * similar to IO command timeout handling, making ioctl commands to wait
3840 	 * until the command completion regardless of timeout value specified in
3841 	 * ioarcb
3842 	 */
3843 	buffer->ioarcb.cmd_timeout = 0;
3844 
3845 	/* If command timeout is specified put caller to wait till that time,
3846 	 * otherwise it would be blocking wait. If command gets timed out, it
3847 	 * will be aborted.
3848 	 */
3849 	if (buffer->ioarcb.cmd_timeout == 0) {
3850 		wait_for_completion(&cmd->wait_for_completion);
3851 	} else if (!wait_for_completion_timeout(
3852 			&cmd->wait_for_completion,
3853 			msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
3854 
3855 		pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
3856 			le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
3857 			cmd->ioa_cb->ioarcb.cdb[0]);
3858 
3859 		spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3860 		cancel_cmd = pmcraid_abort_cmd(cmd);
3861 		spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3862 
3863 		if (cancel_cmd) {
3864 			wait_for_completion(&cancel_cmd->wait_for_completion);
3865 			ioasc = cancel_cmd->ioa_cb->ioasa.ioasc;
3866 			pmcraid_return_cmd(cancel_cmd);
3867 
3868 			/* if abort task couldn't find the command i.e it got
3869 			 * completed prior to aborting, return good completion.
3870 			 * if command got aborted successfully or there was IOA
3871 			 * reset due to abort task itself getting timedout then
3872 			 * return -ETIMEDOUT
3873 			 */
3874 			if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
3875 			    PMCRAID_IOASC_SENSE_KEY(ioasc) == 0x00) {
3876 				if (ioasc != PMCRAID_IOASC_GC_IOARCB_NOTFOUND)
3877 					rc = -ETIMEDOUT;
3878 				goto out_handle_response;
3879 			}
3880 		}
3881 
3882 		/* no command block for abort task or abort task failed to abort
3883 		 * the IOARCB, then wait for 150 more seconds and initiate reset
3884 		 * sequence after timeout
3885 		 */
3886 		if (!wait_for_completion_timeout(
3887 			&cmd->wait_for_completion,
3888 			msecs_to_jiffies(150 * 1000))) {
3889 			pmcraid_reset_bringup(cmd->drv_inst);
3890 			rc = -ETIMEDOUT;
3891 		}
3892 	}
3893 
3894 out_handle_response:
3895 	/* copy entire IOASA buffer and return IOCTL success.
3896 	 * If copying IOASA to user-buffer fails, return
3897 	 * EFAULT
3898 	 */
3899 	if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
3900 		sizeof(struct pmcraid_ioasa))) {
3901 		pmcraid_err("failed to copy ioasa buffer to user\n");
3902 		rc = -EFAULT;
3903 	}
3904 
3905 	/* If the data transfer was from device, copy the data onto user
3906 	 * buffers
3907 	 */
3908 	else if (direction == DMA_FROM_DEVICE && request_size > 0) {
3909 		rc = pmcraid_copy_sglist(cmd->sglist,
3910 					 request_buffer,
3911 					 request_size,
3912 					 direction);
3913 		if (rc) {
3914 			pmcraid_err("failed to copy user buffer\n");
3915 			rc = -EFAULT;
3916 		}
3917 	}
3918 
3919 out_free_sglist:
3920 	pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
3921 	pmcraid_return_cmd(cmd);
3922 
3923 out_free_buffer:
3924 	kfree(buffer);
3925 
3926 	return rc;
3927 }
3928 
3929 
3930 
3931 
3932 /**
3933  * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
3934  *
3935  * @pinstance: pointer to adapter instance structure
3936  * @cmd: ioctl command passed in
3937  * @buflen: length of user_buffer
3938  * @user_buffer: user buffer pointer
3939  *
3940  * Return Value
3941  *   0 in case of success, otherwise appropriate error code
3942  */
3943 static long pmcraid_ioctl_driver(
3944 	struct pmcraid_instance *pinstance,
3945 	unsigned int cmd,
3946 	unsigned int buflen,
3947 	void __user *user_buffer
3948 )
3949 {
3950 	int rc = -ENOSYS;
3951 
3952 	if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
3953 		pmcraid_err("ioctl_driver: access fault in request buffer\n");
3954 		return -EFAULT;
3955 	}
3956 
3957 	switch (cmd) {
3958 	case PMCRAID_IOCTL_RESET_ADAPTER:
3959 		pmcraid_reset_bringup(pinstance);
3960 		rc = 0;
3961 		break;
3962 
3963 	default:
3964 		break;
3965 	}
3966 
3967 	return rc;
3968 }
3969 
3970 /**
3971  * pmcraid_check_ioctl_buffer - check for proper access to user buffer
3972  *
3973  * @cmd: ioctl command
3974  * @arg: user buffer
3975  * @hdr: pointer to kernel memory for pmcraid_ioctl_header
3976  *
3977  * Return Value
3978  *	negetive error code if there are access issues, otherwise zero.
3979  *	Upon success, returns ioctl header copied out of user buffer.
3980  */
3981 
3982 static int pmcraid_check_ioctl_buffer(
3983 	int cmd,
3984 	void __user *arg,
3985 	struct pmcraid_ioctl_header *hdr
3986 )
3987 {
3988 	int rc = 0;
3989 	int access = VERIFY_READ;
3990 
3991 	if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
3992 		pmcraid_err("couldn't copy ioctl header from user buffer\n");
3993 		return -EFAULT;
3994 	}
3995 
3996 	/* check for valid driver signature */
3997 	rc = memcmp(hdr->signature,
3998 		    PMCRAID_IOCTL_SIGNATURE,
3999 		    sizeof(hdr->signature));
4000 	if (rc) {
4001 		pmcraid_err("signature verification failed\n");
4002 		return -EINVAL;
4003 	}
4004 
4005 	/* check for appropriate buffer access */
4006 	if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
4007 		access = VERIFY_WRITE;
4008 
4009 	rc = access_ok(access,
4010 		       (arg + sizeof(struct pmcraid_ioctl_header)),
4011 		       hdr->buffer_length);
4012 	if (!rc) {
4013 		pmcraid_err("access failed for user buffer of size %d\n",
4014 			     hdr->buffer_length);
4015 		return -EFAULT;
4016 	}
4017 
4018 	return 0;
4019 }
4020 
4021 /**
4022  *  pmcraid_ioctl - char node ioctl entry point
4023  */
4024 static long pmcraid_chr_ioctl(
4025 	struct file *filep,
4026 	unsigned int cmd,
4027 	unsigned long arg
4028 )
4029 {
4030 	struct pmcraid_instance *pinstance = NULL;
4031 	struct pmcraid_ioctl_header *hdr = NULL;
4032 	int retval = -ENOTTY;
4033 
4034 	hdr = kmalloc(sizeof(struct pmcraid_ioctl_header), GFP_KERNEL);
4035 
4036 	if (!hdr) {
4037 		pmcraid_err("failed to allocate memory for ioctl header\n");
4038 		return -ENOMEM;
4039 	}
4040 
4041 	retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
4042 
4043 	if (retval) {
4044 		pmcraid_info("chr_ioctl: header check failed\n");
4045 		kfree(hdr);
4046 		return retval;
4047 	}
4048 
4049 	pinstance = filep->private_data;
4050 
4051 	if (!pinstance) {
4052 		pmcraid_info("adapter instance is not found\n");
4053 		kfree(hdr);
4054 		return -ENOTTY;
4055 	}
4056 
4057 	switch (_IOC_TYPE(cmd)) {
4058 
4059 	case PMCRAID_PASSTHROUGH_IOCTL:
4060 		/* If ioctl code is to download microcode, we need to block
4061 		 * mid-layer requests.
4062 		 */
4063 		if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
4064 			scsi_block_requests(pinstance->host);
4065 
4066 		retval = pmcraid_ioctl_passthrough(pinstance,
4067 						   cmd,
4068 						   hdr->buffer_length,
4069 						   arg);
4070 
4071 		if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
4072 			scsi_unblock_requests(pinstance->host);
4073 		break;
4074 
4075 	case PMCRAID_DRIVER_IOCTL:
4076 		arg += sizeof(struct pmcraid_ioctl_header);
4077 		retval = pmcraid_ioctl_driver(pinstance,
4078 					      cmd,
4079 					      hdr->buffer_length,
4080 					      (void __user *)arg);
4081 		break;
4082 
4083 	default:
4084 		retval = -ENOTTY;
4085 		break;
4086 	}
4087 
4088 	kfree(hdr);
4089 
4090 	return retval;
4091 }
4092 
4093 /**
4094  * File operations structure for management interface
4095  */
4096 static const struct file_operations pmcraid_fops = {
4097 	.owner = THIS_MODULE,
4098 	.open = pmcraid_chr_open,
4099 	.fasync = pmcraid_chr_fasync,
4100 	.unlocked_ioctl = pmcraid_chr_ioctl,
4101 #ifdef CONFIG_COMPAT
4102 	.compat_ioctl = pmcraid_chr_ioctl,
4103 #endif
4104 	.llseek = noop_llseek,
4105 };
4106 
4107 
4108 
4109 
4110 /**
4111  * pmcraid_show_log_level - Display adapter's error logging level
4112  * @dev: class device struct
4113  * @buf: buffer
4114  *
4115  * Return value:
4116  *  number of bytes printed to buffer
4117  */
4118 static ssize_t pmcraid_show_log_level(
4119 	struct device *dev,
4120 	struct device_attribute *attr,
4121 	char *buf)
4122 {
4123 	struct Scsi_Host *shost = class_to_shost(dev);
4124 	struct pmcraid_instance *pinstance =
4125 		(struct pmcraid_instance *)shost->hostdata;
4126 	return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
4127 }
4128 
4129 /**
4130  * pmcraid_store_log_level - Change the adapter's error logging level
4131  * @dev: class device struct
4132  * @buf: buffer
4133  * @count: not used
4134  *
4135  * Return value:
4136  *  number of bytes printed to buffer
4137  */
4138 static ssize_t pmcraid_store_log_level(
4139 	struct device *dev,
4140 	struct device_attribute *attr,
4141 	const char *buf,
4142 	size_t count
4143 )
4144 {
4145 	struct Scsi_Host *shost;
4146 	struct pmcraid_instance *pinstance;
4147 	u8 val;
4148 
4149 	if (kstrtou8(buf, 10, &val))
4150 		return -EINVAL;
4151 	/* log-level should be from 0 to 2 */
4152 	if (val > 2)
4153 		return -EINVAL;
4154 
4155 	shost = class_to_shost(dev);
4156 	pinstance = (struct pmcraid_instance *)shost->hostdata;
4157 	pinstance->current_log_level = val;
4158 
4159 	return strlen(buf);
4160 }
4161 
4162 static struct device_attribute pmcraid_log_level_attr = {
4163 	.attr = {
4164 		 .name = "log_level",
4165 		 .mode = S_IRUGO | S_IWUSR,
4166 		 },
4167 	.show = pmcraid_show_log_level,
4168 	.store = pmcraid_store_log_level,
4169 };
4170 
4171 /**
4172  * pmcraid_show_drv_version - Display driver version
4173  * @dev: class device struct
4174  * @buf: buffer
4175  *
4176  * Return value:
4177  *  number of bytes printed to buffer
4178  */
4179 static ssize_t pmcraid_show_drv_version(
4180 	struct device *dev,
4181 	struct device_attribute *attr,
4182 	char *buf
4183 )
4184 {
4185 	return snprintf(buf, PAGE_SIZE, "version: %s\n",
4186 			PMCRAID_DRIVER_VERSION);
4187 }
4188 
4189 static struct device_attribute pmcraid_driver_version_attr = {
4190 	.attr = {
4191 		 .name = "drv_version",
4192 		 .mode = S_IRUGO,
4193 		 },
4194 	.show = pmcraid_show_drv_version,
4195 };
4196 
4197 /**
4198  * pmcraid_show_io_adapter_id - Display driver assigned adapter id
4199  * @dev: class device struct
4200  * @buf: buffer
4201  *
4202  * Return value:
4203  *  number of bytes printed to buffer
4204  */
4205 static ssize_t pmcraid_show_adapter_id(
4206 	struct device *dev,
4207 	struct device_attribute *attr,
4208 	char *buf
4209 )
4210 {
4211 	struct Scsi_Host *shost = class_to_shost(dev);
4212 	struct pmcraid_instance *pinstance =
4213 		(struct pmcraid_instance *)shost->hostdata;
4214 	u32 adapter_id = (pinstance->pdev->bus->number << 8) |
4215 		pinstance->pdev->devfn;
4216 	u32 aen_group = pmcraid_event_family.id;
4217 
4218 	return snprintf(buf, PAGE_SIZE,
4219 			"adapter id: %d\nminor: %d\naen group: %d\n",
4220 			adapter_id, MINOR(pinstance->cdev.dev), aen_group);
4221 }
4222 
4223 static struct device_attribute pmcraid_adapter_id_attr = {
4224 	.attr = {
4225 		 .name = "adapter_id",
4226 		 .mode = S_IRUGO | S_IWUSR,
4227 		 },
4228 	.show = pmcraid_show_adapter_id,
4229 };
4230 
4231 static struct device_attribute *pmcraid_host_attrs[] = {
4232 	&pmcraid_log_level_attr,
4233 	&pmcraid_driver_version_attr,
4234 	&pmcraid_adapter_id_attr,
4235 	NULL,
4236 };
4237 
4238 
4239 /* host template structure for pmcraid driver */
4240 static struct scsi_host_template pmcraid_host_template = {
4241 	.module = THIS_MODULE,
4242 	.name = PMCRAID_DRIVER_NAME,
4243 	.queuecommand = pmcraid_queuecommand,
4244 	.eh_abort_handler = pmcraid_eh_abort_handler,
4245 	.eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
4246 	.eh_target_reset_handler = pmcraid_eh_target_reset_handler,
4247 	.eh_device_reset_handler = pmcraid_eh_device_reset_handler,
4248 	.eh_host_reset_handler = pmcraid_eh_host_reset_handler,
4249 
4250 	.slave_alloc = pmcraid_slave_alloc,
4251 	.slave_configure = pmcraid_slave_configure,
4252 	.slave_destroy = pmcraid_slave_destroy,
4253 	.change_queue_depth = pmcraid_change_queue_depth,
4254 	.change_queue_type  = scsi_change_queue_type,
4255 	.can_queue = PMCRAID_MAX_IO_CMD,
4256 	.this_id = -1,
4257 	.sg_tablesize = PMCRAID_MAX_IOADLS,
4258 	.max_sectors = PMCRAID_IOA_MAX_SECTORS,
4259 	.no_write_same = 1,
4260 	.cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
4261 	.use_clustering = ENABLE_CLUSTERING,
4262 	.shost_attrs = pmcraid_host_attrs,
4263 	.proc_name = PMCRAID_DRIVER_NAME,
4264 	.use_blk_tags = 1,
4265 };
4266 
4267 /*
4268  * pmcraid_isr_msix - implements MSI-X interrupt handling routine
4269  * @irq: interrupt vector number
4270  * @dev_id: pointer hrrq_vector
4271  *
4272  * Return Value
4273  *	 IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4274  */
4275 
4276 static irqreturn_t pmcraid_isr_msix(int irq, void *dev_id)
4277 {
4278 	struct pmcraid_isr_param *hrrq_vector;
4279 	struct pmcraid_instance *pinstance;
4280 	unsigned long lock_flags;
4281 	u32 intrs_val;
4282 	int hrrq_id;
4283 
4284 	hrrq_vector = (struct pmcraid_isr_param *)dev_id;
4285 	hrrq_id = hrrq_vector->hrrq_id;
4286 	pinstance = hrrq_vector->drv_inst;
4287 
4288 	if (!hrrq_id) {
4289 		/* Read the interrupt */
4290 		intrs_val = pmcraid_read_interrupts(pinstance);
4291 		if (intrs_val &&
4292 			((ioread32(pinstance->int_regs.host_ioa_interrupt_reg)
4293 			& DOORBELL_INTR_MSIX_CLR) == 0)) {
4294 			/* Any error interrupts including unit_check,
4295 			 * initiate IOA reset.In case of unit check indicate
4296 			 * to reset_sequence that IOA unit checked and prepare
4297 			 * for a dump during reset sequence
4298 			 */
4299 			if (intrs_val & PMCRAID_ERROR_INTERRUPTS) {
4300 				if (intrs_val & INTRS_IOA_UNIT_CHECK)
4301 					pinstance->ioa_unit_check = 1;
4302 
4303 				pmcraid_err("ISR: error interrupts: %x \
4304 					initiating reset\n", intrs_val);
4305 				spin_lock_irqsave(pinstance->host->host_lock,
4306 					lock_flags);
4307 				pmcraid_initiate_reset(pinstance);
4308 				spin_unlock_irqrestore(
4309 					pinstance->host->host_lock,
4310 					lock_flags);
4311 			}
4312 			/* If interrupt was as part of the ioa initialization,
4313 			 * clear it. Delete the timer and wakeup the
4314 			 * reset engine to proceed with reset sequence
4315 			 */
4316 			if (intrs_val & INTRS_TRANSITION_TO_OPERATIONAL)
4317 				pmcraid_clr_trans_op(pinstance);
4318 
4319 			/* Clear the interrupt register by writing
4320 			 * to host to ioa doorbell. Once done
4321 			 * FW will clear the interrupt.
4322 			 */
4323 			iowrite32(DOORBELL_INTR_MSIX_CLR,
4324 				pinstance->int_regs.host_ioa_interrupt_reg);
4325 			ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
4326 
4327 
4328 		}
4329 	}
4330 
4331 	tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
4332 
4333 	return IRQ_HANDLED;
4334 }
4335 
4336 /**
4337  * pmcraid_isr  - implements legacy interrupt handling routine
4338  *
4339  * @irq: interrupt vector number
4340  * @dev_id: pointer hrrq_vector
4341  *
4342  * Return Value
4343  *	 IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
4344  */
4345 static irqreturn_t pmcraid_isr(int irq, void *dev_id)
4346 {
4347 	struct pmcraid_isr_param *hrrq_vector;
4348 	struct pmcraid_instance *pinstance;
4349 	u32 intrs;
4350 	unsigned long lock_flags;
4351 	int hrrq_id = 0;
4352 
4353 	/* In case of legacy interrupt mode where interrupts are shared across
4354 	 * isrs, it may be possible that the current interrupt is not from IOA
4355 	 */
4356 	if (!dev_id) {
4357 		printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
4358 		return IRQ_NONE;
4359 	}
4360 	hrrq_vector = (struct pmcraid_isr_param *)dev_id;
4361 	pinstance = hrrq_vector->drv_inst;
4362 
4363 	intrs = pmcraid_read_interrupts(pinstance);
4364 
4365 	if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0))
4366 		return IRQ_NONE;
4367 
4368 	/* Any error interrupts including unit_check, initiate IOA reset.
4369 	 * In case of unit check indicate to reset_sequence that IOA unit
4370 	 * checked and prepare for a dump during reset sequence
4371 	 */
4372 	if (intrs & PMCRAID_ERROR_INTERRUPTS) {
4373 
4374 		if (intrs & INTRS_IOA_UNIT_CHECK)
4375 			pinstance->ioa_unit_check = 1;
4376 
4377 		iowrite32(intrs,
4378 			  pinstance->int_regs.ioa_host_interrupt_clr_reg);
4379 		pmcraid_err("ISR: error interrupts: %x initiating reset\n",
4380 			    intrs);
4381 		intrs = ioread32(
4382 				pinstance->int_regs.ioa_host_interrupt_clr_reg);
4383 		spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
4384 		pmcraid_initiate_reset(pinstance);
4385 		spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
4386 	} else {
4387 		/* If interrupt was as part of the ioa initialization,
4388 		 * clear. Delete the timer and wakeup the
4389 		 * reset engine to proceed with reset sequence
4390 		 */
4391 		if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
4392 			pmcraid_clr_trans_op(pinstance);
4393 		} else {
4394 			iowrite32(intrs,
4395 				pinstance->int_regs.ioa_host_interrupt_clr_reg);
4396 			ioread32(
4397 				pinstance->int_regs.ioa_host_interrupt_clr_reg);
4398 
4399 			tasklet_schedule(
4400 					&(pinstance->isr_tasklet[hrrq_id]));
4401 		}
4402 	}
4403 
4404 	return IRQ_HANDLED;
4405 }
4406 
4407 
4408 /**
4409  * pmcraid_worker_function -  worker thread function
4410  *
4411  * @workp: pointer to struct work queue
4412  *
4413  * Return Value
4414  *	 None
4415  */
4416 
4417 static void pmcraid_worker_function(struct work_struct *workp)
4418 {
4419 	struct pmcraid_instance *pinstance;
4420 	struct pmcraid_resource_entry *res;
4421 	struct pmcraid_resource_entry *temp;
4422 	struct scsi_device *sdev;
4423 	unsigned long lock_flags;
4424 	unsigned long host_lock_flags;
4425 	u16 fw_version;
4426 	u8 bus, target, lun;
4427 
4428 	pinstance = container_of(workp, struct pmcraid_instance, worker_q);
4429 	/* add resources only after host is added into system */
4430 	if (!atomic_read(&pinstance->expose_resources))
4431 		return;
4432 
4433 	fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
4434 
4435 	spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
4436 	list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
4437 
4438 		if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
4439 			sdev = res->scsi_dev;
4440 
4441 			/* host_lock must be held before calling
4442 			 * scsi_device_get
4443 			 */
4444 			spin_lock_irqsave(pinstance->host->host_lock,
4445 					  host_lock_flags);
4446 			if (!scsi_device_get(sdev)) {
4447 				spin_unlock_irqrestore(
4448 						pinstance->host->host_lock,
4449 						host_lock_flags);
4450 				pmcraid_info("deleting %x from midlayer\n",
4451 					     res->cfg_entry.resource_address);
4452 				list_move_tail(&res->queue,
4453 						&pinstance->free_res_q);
4454 				spin_unlock_irqrestore(
4455 					&pinstance->resource_lock,
4456 					lock_flags);
4457 				scsi_remove_device(sdev);
4458 				scsi_device_put(sdev);
4459 				spin_lock_irqsave(&pinstance->resource_lock,
4460 						   lock_flags);
4461 				res->change_detected = 0;
4462 			} else {
4463 				spin_unlock_irqrestore(
4464 						pinstance->host->host_lock,
4465 						host_lock_flags);
4466 			}
4467 		}
4468 	}
4469 
4470 	list_for_each_entry(res, &pinstance->used_res_q, queue) {
4471 
4472 		if (res->change_detected == RES_CHANGE_ADD) {
4473 
4474 			if (!pmcraid_expose_resource(fw_version,
4475 						     &res->cfg_entry))
4476 				continue;
4477 
4478 			if (RES_IS_VSET(res->cfg_entry)) {
4479 				bus = PMCRAID_VSET_BUS_ID;
4480 				if (fw_version <= PMCRAID_FW_VERSION_1)
4481 					target = res->cfg_entry.unique_flags1;
4482 				else
4483 					target = res->cfg_entry.array_id & 0xFF;
4484 				lun = PMCRAID_VSET_LUN_ID;
4485 			} else {
4486 				bus = PMCRAID_PHYS_BUS_ID;
4487 				target =
4488 				     RES_TARGET(
4489 					res->cfg_entry.resource_address);
4490 				lun = RES_LUN(res->cfg_entry.resource_address);
4491 			}
4492 
4493 			res->change_detected = 0;
4494 			spin_unlock_irqrestore(&pinstance->resource_lock,
4495 						lock_flags);
4496 			scsi_add_device(pinstance->host, bus, target, lun);
4497 			spin_lock_irqsave(&pinstance->resource_lock,
4498 					   lock_flags);
4499 		}
4500 	}
4501 
4502 	spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
4503 }
4504 
4505 /**
4506  * pmcraid_tasklet_function - Tasklet function
4507  *
4508  * @instance: pointer to msix param structure
4509  *
4510  * Return Value
4511  *	None
4512  */
4513 static void pmcraid_tasklet_function(unsigned long instance)
4514 {
4515 	struct pmcraid_isr_param *hrrq_vector;
4516 	struct pmcraid_instance *pinstance;
4517 	unsigned long hrrq_lock_flags;
4518 	unsigned long pending_lock_flags;
4519 	unsigned long host_lock_flags;
4520 	spinlock_t *lockp; /* hrrq buffer lock */
4521 	int id;
4522 	__le32 resp;
4523 
4524 	hrrq_vector = (struct pmcraid_isr_param *)instance;
4525 	pinstance = hrrq_vector->drv_inst;
4526 	id = hrrq_vector->hrrq_id;
4527 	lockp = &(pinstance->hrrq_lock[id]);
4528 
4529 	/* loop through each of the commands responded by IOA. Each HRRQ buf is
4530 	 * protected by its own lock. Traversals must be done within this lock
4531 	 * as there may be multiple tasklets running on multiple CPUs. Note
4532 	 * that the lock is held just for picking up the response handle and
4533 	 * manipulating hrrq_curr/toggle_bit values.
4534 	 */
4535 	spin_lock_irqsave(lockp, hrrq_lock_flags);
4536 
4537 	resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4538 
4539 	while ((resp & HRRQ_TOGGLE_BIT) ==
4540 		pinstance->host_toggle_bit[id]) {
4541 
4542 		int cmd_index = resp >> 2;
4543 		struct pmcraid_cmd *cmd = NULL;
4544 
4545 		if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
4546 			pinstance->hrrq_curr[id]++;
4547 		} else {
4548 			pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
4549 			pinstance->host_toggle_bit[id] ^= 1u;
4550 		}
4551 
4552 		if (cmd_index >= PMCRAID_MAX_CMD) {
4553 			/* In case of invalid response handle, log message */
4554 			pmcraid_err("Invalid response handle %d\n", cmd_index);
4555 			resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4556 			continue;
4557 		}
4558 
4559 		cmd = pinstance->cmd_list[cmd_index];
4560 		spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4561 
4562 		spin_lock_irqsave(&pinstance->pending_pool_lock,
4563 				   pending_lock_flags);
4564 		list_del(&cmd->free_list);
4565 		spin_unlock_irqrestore(&pinstance->pending_pool_lock,
4566 					pending_lock_flags);
4567 		del_timer(&cmd->timer);
4568 		atomic_dec(&pinstance->outstanding_cmds);
4569 
4570 		if (cmd->cmd_done == pmcraid_ioa_reset) {
4571 			spin_lock_irqsave(pinstance->host->host_lock,
4572 					  host_lock_flags);
4573 			cmd->cmd_done(cmd);
4574 			spin_unlock_irqrestore(pinstance->host->host_lock,
4575 					       host_lock_flags);
4576 		} else if (cmd->cmd_done != NULL) {
4577 			cmd->cmd_done(cmd);
4578 		}
4579 		/* loop over until we are done with all responses */
4580 		spin_lock_irqsave(lockp, hrrq_lock_flags);
4581 		resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
4582 	}
4583 
4584 	spin_unlock_irqrestore(lockp, hrrq_lock_flags);
4585 }
4586 
4587 /**
4588  * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
4589  * @pinstance: pointer to adapter instance structure
4590  *
4591  * This routine un-registers registered interrupt handler and
4592  * also frees irqs/vectors.
4593  *
4594  * Retun Value
4595  *	None
4596  */
4597 static
4598 void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
4599 {
4600 	int i;
4601 
4602 	for (i = 0; i < pinstance->num_hrrq; i++)
4603 		free_irq(pinstance->hrrq_vector[i].vector,
4604 			 &(pinstance->hrrq_vector[i]));
4605 
4606 	if (pinstance->interrupt_mode) {
4607 		pci_disable_msix(pinstance->pdev);
4608 		pinstance->interrupt_mode = 0;
4609 	}
4610 }
4611 
4612 /**
4613  * pmcraid_register_interrupt_handler - registers interrupt handler
4614  * @pinstance: pointer to per-adapter instance structure
4615  *
4616  * Return Value
4617  *	0 on success, non-zero error code otherwise.
4618  */
4619 static int
4620 pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
4621 {
4622 	int rc;
4623 	struct pci_dev *pdev = pinstance->pdev;
4624 
4625 	if ((pmcraid_enable_msix) &&
4626 		(pci_find_capability(pdev, PCI_CAP_ID_MSIX))) {
4627 		int num_hrrq = PMCRAID_NUM_MSIX_VECTORS;
4628 		struct msix_entry entries[PMCRAID_NUM_MSIX_VECTORS];
4629 		int i;
4630 		for (i = 0; i < PMCRAID_NUM_MSIX_VECTORS; i++)
4631 			entries[i].entry = i;
4632 
4633 		num_hrrq = pci_enable_msix_range(pdev, entries, 1, num_hrrq);
4634 		if (num_hrrq < 0)
4635 			goto pmcraid_isr_legacy;
4636 
4637 		for (i = 0; i < num_hrrq; i++) {
4638 			pinstance->hrrq_vector[i].hrrq_id = i;
4639 			pinstance->hrrq_vector[i].drv_inst = pinstance;
4640 			pinstance->hrrq_vector[i].vector = entries[i].vector;
4641 			rc = request_irq(pinstance->hrrq_vector[i].vector,
4642 					pmcraid_isr_msix, 0,
4643 					PMCRAID_DRIVER_NAME,
4644 					&(pinstance->hrrq_vector[i]));
4645 
4646 			if (rc) {
4647 				int j;
4648 				for (j = 0; j < i; j++)
4649 					free_irq(entries[j].vector,
4650 						 &(pinstance->hrrq_vector[j]));
4651 				pci_disable_msix(pdev);
4652 				goto pmcraid_isr_legacy;
4653 			}
4654 		}
4655 
4656 		pinstance->num_hrrq = num_hrrq;
4657 		pinstance->interrupt_mode = 1;
4658 		iowrite32(DOORBELL_INTR_MODE_MSIX,
4659 			  pinstance->int_regs.host_ioa_interrupt_reg);
4660 		ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
4661 		goto pmcraid_isr_out;
4662 	}
4663 
4664 pmcraid_isr_legacy:
4665 	/* If MSI-X registration failed fallback to legacy mode, where
4666 	 * only one hrrq entry will be used
4667 	 */
4668 	pinstance->hrrq_vector[0].hrrq_id = 0;
4669 	pinstance->hrrq_vector[0].drv_inst = pinstance;
4670 	pinstance->hrrq_vector[0].vector = pdev->irq;
4671 	pinstance->num_hrrq = 1;
4672 
4673 	rc = request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
4674 			 PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
4675 pmcraid_isr_out:
4676 	return rc;
4677 }
4678 
4679 /**
4680  * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
4681  * @pinstance: per adapter instance structure pointer
4682  * @max_index: number of buffer blocks to release
4683  *
4684  * Return Value
4685  *  None
4686  */
4687 static void
4688 pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
4689 {
4690 	int i;
4691 	for (i = 0; i < max_index; i++) {
4692 		kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
4693 		pinstance->cmd_list[i] = NULL;
4694 	}
4695 	kmem_cache_destroy(pinstance->cmd_cachep);
4696 	pinstance->cmd_cachep = NULL;
4697 }
4698 
4699 /**
4700  * pmcraid_release_control_blocks - releases buffers alloced for control blocks
4701  * @pinstance: pointer to per adapter instance structure
4702  * @max_index: number of buffers (from 0 onwards) to release
4703  *
4704  * This function assumes that the command blocks for which control blocks are
4705  * linked are not released.
4706  *
4707  * Return Value
4708  *	 None
4709  */
4710 static void
4711 pmcraid_release_control_blocks(
4712 	struct pmcraid_instance *pinstance,
4713 	int max_index
4714 )
4715 {
4716 	int i;
4717 
4718 	if (pinstance->control_pool == NULL)
4719 		return;
4720 
4721 	for (i = 0; i < max_index; i++) {
4722 		pci_pool_free(pinstance->control_pool,
4723 			      pinstance->cmd_list[i]->ioa_cb,
4724 			      pinstance->cmd_list[i]->ioa_cb_bus_addr);
4725 		pinstance->cmd_list[i]->ioa_cb = NULL;
4726 		pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
4727 	}
4728 	pci_pool_destroy(pinstance->control_pool);
4729 	pinstance->control_pool = NULL;
4730 }
4731 
4732 /**
4733  * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
4734  * @pinstance - pointer to per adapter instance structure
4735  *
4736  * Allocates memory for command blocks using kernel slab allocator.
4737  *
4738  * Return Value
4739  *	0 in case of success; -ENOMEM in case of failure
4740  */
4741 static int pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
4742 {
4743 	int i;
4744 
4745 	sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
4746 		pinstance->host->unique_id);
4747 
4748 
4749 	pinstance->cmd_cachep = kmem_cache_create(
4750 					pinstance->cmd_pool_name,
4751 					sizeof(struct pmcraid_cmd), 0,
4752 					SLAB_HWCACHE_ALIGN, NULL);
4753 	if (!pinstance->cmd_cachep)
4754 		return -ENOMEM;
4755 
4756 	for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4757 		pinstance->cmd_list[i] =
4758 			kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
4759 		if (!pinstance->cmd_list[i]) {
4760 			pmcraid_release_cmd_blocks(pinstance, i);
4761 			return -ENOMEM;
4762 		}
4763 	}
4764 	return 0;
4765 }
4766 
4767 /**
4768  * pmcraid_allocate_control_blocks - allocates memory control blocks
4769  * @pinstance : pointer to per adapter instance structure
4770  *
4771  * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
4772  * and IOASAs. This is called after command blocks are already allocated.
4773  *
4774  * Return Value
4775  *  0 in case it can allocate all control blocks, otherwise -ENOMEM
4776  */
4777 static int pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
4778 {
4779 	int i;
4780 
4781 	sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
4782 		pinstance->host->unique_id);
4783 
4784 	pinstance->control_pool =
4785 		pci_pool_create(pinstance->ctl_pool_name,
4786 				pinstance->pdev,
4787 				sizeof(struct pmcraid_control_block),
4788 				PMCRAID_IOARCB_ALIGNMENT, 0);
4789 
4790 	if (!pinstance->control_pool)
4791 		return -ENOMEM;
4792 
4793 	for (i = 0; i < PMCRAID_MAX_CMD; i++) {
4794 		pinstance->cmd_list[i]->ioa_cb =
4795 			pci_pool_alloc(
4796 				pinstance->control_pool,
4797 				GFP_KERNEL,
4798 				&(pinstance->cmd_list[i]->ioa_cb_bus_addr));
4799 
4800 		if (!pinstance->cmd_list[i]->ioa_cb) {
4801 			pmcraid_release_control_blocks(pinstance, i);
4802 			return -ENOMEM;
4803 		}
4804 		memset(pinstance->cmd_list[i]->ioa_cb, 0,
4805 			sizeof(struct pmcraid_control_block));
4806 	}
4807 	return 0;
4808 }
4809 
4810 /**
4811  * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
4812  * @pinstance: pointer to per adapter instance structure
4813  * @maxindex: size of hrrq buffer pointer array
4814  *
4815  * Return Value
4816  *	None
4817  */
4818 static void
4819 pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
4820 {
4821 	int i;
4822 	for (i = 0; i < maxindex; i++) {
4823 
4824 		pci_free_consistent(pinstance->pdev,
4825 				    HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
4826 				    pinstance->hrrq_start[i],
4827 				    pinstance->hrrq_start_bus_addr[i]);
4828 
4829 		/* reset pointers and toggle bit to zeros */
4830 		pinstance->hrrq_start[i] = NULL;
4831 		pinstance->hrrq_start_bus_addr[i] = 0;
4832 		pinstance->host_toggle_bit[i] = 0;
4833 	}
4834 }
4835 
4836 /**
4837  * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
4838  * @pinstance: pointer to per adapter instance structure
4839  *
4840  * Return value
4841  *	0 hrrq buffers are allocated, -ENOMEM otherwise.
4842  */
4843 static int pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
4844 {
4845 	int i, buffer_size;
4846 
4847 	buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
4848 
4849 	for (i = 0; i < pinstance->num_hrrq; i++) {
4850 		pinstance->hrrq_start[i] =
4851 			pci_alloc_consistent(
4852 					pinstance->pdev,
4853 					buffer_size,
4854 					&(pinstance->hrrq_start_bus_addr[i]));
4855 
4856 		if (pinstance->hrrq_start[i] == 0) {
4857 			pmcraid_err("pci_alloc failed for hrrq vector : %d\n",
4858 				    i);
4859 			pmcraid_release_host_rrqs(pinstance, i);
4860 			return -ENOMEM;
4861 		}
4862 
4863 		memset(pinstance->hrrq_start[i], 0, buffer_size);
4864 		pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
4865 		pinstance->hrrq_end[i] =
4866 			pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
4867 		pinstance->host_toggle_bit[i] = 1;
4868 		spin_lock_init(&pinstance->hrrq_lock[i]);
4869 	}
4870 	return 0;
4871 }
4872 
4873 /**
4874  * pmcraid_release_hcams - release HCAM buffers
4875  *
4876  * @pinstance: pointer to per adapter instance structure
4877  *
4878  * Return value
4879  *  none
4880  */
4881 static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
4882 {
4883 	if (pinstance->ccn.msg != NULL) {
4884 		pci_free_consistent(pinstance->pdev,
4885 				    PMCRAID_AEN_HDR_SIZE +
4886 				    sizeof(struct pmcraid_hcam_ccn_ext),
4887 				    pinstance->ccn.msg,
4888 				    pinstance->ccn.baddr);
4889 
4890 		pinstance->ccn.msg = NULL;
4891 		pinstance->ccn.hcam = NULL;
4892 		pinstance->ccn.baddr = 0;
4893 	}
4894 
4895 	if (pinstance->ldn.msg != NULL) {
4896 		pci_free_consistent(pinstance->pdev,
4897 				    PMCRAID_AEN_HDR_SIZE +
4898 				    sizeof(struct pmcraid_hcam_ldn),
4899 				    pinstance->ldn.msg,
4900 				    pinstance->ldn.baddr);
4901 
4902 		pinstance->ldn.msg = NULL;
4903 		pinstance->ldn.hcam = NULL;
4904 		pinstance->ldn.baddr = 0;
4905 	}
4906 }
4907 
4908 /**
4909  * pmcraid_allocate_hcams - allocates HCAM buffers
4910  * @pinstance : pointer to per adapter instance structure
4911  *
4912  * Return Value:
4913  *   0 in case of successful allocation, non-zero otherwise
4914  */
4915 static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
4916 {
4917 	pinstance->ccn.msg = pci_alloc_consistent(
4918 					pinstance->pdev,
4919 					PMCRAID_AEN_HDR_SIZE +
4920 					sizeof(struct pmcraid_hcam_ccn_ext),
4921 					&(pinstance->ccn.baddr));
4922 
4923 	pinstance->ldn.msg = pci_alloc_consistent(
4924 					pinstance->pdev,
4925 					PMCRAID_AEN_HDR_SIZE +
4926 					sizeof(struct pmcraid_hcam_ldn),
4927 					&(pinstance->ldn.baddr));
4928 
4929 	if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
4930 		pmcraid_release_hcams(pinstance);
4931 	} else {
4932 		pinstance->ccn.hcam =
4933 			(void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
4934 		pinstance->ldn.hcam =
4935 			(void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
4936 
4937 		atomic_set(&pinstance->ccn.ignore, 0);
4938 		atomic_set(&pinstance->ldn.ignore, 0);
4939 	}
4940 
4941 	return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
4942 }
4943 
4944 /**
4945  * pmcraid_release_config_buffers - release config.table buffers
4946  * @pinstance: pointer to per adapter instance structure
4947  *
4948  * Return Value
4949  *	 none
4950  */
4951 static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
4952 {
4953 	if (pinstance->cfg_table != NULL &&
4954 	    pinstance->cfg_table_bus_addr != 0) {
4955 		pci_free_consistent(pinstance->pdev,
4956 				    sizeof(struct pmcraid_config_table),
4957 				    pinstance->cfg_table,
4958 				    pinstance->cfg_table_bus_addr);
4959 		pinstance->cfg_table = NULL;
4960 		pinstance->cfg_table_bus_addr = 0;
4961 	}
4962 
4963 	if (pinstance->res_entries != NULL) {
4964 		int i;
4965 
4966 		for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4967 			list_del(&pinstance->res_entries[i].queue);
4968 		kfree(pinstance->res_entries);
4969 		pinstance->res_entries = NULL;
4970 	}
4971 
4972 	pmcraid_release_hcams(pinstance);
4973 }
4974 
4975 /**
4976  * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
4977  * @pinstance : pointer to per adapter instance structure
4978  *
4979  * Return Value
4980  *	0 for successful allocation, -ENOMEM for any failure
4981  */
4982 static int pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
4983 {
4984 	int i;
4985 
4986 	pinstance->res_entries =
4987 			kzalloc(sizeof(struct pmcraid_resource_entry) *
4988 				PMCRAID_MAX_RESOURCES, GFP_KERNEL);
4989 
4990 	if (NULL == pinstance->res_entries) {
4991 		pmcraid_err("failed to allocate memory for resource table\n");
4992 		return -ENOMEM;
4993 	}
4994 
4995 	for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
4996 		list_add_tail(&pinstance->res_entries[i].queue,
4997 			      &pinstance->free_res_q);
4998 
4999 	pinstance->cfg_table =
5000 		pci_alloc_consistent(pinstance->pdev,
5001 				     sizeof(struct pmcraid_config_table),
5002 				     &pinstance->cfg_table_bus_addr);
5003 
5004 	if (NULL == pinstance->cfg_table) {
5005 		pmcraid_err("couldn't alloc DMA memory for config table\n");
5006 		pmcraid_release_config_buffers(pinstance);
5007 		return -ENOMEM;
5008 	}
5009 
5010 	if (pmcraid_allocate_hcams(pinstance)) {
5011 		pmcraid_err("could not alloc DMA memory for HCAMS\n");
5012 		pmcraid_release_config_buffers(pinstance);
5013 		return -ENOMEM;
5014 	}
5015 
5016 	return 0;
5017 }
5018 
5019 /**
5020  * pmcraid_init_tasklets - registers tasklets for response handling
5021  *
5022  * @pinstance: pointer adapter instance structure
5023  *
5024  * Return value
5025  *	none
5026  */
5027 static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
5028 {
5029 	int i;
5030 	for (i = 0; i < pinstance->num_hrrq; i++)
5031 		tasklet_init(&pinstance->isr_tasklet[i],
5032 			     pmcraid_tasklet_function,
5033 			     (unsigned long)&pinstance->hrrq_vector[i]);
5034 }
5035 
5036 /**
5037  * pmcraid_kill_tasklets - destroys tasklets registered for response handling
5038  *
5039  * @pinstance: pointer to adapter instance structure
5040  *
5041  * Return value
5042  *	none
5043  */
5044 static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
5045 {
5046 	int i;
5047 	for (i = 0; i < pinstance->num_hrrq; i++)
5048 		tasklet_kill(&pinstance->isr_tasklet[i]);
5049 }
5050 
5051 /**
5052  * pmcraid_release_buffers - release per-adapter buffers allocated
5053  *
5054  * @pinstance: pointer to adapter soft state
5055  *
5056  * Return Value
5057  *	none
5058  */
5059 static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
5060 {
5061 	pmcraid_release_config_buffers(pinstance);
5062 	pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
5063 	pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
5064 	pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
5065 
5066 	if (pinstance->inq_data != NULL) {
5067 		pci_free_consistent(pinstance->pdev,
5068 				    sizeof(struct pmcraid_inquiry_data),
5069 				    pinstance->inq_data,
5070 				    pinstance->inq_data_baddr);
5071 
5072 		pinstance->inq_data = NULL;
5073 		pinstance->inq_data_baddr = 0;
5074 	}
5075 
5076 	if (pinstance->timestamp_data != NULL) {
5077 		pci_free_consistent(pinstance->pdev,
5078 				    sizeof(struct pmcraid_timestamp_data),
5079 				    pinstance->timestamp_data,
5080 				    pinstance->timestamp_data_baddr);
5081 
5082 		pinstance->timestamp_data = NULL;
5083 		pinstance->timestamp_data_baddr = 0;
5084 	}
5085 }
5086 
5087 /**
5088  * pmcraid_init_buffers - allocates memory and initializes various structures
5089  * @pinstance: pointer to per adapter instance structure
5090  *
5091  * This routine pre-allocates memory based on the type of block as below:
5092  * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
5093  * IOARCBs(PMCRAID_MAX_CMD)  : DMAable memory, using pci pool allocator
5094  * config-table entries      : DMAable memory using pci_alloc_consistent
5095  * HostRRQs                  : DMAable memory, using pci_alloc_consistent
5096  *
5097  * Return Value
5098  *	 0 in case all of the blocks are allocated, -ENOMEM otherwise.
5099  */
5100 static int pmcraid_init_buffers(struct pmcraid_instance *pinstance)
5101 {
5102 	int i;
5103 
5104 	if (pmcraid_allocate_host_rrqs(pinstance)) {
5105 		pmcraid_err("couldn't allocate memory for %d host rrqs\n",
5106 			     pinstance->num_hrrq);
5107 		return -ENOMEM;
5108 	}
5109 
5110 	if (pmcraid_allocate_config_buffers(pinstance)) {
5111 		pmcraid_err("couldn't allocate memory for config buffers\n");
5112 		pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
5113 		return -ENOMEM;
5114 	}
5115 
5116 	if (pmcraid_allocate_cmd_blocks(pinstance)) {
5117 		pmcraid_err("couldn't allocate memory for cmd blocks\n");
5118 		pmcraid_release_config_buffers(pinstance);
5119 		pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
5120 		return -ENOMEM;
5121 	}
5122 
5123 	if (pmcraid_allocate_control_blocks(pinstance)) {
5124 		pmcraid_err("couldn't allocate memory control blocks\n");
5125 		pmcraid_release_config_buffers(pinstance);
5126 		pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
5127 		pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
5128 		return -ENOMEM;
5129 	}
5130 
5131 	/* allocate DMAable memory for page D0 INQUIRY buffer */
5132 	pinstance->inq_data = pci_alloc_consistent(
5133 					pinstance->pdev,
5134 					sizeof(struct pmcraid_inquiry_data),
5135 					&pinstance->inq_data_baddr);
5136 
5137 	if (pinstance->inq_data == NULL) {
5138 		pmcraid_err("couldn't allocate DMA memory for INQUIRY\n");
5139 		pmcraid_release_buffers(pinstance);
5140 		return -ENOMEM;
5141 	}
5142 
5143 	/* allocate DMAable memory for set timestamp data buffer */
5144 	pinstance->timestamp_data = pci_alloc_consistent(
5145 					pinstance->pdev,
5146 					sizeof(struct pmcraid_timestamp_data),
5147 					&pinstance->timestamp_data_baddr);
5148 
5149 	if (pinstance->timestamp_data == NULL) {
5150 		pmcraid_err("couldn't allocate DMA memory for \
5151 				set time_stamp \n");
5152 		pmcraid_release_buffers(pinstance);
5153 		return -ENOMEM;
5154 	}
5155 
5156 
5157 	/* Initialize all the command blocks and add them to free pool. No
5158 	 * need to lock (free_pool_lock) as this is done in initialization
5159 	 * itself
5160 	 */
5161 	for (i = 0; i < PMCRAID_MAX_CMD; i++) {
5162 		struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
5163 		pmcraid_init_cmdblk(cmdp, i);
5164 		cmdp->drv_inst = pinstance;
5165 		list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
5166 	}
5167 
5168 	return 0;
5169 }
5170 
5171 /**
5172  * pmcraid_reinit_buffers - resets various buffer pointers
5173  * @pinstance: pointer to adapter instance
5174  * Return value
5175  *	none
5176  */
5177 static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
5178 {
5179 	int i;
5180 	int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
5181 
5182 	for (i = 0; i < pinstance->num_hrrq; i++) {
5183 		memset(pinstance->hrrq_start[i], 0, buffer_size);
5184 		pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
5185 		pinstance->hrrq_end[i] =
5186 			pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
5187 		pinstance->host_toggle_bit[i] = 1;
5188 	}
5189 }
5190 
5191 /**
5192  * pmcraid_init_instance - initialize per instance data structure
5193  * @pdev: pointer to pci device structure
5194  * @host: pointer to Scsi_Host structure
5195  * @mapped_pci_addr: memory mapped IOA configuration registers
5196  *
5197  * Return Value
5198  *	 0 on success, non-zero in case of any failure
5199  */
5200 static int pmcraid_init_instance(struct pci_dev *pdev, struct Scsi_Host *host,
5201 				 void __iomem *mapped_pci_addr)
5202 {
5203 	struct pmcraid_instance *pinstance =
5204 		(struct pmcraid_instance *)host->hostdata;
5205 
5206 	pinstance->host = host;
5207 	pinstance->pdev = pdev;
5208 
5209 	/* Initialize register addresses */
5210 	pinstance->mapped_dma_addr = mapped_pci_addr;
5211 
5212 	/* Initialize chip-specific details */
5213 	{
5214 		struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
5215 		struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
5216 
5217 		pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
5218 
5219 		pint_regs->ioa_host_interrupt_reg =
5220 			mapped_pci_addr + chip_cfg->ioa_host_intr;
5221 		pint_regs->ioa_host_interrupt_clr_reg =
5222 			mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
5223 		pint_regs->ioa_host_msix_interrupt_reg =
5224 			mapped_pci_addr + chip_cfg->ioa_host_msix_intr;
5225 		pint_regs->host_ioa_interrupt_reg =
5226 			mapped_pci_addr + chip_cfg->host_ioa_intr;
5227 		pint_regs->host_ioa_interrupt_clr_reg =
5228 			mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
5229 
5230 		/* Current version of firmware exposes interrupt mask set
5231 		 * and mask clr registers through memory mapped bar0.
5232 		 */
5233 		pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
5234 		pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
5235 		pint_regs->ioa_host_interrupt_mask_reg =
5236 			mapped_pci_addr + chip_cfg->ioa_host_mask;
5237 		pint_regs->ioa_host_interrupt_mask_clr_reg =
5238 			mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
5239 		pint_regs->global_interrupt_mask_reg =
5240 			mapped_pci_addr + chip_cfg->global_intr_mask;
5241 	};
5242 
5243 	pinstance->ioa_reset_attempts = 0;
5244 	init_waitqueue_head(&pinstance->reset_wait_q);
5245 
5246 	atomic_set(&pinstance->outstanding_cmds, 0);
5247 	atomic_set(&pinstance->last_message_id, 0);
5248 	atomic_set(&pinstance->expose_resources, 0);
5249 
5250 	INIT_LIST_HEAD(&pinstance->free_res_q);
5251 	INIT_LIST_HEAD(&pinstance->used_res_q);
5252 	INIT_LIST_HEAD(&pinstance->free_cmd_pool);
5253 	INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
5254 
5255 	spin_lock_init(&pinstance->free_pool_lock);
5256 	spin_lock_init(&pinstance->pending_pool_lock);
5257 	spin_lock_init(&pinstance->resource_lock);
5258 	mutex_init(&pinstance->aen_queue_lock);
5259 
5260 	/* Work-queue (Shared) for deferred processing error handling */
5261 	INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
5262 
5263 	/* Initialize the default log_level */
5264 	pinstance->current_log_level = pmcraid_log_level;
5265 
5266 	/* Setup variables required for reset engine */
5267 	pinstance->ioa_state = IOA_STATE_UNKNOWN;
5268 	pinstance->reset_cmd = NULL;
5269 	return 0;
5270 }
5271 
5272 /**
5273  * pmcraid_shutdown - shutdown adapter controller.
5274  * @pdev: pci device struct
5275  *
5276  * Issues an adapter shutdown to the card waits for its completion
5277  *
5278  * Return value
5279  *	  none
5280  */
5281 static void pmcraid_shutdown(struct pci_dev *pdev)
5282 {
5283 	struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5284 	pmcraid_reset_bringdown(pinstance);
5285 }
5286 
5287 
5288 /**
5289  * pmcraid_get_minor - returns unused minor number from minor number bitmap
5290  */
5291 static unsigned short pmcraid_get_minor(void)
5292 {
5293 	int minor;
5294 
5295 	minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
5296 	__set_bit(minor, pmcraid_minor);
5297 	return minor;
5298 }
5299 
5300 /**
5301  * pmcraid_release_minor - releases given minor back to minor number bitmap
5302  */
5303 static void pmcraid_release_minor(unsigned short minor)
5304 {
5305 	__clear_bit(minor, pmcraid_minor);
5306 }
5307 
5308 /**
5309  * pmcraid_setup_chrdev - allocates a minor number and registers a char device
5310  *
5311  * @pinstance: pointer to adapter instance for which to register device
5312  *
5313  * Return value
5314  *	0 in case of success, otherwise non-zero
5315  */
5316 static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
5317 {
5318 	int minor;
5319 	int error;
5320 
5321 	minor = pmcraid_get_minor();
5322 	cdev_init(&pinstance->cdev, &pmcraid_fops);
5323 	pinstance->cdev.owner = THIS_MODULE;
5324 
5325 	error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
5326 
5327 	if (error)
5328 		pmcraid_release_minor(minor);
5329 	else
5330 		device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
5331 			      NULL, "%s%u", PMCRAID_DEVFILE, minor);
5332 	return error;
5333 }
5334 
5335 /**
5336  * pmcraid_release_chrdev - unregisters per-adapter management interface
5337  *
5338  * @pinstance: pointer to adapter instance structure
5339  *
5340  * Return value
5341  *  none
5342  */
5343 static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
5344 {
5345 	pmcraid_release_minor(MINOR(pinstance->cdev.dev));
5346 	device_destroy(pmcraid_class,
5347 		       MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
5348 	cdev_del(&pinstance->cdev);
5349 }
5350 
5351 /**
5352  * pmcraid_remove - IOA hot plug remove entry point
5353  * @pdev: pci device struct
5354  *
5355  * Return value
5356  *	  none
5357  */
5358 static void pmcraid_remove(struct pci_dev *pdev)
5359 {
5360 	struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5361 
5362 	/* remove the management interface (/dev file) for this device */
5363 	pmcraid_release_chrdev(pinstance);
5364 
5365 	/* remove host template from scsi midlayer */
5366 	scsi_remove_host(pinstance->host);
5367 
5368 	/* block requests from mid-layer */
5369 	scsi_block_requests(pinstance->host);
5370 
5371 	/* initiate shutdown adapter */
5372 	pmcraid_shutdown(pdev);
5373 
5374 	pmcraid_disable_interrupts(pinstance, ~0);
5375 	flush_work(&pinstance->worker_q);
5376 
5377 	pmcraid_kill_tasklets(pinstance);
5378 	pmcraid_unregister_interrupt_handler(pinstance);
5379 	pmcraid_release_buffers(pinstance);
5380 	iounmap(pinstance->mapped_dma_addr);
5381 	pci_release_regions(pdev);
5382 	scsi_host_put(pinstance->host);
5383 	pci_disable_device(pdev);
5384 
5385 	return;
5386 }
5387 
5388 #ifdef CONFIG_PM
5389 /**
5390  * pmcraid_suspend - driver suspend entry point for power management
5391  * @pdev:   PCI device structure
5392  * @state:  PCI power state to suspend routine
5393  *
5394  * Return Value - 0 always
5395  */
5396 static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
5397 {
5398 	struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5399 
5400 	pmcraid_shutdown(pdev);
5401 	pmcraid_disable_interrupts(pinstance, ~0);
5402 	pmcraid_kill_tasklets(pinstance);
5403 	pci_set_drvdata(pinstance->pdev, pinstance);
5404 	pmcraid_unregister_interrupt_handler(pinstance);
5405 	pci_save_state(pdev);
5406 	pci_disable_device(pdev);
5407 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
5408 
5409 	return 0;
5410 }
5411 
5412 /**
5413  * pmcraid_resume - driver resume entry point PCI power management
5414  * @pdev: PCI device structure
5415  *
5416  * Return Value - 0 in case of success. Error code in case of any failure
5417  */
5418 static int pmcraid_resume(struct pci_dev *pdev)
5419 {
5420 	struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
5421 	struct Scsi_Host *host = pinstance->host;
5422 	int rc;
5423 
5424 	pci_set_power_state(pdev, PCI_D0);
5425 	pci_enable_wake(pdev, PCI_D0, 0);
5426 	pci_restore_state(pdev);
5427 
5428 	rc = pci_enable_device(pdev);
5429 
5430 	if (rc) {
5431 		dev_err(&pdev->dev, "resume: Enable device failed\n");
5432 		return rc;
5433 	}
5434 
5435 	pci_set_master(pdev);
5436 
5437 	if ((sizeof(dma_addr_t) == 4) ||
5438 	     pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5439 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5440 
5441 	if (rc == 0)
5442 		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5443 
5444 	if (rc != 0) {
5445 		dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
5446 		goto disable_device;
5447 	}
5448 
5449 	pmcraid_disable_interrupts(pinstance, ~0);
5450 	atomic_set(&pinstance->outstanding_cmds, 0);
5451 	rc = pmcraid_register_interrupt_handler(pinstance);
5452 
5453 	if (rc) {
5454 		dev_err(&pdev->dev,
5455 			"resume: couldn't register interrupt handlers\n");
5456 		rc = -ENODEV;
5457 		goto release_host;
5458 	}
5459 
5460 	pmcraid_init_tasklets(pinstance);
5461 	pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5462 
5463 	/* Start with hard reset sequence which brings up IOA to operational
5464 	 * state as well as completes the reset sequence.
5465 	 */
5466 	pinstance->ioa_hard_reset = 1;
5467 
5468 	/* Start IOA firmware initialization and bring card to Operational
5469 	 * state.
5470 	 */
5471 	if (pmcraid_reset_bringup(pinstance)) {
5472 		dev_err(&pdev->dev, "couldn't initialize IOA\n");
5473 		rc = -ENODEV;
5474 		goto release_tasklets;
5475 	}
5476 
5477 	return 0;
5478 
5479 release_tasklets:
5480 	pmcraid_disable_interrupts(pinstance, ~0);
5481 	pmcraid_kill_tasklets(pinstance);
5482 	pmcraid_unregister_interrupt_handler(pinstance);
5483 
5484 release_host:
5485 	scsi_host_put(host);
5486 
5487 disable_device:
5488 	pci_disable_device(pdev);
5489 
5490 	return rc;
5491 }
5492 
5493 #else
5494 
5495 #define pmcraid_suspend NULL
5496 #define pmcraid_resume  NULL
5497 
5498 #endif /* CONFIG_PM */
5499 
5500 /**
5501  * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
5502  *				completion of the ioa reset
5503  * @cmd: pointer to reset command block
5504  */
5505 static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
5506 {
5507 	struct pmcraid_instance *pinstance = cmd->drv_inst;
5508 	unsigned long flags;
5509 
5510 	spin_lock_irqsave(pinstance->host->host_lock, flags);
5511 	pmcraid_ioa_reset(cmd);
5512 	spin_unlock_irqrestore(pinstance->host->host_lock, flags);
5513 	scsi_unblock_requests(pinstance->host);
5514 	schedule_work(&pinstance->worker_q);
5515 }
5516 
5517 /**
5518  * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
5519  *
5520  * @cmd: pointer to pmcraid_cmd structure
5521  *
5522  * Return Value
5523  *  0 for success or non-zero for failure cases
5524  */
5525 static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
5526 {
5527 	struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5528 	void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
5529 
5530 	pmcraid_reinit_cmdblk(cmd);
5531 
5532 	ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5533 	ioarcb->request_type = REQ_TYPE_IOACMD;
5534 	ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
5535 	ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
5536 
5537 	/* If this was called as part of resource table reinitialization due to
5538 	 * lost CCN, it is enough to return the command block back to free pool
5539 	 * as part of set_supported_devs completion function.
5540 	 */
5541 	if (cmd->drv_inst->reinit_cfg_table) {
5542 		cmd->drv_inst->reinit_cfg_table = 0;
5543 		cmd->release = 1;
5544 		cmd_done = pmcraid_reinit_cfgtable_done;
5545 	}
5546 
5547 	/* we will be done with the reset sequence after set supported devices,
5548 	 * setup the done function to return the command block back to free
5549 	 * pool
5550 	 */
5551 	pmcraid_send_cmd(cmd,
5552 			 cmd_done,
5553 			 PMCRAID_SET_SUP_DEV_TIMEOUT,
5554 			 pmcraid_timeout_handler);
5555 	return;
5556 }
5557 
5558 /**
5559  * pmcraid_set_timestamp - set the timestamp to IOAFP
5560  *
5561  * @cmd: pointer to pmcraid_cmd structure
5562  *
5563  * Return Value
5564  *  0 for success or non-zero for failure cases
5565  */
5566 static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd)
5567 {
5568 	struct pmcraid_instance *pinstance = cmd->drv_inst;
5569 	struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5570 	__be32 time_stamp_len = cpu_to_be32(PMCRAID_TIMESTAMP_LEN);
5571 	struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
5572 
5573 	struct timeval tv;
5574 	__le64 timestamp;
5575 
5576 	do_gettimeofday(&tv);
5577 	timestamp = tv.tv_sec * 1000;
5578 
5579 	pinstance->timestamp_data->timestamp[0] = (__u8)(timestamp);
5580 	pinstance->timestamp_data->timestamp[1] = (__u8)((timestamp) >> 8);
5581 	pinstance->timestamp_data->timestamp[2] = (__u8)((timestamp) >> 16);
5582 	pinstance->timestamp_data->timestamp[3] = (__u8)((timestamp) >> 24);
5583 	pinstance->timestamp_data->timestamp[4] = (__u8)((timestamp) >> 32);
5584 	pinstance->timestamp_data->timestamp[5] = (__u8)((timestamp)  >> 40);
5585 
5586 	pmcraid_reinit_cmdblk(cmd);
5587 	ioarcb->request_type = REQ_TYPE_SCSI;
5588 	ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5589 	ioarcb->cdb[0] = PMCRAID_SCSI_SET_TIMESTAMP;
5590 	ioarcb->cdb[1] = PMCRAID_SCSI_SERVICE_ACTION;
5591 	memcpy(&(ioarcb->cdb[6]), &time_stamp_len, sizeof(time_stamp_len));
5592 
5593 	ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
5594 					offsetof(struct pmcraid_ioarcb,
5595 						add_data.u.ioadl[0]));
5596 	ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
5597 	ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
5598 
5599 	ioarcb->request_flags0 |= NO_LINK_DESCS;
5600 	ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
5601 	ioarcb->data_transfer_length =
5602 		cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
5603 	ioadl = &(ioarcb->add_data.u.ioadl[0]);
5604 	ioadl->flags = IOADL_FLAGS_LAST_DESC;
5605 	ioadl->address = cpu_to_le64(pinstance->timestamp_data_baddr);
5606 	ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
5607 
5608 	if (!pinstance->timestamp_error) {
5609 		pinstance->timestamp_error = 0;
5610 		pmcraid_send_cmd(cmd, pmcraid_set_supported_devs,
5611 			 PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
5612 	} else {
5613 		pmcraid_send_cmd(cmd, pmcraid_return_cmd,
5614 			 PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
5615 		return;
5616 	}
5617 }
5618 
5619 
5620 /**
5621  * pmcraid_init_res_table - Initialize the resource table
5622  * @cmd:  pointer to pmcraid command struct
5623  *
5624  * This function looks through the existing resource table, comparing
5625  * it with the config table. This function will take care of old/new
5626  * devices and schedule adding/removing them from the mid-layer
5627  * as appropriate.
5628  *
5629  * Return value
5630  *	 None
5631  */
5632 static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
5633 {
5634 	struct pmcraid_instance *pinstance = cmd->drv_inst;
5635 	struct pmcraid_resource_entry *res, *temp;
5636 	struct pmcraid_config_table_entry *cfgte;
5637 	unsigned long lock_flags;
5638 	int found, rc, i;
5639 	u16 fw_version;
5640 	LIST_HEAD(old_res);
5641 
5642 	if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
5643 		pmcraid_err("IOA requires microcode download\n");
5644 
5645 	fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
5646 
5647 	/* resource list is protected by pinstance->resource_lock.
5648 	 * init_res_table can be called from probe (user-thread) or runtime
5649 	 * reset (timer/tasklet)
5650 	 */
5651 	spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
5652 
5653 	list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
5654 		list_move_tail(&res->queue, &old_res);
5655 
5656 	for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
5657 		if (be16_to_cpu(pinstance->inq_data->fw_version) <=
5658 						PMCRAID_FW_VERSION_1)
5659 			cfgte = &pinstance->cfg_table->entries[i];
5660 		else
5661 			cfgte = (struct pmcraid_config_table_entry *)
5662 					&pinstance->cfg_table->entries_ext[i];
5663 
5664 		if (!pmcraid_expose_resource(fw_version, cfgte))
5665 			continue;
5666 
5667 		found = 0;
5668 
5669 		/* If this entry was already detected and initialized */
5670 		list_for_each_entry_safe(res, temp, &old_res, queue) {
5671 
5672 			rc = memcmp(&res->cfg_entry.resource_address,
5673 				    &cfgte->resource_address,
5674 				    sizeof(cfgte->resource_address));
5675 			if (!rc) {
5676 				list_move_tail(&res->queue,
5677 						&pinstance->used_res_q);
5678 				found = 1;
5679 				break;
5680 			}
5681 		}
5682 
5683 		/* If this is new entry, initialize it and add it the queue */
5684 		if (!found) {
5685 
5686 			if (list_empty(&pinstance->free_res_q)) {
5687 				pmcraid_err("Too many devices attached\n");
5688 				break;
5689 			}
5690 
5691 			found = 1;
5692 			res = list_entry(pinstance->free_res_q.next,
5693 					 struct pmcraid_resource_entry, queue);
5694 
5695 			res->scsi_dev = NULL;
5696 			res->change_detected = RES_CHANGE_ADD;
5697 			res->reset_progress = 0;
5698 			list_move_tail(&res->queue, &pinstance->used_res_q);
5699 		}
5700 
5701 		/* copy new configuration table entry details into driver
5702 		 * maintained resource entry
5703 		 */
5704 		if (found) {
5705 			memcpy(&res->cfg_entry, cfgte,
5706 					pinstance->config_table_entry_size);
5707 			pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
5708 				 res->cfg_entry.resource_type,
5709 				 (fw_version <= PMCRAID_FW_VERSION_1 ?
5710 					res->cfg_entry.unique_flags1 :
5711 						res->cfg_entry.array_id & 0xFF),
5712 				 le32_to_cpu(res->cfg_entry.resource_address));
5713 		}
5714 	}
5715 
5716 	/* Detect any deleted entries, mark them for deletion from mid-layer */
5717 	list_for_each_entry_safe(res, temp, &old_res, queue) {
5718 
5719 		if (res->scsi_dev) {
5720 			res->change_detected = RES_CHANGE_DEL;
5721 			res->cfg_entry.resource_handle =
5722 				PMCRAID_INVALID_RES_HANDLE;
5723 			list_move_tail(&res->queue, &pinstance->used_res_q);
5724 		} else {
5725 			list_move_tail(&res->queue, &pinstance->free_res_q);
5726 		}
5727 	}
5728 
5729 	/* release the resource list lock */
5730 	spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
5731 	pmcraid_set_timestamp(cmd);
5732 }
5733 
5734 /**
5735  * pmcraid_querycfg - Send a Query IOA Config to the adapter.
5736  * @cmd: pointer pmcraid_cmd struct
5737  *
5738  * This function sends a Query IOA Configuration command to the adapter to
5739  * retrieve the IOA configuration table.
5740  *
5741  * Return value:
5742  *	none
5743  */
5744 static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
5745 {
5746 	struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
5747 	struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
5748 	struct pmcraid_instance *pinstance = cmd->drv_inst;
5749 	int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
5750 
5751 	if (be16_to_cpu(pinstance->inq_data->fw_version) <=
5752 					PMCRAID_FW_VERSION_1)
5753 		pinstance->config_table_entry_size =
5754 			sizeof(struct pmcraid_config_table_entry);
5755 	else
5756 		pinstance->config_table_entry_size =
5757 			sizeof(struct pmcraid_config_table_entry_ext);
5758 
5759 	ioarcb->request_type = REQ_TYPE_IOACMD;
5760 	ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
5761 
5762 	ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
5763 
5764 	/* firmware requires 4-byte length field, specified in B.E format */
5765 	memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
5766 
5767 	/* Since entire config table can be described by single IOADL, it can
5768 	 * be part of IOARCB itself
5769 	 */
5770 	ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
5771 					offsetof(struct pmcraid_ioarcb,
5772 						add_data.u.ioadl[0]));
5773 	ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
5774 	ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
5775 
5776 	ioarcb->request_flags0 |= NO_LINK_DESCS;
5777 	ioarcb->data_transfer_length =
5778 		cpu_to_le32(sizeof(struct pmcraid_config_table));
5779 
5780 	ioadl = &(ioarcb->add_data.u.ioadl[0]);
5781 	ioadl->flags = IOADL_FLAGS_LAST_DESC;
5782 	ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
5783 	ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
5784 
5785 	pmcraid_send_cmd(cmd, pmcraid_init_res_table,
5786 			 PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
5787 }
5788 
5789 
5790 /**
5791  * pmcraid_probe - PCI probe entry pointer for PMC MaxRAID controller driver
5792  * @pdev: pointer to pci device structure
5793  * @dev_id: pointer to device ids structure
5794  *
5795  * Return Value
5796  *	returns 0 if the device is claimed and successfully configured.
5797  *	returns non-zero error code in case of any failure
5798  */
5799 static int pmcraid_probe(struct pci_dev *pdev,
5800 			 const struct pci_device_id *dev_id)
5801 {
5802 	struct pmcraid_instance *pinstance;
5803 	struct Scsi_Host *host;
5804 	void __iomem *mapped_pci_addr;
5805 	int rc = PCIBIOS_SUCCESSFUL;
5806 
5807 	if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
5808 		pmcraid_err
5809 			("maximum number(%d) of supported adapters reached\n",
5810 			 atomic_read(&pmcraid_adapter_count));
5811 		return -ENOMEM;
5812 	}
5813 
5814 	atomic_inc(&pmcraid_adapter_count);
5815 	rc = pci_enable_device(pdev);
5816 
5817 	if (rc) {
5818 		dev_err(&pdev->dev, "Cannot enable adapter\n");
5819 		atomic_dec(&pmcraid_adapter_count);
5820 		return rc;
5821 	}
5822 
5823 	dev_info(&pdev->dev,
5824 		"Found new IOA(%x:%x), Total IOA count: %d\n",
5825 		 pdev->vendor, pdev->device,
5826 		 atomic_read(&pmcraid_adapter_count));
5827 
5828 	rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
5829 
5830 	if (rc < 0) {
5831 		dev_err(&pdev->dev,
5832 			"Couldn't register memory range of registers\n");
5833 		goto out_disable_device;
5834 	}
5835 
5836 	mapped_pci_addr = pci_iomap(pdev, 0, 0);
5837 
5838 	if (!mapped_pci_addr) {
5839 		dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
5840 		rc = -ENOMEM;
5841 		goto out_release_regions;
5842 	}
5843 
5844 	pci_set_master(pdev);
5845 
5846 	/* Firmware requires the system bus address of IOARCB to be within
5847 	 * 32-bit addressable range though it has 64-bit IOARRIN register.
5848 	 * However, firmware supports 64-bit streaming DMA buffers, whereas
5849 	 * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
5850 	 * returns memory within 4GB (if not, change this logic), coherent
5851 	 * buffers are within firmware acceptable address ranges.
5852 	 */
5853 	if ((sizeof(dma_addr_t) == 4) ||
5854 	    pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
5855 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5856 
5857 	/* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
5858 	 * bit mask for pci_alloc_consistent to return addresses within 4GB
5859 	 */
5860 	if (rc == 0)
5861 		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5862 
5863 	if (rc != 0) {
5864 		dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
5865 		goto cleanup_nomem;
5866 	}
5867 
5868 	host = scsi_host_alloc(&pmcraid_host_template,
5869 				sizeof(struct pmcraid_instance));
5870 
5871 	if (!host) {
5872 		dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
5873 		rc = -ENOMEM;
5874 		goto cleanup_nomem;
5875 	}
5876 
5877 	host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
5878 	host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
5879 	host->unique_id = host->host_no;
5880 	host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
5881 	host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
5882 
5883 	/* zero out entire instance structure */
5884 	pinstance = (struct pmcraid_instance *)host->hostdata;
5885 	memset(pinstance, 0, sizeof(*pinstance));
5886 
5887 	pinstance->chip_cfg =
5888 		(struct pmcraid_chip_details *)(dev_id->driver_data);
5889 
5890 	rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
5891 
5892 	if (rc < 0) {
5893 		dev_err(&pdev->dev, "failed to initialize adapter instance\n");
5894 		goto out_scsi_host_put;
5895 	}
5896 
5897 	pci_set_drvdata(pdev, pinstance);
5898 
5899 	/* Save PCI config-space for use following the reset */
5900 	rc = pci_save_state(pinstance->pdev);
5901 
5902 	if (rc != 0) {
5903 		dev_err(&pdev->dev, "Failed to save PCI config space\n");
5904 		goto out_scsi_host_put;
5905 	}
5906 
5907 	pmcraid_disable_interrupts(pinstance, ~0);
5908 
5909 	rc = pmcraid_register_interrupt_handler(pinstance);
5910 
5911 	if (rc) {
5912 		dev_err(&pdev->dev, "couldn't register interrupt handler\n");
5913 		goto out_scsi_host_put;
5914 	}
5915 
5916 	pmcraid_init_tasklets(pinstance);
5917 
5918 	/* allocate verious buffers used by LLD.*/
5919 	rc = pmcraid_init_buffers(pinstance);
5920 
5921 	if (rc) {
5922 		pmcraid_err("couldn't allocate memory blocks\n");
5923 		goto out_unregister_isr;
5924 	}
5925 
5926 	/* check the reset type required */
5927 	pmcraid_reset_type(pinstance);
5928 
5929 	pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
5930 
5931 	/* Start IOA firmware initialization and bring card to Operational
5932 	 * state.
5933 	 */
5934 	pmcraid_info("starting IOA initialization sequence\n");
5935 	if (pmcraid_reset_bringup(pinstance)) {
5936 		dev_err(&pdev->dev, "couldn't initialize IOA\n");
5937 		rc = 1;
5938 		goto out_release_bufs;
5939 	}
5940 
5941 	/* Add adapter instance into mid-layer list */
5942 	rc = scsi_add_host(pinstance->host, &pdev->dev);
5943 	if (rc != 0) {
5944 		pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
5945 		goto out_release_bufs;
5946 	}
5947 
5948 	scsi_scan_host(pinstance->host);
5949 
5950 	rc = pmcraid_setup_chrdev(pinstance);
5951 
5952 	if (rc != 0) {
5953 		pmcraid_err("couldn't create mgmt interface, error: %x\n",
5954 			     rc);
5955 		goto out_remove_host;
5956 	}
5957 
5958 	/* Schedule worker thread to handle CCN and take care of adding and
5959 	 * removing devices to OS
5960 	 */
5961 	atomic_set(&pinstance->expose_resources, 1);
5962 	schedule_work(&pinstance->worker_q);
5963 	return rc;
5964 
5965 out_remove_host:
5966 	scsi_remove_host(host);
5967 
5968 out_release_bufs:
5969 	pmcraid_release_buffers(pinstance);
5970 
5971 out_unregister_isr:
5972 	pmcraid_kill_tasklets(pinstance);
5973 	pmcraid_unregister_interrupt_handler(pinstance);
5974 
5975 out_scsi_host_put:
5976 	scsi_host_put(host);
5977 
5978 cleanup_nomem:
5979 	iounmap(mapped_pci_addr);
5980 
5981 out_release_regions:
5982 	pci_release_regions(pdev);
5983 
5984 out_disable_device:
5985 	atomic_dec(&pmcraid_adapter_count);
5986 	pci_disable_device(pdev);
5987 	return -ENODEV;
5988 }
5989 
5990 /*
5991  * PCI driver structure of pcmraid driver
5992  */
5993 static struct pci_driver pmcraid_driver = {
5994 	.name = PMCRAID_DRIVER_NAME,
5995 	.id_table = pmcraid_pci_table,
5996 	.probe = pmcraid_probe,
5997 	.remove = pmcraid_remove,
5998 	.suspend = pmcraid_suspend,
5999 	.resume = pmcraid_resume,
6000 	.shutdown = pmcraid_shutdown
6001 };
6002 
6003 /**
6004  * pmcraid_init - module load entry point
6005  */
6006 static int __init pmcraid_init(void)
6007 {
6008 	dev_t dev;
6009 	int error;
6010 
6011 	pmcraid_info("%s Device Driver version: %s\n",
6012 			 PMCRAID_DRIVER_NAME, PMCRAID_DRIVER_VERSION);
6013 
6014 	error = alloc_chrdev_region(&dev, 0,
6015 				    PMCRAID_MAX_ADAPTERS,
6016 				    PMCRAID_DEVFILE);
6017 
6018 	if (error) {
6019 		pmcraid_err("failed to get a major number for adapters\n");
6020 		goto out_init;
6021 	}
6022 
6023 	pmcraid_major = MAJOR(dev);
6024 	pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
6025 
6026 	if (IS_ERR(pmcraid_class)) {
6027 		error = PTR_ERR(pmcraid_class);
6028 		pmcraid_err("failed to register with sysfs, error = %x\n",
6029 			    error);
6030 		goto out_unreg_chrdev;
6031 	}
6032 
6033 	error = pmcraid_netlink_init();
6034 
6035 	if (error)
6036 		goto out_unreg_chrdev;
6037 
6038 	error = pci_register_driver(&pmcraid_driver);
6039 
6040 	if (error == 0)
6041 		goto out_init;
6042 
6043 	pmcraid_err("failed to register pmcraid driver, error = %x\n",
6044 		     error);
6045 	class_destroy(pmcraid_class);
6046 	pmcraid_netlink_release();
6047 
6048 out_unreg_chrdev:
6049 	unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
6050 
6051 out_init:
6052 	return error;
6053 }
6054 
6055 /**
6056  * pmcraid_exit - module unload entry point
6057  */
6058 static void __exit pmcraid_exit(void)
6059 {
6060 	pmcraid_netlink_release();
6061 	unregister_chrdev_region(MKDEV(pmcraid_major, 0),
6062 				 PMCRAID_MAX_ADAPTERS);
6063 	pci_unregister_driver(&pmcraid_driver);
6064 	class_destroy(pmcraid_class);
6065 }
6066 
6067 module_init(pmcraid_init);
6068 module_exit(pmcraid_exit);
6069