fc_exch.c 64 KB

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