fc_exch.c 53 KB

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