xref: /linux/drivers/scsi/libfc/fc_exch.c (revision 3a2926054acf876313afaaddac00a8ad255f6d68)
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>
3042e9a92fSRobert Love 
3142e9a92fSRobert Love #include <scsi/fc/fc_fc2.h>
3242e9a92fSRobert Love 
3342e9a92fSRobert Love #include <scsi/libfc.h>
3442e9a92fSRobert Love #include <scsi/fc_encode.h>
3542e9a92fSRobert Love 
368866a5d9SRobert Love #include "fc_libfc.h"
378866a5d9SRobert Love 
38e4bc50beSVasu Dev u16	fc_cpu_mask;		/* cpu mask for possible cpus */
39e4bc50beSVasu Dev EXPORT_SYMBOL(fc_cpu_mask);
40e4bc50beSVasu Dev static u16	fc_cpu_order;	/* 2's power to represent total possible cpus */
4142e9a92fSRobert Love static struct kmem_cache *fc_em_cachep;	       /* cache for exchanges */
4255204909SRandy Dunlap static struct workqueue_struct *fc_exch_workqueue;
4342e9a92fSRobert Love 
4442e9a92fSRobert Love /*
4542e9a92fSRobert Love  * Structure and function definitions for managing Fibre Channel Exchanges
4642e9a92fSRobert Love  * and Sequences.
4742e9a92fSRobert Love  *
4842e9a92fSRobert Love  * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
4942e9a92fSRobert Love  *
5042e9a92fSRobert Love  * fc_exch_mgr holds the exchange state for an N port
5142e9a92fSRobert Love  *
5242e9a92fSRobert Love  * fc_exch holds state for one exchange and links to its active sequence.
5342e9a92fSRobert Love  *
5442e9a92fSRobert Love  * fc_seq holds the state for an individual sequence.
5542e9a92fSRobert Love  */
5642e9a92fSRobert Love 
573a3b42bfSRobert Love /**
583a3b42bfSRobert Love  * struct fc_exch_pool - Per cpu exchange pool
593a3b42bfSRobert Love  * @next_index:	  Next possible free exchange index
603a3b42bfSRobert Love  * @total_exches: Total allocated exchanges
613a3b42bfSRobert Love  * @lock:	  Exch pool lock
623a3b42bfSRobert Love  * @ex_list:	  List of exchanges
63e4bc50beSVasu Dev  *
64e4bc50beSVasu Dev  * This structure manages per cpu exchanges in array of exchange pointers.
65e4bc50beSVasu Dev  * This array is allocated followed by struct fc_exch_pool memory for
66e4bc50beSVasu Dev  * assigned range of exchanges to per cpu pool.
67e4bc50beSVasu Dev  */
68e4bc50beSVasu Dev struct fc_exch_pool {
69e17b4af7SVasu Dev 	spinlock_t	 lock;
70e17b4af7SVasu Dev 	struct list_head ex_list;
713a3b42bfSRobert Love 	u16		 next_index;
723a3b42bfSRobert Love 	u16		 total_exches;
732034c19cSHillf Danton 
742034c19cSHillf Danton 	/* two cache of free slot in exch array */
752034c19cSHillf Danton 	u16		 left;
762034c19cSHillf Danton 	u16		 right;
77e17b4af7SVasu Dev } ____cacheline_aligned_in_smp;
78e4bc50beSVasu Dev 
793a3b42bfSRobert Love /**
803a3b42bfSRobert Love  * struct fc_exch_mgr - The Exchange Manager (EM).
813a3b42bfSRobert Love  * @class:	    Default class for new sequences
823a3b42bfSRobert Love  * @kref:	    Reference counter
833a3b42bfSRobert Love  * @min_xid:	    Minimum exchange ID
843a3b42bfSRobert Love  * @max_xid:	    Maximum exchange ID
853a3b42bfSRobert Love  * @ep_pool:	    Reserved exchange pointers
863a3b42bfSRobert Love  * @pool_max_index: Max exch array index in exch pool
873a3b42bfSRobert Love  * @pool:	    Per cpu exch pool
883a3b42bfSRobert Love  * @stats:	    Statistics structure
8942e9a92fSRobert Love  *
9042e9a92fSRobert Love  * This structure is the center for creating exchanges and sequences.
9142e9a92fSRobert Love  * It manages the allocation of exchange IDs.
9242e9a92fSRobert Love  */
9342e9a92fSRobert Love struct fc_exch_mgr {
94c6b21c93SBart Van Assche 	struct fc_exch_pool __percpu *pool;
95e17b4af7SVasu Dev 	mempool_t	*ep_pool;
963a3b42bfSRobert Love 	enum fc_class	class;
973a3b42bfSRobert Love 	struct kref	kref;
983a3b42bfSRobert Love 	u16		min_xid;
993a3b42bfSRobert Love 	u16		max_xid;
1003a3b42bfSRobert Love 	u16		pool_max_index;
10142e9a92fSRobert Love 
10242e9a92fSRobert Love 	struct {
10342e9a92fSRobert Love 		atomic_t no_free_exch;
10442e9a92fSRobert Love 		atomic_t no_free_exch_xid;
10542e9a92fSRobert Love 		atomic_t xid_not_found;
10642e9a92fSRobert Love 		atomic_t xid_busy;
10742e9a92fSRobert Love 		atomic_t seq_not_found;
10842e9a92fSRobert Love 		atomic_t non_bls_resp;
10942e9a92fSRobert Love 	} stats;
11042e9a92fSRobert Love };
11142e9a92fSRobert Love 
1123a3b42bfSRobert Love /**
1133a3b42bfSRobert Love  * struct fc_exch_mgr_anchor - primary structure for list of EMs
1143a3b42bfSRobert Love  * @ema_list: Exchange Manager Anchor list
1153a3b42bfSRobert Love  * @mp:	      Exchange Manager associated with this anchor
1163a3b42bfSRobert Love  * @match:    Routine to determine if this anchor's EM should be used
1173a3b42bfSRobert Love  *
1183a3b42bfSRobert Love  * When walking the list of anchors the match routine will be called
1193a3b42bfSRobert Love  * for each anchor to determine if that EM should be used. The last
1203a3b42bfSRobert Love  * anchor in the list will always match to handle any exchanges not
1213a3b42bfSRobert Love  * handled by other EMs. The non-default EMs would be added to the
1221bd49b48SVasu Dev  * anchor list by HW that provides offloads.
1233a3b42bfSRobert Love  */
12496316099SVasu Dev struct fc_exch_mgr_anchor {
12596316099SVasu Dev 	struct list_head ema_list;
12696316099SVasu Dev 	struct fc_exch_mgr *mp;
12796316099SVasu Dev 	bool (*match)(struct fc_frame *);
12896316099SVasu Dev };
12996316099SVasu Dev 
13042e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *);
13192261156SJoe Eykholt static void fc_seq_ls_acc(struct fc_frame *);
13292261156SJoe Eykholt static void fc_seq_ls_rjt(struct fc_frame *, enum fc_els_rjt_reason,
13342e9a92fSRobert Love 			  enum fc_els_rjt_explan);
13492261156SJoe Eykholt static void fc_exch_els_rec(struct fc_frame *);
13592261156SJoe Eykholt static void fc_exch_els_rrq(struct fc_frame *);
13642e9a92fSRobert Love 
13742e9a92fSRobert Love /*
13842e9a92fSRobert Love  * Internal implementation notes.
13942e9a92fSRobert Love  *
14042e9a92fSRobert Love  * The exchange manager is one by default in libfc but LLD may choose
14142e9a92fSRobert Love  * to have one per CPU. The sequence manager is one per exchange manager
14242e9a92fSRobert Love  * and currently never separated.
14342e9a92fSRobert Love  *
14442e9a92fSRobert Love  * Section 9.8 in FC-FS-2 specifies:  "The SEQ_ID is a one-byte field
14542e9a92fSRobert Love  * assigned by the Sequence Initiator that shall be unique for a specific
14642e9a92fSRobert Love  * D_ID and S_ID pair while the Sequence is open."   Note that it isn't
14742e9a92fSRobert Love  * qualified by exchange ID, which one might think it would be.
14842e9a92fSRobert Love  * In practice this limits the number of open sequences and exchanges to 256
14942e9a92fSRobert Love  * per session.	 For most targets we could treat this limit as per exchange.
15042e9a92fSRobert Love  *
15142e9a92fSRobert Love  * The exchange and its sequence are freed when the last sequence is received.
15242e9a92fSRobert Love  * It's possible for the remote port to leave an exchange open without
15342e9a92fSRobert Love  * sending any sequences.
15442e9a92fSRobert Love  *
15542e9a92fSRobert Love  * Notes on reference counts:
15642e9a92fSRobert Love  *
15742e9a92fSRobert Love  * Exchanges are reference counted and exchange gets freed when the reference
15842e9a92fSRobert Love  * count becomes zero.
15942e9a92fSRobert Love  *
16042e9a92fSRobert Love  * Timeouts:
16142e9a92fSRobert Love  * Sequences are timed out for E_D_TOV and R_A_TOV.
16242e9a92fSRobert Love  *
16342e9a92fSRobert Love  * Sequence event handling:
16442e9a92fSRobert Love  *
16542e9a92fSRobert Love  * The following events may occur on initiator sequences:
16642e9a92fSRobert Love  *
16742e9a92fSRobert Love  *	Send.
16842e9a92fSRobert Love  *	    For now, the whole thing is sent.
16942e9a92fSRobert Love  *	Receive ACK
17042e9a92fSRobert Love  *	    This applies only to class F.
17142e9a92fSRobert Love  *	    The sequence is marked complete.
17242e9a92fSRobert Love  *	ULP completion.
17342e9a92fSRobert Love  *	    The upper layer calls fc_exch_done() when done
17442e9a92fSRobert Love  *	    with exchange and sequence tuple.
17542e9a92fSRobert Love  *	RX-inferred completion.
17642e9a92fSRobert Love  *	    When we receive the next sequence on the same exchange, we can
17742e9a92fSRobert Love  *	    retire the previous sequence ID.  (XXX not implemented).
17842e9a92fSRobert Love  *	Timeout.
17942e9a92fSRobert Love  *	    R_A_TOV frees the sequence ID.  If we're waiting for ACK,
18042e9a92fSRobert Love  *	    E_D_TOV causes abort and calls upper layer response handler
18142e9a92fSRobert Love  *	    with FC_EX_TIMEOUT error.
18242e9a92fSRobert Love  *	Receive RJT
18342e9a92fSRobert Love  *	    XXX defer.
18442e9a92fSRobert Love  *	Send ABTS
18542e9a92fSRobert Love  *	    On timeout.
18642e9a92fSRobert Love  *
18742e9a92fSRobert Love  * The following events may occur on recipient sequences:
18842e9a92fSRobert Love  *
18942e9a92fSRobert Love  *	Receive
19042e9a92fSRobert Love  *	    Allocate sequence for first frame received.
19142e9a92fSRobert Love  *	    Hold during receive handler.
19242e9a92fSRobert Love  *	    Release when final frame received.
19342e9a92fSRobert Love  *	    Keep status of last N of these for the ELS RES command.  XXX TBD.
19442e9a92fSRobert Love  *	Receive ABTS
19542e9a92fSRobert Love  *	    Deallocate sequence
19642e9a92fSRobert Love  *	Send RJT
19742e9a92fSRobert Love  *	    Deallocate
19842e9a92fSRobert Love  *
19942e9a92fSRobert Love  * For now, we neglect conditions where only part of a sequence was
20042e9a92fSRobert Love  * received or transmitted, or where out-of-order receipt is detected.
20142e9a92fSRobert Love  */
20242e9a92fSRobert Love 
20342e9a92fSRobert Love /*
20442e9a92fSRobert Love  * Locking notes:
20542e9a92fSRobert Love  *
20642e9a92fSRobert Love  * The EM code run in a per-CPU worker thread.
20742e9a92fSRobert Love  *
20842e9a92fSRobert Love  * To protect against concurrency between a worker thread code and timers,
20942e9a92fSRobert Love  * sequence allocation and deallocation must be locked.
21042e9a92fSRobert Love  *  - exchange refcnt can be done atomicly without locks.
21142e9a92fSRobert Love  *  - sequence allocation must be locked by exch lock.
212b2f0091fSVasu Dev  *  - If the EM pool lock and ex_lock must be taken at the same time, then the
213b2f0091fSVasu Dev  *    EM pool lock must be taken before the ex_lock.
21442e9a92fSRobert Love  */
21542e9a92fSRobert Love 
21642e9a92fSRobert Love /*
21742e9a92fSRobert Love  * opcode names for debugging.
21842e9a92fSRobert Love  */
21942e9a92fSRobert Love static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
22042e9a92fSRobert Love 
2213a3b42bfSRobert Love /**
2223a3b42bfSRobert Love  * fc_exch_name_lookup() - Lookup name by opcode
2233a3b42bfSRobert Love  * @op:	       Opcode to be looked up
2243a3b42bfSRobert Love  * @table:     Opcode/name table
2253a3b42bfSRobert Love  * @max_index: Index not to be exceeded
2263a3b42bfSRobert Love  *
2273a3b42bfSRobert Love  * This routine is used to determine a human-readable string identifying
2283a3b42bfSRobert Love  * a R_CTL opcode.
2293a3b42bfSRobert Love  */
23042e9a92fSRobert Love static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
23142e9a92fSRobert Love 					      unsigned int max_index)
23242e9a92fSRobert Love {
23342e9a92fSRobert Love 	const char *name = NULL;
23442e9a92fSRobert Love 
23542e9a92fSRobert Love 	if (op < max_index)
23642e9a92fSRobert Love 		name = table[op];
23742e9a92fSRobert Love 	if (!name)
23842e9a92fSRobert Love 		name = "unknown";
23942e9a92fSRobert Love 	return name;
24042e9a92fSRobert Love }
24142e9a92fSRobert Love 
2423a3b42bfSRobert Love /**
2433a3b42bfSRobert Love  * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
2443a3b42bfSRobert Love  * @op: The opcode to be looked up
2453a3b42bfSRobert Love  */
24642e9a92fSRobert Love static const char *fc_exch_rctl_name(unsigned int op)
24742e9a92fSRobert Love {
24842e9a92fSRobert Love 	return fc_exch_name_lookup(op, fc_exch_rctl_names,
2497156fffaSKulikov Vasiliy 				   ARRAY_SIZE(fc_exch_rctl_names));
25042e9a92fSRobert Love }
25142e9a92fSRobert Love 
2523a3b42bfSRobert Love /**
2533a3b42bfSRobert Love  * fc_exch_hold() - Increment an exchange's reference count
2543a3b42bfSRobert Love  * @ep: Echange to be held
25542e9a92fSRobert Love  */
2563a3b42bfSRobert Love static inline void fc_exch_hold(struct fc_exch *ep)
25742e9a92fSRobert Love {
25842e9a92fSRobert Love 	atomic_inc(&ep->ex_refcnt);
25942e9a92fSRobert Love }
26042e9a92fSRobert Love 
2613a3b42bfSRobert Love /**
2623a3b42bfSRobert Love  * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
2633a3b42bfSRobert Love  *			 and determine SOF and EOF.
2643a3b42bfSRobert Love  * @ep:	   The exchange to that will use the header
2653a3b42bfSRobert Love  * @fp:	   The frame whose header is to be modified
2663a3b42bfSRobert Love  * @f_ctl: F_CTL bits that will be used for the frame header
2673a3b42bfSRobert Love  *
2683a3b42bfSRobert Love  * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
2693a3b42bfSRobert Love  * fh_seq_id, fh_seq_cnt and the SOF and EOF.
27042e9a92fSRobert Love  */
27142e9a92fSRobert Love static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
27242e9a92fSRobert Love 			      u32 f_ctl)
27342e9a92fSRobert Love {
27442e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
27542e9a92fSRobert Love 	u16 fill;
27642e9a92fSRobert Love 
27742e9a92fSRobert Love 	fr_sof(fp) = ep->class;
27842e9a92fSRobert Love 	if (ep->seq.cnt)
27942e9a92fSRobert Love 		fr_sof(fp) = fc_sof_normal(ep->class);
28042e9a92fSRobert Love 
28142e9a92fSRobert Love 	if (f_ctl & FC_FC_END_SEQ) {
28242e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_T;
28342e9a92fSRobert Love 		if (fc_sof_needs_ack(ep->class))
28442e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
28542e9a92fSRobert Love 		/*
2863a3b42bfSRobert Love 		 * From F_CTL.
28742e9a92fSRobert Love 		 * The number of fill bytes to make the length a 4-byte
28842e9a92fSRobert Love 		 * multiple is the low order 2-bits of the f_ctl.
28942e9a92fSRobert Love 		 * The fill itself will have been cleared by the frame
29042e9a92fSRobert Love 		 * allocation.
29142e9a92fSRobert Love 		 * After this, the length will be even, as expected by
29242e9a92fSRobert Love 		 * the transport.
29342e9a92fSRobert Love 		 */
29442e9a92fSRobert Love 		fill = fr_len(fp) & 3;
29542e9a92fSRobert Love 		if (fill) {
29642e9a92fSRobert Love 			fill = 4 - fill;
29742e9a92fSRobert Love 			/* TODO, this may be a problem with fragmented skb */
29842e9a92fSRobert Love 			skb_put(fp_skb(fp), fill);
29942e9a92fSRobert Love 			hton24(fh->fh_f_ctl, f_ctl | fill);
30042e9a92fSRobert Love 		}
30142e9a92fSRobert Love 	} else {
30242e9a92fSRobert Love 		WARN_ON(fr_len(fp) % 4 != 0);	/* no pad to non last frame */
30342e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
30442e9a92fSRobert Love 	}
30542e9a92fSRobert Love 
30642e9a92fSRobert Love 	/*
30742e9a92fSRobert Love 	 * Initialize remainig fh fields
30842e9a92fSRobert Love 	 * from fc_fill_fc_hdr
30942e9a92fSRobert Love 	 */
31042e9a92fSRobert Love 	fh->fh_ox_id = htons(ep->oxid);
31142e9a92fSRobert Love 	fh->fh_rx_id = htons(ep->rxid);
31242e9a92fSRobert Love 	fh->fh_seq_id = ep->seq.id;
31342e9a92fSRobert Love 	fh->fh_seq_cnt = htons(ep->seq.cnt);
31442e9a92fSRobert Love }
31542e9a92fSRobert Love 
3163a3b42bfSRobert Love /**
3173a3b42bfSRobert Love  * fc_exch_release() - Decrement an exchange's reference count
3183a3b42bfSRobert Love  * @ep: Exchange to be released
3193a3b42bfSRobert Love  *
3203a3b42bfSRobert Love  * If the reference count reaches zero and the exchange is complete,
3213a3b42bfSRobert Love  * it is freed.
32242e9a92fSRobert Love  */
32342e9a92fSRobert Love static void fc_exch_release(struct fc_exch *ep)
32442e9a92fSRobert Love {
32542e9a92fSRobert Love 	struct fc_exch_mgr *mp;
32642e9a92fSRobert Love 
32742e9a92fSRobert Love 	if (atomic_dec_and_test(&ep->ex_refcnt)) {
32842e9a92fSRobert Love 		mp = ep->em;
32942e9a92fSRobert Love 		if (ep->destructor)
33042e9a92fSRobert Love 			ep->destructor(&ep->seq, ep->arg);
331aa6cd29bSJulia Lawall 		WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
33242e9a92fSRobert Love 		mempool_free(ep, mp->ep_pool);
33342e9a92fSRobert Love 	}
33442e9a92fSRobert Love }
33542e9a92fSRobert Love 
3363a3b42bfSRobert Love /**
337b29a4f30SVasu Dev  * fc_exch_timer_cancel() - cancel exch timer
338b29a4f30SVasu Dev  * @ep:		The exchange whose timer to be canceled
339b29a4f30SVasu Dev  */
340b29a4f30SVasu Dev static inline void fc_exch_timer_cancel(struct fc_exch *ep)
341b29a4f30SVasu Dev {
342b29a4f30SVasu Dev 	if (cancel_delayed_work(&ep->timeout_work)) {
343b29a4f30SVasu Dev 		FC_EXCH_DBG(ep, "Exchange timer canceled\n");
344b29a4f30SVasu Dev 		atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
345b29a4f30SVasu Dev 	}
346b29a4f30SVasu Dev }
347b29a4f30SVasu Dev 
348b29a4f30SVasu Dev /**
349b29a4f30SVasu Dev  * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
350b29a4f30SVasu Dev  *				the exchange lock held
351b29a4f30SVasu Dev  * @ep:		The exchange whose timer will start
352b29a4f30SVasu Dev  * @timer_msec: The timeout period
353b29a4f30SVasu Dev  *
354b29a4f30SVasu Dev  * Used for upper level protocols to time out the exchange.
355b29a4f30SVasu Dev  * The timer is cancelled when it fires or when the exchange completes.
356b29a4f30SVasu Dev  */
357b29a4f30SVasu Dev static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
358b29a4f30SVasu Dev 					    unsigned int timer_msec)
359b29a4f30SVasu Dev {
360b29a4f30SVasu Dev 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
361b29a4f30SVasu Dev 		return;
362b29a4f30SVasu Dev 
363b29a4f30SVasu Dev 	FC_EXCH_DBG(ep, "Exchange timer armed : %d msecs\n", timer_msec);
364b29a4f30SVasu Dev 
365b29a4f30SVasu Dev 	if (queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
366b29a4f30SVasu Dev 			       msecs_to_jiffies(timer_msec)))
367b29a4f30SVasu Dev 		fc_exch_hold(ep);		/* hold for timer */
368b29a4f30SVasu Dev }
369b29a4f30SVasu Dev 
370b29a4f30SVasu Dev /**
371b29a4f30SVasu Dev  * fc_exch_timer_set() - Lock the exchange and set the timer
372b29a4f30SVasu Dev  * @ep:		The exchange whose timer will start
373b29a4f30SVasu Dev  * @timer_msec: The timeout period
374b29a4f30SVasu Dev  */
375b29a4f30SVasu Dev static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
376b29a4f30SVasu Dev {
377b29a4f30SVasu Dev 	spin_lock_bh(&ep->ex_lock);
378b29a4f30SVasu Dev 	fc_exch_timer_set_locked(ep, timer_msec);
379b29a4f30SVasu Dev 	spin_unlock_bh(&ep->ex_lock);
380b29a4f30SVasu Dev }
381b29a4f30SVasu Dev 
382b29a4f30SVasu Dev /**
3833a3b42bfSRobert Love  * fc_exch_done_locked() - Complete an exchange with the exchange lock held
3843a3b42bfSRobert Love  * @ep: The exchange that is complete
3853a3b42bfSRobert Love  */
38642e9a92fSRobert Love static int fc_exch_done_locked(struct fc_exch *ep)
38742e9a92fSRobert Love {
38842e9a92fSRobert Love 	int rc = 1;
38942e9a92fSRobert Love 
39042e9a92fSRobert Love 	/*
39142e9a92fSRobert Love 	 * We must check for completion in case there are two threads
39242e9a92fSRobert Love 	 * tyring to complete this. But the rrq code will reuse the
39342e9a92fSRobert Love 	 * ep, and in that case we only clear the resp and set it as
39442e9a92fSRobert Love 	 * complete, so it can be reused by the timer to send the rrq.
39542e9a92fSRobert Love 	 */
39642e9a92fSRobert Love 	ep->resp = NULL;
39742e9a92fSRobert Love 	if (ep->state & FC_EX_DONE)
39842e9a92fSRobert Love 		return rc;
39942e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_COMPLETE;
40042e9a92fSRobert Love 
40142e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
40242e9a92fSRobert Love 		ep->state |= FC_EX_DONE;
403b29a4f30SVasu Dev 		fc_exch_timer_cancel(ep);
40442e9a92fSRobert Love 		rc = 0;
40542e9a92fSRobert Love 	}
40642e9a92fSRobert Love 	return rc;
40742e9a92fSRobert Love }
40842e9a92fSRobert Love 
4093a3b42bfSRobert Love /**
4103a3b42bfSRobert Love  * fc_exch_ptr_get() - Return an exchange from an exchange pool
4113a3b42bfSRobert Love  * @pool:  Exchange Pool to get an exchange from
4123a3b42bfSRobert Love  * @index: Index of the exchange within the pool
4133a3b42bfSRobert Love  *
4143a3b42bfSRobert Love  * Use the index to get an exchange from within an exchange pool. exches
4153a3b42bfSRobert Love  * will point to an array of exchange pointers. The index will select
4163a3b42bfSRobert Love  * the exchange within the array.
4173a3b42bfSRobert Love  */
418e4bc50beSVasu Dev static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
419e4bc50beSVasu Dev 					      u16 index)
420e4bc50beSVasu Dev {
421e4bc50beSVasu Dev 	struct fc_exch **exches = (struct fc_exch **)(pool + 1);
422e4bc50beSVasu Dev 	return exches[index];
423e4bc50beSVasu Dev }
424e4bc50beSVasu Dev 
4253a3b42bfSRobert Love /**
4263a3b42bfSRobert Love  * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
4273a3b42bfSRobert Love  * @pool:  The pool to assign the exchange to
4283a3b42bfSRobert Love  * @index: The index in the pool where the exchange will be assigned
4293a3b42bfSRobert Love  * @ep:	   The exchange to assign to the pool
4303a3b42bfSRobert Love  */
431e4bc50beSVasu Dev static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
432e4bc50beSVasu Dev 				   struct fc_exch *ep)
433e4bc50beSVasu Dev {
434e4bc50beSVasu Dev 	((struct fc_exch **)(pool + 1))[index] = ep;
435e4bc50beSVasu Dev }
436e4bc50beSVasu Dev 
4373a3b42bfSRobert Love /**
4383a3b42bfSRobert Love  * fc_exch_delete() - Delete an exchange
4393a3b42bfSRobert Love  * @ep: The exchange to be deleted
4403a3b42bfSRobert Love  */
441b2f0091fSVasu Dev static void fc_exch_delete(struct fc_exch *ep)
44242e9a92fSRobert Love {
443b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
4442034c19cSHillf Danton 	u16 index;
44542e9a92fSRobert Love 
446b2f0091fSVasu Dev 	pool = ep->pool;
447b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
448b2f0091fSVasu Dev 	WARN_ON(pool->total_exches <= 0);
449b2f0091fSVasu Dev 	pool->total_exches--;
4502034c19cSHillf Danton 
4512034c19cSHillf Danton 	/* update cache of free slot */
4522034c19cSHillf Danton 	index = (ep->xid - ep->em->min_xid) >> fc_cpu_order;
4532034c19cSHillf Danton 	if (pool->left == FC_XID_UNKNOWN)
4542034c19cSHillf Danton 		pool->left = index;
4552034c19cSHillf Danton 	else if (pool->right == FC_XID_UNKNOWN)
4562034c19cSHillf Danton 		pool->right = index;
4572034c19cSHillf Danton 	else
4582034c19cSHillf Danton 		pool->next_index = index;
4592034c19cSHillf Danton 
4602034c19cSHillf Danton 	fc_exch_ptr_set(pool, index, NULL);
46142e9a92fSRobert Love 	list_del(&ep->ex_list);
462b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
46342e9a92fSRobert Love 	fc_exch_release(ep);	/* drop hold for exch in mp */
46442e9a92fSRobert Love }
46542e9a92fSRobert Love 
466fb00cc23SNeil Horman static int fc_seq_send_locked(struct fc_lport *lport, struct fc_seq *sp,
4671a7b75aeSRobert Love 		       struct fc_frame *fp)
4681a7b75aeSRobert Love {
4691a7b75aeSRobert Love 	struct fc_exch *ep;
4701a7b75aeSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
4711a7b75aeSRobert Love 	int error;
4721a7b75aeSRobert Love 	u32 f_ctl;
47314fc315fSVasu Dev 	u8 fh_type = fh->fh_type;
4741a7b75aeSRobert Love 
4751a7b75aeSRobert Love 	ep = fc_seq_exch(sp);
476fb00cc23SNeil Horman 	WARN_ON(!(ep->esb_stat & ESB_ST_SEQ_INIT));
4771a7b75aeSRobert Love 
4781a7b75aeSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
4791a7b75aeSRobert Love 	fc_exch_setup_hdr(ep, fp, f_ctl);
480f60e12e9SJoe Eykholt 	fr_encaps(fp) = ep->encaps;
4811a7b75aeSRobert Love 
4821a7b75aeSRobert Love 	/*
4831a7b75aeSRobert Love 	 * update sequence count if this frame is carrying
4841a7b75aeSRobert Love 	 * multiple FC frames when sequence offload is enabled
4851a7b75aeSRobert Love 	 * by LLD.
4861a7b75aeSRobert Love 	 */
4871a7b75aeSRobert Love 	if (fr_max_payload(fp))
4881a7b75aeSRobert Love 		sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
4891a7b75aeSRobert Love 					fr_max_payload(fp));
4901a7b75aeSRobert Love 	else
4911a7b75aeSRobert Love 		sp->cnt++;
4921a7b75aeSRobert Love 
4931a7b75aeSRobert Love 	/*
4941a7b75aeSRobert Love 	 * Send the frame.
4951a7b75aeSRobert Love 	 */
4963a3b42bfSRobert Love 	error = lport->tt.frame_send(lport, fp);
4971a7b75aeSRobert Love 
49814fc315fSVasu Dev 	if (fh_type == FC_TYPE_BLS)
499fb00cc23SNeil Horman 		goto out;
50077a2b73aSVasu Dev 
5011a7b75aeSRobert Love 	/*
5021a7b75aeSRobert Love 	 * Update the exchange and sequence flags,
5031a7b75aeSRobert Love 	 * assuming all frames for the sequence have been sent.
5041a7b75aeSRobert Love 	 * We can only be called to send once for each sequence.
5051a7b75aeSRobert Love 	 */
5061a7b75aeSRobert Love 	ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ;	/* not first seq */
507cc3593d3SJoe Eykholt 	if (f_ctl & FC_FC_SEQ_INIT)
5081a7b75aeSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
509fb00cc23SNeil Horman out:
510fb00cc23SNeil Horman 	return error;
511fb00cc23SNeil Horman }
512fb00cc23SNeil Horman 
513fb00cc23SNeil Horman /**
514fb00cc23SNeil Horman  * fc_seq_send() - Send a frame using existing sequence/exchange pair
515fb00cc23SNeil Horman  * @lport: The local port that the exchange will be sent on
516fb00cc23SNeil Horman  * @sp:	   The sequence to be sent
517fb00cc23SNeil Horman  * @fp:	   The frame to be sent on the exchange
518fb00cc23SNeil Horman  */
519fb00cc23SNeil Horman static int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp,
520fb00cc23SNeil Horman 		       struct fc_frame *fp)
521fb00cc23SNeil Horman {
522fb00cc23SNeil Horman 	struct fc_exch *ep;
523fb00cc23SNeil Horman 	int error;
524fb00cc23SNeil Horman 	ep = fc_seq_exch(sp);
525fb00cc23SNeil Horman 	spin_lock_bh(&ep->ex_lock);
526fb00cc23SNeil Horman 	error = fc_seq_send_locked(lport, sp, fp);
5271a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
5281a7b75aeSRobert Love 	return error;
5291a7b75aeSRobert Love }
5301a7b75aeSRobert Love 
5311a7b75aeSRobert Love /**
5323a3b42bfSRobert Love  * fc_seq_alloc() - Allocate a sequence for a given exchange
5333a3b42bfSRobert Love  * @ep:	    The exchange to allocate a new sequence for
5343a3b42bfSRobert Love  * @seq_id: The sequence ID to be used
5351a7b75aeSRobert Love  *
5361a7b75aeSRobert Love  * We don't support multiple originated sequences on the same exchange.
5371a7b75aeSRobert Love  * By implication, any previously originated sequence on this exchange
5381a7b75aeSRobert Love  * is complete, and we reallocate the same sequence.
5391a7b75aeSRobert Love  */
5401a7b75aeSRobert Love static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
5411a7b75aeSRobert Love {
5421a7b75aeSRobert Love 	struct fc_seq *sp;
5431a7b75aeSRobert Love 
5441a7b75aeSRobert Love 	sp = &ep->seq;
5451a7b75aeSRobert Love 	sp->ssb_stat = 0;
5461a7b75aeSRobert Love 	sp->cnt = 0;
5471a7b75aeSRobert Love 	sp->id = seq_id;
5481a7b75aeSRobert Love 	return sp;
5491a7b75aeSRobert Love }
5501a7b75aeSRobert Love 
5513a3b42bfSRobert Love /**
5523a3b42bfSRobert Love  * fc_seq_start_next_locked() - Allocate a new sequence on the same
5533a3b42bfSRobert Love  *				exchange as the supplied sequence
5543a3b42bfSRobert Love  * @sp: The sequence/exchange to get a new sequence for
5553a3b42bfSRobert Love  */
5561a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
5571a7b75aeSRobert Love {
5581a7b75aeSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
5591a7b75aeSRobert Love 
5601a7b75aeSRobert Love 	sp = fc_seq_alloc(ep, ep->seq_id++);
5611a7b75aeSRobert Love 	FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
5621a7b75aeSRobert Love 		    ep->f_ctl, sp->id);
5631a7b75aeSRobert Love 	return sp;
5641a7b75aeSRobert Love }
5651a7b75aeSRobert Love 
5661a7b75aeSRobert Love /**
5673a3b42bfSRobert Love  * fc_seq_start_next() - Lock the exchange and get a new sequence
5683a3b42bfSRobert Love  *			 for a given sequence/exchange pair
5693a3b42bfSRobert Love  * @sp: The sequence/exchange to get a new exchange for
5701a7b75aeSRobert Love  */
5711a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
5721a7b75aeSRobert Love {
5731a7b75aeSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
5741a7b75aeSRobert Love 
5751a7b75aeSRobert Love 	spin_lock_bh(&ep->ex_lock);
5761a7b75aeSRobert Love 	sp = fc_seq_start_next_locked(sp);
5771a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
5781a7b75aeSRobert Love 
5791a7b75aeSRobert Love 	return sp;
5801a7b75aeSRobert Love }
5811a7b75aeSRobert Love 
5821a5c2d7eSJoe Eykholt /*
5831a5c2d7eSJoe Eykholt  * Set the response handler for the exchange associated with a sequence.
5841a5c2d7eSJoe Eykholt  */
5851a5c2d7eSJoe Eykholt static void fc_seq_set_resp(struct fc_seq *sp,
5861a5c2d7eSJoe Eykholt 			    void (*resp)(struct fc_seq *, struct fc_frame *,
5871a5c2d7eSJoe Eykholt 					 void *),
5881a5c2d7eSJoe Eykholt 			    void *arg)
5891a5c2d7eSJoe Eykholt {
5901a5c2d7eSJoe Eykholt 	struct fc_exch *ep = fc_seq_exch(sp);
5911a5c2d7eSJoe Eykholt 
5921a5c2d7eSJoe Eykholt 	spin_lock_bh(&ep->ex_lock);
5931a5c2d7eSJoe Eykholt 	ep->resp = resp;
5941a5c2d7eSJoe Eykholt 	ep->arg = arg;
5951a5c2d7eSJoe Eykholt 	spin_unlock_bh(&ep->ex_lock);
5961a5c2d7eSJoe Eykholt }
5971a5c2d7eSJoe Eykholt 
5981a7b75aeSRobert Love /**
59977a2b73aSVasu Dev  * fc_exch_abort_locked() - Abort an exchange
60077a2b73aSVasu Dev  * @ep:	The exchange to be aborted
6013a3b42bfSRobert Love  * @timer_msec: The period of time to wait before aborting
6023a3b42bfSRobert Love  *
60377a2b73aSVasu Dev  * Locking notes:  Called with exch lock held
60477a2b73aSVasu Dev  *
60577a2b73aSVasu Dev  * Return value: 0 on success else error code
6061a7b75aeSRobert Love  */
60777a2b73aSVasu Dev static int fc_exch_abort_locked(struct fc_exch *ep,
6081a7b75aeSRobert Love 				unsigned int timer_msec)
60942e9a92fSRobert Love {
61042e9a92fSRobert Love 	struct fc_seq *sp;
61142e9a92fSRobert Love 	struct fc_frame *fp;
61242e9a92fSRobert Love 	int error;
61342e9a92fSRobert Love 
61442e9a92fSRobert Love 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
61577a2b73aSVasu Dev 	    ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP))
61642e9a92fSRobert Love 		return -ENXIO;
61742e9a92fSRobert Love 
61842e9a92fSRobert Love 	/*
61942e9a92fSRobert Love 	 * Send the abort on a new sequence if possible.
62042e9a92fSRobert Love 	 */
62142e9a92fSRobert Love 	sp = fc_seq_start_next_locked(&ep->seq);
62277a2b73aSVasu Dev 	if (!sp)
62342e9a92fSRobert Love 		return -ENOMEM;
62442e9a92fSRobert Love 
62542e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
62642e9a92fSRobert Love 	if (timer_msec)
62742e9a92fSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
62842e9a92fSRobert Love 
62942e9a92fSRobert Love 	/*
63042e9a92fSRobert Love 	 * If not logged into the fabric, don't send ABTS but leave
63142e9a92fSRobert Love 	 * sequence active until next timeout.
63242e9a92fSRobert Love 	 */
63342e9a92fSRobert Love 	if (!ep->sid)
63442e9a92fSRobert Love 		return 0;
63542e9a92fSRobert Love 
63642e9a92fSRobert Love 	/*
63742e9a92fSRobert Love 	 * Send an abort for the sequence that timed out.
63842e9a92fSRobert Love 	 */
63942e9a92fSRobert Love 	fp = fc_frame_alloc(ep->lp, 0);
64042e9a92fSRobert Love 	if (fp) {
64142e9a92fSRobert Love 		fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
64242e9a92fSRobert Love 			       FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
643fb00cc23SNeil Horman 		error = fc_seq_send_locked(ep->lp, sp, fp);
64442e9a92fSRobert Love 	} else
64542e9a92fSRobert Love 		error = -ENOBUFS;
64642e9a92fSRobert Love 	return error;
64742e9a92fSRobert Love }
64842e9a92fSRobert Love 
6493a3b42bfSRobert Love /**
65077a2b73aSVasu Dev  * fc_seq_exch_abort() - Abort an exchange and sequence
65177a2b73aSVasu Dev  * @req_sp:	The sequence to be aborted
65277a2b73aSVasu Dev  * @timer_msec: The period of time to wait before aborting
65377a2b73aSVasu Dev  *
65477a2b73aSVasu Dev  * Generally called because of a timeout or an abort from the upper layer.
65577a2b73aSVasu Dev  *
65677a2b73aSVasu Dev  * Return value: 0 on success else error code
65777a2b73aSVasu Dev  */
65877a2b73aSVasu Dev static int fc_seq_exch_abort(const struct fc_seq *req_sp,
65977a2b73aSVasu Dev 			     unsigned int timer_msec)
66077a2b73aSVasu Dev {
66177a2b73aSVasu Dev 	struct fc_exch *ep;
66277a2b73aSVasu Dev 	int error;
66377a2b73aSVasu Dev 
66477a2b73aSVasu Dev 	ep = fc_seq_exch(req_sp);
66577a2b73aSVasu Dev 	spin_lock_bh(&ep->ex_lock);
66677a2b73aSVasu Dev 	error = fc_exch_abort_locked(ep, timer_msec);
66777a2b73aSVasu Dev 	spin_unlock_bh(&ep->ex_lock);
66877a2b73aSVasu Dev 	return error;
66977a2b73aSVasu Dev }
67077a2b73aSVasu Dev 
67177a2b73aSVasu Dev /**
6723a3b42bfSRobert Love  * fc_exch_timeout() - Handle exchange timer expiration
6733a3b42bfSRobert Love  * @work: The work_struct identifying the exchange that timed out
67442e9a92fSRobert Love  */
67542e9a92fSRobert Love static void fc_exch_timeout(struct work_struct *work)
67642e9a92fSRobert Love {
67742e9a92fSRobert Love 	struct fc_exch *ep = container_of(work, struct fc_exch,
67842e9a92fSRobert Love 					  timeout_work.work);
67942e9a92fSRobert Love 	struct fc_seq *sp = &ep->seq;
68042e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
68142e9a92fSRobert Love 	void *arg;
68242e9a92fSRobert Love 	u32 e_stat;
68342e9a92fSRobert Love 	int rc = 1;
68442e9a92fSRobert Love 
685cd305ce4SRobert Love 	FC_EXCH_DBG(ep, "Exchange timed out\n");
686cd305ce4SRobert Love 
68742e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
68842e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
68942e9a92fSRobert Love 		goto unlock;
69042e9a92fSRobert Love 
69142e9a92fSRobert Love 	e_stat = ep->esb_stat;
69242e9a92fSRobert Love 	if (e_stat & ESB_ST_COMPLETE) {
69342e9a92fSRobert Love 		ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
694a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
69542e9a92fSRobert Love 		if (e_stat & ESB_ST_REC_QUAL)
69642e9a92fSRobert Love 			fc_exch_rrq(ep);
69742e9a92fSRobert Love 		goto done;
69842e9a92fSRobert Love 	} else {
69942e9a92fSRobert Love 		resp = ep->resp;
70042e9a92fSRobert Love 		arg = ep->arg;
70142e9a92fSRobert Love 		ep->resp = NULL;
70242e9a92fSRobert Love 		if (e_stat & ESB_ST_ABNORMAL)
70342e9a92fSRobert Love 			rc = fc_exch_done_locked(ep);
70442e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
705f3162483SParikh, Neerav 		if (!rc)
706f3162483SParikh, Neerav 			fc_exch_delete(ep);
70742e9a92fSRobert Love 		if (resp)
70842e9a92fSRobert Love 			resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
70942e9a92fSRobert Love 		fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
71042e9a92fSRobert Love 		goto done;
71142e9a92fSRobert Love 	}
71242e9a92fSRobert Love unlock:
71342e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
71442e9a92fSRobert Love done:
71542e9a92fSRobert Love 	/*
71642e9a92fSRobert Love 	 * This release matches the hold taken when the timer was set.
71742e9a92fSRobert Love 	 */
71842e9a92fSRobert Love 	fc_exch_release(ep);
71942e9a92fSRobert Love }
72042e9a92fSRobert Love 
72152ff878cSVasu Dev /**
7223a3b42bfSRobert Love  * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
7233a3b42bfSRobert Love  * @lport: The local port that the exchange is for
7243a3b42bfSRobert Love  * @mp:	   The exchange manager that will allocate the exchange
72542e9a92fSRobert Love  *
726d7179680SVasu Dev  * Returns pointer to allocated fc_exch with exch lock held.
72742e9a92fSRobert Love  */
72852ff878cSVasu Dev static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
729d7179680SVasu Dev 					struct fc_exch_mgr *mp)
73042e9a92fSRobert Love {
73142e9a92fSRobert Love 	struct fc_exch *ep;
732b2f0091fSVasu Dev 	unsigned int cpu;
733b2f0091fSVasu Dev 	u16 index;
734b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
73542e9a92fSRobert Love 
73642e9a92fSRobert Love 	/* allocate memory for exchange */
73742e9a92fSRobert Love 	ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
73842e9a92fSRobert Love 	if (!ep) {
73942e9a92fSRobert Love 		atomic_inc(&mp->stats.no_free_exch);
74042e9a92fSRobert Love 		goto out;
74142e9a92fSRobert Love 	}
74242e9a92fSRobert Love 	memset(ep, 0, sizeof(*ep));
74342e9a92fSRobert Love 
744f018b73aSJoe Eykholt 	cpu = get_cpu();
745b2f0091fSVasu Dev 	pool = per_cpu_ptr(mp->pool, cpu);
746b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
747f018b73aSJoe Eykholt 	put_cpu();
7482034c19cSHillf Danton 
7492034c19cSHillf Danton 	/* peek cache of free slot */
7502034c19cSHillf Danton 	if (pool->left != FC_XID_UNKNOWN) {
7512034c19cSHillf Danton 		index = pool->left;
7522034c19cSHillf Danton 		pool->left = FC_XID_UNKNOWN;
7532034c19cSHillf Danton 		goto hit;
7542034c19cSHillf Danton 	}
7552034c19cSHillf Danton 	if (pool->right != FC_XID_UNKNOWN) {
7562034c19cSHillf Danton 		index = pool->right;
7572034c19cSHillf Danton 		pool->right = FC_XID_UNKNOWN;
7582034c19cSHillf Danton 		goto hit;
7592034c19cSHillf Danton 	}
7602034c19cSHillf Danton 
761b2f0091fSVasu Dev 	index = pool->next_index;
762b2f0091fSVasu Dev 	/* allocate new exch from pool */
763b2f0091fSVasu Dev 	while (fc_exch_ptr_get(pool, index)) {
764b2f0091fSVasu Dev 		index = index == mp->pool_max_index ? 0 : index + 1;
765b2f0091fSVasu Dev 		if (index == pool->next_index)
76642e9a92fSRobert Love 			goto err;
76742e9a92fSRobert Love 	}
768b2f0091fSVasu Dev 	pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
7692034c19cSHillf Danton hit:
77042e9a92fSRobert Love 	fc_exch_hold(ep);	/* hold for exch in mp */
77142e9a92fSRobert Love 	spin_lock_init(&ep->ex_lock);
77242e9a92fSRobert Love 	/*
77342e9a92fSRobert Love 	 * Hold exch lock for caller to prevent fc_exch_reset()
77442e9a92fSRobert Love 	 * from releasing exch	while fc_exch_alloc() caller is
77542e9a92fSRobert Love 	 * still working on exch.
77642e9a92fSRobert Love 	 */
77742e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
77842e9a92fSRobert Love 
779b2f0091fSVasu Dev 	fc_exch_ptr_set(pool, index, ep);
780b2f0091fSVasu Dev 	list_add_tail(&ep->ex_list, &pool->ex_list);
78142e9a92fSRobert Love 	fc_seq_alloc(ep, ep->seq_id++);
782b2f0091fSVasu Dev 	pool->total_exches++;
783b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
78442e9a92fSRobert Love 
78542e9a92fSRobert Love 	/*
78642e9a92fSRobert Love 	 *  update exchange
78742e9a92fSRobert Love 	 */
788b2f0091fSVasu Dev 	ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
78942e9a92fSRobert Love 	ep->em = mp;
790b2f0091fSVasu Dev 	ep->pool = pool;
79152ff878cSVasu Dev 	ep->lp = lport;
79242e9a92fSRobert Love 	ep->f_ctl = FC_FC_FIRST_SEQ;	/* next seq is first seq */
79342e9a92fSRobert Love 	ep->rxid = FC_XID_UNKNOWN;
79442e9a92fSRobert Love 	ep->class = mp->class;
79542e9a92fSRobert Love 	INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
79642e9a92fSRobert Love out:
79742e9a92fSRobert Love 	return ep;
79842e9a92fSRobert Love err:
799b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
80042e9a92fSRobert Love 	atomic_inc(&mp->stats.no_free_exch_xid);
80142e9a92fSRobert Love 	mempool_free(ep, mp->ep_pool);
80242e9a92fSRobert Love 	return NULL;
80342e9a92fSRobert Love }
80452ff878cSVasu Dev 
80552ff878cSVasu Dev /**
8063a3b42bfSRobert Love  * fc_exch_alloc() - Allocate an exchange from an EM on a
8073a3b42bfSRobert Love  *		     local port's list of EMs.
8083a3b42bfSRobert Love  * @lport: The local port that will own the exchange
8093a3b42bfSRobert Love  * @fp:	   The FC frame that the exchange will be for
81052ff878cSVasu Dev  *
8113a3b42bfSRobert Love  * This function walks the list of exchange manager(EM)
8123a3b42bfSRobert Love  * anchors to select an EM for a new exchange allocation. The
8133a3b42bfSRobert Love  * EM is selected when a NULL match function pointer is encountered
8143a3b42bfSRobert Love  * or when a call to a match function returns true.
81552ff878cSVasu Dev  */
8163e22760dSVasu Dev static inline struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
8171a7b75aeSRobert Love 					    struct fc_frame *fp)
81852ff878cSVasu Dev {
81952ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
82052ff878cSVasu Dev 
8213e22760dSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list)
8223e22760dSVasu Dev 		if (!ema->match || ema->match(fp))
8233e22760dSVasu Dev 			return fc_exch_em_alloc(lport, ema->mp);
82452ff878cSVasu Dev 	return NULL;
82552ff878cSVasu Dev }
82642e9a92fSRobert Love 
8273a3b42bfSRobert Love /**
8283a3b42bfSRobert Love  * fc_exch_find() - Lookup and hold an exchange
8293a3b42bfSRobert Love  * @mp:	 The exchange manager to lookup the exchange from
8303a3b42bfSRobert Love  * @xid: The XID of the exchange to look up
83142e9a92fSRobert Love  */
83242e9a92fSRobert Love static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
83342e9a92fSRobert Love {
834b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
83542e9a92fSRobert Love 	struct fc_exch *ep = NULL;
83642e9a92fSRobert Love 
83742e9a92fSRobert Love 	if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
838b2f0091fSVasu Dev 		pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
839b2f0091fSVasu Dev 		spin_lock_bh(&pool->lock);
840b2f0091fSVasu Dev 		ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
841324f6678SVasu Dev 		if (ep && ep->xid == xid)
84242e9a92fSRobert Love 			fc_exch_hold(ep);
843b2f0091fSVasu Dev 		spin_unlock_bh(&pool->lock);
84442e9a92fSRobert Love 	}
84542e9a92fSRobert Love 	return ep;
84642e9a92fSRobert Love }
84742e9a92fSRobert Love 
8481a7b75aeSRobert Love 
8491a7b75aeSRobert Love /**
8501a7b75aeSRobert Love  * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
8511a7b75aeSRobert Love  *		    the memory allocated for the related objects may be freed.
8523a3b42bfSRobert Love  * @sp: The sequence that has completed
8531a7b75aeSRobert Love  */
8541a7b75aeSRobert Love static void fc_exch_done(struct fc_seq *sp)
85542e9a92fSRobert Love {
85642e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
85742e9a92fSRobert Love 	int rc;
85842e9a92fSRobert Love 
85942e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
86042e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
86142e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
86242e9a92fSRobert Love 	if (!rc)
863b2f0091fSVasu Dev 		fc_exch_delete(ep);
86442e9a92fSRobert Love }
86542e9a92fSRobert Love 
8663a3b42bfSRobert Love /**
8673a3b42bfSRobert Love  * fc_exch_resp() - Allocate a new exchange for a response frame
8683a3b42bfSRobert Love  * @lport: The local port that the exchange was for
8693a3b42bfSRobert Love  * @mp:	   The exchange manager to allocate the exchange from
8703a3b42bfSRobert Love  * @fp:	   The response frame
8713a3b42bfSRobert Love  *
87242e9a92fSRobert Love  * Sets the responder ID in the frame header.
87342e9a92fSRobert Love  */
87452ff878cSVasu Dev static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
87552ff878cSVasu Dev 				    struct fc_exch_mgr *mp,
87652ff878cSVasu Dev 				    struct fc_frame *fp)
87742e9a92fSRobert Love {
87842e9a92fSRobert Love 	struct fc_exch *ep;
87942e9a92fSRobert Love 	struct fc_frame_header *fh;
88042e9a92fSRobert Love 
88152ff878cSVasu Dev 	ep = fc_exch_alloc(lport, fp);
88242e9a92fSRobert Love 	if (ep) {
88342e9a92fSRobert Love 		ep->class = fc_frame_class(fp);
88442e9a92fSRobert Love 
88542e9a92fSRobert Love 		/*
88642e9a92fSRobert Love 		 * Set EX_CTX indicating we're responding on this exchange.
88742e9a92fSRobert Love 		 */
88842e9a92fSRobert Love 		ep->f_ctl |= FC_FC_EX_CTX;	/* we're responding */
88942e9a92fSRobert Love 		ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not new */
89042e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
89142e9a92fSRobert Love 		ep->sid = ntoh24(fh->fh_d_id);
89242e9a92fSRobert Love 		ep->did = ntoh24(fh->fh_s_id);
89342e9a92fSRobert Love 		ep->oid = ep->did;
89442e9a92fSRobert Love 
89542e9a92fSRobert Love 		/*
89642e9a92fSRobert Love 		 * Allocated exchange has placed the XID in the
89742e9a92fSRobert Love 		 * originator field. Move it to the responder field,
89842e9a92fSRobert Love 		 * and set the originator XID from the frame.
89942e9a92fSRobert Love 		 */
90042e9a92fSRobert Love 		ep->rxid = ep->xid;
90142e9a92fSRobert Love 		ep->oxid = ntohs(fh->fh_ox_id);
90242e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
90342e9a92fSRobert Love 		if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
90442e9a92fSRobert Love 			ep->esb_stat &= ~ESB_ST_SEQ_INIT;
90542e9a92fSRobert Love 
90642e9a92fSRobert Love 		fc_exch_hold(ep);	/* hold for caller */
90752ff878cSVasu Dev 		spin_unlock_bh(&ep->ex_lock);	/* lock from fc_exch_alloc */
90842e9a92fSRobert Love 	}
90942e9a92fSRobert Love 	return ep;
91042e9a92fSRobert Love }
91142e9a92fSRobert Love 
9123a3b42bfSRobert Love /**
9133a3b42bfSRobert Love  * fc_seq_lookup_recip() - Find a sequence where the other end
9143a3b42bfSRobert Love  *			   originated the sequence
9153a3b42bfSRobert Love  * @lport: The local port that the frame was sent to
9163a3b42bfSRobert Love  * @mp:	   The Exchange Manager to lookup the exchange from
9173a3b42bfSRobert Love  * @fp:	   The frame associated with the sequence we're looking for
9183a3b42bfSRobert Love  *
91942e9a92fSRobert Love  * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
92042e9a92fSRobert Love  * on the ep that should be released by the caller.
92142e9a92fSRobert Love  */
92252ff878cSVasu Dev static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
92352ff878cSVasu Dev 						 struct fc_exch_mgr *mp,
924b2ab99c9SRobert Love 						 struct fc_frame *fp)
92542e9a92fSRobert Love {
92642e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
92742e9a92fSRobert Love 	struct fc_exch *ep = NULL;
92842e9a92fSRobert Love 	struct fc_seq *sp = NULL;
92942e9a92fSRobert Love 	enum fc_pf_rjt_reason reject = FC_RJT_NONE;
93042e9a92fSRobert Love 	u32 f_ctl;
93142e9a92fSRobert Love 	u16 xid;
93242e9a92fSRobert Love 
93342e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
93442e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
93542e9a92fSRobert Love 
93642e9a92fSRobert Love 	/*
93742e9a92fSRobert Love 	 * Lookup or create the exchange if we will be creating the sequence.
93842e9a92fSRobert Love 	 */
93942e9a92fSRobert Love 	if (f_ctl & FC_FC_EX_CTX) {
94042e9a92fSRobert Love 		xid = ntohs(fh->fh_ox_id);	/* we originated exch */
94142e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
94242e9a92fSRobert Love 		if (!ep) {
94342e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
94442e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
94542e9a92fSRobert Love 			goto out;
94642e9a92fSRobert Love 		}
94742e9a92fSRobert Love 		if (ep->rxid == FC_XID_UNKNOWN)
94842e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
94942e9a92fSRobert Love 		else if (ep->rxid != ntohs(fh->fh_rx_id)) {
95042e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
95142e9a92fSRobert Love 			goto rel;
95242e9a92fSRobert Love 		}
95342e9a92fSRobert Love 	} else {
95442e9a92fSRobert Love 		xid = ntohs(fh->fh_rx_id);	/* we are the responder */
95542e9a92fSRobert Love 
95642e9a92fSRobert Love 		/*
95742e9a92fSRobert Love 		 * Special case for MDS issuing an ELS TEST with a
95842e9a92fSRobert Love 		 * bad rxid of 0.
95942e9a92fSRobert Love 		 * XXX take this out once we do the proper reject.
96042e9a92fSRobert Love 		 */
96142e9a92fSRobert Love 		if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
96242e9a92fSRobert Love 		    fc_frame_payload_op(fp) == ELS_TEST) {
96342e9a92fSRobert Love 			fh->fh_rx_id = htons(FC_XID_UNKNOWN);
96442e9a92fSRobert Love 			xid = FC_XID_UNKNOWN;
96542e9a92fSRobert Love 		}
96642e9a92fSRobert Love 
96742e9a92fSRobert Love 		/*
96842e9a92fSRobert Love 		 * new sequence - find the exchange
96942e9a92fSRobert Love 		 */
97042e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
97142e9a92fSRobert Love 		if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
97242e9a92fSRobert Love 			if (ep) {
97342e9a92fSRobert Love 				atomic_inc(&mp->stats.xid_busy);
97442e9a92fSRobert Love 				reject = FC_RJT_RX_ID;
97542e9a92fSRobert Love 				goto rel;
97642e9a92fSRobert Love 			}
97752ff878cSVasu Dev 			ep = fc_exch_resp(lport, mp, fp);
97842e9a92fSRobert Love 			if (!ep) {
97942e9a92fSRobert Love 				reject = FC_RJT_EXCH_EST;	/* XXX */
98042e9a92fSRobert Love 				goto out;
98142e9a92fSRobert Love 			}
98242e9a92fSRobert Love 			xid = ep->xid;	/* get our XID */
98342e9a92fSRobert Love 		} else if (!ep) {
98442e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
98542e9a92fSRobert Love 			reject = FC_RJT_RX_ID;	/* XID not found */
98642e9a92fSRobert Love 			goto out;
98742e9a92fSRobert Love 		}
98842e9a92fSRobert Love 	}
98942e9a92fSRobert Love 
99042e9a92fSRobert Love 	/*
99142e9a92fSRobert Love 	 * At this point, we have the exchange held.
99242e9a92fSRobert Love 	 * Find or create the sequence.
99342e9a92fSRobert Love 	 */
99442e9a92fSRobert Love 	if (fc_sof_is_init(fr_sof(fp))) {
995a104c844SVasu Dev 		sp = &ep->seq;
99642e9a92fSRobert Love 		sp->ssb_stat |= SSB_ST_RESP;
997b3667f91SJoe Eykholt 		sp->id = fh->fh_seq_id;
99842e9a92fSRobert Love 	} else {
99942e9a92fSRobert Love 		sp = &ep->seq;
100042e9a92fSRobert Love 		if (sp->id != fh->fh_seq_id) {
100142e9a92fSRobert Love 			atomic_inc(&mp->stats.seq_not_found);
1002e3e65c69SKiran Patil 			if (f_ctl & FC_FC_END_SEQ) {
1003e3e65c69SKiran Patil 				/*
1004e3e65c69SKiran Patil 				 * Update sequence_id based on incoming last
1005e3e65c69SKiran Patil 				 * frame of sequence exchange. This is needed
10061bd49b48SVasu Dev 				 * for FC target where DDP has been used
1007e3e65c69SKiran Patil 				 * on target where, stack is indicated only
1008e3e65c69SKiran Patil 				 * about last frame's (payload _header) header.
1009e3e65c69SKiran Patil 				 * Whereas "seq_id" which is part of
1010e3e65c69SKiran Patil 				 * frame_header is allocated by initiator
1011e3e65c69SKiran Patil 				 * which is totally different from "seq_id"
1012e3e65c69SKiran Patil 				 * allocated when XFER_RDY was sent by target.
1013e3e65c69SKiran Patil 				 * To avoid false -ve which results into not
1014e3e65c69SKiran Patil 				 * sending RSP, hence write request on other
1015e3e65c69SKiran Patil 				 * end never finishes.
1016e3e65c69SKiran Patil 				 */
1017e3e65c69SKiran Patil 				spin_lock_bh(&ep->ex_lock);
1018e3e65c69SKiran Patil 				sp->ssb_stat |= SSB_ST_RESP;
1019e3e65c69SKiran Patil 				sp->id = fh->fh_seq_id;
1020e3e65c69SKiran Patil 				spin_unlock_bh(&ep->ex_lock);
1021e3e65c69SKiran Patil 			} else {
1022e3e65c69SKiran Patil 				/* sequence/exch should exist */
1023e3e65c69SKiran Patil 				reject = FC_RJT_SEQ_ID;
102442e9a92fSRobert Love 				goto rel;
102542e9a92fSRobert Love 			}
102642e9a92fSRobert Love 		}
1027e3e65c69SKiran Patil 	}
102842e9a92fSRobert Love 	WARN_ON(ep != fc_seq_exch(sp));
102942e9a92fSRobert Love 
103042e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
103142e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
103242e9a92fSRobert Love 
103342e9a92fSRobert Love 	fr_seq(fp) = sp;
103442e9a92fSRobert Love out:
103542e9a92fSRobert Love 	return reject;
103642e9a92fSRobert Love rel:
103742e9a92fSRobert Love 	fc_exch_done(&ep->seq);
103842e9a92fSRobert Love 	fc_exch_release(ep);	/* hold from fc_exch_find/fc_exch_resp */
103942e9a92fSRobert Love 	return reject;
104042e9a92fSRobert Love }
104142e9a92fSRobert Love 
10423a3b42bfSRobert Love /**
10433a3b42bfSRobert Love  * fc_seq_lookup_orig() - Find a sequence where this end
10443a3b42bfSRobert Love  *			  originated the sequence
10453a3b42bfSRobert Love  * @mp:	   The Exchange Manager to lookup the exchange from
10463a3b42bfSRobert Love  * @fp:	   The frame associated with the sequence we're looking for
10473a3b42bfSRobert Love  *
104842e9a92fSRobert Love  * Does not hold the sequence for the caller.
104942e9a92fSRobert Love  */
105042e9a92fSRobert Love static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
105142e9a92fSRobert Love 					 struct fc_frame *fp)
105242e9a92fSRobert Love {
105342e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
105442e9a92fSRobert Love 	struct fc_exch *ep;
105542e9a92fSRobert Love 	struct fc_seq *sp = NULL;
105642e9a92fSRobert Love 	u32 f_ctl;
105742e9a92fSRobert Love 	u16 xid;
105842e9a92fSRobert Love 
105942e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
106042e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
106142e9a92fSRobert Love 	xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
106242e9a92fSRobert Love 	ep = fc_exch_find(mp, xid);
106342e9a92fSRobert Love 	if (!ep)
106442e9a92fSRobert Love 		return NULL;
106542e9a92fSRobert Love 	if (ep->seq.id == fh->fh_seq_id) {
106642e9a92fSRobert Love 		/*
106742e9a92fSRobert Love 		 * Save the RX_ID if we didn't previously know it.
106842e9a92fSRobert Love 		 */
106942e9a92fSRobert Love 		sp = &ep->seq;
107042e9a92fSRobert Love 		if ((f_ctl & FC_FC_EX_CTX) != 0 &&
107142e9a92fSRobert Love 		    ep->rxid == FC_XID_UNKNOWN) {
107242e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
107342e9a92fSRobert Love 		}
107442e9a92fSRobert Love 	}
107542e9a92fSRobert Love 	fc_exch_release(ep);
107642e9a92fSRobert Love 	return sp;
107742e9a92fSRobert Love }
107842e9a92fSRobert Love 
10793a3b42bfSRobert Love /**
10803a3b42bfSRobert Love  * fc_exch_set_addr() - Set the source and destination IDs for an exchange
10813a3b42bfSRobert Love  * @ep:	     The exchange to set the addresses for
10823a3b42bfSRobert Love  * @orig_id: The originator's ID
10833a3b42bfSRobert Love  * @resp_id: The responder's ID
10843a3b42bfSRobert Love  *
108542e9a92fSRobert Love  * Note this must be done before the first sequence of the exchange is sent.
108642e9a92fSRobert Love  */
108742e9a92fSRobert Love static void fc_exch_set_addr(struct fc_exch *ep,
108842e9a92fSRobert Love 			     u32 orig_id, u32 resp_id)
108942e9a92fSRobert Love {
109042e9a92fSRobert Love 	ep->oid = orig_id;
109142e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP) {
109242e9a92fSRobert Love 		ep->sid = resp_id;
109342e9a92fSRobert Love 		ep->did = orig_id;
109442e9a92fSRobert Love 	} else {
109542e9a92fSRobert Love 		ep->sid = orig_id;
109642e9a92fSRobert Love 		ep->did = resp_id;
109742e9a92fSRobert Love 	}
109842e9a92fSRobert Love }
109942e9a92fSRobert Love 
11001a7b75aeSRobert Love /**
110125985edcSLucas De Marchi  * fc_seq_els_rsp_send() - Send an ELS response using information from
11023a3b42bfSRobert Love  *			   the existing sequence/exchange.
110392261156SJoe Eykholt  * @fp:	      The received frame
11043a3b42bfSRobert Love  * @els_cmd:  The ELS command to be sent
11053a3b42bfSRobert Love  * @els_data: The ELS data to be sent
110692261156SJoe Eykholt  *
110792261156SJoe Eykholt  * The received frame is not freed.
110842e9a92fSRobert Love  */
110992261156SJoe Eykholt static void fc_seq_els_rsp_send(struct fc_frame *fp, enum fc_els_cmd els_cmd,
111042e9a92fSRobert Love 				struct fc_seq_els_data *els_data)
111142e9a92fSRobert Love {
111242e9a92fSRobert Love 	switch (els_cmd) {
111342e9a92fSRobert Love 	case ELS_LS_RJT:
111492261156SJoe Eykholt 		fc_seq_ls_rjt(fp, els_data->reason, els_data->explan);
111542e9a92fSRobert Love 		break;
111642e9a92fSRobert Love 	case ELS_LS_ACC:
111792261156SJoe Eykholt 		fc_seq_ls_acc(fp);
111842e9a92fSRobert Love 		break;
111942e9a92fSRobert Love 	case ELS_RRQ:
112092261156SJoe Eykholt 		fc_exch_els_rrq(fp);
112142e9a92fSRobert Love 		break;
112242e9a92fSRobert Love 	case ELS_REC:
112392261156SJoe Eykholt 		fc_exch_els_rec(fp);
112442e9a92fSRobert Love 		break;
112542e9a92fSRobert Love 	default:
112692261156SJoe Eykholt 		FC_LPORT_DBG(fr_dev(fp), "Invalid ELS CMD:%x\n", els_cmd);
112742e9a92fSRobert Love 	}
112842e9a92fSRobert Love }
112942e9a92fSRobert Love 
11303a3b42bfSRobert Love /**
11313a3b42bfSRobert Love  * fc_seq_send_last() - Send a sequence that is the last in the exchange
11323a3b42bfSRobert Love  * @sp:	     The sequence that is to be sent
11333a3b42bfSRobert Love  * @fp:	     The frame that will be sent on the sequence
11343a3b42bfSRobert Love  * @rctl:    The R_CTL information to be sent
11353a3b42bfSRobert Love  * @fh_type: The frame header type
113642e9a92fSRobert Love  */
113742e9a92fSRobert Love static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
113842e9a92fSRobert Love 			     enum fc_rctl rctl, enum fc_fh_type fh_type)
113942e9a92fSRobert Love {
114042e9a92fSRobert Love 	u32 f_ctl;
114142e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
114242e9a92fSRobert Love 
114342e9a92fSRobert Love 	f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
114442e9a92fSRobert Love 	f_ctl |= ep->f_ctl;
114542e9a92fSRobert Love 	fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
1146fb00cc23SNeil Horman 	fc_seq_send_locked(ep->lp, sp, fp);
114742e9a92fSRobert Love }
114842e9a92fSRobert Love 
11493a3b42bfSRobert Love /**
11503a3b42bfSRobert Love  * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
11513a3b42bfSRobert Love  * @sp:	   The sequence to send the ACK on
11523a3b42bfSRobert Love  * @rx_fp: The received frame that is being acknoledged
11533a3b42bfSRobert Love  *
115442e9a92fSRobert Love  * Send ACK_1 (or equiv.) indicating we received something.
115542e9a92fSRobert Love  */
115642e9a92fSRobert Love static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
115742e9a92fSRobert Love {
115842e9a92fSRobert Love 	struct fc_frame *fp;
115942e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
116042e9a92fSRobert Love 	struct fc_frame_header *fh;
116142e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
11623a3b42bfSRobert Love 	struct fc_lport *lport = ep->lp;
116342e9a92fSRobert Love 	unsigned int f_ctl;
116442e9a92fSRobert Love 
116542e9a92fSRobert Love 	/*
116642e9a92fSRobert Love 	 * Don't send ACKs for class 3.
116742e9a92fSRobert Love 	 */
116842e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(rx_fp))) {
11693a3b42bfSRobert Love 		fp = fc_frame_alloc(lport, 0);
117042e9a92fSRobert Love 		if (!fp)
117142e9a92fSRobert Love 			return;
117242e9a92fSRobert Love 
117342e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
117442e9a92fSRobert Love 		fh->fh_r_ctl = FC_RCTL_ACK_1;
117542e9a92fSRobert Love 		fh->fh_type = FC_TYPE_BLS;
117642e9a92fSRobert Love 
117742e9a92fSRobert Love 		/*
117842e9a92fSRobert Love 		 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
117942e9a92fSRobert Love 		 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
118042e9a92fSRobert Love 		 * Bits 9-8 are meaningful (retransmitted or unidirectional).
118142e9a92fSRobert Love 		 * Last ACK uses bits 7-6 (continue sequence),
118242e9a92fSRobert Love 		 * bits 5-4 are meaningful (what kind of ACK to use).
118342e9a92fSRobert Love 		 */
118442e9a92fSRobert Love 		rx_fh = fc_frame_header_get(rx_fp);
118542e9a92fSRobert Love 		f_ctl = ntoh24(rx_fh->fh_f_ctl);
118642e9a92fSRobert Love 		f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
118742e9a92fSRobert Love 			FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
118842e9a92fSRobert Love 			FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
118942e9a92fSRobert Love 			FC_FC_RETX_SEQ | FC_FC_UNI_TX;
119042e9a92fSRobert Love 		f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
119142e9a92fSRobert Love 		hton24(fh->fh_f_ctl, f_ctl);
119242e9a92fSRobert Love 
119342e9a92fSRobert Love 		fc_exch_setup_hdr(ep, fp, f_ctl);
119442e9a92fSRobert Love 		fh->fh_seq_id = rx_fh->fh_seq_id;
119542e9a92fSRobert Love 		fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
119642e9a92fSRobert Love 		fh->fh_parm_offset = htonl(1);	/* ack single frame */
119742e9a92fSRobert Love 
119842e9a92fSRobert Love 		fr_sof(fp) = fr_sof(rx_fp);
119942e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
120042e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_T;
120142e9a92fSRobert Love 		else
120242e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
120342e9a92fSRobert Love 
12043a3b42bfSRobert Love 		lport->tt.frame_send(lport, fp);
120542e9a92fSRobert Love 	}
120642e9a92fSRobert Love }
120742e9a92fSRobert Love 
12083a3b42bfSRobert Love /**
12093a3b42bfSRobert Love  * fc_exch_send_ba_rjt() - Send BLS Reject
12103a3b42bfSRobert Love  * @rx_fp:  The frame being rejected
12113a3b42bfSRobert Love  * @reason: The reason the frame is being rejected
121225985edcSLucas De Marchi  * @explan: The explanation for the rejection
12133a3b42bfSRobert Love  *
121442e9a92fSRobert Love  * This is for rejecting BA_ABTS only.
121542e9a92fSRobert Love  */
1216b2ab99c9SRobert Love static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1217b2ab99c9SRobert Love 				enum fc_ba_rjt_reason reason,
121842e9a92fSRobert Love 				enum fc_ba_rjt_explan explan)
121942e9a92fSRobert Love {
122042e9a92fSRobert Love 	struct fc_frame *fp;
122142e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
122242e9a92fSRobert Love 	struct fc_frame_header *fh;
122342e9a92fSRobert Love 	struct fc_ba_rjt *rp;
12243a3b42bfSRobert Love 	struct fc_lport *lport;
122542e9a92fSRobert Love 	unsigned int f_ctl;
122642e9a92fSRobert Love 
12273a3b42bfSRobert Love 	lport = fr_dev(rx_fp);
12283a3b42bfSRobert Love 	fp = fc_frame_alloc(lport, sizeof(*rp));
122942e9a92fSRobert Love 	if (!fp)
123042e9a92fSRobert Love 		return;
123142e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
123242e9a92fSRobert Love 	rx_fh = fc_frame_header_get(rx_fp);
123342e9a92fSRobert Love 
123442e9a92fSRobert Love 	memset(fh, 0, sizeof(*fh) + sizeof(*rp));
123542e9a92fSRobert Love 
123642e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
123742e9a92fSRobert Love 	rp->br_reason = reason;
123842e9a92fSRobert Love 	rp->br_explan = explan;
123942e9a92fSRobert Love 
124042e9a92fSRobert Love 	/*
124142e9a92fSRobert Love 	 * seq_id, cs_ctl, df_ctl and param/offset are zero.
124242e9a92fSRobert Love 	 */
124342e9a92fSRobert Love 	memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
124442e9a92fSRobert Love 	memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
12451d490ce3SJoe Eykholt 	fh->fh_ox_id = rx_fh->fh_ox_id;
12461d490ce3SJoe Eykholt 	fh->fh_rx_id = rx_fh->fh_rx_id;
124742e9a92fSRobert Love 	fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
124842e9a92fSRobert Love 	fh->fh_r_ctl = FC_RCTL_BA_RJT;
124942e9a92fSRobert Love 	fh->fh_type = FC_TYPE_BLS;
125042e9a92fSRobert Love 
125142e9a92fSRobert Love 	/*
125242e9a92fSRobert Love 	 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
125342e9a92fSRobert Love 	 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
125442e9a92fSRobert Love 	 * Bits 9-8 are meaningful (retransmitted or unidirectional).
125542e9a92fSRobert Love 	 * Last ACK uses bits 7-6 (continue sequence),
125642e9a92fSRobert Love 	 * bits 5-4 are meaningful (what kind of ACK to use).
125742e9a92fSRobert Love 	 * Always set LAST_SEQ, END_SEQ.
125842e9a92fSRobert Love 	 */
125942e9a92fSRobert Love 	f_ctl = ntoh24(rx_fh->fh_f_ctl);
126042e9a92fSRobert Love 	f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
126142e9a92fSRobert Love 		FC_FC_END_CONN | FC_FC_SEQ_INIT |
126242e9a92fSRobert Love 		FC_FC_RETX_SEQ | FC_FC_UNI_TX;
126342e9a92fSRobert Love 	f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
126442e9a92fSRobert Love 	f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
126542e9a92fSRobert Love 	f_ctl &= ~FC_FC_FIRST_SEQ;
126642e9a92fSRobert Love 	hton24(fh->fh_f_ctl, f_ctl);
126742e9a92fSRobert Love 
126842e9a92fSRobert Love 	fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
126942e9a92fSRobert Love 	fr_eof(fp) = FC_EOF_T;
127042e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(fp)))
127142e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
127242e9a92fSRobert Love 
12733a3b42bfSRobert Love 	lport->tt.frame_send(lport, fp);
127442e9a92fSRobert Love }
127542e9a92fSRobert Love 
12763a3b42bfSRobert Love /**
12773a3b42bfSRobert Love  * fc_exch_recv_abts() - Handle an incoming ABTS
12783a3b42bfSRobert Love  * @ep:	   The exchange the abort was on
12793a3b42bfSRobert Love  * @rx_fp: The ABTS frame
12803a3b42bfSRobert Love  *
12813a3b42bfSRobert Love  * This would be for target mode usually, but could be due to lost
12823a3b42bfSRobert Love  * FCP transfer ready, confirm or RRQ. We always handle this as an
12833a3b42bfSRobert Love  * exchange abort, ignoring the parameter.
128442e9a92fSRobert Love  */
128542e9a92fSRobert Love static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
128642e9a92fSRobert Love {
128742e9a92fSRobert Love 	struct fc_frame *fp;
128842e9a92fSRobert Love 	struct fc_ba_acc *ap;
128942e9a92fSRobert Love 	struct fc_frame_header *fh;
129042e9a92fSRobert Love 	struct fc_seq *sp;
129142e9a92fSRobert Love 
129242e9a92fSRobert Love 	if (!ep)
129342e9a92fSRobert Love 		goto reject;
129442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
129542e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
129642e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
129742e9a92fSRobert Love 		goto reject;
129842e9a92fSRobert Love 	}
129942e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL))
130042e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for REC_QUAL */
130142e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
130242e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
130342e9a92fSRobert Love 
130442e9a92fSRobert Love 	fp = fc_frame_alloc(ep->lp, sizeof(*ap));
130542e9a92fSRobert Love 	if (!fp) {
130642e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
130742e9a92fSRobert Love 		goto free;
130842e9a92fSRobert Love 	}
130942e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
131042e9a92fSRobert Love 	ap = fc_frame_payload_get(fp, sizeof(*ap));
131142e9a92fSRobert Love 	memset(ap, 0, sizeof(*ap));
131242e9a92fSRobert Love 	sp = &ep->seq;
131342e9a92fSRobert Love 	ap->ba_high_seq_cnt = htons(0xffff);
131442e9a92fSRobert Love 	if (sp->ssb_stat & SSB_ST_RESP) {
131542e9a92fSRobert Love 		ap->ba_seq_id = sp->id;
131642e9a92fSRobert Love 		ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
131742e9a92fSRobert Love 		ap->ba_high_seq_cnt = fh->fh_seq_cnt;
131842e9a92fSRobert Love 		ap->ba_low_seq_cnt = htons(sp->cnt);
131942e9a92fSRobert Love 	}
1320a7e84f2bSVasu Dev 	sp = fc_seq_start_next_locked(sp);
132142e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1322fb00cc23SNeil Horman 	spin_unlock_bh(&ep->ex_lock);
132342e9a92fSRobert Love 	fc_frame_free(rx_fp);
132442e9a92fSRobert Love 	return;
132542e9a92fSRobert Love 
132642e9a92fSRobert Love reject:
132742e9a92fSRobert Love 	fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
132842e9a92fSRobert Love free:
132942e9a92fSRobert Love 	fc_frame_free(rx_fp);
133042e9a92fSRobert Love }
133142e9a92fSRobert Love 
13323a3b42bfSRobert Love /**
1333239e8104SJoe Eykholt  * fc_seq_assign() - Assign exchange and sequence for incoming request
1334239e8104SJoe Eykholt  * @lport: The local port that received the request
1335239e8104SJoe Eykholt  * @fp:    The request frame
1336239e8104SJoe Eykholt  *
1337239e8104SJoe Eykholt  * On success, the sequence pointer will be returned and also in fr_seq(@fp).
133862bdb645SJoe Eykholt  * A reference will be held on the exchange/sequence for the caller, which
133962bdb645SJoe Eykholt  * must call fc_seq_release().
1340239e8104SJoe Eykholt  */
1341239e8104SJoe Eykholt static struct fc_seq *fc_seq_assign(struct fc_lport *lport, struct fc_frame *fp)
1342239e8104SJoe Eykholt {
1343239e8104SJoe Eykholt 	struct fc_exch_mgr_anchor *ema;
1344239e8104SJoe Eykholt 
1345239e8104SJoe Eykholt 	WARN_ON(lport != fr_dev(fp));
1346239e8104SJoe Eykholt 	WARN_ON(fr_seq(fp));
1347239e8104SJoe Eykholt 	fr_seq(fp) = NULL;
1348239e8104SJoe Eykholt 
1349239e8104SJoe Eykholt 	list_for_each_entry(ema, &lport->ema_list, ema_list)
1350239e8104SJoe Eykholt 		if ((!ema->match || ema->match(fp)) &&
1351530994d6SHillf Danton 		    fc_seq_lookup_recip(lport, ema->mp, fp) == FC_RJT_NONE)
1352239e8104SJoe Eykholt 			break;
1353239e8104SJoe Eykholt 	return fr_seq(fp);
1354239e8104SJoe Eykholt }
1355239e8104SJoe Eykholt 
1356239e8104SJoe Eykholt /**
135762bdb645SJoe Eykholt  * fc_seq_release() - Release the hold
135862bdb645SJoe Eykholt  * @sp:    The sequence.
135962bdb645SJoe Eykholt  */
136062bdb645SJoe Eykholt static void fc_seq_release(struct fc_seq *sp)
136162bdb645SJoe Eykholt {
136262bdb645SJoe Eykholt 	fc_exch_release(fc_seq_exch(sp));
136362bdb645SJoe Eykholt }
136462bdb645SJoe Eykholt 
136562bdb645SJoe Eykholt /**
136692261156SJoe Eykholt  * fc_exch_recv_req() - Handler for an incoming request
13673a3b42bfSRobert Love  * @lport: The local port that received the request
13683a3b42bfSRobert Love  * @mp:	   The EM that the exchange is on
13693a3b42bfSRobert Love  * @fp:	   The request frame
137092261156SJoe Eykholt  *
137192261156SJoe Eykholt  * This is used when the other end is originating the exchange
137292261156SJoe Eykholt  * and the sequence.
137342e9a92fSRobert Love  */
13743a3b42bfSRobert Love static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
137542e9a92fSRobert Love 			     struct fc_frame *fp)
137642e9a92fSRobert Love {
137742e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
137842e9a92fSRobert Love 	struct fc_seq *sp = NULL;
137942e9a92fSRobert Love 	struct fc_exch *ep = NULL;
138042e9a92fSRobert Love 	enum fc_pf_rjt_reason reject;
138142e9a92fSRobert Love 
1382174e1ebfSChris Leech 	/* We can have the wrong fc_lport at this point with NPIV, which is a
1383174e1ebfSChris Leech 	 * problem now that we know a new exchange needs to be allocated
1384174e1ebfSChris Leech 	 */
13853a3b42bfSRobert Love 	lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
13863a3b42bfSRobert Love 	if (!lport) {
1387174e1ebfSChris Leech 		fc_frame_free(fp);
1388174e1ebfSChris Leech 		return;
1389174e1ebfSChris Leech 	}
139092261156SJoe Eykholt 	fr_dev(fp) = lport;
1391174e1ebfSChris Leech 
139292261156SJoe Eykholt 	BUG_ON(fr_seq(fp));		/* XXX remove later */
139392261156SJoe Eykholt 
139492261156SJoe Eykholt 	/*
139592261156SJoe Eykholt 	 * If the RX_ID is 0xffff, don't allocate an exchange.
139692261156SJoe Eykholt 	 * The upper-level protocol may request one later, if needed.
139792261156SJoe Eykholt 	 */
139892261156SJoe Eykholt 	if (fh->fh_rx_id == htons(FC_XID_UNKNOWN))
139992261156SJoe Eykholt 		return lport->tt.lport_recv(lport, fp);
140092261156SJoe Eykholt 
14013a3b42bfSRobert Love 	reject = fc_seq_lookup_recip(lport, mp, fp);
140242e9a92fSRobert Love 	if (reject == FC_RJT_NONE) {
140342e9a92fSRobert Love 		sp = fr_seq(fp);	/* sequence will be held */
140442e9a92fSRobert Love 		ep = fc_seq_exch(sp);
140542e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
1406f60e12e9SJoe Eykholt 		ep->encaps = fr_encaps(fp);
140742e9a92fSRobert Love 
140842e9a92fSRobert Love 		/*
140942e9a92fSRobert Love 		 * Call the receive function.
141042e9a92fSRobert Love 		 *
141142e9a92fSRobert Love 		 * The receive function may allocate a new sequence
141242e9a92fSRobert Love 		 * over the old one, so we shouldn't change the
141342e9a92fSRobert Love 		 * sequence after this.
141442e9a92fSRobert Love 		 *
141542e9a92fSRobert Love 		 * The frame will be freed by the receive function.
141642e9a92fSRobert Love 		 * If new exch resp handler is valid then call that
141742e9a92fSRobert Love 		 * first.
141842e9a92fSRobert Love 		 */
141942e9a92fSRobert Love 		if (ep->resp)
142042e9a92fSRobert Love 			ep->resp(sp, fp, ep->arg);
142142e9a92fSRobert Love 		else
142292261156SJoe Eykholt 			lport->tt.lport_recv(lport, fp);
142342e9a92fSRobert Love 		fc_exch_release(ep);	/* release from lookup */
142442e9a92fSRobert Love 	} else {
14253a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
14263a3b42bfSRobert Love 			     reject);
142742e9a92fSRobert Love 		fc_frame_free(fp);
142842e9a92fSRobert Love 	}
142942e9a92fSRobert Love }
143042e9a92fSRobert Love 
14313a3b42bfSRobert Love /**
14323a3b42bfSRobert Love  * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
14333a3b42bfSRobert Love  *			     end is the originator of the sequence that is a
14343a3b42bfSRobert Love  *			     response to our initial exchange
14353a3b42bfSRobert Love  * @mp: The EM that the exchange is on
14363a3b42bfSRobert Love  * @fp: The response frame
143742e9a92fSRobert Love  */
143842e9a92fSRobert Love static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
143942e9a92fSRobert Love {
144042e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
144142e9a92fSRobert Love 	struct fc_seq *sp;
144242e9a92fSRobert Love 	struct fc_exch *ep;
144342e9a92fSRobert Love 	enum fc_sof sof;
144442e9a92fSRobert Love 	u32 f_ctl;
144542e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
144642e9a92fSRobert Love 	void *ex_resp_arg;
144742e9a92fSRobert Love 	int rc;
144842e9a92fSRobert Love 
144942e9a92fSRobert Love 	ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
145042e9a92fSRobert Love 	if (!ep) {
145142e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
145242e9a92fSRobert Love 		goto out;
145342e9a92fSRobert Love 	}
145430121d14SSteve Ma 	if (ep->esb_stat & ESB_ST_COMPLETE) {
145530121d14SSteve Ma 		atomic_inc(&mp->stats.xid_not_found);
14568236554aSHillf Danton 		goto rel;
145730121d14SSteve Ma 	}
145842e9a92fSRobert Love 	if (ep->rxid == FC_XID_UNKNOWN)
145942e9a92fSRobert Love 		ep->rxid = ntohs(fh->fh_rx_id);
146042e9a92fSRobert Love 	if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
146142e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
146242e9a92fSRobert Love 		goto rel;
146342e9a92fSRobert Love 	}
146442e9a92fSRobert Love 	if (ep->did != ntoh24(fh->fh_s_id) &&
146542e9a92fSRobert Love 	    ep->did != FC_FID_FLOGI) {
146642e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
146742e9a92fSRobert Love 		goto rel;
146842e9a92fSRobert Love 	}
146942e9a92fSRobert Love 	sof = fr_sof(fp);
147042e9a92fSRobert Love 	sp = &ep->seq;
1471b3667f91SJoe Eykholt 	if (fc_sof_is_init(sof)) {
1472a104c844SVasu Dev 		sp->ssb_stat |= SSB_ST_RESP;
1473b3667f91SJoe Eykholt 		sp->id = fh->fh_seq_id;
1474b3667f91SJoe Eykholt 	} else if (sp->id != fh->fh_seq_id) {
147542e9a92fSRobert Love 		atomic_inc(&mp->stats.seq_not_found);
147642e9a92fSRobert Love 		goto rel;
147742e9a92fSRobert Love 	}
1478a104c844SVasu Dev 
147942e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
148042e9a92fSRobert Love 	fr_seq(fp) = sp;
148142e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
148242e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
148342e9a92fSRobert Love 
148442e9a92fSRobert Love 	if (fc_sof_needs_ack(sof))
148542e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
148642e9a92fSRobert Love 	resp = ep->resp;
148742e9a92fSRobert Love 	ex_resp_arg = ep->arg;
148842e9a92fSRobert Love 
148942e9a92fSRobert Love 	if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
149042e9a92fSRobert Love 	    (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
149142e9a92fSRobert Love 	    (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
149242e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
14938d23f4baSVasu Dev 		resp = ep->resp;
149442e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
149542e9a92fSRobert Love 		WARN_ON(fc_seq_exch(sp) != ep);
149642e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
149742e9a92fSRobert Love 		if (!rc)
1498b2f0091fSVasu Dev 			fc_exch_delete(ep);
149942e9a92fSRobert Love 	}
150042e9a92fSRobert Love 
150142e9a92fSRobert Love 	/*
150242e9a92fSRobert Love 	 * Call the receive function.
150342e9a92fSRobert Love 	 * The sequence is held (has a refcnt) for us,
150442e9a92fSRobert Love 	 * but not for the receive function.
150542e9a92fSRobert Love 	 *
150642e9a92fSRobert Love 	 * The receive function may allocate a new sequence
150742e9a92fSRobert Love 	 * over the old one, so we shouldn't change the
150842e9a92fSRobert Love 	 * sequence after this.
150942e9a92fSRobert Love 	 *
151042e9a92fSRobert Love 	 * The frame will be freed by the receive function.
151142e9a92fSRobert Love 	 * If new exch resp handler is valid then call that
151242e9a92fSRobert Love 	 * first.
151342e9a92fSRobert Love 	 */
151442e9a92fSRobert Love 	if (resp)
151542e9a92fSRobert Love 		resp(sp, fp, ex_resp_arg);
151642e9a92fSRobert Love 	else
151742e9a92fSRobert Love 		fc_frame_free(fp);
151842e9a92fSRobert Love 	fc_exch_release(ep);
151942e9a92fSRobert Love 	return;
152042e9a92fSRobert Love rel:
152142e9a92fSRobert Love 	fc_exch_release(ep);
152242e9a92fSRobert Love out:
152342e9a92fSRobert Love 	fc_frame_free(fp);
152442e9a92fSRobert Love }
152542e9a92fSRobert Love 
15263a3b42bfSRobert Love /**
15273a3b42bfSRobert Love  * fc_exch_recv_resp() - Handler for a sequence where other end is
15283a3b42bfSRobert Love  *			 responding to our sequence
15293a3b42bfSRobert Love  * @mp: The EM that the exchange is on
15303a3b42bfSRobert Love  * @fp: The response frame
153142e9a92fSRobert Love  */
153242e9a92fSRobert Love static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
153342e9a92fSRobert Love {
153442e9a92fSRobert Love 	struct fc_seq *sp;
153542e9a92fSRobert Love 
153642e9a92fSRobert Love 	sp = fc_seq_lookup_orig(mp, fp);	/* doesn't hold sequence */
1537d459b7eaSRobert Love 
1538d459b7eaSRobert Love 	if (!sp)
153942e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
1540d459b7eaSRobert Love 	else
154142e9a92fSRobert Love 		atomic_inc(&mp->stats.non_bls_resp);
1542d459b7eaSRobert Love 
154342e9a92fSRobert Love 	fc_frame_free(fp);
154442e9a92fSRobert Love }
154542e9a92fSRobert Love 
15463a3b42bfSRobert Love /**
15473a3b42bfSRobert Love  * fc_exch_abts_resp() - Handler for a response to an ABT
15483a3b42bfSRobert Love  * @ep: The exchange that the frame is on
15493a3b42bfSRobert Love  * @fp: The response frame
15503a3b42bfSRobert Love  *
15513a3b42bfSRobert Love  * This response would be to an ABTS cancelling an exchange or sequence.
15523a3b42bfSRobert Love  * The response can be either BA_ACC or BA_RJT
155342e9a92fSRobert Love  */
155442e9a92fSRobert Love static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
155542e9a92fSRobert Love {
155642e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
155742e9a92fSRobert Love 	void *ex_resp_arg;
155842e9a92fSRobert Love 	struct fc_frame_header *fh;
155942e9a92fSRobert Love 	struct fc_ba_acc *ap;
156042e9a92fSRobert Love 	struct fc_seq *sp;
156142e9a92fSRobert Love 	u16 low;
156242e9a92fSRobert Love 	u16 high;
156342e9a92fSRobert Love 	int rc = 1, has_rec = 0;
156442e9a92fSRobert Love 
156542e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
15667414705eSRobert Love 	FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
15677414705eSRobert Love 		    fc_exch_rctl_name(fh->fh_r_ctl));
156842e9a92fSRobert Love 
1569b29a4f30SVasu Dev 	if (cancel_delayed_work_sync(&ep->timeout_work)) {
1570*3a292605SRobert Love 		FC_EXCH_DBG(ep, "Exchange timer canceled due to ABTS response\n");
157142e9a92fSRobert Love 		fc_exch_release(ep);	/* release from pending timer hold */
1572b29a4f30SVasu Dev 	}
157342e9a92fSRobert Love 
157442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
157542e9a92fSRobert Love 	switch (fh->fh_r_ctl) {
157642e9a92fSRobert Love 	case FC_RCTL_BA_ACC:
157742e9a92fSRobert Love 		ap = fc_frame_payload_get(fp, sizeof(*ap));
157842e9a92fSRobert Love 		if (!ap)
157942e9a92fSRobert Love 			break;
158042e9a92fSRobert Love 
158142e9a92fSRobert Love 		/*
158242e9a92fSRobert Love 		 * Decide whether to establish a Recovery Qualifier.
158342e9a92fSRobert Love 		 * We do this if there is a non-empty SEQ_CNT range and
158442e9a92fSRobert Love 		 * SEQ_ID is the same as the one we aborted.
158542e9a92fSRobert Love 		 */
158642e9a92fSRobert Love 		low = ntohs(ap->ba_low_seq_cnt);
158742e9a92fSRobert Love 		high = ntohs(ap->ba_high_seq_cnt);
158842e9a92fSRobert Love 		if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
158942e9a92fSRobert Love 		    (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
159042e9a92fSRobert Love 		     ap->ba_seq_id == ep->seq_id) && low != high) {
159142e9a92fSRobert Love 			ep->esb_stat |= ESB_ST_REC_QUAL;
159242e9a92fSRobert Love 			fc_exch_hold(ep);  /* hold for recovery qualifier */
159342e9a92fSRobert Love 			has_rec = 1;
159442e9a92fSRobert Love 		}
159542e9a92fSRobert Love 		break;
159642e9a92fSRobert Love 	case FC_RCTL_BA_RJT:
159742e9a92fSRobert Love 		break;
159842e9a92fSRobert Love 	default:
159942e9a92fSRobert Love 		break;
160042e9a92fSRobert Love 	}
160142e9a92fSRobert Love 
160242e9a92fSRobert Love 	resp = ep->resp;
160342e9a92fSRobert Love 	ex_resp_arg = ep->arg;
160442e9a92fSRobert Love 
160542e9a92fSRobert Love 	/* do we need to do some other checks here. Can we reuse more of
160642e9a92fSRobert Love 	 * fc_exch_recv_seq_resp
160742e9a92fSRobert Love 	 */
160842e9a92fSRobert Love 	sp = &ep->seq;
160942e9a92fSRobert Love 	/*
161042e9a92fSRobert Love 	 * do we want to check END_SEQ as well as LAST_SEQ here?
161142e9a92fSRobert Love 	 */
161242e9a92fSRobert Love 	if (ep->fh_type != FC_TYPE_FCP &&
161342e9a92fSRobert Love 	    ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
161442e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
161542e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
161642e9a92fSRobert Love 	if (!rc)
1617b2f0091fSVasu Dev 		fc_exch_delete(ep);
161842e9a92fSRobert Love 
161942e9a92fSRobert Love 	if (resp)
162042e9a92fSRobert Love 		resp(sp, fp, ex_resp_arg);
162142e9a92fSRobert Love 	else
162242e9a92fSRobert Love 		fc_frame_free(fp);
162342e9a92fSRobert Love 
162442e9a92fSRobert Love 	if (has_rec)
162542e9a92fSRobert Love 		fc_exch_timer_set(ep, ep->r_a_tov);
162642e9a92fSRobert Love 
162742e9a92fSRobert Love }
162842e9a92fSRobert Love 
16293a3b42bfSRobert Love /**
16303a3b42bfSRobert Love  * fc_exch_recv_bls() - Handler for a BLS sequence
16313a3b42bfSRobert Love  * @mp: The EM that the exchange is on
16323a3b42bfSRobert Love  * @fp: The request frame
16333a3b42bfSRobert Love  *
16343a3b42bfSRobert Love  * The BLS frame is always a sequence initiated by the remote side.
163542e9a92fSRobert Love  * We may be either the originator or recipient of the exchange.
163642e9a92fSRobert Love  */
163742e9a92fSRobert Love static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
163842e9a92fSRobert Love {
163942e9a92fSRobert Love 	struct fc_frame_header *fh;
164042e9a92fSRobert Love 	struct fc_exch *ep;
164142e9a92fSRobert Love 	u32 f_ctl;
164242e9a92fSRobert Love 
164342e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
164442e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
164542e9a92fSRobert Love 	fr_seq(fp) = NULL;
164642e9a92fSRobert Love 
164742e9a92fSRobert Love 	ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
164842e9a92fSRobert Love 			  ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
164942e9a92fSRobert Love 	if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
165042e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
165142e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
165242e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
165342e9a92fSRobert Love 	}
165442e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_CTX) {
165542e9a92fSRobert Love 		/*
165642e9a92fSRobert Love 		 * A response to a sequence we initiated.
165742e9a92fSRobert Love 		 * This should only be ACKs for class 2 or F.
165842e9a92fSRobert Love 		 */
165942e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
166042e9a92fSRobert Love 		case FC_RCTL_ACK_1:
166142e9a92fSRobert Love 		case FC_RCTL_ACK_0:
166242e9a92fSRobert Love 			break;
166342e9a92fSRobert Love 		default:
1664d4042e9cSBhanu Prakash Gollapudi 			if (ep)
16657414705eSRobert Love 				FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
166642e9a92fSRobert Love 					    fh->fh_r_ctl,
166742e9a92fSRobert Love 					    fc_exch_rctl_name(fh->fh_r_ctl));
166842e9a92fSRobert Love 			break;
166942e9a92fSRobert Love 		}
167042e9a92fSRobert Love 		fc_frame_free(fp);
167142e9a92fSRobert Love 	} else {
167242e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
167342e9a92fSRobert Love 		case FC_RCTL_BA_RJT:
167442e9a92fSRobert Love 		case FC_RCTL_BA_ACC:
167542e9a92fSRobert Love 			if (ep)
167642e9a92fSRobert Love 				fc_exch_abts_resp(ep, fp);
167742e9a92fSRobert Love 			else
167842e9a92fSRobert Love 				fc_frame_free(fp);
167942e9a92fSRobert Love 			break;
168042e9a92fSRobert Love 		case FC_RCTL_BA_ABTS:
168142e9a92fSRobert Love 			fc_exch_recv_abts(ep, fp);
168242e9a92fSRobert Love 			break;
168342e9a92fSRobert Love 		default:			/* ignore junk */
168442e9a92fSRobert Love 			fc_frame_free(fp);
168542e9a92fSRobert Love 			break;
168642e9a92fSRobert Love 		}
168742e9a92fSRobert Love 	}
168842e9a92fSRobert Love 	if (ep)
168942e9a92fSRobert Love 		fc_exch_release(ep);	/* release hold taken by fc_exch_find */
169042e9a92fSRobert Love }
169142e9a92fSRobert Love 
16923a3b42bfSRobert Love /**
16933a3b42bfSRobert Love  * fc_seq_ls_acc() - Accept sequence with LS_ACC
169492261156SJoe Eykholt  * @rx_fp: The received frame, not freed here.
16953a3b42bfSRobert Love  *
169642e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
169742e9a92fSRobert Love  * originator will repeat the sequence.
169842e9a92fSRobert Love  */
169992261156SJoe Eykholt static void fc_seq_ls_acc(struct fc_frame *rx_fp)
170042e9a92fSRobert Love {
170192261156SJoe Eykholt 	struct fc_lport *lport;
170242e9a92fSRobert Love 	struct fc_els_ls_acc *acc;
170342e9a92fSRobert Love 	struct fc_frame *fp;
170442e9a92fSRobert Love 
170592261156SJoe Eykholt 	lport = fr_dev(rx_fp);
170692261156SJoe Eykholt 	fp = fc_frame_alloc(lport, sizeof(*acc));
170792261156SJoe Eykholt 	if (!fp)
170892261156SJoe Eykholt 		return;
170942e9a92fSRobert Love 	acc = fc_frame_payload_get(fp, sizeof(*acc));
171042e9a92fSRobert Love 	memset(acc, 0, sizeof(*acc));
171142e9a92fSRobert Love 	acc->la_cmd = ELS_LS_ACC;
171292261156SJoe Eykholt 	fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
171392261156SJoe Eykholt 	lport->tt.frame_send(lport, fp);
171442e9a92fSRobert Love }
171542e9a92fSRobert Love 
17163a3b42bfSRobert Love /**
17173a3b42bfSRobert Love  * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
171892261156SJoe Eykholt  * @rx_fp: The received frame, not freed here.
17193a3b42bfSRobert Love  * @reason: The reason the sequence is being rejected
172092261156SJoe Eykholt  * @explan: The explanation for the rejection
17213a3b42bfSRobert Love  *
172242e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
172342e9a92fSRobert Love  * originator will repeat the sequence.
172442e9a92fSRobert Love  */
172592261156SJoe Eykholt static void fc_seq_ls_rjt(struct fc_frame *rx_fp, enum fc_els_rjt_reason reason,
172642e9a92fSRobert Love 			  enum fc_els_rjt_explan explan)
172742e9a92fSRobert Love {
172892261156SJoe Eykholt 	struct fc_lport *lport;
172942e9a92fSRobert Love 	struct fc_els_ls_rjt *rjt;
173042e9a92fSRobert Love 	struct fc_frame *fp;
173142e9a92fSRobert Love 
173292261156SJoe Eykholt 	lport = fr_dev(rx_fp);
173392261156SJoe Eykholt 	fp = fc_frame_alloc(lport, sizeof(*rjt));
173492261156SJoe Eykholt 	if (!fp)
173592261156SJoe Eykholt 		return;
173642e9a92fSRobert Love 	rjt = fc_frame_payload_get(fp, sizeof(*rjt));
173742e9a92fSRobert Love 	memset(rjt, 0, sizeof(*rjt));
173842e9a92fSRobert Love 	rjt->er_cmd = ELS_LS_RJT;
173942e9a92fSRobert Love 	rjt->er_reason = reason;
174042e9a92fSRobert Love 	rjt->er_explan = explan;
174192261156SJoe Eykholt 	fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
174292261156SJoe Eykholt 	lport->tt.frame_send(lport, fp);
174342e9a92fSRobert Love }
174442e9a92fSRobert Love 
17453a3b42bfSRobert Love /**
17463a3b42bfSRobert Love  * fc_exch_reset() - Reset an exchange
17473a3b42bfSRobert Love  * @ep: The exchange to be reset
17483a3b42bfSRobert Love  */
174942e9a92fSRobert Love static void fc_exch_reset(struct fc_exch *ep)
175042e9a92fSRobert Love {
175142e9a92fSRobert Love 	struct fc_seq *sp;
175242e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *, void *);
175342e9a92fSRobert Love 	void *arg;
175442e9a92fSRobert Love 	int rc = 1;
175542e9a92fSRobert Love 
175642e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
175777a2b73aSVasu Dev 	fc_exch_abort_locked(ep, 0);
175842e9a92fSRobert Love 	ep->state |= FC_EX_RST_CLEANUP;
1759b29a4f30SVasu Dev 	fc_exch_timer_cancel(ep);
176042e9a92fSRobert Love 	resp = ep->resp;
176142e9a92fSRobert Love 	ep->resp = NULL;
176242e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL)
176342e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec_qual */
176442e9a92fSRobert Love 	ep->esb_stat &= ~ESB_ST_REC_QUAL;
176542e9a92fSRobert Love 	arg = ep->arg;
176642e9a92fSRobert Love 	sp = &ep->seq;
176742e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
176842e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
176942e9a92fSRobert Love 	if (!rc)
1770b2f0091fSVasu Dev 		fc_exch_delete(ep);
177142e9a92fSRobert Love 
177242e9a92fSRobert Love 	if (resp)
177342e9a92fSRobert Love 		resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
177442e9a92fSRobert Love }
177542e9a92fSRobert Love 
1776b2f0091fSVasu Dev /**
17773a3b42bfSRobert Love  * fc_exch_pool_reset() - Reset a per cpu exchange pool
17783a3b42bfSRobert Love  * @lport: The local port that the exchange pool is on
17793a3b42bfSRobert Love  * @pool:  The exchange pool to be reset
17803a3b42bfSRobert Love  * @sid:   The source ID
17813a3b42bfSRobert Love  * @did:   The destination ID
1782b2f0091fSVasu Dev  *
17833a3b42bfSRobert Love  * Resets a per cpu exches pool, releasing all of its sequences
17843a3b42bfSRobert Love  * and exchanges. If sid is non-zero then reset only exchanges
17853a3b42bfSRobert Love  * we sourced from the local port's FID. If did is non-zero then
17863a3b42bfSRobert Love  * only reset exchanges destined for the local port's FID.
178742e9a92fSRobert Love  */
1788b2f0091fSVasu Dev static void fc_exch_pool_reset(struct fc_lport *lport,
1789b2f0091fSVasu Dev 			       struct fc_exch_pool *pool,
1790b2f0091fSVasu Dev 			       u32 sid, u32 did)
179142e9a92fSRobert Love {
179242e9a92fSRobert Love 	struct fc_exch *ep;
179342e9a92fSRobert Love 	struct fc_exch *next;
179442e9a92fSRobert Love 
1795b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
179642e9a92fSRobert Love restart:
1797b2f0091fSVasu Dev 	list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
1798b2f0091fSVasu Dev 		if ((lport == ep->lp) &&
179952ff878cSVasu Dev 		    (sid == 0 || sid == ep->sid) &&
180042e9a92fSRobert Love 		    (did == 0 || did == ep->did)) {
180142e9a92fSRobert Love 			fc_exch_hold(ep);
1802b2f0091fSVasu Dev 			spin_unlock_bh(&pool->lock);
180342e9a92fSRobert Love 
180442e9a92fSRobert Love 			fc_exch_reset(ep);
180542e9a92fSRobert Love 
180642e9a92fSRobert Love 			fc_exch_release(ep);
1807b2f0091fSVasu Dev 			spin_lock_bh(&pool->lock);
180842e9a92fSRobert Love 
180942e9a92fSRobert Love 			/*
181052ff878cSVasu Dev 			 * must restart loop incase while lock
181152ff878cSVasu Dev 			 * was down multiple eps were released.
181242e9a92fSRobert Love 			 */
181342e9a92fSRobert Love 			goto restart;
181442e9a92fSRobert Love 		}
181542e9a92fSRobert Love 	}
1816b6e3c840SVasu Dev 	pool->next_index = 0;
1817b6e3c840SVasu Dev 	pool->left = FC_XID_UNKNOWN;
1818b6e3c840SVasu Dev 	pool->right = FC_XID_UNKNOWN;
1819b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
1820b2f0091fSVasu Dev }
1821b2f0091fSVasu Dev 
1822b2f0091fSVasu Dev /**
18233a3b42bfSRobert Love  * fc_exch_mgr_reset() - Reset all EMs of a local port
18243a3b42bfSRobert Love  * @lport: The local port whose EMs are to be reset
18253a3b42bfSRobert Love  * @sid:   The source ID
18263a3b42bfSRobert Love  * @did:   The destination ID
1827b2f0091fSVasu Dev  *
18283a3b42bfSRobert Love  * Reset all EMs associated with a given local port. Release all
18293a3b42bfSRobert Love  * sequences and exchanges. If sid is non-zero then reset only the
18303a3b42bfSRobert Love  * exchanges sent from the local port's FID. If did is non-zero then
18313a3b42bfSRobert Love  * reset only exchanges destined for the local port's FID.
1832b2f0091fSVasu Dev  */
1833b2f0091fSVasu Dev void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1834b2f0091fSVasu Dev {
1835b2f0091fSVasu Dev 	struct fc_exch_mgr_anchor *ema;
1836b2f0091fSVasu Dev 	unsigned int cpu;
1837b2f0091fSVasu Dev 
1838b2f0091fSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
1839b2f0091fSVasu Dev 		for_each_possible_cpu(cpu)
1840b2f0091fSVasu Dev 			fc_exch_pool_reset(lport,
1841b2f0091fSVasu Dev 					   per_cpu_ptr(ema->mp->pool, cpu),
1842b2f0091fSVasu Dev 					   sid, did);
184342e9a92fSRobert Love 	}
184452ff878cSVasu Dev }
184542e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_reset);
184642e9a92fSRobert Love 
18473a3b42bfSRobert Love /**
184892261156SJoe Eykholt  * fc_exch_lookup() - find an exchange
184992261156SJoe Eykholt  * @lport: The local port
185092261156SJoe Eykholt  * @xid: The exchange ID
185192261156SJoe Eykholt  *
185292261156SJoe Eykholt  * Returns exchange pointer with hold for caller, or NULL if not found.
185392261156SJoe Eykholt  */
185492261156SJoe Eykholt static struct fc_exch *fc_exch_lookup(struct fc_lport *lport, u32 xid)
185592261156SJoe Eykholt {
185692261156SJoe Eykholt 	struct fc_exch_mgr_anchor *ema;
185792261156SJoe Eykholt 
185892261156SJoe Eykholt 	list_for_each_entry(ema, &lport->ema_list, ema_list)
185992261156SJoe Eykholt 		if (ema->mp->min_xid <= xid && xid <= ema->mp->max_xid)
186092261156SJoe Eykholt 			return fc_exch_find(ema->mp, xid);
186192261156SJoe Eykholt 	return NULL;
186292261156SJoe Eykholt }
186392261156SJoe Eykholt 
186492261156SJoe Eykholt /**
18653a3b42bfSRobert Love  * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
186692261156SJoe Eykholt  * @rfp: The REC frame, not freed here.
18673a3b42bfSRobert Love  *
186842e9a92fSRobert Love  * Note that the requesting port may be different than the S_ID in the request.
186942e9a92fSRobert Love  */
187092261156SJoe Eykholt static void fc_exch_els_rec(struct fc_frame *rfp)
187142e9a92fSRobert Love {
187292261156SJoe Eykholt 	struct fc_lport *lport;
187342e9a92fSRobert Love 	struct fc_frame *fp;
187442e9a92fSRobert Love 	struct fc_exch *ep;
187542e9a92fSRobert Love 	struct fc_els_rec *rp;
187642e9a92fSRobert Love 	struct fc_els_rec_acc *acc;
187742e9a92fSRobert Love 	enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
187842e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
187942e9a92fSRobert Love 	u32 sid;
188042e9a92fSRobert Love 	u16 rxid;
188142e9a92fSRobert Love 	u16 oxid;
188242e9a92fSRobert Love 
188392261156SJoe Eykholt 	lport = fr_dev(rfp);
188442e9a92fSRobert Love 	rp = fc_frame_payload_get(rfp, sizeof(*rp));
188542e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
188642e9a92fSRobert Love 	if (!rp)
188742e9a92fSRobert Love 		goto reject;
188842e9a92fSRobert Love 	sid = ntoh24(rp->rec_s_id);
188942e9a92fSRobert Love 	rxid = ntohs(rp->rec_rx_id);
189042e9a92fSRobert Love 	oxid = ntohs(rp->rec_ox_id);
189142e9a92fSRobert Love 
189292261156SJoe Eykholt 	ep = fc_exch_lookup(lport,
189392261156SJoe Eykholt 			    sid == fc_host_port_id(lport->host) ? oxid : rxid);
189442e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
189542e9a92fSRobert Love 	if (!ep)
189642e9a92fSRobert Love 		goto reject;
189792261156SJoe Eykholt 	if (ep->oid != sid || oxid != ep->oxid)
189892261156SJoe Eykholt 		goto rel;
189992261156SJoe Eykholt 	if (rxid != FC_XID_UNKNOWN && rxid != ep->rxid)
190092261156SJoe Eykholt 		goto rel;
190192261156SJoe Eykholt 	fp = fc_frame_alloc(lport, sizeof(*acc));
190292261156SJoe Eykholt 	if (!fp)
190342e9a92fSRobert Love 		goto out;
190492261156SJoe Eykholt 
190542e9a92fSRobert Love 	acc = fc_frame_payload_get(fp, sizeof(*acc));
190642e9a92fSRobert Love 	memset(acc, 0, sizeof(*acc));
190742e9a92fSRobert Love 	acc->reca_cmd = ELS_LS_ACC;
190842e9a92fSRobert Love 	acc->reca_ox_id = rp->rec_ox_id;
190942e9a92fSRobert Love 	memcpy(acc->reca_ofid, rp->rec_s_id, 3);
191042e9a92fSRobert Love 	acc->reca_rx_id = htons(ep->rxid);
191142e9a92fSRobert Love 	if (ep->sid == ep->oid)
191242e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->did);
191342e9a92fSRobert Love 	else
191442e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->sid);
191542e9a92fSRobert Love 	acc->reca_fc4value = htonl(ep->seq.rec_data);
191642e9a92fSRobert Love 	acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
191742e9a92fSRobert Love 						 ESB_ST_SEQ_INIT |
191842e9a92fSRobert Love 						 ESB_ST_COMPLETE));
191992261156SJoe Eykholt 	fc_fill_reply_hdr(fp, rfp, FC_RCTL_ELS_REP, 0);
192092261156SJoe Eykholt 	lport->tt.frame_send(lport, fp);
192142e9a92fSRobert Love out:
192242e9a92fSRobert Love 	fc_exch_release(ep);
192342e9a92fSRobert Love 	return;
192442e9a92fSRobert Love 
192542e9a92fSRobert Love rel:
192642e9a92fSRobert Love 	fc_exch_release(ep);
192742e9a92fSRobert Love reject:
192892261156SJoe Eykholt 	fc_seq_ls_rjt(rfp, reason, explan);
192942e9a92fSRobert Love }
193042e9a92fSRobert Love 
19313a3b42bfSRobert Love /**
19323a3b42bfSRobert Love  * fc_exch_rrq_resp() - Handler for RRQ responses
19333a3b42bfSRobert Love  * @sp:	 The sequence that the RRQ is on
19343a3b42bfSRobert Love  * @fp:	 The RRQ frame
19353a3b42bfSRobert Love  * @arg: The exchange that the RRQ is on
193642e9a92fSRobert Love  *
193742e9a92fSRobert Love  * TODO: fix error handler.
193842e9a92fSRobert Love  */
193942e9a92fSRobert Love static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
194042e9a92fSRobert Love {
194142e9a92fSRobert Love 	struct fc_exch *aborted_ep = arg;
194242e9a92fSRobert Love 	unsigned int op;
194342e9a92fSRobert Love 
194442e9a92fSRobert Love 	if (IS_ERR(fp)) {
194542e9a92fSRobert Love 		int err = PTR_ERR(fp);
194642e9a92fSRobert Love 
194778342da3SVasu Dev 		if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
194842e9a92fSRobert Love 			goto cleanup;
19497414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
19507414705eSRobert Love 			    "frame error %d\n", err);
195142e9a92fSRobert Love 		return;
195242e9a92fSRobert Love 	}
195342e9a92fSRobert Love 
195442e9a92fSRobert Love 	op = fc_frame_payload_op(fp);
195542e9a92fSRobert Love 	fc_frame_free(fp);
195642e9a92fSRobert Love 
195742e9a92fSRobert Love 	switch (op) {
195842e9a92fSRobert Love 	case ELS_LS_RJT:
19597414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
196042e9a92fSRobert Love 		/* fall through */
196142e9a92fSRobert Love 	case ELS_LS_ACC:
196242e9a92fSRobert Love 		goto cleanup;
196342e9a92fSRobert Love 	default:
19647414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
19657414705eSRobert Love 			    "for RRQ", op);
196642e9a92fSRobert Love 		return;
196742e9a92fSRobert Love 	}
196842e9a92fSRobert Love 
196942e9a92fSRobert Love cleanup:
197042e9a92fSRobert Love 	fc_exch_done(&aborted_ep->seq);
197142e9a92fSRobert Love 	/* drop hold for rec qual */
197242e9a92fSRobert Love 	fc_exch_release(aborted_ep);
197342e9a92fSRobert Love }
197442e9a92fSRobert Love 
19751a7b75aeSRobert Love 
19761a7b75aeSRobert Love /**
19773a3b42bfSRobert Love  * fc_exch_seq_send() - Send a frame using a new exchange and sequence
19783a3b42bfSRobert Love  * @lport:	The local port to send the frame on
19793a3b42bfSRobert Love  * @fp:		The frame to be sent
19803a3b42bfSRobert Love  * @resp:	The response handler for this request
19813a3b42bfSRobert Love  * @destructor: The destructor for the exchange
19823a3b42bfSRobert Love  * @arg:	The argument to be passed to the response handler
19833a3b42bfSRobert Love  * @timer_msec: The timeout period for the exchange
19843a3b42bfSRobert Love  *
19853a3b42bfSRobert Love  * The frame pointer with some of the header's fields must be
19863a3b42bfSRobert Love  * filled before calling this routine, those fields are:
19873a3b42bfSRobert Love  *
19883a3b42bfSRobert Love  * - routing control
19893a3b42bfSRobert Love  * - FC port did
19903a3b42bfSRobert Love  * - FC port sid
19913a3b42bfSRobert Love  * - FC header type
19923a3b42bfSRobert Love  * - frame control
19933a3b42bfSRobert Love  * - parameter or relative offset
19941a7b75aeSRobert Love  */
19953a3b42bfSRobert Love static struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
19961a7b75aeSRobert Love 				       struct fc_frame *fp,
19971a7b75aeSRobert Love 				       void (*resp)(struct fc_seq *,
19981a7b75aeSRobert Love 						    struct fc_frame *fp,
19991a7b75aeSRobert Love 						    void *arg),
20001a7b75aeSRobert Love 				       void (*destructor)(struct fc_seq *,
20011a7b75aeSRobert Love 							  void *),
20021a7b75aeSRobert Love 				       void *arg, u32 timer_msec)
20031a7b75aeSRobert Love {
20041a7b75aeSRobert Love 	struct fc_exch *ep;
20051a7b75aeSRobert Love 	struct fc_seq *sp = NULL;
20061a7b75aeSRobert Love 	struct fc_frame_header *fh;
20073ee17f59SYi Zou 	struct fc_fcp_pkt *fsp = NULL;
20081a7b75aeSRobert Love 	int rc = 1;
20091a7b75aeSRobert Love 
20103a3b42bfSRobert Love 	ep = fc_exch_alloc(lport, fp);
20111a7b75aeSRobert Love 	if (!ep) {
20121a7b75aeSRobert Love 		fc_frame_free(fp);
20131a7b75aeSRobert Love 		return NULL;
20141a7b75aeSRobert Love 	}
20151a7b75aeSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT;
20161a7b75aeSRobert Love 	fh = fc_frame_header_get(fp);
20171a7b75aeSRobert Love 	fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
20181a7b75aeSRobert Love 	ep->resp = resp;
20191a7b75aeSRobert Love 	ep->destructor = destructor;
20201a7b75aeSRobert Love 	ep->arg = arg;
20211a7b75aeSRobert Love 	ep->r_a_tov = FC_DEF_R_A_TOV;
20223a3b42bfSRobert Love 	ep->lp = lport;
20231a7b75aeSRobert Love 	sp = &ep->seq;
20241a7b75aeSRobert Love 
20251a7b75aeSRobert Love 	ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
20261a7b75aeSRobert Love 	ep->f_ctl = ntoh24(fh->fh_f_ctl);
20271a7b75aeSRobert Love 	fc_exch_setup_hdr(ep, fp, ep->f_ctl);
20281a7b75aeSRobert Love 	sp->cnt++;
20291a7b75aeSRobert Love 
20303ee17f59SYi Zou 	if (ep->xid <= lport->lro_xid && fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) {
20313ee17f59SYi Zou 		fsp = fr_fsp(fp);
20321a7b75aeSRobert Love 		fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
20333ee17f59SYi Zou 	}
20341a7b75aeSRobert Love 
20353a3b42bfSRobert Love 	if (unlikely(lport->tt.frame_send(lport, fp)))
20361a7b75aeSRobert Love 		goto err;
20371a7b75aeSRobert Love 
20381a7b75aeSRobert Love 	if (timer_msec)
20391a7b75aeSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
20401a7b75aeSRobert Love 	ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not first seq */
20411a7b75aeSRobert Love 
20421a7b75aeSRobert Love 	if (ep->f_ctl & FC_FC_SEQ_INIT)
20431a7b75aeSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
20441a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
20451a7b75aeSRobert Love 	return sp;
20461a7b75aeSRobert Love err:
20473ee17f59SYi Zou 	if (fsp)
20483ee17f59SYi Zou 		fc_fcp_ddp_done(fsp);
20491a7b75aeSRobert Love 	rc = fc_exch_done_locked(ep);
20501a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
20511a7b75aeSRobert Love 	if (!rc)
20521a7b75aeSRobert Love 		fc_exch_delete(ep);
20531a7b75aeSRobert Love 	return NULL;
20541a7b75aeSRobert Love }
20551a7b75aeSRobert Love 
20563a3b42bfSRobert Love /**
20573a3b42bfSRobert Love  * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
20583a3b42bfSRobert Love  * @ep: The exchange to send the RRQ on
20593a3b42bfSRobert Love  *
206042e9a92fSRobert Love  * This tells the remote port to stop blocking the use of
206142e9a92fSRobert Love  * the exchange and the seq_cnt range.
206242e9a92fSRobert Love  */
206342e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *ep)
206442e9a92fSRobert Love {
20653a3b42bfSRobert Love 	struct fc_lport *lport;
206642e9a92fSRobert Love 	struct fc_els_rrq *rrq;
206742e9a92fSRobert Love 	struct fc_frame *fp;
206842e9a92fSRobert Love 	u32 did;
206942e9a92fSRobert Love 
20703a3b42bfSRobert Love 	lport = ep->lp;
207142e9a92fSRobert Love 
20723a3b42bfSRobert Love 	fp = fc_frame_alloc(lport, sizeof(*rrq));
207342e9a92fSRobert Love 	if (!fp)
2074a0cc1eccSVasu Dev 		goto retry;
2075a0cc1eccSVasu Dev 
207642e9a92fSRobert Love 	rrq = fc_frame_payload_get(fp, sizeof(*rrq));
207742e9a92fSRobert Love 	memset(rrq, 0, sizeof(*rrq));
207842e9a92fSRobert Love 	rrq->rrq_cmd = ELS_RRQ;
207942e9a92fSRobert Love 	hton24(rrq->rrq_s_id, ep->sid);
208042e9a92fSRobert Love 	rrq->rrq_ox_id = htons(ep->oxid);
208142e9a92fSRobert Love 	rrq->rrq_rx_id = htons(ep->rxid);
208242e9a92fSRobert Love 
208342e9a92fSRobert Love 	did = ep->did;
208442e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP)
208542e9a92fSRobert Love 		did = ep->sid;
208642e9a92fSRobert Love 
208742e9a92fSRobert Love 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
20887b2787ecSRobert Love 		       lport->port_id, FC_TYPE_ELS,
208942e9a92fSRobert Love 		       FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
209042e9a92fSRobert Love 
20913a3b42bfSRobert Love 	if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
20923a3b42bfSRobert Love 			     lport->e_d_tov))
2093a0cc1eccSVasu Dev 		return;
2094a0cc1eccSVasu Dev 
2095a0cc1eccSVasu Dev retry:
2096a0cc1eccSVasu Dev 	spin_lock_bh(&ep->ex_lock);
2097a0cc1eccSVasu Dev 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
2098a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
2099a0cc1eccSVasu Dev 		/* drop hold for rec qual */
2100a0cc1eccSVasu Dev 		fc_exch_release(ep);
210142e9a92fSRobert Love 		return;
210242e9a92fSRobert Love 	}
2103a0cc1eccSVasu Dev 	ep->esb_stat |= ESB_ST_REC_QUAL;
2104a0cc1eccSVasu Dev 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
2105a0cc1eccSVasu Dev 	spin_unlock_bh(&ep->ex_lock);
210642e9a92fSRobert Love }
210742e9a92fSRobert Love 
21083a3b42bfSRobert Love /**
21093a3b42bfSRobert Love  * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
211092261156SJoe Eykholt  * @fp: The RRQ frame, not freed here.
211142e9a92fSRobert Love  */
211292261156SJoe Eykholt static void fc_exch_els_rrq(struct fc_frame *fp)
211342e9a92fSRobert Love {
211492261156SJoe Eykholt 	struct fc_lport *lport;
21153f127ad9SVasu Dev 	struct fc_exch *ep = NULL;	/* request or subject exchange */
211642e9a92fSRobert Love 	struct fc_els_rrq *rp;
211742e9a92fSRobert Love 	u32 sid;
211842e9a92fSRobert Love 	u16 xid;
211942e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
212042e9a92fSRobert Love 
212192261156SJoe Eykholt 	lport = fr_dev(fp);
212242e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
212342e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
212442e9a92fSRobert Love 	if (!rp)
212542e9a92fSRobert Love 		goto reject;
212642e9a92fSRobert Love 
212742e9a92fSRobert Love 	/*
212842e9a92fSRobert Love 	 * lookup subject exchange.
212942e9a92fSRobert Love 	 */
213042e9a92fSRobert Love 	sid = ntoh24(rp->rrq_s_id);		/* subject source */
213192261156SJoe Eykholt 	xid = fc_host_port_id(lport->host) == sid ?
213292261156SJoe Eykholt 			ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
213392261156SJoe Eykholt 	ep = fc_exch_lookup(lport, xid);
213442e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
213542e9a92fSRobert Love 	if (!ep)
213642e9a92fSRobert Love 		goto reject;
213742e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
213842e9a92fSRobert Love 	if (ep->oxid != ntohs(rp->rrq_ox_id))
213942e9a92fSRobert Love 		goto unlock_reject;
214042e9a92fSRobert Love 	if (ep->rxid != ntohs(rp->rrq_rx_id) &&
214142e9a92fSRobert Love 	    ep->rxid != FC_XID_UNKNOWN)
214242e9a92fSRobert Love 		goto unlock_reject;
214342e9a92fSRobert Love 	explan = ELS_EXPL_SID;
214442e9a92fSRobert Love 	if (ep->sid != sid)
214542e9a92fSRobert Love 		goto unlock_reject;
214642e9a92fSRobert Love 
214742e9a92fSRobert Love 	/*
214842e9a92fSRobert Love 	 * Clear Recovery Qualifier state, and cancel timer if complete.
214942e9a92fSRobert Love 	 */
215042e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL) {
215142e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_REC_QUAL;
215242e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec qual */
215342e9a92fSRobert Love 	}
2154b29a4f30SVasu Dev 	if (ep->esb_stat & ESB_ST_COMPLETE)
2155b29a4f30SVasu Dev 		fc_exch_timer_cancel(ep);
215642e9a92fSRobert Love 
215742e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
215842e9a92fSRobert Love 
215942e9a92fSRobert Love 	/*
216042e9a92fSRobert Love 	 * Send LS_ACC.
216142e9a92fSRobert Love 	 */
216292261156SJoe Eykholt 	fc_seq_ls_acc(fp);
21633f127ad9SVasu Dev 	goto out;
216442e9a92fSRobert Love 
216542e9a92fSRobert Love unlock_reject:
216642e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
216742e9a92fSRobert Love reject:
216892261156SJoe Eykholt 	fc_seq_ls_rjt(fp, ELS_RJT_LOGIC, explan);
21693f127ad9SVasu Dev out:
21703f127ad9SVasu Dev 	if (ep)
21713f127ad9SVasu Dev 		fc_exch_release(ep);	/* drop hold from fc_exch_find */
217242e9a92fSRobert Love }
217342e9a92fSRobert Love 
21743a3b42bfSRobert Love /**
21754e5fae7aSVasu Dev  * fc_exch_update_stats() - update exches stats to lport
21764e5fae7aSVasu Dev  * @lport: The local port to update exchange manager stats
21774e5fae7aSVasu Dev  */
21784e5fae7aSVasu Dev void fc_exch_update_stats(struct fc_lport *lport)
21794e5fae7aSVasu Dev {
21804e5fae7aSVasu Dev 	struct fc_host_statistics *st;
21814e5fae7aSVasu Dev 	struct fc_exch_mgr_anchor *ema;
21824e5fae7aSVasu Dev 	struct fc_exch_mgr *mp;
21834e5fae7aSVasu Dev 
21844e5fae7aSVasu Dev 	st = &lport->host_stats;
21854e5fae7aSVasu Dev 
21864e5fae7aSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
21874e5fae7aSVasu Dev 		mp = ema->mp;
21884e5fae7aSVasu Dev 		st->fc_no_free_exch += atomic_read(&mp->stats.no_free_exch);
21894e5fae7aSVasu Dev 		st->fc_no_free_exch_xid +=
21904e5fae7aSVasu Dev 				atomic_read(&mp->stats.no_free_exch_xid);
21914e5fae7aSVasu Dev 		st->fc_xid_not_found += atomic_read(&mp->stats.xid_not_found);
21924e5fae7aSVasu Dev 		st->fc_xid_busy += atomic_read(&mp->stats.xid_busy);
21934e5fae7aSVasu Dev 		st->fc_seq_not_found += atomic_read(&mp->stats.seq_not_found);
21944e5fae7aSVasu Dev 		st->fc_non_bls_resp += atomic_read(&mp->stats.non_bls_resp);
21954e5fae7aSVasu Dev 	}
21964e5fae7aSVasu Dev }
21974e5fae7aSVasu Dev EXPORT_SYMBOL(fc_exch_update_stats);
21984e5fae7aSVasu Dev 
21994e5fae7aSVasu Dev /**
22003a3b42bfSRobert Love  * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
22013a3b42bfSRobert Love  * @lport: The local port to add the exchange manager to
22023a3b42bfSRobert Love  * @mp:	   The exchange manager to be added to the local port
22033a3b42bfSRobert Love  * @match: The match routine that indicates when this EM should be used
22043a3b42bfSRobert Love  */
220596316099SVasu Dev struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
220696316099SVasu Dev 					   struct fc_exch_mgr *mp,
220796316099SVasu Dev 					   bool (*match)(struct fc_frame *))
220896316099SVasu Dev {
220996316099SVasu Dev 	struct fc_exch_mgr_anchor *ema;
221096316099SVasu Dev 
221196316099SVasu Dev 	ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
221296316099SVasu Dev 	if (!ema)
221396316099SVasu Dev 		return ema;
221496316099SVasu Dev 
221596316099SVasu Dev 	ema->mp = mp;
221696316099SVasu Dev 	ema->match = match;
221796316099SVasu Dev 	/* add EM anchor to EM anchors list */
221896316099SVasu Dev 	list_add_tail(&ema->ema_list, &lport->ema_list);
221996316099SVasu Dev 	kref_get(&mp->kref);
222096316099SVasu Dev 	return ema;
222196316099SVasu Dev }
222296316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_add);
222396316099SVasu Dev 
22243a3b42bfSRobert Love /**
22253a3b42bfSRobert Love  * fc_exch_mgr_destroy() - Destroy an exchange manager
22263a3b42bfSRobert Love  * @kref: The reference to the EM to be destroyed
22273a3b42bfSRobert Love  */
222896316099SVasu Dev static void fc_exch_mgr_destroy(struct kref *kref)
222996316099SVasu Dev {
223096316099SVasu Dev 	struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
223196316099SVasu Dev 
223296316099SVasu Dev 	mempool_destroy(mp->ep_pool);
2233e4bc50beSVasu Dev 	free_percpu(mp->pool);
223496316099SVasu Dev 	kfree(mp);
223596316099SVasu Dev }
223696316099SVasu Dev 
22373a3b42bfSRobert Love /**
22383a3b42bfSRobert Love  * fc_exch_mgr_del() - Delete an EM from a local port's list
22393a3b42bfSRobert Love  * @ema: The exchange manager anchor identifying the EM to be deleted
22403a3b42bfSRobert Love  */
224196316099SVasu Dev void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
224296316099SVasu Dev {
224396316099SVasu Dev 	/* remove EM anchor from EM anchors list */
224496316099SVasu Dev 	list_del(&ema->ema_list);
224596316099SVasu Dev 	kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
224696316099SVasu Dev 	kfree(ema);
224796316099SVasu Dev }
224896316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_del);
224996316099SVasu Dev 
2250174e1ebfSChris Leech /**
22513a3b42bfSRobert Love  * fc_exch_mgr_list_clone() - Share all exchange manager objects
22523a3b42bfSRobert Love  * @src: Source lport to clone exchange managers from
22533a3b42bfSRobert Love  * @dst: New lport that takes references to all the exchange managers
2254174e1ebfSChris Leech  */
2255174e1ebfSChris Leech int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
2256174e1ebfSChris Leech {
2257174e1ebfSChris Leech 	struct fc_exch_mgr_anchor *ema, *tmp;
2258174e1ebfSChris Leech 
2259174e1ebfSChris Leech 	list_for_each_entry(ema, &src->ema_list, ema_list) {
2260174e1ebfSChris Leech 		if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
2261174e1ebfSChris Leech 			goto err;
2262174e1ebfSChris Leech 	}
2263174e1ebfSChris Leech 	return 0;
2264174e1ebfSChris Leech err:
2265174e1ebfSChris Leech 	list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
2266174e1ebfSChris Leech 		fc_exch_mgr_del(ema);
2267174e1ebfSChris Leech 	return -ENOMEM;
2268174e1ebfSChris Leech }
226972fa396bSVasu Dev EXPORT_SYMBOL(fc_exch_mgr_list_clone);
2270174e1ebfSChris Leech 
22713a3b42bfSRobert Love /**
22723a3b42bfSRobert Love  * fc_exch_mgr_alloc() - Allocate an exchange manager
22733a3b42bfSRobert Love  * @lport:   The local port that the new EM will be associated with
22743a3b42bfSRobert Love  * @class:   The default FC class for new exchanges
22753a3b42bfSRobert Love  * @min_xid: The minimum XID for exchanges from the new EM
22763a3b42bfSRobert Love  * @max_xid: The maximum XID for exchanges from the new EM
22773a3b42bfSRobert Love  * @match:   The match routine for the new EM
22783a3b42bfSRobert Love  */
22793a3b42bfSRobert Love struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
228042e9a92fSRobert Love 				      enum fc_class class,
228152ff878cSVasu Dev 				      u16 min_xid, u16 max_xid,
228252ff878cSVasu Dev 				      bool (*match)(struct fc_frame *))
228342e9a92fSRobert Love {
228442e9a92fSRobert Love 	struct fc_exch_mgr *mp;
2285e4bc50beSVasu Dev 	u16 pool_exch_range;
2286e4bc50beSVasu Dev 	size_t pool_size;
2287e4bc50beSVasu Dev 	unsigned int cpu;
2288e4bc50beSVasu Dev 	struct fc_exch_pool *pool;
228942e9a92fSRobert Love 
2290e4bc50beSVasu Dev 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
2291e4bc50beSVasu Dev 	    (min_xid & fc_cpu_mask) != 0) {
22923a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
229342e9a92fSRobert Love 			     min_xid, max_xid);
229442e9a92fSRobert Love 		return NULL;
229542e9a92fSRobert Love 	}
229642e9a92fSRobert Love 
229742e9a92fSRobert Love 	/*
2298b2f0091fSVasu Dev 	 * allocate memory for EM
229942e9a92fSRobert Love 	 */
2300b2f0091fSVasu Dev 	mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
230142e9a92fSRobert Love 	if (!mp)
230242e9a92fSRobert Love 		return NULL;
230342e9a92fSRobert Love 
230442e9a92fSRobert Love 	mp->class = class;
230542e9a92fSRobert Love 	/* adjust em exch xid range for offload */
230642e9a92fSRobert Love 	mp->min_xid = min_xid;
2307011a9008SSteven Clark 
2308011a9008SSteven Clark        /* reduce range so per cpu pool fits into PCPU_MIN_UNIT_SIZE pool */
2309011a9008SSteven Clark 	pool_exch_range = (PCPU_MIN_UNIT_SIZE - sizeof(*pool)) /
2310011a9008SSteven Clark 		sizeof(struct fc_exch *);
2311011a9008SSteven Clark 	if ((max_xid - min_xid + 1) / (fc_cpu_mask + 1) > pool_exch_range) {
2312011a9008SSteven Clark 		mp->max_xid = pool_exch_range * (fc_cpu_mask + 1) +
2313011a9008SSteven Clark 			min_xid - 1;
2314011a9008SSteven Clark 	} else {
231542e9a92fSRobert Love 		mp->max_xid = max_xid;
2316011a9008SSteven Clark 		pool_exch_range = (mp->max_xid - mp->min_xid + 1) /
2317011a9008SSteven Clark 			(fc_cpu_mask + 1);
2318011a9008SSteven Clark 	}
231942e9a92fSRobert Love 
232042e9a92fSRobert Love 	mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
232142e9a92fSRobert Love 	if (!mp->ep_pool)
232242e9a92fSRobert Love 		goto free_mp;
232342e9a92fSRobert Love 
2324e4bc50beSVasu Dev 	/*
2325e4bc50beSVasu Dev 	 * Setup per cpu exch pool with entire exchange id range equally
2326e4bc50beSVasu Dev 	 * divided across all cpus. The exch pointers array memory is
2327e4bc50beSVasu Dev 	 * allocated for exch range per pool.
2328e4bc50beSVasu Dev 	 */
2329e4bc50beSVasu Dev 	mp->pool_max_index = pool_exch_range - 1;
2330e4bc50beSVasu Dev 
2331e4bc50beSVasu Dev 	/*
2332e4bc50beSVasu Dev 	 * Allocate and initialize per cpu exch pool
2333e4bc50beSVasu Dev 	 */
2334e4bc50beSVasu Dev 	pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
2335e4bc50beSVasu Dev 	mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
2336e4bc50beSVasu Dev 	if (!mp->pool)
2337e4bc50beSVasu Dev 		goto free_mempool;
2338e4bc50beSVasu Dev 	for_each_possible_cpu(cpu) {
2339e4bc50beSVasu Dev 		pool = per_cpu_ptr(mp->pool, cpu);
2340b6e3c840SVasu Dev 		pool->next_index = 0;
23412034c19cSHillf Danton 		pool->left = FC_XID_UNKNOWN;
23422034c19cSHillf Danton 		pool->right = FC_XID_UNKNOWN;
2343e4bc50beSVasu Dev 		spin_lock_init(&pool->lock);
2344e4bc50beSVasu Dev 		INIT_LIST_HEAD(&pool->ex_list);
2345e4bc50beSVasu Dev 	}
2346e4bc50beSVasu Dev 
234752ff878cSVasu Dev 	kref_init(&mp->kref);
23483a3b42bfSRobert Love 	if (!fc_exch_mgr_add(lport, mp, match)) {
2349e4bc50beSVasu Dev 		free_percpu(mp->pool);
2350e4bc50beSVasu Dev 		goto free_mempool;
235152ff878cSVasu Dev 	}
235252ff878cSVasu Dev 
235352ff878cSVasu Dev 	/*
235452ff878cSVasu Dev 	 * Above kref_init() sets mp->kref to 1 and then
235552ff878cSVasu Dev 	 * call to fc_exch_mgr_add incremented mp->kref again,
235652ff878cSVasu Dev 	 * so adjust that extra increment.
235752ff878cSVasu Dev 	 */
235852ff878cSVasu Dev 	kref_put(&mp->kref, fc_exch_mgr_destroy);
235942e9a92fSRobert Love 	return mp;
236042e9a92fSRobert Love 
2361e4bc50beSVasu Dev free_mempool:
2362e4bc50beSVasu Dev 	mempool_destroy(mp->ep_pool);
236342e9a92fSRobert Love free_mp:
236442e9a92fSRobert Love 	kfree(mp);
236542e9a92fSRobert Love 	return NULL;
236642e9a92fSRobert Love }
236742e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_alloc);
236842e9a92fSRobert Love 
23693a3b42bfSRobert Love /**
23703a3b42bfSRobert Love  * fc_exch_mgr_free() - Free all exchange managers on a local port
23713a3b42bfSRobert Love  * @lport: The local port whose EMs are to be freed
23723a3b42bfSRobert Love  */
237352ff878cSVasu Dev void fc_exch_mgr_free(struct fc_lport *lport)
237442e9a92fSRobert Love {
237552ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema, *next;
237652ff878cSVasu Dev 
23774ae1e19fSVasu Dev 	flush_workqueue(fc_exch_workqueue);
237852ff878cSVasu Dev 	list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
237952ff878cSVasu Dev 		fc_exch_mgr_del(ema);
238042e9a92fSRobert Love }
238142e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_free);
238242e9a92fSRobert Love 
23833a3b42bfSRobert Love /**
23846c8cc1c0SKiran Patil  * fc_find_ema() - Lookup and return appropriate Exchange Manager Anchor depending
23856c8cc1c0SKiran Patil  * upon 'xid'.
23866c8cc1c0SKiran Patil  * @f_ctl: f_ctl
23876c8cc1c0SKiran Patil  * @lport: The local port the frame was received on
23886c8cc1c0SKiran Patil  * @fh: The received frame header
23896c8cc1c0SKiran Patil  */
23906c8cc1c0SKiran Patil static struct fc_exch_mgr_anchor *fc_find_ema(u32 f_ctl,
23916c8cc1c0SKiran Patil 					      struct fc_lport *lport,
23926c8cc1c0SKiran Patil 					      struct fc_frame_header *fh)
23936c8cc1c0SKiran Patil {
23946c8cc1c0SKiran Patil 	struct fc_exch_mgr_anchor *ema;
23956c8cc1c0SKiran Patil 	u16 xid;
23966c8cc1c0SKiran Patil 
23976c8cc1c0SKiran Patil 	if (f_ctl & FC_FC_EX_CTX)
23986c8cc1c0SKiran Patil 		xid = ntohs(fh->fh_ox_id);
23996c8cc1c0SKiran Patil 	else {
24006c8cc1c0SKiran Patil 		xid = ntohs(fh->fh_rx_id);
24016c8cc1c0SKiran Patil 		if (xid == FC_XID_UNKNOWN)
24026c8cc1c0SKiran Patil 			return list_entry(lport->ema_list.prev,
24036c8cc1c0SKiran Patil 					  typeof(*ema), ema_list);
24046c8cc1c0SKiran Patil 	}
24056c8cc1c0SKiran Patil 
24066c8cc1c0SKiran Patil 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
24076c8cc1c0SKiran Patil 		if ((xid >= ema->mp->min_xid) &&
24086c8cc1c0SKiran Patil 		    (xid <= ema->mp->max_xid))
24096c8cc1c0SKiran Patil 			return ema;
24106c8cc1c0SKiran Patil 	}
24116c8cc1c0SKiran Patil 	return NULL;
24126c8cc1c0SKiran Patil }
24136c8cc1c0SKiran Patil /**
24143a3b42bfSRobert Love  * fc_exch_recv() - Handler for received frames
24153a3b42bfSRobert Love  * @lport: The local port the frame was received on
24163a3b42bfSRobert Love  * @fp:	The received frame
241742e9a92fSRobert Love  */
24183a3b42bfSRobert Love void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
241942e9a92fSRobert Love {
242042e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
242152ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
24226c8cc1c0SKiran Patil 	u32 f_ctl;
242342e9a92fSRobert Love 
242442e9a92fSRobert Love 	/* lport lock ? */
24253a3b42bfSRobert Love 	if (!lport || lport->state == LPORT_ST_DISABLED) {
24263a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "Receiving frames for an lport that "
24277414705eSRobert Love 			     "has not been initialized correctly\n");
242842e9a92fSRobert Love 		fc_frame_free(fp);
242942e9a92fSRobert Love 		return;
243042e9a92fSRobert Love 	}
243142e9a92fSRobert Love 
243252ff878cSVasu Dev 	f_ctl = ntoh24(fh->fh_f_ctl);
24336c8cc1c0SKiran Patil 	ema = fc_find_ema(f_ctl, lport, fh);
24346c8cc1c0SKiran Patil 	if (!ema) {
24356c8cc1c0SKiran Patil 		FC_LPORT_DBG(lport, "Unable to find Exchange Manager Anchor,"
24366c8cc1c0SKiran Patil 				    "fc_ctl <0x%x>, xid <0x%x>\n",
24376c8cc1c0SKiran Patil 				     f_ctl,
24386c8cc1c0SKiran Patil 				     (f_ctl & FC_FC_EX_CTX) ?
24396c8cc1c0SKiran Patil 				     ntohs(fh->fh_ox_id) :
24406c8cc1c0SKiran Patil 				     ntohs(fh->fh_rx_id));
244152ff878cSVasu Dev 		fc_frame_free(fp);
244252ff878cSVasu Dev 		return;
244352ff878cSVasu Dev 	}
244452ff878cSVasu Dev 
244542e9a92fSRobert Love 	/*
244642e9a92fSRobert Love 	 * If frame is marked invalid, just drop it.
244742e9a92fSRobert Love 	 */
244842e9a92fSRobert Love 	switch (fr_eof(fp)) {
244942e9a92fSRobert Love 	case FC_EOF_T:
245042e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
245142e9a92fSRobert Love 			skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
245242e9a92fSRobert Love 		/* fall through */
245342e9a92fSRobert Love 	case FC_EOF_N:
245442e9a92fSRobert Love 		if (fh->fh_type == FC_TYPE_BLS)
245552ff878cSVasu Dev 			fc_exch_recv_bls(ema->mp, fp);
245642e9a92fSRobert Love 		else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
245742e9a92fSRobert Love 			 FC_FC_EX_CTX)
245852ff878cSVasu Dev 			fc_exch_recv_seq_resp(ema->mp, fp);
245942e9a92fSRobert Love 		else if (f_ctl & FC_FC_SEQ_CTX)
246052ff878cSVasu Dev 			fc_exch_recv_resp(ema->mp, fp);
246192261156SJoe Eykholt 		else	/* no EX_CTX and no SEQ_CTX */
24623a3b42bfSRobert Love 			fc_exch_recv_req(lport, ema->mp, fp);
246342e9a92fSRobert Love 		break;
246442e9a92fSRobert Love 	default:
24653a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
24663a3b42bfSRobert Love 			     fr_eof(fp));
246742e9a92fSRobert Love 		fc_frame_free(fp);
246842e9a92fSRobert Love 	}
246942e9a92fSRobert Love }
247042e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_recv);
247142e9a92fSRobert Love 
24723a3b42bfSRobert Love /**
24733a3b42bfSRobert Love  * fc_exch_init() - Initialize the exchange layer for a local port
24743a3b42bfSRobert Love  * @lport: The local port to initialize the exchange layer for
24753a3b42bfSRobert Love  */
24763a3b42bfSRobert Love int fc_exch_init(struct fc_lport *lport)
247742e9a92fSRobert Love {
24783a3b42bfSRobert Love 	if (!lport->tt.seq_start_next)
24793a3b42bfSRobert Love 		lport->tt.seq_start_next = fc_seq_start_next;
248042e9a92fSRobert Love 
24811a5c2d7eSJoe Eykholt 	if (!lport->tt.seq_set_resp)
24821a5c2d7eSJoe Eykholt 		lport->tt.seq_set_resp = fc_seq_set_resp;
24831a5c2d7eSJoe Eykholt 
24843a3b42bfSRobert Love 	if (!lport->tt.exch_seq_send)
24853a3b42bfSRobert Love 		lport->tt.exch_seq_send = fc_exch_seq_send;
248642e9a92fSRobert Love 
24873a3b42bfSRobert Love 	if (!lport->tt.seq_send)
24883a3b42bfSRobert Love 		lport->tt.seq_send = fc_seq_send;
248942e9a92fSRobert Love 
24903a3b42bfSRobert Love 	if (!lport->tt.seq_els_rsp_send)
24913a3b42bfSRobert Love 		lport->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
249242e9a92fSRobert Love 
24933a3b42bfSRobert Love 	if (!lport->tt.exch_done)
24943a3b42bfSRobert Love 		lport->tt.exch_done = fc_exch_done;
249542e9a92fSRobert Love 
24963a3b42bfSRobert Love 	if (!lport->tt.exch_mgr_reset)
24973a3b42bfSRobert Love 		lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
249842e9a92fSRobert Love 
24993a3b42bfSRobert Love 	if (!lport->tt.seq_exch_abort)
25003a3b42bfSRobert Love 		lport->tt.seq_exch_abort = fc_seq_exch_abort;
250142e9a92fSRobert Love 
2502239e8104SJoe Eykholt 	if (!lport->tt.seq_assign)
2503239e8104SJoe Eykholt 		lport->tt.seq_assign = fc_seq_assign;
2504239e8104SJoe Eykholt 
250562bdb645SJoe Eykholt 	if (!lport->tt.seq_release)
250662bdb645SJoe Eykholt 		lport->tt.seq_release = fc_seq_release;
250762bdb645SJoe Eykholt 
250889f19a59SVasu Dev 	return 0;
250989f19a59SVasu Dev }
251089f19a59SVasu Dev EXPORT_SYMBOL(fc_exch_init);
251189f19a59SVasu Dev 
251289f19a59SVasu Dev /**
251389f19a59SVasu Dev  * fc_setup_exch_mgr() - Setup an exchange manager
251489f19a59SVasu Dev  */
251555204909SRandy Dunlap int fc_setup_exch_mgr(void)
251689f19a59SVasu Dev {
251789f19a59SVasu Dev 	fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
251889f19a59SVasu Dev 					 0, SLAB_HWCACHE_ALIGN, NULL);
251989f19a59SVasu Dev 	if (!fc_em_cachep)
252089f19a59SVasu Dev 		return -ENOMEM;
252189f19a59SVasu Dev 
2522e4bc50beSVasu Dev 	/*
2523e4bc50beSVasu Dev 	 * Initialize fc_cpu_mask and fc_cpu_order. The
2524e4bc50beSVasu Dev 	 * fc_cpu_mask is set for nr_cpu_ids rounded up
2525e4bc50beSVasu Dev 	 * to order of 2's * power and order is stored
2526e4bc50beSVasu Dev 	 * in fc_cpu_order as this is later required in
2527e4bc50beSVasu Dev 	 * mapping between an exch id and exch array index
2528e4bc50beSVasu Dev 	 * in per cpu exch pool.
2529e4bc50beSVasu Dev 	 *
2530e4bc50beSVasu Dev 	 * This round up is required to align fc_cpu_mask
2531e4bc50beSVasu Dev 	 * to exchange id's lower bits such that all incoming
2532e4bc50beSVasu Dev 	 * frames of an exchange gets delivered to the same
2533e4bc50beSVasu Dev 	 * cpu on which exchange originated by simple bitwise
2534e4bc50beSVasu Dev 	 * AND operation between fc_cpu_mask and exchange id.
2535e4bc50beSVasu Dev 	 */
2536e4bc50beSVasu Dev 	fc_cpu_mask = 1;
2537e4bc50beSVasu Dev 	fc_cpu_order = 0;
2538e4bc50beSVasu Dev 	while (fc_cpu_mask < nr_cpu_ids) {
2539e4bc50beSVasu Dev 		fc_cpu_mask <<= 1;
2540e4bc50beSVasu Dev 		fc_cpu_order++;
2541e4bc50beSVasu Dev 	}
2542e4bc50beSVasu Dev 	fc_cpu_mask--;
2543e4bc50beSVasu Dev 
25444ae1e19fSVasu Dev 	fc_exch_workqueue = create_singlethread_workqueue("fc_exch_workqueue");
25454ae1e19fSVasu Dev 	if (!fc_exch_workqueue)
25466f06e3a7SHillf Danton 		goto err;
254742e9a92fSRobert Love 	return 0;
25486f06e3a7SHillf Danton err:
25496f06e3a7SHillf Danton 	kmem_cache_destroy(fc_em_cachep);
25506f06e3a7SHillf Danton 	return -ENOMEM;
255142e9a92fSRobert Love }
255242e9a92fSRobert Love 
25533a3b42bfSRobert Love /**
25543a3b42bfSRobert Love  * fc_destroy_exch_mgr() - Destroy an exchange manager
25553a3b42bfSRobert Love  */
255655204909SRandy Dunlap void fc_destroy_exch_mgr(void)
255742e9a92fSRobert Love {
25584ae1e19fSVasu Dev 	destroy_workqueue(fc_exch_workqueue);
255942e9a92fSRobert Love 	kmem_cache_destroy(fc_em_cachep);
256042e9a92fSRobert Love }
2561