fc_exch.c 66 KB

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