[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / scsi / megaraid / megaraid_mm.c
blob37d110e864c49c9c524e8347118e42050cdd3e49
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.6 (Mar 7 2005)
15 * Common management module
18 #include "megaraid_mm.h"
21 // Entry points for char node driver
22 static int mraid_mm_open(struct inode *, struct file *);
23 static int mraid_mm_ioctl(struct inode *, struct file *, uint, unsigned long);
26 // routines to convert to and from the old the format
27 static int mimd_to_kioc(mimd_t __user *, mraid_mmadp_t *, uioc_t *);
28 static int kioc_to_mimd(uioc_t *, mimd_t __user *);
31 // Helper functions
32 static int handle_drvrcmd(void __user *, uint8_t, int *);
33 static int lld_ioctl(mraid_mmadp_t *, uioc_t *);
34 static void ioctl_done(uioc_t *);
35 static void lld_timedout(unsigned long);
36 static void hinfo_to_cinfo(mraid_hba_info_t *, mcontroller_t *);
37 static mraid_mmadp_t *mraid_mm_get_adapter(mimd_t __user *, int *);
38 static uioc_t *mraid_mm_alloc_kioc(mraid_mmadp_t *);
39 static void mraid_mm_dealloc_kioc(mraid_mmadp_t *, uioc_t *);
40 static int mraid_mm_attach_buf(mraid_mmadp_t *, uioc_t *, int);
41 static int mraid_mm_setup_dma_pools(mraid_mmadp_t *);
42 static void mraid_mm_free_adp_resources(mraid_mmadp_t *);
43 static void mraid_mm_teardown_dma_pools(mraid_mmadp_t *);
45 #ifdef CONFIG_COMPAT
46 static long mraid_mm_compat_ioctl(struct file *, unsigned int, unsigned long);
47 #endif
49 MODULE_AUTHOR("LSI Logic Corporation");
50 MODULE_DESCRIPTION("LSI Logic Management Module");
51 MODULE_LICENSE("GPL");
52 MODULE_VERSION(LSI_COMMON_MOD_VERSION);
54 static int dbglevel = CL_ANN;
55 module_param_named(dlevel, dbglevel, int, 0);
56 MODULE_PARM_DESC(dlevel, "Debug level (default=0)");
58 EXPORT_SYMBOL(mraid_mm_register_adp);
59 EXPORT_SYMBOL(mraid_mm_unregister_adp);
60 EXPORT_SYMBOL(mraid_mm_adapter_app_handle);
62 static int majorno;
63 static uint32_t drvr_ver = 0x02200206;
65 static int adapters_count_g;
66 static struct list_head adapters_list_g;
68 static wait_queue_head_t wait_q;
70 static 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 /**
80 * mraid_mm_open - open routine for char node interface
81 * @inod : unused
82 * @filep : unused
84 * allow ioctl operations by apps only if they superuser privilege
86 static int
87 mraid_mm_open(struct inode *inode, struct file *filep)
90 * Only allow superuser to access private ioctl interface
92 if (!capable(CAP_SYS_ADMIN)) return (-EACCES);
94 return 0;
97 /**
98 * mraid_mm_ioctl - module entry-point for ioctls
99 * @inode : inode (ignored)
100 * @filep : file operations pointer (ignored)
101 * @cmd : ioctl command
102 * @arg : user ioctl packet
104 static int
105 mraid_mm_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
106 unsigned long arg)
108 uioc_t *kioc;
109 char signature[EXT_IOCTL_SIGN_SZ] = {0};
110 int rval;
111 mraid_mmadp_t *adp;
112 uint8_t old_ioctl;
113 int drvrcmd_rval;
114 void __user *argp = (void __user *)arg;
117 * Make sure only USCSICMD are issued through this interface.
118 * MIMD application would still fire different command.
121 if ((_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD)) {
122 return (-EINVAL);
126 * Look for signature to see if this is the new or old ioctl format.
128 if (copy_from_user(signature, argp, EXT_IOCTL_SIGN_SZ)) {
129 con_log(CL_ANN, (KERN_WARNING
130 "megaraid cmm: copy from usr addr failed\n"));
131 return (-EFAULT);
134 if (memcmp(signature, EXT_IOCTL_SIGN, EXT_IOCTL_SIGN_SZ) == 0)
135 old_ioctl = 0;
136 else
137 old_ioctl = 1;
140 * At present, we don't support the new ioctl packet
142 if (!old_ioctl )
143 return (-EINVAL);
146 * If it is a driver ioctl (as opposed to fw ioctls), then we can
147 * handle the command locally. rval > 0 means it is not a drvr cmd
149 rval = handle_drvrcmd(argp, old_ioctl, &drvrcmd_rval);
151 if (rval < 0)
152 return rval;
153 else if (rval == 0)
154 return drvrcmd_rval;
156 rval = 0;
157 if ((adp = mraid_mm_get_adapter(argp, &rval)) == NULL) {
158 return rval;
162 * Check if adapter can accept ioctl. We may have marked it offline
163 * if any previous kioc had timedout on this controller.
165 if (!adp->quiescent) {
166 con_log(CL_ANN, (KERN_WARNING
167 "megaraid cmm: controller cannot accept cmds due to "
168 "earlier errors\n" ));
169 return -EFAULT;
173 * The following call will block till a kioc is available
175 kioc = mraid_mm_alloc_kioc(adp);
178 * User sent the old mimd_t ioctl packet. Convert it to uioc_t.
180 if ((rval = mimd_to_kioc(argp, adp, kioc))) {
181 mraid_mm_dealloc_kioc(adp, kioc);
182 return rval;
185 kioc->done = ioctl_done;
188 * Issue the IOCTL to the low level driver. After the IOCTL completes
189 * release the kioc if and only if it was _not_ timedout. If it was
190 * timedout, that means that resources are still with low level driver.
192 if ((rval = lld_ioctl(adp, kioc))) {
194 if (!kioc->timedout)
195 mraid_mm_dealloc_kioc(adp, kioc);
197 return rval;
201 * Convert the kioc back to user space
203 rval = kioc_to_mimd(kioc, argp);
206 * Return the kioc to free pool
208 mraid_mm_dealloc_kioc(adp, kioc);
210 return rval;
215 * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
216 * @umimd : User space mimd_t ioctl packet
217 * @adapter : pointer to the adapter (OUT)
219 static mraid_mmadp_t *
220 mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
222 mraid_mmadp_t *adapter;
223 mimd_t mimd;
224 uint32_t adapno;
225 int iterator;
228 if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) {
229 *rval = -EFAULT;
230 return NULL;
233 adapno = GETADAP(mimd.ui.fcs.adapno);
235 if (adapno >= adapters_count_g) {
236 *rval = -ENODEV;
237 return NULL;
240 adapter = NULL;
241 iterator = 0;
243 list_for_each_entry(adapter, &adapters_list_g, list) {
244 if (iterator++ == adapno) break;
247 if (!adapter) {
248 *rval = -ENODEV;
249 return NULL;
252 return adapter;
256 * handle_drvrcmd - This routine checks if the opcode is a driver
257 * cmd and if it is, handles it.
258 * @arg : packet sent by the user app
259 * @old_ioctl : mimd if 1; uioc otherwise
261 static int
262 handle_drvrcmd(void __user *arg, uint8_t old_ioctl, int *rval)
264 mimd_t __user *umimd;
265 mimd_t kmimd;
266 uint8_t opcode;
267 uint8_t subopcode;
269 if (old_ioctl)
270 goto old_packet;
271 else
272 goto new_packet;
274 new_packet:
275 return (-ENOTSUPP);
277 old_packet:
278 *rval = 0;
279 umimd = arg;
281 if (copy_from_user(&kmimd, umimd, sizeof(mimd_t)))
282 return (-EFAULT);
284 opcode = kmimd.ui.fcs.opcode;
285 subopcode = kmimd.ui.fcs.subopcode;
288 * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
289 * GET_NUMADP, then we can handle. Otherwise we should return 1 to
290 * indicate that we cannot handle this.
292 if (opcode != 0x82)
293 return 1;
295 switch (subopcode) {
297 case MEGAIOC_QDRVRVER:
299 if (copy_to_user(kmimd.data, &drvr_ver, sizeof(uint32_t)))
300 return (-EFAULT);
302 return 0;
304 case MEGAIOC_QNADAP:
306 *rval = adapters_count_g;
308 if (copy_to_user(kmimd.data, &adapters_count_g,
309 sizeof(uint32_t)))
310 return (-EFAULT);
312 return 0;
314 default:
315 /* cannot handle */
316 return 1;
319 return 0;
324 * mimd_to_kioc - Converter from old to new ioctl format
326 * @umimd : user space old MIMD IOCTL
327 * @kioc : kernel space new format IOCTL
329 * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
330 * new packet is in kernel space so that driver can perform operations on it
331 * freely.
334 static int
335 mimd_to_kioc(mimd_t __user *umimd, mraid_mmadp_t *adp, uioc_t *kioc)
337 mbox64_t *mbox64;
338 mbox_t *mbox;
339 mraid_passthru_t *pthru32;
340 uint32_t adapno;
341 uint8_t opcode;
342 uint8_t subopcode;
343 mimd_t mimd;
345 if (copy_from_user(&mimd, umimd, sizeof(mimd_t)))
346 return (-EFAULT);
349 * Applications are not allowed to send extd pthru
351 if ((mimd.mbox[0] == MBOXCMD_PASSTHRU64) ||
352 (mimd.mbox[0] == MBOXCMD_EXTPTHRU))
353 return (-EINVAL);
355 opcode = mimd.ui.fcs.opcode;
356 subopcode = mimd.ui.fcs.subopcode;
357 adapno = GETADAP(mimd.ui.fcs.adapno);
359 if (adapno >= adapters_count_g)
360 return (-ENODEV);
362 kioc->adapno = adapno;
363 kioc->mb_type = MBOX_LEGACY;
364 kioc->app_type = APPTYPE_MIMD;
366 switch (opcode) {
368 case 0x82:
370 if (subopcode == MEGAIOC_QADAPINFO) {
372 kioc->opcode = GET_ADAP_INFO;
373 kioc->data_dir = UIOC_RD;
374 kioc->xferlen = sizeof(mraid_hba_info_t);
376 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
377 return (-ENOMEM);
379 else {
380 con_log(CL_ANN, (KERN_WARNING
381 "megaraid cmm: Invalid subop\n"));
382 return (-EINVAL);
385 break;
387 case 0x81:
389 kioc->opcode = MBOX_CMD;
390 kioc->xferlen = mimd.ui.fcs.length;
391 kioc->user_data_len = kioc->xferlen;
392 kioc->user_data = mimd.ui.fcs.buffer;
394 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
395 return (-ENOMEM);
397 if (mimd.outlen) kioc->data_dir = UIOC_RD;
398 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
400 break;
402 case 0x80:
404 kioc->opcode = MBOX_CMD;
405 kioc->xferlen = (mimd.outlen > mimd.inlen) ?
406 mimd.outlen : mimd.inlen;
407 kioc->user_data_len = kioc->xferlen;
408 kioc->user_data = mimd.data;
410 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
411 return (-ENOMEM);
413 if (mimd.outlen) kioc->data_dir = UIOC_RD;
414 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
416 break;
418 default:
419 return (-EINVAL);
423 * If driver command, nothing else to do
425 if (opcode == 0x82)
426 return 0;
429 * This is a mailbox cmd; copy the mailbox from mimd
431 mbox64 = (mbox64_t *)((unsigned long)kioc->cmdbuf);
432 mbox = &mbox64->mbox32;
433 memcpy(mbox, mimd.mbox, 14);
435 if (mbox->cmd != MBOXCMD_PASSTHRU) { // regular DCMD
437 mbox->xferaddr = (uint32_t)kioc->buf_paddr;
439 if (kioc->data_dir & UIOC_WR) {
440 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
441 kioc->xferlen)) {
442 return (-EFAULT);
446 return 0;
450 * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
451 * Just like in above case, the beginning for memblk is treated as
452 * a mailbox. The passthru will begin at next 1K boundary. And the
453 * data will start 1K after that.
455 pthru32 = kioc->pthru32;
456 kioc->user_pthru = &umimd->pthru;
457 mbox->xferaddr = (uint32_t)kioc->pthru32_h;
459 if (copy_from_user(pthru32, kioc->user_pthru,
460 sizeof(mraid_passthru_t))) {
461 return (-EFAULT);
464 pthru32->dataxferaddr = kioc->buf_paddr;
465 if (kioc->data_dir & UIOC_WR) {
466 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
467 pthru32->dataxferlen)) {
468 return (-EFAULT);
472 return 0;
476 * mraid_mm_attch_buf - Attach a free dma buffer for required size
478 * @adp : Adapter softstate
479 * @kioc : kioc that the buffer needs to be attached to
480 * @xferlen : required length for buffer
482 * First we search for a pool with smallest buffer that is >= @xferlen. If
483 * that pool has no free buffer, we will try for the next bigger size. If none
484 * is available, we will try to allocate the smallest buffer that is >=
485 * @xferlen and attach it the pool.
487 static int
488 mraid_mm_attach_buf(mraid_mmadp_t *adp, uioc_t *kioc, int xferlen)
490 mm_dmapool_t *pool;
491 int right_pool = -1;
492 unsigned long flags;
493 int i;
495 kioc->pool_index = -1;
496 kioc->buf_vaddr = NULL;
497 kioc->buf_paddr = 0;
498 kioc->free_buf = 0;
501 * We need xferlen amount of memory. See if we can get it from our
502 * dma pools. If we don't get exact size, we will try bigger buffer
505 for (i = 0; i < MAX_DMA_POOLS; i++) {
507 pool = &adp->dma_pool_list[i];
509 if (xferlen > pool->buf_size)
510 continue;
512 if (right_pool == -1)
513 right_pool = i;
515 spin_lock_irqsave(&pool->lock, flags);
517 if (!pool->in_use) {
519 pool->in_use = 1;
520 kioc->pool_index = i;
521 kioc->buf_vaddr = pool->vaddr;
522 kioc->buf_paddr = pool->paddr;
524 spin_unlock_irqrestore(&pool->lock, flags);
525 return 0;
527 else {
528 spin_unlock_irqrestore(&pool->lock, flags);
529 continue;
534 * If xferlen doesn't match any of our pools, return error
536 if (right_pool == -1)
537 return -EINVAL;
540 * We did not get any buffer from the preallocated pool. Let us try
541 * to allocate one new buffer. NOTE: This is a blocking call.
543 pool = &adp->dma_pool_list[right_pool];
545 spin_lock_irqsave(&pool->lock, flags);
547 kioc->pool_index = right_pool;
548 kioc->free_buf = 1;
549 kioc->buf_vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
550 &kioc->buf_paddr);
551 spin_unlock_irqrestore(&pool->lock, flags);
553 if (!kioc->buf_vaddr)
554 return -ENOMEM;
556 return 0;
560 * mraid_mm_alloc_kioc - Returns a uioc_t from free list
561 * @adp : Adapter softstate for this module
563 * The kioc_semaphore is initialized with number of kioc nodes in the
564 * free kioc pool. If the kioc pool is empty, this function blocks till
565 * a kioc becomes free.
567 static uioc_t *
568 mraid_mm_alloc_kioc(mraid_mmadp_t *adp)
570 uioc_t *kioc;
571 struct list_head* head;
572 unsigned long flags;
574 down(&adp->kioc_semaphore);
576 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
578 head = &adp->kioc_pool;
580 if (list_empty(head)) {
581 up(&adp->kioc_semaphore);
582 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
584 con_log(CL_ANN, ("megaraid cmm: kioc list empty!\n"));
585 return NULL;
588 kioc = list_entry(head->next, uioc_t, list);
589 list_del_init(&kioc->list);
591 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
593 memset((caddr_t)(unsigned long)kioc->cmdbuf, 0, sizeof(mbox64_t));
594 memset((caddr_t) kioc->pthru32, 0, sizeof(mraid_passthru_t));
596 kioc->buf_vaddr = NULL;
597 kioc->buf_paddr = 0;
598 kioc->pool_index =-1;
599 kioc->free_buf = 0;
600 kioc->user_data = NULL;
601 kioc->user_data_len = 0;
602 kioc->user_pthru = NULL;
603 kioc->timedout = 0;
605 return kioc;
609 * mraid_mm_dealloc_kioc - Return kioc to free pool
611 * @adp : Adapter softstate
612 * @kioc : uioc_t node to be returned to free pool
614 static void
615 mraid_mm_dealloc_kioc(mraid_mmadp_t *adp, uioc_t *kioc)
617 mm_dmapool_t *pool;
618 unsigned long flags;
620 if (kioc->pool_index != -1) {
621 pool = &adp->dma_pool_list[kioc->pool_index];
623 /* This routine may be called in non-isr context also */
624 spin_lock_irqsave(&pool->lock, flags);
627 * While attaching the dma buffer, if we didn't get the
628 * required buffer from the pool, we would have allocated
629 * it at the run time and set the free_buf flag. We must
630 * free that buffer. Otherwise, just mark that the buffer is
631 * not in use
633 if (kioc->free_buf == 1)
634 pci_pool_free(pool->handle, kioc->buf_vaddr,
635 kioc->buf_paddr);
636 else
637 pool->in_use = 0;
639 spin_unlock_irqrestore(&pool->lock, flags);
642 /* Return the kioc to the free pool */
643 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
644 list_add(&kioc->list, &adp->kioc_pool);
645 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
647 /* increment the free kioc count */
648 up(&adp->kioc_semaphore);
650 return;
654 * lld_ioctl - Routine to issue ioctl to low level drvr
656 * @adp : The adapter handle
657 * @kioc : The ioctl packet with kernel addresses
659 static int
660 lld_ioctl(mraid_mmadp_t *adp, uioc_t *kioc)
662 int rval;
663 struct timer_list timer;
664 struct timer_list *tp = NULL;
666 kioc->status = -ENODATA;
667 rval = adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE);
669 if (rval) return rval;
672 * Start the timer
674 if (adp->timeout > 0) {
675 tp = &timer;
676 init_timer(tp);
678 tp->function = lld_timedout;
679 tp->data = (unsigned long)kioc;
680 tp->expires = jiffies + adp->timeout * HZ;
682 add_timer(tp);
686 * Wait till the low level driver completes the ioctl. After this
687 * call, the ioctl either completed successfully or timedout.
689 wait_event(wait_q, (kioc->status != -ENODATA));
690 if (tp) {
691 del_timer_sync(tp);
695 * If the command had timedout, we mark the controller offline
696 * before returning
698 if (kioc->timedout) {
699 adp->quiescent = 0;
702 return kioc->status;
707 * ioctl_done - callback from the low level driver
709 * @kioc : completed ioctl packet
711 static void
712 ioctl_done(uioc_t *kioc)
714 uint32_t adapno;
715 int iterator;
716 mraid_mmadp_t* adapter;
719 * When the kioc returns from driver, make sure it still doesn't
720 * have ENODATA in status. Otherwise, driver will hang on wait_event
721 * forever
723 if (kioc->status == -ENODATA) {
724 con_log(CL_ANN, (KERN_WARNING
725 "megaraid cmm: lld didn't change status!\n"));
727 kioc->status = -EINVAL;
731 * Check if this kioc was timedout before. If so, nobody is waiting
732 * on this kioc. We don't have to wake up anybody. Instead, we just
733 * have to free the kioc
735 if (kioc->timedout) {
736 iterator = 0;
737 adapter = NULL;
738 adapno = kioc->adapno;
740 con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
741 "ioctl that was timedout before\n"));
743 list_for_each_entry(adapter, &adapters_list_g, list) {
744 if (iterator++ == adapno) break;
747 kioc->timedout = 0;
749 if (adapter) {
750 mraid_mm_dealloc_kioc( adapter, kioc );
753 else {
754 wake_up(&wait_q);
760 * lld_timedout : callback from the expired timer
762 * @ptr : ioctl packet that timed out
764 static void
765 lld_timedout(unsigned long ptr)
767 uioc_t *kioc = (uioc_t *)ptr;
769 kioc->status = -ETIME;
770 kioc->timedout = 1;
772 con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n"));
774 wake_up(&wait_q);
779 * kioc_to_mimd : Converter from new back to old format
781 * @kioc : Kernel space IOCTL packet (successfully issued)
782 * @mimd : User space MIMD packet
784 static int
785 kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd)
787 mimd_t kmimd;
788 uint8_t opcode;
789 uint8_t subopcode;
791 mbox64_t *mbox64;
792 mraid_passthru_t __user *upthru32;
793 mraid_passthru_t *kpthru32;
794 mcontroller_t cinfo;
795 mraid_hba_info_t *hinfo;
798 if (copy_from_user(&kmimd, mimd, sizeof(mimd_t)))
799 return (-EFAULT);
801 opcode = kmimd.ui.fcs.opcode;
802 subopcode = kmimd.ui.fcs.subopcode;
804 if (opcode == 0x82) {
805 switch (subopcode) {
807 case MEGAIOC_QADAPINFO:
809 hinfo = (mraid_hba_info_t *)(unsigned long)
810 kioc->buf_vaddr;
812 hinfo_to_cinfo(hinfo, &cinfo);
814 if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo)))
815 return (-EFAULT);
817 return 0;
819 default:
820 return (-EINVAL);
823 return 0;
826 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
828 if (kioc->user_pthru) {
830 upthru32 = kioc->user_pthru;
831 kpthru32 = kioc->pthru32;
833 if (copy_to_user(&upthru32->scsistatus,
834 &kpthru32->scsistatus,
835 sizeof(uint8_t))) {
836 return (-EFAULT);
840 if (kioc->user_data) {
841 if (copy_to_user(kioc->user_data, kioc->buf_vaddr,
842 kioc->user_data_len)) {
843 return (-EFAULT);
847 if (copy_to_user(&mimd->mbox[17],
848 &mbox64->mbox32.status, sizeof(uint8_t))) {
849 return (-EFAULT);
852 return 0;
857 * hinfo_to_cinfo - Convert new format hba info into old format
859 * @hinfo : New format, more comprehensive adapter info
860 * @cinfo : Old format adapter info to support mimd_t apps
862 static void
863 hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo)
865 if (!hinfo || !cinfo)
866 return;
868 cinfo->base = hinfo->baseport;
869 cinfo->irq = hinfo->irq;
870 cinfo->numldrv = hinfo->num_ldrv;
871 cinfo->pcibus = hinfo->pci_bus;
872 cinfo->pcidev = hinfo->pci_slot;
873 cinfo->pcifun = PCI_FUNC(hinfo->pci_dev_fn);
874 cinfo->pciid = hinfo->pci_device_id;
875 cinfo->pcivendor = hinfo->pci_vendor_id;
876 cinfo->pcislot = hinfo->pci_slot;
877 cinfo->uid = hinfo->unique_id;
882 * mraid_mm_register_adp - Registration routine for low level drvrs
884 * @adp : Adapter objejct
887 mraid_mm_register_adp(mraid_mmadp_t *lld_adp)
889 mraid_mmadp_t *adapter;
890 mbox64_t *mbox_list;
891 uioc_t *kioc;
892 uint32_t rval;
893 int i;
896 if (lld_adp->drvr_type != DRVRTYPE_MBOX)
897 return (-EINVAL);
899 adapter = kmalloc(sizeof(mraid_mmadp_t), GFP_KERNEL);
901 if (!adapter) {
902 rval = -ENOMEM;
903 goto memalloc_error;
906 memset(adapter, 0, sizeof(mraid_mmadp_t));
908 adapter->unique_id = lld_adp->unique_id;
909 adapter->drvr_type = lld_adp->drvr_type;
910 adapter->drvr_data = lld_adp->drvr_data;
911 adapter->pdev = lld_adp->pdev;
912 adapter->issue_uioc = lld_adp->issue_uioc;
913 adapter->timeout = lld_adp->timeout;
914 adapter->max_kioc = lld_adp->max_kioc;
915 adapter->quiescent = 1;
918 * Allocate single blocks of memory for all required kiocs,
919 * mailboxes and passthru structures.
921 adapter->kioc_list = kmalloc(sizeof(uioc_t) * lld_adp->max_kioc,
922 GFP_KERNEL);
923 adapter->mbox_list = kmalloc(sizeof(mbox64_t) * lld_adp->max_kioc,
924 GFP_KERNEL);
925 adapter->pthru_dma_pool = pci_pool_create("megaraid mm pthru pool",
926 adapter->pdev,
927 sizeof(mraid_passthru_t),
928 16, 0);
930 if (!adapter->kioc_list || !adapter->mbox_list ||
931 !adapter->pthru_dma_pool) {
933 con_log(CL_ANN, (KERN_WARNING
934 "megaraid cmm: out of memory, %s %d\n", __FUNCTION__,
935 __LINE__));
937 rval = (-ENOMEM);
939 goto memalloc_error;
943 * Slice kioc_list and make a kioc_pool with the individiual kiocs
945 INIT_LIST_HEAD(&adapter->kioc_pool);
946 spin_lock_init(&adapter->kioc_pool_lock);
947 sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
949 mbox_list = (mbox64_t *)adapter->mbox_list;
951 for (i = 0; i < lld_adp->max_kioc; i++) {
953 kioc = adapter->kioc_list + i;
954 kioc->cmdbuf = (uint64_t)(unsigned long)(mbox_list + i);
955 kioc->pthru32 = pci_pool_alloc(adapter->pthru_dma_pool,
956 GFP_KERNEL, &kioc->pthru32_h);
958 if (!kioc->pthru32) {
960 con_log(CL_ANN, (KERN_WARNING
961 "megaraid cmm: out of memory, %s %d\n",
962 __FUNCTION__, __LINE__));
964 rval = (-ENOMEM);
966 goto pthru_dma_pool_error;
969 list_add_tail(&kioc->list, &adapter->kioc_pool);
972 // Setup the dma pools for data buffers
973 if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
974 goto dma_pool_error;
977 list_add_tail(&adapter->list, &adapters_list_g);
979 adapters_count_g++;
981 return 0;
983 dma_pool_error:
984 /* Do nothing */
986 pthru_dma_pool_error:
988 for (i = 0; i < lld_adp->max_kioc; i++) {
989 kioc = adapter->kioc_list + i;
990 if (kioc->pthru32) {
991 pci_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
992 kioc->pthru32_h);
996 memalloc_error:
998 if (adapter->kioc_list)
999 kfree(adapter->kioc_list);
1001 if (adapter->mbox_list)
1002 kfree(adapter->mbox_list);
1004 if (adapter->pthru_dma_pool)
1005 pci_pool_destroy(adapter->pthru_dma_pool);
1007 if (adapter)
1008 kfree(adapter);
1010 return rval;
1015 * mraid_mm_adapter_app_handle - return the application handle for this adapter
1017 * For the given driver data, locate the adadpter in our global list and
1018 * return the corresponding handle, which is also used by applications to
1019 * uniquely identify an adapter.
1021 * @param unique_id : adapter unique identifier
1023 * @return adapter handle if found in the list
1024 * @return 0 if adapter could not be located, should never happen though
1026 uint32_t
1027 mraid_mm_adapter_app_handle(uint32_t unique_id)
1029 mraid_mmadp_t *adapter;
1030 mraid_mmadp_t *tmp;
1031 int index = 0;
1033 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1035 if (adapter->unique_id == unique_id) {
1037 return MKADAP(index);
1040 index++;
1043 return 0;
1048 * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
1050 * @adp : Adapter softstate
1052 * We maintain a pool of dma buffers per each adapter. Each pool has one
1053 * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
1054 * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
1055 * dont' want to waste too much memory by allocating more buffers per each
1056 * pool.
1058 static int
1059 mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
1061 mm_dmapool_t *pool;
1062 int bufsize;
1063 int i;
1066 * Create MAX_DMA_POOLS number of pools
1068 bufsize = MRAID_MM_INIT_BUFF_SIZE;
1070 for (i = 0; i < MAX_DMA_POOLS; i++){
1072 pool = &adp->dma_pool_list[i];
1074 pool->buf_size = bufsize;
1075 spin_lock_init(&pool->lock);
1077 pool->handle = pci_pool_create("megaraid mm data buffer",
1078 adp->pdev, bufsize, 16, 0);
1080 if (!pool->handle) {
1081 goto dma_pool_setup_error;
1084 pool->vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
1085 &pool->paddr);
1087 if (!pool->vaddr)
1088 goto dma_pool_setup_error;
1090 bufsize = bufsize * 2;
1093 return 0;
1095 dma_pool_setup_error:
1097 mraid_mm_teardown_dma_pools(adp);
1098 return (-ENOMEM);
1103 * mraid_mm_unregister_adp - Unregister routine for low level drivers
1104 * Assume no outstanding ioctls to llds.
1106 * @unique_id : UID of the adpater
1109 mraid_mm_unregister_adp(uint32_t unique_id)
1111 mraid_mmadp_t *adapter;
1112 mraid_mmadp_t *tmp;
1114 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1117 if (adapter->unique_id == unique_id) {
1119 adapters_count_g--;
1121 list_del_init(&adapter->list);
1123 mraid_mm_free_adp_resources(adapter);
1125 kfree(adapter);
1127 con_log(CL_ANN, (
1128 "megaraid cmm: Unregistered one adapter:%#x\n",
1129 unique_id));
1131 return 0;
1135 return (-ENODEV);
1139 * mraid_mm_free_adp_resources - Free adapter softstate
1141 * @adp : Adapter softstate
1143 static void
1144 mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
1146 uioc_t *kioc;
1147 int i;
1149 mraid_mm_teardown_dma_pools(adp);
1151 for (i = 0; i < adp->max_kioc; i++) {
1153 kioc = adp->kioc_list + i;
1155 pci_pool_free(adp->pthru_dma_pool, kioc->pthru32,
1156 kioc->pthru32_h);
1159 kfree(adp->kioc_list);
1161 kfree(adp->mbox_list);
1163 pci_pool_destroy(adp->pthru_dma_pool);
1166 return;
1171 * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
1173 * @adp : Adapter softstate
1175 static void
1176 mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
1178 int i;
1179 mm_dmapool_t *pool;
1181 for (i = 0; i < MAX_DMA_POOLS; i++) {
1183 pool = &adp->dma_pool_list[i];
1185 if (pool->handle) {
1187 if (pool->vaddr)
1188 pci_pool_free(pool->handle, pool->vaddr,
1189 pool->paddr);
1191 pci_pool_destroy(pool->handle);
1192 pool->handle = NULL;
1196 return;
1200 * mraid_mm_init : Module entry point
1202 static int __init
1203 mraid_mm_init(void)
1205 // Announce the driver version
1206 con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
1207 LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
1209 majorno = register_chrdev(0, "megadev", &lsi_fops);
1211 if (majorno < 0) {
1212 con_log(CL_ANN, ("megaraid cmm: cannot get major\n"));
1213 return majorno;
1216 init_waitqueue_head(&wait_q);
1218 INIT_LIST_HEAD(&adapters_list_g);
1220 return 0;
1225 * mraid_mm_compat_ioctl : 32bit to 64bit ioctl conversion routine
1227 #ifdef CONFIG_COMPAT
1228 static long
1229 mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd,
1230 unsigned long arg)
1232 int err;
1234 err = mraid_mm_ioctl(NULL, filep, cmd, arg);
1236 return err;
1238 #endif
1241 * mraid_mm_exit : Module exit point
1243 static void __exit
1244 mraid_mm_exit(void)
1246 con_log(CL_DLEVEL1 , ("exiting common mod\n"));
1248 unregister_chrdev(majorno, "megadev");
1251 module_init(mraid_mm_init);
1252 module_exit(mraid_mm_exit);
1254 /* vi: set ts=8 sw=8 tw=78: */