Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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