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

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