fc_exch.c 48 KB

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