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