Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

aoe.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <vsprintf.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <byteswap.h>
  24. #include <gpxe/list.h>
  25. #include <gpxe/if_ether.h>
  26. #include <gpxe/pkbuff.h>
  27. #include <gpxe/uaccess.h>
  28. #include <gpxe/ata.h>
  29. #include <gpxe/netdevice.h>
  30. #include <gpxe/process.h>
  31. #include <gpxe/aoe.h>
  32. /** @file
  33. *
  34. * AoE protocol
  35. *
  36. */
  37. struct net_protocol aoe_protocol;
  38. /** List of all AoE sessions */
  39. static LIST_HEAD ( aoe_sessions );
  40. /**
  41. * Send AoE command
  42. *
  43. * @v aoe AoE session
  44. * @ret rc Return status code
  45. *
  46. * This transmits an AoE command packet. It does not wait for a
  47. * response.
  48. */
  49. static int aoe_send_command ( struct aoe_session *aoe ) {
  50. struct ata_command *command = aoe->command;
  51. struct pk_buff *pkb;
  52. struct aoehdr *aoehdr;
  53. struct aoecmd *aoecmd;
  54. /* Create outgoing packet buffer */
  55. pkb = alloc_pkb ( ETH_HLEN + sizeof ( *aoehdr ) + sizeof ( *aoecmd ) +
  56. command->data_out_len );
  57. if ( ! pkb )
  58. return -ENOMEM;
  59. pkb->net_protocol = &aoe_protocol;
  60. pkb_reserve ( pkb, ETH_HLEN );
  61. aoehdr = pkb_put ( pkb, sizeof ( *aoehdr ) );
  62. aoecmd = pkb_put ( pkb, sizeof ( *aoecmd ) );
  63. memset ( aoehdr, 0, ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) );
  64. /* Fill AoE header */
  65. aoehdr->ver_flags = AOE_VERSION;
  66. aoehdr->major = htons ( aoe->major );
  67. aoehdr->minor = aoe->minor;
  68. aoehdr->tag = htonl ( ++aoe->tag );
  69. /* Fill AoE command */
  70. linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
  71. aoecmd->aflags = ( ( command->cb.lba48 ? AOE_FL_EXTENDED : 0 ) |
  72. ( command->cb.device & ATA_DEV_SLAVE ) |
  73. ( command->data_out_len ? AOE_FL_WRITE : 0 ) );
  74. aoecmd->err_feat = command->cb.err_feat.bytes.cur;
  75. aoecmd->count = command->cb.count.bytes.cur;
  76. aoecmd->cmd_stat = command->cb.cmd_stat;
  77. aoecmd->lba.u64 = cpu_to_le64 ( command->cb.lba.native );
  78. if ( ! command->cb.lba48 )
  79. aoecmd->lba.bytes[3] |= ( command->cb.device & ATA_DEV_MASK );
  80. /* Fill data payload */
  81. copy_from_user ( pkb_put ( pkb, command->data_out_len ),
  82. command->data_out, aoe->command_offset,
  83. command->data_out_len );
  84. /* Send packet */
  85. return net_transmit_via ( pkb, aoe->netdev );
  86. }
  87. /**
  88. * Handle AoE response
  89. *
  90. * @v aoe AoE session
  91. * @v aoehdr AoE header
  92. * @ret rc Return status code
  93. */
  94. static int aoe_rx_response ( struct aoe_session *aoe, struct aoehdr *aoehdr,
  95. unsigned int len ) {
  96. struct aoecmd *aoecmd = aoehdr->arg.command;
  97. struct ata_command *command = aoe->command;
  98. unsigned int data_in_len;
  99. /* Sanity check */
  100. if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) )
  101. return -EINVAL;
  102. /* Set overall status code */
  103. aoe->status = ( ( aoehdr->ver_flags & AOE_FL_ERROR ) ?
  104. aoehdr->error : 0 );
  105. /* Copy ATA results */
  106. command->cb.err_feat.bytes.cur = aoecmd->err_feat;
  107. command->cb.count.bytes.cur = aoecmd->count;
  108. command->cb.cmd_stat = aoecmd->cmd_stat;
  109. command->cb.lba.native = le64_to_cpu ( aoecmd->lba.u64 );
  110. command->cb.lba.bytes.pad = 0;
  111. /* Copy data payload */
  112. data_in_len = ( len - sizeof ( *aoehdr ) - sizeof ( *aoecmd ) );
  113. if ( data_in_len > command->data_in_len ) {
  114. data_in_len = command->data_in_len;
  115. aoe->status |= AOE_STATUS_OVERRUN;
  116. } else if ( data_in_len < command->data_in_len ) {
  117. aoe->status |= AOE_STATUS_UNDERRUN;
  118. }
  119. copy_to_user ( command->data_in, aoe->command_offset,
  120. aoecmd->data, data_in_len );
  121. return 0;
  122. }
  123. /**
  124. * Process incoming AoE packets
  125. *
  126. * @v pkb Packet buffer
  127. * @ret rc Return status code
  128. *
  129. */
  130. static int aoe_rx ( struct pk_buff *pkb ) {
  131. struct aoehdr *aoehdr = pkb->data;
  132. unsigned int len = pkb_len ( pkb );
  133. struct aoe_session *aoe;
  134. int rc = 0;
  135. /* Sanity checks */
  136. if ( len < sizeof ( *aoehdr ) ) {
  137. rc = -EINVAL;
  138. goto done;
  139. }
  140. if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
  141. rc = -EPROTONOSUPPORT;
  142. goto done;
  143. }
  144. if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
  145. /* Ignore AoE requests that we happen to see */
  146. goto done;
  147. }
  148. /* Demultiplex amongst active AoE sessions */
  149. list_for_each_entry ( aoe, &aoe_sessions, list ) {
  150. if ( ntohs ( aoehdr->major ) != aoe->major )
  151. continue;
  152. if ( aoehdr->minor != aoe->minor )
  153. continue;
  154. if ( ntohl ( aoehdr->tag ) != aoe->tag )
  155. continue;
  156. #warning "Need a way to get the MAC address for future reference"
  157. rc = aoe_rx_response ( aoe, aoehdr, len );
  158. break;
  159. }
  160. done:
  161. free_pkb ( pkb );
  162. return rc;
  163. }
  164. /**
  165. * Perform AoE network-layer routing
  166. *
  167. * @v pkb Packet buffer
  168. * @ret source Network-layer source address
  169. * @ret dest Network-layer destination address
  170. * @ret rc Return status code
  171. */
  172. static int aoe_route ( const struct pk_buff *pkb __unused,
  173. struct net_header *nethdr ) {
  174. #warning "Need a way to find out the MAC address"
  175. nethdr->flags = PKT_FL_BROADCAST;
  176. return 0;
  177. }
  178. /** AoE protocol */
  179. struct net_protocol aoe_protocol = {
  180. .name = "AoE",
  181. .net_proto = htons ( ETH_P_AOE ),
  182. .rx_process = aoe_rx,
  183. .route = aoe_route,
  184. };
  185. NET_PROTOCOL ( aoe_protocol );
  186. /**
  187. * Open AoE session
  188. *
  189. * @v aoe AoE session
  190. */
  191. void aoe_open ( struct aoe_session *aoe ) {
  192. list_add ( &aoe->list, &aoe_sessions );
  193. }
  194. /**
  195. * Close AoE session
  196. *
  197. * @v aoe AoE session
  198. */
  199. void aoe_close ( struct aoe_session *aoe ) {
  200. list_del ( &aoe->list );
  201. }
  202. /**
  203. * Kick an AoE session into life
  204. *
  205. * @v aoe AoE session
  206. *
  207. * Transmits an AoE request. Call this function to issue a new
  208. * command, or when a retransmission timer expires.
  209. */
  210. void aoe_kick ( struct aoe_session *aoe ) {
  211. aoe_send_command ( aoe );
  212. }
  213. /**
  214. * Issue ATA command via an open AoE session
  215. *
  216. * @v aoe AoE session
  217. * @v command ATA command
  218. * @ret rc Return status code
  219. *
  220. * The ATA command must fit within a single AoE frame (i.e. the sector
  221. * count must not exceed AOE_MAX_COUNT).
  222. */
  223. int aoe_issue ( struct aoe_session *aoe, struct ata_command *command ) {
  224. aoe->command = command;
  225. aoe->status = AOE_STATUS_PENDING;
  226. aoe_kick ( aoe );
  227. while ( aoe->status & AOE_STATUS_PENDING ) {
  228. step();
  229. }
  230. aoe->command = NULL;
  231. return ( ( aoe->status & AOE_STATUS_ERR_MASK ) ? -EIO : 0 );
  232. }
  233. /**
  234. * Issue ATA command via an open AoE session
  235. *
  236. * @v aoe AoE session
  237. * @v command ATA command
  238. * @ret rc Return status code
  239. *
  240. * The ATA command will be split into several smaller ATA commands,
  241. * each with a sector count no larger than AOE_MAX_COUNT.
  242. */
  243. int aoe_issue_split ( struct aoe_session *aoe, struct ata_command *command ) {
  244. struct ata_command subcommand;
  245. unsigned int offset;
  246. unsigned int count;
  247. unsigned int data_len;
  248. unsigned int status = 0;
  249. int rc = 0;
  250. /* Split ATA command into AoE-sized subcommands */
  251. for ( offset = 0; offset < command->cb.count.native; offset += count ){
  252. memcpy ( &subcommand, command, sizeof ( subcommand ) );
  253. count = ( command->cb.count.native - offset );
  254. if ( count > AOE_MAX_COUNT )
  255. count = AOE_MAX_COUNT;
  256. data_len = count * ATA_SECTOR_SIZE;
  257. if ( subcommand.data_in_len )
  258. subcommand.data_in_len = data_len;
  259. if ( subcommand.data_out_len )
  260. subcommand.data_out_len = data_len;
  261. aoe->command_offset = ( offset * ATA_SECTOR_SIZE );
  262. subcommand.cb.lba.native += offset;
  263. subcommand.cb.count.native = count;
  264. if ( ( rc = aoe_issue ( aoe, &subcommand ) ) != 0 )
  265. goto done;
  266. status |= subcommand.cb.cmd_stat;
  267. }
  268. command->cb.cmd_stat = status;
  269. done:
  270. aoe->command_offset = 0;
  271. return rc;
  272. }