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 */
62static unsigned int pmcraid_debug_log;
63static unsigned int pmcraid_disable_aen;
64static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
65static 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 */
71static 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 */
78static unsigned int pmcraid_major;
79static struct class *pmcraid_class;
80static DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
81
82/*
83 * Module parameters
84 */
85MODULE_AUTHOR("Anil Ravindranath<anil_ravindranath@pmc-sierra.com>");
86MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
87MODULE_LICENSE("GPL");
88MODULE_VERSION(PMCRAID_DRIVER_VERSION);
89
90module_param_named(log_level, pmcraid_log_level, uint, (S_IRUGO | S_IWUSR));
91MODULE_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
96module_param_named(debug, pmcraid_debug_log, uint, (S_IRUGO | S_IWUSR));
97MODULE_PARM_DESC(debug,
98 "Enable driver verbose message logging. Set 1 to enable."
99 "(default: 0)");
100
101module_param_named(disable_aen, pmcraid_disable_aen, uint, (S_IRUGO | S_IWUSR));
102MODULE_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 */
109static 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 */
129static 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
136MODULE_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 */
152static 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 */
225static 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 */
273static 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 */
293static 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 */
309static 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 */
362static 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 */
374static 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 */
404static 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 */
422static 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 */
438static 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 */
466static 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 */
495static 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 */
529static 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
558static void pmcraid_ioa_reset(struct pmcraid_cmd *);
559
560static 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 */
592static 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 */
617static 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 */
653static void pmcraid_notify_ioastate(struct pmcraid_instance *, u32);
654static 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 */
701static 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 */
774static 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 */
812static 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 */
836static 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 */
872static 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 */
906static 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 */
934static 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 */
952static 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 */
987static void pmcraid_querycfg(struct pmcraid_cmd *);
988
989static 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 */
1018static 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 */
1060static 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
1106static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
1107static 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 */
1117static 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 */
1136static 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 */
1207static 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 */
1220static 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 */
1252static 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 */
1286static 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 */
1304static 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 */
1320static 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 */
1339enum {
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 */
1347enum {
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
1354static struct genl_multicast_group pmcraid_mcgrps[] = {
1355 { .name = "events", /* not really used - see ID discussion below */ },
1356};
1357
1358static 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 */
1374static 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 */
1395static 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 */
1408static 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 */
1479static 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 */
1494static 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 */
1510static 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
1526static 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
1658out_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 */
1675static 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 */
1690static 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 */
1713static 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 */
1762static 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 */
1796static void pmcraid_initiate_reset(struct pmcraid_instance *);
1797static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd);
1798
1799static 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 */
1854static 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 */
1864static 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 */
1902static void pmcraid_reinit_buffers(struct pmcraid_instance *);
1903
1904static 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 */
1934static 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 */
1984static 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 */
2001static 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 */
2073static 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 */
2295static 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 */
2334static 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
2392out_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 */
2405static 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 */
2420static 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 */
2436static 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 */
2490static 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 */
2526static 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 */
2598static 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 */
2714static 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
2822static 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
2857static 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 */
2874static 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 */
2920static 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 */
2957static 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 */
3041static 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
3050static 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
3059static 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 */
3078static 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 */
3111static struct pmcraid_ioadl_desc *
3112pmcraid_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 */
3159static 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 */
3217static 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 */
3233static 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 */
3266static 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 */
3340static 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
3448static DEF_SCSI_QCMD(pmcraid_queuecommand)
3449
3450/**
3451 * pmcraid_open -char node "open" entry, allowed only users with admin access
3452 */
3453static 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 */
3473static 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 */
3498static 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 */
3557static 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 */
3585static 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 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 direction = DMA_TO_DEVICE;
3653 } else {
3654 direction = DMA_FROM_DEVICE;
3655 }
3656
3657 if (request_size < 0) {
3658 rc = -EINVAL;
3659 goto out_free_buffer;
3660 }
3661
3662 /* check if we have any additional command parameters */
3663 if (le16_to_cpu(buffer->ioarcb.add_cmd_param_length)
3664 > PMCRAID_ADD_CMD_PARAM_LEN) {
3665 rc = -EINVAL;
3666 goto out_free_buffer;
3667 }
3668
3669 cmd = pmcraid_get_free_cmd(pinstance);
3670
3671 if (!cmd) {
3672 pmcraid_err("free command block is not available\n");
3673 rc = -ENOMEM;
3674 goto out_free_buffer;
3675 }
3676
3677 cmd->scsi_cmd = NULL;
3678 ioarcb = &(cmd->ioa_cb->ioarcb);
3679
3680 /* Copy the user-provided IOARCB stuff field by field */
3681 ioarcb->resource_handle = buffer->ioarcb.resource_handle;
3682 ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
3683 ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
3684 ioarcb->request_type = buffer->ioarcb.request_type;
3685 ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
3686 ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
3687 memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
3688
3689 if (buffer->ioarcb.add_cmd_param_length) {
3690 ioarcb->add_cmd_param_length =
3691 buffer->ioarcb.add_cmd_param_length;
3692 ioarcb->add_cmd_param_offset =
3693 buffer->ioarcb.add_cmd_param_offset;
3694 memcpy(ioarcb->add_data.u.add_cmd_params,
3695 buffer->ioarcb.add_data.u.add_cmd_params,
3696 le16_to_cpu(buffer->ioarcb.add_cmd_param_length));
3697 }
3698
3699 /* set hrrq number where the IOA should respond to. Note that all cmds
3700 * generated internally uses hrrq_id 0, exception to this is the cmd
3701 * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
3702 * hrrq_id assigned here in queuecommand
3703 */
3704 ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
3705 pinstance->num_hrrq;
3706
3707 if (request_size) {
3708 rc = pmcraid_build_passthrough_ioadls(cmd,
3709 request_size,
3710 direction);
3711 if (rc) {
3712 pmcraid_err("couldn't build passthrough ioadls\n");
3713 goto out_free_cmd;
3714 }
3715 }
3716
3717 /* If data is being written into the device, copy the data from user
3718 * buffers
3719 */
3720 if (direction == DMA_TO_DEVICE && request_size > 0) {
3721 rc = pmcraid_copy_sglist(cmd->sglist,
3722 request_buffer,
3723 request_size,
3724 direction);
3725 if (rc) {
3726 pmcraid_err("failed to copy user buffer\n");
3727 goto out_free_sglist;
3728 }
3729 }
3730
3731 /* passthrough ioctl is a blocking command so, put the user to sleep
3732 * until timeout. Note that a timeout value of 0 means, do timeout.
3733 */
3734 cmd->cmd_done = pmcraid_internal_done;
3735 init_completion(&cmd->wait_for_completion);
3736 cmd->completion_req = 1;
3737
3738 pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
3739 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
3740 cmd->ioa_cb->ioarcb.cdb[0],
3741 le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
3742
3743 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3744 _pmcraid_fire_command(cmd);
3745 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3746
3747 /* NOTE ! Remove the below line once abort_task is implemented
3748 * in firmware. This line disables ioctl command timeout handling logic
3749 * similar to IO command timeout handling, making ioctl commands to wait
3750 * until the command completion regardless of timeout value specified in
3751 * ioarcb
3752 */
3753 buffer->ioarcb.cmd_timeout = 0;
3754
3755 /* If command timeout is specified put caller to wait till that time,
3756 * otherwise it would be blocking wait. If command gets timed out, it
3757 * will be aborted.
3758 */
3759 if (buffer->ioarcb.cmd_timeout == 0) {
3760 wait_for_completion(&cmd->wait_for_completion);
3761 } else if (!wait_for_completion_timeout(
3762 &cmd->wait_for_completion,
3763 msecs_to_jiffies(le16_to_cpu(buffer->ioarcb.cmd_timeout) * 1000))) {
3764
3765 pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
3766 le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
3767 cmd->ioa_cb->ioarcb.cdb[0]);
3768
3769 spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
3770 cancel_cmd = pmcraid_abort_cmd(cmd);
3771 spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
3772
3773 if (cancel_cmd) {
3774 wait_for_completion(&cancel_cmd->wait_for_completion);
3775 ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
3776 pmcraid_return_cmd(cancel_cmd);
3777
3778 /* if abort task couldn't find the command i.e it got
3779 * completed prior to aborting, return good completion.
3780 * if command got aborted successfully or there was IOA
3781 * reset due to abort task itself getting timedout then
3782 * return -ETIMEDOUT
3783 */
3784 if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
3785 PMCRAID_IOASC_SENSE_KEY(ioasc) == 0x00) {
3786 if (ioasc != PMCRAID_IOASC_GC_IOARCB_NOTFOUND)
3787 rc = -ETIMEDOUT;
3788 goto out_handle_response;
3789 }
3790 }
3791
3792 /* no command block for abort task or abort task failed to abort
3793 * the IOARCB, then wait for 150 more seconds and initiate reset
3794 * sequence after timeout
3795 */
3796 if (!wait_for_completion_timeout(
3797 &cmd->wait_for_completion,
3798 msecs_to_jiffies(150 * 1000))) {
3799 pmcraid_reset_bringup(cmd->drv_inst);
3800 rc = -ETIMEDOUT;
3801 }
3802 }
3803
3804out_handle_response:
3805 /* copy entire IOASA buffer and return IOCTL success.
3806 * If copying IOASA to user-buffer fails, return
3807 * EFAULT
3808 */
3809 if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
3810 size