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.

aoe.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <string.h>
  20. #include <vsprintf.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <byteswap.h>
  24. #include <gpxe/list.h>
  25. #include <gpxe/if_ether.h>
  26. #include <gpxe/ethernet.h>
  27. #include <gpxe/pkbuff.h>
  28. #include <gpxe/uaccess.h>
  29. #include <gpxe/ata.h>
  30. #include <gpxe/netdevice.h>
  31. #include <gpxe/async.h>
  32. #include <gpxe/aoe.h>
  33. /** @file
  34. *
  35. * AoE protocol
  36. *
  37. */
  38. struct net_protocol aoe_protocol;
  39. /** List of all AoE sessions */
  40. static LIST_HEAD ( aoe_sessions );
  41. /**
  42. * Mark current AoE command complete
  43. *
  44. * @v aoe AoE session
  45. * @v rc Return status code
  46. */
  47. static void aoe_done ( struct aoe_session *aoe, int rc ) {
  48. /* Record overall command status */
  49. aoe->command->cb.cmd_stat = aoe->status;
  50. aoe->command = NULL;
  51. /* Mark async operation as complete */
  52. async_done ( &aoe->aop, rc );
  53. }
  54. /**
  55. * Send AoE command
  56. *
  57. * @v aoe AoE session
  58. * @ret rc Return status code
  59. *
  60. * This transmits an AoE command packet. It does not wait for a
  61. * response.
  62. */
  63. static int aoe_send_command ( struct aoe_session *aoe ) {
  64. struct ata_command *command = aoe->command;
  65. struct pk_buff *pkb;
  66. struct aoehdr *aoehdr;
  67. struct aoecmd *aoecmd;
  68. unsigned int count;
  69. unsigned int data_out_len;
  70. /* Calculate count and data_out_len for this subcommand */
  71. count = command->cb.count.native;
  72. if ( count > AOE_MAX_COUNT )
  73. count = AOE_MAX_COUNT;
  74. data_out_len = ( command->data_out ? ( count * ATA_SECTOR_SIZE ) : 0 );
  75. /* Create outgoing packet buffer */
  76. pkb = alloc_pkb ( ETH_HLEN + sizeof ( *aoehdr ) + sizeof ( *aoecmd ) +
  77. data_out_len );
  78. if ( ! pkb )
  79. return -ENOMEM;
  80. pkb_reserve ( pkb, ETH_HLEN );
  81. aoehdr = pkb_put ( pkb, sizeof ( *aoehdr ) );
  82. aoecmd = pkb_put ( pkb, sizeof ( *aoecmd ) );
  83. memset ( aoehdr, 0, ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) );
  84. /* Fill AoE header */
  85. aoehdr->ver_flags = AOE_VERSION;
  86. aoehdr->major = htons ( aoe->major );
  87. aoehdr->minor = aoe->minor;
  88. aoehdr->tag = htonl ( ++aoe->tag );
  89. /* Fill AoE command */
  90. linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
  91. aoecmd->aflags = ( ( command->cb.lba48 ? AOE_FL_EXTENDED : 0 ) |
  92. ( command->cb.device & ATA_DEV_SLAVE ) |
  93. ( data_out_len ? AOE_FL_WRITE : 0 ) );
  94. aoecmd->err_feat = command->cb.err_feat.bytes.cur;
  95. aoecmd->count = count;
  96. aoecmd->cmd_stat = command->cb.cmd_stat;
  97. aoecmd->lba.u64 = cpu_to_le64 ( command->cb.lba.native );
  98. if ( ! command->cb.lba48 )
  99. aoecmd->lba.bytes[3] |= ( command->cb.device & ATA_DEV_MASK );
  100. /* Fill data payload */
  101. copy_from_user ( pkb_put ( pkb, data_out_len ), command->data_out,
  102. aoe->command_offset, data_out_len );
  103. /* Send packet */
  104. start_timer ( &aoe->timer );
  105. return net_tx ( pkb, aoe->netdev, &aoe_protocol, aoe->target );
  106. }
  107. /**
  108. * Handle AoE retry timer expiry
  109. *
  110. * @v timer AoE retry timer
  111. * @v fail Failure indicator
  112. */
  113. static void aoe_timer_expired ( struct retry_timer *timer, int fail ) {
  114. struct aoe_session *aoe =
  115. container_of ( timer, struct aoe_session, timer );
  116. if ( fail ) {
  117. aoe_done ( aoe, -ETIMEDOUT );
  118. } else {
  119. aoe_send_command ( aoe );
  120. }
  121. }
  122. /**
  123. * Handle AoE response
  124. *
  125. * @v aoe AoE session
  126. * @v aoehdr AoE header
  127. * @ret rc Return status code
  128. */
  129. static int aoe_rx_response ( struct aoe_session *aoe, struct aoehdr *aoehdr,
  130. unsigned int len ) {
  131. struct aoecmd *aoecmd = aoehdr->arg.command;
  132. struct ata_command *command = aoe->command;
  133. unsigned int rx_data_len;
  134. unsigned int count;
  135. unsigned int data_len;
  136. /* Sanity check */
  137. if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) ) {
  138. /* Ignore packet; allow timer to trigger retransmit */
  139. return -EINVAL;
  140. }
  141. rx_data_len = ( len - sizeof ( *aoehdr ) - sizeof ( *aoecmd ) );
  142. /* Stop retry timer. After this point, every code path must
  143. * either terminate the AoE operation via aoe_done(), or
  144. * transmit a new packet.
  145. */
  146. stop_timer ( &aoe->timer );
  147. /* Check for fatal errors */
  148. if ( aoehdr->ver_flags & AOE_FL_ERROR ) {
  149. aoe_done ( aoe, -EIO );
  150. return 0;
  151. }
  152. /* Calculate count and data_len for this subcommand */
  153. count = command->cb.count.native;
  154. if ( count > AOE_MAX_COUNT )
  155. count = AOE_MAX_COUNT;
  156. data_len = count * ATA_SECTOR_SIZE;
  157. /* Merge into overall ATA status */
  158. aoe->status |= aoecmd->cmd_stat;
  159. /* Copy data payload */
  160. if ( command->data_in ) {
  161. if ( rx_data_len > data_len )
  162. rx_data_len = data_len;
  163. copy_to_user ( command->data_in, aoe->command_offset,
  164. aoecmd->data, rx_data_len );
  165. }
  166. /* Update ATA command and offset */
  167. aoe->command_offset += data_len;
  168. command->cb.lba.native += count;
  169. command->cb.count.native -= count;
  170. /* Check for operation complete */
  171. if ( ! command->cb.count.native ) {
  172. aoe_done ( aoe, 0 );
  173. return 0;
  174. }
  175. /* Transmit next portion of request */
  176. aoe_send_command ( aoe );
  177. return 0;
  178. }
  179. /**
  180. * Process incoming AoE packets
  181. *
  182. * @v pkb Packet buffer
  183. * @v netdev Network device
  184. * @v ll_source Link-layer source address
  185. * @ret rc Return status code
  186. *
  187. */
  188. static int aoe_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
  189. const void *ll_source ) {
  190. struct aoehdr *aoehdr = pkb->data;
  191. unsigned int len = pkb_len ( pkb );
  192. struct aoe_session *aoe;
  193. int rc = 0;
  194. /* Sanity checks */
  195. if ( len < sizeof ( *aoehdr ) ) {
  196. rc = -EINVAL;
  197. goto done;
  198. }
  199. if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
  200. rc = -EPROTONOSUPPORT;
  201. goto done;
  202. }
  203. if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
  204. /* Ignore AoE requests that we happen to see */
  205. goto done;
  206. }
  207. /* Demultiplex amongst active AoE sessions */
  208. list_for_each_entry ( aoe, &aoe_sessions, list ) {
  209. if ( ntohs ( aoehdr->major ) != aoe->major )
  210. continue;
  211. if ( aoehdr->minor != aoe->minor )
  212. continue;
  213. if ( ntohl ( aoehdr->tag ) != aoe->tag )
  214. continue;
  215. memcpy ( aoe->target, ll_source, sizeof ( aoe->target ) );
  216. rc = aoe_rx_response ( aoe, aoehdr, len );
  217. break;
  218. }
  219. done:
  220. free_pkb ( pkb );
  221. return rc;
  222. }
  223. /** AoE protocol */
  224. struct net_protocol aoe_protocol __net_protocol = {
  225. .name = "AoE",
  226. .net_proto = htons ( ETH_P_AOE ),
  227. .rx = aoe_rx,
  228. };
  229. /**
  230. * Open AoE session
  231. *
  232. * @v aoe AoE session
  233. */
  234. void aoe_open ( struct aoe_session *aoe ) {
  235. memcpy ( aoe->target, ethernet_protocol.ll_broadcast,
  236. sizeof ( aoe->target ) );
  237. aoe->tag = AOE_TAG_MAGIC;
  238. aoe->timer.expired = aoe_timer_expired;
  239. list_add ( &aoe->list, &aoe_sessions );
  240. }
  241. /**
  242. * Close AoE session
  243. *
  244. * @v aoe AoE session
  245. */
  246. void aoe_close ( struct aoe_session *aoe ) {
  247. list_del ( &aoe->list );
  248. }
  249. /**
  250. * Issue ATA command via an open AoE session
  251. *
  252. * @v aoe AoE session
  253. * @v command ATA command
  254. * @ret aop Asynchronous operation
  255. *
  256. * Only one command may be issued concurrently per session. This call
  257. * is non-blocking; use async_wait() to wait for the command to
  258. * complete.
  259. */
  260. struct async_operation * aoe_issue ( struct aoe_session *aoe,
  261. struct ata_command *command ) {
  262. aoe->command = command;
  263. aoe->status = 0;
  264. aoe->command_offset = 0;
  265. aoe_send_command ( aoe );
  266. return &aoe->aop;
  267. }