PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/staging/greybus/fw-download.c

https://github.com/gby/linux
C | 467 lines | 322 code | 89 blank | 56 comment | 29 complexity | 855f622e5fe16c9dda0a2fe05cc16ea8 MD5 | raw file
  1. /*
  2. * Greybus Firmware Download Protocol Driver.
  3. *
  4. * Copyright 2016 Google Inc.
  5. * Copyright 2016 Linaro Ltd.
  6. *
  7. * Released under the GPLv2 only.
  8. */
  9. #include <linux/firmware.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/mutex.h>
  12. #include <linux/workqueue.h>
  13. #include "firmware.h"
  14. #include "greybus.h"
  15. /* Estimated minimum buffer size, actual size can be smaller than this */
  16. #define MIN_FETCH_SIZE 512
  17. /* Timeout, in jiffies, within which fetch or release firmware must be called */
  18. #define NEXT_REQ_TIMEOUT_J msecs_to_jiffies(1000)
  19. struct fw_request {
  20. u8 firmware_id;
  21. bool disabled;
  22. bool timedout;
  23. char name[FW_NAME_SIZE];
  24. const struct firmware *fw;
  25. struct list_head node;
  26. struct delayed_work dwork;
  27. /* Timeout, in jiffies, within which the firmware shall download */
  28. unsigned long release_timeout_j;
  29. struct kref kref;
  30. struct fw_download *fw_download;
  31. };
  32. struct fw_download {
  33. struct device *parent;
  34. struct gb_connection *connection;
  35. struct list_head fw_requests;
  36. struct ida id_map;
  37. struct mutex mutex;
  38. };
  39. static void fw_req_release(struct kref *kref)
  40. {
  41. struct fw_request *fw_req = container_of(kref, struct fw_request, kref);
  42. dev_dbg(fw_req->fw_download->parent, "firmware %s released\n",
  43. fw_req->name);
  44. release_firmware(fw_req->fw);
  45. /*
  46. * The request timed out and the module may send a fetch-fw or
  47. * release-fw request later. Lets block the id we allocated for this
  48. * request, so that the AP doesn't refer to a later fw-request (with
  49. * same firmware_id) for the old timedout fw-request.
  50. *
  51. * NOTE:
  52. *
  53. * This also means that after 255 timeouts we will fail to service new
  54. * firmware downloads. But what else can we do in that case anyway? Lets
  55. * just hope that it never happens.
  56. */
  57. if (!fw_req->timedout)
  58. ida_simple_remove(&fw_req->fw_download->id_map,
  59. fw_req->firmware_id);
  60. kfree(fw_req);
  61. }
  62. /*
  63. * Incoming requests are serialized for a connection, and the only race possible
  64. * is between the timeout handler freeing this and an incoming request.
  65. *
  66. * The operations on the fw-request list are protected by the mutex and
  67. * get_fw_req() increments the reference count before returning a fw_req pointer
  68. * to the users.
  69. *
  70. * free_firmware() also takes the mutex while removing an entry from the list,
  71. * it guarantees that every user of fw_req has taken a kref-reference by now and
  72. * we wouldn't have any new users.
  73. *
  74. * Once the last user drops the reference, the fw_req structure is freed.
  75. */
  76. static void put_fw_req(struct fw_request *fw_req)
  77. {
  78. kref_put(&fw_req->kref, fw_req_release);
  79. }
  80. /* Caller must call put_fw_req() after using struct fw_request */
  81. static struct fw_request *get_fw_req(struct fw_download *fw_download,
  82. u8 firmware_id)
  83. {
  84. struct fw_request *fw_req;
  85. mutex_lock(&fw_download->mutex);
  86. list_for_each_entry(fw_req, &fw_download->fw_requests, node) {
  87. if (fw_req->firmware_id == firmware_id) {
  88. kref_get(&fw_req->kref);
  89. goto unlock;
  90. }
  91. }
  92. fw_req = NULL;
  93. unlock:
  94. mutex_unlock(&fw_download->mutex);
  95. return fw_req;
  96. }
  97. static void free_firmware(struct fw_download *fw_download,
  98. struct fw_request *fw_req)
  99. {
  100. /* Already disabled from timeout handlers */
  101. if (fw_req->disabled)
  102. return;
  103. mutex_lock(&fw_download->mutex);
  104. list_del(&fw_req->node);
  105. mutex_unlock(&fw_download->mutex);
  106. fw_req->disabled = true;
  107. put_fw_req(fw_req);
  108. }
  109. static void fw_request_timedout(struct work_struct *work)
  110. {
  111. struct delayed_work *dwork = to_delayed_work(work);
  112. struct fw_request *fw_req = container_of(dwork,
  113. struct fw_request, dwork);
  114. struct fw_download *fw_download = fw_req->fw_download;
  115. dev_err(fw_download->parent,
  116. "Timed out waiting for fetch / release firmware requests: %u\n",
  117. fw_req->firmware_id);
  118. fw_req->timedout = true;
  119. free_firmware(fw_download, fw_req);
  120. }
  121. static int exceeds_release_timeout(struct fw_request *fw_req)
  122. {
  123. struct fw_download *fw_download = fw_req->fw_download;
  124. if (time_before(jiffies, fw_req->release_timeout_j))
  125. return 0;
  126. dev_err(fw_download->parent,
  127. "Firmware download didn't finish in time, abort: %d\n",
  128. fw_req->firmware_id);
  129. fw_req->timedout = true;
  130. free_firmware(fw_download, fw_req);
  131. return -ETIMEDOUT;
  132. }
  133. /* This returns path of the firmware blob on the disk */
  134. static struct fw_request *find_firmware(struct fw_download *fw_download,
  135. const char *tag)
  136. {
  137. struct gb_interface *intf = fw_download->connection->bundle->intf;
  138. struct fw_request *fw_req;
  139. int ret, req_count;
  140. fw_req = kzalloc(sizeof(*fw_req), GFP_KERNEL);
  141. if (!fw_req)
  142. return ERR_PTR(-ENOMEM);
  143. /* Allocate ids from 1 to 255 (u8-max), 0 is an invalid id */
  144. ret = ida_simple_get(&fw_download->id_map, 1, 256, GFP_KERNEL);
  145. if (ret < 0) {
  146. dev_err(fw_download->parent,
  147. "failed to allocate firmware id (%d)\n", ret);
  148. goto err_free_req;
  149. }
  150. fw_req->firmware_id = ret;
  151. snprintf(fw_req->name, sizeof(fw_req->name),
  152. FW_NAME_PREFIX "%08x_%08x_%08x_%08x_%s.tftf",
  153. intf->ddbl1_manufacturer_id, intf->ddbl1_product_id,
  154. intf->vendor_id, intf->product_id, tag);
  155. dev_info(fw_download->parent, "Requested firmware package '%s'\n",
  156. fw_req->name);
  157. ret = request_firmware(&fw_req->fw, fw_req->name, fw_download->parent);
  158. if (ret) {
  159. dev_err(fw_download->parent,
  160. "firmware request failed for %s (%d)\n", fw_req->name,
  161. ret);
  162. goto err_free_id;
  163. }
  164. fw_req->fw_download = fw_download;
  165. kref_init(&fw_req->kref);
  166. mutex_lock(&fw_download->mutex);
  167. list_add(&fw_req->node, &fw_download->fw_requests);
  168. mutex_unlock(&fw_download->mutex);
  169. /* Timeout, in jiffies, within which firmware should get loaded */
  170. req_count = DIV_ROUND_UP(fw_req->fw->size, MIN_FETCH_SIZE);
  171. fw_req->release_timeout_j = jiffies + req_count * NEXT_REQ_TIMEOUT_J;
  172. INIT_DELAYED_WORK(&fw_req->dwork, fw_request_timedout);
  173. schedule_delayed_work(&fw_req->dwork, NEXT_REQ_TIMEOUT_J);
  174. return fw_req;
  175. err_free_id:
  176. ida_simple_remove(&fw_download->id_map, fw_req->firmware_id);
  177. err_free_req:
  178. kfree(fw_req);
  179. return ERR_PTR(ret);
  180. }
  181. static int fw_download_find_firmware(struct gb_operation *op)
  182. {
  183. struct gb_connection *connection = op->connection;
  184. struct fw_download *fw_download = gb_connection_get_data(connection);
  185. struct gb_fw_download_find_firmware_request *request;
  186. struct gb_fw_download_find_firmware_response *response;
  187. struct fw_request *fw_req;
  188. const char *tag;
  189. if (op->request->payload_size != sizeof(*request)) {
  190. dev_err(fw_download->parent,
  191. "illegal size of find firmware request (%zu != %zu)\n",
  192. op->request->payload_size, sizeof(*request));
  193. return -EINVAL;
  194. }
  195. request = op->request->payload;
  196. tag = (const char *)request->firmware_tag;
  197. /* firmware_tag must be null-terminated */
  198. if (strnlen(tag, GB_FIRMWARE_TAG_MAX_SIZE) ==
  199. GB_FIRMWARE_TAG_MAX_SIZE) {
  200. dev_err(fw_download->parent,
  201. "firmware-tag is not null-terminated\n");
  202. return -EINVAL;
  203. }
  204. fw_req = find_firmware(fw_download, tag);
  205. if (IS_ERR(fw_req))
  206. return PTR_ERR(fw_req);
  207. if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL)) {
  208. dev_err(fw_download->parent, "error allocating response\n");
  209. free_firmware(fw_download, fw_req);
  210. return -ENOMEM;
  211. }
  212. response = op->response->payload;
  213. response->firmware_id = fw_req->firmware_id;
  214. response->size = cpu_to_le32(fw_req->fw->size);
  215. dev_dbg(fw_download->parent,
  216. "firmware size is %zu bytes\n", fw_req->fw->size);
  217. return 0;
  218. }
  219. static int fw_download_fetch_firmware(struct gb_operation *op)
  220. {
  221. struct gb_connection *connection = op->connection;
  222. struct fw_download *fw_download = gb_connection_get_data(connection);
  223. struct gb_fw_download_fetch_firmware_request *request;
  224. struct gb_fw_download_fetch_firmware_response *response;
  225. struct fw_request *fw_req;
  226. const struct firmware *fw;
  227. unsigned int offset, size;
  228. u8 firmware_id;
  229. int ret = 0;
  230. if (op->request->payload_size != sizeof(*request)) {
  231. dev_err(fw_download->parent,
  232. "Illegal size of fetch firmware request (%zu %zu)\n",
  233. op->request->payload_size, sizeof(*request));
  234. return -EINVAL;
  235. }
  236. request = op->request->payload;
  237. offset = le32_to_cpu(request->offset);
  238. size = le32_to_cpu(request->size);
  239. firmware_id = request->firmware_id;
  240. fw_req = get_fw_req(fw_download, firmware_id);
  241. if (!fw_req) {
  242. dev_err(fw_download->parent,
  243. "firmware not available for id: %02u\n", firmware_id);
  244. return -EINVAL;
  245. }
  246. /* Make sure work handler isn't running in parallel */
  247. cancel_delayed_work_sync(&fw_req->dwork);
  248. /* We timed-out before reaching here ? */
  249. if (fw_req->disabled) {
  250. ret = -ETIMEDOUT;
  251. goto put_fw;
  252. }
  253. /*
  254. * Firmware download must finish within a limited time interval. If it
  255. * doesn't, then we might have a buggy Module on the other side. Abort
  256. * download.
  257. */
  258. ret = exceeds_release_timeout(fw_req);
  259. if (ret)
  260. goto put_fw;
  261. fw = fw_req->fw;
  262. if (offset >= fw->size || size > fw->size - offset) {
  263. dev_err(fw_download->parent,
  264. "bad fetch firmware request (offs = %u, size = %u)\n",
  265. offset, size);
  266. ret = -EINVAL;
  267. goto put_fw;
  268. }
  269. if (!gb_operation_response_alloc(op, sizeof(*response) + size,
  270. GFP_KERNEL)) {
  271. dev_err(fw_download->parent,
  272. "error allocating fetch firmware response\n");
  273. ret = -ENOMEM;
  274. goto put_fw;
  275. }
  276. response = op->response->payload;
  277. memcpy(response->data, fw->data + offset, size);
  278. dev_dbg(fw_download->parent,
  279. "responding with firmware (offs = %u, size = %u)\n", offset,
  280. size);
  281. /* Refresh timeout */
  282. schedule_delayed_work(&fw_req->dwork, NEXT_REQ_TIMEOUT_J);
  283. put_fw:
  284. put_fw_req(fw_req);
  285. return ret;
  286. }
  287. static int fw_download_release_firmware(struct gb_operation *op)
  288. {
  289. struct gb_connection *connection = op->connection;
  290. struct fw_download *fw_download = gb_connection_get_data(connection);
  291. struct gb_fw_download_release_firmware_request *request;
  292. struct fw_request *fw_req;
  293. u8 firmware_id;
  294. if (op->request->payload_size != sizeof(*request)) {
  295. dev_err(fw_download->parent,
  296. "Illegal size of release firmware request (%zu %zu)\n",
  297. op->request->payload_size, sizeof(*request));
  298. return -EINVAL;
  299. }
  300. request = op->request->payload;
  301. firmware_id = request->firmware_id;
  302. fw_req = get_fw_req(fw_download, firmware_id);
  303. if (!fw_req) {
  304. dev_err(fw_download->parent,
  305. "firmware not available for id: %02u\n", firmware_id);
  306. return -EINVAL;
  307. }
  308. cancel_delayed_work_sync(&fw_req->dwork);
  309. free_firmware(fw_download, fw_req);
  310. put_fw_req(fw_req);
  311. dev_dbg(fw_download->parent, "release firmware\n");
  312. return 0;
  313. }
  314. int gb_fw_download_request_handler(struct gb_operation *op)
  315. {
  316. u8 type = op->type;
  317. switch (type) {
  318. case GB_FW_DOWNLOAD_TYPE_FIND_FIRMWARE:
  319. return fw_download_find_firmware(op);
  320. case GB_FW_DOWNLOAD_TYPE_FETCH_FIRMWARE:
  321. return fw_download_fetch_firmware(op);
  322. case GB_FW_DOWNLOAD_TYPE_RELEASE_FIRMWARE:
  323. return fw_download_release_firmware(op);
  324. default:
  325. dev_err(&op->connection->bundle->dev,
  326. "unsupported request: %u\n", type);
  327. return -EINVAL;
  328. }
  329. }
  330. int gb_fw_download_connection_init(struct gb_connection *connection)
  331. {
  332. struct fw_download *fw_download;
  333. int ret;
  334. if (!connection)
  335. return 0;
  336. fw_download = kzalloc(sizeof(*fw_download), GFP_KERNEL);
  337. if (!fw_download)
  338. return -ENOMEM;
  339. fw_download->parent = &connection->bundle->dev;
  340. INIT_LIST_HEAD(&fw_download->fw_requests);
  341. ida_init(&fw_download->id_map);
  342. gb_connection_set_data(connection, fw_download);
  343. fw_download->connection = connection;
  344. mutex_init(&fw_download->mutex);
  345. ret = gb_connection_enable(connection);
  346. if (ret)
  347. goto err_destroy_id_map;
  348. return 0;
  349. err_destroy_id_map:
  350. ida_destroy(&fw_download->id_map);
  351. kfree(fw_download);
  352. return ret;
  353. }
  354. void gb_fw_download_connection_exit(struct gb_connection *connection)
  355. {
  356. struct fw_download *fw_download;
  357. struct fw_request *fw_req, *tmp;
  358. if (!connection)
  359. return;
  360. fw_download = gb_connection_get_data(connection);
  361. gb_connection_disable(fw_download->connection);
  362. /*
  363. * Make sure we have a reference to the pending requests, before they
  364. * are freed from the timeout handler.
  365. */
  366. mutex_lock(&fw_download->mutex);
  367. list_for_each_entry(fw_req, &fw_download->fw_requests, node)
  368. kref_get(&fw_req->kref);
  369. mutex_unlock(&fw_download->mutex);
  370. /* Release pending firmware packages */
  371. list_for_each_entry_safe(fw_req, tmp, &fw_download->fw_requests, node) {
  372. cancel_delayed_work_sync(&fw_req->dwork);
  373. free_firmware(fw_download, fw_req);
  374. put_fw_req(fw_req);
  375. }
  376. ida_destroy(&fw_download->id_map);
  377. kfree(fw_download);
  378. }