xref: /openbmc/linux/drivers/scsi/megaraid/megaraid_mm.c (revision 7296f62f0322d808362b21064deb34f20799c20d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  *			Linux MegaRAID device driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 2003-2004  LSI Logic Corporation.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	   This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *	   modify it under the terms of the GNU General Public License
91da177e4SLinus Torvalds  *	   as published by the Free Software Foundation; either version
101da177e4SLinus Torvalds  *	   2 of the License, or (at your option) any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * FILE		: megaraid_mm.c
130b4972d5SJu, Seokmann  * Version	: v2.20.2.7 (Jul 16 2006)
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * Common management module
161da177e4SLinus Torvalds  */
17e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
19c45d15d2SArnd Bergmann #include <linux/mutex.h>
201da177e4SLinus Torvalds #include "megaraid_mm.h"
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds // Entry points for char node driver
24c45d15d2SArnd Bergmann static DEFINE_MUTEX(mraid_mm_mutex);
251da177e4SLinus Torvalds static int mraid_mm_open(struct inode *, struct file *);
26f4927c45SArnd Bergmann static long mraid_mm_unlocked_ioctl(struct file *, uint, unsigned long);
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds // routines to convert to and from the old the format
301da177e4SLinus Torvalds static int mimd_to_kioc(mimd_t __user *, mraid_mmadp_t *, uioc_t *);
311da177e4SLinus Torvalds static int kioc_to_mimd(uioc_t *, mimd_t __user *);
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds // Helper functions
351da177e4SLinus Torvalds static int handle_drvrcmd(void __user *, uint8_t, int *);
361da177e4SLinus Torvalds static int lld_ioctl(mraid_mmadp_t *, uioc_t *);
371da177e4SLinus Torvalds static void ioctl_done(uioc_t *);
381da177e4SLinus Torvalds static void lld_timedout(unsigned long);
391da177e4SLinus Torvalds static void hinfo_to_cinfo(mraid_hba_info_t *, mcontroller_t *);
401da177e4SLinus Torvalds static mraid_mmadp_t *mraid_mm_get_adapter(mimd_t __user *, int *);
411da177e4SLinus Torvalds static uioc_t *mraid_mm_alloc_kioc(mraid_mmadp_t *);
421da177e4SLinus Torvalds static void mraid_mm_dealloc_kioc(mraid_mmadp_t *, uioc_t *);
431da177e4SLinus Torvalds static int mraid_mm_attach_buf(mraid_mmadp_t *, uioc_t *, int);
441da177e4SLinus Torvalds static int mraid_mm_setup_dma_pools(mraid_mmadp_t *);
451da177e4SLinus Torvalds static void mraid_mm_free_adp_resources(mraid_mmadp_t *);
461da177e4SLinus Torvalds static void mraid_mm_teardown_dma_pools(mraid_mmadp_t *);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
491da177e4SLinus Torvalds static long mraid_mm_compat_ioctl(struct file *, unsigned int, unsigned long);
501da177e4SLinus Torvalds #endif
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds MODULE_AUTHOR("LSI Logic Corporation");
531da177e4SLinus Torvalds MODULE_DESCRIPTION("LSI Logic Management Module");
541da177e4SLinus Torvalds MODULE_LICENSE("GPL");
551da177e4SLinus Torvalds MODULE_VERSION(LSI_COMMON_MOD_VERSION);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static int dbglevel = CL_ANN;
581da177e4SLinus Torvalds module_param_named(dlevel, dbglevel, int, 0);
591da177e4SLinus Torvalds MODULE_PARM_DESC(dlevel, "Debug level (default=0)");
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds EXPORT_SYMBOL(mraid_mm_register_adp);
621da177e4SLinus Torvalds EXPORT_SYMBOL(mraid_mm_unregister_adp);
631da177e4SLinus Torvalds EXPORT_SYMBOL(mraid_mm_adapter_app_handle);
641da177e4SLinus Torvalds 
652a4aa2c4SDavid Milburn static uint32_t drvr_ver	= 0x02200207;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds static int adapters_count_g;
681da177e4SLinus Torvalds static struct list_head adapters_list_g;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static wait_queue_head_t wait_q;
711da177e4SLinus Torvalds 
7200977a59SArjan van de Ven static const struct file_operations lsi_fops = {
731da177e4SLinus Torvalds 	.open	= mraid_mm_open,
74f4927c45SArnd Bergmann 	.unlocked_ioctl = mraid_mm_unlocked_ioctl,
751da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
761da177e4SLinus Torvalds 	.compat_ioctl = mraid_mm_compat_ioctl,
771da177e4SLinus Torvalds #endif
781da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
796038f373SArnd Bergmann 	.llseek = noop_llseek,
801da177e4SLinus Torvalds };
811da177e4SLinus Torvalds 
8290a95af8SThomas Horsten static struct miscdevice megaraid_mm_dev = {
8390a95af8SThomas Horsten 	.minor	= MISC_DYNAMIC_MINOR,
8490a95af8SThomas Horsten 	.name   = "megadev0",
8590a95af8SThomas Horsten 	.fops   = &lsi_fops,
8690a95af8SThomas Horsten };
8790a95af8SThomas Horsten 
881da177e4SLinus Torvalds /**
891da177e4SLinus Torvalds  * mraid_mm_open - open routine for char node interface
90a69b74d3SRandy Dunlap  * @inode	: unused
911da177e4SLinus Torvalds  * @filep	: unused
921da177e4SLinus Torvalds  *
93a69b74d3SRandy Dunlap  * Allow ioctl operations by apps only if they have superuser privilege.
941da177e4SLinus Torvalds  */
951da177e4SLinus Torvalds static int
961da177e4SLinus Torvalds mraid_mm_open(struct inode *inode, struct file *filep)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	/*
991da177e4SLinus Torvalds 	 * Only allow superuser to access private ioctl interface
1001da177e4SLinus Torvalds 	 */
1011da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN)) return (-EACCES);
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	return 0;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /**
1071da177e4SLinus Torvalds  * mraid_mm_ioctl - module entry-point for ioctls
1081da177e4SLinus Torvalds  * @inode	: inode (ignored)
1091da177e4SLinus Torvalds  * @filep	: file operations pointer (ignored)
1101da177e4SLinus Torvalds  * @cmd		: ioctl command
1111da177e4SLinus Torvalds  * @arg		: user ioctl packet
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds static int
114f4927c45SArnd Bergmann mraid_mm_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	uioc_t		*kioc;
1171da177e4SLinus Torvalds 	char		signature[EXT_IOCTL_SIGN_SZ]	= {0};
1181da177e4SLinus Torvalds 	int		rval;
1191da177e4SLinus Torvalds 	mraid_mmadp_t	*adp;
1201da177e4SLinus Torvalds 	uint8_t		old_ioctl;
1211da177e4SLinus Torvalds 	int		drvrcmd_rval;
1221da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	/*
1251da177e4SLinus Torvalds 	 * Make sure only USCSICMD are issued through this interface.
1261da177e4SLinus Torvalds 	 * MIMD application would still fire different command.
1271da177e4SLinus Torvalds 	 */
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	if ((_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD)) {
1301da177e4SLinus Torvalds 		return (-EINVAL);
1311da177e4SLinus Torvalds 	}
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	/*
1341da177e4SLinus Torvalds 	 * Look for signature to see if this is the new or old ioctl format.
1351da177e4SLinus Torvalds 	 */
1361da177e4SLinus Torvalds 	if (copy_from_user(signature, argp, EXT_IOCTL_SIGN_SZ)) {
1371da177e4SLinus Torvalds 		con_log(CL_ANN, (KERN_WARNING
1381da177e4SLinus Torvalds 			"megaraid cmm: copy from usr addr failed\n"));
1391da177e4SLinus Torvalds 		return (-EFAULT);
1401da177e4SLinus Torvalds 	}
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	if (memcmp(signature, EXT_IOCTL_SIGN, EXT_IOCTL_SIGN_SZ) == 0)
1431da177e4SLinus Torvalds 		old_ioctl = 0;
1441da177e4SLinus Torvalds 	else
1451da177e4SLinus Torvalds 		old_ioctl = 1;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	/*
1481da177e4SLinus Torvalds 	 * At present, we don't support the new ioctl packet
1491da177e4SLinus Torvalds 	 */
1501da177e4SLinus Torvalds 	if (!old_ioctl )
1511da177e4SLinus Torvalds 		return (-EINVAL);
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	/*
1541da177e4SLinus Torvalds 	 * If it is a driver ioctl (as opposed to fw ioctls), then we can
1551da177e4SLinus Torvalds 	 * handle the command locally. rval > 0 means it is not a drvr cmd
1561da177e4SLinus Torvalds 	 */
1571da177e4SLinus Torvalds 	rval = handle_drvrcmd(argp, old_ioctl, &drvrcmd_rval);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	if (rval < 0)
1601da177e4SLinus Torvalds 		return rval;
1611da177e4SLinus Torvalds 	else if (rval == 0)
1621da177e4SLinus Torvalds 		return drvrcmd_rval;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	rval = 0;
1651da177e4SLinus Torvalds 	if ((adp = mraid_mm_get_adapter(argp, &rval)) == NULL) {
1661da177e4SLinus Torvalds 		return rval;
1671da177e4SLinus Torvalds 	}
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	/*
1701da177e4SLinus Torvalds 	 * Check if adapter can accept ioctl. We may have marked it offline
1711da177e4SLinus Torvalds 	 * if any previous kioc had timedout on this controller.
1721da177e4SLinus Torvalds 	 */
1731da177e4SLinus Torvalds 	if (!adp->quiescent) {
1741da177e4SLinus Torvalds 		con_log(CL_ANN, (KERN_WARNING
1751da177e4SLinus Torvalds 			"megaraid cmm: controller cannot accept cmds due to "
1761da177e4SLinus Torvalds 			"earlier errors\n" ));
1771da177e4SLinus Torvalds 		return -EFAULT;
1781da177e4SLinus Torvalds 	}
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	/*
1811da177e4SLinus Torvalds 	 * The following call will block till a kioc is available
182*7296f62fSNicholas Krause 	 * or return NULL if the list head is empty for the pointer
183*7296f62fSNicholas Krause 	 * of type mraid_mmapt passed to mraid_mm_alloc_kioc
1841da177e4SLinus Torvalds 	 */
1851da177e4SLinus Torvalds 	kioc = mraid_mm_alloc_kioc(adp);
186*7296f62fSNicholas Krause 	if (!kioc)
187*7296f62fSNicholas Krause 		return -ENXIO;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	/*
1901da177e4SLinus Torvalds 	 * User sent the old mimd_t ioctl packet. Convert it to uioc_t.
1911da177e4SLinus Torvalds 	 */
1921da177e4SLinus Torvalds 	if ((rval = mimd_to_kioc(argp, adp, kioc))) {
1931da177e4SLinus Torvalds 		mraid_mm_dealloc_kioc(adp, kioc);
1941da177e4SLinus Torvalds 		return rval;
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	kioc->done = ioctl_done;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	/*
2001da177e4SLinus Torvalds 	 * Issue the IOCTL to the low level driver. After the IOCTL completes
2011da177e4SLinus Torvalds 	 * release the kioc if and only if it was _not_ timedout. If it was
2021da177e4SLinus Torvalds 	 * timedout, that means that resources are still with low level driver.
2031da177e4SLinus Torvalds 	 */
2041da177e4SLinus Torvalds 	if ((rval = lld_ioctl(adp, kioc))) {
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 		if (!kioc->timedout)
2071da177e4SLinus Torvalds 			mraid_mm_dealloc_kioc(adp, kioc);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 		return rval;
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	/*
2131da177e4SLinus Torvalds 	 * Convert the kioc back to user space
2141da177e4SLinus Torvalds 	 */
2151da177e4SLinus Torvalds 	rval = kioc_to_mimd(kioc, argp);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/*
2181da177e4SLinus Torvalds 	 * Return the kioc to free pool
2191da177e4SLinus Torvalds 	 */
2201da177e4SLinus Torvalds 	mraid_mm_dealloc_kioc(adp, kioc);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	return rval;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
225f4927c45SArnd Bergmann static long
226f4927c45SArnd Bergmann mraid_mm_unlocked_ioctl(struct file *filep, unsigned int cmd,
227f4927c45SArnd Bergmann 		        unsigned long arg)
228f4927c45SArnd Bergmann {
229f4927c45SArnd Bergmann 	int err;
230f4927c45SArnd Bergmann 
231a34f0b31SUwe Kleine-König 	/* inconsistent: mraid_mm_compat_ioctl doesn't take the BKL */
232c45d15d2SArnd Bergmann 	mutex_lock(&mraid_mm_mutex);
233f4927c45SArnd Bergmann 	err = mraid_mm_ioctl(filep, cmd, arg);
234c45d15d2SArnd Bergmann 	mutex_unlock(&mraid_mm_mutex);
235f4927c45SArnd Bergmann 
236f4927c45SArnd Bergmann 	return err;
237f4927c45SArnd Bergmann }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /**
2401da177e4SLinus Torvalds  * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
2411da177e4SLinus Torvalds  * @umimd	: User space mimd_t ioctl packet
242a69b74d3SRandy Dunlap  * @rval	: returned success/error status
243a69b74d3SRandy Dunlap  *
244a69b74d3SRandy Dunlap  * The function return value is a pointer to the located @adapter.
2451da177e4SLinus Torvalds  */
2461da177e4SLinus Torvalds static mraid_mmadp_t *
2471da177e4SLinus Torvalds mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	mraid_mmadp_t	*adapter;
2501da177e4SLinus Torvalds 	mimd_t		mimd;
2511da177e4SLinus Torvalds 	uint32_t	adapno;
2521da177e4SLinus Torvalds 	int		iterator;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) {
2561da177e4SLinus Torvalds 		*rval = -EFAULT;
2571da177e4SLinus Torvalds 		return NULL;
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	adapno = GETADAP(mimd.ui.fcs.adapno);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	if (adapno >= adapters_count_g) {
2631da177e4SLinus Torvalds 		*rval = -ENODEV;
2641da177e4SLinus Torvalds 		return NULL;
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	adapter = NULL;
2681da177e4SLinus Torvalds 	iterator = 0;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	list_for_each_entry(adapter, &adapters_list_g, list) {
2711da177e4SLinus Torvalds 		if (iterator++ == adapno) break;
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	if (!adapter) {
2751da177e4SLinus Torvalds 		*rval = -ENODEV;
2761da177e4SLinus Torvalds 		return NULL;
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	return adapter;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
282a69b74d3SRandy Dunlap /**
283a69b74d3SRandy Dunlap  * handle_drvrcmd - Checks if the opcode is a driver cmd and if it is, handles it.
2841da177e4SLinus Torvalds  * @arg		: packet sent by the user app
2851da177e4SLinus Torvalds  * @old_ioctl	: mimd if 1; uioc otherwise
286a69b74d3SRandy Dunlap  * @rval	: pointer for command's returned value (not function status)
2871da177e4SLinus Torvalds  */
2881da177e4SLinus Torvalds static int
2891da177e4SLinus Torvalds handle_drvrcmd(void __user *arg, uint8_t old_ioctl, int *rval)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	mimd_t		__user *umimd;
2921da177e4SLinus Torvalds 	mimd_t		kmimd;
2931da177e4SLinus Torvalds 	uint8_t		opcode;
2941da177e4SLinus Torvalds 	uint8_t		subopcode;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	if (old_ioctl)
2971da177e4SLinus Torvalds 		goto old_packet;
2981da177e4SLinus Torvalds 	else
2991da177e4SLinus Torvalds 		goto new_packet;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds new_packet:
3021da177e4SLinus Torvalds 	return (-ENOTSUPP);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds old_packet:
3051da177e4SLinus Torvalds 	*rval = 0;
3061da177e4SLinus Torvalds 	umimd = arg;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	if (copy_from_user(&kmimd, umimd, sizeof(mimd_t)))
3091da177e4SLinus Torvalds 		return (-EFAULT);
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	opcode		= kmimd.ui.fcs.opcode;
3121da177e4SLinus Torvalds 	subopcode	= kmimd.ui.fcs.subopcode;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	/*
3151da177e4SLinus Torvalds 	 * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
3161da177e4SLinus Torvalds 	 * GET_NUMADP, then we can handle. Otherwise we should return 1 to
3171da177e4SLinus Torvalds 	 * indicate that we cannot handle this.
3181da177e4SLinus Torvalds 	 */
3191da177e4SLinus Torvalds 	if (opcode != 0x82)
3201da177e4SLinus Torvalds 		return 1;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	switch (subopcode) {
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	case MEGAIOC_QDRVRVER:
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 		if (copy_to_user(kmimd.data, &drvr_ver, sizeof(uint32_t)))
3271da177e4SLinus Torvalds 			return (-EFAULT);
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 		return 0;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	case MEGAIOC_QNADAP:
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 		*rval = adapters_count_g;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 		if (copy_to_user(kmimd.data, &adapters_count_g,
3361da177e4SLinus Torvalds 				sizeof(uint32_t)))
3371da177e4SLinus Torvalds 			return (-EFAULT);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 		return 0;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	default:
3421da177e4SLinus Torvalds 		/* cannot handle */
3431da177e4SLinus Torvalds 		return 1;
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	return 0;
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds /**
3511da177e4SLinus Torvalds  * mimd_to_kioc	- Converter from old to new ioctl format
3521da177e4SLinus Torvalds  * @umimd	: user space old MIMD IOCTL
353a69b74d3SRandy Dunlap  * @adp		: adapter softstate
3541da177e4SLinus Torvalds  * @kioc	: kernel space new format IOCTL
3551da177e4SLinus Torvalds  *
3561da177e4SLinus Torvalds  * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
3571da177e4SLinus Torvalds  * new packet is in kernel space so that driver can perform operations on it
3581da177e4SLinus Torvalds  * freely.
3591da177e4SLinus Torvalds  */
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds static int
3621da177e4SLinus Torvalds mimd_to_kioc(mimd_t __user *umimd, mraid_mmadp_t *adp, uioc_t *kioc)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	mbox64_t		*mbox64;
3651da177e4SLinus Torvalds 	mbox_t			*mbox;
3661da177e4SLinus Torvalds 	mraid_passthru_t	*pthru32;
3671da177e4SLinus Torvalds 	uint32_t		adapno;
3681da177e4SLinus Torvalds 	uint8_t			opcode;
3691da177e4SLinus Torvalds 	uint8_t			subopcode;
3701da177e4SLinus Torvalds 	mimd_t			mimd;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	if (copy_from_user(&mimd, umimd, sizeof(mimd_t)))
3731da177e4SLinus Torvalds 		return (-EFAULT);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	/*
3761da177e4SLinus Torvalds 	 * Applications are not allowed to send extd pthru
3771da177e4SLinus Torvalds 	 */
3781da177e4SLinus Torvalds 	if ((mimd.mbox[0] == MBOXCMD_PASSTHRU64) ||
3791da177e4SLinus Torvalds 			(mimd.mbox[0] == MBOXCMD_EXTPTHRU))
3801da177e4SLinus Torvalds 		return (-EINVAL);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	opcode		= mimd.ui.fcs.opcode;
3831da177e4SLinus Torvalds 	subopcode	= mimd.ui.fcs.subopcode;
3841da177e4SLinus Torvalds 	adapno		= GETADAP(mimd.ui.fcs.adapno);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	if (adapno >= adapters_count_g)
3871da177e4SLinus Torvalds 		return (-ENODEV);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	kioc->adapno	= adapno;
3901da177e4SLinus Torvalds 	kioc->mb_type	= MBOX_LEGACY;
3911da177e4SLinus Torvalds 	kioc->app_type	= APPTYPE_MIMD;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	switch (opcode) {
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	case 0x82:
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 		if (subopcode == MEGAIOC_QADAPINFO) {
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 			kioc->opcode	= GET_ADAP_INFO;
4001da177e4SLinus Torvalds 			kioc->data_dir	= UIOC_RD;
4011da177e4SLinus Torvalds 			kioc->xferlen	= sizeof(mraid_hba_info_t);
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 			if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
4041da177e4SLinus Torvalds 				return (-ENOMEM);
4051da177e4SLinus Torvalds 		}
4061da177e4SLinus Torvalds 		else {
4071da177e4SLinus Torvalds 			con_log(CL_ANN, (KERN_WARNING
4081da177e4SLinus Torvalds 					"megaraid cmm: Invalid subop\n"));
4091da177e4SLinus Torvalds 			return (-EINVAL);
4101da177e4SLinus Torvalds 		}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 		break;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	case 0x81:
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 		kioc->opcode		= MBOX_CMD;
4171da177e4SLinus Torvalds 		kioc->xferlen		= mimd.ui.fcs.length;
4181da177e4SLinus Torvalds 		kioc->user_data_len	= kioc->xferlen;
4191da177e4SLinus Torvalds 		kioc->user_data		= mimd.ui.fcs.buffer;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 		if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
4221da177e4SLinus Torvalds 			return (-ENOMEM);
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 		if (mimd.outlen) kioc->data_dir  = UIOC_RD;
4251da177e4SLinus Torvalds 		if (mimd.inlen) kioc->data_dir |= UIOC_WR;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 		break;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	case 0x80:
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 		kioc->opcode		= MBOX_CMD;
4321da177e4SLinus Torvalds 		kioc->xferlen		= (mimd.outlen > mimd.inlen) ?
4331da177e4SLinus Torvalds 						mimd.outlen : mimd.inlen;
4341da177e4SLinus Torvalds 		kioc->user_data_len	= kioc->xferlen;
4351da177e4SLinus Torvalds 		kioc->user_data		= mimd.data;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 		if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
4381da177e4SLinus Torvalds 			return (-ENOMEM);
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 		if (mimd.outlen) kioc->data_dir  = UIOC_RD;
4411da177e4SLinus Torvalds 		if (mimd.inlen) kioc->data_dir |= UIOC_WR;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 		break;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	default:
4461da177e4SLinus Torvalds 		return (-EINVAL);
4471da177e4SLinus Torvalds 	}
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	/*
4501da177e4SLinus Torvalds 	 * If driver command, nothing else to do
4511da177e4SLinus Torvalds 	 */
4521da177e4SLinus Torvalds 	if (opcode == 0x82)
4531da177e4SLinus Torvalds 		return 0;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	/*
4561da177e4SLinus Torvalds 	 * This is a mailbox cmd; copy the mailbox from mimd
4571da177e4SLinus Torvalds 	 */
4581da177e4SLinus Torvalds 	mbox64	= (mbox64_t *)((unsigned long)kioc->cmdbuf);
4591da177e4SLinus Torvalds 	mbox	= &mbox64->mbox32;
4601da177e4SLinus Torvalds 	memcpy(mbox, mimd.mbox, 14);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	if (mbox->cmd != MBOXCMD_PASSTHRU) {	// regular DCMD
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 		mbox->xferaddr	= (uint32_t)kioc->buf_paddr;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 		if (kioc->data_dir & UIOC_WR) {
4671da177e4SLinus Torvalds 			if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
4681da177e4SLinus Torvalds 							kioc->xferlen)) {
4691da177e4SLinus Torvalds 				return (-EFAULT);
4701da177e4SLinus Torvalds 			}
4711da177e4SLinus Torvalds 		}
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 		return 0;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	/*
4771da177e4SLinus Torvalds 	 * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
4781da177e4SLinus Torvalds 	 * Just like in above case, the beginning for memblk is treated as
4791da177e4SLinus Torvalds 	 * a mailbox. The passthru will begin at next 1K boundary. And the
4801da177e4SLinus Torvalds 	 * data will start 1K after that.
4811da177e4SLinus Torvalds 	 */
4821da177e4SLinus Torvalds 	pthru32			= kioc->pthru32;
4831da177e4SLinus Torvalds 	kioc->user_pthru	= &umimd->pthru;
4841da177e4SLinus Torvalds 	mbox->xferaddr		= (uint32_t)kioc->pthru32_h;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	if (copy_from_user(pthru32, kioc->user_pthru,
4871da177e4SLinus Torvalds 			sizeof(mraid_passthru_t))) {
4881da177e4SLinus Torvalds 		return (-EFAULT);
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	pthru32->dataxferaddr	= kioc->buf_paddr;
4921da177e4SLinus Torvalds 	if (kioc->data_dir & UIOC_WR) {
4933de22601SDan Carpenter 		if (pthru32->dataxferlen > kioc->xferlen)
4943de22601SDan Carpenter 			return -EINVAL;
4951da177e4SLinus Torvalds 		if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
4961da177e4SLinus Torvalds 						pthru32->dataxferlen)) {
4971da177e4SLinus Torvalds 			return (-EFAULT);
4981da177e4SLinus Torvalds 		}
4991da177e4SLinus Torvalds 	}
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	return 0;
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds /**
5051da177e4SLinus Torvalds  * mraid_mm_attch_buf - Attach a free dma buffer for required size
5061da177e4SLinus Torvalds  * @adp		: Adapter softstate
5071da177e4SLinus Torvalds  * @kioc	: kioc that the buffer needs to be attached to
5081da177e4SLinus Torvalds  * @xferlen	: required length for buffer
5091da177e4SLinus Torvalds  *
5101da177e4SLinus Torvalds  * First we search for a pool with smallest buffer that is >= @xferlen. If
5111da177e4SLinus Torvalds  * that pool has no free buffer, we will try for the next bigger size. If none
5121da177e4SLinus Torvalds  * is available, we will try to allocate the smallest buffer that is >=
5131da177e4SLinus Torvalds  * @xferlen and attach it the pool.
5141da177e4SLinus Torvalds  */
5151da177e4SLinus Torvalds static int
5161da177e4SLinus Torvalds mraid_mm_attach_buf(mraid_mmadp_t *adp, uioc_t *kioc, int xferlen)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	mm_dmapool_t	*pool;
5191da177e4SLinus Torvalds 	int		right_pool = -1;
5201da177e4SLinus Torvalds 	unsigned long	flags;
5211da177e4SLinus Torvalds 	int		i;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	kioc->pool_index	= -1;
5241da177e4SLinus Torvalds 	kioc->buf_vaddr		= NULL;
5251da177e4SLinus Torvalds 	kioc->buf_paddr		= 0;
5261da177e4SLinus Torvalds 	kioc->free_buf		= 0;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/*
5291da177e4SLinus Torvalds 	 * We need xferlen amount of memory. See if we can get it from our
5301da177e4SLinus Torvalds 	 * dma pools. If we don't get exact size, we will try bigger buffer
5311da177e4SLinus Torvalds 	 */
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	for (i = 0; i < MAX_DMA_POOLS; i++) {
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 		pool = &adp->dma_pool_list[i];
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 		if (xferlen > pool->buf_size)
5381da177e4SLinus Torvalds 			continue;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 		if (right_pool == -1)
5411da177e4SLinus Torvalds 			right_pool = i;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 		spin_lock_irqsave(&pool->lock, flags);
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 		if (!pool->in_use) {
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 			pool->in_use		= 1;
5481da177e4SLinus Torvalds 			kioc->pool_index	= i;
5491da177e4SLinus Torvalds 			kioc->buf_vaddr		= pool->vaddr;
5501da177e4SLinus Torvalds 			kioc->buf_paddr		= pool->paddr;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 			spin_unlock_irqrestore(&pool->lock, flags);
5531da177e4SLinus Torvalds 			return 0;
5541da177e4SLinus Torvalds 		}
5551da177e4SLinus Torvalds 		else {
5561da177e4SLinus Torvalds 			spin_unlock_irqrestore(&pool->lock, flags);
5571da177e4SLinus Torvalds 			continue;
5581da177e4SLinus Torvalds 		}
5591da177e4SLinus Torvalds 	}
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	/*
5621da177e4SLinus Torvalds 	 * If xferlen doesn't match any of our pools, return error
5631da177e4SLinus Torvalds 	 */
5641da177e4SLinus Torvalds 	if (right_pool == -1)
5651da177e4SLinus Torvalds 		return -EINVAL;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	/*
5681da177e4SLinus Torvalds 	 * We did not get any buffer from the preallocated pool. Let us try
5691da177e4SLinus Torvalds 	 * to allocate one new buffer. NOTE: This is a blocking call.
5701da177e4SLinus Torvalds 	 */
5711da177e4SLinus Torvalds 	pool = &adp->dma_pool_list[right_pool];
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	spin_lock_irqsave(&pool->lock, flags);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	kioc->pool_index	= right_pool;
5761da177e4SLinus Torvalds 	kioc->free_buf		= 1;
5771da177e4SLinus Torvalds 	kioc->buf_vaddr 	= pci_pool_alloc(pool->handle, GFP_KERNEL,
5781da177e4SLinus Torvalds 							&kioc->buf_paddr);
5791da177e4SLinus Torvalds 	spin_unlock_irqrestore(&pool->lock, flags);
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	if (!kioc->buf_vaddr)
5821da177e4SLinus Torvalds 		return -ENOMEM;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	return 0;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds /**
5881da177e4SLinus Torvalds  * mraid_mm_alloc_kioc - Returns a uioc_t from free list
5891da177e4SLinus Torvalds  * @adp	: Adapter softstate for this module
5901da177e4SLinus Torvalds  *
5911da177e4SLinus Torvalds  * The kioc_semaphore is initialized with number of kioc nodes in the
5921da177e4SLinus Torvalds  * free kioc pool. If the kioc pool is empty, this function blocks till
5931da177e4SLinus Torvalds  * a kioc becomes free.
5941da177e4SLinus Torvalds  */
5951da177e4SLinus Torvalds static uioc_t *
5961da177e4SLinus Torvalds mraid_mm_alloc_kioc(mraid_mmadp_t *adp)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	uioc_t			*kioc;
5991da177e4SLinus Torvalds 	struct list_head*	head;
6001da177e4SLinus Torvalds 	unsigned long		flags;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	down(&adp->kioc_semaphore);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	spin_lock_irqsave(&adp->kioc_pool_lock, flags);
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	head = &adp->kioc_pool;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	if (list_empty(head)) {
6091da177e4SLinus Torvalds 		up(&adp->kioc_semaphore);
6101da177e4SLinus Torvalds 		spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 		con_log(CL_ANN, ("megaraid cmm: kioc list empty!\n"));
6131da177e4SLinus Torvalds 		return NULL;
6141da177e4SLinus Torvalds 	}
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	kioc = list_entry(head->next, uioc_t, list);
6171da177e4SLinus Torvalds 	list_del_init(&kioc->list);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	memset((caddr_t)(unsigned long)kioc->cmdbuf, 0, sizeof(mbox64_t));
6221da177e4SLinus Torvalds 	memset((caddr_t) kioc->pthru32, 0, sizeof(mraid_passthru_t));
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	kioc->buf_vaddr		= NULL;
6251da177e4SLinus Torvalds 	kioc->buf_paddr		= 0;
6261da177e4SLinus Torvalds 	kioc->pool_index	=-1;
6271da177e4SLinus Torvalds 	kioc->free_buf		= 0;
6281da177e4SLinus Torvalds 	kioc->user_data		= NULL;
6291da177e4SLinus Torvalds 	kioc->user_data_len	= 0;
6301da177e4SLinus Torvalds 	kioc->user_pthru	= NULL;
6311da177e4SLinus Torvalds 	kioc->timedout		= 0;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	return kioc;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds /**
6371da177e4SLinus Torvalds  * mraid_mm_dealloc_kioc - Return kioc to free pool
6381da177e4SLinus Torvalds  * @adp		: Adapter softstate
6391da177e4SLinus Torvalds  * @kioc	: uioc_t node to be returned to free pool
6401da177e4SLinus Torvalds  */
6411da177e4SLinus Torvalds static void
6421da177e4SLinus Torvalds mraid_mm_dealloc_kioc(mraid_mmadp_t *adp, uioc_t *kioc)
6431da177e4SLinus Torvalds {
6441da177e4SLinus Torvalds 	mm_dmapool_t	*pool;
6451da177e4SLinus Torvalds 	unsigned long	flags;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	if (kioc->pool_index != -1) {
6481da177e4SLinus Torvalds 		pool = &adp->dma_pool_list[kioc->pool_index];
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 		/* This routine may be called in non-isr context also */
6511da177e4SLinus Torvalds 		spin_lock_irqsave(&pool->lock, flags);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 		/*
6541da177e4SLinus Torvalds 		 * While attaching the dma buffer, if we didn't get the
6551da177e4SLinus Torvalds 		 * required buffer from the pool, we would have allocated
6561da177e4SLinus Torvalds 		 * it at the run time and set the free_buf flag. We must
6571da177e4SLinus Torvalds 		 * free that buffer. Otherwise, just mark that the buffer is
6581da177e4SLinus Torvalds 		 * not in use
6591da177e4SLinus Torvalds 		 */
6601da177e4SLinus Torvalds 		if (kioc->free_buf == 1)
6611da177e4SLinus Torvalds 			pci_pool_free(pool->handle, kioc->buf_vaddr,
6621da177e4SLinus Torvalds 							kioc->buf_paddr);
6631da177e4SLinus Torvalds 		else
6641da177e4SLinus Torvalds 			pool->in_use = 0;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pool->lock, flags);
6671da177e4SLinus Torvalds 	}
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	/* Return the kioc to the free pool */
6701da177e4SLinus Torvalds 	spin_lock_irqsave(&adp->kioc_pool_lock, flags);
6711da177e4SLinus Torvalds 	list_add(&kioc->list, &adp->kioc_pool);
6721da177e4SLinus Torvalds 	spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	/* increment the free kioc count */
6751da177e4SLinus Torvalds 	up(&adp->kioc_semaphore);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	return;
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds /**
6811da177e4SLinus Torvalds  * lld_ioctl - Routine to issue ioctl to low level drvr
6821da177e4SLinus Torvalds  * @adp		: The adapter handle
6831da177e4SLinus Torvalds  * @kioc	: The ioctl packet with kernel addresses
6841da177e4SLinus Torvalds  */
6851da177e4SLinus Torvalds static int
6861da177e4SLinus Torvalds lld_ioctl(mraid_mmadp_t *adp, uioc_t *kioc)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	int			rval;
6891da177e4SLinus Torvalds 	struct timer_list	timer;
6901da177e4SLinus Torvalds 	struct timer_list	*tp = NULL;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	kioc->status	= -ENODATA;
6931da177e4SLinus Torvalds 	rval		= adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE);
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	if (rval) return rval;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	/*
6981da177e4SLinus Torvalds 	 * Start the timer
6991da177e4SLinus Torvalds 	 */
7001da177e4SLinus Torvalds 	if (adp->timeout > 0) {
7011da177e4SLinus Torvalds 		tp		= &timer;
7021da177e4SLinus Torvalds 		init_timer(tp);
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 		tp->function	= lld_timedout;
7051da177e4SLinus Torvalds 		tp->data	= (unsigned long)kioc;
7061da177e4SLinus Torvalds 		tp->expires	= jiffies + adp->timeout * HZ;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 		add_timer(tp);
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	/*
7121da177e4SLinus Torvalds 	 * Wait till the low level driver completes the ioctl. After this
7131da177e4SLinus Torvalds 	 * call, the ioctl either completed successfully or timedout.
7141da177e4SLinus Torvalds 	 */
7151da177e4SLinus Torvalds 	wait_event(wait_q, (kioc->status != -ENODATA));
7161da177e4SLinus Torvalds 	if (tp) {
7171da177e4SLinus Torvalds 		del_timer_sync(tp);
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	/*
7211da177e4SLinus Torvalds 	 * If the command had timedout, we mark the controller offline
7221da177e4SLinus Torvalds 	 * before returning
7231da177e4SLinus Torvalds 	 */
7241da177e4SLinus Torvalds 	if (kioc->timedout) {
7251da177e4SLinus Torvalds 		adp->quiescent = 0;
7261da177e4SLinus Torvalds 	}
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	return kioc->status;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds /**
7331da177e4SLinus Torvalds  * ioctl_done - callback from the low level driver
7341da177e4SLinus Torvalds  * @kioc	: completed ioctl packet
7351da177e4SLinus Torvalds  */
7361da177e4SLinus Torvalds static void
7371da177e4SLinus Torvalds ioctl_done(uioc_t *kioc)
7381da177e4SLinus Torvalds {
7391da177e4SLinus Torvalds 	uint32_t	adapno;
7401da177e4SLinus Torvalds 	int		iterator;
7411da177e4SLinus Torvalds 	mraid_mmadp_t*	adapter;
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	/*
7441da177e4SLinus Torvalds 	 * When the kioc returns from driver, make sure it still doesn't
7451da177e4SLinus Torvalds 	 * have ENODATA in status. Otherwise, driver will hang on wait_event
7461da177e4SLinus Torvalds 	 * forever
7471da177e4SLinus Torvalds 	 */
7481da177e4SLinus Torvalds 	if (kioc->status == -ENODATA) {
7491da177e4SLinus Torvalds 		con_log(CL_ANN, (KERN_WARNING
7501da177e4SLinus Torvalds 			"megaraid cmm: lld didn't change status!\n"));
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 		kioc->status = -EINVAL;
7531da177e4SLinus Torvalds 	}
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	/*
7561da177e4SLinus Torvalds 	 * Check if this kioc was timedout before. If so, nobody is waiting
7571da177e4SLinus Torvalds 	 * on this kioc. We don't have to wake up anybody. Instead, we just
7581da177e4SLinus Torvalds 	 * have to free the kioc
7591da177e4SLinus Torvalds 	 */
7601da177e4SLinus Torvalds 	if (kioc->timedout) {
7611da177e4SLinus Torvalds 		iterator	= 0;
7621da177e4SLinus Torvalds 		adapter		= NULL;
7631da177e4SLinus Torvalds 		adapno		= kioc->adapno;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 		con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
7661da177e4SLinus Torvalds 					"ioctl that was timedout before\n"));
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 		list_for_each_entry(adapter, &adapters_list_g, list) {
7691da177e4SLinus Torvalds 			if (iterator++ == adapno) break;
7701da177e4SLinus Torvalds 		}
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 		kioc->timedout = 0;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 		if (adapter) {
7751da177e4SLinus Torvalds 			mraid_mm_dealloc_kioc( adapter, kioc );
7761da177e4SLinus Torvalds 		}
7771da177e4SLinus Torvalds 	}
7781da177e4SLinus Torvalds 	else {
7791da177e4SLinus Torvalds 		wake_up(&wait_q);
7801da177e4SLinus Torvalds 	}
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 
784a69b74d3SRandy Dunlap /**
785a69b74d3SRandy Dunlap  * lld_timedout	- callback from the expired timer
7861da177e4SLinus Torvalds  * @ptr		: ioctl packet that timed out
7871da177e4SLinus Torvalds  */
7881da177e4SLinus Torvalds static void
7891da177e4SLinus Torvalds lld_timedout(unsigned long ptr)
7901da177e4SLinus Torvalds {
7911da177e4SLinus Torvalds 	uioc_t *kioc	= (uioc_t *)ptr;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	kioc->status 	= -ETIME;
7941da177e4SLinus Torvalds 	kioc->timedout	= 1;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n"));
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	wake_up(&wait_q);
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds /**
803a69b74d3SRandy Dunlap  * kioc_to_mimd	- Converter from new back to old format
8041da177e4SLinus Torvalds  * @kioc	: Kernel space IOCTL packet (successfully issued)
8051da177e4SLinus Torvalds  * @mimd	: User space MIMD packet
8061da177e4SLinus Torvalds  */
8071da177e4SLinus Torvalds static int
8081da177e4SLinus Torvalds kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd)
8091da177e4SLinus Torvalds {
8101da177e4SLinus Torvalds 	mimd_t			kmimd;
8111da177e4SLinus Torvalds 	uint8_t			opcode;
8121da177e4SLinus Torvalds 	uint8_t			subopcode;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	mbox64_t		*mbox64;
8151da177e4SLinus Torvalds 	mraid_passthru_t	__user *upthru32;
8161da177e4SLinus Torvalds 	mraid_passthru_t	*kpthru32;
8171da177e4SLinus Torvalds 	mcontroller_t		cinfo;
8181da177e4SLinus Torvalds 	mraid_hba_info_t	*hinfo;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	if (copy_from_user(&kmimd, mimd, sizeof(mimd_t)))
8221da177e4SLinus Torvalds 		return (-EFAULT);
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	opcode		= kmimd.ui.fcs.opcode;
8251da177e4SLinus Torvalds 	subopcode	= kmimd.ui.fcs.subopcode;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	if (opcode == 0x82) {
8281da177e4SLinus Torvalds 		switch (subopcode) {
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 		case MEGAIOC_QADAPINFO:
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 			hinfo = (mraid_hba_info_t *)(unsigned long)
8331da177e4SLinus Torvalds 					kioc->buf_vaddr;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 			hinfo_to_cinfo(hinfo, &cinfo);
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 			if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo)))
8381da177e4SLinus Torvalds 				return (-EFAULT);
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 			return 0;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 		default:
8431da177e4SLinus Torvalds 			return (-EINVAL);
8441da177e4SLinus Torvalds 		}
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 		return 0;
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	if (kioc->user_pthru) {
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 		upthru32 = kioc->user_pthru;
8541da177e4SLinus Torvalds 		kpthru32 = kioc->pthru32;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 		if (copy_to_user(&upthru32->scsistatus,
8571da177e4SLinus Torvalds 					&kpthru32->scsistatus,
8581da177e4SLinus Torvalds 					sizeof(uint8_t))) {
8591da177e4SLinus Torvalds 			return (-EFAULT);
8601da177e4SLinus Torvalds 		}
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	if (kioc->user_data) {
8641da177e4SLinus Torvalds 		if (copy_to_user(kioc->user_data, kioc->buf_vaddr,
8651da177e4SLinus Torvalds 					kioc->user_data_len)) {
8661da177e4SLinus Torvalds 			return (-EFAULT);
8671da177e4SLinus Torvalds 		}
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	if (copy_to_user(&mimd->mbox[17],
8711da177e4SLinus Torvalds 			&mbox64->mbox32.status, sizeof(uint8_t))) {
8721da177e4SLinus Torvalds 		return (-EFAULT);
8731da177e4SLinus Torvalds 	}
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	return 0;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds /**
8801da177e4SLinus Torvalds  * hinfo_to_cinfo - Convert new format hba info into old format
8811da177e4SLinus Torvalds  * @hinfo	: New format, more comprehensive adapter info
8821da177e4SLinus Torvalds  * @cinfo	: Old format adapter info to support mimd_t apps
8831da177e4SLinus Torvalds  */
8841da177e4SLinus Torvalds static void
8851da177e4SLinus Torvalds hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	if (!hinfo || !cinfo)
8881da177e4SLinus Torvalds 		return;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	cinfo->base		= hinfo->baseport;
8911da177e4SLinus Torvalds 	cinfo->irq		= hinfo->irq;
8921da177e4SLinus Torvalds 	cinfo->numldrv		= hinfo->num_ldrv;
8931da177e4SLinus Torvalds 	cinfo->pcibus		= hinfo->pci_bus;
8941da177e4SLinus Torvalds 	cinfo->pcidev		= hinfo->pci_slot;
8951da177e4SLinus Torvalds 	cinfo->pcifun		= PCI_FUNC(hinfo->pci_dev_fn);
8961da177e4SLinus Torvalds 	cinfo->pciid		= hinfo->pci_device_id;
8971da177e4SLinus Torvalds 	cinfo->pcivendor	= hinfo->pci_vendor_id;
8981da177e4SLinus Torvalds 	cinfo->pcislot		= hinfo->pci_slot;
8991da177e4SLinus Torvalds 	cinfo->uid		= hinfo->unique_id;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 
903a69b74d3SRandy Dunlap /**
904a69b74d3SRandy Dunlap  * mraid_mm_register_adp - Registration routine for low level drivers
9053948ff8bSMatthias Schid  * @lld_adp	: Adapter object
9061da177e4SLinus Torvalds  */
9071da177e4SLinus Torvalds int
9081da177e4SLinus Torvalds mraid_mm_register_adp(mraid_mmadp_t *lld_adp)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	mraid_mmadp_t	*adapter;
9111da177e4SLinus Torvalds 	mbox64_t	*mbox_list;
9121da177e4SLinus Torvalds 	uioc_t		*kioc;
9131da177e4SLinus Torvalds 	uint32_t	rval;
9141da177e4SLinus Torvalds 	int		i;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	if (lld_adp->drvr_type != DRVRTYPE_MBOX)
9181da177e4SLinus Torvalds 		return (-EINVAL);
9191da177e4SLinus Torvalds 
920dd00cc48SYoann Padioleau 	adapter = kzalloc(sizeof(mraid_mmadp_t), GFP_KERNEL);
9211da177e4SLinus Torvalds 
9225236467aSAdrian Bunk 	if (!adapter)
9235236467aSAdrian Bunk 		return -ENOMEM;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	adapter->unique_id	= lld_adp->unique_id;
9271da177e4SLinus Torvalds 	adapter->drvr_type	= lld_adp->drvr_type;
9281da177e4SLinus Torvalds 	adapter->drvr_data	= lld_adp->drvr_data;
9291da177e4SLinus Torvalds 	adapter->pdev		= lld_adp->pdev;
9301da177e4SLinus Torvalds 	adapter->issue_uioc	= lld_adp->issue_uioc;
9311da177e4SLinus Torvalds 	adapter->timeout	= lld_adp->timeout;
9321da177e4SLinus Torvalds 	adapter->max_kioc	= lld_adp->max_kioc;
9331da177e4SLinus Torvalds 	adapter->quiescent	= 1;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	/*
9361da177e4SLinus Torvalds 	 * Allocate single blocks of memory for all required kiocs,
9371da177e4SLinus Torvalds 	 * mailboxes and passthru structures.
9381da177e4SLinus Torvalds 	 */
9391da177e4SLinus Torvalds 	adapter->kioc_list	= kmalloc(sizeof(uioc_t) * lld_adp->max_kioc,
9401da177e4SLinus Torvalds 						GFP_KERNEL);
9411da177e4SLinus Torvalds 	adapter->mbox_list	= kmalloc(sizeof(mbox64_t) * lld_adp->max_kioc,
9421da177e4SLinus Torvalds 						GFP_KERNEL);
9431da177e4SLinus Torvalds 	adapter->pthru_dma_pool = pci_pool_create("megaraid mm pthru pool",
9441da177e4SLinus Torvalds 						adapter->pdev,
9451da177e4SLinus Torvalds 						sizeof(mraid_passthru_t),
9461da177e4SLinus Torvalds 						16, 0);
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	if (!adapter->kioc_list || !adapter->mbox_list ||
9491da177e4SLinus Torvalds 			!adapter->pthru_dma_pool) {
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 		con_log(CL_ANN, (KERN_WARNING
952cadbd4a5SHarvey Harrison 			"megaraid cmm: out of memory, %s %d\n", __func__,
9531da177e4SLinus Torvalds 			__LINE__));
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 		rval = (-ENOMEM);
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 		goto memalloc_error;
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	/*
9611da177e4SLinus Torvalds 	 * Slice kioc_list and make a kioc_pool with the individiual kiocs
9621da177e4SLinus Torvalds 	 */
9631da177e4SLinus Torvalds 	INIT_LIST_HEAD(&adapter->kioc_pool);
9641da177e4SLinus Torvalds 	spin_lock_init(&adapter->kioc_pool_lock);
9651da177e4SLinus Torvalds 	sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	mbox_list	= (mbox64_t *)adapter->mbox_list;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	for (i = 0; i < lld_adp->max_kioc; i++) {
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 		kioc		= adapter->kioc_list + i;
9721da177e4SLinus Torvalds 		kioc->cmdbuf	= (uint64_t)(unsigned long)(mbox_list + i);
9731da177e4SLinus Torvalds 		kioc->pthru32	= pci_pool_alloc(adapter->pthru_dma_pool,
9741da177e4SLinus Torvalds 						GFP_KERNEL, &kioc->pthru32_h);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 		if (!kioc->pthru32) {
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 			con_log(CL_ANN, (KERN_WARNING
9791da177e4SLinus Torvalds 				"megaraid cmm: out of memory, %s %d\n",
980cadbd4a5SHarvey Harrison 					__func__, __LINE__));
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 			rval = (-ENOMEM);
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 			goto pthru_dma_pool_error;
9851da177e4SLinus Torvalds 		}
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 		list_add_tail(&kioc->list, &adapter->kioc_pool);
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	// Setup the dma pools for data buffers
9911da177e4SLinus Torvalds 	if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
9921da177e4SLinus Torvalds 		goto dma_pool_error;
9931da177e4SLinus Torvalds 	}
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	list_add_tail(&adapter->list, &adapters_list_g);
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	adapters_count_g++;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	return 0;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds dma_pool_error:
10021da177e4SLinus Torvalds 	/* Do nothing */
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds pthru_dma_pool_error:
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	for (i = 0; i < lld_adp->max_kioc; i++) {
10071da177e4SLinus Torvalds 		kioc = adapter->kioc_list + i;
10081da177e4SLinus Torvalds 		if (kioc->pthru32) {
10091da177e4SLinus Torvalds 			pci_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
10101da177e4SLinus Torvalds 				kioc->pthru32_h);
10111da177e4SLinus Torvalds 		}
10121da177e4SLinus Torvalds 	}
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds memalloc_error:
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	kfree(adapter->kioc_list);
10171da177e4SLinus Torvalds 	kfree(adapter->mbox_list);
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	if (adapter->pthru_dma_pool)
10201da177e4SLinus Torvalds 		pci_pool_destroy(adapter->pthru_dma_pool);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	kfree(adapter);
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	return rval;
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds /**
10291da177e4SLinus Torvalds  * mraid_mm_adapter_app_handle - return the application handle for this adapter
1030a69b74d3SRandy Dunlap  * @unique_id	: adapter unique identifier
10311da177e4SLinus Torvalds  *
1032a69b74d3SRandy Dunlap  * For the given driver data, locate the adapter in our global list and
10331da177e4SLinus Torvalds  * return the corresponding handle, which is also used by applications to
10341da177e4SLinus Torvalds  * uniquely identify an adapter.
10351da177e4SLinus Torvalds  *
1036a69b74d3SRandy Dunlap  * Return adapter handle if found in the list.
1037a69b74d3SRandy Dunlap  * Return 0 if adapter could not be located, should never happen though.
10381da177e4SLinus Torvalds  */
10391da177e4SLinus Torvalds uint32_t
10401da177e4SLinus Torvalds mraid_mm_adapter_app_handle(uint32_t unique_id)
10411da177e4SLinus Torvalds {
10421da177e4SLinus Torvalds 	mraid_mmadp_t	*adapter;
10431da177e4SLinus Torvalds 	mraid_mmadp_t	*tmp;
10441da177e4SLinus Torvalds 	int		index = 0;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 		if (adapter->unique_id == unique_id) {
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 			return MKADAP(index);
10511da177e4SLinus Torvalds 		}
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 		index++;
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	return 0;
10571da177e4SLinus Torvalds }
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds /**
10611da177e4SLinus Torvalds  * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
10621da177e4SLinus Torvalds  * @adp	: Adapter softstate
10631da177e4SLinus Torvalds  *
10641da177e4SLinus Torvalds  * We maintain a pool of dma buffers per each adapter. Each pool has one
10651da177e4SLinus Torvalds  * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
10661da177e4SLinus Torvalds  * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
10671da177e4SLinus Torvalds  * dont' want to waste too much memory by allocating more buffers per each
10681da177e4SLinus Torvalds  * pool.
10691da177e4SLinus Torvalds  */
10701da177e4SLinus Torvalds static int
10711da177e4SLinus Torvalds mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
10721da177e4SLinus Torvalds {
10731da177e4SLinus Torvalds 	mm_dmapool_t	*pool;
10741da177e4SLinus Torvalds 	int		bufsize;
10751da177e4SLinus Torvalds 	int		i;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	/*
10781da177e4SLinus Torvalds 	 * Create MAX_DMA_POOLS number of pools
10791da177e4SLinus Torvalds 	 */
10801da177e4SLinus Torvalds 	bufsize = MRAID_MM_INIT_BUFF_SIZE;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	for (i = 0; i < MAX_DMA_POOLS; i++){
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 		pool = &adp->dma_pool_list[i];
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 		pool->buf_size = bufsize;
10871da177e4SLinus Torvalds 		spin_lock_init(&pool->lock);
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 		pool->handle = pci_pool_create("megaraid mm data buffer",
10901da177e4SLinus Torvalds 						adp->pdev, bufsize, 16, 0);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 		if (!pool->handle) {
10931da177e4SLinus Torvalds 			goto dma_pool_setup_error;
10941da177e4SLinus Torvalds 		}
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 		pool->vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
10971da177e4SLinus Torvalds 							&pool->paddr);
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 		if (!pool->vaddr)
11001da177e4SLinus Torvalds 			goto dma_pool_setup_error;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 		bufsize = bufsize * 2;
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	return 0;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds dma_pool_setup_error:
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	mraid_mm_teardown_dma_pools(adp);
11101da177e4SLinus Torvalds 	return (-ENOMEM);
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 
1114a69b74d3SRandy Dunlap /**
11151da177e4SLinus Torvalds  * mraid_mm_unregister_adp - Unregister routine for low level drivers
11161da177e4SLinus Torvalds  * @unique_id	: UID of the adpater
1117a69b74d3SRandy Dunlap  *
1118a69b74d3SRandy Dunlap  * Assumes no outstanding ioctls to llds.
11191da177e4SLinus Torvalds  */
11201da177e4SLinus Torvalds int
11211da177e4SLinus Torvalds mraid_mm_unregister_adp(uint32_t unique_id)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	mraid_mmadp_t	*adapter;
11241da177e4SLinus Torvalds 	mraid_mmadp_t	*tmp;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 		if (adapter->unique_id == unique_id) {
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 			adapters_count_g--;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 			list_del_init(&adapter->list);
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 			mraid_mm_free_adp_resources(adapter);
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 			kfree(adapter);
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 			con_log(CL_ANN, (
11401da177e4SLinus Torvalds 				"megaraid cmm: Unregistered one adapter:%#x\n",
11411da177e4SLinus Torvalds 				unique_id));
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 			return 0;
11441da177e4SLinus Torvalds 		}
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	return (-ENODEV);
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds /**
11511da177e4SLinus Torvalds  * mraid_mm_free_adp_resources - Free adapter softstate
11521da177e4SLinus Torvalds  * @adp	: Adapter softstate
11531da177e4SLinus Torvalds  */
11541da177e4SLinus Torvalds static void
11551da177e4SLinus Torvalds mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	uioc_t	*kioc;
11581da177e4SLinus Torvalds 	int	i;
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	mraid_mm_teardown_dma_pools(adp);
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	for (i = 0; i < adp->max_kioc; i++) {
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 		kioc = adp->kioc_list + i;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 		pci_pool_free(adp->pthru_dma_pool, kioc->pthru32,
11671da177e4SLinus Torvalds 				kioc->pthru32_h);
11681da177e4SLinus Torvalds 	}
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	kfree(adp->kioc_list);
11711da177e4SLinus Torvalds 	kfree(adp->mbox_list);
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	pci_pool_destroy(adp->pthru_dma_pool);
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	return;
11771da177e4SLinus Torvalds }
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds /**
11811da177e4SLinus Torvalds  * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
11821da177e4SLinus Torvalds  * @adp	: Adapter softstate
11831da177e4SLinus Torvalds  */
11841da177e4SLinus Torvalds static void
11851da177e4SLinus Torvalds mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
11861da177e4SLinus Torvalds {
11871da177e4SLinus Torvalds 	int		i;
11881da177e4SLinus Torvalds 	mm_dmapool_t	*pool;
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	for (i = 0; i < MAX_DMA_POOLS; i++) {
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 		pool = &adp->dma_pool_list[i];
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 		if (pool->handle) {
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 			if (pool->vaddr)
11971da177e4SLinus Torvalds 				pci_pool_free(pool->handle, pool->vaddr,
11981da177e4SLinus Torvalds 							pool->paddr);
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 			pci_pool_destroy(pool->handle);
12011da177e4SLinus Torvalds 			pool->handle = NULL;
12021da177e4SLinus Torvalds 		}
12031da177e4SLinus Torvalds 	}
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	return;
12061da177e4SLinus Torvalds }
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds /**
1209a69b74d3SRandy Dunlap  * mraid_mm_init	- Module entry point
12101da177e4SLinus Torvalds  */
12111da177e4SLinus Torvalds static int __init
12121da177e4SLinus Torvalds mraid_mm_init(void)
12131da177e4SLinus Torvalds {
121490a95af8SThomas Horsten 	int err;
121590a95af8SThomas Horsten 
12161da177e4SLinus Torvalds 	// Announce the driver version
12171da177e4SLinus Torvalds 	con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
12181da177e4SLinus Torvalds 		LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
12191da177e4SLinus Torvalds 
122090a95af8SThomas Horsten 	err = misc_register(&megaraid_mm_dev);
122190a95af8SThomas Horsten 	if (err < 0) {
122290a95af8SThomas Horsten 		con_log(CL_ANN, ("megaraid cmm: cannot register misc device\n"));
122390a95af8SThomas Horsten 		return err;
12241da177e4SLinus Torvalds 	}
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	init_waitqueue_head(&wait_q);
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	INIT_LIST_HEAD(&adapters_list_g);
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	return 0;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
1235a69b74d3SRandy Dunlap /**
1236a69b74d3SRandy Dunlap  * mraid_mm_compat_ioctl	- 32bit to 64bit ioctl conversion routine
1237a69b74d3SRandy Dunlap  * @filep	: file operations pointer (ignored)
1238a69b74d3SRandy Dunlap  * @cmd		: ioctl command
1239a69b74d3SRandy Dunlap  * @arg		: user ioctl packet
1240a69b74d3SRandy Dunlap  */
12411da177e4SLinus Torvalds static long
12421da177e4SLinus Torvalds mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd,
12431da177e4SLinus Torvalds 		      unsigned long arg)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	int err;
1246672b2d38SJu, Seokmann  
1247f4927c45SArnd Bergmann 	err = mraid_mm_ioctl(filep, cmd, arg);
1248672b2d38SJu, Seokmann  
12491da177e4SLinus Torvalds 	return err;
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds #endif
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds /**
1254a69b74d3SRandy Dunlap  * mraid_mm_exit	- Module exit point
12551da177e4SLinus Torvalds  */
12561da177e4SLinus Torvalds static void __exit
12571da177e4SLinus Torvalds mraid_mm_exit(void)
12581da177e4SLinus Torvalds {
12591da177e4SLinus Torvalds 	con_log(CL_DLEVEL1 , ("exiting common mod\n"));
12601da177e4SLinus Torvalds 
126190a95af8SThomas Horsten 	misc_deregister(&megaraid_mm_dev);
12621da177e4SLinus Torvalds }
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds module_init(mraid_mm_init);
12651da177e4SLinus Torvalds module_exit(mraid_mm_exit);
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds /* vi: set ts=8 sw=8 tw=78: */
1268