fc_exch.c 66 KB

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