fc_exch.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  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/slab.h>
  26. #include <linux/err.h>
  27. #include <linux/export.h>
  28. #include <linux/log2.h>
  29. #include <scsi/fc/fc_fc2.h>
  30. #include <scsi/libfc.h>
  31. #include <scsi/fc_encode.h>
  32. #include "fc_libfc.h"
  33. u16 fc_cpu_mask; /* cpu mask for possible cpus */
  34. EXPORT_SYMBOL(fc_cpu_mask);
  35. static u16 fc_cpu_order; /* 2's power to represent total possible cpus */
  36. static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
  37. static struct workqueue_struct *fc_exch_workqueue;
  38. /*
  39. * Structure and function definitions for managing Fibre Channel Exchanges
  40. * and Sequences.
  41. *
  42. * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
  43. *
  44. * fc_exch_mgr holds the exchange state for an N port
  45. *
  46. * fc_exch holds state for one exchange and links to its active sequence.
  47. *
  48. * fc_seq holds the state for an individual sequence.
  49. */
  50. /**
  51. * struct fc_exch_pool - Per cpu exchange pool
  52. * @next_index: Next possible free exchange index
  53. * @total_exches: Total allocated exchanges
  54. * @lock: Exch pool lock
  55. * @ex_list: List of exchanges
  56. *
  57. * This structure manages per cpu exchanges in array of exchange pointers.
  58. * This array is allocated followed by struct fc_exch_pool memory for
  59. * assigned range of exchanges to per cpu pool.
  60. */
  61. struct fc_exch_pool {
  62. spinlock_t lock;
  63. struct list_head ex_list;
  64. u16 next_index;
  65. u16 total_exches;
  66. /* two cache of free slot in exch array */
  67. u16 left;
  68. u16 right;
  69. } ____cacheline_aligned_in_smp;
  70. /**
  71. * struct fc_exch_mgr - The Exchange Manager (EM).
  72. * @class: Default class for new sequences
  73. * @kref: Reference counter
  74. * @min_xid: Minimum exchange ID
  75. * @max_xid: Maximum exchange ID
  76. * @ep_pool: Reserved exchange pointers
  77. * @pool_max_index: Max exch array index in exch pool
  78. * @pool: Per cpu exch pool
  79. * @stats: Statistics structure
  80. *
  81. * This structure is the center for creating exchanges and sequences.
  82. * It manages the allocation of exchange IDs.
  83. */
  84. struct fc_exch_mgr {
  85. struct fc_exch_pool __percpu *pool;
  86. mempool_t *ep_pool;
  87. enum fc_class class;
  88. struct kref kref;
  89. u16 min_xid;
  90. u16 max_xid;
  91. u16 pool_max_index;
  92. struct {
  93. atomic_t no_free_exch;
  94. atomic_t no_free_exch_xid;
  95. atomic_t xid_not_found;
  96. atomic_t xid_busy;
  97. atomic_t seq_not_found;
  98. atomic_t non_bls_resp;
  99. } stats;
  100. };
  101. /**
  102. * struct fc_exch_mgr_anchor - primary structure for list of EMs
  103. * @ema_list: Exchange Manager Anchor list
  104. * @mp: Exchange Manager associated with this anchor
  105. * @match: Routine to determine if this anchor's EM should be used
  106. *
  107. * When walking the list of anchors the match routine will be called
  108. * for each anchor to determine if that EM should be used. The last
  109. * anchor in the list will always match to handle any exchanges not
  110. * handled by other EMs. The non-default EMs would be added to the
  111. * anchor list by HW that provides offloads.
  112. */
  113. struct fc_exch_mgr_anchor {
  114. struct list_head ema_list;
  115. struct fc_exch_mgr *mp;
  116. bool (*match)(struct fc_frame *);
  117. };
  118. static void fc_exch_rrq(struct fc_exch *);
  119. static void fc_seq_ls_acc(struct fc_frame *);
  120. static void fc_seq_ls_rjt(struct fc_frame *, enum fc_els_rjt_reason,
  121. enum fc_els_rjt_explan);
  122. static void fc_exch_els_rec(struct fc_frame *);
  123. static void fc_exch_els_rrq(struct fc_frame *);
  124. /*
  125. * Internal implementation notes.
  126. *
  127. * The exchange manager is one by default in libfc but LLD may choose
  128. * to have one per CPU. The sequence manager is one per exchange manager
  129. * and currently never separated.
  130. *
  131. * Section 9.8 in FC-FS-2 specifies: "The SEQ_ID is a one-byte field
  132. * assigned by the Sequence Initiator that shall be unique for a specific
  133. * D_ID and S_ID pair while the Sequence is open." Note that it isn't
  134. * qualified by exchange ID, which one might think it would be.
  135. * In practice this limits the number of open sequences and exchanges to 256
  136. * per session. For most targets we could treat this limit as per exchange.
  137. *
  138. * The exchange and its sequence are freed when the last sequence is received.
  139. * It's possible for the remote port to leave an exchange open without
  140. * sending any sequences.
  141. *
  142. * Notes on reference counts:
  143. *
  144. * Exchanges are reference counted and exchange gets freed when the reference
  145. * count becomes zero.
  146. *
  147. * Timeouts:
  148. * Sequences are timed out for E_D_TOV and R_A_TOV.
  149. *
  150. * Sequence event handling:
  151. *
  152. * The following events may occur on initiator sequences:
  153. *
  154. * Send.
  155. * For now, the whole thing is sent.
  156. * Receive ACK
  157. * This applies only to class F.
  158. * The sequence is marked complete.
  159. * ULP completion.
  160. * The upper layer calls fc_exch_done() when done
  161. * with exchange and sequence tuple.
  162. * RX-inferred completion.
  163. * When we receive the next sequence on the same exchange, we can
  164. * retire the previous sequence ID. (XXX not implemented).
  165. * Timeout.
  166. * R_A_TOV frees the sequence ID. If we're waiting for ACK,
  167. * E_D_TOV causes abort and calls upper layer response handler
  168. * with FC_EX_TIMEOUT error.
  169. * Receive RJT
  170. * XXX defer.
  171. * Send ABTS
  172. * On timeout.
  173. *
  174. * The following events may occur on recipient sequences:
  175. *
  176. * Receive
  177. * Allocate sequence for first frame received.
  178. * Hold during receive handler.
  179. * Release when final frame received.
  180. * Keep status of last N of these for the ELS RES command. XXX TBD.
  181. * Receive ABTS
  182. * Deallocate sequence
  183. * Send RJT
  184. * Deallocate
  185. *
  186. * For now, we neglect conditions where only part of a sequence was
  187. * received or transmitted, or where out-of-order receipt is detected.
  188. */
  189. /*
  190. * Locking notes:
  191. *
  192. * The EM code run in a per-CPU worker thread.
  193. *
  194. * To protect against concurrency between a worker thread code and timers,
  195. * sequence allocation and deallocation must be locked.
  196. * - exchange refcnt can be done atomicly without locks.
  197. * - sequence allocation must be locked by exch lock.
  198. * - If the EM pool lock and ex_lock must be taken at the same time, then the
  199. * EM pool lock must be taken before the ex_lock.
  200. */
  201. /*
  202. * opcode names for debugging.
  203. */
  204. static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
  205. /**
  206. * fc_exch_name_lookup() - Lookup name by opcode
  207. * @op: Opcode to be looked up
  208. * @table: Opcode/name table
  209. * @max_index: Index not to be exceeded
  210. *
  211. * This routine is used to determine a human-readable string identifying
  212. * a R_CTL opcode.
  213. */
  214. static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
  215. unsigned int max_index)
  216. {
  217. const char *name = NULL;
  218. if (op < max_index)
  219. name = table[op];
  220. if (!name)
  221. name = "unknown";
  222. return name;
  223. }
  224. /**
  225. * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
  226. * @op: The opcode to be looked up
  227. */
  228. static const char *fc_exch_rctl_name(unsigned int op)
  229. {
  230. return fc_exch_name_lookup(op, fc_exch_rctl_names,
  231. ARRAY_SIZE(fc_exch_rctl_names));
  232. }
  233. /**
  234. * fc_exch_hold() - Increment an exchange's reference count
  235. * @ep: Echange to be held
  236. */
  237. static inline void fc_exch_hold(struct fc_exch *ep)
  238. {
  239. atomic_inc(&ep->ex_refcnt);
  240. }
  241. /**
  242. * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
  243. * and determine SOF and EOF.
  244. * @ep: The exchange to that will use the header
  245. * @fp: The frame whose header is to be modified
  246. * @f_ctl: F_CTL bits that will be used for the frame header
  247. *
  248. * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
  249. * fh_seq_id, fh_seq_cnt and the SOF and EOF.
  250. */
  251. static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
  252. u32 f_ctl)
  253. {
  254. struct fc_frame_header *fh = fc_frame_header_get(fp);
  255. u16 fill;
  256. fr_sof(fp) = ep->class;
  257. if (ep->seq.cnt)
  258. fr_sof(fp) = fc_sof_normal(ep->class);
  259. if (f_ctl & FC_FC_END_SEQ) {
  260. fr_eof(fp) = FC_EOF_T;
  261. if (fc_sof_needs_ack(ep->class))
  262. fr_eof(fp) = FC_EOF_N;
  263. /*
  264. * From F_CTL.
  265. * The number of fill bytes to make the length a 4-byte
  266. * multiple is the low order 2-bits of the f_ctl.
  267. * The fill itself will have been cleared by the frame
  268. * allocation.
  269. * After this, the length will be even, as expected by
  270. * the transport.
  271. */
  272. fill = fr_len(fp) & 3;
  273. if (fill) {
  274. fill = 4 - fill;
  275. /* TODO, this may be a problem with fragmented skb */
  276. skb_put(fp_skb(fp), fill);
  277. hton24(fh->fh_f_ctl, f_ctl | fill);
  278. }
  279. } else {
  280. WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
  281. fr_eof(fp) = FC_EOF_N;
  282. }
  283. /* Initialize remaining fh fields from fc_fill_fc_hdr */
  284. fh->fh_ox_id = htons(ep->oxid);
  285. fh->fh_rx_id = htons(ep->rxid);
  286. fh->fh_seq_id = ep->seq.id;
  287. fh->fh_seq_cnt = htons(ep->seq.cnt);
  288. }
  289. /**
  290. * fc_exch_release() - Decrement an exchange's reference count
  291. * @ep: Exchange to be released
  292. *
  293. * If the reference count reaches zero and the exchange is complete,
  294. * it is freed.
  295. */
  296. static void fc_exch_release(struct fc_exch *ep)
  297. {
  298. struct fc_exch_mgr *mp;
  299. if (atomic_dec_and_test(&ep->ex_refcnt)) {
  300. mp = ep->em;
  301. if (ep->destructor)
  302. ep->destructor(&ep->seq, ep->arg);
  303. WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
  304. mempool_free(ep, mp->ep_pool);
  305. }
  306. }
  307. /**
  308. * fc_exch_timer_cancel() - cancel exch timer
  309. * @ep: The exchange whose timer to be canceled
  310. */
  311. static inline void fc_exch_timer_cancel(struct fc_exch *ep)
  312. {
  313. if (cancel_delayed_work(&ep->timeout_work)) {
  314. FC_EXCH_DBG(ep, "Exchange timer canceled\n");
  315. atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
  316. }
  317. }
  318. /**
  319. * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
  320. * the exchange lock held
  321. * @ep: The exchange whose timer will start
  322. * @timer_msec: The timeout period
  323. *
  324. * Used for upper level protocols to time out the exchange.
  325. * The timer is cancelled when it fires or when the exchange completes.
  326. */
  327. static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
  328. unsigned int timer_msec)
  329. {
  330. if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
  331. return;
  332. FC_EXCH_DBG(ep, "Exchange timer armed : %d msecs\n", timer_msec);
  333. fc_exch_hold(ep); /* hold for timer */
  334. if (!queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
  335. msecs_to_jiffies(timer_msec)))
  336. fc_exch_release(ep);
  337. }
  338. /**
  339. * fc_exch_timer_set() - Lock the exchange and set the timer
  340. * @ep: The exchange whose timer will start
  341. * @timer_msec: The timeout period
  342. */
  343. static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
  344. {
  345. spin_lock_bh(&ep->ex_lock);
  346. fc_exch_timer_set_locked(ep, timer_msec);
  347. spin_unlock_bh(&ep->ex_lock);
  348. }
  349. /**
  350. * fc_exch_done_locked() - Complete an exchange with the exchange lock held
  351. * @ep: The exchange that is complete
  352. */
  353. static int fc_exch_done_locked(struct fc_exch *ep)
  354. {
  355. int rc = 1;
  356. /*
  357. * We must check for completion in case there are two threads
  358. * tyring to complete this. But the rrq code will reuse the
  359. * ep, and in that case we only clear the resp and set it as
  360. * complete, so it can be reused by the timer to send the rrq.
  361. */
  362. ep->resp = NULL;
  363. if (ep->state & FC_EX_DONE)
  364. return rc;
  365. ep->esb_stat |= ESB_ST_COMPLETE;
  366. if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
  367. ep->state |= FC_EX_DONE;
  368. fc_exch_timer_cancel(ep);
  369. rc = 0;
  370. }
  371. return rc;
  372. }
  373. /**
  374. * fc_exch_ptr_get() - Return an exchange from an exchange pool
  375. * @pool: Exchange Pool to get an exchange from
  376. * @index: Index of the exchange within the pool
  377. *
  378. * Use the index to get an exchange from within an exchange pool. exches
  379. * will point to an array of exchange pointers. The index will select
  380. * the exchange within the array.
  381. */
  382. static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
  383. u16 index)
  384. {
  385. struct fc_exch **exches = (struct fc_exch **)(pool + 1);
  386. return exches[index];
  387. }
  388. /**
  389. * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
  390. * @pool: The pool to assign the exchange to
  391. * @index: The index in the pool where the exchange will be assigned
  392. * @ep: The exchange to assign to the pool
  393. */
  394. static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
  395. struct fc_exch *ep)
  396. {
  397. ((struct fc_exch **)(pool + 1))[index] = ep;
  398. }
  399. /**
  400. * fc_exch_delete() - Delete an exchange
  401. * @ep: The exchange to be deleted
  402. */
  403. static void fc_exch_delete(struct fc_exch *ep)
  404. {
  405. struct fc_exch_pool *pool;
  406. u16 index;
  407. pool = ep->pool;
  408. spin_lock_bh(&pool->lock);
  409. WARN_ON(pool->total_exches <= 0);
  410. pool->total_exches--;
  411. /* update cache of free slot */
  412. index = (ep->xid - ep->em->min_xid) >> fc_cpu_order;
  413. if (pool->left == FC_XID_UNKNOWN)
  414. pool->left = index;
  415. else if (pool->right == FC_XID_UNKNOWN)
  416. pool->right = index;
  417. else
  418. pool->next_index = index;
  419. fc_exch_ptr_set(pool, index, NULL);
  420. list_del(&ep->ex_list);
  421. spin_unlock_bh(&pool->lock);
  422. fc_exch_release(ep); /* drop hold for exch in mp */
  423. }
  424. static int fc_seq_send_locked(struct fc_lport *lport, struct fc_seq *sp,
  425. struct fc_frame *fp)
  426. {
  427. struct fc_exch *ep;
  428. struct fc_frame_header *fh = fc_frame_header_get(fp);
  429. int error = -ENXIO;
  430. u32 f_ctl;
  431. u8 fh_type = fh->fh_type;
  432. ep = fc_seq_exch(sp);
  433. if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL)) {
  434. fc_frame_free(fp);
  435. goto out;
  436. }
  437. WARN_ON(!(ep->esb_stat & ESB_ST_SEQ_INIT));
  438. f_ctl = ntoh24(fh->fh_f_ctl);
  439. fc_exch_setup_hdr(ep, fp, f_ctl);
  440. fr_encaps(fp) = ep->encaps;
  441. /*
  442. * update sequence count if this frame is carrying
  443. * multiple FC frames when sequence offload is enabled
  444. * by LLD.
  445. */
  446. if (fr_max_payload(fp))
  447. sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
  448. fr_max_payload(fp));
  449. else
  450. sp->cnt++;
  451. /*
  452. * Send the frame.
  453. */
  454. error = lport->tt.frame_send(lport, fp);
  455. if (fh_type == FC_TYPE_BLS)
  456. goto out;
  457. /*
  458. * Update the exchange and sequence flags,
  459. * assuming all frames for the sequence have been sent.
  460. * We can only be called to send once for each sequence.
  461. */
  462. ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
  463. if (f_ctl & FC_FC_SEQ_INIT)
  464. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  465. out:
  466. return error;
  467. }
  468. /**
  469. * fc_seq_send() - Send a frame using existing sequence/exchange pair
  470. * @lport: The local port that the exchange will be sent on
  471. * @sp: The sequence to be sent
  472. * @fp: The frame to be sent on the exchange
  473. *
  474. * Note: The frame will be freed either by a direct call to fc_frame_free(fp)
  475. * or indirectly by calling libfc_function_template.frame_send().
  476. */
  477. static int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp,
  478. struct fc_frame *fp)
  479. {
  480. struct fc_exch *ep;
  481. int error;
  482. ep = fc_seq_exch(sp);
  483. spin_lock_bh(&ep->ex_lock);
  484. error = fc_seq_send_locked(lport, sp, fp);
  485. spin_unlock_bh(&ep->ex_lock);
  486. return error;
  487. }
  488. /**
  489. * fc_seq_alloc() - Allocate a sequence for a given exchange
  490. * @ep: The exchange to allocate a new sequence for
  491. * @seq_id: The sequence ID to be used
  492. *
  493. * We don't support multiple originated sequences on the same exchange.
  494. * By implication, any previously originated sequence on this exchange
  495. * is complete, and we reallocate the same sequence.
  496. */
  497. static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
  498. {
  499. struct fc_seq *sp;
  500. sp = &ep->seq;
  501. sp->ssb_stat = 0;
  502. sp->cnt = 0;
  503. sp->id = seq_id;
  504. return sp;
  505. }
  506. /**
  507. * fc_seq_start_next_locked() - Allocate a new sequence on the same
  508. * exchange as the supplied sequence
  509. * @sp: The sequence/exchange to get a new sequence for
  510. */
  511. static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
  512. {
  513. struct fc_exch *ep = fc_seq_exch(sp);
  514. sp = fc_seq_alloc(ep, ep->seq_id++);
  515. FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
  516. ep->f_ctl, sp->id);
  517. return sp;
  518. }
  519. /**
  520. * fc_seq_start_next() - Lock the exchange and get a new sequence
  521. * for a given sequence/exchange pair
  522. * @sp: The sequence/exchange to get a new exchange for
  523. */
  524. static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
  525. {
  526. struct fc_exch *ep = fc_seq_exch(sp);
  527. spin_lock_bh(&ep->ex_lock);
  528. sp = fc_seq_start_next_locked(sp);
  529. spin_unlock_bh(&ep->ex_lock);
  530. return sp;
  531. }
  532. /*
  533. * Set the response handler for the exchange associated with a sequence.
  534. */
  535. static void fc_seq_set_resp(struct fc_seq *sp,
  536. void (*resp)(struct fc_seq *, struct fc_frame *,
  537. void *),
  538. void *arg)
  539. {
  540. struct fc_exch *ep = fc_seq_exch(sp);
  541. spin_lock_bh(&ep->ex_lock);
  542. ep->resp = resp;
  543. ep->arg = arg;
  544. spin_unlock_bh(&ep->ex_lock);
  545. }
  546. /**
  547. * fc_exch_abort_locked() - Abort an exchange
  548. * @ep: The exchange to be aborted
  549. * @timer_msec: The period of time to wait before aborting
  550. *
  551. * Locking notes: Called with exch lock held
  552. *
  553. * Return value: 0 on success else error code
  554. */
  555. static int fc_exch_abort_locked(struct fc_exch *ep,
  556. unsigned int timer_msec)
  557. {
  558. struct fc_seq *sp;
  559. struct fc_frame *fp;
  560. int error;
  561. if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
  562. ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP))
  563. return -ENXIO;
  564. /*
  565. * Send the abort on a new sequence if possible.
  566. */
  567. sp = fc_seq_start_next_locked(&ep->seq);
  568. if (!sp)
  569. return -ENOMEM;
  570. if (timer_msec)
  571. fc_exch_timer_set_locked(ep, timer_msec);
  572. if (ep->sid) {
  573. /*
  574. * Send an abort for the sequence that timed out.
  575. */
  576. fp = fc_frame_alloc(ep->lp, 0);
  577. if (fp) {
  578. ep->esb_stat |= ESB_ST_SEQ_INIT;
  579. fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
  580. FC_TYPE_BLS, FC_FC_END_SEQ |
  581. FC_FC_SEQ_INIT, 0);
  582. error = fc_seq_send_locked(ep->lp, sp, fp);
  583. } else {
  584. error = -ENOBUFS;
  585. }
  586. } else {
  587. /*
  588. * If not logged into the fabric, don't send ABTS but leave
  589. * sequence active until next timeout.
  590. */
  591. error = 0;
  592. }
  593. ep->esb_stat |= ESB_ST_ABNORMAL;
  594. return error;
  595. }
  596. /**
  597. * fc_seq_exch_abort() - Abort an exchange and sequence
  598. * @req_sp: The sequence to be aborted
  599. * @timer_msec: The period of time to wait before aborting
  600. *
  601. * Generally called because of a timeout or an abort from the upper layer.
  602. *
  603. * Return value: 0 on success else error code
  604. */
  605. static int fc_seq_exch_abort(const struct fc_seq *req_sp,
  606. unsigned int timer_msec)
  607. {
  608. struct fc_exch *ep;
  609. int error;
  610. ep = fc_seq_exch(req_sp);
  611. spin_lock_bh(&ep->ex_lock);
  612. error = fc_exch_abort_locked(ep, timer_msec);
  613. spin_unlock_bh(&ep->ex_lock);
  614. return error;
  615. }
  616. /**
  617. * fc_exch_timeout() - Handle exchange timer expiration
  618. * @work: The work_struct identifying the exchange that timed out
  619. */
  620. static void fc_exch_timeout(struct work_struct *work)
  621. {
  622. struct fc_exch *ep = container_of(work, struct fc_exch,
  623. timeout_work.work);
  624. struct fc_seq *sp = &ep->seq;
  625. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  626. void *arg;
  627. u32 e_stat;
  628. int rc = 1;
  629. FC_EXCH_DBG(ep, "Exchange timed out\n");
  630. spin_lock_bh(&ep->ex_lock);
  631. if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
  632. goto unlock;
  633. e_stat = ep->esb_stat;
  634. if (e_stat & ESB_ST_COMPLETE) {
  635. ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
  636. spin_unlock_bh(&ep->ex_lock);
  637. if (e_stat & ESB_ST_REC_QUAL)
  638. fc_exch_rrq(ep);
  639. goto done;
  640. } else {
  641. resp = ep->resp;
  642. arg = ep->arg;
  643. ep->resp = NULL;
  644. if (e_stat & ESB_ST_ABNORMAL)
  645. rc = fc_exch_done_locked(ep);
  646. spin_unlock_bh(&ep->ex_lock);
  647. if (!rc)
  648. fc_exch_delete(ep);
  649. if (resp)
  650. resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
  651. fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
  652. goto done;
  653. }
  654. unlock:
  655. spin_unlock_bh(&ep->ex_lock);
  656. done:
  657. /*
  658. * This release matches the hold taken when the timer was set.
  659. */
  660. fc_exch_release(ep);
  661. }
  662. /**
  663. * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
  664. * @lport: The local port that the exchange is for
  665. * @mp: The exchange manager that will allocate the exchange
  666. *
  667. * Returns pointer to allocated fc_exch with exch lock held.
  668. */
  669. static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
  670. struct fc_exch_mgr *mp)
  671. {
  672. struct fc_exch *ep;
  673. unsigned int cpu;
  674. u16 index;
  675. struct fc_exch_pool *pool;
  676. /* allocate memory for exchange */
  677. ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
  678. if (!ep) {
  679. atomic_inc(&mp->stats.no_free_exch);
  680. goto out;
  681. }
  682. memset(ep, 0, sizeof(*ep));
  683. cpu = get_cpu();
  684. pool = per_cpu_ptr(mp->pool, cpu);
  685. spin_lock_bh(&pool->lock);
  686. put_cpu();
  687. /* peek cache of free slot */
  688. if (pool->left != FC_XID_UNKNOWN) {
  689. index = pool->left;
  690. pool->left = FC_XID_UNKNOWN;
  691. goto hit;
  692. }
  693. if (pool->right != FC_XID_UNKNOWN) {
  694. index = pool->right;
  695. pool->right = FC_XID_UNKNOWN;
  696. goto hit;
  697. }
  698. index = pool->next_index;
  699. /* allocate new exch from pool */
  700. while (fc_exch_ptr_get(pool, index)) {
  701. index = index == mp->pool_max_index ? 0 : index + 1;
  702. if (index == pool->next_index)
  703. goto err;
  704. }
  705. pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
  706. hit:
  707. fc_exch_hold(ep); /* hold for exch in mp */
  708. spin_lock_init(&ep->ex_lock);
  709. /*
  710. * Hold exch lock for caller to prevent fc_exch_reset()
  711. * from releasing exch while fc_exch_alloc() caller is
  712. * still working on exch.
  713. */
  714. spin_lock_bh(&ep->ex_lock);
  715. fc_exch_ptr_set(pool, index, ep);
  716. list_add_tail(&ep->ex_list, &pool->ex_list);
  717. fc_seq_alloc(ep, ep->seq_id++);
  718. pool->total_exches++;
  719. spin_unlock_bh(&pool->lock);
  720. /*
  721. * update exchange
  722. */
  723. ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
  724. ep->em = mp;
  725. ep->pool = pool;
  726. ep->lp = lport;
  727. ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
  728. ep->rxid = FC_XID_UNKNOWN;
  729. ep->class = mp->class;
  730. INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
  731. out:
  732. return ep;
  733. err:
  734. spin_unlock_bh(&pool->lock);
  735. atomic_inc(&mp->stats.no_free_exch_xid);
  736. mempool_free(ep, mp->ep_pool);
  737. return NULL;
  738. }
  739. /**
  740. * fc_exch_alloc() - Allocate an exchange from an EM on a
  741. * local port's list of EMs.
  742. * @lport: The local port that will own the exchange
  743. * @fp: The FC frame that the exchange will be for
  744. *
  745. * This function walks the list of exchange manager(EM)
  746. * anchors to select an EM for a new exchange allocation. The
  747. * EM is selected when a NULL match function pointer is encountered
  748. * or when a call to a match function returns true.
  749. */
  750. static inline struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
  751. struct fc_frame *fp)
  752. {
  753. struct fc_exch_mgr_anchor *ema;
  754. list_for_each_entry(ema, &lport->ema_list, ema_list)
  755. if (!ema->match || ema->match(fp))
  756. return fc_exch_em_alloc(lport, ema->mp);
  757. return NULL;
  758. }
  759. /**
  760. * fc_exch_find() - Lookup and hold an exchange
  761. * @mp: The exchange manager to lookup the exchange from
  762. * @xid: The XID of the exchange to look up
  763. */
  764. static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
  765. {
  766. struct fc_exch_pool *pool;
  767. struct fc_exch *ep = NULL;
  768. if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
  769. pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
  770. spin_lock_bh(&pool->lock);
  771. ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
  772. if (ep) {
  773. WARN_ON(ep->xid != xid);
  774. fc_exch_hold(ep);
  775. }
  776. spin_unlock_bh(&pool->lock);
  777. }
  778. return ep;
  779. }
  780. /**
  781. * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
  782. * the memory allocated for the related objects may be freed.
  783. * @sp: The sequence that has completed
  784. */
  785. static void fc_exch_done(struct fc_seq *sp)
  786. {
  787. struct fc_exch *ep = fc_seq_exch(sp);
  788. int rc;
  789. spin_lock_bh(&ep->ex_lock);
  790. rc = fc_exch_done_locked(ep);
  791. spin_unlock_bh(&ep->ex_lock);
  792. if (!rc)
  793. fc_exch_delete(ep);
  794. }
  795. /**
  796. * fc_exch_resp() - Allocate a new exchange for a response frame
  797. * @lport: The local port that the exchange was for
  798. * @mp: The exchange manager to allocate the exchange from
  799. * @fp: The response frame
  800. *
  801. * Sets the responder ID in the frame header.
  802. */
  803. static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
  804. struct fc_exch_mgr *mp,
  805. struct fc_frame *fp)
  806. {
  807. struct fc_exch *ep;
  808. struct fc_frame_header *fh;
  809. ep = fc_exch_alloc(lport, fp);
  810. if (ep) {
  811. ep->class = fc_frame_class(fp);
  812. /*
  813. * Set EX_CTX indicating we're responding on this exchange.
  814. */
  815. ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
  816. ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
  817. fh = fc_frame_header_get(fp);
  818. ep->sid = ntoh24(fh->fh_d_id);
  819. ep->did = ntoh24(fh->fh_s_id);
  820. ep->oid = ep->did;
  821. /*
  822. * Allocated exchange has placed the XID in the
  823. * originator field. Move it to the responder field,
  824. * and set the originator XID from the frame.
  825. */
  826. ep->rxid = ep->xid;
  827. ep->oxid = ntohs(fh->fh_ox_id);
  828. ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
  829. if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
  830. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  831. fc_exch_hold(ep); /* hold for caller */
  832. spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */
  833. }
  834. return ep;
  835. }
  836. /**
  837. * fc_seq_lookup_recip() - Find a sequence where the other end
  838. * originated the sequence
  839. * @lport: The local port that the frame was sent to
  840. * @mp: The Exchange Manager to lookup the exchange from
  841. * @fp: The frame associated with the sequence we're looking for
  842. *
  843. * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
  844. * on the ep that should be released by the caller.
  845. */
  846. static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
  847. struct fc_exch_mgr *mp,
  848. struct fc_frame *fp)
  849. {
  850. struct fc_frame_header *fh = fc_frame_header_get(fp);
  851. struct fc_exch *ep = NULL;
  852. struct fc_seq *sp = NULL;
  853. enum fc_pf_rjt_reason reject = FC_RJT_NONE;
  854. u32 f_ctl;
  855. u16 xid;
  856. f_ctl = ntoh24(fh->fh_f_ctl);
  857. WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
  858. /*
  859. * Lookup or create the exchange if we will be creating the sequence.
  860. */
  861. if (f_ctl & FC_FC_EX_CTX) {
  862. xid = ntohs(fh->fh_ox_id); /* we originated exch */
  863. ep = fc_exch_find(mp, xid);
  864. if (!ep) {
  865. atomic_inc(&mp->stats.xid_not_found);
  866. reject = FC_RJT_OX_ID;
  867. goto out;
  868. }
  869. if (ep->rxid == FC_XID_UNKNOWN)
  870. ep->rxid = ntohs(fh->fh_rx_id);
  871. else if (ep->rxid != ntohs(fh->fh_rx_id)) {
  872. reject = FC_RJT_OX_ID;
  873. goto rel;
  874. }
  875. } else {
  876. xid = ntohs(fh->fh_rx_id); /* we are the responder */
  877. /*
  878. * Special case for MDS issuing an ELS TEST with a
  879. * bad rxid of 0.
  880. * XXX take this out once we do the proper reject.
  881. */
  882. if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
  883. fc_frame_payload_op(fp) == ELS_TEST) {
  884. fh->fh_rx_id = htons(FC_XID_UNKNOWN);
  885. xid = FC_XID_UNKNOWN;
  886. }
  887. /*
  888. * new sequence - find the exchange
  889. */
  890. ep = fc_exch_find(mp, xid);
  891. if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
  892. if (ep) {
  893. atomic_inc(&mp->stats.xid_busy);
  894. reject = FC_RJT_RX_ID;
  895. goto rel;
  896. }
  897. ep = fc_exch_resp(lport, mp, fp);
  898. if (!ep) {
  899. reject = FC_RJT_EXCH_EST; /* XXX */
  900. goto out;
  901. }
  902. xid = ep->xid; /* get our XID */
  903. } else if (!ep) {
  904. atomic_inc(&mp->stats.xid_not_found);
  905. reject = FC_RJT_RX_ID; /* XID not found */
  906. goto out;
  907. }
  908. }
  909. spin_lock_bh(&ep->ex_lock);
  910. /*
  911. * At this point, we have the exchange held.
  912. * Find or create the sequence.
  913. */
  914. if (fc_sof_is_init(fr_sof(fp))) {
  915. sp = &ep->seq;
  916. sp->ssb_stat |= SSB_ST_RESP;
  917. sp->id = fh->fh_seq_id;
  918. } else {
  919. sp = &ep->seq;
  920. if (sp->id != fh->fh_seq_id) {
  921. atomic_inc(&mp->stats.seq_not_found);
  922. if (f_ctl & FC_FC_END_SEQ) {
  923. /*
  924. * Update sequence_id based on incoming last
  925. * frame of sequence exchange. This is needed
  926. * for FC target where DDP has been used
  927. * on target where, stack is indicated only
  928. * about last frame's (payload _header) header.
  929. * Whereas "seq_id" which is part of
  930. * frame_header is allocated by initiator
  931. * which is totally different from "seq_id"
  932. * allocated when XFER_RDY was sent by target.
  933. * To avoid false -ve which results into not
  934. * sending RSP, hence write request on other
  935. * end never finishes.
  936. */
  937. sp->ssb_stat |= SSB_ST_RESP;
  938. sp->id = fh->fh_seq_id;
  939. } else {
  940. spin_unlock_bh(&ep->ex_lock);
  941. /* sequence/exch should exist */
  942. reject = FC_RJT_SEQ_ID;
  943. goto rel;
  944. }
  945. }
  946. }
  947. WARN_ON(ep != fc_seq_exch(sp));
  948. if (f_ctl & FC_FC_SEQ_INIT)
  949. ep->esb_stat |= ESB_ST_SEQ_INIT;
  950. spin_unlock_bh(&ep->ex_lock);
  951. fr_seq(fp) = sp;
  952. out:
  953. return reject;
  954. rel:
  955. fc_exch_done(&ep->seq);
  956. fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
  957. return reject;
  958. }
  959. /**
  960. * fc_seq_lookup_orig() - Find a sequence where this end
  961. * originated the sequence
  962. * @mp: The Exchange Manager to lookup the exchange from
  963. * @fp: The frame associated with the sequence we're looking for
  964. *
  965. * Does not hold the sequence for the caller.
  966. */
  967. static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
  968. struct fc_frame *fp)
  969. {
  970. struct fc_frame_header *fh = fc_frame_header_get(fp);
  971. struct fc_exch *ep;
  972. struct fc_seq *sp = NULL;
  973. u32 f_ctl;
  974. u16 xid;
  975. f_ctl = ntoh24(fh->fh_f_ctl);
  976. WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
  977. xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
  978. ep = fc_exch_find(mp, xid);
  979. if (!ep)
  980. return NULL;
  981. if (ep->seq.id == fh->fh_seq_id) {
  982. /*
  983. * Save the RX_ID if we didn't previously know it.
  984. */
  985. sp = &ep->seq;
  986. if ((f_ctl & FC_FC_EX_CTX) != 0 &&
  987. ep->rxid == FC_XID_UNKNOWN) {
  988. ep->rxid = ntohs(fh->fh_rx_id);
  989. }
  990. }
  991. fc_exch_release(ep);
  992. return sp;
  993. }
  994. /**
  995. * fc_exch_set_addr() - Set the source and destination IDs for an exchange
  996. * @ep: The exchange to set the addresses for
  997. * @orig_id: The originator's ID
  998. * @resp_id: The responder's ID
  999. *
  1000. * Note this must be done before the first sequence of the exchange is sent.
  1001. */
  1002. static void fc_exch_set_addr(struct fc_exch *ep,
  1003. u32 orig_id, u32 resp_id)
  1004. {
  1005. ep->oid = orig_id;
  1006. if (ep->esb_stat & ESB_ST_RESP) {
  1007. ep->sid = resp_id;
  1008. ep->did = orig_id;
  1009. } else {
  1010. ep->sid = orig_id;
  1011. ep->did = resp_id;
  1012. }
  1013. }
  1014. /**
  1015. * fc_seq_els_rsp_send() - Send an ELS response using information from
  1016. * the existing sequence/exchange.
  1017. * @fp: The received frame
  1018. * @els_cmd: The ELS command to be sent
  1019. * @els_data: The ELS data to be sent
  1020. *
  1021. * The received frame is not freed.
  1022. */
  1023. static void fc_seq_els_rsp_send(struct fc_frame *fp, enum fc_els_cmd els_cmd,
  1024. struct fc_seq_els_data *els_data)
  1025. {
  1026. switch (els_cmd) {
  1027. case ELS_LS_RJT:
  1028. fc_seq_ls_rjt(fp, els_data->reason, els_data->explan);
  1029. break;
  1030. case ELS_LS_ACC:
  1031. fc_seq_ls_acc(fp);
  1032. break;
  1033. case ELS_RRQ:
  1034. fc_exch_els_rrq(fp);
  1035. break;
  1036. case ELS_REC:
  1037. fc_exch_els_rec(fp);
  1038. break;
  1039. default:
  1040. FC_LPORT_DBG(fr_dev(fp), "Invalid ELS CMD:%x\n", els_cmd);
  1041. }
  1042. }
  1043. /**
  1044. * fc_seq_send_last() - Send a sequence that is the last in the exchange
  1045. * @sp: The sequence that is to be sent
  1046. * @fp: The frame that will be sent on the sequence
  1047. * @rctl: The R_CTL information to be sent
  1048. * @fh_type: The frame header type
  1049. */
  1050. static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
  1051. enum fc_rctl rctl, enum fc_fh_type fh_type)
  1052. {
  1053. u32 f_ctl;
  1054. struct fc_exch *ep = fc_seq_exch(sp);
  1055. f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
  1056. f_ctl |= ep->f_ctl;
  1057. fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
  1058. fc_seq_send_locked(ep->lp, sp, fp);
  1059. }
  1060. /**
  1061. * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
  1062. * @sp: The sequence to send the ACK on
  1063. * @rx_fp: The received frame that is being acknoledged
  1064. *
  1065. * Send ACK_1 (or equiv.) indicating we received something.
  1066. */
  1067. static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
  1068. {
  1069. struct fc_frame *fp;
  1070. struct fc_frame_header *rx_fh;
  1071. struct fc_frame_header *fh;
  1072. struct fc_exch *ep = fc_seq_exch(sp);
  1073. struct fc_lport *lport = ep->lp;
  1074. unsigned int f_ctl;
  1075. /*
  1076. * Don't send ACKs for class 3.
  1077. */
  1078. if (fc_sof_needs_ack(fr_sof(rx_fp))) {
  1079. fp = fc_frame_alloc(lport, 0);
  1080. if (!fp)
  1081. return;
  1082. fh = fc_frame_header_get(fp);
  1083. fh->fh_r_ctl = FC_RCTL_ACK_1;
  1084. fh->fh_type = FC_TYPE_BLS;
  1085. /*
  1086. * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
  1087. * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
  1088. * Bits 9-8 are meaningful (retransmitted or unidirectional).
  1089. * Last ACK uses bits 7-6 (continue sequence),
  1090. * bits 5-4 are meaningful (what kind of ACK to use).
  1091. */
  1092. rx_fh = fc_frame_header_get(rx_fp);
  1093. f_ctl = ntoh24(rx_fh->fh_f_ctl);
  1094. f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
  1095. FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
  1096. FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
  1097. FC_FC_RETX_SEQ | FC_FC_UNI_TX;
  1098. f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
  1099. hton24(fh->fh_f_ctl, f_ctl);
  1100. fc_exch_setup_hdr(ep, fp, f_ctl);
  1101. fh->fh_seq_id = rx_fh->fh_seq_id;
  1102. fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
  1103. fh->fh_parm_offset = htonl(1); /* ack single frame */
  1104. fr_sof(fp) = fr_sof(rx_fp);
  1105. if (f_ctl & FC_FC_END_SEQ)
  1106. fr_eof(fp) = FC_EOF_T;
  1107. else
  1108. fr_eof(fp) = FC_EOF_N;
  1109. lport->tt.frame_send(lport, fp);
  1110. }
  1111. }
  1112. /**
  1113. * fc_exch_send_ba_rjt() - Send BLS Reject
  1114. * @rx_fp: The frame being rejected
  1115. * @reason: The reason the frame is being rejected
  1116. * @explan: The explanation for the rejection
  1117. *
  1118. * This is for rejecting BA_ABTS only.
  1119. */
  1120. static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
  1121. enum fc_ba_rjt_reason reason,
  1122. enum fc_ba_rjt_explan explan)
  1123. {
  1124. struct fc_frame *fp;
  1125. struct fc_frame_header *rx_fh;
  1126. struct fc_frame_header *fh;
  1127. struct fc_ba_rjt *rp;
  1128. struct fc_lport *lport;
  1129. unsigned int f_ctl;
  1130. lport = fr_dev(rx_fp);
  1131. fp = fc_frame_alloc(lport, sizeof(*rp));
  1132. if (!fp)
  1133. return;
  1134. fh = fc_frame_header_get(fp);
  1135. rx_fh = fc_frame_header_get(rx_fp);
  1136. memset(fh, 0, sizeof(*fh) + sizeof(*rp));
  1137. rp = fc_frame_payload_get(fp, sizeof(*rp));
  1138. rp->br_reason = reason;
  1139. rp->br_explan = explan;
  1140. /*
  1141. * seq_id, cs_ctl, df_ctl and param/offset are zero.
  1142. */
  1143. memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
  1144. memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
  1145. fh->fh_ox_id = rx_fh->fh_ox_id;
  1146. fh->fh_rx_id = rx_fh->fh_rx_id;
  1147. fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
  1148. fh->fh_r_ctl = FC_RCTL_BA_RJT;
  1149. fh->fh_type = FC_TYPE_BLS;
  1150. /*
  1151. * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
  1152. * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
  1153. * Bits 9-8 are meaningful (retransmitted or unidirectional).
  1154. * Last ACK uses bits 7-6 (continue sequence),
  1155. * bits 5-4 are meaningful (what kind of ACK to use).
  1156. * Always set LAST_SEQ, END_SEQ.
  1157. */
  1158. f_ctl = ntoh24(rx_fh->fh_f_ctl);
  1159. f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
  1160. FC_FC_END_CONN | FC_FC_SEQ_INIT |
  1161. FC_FC_RETX_SEQ | FC_FC_UNI_TX;
  1162. f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
  1163. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  1164. f_ctl &= ~FC_FC_FIRST_SEQ;
  1165. hton24(fh->fh_f_ctl, f_ctl);
  1166. fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
  1167. fr_eof(fp) = FC_EOF_T;
  1168. if (fc_sof_needs_ack(fr_sof(fp)))
  1169. fr_eof(fp) = FC_EOF_N;
  1170. lport->tt.frame_send(lport, fp);
  1171. }
  1172. /**
  1173. * fc_exch_recv_abts() - Handle an incoming ABTS
  1174. * @ep: The exchange the abort was on
  1175. * @rx_fp: The ABTS frame
  1176. *
  1177. * This would be for target mode usually, but could be due to lost
  1178. * FCP transfer ready, confirm or RRQ. We always handle this as an
  1179. * exchange abort, ignoring the parameter.
  1180. */
  1181. static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
  1182. {
  1183. struct fc_frame *fp;
  1184. struct fc_ba_acc *ap;
  1185. struct fc_frame_header *fh;
  1186. struct fc_seq *sp;
  1187. if (!ep)
  1188. goto reject;
  1189. fp = fc_frame_alloc(ep->lp, sizeof(*ap));
  1190. if (!fp)
  1191. goto free;
  1192. spin_lock_bh(&ep->ex_lock);
  1193. if (ep->esb_stat & ESB_ST_COMPLETE) {
  1194. spin_unlock_bh(&ep->ex_lock);
  1195. fc_frame_free(fp);
  1196. goto reject;
  1197. }
  1198. if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
  1199. ep->esb_stat |= ESB_ST_REC_QUAL;
  1200. fc_exch_hold(ep); /* hold for REC_QUAL */
  1201. }
  1202. fc_exch_timer_set_locked(ep, ep->r_a_tov);
  1203. fh = fc_frame_header_get(fp);
  1204. ap = fc_frame_payload_get(fp, sizeof(*ap));
  1205. memset(ap, 0, sizeof(*ap));
  1206. sp = &ep->seq;
  1207. ap->ba_high_seq_cnt = htons(0xffff);
  1208. if (sp->ssb_stat & SSB_ST_RESP) {
  1209. ap->ba_seq_id = sp->id;
  1210. ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
  1211. ap->ba_high_seq_cnt = fh->fh_seq_cnt;
  1212. ap->ba_low_seq_cnt = htons(sp->cnt);
  1213. }
  1214. sp = fc_seq_start_next_locked(sp);
  1215. fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
  1216. ep->esb_stat |= ESB_ST_ABNORMAL;
  1217. spin_unlock_bh(&ep->ex_lock);
  1218. free:
  1219. fc_frame_free(rx_fp);
  1220. return;
  1221. reject:
  1222. fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
  1223. goto free;
  1224. }
  1225. /**
  1226. * fc_seq_assign() - Assign exchange and sequence for incoming request
  1227. * @lport: The local port that received the request
  1228. * @fp: The request frame
  1229. *
  1230. * On success, the sequence pointer will be returned and also in fr_seq(@fp).
  1231. * A reference will be held on the exchange/sequence for the caller, which
  1232. * must call fc_seq_release().
  1233. */
  1234. static struct fc_seq *fc_seq_assign(struct fc_lport *lport, struct fc_frame *fp)
  1235. {
  1236. struct fc_exch_mgr_anchor *ema;
  1237. WARN_ON(lport != fr_dev(fp));
  1238. WARN_ON(fr_seq(fp));
  1239. fr_seq(fp) = NULL;
  1240. list_for_each_entry(ema, &lport->ema_list, ema_list)
  1241. if ((!ema->match || ema->match(fp)) &&
  1242. fc_seq_lookup_recip(lport, ema->mp, fp) == FC_RJT_NONE)
  1243. break;
  1244. return fr_seq(fp);
  1245. }
  1246. /**
  1247. * fc_seq_release() - Release the hold
  1248. * @sp: The sequence.
  1249. */
  1250. static void fc_seq_release(struct fc_seq *sp)
  1251. {
  1252. fc_exch_release(fc_seq_exch(sp));
  1253. }
  1254. /**
  1255. * fc_exch_recv_req() - Handler for an incoming request
  1256. * @lport: The local port that received the request
  1257. * @mp: The EM that the exchange is on
  1258. * @fp: The request frame
  1259. *
  1260. * This is used when the other end is originating the exchange
  1261. * and the sequence.
  1262. */
  1263. static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
  1264. struct fc_frame *fp)
  1265. {
  1266. struct fc_frame_header *fh = fc_frame_header_get(fp);
  1267. struct fc_seq *sp = NULL;
  1268. struct fc_exch *ep = NULL;
  1269. enum fc_pf_rjt_reason reject;
  1270. /* We can have the wrong fc_lport at this point with NPIV, which is a
  1271. * problem now that we know a new exchange needs to be allocated
  1272. */
  1273. lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
  1274. if (!lport) {
  1275. fc_frame_free(fp);
  1276. return;
  1277. }
  1278. fr_dev(fp) = lport;
  1279. BUG_ON(fr_seq(fp)); /* XXX remove later */
  1280. /*
  1281. * If the RX_ID is 0xffff, don't allocate an exchange.
  1282. * The upper-level protocol may request one later, if needed.
  1283. */
  1284. if (fh->fh_rx_id == htons(FC_XID_UNKNOWN))
  1285. return lport->tt.lport_recv(lport, fp);
  1286. reject = fc_seq_lookup_recip(lport, mp, fp);
  1287. if (reject == FC_RJT_NONE) {
  1288. sp = fr_seq(fp); /* sequence will be held */
  1289. ep = fc_seq_exch(sp);
  1290. fc_seq_send_ack(sp, fp);
  1291. ep->encaps = fr_encaps(fp);
  1292. /*
  1293. * Call the receive function.
  1294. *
  1295. * The receive function may allocate a new sequence
  1296. * over the old one, so we shouldn't change the
  1297. * sequence after this.
  1298. *
  1299. * The frame will be freed by the receive function.
  1300. * If new exch resp handler is valid then call that
  1301. * first.
  1302. */
  1303. if (ep->resp)
  1304. ep->resp(sp, fp, ep->arg);
  1305. else
  1306. lport->tt.lport_recv(lport, fp);
  1307. fc_exch_release(ep); /* release from lookup */
  1308. } else {
  1309. FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
  1310. reject);
  1311. fc_frame_free(fp);
  1312. }
  1313. }
  1314. /**
  1315. * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
  1316. * end is the originator of the sequence that is a
  1317. * response to our initial exchange
  1318. * @mp: The EM that the exchange is on
  1319. * @fp: The response frame
  1320. */
  1321. static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1322. {
  1323. struct fc_frame_header *fh = fc_frame_header_get(fp);
  1324. struct fc_seq *sp;
  1325. struct fc_exch *ep;
  1326. enum fc_sof sof;
  1327. u32 f_ctl;
  1328. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  1329. void *ex_resp_arg;
  1330. int rc;
  1331. ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
  1332. if (!ep) {
  1333. atomic_inc(&mp->stats.xid_not_found);
  1334. goto out;
  1335. }
  1336. if (ep->esb_stat & ESB_ST_COMPLETE) {
  1337. atomic_inc(&mp->stats.xid_not_found);
  1338. goto rel;
  1339. }
  1340. if (ep->rxid == FC_XID_UNKNOWN)
  1341. ep->rxid = ntohs(fh->fh_rx_id);
  1342. if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
  1343. atomic_inc(&mp->stats.xid_not_found);
  1344. goto rel;
  1345. }
  1346. if (ep->did != ntoh24(fh->fh_s_id) &&
  1347. ep->did != FC_FID_FLOGI) {
  1348. atomic_inc(&mp->stats.xid_not_found);
  1349. goto rel;
  1350. }
  1351. sof = fr_sof(fp);
  1352. sp = &ep->seq;
  1353. if (fc_sof_is_init(sof)) {
  1354. sp->ssb_stat |= SSB_ST_RESP;
  1355. sp->id = fh->fh_seq_id;
  1356. } else if (sp->id != fh->fh_seq_id) {
  1357. atomic_inc(&mp->stats.seq_not_found);
  1358. goto rel;
  1359. }
  1360. f_ctl = ntoh24(fh->fh_f_ctl);
  1361. fr_seq(fp) = sp;
  1362. spin_lock_bh(&ep->ex_lock);
  1363. if (f_ctl & FC_FC_SEQ_INIT)
  1364. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1365. spin_unlock_bh(&ep->ex_lock);
  1366. if (fc_sof_needs_ack(sof))
  1367. fc_seq_send_ack(sp, fp);
  1368. resp = ep->resp;
  1369. ex_resp_arg = ep->arg;
  1370. if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
  1371. (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
  1372. (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
  1373. spin_lock_bh(&ep->ex_lock);
  1374. resp = ep->resp;
  1375. rc = fc_exch_done_locked(ep);
  1376. WARN_ON(fc_seq_exch(sp) != ep);
  1377. spin_unlock_bh(&ep->ex_lock);
  1378. if (!rc)
  1379. fc_exch_delete(ep);
  1380. }
  1381. /*
  1382. * Call the receive function.
  1383. * The sequence is held (has a refcnt) for us,
  1384. * but not for the receive function.
  1385. *
  1386. * The receive function may allocate a new sequence
  1387. * over the old one, so we shouldn't change the
  1388. * sequence after this.
  1389. *
  1390. * The frame will be freed by the receive function.
  1391. * If new exch resp handler is valid then call that
  1392. * first.
  1393. */
  1394. if (resp)
  1395. resp(sp, fp, ex_resp_arg);
  1396. else
  1397. fc_frame_free(fp);
  1398. fc_exch_release(ep);
  1399. return;
  1400. rel:
  1401. fc_exch_release(ep);
  1402. out:
  1403. fc_frame_free(fp);
  1404. }
  1405. /**
  1406. * fc_exch_recv_resp() - Handler for a sequence where other end is
  1407. * responding to our sequence
  1408. * @mp: The EM that the exchange is on
  1409. * @fp: The response frame
  1410. */
  1411. static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1412. {
  1413. struct fc_seq *sp;
  1414. sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
  1415. if (!sp)
  1416. atomic_inc(&mp->stats.xid_not_found);
  1417. else
  1418. atomic_inc(&mp->stats.non_bls_resp);
  1419. fc_frame_free(fp);
  1420. }
  1421. /**
  1422. * fc_exch_abts_resp() - Handler for a response to an ABT
  1423. * @ep: The exchange that the frame is on
  1424. * @fp: The response frame
  1425. *
  1426. * This response would be to an ABTS cancelling an exchange or sequence.
  1427. * The response can be either BA_ACC or BA_RJT
  1428. */
  1429. static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
  1430. {
  1431. void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
  1432. void *ex_resp_arg;
  1433. struct fc_frame_header *fh;
  1434. struct fc_ba_acc *ap;
  1435. struct fc_seq *sp;
  1436. u16 low;
  1437. u16 high;
  1438. int rc = 1, has_rec = 0;
  1439. fh = fc_frame_header_get(fp);
  1440. FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
  1441. fc_exch_rctl_name(fh->fh_r_ctl));
  1442. if (cancel_delayed_work_sync(&ep->timeout_work)) {
  1443. FC_EXCH_DBG(ep, "Exchange timer canceled due to ABTS response\n");
  1444. fc_exch_release(ep); /* release from pending timer hold */
  1445. }
  1446. spin_lock_bh(&ep->ex_lock);
  1447. switch (fh->fh_r_ctl) {
  1448. case FC_RCTL_BA_ACC:
  1449. ap = fc_frame_payload_get(fp, sizeof(*ap));
  1450. if (!ap)
  1451. break;
  1452. /*
  1453. * Decide whether to establish a Recovery Qualifier.
  1454. * We do this if there is a non-empty SEQ_CNT range and
  1455. * SEQ_ID is the same as the one we aborted.
  1456. */
  1457. low = ntohs(ap->ba_low_seq_cnt);
  1458. high = ntohs(ap->ba_high_seq_cnt);
  1459. if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
  1460. (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
  1461. ap->ba_seq_id == ep->seq_id) && low != high) {
  1462. ep->esb_stat |= ESB_ST_REC_QUAL;
  1463. fc_exch_hold(ep); /* hold for recovery qualifier */
  1464. has_rec = 1;
  1465. }
  1466. break;
  1467. case FC_RCTL_BA_RJT:
  1468. break;
  1469. default:
  1470. break;
  1471. }
  1472. resp = ep->resp;
  1473. ex_resp_arg = ep->arg;
  1474. /* do we need to do some other checks here. Can we reuse more of
  1475. * fc_exch_recv_seq_resp
  1476. */
  1477. sp = &ep->seq;
  1478. /*
  1479. * do we want to check END_SEQ as well as LAST_SEQ here?
  1480. */
  1481. if (ep->fh_type != FC_TYPE_FCP &&
  1482. ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
  1483. rc = fc_exch_done_locked(ep);
  1484. spin_unlock_bh(&ep->ex_lock);
  1485. if (!rc)
  1486. fc_exch_delete(ep);
  1487. if (resp)
  1488. resp(sp, fp, ex_resp_arg);
  1489. else
  1490. fc_frame_free(fp);
  1491. if (has_rec)
  1492. fc_exch_timer_set(ep, ep->r_a_tov);
  1493. }
  1494. /**
  1495. * fc_exch_recv_bls() - Handler for a BLS sequence
  1496. * @mp: The EM that the exchange is on
  1497. * @fp: The request frame
  1498. *
  1499. * The BLS frame is always a sequence initiated by the remote side.
  1500. * We may be either the originator or recipient of the exchange.
  1501. */
  1502. static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
  1503. {
  1504. struct fc_frame_header *fh;
  1505. struct fc_exch *ep;
  1506. u32 f_ctl;
  1507. fh = fc_frame_header_get(fp);
  1508. f_ctl = ntoh24(fh->fh_f_ctl);
  1509. fr_seq(fp) = NULL;
  1510. ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
  1511. ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
  1512. if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
  1513. spin_lock_bh(&ep->ex_lock);
  1514. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1515. spin_unlock_bh(&ep->ex_lock);
  1516. }
  1517. if (f_ctl & FC_FC_SEQ_CTX) {
  1518. /*
  1519. * A response to a sequence we initiated.
  1520. * This should only be ACKs for class 2 or F.
  1521. */
  1522. switch (fh->fh_r_ctl) {
  1523. case FC_RCTL_ACK_1:
  1524. case FC_RCTL_ACK_0:
  1525. break;
  1526. default:
  1527. if (ep)
  1528. FC_EXCH_DBG(ep, "BLS rctl %x - %s received\n",
  1529. fh->fh_r_ctl,
  1530. fc_exch_rctl_name(fh->fh_r_ctl));
  1531. break;
  1532. }
  1533. fc_frame_free(fp);
  1534. } else {
  1535. switch (fh->fh_r_ctl) {
  1536. case FC_RCTL_BA_RJT:
  1537. case FC_RCTL_BA_ACC:
  1538. if (ep)
  1539. fc_exch_abts_resp(ep, fp);
  1540. else
  1541. fc_frame_free(fp);
  1542. break;
  1543. case FC_RCTL_BA_ABTS:
  1544. fc_exch_recv_abts(ep, fp);
  1545. break;
  1546. default: /* ignore junk */
  1547. fc_frame_free(fp);
  1548. break;
  1549. }
  1550. }
  1551. if (ep)
  1552. fc_exch_release(ep); /* release hold taken by fc_exch_find */
  1553. }
  1554. /**
  1555. * fc_seq_ls_acc() - Accept sequence with LS_ACC
  1556. * @rx_fp: The received frame, not freed here.
  1557. *
  1558. * If this fails due to allocation or transmit congestion, assume the
  1559. * originator will repeat the sequence.
  1560. */
  1561. static void fc_seq_ls_acc(struct fc_frame *rx_fp)
  1562. {
  1563. struct fc_lport *lport;
  1564. struct fc_els_ls_acc *acc;
  1565. struct fc_frame *fp;
  1566. lport = fr_dev(rx_fp);
  1567. fp = fc_frame_alloc(lport, sizeof(*acc));
  1568. if (!fp)
  1569. return;
  1570. acc = fc_frame_payload_get(fp, sizeof(*acc));
  1571. memset(acc, 0, sizeof(*acc));
  1572. acc->la_cmd = ELS_LS_ACC;
  1573. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1574. lport->tt.frame_send(lport, fp);
  1575. }
  1576. /**
  1577. * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
  1578. * @rx_fp: The received frame, not freed here.
  1579. * @reason: The reason the sequence is being rejected
  1580. * @explan: The explanation for the rejection
  1581. *
  1582. * If this fails due to allocation or transmit congestion, assume the
  1583. * originator will repeat the sequence.
  1584. */
  1585. static void fc_seq_ls_rjt(struct fc_frame *rx_fp, enum fc_els_rjt_reason reason,
  1586. enum fc_els_rjt_explan explan)
  1587. {
  1588. struct fc_lport *lport;
  1589. struct fc_els_ls_rjt *rjt;
  1590. struct fc_frame *fp;
  1591. lport = fr_dev(rx_fp);
  1592. fp = fc_frame_alloc(lport, sizeof(*rjt));
  1593. if (!fp)
  1594. return;
  1595. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1596. memset(rjt, 0, sizeof(*rjt));
  1597. rjt->er_cmd = ELS_LS_RJT;
  1598. rjt->er_reason = reason;
  1599. rjt->er_explan = explan;
  1600. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  1601. lport->tt.frame_send(lport, fp);
  1602. }
  1603. /**
  1604. * fc_exch_reset() - Reset an exchange
  1605. * @ep: The exchange to be reset
  1606. */
  1607. static void fc_exch_reset(struct fc_exch *ep)
  1608. {
  1609. struct fc_seq *sp;
  1610. void (*resp)(struct fc_seq *, struct fc_frame *, void *);
  1611. void *arg;
  1612. int rc = 1;
  1613. spin_lock_bh(&ep->ex_lock);
  1614. fc_exch_abort_locked(ep, 0);
  1615. ep->state |= FC_EX_RST_CLEANUP;
  1616. fc_exch_timer_cancel(ep);
  1617. resp = ep->resp;
  1618. ep->resp = NULL;
  1619. if (ep->esb_stat & ESB_ST_REC_QUAL)
  1620. atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
  1621. ep->esb_stat &= ~ESB_ST_REC_QUAL;
  1622. arg = ep->arg;
  1623. sp = &ep->seq;
  1624. rc = fc_exch_done_locked(ep);
  1625. spin_unlock_bh(&ep->ex_lock);
  1626. if (!rc)
  1627. fc_exch_delete(ep);
  1628. if (resp)
  1629. resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
  1630. }
  1631. /**
  1632. * fc_exch_pool_reset() - Reset a per cpu exchange pool
  1633. * @lport: The local port that the exchange pool is on
  1634. * @pool: The exchange pool to be reset
  1635. * @sid: The source ID
  1636. * @did: The destination ID
  1637. *
  1638. * Resets a per cpu exches pool, releasing all of its sequences
  1639. * and exchanges. If sid is non-zero then reset only exchanges
  1640. * we sourced from the local port's FID. If did is non-zero then
  1641. * only reset exchanges destined for the local port's FID.
  1642. */
  1643. static void fc_exch_pool_reset(struct fc_lport *lport,
  1644. struct fc_exch_pool *pool,
  1645. u32 sid, u32 did)
  1646. {
  1647. struct fc_exch *ep;
  1648. struct fc_exch *next;
  1649. spin_lock_bh(&pool->lock);
  1650. restart:
  1651. list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
  1652. if ((lport == ep->lp) &&
  1653. (sid == 0 || sid == ep->sid) &&
  1654. (did == 0 || did == ep->did)) {
  1655. fc_exch_hold(ep);
  1656. spin_unlock_bh(&pool->lock);
  1657. fc_exch_reset(ep);
  1658. fc_exch_release(ep);
  1659. spin_lock_bh(&pool->lock);
  1660. /*
  1661. * must restart loop incase while lock
  1662. * was down multiple eps were released.
  1663. */
  1664. goto restart;
  1665. }
  1666. }
  1667. pool->next_index = 0;
  1668. pool->left = FC_XID_UNKNOWN;
  1669. pool->right = FC_XID_UNKNOWN;
  1670. spin_unlock_bh(&pool->lock);
  1671. }
  1672. /**
  1673. * fc_exch_mgr_reset() - Reset all EMs of a local port
  1674. * @lport: The local port whose EMs are to be reset
  1675. * @sid: The source ID
  1676. * @did: The destination ID
  1677. *
  1678. * Reset all EMs associated with a given local port. Release all
  1679. * sequences and exchanges. If sid is non-zero then reset only the
  1680. * exchanges sent from the local port's FID. If did is non-zero then
  1681. * reset only exchanges destined for the local port's FID.
  1682. */
  1683. void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
  1684. {
  1685. struct fc_exch_mgr_anchor *ema;
  1686. unsigned int cpu;
  1687. list_for_each_entry(ema, &lport->ema_list, ema_list) {
  1688. for_each_possible_cpu(cpu)
  1689. fc_exch_pool_reset(lport,
  1690. per_cpu_ptr(ema->mp->pool, cpu),
  1691. sid, did);
  1692. }
  1693. }
  1694. EXPORT_SYMBOL(fc_exch_mgr_reset);
  1695. /**
  1696. * fc_exch_lookup() - find an exchange
  1697. * @lport: The local port
  1698. * @xid: The exchange ID
  1699. *
  1700. * Returns exchange pointer with hold for caller, or NULL if not found.
  1701. */
  1702. static struct fc_exch *fc_exch_lookup(struct fc_lport *lport, u32 xid)
  1703. {
  1704. struct fc_exch_mgr_anchor *ema;
  1705. list_for_each_entry(ema, &lport->ema_list, ema_list)
  1706. if (ema->mp->min_xid <= xid && xid <= ema->mp->max_xid)
  1707. return fc_exch_find(ema->mp, xid);
  1708. return NULL;
  1709. }
  1710. /**
  1711. * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
  1712. * @rfp: The REC frame, not freed here.
  1713. *
  1714. * Note that the requesting port may be different than the S_ID in the request.
  1715. */
  1716. static void fc_exch_els_rec(struct fc_frame *rfp)
  1717. {
  1718. struct fc_lport *lport;
  1719. struct fc_frame *fp;
  1720. struct fc_exch *ep;
  1721. struct fc_els_rec *rp;
  1722. struct fc_els_rec_acc *acc;
  1723. enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
  1724. enum fc_els_rjt_explan explan;
  1725. u32 sid;
  1726. u16 rxid;
  1727. u16 oxid;
  1728. lport = fr_dev(rfp);
  1729. rp = fc_frame_payload_get(rfp, sizeof(*rp));
  1730. explan = ELS_EXPL_INV_LEN;
  1731. if (!rp)
  1732. goto reject;
  1733. sid = ntoh24(rp->rec_s_id);
  1734. rxid = ntohs(rp->rec_rx_id);
  1735. oxid = ntohs(rp->rec_ox_id);
  1736. ep = fc_exch_lookup(lport,
  1737. sid == fc_host_port_id(lport->host) ? oxid : rxid);
  1738. explan = ELS_EXPL_OXID_RXID;
  1739. if (!ep)
  1740. goto reject;
  1741. if (ep->oid != sid || oxid != ep->oxid)
  1742. goto rel;
  1743. if (rxid != FC_XID_UNKNOWN && rxid != ep->rxid)
  1744. goto rel;
  1745. fp = fc_frame_alloc(lport, sizeof(*acc));
  1746. if (!fp)
  1747. goto out;
  1748. acc = fc_frame_payload_get(fp, sizeof(*acc));
  1749. memset(acc, 0, sizeof(*acc));
  1750. acc->reca_cmd = ELS_LS_ACC;
  1751. acc->reca_ox_id = rp->rec_ox_id;
  1752. memcpy(acc->reca_ofid, rp->rec_s_id, 3);
  1753. acc->reca_rx_id = htons(ep->rxid);
  1754. if (ep->sid == ep->oid)
  1755. hton24(acc->reca_rfid, ep->did);
  1756. else
  1757. hton24(acc->reca_rfid, ep->sid);
  1758. acc->reca_fc4value = htonl(ep->seq.rec_data);
  1759. acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
  1760. ESB_ST_SEQ_INIT |
  1761. ESB_ST_COMPLETE));
  1762. fc_fill_reply_hdr(fp, rfp, FC_RCTL_ELS_REP, 0);
  1763. lport->tt.frame_send(lport, fp);
  1764. out:
  1765. fc_exch_release(ep);
  1766. return;
  1767. rel:
  1768. fc_exch_release(ep);
  1769. reject:
  1770. fc_seq_ls_rjt(rfp, reason, explan);
  1771. }
  1772. /**
  1773. * fc_exch_rrq_resp() - Handler for RRQ responses
  1774. * @sp: The sequence that the RRQ is on
  1775. * @fp: The RRQ frame
  1776. * @arg: The exchange that the RRQ is on
  1777. *
  1778. * TODO: fix error handler.
  1779. */
  1780. static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
  1781. {
  1782. struct fc_exch *aborted_ep = arg;
  1783. unsigned int op;
  1784. if (IS_ERR(fp)) {
  1785. int err = PTR_ERR(fp);
  1786. if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
  1787. goto cleanup;
  1788. FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
  1789. "frame error %d\n", err);
  1790. return;
  1791. }
  1792. op = fc_frame_payload_op(fp);
  1793. fc_frame_free(fp);
  1794. switch (op) {
  1795. case ELS_LS_RJT:
  1796. FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ\n");
  1797. /* fall through */
  1798. case ELS_LS_ACC:
  1799. goto cleanup;
  1800. default:
  1801. FC_EXCH_DBG(aborted_ep, "unexpected response op %x for RRQ\n",
  1802. op);
  1803. return;
  1804. }
  1805. cleanup:
  1806. fc_exch_done(&aborted_ep->seq);
  1807. /* drop hold for rec qual */
  1808. fc_exch_release(aborted_ep);
  1809. }
  1810. /**
  1811. * fc_exch_seq_send() - Send a frame using a new exchange and sequence
  1812. * @lport: The local port to send the frame on
  1813. * @fp: The frame to be sent
  1814. * @resp: The response handler for this request
  1815. * @destructor: The destructor for the exchange
  1816. * @arg: The argument to be passed to the response handler
  1817. * @timer_msec: The timeout period for the exchange
  1818. *
  1819. * The frame pointer with some of the header's fields must be
  1820. * filled before calling this routine, those fields are:
  1821. *
  1822. * - routing control
  1823. * - FC port did
  1824. * - FC port sid
  1825. * - FC header type
  1826. * - frame control
  1827. * - parameter or relative offset
  1828. */
  1829. static struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
  1830. struct fc_frame *fp,
  1831. void (*resp)(struct fc_seq *,
  1832. struct fc_frame *fp,
  1833. void *arg),
  1834. void (*destructor)(struct fc_seq *,
  1835. void *),
  1836. void *arg, u32 timer_msec)
  1837. {
  1838. struct fc_exch *ep;
  1839. struct fc_seq *sp = NULL;
  1840. struct fc_frame_header *fh;
  1841. struct fc_fcp_pkt *fsp = NULL;
  1842. int rc = 1;
  1843. ep = fc_exch_alloc(lport, fp);
  1844. if (!ep) {
  1845. fc_frame_free(fp);
  1846. return NULL;
  1847. }
  1848. ep->esb_stat |= ESB_ST_SEQ_INIT;
  1849. fh = fc_frame_header_get(fp);
  1850. fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
  1851. ep->resp = resp;
  1852. ep->destructor = destructor;
  1853. ep->arg = arg;
  1854. ep->r_a_tov = FC_DEF_R_A_TOV;
  1855. ep->lp = lport;
  1856. sp = &ep->seq;
  1857. ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
  1858. ep->f_ctl = ntoh24(fh->fh_f_ctl);
  1859. fc_exch_setup_hdr(ep, fp, ep->f_ctl);
  1860. sp->cnt++;
  1861. if (ep->xid <= lport->lro_xid && fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) {
  1862. fsp = fr_fsp(fp);
  1863. fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
  1864. }
  1865. if (unlikely(lport->tt.frame_send(lport, fp)))
  1866. goto err;
  1867. if (timer_msec)
  1868. fc_exch_timer_set_locked(ep, timer_msec);
  1869. ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
  1870. if (ep->f_ctl & FC_FC_SEQ_INIT)
  1871. ep->esb_stat &= ~ESB_ST_SEQ_INIT;
  1872. spin_unlock_bh(&ep->ex_lock);
  1873. return sp;
  1874. err:
  1875. if (fsp)
  1876. fc_fcp_ddp_done(fsp);
  1877. rc = fc_exch_done_locked(ep);
  1878. spin_unlock_bh(&ep->ex_lock);
  1879. if (!rc)
  1880. fc_exch_delete(ep);
  1881. return NULL;
  1882. }
  1883. /**
  1884. * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
  1885. * @ep: The exchange to send the RRQ on
  1886. *
  1887. * This tells the remote port to stop blocking the use of
  1888. * the exchange and the seq_cnt range.
  1889. */
  1890. static void fc_exch_rrq(struct fc_exch *ep)
  1891. {
  1892. struct fc_lport *lport;
  1893. struct fc_els_rrq *rrq;
  1894. struct fc_frame *fp;
  1895. u32 did;
  1896. lport = ep->lp;
  1897. fp = fc_frame_alloc(lport, sizeof(*rrq));
  1898. if (!fp)
  1899. goto retry;
  1900. rrq = fc_frame_payload_get(fp, sizeof(*rrq));
  1901. memset(rrq, 0, sizeof(*rrq));
  1902. rrq->rrq_cmd = ELS_RRQ;
  1903. hton24(rrq->rrq_s_id, ep->sid);
  1904. rrq->rrq_ox_id = htons(ep->oxid);
  1905. rrq->rrq_rx_id = htons(ep->rxid);
  1906. did = ep->did;
  1907. if (ep->esb_stat & ESB_ST_RESP)
  1908. did = ep->sid;
  1909. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
  1910. lport->port_id, FC_TYPE_ELS,
  1911. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1912. if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
  1913. lport->e_d_tov))
  1914. return;
  1915. retry:
  1916. spin_lock_bh(&ep->ex_lock);
  1917. if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
  1918. spin_unlock_bh(&ep->ex_lock);
  1919. /* drop hold for rec qual */
  1920. fc_exch_release(ep);
  1921. return;
  1922. }
  1923. ep->esb_stat |= ESB_ST_REC_QUAL;
  1924. fc_exch_timer_set_locked(ep, ep->r_a_tov);
  1925. spin_unlock_bh(&ep->ex_lock);
  1926. }
  1927. /**
  1928. * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
  1929. * @fp: The RRQ frame, not freed here.
  1930. */
  1931. static void fc_exch_els_rrq(struct fc_frame *fp)
  1932. {
  1933. struct fc_lport *lport;
  1934. struct fc_exch *ep = NULL; /* request or subject exchange */
  1935. struct fc_els_rrq *rp;
  1936. u32 sid;
  1937. u16 xid;
  1938. enum fc_els_rjt_explan explan;
  1939. lport = fr_dev(fp);
  1940. rp = fc_frame_payload_get(fp, sizeof(*rp));
  1941. explan = ELS_EXPL_INV_LEN;
  1942. if (!rp)
  1943. goto reject;
  1944. /*
  1945. * lookup subject exchange.
  1946. */
  1947. sid = ntoh24(rp->rrq_s_id); /* subject source */
  1948. xid = fc_host_port_id(lport->host) == sid ?
  1949. ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
  1950. ep = fc_exch_lookup(lport, xid);
  1951. explan = ELS_EXPL_OXID_RXID;
  1952. if (!ep)
  1953. goto reject;
  1954. spin_lock_bh(&ep->ex_lock);
  1955. if (ep->oxid != ntohs(rp->rrq_ox_id))
  1956. goto unlock_reject;
  1957. if (ep->rxid != ntohs(rp->rrq_rx_id) &&
  1958. ep->rxid != FC_XID_UNKNOWN)
  1959. goto unlock_reject;
  1960. explan = ELS_EXPL_SID;
  1961. if (ep->sid != sid)
  1962. goto unlock_reject;
  1963. /*
  1964. * Clear Recovery Qualifier state, and cancel timer if complete.
  1965. */
  1966. if (ep->esb_stat & ESB_ST_REC_QUAL) {
  1967. ep->esb_stat &= ~ESB_ST_REC_QUAL;
  1968. atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
  1969. }
  1970. if (ep->esb_stat & ESB_ST_COMPLETE)
  1971. fc_exch_timer_cancel(ep);
  1972. spin_unlock_bh(&ep->ex_lock);
  1973. /*
  1974. * Send LS_ACC.
  1975. */
  1976. fc_seq_ls_acc(fp);
  1977. goto out;
  1978. unlock_reject:
  1979. spin_unlock_bh(&ep->ex_lock);
  1980. reject:
  1981. fc_seq_ls_rjt(fp, ELS_RJT_LOGIC, explan);
  1982. out:
  1983. if (ep)
  1984. fc_exch_release(ep); /* drop hold from fc_exch_find */
  1985. }
  1986. /**
  1987. * fc_exch_update_stats() - update exches stats to lport
  1988. * @lport: The local port to update exchange manager stats
  1989. */
  1990. void fc_exch_update_stats(struct fc_lport *lport)
  1991. {
  1992. struct fc_host_statistics *st;
  1993. struct fc_exch_mgr_anchor *ema;
  1994. struct fc_exch_mgr *mp;
  1995. st = &lport->host_stats;
  1996. list_for_each_entry(ema, &lport->ema_list, ema_list) {
  1997. mp = ema->mp;
  1998. st->fc_no_free_exch += atomic_read(&mp->stats.no_free_exch);
  1999. st->fc_no_free_exch_xid +=
  2000. atomic_read(&mp->stats.no_free_exch_xid);
  2001. st->fc_xid_not_found += atomic_read(&mp->stats.xid_not_found);
  2002. st->fc_xid_busy += atomic_read(&mp->stats.xid_busy);
  2003. st->fc_seq_not_found += atomic_read(&mp->stats.seq_not_found);
  2004. st->fc_non_bls_resp += atomic_read(&mp->stats.non_bls_resp);
  2005. }
  2006. }
  2007. EXPORT_SYMBOL(fc_exch_update_stats);
  2008. /**
  2009. * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
  2010. * @lport: The local port to add the exchange manager to
  2011. * @mp: The exchange manager to be added to the local port
  2012. * @match: The match routine that indicates when this EM should be used
  2013. */
  2014. struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
  2015. struct fc_exch_mgr *mp,
  2016. bool (*match)(struct fc_frame *))
  2017. {
  2018. struct fc_exch_mgr_anchor *ema;
  2019. ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
  2020. if (!ema)
  2021. return ema;
  2022. ema->mp = mp;
  2023. ema->match = match;
  2024. /* add EM anchor to EM anchors list */
  2025. list_add_tail(&ema->ema_list, &lport->ema_list);
  2026. kref_get(&mp->kref);
  2027. return ema;
  2028. }
  2029. EXPORT_SYMBOL(fc_exch_mgr_add);
  2030. /**
  2031. * fc_exch_mgr_destroy() - Destroy an exchange manager
  2032. * @kref: The reference to the EM to be destroyed
  2033. */
  2034. static void fc_exch_mgr_destroy(struct kref *kref)
  2035. {
  2036. struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
  2037. mempool_destroy(mp->ep_pool);
  2038. free_percpu(mp->pool);
  2039. kfree(mp);
  2040. }
  2041. /**
  2042. * fc_exch_mgr_del() - Delete an EM from a local port's list
  2043. * @ema: The exchange manager anchor identifying the EM to be deleted
  2044. */
  2045. void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
  2046. {
  2047. /* remove EM anchor from EM anchors list */
  2048. list_del(&ema->ema_list);
  2049. kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
  2050. kfree(ema);
  2051. }
  2052. EXPORT_SYMBOL(fc_exch_mgr_del);
  2053. /**
  2054. * fc_exch_mgr_list_clone() - Share all exchange manager objects
  2055. * @src: Source lport to clone exchange managers from
  2056. * @dst: New lport that takes references to all the exchange managers
  2057. */
  2058. int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
  2059. {
  2060. struct fc_exch_mgr_anchor *ema, *tmp;
  2061. list_for_each_entry(ema, &src->ema_list, ema_list) {
  2062. if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
  2063. goto err;
  2064. }
  2065. return 0;
  2066. err:
  2067. list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
  2068. fc_exch_mgr_del(ema);
  2069. return -ENOMEM;
  2070. }
  2071. EXPORT_SYMBOL(fc_exch_mgr_list_clone);
  2072. /**
  2073. * fc_exch_mgr_alloc() - Allocate an exchange manager
  2074. * @lport: The local port that the new EM will be associated with
  2075. * @class: The default FC class for new exchanges
  2076. * @min_xid: The minimum XID for exchanges from the new EM
  2077. * @max_xid: The maximum XID for exchanges from the new EM
  2078. * @match: The match routine for the new EM
  2079. */
  2080. struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
  2081. enum fc_class class,
  2082. u16 min_xid, u16 max_xid,
  2083. bool (*match)(struct fc_frame *))
  2084. {
  2085. struct fc_exch_mgr *mp;
  2086. u16 pool_exch_range;
  2087. size_t pool_size;
  2088. unsigned int cpu;
  2089. struct fc_exch_pool *pool;
  2090. if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
  2091. (min_xid & fc_cpu_mask) != 0) {
  2092. FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
  2093. min_xid, max_xid);
  2094. return NULL;
  2095. }
  2096. /*
  2097. * allocate memory for EM
  2098. */
  2099. mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
  2100. if (!mp)
  2101. return NULL;
  2102. mp->class = class;
  2103. /* adjust em exch xid range for offload */
  2104. mp->min_xid = min_xid;
  2105. /* reduce range so per cpu pool fits into PCPU_MIN_UNIT_SIZE pool */
  2106. pool_exch_range = (PCPU_MIN_UNIT_SIZE - sizeof(*pool)) /
  2107. sizeof(struct fc_exch *);
  2108. if ((max_xid - min_xid + 1) / (fc_cpu_mask + 1) > pool_exch_range) {
  2109. mp->max_xid = pool_exch_range * (fc_cpu_mask + 1) +
  2110. min_xid - 1;
  2111. } else {
  2112. mp->max_xid = max_xid;
  2113. pool_exch_range = (mp->max_xid - mp->min_xid + 1) /
  2114. (fc_cpu_mask + 1);
  2115. }
  2116. mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
  2117. if (!mp->ep_pool)
  2118. goto free_mp;
  2119. /*
  2120. * Setup per cpu exch pool with entire exchange id range equally
  2121. * divided across all cpus. The exch pointers array memory is
  2122. * allocated for exch range per pool.
  2123. */
  2124. mp->pool_max_index = pool_exch_range - 1;
  2125. /*
  2126. * Allocate and initialize per cpu exch pool
  2127. */
  2128. pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
  2129. mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
  2130. if (!mp->pool)
  2131. goto free_mempool;
  2132. for_each_possible_cpu(cpu) {
  2133. pool = per_cpu_ptr(mp->pool, cpu);
  2134. pool->next_index = 0;
  2135. pool->left = FC_XID_UNKNOWN;
  2136. pool->right = FC_XID_UNKNOWN;
  2137. spin_lock_init(&pool->lock);
  2138. INIT_LIST_HEAD(&pool->ex_list);
  2139. }
  2140. kref_init(&mp->kref);
  2141. if (!fc_exch_mgr_add(lport, mp, match)) {
  2142. free_percpu(mp->pool);
  2143. goto free_mempool;
  2144. }
  2145. /*
  2146. * Above kref_init() sets mp->kref to 1 and then
  2147. * call to fc_exch_mgr_add incremented mp->kref again,
  2148. * so adjust that extra increment.
  2149. */
  2150. kref_put(&mp->kref, fc_exch_mgr_destroy);
  2151. return mp;
  2152. free_mempool:
  2153. mempool_destroy(mp->ep_pool);
  2154. free_mp:
  2155. kfree(mp);
  2156. return NULL;
  2157. }
  2158. EXPORT_SYMBOL(fc_exch_mgr_alloc);
  2159. /**
  2160. * fc_exch_mgr_free() - Free all exchange managers on a local port
  2161. * @lport: The local port whose EMs are to be freed
  2162. */
  2163. void fc_exch_mgr_free(struct fc_lport *lport)
  2164. {
  2165. struct fc_exch_mgr_anchor *ema, *next;
  2166. flush_workqueue(fc_exch_workqueue);
  2167. list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
  2168. fc_exch_mgr_del(ema);
  2169. }
  2170. EXPORT_SYMBOL(fc_exch_mgr_free);
  2171. /**
  2172. * fc_find_ema() - Lookup and return appropriate Exchange Manager Anchor depending
  2173. * upon 'xid'.
  2174. * @f_ctl: f_ctl
  2175. * @lport: The local port the frame was received on
  2176. * @fh: The received frame header
  2177. */
  2178. static struct fc_exch_mgr_anchor *fc_find_ema(u32 f_ctl,
  2179. struct fc_lport *lport,
  2180. struct fc_frame_header *fh)
  2181. {
  2182. struct fc_exch_mgr_anchor *ema;
  2183. u16 xid;
  2184. if (f_ctl & FC_FC_EX_CTX)
  2185. xid = ntohs(fh->fh_ox_id);
  2186. else {
  2187. xid = ntohs(fh->fh_rx_id);
  2188. if (xid == FC_XID_UNKNOWN)
  2189. return list_entry(lport->ema_list.prev,
  2190. typeof(*ema), ema_list);
  2191. }
  2192. list_for_each_entry(ema, &lport->ema_list, ema_list) {
  2193. if ((xid >= ema->mp->min_xid) &&
  2194. (xid <= ema->mp->max_xid))
  2195. return ema;
  2196. }
  2197. return NULL;
  2198. }
  2199. /**
  2200. * fc_exch_recv() - Handler for received frames
  2201. * @lport: The local port the frame was received on
  2202. * @fp: The received frame
  2203. */
  2204. void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
  2205. {
  2206. struct fc_frame_header *fh = fc_frame_header_get(fp);
  2207. struct fc_exch_mgr_anchor *ema;
  2208. u32 f_ctl;
  2209. /* lport lock ? */
  2210. if (!lport || lport->state == LPORT_ST_DISABLED) {
  2211. FC_LPORT_DBG(lport, "Receiving frames for an lport that "
  2212. "has not been initialized correctly\n");
  2213. fc_frame_free(fp);
  2214. return;
  2215. }
  2216. f_ctl = ntoh24(fh->fh_f_ctl);
  2217. ema = fc_find_ema(f_ctl, lport, fh);
  2218. if (!ema) {
  2219. FC_LPORT_DBG(lport, "Unable to find Exchange Manager Anchor,"
  2220. "fc_ctl <0x%x>, xid <0x%x>\n",
  2221. f_ctl,
  2222. (f_ctl & FC_FC_EX_CTX) ?
  2223. ntohs(fh->fh_ox_id) :
  2224. ntohs(fh->fh_rx_id));
  2225. fc_frame_free(fp);
  2226. return;
  2227. }
  2228. /*
  2229. * If frame is marked invalid, just drop it.
  2230. */
  2231. switch (fr_eof(fp)) {
  2232. case FC_EOF_T:
  2233. if (f_ctl & FC_FC_END_SEQ)
  2234. skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
  2235. /* fall through */
  2236. case FC_EOF_N:
  2237. if (fh->fh_type == FC_TYPE_BLS)
  2238. fc_exch_recv_bls(ema->mp, fp);
  2239. else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
  2240. FC_FC_EX_CTX)
  2241. fc_exch_recv_seq_resp(ema->mp, fp);
  2242. else if (f_ctl & FC_FC_SEQ_CTX)
  2243. fc_exch_recv_resp(ema->mp, fp);
  2244. else /* no EX_CTX and no SEQ_CTX */
  2245. fc_exch_recv_req(lport, ema->mp, fp);
  2246. break;
  2247. default:
  2248. FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
  2249. fr_eof(fp));
  2250. fc_frame_free(fp);
  2251. }
  2252. }
  2253. EXPORT_SYMBOL(fc_exch_recv);
  2254. /**
  2255. * fc_exch_init() - Initialize the exchange layer for a local port
  2256. * @lport: The local port to initialize the exchange layer for
  2257. */
  2258. int fc_exch_init(struct fc_lport *lport)
  2259. {
  2260. if (!lport->tt.seq_start_next)
  2261. lport->tt.seq_start_next = fc_seq_start_next;
  2262. if (!lport->tt.seq_set_resp)
  2263. lport->tt.seq_set_resp = fc_seq_set_resp;
  2264. if (!lport->tt.exch_seq_send)
  2265. lport->tt.exch_seq_send = fc_exch_seq_send;
  2266. if (!lport->tt.seq_send)
  2267. lport->tt.seq_send = fc_seq_send;
  2268. if (!lport->tt.seq_els_rsp_send)
  2269. lport->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
  2270. if (!lport->tt.exch_done)
  2271. lport->tt.exch_done = fc_exch_done;
  2272. if (!lport->tt.exch_mgr_reset)
  2273. lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
  2274. if (!lport->tt.seq_exch_abort)
  2275. lport->tt.seq_exch_abort = fc_seq_exch_abort;
  2276. if (!lport->tt.seq_assign)
  2277. lport->tt.seq_assign = fc_seq_assign;
  2278. if (!lport->tt.seq_release)
  2279. lport->tt.seq_release = fc_seq_release;
  2280. return 0;
  2281. }
  2282. EXPORT_SYMBOL(fc_exch_init);
  2283. /**
  2284. * fc_setup_exch_mgr() - Setup an exchange manager
  2285. */
  2286. int fc_setup_exch_mgr(void)
  2287. {
  2288. fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
  2289. 0, SLAB_HWCACHE_ALIGN, NULL);
  2290. if (!fc_em_cachep)
  2291. return -ENOMEM;
  2292. /*
  2293. * Initialize fc_cpu_mask and fc_cpu_order. The
  2294. * fc_cpu_mask is set for nr_cpu_ids rounded up
  2295. * to order of 2's * power and order is stored
  2296. * in fc_cpu_order as this is later required in
  2297. * mapping between an exch id and exch array index
  2298. * in per cpu exch pool.
  2299. *
  2300. * This round up is required to align fc_cpu_mask
  2301. * to exchange id's lower bits such that all incoming
  2302. * frames of an exchange gets delivered to the same
  2303. * cpu on which exchange originated by simple bitwise
  2304. * AND operation between fc_cpu_mask and exchange id.
  2305. */
  2306. fc_cpu_order = ilog2(roundup_pow_of_two(nr_cpu_ids));
  2307. fc_cpu_mask = (1 << fc_cpu_order) - 1;
  2308. fc_exch_workqueue = create_singlethread_workqueue("fc_exch_workqueue");
  2309. if (!fc_exch_workqueue)
  2310. goto err;
  2311. return 0;
  2312. err:
  2313. kmem_cache_destroy(fc_em_cachep);
  2314. return -ENOMEM;
  2315. }
  2316. /**
  2317. * fc_destroy_exch_mgr() - Destroy an exchange manager
  2318. */
  2319. void fc_destroy_exch_mgr(void)
  2320. {
  2321. destroy_workqueue(fc_exch_workqueue);
  2322. kmem_cache_destroy(fc_em_cachep);
  2323. }