[SUNLANCE]: Mark sparc_lance_probe_one as __devinit.
[linux-2.6/verdex.git] / drivers / scsi / megaraid / megaraid_mm.c
blobd85b9a8f1b8d94e5040e598b35b957f9a90ff1a7
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
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 return -ENOMEM;
904 memset(adapter, 0, sizeof(mraid_mmadp_t));
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", __FUNCTION__,
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 __FUNCTION__, __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
1011 * For the given driver data, locate the adadpter in our global list and
1012 * return the corresponding handle, which is also used by applications to
1013 * uniquely identify an adapter.
1015 * @param unique_id : adapter unique identifier
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
1044 * @adp : Adapter softstate
1046 * We maintain a pool of dma buffers per each adapter. Each pool has one
1047 * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
1048 * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
1049 * dont' want to waste too much memory by allocating more buffers per each
1050 * pool.
1052 static int
1053 mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
1055 mm_dmapool_t *pool;
1056 int bufsize;
1057 int i;
1060 * Create MAX_DMA_POOLS number of pools
1062 bufsize = MRAID_MM_INIT_BUFF_SIZE;
1064 for (i = 0; i < MAX_DMA_POOLS; i++){
1066 pool = &adp->dma_pool_list[i];
1068 pool->buf_size = bufsize;
1069 spin_lock_init(&pool->lock);
1071 pool->handle = pci_pool_create("megaraid mm data buffer",
1072 adp->pdev, bufsize, 16, 0);
1074 if (!pool->handle) {
1075 goto dma_pool_setup_error;
1078 pool->vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
1079 &pool->paddr);
1081 if (!pool->vaddr)
1082 goto dma_pool_setup_error;
1084 bufsize = bufsize * 2;
1087 return 0;
1089 dma_pool_setup_error:
1091 mraid_mm_teardown_dma_pools(adp);
1092 return (-ENOMEM);
1097 * mraid_mm_unregister_adp - Unregister routine for low level drivers
1098 * Assume no outstanding ioctls to llds.
1100 * @unique_id : UID of the adpater
1103 mraid_mm_unregister_adp(uint32_t unique_id)
1105 mraid_mmadp_t *adapter;
1106 mraid_mmadp_t *tmp;
1108 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1111 if (adapter->unique_id == unique_id) {
1113 adapters_count_g--;
1115 list_del_init(&adapter->list);
1117 mraid_mm_free_adp_resources(adapter);
1119 kfree(adapter);
1121 con_log(CL_ANN, (
1122 "megaraid cmm: Unregistered one adapter:%#x\n",
1123 unique_id));
1125 return 0;
1129 return (-ENODEV);
1133 * mraid_mm_free_adp_resources - Free adapter softstate
1135 * @adp : Adapter softstate
1137 static void
1138 mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
1140 uioc_t *kioc;
1141 int i;
1143 mraid_mm_teardown_dma_pools(adp);
1145 for (i = 0; i < adp->max_kioc; i++) {
1147 kioc = adp->kioc_list + i;
1149 pci_pool_free(adp->pthru_dma_pool, kioc->pthru32,
1150 kioc->pthru32_h);
1153 kfree(adp->kioc_list);
1154 kfree(adp->mbox_list);
1156 pci_pool_destroy(adp->pthru_dma_pool);
1159 return;
1164 * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
1166 * @adp : Adapter softstate
1168 static void
1169 mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
1171 int i;
1172 mm_dmapool_t *pool;
1174 for (i = 0; i < MAX_DMA_POOLS; i++) {
1176 pool = &adp->dma_pool_list[i];
1178 if (pool->handle) {
1180 if (pool->vaddr)
1181 pci_pool_free(pool->handle, pool->vaddr,
1182 pool->paddr);
1184 pci_pool_destroy(pool->handle);
1185 pool->handle = NULL;
1189 return;
1193 * mraid_mm_init : Module entry point
1195 static int __init
1196 mraid_mm_init(void)
1198 // Announce the driver version
1199 con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
1200 LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
1202 majorno = register_chrdev(0, "megadev", &lsi_fops);
1204 if (majorno < 0) {
1205 con_log(CL_ANN, ("megaraid cmm: cannot get major\n"));
1206 return majorno;
1209 init_waitqueue_head(&wait_q);
1211 INIT_LIST_HEAD(&adapters_list_g);
1213 return 0;
1218 * mraid_mm_compat_ioctl : 32bit to 64bit ioctl conversion routine
1220 #ifdef CONFIG_COMPAT
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 unregister_chrdev(majorno, "megadev");
1244 module_init(mraid_mm_init);
1245 module_exit(mraid_mm_exit);
1247 /* vi: set ts=8 sw=8 tw=78: */