/*
 * There are serveal cases as follows:
 *
 * 1. "met-cmd --start --smi=toggle"
 *
 * 2. "met-cmd --start --smi=toggle:master"
 *
 * 3. "met-cmd --start --smi=master:port:rwtype:desttype:bustype"
 *
 * 4. "met-cmd --start --smi=dump"
 *
 */
static int smi_process_argument(const char *arg, int len)
{
	int master, port;
	int rwtype, desttype, bustype;
	int ret;
	int idx;

	if (len < 6)
		return -1;

	memset(err_msg, 0, MET_SMI_BUF_SIZE);

	/* --smi=toggle */
	if ((strncmp(arg, "toggle", 6) == 0) && (len == 6)) {
		if (met_smi.mode != 0)
			return -1;
		/* Set mode */
		met_smi.mode = 2;
	/* --smi=toggle:master */
	} else if ((strncmp(arg, "toggle", 6) == 0) &&
		arg[6] == ':' &&
		len > 7) {
		if (met_smi.mode != 0)
			return -1;
		ret = get_num(&(arg[7]), &toggle_master);
		if (ret == 0) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Toggle master: can't get number [%s]\n",
				arg);
			return -1;
		}
		if (check_master_vaild(toggle_master) != MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Toggle master: check master failed [%s]\n",
				arg);
			return -1;
		}
		/* Set mode */
		met_smi.mode = 3;
	/* --smi=master:port:rwtype:desttype:bustype */
	} else if (len >= 9) {
		if ((met_smi.mode != 0) &&
			(met_smi.mode != 1))
			return -1;
		/* Initial variables */
		master = 0;
		port = 0;
		rwtype = 0;
		desttype = 0;
		bustype = 0;
		/* Get master */
		idx = 0;
		ret = get_num(&(arg[idx]), &master);
		if (ret == 0) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: can't get number [%s]\n",
				arg);
			return -1;
		}
		// Check master
		if (check_master_vaild(master) != MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: check master failed [%s]\n",
				arg);
			return -1;
		}
		/* Get port */
		idx += ret + 1;
		ret = get_num(&(arg[idx]), &port);
		if (ret == 0) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: can't get number [%s]\n",
				arg);
			return -1;
		}
		// check port
		if (check_port_vaild(master, port) != MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: check port failed [%s]\n",
				arg);
			return -1;
		}
		// assign port
		if (assign_port(master, port) != MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: assign port failed [%s]\n",
				arg);
			return -1;
		}
		/* Get rwtype */
		idx += ret + 1;
		ret = get_num(&(arg[idx]), &rwtype);
		if (ret == 0) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: can't get number [%s]\n",
				arg);
			return -1;
		}
		// check rwtype
		if (check_rwtype_vaild(master, port, rwtype) !=
					MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: check rwtype failed [%s]\n",
				arg);
			return -1;
		}
		// assign rwtype
		if (assign_rwtype(master, rwtype) != MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: assign rwtype failed [%s]\n",
				arg);
			return -1;
		}
		/* Get desttype */
		idx += ret + 1;
		ret = get_num(&(arg[idx]), &desttype);
		if (ret == 0) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: can't get number [%s]\n",
				arg);
			return -1;
		}
		// check desttype
		if (check_desttype_valid(master, port, desttype) !=
					MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: check desttype failed [%s]\n",
				arg);
			return -1;
		}
		// assign desttype
		if (assign_desttype(master, desttype) != MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: assign desttype failed [%s]\n",
				arg);
			return -1;
		}
		/* Get bustype */
		idx += ret + 1;
		ret = get_num(&(arg[idx]), &bustype);
		if (ret == 0) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: can't get number [%s]\n",
				arg);
			return -1;
		}
		// check bustype
		if (check_bustype_valid(master, port, bustype) !=
					MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: check bustype failed [%s]\n",
				arg);
			return -1;
		}
		// assign bustype
		if (assign_bustype(master, bustype) != MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: assign bustype failed [%s]\n",
				arg);
			return -1;
		}
		// assign mode for each master TODO: need to re-check
		if (assign_mode(master, 1) != MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Normal: assign mode failed [%s]\n",
				arg);
			return -1;
		}
		/* Set mode */
		met_smi.mode = 1;
	/* --smi=dump */
	} else if (strncmp(arg, "dump", 4) == 0) {
		if (met_smi.mode != 0)
			return -1;
		/* Set mode */
		met_smi.mode = 4;
	} else {
		return -1;
	}

	return 0;
}
Exemple #2
0
/*
 * There are serveal cases as follows:
 *
 * 1. "met-cmd --start --smi=toggle"
 *
 * 2. "met-cmd --start --smi=toggle:master"
 *
 * 3. "met-cmd --start --smi=master:port:rwtype:desttype:bustype"
 *
 * 4. "met-cmd --start --smi=dump"
 *
 * 5. "met-cmd --start --smi=master:port[:port1][:port2][:port3]"
 *
 */
