4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/workqueue.h>
14 #define CPORT_FLAGS_E2EFC (1)
15 #define CPORT_FLAGS_CSD_N (2)
16 #define CPORT_FLAGS_CSV_N (4)
20 GB_SVC_STATE_PROTOCOL_VERSION,
21 GB_SVC_STATE_SVC_HELLO,
25 struct gb_connection *connection;
26 enum gb_svc_state state;
27 struct ida device_id_map;
31 struct work_struct work;
32 struct gb_connection *connection;
33 struct gb_svc_intf_hotplug_request data;
38 * AP's SVC cport is required early to get messages from the SVC. This happens
39 * even before the Endo is created and hence any modules or interfaces.
41 * This is a temporary connection, used only at initial bootup.
43 struct gb_connection *
44 gb_ap_svc_connection_create(struct greybus_host_device *hd)
46 struct gb_connection *connection;
48 connection = gb_connection_create_range(hd, NULL, hd->parent,
58 * We know endo-type and AP's interface id now, lets create a proper svc
59 * connection (and its interface/bundle) now and get rid of the initial
60 * 'partially' initialized one svc connection.
62 static struct gb_interface *
63 gb_ap_interface_create(struct greybus_host_device *hd,
64 struct gb_connection *connection, u8 interface_id)
66 struct gb_interface *intf;
67 struct device *dev = &hd->endo->dev;
69 intf = gb_interface_create(hd, interface_id);
71 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
72 __func__, interface_id);
76 intf->device_id = GB_DEVICE_ID_AP;
77 svc_update_connection(intf, connection);
79 /* Its no longer a partially initialized connection */
80 hd->initial_svc_connection = NULL;
85 static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
87 struct gb_svc_intf_device_id_request request;
89 request.intf_id = intf_id;
90 request.device_id = device_id;
92 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
93 &request, sizeof(request), NULL, 0);
96 int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
98 struct gb_svc_intf_reset_request request;
100 request.intf_id = intf_id;
102 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
103 &request, sizeof(request), NULL, 0);
105 EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
107 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
110 struct gb_svc_dme_peer_get_request request;
111 struct gb_svc_dme_peer_get_response response;
115 request.intf_id = intf_id;
116 request.attr = cpu_to_le16(attr);
117 request.selector = cpu_to_le16(selector);
119 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
120 &request, sizeof(request),
121 &response, sizeof(response));
123 dev_err(&svc->connection->dev,
124 "failed to get DME attribute (%hhu %hx %hu) %d\n",
125 intf_id, attr, selector, ret);
129 result = le16_to_cpu(response.result_code);
131 dev_err(&svc->connection->dev,
132 "Unipro error %hu while getting DME attribute (%hhu %hx %hu)\n",
133 result, intf_id, attr, selector);
138 *value = le32_to_cpu(response.attr_value);
142 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
144 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
147 struct gb_svc_dme_peer_set_request request;
148 struct gb_svc_dme_peer_set_response response;
152 request.intf_id = intf_id;
153 request.attr = cpu_to_le16(attr);
154 request.selector = cpu_to_le16(selector);
155 request.value = cpu_to_le32(value);
157 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
158 &request, sizeof(request),
159 &response, sizeof(response));
161 dev_err(&svc->connection->dev,
162 "failed to set DME attribute (%hhu %hx %hu %u) %d\n",
163 intf_id, attr, selector, value, ret);
167 result = le16_to_cpu(response.result_code);
169 dev_err(&svc->connection->dev,
170 "Unipro error %hu while setting DME attribute (%hhu %hx %hu %u)\n",
171 result, intf_id, attr, selector, value);
177 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
179 int gb_svc_connection_create(struct gb_svc *svc,
180 u8 intf1_id, u16 cport1_id,
181 u8 intf2_id, u16 cport2_id)
183 struct gb_svc_conn_create_request request;
185 request.intf1_id = intf1_id;
186 request.cport1_id = cport1_id;
187 request.intf2_id = intf2_id;
188 request.cport2_id = cport2_id;
190 * XXX: fix connections paramaters to TC0 and all CPort flags
194 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
196 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
197 &request, sizeof(request), NULL, 0);
199 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
201 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
202 u8 intf2_id, u16 cport2_id)
204 struct gb_svc_conn_destroy_request request;
205 struct gb_connection *connection = svc->connection;
208 request.intf1_id = intf1_id;
209 request.cport1_id = cport1_id;
210 request.intf2_id = intf2_id;
211 request.cport2_id = cport2_id;
213 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
214 &request, sizeof(request), NULL, 0);
216 dev_err(&connection->dev,
217 "failed to destroy connection (%hhx:%hx %hhx:%hx) %d\n",
218 intf1_id, cport1_id, intf2_id, cport2_id, ret);
221 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
223 /* Creates bi-directional routes between the devices */
224 static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
225 u8 intf2_id, u8 dev2_id)
227 struct gb_svc_route_create_request request;
229 request.intf1_id = intf1_id;
230 request.dev1_id = dev1_id;
231 request.intf2_id = intf2_id;
232 request.dev2_id = dev2_id;
234 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
235 &request, sizeof(request), NULL, 0);
238 /* Destroys bi-directional routes between the devices */
239 static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
241 struct gb_svc_route_destroy_request request;
244 request.intf1_id = intf1_id;
245 request.intf2_id = intf2_id;
247 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
248 &request, sizeof(request), NULL, 0);
250 dev_err(&svc->connection->dev,
251 "failed to destroy route (%hhx %hhx) %d\n",
252 intf1_id, intf2_id, ret);
256 static int gb_svc_version_request(struct gb_operation *op)
258 struct gb_connection *connection = op->connection;
259 struct gb_protocol_version_response *version;
260 struct device *dev = &connection->dev;
262 version = op->request->payload;
264 if (version->major > GB_SVC_VERSION_MAJOR) {
265 dev_err(&connection->dev,
266 "unsupported major version (%hhu > %hhu)\n",
267 version->major, GB_SVC_VERSION_MAJOR);
271 connection->module_major = version->major;
272 connection->module_minor = version->minor;
274 if (!gb_operation_response_alloc(op, sizeof(*version), GFP_KERNEL)) {
275 dev_err(dev, "%s: error allocating response\n",
280 version = op->response->payload;
281 version->major = connection->module_major;
282 version->minor = connection->module_minor;
287 static int gb_svc_hello(struct gb_operation *op)
289 struct gb_connection *connection = op->connection;
290 struct greybus_host_device *hd = connection->hd;
291 struct gb_svc_hello_request *hello_request;
292 struct device *dev = &connection->dev;
293 struct gb_interface *intf;
299 * SVC sends information about the endo and interface-id on the hello
300 * request, use that to create an endo.
302 if (op->request->payload_size < sizeof(*hello_request)) {
303 dev_err(dev, "%s: Illegal size of hello request (%zu < %zu)\n",
304 __func__, op->request->payload_size,
305 sizeof(*hello_request));
309 hello_request = op->request->payload;
310 endo_id = le16_to_cpu(hello_request->endo_id);
311 interface_id = hello_request->interface_id;
314 ret = greybus_endo_setup(hd, endo_id, interface_id);
319 * Endo and its modules are ready now, fix AP's partially initialized
320 * svc protocol and its connection.
322 intf = gb_ap_interface_create(hd, connection, interface_id);
324 gb_endo_remove(hd->endo);
332 * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it
333 * returns, irrespective of success or Failure in bringing up the module.
335 static void svc_process_hotplug(struct work_struct *work)
337 struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug,
339 struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data;
340 struct gb_connection *connection = svc_hotplug->connection;
341 struct gb_svc *svc = connection->private;
342 struct greybus_host_device *hd = connection->hd;
343 struct device *dev = &connection->dev;
344 struct gb_interface *intf;
345 u8 intf_id, device_id;
349 * Grab the information we need.
351 intf_id = hotplug->intf_id;
353 intf = gb_interface_create(hd, intf_id);
355 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
357 goto free_svc_hotplug;
360 intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
361 intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
362 intf->ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id);
363 intf->ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id);
366 * Create a device id for the interface:
367 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
368 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
370 * XXX Do we need to allocate device ID for SVC or the AP here? And what
371 * XXX about an AP with multiple interface blocks?
373 device_id = ida_simple_get(&svc->device_id_map,
374 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
377 dev_err(dev, "%s: Failed to allocate device id for interface with id %hhu (%d)\n",
378 __func__, intf_id, ret);
379 goto destroy_interface;
382 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
384 dev_err(dev, "%s: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
385 __func__, intf_id, device_id, ret);
390 * Create a two-way route between the AP and the new interface
392 ret = gb_svc_route_create(svc, hd->endo->ap_intf_id, GB_DEVICE_ID_AP,
395 dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
396 __func__, intf_id, device_id, ret);
400 ret = gb_interface_init(intf, device_id);
402 dev_err(dev, "%s: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
403 __func__, intf_id, device_id, ret);
407 goto free_svc_hotplug;
410 gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id);
413 * XXX Should we tell SVC that this id doesn't belong to interface
417 ida_simple_remove(&svc->device_id_map, device_id);
419 gb_interface_remove(hd, intf_id);
425 * Bringing up a module can be time consuming, as that may require lots of
426 * initialization on the module side. Over that, we may also need to download
427 * the firmware first and flash that on the module.
429 * In order to make other hotplug events to not wait for all this to finish,
430 * handle most of module hotplug stuff outside of the hotplug callback, with
431 * help of a workqueue.
433 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
435 struct gb_message *request = op->request;
436 struct svc_hotplug *svc_hotplug;
438 if (request->payload_size < sizeof(svc_hotplug->data)) {
439 dev_err(&op->connection->dev,
440 "%s: short hotplug request received (%zu < %zu)\n",
441 __func__, request->payload_size,
442 sizeof(svc_hotplug->data));
446 svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL);
450 svc_hotplug->connection = op->connection;
451 memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data));
453 INIT_WORK(&svc_hotplug->work, svc_process_hotplug);
454 queue_work(system_unbound_wq, &svc_hotplug->work);
459 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
461 struct gb_message *request = op->request;
462 struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
463 struct greybus_host_device *hd = op->connection->hd;
464 struct device *dev = &op->connection->dev;
465 struct gb_svc *svc = op->connection->private;
467 struct gb_interface *intf;
470 if (request->payload_size < sizeof(*hot_unplug)) {
471 dev_err(&op->connection->dev,
472 "short hot unplug request received (%zu < %zu)\n",
473 request->payload_size, sizeof(*hot_unplug));
477 intf_id = hot_unplug->intf_id;
479 intf = gb_interface_find(hd, intf_id);
481 dev_err(dev, "%s: Couldn't find interface for id %hhu\n",
486 device_id = intf->device_id;
487 gb_interface_remove(hd, intf_id);
490 * Destroy the two-way route between the AP and the interface.
492 gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id);
494 ida_simple_remove(&svc->device_id_map, device_id);
499 static int gb_svc_intf_reset_recv(struct gb_operation *op)
501 struct gb_message *request = op->request;
502 struct gb_svc_intf_reset_request *reset;
505 if (request->payload_size < sizeof(*reset)) {
506 dev_err(&op->connection->dev,
507 "short reset request received (%zu < %zu)\n",
508 request->payload_size, sizeof(*reset));
511 reset = request->payload;
513 intf_id = reset->intf_id;
515 /* FIXME Reset the interface here */
520 static int gb_svc_request_recv(u8 type, struct gb_operation *op)
522 struct gb_connection *connection = op->connection;
523 struct gb_svc *svc = connection->private;
527 * SVC requests need to follow a specific order (at least initially) and
528 * below code takes care of enforcing that. The expected order is:
531 * - Any other request, but the earlier two.
533 * Incoming requests are guaranteed to be serialized and so we don't
534 * need to protect 'state' for any races.
537 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
538 if (svc->state != GB_SVC_STATE_RESET)
541 case GB_SVC_TYPE_SVC_HELLO:
542 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
546 if (svc->state != GB_SVC_STATE_SVC_HELLO)
552 dev_warn(&connection->dev,
553 "unexpected SVC request 0x%02x received (state %u)\n",
559 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
560 ret = gb_svc_version_request(op);
562 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
564 case GB_SVC_TYPE_SVC_HELLO:
565 ret = gb_svc_hello(op);
567 svc->state = GB_SVC_STATE_SVC_HELLO;
569 case GB_SVC_TYPE_INTF_HOTPLUG:
570 return gb_svc_intf_hotplug_recv(op);
571 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
572 return gb_svc_intf_hot_unplug_recv(op);
573 case GB_SVC_TYPE_INTF_RESET:
574 return gb_svc_intf_reset_recv(op);
576 dev_err(&op->connection->dev,
577 "unsupported request: %hhu\n", type);
582 static int gb_svc_connection_init(struct gb_connection *connection)
586 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
590 connection->hd->svc = svc;
591 svc->state = GB_SVC_STATE_RESET;
592 svc->connection = connection;
593 connection->private = svc;
595 WARN_ON(connection->hd->initial_svc_connection);
596 connection->hd->initial_svc_connection = connection;
598 ida_init(&svc->device_id_map);
603 static void gb_svc_connection_exit(struct gb_connection *connection)
605 struct gb_svc *svc = connection->private;
607 ida_destroy(&svc->device_id_map);
608 connection->hd->svc = NULL;
609 connection->private = NULL;
613 static struct gb_protocol svc_protocol = {
615 .id = GREYBUS_PROTOCOL_SVC,
616 .major = GB_SVC_VERSION_MAJOR,
617 .minor = GB_SVC_VERSION_MINOR,
618 .connection_init = gb_svc_connection_init,
619 .connection_exit = gb_svc_connection_exit,
620 .request_recv = gb_svc_request_recv,
621 .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
622 GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
623 GB_PROTOCOL_NO_BUNDLE |
624 GB_PROTOCOL_SKIP_VERSION |
625 GB_PROTOCOL_SKIP_SVC_CONNECTION,
627 gb_builtin_protocol_driver(svc_protocol);