xref: /linux/drivers/scsi/libfc/fc_exch.c (revision f7ce413ceac01d502ea4a27c0ba542b57b728e5c)
142e9a92fSRobert Love /*
242e9a92fSRobert Love  * Copyright(c) 2007 Intel Corporation. All rights reserved.
342e9a92fSRobert Love  * Copyright(c) 2008 Red Hat, Inc.  All rights reserved.
442e9a92fSRobert Love  * Copyright(c) 2008 Mike Christie
542e9a92fSRobert Love  *
642e9a92fSRobert Love  * This program is free software; you can redistribute it and/or modify it
742e9a92fSRobert Love  * under the terms and conditions of the GNU General Public License,
842e9a92fSRobert Love  * version 2, as published by the Free Software Foundation.
942e9a92fSRobert Love  *
1042e9a92fSRobert Love  * This program is distributed in the hope it will be useful, but WITHOUT
1142e9a92fSRobert Love  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1242e9a92fSRobert Love  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1342e9a92fSRobert Love  * more details.
1442e9a92fSRobert Love  *
1542e9a92fSRobert Love  * You should have received a copy of the GNU General Public License along with
1642e9a92fSRobert Love  * this program; if not, write to the Free Software Foundation, Inc.,
1742e9a92fSRobert Love  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
1842e9a92fSRobert Love  *
1942e9a92fSRobert Love  * Maintained at www.Open-FCoE.org
2042e9a92fSRobert Love  */
2142e9a92fSRobert Love 
2242e9a92fSRobert Love /*
2342e9a92fSRobert Love  * Fibre Channel exchange and sequence handling.
2442e9a92fSRobert Love  */
2542e9a92fSRobert Love 
2642e9a92fSRobert Love #include <linux/timer.h>
275a0e3ad6STejun Heo #include <linux/slab.h>
2842e9a92fSRobert Love #include <linux/err.h>
2909703660SPaul Gortmaker #include <linux/export.h>
30a84ea8c7SBart Van Assche #include <linux/log2.h>
3142e9a92fSRobert Love 
3242e9a92fSRobert Love #include <scsi/fc/fc_fc2.h>
3342e9a92fSRobert Love 
3442e9a92fSRobert Love #include <scsi/libfc.h>
3542e9a92fSRobert Love #include <scsi/fc_encode.h>
3642e9a92fSRobert Love 
378866a5d9SRobert Love #include "fc_libfc.h"
388866a5d9SRobert Love 
39e4bc50beSVasu Dev u16	fc_cpu_mask;		/* cpu mask for possible cpus */
40e4bc50beSVasu Dev EXPORT_SYMBOL(fc_cpu_mask);
41e4bc50beSVasu Dev static u16	fc_cpu_order;	/* 2's power to represent total possible cpus */
4242e9a92fSRobert Love static struct kmem_cache *fc_em_cachep;	       /* cache for exchanges */
4355204909SRandy Dunlap static struct workqueue_struct *fc_exch_workqueue;
4442e9a92fSRobert Love 
4542e9a92fSRobert Love /*
4642e9a92fSRobert Love  * Structure and function definitions for managing Fibre Channel Exchanges
4742e9a92fSRobert Love  * and Sequences.
4842e9a92fSRobert Love  *
4942e9a92fSRobert Love  * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
5042e9a92fSRobert Love  *
5142e9a92fSRobert Love  * fc_exch_mgr holds the exchange state for an N port
5242e9a92fSRobert Love  *
5342e9a92fSRobert Love  * fc_exch holds state for one exchange and links to its active sequence.
5442e9a92fSRobert Love  *
5542e9a92fSRobert Love  * fc_seq holds the state for an individual sequence.
5642e9a92fSRobert Love  */
5742e9a92fSRobert Love 
583a3b42bfSRobert Love /**
593a3b42bfSRobert Love  * struct fc_exch_pool - Per cpu exchange pool
603a3b42bfSRobert Love  * @next_index:	  Next possible free exchange index
613a3b42bfSRobert Love  * @total_exches: Total allocated exchanges
623a3b42bfSRobert Love  * @lock:	  Exch pool lock
633a3b42bfSRobert Love  * @ex_list:	  List of exchanges
64e4bc50beSVasu Dev  *
65e4bc50beSVasu Dev  * This structure manages per cpu exchanges in array of exchange pointers.
66e4bc50beSVasu Dev  * This array is allocated followed by struct fc_exch_pool memory for
67e4bc50beSVasu Dev  * assigned range of exchanges to per cpu pool.
68e4bc50beSVasu Dev  */
69e4bc50beSVasu Dev struct fc_exch_pool {
70e17b4af7SVasu Dev 	spinlock_t	 lock;
71e17b4af7SVasu Dev 	struct list_head ex_list;
723a3b42bfSRobert Love 	u16		 next_index;
733a3b42bfSRobert Love 	u16		 total_exches;
742034c19cSHillf Danton 
752034c19cSHillf Danton 	/* two cache of free slot in exch array */
762034c19cSHillf Danton 	u16		 left;
772034c19cSHillf Danton 	u16		 right;
78e17b4af7SVasu Dev } ____cacheline_aligned_in_smp;
79e4bc50beSVasu Dev 
803a3b42bfSRobert Love /**
813a3b42bfSRobert Love  * struct fc_exch_mgr - The Exchange Manager (EM).
823a3b42bfSRobert Love  * @class:	    Default class for new sequences
833a3b42bfSRobert Love  * @kref:	    Reference counter
843a3b42bfSRobert Love  * @min_xid:	    Minimum exchange ID
853a3b42bfSRobert Love  * @max_xid:	    Maximum exchange ID
863a3b42bfSRobert Love  * @ep_pool:	    Reserved exchange pointers
873a3b42bfSRobert Love  * @pool_max_index: Max exch array index in exch pool
883a3b42bfSRobert Love  * @pool:	    Per cpu exch pool
893a3b42bfSRobert Love  * @stats:	    Statistics structure
9042e9a92fSRobert Love  *
9142e9a92fSRobert Love  * This structure is the center for creating exchanges and sequences.
9242e9a92fSRobert Love  * It manages the allocation of exchange IDs.
9342e9a92fSRobert Love  */
9442e9a92fSRobert Love struct fc_exch_mgr {
95c6b21c93SBart Van Assche 	struct fc_exch_pool __percpu *pool;
96e17b4af7SVasu Dev 	mempool_t	*ep_pool;
973a3b42bfSRobert Love 	enum fc_class	class;
983a3b42bfSRobert Love 	struct kref	kref;
993a3b42bfSRobert Love 	u16		min_xid;
1003a3b42bfSRobert Love 	u16		max_xid;
1013a3b42bfSRobert Love 	u16		pool_max_index;
10242e9a92fSRobert Love 
10342e9a92fSRobert Love 	struct {
10442e9a92fSRobert Love 		atomic_t no_free_exch;
10542e9a92fSRobert Love 		atomic_t no_free_exch_xid;
10642e9a92fSRobert Love 		atomic_t xid_not_found;
10742e9a92fSRobert Love 		atomic_t xid_busy;
10842e9a92fSRobert Love 		atomic_t seq_not_found;
10942e9a92fSRobert Love 		atomic_t non_bls_resp;
11042e9a92fSRobert Love 	} stats;
11142e9a92fSRobert Love };
11242e9a92fSRobert Love 
1133a3b42bfSRobert Love /**
1143a3b42bfSRobert Love  * struct fc_exch_mgr_anchor - primary structure for list of EMs
1153a3b42bfSRobert Love  * @ema_list: Exchange Manager Anchor list
1163a3b42bfSRobert Love  * @mp:	      Exchange Manager associated with this anchor
1173a3b42bfSRobert Love  * @match:    Routine to determine if this anchor's EM should be used
1183a3b42bfSRobert Love  *
1193a3b42bfSRobert Love  * When walking the list of anchors the match routine will be called
1203a3b42bfSRobert Love  * for each anchor to determine if that EM should be used. The last
1213a3b42bfSRobert Love  * anchor in the list will always match to handle any exchanges not
1223a3b42bfSRobert Love  * handled by other EMs. The non-default EMs would be added to the
1231bd49b48SVasu Dev  * anchor list by HW that provides offloads.
1243a3b42bfSRobert Love  */
12596316099SVasu Dev struct fc_exch_mgr_anchor {
12696316099SVasu Dev 	struct list_head ema_list;
12796316099SVasu Dev 	struct fc_exch_mgr *mp;
12896316099SVasu Dev 	bool (*match)(struct fc_frame *);
12996316099SVasu Dev };
13096316099SVasu Dev 
13142e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *);
13292261156SJoe Eykholt static void fc_seq_ls_acc(struct fc_frame *);
13392261156SJoe Eykholt static void fc_seq_ls_rjt(struct fc_frame *, enum fc_els_rjt_reason,
13442e9a92fSRobert Love 			  enum fc_els_rjt_explan);
13592261156SJoe Eykholt static void fc_exch_els_rec(struct fc_frame *);
13692261156SJoe Eykholt static void fc_exch_els_rrq(struct fc_frame *);
13742e9a92fSRobert Love 
13842e9a92fSRobert Love /*
13942e9a92fSRobert Love  * Internal implementation notes.
14042e9a92fSRobert Love  *
14142e9a92fSRobert Love  * The exchange manager is one by default in libfc but LLD may choose
14242e9a92fSRobert Love  * to have one per CPU. The sequence manager is one per exchange manager
14342e9a92fSRobert Love  * and currently never separated.
14442e9a92fSRobert Love  *
14542e9a92fSRobert Love  * Section 9.8 in FC-FS-2 specifies:  "The SEQ_ID is a one-byte field
14642e9a92fSRobert Love  * assigned by the Sequence Initiator that shall be unique for a specific
14742e9a92fSRobert Love  * D_ID and S_ID pair while the Sequence is open."   Note that it isn't
14842e9a92fSRobert Love  * qualified by exchange ID, which one might think it would be.
14942e9a92fSRobert Love  * In practice this limits the number of open sequences and exchanges to 256
15042e9a92fSRobert Love  * per session.	 For most targets we could treat this limit as per exchange.
15142e9a92fSRobert Love  *
15242e9a92fSRobert Love  * The exchange and its sequence are freed when the last sequence is received.
15342e9a92fSRobert Love  * It's possible for the remote port to leave an exchange open without
15442e9a92fSRobert Love  * sending any sequences.
15542e9a92fSRobert Love  *
15642e9a92fSRobert Love  * Notes on reference counts:
15742e9a92fSRobert Love  *
15842e9a92fSRobert Love  * Exchanges are reference counted and exchange gets freed when the reference
15942e9a92fSRobert Love  * count becomes zero.
16042e9a92fSRobert Love  *
16142e9a92fSRobert Love  * Timeouts:
16242e9a92fSRobert Love  * Sequences are timed out for E_D_TOV and R_A_TOV.
16342e9a92fSRobert Love  *
16442e9a92fSRobert Love  * Sequence event handling:
16542e9a92fSRobert Love  *
16642e9a92fSRobert Love  * The following events may occur on initiator sequences:
16742e9a92fSRobert Love  *
16842e9a92fSRobert Love  *	Send.
16942e9a92fSRobert Love  *	    For now, the whole thing is sent.
17042e9a92fSRobert Love  *	Receive ACK
17142e9a92fSRobert Love  *	    This applies only to class F.
17242e9a92fSRobert Love  *	    The sequence is marked complete.
17342e9a92fSRobert Love  *	ULP completion.
17442e9a92fSRobert Love  *	    The upper layer calls fc_exch_done() when done
17542e9a92fSRobert Love  *	    with exchange and sequence tuple.
17642e9a92fSRobert Love  *	RX-inferred completion.
17742e9a92fSRobert Love  *	    When we receive the next sequence on the same exchange, we can
17842e9a92fSRobert Love  *	    retire the previous sequence ID.  (XXX not implemented).
17942e9a92fSRobert Love  *	Timeout.
18042e9a92fSRobert Love  *	    R_A_TOV frees the sequence ID.  If we're waiting for ACK,
18142e9a92fSRobert Love  *	    E_D_TOV causes abort and calls upper layer response handler
18242e9a92fSRobert Love  *	    with FC_EX_TIMEOUT error.
18342e9a92fSRobert Love  *	Receive RJT
18442e9a92fSRobert Love  *	    XXX defer.
18542e9a92fSRobert Love  *	Send ABTS
18642e9a92fSRobert Love  *	    On timeout.
18742e9a92fSRobert Love  *
18842e9a92fSRobert Love  * The following events may occur on recipient sequences:
18942e9a92fSRobert Love  *
19042e9a92fSRobert Love  *	Receive
19142e9a92fSRobert Love  *	    Allocate sequence for first frame received.
19242e9a92fSRobert Love  *	    Hold during receive handler.
19342e9a92fSRobert Love  *	    Release when final frame received.
19442e9a92fSRobert Love  *	    Keep status of last N of these for the ELS RES command.  XXX TBD.
19542e9a92fSRobert Love  *	Receive ABTS
19642e9a92fSRobert Love  *	    Deallocate sequence
19742e9a92fSRobert Love  *	Send RJT
19842e9a92fSRobert Love  *	    Deallocate
19942e9a92fSRobert Love  *
20042e9a92fSRobert Love  * For now, we neglect conditions where only part of a sequence was
20142e9a92fSRobert Love  * received or transmitted, or where out-of-order receipt is detected.
20242e9a92fSRobert Love  */
20342e9a92fSRobert Love 
20442e9a92fSRobert Love /*
20542e9a92fSRobert Love  * Locking notes:
20642e9a92fSRobert Love  *
20742e9a92fSRobert Love  * The EM code run in a per-CPU worker thread.
20842e9a92fSRobert Love  *
20942e9a92fSRobert Love  * To protect against concurrency between a worker thread code and timers,
21042e9a92fSRobert Love  * sequence allocation and deallocation must be locked.
21142e9a92fSRobert Love  *  - exchange refcnt can be done atomicly without locks.
21242e9a92fSRobert Love  *  - sequence allocation must be locked by exch lock.
213b2f0091fSVasu Dev  *  - If the EM pool lock and ex_lock must be taken at the same time, then the
214b2f0091fSVasu Dev  *    EM pool lock must be taken before the ex_lock.
21542e9a92fSRobert Love  */
21642e9a92fSRobert Love 
21742e9a92fSRobert Love /*
21842e9a92fSRobert Love  * opcode names for debugging.
21942e9a92fSRobert Love  */
22042e9a92fSRobert Love static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
22142e9a92fSRobert Love 
2223a3b42bfSRobert Love /**
2233a3b42bfSRobert Love  * fc_exch_name_lookup() - Lookup name by opcode
2243a3b42bfSRobert Love  * @op:	       Opcode to be looked up
2253a3b42bfSRobert Love  * @table:     Opcode/name table
2263a3b42bfSRobert Love  * @max_index: Index not to be exceeded
2273a3b42bfSRobert Love  *
2283a3b42bfSRobert Love  * This routine is used to determine a human-readable string identifying
2293a3b42bfSRobert Love  * a R_CTL opcode.
2303a3b42bfSRobert Love  */
23142e9a92fSRobert Love static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
23242e9a92fSRobert Love 					      unsigned int max_index)
23342e9a92fSRobert Love {
23442e9a92fSRobert Love 	const char *name = NULL;
23542e9a92fSRobert Love 
23642e9a92fSRobert Love 	if (op < max_index)
23742e9a92fSRobert Love 		name = table[op];
23842e9a92fSRobert Love 	if (!name)
23942e9a92fSRobert Love 		name = "unknown";
24042e9a92fSRobert Love 	return name;
24142e9a92fSRobert Love }
24242e9a92fSRobert Love 
2433a3b42bfSRobert Love /**
2443a3b42bfSRobert Love  * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
2453a3b42bfSRobert Love  * @op: The opcode to be looked up
2463a3b42bfSRobert Love  */
24742e9a92fSRobert Love static const char *fc_exch_rctl_name(unsigned int op)
24842e9a92fSRobert Love {
24942e9a92fSRobert Love 	return fc_exch_name_lookup(op, fc_exch_rctl_names,
2507156fffaSKulikov Vasiliy 				   ARRAY_SIZE(fc_exch_rctl_names));
25142e9a92fSRobert Love }
25242e9a92fSRobert Love 
2533a3b42bfSRobert Love /**
2543a3b42bfSRobert Love  * fc_exch_hold() - Increment an exchange's reference count
2553a3b42bfSRobert Love  * @ep: Echange to be held
25642e9a92fSRobert Love  */
2573a3b42bfSRobert Love static inline void fc_exch_hold(struct fc_exch *ep)
25842e9a92fSRobert Love {
25942e9a92fSRobert Love 	atomic_inc(&ep->ex_refcnt);
26042e9a92fSRobert Love }
26142e9a92fSRobert Love 
2623a3b42bfSRobert Love /**
2633a3b42bfSRobert Love  * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
2643a3b42bfSRobert Love  *			 and determine SOF and EOF.
2653a3b42bfSRobert Love  * @ep:	   The exchange to that will use the header
2663a3b42bfSRobert Love  * @fp:	   The frame whose header is to be modified
2673a3b42bfSRobert Love  * @f_ctl: F_CTL bits that will be used for the frame header
2683a3b42bfSRobert Love  *
2693a3b42bfSRobert Love  * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
2703a3b42bfSRobert Love  * fh_seq_id, fh_seq_cnt and the SOF and EOF.
27142e9a92fSRobert Love  */
27242e9a92fSRobert Love static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
27342e9a92fSRobert Love 			      u32 f_ctl)
27442e9a92fSRobert Love {
27542e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
27642e9a92fSRobert Love 	u16 fill;
27742e9a92fSRobert Love 
27842e9a92fSRobert Love 	fr_sof(fp) = ep->class;
27942e9a92fSRobert Love 	if (ep->seq.cnt)
28042e9a92fSRobert Love 		fr_sof(fp) = fc_sof_normal(ep->class);
28142e9a92fSRobert Love 
28242e9a92fSRobert Love 	if (f_ctl & FC_FC_END_SEQ) {
28342e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_T;
28442e9a92fSRobert Love 		if (fc_sof_needs_ack(ep->class))
28542e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
28642e9a92fSRobert Love 		/*
2873a3b42bfSRobert Love 		 * From F_CTL.
28842e9a92fSRobert Love 		 * The number of fill bytes to make the length a 4-byte
28942e9a92fSRobert Love 		 * multiple is the low order 2-bits of the f_ctl.
29042e9a92fSRobert Love 		 * The fill itself will have been cleared by the frame
29142e9a92fSRobert Love 		 * allocation.
29242e9a92fSRobert Love 		 * After this, the length will be even, as expected by
29342e9a92fSRobert Love 		 * the transport.
29442e9a92fSRobert Love 		 */
29542e9a92fSRobert Love 		fill = fr_len(fp) & 3;
29642e9a92fSRobert Love 		if (fill) {
29742e9a92fSRobert Love 			fill = 4 - fill;
29842e9a92fSRobert Love 			/* TODO, this may be a problem with fragmented skb */
29942e9a92fSRobert Love 			skb_put(fp_skb(fp), fill);
30042e9a92fSRobert Love 			hton24(fh->fh_f_ctl, f_ctl | fill);
30142e9a92fSRobert Love 		}
30242e9a92fSRobert Love 	} else {
30342e9a92fSRobert Love 		WARN_ON(fr_len(fp) % 4 != 0);	/* no pad to non last frame */
30442e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
30542e9a92fSRobert Love 	}
30642e9a92fSRobert Love 
307c1d45424SBart Van Assche 	/* Initialize remaining fh fields from fc_fill_fc_hdr */
30842e9a92fSRobert Love 	fh->fh_ox_id = htons(ep->oxid);
30942e9a92fSRobert Love 	fh->fh_rx_id = htons(ep->rxid);
31042e9a92fSRobert Love 	fh->fh_seq_id = ep->seq.id;
31142e9a92fSRobert Love 	fh->fh_seq_cnt = htons(ep->seq.cnt);
31242e9a92fSRobert Love }
31342e9a92fSRobert Love 
3143a3b42bfSRobert Love /**
3153a3b42bfSRobert Love  * fc_exch_release() - Decrement an exchange's reference count
3163a3b42bfSRobert Love  * @ep: Exchange to be released
3173a3b42bfSRobert Love  *
3183a3b42bfSRobert Love  * If the reference count reaches zero and the exchange is complete,
3193a3b42bfSRobert Love  * it is freed.
32042e9a92fSRobert Love  */
32142e9a92fSRobert Love static void fc_exch_release(struct fc_exch *ep)
32242e9a92fSRobert Love {
32342e9a92fSRobert Love 	struct fc_exch_mgr *mp;
32442e9a92fSRobert Love 
32542e9a92fSRobert Love 	if (atomic_dec_and_test(&ep->ex_refcnt)) {
32642e9a92fSRobert Love 		mp = ep->em;
32742e9a92fSRobert Love 		if (ep->destructor)
32842e9a92fSRobert Love 			ep->destructor(&ep->seq, ep->arg);
329aa6cd29bSJulia Lawall 		WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
33042e9a92fSRobert Love 		mempool_free(ep, mp->ep_pool);
33142e9a92fSRobert Love 	}
33242e9a92fSRobert Love }
33342e9a92fSRobert Love 
3343a3b42bfSRobert Love /**
335b29a4f30SVasu Dev  * fc_exch_timer_cancel() - cancel exch timer
336b29a4f30SVasu Dev  * @ep:		The exchange whose timer to be canceled
337b29a4f30SVasu Dev  */
338b29a4f30SVasu Dev static inline void fc_exch_timer_cancel(struct fc_exch *ep)
339b29a4f30SVasu Dev {
340b29a4f30SVasu Dev 	if (cancel_delayed_work(&ep->timeout_work)) {
341b29a4f30SVasu Dev 		FC_EXCH_DBG(ep, "Exchange timer canceled\n");
342b29a4f30SVasu Dev 		atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
343b29a4f30SVasu Dev 	}
344b29a4f30SVasu Dev }
345b29a4f30SVasu Dev 
346b29a4f30SVasu Dev /**
347b29a4f30SVasu Dev  * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
348b29a4f30SVasu Dev  *				the exchange lock held
349b29a4f30SVasu Dev  * @ep:		The exchange whose timer will start
350b29a4f30SVasu Dev  * @timer_msec: The timeout period
351b29a4f30SVasu Dev  *
352b29a4f30SVasu Dev  * Used for upper level protocols to time out the exchange.
353b29a4f30SVasu Dev  * The timer is cancelled when it fires or when the exchange completes.
354b29a4f30SVasu Dev  */
355b29a4f30SVasu Dev static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
356b29a4f30SVasu Dev 					    unsigned int timer_msec)
357b29a4f30SVasu Dev {
358b29a4f30SVasu Dev 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
359b29a4f30SVasu Dev 		return;
360b29a4f30SVasu Dev 
361b29a4f30SVasu Dev 	FC_EXCH_DBG(ep, "Exchange timer armed : %d msecs\n", timer_msec);
362b29a4f30SVasu Dev 
363b29a4f30SVasu Dev 	fc_exch_hold(ep);		/* hold for timer */
364b8678865SBart Van Assche 	if (!queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
36557d3ec7eSHannes Reinecke 				msecs_to_jiffies(timer_msec))) {
36657d3ec7eSHannes Reinecke 		FC_EXCH_DBG(ep, "Exchange already queued\n");
367b8678865SBart Van Assche 		fc_exch_release(ep);
368b29a4f30SVasu Dev 	}
36957d3ec7eSHannes Reinecke }
370b29a4f30SVasu Dev 
371b29a4f30SVasu Dev /**
372b29a4f30SVasu Dev  * fc_exch_timer_set() - Lock the exchange and set the timer
373b29a4f30SVasu Dev  * @ep:		The exchange whose timer will start
374b29a4f30SVasu Dev  * @timer_msec: The timeout period
375b29a4f30SVasu Dev  */
376b29a4f30SVasu Dev static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
377b29a4f30SVasu Dev {
378b29a4f30SVasu Dev 	spin_lock_bh(&ep->ex_lock);
379b29a4f30SVasu Dev 	fc_exch_timer_set_locked(ep, timer_msec);
380b29a4f30SVasu Dev 	spin_unlock_bh(&ep->ex_lock);
381b29a4f30SVasu Dev }
382b29a4f30SVasu Dev 
383b29a4f30SVasu Dev /**
3843a3b42bfSRobert Love  * fc_exch_done_locked() - Complete an exchange with the exchange lock held
3853a3b42bfSRobert Love  * @ep: The exchange that is complete
3867030fd62SBart Van Assche  *
3877030fd62SBart Van Assche  * Note: May sleep if invoked from outside a response handler.
3883a3b42bfSRobert Love  */
38942e9a92fSRobert Love static int fc_exch_done_locked(struct fc_exch *ep)
39042e9a92fSRobert Love {
39142e9a92fSRobert Love 	int rc = 1;
39242e9a92fSRobert Love 
39342e9a92fSRobert Love 	/*
39442e9a92fSRobert Love 	 * We must check for completion in case there are two threads
39542e9a92fSRobert Love 	 * tyring to complete this. But the rrq code will reuse the
39642e9a92fSRobert Love 	 * ep, and in that case we only clear the resp and set it as
39742e9a92fSRobert Love 	 * complete, so it can be reused by the timer to send the rrq.
39842e9a92fSRobert Love 	 */
39942e9a92fSRobert Love 	if (ep->state & FC_EX_DONE)
40042e9a92fSRobert Love 		return rc;
40142e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_COMPLETE;
40242e9a92fSRobert Love 
40342e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
40442e9a92fSRobert Love 		ep->state |= FC_EX_DONE;
405b29a4f30SVasu Dev 		fc_exch_timer_cancel(ep);
40642e9a92fSRobert Love 		rc = 0;
40742e9a92fSRobert Love 	}
40842e9a92fSRobert Love 	return rc;
40942e9a92fSRobert Love }
41042e9a92fSRobert Love 
4113a3b42bfSRobert Love /**
4123a3b42bfSRobert Love  * fc_exch_ptr_get() - Return an exchange from an exchange pool
4133a3b42bfSRobert Love  * @pool:  Exchange Pool to get an exchange from
4143a3b42bfSRobert Love  * @index: Index of the exchange within the pool
4153a3b42bfSRobert Love  *
4163a3b42bfSRobert Love  * Use the index to get an exchange from within an exchange pool. exches
4173a3b42bfSRobert Love  * will point to an array of exchange pointers. The index will select
4183a3b42bfSRobert Love  * the exchange within the array.
4193a3b42bfSRobert Love  */
420e4bc50beSVasu Dev static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
421e4bc50beSVasu Dev 					      u16 index)
422e4bc50beSVasu Dev {
423e4bc50beSVasu Dev 	struct fc_exch **exches = (struct fc_exch **)(pool + 1);
424e4bc50beSVasu Dev 	return exches[index];
425e4bc50beSVasu Dev }
426e4bc50beSVasu Dev 
4273a3b42bfSRobert Love /**
4283a3b42bfSRobert Love  * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
4293a3b42bfSRobert Love  * @pool:  The pool to assign the exchange to
4303a3b42bfSRobert Love  * @index: The index in the pool where the exchange will be assigned
4313a3b42bfSRobert Love  * @ep:	   The exchange to assign to the pool
4323a3b42bfSRobert Love  */
433e4bc50beSVasu Dev static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
434e4bc50beSVasu Dev 				   struct fc_exch *ep)
435e4bc50beSVasu Dev {
436e4bc50beSVasu Dev 	((struct fc_exch **)(pool + 1))[index] = ep;
437e4bc50beSVasu Dev }
438e4bc50beSVasu Dev 
4393a3b42bfSRobert Love /**
4403a3b42bfSRobert Love  * fc_exch_delete() - Delete an exchange
4413a3b42bfSRobert Love  * @ep: The exchange to be deleted
4423a3b42bfSRobert Love  */
443b2f0091fSVasu Dev static void fc_exch_delete(struct fc_exch *ep)
44442e9a92fSRobert Love {
445b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
4462034c19cSHillf Danton 	u16 index;
44742e9a92fSRobert Love 
448b2f0091fSVasu Dev 	pool = ep->pool;
449b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
450b2f0091fSVasu Dev 	WARN_ON(pool->total_exches <= 0);
451b2f0091fSVasu Dev 	pool->total_exches--;
4522034c19cSHillf Danton 
4532034c19cSHillf Danton 	/* update cache of free slot */
4542034c19cSHillf Danton 	index = (ep->xid - ep->em->min_xid) >> fc_cpu_order;
4552034c19cSHillf Danton 	if (pool->left == FC_XID_UNKNOWN)
4562034c19cSHillf Danton 		pool->left = index;
4572034c19cSHillf Danton 	else if (pool->right == FC_XID_UNKNOWN)
4582034c19cSHillf Danton 		pool->right = index;
4592034c19cSHillf Danton 	else
4602034c19cSHillf Danton 		pool->next_index = index;
4612034c19cSHillf Danton 
4622034c19cSHillf Danton 	fc_exch_ptr_set(pool, index, NULL);
46342e9a92fSRobert Love 	list_del(&ep->ex_list);
464b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
46542e9a92fSRobert Love 	fc_exch_release(ep);	/* drop hold for exch in mp */
46642e9a92fSRobert Love }
46742e9a92fSRobert Love 
468fb00cc23SNeil Horman static int fc_seq_send_locked(struct fc_lport *lport, struct fc_seq *sp,
4691a7b75aeSRobert Love 			      struct fc_frame *fp)
4701a7b75aeSRobert Love {
4711a7b75aeSRobert Love 	struct fc_exch *ep;
4721a7b75aeSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
473cae7b6ddSBart Van Assche 	int error = -ENXIO;
4741a7b75aeSRobert Love 	u32 f_ctl;
47514fc315fSVasu Dev 	u8 fh_type = fh->fh_type;
4761a7b75aeSRobert Love 
4771a7b75aeSRobert Love 	ep = fc_seq_exch(sp);
478cae7b6ddSBart Van Assche 
479cae7b6ddSBart Van Assche 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL)) {
480cae7b6ddSBart Van Assche 		fc_frame_free(fp);
481cae7b6ddSBart Van Assche 		goto out;
482cae7b6ddSBart Van Assche 	}
483cae7b6ddSBart Van Assche 
484fb00cc23SNeil Horman 	WARN_ON(!(ep->esb_stat & ESB_ST_SEQ_INIT));
4851a7b75aeSRobert Love 
4861a7b75aeSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
4871a7b75aeSRobert Love 	fc_exch_setup_hdr(ep, fp, f_ctl);
488f60e12e9SJoe Eykholt 	fr_encaps(fp) = ep->encaps;
4891a7b75aeSRobert Love 
4901a7b75aeSRobert Love 	/*
4911a7b75aeSRobert Love 	 * update sequence count if this frame is carrying
4921a7b75aeSRobert Love 	 * multiple FC frames when sequence offload is enabled
4931a7b75aeSRobert Love 	 * by LLD.
4941a7b75aeSRobert Love 	 */
4951a7b75aeSRobert Love 	if (fr_max_payload(fp))
4961a7b75aeSRobert Love 		sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
4971a7b75aeSRobert Love 					fr_max_payload(fp));
4981a7b75aeSRobert Love 	else
4991a7b75aeSRobert Love 		sp->cnt++;
5001a7b75aeSRobert Love 
5011a7b75aeSRobert Love 	/*
5021a7b75aeSRobert Love 	 * Send the frame.
5031a7b75aeSRobert Love 	 */
5043a3b42bfSRobert Love 	error = lport->tt.frame_send(lport, fp);
5051a7b75aeSRobert Love 
50614fc315fSVasu Dev 	if (fh_type == FC_TYPE_BLS)
507fb00cc23SNeil Horman 		goto out;
50877a2b73aSVasu Dev 
5091a7b75aeSRobert Love 	/*
5101a7b75aeSRobert Love 	 * Update the exchange and sequence flags,
5111a7b75aeSRobert Love 	 * assuming all frames for the sequence have been sent.
5121a7b75aeSRobert Love 	 * We can only be called to send once for each sequence.
5131a7b75aeSRobert Love 	 */
5141a7b75aeSRobert Love 	ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ;	/* not first seq */
515cc3593d3SJoe Eykholt 	if (f_ctl & FC_FC_SEQ_INIT)
5161a7b75aeSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
517fb00cc23SNeil Horman out:
518fb00cc23SNeil Horman 	return error;
519fb00cc23SNeil Horman }
520fb00cc23SNeil Horman 
521fb00cc23SNeil Horman /**
522fb00cc23SNeil Horman  * fc_seq_send() - Send a frame using existing sequence/exchange pair
523fb00cc23SNeil Horman  * @lport: The local port that the exchange will be sent on
524fb00cc23SNeil Horman  * @sp:	   The sequence to be sent
525fb00cc23SNeil Horman  * @fp:	   The frame to be sent on the exchange
526cae7b6ddSBart Van Assche  *
527cae7b6ddSBart Van Assche  * Note: The frame will be freed either by a direct call to fc_frame_free(fp)
528cae7b6ddSBart Van Assche  * or indirectly by calling libfc_function_template.frame_send().
529fb00cc23SNeil Horman  */
530fb00cc23SNeil Horman static int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp,
531fb00cc23SNeil Horman 		       struct fc_frame *fp)
532fb00cc23SNeil Horman {
533fb00cc23SNeil Horman 	struct fc_exch *ep;
534fb00cc23SNeil Horman 	int error;
535fb00cc23SNeil Horman 	ep = fc_seq_exch(sp);
536fb00cc23SNeil Horman 	spin_lock_bh(&ep->ex_lock);
537fb00cc23SNeil Horman 	error = fc_seq_send_locked(lport, sp, fp);
5381a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
5391a7b75aeSRobert Love 	return error;
5401a7b75aeSRobert Love }
5411a7b75aeSRobert Love 
5421a7b75aeSRobert Love /**
5433a3b42bfSRobert Love  * fc_seq_alloc() - Allocate a sequence for a given exchange
5443a3b42bfSRobert Love  * @ep:	    The exchange to allocate a new sequence for
5453a3b42bfSRobert Love  * @seq_id: The sequence ID to be used
5461a7b75aeSRobert Love  *
5471a7b75aeSRobert Love  * We don't support multiple originated sequences on the same exchange.
5481a7b75aeSRobert Love  * By implication, any previously originated sequence on this exchange
5491a7b75aeSRobert Love  * is complete, and we reallocate the same sequence.
5501a7b75aeSRobert Love  */
5511a7b75aeSRobert Love static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
5521a7b75aeSRobert Love {
5531a7b75aeSRobert Love 	struct fc_seq *sp;
5541a7b75aeSRobert Love 
5551a7b75aeSRobert Love 	sp = &ep->seq;
5561a7b75aeSRobert Love 	sp->ssb_stat = 0;
5571a7b75aeSRobert Love 	sp->cnt = 0;
5581a7b75aeSRobert Love 	sp->id = seq_id;
5591a7b75aeSRobert Love 	return sp;
5601a7b75aeSRobert Love }
5611a7b75aeSRobert Love 
5623a3b42bfSRobert Love /**
5633a3b42bfSRobert Love  * fc_seq_start_next_locked() - Allocate a new sequence on the same
5643a3b42bfSRobert Love  *				exchange as the supplied sequence
5653a3b42bfSRobert Love  * @sp: The sequence/exchange to get a new sequence for
5663a3b42bfSRobert Love  */
5671a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
5681a7b75aeSRobert Love {
5691a7b75aeSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
5701a7b75aeSRobert Love 
5711a7b75aeSRobert Love 	sp = fc_seq_alloc(ep, ep->seq_id++);
5721a7b75aeSRobert Love 	FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
5731a7b75aeSRobert Love 		    ep->f_ctl, sp->id);
5741a7b75aeSRobert Love 	return sp;
5751a7b75aeSRobert Love }
5761a7b75aeSRobert Love 
5771a7b75aeSRobert Love /**
5783a3b42bfSRobert Love  * fc_seq_start_next() - Lock the exchange and get a new sequence
5793a3b42bfSRobert Love  *			 for a given sequence/exchange pair
5803a3b42bfSRobert Love  * @sp: The sequence/exchange to get a new exchange for
5811a7b75aeSRobert Love  */
5821a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
5831a7b75aeSRobert Love {
5841a7b75aeSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
5851a7b75aeSRobert Love 
5861a7b75aeSRobert Love 	spin_lock_bh(&ep->ex_lock);
5871a7b75aeSRobert Love 	sp = fc_seq_start_next_locked(sp);
5881a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
5891a7b75aeSRobert Love 
5901a7b75aeSRobert Love 	return sp;
5911a7b75aeSRobert Love }
5921a7b75aeSRobert Love 
5931a5c2d7eSJoe Eykholt /*
5941a5c2d7eSJoe Eykholt  * Set the response handler for the exchange associated with a sequence.
5957030fd62SBart Van Assche  *
5967030fd62SBart Van Assche  * Note: May sleep if invoked from outside a response handler.
5971a5c2d7eSJoe Eykholt  */
5981a5c2d7eSJoe Eykholt static void fc_seq_set_resp(struct fc_seq *sp,
5991a5c2d7eSJoe Eykholt 			    void (*resp)(struct fc_seq *, struct fc_frame *,
6001a5c2d7eSJoe Eykholt 					 void *),
6011a5c2d7eSJoe Eykholt 			    void *arg)
6021a5c2d7eSJoe Eykholt {
6031a5c2d7eSJoe Eykholt 	struct fc_exch *ep = fc_seq_exch(sp);
6047030fd62SBart Van Assche 	DEFINE_WAIT(wait);
6051a5c2d7eSJoe Eykholt 
6061a5c2d7eSJoe Eykholt 	spin_lock_bh(&ep->ex_lock);
6077030fd62SBart Van Assche 	while (ep->resp_active && ep->resp_task != current) {
6087030fd62SBart Van Assche 		prepare_to_wait(&ep->resp_wq, &wait, TASK_UNINTERRUPTIBLE);
6097030fd62SBart Van Assche 		spin_unlock_bh(&ep->ex_lock);
6107030fd62SBart Van Assche 
6117030fd62SBart Van Assche 		schedule();
6127030fd62SBart Van Assche 
6137030fd62SBart Van Assche 		spin_lock_bh(&ep->ex_lock);
6147030fd62SBart Van Assche 	}
6157030fd62SBart Van Assche 	finish_wait(&ep->resp_wq, &wait);
6161a5c2d7eSJoe Eykholt 	ep->resp = resp;
6171a5c2d7eSJoe Eykholt 	ep->arg = arg;
6181a5c2d7eSJoe Eykholt 	spin_unlock_bh(&ep->ex_lock);
6191a5c2d7eSJoe Eykholt }
6201a5c2d7eSJoe Eykholt 
6211a7b75aeSRobert Love /**
62277a2b73aSVasu Dev  * fc_exch_abort_locked() - Abort an exchange
62377a2b73aSVasu Dev  * @ep:	The exchange to be aborted
6243a3b42bfSRobert Love  * @timer_msec: The period of time to wait before aborting
6253a3b42bfSRobert Love  *
62677a2b73aSVasu Dev  * Locking notes:  Called with exch lock held
62777a2b73aSVasu Dev  *
62877a2b73aSVasu Dev  * Return value: 0 on success else error code
6291a7b75aeSRobert Love  */
63077a2b73aSVasu Dev static int fc_exch_abort_locked(struct fc_exch *ep,
6311a7b75aeSRobert Love 				unsigned int timer_msec)
63242e9a92fSRobert Love {
63342e9a92fSRobert Love 	struct fc_seq *sp;
63442e9a92fSRobert Love 	struct fc_frame *fp;
63542e9a92fSRobert Love 	int error;
63642e9a92fSRobert Love 
63757d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "exch: abort, time %d msecs\n", timer_msec);
63842e9a92fSRobert Love 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
63957d3ec7eSHannes Reinecke 	    ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
64057d3ec7eSHannes Reinecke 		FC_EXCH_DBG(ep, "exch: already completed esb %x state %x\n",
64157d3ec7eSHannes Reinecke 			    ep->esb_stat, ep->state);
64242e9a92fSRobert Love 		return -ENXIO;
64357d3ec7eSHannes Reinecke 	}
64442e9a92fSRobert Love 
64542e9a92fSRobert Love 	/*
64642e9a92fSRobert Love 	 * Send the abort on a new sequence if possible.
64742e9a92fSRobert Love 	 */
64842e9a92fSRobert Love 	sp = fc_seq_start_next_locked(&ep->seq);
64977a2b73aSVasu Dev 	if (!sp)
65042e9a92fSRobert Love 		return -ENOMEM;
65142e9a92fSRobert Love 
65242e9a92fSRobert Love 	if (timer_msec)
65342e9a92fSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
65442e9a92fSRobert Love 
655cae7b6ddSBart Van Assche 	if (ep->sid) {
65642e9a92fSRobert Love 		/*
65742e9a92fSRobert Love 		 * Send an abort for the sequence that timed out.
65842e9a92fSRobert Love 		 */
65942e9a92fSRobert Love 		fp = fc_frame_alloc(ep->lp, 0);
66042e9a92fSRobert Love 		if (fp) {
661cae7b6ddSBart Van Assche 			ep->esb_stat |= ESB_ST_SEQ_INIT;
66242e9a92fSRobert Love 			fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
663cae7b6ddSBart Van Assche 				       FC_TYPE_BLS, FC_FC_END_SEQ |
664cae7b6ddSBart Van Assche 				       FC_FC_SEQ_INIT, 0);
665fb00cc23SNeil Horman 			error = fc_seq_send_locked(ep->lp, sp, fp);
666cae7b6ddSBart Van Assche 		} else {
66742e9a92fSRobert Love 			error = -ENOBUFS;
668cae7b6ddSBart Van Assche 		}
669cae7b6ddSBart Van Assche 	} else {
670cae7b6ddSBart Van Assche 		/*
671cae7b6ddSBart Van Assche 		 * If not logged into the fabric, don't send ABTS but leave
672cae7b6ddSBart Van Assche 		 * sequence active until next timeout.
673cae7b6ddSBart Van Assche 		 */
674cae7b6ddSBart Van Assche 		error = 0;
675cae7b6ddSBart Van Assche 	}
676cae7b6ddSBart Van Assche 	ep->esb_stat |= ESB_ST_ABNORMAL;
67742e9a92fSRobert Love 	return error;
67842e9a92fSRobert Love }
67942e9a92fSRobert Love 
6803a3b42bfSRobert Love /**
68177a2b73aSVasu Dev  * fc_seq_exch_abort() - Abort an exchange and sequence
68277a2b73aSVasu Dev  * @req_sp:	The sequence to be aborted
68377a2b73aSVasu Dev  * @timer_msec: The period of time to wait before aborting
68477a2b73aSVasu Dev  *
68577a2b73aSVasu Dev  * Generally called because of a timeout or an abort from the upper layer.
68677a2b73aSVasu Dev  *
68777a2b73aSVasu Dev  * Return value: 0 on success else error code
68877a2b73aSVasu Dev  */
68977a2b73aSVasu Dev static int fc_seq_exch_abort(const struct fc_seq *req_sp,
69077a2b73aSVasu Dev 			     unsigned int timer_msec)
69177a2b73aSVasu Dev {
69277a2b73aSVasu Dev 	struct fc_exch *ep;
69377a2b73aSVasu Dev 	int error;
69477a2b73aSVasu Dev 
69577a2b73aSVasu Dev 	ep = fc_seq_exch(req_sp);
69677a2b73aSVasu Dev 	spin_lock_bh(&ep->ex_lock);
69777a2b73aSVasu Dev 	error = fc_exch_abort_locked(ep, timer_msec);
69877a2b73aSVasu Dev 	spin_unlock_bh(&ep->ex_lock);
69977a2b73aSVasu Dev 	return error;
70077a2b73aSVasu Dev }
70177a2b73aSVasu Dev 
70277a2b73aSVasu Dev /**
7037030fd62SBart Van Assche  * fc_invoke_resp() - invoke ep->resp()
7047030fd62SBart Van Assche  *
7057030fd62SBart Van Assche  * Notes:
7067030fd62SBart Van Assche  * It is assumed that after initialization finished (this means the
7077030fd62SBart Van Assche  * first unlock of ex_lock after fc_exch_alloc()) ep->resp and ep->arg are
7087030fd62SBart Van Assche  * modified only via fc_seq_set_resp(). This guarantees that none of these
7097030fd62SBart Van Assche  * two variables changes if ep->resp_active > 0.
7107030fd62SBart Van Assche  *
7117030fd62SBart Van Assche  * If an fc_seq_set_resp() call is busy modifying ep->resp and ep->arg when
7127030fd62SBart Van Assche  * this function is invoked, the first spin_lock_bh() call in this function
7137030fd62SBart Van Assche  * will wait until fc_seq_set_resp() has finished modifying these variables.
7147030fd62SBart Van Assche  *
7157030fd62SBart Van Assche  * Since fc_exch_done() invokes fc_seq_set_resp() it is guaranteed that that
7167030fd62SBart Van Assche  * ep->resp() won't be invoked after fc_exch_done() has returned.
7177030fd62SBart Van Assche  *
7187030fd62SBart Van Assche  * The response handler itself may invoke fc_exch_done(), which will clear the
7197030fd62SBart Van Assche  * ep->resp pointer.
7207030fd62SBart Van Assche  *
7217030fd62SBart Van Assche  * Return value:
7227030fd62SBart Van Assche  * Returns true if and only if ep->resp has been invoked.
7237030fd62SBart Van Assche  */
7247030fd62SBart Van Assche static bool fc_invoke_resp(struct fc_exch *ep, struct fc_seq *sp,
7257030fd62SBart Van Assche 			   struct fc_frame *fp)
7267030fd62SBart Van Assche {
7277030fd62SBart Van Assche 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
7287030fd62SBart Van Assche 	void *arg;
7297030fd62SBart Van Assche 	bool res = false;
7307030fd62SBart Van Assche 
7317030fd62SBart Van Assche 	spin_lock_bh(&ep->ex_lock);
7327030fd62SBart Van Assche 	ep->resp_active++;
7337030fd62SBart Van Assche 	if (ep->resp_task != current)
7347030fd62SBart Van Assche 		ep->resp_task = !ep->resp_task ? current : NULL;
7357030fd62SBart Van Assche 	resp = ep->resp;
7367030fd62SBart Van Assche 	arg = ep->arg;
7377030fd62SBart Van Assche 	spin_unlock_bh(&ep->ex_lock);
7387030fd62SBart Van Assche 
7397030fd62SBart Van Assche 	if (resp) {
7407030fd62SBart Van Assche 		resp(sp, fp, arg);
7417030fd62SBart Van Assche 		res = true;
7427030fd62SBart Van Assche 	}
7437030fd62SBart Van Assche 
7447030fd62SBart Van Assche 	spin_lock_bh(&ep->ex_lock);
7457030fd62SBart Van Assche 	if (--ep->resp_active == 0)
7467030fd62SBart Van Assche 		ep->resp_task = NULL;
7477030fd62SBart Van Assche 	spin_unlock_bh(&ep->ex_lock);
7487030fd62SBart Van Assche 
7497030fd62SBart Van Assche 	if (ep->resp_active == 0)
7507030fd62SBart Van Assche 		wake_up(&ep->resp_wq);
7517030fd62SBart Van Assche 
7527030fd62SBart Van Assche 	return res;
7537030fd62SBart Van Assche }
7547030fd62SBart Van Assche 
7557030fd62SBart Van Assche /**
7563a3b42bfSRobert Love  * fc_exch_timeout() - Handle exchange timer expiration
7573a3b42bfSRobert Love  * @work: The work_struct identifying the exchange that timed out
75842e9a92fSRobert Love  */
75942e9a92fSRobert Love static void fc_exch_timeout(struct work_struct *work)
76042e9a92fSRobert Love {
76142e9a92fSRobert Love 	struct fc_exch *ep = container_of(work, struct fc_exch,
76242e9a92fSRobert Love 					  timeout_work.work);
76342e9a92fSRobert Love 	struct fc_seq *sp = &ep->seq;
76442e9a92fSRobert Love 	u32 e_stat;
76542e9a92fSRobert Love 	int rc = 1;
76642e9a92fSRobert Love 
76757d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "Exchange timed out state %x\n", ep->state);
768cd305ce4SRobert Love 
76942e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
77042e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
77142e9a92fSRobert Love 		goto unlock;
77242e9a92fSRobert Love 
77342e9a92fSRobert Love 	e_stat = ep->esb_stat;
77442e9a92fSRobert Love 	if (e_stat & ESB_ST_COMPLETE) {
77542e9a92fSRobert Love 		ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
776a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
77742e9a92fSRobert Love 		if (e_stat & ESB_ST_REC_QUAL)
77842e9a92fSRobert Love 			fc_exch_rrq(ep);
77942e9a92fSRobert Love 		goto done;
78042e9a92fSRobert Love 	} else {
78142e9a92fSRobert Love 		if (e_stat & ESB_ST_ABNORMAL)
78242e9a92fSRobert Love 			rc = fc_exch_done_locked(ep);
78342e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
784f3162483SParikh, Neerav 		if (!rc)
785f3162483SParikh, Neerav 			fc_exch_delete(ep);
7867030fd62SBart Van Assche 		fc_invoke_resp(ep, sp, ERR_PTR(-FC_EX_TIMEOUT));
7877030fd62SBart Van Assche 		fc_seq_set_resp(sp, NULL, ep->arg);
78842e9a92fSRobert Love 		fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
78942e9a92fSRobert Love 		goto done;
79042e9a92fSRobert Love 	}
79142e9a92fSRobert Love unlock:
79242e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
79342e9a92fSRobert Love done:
79442e9a92fSRobert Love 	/*
79542e9a92fSRobert Love 	 * This release matches the hold taken when the timer was set.
79642e9a92fSRobert Love 	 */
79742e9a92fSRobert Love 	fc_exch_release(ep);
79842e9a92fSRobert Love }
79942e9a92fSRobert Love 
80052ff878cSVasu Dev /**
8013a3b42bfSRobert Love  * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
8023a3b42bfSRobert Love  * @lport: The local port that the exchange is for
8033a3b42bfSRobert Love  * @mp:	   The exchange manager that will allocate the exchange
80442e9a92fSRobert Love  *
805d7179680SVasu Dev  * Returns pointer to allocated fc_exch with exch lock held.
80642e9a92fSRobert Love  */
80752ff878cSVasu Dev static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
808d7179680SVasu Dev 					struct fc_exch_mgr *mp)
80942e9a92fSRobert Love {
81042e9a92fSRobert Love 	struct fc_exch *ep;
811b2f0091fSVasu Dev 	unsigned int cpu;
812b2f0091fSVasu Dev 	u16 index;
813b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
81442e9a92fSRobert Love 
81542e9a92fSRobert Love 	/* allocate memory for exchange */
81642e9a92fSRobert Love 	ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
81742e9a92fSRobert Love 	if (!ep) {
81842e9a92fSRobert Love 		atomic_inc(&mp->stats.no_free_exch);
81942e9a92fSRobert Love 		goto out;
82042e9a92fSRobert Love 	}
82142e9a92fSRobert Love 	memset(ep, 0, sizeof(*ep));
82242e9a92fSRobert Love 
823f018b73aSJoe Eykholt 	cpu = get_cpu();
824b2f0091fSVasu Dev 	pool = per_cpu_ptr(mp->pool, cpu);
825b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
826f018b73aSJoe Eykholt 	put_cpu();
8272034c19cSHillf Danton 
8282034c19cSHillf Danton 	/* peek cache of free slot */
8292034c19cSHillf Danton 	if (pool->left != FC_XID_UNKNOWN) {
8302034c19cSHillf Danton 		index = pool->left;
8312034c19cSHillf Danton 		pool->left = FC_XID_UNKNOWN;
8322034c19cSHillf Danton 		goto hit;
8332034c19cSHillf Danton 	}
8342034c19cSHillf Danton 	if (pool->right != FC_XID_UNKNOWN) {
8352034c19cSHillf Danton 		index = pool->right;
8362034c19cSHillf Danton 		pool->right = FC_XID_UNKNOWN;
8372034c19cSHillf Danton 		goto hit;
8382034c19cSHillf Danton 	}
8392034c19cSHillf Danton 
840b2f0091fSVasu Dev 	index = pool->next_index;
841b2f0091fSVasu Dev 	/* allocate new exch from pool */
842b2f0091fSVasu Dev 	while (fc_exch_ptr_get(pool, index)) {
843b2f0091fSVasu Dev 		index = index == mp->pool_max_index ? 0 : index + 1;
844b2f0091fSVasu Dev 		if (index == pool->next_index)
84542e9a92fSRobert Love 			goto err;
84642e9a92fSRobert Love 	}
847b2f0091fSVasu Dev 	pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
8482034c19cSHillf Danton hit:
84942e9a92fSRobert Love 	fc_exch_hold(ep);	/* hold for exch in mp */
85042e9a92fSRobert Love 	spin_lock_init(&ep->ex_lock);
85142e9a92fSRobert Love 	/*
85242e9a92fSRobert Love 	 * Hold exch lock for caller to prevent fc_exch_reset()
85342e9a92fSRobert Love 	 * from releasing exch	while fc_exch_alloc() caller is
85442e9a92fSRobert Love 	 * still working on exch.
85542e9a92fSRobert Love 	 */
85642e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
85742e9a92fSRobert Love 
858b2f0091fSVasu Dev 	fc_exch_ptr_set(pool, index, ep);
859b2f0091fSVasu Dev 	list_add_tail(&ep->ex_list, &pool->ex_list);
86042e9a92fSRobert Love 	fc_seq_alloc(ep, ep->seq_id++);
861b2f0091fSVasu Dev 	pool->total_exches++;
862b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
86342e9a92fSRobert Love 
86442e9a92fSRobert Love 	/*
86542e9a92fSRobert Love 	 *  update exchange
86642e9a92fSRobert Love 	 */
867b2f0091fSVasu Dev 	ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
86842e9a92fSRobert Love 	ep->em = mp;
869b2f0091fSVasu Dev 	ep->pool = pool;
87052ff878cSVasu Dev 	ep->lp = lport;
87142e9a92fSRobert Love 	ep->f_ctl = FC_FC_FIRST_SEQ;	/* next seq is first seq */
87242e9a92fSRobert Love 	ep->rxid = FC_XID_UNKNOWN;
87342e9a92fSRobert Love 	ep->class = mp->class;
8747030fd62SBart Van Assche 	ep->resp_active = 0;
8757030fd62SBart Van Assche 	init_waitqueue_head(&ep->resp_wq);
87642e9a92fSRobert Love 	INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
87742e9a92fSRobert Love out:
87842e9a92fSRobert Love 	return ep;
87942e9a92fSRobert Love err:
880b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
88142e9a92fSRobert Love 	atomic_inc(&mp->stats.no_free_exch_xid);
88242e9a92fSRobert Love 	mempool_free(ep, mp->ep_pool);
88342e9a92fSRobert Love 	return NULL;
88442e9a92fSRobert Love }
88552ff878cSVasu Dev 
88652ff878cSVasu Dev /**
8873a3b42bfSRobert Love  * fc_exch_alloc() - Allocate an exchange from an EM on a
8883a3b42bfSRobert Love  *		     local port's list of EMs.
8893a3b42bfSRobert Love  * @lport: The local port that will own the exchange
8903a3b42bfSRobert Love  * @fp:	   The FC frame that the exchange will be for
89152ff878cSVasu Dev  *
8923a3b42bfSRobert Love  * This function walks the list of exchange manager(EM)
8933a3b42bfSRobert Love  * anchors to select an EM for a new exchange allocation. The
8943a3b42bfSRobert Love  * EM is selected when a NULL match function pointer is encountered
8953a3b42bfSRobert Love  * or when a call to a match function returns true.
89652ff878cSVasu Dev  */
897f8f91f3fSMartin K. Petersen static struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
8981a7b75aeSRobert Love 				     struct fc_frame *fp)
89952ff878cSVasu Dev {
90052ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
901f8f91f3fSMartin K. Petersen 	struct fc_exch *ep;
90252ff878cSVasu Dev 
903f8f91f3fSMartin K. Petersen 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
904f8f91f3fSMartin K. Petersen 		if (!ema->match || ema->match(fp)) {
905f8f91f3fSMartin K. Petersen 			ep = fc_exch_em_alloc(lport, ema->mp);
906f8f91f3fSMartin K. Petersen 			if (ep)
907f8f91f3fSMartin K. Petersen 				return ep;
908f8f91f3fSMartin K. Petersen 		}
909f8f91f3fSMartin K. Petersen 	}
91052ff878cSVasu Dev 	return NULL;
91152ff878cSVasu Dev }
91242e9a92fSRobert Love 
9133a3b42bfSRobert Love /**
9143a3b42bfSRobert Love  * fc_exch_find() - Lookup and hold an exchange
9153a3b42bfSRobert Love  * @mp:	 The exchange manager to lookup the exchange from
9163a3b42bfSRobert Love  * @xid: The XID of the exchange to look up
91742e9a92fSRobert Love  */
91842e9a92fSRobert Love static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
91942e9a92fSRobert Love {
920b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
92142e9a92fSRobert Love 	struct fc_exch *ep = NULL;
922fa068832SChris Leech 	u16 cpu = xid & fc_cpu_mask;
923fa068832SChris Leech 
924fa068832SChris Leech 	if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
925fa068832SChris Leech 		printk_ratelimited(KERN_ERR
926fa068832SChris Leech 			"libfc: lookup request for XID = %d, "
927fa068832SChris Leech 			"indicates invalid CPU %d\n", xid, cpu);
928fa068832SChris Leech 		return NULL;
929fa068832SChris Leech 	}
93042e9a92fSRobert Love 
93142e9a92fSRobert Love 	if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
932fa068832SChris Leech 		pool = per_cpu_ptr(mp->pool, cpu);
933b2f0091fSVasu Dev 		spin_lock_bh(&pool->lock);
934b2f0091fSVasu Dev 		ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
9358d080236SBart Van Assche 		if (ep) {
9368d080236SBart Van Assche 			WARN_ON(ep->xid != xid);
93742e9a92fSRobert Love 			fc_exch_hold(ep);
9388d080236SBart Van Assche 		}
939b2f0091fSVasu Dev 		spin_unlock_bh(&pool->lock);
94042e9a92fSRobert Love 	}
94142e9a92fSRobert Love 	return ep;
94242e9a92fSRobert Love }
94342e9a92fSRobert Love 
9441a7b75aeSRobert Love 
9451a7b75aeSRobert Love /**
9461a7b75aeSRobert Love  * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
9471a7b75aeSRobert Love  *		    the memory allocated for the related objects may be freed.
9483a3b42bfSRobert Love  * @sp: The sequence that has completed
9497030fd62SBart Van Assche  *
9507030fd62SBart Van Assche  * Note: May sleep if invoked from outside a response handler.
9511a7b75aeSRobert Love  */
9521a7b75aeSRobert Love static void fc_exch_done(struct fc_seq *sp)
95342e9a92fSRobert Love {
95442e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
95542e9a92fSRobert Love 	int rc;
95642e9a92fSRobert Love 
95742e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
95842e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
95942e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
9607030fd62SBart Van Assche 
9617030fd62SBart Van Assche 	fc_seq_set_resp(sp, NULL, ep->arg);
96242e9a92fSRobert Love 	if (!rc)
963b2f0091fSVasu Dev 		fc_exch_delete(ep);
96442e9a92fSRobert Love }
96542e9a92fSRobert Love 
9663a3b42bfSRobert Love /**
9673a3b42bfSRobert Love  * fc_exch_resp() - Allocate a new exchange for a response frame
9683a3b42bfSRobert Love  * @lport: The local port that the exchange was for
9693a3b42bfSRobert Love  * @mp:	   The exchange manager to allocate the exchange from
9703a3b42bfSRobert Love  * @fp:	   The response frame
9713a3b42bfSRobert Love  *
97242e9a92fSRobert Love  * Sets the responder ID in the frame header.
97342e9a92fSRobert Love  */
97452ff878cSVasu Dev static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
97552ff878cSVasu Dev 				    struct fc_exch_mgr *mp,
97652ff878cSVasu Dev 				    struct fc_frame *fp)
97742e9a92fSRobert Love {
97842e9a92fSRobert Love 	struct fc_exch *ep;
97942e9a92fSRobert Love 	struct fc_frame_header *fh;
98042e9a92fSRobert Love 
98152ff878cSVasu Dev 	ep = fc_exch_alloc(lport, fp);
98242e9a92fSRobert Love 	if (ep) {
98342e9a92fSRobert Love 		ep->class = fc_frame_class(fp);
98442e9a92fSRobert Love 
98542e9a92fSRobert Love 		/*
98642e9a92fSRobert Love 		 * Set EX_CTX indicating we're responding on this exchange.
98742e9a92fSRobert Love 		 */
98842e9a92fSRobert Love 		ep->f_ctl |= FC_FC_EX_CTX;	/* we're responding */
98942e9a92fSRobert Love 		ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not new */
99042e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
99142e9a92fSRobert Love 		ep->sid = ntoh24(fh->fh_d_id);
99242e9a92fSRobert Love 		ep->did = ntoh24(fh->fh_s_id);
99342e9a92fSRobert Love 		ep->oid = ep->did;
99442e9a92fSRobert Love 
99542e9a92fSRobert Love 		/*
99642e9a92fSRobert Love 		 * Allocated exchange has placed the XID in the
99742e9a92fSRobert Love 		 * originator field. Move it to the responder field,
99842e9a92fSRobert Love 		 * and set the originator XID from the frame.
99942e9a92fSRobert Love 		 */
100042e9a92fSRobert Love 		ep->rxid = ep->xid;
100142e9a92fSRobert Love 		ep->oxid = ntohs(fh->fh_ox_id);
100242e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
100342e9a92fSRobert Love 		if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
100442e9a92fSRobert Love 			ep->esb_stat &= ~ESB_ST_SEQ_INIT;
100542e9a92fSRobert Love 
100642e9a92fSRobert Love 		fc_exch_hold(ep);	/* hold for caller */
100752ff878cSVasu Dev 		spin_unlock_bh(&ep->ex_lock);	/* lock from fc_exch_alloc */
100842e9a92fSRobert Love 	}
100942e9a92fSRobert Love 	return ep;
101042e9a92fSRobert Love }
101142e9a92fSRobert Love 
10123a3b42bfSRobert Love /**
10133a3b42bfSRobert Love  * fc_seq_lookup_recip() - Find a sequence where the other end
10143a3b42bfSRobert Love  *			   originated the sequence
10153a3b42bfSRobert Love  * @lport: The local port that the frame was sent to
10163a3b42bfSRobert Love  * @mp:	   The Exchange Manager to lookup the exchange from
10173a3b42bfSRobert Love  * @fp:	   The frame associated with the sequence we're looking for
10183a3b42bfSRobert Love  *
101942e9a92fSRobert Love  * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
102042e9a92fSRobert Love  * on the ep that should be released by the caller.
102142e9a92fSRobert Love  */
102252ff878cSVasu Dev static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
102352ff878cSVasu Dev 						 struct fc_exch_mgr *mp,
1024b2ab99c9SRobert Love 						 struct fc_frame *fp)
102542e9a92fSRobert Love {
102642e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
102742e9a92fSRobert Love 	struct fc_exch *ep = NULL;
102842e9a92fSRobert Love 	struct fc_seq *sp = NULL;
102942e9a92fSRobert Love 	enum fc_pf_rjt_reason reject = FC_RJT_NONE;
103042e9a92fSRobert Love 	u32 f_ctl;
103142e9a92fSRobert Love 	u16 xid;
103242e9a92fSRobert Love 
103342e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
103442e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
103542e9a92fSRobert Love 
103642e9a92fSRobert Love 	/*
103742e9a92fSRobert Love 	 * Lookup or create the exchange if we will be creating the sequence.
103842e9a92fSRobert Love 	 */
103942e9a92fSRobert Love 	if (f_ctl & FC_FC_EX_CTX) {
104042e9a92fSRobert Love 		xid = ntohs(fh->fh_ox_id);	/* we originated exch */
104142e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
104242e9a92fSRobert Love 		if (!ep) {
104342e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
104442e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
104542e9a92fSRobert Love 			goto out;
104642e9a92fSRobert Love 		}
104742e9a92fSRobert Love 		if (ep->rxid == FC_XID_UNKNOWN)
104842e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
104942e9a92fSRobert Love 		else if (ep->rxid != ntohs(fh->fh_rx_id)) {
105042e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
105142e9a92fSRobert Love 			goto rel;
105242e9a92fSRobert Love 		}
105342e9a92fSRobert Love 	} else {
105442e9a92fSRobert Love 		xid = ntohs(fh->fh_rx_id);	/* we are the responder */
105542e9a92fSRobert Love 
105642e9a92fSRobert Love 		/*
105742e9a92fSRobert Love 		 * Special case for MDS issuing an ELS TEST with a
105842e9a92fSRobert Love 		 * bad rxid of 0.
105942e9a92fSRobert Love 		 * XXX take this out once we do the proper reject.
106042e9a92fSRobert Love 		 */
106142e9a92fSRobert Love 		if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
106242e9a92fSRobert Love 		    fc_frame_payload_op(fp) == ELS_TEST) {
106342e9a92fSRobert Love 			fh->fh_rx_id = htons(FC_XID_UNKNOWN);
106442e9a92fSRobert Love 			xid = FC_XID_UNKNOWN;
106542e9a92fSRobert Love 		}
106642e9a92fSRobert Love 
106742e9a92fSRobert Love 		/*
106842e9a92fSRobert Love 		 * new sequence - find the exchange
106942e9a92fSRobert Love 		 */
107042e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
107142e9a92fSRobert Love 		if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
107242e9a92fSRobert Love 			if (ep) {
107342e9a92fSRobert Love 				atomic_inc(&mp->stats.xid_busy);
107442e9a92fSRobert Love 				reject = FC_RJT_RX_ID;
107542e9a92fSRobert Love 				goto rel;
107642e9a92fSRobert Love 			}
107752ff878cSVasu Dev 			ep = fc_exch_resp(lport, mp, fp);
107842e9a92fSRobert Love 			if (!ep) {
107942e9a92fSRobert Love 				reject = FC_RJT_EXCH_EST;	/* XXX */
108042e9a92fSRobert Love 				goto out;
108142e9a92fSRobert Love 			}
108242e9a92fSRobert Love 			xid = ep->xid;	/* get our XID */
108342e9a92fSRobert Love 		} else if (!ep) {
108442e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
108542e9a92fSRobert Love 			reject = FC_RJT_RX_ID;	/* XID not found */
108642e9a92fSRobert Love 			goto out;
108742e9a92fSRobert Love 		}
108842e9a92fSRobert Love 	}
108942e9a92fSRobert Love 
10905d73bea2SBart Van Assche 	spin_lock_bh(&ep->ex_lock);
109142e9a92fSRobert Love 	/*
109242e9a92fSRobert Love 	 * At this point, we have the exchange held.
109342e9a92fSRobert Love 	 * Find or create the sequence.
109442e9a92fSRobert Love 	 */
109542e9a92fSRobert Love 	if (fc_sof_is_init(fr_sof(fp))) {
1096a104c844SVasu Dev 		sp = &ep->seq;
109742e9a92fSRobert Love 		sp->ssb_stat |= SSB_ST_RESP;
1098b3667f91SJoe Eykholt 		sp->id = fh->fh_seq_id;
109942e9a92fSRobert Love 	} else {
110042e9a92fSRobert Love 		sp = &ep->seq;
110142e9a92fSRobert Love 		if (sp->id != fh->fh_seq_id) {
110242e9a92fSRobert Love 			atomic_inc(&mp->stats.seq_not_found);
1103e3e65c69SKiran Patil 			if (f_ctl & FC_FC_END_SEQ) {
1104e3e65c69SKiran Patil 				/*
1105e3e65c69SKiran Patil 				 * Update sequence_id based on incoming last
1106e3e65c69SKiran Patil 				 * frame of sequence exchange. This is needed
11071bd49b48SVasu Dev 				 * for FC target where DDP has been used
1108e3e65c69SKiran Patil 				 * on target where, stack is indicated only
1109e3e65c69SKiran Patil 				 * about last frame's (payload _header) header.
1110e3e65c69SKiran Patil 				 * Whereas "seq_id" which is part of
1111e3e65c69SKiran Patil 				 * frame_header is allocated by initiator
1112e3e65c69SKiran Patil 				 * which is totally different from "seq_id"
1113e3e65c69SKiran Patil 				 * allocated when XFER_RDY was sent by target.
1114e3e65c69SKiran Patil 				 * To avoid false -ve which results into not
1115e3e65c69SKiran Patil 				 * sending RSP, hence write request on other
1116e3e65c69SKiran Patil 				 * end never finishes.
1117e3e65c69SKiran Patil 				 */
1118e3e65c69SKiran Patil 				sp->ssb_stat |= SSB_ST_RESP;
1119e3e65c69SKiran Patil 				sp->id = fh->fh_seq_id;
1120e3e65c69SKiran Patil 			} else {
11215d73bea2SBart Van Assche 				spin_unlock_bh(&ep->ex_lock);
11225d73bea2SBart Van Assche 
1123e3e65c69SKiran Patil 				/* sequence/exch should exist */
1124e3e65c69SKiran Patil 				reject = FC_RJT_SEQ_ID;
112542e9a92fSRobert Love 				goto rel;
112642e9a92fSRobert Love 			}
112742e9a92fSRobert Love 		}
1128e3e65c69SKiran Patil 	}
112942e9a92fSRobert Love 	WARN_ON(ep != fc_seq_exch(sp));
113042e9a92fSRobert Love 
113142e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
113242e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
11335d73bea2SBart Van Assche 	spin_unlock_bh(&ep->ex_lock);
113442e9a92fSRobert Love 
113542e9a92fSRobert Love 	fr_seq(fp) = sp;
113642e9a92fSRobert Love out:
113742e9a92fSRobert Love 	return reject;
113842e9a92fSRobert Love rel:
113942e9a92fSRobert Love 	fc_exch_done(&ep->seq);
114042e9a92fSRobert Love 	fc_exch_release(ep);	/* hold from fc_exch_find/fc_exch_resp */
114142e9a92fSRobert Love 	return reject;
114242e9a92fSRobert Love }
114342e9a92fSRobert Love 
11443a3b42bfSRobert Love /**
11453a3b42bfSRobert Love  * fc_seq_lookup_orig() - Find a sequence where this end
11463a3b42bfSRobert Love  *			  originated the sequence
11473a3b42bfSRobert Love  * @mp:	   The Exchange Manager to lookup the exchange from
11483a3b42bfSRobert Love  * @fp:	   The frame associated with the sequence we're looking for
11493a3b42bfSRobert Love  *
115042e9a92fSRobert Love  * Does not hold the sequence for the caller.
115142e9a92fSRobert Love  */
115242e9a92fSRobert Love static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
115342e9a92fSRobert Love 					 struct fc_frame *fp)
115442e9a92fSRobert Love {
115542e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
115642e9a92fSRobert Love 	struct fc_exch *ep;
115742e9a92fSRobert Love 	struct fc_seq *sp = NULL;
115842e9a92fSRobert Love 	u32 f_ctl;
115942e9a92fSRobert Love 	u16 xid;
116042e9a92fSRobert Love 
116142e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
116242e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
116342e9a92fSRobert Love 	xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
116442e9a92fSRobert Love 	ep = fc_exch_find(mp, xid);
116542e9a92fSRobert Love 	if (!ep)
116642e9a92fSRobert Love 		return NULL;
116742e9a92fSRobert Love 	if (ep->seq.id == fh->fh_seq_id) {
116842e9a92fSRobert Love 		/*
116942e9a92fSRobert Love 		 * Save the RX_ID if we didn't previously know it.
117042e9a92fSRobert Love 		 */
117142e9a92fSRobert Love 		sp = &ep->seq;
117242e9a92fSRobert Love 		if ((f_ctl & FC_FC_EX_CTX) != 0 &&
117342e9a92fSRobert Love 		    ep->rxid == FC_XID_UNKNOWN) {
117442e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
117542e9a92fSRobert Love 		}
117642e9a92fSRobert Love 	}
117742e9a92fSRobert Love 	fc_exch_release(ep);
117842e9a92fSRobert Love 	return sp;
117942e9a92fSRobert Love }
118042e9a92fSRobert Love 
11813a3b42bfSRobert Love /**
11823a3b42bfSRobert Love  * fc_exch_set_addr() - Set the source and destination IDs for an exchange
11833a3b42bfSRobert Love  * @ep:	     The exchange to set the addresses for
11843a3b42bfSRobert Love  * @orig_id: The originator's ID
11853a3b42bfSRobert Love  * @resp_id: The responder's ID
11863a3b42bfSRobert Love  *
118742e9a92fSRobert Love  * Note this must be done before the first sequence of the exchange is sent.
118842e9a92fSRobert Love  */
118942e9a92fSRobert Love static void fc_exch_set_addr(struct fc_exch *ep,
119042e9a92fSRobert Love 			     u32 orig_id, u32 resp_id)
119142e9a92fSRobert Love {
119242e9a92fSRobert Love 	ep->oid = orig_id;
119342e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP) {
119442e9a92fSRobert Love 		ep->sid = resp_id;
119542e9a92fSRobert Love 		ep->did = orig_id;
119642e9a92fSRobert Love 	} else {
119742e9a92fSRobert Love 		ep->sid = orig_id;
119842e9a92fSRobert Love 		ep->did = resp_id;
119942e9a92fSRobert Love 	}
120042e9a92fSRobert Love }
120142e9a92fSRobert Love 
12021a7b75aeSRobert Love /**
120325985edcSLucas De Marchi  * fc_seq_els_rsp_send() - Send an ELS response using information from
12043a3b42bfSRobert Love  *			   the existing sequence/exchange.
120592261156SJoe Eykholt  * @fp:	      The received frame
12063a3b42bfSRobert Love  * @els_cmd:  The ELS command to be sent
12073a3b42bfSRobert Love  * @els_data: The ELS data to be sent
120892261156SJoe Eykholt  *
120992261156SJoe Eykholt  * The received frame is not freed.
121042e9a92fSRobert Love  */
121192261156SJoe Eykholt static void fc_seq_els_rsp_send(struct fc_frame *fp, enum fc_els_cmd els_cmd,
121242e9a92fSRobert Love 				struct fc_seq_els_data *els_data)
121342e9a92fSRobert Love {
121442e9a92fSRobert Love 	switch (els_cmd) {
121542e9a92fSRobert Love 	case ELS_LS_RJT:
121692261156SJoe Eykholt 		fc_seq_ls_rjt(fp, els_data->reason, els_data->explan);
121742e9a92fSRobert Love 		break;
121842e9a92fSRobert Love 	case ELS_LS_ACC:
121992261156SJoe Eykholt 		fc_seq_ls_acc(fp);
122042e9a92fSRobert Love 		break;
122142e9a92fSRobert Love 	case ELS_RRQ:
122292261156SJoe Eykholt 		fc_exch_els_rrq(fp);
122342e9a92fSRobert Love 		break;
122442e9a92fSRobert Love 	case ELS_REC:
122592261156SJoe Eykholt 		fc_exch_els_rec(fp);
122642e9a92fSRobert Love 		break;
122742e9a92fSRobert Love 	default:
122892261156SJoe Eykholt 		FC_LPORT_DBG(fr_dev(fp), "Invalid ELS CMD:%x\n", els_cmd);
122942e9a92fSRobert Love 	}
123042e9a92fSRobert Love }
123142e9a92fSRobert Love 
12323a3b42bfSRobert Love /**
12333a3b42bfSRobert Love  * fc_seq_send_last() - Send a sequence that is the last in the exchange
12343a3b42bfSRobert Love  * @sp:	     The sequence that is to be sent
12353a3b42bfSRobert Love  * @fp:	     The frame that will be sent on the sequence
12363a3b42bfSRobert Love  * @rctl:    The R_CTL information to be sent
12373a3b42bfSRobert Love  * @fh_type: The frame header type
123842e9a92fSRobert Love  */
123942e9a92fSRobert Love static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
124042e9a92fSRobert Love 			     enum fc_rctl rctl, enum fc_fh_type fh_type)
124142e9a92fSRobert Love {
124242e9a92fSRobert Love 	u32 f_ctl;
124342e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
124442e9a92fSRobert Love 
124542e9a92fSRobert Love 	f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
124642e9a92fSRobert Love 	f_ctl |= ep->f_ctl;
124742e9a92fSRobert Love 	fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
1248fb00cc23SNeil Horman 	fc_seq_send_locked(ep->lp, sp, fp);
124942e9a92fSRobert Love }
125042e9a92fSRobert Love 
12513a3b42bfSRobert Love /**
12523a3b42bfSRobert Love  * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
12533a3b42bfSRobert Love  * @sp:	   The sequence to send the ACK on
12543a3b42bfSRobert Love  * @rx_fp: The received frame that is being acknoledged
12553a3b42bfSRobert Love  *
125642e9a92fSRobert Love  * Send ACK_1 (or equiv.) indicating we received something.
125742e9a92fSRobert Love  */
125842e9a92fSRobert Love static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
125942e9a92fSRobert Love {
126042e9a92fSRobert Love 	struct fc_frame *fp;
126142e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
126242e9a92fSRobert Love 	struct fc_frame_header *fh;
126342e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
12643a3b42bfSRobert Love 	struct fc_lport *lport = ep->lp;
126542e9a92fSRobert Love 	unsigned int f_ctl;
126642e9a92fSRobert Love 
126742e9a92fSRobert Love 	/*
126842e9a92fSRobert Love 	 * Don't send ACKs for class 3.
126942e9a92fSRobert Love 	 */
127042e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(rx_fp))) {
12713a3b42bfSRobert Love 		fp = fc_frame_alloc(lport, 0);
127257d3ec7eSHannes Reinecke 		if (!fp) {
127357d3ec7eSHannes Reinecke 			FC_EXCH_DBG(ep, "Drop ACK request, out of memory\n");
127442e9a92fSRobert Love 			return;
127557d3ec7eSHannes Reinecke 		}
127642e9a92fSRobert Love 
127742e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
127842e9a92fSRobert Love 		fh->fh_r_ctl = FC_RCTL_ACK_1;
127942e9a92fSRobert Love 		fh->fh_type = FC_TYPE_BLS;
128042e9a92fSRobert Love 
128142e9a92fSRobert Love 		/*
128242e9a92fSRobert Love 		 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
128342e9a92fSRobert Love 		 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
128442e9a92fSRobert Love 		 * Bits 9-8 are meaningful (retransmitted or unidirectional).
128542e9a92fSRobert Love 		 * Last ACK uses bits 7-6 (continue sequence),
128642e9a92fSRobert Love 		 * bits 5-4 are meaningful (what kind of ACK to use).
128742e9a92fSRobert Love 		 */
128842e9a92fSRobert Love 		rx_fh = fc_frame_header_get(rx_fp);
128942e9a92fSRobert Love 		f_ctl = ntoh24(rx_fh->fh_f_ctl);
129042e9a92fSRobert Love 		f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
129142e9a92fSRobert Love 			FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
129242e9a92fSRobert Love 			FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
129342e9a92fSRobert Love 			FC_FC_RETX_SEQ | FC_FC_UNI_TX;
129442e9a92fSRobert Love 		f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
129542e9a92fSRobert Love 		hton24(fh->fh_f_ctl, f_ctl);
129642e9a92fSRobert Love 
129742e9a92fSRobert Love 		fc_exch_setup_hdr(ep, fp, f_ctl);
129842e9a92fSRobert Love 		fh->fh_seq_id = rx_fh->fh_seq_id;
129942e9a92fSRobert Love 		fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
130042e9a92fSRobert Love 		fh->fh_parm_offset = htonl(1);	/* ack single frame */
130142e9a92fSRobert Love 
130242e9a92fSRobert Love 		fr_sof(fp) = fr_sof(rx_fp);
130342e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
130442e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_T;
130542e9a92fSRobert Love 		else
130642e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
130742e9a92fSRobert Love 
13083a3b42bfSRobert Love 		lport->tt.frame_send(lport, fp);
130942e9a92fSRobert Love 	}
131042e9a92fSRobert Love }
131142e9a92fSRobert Love 
13123a3b42bfSRobert Love /**
13133a3b42bfSRobert Love  * fc_exch_send_ba_rjt() - Send BLS Reject
13143a3b42bfSRobert Love  * @rx_fp:  The frame being rejected
13153a3b42bfSRobert Love  * @reason: The reason the frame is being rejected
131625985edcSLucas De Marchi  * @explan: The explanation for the rejection
13173a3b42bfSRobert Love  *
131842e9a92fSRobert Love  * This is for rejecting BA_ABTS only.
131942e9a92fSRobert Love  */
1320b2ab99c9SRobert Love static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1321b2ab99c9SRobert Love 				enum fc_ba_rjt_reason reason,
132242e9a92fSRobert Love 				enum fc_ba_rjt_explan explan)
132342e9a92fSRobert Love {
132442e9a92fSRobert Love 	struct fc_frame *fp;
132542e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
132642e9a92fSRobert Love 	struct fc_frame_header *fh;
132742e9a92fSRobert Love 	struct fc_ba_rjt *rp;
132857d3ec7eSHannes Reinecke 	struct fc_seq *sp;
13293a3b42bfSRobert Love 	struct fc_lport *lport;
133042e9a92fSRobert Love 	unsigned int f_ctl;
133142e9a92fSRobert Love 
13323a3b42bfSRobert Love 	lport = fr_dev(rx_fp);
133357d3ec7eSHannes Reinecke 	sp = fr_seq(rx_fp);
13343a3b42bfSRobert Love 	fp = fc_frame_alloc(lport, sizeof(*rp));
133557d3ec7eSHannes Reinecke 	if (!fp) {
133657d3ec7eSHannes Reinecke 		FC_EXCH_DBG(fc_seq_exch(sp),
133757d3ec7eSHannes Reinecke 			     "Drop BA_RJT request, out of memory\n");
133842e9a92fSRobert Love 		return;
133957d3ec7eSHannes Reinecke 	}
134042e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
134142e9a92fSRobert Love 	rx_fh = fc_frame_header_get(rx_fp);
134242e9a92fSRobert Love 
134342e9a92fSRobert Love 	memset(fh, 0, sizeof(*fh) + sizeof(*rp));
134442e9a92fSRobert Love 
134542e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
134642e9a92fSRobert Love 	rp->br_reason = reason;
134742e9a92fSRobert Love 	rp->br_explan = explan;
134842e9a92fSRobert Love 
134942e9a92fSRobert Love 	/*
135042e9a92fSRobert Love 	 * seq_id, cs_ctl, df_ctl and param/offset are zero.
135142e9a92fSRobert Love 	 */
135242e9a92fSRobert Love 	memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
135342e9a92fSRobert Love 	memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
13541d490ce3SJoe Eykholt 	fh->fh_ox_id = rx_fh->fh_ox_id;
13551d490ce3SJoe Eykholt 	fh->fh_rx_id = rx_fh->fh_rx_id;
135642e9a92fSRobert Love 	fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
135742e9a92fSRobert Love 	fh->fh_r_ctl = FC_RCTL_BA_RJT;
135842e9a92fSRobert Love 	fh->fh_type = FC_TYPE_BLS;
135942e9a92fSRobert Love 
136042e9a92fSRobert Love 	/*
136142e9a92fSRobert Love 	 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
136242e9a92fSRobert Love 	 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
136342e9a92fSRobert Love 	 * Bits 9-8 are meaningful (retransmitted or unidirectional).
136442e9a92fSRobert Love 	 * Last ACK uses bits 7-6 (continue sequence),
136542e9a92fSRobert Love 	 * bits 5-4 are meaningful (what kind of ACK to use).
136642e9a92fSRobert Love 	 * Always set LAST_SEQ, END_SEQ.
136742e9a92fSRobert Love 	 */
136842e9a92fSRobert Love 	f_ctl = ntoh24(rx_fh->fh_f_ctl);
136942e9a92fSRobert Love 	f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
137042e9a92fSRobert Love 		FC_FC_END_CONN | FC_FC_SEQ_INIT |
137142e9a92fSRobert Love 		FC_FC_RETX_SEQ | FC_FC_UNI_TX;
137242e9a92fSRobert Love 	f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
137342e9a92fSRobert Love 	f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
137442e9a92fSRobert Love 	f_ctl &= ~FC_FC_FIRST_SEQ;
137542e9a92fSRobert Love 	hton24(fh->fh_f_ctl, f_ctl);
137642e9a92fSRobert Love 
137742e9a92fSRobert Love 	fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
137842e9a92fSRobert Love 	fr_eof(fp) = FC_EOF_T;
137942e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(fp)))
138042e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
138142e9a92fSRobert Love 
13823a3b42bfSRobert Love 	lport->tt.frame_send(lport, fp);
138342e9a92fSRobert Love }
138442e9a92fSRobert Love 
13853a3b42bfSRobert Love /**
13863a3b42bfSRobert Love  * fc_exch_recv_abts() - Handle an incoming ABTS
13873a3b42bfSRobert Love  * @ep:	   The exchange the abort was on
13883a3b42bfSRobert Love  * @rx_fp: The ABTS frame
13893a3b42bfSRobert Love  *
13903a3b42bfSRobert Love  * This would be for target mode usually, but could be due to lost
13913a3b42bfSRobert Love  * FCP transfer ready, confirm or RRQ. We always handle this as an
13923a3b42bfSRobert Love  * exchange abort, ignoring the parameter.
139342e9a92fSRobert Love  */
139442e9a92fSRobert Love static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
139542e9a92fSRobert Love {
139642e9a92fSRobert Love 	struct fc_frame *fp;
139742e9a92fSRobert Love 	struct fc_ba_acc *ap;
139842e9a92fSRobert Love 	struct fc_frame_header *fh;
139942e9a92fSRobert Love 	struct fc_seq *sp;
140042e9a92fSRobert Love 
140142e9a92fSRobert Love 	if (!ep)
140242e9a92fSRobert Love 		goto reject;
1403f95b35cfSBart Van Assche 
140457d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "exch: ABTS received\n");
1405f95b35cfSBart Van Assche 	fp = fc_frame_alloc(ep->lp, sizeof(*ap));
140657d3ec7eSHannes Reinecke 	if (!fp) {
140757d3ec7eSHannes Reinecke 		FC_EXCH_DBG(ep, "Drop ABTS request, out of memory\n");
1408f95b35cfSBart Van Assche 		goto free;
140957d3ec7eSHannes Reinecke 	}
1410f95b35cfSBart Van Assche 
141142e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
141242e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
141342e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
141457d3ec7eSHannes Reinecke 		FC_EXCH_DBG(ep, "exch: ABTS rejected, exchange complete\n");
1415f95b35cfSBart Van Assche 		fc_frame_free(fp);
141642e9a92fSRobert Love 		goto reject;
141742e9a92fSRobert Love 	}
1418cae7b6ddSBart Van Assche 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
1419cae7b6ddSBart Van Assche 		ep->esb_stat |= ESB_ST_REC_QUAL;
142042e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for REC_QUAL */
1421cae7b6ddSBart Van Assche 	}
142242e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
142342e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
142442e9a92fSRobert Love 	ap = fc_frame_payload_get(fp, sizeof(*ap));
142542e9a92fSRobert Love 	memset(ap, 0, sizeof(*ap));
142642e9a92fSRobert Love 	sp = &ep->seq;
142742e9a92fSRobert Love 	ap->ba_high_seq_cnt = htons(0xffff);
142842e9a92fSRobert Love 	if (sp->ssb_stat & SSB_ST_RESP) {
142942e9a92fSRobert Love 		ap->ba_seq_id = sp->id;
143042e9a92fSRobert Love 		ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
143142e9a92fSRobert Love 		ap->ba_high_seq_cnt = fh->fh_seq_cnt;
143242e9a92fSRobert Love 		ap->ba_low_seq_cnt = htons(sp->cnt);
143342e9a92fSRobert Love 	}
1434a7e84f2bSVasu Dev 	sp = fc_seq_start_next_locked(sp);
143542e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1436cae7b6ddSBart Van Assche 	ep->esb_stat |= ESB_ST_ABNORMAL;
1437fb00cc23SNeil Horman 	spin_unlock_bh(&ep->ex_lock);
1438f95b35cfSBart Van Assche 
1439f95b35cfSBart Van Assche free:
144042e9a92fSRobert Love 	fc_frame_free(rx_fp);
144142e9a92fSRobert Love 	return;
144242e9a92fSRobert Love 
144342e9a92fSRobert Love reject:
144442e9a92fSRobert Love 	fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1445f95b35cfSBart Van Assche 	goto free;
144642e9a92fSRobert Love }
144742e9a92fSRobert Love 
14483a3b42bfSRobert Love /**
1449239e8104SJoe Eykholt  * fc_seq_assign() - Assign exchange and sequence for incoming request
1450239e8104SJoe Eykholt  * @lport: The local port that received the request
1451239e8104SJoe Eykholt  * @fp:    The request frame
1452239e8104SJoe Eykholt  *
1453239e8104SJoe Eykholt  * On success, the sequence pointer will be returned and also in fr_seq(@fp).
145462bdb645SJoe Eykholt  * A reference will be held on the exchange/sequence for the caller, which
145562bdb645SJoe Eykholt  * must call fc_seq_release().
1456239e8104SJoe Eykholt  */
1457239e8104SJoe Eykholt static struct fc_seq *fc_seq_assign(struct fc_lport *lport, struct fc_frame *fp)
1458239e8104SJoe Eykholt {
1459239e8104SJoe Eykholt 	struct fc_exch_mgr_anchor *ema;
1460239e8104SJoe Eykholt 
1461239e8104SJoe Eykholt 	WARN_ON(lport != fr_dev(fp));
1462239e8104SJoe Eykholt 	WARN_ON(fr_seq(fp));
1463239e8104SJoe Eykholt 	fr_seq(fp) = NULL;
1464239e8104SJoe Eykholt 
1465239e8104SJoe Eykholt 	list_for_each_entry(ema, &lport->ema_list, ema_list)
1466239e8104SJoe Eykholt 		if ((!ema->match || ema->match(fp)) &&
1467530994d6SHillf Danton 		    fc_seq_lookup_recip(lport, ema->mp, fp) == FC_RJT_NONE)
1468239e8104SJoe Eykholt 			break;
1469239e8104SJoe Eykholt 	return fr_seq(fp);
1470239e8104SJoe Eykholt }
1471239e8104SJoe Eykholt 
1472239e8104SJoe Eykholt /**
147362bdb645SJoe Eykholt  * fc_seq_release() - Release the hold
147462bdb645SJoe Eykholt  * @sp:    The sequence.
147562bdb645SJoe Eykholt  */
147662bdb645SJoe Eykholt static void fc_seq_release(struct fc_seq *sp)
147762bdb645SJoe Eykholt {
147862bdb645SJoe Eykholt 	fc_exch_release(fc_seq_exch(sp));
147962bdb645SJoe Eykholt }
148062bdb645SJoe Eykholt 
148162bdb645SJoe Eykholt /**
148292261156SJoe Eykholt  * fc_exch_recv_req() - Handler for an incoming request
14833a3b42bfSRobert Love  * @lport: The local port that received the request
14843a3b42bfSRobert Love  * @mp:	   The EM that the exchange is on
14853a3b42bfSRobert Love  * @fp:	   The request frame
148692261156SJoe Eykholt  *
148792261156SJoe Eykholt  * This is used when the other end is originating the exchange
148892261156SJoe Eykholt  * and the sequence.
148942e9a92fSRobert Love  */
14903a3b42bfSRobert Love static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
149142e9a92fSRobert Love 			     struct fc_frame *fp)
149242e9a92fSRobert Love {
149342e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
149442e9a92fSRobert Love 	struct fc_seq *sp = NULL;
149542e9a92fSRobert Love 	struct fc_exch *ep = NULL;
149642e9a92fSRobert Love 	enum fc_pf_rjt_reason reject;
149742e9a92fSRobert Love 
1498174e1ebfSChris Leech 	/* We can have the wrong fc_lport at this point with NPIV, which is a
1499174e1ebfSChris Leech 	 * problem now that we know a new exchange needs to be allocated
1500174e1ebfSChris Leech 	 */
15013a3b42bfSRobert Love 	lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
15023a3b42bfSRobert Love 	if (!lport) {
1503174e1ebfSChris Leech 		fc_frame_free(fp);
1504174e1ebfSChris Leech 		return;
1505174e1ebfSChris Leech 	}
150692261156SJoe Eykholt 	fr_dev(fp) = lport;
1507174e1ebfSChris Leech 
150892261156SJoe Eykholt 	BUG_ON(fr_seq(fp));		/* XXX remove later */
150992261156SJoe Eykholt 
151092261156SJoe Eykholt 	/*
151192261156SJoe Eykholt 	 * If the RX_ID is 0xffff, don't allocate an exchange.
151292261156SJoe Eykholt 	 * The upper-level protocol may request one later, if needed.
151392261156SJoe Eykholt 	 */
151492261156SJoe Eykholt 	if (fh->fh_rx_id == htons(FC_XID_UNKNOWN))
151592261156SJoe Eykholt 		return lport->tt.lport_recv(lport, fp);
151692261156SJoe Eykholt 
15173a3b42bfSRobert Love 	reject = fc_seq_lookup_recip(lport, mp, fp);
151842e9a92fSRobert Love 	if (reject == FC_RJT_NONE) {
151942e9a92fSRobert Love 		sp = fr_seq(fp);	/* sequence will be held */
152042e9a92fSRobert Love 		ep = fc_seq_exch(sp);
152142e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
1522f60e12e9SJoe Eykholt 		ep->encaps = fr_encaps(fp);
152342e9a92fSRobert Love 
152442e9a92fSRobert Love 		/*
152542e9a92fSRobert Love 		 * Call the receive function.
152642e9a92fSRobert Love 		 *
152742e9a92fSRobert Love 		 * The receive function may allocate a new sequence
152842e9a92fSRobert Love 		 * over the old one, so we shouldn't change the
152942e9a92fSRobert Love 		 * sequence after this.
153042e9a92fSRobert Love 		 *
153142e9a92fSRobert Love 		 * The frame will be freed by the receive function.
153242e9a92fSRobert Love 		 * If new exch resp handler is valid then call that
153342e9a92fSRobert Love 		 * first.
153442e9a92fSRobert Love 		 */
15357030fd62SBart Van Assche 		if (!fc_invoke_resp(ep, sp, fp))
153692261156SJoe Eykholt 			lport->tt.lport_recv(lport, fp);
153742e9a92fSRobert Love 		fc_exch_release(ep);	/* release from lookup */
153842e9a92fSRobert Love 	} else {
15393a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
15403a3b42bfSRobert Love 			     reject);
154142e9a92fSRobert Love 		fc_frame_free(fp);
154242e9a92fSRobert Love 	}
154342e9a92fSRobert Love }
154442e9a92fSRobert Love 
15453a3b42bfSRobert Love /**
15463a3b42bfSRobert Love  * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
15473a3b42bfSRobert Love  *			     end is the originator of the sequence that is a
15483a3b42bfSRobert Love  *			     response to our initial exchange
15493a3b42bfSRobert Love  * @mp: The EM that the exchange is on
15503a3b42bfSRobert Love  * @fp: The response frame
155142e9a92fSRobert Love  */
155242e9a92fSRobert Love static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
155342e9a92fSRobert Love {
155442e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
155542e9a92fSRobert Love 	struct fc_seq *sp;
155642e9a92fSRobert Love 	struct fc_exch *ep;
155742e9a92fSRobert Love 	enum fc_sof sof;
155842e9a92fSRobert Love 	u32 f_ctl;
155942e9a92fSRobert Love 	int rc;
156042e9a92fSRobert Love 
156142e9a92fSRobert Love 	ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
156242e9a92fSRobert Love 	if (!ep) {
156342e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
156442e9a92fSRobert Love 		goto out;
156542e9a92fSRobert Love 	}
156630121d14SSteve Ma 	if (ep->esb_stat & ESB_ST_COMPLETE) {
156730121d14SSteve Ma 		atomic_inc(&mp->stats.xid_not_found);
15688236554aSHillf Danton 		goto rel;
156930121d14SSteve Ma 	}
157042e9a92fSRobert Love 	if (ep->rxid == FC_XID_UNKNOWN)
157142e9a92fSRobert Love 		ep->rxid = ntohs(fh->fh_rx_id);
157242e9a92fSRobert Love 	if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
157342e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
157442e9a92fSRobert Love 		goto rel;
157542e9a92fSRobert Love 	}
157642e9a92fSRobert Love 	if (ep->did != ntoh24(fh->fh_s_id) &&
157742e9a92fSRobert Love 	    ep->did != FC_FID_FLOGI) {
157842e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
157942e9a92fSRobert Love 		goto rel;
158042e9a92fSRobert Love 	}
158142e9a92fSRobert Love 	sof = fr_sof(fp);
158242e9a92fSRobert Love 	sp = &ep->seq;
1583b3667f91SJoe Eykholt 	if (fc_sof_is_init(sof)) {
1584a104c844SVasu Dev 		sp->ssb_stat |= SSB_ST_RESP;
1585b3667f91SJoe Eykholt 		sp->id = fh->fh_seq_id;
1586b3667f91SJoe Eykholt 	} else if (sp->id != fh->fh_seq_id) {
158742e9a92fSRobert Love 		atomic_inc(&mp->stats.seq_not_found);
158842e9a92fSRobert Love 		goto rel;
158942e9a92fSRobert Love 	}
1590a104c844SVasu Dev 
159142e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
159242e9a92fSRobert Love 	fr_seq(fp) = sp;
15935d73bea2SBart Van Assche 
15945d73bea2SBart Van Assche 	spin_lock_bh(&ep->ex_lock);
159542e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
159642e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
15975d73bea2SBart Van Assche 	spin_unlock_bh(&ep->ex_lock);
159842e9a92fSRobert Love 
159942e9a92fSRobert Love 	if (fc_sof_needs_ack(sof))
160042e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
160142e9a92fSRobert Love 
160242e9a92fSRobert Love 	if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
160342e9a92fSRobert Love 	    (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
160442e9a92fSRobert Love 	    (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
160542e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
160642e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
160742e9a92fSRobert Love 		WARN_ON(fc_seq_exch(sp) != ep);
160842e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
160942e9a92fSRobert Love 		if (!rc)
1610b2f0091fSVasu Dev 			fc_exch_delete(ep);
161142e9a92fSRobert Love 	}
161242e9a92fSRobert Love 
161342e9a92fSRobert Love 	/*
161442e9a92fSRobert Love 	 * Call the receive function.
161542e9a92fSRobert Love 	 * The sequence is held (has a refcnt) for us,
161642e9a92fSRobert Love 	 * but not for the receive function.
161742e9a92fSRobert Love 	 *
161842e9a92fSRobert Love 	 * The receive function may allocate a new sequence
161942e9a92fSRobert Love 	 * over the old one, so we shouldn't change the
162042e9a92fSRobert Love 	 * sequence after this.
162142e9a92fSRobert Love 	 *
162242e9a92fSRobert Love 	 * The frame will be freed by the receive function.
162342e9a92fSRobert Love 	 * If new exch resp handler is valid then call that
162442e9a92fSRobert Love 	 * first.
162542e9a92fSRobert Love 	 */
1626f6979adeSBart Van Assche 	if (!fc_invoke_resp(ep, sp, fp))
1627f6979adeSBart Van Assche 		fc_frame_free(fp);
16287030fd62SBart Van Assche 
162942e9a92fSRobert Love 	fc_exch_release(ep);
163042e9a92fSRobert Love 	return;
163142e9a92fSRobert Love rel:
163242e9a92fSRobert Love 	fc_exch_release(ep);
163342e9a92fSRobert Love out:
163442e9a92fSRobert Love 	fc_frame_free(fp);
163542e9a92fSRobert Love }
163642e9a92fSRobert Love 
16373a3b42bfSRobert Love /**
16383a3b42bfSRobert Love  * fc_exch_recv_resp() - Handler for a sequence where other end is
16393a3b42bfSRobert Love  *			 responding to our sequence
16403a3b42bfSRobert Love  * @mp: The EM that the exchange is on
16413a3b42bfSRobert Love  * @fp: The response frame
164242e9a92fSRobert Love  */
164342e9a92fSRobert Love static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
164442e9a92fSRobert Love {
164542e9a92fSRobert Love 	struct fc_seq *sp;
164642e9a92fSRobert Love 
164742e9a92fSRobert Love 	sp = fc_seq_lookup_orig(mp, fp);	/* doesn't hold sequence */
1648d459b7eaSRobert Love 
1649d459b7eaSRobert Love 	if (!sp)
165042e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
1651d459b7eaSRobert Love 	else
165242e9a92fSRobert Love 		atomic_inc(&mp->stats.non_bls_resp);
1653d459b7eaSRobert Love 
165442e9a92fSRobert Love 	fc_frame_free(fp);
165542e9a92fSRobert Love }
165642e9a92fSRobert Love 
16573a3b42bfSRobert Love /**
16583a3b42bfSRobert Love  * fc_exch_abts_resp() - Handler for a response to an ABT
16593a3b42bfSRobert Love  * @ep: The exchange that the frame is on
16603a3b42bfSRobert Love  * @fp: The response frame
16613a3b42bfSRobert Love  *
16623a3b42bfSRobert Love  * This response would be to an ABTS cancelling an exchange or sequence.
16633a3b42bfSRobert Love  * The response can be either BA_ACC or BA_RJT
166442e9a92fSRobert Love  */
166542e9a92fSRobert Love static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
166642e9a92fSRobert Love {
166742e9a92fSRobert Love 	struct fc_frame_header *fh;
166842e9a92fSRobert Love 	struct fc_ba_acc *ap;
166942e9a92fSRobert Love 	struct fc_seq *sp;
167042e9a92fSRobert Love 	u16 low;
167142e9a92fSRobert Love 	u16 high;
167242e9a92fSRobert Love 	int rc = 1, has_rec = 0;
167342e9a92fSRobert Love 
167442e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
16757414705eSRobert Love 	FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
16767414705eSRobert Love 		    fc_exch_rctl_name(fh->fh_r_ctl));
167742e9a92fSRobert Love 
1678b29a4f30SVasu Dev 	if (cancel_delayed_work_sync(&ep->timeout_work)) {
16793a292605SRobert Love 		FC_EXCH_DBG(ep, "Exchange timer canceled due to ABTS response\n");
168042e9a92fSRobert Love 		fc_exch_release(ep);	/* release from pending timer hold */
1681b29a4f30SVasu Dev 	}
168242e9a92fSRobert Love 
168342e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
168442e9a92fSRobert Love 	switch (fh->fh_r_ctl) {
168542e9a92fSRobert Love 	case FC_RCTL_BA_ACC:
168642e9a92fSRobert Love 		ap = fc_frame_payload_get(fp, sizeof(*ap));
168742e9a92fSRobert Love 		if (!ap)
168842e9a92fSRobert Love 			break;
168942e9a92fSRobert Love 
169042e9a92fSRobert Love 		/*
169142e9a92fSRobert Love 		 * Decide whether to establish a Recovery Qualifier.
169242e9a92fSRobert Love 		 * We do this if there is a non-empty SEQ_CNT range and
169342e9a92fSRobert Love 		 * SEQ_ID is the same as the one we aborted.
169442e9a92fSRobert Love 		 */
169542e9a92fSRobert Love 		low = ntohs(ap->ba_low_seq_cnt);
169642e9a92fSRobert Love 		high = ntohs(ap->ba_high_seq_cnt);
169742e9a92fSRobert Love 		if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
169842e9a92fSRobert Love 		    (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
169942e9a92fSRobert Love 		     ap->ba_seq_id == ep->seq_id) && low != high) {
170042e9a92fSRobert Love 			ep->esb_stat |= ESB_ST_REC_QUAL;
170142e9a92fSRobert Love 			fc_exch_hold(ep);  /* hold for recovery qualifier */
170242e9a92fSRobert Love 			has_rec = 1;
170342e9a92fSRobert Love 		}
170442e9a92fSRobert Love 		break;
170542e9a92fSRobert Love 	case FC_RCTL_BA_RJT:
170642e9a92fSRobert Love 		break;
170742e9a92fSRobert Love 	default:
170842e9a92fSRobert Love 		break;
170942e9a92fSRobert Love 	}
171042e9a92fSRobert Love 
171142e9a92fSRobert Love 	/* do we need to do some other checks here. Can we reuse more of
171242e9a92fSRobert Love 	 * fc_exch_recv_seq_resp
171342e9a92fSRobert Love 	 */
171442e9a92fSRobert Love 	sp = &ep->seq;
171542e9a92fSRobert Love 	/*
171642e9a92fSRobert Love 	 * do we want to check END_SEQ as well as LAST_SEQ here?
171742e9a92fSRobert Love 	 */
171842e9a92fSRobert Love 	if (ep->fh_type != FC_TYPE_FCP &&
171942e9a92fSRobert Love 	    ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
172042e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
172142e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
17227030fd62SBart Van Assche 
17237030fd62SBart Van Assche 	fc_exch_hold(ep);
172442e9a92fSRobert Love 	if (!rc)
1725b2f0091fSVasu Dev 		fc_exch_delete(ep);
1726f6979adeSBart Van Assche 	if (!fc_invoke_resp(ep, sp, fp))
1727f6979adeSBart Van Assche 		fc_frame_free(fp);
172842e9a92fSRobert Love 	if (has_rec)
172942e9a92fSRobert Love 		fc_exch_timer_set(ep, ep->r_a_tov);
17307030fd62SBart Van Assche 	fc_exch_release(ep);
173142e9a92fSRobert Love }
173242e9a92fSRobert Love 
17333a3b42bfSRobert Love /**
17343a3b42bfSRobert Love  * fc_exch_recv_bls() - Handler for a BLS sequence
17353a3b42bfSRobert Love  * @mp: The EM that the exchange is on
17363a3b42bfSRobert Love  * @fp: The request frame
17373a3b42bfSRobert Love  *
17383a3b42bfSRobert Love  * The BLS frame is always a sequence initiated by the remote side.
173942e9a92fSRobert Love  * We may be either the originator or recipient of the exchange.
174042e9a92fSRobert Love  */
174142e9a92fSRobert Love static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
174242e9a92fSRobert Love {
174342e9a92fSRobert Love 	struct fc_frame_header *fh;
174442e9a92fSRobert Love 	struct fc_exch *ep;
174542e9a92fSRobert Love 	u32 f_ctl;
174642e9a92fSRobert Love 
174742e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
174842e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
174942e9a92fSRobert Love 	fr_seq(fp) = NULL;
175042e9a92fSRobert Love 
175142e9a92fSRobert Love 	ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
175242e9a92fSRobert Love 			  ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
175342e9a92fSRobert Love 	if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
175442e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
175542e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
175642e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
175742e9a92fSRobert Love 	}
175842e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_CTX) {
175942e9a92fSRobert Love 		/*
176042e9a92fSRobert Love 		 * A response to a sequence we initiated.
176142e9a92fSRobert Love 		 * This should only be ACKs for class 2 or F.
176242e9a92fSRobert Love 		 */
176342e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
176442e9a92fSRobert Love 		case FC_RCTL_ACK_1:
176542e9a92fSRobert Love 		case FC_RCTL_ACK_0:
176642e9a92fSRobert Love 			break;
176742e9a92fSRobert Love 		default:
1768d4042e9cSBhanu Prakash Gollapudi 			if (ep)
1769b20d9bfdSBart Van Assche 				FC_EXCH_DBG(ep, "BLS rctl %x - %s received\n",
177042e9a92fSRobert Love 					    fh->fh_r_ctl,
177142e9a92fSRobert Love 					    fc_exch_rctl_name(fh->fh_r_ctl));
177242e9a92fSRobert Love 			break;
177342e9a92fSRobert Love 		}
177442e9a92fSRobert Love 		fc_frame_free(fp);
177542e9a92fSRobert Love 	} else {
177642e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
177742e9a92fSRobert Love 		case FC_RCTL_BA_RJT:
177842e9a92fSRobert Love 		case FC_RCTL_BA_ACC:
177942e9a92fSRobert Love 			if (ep)
178042e9a92fSRobert Love 				fc_exch_abts_resp(ep, fp);
178142e9a92fSRobert Love 			else
178242e9a92fSRobert Love 				fc_frame_free(fp);
178342e9a92fSRobert Love 			break;
178442e9a92fSRobert Love 		case FC_RCTL_BA_ABTS:
178542e9a92fSRobert Love 			fc_exch_recv_abts(ep, fp);
178642e9a92fSRobert Love 			break;
178742e9a92fSRobert Love 		default:			/* ignore junk */
178842e9a92fSRobert Love 			fc_frame_free(fp);
178942e9a92fSRobert Love 			break;
179042e9a92fSRobert Love 		}
179142e9a92fSRobert Love 	}
179242e9a92fSRobert Love 	if (ep)
179342e9a92fSRobert Love 		fc_exch_release(ep);	/* release hold taken by fc_exch_find */
179442e9a92fSRobert Love }
179542e9a92fSRobert Love 
17963a3b42bfSRobert Love /**
17973a3b42bfSRobert Love  * fc_seq_ls_acc() - Accept sequence with LS_ACC
179892261156SJoe Eykholt  * @rx_fp: The received frame, not freed here.
17993a3b42bfSRobert Love  *
180042e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
180142e9a92fSRobert Love  * originator will repeat the sequence.
180242e9a92fSRobert Love  */
180392261156SJoe Eykholt static void fc_seq_ls_acc(struct fc_frame *rx_fp)
180442e9a92fSRobert Love {
180592261156SJoe Eykholt 	struct fc_lport *lport;
180642e9a92fSRobert Love 	struct fc_els_ls_acc *acc;
180742e9a92fSRobert Love 	struct fc_frame *fp;
180857d3ec7eSHannes Reinecke 	struct fc_seq *sp;
180942e9a92fSRobert Love 
181092261156SJoe Eykholt 	lport = fr_dev(rx_fp);
181157d3ec7eSHannes Reinecke 	sp = fr_seq(rx_fp);
181292261156SJoe Eykholt 	fp = fc_frame_alloc(lport, sizeof(*acc));
181357d3ec7eSHannes Reinecke 	if (!fp) {
181457d3ec7eSHannes Reinecke 		FC_EXCH_DBG(fc_seq_exch(sp),
181557d3ec7eSHannes Reinecke 			    "exch: drop LS_ACC, out of memory\n");
181692261156SJoe Eykholt 		return;
181757d3ec7eSHannes Reinecke 	}
181842e9a92fSRobert Love 	acc = fc_frame_payload_get(fp, sizeof(*acc));
181942e9a92fSRobert Love 	memset(acc, 0, sizeof(*acc));
182042e9a92fSRobert Love 	acc->la_cmd = ELS_LS_ACC;
182192261156SJoe Eykholt 	fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
182292261156SJoe Eykholt 	lport->tt.frame_send(lport, fp);
182342e9a92fSRobert Love }
182442e9a92fSRobert Love 
18253a3b42bfSRobert Love /**
18263a3b42bfSRobert Love  * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
182792261156SJoe Eykholt  * @rx_fp: The received frame, not freed here.
18283a3b42bfSRobert Love  * @reason: The reason the sequence is being rejected
182992261156SJoe Eykholt  * @explan: The explanation for the rejection
18303a3b42bfSRobert Love  *
183142e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
183242e9a92fSRobert Love  * originator will repeat the sequence.
183342e9a92fSRobert Love  */
183492261156SJoe Eykholt static void fc_seq_ls_rjt(struct fc_frame *rx_fp, enum fc_els_rjt_reason reason,
183542e9a92fSRobert Love 			  enum fc_els_rjt_explan explan)
183642e9a92fSRobert Love {
183792261156SJoe Eykholt 	struct fc_lport *lport;
183842e9a92fSRobert Love 	struct fc_els_ls_rjt *rjt;
183942e9a92fSRobert Love 	struct fc_frame *fp;
184057d3ec7eSHannes Reinecke 	struct fc_seq *sp;
184142e9a92fSRobert Love 
184292261156SJoe Eykholt 	lport = fr_dev(rx_fp);
184357d3ec7eSHannes Reinecke 	sp = fr_seq(rx_fp);
184492261156SJoe Eykholt 	fp = fc_frame_alloc(lport, sizeof(*rjt));
184557d3ec7eSHannes Reinecke 	if (!fp) {
184657d3ec7eSHannes Reinecke 		FC_EXCH_DBG(fc_seq_exch(sp),
184757d3ec7eSHannes Reinecke 			    "exch: drop LS_ACC, out of memory\n");
184892261156SJoe Eykholt 		return;
184957d3ec7eSHannes Reinecke 	}
185042e9a92fSRobert Love 	rjt = fc_frame_payload_get(fp, sizeof(*rjt));
185142e9a92fSRobert Love 	memset(rjt, 0, sizeof(*rjt));
185242e9a92fSRobert Love 	rjt->er_cmd = ELS_LS_RJT;
185342e9a92fSRobert Love 	rjt->er_reason = reason;
185442e9a92fSRobert Love 	rjt->er_explan = explan;
185592261156SJoe Eykholt 	fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
185692261156SJoe Eykholt 	lport->tt.frame_send(lport, fp);
185742e9a92fSRobert Love }
185842e9a92fSRobert Love 
18593a3b42bfSRobert Love /**
18603a3b42bfSRobert Love  * fc_exch_reset() - Reset an exchange
18613a3b42bfSRobert Love  * @ep: The exchange to be reset
18627030fd62SBart Van Assche  *
18637030fd62SBart Van Assche  * Note: May sleep if invoked from outside a response handler.
18643a3b42bfSRobert Love  */
186542e9a92fSRobert Love static void fc_exch_reset(struct fc_exch *ep)
186642e9a92fSRobert Love {
186742e9a92fSRobert Love 	struct fc_seq *sp;
186842e9a92fSRobert Love 	int rc = 1;
186942e9a92fSRobert Love 
187042e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
187142e9a92fSRobert Love 	ep->state |= FC_EX_RST_CLEANUP;
1872b29a4f30SVasu Dev 	fc_exch_timer_cancel(ep);
187342e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL)
187442e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec_qual */
187542e9a92fSRobert Love 	ep->esb_stat &= ~ESB_ST_REC_QUAL;
187642e9a92fSRobert Love 	sp = &ep->seq;
187742e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
187842e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
18797030fd62SBart Van Assche 
18807030fd62SBart Van Assche 	fc_exch_hold(ep);
18817030fd62SBart Van Assche 
188242e9a92fSRobert Love 	if (!rc)
1883b2f0091fSVasu Dev 		fc_exch_delete(ep);
188442e9a92fSRobert Love 
18857030fd62SBart Van Assche 	fc_invoke_resp(ep, sp, ERR_PTR(-FC_EX_CLOSED));
18867030fd62SBart Van Assche 	fc_seq_set_resp(sp, NULL, ep->arg);
18877030fd62SBart Van Assche 	fc_exch_release(ep);
188842e9a92fSRobert Love }
188942e9a92fSRobert Love 
1890b2f0091fSVasu Dev /**
18913a3b42bfSRobert Love  * fc_exch_pool_reset() - Reset a per cpu exchange pool
18923a3b42bfSRobert Love  * @lport: The local port that the exchange pool is on
18933a3b42bfSRobert Love  * @pool:  The exchange pool to be reset
18943a3b42bfSRobert Love  * @sid:   The source ID
18953a3b42bfSRobert Love  * @did:   The destination ID
1896b2f0091fSVasu Dev  *
18973a3b42bfSRobert Love  * Resets a per cpu exches pool, releasing all of its sequences
18983a3b42bfSRobert Love  * and exchanges. If sid is non-zero then reset only exchanges
18993a3b42bfSRobert Love  * we sourced from the local port's FID. If did is non-zero then
19003a3b42bfSRobert Love  * only reset exchanges destined for the local port's FID.
190142e9a92fSRobert Love  */
1902b2f0091fSVasu Dev static void fc_exch_pool_reset(struct fc_lport *lport,
1903b2f0091fSVasu Dev 			       struct fc_exch_pool *pool,
1904b2f0091fSVasu Dev 			       u32 sid, u32 did)
190542e9a92fSRobert Love {
190642e9a92fSRobert Love 	struct fc_exch *ep;
190742e9a92fSRobert Love 	struct fc_exch *next;
190842e9a92fSRobert Love 
1909b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
191042e9a92fSRobert Love restart:
1911b2f0091fSVasu Dev 	list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
1912b2f0091fSVasu Dev 		if ((lport == ep->lp) &&
191352ff878cSVasu Dev 		    (sid == 0 || sid == ep->sid) &&
191442e9a92fSRobert Love 		    (did == 0 || did == ep->did)) {
191542e9a92fSRobert Love 			fc_exch_hold(ep);
1916b2f0091fSVasu Dev 			spin_unlock_bh(&pool->lock);
191742e9a92fSRobert Love 
191842e9a92fSRobert Love 			fc_exch_reset(ep);
191942e9a92fSRobert Love 
192042e9a92fSRobert Love 			fc_exch_release(ep);
1921b2f0091fSVasu Dev 			spin_lock_bh(&pool->lock);
192242e9a92fSRobert Love 
192342e9a92fSRobert Love 			/*
192452ff878cSVasu Dev 			 * must restart loop incase while lock
192552ff878cSVasu Dev 			 * was down multiple eps were released.
192642e9a92fSRobert Love 			 */
192742e9a92fSRobert Love 			goto restart;
192842e9a92fSRobert Love 		}
192942e9a92fSRobert Love 	}
1930b6e3c840SVasu Dev 	pool->next_index = 0;
1931b6e3c840SVasu Dev 	pool->left = FC_XID_UNKNOWN;
1932b6e3c840SVasu Dev 	pool->right = FC_XID_UNKNOWN;
1933b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
1934b2f0091fSVasu Dev }
1935b2f0091fSVasu Dev 
1936b2f0091fSVasu Dev /**
19373a3b42bfSRobert Love  * fc_exch_mgr_reset() - Reset all EMs of a local port
19383a3b42bfSRobert Love  * @lport: The local port whose EMs are to be reset
19393a3b42bfSRobert Love  * @sid:   The source ID
19403a3b42bfSRobert Love  * @did:   The destination ID
1941b2f0091fSVasu Dev  *
19423a3b42bfSRobert Love  * Reset all EMs associated with a given local port. Release all
19433a3b42bfSRobert Love  * sequences and exchanges. If sid is non-zero then reset only the
19443a3b42bfSRobert Love  * exchanges sent from the local port's FID. If did is non-zero then
19453a3b42bfSRobert Love  * reset only exchanges destined for the local port's FID.
1946b2f0091fSVasu Dev  */
1947b2f0091fSVasu Dev void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1948b2f0091fSVasu Dev {
1949b2f0091fSVasu Dev 	struct fc_exch_mgr_anchor *ema;
1950b2f0091fSVasu Dev 	unsigned int cpu;
1951b2f0091fSVasu Dev 
1952b2f0091fSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
1953b2f0091fSVasu Dev 		for_each_possible_cpu(cpu)
1954b2f0091fSVasu Dev 			fc_exch_pool_reset(lport,
1955b2f0091fSVasu Dev 					   per_cpu_ptr(ema->mp->pool, cpu),
1956b2f0091fSVasu Dev 					   sid, did);
195742e9a92fSRobert Love 	}
195852ff878cSVasu Dev }
195942e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_reset);
196042e9a92fSRobert Love 
19613a3b42bfSRobert Love /**
196292261156SJoe Eykholt  * fc_exch_lookup() - find an exchange
196392261156SJoe Eykholt  * @lport: The local port
196492261156SJoe Eykholt  * @xid: The exchange ID
196592261156SJoe Eykholt  *
196692261156SJoe Eykholt  * Returns exchange pointer with hold for caller, or NULL if not found.
196792261156SJoe Eykholt  */
196892261156SJoe Eykholt static struct fc_exch *fc_exch_lookup(struct fc_lport *lport, u32 xid)
196992261156SJoe Eykholt {
197092261156SJoe Eykholt 	struct fc_exch_mgr_anchor *ema;
197192261156SJoe Eykholt 
197292261156SJoe Eykholt 	list_for_each_entry(ema, &lport->ema_list, ema_list)
197392261156SJoe Eykholt 		if (ema->mp->min_xid <= xid && xid <= ema->mp->max_xid)
197492261156SJoe Eykholt 			return fc_exch_find(ema->mp, xid);
197592261156SJoe Eykholt 	return NULL;
197692261156SJoe Eykholt }
197792261156SJoe Eykholt 
197892261156SJoe Eykholt /**
19793a3b42bfSRobert Love  * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
198092261156SJoe Eykholt  * @rfp: The REC frame, not freed here.
19813a3b42bfSRobert Love  *
198242e9a92fSRobert Love  * Note that the requesting port may be different than the S_ID in the request.
198342e9a92fSRobert Love  */
198492261156SJoe Eykholt static void fc_exch_els_rec(struct fc_frame *rfp)
198542e9a92fSRobert Love {
198692261156SJoe Eykholt 	struct fc_lport *lport;
198742e9a92fSRobert Love 	struct fc_frame *fp;
198842e9a92fSRobert Love 	struct fc_exch *ep;
198942e9a92fSRobert Love 	struct fc_els_rec *rp;
199042e9a92fSRobert Love 	struct fc_els_rec_acc *acc;
199142e9a92fSRobert Love 	enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
199242e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
199342e9a92fSRobert Love 	u32 sid;
199442e9a92fSRobert Love 	u16 rxid;
199542e9a92fSRobert Love 	u16 oxid;
199642e9a92fSRobert Love 
199792261156SJoe Eykholt 	lport = fr_dev(rfp);
199842e9a92fSRobert Love 	rp = fc_frame_payload_get(rfp, sizeof(*rp));
199942e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
200042e9a92fSRobert Love 	if (!rp)
200142e9a92fSRobert Love 		goto reject;
200242e9a92fSRobert Love 	sid = ntoh24(rp->rec_s_id);
200342e9a92fSRobert Love 	rxid = ntohs(rp->rec_rx_id);
200442e9a92fSRobert Love 	oxid = ntohs(rp->rec_ox_id);
200542e9a92fSRobert Love 
200692261156SJoe Eykholt 	ep = fc_exch_lookup(lport,
200792261156SJoe Eykholt 			    sid == fc_host_port_id(lport->host) ? oxid : rxid);
200842e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
200957d3ec7eSHannes Reinecke 	if (!ep) {
201057d3ec7eSHannes Reinecke 		FC_LPORT_DBG(lport,
201157d3ec7eSHannes Reinecke 			     "REC request from %x: rxid %x oxid %x not found\n",
201257d3ec7eSHannes Reinecke 			     sid, rxid, oxid);
201342e9a92fSRobert Love 		goto reject;
201457d3ec7eSHannes Reinecke 	}
201557d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "REC request from %x: rxid %x oxid %x\n",
201657d3ec7eSHannes Reinecke 		    sid, rxid, oxid);
201792261156SJoe Eykholt 	if (ep->oid != sid || oxid != ep->oxid)
201892261156SJoe Eykholt 		goto rel;
201992261156SJoe Eykholt 	if (rxid != FC_XID_UNKNOWN && rxid != ep->rxid)
202092261156SJoe Eykholt 		goto rel;
202192261156SJoe Eykholt 	fp = fc_frame_alloc(lport, sizeof(*acc));
202257d3ec7eSHannes Reinecke 	if (!fp) {
202357d3ec7eSHannes Reinecke 		FC_EXCH_DBG(ep, "Drop REC request, out of memory\n");
202442e9a92fSRobert Love 		goto out;
202557d3ec7eSHannes Reinecke 	}
202692261156SJoe Eykholt 
202742e9a92fSRobert Love 	acc = fc_frame_payload_get(fp, sizeof(*acc));
202842e9a92fSRobert Love 	memset(acc, 0, sizeof(*acc));
202942e9a92fSRobert Love 	acc->reca_cmd = ELS_LS_ACC;
203042e9a92fSRobert Love 	acc->reca_ox_id = rp->rec_ox_id;
203142e9a92fSRobert Love 	memcpy(acc->reca_ofid, rp->rec_s_id, 3);
203242e9a92fSRobert Love 	acc->reca_rx_id = htons(ep->rxid);
203342e9a92fSRobert Love 	if (ep->sid == ep->oid)
203442e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->did);
203542e9a92fSRobert Love 	else
203642e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->sid);
203742e9a92fSRobert Love 	acc->reca_fc4value = htonl(ep->seq.rec_data);
203842e9a92fSRobert Love 	acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
203942e9a92fSRobert Love 						 ESB_ST_SEQ_INIT |
204042e9a92fSRobert Love 						 ESB_ST_COMPLETE));
204192261156SJoe Eykholt 	fc_fill_reply_hdr(fp, rfp, FC_RCTL_ELS_REP, 0);
204292261156SJoe Eykholt 	lport->tt.frame_send(lport, fp);
204342e9a92fSRobert Love out:
204442e9a92fSRobert Love 	fc_exch_release(ep);
204542e9a92fSRobert Love 	return;
204642e9a92fSRobert Love 
204742e9a92fSRobert Love rel:
204842e9a92fSRobert Love 	fc_exch_release(ep);
204942e9a92fSRobert Love reject:
205092261156SJoe Eykholt 	fc_seq_ls_rjt(rfp, reason, explan);
205142e9a92fSRobert Love }
205242e9a92fSRobert Love 
20533a3b42bfSRobert Love /**
20543a3b42bfSRobert Love  * fc_exch_rrq_resp() - Handler for RRQ responses
20553a3b42bfSRobert Love  * @sp:	 The sequence that the RRQ is on
20563a3b42bfSRobert Love  * @fp:	 The RRQ frame
20573a3b42bfSRobert Love  * @arg: The exchange that the RRQ is on
205842e9a92fSRobert Love  *
205942e9a92fSRobert Love  * TODO: fix error handler.
206042e9a92fSRobert Love  */
206142e9a92fSRobert Love static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
206242e9a92fSRobert Love {
206342e9a92fSRobert Love 	struct fc_exch *aborted_ep = arg;
206442e9a92fSRobert Love 	unsigned int op;
206542e9a92fSRobert Love 
206642e9a92fSRobert Love 	if (IS_ERR(fp)) {
206742e9a92fSRobert Love 		int err = PTR_ERR(fp);
206842e9a92fSRobert Love 
206978342da3SVasu Dev 		if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
207042e9a92fSRobert Love 			goto cleanup;
20717414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
20727414705eSRobert Love 			    "frame error %d\n", err);
207342e9a92fSRobert Love 		return;
207442e9a92fSRobert Love 	}
207542e9a92fSRobert Love 
207642e9a92fSRobert Love 	op = fc_frame_payload_op(fp);
207742e9a92fSRobert Love 	fc_frame_free(fp);
207842e9a92fSRobert Love 
207942e9a92fSRobert Love 	switch (op) {
208042e9a92fSRobert Love 	case ELS_LS_RJT:
2081b20d9bfdSBart Van Assche 		FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ\n");
208242e9a92fSRobert Love 		/* fall through */
208342e9a92fSRobert Love 	case ELS_LS_ACC:
208442e9a92fSRobert Love 		goto cleanup;
208542e9a92fSRobert Love 	default:
2086b20d9bfdSBart Van Assche 		FC_EXCH_DBG(aborted_ep, "unexpected response op %x for RRQ\n",
2087b20d9bfdSBart Van Assche 			    op);
208842e9a92fSRobert Love 		return;
208942e9a92fSRobert Love 	}
209042e9a92fSRobert Love 
209142e9a92fSRobert Love cleanup:
209242e9a92fSRobert Love 	fc_exch_done(&aborted_ep->seq);
209342e9a92fSRobert Love 	/* drop hold for rec qual */
209442e9a92fSRobert Love 	fc_exch_release(aborted_ep);
209542e9a92fSRobert Love }
209642e9a92fSRobert Love 
20971a7b75aeSRobert Love 
20981a7b75aeSRobert Love /**
20993a3b42bfSRobert Love  * fc_exch_seq_send() - Send a frame using a new exchange and sequence
21003a3b42bfSRobert Love  * @lport:	The local port to send the frame on
21013a3b42bfSRobert Love  * @fp:		The frame to be sent
21023a3b42bfSRobert Love  * @resp:	The response handler for this request
21033a3b42bfSRobert Love  * @destructor: The destructor for the exchange
21043a3b42bfSRobert Love  * @arg:	The argument to be passed to the response handler
21053a3b42bfSRobert Love  * @timer_msec: The timeout period for the exchange
21063a3b42bfSRobert Love  *
21073a3b42bfSRobert Love  * The frame pointer with some of the header's fields must be
21083a3b42bfSRobert Love  * filled before calling this routine, those fields are:
21093a3b42bfSRobert Love  *
21103a3b42bfSRobert Love  * - routing control
21113a3b42bfSRobert Love  * - FC port did
21123a3b42bfSRobert Love  * - FC port sid
21133a3b42bfSRobert Love  * - FC header type
21143a3b42bfSRobert Love  * - frame control
21153a3b42bfSRobert Love  * - parameter or relative offset
21161a7b75aeSRobert Love  */
21173a3b42bfSRobert Love static struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
21181a7b75aeSRobert Love 				       struct fc_frame *fp,
21191a7b75aeSRobert Love 				       void (*resp)(struct fc_seq *,
21201a7b75aeSRobert Love 						    struct fc_frame *fp,
21211a7b75aeSRobert Love 						    void *arg),
21221a7b75aeSRobert Love 				       void (*destructor)(struct fc_seq *,
21231a7b75aeSRobert Love 							  void *),
21241a7b75aeSRobert Love 				       void *arg, u32 timer_msec)
21251a7b75aeSRobert Love {
21261a7b75aeSRobert Love 	struct fc_exch *ep;
21271a7b75aeSRobert Love 	struct fc_seq *sp = NULL;
21281a7b75aeSRobert Love 	struct fc_frame_header *fh;
21293ee17f59SYi Zou 	struct fc_fcp_pkt *fsp = NULL;
21301a7b75aeSRobert Love 	int rc = 1;
21311a7b75aeSRobert Love 
21323a3b42bfSRobert Love 	ep = fc_exch_alloc(lport, fp);
21331a7b75aeSRobert Love 	if (!ep) {
21341a7b75aeSRobert Love 		fc_frame_free(fp);
21351a7b75aeSRobert Love 		return NULL;
21361a7b75aeSRobert Love 	}
21371a7b75aeSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT;
21381a7b75aeSRobert Love 	fh = fc_frame_header_get(fp);
21391a7b75aeSRobert Love 	fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
21401a7b75aeSRobert Love 	ep->resp = resp;
21411a7b75aeSRobert Love 	ep->destructor = destructor;
21421a7b75aeSRobert Love 	ep->arg = arg;
2143*f7ce413cSHannes Reinecke 	ep->r_a_tov = lport->r_a_tov;
21443a3b42bfSRobert Love 	ep->lp = lport;
21451a7b75aeSRobert Love 	sp = &ep->seq;
21461a7b75aeSRobert Love 
21471a7b75aeSRobert Love 	ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
21481a7b75aeSRobert Love 	ep->f_ctl = ntoh24(fh->fh_f_ctl);
21491a7b75aeSRobert Love 	fc_exch_setup_hdr(ep, fp, ep->f_ctl);
21501a7b75aeSRobert Love 	sp->cnt++;
21511a7b75aeSRobert Love 
21523ee17f59SYi Zou 	if (ep->xid <= lport->lro_xid && fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) {
21533ee17f59SYi Zou 		fsp = fr_fsp(fp);
21541a7b75aeSRobert Love 		fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
21553ee17f59SYi Zou 	}
21561a7b75aeSRobert Love 
21573a3b42bfSRobert Love 	if (unlikely(lport->tt.frame_send(lport, fp)))
21581a7b75aeSRobert Love 		goto err;
21591a7b75aeSRobert Love 
21601a7b75aeSRobert Love 	if (timer_msec)
21611a7b75aeSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
21621a7b75aeSRobert Love 	ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not first seq */
21631a7b75aeSRobert Love 
21641a7b75aeSRobert Love 	if (ep->f_ctl & FC_FC_SEQ_INIT)
21651a7b75aeSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
21661a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
21671a7b75aeSRobert Love 	return sp;
21681a7b75aeSRobert Love err:
21693ee17f59SYi Zou 	if (fsp)
21703ee17f59SYi Zou 		fc_fcp_ddp_done(fsp);
21711a7b75aeSRobert Love 	rc = fc_exch_done_locked(ep);
21721a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
21731a7b75aeSRobert Love 	if (!rc)
21741a7b75aeSRobert Love 		fc_exch_delete(ep);
21751a7b75aeSRobert Love 	return NULL;
21761a7b75aeSRobert Love }
21771a7b75aeSRobert Love 
21783a3b42bfSRobert Love /**
21793a3b42bfSRobert Love  * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
21803a3b42bfSRobert Love  * @ep: The exchange to send the RRQ on
21813a3b42bfSRobert Love  *
218242e9a92fSRobert Love  * This tells the remote port to stop blocking the use of
218342e9a92fSRobert Love  * the exchange and the seq_cnt range.
218442e9a92fSRobert Love  */
218542e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *ep)
218642e9a92fSRobert Love {
21873a3b42bfSRobert Love 	struct fc_lport *lport;
218842e9a92fSRobert Love 	struct fc_els_rrq *rrq;
218942e9a92fSRobert Love 	struct fc_frame *fp;
219042e9a92fSRobert Love 	u32 did;
219142e9a92fSRobert Love 
21923a3b42bfSRobert Love 	lport = ep->lp;
219342e9a92fSRobert Love 
21943a3b42bfSRobert Love 	fp = fc_frame_alloc(lport, sizeof(*rrq));
219542e9a92fSRobert Love 	if (!fp)
2196a0cc1eccSVasu Dev 		goto retry;
2197a0cc1eccSVasu Dev 
219842e9a92fSRobert Love 	rrq = fc_frame_payload_get(fp, sizeof(*rrq));
219942e9a92fSRobert Love 	memset(rrq, 0, sizeof(*rrq));
220042e9a92fSRobert Love 	rrq->rrq_cmd = ELS_RRQ;
220142e9a92fSRobert Love 	hton24(rrq->rrq_s_id, ep->sid);
220242e9a92fSRobert Love 	rrq->rrq_ox_id = htons(ep->oxid);
220342e9a92fSRobert Love 	rrq->rrq_rx_id = htons(ep->rxid);
220442e9a92fSRobert Love 
220542e9a92fSRobert Love 	did = ep->did;
220642e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP)
220742e9a92fSRobert Love 		did = ep->sid;
220842e9a92fSRobert Love 
220942e9a92fSRobert Love 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
22107b2787ecSRobert Love 		       lport->port_id, FC_TYPE_ELS,
221142e9a92fSRobert Love 		       FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
221242e9a92fSRobert Love 
22133a3b42bfSRobert Love 	if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
22143a3b42bfSRobert Love 			     lport->e_d_tov))
2215a0cc1eccSVasu Dev 		return;
2216a0cc1eccSVasu Dev 
2217a0cc1eccSVasu Dev retry:
221857d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "exch: RRQ send failed\n");
2219a0cc1eccSVasu Dev 	spin_lock_bh(&ep->ex_lock);
2220a0cc1eccSVasu Dev 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
2221a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
2222a0cc1eccSVasu Dev 		/* drop hold for rec qual */
2223a0cc1eccSVasu Dev 		fc_exch_release(ep);
222442e9a92fSRobert Love 		return;
222542e9a92fSRobert Love 	}
2226a0cc1eccSVasu Dev 	ep->esb_stat |= ESB_ST_REC_QUAL;
2227a0cc1eccSVasu Dev 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
2228a0cc1eccSVasu Dev 	spin_unlock_bh(&ep->ex_lock);
222942e9a92fSRobert Love }
223042e9a92fSRobert Love 
22313a3b42bfSRobert Love /**
22323a3b42bfSRobert Love  * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
223392261156SJoe Eykholt  * @fp: The RRQ frame, not freed here.
223442e9a92fSRobert Love  */
223592261156SJoe Eykholt static void fc_exch_els_rrq(struct fc_frame *fp)
223642e9a92fSRobert Love {
223792261156SJoe Eykholt 	struct fc_lport *lport;
22383f127ad9SVasu Dev 	struct fc_exch *ep = NULL;	/* request or subject exchange */
223942e9a92fSRobert Love 	struct fc_els_rrq *rp;
224042e9a92fSRobert Love 	u32 sid;
224142e9a92fSRobert Love 	u16 xid;
224242e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
224342e9a92fSRobert Love 
224492261156SJoe Eykholt 	lport = fr_dev(fp);
224542e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
224642e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
224742e9a92fSRobert Love 	if (!rp)
224842e9a92fSRobert Love 		goto reject;
224942e9a92fSRobert Love 
225042e9a92fSRobert Love 	/*
225142e9a92fSRobert Love 	 * lookup subject exchange.
225242e9a92fSRobert Love 	 */
225342e9a92fSRobert Love 	sid = ntoh24(rp->rrq_s_id);		/* subject source */
225492261156SJoe Eykholt 	xid = fc_host_port_id(lport->host) == sid ?
225592261156SJoe Eykholt 			ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
225692261156SJoe Eykholt 	ep = fc_exch_lookup(lport, xid);
225742e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
225842e9a92fSRobert Love 	if (!ep)
225942e9a92fSRobert Love 		goto reject;
226042e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
226157d3ec7eSHannes Reinecke 	FC_EXCH_DBG(ep, "RRQ request from %x: xid %x rxid %x oxid %x\n",
226257d3ec7eSHannes Reinecke 		    sid, xid, ntohs(rp->rrq_rx_id), ntohs(rp->rrq_ox_id));
226342e9a92fSRobert Love 	if (ep->oxid != ntohs(rp->rrq_ox_id))
226442e9a92fSRobert Love 		goto unlock_reject;
226542e9a92fSRobert Love 	if (ep->rxid != ntohs(rp->rrq_rx_id) &&
226642e9a92fSRobert Love 	    ep->rxid != FC_XID_UNKNOWN)
226742e9a92fSRobert Love 		goto unlock_reject;
226842e9a92fSRobert Love 	explan = ELS_EXPL_SID;
226942e9a92fSRobert Love 	if (ep->sid != sid)
227042e9a92fSRobert Love 		goto unlock_reject;
227142e9a92fSRobert Love 
227242e9a92fSRobert Love 	/*
227342e9a92fSRobert Love 	 * Clear Recovery Qualifier state, and cancel timer if complete.
227442e9a92fSRobert Love 	 */
227542e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL) {
227642e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_REC_QUAL;
227742e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec qual */
227842e9a92fSRobert Love 	}
2279b29a4f30SVasu Dev 	if (ep->esb_stat & ESB_ST_COMPLETE)
2280b29a4f30SVasu Dev 		fc_exch_timer_cancel(ep);
228142e9a92fSRobert Love 
228242e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
228342e9a92fSRobert Love 
228442e9a92fSRobert Love 	/*
228542e9a92fSRobert Love 	 * Send LS_ACC.
228642e9a92fSRobert Love 	 */
228792261156SJoe Eykholt 	fc_seq_ls_acc(fp);
22883f127ad9SVasu Dev 	goto out;
228942e9a92fSRobert Love 
229042e9a92fSRobert Love unlock_reject:
229142e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
229242e9a92fSRobert Love reject:
229392261156SJoe Eykholt 	fc_seq_ls_rjt(fp, ELS_RJT_LOGIC, explan);
22943f127ad9SVasu Dev out:
22953f127ad9SVasu Dev 	if (ep)
22963f127ad9SVasu Dev 		fc_exch_release(ep);	/* drop hold from fc_exch_find */
229742e9a92fSRobert Love }
229842e9a92fSRobert Love 
22993a3b42bfSRobert Love /**
23004e5fae7aSVasu Dev  * fc_exch_update_stats() - update exches stats to lport
23014e5fae7aSVasu Dev  * @lport: The local port to update exchange manager stats
23024e5fae7aSVasu Dev  */
23034e5fae7aSVasu Dev void fc_exch_update_stats(struct fc_lport *lport)
23044e5fae7aSVasu Dev {
23054e5fae7aSVasu Dev 	struct fc_host_statistics *st;
23064e5fae7aSVasu Dev 	struct fc_exch_mgr_anchor *ema;
23074e5fae7aSVasu Dev 	struct fc_exch_mgr *mp;
23084e5fae7aSVasu Dev 
23094e5fae7aSVasu Dev 	st = &lport->host_stats;
23104e5fae7aSVasu Dev 
23114e5fae7aSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
23124e5fae7aSVasu Dev 		mp = ema->mp;
23134e5fae7aSVasu Dev 		st->fc_no_free_exch += atomic_read(&mp->stats.no_free_exch);
23144e5fae7aSVasu Dev 		st->fc_no_free_exch_xid +=
23154e5fae7aSVasu Dev 				atomic_read(&mp->stats.no_free_exch_xid);
23164e5fae7aSVasu Dev 		st->fc_xid_not_found += atomic_read(&mp->stats.xid_not_found);
23174e5fae7aSVasu Dev 		st->fc_xid_busy += atomic_read(&mp->stats.xid_busy);
23184e5fae7aSVasu Dev 		st->fc_seq_not_found += atomic_read(&mp->stats.seq_not_found);
23194e5fae7aSVasu Dev 		st->fc_non_bls_resp += atomic_read(&mp->stats.non_bls_resp);
23204e5fae7aSVasu Dev 	}
23214e5fae7aSVasu Dev }
23224e5fae7aSVasu Dev EXPORT_SYMBOL(fc_exch_update_stats);
23234e5fae7aSVasu Dev 
23244e5fae7aSVasu Dev /**
23253a3b42bfSRobert Love  * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
23263a3b42bfSRobert Love  * @lport: The local port to add the exchange manager to
23273a3b42bfSRobert Love  * @mp:	   The exchange manager to be added to the local port
23283a3b42bfSRobert Love  * @match: The match routine that indicates when this EM should be used
23293a3b42bfSRobert Love  */
233096316099SVasu Dev struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
233196316099SVasu Dev 					   struct fc_exch_mgr *mp,
233296316099SVasu Dev 					   bool (*match)(struct fc_frame *))
233396316099SVasu Dev {
233496316099SVasu Dev 	struct fc_exch_mgr_anchor *ema;
233596316099SVasu Dev 
233696316099SVasu Dev 	ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
233796316099SVasu Dev 	if (!ema)
233896316099SVasu Dev 		return ema;
233996316099SVasu Dev 
234096316099SVasu Dev 	ema->mp = mp;
234196316099SVasu Dev 	ema->match = match;
234296316099SVasu Dev 	/* add EM anchor to EM anchors list */
234396316099SVasu Dev 	list_add_tail(&ema->ema_list, &lport->ema_list);
234496316099SVasu Dev 	kref_get(&mp->kref);
234596316099SVasu Dev 	return ema;
234696316099SVasu Dev }
234796316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_add);
234896316099SVasu Dev 
23493a3b42bfSRobert Love /**
23503a3b42bfSRobert Love  * fc_exch_mgr_destroy() - Destroy an exchange manager
23513a3b42bfSRobert Love  * @kref: The reference to the EM to be destroyed
23523a3b42bfSRobert Love  */
235396316099SVasu Dev static void fc_exch_mgr_destroy(struct kref *kref)
235496316099SVasu Dev {
235596316099SVasu Dev 	struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
235696316099SVasu Dev 
235796316099SVasu Dev 	mempool_destroy(mp->ep_pool);
2358e4bc50beSVasu Dev 	free_percpu(mp->pool);
235996316099SVasu Dev 	kfree(mp);
236096316099SVasu Dev }
236196316099SVasu Dev 
23623a3b42bfSRobert Love /**
23633a3b42bfSRobert Love  * fc_exch_mgr_del() - Delete an EM from a local port's list
23643a3b42bfSRobert Love  * @ema: The exchange manager anchor identifying the EM to be deleted
23653a3b42bfSRobert Love  */
236696316099SVasu Dev void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
236796316099SVasu Dev {
236896316099SVasu Dev 	/* remove EM anchor from EM anchors list */
236996316099SVasu Dev 	list_del(&ema->ema_list);
237096316099SVasu Dev 	kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
237196316099SVasu Dev 	kfree(ema);
237296316099SVasu Dev }
237396316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_del);
237496316099SVasu Dev 
2375174e1ebfSChris Leech /**
23763a3b42bfSRobert Love  * fc_exch_mgr_list_clone() - Share all exchange manager objects
23773a3b42bfSRobert Love  * @src: Source lport to clone exchange managers from
23783a3b42bfSRobert Love  * @dst: New lport that takes references to all the exchange managers
2379174e1ebfSChris Leech  */
2380174e1ebfSChris Leech int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
2381174e1ebfSChris Leech {
2382174e1ebfSChris Leech 	struct fc_exch_mgr_anchor *ema, *tmp;
2383174e1ebfSChris Leech 
2384174e1ebfSChris Leech 	list_for_each_entry(ema, &src->ema_list, ema_list) {
2385174e1ebfSChris Leech 		if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
2386174e1ebfSChris Leech 			goto err;
2387174e1ebfSChris Leech 	}
2388174e1ebfSChris Leech 	return 0;
2389174e1ebfSChris Leech err:
2390174e1ebfSChris Leech 	list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
2391174e1ebfSChris Leech 		fc_exch_mgr_del(ema);
2392174e1ebfSChris Leech 	return -ENOMEM;
2393174e1ebfSChris Leech }
239472fa396bSVasu Dev EXPORT_SYMBOL(fc_exch_mgr_list_clone);
2395174e1ebfSChris Leech 
23963a3b42bfSRobert Love /**
23973a3b42bfSRobert Love  * fc_exch_mgr_alloc() - Allocate an exchange manager
23983a3b42bfSRobert Love  * @lport:   The local port that the new EM will be associated with
23993a3b42bfSRobert Love  * @class:   The default FC class for new exchanges
24003a3b42bfSRobert Love  * @min_xid: The minimum XID for exchanges from the new EM
24013a3b42bfSRobert Love  * @max_xid: The maximum XID for exchanges from the new EM
24023a3b42bfSRobert Love  * @match:   The match routine for the new EM
24033a3b42bfSRobert Love  */
24043a3b42bfSRobert Love struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
240542e9a92fSRobert Love 				      enum fc_class class,
240652ff878cSVasu Dev 				      u16 min_xid, u16 max_xid,
240752ff878cSVasu Dev 				      bool (*match)(struct fc_frame *))
240842e9a92fSRobert Love {
240942e9a92fSRobert Love 	struct fc_exch_mgr *mp;
2410e4bc50beSVasu Dev 	u16 pool_exch_range;
2411e4bc50beSVasu Dev 	size_t pool_size;
2412e4bc50beSVasu Dev 	unsigned int cpu;
2413e4bc50beSVasu Dev 	struct fc_exch_pool *pool;
241442e9a92fSRobert Love 
2415e4bc50beSVasu Dev 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
2416e4bc50beSVasu Dev 	    (min_xid & fc_cpu_mask) != 0) {
24173a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
241842e9a92fSRobert Love 			     min_xid, max_xid);
241942e9a92fSRobert Love 		return NULL;
242042e9a92fSRobert Love 	}
242142e9a92fSRobert Love 
242242e9a92fSRobert Love 	/*
2423b2f0091fSVasu Dev 	 * allocate memory for EM
242442e9a92fSRobert Love 	 */
2425b2f0091fSVasu Dev 	mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
242642e9a92fSRobert Love 	if (!mp)
242742e9a92fSRobert Love 		return NULL;
242842e9a92fSRobert Love 
242942e9a92fSRobert Love 	mp->class = class;
243042e9a92fSRobert Love 	/* adjust em exch xid range for offload */
243142e9a92fSRobert Love 	mp->min_xid = min_xid;
2432011a9008SSteven Clark 
2433011a9008SSteven Clark        /* reduce range so per cpu pool fits into PCPU_MIN_UNIT_SIZE pool */
2434011a9008SSteven Clark 	pool_exch_range = (PCPU_MIN_UNIT_SIZE - sizeof(*pool)) /
2435011a9008SSteven Clark 		sizeof(struct fc_exch *);
2436011a9008SSteven Clark 	if ((max_xid - min_xid + 1) / (fc_cpu_mask + 1) > pool_exch_range) {
2437011a9008SSteven Clark 		mp->max_xid = pool_exch_range * (fc_cpu_mask + 1) +
2438011a9008SSteven Clark 			min_xid - 1;
2439011a9008SSteven Clark 	} else {
244042e9a92fSRobert Love 		mp->max_xid = max_xid;
2441011a9008SSteven Clark 		pool_exch_range = (mp->max_xid - mp->min_xid + 1) /
2442011a9008SSteven Clark 			(fc_cpu_mask + 1);
2443011a9008SSteven Clark 	}
244442e9a92fSRobert Love 
244542e9a92fSRobert Love 	mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
244642e9a92fSRobert Love 	if (!mp->ep_pool)
244742e9a92fSRobert Love 		goto free_mp;
244842e9a92fSRobert Love 
2449e4bc50beSVasu Dev 	/*
2450e4bc50beSVasu Dev 	 * Setup per cpu exch pool with entire exchange id range equally
2451e4bc50beSVasu Dev 	 * divided across all cpus. The exch pointers array memory is
2452e4bc50beSVasu Dev 	 * allocated for exch range per pool.
2453e4bc50beSVasu Dev 	 */
2454e4bc50beSVasu Dev 	mp->pool_max_index = pool_exch_range - 1;
2455e4bc50beSVasu Dev 
2456e4bc50beSVasu Dev 	/*
2457e4bc50beSVasu Dev 	 * Allocate and initialize per cpu exch pool
2458e4bc50beSVasu Dev 	 */
2459e4bc50beSVasu Dev 	pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
2460e4bc50beSVasu Dev 	mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
2461e4bc50beSVasu Dev 	if (!mp->pool)
2462e4bc50beSVasu Dev 		goto free_mempool;
2463e4bc50beSVasu Dev 	for_each_possible_cpu(cpu) {
2464e4bc50beSVasu Dev 		pool = per_cpu_ptr(mp->pool, cpu);
2465b6e3c840SVasu Dev 		pool->next_index = 0;
24662034c19cSHillf Danton 		pool->left = FC_XID_UNKNOWN;
24672034c19cSHillf Danton 		pool->right = FC_XID_UNKNOWN;
2468e4bc50beSVasu Dev 		spin_lock_init(&pool->lock);
2469e4bc50beSVasu Dev 		INIT_LIST_HEAD(&pool->ex_list);
2470e4bc50beSVasu Dev 	}
2471e4bc50beSVasu Dev 
247252ff878cSVasu Dev 	kref_init(&mp->kref);
24733a3b42bfSRobert Love 	if (!fc_exch_mgr_add(lport, mp, match)) {
2474e4bc50beSVasu Dev 		free_percpu(mp->pool);
2475e4bc50beSVasu Dev 		goto free_mempool;
247652ff878cSVasu Dev 	}
247752ff878cSVasu Dev 
247852ff878cSVasu Dev 	/*
247952ff878cSVasu Dev 	 * Above kref_init() sets mp->kref to 1 and then
248052ff878cSVasu Dev 	 * call to fc_exch_mgr_add incremented mp->kref again,
248152ff878cSVasu Dev 	 * so adjust that extra increment.
248252ff878cSVasu Dev 	 */
248352ff878cSVasu Dev 	kref_put(&mp->kref, fc_exch_mgr_destroy);
248442e9a92fSRobert Love 	return mp;
248542e9a92fSRobert Love 
2486e4bc50beSVasu Dev free_mempool:
2487e4bc50beSVasu Dev 	mempool_destroy(mp->ep_pool);
248842e9a92fSRobert Love free_mp:
248942e9a92fSRobert Love 	kfree(mp);
249042e9a92fSRobert Love 	return NULL;
249142e9a92fSRobert Love }
249242e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_alloc);
249342e9a92fSRobert Love 
24943a3b42bfSRobert Love /**
24953a3b42bfSRobert Love  * fc_exch_mgr_free() - Free all exchange managers on a local port
24963a3b42bfSRobert Love  * @lport: The local port whose EMs are to be freed
24973a3b42bfSRobert Love  */
249852ff878cSVasu Dev void fc_exch_mgr_free(struct fc_lport *lport)
249942e9a92fSRobert Love {
250052ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema, *next;
250152ff878cSVasu Dev 
25024ae1e19fSVasu Dev 	flush_workqueue(fc_exch_workqueue);
250352ff878cSVasu Dev 	list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
250452ff878cSVasu Dev 		fc_exch_mgr_del(ema);
250542e9a92fSRobert Love }
250642e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_free);
250742e9a92fSRobert Love 
25083a3b42bfSRobert Love /**
25096c8cc1c0SKiran Patil  * fc_find_ema() - Lookup and return appropriate Exchange Manager Anchor depending
25106c8cc1c0SKiran Patil  * upon 'xid'.
25116c8cc1c0SKiran Patil  * @f_ctl: f_ctl
25126c8cc1c0SKiran Patil  * @lport: The local port the frame was received on
25136c8cc1c0SKiran Patil  * @fh: The received frame header
25146c8cc1c0SKiran Patil  */
25156c8cc1c0SKiran Patil static struct fc_exch_mgr_anchor *fc_find_ema(u32 f_ctl,
25166c8cc1c0SKiran Patil 					      struct fc_lport *lport,
25176c8cc1c0SKiran Patil 					      struct fc_frame_header *fh)
25186c8cc1c0SKiran Patil {
25196c8cc1c0SKiran Patil 	struct fc_exch_mgr_anchor *ema;
25206c8cc1c0SKiran Patil 	u16 xid;
25216c8cc1c0SKiran Patil 
25226c8cc1c0SKiran Patil 	if (f_ctl & FC_FC_EX_CTX)
25236c8cc1c0SKiran Patil 		xid = ntohs(fh->fh_ox_id);
25246c8cc1c0SKiran Patil 	else {
25256c8cc1c0SKiran Patil 		xid = ntohs(fh->fh_rx_id);
25266c8cc1c0SKiran Patil 		if (xid == FC_XID_UNKNOWN)
25276c8cc1c0SKiran Patil 			return list_entry(lport->ema_list.prev,
25286c8cc1c0SKiran Patil 					  typeof(*ema), ema_list);
25296c8cc1c0SKiran Patil 	}
25306c8cc1c0SKiran Patil 
25316c8cc1c0SKiran Patil 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
25326c8cc1c0SKiran Patil 		if ((xid >= ema->mp->min_xid) &&
25336c8cc1c0SKiran Patil 		    (xid <= ema->mp->max_xid))
25346c8cc1c0SKiran Patil 			return ema;
25356c8cc1c0SKiran Patil 	}
25366c8cc1c0SKiran Patil 	return NULL;
25376c8cc1c0SKiran Patil }
25386c8cc1c0SKiran Patil /**
25393a3b42bfSRobert Love  * fc_exch_recv() - Handler for received frames
25403a3b42bfSRobert Love  * @lport: The local port the frame was received on
25413a3b42bfSRobert Love  * @fp:	The received frame
254242e9a92fSRobert Love  */
25433a3b42bfSRobert Love void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
254442e9a92fSRobert Love {
254542e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
254652ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
25476c8cc1c0SKiran Patil 	u32 f_ctl;
254842e9a92fSRobert Love 
254942e9a92fSRobert Love 	/* lport lock ? */
25503a3b42bfSRobert Love 	if (!lport || lport->state == LPORT_ST_DISABLED) {
25513a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "Receiving frames for an lport that "
25527414705eSRobert Love 			     "has not been initialized correctly\n");
255342e9a92fSRobert Love 		fc_frame_free(fp);
255442e9a92fSRobert Love 		return;
255542e9a92fSRobert Love 	}
255642e9a92fSRobert Love 
255752ff878cSVasu Dev 	f_ctl = ntoh24(fh->fh_f_ctl);
25586c8cc1c0SKiran Patil 	ema = fc_find_ema(f_ctl, lport, fh);
25596c8cc1c0SKiran Patil 	if (!ema) {
25606c8cc1c0SKiran Patil 		FC_LPORT_DBG(lport, "Unable to find Exchange Manager Anchor,"
25616c8cc1c0SKiran Patil 				    "fc_ctl <0x%x>, xid <0x%x>\n",
25626c8cc1c0SKiran Patil 				     f_ctl,
25636c8cc1c0SKiran Patil 				     (f_ctl & FC_FC_EX_CTX) ?
25646c8cc1c0SKiran Patil 				     ntohs(fh->fh_ox_id) :
25656c8cc1c0SKiran Patil 				     ntohs(fh->fh_rx_id));
256652ff878cSVasu Dev 		fc_frame_free(fp);
256752ff878cSVasu Dev 		return;
256852ff878cSVasu Dev 	}
256952ff878cSVasu Dev 
257042e9a92fSRobert Love 	/*
257142e9a92fSRobert Love 	 * If frame is marked invalid, just drop it.
257242e9a92fSRobert Love 	 */
257342e9a92fSRobert Love 	switch (fr_eof(fp)) {
257442e9a92fSRobert Love 	case FC_EOF_T:
257542e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
257642e9a92fSRobert Love 			skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
257742e9a92fSRobert Love 		/* fall through */
257842e9a92fSRobert Love 	case FC_EOF_N:
257942e9a92fSRobert Love 		if (fh->fh_type == FC_TYPE_BLS)
258052ff878cSVasu Dev 			fc_exch_recv_bls(ema->mp, fp);
258142e9a92fSRobert Love 		else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
258242e9a92fSRobert Love 			 FC_FC_EX_CTX)
258352ff878cSVasu Dev 			fc_exch_recv_seq_resp(ema->mp, fp);
258442e9a92fSRobert Love 		else if (f_ctl & FC_FC_SEQ_CTX)
258552ff878cSVasu Dev 			fc_exch_recv_resp(ema->mp, fp);
258692261156SJoe Eykholt 		else	/* no EX_CTX and no SEQ_CTX */
25873a3b42bfSRobert Love 			fc_exch_recv_req(lport, ema->mp, fp);
258842e9a92fSRobert Love 		break;
258942e9a92fSRobert Love 	default:
25903a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
25913a3b42bfSRobert Love 			     fr_eof(fp));
259242e9a92fSRobert Love 		fc_frame_free(fp);
259342e9a92fSRobert Love 	}
259442e9a92fSRobert Love }
259542e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_recv);
259642e9a92fSRobert Love 
25973a3b42bfSRobert Love /**
25983a3b42bfSRobert Love  * fc_exch_init() - Initialize the exchange layer for a local port
25993a3b42bfSRobert Love  * @lport: The local port to initialize the exchange layer for
26003a3b42bfSRobert Love  */
26013a3b42bfSRobert Love int fc_exch_init(struct fc_lport *lport)
260242e9a92fSRobert Love {
26033a3b42bfSRobert Love 	if (!lport->tt.seq_start_next)
26043a3b42bfSRobert Love 		lport->tt.seq_start_next = fc_seq_start_next;
260542e9a92fSRobert Love 
26061a5c2d7eSJoe Eykholt 	if (!lport->tt.seq_set_resp)
26071a5c2d7eSJoe Eykholt 		lport->tt.seq_set_resp = fc_seq_set_resp;
26081a5c2d7eSJoe Eykholt 
26093a3b42bfSRobert Love 	if (!lport->tt.exch_seq_send)
26103a3b42bfSRobert Love 		lport->tt.exch_seq_send = fc_exch_seq_send;
261142e9a92fSRobert Love 
26123a3b42bfSRobert Love 	if (!lport->tt.seq_send)
26133a3b42bfSRobert Love 		lport->tt.seq_send = fc_seq_send;
261442e9a92fSRobert Love 
26153a3b42bfSRobert Love 	if (!lport->tt.seq_els_rsp_send)
26163a3b42bfSRobert Love 		lport->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
261742e9a92fSRobert Love 
26183a3b42bfSRobert Love 	if (!lport->tt.exch_done)
26193a3b42bfSRobert Love 		lport->tt.exch_done = fc_exch_done;
262042e9a92fSRobert Love 
26213a3b42bfSRobert Love 	if (!lport->tt.exch_mgr_reset)
26223a3b42bfSRobert Love 		lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
262342e9a92fSRobert Love 
26243a3b42bfSRobert Love 	if (!lport->tt.seq_exch_abort)
26253a3b42bfSRobert Love 		lport->tt.seq_exch_abort = fc_seq_exch_abort;
262642e9a92fSRobert Love 
2627239e8104SJoe Eykholt 	if (!lport->tt.seq_assign)
2628239e8104SJoe Eykholt 		lport->tt.seq_assign = fc_seq_assign;
2629239e8104SJoe Eykholt 
263062bdb645SJoe Eykholt 	if (!lport->tt.seq_release)
263162bdb645SJoe Eykholt 		lport->tt.seq_release = fc_seq_release;
263262bdb645SJoe Eykholt 
263389f19a59SVasu Dev 	return 0;
263489f19a59SVasu Dev }
263589f19a59SVasu Dev EXPORT_SYMBOL(fc_exch_init);
263689f19a59SVasu Dev 
263789f19a59SVasu Dev /**
263889f19a59SVasu Dev  * fc_setup_exch_mgr() - Setup an exchange manager
263989f19a59SVasu Dev  */
264055204909SRandy Dunlap int fc_setup_exch_mgr(void)
264189f19a59SVasu Dev {
264289f19a59SVasu Dev 	fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
264389f19a59SVasu Dev 					 0, SLAB_HWCACHE_ALIGN, NULL);
264489f19a59SVasu Dev 	if (!fc_em_cachep)
264589f19a59SVasu Dev 		return -ENOMEM;
264689f19a59SVasu Dev 
2647e4bc50beSVasu Dev 	/*
2648e4bc50beSVasu Dev 	 * Initialize fc_cpu_mask and fc_cpu_order. The
2649e4bc50beSVasu Dev 	 * fc_cpu_mask is set for nr_cpu_ids rounded up
2650e4bc50beSVasu Dev 	 * to order of 2's * power and order is stored
2651e4bc50beSVasu Dev 	 * in fc_cpu_order as this is later required in
2652e4bc50beSVasu Dev 	 * mapping between an exch id and exch array index
2653e4bc50beSVasu Dev 	 * in per cpu exch pool.
2654e4bc50beSVasu Dev 	 *
2655e4bc50beSVasu Dev 	 * This round up is required to align fc_cpu_mask
2656e4bc50beSVasu Dev 	 * to exchange id's lower bits such that all incoming
2657e4bc50beSVasu Dev 	 * frames of an exchange gets delivered to the same
2658e4bc50beSVasu Dev 	 * cpu on which exchange originated by simple bitwise
2659e4bc50beSVasu Dev 	 * AND operation between fc_cpu_mask and exchange id.
2660e4bc50beSVasu Dev 	 */
2661a84ea8c7SBart Van Assche 	fc_cpu_order = ilog2(roundup_pow_of_two(nr_cpu_ids));
2662a84ea8c7SBart Van Assche 	fc_cpu_mask = (1 << fc_cpu_order) - 1;
2663e4bc50beSVasu Dev 
26644ae1e19fSVasu Dev 	fc_exch_workqueue = create_singlethread_workqueue("fc_exch_workqueue");
26654ae1e19fSVasu Dev 	if (!fc_exch_workqueue)
26666f06e3a7SHillf Danton 		goto err;
266742e9a92fSRobert Love 	return 0;
26686f06e3a7SHillf Danton err:
26696f06e3a7SHillf Danton 	kmem_cache_destroy(fc_em_cachep);
26706f06e3a7SHillf Danton 	return -ENOMEM;
267142e9a92fSRobert Love }
267242e9a92fSRobert Love 
26733a3b42bfSRobert Love /**
26743a3b42bfSRobert Love  * fc_destroy_exch_mgr() - Destroy an exchange manager
26753a3b42bfSRobert Love  */
267655204909SRandy Dunlap void fc_destroy_exch_mgr(void)
267742e9a92fSRobert Love {
26784ae1e19fSVasu Dev 	destroy_workqueue(fc_exch_workqueue);
267942e9a92fSRobert Love 	kmem_cache_destroy(fc_em_cachep);
268042e9a92fSRobert Love }
2681