blob: c71f41d0ef62995baa1ae6f93bdeb17a8a7a761c [file] [log] [blame]
Alex Elder30c6d9d2015-05-22 13:02:08 -05001/*
2 * SVC Greybus driver.
3 *
4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
6 *
7 * Released under the GPLv2 only.
8 */
9
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Viresh Kumar067906f2015-08-06 12:44:55 +053011#include <linux/workqueue.h>
Alex Elder30c6d9d2015-05-22 13:02:08 -050012
Viresh Kumarf66427a2015-09-02 21:27:13 +053013#include "greybus.h"
14
Johan Hovoldf6c6c132015-11-11 10:07:08 +010015#define CPORT_FLAGS_E2EFC BIT(0)
16#define CPORT_FLAGS_CSD_N BIT(1)
17#define CPORT_FLAGS_CSV_N BIT(2)
Perry Hung0b226492015-07-24 19:02:34 -040018
Viresh Kumarb45864d2015-07-24 15:32:21 +053019
Viresh Kumar067906f2015-08-06 12:44:55 +053020struct svc_hotplug {
21 struct work_struct work;
22 struct gb_connection *connection;
23 struct gb_svc_intf_hotplug_request data;
24};
25
Viresh Kumaread35462015-07-21 17:44:19 +053026
Johan Hovold66069fb2015-11-25 15:59:09 +010027static ssize_t endo_id_show(struct device *dev,
28 struct device_attribute *attr, char *buf)
29{
30 struct gb_svc *svc = to_gb_svc(dev);
31
32 return sprintf(buf, "0x%04x\n", svc->endo_id);
33}
34static DEVICE_ATTR_RO(endo_id);
35
36static ssize_t ap_intf_id_show(struct device *dev,
37 struct device_attribute *attr, char *buf)
38{
39 struct gb_svc *svc = to_gb_svc(dev);
40
41 return sprintf(buf, "%u\n", svc->ap_intf_id);
42}
43static DEVICE_ATTR_RO(ap_intf_id);
44
45static struct attribute *svc_attrs[] = {
46 &dev_attr_endo_id.attr,
47 &dev_attr_ap_intf_id.attr,
48 NULL,
49};
50ATTRIBUTE_GROUPS(svc);
51
Viresh Kumard3d44842015-07-21 17:44:18 +053052/*
53 * AP's SVC cport is required early to get messages from the SVC. This happens
54 * even before the Endo is created and hence any modules or interfaces.
55 *
56 * This is a temporary connection, used only at initial bootup.
57 */
58struct gb_connection *
Johan Hovold25376362015-11-03 18:03:23 +010059gb_ap_svc_connection_create(struct gb_host_device *hd)
Viresh Kumard3d44842015-07-21 17:44:18 +053060{
61 struct gb_connection *connection;
62
Johan Hovold582b3a12015-11-25 15:59:03 +010063 connection = gb_connection_create_range(hd, NULL,
Viresh Kumard3d44842015-07-21 17:44:18 +053064 GB_SVC_CPORT_ID,
65 GREYBUS_PROTOCOL_SVC,
66 GB_SVC_CPORT_ID,
67 GB_SVC_CPORT_ID + 1);
68
69 return connection;
70}
Viresh Kumard3d44842015-07-21 17:44:18 +053071
72/*
73 * We know endo-type and AP's interface id now, lets create a proper svc
74 * connection (and its interface/bundle) now and get rid of the initial
75 * 'partially' initialized one svc connection.
76 */
77static struct gb_interface *
Johan Hovold25376362015-11-03 18:03:23 +010078gb_ap_interface_create(struct gb_host_device *hd,
Viresh Kumard3d44842015-07-21 17:44:18 +053079 struct gb_connection *connection, u8 interface_id)
80{
81 struct gb_interface *intf;
82 struct device *dev = &hd->endo->dev;
Viresh Kumard3d44842015-07-21 17:44:18 +053083
84 intf = gb_interface_create(hd, interface_id);
85 if (!intf) {
86 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
87 __func__, interface_id);
88 return NULL;
89 }
90
91 intf->device_id = GB_DEVICE_ID_AP;
Viresh Kumar67c93ae2015-07-24 15:32:19 +053092 svc_update_connection(intf, connection);
Viresh Kumard3d44842015-07-21 17:44:18 +053093
Viresh Kumardcd05002015-07-24 15:32:20 +053094 /* Its no longer a partially initialized connection */
95 hd->initial_svc_connection = NULL;
96
Viresh Kumard3d44842015-07-21 17:44:18 +053097 return intf;
98}
99
Viresh Kumar505f16c2015-08-31 17:21:07 +0530100static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500101{
102 struct gb_svc_intf_device_id_request request;
103
104 request.intf_id = intf_id;
105 request.device_id = device_id;
106
107 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
108 &request, sizeof(request), NULL, 0);
109}
110
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530111int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500112{
113 struct gb_svc_intf_reset_request request;
114
115 request.intf_id = intf_id;
116
117 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
118 &request, sizeof(request), NULL, 0);
119}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530120EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500121
Viresh Kumar19151c32015-09-09 21:08:29 +0530122int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
123 u32 *value)
124{
125 struct gb_svc_dme_peer_get_request request;
126 struct gb_svc_dme_peer_get_response response;
127 u16 result;
128 int ret;
129
130 request.intf_id = intf_id;
131 request.attr = cpu_to_le16(attr);
132 request.selector = cpu_to_le16(selector);
133
134 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
135 &request, sizeof(request),
136 &response, sizeof(response));
137 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700138 pr_err("failed to get DME attribute (%hhu %hx %hu) %d\n",
139 intf_id, attr, selector, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +0530140 return ret;
141 }
142
143 result = le16_to_cpu(response.result_code);
144 if (result) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700145 pr_err("Unipro error %hu while getting DME attribute (%hhu %hx %hu)\n",
146 result, intf_id, attr, selector);
Viresh Kumar19151c32015-09-09 21:08:29 +0530147 return -EINVAL;
148 }
149
150 if (value)
151 *value = le32_to_cpu(response.attr_value);
152
153 return 0;
154}
155EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
156
157int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
158 u32 value)
159{
160 struct gb_svc_dme_peer_set_request request;
161 struct gb_svc_dme_peer_set_response response;
162 u16 result;
163 int ret;
164
165 request.intf_id = intf_id;
166 request.attr = cpu_to_le16(attr);
167 request.selector = cpu_to_le16(selector);
168 request.value = cpu_to_le32(value);
169
170 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
171 &request, sizeof(request),
172 &response, sizeof(response));
173 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700174 pr_err("failed to set DME attribute (%hhu %hx %hu %u) %d\n",
175 intf_id, attr, selector, value, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +0530176 return ret;
177 }
178
179 result = le16_to_cpu(response.result_code);
180 if (result) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700181 pr_err("Unipro error %hu while setting DME attribute (%hhu %hx %hu %u)\n",
182 result, intf_id, attr, selector, value);
Viresh Kumar19151c32015-09-09 21:08:29 +0530183 return -EINVAL;
184 }
185
186 return 0;
187}
188EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
189
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700190/*
191 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
192 * status attribute. AP needs to read and clear it, after reading a non-zero
193 * value from it.
194 *
195 * FIXME: This is module-hardware dependent and needs to be extended for every
196 * type of module we want to support.
197 */
198static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
199{
Johan Hovold25376362015-11-03 18:03:23 +0100200 struct gb_host_device *hd = intf->hd;
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700201 int ret;
202 u32 value;
203
204 /* Read and clear boot status in T_TstSrcIncrement */
205 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id,
206 DME_ATTR_T_TST_SRC_INCREMENT,
207 DME_ATTR_SELECTOR_INDEX, &value);
208
209 if (ret)
210 return ret;
211
212 /*
213 * A nonzero boot status indicates the module has finished
214 * booting. Clear it.
215 */
216 if (!value) {
217 dev_err(&intf->dev, "Module not ready yet\n");
218 return -ENODEV;
219 }
220
Viresh Kumar1575ef12015-10-07 15:40:24 -0400221 /*
222 * Check if the module needs to boot from unipro.
223 * For ES2: We need to check lowest 8 bits of 'value'.
224 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
225 *
226 * FIXME: Add code to find if we are on ES2 or ES3 to have separate
227 * checks.
228 */
229 if (value == DME_TSI_UNIPRO_BOOT_STARTED ||
230 value == DME_TSI_FALLBACK_UNIPRO_BOOT_STARTED)
231 intf->boot_over_unipro = true;
232
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700233 return gb_svc_dme_peer_set(hd->svc, intf->interface_id,
234 DME_ATTR_T_TST_SRC_INCREMENT,
235 DME_ATTR_SELECTOR_INDEX, 0);
236}
237
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530238int gb_svc_connection_create(struct gb_svc *svc,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500239 u8 intf1_id, u16 cport1_id,
Viresh Kumar1575ef12015-10-07 15:40:24 -0400240 u8 intf2_id, u16 cport2_id,
241 bool boot_over_unipro)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500242{
243 struct gb_svc_conn_create_request request;
244
245 request.intf1_id = intf1_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100246 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500247 request.intf2_id = intf2_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100248 request.cport2_id = cpu_to_le16(cport2_id);
Perry Hung0b226492015-07-24 19:02:34 -0400249 /*
250 * XXX: fix connections paramaters to TC0 and all CPort flags
251 * for now.
252 */
253 request.tc = 0;
Viresh Kumar1575ef12015-10-07 15:40:24 -0400254
255 /*
256 * We need to skip setting E2EFC and other flags to the connection
257 * create request, for all cports, on an interface that need to boot
258 * over unipro, i.e. interfaces required to download firmware.
259 */
260 if (boot_over_unipro)
261 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N;
262 else
263 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500264
265 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
266 &request, sizeof(request), NULL, 0);
267}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530268EXPORT_SYMBOL_GPL(gb_svc_connection_create);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500269
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530270void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
271 u8 intf2_id, u16 cport2_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500272{
273 struct gb_svc_conn_destroy_request request;
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530274 struct gb_connection *connection = svc->connection;
275 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500276
277 request.intf1_id = intf1_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100278 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500279 request.intf2_id = intf2_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100280 request.cport2_id = cpu_to_le16(cport2_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500281
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530282 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
283 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700284 if (ret)
Johan Hovold35a84ba2015-11-11 10:07:07 +0100285 pr_err("failed to destroy connection (%hhu:%hu %hhu:%hu) %d\n",
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700286 intf1_id, cport1_id, intf2_id, cport2_id, ret);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500287}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530288EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500289
Viresh Kumarbb106852015-09-07 16:01:25 +0530290/* Creates bi-directional routes between the devices */
Viresh Kumar505f16c2015-08-31 17:21:07 +0530291static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
292 u8 intf2_id, u8 dev2_id)
Perry Hunge08aaa42015-07-24 19:02:31 -0400293{
294 struct gb_svc_route_create_request request;
295
296 request.intf1_id = intf1_id;
297 request.dev1_id = dev1_id;
298 request.intf2_id = intf2_id;
299 request.dev2_id = dev2_id;
300
301 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
302 &request, sizeof(request), NULL, 0);
303}
Perry Hunge08aaa42015-07-24 19:02:31 -0400304
Viresh Kumar0a020572015-09-07 18:05:26 +0530305/* Destroys bi-directional routes between the devices */
306static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
307{
308 struct gb_svc_route_destroy_request request;
309 int ret;
310
311 request.intf1_id = intf1_id;
312 request.intf2_id = intf2_id;
313
314 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
315 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700316 if (ret)
Johan Hovold35a84ba2015-11-11 10:07:07 +0100317 pr_err("failed to destroy route (%hhu %hhu) %d\n",
Viresh Kumar0a020572015-09-07 18:05:26 +0530318 intf1_id, intf2_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530319}
320
Viresh Kumaread35462015-07-21 17:44:19 +0530321static int gb_svc_version_request(struct gb_operation *op)
322{
323 struct gb_connection *connection = op->connection;
Johan Hovoldcfb16902015-09-15 10:48:01 +0200324 struct gb_protocol_version_request *request;
325 struct gb_protocol_version_response *response;
Viresh Kumaread35462015-07-21 17:44:19 +0530326
Johan Hovold55510842015-11-19 18:28:01 +0100327 if (op->request->payload_size < sizeof(*request)) {
328 pr_err("%d: short version request (%zu < %zu)\n",
329 connection->intf_cport_id,
330 op->request->payload_size,
331 sizeof(*request));
332 return -EINVAL;
333 }
334
Johan Hovoldcfb16902015-09-15 10:48:01 +0200335 request = op->request->payload;
Viresh Kumaread35462015-07-21 17:44:19 +0530336
Johan Hovoldcfb16902015-09-15 10:48:01 +0200337 if (request->major > GB_SVC_VERSION_MAJOR) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700338 pr_err("%d: unsupported major version (%hhu > %hhu)\n",
339 connection->intf_cport_id, request->major,
340 GB_SVC_VERSION_MAJOR);
Viresh Kumaread35462015-07-21 17:44:19 +0530341 return -ENOTSUPP;
342 }
343
Johan Hovoldcfb16902015-09-15 10:48:01 +0200344 connection->module_major = request->major;
345 connection->module_minor = request->minor;
Viresh Kumar3ea959e32015-08-11 07:36:14 +0530346
Johan Hovoldcfb16902015-09-15 10:48:01 +0200347 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL)) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700348 pr_err("%d: error allocating response\n",
349 connection->intf_cport_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530350 return -ENOMEM;
351 }
352
Johan Hovoldcfb16902015-09-15 10:48:01 +0200353 response = op->response->payload;
354 response->major = connection->module_major;
355 response->minor = connection->module_minor;
Johan Hovold59832932015-09-15 10:48:00 +0200356
Viresh Kumaread35462015-07-21 17:44:19 +0530357 return 0;
358}
359
360static int gb_svc_hello(struct gb_operation *op)
361{
362 struct gb_connection *connection = op->connection;
Johan Hovold88f7b962015-11-25 15:59:08 +0100363 struct gb_svc *svc = connection->private;
Johan Hovold25376362015-11-03 18:03:23 +0100364 struct gb_host_device *hd = connection->hd;
Viresh Kumaread35462015-07-21 17:44:19 +0530365 struct gb_svc_hello_request *hello_request;
Viresh Kumaread35462015-07-21 17:44:19 +0530366 struct gb_interface *intf;
Viresh Kumaread35462015-07-21 17:44:19 +0530367 int ret;
368
Viresh Kumaread35462015-07-21 17:44:19 +0530369 /*
370 * SVC sends information about the endo and interface-id on the hello
371 * request, use that to create an endo.
372 */
Viresh Kumar0c32d2a2015-08-11 07:29:19 +0530373 if (op->request->payload_size < sizeof(*hello_request)) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700374 pr_err("%d: Illegal size of hello request (%zu < %zu)\n",
375 connection->intf_cport_id, op->request->payload_size,
376 sizeof(*hello_request));
Viresh Kumaread35462015-07-21 17:44:19 +0530377 return -EINVAL;
378 }
379
380 hello_request = op->request->payload;
Johan Hovold66069fb2015-11-25 15:59:09 +0100381 svc->endo_id = le16_to_cpu(hello_request->endo_id);
382 svc->ap_intf_id = hello_request->interface_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530383
Johan Hovold88f7b962015-11-25 15:59:08 +0100384 ret = device_add(&svc->dev);
385 if (ret) {
386 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
387 return ret;
388 }
389
Viresh Kumaread35462015-07-21 17:44:19 +0530390 /* Setup Endo */
Johan Hovold66069fb2015-11-25 15:59:09 +0100391 ret = greybus_endo_setup(hd, svc->endo_id, svc->ap_intf_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530392 if (ret)
393 return ret;
394
395 /*
396 * Endo and its modules are ready now, fix AP's partially initialized
397 * svc protocol and its connection.
398 */
Johan Hovold66069fb2015-11-25 15:59:09 +0100399 intf = gb_ap_interface_create(hd, connection, svc->ap_intf_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530400 if (!intf) {
401 gb_endo_remove(hd->endo);
402 return ret;
403 }
404
405 return 0;
406}
407
Viresh Kumarbbaca712015-09-23 16:48:08 -0700408static void svc_intf_remove(struct gb_connection *connection,
409 struct gb_interface *intf)
410{
Viresh Kumarbbaca712015-09-23 16:48:08 -0700411 struct gb_svc *svc = connection->private;
412 u8 intf_id = intf->interface_id;
413 u8 device_id;
414
415 device_id = intf->device_id;
Viresh Kumar80d1ede2015-09-23 16:48:10 -0700416 gb_interface_remove(intf);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700417
418 /*
419 * Destroy the two-way route between the AP and the interface.
420 */
Johan Hovold66069fb2015-11-25 15:59:09 +0100421 gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700422
423 ida_simple_remove(&svc->device_id_map, device_id);
424}
425
Viresh Kumar067906f2015-08-06 12:44:55 +0530426/*
427 * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it
428 * returns, irrespective of success or Failure in bringing up the module.
429 */
430static void svc_process_hotplug(struct work_struct *work)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500431{
Viresh Kumar067906f2015-08-06 12:44:55 +0530432 struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug,
433 work);
434 struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data;
435 struct gb_connection *connection = svc_hotplug->connection;
436 struct gb_svc *svc = connection->private;
Johan Hovold25376362015-11-03 18:03:23 +0100437 struct gb_host_device *hd = connection->hd;
Viresh Kumaread35462015-07-21 17:44:19 +0530438 struct gb_interface *intf;
439 u8 intf_id, device_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530440 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500441
Alex Elder30c6d9d2015-05-22 13:02:08 -0500442 /*
443 * Grab the information we need.
Viresh Kumar7eb89192015-07-01 12:13:50 +0530444 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500445 intf_id = hotplug->intf_id;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500446
Viresh Kumarbbaca712015-09-23 16:48:08 -0700447 intf = gb_interface_find(hd, intf_id);
448 if (intf) {
449 /*
450 * We have received a hotplug request for an interface that
451 * already exists.
452 *
453 * This can happen in cases like:
454 * - bootrom loading the firmware image and booting into that,
455 * which only generates a hotplug event. i.e. no hot-unplug
456 * event.
457 * - Or the firmware on the module crashed and sent hotplug
458 * request again to the SVC, which got propagated to AP.
459 *
460 * Remove the interface and add it again, and let user know
461 * about this with a print message.
462 */
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700463 pr_info("%d: Removed interface (%hhu) to add it again\n",
464 connection->intf_cport_id, intf_id);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700465 svc_intf_remove(connection, intf);
466 }
467
Viresh Kumaread35462015-07-21 17:44:19 +0530468 intf = gb_interface_create(hd, intf_id);
469 if (!intf) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700470 pr_err("%d: Failed to create interface with id %hhu\n",
471 connection->intf_cport_id, intf_id);
Viresh Kumar067906f2015-08-06 12:44:55 +0530472 goto free_svc_hotplug;
Viresh Kumaread35462015-07-21 17:44:19 +0530473 }
474
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700475 ret = gb_svc_read_and_clear_module_boot_status(intf);
476 if (ret)
477 goto destroy_interface;
478
Viresh Kumar3944a452015-08-12 09:19:31 +0530479 intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
480 intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
Johan Hovold9f592632015-11-25 15:58:56 +0100481 intf->vendor_id = le32_to_cpu(hotplug->data.ara_vend_id);
482 intf->product_id = le32_to_cpu(hotplug->data.ara_prod_id);
Viresh Kumar3944a452015-08-12 09:19:31 +0530483
Viresh Kumaread35462015-07-21 17:44:19 +0530484 /*
485 * Create a device id for the interface:
486 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
487 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
488 *
489 * XXX Do we need to allocate device ID for SVC or the AP here? And what
490 * XXX about an AP with multiple interface blocks?
491 */
Johan Hovoldc09db182015-09-15 09:18:08 +0200492 device_id = ida_simple_get(&svc->device_id_map,
Johan Hovold89f637f2015-09-01 12:25:25 +0200493 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
Viresh Kumaread35462015-07-21 17:44:19 +0530494 if (device_id < 0) {
495 ret = device_id;
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700496 pr_err("%d: Failed to allocate device id for interface with id %hhu (%d)\n",
497 connection->intf_cport_id, intf_id, ret);
Viresh Kumaread35462015-07-21 17:44:19 +0530498 goto destroy_interface;
499 }
500
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530501 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530502 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700503 pr_err("%d: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
504 connection->intf_cport_id, intf_id, device_id, ret);
Viresh Kumaread35462015-07-21 17:44:19 +0530505 goto ida_put;
506 }
507
Perry Hung7e275462015-07-24 19:02:32 -0400508 /*
509 * Create a two-way route between the AP and the new interface
510 */
Johan Hovold66069fb2015-11-25 15:59:09 +0100511 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP,
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530512 intf_id, device_id);
Perry Hung7e275462015-07-24 19:02:32 -0400513 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700514 pr_err("%d: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
515 connection->intf_cport_id, intf_id, device_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530516 goto svc_id_free;
Perry Hung7e275462015-07-24 19:02:32 -0400517 }
518
Viresh Kumaread35462015-07-21 17:44:19 +0530519 ret = gb_interface_init(intf, device_id);
520 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700521 pr_err("%d: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
522 connection->intf_cport_id, intf_id, device_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530523 goto destroy_route;
Viresh Kumaread35462015-07-21 17:44:19 +0530524 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500525
Viresh Kumar067906f2015-08-06 12:44:55 +0530526 goto free_svc_hotplug;
Viresh Kumaread35462015-07-21 17:44:19 +0530527
Viresh Kumar0a020572015-09-07 18:05:26 +0530528destroy_route:
Johan Hovold66069fb2015-11-25 15:59:09 +0100529 gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530530svc_id_free:
531 /*
532 * XXX Should we tell SVC that this id doesn't belong to interface
533 * XXX anymore.
534 */
535ida_put:
Johan Hovoldc09db182015-09-15 09:18:08 +0200536 ida_simple_remove(&svc->device_id_map, device_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530537destroy_interface:
Viresh Kumar80d1ede2015-09-23 16:48:10 -0700538 gb_interface_remove(intf);
Viresh Kumar067906f2015-08-06 12:44:55 +0530539free_svc_hotplug:
540 kfree(svc_hotplug);
541}
Viresh Kumaread35462015-07-21 17:44:19 +0530542
Viresh Kumar067906f2015-08-06 12:44:55 +0530543/*
544 * Bringing up a module can be time consuming, as that may require lots of
545 * initialization on the module side. Over that, we may also need to download
546 * the firmware first and flash that on the module.
547 *
548 * In order to make other hotplug events to not wait for all this to finish,
549 * handle most of module hotplug stuff outside of the hotplug callback, with
550 * help of a workqueue.
551 */
552static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
553{
554 struct gb_message *request = op->request;
555 struct svc_hotplug *svc_hotplug;
556
557 if (request->payload_size < sizeof(svc_hotplug->data)) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700558 pr_err("%d: short hotplug request received (%zu < %zu)\n",
559 op->connection->intf_cport_id, request->payload_size,
560 sizeof(svc_hotplug->data));
Viresh Kumar067906f2015-08-06 12:44:55 +0530561 return -EINVAL;
562 }
563
Johan Hovold287bba82015-09-01 12:25:26 +0200564 svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL);
Viresh Kumar067906f2015-08-06 12:44:55 +0530565 if (!svc_hotplug)
566 return -ENOMEM;
567
568 svc_hotplug->connection = op->connection;
569 memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data));
570
571 INIT_WORK(&svc_hotplug->work, svc_process_hotplug);
572 queue_work(system_unbound_wq, &svc_hotplug->work);
573
574 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500575}
576
577static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
578{
579 struct gb_message *request = op->request;
Viresh Kumaread35462015-07-21 17:44:19 +0530580 struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
Johan Hovold25376362015-11-03 18:03:23 +0100581 struct gb_host_device *hd = op->connection->hd;
Viresh Kumaread35462015-07-21 17:44:19 +0530582 struct gb_interface *intf;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500583 u8 intf_id;
584
585 if (request->payload_size < sizeof(*hot_unplug)) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700586 pr_err("connection %d: short hot unplug request received (%zu < %zu)\n",
587 op->connection->intf_cport_id, request->payload_size,
588 sizeof(*hot_unplug));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500589 return -EINVAL;
590 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500591
592 intf_id = hot_unplug->intf_id;
593
Viresh Kumaread35462015-07-21 17:44:19 +0530594 intf = gb_interface_find(hd, intf_id);
595 if (!intf) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700596 pr_err("connection %d: Couldn't find interface for id %hhu\n",
597 op->connection->intf_cport_id, intf_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530598 return -EINVAL;
599 }
600
Viresh Kumarbbaca712015-09-23 16:48:08 -0700601 svc_intf_remove(op->connection, intf);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500602
603 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500604}
605
606static int gb_svc_intf_reset_recv(struct gb_operation *op)
607{
608 struct gb_message *request = op->request;
609 struct gb_svc_intf_reset_request *reset;
610 u8 intf_id;
611
612 if (request->payload_size < sizeof(*reset)) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700613 pr_err("connection %d: short reset request received (%zu < %zu)\n",
614 op->connection->intf_cport_id, request->payload_size,
615 sizeof(*reset));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500616 return -EINVAL;
617 }
618 reset = request->payload;
619
620 intf_id = reset->intf_id;
621
622 /* FIXME Reset the interface here */
623
624 return 0;
625}
626
627static int gb_svc_request_recv(u8 type, struct gb_operation *op)
628{
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530629 struct gb_connection *connection = op->connection;
630 struct gb_svc *svc = connection->private;
631 int ret = 0;
632
633 /*
634 * SVC requests need to follow a specific order (at least initially) and
635 * below code takes care of enforcing that. The expected order is:
636 * - PROTOCOL_VERSION
637 * - SVC_HELLO
638 * - Any other request, but the earlier two.
639 *
640 * Incoming requests are guaranteed to be serialized and so we don't
641 * need to protect 'state' for any races.
642 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500643 switch (type) {
Viresh Kumar0e2462d2015-08-14 07:57:38 +0530644 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530645 if (svc->state != GB_SVC_STATE_RESET)
646 ret = -EINVAL;
647 break;
Viresh Kumaread35462015-07-21 17:44:19 +0530648 case GB_SVC_TYPE_SVC_HELLO:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530649 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
650 ret = -EINVAL;
651 break;
652 default:
653 if (svc->state != GB_SVC_STATE_SVC_HELLO)
654 ret = -EINVAL;
655 break;
656 }
657
658 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700659 pr_warn("connection %d: unexpected SVC request 0x%02x received (state %u)\n",
660 connection->intf_cport_id, type, svc->state);
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530661 return ret;
662 }
663
664 switch (type) {
665 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
666 ret = gb_svc_version_request(op);
667 if (!ret)
668 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
669 return ret;
670 case GB_SVC_TYPE_SVC_HELLO:
671 ret = gb_svc_hello(op);
672 if (!ret)
673 svc->state = GB_SVC_STATE_SVC_HELLO;
674 return ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500675 case GB_SVC_TYPE_INTF_HOTPLUG:
676 return gb_svc_intf_hotplug_recv(op);
677 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
678 return gb_svc_intf_hot_unplug_recv(op);
679 case GB_SVC_TYPE_INTF_RESET:
680 return gb_svc_intf_reset_recv(op);
681 default:
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700682 pr_err("connection %d: unsupported request: %hhu\n",
683 connection->intf_cport_id, type);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500684 return -EINVAL;
685 }
686}
687
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100688static void gb_svc_release(struct device *dev)
689{
Johan Hovold88f7b962015-11-25 15:59:08 +0100690 struct gb_svc *svc = to_gb_svc(dev);
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100691
692 ida_destroy(&svc->device_id_map);
693 kfree(svc);
694}
695
696struct device_type greybus_svc_type = {
697 .name = "greybus_svc",
698 .release = gb_svc_release,
699};
700
Alex Elder30c6d9d2015-05-22 13:02:08 -0500701static int gb_svc_connection_init(struct gb_connection *connection)
702{
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100703 struct gb_host_device *hd = connection->hd;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500704 struct gb_svc *svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500705
706 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
707 if (!svc)
708 return -ENOMEM;
709
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100710 svc->dev.parent = &hd->dev;
711 svc->dev.bus = &greybus_bus_type;
712 svc->dev.type = &greybus_svc_type;
Johan Hovold66069fb2015-11-25 15:59:09 +0100713 svc->dev.groups = svc_groups;
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100714 svc->dev.dma_mask = svc->dev.parent->dma_mask;
715 device_initialize(&svc->dev);
716
717 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
718
Johan Hovold6106e512015-11-25 15:59:07 +0100719 ida_init(&svc->device_id_map);
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530720 svc->state = GB_SVC_STATE_RESET;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500721 svc->connection = connection;
722 connection->private = svc;
Viresh Kumard3d44842015-07-21 17:44:18 +0530723
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100724 hd->svc = svc;
725
Viresh Kumardcd05002015-07-24 15:32:20 +0530726 WARN_ON(connection->hd->initial_svc_connection);
727 connection->hd->initial_svc_connection = connection;
Viresh Kumard3d44842015-07-21 17:44:18 +0530728
Viresh Kumar18d777c2015-07-21 17:44:20 +0530729 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500730}
731
732static void gb_svc_connection_exit(struct gb_connection *connection)
733{
734 struct gb_svc *svc = connection->private;
735
Johan Hovold88f7b962015-11-25 15:59:08 +0100736 if (device_is_registered(&svc->dev))
737 device_del(&svc->dev);
738
Perry Hung75a60ed2015-07-24 19:02:33 -0400739 connection->hd->svc = NULL;
Viresh Kumard3d44842015-07-21 17:44:18 +0530740 connection->private = NULL;
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100741
742 put_device(&svc->dev);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500743}
744
745static struct gb_protocol svc_protocol = {
746 .name = "svc",
747 .id = GREYBUS_PROTOCOL_SVC,
Viresh Kumar06e305f2015-07-01 12:13:51 +0530748 .major = GB_SVC_VERSION_MAJOR,
749 .minor = GB_SVC_VERSION_MINOR,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500750 .connection_init = gb_svc_connection_init,
751 .connection_exit = gb_svc_connection_exit,
752 .request_recv = gb_svc_request_recv,
Viresh Kumar5a5296b2015-09-07 16:01:24 +0530753 .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
754 GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
755 GB_PROTOCOL_NO_BUNDLE |
756 GB_PROTOCOL_SKIP_VERSION |
757 GB_PROTOCOL_SKIP_SVC_CONNECTION,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500758};
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530759gb_builtin_protocol_driver(svc_protocol);