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
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.
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
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.
19 * Maintained at www.Open-FCoE.org
23 * Fibre Channel exchange and sequence handling.
26 #include <linux/timer.h>
27 #include <linux/gfp.h>
28 #include <linux/err.h>
30 #include <scsi/fc/fc_fc2.h>
32 #include <scsi/libfc.h>
33 #include <scsi/fc_encode.h>
35 static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
38 * Structure and function definitions for managing Fibre Channel Exchanges
41 * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
43 * fc_exch_mgr holds the exchange state for an N port
45 * fc_exch holds state for one exchange and links to its active sequence.
47 * fc_seq holds the state for an individual sequence.
53 * This structure is the center for creating exchanges and sequences.
54 * It manages the allocation of exchange IDs.
57 enum fc_class class; /* default class for sequences */
58 struct kref kref; /* exchange mgr reference count */
59 spinlock_t em_lock; /* exchange manager lock,
60 must be taken before ex_lock */
61 u16 next_xid; /* next possible free exchange ID */
62 u16 min_xid; /* min exchange ID */
63 u16 max_xid; /* max exchange ID */
64 u16 max_read; /* max exchange ID for read */
65 u16 last_read; /* last xid allocated for read */
66 u32 total_exches; /* total allocated exchanges */
67 struct list_head ex_list; /* allocated exchanges list */
68 mempool_t *ep_pool; /* reserve ep's */
71 * currently exchange mgr stats are updated but not used.
72 * either stats can be expose via sysfs or remove them
73 * all together if not used XXX
76 atomic_t no_free_exch;
77 atomic_t no_free_exch_xid;
78 atomic_t xid_not_found;
80 atomic_t seq_not_found;
81 atomic_t non_bls_resp;
83 struct fc_exch **exches; /* for exch pointers indexed by xid */
85 #define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
87 struct fc_exch_mgr_anchor {
88 struct list_head ema_list;
89 struct fc_exch_mgr *mp;
90 bool (*match)(struct fc_frame *);
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 static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp);
102 * Internal implementation notes.
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.
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.
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.
119 * Notes on reference counts:
121 * Exchanges are reference counted and exchange gets freed when the reference
122 * count becomes zero.
125 * Sequences are timed out for E_D_TOV and R_A_TOV.
127 * Sequence event handling:
129 * The following events may occur on initiator sequences:
132 * For now, the whole thing is sent.
134 * This applies only to class F.
135 * The sequence is marked complete.
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).
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.
151 * The following events may occur on recipient sequences:
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.
159 * Deallocate sequence
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.
170 * The EM code run in a per-CPU worker thread.
172 * To protect against concurrency between a worker thread code and timers,
173 * sequence allocation and deallocation must be locked.
174 * - exchange refcnt can be done atomicly without locks.
175 * - sequence allocation must be locked by exch lock.
176 * - If the em_lock and ex_lock must be taken at the same time, then the
177 * em_lock must be taken before the ex_lock.
181 * opcode names for debugging.
183 static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
185 #define FC_TABLE_SIZE(x) (sizeof(x) / sizeof(x[0]))
187 static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
188 unsigned int max_index)
190 const char *name = NULL;
199 static const char *fc_exch_rctl_name(unsigned int op)
201 return fc_exch_name_lookup(op, fc_exch_rctl_names,
202 FC_TABLE_SIZE(fc_exch_rctl_names));
206 * Hold an exchange - keep it from being freed.
208 static void fc_exch_hold(struct fc_exch *ep)
210 atomic_inc(&ep->ex_refcnt);
214 * setup fc hdr by initializing few more FC header fields and sof/eof.
215 * Initialized fields by this func:
216 * - fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt
219 static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
222 struct fc_frame_header *fh = fc_frame_header_get(fp);
225 fr_sof(fp) = ep->class;
227 fr_sof(fp) = fc_sof_normal(ep->class);
229 if (f_ctl & FC_FC_END_SEQ) {
230 fr_eof(fp) = FC_EOF_T;
231 if (fc_sof_needs_ack(ep->class))
232 fr_eof(fp) = FC_EOF_N;
235 * The number of fill bytes to make the length a 4-byte
236 * multiple is the low order 2-bits of the f_ctl.
237 * The fill itself will have been cleared by the frame
239 * After this, the length will be even, as expected by
242 fill = fr_len(fp) & 3;
245 /* TODO, this may be a problem with fragmented skb */
246 skb_put(fp_skb(fp), fill);
247 hton24(fh->fh_f_ctl, f_ctl | fill);
250 WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
251 fr_eof(fp) = FC_EOF_N;
255 * Initialize remainig fh fields
256 * from fc_fill_fc_hdr
258 fh->fh_ox_id = htons(ep->oxid);
259 fh->fh_rx_id = htons(ep->rxid);
260 fh->fh_seq_id = ep->seq.id;
261 fh->fh_seq_cnt = htons(ep->seq.cnt);
266 * Release a reference to an exchange.
267 * If the refcnt goes to zero and the exchange is complete, it is freed.
269 static void fc_exch_release(struct fc_exch *ep)
271 struct fc_exch_mgr *mp;
273 if (atomic_dec_and_test(&ep->ex_refcnt)) {
276 ep->destructor(&ep->seq, ep->arg);
277 WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
278 mempool_free(ep, mp->ep_pool);
282 static int fc_exch_done_locked(struct fc_exch *ep)
287 * We must check for completion in case there are two threads
288 * tyring to complete this. But the rrq code will reuse the
289 * ep, and in that case we only clear the resp and set it as
290 * complete, so it can be reused by the timer to send the rrq.
293 if (ep->state & FC_EX_DONE)
295 ep->esb_stat |= ESB_ST_COMPLETE;
297 if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
298 ep->state |= FC_EX_DONE;
299 if (cancel_delayed_work(&ep->timeout_work))
300 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
306 static void fc_exch_mgr_delete_ep(struct fc_exch *ep)
308 struct fc_exch_mgr *mp;
311 spin_lock_bh(&mp->em_lock);
312 WARN_ON(mp->total_exches <= 0);
314 mp->exches[ep->xid - mp->min_xid] = NULL;
315 list_del(&ep->ex_list);
316 spin_unlock_bh(&mp->em_lock);
317 fc_exch_release(ep); /* drop hold for exch in mp */
321 * Internal version of fc_exch_timer_set - used with lock held.
323 static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
324 unsigned int timer_msec)
326 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
329 FC_EXCH_DBG(ep, "Exchange timer armed\n");
331 if (schedule_delayed_work(&ep->timeout_work,
332 msecs_to_jiffies(timer_msec)))
333 fc_exch_hold(ep); /* hold for timer */
337 * Set timer for an exchange.
338 * The time is a minimum delay in milliseconds until the timer fires.
339 * Used for upper level protocols to time out the exchange.
340 * The timer is cancelled when it fires or when the exchange completes.
341 * Returns non-zero if a timer couldn't be allocated.
343 static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
345 spin_lock_bh(&ep->ex_lock);
346 fc_exch_timer_set_locked(ep, timer_msec);
347 spin_unlock_bh(&ep->ex_lock);
350 int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec)
357 ep = fc_seq_exch(req_sp);
359 spin_lock_bh(&ep->ex_lock);
360 if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
361 ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
362 spin_unlock_bh(&ep->ex_lock);
367 * Send the abort on a new sequence if possible.
369 sp = fc_seq_start_next_locked(&ep->seq);
371 spin_unlock_bh(&ep->ex_lock);
375 ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
377 fc_exch_timer_set_locked(ep, timer_msec);
378 spin_unlock_bh(&ep->ex_lock);
381 * If not logged into the fabric, don't send ABTS but leave
382 * sequence active until next timeout.
388 * Send an abort for the sequence that timed out.
390 fp = fc_frame_alloc(ep->lp, 0);
392 fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
393 FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
394 error = fc_seq_send(ep->lp, sp, fp);
399 EXPORT_SYMBOL(fc_seq_exch_abort);
402 * Exchange timeout - handle exchange timer expiration.
403 * The timer will have been cancelled before this is called.
405 static void fc_exch_timeout(struct work_struct *work)
407 struct fc_exch *ep = container_of(work, struct fc_exch,
409 struct fc_seq *sp = &ep->seq;
410 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
415 FC_EXCH_DBG(ep, "Exchange timed out\n");
417 spin_lock_bh(&ep->ex_lock);
418 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
421 e_stat = ep->esb_stat;
422 if (e_stat & ESB_ST_COMPLETE) {
423 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
424 spin_unlock_bh(&ep->ex_lock);
425 if (e_stat & ESB_ST_REC_QUAL)
432 if (e_stat & ESB_ST_ABNORMAL)
433 rc = fc_exch_done_locked(ep);
434 spin_unlock_bh(&ep->ex_lock);
436 fc_exch_mgr_delete_ep(ep);
438 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
439 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
443 spin_unlock_bh(&ep->ex_lock);
446 * This release matches the hold taken when the timer was set.
452 * Allocate a sequence.
454 * We don't support multiple originated sequences on the same exchange.
455 * By implication, any previously originated sequence on this exchange
456 * is complete, and we reallocate the same sequence.
458 static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
470 * fc_exch_em_alloc() - allocate an exchange from a specified EM.
471 * @lport: ptr to the local port
472 * @mp: ptr to the exchange manager
474 * Returns pointer to allocated fc_exch with exch lock held.
476 static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
477 struct fc_exch_mgr *mp)
485 /* allocate memory for exchange */
486 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
488 atomic_inc(&mp->stats.no_free_exch);
491 memset(ep, 0, sizeof(*ep));
493 spin_lock_bh(&mp->em_lock);
495 /* alloc a new xid */
496 while (mp->exches[xid - min]) {
497 xid = (xid == max) ? min : xid + 1;
498 if (xid == mp->next_xid)
501 mp->next_xid = (xid == max) ? min : xid + 1;
503 fc_exch_hold(ep); /* hold for exch in mp */
504 spin_lock_init(&ep->ex_lock);
506 * Hold exch lock for caller to prevent fc_exch_reset()
507 * from releasing exch while fc_exch_alloc() caller is
508 * still working on exch.
510 spin_lock_bh(&ep->ex_lock);
512 mp->exches[xid - mp->min_xid] = ep;
513 list_add_tail(&ep->ex_list, &mp->ex_list);
514 fc_seq_alloc(ep, ep->seq_id++);
516 spin_unlock_bh(&mp->em_lock);
521 ep->oxid = ep->xid = xid;
524 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
525 ep->rxid = FC_XID_UNKNOWN;
526 ep->class = mp->class;
527 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
531 spin_unlock_bh(&mp->em_lock);
532 atomic_inc(&mp->stats.no_free_exch_xid);
533 mempool_free(ep, mp->ep_pool);
538 * fc_exch_alloc() - allocate an exchange.
539 * @lport: ptr to the local port
540 * @fp: ptr to the FC frame
542 * This function walks the list of the exchange manager(EM)
543 * anchors to select a EM for new exchange allocation. The
544 * EM is selected having either a NULL match function pointer
545 * or call to match function returning true.
547 struct fc_exch *fc_exch_alloc(struct fc_lport *lport, struct fc_frame *fp)
549 struct fc_exch_mgr_anchor *ema;
552 list_for_each_entry(ema, &lport->ema_list, ema_list) {
553 if (!ema->match || ema->match(fp)) {
554 ep = fc_exch_em_alloc(lport, ema->mp);
561 EXPORT_SYMBOL(fc_exch_alloc);
564 * Lookup and hold an exchange.
566 static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
568 struct fc_exch *ep = NULL;
570 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
571 spin_lock_bh(&mp->em_lock);
572 ep = mp->exches[xid - mp->min_xid];
575 WARN_ON(ep->xid != xid);
577 spin_unlock_bh(&mp->em_lock);
582 void fc_exch_done(struct fc_seq *sp)
584 struct fc_exch *ep = fc_seq_exch(sp);
587 spin_lock_bh(&ep->ex_lock);
588 rc = fc_exch_done_locked(ep);
589 spin_unlock_bh(&ep->ex_lock);
591 fc_exch_mgr_delete_ep(ep);
593 EXPORT_SYMBOL(fc_exch_done);
596 * Allocate a new exchange as responder.
597 * Sets the responder ID in the frame header.
599 static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
600 struct fc_exch_mgr *mp,
604 struct fc_frame_header *fh;
606 ep = fc_exch_alloc(lport, fp);
608 ep->class = fc_frame_class(fp);
611 * Set EX_CTX indicating we're responding on this exchange.
613 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
614 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
615 fh = fc_frame_header_get(fp);
616 ep->sid = ntoh24(fh->fh_d_id);
617 ep->did = ntoh24(fh->fh_s_id);
621 * Allocated exchange has placed the XID in the
622 * originator field. Move it to the responder field,
623 * and set the originator XID from the frame.
626 ep->oxid = ntohs(fh->fh_ox_id);
627 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
628 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
629 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
631 fc_exch_hold(ep); /* hold for caller */
632 spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */
638 * Find a sequence for receive where the other end is originating the sequence.
639 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
640 * on the ep that should be released by the caller.
642 static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
643 struct fc_exch_mgr *mp,
646 struct fc_frame_header *fh = fc_frame_header_get(fp);
647 struct fc_exch *ep = NULL;
648 struct fc_seq *sp = NULL;
649 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
653 f_ctl = ntoh24(fh->fh_f_ctl);
654 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
657 * Lookup or create the exchange if we will be creating the sequence.
659 if (f_ctl & FC_FC_EX_CTX) {
660 xid = ntohs(fh->fh_ox_id); /* we originated exch */
661 ep = fc_exch_find(mp, xid);
663 atomic_inc(&mp->stats.xid_not_found);
664 reject = FC_RJT_OX_ID;
667 if (ep->rxid == FC_XID_UNKNOWN)
668 ep->rxid = ntohs(fh->fh_rx_id);
669 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
670 reject = FC_RJT_OX_ID;
674 xid = ntohs(fh->fh_rx_id); /* we are the responder */
677 * Special case for MDS issuing an ELS TEST with a
679 * XXX take this out once we do the proper reject.
681 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
682 fc_frame_payload_op(fp) == ELS_TEST) {
683 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
684 xid = FC_XID_UNKNOWN;
688 * new sequence - find the exchange
690 ep = fc_exch_find(mp, xid);
691 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
693 atomic_inc(&mp->stats.xid_busy);
694 reject = FC_RJT_RX_ID;
697 ep = fc_exch_resp(lport, mp, fp);
699 reject = FC_RJT_EXCH_EST; /* XXX */
702 xid = ep->xid; /* get our XID */
704 atomic_inc(&mp->stats.xid_not_found);
705 reject = FC_RJT_RX_ID; /* XID not found */
711 * At this point, we have the exchange held.
712 * Find or create the sequence.
714 if (fc_sof_is_init(fr_sof(fp))) {
715 sp = fc_seq_start_next(&ep->seq);
717 reject = FC_RJT_SEQ_XS; /* exchange shortage */
720 sp->id = fh->fh_seq_id;
721 sp->ssb_stat |= SSB_ST_RESP;
724 if (sp->id != fh->fh_seq_id) {
725 atomic_inc(&mp->stats.seq_not_found);
726 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
730 WARN_ON(ep != fc_seq_exch(sp));
732 if (f_ctl & FC_FC_SEQ_INIT)
733 ep->esb_stat |= ESB_ST_SEQ_INIT;
739 fc_exch_done(&ep->seq);
740 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
745 * Find the sequence for a frame being received.
746 * We originated the sequence, so it should be found.
747 * We may or may not have originated the exchange.
748 * Does not hold the sequence for the caller.
750 static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
753 struct fc_frame_header *fh = fc_frame_header_get(fp);
755 struct fc_seq *sp = NULL;
759 f_ctl = ntoh24(fh->fh_f_ctl);
760 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
761 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
762 ep = fc_exch_find(mp, xid);
765 if (ep->seq.id == fh->fh_seq_id) {
767 * Save the RX_ID if we didn't previously know it.
770 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
771 ep->rxid == FC_XID_UNKNOWN) {
772 ep->rxid = ntohs(fh->fh_rx_id);
780 * Set addresses for an exchange.
781 * Note this must be done before the first sequence of the exchange is sent.
783 static void fc_exch_set_addr(struct fc_exch *ep,
784 u32 orig_id, u32 resp_id)
787 if (ep->esb_stat & ESB_ST_RESP) {
796 static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
798 struct fc_exch *ep = fc_seq_exch(sp);
800 sp = fc_seq_alloc(ep, ep->seq_id++);
801 FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
806 * Allocate a new sequence on the same exchange as the supplied sequence.
807 * This will never return NULL.
809 struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
811 struct fc_exch *ep = fc_seq_exch(sp);
813 spin_lock_bh(&ep->ex_lock);
814 sp = fc_seq_start_next_locked(sp);
815 spin_unlock_bh(&ep->ex_lock);
819 EXPORT_SYMBOL(fc_seq_start_next);
821 int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
824 struct fc_frame_header *fh = fc_frame_header_get(fp);
828 ep = fc_seq_exch(sp);
829 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
831 f_ctl = ntoh24(fh->fh_f_ctl);
832 fc_exch_setup_hdr(ep, fp, f_ctl);
835 * update sequence count if this frame is carrying
836 * multiple FC frames when sequence offload is enabled
839 if (fr_max_payload(fp))
840 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
848 error = lp->tt.frame_send(lp, fp);
851 * Update the exchange and sequence flags,
852 * assuming all frames for the sequence have been sent.
853 * We can only be called to send once for each sequence.
855 spin_lock_bh(&ep->ex_lock);
856 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
857 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
858 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
859 spin_unlock_bh(&ep->ex_lock);
862 EXPORT_SYMBOL(fc_seq_send);
864 void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
865 struct fc_seq_els_data *els_data)
869 fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
875 fc_exch_els_rrq(sp, els_data->fp);
878 fc_exch_els_rec(sp, els_data->fp);
881 FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
884 EXPORT_SYMBOL(fc_seq_els_rsp_send);
887 * Send a sequence, which is also the last sequence in the exchange.
889 static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
890 enum fc_rctl rctl, enum fc_fh_type fh_type)
893 struct fc_exch *ep = fc_seq_exch(sp);
895 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
897 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
898 fc_seq_send(ep->lp, sp, fp);
902 * Send ACK_1 (or equiv.) indicating we received something.
903 * The frame we're acking is supplied.
905 static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
908 struct fc_frame_header *rx_fh;
909 struct fc_frame_header *fh;
910 struct fc_exch *ep = fc_seq_exch(sp);
911 struct fc_lport *lp = ep->lp;
915 * Don't send ACKs for class 3.
917 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
918 fp = fc_frame_alloc(lp, 0);
922 fh = fc_frame_header_get(fp);
923 fh->fh_r_ctl = FC_RCTL_ACK_1;
924 fh->fh_type = FC_TYPE_BLS;
927 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
928 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
929 * Bits 9-8 are meaningful (retransmitted or unidirectional).
930 * Last ACK uses bits 7-6 (continue sequence),
931 * bits 5-4 are meaningful (what kind of ACK to use).
933 rx_fh = fc_frame_header_get(rx_fp);
934 f_ctl = ntoh24(rx_fh->fh_f_ctl);
935 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
936 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
937 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
938 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
939 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
940 hton24(fh->fh_f_ctl, f_ctl);
942 fc_exch_setup_hdr(ep, fp, f_ctl);
943 fh->fh_seq_id = rx_fh->fh_seq_id;
944 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
945 fh->fh_parm_offset = htonl(1); /* ack single frame */
947 fr_sof(fp) = fr_sof(rx_fp);
948 if (f_ctl & FC_FC_END_SEQ)
949 fr_eof(fp) = FC_EOF_T;
951 fr_eof(fp) = FC_EOF_N;
953 (void) lp->tt.frame_send(lp, fp);
959 * This is for rejecting BA_ABTS only.
961 static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
962 enum fc_ba_rjt_reason reason,
963 enum fc_ba_rjt_explan explan)
966 struct fc_frame_header *rx_fh;
967 struct fc_frame_header *fh;
968 struct fc_ba_rjt *rp;
973 fp = fc_frame_alloc(lp, sizeof(*rp));
976 fh = fc_frame_header_get(fp);
977 rx_fh = fc_frame_header_get(rx_fp);
979 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
981 rp = fc_frame_payload_get(fp, sizeof(*rp));
982 rp->br_reason = reason;
983 rp->br_explan = explan;
986 * seq_id, cs_ctl, df_ctl and param/offset are zero.
988 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
989 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
990 fh->fh_ox_id = rx_fh->fh_rx_id;
991 fh->fh_rx_id = rx_fh->fh_ox_id;
992 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
993 fh->fh_r_ctl = FC_RCTL_BA_RJT;
994 fh->fh_type = FC_TYPE_BLS;
997 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
998 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
999 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1000 * Last ACK uses bits 7-6 (continue sequence),
1001 * bits 5-4 are meaningful (what kind of ACK to use).
1002 * Always set LAST_SEQ, END_SEQ.
1004 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1005 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1006 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1007 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1008 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1009 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1010 f_ctl &= ~FC_FC_FIRST_SEQ;
1011 hton24(fh->fh_f_ctl, f_ctl);
1013 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1014 fr_eof(fp) = FC_EOF_T;
1015 if (fc_sof_needs_ack(fr_sof(fp)))
1016 fr_eof(fp) = FC_EOF_N;
1018 (void) lp->tt.frame_send(lp, fp);
1022 * Handle an incoming ABTS. This would be for target mode usually,
1023 * but could be due to lost FCP transfer ready, confirm or RRQ.
1024 * We always handle this as an exchange abort, ignoring the parameter.
1026 static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1028 struct fc_frame *fp;
1029 struct fc_ba_acc *ap;
1030 struct fc_frame_header *fh;
1035 spin_lock_bh(&ep->ex_lock);
1036 if (ep->esb_stat & ESB_ST_COMPLETE) {
1037 spin_unlock_bh(&ep->ex_lock);
1040 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1041 fc_exch_hold(ep); /* hold for REC_QUAL */
1042 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1043 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1045 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1047 spin_unlock_bh(&ep->ex_lock);
1050 fh = fc_frame_header_get(fp);
1051 ap = fc_frame_payload_get(fp, sizeof(*ap));
1052 memset(ap, 0, sizeof(*ap));
1054 ap->ba_high_seq_cnt = htons(0xffff);
1055 if (sp->ssb_stat & SSB_ST_RESP) {
1056 ap->ba_seq_id = sp->id;
1057 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1058 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1059 ap->ba_low_seq_cnt = htons(sp->cnt);
1061 sp = fc_seq_start_next_locked(sp);
1062 spin_unlock_bh(&ep->ex_lock);
1063 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1064 fc_frame_free(rx_fp);
1068 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1070 fc_frame_free(rx_fp);
1074 * Handle receive where the other end is originating the sequence.
1076 static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1077 struct fc_frame *fp)
1079 struct fc_frame_header *fh = fc_frame_header_get(fp);
1080 struct fc_seq *sp = NULL;
1081 struct fc_exch *ep = NULL;
1085 enum fc_pf_rjt_reason reject;
1088 reject = fc_seq_lookup_recip(lp, mp, fp);
1089 if (reject == FC_RJT_NONE) {
1090 sp = fr_seq(fp); /* sequence will be held */
1091 ep = fc_seq_exch(sp);
1094 f_ctl = ntoh24(fh->fh_f_ctl);
1095 fc_seq_send_ack(sp, fp);
1098 * Call the receive function.
1100 * The receive function may allocate a new sequence
1101 * over the old one, so we shouldn't change the
1102 * sequence after this.
1104 * The frame will be freed by the receive function.
1105 * If new exch resp handler is valid then call that
1109 ep->resp(sp, fp, ep->arg);
1111 lp->tt.lport_recv(lp, sp, fp);
1112 fc_exch_release(ep); /* release from lookup */
1114 FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject);
1120 * Handle receive where the other end is originating the sequence in
1121 * response to our exchange.
1123 static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1125 struct fc_frame_header *fh = fc_frame_header_get(fp);
1130 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1134 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1136 atomic_inc(&mp->stats.xid_not_found);
1139 if (ep->esb_stat & ESB_ST_COMPLETE) {
1140 atomic_inc(&mp->stats.xid_not_found);
1143 if (ep->rxid == FC_XID_UNKNOWN)
1144 ep->rxid = ntohs(fh->fh_rx_id);
1145 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1146 atomic_inc(&mp->stats.xid_not_found);
1149 if (ep->did != ntoh24(fh->fh_s_id) &&
1150 ep->did != FC_FID_FLOGI) {
1151 atomic_inc(&mp->stats.xid_not_found);
1155 if (fc_sof_is_init(sof)) {
1156 sp = fc_seq_start_next(&ep->seq);
1157 sp->id = fh->fh_seq_id;
1158 sp->ssb_stat |= SSB_ST_RESP;
1161 if (sp->id != fh->fh_seq_id) {
1162 atomic_inc(&mp->stats.seq_not_found);
1166 f_ctl = ntoh24(fh->fh_f_ctl);
1168 if (f_ctl & FC_FC_SEQ_INIT)
1169 ep->esb_stat |= ESB_ST_SEQ_INIT;
1171 if (fc_sof_needs_ack(sof))
1172 fc_seq_send_ack(sp, fp);
1174 ex_resp_arg = ep->arg;
1176 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1177 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1178 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1179 spin_lock_bh(&ep->ex_lock);
1180 rc = fc_exch_done_locked(ep);
1181 WARN_ON(fc_seq_exch(sp) != ep);
1182 spin_unlock_bh(&ep->ex_lock);
1184 fc_exch_mgr_delete_ep(ep);
1188 * Call the receive function.
1189 * The sequence is held (has a refcnt) for us,
1190 * but not for the receive function.
1192 * The receive function may allocate a new sequence
1193 * over the old one, so we shouldn't change the
1194 * sequence after this.
1196 * The frame will be freed by the receive function.
1197 * If new exch resp handler is valid then call that
1201 resp(sp, fp, ex_resp_arg);
1204 fc_exch_release(ep);
1207 fc_exch_release(ep);
1213 * Handle receive for a sequence where other end is responding to our sequence.
1215 static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1219 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
1222 atomic_inc(&mp->stats.xid_not_found);
1224 atomic_inc(&mp->stats.non_bls_resp);
1230 * Handle the response to an ABTS for exchange or sequence.
1231 * This can be BA_ACC or BA_RJT.
1233 static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1235 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1237 struct fc_frame_header *fh;
1238 struct fc_ba_acc *ap;
1242 int rc = 1, has_rec = 0;
1244 fh = fc_frame_header_get(fp);
1245 FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
1246 fc_exch_rctl_name(fh->fh_r_ctl));
1248 if (cancel_delayed_work_sync(&ep->timeout_work))
1249 fc_exch_release(ep); /* release from pending timer hold */
1251 spin_lock_bh(&ep->ex_lock);
1252 switch (fh->fh_r_ctl) {
1253 case FC_RCTL_BA_ACC:
1254 ap = fc_frame_payload_get(fp, sizeof(*ap));
1259 * Decide whether to establish a Recovery Qualifier.
1260 * We do this if there is a non-empty SEQ_CNT range and
1261 * SEQ_ID is the same as the one we aborted.
1263 low = ntohs(ap->ba_low_seq_cnt);
1264 high = ntohs(ap->ba_high_seq_cnt);
1265 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1266 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1267 ap->ba_seq_id == ep->seq_id) && low != high) {
1268 ep->esb_stat |= ESB_ST_REC_QUAL;
1269 fc_exch_hold(ep); /* hold for recovery qualifier */
1273 case FC_RCTL_BA_RJT:
1280 ex_resp_arg = ep->arg;
1282 /* do we need to do some other checks here. Can we reuse more of
1283 * fc_exch_recv_seq_resp
1287 * do we want to check END_SEQ as well as LAST_SEQ here?
1289 if (ep->fh_type != FC_TYPE_FCP &&
1290 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1291 rc = fc_exch_done_locked(ep);
1292 spin_unlock_bh(&ep->ex_lock);
1294 fc_exch_mgr_delete_ep(ep);
1297 resp(sp, fp, ex_resp_arg);
1302 fc_exch_timer_set(ep, ep->r_a_tov);
1307 * Receive BLS sequence.
1308 * This is always a sequence initiated by the remote side.
1309 * We may be either the originator or recipient of the exchange.
1311 static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1313 struct fc_frame_header *fh;
1317 fh = fc_frame_header_get(fp);
1318 f_ctl = ntoh24(fh->fh_f_ctl);
1321 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1322 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1323 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1324 spin_lock_bh(&ep->ex_lock);
1325 ep->esb_stat |= ESB_ST_SEQ_INIT;
1326 spin_unlock_bh(&ep->ex_lock);
1328 if (f_ctl & FC_FC_SEQ_CTX) {
1330 * A response to a sequence we initiated.
1331 * This should only be ACKs for class 2 or F.
1333 switch (fh->fh_r_ctl) {
1338 FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
1340 fc_exch_rctl_name(fh->fh_r_ctl));
1345 switch (fh->fh_r_ctl) {
1346 case FC_RCTL_BA_RJT:
1347 case FC_RCTL_BA_ACC:
1349 fc_exch_abts_resp(ep, fp);
1353 case FC_RCTL_BA_ABTS:
1354 fc_exch_recv_abts(ep, fp);
1356 default: /* ignore junk */
1362 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1366 * Accept sequence with LS_ACC.
1367 * If this fails due to allocation or transmit congestion, assume the
1368 * originator will repeat the sequence.
1370 static void fc_seq_ls_acc(struct fc_seq *req_sp)
1373 struct fc_els_ls_acc *acc;
1374 struct fc_frame *fp;
1376 sp = fc_seq_start_next(req_sp);
1377 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1379 acc = fc_frame_payload_get(fp, sizeof(*acc));
1380 memset(acc, 0, sizeof(*acc));
1381 acc->la_cmd = ELS_LS_ACC;
1382 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1387 * Reject sequence with ELS LS_RJT.
1388 * If this fails due to allocation or transmit congestion, assume the
1389 * originator will repeat the sequence.
1391 static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
1392 enum fc_els_rjt_explan explan)
1395 struct fc_els_ls_rjt *rjt;
1396 struct fc_frame *fp;
1398 sp = fc_seq_start_next(req_sp);
1399 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
1401 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1402 memset(rjt, 0, sizeof(*rjt));
1403 rjt->er_cmd = ELS_LS_RJT;
1404 rjt->er_reason = reason;
1405 rjt->er_explan = explan;
1406 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1410 static void fc_exch_reset(struct fc_exch *ep)
1413 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1417 spin_lock_bh(&ep->ex_lock);
1418 ep->state |= FC_EX_RST_CLEANUP;
1420 * we really want to call del_timer_sync, but cannot due
1421 * to the lport calling with the lport lock held (some resp
1422 * functions can also grab the lport lock which could cause
1425 if (cancel_delayed_work(&ep->timeout_work))
1426 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1429 if (ep->esb_stat & ESB_ST_REC_QUAL)
1430 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1431 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1434 rc = fc_exch_done_locked(ep);
1435 spin_unlock_bh(&ep->ex_lock);
1437 fc_exch_mgr_delete_ep(ep);
1440 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1444 * Reset an exchange manager, releasing all sequences and exchanges.
1445 * If sid is non-zero, reset only exchanges we source from that FID.
1446 * If did is non-zero, reset only exchanges destined to that FID.
1448 void fc_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
1451 struct fc_exch *next;
1452 struct fc_exch_mgr *mp;
1453 struct fc_exch_mgr_anchor *ema;
1455 list_for_each_entry(ema, &lp->ema_list, ema_list) {
1457 spin_lock_bh(&mp->em_lock);
1459 list_for_each_entry_safe(ep, next, &mp->ex_list, ex_list) {
1460 if ((lp == ep->lp) &&
1461 (sid == 0 || sid == ep->sid) &&
1462 (did == 0 || did == ep->did)) {
1464 spin_unlock_bh(&mp->em_lock);
1468 fc_exch_release(ep);
1469 spin_lock_bh(&mp->em_lock);
1472 * must restart loop incase while lock
1473 * was down multiple eps were released.
1478 spin_unlock_bh(&mp->em_lock);
1481 EXPORT_SYMBOL(fc_exch_mgr_reset);
1484 * Handle incoming ELS REC - Read Exchange Concise.
1485 * Note that the requesting port may be different than the S_ID in the request.
1487 static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
1489 struct fc_frame *fp;
1491 struct fc_exch_mgr *em;
1492 struct fc_els_rec *rp;
1493 struct fc_els_rec_acc *acc;
1494 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1495 enum fc_els_rjt_explan explan;
1500 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1501 explan = ELS_EXPL_INV_LEN;
1504 sid = ntoh24(rp->rec_s_id);
1505 rxid = ntohs(rp->rec_rx_id);
1506 oxid = ntohs(rp->rec_ox_id);
1509 * Currently it's hard to find the local S_ID from the exchange
1510 * manager. This will eventually be fixed, but for now it's easier
1511 * to lookup the subject exchange twice, once as if we were
1512 * the initiator, and then again if we weren't.
1514 em = fc_seq_exch(sp)->em;
1515 ep = fc_exch_find(em, oxid);
1516 explan = ELS_EXPL_OXID_RXID;
1517 if (ep && ep->oid == sid) {
1518 if (ep->rxid != FC_XID_UNKNOWN &&
1519 rxid != FC_XID_UNKNOWN &&
1524 fc_exch_release(ep);
1526 if (rxid != FC_XID_UNKNOWN)
1527 ep = fc_exch_find(em, rxid);
1532 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1537 sp = fc_seq_start_next(sp);
1538 acc = fc_frame_payload_get(fp, sizeof(*acc));
1539 memset(acc, 0, sizeof(*acc));
1540 acc->reca_cmd = ELS_LS_ACC;
1541 acc->reca_ox_id = rp->rec_ox_id;
1542 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1543 acc->reca_rx_id = htons(ep->rxid);
1544 if (ep->sid == ep->oid)
1545 hton24(acc->reca_rfid, ep->did);
1547 hton24(acc->reca_rfid, ep->sid);
1548 acc->reca_fc4value = htonl(ep->seq.rec_data);
1549 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1552 sp = fc_seq_start_next(sp);
1553 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1555 fc_exch_release(ep);
1560 fc_exch_release(ep);
1562 fc_seq_ls_rjt(sp, reason, explan);
1567 * Handle response from RRQ.
1568 * Not much to do here, really.
1569 * Should report errors.
1571 * TODO: fix error handler.
1573 static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1575 struct fc_exch *aborted_ep = arg;
1579 int err = PTR_ERR(fp);
1581 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
1583 FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
1584 "frame error %d\n", err);
1588 op = fc_frame_payload_op(fp);
1593 FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
1598 FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
1604 fc_exch_done(&aborted_ep->seq);
1605 /* drop hold for rec qual */
1606 fc_exch_release(aborted_ep);
1610 * Send ELS RRQ - Reinstate Recovery Qualifier.
1611 * This tells the remote port to stop blocking the use of
1612 * the exchange and the seq_cnt range.
1614 static void fc_exch_rrq(struct fc_exch *ep)
1616 struct fc_lport *lp;
1617 struct fc_els_rrq *rrq;
1618 struct fc_frame *fp;
1623 fp = fc_frame_alloc(lp, sizeof(*rrq));
1627 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1628 memset(rrq, 0, sizeof(*rrq));
1629 rrq->rrq_cmd = ELS_RRQ;
1630 hton24(rrq->rrq_s_id, ep->sid);
1631 rrq->rrq_ox_id = htons(ep->oxid);
1632 rrq->rrq_rx_id = htons(ep->rxid);
1635 if (ep->esb_stat & ESB_ST_RESP)
1638 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1639 fc_host_port_id(lp->host), FC_TYPE_ELS,
1640 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1642 if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov))
1646 spin_lock_bh(&ep->ex_lock);
1647 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1648 spin_unlock_bh(&ep->ex_lock);
1649 /* drop hold for rec qual */
1650 fc_exch_release(ep);
1653 ep->esb_stat |= ESB_ST_REC_QUAL;
1654 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1655 spin_unlock_bh(&ep->ex_lock);
1660 * Handle incoming ELS RRQ - Reset Recovery Qualifier.
1662 static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1664 struct fc_exch *ep; /* request or subject exchange */
1665 struct fc_els_rrq *rp;
1668 enum fc_els_rjt_explan explan;
1670 rp = fc_frame_payload_get(fp, sizeof(*rp));
1671 explan = ELS_EXPL_INV_LEN;
1676 * lookup subject exchange.
1678 ep = fc_seq_exch(sp);
1679 sid = ntoh24(rp->rrq_s_id); /* subject source */
1680 xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
1681 ep = fc_exch_find(ep->em, xid);
1683 explan = ELS_EXPL_OXID_RXID;
1686 spin_lock_bh(&ep->ex_lock);
1687 if (ep->oxid != ntohs(rp->rrq_ox_id))
1689 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
1690 ep->rxid != FC_XID_UNKNOWN)
1692 explan = ELS_EXPL_SID;
1697 * Clear Recovery Qualifier state, and cancel timer if complete.
1699 if (ep->esb_stat & ESB_ST_REC_QUAL) {
1700 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1701 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
1703 if (ep->esb_stat & ESB_ST_COMPLETE) {
1704 if (cancel_delayed_work(&ep->timeout_work))
1705 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
1708 spin_unlock_bh(&ep->ex_lock);
1718 spin_unlock_bh(&ep->ex_lock);
1719 fc_exch_release(ep); /* drop hold from fc_exch_find */
1721 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
1725 struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
1726 struct fc_exch_mgr *mp,
1727 bool (*match)(struct fc_frame *))
1729 struct fc_exch_mgr_anchor *ema;
1731 ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
1737 /* add EM anchor to EM anchors list */
1738 list_add_tail(&ema->ema_list, &lport->ema_list);
1739 kref_get(&mp->kref);
1742 EXPORT_SYMBOL(fc_exch_mgr_add);
1744 static void fc_exch_mgr_destroy(struct kref *kref)
1746 struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
1749 * The total exch count must be zero
1750 * before freeing exchange manager.
1752 WARN_ON(mp->total_exches != 0);
1753 mempool_destroy(mp->ep_pool);
1757 void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
1759 /* remove EM anchor from EM anchors list */
1760 list_del(&ema->ema_list);
1761 kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
1764 EXPORT_SYMBOL(fc_exch_mgr_del);
1766 struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1767 enum fc_class class,
1768 u16 min_xid, u16 max_xid,
1769 bool (*match)(struct fc_frame *))
1771 struct fc_exch_mgr *mp;
1774 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
1775 FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
1781 * Memory need for EM
1783 len = (max_xid - min_xid + 1) * (sizeof(struct fc_exch *));
1784 len += sizeof(struct fc_exch_mgr);
1786 mp = kzalloc(len, GFP_ATOMIC);
1791 mp->total_exches = 0;
1792 mp->exches = (struct fc_exch **)(mp + 1);
1793 /* adjust em exch xid range for offload */
1794 mp->min_xid = min_xid;
1795 mp->max_xid = max_xid;
1796 mp->next_xid = min_xid;
1798 INIT_LIST_HEAD(&mp->ex_list);
1799 spin_lock_init(&mp->em_lock);
1801 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
1805 kref_init(&mp->kref);
1806 if (!fc_exch_mgr_add(lp, mp, match)) {
1807 mempool_destroy(mp->ep_pool);
1812 * Above kref_init() sets mp->kref to 1 and then
1813 * call to fc_exch_mgr_add incremented mp->kref again,
1814 * so adjust that extra increment.
1816 kref_put(&mp->kref, fc_exch_mgr_destroy);
1823 EXPORT_SYMBOL(fc_exch_mgr_alloc);
1825 void fc_exch_mgr_free(struct fc_lport *lport)
1827 struct fc_exch_mgr_anchor *ema, *next;
1829 list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
1830 fc_exch_mgr_del(ema);
1832 EXPORT_SYMBOL(fc_exch_mgr_free);
1835 struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
1836 struct fc_frame *fp,
1837 void (*resp)(struct fc_seq *,
1838 struct fc_frame *fp,
1840 void (*destructor)(struct fc_seq *, void *),
1841 void *arg, u32 timer_msec)
1844 struct fc_seq *sp = NULL;
1845 struct fc_frame_header *fh;
1848 ep = fc_exch_alloc(lp, fp);
1853 ep->esb_stat |= ESB_ST_SEQ_INIT;
1854 fh = fc_frame_header_get(fp);
1855 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1857 ep->destructor = destructor;
1859 ep->r_a_tov = FC_DEF_R_A_TOV;
1863 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1864 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1865 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1868 if (ep->xid <= lp->lro_xid)
1869 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1871 if (unlikely(lp->tt.frame_send(lp, fp)))
1875 fc_exch_timer_set_locked(ep, timer_msec);
1876 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1878 if (ep->f_ctl & FC_FC_SEQ_INIT)
1879 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1880 spin_unlock_bh(&ep->ex_lock);
1883 rc = fc_exch_done_locked(ep);
1884 spin_unlock_bh(&ep->ex_lock);
1886 fc_exch_mgr_delete_ep(ep);
1889 EXPORT_SYMBOL(fc_exch_seq_send);
1894 void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp)
1896 struct fc_frame_header *fh = fc_frame_header_get(fp);
1897 struct fc_exch_mgr_anchor *ema;
1898 u32 f_ctl, found = 0;
1902 if (!lp || lp->state == LPORT_ST_DISABLED) {
1903 FC_LPORT_DBG(lp, "Receiving frames for an lport that "
1904 "has not been initialized correctly\n");
1909 f_ctl = ntoh24(fh->fh_f_ctl);
1910 oxid = ntohs(fh->fh_ox_id);
1911 if (f_ctl & FC_FC_EX_CTX) {
1912 list_for_each_entry(ema, &lp->ema_list, ema_list) {
1913 if ((oxid >= ema->mp->min_xid) &&
1914 (oxid <= ema->mp->max_xid)) {
1921 FC_LPORT_DBG(lp, "Received response for out "
1922 "of range oxid:%hx\n", oxid);
1927 ema = list_entry(lp->ema_list.prev, typeof(*ema), ema_list);
1930 * If frame is marked invalid, just drop it.
1932 switch (fr_eof(fp)) {
1934 if (f_ctl & FC_FC_END_SEQ)
1935 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
1938 if (fh->fh_type == FC_TYPE_BLS)
1939 fc_exch_recv_bls(ema->mp, fp);
1940 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
1942 fc_exch_recv_seq_resp(ema->mp, fp);
1943 else if (f_ctl & FC_FC_SEQ_CTX)
1944 fc_exch_recv_resp(ema->mp, fp);
1946 fc_exch_recv_req(lp, ema->mp, fp);
1949 FC_LPORT_DBG(lp, "dropping invalid frame (eof %x)", fr_eof(fp));
1953 EXPORT_SYMBOL(fc_exch_recv);
1955 int fc_exch_init(struct fc_lport *lp)
1957 if (!lp->tt.seq_start_next)
1958 lp->tt.seq_start_next = fc_seq_start_next;
1960 if (!lp->tt.exch_seq_send)
1961 lp->tt.exch_seq_send = fc_exch_seq_send;
1963 if (!lp->tt.seq_send)
1964 lp->tt.seq_send = fc_seq_send;
1966 if (!lp->tt.seq_els_rsp_send)
1967 lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
1969 if (!lp->tt.exch_done)
1970 lp->tt.exch_done = fc_exch_done;
1972 if (!lp->tt.exch_mgr_reset)
1973 lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
1975 if (!lp->tt.seq_exch_abort)
1976 lp->tt.seq_exch_abort = fc_seq_exch_abort;
1980 EXPORT_SYMBOL(fc_exch_init);
1982 int fc_setup_exch_mgr(void)
1984 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
1985 0, SLAB_HWCACHE_ALIGN, NULL);
1991 void fc_destroy_exch_mgr(void)
1993 kmem_cache_destroy(fc_em_cachep);