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. 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. /*
  382. * Exchange timeout - handle exchange timer expiration.
  383. * The timer will have been cancelled before this is called.
  384. */
  385. static void fc_exch_timeout(struct work_struct *work)
  386. {
  387. struct fc_exch *ep = container_of(work, struct fc_exch,
  388. timeout_work.work);
  389. struct fc_seq *sp = &ep->seq;
  390. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  391. void *arg;
  392. u32 e_stat;
  393. int rc = 1;
  394. FC_EXCH_DBG(ep, "Exchange timed out\n");
  395. spin_lock_bh(&ep->ex_lock);
  396. if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
  397. goto unlock;
  398. e_stat = ep->esb_stat;
  399. if (e_stat & ESB_ST_COMPLETE) {
  400. ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
  401. spin_unlock_bh(&ep->ex_lock);
  402. if (e_stat & ESB_ST_REC_QUAL)
  403. fc_exch_rrq(ep);
  404. goto done;
  405. } else {
  406. resp = ep->resp;
  407. arg = ep->arg;
  408. ep->resp = NULL;
  409. if (e_stat & ESB_ST_ABNORMAL)
  410. rc = fc_exch_done_locked(ep);
  411. spin_unlock_bh(&ep->ex_lock);
  412. if (!rc)
  413. fc_exch_delete(ep);
  414. if (resp)
  415. resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
  416. fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
  417. goto done;
  418. }
  419. unlock:
  420. spin_unlock_bh(&ep->ex_lock);
  421. done:
  422. /*
  423. * This release matches the hold taken when the timer was set.
  424. */
  425. fc_exch_release(ep);
  426. }
  427. /*
  428. * Allocate a sequence.
  429. *
  430. * We don't support multiple originated sequences on the same exchange.
  431. * By implication, any previously originated sequence on this exchange
  432. * is complete, and we reallocate the same sequence.
  433. */
  434. static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
  435. {
  436. struct fc_seq *sp;
  437. sp = &ep->seq;
  438. sp->ssb_stat = 0;
  439. sp->cnt = 0;
  440. sp->id = seq_id;
  441. return sp;
  442. }
  443. /**
  444. * fc_exch_em_alloc() - allocate an exchange from a specified EM.
  445. * @lport: ptr to the local port
  446. * @mp: ptr to the exchange manager
  447. *
  448. * Returns pointer to allocated fc_exch with exch lock held.
  449. */
  450. static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
  451. struct fc_exch_mgr *mp)
  452. {
  453. struct fc_exch *ep;
  454. unsigned int cpu;
  455. u16 index;
  456. struct fc_exch_pool *pool;
  457. /* allocate memory for exchange */
  458. ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
  459. if (!ep) {
  460. atomic_inc(&mp->stats.no_free_exch);
  461. goto out;
  462. }
  463. memset(ep, 0, sizeof(*ep));
  464. cpu = smp_processor_id();
  465. pool = per_cpu_ptr(mp->pool, cpu);
  466. spin_lock_bh(&pool->lock);
  467. index = pool->next_index;
  468. /* allocate new exch from pool */
  469. while (fc_exch_ptr_get(pool, index)) {
  470. index = index == mp->pool_max_index ? 0 : index + 1;
  471. if (index == pool->next_index)
  472. goto err;
  473. }
  474. pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
  475. fc_exch_hold(ep); /* hold for exch in mp */
  476. spin_lock_init(&ep->ex_lock);
  477. /*
  478. * Hold exch lock for caller to prevent fc_exch_reset()
  479. * from releasing exch while fc_exch_alloc() caller is
  480. * still working on exch.
  481. */
  482. spin_lock_bh(&ep->ex_lock);
  483. fc_exch_ptr_set(pool, index, ep);
  484. list_add_tail(&ep->ex_list, &pool->ex_list);
  485. fc_seq_alloc(ep, ep->seq_id++);
  486. pool->total_exches++;
  487. spin_unlock_bh(&pool->lock);
  488. /*
  489. * update exchange
  490. */
  491. ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
  492. ep->em = mp;
  493. ep->pool = pool;
  494. ep->lp = lport;
  495. ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
  496. ep->rxid = FC_XID_UNKNOWN;
  497. ep->class = mp->class;
  498. INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
  499. out:
  500. return ep;
  501. err:
  502. spin_unlock_bh(&pool->lock);
  503. atomic_inc(&mp->stats.no_free_exch_xid);
  504. mempool_free(ep, mp->ep_pool);
  505. return NULL;
  506. }
  507. /**
  508. * fc_exch_alloc() - allocate an exchange.
  509. * @lport: ptr to the local port
  510. * @fp: ptr to the FC frame
  511. *
  512. * This function walks the list of the exchange manager(EM)
  513. * anchors to select a EM for new exchange allocation. The
  514. * EM is selected having either a NULL match function pointer
  515. * or call to match function returning true.
  516. */
  517. struct fc_exch *fc_exch_alloc(struct fc_lport *lport, struct fc_frame *fp)
  518. {
  519. struct fc_exch_mgr_anchor *ema;
  520. struct fc_exch *ep;
  521. list_for_each_entry(ema, &lport->ema_list, ema_list) {
  522. if (!ema->match || ema->match(fp)) {
  523. ep = fc_exch_em_alloc(lport, ema->mp);
  524. if (ep)
  525. return ep;
  526. }
  527. }
  528. return NULL;
  529. }
  530. EXPORT_SYMBOL(fc_exch_alloc);
  531. /*
  532. * Lookup and hold an exchange.
  533. */
  534. static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
  535. {
  536. struct fc_exch_pool *pool;
  537. struct fc_exch *ep = NULL;
  538. if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
  539. pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
  540. spin_lock_bh(&pool->lock);
  541. ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
  542. if (ep) {
  543. fc_exch_hold(ep);
  544. WARN_ON(ep->xid != xid);
  545. }
  546. spin_unlock_bh(&pool->lock);
  547. }
  548. return ep;
  549. }
  550. void fc_exch_done(struct fc_seq *sp)
  551. {
  552. struct fc_exch *ep = fc_seq_exch(sp);
  553. int rc;
  554. spin_lock_bh(&ep->ex_lock);
  555. rc = fc_exch_done_locked(ep);
  556. spin_unlock_bh(&ep->ex_lock);
  557. if (!rc)
  558. fc_exch_delete(ep);
  559. }
  560. EXPORT_SYMBOL(fc_exch_done);
  561. /*
  562. * Allocate a new exchange as responder.
  563. * Sets the responder ID in the frame header.
  564. */
  565. static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
  566. struct fc_exch_mgr *mp,
  567. struct fc_frame *fp)
  568. {
  569. struct fc_exch *ep;
  570. struct fc_frame_header *fh;
  571. ep = fc_exch_alloc(lport, fp);
  572. if (ep) {
  573. ep->class = fc_frame_class(fp);
  574. /*
  575. * Set EX_CTX indicating we're responding on this exchange.
  576. */
  577. ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
  578. ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
  579. fh = fc_frame_header_get(fp);
  580. ep->sid = ntoh24(fh->fh_d_id);
  581. ep->did = ntoh24(fh->fh_s_id);
  582. ep->oid = ep->did;
  583. /*
  584. * Allocated exchange has placed the XID in the
  585. * originator field. Move it to the responder field,
  586. * and set the originator XID from the frame.
  587. */
  588. ep->rxid = ep->xid;
  589. ep->oxid = ntohs(fh->fh_ox_id);
  590. ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
  591. if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
  592. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  593. fc_exch_hold(ep); /* hold for caller */
  594. spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */
  595. }
  596. return ep;
  597. }
  598. /*
  599. * Find a sequence for receive where the other end is originating the sequence.
  600. * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
  601. * on the ep that should be released by the caller.
  602. */
  603. static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
  604. struct fc_exch_mgr *mp,
  605. struct fc_frame *fp)
  606. {
  607. struct fc_frame_header *fh = fc_frame_header_get(fp);
  608. struct fc_exch *ep = NULL;
  609. struct fc_seq *sp = NULL;
  610. enum fc_pf_rjt_reason reject = FC_RJT_NONE;
  611. u32 f_ctl;
  612. u16 xid;
  613. f_ctl = ntoh24(fh->fh_f_ctl);
  614. WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
  615. /*
  616. * Lookup or create the exchange if we will be creating the sequence.
  617. */
  618. if (f_ctl & FC_FC_EX_CTX) {
  619. xid = ntohs(fh->fh_ox_id); /* we originated exch */
  620. ep = fc_exch_find(mp, xid);
  621. if (!ep) {
  622. atomic_inc(&mp->stats.xid_not_found);
  623. reject = FC_RJT_OX_ID;
  624. goto out;
  625. }
  626. if (ep->rxid == FC_XID_UNKNOWN)
  627. ep->rxid = ntohs(fh->fh_rx_id);
  628. else if (ep->rxid != ntohs(fh->fh_rx_id)) {
  629. reject = FC_RJT_OX_ID;
  630. goto rel;
  631. }
  632. } else {
  633. xid = ntohs(fh->fh_rx_id); /* we are the responder */
  634. /*
  635. * Special case for MDS issuing an ELS TEST with a
  636. * bad rxid of 0.
  637. * XXX take this out once we do the proper reject.
  638. */
  639. if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
  640. fc_frame_payload_op(fp) == ELS_TEST) {
  641. fh->fh_rx_id = htons(FC_XID_UNKNOWN);
  642. xid = FC_XID_UNKNOWN;
  643. }
  644. /*
  645. * new sequence - find the exchange
  646. */
  647. ep = fc_exch_find(mp, xid);
  648. if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
  649. if (ep) {
  650. atomic_inc(&mp->stats.xid_busy);
  651. reject = FC_RJT_RX_ID;
  652. goto rel;
  653. }
  654. ep = fc_exch_resp(lport, mp, fp);
  655. if (!ep) {
  656. reject = FC_RJT_EXCH_EST; /* XXX */
  657. goto out;
  658. }
  659. xid = ep->xid; /* get our XID */
  660. } else if (!ep) {
  661. atomic_inc(&mp->stats.xid_not_found);
  662. reject = FC_RJT_RX_ID; /* XID not found */
  663. goto out;
  664. }
  665. }
  666. /*
  667. * At this point, we have the exchange held.
  668. * Find or create the sequence.
  669. */
  670. if (fc_sof_is_init(fr_sof(fp))) {
  671. sp = fc_seq_start_next(&ep->seq);
  672. if (!sp) {
  673. reject = FC_RJT_SEQ_XS; /* exchange shortage */
  674. goto rel;
  675. }
  676. sp->id = fh->fh_seq_id;
  677. sp->ssb_stat |= SSB_ST_RESP;
  678. } else {
  679. sp = &ep->seq;
  680. if (sp->id != fh->fh_seq_id) {
  681. atomic_inc(&mp->stats.seq_not_found);
  682. reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
  683. goto rel;
  684. }
  685. }
  686. WARN_ON(ep != fc_seq_exch(sp));
  687. if (f_ctl & FC_FC_SEQ_INIT)
  688. ep->esb_stat |= ESB_ST_SEQ_INIT;
  689. fr_seq(fp) = sp;
  690. out:
  691. return reject;
  692. rel:
  693. fc_exch_done(&ep->seq);
  694. fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
  695. return reject;
  696. }
  697. /*
  698. * Find the sequence for a frame being received.
  699. * We originated the sequence, so it should be found.
  700. * We may or may not have originated the exchange.
  701. * Does not hold the sequence for the caller.
  702. */
  703. static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
  704. struct fc_frame *fp)
  705. {
  706. struct fc_frame_header *fh = fc_frame_header_get(fp);
  707. struct fc_exch *ep;
  708. struct fc_seq *sp = NULL;
  709. u32 f_ctl;
  710. u16 xid;
  711. f_ctl = ntoh24(fh->fh_f_ctl);
  712. WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
  713. xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
  714. ep = fc_exch_find(mp, xid);
  715. if (!ep)
  716. return NULL;
  717. if (ep->seq.id == fh->fh_seq_id) {
  718. /*
  719. * Save the RX_ID if we didn't previously know it.
  720. */
  721. sp = &ep->seq;
  722. if ((f_ctl & FC_FC_EX_CTX) != 0 &&
  723. ep->rxid == FC_XID_UNKNOWN) {
  724. ep->rxid = ntohs(fh->fh_rx_id);
  725. }
  726. }
  727. fc_exch_release(ep);
  728. return sp;
  729. }
  730. /*
  731. * Set addresses for an exchange.
  732. * Note this must be done before the first sequence of the exchange is sent.
  733. */
  734. static void fc_exch_set_addr(struct fc_exch *ep,
  735. u32 orig_id, u32 resp_id)
  736. {
  737. ep->oid = orig_id;
  738. if (ep->esb_stat & ESB_ST_RESP) {
  739. ep->sid = resp_id;
  740. ep->did = orig_id;
  741. } else {
  742. ep->sid = orig_id;
  743. ep->did = resp_id;
  744. }
  745. }
  746. static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
  747. {
  748. struct fc_exch *ep = fc_seq_exch(sp);
  749. sp = fc_seq_alloc(ep, ep->seq_id++);
  750. FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
  751. ep->f_ctl, sp->id);
  752. return sp;
  753. }
  754. /*
  755. * Allocate a new sequence on the same exchange as the supplied sequence.
  756. * This will never return NULL.
  757. */
  758. struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
  759. {
  760. struct fc_exch *ep = fc_seq_exch(sp);
  761. spin_lock_bh(&ep->ex_lock);
  762. sp = fc_seq_start_next_locked(sp);
  763. spin_unlock_bh(&ep->ex_lock);
  764. return sp;
  765. }
  766. EXPORT_SYMBOL(fc_seq_start_next);
  767. int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
  768. {
  769. struct fc_exch *ep;
  770. struct fc_frame_header *fh = fc_frame_header_get(fp);
  771. int error;
  772. u32 f_ctl;
  773. ep = fc_seq_exch(sp);
  774. WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
  775. f_ctl = ntoh24(fh->fh_f_ctl);
  776. fc_exch_setup_hdr(ep, fp, f_ctl);
  777. /*
  778. * update sequence count if this frame is carrying
  779. * multiple FC frames when sequence offload is enabled
  780. * by LLD.
  781. */
  782. if (fr_max_payload(fp))
  783. sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
  784. fr_max_payload(fp));
  785. else
  786. sp->cnt++;
  787. /*
  788. * Send the frame.
  789. */
  790. error = lp->tt.frame_send(lp, fp);
  791. /*
  792. * Update the exchange and sequence flags,
  793. * assuming all frames for the sequence have been sent.
  794. * We can only be called to send once for each sequence.
  795. */
  796. spin_lock_bh(&ep->ex_lock);
  797. ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
  798. if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
  799. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  800. spin_unlock_bh(&ep->ex_lock);
  801. return error;
  802. }
  803. EXPORT_SYMBOL(fc_seq_send);
  804. void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
  805. struct fc_seq_els_data *els_data)
  806. {
  807. switch (els_cmd) {
  808. case ELS_LS_RJT:
  809. fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
  810. break;
  811. case ELS_LS_ACC:
  812. fc_seq_ls_acc(sp);
  813. break;
  814. case ELS_RRQ:
  815. fc_exch_els_rrq(sp, els_data->fp);
  816. break;
  817. case ELS_REC:
  818. fc_exch_els_rec(sp, els_data->fp);
  819. break;
  820. default:
  821. FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
  822. }
  823. }
  824. EXPORT_SYMBOL(fc_seq_els_rsp_send);
  825. /*
  826. * Send a sequence, which is also the last sequence in the exchange.
  827. */
  828. static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
  829. enum fc_rctl rctl, enum fc_fh_type fh_type)
  830. {
  831. u32 f_ctl;
  832. struct fc_exch *ep = fc_seq_exch(sp);
  833. f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  834. f_ctl |= ep->f_ctl;
  835. fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
  836. fc_seq_send(ep->lp, sp, fp);
  837. }
  838. /*
  839. * Send ACK_1 (or equiv.) indicating we received something.
  840. * The frame we're acking is supplied.
  841. */
  842. static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
  843. {
  844. struct fc_frame *fp;
  845. struct fc_frame_header *rx_fh;
  846. struct fc_frame_header *fh;
  847. struct fc_exch *ep = fc_seq_exch(sp);
  848. struct fc_lport *lp = ep->lp;
  849. unsigned int f_ctl;
  850. /*
  851. * Don't send ACKs for class 3.
  852. */
  853. if (fc_sof_needs_ack(fr_sof(rx_fp))) {
  854. fp = fc_frame_alloc(lp, 0);
  855. if (!fp)
  856. return;
  857. fh = fc_frame_header_get(fp);
  858. fh->fh_r_ctl = FC_RCTL_ACK_1;
  859. fh->fh_type = FC_TYPE_BLS;
  860. /*
  861. * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
  862. * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
  863. * Bits 9-8 are meaningful (retransmitted or unidirectional).
  864. * Last ACK uses bits 7-6 (continue sequence),
  865. * bits 5-4 are meaningful (what kind of ACK to use).
  866. */
  867. rx_fh = fc_frame_header_get(rx_fp);
  868. f_ctl = ntoh24(rx_fh->fh_f_ctl);
  869. f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
  870. FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
  871. FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
  872. FC_FC_RETX_SEQ | FC_FC_UNI_TX;
  873. f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
  874. hton24(fh->fh_f_ctl, f_ctl);
  875. fc_exch_setup_hdr(ep, fp, f_ctl);
  876. fh->fh_seq_id = rx_fh->fh_seq_id;
  877. fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
  878. fh->fh_parm_offset = htonl(1); /* ack single frame */
  879. fr_sof(fp) = fr_sof(rx_fp);
  880. if (f_ctl & FC_FC_END_SEQ)
  881. fr_eof(fp) = FC_EOF_T;
  882. else
  883. fr_eof(fp) = FC_EOF_N;
  884. (void) lp->tt.frame_send(lp, fp);
  885. }
  886. }
  887. /*
  888. * Send BLS Reject.
  889. * This is for rejecting BA_ABTS only.
  890. */
  891. static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
  892. enum fc_ba_rjt_reason reason,
  893. enum fc_ba_rjt_explan explan)
  894. {
  895. struct fc_frame *fp;
  896. struct fc_frame_header *rx_fh;
  897. struct fc_frame_header *fh;
  898. struct fc_ba_rjt *rp;
  899. struct fc_lport *lp;
  900. unsigned int f_ctl;
  901. lp = fr_dev(rx_fp);
  902. fp = fc_frame_alloc(lp, sizeof(*rp));
  903. if (!fp)
  904. return;
  905. fh = fc_frame_header_get(fp);
  906. rx_fh = fc_frame_header_get(rx_fp);
  907. memset(fh, 0, sizeof(*fh) + sizeof(*rp));
  908. rp = fc_frame_payload_get(fp, sizeof(*rp));
  909. rp->br_reason = reason;
  910. rp->br_explan = explan;
  911. /*
  912. * seq_id, cs_ctl, df_ctl and param/offset are zero.
  913. */
  914. memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
  915. memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
  916. fh->fh_ox_id = rx_fh->fh_ox_id;
  917. fh->fh_rx_id = rx_fh->fh_rx_id;
  918. fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
  919. fh->fh_r_ctl = FC_RCTL_BA_RJT;
  920. fh->fh_type = FC_TYPE_BLS;
  921. /*
  922. * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
  923. * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
  924. * Bits 9-8 are meaningful (retransmitted or unidirectional).
  925. * Last ACK uses bits 7-6 (continue sequence),
  926. * bits 5-4 are meaningful (what kind of ACK to use).
  927. * Always set LAST_SEQ, END_SEQ.
  928. */
  929. f_ctl = ntoh24(rx_fh->fh_f_ctl);
  930. f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
  931. FC_FC_END_CONN | FC_FC_SEQ_INIT |
  932. FC_FC_RETX_SEQ | FC_FC_UNI_TX;
  933. f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
  934. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  935. f_ctl &= ~FC_FC_FIRST_SEQ;
  936. hton24(fh->fh_f_ctl, f_ctl);
  937. fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
  938. fr_eof(fp) = FC_EOF_T;
  939. if (fc_sof_needs_ack(fr_sof(fp)))
  940. fr_eof(fp) = FC_EOF_N;
  941. (void) lp->tt.frame_send(lp, fp);
  942. }
  943. /*
  944. * Handle an incoming ABTS. This would be for target mode usually,
  945. * but could be due to lost FCP transfer ready, confirm or RRQ.
  946. * We always handle this as an exchange abort, ignoring the parameter.
  947. */
  948. static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
  949. {
  950. struct fc_frame *fp;
  951. struct fc_ba_acc *ap;
  952. struct fc_frame_header *fh;
  953. struct fc_seq *sp;
  954. if (!ep)
  955. goto reject;
  956. spin_lock_bh(&ep->ex_lock);
  957. if (ep->esb_stat & ESB_ST_COMPLETE) {
  958. spin_unlock_bh(&ep->ex_lock);
  959. goto reject;
  960. }
  961. if (!(ep->esb_stat & ESB_ST_REC_QUAL))
  962. fc_exch_hold(ep); /* hold for REC_QUAL */
  963. ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
  964. fc_exch_timer_set_locked(ep, ep->r_a_tov);
  965. fp = fc_frame_alloc(ep->lp, sizeof(*ap));
  966. if (!fp) {
  967. spin_unlock_bh(&ep->ex_lock);
  968. goto free;
  969. }
  970. fh = fc_frame_header_get(fp);
  971. ap = fc_frame_payload_get(fp, sizeof(*ap));
  972. memset(ap, 0, sizeof(*ap));
  973. sp = &ep->seq;
  974. ap->ba_high_seq_cnt = htons(0xffff);
  975. if (sp->ssb_stat & SSB_ST_RESP) {
  976. ap->ba_seq_id = sp->id;
  977. ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
  978. ap->ba_high_seq_cnt = fh->fh_seq_cnt;
  979. ap->ba_low_seq_cnt = htons(sp->cnt);
  980. }
  981. sp = fc_seq_start_next_locked(sp);
  982. spin_unlock_bh(&ep->ex_lock);
  983. fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
  984. fc_frame_free(rx_fp);
  985. return;
  986. reject:
  987. fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
  988. free:
  989. fc_frame_free(rx_fp);
  990. }
  991. /*
  992. * Handle receive where the other end is originating the sequence.
  993. */
  994. static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
  995. struct fc_frame *fp)
  996. {
  997. struct fc_frame_header *fh = fc_frame_header_get(fp);
  998. struct fc_seq *sp = NULL;
  999. struct fc_exch *ep = NULL;
  1000. enum fc_sof sof;
  1001. enum fc_eof eof;
  1002. u32 f_ctl;
  1003. enum fc_pf_rjt_reason reject;
  1004. fr_seq(fp) = NULL;
  1005. reject = fc_seq_lookup_recip(lp, mp, fp);
  1006. if (reject == FC_RJT_NONE) {
  1007. sp = fr_seq(fp); /* sequence will be held */
  1008. ep = fc_seq_exch(sp);
  1009. sof = fr_sof(fp);
  1010. eof = fr_eof(fp);
  1011. f_ctl = ntoh24(fh->fh_f_ctl);
  1012. fc_seq_send_ack(sp, fp);
  1013. /*
  1014. * Call the receive function.
  1015. *
  1016. * The receive function may allocate a new sequence
  1017. * over the old one, so we shouldn't change the
  1018. * sequence after this.
  1019. *
  1020. * The frame will be freed by the receive function.
  1021. * If new exch resp handler is valid then call that
  1022. * first.
  1023. */
  1024. if (ep->resp)
  1025. ep->resp(sp, fp, ep->arg);
  1026. else
  1027. lp->tt.lport_recv(lp, sp, fp);
  1028. fc_exch_release(ep); /* release from lookup */
  1029. } else {
  1030. FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject);
  1031. fc_frame_free(fp);
  1032. }
  1033. }
  1034. /*
  1035. * Handle receive where the other end is originating the sequence in
  1036. * response to our exchange.
  1037. */
  1038. static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1039. {
  1040. struct fc_frame_header *fh = fc_frame_header_get(fp);
  1041. struct fc_seq *sp;
  1042. struct fc_exch *ep;
  1043. enum fc_sof sof;
  1044. u32 f_ctl;
  1045. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  1046. void *ex_resp_arg;
  1047. int rc;
  1048. ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
  1049. if (!ep) {
  1050. atomic_inc(&mp->stats.xid_not_found);
  1051. goto out;
  1052. }
  1053. if (ep->esb_stat & ESB_ST_COMPLETE) {
  1054. atomic_inc(&mp->stats.xid_not_found);
  1055. goto out;
  1056. }
  1057. if (ep->rxid == FC_XID_UNKNOWN)
  1058. ep->rxid = ntohs(fh->fh_rx_id);
  1059. if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
  1060. atomic_inc(&mp->stats.xid_not_found);
  1061. goto rel;
  1062. }
  1063. if (ep->did != ntoh24(fh->fh_s_id) &&
  1064. ep->did != FC_FID_FLOGI) {
  1065. atomic_inc(&mp->stats.xid_not_found);
  1066. goto rel;
  1067. }
  1068. sof = fr_sof(fp);
  1069. if (fc_sof_is_init(sof)) {
  1070. sp = fc_seq_start_next(&ep->seq);
  1071. sp->id = fh->fh_seq_id;
  1072. sp->ssb_stat |= SSB_ST_RESP;
  1073. } else {
  1074. sp = &ep->seq;
  1075. if (sp->id != fh->fh_seq_id) {
  1076. atomic_inc(&mp->stats.seq_not_found);
  1077. goto rel;
  1078. }
  1079. }
  1080. f_ctl = ntoh24(fh->fh_f_ctl);
  1081. fr_seq(fp) = sp;
  1082. if (f_ctl & FC_FC_SEQ_INIT)
  1083. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1084. if (fc_sof_needs_ack(sof))
  1085. fc_seq_send_ack(sp, fp);
  1086. resp = ep->resp;
  1087. ex_resp_arg = ep->arg;
  1088. if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
  1089. (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
  1090. (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
  1091. spin_lock_bh(&ep->ex_lock);
  1092. rc = fc_exch_done_locked(ep);
  1093. WARN_ON(fc_seq_exch(sp) != ep);
  1094. spin_unlock_bh(&ep->ex_lock);
  1095. if (!rc)
  1096. fc_exch_delete(ep);
  1097. }
  1098. /*
  1099. * Call the receive function.
  1100. * The sequence is held (has a refcnt) for us,
  1101. * but not for the receive function.
  1102. *
  1103. * The receive function may allocate a new sequence
  1104. * over the old one, so we shouldn't change the
  1105. * sequence after this.
  1106. *
  1107. * The frame will be freed by the receive function.
  1108. * If new exch resp handler is valid then call that
  1109. * first.
  1110. */
  1111. if (resp)
  1112. resp(sp, fp, ex_resp_arg);
  1113. else
  1114. fc_frame_free(fp);
  1115. fc_exch_release(ep);
  1116. return;
  1117. rel:
  1118. fc_exch_release(ep);
  1119. out:
  1120. fc_frame_free(fp);
  1121. }
  1122. /*
  1123. * Handle receive for a sequence where other end is responding to our sequence.
  1124. */
  1125. static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1126. {
  1127. struct fc_seq *sp;
  1128. sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
  1129. if (!sp)
  1130. atomic_inc(&mp->stats.xid_not_found);
  1131. else
  1132. atomic_inc(&mp->stats.non_bls_resp);
  1133. fc_frame_free(fp);
  1134. }
  1135. /*
  1136. * Handle the response to an ABTS for exchange or sequence.
  1137. * This can be BA_ACC or BA_RJT.
  1138. */
  1139. static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
  1140. {
  1141. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  1142. void *ex_resp_arg;
  1143. struct fc_frame_header *fh;
  1144. struct fc_ba_acc *ap;
  1145. struct fc_seq *sp;
  1146. u16 low;
  1147. u16 high;
  1148. int rc = 1, has_rec = 0;
  1149. fh = fc_frame_header_get(fp);
  1150. FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
  1151. fc_exch_rctl_name(fh->fh_r_ctl));
  1152. if (cancel_delayed_work_sync(&ep->timeout_work))
  1153. fc_exch_release(ep); /* release from pending timer hold */
  1154. spin_lock_bh(&ep->ex_lock);
  1155. switch (fh->fh_r_ctl) {
  1156. case FC_RCTL_BA_ACC:
  1157. ap = fc_frame_payload_get(fp, sizeof(*ap));
  1158. if (!ap)
  1159. break;
  1160. /*
  1161. * Decide whether to establish a Recovery Qualifier.
  1162. * We do this if there is a non-empty SEQ_CNT range and
  1163. * SEQ_ID is the same as the one we aborted.
  1164. */
  1165. low = ntohs(ap->ba_low_seq_cnt);
  1166. high = ntohs(ap->ba_high_seq_cnt);
  1167. if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
  1168. (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
  1169. ap->ba_seq_id == ep->seq_id) && low != high) {
  1170. ep->esb_stat |= ESB_ST_REC_QUAL;
  1171. fc_exch_hold(ep); /* hold for recovery qualifier */
  1172. has_rec = 1;
  1173. }
  1174. break;
  1175. case FC_RCTL_BA_RJT:
  1176. break;
  1177. default:
  1178. break;
  1179. }
  1180. resp = ep->resp;
  1181. ex_resp_arg = ep->arg;
  1182. /* do we need to do some other checks here. Can we reuse more of
  1183. * fc_exch_recv_seq_resp
  1184. */
  1185. sp = &ep->seq;
  1186. /*
  1187. * do we want to check END_SEQ as well as LAST_SEQ here?
  1188. */
  1189. if (ep->fh_type != FC_TYPE_FCP &&
  1190. ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
  1191. rc = fc_exch_done_locked(ep);
  1192. spin_unlock_bh(&ep->ex_lock);
  1193. if (!rc)
  1194. fc_exch_delete(ep);
  1195. if (resp)
  1196. resp(sp, fp, ex_resp_arg);
  1197. else
  1198. fc_frame_free(fp);
  1199. if (has_rec)
  1200. fc_exch_timer_set(ep, ep->r_a_tov);
  1201. }
  1202. /*
  1203. * Receive BLS sequence.
  1204. * This is always a sequence initiated by the remote side.
  1205. * We may be either the originator or recipient of the exchange.
  1206. */
  1207. static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1208. {
  1209. struct fc_frame_header *fh;
  1210. struct fc_exch *ep;
  1211. u32 f_ctl;
  1212. fh = fc_frame_header_get(fp);
  1213. f_ctl = ntoh24(fh->fh_f_ctl);
  1214. fr_seq(fp) = NULL;
  1215. ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
  1216. ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
  1217. if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
  1218. spin_lock_bh(&ep->ex_lock);
  1219. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1220. spin_unlock_bh(&ep->ex_lock);
  1221. }
  1222. if (f_ctl & FC_FC_SEQ_CTX) {
  1223. /*
  1224. * A response to a sequence we initiated.
  1225. * This should only be ACKs for class 2 or F.
  1226. */
  1227. switch (fh->fh_r_ctl) {
  1228. case FC_RCTL_ACK_1:
  1229. case FC_RCTL_ACK_0:
  1230. break;
  1231. default:
  1232. FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
  1233. fh->fh_r_ctl,
  1234. fc_exch_rctl_name(fh->fh_r_ctl));
  1235. break;
  1236. }
  1237. fc_frame_free(fp);
  1238. } else {
  1239. switch (fh->fh_r_ctl) {
  1240. case FC_RCTL_BA_RJT:
  1241. case FC_RCTL_BA_ACC:
  1242. if (ep)
  1243. fc_exch_abts_resp(ep, fp);
  1244. else
  1245. fc_frame_free(fp);
  1246. break;
  1247. case FC_RCTL_BA_ABTS:
  1248. fc_exch_recv_abts(ep, fp);
  1249. break;
  1250. default: /* ignore junk */
  1251. fc_frame_free(fp);
  1252. break;
  1253. }
  1254. }
  1255. if (ep)
  1256. fc_exch_release(ep); /* release hold taken by fc_exch_find */
  1257. }
  1258. /*
  1259. * Accept sequence with LS_ACC.
  1260. * If this fails due to allocation or transmit congestion, assume the
  1261. * originator will repeat the sequence.
  1262. */
  1263. static void fc_seq_ls_acc(struct fc_seq *req_sp)
  1264. {
  1265. struct fc_seq *sp;
  1266. struct fc_els_ls_acc *acc;
  1267. struct fc_frame *fp;
  1268. sp = fc_seq_start_next(req_sp);
  1269. fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
  1270. if (fp) {
  1271. acc = fc_frame_payload_get(fp, sizeof(*acc));
  1272. memset(acc, 0, sizeof(*acc));
  1273. acc->la_cmd = ELS_LS_ACC;
  1274. fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
  1275. }
  1276. }
  1277. /*
  1278. * Reject sequence with ELS LS_RJT.
  1279. * If this fails due to allocation or transmit congestion, assume the
  1280. * originator will repeat the sequence.
  1281. */
  1282. static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
  1283. enum fc_els_rjt_explan explan)
  1284. {
  1285. struct fc_seq *sp;
  1286. struct fc_els_ls_rjt *rjt;
  1287. struct fc_frame *fp;
  1288. sp = fc_seq_start_next(req_sp);
  1289. fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
  1290. if (fp) {
  1291. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1292. memset(rjt, 0, sizeof(*rjt));
  1293. rjt->er_cmd = ELS_LS_RJT;
  1294. rjt->er_reason = reason;
  1295. rjt->er_explan = explan;
  1296. fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
  1297. }
  1298. }
  1299. static void fc_exch_reset(struct fc_exch *ep)
  1300. {
  1301. struct fc_seq *sp;
  1302. void (*resp)(struct fc_seq *, struct fc_frame *, void *);
  1303. void *arg;
  1304. int rc = 1;
  1305. spin_lock_bh(&ep->ex_lock);
  1306. ep->state |= FC_EX_RST_CLEANUP;
  1307. /*
  1308. * we really want to call del_timer_sync, but cannot due
  1309. * to the lport calling with the lport lock held (some resp
  1310. * functions can also grab the lport lock which could cause
  1311. * a deadlock).
  1312. */
  1313. if (cancel_delayed_work(&ep->timeout_work))
  1314. atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
  1315. resp = ep->resp;
  1316. ep->resp = NULL;
  1317. if (ep->esb_stat & ESB_ST_REC_QUAL)
  1318. atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
  1319. ep->esb_stat &= ~ESB_ST_REC_QUAL;
  1320. arg = ep->arg;
  1321. sp = &ep->seq;
  1322. rc = fc_exch_done_locked(ep);
  1323. spin_unlock_bh(&ep->ex_lock);
  1324. if (!rc)
  1325. fc_exch_delete(ep);
  1326. if (resp)
  1327. resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
  1328. }
  1329. /**
  1330. * fc_exch_pool_reset() - Resets an per cpu exches pool.
  1331. * @lport: ptr to the local port
  1332. * @pool: ptr to the per cpu exches pool
  1333. * @sid: source FC ID
  1334. * @did: destination FC ID
  1335. *
  1336. * Resets an per cpu exches pool, releasing its all sequences
  1337. * and exchanges. If sid is non-zero, then reset only exchanges
  1338. * we sourced from that FID. If did is non-zero, reset only
  1339. * exchanges destined to that FID.
  1340. */
  1341. static void fc_exch_pool_reset(struct fc_lport *lport,
  1342. struct fc_exch_pool *pool,
  1343. u32 sid, u32 did)
  1344. {
  1345. struct fc_exch *ep;
  1346. struct fc_exch *next;
  1347. spin_lock_bh(&pool->lock);
  1348. restart:
  1349. list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
  1350. if ((lport == ep->lp) &&
  1351. (sid == 0 || sid == ep->sid) &&
  1352. (did == 0 || did == ep->did)) {
  1353. fc_exch_hold(ep);
  1354. spin_unlock_bh(&pool->lock);
  1355. fc_exch_reset(ep);
  1356. fc_exch_release(ep);
  1357. spin_lock_bh(&pool->lock);
  1358. /*
  1359. * must restart loop incase while lock
  1360. * was down multiple eps were released.
  1361. */
  1362. goto restart;
  1363. }
  1364. }
  1365. spin_unlock_bh(&pool->lock);
  1366. }
  1367. /**
  1368. * fc_exch_mgr_reset() - Resets all EMs of a lport
  1369. * @lport: ptr to the local port
  1370. * @sid: source FC ID
  1371. * @did: destination FC ID
  1372. *
  1373. * Reset all EMs of a lport, releasing its all sequences and
  1374. * exchanges. If sid is non-zero, then reset only exchanges
  1375. * we sourced from that FID. If did is non-zero, reset only
  1376. * exchanges destined to that FID.
  1377. */
  1378. void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
  1379. {
  1380. struct fc_exch_mgr_anchor *ema;
  1381. unsigned int cpu;
  1382. list_for_each_entry(ema, &lport->ema_list, ema_list) {
  1383. for_each_possible_cpu(cpu)
  1384. fc_exch_pool_reset(lport,
  1385. per_cpu_ptr(ema->mp->pool, cpu),
  1386. sid, did);
  1387. }
  1388. }
  1389. EXPORT_SYMBOL(fc_exch_mgr_reset);
  1390. /*
  1391. * Handle incoming ELS REC - Read Exchange Concise.
  1392. * Note that the requesting port may be different than the S_ID in the request.
  1393. */
  1394. static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
  1395. {
  1396. struct fc_frame *fp;
  1397. struct fc_exch *ep;
  1398. struct fc_exch_mgr *em;
  1399. struct fc_els_rec *rp;
  1400. struct fc_els_rec_acc *acc;
  1401. enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
  1402. enum fc_els_rjt_explan explan;
  1403. u32 sid;
  1404. u16 rxid;
  1405. u16 oxid;
  1406. rp = fc_frame_payload_get(rfp, sizeof(*rp));
  1407. explan = ELS_EXPL_INV_LEN;
  1408. if (!rp)
  1409. goto reject;
  1410. sid = ntoh24(rp->rec_s_id);
  1411. rxid = ntohs(rp->rec_rx_id);
  1412. oxid = ntohs(rp->rec_ox_id);
  1413. /*
  1414. * Currently it's hard to find the local S_ID from the exchange
  1415. * manager. This will eventually be fixed, but for now it's easier
  1416. * to lookup the subject exchange twice, once as if we were
  1417. * the initiator, and then again if we weren't.
  1418. */
  1419. em = fc_seq_exch(sp)->em;
  1420. ep = fc_exch_find(em, oxid);
  1421. explan = ELS_EXPL_OXID_RXID;
  1422. if (ep && ep->oid == sid) {
  1423. if (ep->rxid != FC_XID_UNKNOWN &&
  1424. rxid != FC_XID_UNKNOWN &&
  1425. ep->rxid != rxid)
  1426. goto rel;
  1427. } else {
  1428. if (ep)
  1429. fc_exch_release(ep);
  1430. ep = NULL;
  1431. if (rxid != FC_XID_UNKNOWN)
  1432. ep = fc_exch_find(em, rxid);
  1433. if (!ep)
  1434. goto reject;
  1435. }
  1436. fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
  1437. if (!fp) {
  1438. fc_exch_done(sp);
  1439. goto out;
  1440. }
  1441. sp = fc_seq_start_next(sp);
  1442. acc = fc_frame_payload_get(fp, sizeof(*acc));
  1443. memset(acc, 0, sizeof(*acc));
  1444. acc->reca_cmd = ELS_LS_ACC;
  1445. acc->reca_ox_id = rp->rec_ox_id;
  1446. memcpy(acc->reca_ofid, rp->rec_s_id, 3);
  1447. acc->reca_rx_id = htons(ep->rxid);
  1448. if (ep->sid == ep->oid)
  1449. hton24(acc->reca_rfid, ep->did);
  1450. else
  1451. hton24(acc->reca_rfid, ep->sid);
  1452. acc->reca_fc4value = htonl(ep->seq.rec_data);
  1453. acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
  1454. ESB_ST_SEQ_INIT |
  1455. ESB_ST_COMPLETE));
  1456. sp = fc_seq_start_next(sp);
  1457. fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
  1458. out:
  1459. fc_exch_release(ep);
  1460. fc_frame_free(rfp);
  1461. return;
  1462. rel:
  1463. fc_exch_release(ep);
  1464. reject:
  1465. fc_seq_ls_rjt(sp, reason, explan);
  1466. fc_frame_free(rfp);
  1467. }
  1468. /*
  1469. * Handle response from RRQ.
  1470. * Not much to do here, really.
  1471. * Should report errors.
  1472. *
  1473. * TODO: fix error handler.
  1474. */
  1475. static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
  1476. {
  1477. struct fc_exch *aborted_ep = arg;
  1478. unsigned int op;
  1479. if (IS_ERR(fp)) {
  1480. int err = PTR_ERR(fp);
  1481. if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
  1482. goto cleanup;
  1483. FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
  1484. "frame error %d\n", err);
  1485. return;
  1486. }
  1487. op = fc_frame_payload_op(fp);
  1488. fc_frame_free(fp);
  1489. switch (op) {
  1490. case ELS_LS_RJT:
  1491. FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
  1492. /* fall through */
  1493. case ELS_LS_ACC:
  1494. goto cleanup;
  1495. default:
  1496. FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
  1497. "for RRQ", op);
  1498. return;
  1499. }
  1500. cleanup:
  1501. fc_exch_done(&aborted_ep->seq);
  1502. /* drop hold for rec qual */
  1503. fc_exch_release(aborted_ep);
  1504. }
  1505. /*
  1506. * Send ELS RRQ - Reinstate Recovery Qualifier.
  1507. * This tells the remote port to stop blocking the use of
  1508. * the exchange and the seq_cnt range.
  1509. */
  1510. static void fc_exch_rrq(struct fc_exch *ep)
  1511. {
  1512. struct fc_lport *lp;
  1513. struct fc_els_rrq *rrq;
  1514. struct fc_frame *fp;
  1515. u32 did;
  1516. lp = ep->lp;
  1517. fp = fc_frame_alloc(lp, sizeof(*rrq));
  1518. if (!fp)
  1519. goto retry;
  1520. rrq = fc_frame_payload_get(fp, sizeof(*rrq));
  1521. memset(rrq, 0, sizeof(*rrq));
  1522. rrq->rrq_cmd = ELS_RRQ;
  1523. hton24(rrq->rrq_s_id, ep->sid);
  1524. rrq->rrq_ox_id = htons(ep->oxid);
  1525. rrq->rrq_rx_id = htons(ep->rxid);
  1526. did = ep->did;
  1527. if (ep->esb_stat & ESB_ST_RESP)
  1528. did = ep->sid;
  1529. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
  1530. fc_host_port_id(lp->host), FC_TYPE_ELS,
  1531. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1532. if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov))
  1533. return;
  1534. retry:
  1535. spin_lock_bh(&ep->ex_lock);
  1536. if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
  1537. spin_unlock_bh(&ep->ex_lock);
  1538. /* drop hold for rec qual */
  1539. fc_exch_release(ep);
  1540. return;
  1541. }
  1542. ep->esb_stat |= ESB_ST_REC_QUAL;
  1543. fc_exch_timer_set_locked(ep, ep->r_a_tov);
  1544. spin_unlock_bh(&ep->ex_lock);
  1545. }
  1546. /*
  1547. * Handle incoming ELS RRQ - Reset Recovery Qualifier.
  1548. */
  1549. static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
  1550. {
  1551. struct fc_exch *ep; /* request or subject exchange */
  1552. struct fc_els_rrq *rp;
  1553. u32 sid;
  1554. u16 xid;
  1555. enum fc_els_rjt_explan explan;
  1556. rp = fc_frame_payload_get(fp, sizeof(*rp));
  1557. explan = ELS_EXPL_INV_LEN;
  1558. if (!rp)
  1559. goto reject;
  1560. /*
  1561. * lookup subject exchange.
  1562. */
  1563. ep = fc_seq_exch(sp);
  1564. sid = ntoh24(rp->rrq_s_id); /* subject source */
  1565. xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
  1566. ep = fc_exch_find(ep->em, xid);
  1567. explan = ELS_EXPL_OXID_RXID;
  1568. if (!ep)
  1569. goto reject;
  1570. spin_lock_bh(&ep->ex_lock);
  1571. if (ep->oxid != ntohs(rp->rrq_ox_id))
  1572. goto unlock_reject;
  1573. if (ep->rxid != ntohs(rp->rrq_rx_id) &&
  1574. ep->rxid != FC_XID_UNKNOWN)
  1575. goto unlock_reject;
  1576. explan = ELS_EXPL_SID;
  1577. if (ep->sid != sid)
  1578. goto unlock_reject;
  1579. /*
  1580. * Clear Recovery Qualifier state, and cancel timer if complete.
  1581. */
  1582. if (ep->esb_stat & ESB_ST_REC_QUAL) {
  1583. ep->esb_stat &= ~ESB_ST_REC_QUAL;
  1584. atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
  1585. }
  1586. if (ep->esb_stat & ESB_ST_COMPLETE) {
  1587. if (cancel_delayed_work(&ep->timeout_work))
  1588. atomic_dec(&ep->ex_refcnt); /* drop timer hold */
  1589. }
  1590. spin_unlock_bh(&ep->ex_lock);
  1591. /*
  1592. * Send LS_ACC.
  1593. */
  1594. fc_seq_ls_acc(sp);
  1595. fc_frame_free(fp);
  1596. return;
  1597. unlock_reject:
  1598. spin_unlock_bh(&ep->ex_lock);
  1599. fc_exch_release(ep); /* drop hold from fc_exch_find */
  1600. reject:
  1601. fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
  1602. fc_frame_free(fp);
  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. }