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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. /*
  2. * Copyright (C) 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stddef.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <byteswap.h>
  31. #include <ipxe/refcnt.h>
  32. #include <ipxe/list.h>
  33. #include <ipxe/tables.h>
  34. #include <ipxe/timer.h>
  35. #include <ipxe/retry.h>
  36. #include <ipxe/interface.h>
  37. #include <ipxe/xfer.h>
  38. #include <ipxe/iobuf.h>
  39. #include <ipxe/fc.h>
  40. #include <ipxe/fcels.h>
  41. #include <ipxe/fcns.h>
  42. /** @file
  43. *
  44. * Fibre Channel
  45. *
  46. */
  47. /** List of Fibre Channel ports */
  48. LIST_HEAD ( fc_ports );
  49. /** List of Fibre Channel peers */
  50. LIST_HEAD ( fc_peers );
  51. /******************************************************************************
  52. *
  53. * Well-known addresses
  54. *
  55. ******************************************************************************
  56. */
  57. /** Unassigned port ID */
  58. struct fc_port_id fc_empty_port_id = { .bytes = { 0x00, 0x00, 0x00 } };
  59. /** F_Port contoller port ID */
  60. struct fc_port_id fc_f_port_id = { .bytes = { 0xff, 0xff, 0xfe } };
  61. /** Generic services port ID */
  62. struct fc_port_id fc_gs_port_id = { .bytes = { 0xff, 0xff, 0xfc } };
  63. /** Point-to-point low port ID */
  64. struct fc_port_id fc_ptp_low_port_id = { .bytes = { 0x01, 0x01, 0x01 } };
  65. /** Point-to-point high port ID */
  66. struct fc_port_id fc_ptp_high_port_id = { .bytes = { 0x01, 0x01, 0x02 } };
  67. /******************************************************************************
  68. *
  69. * Utility functions
  70. *
  71. ******************************************************************************
  72. */
  73. /**
  74. * Format Fibre Channel port ID
  75. *
  76. * @v id Fibre Channel port ID
  77. * @ret id_text Port ID text
  78. */
  79. const char * fc_id_ntoa ( const struct fc_port_id *id ) {
  80. static char id_text[ FC_PORT_ID_STRLEN + 1 /* NUL */ ];
  81. snprintf ( id_text, sizeof ( id_text ), "%02x.%02x.%02x",
  82. id->bytes[0], id->bytes[1], id->bytes[2] );
  83. return id_text;
  84. }
  85. /**
  86. * Parse Fibre Channel port ID
  87. *
  88. * @v id_text Port ID text
  89. * @ret id Fibre Channel port ID
  90. * @ret rc Return status code
  91. */
  92. int fc_id_aton ( const char *id_text, struct fc_port_id *id ) {
  93. char *ptr = ( ( char * ) id_text );
  94. unsigned int i = 0;
  95. while ( 1 ) {
  96. id->bytes[i++] = strtoul ( ptr, &ptr, 16 );
  97. if ( i == sizeof ( id->bytes ) )
  98. return ( ( *ptr == '\0' ) ? 0 : -EINVAL );
  99. if ( *ptr != '.' )
  100. return -EINVAL;
  101. ptr++;
  102. }
  103. }
  104. /**
  105. * Format Fibre Channel WWN
  106. *
  107. * @v wwn Fibre Channel WWN
  108. * @ret wwn_text WWN text
  109. */
  110. const char * fc_ntoa ( const struct fc_name *wwn ) {
  111. static char wwn_text[ FC_NAME_STRLEN + 1 /* NUL */ ];
  112. snprintf ( wwn_text, sizeof ( wwn_text ),
  113. "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
  114. wwn->bytes[0], wwn->bytes[1], wwn->bytes[2], wwn->bytes[3],
  115. wwn->bytes[4], wwn->bytes[5], wwn->bytes[6], wwn->bytes[7] );
  116. return wwn_text;
  117. }
  118. /**
  119. * Parse Fibre Channel WWN
  120. *
  121. * @v wwn_text WWN text
  122. * @ret wwn Fibre Channel WWN
  123. * @ret rc Return status code
  124. */
  125. int fc_aton ( const char *wwn_text, struct fc_name *wwn ) {
  126. char *ptr = ( ( char * ) wwn_text );
  127. unsigned int i = 0;
  128. while ( 1 ) {
  129. wwn->bytes[i++] = strtoul ( ptr, &ptr, 16 );
  130. if ( i == sizeof ( wwn->bytes ) )
  131. return ( ( *ptr == '\0' ) ? 0 : -EINVAL );
  132. if ( *ptr != ':' )
  133. return -EINVAL;
  134. ptr++;
  135. }
  136. }
  137. /**
  138. * Fill Fibre Channel socket address
  139. *
  140. * @v sa_fc Fibre Channel socket address to fill in
  141. * @v id Fibre Channel port ID
  142. * @ret sa Socket address
  143. */
  144. struct sockaddr * fc_fill_sockaddr ( struct sockaddr_fc *sa_fc,
  145. struct fc_port_id *id ) {
  146. union {
  147. struct sockaddr sa;
  148. struct sockaddr_fc fc;
  149. } *u = container_of ( sa_fc, typeof ( *u ), fc );
  150. memset ( sa_fc, 0, sizeof ( *sa_fc ) );
  151. sa_fc->sfc_family = AF_FC;
  152. memcpy ( &sa_fc->sfc_port_id, id, sizeof ( sa_fc->sfc_port_id ) );
  153. return &u->sa;
  154. }
  155. /******************************************************************************
  156. *
  157. * Fibre Channel link state
  158. *
  159. ******************************************************************************
  160. */
  161. /** Default link status code */
  162. #define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
  163. #define EINFO_EUNKNOWN_LINK_STATUS \
  164. __einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )
  165. /**
  166. * Mark Fibre Channel link as up
  167. *
  168. * @v link Fibre Channel link state monitor
  169. */
  170. static void fc_link_up ( struct fc_link_state *link ) {
  171. /* Stop retry timer */
  172. stop_timer ( &link->timer );
  173. /* Record link state */
  174. link->rc = 0;
  175. }
  176. /**
  177. * Mark Fibre Channel link as down
  178. *
  179. * @v link Fibre Channel link state monitor
  180. * @v rc Link state
  181. */
  182. static void fc_link_err ( struct fc_link_state *link, int rc ) {
  183. /* Record link state */
  184. if ( rc == 0 )
  185. rc = -EUNKNOWN_LINK_STATUS;
  186. link->rc = rc;
  187. /* Schedule another link examination */
  188. start_timer_fixed ( &link->timer, FC_LINK_RETRY_DELAY );
  189. }
  190. /**
  191. * Examine Fibre Channel link state
  192. *
  193. * @v link Fibre Channel link state monitor
  194. */
  195. static void fc_link_examine ( struct fc_link_state *link ) {
  196. link->examine ( link );
  197. }
  198. /**
  199. * Handle Fibre Channel link retry timer expiry
  200. */
  201. static void fc_link_expired ( struct retry_timer *timer, int over __unused ) {
  202. struct fc_link_state *link =
  203. container_of ( timer, struct fc_link_state, timer );
  204. /* Schedule another link examination */
  205. start_timer_fixed ( &link->timer, FC_LINK_RETRY_DELAY );
  206. /* Examine link */
  207. fc_link_examine ( link );
  208. }
  209. /**
  210. * Initialise Fibre Channel link state monitor
  211. *
  212. * @v link Fibre Channel link state monitor
  213. * @v examine Examine link state method
  214. * @v refcnt Reference counter
  215. */
  216. static void fc_link_init ( struct fc_link_state *link,
  217. void ( * examine ) ( struct fc_link_state *link ),
  218. struct refcnt *refcnt ) {
  219. link->rc = -EUNKNOWN_LINK_STATUS;
  220. timer_init ( &link->timer, fc_link_expired, refcnt );
  221. link->examine = examine;
  222. }
  223. /**
  224. * Start monitoring Fibre Channel link state
  225. *
  226. * @v link Fibre Channel link state monitor
  227. */
  228. static void fc_link_start ( struct fc_link_state *link ) {
  229. start_timer_nodelay ( &link->timer );
  230. }
  231. /**
  232. * Stop monitoring Fibre Channel link state
  233. *
  234. * @v link Fibre Channel link state monitor
  235. */
  236. static void fc_link_stop ( struct fc_link_state *link ) {
  237. stop_timer ( &link->timer );
  238. }
  239. /******************************************************************************
  240. *
  241. * Fibre Channel exchanges
  242. *
  243. ******************************************************************************
  244. */
  245. /** A Fibre Channel exchange */
  246. struct fc_exchange {
  247. /** Reference count */
  248. struct refcnt refcnt;
  249. /** Fibre Channel port */
  250. struct fc_port *port;
  251. /** List of active exchanges within this port */
  252. struct list_head list;
  253. /** Peer port ID */
  254. struct fc_port_id peer_port_id;
  255. /** Data structure type */
  256. unsigned int type;
  257. /** Flags */
  258. unsigned int flags;
  259. /** Local exchange ID */
  260. uint16_t xchg_id;
  261. /** Peer exchange ID */
  262. uint16_t peer_xchg_id;
  263. /** Active sequence ID */
  264. uint8_t seq_id;
  265. /** Active sequence count */
  266. uint16_t seq_cnt;
  267. /** Timeout timer */
  268. struct retry_timer timer;
  269. /** Upper-layer protocol interface */
  270. struct interface ulp;
  271. };
  272. /** Fibre Channel exchange flags */
  273. enum fc_exchange_flags {
  274. /** We are the exchange originator */
  275. FC_XCHG_ORIGINATOR = 0x0001,
  276. /** We have the sequence initiative */
  277. FC_XCHG_SEQ_INITIATIVE = 0x0002,
  278. /** This is the first sequence of the exchange */
  279. FC_XCHG_SEQ_FIRST = 0x0004,
  280. };
  281. /** Fibre Channel timeout */
  282. #define FC_TIMEOUT ( 1 * TICKS_PER_SEC )
  283. /**
  284. * Create local Fibre Channel exchange identifier
  285. *
  286. * @ret xchg_id Local exchange ID
  287. */
  288. static unsigned int fc_new_xchg_id ( void ) {
  289. static uint16_t next_id = 0x0000;
  290. /* We must avoid using FC_RX_ID_UNKNOWN (0xffff) */
  291. next_id += 2;
  292. return next_id;
  293. }
  294. /**
  295. * Create local Fibre Channel sequence identifier
  296. *
  297. * @ret seq_id Local sequence identifier
  298. */
  299. static unsigned int fc_new_seq_id ( void ) {
  300. static uint8_t seq_id = 0x00;
  301. return (++seq_id);
  302. }
  303. /**
  304. * Free Fibre Channel exchange
  305. *
  306. * @v refcnt Reference count
  307. */
  308. static void fc_xchg_free ( struct refcnt *refcnt ) {
  309. struct fc_exchange *xchg =
  310. container_of ( refcnt, struct fc_exchange, refcnt );
  311. assert ( ! timer_running ( &xchg->timer ) );
  312. assert ( list_empty ( &xchg->list ) );
  313. fc_port_put ( xchg->port );
  314. free ( xchg );
  315. }
  316. /**
  317. * Close Fibre Channel exchange
  318. *
  319. * @v xchg Fibre Channel exchange
  320. * @v rc Reason for close
  321. */
  322. static void fc_xchg_close ( struct fc_exchange *xchg, int rc ) {
  323. struct fc_port *port = xchg->port;
  324. if ( rc != 0 ) {
  325. DBGC2 ( port, "FCXCHG %s/%04x closed: %s\n",
  326. port->name, xchg->xchg_id, strerror ( rc ) );
  327. }
  328. /* Stop timer */
  329. stop_timer ( &xchg->timer );
  330. /* If list still holds a reference, remove from list of open
  331. * exchanges and drop list's reference.
  332. */
  333. if ( ! list_empty ( &xchg->list ) ) {
  334. list_del ( &xchg->list );
  335. INIT_LIST_HEAD ( &xchg->list );
  336. ref_put ( &xchg->refcnt );
  337. }
  338. /* Shutdown interfaces */
  339. intf_shutdown ( &xchg->ulp, rc );
  340. }
  341. /**
  342. * Handle exchange timeout
  343. *
  344. * @v timer Timeout timer
  345. * @v over Failure indicator
  346. */
  347. static void fc_xchg_expired ( struct retry_timer *timer, int over __unused ) {
  348. struct fc_exchange *xchg =
  349. container_of ( timer, struct fc_exchange, timer );
  350. struct fc_port *port = xchg->port;
  351. DBGC ( port, "FCXCHG %s/%04x timed out\n", port->name, xchg->xchg_id );
  352. /* Terminate the exchange */
  353. fc_xchg_close ( xchg, -ETIMEDOUT );
  354. }
  355. /**
  356. * Check Fibre Channel exchange window
  357. *
  358. * @v xchg Fibre Channel exchange
  359. * @ret len Length opf window
  360. */
  361. static size_t fc_xchg_window ( struct fc_exchange *xchg __unused ) {
  362. /* We don't currently store the path MTU */
  363. return FC_LOGIN_DEFAULT_MTU;
  364. }
  365. /**
  366. * Allocate Fibre Channel I/O buffer
  367. *
  368. * @v xchg Fibre Channel exchange
  369. * @v len Payload length
  370. * @ret iobuf I/O buffer, or NULL
  371. */
  372. static struct io_buffer * fc_xchg_alloc_iob ( struct fc_exchange *xchg,
  373. size_t len ) {
  374. struct fc_port *port = xchg->port;
  375. struct io_buffer *iobuf;
  376. iobuf = xfer_alloc_iob ( &port->transport,
  377. ( sizeof ( struct fc_frame_header ) + len ) );
  378. if ( iobuf ) {
  379. iob_reserve ( iobuf, sizeof ( struct fc_frame_header ) );
  380. }
  381. return iobuf;
  382. }
  383. /**
  384. * Transmit data as part of a Fibre Channel exchange
  385. *
  386. * @v xchg Fibre Channel exchange
  387. * @v iobuf I/O buffer
  388. * @v meta Data transfer metadata
  389. * @ret rc Return status code
  390. */
  391. static int fc_xchg_tx ( struct fc_exchange *xchg, struct io_buffer *iobuf,
  392. struct xfer_metadata *meta ) {
  393. struct fc_port *port = xchg->port;
  394. struct sockaddr_fc *dest = ( ( struct sockaddr_fc * ) meta->dest );
  395. struct fc_frame_header *fchdr;
  396. unsigned int r_ctl;
  397. unsigned int f_ctl_es;
  398. int rc;
  399. /* Sanity checks */
  400. if ( ! ( xchg->flags & FC_XCHG_SEQ_INITIATIVE ) ) {
  401. DBGC ( port, "FCXCHG %s/%04x cannot transmit while not "
  402. "holding sequence initiative\n",
  403. port->name, xchg->xchg_id );
  404. rc = -EBUSY;
  405. goto done;
  406. }
  407. /* Calculate routing control */
  408. switch ( xchg->type ) {
  409. case FC_TYPE_ELS:
  410. r_ctl = FC_R_CTL_ELS;
  411. if ( meta->flags & XFER_FL_RESPONSE ) {
  412. r_ctl |= FC_R_CTL_SOL_CTRL;
  413. } else {
  414. r_ctl |= FC_R_CTL_UNSOL_CTRL;
  415. }
  416. break;
  417. case FC_TYPE_CT:
  418. r_ctl = FC_R_CTL_DATA;
  419. if ( meta->flags & XFER_FL_RESPONSE ) {
  420. r_ctl |= FC_R_CTL_SOL_CTRL;
  421. } else {
  422. r_ctl |= FC_R_CTL_UNSOL_CTRL;
  423. }
  424. break;
  425. default:
  426. r_ctl = FC_R_CTL_DATA;
  427. switch ( meta->flags &
  428. ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ) ) {
  429. case ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ):
  430. r_ctl |= FC_R_CTL_CMD_STAT;
  431. break;
  432. case ( XFER_FL_CMD_STAT ):
  433. r_ctl |= FC_R_CTL_UNSOL_CMD;
  434. break;
  435. case ( XFER_FL_RESPONSE ):
  436. r_ctl |= FC_R_CTL_SOL_DATA;
  437. break;
  438. default:
  439. r_ctl |= FC_R_CTL_UNSOL_DATA;
  440. break;
  441. }
  442. break;
  443. }
  444. /* Calculate exchange and sequence control */
  445. f_ctl_es = 0;
  446. if ( ! ( xchg->flags & FC_XCHG_ORIGINATOR ) )
  447. f_ctl_es |= FC_F_CTL_ES_RESPONDER;
  448. if ( xchg->flags & FC_XCHG_SEQ_FIRST )
  449. f_ctl_es |= FC_F_CTL_ES_FIRST;
  450. if ( meta->flags & XFER_FL_OUT )
  451. f_ctl_es |= ( FC_F_CTL_ES_END | FC_F_CTL_ES_LAST );
  452. if ( meta->flags & XFER_FL_OVER )
  453. f_ctl_es |= ( FC_F_CTL_ES_END | FC_F_CTL_ES_TRANSFER );
  454. /* Create frame header */
  455. fchdr = iob_push ( iobuf, sizeof ( *fchdr ) );
  456. memset ( fchdr, 0, sizeof ( *fchdr ) );
  457. fchdr->r_ctl = r_ctl;
  458. memcpy ( &fchdr->d_id,
  459. ( dest ? &dest->sfc_port_id : &xchg->peer_port_id ),
  460. sizeof ( fchdr->d_id ) );
  461. memcpy ( &fchdr->s_id, &port->port_id, sizeof ( fchdr->s_id ) );
  462. fchdr->type = xchg->type;
  463. fchdr->f_ctl_es = f_ctl_es;
  464. fchdr->seq_id = xchg->seq_id;
  465. fchdr->seq_cnt = htons ( xchg->seq_cnt++ );
  466. fchdr->ox_id = htons ( ( xchg->flags & FC_XCHG_ORIGINATOR ) ?
  467. xchg->xchg_id : xchg->peer_xchg_id );
  468. fchdr->rx_id = htons ( ( xchg->flags & FC_XCHG_ORIGINATOR ) ?
  469. xchg->peer_xchg_id : xchg->xchg_id );
  470. if ( meta->flags & XFER_FL_ABS_OFFSET ) {
  471. fchdr->f_ctl_misc |= FC_F_CTL_MISC_REL_OFF;
  472. fchdr->parameter = htonl ( meta->offset );
  473. }
  474. /* Relinquish sequence initiative if applicable */
  475. if ( meta->flags & XFER_FL_OVER ) {
  476. xchg->flags &= ~( FC_XCHG_SEQ_INITIATIVE | FC_XCHG_SEQ_FIRST );
  477. xchg->seq_cnt = 0;
  478. }
  479. /* Reset timeout */
  480. start_timer_fixed ( &xchg->timer, FC_TIMEOUT );
  481. /* Deliver frame */
  482. if ( ( rc = xfer_deliver_iob ( &port->transport,
  483. iob_disown ( iobuf ) ) ) != 0 ) {
  484. DBGC ( port, "FCXCHG %s/%04x cannot transmit: %s\n",
  485. port->name, xchg->xchg_id, strerror ( rc ) );
  486. goto done;
  487. }
  488. done:
  489. free_iob ( iobuf );
  490. return rc;
  491. }
  492. /** Mapping from Fibre Channel routing control information to xfer metadata */
  493. static const uint8_t fc_r_ctl_info_meta_flags[ FC_R_CTL_INFO_MASK + 1 ] = {
  494. [FC_R_CTL_UNCAT] = ( 0 ),
  495. [FC_R_CTL_SOL_DATA] = ( XFER_FL_RESPONSE ),
  496. [FC_R_CTL_UNSOL_CTRL] = ( XFER_FL_CMD_STAT ),
  497. [FC_R_CTL_SOL_CTRL] = ( XFER_FL_CMD_STAT ),
  498. [FC_R_CTL_UNSOL_DATA] = ( 0 ),
  499. [FC_R_CTL_DATA_DESC] = ( XFER_FL_CMD_STAT ),
  500. [FC_R_CTL_UNSOL_CMD] = ( XFER_FL_CMD_STAT ),
  501. [FC_R_CTL_CMD_STAT] = ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ),
  502. };
  503. /**
  504. * Receive data as part of a Fibre Channel exchange
  505. *
  506. * @v xchg Fibre Channel exchange
  507. * @v iobuf I/O buffer
  508. * @v meta Data transfer metadata
  509. * @ret rc Return status code
  510. */
  511. static int fc_xchg_rx ( struct fc_exchange *xchg, struct io_buffer *iobuf,
  512. struct xfer_metadata *meta __unused ) {
  513. struct fc_port *port = xchg->port;
  514. struct fc_frame_header *fchdr = iobuf->data;
  515. struct xfer_metadata fc_meta;
  516. struct sockaddr_fc src;
  517. struct sockaddr_fc dest;
  518. int rc;
  519. /* Record peer exchange ID */
  520. xchg->peer_xchg_id =
  521. ntohs ( ( fchdr->f_ctl_es & FC_F_CTL_ES_RESPONDER ) ?
  522. fchdr->rx_id : fchdr->ox_id );
  523. /* Sequence checks */
  524. if ( xchg->flags & FC_XCHG_SEQ_INITIATIVE ) {
  525. DBGC ( port, "FCXCHG %s/%04x received frame while holding "
  526. "sequence initiative\n", port->name, xchg->xchg_id );
  527. rc = -EBUSY;
  528. goto done;
  529. }
  530. if ( ntohs ( fchdr->seq_cnt ) != xchg->seq_cnt ) {
  531. DBGC ( port, "FCXCHG %s/%04x received out-of-order frame %d "
  532. "(expected %d)\n", port->name, xchg->xchg_id,
  533. ntohs ( fchdr->seq_cnt ), xchg->seq_cnt );
  534. rc = -EPIPE;
  535. goto done;
  536. }
  537. if ( xchg->seq_cnt == 0 )
  538. xchg->seq_id = fchdr->seq_id;
  539. xchg->seq_cnt++;
  540. if ( fchdr->seq_id != xchg->seq_id ) {
  541. DBGC ( port, "FCXCHG %s/%04x received frame for incorrect "
  542. "sequence %02x (expected %02x)\n", port->name,
  543. xchg->xchg_id, fchdr->seq_id, xchg->seq_id );
  544. rc = -EPIPE;
  545. goto done;
  546. }
  547. /* Check for end of sequence and transfer of sequence initiative */
  548. if ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) {
  549. xchg->seq_cnt = 0;
  550. if ( fchdr->f_ctl_es & FC_F_CTL_ES_TRANSFER ) {
  551. xchg->flags |= FC_XCHG_SEQ_INITIATIVE;
  552. xchg->seq_id = fc_new_seq_id();
  553. }
  554. }
  555. /* Construct metadata */
  556. memset ( &fc_meta, 0, sizeof ( fc_meta ) );
  557. fc_meta.flags =
  558. fc_r_ctl_info_meta_flags[ fchdr->r_ctl & FC_R_CTL_INFO_MASK ];
  559. if ( fchdr->f_ctl_es & FC_F_CTL_ES_TRANSFER ) {
  560. fc_meta.flags |= XFER_FL_OVER;
  561. }
  562. if ( ( fchdr->f_ctl_es & FC_F_CTL_ES_LAST ) &&
  563. ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) ) {
  564. fc_meta.flags |= XFER_FL_OUT;
  565. }
  566. if ( fchdr->f_ctl_misc & FC_F_CTL_MISC_REL_OFF ) {
  567. fc_meta.flags |= XFER_FL_ABS_OFFSET;
  568. fc_meta.offset = ntohl ( fchdr->parameter );
  569. }
  570. fc_meta.src = fc_fill_sockaddr ( &src, &fchdr->s_id );
  571. fc_meta.dest = fc_fill_sockaddr ( &dest, &fchdr->d_id );
  572. /* Reset timeout */
  573. start_timer_fixed ( &xchg->timer, FC_TIMEOUT );
  574. /* Deliver via exchange's ULP interface */
  575. iob_pull ( iobuf, sizeof ( *fchdr ) );
  576. if ( ( rc = xfer_deliver ( &xchg->ulp, iob_disown ( iobuf ),
  577. &fc_meta ) ) != 0 ) {
  578. DBGC ( port, "FCXCHG %s/%04x cannot deliver frame: %s\n",
  579. port->name, xchg->xchg_id, strerror ( rc ) );
  580. goto done;
  581. }
  582. /* Close exchange if applicable */
  583. if ( ( fchdr->f_ctl_es & FC_F_CTL_ES_LAST ) &&
  584. ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) ) {
  585. fc_xchg_close ( xchg, 0 );
  586. }
  587. done:
  588. free_iob ( iobuf );
  589. return rc;
  590. }
  591. /** Fibre Channel exchange ULP interface operations */
  592. static struct interface_operation fc_xchg_ulp_op[] = {
  593. INTF_OP ( xfer_deliver, struct fc_exchange *, fc_xchg_tx ),
  594. INTF_OP ( xfer_alloc_iob, struct fc_exchange *, fc_xchg_alloc_iob ),
  595. INTF_OP ( xfer_window, struct fc_exchange *, fc_xchg_window ),
  596. INTF_OP ( intf_close, struct fc_exchange *, fc_xchg_close ),
  597. };
  598. /** Fibre Channel exchange ULP interface descriptor */
  599. static struct interface_descriptor fc_xchg_ulp_desc =
  600. INTF_DESC ( struct fc_exchange, ulp, fc_xchg_ulp_op );
  601. /**
  602. * Create new Fibre Channel exchange
  603. *
  604. * @v port Fibre Channel port
  605. * @v peer_port_id Peer port ID
  606. * @ret xchg Exchange, or NULL
  607. */
  608. static struct fc_exchange * fc_xchg_create ( struct fc_port *port,
  609. struct fc_port_id *peer_port_id,
  610. unsigned int type ) {
  611. struct fc_exchange *xchg;
  612. /* Allocate and initialise structure */
  613. xchg = zalloc ( sizeof ( *xchg ) );
  614. if ( ! xchg )
  615. return NULL;
  616. ref_init ( &xchg->refcnt, fc_xchg_free );
  617. intf_init ( &xchg->ulp, &fc_xchg_ulp_desc, &xchg->refcnt );
  618. timer_init ( &xchg->timer, fc_xchg_expired, &xchg->refcnt );
  619. xchg->port = fc_port_get ( port );
  620. memcpy ( &xchg->peer_port_id, peer_port_id,
  621. sizeof ( xchg->peer_port_id ) );
  622. xchg->type = type;
  623. xchg->xchg_id = fc_new_xchg_id();
  624. xchg->peer_xchg_id = FC_RX_ID_UNKNOWN;
  625. xchg->seq_id = fc_new_seq_id();
  626. /* Transfer reference to list of exchanges and return */
  627. list_add ( &xchg->list, &port->xchgs );
  628. return xchg;
  629. }
  630. /**
  631. * Originate a new Fibre Channel exchange
  632. *
  633. * @v parent Interface to which to attach
  634. * @v port Fibre Channel port
  635. * @v peer_port_id Peer port ID
  636. * @ret xchg_id Exchange ID, or negative error
  637. */
  638. int fc_xchg_originate ( struct interface *parent, struct fc_port *port,
  639. struct fc_port_id *peer_port_id, unsigned int type ) {
  640. struct fc_exchange *xchg;
  641. /* Allocate and initialise structure */
  642. xchg = fc_xchg_create ( port, peer_port_id, type );
  643. if ( ! xchg )
  644. return -ENOMEM;
  645. xchg->flags = ( FC_XCHG_ORIGINATOR | FC_XCHG_SEQ_INITIATIVE |
  646. FC_XCHG_SEQ_FIRST );
  647. DBGC2 ( port, "FCXCHG %s/%04x originating to %s (type %02x)\n",
  648. port->name, xchg->xchg_id, fc_id_ntoa ( &xchg->peer_port_id ),
  649. xchg->type );
  650. /* Attach to parent interface and return */
  651. intf_plug_plug ( &xchg->ulp, parent );
  652. return xchg->xchg_id;
  653. }
  654. /**
  655. * Open a new responder Fibre Channel exchange
  656. *
  657. * @v port Fibre Channel port
  658. * @v fchdr Fibre Channel frame header
  659. * @ret xchg Fibre Channel exchange, or NULL
  660. */
  661. static struct fc_exchange * fc_xchg_respond ( struct fc_port *port,
  662. struct fc_frame_header *fchdr ) {
  663. struct fc_exchange *xchg;
  664. struct fc_responder *responder;
  665. unsigned int type = fchdr->type;
  666. int rc;
  667. /* Allocate and initialise structure */
  668. xchg = fc_xchg_create ( port, &fchdr->s_id, type );
  669. if ( ! xchg )
  670. return NULL;
  671. xchg->seq_id = fchdr->seq_id;
  672. DBGC2 ( port, "FCXCHG %s/%04x responding to %s xchg %04x (type "
  673. "%02x)\n", port->name, xchg->xchg_id,
  674. fc_id_ntoa ( &xchg->peer_port_id ),
  675. ntohs ( fchdr->ox_id ), xchg->type );
  676. /* Find a responder, if any */
  677. for_each_table_entry ( responder, FC_RESPONDERS ) {
  678. if ( responder->type == type ) {
  679. if ( ( rc = responder->respond ( &xchg->ulp, port,
  680. &fchdr->d_id,
  681. &fchdr->s_id ) ) !=0 ){
  682. DBGC ( port, "FCXCHG %s/%04x could not "
  683. "respond: %s\n", port->name,
  684. xchg->xchg_id, strerror ( rc ) );
  685. }
  686. }
  687. break;
  688. }
  689. /* We may or may not have a ULP attached at this point, but
  690. * the exchange does exist.
  691. */
  692. return xchg;
  693. }
  694. /******************************************************************************
  695. *
  696. * Fibre Channel ports
  697. *
  698. ******************************************************************************
  699. */
  700. /**
  701. * Close Fibre Channel port
  702. *
  703. * @v port Fibre Channel port
  704. * @v rc Reason for close
  705. */
  706. static void fc_port_close ( struct fc_port *port, int rc ) {
  707. struct fc_exchange *xchg;
  708. struct fc_exchange *tmp;
  709. DBGC ( port, "FCPORT %s closed\n", port->name );
  710. /* Log out port, if necessary */
  711. if ( fc_link_ok ( &port->link ) )
  712. fc_port_logout ( port, rc );
  713. /* Stop link monitor */
  714. fc_link_stop ( &port->link );
  715. /* Shut down interfaces */
  716. intf_shutdown ( &port->transport, rc );
  717. intf_shutdown ( &port->flogi, rc );
  718. intf_shutdown ( &port->ns_plogi, rc );
  719. /* Shut down any remaining exchanges */
  720. list_for_each_entry_safe ( xchg, tmp, &port->xchgs, list )
  721. fc_xchg_close ( xchg, rc );
  722. /* Remove from list of ports */
  723. list_del ( &port->list );
  724. INIT_LIST_HEAD ( &port->list );
  725. }
  726. /**
  727. * Identify Fibre Channel exchange by local exchange ID
  728. *
  729. * @v port Fibre Channel port
  730. * @v xchg_id Local exchange ID
  731. * @ret xchg Fibre Channel exchange, or NULL
  732. */
  733. static struct fc_exchange * fc_port_demux ( struct fc_port *port,
  734. unsigned int xchg_id ) {
  735. struct fc_exchange *xchg;
  736. list_for_each_entry ( xchg, &port->xchgs, list ) {
  737. if ( xchg->xchg_id == xchg_id )
  738. return xchg;
  739. }
  740. return NULL;
  741. }
  742. /**
  743. * Handle received frame from Fibre Channel port
  744. *
  745. * @v port Fibre Channel port
  746. * @v iobuf I/O buffer
  747. * @v meta Data transfer metadata
  748. * @ret rc Return status code
  749. */
  750. static int fc_port_deliver ( struct fc_port *port, struct io_buffer *iobuf,
  751. struct xfer_metadata *meta ) {
  752. struct fc_frame_header *fchdr = iobuf->data;
  753. unsigned int xchg_id;
  754. struct fc_exchange *xchg;
  755. int rc;
  756. /* Sanity check */
  757. if ( iob_len ( iobuf ) < sizeof ( *fchdr ) ) {
  758. DBGC ( port, "FCPORT %s received underlength frame (%zd "
  759. "bytes)\n", port->name, iob_len ( iobuf ) );
  760. rc = -EINVAL;
  761. goto err_sanity;
  762. }
  763. /* Verify local port ID */
  764. if ( ( memcmp ( &fchdr->d_id, &port->port_id,
  765. sizeof ( fchdr->d_id ) ) != 0 ) &&
  766. ( memcmp ( &fchdr->d_id, &fc_f_port_id,
  767. sizeof ( fchdr->d_id ) ) != 0 ) &&
  768. ( memcmp ( &port->port_id, &fc_empty_port_id,
  769. sizeof ( port->port_id ) ) != 0 ) ) {
  770. DBGC ( port, "FCPORT %s received frame for incorrect port ID "
  771. "%s\n", port->name, fc_id_ntoa ( &fchdr->d_id ) );
  772. rc = -ENOTCONN;
  773. goto err_port_id;
  774. }
  775. /* Demultiplex amongst active exchanges */
  776. xchg_id = ntohs ( ( fchdr->f_ctl_es & FC_F_CTL_ES_RESPONDER ) ?
  777. fchdr->ox_id : fchdr->rx_id );
  778. xchg = fc_port_demux ( port, xchg_id );
  779. /* If we have no active exchange and this frame starts a new
  780. * exchange, try to create a new responder exchange
  781. */
  782. if ( ( fchdr->f_ctl_es & FC_F_CTL_ES_FIRST ) &&
  783. ( fchdr->seq_cnt == 0 ) ) {
  784. /* Create new exchange */
  785. xchg = fc_xchg_respond ( port, fchdr );
  786. if ( ! xchg ) {
  787. DBGC ( port, "FCPORT %s cannot create new exchange\n",
  788. port->name );
  789. rc = -ENOMEM;
  790. goto err_respond;
  791. }
  792. }
  793. /* Fail if no exchange exists */
  794. if ( ! xchg ) {
  795. DBGC ( port, "FCPORT %s xchg %04x unknown\n",
  796. port->name, xchg_id );
  797. rc = -ENOTCONN;
  798. goto err_no_xchg;
  799. }
  800. /* Pass received frame to exchange */
  801. ref_get ( &xchg->refcnt );
  802. if ( ( rc = fc_xchg_rx ( xchg, iob_disown ( iobuf ), meta ) ) != 0 )
  803. goto err_xchg_rx;
  804. err_xchg_rx:
  805. ref_put ( &xchg->refcnt );
  806. err_no_xchg:
  807. err_respond:
  808. err_port_id:
  809. err_sanity:
  810. free_iob ( iobuf );
  811. return rc;
  812. }
  813. /**
  814. * Log in Fibre Channel port
  815. *
  816. * @v port Fibre Channel port
  817. * @v port_id Local port ID
  818. * @v link_node_wwn Link node name
  819. * @v link_port_wwn Link port name
  820. * @v has_fabric Link is to a fabric
  821. * @ret rc Return status code
  822. */
  823. int fc_port_login ( struct fc_port *port, struct fc_port_id *port_id,
  824. const struct fc_name *link_node_wwn,
  825. const struct fc_name *link_port_wwn, int has_fabric ) {
  826. struct fc_peer *peer;
  827. struct fc_peer *tmp;
  828. int rc;
  829. /* Perform implicit logout if logged in and details differ */
  830. if ( fc_link_ok ( &port->link ) &&
  831. ( ( ( !! ( port->flags & FC_PORT_HAS_FABRIC ) ) !=
  832. ( !! has_fabric ) ) ||
  833. ( memcmp ( &port->link_node_wwn, link_node_wwn,
  834. sizeof ( port->link_node_wwn ) ) != 0 ) ||
  835. ( memcmp ( &port->link_port_wwn, link_port_wwn,
  836. sizeof ( port->link_port_wwn ) ) != 0 ) ||
  837. ( has_fabric &&
  838. ( memcmp ( &port->port_id, port_id,
  839. sizeof ( port->port_id ) ) != 0 ) ) ) ) {
  840. fc_port_logout ( port, 0 );
  841. }
  842. /* Log in, if applicable */
  843. if ( ! fc_link_ok ( &port->link ) ) {
  844. /* Record link port name */
  845. memcpy ( &port->link_node_wwn, link_node_wwn,
  846. sizeof ( port->link_node_wwn ) );
  847. memcpy ( &port->link_port_wwn, link_port_wwn,
  848. sizeof ( port->link_port_wwn ) );
  849. DBGC ( port, "FCPORT %s logged in to %s",
  850. port->name, fc_ntoa ( &port->link_node_wwn ) );
  851. DBGC ( port, " port %s\n", fc_ntoa ( &port->link_port_wwn ) );
  852. /* Calculate local (and possibly remote) port IDs */
  853. if ( has_fabric ) {
  854. port->flags |= FC_PORT_HAS_FABRIC;
  855. memcpy ( &port->port_id, port_id,
  856. sizeof ( port->port_id ) );
  857. } else {
  858. port->flags &= ~FC_PORT_HAS_FABRIC;
  859. if ( memcmp ( &port->port_wwn, link_port_wwn,
  860. sizeof ( port->port_wwn ) ) > 0 ) {
  861. memcpy ( &port->port_id, &fc_ptp_high_port_id,
  862. sizeof ( port->port_id ) );
  863. memcpy ( &port->ptp_link_port_id,
  864. &fc_ptp_low_port_id,
  865. sizeof ( port->ptp_link_port_id ) );
  866. } else {
  867. memcpy ( &port->port_id, &fc_ptp_low_port_id,
  868. sizeof ( port->port_id ) );
  869. memcpy ( &port->ptp_link_port_id,
  870. &fc_ptp_high_port_id,
  871. sizeof ( port->ptp_link_port_id ) );
  872. }
  873. }
  874. DBGC ( port, "FCPORT %s logged in via a %s, with local ID "
  875. "%s\n", port->name,
  876. ( ( port->flags & FC_PORT_HAS_FABRIC ) ?
  877. "fabric" : "point-to-point link" ),
  878. fc_id_ntoa ( &port->port_id ) );
  879. }
  880. /* Log in to name server, if attached to a fabric */
  881. if ( has_fabric && ! ( port->flags & FC_PORT_HAS_NS ) ) {
  882. DBGC ( port, "FCPORT %s attempting login to name server\n",
  883. port->name );
  884. intf_restart ( &port->ns_plogi, -ECANCELED );
  885. if ( ( rc = fc_els_plogi ( &port->ns_plogi, port,
  886. &fc_gs_port_id ) ) != 0 ) {
  887. DBGC ( port, "FCPORT %s could not initiate name "
  888. "server PLOGI: %s\n",
  889. port->name, strerror ( rc ) );
  890. fc_port_logout ( port, rc );
  891. return rc;
  892. }
  893. }
  894. /* Record login */
  895. fc_link_up ( &port->link );
  896. /* Notify peers of link state change */
  897. list_for_each_entry_safe ( peer, tmp, &fc_peers, list ) {
  898. fc_peer_get ( peer );
  899. fc_link_examine ( &peer->link );
  900. fc_peer_put ( peer );
  901. }
  902. return 0;
  903. }
  904. /**
  905. * Log out Fibre Channel port
  906. *
  907. * @v port Fibre Channel port
  908. * @v rc Reason for logout
  909. */
  910. void fc_port_logout ( struct fc_port *port, int rc ) {
  911. struct fc_peer *peer;
  912. struct fc_peer *tmp;
  913. DBGC ( port, "FCPORT %s logged out: %s\n",
  914. port->name, strerror ( rc ) );
  915. /* Erase port details */
  916. memset ( &port->port_id, 0, sizeof ( port->port_id ) );
  917. port->flags = 0;
  918. /* Record logout */
  919. fc_link_err ( &port->link, rc );
  920. /* Notify peers of link state change */
  921. list_for_each_entry_safe ( peer, tmp, &fc_peers, list ) {
  922. fc_peer_get ( peer );
  923. fc_link_examine ( &peer->link );
  924. fc_peer_put ( peer );
  925. }
  926. }
  927. /**
  928. * Handle FLOGI completion
  929. *
  930. * @v port Fibre Channel port
  931. * @v rc Reason for completion
  932. */
  933. static void fc_port_flogi_done ( struct fc_port *port, int rc ) {
  934. intf_restart ( &port->flogi, rc );
  935. if ( rc != 0 )
  936. fc_port_logout ( port, rc );
  937. }
  938. /**
  939. * Handle name server PLOGI completion
  940. *
  941. * @v port Fibre Channel port
  942. * @v rc Reason for completion
  943. */
  944. static void fc_port_ns_plogi_done ( struct fc_port *port, int rc ) {
  945. intf_restart ( &port->ns_plogi, rc );
  946. if ( rc == 0 ) {
  947. port->flags |= FC_PORT_HAS_NS;
  948. DBGC ( port, "FCPORT %s logged in to name server\n",
  949. port->name );
  950. } else {
  951. DBGC ( port, "FCPORT %s could not log in to name server: %s\n",
  952. port->name, strerror ( rc ) );
  953. /* Absence of a name server is not a fatal error */
  954. }
  955. }
  956. /**
  957. * Examine Fibre Channel port link state
  958. *
  959. * @ link Fibre Channel link state monitor
  960. */
  961. static void fc_port_examine ( struct fc_link_state *link ) {
  962. struct fc_port *port = container_of ( link, struct fc_port, link );
  963. int rc;
  964. /* Do nothing if already logged in */
  965. if ( fc_link_ok ( &port->link ) )
  966. return;
  967. DBGC ( port, "FCPORT %s attempting login\n", port->name );
  968. /* Try to create FLOGI ELS */
  969. intf_restart ( &port->flogi, -ECANCELED );
  970. if ( ( rc = fc_els_flogi ( &port->flogi, port ) ) != 0 ) {
  971. DBGC ( port, "FCPORT %s could not initiate FLOGI: %s\n",
  972. port->name, strerror ( rc ) );
  973. fc_port_logout ( port, rc );
  974. return;
  975. }
  976. }
  977. /**
  978. * Handle change of flow control window
  979. *
  980. * @v port Fibre Channel port
  981. */
  982. static void fc_port_window_changed ( struct fc_port *port ) {
  983. size_t window;
  984. /* Check if transport layer is ready */
  985. window = xfer_window ( &port->transport );
  986. if ( window > 0 ) {
  987. /* Transport layer is ready. Start login if the link
  988. * is not already up.
  989. */
  990. if ( ! fc_link_ok ( &port->link ) )
  991. fc_link_start ( &port->link );
  992. } else {
  993. /* Transport layer is not ready. Log out port and
  994. * wait for transport layer before attempting log in
  995. * again.
  996. */
  997. fc_port_logout ( port, -ENOTCONN );
  998. fc_link_stop ( &port->link );
  999. }
  1000. }
  1001. /** Fibre Channel port transport interface operations */
  1002. static struct interface_operation fc_port_transport_op[] = {
  1003. INTF_OP ( xfer_deliver, struct fc_port *, fc_port_deliver ),
  1004. INTF_OP ( xfer_window_changed, struct fc_port *,
  1005. fc_port_window_changed ),
  1006. INTF_OP ( intf_close, struct fc_port *, fc_port_close ),
  1007. };
  1008. /** Fibre Channel port transport interface descriptor */
  1009. static struct interface_descriptor fc_port_transport_desc =
  1010. INTF_DESC ( struct fc_port, transport, fc_port_transport_op );
  1011. /** Fibre Channel port FLOGI interface operations */
  1012. static struct interface_operation fc_port_flogi_op[] = {
  1013. INTF_OP ( intf_close, struct fc_port *, fc_port_flogi_done ),
  1014. };
  1015. /** Fibre Channel port FLOGI interface descriptor */
  1016. static struct interface_descriptor fc_port_flogi_desc =
  1017. INTF_DESC ( struct fc_port, flogi, fc_port_flogi_op );
  1018. /** Fibre Channel port name server PLOGI interface operations */
  1019. static struct interface_operation fc_port_ns_plogi_op[] = {
  1020. INTF_OP ( intf_close, struct fc_port *, fc_port_ns_plogi_done ),
  1021. };
  1022. /** Fibre Channel port name server PLOGI interface descriptor */
  1023. static struct interface_descriptor fc_port_ns_plogi_desc =
  1024. INTF_DESC ( struct fc_port, ns_plogi, fc_port_ns_plogi_op );
  1025. /**
  1026. * Create Fibre Channel port
  1027. *
  1028. * @v transport Transport interface
  1029. * @v node Fibre Channel node name
  1030. * @v port Fibre Channel port name
  1031. * @v name Symbolic port name
  1032. * @ret rc Return status code
  1033. */
  1034. int fc_port_open ( struct interface *transport, const struct fc_name *node_wwn,
  1035. const struct fc_name *port_wwn, const char *name ) {
  1036. struct fc_port *port;
  1037. /* Allocate and initialise structure */
  1038. port = zalloc ( sizeof ( *port ) );
  1039. if ( ! port )
  1040. return -ENOMEM;
  1041. ref_init ( &port->refcnt, NULL );
  1042. intf_init ( &port->transport, &fc_port_transport_desc, &port->refcnt );
  1043. fc_link_init ( &port->link, fc_port_examine, &port->refcnt );
  1044. intf_init ( &port->flogi, &fc_port_flogi_desc, &port->refcnt );
  1045. intf_init ( &port->ns_plogi, &fc_port_ns_plogi_desc, &port->refcnt );
  1046. list_add_tail ( &port->list, &fc_ports );
  1047. INIT_LIST_HEAD ( &port->xchgs );
  1048. memcpy ( &port->node_wwn, node_wwn, sizeof ( port->node_wwn ) );
  1049. memcpy ( &port->port_wwn, port_wwn, sizeof ( port->port_wwn ) );
  1050. snprintf ( port->name, sizeof ( port->name ), "%s", name );
  1051. DBGC ( port, "FCPORT %s opened as %s",
  1052. port->name, fc_ntoa ( &port->node_wwn ) );
  1053. DBGC ( port, " port %s\n", fc_ntoa ( &port->port_wwn ) );
  1054. /* Attach to transport layer, mortalise self, and return */
  1055. intf_plug_plug ( &port->transport, transport );
  1056. ref_put ( &port->refcnt );
  1057. return 0;
  1058. }
  1059. /**
  1060. * Find Fibre Channel port by name
  1061. *
  1062. * @v name Fibre Channel port name
  1063. * @ret port Fibre Channel port, or NULL
  1064. */
  1065. struct fc_port * fc_port_find ( const char *name ) {
  1066. struct fc_port *port;
  1067. list_for_each_entry ( port, &fc_ports, list ) {
  1068. if ( strcmp ( name, port->name ) == 0 )
  1069. return port;
  1070. }
  1071. return NULL;
  1072. }
  1073. /******************************************************************************
  1074. *
  1075. * Fibre Channel peers
  1076. *
  1077. ******************************************************************************
  1078. */
  1079. /**
  1080. * Close Fibre Channel peer
  1081. *
  1082. * @v peer Fibre Channel peer
  1083. * @v rc Reason for close
  1084. */
  1085. static void fc_peer_close ( struct fc_peer *peer, int rc ) {
  1086. DBGC ( peer, "FCPEER %s closed: %s\n",
  1087. fc_ntoa ( &peer->port_wwn ) , strerror ( rc ) );
  1088. /* Sanity check */
  1089. assert ( list_empty ( &peer->ulps ) );
  1090. /* Stop link timer */
  1091. fc_link_stop ( &peer->link );
  1092. /* Shut down interfaces */
  1093. intf_shutdown ( &peer->plogi, rc );
  1094. /* Remove from list of peers */
  1095. list_del ( &peer->list );
  1096. INIT_LIST_HEAD ( &peer->list );
  1097. }
  1098. /**
  1099. * Increment Fibre Channel peer active usage count
  1100. *
  1101. * @v peer Fibre Channel peer
  1102. */
  1103. static void fc_peer_increment ( struct fc_peer *peer ) {
  1104. /* Increment our usage count */
  1105. peer->usage++;
  1106. }
  1107. /**
  1108. * Decrement Fibre Channel peer active usage count
  1109. *
  1110. * @v peer Fibre Channel peer
  1111. */
  1112. static void fc_peer_decrement ( struct fc_peer *peer ) {
  1113. /* Sanity check */
  1114. assert ( peer->usage > 0 );
  1115. /* Decrement our usage count and log out if we reach zero */
  1116. if ( --(peer->usage) == 0 )
  1117. fc_peer_logout ( peer, 0 );
  1118. }
  1119. /**
  1120. * Log in Fibre Channel peer
  1121. *
  1122. * @v peer Fibre Channel peer
  1123. * @v port Fibre Channel port
  1124. * @v port_id Port ID
  1125. * @ret rc Return status code
  1126. */
  1127. int fc_peer_login ( struct fc_peer *peer, struct fc_port *port,
  1128. struct fc_port_id *port_id ) {
  1129. struct fc_ulp *ulp;
  1130. struct fc_ulp *tmp;
  1131. /* Perform implicit logout if logged in and details differ */
  1132. if ( fc_link_ok ( &peer->link ) &&
  1133. ( ( peer->port != port ) ||
  1134. ( memcmp ( &peer->port_id, port_id,
  1135. sizeof ( peer->port_id ) ) !=0 ) ) ) {
  1136. fc_peer_logout ( peer, 0 );
  1137. }
  1138. /* Log in, if applicable */
  1139. if ( ! fc_link_ok ( &peer->link ) ) {
  1140. /* Record peer details */
  1141. assert ( peer->port == NULL );
  1142. peer->port = fc_port_get ( port );
  1143. memcpy ( &peer->port_id, port_id, sizeof ( peer->port_id ) );
  1144. DBGC ( peer, "FCPEER %s logged in via %s as %s\n",
  1145. fc_ntoa ( &peer->port_wwn ), peer->port->name,
  1146. fc_id_ntoa ( &peer->port_id ) );
  1147. /* Add login reference */
  1148. fc_peer_get ( peer );
  1149. }
  1150. /* Record login */
  1151. fc_link_up ( &peer->link );
  1152. /* Notify ULPs of link state change */
  1153. list_for_each_entry_safe ( ulp, tmp, &peer->ulps, list ) {
  1154. fc_ulp_get ( ulp );
  1155. fc_link_examine ( &ulp->link );
  1156. fc_ulp_put ( ulp );
  1157. }
  1158. return 0;
  1159. }
  1160. /**
  1161. * Log out Fibre Channel peer
  1162. *
  1163. * @v peer Fibre Channel peer
  1164. * @v rc Reason for logout
  1165. */
  1166. void fc_peer_logout ( struct fc_peer *peer, int rc ) {
  1167. struct fc_ulp *ulp;
  1168. struct fc_ulp *tmp;
  1169. DBGC ( peer, "FCPEER %s logged out: %s\n",
  1170. fc_ntoa ( &peer->port_wwn ), strerror ( rc ) );
  1171. /* Drop login reference, if applicable */
  1172. if ( fc_link_ok ( &peer->link ) )
  1173. fc_peer_put ( peer );
  1174. /* Erase peer details */
  1175. fc_port_put ( peer->port );
  1176. peer->port = NULL;
  1177. /* Record logout */
  1178. fc_link_err ( &peer->link, rc );
  1179. /* Notify ULPs of link state change */
  1180. list_for_each_entry_safe ( ulp, tmp, &peer->ulps, list ) {
  1181. fc_ulp_get ( ulp );
  1182. fc_link_examine ( &ulp->link );
  1183. fc_ulp_put ( ulp );
  1184. }
  1185. /* Close peer if there are no active users */
  1186. if ( peer->usage == 0 )
  1187. fc_peer_close ( peer, rc );
  1188. }
  1189. /**
  1190. * Handle PLOGI completion
  1191. *
  1192. * @v peer Fibre Channel peer
  1193. * @v rc Reason for completion
  1194. */
  1195. static void fc_peer_plogi_done ( struct fc_peer *peer, int rc ) {
  1196. intf_restart ( &peer->plogi, rc );
  1197. if ( rc != 0 )
  1198. fc_peer_logout ( peer, rc );
  1199. }
  1200. /**
  1201. * Initiate PLOGI
  1202. *
  1203. * @v peer Fibre Channel peer
  1204. * @v port Fibre Channel port
  1205. * @v peer_port_id Peer port ID
  1206. * @ret rc Return status code
  1207. */
  1208. static int fc_peer_plogi ( struct fc_peer *peer, struct fc_port *port,
  1209. struct fc_port_id *peer_port_id ) {
  1210. int rc;
  1211. /* Try to create PLOGI ELS */
  1212. intf_restart ( &peer->plogi, -ECANCELED );
  1213. if ( ( rc = fc_els_plogi ( &peer->plogi, port, peer_port_id ) ) != 0 ) {
  1214. DBGC ( peer, "FCPEER %s could not initiate PLOGI: %s\n",
  1215. fc_ntoa ( &peer->port_wwn ), strerror ( rc ) );
  1216. fc_peer_logout ( peer, rc );
  1217. return rc;
  1218. }
  1219. return 0;
  1220. }
  1221. /**
  1222. * Examine Fibre Channel peer link state
  1223. *
  1224. * @ link Fibre Channel link state monitor
  1225. */
  1226. static void fc_peer_examine ( struct fc_link_state *link ) {
  1227. struct fc_peer *peer = container_of ( link, struct fc_peer, link );
  1228. struct fc_port *port;
  1229. int rc;
  1230. /* Check to see if underlying port link has gone down */
  1231. if ( peer->port && ( ! fc_link_ok ( &peer->port->link ) ) ) {
  1232. fc_peer_logout ( peer, -ENOTCONN );
  1233. return;
  1234. }
  1235. /* Do nothing if already logged in */
  1236. if ( fc_link_ok ( &peer->link ) )
  1237. return;
  1238. DBGC ( peer, "FCPEER %s attempting login\n",
  1239. fc_ntoa ( &peer->port_wwn ) );
  1240. /* Sanity check */
  1241. assert ( peer->port == NULL );
  1242. /* First, look for a port with the peer attached via a
  1243. * point-to-point link.
  1244. */
  1245. list_for_each_entry ( port, &fc_ports, list ) {
  1246. if ( fc_link_ok ( &port->link ) &&
  1247. ( ! ( port->flags & FC_PORT_HAS_FABRIC ) ) &&
  1248. ( memcmp ( &peer->port_wwn, &port->link_port_wwn,
  1249. sizeof ( peer->port_wwn ) ) == 0 ) ) {
  1250. /* Use this peer port ID, and stop looking */
  1251. fc_peer_plogi ( peer, port, &port->ptp_link_port_id );
  1252. return;
  1253. }
  1254. }
  1255. /* If the peer is not directly attached, try initiating a name
  1256. * server lookup on any suitable ports.
  1257. */
  1258. list_for_each_entry ( port, &fc_ports, list ) {
  1259. if ( fc_link_ok ( &port->link ) &&
  1260. ( port->flags & FC_PORT_HAS_FABRIC ) &&
  1261. ( port->flags & FC_PORT_HAS_NS ) ) {
  1262. if ( ( rc = fc_ns_query ( peer, port,
  1263. fc_peer_plogi ) ) != 0 ) {
  1264. DBGC ( peer, "FCPEER %s could not attempt "
  1265. "name server lookup on %s: %s\n",
  1266. fc_ntoa ( &peer->port_wwn ), port->name,
  1267. strerror ( rc ) );
  1268. /* Non-fatal */
  1269. }
  1270. }
  1271. }
  1272. }
  1273. /** Fibre Channel peer PLOGI interface operations */
  1274. static struct interface_operation fc_peer_plogi_op[] = {
  1275. INTF_OP ( intf_close, struct fc_peer *, fc_peer_plogi_done ),
  1276. };
  1277. /** Fibre Channel peer PLOGI interface descriptor */
  1278. static struct interface_descriptor fc_peer_plogi_desc =
  1279. INTF_DESC ( struct fc_peer, plogi, fc_peer_plogi_op );
  1280. /**
  1281. * Create Fibre Channel peer
  1282. *
  1283. * @v port_wwn Node name
  1284. * @ret peer Fibre Channel peer, or NULL
  1285. */
  1286. static struct fc_peer * fc_peer_create ( const struct fc_name *port_wwn ) {
  1287. struct fc_peer *peer;
  1288. /* Allocate and initialise structure */
  1289. peer = zalloc ( sizeof ( *peer ) );
  1290. if ( ! peer )
  1291. return NULL;
  1292. ref_init ( &peer->refcnt, NULL );
  1293. fc_link_init ( &peer->link, fc_peer_examine, &peer->refcnt );
  1294. intf_init ( &peer->plogi, &fc_peer_plogi_desc, &peer->refcnt );
  1295. list_add_tail ( &peer->list, &fc_peers );
  1296. memcpy ( &peer->port_wwn, port_wwn, sizeof ( peer->port_wwn ) );
  1297. INIT_LIST_HEAD ( &peer->ulps );
  1298. /* Start link monitor */
  1299. fc_link_start ( &peer->link );
  1300. DBGC ( peer, "FCPEER %s created\n", fc_ntoa ( &peer->port_wwn ) );
  1301. return peer;
  1302. }
  1303. /**
  1304. * Get Fibre Channel peer by node name
  1305. *
  1306. * @v port_wwn Node name
  1307. * @ret peer Fibre Channel peer, or NULL
  1308. */
  1309. struct fc_peer * fc_peer_get_wwn ( const struct fc_name *port_wwn ) {
  1310. struct fc_peer *peer;
  1311. /* Look for an existing peer */
  1312. list_for_each_entry ( peer, &fc_peers, list ) {
  1313. if ( memcmp ( &peer->port_wwn, port_wwn,
  1314. sizeof ( peer->port_wwn ) ) == 0 )
  1315. return fc_peer_get ( peer );
  1316. }
  1317. /* Create a new peer */
  1318. peer = fc_peer_create ( port_wwn );
  1319. if ( ! peer )
  1320. return NULL;
  1321. return peer;
  1322. }
  1323. /**
  1324. * Get Fibre Channel peer by port ID
  1325. *
  1326. * @v port Fibre Channel port
  1327. * @v peer_port_id Peer port ID
  1328. * @ret peer Fibre Channel peer, or NULL
  1329. */
  1330. struct fc_peer * fc_peer_get_port_id ( struct fc_port *port,
  1331. const struct fc_port_id *peer_port_id ){
  1332. struct fc_peer *peer;
  1333. /* Look for an existing peer */
  1334. list_for_each_entry ( peer, &fc_peers, list ) {
  1335. if ( ( peer->port == port ) &&
  1336. ( memcmp ( &peer->port_id, peer_port_id,
  1337. sizeof ( peer->port_id ) ) == 0 ) )
  1338. return fc_peer_get ( peer );
  1339. }
  1340. /* Cannot create a new peer, since we have no port name to use */
  1341. return NULL;
  1342. }
  1343. /******************************************************************************
  1344. *
  1345. * Fibre Channel upper-layer protocols
  1346. *
  1347. ******************************************************************************
  1348. */
  1349. /**
  1350. * Free Fibre Channel upper-layer protocol
  1351. *
  1352. * @v refcnt Reference count
  1353. */
  1354. static void fc_ulp_free ( struct refcnt *refcnt ) {
  1355. struct fc_ulp *ulp = container_of ( refcnt, struct fc_ulp, refcnt );
  1356. fc_peer_put ( ulp->peer );
  1357. free ( ulp );
  1358. }
  1359. /**
  1360. * Close Fibre Channel upper-layer protocol
  1361. *
  1362. * @v ulp Fibre Channel upper-layer protocol
  1363. * @v rc Reason for close
  1364. */
  1365. static void fc_ulp_close ( struct fc_ulp *ulp, int rc ) {
  1366. DBGC ( ulp, "FCULP %s/%02x closed: %s\n",
  1367. fc_ntoa ( &ulp->peer->port_wwn ), ulp->type, strerror ( rc ) );
  1368. /* Sanity check */
  1369. assert ( list_empty ( &ulp->users ) );
  1370. /* Stop link monitor */
  1371. fc_link_stop ( &ulp->link );
  1372. /* Shut down interfaces */
  1373. intf_shutdown ( &ulp->prli, rc );
  1374. /* Remove from list of ULPs */
  1375. list_del ( &ulp->list );
  1376. INIT_LIST_HEAD ( &ulp->list );
  1377. }
  1378. /**
  1379. * Attach Fibre Channel upper-layer protocol user
  1380. *
  1381. * @v ulp Fibre Channel upper-layer protocol
  1382. * @v user Fibre Channel upper-layer protocol user
  1383. */
  1384. void fc_ulp_attach ( struct fc_ulp *ulp, struct fc_ulp_user *user ) {
  1385. /* Sanity check */
  1386. assert ( user->ulp == NULL );
  1387. /* Increment peer's usage count */
  1388. fc_peer_increment ( ulp->peer );
  1389. /* Attach user */
  1390. user->ulp = fc_ulp_get ( ulp );
  1391. list_add ( &user->list, &ulp->users );
  1392. }
  1393. /**
  1394. * Detach Fibre Channel upper-layer protocol user
  1395. *
  1396. * @v user Fibre Channel upper-layer protocol user
  1397. */
  1398. void fc_ulp_detach ( struct fc_ulp_user *user ) {
  1399. struct fc_ulp *ulp = user->ulp;
  1400. /* Do nothing if not attached */
  1401. if ( ! ulp )
  1402. return;
  1403. /* Sanity checks */
  1404. list_check_contains_entry ( user, &ulp->users, list );
  1405. /* Detach user and log out if no users remain */
  1406. list_del ( &user->list );
  1407. if ( list_empty ( &ulp->users ) )
  1408. fc_ulp_logout ( ulp, 0 );
  1409. /* Decrement our peer's usage count */
  1410. fc_peer_decrement ( ulp->peer );
  1411. /* Drop reference */
  1412. user->ulp = NULL;
  1413. fc_ulp_put ( ulp );
  1414. }
  1415. /**
  1416. * Log in Fibre Channel upper-layer protocol
  1417. *
  1418. * @v ulp Fibre Channel upper-layer protocol
  1419. * @v param Service parameters
  1420. * @v param_len Length of service parameters
  1421. * @v originated Login was originated by us
  1422. * @ret rc Return status code
  1423. */
  1424. int fc_ulp_login ( struct fc_ulp *ulp, const void *param, size_t param_len,
  1425. int originated ) {
  1426. struct fc_ulp_user *user;
  1427. struct fc_ulp_user *tmp;
  1428. /* Perform implicit logout if logged in and service parameters differ */
  1429. if ( fc_link_ok ( &ulp->link ) &&
  1430. ( ( ulp->param_len != param_len ) ||
  1431. ( memcmp ( ulp->param, param, ulp->param_len ) != 0 ) ) ) {
  1432. fc_ulp_logout ( ulp, 0 );
  1433. }
  1434. /* Work around a bug in some versions of the Linux Fibre
  1435. * Channel stack, which fail to fully initialise image pairs
  1436. * established via a PRLI originated by the Linux stack
  1437. * itself.
  1438. */
  1439. if ( originated )
  1440. ulp->flags |= FC_ULP_ORIGINATED_LOGIN_OK;
  1441. if ( ! ( ulp->flags & FC_ULP_ORIGINATED_LOGIN_OK ) ) {
  1442. DBGC ( ulp, "FCULP %s/%02x sending extra PRLI to work around "
  1443. "Linux bug\n",
  1444. fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
  1445. fc_link_stop ( &ulp->link );
  1446. fc_link_start ( &ulp->link );
  1447. return 0;
  1448. }
  1449. /* Log in, if applicable */
  1450. if ( ! fc_link_ok ( &ulp->link ) ) {
  1451. /* Record service parameters */
  1452. assert ( ulp->param == NULL );
  1453. assert ( ulp->param_len == 0 );
  1454. ulp->param = malloc ( param_len );
  1455. if ( ! ulp->param ) {
  1456. DBGC ( ulp, "FCULP %s/%02x could not record "
  1457. "parameters\n",
  1458. fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
  1459. return -ENOMEM;
  1460. }
  1461. memcpy ( ulp->param, param, param_len );
  1462. ulp->param_len = param_len;
  1463. DBGC ( ulp, "FCULP %s/%02x logged in with parameters:\n",
  1464. fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
  1465. DBGC_HDA ( ulp, 0, ulp->param, ulp->param_len );
  1466. /* Add login reference */
  1467. fc_ulp_get ( ulp );
  1468. }
  1469. /* Record login */
  1470. fc_link_up ( &ulp->link );
  1471. /* Notify users of link state change */
  1472. list_for_each_entry_safe ( user, tmp, &ulp->users, list ) {
  1473. fc_ulp_user_get ( user );
  1474. user->examine ( user );
  1475. fc_ulp_user_put ( user );
  1476. }
  1477. return 0;
  1478. }
  1479. /**
  1480. * Log out Fibre Channel upper-layer protocol
  1481. *
  1482. * @v ulp Fibre Channel upper-layer protocol
  1483. * @v rc Reason for logout
  1484. */
  1485. void fc_ulp_logout ( struct fc_ulp *ulp, int rc ) {
  1486. struct fc_ulp_user *user;
  1487. struct fc_ulp_user *tmp;
  1488. DBGC ( ulp, "FCULP %s/%02x logged out: %s\n",
  1489. fc_ntoa ( &ulp->peer->port_wwn ), ulp->type, strerror ( rc ) );
  1490. /* Drop login reference, if applicable */
  1491. if ( fc_link_ok ( &ulp->link ) )
  1492. fc_ulp_put ( ulp );
  1493. /* Discard service parameters */
  1494. free ( ulp->param );
  1495. ulp->param = NULL;
  1496. ulp->param_len = 0;
  1497. ulp->flags = 0;
  1498. /* Record logout */
  1499. fc_link_err ( &ulp->link, rc );
  1500. /* Notify users of link state change */
  1501. list_for_each_entry_safe ( user, tmp, &ulp->users, list ) {
  1502. fc_ulp_user_get ( user );
  1503. user->examine ( user );
  1504. fc_ulp_user_put ( user );
  1505. }
  1506. /* Close ULP if there are no clients attached */
  1507. if ( list_empty ( &ulp->users ) )
  1508. fc_ulp_close ( ulp, rc );
  1509. }
  1510. /**
  1511. * Handle PRLI completion
  1512. *
  1513. * @v ulp Fibre Channel upper-layer protocol
  1514. * @v rc Reason for completion
  1515. */
  1516. static void fc_ulp_prli_done ( struct fc_ulp *ulp, int rc ) {
  1517. intf_restart ( &ulp->prli, rc );
  1518. if ( rc != 0 )
  1519. fc_ulp_logout ( ulp, rc );
  1520. }
  1521. /**
  1522. * Examine Fibre Channel upper-layer protocol link state
  1523. *
  1524. * @ link Fibre Channel link state monitor
  1525. */
  1526. static void fc_ulp_examine ( struct fc_link_state *link ) {
  1527. struct fc_ulp *ulp = container_of ( link, struct fc_ulp, link );
  1528. int rc;
  1529. /* Check to see if underlying peer link has gone down */
  1530. if ( ! fc_link_ok ( &ulp->peer->link ) ) {
  1531. fc_ulp_logout ( ulp, -ENOTCONN );
  1532. return;
  1533. }
  1534. /* Do nothing if already logged in */
  1535. if ( fc_link_ok ( &ulp->link ) &&
  1536. ( ulp->flags & FC_ULP_ORIGINATED_LOGIN_OK ) )
  1537. return;
  1538. DBGC ( ulp, "FCULP %s/%02x attempting login\n",
  1539. fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
  1540. /* Try to create PRLI ELS */
  1541. intf_restart ( &ulp->prli, -ECANCELED );
  1542. if ( ( rc = fc_els_prli ( &ulp->prli, ulp->peer->port,
  1543. &ulp->peer->port_id, ulp->type ) ) != 0 ) {
  1544. DBGC ( ulp, "FCULP %s/%02x could not initiate PRLI: %s\n",
  1545. fc_ntoa ( &ulp->peer->port_wwn ), ulp->type,
  1546. strerror ( rc ) );
  1547. fc_ulp_logout ( ulp, rc );
  1548. return;
  1549. }
  1550. }
  1551. /** Fibre Channel upper-layer protocol PRLI interface operations */
  1552. static struct interface_operation fc_ulp_prli_op[] = {
  1553. INTF_OP ( intf_close, struct fc_ulp *, fc_ulp_prli_done ),
  1554. };
  1555. /** Fibre Channel upper-layer protocol PRLI interface descriptor */
  1556. static struct interface_descriptor fc_ulp_prli_desc =
  1557. INTF_DESC ( struct fc_ulp, prli, fc_ulp_prli_op );
  1558. /**
  1559. * Create Fibre Channel upper-layer protocl
  1560. *
  1561. * @v peer Fibre Channel peer
  1562. * @v type Type
  1563. * @ret ulp Fibre Channel upper-layer protocol, or NULL
  1564. */
  1565. static struct fc_ulp * fc_ulp_create ( struct fc_peer *peer,
  1566. unsigned int type ) {
  1567. struct fc_ulp *ulp;
  1568. /* Allocate and initialise structure */
  1569. ulp = zalloc ( sizeof ( *ulp ) );
  1570. if ( ! ulp )
  1571. return NULL;
  1572. ref_init ( &ulp->refcnt, fc_ulp_free );
  1573. fc_link_init ( &ulp->link, fc_ulp_examine, &ulp->refcnt );
  1574. intf_init ( &ulp->prli, &fc_ulp_prli_desc, &ulp->refcnt );
  1575. ulp->peer = fc_peer_get ( peer );
  1576. list_add_tail ( &ulp->list, &peer->ulps );
  1577. ulp->type = type;
  1578. INIT_LIST_HEAD ( &ulp->users );
  1579. /* Start link state monitor */
  1580. fc_link_start ( &ulp->link );
  1581. DBGC ( ulp, "FCULP %s/%02x created\n",
  1582. fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
  1583. return ulp;
  1584. }
  1585. /**
  1586. * Get Fibre Channel upper-layer protocol by peer and type
  1587. *
  1588. * @v peer Fibre Channel peer
  1589. * @v type Type
  1590. * @ret ulp Fibre Channel upper-layer protocol, or NULL
  1591. */
  1592. static struct fc_ulp * fc_ulp_get_type ( struct fc_peer *peer,
  1593. unsigned int type ) {
  1594. struct fc_ulp *ulp;
  1595. /* Look for an existing ULP */
  1596. list_for_each_entry ( ulp, &peer->ulps, list ) {
  1597. if ( ulp->type == type )
  1598. return fc_ulp_get ( ulp );
  1599. }
  1600. /* Create a new ULP */
  1601. ulp = fc_ulp_create ( peer, type );
  1602. if ( ! ulp )
  1603. return NULL;
  1604. return ulp;
  1605. }
  1606. /**
  1607. * Get Fibre Channel upper-layer protocol by port name and type
  1608. *
  1609. * @v port_wwn Port name
  1610. * @v type Type
  1611. * @ret ulp Fibre Channel upper-layer protocol, or NULL
  1612. */
  1613. struct fc_ulp * fc_ulp_get_wwn_type ( const struct fc_name *port_wwn,
  1614. unsigned int type ) {
  1615. struct fc_ulp *ulp;
  1616. struct fc_peer *peer;
  1617. /* Get peer */
  1618. peer = fc_peer_get_wwn ( port_wwn );
  1619. if ( ! peer )
  1620. goto err_peer_get_wwn;
  1621. /* Get ULP */
  1622. ulp = fc_ulp_get_type ( peer, type );
  1623. if ( ! ulp )
  1624. goto err_ulp_get_type;
  1625. /* Drop temporary reference to peer */
  1626. fc_peer_put ( peer );
  1627. return ulp;
  1628. fc_ulp_put ( ulp );
  1629. err_ulp_get_type:
  1630. fc_peer_put ( peer );
  1631. err_peer_get_wwn:
  1632. return NULL;
  1633. }
  1634. /**
  1635. * Get Fibre Channel upper-layer protocol by port ID and type
  1636. *
  1637. * @v port Fibre Channel port
  1638. * @v peer_port_id Peer port ID
  1639. * @v type Type
  1640. * @ret ulp Fibre Channel upper-layer protocol, or NULL
  1641. */
  1642. struct fc_ulp * fc_ulp_get_port_id_type ( struct fc_port *port,
  1643. const struct fc_port_id *peer_port_id,
  1644. unsigned int type ) {
  1645. struct fc_ulp *ulp;
  1646. struct fc_peer *peer;
  1647. /* Get peer */
  1648. peer = fc_peer_get_port_id ( port, peer_port_id );
  1649. if ( ! peer )
  1650. goto err_peer_get_wwn;
  1651. /* Get ULP */
  1652. ulp = fc_ulp_get_type ( peer, type );
  1653. if ( ! ulp )
  1654. goto err_ulp_get_type;
  1655. /* Drop temporary reference to peer */
  1656. fc_peer_put ( peer );
  1657. return ulp;
  1658. fc_ulp_put ( ulp );
  1659. err_ulp_get_type:
  1660. fc_peer_put ( peer );
  1661. err_peer_get_wwn:
  1662. return NULL;
  1663. }
  1664. /* Drag in objects via fc_ports */
  1665. REQUIRING_SYMBOL ( fc_ports );
  1666. /* Drag in Fibre Channel configuration */
  1667. REQUIRE_OBJECT ( config_fc );