fc_exch.c 49 KB

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