fc_exch.c 48 KB

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