xref: /OK3568_Linux_fs/kernel/drivers/greybus/svc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * SVC Greybus driver.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2015 Google Inc.
6*4882a593Smuzhiyun  * Copyright 2015 Linaro Ltd.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/debugfs.h>
10*4882a593Smuzhiyun #include <linux/workqueue.h>
11*4882a593Smuzhiyun #include <linux/greybus.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #define SVC_INTF_EJECT_TIMEOUT		9000
14*4882a593Smuzhiyun #define SVC_INTF_ACTIVATE_TIMEOUT	6000
15*4882a593Smuzhiyun #define SVC_INTF_RESUME_TIMEOUT		3000
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun struct gb_svc_deferred_request {
18*4882a593Smuzhiyun 	struct work_struct work;
19*4882a593Smuzhiyun 	struct gb_operation *operation;
20*4882a593Smuzhiyun };
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun static int gb_svc_queue_deferred_request(struct gb_operation *operation);
23*4882a593Smuzhiyun 
endo_id_show(struct device * dev,struct device_attribute * attr,char * buf)24*4882a593Smuzhiyun static ssize_t endo_id_show(struct device *dev,
25*4882a593Smuzhiyun 			    struct device_attribute *attr, char *buf)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	struct gb_svc *svc = to_gb_svc(dev);
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	return sprintf(buf, "0x%04x\n", svc->endo_id);
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun static DEVICE_ATTR_RO(endo_id);
32*4882a593Smuzhiyun 
ap_intf_id_show(struct device * dev,struct device_attribute * attr,char * buf)33*4882a593Smuzhiyun static ssize_t ap_intf_id_show(struct device *dev,
34*4882a593Smuzhiyun 			       struct device_attribute *attr, char *buf)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	struct gb_svc *svc = to_gb_svc(dev);
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", svc->ap_intf_id);
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun static DEVICE_ATTR_RO(ap_intf_id);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun // FIXME
43*4882a593Smuzhiyun // This is a hack, we need to do this "right" and clean the interface up
44*4882a593Smuzhiyun // properly, not just forcibly yank the thing out of the system and hope for the
45*4882a593Smuzhiyun // best.  But for now, people want their modules to come out without having to
46*4882a593Smuzhiyun // throw the thing to the ground or get out a screwdriver.
intf_eject_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)47*4882a593Smuzhiyun static ssize_t intf_eject_store(struct device *dev,
48*4882a593Smuzhiyun 				struct device_attribute *attr, const char *buf,
49*4882a593Smuzhiyun 				size_t len)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	struct gb_svc *svc = to_gb_svc(dev);
52*4882a593Smuzhiyun 	unsigned short intf_id;
53*4882a593Smuzhiyun 	int ret;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	ret = kstrtou16(buf, 10, &intf_id);
56*4882a593Smuzhiyun 	if (ret < 0)
57*4882a593Smuzhiyun 		return ret;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	ret = gb_svc_intf_eject(svc, intf_id);
62*4882a593Smuzhiyun 	if (ret < 0)
63*4882a593Smuzhiyun 		return ret;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	return len;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun static DEVICE_ATTR_WO(intf_eject);
68*4882a593Smuzhiyun 
watchdog_show(struct device * dev,struct device_attribute * attr,char * buf)69*4882a593Smuzhiyun static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr,
70*4882a593Smuzhiyun 			     char *buf)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	struct gb_svc *svc = to_gb_svc(dev);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	return sprintf(buf, "%s\n",
75*4882a593Smuzhiyun 		       gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled");
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
watchdog_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)78*4882a593Smuzhiyun static ssize_t watchdog_store(struct device *dev,
79*4882a593Smuzhiyun 			      struct device_attribute *attr, const char *buf,
80*4882a593Smuzhiyun 			      size_t len)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	struct gb_svc *svc = to_gb_svc(dev);
83*4882a593Smuzhiyun 	int retval;
84*4882a593Smuzhiyun 	bool user_request;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	retval = strtobool(buf, &user_request);
87*4882a593Smuzhiyun 	if (retval)
88*4882a593Smuzhiyun 		return retval;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	if (user_request)
91*4882a593Smuzhiyun 		retval = gb_svc_watchdog_enable(svc);
92*4882a593Smuzhiyun 	else
93*4882a593Smuzhiyun 		retval = gb_svc_watchdog_disable(svc);
94*4882a593Smuzhiyun 	if (retval)
95*4882a593Smuzhiyun 		return retval;
96*4882a593Smuzhiyun 	return len;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun static DEVICE_ATTR_RW(watchdog);
99*4882a593Smuzhiyun 
watchdog_action_show(struct device * dev,struct device_attribute * attr,char * buf)100*4882a593Smuzhiyun static ssize_t watchdog_action_show(struct device *dev,
101*4882a593Smuzhiyun 				    struct device_attribute *attr, char *buf)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	struct gb_svc *svc = to_gb_svc(dev);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	if (svc->action == GB_SVC_WATCHDOG_BITE_PANIC_KERNEL)
106*4882a593Smuzhiyun 		return sprintf(buf, "panic\n");
107*4882a593Smuzhiyun 	else if (svc->action == GB_SVC_WATCHDOG_BITE_RESET_UNIPRO)
108*4882a593Smuzhiyun 		return sprintf(buf, "reset\n");
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	return -EINVAL;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
watchdog_action_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)113*4882a593Smuzhiyun static ssize_t watchdog_action_store(struct device *dev,
114*4882a593Smuzhiyun 				     struct device_attribute *attr,
115*4882a593Smuzhiyun 				     const char *buf, size_t len)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	struct gb_svc *svc = to_gb_svc(dev);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (sysfs_streq(buf, "panic"))
120*4882a593Smuzhiyun 		svc->action = GB_SVC_WATCHDOG_BITE_PANIC_KERNEL;
121*4882a593Smuzhiyun 	else if (sysfs_streq(buf, "reset"))
122*4882a593Smuzhiyun 		svc->action = GB_SVC_WATCHDOG_BITE_RESET_UNIPRO;
123*4882a593Smuzhiyun 	else
124*4882a593Smuzhiyun 		return -EINVAL;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	return len;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun static DEVICE_ATTR_RW(watchdog_action);
129*4882a593Smuzhiyun 
gb_svc_pwrmon_rail_count_get(struct gb_svc * svc,u8 * value)130*4882a593Smuzhiyun static int gb_svc_pwrmon_rail_count_get(struct gb_svc *svc, u8 *value)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	struct gb_svc_pwrmon_rail_count_get_response response;
133*4882a593Smuzhiyun 	int ret;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection,
136*4882a593Smuzhiyun 				GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET, NULL, 0,
137*4882a593Smuzhiyun 				&response, sizeof(response));
138*4882a593Smuzhiyun 	if (ret) {
139*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to get rail count: %d\n", ret);
140*4882a593Smuzhiyun 		return ret;
141*4882a593Smuzhiyun 	}
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	*value = response.rail_count;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	return 0;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
gb_svc_pwrmon_rail_names_get(struct gb_svc * svc,struct gb_svc_pwrmon_rail_names_get_response * response,size_t bufsize)148*4882a593Smuzhiyun static int gb_svc_pwrmon_rail_names_get(struct gb_svc *svc,
149*4882a593Smuzhiyun 		struct gb_svc_pwrmon_rail_names_get_response *response,
150*4882a593Smuzhiyun 		size_t bufsize)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	int ret;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection,
155*4882a593Smuzhiyun 				GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET, NULL, 0,
156*4882a593Smuzhiyun 				response, bufsize);
157*4882a593Smuzhiyun 	if (ret) {
158*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to get rail names: %d\n", ret);
159*4882a593Smuzhiyun 		return ret;
160*4882a593Smuzhiyun 	}
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (response->status != GB_SVC_OP_SUCCESS) {
163*4882a593Smuzhiyun 		dev_err(&svc->dev,
164*4882a593Smuzhiyun 			"SVC error while getting rail names: %u\n",
165*4882a593Smuzhiyun 			response->status);
166*4882a593Smuzhiyun 		return -EREMOTEIO;
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return 0;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
gb_svc_pwrmon_sample_get(struct gb_svc * svc,u8 rail_id,u8 measurement_type,u32 * value)172*4882a593Smuzhiyun static int gb_svc_pwrmon_sample_get(struct gb_svc *svc, u8 rail_id,
173*4882a593Smuzhiyun 				    u8 measurement_type, u32 *value)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	struct gb_svc_pwrmon_sample_get_request request;
176*4882a593Smuzhiyun 	struct gb_svc_pwrmon_sample_get_response response;
177*4882a593Smuzhiyun 	int ret;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	request.rail_id = rail_id;
180*4882a593Smuzhiyun 	request.measurement_type = measurement_type;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_PWRMON_SAMPLE_GET,
183*4882a593Smuzhiyun 				&request, sizeof(request),
184*4882a593Smuzhiyun 				&response, sizeof(response));
185*4882a593Smuzhiyun 	if (ret) {
186*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to get rail sample: %d\n", ret);
187*4882a593Smuzhiyun 		return ret;
188*4882a593Smuzhiyun 	}
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (response.result) {
191*4882a593Smuzhiyun 		dev_err(&svc->dev,
192*4882a593Smuzhiyun 			"UniPro error while getting rail power sample (%d %d): %d\n",
193*4882a593Smuzhiyun 			rail_id, measurement_type, response.result);
194*4882a593Smuzhiyun 		switch (response.result) {
195*4882a593Smuzhiyun 		case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
196*4882a593Smuzhiyun 			return -EINVAL;
197*4882a593Smuzhiyun 		case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
198*4882a593Smuzhiyun 			return -ENOMSG;
199*4882a593Smuzhiyun 		default:
200*4882a593Smuzhiyun 			return -EREMOTEIO;
201*4882a593Smuzhiyun 		}
202*4882a593Smuzhiyun 	}
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	*value = le32_to_cpu(response.measurement);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	return 0;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
gb_svc_pwrmon_intf_sample_get(struct gb_svc * svc,u8 intf_id,u8 measurement_type,u32 * value)209*4882a593Smuzhiyun int gb_svc_pwrmon_intf_sample_get(struct gb_svc *svc, u8 intf_id,
210*4882a593Smuzhiyun 				  u8 measurement_type, u32 *value)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	struct gb_svc_pwrmon_intf_sample_get_request request;
213*4882a593Smuzhiyun 	struct gb_svc_pwrmon_intf_sample_get_response response;
214*4882a593Smuzhiyun 	int ret;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	request.intf_id = intf_id;
217*4882a593Smuzhiyun 	request.measurement_type = measurement_type;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection,
220*4882a593Smuzhiyun 				GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET,
221*4882a593Smuzhiyun 				&request, sizeof(request),
222*4882a593Smuzhiyun 				&response, sizeof(response));
223*4882a593Smuzhiyun 	if (ret) {
224*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to get intf sample: %d\n", ret);
225*4882a593Smuzhiyun 		return ret;
226*4882a593Smuzhiyun 	}
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	if (response.result) {
229*4882a593Smuzhiyun 		dev_err(&svc->dev,
230*4882a593Smuzhiyun 			"UniPro error while getting intf power sample (%d %d): %d\n",
231*4882a593Smuzhiyun 			intf_id, measurement_type, response.result);
232*4882a593Smuzhiyun 		switch (response.result) {
233*4882a593Smuzhiyun 		case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
234*4882a593Smuzhiyun 			return -EINVAL;
235*4882a593Smuzhiyun 		case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
236*4882a593Smuzhiyun 			return -ENOMSG;
237*4882a593Smuzhiyun 		default:
238*4882a593Smuzhiyun 			return -EREMOTEIO;
239*4882a593Smuzhiyun 		}
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	*value = le32_to_cpu(response.measurement);
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	return 0;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun static struct attribute *svc_attrs[] = {
248*4882a593Smuzhiyun 	&dev_attr_endo_id.attr,
249*4882a593Smuzhiyun 	&dev_attr_ap_intf_id.attr,
250*4882a593Smuzhiyun 	&dev_attr_intf_eject.attr,
251*4882a593Smuzhiyun 	&dev_attr_watchdog.attr,
252*4882a593Smuzhiyun 	&dev_attr_watchdog_action.attr,
253*4882a593Smuzhiyun 	NULL,
254*4882a593Smuzhiyun };
255*4882a593Smuzhiyun ATTRIBUTE_GROUPS(svc);
256*4882a593Smuzhiyun 
gb_svc_intf_device_id(struct gb_svc * svc,u8 intf_id,u8 device_id)257*4882a593Smuzhiyun int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	struct gb_svc_intf_device_id_request request;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	request.intf_id = intf_id;
262*4882a593Smuzhiyun 	request.device_id = device_id;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
265*4882a593Smuzhiyun 				 &request, sizeof(request), NULL, 0);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
gb_svc_intf_eject(struct gb_svc * svc,u8 intf_id)268*4882a593Smuzhiyun int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	struct gb_svc_intf_eject_request request;
271*4882a593Smuzhiyun 	int ret;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	request.intf_id = intf_id;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	/*
276*4882a593Smuzhiyun 	 * The pulse width for module release in svc is long so we need to
277*4882a593Smuzhiyun 	 * increase the timeout so the operation will not return to soon.
278*4882a593Smuzhiyun 	 */
279*4882a593Smuzhiyun 	ret = gb_operation_sync_timeout(svc->connection,
280*4882a593Smuzhiyun 					GB_SVC_TYPE_INTF_EJECT, &request,
281*4882a593Smuzhiyun 					sizeof(request), NULL, 0,
282*4882a593Smuzhiyun 					SVC_INTF_EJECT_TIMEOUT);
283*4882a593Smuzhiyun 	if (ret) {
284*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
285*4882a593Smuzhiyun 		return ret;
286*4882a593Smuzhiyun 	}
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	return 0;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
gb_svc_intf_vsys_set(struct gb_svc * svc,u8 intf_id,bool enable)291*4882a593Smuzhiyun int gb_svc_intf_vsys_set(struct gb_svc *svc, u8 intf_id, bool enable)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	struct gb_svc_intf_vsys_request request;
294*4882a593Smuzhiyun 	struct gb_svc_intf_vsys_response response;
295*4882a593Smuzhiyun 	int type, ret;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	request.intf_id = intf_id;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	if (enable)
300*4882a593Smuzhiyun 		type = GB_SVC_TYPE_INTF_VSYS_ENABLE;
301*4882a593Smuzhiyun 	else
302*4882a593Smuzhiyun 		type = GB_SVC_TYPE_INTF_VSYS_DISABLE;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection, type,
305*4882a593Smuzhiyun 				&request, sizeof(request),
306*4882a593Smuzhiyun 				&response, sizeof(response));
307*4882a593Smuzhiyun 	if (ret < 0)
308*4882a593Smuzhiyun 		return ret;
309*4882a593Smuzhiyun 	if (response.result_code != GB_SVC_INTF_VSYS_OK)
310*4882a593Smuzhiyun 		return -EREMOTEIO;
311*4882a593Smuzhiyun 	return 0;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun 
gb_svc_intf_refclk_set(struct gb_svc * svc,u8 intf_id,bool enable)314*4882a593Smuzhiyun int gb_svc_intf_refclk_set(struct gb_svc *svc, u8 intf_id, bool enable)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	struct gb_svc_intf_refclk_request request;
317*4882a593Smuzhiyun 	struct gb_svc_intf_refclk_response response;
318*4882a593Smuzhiyun 	int type, ret;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	request.intf_id = intf_id;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (enable)
323*4882a593Smuzhiyun 		type = GB_SVC_TYPE_INTF_REFCLK_ENABLE;
324*4882a593Smuzhiyun 	else
325*4882a593Smuzhiyun 		type = GB_SVC_TYPE_INTF_REFCLK_DISABLE;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection, type,
328*4882a593Smuzhiyun 				&request, sizeof(request),
329*4882a593Smuzhiyun 				&response, sizeof(response));
330*4882a593Smuzhiyun 	if (ret < 0)
331*4882a593Smuzhiyun 		return ret;
332*4882a593Smuzhiyun 	if (response.result_code != GB_SVC_INTF_REFCLK_OK)
333*4882a593Smuzhiyun 		return -EREMOTEIO;
334*4882a593Smuzhiyun 	return 0;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun 
gb_svc_intf_unipro_set(struct gb_svc * svc,u8 intf_id,bool enable)337*4882a593Smuzhiyun int gb_svc_intf_unipro_set(struct gb_svc *svc, u8 intf_id, bool enable)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	struct gb_svc_intf_unipro_request request;
340*4882a593Smuzhiyun 	struct gb_svc_intf_unipro_response response;
341*4882a593Smuzhiyun 	int type, ret;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	request.intf_id = intf_id;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	if (enable)
346*4882a593Smuzhiyun 		type = GB_SVC_TYPE_INTF_UNIPRO_ENABLE;
347*4882a593Smuzhiyun 	else
348*4882a593Smuzhiyun 		type = GB_SVC_TYPE_INTF_UNIPRO_DISABLE;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection, type,
351*4882a593Smuzhiyun 				&request, sizeof(request),
352*4882a593Smuzhiyun 				&response, sizeof(response));
353*4882a593Smuzhiyun 	if (ret < 0)
354*4882a593Smuzhiyun 		return ret;
355*4882a593Smuzhiyun 	if (response.result_code != GB_SVC_INTF_UNIPRO_OK)
356*4882a593Smuzhiyun 		return -EREMOTEIO;
357*4882a593Smuzhiyun 	return 0;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
gb_svc_intf_activate(struct gb_svc * svc,u8 intf_id,u8 * intf_type)360*4882a593Smuzhiyun int gb_svc_intf_activate(struct gb_svc *svc, u8 intf_id, u8 *intf_type)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	struct gb_svc_intf_activate_request request;
363*4882a593Smuzhiyun 	struct gb_svc_intf_activate_response response;
364*4882a593Smuzhiyun 	int ret;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	request.intf_id = intf_id;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	ret = gb_operation_sync_timeout(svc->connection,
369*4882a593Smuzhiyun 					GB_SVC_TYPE_INTF_ACTIVATE,
370*4882a593Smuzhiyun 					&request, sizeof(request),
371*4882a593Smuzhiyun 					&response, sizeof(response),
372*4882a593Smuzhiyun 					SVC_INTF_ACTIVATE_TIMEOUT);
373*4882a593Smuzhiyun 	if (ret < 0)
374*4882a593Smuzhiyun 		return ret;
375*4882a593Smuzhiyun 	if (response.status != GB_SVC_OP_SUCCESS) {
376*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to activate interface %u: %u\n",
377*4882a593Smuzhiyun 			intf_id, response.status);
378*4882a593Smuzhiyun 		return -EREMOTEIO;
379*4882a593Smuzhiyun 	}
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	*intf_type = response.intf_type;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	return 0;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun 
gb_svc_intf_resume(struct gb_svc * svc,u8 intf_id)386*4882a593Smuzhiyun int gb_svc_intf_resume(struct gb_svc *svc, u8 intf_id)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun 	struct gb_svc_intf_resume_request request;
389*4882a593Smuzhiyun 	struct gb_svc_intf_resume_response response;
390*4882a593Smuzhiyun 	int ret;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	request.intf_id = intf_id;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	ret = gb_operation_sync_timeout(svc->connection,
395*4882a593Smuzhiyun 					GB_SVC_TYPE_INTF_RESUME,
396*4882a593Smuzhiyun 					&request, sizeof(request),
397*4882a593Smuzhiyun 					&response, sizeof(response),
398*4882a593Smuzhiyun 					SVC_INTF_RESUME_TIMEOUT);
399*4882a593Smuzhiyun 	if (ret < 0) {
400*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to send interface resume %u: %d\n",
401*4882a593Smuzhiyun 			intf_id, ret);
402*4882a593Smuzhiyun 		return ret;
403*4882a593Smuzhiyun 	}
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	if (response.status != GB_SVC_OP_SUCCESS) {
406*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to resume interface %u: %u\n",
407*4882a593Smuzhiyun 			intf_id, response.status);
408*4882a593Smuzhiyun 		return -EREMOTEIO;
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	return 0;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
gb_svc_dme_peer_get(struct gb_svc * svc,u8 intf_id,u16 attr,u16 selector,u32 * value)414*4882a593Smuzhiyun int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
415*4882a593Smuzhiyun 			u32 *value)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	struct gb_svc_dme_peer_get_request request;
418*4882a593Smuzhiyun 	struct gb_svc_dme_peer_get_response response;
419*4882a593Smuzhiyun 	u16 result;
420*4882a593Smuzhiyun 	int ret;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	request.intf_id = intf_id;
423*4882a593Smuzhiyun 	request.attr = cpu_to_le16(attr);
424*4882a593Smuzhiyun 	request.selector = cpu_to_le16(selector);
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
427*4882a593Smuzhiyun 				&request, sizeof(request),
428*4882a593Smuzhiyun 				&response, sizeof(response));
429*4882a593Smuzhiyun 	if (ret) {
430*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
431*4882a593Smuzhiyun 			intf_id, attr, selector, ret);
432*4882a593Smuzhiyun 		return ret;
433*4882a593Smuzhiyun 	}
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	result = le16_to_cpu(response.result_code);
436*4882a593Smuzhiyun 	if (result) {
437*4882a593Smuzhiyun 		dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
438*4882a593Smuzhiyun 			intf_id, attr, selector, result);
439*4882a593Smuzhiyun 		return -EREMOTEIO;
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	if (value)
443*4882a593Smuzhiyun 		*value = le32_to_cpu(response.attr_value);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	return 0;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun 
gb_svc_dme_peer_set(struct gb_svc * svc,u8 intf_id,u16 attr,u16 selector,u32 value)448*4882a593Smuzhiyun int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
449*4882a593Smuzhiyun 			u32 value)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun 	struct gb_svc_dme_peer_set_request request;
452*4882a593Smuzhiyun 	struct gb_svc_dme_peer_set_response response;
453*4882a593Smuzhiyun 	u16 result;
454*4882a593Smuzhiyun 	int ret;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	request.intf_id = intf_id;
457*4882a593Smuzhiyun 	request.attr = cpu_to_le16(attr);
458*4882a593Smuzhiyun 	request.selector = cpu_to_le16(selector);
459*4882a593Smuzhiyun 	request.value = cpu_to_le32(value);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
462*4882a593Smuzhiyun 				&request, sizeof(request),
463*4882a593Smuzhiyun 				&response, sizeof(response));
464*4882a593Smuzhiyun 	if (ret) {
465*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
466*4882a593Smuzhiyun 			intf_id, attr, selector, value, ret);
467*4882a593Smuzhiyun 		return ret;
468*4882a593Smuzhiyun 	}
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	result = le16_to_cpu(response.result_code);
471*4882a593Smuzhiyun 	if (result) {
472*4882a593Smuzhiyun 		dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
473*4882a593Smuzhiyun 			intf_id, attr, selector, value, result);
474*4882a593Smuzhiyun 		return -EREMOTEIO;
475*4882a593Smuzhiyun 	}
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	return 0;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
gb_svc_connection_create(struct gb_svc * svc,u8 intf1_id,u16 cport1_id,u8 intf2_id,u16 cport2_id,u8 cport_flags)480*4882a593Smuzhiyun int gb_svc_connection_create(struct gb_svc *svc,
481*4882a593Smuzhiyun 			     u8 intf1_id, u16 cport1_id,
482*4882a593Smuzhiyun 			     u8 intf2_id, u16 cport2_id,
483*4882a593Smuzhiyun 			     u8 cport_flags)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun 	struct gb_svc_conn_create_request request;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	request.intf1_id = intf1_id;
488*4882a593Smuzhiyun 	request.cport1_id = cpu_to_le16(cport1_id);
489*4882a593Smuzhiyun 	request.intf2_id = intf2_id;
490*4882a593Smuzhiyun 	request.cport2_id = cpu_to_le16(cport2_id);
491*4882a593Smuzhiyun 	request.tc = 0;		/* TC0 */
492*4882a593Smuzhiyun 	request.flags = cport_flags;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
495*4882a593Smuzhiyun 				 &request, sizeof(request), NULL, 0);
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun 
gb_svc_connection_destroy(struct gb_svc * svc,u8 intf1_id,u16 cport1_id,u8 intf2_id,u16 cport2_id)498*4882a593Smuzhiyun void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
499*4882a593Smuzhiyun 			       u8 intf2_id, u16 cport2_id)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	struct gb_svc_conn_destroy_request request;
502*4882a593Smuzhiyun 	struct gb_connection *connection = svc->connection;
503*4882a593Smuzhiyun 	int ret;
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	request.intf1_id = intf1_id;
506*4882a593Smuzhiyun 	request.cport1_id = cpu_to_le16(cport1_id);
507*4882a593Smuzhiyun 	request.intf2_id = intf2_id;
508*4882a593Smuzhiyun 	request.cport2_id = cpu_to_le16(cport2_id);
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
511*4882a593Smuzhiyun 				&request, sizeof(request), NULL, 0);
512*4882a593Smuzhiyun 	if (ret) {
513*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
514*4882a593Smuzhiyun 			intf1_id, cport1_id, intf2_id, cport2_id, ret);
515*4882a593Smuzhiyun 	}
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun /* Creates bi-directional routes between the devices */
gb_svc_route_create(struct gb_svc * svc,u8 intf1_id,u8 dev1_id,u8 intf2_id,u8 dev2_id)519*4882a593Smuzhiyun int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
520*4882a593Smuzhiyun 			u8 intf2_id, u8 dev2_id)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	struct gb_svc_route_create_request request;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	request.intf1_id = intf1_id;
525*4882a593Smuzhiyun 	request.dev1_id = dev1_id;
526*4882a593Smuzhiyun 	request.intf2_id = intf2_id;
527*4882a593Smuzhiyun 	request.dev2_id = dev2_id;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
530*4882a593Smuzhiyun 				 &request, sizeof(request), NULL, 0);
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun /* Destroys bi-directional routes between the devices */
gb_svc_route_destroy(struct gb_svc * svc,u8 intf1_id,u8 intf2_id)534*4882a593Smuzhiyun void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	struct gb_svc_route_destroy_request request;
537*4882a593Smuzhiyun 	int ret;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	request.intf1_id = intf1_id;
540*4882a593Smuzhiyun 	request.intf2_id = intf2_id;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
543*4882a593Smuzhiyun 				&request, sizeof(request), NULL, 0);
544*4882a593Smuzhiyun 	if (ret) {
545*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
546*4882a593Smuzhiyun 			intf1_id, intf2_id, ret);
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun 
gb_svc_intf_set_power_mode(struct gb_svc * svc,u8 intf_id,u8 hs_series,u8 tx_mode,u8 tx_gear,u8 tx_nlanes,u8 tx_amplitude,u8 tx_hs_equalizer,u8 rx_mode,u8 rx_gear,u8 rx_nlanes,u8 flags,u32 quirks,struct gb_svc_l2_timer_cfg * local,struct gb_svc_l2_timer_cfg * remote)550*4882a593Smuzhiyun int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
551*4882a593Smuzhiyun 			       u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
552*4882a593Smuzhiyun 			       u8 tx_amplitude, u8 tx_hs_equalizer,
553*4882a593Smuzhiyun 			       u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
554*4882a593Smuzhiyun 			       u8 flags, u32 quirks,
555*4882a593Smuzhiyun 			       struct gb_svc_l2_timer_cfg *local,
556*4882a593Smuzhiyun 			       struct gb_svc_l2_timer_cfg *remote)
557*4882a593Smuzhiyun {
558*4882a593Smuzhiyun 	struct gb_svc_intf_set_pwrm_request request;
559*4882a593Smuzhiyun 	struct gb_svc_intf_set_pwrm_response response;
560*4882a593Smuzhiyun 	int ret;
561*4882a593Smuzhiyun 	u16 result_code;
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	request.intf_id = intf_id;
566*4882a593Smuzhiyun 	request.hs_series = hs_series;
567*4882a593Smuzhiyun 	request.tx_mode = tx_mode;
568*4882a593Smuzhiyun 	request.tx_gear = tx_gear;
569*4882a593Smuzhiyun 	request.tx_nlanes = tx_nlanes;
570*4882a593Smuzhiyun 	request.tx_amplitude = tx_amplitude;
571*4882a593Smuzhiyun 	request.tx_hs_equalizer = tx_hs_equalizer;
572*4882a593Smuzhiyun 	request.rx_mode = rx_mode;
573*4882a593Smuzhiyun 	request.rx_gear = rx_gear;
574*4882a593Smuzhiyun 	request.rx_nlanes = rx_nlanes;
575*4882a593Smuzhiyun 	request.flags = flags;
576*4882a593Smuzhiyun 	request.quirks = cpu_to_le32(quirks);
577*4882a593Smuzhiyun 	if (local)
578*4882a593Smuzhiyun 		request.local_l2timerdata = *local;
579*4882a593Smuzhiyun 	if (remote)
580*4882a593Smuzhiyun 		request.remote_l2timerdata = *remote;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
583*4882a593Smuzhiyun 				&request, sizeof(request),
584*4882a593Smuzhiyun 				&response, sizeof(response));
585*4882a593Smuzhiyun 	if (ret < 0)
586*4882a593Smuzhiyun 		return ret;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	result_code = response.result_code;
589*4882a593Smuzhiyun 	if (result_code != GB_SVC_SETPWRM_PWR_LOCAL) {
590*4882a593Smuzhiyun 		dev_err(&svc->dev, "set power mode = %d\n", result_code);
591*4882a593Smuzhiyun 		return -EIO;
592*4882a593Smuzhiyun 	}
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	return 0;
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
597*4882a593Smuzhiyun 
gb_svc_intf_set_power_mode_hibernate(struct gb_svc * svc,u8 intf_id)598*4882a593Smuzhiyun int gb_svc_intf_set_power_mode_hibernate(struct gb_svc *svc, u8 intf_id)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun 	struct gb_svc_intf_set_pwrm_request request;
601*4882a593Smuzhiyun 	struct gb_svc_intf_set_pwrm_response response;
602*4882a593Smuzhiyun 	int ret;
603*4882a593Smuzhiyun 	u16 result_code;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	request.intf_id = intf_id;
608*4882a593Smuzhiyun 	request.hs_series = GB_SVC_UNIPRO_HS_SERIES_A;
609*4882a593Smuzhiyun 	request.tx_mode = GB_SVC_UNIPRO_HIBERNATE_MODE;
610*4882a593Smuzhiyun 	request.rx_mode = GB_SVC_UNIPRO_HIBERNATE_MODE;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
613*4882a593Smuzhiyun 				&request, sizeof(request),
614*4882a593Smuzhiyun 				&response, sizeof(response));
615*4882a593Smuzhiyun 	if (ret < 0) {
616*4882a593Smuzhiyun 		dev_err(&svc->dev,
617*4882a593Smuzhiyun 			"failed to send set power mode operation to interface %u: %d\n",
618*4882a593Smuzhiyun 			intf_id, ret);
619*4882a593Smuzhiyun 		return ret;
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	result_code = response.result_code;
623*4882a593Smuzhiyun 	if (result_code != GB_SVC_SETPWRM_PWR_OK) {
624*4882a593Smuzhiyun 		dev_err(&svc->dev,
625*4882a593Smuzhiyun 			"failed to hibernate the link for interface %u: %u\n",
626*4882a593Smuzhiyun 			intf_id, result_code);
627*4882a593Smuzhiyun 		return -EIO;
628*4882a593Smuzhiyun 	}
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	return 0;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun 
gb_svc_ping(struct gb_svc * svc)633*4882a593Smuzhiyun int gb_svc_ping(struct gb_svc *svc)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun 	return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
636*4882a593Smuzhiyun 					 NULL, 0, NULL, 0,
637*4882a593Smuzhiyun 					 GB_OPERATION_TIMEOUT_DEFAULT * 2);
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun 
gb_svc_version_request(struct gb_operation * op)640*4882a593Smuzhiyun static int gb_svc_version_request(struct gb_operation *op)
641*4882a593Smuzhiyun {
642*4882a593Smuzhiyun 	struct gb_connection *connection = op->connection;
643*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(connection);
644*4882a593Smuzhiyun 	struct gb_svc_version_request *request;
645*4882a593Smuzhiyun 	struct gb_svc_version_response *response;
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	if (op->request->payload_size < sizeof(*request)) {
648*4882a593Smuzhiyun 		dev_err(&svc->dev, "short version request (%zu < %zu)\n",
649*4882a593Smuzhiyun 			op->request->payload_size,
650*4882a593Smuzhiyun 			sizeof(*request));
651*4882a593Smuzhiyun 		return -EINVAL;
652*4882a593Smuzhiyun 	}
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	request = op->request->payload;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	if (request->major > GB_SVC_VERSION_MAJOR) {
657*4882a593Smuzhiyun 		dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
658*4882a593Smuzhiyun 			 request->major, GB_SVC_VERSION_MAJOR);
659*4882a593Smuzhiyun 		return -ENOTSUPP;
660*4882a593Smuzhiyun 	}
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	svc->protocol_major = request->major;
663*4882a593Smuzhiyun 	svc->protocol_minor = request->minor;
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
666*4882a593Smuzhiyun 		return -ENOMEM;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	response = op->response->payload;
669*4882a593Smuzhiyun 	response->major = svc->protocol_major;
670*4882a593Smuzhiyun 	response->minor = svc->protocol_minor;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	return 0;
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun 
pwr_debugfs_voltage_read(struct file * file,char __user * buf,size_t len,loff_t * offset)675*4882a593Smuzhiyun static ssize_t pwr_debugfs_voltage_read(struct file *file, char __user *buf,
676*4882a593Smuzhiyun 					size_t len, loff_t *offset)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun 	struct svc_debugfs_pwrmon_rail *pwrmon_rails =
679*4882a593Smuzhiyun 		file_inode(file)->i_private;
680*4882a593Smuzhiyun 	struct gb_svc *svc = pwrmon_rails->svc;
681*4882a593Smuzhiyun 	int ret, desc;
682*4882a593Smuzhiyun 	u32 value;
683*4882a593Smuzhiyun 	char buff[16];
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
686*4882a593Smuzhiyun 				       GB_SVC_PWRMON_TYPE_VOL, &value);
687*4882a593Smuzhiyun 	if (ret) {
688*4882a593Smuzhiyun 		dev_err(&svc->dev,
689*4882a593Smuzhiyun 			"failed to get voltage sample %u: %d\n",
690*4882a593Smuzhiyun 			pwrmon_rails->id, ret);
691*4882a593Smuzhiyun 		return ret;
692*4882a593Smuzhiyun 	}
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	desc = scnprintf(buff, sizeof(buff), "%u\n", value);
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	return simple_read_from_buffer(buf, len, offset, buff, desc);
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun 
pwr_debugfs_current_read(struct file * file,char __user * buf,size_t len,loff_t * offset)699*4882a593Smuzhiyun static ssize_t pwr_debugfs_current_read(struct file *file, char __user *buf,
700*4882a593Smuzhiyun 					size_t len, loff_t *offset)
701*4882a593Smuzhiyun {
702*4882a593Smuzhiyun 	struct svc_debugfs_pwrmon_rail *pwrmon_rails =
703*4882a593Smuzhiyun 		file_inode(file)->i_private;
704*4882a593Smuzhiyun 	struct gb_svc *svc = pwrmon_rails->svc;
705*4882a593Smuzhiyun 	int ret, desc;
706*4882a593Smuzhiyun 	u32 value;
707*4882a593Smuzhiyun 	char buff[16];
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
710*4882a593Smuzhiyun 				       GB_SVC_PWRMON_TYPE_CURR, &value);
711*4882a593Smuzhiyun 	if (ret) {
712*4882a593Smuzhiyun 		dev_err(&svc->dev,
713*4882a593Smuzhiyun 			"failed to get current sample %u: %d\n",
714*4882a593Smuzhiyun 			pwrmon_rails->id, ret);
715*4882a593Smuzhiyun 		return ret;
716*4882a593Smuzhiyun 	}
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	desc = scnprintf(buff, sizeof(buff), "%u\n", value);
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	return simple_read_from_buffer(buf, len, offset, buff, desc);
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun 
pwr_debugfs_power_read(struct file * file,char __user * buf,size_t len,loff_t * offset)723*4882a593Smuzhiyun static ssize_t pwr_debugfs_power_read(struct file *file, char __user *buf,
724*4882a593Smuzhiyun 				      size_t len, loff_t *offset)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun 	struct svc_debugfs_pwrmon_rail *pwrmon_rails =
727*4882a593Smuzhiyun 		file_inode(file)->i_private;
728*4882a593Smuzhiyun 	struct gb_svc *svc = pwrmon_rails->svc;
729*4882a593Smuzhiyun 	int ret, desc;
730*4882a593Smuzhiyun 	u32 value;
731*4882a593Smuzhiyun 	char buff[16];
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
734*4882a593Smuzhiyun 				       GB_SVC_PWRMON_TYPE_PWR, &value);
735*4882a593Smuzhiyun 	if (ret) {
736*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to get power sample %u: %d\n",
737*4882a593Smuzhiyun 			pwrmon_rails->id, ret);
738*4882a593Smuzhiyun 		return ret;
739*4882a593Smuzhiyun 	}
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	desc = scnprintf(buff, sizeof(buff), "%u\n", value);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	return simple_read_from_buffer(buf, len, offset, buff, desc);
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun static const struct file_operations pwrmon_debugfs_voltage_fops = {
747*4882a593Smuzhiyun 	.read		= pwr_debugfs_voltage_read,
748*4882a593Smuzhiyun };
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun static const struct file_operations pwrmon_debugfs_current_fops = {
751*4882a593Smuzhiyun 	.read		= pwr_debugfs_current_read,
752*4882a593Smuzhiyun };
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun static const struct file_operations pwrmon_debugfs_power_fops = {
755*4882a593Smuzhiyun 	.read		= pwr_debugfs_power_read,
756*4882a593Smuzhiyun };
757*4882a593Smuzhiyun 
gb_svc_pwrmon_debugfs_init(struct gb_svc * svc)758*4882a593Smuzhiyun static void gb_svc_pwrmon_debugfs_init(struct gb_svc *svc)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun 	int i;
761*4882a593Smuzhiyun 	size_t bufsize;
762*4882a593Smuzhiyun 	struct dentry *dent;
763*4882a593Smuzhiyun 	struct gb_svc_pwrmon_rail_names_get_response *rail_names;
764*4882a593Smuzhiyun 	u8 rail_count;
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	dent = debugfs_create_dir("pwrmon", svc->debugfs_dentry);
767*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(dent))
768*4882a593Smuzhiyun 		return;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	if (gb_svc_pwrmon_rail_count_get(svc, &rail_count))
771*4882a593Smuzhiyun 		goto err_pwrmon_debugfs;
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	if (!rail_count || rail_count > GB_SVC_PWRMON_MAX_RAIL_COUNT)
774*4882a593Smuzhiyun 		goto err_pwrmon_debugfs;
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	bufsize = sizeof(*rail_names) +
777*4882a593Smuzhiyun 		GB_SVC_PWRMON_RAIL_NAME_BUFSIZE * rail_count;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	rail_names = kzalloc(bufsize, GFP_KERNEL);
780*4882a593Smuzhiyun 	if (!rail_names)
781*4882a593Smuzhiyun 		goto err_pwrmon_debugfs;
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	svc->pwrmon_rails = kcalloc(rail_count, sizeof(*svc->pwrmon_rails),
784*4882a593Smuzhiyun 				    GFP_KERNEL);
785*4882a593Smuzhiyun 	if (!svc->pwrmon_rails)
786*4882a593Smuzhiyun 		goto err_pwrmon_debugfs_free;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	if (gb_svc_pwrmon_rail_names_get(svc, rail_names, bufsize))
789*4882a593Smuzhiyun 		goto err_pwrmon_debugfs_free;
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	for (i = 0; i < rail_count; i++) {
792*4882a593Smuzhiyun 		struct dentry *dir;
793*4882a593Smuzhiyun 		struct svc_debugfs_pwrmon_rail *rail = &svc->pwrmon_rails[i];
794*4882a593Smuzhiyun 		char fname[GB_SVC_PWRMON_RAIL_NAME_BUFSIZE];
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 		snprintf(fname, sizeof(fname), "%s",
797*4882a593Smuzhiyun 			 (char *)&rail_names->name[i]);
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 		rail->id = i;
800*4882a593Smuzhiyun 		rail->svc = svc;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 		dir = debugfs_create_dir(fname, dent);
803*4882a593Smuzhiyun 		debugfs_create_file("voltage_now", 0444, dir, rail,
804*4882a593Smuzhiyun 				    &pwrmon_debugfs_voltage_fops);
805*4882a593Smuzhiyun 		debugfs_create_file("current_now", 0444, dir, rail,
806*4882a593Smuzhiyun 				    &pwrmon_debugfs_current_fops);
807*4882a593Smuzhiyun 		debugfs_create_file("power_now", 0444, dir, rail,
808*4882a593Smuzhiyun 				    &pwrmon_debugfs_power_fops);
809*4882a593Smuzhiyun 	}
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	kfree(rail_names);
812*4882a593Smuzhiyun 	return;
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun err_pwrmon_debugfs_free:
815*4882a593Smuzhiyun 	kfree(rail_names);
816*4882a593Smuzhiyun 	kfree(svc->pwrmon_rails);
817*4882a593Smuzhiyun 	svc->pwrmon_rails = NULL;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun err_pwrmon_debugfs:
820*4882a593Smuzhiyun 	debugfs_remove(dent);
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun 
gb_svc_debugfs_init(struct gb_svc * svc)823*4882a593Smuzhiyun static void gb_svc_debugfs_init(struct gb_svc *svc)
824*4882a593Smuzhiyun {
825*4882a593Smuzhiyun 	svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev),
826*4882a593Smuzhiyun 						 gb_debugfs_get());
827*4882a593Smuzhiyun 	gb_svc_pwrmon_debugfs_init(svc);
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun 
gb_svc_debugfs_exit(struct gb_svc * svc)830*4882a593Smuzhiyun static void gb_svc_debugfs_exit(struct gb_svc *svc)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun 	debugfs_remove_recursive(svc->debugfs_dentry);
833*4882a593Smuzhiyun 	kfree(svc->pwrmon_rails);
834*4882a593Smuzhiyun 	svc->pwrmon_rails = NULL;
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun 
gb_svc_hello(struct gb_operation * op)837*4882a593Smuzhiyun static int gb_svc_hello(struct gb_operation *op)
838*4882a593Smuzhiyun {
839*4882a593Smuzhiyun 	struct gb_connection *connection = op->connection;
840*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(connection);
841*4882a593Smuzhiyun 	struct gb_svc_hello_request *hello_request;
842*4882a593Smuzhiyun 	int ret;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	if (op->request->payload_size < sizeof(*hello_request)) {
845*4882a593Smuzhiyun 		dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
846*4882a593Smuzhiyun 			 op->request->payload_size,
847*4882a593Smuzhiyun 			 sizeof(*hello_request));
848*4882a593Smuzhiyun 		return -EINVAL;
849*4882a593Smuzhiyun 	}
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	hello_request = op->request->payload;
852*4882a593Smuzhiyun 	svc->endo_id = le16_to_cpu(hello_request->endo_id);
853*4882a593Smuzhiyun 	svc->ap_intf_id = hello_request->interface_id;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	ret = device_add(&svc->dev);
856*4882a593Smuzhiyun 	if (ret) {
857*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
858*4882a593Smuzhiyun 		return ret;
859*4882a593Smuzhiyun 	}
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	ret = gb_svc_watchdog_create(svc);
862*4882a593Smuzhiyun 	if (ret) {
863*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
864*4882a593Smuzhiyun 		goto err_unregister_device;
865*4882a593Smuzhiyun 	}
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 	gb_svc_debugfs_init(svc);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	ret = gb_svc_queue_deferred_request(op);
870*4882a593Smuzhiyun 	if (ret)
871*4882a593Smuzhiyun 		goto err_remove_debugfs;
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	return 0;
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun err_remove_debugfs:
876*4882a593Smuzhiyun 	gb_svc_debugfs_exit(svc);
877*4882a593Smuzhiyun err_unregister_device:
878*4882a593Smuzhiyun 	gb_svc_watchdog_destroy(svc);
879*4882a593Smuzhiyun 	device_del(&svc->dev);
880*4882a593Smuzhiyun 	return ret;
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun 
gb_svc_interface_lookup(struct gb_svc * svc,u8 intf_id)883*4882a593Smuzhiyun static struct gb_interface *gb_svc_interface_lookup(struct gb_svc *svc,
884*4882a593Smuzhiyun 						    u8 intf_id)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun 	struct gb_host_device *hd = svc->hd;
887*4882a593Smuzhiyun 	struct gb_module *module;
888*4882a593Smuzhiyun 	size_t num_interfaces;
889*4882a593Smuzhiyun 	u8 module_id;
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	list_for_each_entry(module, &hd->modules, hd_node) {
892*4882a593Smuzhiyun 		module_id = module->module_id;
893*4882a593Smuzhiyun 		num_interfaces = module->num_interfaces;
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 		if (intf_id >= module_id &&
896*4882a593Smuzhiyun 		    intf_id < module_id + num_interfaces) {
897*4882a593Smuzhiyun 			return module->interfaces[intf_id - module_id];
898*4882a593Smuzhiyun 		}
899*4882a593Smuzhiyun 	}
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	return NULL;
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun 
gb_svc_module_lookup(struct gb_svc * svc,u8 module_id)904*4882a593Smuzhiyun static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id)
905*4882a593Smuzhiyun {
906*4882a593Smuzhiyun 	struct gb_host_device *hd = svc->hd;
907*4882a593Smuzhiyun 	struct gb_module *module;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	list_for_each_entry(module, &hd->modules, hd_node) {
910*4882a593Smuzhiyun 		if (module->module_id == module_id)
911*4882a593Smuzhiyun 			return module;
912*4882a593Smuzhiyun 	}
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	return NULL;
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun 
gb_svc_process_hello_deferred(struct gb_operation * operation)917*4882a593Smuzhiyun static void gb_svc_process_hello_deferred(struct gb_operation *operation)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun 	struct gb_connection *connection = operation->connection;
920*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(connection);
921*4882a593Smuzhiyun 	int ret;
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	/*
924*4882a593Smuzhiyun 	 * XXX This is a hack/work-around to reconfigure the APBridgeA-Switch
925*4882a593Smuzhiyun 	 * link to PWM G2, 1 Lane, Slow Auto, so that it has sufficient
926*4882a593Smuzhiyun 	 * bandwidth for 3 audio streams plus boot-over-UniPro of a hot-plugged
927*4882a593Smuzhiyun 	 * module.
928*4882a593Smuzhiyun 	 *
929*4882a593Smuzhiyun 	 * The code should be removed once SW-2217, Heuristic for UniPro
930*4882a593Smuzhiyun 	 * Power Mode Changes is resolved.
931*4882a593Smuzhiyun 	 */
932*4882a593Smuzhiyun 	ret = gb_svc_intf_set_power_mode(svc, svc->ap_intf_id,
933*4882a593Smuzhiyun 					 GB_SVC_UNIPRO_HS_SERIES_A,
934*4882a593Smuzhiyun 					 GB_SVC_UNIPRO_SLOW_AUTO_MODE,
935*4882a593Smuzhiyun 					 2, 1,
936*4882a593Smuzhiyun 					 GB_SVC_SMALL_AMPLITUDE,
937*4882a593Smuzhiyun 					 GB_SVC_NO_DE_EMPHASIS,
938*4882a593Smuzhiyun 					 GB_SVC_UNIPRO_SLOW_AUTO_MODE,
939*4882a593Smuzhiyun 					 2, 1,
940*4882a593Smuzhiyun 					 0, 0,
941*4882a593Smuzhiyun 					 NULL, NULL);
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	if (ret)
944*4882a593Smuzhiyun 		dev_warn(&svc->dev,
945*4882a593Smuzhiyun 			 "power mode change failed on AP to switch link: %d\n",
946*4882a593Smuzhiyun 			 ret);
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun 
gb_svc_process_module_inserted(struct gb_operation * operation)949*4882a593Smuzhiyun static void gb_svc_process_module_inserted(struct gb_operation *operation)
950*4882a593Smuzhiyun {
951*4882a593Smuzhiyun 	struct gb_svc_module_inserted_request *request;
952*4882a593Smuzhiyun 	struct gb_connection *connection = operation->connection;
953*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(connection);
954*4882a593Smuzhiyun 	struct gb_host_device *hd = svc->hd;
955*4882a593Smuzhiyun 	struct gb_module *module;
956*4882a593Smuzhiyun 	size_t num_interfaces;
957*4882a593Smuzhiyun 	u8 module_id;
958*4882a593Smuzhiyun 	u16 flags;
959*4882a593Smuzhiyun 	int ret;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	/* The request message size has already been verified. */
962*4882a593Smuzhiyun 	request = operation->request->payload;
963*4882a593Smuzhiyun 	module_id = request->primary_intf_id;
964*4882a593Smuzhiyun 	num_interfaces = request->intf_count;
965*4882a593Smuzhiyun 	flags = le16_to_cpu(request->flags);
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	dev_dbg(&svc->dev, "%s - id = %u, num_interfaces = %zu, flags = 0x%04x\n",
968*4882a593Smuzhiyun 		__func__, module_id, num_interfaces, flags);
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	if (flags & GB_SVC_MODULE_INSERTED_FLAG_NO_PRIMARY) {
971*4882a593Smuzhiyun 		dev_warn(&svc->dev, "no primary interface detected on module %u\n",
972*4882a593Smuzhiyun 			 module_id);
973*4882a593Smuzhiyun 	}
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 	module = gb_svc_module_lookup(svc, module_id);
976*4882a593Smuzhiyun 	if (module) {
977*4882a593Smuzhiyun 		dev_warn(&svc->dev, "unexpected module-inserted event %u\n",
978*4882a593Smuzhiyun 			 module_id);
979*4882a593Smuzhiyun 		return;
980*4882a593Smuzhiyun 	}
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	module = gb_module_create(hd, module_id, num_interfaces);
983*4882a593Smuzhiyun 	if (!module) {
984*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to create module\n");
985*4882a593Smuzhiyun 		return;
986*4882a593Smuzhiyun 	}
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	ret = gb_module_add(module);
989*4882a593Smuzhiyun 	if (ret) {
990*4882a593Smuzhiyun 		gb_module_put(module);
991*4882a593Smuzhiyun 		return;
992*4882a593Smuzhiyun 	}
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 	list_add(&module->hd_node, &hd->modules);
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun 
gb_svc_process_module_removed(struct gb_operation * operation)997*4882a593Smuzhiyun static void gb_svc_process_module_removed(struct gb_operation *operation)
998*4882a593Smuzhiyun {
999*4882a593Smuzhiyun 	struct gb_svc_module_removed_request *request;
1000*4882a593Smuzhiyun 	struct gb_connection *connection = operation->connection;
1001*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(connection);
1002*4882a593Smuzhiyun 	struct gb_module *module;
1003*4882a593Smuzhiyun 	u8 module_id;
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun 	/* The request message size has already been verified. */
1006*4882a593Smuzhiyun 	request = operation->request->payload;
1007*4882a593Smuzhiyun 	module_id = request->primary_intf_id;
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	dev_dbg(&svc->dev, "%s - id = %u\n", __func__, module_id);
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	module = gb_svc_module_lookup(svc, module_id);
1012*4882a593Smuzhiyun 	if (!module) {
1013*4882a593Smuzhiyun 		dev_warn(&svc->dev, "unexpected module-removed event %u\n",
1014*4882a593Smuzhiyun 			 module_id);
1015*4882a593Smuzhiyun 		return;
1016*4882a593Smuzhiyun 	}
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	module->disconnected = true;
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	gb_module_del(module);
1021*4882a593Smuzhiyun 	list_del(&module->hd_node);
1022*4882a593Smuzhiyun 	gb_module_put(module);
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun 
gb_svc_process_intf_oops(struct gb_operation * operation)1025*4882a593Smuzhiyun static void gb_svc_process_intf_oops(struct gb_operation *operation)
1026*4882a593Smuzhiyun {
1027*4882a593Smuzhiyun 	struct gb_svc_intf_oops_request *request;
1028*4882a593Smuzhiyun 	struct gb_connection *connection = operation->connection;
1029*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(connection);
1030*4882a593Smuzhiyun 	struct gb_interface *intf;
1031*4882a593Smuzhiyun 	u8 intf_id;
1032*4882a593Smuzhiyun 	u8 reason;
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 	/* The request message size has already been verified. */
1035*4882a593Smuzhiyun 	request = operation->request->payload;
1036*4882a593Smuzhiyun 	intf_id = request->intf_id;
1037*4882a593Smuzhiyun 	reason = request->reason;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	intf = gb_svc_interface_lookup(svc, intf_id);
1040*4882a593Smuzhiyun 	if (!intf) {
1041*4882a593Smuzhiyun 		dev_warn(&svc->dev, "unexpected interface-oops event %u\n",
1042*4882a593Smuzhiyun 			 intf_id);
1043*4882a593Smuzhiyun 		return;
1044*4882a593Smuzhiyun 	}
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	dev_info(&svc->dev, "Deactivating interface %u, interface oops reason = %u\n",
1047*4882a593Smuzhiyun 		 intf_id, reason);
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	mutex_lock(&intf->mutex);
1050*4882a593Smuzhiyun 	intf->disconnected = true;
1051*4882a593Smuzhiyun 	gb_interface_disable(intf);
1052*4882a593Smuzhiyun 	gb_interface_deactivate(intf);
1053*4882a593Smuzhiyun 	mutex_unlock(&intf->mutex);
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun 
gb_svc_process_intf_mailbox_event(struct gb_operation * operation)1056*4882a593Smuzhiyun static void gb_svc_process_intf_mailbox_event(struct gb_operation *operation)
1057*4882a593Smuzhiyun {
1058*4882a593Smuzhiyun 	struct gb_svc_intf_mailbox_event_request *request;
1059*4882a593Smuzhiyun 	struct gb_connection *connection = operation->connection;
1060*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(connection);
1061*4882a593Smuzhiyun 	struct gb_interface *intf;
1062*4882a593Smuzhiyun 	u8 intf_id;
1063*4882a593Smuzhiyun 	u16 result_code;
1064*4882a593Smuzhiyun 	u32 mailbox;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	/* The request message size has already been verified. */
1067*4882a593Smuzhiyun 	request = operation->request->payload;
1068*4882a593Smuzhiyun 	intf_id = request->intf_id;
1069*4882a593Smuzhiyun 	result_code = le16_to_cpu(request->result_code);
1070*4882a593Smuzhiyun 	mailbox = le32_to_cpu(request->mailbox);
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	dev_dbg(&svc->dev, "%s - id = %u, result = 0x%04x, mailbox = 0x%08x\n",
1073*4882a593Smuzhiyun 		__func__, intf_id, result_code, mailbox);
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 	intf = gb_svc_interface_lookup(svc, intf_id);
1076*4882a593Smuzhiyun 	if (!intf) {
1077*4882a593Smuzhiyun 		dev_warn(&svc->dev, "unexpected mailbox event %u\n", intf_id);
1078*4882a593Smuzhiyun 		return;
1079*4882a593Smuzhiyun 	}
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	gb_interface_mailbox_event(intf, result_code, mailbox);
1082*4882a593Smuzhiyun }
1083*4882a593Smuzhiyun 
gb_svc_process_deferred_request(struct work_struct * work)1084*4882a593Smuzhiyun static void gb_svc_process_deferred_request(struct work_struct *work)
1085*4882a593Smuzhiyun {
1086*4882a593Smuzhiyun 	struct gb_svc_deferred_request *dr;
1087*4882a593Smuzhiyun 	struct gb_operation *operation;
1088*4882a593Smuzhiyun 	struct gb_svc *svc;
1089*4882a593Smuzhiyun 	u8 type;
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	dr = container_of(work, struct gb_svc_deferred_request, work);
1092*4882a593Smuzhiyun 	operation = dr->operation;
1093*4882a593Smuzhiyun 	svc = gb_connection_get_data(operation->connection);
1094*4882a593Smuzhiyun 	type = operation->request->header->type;
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	switch (type) {
1097*4882a593Smuzhiyun 	case GB_SVC_TYPE_SVC_HELLO:
1098*4882a593Smuzhiyun 		gb_svc_process_hello_deferred(operation);
1099*4882a593Smuzhiyun 		break;
1100*4882a593Smuzhiyun 	case GB_SVC_TYPE_MODULE_INSERTED:
1101*4882a593Smuzhiyun 		gb_svc_process_module_inserted(operation);
1102*4882a593Smuzhiyun 		break;
1103*4882a593Smuzhiyun 	case GB_SVC_TYPE_MODULE_REMOVED:
1104*4882a593Smuzhiyun 		gb_svc_process_module_removed(operation);
1105*4882a593Smuzhiyun 		break;
1106*4882a593Smuzhiyun 	case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
1107*4882a593Smuzhiyun 		gb_svc_process_intf_mailbox_event(operation);
1108*4882a593Smuzhiyun 		break;
1109*4882a593Smuzhiyun 	case GB_SVC_TYPE_INTF_OOPS:
1110*4882a593Smuzhiyun 		gb_svc_process_intf_oops(operation);
1111*4882a593Smuzhiyun 		break;
1112*4882a593Smuzhiyun 	default:
1113*4882a593Smuzhiyun 		dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
1114*4882a593Smuzhiyun 	}
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	gb_operation_put(operation);
1117*4882a593Smuzhiyun 	kfree(dr);
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun 
gb_svc_queue_deferred_request(struct gb_operation * operation)1120*4882a593Smuzhiyun static int gb_svc_queue_deferred_request(struct gb_operation *operation)
1121*4882a593Smuzhiyun {
1122*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(operation->connection);
1123*4882a593Smuzhiyun 	struct gb_svc_deferred_request *dr;
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1126*4882a593Smuzhiyun 	if (!dr)
1127*4882a593Smuzhiyun 		return -ENOMEM;
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	gb_operation_get(operation);
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	dr->operation = operation;
1132*4882a593Smuzhiyun 	INIT_WORK(&dr->work, gb_svc_process_deferred_request);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	queue_work(svc->wq, &dr->work);
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 	return 0;
1137*4882a593Smuzhiyun }
1138*4882a593Smuzhiyun 
gb_svc_intf_reset_recv(struct gb_operation * op)1139*4882a593Smuzhiyun static int gb_svc_intf_reset_recv(struct gb_operation *op)
1140*4882a593Smuzhiyun {
1141*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(op->connection);
1142*4882a593Smuzhiyun 	struct gb_message *request = op->request;
1143*4882a593Smuzhiyun 	struct gb_svc_intf_reset_request *reset;
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	if (request->payload_size < sizeof(*reset)) {
1146*4882a593Smuzhiyun 		dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
1147*4882a593Smuzhiyun 			 request->payload_size, sizeof(*reset));
1148*4882a593Smuzhiyun 		return -EINVAL;
1149*4882a593Smuzhiyun 	}
1150*4882a593Smuzhiyun 	reset = request->payload;
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	/* FIXME Reset the interface here */
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	return 0;
1155*4882a593Smuzhiyun }
1156*4882a593Smuzhiyun 
gb_svc_module_inserted_recv(struct gb_operation * op)1157*4882a593Smuzhiyun static int gb_svc_module_inserted_recv(struct gb_operation *op)
1158*4882a593Smuzhiyun {
1159*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(op->connection);
1160*4882a593Smuzhiyun 	struct gb_svc_module_inserted_request *request;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	if (op->request->payload_size < sizeof(*request)) {
1163*4882a593Smuzhiyun 		dev_warn(&svc->dev, "short module-inserted request received (%zu < %zu)\n",
1164*4882a593Smuzhiyun 			 op->request->payload_size, sizeof(*request));
1165*4882a593Smuzhiyun 		return -EINVAL;
1166*4882a593Smuzhiyun 	}
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 	request = op->request->payload;
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun 	dev_dbg(&svc->dev, "%s - id = %u\n", __func__,
1171*4882a593Smuzhiyun 		request->primary_intf_id);
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	return gb_svc_queue_deferred_request(op);
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun 
gb_svc_module_removed_recv(struct gb_operation * op)1176*4882a593Smuzhiyun static int gb_svc_module_removed_recv(struct gb_operation *op)
1177*4882a593Smuzhiyun {
1178*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(op->connection);
1179*4882a593Smuzhiyun 	struct gb_svc_module_removed_request *request;
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun 	if (op->request->payload_size < sizeof(*request)) {
1182*4882a593Smuzhiyun 		dev_warn(&svc->dev, "short module-removed request received (%zu < %zu)\n",
1183*4882a593Smuzhiyun 			 op->request->payload_size, sizeof(*request));
1184*4882a593Smuzhiyun 		return -EINVAL;
1185*4882a593Smuzhiyun 	}
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun 	request = op->request->payload;
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	dev_dbg(&svc->dev, "%s - id = %u\n", __func__,
1190*4882a593Smuzhiyun 		request->primary_intf_id);
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	return gb_svc_queue_deferred_request(op);
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun 
gb_svc_intf_oops_recv(struct gb_operation * op)1195*4882a593Smuzhiyun static int gb_svc_intf_oops_recv(struct gb_operation *op)
1196*4882a593Smuzhiyun {
1197*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(op->connection);
1198*4882a593Smuzhiyun 	struct gb_svc_intf_oops_request *request;
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun 	if (op->request->payload_size < sizeof(*request)) {
1201*4882a593Smuzhiyun 		dev_warn(&svc->dev, "short intf-oops request received (%zu < %zu)\n",
1202*4882a593Smuzhiyun 			 op->request->payload_size, sizeof(*request));
1203*4882a593Smuzhiyun 		return -EINVAL;
1204*4882a593Smuzhiyun 	}
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 	return gb_svc_queue_deferred_request(op);
1207*4882a593Smuzhiyun }
1208*4882a593Smuzhiyun 
gb_svc_intf_mailbox_event_recv(struct gb_operation * op)1209*4882a593Smuzhiyun static int gb_svc_intf_mailbox_event_recv(struct gb_operation *op)
1210*4882a593Smuzhiyun {
1211*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(op->connection);
1212*4882a593Smuzhiyun 	struct gb_svc_intf_mailbox_event_request *request;
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	if (op->request->payload_size < sizeof(*request)) {
1215*4882a593Smuzhiyun 		dev_warn(&svc->dev, "short mailbox request received (%zu < %zu)\n",
1216*4882a593Smuzhiyun 			 op->request->payload_size, sizeof(*request));
1217*4882a593Smuzhiyun 		return -EINVAL;
1218*4882a593Smuzhiyun 	}
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 	request = op->request->payload;
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 	return gb_svc_queue_deferred_request(op);
1225*4882a593Smuzhiyun }
1226*4882a593Smuzhiyun 
gb_svc_request_handler(struct gb_operation * op)1227*4882a593Smuzhiyun static int gb_svc_request_handler(struct gb_operation *op)
1228*4882a593Smuzhiyun {
1229*4882a593Smuzhiyun 	struct gb_connection *connection = op->connection;
1230*4882a593Smuzhiyun 	struct gb_svc *svc = gb_connection_get_data(connection);
1231*4882a593Smuzhiyun 	u8 type = op->type;
1232*4882a593Smuzhiyun 	int ret = 0;
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	/*
1235*4882a593Smuzhiyun 	 * SVC requests need to follow a specific order (at least initially) and
1236*4882a593Smuzhiyun 	 * below code takes care of enforcing that. The expected order is:
1237*4882a593Smuzhiyun 	 * - PROTOCOL_VERSION
1238*4882a593Smuzhiyun 	 * - SVC_HELLO
1239*4882a593Smuzhiyun 	 * - Any other request, but the earlier two.
1240*4882a593Smuzhiyun 	 *
1241*4882a593Smuzhiyun 	 * Incoming requests are guaranteed to be serialized and so we don't
1242*4882a593Smuzhiyun 	 * need to protect 'state' for any races.
1243*4882a593Smuzhiyun 	 */
1244*4882a593Smuzhiyun 	switch (type) {
1245*4882a593Smuzhiyun 	case GB_SVC_TYPE_PROTOCOL_VERSION:
1246*4882a593Smuzhiyun 		if (svc->state != GB_SVC_STATE_RESET)
1247*4882a593Smuzhiyun 			ret = -EINVAL;
1248*4882a593Smuzhiyun 		break;
1249*4882a593Smuzhiyun 	case GB_SVC_TYPE_SVC_HELLO:
1250*4882a593Smuzhiyun 		if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
1251*4882a593Smuzhiyun 			ret = -EINVAL;
1252*4882a593Smuzhiyun 		break;
1253*4882a593Smuzhiyun 	default:
1254*4882a593Smuzhiyun 		if (svc->state != GB_SVC_STATE_SVC_HELLO)
1255*4882a593Smuzhiyun 			ret = -EINVAL;
1256*4882a593Smuzhiyun 		break;
1257*4882a593Smuzhiyun 	}
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	if (ret) {
1260*4882a593Smuzhiyun 		dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
1261*4882a593Smuzhiyun 			 type, svc->state);
1262*4882a593Smuzhiyun 		return ret;
1263*4882a593Smuzhiyun 	}
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	switch (type) {
1266*4882a593Smuzhiyun 	case GB_SVC_TYPE_PROTOCOL_VERSION:
1267*4882a593Smuzhiyun 		ret = gb_svc_version_request(op);
1268*4882a593Smuzhiyun 		if (!ret)
1269*4882a593Smuzhiyun 			svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
1270*4882a593Smuzhiyun 		return ret;
1271*4882a593Smuzhiyun 	case GB_SVC_TYPE_SVC_HELLO:
1272*4882a593Smuzhiyun 		ret = gb_svc_hello(op);
1273*4882a593Smuzhiyun 		if (!ret)
1274*4882a593Smuzhiyun 			svc->state = GB_SVC_STATE_SVC_HELLO;
1275*4882a593Smuzhiyun 		return ret;
1276*4882a593Smuzhiyun 	case GB_SVC_TYPE_INTF_RESET:
1277*4882a593Smuzhiyun 		return gb_svc_intf_reset_recv(op);
1278*4882a593Smuzhiyun 	case GB_SVC_TYPE_MODULE_INSERTED:
1279*4882a593Smuzhiyun 		return gb_svc_module_inserted_recv(op);
1280*4882a593Smuzhiyun 	case GB_SVC_TYPE_MODULE_REMOVED:
1281*4882a593Smuzhiyun 		return gb_svc_module_removed_recv(op);
1282*4882a593Smuzhiyun 	case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
1283*4882a593Smuzhiyun 		return gb_svc_intf_mailbox_event_recv(op);
1284*4882a593Smuzhiyun 	case GB_SVC_TYPE_INTF_OOPS:
1285*4882a593Smuzhiyun 		return gb_svc_intf_oops_recv(op);
1286*4882a593Smuzhiyun 	default:
1287*4882a593Smuzhiyun 		dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
1288*4882a593Smuzhiyun 		return -EINVAL;
1289*4882a593Smuzhiyun 	}
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun 
gb_svc_release(struct device * dev)1292*4882a593Smuzhiyun static void gb_svc_release(struct device *dev)
1293*4882a593Smuzhiyun {
1294*4882a593Smuzhiyun 	struct gb_svc *svc = to_gb_svc(dev);
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun 	if (svc->connection)
1297*4882a593Smuzhiyun 		gb_connection_destroy(svc->connection);
1298*4882a593Smuzhiyun 	ida_destroy(&svc->device_id_map);
1299*4882a593Smuzhiyun 	destroy_workqueue(svc->wq);
1300*4882a593Smuzhiyun 	kfree(svc);
1301*4882a593Smuzhiyun }
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun struct device_type greybus_svc_type = {
1304*4882a593Smuzhiyun 	.name		= "greybus_svc",
1305*4882a593Smuzhiyun 	.release	= gb_svc_release,
1306*4882a593Smuzhiyun };
1307*4882a593Smuzhiyun 
gb_svc_create(struct gb_host_device * hd)1308*4882a593Smuzhiyun struct gb_svc *gb_svc_create(struct gb_host_device *hd)
1309*4882a593Smuzhiyun {
1310*4882a593Smuzhiyun 	struct gb_svc *svc;
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	svc = kzalloc(sizeof(*svc), GFP_KERNEL);
1313*4882a593Smuzhiyun 	if (!svc)
1314*4882a593Smuzhiyun 		return NULL;
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun 	svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
1317*4882a593Smuzhiyun 	if (!svc->wq) {
1318*4882a593Smuzhiyun 		kfree(svc);
1319*4882a593Smuzhiyun 		return NULL;
1320*4882a593Smuzhiyun 	}
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun 	svc->dev.parent = &hd->dev;
1323*4882a593Smuzhiyun 	svc->dev.bus = &greybus_bus_type;
1324*4882a593Smuzhiyun 	svc->dev.type = &greybus_svc_type;
1325*4882a593Smuzhiyun 	svc->dev.groups = svc_groups;
1326*4882a593Smuzhiyun 	svc->dev.dma_mask = svc->dev.parent->dma_mask;
1327*4882a593Smuzhiyun 	device_initialize(&svc->dev);
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	ida_init(&svc->device_id_map);
1332*4882a593Smuzhiyun 	svc->state = GB_SVC_STATE_RESET;
1333*4882a593Smuzhiyun 	svc->hd = hd;
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun 	svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
1336*4882a593Smuzhiyun 						      gb_svc_request_handler);
1337*4882a593Smuzhiyun 	if (IS_ERR(svc->connection)) {
1338*4882a593Smuzhiyun 		dev_err(&svc->dev, "failed to create connection: %ld\n",
1339*4882a593Smuzhiyun 			PTR_ERR(svc->connection));
1340*4882a593Smuzhiyun 		goto err_put_device;
1341*4882a593Smuzhiyun 	}
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	gb_connection_set_data(svc->connection, svc);
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	return svc;
1346*4882a593Smuzhiyun 
1347*4882a593Smuzhiyun err_put_device:
1348*4882a593Smuzhiyun 	put_device(&svc->dev);
1349*4882a593Smuzhiyun 	return NULL;
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun 
gb_svc_add(struct gb_svc * svc)1352*4882a593Smuzhiyun int gb_svc_add(struct gb_svc *svc)
1353*4882a593Smuzhiyun {
1354*4882a593Smuzhiyun 	int ret;
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	/*
1357*4882a593Smuzhiyun 	 * The SVC protocol is currently driven by the SVC, so the SVC device
1358*4882a593Smuzhiyun 	 * is added from the connection request handler when enough
1359*4882a593Smuzhiyun 	 * information has been received.
1360*4882a593Smuzhiyun 	 */
1361*4882a593Smuzhiyun 	ret = gb_connection_enable(svc->connection);
1362*4882a593Smuzhiyun 	if (ret)
1363*4882a593Smuzhiyun 		return ret;
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	return 0;
1366*4882a593Smuzhiyun }
1367*4882a593Smuzhiyun 
gb_svc_remove_modules(struct gb_svc * svc)1368*4882a593Smuzhiyun static void gb_svc_remove_modules(struct gb_svc *svc)
1369*4882a593Smuzhiyun {
1370*4882a593Smuzhiyun 	struct gb_host_device *hd = svc->hd;
1371*4882a593Smuzhiyun 	struct gb_module *module, *tmp;
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 	list_for_each_entry_safe(module, tmp, &hd->modules, hd_node) {
1374*4882a593Smuzhiyun 		gb_module_del(module);
1375*4882a593Smuzhiyun 		list_del(&module->hd_node);
1376*4882a593Smuzhiyun 		gb_module_put(module);
1377*4882a593Smuzhiyun 	}
1378*4882a593Smuzhiyun }
1379*4882a593Smuzhiyun 
gb_svc_del(struct gb_svc * svc)1380*4882a593Smuzhiyun void gb_svc_del(struct gb_svc *svc)
1381*4882a593Smuzhiyun {
1382*4882a593Smuzhiyun 	gb_connection_disable_rx(svc->connection);
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun 	/*
1385*4882a593Smuzhiyun 	 * The SVC device may have been registered from the request handler.
1386*4882a593Smuzhiyun 	 */
1387*4882a593Smuzhiyun 	if (device_is_registered(&svc->dev)) {
1388*4882a593Smuzhiyun 		gb_svc_debugfs_exit(svc);
1389*4882a593Smuzhiyun 		gb_svc_watchdog_destroy(svc);
1390*4882a593Smuzhiyun 		device_del(&svc->dev);
1391*4882a593Smuzhiyun 	}
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun 	flush_workqueue(svc->wq);
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun 	gb_svc_remove_modules(svc);
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 	gb_connection_disable(svc->connection);
1398*4882a593Smuzhiyun }
1399*4882a593Smuzhiyun 
gb_svc_put(struct gb_svc * svc)1400*4882a593Smuzhiyun void gb_svc_put(struct gb_svc *svc)
1401*4882a593Smuzhiyun {
1402*4882a593Smuzhiyun 	put_device(&svc->dev);
1403*4882a593Smuzhiyun }
1404