Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

fc.c 50KB

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