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