fc_exch.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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. if (e_stat & ESB_ST_REC_QUAL)
  376. fc_exch_rrq(ep);
  377. spin_unlock_bh(&ep->ex_lock);
  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. WARN_ON((ep->esb_stat & ESB_ST_COMPLETE) != 0);
  748. sp = fc_seq_start_next_locked(sp);
  749. spin_unlock_bh(&ep->ex_lock);
  750. return sp;
  751. }
  752. EXPORT_SYMBOL(fc_seq_start_next);
  753. int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
  754. {
  755. struct fc_exch *ep;
  756. struct fc_frame_header *fh = fc_frame_header_get(fp);
  757. int error;
  758. u32 f_ctl;
  759. ep = fc_seq_exch(sp);
  760. WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
  761. f_ctl = ntoh24(fh->fh_f_ctl);
  762. fc_exch_setup_hdr(ep, fp, f_ctl);
  763. /*
  764. * update sequence count if this frame is carrying
  765. * multiple FC frames when sequence offload is enabled
  766. * by LLD.
  767. */
  768. if (fr_max_payload(fp))
  769. sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
  770. fr_max_payload(fp));
  771. else
  772. sp->cnt++;
  773. /*
  774. * Send the frame.
  775. */
  776. error = lp->tt.frame_send(lp, fp);
  777. /*
  778. * Update the exchange and sequence flags,
  779. * assuming all frames for the sequence have been sent.
  780. * We can only be called to send once for each sequence.
  781. */
  782. spin_lock_bh(&ep->ex_lock);
  783. ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
  784. if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
  785. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  786. spin_unlock_bh(&ep->ex_lock);
  787. return error;
  788. }
  789. EXPORT_SYMBOL(fc_seq_send);
  790. void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
  791. struct fc_seq_els_data *els_data)
  792. {
  793. switch (els_cmd) {
  794. case ELS_LS_RJT:
  795. fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
  796. break;
  797. case ELS_LS_ACC:
  798. fc_seq_ls_acc(sp);
  799. break;
  800. case ELS_RRQ:
  801. fc_exch_els_rrq(sp, els_data->fp);
  802. break;
  803. case ELS_REC:
  804. fc_exch_els_rec(sp, els_data->fp);
  805. break;
  806. default:
  807. FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
  808. }
  809. }
  810. EXPORT_SYMBOL(fc_seq_els_rsp_send);
  811. /*
  812. * Send a sequence, which is also the last sequence in the exchange.
  813. */
  814. static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
  815. enum fc_rctl rctl, enum fc_fh_type fh_type)
  816. {
  817. u32 f_ctl;
  818. struct fc_exch *ep = fc_seq_exch(sp);
  819. f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  820. f_ctl |= ep->f_ctl;
  821. fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
  822. fc_seq_send(ep->lp, sp, fp);
  823. }
  824. /*
  825. * Send ACK_1 (or equiv.) indicating we received something.
  826. * The frame we're acking is supplied.
  827. */
  828. static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
  829. {
  830. struct fc_frame *fp;
  831. struct fc_frame_header *rx_fh;
  832. struct fc_frame_header *fh;
  833. struct fc_exch *ep = fc_seq_exch(sp);
  834. struct fc_lport *lp = ep->lp;
  835. unsigned int f_ctl;
  836. /*
  837. * Don't send ACKs for class 3.
  838. */
  839. if (fc_sof_needs_ack(fr_sof(rx_fp))) {
  840. fp = fc_frame_alloc(lp, 0);
  841. if (!fp)
  842. return;
  843. fh = fc_frame_header_get(fp);
  844. fh->fh_r_ctl = FC_RCTL_ACK_1;
  845. fh->fh_type = FC_TYPE_BLS;
  846. /*
  847. * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
  848. * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
  849. * Bits 9-8 are meaningful (retransmitted or unidirectional).
  850. * Last ACK uses bits 7-6 (continue sequence),
  851. * bits 5-4 are meaningful (what kind of ACK to use).
  852. */
  853. rx_fh = fc_frame_header_get(rx_fp);
  854. f_ctl = ntoh24(rx_fh->fh_f_ctl);
  855. f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
  856. FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
  857. FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
  858. FC_FC_RETX_SEQ | FC_FC_UNI_TX;
  859. f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
  860. hton24(fh->fh_f_ctl, f_ctl);
  861. fc_exch_setup_hdr(ep, fp, f_ctl);
  862. fh->fh_seq_id = rx_fh->fh_seq_id;
  863. fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
  864. fh->fh_parm_offset = htonl(1); /* ack single frame */
  865. fr_sof(fp) = fr_sof(rx_fp);
  866. if (f_ctl & FC_FC_END_SEQ)
  867. fr_eof(fp) = FC_EOF_T;
  868. else
  869. fr_eof(fp) = FC_EOF_N;
  870. (void) lp->tt.frame_send(lp, fp);
  871. }
  872. }
  873. /*
  874. * Send BLS Reject.
  875. * This is for rejecting BA_ABTS only.
  876. */
  877. static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
  878. enum fc_ba_rjt_reason reason,
  879. enum fc_ba_rjt_explan explan)
  880. {
  881. struct fc_frame *fp;
  882. struct fc_frame_header *rx_fh;
  883. struct fc_frame_header *fh;
  884. struct fc_ba_rjt *rp;
  885. struct fc_lport *lp;
  886. unsigned int f_ctl;
  887. lp = fr_dev(rx_fp);
  888. fp = fc_frame_alloc(lp, sizeof(*rp));
  889. if (!fp)
  890. return;
  891. fh = fc_frame_header_get(fp);
  892. rx_fh = fc_frame_header_get(rx_fp);
  893. memset(fh, 0, sizeof(*fh) + sizeof(*rp));
  894. rp = fc_frame_payload_get(fp, sizeof(*rp));
  895. rp->br_reason = reason;
  896. rp->br_explan = explan;
  897. /*
  898. * seq_id, cs_ctl, df_ctl and param/offset are zero.
  899. */
  900. memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
  901. memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
  902. fh->fh_ox_id = rx_fh->fh_rx_id;
  903. fh->fh_rx_id = rx_fh->fh_ox_id;
  904. fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
  905. fh->fh_r_ctl = FC_RCTL_BA_RJT;
  906. fh->fh_type = FC_TYPE_BLS;
  907. /*
  908. * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
  909. * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
  910. * Bits 9-8 are meaningful (retransmitted or unidirectional).
  911. * Last ACK uses bits 7-6 (continue sequence),
  912. * bits 5-4 are meaningful (what kind of ACK to use).
  913. * Always set LAST_SEQ, END_SEQ.
  914. */
  915. f_ctl = ntoh24(rx_fh->fh_f_ctl);
  916. f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
  917. FC_FC_END_CONN | FC_FC_SEQ_INIT |
  918. FC_FC_RETX_SEQ | FC_FC_UNI_TX;
  919. f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
  920. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  921. f_ctl &= ~FC_FC_FIRST_SEQ;
  922. hton24(fh->fh_f_ctl, f_ctl);
  923. fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
  924. fr_eof(fp) = FC_EOF_T;
  925. if (fc_sof_needs_ack(fr_sof(fp)))
  926. fr_eof(fp) = FC_EOF_N;
  927. (void) lp->tt.frame_send(lp, fp);
  928. }
  929. /*
  930. * Handle an incoming ABTS. This would be for target mode usually,
  931. * but could be due to lost FCP transfer ready, confirm or RRQ.
  932. * We always handle this as an exchange abort, ignoring the parameter.
  933. */
  934. static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
  935. {
  936. struct fc_frame *fp;
  937. struct fc_ba_acc *ap;
  938. struct fc_frame_header *fh;
  939. struct fc_seq *sp;
  940. if (!ep)
  941. goto reject;
  942. spin_lock_bh(&ep->ex_lock);
  943. if (ep->esb_stat & ESB_ST_COMPLETE) {
  944. spin_unlock_bh(&ep->ex_lock);
  945. goto reject;
  946. }
  947. if (!(ep->esb_stat & ESB_ST_REC_QUAL))
  948. fc_exch_hold(ep); /* hold for REC_QUAL */
  949. ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
  950. fc_exch_timer_set_locked(ep, ep->r_a_tov);
  951. fp = fc_frame_alloc(ep->lp, sizeof(*ap));
  952. if (!fp) {
  953. spin_unlock_bh(&ep->ex_lock);
  954. goto free;
  955. }
  956. fh = fc_frame_header_get(fp);
  957. ap = fc_frame_payload_get(fp, sizeof(*ap));
  958. memset(ap, 0, sizeof(*ap));
  959. sp = &ep->seq;
  960. ap->ba_high_seq_cnt = htons(0xffff);
  961. if (sp->ssb_stat & SSB_ST_RESP) {
  962. ap->ba_seq_id = sp->id;
  963. ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
  964. ap->ba_high_seq_cnt = fh->fh_seq_cnt;
  965. ap->ba_low_seq_cnt = htons(sp->cnt);
  966. }
  967. sp = fc_seq_start_next_locked(sp);
  968. spin_unlock_bh(&ep->ex_lock);
  969. fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
  970. fc_frame_free(rx_fp);
  971. return;
  972. reject:
  973. fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
  974. free:
  975. fc_frame_free(rx_fp);
  976. }
  977. /*
  978. * Handle receive where the other end is originating the sequence.
  979. */
  980. static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
  981. struct fc_frame *fp)
  982. {
  983. struct fc_frame_header *fh = fc_frame_header_get(fp);
  984. struct fc_seq *sp = NULL;
  985. struct fc_exch *ep = NULL;
  986. enum fc_sof sof;
  987. enum fc_eof eof;
  988. u32 f_ctl;
  989. enum fc_pf_rjt_reason reject;
  990. fr_seq(fp) = NULL;
  991. reject = fc_seq_lookup_recip(mp, fp);
  992. if (reject == FC_RJT_NONE) {
  993. sp = fr_seq(fp); /* sequence will be held */
  994. ep = fc_seq_exch(sp);
  995. sof = fr_sof(fp);
  996. eof = fr_eof(fp);
  997. f_ctl = ntoh24(fh->fh_f_ctl);
  998. fc_seq_send_ack(sp, fp);
  999. /*
  1000. * Call the receive function.
  1001. *
  1002. * The receive function may allocate a new sequence
  1003. * over the old one, so we shouldn't change the
  1004. * sequence after this.
  1005. *
  1006. * The frame will be freed by the receive function.
  1007. * If new exch resp handler is valid then call that
  1008. * first.
  1009. */
  1010. if (ep->resp)
  1011. ep->resp(sp, fp, ep->arg);
  1012. else
  1013. lp->tt.lport_recv(lp, sp, fp);
  1014. fc_exch_release(ep); /* release from lookup */
  1015. } else {
  1016. FC_EM_DBG(mp, "exch/seq lookup failed: reject %x\n", reject);
  1017. fc_frame_free(fp);
  1018. }
  1019. }
  1020. /*
  1021. * Handle receive where the other end is originating the sequence in
  1022. * response to our exchange.
  1023. */
  1024. static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1025. {
  1026. struct fc_frame_header *fh = fc_frame_header_get(fp);
  1027. struct fc_seq *sp;
  1028. struct fc_exch *ep;
  1029. enum fc_sof sof;
  1030. u32 f_ctl;
  1031. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  1032. void *ex_resp_arg;
  1033. int rc;
  1034. ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
  1035. if (!ep) {
  1036. atomic_inc(&mp->stats.xid_not_found);
  1037. goto out;
  1038. }
  1039. if (ep->esb_stat & ESB_ST_COMPLETE) {
  1040. atomic_inc(&mp->stats.xid_not_found);
  1041. goto out;
  1042. }
  1043. if (ep->rxid == FC_XID_UNKNOWN)
  1044. ep->rxid = ntohs(fh->fh_rx_id);
  1045. if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
  1046. atomic_inc(&mp->stats.xid_not_found);
  1047. goto rel;
  1048. }
  1049. if (ep->did != ntoh24(fh->fh_s_id) &&
  1050. ep->did != FC_FID_FLOGI) {
  1051. atomic_inc(&mp->stats.xid_not_found);
  1052. goto rel;
  1053. }
  1054. sof = fr_sof(fp);
  1055. if (fc_sof_is_init(sof)) {
  1056. sp = fc_seq_start_next(&ep->seq);
  1057. sp->id = fh->fh_seq_id;
  1058. sp->ssb_stat |= SSB_ST_RESP;
  1059. } else {
  1060. sp = &ep->seq;
  1061. if (sp->id != fh->fh_seq_id) {
  1062. atomic_inc(&mp->stats.seq_not_found);
  1063. goto rel;
  1064. }
  1065. }
  1066. f_ctl = ntoh24(fh->fh_f_ctl);
  1067. fr_seq(fp) = sp;
  1068. if (f_ctl & FC_FC_SEQ_INIT)
  1069. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1070. if (fc_sof_needs_ack(sof))
  1071. fc_seq_send_ack(sp, fp);
  1072. resp = ep->resp;
  1073. ex_resp_arg = ep->arg;
  1074. if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
  1075. (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
  1076. (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
  1077. spin_lock_bh(&ep->ex_lock);
  1078. rc = fc_exch_done_locked(ep);
  1079. WARN_ON(fc_seq_exch(sp) != ep);
  1080. spin_unlock_bh(&ep->ex_lock);
  1081. if (!rc)
  1082. fc_exch_mgr_delete_ep(ep);
  1083. }
  1084. /*
  1085. * Call the receive function.
  1086. * The sequence is held (has a refcnt) for us,
  1087. * but not for the receive function.
  1088. *
  1089. * The receive function may allocate a new sequence
  1090. * over the old one, so we shouldn't change the
  1091. * sequence after this.
  1092. *
  1093. * The frame will be freed by the receive function.
  1094. * If new exch resp handler is valid then call that
  1095. * first.
  1096. */
  1097. if (resp)
  1098. resp(sp, fp, ex_resp_arg);
  1099. else
  1100. fc_frame_free(fp);
  1101. fc_exch_release(ep);
  1102. return;
  1103. rel:
  1104. fc_exch_release(ep);
  1105. out:
  1106. fc_frame_free(fp);
  1107. }
  1108. /*
  1109. * Handle receive for a sequence where other end is responding to our sequence.
  1110. */
  1111. static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1112. {
  1113. struct fc_seq *sp;
  1114. sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
  1115. if (!sp) {
  1116. atomic_inc(&mp->stats.xid_not_found);
  1117. FC_EM_DBG(mp, "seq lookup failed\n");
  1118. } else {
  1119. atomic_inc(&mp->stats.non_bls_resp);
  1120. FC_EM_DBG(mp, "non-BLS response to sequence");
  1121. }
  1122. fc_frame_free(fp);
  1123. }
  1124. /*
  1125. * Handle the response to an ABTS for exchange or sequence.
  1126. * This can be BA_ACC or BA_RJT.
  1127. */
  1128. static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
  1129. {
  1130. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  1131. void *ex_resp_arg;
  1132. struct fc_frame_header *fh;
  1133. struct fc_ba_acc *ap;
  1134. struct fc_seq *sp;
  1135. u16 low;
  1136. u16 high;
  1137. int rc = 1, has_rec = 0;
  1138. fh = fc_frame_header_get(fp);
  1139. FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
  1140. fc_exch_rctl_name(fh->fh_r_ctl));
  1141. if (cancel_delayed_work_sync(&ep->timeout_work))
  1142. fc_exch_release(ep); /* release from pending timer hold */
  1143. spin_lock_bh(&ep->ex_lock);
  1144. switch (fh->fh_r_ctl) {
  1145. case FC_RCTL_BA_ACC:
  1146. ap = fc_frame_payload_get(fp, sizeof(*ap));
  1147. if (!ap)
  1148. break;
  1149. /*
  1150. * Decide whether to establish a Recovery Qualifier.
  1151. * We do this if there is a non-empty SEQ_CNT range and
  1152. * SEQ_ID is the same as the one we aborted.
  1153. */
  1154. low = ntohs(ap->ba_low_seq_cnt);
  1155. high = ntohs(ap->ba_high_seq_cnt);
  1156. if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
  1157. (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
  1158. ap->ba_seq_id == ep->seq_id) && low != high) {
  1159. ep->esb_stat |= ESB_ST_REC_QUAL;
  1160. fc_exch_hold(ep); /* hold for recovery qualifier */
  1161. has_rec = 1;
  1162. }
  1163. break;
  1164. case FC_RCTL_BA_RJT:
  1165. break;
  1166. default:
  1167. break;
  1168. }
  1169. resp = ep->resp;
  1170. ex_resp_arg = ep->arg;
  1171. /* do we need to do some other checks here. Can we reuse more of
  1172. * fc_exch_recv_seq_resp
  1173. */
  1174. sp = &ep->seq;
  1175. /*
  1176. * do we want to check END_SEQ as well as LAST_SEQ here?
  1177. */
  1178. if (ep->fh_type != FC_TYPE_FCP &&
  1179. ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
  1180. rc = fc_exch_done_locked(ep);
  1181. spin_unlock_bh(&ep->ex_lock);
  1182. if (!rc)
  1183. fc_exch_mgr_delete_ep(ep);
  1184. if (resp)
  1185. resp(sp, fp, ex_resp_arg);
  1186. else
  1187. fc_frame_free(fp);
  1188. if (has_rec)
  1189. fc_exch_timer_set(ep, ep->r_a_tov);
  1190. }
  1191. /*
  1192. * Receive BLS sequence.
  1193. * This is always a sequence initiated by the remote side.
  1194. * We may be either the originator or recipient of the exchange.
  1195. */
  1196. static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1197. {
  1198. struct fc_frame_header *fh;
  1199. struct fc_exch *ep;
  1200. u32 f_ctl;
  1201. fh = fc_frame_header_get(fp);
  1202. f_ctl = ntoh24(fh->fh_f_ctl);
  1203. fr_seq(fp) = NULL;
  1204. ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
  1205. ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
  1206. if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
  1207. spin_lock_bh(&ep->ex_lock);
  1208. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1209. spin_unlock_bh(&ep->ex_lock);
  1210. }
  1211. if (f_ctl & FC_FC_SEQ_CTX) {
  1212. /*
  1213. * A response to a sequence we initiated.
  1214. * This should only be ACKs for class 2 or F.
  1215. */
  1216. switch (fh->fh_r_ctl) {
  1217. case FC_RCTL_ACK_1:
  1218. case FC_RCTL_ACK_0:
  1219. break;
  1220. default:
  1221. FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
  1222. fh->fh_r_ctl,
  1223. fc_exch_rctl_name(fh->fh_r_ctl));
  1224. break;
  1225. }
  1226. fc_frame_free(fp);
  1227. } else {
  1228. switch (fh->fh_r_ctl) {
  1229. case FC_RCTL_BA_RJT:
  1230. case FC_RCTL_BA_ACC:
  1231. if (ep)
  1232. fc_exch_abts_resp(ep, fp);
  1233. else
  1234. fc_frame_free(fp);
  1235. break;
  1236. case FC_RCTL_BA_ABTS:
  1237. fc_exch_recv_abts(ep, fp);
  1238. break;
  1239. default: /* ignore junk */
  1240. fc_frame_free(fp);
  1241. break;
  1242. }
  1243. }
  1244. if (ep)
  1245. fc_exch_release(ep); /* release hold taken by fc_exch_find */
  1246. }
  1247. /*
  1248. * Accept sequence with LS_ACC.
  1249. * If this fails due to allocation or transmit congestion, assume the
  1250. * originator will repeat the sequence.
  1251. */
  1252. static void fc_seq_ls_acc(struct fc_seq *req_sp)
  1253. {
  1254. struct fc_seq *sp;
  1255. struct fc_els_ls_acc *acc;
  1256. struct fc_frame *fp;
  1257. sp = fc_seq_start_next(req_sp);
  1258. fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
  1259. if (fp) {
  1260. acc = fc_frame_payload_get(fp, sizeof(*acc));
  1261. memset(acc, 0, sizeof(*acc));
  1262. acc->la_cmd = ELS_LS_ACC;
  1263. fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
  1264. }
  1265. }
  1266. /*
  1267. * Reject sequence with ELS LS_RJT.
  1268. * If this fails due to allocation or transmit congestion, assume the
  1269. * originator will repeat the sequence.
  1270. */
  1271. static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
  1272. enum fc_els_rjt_explan explan)
  1273. {
  1274. struct fc_seq *sp;
  1275. struct fc_els_ls_rjt *rjt;
  1276. struct fc_frame *fp;
  1277. sp = fc_seq_start_next(req_sp);
  1278. fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
  1279. if (fp) {
  1280. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1281. memset(rjt, 0, sizeof(*rjt));
  1282. rjt->er_cmd = ELS_LS_RJT;
  1283. rjt->er_reason = reason;
  1284. rjt->er_explan = explan;
  1285. fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
  1286. }
  1287. }
  1288. static void fc_exch_reset(struct fc_exch *ep)
  1289. {
  1290. struct fc_seq *sp;
  1291. void (*resp)(struct fc_seq *, struct fc_frame *, void *);
  1292. void *arg;
  1293. int rc = 1;
  1294. spin_lock_bh(&ep->ex_lock);
  1295. ep->state |= FC_EX_RST_CLEANUP;
  1296. /*
  1297. * we really want to call del_timer_sync, but cannot due
  1298. * to the lport calling with the lport lock held (some resp
  1299. * functions can also grab the lport lock which could cause
  1300. * a deadlock).
  1301. */
  1302. if (cancel_delayed_work(&ep->timeout_work))
  1303. atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
  1304. resp = ep->resp;
  1305. ep->resp = NULL;
  1306. if (ep->esb_stat & ESB_ST_REC_QUAL)
  1307. atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
  1308. ep->esb_stat &= ~ESB_ST_REC_QUAL;
  1309. arg = ep->arg;
  1310. sp = &ep->seq;
  1311. rc = fc_exch_done_locked(ep);
  1312. spin_unlock_bh(&ep->ex_lock);
  1313. if (!rc)
  1314. fc_exch_mgr_delete_ep(ep);
  1315. if (resp)
  1316. resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
  1317. }
  1318. /*
  1319. * Reset an exchange manager, releasing all sequences and exchanges.
  1320. * If sid is non-zero, reset only exchanges we source from that FID.
  1321. * If did is non-zero, reset only exchanges destined to that FID.
  1322. */
  1323. void fc_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
  1324. {
  1325. struct fc_exch *ep;
  1326. struct fc_exch *next;
  1327. struct fc_exch_mgr *mp = lp->emp;
  1328. spin_lock_bh(&mp->em_lock);
  1329. restart:
  1330. list_for_each_entry_safe(ep, next, &mp->ex_list, ex_list) {
  1331. if ((sid == 0 || sid == ep->sid) &&
  1332. (did == 0 || did == ep->did)) {
  1333. fc_exch_hold(ep);
  1334. spin_unlock_bh(&mp->em_lock);
  1335. fc_exch_reset(ep);
  1336. fc_exch_release(ep);
  1337. spin_lock_bh(&mp->em_lock);
  1338. /*
  1339. * must restart loop incase while lock was down
  1340. * multiple eps were released.
  1341. */
  1342. goto restart;
  1343. }
  1344. }
  1345. spin_unlock_bh(&mp->em_lock);
  1346. }
  1347. EXPORT_SYMBOL(fc_exch_mgr_reset);
  1348. /*
  1349. * Handle incoming ELS REC - Read Exchange Concise.
  1350. * Note that the requesting port may be different than the S_ID in the request.
  1351. */
  1352. static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
  1353. {
  1354. struct fc_frame *fp;
  1355. struct fc_exch *ep;
  1356. struct fc_exch_mgr *em;
  1357. struct fc_els_rec *rp;
  1358. struct fc_els_rec_acc *acc;
  1359. enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
  1360. enum fc_els_rjt_explan explan;
  1361. u32 sid;
  1362. u16 rxid;
  1363. u16 oxid;
  1364. rp = fc_frame_payload_get(rfp, sizeof(*rp));
  1365. explan = ELS_EXPL_INV_LEN;
  1366. if (!rp)
  1367. goto reject;
  1368. sid = ntoh24(rp->rec_s_id);
  1369. rxid = ntohs(rp->rec_rx_id);
  1370. oxid = ntohs(rp->rec_ox_id);
  1371. /*
  1372. * Currently it's hard to find the local S_ID from the exchange
  1373. * manager. This will eventually be fixed, but for now it's easier
  1374. * to lookup the subject exchange twice, once as if we were
  1375. * the initiator, and then again if we weren't.
  1376. */
  1377. em = fc_seq_exch(sp)->em;
  1378. ep = fc_exch_find(em, oxid);
  1379. explan = ELS_EXPL_OXID_RXID;
  1380. if (ep && ep->oid == sid) {
  1381. if (ep->rxid != FC_XID_UNKNOWN &&
  1382. rxid != FC_XID_UNKNOWN &&
  1383. ep->rxid != rxid)
  1384. goto rel;
  1385. } else {
  1386. if (ep)
  1387. fc_exch_release(ep);
  1388. ep = NULL;
  1389. if (rxid != FC_XID_UNKNOWN)
  1390. ep = fc_exch_find(em, rxid);
  1391. if (!ep)
  1392. goto reject;
  1393. }
  1394. fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
  1395. if (!fp) {
  1396. fc_exch_done(sp);
  1397. goto out;
  1398. }
  1399. sp = fc_seq_start_next(sp);
  1400. acc = fc_frame_payload_get(fp, sizeof(*acc));
  1401. memset(acc, 0, sizeof(*acc));
  1402. acc->reca_cmd = ELS_LS_ACC;
  1403. acc->reca_ox_id = rp->rec_ox_id;
  1404. memcpy(acc->reca_ofid, rp->rec_s_id, 3);
  1405. acc->reca_rx_id = htons(ep->rxid);
  1406. if (ep->sid == ep->oid)
  1407. hton24(acc->reca_rfid, ep->did);
  1408. else
  1409. hton24(acc->reca_rfid, ep->sid);
  1410. acc->reca_fc4value = htonl(ep->seq.rec_data);
  1411. acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
  1412. ESB_ST_SEQ_INIT |
  1413. ESB_ST_COMPLETE));
  1414. sp = fc_seq_start_next(sp);
  1415. fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
  1416. out:
  1417. fc_exch_release(ep);
  1418. fc_frame_free(rfp);
  1419. return;
  1420. rel:
  1421. fc_exch_release(ep);
  1422. reject:
  1423. fc_seq_ls_rjt(sp, reason, explan);
  1424. fc_frame_free(rfp);
  1425. }
  1426. /*
  1427. * Handle response from RRQ.
  1428. * Not much to do here, really.
  1429. * Should report errors.
  1430. *
  1431. * TODO: fix error handler.
  1432. */
  1433. static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
  1434. {
  1435. struct fc_exch *aborted_ep = arg;
  1436. unsigned int op;
  1437. if (IS_ERR(fp)) {
  1438. int err = PTR_ERR(fp);
  1439. if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
  1440. goto cleanup;
  1441. FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
  1442. "frame error %d\n", err);
  1443. return;
  1444. }
  1445. op = fc_frame_payload_op(fp);
  1446. fc_frame_free(fp);
  1447. switch (op) {
  1448. case ELS_LS_RJT:
  1449. FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
  1450. /* fall through */
  1451. case ELS_LS_ACC:
  1452. goto cleanup;
  1453. default:
  1454. FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
  1455. "for RRQ", op);
  1456. return;
  1457. }
  1458. cleanup:
  1459. fc_exch_done(&aborted_ep->seq);
  1460. /* drop hold for rec qual */
  1461. fc_exch_release(aborted_ep);
  1462. }
  1463. /*
  1464. * Send ELS RRQ - Reinstate Recovery Qualifier.
  1465. * This tells the remote port to stop blocking the use of
  1466. * the exchange and the seq_cnt range.
  1467. */
  1468. static void fc_exch_rrq(struct fc_exch *ep)
  1469. {
  1470. struct fc_lport *lp;
  1471. struct fc_els_rrq *rrq;
  1472. struct fc_frame *fp;
  1473. struct fc_seq *rrq_sp;
  1474. u32 did;
  1475. lp = ep->lp;
  1476. fp = fc_frame_alloc(lp, sizeof(*rrq));
  1477. if (!fp)
  1478. return;
  1479. rrq = fc_frame_payload_get(fp, sizeof(*rrq));
  1480. memset(rrq, 0, sizeof(*rrq));
  1481. rrq->rrq_cmd = ELS_RRQ;
  1482. hton24(rrq->rrq_s_id, ep->sid);
  1483. rrq->rrq_ox_id = htons(ep->oxid);
  1484. rrq->rrq_rx_id = htons(ep->rxid);
  1485. did = ep->did;
  1486. if (ep->esb_stat & ESB_ST_RESP)
  1487. did = ep->sid;
  1488. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
  1489. fc_host_port_id(lp->host), FC_TYPE_ELS,
  1490. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1491. rrq_sp = fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep,
  1492. lp->e_d_tov);
  1493. if (!rrq_sp) {
  1494. ep->esb_stat |= ESB_ST_REC_QUAL;
  1495. fc_exch_timer_set_locked(ep, ep->r_a_tov);
  1496. return;
  1497. }
  1498. }
  1499. /*
  1500. * Handle incoming ELS RRQ - Reset Recovery Qualifier.
  1501. */
  1502. static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
  1503. {
  1504. struct fc_exch *ep; /* request or subject exchange */
  1505. struct fc_els_rrq *rp;
  1506. u32 sid;
  1507. u16 xid;
  1508. enum fc_els_rjt_explan explan;
  1509. rp = fc_frame_payload_get(fp, sizeof(*rp));
  1510. explan = ELS_EXPL_INV_LEN;
  1511. if (!rp)
  1512. goto reject;
  1513. /*
  1514. * lookup subject exchange.
  1515. */
  1516. ep = fc_seq_exch(sp);
  1517. sid = ntoh24(rp->rrq_s_id); /* subject source */
  1518. xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
  1519. ep = fc_exch_find(ep->em, xid);
  1520. explan = ELS_EXPL_OXID_RXID;
  1521. if (!ep)
  1522. goto reject;
  1523. spin_lock_bh(&ep->ex_lock);
  1524. if (ep->oxid != ntohs(rp->rrq_ox_id))
  1525. goto unlock_reject;
  1526. if (ep->rxid != ntohs(rp->rrq_rx_id) &&
  1527. ep->rxid != FC_XID_UNKNOWN)
  1528. goto unlock_reject;
  1529. explan = ELS_EXPL_SID;
  1530. if (ep->sid != sid)
  1531. goto unlock_reject;
  1532. /*
  1533. * Clear Recovery Qualifier state, and cancel timer if complete.
  1534. */
  1535. if (ep->esb_stat & ESB_ST_REC_QUAL) {
  1536. ep->esb_stat &= ~ESB_ST_REC_QUAL;
  1537. atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
  1538. }
  1539. if (ep->esb_stat & ESB_ST_COMPLETE) {
  1540. if (cancel_delayed_work(&ep->timeout_work))
  1541. atomic_dec(&ep->ex_refcnt); /* drop timer hold */
  1542. }
  1543. spin_unlock_bh(&ep->ex_lock);
  1544. /*
  1545. * Send LS_ACC.
  1546. */
  1547. fc_seq_ls_acc(sp);
  1548. fc_frame_free(fp);
  1549. return;
  1550. unlock_reject:
  1551. spin_unlock_bh(&ep->ex_lock);
  1552. fc_exch_release(ep); /* drop hold from fc_exch_find */
  1553. reject:
  1554. fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
  1555. fc_frame_free(fp);
  1556. }
  1557. struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
  1558. enum fc_class class,
  1559. u16 min_xid, u16 max_xid)
  1560. {
  1561. struct fc_exch_mgr *mp;
  1562. size_t len;
  1563. if (max_xid <= min_xid || min_xid == 0 || max_xid == FC_XID_UNKNOWN) {
  1564. FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
  1565. min_xid, max_xid);
  1566. return NULL;
  1567. }
  1568. /*
  1569. * Memory need for EM
  1570. */
  1571. #define xid_ok(i, m1, m2) (((i) >= (m1)) && ((i) <= (m2)))
  1572. len = (max_xid - min_xid + 1) * (sizeof(struct fc_exch *));
  1573. len += sizeof(struct fc_exch_mgr);
  1574. mp = kzalloc(len, GFP_ATOMIC);
  1575. if (!mp)
  1576. return NULL;
  1577. mp->class = class;
  1578. mp->total_exches = 0;
  1579. mp->exches = (struct fc_exch **)(mp + 1);
  1580. mp->lp = lp;
  1581. /* adjust em exch xid range for offload */
  1582. mp->min_xid = min_xid;
  1583. mp->max_xid = max_xid;
  1584. mp->last_xid = min_xid - 1;
  1585. mp->max_read = 0;
  1586. mp->last_read = 0;
  1587. if (lp->lro_enabled && xid_ok(lp->lro_xid, min_xid, max_xid)) {
  1588. mp->max_read = lp->lro_xid;
  1589. mp->last_read = min_xid - 1;
  1590. mp->last_xid = mp->max_read;
  1591. } else {
  1592. /* disable lro if no xid control over read */
  1593. lp->lro_enabled = 0;
  1594. }
  1595. INIT_LIST_HEAD(&mp->ex_list);
  1596. spin_lock_init(&mp->em_lock);
  1597. mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
  1598. if (!mp->ep_pool)
  1599. goto free_mp;
  1600. return mp;
  1601. free_mp:
  1602. kfree(mp);
  1603. return NULL;
  1604. }
  1605. EXPORT_SYMBOL(fc_exch_mgr_alloc);
  1606. void fc_exch_mgr_free(struct fc_exch_mgr *mp)
  1607. {
  1608. WARN_ON(!mp);
  1609. /*
  1610. * The total exch count must be zero
  1611. * before freeing exchange manager.
  1612. */
  1613. WARN_ON(mp->total_exches != 0);
  1614. mempool_destroy(mp->ep_pool);
  1615. kfree(mp);
  1616. }
  1617. EXPORT_SYMBOL(fc_exch_mgr_free);
  1618. struct fc_exch *fc_exch_get(struct fc_lport *lp, struct fc_frame *fp)
  1619. {
  1620. if (!lp || !lp->emp)
  1621. return NULL;
  1622. return fc_exch_alloc(lp->emp, fp, 0);
  1623. }
  1624. EXPORT_SYMBOL(fc_exch_get);
  1625. struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
  1626. struct fc_frame *fp,
  1627. void (*resp)(struct fc_seq *,
  1628. struct fc_frame *fp,
  1629. void *arg),
  1630. void (*destructor)(struct fc_seq *, void *),
  1631. void *arg, u32 timer_msec)
  1632. {
  1633. struct fc_exch *ep;
  1634. struct fc_seq *sp = NULL;
  1635. struct fc_frame_header *fh;
  1636. int rc = 1;
  1637. ep = lp->tt.exch_get(lp, fp);
  1638. if (!ep) {
  1639. fc_frame_free(fp);
  1640. return NULL;
  1641. }
  1642. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1643. fh = fc_frame_header_get(fp);
  1644. fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
  1645. ep->resp = resp;
  1646. ep->destructor = destructor;
  1647. ep->arg = arg;
  1648. ep->r_a_tov = FC_DEF_R_A_TOV;
  1649. ep->lp = lp;
  1650. sp = &ep->seq;
  1651. ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
  1652. ep->f_ctl = ntoh24(fh->fh_f_ctl);
  1653. fc_exch_setup_hdr(ep, fp, ep->f_ctl);
  1654. sp->cnt++;
  1655. fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
  1656. if (unlikely(lp->tt.frame_send(lp, fp)))
  1657. goto err;
  1658. if (timer_msec)
  1659. fc_exch_timer_set_locked(ep, timer_msec);
  1660. ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
  1661. if (ep->f_ctl & FC_FC_SEQ_INIT)
  1662. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  1663. spin_unlock_bh(&ep->ex_lock);
  1664. return sp;
  1665. err:
  1666. rc = fc_exch_done_locked(ep);
  1667. spin_unlock_bh(&ep->ex_lock);
  1668. if (!rc)
  1669. fc_exch_mgr_delete_ep(ep);
  1670. return NULL;
  1671. }
  1672. EXPORT_SYMBOL(fc_exch_seq_send);
  1673. /*
  1674. * Receive a frame
  1675. */
  1676. void fc_exch_recv(struct fc_lport *lp, struct fc_exch_mgr *mp,
  1677. struct fc_frame *fp)
  1678. {
  1679. struct fc_frame_header *fh = fc_frame_header_get(fp);
  1680. u32 f_ctl;
  1681. /* lport lock ? */
  1682. if (!lp || !mp || (lp->state == LPORT_ST_NONE)) {
  1683. FC_LPORT_DBG(lp, "Receiving frames for an lport that "
  1684. "has not been initialized correctly\n");
  1685. fc_frame_free(fp);
  1686. return;
  1687. }
  1688. /*
  1689. * If frame is marked invalid, just drop it.
  1690. */
  1691. f_ctl = ntoh24(fh->fh_f_ctl);
  1692. switch (fr_eof(fp)) {
  1693. case FC_EOF_T:
  1694. if (f_ctl & FC_FC_END_SEQ)
  1695. skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
  1696. /* fall through */
  1697. case FC_EOF_N:
  1698. if (fh->fh_type == FC_TYPE_BLS)
  1699. fc_exch_recv_bls(mp, fp);
  1700. else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
  1701. FC_FC_EX_CTX)
  1702. fc_exch_recv_seq_resp(mp, fp);
  1703. else if (f_ctl & FC_FC_SEQ_CTX)
  1704. fc_exch_recv_resp(mp, fp);
  1705. else
  1706. fc_exch_recv_req(lp, mp, fp);
  1707. break;
  1708. default:
  1709. FC_EM_DBG(mp, "dropping invalid frame (eof %x)", fr_eof(fp));
  1710. fc_frame_free(fp);
  1711. break;
  1712. }
  1713. }
  1714. EXPORT_SYMBOL(fc_exch_recv);
  1715. int fc_exch_init(struct fc_lport *lp)
  1716. {
  1717. if (!lp->tt.exch_get) {
  1718. /*
  1719. * exch_put() should be NULL if
  1720. * exch_get() is NULL
  1721. */
  1722. WARN_ON(lp->tt.exch_put);
  1723. lp->tt.exch_get = fc_exch_get;
  1724. }
  1725. if (!lp->tt.seq_start_next)
  1726. lp->tt.seq_start_next = fc_seq_start_next;
  1727. if (!lp->tt.exch_seq_send)
  1728. lp->tt.exch_seq_send = fc_exch_seq_send;
  1729. if (!lp->tt.seq_send)
  1730. lp->tt.seq_send = fc_seq_send;
  1731. if (!lp->tt.seq_els_rsp_send)
  1732. lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
  1733. if (!lp->tt.exch_done)
  1734. lp->tt.exch_done = fc_exch_done;
  1735. if (!lp->tt.exch_mgr_reset)
  1736. lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
  1737. if (!lp->tt.seq_exch_abort)
  1738. lp->tt.seq_exch_abort = fc_seq_exch_abort;
  1739. return 0;
  1740. }
  1741. EXPORT_SYMBOL(fc_exch_init);
  1742. int fc_setup_exch_mgr(void)
  1743. {
  1744. fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
  1745. 0, SLAB_HWCACHE_ALIGN, NULL);
  1746. if (!fc_em_cachep)
  1747. return -ENOMEM;
  1748. return 0;
  1749. }
  1750. void fc_destroy_exch_mgr(void)
  1751. {
  1752. kmem_cache_destroy(fc_em_cachep);
  1753. }