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.

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