Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / megaraid / megaraid_mm.c
blob9f1b550713ecd12a7c5b5977280c7eff1606a7db
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.5 (Jan 21 2005)
15 * Common management module
18 #include "megaraid_mm.h"
19 #include <linux/smp_lock.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 int majorno;
64 static uint32_t drvr_ver = 0x02200201;
66 static int adapters_count_g;
67 static struct list_head adapters_list_g;
69 static wait_queue_head_t wait_q;
71 static 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 /**
81 * mraid_mm_open - open routine for char node interface
82 * @inod : unused
83 * @filep : unused
85 * allow ioctl operations by apps only if they superuser privilege
87 static int
88 mraid_mm_open(struct inode *inode, struct file *filep)
91 * Only allow superuser to access private ioctl interface
93 if (!capable(CAP_SYS_ADMIN)) return (-EACCES);
95 return 0;
98 /**
99 * mraid_mm_ioctl - module entry-point for ioctls
100 * @inode : inode (ignored)
101 * @filep : file operations pointer (ignored)
102 * @cmd : ioctl command
103 * @arg : user ioctl packet
105 static int
106 mraid_mm_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
107 unsigned long arg)
109 uioc_t *kioc;
110 char signature[EXT_IOCTL_SIGN_SZ] = {0};
111 int rval;
112 mraid_mmadp_t *adp;
113 uint8_t old_ioctl;
114 int drvrcmd_rval;
115 void __user *argp = (void __user *)arg;
118 * Make sure only USCSICMD are issued through this interface.
119 * MIMD application would still fire different command.
122 if ((_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD)) {
123 return (-EINVAL);
127 * Look for signature to see if this is the new or old ioctl format.
129 if (copy_from_user(signature, argp, EXT_IOCTL_SIGN_SZ)) {
130 con_log(CL_ANN, (KERN_WARNING
131 "megaraid cmm: copy from usr addr failed\n"));
132 return (-EFAULT);
135 if (memcmp(signature, EXT_IOCTL_SIGN, EXT_IOCTL_SIGN_SZ) == 0)
136 old_ioctl = 0;
137 else
138 old_ioctl = 1;
141 * At present, we don't support the new ioctl packet
143 if (!old_ioctl )
144 return (-EINVAL);
147 * If it is a driver ioctl (as opposed to fw ioctls), then we can
148 * handle the command locally. rval > 0 means it is not a drvr cmd
150 rval = handle_drvrcmd(argp, old_ioctl, &drvrcmd_rval);
152 if (rval < 0)
153 return rval;
154 else if (rval == 0)
155 return drvrcmd_rval;
157 rval = 0;
158 if ((adp = mraid_mm_get_adapter(argp, &rval)) == NULL) {
159 return rval;
163 * Check if adapter can accept ioctl. We may have marked it offline
164 * if any previous kioc had timedout on this controller.
166 if (!adp->quiescent) {
167 con_log(CL_ANN, (KERN_WARNING
168 "megaraid cmm: controller cannot accept cmds due to "
169 "earlier errors\n" ));
170 return -EFAULT;
174 * The following call will block till a kioc is available
176 kioc = mraid_mm_alloc_kioc(adp);
179 * User sent the old mimd_t ioctl packet. Convert it to uioc_t.
181 if ((rval = mimd_to_kioc(argp, adp, kioc))) {
182 mraid_mm_dealloc_kioc(adp, kioc);
183 return rval;
186 kioc->done = ioctl_done;
189 * Issue the IOCTL to the low level driver. After the IOCTL completes
190 * release the kioc if and only if it was _not_ timedout. If it was
191 * timedout, that means that resources are still with low level driver.
193 if ((rval = lld_ioctl(adp, kioc))) {
195 if (!kioc->timedout)
196 mraid_mm_dealloc_kioc(adp, kioc);
198 return rval;
202 * Convert the kioc back to user space
204 rval = kioc_to_mimd(kioc, argp);
207 * Return the kioc to free pool
209 mraid_mm_dealloc_kioc(adp, kioc);
211 return rval;
216 * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
217 * @umimd : User space mimd_t ioctl packet
218 * @adapter : pointer to the adapter (OUT)
220 static mraid_mmadp_t *
221 mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
223 mraid_mmadp_t *adapter;
224 mimd_t mimd;
225 uint32_t adapno;
226 int iterator;
229 if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) {
230 *rval = -EFAULT;
231 return NULL;
234 adapno = GETADAP(mimd.ui.fcs.adapno);
236 if (adapno >= adapters_count_g) {
237 *rval = -ENODEV;
238 return NULL;
241 adapter = NULL;
242 iterator = 0;
244 list_for_each_entry(adapter, &adapters_list_g, list) {
245 if (iterator++ == adapno) break;
248 if (!adapter) {
249 *rval = -ENODEV;
250 return NULL;
253 return adapter;
257 * handle_drvrcmd - This routine checks if the opcode is a driver
258 * cmd and if it is, handles it.
259 * @arg : packet sent by the user app
260 * @old_ioctl : mimd if 1; uioc otherwise
262 static int
263 handle_drvrcmd(void __user *arg, uint8_t old_ioctl, int *rval)
265 mimd_t __user *umimd;
266 mimd_t kmimd;
267 uint8_t opcode;
268 uint8_t subopcode;
270 if (old_ioctl)
271 goto old_packet;
272 else
273 goto new_packet;
275 new_packet:
276 return (-ENOTSUPP);
278 old_packet:
279 *rval = 0;
280 umimd = arg;
282 if (copy_from_user(&kmimd, umimd, sizeof(mimd_t)))
283 return (-EFAULT);
285 opcode = kmimd.ui.fcs.opcode;
286 subopcode = kmimd.ui.fcs.subopcode;
289 * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
290 * GET_NUMADP, then we can handle. Otherwise we should return 1 to
291 * indicate that we cannot handle this.
293 if (opcode != 0x82)
294 return 1;
296 switch (subopcode) {
298 case MEGAIOC_QDRVRVER:
300 if (copy_to_user(kmimd.data, &drvr_ver, sizeof(uint32_t)))
301 return (-EFAULT);
303 return 0;
305 case MEGAIOC_QNADAP:
307 *rval = adapters_count_g;
309 if (copy_to_user(kmimd.data, &adapters_count_g,
310 sizeof(uint32_t)))
311 return (-EFAULT);
313 return 0;
315 default:
316 /* cannot handle */
317 return 1;
320 return 0;
325 * mimd_to_kioc - Converter from old to new ioctl format
327 * @umimd : user space old MIMD IOCTL
328 * @kioc : kernel space new format IOCTL
330 * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
331 * new packet is in kernel space so that driver can perform operations on it
332 * freely.
335 static int
336 mimd_to_kioc(mimd_t __user *umimd, mraid_mmadp_t *adp, uioc_t *kioc)
338 mbox64_t *mbox64;
339 mbox_t *mbox;
340 mraid_passthru_t *pthru32;
341 uint32_t adapno;
342 uint8_t opcode;
343 uint8_t subopcode;
344 mimd_t mimd;
346 if (copy_from_user(&mimd, umimd, sizeof(mimd_t)))
347 return (-EFAULT);
350 * Applications are not allowed to send extd pthru
352 if ((mimd.mbox[0] == MBOXCMD_PASSTHRU64) ||
353 (mimd.mbox[0] == MBOXCMD_EXTPTHRU))
354 return (-EINVAL);
356 opcode = mimd.ui.fcs.opcode;
357 subopcode = mimd.ui.fcs.subopcode;
358 adapno = GETADAP(mimd.ui.fcs.adapno);
360 if (adapno >= adapters_count_g)
361 return (-ENODEV);
363 kioc->adapno = adapno;
364 kioc->mb_type = MBOX_LEGACY;
365 kioc->app_type = APPTYPE_MIMD;
367 switch (opcode) {
369 case 0x82:
371 if (subopcode == MEGAIOC_QADAPINFO) {
373 kioc->opcode = GET_ADAP_INFO;
374 kioc->data_dir = UIOC_RD;
375 kioc->xferlen = sizeof(mraid_hba_info_t);
377 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
378 return (-ENOMEM);
380 else {
381 con_log(CL_ANN, (KERN_WARNING
382 "megaraid cmm: Invalid subop\n"));
383 return (-EINVAL);
386 break;
388 case 0x81:
390 kioc->opcode = MBOX_CMD;
391 kioc->xferlen = mimd.ui.fcs.length;
392 kioc->user_data_len = kioc->xferlen;
393 kioc->user_data = mimd.ui.fcs.buffer;
395 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
396 return (-ENOMEM);
398 if (mimd.outlen) kioc->data_dir = UIOC_RD;
399 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
401 break;
403 case 0x80:
405 kioc->opcode = MBOX_CMD;
406 kioc->xferlen = (mimd.outlen > mimd.inlen) ?
407 mimd.outlen : mimd.inlen;
408 kioc->user_data_len = kioc->xferlen;
409 kioc->user_data = mimd.data;
411 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
412 return (-ENOMEM);
414 if (mimd.outlen) kioc->data_dir = UIOC_RD;
415 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
417 break;
419 default:
420 return (-EINVAL);
424 * If driver command, nothing else to do
426 if (opcode == 0x82)
427 return 0;
430 * This is a mailbox cmd; copy the mailbox from mimd
432 mbox64 = (mbox64_t *)((unsigned long)kioc->cmdbuf);
433 mbox = &mbox64->mbox32;
434 memcpy(mbox, mimd.mbox, 14);
436 if (mbox->cmd != MBOXCMD_PASSTHRU) { // regular DCMD
438 mbox->xferaddr = (uint32_t)kioc->buf_paddr;
440 if (kioc->data_dir & UIOC_WR) {
441 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
442 kioc->xferlen)) {
443 return (-EFAULT);
447 return 0;
451 * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
452 * Just like in above case, the beginning for memblk is treated as
453 * a mailbox. The passthru will begin at next 1K boundary. And the
454 * data will start 1K after that.
456 pthru32 = kioc->pthru32;
457 kioc->user_pthru = &umimd->pthru;
458 mbox->xferaddr = (uint32_t)kioc->pthru32_h;
460 if (copy_from_user(pthru32, kioc->user_pthru,
461 sizeof(mraid_passthru_t))) {
462 return (-EFAULT);
465 pthru32->dataxferaddr = kioc->buf_paddr;
466 if (kioc->data_dir & UIOC_WR) {
467 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
468 pthru32->dataxferlen)) {
469 return (-EFAULT);
473 return 0;
477 * mraid_mm_attch_buf - Attach a free dma buffer for required size
479 * @adp : Adapter softstate
480 * @kioc : kioc that the buffer needs to be attached to
481 * @xferlen : required length for buffer
483 * First we search for a pool with smallest buffer that is >= @xferlen. If
484 * that pool has no free buffer, we will try for the next bigger size. If none
485 * is available, we will try to allocate the smallest buffer that is >=
486 * @xferlen and attach it the pool.
488 static int
489 mraid_mm_attach_buf(mraid_mmadp_t *adp, uioc_t *kioc, int xferlen)
491 mm_dmapool_t *pool;
492 int right_pool = -1;
493 unsigned long flags;
494 int i;
496 kioc->pool_index = -1;
497 kioc->buf_vaddr = NULL;
498 kioc->buf_paddr = 0;
499 kioc->free_buf = 0;
502 * We need xferlen amount of memory. See if we can get it from our
503 * dma pools. If we don't get exact size, we will try bigger buffer
506 for (i = 0; i < MAX_DMA_POOLS; i++) {
508 pool = &adp->dma_pool_list[i];
510 if (xferlen > pool->buf_size)
511 continue;
513 if (right_pool == -1)
514 right_pool = i;
516 spin_lock_irqsave(&pool->lock, flags);
518 if (!pool->in_use) {
520 pool->in_use = 1;
521 kioc->pool_index = i;
522 kioc->buf_vaddr = pool->vaddr;
523 kioc->buf_paddr = pool->paddr;
525 spin_unlock_irqrestore(&pool->lock, flags);
526 return 0;
528 else {
529 spin_unlock_irqrestore(&pool->lock, flags);
530 continue;
535 * If xferlen doesn't match any of our pools, return error
537 if (right_pool == -1)
538 return -EINVAL;
541 * We did not get any buffer from the preallocated pool. Let us try
542 * to allocate one new buffer. NOTE: This is a blocking call.
544 pool = &adp->dma_pool_list[right_pool];
546 spin_lock_irqsave(&pool->lock, flags);
548 kioc->pool_index = right_pool;
549 kioc->free_buf = 1;
550 kioc->buf_vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
551 &kioc->buf_paddr);
552 spin_unlock_irqrestore(&pool->lock, flags);
554 if (!kioc->buf_vaddr)
555 return -ENOMEM;
557 return 0;
561 * mraid_mm_alloc_kioc - Returns a uioc_t from free list
562 * @adp : Adapter softstate for this module
564 * The kioc_semaphore is initialized with number of kioc nodes in the
565 * free kioc pool. If the kioc pool is empty, this function blocks till
566 * a kioc becomes free.
568 static uioc_t *
569 mraid_mm_alloc_kioc(mraid_mmadp_t *adp)
571 uioc_t *kioc;
572 struct list_head* head;
573 unsigned long flags;
575 down(&adp->kioc_semaphore);
577 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
579 head = &adp->kioc_pool;
581 if (list_empty(head)) {
582 up(&adp->kioc_semaphore);
583 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
585 con_log(CL_ANN, ("megaraid cmm: kioc list empty!\n"));
586 return NULL;
589 kioc = list_entry(head->next, uioc_t, list);
590 list_del_init(&kioc->list);
592 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
594 memset((caddr_t)(unsigned long)kioc->cmdbuf, 0, sizeof(mbox64_t));
595 memset((caddr_t) kioc->pthru32, 0, sizeof(mraid_passthru_t));
597 kioc->buf_vaddr = NULL;
598 kioc->buf_paddr = 0;
599 kioc->pool_index =-1;
600 kioc->free_buf = 0;
601 kioc->user_data = NULL;
602 kioc->user_data_len = 0;
603 kioc->user_pthru = NULL;
604 kioc->timedout = 0;
606 return kioc;
610 * mraid_mm_dealloc_kioc - Return kioc to free pool
612 * @adp : Adapter softstate
613 * @kioc : uioc_t node to be returned to free pool
615 static void
616 mraid_mm_dealloc_kioc(mraid_mmadp_t *adp, uioc_t *kioc)
618 mm_dmapool_t *pool;
619 unsigned long flags;
621 if (kioc->pool_index != -1) {
622 pool = &adp->dma_pool_list[kioc->pool_index];
624 /* This routine may be called in non-isr context also */
625 spin_lock_irqsave(&pool->lock, flags);
628 * While attaching the dma buffer, if we didn't get the
629 * required buffer from the pool, we would have allocated
630 * it at the run time and set the free_buf flag. We must
631 * free that buffer. Otherwise, just mark that the buffer is
632 * not in use
634 if (kioc->free_buf == 1)
635 pci_pool_free(pool->handle, kioc->buf_vaddr,
636 kioc->buf_paddr);
637 else
638 pool->in_use = 0;
640 spin_unlock_irqrestore(&pool->lock, flags);
643 /* Return the kioc to the free pool */
644 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
645 list_add(&kioc->list, &adp->kioc_pool);
646 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
648 /* increment the free kioc count */
649 up(&adp->kioc_semaphore);
651 return;
655 * lld_ioctl - Routine to issue ioctl to low level drvr
657 * @adp : The adapter handle
658 * @kioc : The ioctl packet with kernel addresses
660 static int
661 lld_ioctl(mraid_mmadp_t *adp, uioc_t *kioc)
663 int rval;
664 struct timer_list timer;
665 struct timer_list *tp = NULL;
667 kioc->status = -ENODATA;
668 rval = adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE);
670 if (rval) return rval;
673 * Start the timer
675 if (adp->timeout > 0) {
676 tp = &timer;
677 init_timer(tp);
679 tp->function = lld_timedout;
680 tp->data = (unsigned long)kioc;
681 tp->expires = jiffies + adp->timeout * HZ;
683 add_timer(tp);
687 * Wait till the low level driver completes the ioctl. After this
688 * call, the ioctl either completed successfully or timedout.
690 wait_event(wait_q, (kioc->status != -ENODATA));
691 if (tp) {
692 del_timer_sync(tp);
696 * If the command had timedout, we mark the controller offline
697 * before returning
699 if (kioc->timedout) {
700 adp->quiescent = 0;
703 return kioc->status;
708 * ioctl_done - callback from the low level driver
710 * @kioc : completed ioctl packet
712 static void
713 ioctl_done(uioc_t *kioc)
715 uint32_t adapno;
716 int iterator;
717 mraid_mmadp_t* adapter;
720 * When the kioc returns from driver, make sure it still doesn't
721 * have ENODATA in status. Otherwise, driver will hang on wait_event
722 * forever
724 if (kioc->status == -ENODATA) {
725 con_log(CL_ANN, (KERN_WARNING
726 "megaraid cmm: lld didn't change status!\n"));
728 kioc->status = -EINVAL;
732 * Check if this kioc was timedout before. If so, nobody is waiting
733 * on this kioc. We don't have to wake up anybody. Instead, we just
734 * have to free the kioc
736 if (kioc->timedout) {
737 iterator = 0;
738 adapter = NULL;
739 adapno = kioc->adapno;
741 con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
742 "ioctl that was timedout before\n"));
744 list_for_each_entry(adapter, &adapters_list_g, list) {
745 if (iterator++ == adapno) break;
748 kioc->timedout = 0;
750 if (adapter) {
751 mraid_mm_dealloc_kioc( adapter, kioc );
754 else {
755 wake_up(&wait_q);
761 * lld_timedout : callback from the expired timer
763 * @ptr : ioctl packet that timed out
765 static void
766 lld_timedout(unsigned long ptr)
768 uioc_t *kioc = (uioc_t *)ptr;
770 kioc->status = -ETIME;
771 kioc->timedout = 1;
773 con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n"));
775 wake_up(&wait_q);
780 * kioc_to_mimd : Converter from new back to old format
782 * @kioc : Kernel space IOCTL packet (successfully issued)
783 * @mimd : User space MIMD packet
785 static int
786 kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd)
788 mimd_t kmimd;
789 uint8_t opcode;
790 uint8_t subopcode;
792 mbox64_t *mbox64;
793 mraid_passthru_t __user *upthru32;
794 mraid_passthru_t *kpthru32;
795 mcontroller_t cinfo;
796 mraid_hba_info_t *hinfo;
799 if (copy_from_user(&kmimd, mimd, sizeof(mimd_t)))
800 return (-EFAULT);
802 opcode = kmimd.ui.fcs.opcode;
803 subopcode = kmimd.ui.fcs.subopcode;
805 if (opcode == 0x82) {
806 switch (subopcode) {
808 case MEGAIOC_QADAPINFO:
810 hinfo = (mraid_hba_info_t *)(unsigned long)
811 kioc->buf_vaddr;
813 hinfo_to_cinfo(hinfo, &cinfo);
815 if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo)))
816 return (-EFAULT);
818 return 0;
820 default:
821 return (-EINVAL);
824 return 0;
827 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
829 if (kioc->user_pthru) {
831 upthru32 = kioc->user_pthru;
832 kpthru32 = kioc->pthru32;
834 if (copy_to_user(&upthru32->scsistatus,
835 &kpthru32->scsistatus,
836 sizeof(uint8_t))) {
837 return (-EFAULT);
841 if (kioc->user_data) {
842 if (copy_to_user(kioc->user_data, kioc->buf_vaddr,
843 kioc->user_data_len)) {
844 return (-EFAULT);
848 if (copy_to_user(&mimd->mbox[17],
849 &mbox64->mbox32.status, sizeof(uint8_t))) {
850 return (-EFAULT);
853 return 0;
858 * hinfo_to_cinfo - Convert new format hba info into old format
860 * @hinfo : New format, more comprehensive adapter info
861 * @cinfo : Old format adapter info to support mimd_t apps
863 static void
864 hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo)
866 if (!hinfo || !cinfo)
867 return;
869 cinfo->base = hinfo->baseport;
870 cinfo->irq = hinfo->irq;
871 cinfo->numldrv = hinfo->num_ldrv;
872 cinfo->pcibus = hinfo->pci_bus;
873 cinfo->pcidev = hinfo->pci_slot;
874 cinfo->pcifun = PCI_FUNC(hinfo->pci_dev_fn);
875 cinfo->pciid = hinfo->pci_device_id;
876 cinfo->pcivendor = hinfo->pci_vendor_id;
877 cinfo->pcislot = hinfo->pci_slot;
878 cinfo->uid = hinfo->unique_id;
883 * mraid_mm_register_adp - Registration routine for low level drvrs
885 * @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 = kmalloc(sizeof(mraid_mmadp_t), GFP_KERNEL);
902 if (!adapter) {
903 rval = -ENOMEM;
904 goto memalloc_error;
907 memset(adapter, 0, sizeof(mraid_mmadp_t));
909 adapter->unique_id = lld_adp->unique_id;
910 adapter->drvr_type = lld_adp->drvr_type;
911 adapter->drvr_data = lld_adp->drvr_data;
912 adapter->pdev = lld_adp->pdev;
913 adapter->issue_uioc = lld_adp->issue_uioc;
914 adapter->timeout = lld_adp->timeout;
915 adapter->max_kioc = lld_adp->max_kioc;
916 adapter->quiescent = 1;
919 * Allocate single blocks of memory for all required kiocs,
920 * mailboxes and passthru structures.
922 adapter->kioc_list = kmalloc(sizeof(uioc_t) * lld_adp->max_kioc,
923 GFP_KERNEL);
924 adapter->mbox_list = kmalloc(sizeof(mbox64_t) * lld_adp->max_kioc,
925 GFP_KERNEL);
926 adapter->pthru_dma_pool = pci_pool_create("megaraid mm pthru pool",
927 adapter->pdev,
928 sizeof(mraid_passthru_t),
929 16, 0);
931 if (!adapter->kioc_list || !adapter->mbox_list ||
932 !adapter->pthru_dma_pool) {
934 con_log(CL_ANN, (KERN_WARNING
935 "megaraid cmm: out of memory, %s %d\n", __FUNCTION__,
936 __LINE__));
938 rval = (-ENOMEM);
940 goto memalloc_error;
944 * Slice kioc_list and make a kioc_pool with the individiual kiocs
946 INIT_LIST_HEAD(&adapter->kioc_pool);
947 spin_lock_init(&adapter->kioc_pool_lock);
948 sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
950 mbox_list = (mbox64_t *)adapter->mbox_list;
952 for (i = 0; i < lld_adp->max_kioc; i++) {
954 kioc = adapter->kioc_list + i;
955 kioc->cmdbuf = (uint64_t)(unsigned long)(mbox_list + i);
956 kioc->pthru32 = pci_pool_alloc(adapter->pthru_dma_pool,
957 GFP_KERNEL, &kioc->pthru32_h);
959 if (!kioc->pthru32) {
961 con_log(CL_ANN, (KERN_WARNING
962 "megaraid cmm: out of memory, %s %d\n",
963 __FUNCTION__, __LINE__));
965 rval = (-ENOMEM);
967 goto pthru_dma_pool_error;
970 list_add_tail(&kioc->list, &adapter->kioc_pool);
973 // Setup the dma pools for data buffers
974 if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
975 goto dma_pool_error;
978 list_add_tail(&adapter->list, &adapters_list_g);
980 adapters_count_g++;
982 return 0;
984 dma_pool_error:
985 /* Do nothing */
987 pthru_dma_pool_error:
989 for (i = 0; i < lld_adp->max_kioc; i++) {
990 kioc = adapter->kioc_list + i;
991 if (kioc->pthru32) {
992 pci_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
993 kioc->pthru32_h);
997 memalloc_error:
999 if (adapter->kioc_list)
1000 kfree(adapter->kioc_list);
1002 if (adapter->mbox_list)
1003 kfree(adapter->mbox_list);
1005 if (adapter->pthru_dma_pool)
1006 pci_pool_destroy(adapter->pthru_dma_pool);
1008 if (adapter)
1009 kfree(adapter);
1011 return rval;
1016 * mraid_mm_adapter_app_handle - return the application handle for this adapter
1018 * For the given driver data, locate the adadpter in our global list and
1019 * return the corresponding handle, which is also used by applications to
1020 * uniquely identify an adapter.
1022 * @param unique_id : adapter unique identifier
1024 * @return adapter handle if found in the list
1025 * @return 0 if adapter could not be located, should never happen though
1027 uint32_t
1028 mraid_mm_adapter_app_handle(uint32_t unique_id)
1030 mraid_mmadp_t *adapter;
1031 mraid_mmadp_t *tmp;
1032 int index = 0;
1034 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1036 if (adapter->unique_id == unique_id) {
1038 return MKADAP(index);
1041 index++;
1044 return 0;
1049 * 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
1057 * pool.
1059 static int
1060 mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
1062 mm_dmapool_t *pool;
1063 int bufsize;
1064 int i;
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 = pci_pool_create("megaraid mm data buffer",
1079 adp->pdev, bufsize, 16, 0);
1081 if (!pool->handle) {
1082 goto dma_pool_setup_error;
1085 pool->vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
1086 &pool->paddr);
1088 if (!pool->vaddr)
1089 goto dma_pool_setup_error;
1091 bufsize = bufsize * 2;
1094 return 0;
1096 dma_pool_setup_error:
1098 mraid_mm_teardown_dma_pools(adp);
1099 return (-ENOMEM);
1104 * mraid_mm_unregister_adp - Unregister routine for low level drivers
1105 * Assume no outstanding ioctls to llds.
1107 * @unique_id : UID of the adpater
1110 mraid_mm_unregister_adp(uint32_t unique_id)
1112 mraid_mmadp_t *adapter;
1113 mraid_mmadp_t *tmp;
1115 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1118 if (adapter->unique_id == unique_id) {
1120 adapters_count_g--;
1122 list_del_init(&adapter->list);
1124 mraid_mm_free_adp_resources(adapter);
1126 kfree(adapter);
1128 con_log(CL_ANN, (
1129 "megaraid cmm: Unregistered one adapter:%#x\n",
1130 unique_id));
1132 return 0;
1136 return (-ENODEV);
1140 * mraid_mm_free_adp_resources - Free adapter softstate
1142 * @adp : Adapter softstate
1144 static void
1145 mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
1147 uioc_t *kioc;
1148 int i;
1150 mraid_mm_teardown_dma_pools(adp);
1152 for (i = 0; i < adp->max_kioc; i++) {
1154 kioc = adp->kioc_list + i;
1156 pci_pool_free(adp->pthru_dma_pool, kioc->pthru32,
1157 kioc->pthru32_h);
1160 kfree(adp->kioc_list);
1162 kfree(adp->mbox_list);
1164 pci_pool_destroy(adp->pthru_dma_pool);
1167 return;
1172 * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
1174 * @adp : Adapter softstate
1176 static void
1177 mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
1179 int i;
1180 mm_dmapool_t *pool;
1182 for (i = 0; i < MAX_DMA_POOLS; i++) {
1184 pool = &adp->dma_pool_list[i];
1186 if (pool->handle) {
1188 if (pool->vaddr)
1189 pci_pool_free(pool->handle, pool->vaddr,
1190 pool->paddr);
1192 pci_pool_destroy(pool->handle);
1193 pool->handle = NULL;
1197 return;
1201 * mraid_mm_init : Module entry point
1203 static int __init
1204 mraid_mm_init(void)
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 majorno = register_chrdev(0, "megadev", &lsi_fops);
1212 if (majorno < 0) {
1213 con_log(CL_ANN, ("megaraid cmm: cannot get major\n"));
1214 return majorno;
1217 init_waitqueue_head(&wait_q);
1219 INIT_LIST_HEAD(&adapters_list_g);
1221 return 0;
1226 * mraid_mm_compat_ioctl : 32bit to 64bit ioctl conversion routine
1228 #ifdef CONFIG_COMPAT
1229 static long
1230 mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd,
1231 unsigned long arg)
1233 int err;
1234 lock_kernel();
1235 err = mraid_mm_ioctl(NULL, filep, cmd, arg);
1236 unlock_kernel();
1237 return err;
1239 #endif
1242 * mraid_mm_exit : Module exit point
1244 static void __exit
1245 mraid_mm_exit(void)
1247 con_log(CL_DLEVEL1 , ("exiting common mod\n"));
1249 unregister_chrdev(majorno, "megadev");
1252 module_init(mraid_mm_init);
1253 module_exit(mraid_mm_exit);
1255 /* vi: set ts=8 sw=8 tw=78: */