fc_exch.c 49 KB

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