Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 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 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 10 | #include <linux/workqueue.h> |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 11 | |
Viresh Kumar | f66427a | 2015-09-02 21:27:13 +0530 | [diff] [blame] | 12 | #include "greybus.h" |
| 13 | |
Perry Hung | 0b22649 | 2015-07-24 19:02:34 -0400 | [diff] [blame] | 14 | #define CPORT_FLAGS_E2EFC (1) |
| 15 | #define CPORT_FLAGS_CSD_N (2) |
| 16 | #define CPORT_FLAGS_CSV_N (4) |
| 17 | |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 18 | enum gb_svc_state { |
| 19 | GB_SVC_STATE_RESET, |
| 20 | GB_SVC_STATE_PROTOCOL_VERSION, |
| 21 | GB_SVC_STATE_SVC_HELLO, |
| 22 | }; |
| 23 | |
Viresh Kumar | b45864d | 2015-07-24 15:32:21 +0530 | [diff] [blame] | 24 | struct gb_svc { |
| 25 | struct gb_connection *connection; |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 26 | enum gb_svc_state state; |
Johan Hovold | c09db18 | 2015-09-15 09:18:08 +0200 | [diff] [blame] | 27 | struct ida device_id_map; |
Viresh Kumar | b45864d | 2015-07-24 15:32:21 +0530 | [diff] [blame] | 28 | }; |
| 29 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 30 | struct svc_hotplug { |
| 31 | struct work_struct work; |
| 32 | struct gb_connection *connection; |
| 33 | struct gb_svc_intf_hotplug_request data; |
| 34 | }; |
| 35 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 36 | |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 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 | } |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 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; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 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; |
Viresh Kumar | 67c93ae | 2015-07-24 15:32:19 +0530 | [diff] [blame] | 77 | svc_update_connection(intf, connection); |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 78 | |
Viresh Kumar | dcd0500 | 2015-07-24 15:32:20 +0530 | [diff] [blame] | 79 | /* Its no longer a partially initialized connection */ |
| 80 | hd->initial_svc_connection = NULL; |
| 81 | |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 82 | return intf; |
| 83 | } |
| 84 | |
Viresh Kumar | 505f16c | 2015-08-31 17:21:07 +0530 | [diff] [blame] | 85 | static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 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 | |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 96 | int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 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 | } |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 105 | EXPORT_SYMBOL_GPL(gb_svc_intf_reset); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 106 | |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 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 | |
Viresh Kumar | 6bec5c7 | 2015-09-24 14:40:29 -0700 | [diff] [blame^] | 179 | /* |
| 180 | * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot |
| 181 | * status attribute. AP needs to read and clear it, after reading a non-zero |
| 182 | * value from it. |
| 183 | * |
| 184 | * FIXME: This is module-hardware dependent and needs to be extended for every |
| 185 | * type of module we want to support. |
| 186 | */ |
| 187 | static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf) |
| 188 | { |
| 189 | struct greybus_host_device *hd = intf->hd; |
| 190 | int ret; |
| 191 | u32 value; |
| 192 | |
| 193 | /* Read and clear boot status in T_TstSrcIncrement */ |
| 194 | ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, |
| 195 | DME_ATTR_T_TST_SRC_INCREMENT, |
| 196 | DME_ATTR_SELECTOR_INDEX, &value); |
| 197 | |
| 198 | if (ret) |
| 199 | return ret; |
| 200 | |
| 201 | /* |
| 202 | * A nonzero boot status indicates the module has finished |
| 203 | * booting. Clear it. |
| 204 | */ |
| 205 | if (!value) { |
| 206 | dev_err(&intf->dev, "Module not ready yet\n"); |
| 207 | return -ENODEV; |
| 208 | } |
| 209 | |
| 210 | return gb_svc_dme_peer_set(hd->svc, intf->interface_id, |
| 211 | DME_ATTR_T_TST_SRC_INCREMENT, |
| 212 | DME_ATTR_SELECTOR_INDEX, 0); |
| 213 | } |
| 214 | |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 215 | int gb_svc_connection_create(struct gb_svc *svc, |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 216 | u8 intf1_id, u16 cport1_id, |
| 217 | u8 intf2_id, u16 cport2_id) |
| 218 | { |
| 219 | struct gb_svc_conn_create_request request; |
| 220 | |
| 221 | request.intf1_id = intf1_id; |
Rui Miguel Silva | 2498050 | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 222 | request.cport1_id = cpu_to_le16(cport1_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 223 | request.intf2_id = intf2_id; |
Rui Miguel Silva | 2498050 | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 224 | request.cport2_id = cpu_to_le16(cport2_id); |
Perry Hung | 0b22649 | 2015-07-24 19:02:34 -0400 | [diff] [blame] | 225 | /* |
| 226 | * XXX: fix connections paramaters to TC0 and all CPort flags |
| 227 | * for now. |
| 228 | */ |
| 229 | request.tc = 0; |
| 230 | request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 231 | |
| 232 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE, |
| 233 | &request, sizeof(request), NULL, 0); |
| 234 | } |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 235 | EXPORT_SYMBOL_GPL(gb_svc_connection_create); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 236 | |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 237 | void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id, |
| 238 | u8 intf2_id, u16 cport2_id) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 239 | { |
| 240 | struct gb_svc_conn_destroy_request request; |
Viresh Kumar | d9fcfff | 2015-08-31 17:21:05 +0530 | [diff] [blame] | 241 | struct gb_connection *connection = svc->connection; |
| 242 | int ret; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 243 | |
| 244 | request.intf1_id = intf1_id; |
Rui Miguel Silva | 2498050 | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 245 | request.cport1_id = cpu_to_le16(cport1_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 246 | request.intf2_id = intf2_id; |
Rui Miguel Silva | 2498050 | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 247 | request.cport2_id = cpu_to_le16(cport2_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 248 | |
Viresh Kumar | d9fcfff | 2015-08-31 17:21:05 +0530 | [diff] [blame] | 249 | ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY, |
| 250 | &request, sizeof(request), NULL, 0); |
| 251 | if (ret) { |
| 252 | dev_err(&connection->dev, |
| 253 | "failed to destroy connection (%hhx:%hx %hhx:%hx) %d\n", |
| 254 | intf1_id, cport1_id, intf2_id, cport2_id, ret); |
| 255 | } |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 256 | } |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 257 | EXPORT_SYMBOL_GPL(gb_svc_connection_destroy); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 258 | |
Viresh Kumar | bb10685 | 2015-09-07 16:01:25 +0530 | [diff] [blame] | 259 | /* Creates bi-directional routes between the devices */ |
Viresh Kumar | 505f16c | 2015-08-31 17:21:07 +0530 | [diff] [blame] | 260 | static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id, |
| 261 | u8 intf2_id, u8 dev2_id) |
Perry Hung | e08aaa4 | 2015-07-24 19:02:31 -0400 | [diff] [blame] | 262 | { |
| 263 | struct gb_svc_route_create_request request; |
| 264 | |
| 265 | request.intf1_id = intf1_id; |
| 266 | request.dev1_id = dev1_id; |
| 267 | request.intf2_id = intf2_id; |
| 268 | request.dev2_id = dev2_id; |
| 269 | |
| 270 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE, |
| 271 | &request, sizeof(request), NULL, 0); |
| 272 | } |
Perry Hung | e08aaa4 | 2015-07-24 19:02:31 -0400 | [diff] [blame] | 273 | |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 274 | /* Destroys bi-directional routes between the devices */ |
| 275 | static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id) |
| 276 | { |
| 277 | struct gb_svc_route_destroy_request request; |
| 278 | int ret; |
| 279 | |
| 280 | request.intf1_id = intf1_id; |
| 281 | request.intf2_id = intf2_id; |
| 282 | |
| 283 | ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY, |
| 284 | &request, sizeof(request), NULL, 0); |
| 285 | if (ret) { |
| 286 | dev_err(&svc->connection->dev, |
| 287 | "failed to destroy route (%hhx %hhx) %d\n", |
| 288 | intf1_id, intf2_id, ret); |
| 289 | } |
| 290 | } |
| 291 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 292 | static int gb_svc_version_request(struct gb_operation *op) |
| 293 | { |
| 294 | struct gb_connection *connection = op->connection; |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 295 | struct gb_protocol_version_request *request; |
| 296 | struct gb_protocol_version_response *response; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 297 | struct device *dev = &connection->dev; |
| 298 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 299 | request = op->request->payload; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 300 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 301 | if (request->major > GB_SVC_VERSION_MAJOR) { |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 302 | dev_err(&connection->dev, |
| 303 | "unsupported major version (%hhu > %hhu)\n", |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 304 | request->major, GB_SVC_VERSION_MAJOR); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 305 | return -ENOTSUPP; |
| 306 | } |
| 307 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 308 | connection->module_major = request->major; |
| 309 | connection->module_minor = request->minor; |
Viresh Kumar | 3ea959e3 | 2015-08-11 07:36:14 +0530 | [diff] [blame] | 310 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 311 | if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL)) { |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 312 | dev_err(dev, "%s: error allocating response\n", |
| 313 | __func__); |
| 314 | return -ENOMEM; |
| 315 | } |
| 316 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 317 | response = op->response->payload; |
| 318 | response->major = connection->module_major; |
| 319 | response->minor = connection->module_minor; |
Johan Hovold | 5983293 | 2015-09-15 10:48:00 +0200 | [diff] [blame] | 320 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static int gb_svc_hello(struct gb_operation *op) |
| 325 | { |
| 326 | struct gb_connection *connection = op->connection; |
| 327 | struct greybus_host_device *hd = connection->hd; |
| 328 | struct gb_svc_hello_request *hello_request; |
| 329 | struct device *dev = &connection->dev; |
| 330 | struct gb_interface *intf; |
| 331 | u16 endo_id; |
| 332 | u8 interface_id; |
| 333 | int ret; |
| 334 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 335 | /* |
| 336 | * SVC sends information about the endo and interface-id on the hello |
| 337 | * request, use that to create an endo. |
| 338 | */ |
Viresh Kumar | 0c32d2a | 2015-08-11 07:29:19 +0530 | [diff] [blame] | 339 | if (op->request->payload_size < sizeof(*hello_request)) { |
| 340 | dev_err(dev, "%s: Illegal size of hello request (%zu < %zu)\n", |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 341 | __func__, op->request->payload_size, |
| 342 | sizeof(*hello_request)); |
| 343 | return -EINVAL; |
| 344 | } |
| 345 | |
| 346 | hello_request = op->request->payload; |
| 347 | endo_id = le16_to_cpu(hello_request->endo_id); |
| 348 | interface_id = hello_request->interface_id; |
| 349 | |
| 350 | /* Setup Endo */ |
| 351 | ret = greybus_endo_setup(hd, endo_id, interface_id); |
| 352 | if (ret) |
| 353 | return ret; |
| 354 | |
| 355 | /* |
| 356 | * Endo and its modules are ready now, fix AP's partially initialized |
| 357 | * svc protocol and its connection. |
| 358 | */ |
| 359 | intf = gb_ap_interface_create(hd, connection, interface_id); |
| 360 | if (!intf) { |
| 361 | gb_endo_remove(hd->endo); |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 368 | static void svc_intf_remove(struct gb_connection *connection, |
| 369 | struct gb_interface *intf) |
| 370 | { |
| 371 | struct greybus_host_device *hd = connection->hd; |
| 372 | struct gb_svc *svc = connection->private; |
| 373 | u8 intf_id = intf->interface_id; |
| 374 | u8 device_id; |
| 375 | |
| 376 | device_id = intf->device_id; |
| 377 | gb_interface_remove(hd, intf_id); |
| 378 | |
| 379 | /* |
| 380 | * Destroy the two-way route between the AP and the interface. |
| 381 | */ |
| 382 | gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id); |
| 383 | |
| 384 | ida_simple_remove(&svc->device_id_map, device_id); |
| 385 | } |
| 386 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 387 | /* |
| 388 | * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it |
| 389 | * returns, irrespective of success or Failure in bringing up the module. |
| 390 | */ |
| 391 | static void svc_process_hotplug(struct work_struct *work) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 392 | { |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 393 | struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug, |
| 394 | work); |
| 395 | struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data; |
| 396 | struct gb_connection *connection = svc_hotplug->connection; |
| 397 | struct gb_svc *svc = connection->private; |
Viresh Kumar | b9fb704 | 2015-09-01 17:16:16 +0530 | [diff] [blame] | 398 | struct greybus_host_device *hd = connection->hd; |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 399 | struct device *dev = &connection->dev; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 400 | struct gb_interface *intf; |
| 401 | u8 intf_id, device_id; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 402 | int ret; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 403 | |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 404 | /* |
| 405 | * Grab the information we need. |
Viresh Kumar | 7eb8919 | 2015-07-01 12:13:50 +0530 | [diff] [blame] | 406 | */ |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 407 | intf_id = hotplug->intf_id; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 408 | |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 409 | intf = gb_interface_find(hd, intf_id); |
| 410 | if (intf) { |
| 411 | /* |
| 412 | * We have received a hotplug request for an interface that |
| 413 | * already exists. |
| 414 | * |
| 415 | * This can happen in cases like: |
| 416 | * - bootrom loading the firmware image and booting into that, |
| 417 | * which only generates a hotplug event. i.e. no hot-unplug |
| 418 | * event. |
| 419 | * - Or the firmware on the module crashed and sent hotplug |
| 420 | * request again to the SVC, which got propagated to AP. |
| 421 | * |
| 422 | * Remove the interface and add it again, and let user know |
| 423 | * about this with a print message. |
| 424 | */ |
| 425 | dev_info(dev, "Removed interface (%hhu) to add it again\n", |
| 426 | intf_id); |
| 427 | svc_intf_remove(connection, intf); |
| 428 | } |
| 429 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 430 | intf = gb_interface_create(hd, intf_id); |
| 431 | if (!intf) { |
| 432 | dev_err(dev, "%s: Failed to create interface with id %hhu\n", |
| 433 | __func__, intf_id); |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 434 | goto free_svc_hotplug; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 435 | } |
| 436 | |
Viresh Kumar | 6bec5c7 | 2015-09-24 14:40:29 -0700 | [diff] [blame^] | 437 | ret = gb_svc_read_and_clear_module_boot_status(intf); |
| 438 | if (ret) |
| 439 | goto destroy_interface; |
| 440 | |
Viresh Kumar | 3944a45 | 2015-08-12 09:19:31 +0530 | [diff] [blame] | 441 | intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id); |
| 442 | intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id); |
| 443 | intf->ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id); |
| 444 | intf->ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id); |
| 445 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 446 | /* |
| 447 | * Create a device id for the interface: |
| 448 | * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC |
| 449 | * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP |
| 450 | * |
| 451 | * XXX Do we need to allocate device ID for SVC or the AP here? And what |
| 452 | * XXX about an AP with multiple interface blocks? |
| 453 | */ |
Johan Hovold | c09db18 | 2015-09-15 09:18:08 +0200 | [diff] [blame] | 454 | device_id = ida_simple_get(&svc->device_id_map, |
Johan Hovold | 89f637f | 2015-09-01 12:25:25 +0200 | [diff] [blame] | 455 | GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 456 | if (device_id < 0) { |
| 457 | ret = device_id; |
| 458 | dev_err(dev, "%s: Failed to allocate device id for interface with id %hhu (%d)\n", |
| 459 | __func__, intf_id, ret); |
| 460 | goto destroy_interface; |
| 461 | } |
| 462 | |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 463 | ret = gb_svc_intf_device_id(svc, intf_id, device_id); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 464 | if (ret) { |
| 465 | dev_err(dev, "%s: Device id operation failed, interface %hhu device_id %hhu (%d)\n", |
| 466 | __func__, intf_id, device_id, ret); |
| 467 | goto ida_put; |
| 468 | } |
| 469 | |
Perry Hung | 7e27546 | 2015-07-24 19:02:32 -0400 | [diff] [blame] | 470 | /* |
| 471 | * Create a two-way route between the AP and the new interface |
| 472 | */ |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 473 | ret = gb_svc_route_create(svc, hd->endo->ap_intf_id, GB_DEVICE_ID_AP, |
| 474 | intf_id, device_id); |
Perry Hung | 7e27546 | 2015-07-24 19:02:32 -0400 | [diff] [blame] | 475 | if (ret) { |
| 476 | dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n", |
| 477 | __func__, intf_id, device_id, ret); |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 478 | goto svc_id_free; |
Perry Hung | 7e27546 | 2015-07-24 19:02:32 -0400 | [diff] [blame] | 479 | } |
| 480 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 481 | ret = gb_interface_init(intf, device_id); |
| 482 | if (ret) { |
| 483 | dev_err(dev, "%s: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n", |
| 484 | __func__, intf_id, device_id, ret); |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 485 | goto destroy_route; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 486 | } |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 487 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 488 | goto free_svc_hotplug; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 489 | |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 490 | destroy_route: |
| 491 | gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 492 | svc_id_free: |
| 493 | /* |
| 494 | * XXX Should we tell SVC that this id doesn't belong to interface |
| 495 | * XXX anymore. |
| 496 | */ |
| 497 | ida_put: |
Johan Hovold | c09db18 | 2015-09-15 09:18:08 +0200 | [diff] [blame] | 498 | ida_simple_remove(&svc->device_id_map, device_id); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 499 | destroy_interface: |
| 500 | gb_interface_remove(hd, intf_id); |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 501 | free_svc_hotplug: |
| 502 | kfree(svc_hotplug); |
| 503 | } |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 504 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 505 | /* |
| 506 | * Bringing up a module can be time consuming, as that may require lots of |
| 507 | * initialization on the module side. Over that, we may also need to download |
| 508 | * the firmware first and flash that on the module. |
| 509 | * |
| 510 | * In order to make other hotplug events to not wait for all this to finish, |
| 511 | * handle most of module hotplug stuff outside of the hotplug callback, with |
| 512 | * help of a workqueue. |
| 513 | */ |
| 514 | static int gb_svc_intf_hotplug_recv(struct gb_operation *op) |
| 515 | { |
| 516 | struct gb_message *request = op->request; |
| 517 | struct svc_hotplug *svc_hotplug; |
| 518 | |
| 519 | if (request->payload_size < sizeof(svc_hotplug->data)) { |
| 520 | dev_err(&op->connection->dev, |
| 521 | "%s: short hotplug request received (%zu < %zu)\n", |
| 522 | __func__, request->payload_size, |
| 523 | sizeof(svc_hotplug->data)); |
| 524 | return -EINVAL; |
| 525 | } |
| 526 | |
Johan Hovold | 287bba8 | 2015-09-01 12:25:26 +0200 | [diff] [blame] | 527 | svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL); |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 528 | if (!svc_hotplug) |
| 529 | return -ENOMEM; |
| 530 | |
| 531 | svc_hotplug->connection = op->connection; |
| 532 | memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data)); |
| 533 | |
| 534 | INIT_WORK(&svc_hotplug->work, svc_process_hotplug); |
| 535 | queue_work(system_unbound_wq, &svc_hotplug->work); |
| 536 | |
| 537 | return 0; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op) |
| 541 | { |
| 542 | struct gb_message *request = op->request; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 543 | struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload; |
Viresh Kumar | b9fb704 | 2015-09-01 17:16:16 +0530 | [diff] [blame] | 544 | struct greybus_host_device *hd = op->connection->hd; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 545 | struct device *dev = &op->connection->dev; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 546 | struct gb_interface *intf; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 547 | u8 intf_id; |
| 548 | |
| 549 | if (request->payload_size < sizeof(*hot_unplug)) { |
Viresh Kumar | c06307c | 2015-09-23 16:48:07 -0700 | [diff] [blame] | 550 | dev_err(dev, "short hot unplug request received (%zu < %zu)\n", |
Viresh Kumar | 6d05ad3 | 2015-08-06 12:44:54 +0530 | [diff] [blame] | 551 | request->payload_size, sizeof(*hot_unplug)); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 552 | return -EINVAL; |
| 553 | } |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 554 | |
| 555 | intf_id = hot_unplug->intf_id; |
| 556 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 557 | intf = gb_interface_find(hd, intf_id); |
| 558 | if (!intf) { |
| 559 | dev_err(dev, "%s: Couldn't find interface for id %hhu\n", |
| 560 | __func__, intf_id); |
| 561 | return -EINVAL; |
| 562 | } |
| 563 | |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 564 | svc_intf_remove(op->connection, intf); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 565 | |
| 566 | return 0; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static int gb_svc_intf_reset_recv(struct gb_operation *op) |
| 570 | { |
| 571 | struct gb_message *request = op->request; |
| 572 | struct gb_svc_intf_reset_request *reset; |
| 573 | u8 intf_id; |
| 574 | |
| 575 | if (request->payload_size < sizeof(*reset)) { |
| 576 | dev_err(&op->connection->dev, |
Viresh Kumar | 6d05ad3 | 2015-08-06 12:44:54 +0530 | [diff] [blame] | 577 | "short reset request received (%zu < %zu)\n", |
| 578 | request->payload_size, sizeof(*reset)); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 579 | return -EINVAL; |
| 580 | } |
| 581 | reset = request->payload; |
| 582 | |
| 583 | intf_id = reset->intf_id; |
| 584 | |
| 585 | /* FIXME Reset the interface here */ |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | static int gb_svc_request_recv(u8 type, struct gb_operation *op) |
| 591 | { |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 592 | struct gb_connection *connection = op->connection; |
| 593 | struct gb_svc *svc = connection->private; |
| 594 | int ret = 0; |
| 595 | |
| 596 | /* |
| 597 | * SVC requests need to follow a specific order (at least initially) and |
| 598 | * below code takes care of enforcing that. The expected order is: |
| 599 | * - PROTOCOL_VERSION |
| 600 | * - SVC_HELLO |
| 601 | * - Any other request, but the earlier two. |
| 602 | * |
| 603 | * Incoming requests are guaranteed to be serialized and so we don't |
| 604 | * need to protect 'state' for any races. |
| 605 | */ |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 606 | switch (type) { |
Viresh Kumar | 0e2462d | 2015-08-14 07:57:38 +0530 | [diff] [blame] | 607 | case GB_REQUEST_TYPE_PROTOCOL_VERSION: |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 608 | if (svc->state != GB_SVC_STATE_RESET) |
| 609 | ret = -EINVAL; |
| 610 | break; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 611 | case GB_SVC_TYPE_SVC_HELLO: |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 612 | if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION) |
| 613 | ret = -EINVAL; |
| 614 | break; |
| 615 | default: |
| 616 | if (svc->state != GB_SVC_STATE_SVC_HELLO) |
| 617 | ret = -EINVAL; |
| 618 | break; |
| 619 | } |
| 620 | |
| 621 | if (ret) { |
| 622 | dev_warn(&connection->dev, |
| 623 | "unexpected SVC request 0x%02x received (state %u)\n", |
| 624 | type, svc->state); |
| 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | switch (type) { |
| 629 | case GB_REQUEST_TYPE_PROTOCOL_VERSION: |
| 630 | ret = gb_svc_version_request(op); |
| 631 | if (!ret) |
| 632 | svc->state = GB_SVC_STATE_PROTOCOL_VERSION; |
| 633 | return ret; |
| 634 | case GB_SVC_TYPE_SVC_HELLO: |
| 635 | ret = gb_svc_hello(op); |
| 636 | if (!ret) |
| 637 | svc->state = GB_SVC_STATE_SVC_HELLO; |
| 638 | return ret; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 639 | case GB_SVC_TYPE_INTF_HOTPLUG: |
| 640 | return gb_svc_intf_hotplug_recv(op); |
| 641 | case GB_SVC_TYPE_INTF_HOT_UNPLUG: |
| 642 | return gb_svc_intf_hot_unplug_recv(op); |
| 643 | case GB_SVC_TYPE_INTF_RESET: |
| 644 | return gb_svc_intf_reset_recv(op); |
| 645 | default: |
| 646 | dev_err(&op->connection->dev, |
| 647 | "unsupported request: %hhu\n", type); |
| 648 | return -EINVAL; |
| 649 | } |
| 650 | } |
| 651 | |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 652 | static int gb_svc_connection_init(struct gb_connection *connection) |
| 653 | { |
| 654 | struct gb_svc *svc; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 655 | |
| 656 | svc = kzalloc(sizeof(*svc), GFP_KERNEL); |
| 657 | if (!svc) |
| 658 | return -ENOMEM; |
| 659 | |
Perry Hung | 75a60ed | 2015-07-24 19:02:33 -0400 | [diff] [blame] | 660 | connection->hd->svc = svc; |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 661 | svc->state = GB_SVC_STATE_RESET; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 662 | svc->connection = connection; |
| 663 | connection->private = svc; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 664 | |
Viresh Kumar | dcd0500 | 2015-07-24 15:32:20 +0530 | [diff] [blame] | 665 | WARN_ON(connection->hd->initial_svc_connection); |
| 666 | connection->hd->initial_svc_connection = connection; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 667 | |
Johan Hovold | c09db18 | 2015-09-15 09:18:08 +0200 | [diff] [blame] | 668 | ida_init(&svc->device_id_map); |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 669 | |
Viresh Kumar | 18d777c | 2015-07-21 17:44:20 +0530 | [diff] [blame] | 670 | return 0; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static void gb_svc_connection_exit(struct gb_connection *connection) |
| 674 | { |
| 675 | struct gb_svc *svc = connection->private; |
| 676 | |
Johan Hovold | c09db18 | 2015-09-15 09:18:08 +0200 | [diff] [blame] | 677 | ida_destroy(&svc->device_id_map); |
Perry Hung | 75a60ed | 2015-07-24 19:02:33 -0400 | [diff] [blame] | 678 | connection->hd->svc = NULL; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 679 | connection->private = NULL; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 680 | kfree(svc); |
| 681 | } |
| 682 | |
| 683 | static struct gb_protocol svc_protocol = { |
| 684 | .name = "svc", |
| 685 | .id = GREYBUS_PROTOCOL_SVC, |
Viresh Kumar | 06e305f | 2015-07-01 12:13:51 +0530 | [diff] [blame] | 686 | .major = GB_SVC_VERSION_MAJOR, |
| 687 | .minor = GB_SVC_VERSION_MINOR, |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 688 | .connection_init = gb_svc_connection_init, |
| 689 | .connection_exit = gb_svc_connection_exit, |
| 690 | .request_recv = gb_svc_request_recv, |
Viresh Kumar | 5a5296b | 2015-09-07 16:01:24 +0530 | [diff] [blame] | 691 | .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED | |
| 692 | GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED | |
| 693 | GB_PROTOCOL_NO_BUNDLE | |
| 694 | GB_PROTOCOL_SKIP_VERSION | |
| 695 | GB_PROTOCOL_SKIP_SVC_CONNECTION, |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 696 | }; |
Viresh Kumar | ab69c4c | 2015-07-03 17:00:29 +0530 | [diff] [blame] | 697 | gb_builtin_protocol_driver(svc_protocol); |