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>
36 * fc_exch_debug can be set in debugger or at compile time to get more logs.
38 static int fc_exch_debug;
40 #define FC_DEBUG_EXCH(fmt...) \
46 static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
49 * Structure and function definitions for managing Fibre Channel Exchanges
52 * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
54 * fc_exch_mgr holds the exchange state for an N port
56 * fc_exch holds state for one exchange and links to its active sequence.
58 * fc_seq holds the state for an individual sequence.
64 * This structure is the center for creating exchanges and sequences.
65 * It manages the allocation of exchange IDs.
68 enum fc_class class; /* default class for sequences */
69 spinlock_t em_lock; /* exchange manager lock,
70 must be taken before ex_lock */
71 u16 last_xid; /* last allocated exchange ID */
72 u16 min_xid; /* min exchange ID */
73 u16 max_xid; /* max exchange ID */
74 u16 max_read; /* max exchange ID for read */
75 u16 last_read; /* last xid allocated for read */
76 u32 total_exches; /* total allocated exchanges */
77 struct list_head ex_list; /* allocated exchanges list */
78 struct fc_lport *lp; /* fc device instance */
79 mempool_t *ep_pool; /* reserve ep's */
82 * currently exchange mgr stats are updated but not used.
83 * either stats can be expose via sysfs or remove them
84 * all together if not used XXX
87 atomic_t no_free_exch;
88 atomic_t no_free_exch_xid;
89 atomic_t xid_not_found;
91 atomic_t seq_not_found;
92 atomic_t non_bls_resp;
94 struct fc_exch **exches; /* for exch pointers indexed by xid */
96 #define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
98 static void fc_exch_rrq(struct fc_exch *);
99 static void fc_seq_ls_acc(struct fc_seq *);
100 static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason,
101 enum fc_els_rjt_explan);
102 static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *);
103 static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *);
104 static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp);
107 * Internal implementation notes.
109 * The exchange manager is one by default in libfc but LLD may choose
110 * to have one per CPU. The sequence manager is one per exchange manager
111 * and currently never separated.
113 * Section 9.8 in FC-FS-2 specifies: "The SEQ_ID is a one-byte field
114 * assigned by the Sequence Initiator that shall be unique for a specific
115 * D_ID and S_ID pair while the Sequence is open." Note that it isn't
116 * qualified by exchange ID, which one might think it would be.
117 * In practice this limits the number of open sequences and exchanges to 256
118 * per session. For most targets we could treat this limit as per exchange.
120 * The exchange and its sequence are freed when the last sequence is received.
121 * It's possible for the remote port to leave an exchange open without
122 * sending any sequences.
124 * Notes on reference counts:
126 * Exchanges are reference counted and exchange gets freed when the reference
127 * count becomes zero.
130 * Sequences are timed out for E_D_TOV and R_A_TOV.
132 * Sequence event handling:
134 * The following events may occur on initiator sequences:
137 * For now, the whole thing is sent.
139 * This applies only to class F.
140 * The sequence is marked complete.
142 * The upper layer calls fc_exch_done() when done
143 * with exchange and sequence tuple.
144 * RX-inferred completion.
145 * When we receive the next sequence on the same exchange, we can
146 * retire the previous sequence ID. (XXX not implemented).
148 * R_A_TOV frees the sequence ID. If we're waiting for ACK,
149 * E_D_TOV causes abort and calls upper layer response handler
150 * with FC_EX_TIMEOUT error.
156 * The following events may occur on recipient sequences:
159 * Allocate sequence for first frame received.
160 * Hold during receive handler.
161 * Release when final frame received.
162 * Keep status of last N of these for the ELS RES command. XXX TBD.
164 * Deallocate sequence
168 * For now, we neglect conditions where only part of a sequence was
169 * received or transmitted, or where out-of-order receipt is detected.
175 * The EM code run in a per-CPU worker thread.
177 * To protect against concurrency between a worker thread code and timers,
178 * sequence allocation and deallocation must be locked.
179 * - exchange refcnt can be done atomicly without locks.
180 * - sequence allocation must be locked by exch lock.
181 * - If the em_lock and ex_lock must be taken at the same time, then the
182 * em_lock must be taken before the ex_lock.
186 * opcode names for debugging.
188 static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
190 #define FC_TABLE_SIZE(x) (sizeof(x) / sizeof(x[0]))
192 static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
193 unsigned int max_index)
195 const char *name = NULL;
204 static const char *fc_exch_rctl_name(unsigned int op)
206 return fc_exch_name_lookup(op, fc_exch_rctl_names,
207 FC_TABLE_SIZE(fc_exch_rctl_names));
211 * Hold an exchange - keep it from being freed.
213 static void fc_exch_hold(struct fc_exch *ep)
215 atomic_inc(&ep->ex_refcnt);
219 * setup fc hdr by initializing few more FC header fields and sof/eof.
220 * Initialized fields by this func:
221 * - fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt
224 static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
227 struct fc_frame_header *fh = fc_frame_header_get(fp);
230 fr_sof(fp) = ep->class;
232 fr_sof(fp) = fc_sof_normal(ep->class);
234 if (f_ctl & FC_FC_END_SEQ) {
235 fr_eof(fp) = FC_EOF_T;
236 if (fc_sof_needs_ack(ep->class))
237 fr_eof(fp) = FC_EOF_N;
240 * The number of fill bytes to make the length a 4-byte
241 * multiple is the low order 2-bits of the f_ctl.
242 * The fill itself will have been cleared by the frame
244 * After this, the length will be even, as expected by
247 fill = fr_len(fp) & 3;
250 /* TODO, this may be a problem with fragmented skb */
251 skb_put(fp_skb(fp), fill);
252 hton24(fh->fh_f_ctl, f_ctl | fill);
255 WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
256 fr_eof(fp) = FC_EOF_N;
260 * Initialize remainig fh fields
261 * from fc_fill_fc_hdr
263 fh->fh_ox_id = htons(ep->oxid);
264 fh->fh_rx_id = htons(ep->rxid);
265 fh->fh_seq_id = ep->seq.id;
266 fh->fh_seq_cnt = htons(ep->seq.cnt);
271 * Release a reference to an exchange.
272 * If the refcnt goes to zero and the exchange is complete, it is freed.
274 static void fc_exch_release(struct fc_exch *ep)
276 struct fc_exch_mgr *mp;
278 if (atomic_dec_and_test(&ep->ex_refcnt)) {
281 ep->destructor(&ep->seq, ep->arg);
282 if (ep->lp->tt.exch_put)
283 ep->lp->tt.exch_put(ep->lp, mp, ep->xid);
284 WARN_ON(!ep->esb_stat & ESB_ST_COMPLETE);
285 mempool_free(ep, mp->ep_pool);
289 static int fc_exch_done_locked(struct fc_exch *ep)
294 * We must check for completion in case there are two threads
295 * tyring to complete this. But the rrq code will reuse the
296 * ep, and in that case we only clear the resp and set it as
297 * complete, so it can be reused by the timer to send the rrq.
300 if (ep->state & FC_EX_DONE)
302 ep->esb_stat |= ESB_ST_COMPLETE;
304 if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
305 ep->state |= FC_EX_DONE;
306 if (cancel_delayed_work(&ep->timeout_work))
307 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
313 static void fc_exch_mgr_delete_ep(struct fc_exch *ep)
315 struct fc_exch_mgr *mp;
318 spin_lock_bh(&mp->em_lock);
319 WARN_ON(mp->total_exches <= 0);
321 mp->exches[ep->xid - mp->min_xid] = NULL;
322 list_del(&ep->ex_list);
323 spin_unlock_bh(&mp->em_lock);
324 fc_exch_release(ep); /* drop hold for exch in mp */
328 * Internal version of fc_exch_timer_set - used with lock held.
330 static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
331 unsigned int timer_msec)
333 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
336 FC_DEBUG_EXCH("Exchange (%4x) timed out, notifying the upper layer\n",
338 if (schedule_delayed_work(&ep->timeout_work,
339 msecs_to_jiffies(timer_msec)))
340 fc_exch_hold(ep); /* hold for timer */
344 * Set timer for an exchange.
345 * The time is a minimum delay in milliseconds until the timer fires.
346 * Used for upper level protocols to time out the exchange.
347 * The timer is cancelled when it fires or when the exchange completes.
348 * Returns non-zero if a timer couldn't be allocated.
350 static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
352 spin_lock_bh(&ep->ex_lock);
353 fc_exch_timer_set_locked(ep, timer_msec);
354 spin_unlock_bh(&ep->ex_lock);
357 int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec)
364 ep = fc_seq_exch(req_sp);
366 spin_lock_bh(&ep->ex_lock);
367 if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
368 ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
369 spin_unlock_bh(&ep->ex_lock);
374 * Send the abort on a new sequence if possible.
376 sp = fc_seq_start_next_locked(&ep->seq);
378 spin_unlock_bh(&ep->ex_lock);
382 ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
384 fc_exch_timer_set_locked(ep, timer_msec);
385 spin_unlock_bh(&ep->ex_lock);
388 * If not logged into the fabric, don't send ABTS but leave
389 * sequence active until next timeout.
395 * Send an abort for the sequence that timed out.
397 fp = fc_frame_alloc(ep->lp, 0);
399 fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
400 FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
401 error = fc_seq_send(ep->lp, sp, fp);
406 EXPORT_SYMBOL(fc_seq_exch_abort);
409 * Exchange timeout - handle exchange timer expiration.
410 * The timer will have been cancelled before this is called.
412 static void fc_exch_timeout(struct work_struct *work)
414 struct fc_exch *ep = container_of(work, struct fc_exch,
416 struct fc_seq *sp = &ep->seq;
417 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
422 spin_lock_bh(&ep->ex_lock);
423 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
426 e_stat = ep->esb_stat;
427 if (e_stat & ESB_ST_COMPLETE) {
428 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
429 if (e_stat & ESB_ST_REC_QUAL)
431 spin_unlock_bh(&ep->ex_lock);
437 if (e_stat & ESB_ST_ABNORMAL)
438 rc = fc_exch_done_locked(ep);
439 spin_unlock_bh(&ep->ex_lock);
441 fc_exch_mgr_delete_ep(ep);
443 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
444 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
448 spin_unlock_bh(&ep->ex_lock);
451 * This release matches the hold taken when the timer was set.
457 * Allocate a sequence.
459 * We don't support multiple originated sequences on the same exchange.
460 * By implication, any previously originated sequence on this exchange
461 * is complete, and we reallocate the same sequence.
463 static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
475 * fc_em_alloc_xid - returns an xid based on request type
476 * @lp : ptr to associated lport
477 * @fp : ptr to the assocated frame
479 * check the associated fc_fsp_pkt to get scsi command type and
480 * command direction to decide from which range this exch id
481 * will be allocated from.
483 * Returns : 0 or an valid xid
485 static u16 fc_em_alloc_xid(struct fc_exch_mgr *mp, const struct fc_frame *fp)
489 struct fc_exch *ep = NULL;
492 if (fc_frame_is_read(fp)) {
495 plast = &mp->last_read;
497 min = mp->max_read + 1;
499 plast = &mp->last_xid;
504 plast = &mp->last_xid;
508 xid = (xid == max) ? min : xid + 1;
509 ep = mp->exches[xid - mp->min_xid];
510 } while ((ep != NULL) && (xid != *plast));
521 * fc_exch_alloc - allocate an exchange.
522 * @mp : ptr to the exchange manager
525 * if xid is supplied zero then assign next free exchange ID
526 * from exchange manager, otherwise use supplied xid.
527 * Returns with exch lock held.
529 struct fc_exch *fc_exch_alloc(struct fc_exch_mgr *mp,
530 struct fc_frame *fp, u16 xid)
534 /* allocate memory for exchange */
535 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
537 atomic_inc(&mp->stats.no_free_exch);
540 memset(ep, 0, sizeof(*ep));
542 spin_lock_bh(&mp->em_lock);
543 /* alloc xid if input xid 0 */
545 /* alloc a new xid */
546 xid = fc_em_alloc_xid(mp, fp);
548 printk(KERN_ERR "fc_em_alloc_xid() failed\n");
553 fc_exch_hold(ep); /* hold for exch in mp */
554 spin_lock_init(&ep->ex_lock);
556 * Hold exch lock for caller to prevent fc_exch_reset()
557 * from releasing exch while fc_exch_alloc() caller is
558 * still working on exch.
560 spin_lock_bh(&ep->ex_lock);
562 mp->exches[xid - mp->min_xid] = ep;
563 list_add_tail(&ep->ex_list, &mp->ex_list);
564 fc_seq_alloc(ep, ep->seq_id++);
566 spin_unlock_bh(&mp->em_lock);
571 ep->oxid = ep->xid = xid;
574 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
575 ep->rxid = FC_XID_UNKNOWN;
576 ep->class = mp->class;
577 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
581 spin_unlock_bh(&mp->em_lock);
582 atomic_inc(&mp->stats.no_free_exch_xid);
583 mempool_free(ep, mp->ep_pool);
586 EXPORT_SYMBOL(fc_exch_alloc);
589 * Lookup and hold an exchange.
591 static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
593 struct fc_exch *ep = NULL;
595 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
596 spin_lock_bh(&mp->em_lock);
597 ep = mp->exches[xid - mp->min_xid];
600 WARN_ON(ep->xid != xid);
602 spin_unlock_bh(&mp->em_lock);
607 void fc_exch_done(struct fc_seq *sp)
609 struct fc_exch *ep = fc_seq_exch(sp);
612 spin_lock_bh(&ep->ex_lock);
613 rc = fc_exch_done_locked(ep);
614 spin_unlock_bh(&ep->ex_lock);
616 fc_exch_mgr_delete_ep(ep);
618 EXPORT_SYMBOL(fc_exch_done);
621 * Allocate a new exchange as responder.
622 * Sets the responder ID in the frame header.
624 static struct fc_exch *fc_exch_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
627 struct fc_frame_header *fh;
630 ep = mp->lp->tt.exch_get(mp->lp, fp);
632 ep->class = fc_frame_class(fp);
635 * Set EX_CTX indicating we're responding on this exchange.
637 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
638 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
639 fh = fc_frame_header_get(fp);
640 ep->sid = ntoh24(fh->fh_d_id);
641 ep->did = ntoh24(fh->fh_s_id);
645 * Allocated exchange has placed the XID in the
646 * originator field. Move it to the responder field,
647 * and set the originator XID from the frame.
650 ep->oxid = ntohs(fh->fh_ox_id);
651 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
652 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
653 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
656 * Set the responder ID in the frame header.
657 * The old one should've been 0xffff.
658 * If it isn't, don't assign one.
659 * Incoming basic link service frames may specify
660 * a referenced RX_ID.
662 if (fh->fh_type != FC_TYPE_BLS) {
663 rxid = ntohs(fh->fh_rx_id);
664 WARN_ON(rxid != FC_XID_UNKNOWN);
665 fh->fh_rx_id = htons(ep->rxid);
667 fc_exch_hold(ep); /* hold for caller */
668 spin_unlock_bh(&ep->ex_lock); /* lock from exch_get */
674 * Find a sequence for receive where the other end is originating the sequence.
675 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
676 * on the ep that should be released by the caller.
678 static enum fc_pf_rjt_reason
679 fc_seq_lookup_recip(struct fc_exch_mgr *mp, struct fc_frame *fp)
681 struct fc_frame_header *fh = fc_frame_header_get(fp);
682 struct fc_exch *ep = NULL;
683 struct fc_seq *sp = NULL;
684 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
688 f_ctl = ntoh24(fh->fh_f_ctl);
689 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
692 * Lookup or create the exchange if we will be creating the sequence.
694 if (f_ctl & FC_FC_EX_CTX) {
695 xid = ntohs(fh->fh_ox_id); /* we originated exch */
696 ep = fc_exch_find(mp, xid);
698 atomic_inc(&mp->stats.xid_not_found);
699 reject = FC_RJT_OX_ID;
702 if (ep->rxid == FC_XID_UNKNOWN)
703 ep->rxid = ntohs(fh->fh_rx_id);
704 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
705 reject = FC_RJT_OX_ID;
709 xid = ntohs(fh->fh_rx_id); /* we are the responder */
712 * Special case for MDS issuing an ELS TEST with a
714 * XXX take this out once we do the proper reject.
716 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
717 fc_frame_payload_op(fp) == ELS_TEST) {
718 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
719 xid = FC_XID_UNKNOWN;
723 * new sequence - find the exchange
725 ep = fc_exch_find(mp, xid);
726 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
728 atomic_inc(&mp->stats.xid_busy);
729 reject = FC_RJT_RX_ID;
732 ep = fc_exch_resp(mp, fp);
734 reject = FC_RJT_EXCH_EST; /* XXX */
737 xid = ep->xid; /* get our XID */
739 atomic_inc(&mp->stats.xid_not_found);
740 reject = FC_RJT_RX_ID; /* XID not found */
746 * At this point, we have the exchange held.
747 * Find or create the sequence.
749 if (fc_sof_is_init(fr_sof(fp))) {
750 sp = fc_seq_start_next(&ep->seq);
752 reject = FC_RJT_SEQ_XS; /* exchange shortage */
755 sp->id = fh->fh_seq_id;
756 sp->ssb_stat |= SSB_ST_RESP;
759 if (sp->id != fh->fh_seq_id) {
760 atomic_inc(&mp->stats.seq_not_found);
761 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
765 WARN_ON(ep != fc_seq_exch(sp));
767 if (f_ctl & FC_FC_SEQ_INIT)
768 ep->esb_stat |= ESB_ST_SEQ_INIT;
774 fc_exch_done(&ep->seq);
775 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
780 * Find the sequence for a frame being received.
781 * We originated the sequence, so it should be found.
782 * We may or may not have originated the exchange.
783 * Does not hold the sequence for the caller.
785 static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
788 struct fc_frame_header *fh = fc_frame_header_get(fp);
790 struct fc_seq *sp = NULL;
794 f_ctl = ntoh24(fh->fh_f_ctl);
795 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
796 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
797 ep = fc_exch_find(mp, xid);
800 if (ep->seq.id == fh->fh_seq_id) {
802 * Save the RX_ID if we didn't previously know it.
805 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
806 ep->rxid == FC_XID_UNKNOWN) {
807 ep->rxid = ntohs(fh->fh_rx_id);
815 * Set addresses for an exchange.
816 * Note this must be done before the first sequence of the exchange is sent.
818 static void fc_exch_set_addr(struct fc_exch *ep,
819 u32 orig_id, u32 resp_id)
822 if (ep->esb_stat & ESB_ST_RESP) {
831 static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
833 struct fc_exch *ep = fc_seq_exch(sp);
835 sp = fc_seq_alloc(ep, ep->seq_id++);
836 FC_DEBUG_EXCH("exch %4x f_ctl %6x seq %2x\n",
837 ep->xid, ep->f_ctl, sp->id);
841 * Allocate a new sequence on the same exchange as the supplied sequence.
842 * This will never return NULL.
844 struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
846 struct fc_exch *ep = fc_seq_exch(sp);
848 spin_lock_bh(&ep->ex_lock);
849 WARN_ON((ep->esb_stat & ESB_ST_COMPLETE) != 0);
850 sp = fc_seq_start_next_locked(sp);
851 spin_unlock_bh(&ep->ex_lock);
855 EXPORT_SYMBOL(fc_seq_start_next);
857 int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
860 struct fc_frame_header *fh = fc_frame_header_get(fp);
864 ep = fc_seq_exch(sp);
865 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
867 f_ctl = ntoh24(fh->fh_f_ctl);
868 fc_exch_setup_hdr(ep, fp, f_ctl);
871 * update sequence count if this frame is carrying
872 * multiple FC frames when sequence offload is enabled
875 if (fr_max_payload(fp))
876 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
884 error = lp->tt.frame_send(lp, fp);
887 * Update the exchange and sequence flags,
888 * assuming all frames for the sequence have been sent.
889 * We can only be called to send once for each sequence.
891 spin_lock_bh(&ep->ex_lock);
892 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
893 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
894 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
895 spin_unlock_bh(&ep->ex_lock);
898 EXPORT_SYMBOL(fc_seq_send);
900 void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
901 struct fc_seq_els_data *els_data)
905 fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
911 fc_exch_els_rrq(sp, els_data->fp);
914 fc_exch_els_rec(sp, els_data->fp);
917 FC_DBG("Invalid ELS CMD:%x\n", els_cmd);
920 EXPORT_SYMBOL(fc_seq_els_rsp_send);
923 * Send a sequence, which is also the last sequence in the exchange.
925 static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
926 enum fc_rctl rctl, enum fc_fh_type fh_type)
929 struct fc_exch *ep = fc_seq_exch(sp);
931 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
933 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
934 fc_seq_send(ep->lp, sp, fp);
938 * Send ACK_1 (or equiv.) indicating we received something.
939 * The frame we're acking is supplied.
941 static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
944 struct fc_frame_header *rx_fh;
945 struct fc_frame_header *fh;
946 struct fc_exch *ep = fc_seq_exch(sp);
947 struct fc_lport *lp = ep->lp;
951 * Don't send ACKs for class 3.
953 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
954 fp = fc_frame_alloc(lp, 0);
958 fh = fc_frame_header_get(fp);
959 fh->fh_r_ctl = FC_RCTL_ACK_1;
960 fh->fh_type = FC_TYPE_BLS;
963 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
964 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
965 * Bits 9-8 are meaningful (retransmitted or unidirectional).
966 * Last ACK uses bits 7-6 (continue sequence),
967 * bits 5-4 are meaningful (what kind of ACK to use).
969 rx_fh = fc_frame_header_get(rx_fp);
970 f_ctl = ntoh24(rx_fh->fh_f_ctl);
971 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
972 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
973 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
974 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
975 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
976 hton24(fh->fh_f_ctl, f_ctl);
978 fc_exch_setup_hdr(ep, fp, f_ctl);
979 fh->fh_seq_id = rx_fh->fh_seq_id;
980 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
981 fh->fh_parm_offset = htonl(1); /* ack single frame */
983 fr_sof(fp) = fr_sof(rx_fp);
984 if (f_ctl & FC_FC_END_SEQ)
985 fr_eof(fp) = FC_EOF_T;
987 fr_eof(fp) = FC_EOF_N;
989 (void) lp->tt.frame_send(lp, fp);
995 * This is for rejecting BA_ABTS only.
998 fc_exch_send_ba_rjt(struct fc_frame *rx_fp, enum fc_ba_rjt_reason reason,
999 enum fc_ba_rjt_explan explan)
1001 struct fc_frame *fp;
1002 struct fc_frame_header *rx_fh;
1003 struct fc_frame_header *fh;
1004 struct fc_ba_rjt *rp;
1005 struct fc_lport *lp;
1009 fp = fc_frame_alloc(lp, sizeof(*rp));
1012 fh = fc_frame_header_get(fp);
1013 rx_fh = fc_frame_header_get(rx_fp);
1015 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
1017 rp = fc_frame_payload_get(fp, sizeof(*rp));
1018 rp->br_reason = reason;
1019 rp->br_explan = explan;
1022 * seq_id, cs_ctl, df_ctl and param/offset are zero.
1024 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
1025 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
1026 fh->fh_ox_id = rx_fh->fh_rx_id;
1027 fh->fh_rx_id = rx_fh->fh_ox_id;
1028 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1029 fh->fh_r_ctl = FC_RCTL_BA_RJT;
1030 fh->fh_type = FC_TYPE_BLS;
1033 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
1034 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
1035 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1036 * Last ACK uses bits 7-6 (continue sequence),
1037 * bits 5-4 are meaningful (what kind of ACK to use).
1038 * Always set LAST_SEQ, END_SEQ.
1040 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1041 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1042 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1043 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1044 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1045 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1046 f_ctl &= ~FC_FC_FIRST_SEQ;
1047 hton24(fh->fh_f_ctl, f_ctl);
1049 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1050 fr_eof(fp) = FC_EOF_T;
1051 if (fc_sof_needs_ack(fr_sof(fp)))
1052 fr_eof(fp) = FC_EOF_N;
1054 (void) lp->tt.frame_send(lp, fp);
1058 * Handle an incoming ABTS. This would be for target mode usually,
1059 * but could be due to lost FCP transfer ready, confirm or RRQ.
1060 * We always handle this as an exchange abort, ignoring the parameter.
1062 static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1064 struct fc_frame *fp;
1065 struct fc_ba_acc *ap;
1066 struct fc_frame_header *fh;
1071 spin_lock_bh(&ep->ex_lock);
1072 if (ep->esb_stat & ESB_ST_COMPLETE) {
1073 spin_unlock_bh(&ep->ex_lock);
1076 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1077 fc_exch_hold(ep); /* hold for REC_QUAL */
1078 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1079 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1081 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1083 spin_unlock_bh(&ep->ex_lock);
1086 fh = fc_frame_header_get(fp);
1087 ap = fc_frame_payload_get(fp, sizeof(*ap));
1088 memset(ap, 0, sizeof(*ap));
1090 ap->ba_high_seq_cnt = htons(0xffff);
1091 if (sp->ssb_stat & SSB_ST_RESP) {
1092 ap->ba_seq_id = sp->id;
1093 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1094 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1095 ap->ba_low_seq_cnt = htons(sp->cnt);
1097 sp = fc_seq_start_next_locked(sp);
1098 spin_unlock_bh(&ep->ex_lock);
1099 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1100 fc_frame_free(rx_fp);
1104 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1106 fc_frame_free(rx_fp);
1110 * Handle receive where the other end is originating the sequence.
1112 static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1113 struct fc_frame *fp)
1115 struct fc_frame_header *fh = fc_frame_header_get(fp);
1116 struct fc_seq *sp = NULL;
1117 struct fc_exch *ep = NULL;
1121 enum fc_pf_rjt_reason reject;
1124 reject = fc_seq_lookup_recip(mp, fp);
1125 if (reject == FC_RJT_NONE) {
1126 sp = fr_seq(fp); /* sequence will be held */
1127 ep = fc_seq_exch(sp);
1130 f_ctl = ntoh24(fh->fh_f_ctl);
1131 fc_seq_send_ack(sp, fp);
1134 * Call the receive function.
1136 * The receive function may allocate a new sequence
1137 * over the old one, so we shouldn't change the
1138 * sequence after this.
1140 * The frame will be freed by the receive function.
1141 * If new exch resp handler is valid then call that
1145 ep->resp(sp, fp, ep->arg);
1147 lp->tt.lport_recv(lp, sp, fp);
1148 fc_exch_release(ep); /* release from lookup */
1150 FC_DEBUG_EXCH("exch/seq lookup failed: reject %x\n", reject);
1156 * Handle receive where the other end is originating the sequence in
1157 * response to our exchange.
1159 static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1161 struct fc_frame_header *fh = fc_frame_header_get(fp);
1166 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1170 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1172 atomic_inc(&mp->stats.xid_not_found);
1175 if (ep->rxid == FC_XID_UNKNOWN)
1176 ep->rxid = ntohs(fh->fh_rx_id);
1177 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1178 atomic_inc(&mp->stats.xid_not_found);
1181 if (ep->did != ntoh24(fh->fh_s_id) &&
1182 ep->did != FC_FID_FLOGI) {
1183 atomic_inc(&mp->stats.xid_not_found);
1187 if (fc_sof_is_init(sof)) {
1188 sp = fc_seq_start_next(&ep->seq);
1189 sp->id = fh->fh_seq_id;
1190 sp->ssb_stat |= SSB_ST_RESP;
1193 if (sp->id != fh->fh_seq_id) {
1194 atomic_inc(&mp->stats.seq_not_found);
1198 f_ctl = ntoh24(fh->fh_f_ctl);
1200 if (f_ctl & FC_FC_SEQ_INIT)
1201 ep->esb_stat |= ESB_ST_SEQ_INIT;
1203 if (fc_sof_needs_ack(sof))
1204 fc_seq_send_ack(sp, fp);
1206 ex_resp_arg = ep->arg;
1208 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1209 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1210 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1211 spin_lock_bh(&ep->ex_lock);
1212 rc = fc_exch_done_locked(ep);
1213 WARN_ON(fc_seq_exch(sp) != ep);
1214 spin_unlock_bh(&ep->ex_lock);
1216 fc_exch_mgr_delete_ep(ep);
1220 * Call the receive function.
1221 * The sequence is held (has a refcnt) for us,
1222 * but not for the receive function.
1224 * The receive function may allocate a new sequence
1225 * over the old one, so we shouldn't change the
1226 * sequence after this.
1228 * The frame will be freed by the receive function.
1229 * If new exch resp handler is valid then call that
1233 resp(sp, fp, ex_resp_arg);
1236 fc_exch_release(ep);
1239 fc_exch_release(ep);
1245 * Handle receive for a sequence where other end is responding to our sequence.
1247 static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1251 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
1253 atomic_inc(&mp->stats.xid_not_found);
1254 FC_DEBUG_EXCH("seq lookup failed\n");
1256 atomic_inc(&mp->stats.non_bls_resp);
1257 FC_DEBUG_EXCH("non-BLS response to sequence");
1263 * Handle the response to an ABTS for exchange or sequence.
1264 * This can be BA_ACC or BA_RJT.
1266 static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1268 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1270 struct fc_frame_header *fh;
1271 struct fc_ba_acc *ap;
1275 int rc = 1, has_rec = 0;
1277 fh = fc_frame_header_get(fp);
1278 FC_DEBUG_EXCH("exch: BLS rctl %x - %s\n",
1279 fh->fh_r_ctl, fc_exch_rctl_name(fh->fh_r_ctl));
1281 if (cancel_delayed_work_sync(&ep->timeout_work))
1282 fc_exch_release(ep); /* release from pending timer hold */
1284 spin_lock_bh(&ep->ex_lock);
1285 switch (fh->fh_r_ctl) {
1286 case FC_RCTL_BA_ACC:
1287 ap = fc_frame_payload_get(fp, sizeof(*ap));
1292 * Decide whether to establish a Recovery Qualifier.
1293 * We do this if there is a non-empty SEQ_CNT range and
1294 * SEQ_ID is the same as the one we aborted.
1296 low = ntohs(ap->ba_low_seq_cnt);
1297 high = ntohs(ap->ba_high_seq_cnt);
1298 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1299 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1300 ap->ba_seq_id == ep->seq_id) && low != high) {
1301 ep->esb_stat |= ESB_ST_REC_QUAL;
1302 fc_exch_hold(ep); /* hold for recovery qualifier */
1306 case FC_RCTL_BA_RJT:
1313 ex_resp_arg = ep->arg;
1315 /* do we need to do some other checks here. Can we reuse more of
1316 * fc_exch_recv_seq_resp
1320 * do we want to check END_SEQ as well as LAST_SEQ here?
1322 if (ep->fh_type != FC_TYPE_FCP &&
1323 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1324 rc = fc_exch_done_locked(ep);
1325 spin_unlock_bh(&ep->ex_lock);
1327 fc_exch_mgr_delete_ep(ep);
1330 resp(sp, fp, ex_resp_arg);
1335 fc_exch_timer_set(ep, ep->r_a_tov);
1340 * Receive BLS sequence.
1341 * This is always a sequence initiated by the remote side.
1342 * We may be either the originator or recipient of the exchange.
1344 static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1346 struct fc_frame_header *fh;
1350 fh = fc_frame_header_get(fp);
1351 f_ctl = ntoh24(fh->fh_f_ctl);
1354 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1355 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1356 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1357 spin_lock_bh(&ep->ex_lock);
1358 ep->esb_stat |= ESB_ST_SEQ_INIT;
1359 spin_unlock_bh(&ep->ex_lock);
1361 if (f_ctl & FC_FC_SEQ_CTX) {
1363 * A response to a sequence we initiated.
1364 * This should only be ACKs for class 2 or F.
1366 switch (fh->fh_r_ctl) {
1371 FC_DEBUG_EXCH("BLS rctl %x - %s received",
1373 fc_exch_rctl_name(fh->fh_r_ctl));
1378 switch (fh->fh_r_ctl) {
1379 case FC_RCTL_BA_RJT:
1380 case FC_RCTL_BA_ACC:
1382 fc_exch_abts_resp(ep, fp);
1386 case FC_RCTL_BA_ABTS:
1387 fc_exch_recv_abts(ep, fp);
1389 default: /* ignore junk */
1395 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1399 * Accept sequence with LS_ACC.
1400 * If this fails due to allocation or transmit congestion, assume the
1401 * originator will repeat the sequence.
1403 static void fc_seq_ls_acc(struct fc_seq *req_sp)
1406 struct fc_els_ls_acc *acc;
1407 struct fc_frame *fp;
1409 sp = fc_seq_start_next(req_sp);
1410 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1412 acc = fc_frame_payload_get(fp, sizeof(*acc));
1413 memset(acc, 0, sizeof(*acc));
1414 acc->la_cmd = ELS_LS_ACC;
1415 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1420 * Reject sequence with ELS LS_RJT.
1421 * If this fails due to allocation or transmit congestion, assume the
1422 * originator will repeat the sequence.
1424 static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
1425 enum fc_els_rjt_explan explan)
1428 struct fc_els_ls_rjt *rjt;
1429 struct fc_frame *fp;
1431 sp = fc_seq_start_next(req_sp);
1432 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
1434 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1435 memset(rjt, 0, sizeof(*rjt));
1436 rjt->er_cmd = ELS_LS_RJT;
1437 rjt->er_reason = reason;
1438 rjt->er_explan = explan;
1439 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1443 static void fc_exch_reset(struct fc_exch *ep)
1446 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1450 spin_lock_bh(&ep->ex_lock);
1451 ep->state |= FC_EX_RST_CLEANUP;
1453 * we really want to call del_timer_sync, but cannot due
1454 * to the lport calling with the lport lock held (some resp
1455 * functions can also grab the lport lock which could cause
1458 if (cancel_delayed_work(&ep->timeout_work))
1459 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1462 if (ep->esb_stat & ESB_ST_REC_QUAL)
1463 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1464 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1467 rc = fc_exch_done_locked(ep);
1468 spin_unlock_bh(&ep->ex_lock);
1470 fc_exch_mgr_delete_ep(ep);
1473 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1477 * Reset an exchange manager, releasing all sequences and exchanges.
1478 * If sid is non-zero, reset only exchanges we source from that FID.
1479 * If did is non-zero, reset only exchanges destined to that FID.
1481 void fc_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
1484 struct fc_exch *next;
1485 struct fc_exch_mgr *mp = lp->emp;
1487 spin_lock_bh(&mp->em_lock);
1489 list_for_each_entry_safe(ep, next, &mp->ex_list, ex_list) {
1490 if ((sid == 0 || sid == ep->sid) &&
1491 (did == 0 || did == ep->did)) {
1493 spin_unlock_bh(&mp->em_lock);
1497 fc_exch_release(ep);
1498 spin_lock_bh(&mp->em_lock);
1501 * must restart loop incase while lock was down
1502 * multiple eps were released.
1507 spin_unlock_bh(&mp->em_lock);
1509 EXPORT_SYMBOL(fc_exch_mgr_reset);
1512 * Handle incoming ELS REC - Read Exchange Concise.
1513 * Note that the requesting port may be different than the S_ID in the request.
1515 static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
1517 struct fc_frame *fp;
1519 struct fc_exch_mgr *em;
1520 struct fc_els_rec *rp;
1521 struct fc_els_rec_acc *acc;
1522 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1523 enum fc_els_rjt_explan explan;
1528 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1529 explan = ELS_EXPL_INV_LEN;
1532 sid = ntoh24(rp->rec_s_id);
1533 rxid = ntohs(rp->rec_rx_id);
1534 oxid = ntohs(rp->rec_ox_id);
1537 * Currently it's hard to find the local S_ID from the exchange
1538 * manager. This will eventually be fixed, but for now it's easier
1539 * to lookup the subject exchange twice, once as if we were
1540 * the initiator, and then again if we weren't.
1542 em = fc_seq_exch(sp)->em;
1543 ep = fc_exch_find(em, oxid);
1544 explan = ELS_EXPL_OXID_RXID;
1545 if (ep && ep->oid == sid) {
1546 if (ep->rxid != FC_XID_UNKNOWN &&
1547 rxid != FC_XID_UNKNOWN &&
1552 fc_exch_release(ep);
1554 if (rxid != FC_XID_UNKNOWN)
1555 ep = fc_exch_find(em, rxid);
1560 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1565 sp = fc_seq_start_next(sp);
1566 acc = fc_frame_payload_get(fp, sizeof(*acc));
1567 memset(acc, 0, sizeof(*acc));
1568 acc->reca_cmd = ELS_LS_ACC;
1569 acc->reca_ox_id = rp->rec_ox_id;
1570 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1571 acc->reca_rx_id = htons(ep->rxid);
1572 if (ep->sid == ep->oid)
1573 hton24(acc->reca_rfid, ep->did);
1575 hton24(acc->reca_rfid, ep->sid);
1576 acc->reca_fc4value = htonl(ep->seq.rec_data);
1577 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1580 sp = fc_seq_start_next(sp);
1581 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1583 fc_exch_release(ep);
1588 fc_exch_release(ep);
1590 fc_seq_ls_rjt(sp, reason, explan);
1595 * Handle response from RRQ.
1596 * Not much to do here, really.
1597 * Should report errors.
1599 * TODO: fix error handler.
1601 static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1603 struct fc_exch *aborted_ep = arg;
1607 int err = PTR_ERR(fp);
1609 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
1611 FC_DBG("Cannot process RRQ, because of frame error %d\n", err);
1615 op = fc_frame_payload_op(fp);
1620 FC_DBG("LS_RJT for RRQ");
1625 FC_DBG("unexpected response op %x for RRQ", op);
1630 fc_exch_done(&aborted_ep->seq);
1631 /* drop hold for rec qual */
1632 fc_exch_release(aborted_ep);
1636 * Send ELS RRQ - Reinstate Recovery Qualifier.
1637 * This tells the remote port to stop blocking the use of
1638 * the exchange and the seq_cnt range.
1640 static void fc_exch_rrq(struct fc_exch *ep)
1642 struct fc_lport *lp;
1643 struct fc_els_rrq *rrq;
1644 struct fc_frame *fp;
1645 struct fc_seq *rrq_sp;
1650 fp = fc_frame_alloc(lp, sizeof(*rrq));
1653 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1654 memset(rrq, 0, sizeof(*rrq));
1655 rrq->rrq_cmd = ELS_RRQ;
1656 hton24(rrq->rrq_s_id, ep->sid);
1657 rrq->rrq_ox_id = htons(ep->oxid);
1658 rrq->rrq_rx_id = htons(ep->rxid);
1661 if (ep->esb_stat & ESB_ST_RESP)
1664 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1665 fc_host_port_id(lp->host), FC_TYPE_ELS,
1666 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1668 rrq_sp = fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep,
1671 ep->esb_stat |= ESB_ST_REC_QUAL;
1672 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1679 * Handle incoming ELS RRQ - Reset Recovery Qualifier.
1681 static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1683 struct fc_exch *ep; /* request or subject exchange */
1684 struct fc_els_rrq *rp;
1687 enum fc_els_rjt_explan explan;
1689 rp = fc_frame_payload_get(fp, sizeof(*rp));
1690 explan = ELS_EXPL_INV_LEN;
1695 * lookup subject exchange.
1697 ep = fc_seq_exch(sp);
1698 sid = ntoh24(rp->rrq_s_id); /* subject source */
1699 xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
1700 ep = fc_exch_find(ep->em, xid);
1702 explan = ELS_EXPL_OXID_RXID;
1705 spin_lock_bh(&ep->ex_lock);
1706 if (ep->oxid != ntohs(rp->rrq_ox_id))
1708 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
1709 ep->rxid != FC_XID_UNKNOWN)
1711 explan = ELS_EXPL_SID;
1716 * Clear Recovery Qualifier state, and cancel timer if complete.
1718 if (ep->esb_stat & ESB_ST_REC_QUAL) {
1719 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1720 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
1722 if (ep->esb_stat & ESB_ST_COMPLETE) {
1723 if (cancel_delayed_work(&ep->timeout_work))
1724 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
1727 spin_unlock_bh(&ep->ex_lock);
1737 spin_unlock_bh(&ep->ex_lock);
1738 fc_exch_release(ep); /* drop hold from fc_exch_find */
1740 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
1744 struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1745 enum fc_class class,
1746 u16 min_xid, u16 max_xid)
1748 struct fc_exch_mgr *mp;
1751 if (max_xid <= min_xid || min_xid == 0 || max_xid == FC_XID_UNKNOWN) {
1752 FC_DBG("Invalid min_xid 0x:%x and max_xid 0x:%x\n",
1758 * Memory need for EM
1760 #define xid_ok(i, m1, m2) (((i) >= (m1)) && ((i) <= (m2)))
1761 len = (max_xid - min_xid + 1) * (sizeof(struct fc_exch *));
1762 len += sizeof(struct fc_exch_mgr);
1764 mp = kzalloc(len, GFP_ATOMIC);
1769 mp->total_exches = 0;
1770 mp->exches = (struct fc_exch **)(mp + 1);
1772 /* adjust em exch xid range for offload */
1773 mp->min_xid = min_xid;
1774 mp->max_xid = max_xid;
1775 mp->last_xid = min_xid - 1;
1778 if (lp->lro_enabled && xid_ok(lp->lro_xid, min_xid, max_xid)) {
1779 mp->max_read = lp->lro_xid;
1780 mp->last_read = min_xid - 1;
1781 mp->last_xid = mp->max_read;
1783 /* disable lro if no xid control over read */
1784 lp->lro_enabled = 0;
1787 INIT_LIST_HEAD(&mp->ex_list);
1788 spin_lock_init(&mp->em_lock);
1790 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
1800 EXPORT_SYMBOL(fc_exch_mgr_alloc);
1802 void fc_exch_mgr_free(struct fc_exch_mgr *mp)
1806 * The total exch count must be zero
1807 * before freeing exchange manager.
1809 WARN_ON(mp->total_exches != 0);
1810 mempool_destroy(mp->ep_pool);
1813 EXPORT_SYMBOL(fc_exch_mgr_free);
1815 struct fc_exch *fc_exch_get(struct fc_lport *lp, struct fc_frame *fp)
1817 if (!lp || !lp->emp)
1820 return fc_exch_alloc(lp->emp, fp, 0);
1822 EXPORT_SYMBOL(fc_exch_get);
1824 struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
1825 struct fc_frame *fp,
1826 void (*resp)(struct fc_seq *,
1827 struct fc_frame *fp,
1829 void (*destructor)(struct fc_seq *, void *),
1830 void *arg, u32 timer_msec)
1833 struct fc_seq *sp = NULL;
1834 struct fc_frame_header *fh;
1837 ep = lp->tt.exch_get(lp, fp);
1842 ep->esb_stat |= ESB_ST_SEQ_INIT;
1843 fh = fc_frame_header_get(fp);
1844 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1846 ep->destructor = destructor;
1848 ep->r_a_tov = FC_DEF_R_A_TOV;
1852 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1853 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1854 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1857 if (unlikely(lp->tt.frame_send(lp, fp)))
1861 fc_exch_timer_set_locked(ep, timer_msec);
1862 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1864 if (ep->f_ctl & FC_FC_SEQ_INIT)
1865 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1866 spin_unlock_bh(&ep->ex_lock);
1869 rc = fc_exch_done_locked(ep);
1870 spin_unlock_bh(&ep->ex_lock);
1872 fc_exch_mgr_delete_ep(ep);
1875 EXPORT_SYMBOL(fc_exch_seq_send);
1880 void fc_exch_recv(struct fc_lport *lp, struct fc_exch_mgr *mp,
1881 struct fc_frame *fp)
1883 struct fc_frame_header *fh = fc_frame_header_get(fp);
1887 if (!lp || !mp || (lp->state == LPORT_ST_NONE)) {
1888 FC_DBG("fc_lport or EM is not allocated and configured");
1894 * If frame is marked invalid, just drop it.
1896 f_ctl = ntoh24(fh->fh_f_ctl);
1897 switch (fr_eof(fp)) {
1899 if (f_ctl & FC_FC_END_SEQ)
1900 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
1903 if (fh->fh_type == FC_TYPE_BLS)
1904 fc_exch_recv_bls(mp, fp);
1905 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
1907 fc_exch_recv_seq_resp(mp, fp);
1908 else if (f_ctl & FC_FC_SEQ_CTX)
1909 fc_exch_recv_resp(mp, fp);
1911 fc_exch_recv_req(lp, mp, fp);
1914 FC_DBG("dropping invalid frame (eof %x)", fr_eof(fp));
1919 EXPORT_SYMBOL(fc_exch_recv);
1921 int fc_exch_init(struct fc_lport *lp)
1923 if (!lp->tt.exch_get) {
1925 * exch_put() should be NULL if
1926 * exch_get() is NULL
1928 WARN_ON(lp->tt.exch_put);
1929 lp->tt.exch_get = fc_exch_get;
1932 if (!lp->tt.seq_start_next)
1933 lp->tt.seq_start_next = fc_seq_start_next;
1935 if (!lp->tt.exch_seq_send)
1936 lp->tt.exch_seq_send = fc_exch_seq_send;
1938 if (!lp->tt.seq_send)
1939 lp->tt.seq_send = fc_seq_send;
1941 if (!lp->tt.seq_els_rsp_send)
1942 lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
1944 if (!lp->tt.exch_done)
1945 lp->tt.exch_done = fc_exch_done;
1947 if (!lp->tt.exch_mgr_reset)
1948 lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
1950 if (!lp->tt.seq_exch_abort)
1951 lp->tt.seq_exch_abort = fc_seq_exch_abort;
1955 EXPORT_SYMBOL(fc_exch_init);
1957 int fc_setup_exch_mgr(void)
1959 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
1960 0, SLAB_HWCACHE_ALIGN, NULL);
1966 void fc_destroy_exch_mgr(void)
1968 kmem_cache_destroy(fc_em_cachep);