You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iobuf.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <strings.h>
  22. #include <errno.h>
  23. #include <ipxe/malloc.h>
  24. #include <ipxe/iobuf.h>
  25. /** @file
  26. *
  27. * I/O buffers
  28. *
  29. */
  30. /**
  31. * Allocate I/O buffer
  32. *
  33. * @v len Required length of buffer
  34. * @ret iobuf I/O buffer, or NULL if none available
  35. *
  36. * The I/O buffer will be physically aligned to a multiple of
  37. * @c IOBUF_SIZE.
  38. */
  39. struct io_buffer * alloc_iob ( size_t len ) {
  40. struct io_buffer *iobuf;
  41. size_t align;
  42. void *data;
  43. /* Pad to minimum length */
  44. if ( len < IOB_ZLEN )
  45. len = IOB_ZLEN;
  46. /* Align buffer length to ensure that struct io_buffer is aligned */
  47. len = ( len + __alignof__ ( *iobuf ) - 1 ) &
  48. ~( __alignof__ ( *iobuf ) - 1 );
  49. /* Align buffer on its own size to avoid potential problems
  50. * with boundary-crossing DMA.
  51. */
  52. align = ( 1 << fls ( len - 1 ) );
  53. /* Allocate buffer plus descriptor as a single unit, unless
  54. * doing so will push the total size over the alignment
  55. * boundary.
  56. */
  57. if ( ( len + sizeof ( *iobuf ) ) <= align ) {
  58. /* Allocate memory for buffer plus descriptor */
  59. data = malloc_dma ( len + sizeof ( *iobuf ), align );
  60. if ( ! data )
  61. return NULL;
  62. iobuf = ( data + len );
  63. } else {
  64. /* Allocate memory for buffer */
  65. data = malloc_dma ( len, align );
  66. if ( ! data )
  67. return NULL;
  68. /* Allocate memory for descriptor */
  69. iobuf = malloc ( sizeof ( *iobuf ) );
  70. if ( ! iobuf ) {
  71. free_dma ( data, len );
  72. return NULL;
  73. }
  74. }
  75. /* Populate descriptor */
  76. iobuf->head = iobuf->data = iobuf->tail = data;
  77. iobuf->end = ( data + len );
  78. return iobuf;
  79. }
  80. /**
  81. * Free I/O buffer
  82. *
  83. * @v iobuf I/O buffer
  84. */
  85. void free_iob ( struct io_buffer *iobuf ) {
  86. size_t len;
  87. /* Allow free_iob(NULL) to be valid */
  88. if ( ! iobuf )
  89. return;
  90. /* Sanity checks */
  91. assert ( iobuf->head <= iobuf->data );
  92. assert ( iobuf->data <= iobuf->tail );
  93. assert ( iobuf->tail <= iobuf->end );
  94. /* Free buffer */
  95. len = ( iobuf->end - iobuf->head );
  96. if ( iobuf->end == iobuf ) {
  97. /* Descriptor is inline */
  98. free_dma ( iobuf->head, ( len + sizeof ( *iobuf ) ) );
  99. } else {
  100. /* Descriptor is detached */
  101. free_dma ( iobuf->head, len );
  102. free ( iobuf );
  103. }
  104. }
  105. /**
  106. * Ensure I/O buffer has sufficient headroom
  107. *
  108. * @v iobuf I/O buffer
  109. * @v len Required headroom
  110. *
  111. * This function currently only checks for the required headroom; it
  112. * does not reallocate the I/O buffer if required. If we ever have a
  113. * code path that requires this functionality, it's a fairly trivial
  114. * change to make.
  115. */
  116. int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ) {
  117. if ( iob_headroom ( iobuf ) >= len )
  118. return 0;
  119. return -ENOBUFS;
  120. }