fc_exch.c 66 KB

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