4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
7 * Released under the GPLv2 only.
12 #define CPORT_FLAGS_E2EFC (1)
13 #define CPORT_FLAGS_CSD_N (2)
14 #define CPORT_FLAGS_CSV_N (4)
17 struct gb_connection *connection;
22 static struct ida greybus_svc_device_id_map;
25 * AP's SVC cport is required early to get messages from the SVC. This happens
26 * even before the Endo is created and hence any modules or interfaces.
28 * This is a temporary connection, used only at initial bootup.
30 struct gb_connection *
31 gb_ap_svc_connection_create(struct greybus_host_device *hd)
33 struct gb_connection *connection;
35 connection = gb_connection_create_range(hd, NULL, hd->parent,
45 * We know endo-type and AP's interface id now, lets create a proper svc
46 * connection (and its interface/bundle) now and get rid of the initial
47 * 'partially' initialized one svc connection.
49 static struct gb_interface *
50 gb_ap_interface_create(struct greybus_host_device *hd,
51 struct gb_connection *connection, u8 interface_id)
53 struct gb_interface *intf;
54 struct device *dev = &hd->endo->dev;
56 intf = gb_interface_create(hd, interface_id);
58 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
59 __func__, interface_id);
63 intf->device_id = GB_DEVICE_ID_AP;
64 svc_update_connection(intf, connection);
66 /* Its no longer a partially initialized connection */
67 hd->initial_svc_connection = NULL;
72 static int intf_device_id_operation(struct gb_svc *svc,
73 u8 intf_id, u8 device_id)
75 struct gb_svc_intf_device_id_request request;
77 request.intf_id = intf_id;
78 request.device_id = device_id;
80 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
81 &request, sizeof(request), NULL, 0);
84 static int intf_reset_operation(struct gb_svc *svc, u8 intf_id)
86 struct gb_svc_intf_reset_request request;
88 request.intf_id = intf_id;
90 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
91 &request, sizeof(request), NULL, 0);
94 static int connection_create_operation(struct gb_svc *svc,
95 u8 intf1_id, u16 cport1_id,
96 u8 intf2_id, u16 cport2_id)
98 struct gb_svc_conn_create_request request;
100 request.intf1_id = intf1_id;
101 request.cport1_id = cport1_id;
102 request.intf2_id = intf2_id;
103 request.cport2_id = cport2_id;
105 * XXX: fix connections paramaters to TC0 and all CPort flags
109 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
111 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
112 &request, sizeof(request), NULL, 0);
115 static int connection_destroy_operation(struct gb_svc *svc,
116 u8 intf1_id, u16 cport1_id,
117 u8 intf2_id, u16 cport2_id)
119 struct gb_svc_conn_destroy_request request;
121 request.intf1_id = intf1_id;
122 request.cport1_id = cport1_id;
123 request.intf2_id = intf2_id;
124 request.cport2_id = cport2_id;
126 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_DESTROY,
127 &request, sizeof(request), NULL, 0);
130 static int route_create_operation(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
131 u8 intf2_id, u8 dev2_id)
133 struct gb_svc_route_create_request request;
135 request.intf1_id = intf1_id;
136 request.dev1_id = dev1_id;
137 request.intf2_id = intf2_id;
138 request.dev2_id = dev2_id;
140 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
141 &request, sizeof(request), NULL, 0);
144 int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
146 return intf_device_id_operation(svc, intf_id, device_id);
148 EXPORT_SYMBOL_GPL(gb_svc_intf_device_id);
150 int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
152 return intf_reset_operation(svc, intf_id);
154 EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
156 int gb_svc_connection_create(struct gb_svc *svc,
157 u8 intf1_id, u16 cport1_id,
158 u8 intf2_id, u16 cport2_id)
160 return connection_create_operation(svc, intf1_id, cport1_id,
161 intf2_id, cport2_id);
163 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
165 int gb_svc_connection_destroy(struct gb_svc *svc,
166 u8 intf1_id, u16 cport1_id,
167 u8 intf2_id, u16 cport2_id)
169 return connection_destroy_operation(svc, intf1_id, cport1_id,
170 intf2_id, cport2_id);
172 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
174 int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
175 u8 intf2_id, u8 dev2_id)
177 return route_create_operation(svc, intf1_id, dev1_id,
180 EXPORT_SYMBOL_GPL(gb_svc_route_create);
182 static int gb_svc_version_request(struct gb_operation *op)
184 struct gb_connection *connection = op->connection;
185 struct gb_protocol_version_response *version;
186 struct device *dev = &connection->dev;
188 version = op->request->payload;
190 if (version->major > GB_SVC_VERSION_MAJOR) {
191 dev_err(&connection->dev,
192 "unsupported major version (%hhu > %hhu)\n",
193 version->major, GB_SVC_VERSION_MAJOR);
197 if (!gb_operation_response_alloc(op, sizeof(*version), GFP_KERNEL)) {
198 dev_err(dev, "%s: error allocating response\n",
203 version = op->response->payload;
204 version->major = GB_SVC_VERSION_MAJOR;
205 version->minor = GB_SVC_VERSION_MINOR;
209 static int gb_svc_hello(struct gb_operation *op)
211 struct gb_connection *connection = op->connection;
212 struct greybus_host_device *hd = connection->hd;
213 struct gb_svc_hello_request *hello_request;
214 struct device *dev = &connection->dev;
215 struct gb_interface *intf;
220 /* Hello message should be received only during early bootup */
221 WARN_ON(hd->initial_svc_connection != connection);
224 * SVC sends information about the endo and interface-id on the hello
225 * request, use that to create an endo.
227 if (op->request->payload_size != sizeof(*hello_request)) {
228 dev_err(dev, "%s: Illegal size of hello request (%zu %zu)\n",
229 __func__, op->request->payload_size,
230 sizeof(*hello_request));
234 hello_request = op->request->payload;
235 endo_id = le16_to_cpu(hello_request->endo_id);
236 interface_id = hello_request->interface_id;
239 ret = greybus_endo_setup(hd, endo_id, interface_id);
244 * Endo and its modules are ready now, fix AP's partially initialized
245 * svc protocol and its connection.
247 intf = gb_ap_interface_create(hd, connection, interface_id);
249 gb_endo_remove(hd->endo);
256 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
258 struct gb_message *request = op->request;
259 struct gb_svc_intf_hotplug_request *hotplug = request->payload;
260 struct gb_svc *svc = op->connection->private;
261 struct greybus_host_device *hd = op->connection->bundle->intf->hd;
262 struct device *dev = &op->connection->dev;
263 struct gb_interface *intf;
264 u8 intf_id, device_id;
271 if (request->payload_size < sizeof(*hotplug)) {
272 dev_err(dev, "%s: short hotplug request received\n", __func__);
277 * Grab the information we need.
279 * XXX I'd really like to acknowledge receipt, and then
280 * XXX continue processing the request. There's no need
281 * XXX for the SVC to wait. In fact, it might be best to
282 * XXX have the SVC get acknowledgement before we proceed.
284 intf_id = hotplug->intf_id;
285 unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
286 unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
287 ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id);
288 ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id);
290 // FIXME May require firmware download
291 intf = gb_interface_create(hd, intf_id);
293 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
299 * Create a device id for the interface:
300 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
301 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
303 * XXX Do we need to allocate device ID for SVC or the AP here? And what
304 * XXX about an AP with multiple interface blocks?
306 device_id = ida_simple_get(&greybus_svc_device_id_map,
307 GB_DEVICE_ID_MODULES_START, 0, GFP_ATOMIC);
310 dev_err(dev, "%s: Failed to allocate device id for interface with id %hhu (%d)\n",
311 __func__, intf_id, ret);
312 goto destroy_interface;
315 ret = intf_device_id_operation(svc, intf_id, device_id);
317 dev_err(dev, "%s: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
318 __func__, intf_id, device_id, ret);
323 * Create a two-way route between the AP and the new interface
325 ret = route_create_operation(svc, hd->endo->ap_intf_id,
326 GB_DEVICE_ID_AP, intf_id, device_id);
328 dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
329 __func__, intf_id, device_id, ret);
333 ret = route_create_operation(svc, intf_id, device_id,
334 hd->endo->ap_intf_id, GB_DEVICE_ID_AP);
336 dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
337 __func__, intf_id, device_id, ret);
341 ret = gb_interface_init(intf, device_id);
343 dev_err(dev, "%s: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
344 __func__, intf_id, device_id, ret);
352 * XXX Should we tell SVC that this id doesn't belong to interface
356 ida_simple_remove(&greybus_svc_device_id_map, device_id);
358 gb_interface_remove(hd, intf_id);
363 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
365 struct gb_message *request = op->request;
366 struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
367 struct greybus_host_device *hd = op->connection->bundle->intf->hd;
368 struct device *dev = &op->connection->dev;
370 struct gb_interface *intf;
373 if (request->payload_size < sizeof(*hot_unplug)) {
374 dev_err(&op->connection->dev,
375 "short hot unplug request received\n");
379 intf_id = hot_unplug->intf_id;
381 intf = gb_interface_find(hd, intf_id);
383 dev_err(dev, "%s: Couldn't find interface for id %hhu\n",
388 device_id = intf->device_id;
389 gb_interface_remove(hd, intf_id);
390 ida_simple_remove(&greybus_svc_device_id_map, device_id);
395 static int gb_svc_intf_reset_recv(struct gb_operation *op)
397 struct gb_message *request = op->request;
398 struct gb_svc_intf_reset_request *reset;
401 if (request->payload_size < sizeof(*reset)) {
402 dev_err(&op->connection->dev,
403 "short reset request received\n");
406 reset = request->payload;
408 intf_id = reset->intf_id;
410 /* FIXME Reset the interface here */
415 static int gb_svc_request_recv(u8 type, struct gb_operation *op)
418 case GB_SVC_TYPE_PROTOCOL_VERSION:
419 return gb_svc_version_request(op);
420 case GB_SVC_TYPE_SVC_HELLO:
421 return gb_svc_hello(op);
422 case GB_SVC_TYPE_INTF_HOTPLUG:
423 return gb_svc_intf_hotplug_recv(op);
424 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
425 return gb_svc_intf_hot_unplug_recv(op);
426 case GB_SVC_TYPE_INTF_RESET:
427 return gb_svc_intf_reset_recv(op);
429 dev_err(&op->connection->dev,
430 "unsupported request: %hhu\n", type);
435 static int gb_svc_connection_init(struct gb_connection *connection)
439 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
443 connection->hd->svc = svc;
444 svc->connection = connection;
445 connection->private = svc;
447 WARN_ON(connection->hd->initial_svc_connection);
448 connection->hd->initial_svc_connection = connection;
450 ida_init(&greybus_svc_device_id_map);
455 static void gb_svc_connection_exit(struct gb_connection *connection)
457 struct gb_svc *svc = connection->private;
459 connection->hd->svc = NULL;
460 connection->private = NULL;
464 static struct gb_protocol svc_protocol = {
466 .id = GREYBUS_PROTOCOL_SVC,
467 .major = GB_SVC_VERSION_MAJOR,
468 .minor = GB_SVC_VERSION_MINOR,
469 .connection_init = gb_svc_connection_init,
470 .connection_exit = gb_svc_connection_exit,
471 .request_recv = gb_svc_request_recv,
473 gb_builtin_protocol_driver(svc_protocol);