fc_exch.c 61 KB

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