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