Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / scsi / megaraid / megaraid_mm.c
blob36e0b7d05c1dbc13cc6252094f02b68fbabc02c4
1 /*
3 * Linux MegaRAID device driver
5 * Copyright (c) 2003-2004 LSI Logic Corporation.
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.
12 * FILE : megaraid_mm.c
13 * Version : v2.20.2.7 (Jul 16 2006)
15 * Common management module
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/smp_lock.h>
20 #include "megaraid_mm.h"
23 // Entry points for char node driver
24 static int mraid_mm_open(struct inode *, struct file *);
25 static int mraid_mm_ioctl(struct inode *, struct file *, uint, unsigned long);
28 // routines to convert to and from the old the format
29 static int mimd_to_kioc(mimd_t __user *, mraid_mmadp_t *, uioc_t *);
30 static int kioc_to_mimd(uioc_t *, mimd_t __user *);
33 // Helper functions
34 static int handle_drvrcmd(void __user *, uint8_t, int *);
35 static int lld_ioctl(mraid_mmadp_t *, uioc_t *);
36 static void ioctl_done(uioc_t *);
37 static void lld_timedout(unsigned long);
38 static void hinfo_to_cinfo(mraid_hba_info_t *, mcontroller_t *);
39 static mraid_mmadp_t *mraid_mm_get_adapter(mimd_t __user *, int *);
40 static uioc_t *mraid_mm_alloc_kioc(mraid_mmadp_t *);
41 static void mraid_mm_dealloc_kioc(mraid_mmadp_t *, uioc_t *);
42 static int mraid_mm_attach_buf(mraid_mmadp_t *, uioc_t *, int);
43 static int mraid_mm_setup_dma_pools(mraid_mmadp_t *);
44 static void mraid_mm_free_adp_resources(mraid_mmadp_t *);
45 static void mraid_mm_teardown_dma_pools(mraid_mmadp_t *);
47 #ifdef CONFIG_COMPAT
48 static long mraid_mm_compat_ioctl(struct file *, unsigned int, unsigned long);
49 #endif
51 MODULE_AUTHOR("LSI Logic Corporation");
52 MODULE_DESCRIPTION("LSI Logic Management Module");
53 MODULE_LICENSE("GPL");
54 MODULE_VERSION(LSI_COMMON_MOD_VERSION);
56 static int dbglevel = CL_ANN;
57 module_param_named(dlevel, dbglevel, int, 0);
58 MODULE_PARM_DESC(dlevel, "Debug level (default=0)");
60 EXPORT_SYMBOL(mraid_mm_register_adp);
61 EXPORT_SYMBOL(mraid_mm_unregister_adp);
62 EXPORT_SYMBOL(mraid_mm_adapter_app_handle);
64 static uint32_t drvr_ver = 0x02200207;
66 static int adapters_count_g;
67 static struct list_head adapters_list_g;
69 static wait_queue_head_t wait_q;
71 static const struct file_operations lsi_fops = {
72 .open = mraid_mm_open,
73 .ioctl = mraid_mm_ioctl,
74 #ifdef CONFIG_COMPAT
75 .compat_ioctl = mraid_mm_compat_ioctl,
76 #endif
77 .owner = THIS_MODULE,
80 static struct miscdevice megaraid_mm_dev = {
81 .minor = MISC_DYNAMIC_MINOR,
82 .name = "megadev0",
83 .fops = &lsi_fops,
86 /**
87 * mraid_mm_open - open routine for char node interface
88 * @inode : unused
89 * @filep : unused
91 * Allow ioctl operations by apps only if they have superuser privilege.
93 static int
94 mraid_mm_open(struct inode *inode, struct file *filep)
97 * Only allow superuser to access private ioctl interface
99 if (!capable(CAP_SYS_ADMIN)) return (-EACCES);
101 cycle_kernel_lock();
102 return 0;
106 * mraid_mm_ioctl - module entry-point for ioctls
107 * @inode : inode (ignored)
108 * @filep : file operations pointer (ignored)
109 * @cmd : ioctl command
110 * @arg : user ioctl packet
112 static int
113 mraid_mm_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
114 unsigned long arg)
116 uioc_t *kioc;
117 char signature[EXT_IOCTL_SIGN_SZ] = {0};
118 int rval;
119 mraid_mmadp_t *adp;
120 uint8_t old_ioctl;
121 int drvrcmd_rval;
122 void __user *argp = (void __user *)arg;
125 * Make sure only USCSICMD are issued through this interface.
126 * MIMD application would still fire different command.
129 if ((_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD)) {
130 return (-EINVAL);
134 * Look for signature to see if this is the new or old ioctl format.
136 if (copy_from_user(signature, argp, EXT_IOCTL_SIGN_SZ)) {
137 con_log(CL_ANN, (KERN_WARNING
138 "megaraid cmm: copy from usr addr failed\n"));
139 return (-EFAULT);
142 if (memcmp(signature, EXT_IOCTL_SIGN, EXT_IOCTL_SIGN_SZ) == 0)
143 old_ioctl = 0;
144 else
145 old_ioctl = 1;
148 * At present, we don't support the new ioctl packet
150 if (!old_ioctl )
151 return (-EINVAL);
154 * If it is a driver ioctl (as opposed to fw ioctls), then we can
155 * handle the command locally. rval > 0 means it is not a drvr cmd
157 rval = handle_drvrcmd(argp, old_ioctl, &drvrcmd_rval);
159 if (rval < 0)
160 return rval;
161 else if (rval == 0)
162 return drvrcmd_rval;
164 rval = 0;
165 if ((adp = mraid_mm_get_adapter(argp, &rval)) == NULL) {
166 return rval;
170 * Check if adapter can accept ioctl. We may have marked it offline
171 * if any previous kioc had timedout on this controller.
173 if (!adp->quiescent) {
174 con_log(CL_ANN, (KERN_WARNING
175 "megaraid cmm: controller cannot accept cmds due to "
176 "earlier errors\n" ));
177 return -EFAULT;
181 * The following call will block till a kioc is available
183 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);
190 return rval;
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))) {
202 if (!kioc->timedout)
203 mraid_mm_dealloc_kioc(adp, kioc);
205 return rval;
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);
218 return rval;
223 * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
224 * @umimd : User space mimd_t ioctl packet
225 * @rval : returned success/error status
227 * The function return value is a pointer to the located @adapter.
229 static mraid_mmadp_t *
230 mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
232 mraid_mmadp_t *adapter;
233 mimd_t mimd;
234 uint32_t adapno;
235 int iterator;
238 if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) {
239 *rval = -EFAULT;
240 return NULL;
243 adapno = GETADAP(mimd.ui.fcs.adapno);
245 if (adapno >= adapters_count_g) {
246 *rval = -ENODEV;
247 return NULL;
250 adapter = NULL;
251 iterator = 0;
253 list_for_each_entry(adapter, &adapters_list_g, list) {
254 if (iterator++ == adapno) break;
257 if (!adapter) {
258 *rval = -ENODEV;
259 return NULL;
262 return adapter;
266 * handle_drvrcmd - Checks if the opcode is a driver cmd and if it is, handles it.
267 * @arg : packet sent by the user app
268 * @old_ioctl : mimd if 1; uioc otherwise
269 * @rval : pointer for command's returned value (not function status)
271 static int
272 handle_drvrcmd(void __user *arg, uint8_t old_ioctl, int *rval)
274 mimd_t __user *umimd;
275 mimd_t kmimd;
276 uint8_t opcode;
277 uint8_t subopcode;
279 if (old_ioctl)
280 goto old_packet;
281 else
282 goto new_packet;
284 new_packet:
285 return (-ENOTSUPP);
287 old_packet:
288 *rval = 0;
289 umimd = arg;
291 if (copy_from_user(&kmimd, umimd, sizeof(mimd_t)))
292 return (-EFAULT);
294 opcode = kmimd.ui.fcs.opcode;
295 subopcode = kmimd.ui.fcs.subopcode;
298 * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
299 * GET_NUMADP, then we can handle. Otherwise we should return 1 to
300 * indicate that we cannot handle this.
302 if (opcode != 0x82)
303 return 1;
305 switch (subopcode) {
307 case MEGAIOC_QDRVRVER:
309 if (copy_to_user(kmimd.data, &drvr_ver, sizeof(uint32_t)))
310 return (-EFAULT);
312 return 0;
314 case MEGAIOC_QNADAP:
316 *rval = adapters_count_g;
318 if (copy_to_user(kmimd.data, &adapters_count_g,
319 sizeof(uint32_t)))
320 return (-EFAULT);
322 return 0;
324 default:
325 /* cannot handle */
326 return 1;
329 return 0;
334 * mimd_to_kioc - Converter from old to new ioctl format
335 * @umimd : user space old MIMD IOCTL
336 * @adp : adapter softstate
337 * @kioc : kernel space new format IOCTL
339 * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
340 * new packet is in kernel space so that driver can perform operations on it
341 * freely.
344 static int
345 mimd_to_kioc(mimd_t __user *umimd, mraid_mmadp_t *adp, uioc_t *kioc)
347 mbox64_t *mbox64;
348 mbox_t *mbox;
349 mraid_passthru_t *pthru32;
350 uint32_t adapno;
351 uint8_t opcode;
352 uint8_t subopcode;
353 mimd_t mimd;
355 if (copy_from_user(&mimd, umimd, sizeof(mimd_t)))
356 return (-EFAULT);
359 * Applications are not allowed to send extd pthru
361 if ((mimd.mbox[0] == MBOXCMD_PASSTHRU64) ||
362 (mimd.mbox[0] == MBOXCMD_EXTPTHRU))
363 return (-EINVAL);
365 opcode = mimd.ui.fcs.opcode;
366 subopcode = mimd.ui.fcs.subopcode;
367 adapno = GETADAP(mimd.ui.fcs.adapno);
369 if (adapno >= adapters_count_g)
370 return (-ENODEV);
372 kioc->adapno = adapno;
373 kioc->mb_type = MBOX_LEGACY;
374 kioc->app_type = APPTYPE_MIMD;
376 switch (opcode) {
378 case 0x82:
380 if (subopcode == MEGAIOC_QADAPINFO) {
382 kioc->opcode = GET_ADAP_INFO;
383 kioc->data_dir = UIOC_RD;
384 kioc->xferlen = sizeof(mraid_hba_info_t);
386 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
387 return (-ENOMEM);
389 else {
390 con_log(CL_ANN, (KERN_WARNING
391 "megaraid cmm: Invalid subop\n"));
392 return (-EINVAL);
395 break;
397 case 0x81:
399 kioc->opcode = MBOX_CMD;
400 kioc->xferlen = mimd.ui.fcs.length;
401 kioc->user_data_len = kioc->xferlen;
402 kioc->user_data = mimd.ui.fcs.buffer;
404 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
405 return (-ENOMEM);
407 if (mimd.outlen) kioc->data_dir = UIOC_RD;
408 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
410 break;
412 case 0x80:
414 kioc->opcode = MBOX_CMD;
415 kioc->xferlen = (mimd.outlen > mimd.inlen) ?
416 mimd.outlen : mimd.inlen;
417 kioc->user_data_len = kioc->xferlen;
418 kioc->user_data = mimd.data;
420 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
421 return (-ENOMEM);
423 if (mimd.outlen) kioc->data_dir = UIOC_RD;
424 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
426 break;
428 default:
429 return (-EINVAL);
433 * If driver command, nothing else to do
435 if (opcode == 0x82)
436 return 0;
439 * This is a mailbox cmd; copy the mailbox from mimd
441 mbox64 = (mbox64_t *)((unsigned long)kioc->cmdbuf);
442 mbox = &mbox64->mbox32;
443 memcpy(mbox, mimd.mbox, 14);
445 if (mbox->cmd != MBOXCMD_PASSTHRU) { // regular DCMD
447 mbox->xferaddr = (uint32_t)kioc->buf_paddr;
449 if (kioc->data_dir & UIOC_WR) {
450 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
451 kioc->xferlen)) {
452 return (-EFAULT);
456 return 0;
460 * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
461 * Just like in above case, the beginning for memblk is treated as
462 * a mailbox. The passthru will begin at next 1K boundary. And the
463 * data will start 1K after that.
465 pthru32 = kioc->pthru32;
466 kioc->user_pthru = &umimd->pthru;
467 mbox->xferaddr = (uint32_t)kioc->pthru32_h;
469 if (copy_from_user(pthru32, kioc->user_pthru,
470 sizeof(mraid_passthru_t))) {
471 return (-EFAULT);
474 pthru32->dataxferaddr = kioc->buf_paddr;
475 if (kioc->data_dir & UIOC_WR) {
476 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
477 pthru32->dataxferlen)) {
478 return (-EFAULT);
482 return 0;
486 * mraid_mm_attch_buf - Attach a free dma buffer for required size
487 * @adp : Adapter softstate
488 * @kioc : kioc that the buffer needs to be attached to
489 * @xferlen : required length for buffer
491 * First we search for a pool with smallest buffer that is >= @xferlen. If
492 * that pool has no free buffer, we will try for the next bigger size. If none
493 * is available, we will try to allocate the smallest buffer that is >=
494 * @xferlen and attach it the pool.
496 static int
497 mraid_mm_attach_buf(mraid_mmadp_t *adp, uioc_t *kioc, int xferlen)
499 mm_dmapool_t *pool;
500 int right_pool = -1;
501 unsigned long flags;
502 int i;
504 kioc->pool_index = -1;
505 kioc->buf_vaddr = NULL;
506 kioc->buf_paddr = 0;
507 kioc->free_buf = 0;
510 * We need xferlen amount of memory. See if we can get it from our
511 * dma pools. If we don't get exact size, we will try bigger buffer
514 for (i = 0; i < MAX_DMA_POOLS; i++) {
516 pool = &adp->dma_pool_list[i];
518 if (xferlen > pool->buf_size)
519 continue;
521 if (right_pool == -1)
522 right_pool = i;
524 spin_lock_irqsave(&pool->lock, flags);
526 if (!pool->in_use) {
528 pool->in_use = 1;
529 kioc->pool_index = i;
530 kioc->buf_vaddr = pool->vaddr;
531 kioc->buf_paddr = pool->paddr;
533 spin_unlock_irqrestore(&pool->lock, flags);
534 return 0;
536 else {
537 spin_unlock_irqrestore(&pool->lock, flags);
538 continue;
543 * If xferlen doesn't match any of our pools, return error
545 if (right_pool == -1)
546 return -EINVAL;
549 * We did not get any buffer from the preallocated pool. Let us try
550 * to allocate one new buffer. NOTE: This is a blocking call.
552 pool = &adp->dma_pool_list[right_pool];
554 spin_lock_irqsave(&pool->lock, flags);
556 kioc->pool_index = right_pool;
557 kioc->free_buf = 1;
558 kioc->buf_vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
559 &kioc->buf_paddr);
560 spin_unlock_irqrestore(&pool->lock, flags);
562 if (!kioc->buf_vaddr)
563 return -ENOMEM;
565 return 0;
569 * mraid_mm_alloc_kioc - Returns a uioc_t from free list
570 * @adp : Adapter softstate for this module
572 * The kioc_semaphore is initialized with number of kioc nodes in the
573 * free kioc pool. If the kioc pool is empty, this function blocks till
574 * a kioc becomes free.
576 static uioc_t *
577 mraid_mm_alloc_kioc(mraid_mmadp_t *adp)
579 uioc_t *kioc;
580 struct list_head* head;
581 unsigned long flags;
583 down(&adp->kioc_semaphore);
585 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
587 head = &adp->kioc_pool;
589 if (list_empty(head)) {
590 up(&adp->kioc_semaphore);
591 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
593 con_log(CL_ANN, ("megaraid cmm: kioc list empty!\n"));
594 return NULL;
597 kioc = list_entry(head->next, uioc_t, list);
598 list_del_init(&kioc->list);
600 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
602 memset((caddr_t)(unsigned long)kioc->cmdbuf, 0, sizeof(mbox64_t));
603 memset((caddr_t) kioc->pthru32, 0, sizeof(mraid_passthru_t));
605 kioc->buf_vaddr = NULL;
606 kioc->buf_paddr = 0;
607 kioc->pool_index =-1;
608 kioc->free_buf = 0;
609 kioc->user_data = NULL;
610 kioc->user_data_len = 0;
611 kioc->user_pthru = NULL;
612 kioc->timedout = 0;
614 return kioc;
618 * mraid_mm_dealloc_kioc - Return kioc to free pool
619 * @adp : Adapter softstate
620 * @kioc : uioc_t node to be returned to free pool
622 static void
623 mraid_mm_dealloc_kioc(mraid_mmadp_t *adp, uioc_t *kioc)
625 mm_dmapool_t *pool;
626 unsigned long flags;
628 if (kioc->pool_index != -1) {
629 pool = &adp->dma_pool_list[kioc->pool_index];
631 /* This routine may be called in non-isr context also */
632 spin_lock_irqsave(&pool->lock, flags);
635 * While attaching the dma buffer, if we didn't get the
636 * required buffer from the pool, we would have allocated
637 * it at the run time and set the free_buf flag. We must
638 * free that buffer. Otherwise, just mark that the buffer is
639 * not in use
641 if (kioc->free_buf == 1)
642 pci_pool_free(pool->handle, kioc->buf_vaddr,
643 kioc->buf_paddr);
644 else
645 pool->in_use = 0;
647 spin_unlock_irqrestore(&pool->lock, flags);
650 /* Return the kioc to the free pool */
651 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
652 list_add(&kioc->list, &adp->kioc_pool);
653 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
655 /* increment the free kioc count */
656 up(&adp->kioc_semaphore);
658 return;
662 * lld_ioctl - Routine to issue ioctl to low level drvr
663 * @adp : The adapter handle
664 * @kioc : The ioctl packet with kernel addresses
666 static int
667 lld_ioctl(mraid_mmadp_t *adp, uioc_t *kioc)
669 int rval;
670 struct timer_list timer;
671 struct timer_list *tp = NULL;
673 kioc->status = -ENODATA;
674 rval = adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE);
676 if (rval) return rval;
679 * Start the timer
681 if (adp->timeout > 0) {
682 tp = &timer;
683 init_timer(tp);
685 tp->function = lld_timedout;
686 tp->data = (unsigned long)kioc;
687 tp->expires = jiffies + adp->timeout * HZ;
689 add_timer(tp);
693 * Wait till the low level driver completes the ioctl. After this
694 * call, the ioctl either completed successfully or timedout.
696 wait_event(wait_q, (kioc->status != -ENODATA));
697 if (tp) {
698 del_timer_sync(tp);
702 * If the command had timedout, we mark the controller offline
703 * before returning
705 if (kioc->timedout) {
706 adp->quiescent = 0;
709 return kioc->status;
714 * ioctl_done - callback from the low level driver
715 * @kioc : completed ioctl packet
717 static void
718 ioctl_done(uioc_t *kioc)
720 uint32_t adapno;
721 int iterator;
722 mraid_mmadp_t* adapter;
725 * When the kioc returns from driver, make sure it still doesn't
726 * have ENODATA in status. Otherwise, driver will hang on wait_event
727 * forever
729 if (kioc->status == -ENODATA) {
730 con_log(CL_ANN, (KERN_WARNING
731 "megaraid cmm: lld didn't change status!\n"));
733 kioc->status = -EINVAL;
737 * Check if this kioc was timedout before. If so, nobody is waiting
738 * on this kioc. We don't have to wake up anybody. Instead, we just
739 * have to free the kioc
741 if (kioc->timedout) {
742 iterator = 0;
743 adapter = NULL;
744 adapno = kioc->adapno;
746 con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
747 "ioctl that was timedout before\n"));
749 list_for_each_entry(adapter, &adapters_list_g, list) {
750 if (iterator++ == adapno) break;
753 kioc->timedout = 0;
755 if (adapter) {
756 mraid_mm_dealloc_kioc( adapter, kioc );
759 else {
760 wake_up(&wait_q);
766 * lld_timedout - callback from the expired timer
767 * @ptr : ioctl packet that timed out
769 static void
770 lld_timedout(unsigned long ptr)
772 uioc_t *kioc = (uioc_t *)ptr;
774 kioc->status = -ETIME;
775 kioc->timedout = 1;
777 con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n"));
779 wake_up(&wait_q);
784 * kioc_to_mimd - Converter from new back to old format
785 * @kioc : Kernel space IOCTL packet (successfully issued)
786 * @mimd : User space MIMD packet
788 static int
789 kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd)
791 mimd_t kmimd;
792 uint8_t opcode;
793 uint8_t subopcode;
795 mbox64_t *mbox64;
796 mraid_passthru_t __user *upthru32;
797 mraid_passthru_t *kpthru32;
798 mcontroller_t cinfo;
799 mraid_hba_info_t *hinfo;
802 if (copy_from_user(&kmimd, mimd, sizeof(mimd_t)))
803 return (-EFAULT);
805 opcode = kmimd.ui.fcs.opcode;
806 subopcode = kmimd.ui.fcs.subopcode;
808 if (opcode == 0x82) {
809 switch (subopcode) {
811 case MEGAIOC_QADAPINFO:
813 hinfo = (mraid_hba_info_t *)(unsigned long)
814 kioc->buf_vaddr;
816 hinfo_to_cinfo(hinfo, &cinfo);
818 if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo)))
819 return (-EFAULT);
821 return 0;
823 default:
824 return (-EINVAL);
827 return 0;
830 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
832 if (kioc->user_pthru) {
834 upthru32 = kioc->user_pthru;
835 kpthru32 = kioc->pthru32;
837 if (copy_to_user(&upthru32->scsistatus,
838 &kpthru32->scsistatus,
839 sizeof(uint8_t))) {
840 return (-EFAULT);
844 if (kioc->user_data) {
845 if (copy_to_user(kioc->user_data, kioc->buf_vaddr,
846 kioc->user_data_len)) {
847 return (-EFAULT);
851 if (copy_to_user(&mimd->mbox[17],
852 &mbox64->mbox32.status, sizeof(uint8_t))) {
853 return (-EFAULT);
856 return 0;
861 * hinfo_to_cinfo - Convert new format hba info into old format
862 * @hinfo : New format, more comprehensive adapter info
863 * @cinfo : Old format adapter info to support mimd_t apps
865 static void
866 hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo)
868 if (!hinfo || !cinfo)
869 return;
871 cinfo->base = hinfo->baseport;
872 cinfo->irq = hinfo->irq;
873 cinfo->numldrv = hinfo->num_ldrv;
874 cinfo->pcibus = hinfo->pci_bus;
875 cinfo->pcidev = hinfo->pci_slot;
876 cinfo->pcifun = PCI_FUNC(hinfo->pci_dev_fn);
877 cinfo->pciid = hinfo->pci_device_id;
878 cinfo->pcivendor = hinfo->pci_vendor_id;
879 cinfo->pcislot = hinfo->pci_slot;
880 cinfo->uid = hinfo->unique_id;
885 * mraid_mm_register_adp - Registration routine for low level drivers
886 * @lld_adp : Adapter objejct
889 mraid_mm_register_adp(mraid_mmadp_t *lld_adp)
891 mraid_mmadp_t *adapter;
892 mbox64_t *mbox_list;
893 uioc_t *kioc;
894 uint32_t rval;
895 int i;
898 if (lld_adp->drvr_type != DRVRTYPE_MBOX)
899 return (-EINVAL);
901 adapter = kzalloc(sizeof(mraid_mmadp_t), GFP_KERNEL);
903 if (!adapter)
904 return -ENOMEM;
907 adapter->unique_id = lld_adp->unique_id;
908 adapter->drvr_type = lld_adp->drvr_type;
909 adapter->drvr_data = lld_adp->drvr_data;
910 adapter->pdev = lld_adp->pdev;
911 adapter->issue_uioc = lld_adp->issue_uioc;
912 adapter->timeout = lld_adp->timeout;
913 adapter->max_kioc = lld_adp->max_kioc;
914 adapter->quiescent = 1;
917 * Allocate single blocks of memory for all required kiocs,
918 * mailboxes and passthru structures.
920 adapter->kioc_list = kmalloc(sizeof(uioc_t) * lld_adp->max_kioc,
921 GFP_KERNEL);
922 adapter->mbox_list = kmalloc(sizeof(mbox64_t) * lld_adp->max_kioc,
923 GFP_KERNEL);
924 adapter->pthru_dma_pool = pci_pool_create("megaraid mm pthru pool",
925 adapter->pdev,
926 sizeof(mraid_passthru_t),
927 16, 0);
929 if (!adapter->kioc_list || !adapter->mbox_list ||
930 !adapter->pthru_dma_pool) {
932 con_log(CL_ANN, (KERN_WARNING
933 "megaraid cmm: out of memory, %s %d\n", __func__,
934 __LINE__));
936 rval = (-ENOMEM);
938 goto memalloc_error;
942 * Slice kioc_list and make a kioc_pool with the individiual kiocs
944 INIT_LIST_HEAD(&adapter->kioc_pool);
945 spin_lock_init(&adapter->kioc_pool_lock);
946 sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
948 mbox_list = (mbox64_t *)adapter->mbox_list;
950 for (i = 0; i < lld_adp->max_kioc; i++) {
952 kioc = adapter->kioc_list + i;
953 kioc->cmdbuf = (uint64_t)(unsigned long)(mbox_list + i);
954 kioc->pthru32 = pci_pool_alloc(adapter->pthru_dma_pool,
955 GFP_KERNEL, &kioc->pthru32_h);
957 if (!kioc->pthru32) {
959 con_log(CL_ANN, (KERN_WARNING
960 "megaraid cmm: out of memory, %s %d\n",
961 __func__, __LINE__));
963 rval = (-ENOMEM);
965 goto pthru_dma_pool_error;
968 list_add_tail(&kioc->list, &adapter->kioc_pool);
971 // Setup the dma pools for data buffers
972 if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
973 goto dma_pool_error;
976 list_add_tail(&adapter->list, &adapters_list_g);
978 adapters_count_g++;
980 return 0;
982 dma_pool_error:
983 /* Do nothing */
985 pthru_dma_pool_error:
987 for (i = 0; i < lld_adp->max_kioc; i++) {
988 kioc = adapter->kioc_list + i;
989 if (kioc->pthru32) {
990 pci_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
991 kioc->pthru32_h);
995 memalloc_error:
997 kfree(adapter->kioc_list);
998 kfree(adapter->mbox_list);
1000 if (adapter->pthru_dma_pool)
1001 pci_pool_destroy(adapter->pthru_dma_pool);
1003 kfree(adapter);
1005 return rval;
1010 * mraid_mm_adapter_app_handle - return the application handle for this adapter
1011 * @unique_id : adapter unique identifier
1013 * For the given driver data, locate the adapter in our global list and
1014 * return the corresponding handle, which is also used by applications to
1015 * uniquely identify an adapter.
1017 * Return adapter handle if found in the list.
1018 * Return 0 if adapter could not be located, should never happen though.
1020 uint32_t
1021 mraid_mm_adapter_app_handle(uint32_t unique_id)
1023 mraid_mmadp_t *adapter;
1024 mraid_mmadp_t *tmp;
1025 int index = 0;
1027 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1029 if (adapter->unique_id == unique_id) {
1031 return MKADAP(index);
1034 index++;
1037 return 0;
1042 * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
1043 * @adp : Adapter softstate
1045 * We maintain a pool of dma buffers per each adapter. Each pool has one
1046 * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
1047 * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
1048 * dont' want to waste too much memory by allocating more buffers per each
1049 * pool.
1051 static int
1052 mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
1054 mm_dmapool_t *pool;
1055 int bufsize;
1056 int i;
1059 * Create MAX_DMA_POOLS number of pools
1061 bufsize = MRAID_MM_INIT_BUFF_SIZE;
1063 for (i = 0; i < MAX_DMA_POOLS; i++){
1065 pool = &adp->dma_pool_list[i];
1067 pool->buf_size = bufsize;
1068 spin_lock_init(&pool->lock);
1070 pool->handle = pci_pool_create("megaraid mm data buffer",
1071 adp->pdev, bufsize, 16, 0);
1073 if (!pool->handle) {
1074 goto dma_pool_setup_error;
1077 pool->vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
1078 &pool->paddr);
1080 if (!pool->vaddr)
1081 goto dma_pool_setup_error;
1083 bufsize = bufsize * 2;
1086 return 0;
1088 dma_pool_setup_error:
1090 mraid_mm_teardown_dma_pools(adp);
1091 return (-ENOMEM);
1096 * mraid_mm_unregister_adp - Unregister routine for low level drivers
1097 * @unique_id : UID of the adpater
1099 * Assumes no outstanding ioctls to llds.
1102 mraid_mm_unregister_adp(uint32_t unique_id)
1104 mraid_mmadp_t *adapter;
1105 mraid_mmadp_t *tmp;
1107 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1110 if (adapter->unique_id == unique_id) {
1112 adapters_count_g--;
1114 list_del_init(&adapter->list);
1116 mraid_mm_free_adp_resources(adapter);
1118 kfree(adapter);
1120 con_log(CL_ANN, (
1121 "megaraid cmm: Unregistered one adapter:%#x\n",
1122 unique_id));
1124 return 0;
1128 return (-ENODEV);
1132 * mraid_mm_free_adp_resources - Free adapter softstate
1133 * @adp : Adapter softstate
1135 static void
1136 mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
1138 uioc_t *kioc;
1139 int i;
1141 mraid_mm_teardown_dma_pools(adp);
1143 for (i = 0; i < adp->max_kioc; i++) {
1145 kioc = adp->kioc_list + i;
1147 pci_pool_free(adp->pthru_dma_pool, kioc->pthru32,
1148 kioc->pthru32_h);
1151 kfree(adp->kioc_list);
1152 kfree(adp->mbox_list);
1154 pci_pool_destroy(adp->pthru_dma_pool);
1157 return;
1162 * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
1163 * @adp : Adapter softstate
1165 static void
1166 mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
1168 int i;
1169 mm_dmapool_t *pool;
1171 for (i = 0; i < MAX_DMA_POOLS; i++) {
1173 pool = &adp->dma_pool_list[i];
1175 if (pool->handle) {
1177 if (pool->vaddr)
1178 pci_pool_free(pool->handle, pool->vaddr,
1179 pool->paddr);
1181 pci_pool_destroy(pool->handle);
1182 pool->handle = NULL;
1186 return;
1190 * mraid_mm_init - Module entry point
1192 static int __init
1193 mraid_mm_init(void)
1195 int err;
1197 // Announce the driver version
1198 con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
1199 LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
1201 err = misc_register(&megaraid_mm_dev);
1202 if (err < 0) {
1203 con_log(CL_ANN, ("megaraid cmm: cannot register misc device\n"));
1204 return err;
1207 init_waitqueue_head(&wait_q);
1209 INIT_LIST_HEAD(&adapters_list_g);
1211 return 0;
1215 #ifdef CONFIG_COMPAT
1217 * mraid_mm_compat_ioctl - 32bit to 64bit ioctl conversion routine
1218 * @filep : file operations pointer (ignored)
1219 * @cmd : ioctl command
1220 * @arg : user ioctl packet
1222 static long
1223 mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd,
1224 unsigned long arg)
1226 int err;
1228 err = mraid_mm_ioctl(NULL, filep, cmd, arg);
1230 return err;
1232 #endif
1235 * mraid_mm_exit - Module exit point
1237 static void __exit
1238 mraid_mm_exit(void)
1240 con_log(CL_DLEVEL1 , ("exiting common mod\n"));
1242 misc_deregister(&megaraid_mm_dev);
1245 module_init(mraid_mm_init);
1246 module_exit(mraid_mm_exit);
1248 /* vi: set ts=8 sw=8 tw=78: */