fc_exch.c 51 KB

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