fc_exch.c 53 KB

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