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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 <ipxe/list.h>
  27. #include <ipxe/if_ether.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/uaccess.h>
  30. #include <ipxe/netdevice.h>
  31. #include <ipxe/features.h>
  32. #include <ipxe/interface.h>
  33. #include <ipxe/xfer.h>
  34. #include <ipxe/uri.h>
  35. #include <ipxe/open.h>
  36. #include <ipxe/ata.h>
  37. #include <ipxe/device.h>
  38. #include <ipxe/aoe.h>
  39. /** @file
  40. *
  41. * AoE protocol
  42. *
  43. */
  44. FEATURE ( FEATURE_PROTOCOL, "AoE", DHCP_EB_FEATURE_AOE, 1 );
  45. struct net_protocol aoe_protocol __net_protocol;
  46. /******************************************************************************
  47. *
  48. * AoE devices and commands
  49. *
  50. ******************************************************************************
  51. */
  52. /** List of all AoE devices */
  53. static LIST_HEAD ( aoe_devices );
  54. /** List of active AoE commands */
  55. static LIST_HEAD ( aoe_commands );
  56. /** An AoE device */
  57. struct aoe_device {
  58. /** Reference counter */
  59. struct refcnt refcnt;
  60. /** Network device */
  61. struct net_device *netdev;
  62. /** ATA command issuing interface */
  63. struct interface ata;
  64. /** Major number */
  65. uint16_t major;
  66. /** Minor number */
  67. uint8_t minor;
  68. /** Target MAC address */
  69. uint8_t target[MAX_LL_ADDR_LEN];
  70. /** Saved timeout value */
  71. unsigned long timeout;
  72. /** Configuration command interface */
  73. struct interface config;
  74. /** Device is configued */
  75. int configured;
  76. };
  77. /** An AoE command */
  78. struct aoe_command {
  79. /** Reference count */
  80. struct refcnt refcnt;
  81. /** AOE device */
  82. struct aoe_device *aoedev;
  83. /** List of active commands */
  84. struct list_head list;
  85. /** ATA command interface */
  86. struct interface ata;
  87. /** ATA command */
  88. struct ata_cmd command;
  89. /** Command type */
  90. struct aoe_command_type *type;
  91. /** Command tag */
  92. uint32_t tag;
  93. /** Retransmission timer */
  94. struct retry_timer timer;
  95. };
  96. /** An AoE command type */
  97. struct aoe_command_type {
  98. /**
  99. * Calculate length of AoE command IU
  100. *
  101. * @v aoecmd AoE command
  102. * @ret len Length of command IU
  103. */
  104. size_t ( * cmd_len ) ( struct aoe_command *aoecmd );
  105. /**
  106. * Build AoE command IU
  107. *
  108. * @v aoecmd AoE command
  109. * @v data Command IU
  110. * @v len Length of command IU
  111. */
  112. void ( * cmd ) ( struct aoe_command *aoecmd, void *data, size_t len );
  113. /**
  114. * Handle AoE response IU
  115. *
  116. * @v aoecmd AoE command
  117. * @v data Response IU
  118. * @v len Length of response IU
  119. * @v ll_source Link-layer source address
  120. * @ret rc Return status code
  121. */
  122. int ( * rsp ) ( struct aoe_command *aoecmd, const void *data,
  123. size_t len, const void *ll_source );
  124. };
  125. /**
  126. * Get reference to AoE device
  127. *
  128. * @v aoedev AoE device
  129. * @ret aoedev AoE device
  130. */
  131. static inline __attribute__ (( always_inline )) struct aoe_device *
  132. aoedev_get ( struct aoe_device *aoedev ) {
  133. ref_get ( &aoedev->refcnt );
  134. return aoedev;
  135. }
  136. /**
  137. * Drop reference to AoE device
  138. *
  139. * @v aoedev AoE device
  140. */
  141. static inline __attribute__ (( always_inline )) void
  142. aoedev_put ( struct aoe_device *aoedev ) {
  143. ref_put ( &aoedev->refcnt );
  144. }
  145. /**
  146. * Get reference to AoE command
  147. *
  148. * @v aoecmd AoE command
  149. * @ret aoecmd AoE command
  150. */
  151. static inline __attribute__ (( always_inline )) struct aoe_command *
  152. aoecmd_get ( struct aoe_command *aoecmd ) {
  153. ref_get ( &aoecmd->refcnt );
  154. return aoecmd;
  155. }
  156. /**
  157. * Drop reference to AoE command
  158. *
  159. * @v aoecmd AoE command
  160. */
  161. static inline __attribute__ (( always_inline )) void
  162. aoecmd_put ( struct aoe_command *aoecmd ) {
  163. ref_put ( &aoecmd->refcnt );
  164. }
  165. /**
  166. * Name AoE device
  167. *
  168. * @v aoedev AoE device
  169. * @ret name AoE device name
  170. */
  171. static const char * aoedev_name ( struct aoe_device *aoedev ) {
  172. static char buf[16];
  173. snprintf ( buf, sizeof ( buf ), "%s/e%d.%d", aoedev->netdev->name,
  174. aoedev->major, aoedev->minor );
  175. return buf;
  176. }
  177. /**
  178. * Free AoE command
  179. *
  180. * @v refcnt Reference counter
  181. */
  182. static void aoecmd_free ( struct refcnt *refcnt ) {
  183. struct aoe_command *aoecmd =
  184. container_of ( refcnt, struct aoe_command, refcnt );
  185. assert ( ! timer_running ( &aoecmd->timer ) );
  186. assert ( list_empty ( &aoecmd->list ) );
  187. aoedev_put ( aoecmd->aoedev );
  188. free ( aoecmd );
  189. }
  190. /**
  191. * Close AoE command
  192. *
  193. * @v aoecmd AoE command
  194. * @v rc Reason for close
  195. */
  196. static void aoecmd_close ( struct aoe_command *aoecmd, int rc ) {
  197. struct aoe_device *aoedev = aoecmd->aoedev;
  198. /* Stop timer */
  199. stop_timer ( &aoecmd->timer );
  200. /* Preserve the timeout value for subsequent commands */
  201. aoedev->timeout = aoecmd->timer.timeout;
  202. /* Remove from list of commands */
  203. if ( ! list_empty ( &aoecmd->list ) ) {
  204. list_del ( &aoecmd->list );
  205. INIT_LIST_HEAD ( &aoecmd->list );
  206. aoecmd_put ( aoecmd );
  207. }
  208. /* Shut down interfaces */
  209. intf_shutdown ( &aoecmd->ata, rc );
  210. }
  211. /**
  212. * Transmit AoE command request
  213. *
  214. * @v aoecmd AoE command
  215. * @ret rc Return status code
  216. */
  217. static int aoecmd_tx ( struct aoe_command *aoecmd ) {
  218. struct aoe_device *aoedev = aoecmd->aoedev;
  219. struct net_device *netdev = aoedev->netdev;
  220. struct io_buffer *iobuf;
  221. struct aoehdr *aoehdr;
  222. size_t cmd_len;
  223. int rc;
  224. /* Sanity check */
  225. assert ( netdev != NULL );
  226. /* If we are transmitting anything that requires a response,
  227. * start the retransmission timer. Do this before attempting
  228. * to allocate the I/O buffer, in case allocation itself
  229. * fails.
  230. */
  231. start_timer ( &aoecmd->timer );
  232. /* Create outgoing I/O buffer */
  233. cmd_len = aoecmd->type->cmd_len ( aoecmd );
  234. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + cmd_len );
  235. if ( ! iobuf )
  236. return -ENOMEM;
  237. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  238. aoehdr = iob_put ( iobuf, cmd_len );
  239. /* Fill AoE header */
  240. memset ( aoehdr, 0, sizeof ( *aoehdr ) );
  241. aoehdr->ver_flags = AOE_VERSION;
  242. aoehdr->major = htons ( aoedev->major );
  243. aoehdr->minor = aoedev->minor;
  244. aoehdr->tag = htonl ( aoecmd->tag );
  245. aoecmd->type->cmd ( aoecmd, iobuf->data, iob_len ( iobuf ) );
  246. /* Send packet */
  247. if ( ( rc = net_tx ( iobuf, netdev, &aoe_protocol, aoedev->target,
  248. netdev->ll_addr ) ) != 0 ) {
  249. DBGC ( aoedev, "AoE %s/%08x could not transmit: %s\n",
  250. aoedev_name ( aoedev ), aoecmd->tag,
  251. strerror ( rc ) );
  252. return rc;
  253. }
  254. return 0;
  255. }
  256. /**
  257. * Receive AoE command response
  258. *
  259. * @v aoecmd AoE command
  260. * @v iobuf I/O buffer
  261. * @v ll_source Link-layer source address
  262. * @ret rc Return status code
  263. */
  264. static int aoecmd_rx ( struct aoe_command *aoecmd, struct io_buffer *iobuf,
  265. const void *ll_source ) {
  266. struct aoe_device *aoedev = aoecmd->aoedev;
  267. struct aoehdr *aoehdr = iobuf->data;
  268. int rc;
  269. /* Sanity check */
  270. if ( iob_len ( iobuf ) < sizeof ( *aoehdr ) ) {
  271. DBGC ( aoedev, "AoE %s/%08x received underlength response "
  272. "(%zd bytes)\n", aoedev_name ( aoedev ),
  273. aoecmd->tag, iob_len ( iobuf ) );
  274. rc = -EINVAL;
  275. goto done;
  276. }
  277. if ( ( ntohs ( aoehdr->major ) != aoedev->major ) ||
  278. ( aoehdr->minor != aoedev->minor ) ) {
  279. DBGC ( aoedev, "AoE %s/%08x received response for incorrect "
  280. "device e%d.%d\n", aoedev_name ( aoedev ), aoecmd->tag,
  281. ntohs ( aoehdr->major ), aoehdr->minor );
  282. rc = -EINVAL;
  283. goto done;
  284. }
  285. /* Catch command failures */
  286. if ( aoehdr->ver_flags & AOE_FL_ERROR ) {
  287. DBGC ( aoedev, "AoE %s/%08x terminated in error\n",
  288. aoedev_name ( aoedev ), aoecmd->tag );
  289. aoecmd_close ( aoecmd, -EIO );
  290. rc = -EIO;
  291. goto done;
  292. }
  293. /* Hand off to command completion handler */
  294. if ( ( rc = aoecmd->type->rsp ( aoecmd, iobuf->data, iob_len ( iobuf ),
  295. ll_source ) ) != 0 )
  296. goto done;
  297. done:
  298. /* Free I/O buffer */
  299. free_iob ( iobuf );
  300. /* Terminate command */
  301. aoecmd_close ( aoecmd, rc );
  302. return rc;
  303. }
  304. /**
  305. * Handle AoE retry timer expiry
  306. *
  307. * @v timer AoE retry timer
  308. * @v fail Failure indicator
  309. */
  310. static void aoecmd_expired ( struct retry_timer *timer, int fail ) {
  311. struct aoe_command *aoecmd =
  312. container_of ( timer, struct aoe_command, timer );
  313. if ( fail ) {
  314. aoecmd_close ( aoecmd, -ETIMEDOUT );
  315. } else {
  316. aoecmd_tx ( aoecmd );
  317. }
  318. }
  319. /**
  320. * Calculate length of AoE ATA command IU
  321. *
  322. * @v aoecmd AoE command
  323. * @ret len Length of command IU
  324. */
  325. static size_t aoecmd_ata_cmd_len ( struct aoe_command *aoecmd ) {
  326. struct ata_cmd *command = &aoecmd->command;
  327. return ( sizeof ( struct aoehdr ) + sizeof ( struct aoeata ) +
  328. command->data_out_len );
  329. }
  330. /**
  331. * Build AoE ATA command IU
  332. *
  333. * @v aoecmd AoE command
  334. * @v data Command IU
  335. * @v len Length of command IU
  336. */
  337. static void aoecmd_ata_cmd ( struct aoe_command *aoecmd,
  338. void *data, size_t len ) {
  339. struct aoe_device *aoedev = aoecmd->aoedev;
  340. struct ata_cmd *command = &aoecmd->command;
  341. struct aoehdr *aoehdr = data;
  342. struct aoeata *aoeata = &aoehdr->payload[0].ata;
  343. /* Sanity check */
  344. linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
  345. assert ( len == ( sizeof ( *aoehdr ) + sizeof ( *aoeata ) +
  346. command->data_out_len ) );
  347. /* Build IU */
  348. aoehdr->command = AOE_CMD_ATA;
  349. memset ( aoeata, 0, sizeof ( *aoeata ) );
  350. aoeata->aflags = ( ( command->cb.lba48 ? AOE_FL_EXTENDED : 0 ) |
  351. ( command->cb.device & ATA_DEV_SLAVE ) |
  352. ( command->data_out_len ? AOE_FL_WRITE : 0 ) );
  353. aoeata->err_feat = command->cb.err_feat.bytes.cur;
  354. aoeata->count = command->cb.count.native;
  355. aoeata->cmd_stat = command->cb.cmd_stat;
  356. aoeata->lba.u64 = cpu_to_le64 ( command->cb.lba.native );
  357. if ( ! command->cb.lba48 )
  358. aoeata->lba.bytes[3] |=
  359. ( command->cb.device & ATA_DEV_MASK );
  360. copy_from_user ( aoeata->data, command->data_out, 0,
  361. command->data_out_len );
  362. DBGC2 ( aoedev, "AoE %s/%08x ATA cmd %02x:%02x:%02x:%02x:%08llx",
  363. aoedev_name ( aoedev ), aoecmd->tag, aoeata->aflags,
  364. aoeata->err_feat, aoeata->count, aoeata->cmd_stat,
  365. aoeata->lba.u64 );
  366. if ( command->data_out_len )
  367. DBGC2 ( aoedev, " out %04zx", command->data_out_len );
  368. if ( command->data_in_len )
  369. DBGC2 ( aoedev, " in %04zx", command->data_in_len );
  370. DBGC2 ( aoedev, "\n" );
  371. }
  372. /**
  373. * Handle AoE ATA response IU
  374. *
  375. * @v aoecmd AoE command
  376. * @v data Response IU
  377. * @v len Length of response IU
  378. * @v ll_source Link-layer source address
  379. * @ret rc Return status code
  380. */
  381. static int aoecmd_ata_rsp ( struct aoe_command *aoecmd, const void *data,
  382. size_t len, const void *ll_source __unused ) {
  383. struct aoe_device *aoedev = aoecmd->aoedev;
  384. struct ata_cmd *command = &aoecmd->command;
  385. const struct aoehdr *aoehdr = data;
  386. const struct aoeata *aoeata = &aoehdr->payload[0].ata;
  387. size_t data_len;
  388. /* Sanity check */
  389. if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoeata ) ) ) {
  390. DBGC ( aoedev, "AoE %s/%08x received underlength ATA response "
  391. "(%zd bytes)\n", aoedev_name ( aoedev ),
  392. aoecmd->tag, len );
  393. return -EINVAL;
  394. }
  395. data_len = ( len - ( sizeof ( *aoehdr ) + sizeof ( *aoeata ) ) );
  396. DBGC2 ( aoedev, "AoE %s/%08x ATA rsp %02x in %04zx\n",
  397. aoedev_name ( aoedev ), aoecmd->tag, aoeata->cmd_stat,
  398. data_len );
  399. /* Check for command failure */
  400. if ( aoeata->cmd_stat & ATA_STAT_ERR ) {
  401. DBGC ( aoedev, "AoE %s/%08x status %02x\n",
  402. aoedev_name ( aoedev ), aoecmd->tag, aoeata->cmd_stat );
  403. return -EIO;
  404. }
  405. /* Check data-in length is sufficient. (There may be trailing
  406. * garbage due to Ethernet minimum-frame-size padding.)
  407. */
  408. if ( data_len < command->data_in_len ) {
  409. DBGC ( aoedev, "AoE %s/%08x data-in underrun (received %zd, "
  410. "expected %zd)\n", aoedev_name ( aoedev ), aoecmd->tag,
  411. data_len, command->data_in_len );
  412. return -ERANGE;
  413. }
  414. /* Copy out data payload */
  415. copy_to_user ( command->data_in, 0, aoeata->data,
  416. command->data_in_len );
  417. return 0;
  418. }
  419. /** AoE ATA command */
  420. static struct aoe_command_type aoecmd_ata = {
  421. .cmd_len = aoecmd_ata_cmd_len,
  422. .cmd = aoecmd_ata_cmd,
  423. .rsp = aoecmd_ata_rsp,
  424. };
  425. /**
  426. * Calculate length of AoE configuration command IU
  427. *
  428. * @v aoecmd AoE command
  429. * @ret len Length of command IU
  430. */
  431. static size_t aoecmd_cfg_cmd_len ( struct aoe_command *aoecmd __unused ) {
  432. return ( sizeof ( struct aoehdr ) + sizeof ( struct aoecfg ) );
  433. }
  434. /**
  435. * Build AoE configuration command IU
  436. *
  437. * @v aoecmd AoE command
  438. * @v data Command IU
  439. * @v len Length of command IU
  440. */
  441. static void aoecmd_cfg_cmd ( struct aoe_command *aoecmd,
  442. void *data, size_t len ) {
  443. struct aoe_device *aoedev = aoecmd->aoedev;
  444. struct aoehdr *aoehdr = data;
  445. struct aoecfg *aoecfg = &aoehdr->payload[0].cfg;
  446. /* Sanity check */
  447. assert ( len == ( sizeof ( *aoehdr ) + sizeof ( *aoecfg ) ) );
  448. /* Build IU */
  449. aoehdr->command = AOE_CMD_CONFIG;
  450. memset ( aoecfg, 0, sizeof ( *aoecfg ) );
  451. DBGC ( aoedev, "AoE %s/%08x CONFIG cmd\n",
  452. aoedev_name ( aoedev ), aoecmd->tag );
  453. }
  454. /**
  455. * Handle AoE configuration response IU
  456. *
  457. * @v aoecmd AoE command
  458. * @v data Response IU
  459. * @v len Length of response IU
  460. * @v ll_source Link-layer source address
  461. * @ret rc Return status code
  462. */
  463. static int aoecmd_cfg_rsp ( struct aoe_command *aoecmd, const void *data,
  464. size_t len, const void *ll_source ) {
  465. struct aoe_device *aoedev = aoecmd->aoedev;
  466. struct ll_protocol *ll_protocol = aoedev->netdev->ll_protocol;
  467. const struct aoehdr *aoehdr = data;
  468. const struct aoecfg *aoecfg = &aoehdr->payload[0].cfg;
  469. /* Sanity check */
  470. if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecfg ) ) ) {
  471. DBGC ( aoedev, "AoE %s/%08x received underlength "
  472. "configuration response (%zd bytes)\n",
  473. aoedev_name ( aoedev ), aoecmd->tag, len );
  474. return -EINVAL;
  475. }
  476. DBGC ( aoedev, "AoE %s/%08x CONFIG rsp buf %04x fw %04x scnt %02x\n",
  477. aoedev_name ( aoedev ), aoecmd->tag, ntohs ( aoecfg->bufcnt ),
  478. aoecfg->fwver, aoecfg->scnt );
  479. /* Record target MAC address */
  480. memcpy ( aoedev->target, ll_source, ll_protocol->ll_addr_len );
  481. DBGC ( aoedev, "AoE %s has MAC address %s\n",
  482. aoedev_name ( aoedev ), ll_protocol->ntoa ( aoedev->target ) );
  483. return 0;
  484. }
  485. /** AoE configuration command */
  486. static struct aoe_command_type aoecmd_cfg = {
  487. .cmd_len = aoecmd_cfg_cmd_len,
  488. .cmd = aoecmd_cfg_cmd,
  489. .rsp = aoecmd_cfg_rsp,
  490. };
  491. /** AoE command ATA interface operations */
  492. static struct interface_operation aoecmd_ata_op[] = {
  493. INTF_OP ( intf_close, struct aoe_command *, aoecmd_close ),
  494. };
  495. /** AoE command ATA interface descriptor */
  496. static struct interface_descriptor aoecmd_ata_desc =
  497. INTF_DESC ( struct aoe_command, ata, aoecmd_ata_op );
  498. /**
  499. * Identify AoE command by tag
  500. *
  501. * @v tag Command tag
  502. * @ret aoecmd AoE command, or NULL
  503. */
  504. static struct aoe_command * aoecmd_find_tag ( uint32_t tag ) {
  505. struct aoe_command *aoecmd;
  506. list_for_each_entry ( aoecmd, &aoe_commands, list ) {
  507. if ( aoecmd->tag == tag )
  508. return aoecmd;
  509. }
  510. return NULL;
  511. }
  512. /**
  513. * Choose an AoE command tag
  514. *
  515. * @ret tag New tag, or negative error
  516. */
  517. static int aoecmd_new_tag ( void ) {
  518. static uint16_t tag_idx;
  519. unsigned int i;
  520. for ( i = 0 ; i < 65536 ; i++ ) {
  521. tag_idx++;
  522. if ( aoecmd_find_tag ( tag_idx ) == NULL )
  523. return ( AOE_TAG_MAGIC | tag_idx );
  524. }
  525. return -EADDRINUSE;
  526. }
  527. /**
  528. * Create AoE command
  529. *
  530. * @v aoedev AoE device
  531. * @v type AoE command type
  532. * @ret aoecmd AoE command
  533. */
  534. static struct aoe_command * aoecmd_create ( struct aoe_device *aoedev,
  535. struct aoe_command_type *type ) {
  536. struct aoe_command *aoecmd;
  537. int tag;
  538. /* Allocate command tag */
  539. tag = aoecmd_new_tag();
  540. if ( tag < 0 )
  541. return NULL;
  542. /* Allocate and initialise structure */
  543. aoecmd = zalloc ( sizeof ( *aoecmd ) );
  544. if ( ! aoecmd )
  545. return NULL;
  546. ref_init ( &aoecmd->refcnt, aoecmd_free );
  547. list_add ( &aoecmd->list, &aoe_commands );
  548. intf_init ( &aoecmd->ata, &aoecmd_ata_desc, &aoecmd->refcnt );
  549. timer_init ( &aoecmd->timer, aoecmd_expired, &aoecmd->refcnt );
  550. aoecmd->aoedev = aoedev_get ( aoedev );
  551. aoecmd->type = type;
  552. aoecmd->tag = tag;
  553. /* Preserve timeout from last completed command */
  554. aoecmd->timer.timeout = aoedev->timeout;
  555. /* Return already mortalised. (Reference is held by command list.) */
  556. return aoecmd;
  557. }
  558. /**
  559. * Issue AoE ATA command
  560. *
  561. * @v aoedev AoE device
  562. * @v parent Parent interface
  563. * @v command ATA command
  564. * @ret tag Command tag, or negative error
  565. */
  566. static int aoedev_ata_command ( struct aoe_device *aoedev,
  567. struct interface *parent,
  568. struct ata_cmd *command ) {
  569. struct net_device *netdev = aoedev->netdev;
  570. struct aoe_command *aoecmd;
  571. /* Fail immediately if net device is closed */
  572. if ( ! netdev_is_open ( netdev ) ) {
  573. DBGC ( aoedev, "AoE %s cannot issue command while net device "
  574. "is closed\n", aoedev_name ( aoedev ) );
  575. return -EWOULDBLOCK;
  576. }
  577. /* Create command */
  578. aoecmd = aoecmd_create ( aoedev, &aoecmd_ata );
  579. if ( ! aoecmd )
  580. return -ENOMEM;
  581. memcpy ( &aoecmd->command, command, sizeof ( aoecmd->command ) );
  582. /* Attempt to send command. Allow failures to be handled by
  583. * the retry timer.
  584. */
  585. aoecmd_tx ( aoecmd );
  586. /* Attach to parent interface, leave reference with command
  587. * list, and return.
  588. */
  589. intf_plug_plug ( &aoecmd->ata, parent );
  590. return aoecmd->tag;
  591. }
  592. /**
  593. * Issue AoE configuration command
  594. *
  595. * @v aoedev AoE device
  596. * @v parent Parent interface
  597. * @ret tag Command tag, or negative error
  598. */
  599. static int aoedev_cfg_command ( struct aoe_device *aoedev,
  600. struct interface *parent ) {
  601. struct aoe_command *aoecmd;
  602. /* Create command */
  603. aoecmd = aoecmd_create ( aoedev, &aoecmd_cfg );
  604. if ( ! aoecmd )
  605. return -ENOMEM;
  606. /* Attempt to send command. Allow failures to be handled by
  607. * the retry timer.
  608. */
  609. aoecmd_tx ( aoecmd );
  610. /* Attach to parent interface, leave reference with command
  611. * list, and return.
  612. */
  613. intf_plug_plug ( &aoecmd->ata, parent );
  614. return aoecmd->tag;
  615. }
  616. /**
  617. * Free AoE device
  618. *
  619. * @v refcnt Reference count
  620. */
  621. static void aoedev_free ( struct refcnt *refcnt ) {
  622. struct aoe_device *aoedev =
  623. container_of ( refcnt, struct aoe_device, refcnt );
  624. netdev_put ( aoedev->netdev );
  625. free ( aoedev );
  626. }
  627. /**
  628. * Close AoE device
  629. *
  630. * @v aoedev AoE device
  631. * @v rc Reason for close
  632. */
  633. static void aoedev_close ( struct aoe_device *aoedev, int rc ) {
  634. struct aoe_command *aoecmd;
  635. struct aoe_command *tmp;
  636. /* Shut down interfaces */
  637. intf_shutdown ( &aoedev->ata, rc );
  638. intf_shutdown ( &aoedev->config, rc );
  639. /* Shut down any active commands */
  640. list_for_each_entry_safe ( aoecmd, tmp, &aoe_commands, list ) {
  641. if ( aoecmd->aoedev != aoedev )
  642. continue;
  643. aoecmd_get ( aoecmd );
  644. aoecmd_close ( aoecmd, rc );
  645. aoecmd_put ( aoecmd );
  646. }
  647. }
  648. /**
  649. * Check AoE device flow-control window
  650. *
  651. * @v aoedev AoE device
  652. * @ret len Length of window
  653. */
  654. static size_t aoedev_window ( struct aoe_device *aoedev ) {
  655. return ( aoedev->configured ? ~( ( size_t ) 0 ) : 0 );
  656. }
  657. /**
  658. * Handle AoE device configuration completion
  659. *
  660. * @v aoedev AoE device
  661. * @v rc Reason for completion
  662. */
  663. static void aoedev_config_done ( struct aoe_device *aoedev, int rc ) {
  664. /* Shut down interface */
  665. intf_shutdown ( &aoedev->config, rc );
  666. /* Close device on failure */
  667. if ( rc != 0 ) {
  668. aoedev_close ( aoedev, rc );
  669. return;
  670. }
  671. /* Mark device as configured */
  672. aoedev->configured = 1;
  673. xfer_window_changed ( &aoedev->ata );
  674. }
  675. /**
  676. * Identify device underlying AoE device
  677. *
  678. * @v aoedev AoE device
  679. * @ret device Underlying device
  680. */
  681. static struct device * aoedev_identify_device ( struct aoe_device *aoedev ) {
  682. return aoedev->netdev->dev;
  683. }
  684. /**
  685. * Describe AoE device in an ACPI table
  686. *
  687. * @v aoedev AoE device
  688. * @v acpi ACPI table
  689. * @v len Length of ACPI table
  690. * @ret rc Return status code
  691. */
  692. static int aoedev_describe ( struct aoe_device *aoedev,
  693. struct acpi_description_header *acpi,
  694. size_t len ) {
  695. struct abft_table *abft =
  696. container_of ( acpi, struct abft_table, acpi );
  697. /* Sanity check */
  698. if ( len < sizeof ( *abft ) )
  699. return -ENOBUFS;
  700. /* Populate table */
  701. abft->acpi.signature = cpu_to_le32 ( ABFT_SIG );
  702. abft->acpi.length = cpu_to_le32 ( sizeof ( *abft ) );
  703. abft->acpi.revision = 1;
  704. abft->shelf = cpu_to_le16 ( aoedev->major );
  705. abft->slot = aoedev->minor;
  706. memcpy ( abft->mac, aoedev->netdev->ll_addr, sizeof ( abft->mac ) );
  707. return 0;
  708. }
  709. /** AoE device ATA interface operations */
  710. static struct interface_operation aoedev_ata_op[] = {
  711. INTF_OP ( ata_command, struct aoe_device *, aoedev_ata_command ),
  712. INTF_OP ( xfer_window, struct aoe_device *, aoedev_window ),
  713. INTF_OP ( intf_close, struct aoe_device *, aoedev_close ),
  714. INTF_OP ( acpi_describe, struct aoe_device *, aoedev_describe ),
  715. INTF_OP ( identify_device, struct aoe_device *,
  716. aoedev_identify_device ),
  717. };
  718. /** AoE device ATA interface descriptor */
  719. static struct interface_descriptor aoedev_ata_desc =
  720. INTF_DESC ( struct aoe_device, ata, aoedev_ata_op );
  721. /** AoE device configuration interface operations */
  722. static struct interface_operation aoedev_config_op[] = {
  723. INTF_OP ( intf_close, struct aoe_device *, aoedev_config_done ),
  724. };
  725. /** AoE device configuration interface descriptor */
  726. static struct interface_descriptor aoedev_config_desc =
  727. INTF_DESC ( struct aoe_device, config, aoedev_config_op );
  728. /**
  729. * Open AoE device
  730. *
  731. * @v parent Parent interface
  732. * @v netdev Network device
  733. * @v major Device major number
  734. * @v minor Device minor number
  735. * @ret rc Return status code
  736. */
  737. static int aoedev_open ( struct interface *parent, struct net_device *netdev,
  738. unsigned int major, unsigned int minor ) {
  739. struct aoe_device *aoedev;
  740. int rc;
  741. /* Allocate and initialise structure */
  742. aoedev = zalloc ( sizeof ( *aoedev ) );
  743. if ( ! aoedev ) {
  744. rc = -ENOMEM;
  745. goto err_zalloc;
  746. }
  747. ref_init ( &aoedev->refcnt, aoedev_free );
  748. intf_init ( &aoedev->ata, &aoedev_ata_desc, &aoedev->refcnt );
  749. intf_init ( &aoedev->config, &aoedev_config_desc, &aoedev->refcnt );
  750. aoedev->netdev = netdev_get ( netdev );
  751. aoedev->major = major;
  752. aoedev->minor = minor;
  753. memcpy ( aoedev->target, netdev->ll_broadcast,
  754. netdev->ll_protocol->ll_addr_len );
  755. /* Initiate configuration */
  756. if ( ( rc = aoedev_cfg_command ( aoedev, &aoedev->config ) ) < 0 ) {
  757. DBGC ( aoedev, "AoE %s could not initiate configuration: %s\n",
  758. aoedev_name ( aoedev ), strerror ( rc ) );
  759. goto err_config;
  760. }
  761. /* Attach ATA device to parent interface */
  762. if ( ( rc = ata_open ( parent, &aoedev->ata, ATA_DEV_MASTER,
  763. AOE_MAX_COUNT ) ) != 0 ) {
  764. DBGC ( aoedev, "AoE %s could not create ATA device: %s\n",
  765. aoedev_name ( aoedev ), strerror ( rc ) );
  766. goto err_ata_open;
  767. }
  768. /* Mortalise self and return */
  769. ref_put ( &aoedev->refcnt );
  770. return 0;
  771. err_ata_open:
  772. err_config:
  773. aoedev_close ( aoedev, rc );
  774. ref_put ( &aoedev->refcnt );
  775. err_zalloc:
  776. return rc;
  777. }
  778. /******************************************************************************
  779. *
  780. * AoE network protocol
  781. *
  782. ******************************************************************************
  783. */
  784. /**
  785. * Process incoming AoE packets
  786. *
  787. * @v iobuf I/O buffer
  788. * @v netdev Network device
  789. * @v ll_dest Link-layer destination address
  790. * @v ll_source Link-layer source address
  791. * @ret rc Return status code
  792. *
  793. */
  794. static int aoe_rx ( struct io_buffer *iobuf,
  795. struct net_device *netdev __unused,
  796. const void *ll_dest __unused,
  797. const void *ll_source ) {
  798. struct aoehdr *aoehdr = iobuf->data;
  799. struct aoe_command *aoecmd;
  800. int rc;
  801. /* Sanity check */
  802. if ( iob_len ( iobuf ) < sizeof ( *aoehdr ) ) {
  803. DBG ( "AoE received underlength packet (%zd bytes)\n",
  804. iob_len ( iobuf ) );
  805. rc = -EINVAL;
  806. goto err_sanity;
  807. }
  808. if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
  809. DBG ( "AoE received packet for unsupported protocol version "
  810. "%02x\n", ( aoehdr->ver_flags & AOE_VERSION_MASK ) );
  811. rc = -EPROTONOSUPPORT;
  812. goto err_sanity;
  813. }
  814. if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
  815. DBG ( "AoE received request packet\n" );
  816. rc = -EOPNOTSUPP;
  817. goto err_sanity;
  818. }
  819. /* Demultiplex amongst active AoE commands */
  820. aoecmd = aoecmd_find_tag ( ntohl ( aoehdr->tag ) );
  821. if ( ! aoecmd ) {
  822. DBG ( "AoE received packet for unused tag %08x\n",
  823. ntohl ( aoehdr->tag ) );
  824. rc = -ENOENT;
  825. goto err_demux;
  826. }
  827. /* Pass received frame to command */
  828. aoecmd_get ( aoecmd );
  829. if ( ( rc = aoecmd_rx ( aoecmd, iob_disown ( iobuf ),
  830. ll_source ) ) != 0 )
  831. goto err_rx;
  832. err_rx:
  833. aoecmd_put ( aoecmd );
  834. err_demux:
  835. err_sanity:
  836. free_iob ( iobuf );
  837. return rc;
  838. }
  839. /** AoE protocol */
  840. struct net_protocol aoe_protocol __net_protocol = {
  841. .name = "AoE",
  842. .net_proto = htons ( ETH_P_AOE ),
  843. .rx = aoe_rx,
  844. };
  845. /******************************************************************************
  846. *
  847. * AoE URIs
  848. *
  849. ******************************************************************************
  850. */
  851. /**
  852. * Parse AoE URI
  853. *
  854. * @v uri URI
  855. * @ret major Major device number
  856. * @ret minor Minor device number
  857. * @ret rc Return status code
  858. *
  859. * An AoE URI has the form "aoe:e<major>.<minor>".
  860. */
  861. static int aoe_parse_uri ( struct uri *uri, unsigned int *major,
  862. unsigned int *minor ) {
  863. const char *ptr;
  864. char *end;
  865. /* Check for URI with opaque portion */
  866. if ( ! uri->opaque )
  867. return -EINVAL;
  868. ptr = uri->opaque;
  869. /* Check for initial 'e' */
  870. if ( *ptr != 'e' )
  871. return -EINVAL;
  872. ptr++;
  873. /* Parse major device number */
  874. *major = strtoul ( ptr, &end, 10 );
  875. if ( *end != '.' )
  876. return -EINVAL;
  877. ptr = ( end + 1 );
  878. /* Parse minor device number */
  879. *minor = strtoul ( ptr, &end, 10 );
  880. if ( *end )
  881. return -EINVAL;
  882. return 0;
  883. }
  884. /**
  885. * Open AoE URI
  886. *
  887. * @v parent Parent interface
  888. * @v uri URI
  889. * @ret rc Return status code
  890. */
  891. static int aoe_open ( struct interface *parent, struct uri *uri ) {
  892. struct net_device *netdev;
  893. unsigned int major;
  894. unsigned int minor;
  895. int rc;
  896. /* Identify network device. This is something of a hack, but
  897. * the AoE URI scheme that has been in use for some time now
  898. * provides no way to specify a particular device.
  899. */
  900. netdev = last_opened_netdev();
  901. if ( ! netdev ) {
  902. DBG ( "AoE cannot identify network device\n" );
  903. return -ENODEV;
  904. }
  905. /* Parse URI */
  906. if ( ( rc = aoe_parse_uri ( uri, &major, &minor ) ) != 0 ) {
  907. DBG ( "AoE cannot parse URI\n" );
  908. return rc;
  909. }
  910. /* Open AoE device */
  911. if ( ( rc = aoedev_open ( parent, netdev, major, minor ) ) != 0 )
  912. return rc;
  913. return 0;
  914. }
  915. /** AoE URI opener */
  916. struct uri_opener aoe_uri_opener __uri_opener = {
  917. .scheme = "aoe",
  918. .open = aoe_open,
  919. };