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