fc_exch.c 48 KB

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