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