]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/svc.c
greybus: svc: No need of per-direction route-create requests
[karo-tx-linux.git] / drivers / staging / greybus / svc.c
1 /*
2  * SVC Greybus driver.
3  *
4  * Copyright 2015 Google Inc.
5  * Copyright 2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include <linux/workqueue.h>
11
12 #include "greybus.h"
13
14 #define CPORT_FLAGS_E2EFC       (1)
15 #define CPORT_FLAGS_CSD_N       (2)
16 #define CPORT_FLAGS_CSV_N       (4)
17
18 enum gb_svc_state {
19         GB_SVC_STATE_RESET,
20         GB_SVC_STATE_PROTOCOL_VERSION,
21         GB_SVC_STATE_SVC_HELLO,
22 };
23
24 struct gb_svc {
25         struct gb_connection    *connection;
26         enum gb_svc_state       state;
27 };
28
29 struct svc_hotplug {
30         struct work_struct work;
31         struct gb_connection *connection;
32         struct gb_svc_intf_hotplug_request data;
33 };
34
35 static struct ida greybus_svc_device_id_map;
36
37 /*
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.
40  *
41  * This is a temporary connection, used only at initial bootup.
42  */
43 struct gb_connection *
44 gb_ap_svc_connection_create(struct greybus_host_device *hd)
45 {
46         struct gb_connection *connection;
47
48         connection = gb_connection_create_range(hd, NULL, hd->parent,
49                                                 GB_SVC_CPORT_ID,
50                                                 GREYBUS_PROTOCOL_SVC,
51                                                 GB_SVC_CPORT_ID,
52                                                 GB_SVC_CPORT_ID + 1);
53
54         return connection;
55 }
56
57 /*
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.
61  */
62 static struct gb_interface *
63 gb_ap_interface_create(struct greybus_host_device *hd,
64                        struct gb_connection *connection, u8 interface_id)
65 {
66         struct gb_interface *intf;
67         struct device *dev = &hd->endo->dev;
68
69         intf = gb_interface_create(hd, interface_id);
70         if (!intf) {
71                 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
72                         __func__, interface_id);
73                 return NULL;
74         }
75
76         intf->device_id = GB_DEVICE_ID_AP;
77         svc_update_connection(intf, connection);
78
79         /* Its no longer a partially initialized connection */
80         hd->initial_svc_connection = NULL;
81
82         return intf;
83 }
84
85 static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
86 {
87         struct gb_svc_intf_device_id_request request;
88
89         request.intf_id = intf_id;
90         request.device_id = device_id;
91
92         return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
93                                  &request, sizeof(request), NULL, 0);
94 }
95
96 int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
97 {
98         struct gb_svc_intf_reset_request request;
99
100         request.intf_id = intf_id;
101
102         return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
103                                  &request, sizeof(request), NULL, 0);
104 }
105 EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
106
107 int gb_svc_connection_create(struct gb_svc *svc,
108                                 u8 intf1_id, u16 cport1_id,
109                                 u8 intf2_id, u16 cport2_id)
110 {
111         struct gb_svc_conn_create_request request;
112
113         request.intf1_id = intf1_id;
114         request.cport1_id = cport1_id;
115         request.intf2_id = intf2_id;
116         request.cport2_id = cport2_id;
117         /*
118          * XXX: fix connections paramaters to TC0 and all CPort flags
119          * for now.
120          */
121         request.tc = 0;
122         request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
123
124         return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
125                                  &request, sizeof(request), NULL, 0);
126 }
127 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
128
129 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
130                                u8 intf2_id, u16 cport2_id)
131 {
132         struct gb_svc_conn_destroy_request request;
133         struct gb_connection *connection = svc->connection;
134         int ret;
135
136         request.intf1_id = intf1_id;
137         request.cport1_id = cport1_id;
138         request.intf2_id = intf2_id;
139         request.cport2_id = cport2_id;
140
141         ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
142                                 &request, sizeof(request), NULL, 0);
143         if (ret) {
144                 dev_err(&connection->dev,
145                         "failed to destroy connection (%hhx:%hx %hhx:%hx) %d\n",
146                         intf1_id, cport1_id, intf2_id, cport2_id, ret);
147         }
148 }
149 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
150
151 /* Creates bi-directional routes between the devices */
152 static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
153                                u8 intf2_id, u8 dev2_id)
154 {
155         struct gb_svc_route_create_request request;
156
157         request.intf1_id = intf1_id;
158         request.dev1_id = dev1_id;
159         request.intf2_id = intf2_id;
160         request.dev2_id = dev2_id;
161
162         return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
163                                  &request, sizeof(request), NULL, 0);
164 }
165
166 static int gb_svc_version_request(struct gb_operation *op)
167 {
168         struct gb_connection *connection = op->connection;
169         struct gb_protocol_version_response *version;
170         struct device *dev = &connection->dev;
171
172         version = op->request->payload;
173
174         if (version->major > GB_SVC_VERSION_MAJOR) {
175                 dev_err(&connection->dev,
176                         "unsupported major version (%hhu > %hhu)\n",
177                         version->major, GB_SVC_VERSION_MAJOR);
178                 return -ENOTSUPP;
179         }
180
181         connection->module_major = version->major;
182         connection->module_minor = version->minor;
183
184         if (!gb_operation_response_alloc(op, sizeof(*version), GFP_KERNEL)) {
185                 dev_err(dev, "%s: error allocating response\n",
186                                 __func__);
187                 return -ENOMEM;
188         }
189
190         version = op->response->payload;
191         version->major = GB_SVC_VERSION_MAJOR;
192         version->minor = GB_SVC_VERSION_MINOR;
193         return 0;
194 }
195
196 static int gb_svc_hello(struct gb_operation *op)
197 {
198         struct gb_connection *connection = op->connection;
199         struct greybus_host_device *hd = connection->hd;
200         struct gb_svc_hello_request *hello_request;
201         struct device *dev = &connection->dev;
202         struct gb_interface *intf;
203         u16 endo_id;
204         u8 interface_id;
205         int ret;
206
207         /*
208          * SVC sends information about the endo and interface-id on the hello
209          * request, use that to create an endo.
210          */
211         if (op->request->payload_size < sizeof(*hello_request)) {
212                 dev_err(dev, "%s: Illegal size of hello request (%zu < %zu)\n",
213                         __func__, op->request->payload_size,
214                         sizeof(*hello_request));
215                 return -EINVAL;
216         }
217
218         hello_request = op->request->payload;
219         endo_id = le16_to_cpu(hello_request->endo_id);
220         interface_id = hello_request->interface_id;
221
222         /* Setup Endo */
223         ret = greybus_endo_setup(hd, endo_id, interface_id);
224         if (ret)
225                 return ret;
226
227         /*
228          * Endo and its modules are ready now, fix AP's partially initialized
229          * svc protocol and its connection.
230          */
231         intf = gb_ap_interface_create(hd, connection, interface_id);
232         if (!intf) {
233                 gb_endo_remove(hd->endo);
234                 return ret;
235         }
236
237         return 0;
238 }
239
240 /*
241  * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it
242  * returns, irrespective of success or Failure in bringing up the module.
243  */
244 static void svc_process_hotplug(struct work_struct *work)
245 {
246         struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug,
247                                                        work);
248         struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data;
249         struct gb_connection *connection = svc_hotplug->connection;
250         struct gb_svc *svc = connection->private;
251         struct greybus_host_device *hd = connection->hd;
252         struct device *dev = &connection->dev;
253         struct gb_interface *intf;
254         u8 intf_id, device_id;
255         int ret;
256
257         /*
258          * Grab the information we need.
259          */
260         intf_id = hotplug->intf_id;
261
262         intf = gb_interface_create(hd, intf_id);
263         if (!intf) {
264                 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
265                         __func__, intf_id);
266                 goto free_svc_hotplug;
267         }
268
269         intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
270         intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
271         intf->ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id);
272         intf->ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id);
273
274         /*
275          * Create a device id for the interface:
276          * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
277          * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
278          *
279          * XXX Do we need to allocate device ID for SVC or the AP here? And what
280          * XXX about an AP with multiple interface blocks?
281          */
282         device_id = ida_simple_get(&greybus_svc_device_id_map,
283                                    GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
284         if (device_id < 0) {
285                 ret = device_id;
286                 dev_err(dev, "%s: Failed to allocate device id for interface with id %hhu (%d)\n",
287                         __func__, intf_id, ret);
288                 goto destroy_interface;
289         }
290
291         ret = gb_svc_intf_device_id(svc, intf_id, device_id);
292         if (ret) {
293                 dev_err(dev, "%s: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
294                         __func__, intf_id, device_id, ret);
295                 goto ida_put;
296         }
297
298         /*
299          * Create a two-way route between the AP and the new interface
300          */
301         ret = gb_svc_route_create(svc, hd->endo->ap_intf_id, GB_DEVICE_ID_AP,
302                                   intf_id, device_id);
303         if (ret) {
304                 dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
305                         __func__, intf_id, device_id, ret);
306                 goto ida_put;
307         }
308
309         ret = gb_interface_init(intf, device_id);
310         if (ret) {
311                 dev_err(dev, "%s: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
312                         __func__, intf_id, device_id, ret);
313                 goto svc_id_free;
314         }
315
316         goto free_svc_hotplug;
317
318 svc_id_free:
319         /*
320          * XXX Should we tell SVC that this id doesn't belong to interface
321          * XXX anymore.
322          */
323 ida_put:
324         ida_simple_remove(&greybus_svc_device_id_map, device_id);
325 destroy_interface:
326         gb_interface_remove(hd, intf_id);
327 free_svc_hotplug:
328         kfree(svc_hotplug);
329 }
330
331 /*
332  * Bringing up a module can be time consuming, as that may require lots of
333  * initialization on the module side. Over that, we may also need to download
334  * the firmware first and flash that on the module.
335  *
336  * In order to make other hotplug events to not wait for all this to finish,
337  * handle most of module hotplug stuff outside of the hotplug callback, with
338  * help of a workqueue.
339  */
340 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
341 {
342         struct gb_message *request = op->request;
343         struct svc_hotplug *svc_hotplug;
344
345         if (request->payload_size < sizeof(svc_hotplug->data)) {
346                 dev_err(&op->connection->dev,
347                         "%s: short hotplug request received (%zu < %zu)\n",
348                         __func__, request->payload_size,
349                         sizeof(svc_hotplug->data));
350                 return -EINVAL;
351         }
352
353         svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL);
354         if (!svc_hotplug)
355                 return -ENOMEM;
356
357         svc_hotplug->connection = op->connection;
358         memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data));
359
360         INIT_WORK(&svc_hotplug->work, svc_process_hotplug);
361         queue_work(system_unbound_wq, &svc_hotplug->work);
362
363         return 0;
364 }
365
366 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
367 {
368         struct gb_message *request = op->request;
369         struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
370         struct greybus_host_device *hd = op->connection->hd;
371         struct device *dev = &op->connection->dev;
372         u8 device_id;
373         struct gb_interface *intf;
374         u8 intf_id;
375
376         if (request->payload_size < sizeof(*hot_unplug)) {
377                 dev_err(&op->connection->dev,
378                         "short hot unplug request received (%zu < %zu)\n",
379                         request->payload_size, sizeof(*hot_unplug));
380                 return -EINVAL;
381         }
382
383         intf_id = hot_unplug->intf_id;
384
385         intf = gb_interface_find(hd, intf_id);
386         if (!intf) {
387                 dev_err(dev, "%s: Couldn't find interface for id %hhu\n",
388                         __func__, intf_id);
389                 return -EINVAL;
390         }
391
392         device_id = intf->device_id;
393         gb_interface_remove(hd, intf_id);
394         ida_simple_remove(&greybus_svc_device_id_map, device_id);
395
396         return 0;
397 }
398
399 static int gb_svc_intf_reset_recv(struct gb_operation *op)
400 {
401         struct gb_message *request = op->request;
402         struct gb_svc_intf_reset_request *reset;
403         u8 intf_id;
404
405         if (request->payload_size < sizeof(*reset)) {
406                 dev_err(&op->connection->dev,
407                         "short reset request received (%zu < %zu)\n",
408                         request->payload_size, sizeof(*reset));
409                 return -EINVAL;
410         }
411         reset = request->payload;
412
413         intf_id = reset->intf_id;
414
415         /* FIXME Reset the interface here */
416
417         return 0;
418 }
419
420 static int gb_svc_request_recv(u8 type, struct gb_operation *op)
421 {
422         struct gb_connection *connection = op->connection;
423         struct gb_svc *svc = connection->private;
424         int ret = 0;
425
426         /*
427          * SVC requests need to follow a specific order (at least initially) and
428          * below code takes care of enforcing that. The expected order is:
429          * - PROTOCOL_VERSION
430          * - SVC_HELLO
431          * - Any other request, but the earlier two.
432          *
433          * Incoming requests are guaranteed to be serialized and so we don't
434          * need to protect 'state' for any races.
435          */
436         switch (type) {
437         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
438                 if (svc->state != GB_SVC_STATE_RESET)
439                         ret = -EINVAL;
440                 break;
441         case GB_SVC_TYPE_SVC_HELLO:
442                 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
443                         ret = -EINVAL;
444                 break;
445         default:
446                 if (svc->state != GB_SVC_STATE_SVC_HELLO)
447                         ret = -EINVAL;
448                 break;
449         }
450
451         if (ret) {
452                 dev_warn(&connection->dev,
453                          "unexpected SVC request 0x%02x received (state %u)\n",
454                          type, svc->state);
455                 return ret;
456         }
457
458         switch (type) {
459         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
460                 ret = gb_svc_version_request(op);
461                 if (!ret)
462                         svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
463                 return ret;
464         case GB_SVC_TYPE_SVC_HELLO:
465                 ret = gb_svc_hello(op);
466                 if (!ret)
467                         svc->state = GB_SVC_STATE_SVC_HELLO;
468                 return ret;
469         case GB_SVC_TYPE_INTF_HOTPLUG:
470                 return gb_svc_intf_hotplug_recv(op);
471         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
472                 return gb_svc_intf_hot_unplug_recv(op);
473         case GB_SVC_TYPE_INTF_RESET:
474                 return gb_svc_intf_reset_recv(op);
475         default:
476                 dev_err(&op->connection->dev,
477                         "unsupported request: %hhu\n", type);
478                 return -EINVAL;
479         }
480 }
481
482 static int gb_svc_connection_init(struct gb_connection *connection)
483 {
484         struct gb_svc *svc;
485
486         svc = kzalloc(sizeof(*svc), GFP_KERNEL);
487         if (!svc)
488                 return -ENOMEM;
489
490         connection->hd->svc = svc;
491         svc->state = GB_SVC_STATE_RESET;
492         svc->connection = connection;
493         connection->private = svc;
494
495         WARN_ON(connection->hd->initial_svc_connection);
496         connection->hd->initial_svc_connection = connection;
497
498         ida_init(&greybus_svc_device_id_map);
499
500         return 0;
501 }
502
503 static void gb_svc_connection_exit(struct gb_connection *connection)
504 {
505         struct gb_svc *svc = connection->private;
506
507         connection->hd->svc = NULL;
508         connection->private = NULL;
509         kfree(svc);
510 }
511
512 static struct gb_protocol svc_protocol = {
513         .name                   = "svc",
514         .id                     = GREYBUS_PROTOCOL_SVC,
515         .major                  = GB_SVC_VERSION_MAJOR,
516         .minor                  = GB_SVC_VERSION_MINOR,
517         .connection_init        = gb_svc_connection_init,
518         .connection_exit        = gb_svc_connection_exit,
519         .request_recv           = gb_svc_request_recv,
520         .flags                  = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
521                                   GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
522                                   GB_PROTOCOL_NO_BUNDLE |
523                                   GB_PROTOCOL_SKIP_VERSION |
524                                   GB_PROTOCOL_SKIP_SVC_CONNECTION,
525 };
526 gb_builtin_protocol_driver(svc_protocol);