您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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