1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Linux MegaRAID device driver
6 * Copyright (c) 2003-2004 LSI Logic Corporation.
9 * Version : v2.20.2.7 (Jul 16 2006)
11 * Common management module
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/mutex.h>
16 #include "megaraid_mm.h"
19 // Entry points for char node driver
20 static DEFINE_MUTEX(mraid_mm_mutex
);
21 static int mraid_mm_open(struct inode
*, struct file
*);
22 static long mraid_mm_unlocked_ioctl(struct file
*, uint
, unsigned long);
25 // routines to convert to and from the old the format
26 static int mimd_to_kioc(mimd_t __user
*, mraid_mmadp_t
*, uioc_t
*);
27 static int kioc_to_mimd(uioc_t
*, mimd_t __user
*);
31 static int handle_drvrcmd(void __user
*, uint8_t, int *);
32 static int lld_ioctl(mraid_mmadp_t
*, uioc_t
*);
33 static void ioctl_done(uioc_t
*);
34 static void lld_timedout(struct timer_list
*);
35 static void hinfo_to_cinfo(mraid_hba_info_t
*, mcontroller_t
*);
36 static mraid_mmadp_t
*mraid_mm_get_adapter(mimd_t __user
*, int *);
37 static uioc_t
*mraid_mm_alloc_kioc(mraid_mmadp_t
*);
38 static void mraid_mm_dealloc_kioc(mraid_mmadp_t
*, uioc_t
*);
39 static int mraid_mm_attach_buf(mraid_mmadp_t
*, uioc_t
*, int);
40 static int mraid_mm_setup_dma_pools(mraid_mmadp_t
*);
41 static void mraid_mm_free_adp_resources(mraid_mmadp_t
*);
42 static void mraid_mm_teardown_dma_pools(mraid_mmadp_t
*);
44 MODULE_AUTHOR("LSI Logic Corporation");
45 MODULE_DESCRIPTION("LSI Logic Management Module");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(LSI_COMMON_MOD_VERSION
);
49 static int dbglevel
= CL_ANN
;
50 module_param_named(dlevel
, dbglevel
, int, 0);
51 MODULE_PARM_DESC(dlevel
, "Debug level (default=0)");
53 EXPORT_SYMBOL(mraid_mm_register_adp
);
54 EXPORT_SYMBOL(mraid_mm_unregister_adp
);
55 EXPORT_SYMBOL(mraid_mm_adapter_app_handle
);
57 static uint32_t drvr_ver
= 0x02200207;
59 static int adapters_count_g
;
60 static struct list_head adapters_list_g
;
62 static wait_queue_head_t wait_q
;
64 static const struct file_operations lsi_fops
= {
65 .open
= mraid_mm_open
,
66 .unlocked_ioctl
= mraid_mm_unlocked_ioctl
,
67 .compat_ioctl
= compat_ptr_ioctl
,
69 .llseek
= noop_llseek
,
72 static struct miscdevice megaraid_mm_dev
= {
73 .minor
= MISC_DYNAMIC_MINOR
,
79 * mraid_mm_open - open routine for char node interface
83 * Allow ioctl operations by apps only if they have superuser privilege.
86 mraid_mm_open(struct inode
*inode
, struct file
*filep
)
89 * Only allow superuser to access private ioctl interface
91 if (!capable(CAP_SYS_ADMIN
)) return (-EACCES
);
97 * mraid_mm_ioctl - module entry-point for ioctls
98 * @inode : inode (ignored)
99 * @filep : file operations pointer (ignored)
100 * @cmd : ioctl command
101 * @arg : user ioctl packet
104 mraid_mm_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
107 char signature
[EXT_IOCTL_SIGN_SZ
] = {0};
112 void __user
*argp
= (void __user
*)arg
;
115 * Make sure only USCSICMD are issued through this interface.
116 * MIMD application would still fire different command.
119 if ((_IOC_TYPE(cmd
) != MEGAIOC_MAGIC
) && (cmd
!= USCSICMD
)) {
124 * Look for signature to see if this is the new or old ioctl format.
126 if (copy_from_user(signature
, argp
, EXT_IOCTL_SIGN_SZ
)) {
127 con_log(CL_ANN
, (KERN_WARNING
128 "megaraid cmm: copy from usr addr failed\n"));
132 if (memcmp(signature
, EXT_IOCTL_SIGN
, EXT_IOCTL_SIGN_SZ
) == 0)
138 * At present, we don't support the new ioctl packet
144 * If it is a driver ioctl (as opposed to fw ioctls), then we can
145 * handle the command locally. rval > 0 means it is not a drvr cmd
147 rval
= handle_drvrcmd(argp
, old_ioctl
, &drvrcmd_rval
);
155 if ((adp
= mraid_mm_get_adapter(argp
, &rval
)) == NULL
) {
160 * Check if adapter can accept ioctl. We may have marked it offline
161 * if any previous kioc had timedout on this controller.
163 if (!adp
->quiescent
) {
164 con_log(CL_ANN
, (KERN_WARNING
165 "megaraid cmm: controller cannot accept cmds due to "
166 "earlier errors\n" ));
171 * The following call will block till a kioc is available
172 * or return NULL if the list head is empty for the pointer
173 * of type mraid_mmapt passed to mraid_mm_alloc_kioc
175 kioc
= mraid_mm_alloc_kioc(adp
);
180 * User sent the old mimd_t ioctl packet. Convert it to uioc_t.
182 if ((rval
= mimd_to_kioc(argp
, adp
, kioc
))) {
183 mraid_mm_dealloc_kioc(adp
, kioc
);
187 kioc
->done
= ioctl_done
;
190 * Issue the IOCTL to the low level driver. After the IOCTL completes
191 * release the kioc if and only if it was _not_ timedout. If it was
192 * timedout, that means that resources are still with low level driver.
194 if ((rval
= lld_ioctl(adp
, kioc
))) {
197 mraid_mm_dealloc_kioc(adp
, kioc
);
203 * Convert the kioc back to user space
205 rval
= kioc_to_mimd(kioc
, argp
);
208 * Return the kioc to free pool
210 mraid_mm_dealloc_kioc(adp
, kioc
);
216 mraid_mm_unlocked_ioctl(struct file
*filep
, unsigned int cmd
,
221 mutex_lock(&mraid_mm_mutex
);
222 err
= mraid_mm_ioctl(filep
, cmd
, arg
);
223 mutex_unlock(&mraid_mm_mutex
);
229 * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
230 * @umimd : User space mimd_t ioctl packet
231 * @rval : returned success/error status
233 * The function return value is a pointer to the located @adapter.
235 static mraid_mmadp_t
*
236 mraid_mm_get_adapter(mimd_t __user
*umimd
, int *rval
)
238 mraid_mmadp_t
*adapter
;
244 if (copy_from_user(&mimd
, umimd
, sizeof(mimd_t
))) {
249 adapno
= GETADAP(mimd
.ui
.fcs
.adapno
);
251 if (adapno
>= adapters_count_g
) {
259 list_for_each_entry(adapter
, &adapters_list_g
, list
) {
260 if (iterator
++ == adapno
) break;
272 * handle_drvrcmd - Checks if the opcode is a driver cmd and if it is, handles it.
273 * @arg : packet sent by the user app
274 * @old_ioctl : mimd if 1; uioc otherwise
275 * @rval : pointer for command's returned value (not function status)
278 handle_drvrcmd(void __user
*arg
, uint8_t old_ioctl
, int *rval
)
280 mimd_t __user
*umimd
;
297 if (copy_from_user(&kmimd
, umimd
, sizeof(mimd_t
)))
300 opcode
= kmimd
.ui
.fcs
.opcode
;
301 subopcode
= kmimd
.ui
.fcs
.subopcode
;
304 * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
305 * GET_NUMADP, then we can handle. Otherwise we should return 1 to
306 * indicate that we cannot handle this.
313 case MEGAIOC_QDRVRVER
:
315 if (copy_to_user(kmimd
.data
, &drvr_ver
, sizeof(uint32_t)))
322 *rval
= adapters_count_g
;
324 if (copy_to_user(kmimd
.data
, &adapters_count_g
,
340 * mimd_to_kioc - Converter from old to new ioctl format
341 * @umimd : user space old MIMD IOCTL
342 * @adp : adapter softstate
343 * @kioc : kernel space new format IOCTL
345 * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
346 * new packet is in kernel space so that driver can perform operations on it
351 mimd_to_kioc(mimd_t __user
*umimd
, mraid_mmadp_t
*adp
, uioc_t
*kioc
)
355 mraid_passthru_t
*pthru32
;
361 if (copy_from_user(&mimd
, umimd
, sizeof(mimd_t
)))
365 * Applications are not allowed to send extd pthru
367 if ((mimd
.mbox
[0] == MBOXCMD_PASSTHRU64
) ||
368 (mimd
.mbox
[0] == MBOXCMD_EXTPTHRU
))
371 opcode
= mimd
.ui
.fcs
.opcode
;
372 subopcode
= mimd
.ui
.fcs
.subopcode
;
373 adapno
= GETADAP(mimd
.ui
.fcs
.adapno
);
375 if (adapno
>= adapters_count_g
)
378 kioc
->adapno
= adapno
;
379 kioc
->mb_type
= MBOX_LEGACY
;
380 kioc
->app_type
= APPTYPE_MIMD
;
386 if (subopcode
== MEGAIOC_QADAPINFO
) {
388 kioc
->opcode
= GET_ADAP_INFO
;
389 kioc
->data_dir
= UIOC_RD
;
390 kioc
->xferlen
= sizeof(mraid_hba_info_t
);
392 if (mraid_mm_attach_buf(adp
, kioc
, kioc
->xferlen
))
396 con_log(CL_ANN
, (KERN_WARNING
397 "megaraid cmm: Invalid subop\n"));
405 kioc
->opcode
= MBOX_CMD
;
406 kioc
->xferlen
= mimd
.ui
.fcs
.length
;
407 kioc
->user_data_len
= kioc
->xferlen
;
408 kioc
->user_data
= mimd
.ui
.fcs
.buffer
;
410 if (mraid_mm_attach_buf(adp
, kioc
, kioc
->xferlen
))
413 if (mimd
.outlen
) kioc
->data_dir
= UIOC_RD
;
414 if (mimd
.inlen
) kioc
->data_dir
|= UIOC_WR
;
420 kioc
->opcode
= MBOX_CMD
;
421 kioc
->xferlen
= (mimd
.outlen
> mimd
.inlen
) ?
422 mimd
.outlen
: mimd
.inlen
;
423 kioc
->user_data_len
= kioc
->xferlen
;
424 kioc
->user_data
= mimd
.data
;
426 if (mraid_mm_attach_buf(adp
, kioc
, kioc
->xferlen
))
429 if (mimd
.outlen
) kioc
->data_dir
= UIOC_RD
;
430 if (mimd
.inlen
) kioc
->data_dir
|= UIOC_WR
;
439 * If driver command, nothing else to do
445 * This is a mailbox cmd; copy the mailbox from mimd
447 mbox64
= (mbox64_t
*)((unsigned long)kioc
->cmdbuf
);
448 mbox
= &mbox64
->mbox32
;
449 memcpy(mbox
, mimd
.mbox
, 14);
451 if (mbox
->cmd
!= MBOXCMD_PASSTHRU
) { // regular DCMD
453 mbox
->xferaddr
= (uint32_t)kioc
->buf_paddr
;
455 if (kioc
->data_dir
& UIOC_WR
) {
456 if (copy_from_user(kioc
->buf_vaddr
, kioc
->user_data
,
466 * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
467 * Just like in above case, the beginning for memblk is treated as
468 * a mailbox. The passthru will begin at next 1K boundary. And the
469 * data will start 1K after that.
471 pthru32
= kioc
->pthru32
;
472 kioc
->user_pthru
= &umimd
->pthru
;
473 mbox
->xferaddr
= (uint32_t)kioc
->pthru32_h
;
475 if (copy_from_user(pthru32
, kioc
->user_pthru
,
476 sizeof(mraid_passthru_t
))) {
480 pthru32
->dataxferaddr
= kioc
->buf_paddr
;
481 if (kioc
->data_dir
& UIOC_WR
) {
482 if (pthru32
->dataxferlen
> kioc
->xferlen
)
484 if (copy_from_user(kioc
->buf_vaddr
, kioc
->user_data
,
485 pthru32
->dataxferlen
)) {
494 * mraid_mm_attch_buf - Attach a free dma buffer for required size
495 * @adp : Adapter softstate
496 * @kioc : kioc that the buffer needs to be attached to
497 * @xferlen : required length for buffer
499 * First we search for a pool with smallest buffer that is >= @xferlen. If
500 * that pool has no free buffer, we will try for the next bigger size. If none
501 * is available, we will try to allocate the smallest buffer that is >=
502 * @xferlen and attach it the pool.
505 mraid_mm_attach_buf(mraid_mmadp_t
*adp
, uioc_t
*kioc
, int xferlen
)
512 kioc
->pool_index
= -1;
513 kioc
->buf_vaddr
= NULL
;
518 * We need xferlen amount of memory. See if we can get it from our
519 * dma pools. If we don't get exact size, we will try bigger buffer
522 for (i
= 0; i
< MAX_DMA_POOLS
; i
++) {
524 pool
= &adp
->dma_pool_list
[i
];
526 if (xferlen
> pool
->buf_size
)
529 if (right_pool
== -1)
532 spin_lock_irqsave(&pool
->lock
, flags
);
537 kioc
->pool_index
= i
;
538 kioc
->buf_vaddr
= pool
->vaddr
;
539 kioc
->buf_paddr
= pool
->paddr
;
541 spin_unlock_irqrestore(&pool
->lock
, flags
);
545 spin_unlock_irqrestore(&pool
->lock
, flags
);
551 * If xferlen doesn't match any of our pools, return error
553 if (right_pool
== -1)
557 * We did not get any buffer from the preallocated pool. Let us try
558 * to allocate one new buffer. NOTE: This is a blocking call.
560 pool
= &adp
->dma_pool_list
[right_pool
];
562 spin_lock_irqsave(&pool
->lock
, flags
);
564 kioc
->pool_index
= right_pool
;
566 kioc
->buf_vaddr
= dma_pool_alloc(pool
->handle
, GFP_ATOMIC
,
568 spin_unlock_irqrestore(&pool
->lock
, flags
);
570 if (!kioc
->buf_vaddr
)
577 * mraid_mm_alloc_kioc - Returns a uioc_t from free list
578 * @adp : Adapter softstate for this module
580 * The kioc_semaphore is initialized with number of kioc nodes in the
581 * free kioc pool. If the kioc pool is empty, this function blocks till
582 * a kioc becomes free.
585 mraid_mm_alloc_kioc(mraid_mmadp_t
*adp
)
588 struct list_head
* head
;
591 down(&adp
->kioc_semaphore
);
593 spin_lock_irqsave(&adp
->kioc_pool_lock
, flags
);
595 head
= &adp
->kioc_pool
;
597 if (list_empty(head
)) {
598 up(&adp
->kioc_semaphore
);
599 spin_unlock_irqrestore(&adp
->kioc_pool_lock
, flags
);
601 con_log(CL_ANN
, ("megaraid cmm: kioc list empty!\n"));
605 kioc
= list_entry(head
->next
, uioc_t
, list
);
606 list_del_init(&kioc
->list
);
608 spin_unlock_irqrestore(&adp
->kioc_pool_lock
, flags
);
610 memset((caddr_t
)(unsigned long)kioc
->cmdbuf
, 0, sizeof(mbox64_t
));
611 memset((caddr_t
) kioc
->pthru32
, 0, sizeof(mraid_passthru_t
));
613 kioc
->buf_vaddr
= NULL
;
615 kioc
->pool_index
=-1;
617 kioc
->user_data
= NULL
;
618 kioc
->user_data_len
= 0;
619 kioc
->user_pthru
= NULL
;
626 * mraid_mm_dealloc_kioc - Return kioc to free pool
627 * @adp : Adapter softstate
628 * @kioc : uioc_t node to be returned to free pool
631 mraid_mm_dealloc_kioc(mraid_mmadp_t
*adp
, uioc_t
*kioc
)
636 if (kioc
->pool_index
!= -1) {
637 pool
= &adp
->dma_pool_list
[kioc
->pool_index
];
639 /* This routine may be called in non-isr context also */
640 spin_lock_irqsave(&pool
->lock
, flags
);
643 * While attaching the dma buffer, if we didn't get the
644 * required buffer from the pool, we would have allocated
645 * it at the run time and set the free_buf flag. We must
646 * free that buffer. Otherwise, just mark that the buffer is
649 if (kioc
->free_buf
== 1)
650 dma_pool_free(pool
->handle
, kioc
->buf_vaddr
,
655 spin_unlock_irqrestore(&pool
->lock
, flags
);
658 /* Return the kioc to the free pool */
659 spin_lock_irqsave(&adp
->kioc_pool_lock
, flags
);
660 list_add(&kioc
->list
, &adp
->kioc_pool
);
661 spin_unlock_irqrestore(&adp
->kioc_pool_lock
, flags
);
663 /* increment the free kioc count */
664 up(&adp
->kioc_semaphore
);
670 * lld_ioctl - Routine to issue ioctl to low level drvr
671 * @adp : The adapter handle
672 * @kioc : The ioctl packet with kernel addresses
675 lld_ioctl(mraid_mmadp_t
*adp
, uioc_t
*kioc
)
678 struct uioc_timeout timeout
= { };
680 kioc
->status
= -ENODATA
;
681 rval
= adp
->issue_uioc(adp
->drvr_data
, kioc
, IOCTL_ISSUE
);
683 if (rval
) return rval
;
688 if (adp
->timeout
> 0) {
690 timer_setup_on_stack(&timeout
.timer
, lld_timedout
, 0);
692 timeout
.timer
.expires
= jiffies
+ adp
->timeout
* HZ
;
694 add_timer(&timeout
.timer
);
698 * Wait till the low level driver completes the ioctl. After this
699 * call, the ioctl either completed successfully or timedout.
701 wait_event(wait_q
, (kioc
->status
!= -ENODATA
));
702 if (timeout
.timer
.function
) {
703 del_timer_sync(&timeout
.timer
);
704 destroy_timer_on_stack(&timeout
.timer
);
708 * If the command had timedout, we mark the controller offline
711 if (kioc
->timedout
) {
720 * ioctl_done - callback from the low level driver
721 * @kioc : completed ioctl packet
724 ioctl_done(uioc_t
*kioc
)
728 mraid_mmadp_t
* adapter
;
731 * When the kioc returns from driver, make sure it still doesn't
732 * have ENODATA in status. Otherwise, driver will hang on wait_event
735 if (kioc
->status
== -ENODATA
) {
736 con_log(CL_ANN
, (KERN_WARNING
737 "megaraid cmm: lld didn't change status!\n"));
739 kioc
->status
= -EINVAL
;
743 * Check if this kioc was timedout before. If so, nobody is waiting
744 * on this kioc. We don't have to wake up anybody. Instead, we just
745 * have to free the kioc
747 if (kioc
->timedout
) {
750 adapno
= kioc
->adapno
;
752 con_log(CL_ANN
, ( KERN_WARNING
"megaraid cmm: completed "
753 "ioctl that was timedout before\n"));
755 list_for_each_entry(adapter
, &adapters_list_g
, list
) {
756 if (iterator
++ == adapno
) break;
762 mraid_mm_dealloc_kioc( adapter
, kioc
);
772 * lld_timedout - callback from the expired timer
773 * @t : timer that timed out
776 lld_timedout(struct timer_list
*t
)
778 struct uioc_timeout
*timeout
= from_timer(timeout
, t
, timer
);
779 uioc_t
*kioc
= timeout
->uioc
;
781 kioc
->status
= -ETIME
;
784 con_log(CL_ANN
, (KERN_WARNING
"megaraid cmm: ioctl timed out\n"));
791 * kioc_to_mimd - Converter from new back to old format
792 * @kioc : Kernel space IOCTL packet (successfully issued)
793 * @mimd : User space MIMD packet
796 kioc_to_mimd(uioc_t
*kioc
, mimd_t __user
*mimd
)
803 mraid_passthru_t __user
*upthru32
;
804 mraid_passthru_t
*kpthru32
;
806 mraid_hba_info_t
*hinfo
;
809 if (copy_from_user(&kmimd
, mimd
, sizeof(mimd_t
)))
812 opcode
= kmimd
.ui
.fcs
.opcode
;
813 subopcode
= kmimd
.ui
.fcs
.subopcode
;
815 if (opcode
== 0x82) {
818 case MEGAIOC_QADAPINFO
:
820 hinfo
= (mraid_hba_info_t
*)(unsigned long)
823 hinfo_to_cinfo(hinfo
, &cinfo
);
825 if (copy_to_user(kmimd
.data
, &cinfo
, sizeof(cinfo
)))
837 mbox64
= (mbox64_t
*)(unsigned long)kioc
->cmdbuf
;
839 if (kioc
->user_pthru
) {
841 upthru32
= kioc
->user_pthru
;
842 kpthru32
= kioc
->pthru32
;
844 if (copy_to_user(&upthru32
->scsistatus
,
845 &kpthru32
->scsistatus
,
851 if (kioc
->user_data
) {
852 if (copy_to_user(kioc
->user_data
, kioc
->buf_vaddr
,
853 kioc
->user_data_len
)) {
858 if (copy_to_user(&mimd
->mbox
[17],
859 &mbox64
->mbox32
.status
, sizeof(uint8_t))) {
868 * hinfo_to_cinfo - Convert new format hba info into old format
869 * @hinfo : New format, more comprehensive adapter info
870 * @cinfo : Old format adapter info to support mimd_t apps
873 hinfo_to_cinfo(mraid_hba_info_t
*hinfo
, mcontroller_t
*cinfo
)
875 if (!hinfo
|| !cinfo
)
878 cinfo
->base
= hinfo
->baseport
;
879 cinfo
->irq
= hinfo
->irq
;
880 cinfo
->numldrv
= hinfo
->num_ldrv
;
881 cinfo
->pcibus
= hinfo
->pci_bus
;
882 cinfo
->pcidev
= hinfo
->pci_slot
;
883 cinfo
->pcifun
= PCI_FUNC(hinfo
->pci_dev_fn
);
884 cinfo
->pciid
= hinfo
->pci_device_id
;
885 cinfo
->pcivendor
= hinfo
->pci_vendor_id
;
886 cinfo
->pcislot
= hinfo
->pci_slot
;
887 cinfo
->uid
= hinfo
->unique_id
;
892 * mraid_mm_register_adp - Registration routine for low level drivers
893 * @lld_adp : Adapter object
896 mraid_mm_register_adp(mraid_mmadp_t
*lld_adp
)
898 mraid_mmadp_t
*adapter
;
905 if (lld_adp
->drvr_type
!= DRVRTYPE_MBOX
)
908 adapter
= kzalloc(sizeof(mraid_mmadp_t
), GFP_KERNEL
);
914 adapter
->unique_id
= lld_adp
->unique_id
;
915 adapter
->drvr_type
= lld_adp
->drvr_type
;
916 adapter
->drvr_data
= lld_adp
->drvr_data
;
917 adapter
->pdev
= lld_adp
->pdev
;
918 adapter
->issue_uioc
= lld_adp
->issue_uioc
;
919 adapter
->timeout
= lld_adp
->timeout
;
920 adapter
->max_kioc
= lld_adp
->max_kioc
;
921 adapter
->quiescent
= 1;
924 * Allocate single blocks of memory for all required kiocs,
925 * mailboxes and passthru structures.
927 adapter
->kioc_list
= kmalloc_array(lld_adp
->max_kioc
,
930 adapter
->mbox_list
= kmalloc_array(lld_adp
->max_kioc
,
933 adapter
->pthru_dma_pool
= dma_pool_create("megaraid mm pthru pool",
935 sizeof(mraid_passthru_t
),
938 if (!adapter
->kioc_list
|| !adapter
->mbox_list
||
939 !adapter
->pthru_dma_pool
) {
941 con_log(CL_ANN
, (KERN_WARNING
942 "megaraid cmm: out of memory, %s %d\n", __func__
,
951 * Slice kioc_list and make a kioc_pool with the individiual kiocs
953 INIT_LIST_HEAD(&adapter
->kioc_pool
);
954 spin_lock_init(&adapter
->kioc_pool_lock
);
955 sema_init(&adapter
->kioc_semaphore
, lld_adp
->max_kioc
);
957 mbox_list
= (mbox64_t
*)adapter
->mbox_list
;
959 for (i
= 0; i
< lld_adp
->max_kioc
; i
++) {
961 kioc
= adapter
->kioc_list
+ i
;
962 kioc
->cmdbuf
= (uint64_t)(unsigned long)(mbox_list
+ i
);
963 kioc
->pthru32
= dma_pool_alloc(adapter
->pthru_dma_pool
,
964 GFP_KERNEL
, &kioc
->pthru32_h
);
966 if (!kioc
->pthru32
) {
968 con_log(CL_ANN
, (KERN_WARNING
969 "megaraid cmm: out of memory, %s %d\n",
970 __func__
, __LINE__
));
974 goto pthru_dma_pool_error
;
977 list_add_tail(&kioc
->list
, &adapter
->kioc_pool
);
980 // Setup the dma pools for data buffers
981 if ((rval
= mraid_mm_setup_dma_pools(adapter
)) != 0) {
985 list_add_tail(&adapter
->list
, &adapters_list_g
);
994 pthru_dma_pool_error
:
996 for (i
= 0; i
< lld_adp
->max_kioc
; i
++) {
997 kioc
= adapter
->kioc_list
+ i
;
999 dma_pool_free(adapter
->pthru_dma_pool
, kioc
->pthru32
,
1006 kfree(adapter
->kioc_list
);
1007 kfree(adapter
->mbox_list
);
1009 dma_pool_destroy(adapter
->pthru_dma_pool
);
1018 * mraid_mm_adapter_app_handle - return the application handle for this adapter
1019 * @unique_id : adapter unique identifier
1021 * For the given driver data, locate the adapter in our global list and
1022 * return the corresponding handle, which is also used by applications to
1023 * uniquely identify an adapter.
1025 * Return adapter handle if found in the list.
1026 * Return 0 if adapter could not be located, should never happen though.
1029 mraid_mm_adapter_app_handle(uint32_t unique_id
)
1031 mraid_mmadp_t
*adapter
;
1035 list_for_each_entry_safe(adapter
, tmp
, &adapters_list_g
, list
) {
1037 if (adapter
->unique_id
== unique_id
) {
1039 return MKADAP(index
);
1050 * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
1051 * @adp : Adapter softstate
1053 * We maintain a pool of dma buffers per each adapter. Each pool has one
1054 * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
1055 * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
1056 * dont' want to waste too much memory by allocating more buffers per each
1060 mraid_mm_setup_dma_pools(mraid_mmadp_t
*adp
)
1067 * Create MAX_DMA_POOLS number of pools
1069 bufsize
= MRAID_MM_INIT_BUFF_SIZE
;
1071 for (i
= 0; i
< MAX_DMA_POOLS
; i
++){
1073 pool
= &adp
->dma_pool_list
[i
];
1075 pool
->buf_size
= bufsize
;
1076 spin_lock_init(&pool
->lock
);
1078 pool
->handle
= dma_pool_create("megaraid mm data buffer",
1079 &adp
->pdev
->dev
, bufsize
,
1082 if (!pool
->handle
) {
1083 goto dma_pool_setup_error
;
1086 pool
->vaddr
= dma_pool_alloc(pool
->handle
, GFP_KERNEL
,
1090 goto dma_pool_setup_error
;
1092 bufsize
= bufsize
* 2;
1097 dma_pool_setup_error
:
1099 mraid_mm_teardown_dma_pools(adp
);
1105 * mraid_mm_unregister_adp - Unregister routine for low level drivers
1106 * @unique_id : UID of the adpater
1108 * Assumes no outstanding ioctls to llds.
1111 mraid_mm_unregister_adp(uint32_t unique_id
)
1113 mraid_mmadp_t
*adapter
;
1116 list_for_each_entry_safe(adapter
, tmp
, &adapters_list_g
, list
) {
1119 if (adapter
->unique_id
== unique_id
) {
1123 list_del_init(&adapter
->list
);
1125 mraid_mm_free_adp_resources(adapter
);
1130 "megaraid cmm: Unregistered one adapter:%#x\n",
1141 * mraid_mm_free_adp_resources - Free adapter softstate
1142 * @adp : Adapter softstate
1145 mraid_mm_free_adp_resources(mraid_mmadp_t
*adp
)
1150 mraid_mm_teardown_dma_pools(adp
);
1152 for (i
= 0; i
< adp
->max_kioc
; i
++) {
1154 kioc
= adp
->kioc_list
+ i
;
1156 dma_pool_free(adp
->pthru_dma_pool
, kioc
->pthru32
,
1160 kfree(adp
->kioc_list
);
1161 kfree(adp
->mbox_list
);
1163 dma_pool_destroy(adp
->pthru_dma_pool
);
1171 * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
1172 * @adp : Adapter softstate
1175 mraid_mm_teardown_dma_pools(mraid_mmadp_t
*adp
)
1180 for (i
= 0; i
< MAX_DMA_POOLS
; i
++) {
1182 pool
= &adp
->dma_pool_list
[i
];
1187 dma_pool_free(pool
->handle
, pool
->vaddr
,
1190 dma_pool_destroy(pool
->handle
);
1191 pool
->handle
= NULL
;
1199 * mraid_mm_init - Module entry point
1206 // Announce the driver version
1207 con_log(CL_ANN
, (KERN_INFO
"megaraid cmm: %s %s\n",
1208 LSI_COMMON_MOD_VERSION
, LSI_COMMON_MOD_EXT_VERSION
));
1210 err
= misc_register(&megaraid_mm_dev
);
1212 con_log(CL_ANN
, ("megaraid cmm: cannot register misc device\n"));
1216 init_waitqueue_head(&wait_q
);
1218 INIT_LIST_HEAD(&adapters_list_g
);
1225 * mraid_mm_exit - Module exit point
1230 con_log(CL_DLEVEL1
, ("exiting common mod\n"));
1232 misc_deregister(&megaraid_mm_dev
);
1235 module_init(mraid_mm_init
);
1236 module_exit(mraid_mm_exit
);
1238 /* vi: set ts=8 sw=8 tw=78: */