Merge branch 'drm-edid-fixes' into drm-core-next
[pandora-kernel.git] / drivers / scsi / megaraid / megaraid_mm.c
1 /*
2  *
3  *                      Linux MegaRAID device driver
4  *
5  * Copyright (c) 2003-2004  LSI Logic Corporation.
6  *
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.
11  *
12  * FILE         : megaraid_mm.c
13  * Version      : v2.20.2.7 (Jul 16 2006)
14  *
15  * Common management module
16  */
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/smp_lock.h>
20 #include "megaraid_mm.h"
21
22
23 // Entry points for char node driver
24 static int mraid_mm_open(struct inode *, struct file *);
25 static int mraid_mm_ioctl(struct inode *, struct file *, uint, unsigned long);
26
27
28 // routines to convert to and from the old the format
29 static int mimd_to_kioc(mimd_t __user *, mraid_mmadp_t *, uioc_t *);
30 static int kioc_to_mimd(uioc_t *, mimd_t __user *);
31
32
33 // Helper functions
34 static int handle_drvrcmd(void __user *, uint8_t, int *);
35 static int lld_ioctl(mraid_mmadp_t *, uioc_t *);
36 static void ioctl_done(uioc_t *);
37 static void lld_timedout(unsigned long);
38 static void hinfo_to_cinfo(mraid_hba_info_t *, mcontroller_t *);
39 static mraid_mmadp_t *mraid_mm_get_adapter(mimd_t __user *, int *);
40 static uioc_t *mraid_mm_alloc_kioc(mraid_mmadp_t *);
41 static void mraid_mm_dealloc_kioc(mraid_mmadp_t *, uioc_t *);
42 static int mraid_mm_attach_buf(mraid_mmadp_t *, uioc_t *, int);
43 static int mraid_mm_setup_dma_pools(mraid_mmadp_t *);
44 static void mraid_mm_free_adp_resources(mraid_mmadp_t *);
45 static void mraid_mm_teardown_dma_pools(mraid_mmadp_t *);
46
47 #ifdef CONFIG_COMPAT
48 static long mraid_mm_compat_ioctl(struct file *, unsigned int, unsigned long);
49 #endif
50
51 MODULE_AUTHOR("LSI Logic Corporation");
52 MODULE_DESCRIPTION("LSI Logic Management Module");
53 MODULE_LICENSE("GPL");
54 MODULE_VERSION(LSI_COMMON_MOD_VERSION);
55
56 static int dbglevel = CL_ANN;
57 module_param_named(dlevel, dbglevel, int, 0);
58 MODULE_PARM_DESC(dlevel, "Debug level (default=0)");
59
60 EXPORT_SYMBOL(mraid_mm_register_adp);
61 EXPORT_SYMBOL(mraid_mm_unregister_adp);
62 EXPORT_SYMBOL(mraid_mm_adapter_app_handle);
63
64 static uint32_t drvr_ver        = 0x02200207;
65
66 static int adapters_count_g;
67 static struct list_head adapters_list_g;
68
69 static wait_queue_head_t wait_q;
70
71 static const struct file_operations lsi_fops = {
72         .open   = mraid_mm_open,
73         .ioctl  = mraid_mm_ioctl,
74 #ifdef CONFIG_COMPAT
75         .compat_ioctl = mraid_mm_compat_ioctl,
76 #endif
77         .owner  = THIS_MODULE,
78 };
79
80 static struct miscdevice megaraid_mm_dev = {
81         .minor  = MISC_DYNAMIC_MINOR,
82         .name   = "megadev0",
83         .fops   = &lsi_fops,
84 };
85
86 /**
87  * mraid_mm_open - open routine for char node interface
88  * @inode       : unused
89  * @filep       : unused
90  *
91  * Allow ioctl operations by apps only if they have superuser privilege.
92  */
93 static int
94 mraid_mm_open(struct inode *inode, struct file *filep)
95 {
96         /*
97          * Only allow superuser to access private ioctl interface
98          */
99         if (!capable(CAP_SYS_ADMIN)) return (-EACCES);
100
101         cycle_kernel_lock();
102         return 0;
103 }
104
105 /**
106  * mraid_mm_ioctl - module entry-point for ioctls
107  * @inode       : inode (ignored)
108  * @filep       : file operations pointer (ignored)
109  * @cmd         : ioctl command
110  * @arg         : user ioctl packet
111  */
112 static int
113 mraid_mm_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
114                                                         unsigned long arg)
115 {
116         uioc_t          *kioc;
117         char            signature[EXT_IOCTL_SIGN_SZ]    = {0};
118         int             rval;
119         mraid_mmadp_t   *adp;
120         uint8_t         old_ioctl;
121         int             drvrcmd_rval;
122         void __user *argp = (void __user *)arg;
123
124         /*
125          * Make sure only USCSICMD are issued through this interface.
126          * MIMD application would still fire different command.
127          */
128
129         if ((_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD)) {
130                 return (-EINVAL);
131         }
132
133         /*
134          * Look for signature to see if this is the new or old ioctl format.
135          */
136         if (copy_from_user(signature, argp, EXT_IOCTL_SIGN_SZ)) {
137                 con_log(CL_ANN, (KERN_WARNING
138                         "megaraid cmm: copy from usr addr failed\n"));
139                 return (-EFAULT);
140         }
141
142         if (memcmp(signature, EXT_IOCTL_SIGN, EXT_IOCTL_SIGN_SZ) == 0)
143                 old_ioctl = 0;
144         else
145                 old_ioctl = 1;
146
147         /*
148          * At present, we don't support the new ioctl packet
149          */
150         if (!old_ioctl )
151                 return (-EINVAL);
152
153         /*
154          * If it is a driver ioctl (as opposed to fw ioctls), then we can
155          * handle the command locally. rval > 0 means it is not a drvr cmd
156          */
157         rval = handle_drvrcmd(argp, old_ioctl, &drvrcmd_rval);
158
159         if (rval < 0)
160                 return rval;
161         else if (rval == 0)
162                 return drvrcmd_rval;
163
164         rval = 0;
165         if ((adp = mraid_mm_get_adapter(argp, &rval)) == NULL) {
166                 return rval;
167         }
168
169         /*
170          * Check if adapter can accept ioctl. We may have marked it offline
171          * if any previous kioc had timedout on this controller.
172          */
173         if (!adp->quiescent) {
174                 con_log(CL_ANN, (KERN_WARNING
175                         "megaraid cmm: controller cannot accept cmds due to "
176                         "earlier errors\n" ));
177                 return -EFAULT;
178         }
179
180         /*
181          * The following call will block till a kioc is available
182          */
183         kioc = mraid_mm_alloc_kioc(adp);
184
185         /*
186          * User sent the old mimd_t ioctl packet. Convert it to uioc_t.
187          */
188         if ((rval = mimd_to_kioc(argp, adp, kioc))) {
189                 mraid_mm_dealloc_kioc(adp, kioc);
190                 return rval;
191         }
192
193         kioc->done = ioctl_done;
194
195         /*
196          * Issue the IOCTL to the low level driver. After the IOCTL completes
197          * release the kioc if and only if it was _not_ timedout. If it was
198          * timedout, that means that resources are still with low level driver.
199          */
200         if ((rval = lld_ioctl(adp, kioc))) {
201
202                 if (!kioc->timedout)
203                         mraid_mm_dealloc_kioc(adp, kioc);
204
205                 return rval;
206         }
207
208         /*
209          * Convert the kioc back to user space
210          */
211         rval = kioc_to_mimd(kioc, argp);
212
213         /*
214          * Return the kioc to free pool
215          */
216         mraid_mm_dealloc_kioc(adp, kioc);
217
218         return rval;
219 }
220
221
222 /**
223  * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
224  * @umimd       : User space mimd_t ioctl packet
225  * @rval        : returned success/error status
226  *
227  * The function return value is a pointer to the located @adapter.
228  */
229 static mraid_mmadp_t *
230 mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
231 {
232         mraid_mmadp_t   *adapter;
233         mimd_t          mimd;
234         uint32_t        adapno;
235         int             iterator;
236
237
238         if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) {
239                 *rval = -EFAULT;
240                 return NULL;
241         }
242
243         adapno = GETADAP(mimd.ui.fcs.adapno);
244
245         if (adapno >= adapters_count_g) {
246                 *rval = -ENODEV;
247                 return NULL;
248         }
249
250         adapter = NULL;
251         iterator = 0;
252
253         list_for_each_entry(adapter, &adapters_list_g, list) {
254                 if (iterator++ == adapno) break;
255         }
256
257         if (!adapter) {
258                 *rval = -ENODEV;
259                 return NULL;
260         }
261
262         return adapter;
263 }
264
265 /**
266  * handle_drvrcmd - Checks if the opcode is a driver cmd and if it is, handles it.
267  * @arg         : packet sent by the user app
268  * @old_ioctl   : mimd if 1; uioc otherwise
269  * @rval        : pointer for command's returned value (not function status)
270  */
271 static int
272 handle_drvrcmd(void __user *arg, uint8_t old_ioctl, int *rval)
273 {
274         mimd_t          __user *umimd;
275         mimd_t          kmimd;
276         uint8_t         opcode;
277         uint8_t         subopcode;
278
279         if (old_ioctl)
280                 goto old_packet;
281         else
282                 goto new_packet;
283
284 new_packet:
285         return (-ENOTSUPP);
286
287 old_packet:
288         *rval = 0;
289         umimd = arg;
290
291         if (copy_from_user(&kmimd, umimd, sizeof(mimd_t)))
292                 return (-EFAULT);
293
294         opcode          = kmimd.ui.fcs.opcode;
295         subopcode       = kmimd.ui.fcs.subopcode;
296
297         /*
298          * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
299          * GET_NUMADP, then we can handle. Otherwise we should return 1 to
300          * indicate that we cannot handle this.
301          */
302         if (opcode != 0x82)
303                 return 1;
304
305         switch (subopcode) {
306
307         case MEGAIOC_QDRVRVER:
308
309                 if (copy_to_user(kmimd.data, &drvr_ver, sizeof(uint32_t)))
310                         return (-EFAULT);
311
312                 return 0;
313
314         case MEGAIOC_QNADAP:
315
316                 *rval = adapters_count_g;
317
318                 if (copy_to_user(kmimd.data, &adapters_count_g,
319                                 sizeof(uint32_t)))
320                         return (-EFAULT);
321
322                 return 0;
323
324         default:
325                 /* cannot handle */
326                 return 1;
327         }
328
329         return 0;
330 }
331
332
333 /**
334  * mimd_to_kioc - Converter from old to new ioctl format
335  * @umimd       : user space old MIMD IOCTL
336  * @adp         : adapter softstate
337  * @kioc        : kernel space new format IOCTL
338  *
339  * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
340  * new packet is in kernel space so that driver can perform operations on it
341  * freely.
342  */
343
344 static int
345 mimd_to_kioc(mimd_t __user *umimd, mraid_mmadp_t *adp, uioc_t *kioc)
346 {
347         mbox64_t                *mbox64;
348         mbox_t                  *mbox;
349         mraid_passthru_t        *pthru32;
350         uint32_t                adapno;
351         uint8_t                 opcode;
352         uint8_t                 subopcode;
353         mimd_t                  mimd;
354
355         if (copy_from_user(&mimd, umimd, sizeof(mimd_t)))
356                 return (-EFAULT);
357
358         /*
359          * Applications are not allowed to send extd pthru
360          */
361         if ((mimd.mbox[0] == MBOXCMD_PASSTHRU64) ||
362                         (mimd.mbox[0] == MBOXCMD_EXTPTHRU))
363                 return (-EINVAL);
364
365         opcode          = mimd.ui.fcs.opcode;
366         subopcode       = mimd.ui.fcs.subopcode;
367         adapno          = GETADAP(mimd.ui.fcs.adapno);
368
369         if (adapno >= adapters_count_g)
370                 return (-ENODEV);
371
372         kioc->adapno    = adapno;
373         kioc->mb_type   = MBOX_LEGACY;
374         kioc->app_type  = APPTYPE_MIMD;
375
376         switch (opcode) {
377
378         case 0x82:
379
380                 if (subopcode == MEGAIOC_QADAPINFO) {
381
382                         kioc->opcode    = GET_ADAP_INFO;
383                         kioc->data_dir  = UIOC_RD;
384                         kioc->xferlen   = sizeof(mraid_hba_info_t);
385
386                         if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
387                                 return (-ENOMEM);
388                 }
389                 else {
390                         con_log(CL_ANN, (KERN_WARNING
391                                         "megaraid cmm: Invalid subop\n"));
392                         return (-EINVAL);
393                 }
394
395                 break;
396
397         case 0x81:
398
399                 kioc->opcode            = MBOX_CMD;
400                 kioc->xferlen           = mimd.ui.fcs.length;
401                 kioc->user_data_len     = kioc->xferlen;
402                 kioc->user_data         = mimd.ui.fcs.buffer;
403
404                 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
405                         return (-ENOMEM);
406
407                 if (mimd.outlen) kioc->data_dir  = UIOC_RD;
408                 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
409
410                 break;
411
412         case 0x80:
413
414                 kioc->opcode            = MBOX_CMD;
415                 kioc->xferlen           = (mimd.outlen > mimd.inlen) ?
416                                                 mimd.outlen : mimd.inlen;
417                 kioc->user_data_len     = kioc->xferlen;
418                 kioc->user_data         = mimd.data;
419
420                 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
421                         return (-ENOMEM);
422
423                 if (mimd.outlen) kioc->data_dir  = UIOC_RD;
424                 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
425
426                 break;
427
428         default:
429                 return (-EINVAL);
430         }
431
432         /*
433          * If driver command, nothing else to do
434          */
435         if (opcode == 0x82)
436                 return 0;
437
438         /*
439          * This is a mailbox cmd; copy the mailbox from mimd
440          */
441         mbox64  = (mbox64_t *)((unsigned long)kioc->cmdbuf);
442         mbox    = &mbox64->mbox32;
443         memcpy(mbox, mimd.mbox, 14);
444
445         if (mbox->cmd != MBOXCMD_PASSTHRU) {    // regular DCMD
446
447                 mbox->xferaddr  = (uint32_t)kioc->buf_paddr;
448
449                 if (kioc->data_dir & UIOC_WR) {
450                         if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
451                                                         kioc->xferlen)) {
452                                 return (-EFAULT);
453                         }
454                 }
455
456                 return 0;
457         }
458
459         /*
460          * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
461          * Just like in above case, the beginning for memblk is treated as
462          * a mailbox. The passthru will begin at next 1K boundary. And the
463          * data will start 1K after that.
464          */
465         pthru32                 = kioc->pthru32;
466         kioc->user_pthru        = &umimd->pthru;
467         mbox->xferaddr          = (uint32_t)kioc->pthru32_h;
468
469         if (copy_from_user(pthru32, kioc->user_pthru,
470                         sizeof(mraid_passthru_t))) {
471                 return (-EFAULT);
472         }
473
474         pthru32->dataxferaddr   = kioc->buf_paddr;
475         if (kioc->data_dir & UIOC_WR) {
476                 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
477                                                 pthru32->dataxferlen)) {
478                         return (-EFAULT);
479                 }
480         }
481
482         return 0;
483 }
484
485 /**
486  * mraid_mm_attch_buf - Attach a free dma buffer for required size
487  * @adp         : Adapter softstate
488  * @kioc        : kioc that the buffer needs to be attached to
489  * @xferlen     : required length for buffer
490  *
491  * First we search for a pool with smallest buffer that is >= @xferlen. If
492  * that pool has no free buffer, we will try for the next bigger size. If none
493  * is available, we will try to allocate the smallest buffer that is >=
494  * @xferlen and attach it the pool.
495  */
496 static int
497 mraid_mm_attach_buf(mraid_mmadp_t *adp, uioc_t *kioc, int xferlen)
498 {
499         mm_dmapool_t    *pool;
500         int             right_pool = -1;
501         unsigned long   flags;
502         int             i;
503
504         kioc->pool_index        = -1;
505         kioc->buf_vaddr         = NULL;
506         kioc->buf_paddr         = 0;
507         kioc->free_buf          = 0;
508
509         /*
510          * We need xferlen amount of memory. See if we can get it from our
511          * dma pools. If we don't get exact size, we will try bigger buffer
512          */
513
514         for (i = 0; i < MAX_DMA_POOLS; i++) {
515
516                 pool = &adp->dma_pool_list[i];
517
518                 if (xferlen > pool->buf_size)
519                         continue;
520
521                 if (right_pool == -1)
522                         right_pool = i;
523
524                 spin_lock_irqsave(&pool->lock, flags);
525
526                 if (!pool->in_use) {
527
528                         pool->in_use            = 1;
529                         kioc->pool_index        = i;
530                         kioc->buf_vaddr         = pool->vaddr;
531                         kioc->buf_paddr         = pool->paddr;
532
533                         spin_unlock_irqrestore(&pool->lock, flags);
534                         return 0;
535                 }
536                 else {
537                         spin_unlock_irqrestore(&pool->lock, flags);
538                         continue;
539                 }
540         }
541
542         /*
543          * If xferlen doesn't match any of our pools, return error
544          */
545         if (right_pool == -1)
546                 return -EINVAL;
547
548         /*
549          * We did not get any buffer from the preallocated pool. Let us try
550          * to allocate one new buffer. NOTE: This is a blocking call.
551          */
552         pool = &adp->dma_pool_list[right_pool];
553
554         spin_lock_irqsave(&pool->lock, flags);
555
556         kioc->pool_index        = right_pool;
557         kioc->free_buf          = 1;
558         kioc->buf_vaddr         = pci_pool_alloc(pool->handle, GFP_KERNEL,
559                                                         &kioc->buf_paddr);
560         spin_unlock_irqrestore(&pool->lock, flags);
561
562         if (!kioc->buf_vaddr)
563                 return -ENOMEM;
564
565         return 0;
566 }
567
568 /**
569  * mraid_mm_alloc_kioc - Returns a uioc_t from free list
570  * @adp : Adapter softstate for this module
571  *
572  * The kioc_semaphore is initialized with number of kioc nodes in the
573  * free kioc pool. If the kioc pool is empty, this function blocks till
574  * a kioc becomes free.
575  */
576 static uioc_t *
577 mraid_mm_alloc_kioc(mraid_mmadp_t *adp)
578 {
579         uioc_t                  *kioc;
580         struct list_head*       head;
581         unsigned long           flags;
582
583         down(&adp->kioc_semaphore);
584
585         spin_lock_irqsave(&adp->kioc_pool_lock, flags);
586
587         head = &adp->kioc_pool;
588
589         if (list_empty(head)) {
590                 up(&adp->kioc_semaphore);
591                 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
592
593                 con_log(CL_ANN, ("megaraid cmm: kioc list empty!\n"));
594                 return NULL;
595         }
596
597         kioc = list_entry(head->next, uioc_t, list);
598         list_del_init(&kioc->list);
599
600         spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
601
602         memset((caddr_t)(unsigned long)kioc->cmdbuf, 0, sizeof(mbox64_t));
603         memset((caddr_t) kioc->pthru32, 0, sizeof(mraid_passthru_t));
604
605         kioc->buf_vaddr         = NULL;
606         kioc->buf_paddr         = 0;
607         kioc->pool_index        =-1;
608         kioc->free_buf          = 0;
609         kioc->user_data         = NULL;
610         kioc->user_data_len     = 0;
611         kioc->user_pthru        = NULL;
612         kioc->timedout          = 0;
613
614         return kioc;
615 }
616
617 /**
618  * mraid_mm_dealloc_kioc - Return kioc to free pool
619  * @adp         : Adapter softstate
620  * @kioc        : uioc_t node to be returned to free pool
621  */
622 static void
623 mraid_mm_dealloc_kioc(mraid_mmadp_t *adp, uioc_t *kioc)
624 {
625         mm_dmapool_t    *pool;
626         unsigned long   flags;
627
628         if (kioc->pool_index != -1) {
629                 pool = &adp->dma_pool_list[kioc->pool_index];
630
631                 /* This routine may be called in non-isr context also */
632                 spin_lock_irqsave(&pool->lock, flags);
633
634                 /*
635                  * While attaching the dma buffer, if we didn't get the 
636                  * required buffer from the pool, we would have allocated 
637                  * it at the run time and set the free_buf flag. We must 
638                  * free that buffer. Otherwise, just mark that the buffer is 
639                  * not in use
640                  */
641                 if (kioc->free_buf == 1)
642                         pci_pool_free(pool->handle, kioc->buf_vaddr, 
643                                                         kioc->buf_paddr);
644                 else
645                         pool->in_use = 0;
646
647                 spin_unlock_irqrestore(&pool->lock, flags);
648         }
649
650         /* Return the kioc to the free pool */
651         spin_lock_irqsave(&adp->kioc_pool_lock, flags);
652         list_add(&kioc->list, &adp->kioc_pool);
653         spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
654
655         /* increment the free kioc count */
656         up(&adp->kioc_semaphore);
657
658         return;
659 }
660
661 /**
662  * lld_ioctl - Routine to issue ioctl to low level drvr
663  * @adp         : The adapter handle
664  * @kioc        : The ioctl packet with kernel addresses
665  */
666 static int
667 lld_ioctl(mraid_mmadp_t *adp, uioc_t *kioc)
668 {
669         int                     rval;
670         struct timer_list       timer;
671         struct timer_list       *tp = NULL;
672
673         kioc->status    = -ENODATA;
674         rval            = adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE);
675
676         if (rval) return rval;
677
678         /*
679          * Start the timer
680          */
681         if (adp->timeout > 0) {
682                 tp              = &timer;
683                 init_timer(tp);
684
685                 tp->function    = lld_timedout;
686                 tp->data        = (unsigned long)kioc;
687                 tp->expires     = jiffies + adp->timeout * HZ;
688
689                 add_timer(tp);
690         }
691
692         /*
693          * Wait till the low level driver completes the ioctl. After this
694          * call, the ioctl either completed successfully or timedout.
695          */
696         wait_event(wait_q, (kioc->status != -ENODATA));
697         if (tp) {
698                 del_timer_sync(tp);
699         }
700
701         /*
702          * If the command had timedout, we mark the controller offline
703          * before returning
704          */
705         if (kioc->timedout) {
706                 adp->quiescent = 0;
707         }
708
709         return kioc->status;
710 }
711
712
713 /**
714  * ioctl_done - callback from the low level driver
715  * @kioc        : completed ioctl packet
716  */
717 static void
718 ioctl_done(uioc_t *kioc)
719 {
720         uint32_t        adapno;
721         int             iterator;
722         mraid_mmadp_t*  adapter;
723
724         /*
725          * When the kioc returns from driver, make sure it still doesn't
726          * have ENODATA in status. Otherwise, driver will hang on wait_event
727          * forever
728          */
729         if (kioc->status == -ENODATA) {
730                 con_log(CL_ANN, (KERN_WARNING
731                         "megaraid cmm: lld didn't change status!\n"));
732
733                 kioc->status = -EINVAL;
734         }
735
736         /*
737          * Check if this kioc was timedout before. If so, nobody is waiting
738          * on this kioc. We don't have to wake up anybody. Instead, we just
739          * have to free the kioc
740          */
741         if (kioc->timedout) {
742                 iterator        = 0;
743                 adapter         = NULL;
744                 adapno          = kioc->adapno;
745
746                 con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
747                                         "ioctl that was timedout before\n"));
748
749                 list_for_each_entry(adapter, &adapters_list_g, list) {
750                         if (iterator++ == adapno) break;
751                 }
752
753                 kioc->timedout = 0;
754
755                 if (adapter) {
756                         mraid_mm_dealloc_kioc( adapter, kioc );
757                 }
758         }
759         else {
760                 wake_up(&wait_q);
761         }
762 }
763
764
765 /**
766  * lld_timedout - callback from the expired timer
767  * @ptr         : ioctl packet that timed out
768  */
769 static void
770 lld_timedout(unsigned long ptr)
771 {
772         uioc_t *kioc    = (uioc_t *)ptr;
773
774         kioc->status    = -ETIME;
775         kioc->timedout  = 1;
776
777         con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n"));
778
779         wake_up(&wait_q);
780 }
781
782
783 /**
784  * kioc_to_mimd - Converter from new back to old format
785  * @kioc        : Kernel space IOCTL packet (successfully issued)
786  * @mimd        : User space MIMD packet
787  */
788 static int
789 kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd)
790 {
791         mimd_t                  kmimd;
792         uint8_t                 opcode;
793         uint8_t                 subopcode;
794
795         mbox64_t                *mbox64;
796         mraid_passthru_t        __user *upthru32;
797         mraid_passthru_t        *kpthru32;
798         mcontroller_t           cinfo;
799         mraid_hba_info_t        *hinfo;
800
801
802         if (copy_from_user(&kmimd, mimd, sizeof(mimd_t)))
803                 return (-EFAULT);
804
805         opcode          = kmimd.ui.fcs.opcode;
806         subopcode       = kmimd.ui.fcs.subopcode;
807
808         if (opcode == 0x82) {
809                 switch (subopcode) {
810
811                 case MEGAIOC_QADAPINFO:
812
813                         hinfo = (mraid_hba_info_t *)(unsigned long)
814                                         kioc->buf_vaddr;
815
816                         hinfo_to_cinfo(hinfo, &cinfo);
817
818                         if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo)))
819                                 return (-EFAULT);
820
821                         return 0;
822
823                 default:
824                         return (-EINVAL);
825                 }
826
827                 return 0;
828         }
829
830         mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
831
832         if (kioc->user_pthru) {
833
834                 upthru32 = kioc->user_pthru;
835                 kpthru32 = kioc->pthru32;
836
837                 if (copy_to_user(&upthru32->scsistatus,
838                                         &kpthru32->scsistatus,
839                                         sizeof(uint8_t))) {
840                         return (-EFAULT);
841                 }
842         }
843
844         if (kioc->user_data) {
845                 if (copy_to_user(kioc->user_data, kioc->buf_vaddr,
846                                         kioc->user_data_len)) {
847                         return (-EFAULT);
848                 }
849         }
850
851         if (copy_to_user(&mimd->mbox[17],
852                         &mbox64->mbox32.status, sizeof(uint8_t))) {
853                 return (-EFAULT);
854         }
855
856         return 0;
857 }
858
859
860 /**
861  * hinfo_to_cinfo - Convert new format hba info into old format
862  * @hinfo       : New format, more comprehensive adapter info
863  * @cinfo       : Old format adapter info to support mimd_t apps
864  */
865 static void
866 hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo)
867 {
868         if (!hinfo || !cinfo)
869                 return;
870
871         cinfo->base             = hinfo->baseport;
872         cinfo->irq              = hinfo->irq;
873         cinfo->numldrv          = hinfo->num_ldrv;
874         cinfo->pcibus           = hinfo->pci_bus;
875         cinfo->pcidev           = hinfo->pci_slot;
876         cinfo->pcifun           = PCI_FUNC(hinfo->pci_dev_fn);
877         cinfo->pciid            = hinfo->pci_device_id;
878         cinfo->pcivendor        = hinfo->pci_vendor_id;
879         cinfo->pcislot          = hinfo->pci_slot;
880         cinfo->uid              = hinfo->unique_id;
881 }
882
883
884 /**
885  * mraid_mm_register_adp - Registration routine for low level drivers
886  * @lld_adp     : Adapter objejct
887  */
888 int
889 mraid_mm_register_adp(mraid_mmadp_t *lld_adp)
890 {
891         mraid_mmadp_t   *adapter;
892         mbox64_t        *mbox_list;
893         uioc_t          *kioc;
894         uint32_t        rval;
895         int             i;
896
897
898         if (lld_adp->drvr_type != DRVRTYPE_MBOX)
899                 return (-EINVAL);
900
901         adapter = kzalloc(sizeof(mraid_mmadp_t), GFP_KERNEL);
902
903         if (!adapter)
904                 return -ENOMEM;
905
906
907         adapter->unique_id      = lld_adp->unique_id;
908         adapter->drvr_type      = lld_adp->drvr_type;
909         adapter->drvr_data      = lld_adp->drvr_data;
910         adapter->pdev           = lld_adp->pdev;
911         adapter->issue_uioc     = lld_adp->issue_uioc;
912         adapter->timeout        = lld_adp->timeout;
913         adapter->max_kioc       = lld_adp->max_kioc;
914         adapter->quiescent      = 1;
915
916         /*
917          * Allocate single blocks of memory for all required kiocs,
918          * mailboxes and passthru structures.
919          */
920         adapter->kioc_list      = kmalloc(sizeof(uioc_t) * lld_adp->max_kioc,
921                                                 GFP_KERNEL);
922         adapter->mbox_list      = kmalloc(sizeof(mbox64_t) * lld_adp->max_kioc,
923                                                 GFP_KERNEL);
924         adapter->pthru_dma_pool = pci_pool_create("megaraid mm pthru pool",
925                                                 adapter->pdev,
926                                                 sizeof(mraid_passthru_t),
927                                                 16, 0);
928
929         if (!adapter->kioc_list || !adapter->mbox_list ||
930                         !adapter->pthru_dma_pool) {
931
932                 con_log(CL_ANN, (KERN_WARNING
933                         "megaraid cmm: out of memory, %s %d\n", __func__,
934                         __LINE__));
935
936                 rval = (-ENOMEM);
937
938                 goto memalloc_error;
939         }
940
941         /*
942          * Slice kioc_list and make a kioc_pool with the individiual kiocs
943          */
944         INIT_LIST_HEAD(&adapter->kioc_pool);
945         spin_lock_init(&adapter->kioc_pool_lock);
946         sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
947
948         mbox_list       = (mbox64_t *)adapter->mbox_list;
949
950         for (i = 0; i < lld_adp->max_kioc; i++) {
951
952                 kioc            = adapter->kioc_list + i;
953                 kioc->cmdbuf    = (uint64_t)(unsigned long)(mbox_list + i);
954                 kioc->pthru32   = pci_pool_alloc(adapter->pthru_dma_pool,
955                                                 GFP_KERNEL, &kioc->pthru32_h);
956
957                 if (!kioc->pthru32) {
958
959                         con_log(CL_ANN, (KERN_WARNING
960                                 "megaraid cmm: out of memory, %s %d\n",
961                                         __func__, __LINE__));
962
963                         rval = (-ENOMEM);
964
965                         goto pthru_dma_pool_error;
966                 }
967
968                 list_add_tail(&kioc->list, &adapter->kioc_pool);
969         }
970
971         // Setup the dma pools for data buffers
972         if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
973                 goto dma_pool_error;
974         }
975
976         list_add_tail(&adapter->list, &adapters_list_g);
977
978         adapters_count_g++;
979
980         return 0;
981
982 dma_pool_error:
983         /* Do nothing */
984
985 pthru_dma_pool_error:
986
987         for (i = 0; i < lld_adp->max_kioc; i++) {
988                 kioc = adapter->kioc_list + i;
989                 if (kioc->pthru32) {
990                         pci_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
991                                 kioc->pthru32_h);
992                 }
993         }
994
995 memalloc_error:
996
997         kfree(adapter->kioc_list);
998         kfree(adapter->mbox_list);
999
1000         if (adapter->pthru_dma_pool)
1001                 pci_pool_destroy(adapter->pthru_dma_pool);
1002
1003         kfree(adapter);
1004
1005         return rval;
1006 }
1007
1008
1009 /**
1010  * mraid_mm_adapter_app_handle - return the application handle for this adapter
1011  * @unique_id   : adapter unique identifier
1012  *
1013  * For the given driver data, locate the adapter in our global list and
1014  * return the corresponding handle, which is also used by applications to
1015  * uniquely identify an adapter.
1016  *
1017  * Return adapter handle if found in the list.
1018  * Return 0 if adapter could not be located, should never happen though.
1019  */
1020 uint32_t
1021 mraid_mm_adapter_app_handle(uint32_t unique_id)
1022 {
1023         mraid_mmadp_t   *adapter;
1024         mraid_mmadp_t   *tmp;
1025         int             index = 0;
1026
1027         list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1028
1029                 if (adapter->unique_id == unique_id) {
1030
1031                         return MKADAP(index);
1032                 }
1033
1034                 index++;
1035         }
1036
1037         return 0;
1038 }
1039
1040
1041 /**
1042  * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
1043  * @adp : Adapter softstate
1044  *
1045  * We maintain a pool of dma buffers per each adapter. Each pool has one
1046  * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
1047  * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
1048  * dont' want to waste too much memory by allocating more buffers per each
1049  * pool.
1050  */
1051 static int
1052 mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
1053 {
1054         mm_dmapool_t    *pool;
1055         int             bufsize;
1056         int             i;
1057
1058         /*
1059          * Create MAX_DMA_POOLS number of pools
1060          */
1061         bufsize = MRAID_MM_INIT_BUFF_SIZE;
1062
1063         for (i = 0; i < MAX_DMA_POOLS; i++){
1064
1065                 pool = &adp->dma_pool_list[i];
1066
1067                 pool->buf_size = bufsize;
1068                 spin_lock_init(&pool->lock);
1069
1070                 pool->handle = pci_pool_create("megaraid mm data buffer",
1071                                                 adp->pdev, bufsize, 16, 0);
1072
1073                 if (!pool->handle) {
1074                         goto dma_pool_setup_error;
1075                 }
1076
1077                 pool->vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
1078                                                         &pool->paddr);
1079
1080                 if (!pool->vaddr)
1081                         goto dma_pool_setup_error;
1082
1083                 bufsize = bufsize * 2;
1084         }
1085
1086         return 0;
1087
1088 dma_pool_setup_error:
1089
1090         mraid_mm_teardown_dma_pools(adp);
1091         return (-ENOMEM);
1092 }
1093
1094
1095 /**
1096  * mraid_mm_unregister_adp - Unregister routine for low level drivers
1097  * @unique_id   : UID of the adpater
1098  *
1099  * Assumes no outstanding ioctls to llds.
1100  */
1101 int
1102 mraid_mm_unregister_adp(uint32_t unique_id)
1103 {
1104         mraid_mmadp_t   *adapter;
1105         mraid_mmadp_t   *tmp;
1106
1107         list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1108
1109
1110                 if (adapter->unique_id == unique_id) {
1111
1112                         adapters_count_g--;
1113
1114                         list_del_init(&adapter->list);
1115
1116                         mraid_mm_free_adp_resources(adapter);
1117
1118                         kfree(adapter);
1119
1120                         con_log(CL_ANN, (
1121                                 "megaraid cmm: Unregistered one adapter:%#x\n",
1122                                 unique_id));
1123
1124                         return 0;
1125                 }
1126         }
1127
1128         return (-ENODEV);
1129 }
1130
1131 /**
1132  * mraid_mm_free_adp_resources - Free adapter softstate
1133  * @adp : Adapter softstate
1134  */
1135 static void
1136 mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
1137 {
1138         uioc_t  *kioc;
1139         int     i;
1140
1141         mraid_mm_teardown_dma_pools(adp);
1142
1143         for (i = 0; i < adp->max_kioc; i++) {
1144
1145                 kioc = adp->kioc_list + i;
1146
1147                 pci_pool_free(adp->pthru_dma_pool, kioc->pthru32,
1148                                 kioc->pthru32_h);
1149         }
1150
1151         kfree(adp->kioc_list);
1152         kfree(adp->mbox_list);
1153
1154         pci_pool_destroy(adp->pthru_dma_pool);
1155
1156
1157         return;
1158 }
1159
1160
1161 /**
1162  * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
1163  * @adp : Adapter softstate
1164  */
1165 static void
1166 mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
1167 {
1168         int             i;
1169         mm_dmapool_t    *pool;
1170
1171         for (i = 0; i < MAX_DMA_POOLS; i++) {
1172
1173                 pool = &adp->dma_pool_list[i];
1174
1175                 if (pool->handle) {
1176
1177                         if (pool->vaddr)
1178                                 pci_pool_free(pool->handle, pool->vaddr,
1179                                                         pool->paddr);
1180
1181                         pci_pool_destroy(pool->handle);
1182                         pool->handle = NULL;
1183                 }
1184         }
1185
1186         return;
1187 }
1188
1189 /**
1190  * mraid_mm_init        - Module entry point
1191  */
1192 static int __init
1193 mraid_mm_init(void)
1194 {
1195         int err;
1196
1197         // Announce the driver version
1198         con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
1199                 LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
1200
1201         err = misc_register(&megaraid_mm_dev);
1202         if (err < 0) {
1203                 con_log(CL_ANN, ("megaraid cmm: cannot register misc device\n"));
1204                 return err;
1205         }
1206
1207         init_waitqueue_head(&wait_q);
1208
1209         INIT_LIST_HEAD(&adapters_list_g);
1210
1211         return 0;
1212 }
1213
1214
1215 #ifdef CONFIG_COMPAT
1216 /**
1217  * mraid_mm_compat_ioctl        - 32bit to 64bit ioctl conversion routine
1218  * @filep       : file operations pointer (ignored)
1219  * @cmd         : ioctl command
1220  * @arg         : user ioctl packet
1221  */
1222 static long
1223 mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd,
1224                       unsigned long arg)
1225 {
1226         int err;
1227
1228         err = mraid_mm_ioctl(NULL, filep, cmd, arg);
1229
1230         return err;
1231 }
1232 #endif
1233
1234 /**
1235  * mraid_mm_exit        - Module exit point
1236  */
1237 static void __exit
1238 mraid_mm_exit(void)
1239 {
1240         con_log(CL_DLEVEL1 , ("exiting common mod\n"));
1241
1242         misc_deregister(&megaraid_mm_dev);
1243 }
1244
1245 module_init(mraid_mm_init);
1246 module_exit(mraid_mm_exit);
1247
1248 /* vi: set ts=8 sw=8 tw=78: */