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 #define FC_DEF_R_A_TOV (10 * 1000) /* resource allocation timeout */
38 * fc_exch_debug can be set in debugger or at compile time to get more logs.
40 static int fc_exch_debug;
42 #define FC_DEBUG_EXCH(fmt...) \
48 static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
51 * Structure and function definitions for managing Fibre Channel Exchanges
54 * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
56 * fc_exch_mgr holds the exchange state for an N port
58 * fc_exch holds state for one exchange and links to its active sequence.
60 * fc_seq holds the state for an individual sequence.
66 * This structure is the center for creating exchanges and sequences.
67 * It manages the allocation of exchange IDs.
70 enum fc_class class; /* default class for sequences */
71 spinlock_t em_lock; /* exchange manager lock,
72 must be taken before ex_lock */
73 u16 last_xid; /* last allocated exchange ID */
74 u16 min_xid; /* min exchange ID */
75 u16 max_xid; /* max exchange ID */
76 u16 max_read; /* max exchange ID for read */
77 u16 last_read; /* last xid allocated for read */
78 u32 total_exches; /* total allocated exchanges */
79 struct list_head ex_list; /* allocated exchanges list */
80 struct fc_lport *lp; /* fc device instance */
81 mempool_t *ep_pool; /* reserve ep's */
84 * currently exchange mgr stats are updated but not used.
85 * either stats can be expose via sysfs or remove them
86 * all together if not used XXX
89 atomic_t no_free_exch;
90 atomic_t no_free_exch_xid;
91 atomic_t xid_not_found;
93 atomic_t seq_not_found;
94 atomic_t non_bls_resp;
96 struct fc_exch **exches; /* for exch pointers indexed by xid */
98 #define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
100 static void fc_exch_rrq(struct fc_exch *);
101 static void fc_seq_ls_acc(struct fc_seq *);
102 static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason,
103 enum fc_els_rjt_explan);
104 static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *);
105 static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *);
106 static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp);
109 * Internal implementation notes.
111 * The exchange manager is one by default in libfc but LLD may choose
112 * to have one per CPU. The sequence manager is one per exchange manager
113 * and currently never separated.
115 * Section 9.8 in FC-FS-2 specifies: "The SEQ_ID is a one-byte field
116 * assigned by the Sequence Initiator that shall be unique for a specific
117 * D_ID and S_ID pair while the Sequence is open." Note that it isn't
118 * qualified by exchange ID, which one might think it would be.
119 * In practice this limits the number of open sequences and exchanges to 256
120 * per session. For most targets we could treat this limit as per exchange.
122 * The exchange and its sequence are freed when the last sequence is received.
123 * It's possible for the remote port to leave an exchange open without
124 * sending any sequences.
126 * Notes on reference counts:
128 * Exchanges are reference counted and exchange gets freed when the reference
129 * count becomes zero.
132 * Sequences are timed out for E_D_TOV and R_A_TOV.
134 * Sequence event handling:
136 * The following events may occur on initiator sequences:
139 * For now, the whole thing is sent.
141 * This applies only to class F.
142 * The sequence is marked complete.
144 * The upper layer calls fc_exch_done() when done
145 * with exchange and sequence tuple.
146 * RX-inferred completion.
147 * When we receive the next sequence on the same exchange, we can
148 * retire the previous sequence ID. (XXX not implemented).
150 * R_A_TOV frees the sequence ID. If we're waiting for ACK,
151 * E_D_TOV causes abort and calls upper layer response handler
152 * with FC_EX_TIMEOUT error.
158 * The following events may occur on recipient sequences:
161 * Allocate sequence for first frame received.
162 * Hold during receive handler.
163 * Release when final frame received.
164 * Keep status of last N of these for the ELS RES command. XXX TBD.
166 * Deallocate sequence
170 * For now, we neglect conditions where only part of a sequence was
171 * received or transmitted, or where out-of-order receipt is detected.
177 * The EM code run in a per-CPU worker thread.
179 * To protect against concurrency between a worker thread code and timers,
180 * sequence allocation and deallocation must be locked.
181 * - exchange refcnt can be done atomicly without locks.
182 * - sequence allocation must be locked by exch lock.
183 * - If the em_lock and ex_lock must be taken at the same time, then the
184 * em_lock must be taken before the ex_lock.
188 * opcode names for debugging.
190 static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
192 #define FC_TABLE_SIZE(x) (sizeof(x) / sizeof(x[0]))
194 static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
195 unsigned int max_index)
197 const char *name = NULL;
206 static const char *fc_exch_rctl_name(unsigned int op)
208 return fc_exch_name_lookup(op, fc_exch_rctl_names,
209 FC_TABLE_SIZE(fc_exch_rctl_names));
213 * Hold an exchange - keep it from being freed.
215 static void fc_exch_hold(struct fc_exch *ep)
217 atomic_inc(&ep->ex_refcnt);
221 * setup fc hdr by initializing few more FC header fields and sof/eof.
222 * Initialized fields by this func:
223 * - fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt
226 static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
229 struct fc_frame_header *fh = fc_frame_header_get(fp);
232 fr_sof(fp) = ep->class;
234 fr_sof(fp) = fc_sof_normal(ep->class);
236 if (f_ctl & FC_FC_END_SEQ) {
237 fr_eof(fp) = FC_EOF_T;
238 if (fc_sof_needs_ack(ep->class))
239 fr_eof(fp) = FC_EOF_N;
242 * The number of fill bytes to make the length a 4-byte
243 * multiple is the low order 2-bits of the f_ctl.
244 * The fill itself will have been cleared by the frame
246 * After this, the length will be even, as expected by
249 fill = fr_len(fp) & 3;
252 /* TODO, this may be a problem with fragmented skb */
253 skb_put(fp_skb(fp), fill);
254 hton24(fh->fh_f_ctl, f_ctl | fill);
257 WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
258 fr_eof(fp) = FC_EOF_N;
262 * Initialize remainig fh fields
263 * from fc_fill_fc_hdr
265 fh->fh_ox_id = htons(ep->oxid);
266 fh->fh_rx_id = htons(ep->rxid);
267 fh->fh_seq_id = ep->seq.id;
268 fh->fh_seq_cnt = htons(ep->seq.cnt);
273 * Release a reference to an exchange.
274 * If the refcnt goes to zero and the exchange is complete, it is freed.
276 static void fc_exch_release(struct fc_exch *ep)
278 struct fc_exch_mgr *mp;
280 if (atomic_dec_and_test(&ep->ex_refcnt)) {
283 ep->destructor(&ep->seq, ep->arg);
284 if (ep->lp->tt.exch_put)
285 ep->lp->tt.exch_put(ep->lp, mp, ep->xid);
286 WARN_ON(!ep->esb_stat & ESB_ST_COMPLETE);
287 mempool_free(ep, mp->ep_pool);
291 static int fc_exch_done_locked(struct fc_exch *ep)
296 * We must check for completion in case there are two threads
297 * tyring to complete this. But the rrq code will reuse the
298 * ep, and in that case we only clear the resp and set it as
299 * complete, so it can be reused by the timer to send the rrq.
302 if (ep->state & FC_EX_DONE)
304 ep->esb_stat |= ESB_ST_COMPLETE;
306 if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
307 ep->state |= FC_EX_DONE;
308 if (cancel_delayed_work(&ep->timeout_work))
309 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
315 static void fc_exch_mgr_delete_ep(struct fc_exch *ep)
317 struct fc_exch_mgr *mp;
320 spin_lock_bh(&mp->em_lock);
321 WARN_ON(mp->total_exches <= 0);
323 mp->exches[ep->xid - mp->min_xid] = NULL;
324 list_del(&ep->ex_list);
325 spin_unlock_bh(&mp->em_lock);
326 fc_exch_release(ep); /* drop hold for exch in mp */
330 * Internal version of fc_exch_timer_set - used with lock held.
332 static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
333 unsigned int timer_msec)
335 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
338 FC_DEBUG_EXCH("Exchange (%4x) timed out, notifying the upper layer\n",
340 if (schedule_delayed_work(&ep->timeout_work,
341 msecs_to_jiffies(timer_msec)))
342 fc_exch_hold(ep); /* hold for timer */
346 * Set timer for an exchange.
347 * The time is a minimum delay in milliseconds until the timer fires.
348 * Used for upper level protocols to time out the exchange.
349 * The timer is cancelled when it fires or when the exchange completes.
350 * Returns non-zero if a timer couldn't be allocated.
352 static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
354 spin_lock_bh(&ep->ex_lock);
355 fc_exch_timer_set_locked(ep, timer_msec);
356 spin_unlock_bh(&ep->ex_lock);
359 int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec)
366 ep = fc_seq_exch(req_sp);
368 spin_lock_bh(&ep->ex_lock);
369 if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
370 ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
371 spin_unlock_bh(&ep->ex_lock);
376 * Send the abort on a new sequence if possible.
378 sp = fc_seq_start_next_locked(&ep->seq);
380 spin_unlock_bh(&ep->ex_lock);
384 ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
386 fc_exch_timer_set_locked(ep, timer_msec);
387 spin_unlock_bh(&ep->ex_lock);
390 * If not logged into the fabric, don't send ABTS but leave
391 * sequence active until next timeout.
397 * Send an abort for the sequence that timed out.
399 fp = fc_frame_alloc(ep->lp, 0);
401 fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
402 FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
403 error = fc_seq_send(ep->lp, sp, fp);
408 EXPORT_SYMBOL(fc_seq_exch_abort);
411 * Exchange timeout - handle exchange timer expiration.
412 * The timer will have been cancelled before this is called.
414 static void fc_exch_timeout(struct work_struct *work)
416 struct fc_exch *ep = container_of(work, struct fc_exch,
418 struct fc_seq *sp = &ep->seq;
419 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
424 spin_lock_bh(&ep->ex_lock);
425 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
428 e_stat = ep->esb_stat;
429 if (e_stat & ESB_ST_COMPLETE) {
430 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
431 if (e_stat & ESB_ST_REC_QUAL)
433 spin_unlock_bh(&ep->ex_lock);
439 if (e_stat & ESB_ST_ABNORMAL)
440 rc = fc_exch_done_locked(ep);
441 spin_unlock_bh(&ep->ex_lock);
443 fc_exch_mgr_delete_ep(ep);
445 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
446 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
450 spin_unlock_bh(&ep->ex_lock);
453 * This release matches the hold taken when the timer was set.
459 * Allocate a sequence.
461 * We don't support multiple originated sequences on the same exchange.
462 * By implication, any previously originated sequence on this exchange
463 * is complete, and we reallocate the same sequence.
465 static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
477 * fc_em_alloc_xid - returns an xid based on request type
478 * @lp : ptr to associated lport
479 * @fp : ptr to the assocated frame
481 * check the associated fc_fsp_pkt to get scsi command type and
482 * command direction to decide from which range this exch id
483 * will be allocated from.
485 * Returns : 0 or an valid xid
487 static u16 fc_em_alloc_xid(struct fc_exch_mgr *mp, const struct fc_frame *fp)
491 struct fc_exch *ep = NULL;
494 if (fc_frame_is_read(fp)) {
497 plast = &mp->last_read;
499 min = mp->max_read + 1;
501 plast = &mp->last_xid;
506 plast = &mp->last_xid;
510 xid = (xid == max) ? min : xid + 1;
511 ep = mp->exches[xid - mp->min_xid];
512 } while ((ep != NULL) && (xid != *plast));
523 * fc_exch_alloc - allocate an exchange.
524 * @mp : ptr to the exchange manager
527 * if xid is supplied zero then assign next free exchange ID
528 * from exchange manager, otherwise use supplied xid.
529 * Returns with exch lock held.
531 struct fc_exch *fc_exch_alloc(struct fc_exch_mgr *mp,
532 struct fc_frame *fp, u16 xid)
536 /* allocate memory for exchange */
537 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
539 atomic_inc(&mp->stats.no_free_exch);
542 memset(ep, 0, sizeof(*ep));
544 spin_lock_bh(&mp->em_lock);
545 /* alloc xid if input xid 0 */
547 /* alloc a new xid */
548 xid = fc_em_alloc_xid(mp, fp);
550 printk(KERN_ERR "fc_em_alloc_xid() failed\n");
555 fc_exch_hold(ep); /* hold for exch in mp */
556 spin_lock_init(&ep->ex_lock);
558 * Hold exch lock for caller to prevent fc_exch_reset()
559 * from releasing exch while fc_exch_alloc() caller is
560 * still working on exch.
562 spin_lock_bh(&ep->ex_lock);
564 mp->exches[xid - mp->min_xid] = ep;
565 list_add_tail(&ep->ex_list, &mp->ex_list);
566 fc_seq_alloc(ep, ep->seq_id++);
568 spin_unlock_bh(&mp->em_lock);
573 ep->oxid = ep->xid = xid;
576 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
577 ep->rxid = FC_XID_UNKNOWN;
578 ep->class = mp->class;
579 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
583 spin_unlock_bh(&mp->em_lock);
584 atomic_inc(&mp->stats.no_free_exch_xid);
585 mempool_free(ep, mp->ep_pool);
588 EXPORT_SYMBOL(fc_exch_alloc);
591 * Lookup and hold an exchange.
593 static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
595 struct fc_exch *ep = NULL;
597 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
598 spin_lock_bh(&mp->em_lock);
599 ep = mp->exches[xid - mp->min_xid];
602 WARN_ON(ep->xid != xid);
604 spin_unlock_bh(&mp->em_lock);
609 void fc_exch_done(struct fc_seq *sp)
611 struct fc_exch *ep = fc_seq_exch(sp);
614 spin_lock_bh(&ep->ex_lock);
615 rc = fc_exch_done_locked(ep);
616 spin_unlock_bh(&ep->ex_lock);
618 fc_exch_mgr_delete_ep(ep);
620 EXPORT_SYMBOL(fc_exch_done);
623 * Allocate a new exchange as responder.
624 * Sets the responder ID in the frame header.
626 static struct fc_exch *fc_exch_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
629 struct fc_frame_header *fh;
632 ep = mp->lp->tt.exch_get(mp->lp, fp);
634 ep->class = fc_frame_class(fp);
637 * Set EX_CTX indicating we're responding on this exchange.
639 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
640 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
641 fh = fc_frame_header_get(fp);
642 ep->sid = ntoh24(fh->fh_d_id);
643 ep->did = ntoh24(fh->fh_s_id);
647 * Allocated exchange has placed the XID in the
648 * originator field. Move it to the responder field,
649 * and set the originator XID from the frame.
652 ep->oxid = ntohs(fh->fh_ox_id);
653 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
654 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
655 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
658 * Set the responder ID in the frame header.
659 * The old one should've been 0xffff.
660 * If it isn't, don't assign one.
661 * Incoming basic link service frames may specify
662 * a referenced RX_ID.
664 if (fh->fh_type != FC_TYPE_BLS) {
665 rxid = ntohs(fh->fh_rx_id);
666 WARN_ON(rxid != FC_XID_UNKNOWN);
667 fh->fh_rx_id = htons(ep->rxid);
669 fc_exch_hold(ep); /* hold for caller */
670 spin_unlock_bh(&ep->ex_lock); /* lock from exch_get */
676 * Find a sequence for receive where the other end is originating the sequence.
677 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
678 * on the ep that should be released by the caller.
680 static enum fc_pf_rjt_reason
681 fc_seq_lookup_recip(struct fc_exch_mgr *mp, struct fc_frame *fp)
683 struct fc_frame_header *fh = fc_frame_header_get(fp);
684 struct fc_exch *ep = NULL;
685 struct fc_seq *sp = NULL;
686 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
690 f_ctl = ntoh24(fh->fh_f_ctl);
691 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
694 * Lookup or create the exchange if we will be creating the sequence.
696 if (f_ctl & FC_FC_EX_CTX) {
697 xid = ntohs(fh->fh_ox_id); /* we originated exch */
698 ep = fc_exch_find(mp, xid);
700 atomic_inc(&mp->stats.xid_not_found);
701 reject = FC_RJT_OX_ID;
704 if (ep->rxid == FC_XID_UNKNOWN)
705 ep->rxid = ntohs(fh->fh_rx_id);
706 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
707 reject = FC_RJT_OX_ID;
711 xid = ntohs(fh->fh_rx_id); /* we are the responder */
714 * Special case for MDS issuing an ELS TEST with a
716 * XXX take this out once we do the proper reject.
718 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
719 fc_frame_payload_op(fp) == ELS_TEST) {
720 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
721 xid = FC_XID_UNKNOWN;
725 * new sequence - find the exchange
727 ep = fc_exch_find(mp, xid);
728 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
730 atomic_inc(&mp->stats.xid_busy);
731 reject = FC_RJT_RX_ID;
734 ep = fc_exch_resp(mp, fp);
736 reject = FC_RJT_EXCH_EST; /* XXX */
739 xid = ep->xid; /* get our XID */
741 atomic_inc(&mp->stats.xid_not_found);
742 reject = FC_RJT_RX_ID; /* XID not found */
748 * At this point, we have the exchange held.
749 * Find or create the sequence.
751 if (fc_sof_is_init(fr_sof(fp))) {
752 sp = fc_seq_start_next(&ep->seq);
754 reject = FC_RJT_SEQ_XS; /* exchange shortage */
757 sp->id = fh->fh_seq_id;
758 sp->ssb_stat |= SSB_ST_RESP;
761 if (sp->id != fh->fh_seq_id) {
762 atomic_inc(&mp->stats.seq_not_found);
763 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
767 WARN_ON(ep != fc_seq_exch(sp));
769 if (f_ctl & FC_FC_SEQ_INIT)
770 ep->esb_stat |= ESB_ST_SEQ_INIT;
776 fc_exch_done(&ep->seq);
777 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
782 * Find the sequence for a frame being received.
783 * We originated the sequence, so it should be found.
784 * We may or may not have originated the exchange.
785 * Does not hold the sequence for the caller.
787 static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
790 struct fc_frame_header *fh = fc_frame_header_get(fp);
792 struct fc_seq *sp = NULL;
796 f_ctl = ntoh24(fh->fh_f_ctl);
797 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
798 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
799 ep = fc_exch_find(mp, xid);
802 if (ep->seq.id == fh->fh_seq_id) {
804 * Save the RX_ID if we didn't previously know it.
807 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
808 ep->rxid == FC_XID_UNKNOWN) {
809 ep->rxid = ntohs(fh->fh_rx_id);
817 * Set addresses for an exchange.
818 * Note this must be done before the first sequence of the exchange is sent.
820 static void fc_exch_set_addr(struct fc_exch *ep,
821 u32 orig_id, u32 resp_id)
824 if (ep->esb_stat & ESB_ST_RESP) {
833 static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
835 struct fc_exch *ep = fc_seq_exch(sp);
837 sp = fc_seq_alloc(ep, ep->seq_id++);
838 FC_DEBUG_EXCH("exch %4x f_ctl %6x seq %2x\n",
839 ep->xid, ep->f_ctl, sp->id);
843 * Allocate a new sequence on the same exchange as the supplied sequence.
844 * This will never return NULL.
846 struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
848 struct fc_exch *ep = fc_seq_exch(sp);
850 spin_lock_bh(&ep->ex_lock);
851 WARN_ON((ep->esb_stat & ESB_ST_COMPLETE) != 0);
852 sp = fc_seq_start_next_locked(sp);
853 spin_unlock_bh(&ep->ex_lock);
857 EXPORT_SYMBOL(fc_seq_start_next);
859 int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
862 struct fc_frame_header *fh = fc_frame_header_get(fp);
866 ep = fc_seq_exch(sp);
867 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
869 f_ctl = ntoh24(fh->fh_f_ctl);
870 fc_exch_setup_hdr(ep, fp, f_ctl);
873 * update sequence count if this frame is carrying
874 * multiple FC frames when sequence offload is enabled
877 if (fr_max_payload(fp))
878 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
886 error = lp->tt.frame_send(lp, fp);
889 * Update the exchange and sequence flags,
890 * assuming all frames for the sequence have been sent.
891 * We can only be called to send once for each sequence.
893 spin_lock_bh(&ep->ex_lock);
894 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
895 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
896 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
897 spin_unlock_bh(&ep->ex_lock);
900 EXPORT_SYMBOL(fc_seq_send);
902 void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
903 struct fc_seq_els_data *els_data)
907 fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
913 fc_exch_els_rrq(sp, els_data->fp);
916 fc_exch_els_rec(sp, els_data->fp);
919 FC_DBG("Invalid ELS CMD:%x\n", els_cmd);
922 EXPORT_SYMBOL(fc_seq_els_rsp_send);
925 * Send a sequence, which is also the last sequence in the exchange.
927 static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
928 enum fc_rctl rctl, enum fc_fh_type fh_type)
931 struct fc_exch *ep = fc_seq_exch(sp);
933 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
935 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
936 fc_seq_send(ep->lp, sp, fp);
940 * Send ACK_1 (or equiv.) indicating we received something.
941 * The frame we're acking is supplied.
943 static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
946 struct fc_frame_header *rx_fh;
947 struct fc_frame_header *fh;
948 struct fc_exch *ep = fc_seq_exch(sp);
949 struct fc_lport *lp = ep->lp;
953 * Don't send ACKs for class 3.
955 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
956 fp = fc_frame_alloc(lp, 0);
960 fh = fc_frame_header_get(fp);
961 fh->fh_r_ctl = FC_RCTL_ACK_1;
962 fh->fh_type = FC_TYPE_BLS;
965 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
966 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
967 * Bits 9-8 are meaningful (retransmitted or unidirectional).
968 * Last ACK uses bits 7-6 (continue sequence),
969 * bits 5-4 are meaningful (what kind of ACK to use).
971 rx_fh = fc_frame_header_get(rx_fp);
972 f_ctl = ntoh24(rx_fh->fh_f_ctl);
973 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
974 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
975 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
976 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
977 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
978 hton24(fh->fh_f_ctl, f_ctl);
980 fc_exch_setup_hdr(ep, fp, f_ctl);
981 fh->fh_seq_id = rx_fh->fh_seq_id;
982 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
983 fh->fh_parm_offset = htonl(1); /* ack single frame */
985 fr_sof(fp) = fr_sof(rx_fp);
986 if (f_ctl & FC_FC_END_SEQ)
987 fr_eof(fp) = FC_EOF_T;
989 fr_eof(fp) = FC_EOF_N;
991 (void) lp->tt.frame_send(lp, fp);
997 * This is for rejecting BA_ABTS only.
1000 fc_exch_send_ba_rjt(struct fc_frame *rx_fp, enum fc_ba_rjt_reason reason,
1001 enum fc_ba_rjt_explan explan)
1003 struct fc_frame *fp;
1004 struct fc_frame_header *rx_fh;
1005 struct fc_frame_header *fh;
1006 struct fc_ba_rjt *rp;
1007 struct fc_lport *lp;
1011 fp = fc_frame_alloc(lp, sizeof(*rp));
1014 fh = fc_frame_header_get(fp);
1015 rx_fh = fc_frame_header_get(rx_fp);
1017 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
1019 rp = fc_frame_payload_get(fp, sizeof(*rp));
1020 rp->br_reason = reason;
1021 rp->br_explan = explan;
1024 * seq_id, cs_ctl, df_ctl and param/offset are zero.
1026 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
1027 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
1028 fh->fh_ox_id = rx_fh->fh_rx_id;
1029 fh->fh_rx_id = rx_fh->fh_ox_id;
1030 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1031 fh->fh_r_ctl = FC_RCTL_BA_RJT;
1032 fh->fh_type = FC_TYPE_BLS;
1035 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
1036 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
1037 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1038 * Last ACK uses bits 7-6 (continue sequence),
1039 * bits 5-4 are meaningful (what kind of ACK to use).
1040 * Always set LAST_SEQ, END_SEQ.
1042 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1043 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1044 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1045 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1046 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1047 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1048 f_ctl &= ~FC_FC_FIRST_SEQ;
1049 hton24(fh->fh_f_ctl, f_ctl);
1051 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1052 fr_eof(fp) = FC_EOF_T;
1053 if (fc_sof_needs_ack(fr_sof(fp)))
1054 fr_eof(fp) = FC_EOF_N;
1056 (void) lp->tt.frame_send(lp, fp);
1060 * Handle an incoming ABTS. This would be for target mode usually,
1061 * but could be due to lost FCP transfer ready, confirm or RRQ.
1062 * We always handle this as an exchange abort, ignoring the parameter.
1064 static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1066 struct fc_frame *fp;
1067 struct fc_ba_acc *ap;
1068 struct fc_frame_header *fh;
1073 spin_lock_bh(&ep->ex_lock);
1074 if (ep->esb_stat & ESB_ST_COMPLETE) {
1075 spin_unlock_bh(&ep->ex_lock);
1078 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1079 fc_exch_hold(ep); /* hold for REC_QUAL */
1080 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1081 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1083 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1085 spin_unlock_bh(&ep->ex_lock);
1088 fh = fc_frame_header_get(fp);
1089 ap = fc_frame_payload_get(fp, sizeof(*ap));
1090 memset(ap, 0, sizeof(*ap));
1092 ap->ba_high_seq_cnt = htons(0xffff);
1093 if (sp->ssb_stat & SSB_ST_RESP) {
1094 ap->ba_seq_id = sp->id;
1095 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1096 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1097 ap->ba_low_seq_cnt = htons(sp->cnt);
1099 sp = fc_seq_start_next_locked(sp);
1100 spin_unlock_bh(&ep->ex_lock);
1101 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1102 fc_frame_free(rx_fp);
1106 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1108 fc_frame_free(rx_fp);
1112 * Handle receive where the other end is originating the sequence.
1114 static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1115 struct fc_frame *fp)
1117 struct fc_frame_header *fh = fc_frame_header_get(fp);
1118 struct fc_seq *sp = NULL;
1119 struct fc_exch *ep = NULL;
1123 enum fc_pf_rjt_reason reject;
1126 reject = fc_seq_lookup_recip(mp, fp);
1127 if (reject == FC_RJT_NONE) {
1128 sp = fr_seq(fp); /* sequence will be held */
1129 ep = fc_seq_exch(sp);
1132 f_ctl = ntoh24(fh->fh_f_ctl);
1133 fc_seq_send_ack(sp, fp);
1136 * Call the receive function.
1138 * The receive function may allocate a new sequence
1139 * over the old one, so we shouldn't change the
1140 * sequence after this.
1142 * The frame will be freed by the receive function.
1143 * If new exch resp handler is valid then call that
1147 ep->resp(sp, fp, ep->arg);
1149 lp->tt.lport_recv(lp, sp, fp);
1150 fc_exch_release(ep); /* release from lookup */
1152 FC_DEBUG_EXCH("exch/seq lookup failed: reject %x\n", reject);
1158 * Handle receive where the other end is originating the sequence in
1159 * response to our exchange.
1161 static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1163 struct fc_frame_header *fh = fc_frame_header_get(fp);
1168 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1172 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1174 atomic_inc(&mp->stats.xid_not_found);
1177 if (ep->rxid == FC_XID_UNKNOWN)
1178 ep->rxid = ntohs(fh->fh_rx_id);
1179 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1180 atomic_inc(&mp->stats.xid_not_found);
1183 if (ep->did != ntoh24(fh->fh_s_id) &&
1184 ep->did != FC_FID_FLOGI) {
1185 atomic_inc(&mp->stats.xid_not_found);
1189 if (fc_sof_is_init(sof)) {
1190 sp = fc_seq_start_next(&ep->seq);
1191 sp->id = fh->fh_seq_id;
1192 sp->ssb_stat |= SSB_ST_RESP;
1195 if (sp->id != fh->fh_seq_id) {
1196 atomic_inc(&mp->stats.seq_not_found);
1200 f_ctl = ntoh24(fh->fh_f_ctl);
1202 if (f_ctl & FC_FC_SEQ_INIT)
1203 ep->esb_stat |= ESB_ST_SEQ_INIT;
1205 if (fc_sof_needs_ack(sof))
1206 fc_seq_send_ack(sp, fp);
1208 ex_resp_arg = ep->arg;
1210 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1211 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1212 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1213 spin_lock_bh(&ep->ex_lock);
1214 rc = fc_exch_done_locked(ep);
1215 WARN_ON(fc_seq_exch(sp) != ep);
1216 spin_unlock_bh(&ep->ex_lock);
1218 fc_exch_mgr_delete_ep(ep);
1222 * Call the receive function.
1223 * The sequence is held (has a refcnt) for us,
1224 * but not for the receive function.
1226 * The receive function may allocate a new sequence
1227 * over the old one, so we shouldn't change the
1228 * sequence after this.
1230 * The frame will be freed by the receive function.
1231 * If new exch resp handler is valid then call that
1235 resp(sp, fp, ex_resp_arg);
1238 fc_exch_release(ep);
1241 fc_exch_release(ep);
1247 * Handle receive for a sequence where other end is responding to our sequence.
1249 static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1253 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
1255 atomic_inc(&mp->stats.xid_not_found);
1256 FC_DEBUG_EXCH("seq lookup failed\n");
1258 atomic_inc(&mp->stats.non_bls_resp);
1259 FC_DEBUG_EXCH("non-BLS response to sequence");
1265 * Handle the response to an ABTS for exchange or sequence.
1266 * This can be BA_ACC or BA_RJT.
1268 static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1270 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1272 struct fc_frame_header *fh;
1273 struct fc_ba_acc *ap;
1277 int rc = 1, has_rec = 0;
1279 fh = fc_frame_header_get(fp);
1280 FC_DEBUG_EXCH("exch: BLS rctl %x - %s\n",
1281 fh->fh_r_ctl, fc_exch_rctl_name(fh->fh_r_ctl));
1283 if (cancel_delayed_work_sync(&ep->timeout_work))
1284 fc_exch_release(ep); /* release from pending timer hold */
1286 spin_lock_bh(&ep->ex_lock);
1287 switch (fh->fh_r_ctl) {
1288 case FC_RCTL_BA_ACC:
1289 ap = fc_frame_payload_get(fp, sizeof(*ap));
1294 * Decide whether to establish a Recovery Qualifier.
1295 * We do this if there is a non-empty SEQ_CNT range and
1296 * SEQ_ID is the same as the one we aborted.
1298 low = ntohs(ap->ba_low_seq_cnt);
1299 high = ntohs(ap->ba_high_seq_cnt);
1300 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1301 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1302 ap->ba_seq_id == ep->seq_id) && low != high) {
1303 ep->esb_stat |= ESB_ST_REC_QUAL;
1304 fc_exch_hold(ep); /* hold for recovery qualifier */
1308 case FC_RCTL_BA_RJT:
1315 ex_resp_arg = ep->arg;
1317 /* do we need to do some other checks here. Can we reuse more of
1318 * fc_exch_recv_seq_resp
1322 * do we want to check END_SEQ as well as LAST_SEQ here?
1324 if (ep->fh_type != FC_TYPE_FCP &&
1325 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1326 rc = fc_exch_done_locked(ep);
1327 spin_unlock_bh(&ep->ex_lock);
1329 fc_exch_mgr_delete_ep(ep);
1332 resp(sp, fp, ex_resp_arg);
1337 fc_exch_timer_set(ep, ep->r_a_tov);
1342 * Receive BLS sequence.
1343 * This is always a sequence initiated by the remote side.
1344 * We may be either the originator or recipient of the exchange.
1346 static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1348 struct fc_frame_header *fh;
1352 fh = fc_frame_header_get(fp);
1353 f_ctl = ntoh24(fh->fh_f_ctl);
1356 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1357 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1358 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1359 spin_lock_bh(&ep->ex_lock);
1360 ep->esb_stat |= ESB_ST_SEQ_INIT;
1361 spin_unlock_bh(&ep->ex_lock);
1363 if (f_ctl & FC_FC_SEQ_CTX) {
1365 * A response to a sequence we initiated.
1366 * This should only be ACKs for class 2 or F.
1368 switch (fh->fh_r_ctl) {
1373 FC_DEBUG_EXCH("BLS rctl %x - %s received",
1375 fc_exch_rctl_name(fh->fh_r_ctl));
1380 switch (fh->fh_r_ctl) {
1381 case FC_RCTL_BA_RJT:
1382 case FC_RCTL_BA_ACC:
1384 fc_exch_abts_resp(ep, fp);
1388 case FC_RCTL_BA_ABTS:
1389 fc_exch_recv_abts(ep, fp);
1391 default: /* ignore junk */
1397 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1401 * Accept sequence with LS_ACC.
1402 * If this fails due to allocation or transmit congestion, assume the
1403 * originator will repeat the sequence.
1405 static void fc_seq_ls_acc(struct fc_seq *req_sp)
1408 struct fc_els_ls_acc *acc;
1409 struct fc_frame *fp;
1411 sp = fc_seq_start_next(req_sp);
1412 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1414 acc = fc_frame_payload_get(fp, sizeof(*acc));
1415 memset(acc, 0, sizeof(*acc));
1416 acc->la_cmd = ELS_LS_ACC;
1417 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1422 * Reject sequence with ELS LS_RJT.
1423 * If this fails due to allocation or transmit congestion, assume the
1424 * originator will repeat the sequence.
1426 static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
1427 enum fc_els_rjt_explan explan)
1430 struct fc_els_ls_rjt *rjt;
1431 struct fc_frame *fp;
1433 sp = fc_seq_start_next(req_sp);
1434 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
1436 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1437 memset(rjt, 0, sizeof(*rjt));
1438 rjt->er_cmd = ELS_LS_RJT;
1439 rjt->er_reason = reason;
1440 rjt->er_explan = explan;
1441 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1445 static void fc_exch_reset(struct fc_exch *ep)
1448 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1452 spin_lock_bh(&ep->ex_lock);
1453 ep->state |= FC_EX_RST_CLEANUP;
1455 * we really want to call del_timer_sync, but cannot due
1456 * to the lport calling with the lport lock held (some resp
1457 * functions can also grab the lport lock which could cause
1460 if (cancel_delayed_work(&ep->timeout_work))
1461 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1464 if (ep->esb_stat & ESB_ST_REC_QUAL)
1465 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1466 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1469 rc = fc_exch_done_locked(ep);
1470 spin_unlock_bh(&ep->ex_lock);
1472 fc_exch_mgr_delete_ep(ep);
1475 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1479 * Reset an exchange manager, releasing all sequences and exchanges.
1480 * If sid is non-zero, reset only exchanges we source from that FID.
1481 * If did is non-zero, reset only exchanges destined to that FID.
1483 void fc_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
1486 struct fc_exch *next;
1487 struct fc_exch_mgr *mp = lp->emp;
1489 spin_lock_bh(&mp->em_lock);
1491 list_for_each_entry_safe(ep, next, &mp->ex_list, ex_list) {
1492 if ((sid == 0 || sid == ep->sid) &&
1493 (did == 0 || did == ep->did)) {
1495 spin_unlock_bh(&mp->em_lock);
1499 fc_exch_release(ep);
1500 spin_lock_bh(&mp->em_lock);
1503 * must restart loop incase while lock was down
1504 * multiple eps were released.
1509 spin_unlock_bh(&mp->em_lock);
1511 EXPORT_SYMBOL(fc_exch_mgr_reset);
1514 * Handle incoming ELS REC - Read Exchange Concise.
1515 * Note that the requesting port may be different than the S_ID in the request.
1517 static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
1519 struct fc_frame *fp;
1521 struct fc_exch_mgr *em;
1522 struct fc_els_rec *rp;
1523 struct fc_els_rec_acc *acc;
1524 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1525 enum fc_els_rjt_explan explan;
1530 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1531 explan = ELS_EXPL_INV_LEN;
1534 sid = ntoh24(rp->rec_s_id);
1535 rxid = ntohs(rp->rec_rx_id);
1536 oxid = ntohs(rp->rec_ox_id);
1539 * Currently it's hard to find the local S_ID from the exchange
1540 * manager. This will eventually be fixed, but for now it's easier
1541 * to lookup the subject exchange twice, once as if we were
1542 * the initiator, and then again if we weren't.
1544 em = fc_seq_exch(sp)->em;
1545 ep = fc_exch_find(em, oxid);
1546 explan = ELS_EXPL_OXID_RXID;
1547 if (ep && ep->oid == sid) {
1548 if (ep->rxid != FC_XID_UNKNOWN &&
1549 rxid != FC_XID_UNKNOWN &&
1554 fc_exch_release(ep);
1556 if (rxid != FC_XID_UNKNOWN)
1557 ep = fc_exch_find(em, rxid);
1562 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1567 sp = fc_seq_start_next(sp);
1568 acc = fc_frame_payload_get(fp, sizeof(*acc));
1569 memset(acc, 0, sizeof(*acc));
1570 acc->reca_cmd = ELS_LS_ACC;
1571 acc->reca_ox_id = rp->rec_ox_id;
1572 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1573 acc->reca_rx_id = htons(ep->rxid);
1574 if (ep->sid == ep->oid)
1575 hton24(acc->reca_rfid, ep->did);
1577 hton24(acc->reca_rfid, ep->sid);
1578 acc->reca_fc4value = htonl(ep->seq.rec_data);
1579 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1582 sp = fc_seq_start_next(sp);
1583 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1585 fc_exch_release(ep);
1590 fc_exch_release(ep);
1592 fc_seq_ls_rjt(sp, reason, explan);
1597 * Handle response from RRQ.
1598 * Not much to do here, really.
1599 * Should report errors.
1601 * TODO: fix error handler.
1603 static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1605 struct fc_exch *aborted_ep = arg;
1609 int err = PTR_ERR(fp);
1611 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
1613 FC_DBG("Cannot process RRQ, because of frame error %d\n", err);
1617 op = fc_frame_payload_op(fp);
1622 FC_DBG("LS_RJT for RRQ");
1627 FC_DBG("unexpected response op %x for RRQ", op);
1632 fc_exch_done(&aborted_ep->seq);
1633 /* drop hold for rec qual */
1634 fc_exch_release(aborted_ep);
1638 * Send ELS RRQ - Reinstate Recovery Qualifier.
1639 * This tells the remote port to stop blocking the use of
1640 * the exchange and the seq_cnt range.
1642 static void fc_exch_rrq(struct fc_exch *ep)
1644 struct fc_lport *lp;
1645 struct fc_els_rrq *rrq;
1646 struct fc_frame *fp;
1647 struct fc_seq *rrq_sp;
1652 fp = fc_frame_alloc(lp, sizeof(*rrq));
1655 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1656 memset(rrq, 0, sizeof(*rrq));
1657 rrq->rrq_cmd = ELS_RRQ;
1658 hton24(rrq->rrq_s_id, ep->sid);
1659 rrq->rrq_ox_id = htons(ep->oxid);
1660 rrq->rrq_rx_id = htons(ep->rxid);
1663 if (ep->esb_stat & ESB_ST_RESP)
1666 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1667 fc_host_port_id(lp->host), FC_TYPE_ELS,
1668 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1670 rrq_sp = fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep,
1673 ep->esb_stat |= ESB_ST_REC_QUAL;
1674 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1681 * Handle incoming ELS RRQ - Reset Recovery Qualifier.
1683 static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1685 struct fc_exch *ep; /* request or subject exchange */
1686 struct fc_els_rrq *rp;
1689 enum fc_els_rjt_explan explan;
1691 rp = fc_frame_payload_get(fp, sizeof(*rp));
1692 explan = ELS_EXPL_INV_LEN;
1697 * lookup subject exchange.
1699 ep = fc_seq_exch(sp);
1700 sid = ntoh24(rp->rrq_s_id); /* subject source */
1701 xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
1702 ep = fc_exch_find(ep->em, xid);
1704 explan = ELS_EXPL_OXID_RXID;
1707 spin_lock_bh(&ep->ex_lock);
1708 if (ep->oxid != ntohs(rp->rrq_ox_id))
1710 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
1711 ep->rxid != FC_XID_UNKNOWN)
1713 explan = ELS_EXPL_SID;
1718 * Clear Recovery Qualifier state, and cancel timer if complete.
1720 if (ep->esb_stat & ESB_ST_REC_QUAL) {
1721 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1722 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
1724 if (ep->esb_stat & ESB_ST_COMPLETE) {
1725 if (cancel_delayed_work(&ep->timeout_work))
1726 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
1729 spin_unlock_bh(&ep->ex_lock);
1739 spin_unlock_bh(&ep->ex_lock);
1740 fc_exch_release(ep); /* drop hold from fc_exch_find */
1742 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
1746 struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1747 enum fc_class class,
1748 u16 min_xid, u16 max_xid)
1750 struct fc_exch_mgr *mp;
1753 if (max_xid <= min_xid || min_xid == 0 || max_xid == FC_XID_UNKNOWN) {
1754 FC_DBG("Invalid min_xid 0x:%x and max_xid 0x:%x\n",
1760 * Memory need for EM
1762 #define xid_ok(i, m1, m2) (((i) >= (m1)) && ((i) <= (m2)))
1763 len = (max_xid - min_xid + 1) * (sizeof(struct fc_exch *));
1764 len += sizeof(struct fc_exch_mgr);
1766 mp = kzalloc(len, GFP_ATOMIC);
1771 mp->total_exches = 0;
1772 mp->exches = (struct fc_exch **)(mp + 1);
1774 /* adjust em exch xid range for offload */
1775 mp->min_xid = min_xid;
1776 mp->max_xid = max_xid;
1777 mp->last_xid = min_xid - 1;
1780 if (lp->lro_enabled && xid_ok(lp->lro_xid, min_xid, max_xid)) {
1781 mp->max_read = lp->lro_xid;
1782 mp->last_read = min_xid - 1;
1783 mp->last_xid = mp->max_read;
1785 /* disable lro if no xid control over read */
1786 lp->lro_enabled = 0;
1789 INIT_LIST_HEAD(&mp->ex_list);
1790 spin_lock_init(&mp->em_lock);
1792 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
1802 EXPORT_SYMBOL(fc_exch_mgr_alloc);
1804 void fc_exch_mgr_free(struct fc_exch_mgr *mp)
1808 * The total exch count must be zero
1809 * before freeing exchange manager.
1811 WARN_ON(mp->total_exches != 0);
1812 mempool_destroy(mp->ep_pool);
1815 EXPORT_SYMBOL(fc_exch_mgr_free);
1817 struct fc_exch *fc_exch_get(struct fc_lport *lp, struct fc_frame *fp)
1819 if (!lp || !lp->emp)
1822 return fc_exch_alloc(lp->emp, fp, 0);
1824 EXPORT_SYMBOL(fc_exch_get);
1826 struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
1827 struct fc_frame *fp,
1828 void (*resp)(struct fc_seq *,
1829 struct fc_frame *fp,
1831 void (*destructor)(struct fc_seq *, void *),
1832 void *arg, u32 timer_msec)
1835 struct fc_seq *sp = NULL;
1836 struct fc_frame_header *fh;
1839 ep = lp->tt.exch_get(lp, fp);
1844 ep->esb_stat |= ESB_ST_SEQ_INIT;
1845 fh = fc_frame_header_get(fp);
1846 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1848 ep->destructor = destructor;
1850 ep->r_a_tov = FC_DEF_R_A_TOV;
1854 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1855 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1856 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1859 if (unlikely(lp->tt.frame_send(lp, fp)))
1863 fc_exch_timer_set_locked(ep, timer_msec);
1864 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1866 if (ep->f_ctl & FC_FC_SEQ_INIT)
1867 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1868 spin_unlock_bh(&ep->ex_lock);
1871 rc = fc_exch_done_locked(ep);
1872 spin_unlock_bh(&ep->ex_lock);
1874 fc_exch_mgr_delete_ep(ep);
1877 EXPORT_SYMBOL(fc_exch_seq_send);
1882 void fc_exch_recv(struct fc_lport *lp, struct fc_exch_mgr *mp,
1883 struct fc_frame *fp)
1885 struct fc_frame_header *fh = fc_frame_header_get(fp);
1889 if (!lp || !mp || (lp->state == LPORT_ST_NONE)) {
1890 FC_DBG("fc_lport or EM is not allocated and configured");
1896 * If frame is marked invalid, just drop it.
1898 f_ctl = ntoh24(fh->fh_f_ctl);
1899 switch (fr_eof(fp)) {
1901 if (f_ctl & FC_FC_END_SEQ)
1902 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
1905 if (fh->fh_type == FC_TYPE_BLS)
1906 fc_exch_recv_bls(mp, fp);
1907 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
1909 fc_exch_recv_seq_resp(mp, fp);
1910 else if (f_ctl & FC_FC_SEQ_CTX)
1911 fc_exch_recv_resp(mp, fp);
1913 fc_exch_recv_req(lp, mp, fp);
1916 FC_DBG("dropping invalid frame (eof %x)", fr_eof(fp));
1921 EXPORT_SYMBOL(fc_exch_recv);
1923 int fc_exch_init(struct fc_lport *lp)
1925 if (!lp->tt.exch_get) {
1927 * exch_put() should be NULL if
1928 * exch_get() is NULL
1930 WARN_ON(lp->tt.exch_put);
1931 lp->tt.exch_get = fc_exch_get;
1934 if (!lp->tt.seq_start_next)
1935 lp->tt.seq_start_next = fc_seq_start_next;
1937 if (!lp->tt.exch_seq_send)
1938 lp->tt.exch_seq_send = fc_exch_seq_send;
1940 if (!lp->tt.seq_send)
1941 lp->tt.seq_send = fc_seq_send;
1943 if (!lp->tt.seq_els_rsp_send)
1944 lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
1946 if (!lp->tt.exch_done)
1947 lp->tt.exch_done = fc_exch_done;
1949 if (!lp->tt.exch_mgr_reset)
1950 lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
1952 if (!lp->tt.seq_exch_abort)
1953 lp->tt.seq_exch_abort = fc_seq_exch_abort;
1957 EXPORT_SYMBOL(fc_exch_init);
1959 int fc_setup_exch_mgr(void)
1961 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
1962 0, SLAB_HWCACHE_ALIGN, NULL);
1968 void fc_destroy_exch_mgr(void)
1970 kmem_cache_destroy(fc_em_cachep);