fc_exch.c 61 KB

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