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 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <stdlib.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <byteswap.h>
  25. #include <gpxe/list.h>
  26. #include <gpxe/if_ether.h>
  27. #include <gpxe/ethernet.h>
  28. #include <gpxe/iobuf.h>
  29. #include <gpxe/uaccess.h>
  30. #include <gpxe/ata.h>
  31. #include <gpxe/netdevice.h>
  32. #include <gpxe/process.h>
  33. #include <gpxe/features.h>
  34. #include <gpxe/aoe.h>
  35. /** @file
  36. *
  37. * AoE protocol
  38. *
  39. */
  40. FEATURE ( FEATURE_PROTOCOL, "AoE", DHCP_EB_FEATURE_AOE, 1 );
  41. struct net_protocol aoe_protocol;
  42. /** List of all AoE sessions */
  43. static LIST_HEAD ( aoe_sessions );
  44. static void aoe_free ( struct refcnt *refcnt ) {
  45. struct aoe_session *aoe =
  46. container_of ( refcnt, struct aoe_session, refcnt );
  47. netdev_put ( aoe->netdev );
  48. free ( aoe );
  49. }
  50. /**
  51. * Mark current AoE command complete
  52. *
  53. * @v aoe AoE session
  54. * @v rc Return status code
  55. */
  56. static void aoe_done ( struct aoe_session *aoe, int rc ) {
  57. /* Record overall command status */
  58. aoe->command->cb.cmd_stat = aoe->status;
  59. aoe->command = NULL;
  60. /* Mark operation as complete */
  61. aoe->rc = rc;
  62. }
  63. /**
  64. * Send AoE command
  65. *
  66. * @v aoe AoE session
  67. * @ret rc Return status code
  68. *
  69. * This transmits an AoE command packet. It does not wait for a
  70. * response.
  71. */
  72. static int aoe_send_command ( struct aoe_session *aoe ) {
  73. struct ata_command *command = aoe->command;
  74. struct io_buffer *iobuf;
  75. struct aoehdr *aoehdr;
  76. struct aoecmd *aoecmd;
  77. unsigned int count;
  78. unsigned int data_out_len;
  79. /* Fail immediately if we have no netdev to send on */
  80. if ( ! aoe->netdev ) {
  81. aoe_done ( aoe, -ENETUNREACH );
  82. return -ENETUNREACH;
  83. }
  84. /* If we are transmitting anything that requires a response,
  85. * start the retransmission timer. Do this before attempting
  86. * to allocate the I/O buffer, in case allocation itself
  87. * fails.
  88. */
  89. start_timer ( &aoe->timer );
  90. /* Calculate count and data_out_len for this subcommand */
  91. count = command->cb.count.native;
  92. if ( count > AOE_MAX_COUNT )
  93. count = AOE_MAX_COUNT;
  94. data_out_len = ( command->data_out ? ( count * ATA_SECTOR_SIZE ) : 0 );
  95. /* Create outgoing I/O buffer */
  96. iobuf = alloc_iob ( ETH_HLEN + sizeof ( *aoehdr ) +
  97. sizeof ( *aoecmd ) + data_out_len );
  98. if ( ! iobuf )
  99. return -ENOMEM;
  100. iob_reserve ( iobuf, ETH_HLEN );
  101. aoehdr = iob_put ( iobuf, sizeof ( *aoehdr ) );
  102. aoecmd = iob_put ( iobuf, sizeof ( *aoecmd ) );
  103. memset ( aoehdr, 0, ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) );
  104. /* Fill AoE header */
  105. aoehdr->ver_flags = AOE_VERSION;
  106. aoehdr->major = htons ( aoe->major );
  107. aoehdr->minor = aoe->minor;
  108. aoehdr->tag = htonl ( ++aoe->tag );
  109. /* Fill AoE command */
  110. linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
  111. aoecmd->aflags = ( ( command->cb.lba48 ? AOE_FL_EXTENDED : 0 ) |
  112. ( command->cb.device & ATA_DEV_SLAVE ) |
  113. ( data_out_len ? AOE_FL_WRITE : 0 ) );
  114. aoecmd->err_feat = command->cb.err_feat.bytes.cur;
  115. aoecmd->count = count;
  116. aoecmd->cmd_stat = command->cb.cmd_stat;
  117. aoecmd->lba.u64 = cpu_to_le64 ( command->cb.lba.native );
  118. if ( ! command->cb.lba48 )
  119. aoecmd->lba.bytes[3] |= ( command->cb.device & ATA_DEV_MASK );
  120. /* Fill data payload */
  121. copy_from_user ( iob_put ( iobuf, data_out_len ), command->data_out,
  122. aoe->command_offset, data_out_len );
  123. /* Send packet */
  124. return net_tx ( iobuf, aoe->netdev, &aoe_protocol, aoe->target );
  125. }
  126. /**
  127. * Handle AoE retry timer expiry
  128. *
  129. * @v timer AoE retry timer
  130. * @v fail Failure indicator
  131. */
  132. static void aoe_timer_expired ( struct retry_timer *timer, int fail ) {
  133. struct aoe_session *aoe =
  134. container_of ( timer, struct aoe_session, timer );
  135. if ( fail ) {
  136. aoe_done ( aoe, -ETIMEDOUT );
  137. } else {
  138. aoe_send_command ( aoe );
  139. }
  140. }
  141. /**
  142. * Handle AoE response
  143. *
  144. * @v aoe AoE session
  145. * @v aoehdr AoE header
  146. * @ret rc Return status code
  147. */
  148. static int aoe_rx_response ( struct aoe_session *aoe, struct aoehdr *aoehdr,
  149. unsigned int len ) {
  150. struct aoecmd *aoecmd = aoehdr->arg.command;
  151. struct ata_command *command = aoe->command;
  152. unsigned int rx_data_len;
  153. unsigned int count;
  154. unsigned int data_len;
  155. /* Sanity check */
  156. if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) ) {
  157. /* Ignore packet; allow timer to trigger retransmit */
  158. return -EINVAL;
  159. }
  160. rx_data_len = ( len - sizeof ( *aoehdr ) - sizeof ( *aoecmd ) );
  161. /* Stop retry timer. After this point, every code path must
  162. * either terminate the AoE operation via aoe_done(), or
  163. * transmit a new packet.
  164. */
  165. stop_timer ( &aoe->timer );
  166. /* Check for fatal errors */
  167. if ( aoehdr->ver_flags & AOE_FL_ERROR ) {
  168. aoe_done ( aoe, -EIO );
  169. return 0;
  170. }
  171. /* Calculate count and data_len for this subcommand */
  172. count = command->cb.count.native;
  173. if ( count > AOE_MAX_COUNT )
  174. count = AOE_MAX_COUNT;
  175. data_len = count * ATA_SECTOR_SIZE;
  176. /* Merge into overall ATA status */
  177. aoe->status |= aoecmd->cmd_stat;
  178. /* Copy data payload */
  179. if ( command->data_in ) {
  180. if ( rx_data_len > data_len )
  181. rx_data_len = data_len;
  182. copy_to_user ( command->data_in, aoe->command_offset,
  183. aoecmd->data, rx_data_len );
  184. }
  185. /* Update ATA command and offset */
  186. aoe->command_offset += data_len;
  187. command->cb.lba.native += count;
  188. command->cb.count.native -= count;
  189. /* Check for operation complete */
  190. if ( ! command->cb.count.native ) {
  191. aoe_done ( aoe, 0 );
  192. return 0;
  193. }
  194. /* Transmit next portion of request */
  195. aoe_send_command ( aoe );
  196. return 0;
  197. }
  198. /**
  199. * Process incoming AoE packets
  200. *
  201. * @v iobuf I/O buffer
  202. * @v netdev Network device
  203. * @v ll_source Link-layer source address
  204. * @ret rc Return status code
  205. *
  206. */
  207. static int aoe_rx ( struct io_buffer *iobuf,
  208. struct net_device *netdev __unused,
  209. const void *ll_source ) {
  210. struct aoehdr *aoehdr = iobuf->data;
  211. unsigned int len = iob_len ( iobuf );
  212. struct aoe_session *aoe;
  213. int rc = 0;
  214. /* Sanity checks */
  215. if ( len < sizeof ( *aoehdr ) ) {
  216. rc = -EINVAL;
  217. goto done;
  218. }
  219. if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
  220. rc = -EPROTONOSUPPORT;
  221. goto done;
  222. }
  223. if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
  224. /* Ignore AoE requests that we happen to see */
  225. goto done;
  226. }
  227. /* Demultiplex amongst active AoE sessions */
  228. list_for_each_entry ( aoe, &aoe_sessions, list ) {
  229. if ( ntohs ( aoehdr->major ) != aoe->major )
  230. continue;
  231. if ( aoehdr->minor != aoe->minor )
  232. continue;
  233. if ( ntohl ( aoehdr->tag ) != aoe->tag )
  234. continue;
  235. memcpy ( aoe->target, ll_source, sizeof ( aoe->target ) );
  236. rc = aoe_rx_response ( aoe, aoehdr, len );
  237. break;
  238. }
  239. done:
  240. free_iob ( iobuf );
  241. return rc;
  242. }
  243. /** AoE protocol */
  244. struct net_protocol aoe_protocol __net_protocol = {
  245. .name = "AoE",
  246. .net_proto = htons ( ETH_P_AOE ),
  247. .rx = aoe_rx,
  248. };
  249. /**
  250. * Issue ATA command via an open AoE session
  251. *
  252. * @v ata ATA device
  253. * @v command ATA command
  254. * @ret rc Return status code
  255. */
  256. static int aoe_command ( struct ata_device *ata,
  257. struct ata_command *command ) {
  258. struct aoe_session *aoe =
  259. container_of ( ata->backend, struct aoe_session, refcnt );
  260. int rc;
  261. aoe->command = command;
  262. aoe->status = 0;
  263. aoe->command_offset = 0;
  264. aoe_send_command ( aoe );
  265. aoe->rc = -EINPROGRESS;
  266. while ( aoe->rc == -EINPROGRESS )
  267. step();
  268. rc = aoe->rc;
  269. return rc;
  270. }
  271. static int aoe_detached_command ( struct ata_device *ata __unused,
  272. struct ata_command *command __unused ) {
  273. return -ENODEV;
  274. }
  275. void aoe_detach ( struct ata_device *ata ) {
  276. struct aoe_session *aoe =
  277. container_of ( ata->backend, struct aoe_session, refcnt );
  278. stop_timer ( &aoe->timer );
  279. ata->command = aoe_detached_command;
  280. list_del ( &aoe->list );
  281. ref_put ( ata->backend );
  282. ata->backend = NULL;
  283. }
  284. static int aoe_parse_root_path ( struct aoe_session *aoe,
  285. const char *root_path ) {
  286. char *ptr;
  287. if ( strncmp ( root_path, "aoe:", 4 ) != 0 )
  288. return -EINVAL;
  289. ptr = ( ( char * ) root_path + 4 );
  290. if ( *ptr++ != 'e' )
  291. return -EINVAL;
  292. aoe->major = strtoul ( ptr, &ptr, 10 );
  293. if ( *ptr++ != '.' )
  294. return -EINVAL;
  295. aoe->minor = strtoul ( ptr, &ptr, 10 );
  296. if ( *ptr )
  297. return -EINVAL;
  298. return 0;
  299. }
  300. int aoe_attach ( struct ata_device *ata, struct net_device *netdev,
  301. const char *root_path ) {
  302. struct aoe_session *aoe;
  303. int rc;
  304. /* Allocate and initialise structure */
  305. aoe = zalloc ( sizeof ( *aoe ) );
  306. if ( ! aoe )
  307. return -ENOMEM;
  308. aoe->refcnt.free = aoe_free;
  309. aoe->netdev = netdev_get ( netdev );
  310. memcpy ( aoe->target, ethernet_protocol.ll_broadcast,
  311. sizeof ( aoe->target ) );
  312. aoe->tag = AOE_TAG_MAGIC;
  313. aoe->timer.expired = aoe_timer_expired;
  314. /* Parse root path */
  315. if ( ( rc = aoe_parse_root_path ( aoe, root_path ) ) != 0 )
  316. goto err;
  317. /* Attach parent interface, transfer reference to connection
  318. * list, and return
  319. */
  320. ata->backend = ref_get ( &aoe->refcnt );
  321. ata->command = aoe_command;
  322. list_add ( &aoe->list, &aoe_sessions );
  323. return 0;
  324. err:
  325. ref_put ( &aoe->refcnt );
  326. return rc;
  327. }