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