選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

aoe.c 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <stdio.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->async, 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. /* Fail immediately if we have no netdev to send on */
  71. if ( ! aoe->netdev ) {
  72. aoe_done ( aoe, -ENETUNREACH );
  73. return -ENETUNREACH;
  74. }
  75. /* Calculate count and data_out_len for this subcommand */
  76. count = command->cb.count.native;
  77. if ( count > AOE_MAX_COUNT )
  78. count = AOE_MAX_COUNT;
  79. data_out_len = ( command->data_out ? ( count * ATA_SECTOR_SIZE ) : 0 );
  80. /* Create outgoing packet buffer */
  81. pkb = alloc_pkb ( ETH_HLEN + sizeof ( *aoehdr ) + sizeof ( *aoecmd ) +
  82. data_out_len );
  83. if ( ! pkb )
  84. return -ENOMEM;
  85. pkb_reserve ( pkb, ETH_HLEN );
  86. aoehdr = pkb_put ( pkb, sizeof ( *aoehdr ) );
  87. aoecmd = pkb_put ( pkb, sizeof ( *aoecmd ) );
  88. memset ( aoehdr, 0, ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) );
  89. /* Fill AoE header */
  90. aoehdr->ver_flags = AOE_VERSION;
  91. aoehdr->major = htons ( aoe->major );
  92. aoehdr->minor = aoe->minor;
  93. aoehdr->tag = htonl ( ++aoe->tag );
  94. /* Fill AoE command */
  95. linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
  96. aoecmd->aflags = ( ( command->cb.lba48 ? AOE_FL_EXTENDED : 0 ) |
  97. ( command->cb.device & ATA_DEV_SLAVE ) |
  98. ( data_out_len ? AOE_FL_WRITE : 0 ) );
  99. aoecmd->err_feat = command->cb.err_feat.bytes.cur;
  100. aoecmd->count = count;
  101. aoecmd->cmd_stat = command->cb.cmd_stat;
  102. aoecmd->lba.u64 = cpu_to_le64 ( command->cb.lba.native );
  103. if ( ! command->cb.lba48 )
  104. aoecmd->lba.bytes[3] |= ( command->cb.device & ATA_DEV_MASK );
  105. /* Fill data payload */
  106. copy_from_user ( pkb_put ( pkb, data_out_len ), command->data_out,
  107. aoe->command_offset, data_out_len );
  108. /* Send packet */
  109. start_timer ( &aoe->timer );
  110. return net_tx ( pkb, aoe->netdev, &aoe_protocol, aoe->target );
  111. }
  112. /**
  113. * Handle AoE retry timer expiry
  114. *
  115. * @v timer AoE retry timer
  116. * @v fail Failure indicator
  117. */
  118. static void aoe_timer_expired ( struct retry_timer *timer, int fail ) {
  119. struct aoe_session *aoe =
  120. container_of ( timer, struct aoe_session, timer );
  121. if ( fail ) {
  122. aoe_done ( aoe, -ETIMEDOUT );
  123. } else {
  124. aoe_send_command ( aoe );
  125. }
  126. }
  127. /**
  128. * Handle AoE response
  129. *
  130. * @v aoe AoE session
  131. * @v aoehdr AoE header
  132. * @ret rc Return status code
  133. */
  134. static int aoe_rx_response ( struct aoe_session *aoe, struct aoehdr *aoehdr,
  135. unsigned int len ) {
  136. struct aoecmd *aoecmd = aoehdr->arg.command;
  137. struct ata_command *command = aoe->command;
  138. unsigned int rx_data_len;
  139. unsigned int count;
  140. unsigned int data_len;
  141. /* Sanity check */
  142. if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) ) {
  143. /* Ignore packet; allow timer to trigger retransmit */
  144. return -EINVAL;
  145. }
  146. rx_data_len = ( len - sizeof ( *aoehdr ) - sizeof ( *aoecmd ) );
  147. /* Stop retry timer. After this point, every code path must
  148. * either terminate the AoE operation via aoe_done(), or
  149. * transmit a new packet.
  150. */
  151. stop_timer ( &aoe->timer );
  152. /* Check for fatal errors */
  153. if ( aoehdr->ver_flags & AOE_FL_ERROR ) {
  154. aoe_done ( aoe, -EIO );
  155. return 0;
  156. }
  157. /* Calculate count and data_len for this subcommand */
  158. count = command->cb.count.native;
  159. if ( count > AOE_MAX_COUNT )
  160. count = AOE_MAX_COUNT;
  161. data_len = count * ATA_SECTOR_SIZE;
  162. /* Merge into overall ATA status */
  163. aoe->status |= aoecmd->cmd_stat;
  164. /* Copy data payload */
  165. if ( command->data_in ) {
  166. if ( rx_data_len > data_len )
  167. rx_data_len = data_len;
  168. copy_to_user ( command->data_in, aoe->command_offset,
  169. aoecmd->data, rx_data_len );
  170. }
  171. /* Update ATA command and offset */
  172. aoe->command_offset += data_len;
  173. command->cb.lba.native += count;
  174. command->cb.count.native -= count;
  175. /* Check for operation complete */
  176. if ( ! command->cb.count.native ) {
  177. aoe_done ( aoe, 0 );
  178. return 0;
  179. }
  180. /* Transmit next portion of request */
  181. aoe_send_command ( aoe );
  182. return 0;
  183. }
  184. /**
  185. * Process incoming AoE packets
  186. *
  187. * @v pkb Packet buffer
  188. * @v netdev Network device
  189. * @v ll_source Link-layer source address
  190. * @ret rc Return status code
  191. *
  192. */
  193. static int aoe_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
  194. const void *ll_source ) {
  195. struct aoehdr *aoehdr = pkb->data;
  196. unsigned int len = pkb_len ( pkb );
  197. struct aoe_session *aoe;
  198. int rc = 0;
  199. /* Sanity checks */
  200. if ( len < sizeof ( *aoehdr ) ) {
  201. rc = -EINVAL;
  202. goto done;
  203. }
  204. if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
  205. rc = -EPROTONOSUPPORT;
  206. goto done;
  207. }
  208. if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
  209. /* Ignore AoE requests that we happen to see */
  210. goto done;
  211. }
  212. /* Demultiplex amongst active AoE sessions */
  213. list_for_each_entry ( aoe, &aoe_sessions, list ) {
  214. if ( ntohs ( aoehdr->major ) != aoe->major )
  215. continue;
  216. if ( aoehdr->minor != aoe->minor )
  217. continue;
  218. if ( ntohl ( aoehdr->tag ) != aoe->tag )
  219. continue;
  220. memcpy ( aoe->target, ll_source, sizeof ( aoe->target ) );
  221. rc = aoe_rx_response ( aoe, aoehdr, len );
  222. break;
  223. }
  224. done:
  225. free_pkb ( pkb );
  226. return rc;
  227. }
  228. /** AoE protocol */
  229. struct net_protocol aoe_protocol __net_protocol = {
  230. .name = "AoE",
  231. .net_proto = htons ( ETH_P_AOE ),
  232. .rx = aoe_rx,
  233. };
  234. /**
  235. * Forget reference to net_device
  236. *
  237. * @v ref Persistent reference
  238. */
  239. static void aoe_forget_netdev ( struct reference *ref ) {
  240. struct aoe_session *aoe
  241. = container_of ( ref, struct aoe_session, netdev_ref );
  242. aoe->netdev = NULL;
  243. ref_del ( &aoe->netdev_ref );
  244. }
  245. /**
  246. * Open AoE session
  247. *
  248. * @v aoe AoE session
  249. */
  250. void aoe_open ( struct aoe_session *aoe ) {
  251. memcpy ( aoe->target, ethernet_protocol.ll_broadcast,
  252. sizeof ( aoe->target ) );
  253. aoe->tag = AOE_TAG_MAGIC;
  254. aoe->timer.expired = aoe_timer_expired;
  255. aoe->netdev_ref.forget = aoe_forget_netdev;
  256. ref_add ( &aoe->netdev_ref, &aoe->netdev->references );
  257. list_add ( &aoe->list, &aoe_sessions );
  258. }
  259. /**
  260. * Close AoE session
  261. *
  262. * @v aoe AoE session
  263. */
  264. void aoe_close ( struct aoe_session *aoe ) {
  265. if ( aoe->netdev )
  266. ref_del ( &aoe->netdev_ref );
  267. list_del ( &aoe->list );
  268. }
  269. /**
  270. * Issue ATA command via an open AoE session
  271. *
  272. * @v aoe AoE session
  273. * @v command ATA command
  274. * @v parent Parent asynchronous operation
  275. * @ret rc Return status code
  276. *
  277. * Only one command may be issued concurrently per session. This call
  278. * is non-blocking; use async_wait() to wait for the command to
  279. * complete.
  280. */
  281. int aoe_issue ( struct aoe_session *aoe, struct ata_command *command,
  282. struct async *parent ) {
  283. aoe->command = command;
  284. aoe->status = 0;
  285. aoe->command_offset = 0;
  286. aoe_send_command ( aoe );
  287. async_init ( &aoe->async, &default_async_operations, parent );
  288. return 0;
  289. }