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 last_xid; /* last allocated 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 struct fc_lport *lp; /* fc device instance */
69 mempool_t *ep_pool; /* reserve ep's */
72 * currently exchange mgr stats are updated but not used.
73 * either stats can be expose via sysfs or remove them
74 * all together if not used XXX
77 atomic_t no_free_exch;
78 atomic_t no_free_exch_xid;
79 atomic_t xid_not_found;
81 atomic_t seq_not_found;
82 atomic_t non_bls_resp;
84 struct fc_exch **exches; /* for exch pointers indexed by xid */
86 #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 *);
94 static void fc_exch_rrq(struct fc_exch *);
95 static void fc_seq_ls_acc(struct fc_seq *);
96 static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason,
97 enum fc_els_rjt_explan);
98 static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *);
99 static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *);
100 static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp);
103 * Internal implementation notes.
105 * The exchange manager is one by default in libfc but LLD may choose
106 * to have one per CPU. The sequence manager is one per exchange manager
107 * and currently never separated.
109 * Section 9.8 in FC-FS-2 specifies: "The SEQ_ID is a one-byte field
110 * assigned by the Sequence Initiator that shall be unique for a specific
111 * D_ID and S_ID pair while the Sequence is open." Note that it isn't
112 * qualified by exchange ID, which one might think it would be.
113 * In practice this limits the number of open sequences and exchanges to 256
114 * per session. For most targets we could treat this limit as per exchange.
116 * The exchange and its sequence are freed when the last sequence is received.
117 * It's possible for the remote port to leave an exchange open without
118 * sending any sequences.
120 * Notes on reference counts:
122 * Exchanges are reference counted and exchange gets freed when the reference
123 * count becomes zero.
126 * Sequences are timed out for E_D_TOV and R_A_TOV.
128 * Sequence event handling:
130 * The following events may occur on initiator sequences:
133 * For now, the whole thing is sent.
135 * This applies only to class F.
136 * The sequence is marked complete.
138 * The upper layer calls fc_exch_done() when done
139 * with exchange and sequence tuple.
140 * RX-inferred completion.
141 * When we receive the next sequence on the same exchange, we can
142 * retire the previous sequence ID. (XXX not implemented).
144 * R_A_TOV frees the sequence ID. If we're waiting for ACK,
145 * E_D_TOV causes abort and calls upper layer response handler
146 * with FC_EX_TIMEOUT error.
152 * The following events may occur on recipient sequences:
155 * Allocate sequence for first frame received.
156 * Hold during receive handler.
157 * Release when final frame received.
158 * Keep status of last N of these for the ELS RES command. XXX TBD.
160 * Deallocate sequence
164 * For now, we neglect conditions where only part of a sequence was
165 * received or transmitted, or where out-of-order receipt is detected.
171 * The EM code run in a per-CPU worker thread.
173 * To protect against concurrency between a worker thread code and timers,
174 * sequence allocation and deallocation must be locked.
175 * - exchange refcnt can be done atomicly without locks.
176 * - sequence allocation must be locked by exch lock.
177 * - If the em_lock and ex_lock must be taken at the same time, then the
178 * em_lock must be taken before the ex_lock.
182 * opcode names for debugging.
184 static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
186 #define FC_TABLE_SIZE(x) (sizeof(x) / sizeof(x[0]))
188 static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
189 unsigned int max_index)
191 const char *name = NULL;
200 static const char *fc_exch_rctl_name(unsigned int op)
202 return fc_exch_name_lookup(op, fc_exch_rctl_names,
203 FC_TABLE_SIZE(fc_exch_rctl_names));
207 * Hold an exchange - keep it from being freed.
209 static void fc_exch_hold(struct fc_exch *ep)
211 atomic_inc(&ep->ex_refcnt);
215 * setup fc hdr by initializing few more FC header fields and sof/eof.
216 * Initialized fields by this func:
217 * - fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt
220 static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
223 struct fc_frame_header *fh = fc_frame_header_get(fp);
226 fr_sof(fp) = ep->class;
228 fr_sof(fp) = fc_sof_normal(ep->class);
230 if (f_ctl & FC_FC_END_SEQ) {
231 fr_eof(fp) = FC_EOF_T;
232 if (fc_sof_needs_ack(ep->class))
233 fr_eof(fp) = FC_EOF_N;
236 * The number of fill bytes to make the length a 4-byte
237 * multiple is the low order 2-bits of the f_ctl.
238 * The fill itself will have been cleared by the frame
240 * After this, the length will be even, as expected by
243 fill = fr_len(fp) & 3;
246 /* TODO, this may be a problem with fragmented skb */
247 skb_put(fp_skb(fp), fill);
248 hton24(fh->fh_f_ctl, f_ctl | fill);
251 WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
252 fr_eof(fp) = FC_EOF_N;
256 * Initialize remainig fh fields
257 * from fc_fill_fc_hdr
259 fh->fh_ox_id = htons(ep->oxid);
260 fh->fh_rx_id = htons(ep->rxid);
261 fh->fh_seq_id = ep->seq.id;
262 fh->fh_seq_cnt = htons(ep->seq.cnt);
267 * Release a reference to an exchange.
268 * If the refcnt goes to zero and the exchange is complete, it is freed.
270 static void fc_exch_release(struct fc_exch *ep)
272 struct fc_exch_mgr *mp;
274 if (atomic_dec_and_test(&ep->ex_refcnt)) {
277 ep->destructor(&ep->seq, ep->arg);
278 if (ep->lp->tt.exch_put)
279 ep->lp->tt.exch_put(ep->lp, mp, ep->xid);
280 WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
281 mempool_free(ep, mp->ep_pool);
285 static int fc_exch_done_locked(struct fc_exch *ep)
290 * We must check for completion in case there are two threads
291 * tyring to complete this. But the rrq code will reuse the
292 * ep, and in that case we only clear the resp and set it as
293 * complete, so it can be reused by the timer to send the rrq.
296 if (ep->state & FC_EX_DONE)
298 ep->esb_stat |= ESB_ST_COMPLETE;
300 if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
301 ep->state |= FC_EX_DONE;
302 if (cancel_delayed_work(&ep->timeout_work))
303 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
309 static void fc_exch_mgr_delete_ep(struct fc_exch *ep)
311 struct fc_exch_mgr *mp;
314 spin_lock_bh(&mp->em_lock);
315 WARN_ON(mp->total_exches <= 0);
317 mp->exches[ep->xid - mp->min_xid] = NULL;
318 list_del(&ep->ex_list);
319 spin_unlock_bh(&mp->em_lock);
320 fc_exch_release(ep); /* drop hold for exch in mp */
324 * Internal version of fc_exch_timer_set - used with lock held.
326 static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
327 unsigned int timer_msec)
329 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
332 FC_EXCH_DBG(ep, "Exchange timed out, notifying the upper layer\n");
334 if (schedule_delayed_work(&ep->timeout_work,
335 msecs_to_jiffies(timer_msec)))
336 fc_exch_hold(ep); /* hold for timer */
340 * Set timer for an exchange.
341 * The time is a minimum delay in milliseconds until the timer fires.
342 * Used for upper level protocols to time out the exchange.
343 * The timer is cancelled when it fires or when the exchange completes.
344 * Returns non-zero if a timer couldn't be allocated.
346 static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
348 spin_lock_bh(&ep->ex_lock);
349 fc_exch_timer_set_locked(ep, timer_msec);
350 spin_unlock_bh(&ep->ex_lock);
353 int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec)
360 ep = fc_seq_exch(req_sp);
362 spin_lock_bh(&ep->ex_lock);
363 if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
364 ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
365 spin_unlock_bh(&ep->ex_lock);
370 * Send the abort on a new sequence if possible.
372 sp = fc_seq_start_next_locked(&ep->seq);
374 spin_unlock_bh(&ep->ex_lock);
378 ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
380 fc_exch_timer_set_locked(ep, timer_msec);
381 spin_unlock_bh(&ep->ex_lock);
384 * If not logged into the fabric, don't send ABTS but leave
385 * sequence active until next timeout.
391 * Send an abort for the sequence that timed out.
393 fp = fc_frame_alloc(ep->lp, 0);
395 fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
396 FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
397 error = fc_seq_send(ep->lp, sp, fp);
402 EXPORT_SYMBOL(fc_seq_exch_abort);
405 * Exchange timeout - handle exchange timer expiration.
406 * The timer will have been cancelled before this is called.
408 static void fc_exch_timeout(struct work_struct *work)
410 struct fc_exch *ep = container_of(work, struct fc_exch,
412 struct fc_seq *sp = &ep->seq;
413 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
418 spin_lock_bh(&ep->ex_lock);
419 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
422 e_stat = ep->esb_stat;
423 if (e_stat & ESB_ST_COMPLETE) {
424 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
425 spin_unlock_bh(&ep->ex_lock);
426 if (e_stat & ESB_ST_REC_QUAL)
433 if (e_stat & ESB_ST_ABNORMAL)
434 rc = fc_exch_done_locked(ep);
435 spin_unlock_bh(&ep->ex_lock);
437 fc_exch_mgr_delete_ep(ep);
439 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
440 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
444 spin_unlock_bh(&ep->ex_lock);
447 * This release matches the hold taken when the timer was set.
453 * Allocate a sequence.
455 * We don't support multiple originated sequences on the same exchange.
456 * By implication, any previously originated sequence on this exchange
457 * is complete, and we reallocate the same sequence.
459 static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
471 * fc_em_alloc_xid - returns an xid based on request type
472 * @lp : ptr to associated lport
473 * @fp : ptr to the assocated frame
475 * check the associated fc_fsp_pkt to get scsi command type and
476 * command direction to decide from which range this exch id
477 * will be allocated from.
479 * Returns : 0 or an valid xid
481 static u16 fc_em_alloc_xid(struct fc_exch_mgr *mp, const struct fc_frame *fp)
485 struct fc_exch *ep = NULL;
488 if (fc_fcp_is_read(fr_fsp(fp))) {
491 plast = &mp->last_read;
493 min = mp->max_read + 1;
495 plast = &mp->last_xid;
500 plast = &mp->last_xid;
504 xid = (xid == max) ? min : xid + 1;
505 ep = mp->exches[xid - mp->min_xid];
506 } while ((ep != NULL) && (xid != *plast));
517 * fc_exch_alloc - allocate an exchange.
518 * @mp : ptr to the exchange manager
521 * if xid is supplied zero then assign next free exchange ID
522 * from exchange manager, otherwise use supplied xid.
523 * Returns with exch lock held.
525 struct fc_exch *fc_exch_alloc(struct fc_exch_mgr *mp,
526 struct fc_frame *fp, u16 xid)
530 /* allocate memory for exchange */
531 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
533 atomic_inc(&mp->stats.no_free_exch);
536 memset(ep, 0, sizeof(*ep));
538 spin_lock_bh(&mp->em_lock);
539 /* alloc xid if input xid 0 */
541 /* alloc a new xid */
542 xid = fc_em_alloc_xid(mp, fp);
544 printk(KERN_WARNING "libfc: Failed to allocate an exhange\n");
549 fc_exch_hold(ep); /* hold for exch in mp */
550 spin_lock_init(&ep->ex_lock);
552 * Hold exch lock for caller to prevent fc_exch_reset()
553 * from releasing exch while fc_exch_alloc() caller is
554 * still working on exch.
556 spin_lock_bh(&ep->ex_lock);
558 mp->exches[xid - mp->min_xid] = ep;
559 list_add_tail(&ep->ex_list, &mp->ex_list);
560 fc_seq_alloc(ep, ep->seq_id++);
562 spin_unlock_bh(&mp->em_lock);
567 ep->oxid = ep->xid = xid;
570 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
571 ep->rxid = FC_XID_UNKNOWN;
572 ep->class = mp->class;
573 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
577 spin_unlock_bh(&mp->em_lock);
578 atomic_inc(&mp->stats.no_free_exch_xid);
579 mempool_free(ep, mp->ep_pool);
582 EXPORT_SYMBOL(fc_exch_alloc);
585 * Lookup and hold an exchange.
587 static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
589 struct fc_exch *ep = NULL;
591 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
592 spin_lock_bh(&mp->em_lock);
593 ep = mp->exches[xid - mp->min_xid];
596 WARN_ON(ep->xid != xid);
598 spin_unlock_bh(&mp->em_lock);
603 void fc_exch_done(struct fc_seq *sp)
605 struct fc_exch *ep = fc_seq_exch(sp);
608 spin_lock_bh(&ep->ex_lock);
609 rc = fc_exch_done_locked(ep);
610 spin_unlock_bh(&ep->ex_lock);
612 fc_exch_mgr_delete_ep(ep);
614 EXPORT_SYMBOL(fc_exch_done);
617 * Allocate a new exchange as responder.
618 * Sets the responder ID in the frame header.
620 static struct fc_exch *fc_exch_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
623 struct fc_frame_header *fh;
625 ep = mp->lp->tt.exch_get(mp->lp, fp);
627 ep->class = fc_frame_class(fp);
630 * Set EX_CTX indicating we're responding on this exchange.
632 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
633 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
634 fh = fc_frame_header_get(fp);
635 ep->sid = ntoh24(fh->fh_d_id);
636 ep->did = ntoh24(fh->fh_s_id);
640 * Allocated exchange has placed the XID in the
641 * originator field. Move it to the responder field,
642 * and set the originator XID from the frame.
645 ep->oxid = ntohs(fh->fh_ox_id);
646 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
647 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
648 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
650 fc_exch_hold(ep); /* hold for caller */
651 spin_unlock_bh(&ep->ex_lock); /* lock from exch_get */
657 * Find a sequence for receive where the other end is originating the sequence.
658 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
659 * on the ep that should be released by the caller.
661 static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_exch_mgr *mp,
664 struct fc_frame_header *fh = fc_frame_header_get(fp);
665 struct fc_exch *ep = NULL;
666 struct fc_seq *sp = NULL;
667 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
671 f_ctl = ntoh24(fh->fh_f_ctl);
672 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
675 * Lookup or create the exchange if we will be creating the sequence.
677 if (f_ctl & FC_FC_EX_CTX) {
678 xid = ntohs(fh->fh_ox_id); /* we originated exch */
679 ep = fc_exch_find(mp, xid);
681 atomic_inc(&mp->stats.xid_not_found);
682 reject = FC_RJT_OX_ID;
685 if (ep->rxid == FC_XID_UNKNOWN)
686 ep->rxid = ntohs(fh->fh_rx_id);
687 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
688 reject = FC_RJT_OX_ID;
692 xid = ntohs(fh->fh_rx_id); /* we are the responder */
695 * Special case for MDS issuing an ELS TEST with a
697 * XXX take this out once we do the proper reject.
699 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
700 fc_frame_payload_op(fp) == ELS_TEST) {
701 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
702 xid = FC_XID_UNKNOWN;
706 * new sequence - find the exchange
708 ep = fc_exch_find(mp, xid);
709 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
711 atomic_inc(&mp->stats.xid_busy);
712 reject = FC_RJT_RX_ID;
715 ep = fc_exch_resp(mp, fp);
717 reject = FC_RJT_EXCH_EST; /* XXX */
720 xid = ep->xid; /* get our XID */
722 atomic_inc(&mp->stats.xid_not_found);
723 reject = FC_RJT_RX_ID; /* XID not found */
729 * At this point, we have the exchange held.
730 * Find or create the sequence.
732 if (fc_sof_is_init(fr_sof(fp))) {
733 sp = fc_seq_start_next(&ep->seq);
735 reject = FC_RJT_SEQ_XS; /* exchange shortage */
738 sp->id = fh->fh_seq_id;
739 sp->ssb_stat |= SSB_ST_RESP;
742 if (sp->id != fh->fh_seq_id) {
743 atomic_inc(&mp->stats.seq_not_found);
744 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
748 WARN_ON(ep != fc_seq_exch(sp));
750 if (f_ctl & FC_FC_SEQ_INIT)
751 ep->esb_stat |= ESB_ST_SEQ_INIT;
757 fc_exch_done(&ep->seq);
758 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
763 * Find the sequence for a frame being received.
764 * We originated the sequence, so it should be found.
765 * We may or may not have originated the exchange.
766 * Does not hold the sequence for the caller.
768 static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
771 struct fc_frame_header *fh = fc_frame_header_get(fp);
773 struct fc_seq *sp = NULL;
777 f_ctl = ntoh24(fh->fh_f_ctl);
778 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
779 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
780 ep = fc_exch_find(mp, xid);
783 if (ep->seq.id == fh->fh_seq_id) {
785 * Save the RX_ID if we didn't previously know it.
788 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
789 ep->rxid == FC_XID_UNKNOWN) {
790 ep->rxid = ntohs(fh->fh_rx_id);
798 * Set addresses for an exchange.
799 * Note this must be done before the first sequence of the exchange is sent.
801 static void fc_exch_set_addr(struct fc_exch *ep,
802 u32 orig_id, u32 resp_id)
805 if (ep->esb_stat & ESB_ST_RESP) {
814 static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
816 struct fc_exch *ep = fc_seq_exch(sp);
818 sp = fc_seq_alloc(ep, ep->seq_id++);
819 FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
824 * Allocate a new sequence on the same exchange as the supplied sequence.
825 * This will never return NULL.
827 struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
829 struct fc_exch *ep = fc_seq_exch(sp);
831 spin_lock_bh(&ep->ex_lock);
832 sp = fc_seq_start_next_locked(sp);
833 spin_unlock_bh(&ep->ex_lock);
837 EXPORT_SYMBOL(fc_seq_start_next);
839 int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
842 struct fc_frame_header *fh = fc_frame_header_get(fp);
846 ep = fc_seq_exch(sp);
847 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
849 f_ctl = ntoh24(fh->fh_f_ctl);
850 fc_exch_setup_hdr(ep, fp, f_ctl);
853 * update sequence count if this frame is carrying
854 * multiple FC frames when sequence offload is enabled
857 if (fr_max_payload(fp))
858 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
866 error = lp->tt.frame_send(lp, fp);
869 * Update the exchange and sequence flags,
870 * assuming all frames for the sequence have been sent.
871 * We can only be called to send once for each sequence.
873 spin_lock_bh(&ep->ex_lock);
874 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
875 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
876 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
877 spin_unlock_bh(&ep->ex_lock);
880 EXPORT_SYMBOL(fc_seq_send);
882 void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
883 struct fc_seq_els_data *els_data)
887 fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
893 fc_exch_els_rrq(sp, els_data->fp);
896 fc_exch_els_rec(sp, els_data->fp);
899 FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
902 EXPORT_SYMBOL(fc_seq_els_rsp_send);
905 * Send a sequence, which is also the last sequence in the exchange.
907 static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
908 enum fc_rctl rctl, enum fc_fh_type fh_type)
911 struct fc_exch *ep = fc_seq_exch(sp);
913 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
915 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
916 fc_seq_send(ep->lp, sp, fp);
920 * Send ACK_1 (or equiv.) indicating we received something.
921 * The frame we're acking is supplied.
923 static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
926 struct fc_frame_header *rx_fh;
927 struct fc_frame_header *fh;
928 struct fc_exch *ep = fc_seq_exch(sp);
929 struct fc_lport *lp = ep->lp;
933 * Don't send ACKs for class 3.
935 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
936 fp = fc_frame_alloc(lp, 0);
940 fh = fc_frame_header_get(fp);
941 fh->fh_r_ctl = FC_RCTL_ACK_1;
942 fh->fh_type = FC_TYPE_BLS;
945 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
946 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
947 * Bits 9-8 are meaningful (retransmitted or unidirectional).
948 * Last ACK uses bits 7-6 (continue sequence),
949 * bits 5-4 are meaningful (what kind of ACK to use).
951 rx_fh = fc_frame_header_get(rx_fp);
952 f_ctl = ntoh24(rx_fh->fh_f_ctl);
953 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
954 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
955 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
956 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
957 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
958 hton24(fh->fh_f_ctl, f_ctl);
960 fc_exch_setup_hdr(ep, fp, f_ctl);
961 fh->fh_seq_id = rx_fh->fh_seq_id;
962 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
963 fh->fh_parm_offset = htonl(1); /* ack single frame */
965 fr_sof(fp) = fr_sof(rx_fp);
966 if (f_ctl & FC_FC_END_SEQ)
967 fr_eof(fp) = FC_EOF_T;
969 fr_eof(fp) = FC_EOF_N;
971 (void) lp->tt.frame_send(lp, fp);
977 * This is for rejecting BA_ABTS only.
979 static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
980 enum fc_ba_rjt_reason reason,
981 enum fc_ba_rjt_explan explan)
984 struct fc_frame_header *rx_fh;
985 struct fc_frame_header *fh;
986 struct fc_ba_rjt *rp;
991 fp = fc_frame_alloc(lp, sizeof(*rp));
994 fh = fc_frame_header_get(fp);
995 rx_fh = fc_frame_header_get(rx_fp);
997 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
999 rp = fc_frame_payload_get(fp, sizeof(*rp));
1000 rp->br_reason = reason;
1001 rp->br_explan = explan;
1004 * seq_id, cs_ctl, df_ctl and param/offset are zero.
1006 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
1007 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
1008 fh->fh_ox_id = rx_fh->fh_rx_id;
1009 fh->fh_rx_id = rx_fh->fh_ox_id;
1010 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1011 fh->fh_r_ctl = FC_RCTL_BA_RJT;
1012 fh->fh_type = FC_TYPE_BLS;
1015 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
1016 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
1017 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1018 * Last ACK uses bits 7-6 (continue sequence),
1019 * bits 5-4 are meaningful (what kind of ACK to use).
1020 * Always set LAST_SEQ, END_SEQ.
1022 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1023 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1024 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1025 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1026 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1027 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1028 f_ctl &= ~FC_FC_FIRST_SEQ;
1029 hton24(fh->fh_f_ctl, f_ctl);
1031 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1032 fr_eof(fp) = FC_EOF_T;
1033 if (fc_sof_needs_ack(fr_sof(fp)))
1034 fr_eof(fp) = FC_EOF_N;
1036 (void) lp->tt.frame_send(lp, fp);
1040 * Handle an incoming ABTS. This would be for target mode usually,
1041 * but could be due to lost FCP transfer ready, confirm or RRQ.
1042 * We always handle this as an exchange abort, ignoring the parameter.
1044 static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1046 struct fc_frame *fp;
1047 struct fc_ba_acc *ap;
1048 struct fc_frame_header *fh;
1053 spin_lock_bh(&ep->ex_lock);
1054 if (ep->esb_stat & ESB_ST_COMPLETE) {
1055 spin_unlock_bh(&ep->ex_lock);
1058 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1059 fc_exch_hold(ep); /* hold for REC_QUAL */
1060 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1061 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1063 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1065 spin_unlock_bh(&ep->ex_lock);
1068 fh = fc_frame_header_get(fp);
1069 ap = fc_frame_payload_get(fp, sizeof(*ap));
1070 memset(ap, 0, sizeof(*ap));
1072 ap->ba_high_seq_cnt = htons(0xffff);
1073 if (sp->ssb_stat & SSB_ST_RESP) {
1074 ap->ba_seq_id = sp->id;
1075 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1076 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1077 ap->ba_low_seq_cnt = htons(sp->cnt);
1079 sp = fc_seq_start_next_locked(sp);
1080 spin_unlock_bh(&ep->ex_lock);
1081 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1082 fc_frame_free(rx_fp);
1086 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1088 fc_frame_free(rx_fp);
1092 * Handle receive where the other end is originating the sequence.
1094 static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1095 struct fc_frame *fp)
1097 struct fc_frame_header *fh = fc_frame_header_get(fp);
1098 struct fc_seq *sp = NULL;
1099 struct fc_exch *ep = NULL;
1103 enum fc_pf_rjt_reason reject;
1106 reject = fc_seq_lookup_recip(mp, fp);
1107 if (reject == FC_RJT_NONE) {
1108 sp = fr_seq(fp); /* sequence will be held */
1109 ep = fc_seq_exch(sp);
1112 f_ctl = ntoh24(fh->fh_f_ctl);
1113 fc_seq_send_ack(sp, fp);
1116 * Call the receive function.
1118 * The receive function may allocate a new sequence
1119 * over the old one, so we shouldn't change the
1120 * sequence after this.
1122 * The frame will be freed by the receive function.
1123 * If new exch resp handler is valid then call that
1127 ep->resp(sp, fp, ep->arg);
1129 lp->tt.lport_recv(lp, sp, fp);
1130 fc_exch_release(ep); /* release from lookup */
1132 FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject);
1138 * Handle receive where the other end is originating the sequence in
1139 * response to our exchange.
1141 static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1143 struct fc_frame_header *fh = fc_frame_header_get(fp);
1148 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1152 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1154 atomic_inc(&mp->stats.xid_not_found);
1157 if (ep->esb_stat & ESB_ST_COMPLETE) {
1158 atomic_inc(&mp->stats.xid_not_found);
1161 if (ep->rxid == FC_XID_UNKNOWN)
1162 ep->rxid = ntohs(fh->fh_rx_id);
1163 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1164 atomic_inc(&mp->stats.xid_not_found);
1167 if (ep->did != ntoh24(fh->fh_s_id) &&
1168 ep->did != FC_FID_FLOGI) {
1169 atomic_inc(&mp->stats.xid_not_found);
1173 if (fc_sof_is_init(sof)) {
1174 sp = fc_seq_start_next(&ep->seq);
1175 sp->id = fh->fh_seq_id;
1176 sp->ssb_stat |= SSB_ST_RESP;
1179 if (sp->id != fh->fh_seq_id) {
1180 atomic_inc(&mp->stats.seq_not_found);
1184 f_ctl = ntoh24(fh->fh_f_ctl);
1186 if (f_ctl & FC_FC_SEQ_INIT)
1187 ep->esb_stat |= ESB_ST_SEQ_INIT;
1189 if (fc_sof_needs_ack(sof))
1190 fc_seq_send_ack(sp, fp);
1192 ex_resp_arg = ep->arg;
1194 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1195 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1196 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1197 spin_lock_bh(&ep->ex_lock);
1198 rc = fc_exch_done_locked(ep);
1199 WARN_ON(fc_seq_exch(sp) != ep);
1200 spin_unlock_bh(&ep->ex_lock);
1202 fc_exch_mgr_delete_ep(ep);
1206 * Call the receive function.
1207 * The sequence is held (has a refcnt) for us,
1208 * but not for the receive function.
1210 * The receive function may allocate a new sequence
1211 * over the old one, so we shouldn't change the
1212 * sequence after this.
1214 * The frame will be freed by the receive function.
1215 * If new exch resp handler is valid then call that
1219 resp(sp, fp, ex_resp_arg);
1222 fc_exch_release(ep);
1225 fc_exch_release(ep);
1231 * Handle receive for a sequence where other end is responding to our sequence.
1233 static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1237 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
1240 atomic_inc(&mp->stats.xid_not_found);
1242 atomic_inc(&mp->stats.non_bls_resp);
1248 * Handle the response to an ABTS for exchange or sequence.
1249 * This can be BA_ACC or BA_RJT.
1251 static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1253 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1255 struct fc_frame_header *fh;
1256 struct fc_ba_acc *ap;
1260 int rc = 1, has_rec = 0;
1262 fh = fc_frame_header_get(fp);
1263 FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
1264 fc_exch_rctl_name(fh->fh_r_ctl));
1266 if (cancel_delayed_work_sync(&ep->timeout_work))
1267 fc_exch_release(ep); /* release from pending timer hold */
1269 spin_lock_bh(&ep->ex_lock);
1270 switch (fh->fh_r_ctl) {
1271 case FC_RCTL_BA_ACC:
1272 ap = fc_frame_payload_get(fp, sizeof(*ap));
1277 * Decide whether to establish a Recovery Qualifier.
1278 * We do this if there is a non-empty SEQ_CNT range and
1279 * SEQ_ID is the same as the one we aborted.
1281 low = ntohs(ap->ba_low_seq_cnt);
1282 high = ntohs(ap->ba_high_seq_cnt);
1283 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1284 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1285 ap->ba_seq_id == ep->seq_id) && low != high) {
1286 ep->esb_stat |= ESB_ST_REC_QUAL;
1287 fc_exch_hold(ep); /* hold for recovery qualifier */
1291 case FC_RCTL_BA_RJT:
1298 ex_resp_arg = ep->arg;
1300 /* do we need to do some other checks here. Can we reuse more of
1301 * fc_exch_recv_seq_resp
1305 * do we want to check END_SEQ as well as LAST_SEQ here?
1307 if (ep->fh_type != FC_TYPE_FCP &&
1308 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1309 rc = fc_exch_done_locked(ep);
1310 spin_unlock_bh(&ep->ex_lock);
1312 fc_exch_mgr_delete_ep(ep);
1315 resp(sp, fp, ex_resp_arg);
1320 fc_exch_timer_set(ep, ep->r_a_tov);
1325 * Receive BLS sequence.
1326 * This is always a sequence initiated by the remote side.
1327 * We may be either the originator or recipient of the exchange.
1329 static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1331 struct fc_frame_header *fh;
1335 fh = fc_frame_header_get(fp);
1336 f_ctl = ntoh24(fh->fh_f_ctl);
1339 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1340 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1341 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1342 spin_lock_bh(&ep->ex_lock);
1343 ep->esb_stat |= ESB_ST_SEQ_INIT;
1344 spin_unlock_bh(&ep->ex_lock);
1346 if (f_ctl & FC_FC_SEQ_CTX) {
1348 * A response to a sequence we initiated.
1349 * This should only be ACKs for class 2 or F.
1351 switch (fh->fh_r_ctl) {
1356 FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
1358 fc_exch_rctl_name(fh->fh_r_ctl));
1363 switch (fh->fh_r_ctl) {
1364 case FC_RCTL_BA_RJT:
1365 case FC_RCTL_BA_ACC:
1367 fc_exch_abts_resp(ep, fp);
1371 case FC_RCTL_BA_ABTS:
1372 fc_exch_recv_abts(ep, fp);
1374 default: /* ignore junk */
1380 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1384 * Accept sequence with LS_ACC.
1385 * If this fails due to allocation or transmit congestion, assume the
1386 * originator will repeat the sequence.
1388 static void fc_seq_ls_acc(struct fc_seq *req_sp)
1391 struct fc_els_ls_acc *acc;
1392 struct fc_frame *fp;
1394 sp = fc_seq_start_next(req_sp);
1395 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1397 acc = fc_frame_payload_get(fp, sizeof(*acc));
1398 memset(acc, 0, sizeof(*acc));
1399 acc->la_cmd = ELS_LS_ACC;
1400 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1405 * Reject sequence with ELS LS_RJT.
1406 * If this fails due to allocation or transmit congestion, assume the
1407 * originator will repeat the sequence.
1409 static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
1410 enum fc_els_rjt_explan explan)
1413 struct fc_els_ls_rjt *rjt;
1414 struct fc_frame *fp;
1416 sp = fc_seq_start_next(req_sp);
1417 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
1419 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1420 memset(rjt, 0, sizeof(*rjt));
1421 rjt->er_cmd = ELS_LS_RJT;
1422 rjt->er_reason = reason;
1423 rjt->er_explan = explan;
1424 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1428 static void fc_exch_reset(struct fc_exch *ep)
1431 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1435 spin_lock_bh(&ep->ex_lock);
1436 ep->state |= FC_EX_RST_CLEANUP;
1438 * we really want to call del_timer_sync, but cannot due
1439 * to the lport calling with the lport lock held (some resp
1440 * functions can also grab the lport lock which could cause
1443 if (cancel_delayed_work(&ep->timeout_work))
1444 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1447 if (ep->esb_stat & ESB_ST_REC_QUAL)
1448 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1449 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1452 rc = fc_exch_done_locked(ep);
1453 spin_unlock_bh(&ep->ex_lock);
1455 fc_exch_mgr_delete_ep(ep);
1458 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1462 * Reset an exchange manager, releasing all sequences and exchanges.
1463 * If sid is non-zero, reset only exchanges we source from that FID.
1464 * If did is non-zero, reset only exchanges destined to that FID.
1466 void fc_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
1469 struct fc_exch *next;
1470 struct fc_exch_mgr *mp = lp->emp;
1472 spin_lock_bh(&mp->em_lock);
1474 list_for_each_entry_safe(ep, next, &mp->ex_list, ex_list) {
1475 if ((sid == 0 || sid == ep->sid) &&
1476 (did == 0 || did == ep->did)) {
1478 spin_unlock_bh(&mp->em_lock);
1482 fc_exch_release(ep);
1483 spin_lock_bh(&mp->em_lock);
1486 * must restart loop incase while lock was down
1487 * multiple eps were released.
1492 spin_unlock_bh(&mp->em_lock);
1494 EXPORT_SYMBOL(fc_exch_mgr_reset);
1497 * Handle incoming ELS REC - Read Exchange Concise.
1498 * Note that the requesting port may be different than the S_ID in the request.
1500 static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
1502 struct fc_frame *fp;
1504 struct fc_exch_mgr *em;
1505 struct fc_els_rec *rp;
1506 struct fc_els_rec_acc *acc;
1507 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1508 enum fc_els_rjt_explan explan;
1513 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1514 explan = ELS_EXPL_INV_LEN;
1517 sid = ntoh24(rp->rec_s_id);
1518 rxid = ntohs(rp->rec_rx_id);
1519 oxid = ntohs(rp->rec_ox_id);
1522 * Currently it's hard to find the local S_ID from the exchange
1523 * manager. This will eventually be fixed, but for now it's easier
1524 * to lookup the subject exchange twice, once as if we were
1525 * the initiator, and then again if we weren't.
1527 em = fc_seq_exch(sp)->em;
1528 ep = fc_exch_find(em, oxid);
1529 explan = ELS_EXPL_OXID_RXID;
1530 if (ep && ep->oid == sid) {
1531 if (ep->rxid != FC_XID_UNKNOWN &&
1532 rxid != FC_XID_UNKNOWN &&
1537 fc_exch_release(ep);
1539 if (rxid != FC_XID_UNKNOWN)
1540 ep = fc_exch_find(em, rxid);
1545 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1550 sp = fc_seq_start_next(sp);
1551 acc = fc_frame_payload_get(fp, sizeof(*acc));
1552 memset(acc, 0, sizeof(*acc));
1553 acc->reca_cmd = ELS_LS_ACC;
1554 acc->reca_ox_id = rp->rec_ox_id;
1555 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1556 acc->reca_rx_id = htons(ep->rxid);
1557 if (ep->sid == ep->oid)
1558 hton24(acc->reca_rfid, ep->did);
1560 hton24(acc->reca_rfid, ep->sid);
1561 acc->reca_fc4value = htonl(ep->seq.rec_data);
1562 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1565 sp = fc_seq_start_next(sp);
1566 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1568 fc_exch_release(ep);
1573 fc_exch_release(ep);
1575 fc_seq_ls_rjt(sp, reason, explan);
1580 * Handle response from RRQ.
1581 * Not much to do here, really.
1582 * Should report errors.
1584 * TODO: fix error handler.
1586 static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1588 struct fc_exch *aborted_ep = arg;
1592 int err = PTR_ERR(fp);
1594 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
1596 FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
1597 "frame error %d\n", err);
1601 op = fc_frame_payload_op(fp);
1606 FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
1611 FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
1617 fc_exch_done(&aborted_ep->seq);
1618 /* drop hold for rec qual */
1619 fc_exch_release(aborted_ep);
1623 * Send ELS RRQ - Reinstate Recovery Qualifier.
1624 * This tells the remote port to stop blocking the use of
1625 * the exchange and the seq_cnt range.
1627 static void fc_exch_rrq(struct fc_exch *ep)
1629 struct fc_lport *lp;
1630 struct fc_els_rrq *rrq;
1631 struct fc_frame *fp;
1636 fp = fc_frame_alloc(lp, sizeof(*rrq));
1640 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1641 memset(rrq, 0, sizeof(*rrq));
1642 rrq->rrq_cmd = ELS_RRQ;
1643 hton24(rrq->rrq_s_id, ep->sid);
1644 rrq->rrq_ox_id = htons(ep->oxid);
1645 rrq->rrq_rx_id = htons(ep->rxid);
1648 if (ep->esb_stat & ESB_ST_RESP)
1651 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1652 fc_host_port_id(lp->host), FC_TYPE_ELS,
1653 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1655 if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov))
1659 spin_lock_bh(&ep->ex_lock);
1660 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1661 spin_unlock_bh(&ep->ex_lock);
1662 /* drop hold for rec qual */
1663 fc_exch_release(ep);
1666 ep->esb_stat |= ESB_ST_REC_QUAL;
1667 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1668 spin_unlock_bh(&ep->ex_lock);
1673 * Handle incoming ELS RRQ - Reset Recovery Qualifier.
1675 static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1677 struct fc_exch *ep; /* request or subject exchange */
1678 struct fc_els_rrq *rp;
1681 enum fc_els_rjt_explan explan;
1683 rp = fc_frame_payload_get(fp, sizeof(*rp));
1684 explan = ELS_EXPL_INV_LEN;
1689 * lookup subject exchange.
1691 ep = fc_seq_exch(sp);
1692 sid = ntoh24(rp->rrq_s_id); /* subject source */
1693 xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
1694 ep = fc_exch_find(ep->em, xid);
1696 explan = ELS_EXPL_OXID_RXID;
1699 spin_lock_bh(&ep->ex_lock);
1700 if (ep->oxid != ntohs(rp->rrq_ox_id))
1702 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
1703 ep->rxid != FC_XID_UNKNOWN)
1705 explan = ELS_EXPL_SID;
1710 * Clear Recovery Qualifier state, and cancel timer if complete.
1712 if (ep->esb_stat & ESB_ST_REC_QUAL) {
1713 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1714 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
1716 if (ep->esb_stat & ESB_ST_COMPLETE) {
1717 if (cancel_delayed_work(&ep->timeout_work))
1718 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
1721 spin_unlock_bh(&ep->ex_lock);
1731 spin_unlock_bh(&ep->ex_lock);
1732 fc_exch_release(ep); /* drop hold from fc_exch_find */
1734 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
1738 struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
1739 struct fc_exch_mgr *mp,
1740 bool (*match)(struct fc_frame *))
1742 struct fc_exch_mgr_anchor *ema;
1744 ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
1750 /* add EM anchor to EM anchors list */
1751 list_add_tail(&ema->ema_list, &lport->ema_list);
1752 kref_get(&mp->kref);
1755 EXPORT_SYMBOL(fc_exch_mgr_add);
1757 static void fc_exch_mgr_destroy(struct kref *kref)
1759 struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
1762 * The total exch count must be zero
1763 * before freeing exchange manager.
1765 WARN_ON(mp->total_exches != 0);
1766 mempool_destroy(mp->ep_pool);
1770 void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
1772 /* remove EM anchor from EM anchors list */
1773 list_del(&ema->ema_list);
1774 kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
1777 EXPORT_SYMBOL(fc_exch_mgr_del);
1779 struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1780 enum fc_class class,
1781 u16 min_xid, u16 max_xid)
1783 struct fc_exch_mgr *mp;
1786 if (max_xid <= min_xid || min_xid == 0 || max_xid == FC_XID_UNKNOWN) {
1787 FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
1793 * Memory need for EM
1795 #define xid_ok(i, m1, m2) (((i) >= (m1)) && ((i) <= (m2)))
1796 len = (max_xid - min_xid + 1) * (sizeof(struct fc_exch *));
1797 len += sizeof(struct fc_exch_mgr);
1799 mp = kzalloc(len, GFP_ATOMIC);
1804 mp->total_exches = 0;
1805 mp->exches = (struct fc_exch **)(mp + 1);
1807 /* adjust em exch xid range for offload */
1808 mp->min_xid = min_xid;
1809 mp->max_xid = max_xid;
1810 mp->last_xid = min_xid - 1;
1813 if (lp->lro_enabled && xid_ok(lp->lro_xid, min_xid, max_xid)) {
1814 mp->max_read = lp->lro_xid;
1815 mp->last_read = min_xid - 1;
1816 mp->last_xid = mp->max_read;
1818 /* disable lro if no xid control over read */
1819 lp->lro_enabled = 0;
1822 INIT_LIST_HEAD(&mp->ex_list);
1823 spin_lock_init(&mp->em_lock);
1825 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
1835 EXPORT_SYMBOL(fc_exch_mgr_alloc);
1837 void fc_exch_mgr_free(struct fc_exch_mgr *mp)
1841 * The total exch count must be zero
1842 * before freeing exchange manager.
1844 WARN_ON(mp->total_exches != 0);
1845 mempool_destroy(mp->ep_pool);
1848 EXPORT_SYMBOL(fc_exch_mgr_free);
1850 struct fc_exch *fc_exch_get(struct fc_lport *lp, struct fc_frame *fp)
1852 if (!lp || !lp->emp)
1855 return fc_exch_alloc(lp->emp, fp, 0);
1857 EXPORT_SYMBOL(fc_exch_get);
1859 struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
1860 struct fc_frame *fp,
1861 void (*resp)(struct fc_seq *,
1862 struct fc_frame *fp,
1864 void (*destructor)(struct fc_seq *, void *),
1865 void *arg, u32 timer_msec)
1868 struct fc_seq *sp = NULL;
1869 struct fc_frame_header *fh;
1872 ep = lp->tt.exch_get(lp, fp);
1877 ep->esb_stat |= ESB_ST_SEQ_INIT;
1878 fh = fc_frame_header_get(fp);
1879 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1881 ep->destructor = destructor;
1883 ep->r_a_tov = FC_DEF_R_A_TOV;
1887 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1888 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1889 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1892 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1894 if (unlikely(lp->tt.frame_send(lp, fp)))
1898 fc_exch_timer_set_locked(ep, timer_msec);
1899 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1901 if (ep->f_ctl & FC_FC_SEQ_INIT)
1902 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1903 spin_unlock_bh(&ep->ex_lock);
1906 rc = fc_exch_done_locked(ep);
1907 spin_unlock_bh(&ep->ex_lock);
1909 fc_exch_mgr_delete_ep(ep);
1912 EXPORT_SYMBOL(fc_exch_seq_send);
1917 void fc_exch_recv(struct fc_lport *lp, struct fc_exch_mgr *mp,
1918 struct fc_frame *fp)
1920 struct fc_frame_header *fh = fc_frame_header_get(fp);
1924 if (!lp || !mp || lp->state == LPORT_ST_DISABLED) {
1925 FC_LPORT_DBG(lp, "Receiving frames for an lport that "
1926 "has not been initialized correctly\n");
1932 * If frame is marked invalid, just drop it.
1934 f_ctl = ntoh24(fh->fh_f_ctl);
1935 switch (fr_eof(fp)) {
1937 if (f_ctl & FC_FC_END_SEQ)
1938 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
1941 if (fh->fh_type == FC_TYPE_BLS)
1942 fc_exch_recv_bls(mp, fp);
1943 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
1945 fc_exch_recv_seq_resp(mp, fp);
1946 else if (f_ctl & FC_FC_SEQ_CTX)
1947 fc_exch_recv_resp(mp, fp);
1949 fc_exch_recv_req(lp, mp, fp);
1952 FC_LPORT_DBG(lp, "dropping invalid frame (eof %x)", fr_eof(fp));
1957 EXPORT_SYMBOL(fc_exch_recv);
1959 int fc_exch_init(struct fc_lport *lp)
1961 if (!lp->tt.exch_get) {
1963 * exch_put() should be NULL if
1964 * exch_get() is NULL
1966 WARN_ON(lp->tt.exch_put);
1967 lp->tt.exch_get = fc_exch_get;
1970 if (!lp->tt.seq_start_next)
1971 lp->tt.seq_start_next = fc_seq_start_next;
1973 if (!lp->tt.exch_seq_send)
1974 lp->tt.exch_seq_send = fc_exch_seq_send;
1976 if (!lp->tt.seq_send)
1977 lp->tt.seq_send = fc_seq_send;
1979 if (!lp->tt.seq_els_rsp_send)
1980 lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
1982 if (!lp->tt.exch_done)
1983 lp->tt.exch_done = fc_exch_done;
1985 if (!lp->tt.exch_mgr_reset)
1986 lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
1988 if (!lp->tt.seq_exch_abort)
1989 lp->tt.seq_exch_abort = fc_seq_exch_abort;
1993 EXPORT_SYMBOL(fc_exch_init);
1995 int fc_setup_exch_mgr(void)
1997 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
1998 0, SLAB_HWCACHE_ALIGN, NULL);
2004 void fc_destroy_exch_mgr(void)
2006 kmem_cache_destroy(fc_em_cachep);