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.1KB

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