[SCSI] aic7xxx: fix timer handling bug
[deliverable/linux.git] / drivers / scsi / megaraid / megaraid_sas.c
CommitLineData
c4a3e0a5
BS
1/*
2 *
3 * Linux MegaRAID driver for SAS based RAID controllers
4 *
5 * Copyright (c) 2003-2005 LSI Logic Corporation.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * FILE : megaraid_sas.c
13 * Version : v00.00.02.00-rc4
14 *
15 * Authors:
16 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
17 * Sumant Patro <Sumant.Patro@lsil.com>
18 *
19 * List of supported controllers
20 *
21 * OEM Product Name VID DID SSVID SSID
22 * --- ------------ --- --- ---- ----
23 */
24
25#include <linux/kernel.h>
26#include <linux/types.h>
27#include <linux/pci.h>
28#include <linux/list.h>
c4a3e0a5
BS
29#include <linux/moduleparam.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/interrupt.h>
33#include <linux/delay.h>
34#include <linux/uio.h>
35#include <asm/uaccess.h>
43399236 36#include <linux/fs.h>
c4a3e0a5
BS
37#include <linux/compat.h>
38
39#include <scsi/scsi.h>
40#include <scsi/scsi_cmnd.h>
41#include <scsi/scsi_device.h>
42#include <scsi/scsi_host.h>
43#include "megaraid_sas.h"
44
45MODULE_LICENSE("GPL");
46MODULE_VERSION(MEGASAS_VERSION);
47MODULE_AUTHOR("sreenivas.bagalkote@lsil.com");
48MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
49
50/*
51 * PCI ID table for all supported controllers
52 */
53static struct pci_device_id megasas_pci_table[] = {
54
55 {
56 PCI_VENDOR_ID_LSI_LOGIC,
57 PCI_DEVICE_ID_LSI_SAS1064R,
58 PCI_ANY_ID,
59 PCI_ANY_ID,
60 },
61 {
62 PCI_VENDOR_ID_DELL,
63 PCI_DEVICE_ID_DELL_PERC5,
64 PCI_ANY_ID,
65 PCI_ANY_ID,
66 },
67 {0} /* Terminating entry */
68};
69
70MODULE_DEVICE_TABLE(pci, megasas_pci_table);
71
72static int megasas_mgmt_majorno;
73static struct megasas_mgmt_info megasas_mgmt_info;
74static struct fasync_struct *megasas_async_queue;
75static DECLARE_MUTEX(megasas_async_queue_mutex);
76
77/**
78 * megasas_get_cmd - Get a command from the free pool
79 * @instance: Adapter soft state
80 *
81 * Returns a free command from the pool
82 */
83static inline struct megasas_cmd *megasas_get_cmd(struct megasas_instance
84 *instance)
85{
86 unsigned long flags;
87 struct megasas_cmd *cmd = NULL;
88
89 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
90
91 if (!list_empty(&instance->cmd_pool)) {
92 cmd = list_entry((&instance->cmd_pool)->next,
93 struct megasas_cmd, list);
94 list_del_init(&cmd->list);
95 } else {
96 printk(KERN_ERR "megasas: Command pool empty!\n");
97 }
98
99 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
100 return cmd;
101}
102
103/**
104 * megasas_return_cmd - Return a cmd to free command pool
105 * @instance: Adapter soft state
106 * @cmd: Command packet to be returned to free command pool
107 */
108static inline void
109megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
110{
111 unsigned long flags;
112
113 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
114
115 cmd->scmd = NULL;
116 list_add_tail(&cmd->list, &instance->cmd_pool);
117
118 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
119}
120
121/**
122 * megasas_enable_intr - Enables interrupts
123 * @regs: MFI register set
124 */
125static inline void
126megasas_enable_intr(struct megasas_register_set __iomem * regs)
127{
128 writel(1, &(regs)->outbound_intr_mask);
129
130 /* Dummy readl to force pci flush */
131 readl(&regs->outbound_intr_mask);
132}
133
134/**
135 * megasas_disable_intr - Disables interrupts
136 * @regs: MFI register set
137 */
138static inline void
139megasas_disable_intr(struct megasas_register_set __iomem * regs)
140{
141 u32 mask = readl(&regs->outbound_intr_mask) & (~0x00000001);
142 writel(mask, &regs->outbound_intr_mask);
143
144 /* Dummy readl to force pci flush */
145 readl(&regs->outbound_intr_mask);
146}
147
148/**
149 * megasas_issue_polled - Issues a polling command
150 * @instance: Adapter soft state
151 * @cmd: Command packet to be issued
152 *
153 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
154 */
155static int
156megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
157{
158 int i;
159 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
160
161 struct megasas_header *frame_hdr = &cmd->frame->hdr;
162
163 frame_hdr->cmd_status = 0xFF;
164 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
165
166 /*
167 * Issue the frame using inbound queue port
168 */
169 writel(cmd->frame_phys_addr >> 3,
170 &instance->reg_set->inbound_queue_port);
171
172 /*
173 * Wait for cmd_status to change
174 */
175 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
176 rmb();
177 msleep(1);
178 }
179
180 if (frame_hdr->cmd_status == 0xff)
181 return -ETIME;
182
183 return 0;
184}
185
186/**
187 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
188 * @instance: Adapter soft state
189 * @cmd: Command to be issued
190 *
191 * This function waits on an event for the command to be returned from ISR.
192 * Used to issue ioctl commands.
193 */
194static int
195megasas_issue_blocked_cmd(struct megasas_instance *instance,
196 struct megasas_cmd *cmd)
197{
198 cmd->cmd_status = ENODATA;
199
200 writel(cmd->frame_phys_addr >> 3,
201 &instance->reg_set->inbound_queue_port);
202
203 wait_event(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA));
204
205 return 0;
206}
207
208/**
209 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
210 * @instance: Adapter soft state
211 * @cmd_to_abort: Previously issued cmd to be aborted
212 *
213 * MFI firmware can abort previously issued AEN comamnd (automatic event
214 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
215 * cmd and blocks till it is completed.
216 */
217static int
218megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
219 struct megasas_cmd *cmd_to_abort)
220{
221 struct megasas_cmd *cmd;
222 struct megasas_abort_frame *abort_fr;
223
224 cmd = megasas_get_cmd(instance);
225
226 if (!cmd)
227 return -1;
228
229 abort_fr = &cmd->frame->abort;
230
231 /*
232 * Prepare and issue the abort frame
233 */
234 abort_fr->cmd = MFI_CMD_ABORT;
235 abort_fr->cmd_status = 0xFF;
236 abort_fr->flags = 0;
237 abort_fr->abort_context = cmd_to_abort->index;
238 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
239 abort_fr->abort_mfi_phys_addr_hi = 0;
240
241 cmd->sync_cmd = 1;
242 cmd->cmd_status = 0xFF;
243
244 writel(cmd->frame_phys_addr >> 3,
245 &instance->reg_set->inbound_queue_port);
246
247 /*
248 * Wait for this cmd to complete
249 */
250 wait_event(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF));
251
252 megasas_return_cmd(instance, cmd);
253 return 0;
254}
255
256/**
257 * megasas_make_sgl32 - Prepares 32-bit SGL
258 * @instance: Adapter soft state
259 * @scp: SCSI command from the mid-layer
260 * @mfi_sgl: SGL to be filled in
261 *
262 * If successful, this function returns the number of SG elements. Otherwise,
263 * it returnes -1.
264 */
265static inline int
266megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
267 union megasas_sgl *mfi_sgl)
268{
269 int i;
270 int sge_count;
271 struct scatterlist *os_sgl;
272
273 /*
274 * Return 0 if there is no data transfer
275 */
276 if (!scp->request_buffer || !scp->request_bufflen)
277 return 0;
278
279 if (!scp->use_sg) {
280 mfi_sgl->sge32[0].phys_addr = pci_map_single(instance->pdev,
281 scp->
282 request_buffer,
283 scp->
284 request_bufflen,
285 scp->
286 sc_data_direction);
287 mfi_sgl->sge32[0].length = scp->request_bufflen;
288
289 return 1;
290 }
291
292 os_sgl = (struct scatterlist *)scp->request_buffer;
293 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
294 scp->sc_data_direction);
295
296 for (i = 0; i < sge_count; i++, os_sgl++) {
297 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
298 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
299 }
300
301 return sge_count;
302}
303
304/**
305 * megasas_make_sgl64 - Prepares 64-bit SGL
306 * @instance: Adapter soft state
307 * @scp: SCSI command from the mid-layer
308 * @mfi_sgl: SGL to be filled in
309 *
310 * If successful, this function returns the number of SG elements. Otherwise,
311 * it returnes -1.
312 */
313static inline int
314megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
315 union megasas_sgl *mfi_sgl)
316{
317 int i;
318 int sge_count;
319 struct scatterlist *os_sgl;
320
321 /*
322 * Return 0 if there is no data transfer
323 */
324 if (!scp->request_buffer || !scp->request_bufflen)
325 return 0;
326
327 if (!scp->use_sg) {
328 mfi_sgl->sge64[0].phys_addr = pci_map_single(instance->pdev,
329 scp->
330 request_buffer,
331 scp->
332 request_bufflen,
333 scp->
334 sc_data_direction);
335
336 mfi_sgl->sge64[0].length = scp->request_bufflen;
337
338 return 1;
339 }
340
341 os_sgl = (struct scatterlist *)scp->request_buffer;
342 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
343 scp->sc_data_direction);
344
345 for (i = 0; i < sge_count; i++, os_sgl++) {
346 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
347 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
348 }
349
350 return sge_count;
351}
352
353/**
354 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
355 * @instance: Adapter soft state
356 * @scp: SCSI command
357 * @cmd: Command to be prepared in
358 *
359 * This function prepares CDB commands. These are typcially pass-through
360 * commands to the devices.
361 */
362static inline int
363megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
364 struct megasas_cmd *cmd)
365{
366 u32 sge_sz;
367 int sge_bytes;
368 u32 is_logical;
369 u32 device_id;
370 u16 flags = 0;
371 struct megasas_pthru_frame *pthru;
372
373 is_logical = MEGASAS_IS_LOGICAL(scp);
374 device_id = MEGASAS_DEV_INDEX(instance, scp);
375 pthru = (struct megasas_pthru_frame *)cmd->frame;
376
377 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
378 flags = MFI_FRAME_DIR_WRITE;
379 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
380 flags = MFI_FRAME_DIR_READ;
381 else if (scp->sc_data_direction == PCI_DMA_NONE)
382 flags = MFI_FRAME_DIR_NONE;
383
384 /*
385 * Prepare the DCDB frame
386 */
387 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
388 pthru->cmd_status = 0x0;
389 pthru->scsi_status = 0x0;
390 pthru->target_id = device_id;
391 pthru->lun = scp->device->lun;
392 pthru->cdb_len = scp->cmd_len;
393 pthru->timeout = 0;
394 pthru->flags = flags;
395 pthru->data_xfer_len = scp->request_bufflen;
396
397 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
398
399 /*
400 * Construct SGL
401 */
402 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
403 sizeof(struct megasas_sge32);
404
405 if (IS_DMA64) {
406 pthru->flags |= MFI_FRAME_SGL64;
407 pthru->sge_count = megasas_make_sgl64(instance, scp,
408 &pthru->sgl);
409 } else
410 pthru->sge_count = megasas_make_sgl32(instance, scp,
411 &pthru->sgl);
412
413 /*
414 * Sense info specific
415 */
416 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
417 pthru->sense_buf_phys_addr_hi = 0;
418 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
419
420 sge_bytes = sge_sz * pthru->sge_count;
421
422 /*
423 * Compute the total number of frames this command consumes. FW uses
424 * this number to pull sufficient number of frames from host memory.
425 */
426 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
427 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
428
429 if (cmd->frame_count > 7)
430 cmd->frame_count = 8;
431
432 return cmd->frame_count;
433}
434
435/**
436 * megasas_build_ldio - Prepares IOs to logical devices
437 * @instance: Adapter soft state
438 * @scp: SCSI command
439 * @cmd: Command to to be prepared
440 *
441 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
442 */
443static inline int
444megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
445 struct megasas_cmd *cmd)
446{
447 u32 sge_sz;
448 int sge_bytes;
449 u32 device_id;
450 u8 sc = scp->cmnd[0];
451 u16 flags = 0;
452 struct megasas_io_frame *ldio;
453
454 device_id = MEGASAS_DEV_INDEX(instance, scp);
455 ldio = (struct megasas_io_frame *)cmd->frame;
456
457 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
458 flags = MFI_FRAME_DIR_WRITE;
459 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
460 flags = MFI_FRAME_DIR_READ;
461
462 /*
463 * Preare the Logical IO frame: 2nd bit is zero for all read cmds
464 */
465 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
466 ldio->cmd_status = 0x0;
467 ldio->scsi_status = 0x0;
468 ldio->target_id = device_id;
469 ldio->timeout = 0;
470 ldio->reserved_0 = 0;
471 ldio->pad_0 = 0;
472 ldio->flags = flags;
473 ldio->start_lba_hi = 0;
474 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
475
476 /*
477 * 6-byte READ(0x08) or WRITE(0x0A) cdb
478 */
479 if (scp->cmd_len == 6) {
480 ldio->lba_count = (u32) scp->cmnd[4];
481 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
482 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
483
484 ldio->start_lba_lo &= 0x1FFFFF;
485 }
486
487 /*
488 * 10-byte READ(0x28) or WRITE(0x2A) cdb
489 */
490 else if (scp->cmd_len == 10) {
491 ldio->lba_count = (u32) scp->cmnd[8] |
492 ((u32) scp->cmnd[7] << 8);
493 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
494 ((u32) scp->cmnd[3] << 16) |
495 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
496 }
497
498 /*
499 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
500 */
501 else if (scp->cmd_len == 12) {
502 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
503 ((u32) scp->cmnd[7] << 16) |
504 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
505
506 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
507 ((u32) scp->cmnd[3] << 16) |
508 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
509 }
510
511 /*
512 * 16-byte READ(0x88) or WRITE(0x8A) cdb
513 */
514 else if (scp->cmd_len == 16) {
515 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
516 ((u32) scp->cmnd[11] << 16) |
517 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
518
519 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
520 ((u32) scp->cmnd[7] << 16) |
521 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
522
523 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
524 ((u32) scp->cmnd[3] << 16) |
525 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
526
527 }
528
529 /*
530 * Construct SGL
531 */
532 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
533 sizeof(struct megasas_sge32);
534
535 if (IS_DMA64) {
536 ldio->flags |= MFI_FRAME_SGL64;
537 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
538 } else
539 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
540
541 /*
542 * Sense info specific
543 */
544 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
545 ldio->sense_buf_phys_addr_hi = 0;
546 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
547
548 sge_bytes = sge_sz * ldio->sge_count;
549
550 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
551 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
552
553 if (cmd->frame_count > 7)
554 cmd->frame_count = 8;
555
556 return cmd->frame_count;
557}
558
559/**
560 * megasas_build_cmd - Prepares a command packet
561 * @instance: Adapter soft state
562 * @scp: SCSI command
563 * @frame_count: [OUT] Number of frames used to prepare this command
564 */
565static inline struct megasas_cmd *megasas_build_cmd(struct megasas_instance
566 *instance,
567 struct scsi_cmnd *scp,
568 int *frame_count)
569{
570 u32 logical_cmd;
571 struct megasas_cmd *cmd;
572
573 /*
574 * Find out if this is logical or physical drive command.
575 */
576 logical_cmd = MEGASAS_IS_LOGICAL(scp);
577
578 /*
579 * Logical drive command
580 */
581 if (logical_cmd) {
582
583 if (scp->device->id >= MEGASAS_MAX_LD) {
584 scp->result = DID_BAD_TARGET << 16;
585 return NULL;
586 }
587
588 switch (scp->cmnd[0]) {
589
590 case READ_10:
591 case WRITE_10:
592 case READ_12:
593 case WRITE_12:
594 case READ_6:
595 case WRITE_6:
596 case READ_16:
597 case WRITE_16:
598 /*
599 * Fail for LUN > 0
600 */
601 if (scp->device->lun) {
602 scp->result = DID_BAD_TARGET << 16;
603 return NULL;
604 }
605
606 cmd = megasas_get_cmd(instance);
607
608 if (!cmd) {
609 scp->result = DID_IMM_RETRY << 16;
610 return NULL;
611 }
612
613 *frame_count = megasas_build_ldio(instance, scp, cmd);
614
615 if (!(*frame_count)) {
616 megasas_return_cmd(instance, cmd);
617 return NULL;
618 }
619
620 return cmd;
621
622 default:
623 /*
624 * Fail for LUN > 0
625 */
626 if (scp->device->lun) {
627 scp->result = DID_BAD_TARGET << 16;
628 return NULL;
629 }
630
631 cmd = megasas_get_cmd(instance);
632
633 if (!cmd) {
634 scp->result = DID_IMM_RETRY << 16;
635 return NULL;
636 }
637
638 *frame_count = megasas_build_dcdb(instance, scp, cmd);
639
640 if (!(*frame_count)) {
641 megasas_return_cmd(instance, cmd);
642 return NULL;
643 }
644
645 return cmd;
646 }
647 } else {
648 cmd = megasas_get_cmd(instance);
649
650 if (!cmd) {
651 scp->result = DID_IMM_RETRY << 16;
652 return NULL;
653 }
654
655 *frame_count = megasas_build_dcdb(instance, scp, cmd);
656
657 if (!(*frame_count)) {
658 megasas_return_cmd(instance, cmd);
659 return NULL;
660 }
661
662 return cmd;
663 }
664
665 return NULL;
666}
667
668/**
669 * megasas_queue_command - Queue entry point
670 * @scmd: SCSI command to be queued
671 * @done: Callback entry point
672 */
673static int
674megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
675{
676 u32 frame_count;
677 unsigned long flags;
678 struct megasas_cmd *cmd;
679 struct megasas_instance *instance;
680
681 instance = (struct megasas_instance *)
682 scmd->device->host->hostdata;
683 scmd->scsi_done = done;
684 scmd->result = 0;
685
686 cmd = megasas_build_cmd(instance, scmd, &frame_count);
687
688 if (!cmd) {
689 done(scmd);
690 return 0;
691 }
692
693 cmd->scmd = scmd;
694 scmd->SCp.ptr = (char *)cmd;
695 scmd->SCp.sent_command = jiffies;
696
697 /*
698 * Issue the command to the FW
699 */
700 spin_lock_irqsave(&instance->instance_lock, flags);
701 instance->fw_outstanding++;
702 spin_unlock_irqrestore(&instance->instance_lock, flags);
703
704 writel(((cmd->frame_phys_addr >> 3) | (cmd->frame_count - 1)),
705 &instance->reg_set->inbound_queue_port);
706
707 return 0;
708}
709
710/**
711 * megasas_wait_for_outstanding - Wait for all outstanding cmds
712 * @instance: Adapter soft state
713 *
714 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
715 * complete all its outstanding commands. Returns error if one or more IOs
716 * are pending after this time period. It also marks the controller dead.
717 */
718static int megasas_wait_for_outstanding(struct megasas_instance *instance)
719{
720 int i;
721 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
722
723 for (i = 0; i < wait_time; i++) {
724
725 if (!instance->fw_outstanding)
726 break;
727
728 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
729 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
730 "commands to complete\n", i,
731 instance->fw_outstanding);
732 }
733
734 msleep(1000);
735 }
736
737 if (instance->fw_outstanding) {
738 instance->hw_crit_error = 1;
739 return FAILED;
740 }
741
742 return SUCCESS;
743}
744
745/**
746 * megasas_generic_reset - Generic reset routine
747 * @scmd: Mid-layer SCSI command
748 *
749 * This routine implements a generic reset handler for device, bus and host
750 * reset requests. Device, bus and host specific reset handlers can use this
751 * function after they do their specific tasks.
752 */
753static int megasas_generic_reset(struct scsi_cmnd *scmd)
754{
755 int ret_val;
756 struct megasas_instance *instance;
757
758 instance = (struct megasas_instance *)scmd->device->host->hostdata;
759
017560fc
JG
760 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
761 scmd->serial_number, scmd->cmnd[0]);
c4a3e0a5
BS
762
763 if (instance->hw_crit_error) {
764 printk(KERN_ERR "megasas: cannot recover from previous reset "
765 "failures\n");
766 return FAILED;
767 }
768
c4a3e0a5 769 ret_val = megasas_wait_for_outstanding(instance);
c4a3e0a5
BS
770 if (ret_val == SUCCESS)
771 printk(KERN_NOTICE "megasas: reset successful \n");
772 else
773 printk(KERN_ERR "megasas: failed to do reset\n");
774
c4a3e0a5
BS
775 return ret_val;
776}
777
778static enum scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
779{
780 unsigned long seconds;
781
782 if (scmd->SCp.ptr) {
783 seconds = (jiffies - scmd->SCp.sent_command) / HZ;
784
785 if (seconds < 90) {
786 return EH_RESET_TIMER;
787 } else {
788 return EH_NOT_HANDLED;
789 }
790 }
791
792 return EH_HANDLED;
793}
794
795/**
796 * megasas_reset_device - Device reset handler entry point
797 */
798static int megasas_reset_device(struct scsi_cmnd *scmd)
799{
800 int ret;
801
802 /*
803 * First wait for all commands to complete
804 */
805 ret = megasas_generic_reset(scmd);
806
807 return ret;
808}
809
810/**
811 * megasas_reset_bus_host - Bus & host reset handler entry point
812 */
813static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
814{
815 int ret;
816
817 /*
818 * Frist wait for all commands to complete
819 */
820 ret = megasas_generic_reset(scmd);
821
822 return ret;
823}
824
825/**
826 * megasas_service_aen - Processes an event notification
827 * @instance: Adapter soft state
828 * @cmd: AEN command completed by the ISR
829 *
830 * For AEN, driver sends a command down to FW that is held by the FW till an
831 * event occurs. When an event of interest occurs, FW completes the command
832 * that it was previously holding.
833 *
834 * This routines sends SIGIO signal to processes that have registered with the
835 * driver for AEN.
836 */
837static void
838megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
839{
840 /*
841 * Don't signal app if it is just an aborted previously registered aen
842 */
843 if (!cmd->abort_aen)
844 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
845 else
846 cmd->abort_aen = 0;
847
848 instance->aen_cmd = NULL;
849 megasas_return_cmd(instance, cmd);
850}
851
852/*
853 * Scsi host template for megaraid_sas driver
854 */
855static struct scsi_host_template megasas_template = {
856
857 .module = THIS_MODULE,
858 .name = "LSI Logic SAS based MegaRAID driver",
859 .proc_name = "megaraid_sas",
860 .queuecommand = megasas_queue_command,
861 .eh_device_reset_handler = megasas_reset_device,
862 .eh_bus_reset_handler = megasas_reset_bus_host,
863 .eh_host_reset_handler = megasas_reset_bus_host,
864 .eh_timed_out = megasas_reset_timer,
865 .use_clustering = ENABLE_CLUSTERING,
866};
867
868/**
869 * megasas_complete_int_cmd - Completes an internal command
870 * @instance: Adapter soft state
871 * @cmd: Command to be completed
872 *
873 * The megasas_issue_blocked_cmd() function waits for a command to complete
874 * after it issues a command. This function wakes up that waiting routine by
875 * calling wake_up() on the wait queue.
876 */
877static void
878megasas_complete_int_cmd(struct megasas_instance *instance,
879 struct megasas_cmd *cmd)
880{
881 cmd->cmd_status = cmd->frame->io.cmd_status;
882
883 if (cmd->cmd_status == ENODATA) {
884 cmd->cmd_status = 0;
885 }
886 wake_up(&instance->int_cmd_wait_q);
887}
888
889/**
890 * megasas_complete_abort - Completes aborting a command
891 * @instance: Adapter soft state
892 * @cmd: Cmd that was issued to abort another cmd
893 *
894 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
895 * after it issues an abort on a previously issued command. This function
896 * wakes up all functions waiting on the same wait queue.
897 */
898static void
899megasas_complete_abort(struct megasas_instance *instance,
900 struct megasas_cmd *cmd)
901{
902 if (cmd->sync_cmd) {
903 cmd->sync_cmd = 0;
904 cmd->cmd_status = 0;
905 wake_up(&instance->abort_cmd_wait_q);
906 }
907
908 return;
909}
910
911/**
912 * megasas_unmap_sgbuf - Unmap SG buffers
913 * @instance: Adapter soft state
914 * @cmd: Completed command
915 */
916static inline void
917megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
918{
919 dma_addr_t buf_h;
920 u8 opcode;
921
922 if (cmd->scmd->use_sg) {
923 pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
924 cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
925 return;
926 }
927
928 if (!cmd->scmd->request_bufflen)
929 return;
930
931 opcode = cmd->frame->hdr.cmd;
932
933 if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
934 if (IS_DMA64)
935 buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
936 else
937 buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
938 } else {
939 if (IS_DMA64)
940 buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
941 else
942 buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
943 }
944
945 pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
946 cmd->scmd->sc_data_direction);
947 return;
948}
949
950/**
951 * megasas_complete_cmd - Completes a command
952 * @instance: Adapter soft state
953 * @cmd: Command to be completed
954 * @alt_status: If non-zero, use this value as status to
955 * SCSI mid-layer instead of the value returned
956 * by the FW. This should be used if caller wants
957 * an alternate status (as in the case of aborted
958 * commands)
959 */
960static inline void
961megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
962 u8 alt_status)
963{
964 int exception = 0;
965 struct megasas_header *hdr = &cmd->frame->hdr;
966 unsigned long flags;
967
968 if (cmd->scmd) {
969 cmd->scmd->SCp.ptr = (char *)0;
970 }
971
972 switch (hdr->cmd) {
973
974 case MFI_CMD_PD_SCSI_IO:
975 case MFI_CMD_LD_SCSI_IO:
976
977 /*
978 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
979 * issued either through an IO path or an IOCTL path. If it
980 * was via IOCTL, we will send it to internal completion.
981 */
982 if (cmd->sync_cmd) {
983 cmd->sync_cmd = 0;
984 megasas_complete_int_cmd(instance, cmd);
985 break;
986 }
987
988 /*
989 * Don't export physical disk devices to mid-layer.
990 */
991 if (!MEGASAS_IS_LOGICAL(cmd->scmd) &&
992 (hdr->cmd_status == MFI_STAT_OK) &&
993 (cmd->scmd->cmnd[0] == INQUIRY)) {
994
995 if (((*(u8 *) cmd->scmd->request_buffer) & 0x1F) ==
996 TYPE_DISK) {
997 cmd->scmd->result = DID_BAD_TARGET << 16;
998 exception = 1;
999 }
1000 }
1001
1002 case MFI_CMD_LD_READ:
1003 case MFI_CMD_LD_WRITE:
1004
1005 if (alt_status) {
1006 cmd->scmd->result = alt_status << 16;
1007 exception = 1;
1008 }
1009
1010 if (exception) {
1011
1012 spin_lock_irqsave(&instance->instance_lock, flags);
1013 instance->fw_outstanding--;
1014 spin_unlock_irqrestore(&instance->instance_lock, flags);
1015
1016 megasas_unmap_sgbuf(instance, cmd);
1017 cmd->scmd->scsi_done(cmd->scmd);
1018 megasas_return_cmd(instance, cmd);
1019
1020 break;
1021 }
1022
1023 switch (hdr->cmd_status) {
1024
1025 case MFI_STAT_OK:
1026 cmd->scmd->result = DID_OK << 16;
1027 break;
1028
1029 case MFI_STAT_SCSI_IO_FAILED:
1030 case MFI_STAT_LD_INIT_IN_PROGRESS:
1031 cmd->scmd->result =
1032 (DID_ERROR << 16) | hdr->scsi_status;
1033 break;
1034
1035 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1036
1037 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1038
1039 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1040 memset(cmd->scmd->sense_buffer, 0,
1041 SCSI_SENSE_BUFFERSIZE);
1042 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1043 hdr->sense_len);
1044
1045 cmd->scmd->result |= DRIVER_SENSE << 24;
1046 }
1047
1048 break;
1049
1050 case MFI_STAT_LD_OFFLINE:
1051 case MFI_STAT_DEVICE_NOT_FOUND:
1052 cmd->scmd->result = DID_BAD_TARGET << 16;
1053 break;
1054
1055 default:
1056 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1057 hdr->cmd_status);
1058 cmd->scmd->result = DID_ERROR << 16;
1059 break;
1060 }
1061
1062 spin_lock_irqsave(&instance->instance_lock, flags);
1063 instance->fw_outstanding--;
1064 spin_unlock_irqrestore(&instance->instance_lock, flags);
1065
1066 megasas_unmap_sgbuf(instance, cmd);
1067 cmd->scmd->scsi_done(cmd->scmd);
1068 megasas_return_cmd(instance, cmd);
1069
1070 break;
1071
1072 case MFI_CMD_SMP:
1073 case MFI_CMD_STP:
1074 case MFI_CMD_DCMD:
1075
1076 /*
1077 * See if got an event notification
1078 */
1079 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1080 megasas_service_aen(instance, cmd);
1081 else
1082 megasas_complete_int_cmd(instance, cmd);
1083
1084 break;
1085
1086 case MFI_CMD_ABORT:
1087 /*
1088 * Cmd issued to abort another cmd returned
1089 */
1090 megasas_complete_abort(instance, cmd);
1091 break;
1092
1093 default:
1094 printk("megasas: Unknown command completed! [0x%X]\n",
1095 hdr->cmd);
1096 break;
1097 }
1098}
1099
1100/**
1101 * megasas_deplete_reply_queue - Processes all completed commands
1102 * @instance: Adapter soft state
1103 * @alt_status: Alternate status to be returned to
1104 * SCSI mid-layer instead of the status
1105 * returned by the FW
1106 */
1107static inline int
1108megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1109{
1110 u32 status;
1111 u32 producer;
1112 u32 consumer;
1113 u32 context;
1114 struct megasas_cmd *cmd;
1115
1116 /*
1117 * Check if it is our interrupt
1118 */
1119 status = readl(&instance->reg_set->outbound_intr_status);
1120
1121 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
1122 return IRQ_NONE;
1123 }
1124
1125 /*
1126 * Clear the interrupt by writing back the same value
1127 */
1128 writel(status, &instance->reg_set->outbound_intr_status);
1129
1130 producer = *instance->producer;
1131 consumer = *instance->consumer;
1132
1133 while (consumer != producer) {
1134 context = instance->reply_queue[consumer];
1135
1136 cmd = instance->cmd_list[context];
1137
1138 megasas_complete_cmd(instance, cmd, alt_status);
1139
1140 consumer++;
1141 if (consumer == (instance->max_fw_cmds + 1)) {
1142 consumer = 0;
1143 }
1144 }
1145
1146 *instance->consumer = producer;
1147
1148 return IRQ_HANDLED;
1149}
1150
1151/**
1152 * megasas_isr - isr entry point
1153 */
1154static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
1155{
1156 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1157 DID_OK);
1158}
1159
1160/**
1161 * megasas_transition_to_ready - Move the FW to READY state
1162 * @reg_set: MFI register set
1163 *
1164 * During the initialization, FW passes can potentially be in any one of
1165 * several possible states. If the FW in operational, waiting-for-handshake
1166 * states, driver must take steps to bring it to ready state. Otherwise, it
1167 * has to wait for the ready state.
1168 */
1169static int
1170megasas_transition_to_ready(struct megasas_register_set __iomem * reg_set)
1171{
1172 int i;
1173 u8 max_wait;
1174 u32 fw_state;
1175 u32 cur_state;
1176
1177 fw_state = readl(&reg_set->outbound_msg_0) & MFI_STATE_MASK;
1178
1179 while (fw_state != MFI_STATE_READY) {
1180
1181 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1182 " state\n");
1183 switch (fw_state) {
1184
1185 case MFI_STATE_FAULT:
1186
1187 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1188 return -ENODEV;
1189
1190 case MFI_STATE_WAIT_HANDSHAKE:
1191 /*
1192 * Set the CLR bit in inbound doorbell
1193 */
1194 writel(MFI_INIT_CLEAR_HANDSHAKE,
1195 &reg_set->inbound_doorbell);
1196
1197 max_wait = 2;
1198 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1199 break;
1200
1201 case MFI_STATE_OPERATIONAL:
1202 /*
1203 * Bring it to READY state; assuming max wait 2 secs
1204 */
1205 megasas_disable_intr(reg_set);
1206 writel(MFI_INIT_READY, &reg_set->inbound_doorbell);
1207
1208 max_wait = 10;
1209 cur_state = MFI_STATE_OPERATIONAL;
1210 break;
1211
1212 case MFI_STATE_UNDEFINED:
1213 /*
1214 * This state should not last for more than 2 seconds
1215 */
1216 max_wait = 2;
1217 cur_state = MFI_STATE_UNDEFINED;
1218 break;
1219
1220 case MFI_STATE_BB_INIT:
1221 max_wait = 2;
1222 cur_state = MFI_STATE_BB_INIT;
1223 break;
1224
1225 case MFI_STATE_FW_INIT:
1226 max_wait = 20;
1227 cur_state = MFI_STATE_FW_INIT;
1228 break;
1229
1230 case MFI_STATE_FW_INIT_2:
1231 max_wait = 20;
1232 cur_state = MFI_STATE_FW_INIT_2;
1233 break;
1234
1235 case MFI_STATE_DEVICE_SCAN:
1236 max_wait = 20;
1237 cur_state = MFI_STATE_DEVICE_SCAN;
1238 break;
1239
1240 case MFI_STATE_FLUSH_CACHE:
1241 max_wait = 20;
1242 cur_state = MFI_STATE_FLUSH_CACHE;
1243 break;
1244
1245 default:
1246 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1247 fw_state);
1248 return -ENODEV;
1249 }
1250
1251 /*
1252 * The cur_state should not last for more than max_wait secs
1253 */
1254 for (i = 0; i < (max_wait * 1000); i++) {
1255 fw_state = MFI_STATE_MASK &
1256 readl(&reg_set->outbound_msg_0);
1257
1258 if (fw_state == cur_state) {
1259 msleep(1);
1260 } else
1261 break;
1262 }
1263
1264 /*
1265 * Return error if fw_state hasn't changed after max_wait
1266 */
1267 if (fw_state == cur_state) {
1268 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1269 "in %d secs\n", fw_state, max_wait);
1270 return -ENODEV;
1271 }
1272 };
1273
1274 return 0;
1275}
1276
1277/**
1278 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1279 * @instance: Adapter soft state
1280 */
1281static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1282{
1283 int i;
1284 u32 max_cmd = instance->max_fw_cmds;
1285 struct megasas_cmd *cmd;
1286
1287 if (!instance->frame_dma_pool)
1288 return;
1289
1290 /*
1291 * Return all frames to pool
1292 */
1293 for (i = 0; i < max_cmd; i++) {
1294
1295 cmd = instance->cmd_list[i];
1296
1297 if (cmd->frame)
1298 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1299 cmd->frame_phys_addr);
1300
1301 if (cmd->sense)
1302 pci_pool_free(instance->sense_dma_pool, cmd->frame,
1303 cmd->sense_phys_addr);
1304 }
1305
1306 /*
1307 * Now destroy the pool itself
1308 */
1309 pci_pool_destroy(instance->frame_dma_pool);
1310 pci_pool_destroy(instance->sense_dma_pool);
1311
1312 instance->frame_dma_pool = NULL;
1313 instance->sense_dma_pool = NULL;
1314}
1315
1316/**
1317 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1318 * @instance: Adapter soft state
1319 *
1320 * Each command packet has an embedded DMA memory buffer that is used for
1321 * filling MFI frame and the SG list that immediately follows the frame. This
1322 * function creates those DMA memory buffers for each command packet by using
1323 * PCI pool facility.
1324 */
1325static int megasas_create_frame_pool(struct megasas_instance *instance)
1326{
1327 int i;
1328 u32 max_cmd;
1329 u32 sge_sz;
1330 u32 sgl_sz;
1331 u32 total_sz;
1332 u32 frame_count;
1333 struct megasas_cmd *cmd;
1334
1335 max_cmd = instance->max_fw_cmds;
1336
1337 /*
1338 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1339 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1340 */
1341 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1342 sizeof(struct megasas_sge32);
1343
1344 /*
1345 * Calculated the number of 64byte frames required for SGL
1346 */
1347 sgl_sz = sge_sz * instance->max_num_sge;
1348 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1349
1350 /*
1351 * We need one extra frame for the MFI command
1352 */
1353 frame_count++;
1354
1355 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1356 /*
1357 * Use DMA pool facility provided by PCI layer
1358 */
1359 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1360 instance->pdev, total_sz, 64,
1361 0);
1362
1363 if (!instance->frame_dma_pool) {
1364 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1365 return -ENOMEM;
1366 }
1367
1368 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1369 instance->pdev, 128, 4, 0);
1370
1371 if (!instance->sense_dma_pool) {
1372 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1373
1374 pci_pool_destroy(instance->frame_dma_pool);
1375 instance->frame_dma_pool = NULL;
1376
1377 return -ENOMEM;
1378 }
1379
1380 /*
1381 * Allocate and attach a frame to each of the commands in cmd_list.
1382 * By making cmd->index as the context instead of the &cmd, we can
1383 * always use 32bit context regardless of the architecture
1384 */
1385 for (i = 0; i < max_cmd; i++) {
1386
1387 cmd = instance->cmd_list[i];
1388
1389 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1390 GFP_KERNEL, &cmd->frame_phys_addr);
1391
1392 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1393 GFP_KERNEL, &cmd->sense_phys_addr);
1394
1395 /*
1396 * megasas_teardown_frame_pool() takes care of freeing
1397 * whatever has been allocated
1398 */
1399 if (!cmd->frame || !cmd->sense) {
1400 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1401 megasas_teardown_frame_pool(instance);
1402 return -ENOMEM;
1403 }
1404
1405 cmd->frame->io.context = cmd->index;
1406 }
1407
1408 return 0;
1409}
1410
1411/**
1412 * megasas_free_cmds - Free all the cmds in the free cmd pool
1413 * @instance: Adapter soft state
1414 */
1415static void megasas_free_cmds(struct megasas_instance *instance)
1416{
1417 int i;
1418 /* First free the MFI frame pool */
1419 megasas_teardown_frame_pool(instance);
1420
1421 /* Free all the commands in the cmd_list */
1422 for (i = 0; i < instance->max_fw_cmds; i++)
1423 kfree(instance->cmd_list[i]);
1424
1425 /* Free the cmd_list buffer itself */
1426 kfree(instance->cmd_list);
1427 instance->cmd_list = NULL;
1428
1429 INIT_LIST_HEAD(&instance->cmd_pool);
1430}
1431
1432/**
1433 * megasas_alloc_cmds - Allocates the command packets
1434 * @instance: Adapter soft state
1435 *
1436 * Each command that is issued to the FW, whether IO commands from the OS or
1437 * internal commands like IOCTLs, are wrapped in local data structure called
1438 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1439 * the FW.
1440 *
1441 * Each frame has a 32-bit field called context (tag). This context is used
1442 * to get back the megasas_cmd from the frame when a frame gets completed in
1443 * the ISR. Typically the address of the megasas_cmd itself would be used as
1444 * the context. But we wanted to keep the differences between 32 and 64 bit
1445 * systems to the mininum. We always use 32 bit integers for the context. In
1446 * this driver, the 32 bit values are the indices into an array cmd_list.
1447 * This array is used only to look up the megasas_cmd given the context. The
1448 * free commands themselves are maintained in a linked list called cmd_pool.
1449 */
1450static int megasas_alloc_cmds(struct megasas_instance *instance)
1451{
1452 int i;
1453 int j;
1454 u32 max_cmd;
1455 struct megasas_cmd *cmd;
1456
1457 max_cmd = instance->max_fw_cmds;
1458
1459 /*
1460 * instance->cmd_list is an array of struct megasas_cmd pointers.
1461 * Allocate the dynamic array first and then allocate individual
1462 * commands.
1463 */
1464 instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1465 GFP_KERNEL);
1466
1467 if (!instance->cmd_list) {
1468 printk(KERN_DEBUG "megasas: out of memory\n");
1469 return -ENOMEM;
1470 }
1471
1472 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1473
1474 for (i = 0; i < max_cmd; i++) {
1475 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1476 GFP_KERNEL);
1477
1478 if (!instance->cmd_list[i]) {
1479
1480 for (j = 0; j < i; j++)
1481 kfree(instance->cmd_list[j]);
1482
1483 kfree(instance->cmd_list);
1484 instance->cmd_list = NULL;
1485
1486 return -ENOMEM;
1487 }
1488 }
1489
1490 /*
1491 * Add all the commands to command pool (instance->cmd_pool)
1492 */
1493 for (i = 0; i < max_cmd; i++) {
1494 cmd = instance->cmd_list[i];
1495 memset(cmd, 0, sizeof(struct megasas_cmd));
1496 cmd->index = i;
1497 cmd->instance = instance;
1498
1499 list_add_tail(&cmd->list, &instance->cmd_pool);
1500 }
1501
1502 /*
1503 * Create a frame pool and assign one frame to each cmd
1504 */
1505 if (megasas_create_frame_pool(instance)) {
1506 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1507 megasas_free_cmds(instance);
1508 }
1509
1510 return 0;
1511}
1512
1513/**
1514 * megasas_get_controller_info - Returns FW's controller structure
1515 * @instance: Adapter soft state
1516 * @ctrl_info: Controller information structure
1517 *
1518 * Issues an internal command (DCMD) to get the FW's controller structure.
1519 * This information is mainly used to find out the maximum IO transfer per
1520 * command supported by the FW.
1521 */
1522static int
1523megasas_get_ctrl_info(struct megasas_instance *instance,
1524 struct megasas_ctrl_info *ctrl_info)
1525{
1526 int ret = 0;
1527 struct megasas_cmd *cmd;
1528 struct megasas_dcmd_frame *dcmd;
1529 struct megasas_ctrl_info *ci;
1530 dma_addr_t ci_h = 0;
1531
1532 cmd = megasas_get_cmd(instance);
1533
1534 if (!cmd) {
1535 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1536 return -ENOMEM;
1537 }
1538
1539 dcmd = &cmd->frame->dcmd;
1540
1541 ci = pci_alloc_consistent(instance->pdev,
1542 sizeof(struct megasas_ctrl_info), &ci_h);
1543
1544 if (!ci) {
1545 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1546 megasas_return_cmd(instance, cmd);
1547 return -ENOMEM;
1548 }
1549
1550 memset(ci, 0, sizeof(*ci));
1551 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1552
1553 dcmd->cmd = MFI_CMD_DCMD;
1554 dcmd->cmd_status = 0xFF;
1555 dcmd->sge_count = 1;
1556 dcmd->flags = MFI_FRAME_DIR_READ;
1557 dcmd->timeout = 0;
1558 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1559 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1560 dcmd->sgl.sge32[0].phys_addr = ci_h;
1561 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1562
1563 if (!megasas_issue_polled(instance, cmd)) {
1564 ret = 0;
1565 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1566 } else {
1567 ret = -1;
1568 }
1569
1570 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1571 ci, ci_h);
1572
1573 megasas_return_cmd(instance, cmd);
1574 return ret;
1575}
1576
1577/**
1578 * megasas_init_mfi - Initializes the FW
1579 * @instance: Adapter soft state
1580 *
1581 * This is the main function for initializing MFI firmware.
1582 */
1583static int megasas_init_mfi(struct megasas_instance *instance)
1584{
1585 u32 context_sz;
1586 u32 reply_q_sz;
1587 u32 max_sectors_1;
1588 u32 max_sectors_2;
1589 struct megasas_register_set __iomem *reg_set;
1590
1591 struct megasas_cmd *cmd;
1592 struct megasas_ctrl_info *ctrl_info;
1593
1594 struct megasas_init_frame *init_frame;
1595 struct megasas_init_queue_info *initq_info;
1596 dma_addr_t init_frame_h;
1597 dma_addr_t initq_info_h;
1598
1599 /*
1600 * Map the message registers
1601 */
1602 instance->base_addr = pci_resource_start(instance->pdev, 0);
1603
1604 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1605 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1606 return -EBUSY;
1607 }
1608
1609 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1610
1611 if (!instance->reg_set) {
1612 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1613 goto fail_ioremap;
1614 }
1615
1616 reg_set = instance->reg_set;
1617
1618 /*
1619 * We expect the FW state to be READY
1620 */
1621 if (megasas_transition_to_ready(instance->reg_set))
1622 goto fail_ready_state;
1623
1624 /*
1625 * Get various operational parameters from status register
1626 */
1627 instance->max_fw_cmds = readl(&reg_set->outbound_msg_0) & 0x00FFFF;
1628 instance->max_num_sge = (readl(&reg_set->outbound_msg_0) & 0xFF0000) >>
1629 0x10;
1630 /*
1631 * Create a pool of commands
1632 */
1633 if (megasas_alloc_cmds(instance))
1634 goto fail_alloc_cmds;
1635
1636 /*
1637 * Allocate memory for reply queue. Length of reply queue should
1638 * be _one_ more than the maximum commands handled by the firmware.
1639 *
1640 * Note: When FW completes commands, it places corresponding contex
1641 * values in this circular reply queue. This circular queue is a fairly
1642 * typical producer-consumer queue. FW is the producer (of completed
1643 * commands) and the driver is the consumer.
1644 */
1645 context_sz = sizeof(u32);
1646 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1647
1648 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1649 reply_q_sz,
1650 &instance->reply_queue_h);
1651
1652 if (!instance->reply_queue) {
1653 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1654 goto fail_reply_queue;
1655 }
1656
1657 /*
1658 * Prepare a init frame. Note the init frame points to queue info
1659 * structure. Each frame has SGL allocated after first 64 bytes. For
1660 * this frame - since we don't need any SGL - we use SGL's space as
1661 * queue info structure
1662 *
1663 * We will not get a NULL command below. We just created the pool.
1664 */
1665 cmd = megasas_get_cmd(instance);
1666
1667 init_frame = (struct megasas_init_frame *)cmd->frame;
1668 initq_info = (struct megasas_init_queue_info *)
1669 ((unsigned long)init_frame + 64);
1670
1671 init_frame_h = cmd->frame_phys_addr;
1672 initq_info_h = init_frame_h + 64;
1673
1674 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1675 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1676
1677 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1678 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1679
1680 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1681 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1682
1683 init_frame->cmd = MFI_CMD_INIT;
1684 init_frame->cmd_status = 0xFF;
1685 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1686
1687 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1688
1689 /*
1690 * Issue the init frame in polled mode
1691 */
1692 if (megasas_issue_polled(instance, cmd)) {
1693 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1694 goto fail_fw_init;
1695 }
1696
1697 megasas_return_cmd(instance, cmd);
1698
1699 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1700
1701 /*
1702 * Compute the max allowed sectors per IO: The controller info has two
1703 * limits on max sectors. Driver should use the minimum of these two.
1704 *
1705 * 1 << stripe_sz_ops.min = max sectors per strip
1706 *
1707 * Note that older firmwares ( < FW ver 30) didn't report information
1708 * to calculate max_sectors_1. So the number ended up as zero always.
1709 */
1710 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1711
1712 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1713 ctrl_info->max_strips_per_io;
1714 max_sectors_2 = ctrl_info->max_request_size;
1715
1716 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1717 ? max_sectors_1 : max_sectors_2;
1718 } else
1719 instance->max_sectors_per_req = instance->max_num_sge *
1720 PAGE_SIZE / 512;
1721
1722 kfree(ctrl_info);
1723
1724 return 0;
1725
1726 fail_fw_init:
1727 megasas_return_cmd(instance, cmd);
1728
1729 pci_free_consistent(instance->pdev, reply_q_sz,
1730 instance->reply_queue, instance->reply_queue_h);
1731 fail_reply_queue:
1732 megasas_free_cmds(instance);
1733
1734 fail_alloc_cmds:
1735 fail_ready_state:
1736 iounmap(instance->reg_set);
1737
1738 fail_ioremap:
1739 pci_release_regions(instance->pdev);
1740
1741 return -EINVAL;
1742}
1743
1744/**
1745 * megasas_release_mfi - Reverses the FW initialization
1746 * @intance: Adapter soft state
1747 */
1748static void megasas_release_mfi(struct megasas_instance *instance)
1749{
1750 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
1751
1752 pci_free_consistent(instance->pdev, reply_q_sz,
1753 instance->reply_queue, instance->reply_queue_h);
1754
1755 megasas_free_cmds(instance);
1756
1757 iounmap(instance->reg_set);
1758
1759 pci_release_regions(instance->pdev);
1760}
1761
1762/**
1763 * megasas_get_seq_num - Gets latest event sequence numbers
1764 * @instance: Adapter soft state
1765 * @eli: FW event log sequence numbers information
1766 *
1767 * FW maintains a log of all events in a non-volatile area. Upper layers would
1768 * usually find out the latest sequence number of the events, the seq number at
1769 * the boot etc. They would "read" all the events below the latest seq number
1770 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1771 * number), they would subsribe to AEN (asynchronous event notification) and
1772 * wait for the events to happen.
1773 */
1774static int
1775megasas_get_seq_num(struct megasas_instance *instance,
1776 struct megasas_evt_log_info *eli)
1777{
1778 struct megasas_cmd *cmd;
1779 struct megasas_dcmd_frame *dcmd;
1780 struct megasas_evt_log_info *el_info;
1781 dma_addr_t el_info_h = 0;
1782
1783 cmd = megasas_get_cmd(instance);
1784
1785 if (!cmd) {
1786 return -ENOMEM;
1787 }
1788
1789 dcmd = &cmd->frame->dcmd;
1790 el_info = pci_alloc_consistent(instance->pdev,
1791 sizeof(struct megasas_evt_log_info),
1792 &el_info_h);
1793
1794 if (!el_info) {
1795 megasas_return_cmd(instance, cmd);
1796 return -ENOMEM;
1797 }
1798
1799 memset(el_info, 0, sizeof(*el_info));
1800 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1801
1802 dcmd->cmd = MFI_CMD_DCMD;
1803 dcmd->cmd_status = 0x0;
1804 dcmd->sge_count = 1;
1805 dcmd->flags = MFI_FRAME_DIR_READ;
1806 dcmd->timeout = 0;
1807 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
1808 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
1809 dcmd->sgl.sge32[0].phys_addr = el_info_h;
1810 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
1811
1812 megasas_issue_blocked_cmd(instance, cmd);
1813
1814 /*
1815 * Copy the data back into callers buffer
1816 */
1817 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
1818
1819 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
1820 el_info, el_info_h);
1821
1822 megasas_return_cmd(instance, cmd);
1823
1824 return 0;
1825}
1826
1827/**
1828 * megasas_register_aen - Registers for asynchronous event notification
1829 * @instance: Adapter soft state
1830 * @seq_num: The starting sequence number
1831 * @class_locale: Class of the event
1832 *
1833 * This function subscribes for AEN for events beyond the @seq_num. It requests
1834 * to be notified if and only if the event is of type @class_locale
1835 */
1836static int
1837megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
1838 u32 class_locale_word)
1839{
1840 int ret_val;
1841 struct megasas_cmd *cmd;
1842 struct megasas_dcmd_frame *dcmd;
1843 union megasas_evt_class_locale curr_aen;
1844 union megasas_evt_class_locale prev_aen;
1845
1846 /*
1847 * If there an AEN pending already (aen_cmd), check if the
1848 * class_locale of that pending AEN is inclusive of the new
1849 * AEN request we currently have. If it is, then we don't have
1850 * to do anything. In other words, whichever events the current
1851 * AEN request is subscribing to, have already been subscribed
1852 * to.
1853 *
1854 * If the old_cmd is _not_ inclusive, then we have to abort
1855 * that command, form a class_locale that is superset of both
1856 * old and current and re-issue to the FW
1857 */
1858
1859 curr_aen.word = class_locale_word;
1860
1861 if (instance->aen_cmd) {
1862
1863 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
1864
1865 /*
1866 * A class whose enum value is smaller is inclusive of all
1867 * higher values. If a PROGRESS (= -1) was previously
1868 * registered, then a new registration requests for higher
1869 * classes need not be sent to FW. They are automatically
1870 * included.
1871 *
1872 * Locale numbers don't have such hierarchy. They are bitmap
1873 * values
1874 */
1875 if ((prev_aen.members.class <= curr_aen.members.class) &&
1876 !((prev_aen.members.locale & curr_aen.members.locale) ^
1877 curr_aen.members.locale)) {
1878 /*
1879 * Previously issued event registration includes
1880 * current request. Nothing to do.
1881 */
1882 return 0;
1883 } else {
1884 curr_aen.members.locale |= prev_aen.members.locale;
1885
1886 if (prev_aen.members.class < curr_aen.members.class)
1887 curr_aen.members.class = prev_aen.members.class;
1888
1889 instance->aen_cmd->abort_aen = 1;
1890 ret_val = megasas_issue_blocked_abort_cmd(instance,
1891 instance->
1892 aen_cmd);
1893
1894 if (ret_val) {
1895 printk(KERN_DEBUG "megasas: Failed to abort "
1896 "previous AEN command\n");
1897 return ret_val;
1898 }
1899 }
1900 }
1901
1902 cmd = megasas_get_cmd(instance);
1903
1904 if (!cmd)
1905 return -ENOMEM;
1906
1907 dcmd = &cmd->frame->dcmd;
1908
1909 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
1910
1911 /*
1912 * Prepare DCMD for aen registration
1913 */
1914 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1915
1916 dcmd->cmd = MFI_CMD_DCMD;
1917 dcmd->cmd_status = 0x0;
1918 dcmd->sge_count = 1;
1919 dcmd->flags = MFI_FRAME_DIR_READ;
1920 dcmd->timeout = 0;
1921 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
1922 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
1923 dcmd->mbox.w[0] = seq_num;
1924 dcmd->mbox.w[1] = curr_aen.word;
1925 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
1926 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
1927
1928 /*
1929 * Store reference to the cmd used to register for AEN. When an
1930 * application wants us to register for AEN, we have to abort this
1931 * cmd and re-register with a new EVENT LOCALE supplied by that app
1932 */
1933 instance->aen_cmd = cmd;
1934
1935 /*
1936 * Issue the aen registration frame
1937 */
1938 writel(cmd->frame_phys_addr >> 3,
1939 &instance->reg_set->inbound_queue_port);
1940
1941 return 0;
1942}
1943
1944/**
1945 * megasas_start_aen - Subscribes to AEN during driver load time
1946 * @instance: Adapter soft state
1947 */
1948static int megasas_start_aen(struct megasas_instance *instance)
1949{
1950 struct megasas_evt_log_info eli;
1951 union megasas_evt_class_locale class_locale;
1952
1953 /*
1954 * Get the latest sequence number from FW
1955 */
1956 memset(&eli, 0, sizeof(eli));
1957
1958 if (megasas_get_seq_num(instance, &eli))
1959 return -1;
1960
1961 /*
1962 * Register AEN with FW for latest sequence number plus 1
1963 */
1964 class_locale.members.reserved = 0;
1965 class_locale.members.locale = MR_EVT_LOCALE_ALL;
1966 class_locale.members.class = MR_EVT_CLASS_DEBUG;
1967
1968 return megasas_register_aen(instance, eli.newest_seq_num + 1,
1969 class_locale.word);
1970}
1971
1972/**
1973 * megasas_io_attach - Attaches this driver to SCSI mid-layer
1974 * @instance: Adapter soft state
1975 */
1976static int megasas_io_attach(struct megasas_instance *instance)
1977{
1978 struct Scsi_Host *host = instance->host;
1979
1980 /*
1981 * Export parameters required by SCSI mid-layer
1982 */
1983 host->irq = instance->pdev->irq;
1984 host->unique_id = instance->unique_id;
1985 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
1986 host->this_id = instance->init_id;
1987 host->sg_tablesize = instance->max_num_sge;
1988 host->max_sectors = instance->max_sectors_per_req;
1989 host->cmd_per_lun = 128;
1990 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
1991 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
1992 host->max_lun = MEGASAS_MAX_LUN;
1993
1994 /*
1995 * Notify the mid-layer about the new controller
1996 */
1997 if (scsi_add_host(host, &instance->pdev->dev)) {
1998 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
1999 return -ENODEV;
2000 }
2001
2002 /*
2003 * Trigger SCSI to scan our drives
2004 */
2005 scsi_scan_host(host);
2006 return 0;
2007}
2008
2009/**
2010 * megasas_probe_one - PCI hotplug entry point
2011 * @pdev: PCI device structure
2012 * @id: PCI ids of supported hotplugged adapter
2013 */
2014static int __devinit
2015megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2016{
2017 int rval;
2018 struct Scsi_Host *host;
2019 struct megasas_instance *instance;
2020
2021 /*
2022 * Announce PCI information
2023 */
2024 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2025 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2026 pdev->subsystem_device);
2027
2028 printk("bus %d:slot %d:func %d\n",
2029 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2030
2031 /*
2032 * PCI prepping: enable device set bus mastering and dma mask
2033 */
2034 rval = pci_enable_device(pdev);
2035
2036 if (rval) {
2037 return rval;
2038 }
2039
2040 pci_set_master(pdev);
2041
2042 /*
2043 * All our contollers are capable of performing 64-bit DMA
2044 */
2045 if (IS_DMA64) {
2046 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2047
2048 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2049 goto fail_set_dma_mask;
2050 }
2051 } else {
2052 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2053 goto fail_set_dma_mask;
2054 }
2055
2056 host = scsi_host_alloc(&megasas_template,
2057 sizeof(struct megasas_instance));
2058
2059 if (!host) {
2060 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2061 goto fail_alloc_instance;
2062 }
2063
2064 instance = (struct megasas_instance *)host->hostdata;
2065 memset(instance, 0, sizeof(*instance));
2066
2067 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2068 &instance->producer_h);
2069 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2070 &instance->consumer_h);
2071
2072 if (!instance->producer || !instance->consumer) {
2073 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2074 "producer, consumer\n");
2075 goto fail_alloc_dma_buf;
2076 }
2077
2078 *instance->producer = 0;
2079 *instance->consumer = 0;
2080
2081 instance->evt_detail = pci_alloc_consistent(pdev,
2082 sizeof(struct
2083 megasas_evt_detail),
2084 &instance->evt_detail_h);
2085
2086 if (!instance->evt_detail) {
2087 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2088 "event detail structure\n");
2089 goto fail_alloc_dma_buf;
2090 }
2091
2092 /*
2093 * Initialize locks and queues
2094 */
2095 INIT_LIST_HEAD(&instance->cmd_pool);
2096
2097 init_waitqueue_head(&instance->int_cmd_wait_q);
2098 init_waitqueue_head(&instance->abort_cmd_wait_q);
2099
2100 spin_lock_init(&instance->cmd_pool_lock);
2101 spin_lock_init(&instance->instance_lock);
2102
2103 sema_init(&instance->aen_mutex, 1);
2104 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2105
2106 /*
2107 * Initialize PCI related and misc parameters
2108 */
2109 instance->pdev = pdev;
2110 instance->host = host;
2111 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2112 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2113
2114 /*
2115 * Initialize MFI Firmware
2116 */
2117 if (megasas_init_mfi(instance))
2118 goto fail_init_mfi;
2119
2120 /*
2121 * Register IRQ
2122 */
2123 if (request_irq(pdev->irq, megasas_isr, SA_SHIRQ, "megasas", instance)) {
2124 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2125 goto fail_irq;
2126 }
2127
2128 megasas_enable_intr(instance->reg_set);
2129
2130 /*
2131 * Store instance in PCI softstate
2132 */
2133 pci_set_drvdata(pdev, instance);
2134
2135 /*
2136 * Add this controller to megasas_mgmt_info structure so that it
2137 * can be exported to management applications
2138 */
2139 megasas_mgmt_info.count++;
2140 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2141 megasas_mgmt_info.max_index++;
2142
2143 /*
2144 * Initiate AEN (Asynchronous Event Notification)
2145 */
2146 if (megasas_start_aen(instance)) {
2147 printk(KERN_DEBUG "megasas: start aen failed\n");
2148 goto fail_start_aen;
2149 }
2150
2151 /*
2152 * Register with SCSI mid-layer
2153 */
2154 if (megasas_io_attach(instance))
2155 goto fail_io_attach;
2156
2157 return 0;
2158
2159 fail_start_aen:
2160 fail_io_attach:
2161 megasas_mgmt_info.count--;
2162 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2163 megasas_mgmt_info.max_index--;
2164
2165 pci_set_drvdata(pdev, NULL);
2166 megasas_disable_intr(instance->reg_set);
2167 free_irq(instance->pdev->irq, instance);
2168
2169 megasas_release_mfi(instance);
2170
2171 fail_irq:
2172 fail_init_mfi:
2173 fail_alloc_dma_buf:
2174 if (instance->evt_detail)
2175 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2176 instance->evt_detail,
2177 instance->evt_detail_h);
2178
2179 if (instance->producer)
2180 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2181 instance->producer_h);
2182 if (instance->consumer)
2183 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2184 instance->consumer_h);
2185 scsi_host_put(host);
2186
2187 fail_alloc_instance:
2188 fail_set_dma_mask:
2189 pci_disable_device(pdev);
2190
2191 return -ENODEV;
2192}
2193
2194/**
2195 * megasas_flush_cache - Requests FW to flush all its caches
2196 * @instance: Adapter soft state
2197 */
2198static void megasas_flush_cache(struct megasas_instance *instance)
2199{
2200 struct megasas_cmd *cmd;
2201 struct megasas_dcmd_frame *dcmd;
2202
2203 cmd = megasas_get_cmd(instance);
2204
2205 if (!cmd)
2206 return;
2207
2208 dcmd = &cmd->frame->dcmd;
2209
2210 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2211
2212 dcmd->cmd = MFI_CMD_DCMD;
2213 dcmd->cmd_status = 0x0;
2214 dcmd->sge_count = 0;
2215 dcmd->flags = MFI_FRAME_DIR_NONE;
2216 dcmd->timeout = 0;
2217 dcmd->data_xfer_len = 0;
2218 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2219 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2220
2221 megasas_issue_blocked_cmd(instance, cmd);
2222
2223 megasas_return_cmd(instance, cmd);
2224
2225 return;
2226}
2227
2228/**
2229 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2230 * @instance: Adapter soft state
2231 */
2232static void megasas_shutdown_controller(struct megasas_instance *instance)
2233{
2234 struct megasas_cmd *cmd;
2235 struct megasas_dcmd_frame *dcmd;
2236
2237 cmd = megasas_get_cmd(instance);
2238
2239 if (!cmd)
2240 return;
2241
2242 if (instance->aen_cmd)
2243 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2244
2245 dcmd = &cmd->frame->dcmd;
2246
2247 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2248
2249 dcmd->cmd = MFI_CMD_DCMD;
2250 dcmd->cmd_status = 0x0;
2251 dcmd->sge_count = 0;
2252 dcmd->flags = MFI_FRAME_DIR_NONE;
2253 dcmd->timeout = 0;
2254 dcmd->data_xfer_len = 0;
2255 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2256
2257 megasas_issue_blocked_cmd(instance, cmd);
2258
2259 megasas_return_cmd(instance, cmd);
2260
2261 return;
2262}
2263
2264/**
2265 * megasas_detach_one - PCI hot"un"plug entry point
2266 * @pdev: PCI device structure
2267 */
2268static void megasas_detach_one(struct pci_dev *pdev)
2269{
2270 int i;
2271 struct Scsi_Host *host;
2272 struct megasas_instance *instance;
2273
2274 instance = pci_get_drvdata(pdev);
2275 host = instance->host;
2276
2277 scsi_remove_host(instance->host);
2278 megasas_flush_cache(instance);
2279 megasas_shutdown_controller(instance);
2280
2281 /*
2282 * Take the instance off the instance array. Note that we will not
2283 * decrement the max_index. We let this array be sparse array
2284 */
2285 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2286 if (megasas_mgmt_info.instance[i] == instance) {
2287 megasas_mgmt_info.count--;
2288 megasas_mgmt_info.instance[i] = NULL;
2289
2290 break;
2291 }
2292 }
2293
2294 pci_set_drvdata(instance->pdev, NULL);
2295
2296 megasas_disable_intr(instance->reg_set);
2297
2298 free_irq(instance->pdev->irq, instance);
2299
2300 megasas_release_mfi(instance);
2301
2302 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2303 instance->evt_detail, instance->evt_detail_h);
2304
2305 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2306 instance->producer_h);
2307
2308 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2309 instance->consumer_h);
2310
2311 scsi_host_put(host);
2312
2313 pci_set_drvdata(pdev, NULL);
2314
2315 pci_disable_device(pdev);
2316
2317 return;
2318}
2319
2320/**
2321 * megasas_shutdown - Shutdown entry point
2322 * @device: Generic device structure
2323 */
2324static void megasas_shutdown(struct pci_dev *pdev)
2325{
2326 struct megasas_instance *instance = pci_get_drvdata(pdev);
2327 megasas_flush_cache(instance);
2328}
2329
2330/**
2331 * megasas_mgmt_open - char node "open" entry point
2332 */
2333static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2334{
2335 /*
2336 * Allow only those users with admin rights
2337 */
2338 if (!capable(CAP_SYS_ADMIN))
2339 return -EACCES;
2340
2341 return 0;
2342}
2343
2344/**
2345 * megasas_mgmt_release - char node "release" entry point
2346 */
2347static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2348{
2349 filep->private_data = NULL;
2350 fasync_helper(-1, filep, 0, &megasas_async_queue);
2351
2352 return 0;
2353}
2354
2355/**
2356 * megasas_mgmt_fasync - Async notifier registration from applications
2357 *
2358 * This function adds the calling process to a driver global queue. When an
2359 * event occurs, SIGIO will be sent to all processes in this queue.
2360 */
2361static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2362{
2363 int rc;
2364
2365 down(&megasas_async_queue_mutex);
2366
2367 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2368
2369 up(&megasas_async_queue_mutex);
2370
2371 if (rc >= 0) {
2372 /* For sanity check when we get ioctl */
2373 filep->private_data = filep;
2374 return 0;
2375 }
2376
2377 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2378
2379 return rc;
2380}
2381
2382/**
2383 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2384 * @instance: Adapter soft state
2385 * @argp: User's ioctl packet
2386 */
2387static int
2388megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2389 struct megasas_iocpacket __user * user_ioc,
2390 struct megasas_iocpacket *ioc)
2391{
2392 struct megasas_sge32 *kern_sge32;
2393 struct megasas_cmd *cmd;
2394 void *kbuff_arr[MAX_IOCTL_SGE];
2395 dma_addr_t buf_handle = 0;
2396 int error = 0, i;
2397 void *sense = NULL;
2398 dma_addr_t sense_handle;
2399 u32 *sense_ptr;
2400
2401 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2402
2403 if (ioc->sge_count > MAX_IOCTL_SGE) {
2404 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2405 ioc->sge_count, MAX_IOCTL_SGE);
2406 return -EINVAL;
2407 }
2408
2409 cmd = megasas_get_cmd(instance);
2410 if (!cmd) {
2411 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2412 return -ENOMEM;
2413 }
2414
2415 /*
2416 * User's IOCTL packet has 2 frames (maximum). Copy those two
2417 * frames into our cmd's frames. cmd->frame's context will get
2418 * overwritten when we copy from user's frames. So set that value
2419 * alone separately
2420 */
2421 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2422 cmd->frame->hdr.context = cmd->index;
2423
2424 /*
2425 * The management interface between applications and the fw uses
2426 * MFI frames. E.g, RAID configuration changes, LD property changes
2427 * etc are accomplishes through different kinds of MFI frames. The
2428 * driver needs to care only about substituting user buffers with
2429 * kernel buffers in SGLs. The location of SGL is embedded in the
2430 * struct iocpacket itself.
2431 */
2432 kern_sge32 = (struct megasas_sge32 *)
2433 ((unsigned long)cmd->frame + ioc->sgl_off);
2434
2435 /*
2436 * For each user buffer, create a mirror buffer and copy in
2437 */
2438 for (i = 0; i < ioc->sge_count; i++) {
2439 kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
2440 ioc->sgl[i].iov_len,
2441 &buf_handle);
2442 if (!kbuff_arr[i]) {
2443 printk(KERN_DEBUG "megasas: Failed to alloc "
2444 "kernel SGL buffer for IOCTL \n");
2445 error = -ENOMEM;
2446 goto out;
2447 }
2448
2449 /*
2450 * We don't change the dma_coherent_mask, so
2451 * pci_alloc_consistent only returns 32bit addresses
2452 */
2453 kern_sge32[i].phys_addr = (u32) buf_handle;
2454 kern_sge32[i].length = ioc->sgl[i].iov_len;
2455
2456 /*
2457 * We created a kernel buffer corresponding to the
2458 * user buffer. Now copy in from the user buffer
2459 */
2460 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2461 (u32) (ioc->sgl[i].iov_len))) {
2462 error = -EFAULT;
2463 goto out;
2464 }
2465 }
2466
2467 if (ioc->sense_len) {
2468 sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
2469 &sense_handle);
2470 if (!sense) {
2471 error = -ENOMEM;
2472 goto out;
2473 }
2474
2475 sense_ptr =
2476 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2477 *sense_ptr = sense_handle;
2478 }
2479
2480 /*
2481 * Set the sync_cmd flag so that the ISR knows not to complete this
2482 * cmd to the SCSI mid-layer
2483 */
2484 cmd->sync_cmd = 1;
2485 megasas_issue_blocked_cmd(instance, cmd);
2486 cmd->sync_cmd = 0;
2487
2488 /*
2489 * copy out the kernel buffers to user buffers
2490 */
2491 for (i = 0; i < ioc->sge_count; i++) {
2492 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2493 ioc->sgl[i].iov_len)) {
2494 error = -EFAULT;
2495 goto out;
2496 }
2497 }
2498
2499 /*
2500 * copy out the sense
2501 */
2502 if (ioc->sense_len) {
2503 /*
2504 * sense_ptr points to the location that has the user
2505 * sense buffer address
2506 */
2507 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2508 ioc->sense_off);
2509
2510 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2511 sense, ioc->sense_len)) {
2512 error = -EFAULT;
2513 goto out;
2514 }
2515 }
2516
2517 /*
2518 * copy the status codes returned by the fw
2519 */
2520 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2521 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2522 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2523 error = -EFAULT;
2524 }
2525
2526 out:
2527 if (sense) {
2528 pci_free_consistent(instance->pdev, ioc->sense_len,
2529 sense, sense_handle);
2530 }
2531
2532 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2533 pci_free_consistent(instance->pdev,
2534 kern_sge32[i].length,
2535 kbuff_arr[i], kern_sge32[i].phys_addr);
2536 }
2537
2538 megasas_return_cmd(instance, cmd);
2539 return error;
2540}
2541
2542static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2543{
2544 int i;
2545
2546 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2547
2548 if ((megasas_mgmt_info.instance[i]) &&
2549 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2550 return megasas_mgmt_info.instance[i];
2551 }
2552
2553 return NULL;
2554}
2555
2556static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2557{
2558 struct megasas_iocpacket __user *user_ioc =
2559 (struct megasas_iocpacket __user *)arg;
2560 struct megasas_iocpacket *ioc;
2561 struct megasas_instance *instance;
2562 int error;
2563
2564 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2565 if (!ioc)
2566 return -ENOMEM;
2567
2568 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2569 error = -EFAULT;
2570 goto out_kfree_ioc;
2571 }
2572
2573 instance = megasas_lookup_instance(ioc->host_no);
2574 if (!instance) {
2575 error = -ENODEV;
2576 goto out_kfree_ioc;
2577 }
2578
2579 /*
2580 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2581 */
2582 if (down_interruptible(&instance->ioctl_sem)) {
2583 error = -ERESTARTSYS;
2584 goto out_kfree_ioc;
2585 }
2586 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2587 up(&instance->ioctl_sem);
2588
2589 out_kfree_ioc:
2590 kfree(ioc);
2591 return error;
2592}
2593
2594static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2595{
2596 struct megasas_instance *instance;
2597 struct megasas_aen aen;
2598 int error;
2599
2600 if (file->private_data != file) {
2601 printk(KERN_DEBUG "megasas: fasync_helper was not "
2602 "called first\n");
2603 return -EINVAL;
2604 }
2605
2606 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2607 return -EFAULT;
2608
2609 instance = megasas_lookup_instance(aen.host_no);
2610
2611 if (!instance)
2612 return -ENODEV;
2613
2614 down(&instance->aen_mutex);
2615 error = megasas_register_aen(instance, aen.seq_num,
2616 aen.class_locale_word);
2617 up(&instance->aen_mutex);
2618 return error;
2619}
2620
2621/**
2622 * megasas_mgmt_ioctl - char node ioctl entry point
2623 */
2624static long
2625megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2626{
2627 switch (cmd) {
2628 case MEGASAS_IOC_FIRMWARE:
2629 return megasas_mgmt_ioctl_fw(file, arg);
2630
2631 case MEGASAS_IOC_GET_AEN:
2632 return megasas_mgmt_ioctl_aen(file, arg);
2633 }
2634
2635 return -ENOTTY;
2636}
2637
2638#ifdef CONFIG_COMPAT
2639static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2640{
2641 struct compat_megasas_iocpacket __user *cioc =
2642 (struct compat_megasas_iocpacket __user *)arg;
2643 struct megasas_iocpacket __user *ioc =
2644 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2645 int i;
2646 int error = 0;
2647
2648 clear_user(ioc, sizeof(*ioc));
2649
2650 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2651 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2652 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2653 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2654 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2655 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2656 return -EFAULT;
2657
2658 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2659 compat_uptr_t ptr;
2660
2661 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2662 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2663 copy_in_user(&ioc->sgl[i].iov_len,
2664 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2665 return -EFAULT;
2666 }
2667
2668 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2669
2670 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2671 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2672 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2673 return -EFAULT;
2674 }
2675 return error;
2676}
2677
2678static long
2679megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2680 unsigned long arg)
2681{
2682 switch (cmd) {
2683 case MEGASAS_IOC_FIRMWARE:{
2684 return megasas_mgmt_compat_ioctl_fw(file, arg);
2685 }
2686 case MEGASAS_IOC_GET_AEN:
2687 return megasas_mgmt_ioctl_aen(file, arg);
2688 }
2689
2690 return -ENOTTY;
2691}
2692#endif
2693
2694/*
2695 * File operations structure for management interface
2696 */
2697static struct file_operations megasas_mgmt_fops = {
2698 .owner = THIS_MODULE,
2699 .open = megasas_mgmt_open,
2700 .release = megasas_mgmt_release,
2701 .fasync = megasas_mgmt_fasync,
2702 .unlocked_ioctl = megasas_mgmt_ioctl,
2703#ifdef CONFIG_COMPAT
2704 .compat_ioctl = megasas_mgmt_compat_ioctl,
2705#endif
2706};
2707
2708/*
2709 * PCI hotplug support registration structure
2710 */
2711static struct pci_driver megasas_pci_driver = {
2712
2713 .name = "megaraid_sas",
2714 .id_table = megasas_pci_table,
2715 .probe = megasas_probe_one,
2716 .remove = __devexit_p(megasas_detach_one),
2717 .shutdown = megasas_shutdown,
2718};
2719
2720/*
2721 * Sysfs driver attributes
2722 */
2723static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2724{
2725 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2726 MEGASAS_VERSION);
2727}
2728
2729static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2730
2731static ssize_t
2732megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2733{
2734 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2735 MEGASAS_RELDATE);
2736}
2737
2738static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
2739 NULL);
2740
2741/**
2742 * megasas_init - Driver load entry point
2743 */
2744static int __init megasas_init(void)
2745{
2746 int rval;
2747
2748 /*
2749 * Announce driver version and other information
2750 */
2751 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
2752 MEGASAS_EXT_VERSION);
2753
2754 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
2755
2756 /*
2757 * Register character device node
2758 */
2759 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
2760
2761 if (rval < 0) {
2762 printk(KERN_DEBUG "megasas: failed to open device node\n");
2763 return rval;
2764 }
2765
2766 megasas_mgmt_majorno = rval;
2767
2768 /*
2769 * Register ourselves as PCI hotplug module
2770 */
2771 rval = pci_module_init(&megasas_pci_driver);
2772
2773 if (rval) {
2774 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
2775 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2776 }
2777
2778 driver_create_file(&megasas_pci_driver.driver, &driver_attr_version);
2779 driver_create_file(&megasas_pci_driver.driver,
2780 &driver_attr_release_date);
2781
2782 return rval;
2783}
2784
2785/**
2786 * megasas_exit - Driver unload entry point
2787 */
2788static void __exit megasas_exit(void)
2789{
2790 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
2791 driver_remove_file(&megasas_pci_driver.driver,
2792 &driver_attr_release_date);
2793
2794 pci_unregister_driver(&megasas_pci_driver);
2795 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2796}
2797
2798module_init(megasas_init);
2799module_exit(megasas_exit);
This page took 0.15062 seconds and 5 git commands to generate.