xref: /linux/drivers/scsi/libfc/fc_exch.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
242e9a92fSRobert Love /*
342e9a92fSRobert Love  * Copyright(c) 2007 Intel Corporation. All rights reserved.
442e9a92fSRobert Love  * Copyright(c) 2008 Red Hat, Inc.  All rights reserved.
542e9a92fSRobert Love  * Copyright(c) 2008 Mike Christie
642e9a92fSRobert Love  *
742e9a92fSRobert Love  * Maintained at www.Open-FCoE.org
842e9a92fSRobert Love  */
942e9a92fSRobert Love 
1042e9a92fSRobert Love /*
1142e9a92fSRobert Love  * Fibre Channel exchange and sequence handling.
1242e9a92fSRobert Love  */
1342e9a92fSRobert Love 
1442e9a92fSRobert Love #include <linux/timer.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
1642e9a92fSRobert Love #include <linux/err.h>
1709703660SPaul Gortmaker #include <linux/export.h>
18a84ea8c7SBart Van Assche #include <linux/log2.h>
1942e9a92fSRobert Love 
2042e9a92fSRobert Love #include <scsi/fc/fc_fc2.h>
2142e9a92fSRobert Love 
2242e9a92fSRobert Love #include <scsi/libfc.h>
2342e9a92fSRobert Love 
248866a5d9SRobert Love #include "fc_libfc.h"
258866a5d9SRobert Love 
26e4bc50beSVasu Dev u16	fc_cpu_mask;		/* cpu mask for possible cpus */
27e4bc50beSVasu Dev EXPORT_SYMBOL(fc_cpu_mask);
28e4bc50beSVasu Dev static u16	fc_cpu_order;	/* 2's power to represent total possible cpus */
2942e9a92fSRobert Love static struct kmem_cache *fc_em_cachep;	       /* cache for exchanges */
3055204909SRandy Dunlap static struct workqueue_struct *fc_exch_workqueue;
3142e9a92fSRobert Love 
3242e9a92fSRobert Love /*
3342e9a92fSRobert Love  * Structure and function definitions for managing Fibre Channel Exchanges
3442e9a92fSRobert Love  * and Sequences.
3542e9a92fSRobert Love  *
3642e9a92fSRobert Love  * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
3742e9a92fSRobert Love  *
3842e9a92fSRobert Love  * fc_exch_mgr holds the exchange state for an N port
3942e9a92fSRobert Love  *
4042e9a92fSRobert Love  * fc_exch holds state for one exchange and links to its active sequence.
4142e9a92fSRobert Love  *
4242e9a92fSRobert Love  * fc_seq holds the state for an individual sequence.
4342e9a92fSRobert Love  */
4442e9a92fSRobert Love 
453a3b42bfSRobert Love /**
463a3b42bfSRobert Love  * struct fc_exch_pool - Per cpu exchange pool
473a3b42bfSRobert Love  * @next_index:	  Next possible free exchange index
483a3b42bfSRobert Love  * @total_exches: Total allocated exchanges
493a3b42bfSRobert Love  * @lock:	  Exch pool lock
503a3b42bfSRobert Love  * @ex_list:	  List of exchanges
5174341d35SLee Jones  * @left:	  Cache of free slot in exch array
5274341d35SLee Jones  * @right:	  Cache of free slot in exch array
53e4bc50beSVasu Dev  *
54e4bc50beSVasu Dev  * This structure manages per cpu exchanges in array of exchange pointers.
55e4bc50beSVasu Dev  * This array is allocated followed by struct fc_exch_pool memory for
56e4bc50beSVasu Dev  * assigned range of exchanges to per cpu pool.
57e4bc50beSVasu Dev  */
58e4bc50beSVasu Dev struct fc_exch_pool {
59e17b4af7SVasu Dev 	spinlock_t	 lock;
60e17b4af7SVasu Dev 	struct list_head ex_list;
613a3b42bfSRobert Love 	u16		 next_index;
623a3b42bfSRobert Love 	u16		 total_exches;
632034c19cSHillf Danton 
642034c19cSHillf Danton 	u16		 left;
652034c19cSHillf Danton 	u16		 right;
66e17b4af7SVasu Dev } ____cacheline_aligned_in_smp;
67e4bc50beSVasu Dev 
683a3b42bfSRobert Love /**
693a3b42bfSRobert Love  * struct fc_exch_mgr - The Exchange Manager (EM).
703a3b42bfSRobert Love  * @class:	    Default class for new sequences
713a3b42bfSRobert Love  * @kref:	    Reference counter
723a3b42bfSRobert Love  * @min_xid:	    Minimum exchange ID
733a3b42bfSRobert Love  * @max_xid:	    Maximum exchange ID
743a3b42bfSRobert Love  * @ep_pool:	    Reserved exchange pointers
753a3b42bfSRobert Love  * @pool_max_index: Max exch array index in exch pool
763a3b42bfSRobert Love  * @pool:	    Per cpu exch pool
7774341d35SLee Jones  * @lport:	    Local exchange port
783a3b42bfSRobert Love  * @stats:	    Statistics structure
7942e9a92fSRobert Love  *
8042e9a92fSRobert Love  * This structure is the center for creating exchanges and sequences.
8142e9a92fSRobert Love  * It manages the allocation of exchange IDs.
8242e9a92fSRobert Love  */
8342e9a92fSRobert Love struct fc_exch_mgr {
84c6b21c93SBart Van Assche 	struct fc_exch_pool __percpu *pool;
85e17b4af7SVasu Dev 	mempool_t	*ep_pool;
869ca1e182SHannes Reinecke 	struct fc_lport	*lport;
873a3b42bfSRobert Love 	enum fc_class	class;
883a3b42bfSRobert Love 	struct kref	kref;
893a3b42bfSRobert Love 	u16		min_xid;
903a3b42bfSRobert Love 	u16		max_xid;
913a3b42bfSRobert Love 	u16		pool_max_index;
9242e9a92fSRobert Love 
9342e9a92fSRobert Love 	struct {
9442e9a92fSRobert Love 		atomic_t no_free_exch;
9542e9a92fSRobert Love 		atomic_t no_free_exch_xid;
9642e9a92fSRobert Love 		atomic_t xid_not_found;
9742e9a92fSRobert Love 		atomic_t xid_busy;
9842e9a92fSRobert Love 		atomic_t seq_not_found;
9942e9a92fSRobert Love 		atomic_t non_bls_resp;
10042e9a92fSRobert Love 	} stats;
10142e9a92fSRobert Love };
10242e9a92fSRobert Love 
1033a3b42bfSRobert Love /**
1043a3b42bfSRobert Love  * struct fc_exch_mgr_anchor - primary structure for list of EMs
1053a3b42bfSRobert Love  * @ema_list: Exchange Manager Anchor list
1063a3b42bfSRobert Love  * @mp:	      Exchange Manager associated with this anchor
1073a3b42bfSRobert Love  * @match:    Routine to determine if this anchor's EM should be used
1083a3b42bfSRobert Love  *
1093a3b42bfSRobert Love  * When walking the list of anchors the match routine will be called
1103a3b42bfSRobert Love  * for each anchor to determine if that EM should be used. The last
1113a3b42bfSRobert Love  * anchor in the list will always match to handle any exchanges not
1123a3b42bfSRobert Love  * handled by other EMs. The non-default EMs would be added to the
1131bd49b48SVasu Dev  * anchor list by HW that provides offloads.
1143a3b42bfSRobert Love  */
11596316099SVasu Dev struct fc_exch_mgr_anchor {
11696316099SVasu Dev 	struct list_head ema_list;
11796316099SVasu Dev 	struct fc_exch_mgr *mp;
11896316099SVasu Dev 	bool (*match)(struct fc_frame *);
11996316099SVasu Dev };
12096316099SVasu Dev 
12142e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *);
12292261156SJoe Eykholt static void fc_seq_ls_acc(struct fc_frame *);
12392261156SJoe Eykholt static void fc_seq_ls_rjt(struct fc_frame *, enum fc_els_rjt_reason,
12442e9a92fSRobert Love 			  enum fc_els_rjt_explan);
12592261156SJoe Eykholt static void fc_exch_els_rec(struct fc_frame *);
12692261156SJoe Eykholt static void fc_exch_els_rrq(struct fc_frame *);
12742e9a92fSRobert Love 
12842e9a92fSRobert Love /*
12942e9a92fSRobert Love  * Internal implementation notes.
13042e9a92fSRobert Love  *
13142e9a92fSRobert Love  * The exchange manager is one by default in libfc but LLD may choose
13242e9a92fSRobert Love  * to have one per CPU. The sequence manager is one per exchange manager
13342e9a92fSRobert Love  * and currently never separated.
13442e9a92fSRobert Love  *
13542e9a92fSRobert Love  * Section 9.8 in FC-FS-2 specifies:  "The SEQ_ID is a one-byte field
13642e9a92fSRobert Love  * assigned by the Sequence Initiator that shall be unique for a specific
13742e9a92fSRobert Love  * D_ID and S_ID pair while the Sequence is open."   Note that it isn't
13842e9a92fSRobert Love  * qualified by exchange ID, which one might think it would be.
13942e9a92fSRobert Love  * In practice this limits the number of open sequences and exchanges to 256
14042e9a92fSRobert Love  * per session.	 For most targets we could treat this limit as per exchange.
14142e9a92fSRobert Love  *
14242e9a92fSRobert Love  * The exchange and its sequence are freed when the last sequence is received.
14342e9a92fSRobert Love  * It's possible for the remote port to leave an exchange open without
14442e9a92fSRobert Love  * sending any sequences.
14542e9a92fSRobert Love  *
14642e9a92fSRobert Love  * Notes on reference counts:
14742e9a92fSRobert Love  *
14842e9a92fSRobert Love  * Exchanges are reference counted and exchange gets freed when the reference
14942e9a92fSRobert Love  * count becomes zero.
15042e9a92fSRobert Love  *
15142e9a92fSRobert Love  * Timeouts:
15242e9a92fSRobert Love  * Sequences are timed out for E_D_TOV and R_A_TOV.
15342e9a92fSRobert Love  *
15442e9a92fSRobert Love  * Sequence event handling:
15542e9a92fSRobert Love  *
15642e9a92fSRobert Love  * The following events may occur on initiator sequences:
15742e9a92fSRobert Love  *
15842e9a92fSRobert Love  *	Send.
15942e9a92fSRobert Love  *	    For now, the whole thing is sent.
16042e9a92fSRobert Love  *	Receive ACK
16142e9a92fSRobert Love  *	    This applies only to class F.
16242e9a92fSRobert Love  *	    The sequence is marked complete.
16342e9a92fSRobert Love  *	ULP completion.
16442e9a92fSRobert Love  *	    The upper layer calls fc_exch_done() when done
16542e9a92fSRobert Love  *	    with exchange and sequence tuple.
16642e9a92fSRobert Love  *	RX-inferred completion.
16742e9a92fSRobert Love  *	    When we receive the next sequence on the same exchange, we can
16842e9a92fSRobert Love  *	    retire the previous sequence ID.  (XXX not implemented).
16942e9a92fSRobert Love  *	Timeout.
17042e9a92fSRobert Love  *	    R_A_TOV frees the sequence ID.  If we're waiting for ACK,
17142e9a92fSRobert Love  *	    E_D_TOV causes abort and calls upper layer response handler
17242e9a92fSRobert Love  *	    with FC_EX_TIMEOUT error.
17342e9a92fSRobert Love  *	Receive RJT
17442e9a92fSRobert Love  *	    XXX defer.
17542e9a92fSRobert Love  *	Send ABTS
17642e9a92fSRobert Love  *	    On timeout.
17742e9a92fSRobert Love  *
17842e9a92fSRobert Love  * The following events may occur on recipient sequences:
17942e9a92fSRobert Love  *
18042e9a92fSRobert Love  *	Receive
18142e9a92fSRobert Love  *	    Allocate sequence for first frame received.
18242e9a92fSRobert Love  *	    Hold during receive handler.
18342e9a92fSRobert Love  *	    Release when final frame received.
18442e9a92fSRobert Love  *	    Keep status of last N of these for the ELS RES command.  XXX TBD.
18542e9a92fSRobert Love  *	Receive ABTS
18642e9a92fSRobert Love  *	    Deallocate sequence
18742e9a92fSRobert Love  *	Send RJT
18842e9a92fSRobert Love  *	    Deallocate
18942e9a92fSRobert Love  *
19042e9a92fSRobert Love  * For now, we neglect conditions where only part of a sequence was
19142e9a92fSRobert Love  * received or transmitted, or where out-of-order receipt is detected.
19242e9a92fSRobert Love  */
19342e9a92fSRobert Love 
19442e9a92fSRobert Love /*
19542e9a92fSRobert Love  * Locking notes:
19642e9a92fSRobert Love  *
19742e9a92fSRobert Love  * The EM code run in a per-CPU worker thread.
19842e9a92fSRobert Love  *
19942e9a92fSRobert Love  * To protect against concurrency between a worker thread code and timers,
20042e9a92fSRobert Love  * sequence allocation and deallocation must be locked.
20142e9a92fSRobert Love  *  - exchange refcnt can be done atomicly without locks.
20242e9a92fSRobert Love  *  - sequence allocation must be locked by exch lock.
203b2f0091fSVasu Dev  *  - If the EM pool lock and ex_lock must be taken at the same time, then the
204b2f0091fSVasu Dev  *    EM pool lock must be taken before the ex_lock.
20542e9a92fSRobert Love  */
20642e9a92fSRobert Love 
20742e9a92fSRobert Love /*
20842e9a92fSRobert Love  * opcode names for debugging.
20942e9a92fSRobert Love  */
21042e9a92fSRobert Love static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
21142e9a92fSRobert Love 
2123a3b42bfSRobert Love /**
2133a3b42bfSRobert Love  * fc_exch_name_lookup() - Lookup name by opcode
2143a3b42bfSRobert Love  * @op:	       Opcode to be looked up
2153a3b42bfSRobert Love  * @table:     Opcode/name table
2163a3b42bfSRobert Love  * @max_index: Index not to be exceeded
2173a3b42bfSRobert Love  *
2183a3b42bfSRobert Love  * This routine is used to determine a human-readable string identifying
2193a3b42bfSRobert Love  * a R_CTL opcode.
2203a3b42bfSRobert Love  */
fc_exch_name_lookup(unsigned int op,char ** table,unsigned int max_index)22142e9a92fSRobert Love static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
22242e9a92fSRobert Love 					      unsigned int max_index)
22342e9a92fSRobert Love {
22442e9a92fSRobert Love 	const char *name = NULL;
22542e9a92fSRobert Love 
22642e9a92fSRobert Love 	if (op < max_index)
22742e9a92fSRobert Love 		name = table[op];
22842e9a92fSRobert Love 	if (!name)
22942e9a92fSRobert Love 		name = "unknown";
23042e9a92fSRobert Love 	return name;
23142e9a92fSRobert Love }
23242e9a92fSRobert Love 
2333a3b42bfSRobert Love /**
2343a3b42bfSRobert Love  * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
2353a3b42bfSRobert Love  * @op: The opcode to be looked up
2363a3b42bfSRobert Love  */
fc_exch_rctl_name(unsigned int op)23742e9a92fSRobert Love static const char *fc_exch_rctl_name(unsigned int op)
23842e9a92fSRobert Love {
23942e9a92fSRobert Love 	return fc_exch_name_lookup(op, fc_exch_rctl_names,
2407156fffaSKulikov Vasiliy 				   ARRAY_SIZE(fc_exch_rctl_names));
24142e9a92fSRobert Love }
24242e9a92fSRobert Love 
2433a3b42bfSRobert Love /**
2443a3b42bfSRobert Love  * fc_exch_hold() - Increment an exchange's reference count
2453a3b42bfSRobert Love  * @ep: Echange to be held
24642e9a92fSRobert Love  */
fc_exch_hold(struct fc_exch * ep)2473a3b42bfSRobert Love static inline void fc_exch_hold(struct fc_exch *ep)
24842e9a92fSRobert Love {
24942e9a92fSRobert Love 	atomic_inc(&ep->ex_refcnt);
25042e9a92fSRobert Love }
25142e9a92fSRobert Love 
2523a3b42bfSRobert Love /**
2533a3b42bfSRobert Love  * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
2543a3b42bfSRobert Love  *			 and determine SOF and EOF.
2553a3b42bfSRobert Love  * @ep:	   The exchange to that will use the header
2563a3b42bfSRobert Love  * @fp:	   The frame whose header is to be modified
2573a3b42bfSRobert Love  * @f_ctl: F_CTL bits that will be used for the frame header
2583a3b42bfSRobert Love  *
2593a3b42bfSRobert Love  * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
2603a3b42bfSRobert Love  * fh_seq_id, fh_seq_cnt and the SOF and EOF.
26142e9a92fSRobert Love  */
fc_exch_setup_hdr(struct fc_exch * ep,struct fc_frame * fp,u32 f_ctl)26242e9a92fSRobert Love static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
26342e9a92fSRobert Love 			      u32 f_ctl)
26442e9a92fSRobert Love {
26542e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
26642e9a92fSRobert Love 	u16 fill;
26742e9a92fSRobert Love 
26842e9a92fSRobert Love 	fr_sof(fp) = ep->class;
26942e9a92fSRobert Love 	if (ep->seq.cnt)
27042e9a92fSRobert Love 		fr_sof(fp) = fc_sof_normal(ep->class);
27142e9a92fSRobert Love 
27242e9a92fSRobert Love 	if (f_ctl & FC_FC_END_SEQ) {
27342e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_T;
2743fb52041SArnd Bergmann 		if (fc_sof_needs_ack((enum fc_sof)ep->class))
27542e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
27642e9a92fSRobert Love 		/*
2773a3b42bfSRobert Love 		 * From F_CTL.
27842e9a92fSRobert Love 		 * The number of fill bytes to make the length a 4-byte
27942e9a92fSRobert Love 		 * multiple is the low order 2-bits of the f_ctl.
28042e9a92fSRobert Love 		 * The fill itself will have been cleared by the frame
28142e9a92fSRobert Love 		 * allocation.
28242e9a92fSRobert Love 		 * After this, the length will be even, as expected by
28342e9a92fSRobert Love 		 * the transport.
28442e9a92fSRobert Love 		 */
28542e9a92fSRobert Love 		fill = fr_len(fp) & 3;
28642e9a92fSRobert Love 		if (fill) {
28742e9a92fSRobert Love 			fill = 4 - fill;
28842e9a92fSRobert Love 			/* TODO, this may be a problem with fragmented skb */
28942e9a92fSRobert Love 			skb_put(fp_skb(fp), fill);
29042e9a92fSRobert Love 			hton24(fh->fh_f_ctl, f_ctl | fill);
29142e9a92fSRobert Love 		}
29242e9a92fSRobert Love 	} else {
29342e9a92fSRobert Love 		WARN_ON(fr_len(fp) % 4 != 0);	/* no pad to non last frame */
29442e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
29542e9a92fSRobert Love 	}
29642e9a92fSRobert Love 
297c1d45424SBart Van Assche 	/* Initialize remaining fh fields from fc_fill_fc_hdr */
29842e9a92fSRobert Love 	fh->fh_ox_id = htons(ep->oxid);
29942e9a92fSRobert Love 	fh->fh_rx_id = htons(ep->rxid);
30042e9a92fSRobert Love 	fh->fh_seq_id = ep->seq.id;
30142e9a92fSRobert Love 	fh->fh_seq_cnt = htons(ep->seq.cnt);
30242e9a92fSRobert Love }
30342e9a92fSRobert Love 
3043a3b42bfSRobert Love /**
3053a3b42bfSRobert Love  * fc_exch_release() - Decrement an exchange's reference count
3063a3b42bfSRobert Love  * @ep: Exchange to be released
3073a3b42bfSRobert Love  *
3083a3b42bfSRobert Love  * If the reference count reaches zero and the exchange is complete,
3093a3b42bfSRobert Love  * it is freed.
31042e9a92fSRobert Love  */
fc_exch_release(struct fc_exch * ep)31142e9a92fSRobert Love static void fc_exch_release(struct fc_exch *ep)
31242e9a92fSRobert Love {
31342e9a92fSRobert Love 	struct fc_exch_mgr *mp;
31442e9a92fSRobert Love 
31542e9a92fSRobert Love 	if (atomic_dec_and_test(&ep->ex_refcnt)) {
31642e9a92fSRobert Love 		mp = ep->em;
31742e9a92fSRobert Love 		if (ep->destructor)
31842e9a92fSRobert Love 			ep->destructor(&ep->seq, ep->arg);
319aa6cd29bSJulia Lawall 		WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
32042e9a92fSRobert Love 		mempool_free(ep, mp->ep_pool);
32142e9a92fSRobert Love 	}
32242e9a92fSRobert Love }
32342e9a92fSRobert Love 
3243a3b42bfSRobert Love /**
325b29a4f30SVasu Dev  * fc_exch_timer_cancel() - cancel exch timer
326b29a4f30SVasu Dev  * @ep:		The exchange whose timer to be canceled
327b29a4f30SVasu Dev  */
fc_exch_timer_cancel(struct fc_exch * ep)328b29a4f30SVasu Dev static inline void fc_exch_timer_cancel(struct fc_exch *ep)
329b29a4f30SVasu Dev {
330b29a4f30SVasu Dev 	if (cancel_delayed_work(&ep->timeout_work)) {
331b29a4f30SVasu Dev 		FC_EXCH_DBG(ep, "Exchange timer canceled\n");
332b29a4f30SVasu Dev 		atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
333b29a4f30SVasu Dev 	}
334b29a4f30SVasu Dev }
335b29a4f30SVasu Dev 
336b29a4f30SVasu Dev /**
337b29a4f30SVasu Dev  * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
338b29a4f30SVasu Dev  *				the exchange lock held
339b29a4f30SVasu Dev  * @ep:		The exchange whose timer will start
340b29a4f30SVasu Dev  * @timer_msec: The timeout period
341b29a4f30SVasu Dev  *
342b29a4f30SVasu Dev  * Used for upper level protocols to time out the exchange.
343b29a4f30SVasu Dev  * The timer is cancelled when it fires or when the exchange completes.
344b29a4f30SVasu Dev  */
fc_exch_timer_set_locked(struct fc_exch * ep,unsigned int timer_msec)345b29a4f30SVasu Dev static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
346b29a4f30SVasu Dev 					    unsigned int timer_msec)
347b29a4f30SVasu Dev {
348b29a4f30SVasu Dev 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
349b29a4f30SVasu Dev 		return;
350b29a4f30SVasu Dev 
351b29a4f30SVasu Dev 	FC_EXCH_DBG(ep, "Exchange timer armed : %d msecs\n", timer_msec);
352b29a4f30SVasu Dev 
353b29a4f30SVasu Dev 	fc_exch_hold(ep);		/* hold for timer */
354b8678865SBart Van Assche 	if (!queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
35557d3ec7eSHannes Reinecke 				msecs_to_jiffies(timer_msec))) {
35657d3ec7eSHannes Reinecke 		FC_EXCH_DBG(ep, "Exchange already queued\n");
357b8678865SBart Van Assche 		fc_exch_release(ep);
358b29a4f30SVasu Dev 	}
35957d3ec7eSHannes Reinecke }
360b29a4f30SVasu Dev 
361b29a4f30SVasu Dev /**
362b29a4f30SVasu Dev  * fc_exch_timer_set() - Lock the exchange and set the timer
363b29a4f30SVasu Dev  * @ep:		The exchange whose timer will start
364b29a4f30SVasu Dev  * @timer_msec: The timeout period
365b29a4f30SVasu Dev  */
fc_exch_timer_set(struct fc_exch * ep,unsigned int timer_msec)366b29a4f30SVasu Dev static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
367b29a4f30SVasu Dev {
368b29a4f30SVasu Dev 	spin_lock_bh(&ep->ex_lock);
369b29a4f30SVasu Dev 	fc_exch_timer_set_locked(ep, timer_msec);
370b29a4f30SVasu Dev 	spin_unlock_bh(&ep->ex_lock);
371b29a4f30SVasu Dev }
372b29a4f30SVasu Dev 
373b29a4f30SVasu Dev /**
3743a3b42bfSRobert Love  * fc_exch_done_locked() - Complete an exchange with the exchange lock held
3753a3b42bfSRobert Love  * @ep: The exchange that is complete
3767030fd62SBart Van Assche  *
3777030fd62SBart Van Assche  * Note: May sleep if invoked from outside a response handler.
3783a3b42bfSRobert Love  */
fc_exch_done_locked(struct fc_exch * ep)37942e9a92fSRobert Love static int fc_exch_done_locked(struct fc_exch *ep)
38042e9a92fSRobert Love {
38142e9a92fSRobert Love 	int rc = 1;
38242e9a92fSRobert Love 
38342e9a92fSRobert Love 	/*
38442e9a92fSRobert Love 	 * We must check for completion in case there are two threads
38542e9a92fSRobert Love 	 * tyring to complete this. But the rrq code will reuse the
38642e9a92fSRobert Love 	 * ep, and in that case we only clear the resp and set it as
38742e9a92fSRobert Love 	 * complete, so it can be reused by the timer to send the rrq.
38842e9a92fSRobert Love 	 */
38942e9a92fSRobert Love 	if (ep->state & FC_EX_DONE)
39042e9a92fSRobert Love 		return rc;
39142e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_COMPLETE;
39242e9a92fSRobert Love 
39342e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
39442e9a92fSRobert Love 		ep->state |= FC_EX_DONE;
395b29a4f30SVasu Dev 		fc_exch_timer_cancel(ep);
39642e9a92fSRobert Love 		rc = 0;
39742e9a92fSRobert Love 	}
39842e9a92fSRobert Love 	return rc;
39942e9a92fSRobert Love }
40042e9a92fSRobert Love 
4019ca1e182SHannes Reinecke static struct fc_exch fc_quarantine_exch;
4029ca1e182SHannes Reinecke 
4033a3b42bfSRobert Love /**
4043a3b42bfSRobert Love  * fc_exch_ptr_get() - Return an exchange from an exchange pool
4053a3b42bfSRobert Love  * @pool:  Exchange Pool to get an exchange from
4063a3b42bfSRobert Love  * @index: Index of the exchange within the pool
4073a3b42bfSRobert Love  *
4083a3b42bfSRobert Love  * Use the index to get an exchange from within an exchange pool. exches
4093a3b42bfSRobert Love  * will point to an array of exchange pointers. The index will select
4103a3b42bfSRobert Love  * the exchange within the array.
4113a3b42bfSRobert Love  */
fc_exch_ptr_get(struct fc_exch_pool * pool,u16 index)412e4bc50beSVasu Dev static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
413e4bc50beSVasu Dev 					      u16 index)
414e4bc50beSVasu Dev {
415e4bc50beSVasu Dev 	struct fc_exch **exches = (struct fc_exch **)(pool + 1);
416e4bc50beSVasu Dev 	return exches[index];
417e4bc50beSVasu Dev }
418e4bc50beSVasu Dev 
4193a3b42bfSRobert Love /**
4203a3b42bfSRobert Love  * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
4213a3b42bfSRobert Love  * @pool:  The pool to assign the exchange to
4223a3b42bfSRobert Love  * @index: The index in the pool where the exchange will be assigned
4233a3b42bfSRobert Love  * @ep:	   The exchange to assign to the pool
4243a3b42bfSRobert Love  */
fc_exch_ptr_set(struct fc_exch_pool * pool,u16 index,struct fc_exch * ep)425e4bc50beSVasu Dev static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
426e4bc50beSVasu Dev 				   struct fc_exch *ep)
427e4bc50beSVasu Dev {
428e4bc50beSVasu Dev 	((struct fc_exch **)(pool + 1))[index] = ep;
429e4bc50beSVasu Dev }
430e4bc50beSVasu Dev 
4313a3b42bfSRobert Love /**
4323a3b42bfSRobert Love  * fc_exch_delete() - Delete an exchange
4333a3b42bfSRobert Love  * @ep: The exchange to be deleted
4343a3b42bfSRobert Love  */
fc_exch_delete(struct fc_exch * ep)435b2f0091fSVasu Dev static void fc_exch_delete(struct fc_exch *ep)
43642e9a92fSRobert Love {
437b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
4382034c19cSHillf Danton 	u16 index;
43942e9a92fSRobert Love 
440b2f0091fSVasu Dev 	pool = ep->pool;
441b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
442b2f0091fSVasu Dev 	WARN_ON(pool->total_exches <= 0);
443b2f0091fSVasu Dev 	pool->total_exches--;
4442034c19cSHillf Danton 
4452034c19cSHillf Danton 	/* update cache of free slot */
4462034c19cSHillf Danton 	index = (ep->xid - ep->em->min_xid) >> fc_cpu_order;
4479ca1e182SHannes Reinecke 	if (!(ep->state & FC_EX_QUARANTINE)) {
4482034c19cSHillf Danton 		if (pool->left == FC_XID_UNKNOWN)
4492034c19cSHillf Danton 			pool->left = index;
4502034c19cSHillf Danton 		else if (pool->right == FC_XID_UNKNOWN)
4512034c19cSHillf Danton 			pool->right = index;
4522034c19cSHillf Danton 		else
4532034c19cSHillf Danton 			pool->next_index = index;
4542034c19cSHillf Danton 		fc_exch_ptr_set(pool, index, NULL);
4559ca1e182SHannes Reinecke 	} else {
4569ca1e182SHannes Reinecke 		fc_exch_ptr_set(pool, index, &fc_quarantine_exch);
4579ca1e182SHannes Reinecke 	}
45842e9a92fSRobert Love 	list_del(&ep->ex_list);
459b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
46042e9a92fSRobert Love 	fc_exch_release(ep);	/* drop hold for exch in mp */
46142e9a92fSRobert Love }
46242e9a92fSRobert Love 
fc_seq_send_locked(struct fc_lport * lport,struct fc_seq * sp,struct fc_frame * fp)463fb00cc23SNeil Horman static int fc_seq_send_locked(struct fc_lport *lport, struct fc_seq *sp,
4641a7b75aeSRobert Love 			      struct fc_frame *fp)
4651a7b75aeSRobert Love {
4661a7b75aeSRobert Love 	struct fc_exch *ep;
4671a7b75aeSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
468cae7b6ddSBart Van Assche 	int error = -ENXIO;
4691a7b75aeSRobert Love 	u32 f_ctl;
47014fc315fSVasu Dev 	u8 fh_type = fh->fh_type;
4711a7b75aeSRobert Love 
4721a7b75aeSRobert Love 	ep = fc_seq_exch(sp);
473cae7b6ddSBart Van Assche 
474cae7b6ddSBart Van Assche 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL)) {
475cae7b6ddSBart Van Assche 		fc_frame_free(fp);
476cae7b6ddSBart Van Assche 		goto out;
477cae7b6ddSBart Van Assche 	}
478cae7b6ddSBart Van Assche 
479fb00cc23SNeil Horman 	WARN_ON(!(ep->esb_stat & ESB_ST_SEQ_INIT));
4801a7b75aeSRobert Love 
4811a7b75aeSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
4821a7b75aeSRobert Love 	fc_exch_setup_hdr(ep, fp, f_ctl);
483f60e12e9SJoe Eykholt 	fr_encaps(fp) = ep->encaps;
4841a7b75aeSRobert Love 
4851a7b75aeSRobert Love 	/*
4861a7b75aeSRobert Love 	 * update sequence count if this frame is carrying
4871a7b75aeSRobert Love 	 * multiple FC frames when sequence offload is enabled
4881a7b75aeSRobert Love 	 * by LLD.
4891a7b75aeSRobert Love 	 */
4901a7b75aeSRobert Love 	if (fr_max_payload(fp))
4911a7b75aeSRobert Love 		sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
4921a7b75aeSRobert Love 					fr_max_payload(fp));
4931a7b75aeSRobert Love 	else
4941a7b75aeSRobert Love 		sp->cnt++;
4951a7b75aeSRobert Love 
4961a7b75aeSRobert Love 	/*
4971a7b75aeSRobert Love 	 * Send the frame.
4981a7b75aeSRobert Love 	 */
4993a3b42bfSRobert Love 	error = lport->tt.frame_send(lport, fp);
5001a7b75aeSRobert Love 
50114fc315fSVasu Dev 	if (fh_type == FC_TYPE_BLS)
502fb00cc23SNeil Horman 		goto out;
50377a2b73aSVasu Dev 
5041a7b75aeSRobert Love 	/*
5051a7b75aeSRobert Love 	 * Update the exchange and sequence flags,
5061a7b75aeSRobert Love 	 * assuming all frames for the sequence have been sent.
5071a7b75aeSRobert Love 	 * We can only be called to send once for each sequence.
5081a7b75aeSRobert Love 	 */
5091a7b75aeSRobert Love 	ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ;	/* not first seq */
510cc3593d3SJoe Eykholt 	if (f_ctl & FC_FC_SEQ_INIT)
5111a7b75aeSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
512fb00cc23SNeil Horman out:
513fb00cc23SNeil Horman 	return error;
514fb00cc23SNeil Horman }
515fb00cc23SNeil Horman 
516fb00cc23SNeil Horman /**
517fb00cc23SNeil Horman  * fc_seq_send() - Send a frame using existing sequence/exchange pair
518fb00cc23SNeil Horman  * @lport: The local port that the exchange will be sent on
519fb00cc23SNeil Horman  * @sp:	   The sequence to be sent
520fb00cc23SNeil Horman  * @fp:	   The frame to be sent on the exchange
521cae7b6ddSBart Van Assche  *
522cae7b6ddSBart Van Assche  * Note: The frame will be freed either by a direct call to fc_frame_free(fp)
523cae7b6ddSBart Van Assche  * or indirectly by calling libfc_function_template.frame_send().
524fb00cc23SNeil Horman  */
fc_seq_send(struct fc_lport * lport,struct fc_seq * sp,struct fc_frame * fp)5250cac937dSHannes Reinecke int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp, struct fc_frame *fp)
526fb00cc23SNeil Horman {
527fb00cc23SNeil Horman 	struct fc_exch *ep;
528fb00cc23SNeil Horman 	int error;
529fb00cc23SNeil Horman 	ep = fc_seq_exch(sp);
530fb00cc23SNeil Horman 	spin_lock_bh(&ep->ex_lock);
531fb00cc23SNeil Horman 	error = fc_seq_send_locked(lport, sp, fp);
5321a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
5331a7b75aeSRobert Love 	return error;
5341a7b75aeSRobert Love }
5350cac937dSHannes Reinecke EXPORT_SYMBOL(fc_seq_send);
5361a7b75aeSRobert Love 
5371a7b75aeSRobert Love /**
5383a3b42bfSRobert Love  * fc_seq_alloc() - Allocate a sequence for a given exchange
5393a3b42bfSRobert Love  * @ep:	    The exchange to allocate a new sequence for
5403a3b42bfSRobert Love  * @seq_id: The sequence ID to be used
5411a7b75aeSRobert Love  *
5421a7b75aeSRobert Love  * We don't support multiple originated sequences on the same exchange.
5431a7b75aeSRobert Love  * By implication, any previously originated sequence on this exchange
5441a7b75aeSRobert Love  * is complete, and we reallocate the same sequence.
5451a7b75aeSRobert Love  */
fc_seq_alloc(struct fc_exch * ep,u8 seq_id)5461a7b75aeSRobert Love static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
5471a7b75aeSRobert Love {
5481a7b75aeSRobert Love 	struct fc_seq *sp;
5491a7b75aeSRobert Love 
5501a7b75aeSRobert Love 	sp = &ep->seq;
5511a7b75aeSRobert Love 	sp->ssb_stat = 0;
5521a7b75aeSRobert Love 	sp->cnt = 0;
5531a7b75aeSRobert Love 	sp->id = seq_id;
5541a7b75aeSRobert Love 	return sp;
5551a7b75aeSRobert Love }
5561a7b75aeSRobert Love 
5573a3b42bfSRobert Love /**
5583a3b42bfSRobert Love  * fc_seq_start_next_locked() - Allocate a new sequence on the same
5593a3b42bfSRobert Love  *				exchange as the supplied sequence
5603a3b42bfSRobert Love  * @sp: The sequence/exchange to get a new sequence for
5613a3b42bfSRobert Love  */
fc_seq_start_next_locked(struct fc_seq * sp)5621a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
5631a7b75aeSRobert Love {
5641a7b75aeSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
5651a7b75aeSRobert Love 
5661a7b75aeSRobert Love 	sp = fc_seq_alloc(ep, ep->seq_id++);
5671a7b75aeSRobert Love 	FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
5681a7b75aeSRobert Love 		    ep->f_ctl, sp->id);
5691a7b75aeSRobert Love 	return sp;
5701a7b75aeSRobert Love }
5711a7b75aeSRobert Love 
5721a7b75aeSRobert Love /**
5733a3b42bfSRobert Love  * fc_seq_start_next() - Lock the exchange and get a new sequence
5743a3b42bfSRobert Love  *			 for a given sequence/exchange pair
5753a3b42bfSRobert Love  * @sp: The sequence/exchange to get a new exchange for
5761a7b75aeSRobert Love  */
fc_seq_start_next(struct fc_seq * sp)577c6865b30SHannes Reinecke struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
5781a7b75aeSRobert Love {
5791a7b75aeSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
5801a7b75aeSRobert Love 
5811a7b75aeSRobert Love 	spin_lock_bh(&ep->ex_lock);
5821a7b75aeSRobert Love 	sp = fc_seq_start_next_locked(sp);
5831a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
5841a7b75aeSRobert Love 
5851a7b75aeSRobert Love 	return sp;
5861a7b75aeSRobert Love }
587c6865b30SHannes Reinecke EXPORT_SYMBOL(fc_seq_start_next);
5881a7b75aeSRobert Love 
5891a5c2d7eSJoe Eykholt /*
5901a5c2d7eSJoe Eykholt  * Set the response handler for the exchange associated with a sequence.
5917030fd62SBart Van Assche  *
5927030fd62SBart Van Assche  * Note: May sleep if invoked from outside a response handler.
5931a5c2d7eSJoe Eykholt  */
fc_seq_set_resp(struct fc_seq * sp,void (* resp)(struct fc_seq *,struct fc_frame *,void *),void * arg)594f1d61e6eSHannes Reinecke void fc_seq_set_resp(struct fc_seq *sp,
595f1d61e6eSHannes Reinecke 		     void (*resp)(struct fc_seq *, struct fc_frame *, void *),
5961a5c2d7eSJoe Eykholt 		     void *arg)
5971a5c2d7eSJoe Eykholt {
5981a5c2d7eSJoe Eykholt 	struct fc_exch *ep = fc_seq_exch(sp);
5997030fd62SBart Van Assche 	DEFINE_WAIT(wait);
6001a5c2d7eSJoe Eykholt 
6011a5c2d7eSJoe Eykholt 	spin_lock_bh(&ep->ex_lock);
6027030fd62SBart Van Assche 	while (ep->resp_active && ep->resp_task != current) {
6037030fd62SBart Van Assche 		prepare_to_wait(&ep->resp_wq, &wait, TASK_UNINTERRUPTIBLE);
6047030fd62SBart Van Assche 		spin_unlock_bh(&ep->ex_lock);
6057030fd62SBart Van Assche 
6067030fd62SBart Van Assche 		schedule();
6077030fd62SBart Van Assche 
6087030fd62SBart Van Assche 		spin_lock_bh(&ep->ex_lock);
6097030fd62SBart Van Assche 	}
6107030fd62SBart Van Assche 	finish_wait(&ep->resp_wq, &wait);
6111a5c2d7eSJoe Eykholt 	ep->resp = resp;
6121a5c2d7eSJoe Eykholt 	ep->arg = arg;
6131a5c2d7eSJoe Eykholt 	spin_unlock_bh(&ep->ex_lock);
6141a5c2d7eSJoe Eykholt }
615f1d61e6eSHannes Reinecke EXPORT_SYMBOL(fc_seq_set_resp);
6161a5c2d7eSJoe Eykholt 
6171a7b75aeSRobert Love /**
61877a2b73aSVasu Dev  * fc_exch_abort_locked() - Abort an exchange
61977a2b73aSVasu Dev  * @ep:	The exchange to be aborted
6203a3b42bfSRobert Love  * @timer_msec: The period of time to wait before aborting
6213a3b42bfSRobert Love  *
6220ebaed17SHannes Reinecke  * Abort an exchange and sequence. Generally called because of a
6230ebaed17SHannes Reinecke  * exchange timeout or an abort from the upper layer.
6240ebaed17SHannes Reinecke  *
6250ebaed17SHannes Reinecke  * A timer_msec can be specified for abort timeout, if non-zero
6260ebaed17SHannes Reinecke  * timer_msec value is specified then exchange resp handler
6270ebaed17SHannes Reinecke  * will be called with timeout error if no response to abort.
6280ebaed17SHannes Reinecke  *
62977a2b73aSVasu Dev  * Locking notes:  Called with exch lock held
63077a2b73aSVasu Dev  *
63177a2b73aSVasu Dev  * Return value: 0 on success else error code
6321a7b75aeSRobert Love  */
fc_exch_abort_locked(struct fc_exch * ep,unsigned int timer_msec)63377a2b73aSVasu Dev static int fc_exch_abort_locked(struct fc_exch *ep,
6341a7b75aeSRobert Love 				unsigned int timer_msec)
63542e9a92fSRobert Love {
63642e9a92fSRobert Love 	struct fc_seq *sp;
63742e9a92fSRobert Love 	struct fc_frame *fp;
63842e9a92fSRobert Love 	int error;
63942e9a92fSRobert Love 
64057d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "exch: abort, time %d msecs\n", timer_msec);
64142e9a92fSRobert Love 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
64257d3ec7eSHannes Reinecke 	    ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
64357d3ec7eSHannes Reinecke 		FC_EXCH_DBG(ep, "exch: already completed esb %x state %x\n",
64457d3ec7eSHannes Reinecke 			    ep->esb_stat, ep->state);
64542e9a92fSRobert Love 		return -ENXIO;
64657d3ec7eSHannes Reinecke 	}
64742e9a92fSRobert Love 
64842e9a92fSRobert Love 	/*
64942e9a92fSRobert Love 	 * Send the abort on a new sequence if possible.
65042e9a92fSRobert Love 	 */
65142e9a92fSRobert Love 	sp = fc_seq_start_next_locked(&ep->seq);
65277a2b73aSVasu Dev 	if (!sp)
65342e9a92fSRobert Love 		return -ENOMEM;
65442e9a92fSRobert Love 
65542e9a92fSRobert Love 	if (timer_msec)
65642e9a92fSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
65742e9a92fSRobert Love 
658cae7b6ddSBart Van Assche 	if (ep->sid) {
65942e9a92fSRobert Love 		/*
66042e9a92fSRobert Love 		 * Send an abort for the sequence that timed out.
66142e9a92fSRobert Love 		 */
66242e9a92fSRobert Love 		fp = fc_frame_alloc(ep->lp, 0);
66342e9a92fSRobert Love 		if (fp) {
664cae7b6ddSBart Van Assche 			ep->esb_stat |= ESB_ST_SEQ_INIT;
66542e9a92fSRobert Love 			fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
666cae7b6ddSBart Van Assche 				       FC_TYPE_BLS, FC_FC_END_SEQ |
667cae7b6ddSBart Van Assche 				       FC_FC_SEQ_INIT, 0);
668fb00cc23SNeil Horman 			error = fc_seq_send_locked(ep->lp, sp, fp);
669cae7b6ddSBart Van Assche 		} else {
67042e9a92fSRobert Love 			error = -ENOBUFS;
671cae7b6ddSBart Van Assche 		}
672cae7b6ddSBart Van Assche 	} else {
673cae7b6ddSBart Van Assche 		/*
674cae7b6ddSBart Van Assche 		 * If not logged into the fabric, don't send ABTS but leave
675cae7b6ddSBart Van Assche 		 * sequence active until next timeout.
676cae7b6ddSBart Van Assche 		 */
677cae7b6ddSBart Van Assche 		error = 0;
678cae7b6ddSBart Van Assche 	}
679cae7b6ddSBart Van Assche 	ep->esb_stat |= ESB_ST_ABNORMAL;
68042e9a92fSRobert Love 	return error;
68142e9a92fSRobert Love }
68242e9a92fSRobert Love 
6833a3b42bfSRobert Love /**
68477a2b73aSVasu Dev  * fc_seq_exch_abort() - Abort an exchange and sequence
68577a2b73aSVasu Dev  * @req_sp:	The sequence to be aborted
68677a2b73aSVasu Dev  * @timer_msec: The period of time to wait before aborting
68777a2b73aSVasu Dev  *
68877a2b73aSVasu Dev  * Generally called because of a timeout or an abort from the upper layer.
68977a2b73aSVasu Dev  *
69077a2b73aSVasu Dev  * Return value: 0 on success else error code
69177a2b73aSVasu Dev  */
fc_seq_exch_abort(const struct fc_seq * req_sp,unsigned int timer_msec)6920ebaed17SHannes Reinecke int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec)
69377a2b73aSVasu Dev {
69477a2b73aSVasu Dev 	struct fc_exch *ep;
69577a2b73aSVasu Dev 	int error;
69677a2b73aSVasu Dev 
69777a2b73aSVasu Dev 	ep = fc_seq_exch(req_sp);
69877a2b73aSVasu Dev 	spin_lock_bh(&ep->ex_lock);
69977a2b73aSVasu Dev 	error = fc_exch_abort_locked(ep, timer_msec);
70077a2b73aSVasu Dev 	spin_unlock_bh(&ep->ex_lock);
70177a2b73aSVasu Dev 	return error;
70277a2b73aSVasu Dev }
70377a2b73aSVasu Dev 
70477a2b73aSVasu Dev /**
7057030fd62SBart Van Assche  * fc_invoke_resp() - invoke ep->resp()
70674341d35SLee Jones  * @ep:	   The exchange to be operated on
70774341d35SLee Jones  * @fp:	   The frame pointer to pass through to ->resp()
70874341d35SLee Jones  * @sp:	   The sequence pointer to pass through to ->resp()
7097030fd62SBart Van Assche  *
7107030fd62SBart Van Assche  * Notes:
7117030fd62SBart Van Assche  * It is assumed that after initialization finished (this means the
7127030fd62SBart Van Assche  * first unlock of ex_lock after fc_exch_alloc()) ep->resp and ep->arg are
7137030fd62SBart Van Assche  * modified only via fc_seq_set_resp(). This guarantees that none of these
7147030fd62SBart Van Assche  * two variables changes if ep->resp_active > 0.
7157030fd62SBart Van Assche  *
7167030fd62SBart Van Assche  * If an fc_seq_set_resp() call is busy modifying ep->resp and ep->arg when
7177030fd62SBart Van Assche  * this function is invoked, the first spin_lock_bh() call in this function
7187030fd62SBart Van Assche  * will wait until fc_seq_set_resp() has finished modifying these variables.
7197030fd62SBart Van Assche  *
7207030fd62SBart Van Assche  * Since fc_exch_done() invokes fc_seq_set_resp() it is guaranteed that that
7217030fd62SBart Van Assche  * ep->resp() won't be invoked after fc_exch_done() has returned.
7227030fd62SBart Van Assche  *
7237030fd62SBart Van Assche  * The response handler itself may invoke fc_exch_done(), which will clear the
7247030fd62SBart Van Assche  * ep->resp pointer.
7257030fd62SBart Van Assche  *
7267030fd62SBart Van Assche  * Return value:
7277030fd62SBart Van Assche  * Returns true if and only if ep->resp has been invoked.
7287030fd62SBart Van Assche  */
fc_invoke_resp(struct fc_exch * ep,struct fc_seq * sp,struct fc_frame * fp)7297030fd62SBart Van Assche static bool fc_invoke_resp(struct fc_exch *ep, struct fc_seq *sp,
7307030fd62SBart Van Assche 			   struct fc_frame *fp)
7317030fd62SBart Van Assche {
7327030fd62SBart Van Assche 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
7337030fd62SBart Van Assche 	void *arg;
7347030fd62SBart Van Assche 	bool res = false;
7357030fd62SBart Van Assche 
7367030fd62SBart Van Assche 	spin_lock_bh(&ep->ex_lock);
7377030fd62SBart Van Assche 	ep->resp_active++;
7387030fd62SBart Van Assche 	if (ep->resp_task != current)
7397030fd62SBart Van Assche 		ep->resp_task = !ep->resp_task ? current : NULL;
7407030fd62SBart Van Assche 	resp = ep->resp;
7417030fd62SBart Van Assche 	arg = ep->arg;
7427030fd62SBart Van Assche 	spin_unlock_bh(&ep->ex_lock);
7437030fd62SBart Van Assche 
7447030fd62SBart Van Assche 	if (resp) {
7457030fd62SBart Van Assche 		resp(sp, fp, arg);
7467030fd62SBart Van Assche 		res = true;
7477030fd62SBart Van Assche 	}
7487030fd62SBart Van Assche 
7497030fd62SBart Van Assche 	spin_lock_bh(&ep->ex_lock);
7507030fd62SBart Van Assche 	if (--ep->resp_active == 0)
7517030fd62SBart Van Assche 		ep->resp_task = NULL;
7527030fd62SBart Van Assche 	spin_unlock_bh(&ep->ex_lock);
7537030fd62SBart Van Assche 
7547030fd62SBart Van Assche 	if (ep->resp_active == 0)
7557030fd62SBart Van Assche 		wake_up(&ep->resp_wq);
7567030fd62SBart Van Assche 
7577030fd62SBart Van Assche 	return res;
7587030fd62SBart Van Assche }
7597030fd62SBart Van Assche 
7607030fd62SBart Van Assche /**
7613a3b42bfSRobert Love  * fc_exch_timeout() - Handle exchange timer expiration
7623a3b42bfSRobert Love  * @work: The work_struct identifying the exchange that timed out
76342e9a92fSRobert Love  */
fc_exch_timeout(struct work_struct * work)76442e9a92fSRobert Love static void fc_exch_timeout(struct work_struct *work)
76542e9a92fSRobert Love {
76642e9a92fSRobert Love 	struct fc_exch *ep = container_of(work, struct fc_exch,
76742e9a92fSRobert Love 					  timeout_work.work);
76842e9a92fSRobert Love 	struct fc_seq *sp = &ep->seq;
76942e9a92fSRobert Love 	u32 e_stat;
77042e9a92fSRobert Love 	int rc = 1;
77142e9a92fSRobert Love 
77257d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "Exchange timed out state %x\n", ep->state);
773cd305ce4SRobert Love 
77442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
77542e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
77642e9a92fSRobert Love 		goto unlock;
77742e9a92fSRobert Love 
77842e9a92fSRobert Love 	e_stat = ep->esb_stat;
77942e9a92fSRobert Love 	if (e_stat & ESB_ST_COMPLETE) {
78042e9a92fSRobert Love 		ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
781a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
78242e9a92fSRobert Love 		if (e_stat & ESB_ST_REC_QUAL)
78342e9a92fSRobert Love 			fc_exch_rrq(ep);
78442e9a92fSRobert Love 		goto done;
78542e9a92fSRobert Love 	} else {
78642e9a92fSRobert Love 		if (e_stat & ESB_ST_ABNORMAL)
78742e9a92fSRobert Love 			rc = fc_exch_done_locked(ep);
78842e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
789f3162483SParikh, Neerav 		if (!rc)
790f3162483SParikh, Neerav 			fc_exch_delete(ep);
7917030fd62SBart Van Assche 		fc_invoke_resp(ep, sp, ERR_PTR(-FC_EX_TIMEOUT));
7927030fd62SBart Van Assche 		fc_seq_set_resp(sp, NULL, ep->arg);
79342e9a92fSRobert Love 		fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
79442e9a92fSRobert Love 		goto done;
79542e9a92fSRobert Love 	}
79642e9a92fSRobert Love unlock:
79742e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
79842e9a92fSRobert Love done:
79942e9a92fSRobert Love 	/*
80042e9a92fSRobert Love 	 * This release matches the hold taken when the timer was set.
80142e9a92fSRobert Love 	 */
80242e9a92fSRobert Love 	fc_exch_release(ep);
80342e9a92fSRobert Love }
80442e9a92fSRobert Love 
80552ff878cSVasu Dev /**
8063a3b42bfSRobert Love  * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
8073a3b42bfSRobert Love  * @lport: The local port that the exchange is for
8083a3b42bfSRobert Love  * @mp:	   The exchange manager that will allocate the exchange
80942e9a92fSRobert Love  *
810d7179680SVasu Dev  * Returns pointer to allocated fc_exch with exch lock held.
81142e9a92fSRobert Love  */
fc_exch_em_alloc(struct fc_lport * lport,struct fc_exch_mgr * mp)81252ff878cSVasu Dev static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
813d7179680SVasu Dev 					struct fc_exch_mgr *mp)
81442e9a92fSRobert Love {
81542e9a92fSRobert Love 	struct fc_exch *ep;
816b2f0091fSVasu Dev 	unsigned int cpu;
817b2f0091fSVasu Dev 	u16 index;
818b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
81942e9a92fSRobert Love 
82042e9a92fSRobert Love 	/* allocate memory for exchange */
82142e9a92fSRobert Love 	ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
82242e9a92fSRobert Love 	if (!ep) {
82342e9a92fSRobert Love 		atomic_inc(&mp->stats.no_free_exch);
82442e9a92fSRobert Love 		goto out;
82542e9a92fSRobert Love 	}
82642e9a92fSRobert Love 	memset(ep, 0, sizeof(*ep));
82742e9a92fSRobert Love 
828a0548edfSDavidlohr Bueso 	cpu = raw_smp_processor_id();
829b2f0091fSVasu Dev 	pool = per_cpu_ptr(mp->pool, cpu);
830b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
8312034c19cSHillf Danton 
8322034c19cSHillf Danton 	/* peek cache of free slot */
8332034c19cSHillf Danton 	if (pool->left != FC_XID_UNKNOWN) {
834b73aa56eSHannes Reinecke 		if (!WARN_ON(fc_exch_ptr_get(pool, pool->left))) {
8352034c19cSHillf Danton 			index = pool->left;
8362034c19cSHillf Danton 			pool->left = FC_XID_UNKNOWN;
8372034c19cSHillf Danton 			goto hit;
8382034c19cSHillf Danton 		}
839b73aa56eSHannes Reinecke 	}
8402034c19cSHillf Danton 	if (pool->right != FC_XID_UNKNOWN) {
841b73aa56eSHannes Reinecke 		if (!WARN_ON(fc_exch_ptr_get(pool, pool->right))) {
8422034c19cSHillf Danton 			index = pool->right;
8432034c19cSHillf Danton 			pool->right = FC_XID_UNKNOWN;
8442034c19cSHillf Danton 			goto hit;
8452034c19cSHillf Danton 		}
846b73aa56eSHannes Reinecke 	}
8472034c19cSHillf Danton 
848b2f0091fSVasu Dev 	index = pool->next_index;
849b2f0091fSVasu Dev 	/* allocate new exch from pool */
850b2f0091fSVasu Dev 	while (fc_exch_ptr_get(pool, index)) {
851b2f0091fSVasu Dev 		index = index == mp->pool_max_index ? 0 : index + 1;
852b2f0091fSVasu Dev 		if (index == pool->next_index)
85342e9a92fSRobert Love 			goto err;
85442e9a92fSRobert Love 	}
855b2f0091fSVasu Dev 	pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
8562034c19cSHillf Danton hit:
85742e9a92fSRobert Love 	fc_exch_hold(ep);	/* hold for exch in mp */
85842e9a92fSRobert Love 	spin_lock_init(&ep->ex_lock);
85942e9a92fSRobert Love 	/*
86042e9a92fSRobert Love 	 * Hold exch lock for caller to prevent fc_exch_reset()
86142e9a92fSRobert Love 	 * from releasing exch	while fc_exch_alloc() caller is
86242e9a92fSRobert Love 	 * still working on exch.
86342e9a92fSRobert Love 	 */
86442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
86542e9a92fSRobert Love 
866b2f0091fSVasu Dev 	fc_exch_ptr_set(pool, index, ep);
867b2f0091fSVasu Dev 	list_add_tail(&ep->ex_list, &pool->ex_list);
86842e9a92fSRobert Love 	fc_seq_alloc(ep, ep->seq_id++);
869b2f0091fSVasu Dev 	pool->total_exches++;
870b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
87142e9a92fSRobert Love 
87242e9a92fSRobert Love 	/*
87342e9a92fSRobert Love 	 *  update exchange
87442e9a92fSRobert Love 	 */
875b2f0091fSVasu Dev 	ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
87642e9a92fSRobert Love 	ep->em = mp;
877b2f0091fSVasu Dev 	ep->pool = pool;
87852ff878cSVasu Dev 	ep->lp = lport;
87942e9a92fSRobert Love 	ep->f_ctl = FC_FC_FIRST_SEQ;	/* next seq is first seq */
88042e9a92fSRobert Love 	ep->rxid = FC_XID_UNKNOWN;
88142e9a92fSRobert Love 	ep->class = mp->class;
8827030fd62SBart Van Assche 	ep->resp_active = 0;
8837030fd62SBart Van Assche 	init_waitqueue_head(&ep->resp_wq);
88442e9a92fSRobert Love 	INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
88542e9a92fSRobert Love out:
88642e9a92fSRobert Love 	return ep;
88742e9a92fSRobert Love err:
888b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
88942e9a92fSRobert Love 	atomic_inc(&mp->stats.no_free_exch_xid);
89042e9a92fSRobert Love 	mempool_free(ep, mp->ep_pool);
89142e9a92fSRobert Love 	return NULL;
89242e9a92fSRobert Love }
89352ff878cSVasu Dev 
89452ff878cSVasu Dev /**
8953a3b42bfSRobert Love  * fc_exch_alloc() - Allocate an exchange from an EM on a
8963a3b42bfSRobert Love  *		     local port's list of EMs.
8973a3b42bfSRobert Love  * @lport: The local port that will own the exchange
8983a3b42bfSRobert Love  * @fp:	   The FC frame that the exchange will be for
89952ff878cSVasu Dev  *
9003a3b42bfSRobert Love  * This function walks the list of exchange manager(EM)
9013a3b42bfSRobert Love  * anchors to select an EM for a new exchange allocation. The
9023a3b42bfSRobert Love  * EM is selected when a NULL match function pointer is encountered
9033a3b42bfSRobert Love  * or when a call to a match function returns true.
90452ff878cSVasu Dev  */
fc_exch_alloc(struct fc_lport * lport,struct fc_frame * fp)905f8f91f3fSMartin K. Petersen static struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
9061a7b75aeSRobert Love 				     struct fc_frame *fp)
90752ff878cSVasu Dev {
90852ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
909f8f91f3fSMartin K. Petersen 	struct fc_exch *ep;
91052ff878cSVasu Dev 
911f8f91f3fSMartin K. Petersen 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
912f8f91f3fSMartin K. Petersen 		if (!ema->match || ema->match(fp)) {
913f8f91f3fSMartin K. Petersen 			ep = fc_exch_em_alloc(lport, ema->mp);
914f8f91f3fSMartin K. Petersen 			if (ep)
915f8f91f3fSMartin K. Petersen 				return ep;
916f8f91f3fSMartin K. Petersen 		}
917f8f91f3fSMartin K. Petersen 	}
91852ff878cSVasu Dev 	return NULL;
91952ff878cSVasu Dev }
92042e9a92fSRobert Love 
9213a3b42bfSRobert Love /**
9223a3b42bfSRobert Love  * fc_exch_find() - Lookup and hold an exchange
9233a3b42bfSRobert Love  * @mp:	 The exchange manager to lookup the exchange from
9243a3b42bfSRobert Love  * @xid: The XID of the exchange to look up
92542e9a92fSRobert Love  */
fc_exch_find(struct fc_exch_mgr * mp,u16 xid)92642e9a92fSRobert Love static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
92742e9a92fSRobert Love {
9289ca1e182SHannes Reinecke 	struct fc_lport *lport = mp->lport;
929b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
93042e9a92fSRobert Love 	struct fc_exch *ep = NULL;
931fa068832SChris Leech 	u16 cpu = xid & fc_cpu_mask;
932fa068832SChris Leech 
933d5c3eb26SChris Leech 	if (xid == FC_XID_UNKNOWN)
934d5c3eb26SChris Leech 		return NULL;
935d5c3eb26SChris Leech 
936fa068832SChris Leech 	if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
9379ca1e182SHannes Reinecke 		pr_err("host%u: lport %6.6x: xid %d invalid CPU %d\n:",
9389ca1e182SHannes Reinecke 		       lport->host->host_no, lport->port_id, xid, cpu);
939fa068832SChris Leech 		return NULL;
940fa068832SChris Leech 	}
94142e9a92fSRobert Love 
94242e9a92fSRobert Love 	if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
943fa068832SChris Leech 		pool = per_cpu_ptr(mp->pool, cpu);
944b2f0091fSVasu Dev 		spin_lock_bh(&pool->lock);
945b2f0091fSVasu Dev 		ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
9469ca1e182SHannes Reinecke 		if (ep == &fc_quarantine_exch) {
9479ca1e182SHannes Reinecke 			FC_LPORT_DBG(lport, "xid %x quarantined\n", xid);
9489ca1e182SHannes Reinecke 			ep = NULL;
9499ca1e182SHannes Reinecke 		}
9508d080236SBart Van Assche 		if (ep) {
9518d080236SBart Van Assche 			WARN_ON(ep->xid != xid);
95242e9a92fSRobert Love 			fc_exch_hold(ep);
9538d080236SBart Van Assche 		}
954b2f0091fSVasu Dev 		spin_unlock_bh(&pool->lock);
95542e9a92fSRobert Love 	}
95642e9a92fSRobert Love 	return ep;
95742e9a92fSRobert Love }
95842e9a92fSRobert Love 
9591a7b75aeSRobert Love 
9601a7b75aeSRobert Love /**
9611a7b75aeSRobert Love  * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
9621a7b75aeSRobert Love  *		    the memory allocated for the related objects may be freed.
9633a3b42bfSRobert Love  * @sp: The sequence that has completed
9647030fd62SBart Van Assche  *
9657030fd62SBart Van Assche  * Note: May sleep if invoked from outside a response handler.
9661a7b75aeSRobert Love  */
fc_exch_done(struct fc_seq * sp)967768c72ccSHannes Reinecke void fc_exch_done(struct fc_seq *sp)
96842e9a92fSRobert Love {
96942e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
97042e9a92fSRobert Love 	int rc;
97142e9a92fSRobert Love 
97242e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
97342e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
97442e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
9757030fd62SBart Van Assche 
9767030fd62SBart Van Assche 	fc_seq_set_resp(sp, NULL, ep->arg);
97742e9a92fSRobert Love 	if (!rc)
978b2f0091fSVasu Dev 		fc_exch_delete(ep);
97942e9a92fSRobert Love }
980768c72ccSHannes Reinecke EXPORT_SYMBOL(fc_exch_done);
98142e9a92fSRobert Love 
9823a3b42bfSRobert Love /**
9833a3b42bfSRobert Love  * fc_exch_resp() - Allocate a new exchange for a response frame
9843a3b42bfSRobert Love  * @lport: The local port that the exchange was for
9853a3b42bfSRobert Love  * @mp:	   The exchange manager to allocate the exchange from
9863a3b42bfSRobert Love  * @fp:	   The response frame
9873a3b42bfSRobert Love  *
98842e9a92fSRobert Love  * Sets the responder ID in the frame header.
98942e9a92fSRobert Love  */
fc_exch_resp(struct fc_lport * lport,struct fc_exch_mgr * mp,struct fc_frame * fp)99052ff878cSVasu Dev static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
99152ff878cSVasu Dev 				    struct fc_exch_mgr *mp,
99252ff878cSVasu Dev 				    struct fc_frame *fp)
99342e9a92fSRobert Love {
99442e9a92fSRobert Love 	struct fc_exch *ep;
99542e9a92fSRobert Love 	struct fc_frame_header *fh;
99642e9a92fSRobert Love 
99752ff878cSVasu Dev 	ep = fc_exch_alloc(lport, fp);
99842e9a92fSRobert Love 	if (ep) {
99942e9a92fSRobert Love 		ep->class = fc_frame_class(fp);
100042e9a92fSRobert Love 
100142e9a92fSRobert Love 		/*
100242e9a92fSRobert Love 		 * Set EX_CTX indicating we're responding on this exchange.
100342e9a92fSRobert Love 		 */
100442e9a92fSRobert Love 		ep->f_ctl |= FC_FC_EX_CTX;	/* we're responding */
100542e9a92fSRobert Love 		ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not new */
100642e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
100742e9a92fSRobert Love 		ep->sid = ntoh24(fh->fh_d_id);
100842e9a92fSRobert Love 		ep->did = ntoh24(fh->fh_s_id);
100942e9a92fSRobert Love 		ep->oid = ep->did;
101042e9a92fSRobert Love 
101142e9a92fSRobert Love 		/*
101242e9a92fSRobert Love 		 * Allocated exchange has placed the XID in the
101342e9a92fSRobert Love 		 * originator field. Move it to the responder field,
101442e9a92fSRobert Love 		 * and set the originator XID from the frame.
101542e9a92fSRobert Love 		 */
101642e9a92fSRobert Love 		ep->rxid = ep->xid;
101742e9a92fSRobert Love 		ep->oxid = ntohs(fh->fh_ox_id);
101842e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
101942e9a92fSRobert Love 		if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
102042e9a92fSRobert Love 			ep->esb_stat &= ~ESB_ST_SEQ_INIT;
102142e9a92fSRobert Love 
102242e9a92fSRobert Love 		fc_exch_hold(ep);	/* hold for caller */
102352ff878cSVasu Dev 		spin_unlock_bh(&ep->ex_lock);	/* lock from fc_exch_alloc */
102442e9a92fSRobert Love 	}
102542e9a92fSRobert Love 	return ep;
102642e9a92fSRobert Love }
102742e9a92fSRobert Love 
10283a3b42bfSRobert Love /**
10293a3b42bfSRobert Love  * fc_seq_lookup_recip() - Find a sequence where the other end
10303a3b42bfSRobert Love  *			   originated the sequence
10313a3b42bfSRobert Love  * @lport: The local port that the frame was sent to
10323a3b42bfSRobert Love  * @mp:	   The Exchange Manager to lookup the exchange from
10333a3b42bfSRobert Love  * @fp:	   The frame associated with the sequence we're looking for
10343a3b42bfSRobert Love  *
103542e9a92fSRobert Love  * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
103642e9a92fSRobert Love  * on the ep that should be released by the caller.
103742e9a92fSRobert Love  */
fc_seq_lookup_recip(struct fc_lport * lport,struct fc_exch_mgr * mp,struct fc_frame * fp)103852ff878cSVasu Dev static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
103952ff878cSVasu Dev 						 struct fc_exch_mgr *mp,
1040b2ab99c9SRobert Love 						 struct fc_frame *fp)
104142e9a92fSRobert Love {
104242e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
104342e9a92fSRobert Love 	struct fc_exch *ep = NULL;
104442e9a92fSRobert Love 	struct fc_seq *sp = NULL;
104542e9a92fSRobert Love 	enum fc_pf_rjt_reason reject = FC_RJT_NONE;
104642e9a92fSRobert Love 	u32 f_ctl;
104742e9a92fSRobert Love 	u16 xid;
104842e9a92fSRobert Love 
104942e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
105042e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
105142e9a92fSRobert Love 
105242e9a92fSRobert Love 	/*
105342e9a92fSRobert Love 	 * Lookup or create the exchange if we will be creating the sequence.
105442e9a92fSRobert Love 	 */
105542e9a92fSRobert Love 	if (f_ctl & FC_FC_EX_CTX) {
105642e9a92fSRobert Love 		xid = ntohs(fh->fh_ox_id);	/* we originated exch */
105742e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
105842e9a92fSRobert Love 		if (!ep) {
105942e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
106042e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
106142e9a92fSRobert Love 			goto out;
106242e9a92fSRobert Love 		}
106342e9a92fSRobert Love 		if (ep->rxid == FC_XID_UNKNOWN)
106442e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
106542e9a92fSRobert Love 		else if (ep->rxid != ntohs(fh->fh_rx_id)) {
106642e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
106742e9a92fSRobert Love 			goto rel;
106842e9a92fSRobert Love 		}
106942e9a92fSRobert Love 	} else {
107042e9a92fSRobert Love 		xid = ntohs(fh->fh_rx_id);	/* we are the responder */
107142e9a92fSRobert Love 
107242e9a92fSRobert Love 		/*
107342e9a92fSRobert Love 		 * Special case for MDS issuing an ELS TEST with a
107442e9a92fSRobert Love 		 * bad rxid of 0.
107542e9a92fSRobert Love 		 * XXX take this out once we do the proper reject.
107642e9a92fSRobert Love 		 */
107742e9a92fSRobert Love 		if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
107842e9a92fSRobert Love 		    fc_frame_payload_op(fp) == ELS_TEST) {
107942e9a92fSRobert Love 			fh->fh_rx_id = htons(FC_XID_UNKNOWN);
108042e9a92fSRobert Love 			xid = FC_XID_UNKNOWN;
108142e9a92fSRobert Love 		}
108242e9a92fSRobert Love 
108342e9a92fSRobert Love 		/*
108442e9a92fSRobert Love 		 * new sequence - find the exchange
108542e9a92fSRobert Love 		 */
108642e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
108742e9a92fSRobert Love 		if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
108842e9a92fSRobert Love 			if (ep) {
108942e9a92fSRobert Love 				atomic_inc(&mp->stats.xid_busy);
109042e9a92fSRobert Love 				reject = FC_RJT_RX_ID;
109142e9a92fSRobert Love 				goto rel;
109242e9a92fSRobert Love 			}
109352ff878cSVasu Dev 			ep = fc_exch_resp(lport, mp, fp);
109442e9a92fSRobert Love 			if (!ep) {
109542e9a92fSRobert Love 				reject = FC_RJT_EXCH_EST;	/* XXX */
109642e9a92fSRobert Love 				goto out;
109742e9a92fSRobert Love 			}
109842e9a92fSRobert Love 			xid = ep->xid;	/* get our XID */
109942e9a92fSRobert Love 		} else if (!ep) {
110042e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
110142e9a92fSRobert Love 			reject = FC_RJT_RX_ID;	/* XID not found */
110242e9a92fSRobert Love 			goto out;
110342e9a92fSRobert Love 		}
110442e9a92fSRobert Love 	}
110542e9a92fSRobert Love 
11065d73bea2SBart Van Assche 	spin_lock_bh(&ep->ex_lock);
110742e9a92fSRobert Love 	/*
110842e9a92fSRobert Love 	 * At this point, we have the exchange held.
110942e9a92fSRobert Love 	 * Find or create the sequence.
111042e9a92fSRobert Love 	 */
111142e9a92fSRobert Love 	if (fc_sof_is_init(fr_sof(fp))) {
1112a104c844SVasu Dev 		sp = &ep->seq;
111342e9a92fSRobert Love 		sp->ssb_stat |= SSB_ST_RESP;
1114b3667f91SJoe Eykholt 		sp->id = fh->fh_seq_id;
111542e9a92fSRobert Love 	} else {
111642e9a92fSRobert Love 		sp = &ep->seq;
111742e9a92fSRobert Love 		if (sp->id != fh->fh_seq_id) {
111842e9a92fSRobert Love 			atomic_inc(&mp->stats.seq_not_found);
1119e3e65c69SKiran Patil 			if (f_ctl & FC_FC_END_SEQ) {
1120e3e65c69SKiran Patil 				/*
1121e3e65c69SKiran Patil 				 * Update sequence_id based on incoming last
1122e3e65c69SKiran Patil 				 * frame of sequence exchange. This is needed
11231bd49b48SVasu Dev 				 * for FC target where DDP has been used
1124e3e65c69SKiran Patil 				 * on target where, stack is indicated only
1125e3e65c69SKiran Patil 				 * about last frame's (payload _header) header.
1126e3e65c69SKiran Patil 				 * Whereas "seq_id" which is part of
1127e3e65c69SKiran Patil 				 * frame_header is allocated by initiator
1128e3e65c69SKiran Patil 				 * which is totally different from "seq_id"
1129e3e65c69SKiran Patil 				 * allocated when XFER_RDY was sent by target.
1130e3e65c69SKiran Patil 				 * To avoid false -ve which results into not
1131e3e65c69SKiran Patil 				 * sending RSP, hence write request on other
1132e3e65c69SKiran Patil 				 * end never finishes.
1133e3e65c69SKiran Patil 				 */
1134e3e65c69SKiran Patil 				sp->ssb_stat |= SSB_ST_RESP;
1135e3e65c69SKiran Patil 				sp->id = fh->fh_seq_id;
1136e3e65c69SKiran Patil 			} else {
11375d73bea2SBart Van Assche 				spin_unlock_bh(&ep->ex_lock);
11385d73bea2SBart Van Assche 
1139e3e65c69SKiran Patil 				/* sequence/exch should exist */
1140e3e65c69SKiran Patil 				reject = FC_RJT_SEQ_ID;
114142e9a92fSRobert Love 				goto rel;
114242e9a92fSRobert Love 			}
114342e9a92fSRobert Love 		}
1144e3e65c69SKiran Patil 	}
114542e9a92fSRobert Love 	WARN_ON(ep != fc_seq_exch(sp));
114642e9a92fSRobert Love 
114742e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
114842e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
11495d73bea2SBart Van Assche 	spin_unlock_bh(&ep->ex_lock);
115042e9a92fSRobert Love 
115142e9a92fSRobert Love 	fr_seq(fp) = sp;
115242e9a92fSRobert Love out:
115342e9a92fSRobert Love 	return reject;
115442e9a92fSRobert Love rel:
115542e9a92fSRobert Love 	fc_exch_done(&ep->seq);
115642e9a92fSRobert Love 	fc_exch_release(ep);	/* hold from fc_exch_find/fc_exch_resp */
115742e9a92fSRobert Love 	return reject;
115842e9a92fSRobert Love }
115942e9a92fSRobert Love 
11603a3b42bfSRobert Love /**
11613a3b42bfSRobert Love  * fc_seq_lookup_orig() - Find a sequence where this end
11623a3b42bfSRobert Love  *			  originated the sequence
11633a3b42bfSRobert Love  * @mp:	   The Exchange Manager to lookup the exchange from
11643a3b42bfSRobert Love  * @fp:	   The frame associated with the sequence we're looking for
11653a3b42bfSRobert Love  *
116642e9a92fSRobert Love  * Does not hold the sequence for the caller.
116742e9a92fSRobert Love  */
fc_seq_lookup_orig(struct fc_exch_mgr * mp,struct fc_frame * fp)116842e9a92fSRobert Love static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
116942e9a92fSRobert Love 					 struct fc_frame *fp)
117042e9a92fSRobert Love {
117142e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
117242e9a92fSRobert Love 	struct fc_exch *ep;
117342e9a92fSRobert Love 	struct fc_seq *sp = NULL;
117442e9a92fSRobert Love 	u32 f_ctl;
117542e9a92fSRobert Love 	u16 xid;
117642e9a92fSRobert Love 
117742e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
117842e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
117942e9a92fSRobert Love 	xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
118042e9a92fSRobert Love 	ep = fc_exch_find(mp, xid);
118142e9a92fSRobert Love 	if (!ep)
118242e9a92fSRobert Love 		return NULL;
118342e9a92fSRobert Love 	if (ep->seq.id == fh->fh_seq_id) {
118442e9a92fSRobert Love 		/*
118542e9a92fSRobert Love 		 * Save the RX_ID if we didn't previously know it.
118642e9a92fSRobert Love 		 */
118742e9a92fSRobert Love 		sp = &ep->seq;
118842e9a92fSRobert Love 		if ((f_ctl & FC_FC_EX_CTX) != 0 &&
118942e9a92fSRobert Love 		    ep->rxid == FC_XID_UNKNOWN) {
119042e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
119142e9a92fSRobert Love 		}
119242e9a92fSRobert Love 	}
119342e9a92fSRobert Love 	fc_exch_release(ep);
119442e9a92fSRobert Love 	return sp;
119542e9a92fSRobert Love }
119642e9a92fSRobert Love 
11973a3b42bfSRobert Love /**
11983a3b42bfSRobert Love  * fc_exch_set_addr() - Set the source and destination IDs for an exchange
11993a3b42bfSRobert Love  * @ep:	     The exchange to set the addresses for
12003a3b42bfSRobert Love  * @orig_id: The originator's ID
12013a3b42bfSRobert Love  * @resp_id: The responder's ID
12023a3b42bfSRobert Love  *
120342e9a92fSRobert Love  * Note this must be done before the first sequence of the exchange is sent.
120442e9a92fSRobert Love  */
fc_exch_set_addr(struct fc_exch * ep,u32 orig_id,u32 resp_id)120542e9a92fSRobert Love static void fc_exch_set_addr(struct fc_exch *ep,
120642e9a92fSRobert Love 			     u32 orig_id, u32 resp_id)
120742e9a92fSRobert Love {
120842e9a92fSRobert Love 	ep->oid = orig_id;
120942e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP) {
121042e9a92fSRobert Love 		ep->sid = resp_id;
121142e9a92fSRobert Love 		ep->did = orig_id;
121242e9a92fSRobert Love 	} else {
121342e9a92fSRobert Love 		ep->sid = orig_id;
121442e9a92fSRobert Love 		ep->did = resp_id;
121542e9a92fSRobert Love 	}
121642e9a92fSRobert Love }
121742e9a92fSRobert Love 
12181a7b75aeSRobert Love /**
121925985edcSLucas De Marchi  * fc_seq_els_rsp_send() - Send an ELS response using information from
12203a3b42bfSRobert Love  *			   the existing sequence/exchange.
122192261156SJoe Eykholt  * @fp:	      The received frame
12223a3b42bfSRobert Love  * @els_cmd:  The ELS command to be sent
12233a3b42bfSRobert Love  * @els_data: The ELS data to be sent
122492261156SJoe Eykholt  *
122592261156SJoe Eykholt  * The received frame is not freed.
122642e9a92fSRobert Love  */
fc_seq_els_rsp_send(struct fc_frame * fp,enum fc_els_cmd els_cmd,struct fc_seq_els_data * els_data)12277ab24dd1SHannes Reinecke void fc_seq_els_rsp_send(struct fc_frame *fp, enum fc_els_cmd els_cmd,
122842e9a92fSRobert Love 			 struct fc_seq_els_data *els_data)
122942e9a92fSRobert Love {
123042e9a92fSRobert Love 	switch (els_cmd) {
123142e9a92fSRobert Love 	case ELS_LS_RJT:
123292261156SJoe Eykholt 		fc_seq_ls_rjt(fp, els_data->reason, els_data->explan);
123342e9a92fSRobert Love 		break;
123442e9a92fSRobert Love 	case ELS_LS_ACC:
123592261156SJoe Eykholt 		fc_seq_ls_acc(fp);
123642e9a92fSRobert Love 		break;
123742e9a92fSRobert Love 	case ELS_RRQ:
123892261156SJoe Eykholt 		fc_exch_els_rrq(fp);
123942e9a92fSRobert Love 		break;
124042e9a92fSRobert Love 	case ELS_REC:
124192261156SJoe Eykholt 		fc_exch_els_rec(fp);
124242e9a92fSRobert Love 		break;
124342e9a92fSRobert Love 	default:
124492261156SJoe Eykholt 		FC_LPORT_DBG(fr_dev(fp), "Invalid ELS CMD:%x\n", els_cmd);
124542e9a92fSRobert Love 	}
124642e9a92fSRobert Love }
12477ab24dd1SHannes Reinecke EXPORT_SYMBOL_GPL(fc_seq_els_rsp_send);
124842e9a92fSRobert Love 
12493a3b42bfSRobert Love /**
12503a3b42bfSRobert Love  * fc_seq_send_last() - Send a sequence that is the last in the exchange
12513a3b42bfSRobert Love  * @sp:	     The sequence that is to be sent
12523a3b42bfSRobert Love  * @fp:	     The frame that will be sent on the sequence
12533a3b42bfSRobert Love  * @rctl:    The R_CTL information to be sent
12543a3b42bfSRobert Love  * @fh_type: The frame header type
125542e9a92fSRobert Love  */
fc_seq_send_last(struct fc_seq * sp,struct fc_frame * fp,enum fc_rctl rctl,enum fc_fh_type fh_type)125642e9a92fSRobert Love static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
125742e9a92fSRobert Love 			     enum fc_rctl rctl, enum fc_fh_type fh_type)
125842e9a92fSRobert Love {
125942e9a92fSRobert Love 	u32 f_ctl;
126042e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
126142e9a92fSRobert Love 
126242e9a92fSRobert Love 	f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
126342e9a92fSRobert Love 	f_ctl |= ep->f_ctl;
126442e9a92fSRobert Love 	fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
1265fb00cc23SNeil Horman 	fc_seq_send_locked(ep->lp, sp, fp);
126642e9a92fSRobert Love }
126742e9a92fSRobert Love 
12683a3b42bfSRobert Love /**
12693a3b42bfSRobert Love  * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
12703a3b42bfSRobert Love  * @sp:	   The sequence to send the ACK on
12713a3b42bfSRobert Love  * @rx_fp: The received frame that is being acknoledged
12723a3b42bfSRobert Love  *
127342e9a92fSRobert Love  * Send ACK_1 (or equiv.) indicating we received something.
127442e9a92fSRobert Love  */
fc_seq_send_ack(struct fc_seq * sp,const struct fc_frame * rx_fp)127542e9a92fSRobert Love static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
127642e9a92fSRobert Love {
127742e9a92fSRobert Love 	struct fc_frame *fp;
127842e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
127942e9a92fSRobert Love 	struct fc_frame_header *fh;
128042e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
12813a3b42bfSRobert Love 	struct fc_lport *lport = ep->lp;
128242e9a92fSRobert Love 	unsigned int f_ctl;
128342e9a92fSRobert Love 
128442e9a92fSRobert Love 	/*
128542e9a92fSRobert Love 	 * Don't send ACKs for class 3.
128642e9a92fSRobert Love 	 */
128742e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(rx_fp))) {
12883a3b42bfSRobert Love 		fp = fc_frame_alloc(lport, 0);
128957d3ec7eSHannes Reinecke 		if (!fp) {
129057d3ec7eSHannes Reinecke 			FC_EXCH_DBG(ep, "Drop ACK request, out of memory\n");
129142e9a92fSRobert Love 			return;
129257d3ec7eSHannes Reinecke 		}
129342e9a92fSRobert Love 
129442e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
129542e9a92fSRobert Love 		fh->fh_r_ctl = FC_RCTL_ACK_1;
129642e9a92fSRobert Love 		fh->fh_type = FC_TYPE_BLS;
129742e9a92fSRobert Love 
129842e9a92fSRobert Love 		/*
129942e9a92fSRobert Love 		 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
130042e9a92fSRobert Love 		 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
130142e9a92fSRobert Love 		 * Bits 9-8 are meaningful (retransmitted or unidirectional).
130242e9a92fSRobert Love 		 * Last ACK uses bits 7-6 (continue sequence),
130342e9a92fSRobert Love 		 * bits 5-4 are meaningful (what kind of ACK to use).
130442e9a92fSRobert Love 		 */
130542e9a92fSRobert Love 		rx_fh = fc_frame_header_get(rx_fp);
130642e9a92fSRobert Love 		f_ctl = ntoh24(rx_fh->fh_f_ctl);
130742e9a92fSRobert Love 		f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
130842e9a92fSRobert Love 			FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
130942e9a92fSRobert Love 			FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
131042e9a92fSRobert Love 			FC_FC_RETX_SEQ | FC_FC_UNI_TX;
131142e9a92fSRobert Love 		f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
131242e9a92fSRobert Love 		hton24(fh->fh_f_ctl, f_ctl);
131342e9a92fSRobert Love 
131442e9a92fSRobert Love 		fc_exch_setup_hdr(ep, fp, f_ctl);
131542e9a92fSRobert Love 		fh->fh_seq_id = rx_fh->fh_seq_id;
131642e9a92fSRobert Love 		fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
131742e9a92fSRobert Love 		fh->fh_parm_offset = htonl(1);	/* ack single frame */
131842e9a92fSRobert Love 
131942e9a92fSRobert Love 		fr_sof(fp) = fr_sof(rx_fp);
132042e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
132142e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_T;
132242e9a92fSRobert Love 		else
132342e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
132442e9a92fSRobert Love 
13253a3b42bfSRobert Love 		lport->tt.frame_send(lport, fp);
132642e9a92fSRobert Love 	}
132742e9a92fSRobert Love }
132842e9a92fSRobert Love 
13293a3b42bfSRobert Love /**
13303a3b42bfSRobert Love  * fc_exch_send_ba_rjt() - Send BLS Reject
13313a3b42bfSRobert Love  * @rx_fp:  The frame being rejected
13323a3b42bfSRobert Love  * @reason: The reason the frame is being rejected
133325985edcSLucas De Marchi  * @explan: The explanation for the rejection
13343a3b42bfSRobert Love  *
133542e9a92fSRobert Love  * This is for rejecting BA_ABTS only.
133642e9a92fSRobert Love  */
fc_exch_send_ba_rjt(struct fc_frame * rx_fp,enum fc_ba_rjt_reason reason,enum fc_ba_rjt_explan explan)1337b2ab99c9SRobert Love static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1338b2ab99c9SRobert Love 				enum fc_ba_rjt_reason reason,
133942e9a92fSRobert Love 				enum fc_ba_rjt_explan explan)
134042e9a92fSRobert Love {
134142e9a92fSRobert Love 	struct fc_frame *fp;
134242e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
134342e9a92fSRobert Love 	struct fc_frame_header *fh;
134442e9a92fSRobert Love 	struct fc_ba_rjt *rp;
134557d3ec7eSHannes Reinecke 	struct fc_seq *sp;
13463a3b42bfSRobert Love 	struct fc_lport *lport;
134742e9a92fSRobert Love 	unsigned int f_ctl;
134842e9a92fSRobert Love 
13493a3b42bfSRobert Love 	lport = fr_dev(rx_fp);
135057d3ec7eSHannes Reinecke 	sp = fr_seq(rx_fp);
13513a3b42bfSRobert Love 	fp = fc_frame_alloc(lport, sizeof(*rp));
135257d3ec7eSHannes Reinecke 	if (!fp) {
135357d3ec7eSHannes Reinecke 		FC_EXCH_DBG(fc_seq_exch(sp),
135457d3ec7eSHannes Reinecke 			     "Drop BA_RJT request, out of memory\n");
135542e9a92fSRobert Love 		return;
135657d3ec7eSHannes Reinecke 	}
135742e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
135842e9a92fSRobert Love 	rx_fh = fc_frame_header_get(rx_fp);
135942e9a92fSRobert Love 
136042e9a92fSRobert Love 	memset(fh, 0, sizeof(*fh) + sizeof(*rp));
136142e9a92fSRobert Love 
136242e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
136342e9a92fSRobert Love 	rp->br_reason = reason;
136442e9a92fSRobert Love 	rp->br_explan = explan;
136542e9a92fSRobert Love 
136642e9a92fSRobert Love 	/*
136742e9a92fSRobert Love 	 * seq_id, cs_ctl, df_ctl and param/offset are zero.
136842e9a92fSRobert Love 	 */
136942e9a92fSRobert Love 	memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
137042e9a92fSRobert Love 	memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
13711d490ce3SJoe Eykholt 	fh->fh_ox_id = rx_fh->fh_ox_id;
13721d490ce3SJoe Eykholt 	fh->fh_rx_id = rx_fh->fh_rx_id;
137342e9a92fSRobert Love 	fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
137442e9a92fSRobert Love 	fh->fh_r_ctl = FC_RCTL_BA_RJT;
137542e9a92fSRobert Love 	fh->fh_type = FC_TYPE_BLS;
137642e9a92fSRobert Love 
137742e9a92fSRobert Love 	/*
137842e9a92fSRobert Love 	 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
137942e9a92fSRobert Love 	 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
138042e9a92fSRobert Love 	 * Bits 9-8 are meaningful (retransmitted or unidirectional).
138142e9a92fSRobert Love 	 * Last ACK uses bits 7-6 (continue sequence),
138242e9a92fSRobert Love 	 * bits 5-4 are meaningful (what kind of ACK to use).
138342e9a92fSRobert Love 	 * Always set LAST_SEQ, END_SEQ.
138442e9a92fSRobert Love 	 */
138542e9a92fSRobert Love 	f_ctl = ntoh24(rx_fh->fh_f_ctl);
138642e9a92fSRobert Love 	f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
138742e9a92fSRobert Love 		FC_FC_END_CONN | FC_FC_SEQ_INIT |
138842e9a92fSRobert Love 		FC_FC_RETX_SEQ | FC_FC_UNI_TX;
138942e9a92fSRobert Love 	f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
139042e9a92fSRobert Love 	f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
139142e9a92fSRobert Love 	f_ctl &= ~FC_FC_FIRST_SEQ;
139242e9a92fSRobert Love 	hton24(fh->fh_f_ctl, f_ctl);
139342e9a92fSRobert Love 
139442e9a92fSRobert Love 	fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
139542e9a92fSRobert Love 	fr_eof(fp) = FC_EOF_T;
139642e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(fp)))
139742e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
139842e9a92fSRobert Love 
13993a3b42bfSRobert Love 	lport->tt.frame_send(lport, fp);
140042e9a92fSRobert Love }
140142e9a92fSRobert Love 
14023a3b42bfSRobert Love /**
14033a3b42bfSRobert Love  * fc_exch_recv_abts() - Handle an incoming ABTS
14043a3b42bfSRobert Love  * @ep:	   The exchange the abort was on
14053a3b42bfSRobert Love  * @rx_fp: The ABTS frame
14063a3b42bfSRobert Love  *
14073a3b42bfSRobert Love  * This would be for target mode usually, but could be due to lost
14083a3b42bfSRobert Love  * FCP transfer ready, confirm or RRQ. We always handle this as an
14093a3b42bfSRobert Love  * exchange abort, ignoring the parameter.
141042e9a92fSRobert Love  */
fc_exch_recv_abts(struct fc_exch * ep,struct fc_frame * rx_fp)141142e9a92fSRobert Love static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
141242e9a92fSRobert Love {
141342e9a92fSRobert Love 	struct fc_frame *fp;
141442e9a92fSRobert Love 	struct fc_ba_acc *ap;
141542e9a92fSRobert Love 	struct fc_frame_header *fh;
141642e9a92fSRobert Love 	struct fc_seq *sp;
141742e9a92fSRobert Love 
141842e9a92fSRobert Love 	if (!ep)
141942e9a92fSRobert Love 		goto reject;
1420f95b35cfSBart Van Assche 
142157d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "exch: ABTS received\n");
1422f95b35cfSBart Van Assche 	fp = fc_frame_alloc(ep->lp, sizeof(*ap));
142357d3ec7eSHannes Reinecke 	if (!fp) {
142457d3ec7eSHannes Reinecke 		FC_EXCH_DBG(ep, "Drop ABTS request, out of memory\n");
1425f95b35cfSBart Van Assche 		goto free;
142657d3ec7eSHannes Reinecke 	}
1427f95b35cfSBart Van Assche 
142842e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
142942e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
143042e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
143157d3ec7eSHannes Reinecke 		FC_EXCH_DBG(ep, "exch: ABTS rejected, exchange complete\n");
1432f95b35cfSBart Van Assche 		fc_frame_free(fp);
143342e9a92fSRobert Love 		goto reject;
143442e9a92fSRobert Love 	}
1435cae7b6ddSBart Van Assche 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
1436cae7b6ddSBart Van Assche 		ep->esb_stat |= ESB_ST_REC_QUAL;
143742e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for REC_QUAL */
1438cae7b6ddSBart Van Assche 	}
143942e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
144042e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
144142e9a92fSRobert Love 	ap = fc_frame_payload_get(fp, sizeof(*ap));
144242e9a92fSRobert Love 	memset(ap, 0, sizeof(*ap));
144342e9a92fSRobert Love 	sp = &ep->seq;
144442e9a92fSRobert Love 	ap->ba_high_seq_cnt = htons(0xffff);
144542e9a92fSRobert Love 	if (sp->ssb_stat & SSB_ST_RESP) {
144642e9a92fSRobert Love 		ap->ba_seq_id = sp->id;
144742e9a92fSRobert Love 		ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
144842e9a92fSRobert Love 		ap->ba_high_seq_cnt = fh->fh_seq_cnt;
144942e9a92fSRobert Love 		ap->ba_low_seq_cnt = htons(sp->cnt);
145042e9a92fSRobert Love 	}
1451a7e84f2bSVasu Dev 	sp = fc_seq_start_next_locked(sp);
145242e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1453cae7b6ddSBart Van Assche 	ep->esb_stat |= ESB_ST_ABNORMAL;
1454fb00cc23SNeil Horman 	spin_unlock_bh(&ep->ex_lock);
1455f95b35cfSBart Van Assche 
1456f95b35cfSBart Van Assche free:
145742e9a92fSRobert Love 	fc_frame_free(rx_fp);
145842e9a92fSRobert Love 	return;
145942e9a92fSRobert Love 
146042e9a92fSRobert Love reject:
146142e9a92fSRobert Love 	fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1462f95b35cfSBart Van Assche 	goto free;
146342e9a92fSRobert Love }
146442e9a92fSRobert Love 
14653a3b42bfSRobert Love /**
1466239e8104SJoe Eykholt  * fc_seq_assign() - Assign exchange and sequence for incoming request
1467239e8104SJoe Eykholt  * @lport: The local port that received the request
1468239e8104SJoe Eykholt  * @fp:    The request frame
1469239e8104SJoe Eykholt  *
1470239e8104SJoe Eykholt  * On success, the sequence pointer will be returned and also in fr_seq(@fp).
147162bdb645SJoe Eykholt  * A reference will be held on the exchange/sequence for the caller, which
147262bdb645SJoe Eykholt  * must call fc_seq_release().
1473239e8104SJoe Eykholt  */
fc_seq_assign(struct fc_lport * lport,struct fc_frame * fp)147496d564e2SHannes Reinecke struct fc_seq *fc_seq_assign(struct fc_lport *lport, struct fc_frame *fp)
1475239e8104SJoe Eykholt {
1476239e8104SJoe Eykholt 	struct fc_exch_mgr_anchor *ema;
1477239e8104SJoe Eykholt 
1478239e8104SJoe Eykholt 	WARN_ON(lport != fr_dev(fp));
1479239e8104SJoe Eykholt 	WARN_ON(fr_seq(fp));
1480239e8104SJoe Eykholt 	fr_seq(fp) = NULL;
1481239e8104SJoe Eykholt 
1482239e8104SJoe Eykholt 	list_for_each_entry(ema, &lport->ema_list, ema_list)
1483239e8104SJoe Eykholt 		if ((!ema->match || ema->match(fp)) &&
1484530994d6SHillf Danton 		    fc_seq_lookup_recip(lport, ema->mp, fp) == FC_RJT_NONE)
1485239e8104SJoe Eykholt 			break;
1486239e8104SJoe Eykholt 	return fr_seq(fp);
1487239e8104SJoe Eykholt }
148896d564e2SHannes Reinecke EXPORT_SYMBOL(fc_seq_assign);
1489239e8104SJoe Eykholt 
1490239e8104SJoe Eykholt /**
149162bdb645SJoe Eykholt  * fc_seq_release() - Release the hold
149262bdb645SJoe Eykholt  * @sp:    The sequence.
149362bdb645SJoe Eykholt  */
fc_seq_release(struct fc_seq * sp)14949625cc48SHannes Reinecke void fc_seq_release(struct fc_seq *sp)
149562bdb645SJoe Eykholt {
149662bdb645SJoe Eykholt 	fc_exch_release(fc_seq_exch(sp));
149762bdb645SJoe Eykholt }
14989625cc48SHannes Reinecke EXPORT_SYMBOL(fc_seq_release);
149962bdb645SJoe Eykholt 
150062bdb645SJoe Eykholt /**
150192261156SJoe Eykholt  * fc_exch_recv_req() - Handler for an incoming request
15023a3b42bfSRobert Love  * @lport: The local port that received the request
15033a3b42bfSRobert Love  * @mp:	   The EM that the exchange is on
15043a3b42bfSRobert Love  * @fp:	   The request frame
150592261156SJoe Eykholt  *
150692261156SJoe Eykholt  * This is used when the other end is originating the exchange
150792261156SJoe Eykholt  * and the sequence.
150842e9a92fSRobert Love  */
fc_exch_recv_req(struct fc_lport * lport,struct fc_exch_mgr * mp,struct fc_frame * fp)15093a3b42bfSRobert Love static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
151042e9a92fSRobert Love 			     struct fc_frame *fp)
151142e9a92fSRobert Love {
151242e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
151342e9a92fSRobert Love 	struct fc_seq *sp = NULL;
151442e9a92fSRobert Love 	struct fc_exch *ep = NULL;
151542e9a92fSRobert Love 	enum fc_pf_rjt_reason reject;
151642e9a92fSRobert Love 
1517174e1ebfSChris Leech 	/* We can have the wrong fc_lport at this point with NPIV, which is a
1518174e1ebfSChris Leech 	 * problem now that we know a new exchange needs to be allocated
1519174e1ebfSChris Leech 	 */
15203a3b42bfSRobert Love 	lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
15213a3b42bfSRobert Love 	if (!lport) {
1522174e1ebfSChris Leech 		fc_frame_free(fp);
1523174e1ebfSChris Leech 		return;
1524174e1ebfSChris Leech 	}
152592261156SJoe Eykholt 	fr_dev(fp) = lport;
1526174e1ebfSChris Leech 
152792261156SJoe Eykholt 	BUG_ON(fr_seq(fp));		/* XXX remove later */
152892261156SJoe Eykholt 
152992261156SJoe Eykholt 	/*
153092261156SJoe Eykholt 	 * If the RX_ID is 0xffff, don't allocate an exchange.
153192261156SJoe Eykholt 	 * The upper-level protocol may request one later, if needed.
153292261156SJoe Eykholt 	 */
153392261156SJoe Eykholt 	if (fh->fh_rx_id == htons(FC_XID_UNKNOWN))
1534c5cb444cSHannes Reinecke 		return fc_lport_recv(lport, fp);
153592261156SJoe Eykholt 
15363a3b42bfSRobert Love 	reject = fc_seq_lookup_recip(lport, mp, fp);
153742e9a92fSRobert Love 	if (reject == FC_RJT_NONE) {
153842e9a92fSRobert Love 		sp = fr_seq(fp);	/* sequence will be held */
153942e9a92fSRobert Love 		ep = fc_seq_exch(sp);
154042e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
1541f60e12e9SJoe Eykholt 		ep->encaps = fr_encaps(fp);
154242e9a92fSRobert Love 
154342e9a92fSRobert Love 		/*
154442e9a92fSRobert Love 		 * Call the receive function.
154542e9a92fSRobert Love 		 *
154642e9a92fSRobert Love 		 * The receive function may allocate a new sequence
154742e9a92fSRobert Love 		 * over the old one, so we shouldn't change the
154842e9a92fSRobert Love 		 * sequence after this.
154942e9a92fSRobert Love 		 *
155042e9a92fSRobert Love 		 * The frame will be freed by the receive function.
155142e9a92fSRobert Love 		 * If new exch resp handler is valid then call that
155242e9a92fSRobert Love 		 * first.
155342e9a92fSRobert Love 		 */
15547030fd62SBart Van Assche 		if (!fc_invoke_resp(ep, sp, fp))
1555c5cb444cSHannes Reinecke 			fc_lport_recv(lport, fp);
155642e9a92fSRobert Love 		fc_exch_release(ep);	/* release from lookup */
155742e9a92fSRobert Love 	} else {
15583a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
15593a3b42bfSRobert Love 			     reject);
156042e9a92fSRobert Love 		fc_frame_free(fp);
156142e9a92fSRobert Love 	}
156242e9a92fSRobert Love }
156342e9a92fSRobert Love 
15643a3b42bfSRobert Love /**
15653a3b42bfSRobert Love  * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
15663a3b42bfSRobert Love  *			     end is the originator of the sequence that is a
15673a3b42bfSRobert Love  *			     response to our initial exchange
15683a3b42bfSRobert Love  * @mp: The EM that the exchange is on
15693a3b42bfSRobert Love  * @fp: The response frame
157042e9a92fSRobert Love  */
fc_exch_recv_seq_resp(struct fc_exch_mgr * mp,struct fc_frame * fp)157142e9a92fSRobert Love static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
157242e9a92fSRobert Love {
157342e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
157442e9a92fSRobert Love 	struct fc_seq *sp;
157542e9a92fSRobert Love 	struct fc_exch *ep;
157642e9a92fSRobert Love 	enum fc_sof sof;
157742e9a92fSRobert Love 	u32 f_ctl;
157842e9a92fSRobert Love 	int rc;
157942e9a92fSRobert Love 
158042e9a92fSRobert Love 	ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
158142e9a92fSRobert Love 	if (!ep) {
158242e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
158342e9a92fSRobert Love 		goto out;
158442e9a92fSRobert Love 	}
158530121d14SSteve Ma 	if (ep->esb_stat & ESB_ST_COMPLETE) {
158630121d14SSteve Ma 		atomic_inc(&mp->stats.xid_not_found);
15878236554aSHillf Danton 		goto rel;
158830121d14SSteve Ma 	}
158942e9a92fSRobert Love 	if (ep->rxid == FC_XID_UNKNOWN)
159042e9a92fSRobert Love 		ep->rxid = ntohs(fh->fh_rx_id);
159142e9a92fSRobert Love 	if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
159242e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
159342e9a92fSRobert Love 		goto rel;
159442e9a92fSRobert Love 	}
159542e9a92fSRobert Love 	if (ep->did != ntoh24(fh->fh_s_id) &&
159642e9a92fSRobert Love 	    ep->did != FC_FID_FLOGI) {
159742e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
159842e9a92fSRobert Love 		goto rel;
159942e9a92fSRobert Love 	}
160042e9a92fSRobert Love 	sof = fr_sof(fp);
160142e9a92fSRobert Love 	sp = &ep->seq;
1602b3667f91SJoe Eykholt 	if (fc_sof_is_init(sof)) {
1603a104c844SVasu Dev 		sp->ssb_stat |= SSB_ST_RESP;
1604b3667f91SJoe Eykholt 		sp->id = fh->fh_seq_id;
160542e9a92fSRobert Love 	}
1606a104c844SVasu Dev 
160742e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
160842e9a92fSRobert Love 	fr_seq(fp) = sp;
16095d73bea2SBart Van Assche 
16105d73bea2SBart Van Assche 	spin_lock_bh(&ep->ex_lock);
161142e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
161242e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
16135d73bea2SBart Van Assche 	spin_unlock_bh(&ep->ex_lock);
161442e9a92fSRobert Love 
161542e9a92fSRobert Love 	if (fc_sof_needs_ack(sof))
161642e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
161742e9a92fSRobert Love 
161842e9a92fSRobert Love 	if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
161942e9a92fSRobert Love 	    (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
162042e9a92fSRobert Love 	    (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
162142e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
162242e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
162342e9a92fSRobert Love 		WARN_ON(fc_seq_exch(sp) != ep);
162442e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
1625b2b0f16fSJaved Hasan 		if (!rc) {
1626b2f0091fSVasu Dev 			fc_exch_delete(ep);
1627b2b0f16fSJaved Hasan 		} else {
1628b2b0f16fSJaved Hasan 			FC_EXCH_DBG(ep, "ep is completed already,"
1629b2b0f16fSJaved Hasan 					"hence skip calling the resp\n");
1630b2b0f16fSJaved Hasan 			goto skip_resp;
1631b2b0f16fSJaved Hasan 		}
163242e9a92fSRobert Love 	}
163342e9a92fSRobert Love 
163442e9a92fSRobert Love 	/*
163542e9a92fSRobert Love 	 * Call the receive function.
163642e9a92fSRobert Love 	 * The sequence is held (has a refcnt) for us,
163742e9a92fSRobert Love 	 * but not for the receive function.
163842e9a92fSRobert Love 	 *
163942e9a92fSRobert Love 	 * The receive function may allocate a new sequence
164042e9a92fSRobert Love 	 * over the old one, so we shouldn't change the
164142e9a92fSRobert Love 	 * sequence after this.
164242e9a92fSRobert Love 	 *
164342e9a92fSRobert Love 	 * The frame will be freed by the receive function.
164442e9a92fSRobert Love 	 * If new exch resp handler is valid then call that
164542e9a92fSRobert Love 	 * first.
164642e9a92fSRobert Love 	 */
1647f6979adeSBart Van Assche 	if (!fc_invoke_resp(ep, sp, fp))
1648f6979adeSBart Van Assche 		fc_frame_free(fp);
16497030fd62SBart Van Assche 
1650b2b0f16fSJaved Hasan skip_resp:
165142e9a92fSRobert Love 	fc_exch_release(ep);
165242e9a92fSRobert Love 	return;
165342e9a92fSRobert Love rel:
165442e9a92fSRobert Love 	fc_exch_release(ep);
165542e9a92fSRobert Love out:
165642e9a92fSRobert Love 	fc_frame_free(fp);
165742e9a92fSRobert Love }
165842e9a92fSRobert Love 
16593a3b42bfSRobert Love /**
16603a3b42bfSRobert Love  * fc_exch_recv_resp() - Handler for a sequence where other end is
16613a3b42bfSRobert Love  *			 responding to our sequence
16623a3b42bfSRobert Love  * @mp: The EM that the exchange is on
16633a3b42bfSRobert Love  * @fp: The response frame
166442e9a92fSRobert Love  */
fc_exch_recv_resp(struct fc_exch_mgr * mp,struct fc_frame * fp)166542e9a92fSRobert Love static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
166642e9a92fSRobert Love {
166742e9a92fSRobert Love 	struct fc_seq *sp;
166842e9a92fSRobert Love 
166942e9a92fSRobert Love 	sp = fc_seq_lookup_orig(mp, fp);	/* doesn't hold sequence */
1670d459b7eaSRobert Love 
1671d459b7eaSRobert Love 	if (!sp)
167242e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
1673d459b7eaSRobert Love 	else
167442e9a92fSRobert Love 		atomic_inc(&mp->stats.non_bls_resp);
1675d459b7eaSRobert Love 
167642e9a92fSRobert Love 	fc_frame_free(fp);
167742e9a92fSRobert Love }
167842e9a92fSRobert Love 
16793a3b42bfSRobert Love /**
16803a3b42bfSRobert Love  * fc_exch_abts_resp() - Handler for a response to an ABT
16813a3b42bfSRobert Love  * @ep: The exchange that the frame is on
16823a3b42bfSRobert Love  * @fp: The response frame
16833a3b42bfSRobert Love  *
16843a3b42bfSRobert Love  * This response would be to an ABTS cancelling an exchange or sequence.
16853a3b42bfSRobert Love  * The response can be either BA_ACC or BA_RJT
168642e9a92fSRobert Love  */
fc_exch_abts_resp(struct fc_exch * ep,struct fc_frame * fp)168742e9a92fSRobert Love static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
168842e9a92fSRobert Love {
168942e9a92fSRobert Love 	struct fc_frame_header *fh;
169042e9a92fSRobert Love 	struct fc_ba_acc *ap;
169142e9a92fSRobert Love 	struct fc_seq *sp;
169242e9a92fSRobert Love 	u16 low;
169342e9a92fSRobert Love 	u16 high;
169442e9a92fSRobert Love 	int rc = 1, has_rec = 0;
169542e9a92fSRobert Love 
169642e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
16977414705eSRobert Love 	FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
16987414705eSRobert Love 		    fc_exch_rctl_name(fh->fh_r_ctl));
169942e9a92fSRobert Love 
1700b29a4f30SVasu Dev 	if (cancel_delayed_work_sync(&ep->timeout_work)) {
17013a292605SRobert Love 		FC_EXCH_DBG(ep, "Exchange timer canceled due to ABTS response\n");
170242e9a92fSRobert Love 		fc_exch_release(ep);	/* release from pending timer hold */
1703271add11SJianglei Nie 		return;
1704b29a4f30SVasu Dev 	}
170542e9a92fSRobert Love 
170642e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
170742e9a92fSRobert Love 	switch (fh->fh_r_ctl) {
170842e9a92fSRobert Love 	case FC_RCTL_BA_ACC:
170942e9a92fSRobert Love 		ap = fc_frame_payload_get(fp, sizeof(*ap));
171042e9a92fSRobert Love 		if (!ap)
171142e9a92fSRobert Love 			break;
171242e9a92fSRobert Love 
171342e9a92fSRobert Love 		/*
171442e9a92fSRobert Love 		 * Decide whether to establish a Recovery Qualifier.
171542e9a92fSRobert Love 		 * We do this if there is a non-empty SEQ_CNT range and
171642e9a92fSRobert Love 		 * SEQ_ID is the same as the one we aborted.
171742e9a92fSRobert Love 		 */
171842e9a92fSRobert Love 		low = ntohs(ap->ba_low_seq_cnt);
171942e9a92fSRobert Love 		high = ntohs(ap->ba_high_seq_cnt);
172042e9a92fSRobert Love 		if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
172142e9a92fSRobert Love 		    (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
172242e9a92fSRobert Love 		     ap->ba_seq_id == ep->seq_id) && low != high) {
172342e9a92fSRobert Love 			ep->esb_stat |= ESB_ST_REC_QUAL;
172442e9a92fSRobert Love 			fc_exch_hold(ep);  /* hold for recovery qualifier */
172542e9a92fSRobert Love 			has_rec = 1;
172642e9a92fSRobert Love 		}
172742e9a92fSRobert Love 		break;
172842e9a92fSRobert Love 	case FC_RCTL_BA_RJT:
172942e9a92fSRobert Love 		break;
173042e9a92fSRobert Love 	default:
173142e9a92fSRobert Love 		break;
173242e9a92fSRobert Love 	}
173342e9a92fSRobert Love 
173442e9a92fSRobert Love 	/* do we need to do some other checks here. Can we reuse more of
173542e9a92fSRobert Love 	 * fc_exch_recv_seq_resp
173642e9a92fSRobert Love 	 */
173742e9a92fSRobert Love 	sp = &ep->seq;
173842e9a92fSRobert Love 	/*
173942e9a92fSRobert Love 	 * do we want to check END_SEQ as well as LAST_SEQ here?
174042e9a92fSRobert Love 	 */
174142e9a92fSRobert Love 	if (ep->fh_type != FC_TYPE_FCP &&
174242e9a92fSRobert Love 	    ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
174342e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
174442e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
17457030fd62SBart Van Assche 
17467030fd62SBart Van Assche 	fc_exch_hold(ep);
174742e9a92fSRobert Love 	if (!rc)
1748b2f0091fSVasu Dev 		fc_exch_delete(ep);
1749f6979adeSBart Van Assche 	if (!fc_invoke_resp(ep, sp, fp))
1750f6979adeSBart Van Assche 		fc_frame_free(fp);
175142e9a92fSRobert Love 	if (has_rec)
175242e9a92fSRobert Love 		fc_exch_timer_set(ep, ep->r_a_tov);
17537030fd62SBart Van Assche 	fc_exch_release(ep);
175442e9a92fSRobert Love }
175542e9a92fSRobert Love 
17563a3b42bfSRobert Love /**
17573a3b42bfSRobert Love  * fc_exch_recv_bls() - Handler for a BLS sequence
17583a3b42bfSRobert Love  * @mp: The EM that the exchange is on
17593a3b42bfSRobert Love  * @fp: The request frame
17603a3b42bfSRobert Love  *
17613a3b42bfSRobert Love  * The BLS frame is always a sequence initiated by the remote side.
176242e9a92fSRobert Love  * We may be either the originator or recipient of the exchange.
176342e9a92fSRobert Love  */
fc_exch_recv_bls(struct fc_exch_mgr * mp,struct fc_frame * fp)176442e9a92fSRobert Love static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
176542e9a92fSRobert Love {
176642e9a92fSRobert Love 	struct fc_frame_header *fh;
176742e9a92fSRobert Love 	struct fc_exch *ep;
176842e9a92fSRobert Love 	u32 f_ctl;
176942e9a92fSRobert Love 
177042e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
177142e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
177242e9a92fSRobert Love 	fr_seq(fp) = NULL;
177342e9a92fSRobert Love 
177442e9a92fSRobert Love 	ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
177542e9a92fSRobert Love 			  ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
177642e9a92fSRobert Love 	if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
177742e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
177842e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
177942e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
178042e9a92fSRobert Love 	}
178142e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_CTX) {
178242e9a92fSRobert Love 		/*
178342e9a92fSRobert Love 		 * A response to a sequence we initiated.
178442e9a92fSRobert Love 		 * This should only be ACKs for class 2 or F.
178542e9a92fSRobert Love 		 */
178642e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
178742e9a92fSRobert Love 		case FC_RCTL_ACK_1:
178842e9a92fSRobert Love 		case FC_RCTL_ACK_0:
178942e9a92fSRobert Love 			break;
179042e9a92fSRobert Love 		default:
1791d4042e9cSBhanu Prakash Gollapudi 			if (ep)
1792b20d9bfdSBart Van Assche 				FC_EXCH_DBG(ep, "BLS rctl %x - %s received\n",
179342e9a92fSRobert Love 					    fh->fh_r_ctl,
179442e9a92fSRobert Love 					    fc_exch_rctl_name(fh->fh_r_ctl));
179542e9a92fSRobert Love 			break;
179642e9a92fSRobert Love 		}
179742e9a92fSRobert Love 		fc_frame_free(fp);
179842e9a92fSRobert Love 	} else {
179942e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
180042e9a92fSRobert Love 		case FC_RCTL_BA_RJT:
180142e9a92fSRobert Love 		case FC_RCTL_BA_ACC:
180242e9a92fSRobert Love 			if (ep)
180342e9a92fSRobert Love 				fc_exch_abts_resp(ep, fp);
180442e9a92fSRobert Love 			else
180542e9a92fSRobert Love 				fc_frame_free(fp);
180642e9a92fSRobert Love 			break;
180742e9a92fSRobert Love 		case FC_RCTL_BA_ABTS:
1808b73aa56eSHannes Reinecke 			if (ep)
180942e9a92fSRobert Love 				fc_exch_recv_abts(ep, fp);
1810b73aa56eSHannes Reinecke 			else
1811b73aa56eSHannes Reinecke 				fc_frame_free(fp);
181242e9a92fSRobert Love 			break;
181342e9a92fSRobert Love 		default:			/* ignore junk */
181442e9a92fSRobert Love 			fc_frame_free(fp);
181542e9a92fSRobert Love 			break;
181642e9a92fSRobert Love 		}
181742e9a92fSRobert Love 	}
181842e9a92fSRobert Love 	if (ep)
181942e9a92fSRobert Love 		fc_exch_release(ep);	/* release hold taken by fc_exch_find */
182042e9a92fSRobert Love }
182142e9a92fSRobert Love 
18223a3b42bfSRobert Love /**
18233a3b42bfSRobert Love  * fc_seq_ls_acc() - Accept sequence with LS_ACC
182492261156SJoe Eykholt  * @rx_fp: The received frame, not freed here.
18253a3b42bfSRobert Love  *
182642e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
182742e9a92fSRobert Love  * originator will repeat the sequence.
182842e9a92fSRobert Love  */
fc_seq_ls_acc(struct fc_frame * rx_fp)182992261156SJoe Eykholt static void fc_seq_ls_acc(struct fc_frame *rx_fp)
183042e9a92fSRobert Love {
183192261156SJoe Eykholt 	struct fc_lport *lport;
183242e9a92fSRobert Love 	struct fc_els_ls_acc *acc;
183342e9a92fSRobert Love 	struct fc_frame *fp;
183457d3ec7eSHannes Reinecke 	struct fc_seq *sp;
183542e9a92fSRobert Love 
183692261156SJoe Eykholt 	lport = fr_dev(rx_fp);
183757d3ec7eSHannes Reinecke 	sp = fr_seq(rx_fp);
183892261156SJoe Eykholt 	fp = fc_frame_alloc(lport, sizeof(*acc));
183957d3ec7eSHannes Reinecke 	if (!fp) {
184057d3ec7eSHannes Reinecke 		FC_EXCH_DBG(fc_seq_exch(sp),
184157d3ec7eSHannes Reinecke 			    "exch: drop LS_ACC, out of memory\n");
184292261156SJoe Eykholt 		return;
184357d3ec7eSHannes Reinecke 	}
184442e9a92fSRobert Love 	acc = fc_frame_payload_get(fp, sizeof(*acc));
184542e9a92fSRobert Love 	memset(acc, 0, sizeof(*acc));
184642e9a92fSRobert Love 	acc->la_cmd = ELS_LS_ACC;
184792261156SJoe Eykholt 	fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
184892261156SJoe Eykholt 	lport->tt.frame_send(lport, fp);
184942e9a92fSRobert Love }
185042e9a92fSRobert Love 
18513a3b42bfSRobert Love /**
18523a3b42bfSRobert Love  * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
185392261156SJoe Eykholt  * @rx_fp: The received frame, not freed here.
18543a3b42bfSRobert Love  * @reason: The reason the sequence is being rejected
185592261156SJoe Eykholt  * @explan: The explanation for the rejection
18563a3b42bfSRobert Love  *
185742e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
185842e9a92fSRobert Love  * originator will repeat the sequence.
185942e9a92fSRobert Love  */
fc_seq_ls_rjt(struct fc_frame * rx_fp,enum fc_els_rjt_reason reason,enum fc_els_rjt_explan explan)186092261156SJoe Eykholt static void fc_seq_ls_rjt(struct fc_frame *rx_fp, enum fc_els_rjt_reason reason,
186142e9a92fSRobert Love 			  enum fc_els_rjt_explan explan)
186242e9a92fSRobert Love {
186392261156SJoe Eykholt 	struct fc_lport *lport;
186442e9a92fSRobert Love 	struct fc_els_ls_rjt *rjt;
186542e9a92fSRobert Love 	struct fc_frame *fp;
186657d3ec7eSHannes Reinecke 	struct fc_seq *sp;
186742e9a92fSRobert Love 
186892261156SJoe Eykholt 	lport = fr_dev(rx_fp);
186957d3ec7eSHannes Reinecke 	sp = fr_seq(rx_fp);
187092261156SJoe Eykholt 	fp = fc_frame_alloc(lport, sizeof(*rjt));
187157d3ec7eSHannes Reinecke 	if (!fp) {
187257d3ec7eSHannes Reinecke 		FC_EXCH_DBG(fc_seq_exch(sp),
187357d3ec7eSHannes Reinecke 			    "exch: drop LS_ACC, out of memory\n");
187492261156SJoe Eykholt 		return;
187557d3ec7eSHannes Reinecke 	}
187642e9a92fSRobert Love 	rjt = fc_frame_payload_get(fp, sizeof(*rjt));
187742e9a92fSRobert Love 	memset(rjt, 0, sizeof(*rjt));
187842e9a92fSRobert Love 	rjt->er_cmd = ELS_LS_RJT;
187942e9a92fSRobert Love 	rjt->er_reason = reason;
188042e9a92fSRobert Love 	rjt->er_explan = explan;
188192261156SJoe Eykholt 	fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
188292261156SJoe Eykholt 	lport->tt.frame_send(lport, fp);
188342e9a92fSRobert Love }
188442e9a92fSRobert Love 
18853a3b42bfSRobert Love /**
18863a3b42bfSRobert Love  * fc_exch_reset() - Reset an exchange
18873a3b42bfSRobert Love  * @ep: The exchange to be reset
18887030fd62SBart Van Assche  *
18897030fd62SBart Van Assche  * Note: May sleep if invoked from outside a response handler.
18903a3b42bfSRobert Love  */
fc_exch_reset(struct fc_exch * ep)189142e9a92fSRobert Love static void fc_exch_reset(struct fc_exch *ep)
189242e9a92fSRobert Love {
189342e9a92fSRobert Love 	struct fc_seq *sp;
189442e9a92fSRobert Love 	int rc = 1;
189542e9a92fSRobert Love 
189642e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
189742e9a92fSRobert Love 	ep->state |= FC_EX_RST_CLEANUP;
1898b29a4f30SVasu Dev 	fc_exch_timer_cancel(ep);
189942e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL)
190042e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec_qual */
190142e9a92fSRobert Love 	ep->esb_stat &= ~ESB_ST_REC_QUAL;
190242e9a92fSRobert Love 	sp = &ep->seq;
190342e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
190442e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
19057030fd62SBart Van Assche 
19067030fd62SBart Van Assche 	fc_exch_hold(ep);
19077030fd62SBart Van Assche 
1908b2b0f16fSJaved Hasan 	if (!rc) {
1909b2f0091fSVasu Dev 		fc_exch_delete(ep);
1910b2b0f16fSJaved Hasan 	} else {
1911b2b0f16fSJaved Hasan 		FC_EXCH_DBG(ep, "ep is completed already,"
1912b2b0f16fSJaved Hasan 				"hence skip calling the resp\n");
1913b2b0f16fSJaved Hasan 		goto skip_resp;
1914b2b0f16fSJaved Hasan 	}
191542e9a92fSRobert Love 
19167030fd62SBart Van Assche 	fc_invoke_resp(ep, sp, ERR_PTR(-FC_EX_CLOSED));
1917b2b0f16fSJaved Hasan skip_resp:
19187030fd62SBart Van Assche 	fc_seq_set_resp(sp, NULL, ep->arg);
19197030fd62SBart Van Assche 	fc_exch_release(ep);
192042e9a92fSRobert Love }
192142e9a92fSRobert Love 
1922b2f0091fSVasu Dev /**
19233a3b42bfSRobert Love  * fc_exch_pool_reset() - Reset a per cpu exchange pool
19243a3b42bfSRobert Love  * @lport: The local port that the exchange pool is on
19253a3b42bfSRobert Love  * @pool:  The exchange pool to be reset
19263a3b42bfSRobert Love  * @sid:   The source ID
19273a3b42bfSRobert Love  * @did:   The destination ID
1928b2f0091fSVasu Dev  *
19293a3b42bfSRobert Love  * Resets a per cpu exches pool, releasing all of its sequences
19303a3b42bfSRobert Love  * and exchanges. If sid is non-zero then reset only exchanges
19313a3b42bfSRobert Love  * we sourced from the local port's FID. If did is non-zero then
19323a3b42bfSRobert Love  * only reset exchanges destined for the local port's FID.
193342e9a92fSRobert Love  */
fc_exch_pool_reset(struct fc_lport * lport,struct fc_exch_pool * pool,u32 sid,u32 did)1934b2f0091fSVasu Dev static void fc_exch_pool_reset(struct fc_lport *lport,
1935b2f0091fSVasu Dev 			       struct fc_exch_pool *pool,
1936b2f0091fSVasu Dev 			       u32 sid, u32 did)
193742e9a92fSRobert Love {
193842e9a92fSRobert Love 	struct fc_exch *ep;
193942e9a92fSRobert Love 	struct fc_exch *next;
194042e9a92fSRobert Love 
1941b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
194242e9a92fSRobert Love restart:
1943b2f0091fSVasu Dev 	list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
1944b2f0091fSVasu Dev 		if ((lport == ep->lp) &&
194552ff878cSVasu Dev 		    (sid == 0 || sid == ep->sid) &&
194642e9a92fSRobert Love 		    (did == 0 || did == ep->did)) {
194742e9a92fSRobert Love 			fc_exch_hold(ep);
1948b2f0091fSVasu Dev 			spin_unlock_bh(&pool->lock);
194942e9a92fSRobert Love 
195042e9a92fSRobert Love 			fc_exch_reset(ep);
195142e9a92fSRobert Love 
195242e9a92fSRobert Love 			fc_exch_release(ep);
1953b2f0091fSVasu Dev 			spin_lock_bh(&pool->lock);
195442e9a92fSRobert Love 
195542e9a92fSRobert Love 			/*
195652ff878cSVasu Dev 			 * must restart loop incase while lock
195752ff878cSVasu Dev 			 * was down multiple eps were released.
195842e9a92fSRobert Love 			 */
195942e9a92fSRobert Love 			goto restart;
196042e9a92fSRobert Love 		}
196142e9a92fSRobert Love 	}
1962b6e3c840SVasu Dev 	pool->next_index = 0;
1963b6e3c840SVasu Dev 	pool->left = FC_XID_UNKNOWN;
1964b6e3c840SVasu Dev 	pool->right = FC_XID_UNKNOWN;
1965b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
1966b2f0091fSVasu Dev }
1967b2f0091fSVasu Dev 
1968b2f0091fSVasu Dev /**
19693a3b42bfSRobert Love  * fc_exch_mgr_reset() - Reset all EMs of a local port
19703a3b42bfSRobert Love  * @lport: The local port whose EMs are to be reset
19713a3b42bfSRobert Love  * @sid:   The source ID
19723a3b42bfSRobert Love  * @did:   The destination ID
1973b2f0091fSVasu Dev  *
19743a3b42bfSRobert Love  * Reset all EMs associated with a given local port. Release all
19753a3b42bfSRobert Love  * sequences and exchanges. If sid is non-zero then reset only the
19763a3b42bfSRobert Love  * exchanges sent from the local port's FID. If did is non-zero then
19773a3b42bfSRobert Love  * reset only exchanges destined for the local port's FID.
1978b2f0091fSVasu Dev  */
fc_exch_mgr_reset(struct fc_lport * lport,u32 sid,u32 did)1979b2f0091fSVasu Dev void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1980b2f0091fSVasu Dev {
1981b2f0091fSVasu Dev 	struct fc_exch_mgr_anchor *ema;
1982b2f0091fSVasu Dev 	unsigned int cpu;
1983b2f0091fSVasu Dev 
1984b2f0091fSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
1985b2f0091fSVasu Dev 		for_each_possible_cpu(cpu)
1986b2f0091fSVasu Dev 			fc_exch_pool_reset(lport,
1987b2f0091fSVasu Dev 					   per_cpu_ptr(ema->mp->pool, cpu),
1988b2f0091fSVasu Dev 					   sid, did);
198942e9a92fSRobert Love 	}
199052ff878cSVasu Dev }
199142e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_reset);
199242e9a92fSRobert Love 
19933a3b42bfSRobert Love /**
199492261156SJoe Eykholt  * fc_exch_lookup() - find an exchange
199592261156SJoe Eykholt  * @lport: The local port
199692261156SJoe Eykholt  * @xid: The exchange ID
199792261156SJoe Eykholt  *
199892261156SJoe Eykholt  * Returns exchange pointer with hold for caller, or NULL if not found.
199992261156SJoe Eykholt  */
fc_exch_lookup(struct fc_lport * lport,u32 xid)200092261156SJoe Eykholt static struct fc_exch *fc_exch_lookup(struct fc_lport *lport, u32 xid)
200192261156SJoe Eykholt {
200292261156SJoe Eykholt 	struct fc_exch_mgr_anchor *ema;
200392261156SJoe Eykholt 
200492261156SJoe Eykholt 	list_for_each_entry(ema, &lport->ema_list, ema_list)
200592261156SJoe Eykholt 		if (ema->mp->min_xid <= xid && xid <= ema->mp->max_xid)
200692261156SJoe Eykholt 			return fc_exch_find(ema->mp, xid);
200792261156SJoe Eykholt 	return NULL;
200892261156SJoe Eykholt }
200992261156SJoe Eykholt 
201092261156SJoe Eykholt /**
20113a3b42bfSRobert Love  * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
201292261156SJoe Eykholt  * @rfp: The REC frame, not freed here.
20133a3b42bfSRobert Love  *
201442e9a92fSRobert Love  * Note that the requesting port may be different than the S_ID in the request.
201542e9a92fSRobert Love  */
fc_exch_els_rec(struct fc_frame * rfp)201692261156SJoe Eykholt static void fc_exch_els_rec(struct fc_frame *rfp)
201742e9a92fSRobert Love {
201892261156SJoe Eykholt 	struct fc_lport *lport;
201942e9a92fSRobert Love 	struct fc_frame *fp;
202042e9a92fSRobert Love 	struct fc_exch *ep;
202142e9a92fSRobert Love 	struct fc_els_rec *rp;
202242e9a92fSRobert Love 	struct fc_els_rec_acc *acc;
202342e9a92fSRobert Love 	enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
202442e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
202542e9a92fSRobert Love 	u32 sid;
2026e0a25286SHannes Reinecke 	u16 xid, rxid, oxid;
202742e9a92fSRobert Love 
202892261156SJoe Eykholt 	lport = fr_dev(rfp);
202942e9a92fSRobert Love 	rp = fc_frame_payload_get(rfp, sizeof(*rp));
203042e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
203142e9a92fSRobert Love 	if (!rp)
203242e9a92fSRobert Love 		goto reject;
203342e9a92fSRobert Love 	sid = ntoh24(rp->rec_s_id);
203442e9a92fSRobert Love 	rxid = ntohs(rp->rec_rx_id);
203542e9a92fSRobert Love 	oxid = ntohs(rp->rec_ox_id);
203642e9a92fSRobert Love 
203742e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
2038e0a25286SHannes Reinecke 	if (sid == fc_host_port_id(lport->host))
2039e0a25286SHannes Reinecke 		xid = oxid;
2040e0a25286SHannes Reinecke 	else
2041e0a25286SHannes Reinecke 		xid = rxid;
2042e0a25286SHannes Reinecke 	if (xid == FC_XID_UNKNOWN) {
2043e0a25286SHannes Reinecke 		FC_LPORT_DBG(lport,
2044e0a25286SHannes Reinecke 			     "REC request from %x: invalid rxid %x oxid %x\n",
2045e0a25286SHannes Reinecke 			     sid, rxid, oxid);
2046e0a25286SHannes Reinecke 		goto reject;
2047e0a25286SHannes Reinecke 	}
2048e0a25286SHannes Reinecke 	ep = fc_exch_lookup(lport, xid);
204957d3ec7eSHannes Reinecke 	if (!ep) {
205057d3ec7eSHannes Reinecke 		FC_LPORT_DBG(lport,
205157d3ec7eSHannes Reinecke 			     "REC request from %x: rxid %x oxid %x not found\n",
205257d3ec7eSHannes Reinecke 			     sid, rxid, oxid);
205342e9a92fSRobert Love 		goto reject;
205457d3ec7eSHannes Reinecke 	}
205557d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "REC request from %x: rxid %x oxid %x\n",
205657d3ec7eSHannes Reinecke 		    sid, rxid, oxid);
205792261156SJoe Eykholt 	if (ep->oid != sid || oxid != ep->oxid)
205892261156SJoe Eykholt 		goto rel;
205992261156SJoe Eykholt 	if (rxid != FC_XID_UNKNOWN && rxid != ep->rxid)
206092261156SJoe Eykholt 		goto rel;
206192261156SJoe Eykholt 	fp = fc_frame_alloc(lport, sizeof(*acc));
206257d3ec7eSHannes Reinecke 	if (!fp) {
206357d3ec7eSHannes Reinecke 		FC_EXCH_DBG(ep, "Drop REC request, out of memory\n");
206442e9a92fSRobert Love 		goto out;
206557d3ec7eSHannes Reinecke 	}
206692261156SJoe Eykholt 
206742e9a92fSRobert Love 	acc = fc_frame_payload_get(fp, sizeof(*acc));
206842e9a92fSRobert Love 	memset(acc, 0, sizeof(*acc));
206942e9a92fSRobert Love 	acc->reca_cmd = ELS_LS_ACC;
207042e9a92fSRobert Love 	acc->reca_ox_id = rp->rec_ox_id;
207142e9a92fSRobert Love 	memcpy(acc->reca_ofid, rp->rec_s_id, 3);
207242e9a92fSRobert Love 	acc->reca_rx_id = htons(ep->rxid);
207342e9a92fSRobert Love 	if (ep->sid == ep->oid)
207442e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->did);
207542e9a92fSRobert Love 	else
207642e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->sid);
207742e9a92fSRobert Love 	acc->reca_fc4value = htonl(ep->seq.rec_data);
207842e9a92fSRobert Love 	acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
207942e9a92fSRobert Love 						 ESB_ST_SEQ_INIT |
208042e9a92fSRobert Love 						 ESB_ST_COMPLETE));
208192261156SJoe Eykholt 	fc_fill_reply_hdr(fp, rfp, FC_RCTL_ELS_REP, 0);
208292261156SJoe Eykholt 	lport->tt.frame_send(lport, fp);
208342e9a92fSRobert Love out:
208442e9a92fSRobert Love 	fc_exch_release(ep);
208542e9a92fSRobert Love 	return;
208642e9a92fSRobert Love 
208742e9a92fSRobert Love rel:
208842e9a92fSRobert Love 	fc_exch_release(ep);
208942e9a92fSRobert Love reject:
209092261156SJoe Eykholt 	fc_seq_ls_rjt(rfp, reason, explan);
209142e9a92fSRobert Love }
209242e9a92fSRobert Love 
20933a3b42bfSRobert Love /**
20943a3b42bfSRobert Love  * fc_exch_rrq_resp() - Handler for RRQ responses
20953a3b42bfSRobert Love  * @sp:	 The sequence that the RRQ is on
20963a3b42bfSRobert Love  * @fp:	 The RRQ frame
20973a3b42bfSRobert Love  * @arg: The exchange that the RRQ is on
209842e9a92fSRobert Love  *
209942e9a92fSRobert Love  * TODO: fix error handler.
210042e9a92fSRobert Love  */
fc_exch_rrq_resp(struct fc_seq * sp,struct fc_frame * fp,void * arg)210142e9a92fSRobert Love static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
210242e9a92fSRobert Love {
210342e9a92fSRobert Love 	struct fc_exch *aborted_ep = arg;
210442e9a92fSRobert Love 	unsigned int op;
210542e9a92fSRobert Love 
210642e9a92fSRobert Love 	if (IS_ERR(fp)) {
210742e9a92fSRobert Love 		int err = PTR_ERR(fp);
210842e9a92fSRobert Love 
210978342da3SVasu Dev 		if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
211042e9a92fSRobert Love 			goto cleanup;
21117414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
21127414705eSRobert Love 			    "frame error %d\n", err);
211342e9a92fSRobert Love 		return;
211442e9a92fSRobert Love 	}
211542e9a92fSRobert Love 
211642e9a92fSRobert Love 	op = fc_frame_payload_op(fp);
211742e9a92fSRobert Love 	fc_frame_free(fp);
211842e9a92fSRobert Love 
211942e9a92fSRobert Love 	switch (op) {
212042e9a92fSRobert Love 	case ELS_LS_RJT:
2121b20d9bfdSBart Van Assche 		FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ\n");
2122df561f66SGustavo A. R. Silva 		fallthrough;
212342e9a92fSRobert Love 	case ELS_LS_ACC:
212442e9a92fSRobert Love 		goto cleanup;
212542e9a92fSRobert Love 	default:
2126b20d9bfdSBart Van Assche 		FC_EXCH_DBG(aborted_ep, "unexpected response op %x for RRQ\n",
2127b20d9bfdSBart Van Assche 			    op);
212842e9a92fSRobert Love 		return;
212942e9a92fSRobert Love 	}
213042e9a92fSRobert Love 
213142e9a92fSRobert Love cleanup:
213242e9a92fSRobert Love 	fc_exch_done(&aborted_ep->seq);
213342e9a92fSRobert Love 	/* drop hold for rec qual */
213442e9a92fSRobert Love 	fc_exch_release(aborted_ep);
213542e9a92fSRobert Love }
213642e9a92fSRobert Love 
21371a7b75aeSRobert Love 
21381a7b75aeSRobert Love /**
21393a3b42bfSRobert Love  * fc_exch_seq_send() - Send a frame using a new exchange and sequence
21403a3b42bfSRobert Love  * @lport:	The local port to send the frame on
21413a3b42bfSRobert Love  * @fp:		The frame to be sent
21423a3b42bfSRobert Love  * @resp:	The response handler for this request
21433a3b42bfSRobert Love  * @destructor: The destructor for the exchange
21443a3b42bfSRobert Love  * @arg:	The argument to be passed to the response handler
21453a3b42bfSRobert Love  * @timer_msec: The timeout period for the exchange
21463a3b42bfSRobert Love  *
21473afd2d15SHannes Reinecke  * The exchange response handler is set in this routine to resp()
21483afd2d15SHannes Reinecke  * function pointer. It can be called in two scenarios: if a timeout
21493afd2d15SHannes Reinecke  * occurs or if a response frame is received for the exchange. The
21503afd2d15SHannes Reinecke  * fc_frame pointer in response handler will also indicate timeout
21513afd2d15SHannes Reinecke  * as error using IS_ERR related macros.
21523afd2d15SHannes Reinecke  *
21533afd2d15SHannes Reinecke  * The exchange destructor handler is also set in this routine.
21543afd2d15SHannes Reinecke  * The destructor handler is invoked by EM layer when exchange
21553afd2d15SHannes Reinecke  * is about to free, this can be used by caller to free its
21563afd2d15SHannes Reinecke  * resources along with exchange free.
21573afd2d15SHannes Reinecke  *
21583afd2d15SHannes Reinecke  * The arg is passed back to resp and destructor handler.
21593afd2d15SHannes Reinecke  *
21603afd2d15SHannes Reinecke  * The timeout value (in msec) for an exchange is set if non zero
21613afd2d15SHannes Reinecke  * timer_msec argument is specified. The timer is canceled when
21623afd2d15SHannes Reinecke  * it fires or when the exchange is done. The exchange timeout handler
21633afd2d15SHannes Reinecke  * is registered by EM layer.
21643afd2d15SHannes Reinecke  *
21653a3b42bfSRobert Love  * The frame pointer with some of the header's fields must be
21663a3b42bfSRobert Love  * filled before calling this routine, those fields are:
21673a3b42bfSRobert Love  *
21683a3b42bfSRobert Love  * - routing control
21693a3b42bfSRobert Love  * - FC port did
21703a3b42bfSRobert Love  * - FC port sid
21713a3b42bfSRobert Love  * - FC header type
21723a3b42bfSRobert Love  * - frame control
21733a3b42bfSRobert Love  * - parameter or relative offset
21741a7b75aeSRobert Love  */
fc_exch_seq_send(struct fc_lport * lport,struct fc_frame * fp,void (* resp)(struct fc_seq *,struct fc_frame * fp,void * arg),void (* destructor)(struct fc_seq *,void *),void * arg,u32 timer_msec)21753afd2d15SHannes Reinecke struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
21761a7b75aeSRobert Love 				struct fc_frame *fp,
21771a7b75aeSRobert Love 				void (*resp)(struct fc_seq *,
21781a7b75aeSRobert Love 					     struct fc_frame *fp,
21791a7b75aeSRobert Love 					     void *arg),
21803afd2d15SHannes Reinecke 				void (*destructor)(struct fc_seq *, void *),
21811a7b75aeSRobert Love 				void *arg, u32 timer_msec)
21821a7b75aeSRobert Love {
21831a7b75aeSRobert Love 	struct fc_exch *ep;
21841a7b75aeSRobert Love 	struct fc_seq *sp = NULL;
21851a7b75aeSRobert Love 	struct fc_frame_header *fh;
21863ee17f59SYi Zou 	struct fc_fcp_pkt *fsp = NULL;
21871a7b75aeSRobert Love 	int rc = 1;
21881a7b75aeSRobert Love 
21893a3b42bfSRobert Love 	ep = fc_exch_alloc(lport, fp);
21901a7b75aeSRobert Love 	if (!ep) {
21911a7b75aeSRobert Love 		fc_frame_free(fp);
21921a7b75aeSRobert Love 		return NULL;
21931a7b75aeSRobert Love 	}
21941a7b75aeSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT;
21951a7b75aeSRobert Love 	fh = fc_frame_header_get(fp);
21961a7b75aeSRobert Love 	fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
21971a7b75aeSRobert Love 	ep->resp = resp;
21981a7b75aeSRobert Love 	ep->destructor = destructor;
21991a7b75aeSRobert Love 	ep->arg = arg;
2200f7ce413cSHannes Reinecke 	ep->r_a_tov = lport->r_a_tov;
22013a3b42bfSRobert Love 	ep->lp = lport;
22021a7b75aeSRobert Love 	sp = &ep->seq;
22031a7b75aeSRobert Love 
22041a7b75aeSRobert Love 	ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
22051a7b75aeSRobert Love 	ep->f_ctl = ntoh24(fh->fh_f_ctl);
22061a7b75aeSRobert Love 	fc_exch_setup_hdr(ep, fp, ep->f_ctl);
22071a7b75aeSRobert Love 	sp->cnt++;
22081a7b75aeSRobert Love 
22093ee17f59SYi Zou 	if (ep->xid <= lport->lro_xid && fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) {
22103ee17f59SYi Zou 		fsp = fr_fsp(fp);
22111a7b75aeSRobert Love 		fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
22123ee17f59SYi Zou 	}
22131a7b75aeSRobert Love 
22143a3b42bfSRobert Love 	if (unlikely(lport->tt.frame_send(lport, fp)))
22151a7b75aeSRobert Love 		goto err;
22161a7b75aeSRobert Love 
22171a7b75aeSRobert Love 	if (timer_msec)
22181a7b75aeSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
22191a7b75aeSRobert Love 	ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not first seq */
22201a7b75aeSRobert Love 
22211a7b75aeSRobert Love 	if (ep->f_ctl & FC_FC_SEQ_INIT)
22221a7b75aeSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
22231a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
22241a7b75aeSRobert Love 	return sp;
22251a7b75aeSRobert Love err:
22263ee17f59SYi Zou 	if (fsp)
22273ee17f59SYi Zou 		fc_fcp_ddp_done(fsp);
22281a7b75aeSRobert Love 	rc = fc_exch_done_locked(ep);
22291a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
22301a7b75aeSRobert Love 	if (!rc)
22311a7b75aeSRobert Love 		fc_exch_delete(ep);
22321a7b75aeSRobert Love 	return NULL;
22331a7b75aeSRobert Love }
22343afd2d15SHannes Reinecke EXPORT_SYMBOL(fc_exch_seq_send);
22351a7b75aeSRobert Love 
22363a3b42bfSRobert Love /**
22373a3b42bfSRobert Love  * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
22383a3b42bfSRobert Love  * @ep: The exchange to send the RRQ on
22393a3b42bfSRobert Love  *
224042e9a92fSRobert Love  * This tells the remote port to stop blocking the use of
224142e9a92fSRobert Love  * the exchange and the seq_cnt range.
224242e9a92fSRobert Love  */
fc_exch_rrq(struct fc_exch * ep)224342e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *ep)
224442e9a92fSRobert Love {
22453a3b42bfSRobert Love 	struct fc_lport *lport;
224642e9a92fSRobert Love 	struct fc_els_rrq *rrq;
224742e9a92fSRobert Love 	struct fc_frame *fp;
224842e9a92fSRobert Love 	u32 did;
224942e9a92fSRobert Love 
22503a3b42bfSRobert Love 	lport = ep->lp;
225142e9a92fSRobert Love 
22523a3b42bfSRobert Love 	fp = fc_frame_alloc(lport, sizeof(*rrq));
225342e9a92fSRobert Love 	if (!fp)
2254a0cc1eccSVasu Dev 		goto retry;
2255a0cc1eccSVasu Dev 
225642e9a92fSRobert Love 	rrq = fc_frame_payload_get(fp, sizeof(*rrq));
225742e9a92fSRobert Love 	memset(rrq, 0, sizeof(*rrq));
225842e9a92fSRobert Love 	rrq->rrq_cmd = ELS_RRQ;
225942e9a92fSRobert Love 	hton24(rrq->rrq_s_id, ep->sid);
226042e9a92fSRobert Love 	rrq->rrq_ox_id = htons(ep->oxid);
226142e9a92fSRobert Love 	rrq->rrq_rx_id = htons(ep->rxid);
226242e9a92fSRobert Love 
226342e9a92fSRobert Love 	did = ep->did;
226442e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP)
226542e9a92fSRobert Love 		did = ep->sid;
226642e9a92fSRobert Love 
226742e9a92fSRobert Love 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
22687b2787ecSRobert Love 		       lport->port_id, FC_TYPE_ELS,
226942e9a92fSRobert Love 		       FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
227042e9a92fSRobert Love 
22713a3b42bfSRobert Love 	if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
22723a3b42bfSRobert Love 			     lport->e_d_tov))
2273a0cc1eccSVasu Dev 		return;
2274a0cc1eccSVasu Dev 
2275a0cc1eccSVasu Dev retry:
227657d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "exch: RRQ send failed\n");
2277a0cc1eccSVasu Dev 	spin_lock_bh(&ep->ex_lock);
2278a0cc1eccSVasu Dev 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
2279a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
2280a0cc1eccSVasu Dev 		/* drop hold for rec qual */
2281a0cc1eccSVasu Dev 		fc_exch_release(ep);
228242e9a92fSRobert Love 		return;
228342e9a92fSRobert Love 	}
2284a0cc1eccSVasu Dev 	ep->esb_stat |= ESB_ST_REC_QUAL;
2285a0cc1eccSVasu Dev 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
2286a0cc1eccSVasu Dev 	spin_unlock_bh(&ep->ex_lock);
228742e9a92fSRobert Love }
228842e9a92fSRobert Love 
22893a3b42bfSRobert Love /**
22903a3b42bfSRobert Love  * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
229192261156SJoe Eykholt  * @fp: The RRQ frame, not freed here.
229242e9a92fSRobert Love  */
fc_exch_els_rrq(struct fc_frame * fp)229392261156SJoe Eykholt static void fc_exch_els_rrq(struct fc_frame *fp)
229442e9a92fSRobert Love {
229592261156SJoe Eykholt 	struct fc_lport *lport;
22963f127ad9SVasu Dev 	struct fc_exch *ep = NULL;	/* request or subject exchange */
229742e9a92fSRobert Love 	struct fc_els_rrq *rp;
229842e9a92fSRobert Love 	u32 sid;
229942e9a92fSRobert Love 	u16 xid;
230042e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
230142e9a92fSRobert Love 
230292261156SJoe Eykholt 	lport = fr_dev(fp);
230342e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
230442e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
230542e9a92fSRobert Love 	if (!rp)
230642e9a92fSRobert Love 		goto reject;
230742e9a92fSRobert Love 
230842e9a92fSRobert Love 	/*
230942e9a92fSRobert Love 	 * lookup subject exchange.
231042e9a92fSRobert Love 	 */
231142e9a92fSRobert Love 	sid = ntoh24(rp->rrq_s_id);		/* subject source */
231292261156SJoe Eykholt 	xid = fc_host_port_id(lport->host) == sid ?
231392261156SJoe Eykholt 			ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
231492261156SJoe Eykholt 	ep = fc_exch_lookup(lport, xid);
231542e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
231642e9a92fSRobert Love 	if (!ep)
231742e9a92fSRobert Love 		goto reject;
231842e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
231957d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "RRQ request from %x: xid %x rxid %x oxid %x\n",
232057d3ec7eSHannes Reinecke 		    sid, xid, ntohs(rp->rrq_rx_id), ntohs(rp->rrq_ox_id));
232142e9a92fSRobert Love 	if (ep->oxid != ntohs(rp->rrq_ox_id))
232242e9a92fSRobert Love 		goto unlock_reject;
232342e9a92fSRobert Love 	if (ep->rxid != ntohs(rp->rrq_rx_id) &&
232442e9a92fSRobert Love 	    ep->rxid != FC_XID_UNKNOWN)
232542e9a92fSRobert Love 		goto unlock_reject;
232642e9a92fSRobert Love 	explan = ELS_EXPL_SID;
232742e9a92fSRobert Love 	if (ep->sid != sid)
232842e9a92fSRobert Love 		goto unlock_reject;
232942e9a92fSRobert Love 
233042e9a92fSRobert Love 	/*
233142e9a92fSRobert Love 	 * Clear Recovery Qualifier state, and cancel timer if complete.
233242e9a92fSRobert Love 	 */
233342e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL) {
233442e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_REC_QUAL;
233542e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec qual */
233642e9a92fSRobert Love 	}
2337b29a4f30SVasu Dev 	if (ep->esb_stat & ESB_ST_COMPLETE)
2338b29a4f30SVasu Dev 		fc_exch_timer_cancel(ep);
233942e9a92fSRobert Love 
234042e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
234142e9a92fSRobert Love 
234242e9a92fSRobert Love 	/*
234342e9a92fSRobert Love 	 * Send LS_ACC.
234442e9a92fSRobert Love 	 */
234592261156SJoe Eykholt 	fc_seq_ls_acc(fp);
23463f127ad9SVasu Dev 	goto out;
234742e9a92fSRobert Love 
234842e9a92fSRobert Love unlock_reject:
234942e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
235042e9a92fSRobert Love reject:
235192261156SJoe Eykholt 	fc_seq_ls_rjt(fp, ELS_RJT_LOGIC, explan);
23523f127ad9SVasu Dev out:
23533f127ad9SVasu Dev 	if (ep)
23543f127ad9SVasu Dev 		fc_exch_release(ep);	/* drop hold from fc_exch_find */
235542e9a92fSRobert Love }
235642e9a92fSRobert Love 
23573a3b42bfSRobert Love /**
23584e5fae7aSVasu Dev  * fc_exch_update_stats() - update exches stats to lport
23594e5fae7aSVasu Dev  * @lport: The local port to update exchange manager stats
23604e5fae7aSVasu Dev  */
fc_exch_update_stats(struct fc_lport * lport)23614e5fae7aSVasu Dev void fc_exch_update_stats(struct fc_lport *lport)
23624e5fae7aSVasu Dev {
23634e5fae7aSVasu Dev 	struct fc_host_statistics *st;
23644e5fae7aSVasu Dev 	struct fc_exch_mgr_anchor *ema;
23654e5fae7aSVasu Dev 	struct fc_exch_mgr *mp;
23664e5fae7aSVasu Dev 
23674e5fae7aSVasu Dev 	st = &lport->host_stats;
23684e5fae7aSVasu Dev 
23694e5fae7aSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
23704e5fae7aSVasu Dev 		mp = ema->mp;
23714e5fae7aSVasu Dev 		st->fc_no_free_exch += atomic_read(&mp->stats.no_free_exch);
23724e5fae7aSVasu Dev 		st->fc_no_free_exch_xid +=
23734e5fae7aSVasu Dev 				atomic_read(&mp->stats.no_free_exch_xid);
23744e5fae7aSVasu Dev 		st->fc_xid_not_found += atomic_read(&mp->stats.xid_not_found);
23754e5fae7aSVasu Dev 		st->fc_xid_busy += atomic_read(&mp->stats.xid_busy);
23764e5fae7aSVasu Dev 		st->fc_seq_not_found += atomic_read(&mp->stats.seq_not_found);
23774e5fae7aSVasu Dev 		st->fc_non_bls_resp += atomic_read(&mp->stats.non_bls_resp);
23784e5fae7aSVasu Dev 	}
23794e5fae7aSVasu Dev }
23804e5fae7aSVasu Dev EXPORT_SYMBOL(fc_exch_update_stats);
23814e5fae7aSVasu Dev 
23824e5fae7aSVasu Dev /**
23833a3b42bfSRobert Love  * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
23843a3b42bfSRobert Love  * @lport: The local port to add the exchange manager to
23853a3b42bfSRobert Love  * @mp:	   The exchange manager to be added to the local port
23863a3b42bfSRobert Love  * @match: The match routine that indicates when this EM should be used
23873a3b42bfSRobert Love  */
fc_exch_mgr_add(struct fc_lport * lport,struct fc_exch_mgr * mp,bool (* match)(struct fc_frame *))238896316099SVasu Dev struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
238996316099SVasu Dev 					   struct fc_exch_mgr *mp,
239096316099SVasu Dev 					   bool (*match)(struct fc_frame *))
239196316099SVasu Dev {
239296316099SVasu Dev 	struct fc_exch_mgr_anchor *ema;
239396316099SVasu Dev 
239496316099SVasu Dev 	ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
239596316099SVasu Dev 	if (!ema)
239696316099SVasu Dev 		return ema;
239796316099SVasu Dev 
239896316099SVasu Dev 	ema->mp = mp;
239996316099SVasu Dev 	ema->match = match;
240096316099SVasu Dev 	/* add EM anchor to EM anchors list */
240196316099SVasu Dev 	list_add_tail(&ema->ema_list, &lport->ema_list);
240296316099SVasu Dev 	kref_get(&mp->kref);
240396316099SVasu Dev 	return ema;
240496316099SVasu Dev }
240596316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_add);
240696316099SVasu Dev 
24073a3b42bfSRobert Love /**
24083a3b42bfSRobert Love  * fc_exch_mgr_destroy() - Destroy an exchange manager
24093a3b42bfSRobert Love  * @kref: The reference to the EM to be destroyed
24103a3b42bfSRobert Love  */
fc_exch_mgr_destroy(struct kref * kref)241196316099SVasu Dev static void fc_exch_mgr_destroy(struct kref *kref)
241296316099SVasu Dev {
241396316099SVasu Dev 	struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
241496316099SVasu Dev 
241596316099SVasu Dev 	mempool_destroy(mp->ep_pool);
2416e4bc50beSVasu Dev 	free_percpu(mp->pool);
241796316099SVasu Dev 	kfree(mp);
241896316099SVasu Dev }
241996316099SVasu Dev 
24203a3b42bfSRobert Love /**
24213a3b42bfSRobert Love  * fc_exch_mgr_del() - Delete an EM from a local port's list
24223a3b42bfSRobert Love  * @ema: The exchange manager anchor identifying the EM to be deleted
24233a3b42bfSRobert Love  */
fc_exch_mgr_del(struct fc_exch_mgr_anchor * ema)242496316099SVasu Dev void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
242596316099SVasu Dev {
242696316099SVasu Dev 	/* remove EM anchor from EM anchors list */
242796316099SVasu Dev 	list_del(&ema->ema_list);
242896316099SVasu Dev 	kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
242996316099SVasu Dev 	kfree(ema);
243096316099SVasu Dev }
243196316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_del);
243296316099SVasu Dev 
2433174e1ebfSChris Leech /**
24343a3b42bfSRobert Love  * fc_exch_mgr_list_clone() - Share all exchange manager objects
24353a3b42bfSRobert Love  * @src: Source lport to clone exchange managers from
24363a3b42bfSRobert Love  * @dst: New lport that takes references to all the exchange managers
2437174e1ebfSChris Leech  */
fc_exch_mgr_list_clone(struct fc_lport * src,struct fc_lport * dst)2438174e1ebfSChris Leech int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
2439174e1ebfSChris Leech {
2440174e1ebfSChris Leech 	struct fc_exch_mgr_anchor *ema, *tmp;
2441174e1ebfSChris Leech 
2442174e1ebfSChris Leech 	list_for_each_entry(ema, &src->ema_list, ema_list) {
2443174e1ebfSChris Leech 		if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
2444174e1ebfSChris Leech 			goto err;
2445174e1ebfSChris Leech 	}
2446174e1ebfSChris Leech 	return 0;
2447174e1ebfSChris Leech err:
2448174e1ebfSChris Leech 	list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
2449174e1ebfSChris Leech 		fc_exch_mgr_del(ema);
2450174e1ebfSChris Leech 	return -ENOMEM;
2451174e1ebfSChris Leech }
245272fa396bSVasu Dev EXPORT_SYMBOL(fc_exch_mgr_list_clone);
2453174e1ebfSChris Leech 
24543a3b42bfSRobert Love /**
24553a3b42bfSRobert Love  * fc_exch_mgr_alloc() - Allocate an exchange manager
24563a3b42bfSRobert Love  * @lport:   The local port that the new EM will be associated with
24573a3b42bfSRobert Love  * @class:   The default FC class for new exchanges
24583a3b42bfSRobert Love  * @min_xid: The minimum XID for exchanges from the new EM
24593a3b42bfSRobert Love  * @max_xid: The maximum XID for exchanges from the new EM
24603a3b42bfSRobert Love  * @match:   The match routine for the new EM
24613a3b42bfSRobert Love  */
fc_exch_mgr_alloc(struct fc_lport * lport,enum fc_class class,u16 min_xid,u16 max_xid,bool (* match)(struct fc_frame *))24623a3b42bfSRobert Love struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
246342e9a92fSRobert Love 				      enum fc_class class,
246452ff878cSVasu Dev 				      u16 min_xid, u16 max_xid,
246552ff878cSVasu Dev 				      bool (*match)(struct fc_frame *))
246642e9a92fSRobert Love {
246742e9a92fSRobert Love 	struct fc_exch_mgr *mp;
2468e4bc50beSVasu Dev 	u16 pool_exch_range;
2469e4bc50beSVasu Dev 	size_t pool_size;
2470e4bc50beSVasu Dev 	unsigned int cpu;
2471e4bc50beSVasu Dev 	struct fc_exch_pool *pool;
247242e9a92fSRobert Love 
2473e4bc50beSVasu Dev 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
2474e4bc50beSVasu Dev 	    (min_xid & fc_cpu_mask) != 0) {
24753a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
247642e9a92fSRobert Love 			     min_xid, max_xid);
247742e9a92fSRobert Love 		return NULL;
247842e9a92fSRobert Love 	}
247942e9a92fSRobert Love 
248042e9a92fSRobert Love 	/*
2481b2f0091fSVasu Dev 	 * allocate memory for EM
248242e9a92fSRobert Love 	 */
2483b2f0091fSVasu Dev 	mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
248442e9a92fSRobert Love 	if (!mp)
248542e9a92fSRobert Love 		return NULL;
248642e9a92fSRobert Love 
248742e9a92fSRobert Love 	mp->class = class;
24889ca1e182SHannes Reinecke 	mp->lport = lport;
248942e9a92fSRobert Love 	/* adjust em exch xid range for offload */
249042e9a92fSRobert Love 	mp->min_xid = min_xid;
2491011a9008SSteven Clark 
2492011a9008SSteven Clark        /* reduce range so per cpu pool fits into PCPU_MIN_UNIT_SIZE pool */
2493011a9008SSteven Clark 	pool_exch_range = (PCPU_MIN_UNIT_SIZE - sizeof(*pool)) /
2494011a9008SSteven Clark 		sizeof(struct fc_exch *);
2495011a9008SSteven Clark 	if ((max_xid - min_xid + 1) / (fc_cpu_mask + 1) > pool_exch_range) {
2496011a9008SSteven Clark 		mp->max_xid = pool_exch_range * (fc_cpu_mask + 1) +
2497011a9008SSteven Clark 			min_xid - 1;
2498011a9008SSteven Clark 	} else {
249942e9a92fSRobert Love 		mp->max_xid = max_xid;
2500011a9008SSteven Clark 		pool_exch_range = (mp->max_xid - mp->min_xid + 1) /
2501011a9008SSteven Clark 			(fc_cpu_mask + 1);
2502011a9008SSteven Clark 	}
250342e9a92fSRobert Love 
250442e9a92fSRobert Love 	mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
250542e9a92fSRobert Love 	if (!mp->ep_pool)
250642e9a92fSRobert Love 		goto free_mp;
250742e9a92fSRobert Love 
2508e4bc50beSVasu Dev 	/*
2509e4bc50beSVasu Dev 	 * Setup per cpu exch pool with entire exchange id range equally
2510e4bc50beSVasu Dev 	 * divided across all cpus. The exch pointers array memory is
2511e4bc50beSVasu Dev 	 * allocated for exch range per pool.
2512e4bc50beSVasu Dev 	 */
2513e4bc50beSVasu Dev 	mp->pool_max_index = pool_exch_range - 1;
2514e4bc50beSVasu Dev 
2515e4bc50beSVasu Dev 	/*
2516e4bc50beSVasu Dev 	 * Allocate and initialize per cpu exch pool
2517e4bc50beSVasu Dev 	 */
2518e4bc50beSVasu Dev 	pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
2519e4bc50beSVasu Dev 	mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
2520e4bc50beSVasu Dev 	if (!mp->pool)
2521e4bc50beSVasu Dev 		goto free_mempool;
2522e4bc50beSVasu Dev 	for_each_possible_cpu(cpu) {
2523e4bc50beSVasu Dev 		pool = per_cpu_ptr(mp->pool, cpu);
2524b6e3c840SVasu Dev 		pool->next_index = 0;
25252034c19cSHillf Danton 		pool->left = FC_XID_UNKNOWN;
25262034c19cSHillf Danton 		pool->right = FC_XID_UNKNOWN;
2527e4bc50beSVasu Dev 		spin_lock_init(&pool->lock);
2528e4bc50beSVasu Dev 		INIT_LIST_HEAD(&pool->ex_list);
2529e4bc50beSVasu Dev 	}
2530e4bc50beSVasu Dev 
253152ff878cSVasu Dev 	kref_init(&mp->kref);
25323a3b42bfSRobert Love 	if (!fc_exch_mgr_add(lport, mp, match)) {
2533e4bc50beSVasu Dev 		free_percpu(mp->pool);
2534e4bc50beSVasu Dev 		goto free_mempool;
253552ff878cSVasu Dev 	}
253652ff878cSVasu Dev 
253752ff878cSVasu Dev 	/*
253852ff878cSVasu Dev 	 * Above kref_init() sets mp->kref to 1 and then
253952ff878cSVasu Dev 	 * call to fc_exch_mgr_add incremented mp->kref again,
254052ff878cSVasu Dev 	 * so adjust that extra increment.
254152ff878cSVasu Dev 	 */
254252ff878cSVasu Dev 	kref_put(&mp->kref, fc_exch_mgr_destroy);
254342e9a92fSRobert Love 	return mp;
254442e9a92fSRobert Love 
2545e4bc50beSVasu Dev free_mempool:
2546e4bc50beSVasu Dev 	mempool_destroy(mp->ep_pool);
254742e9a92fSRobert Love free_mp:
254842e9a92fSRobert Love 	kfree(mp);
254942e9a92fSRobert Love 	return NULL;
255042e9a92fSRobert Love }
255142e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_alloc);
255242e9a92fSRobert Love 
25533a3b42bfSRobert Love /**
25543a3b42bfSRobert Love  * fc_exch_mgr_free() - Free all exchange managers on a local port
25553a3b42bfSRobert Love  * @lport: The local port whose EMs are to be freed
25563a3b42bfSRobert Love  */
fc_exch_mgr_free(struct fc_lport * lport)255752ff878cSVasu Dev void fc_exch_mgr_free(struct fc_lport *lport)
255842e9a92fSRobert Love {
255952ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema, *next;
256052ff878cSVasu Dev 
25614ae1e19fSVasu Dev 	flush_workqueue(fc_exch_workqueue);
256252ff878cSVasu Dev 	list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
256352ff878cSVasu Dev 		fc_exch_mgr_del(ema);
256442e9a92fSRobert Love }
256542e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_free);
256642e9a92fSRobert Love 
25673a3b42bfSRobert Love /**
25686c8cc1c0SKiran Patil  * fc_find_ema() - Lookup and return appropriate Exchange Manager Anchor depending
25696c8cc1c0SKiran Patil  * upon 'xid'.
25706c8cc1c0SKiran Patil  * @f_ctl: f_ctl
25716c8cc1c0SKiran Patil  * @lport: The local port the frame was received on
25726c8cc1c0SKiran Patil  * @fh: The received frame header
25736c8cc1c0SKiran Patil  */
fc_find_ema(u32 f_ctl,struct fc_lport * lport,struct fc_frame_header * fh)25746c8cc1c0SKiran Patil static struct fc_exch_mgr_anchor *fc_find_ema(u32 f_ctl,
25756c8cc1c0SKiran Patil 					      struct fc_lport *lport,
25766c8cc1c0SKiran Patil 					      struct fc_frame_header *fh)
25776c8cc1c0SKiran Patil {
25786c8cc1c0SKiran Patil 	struct fc_exch_mgr_anchor *ema;
25796c8cc1c0SKiran Patil 	u16 xid;
25806c8cc1c0SKiran Patil 
25816c8cc1c0SKiran Patil 	if (f_ctl & FC_FC_EX_CTX)
25826c8cc1c0SKiran Patil 		xid = ntohs(fh->fh_ox_id);
25836c8cc1c0SKiran Patil 	else {
25846c8cc1c0SKiran Patil 		xid = ntohs(fh->fh_rx_id);
25856c8cc1c0SKiran Patil 		if (xid == FC_XID_UNKNOWN)
25866c8cc1c0SKiran Patil 			return list_entry(lport->ema_list.prev,
25876c8cc1c0SKiran Patil 					  typeof(*ema), ema_list);
25886c8cc1c0SKiran Patil 	}
25896c8cc1c0SKiran Patil 
25906c8cc1c0SKiran Patil 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
25916c8cc1c0SKiran Patil 		if ((xid >= ema->mp->min_xid) &&
25926c8cc1c0SKiran Patil 		    (xid <= ema->mp->max_xid))
25936c8cc1c0SKiran Patil 			return ema;
25946c8cc1c0SKiran Patil 	}
25956c8cc1c0SKiran Patil 	return NULL;
25966c8cc1c0SKiran Patil }
25976c8cc1c0SKiran Patil /**
25983a3b42bfSRobert Love  * fc_exch_recv() - Handler for received frames
25993a3b42bfSRobert Love  * @lport: The local port the frame was received on
26003a3b42bfSRobert Love  * @fp:	The received frame
260142e9a92fSRobert Love  */
fc_exch_recv(struct fc_lport * lport,struct fc_frame * fp)26023a3b42bfSRobert Love void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
260342e9a92fSRobert Love {
260442e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
260552ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
26066c8cc1c0SKiran Patil 	u32 f_ctl;
260742e9a92fSRobert Love 
260842e9a92fSRobert Love 	/* lport lock ? */
26093a3b42bfSRobert Love 	if (!lport || lport->state == LPORT_ST_DISABLED) {
261041a6bf65SColin Ian King 		FC_LIBFC_DBG("Receiving frames for an lport that "
26117414705eSRobert Love 			     "has not been initialized correctly\n");
261242e9a92fSRobert Love 		fc_frame_free(fp);
261342e9a92fSRobert Love 		return;
261442e9a92fSRobert Love 	}
261542e9a92fSRobert Love 
261652ff878cSVasu Dev 	f_ctl = ntoh24(fh->fh_f_ctl);
26176c8cc1c0SKiran Patil 	ema = fc_find_ema(f_ctl, lport, fh);
26186c8cc1c0SKiran Patil 	if (!ema) {
26196c8cc1c0SKiran Patil 		FC_LPORT_DBG(lport, "Unable to find Exchange Manager Anchor,"
26206c8cc1c0SKiran Patil 				    "fc_ctl <0x%x>, xid <0x%x>\n",
26216c8cc1c0SKiran Patil 				     f_ctl,
26226c8cc1c0SKiran Patil 				     (f_ctl & FC_FC_EX_CTX) ?
26236c8cc1c0SKiran Patil 				     ntohs(fh->fh_ox_id) :
26246c8cc1c0SKiran Patil 				     ntohs(fh->fh_rx_id));
262552ff878cSVasu Dev 		fc_frame_free(fp);
262652ff878cSVasu Dev 		return;
262752ff878cSVasu Dev 	}
262852ff878cSVasu Dev 
262942e9a92fSRobert Love 	/*
263042e9a92fSRobert Love 	 * If frame is marked invalid, just drop it.
263142e9a92fSRobert Love 	 */
263242e9a92fSRobert Love 	switch (fr_eof(fp)) {
263342e9a92fSRobert Love 	case FC_EOF_T:
263442e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
263542e9a92fSRobert Love 			skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
2636df561f66SGustavo A. R. Silva 		fallthrough;
263742e9a92fSRobert Love 	case FC_EOF_N:
263842e9a92fSRobert Love 		if (fh->fh_type == FC_TYPE_BLS)
263952ff878cSVasu Dev 			fc_exch_recv_bls(ema->mp, fp);
264042e9a92fSRobert Love 		else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
264142e9a92fSRobert Love 			 FC_FC_EX_CTX)
264252ff878cSVasu Dev 			fc_exch_recv_seq_resp(ema->mp, fp);
264342e9a92fSRobert Love 		else if (f_ctl & FC_FC_SEQ_CTX)
264452ff878cSVasu Dev 			fc_exch_recv_resp(ema->mp, fp);
264592261156SJoe Eykholt 		else	/* no EX_CTX and no SEQ_CTX */
26463a3b42bfSRobert Love 			fc_exch_recv_req(lport, ema->mp, fp);
264742e9a92fSRobert Love 		break;
264842e9a92fSRobert Love 	default:
26493a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
26503a3b42bfSRobert Love 			     fr_eof(fp));
265142e9a92fSRobert Love 		fc_frame_free(fp);
265242e9a92fSRobert Love 	}
265342e9a92fSRobert Love }
265442e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_recv);
265542e9a92fSRobert Love 
26563a3b42bfSRobert Love /**
26573a3b42bfSRobert Love  * fc_exch_init() - Initialize the exchange layer for a local port
26583a3b42bfSRobert Love  * @lport: The local port to initialize the exchange layer for
26593a3b42bfSRobert Love  */
fc_exch_init(struct fc_lport * lport)26603a3b42bfSRobert Love int fc_exch_init(struct fc_lport *lport)
266142e9a92fSRobert Love {
26623a3b42bfSRobert Love 	if (!lport->tt.exch_mgr_reset)
26633a3b42bfSRobert Love 		lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
266442e9a92fSRobert Love 
266589f19a59SVasu Dev 	return 0;
266689f19a59SVasu Dev }
266789f19a59SVasu Dev EXPORT_SYMBOL(fc_exch_init);
266889f19a59SVasu Dev 
266989f19a59SVasu Dev /**
267089f19a59SVasu Dev  * fc_setup_exch_mgr() - Setup an exchange manager
267189f19a59SVasu Dev  */
fc_setup_exch_mgr(void)267255204909SRandy Dunlap int fc_setup_exch_mgr(void)
267389f19a59SVasu Dev {
267489f19a59SVasu Dev 	fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
267589f19a59SVasu Dev 					 0, SLAB_HWCACHE_ALIGN, NULL);
267689f19a59SVasu Dev 	if (!fc_em_cachep)
267789f19a59SVasu Dev 		return -ENOMEM;
267889f19a59SVasu Dev 
2679e4bc50beSVasu Dev 	/*
2680e4bc50beSVasu Dev 	 * Initialize fc_cpu_mask and fc_cpu_order. The
2681e4bc50beSVasu Dev 	 * fc_cpu_mask is set for nr_cpu_ids rounded up
2682e4bc50beSVasu Dev 	 * to order of 2's * power and order is stored
2683e4bc50beSVasu Dev 	 * in fc_cpu_order as this is later required in
2684e4bc50beSVasu Dev 	 * mapping between an exch id and exch array index
2685e4bc50beSVasu Dev 	 * in per cpu exch pool.
2686e4bc50beSVasu Dev 	 *
2687e4bc50beSVasu Dev 	 * This round up is required to align fc_cpu_mask
2688e4bc50beSVasu Dev 	 * to exchange id's lower bits such that all incoming
2689e4bc50beSVasu Dev 	 * frames of an exchange gets delivered to the same
2690e4bc50beSVasu Dev 	 * cpu on which exchange originated by simple bitwise
2691e4bc50beSVasu Dev 	 * AND operation between fc_cpu_mask and exchange id.
2692e4bc50beSVasu Dev 	 */
2693a84ea8c7SBart Van Assche 	fc_cpu_order = ilog2(roundup_pow_of_two(nr_cpu_ids));
2694a84ea8c7SBart Van Assche 	fc_cpu_mask = (1 << fc_cpu_order) - 1;
2695e4bc50beSVasu Dev 
2696*b97c0741SBart Van Assche 	fc_exch_workqueue = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
2697*b97c0741SBart Van Assche 						    "fc_exch_workqueue");
26984ae1e19fSVasu Dev 	if (!fc_exch_workqueue)
26996f06e3a7SHillf Danton 		goto err;
270042e9a92fSRobert Love 	return 0;
27016f06e3a7SHillf Danton err:
27026f06e3a7SHillf Danton 	kmem_cache_destroy(fc_em_cachep);
27036f06e3a7SHillf Danton 	return -ENOMEM;
270442e9a92fSRobert Love }
270542e9a92fSRobert Love 
27063a3b42bfSRobert Love /**
27073a3b42bfSRobert Love  * fc_destroy_exch_mgr() - Destroy an exchange manager
27083a3b42bfSRobert Love  */
fc_destroy_exch_mgr(void)270955204909SRandy Dunlap void fc_destroy_exch_mgr(void)
271042e9a92fSRobert Love {
27114ae1e19fSVasu Dev 	destroy_workqueue(fc_exch_workqueue);
271242e9a92fSRobert Love 	kmem_cache_destroy(fc_em_cachep);
271342e9a92fSRobert Love }
2714