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