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