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.

fc.c 49KB

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