thinkpad-acpi: drop HKEY event 0x5010
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / megaraid / megaraid_mm.c
blobf680561d2c6f6101bac31fb4ad5ef7dcf2ded20c
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/smp_lock.h>
19 #include "megaraid_mm.h"
22 // Entry points for char node driver
23 static int mraid_mm_open(struct inode *, struct file *);
24 static int mraid_mm_ioctl(struct inode *, struct file *, uint, unsigned long);
27 // routines to convert to and from the old the format
28 static int mimd_to_kioc(mimd_t __user *, mraid_mmadp_t *, uioc_t *);
29 static int kioc_to_mimd(uioc_t *, mimd_t __user *);
32 // Helper functions
33 static int handle_drvrcmd(void __user *, uint8_t, int *);
34 static int lld_ioctl(mraid_mmadp_t *, uioc_t *);
35 static void ioctl_done(uioc_t *);
36 static void lld_timedout(unsigned long);
37 static void hinfo_to_cinfo(mraid_hba_info_t *, mcontroller_t *);
38 static mraid_mmadp_t *mraid_mm_get_adapter(mimd_t __user *, int *);
39 static uioc_t *mraid_mm_alloc_kioc(mraid_mmadp_t *);
40 static void mraid_mm_dealloc_kioc(mraid_mmadp_t *, uioc_t *);
41 static int mraid_mm_attach_buf(mraid_mmadp_t *, uioc_t *, int);
42 static int mraid_mm_setup_dma_pools(mraid_mmadp_t *);
43 static void mraid_mm_free_adp_resources(mraid_mmadp_t *);
44 static void mraid_mm_teardown_dma_pools(mraid_mmadp_t *);
46 #ifdef CONFIG_COMPAT
47 static long mraid_mm_compat_ioctl(struct file *, unsigned int, unsigned long);
48 #endif
50 MODULE_AUTHOR("LSI Logic Corporation");
51 MODULE_DESCRIPTION("LSI Logic Management Module");
52 MODULE_LICENSE("GPL");
53 MODULE_VERSION(LSI_COMMON_MOD_VERSION);
55 static int dbglevel = CL_ANN;
56 module_param_named(dlevel, dbglevel, int, 0);
57 MODULE_PARM_DESC(dlevel, "Debug level (default=0)");
59 EXPORT_SYMBOL(mraid_mm_register_adp);
60 EXPORT_SYMBOL(mraid_mm_unregister_adp);
61 EXPORT_SYMBOL(mraid_mm_adapter_app_handle);
63 static uint32_t drvr_ver = 0x02200207;
65 static int adapters_count_g;
66 static struct list_head adapters_list_g;
68 static wait_queue_head_t wait_q;
70 static const struct file_operations lsi_fops = {
71 .open = mraid_mm_open,
72 .ioctl = mraid_mm_ioctl,
73 #ifdef CONFIG_COMPAT
74 .compat_ioctl = mraid_mm_compat_ioctl,
75 #endif
76 .owner = THIS_MODULE,
79 static struct miscdevice megaraid_mm_dev = {
80 .minor = MISC_DYNAMIC_MINOR,
81 .name = "megadev0",
82 .fops = &lsi_fops,
85 /**
86 * mraid_mm_open - open routine for char node interface
87 * @inode : unused
88 * @filep : unused
90 * Allow ioctl operations by apps only if they have superuser privilege.
92 static int
93 mraid_mm_open(struct inode *inode, struct file *filep)
96 * Only allow superuser to access private ioctl interface
98 if (!capable(CAP_SYS_ADMIN)) return (-EACCES);
100 cycle_kernel_lock();
101 return 0;
105 * mraid_mm_ioctl - module entry-point for ioctls
106 * @inode : inode (ignored)
107 * @filep : file operations pointer (ignored)
108 * @cmd : ioctl command
109 * @arg : user ioctl packet
111 static int
112 mraid_mm_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
113 unsigned long arg)
115 uioc_t *kioc;
116 char signature[EXT_IOCTL_SIGN_SZ] = {0};
117 int rval;
118 mraid_mmadp_t *adp;
119 uint8_t old_ioctl;
120 int drvrcmd_rval;
121 void __user *argp = (void __user *)arg;
124 * Make sure only USCSICMD are issued through this interface.
125 * MIMD application would still fire different command.
128 if ((_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD)) {
129 return (-EINVAL);
133 * Look for signature to see if this is the new or old ioctl format.
135 if (copy_from_user(signature, argp, EXT_IOCTL_SIGN_SZ)) {
136 con_log(CL_ANN, (KERN_WARNING
137 "megaraid cmm: copy from usr addr failed\n"));
138 return (-EFAULT);
141 if (memcmp(signature, EXT_IOCTL_SIGN, EXT_IOCTL_SIGN_SZ) == 0)
142 old_ioctl = 0;
143 else
144 old_ioctl = 1;
147 * At present, we don't support the new ioctl packet
149 if (!old_ioctl )
150 return (-EINVAL);
153 * If it is a driver ioctl (as opposed to fw ioctls), then we can
154 * handle the command locally. rval > 0 means it is not a drvr cmd
156 rval = handle_drvrcmd(argp, old_ioctl, &drvrcmd_rval);
158 if (rval < 0)
159 return rval;
160 else if (rval == 0)
161 return drvrcmd_rval;
163 rval = 0;
164 if ((adp = mraid_mm_get_adapter(argp, &rval)) == NULL) {
165 return rval;
169 * Check if adapter can accept ioctl. We may have marked it offline
170 * if any previous kioc had timedout on this controller.
172 if (!adp->quiescent) {
173 con_log(CL_ANN, (KERN_WARNING
174 "megaraid cmm: controller cannot accept cmds due to "
175 "earlier errors\n" ));
176 return -EFAULT;
180 * The following call will block till a kioc is available
182 kioc = mraid_mm_alloc_kioc(adp);
185 * User sent the old mimd_t ioctl packet. Convert it to uioc_t.
187 if ((rval = mimd_to_kioc(argp, adp, kioc))) {
188 mraid_mm_dealloc_kioc(adp, kioc);
189 return rval;
192 kioc->done = ioctl_done;
195 * Issue the IOCTL to the low level driver. After the IOCTL completes
196 * release the kioc if and only if it was _not_ timedout. If it was
197 * timedout, that means that resources are still with low level driver.
199 if ((rval = lld_ioctl(adp, kioc))) {
201 if (!kioc->timedout)
202 mraid_mm_dealloc_kioc(adp, kioc);
204 return rval;
208 * Convert the kioc back to user space
210 rval = kioc_to_mimd(kioc, argp);
213 * Return the kioc to free pool
215 mraid_mm_dealloc_kioc(adp, kioc);
217 return rval;
222 * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
223 * @umimd : User space mimd_t ioctl packet
224 * @rval : returned success/error status
226 * The function return value is a pointer to the located @adapter.
228 static mraid_mmadp_t *
229 mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
231 mraid_mmadp_t *adapter;
232 mimd_t mimd;
233 uint32_t adapno;
234 int iterator;
237 if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) {
238 *rval = -EFAULT;
239 return NULL;
242 adapno = GETADAP(mimd.ui.fcs.adapno);
244 if (adapno >= adapters_count_g) {
245 *rval = -ENODEV;
246 return NULL;
249 adapter = NULL;
250 iterator = 0;
252 list_for_each_entry(adapter, &adapters_list_g, list) {
253 if (iterator++ == adapno) break;
256 if (!adapter) {
257 *rval = -ENODEV;
258 return NULL;
261 return adapter;
265 * handle_drvrcmd - Checks if the opcode is a driver cmd and if it is, handles it.
266 * @arg : packet sent by the user app
267 * @old_ioctl : mimd if 1; uioc otherwise
268 * @rval : pointer for command's returned value (not function status)
270 static int
271 handle_drvrcmd(void __user *arg, uint8_t old_ioctl, int *rval)
273 mimd_t __user *umimd;
274 mimd_t kmimd;
275 uint8_t opcode;
276 uint8_t subopcode;
278 if (old_ioctl)
279 goto old_packet;
280 else
281 goto new_packet;
283 new_packet:
284 return (-ENOTSUPP);
286 old_packet:
287 *rval = 0;
288 umimd = arg;
290 if (copy_from_user(&kmimd, umimd, sizeof(mimd_t)))
291 return (-EFAULT);
293 opcode = kmimd.ui.fcs.opcode;
294 subopcode = kmimd.ui.fcs.subopcode;
297 * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
298 * GET_NUMADP, then we can handle. Otherwise we should return 1 to
299 * indicate that we cannot handle this.
301 if (opcode != 0x82)
302 return 1;
304 switch (subopcode) {
306 case MEGAIOC_QDRVRVER:
308 if (copy_to_user(kmimd.data, &drvr_ver, sizeof(uint32_t)))
309 return (-EFAULT);
311 return 0;
313 case MEGAIOC_QNADAP:
315 *rval = adapters_count_g;
317 if (copy_to_user(kmimd.data, &adapters_count_g,
318 sizeof(uint32_t)))
319 return (-EFAULT);
321 return 0;
323 default:
324 /* cannot handle */
325 return 1;
328 return 0;
333 * mimd_to_kioc - Converter from old to new ioctl format
334 * @umimd : user space old MIMD IOCTL
335 * @adp : adapter softstate
336 * @kioc : kernel space new format IOCTL
338 * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
339 * new packet is in kernel space so that driver can perform operations on it
340 * freely.
343 static int
344 mimd_to_kioc(mimd_t __user *umimd, mraid_mmadp_t *adp, uioc_t *kioc)
346 mbox64_t *mbox64;
347 mbox_t *mbox;
348 mraid_passthru_t *pthru32;
349 uint32_t adapno;
350 uint8_t opcode;
351 uint8_t subopcode;
352 mimd_t mimd;
354 if (copy_from_user(&mimd, umimd, sizeof(mimd_t)))
355 return (-EFAULT);
358 * Applications are not allowed to send extd pthru
360 if ((mimd.mbox[0] == MBOXCMD_PASSTHRU64) ||
361 (mimd.mbox[0] == MBOXCMD_EXTPTHRU))
362 return (-EINVAL);
364 opcode = mimd.ui.fcs.opcode;
365 subopcode = mimd.ui.fcs.subopcode;
366 adapno = GETADAP(mimd.ui.fcs.adapno);
368 if (adapno >= adapters_count_g)
369 return (-ENODEV);
371 kioc->adapno = adapno;
372 kioc->mb_type = MBOX_LEGACY;
373 kioc->app_type = APPTYPE_MIMD;
375 switch (opcode) {
377 case 0x82:
379 if (subopcode == MEGAIOC_QADAPINFO) {
381 kioc->opcode = GET_ADAP_INFO;
382 kioc->data_dir = UIOC_RD;
383 kioc->xferlen = sizeof(mraid_hba_info_t);
385 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
386 return (-ENOMEM);
388 else {
389 con_log(CL_ANN, (KERN_WARNING
390 "megaraid cmm: Invalid subop\n"));
391 return (-EINVAL);
394 break;
396 case 0x81:
398 kioc->opcode = MBOX_CMD;
399 kioc->xferlen = mimd.ui.fcs.length;
400 kioc->user_data_len = kioc->xferlen;
401 kioc->user_data = mimd.ui.fcs.buffer;
403 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
404 return (-ENOMEM);
406 if (mimd.outlen) kioc->data_dir = UIOC_RD;
407 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
409 break;
411 case 0x80:
413 kioc->opcode = MBOX_CMD;
414 kioc->xferlen = (mimd.outlen > mimd.inlen) ?
415 mimd.outlen : mimd.inlen;
416 kioc->user_data_len = kioc->xferlen;
417 kioc->user_data = mimd.data;
419 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
420 return (-ENOMEM);
422 if (mimd.outlen) kioc->data_dir = UIOC_RD;
423 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
425 break;
427 default:
428 return (-EINVAL);
432 * If driver command, nothing else to do
434 if (opcode == 0x82)
435 return 0;
438 * This is a mailbox cmd; copy the mailbox from mimd
440 mbox64 = (mbox64_t *)((unsigned long)kioc->cmdbuf);
441 mbox = &mbox64->mbox32;
442 memcpy(mbox, mimd.mbox, 14);
444 if (mbox->cmd != MBOXCMD_PASSTHRU) { // regular DCMD
446 mbox->xferaddr = (uint32_t)kioc->buf_paddr;
448 if (kioc->data_dir & UIOC_WR) {
449 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
450 kioc->xferlen)) {
451 return (-EFAULT);
455 return 0;
459 * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
460 * Just like in above case, the beginning for memblk is treated as
461 * a mailbox. The passthru will begin at next 1K boundary. And the
462 * data will start 1K after that.
464 pthru32 = kioc->pthru32;
465 kioc->user_pthru = &umimd->pthru;
466 mbox->xferaddr = (uint32_t)kioc->pthru32_h;
468 if (copy_from_user(pthru32, kioc->user_pthru,
469 sizeof(mraid_passthru_t))) {
470 return (-EFAULT);
473 pthru32->dataxferaddr = kioc->buf_paddr;
474 if (kioc->data_dir & UIOC_WR) {
475 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
476 pthru32->dataxferlen)) {
477 return (-EFAULT);
481 return 0;
485 * mraid_mm_attch_buf - Attach a free dma buffer for required size
486 * @adp : Adapter softstate
487 * @kioc : kioc that the buffer needs to be attached to
488 * @xferlen : required length for buffer
490 * First we search for a pool with smallest buffer that is >= @xferlen. If
491 * that pool has no free buffer, we will try for the next bigger size. If none
492 * is available, we will try to allocate the smallest buffer that is >=
493 * @xferlen and attach it the pool.
495 static int
496 mraid_mm_attach_buf(mraid_mmadp_t *adp, uioc_t *kioc, int xferlen)
498 mm_dmapool_t *pool;
499 int right_pool = -1;
500 unsigned long flags;
501 int i;
503 kioc->pool_index = -1;
504 kioc->buf_vaddr = NULL;
505 kioc->buf_paddr = 0;
506 kioc->free_buf = 0;
509 * We need xferlen amount of memory. See if we can get it from our
510 * dma pools. If we don't get exact size, we will try bigger buffer
513 for (i = 0; i < MAX_DMA_POOLS; i++) {
515 pool = &adp->dma_pool_list[i];
517 if (xferlen > pool->buf_size)
518 continue;
520 if (right_pool == -1)
521 right_pool = i;
523 spin_lock_irqsave(&pool->lock, flags);
525 if (!pool->in_use) {
527 pool->in_use = 1;
528 kioc->pool_index = i;
529 kioc->buf_vaddr = pool->vaddr;
530 kioc->buf_paddr = pool->paddr;
532 spin_unlock_irqrestore(&pool->lock, flags);
533 return 0;
535 else {
536 spin_unlock_irqrestore(&pool->lock, flags);
537 continue;
542 * If xferlen doesn't match any of our pools, return error
544 if (right_pool == -1)
545 return -EINVAL;
548 * We did not get any buffer from the preallocated pool. Let us try
549 * to allocate one new buffer. NOTE: This is a blocking call.
551 pool = &adp->dma_pool_list[right_pool];
553 spin_lock_irqsave(&pool->lock, flags);
555 kioc->pool_index = right_pool;
556 kioc->free_buf = 1;
557 kioc->buf_vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
558 &kioc->buf_paddr);
559 spin_unlock_irqrestore(&pool->lock, flags);
561 if (!kioc->buf_vaddr)
562 return -ENOMEM;
564 return 0;
568 * mraid_mm_alloc_kioc - Returns a uioc_t from free list
569 * @adp : Adapter softstate for this module
571 * The kioc_semaphore is initialized with number of kioc nodes in the
572 * free kioc pool. If the kioc pool is empty, this function blocks till
573 * a kioc becomes free.
575 static uioc_t *
576 mraid_mm_alloc_kioc(mraid_mmadp_t *adp)
578 uioc_t *kioc;
579 struct list_head* head;
580 unsigned long flags;
582 down(&adp->kioc_semaphore);
584 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
586 head = &adp->kioc_pool;
588 if (list_empty(head)) {
589 up(&adp->kioc_semaphore);
590 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
592 con_log(CL_ANN, ("megaraid cmm: kioc list empty!\n"));
593 return NULL;
596 kioc = list_entry(head->next, uioc_t, list);
597 list_del_init(&kioc->list);
599 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
601 memset((caddr_t)(unsigned long)kioc->cmdbuf, 0, sizeof(mbox64_t));
602 memset((caddr_t) kioc->pthru32, 0, sizeof(mraid_passthru_t));
604 kioc->buf_vaddr = NULL;
605 kioc->buf_paddr = 0;
606 kioc->pool_index =-1;
607 kioc->free_buf = 0;
608 kioc->user_data = NULL;
609 kioc->user_data_len = 0;
610 kioc->user_pthru = NULL;
611 kioc->timedout = 0;
613 return kioc;
617 * mraid_mm_dealloc_kioc - Return kioc to free pool
618 * @adp : Adapter softstate
619 * @kioc : uioc_t node to be returned to free pool
621 static void
622 mraid_mm_dealloc_kioc(mraid_mmadp_t *adp, uioc_t *kioc)
624 mm_dmapool_t *pool;
625 unsigned long flags;
627 if (kioc->pool_index != -1) {
628 pool = &adp->dma_pool_list[kioc->pool_index];
630 /* This routine may be called in non-isr context also */
631 spin_lock_irqsave(&pool->lock, flags);
634 * While attaching the dma buffer, if we didn't get the
635 * required buffer from the pool, we would have allocated
636 * it at the run time and set the free_buf flag. We must
637 * free that buffer. Otherwise, just mark that the buffer is
638 * not in use
640 if (kioc->free_buf == 1)
641 pci_pool_free(pool->handle, kioc->buf_vaddr,
642 kioc->buf_paddr);
643 else
644 pool->in_use = 0;
646 spin_unlock_irqrestore(&pool->lock, flags);
649 /* Return the kioc to the free pool */
650 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
651 list_add(&kioc->list, &adp->kioc_pool);
652 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
654 /* increment the free kioc count */
655 up(&adp->kioc_semaphore);
657 return;
661 * lld_ioctl - Routine to issue ioctl to low level drvr
662 * @adp : The adapter handle
663 * @kioc : The ioctl packet with kernel addresses
665 static int
666 lld_ioctl(mraid_mmadp_t *adp, uioc_t *kioc)
668 int rval;
669 struct timer_list timer;
670 struct timer_list *tp = NULL;
672 kioc->status = -ENODATA;
673 rval = adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE);
675 if (rval) return rval;
678 * Start the timer
680 if (adp->timeout > 0) {
681 tp = &timer;
682 init_timer(tp);
684 tp->function = lld_timedout;
685 tp->data = (unsigned long)kioc;
686 tp->expires = jiffies + adp->timeout * HZ;
688 add_timer(tp);
692 * Wait till the low level driver completes the ioctl. After this
693 * call, the ioctl either completed successfully or timedout.
695 wait_event(wait_q, (kioc->status != -ENODATA));
696 if (tp) {
697 del_timer_sync(tp);
701 * If the command had timedout, we mark the controller offline
702 * before returning
704 if (kioc->timedout) {
705 adp->quiescent = 0;
708 return kioc->status;
713 * ioctl_done - callback from the low level driver
714 * @kioc : completed ioctl packet
716 static void
717 ioctl_done(uioc_t *kioc)
719 uint32_t adapno;
720 int iterator;
721 mraid_mmadp_t* adapter;
724 * When the kioc returns from driver, make sure it still doesn't
725 * have ENODATA in status. Otherwise, driver will hang on wait_event
726 * forever
728 if (kioc->status == -ENODATA) {
729 con_log(CL_ANN, (KERN_WARNING
730 "megaraid cmm: lld didn't change status!\n"));
732 kioc->status = -EINVAL;
736 * Check if this kioc was timedout before. If so, nobody is waiting
737 * on this kioc. We don't have to wake up anybody. Instead, we just
738 * have to free the kioc
740 if (kioc->timedout) {
741 iterator = 0;
742 adapter = NULL;
743 adapno = kioc->adapno;
745 con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
746 "ioctl that was timedout before\n"));
748 list_for_each_entry(adapter, &adapters_list_g, list) {
749 if (iterator++ == adapno) break;
752 kioc->timedout = 0;
754 if (adapter) {
755 mraid_mm_dealloc_kioc( adapter, kioc );
758 else {
759 wake_up(&wait_q);
765 * lld_timedout - callback from the expired timer
766 * @ptr : ioctl packet that timed out
768 static void
769 lld_timedout(unsigned long ptr)
771 uioc_t *kioc = (uioc_t *)ptr;
773 kioc->status = -ETIME;
774 kioc->timedout = 1;
776 con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n"));
778 wake_up(&wait_q);
783 * kioc_to_mimd - Converter from new back to old format
784 * @kioc : Kernel space IOCTL packet (successfully issued)
785 * @mimd : User space MIMD packet
787 static int
788 kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd)
790 mimd_t kmimd;
791 uint8_t opcode;
792 uint8_t subopcode;
794 mbox64_t *mbox64;
795 mraid_passthru_t __user *upthru32;
796 mraid_passthru_t *kpthru32;
797 mcontroller_t cinfo;
798 mraid_hba_info_t *hinfo;
801 if (copy_from_user(&kmimd, mimd, sizeof(mimd_t)))
802 return (-EFAULT);
804 opcode = kmimd.ui.fcs.opcode;
805 subopcode = kmimd.ui.fcs.subopcode;
807 if (opcode == 0x82) {
808 switch (subopcode) {
810 case MEGAIOC_QADAPINFO:
812 hinfo = (mraid_hba_info_t *)(unsigned long)
813 kioc->buf_vaddr;
815 hinfo_to_cinfo(hinfo, &cinfo);
817 if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo)))
818 return (-EFAULT);
820 return 0;
822 default:
823 return (-EINVAL);
826 return 0;
829 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
831 if (kioc->user_pthru) {
833 upthru32 = kioc->user_pthru;
834 kpthru32 = kioc->pthru32;
836 if (copy_to_user(&upthru32->scsistatus,
837 &kpthru32->scsistatus,
838 sizeof(uint8_t))) {
839 return (-EFAULT);
843 if (kioc->user_data) {
844 if (copy_to_user(kioc->user_data, kioc->buf_vaddr,
845 kioc->user_data_len)) {
846 return (-EFAULT);
850 if (copy_to_user(&mimd->mbox[17],
851 &mbox64->mbox32.status, sizeof(uint8_t))) {
852 return (-EFAULT);
855 return 0;
860 * hinfo_to_cinfo - Convert new format hba info into old format
861 * @hinfo : New format, more comprehensive adapter info
862 * @cinfo : Old format adapter info to support mimd_t apps
864 static void
865 hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo)
867 if (!hinfo || !cinfo)
868 return;
870 cinfo->base = hinfo->baseport;
871 cinfo->irq = hinfo->irq;
872 cinfo->numldrv = hinfo->num_ldrv;
873 cinfo->pcibus = hinfo->pci_bus;
874 cinfo->pcidev = hinfo->pci_slot;
875 cinfo->pcifun = PCI_FUNC(hinfo->pci_dev_fn);
876 cinfo->pciid = hinfo->pci_device_id;
877 cinfo->pcivendor = hinfo->pci_vendor_id;
878 cinfo->pcislot = hinfo->pci_slot;
879 cinfo->uid = hinfo->unique_id;
884 * mraid_mm_register_adp - Registration routine for low level drivers
885 * @lld_adp : Adapter objejct
888 mraid_mm_register_adp(mraid_mmadp_t *lld_adp)
890 mraid_mmadp_t *adapter;
891 mbox64_t *mbox_list;
892 uioc_t *kioc;
893 uint32_t rval;
894 int i;
897 if (lld_adp->drvr_type != DRVRTYPE_MBOX)
898 return (-EINVAL);
900 adapter = kzalloc(sizeof(mraid_mmadp_t), GFP_KERNEL);
902 if (!adapter)
903 return -ENOMEM;
906 adapter->unique_id = lld_adp->unique_id;
907 adapter->drvr_type = lld_adp->drvr_type;
908 adapter->drvr_data = lld_adp->drvr_data;
909 adapter->pdev = lld_adp->pdev;
910 adapter->issue_uioc = lld_adp->issue_uioc;
911 adapter->timeout = lld_adp->timeout;
912 adapter->max_kioc = lld_adp->max_kioc;
913 adapter->quiescent = 1;
916 * Allocate single blocks of memory for all required kiocs,
917 * mailboxes and passthru structures.
919 adapter->kioc_list = kmalloc(sizeof(uioc_t) * lld_adp->max_kioc,
920 GFP_KERNEL);
921 adapter->mbox_list = kmalloc(sizeof(mbox64_t) * lld_adp->max_kioc,
922 GFP_KERNEL);
923 adapter->pthru_dma_pool = pci_pool_create("megaraid mm pthru pool",
924 adapter->pdev,
925 sizeof(mraid_passthru_t),
926 16, 0);
928 if (!adapter->kioc_list || !adapter->mbox_list ||
929 !adapter->pthru_dma_pool) {
931 con_log(CL_ANN, (KERN_WARNING
932 "megaraid cmm: out of memory, %s %d\n", __func__,
933 __LINE__));
935 rval = (-ENOMEM);
937 goto memalloc_error;
941 * Slice kioc_list and make a kioc_pool with the individiual kiocs
943 INIT_LIST_HEAD(&adapter->kioc_pool);
944 spin_lock_init(&adapter->kioc_pool_lock);
945 sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
947 mbox_list = (mbox64_t *)adapter->mbox_list;
949 for (i = 0; i < lld_adp->max_kioc; i++) {
951 kioc = adapter->kioc_list + i;
952 kioc->cmdbuf = (uint64_t)(unsigned long)(mbox_list + i);
953 kioc->pthru32 = pci_pool_alloc(adapter->pthru_dma_pool,
954 GFP_KERNEL, &kioc->pthru32_h);
956 if (!kioc->pthru32) {
958 con_log(CL_ANN, (KERN_WARNING
959 "megaraid cmm: out of memory, %s %d\n",
960 __func__, __LINE__));
962 rval = (-ENOMEM);
964 goto pthru_dma_pool_error;
967 list_add_tail(&kioc->list, &adapter->kioc_pool);
970 // Setup the dma pools for data buffers
971 if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
972 goto dma_pool_error;
975 list_add_tail(&adapter->list, &adapters_list_g);
977 adapters_count_g++;
979 return 0;
981 dma_pool_error:
982 /* Do nothing */
984 pthru_dma_pool_error:
986 for (i = 0; i < lld_adp->max_kioc; i++) {
987 kioc = adapter->kioc_list + i;
988 if (kioc->pthru32) {
989 pci_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
990 kioc->pthru32_h);
994 memalloc_error:
996 kfree(adapter->kioc_list);
997 kfree(adapter->mbox_list);
999 if (adapter->pthru_dma_pool)
1000 pci_pool_destroy(adapter->pthru_dma_pool);
1002 kfree(adapter);
1004 return rval;
1009 * mraid_mm_adapter_app_handle - return the application handle for this adapter
1010 * @unique_id : adapter unique identifier
1012 * For the given driver data, locate the adapter in our global list and
1013 * return the corresponding handle, which is also used by applications to
1014 * uniquely identify an adapter.
1016 * Return adapter handle if found in the list.
1017 * Return 0 if adapter could not be located, should never happen though.
1019 uint32_t
1020 mraid_mm_adapter_app_handle(uint32_t unique_id)
1022 mraid_mmadp_t *adapter;
1023 mraid_mmadp_t *tmp;
1024 int index = 0;
1026 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1028 if (adapter->unique_id == unique_id) {
1030 return MKADAP(index);
1033 index++;
1036 return 0;
1041 * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
1042 * @adp : Adapter softstate
1044 * We maintain a pool of dma buffers per each adapter. Each pool has one
1045 * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
1046 * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
1047 * dont' want to waste too much memory by allocating more buffers per each
1048 * pool.
1050 static int
1051 mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
1053 mm_dmapool_t *pool;
1054 int bufsize;
1055 int i;
1058 * Create MAX_DMA_POOLS number of pools
1060 bufsize = MRAID_MM_INIT_BUFF_SIZE;
1062 for (i = 0; i < MAX_DMA_POOLS; i++){
1064 pool = &adp->dma_pool_list[i];
1066 pool->buf_size = bufsize;
1067 spin_lock_init(&pool->lock);
1069 pool->handle = pci_pool_create("megaraid mm data buffer",
1070 adp->pdev, bufsize, 16, 0);
1072 if (!pool->handle) {
1073 goto dma_pool_setup_error;
1076 pool->vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
1077 &pool->paddr);
1079 if (!pool->vaddr)
1080 goto dma_pool_setup_error;
1082 bufsize = bufsize * 2;
1085 return 0;
1087 dma_pool_setup_error:
1089 mraid_mm_teardown_dma_pools(adp);
1090 return (-ENOMEM);
1095 * mraid_mm_unregister_adp - Unregister routine for low level drivers
1096 * @unique_id : UID of the adpater
1098 * Assumes no outstanding ioctls to llds.
1101 mraid_mm_unregister_adp(uint32_t unique_id)
1103 mraid_mmadp_t *adapter;
1104 mraid_mmadp_t *tmp;
1106 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1109 if (adapter->unique_id == unique_id) {
1111 adapters_count_g--;
1113 list_del_init(&adapter->list);
1115 mraid_mm_free_adp_resources(adapter);
1117 kfree(adapter);
1119 con_log(CL_ANN, (
1120 "megaraid cmm: Unregistered one adapter:%#x\n",
1121 unique_id));
1123 return 0;
1127 return (-ENODEV);
1131 * mraid_mm_free_adp_resources - Free adapter softstate
1132 * @adp : Adapter softstate
1134 static void
1135 mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
1137 uioc_t *kioc;
1138 int i;
1140 mraid_mm_teardown_dma_pools(adp);
1142 for (i = 0; i < adp->max_kioc; i++) {
1144 kioc = adp->kioc_list + i;
1146 pci_pool_free(adp->pthru_dma_pool, kioc->pthru32,
1147 kioc->pthru32_h);
1150 kfree(adp->kioc_list);
1151 kfree(adp->mbox_list);
1153 pci_pool_destroy(adp->pthru_dma_pool);
1156 return;
1161 * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
1162 * @adp : Adapter softstate
1164 static void
1165 mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
1167 int i;
1168 mm_dmapool_t *pool;
1170 for (i = 0; i < MAX_DMA_POOLS; i++) {
1172 pool = &adp->dma_pool_list[i];
1174 if (pool->handle) {
1176 if (pool->vaddr)
1177 pci_pool_free(pool->handle, pool->vaddr,
1178 pool->paddr);
1180 pci_pool_destroy(pool->handle);
1181 pool->handle = NULL;
1185 return;
1189 * mraid_mm_init - Module entry point
1191 static int __init
1192 mraid_mm_init(void)
1194 int err;
1196 // Announce the driver version
1197 con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
1198 LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
1200 err = misc_register(&megaraid_mm_dev);
1201 if (err < 0) {
1202 con_log(CL_ANN, ("megaraid cmm: cannot register misc device\n"));
1203 return err;
1206 init_waitqueue_head(&wait_q);
1208 INIT_LIST_HEAD(&adapters_list_g);
1210 return 0;
1214 #ifdef CONFIG_COMPAT
1216 * mraid_mm_compat_ioctl - 32bit to 64bit ioctl conversion routine
1217 * @filep : file operations pointer (ignored)
1218 * @cmd : ioctl command
1219 * @arg : user ioctl packet
1221 static long
1222 mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd,
1223 unsigned long arg)
1225 int err;
1227 err = mraid_mm_ioctl(NULL, filep, cmd, arg);
1229 return err;
1231 #endif
1234 * mraid_mm_exit - Module exit point
1236 static void __exit
1237 mraid_mm_exit(void)
1239 con_log(CL_DLEVEL1 , ("exiting common mod\n"));
1241 misc_deregister(&megaraid_mm_dev);
1244 module_init(mraid_mm_init);
1245 module_exit(mraid_mm_exit);
1247 /* vi: set ts=8 sw=8 tw=78: */