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