fc_exch.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  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_frame_is_read(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. if (unlikely(lp->tt.frame_send(lp, fp)))
  1660. goto err;
  1661. if (timer_msec)
  1662. fc_exch_timer_set_locked(ep, timer_msec);
  1663. ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
  1664. if (ep->f_ctl & FC_FC_SEQ_INIT)
  1665. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  1666. spin_unlock_bh(&ep->ex_lock);
  1667. return sp;
  1668. err:
  1669. rc = fc_exch_done_locked(ep);
  1670. spin_unlock_bh(&ep->ex_lock);
  1671. if (!rc)
  1672. fc_exch_mgr_delete_ep(ep);
  1673. return NULL;
  1674. }
  1675. EXPORT_SYMBOL(fc_exch_seq_send);
  1676. /*
  1677. * Receive a frame
  1678. */
  1679. void fc_exch_recv(struct fc_lport *lp, struct fc_exch_mgr *mp,
  1680. struct fc_frame *fp)
  1681. {
  1682. struct fc_frame_header *fh = fc_frame_header_get(fp);
  1683. u32 f_ctl;
  1684. /* lport lock ? */
  1685. if (!lp || !mp || (lp->state == LPORT_ST_NONE)) {
  1686. FC_DBG("fc_lport or EM is not allocated and configured");
  1687. fc_frame_free(fp);
  1688. return;
  1689. }
  1690. /*
  1691. * If frame is marked invalid, just drop it.
  1692. */
  1693. f_ctl = ntoh24(fh->fh_f_ctl);
  1694. switch (fr_eof(fp)) {
  1695. case FC_EOF_T:
  1696. if (f_ctl & FC_FC_END_SEQ)
  1697. skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
  1698. /* fall through */
  1699. case FC_EOF_N:
  1700. if (fh->fh_type == FC_TYPE_BLS)
  1701. fc_exch_recv_bls(mp, fp);
  1702. else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
  1703. FC_FC_EX_CTX)
  1704. fc_exch_recv_seq_resp(mp, fp);
  1705. else if (f_ctl & FC_FC_SEQ_CTX)
  1706. fc_exch_recv_resp(mp, fp);
  1707. else
  1708. fc_exch_recv_req(lp, mp, fp);
  1709. break;
  1710. default:
  1711. FC_DBG("dropping invalid frame (eof %x)", fr_eof(fp));
  1712. fc_frame_free(fp);
  1713. break;
  1714. }
  1715. }
  1716. EXPORT_SYMBOL(fc_exch_recv);
  1717. int fc_exch_init(struct fc_lport *lp)
  1718. {
  1719. if (!lp->tt.exch_get) {
  1720. /*
  1721. * exch_put() should be NULL if
  1722. * exch_get() is NULL
  1723. */
  1724. WARN_ON(lp->tt.exch_put);
  1725. lp->tt.exch_get = fc_exch_get;
  1726. }
  1727. if (!lp->tt.seq_start_next)
  1728. lp->tt.seq_start_next = fc_seq_start_next;
  1729. if (!lp->tt.exch_seq_send)
  1730. lp->tt.exch_seq_send = fc_exch_seq_send;
  1731. if (!lp->tt.seq_send)
  1732. lp->tt.seq_send = fc_seq_send;
  1733. if (!lp->tt.seq_els_rsp_send)
  1734. lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
  1735. if (!lp->tt.exch_done)
  1736. lp->tt.exch_done = fc_exch_done;
  1737. if (!lp->tt.exch_mgr_reset)
  1738. lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
  1739. if (!lp->tt.seq_exch_abort)
  1740. lp->tt.seq_exch_abort = fc_seq_exch_abort;
  1741. return 0;
  1742. }
  1743. EXPORT_SYMBOL(fc_exch_init);
  1744. int fc_setup_exch_mgr(void)
  1745. {
  1746. fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
  1747. 0, SLAB_HWCACHE_ALIGN, NULL);
  1748. if (!fc_em_cachep)
  1749. return -ENOMEM;
  1750. return 0;
  1751. }
  1752. void fc_destroy_exch_mgr(void)
  1753. {
  1754. kmem_cache_destroy(fc_em_cachep);
  1755. }