fc_exch.c 68 KB

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