fc_exch.c 66 KB

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