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