fc_exch.c 49 KB

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