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