fc_exch.c 71 KB

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