fc_exch.c 62 KB

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