io_block.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright (c) 2016-2023, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <platform_def.h>
  10. #include <common/debug.h>
  11. #include <drivers/io/io_block.h>
  12. #include <drivers/io/io_driver.h>
  13. #include <drivers/io/io_storage.h>
  14. #include <lib/utils.h>
  15. typedef struct {
  16. io_block_dev_spec_t *dev_spec;
  17. uintptr_t base;
  18. unsigned long long file_pos;
  19. unsigned long long size;
  20. } block_dev_state_t;
  21. #define is_power_of_2(x) (((x) != 0U) && (((x) & ((x) - 1U)) == 0U))
  22. io_type_t device_type_block(void);
  23. static int block_open(io_dev_info_t *dev_info, const uintptr_t spec,
  24. io_entity_t *entity);
  25. static int block_seek(io_entity_t *entity, int mode, signed long long offset);
  26. static int block_read(io_entity_t *entity, uintptr_t buffer, size_t length,
  27. size_t *length_read);
  28. static int block_write(io_entity_t *entity, const uintptr_t buffer,
  29. size_t length, size_t *length_written);
  30. static int block_close(io_entity_t *entity);
  31. static int block_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
  32. static int block_dev_close(io_dev_info_t *dev_info);
  33. static const io_dev_connector_t block_dev_connector = {
  34. .dev_open = block_dev_open
  35. };
  36. static const io_dev_funcs_t block_dev_funcs = {
  37. .type = device_type_block,
  38. .open = block_open,
  39. .seek = block_seek,
  40. .size = NULL,
  41. .read = block_read,
  42. .write = block_write,
  43. .close = block_close,
  44. .dev_init = NULL,
  45. .dev_close = block_dev_close,
  46. };
  47. static block_dev_state_t state_pool[MAX_IO_BLOCK_DEVICES];
  48. static io_dev_info_t dev_info_pool[MAX_IO_BLOCK_DEVICES];
  49. /* Track number of allocated block state */
  50. static unsigned int block_dev_count;
  51. io_type_t device_type_block(void)
  52. {
  53. return IO_TYPE_BLOCK;
  54. }
  55. /* Locate a block state in the pool, specified by address */
  56. static int find_first_block_state(const io_block_dev_spec_t *dev_spec,
  57. unsigned int *index_out)
  58. {
  59. unsigned int index;
  60. int result = -ENOENT;
  61. for (index = 0U; index < MAX_IO_BLOCK_DEVICES; ++index) {
  62. /* dev_spec is used as identifier since it's unique */
  63. if (state_pool[index].dev_spec == dev_spec) {
  64. result = 0;
  65. *index_out = index;
  66. break;
  67. }
  68. }
  69. return result;
  70. }
  71. /* Allocate a device info from the pool and return a pointer to it */
  72. static int allocate_dev_info(io_dev_info_t **dev_info)
  73. {
  74. int result = -ENOMEM;
  75. assert(dev_info != NULL);
  76. if (block_dev_count < MAX_IO_BLOCK_DEVICES) {
  77. unsigned int index = 0;
  78. result = find_first_block_state(NULL, &index);
  79. assert(result == 0);
  80. /* initialize dev_info */
  81. dev_info_pool[index].funcs = &block_dev_funcs;
  82. dev_info_pool[index].info = (uintptr_t)&state_pool[index];
  83. *dev_info = &dev_info_pool[index];
  84. ++block_dev_count;
  85. }
  86. return result;
  87. }
  88. /* Release a device info to the pool */
  89. static int free_dev_info(io_dev_info_t *dev_info)
  90. {
  91. int result;
  92. unsigned int index = 0;
  93. block_dev_state_t *state;
  94. assert(dev_info != NULL);
  95. state = (block_dev_state_t *)dev_info->info;
  96. result = find_first_block_state(state->dev_spec, &index);
  97. if (result == 0) {
  98. /* free if device info is valid */
  99. zeromem(state, sizeof(block_dev_state_t));
  100. zeromem(dev_info, sizeof(io_dev_info_t));
  101. --block_dev_count;
  102. }
  103. return result;
  104. }
  105. static int block_open(io_dev_info_t *dev_info, const uintptr_t spec,
  106. io_entity_t *entity)
  107. {
  108. block_dev_state_t *cur;
  109. io_block_spec_t *region;
  110. assert((dev_info->info != (uintptr_t)NULL) &&
  111. (spec != (uintptr_t)NULL) &&
  112. (entity->info == (uintptr_t)NULL));
  113. region = (io_block_spec_t *)spec;
  114. cur = (block_dev_state_t *)dev_info->info;
  115. assert(((region->offset % cur->dev_spec->block_size) == 0) &&
  116. ((region->length % cur->dev_spec->block_size) == 0));
  117. cur->base = region->offset;
  118. cur->size = region->length;
  119. cur->file_pos = 0;
  120. entity->info = (uintptr_t)cur;
  121. return 0;
  122. }
  123. /* parameter offset is relative address at here */
  124. static int block_seek(io_entity_t *entity, int mode, signed long long offset)
  125. {
  126. block_dev_state_t *cur;
  127. assert(entity->info != (uintptr_t)NULL);
  128. cur = (block_dev_state_t *)entity->info;
  129. assert((offset >= 0) && ((unsigned long long)offset < cur->size));
  130. switch (mode) {
  131. case IO_SEEK_SET:
  132. cur->file_pos = (unsigned long long)offset;
  133. break;
  134. case IO_SEEK_CUR:
  135. cur->file_pos += (unsigned long long)offset;
  136. break;
  137. default:
  138. return -EINVAL;
  139. }
  140. assert(cur->file_pos < cur->size);
  141. return 0;
  142. }
  143. /*
  144. * This function allows the caller to read any number of bytes
  145. * from any position. It hides from the caller that the low level
  146. * driver only can read aligned blocks of data. For this reason
  147. * we need to handle the use case where the first byte to be read is not
  148. * aligned to start of the block, the last byte to be read is also not
  149. * aligned to the end of a block, and there are zero or more blocks-worth
  150. * of data in between.
  151. *
  152. * In such a case we need to read more bytes than requested (i.e. full
  153. * blocks) and strip-out the leading bytes (aka skip) and the trailing
  154. * bytes (aka padding). See diagram below
  155. *
  156. * cur->file_pos ------------
  157. * |
  158. * cur->base |
  159. * | |
  160. * v v<---- length ---->
  161. * --------------------------------------------------------------
  162. * | | block#1 | | block#n |
  163. * | block#0 | + | ... | + |
  164. * | | <- skip -> + | | + <- padding ->|
  165. * ------------------------+----------------------+--------------
  166. * ^ ^
  167. * | |
  168. * v iteration#1 iteration#n v
  169. * --------------------------------------------------
  170. * | | | |
  171. * |<---- request ---->| ... |<----- request ---->|
  172. * | | | |
  173. * --------------------------------------------------
  174. * / / | |
  175. * / / | |
  176. * / / | |
  177. * / / | |
  178. * / / | |
  179. * / / | |
  180. * / / | |
  181. * / / | |
  182. * / / | |
  183. * / / | |
  184. * <---- request ------> <------ request ----->
  185. * --------------------- -----------------------
  186. * | | | | | |
  187. * |<-skip->|<-nbytes->| -------->|<-nbytes->|<-padding->|
  188. * | | | | | | |
  189. * --------------------- | -----------------------
  190. * ^ \ \ | | |
  191. * | \ \ | | |
  192. * | \ \ | | |
  193. * buf->offset \ \ buf->offset | |
  194. * \ \ | |
  195. * \ \ | |
  196. * \ \ | |
  197. * \ \ | |
  198. * \ \ | |
  199. * \ \ | |
  200. * \ \ | |
  201. * --------------------------------
  202. * | | | |
  203. * buffer-------------->| | ... | |
  204. * | | | |
  205. * --------------------------------
  206. * <-count#1->| |
  207. * <---------- count#n -------->
  208. * <---------- length ---------->
  209. *
  210. * Additionally, the IO driver has an underlying buffer that is at least
  211. * one block-size and may be big enough to allow.
  212. */
  213. static int block_read(io_entity_t *entity, uintptr_t buffer, size_t length,
  214. size_t *length_read)
  215. {
  216. block_dev_state_t *cur;
  217. io_block_spec_t *buf;
  218. io_block_ops_t *ops;
  219. int lba;
  220. size_t block_size, left;
  221. size_t nbytes; /* number of bytes read in one iteration */
  222. size_t request; /* number of requested bytes in one iteration */
  223. size_t count; /* number of bytes already read */
  224. /*
  225. * number of leading bytes from start of the block
  226. * to the first byte to be read
  227. */
  228. size_t skip;
  229. /*
  230. * number of trailing bytes between the last byte
  231. * to be read and the end of the block
  232. */
  233. size_t padding;
  234. assert(entity->info != (uintptr_t)NULL);
  235. cur = (block_dev_state_t *)entity->info;
  236. ops = &(cur->dev_spec->ops);
  237. buf = &(cur->dev_spec->buffer);
  238. block_size = cur->dev_spec->block_size;
  239. assert((length <= cur->size) &&
  240. (length > 0U) &&
  241. (ops->read != NULL));
  242. /*
  243. * We don't know the number of bytes that we are going
  244. * to read in every iteration, because it will depend
  245. * on the low level driver.
  246. */
  247. count = 0;
  248. for (left = length; left > 0U; left -= nbytes) {
  249. /*
  250. * We must only request operations aligned to the block
  251. * size. Therefore if file_pos is not block-aligned,
  252. * we have to request the operation to start at the
  253. * previous block boundary and skip the leading bytes. And
  254. * similarly, the number of bytes requested must be a
  255. * block size multiple
  256. */
  257. skip = cur->file_pos & (block_size - 1U);
  258. /*
  259. * Calculate the block number containing file_pos
  260. * - e.g. block 3.
  261. */
  262. lba = (cur->file_pos + cur->base) / block_size;
  263. if ((skip + left) > buf->length) {
  264. /*
  265. * The underlying read buffer is too small to
  266. * read all the required data - limit to just
  267. * fill the buffer, and then read again.
  268. */
  269. request = buf->length;
  270. } else {
  271. /*
  272. * The underlying read buffer is big enough to
  273. * read all the required data. Calculate the
  274. * number of bytes to read to align with the
  275. * block size.
  276. */
  277. request = skip + left;
  278. request = (request + (block_size - 1U)) &
  279. ~(block_size - 1U);
  280. }
  281. request = ops->read(lba, buf->offset, request);
  282. if (request <= skip) {
  283. /*
  284. * We couldn't read enough bytes to jump over
  285. * the skip bytes, so we should have to read
  286. * again the same block, thus generating
  287. * the same error.
  288. */
  289. return -EIO;
  290. }
  291. /*
  292. * Need to remove skip and padding bytes,if any, from
  293. * the read data when copying to the user buffer.
  294. */
  295. nbytes = request - skip;
  296. padding = (nbytes > left) ? nbytes - left : 0U;
  297. nbytes -= padding;
  298. memcpy((void *)(buffer + count),
  299. (void *)(buf->offset + skip),
  300. nbytes);
  301. cur->file_pos += nbytes;
  302. count += nbytes;
  303. }
  304. assert(count == length);
  305. *length_read = count;
  306. return 0;
  307. }
  308. /*
  309. * This function allows the caller to write any number of bytes
  310. * from any position. It hides from the caller that the low level
  311. * driver only can write aligned blocks of data.
  312. * See comments for block_read for more details.
  313. */
  314. static int block_write(io_entity_t *entity, const uintptr_t buffer,
  315. size_t length, size_t *length_written)
  316. {
  317. block_dev_state_t *cur;
  318. io_block_spec_t *buf;
  319. io_block_ops_t *ops;
  320. int lba;
  321. size_t block_size, left;
  322. size_t nbytes; /* number of bytes read in one iteration */
  323. size_t request; /* number of requested bytes in one iteration */
  324. size_t count; /* number of bytes already read */
  325. /*
  326. * number of leading bytes from start of the block
  327. * to the first byte to be read
  328. */
  329. size_t skip;
  330. /*
  331. * number of trailing bytes between the last byte
  332. * to be read and the end of the block
  333. */
  334. size_t padding;
  335. assert(entity->info != (uintptr_t)NULL);
  336. cur = (block_dev_state_t *)entity->info;
  337. ops = &(cur->dev_spec->ops);
  338. buf = &(cur->dev_spec->buffer);
  339. block_size = cur->dev_spec->block_size;
  340. assert((length <= cur->size) &&
  341. (length > 0U) &&
  342. (ops->read != NULL) &&
  343. (ops->write != NULL));
  344. /*
  345. * We don't know the number of bytes that we are going
  346. * to write in every iteration, because it will depend
  347. * on the low level driver.
  348. */
  349. count = 0;
  350. for (left = length; left > 0U; left -= nbytes) {
  351. /*
  352. * We must only request operations aligned to the block
  353. * size. Therefore if file_pos is not block-aligned,
  354. * we have to request the operation to start at the
  355. * previous block boundary and skip the leading bytes. And
  356. * similarly, the number of bytes requested must be a
  357. * block size multiple
  358. */
  359. skip = cur->file_pos & (block_size - 1U);
  360. /*
  361. * Calculate the block number containing file_pos
  362. * - e.g. block 3.
  363. */
  364. lba = (cur->file_pos + cur->base) / block_size;
  365. if ((skip + left) > buf->length) {
  366. /*
  367. * The underlying read buffer is too small to
  368. * read all the required data - limit to just
  369. * fill the buffer, and then read again.
  370. */
  371. request = buf->length;
  372. } else {
  373. /*
  374. * The underlying read buffer is big enough to
  375. * read all the required data. Calculate the
  376. * number of bytes to read to align with the
  377. * block size.
  378. */
  379. request = skip + left;
  380. request = (request + (block_size - 1U)) &
  381. ~(block_size - 1U);
  382. }
  383. /*
  384. * The number of bytes that we are going to write
  385. * from the user buffer will depend of the size
  386. * of the current request.
  387. */
  388. nbytes = request - skip;
  389. padding = (nbytes > left) ? nbytes - left : 0U;
  390. nbytes -= padding;
  391. /*
  392. * If we have skip or padding bytes then we have to preserve
  393. * some content and it means that we have to read before
  394. * writing
  395. */
  396. if ((skip > 0U) || (padding > 0U)) {
  397. request = ops->read(lba, buf->offset, request);
  398. /*
  399. * The read may return size less than
  400. * requested. Round down to the nearest block
  401. * boundary
  402. */
  403. request &= ~(block_size - 1U);
  404. if (request <= skip) {
  405. /*
  406. * We couldn't read enough bytes to jump over
  407. * the skip bytes, so we should have to read
  408. * again the same block, thus generating
  409. * the same error.
  410. */
  411. return -EIO;
  412. }
  413. nbytes = request - skip;
  414. padding = (nbytes > left) ? nbytes - left : 0U;
  415. nbytes -= padding;
  416. }
  417. memcpy((void *)(buf->offset + skip),
  418. (void *)(buffer + count),
  419. nbytes);
  420. request = ops->write(lba, buf->offset, request);
  421. if (request <= skip)
  422. return -EIO;
  423. /*
  424. * And the previous write operation may modify the size
  425. * of the request, so again, we have to calculate the
  426. * number of bytes that we consumed from the user
  427. * buffer
  428. */
  429. nbytes = request - skip;
  430. padding = (nbytes > left) ? nbytes - left : 0U;
  431. nbytes -= padding;
  432. cur->file_pos += nbytes;
  433. count += nbytes;
  434. }
  435. assert(count == length);
  436. *length_written = count;
  437. return 0;
  438. }
  439. static int block_close(io_entity_t *entity)
  440. {
  441. entity->info = (uintptr_t)NULL;
  442. return 0;
  443. }
  444. static int block_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info)
  445. {
  446. block_dev_state_t *cur;
  447. io_block_spec_t *buffer;
  448. io_dev_info_t *info;
  449. size_t block_size;
  450. int result;
  451. assert(dev_info != NULL);
  452. result = allocate_dev_info(&info);
  453. if (result != 0)
  454. return -ENOENT;
  455. cur = (block_dev_state_t *)info->info;
  456. /* dev_spec is type of io_block_dev_spec_t. */
  457. cur->dev_spec = (io_block_dev_spec_t *)dev_spec;
  458. buffer = &(cur->dev_spec->buffer);
  459. block_size = cur->dev_spec->block_size;
  460. assert((block_size > 0U) &&
  461. (is_power_of_2(block_size) != 0U) &&
  462. ((buffer->offset % block_size) == 0U) &&
  463. ((buffer->length % block_size) == 0U));
  464. *dev_info = info; /* cast away const */
  465. (void)block_size;
  466. (void)buffer;
  467. return 0;
  468. }
  469. static int block_dev_close(io_dev_info_t *dev_info)
  470. {
  471. return free_dev_info(dev_info);
  472. }
  473. /* Exported functions */
  474. /* Register the Block driver with the IO abstraction */
  475. int register_io_dev_block(const io_dev_connector_t **dev_con)
  476. {
  477. int result;
  478. assert(dev_con != NULL);
  479. /*
  480. * Since dev_info isn't really used in io_register_device, always
  481. * use the same device info at here instead.
  482. */
  483. result = io_register_device(&dev_info_pool[0]);
  484. if (result == 0)
  485. *dev_con = &block_dev_connector;
  486. return result;
  487. }