fc_exch.c 52 KB

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