fc_exch.c 65 KB

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