static int smi_process_argument(const char *arg, int len)
{
	int		master;
	unsigned int	port, rwtype, desttype, bustype;
	int		ret;
	int		idx;

	if (len < 3)
		return -1;

	memset(err_msg, 0, MET_SMI_BUF_SIZE);

	/* --smi=toggle */
	if ((strncmp(arg, "toggle", 6) == 0) && (len == 6)) {
		if (met_smi.mode != 0)
			return -1;
		/* Set mode */
		met_smi.mode = 2;
	/* --smi=toggle:master */
	} else if ((strncmp(arg, "toggle", 6) == 0) &&
		arg[6] == ':' &&
		len > 7) {
		if (met_smi.mode != 0)
			return -1;
		ret = get_num(&(arg[7]), &toggle_master);
		if (ret == 0) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Toggle master: can't get number [%s]\n",
				arg);
			return -1;
		}
		if (check_master_vaild(toggle_master) != MET_SMI_SUCCESS) {
			snprintf(err_msg,
				MET_SMI_BUF_SIZE,
				"Toggle master: check master failed [%s]\n",
				arg);
			return -1;
		}
		/* Set mode */
		met_smi.mode = 3;
	/* --smi=master:port:rwtype:desttype:bustype */
	} else if (len >= 3) {
		if ((met_smi.mode != 0) &&
			(met_smi.mode != 1))
			return -1;
		if (parallel_mode == 0) {
			/* Initial variables */
			master = 0;
			port = 0;
			rwtype = 0;
			desttype = 0;
			bustype = 0;
			/* Get master */
			idx = 0;
			ret = get_num(&(arg[idx]), &master);
			if (ret == 0) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: can't get number [%s]\n",
					arg);
				return -1;
			}
			// Check master
			if (check_master_vaild(master) != MET_SMI_SUCCESS) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: check master failed [%s]\n",
					arg);
				return -1;
			}
			/* Get port */
			idx += ret + 1;
			ret = get_port(&(arg[idx]), master, &port);
			if (ret == MET_SMI_FAIL) {
				return -1;
			}
			/* Get rwtype */
			idx += ret + 1;
			ret = get_num(&(arg[idx]), &rwtype);
			if (ret == 0) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: can't get number [%s]\n",
					arg);
				return -1;
			}
			// check rwtype
			if (check_rwtype_vaild(master, port, rwtype) !=
						MET_SMI_SUCCESS) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: check rwtype failed [%s]\n",
					arg);
				return -1;
			}
			// assign rwtype
			if (assign_rwtype(master, rwtype) != MET_SMI_SUCCESS) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: assign rwtype failed [%s]\n",
					arg);
				return -1;
			}
			/* Get desttype */
			idx += ret + 1;
			ret = get_num(&(arg[idx]), &desttype);
			if (ret == 0) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: can't get number [%s]\n",
					arg);
				return -1;
			}
			// check desttype
			if (check_desttype_valid(master, port, desttype) !=
						MET_SMI_SUCCESS) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: check desttype failed [%s]\n",
					arg);
				return -1;
			}
			// assign desttype
			if (assign_desttype(master, desttype) !=
						MET_SMI_SUCCESS) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: assign desttype failed [%s]\n",
					arg);
				return -1;
			}
			/* Get bustype */
			idx += ret + 1;
			ret = get_num(&(arg[idx]), &bustype);
			if (ret == 0) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: can't get number [%s]\n",
					arg);
				return -1;
			}
			// check bustype
			if (check_bustype_valid(master, port, bustype) !=
						MET_SMI_SUCCESS) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: check bustype failed [%s]\n",
					arg);
				return -1;
			}
			// assign bustype
			if (assign_bustype(master, bustype) !=
				MET_SMI_SUCCESS) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: assign bustype failed [%s]\n",
					arg);
				return -1;
			}
			// assign mode for each master TODO: need to re-check
			if (assign_mode(master, 1) != MET_SMI_SUCCESS) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: assign mode failed [%s]\n",
					arg);
				return -1;
			}
#ifdef MET_SMI_DEBUG
			if (master < SMI_LARB_NUMBER) {
			printk("===Setting Master[%d]: "
				"mode = %x, "
				"master = %x, "
				"port = %x, "
				"rwtype = %x, "
				"desttype = %x, "
				"bustype = %x\n",
				master,
				smi_larb[master].mode,
				smi_larb[master].master,
				smi_larb[master].port,
				smi_larb[master].rwtype,
				smi_larb[master].desttype,
				smi_larb[master].bustype);
			} else {
			printk("===Setting Master[%d]: "
				"mode = %x, "
				"master = %x, "
				"port = %x, "
				"rwtype = %x, "
				"desttype = %x, "
				"bustype = %x\n",
				master,
				smi_comm[master-SMI_LARB_NUMBER].mode,
				smi_comm[master-SMI_LARB_NUMBER].master,
				smi_comm[master-SMI_LARB_NUMBER].port,
				smi_comm[master-SMI_LARB_NUMBER].rwtype,
				smi_comm[master-SMI_LARB_NUMBER].desttype,
				smi_comm[master-SMI_LARB_NUMBER].bustype);
			}
#endif
		/* --smi=master:port[:port1][:port2][:port3] */
		} else {
			/* Initial variables */
			master = 0;
			port = 0;
			/* Get master */
			idx = 0;
			ret = get_num(&(arg[idx]), &master);
			if (ret == 0) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: can't get number [%s]\n",
					arg);
				return -1;
			}
			// Check master
			if (check_master_vaild(master) != MET_SMI_SUCCESS) {
				snprintf(err_msg,
					MET_SMI_BUF_SIZE,
					"Normal: check master failed [%s]\n",
					arg);
				return -1;
			}
			/* Get port */
			do {
				idx += ret + 1;
				ret = get_port(&(arg[idx]), master, &port);
				if (ret == MET_SMI_FAIL) {
					return -1;
				}
			} while (isalnum(arg[idx+ret+1]));
		}
		/* Set mode */
		met_smi.mode = 1;
	/* --smi=dump */
	} else if (strncmp(arg, "dump", 4) == 0) {
		if (met_smi.mode != 0)
			return -1;
		/* Set mode */
		met_smi.mode = 4;
	} else {
		return -1;
	}

	return 0;
}