greybus: svc: Allow consecutive hotplug events for the same module
[cascardo/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         struct ida              device_id_map;
28 };
29
30 struct svc_hotplug {
31         struct work_struct work;
32         struct gb_connection *connection;
33         struct gb_svc_intf_hotplug_request data;
34 };
35
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_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
108                         u32 *value)
109 {
110         struct gb_svc_dme_peer_get_request request;
111         struct gb_svc_dme_peer_get_response response;
112         u16 result;
113         int ret;
114
115         request.intf_id = intf_id;
116         request.attr = cpu_to_le16(attr);
117         request.selector = cpu_to_le16(selector);
118
119         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
120                                 &request, sizeof(request),
121                                 &response, sizeof(response));
122         if (ret) {
123                 dev_err(&svc->connection->dev,
124                         "failed to get DME attribute (%hhu %hx %hu) %d\n",
125                         intf_id, attr, selector, ret);
126                 return ret;
127         }
128
129         result = le16_to_cpu(response.result_code);
130         if (result) {
131                 dev_err(&svc->connection->dev,
132                         "Unipro error %hu while getting DME attribute (%hhu %hx %hu)\n",
133                         result, intf_id, attr, selector);
134                 return -EINVAL;
135         }
136
137         if (value)
138                 *value = le32_to_cpu(response.attr_value);
139
140         return 0;
141 }
142 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
143
144 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
145                         u32 value)
146 {
147         struct gb_svc_dme_peer_set_request request;
148         struct gb_svc_dme_peer_set_response response;
149         u16 result;
150         int ret;
151
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);
156
157         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
158                                 &request, sizeof(request),
159                                 &response, sizeof(response));
160         if (ret) {
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);
164                 return ret;
165         }
166
167         result = le16_to_cpu(response.result_code);
168         if (result) {
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);
172                 return -EINVAL;
173         }
174
175         return 0;
176 }
177 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
178
179 int gb_svc_connection_create(struct gb_svc *svc,
180                                 u8 intf1_id, u16 cport1_id,
181                                 u8 intf2_id, u16 cport2_id)
182 {
183         struct gb_svc_conn_create_request request;
184
185         request.intf1_id = intf1_id;
186         request.cport1_id = cpu_to_le16(cport1_id);
187         request.intf2_id = intf2_id;
188         request.cport2_id = cpu_to_le16(cport2_id);
189         /*
190          * XXX: fix connections paramaters to TC0 and all CPort flags
191          * for now.
192          */
193         request.tc = 0;
194         request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
195
196         return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
197                                  &request, sizeof(request), NULL, 0);
198 }
199 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
200
201 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
202                                u8 intf2_id, u16 cport2_id)
203 {
204         struct gb_svc_conn_destroy_request request;
205         struct gb_connection *connection = svc->connection;
206         int ret;
207
208         request.intf1_id = intf1_id;
209         request.cport1_id = cpu_to_le16(cport1_id);
210         request.intf2_id = intf2_id;
211         request.cport2_id = cpu_to_le16(cport2_id);
212
213         ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
214                                 &request, sizeof(request), NULL, 0);
215         if (ret) {
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);
219         }
220 }
221 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
222
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)
226 {
227         struct gb_svc_route_create_request request;
228
229         request.intf1_id = intf1_id;
230         request.dev1_id = dev1_id;
231         request.intf2_id = intf2_id;
232         request.dev2_id = dev2_id;
233
234         return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
235                                  &request, sizeof(request), NULL, 0);
236 }
237
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)
240 {
241         struct gb_svc_route_destroy_request request;
242         int ret;
243
244         request.intf1_id = intf1_id;
245         request.intf2_id = intf2_id;
246
247         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
248                                 &request, sizeof(request), NULL, 0);
249         if (ret) {
250                 dev_err(&svc->connection->dev,
251                         "failed to destroy route (%hhx %hhx) %d\n",
252                         intf1_id, intf2_id, ret);
253         }
254 }
255
256 static int gb_svc_version_request(struct gb_operation *op)
257 {
258         struct gb_connection *connection = op->connection;
259         struct gb_protocol_version_request *request;
260         struct gb_protocol_version_response *response;
261         struct device *dev = &connection->dev;
262
263         request = op->request->payload;
264
265         if (request->major > GB_SVC_VERSION_MAJOR) {
266                 dev_err(&connection->dev,
267                         "unsupported major version (%hhu > %hhu)\n",
268                         request->major, GB_SVC_VERSION_MAJOR);
269                 return -ENOTSUPP;
270         }
271
272         connection->module_major = request->major;
273         connection->module_minor = request->minor;
274
275         if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL)) {
276                 dev_err(dev, "%s: error allocating response\n",
277                                 __func__);
278                 return -ENOMEM;
279         }
280
281         response = op->response->payload;
282         response->major = connection->module_major;
283         response->minor = connection->module_minor;
284
285         return 0;
286 }
287
288 static int gb_svc_hello(struct gb_operation *op)
289 {
290         struct gb_connection *connection = op->connection;
291         struct greybus_host_device *hd = connection->hd;
292         struct gb_svc_hello_request *hello_request;
293         struct device *dev = &connection->dev;
294         struct gb_interface *intf;
295         u16 endo_id;
296         u8 interface_id;
297         int ret;
298
299         /*
300          * SVC sends information about the endo and interface-id on the hello
301          * request, use that to create an endo.
302          */
303         if (op->request->payload_size < sizeof(*hello_request)) {
304                 dev_err(dev, "%s: Illegal size of hello request (%zu < %zu)\n",
305                         __func__, op->request->payload_size,
306                         sizeof(*hello_request));
307                 return -EINVAL;
308         }
309
310         hello_request = op->request->payload;
311         endo_id = le16_to_cpu(hello_request->endo_id);
312         interface_id = hello_request->interface_id;
313
314         /* Setup Endo */
315         ret = greybus_endo_setup(hd, endo_id, interface_id);
316         if (ret)
317                 return ret;
318
319         /*
320          * Endo and its modules are ready now, fix AP's partially initialized
321          * svc protocol and its connection.
322          */
323         intf = gb_ap_interface_create(hd, connection, interface_id);
324         if (!intf) {
325                 gb_endo_remove(hd->endo);
326                 return ret;
327         }
328
329         return 0;
330 }
331
332 static void svc_intf_remove(struct gb_connection *connection,
333                             struct gb_interface *intf)
334 {
335         struct greybus_host_device *hd = connection->hd;
336         struct gb_svc *svc = connection->private;
337         u8 intf_id = intf->interface_id;
338         u8 device_id;
339
340         device_id = intf->device_id;
341         gb_interface_remove(hd, intf_id);
342
343         /*
344          * Destroy the two-way route between the AP and the interface.
345          */
346         gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id);
347
348         ida_simple_remove(&svc->device_id_map, device_id);
349 }
350
351 /*
352  * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it
353  * returns, irrespective of success or Failure in bringing up the module.
354  */
355 static void svc_process_hotplug(struct work_struct *work)
356 {
357         struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug,
358                                                        work);
359         struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data;
360         struct gb_connection *connection = svc_hotplug->connection;
361         struct gb_svc *svc = connection->private;
362         struct greybus_host_device *hd = connection->hd;
363         struct device *dev = &connection->dev;
364         struct gb_interface *intf;
365         u8 intf_id, device_id;
366         int ret;
367
368         /*
369          * Grab the information we need.
370          */
371         intf_id = hotplug->intf_id;
372
373         intf = gb_interface_find(hd, intf_id);
374         if (intf) {
375                 /*
376                  * We have received a hotplug request for an interface that
377                  * already exists.
378                  *
379                  * This can happen in cases like:
380                  * - bootrom loading the firmware image and booting into that,
381                  *   which only generates a hotplug event. i.e. no hot-unplug
382                  *   event.
383                  * - Or the firmware on the module crashed and sent hotplug
384                  *   request again to the SVC, which got propagated to AP.
385                  *
386                  * Remove the interface and add it again, and let user know
387                  * about this with a print message.
388                  */
389                 dev_info(dev, "Removed interface (%hhu) to add it again\n",
390                          intf_id);
391                 svc_intf_remove(connection, intf);
392         }
393
394         intf = gb_interface_create(hd, intf_id);
395         if (!intf) {
396                 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
397                         __func__, intf_id);
398                 goto free_svc_hotplug;
399         }
400
401         intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
402         intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
403         intf->ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id);
404         intf->ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id);
405
406         /*
407          * Create a device id for the interface:
408          * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
409          * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
410          *
411          * XXX Do we need to allocate device ID for SVC or the AP here? And what
412          * XXX about an AP with multiple interface blocks?
413          */
414         device_id = ida_simple_get(&svc->device_id_map,
415                                    GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
416         if (device_id < 0) {
417                 ret = device_id;
418                 dev_err(dev, "%s: Failed to allocate device id for interface with id %hhu (%d)\n",
419                         __func__, intf_id, ret);
420                 goto destroy_interface;
421         }
422
423         ret = gb_svc_intf_device_id(svc, intf_id, device_id);
424         if (ret) {
425                 dev_err(dev, "%s: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
426                         __func__, intf_id, device_id, ret);
427                 goto ida_put;
428         }
429
430         /*
431          * Create a two-way route between the AP and the new interface
432          */
433         ret = gb_svc_route_create(svc, hd->endo->ap_intf_id, GB_DEVICE_ID_AP,
434                                   intf_id, device_id);
435         if (ret) {
436                 dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
437                         __func__, intf_id, device_id, ret);
438                 goto svc_id_free;
439         }
440
441         ret = gb_interface_init(intf, device_id);
442         if (ret) {
443                 dev_err(dev, "%s: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
444                         __func__, intf_id, device_id, ret);
445                 goto destroy_route;
446         }
447
448         goto free_svc_hotplug;
449
450 destroy_route:
451         gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id);
452 svc_id_free:
453         /*
454          * XXX Should we tell SVC that this id doesn't belong to interface
455          * XXX anymore.
456          */
457 ida_put:
458         ida_simple_remove(&svc->device_id_map, device_id);
459 destroy_interface:
460         gb_interface_remove(hd, intf_id);
461 free_svc_hotplug:
462         kfree(svc_hotplug);
463 }
464
465 /*
466  * Bringing up a module can be time consuming, as that may require lots of
467  * initialization on the module side. Over that, we may also need to download
468  * the firmware first and flash that on the module.
469  *
470  * In order to make other hotplug events to not wait for all this to finish,
471  * handle most of module hotplug stuff outside of the hotplug callback, with
472  * help of a workqueue.
473  */
474 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
475 {
476         struct gb_message *request = op->request;
477         struct svc_hotplug *svc_hotplug;
478
479         if (request->payload_size < sizeof(svc_hotplug->data)) {
480                 dev_err(&op->connection->dev,
481                         "%s: short hotplug request received (%zu < %zu)\n",
482                         __func__, request->payload_size,
483                         sizeof(svc_hotplug->data));
484                 return -EINVAL;
485         }
486
487         svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL);
488         if (!svc_hotplug)
489                 return -ENOMEM;
490
491         svc_hotplug->connection = op->connection;
492         memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data));
493
494         INIT_WORK(&svc_hotplug->work, svc_process_hotplug);
495         queue_work(system_unbound_wq, &svc_hotplug->work);
496
497         return 0;
498 }
499
500 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
501 {
502         struct gb_message *request = op->request;
503         struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
504         struct greybus_host_device *hd = op->connection->hd;
505         struct device *dev = &op->connection->dev;
506         struct gb_interface *intf;
507         u8 intf_id;
508
509         if (request->payload_size < sizeof(*hot_unplug)) {
510                 dev_err(dev, "short hot unplug request received (%zu < %zu)\n",
511                         request->payload_size, sizeof(*hot_unplug));
512                 return -EINVAL;
513         }
514
515         intf_id = hot_unplug->intf_id;
516
517         intf = gb_interface_find(hd, intf_id);
518         if (!intf) {
519                 dev_err(dev, "%s: Couldn't find interface for id %hhu\n",
520                         __func__, intf_id);
521                 return -EINVAL;
522         }
523
524         svc_intf_remove(op->connection, intf);
525
526         return 0;
527 }
528
529 static int gb_svc_intf_reset_recv(struct gb_operation *op)
530 {
531         struct gb_message *request = op->request;
532         struct gb_svc_intf_reset_request *reset;
533         u8 intf_id;
534
535         if (request->payload_size < sizeof(*reset)) {
536                 dev_err(&op->connection->dev,
537                         "short reset request received (%zu < %zu)\n",
538                         request->payload_size, sizeof(*reset));
539                 return -EINVAL;
540         }
541         reset = request->payload;
542
543         intf_id = reset->intf_id;
544
545         /* FIXME Reset the interface here */
546
547         return 0;
548 }
549
550 static int gb_svc_request_recv(u8 type, struct gb_operation *op)
551 {
552         struct gb_connection *connection = op->connection;
553         struct gb_svc *svc = connection->private;
554         int ret = 0;
555
556         /*
557          * SVC requests need to follow a specific order (at least initially) and
558          * below code takes care of enforcing that. The expected order is:
559          * - PROTOCOL_VERSION
560          * - SVC_HELLO
561          * - Any other request, but the earlier two.
562          *
563          * Incoming requests are guaranteed to be serialized and so we don't
564          * need to protect 'state' for any races.
565          */
566         switch (type) {
567         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
568                 if (svc->state != GB_SVC_STATE_RESET)
569                         ret = -EINVAL;
570                 break;
571         case GB_SVC_TYPE_SVC_HELLO:
572                 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
573                         ret = -EINVAL;
574                 break;
575         default:
576                 if (svc->state != GB_SVC_STATE_SVC_HELLO)
577                         ret = -EINVAL;
578                 break;
579         }
580
581         if (ret) {
582                 dev_warn(&connection->dev,
583                          "unexpected SVC request 0x%02x received (state %u)\n",
584                          type, svc->state);
585                 return ret;
586         }
587
588         switch (type) {
589         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
590                 ret = gb_svc_version_request(op);
591                 if (!ret)
592                         svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
593                 return ret;
594         case GB_SVC_TYPE_SVC_HELLO:
595                 ret = gb_svc_hello(op);
596                 if (!ret)
597                         svc->state = GB_SVC_STATE_SVC_HELLO;
598                 return ret;
599         case GB_SVC_TYPE_INTF_HOTPLUG:
600                 return gb_svc_intf_hotplug_recv(op);
601         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
602                 return gb_svc_intf_hot_unplug_recv(op);
603         case GB_SVC_TYPE_INTF_RESET:
604                 return gb_svc_intf_reset_recv(op);
605         default:
606                 dev_err(&op->connection->dev,
607                         "unsupported request: %hhu\n", type);
608                 return -EINVAL;
609         }
610 }
611
612 static int gb_svc_connection_init(struct gb_connection *connection)
613 {
614         struct gb_svc *svc;
615
616         svc = kzalloc(sizeof(*svc), GFP_KERNEL);
617         if (!svc)
618                 return -ENOMEM;
619
620         connection->hd->svc = svc;
621         svc->state = GB_SVC_STATE_RESET;
622         svc->connection = connection;
623         connection->private = svc;
624
625         WARN_ON(connection->hd->initial_svc_connection);
626         connection->hd->initial_svc_connection = connection;
627
628         ida_init(&svc->device_id_map);
629
630         return 0;
631 }
632
633 static void gb_svc_connection_exit(struct gb_connection *connection)
634 {
635         struct gb_svc *svc = connection->private;
636
637         ida_destroy(&svc->device_id_map);
638         connection->hd->svc = NULL;
639         connection->private = NULL;
640         kfree(svc);
641 }
642
643 static struct gb_protocol svc_protocol = {
644         .name                   = "svc",
645         .id                     = GREYBUS_PROTOCOL_SVC,
646         .major                  = GB_SVC_VERSION_MAJOR,
647         .minor                  = GB_SVC_VERSION_MINOR,
648         .connection_init        = gb_svc_connection_init,
649         .connection_exit        = gb_svc_connection_exit,
650         .request_recv           = gb_svc_request_recv,
651         .flags                  = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
652                                   GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
653                                   GB_PROTOCOL_NO_BUNDLE |
654                                   GB_PROTOCOL_SKIP_VERSION |
655                                   GB_PROTOCOL_SKIP_SVC_CONNECTION,
656 };
657 gb_builtin_protocol_driver(svc_protocol);