fc_exch.c 50 KB

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