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