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