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 *);
45 static long mraid_mm_compat_ioctl(struct file *, unsigned int, unsigned long);
48 MODULE_AUTHOR("LSI Logic Corporation");
49 MODULE_DESCRIPTION("LSI Logic Management Module");
50 MODULE_LICENSE("GPL");
51 MODULE_VERSION(LSI_COMMON_MOD_VERSION);
53 static int dbglevel = CL_ANN;
54 module_param_named(dlevel, dbglevel, int, 0);
55 MODULE_PARM_DESC(dlevel, "Debug level (default=0)");
57 EXPORT_SYMBOL(mraid_mm_register_adp);
58 EXPORT_SYMBOL(mraid_mm_unregister_adp);
59 EXPORT_SYMBOL(mraid_mm_adapter_app_handle);
61 static uint32_t drvr_ver = 0x02200207;
63 static int adapters_count_g;
64 static struct list_head adapters_list_g;
66 static wait_queue_head_t wait_q;
68 static const struct file_operations lsi_fops = {
69 .open = mraid_mm_open,
70 .unlocked_ioctl = mraid_mm_unlocked_ioctl,
72 .compat_ioctl = mraid_mm_compat_ioctl,
75 .llseek = noop_llseek,
78 static struct miscdevice megaraid_mm_dev = {
79 .minor = MISC_DYNAMIC_MINOR,
85 * mraid_mm_open - open routine for char node interface
89 * Allow ioctl operations by apps only if they have superuser privilege.
92 mraid_mm_open(struct inode *inode, struct file *filep)
95 * Only allow superuser to access private ioctl interface
97 if (!capable(CAP_SYS_ADMIN)) return (-EACCES);
103 * mraid_mm_ioctl - module entry-point for ioctls
104 * @inode : inode (ignored)
105 * @filep : file operations pointer (ignored)
106 * @cmd : ioctl command
107 * @arg : user ioctl packet
110 mraid_mm_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
113 char signature[EXT_IOCTL_SIGN_SZ] = {0};
118 void __user *argp = (void __user *)arg;
121 * Make sure only USCSICMD are issued through this interface.
122 * MIMD application would still fire different command.
125 if ((_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD)) {
130 * Look for signature to see if this is the new or old ioctl format.
132 if (copy_from_user(signature, argp, EXT_IOCTL_SIGN_SZ)) {
133 con_log(CL_ANN, (KERN_WARNING
134 "megaraid cmm: copy from usr addr failed\n"));
138 if (memcmp(signature, EXT_IOCTL_SIGN, EXT_IOCTL_SIGN_SZ) == 0)
144 * At present, we don't support the new ioctl packet
150 * If it is a driver ioctl (as opposed to fw ioctls), then we can
151 * handle the command locally. rval > 0 means it is not a drvr cmd
153 rval = handle_drvrcmd(argp, old_ioctl, &drvrcmd_rval);
161 if ((adp = mraid_mm_get_adapter(argp, &rval)) == NULL) {
166 * Check if adapter can accept ioctl. We may have marked it offline
167 * if any previous kioc had timedout on this controller.
169 if (!adp->quiescent) {
170 con_log(CL_ANN, (KERN_WARNING
171 "megaraid cmm: controller cannot accept cmds due to "
172 "earlier errors\n" ));
177 * The following call will block till a kioc is available
178 * or return NULL if the list head is empty for the pointer
179 * of type mraid_mmapt passed to mraid_mm_alloc_kioc
181 kioc = mraid_mm_alloc_kioc(adp);
186 * User sent the old mimd_t ioctl packet. Convert it to uioc_t.
188 if ((rval = mimd_to_kioc(argp, adp, kioc))) {
189 mraid_mm_dealloc_kioc(adp, kioc);
193 kioc->done = ioctl_done;
196 * Issue the IOCTL to the low level driver. After the IOCTL completes
197 * release the kioc if and only if it was _not_ timedout. If it was
198 * timedout, that means that resources are still with low level driver.
200 if ((rval = lld_ioctl(adp, kioc))) {
203 mraid_mm_dealloc_kioc(adp, kioc);
209 * Convert the kioc back to user space
211 rval = kioc_to_mimd(kioc, argp);
214 * Return the kioc to free pool
216 mraid_mm_dealloc_kioc(adp, kioc);
222 mraid_mm_unlocked_ioctl(struct file *filep, unsigned int cmd,
227 /* inconsistent: mraid_mm_compat_ioctl doesn't take the BKL */
228 mutex_lock(&mraid_mm_mutex);
229 err = mraid_mm_ioctl(filep, cmd, arg);
230 mutex_unlock(&mraid_mm_mutex);
236 * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
237 * @umimd : User space mimd_t ioctl packet
238 * @rval : returned success/error status
240 * The function return value is a pointer to the located @adapter.
242 static mraid_mmadp_t *
243 mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
245 mraid_mmadp_t *adapter;
251 if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) {
256 adapno = GETADAP(mimd.ui.fcs.adapno);
258 if (adapno >= adapters_count_g) {
266 list_for_each_entry(adapter, &adapters_list_g, list) {
267 if (iterator++ == adapno) break;
279 * handle_drvrcmd - Checks if the opcode is a driver cmd and if it is, handles it.
280 * @arg : packet sent by the user app
281 * @old_ioctl : mimd if 1; uioc otherwise
282 * @rval : pointer for command's returned value (not function status)
285 handle_drvrcmd(void __user *arg, uint8_t old_ioctl, int *rval)
287 mimd_t __user *umimd;
304 if (copy_from_user(&kmimd, umimd, sizeof(mimd_t)))
307 opcode = kmimd.ui.fcs.opcode;
308 subopcode = kmimd.ui.fcs.subopcode;
311 * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
312 * GET_NUMADP, then we can handle. Otherwise we should return 1 to
313 * indicate that we cannot handle this.
320 case MEGAIOC_QDRVRVER:
322 if (copy_to_user(kmimd.data, &drvr_ver, sizeof(uint32_t)))
329 *rval = adapters_count_g;
331 if (copy_to_user(kmimd.data, &adapters_count_g,
347 * mimd_to_kioc - Converter from old to new ioctl format
348 * @umimd : user space old MIMD IOCTL
349 * @adp : adapter softstate
350 * @kioc : kernel space new format IOCTL
352 * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
353 * new packet is in kernel space so that driver can perform operations on it
358 mimd_to_kioc(mimd_t __user *umimd, mraid_mmadp_t *adp, uioc_t *kioc)
362 mraid_passthru_t *pthru32;
368 if (copy_from_user(&mimd, umimd, sizeof(mimd_t)))
372 * Applications are not allowed to send extd pthru
374 if ((mimd.mbox[0] == MBOXCMD_PASSTHRU64) ||
375 (mimd.mbox[0] == MBOXCMD_EXTPTHRU))
378 opcode = mimd.ui.fcs.opcode;
379 subopcode = mimd.ui.fcs.subopcode;
380 adapno = GETADAP(mimd.ui.fcs.adapno);
382 if (adapno >= adapters_count_g)
385 kioc->adapno = adapno;
386 kioc->mb_type = MBOX_LEGACY;
387 kioc->app_type = APPTYPE_MIMD;
393 if (subopcode == MEGAIOC_QADAPINFO) {
395 kioc->opcode = GET_ADAP_INFO;
396 kioc->data_dir = UIOC_RD;
397 kioc->xferlen = sizeof(mraid_hba_info_t);
399 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
403 con_log(CL_ANN, (KERN_WARNING
404 "megaraid cmm: Invalid subop\n"));
412 kioc->opcode = MBOX_CMD;
413 kioc->xferlen = mimd.ui.fcs.length;
414 kioc->user_data_len = kioc->xferlen;
415 kioc->user_data = mimd.ui.fcs.buffer;
417 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
420 if (mimd.outlen) kioc->data_dir = UIOC_RD;
421 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
427 kioc->opcode = MBOX_CMD;
428 kioc->xferlen = (mimd.outlen > mimd.inlen) ?
429 mimd.outlen : mimd.inlen;
430 kioc->user_data_len = kioc->xferlen;
431 kioc->user_data = mimd.data;
433 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
436 if (mimd.outlen) kioc->data_dir = UIOC_RD;
437 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
446 * If driver command, nothing else to do
452 * This is a mailbox cmd; copy the mailbox from mimd
454 mbox64 = (mbox64_t *)((unsigned long)kioc->cmdbuf);
455 mbox = &mbox64->mbox32;
456 memcpy(mbox, mimd.mbox, 14);
458 if (mbox->cmd != MBOXCMD_PASSTHRU) { // regular DCMD
460 mbox->xferaddr = (uint32_t)kioc->buf_paddr;
462 if (kioc->data_dir & UIOC_WR) {
463 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
473 * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
474 * Just like in above case, the beginning for memblk is treated as
475 * a mailbox. The passthru will begin at next 1K boundary. And the
476 * data will start 1K after that.
478 pthru32 = kioc->pthru32;
479 kioc->user_pthru = &umimd->pthru;
480 mbox->xferaddr = (uint32_t)kioc->pthru32_h;
482 if (copy_from_user(pthru32, kioc->user_pthru,
483 sizeof(mraid_passthru_t))) {
487 pthru32->dataxferaddr = kioc->buf_paddr;
488 if (kioc->data_dir & UIOC_WR) {
489 if (pthru32->dataxferlen > kioc->xferlen)
491 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
492 pthru32->dataxferlen)) {
501 * mraid_mm_attch_buf - Attach a free dma buffer for required size
502 * @adp : Adapter softstate
503 * @kioc : kioc that the buffer needs to be attached to
504 * @xferlen : required length for buffer
506 * First we search for a pool with smallest buffer that is >= @xferlen. If
507 * that pool has no free buffer, we will try for the next bigger size. If none
508 * is available, we will try to allocate the smallest buffer that is >=
509 * @xferlen and attach it the pool.
512 mraid_mm_attach_buf(mraid_mmadp_t *adp, uioc_t *kioc, int xferlen)
519 kioc->pool_index = -1;
520 kioc->buf_vaddr = NULL;
525 * We need xferlen amount of memory. See if we can get it from our
526 * dma pools. If we don't get exact size, we will try bigger buffer
529 for (i = 0; i < MAX_DMA_POOLS; i++) {
531 pool = &adp->dma_pool_list[i];
533 if (xferlen > pool->buf_size)
536 if (right_pool == -1)
539 spin_lock_irqsave(&pool->lock, flags);
544 kioc->pool_index = i;
545 kioc->buf_vaddr = pool->vaddr;
546 kioc->buf_paddr = pool->paddr;
548 spin_unlock_irqrestore(&pool->lock, flags);
552 spin_unlock_irqrestore(&pool->lock, flags);
558 * If xferlen doesn't match any of our pools, return error
560 if (right_pool == -1)
564 * We did not get any buffer from the preallocated pool. Let us try
565 * to allocate one new buffer. NOTE: This is a blocking call.
567 pool = &adp->dma_pool_list[right_pool];
569 spin_lock_irqsave(&pool->lock, flags);
571 kioc->pool_index = right_pool;
573 kioc->buf_vaddr = dma_pool_alloc(pool->handle, GFP_ATOMIC,
575 spin_unlock_irqrestore(&pool->lock, flags);
577 if (!kioc->buf_vaddr)
584 * mraid_mm_alloc_kioc - Returns a uioc_t from free list
585 * @adp : Adapter softstate for this module
587 * The kioc_semaphore is initialized with number of kioc nodes in the
588 * free kioc pool. If the kioc pool is empty, this function blocks till
589 * a kioc becomes free.
592 mraid_mm_alloc_kioc(mraid_mmadp_t *adp)
595 struct list_head* head;
598 down(&adp->kioc_semaphore);
600 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
602 head = &adp->kioc_pool;
604 if (list_empty(head)) {
605 up(&adp->kioc_semaphore);
606 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
608 con_log(CL_ANN, ("megaraid cmm: kioc list empty!\n"));
612 kioc = list_entry(head->next, uioc_t, list);
613 list_del_init(&kioc->list);
615 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
617 memset((caddr_t)(unsigned long)kioc->cmdbuf, 0, sizeof(mbox64_t));
618 memset((caddr_t) kioc->pthru32, 0, sizeof(mraid_passthru_t));
620 kioc->buf_vaddr = NULL;
622 kioc->pool_index =-1;
624 kioc->user_data = NULL;
625 kioc->user_data_len = 0;
626 kioc->user_pthru = NULL;
633 * mraid_mm_dealloc_kioc - Return kioc to free pool
634 * @adp : Adapter softstate
635 * @kioc : uioc_t node to be returned to free pool
638 mraid_mm_dealloc_kioc(mraid_mmadp_t *adp, uioc_t *kioc)
643 if (kioc->pool_index != -1) {
644 pool = &adp->dma_pool_list[kioc->pool_index];
646 /* This routine may be called in non-isr context also */
647 spin_lock_irqsave(&pool->lock, flags);
650 * While attaching the dma buffer, if we didn't get the
651 * required buffer from the pool, we would have allocated
652 * it at the run time and set the free_buf flag. We must
653 * free that buffer. Otherwise, just mark that the buffer is
656 if (kioc->free_buf == 1)
657 dma_pool_free(pool->handle, kioc->buf_vaddr,
662 spin_unlock_irqrestore(&pool->lock, flags);
665 /* Return the kioc to the free pool */
666 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
667 list_add(&kioc->list, &adp->kioc_pool);
668 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
670 /* increment the free kioc count */
671 up(&adp->kioc_semaphore);
677 * lld_ioctl - Routine to issue ioctl to low level drvr
678 * @adp : The adapter handle
679 * @kioc : The ioctl packet with kernel addresses
682 lld_ioctl(mraid_mmadp_t *adp, uioc_t *kioc)
685 struct uioc_timeout timeout = { };
687 kioc->status = -ENODATA;
688 rval = adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE);
690 if (rval) return rval;
695 if (adp->timeout > 0) {
697 timer_setup_on_stack(&timeout.timer, lld_timedout, 0);
699 timeout.timer.expires = jiffies + adp->timeout * HZ;
701 add_timer(&timeout.timer);
705 * Wait till the low level driver completes the ioctl. After this
706 * call, the ioctl either completed successfully or timedout.
708 wait_event(wait_q, (kioc->status != -ENODATA));
709 if (timeout.timer.function) {
710 del_timer_sync(&timeout.timer);
711 destroy_timer_on_stack(&timeout.timer);
715 * If the command had timedout, we mark the controller offline
718 if (kioc->timedout) {
727 * ioctl_done - callback from the low level driver
728 * @kioc : completed ioctl packet
731 ioctl_done(uioc_t *kioc)
735 mraid_mmadp_t* adapter;
738 * When the kioc returns from driver, make sure it still doesn't
739 * have ENODATA in status. Otherwise, driver will hang on wait_event
742 if (kioc->status == -ENODATA) {
743 con_log(CL_ANN, (KERN_WARNING
744 "megaraid cmm: lld didn't change status!\n"));
746 kioc->status = -EINVAL;
750 * Check if this kioc was timedout before. If so, nobody is waiting
751 * on this kioc. We don't have to wake up anybody. Instead, we just
752 * have to free the kioc
754 if (kioc->timedout) {
757 adapno = kioc->adapno;
759 con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
760 "ioctl that was timedout before\n"));
762 list_for_each_entry(adapter, &adapters_list_g, list) {
763 if (iterator++ == adapno) break;
769 mraid_mm_dealloc_kioc( adapter, kioc );
779 * lld_timedout - callback from the expired timer
780 * @t : timer that timed out
783 lld_timedout(struct timer_list *t)
785 struct uioc_timeout *timeout = from_timer(timeout, t, timer);
786 uioc_t *kioc = timeout->uioc;
788 kioc->status = -ETIME;
791 con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n"));
798 * kioc_to_mimd - Converter from new back to old format
799 * @kioc : Kernel space IOCTL packet (successfully issued)
800 * @mimd : User space MIMD packet
803 kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd)
810 mraid_passthru_t __user *upthru32;
811 mraid_passthru_t *kpthru32;
813 mraid_hba_info_t *hinfo;
816 if (copy_from_user(&kmimd, mimd, sizeof(mimd_t)))
819 opcode = kmimd.ui.fcs.opcode;
820 subopcode = kmimd.ui.fcs.subopcode;
822 if (opcode == 0x82) {
825 case MEGAIOC_QADAPINFO:
827 hinfo = (mraid_hba_info_t *)(unsigned long)
830 hinfo_to_cinfo(hinfo, &cinfo);
832 if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo)))
844 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
846 if (kioc->user_pthru) {
848 upthru32 = kioc->user_pthru;
849 kpthru32 = kioc->pthru32;
851 if (copy_to_user(&upthru32->scsistatus,
852 &kpthru32->scsistatus,
858 if (kioc->user_data) {
859 if (copy_to_user(kioc->user_data, kioc->buf_vaddr,
860 kioc->user_data_len)) {
865 if (copy_to_user(&mimd->mbox[17],
866 &mbox64->mbox32.status, sizeof(uint8_t))) {
875 * hinfo_to_cinfo - Convert new format hba info into old format
876 * @hinfo : New format, more comprehensive adapter info
877 * @cinfo : Old format adapter info to support mimd_t apps
880 hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo)
882 if (!hinfo || !cinfo)
885 cinfo->base = hinfo->baseport;
886 cinfo->irq = hinfo->irq;
887 cinfo->numldrv = hinfo->num_ldrv;
888 cinfo->pcibus = hinfo->pci_bus;
889 cinfo->pcidev = hinfo->pci_slot;
890 cinfo->pcifun = PCI_FUNC(hinfo->pci_dev_fn);
891 cinfo->pciid = hinfo->pci_device_id;
892 cinfo->pcivendor = hinfo->pci_vendor_id;
893 cinfo->pcislot = hinfo->pci_slot;
894 cinfo->uid = hinfo->unique_id;
899 * mraid_mm_register_adp - Registration routine for low level drivers
900 * @lld_adp : Adapter object
903 mraid_mm_register_adp(mraid_mmadp_t *lld_adp)
905 mraid_mmadp_t *adapter;
912 if (lld_adp->drvr_type != DRVRTYPE_MBOX)
915 adapter = kzalloc(sizeof(mraid_mmadp_t), GFP_KERNEL);
921 adapter->unique_id = lld_adp->unique_id;
922 adapter->drvr_type = lld_adp->drvr_type;
923 adapter->drvr_data = lld_adp->drvr_data;
924 adapter->pdev = lld_adp->pdev;
925 adapter->issue_uioc = lld_adp->issue_uioc;
926 adapter->timeout = lld_adp->timeout;
927 adapter->max_kioc = lld_adp->max_kioc;
928 adapter->quiescent = 1;
931 * Allocate single blocks of memory for all required kiocs,
932 * mailboxes and passthru structures.
934 adapter->kioc_list = kmalloc_array(lld_adp->max_kioc,
937 adapter->mbox_list = kmalloc_array(lld_adp->max_kioc,
940 adapter->pthru_dma_pool = dma_pool_create("megaraid mm pthru pool",
942 sizeof(mraid_passthru_t),
945 if (!adapter->kioc_list || !adapter->mbox_list ||
946 !adapter->pthru_dma_pool) {
948 con_log(CL_ANN, (KERN_WARNING
949 "megaraid cmm: out of memory, %s %d\n", __func__,
958 * Slice kioc_list and make a kioc_pool with the individiual kiocs
960 INIT_LIST_HEAD(&adapter->kioc_pool);
961 spin_lock_init(&adapter->kioc_pool_lock);
962 sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
964 mbox_list = (mbox64_t *)adapter->mbox_list;
966 for (i = 0; i < lld_adp->max_kioc; i++) {
968 kioc = adapter->kioc_list + i;
969 kioc->cmdbuf = (uint64_t)(unsigned long)(mbox_list + i);
970 kioc->pthru32 = dma_pool_alloc(adapter->pthru_dma_pool,
971 GFP_KERNEL, &kioc->pthru32_h);
973 if (!kioc->pthru32) {
975 con_log(CL_ANN, (KERN_WARNING
976 "megaraid cmm: out of memory, %s %d\n",
977 __func__, __LINE__));
981 goto pthru_dma_pool_error;
984 list_add_tail(&kioc->list, &adapter->kioc_pool);
987 // Setup the dma pools for data buffers
988 if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
992 list_add_tail(&adapter->list, &adapters_list_g);
1001 pthru_dma_pool_error:
1003 for (i = 0; i < lld_adp->max_kioc; i++) {
1004 kioc = adapter->kioc_list + i;
1005 if (kioc->pthru32) {
1006 dma_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
1013 kfree(adapter->kioc_list);
1014 kfree(adapter->mbox_list);
1016 dma_pool_destroy(adapter->pthru_dma_pool);
1025 * mraid_mm_adapter_app_handle - return the application handle for this adapter
1026 * @unique_id : adapter unique identifier
1028 * For the given driver data, locate the adapter in our global list and
1029 * return the corresponding handle, which is also used by applications to
1030 * uniquely identify an adapter.
1032 * Return adapter handle if found in the list.
1033 * Return 0 if adapter could not be located, should never happen though.
1036 mraid_mm_adapter_app_handle(uint32_t unique_id)
1038 mraid_mmadp_t *adapter;
1042 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1044 if (adapter->unique_id == unique_id) {
1046 return MKADAP(index);
1057 * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
1058 * @adp : Adapter softstate
1060 * We maintain a pool of dma buffers per each adapter. Each pool has one
1061 * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
1062 * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
1063 * dont' want to waste too much memory by allocating more buffers per each
1067 mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
1074 * Create MAX_DMA_POOLS number of pools
1076 bufsize = MRAID_MM_INIT_BUFF_SIZE;
1078 for (i = 0; i < MAX_DMA_POOLS; i++){
1080 pool = &adp->dma_pool_list[i];
1082 pool->buf_size = bufsize;
1083 spin_lock_init(&pool->lock);
1085 pool->handle = dma_pool_create("megaraid mm data buffer",
1086 &adp->pdev->dev, bufsize,
1089 if (!pool->handle) {
1090 goto dma_pool_setup_error;
1093 pool->vaddr = dma_pool_alloc(pool->handle, GFP_KERNEL,
1097 goto dma_pool_setup_error;
1099 bufsize = bufsize * 2;
1104 dma_pool_setup_error:
1106 mraid_mm_teardown_dma_pools(adp);
1112 * mraid_mm_unregister_adp - Unregister routine for low level drivers
1113 * @unique_id : UID of the adpater
1115 * Assumes no outstanding ioctls to llds.
1118 mraid_mm_unregister_adp(uint32_t unique_id)
1120 mraid_mmadp_t *adapter;
1123 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1126 if (adapter->unique_id == unique_id) {
1130 list_del_init(&adapter->list);
1132 mraid_mm_free_adp_resources(adapter);
1137 "megaraid cmm: Unregistered one adapter:%#x\n",
1148 * mraid_mm_free_adp_resources - Free adapter softstate
1149 * @adp : Adapter softstate
1152 mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
1157 mraid_mm_teardown_dma_pools(adp);
1159 for (i = 0; i < adp->max_kioc; i++) {
1161 kioc = adp->kioc_list + i;
1163 dma_pool_free(adp->pthru_dma_pool, kioc->pthru32,
1167 kfree(adp->kioc_list);
1168 kfree(adp->mbox_list);
1170 dma_pool_destroy(adp->pthru_dma_pool);
1178 * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
1179 * @adp : Adapter softstate
1182 mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
1187 for (i = 0; i < MAX_DMA_POOLS; i++) {
1189 pool = &adp->dma_pool_list[i];
1194 dma_pool_free(pool->handle, pool->vaddr,
1197 dma_pool_destroy(pool->handle);
1198 pool->handle = NULL;
1206 * mraid_mm_init - Module entry point
1213 // Announce the driver version
1214 con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
1215 LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
1217 err = misc_register(&megaraid_mm_dev);
1219 con_log(CL_ANN, ("megaraid cmm: cannot register misc device\n"));
1223 init_waitqueue_head(&wait_q);
1225 INIT_LIST_HEAD(&adapters_list_g);
1231 #ifdef CONFIG_COMPAT
1233 * mraid_mm_compat_ioctl - 32bit to 64bit ioctl conversion routine
1234 * @filep : file operations pointer (ignored)
1235 * @cmd : ioctl command
1236 * @arg : user ioctl packet
1239 mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd,
1244 err = mraid_mm_ioctl(filep, cmd, arg);
1251 * mraid_mm_exit - Module exit point
1256 con_log(CL_DLEVEL1 , ("exiting common mod\n"));
1258 misc_deregister(&megaraid_mm_dev);
1261 module_init(mraid_mm_init);
1262 module_exit(mraid_mm_exit);
1264 /* vi: set ts=8 sw=8 tw=78: */