fw-download.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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, struct fw_request, dwork);
  113. struct fw_download *fw_download = fw_req->fw_download;
  114. dev_err(fw_download->parent,
  115. "Timed out waiting for fetch / release firmware requests: %u\n",
  116. fw_req->firmware_id);
  117. fw_req->timedout = true;
  118. free_firmware(fw_download, fw_req);
  119. }
  120. static int exceeds_release_timeout(struct fw_request *fw_req)
  121. {
  122. struct fw_download *fw_download = fw_req->fw_download;
  123. if (time_before(jiffies, fw_req->release_timeout_j))
  124. return 0;
  125. dev_err(fw_download->parent,
  126. "Firmware download didn't finish in time, abort: %d\n",
  127. fw_req->firmware_id);
  128. fw_req->timedout = true;
  129. free_firmware(fw_download, fw_req);
  130. return -ETIMEDOUT;
  131. }
  132. /* This returns path of the firmware blob on the disk */
  133. static struct fw_request *find_firmware(struct fw_download *fw_download,
  134. const char *tag)
  135. {
  136. struct gb_interface *intf = fw_download->connection->bundle->intf;
  137. struct fw_request *fw_req;
  138. int ret, req_count;
  139. fw_req = kzalloc(sizeof(*fw_req), GFP_KERNEL);
  140. if (!fw_req)
  141. return ERR_PTR(-ENOMEM);
  142. /* Allocate ids from 1 to 255 (u8-max), 0 is an invalid id */
  143. ret = ida_simple_get(&fw_download->id_map, 1, 256, GFP_KERNEL);
  144. if (ret < 0) {
  145. dev_err(fw_download->parent,
  146. "failed to allocate firmware id (%d)\n", ret);
  147. goto err_free_req;
  148. }
  149. fw_req->firmware_id = ret;
  150. snprintf(fw_req->name, sizeof(fw_req->name),
  151. FW_NAME_PREFIX "/*(DEBLOBBED)*/",
  152. intf->ddbl1_manufacturer_id, intf->ddbl1_product_id,
  153. intf->vendor_id, intf->product_id, tag);
  154. dev_info(fw_download->parent, "Requested firmware package '%s'\n",
  155. fw_req->name);
  156. ret = reject_firmware(&fw_req->fw, fw_req->name, fw_download->parent);
  157. if (ret) {
  158. dev_err(fw_download->parent,
  159. "firmware request failed for %s (%d)\n", fw_req->name,
  160. ret);
  161. goto err_free_id;
  162. }
  163. fw_req->fw_download = fw_download;
  164. kref_init(&fw_req->kref);
  165. mutex_lock(&fw_download->mutex);
  166. list_add(&fw_req->node, &fw_download->fw_requests);
  167. mutex_unlock(&fw_download->mutex);
  168. /* Timeout, in jiffies, within which firmware should get loaded */
  169. req_count = DIV_ROUND_UP(fw_req->fw->size, MIN_FETCH_SIZE);
  170. fw_req->release_timeout_j = jiffies + req_count * NEXT_REQ_TIMEOUT_J;
  171. INIT_DELAYED_WORK(&fw_req->dwork, fw_request_timedout);
  172. schedule_delayed_work(&fw_req->dwork, NEXT_REQ_TIMEOUT_J);
  173. return fw_req;
  174. err_free_id:
  175. ida_simple_remove(&fw_download->id_map, fw_req->firmware_id);
  176. err_free_req:
  177. kfree(fw_req);
  178. return ERR_PTR(ret);
  179. }
  180. static int fw_download_find_firmware(struct gb_operation *op)
  181. {
  182. struct gb_connection *connection = op->connection;
  183. struct fw_download *fw_download = gb_connection_get_data(connection);
  184. struct gb_fw_download_find_firmware_request *request;
  185. struct gb_fw_download_find_firmware_response *response;
  186. struct fw_request *fw_req;
  187. const char *tag;
  188. if (op->request->payload_size != sizeof(*request)) {
  189. dev_err(fw_download->parent,
  190. "illegal size of find firmware request (%zu != %zu)\n",
  191. op->request->payload_size, sizeof(*request));
  192. return -EINVAL;
  193. }
  194. request = op->request->payload;
  195. tag = (const char *)request->firmware_tag;
  196. /* firmware_tag must be null-terminated */
  197. if (strnlen(tag, GB_FIRMWARE_TAG_MAX_SIZE) == GB_FIRMWARE_TAG_MAX_SIZE) {
  198. dev_err(fw_download->parent,
  199. "firmware-tag is not null-terminated\n");
  200. return -EINVAL;
  201. }
  202. fw_req = find_firmware(fw_download, tag);
  203. if (IS_ERR(fw_req))
  204. return PTR_ERR(fw_req);
  205. if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL)) {
  206. dev_err(fw_download->parent, "error allocating response\n");
  207. free_firmware(fw_download, fw_req);
  208. return -ENOMEM;
  209. }
  210. response = op->response->payload;
  211. response->firmware_id = fw_req->firmware_id;
  212. response->size = cpu_to_le32(fw_req->fw->size);
  213. dev_dbg(fw_download->parent,
  214. "firmware size is %zu bytes\n", fw_req->fw->size);
  215. return 0;
  216. }
  217. static int fw_download_fetch_firmware(struct gb_operation *op)
  218. {
  219. struct gb_connection *connection = op->connection;
  220. struct fw_download *fw_download = gb_connection_get_data(connection);
  221. struct gb_fw_download_fetch_firmware_request *request;
  222. struct gb_fw_download_fetch_firmware_response *response;
  223. struct fw_request *fw_req;
  224. const struct firmware *fw;
  225. unsigned int offset, size;
  226. u8 firmware_id;
  227. int ret = 0;
  228. if (op->request->payload_size != sizeof(*request)) {
  229. dev_err(fw_download->parent,
  230. "Illegal size of fetch firmware request (%zu %zu)\n",
  231. op->request->payload_size, sizeof(*request));
  232. return -EINVAL;
  233. }
  234. request = op->request->payload;
  235. offset = le32_to_cpu(request->offset);
  236. size = le32_to_cpu(request->size);
  237. firmware_id = request->firmware_id;
  238. fw_req = get_fw_req(fw_download, firmware_id);
  239. if (!fw_req) {
  240. dev_err(fw_download->parent,
  241. "firmware not available for id: %02u\n", firmware_id);
  242. return -EINVAL;
  243. }
  244. /* Make sure work handler isn't running in parallel */
  245. cancel_delayed_work_sync(&fw_req->dwork);
  246. /* We timed-out before reaching here ? */
  247. if (fw_req->disabled) {
  248. ret = -ETIMEDOUT;
  249. goto put_fw;
  250. }
  251. /*
  252. * Firmware download must finish within a limited time interval. If it
  253. * doesn't, then we might have a buggy Module on the other side. Abort
  254. * download.
  255. */
  256. ret = exceeds_release_timeout(fw_req);
  257. if (ret)
  258. goto put_fw;
  259. fw = fw_req->fw;
  260. if (offset >= fw->size || size > fw->size - offset) {
  261. dev_err(fw_download->parent,
  262. "bad fetch firmware request (offs = %u, size = %u)\n",
  263. offset, size);
  264. ret = -EINVAL;
  265. goto put_fw;
  266. }
  267. if (!gb_operation_response_alloc(op, sizeof(*response) + size,
  268. GFP_KERNEL)) {
  269. dev_err(fw_download->parent,
  270. "error allocating fetch firmware response\n");
  271. ret = -ENOMEM;
  272. goto put_fw;
  273. }
  274. response = op->response->payload;
  275. memcpy(response->data, fw->data + offset, size);
  276. dev_dbg(fw_download->parent,
  277. "responding with firmware (offs = %u, size = %u)\n", offset,
  278. size);
  279. /* Refresh timeout */
  280. schedule_delayed_work(&fw_req->dwork, NEXT_REQ_TIMEOUT_J);
  281. put_fw:
  282. put_fw_req(fw_req);
  283. return ret;
  284. }
  285. static int fw_download_release_firmware(struct gb_operation *op)
  286. {
  287. struct gb_connection *connection = op->connection;
  288. struct fw_download *fw_download = gb_connection_get_data(connection);
  289. struct gb_fw_download_release_firmware_request *request;
  290. struct fw_request *fw_req;
  291. u8 firmware_id;
  292. if (op->request->payload_size != sizeof(*request)) {
  293. dev_err(fw_download->parent,
  294. "Illegal size of release firmware request (%zu %zu)\n",
  295. op->request->payload_size, sizeof(*request));
  296. return -EINVAL;
  297. }
  298. request = op->request->payload;
  299. firmware_id = request->firmware_id;
  300. fw_req = get_fw_req(fw_download, firmware_id);
  301. if (!fw_req) {
  302. dev_err(fw_download->parent,
  303. "firmware not available for id: %02u\n", firmware_id);
  304. return -EINVAL;
  305. }
  306. cancel_delayed_work_sync(&fw_req->dwork);
  307. free_firmware(fw_download, fw_req);
  308. put_fw_req(fw_req);
  309. dev_dbg(fw_download->parent, "release firmware\n");
  310. return 0;
  311. }
  312. int gb_fw_download_request_handler(struct gb_operation *op)
  313. {
  314. u8 type = op->type;
  315. switch (type) {
  316. case GB_FW_DOWNLOAD_TYPE_FIND_FIRMWARE:
  317. return fw_download_find_firmware(op);
  318. case GB_FW_DOWNLOAD_TYPE_FETCH_FIRMWARE:
  319. return fw_download_fetch_firmware(op);
  320. case GB_FW_DOWNLOAD_TYPE_RELEASE_FIRMWARE:
  321. return fw_download_release_firmware(op);
  322. default:
  323. dev_err(&op->connection->bundle->dev,
  324. "unsupported request: %u\n", type);
  325. return -EINVAL;
  326. }
  327. }
  328. int gb_fw_download_connection_init(struct gb_connection *connection)
  329. {
  330. struct fw_download *fw_download;
  331. int ret;
  332. if (!connection)
  333. return 0;
  334. fw_download = kzalloc(sizeof(*fw_download), GFP_KERNEL);
  335. if (!fw_download)
  336. return -ENOMEM;
  337. fw_download->parent = &connection->bundle->dev;
  338. INIT_LIST_HEAD(&fw_download->fw_requests);
  339. ida_init(&fw_download->id_map);
  340. gb_connection_set_data(connection, fw_download);
  341. fw_download->connection = connection;
  342. mutex_init(&fw_download->mutex);
  343. ret = gb_connection_enable(connection);
  344. if (ret)
  345. goto err_destroy_id_map;
  346. return 0;
  347. err_destroy_id_map:
  348. ida_destroy(&fw_download->id_map);
  349. kfree(fw_download);
  350. return ret;
  351. }
  352. void gb_fw_download_connection_exit(struct gb_connection *connection)
  353. {
  354. struct fw_download *fw_download;
  355. struct fw_request *fw_req, *tmp;
  356. if (!connection)
  357. return;
  358. fw_download = gb_connection_get_data(connection);
  359. gb_connection_disable(fw_download->connection);
  360. /*
  361. * Make sure we have a reference to the pending requests, before they
  362. * are freed from the timeout handler.
  363. */
  364. mutex_lock(&fw_download->mutex);
  365. list_for_each_entry(fw_req, &fw_download->fw_requests, node)
  366. kref_get(&fw_req->kref);
  367. mutex_unlock(&fw_download->mutex);
  368. /* Release pending firmware packages */
  369. list_for_each_entry_safe(fw_req, tmp, &fw_download->fw_requests, node) {
  370. cancel_delayed_work_sync(&fw_req->dwork);
  371. free_firmware(fw_download, fw_req);
  372. put_fw_req(fw_req);
  373. }
  374. ida_destroy(&fw_download->id_map);
  375. kfree(fw_download);
  376. }