fc_exch.c 52 KB

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