fc_exch.c 52 KB

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