fc_exch.c 61 KB

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