fc_exch.c 71 KB

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