fc_exch.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
  4. * Copyright(c) 2008 Mike Christie
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Maintained at www.Open-FCoE.org
  20. */
  21. /*
  22. * Fibre Channel exchange and sequence handling.
  23. */
  24. #include <linux/timer.h>
  25. #include <linux/slab.h>
  26. #include <linux/err.h>
  27. #include <scsi/fc/fc_fc2.h>
  28. #include <scsi/libfc.h>
  29. #include <scsi/fc_encode.h>
  30. #include "fc_libfc.h"
  31. u16 fc_cpu_mask; /* cpu mask for possible cpus */
  32. EXPORT_SYMBOL(fc_cpu_mask);
  33. static u16 fc_cpu_order; /* 2's power to represent total possible cpus */
  34. static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
  35. static struct workqueue_struct *fc_exch_workqueue;
  36. /*
  37. * Structure and function definitions for managing Fibre Channel Exchanges
  38. * and Sequences.
  39. *
  40. * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
  41. *
  42. * fc_exch_mgr holds the exchange state for an N port
  43. *
  44. * fc_exch holds state for one exchange and links to its active sequence.
  45. *
  46. * fc_seq holds the state for an individual sequence.
  47. */
  48. /**
  49. * struct fc_exch_pool - Per cpu exchange pool
  50. * @next_index: Next possible free exchange index
  51. * @total_exches: Total allocated exchanges
  52. * @lock: Exch pool lock
  53. * @ex_list: List of exchanges
  54. *
  55. * This structure manages per cpu exchanges in array of exchange pointers.
  56. * This array is allocated followed by struct fc_exch_pool memory for
  57. * assigned range of exchanges to per cpu pool.
  58. */
  59. struct fc_exch_pool {
  60. u16 next_index;
  61. u16 total_exches;
  62. /* two cache of free slot in exch array */
  63. u16 left;
  64. u16 right;
  65. spinlock_t lock;
  66. struct list_head ex_list;
  67. };
  68. /**
  69. * struct fc_exch_mgr - The Exchange Manager (EM).
  70. * @class: Default class for new sequences
  71. * @kref: Reference counter
  72. * @min_xid: Minimum exchange ID
  73. * @max_xid: Maximum exchange ID
  74. * @ep_pool: Reserved exchange pointers
  75. * @pool_max_index: Max exch array index in exch pool
  76. * @pool: Per cpu exch pool
  77. * @stats: Statistics structure
  78. *
  79. * This structure is the center for creating exchanges and sequences.
  80. * It manages the allocation of exchange IDs.
  81. */
  82. struct fc_exch_mgr {
  83. enum fc_class class;
  84. struct kref kref;
  85. u16 min_xid;
  86. u16 max_xid;
  87. mempool_t *ep_pool;
  88. u16 pool_max_index;
  89. struct fc_exch_pool *pool;
  90. /*
  91. * currently exchange mgr stats are updated but not used.
  92. * either stats can be expose via sysfs or remove them
  93. * all together if not used XXX
  94. */
  95. struct {
  96. atomic_t no_free_exch;
  97. atomic_t no_free_exch_xid;
  98. atomic_t xid_not_found;
  99. atomic_t xid_busy;
  100. atomic_t seq_not_found;
  101. atomic_t non_bls_resp;
  102. } stats;
  103. };
  104. /**
  105. * struct fc_exch_mgr_anchor - primary structure for list of EMs
  106. * @ema_list: Exchange Manager Anchor list
  107. * @mp: Exchange Manager associated with this anchor
  108. * @match: Routine to determine if this anchor's EM should be used
  109. *
  110. * When walking the list of anchors the match routine will be called
  111. * for each anchor to determine if that EM should be used. The last
  112. * anchor in the list will always match to handle any exchanges not
  113. * handled by other EMs. The non-default EMs would be added to the
  114. * anchor list by HW that provides FCoE offloads.
  115. */
  116. struct fc_exch_mgr_anchor {
  117. struct list_head ema_list;
  118. struct fc_exch_mgr *mp;
  119. bool (*match)(struct fc_frame *);
  120. };
  121. static void fc_exch_rrq(struct fc_exch *);
  122. static void fc_seq_ls_acc(struct fc_frame *);
  123. static void fc_seq_ls_rjt(struct fc_frame *, enum fc_els_rjt_reason,
  124. enum fc_els_rjt_explan);
  125. static void fc_exch_els_rec(struct fc_frame *);
  126. static void fc_exch_els_rrq(struct fc_frame *);
  127. /*
  128. * Internal implementation notes.
  129. *
  130. * The exchange manager is one by default in libfc but LLD may choose
  131. * to have one per CPU. The sequence manager is one per exchange manager
  132. * and currently never separated.
  133. *
  134. * Section 9.8 in FC-FS-2 specifies: "The SEQ_ID is a one-byte field
  135. * assigned by the Sequence Initiator that shall be unique for a specific
  136. * D_ID and S_ID pair while the Sequence is open." Note that it isn't
  137. * qualified by exchange ID, which one might think it would be.
  138. * In practice this limits the number of open sequences and exchanges to 256
  139. * per session. For most targets we could treat this limit as per exchange.
  140. *
  141. * The exchange and its sequence are freed when the last sequence is received.
  142. * It's possible for the remote port to leave an exchange open without
  143. * sending any sequences.
  144. *
  145. * Notes on reference counts:
  146. *
  147. * Exchanges are reference counted and exchange gets freed when the reference
  148. * count becomes zero.
  149. *
  150. * Timeouts:
  151. * Sequences are timed out for E_D_TOV and R_A_TOV.
  152. *
  153. * Sequence event handling:
  154. *
  155. * The following events may occur on initiator sequences:
  156. *
  157. * Send.
  158. * For now, the whole thing is sent.
  159. * Receive ACK
  160. * This applies only to class F.
  161. * The sequence is marked complete.
  162. * ULP completion.
  163. * The upper layer calls fc_exch_done() when done
  164. * with exchange and sequence tuple.
  165. * RX-inferred completion.
  166. * When we receive the next sequence on the same exchange, we can
  167. * retire the previous sequence ID. (XXX not implemented).
  168. * Timeout.
  169. * R_A_TOV frees the sequence ID. If we're waiting for ACK,
  170. * E_D_TOV causes abort and calls upper layer response handler
  171. * with FC_EX_TIMEOUT error.
  172. * Receive RJT
  173. * XXX defer.
  174. * Send ABTS
  175. * On timeout.
  176. *
  177. * The following events may occur on recipient sequences:
  178. *
  179. * Receive
  180. * Allocate sequence for first frame received.
  181. * Hold during receive handler.
  182. * Release when final frame received.
  183. * Keep status of last N of these for the ELS RES command. XXX TBD.
  184. * Receive ABTS
  185. * Deallocate sequence
  186. * Send RJT
  187. * Deallocate
  188. *
  189. * For now, we neglect conditions where only part of a sequence was
  190. * received or transmitted, or where out-of-order receipt is detected.
  191. */
  192. /*
  193. * Locking notes:
  194. *
  195. * The EM code run in a per-CPU worker thread.
  196. *
  197. * To protect against concurrency between a worker thread code and timers,
  198. * sequence allocation and deallocation must be locked.
  199. * - exchange refcnt can be done atomicly without locks.
  200. * - sequence allocation must be locked by exch lock.
  201. * - If the EM pool lock and ex_lock must be taken at the same time, then the
  202. * EM pool lock must be taken before the ex_lock.
  203. */
  204. /*
  205. * opcode names for debugging.
  206. */
  207. static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
  208. /**
  209. * fc_exch_name_lookup() - Lookup name by opcode
  210. * @op: Opcode to be looked up
  211. * @table: Opcode/name table
  212. * @max_index: Index not to be exceeded
  213. *
  214. * This routine is used to determine a human-readable string identifying
  215. * a R_CTL opcode.
  216. */
  217. static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
  218. unsigned int max_index)
  219. {
  220. const char *name = NULL;
  221. if (op < max_index)
  222. name = table[op];
  223. if (!name)
  224. name = "unknown";
  225. return name;
  226. }
  227. /**
  228. * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
  229. * @op: The opcode to be looked up
  230. */
  231. static const char *fc_exch_rctl_name(unsigned int op)
  232. {
  233. return fc_exch_name_lookup(op, fc_exch_rctl_names,
  234. ARRAY_SIZE(fc_exch_rctl_names));
  235. }
  236. /**
  237. * fc_exch_hold() - Increment an exchange's reference count
  238. * @ep: Echange to be held
  239. */
  240. static inline void fc_exch_hold(struct fc_exch *ep)
  241. {
  242. atomic_inc(&ep->ex_refcnt);
  243. }
  244. /**
  245. * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
  246. * and determine SOF and EOF.
  247. * @ep: The exchange to that will use the header
  248. * @fp: The frame whose header is to be modified
  249. * @f_ctl: F_CTL bits that will be used for the frame header
  250. *
  251. * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
  252. * fh_seq_id, fh_seq_cnt and the SOF and EOF.
  253. */
  254. static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
  255. u32 f_ctl)
  256. {
  257. struct fc_frame_header *fh = fc_frame_header_get(fp);
  258. u16 fill;
  259. fr_sof(fp) = ep->class;
  260. if (ep->seq.cnt)
  261. fr_sof(fp) = fc_sof_normal(ep->class);
  262. if (f_ctl & FC_FC_END_SEQ) {
  263. fr_eof(fp) = FC_EOF_T;
  264. if (fc_sof_needs_ack(ep->class))
  265. fr_eof(fp) = FC_EOF_N;
  266. /*
  267. * From F_CTL.
  268. * The number of fill bytes to make the length a 4-byte
  269. * multiple is the low order 2-bits of the f_ctl.
  270. * The fill itself will have been cleared by the frame
  271. * allocation.
  272. * After this, the length will be even, as expected by
  273. * the transport.
  274. */
  275. fill = fr_len(fp) & 3;
  276. if (fill) {
  277. fill = 4 - fill;
  278. /* TODO, this may be a problem with fragmented skb */
  279. skb_put(fp_skb(fp), fill);
  280. hton24(fh->fh_f_ctl, f_ctl | fill);
  281. }
  282. } else {
  283. WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
  284. fr_eof(fp) = FC_EOF_N;
  285. }
  286. /*
  287. * Initialize remainig fh fields
  288. * from fc_fill_fc_hdr
  289. */
  290. fh->fh_ox_id = htons(ep->oxid);
  291. fh->fh_rx_id = htons(ep->rxid);
  292. fh->fh_seq_id = ep->seq.id;
  293. fh->fh_seq_cnt = htons(ep->seq.cnt);
  294. }
  295. /**
  296. * fc_exch_release() - Decrement an exchange's reference count
  297. * @ep: Exchange to be released
  298. *
  299. * If the reference count reaches zero and the exchange is complete,
  300. * it is freed.
  301. */
  302. static void fc_exch_release(struct fc_exch *ep)
  303. {
  304. struct fc_exch_mgr *mp;
  305. if (atomic_dec_and_test(&ep->ex_refcnt)) {
  306. mp = ep->em;
  307. if (ep->destructor)
  308. ep->destructor(&ep->seq, ep->arg);
  309. WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
  310. mempool_free(ep, mp->ep_pool);
  311. }
  312. }
  313. /**
  314. * fc_exch_done_locked() - Complete an exchange with the exchange lock held
  315. * @ep: The exchange that is complete
  316. */
  317. static int fc_exch_done_locked(struct fc_exch *ep)
  318. {
  319. int rc = 1;
  320. /*
  321. * We must check for completion in case there are two threads
  322. * tyring to complete this. But the rrq code will reuse the
  323. * ep, and in that case we only clear the resp and set it as
  324. * complete, so it can be reused by the timer to send the rrq.
  325. */
  326. ep->resp = NULL;
  327. if (ep->state & FC_EX_DONE)
  328. return rc;
  329. ep->esb_stat |= ESB_ST_COMPLETE;
  330. if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
  331. ep->state |= FC_EX_DONE;
  332. if (cancel_delayed_work(&ep->timeout_work))
  333. atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
  334. rc = 0;
  335. }
  336. return rc;
  337. }
  338. /**
  339. * fc_exch_ptr_get() - Return an exchange from an exchange pool
  340. * @pool: Exchange Pool to get an exchange from
  341. * @index: Index of the exchange within the pool
  342. *
  343. * Use the index to get an exchange from within an exchange pool. exches
  344. * will point to an array of exchange pointers. The index will select
  345. * the exchange within the array.
  346. */
  347. static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
  348. u16 index)
  349. {
  350. struct fc_exch **exches = (struct fc_exch **)(pool + 1);
  351. return exches[index];
  352. }
  353. /**
  354. * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
  355. * @pool: The pool to assign the exchange to
  356. * @index: The index in the pool where the exchange will be assigned
  357. * @ep: The exchange to assign to the pool
  358. */
  359. static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
  360. struct fc_exch *ep)
  361. {
  362. ((struct fc_exch **)(pool + 1))[index] = ep;
  363. }
  364. /**
  365. * fc_exch_delete() - Delete an exchange
  366. * @ep: The exchange to be deleted
  367. */
  368. static void fc_exch_delete(struct fc_exch *ep)
  369. {
  370. struct fc_exch_pool *pool;
  371. u16 index;
  372. pool = ep->pool;
  373. spin_lock_bh(&pool->lock);
  374. WARN_ON(pool->total_exches <= 0);
  375. pool->total_exches--;
  376. /* update cache of free slot */
  377. index = (ep->xid - ep->em->min_xid) >> fc_cpu_order;
  378. if (pool->left == FC_XID_UNKNOWN)
  379. pool->left = index;
  380. else if (pool->right == FC_XID_UNKNOWN)
  381. pool->right = index;
  382. else
  383. pool->next_index = index;
  384. fc_exch_ptr_set(pool, index, NULL);
  385. list_del(&ep->ex_list);
  386. spin_unlock_bh(&pool->lock);
  387. fc_exch_release(ep); /* drop hold for exch in mp */
  388. }
  389. /**
  390. * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
  391. * the exchange lock held
  392. * @ep: The exchange whose timer will start
  393. * @timer_msec: The timeout period
  394. *
  395. * Used for upper level protocols to time out the exchange.
  396. * The timer is cancelled when it fires or when the exchange completes.
  397. */
  398. static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
  399. unsigned int timer_msec)
  400. {
  401. if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
  402. return;
  403. FC_EXCH_DBG(ep, "Exchange timer armed\n");
  404. if (queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
  405. msecs_to_jiffies(timer_msec)))
  406. fc_exch_hold(ep); /* hold for timer */
  407. }
  408. /**
  409. * fc_exch_timer_set() - Lock the exchange and set the timer
  410. * @ep: The exchange whose timer will start
  411. * @timer_msec: The timeout period
  412. */
  413. static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
  414. {
  415. spin_lock_bh(&ep->ex_lock);
  416. fc_exch_timer_set_locked(ep, timer_msec);
  417. spin_unlock_bh(&ep->ex_lock);
  418. }
  419. /**
  420. * fc_seq_send() - Send a frame using existing sequence/exchange pair
  421. * @lport: The local port that the exchange will be sent on
  422. * @sp: The sequence to be sent
  423. * @fp: The frame to be sent on the exchange
  424. */
  425. static int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp,
  426. struct fc_frame *fp)
  427. {
  428. struct fc_exch *ep;
  429. struct fc_frame_header *fh = fc_frame_header_get(fp);
  430. int error;
  431. u32 f_ctl;
  432. ep = fc_seq_exch(sp);
  433. WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
  434. f_ctl = ntoh24(fh->fh_f_ctl);
  435. fc_exch_setup_hdr(ep, fp, f_ctl);
  436. fr_encaps(fp) = ep->encaps;
  437. /*
  438. * update sequence count if this frame is carrying
  439. * multiple FC frames when sequence offload is enabled
  440. * by LLD.
  441. */
  442. if (fr_max_payload(fp))
  443. sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
  444. fr_max_payload(fp));
  445. else
  446. sp->cnt++;
  447. /*
  448. * Send the frame.
  449. */
  450. error = lport->tt.frame_send(lport, fp);
  451. /*
  452. * Update the exchange and sequence flags,
  453. * assuming all frames for the sequence have been sent.
  454. * We can only be called to send once for each sequence.
  455. */
  456. spin_lock_bh(&ep->ex_lock);
  457. ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
  458. if (f_ctl & FC_FC_SEQ_INIT)
  459. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  460. spin_unlock_bh(&ep->ex_lock);
  461. return error;
  462. }
  463. /**
  464. * fc_seq_alloc() - Allocate a sequence for a given exchange
  465. * @ep: The exchange to allocate a new sequence for
  466. * @seq_id: The sequence ID to be used
  467. *
  468. * We don't support multiple originated sequences on the same exchange.
  469. * By implication, any previously originated sequence on this exchange
  470. * is complete, and we reallocate the same sequence.
  471. */
  472. static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
  473. {
  474. struct fc_seq *sp;
  475. sp = &ep->seq;
  476. sp->ssb_stat = 0;
  477. sp->cnt = 0;
  478. sp->id = seq_id;
  479. return sp;
  480. }
  481. /**
  482. * fc_seq_start_next_locked() - Allocate a new sequence on the same
  483. * exchange as the supplied sequence
  484. * @sp: The sequence/exchange to get a new sequence for
  485. */
  486. static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
  487. {
  488. struct fc_exch *ep = fc_seq_exch(sp);
  489. sp = fc_seq_alloc(ep, ep->seq_id++);
  490. FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
  491. ep->f_ctl, sp->id);
  492. return sp;
  493. }
  494. /**
  495. * fc_seq_start_next() - Lock the exchange and get a new sequence
  496. * for a given sequence/exchange pair
  497. * @sp: The sequence/exchange to get a new exchange for
  498. */
  499. static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
  500. {
  501. struct fc_exch *ep = fc_seq_exch(sp);
  502. spin_lock_bh(&ep->ex_lock);
  503. sp = fc_seq_start_next_locked(sp);
  504. spin_unlock_bh(&ep->ex_lock);
  505. return sp;
  506. }
  507. /*
  508. * Set the response handler for the exchange associated with a sequence.
  509. */
  510. static void fc_seq_set_resp(struct fc_seq *sp,
  511. void (*resp)(struct fc_seq *, struct fc_frame *,
  512. void *),
  513. void *arg)
  514. {
  515. struct fc_exch *ep = fc_seq_exch(sp);
  516. spin_lock_bh(&ep->ex_lock);
  517. ep->resp = resp;
  518. ep->arg = arg;
  519. spin_unlock_bh(&ep->ex_lock);
  520. }
  521. /**
  522. * fc_seq_exch_abort() - Abort an exchange and sequence
  523. * @req_sp: The sequence to be aborted
  524. * @timer_msec: The period of time to wait before aborting
  525. *
  526. * Generally called because of a timeout or an abort from the upper layer.
  527. */
  528. static int fc_seq_exch_abort(const struct fc_seq *req_sp,
  529. unsigned int timer_msec)
  530. {
  531. struct fc_seq *sp;
  532. struct fc_exch *ep;
  533. struct fc_frame *fp;
  534. int error;
  535. ep = fc_seq_exch(req_sp);
  536. spin_lock_bh(&ep->ex_lock);
  537. if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
  538. ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
  539. spin_unlock_bh(&ep->ex_lock);
  540. return -ENXIO;
  541. }
  542. /*
  543. * Send the abort on a new sequence if possible.
  544. */
  545. sp = fc_seq_start_next_locked(&ep->seq);
  546. if (!sp) {
  547. spin_unlock_bh(&ep->ex_lock);
  548. return -ENOMEM;
  549. }
  550. ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
  551. if (timer_msec)
  552. fc_exch_timer_set_locked(ep, timer_msec);
  553. spin_unlock_bh(&ep->ex_lock);
  554. /*
  555. * If not logged into the fabric, don't send ABTS but leave
  556. * sequence active until next timeout.
  557. */
  558. if (!ep->sid)
  559. return 0;
  560. /*
  561. * Send an abort for the sequence that timed out.
  562. */
  563. fp = fc_frame_alloc(ep->lp, 0);
  564. if (fp) {
  565. fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
  566. FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  567. error = fc_seq_send(ep->lp, sp, fp);
  568. } else
  569. error = -ENOBUFS;
  570. return error;
  571. }
  572. /**
  573. * fc_exch_timeout() - Handle exchange timer expiration
  574. * @work: The work_struct identifying the exchange that timed out
  575. */
  576. static void fc_exch_timeout(struct work_struct *work)
  577. {
  578. struct fc_exch *ep = container_of(work, struct fc_exch,
  579. timeout_work.work);
  580. struct fc_seq *sp = &ep->seq;
  581. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  582. void *arg;
  583. u32 e_stat;
  584. int rc = 1;
  585. FC_EXCH_DBG(ep, "Exchange timed out\n");
  586. spin_lock_bh(&ep->ex_lock);
  587. if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
  588. goto unlock;
  589. e_stat = ep->esb_stat;
  590. if (e_stat & ESB_ST_COMPLETE) {
  591. ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
  592. spin_unlock_bh(&ep->ex_lock);
  593. if (e_stat & ESB_ST_REC_QUAL)
  594. fc_exch_rrq(ep);
  595. goto done;
  596. } else {
  597. resp = ep->resp;
  598. arg = ep->arg;
  599. ep->resp = NULL;
  600. if (e_stat & ESB_ST_ABNORMAL)
  601. rc = fc_exch_done_locked(ep);
  602. spin_unlock_bh(&ep->ex_lock);
  603. if (resp)
  604. resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
  605. if (!rc) {
  606. /* delete the exchange if it's already being aborted */
  607. fc_exch_delete(ep);
  608. return;
  609. }
  610. fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
  611. goto done;
  612. }
  613. unlock:
  614. spin_unlock_bh(&ep->ex_lock);
  615. done:
  616. /*
  617. * This release matches the hold taken when the timer was set.
  618. */
  619. fc_exch_release(ep);
  620. }
  621. /**
  622. * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
  623. * @lport: The local port that the exchange is for
  624. * @mp: The exchange manager that will allocate the exchange
  625. *
  626. * Returns pointer to allocated fc_exch with exch lock held.
  627. */
  628. static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
  629. struct fc_exch_mgr *mp)
  630. {
  631. struct fc_exch *ep;
  632. unsigned int cpu;
  633. u16 index;
  634. struct fc_exch_pool *pool;
  635. /* allocate memory for exchange */
  636. ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
  637. if (!ep) {
  638. atomic_inc(&mp->stats.no_free_exch);
  639. goto out;
  640. }
  641. memset(ep, 0, sizeof(*ep));
  642. cpu = get_cpu();
  643. pool = per_cpu_ptr(mp->pool, cpu);
  644. spin_lock_bh(&pool->lock);
  645. put_cpu();
  646. /* peek cache of free slot */
  647. if (pool->left != FC_XID_UNKNOWN) {
  648. index = pool->left;
  649. pool->left = FC_XID_UNKNOWN;
  650. goto hit;
  651. }
  652. if (pool->right != FC_XID_UNKNOWN) {
  653. index = pool->right;
  654. pool->right = FC_XID_UNKNOWN;
  655. goto hit;
  656. }
  657. index = pool->next_index;
  658. /* allocate new exch from pool */
  659. while (fc_exch_ptr_get(pool, index)) {
  660. index = index == mp->pool_max_index ? 0 : index + 1;
  661. if (index == pool->next_index)
  662. goto err;
  663. }
  664. pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
  665. hit:
  666. fc_exch_hold(ep); /* hold for exch in mp */
  667. spin_lock_init(&ep->ex_lock);
  668. /*
  669. * Hold exch lock for caller to prevent fc_exch_reset()
  670. * from releasing exch while fc_exch_alloc() caller is
  671. * still working on exch.
  672. */
  673. spin_lock_bh(&ep->ex_lock);
  674. fc_exch_ptr_set(pool, index, ep);
  675. list_add_tail(&ep->ex_list, &pool->ex_list);
  676. fc_seq_alloc(ep, ep->seq_id++);
  677. pool->total_exches++;
  678. spin_unlock_bh(&pool->lock);
  679. /*
  680. * update exchange
  681. */
  682. ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
  683. ep->em = mp;
  684. ep->pool = pool;
  685. ep->lp = lport;
  686. ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
  687. ep->rxid = FC_XID_UNKNOWN;
  688. ep->class = mp->class;
  689. INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
  690. out:
  691. return ep;
  692. err:
  693. spin_unlock_bh(&pool->lock);
  694. atomic_inc(&mp->stats.no_free_exch_xid);
  695. mempool_free(ep, mp->ep_pool);
  696. return NULL;
  697. }
  698. /**
  699. * fc_exch_alloc() - Allocate an exchange from an EM on a
  700. * local port's list of EMs.
  701. * @lport: The local port that will own the exchange
  702. * @fp: The FC frame that the exchange will be for
  703. *
  704. * This function walks the list of exchange manager(EM)
  705. * anchors to select an EM for a new exchange allocation. The
  706. * EM is selected when a NULL match function pointer is encountered
  707. * or when a call to a match function returns true.
  708. */
  709. static inline struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
  710. struct fc_frame *fp)
  711. {
  712. struct fc_exch_mgr_anchor *ema;
  713. list_for_each_entry(ema, &lport->ema_list, ema_list)
  714. if (!ema->match || ema->match(fp))
  715. return fc_exch_em_alloc(lport, ema->mp);
  716. return NULL;
  717. }
  718. /**
  719. * fc_exch_find() - Lookup and hold an exchange
  720. * @mp: The exchange manager to lookup the exchange from
  721. * @xid: The XID of the exchange to look up
  722. */
  723. static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
  724. {
  725. struct fc_exch_pool *pool;
  726. struct fc_exch *ep = NULL;
  727. if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
  728. pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
  729. spin_lock_bh(&pool->lock);
  730. ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
  731. if (ep) {
  732. fc_exch_hold(ep);
  733. WARN_ON(ep->xid != xid);
  734. }
  735. spin_unlock_bh(&pool->lock);
  736. }
  737. return ep;
  738. }
  739. /**
  740. * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
  741. * the memory allocated for the related objects may be freed.
  742. * @sp: The sequence that has completed
  743. */
  744. static void fc_exch_done(struct fc_seq *sp)
  745. {
  746. struct fc_exch *ep = fc_seq_exch(sp);
  747. int rc;
  748. spin_lock_bh(&ep->ex_lock);
  749. rc = fc_exch_done_locked(ep);
  750. spin_unlock_bh(&ep->ex_lock);
  751. if (!rc)
  752. fc_exch_delete(ep);
  753. }
  754. /**
  755. * fc_exch_resp() - Allocate a new exchange for a response frame
  756. * @lport: The local port that the exchange was for
  757. * @mp: The exchange manager to allocate the exchange from
  758. * @fp: The response frame
  759. *
  760. * Sets the responder ID in the frame header.
  761. */
  762. static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
  763. struct fc_exch_mgr *mp,
  764. struct fc_frame *fp)
  765. {
  766. struct fc_exch *ep;
  767. struct fc_frame_header *fh;
  768. ep = fc_exch_alloc(lport, fp);
  769. if (ep) {
  770. ep->class = fc_frame_class(fp);
  771. /*
  772. * Set EX_CTX indicating we're responding on this exchange.
  773. */
  774. ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
  775. ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
  776. fh = fc_frame_header_get(fp);
  777. ep->sid = ntoh24(fh->fh_d_id);
  778. ep->did = ntoh24(fh->fh_s_id);
  779. ep->oid = ep->did;
  780. /*
  781. * Allocated exchange has placed the XID in the
  782. * originator field. Move it to the responder field,
  783. * and set the originator XID from the frame.
  784. */
  785. ep->rxid = ep->xid;
  786. ep->oxid = ntohs(fh->fh_ox_id);
  787. ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
  788. if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
  789. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  790. fc_exch_hold(ep); /* hold for caller */
  791. spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */
  792. }
  793. return ep;
  794. }
  795. /**
  796. * fc_seq_lookup_recip() - Find a sequence where the other end
  797. * originated the sequence
  798. * @lport: The local port that the frame was sent to
  799. * @mp: The Exchange Manager to lookup the exchange from
  800. * @fp: The frame associated with the sequence we're looking for
  801. *
  802. * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
  803. * on the ep that should be released by the caller.
  804. */
  805. static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
  806. struct fc_exch_mgr *mp,
  807. struct fc_frame *fp)
  808. {
  809. struct fc_frame_header *fh = fc_frame_header_get(fp);
  810. struct fc_exch *ep = NULL;
  811. struct fc_seq *sp = NULL;
  812. enum fc_pf_rjt_reason reject = FC_RJT_NONE;
  813. u32 f_ctl;
  814. u16 xid;
  815. f_ctl = ntoh24(fh->fh_f_ctl);
  816. WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
  817. /*
  818. * Lookup or create the exchange if we will be creating the sequence.
  819. */
  820. if (f_ctl & FC_FC_EX_CTX) {
  821. xid = ntohs(fh->fh_ox_id); /* we originated exch */
  822. ep = fc_exch_find(mp, xid);
  823. if (!ep) {
  824. atomic_inc(&mp->stats.xid_not_found);
  825. reject = FC_RJT_OX_ID;
  826. goto out;
  827. }
  828. if (ep->rxid == FC_XID_UNKNOWN)
  829. ep->rxid = ntohs(fh->fh_rx_id);
  830. else if (ep->rxid != ntohs(fh->fh_rx_id)) {
  831. reject = FC_RJT_OX_ID;
  832. goto rel;
  833. }
  834. } else {
  835. xid = ntohs(fh->fh_rx_id); /* we are the responder */
  836. /*
  837. * Special case for MDS issuing an ELS TEST with a
  838. * bad rxid of 0.
  839. * XXX take this out once we do the proper reject.
  840. */
  841. if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
  842. fc_frame_payload_op(fp) == ELS_TEST) {
  843. fh->fh_rx_id = htons(FC_XID_UNKNOWN);
  844. xid = FC_XID_UNKNOWN;
  845. }
  846. /*
  847. * new sequence - find the exchange
  848. */
  849. ep = fc_exch_find(mp, xid);
  850. if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
  851. if (ep) {
  852. atomic_inc(&mp->stats.xid_busy);
  853. reject = FC_RJT_RX_ID;
  854. goto rel;
  855. }
  856. ep = fc_exch_resp(lport, mp, fp);
  857. if (!ep) {
  858. reject = FC_RJT_EXCH_EST; /* XXX */
  859. goto out;
  860. }
  861. xid = ep->xid; /* get our XID */
  862. } else if (!ep) {
  863. atomic_inc(&mp->stats.xid_not_found);
  864. reject = FC_RJT_RX_ID; /* XID not found */
  865. goto out;
  866. }
  867. }
  868. /*
  869. * At this point, we have the exchange held.
  870. * Find or create the sequence.
  871. */
  872. if (fc_sof_is_init(fr_sof(fp))) {
  873. sp = &ep->seq;
  874. sp->ssb_stat |= SSB_ST_RESP;
  875. sp->id = fh->fh_seq_id;
  876. } else {
  877. sp = &ep->seq;
  878. if (sp->id != fh->fh_seq_id) {
  879. atomic_inc(&mp->stats.seq_not_found);
  880. reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
  881. goto rel;
  882. }
  883. }
  884. WARN_ON(ep != fc_seq_exch(sp));
  885. if (f_ctl & FC_FC_SEQ_INIT)
  886. ep->esb_stat |= ESB_ST_SEQ_INIT;
  887. fr_seq(fp) = sp;
  888. out:
  889. return reject;
  890. rel:
  891. fc_exch_done(&ep->seq);
  892. fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
  893. return reject;
  894. }
  895. /**
  896. * fc_seq_lookup_orig() - Find a sequence where this end
  897. * originated the sequence
  898. * @mp: The Exchange Manager to lookup the exchange from
  899. * @fp: The frame associated with the sequence we're looking for
  900. *
  901. * Does not hold the sequence for the caller.
  902. */
  903. static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
  904. struct fc_frame *fp)
  905. {
  906. struct fc_frame_header *fh = fc_frame_header_get(fp);
  907. struct fc_exch *ep;
  908. struct fc_seq *sp = NULL;
  909. u32 f_ctl;
  910. u16 xid;
  911. f_ctl = ntoh24(fh->fh_f_ctl);
  912. WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
  913. xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
  914. ep = fc_exch_find(mp, xid);
  915. if (!ep)
  916. return NULL;
  917. if (ep->seq.id == fh->fh_seq_id) {
  918. /*
  919. * Save the RX_ID if we didn't previously know it.
  920. */
  921. sp = &ep->seq;
  922. if ((f_ctl & FC_FC_EX_CTX) != 0 &&
  923. ep->rxid == FC_XID_UNKNOWN) {
  924. ep->rxid = ntohs(fh->fh_rx_id);
  925. }
  926. }
  927. fc_exch_release(ep);
  928. return sp;
  929. }
  930. /**
  931. * fc_exch_set_addr() - Set the source and destination IDs for an exchange
  932. * @ep: The exchange to set the addresses for
  933. * @orig_id: The originator's ID
  934. * @resp_id: The responder's ID
  935. *
  936. * Note this must be done before the first sequence of the exchange is sent.
  937. */
  938. static void fc_exch_set_addr(struct fc_exch *ep,
  939. u32 orig_id, u32 resp_id)
  940. {
  941. ep->oid = orig_id;
  942. if (ep->esb_stat & ESB_ST_RESP) {
  943. ep->sid = resp_id;
  944. ep->did = orig_id;
  945. } else {
  946. ep->sid = orig_id;
  947. ep->did = resp_id;
  948. }
  949. }
  950. /**
  951. * fc_seq_els_rsp_send() - Send an ELS response using infomation from
  952. * the existing sequence/exchange.
  953. * @fp: The received frame
  954. * @els_cmd: The ELS command to be sent
  955. * @els_data: The ELS data to be sent
  956. *
  957. * The received frame is not freed.
  958. */
  959. static void fc_seq_els_rsp_send(struct fc_frame *fp, enum fc_els_cmd els_cmd,
  960. struct fc_seq_els_data *els_data)
  961. {
  962. switch (els_cmd) {
  963. case ELS_LS_RJT:
  964. fc_seq_ls_rjt(fp, els_data->reason, els_data->explan);
  965. break;
  966. case ELS_LS_ACC:
  967. fc_seq_ls_acc(fp);
  968. break;
  969. case ELS_RRQ:
  970. fc_exch_els_rrq(fp);
  971. break;
  972. case ELS_REC:
  973. fc_exch_els_rec(fp);
  974. break;
  975. default:
  976. FC_LPORT_DBG(fr_dev(fp), "Invalid ELS CMD:%x\n", els_cmd);
  977. }
  978. }
  979. /**
  980. * fc_seq_send_last() - Send a sequence that is the last in the exchange
  981. * @sp: The sequence that is to be sent
  982. * @fp: The frame that will be sent on the sequence
  983. * @rctl: The R_CTL information to be sent
  984. * @fh_type: The frame header type
  985. */
  986. static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
  987. enum fc_rctl rctl, enum fc_fh_type fh_type)
  988. {
  989. u32 f_ctl;
  990. struct fc_exch *ep = fc_seq_exch(sp);
  991. f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  992. f_ctl |= ep->f_ctl;
  993. fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
  994. fc_seq_send(ep->lp, sp, fp);
  995. }
  996. /**
  997. * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
  998. * @sp: The sequence to send the ACK on
  999. * @rx_fp: The received frame that is being acknoledged
  1000. *
  1001. * Send ACK_1 (or equiv.) indicating we received something.
  1002. */
  1003. static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
  1004. {
  1005. struct fc_frame *fp;
  1006. struct fc_frame_header *rx_fh;
  1007. struct fc_frame_header *fh;
  1008. struct fc_exch *ep = fc_seq_exch(sp);
  1009. struct fc_lport *lport = ep->lp;
  1010. unsigned int f_ctl;
  1011. /*
  1012. * Don't send ACKs for class 3.
  1013. */
  1014. if (fc_sof_needs_ack(fr_sof(rx_fp))) {
  1015. fp = fc_frame_alloc(lport, 0);
  1016. if (!fp)
  1017. return;
  1018. fh = fc_frame_header_get(fp);
  1019. fh->fh_r_ctl = FC_RCTL_ACK_1;
  1020. fh->fh_type = FC_TYPE_BLS;
  1021. /*
  1022. * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
  1023. * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
  1024. * Bits 9-8 are meaningful (retransmitted or unidirectional).
  1025. * Last ACK uses bits 7-6 (continue sequence),
  1026. * bits 5-4 are meaningful (what kind of ACK to use).
  1027. */
  1028. rx_fh = fc_frame_header_get(rx_fp);
  1029. f_ctl = ntoh24(rx_fh->fh_f_ctl);
  1030. f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
  1031. FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
  1032. FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
  1033. FC_FC_RETX_SEQ | FC_FC_UNI_TX;
  1034. f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
  1035. hton24(fh->fh_f_ctl, f_ctl);
  1036. fc_exch_setup_hdr(ep, fp, f_ctl);
  1037. fh->fh_seq_id = rx_fh->fh_seq_id;
  1038. fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
  1039. fh->fh_parm_offset = htonl(1); /* ack single frame */
  1040. fr_sof(fp) = fr_sof(rx_fp);
  1041. if (f_ctl & FC_FC_END_SEQ)
  1042. fr_eof(fp) = FC_EOF_T;
  1043. else
  1044. fr_eof(fp) = FC_EOF_N;
  1045. lport->tt.frame_send(lport, fp);
  1046. }
  1047. }
  1048. /**
  1049. * fc_exch_send_ba_rjt() - Send BLS Reject
  1050. * @rx_fp: The frame being rejected
  1051. * @reason: The reason the frame is being rejected
  1052. * @explan: The explaination for the rejection
  1053. *
  1054. * This is for rejecting BA_ABTS only.
  1055. */
  1056. static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
  1057. enum fc_ba_rjt_reason reason,
  1058. enum fc_ba_rjt_explan explan)
  1059. {
  1060. struct fc_frame *fp;
  1061. struct fc_frame_header *rx_fh;
  1062. struct fc_frame_header *fh;
  1063. struct fc_ba_rjt *rp;
  1064. struct fc_lport *lport;
  1065. unsigned int f_ctl;
  1066. lport = fr_dev(rx_fp);
  1067. fp = fc_frame_alloc(lport, sizeof(*rp));
  1068. if (!fp)
  1069. return;
  1070. fh = fc_frame_header_get(fp);
  1071. rx_fh = fc_frame_header_get(rx_fp);
  1072. memset(fh, 0, sizeof(*fh) + sizeof(*rp));
  1073. rp = fc_frame_payload_get(fp, sizeof(*rp));
  1074. rp->br_reason = reason;
  1075. rp->br_explan = explan;
  1076. /*
  1077. * seq_id, cs_ctl, df_ctl and param/offset are zero.
  1078. */
  1079. memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
  1080. memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
  1081. fh->fh_ox_id = rx_fh->fh_ox_id;
  1082. fh->fh_rx_id = rx_fh->fh_rx_id;
  1083. fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
  1084. fh->fh_r_ctl = FC_RCTL_BA_RJT;
  1085. fh->fh_type = FC_TYPE_BLS;
  1086. /*
  1087. * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
  1088. * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
  1089. * Bits 9-8 are meaningful (retransmitted or unidirectional).
  1090. * Last ACK uses bits 7-6 (continue sequence),
  1091. * bits 5-4 are meaningful (what kind of ACK to use).
  1092. * Always set LAST_SEQ, END_SEQ.
  1093. */
  1094. f_ctl = ntoh24(rx_fh->fh_f_ctl);
  1095. f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
  1096. FC_FC_END_CONN | FC_FC_SEQ_INIT |
  1097. FC_FC_RETX_SEQ | FC_FC_UNI_TX;
  1098. f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
  1099. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  1100. f_ctl &= ~FC_FC_FIRST_SEQ;
  1101. hton24(fh->fh_f_ctl, f_ctl);
  1102. fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
  1103. fr_eof(fp) = FC_EOF_T;
  1104. if (fc_sof_needs_ack(fr_sof(fp)))
  1105. fr_eof(fp) = FC_EOF_N;
  1106. lport->tt.frame_send(lport, fp);
  1107. }
  1108. /**
  1109. * fc_exch_recv_abts() - Handle an incoming ABTS
  1110. * @ep: The exchange the abort was on
  1111. * @rx_fp: The ABTS frame
  1112. *
  1113. * This would be for target mode usually, but could be due to lost
  1114. * FCP transfer ready, confirm or RRQ. We always handle this as an
  1115. * exchange abort, ignoring the parameter.
  1116. */
  1117. static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
  1118. {
  1119. struct fc_frame *fp;
  1120. struct fc_ba_acc *ap;
  1121. struct fc_frame_header *fh;
  1122. struct fc_seq *sp;
  1123. if (!ep)
  1124. goto reject;
  1125. spin_lock_bh(&ep->ex_lock);
  1126. if (ep->esb_stat & ESB_ST_COMPLETE) {
  1127. spin_unlock_bh(&ep->ex_lock);
  1128. goto reject;
  1129. }
  1130. if (!(ep->esb_stat & ESB_ST_REC_QUAL))
  1131. fc_exch_hold(ep); /* hold for REC_QUAL */
  1132. ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
  1133. fc_exch_timer_set_locked(ep, ep->r_a_tov);
  1134. fp = fc_frame_alloc(ep->lp, sizeof(*ap));
  1135. if (!fp) {
  1136. spin_unlock_bh(&ep->ex_lock);
  1137. goto free;
  1138. }
  1139. fh = fc_frame_header_get(fp);
  1140. ap = fc_frame_payload_get(fp, sizeof(*ap));
  1141. memset(ap, 0, sizeof(*ap));
  1142. sp = &ep->seq;
  1143. ap->ba_high_seq_cnt = htons(0xffff);
  1144. if (sp->ssb_stat & SSB_ST_RESP) {
  1145. ap->ba_seq_id = sp->id;
  1146. ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
  1147. ap->ba_high_seq_cnt = fh->fh_seq_cnt;
  1148. ap->ba_low_seq_cnt = htons(sp->cnt);
  1149. }
  1150. sp = fc_seq_start_next_locked(sp);
  1151. spin_unlock_bh(&ep->ex_lock);
  1152. fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
  1153. fc_frame_free(rx_fp);
  1154. return;
  1155. reject:
  1156. fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
  1157. free:
  1158. fc_frame_free(rx_fp);
  1159. }
  1160. /**
  1161. * fc_seq_assign() - Assign exchange and sequence for incoming request
  1162. * @lport: The local port that received the request
  1163. * @fp: The request frame
  1164. *
  1165. * On success, the sequence pointer will be returned and also in fr_seq(@fp).
  1166. */
  1167. static struct fc_seq *fc_seq_assign(struct fc_lport *lport, struct fc_frame *fp)
  1168. {
  1169. struct fc_exch_mgr_anchor *ema;
  1170. WARN_ON(lport != fr_dev(fp));
  1171. WARN_ON(fr_seq(fp));
  1172. fr_seq(fp) = NULL;
  1173. list_for_each_entry(ema, &lport->ema_list, ema_list)
  1174. if ((!ema->match || ema->match(fp)) &&
  1175. fc_seq_lookup_recip(lport, ema->mp, fp) == FC_RJT_NONE)
  1176. break;
  1177. return fr_seq(fp);
  1178. }
  1179. /**
  1180. * fc_exch_recv_req() - Handler for an incoming request
  1181. * @lport: The local port that received the request
  1182. * @mp: The EM that the exchange is on
  1183. * @fp: The request frame
  1184. *
  1185. * This is used when the other end is originating the exchange
  1186. * and the sequence.
  1187. */
  1188. static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
  1189. struct fc_frame *fp)
  1190. {
  1191. struct fc_frame_header *fh = fc_frame_header_get(fp);
  1192. struct fc_seq *sp = NULL;
  1193. struct fc_exch *ep = NULL;
  1194. enum fc_pf_rjt_reason reject;
  1195. /* We can have the wrong fc_lport at this point with NPIV, which is a
  1196. * problem now that we know a new exchange needs to be allocated
  1197. */
  1198. lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
  1199. if (!lport) {
  1200. fc_frame_free(fp);
  1201. return;
  1202. }
  1203. fr_dev(fp) = lport;
  1204. BUG_ON(fr_seq(fp)); /* XXX remove later */
  1205. /*
  1206. * If the RX_ID is 0xffff, don't allocate an exchange.
  1207. * The upper-level protocol may request one later, if needed.
  1208. */
  1209. if (fh->fh_rx_id == htons(FC_XID_UNKNOWN))
  1210. return lport->tt.lport_recv(lport, fp);
  1211. reject = fc_seq_lookup_recip(lport, mp, fp);
  1212. if (reject == FC_RJT_NONE) {
  1213. sp = fr_seq(fp); /* sequence will be held */
  1214. ep = fc_seq_exch(sp);
  1215. fc_seq_send_ack(sp, fp);
  1216. ep->encaps = fr_encaps(fp);
  1217. /*
  1218. * Call the receive function.
  1219. *
  1220. * The receive function may allocate a new sequence
  1221. * over the old one, so we shouldn't change the
  1222. * sequence after this.
  1223. *
  1224. * The frame will be freed by the receive function.
  1225. * If new exch resp handler is valid then call that
  1226. * first.
  1227. */
  1228. if (ep->resp)
  1229. ep->resp(sp, fp, ep->arg);
  1230. else
  1231. lport->tt.lport_recv(lport, fp);
  1232. fc_exch_release(ep); /* release from lookup */
  1233. } else {
  1234. FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
  1235. reject);
  1236. fc_frame_free(fp);
  1237. }
  1238. }
  1239. /**
  1240. * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
  1241. * end is the originator of the sequence that is a
  1242. * response to our initial exchange
  1243. * @mp: The EM that the exchange is on
  1244. * @fp: The response frame
  1245. */
  1246. static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1247. {
  1248. struct fc_frame_header *fh = fc_frame_header_get(fp);
  1249. struct fc_seq *sp;
  1250. struct fc_exch *ep;
  1251. enum fc_sof sof;
  1252. u32 f_ctl;
  1253. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  1254. void *ex_resp_arg;
  1255. int rc;
  1256. ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
  1257. if (!ep) {
  1258. atomic_inc(&mp->stats.xid_not_found);
  1259. goto out;
  1260. }
  1261. if (ep->esb_stat & ESB_ST_COMPLETE) {
  1262. atomic_inc(&mp->stats.xid_not_found);
  1263. goto rel;
  1264. }
  1265. if (ep->rxid == FC_XID_UNKNOWN)
  1266. ep->rxid = ntohs(fh->fh_rx_id);
  1267. if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
  1268. atomic_inc(&mp->stats.xid_not_found);
  1269. goto rel;
  1270. }
  1271. if (ep->did != ntoh24(fh->fh_s_id) &&
  1272. ep->did != FC_FID_FLOGI) {
  1273. atomic_inc(&mp->stats.xid_not_found);
  1274. goto rel;
  1275. }
  1276. sof = fr_sof(fp);
  1277. sp = &ep->seq;
  1278. if (fc_sof_is_init(sof)) {
  1279. sp->ssb_stat |= SSB_ST_RESP;
  1280. sp->id = fh->fh_seq_id;
  1281. } else if (sp->id != fh->fh_seq_id) {
  1282. atomic_inc(&mp->stats.seq_not_found);
  1283. goto rel;
  1284. }
  1285. f_ctl = ntoh24(fh->fh_f_ctl);
  1286. fr_seq(fp) = sp;
  1287. if (f_ctl & FC_FC_SEQ_INIT)
  1288. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1289. if (fc_sof_needs_ack(sof))
  1290. fc_seq_send_ack(sp, fp);
  1291. resp = ep->resp;
  1292. ex_resp_arg = ep->arg;
  1293. if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
  1294. (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
  1295. (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
  1296. spin_lock_bh(&ep->ex_lock);
  1297. rc = fc_exch_done_locked(ep);
  1298. WARN_ON(fc_seq_exch(sp) != ep);
  1299. spin_unlock_bh(&ep->ex_lock);
  1300. if (!rc)
  1301. fc_exch_delete(ep);
  1302. }
  1303. /*
  1304. * Call the receive function.
  1305. * The sequence is held (has a refcnt) for us,
  1306. * but not for the receive function.
  1307. *
  1308. * The receive function may allocate a new sequence
  1309. * over the old one, so we shouldn't change the
  1310. * sequence after this.
  1311. *
  1312. * The frame will be freed by the receive function.
  1313. * If new exch resp handler is valid then call that
  1314. * first.
  1315. */
  1316. if (resp)
  1317. resp(sp, fp, ex_resp_arg);
  1318. else
  1319. fc_frame_free(fp);
  1320. fc_exch_release(ep);
  1321. return;
  1322. rel:
  1323. fc_exch_release(ep);
  1324. out:
  1325. fc_frame_free(fp);
  1326. }
  1327. /**
  1328. * fc_exch_recv_resp() - Handler for a sequence where other end is
  1329. * responding to our sequence
  1330. * @mp: The EM that the exchange is on
  1331. * @fp: The response frame
  1332. */
  1333. static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1334. {
  1335. struct fc_seq *sp;
  1336. sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
  1337. if (!sp)
  1338. atomic_inc(&mp->stats.xid_not_found);
  1339. else
  1340. atomic_inc(&mp->stats.non_bls_resp);
  1341. fc_frame_free(fp);
  1342. }
  1343. /**
  1344. * fc_exch_abts_resp() - Handler for a response to an ABT
  1345. * @ep: The exchange that the frame is on
  1346. * @fp: The response frame
  1347. *
  1348. * This response would be to an ABTS cancelling an exchange or sequence.
  1349. * The response can be either BA_ACC or BA_RJT
  1350. */
  1351. static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
  1352. {
  1353. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  1354. void *ex_resp_arg;
  1355. struct fc_frame_header *fh;
  1356. struct fc_ba_acc *ap;
  1357. struct fc_seq *sp;
  1358. u16 low;
  1359. u16 high;
  1360. int rc = 1, has_rec = 0;
  1361. fh = fc_frame_header_get(fp);
  1362. FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
  1363. fc_exch_rctl_name(fh->fh_r_ctl));
  1364. if (cancel_delayed_work_sync(&ep->timeout_work))
  1365. fc_exch_release(ep); /* release from pending timer hold */
  1366. spin_lock_bh(&ep->ex_lock);
  1367. switch (fh->fh_r_ctl) {
  1368. case FC_RCTL_BA_ACC:
  1369. ap = fc_frame_payload_get(fp, sizeof(*ap));
  1370. if (!ap)
  1371. break;
  1372. /*
  1373. * Decide whether to establish a Recovery Qualifier.
  1374. * We do this if there is a non-empty SEQ_CNT range and
  1375. * SEQ_ID is the same as the one we aborted.
  1376. */
  1377. low = ntohs(ap->ba_low_seq_cnt);
  1378. high = ntohs(ap->ba_high_seq_cnt);
  1379. if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
  1380. (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
  1381. ap->ba_seq_id == ep->seq_id) && low != high) {
  1382. ep->esb_stat |= ESB_ST_REC_QUAL;
  1383. fc_exch_hold(ep); /* hold for recovery qualifier */
  1384. has_rec = 1;
  1385. }
  1386. break;
  1387. case FC_RCTL_BA_RJT:
  1388. break;
  1389. default:
  1390. break;
  1391. }
  1392. resp = ep->resp;
  1393. ex_resp_arg = ep->arg;
  1394. /* do we need to do some other checks here. Can we reuse more of
  1395. * fc_exch_recv_seq_resp
  1396. */
  1397. sp = &ep->seq;
  1398. /*
  1399. * do we want to check END_SEQ as well as LAST_SEQ here?
  1400. */
  1401. if (ep->fh_type != FC_TYPE_FCP &&
  1402. ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
  1403. rc = fc_exch_done_locked(ep);
  1404. spin_unlock_bh(&ep->ex_lock);
  1405. if (!rc)
  1406. fc_exch_delete(ep);
  1407. if (resp)
  1408. resp(sp, fp, ex_resp_arg);
  1409. else
  1410. fc_frame_free(fp);
  1411. if (has_rec)
  1412. fc_exch_timer_set(ep, ep->r_a_tov);
  1413. }
  1414. /**
  1415. * fc_exch_recv_bls() - Handler for a BLS sequence
  1416. * @mp: The EM that the exchange is on
  1417. * @fp: The request frame
  1418. *
  1419. * The BLS frame is always a sequence initiated by the remote side.
  1420. * We may be either the originator or recipient of the exchange.
  1421. */
  1422. static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1423. {
  1424. struct fc_frame_header *fh;
  1425. struct fc_exch *ep;
  1426. u32 f_ctl;
  1427. fh = fc_frame_header_get(fp);
  1428. f_ctl = ntoh24(fh->fh_f_ctl);
  1429. fr_seq(fp) = NULL;
  1430. ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
  1431. ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
  1432. if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
  1433. spin_lock_bh(&ep->ex_lock);
  1434. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1435. spin_unlock_bh(&ep->ex_lock);
  1436. }
  1437. if (f_ctl & FC_FC_SEQ_CTX) {
  1438. /*
  1439. * A response to a sequence we initiated.
  1440. * This should only be ACKs for class 2 or F.
  1441. */
  1442. switch (fh->fh_r_ctl) {
  1443. case FC_RCTL_ACK_1:
  1444. case FC_RCTL_ACK_0:
  1445. break;
  1446. default:
  1447. FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
  1448. fh->fh_r_ctl,
  1449. fc_exch_rctl_name(fh->fh_r_ctl));
  1450. break;
  1451. }
  1452. fc_frame_free(fp);
  1453. } else {
  1454. switch (fh->fh_r_ctl) {
  1455. case FC_RCTL_BA_RJT:
  1456. case FC_RCTL_BA_ACC:
  1457. if (ep)
  1458. fc_exch_abts_resp(ep, fp);
  1459. else
  1460. fc_frame_free(fp);
  1461. break;
  1462. case FC_RCTL_BA_ABTS:
  1463. fc_exch_recv_abts(ep, fp);
  1464. break;
  1465. default: /* ignore junk */
  1466. fc_frame_free(fp);
  1467. break;
  1468. }
  1469. }
  1470. if (ep)
  1471. fc_exch_release(ep); /* release hold taken by fc_exch_find */
  1472. }
  1473. /**
  1474. * fc_seq_ls_acc() - Accept sequence with LS_ACC
  1475. * @rx_fp: The received frame, not freed here.
  1476. *
  1477. * If this fails due to allocation or transmit congestion, assume the
  1478. * originator will repeat the sequence.
  1479. */
  1480. static void fc_seq_ls_acc(struct fc_frame *rx_fp)
  1481. {
  1482. struct fc_lport *lport;
  1483. struct fc_els_ls_acc *acc;
  1484. struct fc_frame *fp;
  1485. lport = fr_dev(rx_fp);
  1486. fp = fc_frame_alloc(lport, sizeof(*acc));
  1487. if (!fp)
  1488. return;
  1489. acc = fc_frame_payload_get(fp, sizeof(*acc));
  1490. memset(acc, 0, sizeof(*acc));
  1491. acc->la_cmd = ELS_LS_ACC;
  1492. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1493. lport->tt.frame_send(lport, fp);
  1494. }
  1495. /**
  1496. * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
  1497. * @rx_fp: The received frame, not freed here.
  1498. * @reason: The reason the sequence is being rejected
  1499. * @explan: The explanation for the rejection
  1500. *
  1501. * If this fails due to allocation or transmit congestion, assume the
  1502. * originator will repeat the sequence.
  1503. */
  1504. static void fc_seq_ls_rjt(struct fc_frame *rx_fp, enum fc_els_rjt_reason reason,
  1505. enum fc_els_rjt_explan explan)
  1506. {
  1507. struct fc_lport *lport;
  1508. struct fc_els_ls_rjt *rjt;
  1509. struct fc_frame *fp;
  1510. lport = fr_dev(rx_fp);
  1511. fp = fc_frame_alloc(lport, sizeof(*rjt));
  1512. if (!fp)
  1513. return;
  1514. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1515. memset(rjt, 0, sizeof(*rjt));
  1516. rjt->er_cmd = ELS_LS_RJT;
  1517. rjt->er_reason = reason;
  1518. rjt->er_explan = explan;
  1519. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1520. lport->tt.frame_send(lport, fp);
  1521. }
  1522. /**
  1523. * fc_exch_reset() - Reset an exchange
  1524. * @ep: The exchange to be reset
  1525. */
  1526. static void fc_exch_reset(struct fc_exch *ep)
  1527. {
  1528. struct fc_seq *sp;
  1529. void (*resp)(struct fc_seq *, struct fc_frame *, void *);
  1530. void *arg;
  1531. int rc = 1;
  1532. spin_lock_bh(&ep->ex_lock);
  1533. ep->state |= FC_EX_RST_CLEANUP;
  1534. if (cancel_delayed_work(&ep->timeout_work))
  1535. atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
  1536. resp = ep->resp;
  1537. ep->resp = NULL;
  1538. if (ep->esb_stat & ESB_ST_REC_QUAL)
  1539. atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
  1540. ep->esb_stat &= ~ESB_ST_REC_QUAL;
  1541. arg = ep->arg;
  1542. sp = &ep->seq;
  1543. rc = fc_exch_done_locked(ep);
  1544. spin_unlock_bh(&ep->ex_lock);
  1545. if (!rc)
  1546. fc_exch_delete(ep);
  1547. if (resp)
  1548. resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
  1549. }
  1550. /**
  1551. * fc_exch_pool_reset() - Reset a per cpu exchange pool
  1552. * @lport: The local port that the exchange pool is on
  1553. * @pool: The exchange pool to be reset
  1554. * @sid: The source ID
  1555. * @did: The destination ID
  1556. *
  1557. * Resets a per cpu exches pool, releasing all of its sequences
  1558. * and exchanges. If sid is non-zero then reset only exchanges
  1559. * we sourced from the local port's FID. If did is non-zero then
  1560. * only reset exchanges destined for the local port's FID.
  1561. */
  1562. static void fc_exch_pool_reset(struct fc_lport *lport,
  1563. struct fc_exch_pool *pool,
  1564. u32 sid, u32 did)
  1565. {
  1566. struct fc_exch *ep;
  1567. struct fc_exch *next;
  1568. spin_lock_bh(&pool->lock);
  1569. restart:
  1570. list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
  1571. if ((lport == ep->lp) &&
  1572. (sid == 0 || sid == ep->sid) &&
  1573. (did == 0 || did == ep->did)) {
  1574. fc_exch_hold(ep);
  1575. spin_unlock_bh(&pool->lock);
  1576. fc_exch_reset(ep);
  1577. fc_exch_release(ep);
  1578. spin_lock_bh(&pool->lock);
  1579. /*
  1580. * must restart loop incase while lock
  1581. * was down multiple eps were released.
  1582. */
  1583. goto restart;
  1584. }
  1585. }
  1586. spin_unlock_bh(&pool->lock);
  1587. }
  1588. /**
  1589. * fc_exch_mgr_reset() - Reset all EMs of a local port
  1590. * @lport: The local port whose EMs are to be reset
  1591. * @sid: The source ID
  1592. * @did: The destination ID
  1593. *
  1594. * Reset all EMs associated with a given local port. Release all
  1595. * sequences and exchanges. If sid is non-zero then reset only the
  1596. * exchanges sent from the local port's FID. If did is non-zero then
  1597. * reset only exchanges destined for the local port's FID.
  1598. */
  1599. void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
  1600. {
  1601. struct fc_exch_mgr_anchor *ema;
  1602. unsigned int cpu;
  1603. list_for_each_entry(ema, &lport->ema_list, ema_list) {
  1604. for_each_possible_cpu(cpu)
  1605. fc_exch_pool_reset(lport,
  1606. per_cpu_ptr(ema->mp->pool, cpu),
  1607. sid, did);
  1608. }
  1609. }
  1610. EXPORT_SYMBOL(fc_exch_mgr_reset);
  1611. /**
  1612. * fc_exch_lookup() - find an exchange
  1613. * @lport: The local port
  1614. * @xid: The exchange ID
  1615. *
  1616. * Returns exchange pointer with hold for caller, or NULL if not found.
  1617. */
  1618. static struct fc_exch *fc_exch_lookup(struct fc_lport *lport, u32 xid)
  1619. {
  1620. struct fc_exch_mgr_anchor *ema;
  1621. list_for_each_entry(ema, &lport->ema_list, ema_list)
  1622. if (ema->mp->min_xid <= xid && xid <= ema->mp->max_xid)
  1623. return fc_exch_find(ema->mp, xid);
  1624. return NULL;
  1625. }
  1626. /**
  1627. * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
  1628. * @rfp: The REC frame, not freed here.
  1629. *
  1630. * Note that the requesting port may be different than the S_ID in the request.
  1631. */
  1632. static void fc_exch_els_rec(struct fc_frame *rfp)
  1633. {
  1634. struct fc_lport *lport;
  1635. struct fc_frame *fp;
  1636. struct fc_exch *ep;
  1637. struct fc_els_rec *rp;
  1638. struct fc_els_rec_acc *acc;
  1639. enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
  1640. enum fc_els_rjt_explan explan;
  1641. u32 sid;
  1642. u16 rxid;
  1643. u16 oxid;
  1644. lport = fr_dev(rfp);
  1645. rp = fc_frame_payload_get(rfp, sizeof(*rp));
  1646. explan = ELS_EXPL_INV_LEN;
  1647. if (!rp)
  1648. goto reject;
  1649. sid = ntoh24(rp->rec_s_id);
  1650. rxid = ntohs(rp->rec_rx_id);
  1651. oxid = ntohs(rp->rec_ox_id);
  1652. ep = fc_exch_lookup(lport,
  1653. sid == fc_host_port_id(lport->host) ? oxid : rxid);
  1654. explan = ELS_EXPL_OXID_RXID;
  1655. if (!ep)
  1656. goto reject;
  1657. if (ep->oid != sid || oxid != ep->oxid)
  1658. goto rel;
  1659. if (rxid != FC_XID_UNKNOWN && rxid != ep->rxid)
  1660. goto rel;
  1661. fp = fc_frame_alloc(lport, sizeof(*acc));
  1662. if (!fp)
  1663. goto out;
  1664. acc = fc_frame_payload_get(fp, sizeof(*acc));
  1665. memset(acc, 0, sizeof(*acc));
  1666. acc->reca_cmd = ELS_LS_ACC;
  1667. acc->reca_ox_id = rp->rec_ox_id;
  1668. memcpy(acc->reca_ofid, rp->rec_s_id, 3);
  1669. acc->reca_rx_id = htons(ep->rxid);
  1670. if (ep->sid == ep->oid)
  1671. hton24(acc->reca_rfid, ep->did);
  1672. else
  1673. hton24(acc->reca_rfid, ep->sid);
  1674. acc->reca_fc4value = htonl(ep->seq.rec_data);
  1675. acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
  1676. ESB_ST_SEQ_INIT |
  1677. ESB_ST_COMPLETE));
  1678. fc_fill_reply_hdr(fp, rfp, FC_RCTL_ELS_REP, 0);
  1679. lport->tt.frame_send(lport, fp);
  1680. out:
  1681. fc_exch_release(ep);
  1682. return;
  1683. rel:
  1684. fc_exch_release(ep);
  1685. reject:
  1686. fc_seq_ls_rjt(rfp, reason, explan);
  1687. }
  1688. /**
  1689. * fc_exch_rrq_resp() - Handler for RRQ responses
  1690. * @sp: The sequence that the RRQ is on
  1691. * @fp: The RRQ frame
  1692. * @arg: The exchange that the RRQ is on
  1693. *
  1694. * TODO: fix error handler.
  1695. */
  1696. static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
  1697. {
  1698. struct fc_exch *aborted_ep = arg;
  1699. unsigned int op;
  1700. if (IS_ERR(fp)) {
  1701. int err = PTR_ERR(fp);
  1702. if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
  1703. goto cleanup;
  1704. FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
  1705. "frame error %d\n", err);
  1706. return;
  1707. }
  1708. op = fc_frame_payload_op(fp);
  1709. fc_frame_free(fp);
  1710. switch (op) {
  1711. case ELS_LS_RJT:
  1712. FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
  1713. /* fall through */
  1714. case ELS_LS_ACC:
  1715. goto cleanup;
  1716. default:
  1717. FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
  1718. "for RRQ", op);
  1719. return;
  1720. }
  1721. cleanup:
  1722. fc_exch_done(&aborted_ep->seq);
  1723. /* drop hold for rec qual */
  1724. fc_exch_release(aborted_ep);
  1725. }
  1726. /**
  1727. * fc_exch_seq_send() - Send a frame using a new exchange and sequence
  1728. * @lport: The local port to send the frame on
  1729. * @fp: The frame to be sent
  1730. * @resp: The response handler for this request
  1731. * @destructor: The destructor for the exchange
  1732. * @arg: The argument to be passed to the response handler
  1733. * @timer_msec: The timeout period for the exchange
  1734. *
  1735. * The frame pointer with some of the header's fields must be
  1736. * filled before calling this routine, those fields are:
  1737. *
  1738. * - routing control
  1739. * - FC port did
  1740. * - FC port sid
  1741. * - FC header type
  1742. * - frame control
  1743. * - parameter or relative offset
  1744. */
  1745. static struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
  1746. struct fc_frame *fp,
  1747. void (*resp)(struct fc_seq *,
  1748. struct fc_frame *fp,
  1749. void *arg),
  1750. void (*destructor)(struct fc_seq *,
  1751. void *),
  1752. void *arg, u32 timer_msec)
  1753. {
  1754. struct fc_exch *ep;
  1755. struct fc_seq *sp = NULL;
  1756. struct fc_frame_header *fh;
  1757. int rc = 1;
  1758. ep = fc_exch_alloc(lport, fp);
  1759. if (!ep) {
  1760. fc_frame_free(fp);
  1761. return NULL;
  1762. }
  1763. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1764. fh = fc_frame_header_get(fp);
  1765. fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
  1766. ep->resp = resp;
  1767. ep->destructor = destructor;
  1768. ep->arg = arg;
  1769. ep->r_a_tov = FC_DEF_R_A_TOV;
  1770. ep->lp = lport;
  1771. sp = &ep->seq;
  1772. ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
  1773. ep->f_ctl = ntoh24(fh->fh_f_ctl);
  1774. fc_exch_setup_hdr(ep, fp, ep->f_ctl);
  1775. sp->cnt++;
  1776. if (ep->xid <= lport->lro_xid && fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD)
  1777. fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
  1778. if (unlikely(lport->tt.frame_send(lport, fp)))
  1779. goto err;
  1780. if (timer_msec)
  1781. fc_exch_timer_set_locked(ep, timer_msec);
  1782. ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
  1783. if (ep->f_ctl & FC_FC_SEQ_INIT)
  1784. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  1785. spin_unlock_bh(&ep->ex_lock);
  1786. return sp;
  1787. err:
  1788. rc = fc_exch_done_locked(ep);
  1789. spin_unlock_bh(&ep->ex_lock);
  1790. if (!rc)
  1791. fc_exch_delete(ep);
  1792. return NULL;
  1793. }
  1794. /**
  1795. * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
  1796. * @ep: The exchange to send the RRQ on
  1797. *
  1798. * This tells the remote port to stop blocking the use of
  1799. * the exchange and the seq_cnt range.
  1800. */
  1801. static void fc_exch_rrq(struct fc_exch *ep)
  1802. {
  1803. struct fc_lport *lport;
  1804. struct fc_els_rrq *rrq;
  1805. struct fc_frame *fp;
  1806. u32 did;
  1807. lport = ep->lp;
  1808. fp = fc_frame_alloc(lport, sizeof(*rrq));
  1809. if (!fp)
  1810. goto retry;
  1811. rrq = fc_frame_payload_get(fp, sizeof(*rrq));
  1812. memset(rrq, 0, sizeof(*rrq));
  1813. rrq->rrq_cmd = ELS_RRQ;
  1814. hton24(rrq->rrq_s_id, ep->sid);
  1815. rrq->rrq_ox_id = htons(ep->oxid);
  1816. rrq->rrq_rx_id = htons(ep->rxid);
  1817. did = ep->did;
  1818. if (ep->esb_stat & ESB_ST_RESP)
  1819. did = ep->sid;
  1820. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
  1821. lport->port_id, FC_TYPE_ELS,
  1822. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1823. if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
  1824. lport->e_d_tov))
  1825. return;
  1826. retry:
  1827. spin_lock_bh(&ep->ex_lock);
  1828. if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
  1829. spin_unlock_bh(&ep->ex_lock);
  1830. /* drop hold for rec qual */
  1831. fc_exch_release(ep);
  1832. return;
  1833. }
  1834. ep->esb_stat |= ESB_ST_REC_QUAL;
  1835. fc_exch_timer_set_locked(ep, ep->r_a_tov);
  1836. spin_unlock_bh(&ep->ex_lock);
  1837. }
  1838. /**
  1839. * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
  1840. * @fp: The RRQ frame, not freed here.
  1841. */
  1842. static void fc_exch_els_rrq(struct fc_frame *fp)
  1843. {
  1844. struct fc_lport *lport;
  1845. struct fc_exch *ep = NULL; /* request or subject exchange */
  1846. struct fc_els_rrq *rp;
  1847. u32 sid;
  1848. u16 xid;
  1849. enum fc_els_rjt_explan explan;
  1850. lport = fr_dev(fp);
  1851. rp = fc_frame_payload_get(fp, sizeof(*rp));
  1852. explan = ELS_EXPL_INV_LEN;
  1853. if (!rp)
  1854. goto reject;
  1855. /*
  1856. * lookup subject exchange.
  1857. */
  1858. sid = ntoh24(rp->rrq_s_id); /* subject source */
  1859. xid = fc_host_port_id(lport->host) == sid ?
  1860. ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
  1861. ep = fc_exch_lookup(lport, xid);
  1862. explan = ELS_EXPL_OXID_RXID;
  1863. if (!ep)
  1864. goto reject;
  1865. spin_lock_bh(&ep->ex_lock);
  1866. if (ep->oxid != ntohs(rp->rrq_ox_id))
  1867. goto unlock_reject;
  1868. if (ep->rxid != ntohs(rp->rrq_rx_id) &&
  1869. ep->rxid != FC_XID_UNKNOWN)
  1870. goto unlock_reject;
  1871. explan = ELS_EXPL_SID;
  1872. if (ep->sid != sid)
  1873. goto unlock_reject;
  1874. /*
  1875. * Clear Recovery Qualifier state, and cancel timer if complete.
  1876. */
  1877. if (ep->esb_stat & ESB_ST_REC_QUAL) {
  1878. ep->esb_stat &= ~ESB_ST_REC_QUAL;
  1879. atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
  1880. }
  1881. if (ep->esb_stat & ESB_ST_COMPLETE) {
  1882. if (cancel_delayed_work(&ep->timeout_work))
  1883. atomic_dec(&ep->ex_refcnt); /* drop timer hold */
  1884. }
  1885. spin_unlock_bh(&ep->ex_lock);
  1886. /*
  1887. * Send LS_ACC.
  1888. */
  1889. fc_seq_ls_acc(fp);
  1890. goto out;
  1891. unlock_reject:
  1892. spin_unlock_bh(&ep->ex_lock);
  1893. reject:
  1894. fc_seq_ls_rjt(fp, ELS_RJT_LOGIC, explan);
  1895. out:
  1896. if (ep)
  1897. fc_exch_release(ep); /* drop hold from fc_exch_find */
  1898. }
  1899. /**
  1900. * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
  1901. * @lport: The local port to add the exchange manager to
  1902. * @mp: The exchange manager to be added to the local port
  1903. * @match: The match routine that indicates when this EM should be used
  1904. */
  1905. struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
  1906. struct fc_exch_mgr *mp,
  1907. bool (*match)(struct fc_frame *))
  1908. {
  1909. struct fc_exch_mgr_anchor *ema;
  1910. ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
  1911. if (!ema)
  1912. return ema;
  1913. ema->mp = mp;
  1914. ema->match = match;
  1915. /* add EM anchor to EM anchors list */
  1916. list_add_tail(&ema->ema_list, &lport->ema_list);
  1917. kref_get(&mp->kref);
  1918. return ema;
  1919. }
  1920. EXPORT_SYMBOL(fc_exch_mgr_add);
  1921. /**
  1922. * fc_exch_mgr_destroy() - Destroy an exchange manager
  1923. * @kref: The reference to the EM to be destroyed
  1924. */
  1925. static void fc_exch_mgr_destroy(struct kref *kref)
  1926. {
  1927. struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
  1928. mempool_destroy(mp->ep_pool);
  1929. free_percpu(mp->pool);
  1930. kfree(mp);
  1931. }
  1932. /**
  1933. * fc_exch_mgr_del() - Delete an EM from a local port's list
  1934. * @ema: The exchange manager anchor identifying the EM to be deleted
  1935. */
  1936. void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
  1937. {
  1938. /* remove EM anchor from EM anchors list */
  1939. list_del(&ema->ema_list);
  1940. kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
  1941. kfree(ema);
  1942. }
  1943. EXPORT_SYMBOL(fc_exch_mgr_del);
  1944. /**
  1945. * fc_exch_mgr_list_clone() - Share all exchange manager objects
  1946. * @src: Source lport to clone exchange managers from
  1947. * @dst: New lport that takes references to all the exchange managers
  1948. */
  1949. int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
  1950. {
  1951. struct fc_exch_mgr_anchor *ema, *tmp;
  1952. list_for_each_entry(ema, &src->ema_list, ema_list) {
  1953. if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
  1954. goto err;
  1955. }
  1956. return 0;
  1957. err:
  1958. list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
  1959. fc_exch_mgr_del(ema);
  1960. return -ENOMEM;
  1961. }
  1962. /**
  1963. * fc_exch_mgr_alloc() - Allocate an exchange manager
  1964. * @lport: The local port that the new EM will be associated with
  1965. * @class: The default FC class for new exchanges
  1966. * @min_xid: The minimum XID for exchanges from the new EM
  1967. * @max_xid: The maximum XID for exchanges from the new EM
  1968. * @match: The match routine for the new EM
  1969. */
  1970. struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
  1971. enum fc_class class,
  1972. u16 min_xid, u16 max_xid,
  1973. bool (*match)(struct fc_frame *))
  1974. {
  1975. struct fc_exch_mgr *mp;
  1976. u16 pool_exch_range;
  1977. size_t pool_size;
  1978. unsigned int cpu;
  1979. struct fc_exch_pool *pool;
  1980. if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
  1981. (min_xid & fc_cpu_mask) != 0) {
  1982. FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
  1983. min_xid, max_xid);
  1984. return NULL;
  1985. }
  1986. /*
  1987. * allocate memory for EM
  1988. */
  1989. mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
  1990. if (!mp)
  1991. return NULL;
  1992. mp->class = class;
  1993. /* adjust em exch xid range for offload */
  1994. mp->min_xid = min_xid;
  1995. mp->max_xid = max_xid;
  1996. mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
  1997. if (!mp->ep_pool)
  1998. goto free_mp;
  1999. /*
  2000. * Setup per cpu exch pool with entire exchange id range equally
  2001. * divided across all cpus. The exch pointers array memory is
  2002. * allocated for exch range per pool.
  2003. */
  2004. pool_exch_range = (mp->max_xid - mp->min_xid + 1) / (fc_cpu_mask + 1);
  2005. mp->pool_max_index = pool_exch_range - 1;
  2006. /*
  2007. * Allocate and initialize per cpu exch pool
  2008. */
  2009. pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
  2010. mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
  2011. if (!mp->pool)
  2012. goto free_mempool;
  2013. for_each_possible_cpu(cpu) {
  2014. pool = per_cpu_ptr(mp->pool, cpu);
  2015. pool->left = FC_XID_UNKNOWN;
  2016. pool->right = FC_XID_UNKNOWN;
  2017. spin_lock_init(&pool->lock);
  2018. INIT_LIST_HEAD(&pool->ex_list);
  2019. }
  2020. kref_init(&mp->kref);
  2021. if (!fc_exch_mgr_add(lport, mp, match)) {
  2022. free_percpu(mp->pool);
  2023. goto free_mempool;
  2024. }
  2025. /*
  2026. * Above kref_init() sets mp->kref to 1 and then
  2027. * call to fc_exch_mgr_add incremented mp->kref again,
  2028. * so adjust that extra increment.
  2029. */
  2030. kref_put(&mp->kref, fc_exch_mgr_destroy);
  2031. return mp;
  2032. free_mempool:
  2033. mempool_destroy(mp->ep_pool);
  2034. free_mp:
  2035. kfree(mp);
  2036. return NULL;
  2037. }
  2038. EXPORT_SYMBOL(fc_exch_mgr_alloc);
  2039. /**
  2040. * fc_exch_mgr_free() - Free all exchange managers on a local port
  2041. * @lport: The local port whose EMs are to be freed
  2042. */
  2043. void fc_exch_mgr_free(struct fc_lport *lport)
  2044. {
  2045. struct fc_exch_mgr_anchor *ema, *next;
  2046. flush_workqueue(fc_exch_workqueue);
  2047. list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
  2048. fc_exch_mgr_del(ema);
  2049. }
  2050. EXPORT_SYMBOL(fc_exch_mgr_free);
  2051. /**
  2052. * fc_exch_recv() - Handler for received frames
  2053. * @lport: The local port the frame was received on
  2054. * @fp: The received frame
  2055. */
  2056. void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
  2057. {
  2058. struct fc_frame_header *fh = fc_frame_header_get(fp);
  2059. struct fc_exch_mgr_anchor *ema;
  2060. u32 f_ctl, found = 0;
  2061. u16 oxid;
  2062. /* lport lock ? */
  2063. if (!lport || lport->state == LPORT_ST_DISABLED) {
  2064. FC_LPORT_DBG(lport, "Receiving frames for an lport that "
  2065. "has not been initialized correctly\n");
  2066. fc_frame_free(fp);
  2067. return;
  2068. }
  2069. f_ctl = ntoh24(fh->fh_f_ctl);
  2070. oxid = ntohs(fh->fh_ox_id);
  2071. if (f_ctl & FC_FC_EX_CTX) {
  2072. list_for_each_entry(ema, &lport->ema_list, ema_list) {
  2073. if ((oxid >= ema->mp->min_xid) &&
  2074. (oxid <= ema->mp->max_xid)) {
  2075. found = 1;
  2076. break;
  2077. }
  2078. }
  2079. if (!found) {
  2080. FC_LPORT_DBG(lport, "Received response for out "
  2081. "of range oxid:%hx\n", oxid);
  2082. fc_frame_free(fp);
  2083. return;
  2084. }
  2085. } else
  2086. ema = list_entry(lport->ema_list.prev, typeof(*ema), ema_list);
  2087. /*
  2088. * If frame is marked invalid, just drop it.
  2089. */
  2090. switch (fr_eof(fp)) {
  2091. case FC_EOF_T:
  2092. if (f_ctl & FC_FC_END_SEQ)
  2093. skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
  2094. /* fall through */
  2095. case FC_EOF_N:
  2096. if (fh->fh_type == FC_TYPE_BLS)
  2097. fc_exch_recv_bls(ema->mp, fp);
  2098. else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
  2099. FC_FC_EX_CTX)
  2100. fc_exch_recv_seq_resp(ema->mp, fp);
  2101. else if (f_ctl & FC_FC_SEQ_CTX)
  2102. fc_exch_recv_resp(ema->mp, fp);
  2103. else /* no EX_CTX and no SEQ_CTX */
  2104. fc_exch_recv_req(lport, ema->mp, fp);
  2105. break;
  2106. default:
  2107. FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
  2108. fr_eof(fp));
  2109. fc_frame_free(fp);
  2110. }
  2111. }
  2112. EXPORT_SYMBOL(fc_exch_recv);
  2113. /**
  2114. * fc_exch_init() - Initialize the exchange layer for a local port
  2115. * @lport: The local port to initialize the exchange layer for
  2116. */
  2117. int fc_exch_init(struct fc_lport *lport)
  2118. {
  2119. if (!lport->tt.seq_start_next)
  2120. lport->tt.seq_start_next = fc_seq_start_next;
  2121. if (!lport->tt.seq_set_resp)
  2122. lport->tt.seq_set_resp = fc_seq_set_resp;
  2123. if (!lport->tt.exch_seq_send)
  2124. lport->tt.exch_seq_send = fc_exch_seq_send;
  2125. if (!lport->tt.seq_send)
  2126. lport->tt.seq_send = fc_seq_send;
  2127. if (!lport->tt.seq_els_rsp_send)
  2128. lport->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
  2129. if (!lport->tt.exch_done)
  2130. lport->tt.exch_done = fc_exch_done;
  2131. if (!lport->tt.exch_mgr_reset)
  2132. lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
  2133. if (!lport->tt.seq_exch_abort)
  2134. lport->tt.seq_exch_abort = fc_seq_exch_abort;
  2135. if (!lport->tt.seq_assign)
  2136. lport->tt.seq_assign = fc_seq_assign;
  2137. return 0;
  2138. }
  2139. EXPORT_SYMBOL(fc_exch_init);
  2140. /**
  2141. * fc_setup_exch_mgr() - Setup an exchange manager
  2142. */
  2143. int fc_setup_exch_mgr(void)
  2144. {
  2145. fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
  2146. 0, SLAB_HWCACHE_ALIGN, NULL);
  2147. if (!fc_em_cachep)
  2148. return -ENOMEM;
  2149. /*
  2150. * Initialize fc_cpu_mask and fc_cpu_order. The
  2151. * fc_cpu_mask is set for nr_cpu_ids rounded up
  2152. * to order of 2's * power and order is stored
  2153. * in fc_cpu_order as this is later required in
  2154. * mapping between an exch id and exch array index
  2155. * in per cpu exch pool.
  2156. *
  2157. * This round up is required to align fc_cpu_mask
  2158. * to exchange id's lower bits such that all incoming
  2159. * frames of an exchange gets delivered to the same
  2160. * cpu on which exchange originated by simple bitwise
  2161. * AND operation between fc_cpu_mask and exchange id.
  2162. */
  2163. fc_cpu_mask = 1;
  2164. fc_cpu_order = 0;
  2165. while (fc_cpu_mask < nr_cpu_ids) {
  2166. fc_cpu_mask <<= 1;
  2167. fc_cpu_order++;
  2168. }
  2169. fc_cpu_mask--;
  2170. fc_exch_workqueue = create_singlethread_workqueue("fc_exch_workqueue");
  2171. if (!fc_exch_workqueue)
  2172. return -ENOMEM;
  2173. return 0;
  2174. }
  2175. /**
  2176. * fc_destroy_exch_mgr() - Destroy an exchange manager
  2177. */
  2178. void fc_destroy_exch_mgr(void)
  2179. {
  2180. destroy_workqueue(fc_exch_workqueue);
  2181. kmem_cache_destroy(fc_em_cachep);
  2182. }