xref: /linux/drivers/scsi/libfc/fc_exch.c (revision 3a3b42bf89a9b90ae9ed2c57fdc378e5473a0ef9)
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>
2742e9a92fSRobert Love #include <linux/gfp.h>
2842e9a92fSRobert Love #include <linux/err.h>
2942e9a92fSRobert Love 
3042e9a92fSRobert Love #include <scsi/fc/fc_fc2.h>
3142e9a92fSRobert Love 
3242e9a92fSRobert Love #include <scsi/libfc.h>
3342e9a92fSRobert Love #include <scsi/fc_encode.h>
3442e9a92fSRobert Love 
358866a5d9SRobert Love #include "fc_libfc.h"
368866a5d9SRobert Love 
37e4bc50beSVasu Dev u16	fc_cpu_mask;		/* cpu mask for possible cpus */
38e4bc50beSVasu Dev EXPORT_SYMBOL(fc_cpu_mask);
39e4bc50beSVasu Dev static u16	fc_cpu_order;	/* 2's power to represent total possible cpus */
4042e9a92fSRobert Love static struct kmem_cache *fc_em_cachep;	       /* cache for exchanges */
4142e9a92fSRobert Love 
4242e9a92fSRobert Love /*
4342e9a92fSRobert Love  * Structure and function definitions for managing Fibre Channel Exchanges
4442e9a92fSRobert Love  * and Sequences.
4542e9a92fSRobert Love  *
4642e9a92fSRobert Love  * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
4742e9a92fSRobert Love  *
4842e9a92fSRobert Love  * fc_exch_mgr holds the exchange state for an N port
4942e9a92fSRobert Love  *
5042e9a92fSRobert Love  * fc_exch holds state for one exchange and links to its active sequence.
5142e9a92fSRobert Love  *
5242e9a92fSRobert Love  * fc_seq holds the state for an individual sequence.
5342e9a92fSRobert Love  */
5442e9a92fSRobert Love 
55*3a3b42bfSRobert Love /**
56*3a3b42bfSRobert Love  * struct fc_exch_pool - Per cpu exchange pool
57*3a3b42bfSRobert Love  * @next_index:	  Next possible free exchange index
58*3a3b42bfSRobert Love  * @total_exches: Total allocated exchanges
59*3a3b42bfSRobert Love  * @lock:	  Exch pool lock
60*3a3b42bfSRobert Love  * @ex_list:	  List of exchanges
61e4bc50beSVasu Dev  *
62e4bc50beSVasu Dev  * This structure manages per cpu exchanges in array of exchange pointers.
63e4bc50beSVasu Dev  * This array is allocated followed by struct fc_exch_pool memory for
64e4bc50beSVasu Dev  * assigned range of exchanges to per cpu pool.
65e4bc50beSVasu Dev  */
66e4bc50beSVasu Dev struct fc_exch_pool {
67*3a3b42bfSRobert Love 	u16		 next_index;
68*3a3b42bfSRobert Love 	u16		 total_exches;
69*3a3b42bfSRobert Love 	spinlock_t	 lock;
70*3a3b42bfSRobert Love 	struct list_head ex_list;
71e4bc50beSVasu Dev };
72e4bc50beSVasu Dev 
73*3a3b42bfSRobert Love /**
74*3a3b42bfSRobert Love  * struct fc_exch_mgr - The Exchange Manager (EM).
75*3a3b42bfSRobert Love  * @class:	    Default class for new sequences
76*3a3b42bfSRobert Love  * @kref:	    Reference counter
77*3a3b42bfSRobert Love  * @min_xid:	    Minimum exchange ID
78*3a3b42bfSRobert Love  * @max_xid:	    Maximum exchange ID
79*3a3b42bfSRobert Love  * @ep_pool:	    Reserved exchange pointers
80*3a3b42bfSRobert Love  * @pool_max_index: Max exch array index in exch pool
81*3a3b42bfSRobert Love  * @pool:	    Per cpu exch pool
82*3a3b42bfSRobert Love  * @stats:	    Statistics structure
8342e9a92fSRobert Love  *
8442e9a92fSRobert Love  * This structure is the center for creating exchanges and sequences.
8542e9a92fSRobert Love  * It manages the allocation of exchange IDs.
8642e9a92fSRobert Love  */
8742e9a92fSRobert Love struct fc_exch_mgr {
88*3a3b42bfSRobert Love 	enum fc_class	class;
89*3a3b42bfSRobert Love 	struct kref	kref;
90*3a3b42bfSRobert Love 	u16		min_xid;
91*3a3b42bfSRobert Love 	u16		max_xid;
92*3a3b42bfSRobert Love 	mempool_t	*ep_pool;
93*3a3b42bfSRobert Love 	u16		pool_max_index;
94*3a3b42bfSRobert Love 	struct fc_exch_pool *pool;
9542e9a92fSRobert Love 
9642e9a92fSRobert Love 	/*
9742e9a92fSRobert Love 	 * currently exchange mgr stats are updated but not used.
9842e9a92fSRobert Love 	 * either stats can be expose via sysfs or remove them
9942e9a92fSRobert Love 	 * all together if not used XXX
10042e9a92fSRobert Love 	 */
10142e9a92fSRobert Love 	struct {
10242e9a92fSRobert Love 		atomic_t no_free_exch;
10342e9a92fSRobert Love 		atomic_t no_free_exch_xid;
10442e9a92fSRobert Love 		atomic_t xid_not_found;
10542e9a92fSRobert Love 		atomic_t xid_busy;
10642e9a92fSRobert Love 		atomic_t seq_not_found;
10742e9a92fSRobert Love 		atomic_t non_bls_resp;
10842e9a92fSRobert Love 	} stats;
10942e9a92fSRobert Love };
11042e9a92fSRobert Love #define	fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
11142e9a92fSRobert Love 
112*3a3b42bfSRobert Love /**
113*3a3b42bfSRobert Love  * struct fc_exch_mgr_anchor - primary structure for list of EMs
114*3a3b42bfSRobert Love  * @ema_list: Exchange Manager Anchor list
115*3a3b42bfSRobert Love  * @mp:	      Exchange Manager associated with this anchor
116*3a3b42bfSRobert Love  * @match:    Routine to determine if this anchor's EM should be used
117*3a3b42bfSRobert Love  *
118*3a3b42bfSRobert Love  * When walking the list of anchors the match routine will be called
119*3a3b42bfSRobert Love  * for each anchor to determine if that EM should be used. The last
120*3a3b42bfSRobert Love  * anchor in the list will always match to handle any exchanges not
121*3a3b42bfSRobert Love  * handled by other EMs. The non-default EMs would be added to the
122*3a3b42bfSRobert Love  * anchor list by HW that provides FCoE offloads.
123*3a3b42bfSRobert 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 *);
13142e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *);
13242e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason,
13342e9a92fSRobert Love 			  enum fc_els_rjt_explan);
13442e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *);
13542e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *, 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 
22142e9a92fSRobert Love #define FC_TABLE_SIZE(x)   (sizeof(x) / sizeof(x[0]))
22242e9a92fSRobert Love 
223*3a3b42bfSRobert Love /**
224*3a3b42bfSRobert Love  * fc_exch_name_lookup() - Lookup name by opcode
225*3a3b42bfSRobert Love  * @op:	       Opcode to be looked up
226*3a3b42bfSRobert Love  * @table:     Opcode/name table
227*3a3b42bfSRobert Love  * @max_index: Index not to be exceeded
228*3a3b42bfSRobert Love  *
229*3a3b42bfSRobert Love  * This routine is used to determine a human-readable string identifying
230*3a3b42bfSRobert Love  * a R_CTL opcode.
231*3a3b42bfSRobert Love  */
23242e9a92fSRobert Love static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
23342e9a92fSRobert Love 					      unsigned int max_index)
23442e9a92fSRobert Love {
23542e9a92fSRobert Love 	const char *name = NULL;
23642e9a92fSRobert Love 
23742e9a92fSRobert Love 	if (op < max_index)
23842e9a92fSRobert Love 		name = table[op];
23942e9a92fSRobert Love 	if (!name)
24042e9a92fSRobert Love 		name = "unknown";
24142e9a92fSRobert Love 	return name;
24242e9a92fSRobert Love }
24342e9a92fSRobert Love 
244*3a3b42bfSRobert Love /**
245*3a3b42bfSRobert Love  * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
246*3a3b42bfSRobert Love  * @op: The opcode to be looked up
247*3a3b42bfSRobert Love  */
24842e9a92fSRobert Love static const char *fc_exch_rctl_name(unsigned int op)
24942e9a92fSRobert Love {
25042e9a92fSRobert Love 	return fc_exch_name_lookup(op, fc_exch_rctl_names,
25142e9a92fSRobert Love 				   FC_TABLE_SIZE(fc_exch_rctl_names));
25242e9a92fSRobert Love }
25342e9a92fSRobert Love 
254*3a3b42bfSRobert Love /**
255*3a3b42bfSRobert Love  * fc_exch_hold() - Increment an exchange's reference count
256*3a3b42bfSRobert Love  * @ep: Echange to be held
25742e9a92fSRobert Love  */
258*3a3b42bfSRobert Love static inline void fc_exch_hold(struct fc_exch *ep)
25942e9a92fSRobert Love {
26042e9a92fSRobert Love 	atomic_inc(&ep->ex_refcnt);
26142e9a92fSRobert Love }
26242e9a92fSRobert Love 
263*3a3b42bfSRobert Love /**
264*3a3b42bfSRobert Love  * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
265*3a3b42bfSRobert Love  *			 and determine SOF and EOF.
266*3a3b42bfSRobert Love  * @ep:	   The exchange to that will use the header
267*3a3b42bfSRobert Love  * @fp:	   The frame whose header is to be modified
268*3a3b42bfSRobert Love  * @f_ctl: F_CTL bits that will be used for the frame header
269*3a3b42bfSRobert Love  *
270*3a3b42bfSRobert Love  * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
271*3a3b42bfSRobert Love  * fh_seq_id, fh_seq_cnt and the SOF and EOF.
27242e9a92fSRobert Love  */
27342e9a92fSRobert Love static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
27442e9a92fSRobert Love 			      u32 f_ctl)
27542e9a92fSRobert Love {
27642e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
27742e9a92fSRobert Love 	u16 fill;
27842e9a92fSRobert Love 
27942e9a92fSRobert Love 	fr_sof(fp) = ep->class;
28042e9a92fSRobert Love 	if (ep->seq.cnt)
28142e9a92fSRobert Love 		fr_sof(fp) = fc_sof_normal(ep->class);
28242e9a92fSRobert Love 
28342e9a92fSRobert Love 	if (f_ctl & FC_FC_END_SEQ) {
28442e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_T;
28542e9a92fSRobert Love 		if (fc_sof_needs_ack(ep->class))
28642e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
28742e9a92fSRobert Love 		/*
288*3a3b42bfSRobert Love 		 * From F_CTL.
28942e9a92fSRobert Love 		 * The number of fill bytes to make the length a 4-byte
29042e9a92fSRobert Love 		 * multiple is the low order 2-bits of the f_ctl.
29142e9a92fSRobert Love 		 * The fill itself will have been cleared by the frame
29242e9a92fSRobert Love 		 * allocation.
29342e9a92fSRobert Love 		 * After this, the length will be even, as expected by
29442e9a92fSRobert Love 		 * the transport.
29542e9a92fSRobert Love 		 */
29642e9a92fSRobert Love 		fill = fr_len(fp) & 3;
29742e9a92fSRobert Love 		if (fill) {
29842e9a92fSRobert Love 			fill = 4 - fill;
29942e9a92fSRobert Love 			/* TODO, this may be a problem with fragmented skb */
30042e9a92fSRobert Love 			skb_put(fp_skb(fp), fill);
30142e9a92fSRobert Love 			hton24(fh->fh_f_ctl, f_ctl | fill);
30242e9a92fSRobert Love 		}
30342e9a92fSRobert Love 	} else {
30442e9a92fSRobert Love 		WARN_ON(fr_len(fp) % 4 != 0);	/* no pad to non last frame */
30542e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
30642e9a92fSRobert Love 	}
30742e9a92fSRobert Love 
30842e9a92fSRobert Love 	/*
30942e9a92fSRobert Love 	 * Initialize remainig fh fields
31042e9a92fSRobert Love 	 * from fc_fill_fc_hdr
31142e9a92fSRobert Love 	 */
31242e9a92fSRobert Love 	fh->fh_ox_id = htons(ep->oxid);
31342e9a92fSRobert Love 	fh->fh_rx_id = htons(ep->rxid);
31442e9a92fSRobert Love 	fh->fh_seq_id = ep->seq.id;
31542e9a92fSRobert Love 	fh->fh_seq_cnt = htons(ep->seq.cnt);
31642e9a92fSRobert Love }
31742e9a92fSRobert Love 
318*3a3b42bfSRobert Love /**
319*3a3b42bfSRobert Love  * fc_exch_release() - Decrement an exchange's reference count
320*3a3b42bfSRobert Love  * @ep: Exchange to be released
321*3a3b42bfSRobert Love  *
322*3a3b42bfSRobert Love  * If the reference count reaches zero and the exchange is complete,
323*3a3b42bfSRobert Love  * it is freed.
32442e9a92fSRobert Love  */
32542e9a92fSRobert Love static void fc_exch_release(struct fc_exch *ep)
32642e9a92fSRobert Love {
32742e9a92fSRobert Love 	struct fc_exch_mgr *mp;
32842e9a92fSRobert Love 
32942e9a92fSRobert Love 	if (atomic_dec_and_test(&ep->ex_refcnt)) {
33042e9a92fSRobert Love 		mp = ep->em;
33142e9a92fSRobert Love 		if (ep->destructor)
33242e9a92fSRobert Love 			ep->destructor(&ep->seq, ep->arg);
333aa6cd29bSJulia Lawall 		WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
33442e9a92fSRobert Love 		mempool_free(ep, mp->ep_pool);
33542e9a92fSRobert Love 	}
33642e9a92fSRobert Love }
33742e9a92fSRobert Love 
338*3a3b42bfSRobert Love /**
339*3a3b42bfSRobert Love  * fc_exch_done_locked() - Complete an exchange with the exchange lock held
340*3a3b42bfSRobert Love  * @ep: The exchange that is complete
341*3a3b42bfSRobert Love  */
34242e9a92fSRobert Love static int fc_exch_done_locked(struct fc_exch *ep)
34342e9a92fSRobert Love {
34442e9a92fSRobert Love 	int rc = 1;
34542e9a92fSRobert Love 
34642e9a92fSRobert Love 	/*
34742e9a92fSRobert Love 	 * We must check for completion in case there are two threads
34842e9a92fSRobert Love 	 * tyring to complete this. But the rrq code will reuse the
34942e9a92fSRobert Love 	 * ep, and in that case we only clear the resp and set it as
35042e9a92fSRobert Love 	 * complete, so it can be reused by the timer to send the rrq.
35142e9a92fSRobert Love 	 */
35242e9a92fSRobert Love 	ep->resp = NULL;
35342e9a92fSRobert Love 	if (ep->state & FC_EX_DONE)
35442e9a92fSRobert Love 		return rc;
35542e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_COMPLETE;
35642e9a92fSRobert Love 
35742e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
35842e9a92fSRobert Love 		ep->state |= FC_EX_DONE;
35942e9a92fSRobert Love 		if (cancel_delayed_work(&ep->timeout_work))
36042e9a92fSRobert Love 			atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
36142e9a92fSRobert Love 		rc = 0;
36242e9a92fSRobert Love 	}
36342e9a92fSRobert Love 	return rc;
36442e9a92fSRobert Love }
36542e9a92fSRobert Love 
366*3a3b42bfSRobert Love /**
367*3a3b42bfSRobert Love  * fc_exch_ptr_get() - Return an exchange from an exchange pool
368*3a3b42bfSRobert Love  * @pool:  Exchange Pool to get an exchange from
369*3a3b42bfSRobert Love  * @index: Index of the exchange within the pool
370*3a3b42bfSRobert Love  *
371*3a3b42bfSRobert Love  * Use the index to get an exchange from within an exchange pool. exches
372*3a3b42bfSRobert Love  * will point to an array of exchange pointers. The index will select
373*3a3b42bfSRobert Love  * the exchange within the array.
374*3a3b42bfSRobert Love  */
375e4bc50beSVasu Dev static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
376e4bc50beSVasu Dev 					      u16 index)
377e4bc50beSVasu Dev {
378e4bc50beSVasu Dev 	struct fc_exch **exches = (struct fc_exch **)(pool + 1);
379e4bc50beSVasu Dev 	return exches[index];
380e4bc50beSVasu Dev }
381e4bc50beSVasu Dev 
382*3a3b42bfSRobert Love /**
383*3a3b42bfSRobert Love  * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
384*3a3b42bfSRobert Love  * @pool:  The pool to assign the exchange to
385*3a3b42bfSRobert Love  * @index: The index in the pool where the exchange will be assigned
386*3a3b42bfSRobert Love  * @ep:	   The exchange to assign to the pool
387*3a3b42bfSRobert Love  */
388e4bc50beSVasu Dev static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
389e4bc50beSVasu Dev 				   struct fc_exch *ep)
390e4bc50beSVasu Dev {
391e4bc50beSVasu Dev 	((struct fc_exch **)(pool + 1))[index] = ep;
392e4bc50beSVasu Dev }
393e4bc50beSVasu Dev 
394*3a3b42bfSRobert Love /**
395*3a3b42bfSRobert Love  * fc_exch_delete() - Delete an exchange
396*3a3b42bfSRobert Love  * @ep: The exchange to be deleted
397*3a3b42bfSRobert Love  */
398b2f0091fSVasu Dev static void fc_exch_delete(struct fc_exch *ep)
39942e9a92fSRobert Love {
400b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
40142e9a92fSRobert Love 
402b2f0091fSVasu Dev 	pool = ep->pool;
403b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
404b2f0091fSVasu Dev 	WARN_ON(pool->total_exches <= 0);
405b2f0091fSVasu Dev 	pool->total_exches--;
406b2f0091fSVasu Dev 	fc_exch_ptr_set(pool, (ep->xid - ep->em->min_xid) >> fc_cpu_order,
407b2f0091fSVasu Dev 			NULL);
40842e9a92fSRobert Love 	list_del(&ep->ex_list);
409b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
41042e9a92fSRobert Love 	fc_exch_release(ep);	/* drop hold for exch in mp */
41142e9a92fSRobert Love }
41242e9a92fSRobert Love 
413*3a3b42bfSRobert Love /**
414*3a3b42bfSRobert Love  * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
415*3a3b42bfSRobert Love  *				the exchange lock held
416*3a3b42bfSRobert Love  * @ep:		The exchange whose timer will start
417*3a3b42bfSRobert Love  * @timer_msec: The timeout period
418*3a3b42bfSRobert Love  *
419*3a3b42bfSRobert Love  * Used for upper level protocols to time out the exchange.
420*3a3b42bfSRobert Love  * The timer is cancelled when it fires or when the exchange completes.
42142e9a92fSRobert Love  */
42242e9a92fSRobert Love static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
42342e9a92fSRobert Love 					    unsigned int timer_msec)
42442e9a92fSRobert Love {
42542e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
42642e9a92fSRobert Love 		return;
42742e9a92fSRobert Love 
428cd305ce4SRobert Love 	FC_EXCH_DBG(ep, "Exchange timer armed\n");
4297414705eSRobert Love 
43042e9a92fSRobert Love 	if (schedule_delayed_work(&ep->timeout_work,
43142e9a92fSRobert Love 				  msecs_to_jiffies(timer_msec)))
43242e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for timer */
43342e9a92fSRobert Love }
43442e9a92fSRobert Love 
435*3a3b42bfSRobert Love /**
436*3a3b42bfSRobert Love  * fc_exch_timer_set() - Lock the exchange and set the timer
437*3a3b42bfSRobert Love  * @ep:		The exchange whose timer will start
438*3a3b42bfSRobert Love  * @timer_msec: The timeout period
43942e9a92fSRobert Love  */
44042e9a92fSRobert Love static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
44142e9a92fSRobert Love {
44242e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
44342e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, timer_msec);
44442e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
44542e9a92fSRobert Love }
44642e9a92fSRobert Love 
4471a7b75aeSRobert Love /**
448*3a3b42bfSRobert Love  * fc_seq_send() - Send a frame using existing sequence/exchange pair
449*3a3b42bfSRobert Love  * @lport: The local port that the exchange will be sent on
450*3a3b42bfSRobert Love  * @sp:	   The sequence to be sent
451*3a3b42bfSRobert Love  * @fp:	   The frame to be sent on the exchange
4521a7b75aeSRobert Love  */
453*3a3b42bfSRobert Love static int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp,
4541a7b75aeSRobert Love 		       struct fc_frame *fp)
4551a7b75aeSRobert Love {
4561a7b75aeSRobert Love 	struct fc_exch *ep;
4571a7b75aeSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
4581a7b75aeSRobert Love 	int error;
4591a7b75aeSRobert Love 	u32 f_ctl;
4601a7b75aeSRobert Love 
4611a7b75aeSRobert Love 	ep = fc_seq_exch(sp);
4621a7b75aeSRobert Love 	WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
4631a7b75aeSRobert Love 
4641a7b75aeSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
4651a7b75aeSRobert Love 	fc_exch_setup_hdr(ep, fp, f_ctl);
4661a7b75aeSRobert Love 
4671a7b75aeSRobert Love 	/*
4681a7b75aeSRobert Love 	 * update sequence count if this frame is carrying
4691a7b75aeSRobert Love 	 * multiple FC frames when sequence offload is enabled
4701a7b75aeSRobert Love 	 * by LLD.
4711a7b75aeSRobert Love 	 */
4721a7b75aeSRobert Love 	if (fr_max_payload(fp))
4731a7b75aeSRobert Love 		sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
4741a7b75aeSRobert Love 					fr_max_payload(fp));
4751a7b75aeSRobert Love 	else
4761a7b75aeSRobert Love 		sp->cnt++;
4771a7b75aeSRobert Love 
4781a7b75aeSRobert Love 	/*
4791a7b75aeSRobert Love 	 * Send the frame.
4801a7b75aeSRobert Love 	 */
481*3a3b42bfSRobert Love 	error = lport->tt.frame_send(lport, fp);
4821a7b75aeSRobert Love 
4831a7b75aeSRobert Love 	/*
4841a7b75aeSRobert Love 	 * Update the exchange and sequence flags,
4851a7b75aeSRobert Love 	 * assuming all frames for the sequence have been sent.
4861a7b75aeSRobert Love 	 * We can only be called to send once for each sequence.
4871a7b75aeSRobert Love 	 */
4881a7b75aeSRobert Love 	spin_lock_bh(&ep->ex_lock);
4891a7b75aeSRobert Love 	ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ;	/* not first seq */
4901a7b75aeSRobert Love 	if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
4911a7b75aeSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
4921a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
4931a7b75aeSRobert Love 	return error;
4941a7b75aeSRobert Love }
4951a7b75aeSRobert Love 
4961a7b75aeSRobert Love /**
497*3a3b42bfSRobert Love  * fc_seq_alloc() - Allocate a sequence for a given exchange
498*3a3b42bfSRobert Love  * @ep:	    The exchange to allocate a new sequence for
499*3a3b42bfSRobert Love  * @seq_id: The sequence ID to be used
5001a7b75aeSRobert Love  *
5011a7b75aeSRobert Love  * We don't support multiple originated sequences on the same exchange.
5021a7b75aeSRobert Love  * By implication, any previously originated sequence on this exchange
5031a7b75aeSRobert Love  * is complete, and we reallocate the same sequence.
5041a7b75aeSRobert Love  */
5051a7b75aeSRobert Love static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
5061a7b75aeSRobert Love {
5071a7b75aeSRobert Love 	struct fc_seq *sp;
5081a7b75aeSRobert Love 
5091a7b75aeSRobert Love 	sp = &ep->seq;
5101a7b75aeSRobert Love 	sp->ssb_stat = 0;
5111a7b75aeSRobert Love 	sp->cnt = 0;
5121a7b75aeSRobert Love 	sp->id = seq_id;
5131a7b75aeSRobert Love 	return sp;
5141a7b75aeSRobert Love }
5151a7b75aeSRobert Love 
516*3a3b42bfSRobert Love /**
517*3a3b42bfSRobert Love  * fc_seq_start_next_locked() - Allocate a new sequence on the same
518*3a3b42bfSRobert Love  *				exchange as the supplied sequence
519*3a3b42bfSRobert Love  * @sp: The sequence/exchange to get a new sequence for
520*3a3b42bfSRobert Love  */
5211a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
5221a7b75aeSRobert Love {
5231a7b75aeSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
5241a7b75aeSRobert Love 
5251a7b75aeSRobert Love 	sp = fc_seq_alloc(ep, ep->seq_id++);
5261a7b75aeSRobert Love 	FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
5271a7b75aeSRobert Love 		    ep->f_ctl, sp->id);
5281a7b75aeSRobert Love 	return sp;
5291a7b75aeSRobert Love }
5301a7b75aeSRobert Love 
5311a7b75aeSRobert Love /**
532*3a3b42bfSRobert Love  * fc_seq_start_next() - Lock the exchange and get a new sequence
533*3a3b42bfSRobert Love  *			 for a given sequence/exchange pair
534*3a3b42bfSRobert Love  * @sp: The sequence/exchange to get a new exchange for
5351a7b75aeSRobert Love  */
5361a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
5371a7b75aeSRobert Love {
5381a7b75aeSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
5391a7b75aeSRobert Love 
5401a7b75aeSRobert Love 	spin_lock_bh(&ep->ex_lock);
5411a7b75aeSRobert Love 	sp = fc_seq_start_next_locked(sp);
5421a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
5431a7b75aeSRobert Love 
5441a7b75aeSRobert Love 	return sp;
5451a7b75aeSRobert Love }
5461a7b75aeSRobert Love 
5471a7b75aeSRobert Love /**
548*3a3b42bfSRobert Love  * fc_seq_exch_abort() - Abort an exchange and sequence
549*3a3b42bfSRobert Love  * @req_sp:	The sequence to be aborted
550*3a3b42bfSRobert Love  * @timer_msec: The period of time to wait before aborting
551*3a3b42bfSRobert Love  *
552*3a3b42bfSRobert Love  * Generally called because of a timeout or an abort from the upper layer.
5531a7b75aeSRobert Love  */
5541a7b75aeSRobert Love static int fc_seq_exch_abort(const struct fc_seq *req_sp,
5551a7b75aeSRobert Love 			     unsigned int timer_msec)
55642e9a92fSRobert Love {
55742e9a92fSRobert Love 	struct fc_seq *sp;
55842e9a92fSRobert Love 	struct fc_exch *ep;
55942e9a92fSRobert Love 	struct fc_frame *fp;
56042e9a92fSRobert Love 	int error;
56142e9a92fSRobert Love 
56242e9a92fSRobert Love 	ep = fc_seq_exch(req_sp);
56342e9a92fSRobert Love 
56442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
56542e9a92fSRobert Love 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
56642e9a92fSRobert Love 	    ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
56742e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
56842e9a92fSRobert Love 		return -ENXIO;
56942e9a92fSRobert Love 	}
57042e9a92fSRobert Love 
57142e9a92fSRobert Love 	/*
57242e9a92fSRobert Love 	 * Send the abort on a new sequence if possible.
57342e9a92fSRobert Love 	 */
57442e9a92fSRobert Love 	sp = fc_seq_start_next_locked(&ep->seq);
57542e9a92fSRobert Love 	if (!sp) {
57642e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
57742e9a92fSRobert Love 		return -ENOMEM;
57842e9a92fSRobert Love 	}
57942e9a92fSRobert Love 
58042e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
58142e9a92fSRobert Love 	if (timer_msec)
58242e9a92fSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
58342e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
58442e9a92fSRobert Love 
58542e9a92fSRobert Love 	/*
58642e9a92fSRobert Love 	 * If not logged into the fabric, don't send ABTS but leave
58742e9a92fSRobert Love 	 * sequence active until next timeout.
58842e9a92fSRobert Love 	 */
58942e9a92fSRobert Love 	if (!ep->sid)
59042e9a92fSRobert Love 		return 0;
59142e9a92fSRobert Love 
59242e9a92fSRobert Love 	/*
59342e9a92fSRobert Love 	 * Send an abort for the sequence that timed out.
59442e9a92fSRobert Love 	 */
59542e9a92fSRobert Love 	fp = fc_frame_alloc(ep->lp, 0);
59642e9a92fSRobert Love 	if (fp) {
59742e9a92fSRobert Love 		fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
59842e9a92fSRobert Love 			       FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
59942e9a92fSRobert Love 		error = fc_seq_send(ep->lp, sp, fp);
60042e9a92fSRobert Love 	} else
60142e9a92fSRobert Love 		error = -ENOBUFS;
60242e9a92fSRobert Love 	return error;
60342e9a92fSRobert Love }
60442e9a92fSRobert Love 
605*3a3b42bfSRobert Love /**
606*3a3b42bfSRobert Love  * fc_exch_timeout() - Handle exchange timer expiration
607*3a3b42bfSRobert Love  * @work: The work_struct identifying the exchange that timed out
60842e9a92fSRobert Love  */
60942e9a92fSRobert Love static void fc_exch_timeout(struct work_struct *work)
61042e9a92fSRobert Love {
61142e9a92fSRobert Love 	struct fc_exch *ep = container_of(work, struct fc_exch,
61242e9a92fSRobert Love 					  timeout_work.work);
61342e9a92fSRobert Love 	struct fc_seq *sp = &ep->seq;
61442e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
61542e9a92fSRobert Love 	void *arg;
61642e9a92fSRobert Love 	u32 e_stat;
61742e9a92fSRobert Love 	int rc = 1;
61842e9a92fSRobert Love 
619cd305ce4SRobert Love 	FC_EXCH_DBG(ep, "Exchange timed out\n");
620cd305ce4SRobert Love 
62142e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
62242e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
62342e9a92fSRobert Love 		goto unlock;
62442e9a92fSRobert Love 
62542e9a92fSRobert Love 	e_stat = ep->esb_stat;
62642e9a92fSRobert Love 	if (e_stat & ESB_ST_COMPLETE) {
62742e9a92fSRobert Love 		ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
628a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
62942e9a92fSRobert Love 		if (e_stat & ESB_ST_REC_QUAL)
63042e9a92fSRobert Love 			fc_exch_rrq(ep);
63142e9a92fSRobert Love 		goto done;
63242e9a92fSRobert Love 	} else {
63342e9a92fSRobert Love 		resp = ep->resp;
63442e9a92fSRobert Love 		arg = ep->arg;
63542e9a92fSRobert Love 		ep->resp = NULL;
63642e9a92fSRobert Love 		if (e_stat & ESB_ST_ABNORMAL)
63742e9a92fSRobert Love 			rc = fc_exch_done_locked(ep);
63842e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
63942e9a92fSRobert Love 		if (!rc)
640b2f0091fSVasu Dev 			fc_exch_delete(ep);
64142e9a92fSRobert Love 		if (resp)
64242e9a92fSRobert Love 			resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
64342e9a92fSRobert Love 		fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
64442e9a92fSRobert Love 		goto done;
64542e9a92fSRobert Love 	}
64642e9a92fSRobert Love unlock:
64742e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
64842e9a92fSRobert Love done:
64942e9a92fSRobert Love 	/*
65042e9a92fSRobert Love 	 * This release matches the hold taken when the timer was set.
65142e9a92fSRobert Love 	 */
65242e9a92fSRobert Love 	fc_exch_release(ep);
65342e9a92fSRobert Love }
65442e9a92fSRobert Love 
65552ff878cSVasu Dev /**
656*3a3b42bfSRobert Love  * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
657*3a3b42bfSRobert Love  * @lport: The local port that the exchange is for
658*3a3b42bfSRobert Love  * @mp:	   The exchange manager that will allocate the exchange
65942e9a92fSRobert Love  *
660d7179680SVasu Dev  * Returns pointer to allocated fc_exch with exch lock held.
66142e9a92fSRobert Love  */
66252ff878cSVasu Dev static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
663d7179680SVasu Dev 					struct fc_exch_mgr *mp)
66442e9a92fSRobert Love {
66542e9a92fSRobert Love 	struct fc_exch *ep;
666b2f0091fSVasu Dev 	unsigned int cpu;
667b2f0091fSVasu Dev 	u16 index;
668b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
66942e9a92fSRobert Love 
67042e9a92fSRobert Love 	/* allocate memory for exchange */
67142e9a92fSRobert Love 	ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
67242e9a92fSRobert Love 	if (!ep) {
67342e9a92fSRobert Love 		atomic_inc(&mp->stats.no_free_exch);
67442e9a92fSRobert Love 		goto out;
67542e9a92fSRobert Love 	}
67642e9a92fSRobert Love 	memset(ep, 0, sizeof(*ep));
67742e9a92fSRobert Love 
678b2f0091fSVasu Dev 	cpu = smp_processor_id();
679b2f0091fSVasu Dev 	pool = per_cpu_ptr(mp->pool, cpu);
680b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
681b2f0091fSVasu Dev 	index = pool->next_index;
682b2f0091fSVasu Dev 	/* allocate new exch from pool */
683b2f0091fSVasu Dev 	while (fc_exch_ptr_get(pool, index)) {
684b2f0091fSVasu Dev 		index = index == mp->pool_max_index ? 0 : index + 1;
685b2f0091fSVasu Dev 		if (index == pool->next_index)
68642e9a92fSRobert Love 			goto err;
68742e9a92fSRobert Love 	}
688b2f0091fSVasu Dev 	pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
68942e9a92fSRobert Love 
69042e9a92fSRobert Love 	fc_exch_hold(ep);	/* hold for exch in mp */
69142e9a92fSRobert Love 	spin_lock_init(&ep->ex_lock);
69242e9a92fSRobert Love 	/*
69342e9a92fSRobert Love 	 * Hold exch lock for caller to prevent fc_exch_reset()
69442e9a92fSRobert Love 	 * from releasing exch	while fc_exch_alloc() caller is
69542e9a92fSRobert Love 	 * still working on exch.
69642e9a92fSRobert Love 	 */
69742e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
69842e9a92fSRobert Love 
699b2f0091fSVasu Dev 	fc_exch_ptr_set(pool, index, ep);
700b2f0091fSVasu Dev 	list_add_tail(&ep->ex_list, &pool->ex_list);
70142e9a92fSRobert Love 	fc_seq_alloc(ep, ep->seq_id++);
702b2f0091fSVasu Dev 	pool->total_exches++;
703b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
70442e9a92fSRobert Love 
70542e9a92fSRobert Love 	/*
70642e9a92fSRobert Love 	 *  update exchange
70742e9a92fSRobert Love 	 */
708b2f0091fSVasu Dev 	ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
70942e9a92fSRobert Love 	ep->em = mp;
710b2f0091fSVasu Dev 	ep->pool = pool;
71152ff878cSVasu Dev 	ep->lp = lport;
71242e9a92fSRobert Love 	ep->f_ctl = FC_FC_FIRST_SEQ;	/* next seq is first seq */
71342e9a92fSRobert Love 	ep->rxid = FC_XID_UNKNOWN;
71442e9a92fSRobert Love 	ep->class = mp->class;
71542e9a92fSRobert Love 	INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
71642e9a92fSRobert Love out:
71742e9a92fSRobert Love 	return ep;
71842e9a92fSRobert Love err:
719b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
72042e9a92fSRobert Love 	atomic_inc(&mp->stats.no_free_exch_xid);
72142e9a92fSRobert Love 	mempool_free(ep, mp->ep_pool);
72242e9a92fSRobert Love 	return NULL;
72342e9a92fSRobert Love }
72452ff878cSVasu Dev 
72552ff878cSVasu Dev /**
726*3a3b42bfSRobert Love  * fc_exch_alloc() - Allocate an exchange from an EM on a
727*3a3b42bfSRobert Love  *		     local port's list of EMs.
728*3a3b42bfSRobert Love  * @lport: The local port that will own the exchange
729*3a3b42bfSRobert Love  * @fp:	   The FC frame that the exchange will be for
73052ff878cSVasu Dev  *
731*3a3b42bfSRobert Love  * This function walks the list of exchange manager(EM)
732*3a3b42bfSRobert Love  * anchors to select an EM for a new exchange allocation. The
733*3a3b42bfSRobert Love  * EM is selected when a NULL match function pointer is encountered
734*3a3b42bfSRobert Love  * or when a call to a match function returns true.
73552ff878cSVasu Dev  */
7361a7b75aeSRobert Love static struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
7371a7b75aeSRobert Love 				     struct fc_frame *fp)
73852ff878cSVasu Dev {
73952ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
74052ff878cSVasu Dev 	struct fc_exch *ep;
74152ff878cSVasu Dev 
74252ff878cSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
74352ff878cSVasu Dev 		if (!ema->match || ema->match(fp)) {
744d7179680SVasu Dev 			ep = fc_exch_em_alloc(lport, ema->mp);
74552ff878cSVasu Dev 			if (ep)
74652ff878cSVasu Dev 				return ep;
74752ff878cSVasu Dev 		}
74852ff878cSVasu Dev 	}
74952ff878cSVasu Dev 	return NULL;
75052ff878cSVasu Dev }
75142e9a92fSRobert Love 
752*3a3b42bfSRobert Love /**
753*3a3b42bfSRobert Love  * fc_exch_find() - Lookup and hold an exchange
754*3a3b42bfSRobert Love  * @mp:	 The exchange manager to lookup the exchange from
755*3a3b42bfSRobert Love  * @xid: The XID of the exchange to look up
75642e9a92fSRobert Love  */
75742e9a92fSRobert Love static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
75842e9a92fSRobert Love {
759b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
76042e9a92fSRobert Love 	struct fc_exch *ep = NULL;
76142e9a92fSRobert Love 
76242e9a92fSRobert Love 	if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
763b2f0091fSVasu Dev 		pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
764b2f0091fSVasu Dev 		spin_lock_bh(&pool->lock);
765b2f0091fSVasu Dev 		ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
76642e9a92fSRobert Love 		if (ep) {
76742e9a92fSRobert Love 			fc_exch_hold(ep);
76842e9a92fSRobert Love 			WARN_ON(ep->xid != xid);
76942e9a92fSRobert Love 		}
770b2f0091fSVasu Dev 		spin_unlock_bh(&pool->lock);
77142e9a92fSRobert Love 	}
77242e9a92fSRobert Love 	return ep;
77342e9a92fSRobert Love }
77442e9a92fSRobert Love 
7751a7b75aeSRobert Love 
7761a7b75aeSRobert Love /**
7771a7b75aeSRobert Love  * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
7781a7b75aeSRobert Love  *		    the memory allocated for the related objects may be freed.
779*3a3b42bfSRobert Love  * @sp: The sequence that has completed
7801a7b75aeSRobert Love  */
7811a7b75aeSRobert Love static void fc_exch_done(struct fc_seq *sp)
78242e9a92fSRobert Love {
78342e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
78442e9a92fSRobert Love 	int rc;
78542e9a92fSRobert Love 
78642e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
78742e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
78842e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
78942e9a92fSRobert Love 	if (!rc)
790b2f0091fSVasu Dev 		fc_exch_delete(ep);
79142e9a92fSRobert Love }
79242e9a92fSRobert Love 
793*3a3b42bfSRobert Love /**
794*3a3b42bfSRobert Love  * fc_exch_resp() - Allocate a new exchange for a response frame
795*3a3b42bfSRobert Love  * @lport: The local port that the exchange was for
796*3a3b42bfSRobert Love  * @mp:	   The exchange manager to allocate the exchange from
797*3a3b42bfSRobert Love  * @fp:	   The response frame
798*3a3b42bfSRobert Love  *
79942e9a92fSRobert Love  * Sets the responder ID in the frame header.
80042e9a92fSRobert Love  */
80152ff878cSVasu Dev static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
80252ff878cSVasu Dev 				    struct fc_exch_mgr *mp,
80352ff878cSVasu Dev 				    struct fc_frame *fp)
80442e9a92fSRobert Love {
80542e9a92fSRobert Love 	struct fc_exch *ep;
80642e9a92fSRobert Love 	struct fc_frame_header *fh;
80742e9a92fSRobert Love 
80852ff878cSVasu Dev 	ep = fc_exch_alloc(lport, fp);
80942e9a92fSRobert Love 	if (ep) {
81042e9a92fSRobert Love 		ep->class = fc_frame_class(fp);
81142e9a92fSRobert Love 
81242e9a92fSRobert Love 		/*
81342e9a92fSRobert Love 		 * Set EX_CTX indicating we're responding on this exchange.
81442e9a92fSRobert Love 		 */
81542e9a92fSRobert Love 		ep->f_ctl |= FC_FC_EX_CTX;	/* we're responding */
81642e9a92fSRobert Love 		ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not new */
81742e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
81842e9a92fSRobert Love 		ep->sid = ntoh24(fh->fh_d_id);
81942e9a92fSRobert Love 		ep->did = ntoh24(fh->fh_s_id);
82042e9a92fSRobert Love 		ep->oid = ep->did;
82142e9a92fSRobert Love 
82242e9a92fSRobert Love 		/*
82342e9a92fSRobert Love 		 * Allocated exchange has placed the XID in the
82442e9a92fSRobert Love 		 * originator field. Move it to the responder field,
82542e9a92fSRobert Love 		 * and set the originator XID from the frame.
82642e9a92fSRobert Love 		 */
82742e9a92fSRobert Love 		ep->rxid = ep->xid;
82842e9a92fSRobert Love 		ep->oxid = ntohs(fh->fh_ox_id);
82942e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
83042e9a92fSRobert Love 		if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
83142e9a92fSRobert Love 			ep->esb_stat &= ~ESB_ST_SEQ_INIT;
83242e9a92fSRobert Love 
83342e9a92fSRobert Love 		fc_exch_hold(ep);	/* hold for caller */
83452ff878cSVasu Dev 		spin_unlock_bh(&ep->ex_lock);	/* lock from fc_exch_alloc */
83542e9a92fSRobert Love 	}
83642e9a92fSRobert Love 	return ep;
83742e9a92fSRobert Love }
83842e9a92fSRobert Love 
839*3a3b42bfSRobert Love /**
840*3a3b42bfSRobert Love  * fc_seq_lookup_recip() - Find a sequence where the other end
841*3a3b42bfSRobert Love  *			   originated the sequence
842*3a3b42bfSRobert Love  * @lport: The local port that the frame was sent to
843*3a3b42bfSRobert Love  * @mp:	   The Exchange Manager to lookup the exchange from
844*3a3b42bfSRobert Love  * @fp:	   The frame associated with the sequence we're looking for
845*3a3b42bfSRobert Love  *
84642e9a92fSRobert Love  * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
84742e9a92fSRobert Love  * on the ep that should be released by the caller.
84842e9a92fSRobert Love  */
84952ff878cSVasu Dev static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
85052ff878cSVasu Dev 						 struct fc_exch_mgr *mp,
851b2ab99c9SRobert Love 						 struct fc_frame *fp)
85242e9a92fSRobert Love {
85342e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
85442e9a92fSRobert Love 	struct fc_exch *ep = NULL;
85542e9a92fSRobert Love 	struct fc_seq *sp = NULL;
85642e9a92fSRobert Love 	enum fc_pf_rjt_reason reject = FC_RJT_NONE;
85742e9a92fSRobert Love 	u32 f_ctl;
85842e9a92fSRobert Love 	u16 xid;
85942e9a92fSRobert Love 
86042e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
86142e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
86242e9a92fSRobert Love 
86342e9a92fSRobert Love 	/*
86442e9a92fSRobert Love 	 * Lookup or create the exchange if we will be creating the sequence.
86542e9a92fSRobert Love 	 */
86642e9a92fSRobert Love 	if (f_ctl & FC_FC_EX_CTX) {
86742e9a92fSRobert Love 		xid = ntohs(fh->fh_ox_id);	/* we originated exch */
86842e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
86942e9a92fSRobert Love 		if (!ep) {
87042e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
87142e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
87242e9a92fSRobert Love 			goto out;
87342e9a92fSRobert Love 		}
87442e9a92fSRobert Love 		if (ep->rxid == FC_XID_UNKNOWN)
87542e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
87642e9a92fSRobert Love 		else if (ep->rxid != ntohs(fh->fh_rx_id)) {
87742e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
87842e9a92fSRobert Love 			goto rel;
87942e9a92fSRobert Love 		}
88042e9a92fSRobert Love 	} else {
88142e9a92fSRobert Love 		xid = ntohs(fh->fh_rx_id);	/* we are the responder */
88242e9a92fSRobert Love 
88342e9a92fSRobert Love 		/*
88442e9a92fSRobert Love 		 * Special case for MDS issuing an ELS TEST with a
88542e9a92fSRobert Love 		 * bad rxid of 0.
88642e9a92fSRobert Love 		 * XXX take this out once we do the proper reject.
88742e9a92fSRobert Love 		 */
88842e9a92fSRobert Love 		if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
88942e9a92fSRobert Love 		    fc_frame_payload_op(fp) == ELS_TEST) {
89042e9a92fSRobert Love 			fh->fh_rx_id = htons(FC_XID_UNKNOWN);
89142e9a92fSRobert Love 			xid = FC_XID_UNKNOWN;
89242e9a92fSRobert Love 		}
89342e9a92fSRobert Love 
89442e9a92fSRobert Love 		/*
89542e9a92fSRobert Love 		 * new sequence - find the exchange
89642e9a92fSRobert Love 		 */
89742e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
89842e9a92fSRobert Love 		if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
89942e9a92fSRobert Love 			if (ep) {
90042e9a92fSRobert Love 				atomic_inc(&mp->stats.xid_busy);
90142e9a92fSRobert Love 				reject = FC_RJT_RX_ID;
90242e9a92fSRobert Love 				goto rel;
90342e9a92fSRobert Love 			}
90452ff878cSVasu Dev 			ep = fc_exch_resp(lport, mp, fp);
90542e9a92fSRobert Love 			if (!ep) {
90642e9a92fSRobert Love 				reject = FC_RJT_EXCH_EST;	/* XXX */
90742e9a92fSRobert Love 				goto out;
90842e9a92fSRobert Love 			}
90942e9a92fSRobert Love 			xid = ep->xid;	/* get our XID */
91042e9a92fSRobert Love 		} else if (!ep) {
91142e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
91242e9a92fSRobert Love 			reject = FC_RJT_RX_ID;	/* XID not found */
91342e9a92fSRobert Love 			goto out;
91442e9a92fSRobert Love 		}
91542e9a92fSRobert Love 	}
91642e9a92fSRobert Love 
91742e9a92fSRobert Love 	/*
91842e9a92fSRobert Love 	 * At this point, we have the exchange held.
91942e9a92fSRobert Love 	 * Find or create the sequence.
92042e9a92fSRobert Love 	 */
92142e9a92fSRobert Love 	if (fc_sof_is_init(fr_sof(fp))) {
92242e9a92fSRobert Love 		sp = fc_seq_start_next(&ep->seq);
92342e9a92fSRobert Love 		if (!sp) {
92442e9a92fSRobert Love 			reject = FC_RJT_SEQ_XS;	/* exchange shortage */
92542e9a92fSRobert Love 			goto rel;
92642e9a92fSRobert Love 		}
92742e9a92fSRobert Love 		sp->id = fh->fh_seq_id;
92842e9a92fSRobert Love 		sp->ssb_stat |= SSB_ST_RESP;
92942e9a92fSRobert Love 	} else {
93042e9a92fSRobert Love 		sp = &ep->seq;
93142e9a92fSRobert Love 		if (sp->id != fh->fh_seq_id) {
93242e9a92fSRobert Love 			atomic_inc(&mp->stats.seq_not_found);
93342e9a92fSRobert Love 			reject = FC_RJT_SEQ_ID;	/* sequence/exch should exist */
93442e9a92fSRobert Love 			goto rel;
93542e9a92fSRobert Love 		}
93642e9a92fSRobert Love 	}
93742e9a92fSRobert Love 	WARN_ON(ep != fc_seq_exch(sp));
93842e9a92fSRobert Love 
93942e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
94042e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
94142e9a92fSRobert Love 
94242e9a92fSRobert Love 	fr_seq(fp) = sp;
94342e9a92fSRobert Love out:
94442e9a92fSRobert Love 	return reject;
94542e9a92fSRobert Love rel:
94642e9a92fSRobert Love 	fc_exch_done(&ep->seq);
94742e9a92fSRobert Love 	fc_exch_release(ep);	/* hold from fc_exch_find/fc_exch_resp */
94842e9a92fSRobert Love 	return reject;
94942e9a92fSRobert Love }
95042e9a92fSRobert Love 
951*3a3b42bfSRobert Love /**
952*3a3b42bfSRobert Love  * fc_seq_lookup_orig() - Find a sequence where this end
953*3a3b42bfSRobert Love  *			  originated the sequence
954*3a3b42bfSRobert Love  * @mp:	   The Exchange Manager to lookup the exchange from
955*3a3b42bfSRobert Love  * @fp:	   The frame associated with the sequence we're looking for
956*3a3b42bfSRobert Love  *
95742e9a92fSRobert Love  * Does not hold the sequence for the caller.
95842e9a92fSRobert Love  */
95942e9a92fSRobert Love static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
96042e9a92fSRobert Love 					 struct fc_frame *fp)
96142e9a92fSRobert Love {
96242e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
96342e9a92fSRobert Love 	struct fc_exch *ep;
96442e9a92fSRobert Love 	struct fc_seq *sp = NULL;
96542e9a92fSRobert Love 	u32 f_ctl;
96642e9a92fSRobert Love 	u16 xid;
96742e9a92fSRobert Love 
96842e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
96942e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
97042e9a92fSRobert Love 	xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
97142e9a92fSRobert Love 	ep = fc_exch_find(mp, xid);
97242e9a92fSRobert Love 	if (!ep)
97342e9a92fSRobert Love 		return NULL;
97442e9a92fSRobert Love 	if (ep->seq.id == fh->fh_seq_id) {
97542e9a92fSRobert Love 		/*
97642e9a92fSRobert Love 		 * Save the RX_ID if we didn't previously know it.
97742e9a92fSRobert Love 		 */
97842e9a92fSRobert Love 		sp = &ep->seq;
97942e9a92fSRobert Love 		if ((f_ctl & FC_FC_EX_CTX) != 0 &&
98042e9a92fSRobert Love 		    ep->rxid == FC_XID_UNKNOWN) {
98142e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
98242e9a92fSRobert Love 		}
98342e9a92fSRobert Love 	}
98442e9a92fSRobert Love 	fc_exch_release(ep);
98542e9a92fSRobert Love 	return sp;
98642e9a92fSRobert Love }
98742e9a92fSRobert Love 
988*3a3b42bfSRobert Love /**
989*3a3b42bfSRobert Love  * fc_exch_set_addr() - Set the source and destination IDs for an exchange
990*3a3b42bfSRobert Love  * @ep:	     The exchange to set the addresses for
991*3a3b42bfSRobert Love  * @orig_id: The originator's ID
992*3a3b42bfSRobert Love  * @resp_id: The responder's ID
993*3a3b42bfSRobert Love  *
99442e9a92fSRobert Love  * Note this must be done before the first sequence of the exchange is sent.
99542e9a92fSRobert Love  */
99642e9a92fSRobert Love static void fc_exch_set_addr(struct fc_exch *ep,
99742e9a92fSRobert Love 			     u32 orig_id, u32 resp_id)
99842e9a92fSRobert Love {
99942e9a92fSRobert Love 	ep->oid = orig_id;
100042e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP) {
100142e9a92fSRobert Love 		ep->sid = resp_id;
100242e9a92fSRobert Love 		ep->did = orig_id;
100342e9a92fSRobert Love 	} else {
100442e9a92fSRobert Love 		ep->sid = orig_id;
100542e9a92fSRobert Love 		ep->did = resp_id;
100642e9a92fSRobert Love 	}
100742e9a92fSRobert Love }
100842e9a92fSRobert Love 
10091a7b75aeSRobert Love /**
1010*3a3b42bfSRobert Love  * fc_seq_els_rsp_send() - Send an ELS response using infomation from
1011*3a3b42bfSRobert Love  *			   the existing sequence/exchange.
1012*3a3b42bfSRobert Love  * @sp:	      The sequence/exchange to get information from
1013*3a3b42bfSRobert Love  * @els_cmd:  The ELS command to be sent
1014*3a3b42bfSRobert Love  * @els_data: The ELS data to be sent
101542e9a92fSRobert Love  */
10161a7b75aeSRobert Love static void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
101742e9a92fSRobert Love 				struct fc_seq_els_data *els_data)
101842e9a92fSRobert Love {
101942e9a92fSRobert Love 	switch (els_cmd) {
102042e9a92fSRobert Love 	case ELS_LS_RJT:
102142e9a92fSRobert Love 		fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
102242e9a92fSRobert Love 		break;
102342e9a92fSRobert Love 	case ELS_LS_ACC:
102442e9a92fSRobert Love 		fc_seq_ls_acc(sp);
102542e9a92fSRobert Love 		break;
102642e9a92fSRobert Love 	case ELS_RRQ:
102742e9a92fSRobert Love 		fc_exch_els_rrq(sp, els_data->fp);
102842e9a92fSRobert Love 		break;
102942e9a92fSRobert Love 	case ELS_REC:
103042e9a92fSRobert Love 		fc_exch_els_rec(sp, els_data->fp);
103142e9a92fSRobert Love 		break;
103242e9a92fSRobert Love 	default:
10337414705eSRobert Love 		FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
103442e9a92fSRobert Love 	}
103542e9a92fSRobert Love }
103642e9a92fSRobert Love 
1037*3a3b42bfSRobert Love /**
1038*3a3b42bfSRobert Love  * fc_seq_send_last() - Send a sequence that is the last in the exchange
1039*3a3b42bfSRobert Love  * @sp:	     The sequence that is to be sent
1040*3a3b42bfSRobert Love  * @fp:	     The frame that will be sent on the sequence
1041*3a3b42bfSRobert Love  * @rctl:    The R_CTL information to be sent
1042*3a3b42bfSRobert Love  * @fh_type: The frame header type
104342e9a92fSRobert Love  */
104442e9a92fSRobert Love static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
104542e9a92fSRobert Love 			     enum fc_rctl rctl, enum fc_fh_type fh_type)
104642e9a92fSRobert Love {
104742e9a92fSRobert Love 	u32 f_ctl;
104842e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
104942e9a92fSRobert Love 
105042e9a92fSRobert Love 	f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
105142e9a92fSRobert Love 	f_ctl |= ep->f_ctl;
105242e9a92fSRobert Love 	fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
105342e9a92fSRobert Love 	fc_seq_send(ep->lp, sp, fp);
105442e9a92fSRobert Love }
105542e9a92fSRobert Love 
1056*3a3b42bfSRobert Love /**
1057*3a3b42bfSRobert Love  * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
1058*3a3b42bfSRobert Love  * @sp:	   The sequence to send the ACK on
1059*3a3b42bfSRobert Love  * @rx_fp: The received frame that is being acknoledged
1060*3a3b42bfSRobert Love  *
106142e9a92fSRobert Love  * Send ACK_1 (or equiv.) indicating we received something.
106242e9a92fSRobert Love  */
106342e9a92fSRobert Love static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
106442e9a92fSRobert Love {
106542e9a92fSRobert Love 	struct fc_frame *fp;
106642e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
106742e9a92fSRobert Love 	struct fc_frame_header *fh;
106842e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
1069*3a3b42bfSRobert Love 	struct fc_lport *lport = ep->lp;
107042e9a92fSRobert Love 	unsigned int f_ctl;
107142e9a92fSRobert Love 
107242e9a92fSRobert Love 	/*
107342e9a92fSRobert Love 	 * Don't send ACKs for class 3.
107442e9a92fSRobert Love 	 */
107542e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(rx_fp))) {
1076*3a3b42bfSRobert Love 		fp = fc_frame_alloc(lport, 0);
107742e9a92fSRobert Love 		if (!fp)
107842e9a92fSRobert Love 			return;
107942e9a92fSRobert Love 
108042e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
108142e9a92fSRobert Love 		fh->fh_r_ctl = FC_RCTL_ACK_1;
108242e9a92fSRobert Love 		fh->fh_type = FC_TYPE_BLS;
108342e9a92fSRobert Love 
108442e9a92fSRobert Love 		/*
108542e9a92fSRobert Love 		 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
108642e9a92fSRobert Love 		 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
108742e9a92fSRobert Love 		 * Bits 9-8 are meaningful (retransmitted or unidirectional).
108842e9a92fSRobert Love 		 * Last ACK uses bits 7-6 (continue sequence),
108942e9a92fSRobert Love 		 * bits 5-4 are meaningful (what kind of ACK to use).
109042e9a92fSRobert Love 		 */
109142e9a92fSRobert Love 		rx_fh = fc_frame_header_get(rx_fp);
109242e9a92fSRobert Love 		f_ctl = ntoh24(rx_fh->fh_f_ctl);
109342e9a92fSRobert Love 		f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
109442e9a92fSRobert Love 			FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
109542e9a92fSRobert Love 			FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
109642e9a92fSRobert Love 			FC_FC_RETX_SEQ | FC_FC_UNI_TX;
109742e9a92fSRobert Love 		f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
109842e9a92fSRobert Love 		hton24(fh->fh_f_ctl, f_ctl);
109942e9a92fSRobert Love 
110042e9a92fSRobert Love 		fc_exch_setup_hdr(ep, fp, f_ctl);
110142e9a92fSRobert Love 		fh->fh_seq_id = rx_fh->fh_seq_id;
110242e9a92fSRobert Love 		fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
110342e9a92fSRobert Love 		fh->fh_parm_offset = htonl(1);	/* ack single frame */
110442e9a92fSRobert Love 
110542e9a92fSRobert Love 		fr_sof(fp) = fr_sof(rx_fp);
110642e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
110742e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_T;
110842e9a92fSRobert Love 		else
110942e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
111042e9a92fSRobert Love 
1111*3a3b42bfSRobert Love 		lport->tt.frame_send(lport, fp);
111242e9a92fSRobert Love 	}
111342e9a92fSRobert Love }
111442e9a92fSRobert Love 
1115*3a3b42bfSRobert Love /**
1116*3a3b42bfSRobert Love  * fc_exch_send_ba_rjt() - Send BLS Reject
1117*3a3b42bfSRobert Love  * @rx_fp:  The frame being rejected
1118*3a3b42bfSRobert Love  * @reason: The reason the frame is being rejected
1119*3a3b42bfSRobert Love  * @explan: The explaination for the rejection
1120*3a3b42bfSRobert Love  *
112142e9a92fSRobert Love  * This is for rejecting BA_ABTS only.
112242e9a92fSRobert Love  */
1123b2ab99c9SRobert Love static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1124b2ab99c9SRobert Love 				enum fc_ba_rjt_reason reason,
112542e9a92fSRobert Love 				enum fc_ba_rjt_explan explan)
112642e9a92fSRobert Love {
112742e9a92fSRobert Love 	struct fc_frame *fp;
112842e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
112942e9a92fSRobert Love 	struct fc_frame_header *fh;
113042e9a92fSRobert Love 	struct fc_ba_rjt *rp;
1131*3a3b42bfSRobert Love 	struct fc_lport *lport;
113242e9a92fSRobert Love 	unsigned int f_ctl;
113342e9a92fSRobert Love 
1134*3a3b42bfSRobert Love 	lport = fr_dev(rx_fp);
1135*3a3b42bfSRobert Love 	fp = fc_frame_alloc(lport, sizeof(*rp));
113642e9a92fSRobert Love 	if (!fp)
113742e9a92fSRobert Love 		return;
113842e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
113942e9a92fSRobert Love 	rx_fh = fc_frame_header_get(rx_fp);
114042e9a92fSRobert Love 
114142e9a92fSRobert Love 	memset(fh, 0, sizeof(*fh) + sizeof(*rp));
114242e9a92fSRobert Love 
114342e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
114442e9a92fSRobert Love 	rp->br_reason = reason;
114542e9a92fSRobert Love 	rp->br_explan = explan;
114642e9a92fSRobert Love 
114742e9a92fSRobert Love 	/*
114842e9a92fSRobert Love 	 * seq_id, cs_ctl, df_ctl and param/offset are zero.
114942e9a92fSRobert Love 	 */
115042e9a92fSRobert Love 	memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
115142e9a92fSRobert Love 	memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
11521d490ce3SJoe Eykholt 	fh->fh_ox_id = rx_fh->fh_ox_id;
11531d490ce3SJoe Eykholt 	fh->fh_rx_id = rx_fh->fh_rx_id;
115442e9a92fSRobert Love 	fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
115542e9a92fSRobert Love 	fh->fh_r_ctl = FC_RCTL_BA_RJT;
115642e9a92fSRobert Love 	fh->fh_type = FC_TYPE_BLS;
115742e9a92fSRobert Love 
115842e9a92fSRobert Love 	/*
115942e9a92fSRobert Love 	 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
116042e9a92fSRobert Love 	 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
116142e9a92fSRobert Love 	 * Bits 9-8 are meaningful (retransmitted or unidirectional).
116242e9a92fSRobert Love 	 * Last ACK uses bits 7-6 (continue sequence),
116342e9a92fSRobert Love 	 * bits 5-4 are meaningful (what kind of ACK to use).
116442e9a92fSRobert Love 	 * Always set LAST_SEQ, END_SEQ.
116542e9a92fSRobert Love 	 */
116642e9a92fSRobert Love 	f_ctl = ntoh24(rx_fh->fh_f_ctl);
116742e9a92fSRobert Love 	f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
116842e9a92fSRobert Love 		FC_FC_END_CONN | FC_FC_SEQ_INIT |
116942e9a92fSRobert Love 		FC_FC_RETX_SEQ | FC_FC_UNI_TX;
117042e9a92fSRobert Love 	f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
117142e9a92fSRobert Love 	f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
117242e9a92fSRobert Love 	f_ctl &= ~FC_FC_FIRST_SEQ;
117342e9a92fSRobert Love 	hton24(fh->fh_f_ctl, f_ctl);
117442e9a92fSRobert Love 
117542e9a92fSRobert Love 	fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
117642e9a92fSRobert Love 	fr_eof(fp) = FC_EOF_T;
117742e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(fp)))
117842e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
117942e9a92fSRobert Love 
1180*3a3b42bfSRobert Love 	lport->tt.frame_send(lport, fp);
118142e9a92fSRobert Love }
118242e9a92fSRobert Love 
1183*3a3b42bfSRobert Love /**
1184*3a3b42bfSRobert Love  * fc_exch_recv_abts() - Handle an incoming ABTS
1185*3a3b42bfSRobert Love  * @ep:	   The exchange the abort was on
1186*3a3b42bfSRobert Love  * @rx_fp: The ABTS frame
1187*3a3b42bfSRobert Love  *
1188*3a3b42bfSRobert Love  * This would be for target mode usually, but could be due to lost
1189*3a3b42bfSRobert Love  * FCP transfer ready, confirm or RRQ. We always handle this as an
1190*3a3b42bfSRobert Love  * exchange abort, ignoring the parameter.
119142e9a92fSRobert Love  */
119242e9a92fSRobert Love static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
119342e9a92fSRobert Love {
119442e9a92fSRobert Love 	struct fc_frame *fp;
119542e9a92fSRobert Love 	struct fc_ba_acc *ap;
119642e9a92fSRobert Love 	struct fc_frame_header *fh;
119742e9a92fSRobert Love 	struct fc_seq *sp;
119842e9a92fSRobert Love 
119942e9a92fSRobert Love 	if (!ep)
120042e9a92fSRobert Love 		goto reject;
120142e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
120242e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
120342e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
120442e9a92fSRobert Love 		goto reject;
120542e9a92fSRobert Love 	}
120642e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL))
120742e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for REC_QUAL */
120842e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
120942e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
121042e9a92fSRobert Love 
121142e9a92fSRobert Love 	fp = fc_frame_alloc(ep->lp, sizeof(*ap));
121242e9a92fSRobert Love 	if (!fp) {
121342e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
121442e9a92fSRobert Love 		goto free;
121542e9a92fSRobert Love 	}
121642e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
121742e9a92fSRobert Love 	ap = fc_frame_payload_get(fp, sizeof(*ap));
121842e9a92fSRobert Love 	memset(ap, 0, sizeof(*ap));
121942e9a92fSRobert Love 	sp = &ep->seq;
122042e9a92fSRobert Love 	ap->ba_high_seq_cnt = htons(0xffff);
122142e9a92fSRobert Love 	if (sp->ssb_stat & SSB_ST_RESP) {
122242e9a92fSRobert Love 		ap->ba_seq_id = sp->id;
122342e9a92fSRobert Love 		ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
122442e9a92fSRobert Love 		ap->ba_high_seq_cnt = fh->fh_seq_cnt;
122542e9a92fSRobert Love 		ap->ba_low_seq_cnt = htons(sp->cnt);
122642e9a92fSRobert Love 	}
1227a7e84f2bSVasu Dev 	sp = fc_seq_start_next_locked(sp);
122842e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
122942e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
123042e9a92fSRobert Love 	fc_frame_free(rx_fp);
123142e9a92fSRobert Love 	return;
123242e9a92fSRobert Love 
123342e9a92fSRobert Love reject:
123442e9a92fSRobert Love 	fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
123542e9a92fSRobert Love free:
123642e9a92fSRobert Love 	fc_frame_free(rx_fp);
123742e9a92fSRobert Love }
123842e9a92fSRobert Love 
1239*3a3b42bfSRobert Love /**
1240*3a3b42bfSRobert Love  * fc_exch_recv_req() - Handler for an incoming request where is other
1241*3a3b42bfSRobert Love  *			end is originating the sequence
1242*3a3b42bfSRobert Love  * @lport: The local port that received the request
1243*3a3b42bfSRobert Love  * @mp:	   The EM that the exchange is on
1244*3a3b42bfSRobert Love  * @fp:	   The request frame
124542e9a92fSRobert Love  */
1246*3a3b42bfSRobert Love static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
124742e9a92fSRobert Love 			     struct fc_frame *fp)
124842e9a92fSRobert Love {
124942e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
125042e9a92fSRobert Love 	struct fc_seq *sp = NULL;
125142e9a92fSRobert Love 	struct fc_exch *ep = NULL;
125242e9a92fSRobert Love 	enum fc_sof sof;
125342e9a92fSRobert Love 	enum fc_eof eof;
125442e9a92fSRobert Love 	u32 f_ctl;
125542e9a92fSRobert Love 	enum fc_pf_rjt_reason reject;
125642e9a92fSRobert Love 
1257174e1ebfSChris Leech 	/* We can have the wrong fc_lport at this point with NPIV, which is a
1258174e1ebfSChris Leech 	 * problem now that we know a new exchange needs to be allocated
1259174e1ebfSChris Leech 	 */
1260*3a3b42bfSRobert Love 	lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
1261*3a3b42bfSRobert Love 	if (!lport) {
1262174e1ebfSChris Leech 		fc_frame_free(fp);
1263174e1ebfSChris Leech 		return;
1264174e1ebfSChris Leech 	}
1265174e1ebfSChris Leech 
126642e9a92fSRobert Love 	fr_seq(fp) = NULL;
1267*3a3b42bfSRobert Love 	reject = fc_seq_lookup_recip(lport, mp, fp);
126842e9a92fSRobert Love 	if (reject == FC_RJT_NONE) {
126942e9a92fSRobert Love 		sp = fr_seq(fp);	/* sequence will be held */
127042e9a92fSRobert Love 		ep = fc_seq_exch(sp);
127142e9a92fSRobert Love 		sof = fr_sof(fp);
127242e9a92fSRobert Love 		eof = fr_eof(fp);
127342e9a92fSRobert Love 		f_ctl = ntoh24(fh->fh_f_ctl);
127442e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
127542e9a92fSRobert Love 
127642e9a92fSRobert Love 		/*
127742e9a92fSRobert Love 		 * Call the receive function.
127842e9a92fSRobert Love 		 *
127942e9a92fSRobert Love 		 * The receive function may allocate a new sequence
128042e9a92fSRobert Love 		 * over the old one, so we shouldn't change the
128142e9a92fSRobert Love 		 * sequence after this.
128242e9a92fSRobert Love 		 *
128342e9a92fSRobert Love 		 * The frame will be freed by the receive function.
128442e9a92fSRobert Love 		 * If new exch resp handler is valid then call that
128542e9a92fSRobert Love 		 * first.
128642e9a92fSRobert Love 		 */
128742e9a92fSRobert Love 		if (ep->resp)
128842e9a92fSRobert Love 			ep->resp(sp, fp, ep->arg);
128942e9a92fSRobert Love 		else
1290*3a3b42bfSRobert Love 			lport->tt.lport_recv(lport, sp, fp);
129142e9a92fSRobert Love 		fc_exch_release(ep);	/* release from lookup */
129242e9a92fSRobert Love 	} else {
1293*3a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
1294*3a3b42bfSRobert Love 			     reject);
129542e9a92fSRobert Love 		fc_frame_free(fp);
129642e9a92fSRobert Love 	}
129742e9a92fSRobert Love }
129842e9a92fSRobert Love 
1299*3a3b42bfSRobert Love /**
1300*3a3b42bfSRobert Love  * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
1301*3a3b42bfSRobert Love  *			     end is the originator of the sequence that is a
1302*3a3b42bfSRobert Love  *			     response to our initial exchange
1303*3a3b42bfSRobert Love  * @mp: The EM that the exchange is on
1304*3a3b42bfSRobert Love  * @fp: The response frame
130542e9a92fSRobert Love  */
130642e9a92fSRobert Love static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
130742e9a92fSRobert Love {
130842e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
130942e9a92fSRobert Love 	struct fc_seq *sp;
131042e9a92fSRobert Love 	struct fc_exch *ep;
131142e9a92fSRobert Love 	enum fc_sof sof;
131242e9a92fSRobert Love 	u32 f_ctl;
131342e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
131442e9a92fSRobert Love 	void *ex_resp_arg;
131542e9a92fSRobert Love 	int rc;
131642e9a92fSRobert Love 
131742e9a92fSRobert Love 	ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
131842e9a92fSRobert Love 	if (!ep) {
131942e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
132042e9a92fSRobert Love 		goto out;
132142e9a92fSRobert Love 	}
132230121d14SSteve Ma 	if (ep->esb_stat & ESB_ST_COMPLETE) {
132330121d14SSteve Ma 		atomic_inc(&mp->stats.xid_not_found);
132430121d14SSteve Ma 		goto out;
132530121d14SSteve Ma 	}
132642e9a92fSRobert Love 	if (ep->rxid == FC_XID_UNKNOWN)
132742e9a92fSRobert Love 		ep->rxid = ntohs(fh->fh_rx_id);
132842e9a92fSRobert Love 	if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
132942e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
133042e9a92fSRobert Love 		goto rel;
133142e9a92fSRobert Love 	}
133242e9a92fSRobert Love 	if (ep->did != ntoh24(fh->fh_s_id) &&
133342e9a92fSRobert Love 	    ep->did != FC_FID_FLOGI) {
133442e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
133542e9a92fSRobert Love 		goto rel;
133642e9a92fSRobert Love 	}
133742e9a92fSRobert Love 	sof = fr_sof(fp);
133842e9a92fSRobert Love 	if (fc_sof_is_init(sof)) {
133942e9a92fSRobert Love 		sp = fc_seq_start_next(&ep->seq);
134042e9a92fSRobert Love 		sp->id = fh->fh_seq_id;
134142e9a92fSRobert Love 		sp->ssb_stat |= SSB_ST_RESP;
134242e9a92fSRobert Love 	} else {
134342e9a92fSRobert Love 		sp = &ep->seq;
134442e9a92fSRobert Love 		if (sp->id != fh->fh_seq_id) {
134542e9a92fSRobert Love 			atomic_inc(&mp->stats.seq_not_found);
134642e9a92fSRobert Love 			goto rel;
134742e9a92fSRobert Love 		}
134842e9a92fSRobert Love 	}
134942e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
135042e9a92fSRobert Love 	fr_seq(fp) = sp;
135142e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
135242e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
135342e9a92fSRobert Love 
135442e9a92fSRobert Love 	if (fc_sof_needs_ack(sof))
135542e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
135642e9a92fSRobert Love 	resp = ep->resp;
135742e9a92fSRobert Love 	ex_resp_arg = ep->arg;
135842e9a92fSRobert Love 
135942e9a92fSRobert Love 	if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
136042e9a92fSRobert Love 	    (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
136142e9a92fSRobert Love 	    (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
136242e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
136342e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
136442e9a92fSRobert Love 		WARN_ON(fc_seq_exch(sp) != ep);
136542e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
136642e9a92fSRobert Love 		if (!rc)
1367b2f0091fSVasu Dev 			fc_exch_delete(ep);
136842e9a92fSRobert Love 	}
136942e9a92fSRobert Love 
137042e9a92fSRobert Love 	/*
137142e9a92fSRobert Love 	 * Call the receive function.
137242e9a92fSRobert Love 	 * The sequence is held (has a refcnt) for us,
137342e9a92fSRobert Love 	 * but not for the receive function.
137442e9a92fSRobert Love 	 *
137542e9a92fSRobert Love 	 * The receive function may allocate a new sequence
137642e9a92fSRobert Love 	 * over the old one, so we shouldn't change the
137742e9a92fSRobert Love 	 * sequence after this.
137842e9a92fSRobert Love 	 *
137942e9a92fSRobert Love 	 * The frame will be freed by the receive function.
138042e9a92fSRobert Love 	 * If new exch resp handler is valid then call that
138142e9a92fSRobert Love 	 * first.
138242e9a92fSRobert Love 	 */
138342e9a92fSRobert Love 	if (resp)
138442e9a92fSRobert Love 		resp(sp, fp, ex_resp_arg);
138542e9a92fSRobert Love 	else
138642e9a92fSRobert Love 		fc_frame_free(fp);
138742e9a92fSRobert Love 	fc_exch_release(ep);
138842e9a92fSRobert Love 	return;
138942e9a92fSRobert Love rel:
139042e9a92fSRobert Love 	fc_exch_release(ep);
139142e9a92fSRobert Love out:
139242e9a92fSRobert Love 	fc_frame_free(fp);
139342e9a92fSRobert Love }
139442e9a92fSRobert Love 
1395*3a3b42bfSRobert Love /**
1396*3a3b42bfSRobert Love  * fc_exch_recv_resp() - Handler for a sequence where other end is
1397*3a3b42bfSRobert Love  *			 responding to our sequence
1398*3a3b42bfSRobert Love  * @mp: The EM that the exchange is on
1399*3a3b42bfSRobert Love  * @fp: The response frame
140042e9a92fSRobert Love  */
140142e9a92fSRobert Love static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
140242e9a92fSRobert Love {
140342e9a92fSRobert Love 	struct fc_seq *sp;
140442e9a92fSRobert Love 
140542e9a92fSRobert Love 	sp = fc_seq_lookup_orig(mp, fp);	/* doesn't hold sequence */
1406d459b7eaSRobert Love 
1407d459b7eaSRobert Love 	if (!sp)
140842e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
1409d459b7eaSRobert Love 	else
141042e9a92fSRobert Love 		atomic_inc(&mp->stats.non_bls_resp);
1411d459b7eaSRobert Love 
141242e9a92fSRobert Love 	fc_frame_free(fp);
141342e9a92fSRobert Love }
141442e9a92fSRobert Love 
1415*3a3b42bfSRobert Love /**
1416*3a3b42bfSRobert Love  * fc_exch_abts_resp() - Handler for a response to an ABT
1417*3a3b42bfSRobert Love  * @ep: The exchange that the frame is on
1418*3a3b42bfSRobert Love  * @fp: The response frame
1419*3a3b42bfSRobert Love  *
1420*3a3b42bfSRobert Love  * This response would be to an ABTS cancelling an exchange or sequence.
1421*3a3b42bfSRobert Love  * The response can be either BA_ACC or BA_RJT
142242e9a92fSRobert Love  */
142342e9a92fSRobert Love static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
142442e9a92fSRobert Love {
142542e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
142642e9a92fSRobert Love 	void *ex_resp_arg;
142742e9a92fSRobert Love 	struct fc_frame_header *fh;
142842e9a92fSRobert Love 	struct fc_ba_acc *ap;
142942e9a92fSRobert Love 	struct fc_seq *sp;
143042e9a92fSRobert Love 	u16 low;
143142e9a92fSRobert Love 	u16 high;
143242e9a92fSRobert Love 	int rc = 1, has_rec = 0;
143342e9a92fSRobert Love 
143442e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
14357414705eSRobert Love 	FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
14367414705eSRobert Love 		    fc_exch_rctl_name(fh->fh_r_ctl));
143742e9a92fSRobert Love 
143842e9a92fSRobert Love 	if (cancel_delayed_work_sync(&ep->timeout_work))
143942e9a92fSRobert Love 		fc_exch_release(ep);	/* release from pending timer hold */
144042e9a92fSRobert Love 
144142e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
144242e9a92fSRobert Love 	switch (fh->fh_r_ctl) {
144342e9a92fSRobert Love 	case FC_RCTL_BA_ACC:
144442e9a92fSRobert Love 		ap = fc_frame_payload_get(fp, sizeof(*ap));
144542e9a92fSRobert Love 		if (!ap)
144642e9a92fSRobert Love 			break;
144742e9a92fSRobert Love 
144842e9a92fSRobert Love 		/*
144942e9a92fSRobert Love 		 * Decide whether to establish a Recovery Qualifier.
145042e9a92fSRobert Love 		 * We do this if there is a non-empty SEQ_CNT range and
145142e9a92fSRobert Love 		 * SEQ_ID is the same as the one we aborted.
145242e9a92fSRobert Love 		 */
145342e9a92fSRobert Love 		low = ntohs(ap->ba_low_seq_cnt);
145442e9a92fSRobert Love 		high = ntohs(ap->ba_high_seq_cnt);
145542e9a92fSRobert Love 		if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
145642e9a92fSRobert Love 		    (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
145742e9a92fSRobert Love 		     ap->ba_seq_id == ep->seq_id) && low != high) {
145842e9a92fSRobert Love 			ep->esb_stat |= ESB_ST_REC_QUAL;
145942e9a92fSRobert Love 			fc_exch_hold(ep);  /* hold for recovery qualifier */
146042e9a92fSRobert Love 			has_rec = 1;
146142e9a92fSRobert Love 		}
146242e9a92fSRobert Love 		break;
146342e9a92fSRobert Love 	case FC_RCTL_BA_RJT:
146442e9a92fSRobert Love 		break;
146542e9a92fSRobert Love 	default:
146642e9a92fSRobert Love 		break;
146742e9a92fSRobert Love 	}
146842e9a92fSRobert Love 
146942e9a92fSRobert Love 	resp = ep->resp;
147042e9a92fSRobert Love 	ex_resp_arg = ep->arg;
147142e9a92fSRobert Love 
147242e9a92fSRobert Love 	/* do we need to do some other checks here. Can we reuse more of
147342e9a92fSRobert Love 	 * fc_exch_recv_seq_resp
147442e9a92fSRobert Love 	 */
147542e9a92fSRobert Love 	sp = &ep->seq;
147642e9a92fSRobert Love 	/*
147742e9a92fSRobert Love 	 * do we want to check END_SEQ as well as LAST_SEQ here?
147842e9a92fSRobert Love 	 */
147942e9a92fSRobert Love 	if (ep->fh_type != FC_TYPE_FCP &&
148042e9a92fSRobert Love 	    ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
148142e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
148242e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
148342e9a92fSRobert Love 	if (!rc)
1484b2f0091fSVasu Dev 		fc_exch_delete(ep);
148542e9a92fSRobert Love 
148642e9a92fSRobert Love 	if (resp)
148742e9a92fSRobert Love 		resp(sp, fp, ex_resp_arg);
148842e9a92fSRobert Love 	else
148942e9a92fSRobert Love 		fc_frame_free(fp);
149042e9a92fSRobert Love 
149142e9a92fSRobert Love 	if (has_rec)
149242e9a92fSRobert Love 		fc_exch_timer_set(ep, ep->r_a_tov);
149342e9a92fSRobert Love 
149442e9a92fSRobert Love }
149542e9a92fSRobert Love 
1496*3a3b42bfSRobert Love /**
1497*3a3b42bfSRobert Love  * fc_exch_recv_bls() - Handler for a BLS sequence
1498*3a3b42bfSRobert Love  * @mp: The EM that the exchange is on
1499*3a3b42bfSRobert Love  * @fp: The request frame
1500*3a3b42bfSRobert Love  *
1501*3a3b42bfSRobert Love  * The BLS frame is always a sequence initiated by the remote side.
150242e9a92fSRobert Love  * We may be either the originator or recipient of the exchange.
150342e9a92fSRobert Love  */
150442e9a92fSRobert Love static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
150542e9a92fSRobert Love {
150642e9a92fSRobert Love 	struct fc_frame_header *fh;
150742e9a92fSRobert Love 	struct fc_exch *ep;
150842e9a92fSRobert Love 	u32 f_ctl;
150942e9a92fSRobert Love 
151042e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
151142e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
151242e9a92fSRobert Love 	fr_seq(fp) = NULL;
151342e9a92fSRobert Love 
151442e9a92fSRobert Love 	ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
151542e9a92fSRobert Love 			  ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
151642e9a92fSRobert Love 	if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
151742e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
151842e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
151942e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
152042e9a92fSRobert Love 	}
152142e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_CTX) {
152242e9a92fSRobert Love 		/*
152342e9a92fSRobert Love 		 * A response to a sequence we initiated.
152442e9a92fSRobert Love 		 * This should only be ACKs for class 2 or F.
152542e9a92fSRobert Love 		 */
152642e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
152742e9a92fSRobert Love 		case FC_RCTL_ACK_1:
152842e9a92fSRobert Love 		case FC_RCTL_ACK_0:
152942e9a92fSRobert Love 			break;
153042e9a92fSRobert Love 		default:
15317414705eSRobert Love 			FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
153242e9a92fSRobert Love 				    fh->fh_r_ctl,
153342e9a92fSRobert Love 				    fc_exch_rctl_name(fh->fh_r_ctl));
153442e9a92fSRobert Love 			break;
153542e9a92fSRobert Love 		}
153642e9a92fSRobert Love 		fc_frame_free(fp);
153742e9a92fSRobert Love 	} else {
153842e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
153942e9a92fSRobert Love 		case FC_RCTL_BA_RJT:
154042e9a92fSRobert Love 		case FC_RCTL_BA_ACC:
154142e9a92fSRobert Love 			if (ep)
154242e9a92fSRobert Love 				fc_exch_abts_resp(ep, fp);
154342e9a92fSRobert Love 			else
154442e9a92fSRobert Love 				fc_frame_free(fp);
154542e9a92fSRobert Love 			break;
154642e9a92fSRobert Love 		case FC_RCTL_BA_ABTS:
154742e9a92fSRobert Love 			fc_exch_recv_abts(ep, fp);
154842e9a92fSRobert Love 			break;
154942e9a92fSRobert Love 		default:			/* ignore junk */
155042e9a92fSRobert Love 			fc_frame_free(fp);
155142e9a92fSRobert Love 			break;
155242e9a92fSRobert Love 		}
155342e9a92fSRobert Love 	}
155442e9a92fSRobert Love 	if (ep)
155542e9a92fSRobert Love 		fc_exch_release(ep);	/* release hold taken by fc_exch_find */
155642e9a92fSRobert Love }
155742e9a92fSRobert Love 
1558*3a3b42bfSRobert Love /**
1559*3a3b42bfSRobert Love  * fc_seq_ls_acc() - Accept sequence with LS_ACC
1560*3a3b42bfSRobert Love  * @req_sp: The request sequence
1561*3a3b42bfSRobert Love  *
156242e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
156342e9a92fSRobert Love  * originator will repeat the sequence.
156442e9a92fSRobert Love  */
156542e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *req_sp)
156642e9a92fSRobert Love {
156742e9a92fSRobert Love 	struct fc_seq *sp;
156842e9a92fSRobert Love 	struct fc_els_ls_acc *acc;
156942e9a92fSRobert Love 	struct fc_frame *fp;
157042e9a92fSRobert Love 
157142e9a92fSRobert Love 	sp = fc_seq_start_next(req_sp);
157242e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
157342e9a92fSRobert Love 	if (fp) {
157442e9a92fSRobert Love 		acc = fc_frame_payload_get(fp, sizeof(*acc));
157542e9a92fSRobert Love 		memset(acc, 0, sizeof(*acc));
157642e9a92fSRobert Love 		acc->la_cmd = ELS_LS_ACC;
157742e9a92fSRobert Love 		fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
157842e9a92fSRobert Love 	}
157942e9a92fSRobert Love }
158042e9a92fSRobert Love 
1581*3a3b42bfSRobert Love /**
1582*3a3b42bfSRobert Love  * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
1583*3a3b42bfSRobert Love  * @req_sp: The request sequence
1584*3a3b42bfSRobert Love  * @reason: The reason the sequence is being rejected
1585*3a3b42bfSRobert Love  * @explan: The explaination for the rejection
1586*3a3b42bfSRobert Love  *
158742e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
158842e9a92fSRobert Love  * originator will repeat the sequence.
158942e9a92fSRobert Love  */
159042e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
159142e9a92fSRobert Love 			  enum fc_els_rjt_explan explan)
159242e9a92fSRobert Love {
159342e9a92fSRobert Love 	struct fc_seq *sp;
159442e9a92fSRobert Love 	struct fc_els_ls_rjt *rjt;
159542e9a92fSRobert Love 	struct fc_frame *fp;
159642e9a92fSRobert Love 
159742e9a92fSRobert Love 	sp = fc_seq_start_next(req_sp);
159842e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
159942e9a92fSRobert Love 	if (fp) {
160042e9a92fSRobert Love 		rjt = fc_frame_payload_get(fp, sizeof(*rjt));
160142e9a92fSRobert Love 		memset(rjt, 0, sizeof(*rjt));
160242e9a92fSRobert Love 		rjt->er_cmd = ELS_LS_RJT;
160342e9a92fSRobert Love 		rjt->er_reason = reason;
160442e9a92fSRobert Love 		rjt->er_explan = explan;
160542e9a92fSRobert Love 		fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
160642e9a92fSRobert Love 	}
160742e9a92fSRobert Love }
160842e9a92fSRobert Love 
1609*3a3b42bfSRobert Love /**
1610*3a3b42bfSRobert Love  * fc_exch_reset() - Reset an exchange
1611*3a3b42bfSRobert Love  * @ep: The exchange to be reset
1612*3a3b42bfSRobert Love  */
161342e9a92fSRobert Love static void fc_exch_reset(struct fc_exch *ep)
161442e9a92fSRobert Love {
161542e9a92fSRobert Love 	struct fc_seq *sp;
161642e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *, void *);
161742e9a92fSRobert Love 	void *arg;
161842e9a92fSRobert Love 	int rc = 1;
161942e9a92fSRobert Love 
162042e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
162142e9a92fSRobert Love 	ep->state |= FC_EX_RST_CLEANUP;
162242e9a92fSRobert Love 	/*
162342e9a92fSRobert Love 	 * we really want to call del_timer_sync, but cannot due
162442e9a92fSRobert Love 	 * to the lport calling with the lport lock held (some resp
162542e9a92fSRobert Love 	 * functions can also grab the lport lock which could cause
162642e9a92fSRobert Love 	 * a deadlock).
162742e9a92fSRobert Love 	 */
162842e9a92fSRobert Love 	if (cancel_delayed_work(&ep->timeout_work))
162942e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for timer */
163042e9a92fSRobert Love 	resp = ep->resp;
163142e9a92fSRobert Love 	ep->resp = NULL;
163242e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL)
163342e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec_qual */
163442e9a92fSRobert Love 	ep->esb_stat &= ~ESB_ST_REC_QUAL;
163542e9a92fSRobert Love 	arg = ep->arg;
163642e9a92fSRobert Love 	sp = &ep->seq;
163742e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
163842e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
163942e9a92fSRobert Love 	if (!rc)
1640b2f0091fSVasu Dev 		fc_exch_delete(ep);
164142e9a92fSRobert Love 
164242e9a92fSRobert Love 	if (resp)
164342e9a92fSRobert Love 		resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
164442e9a92fSRobert Love }
164542e9a92fSRobert Love 
1646b2f0091fSVasu Dev /**
1647*3a3b42bfSRobert Love  * fc_exch_pool_reset() - Reset a per cpu exchange pool
1648*3a3b42bfSRobert Love  * @lport: The local port that the exchange pool is on
1649*3a3b42bfSRobert Love  * @pool:  The exchange pool to be reset
1650*3a3b42bfSRobert Love  * @sid:   The source ID
1651*3a3b42bfSRobert Love  * @did:   The destination ID
1652b2f0091fSVasu Dev  *
1653*3a3b42bfSRobert Love  * Resets a per cpu exches pool, releasing all of its sequences
1654*3a3b42bfSRobert Love  * and exchanges. If sid is non-zero then reset only exchanges
1655*3a3b42bfSRobert Love  * we sourced from the local port's FID. If did is non-zero then
1656*3a3b42bfSRobert Love  * only reset exchanges destined for the local port's FID.
165742e9a92fSRobert Love  */
1658b2f0091fSVasu Dev static void fc_exch_pool_reset(struct fc_lport *lport,
1659b2f0091fSVasu Dev 			       struct fc_exch_pool *pool,
1660b2f0091fSVasu Dev 			       u32 sid, u32 did)
166142e9a92fSRobert Love {
166242e9a92fSRobert Love 	struct fc_exch *ep;
166342e9a92fSRobert Love 	struct fc_exch *next;
166442e9a92fSRobert Love 
1665b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
166642e9a92fSRobert Love restart:
1667b2f0091fSVasu Dev 	list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
1668b2f0091fSVasu Dev 		if ((lport == ep->lp) &&
166952ff878cSVasu Dev 		    (sid == 0 || sid == ep->sid) &&
167042e9a92fSRobert Love 		    (did == 0 || did == ep->did)) {
167142e9a92fSRobert Love 			fc_exch_hold(ep);
1672b2f0091fSVasu Dev 			spin_unlock_bh(&pool->lock);
167342e9a92fSRobert Love 
167442e9a92fSRobert Love 			fc_exch_reset(ep);
167542e9a92fSRobert Love 
167642e9a92fSRobert Love 			fc_exch_release(ep);
1677b2f0091fSVasu Dev 			spin_lock_bh(&pool->lock);
167842e9a92fSRobert Love 
167942e9a92fSRobert Love 			/*
168052ff878cSVasu Dev 			 * must restart loop incase while lock
168152ff878cSVasu Dev 			 * was down multiple eps were released.
168242e9a92fSRobert Love 			 */
168342e9a92fSRobert Love 			goto restart;
168442e9a92fSRobert Love 		}
168542e9a92fSRobert Love 	}
1686b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
1687b2f0091fSVasu Dev }
1688b2f0091fSVasu Dev 
1689b2f0091fSVasu Dev /**
1690*3a3b42bfSRobert Love  * fc_exch_mgr_reset() - Reset all EMs of a local port
1691*3a3b42bfSRobert Love  * @lport: The local port whose EMs are to be reset
1692*3a3b42bfSRobert Love  * @sid:   The source ID
1693*3a3b42bfSRobert Love  * @did:   The destination ID
1694b2f0091fSVasu Dev  *
1695*3a3b42bfSRobert Love  * Reset all EMs associated with a given local port. Release all
1696*3a3b42bfSRobert Love  * sequences and exchanges. If sid is non-zero then reset only the
1697*3a3b42bfSRobert Love  * exchanges sent from the local port's FID. If did is non-zero then
1698*3a3b42bfSRobert Love  * reset only exchanges destined for the local port's FID.
1699b2f0091fSVasu Dev  */
1700b2f0091fSVasu Dev void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1701b2f0091fSVasu Dev {
1702b2f0091fSVasu Dev 	struct fc_exch_mgr_anchor *ema;
1703b2f0091fSVasu Dev 	unsigned int cpu;
1704b2f0091fSVasu Dev 
1705b2f0091fSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
1706b2f0091fSVasu Dev 		for_each_possible_cpu(cpu)
1707b2f0091fSVasu Dev 			fc_exch_pool_reset(lport,
1708b2f0091fSVasu Dev 					   per_cpu_ptr(ema->mp->pool, cpu),
1709b2f0091fSVasu Dev 					   sid, did);
171042e9a92fSRobert Love 	}
171152ff878cSVasu Dev }
171242e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_reset);
171342e9a92fSRobert Love 
1714*3a3b42bfSRobert Love /**
1715*3a3b42bfSRobert Love  * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
1716*3a3b42bfSRobert Love  * @sp:	 The sequence the REC is on
1717*3a3b42bfSRobert Love  * @rfp: The REC frame
1718*3a3b42bfSRobert Love  *
171942e9a92fSRobert Love  * Note that the requesting port may be different than the S_ID in the request.
172042e9a92fSRobert Love  */
172142e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
172242e9a92fSRobert Love {
172342e9a92fSRobert Love 	struct fc_frame *fp;
172442e9a92fSRobert Love 	struct fc_exch *ep;
172542e9a92fSRobert Love 	struct fc_exch_mgr *em;
172642e9a92fSRobert Love 	struct fc_els_rec *rp;
172742e9a92fSRobert Love 	struct fc_els_rec_acc *acc;
172842e9a92fSRobert Love 	enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
172942e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
173042e9a92fSRobert Love 	u32 sid;
173142e9a92fSRobert Love 	u16 rxid;
173242e9a92fSRobert Love 	u16 oxid;
173342e9a92fSRobert Love 
173442e9a92fSRobert Love 	rp = fc_frame_payload_get(rfp, sizeof(*rp));
173542e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
173642e9a92fSRobert Love 	if (!rp)
173742e9a92fSRobert Love 		goto reject;
173842e9a92fSRobert Love 	sid = ntoh24(rp->rec_s_id);
173942e9a92fSRobert Love 	rxid = ntohs(rp->rec_rx_id);
174042e9a92fSRobert Love 	oxid = ntohs(rp->rec_ox_id);
174142e9a92fSRobert Love 
174242e9a92fSRobert Love 	/*
174342e9a92fSRobert Love 	 * Currently it's hard to find the local S_ID from the exchange
174442e9a92fSRobert Love 	 * manager.  This will eventually be fixed, but for now it's easier
174542e9a92fSRobert Love 	 * to lookup the subject exchange twice, once as if we were
174642e9a92fSRobert Love 	 * the initiator, and then again if we weren't.
174742e9a92fSRobert Love 	 */
174842e9a92fSRobert Love 	em = fc_seq_exch(sp)->em;
174942e9a92fSRobert Love 	ep = fc_exch_find(em, oxid);
175042e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
175142e9a92fSRobert Love 	if (ep && ep->oid == sid) {
175242e9a92fSRobert Love 		if (ep->rxid != FC_XID_UNKNOWN &&
175342e9a92fSRobert Love 		    rxid != FC_XID_UNKNOWN &&
175442e9a92fSRobert Love 		    ep->rxid != rxid)
175542e9a92fSRobert Love 			goto rel;
175642e9a92fSRobert Love 	} else {
175742e9a92fSRobert Love 		if (ep)
175842e9a92fSRobert Love 			fc_exch_release(ep);
175942e9a92fSRobert Love 		ep = NULL;
176042e9a92fSRobert Love 		if (rxid != FC_XID_UNKNOWN)
176142e9a92fSRobert Love 			ep = fc_exch_find(em, rxid);
176242e9a92fSRobert Love 		if (!ep)
176342e9a92fSRobert Love 			goto reject;
176442e9a92fSRobert Love 	}
176542e9a92fSRobert Love 
176642e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
176742e9a92fSRobert Love 	if (!fp) {
176842e9a92fSRobert Love 		fc_exch_done(sp);
176942e9a92fSRobert Love 		goto out;
177042e9a92fSRobert Love 	}
177142e9a92fSRobert Love 	sp = fc_seq_start_next(sp);
177242e9a92fSRobert Love 	acc = fc_frame_payload_get(fp, sizeof(*acc));
177342e9a92fSRobert Love 	memset(acc, 0, sizeof(*acc));
177442e9a92fSRobert Love 	acc->reca_cmd = ELS_LS_ACC;
177542e9a92fSRobert Love 	acc->reca_ox_id = rp->rec_ox_id;
177642e9a92fSRobert Love 	memcpy(acc->reca_ofid, rp->rec_s_id, 3);
177742e9a92fSRobert Love 	acc->reca_rx_id = htons(ep->rxid);
177842e9a92fSRobert Love 	if (ep->sid == ep->oid)
177942e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->did);
178042e9a92fSRobert Love 	else
178142e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->sid);
178242e9a92fSRobert Love 	acc->reca_fc4value = htonl(ep->seq.rec_data);
178342e9a92fSRobert Love 	acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
178442e9a92fSRobert Love 						 ESB_ST_SEQ_INIT |
178542e9a92fSRobert Love 						 ESB_ST_COMPLETE));
178642e9a92fSRobert Love 	sp = fc_seq_start_next(sp);
178742e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
178842e9a92fSRobert Love out:
178942e9a92fSRobert Love 	fc_exch_release(ep);
179042e9a92fSRobert Love 	fc_frame_free(rfp);
179142e9a92fSRobert Love 	return;
179242e9a92fSRobert Love 
179342e9a92fSRobert Love rel:
179442e9a92fSRobert Love 	fc_exch_release(ep);
179542e9a92fSRobert Love reject:
179642e9a92fSRobert Love 	fc_seq_ls_rjt(sp, reason, explan);
179742e9a92fSRobert Love 	fc_frame_free(rfp);
179842e9a92fSRobert Love }
179942e9a92fSRobert Love 
1800*3a3b42bfSRobert Love /**
1801*3a3b42bfSRobert Love  * fc_exch_rrq_resp() - Handler for RRQ responses
1802*3a3b42bfSRobert Love  * @sp:	 The sequence that the RRQ is on
1803*3a3b42bfSRobert Love  * @fp:	 The RRQ frame
1804*3a3b42bfSRobert Love  * @arg: The exchange that the RRQ is on
180542e9a92fSRobert Love  *
180642e9a92fSRobert Love  * TODO: fix error handler.
180742e9a92fSRobert Love  */
180842e9a92fSRobert Love static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
180942e9a92fSRobert Love {
181042e9a92fSRobert Love 	struct fc_exch *aborted_ep = arg;
181142e9a92fSRobert Love 	unsigned int op;
181242e9a92fSRobert Love 
181342e9a92fSRobert Love 	if (IS_ERR(fp)) {
181442e9a92fSRobert Love 		int err = PTR_ERR(fp);
181542e9a92fSRobert Love 
181678342da3SVasu Dev 		if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
181742e9a92fSRobert Love 			goto cleanup;
18187414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
18197414705eSRobert Love 			    "frame error %d\n", err);
182042e9a92fSRobert Love 		return;
182142e9a92fSRobert Love 	}
182242e9a92fSRobert Love 
182342e9a92fSRobert Love 	op = fc_frame_payload_op(fp);
182442e9a92fSRobert Love 	fc_frame_free(fp);
182542e9a92fSRobert Love 
182642e9a92fSRobert Love 	switch (op) {
182742e9a92fSRobert Love 	case ELS_LS_RJT:
18287414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
182942e9a92fSRobert Love 		/* fall through */
183042e9a92fSRobert Love 	case ELS_LS_ACC:
183142e9a92fSRobert Love 		goto cleanup;
183242e9a92fSRobert Love 	default:
18337414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
18347414705eSRobert Love 			    "for RRQ", op);
183542e9a92fSRobert Love 		return;
183642e9a92fSRobert Love 	}
183742e9a92fSRobert Love 
183842e9a92fSRobert Love cleanup:
183942e9a92fSRobert Love 	fc_exch_done(&aborted_ep->seq);
184042e9a92fSRobert Love 	/* drop hold for rec qual */
184142e9a92fSRobert Love 	fc_exch_release(aborted_ep);
184242e9a92fSRobert Love }
184342e9a92fSRobert Love 
18441a7b75aeSRobert Love 
18451a7b75aeSRobert Love /**
1846*3a3b42bfSRobert Love  * fc_exch_seq_send() - Send a frame using a new exchange and sequence
1847*3a3b42bfSRobert Love  * @lport:	The local port to send the frame on
1848*3a3b42bfSRobert Love  * @fp:		The frame to be sent
1849*3a3b42bfSRobert Love  * @resp:	The response handler for this request
1850*3a3b42bfSRobert Love  * @destructor: The destructor for the exchange
1851*3a3b42bfSRobert Love  * @arg:	The argument to be passed to the response handler
1852*3a3b42bfSRobert Love  * @timer_msec: The timeout period for the exchange
1853*3a3b42bfSRobert Love  *
1854*3a3b42bfSRobert Love  * The frame pointer with some of the header's fields must be
1855*3a3b42bfSRobert Love  * filled before calling this routine, those fields are:
1856*3a3b42bfSRobert Love  *
1857*3a3b42bfSRobert Love  * - routing control
1858*3a3b42bfSRobert Love  * - FC port did
1859*3a3b42bfSRobert Love  * - FC port sid
1860*3a3b42bfSRobert Love  * - FC header type
1861*3a3b42bfSRobert Love  * - frame control
1862*3a3b42bfSRobert Love  * - parameter or relative offset
18631a7b75aeSRobert Love  */
1864*3a3b42bfSRobert Love static struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
18651a7b75aeSRobert Love 				       struct fc_frame *fp,
18661a7b75aeSRobert Love 				       void (*resp)(struct fc_seq *,
18671a7b75aeSRobert Love 						    struct fc_frame *fp,
18681a7b75aeSRobert Love 						    void *arg),
18691a7b75aeSRobert Love 				       void (*destructor)(struct fc_seq *,
18701a7b75aeSRobert Love 							  void *),
18711a7b75aeSRobert Love 				       void *arg, u32 timer_msec)
18721a7b75aeSRobert Love {
18731a7b75aeSRobert Love 	struct fc_exch *ep;
18741a7b75aeSRobert Love 	struct fc_seq *sp = NULL;
18751a7b75aeSRobert Love 	struct fc_frame_header *fh;
18761a7b75aeSRobert Love 	int rc = 1;
18771a7b75aeSRobert Love 
1878*3a3b42bfSRobert Love 	ep = fc_exch_alloc(lport, fp);
18791a7b75aeSRobert Love 	if (!ep) {
18801a7b75aeSRobert Love 		fc_frame_free(fp);
18811a7b75aeSRobert Love 		return NULL;
18821a7b75aeSRobert Love 	}
18831a7b75aeSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT;
18841a7b75aeSRobert Love 	fh = fc_frame_header_get(fp);
18851a7b75aeSRobert Love 	fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
18861a7b75aeSRobert Love 	ep->resp = resp;
18871a7b75aeSRobert Love 	ep->destructor = destructor;
18881a7b75aeSRobert Love 	ep->arg = arg;
18891a7b75aeSRobert Love 	ep->r_a_tov = FC_DEF_R_A_TOV;
1890*3a3b42bfSRobert Love 	ep->lp = lport;
18911a7b75aeSRobert Love 	sp = &ep->seq;
18921a7b75aeSRobert Love 
18931a7b75aeSRobert Love 	ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
18941a7b75aeSRobert Love 	ep->f_ctl = ntoh24(fh->fh_f_ctl);
18951a7b75aeSRobert Love 	fc_exch_setup_hdr(ep, fp, ep->f_ctl);
18961a7b75aeSRobert Love 	sp->cnt++;
18971a7b75aeSRobert Love 
1898*3a3b42bfSRobert Love 	if (ep->xid <= lport->lro_xid)
18991a7b75aeSRobert Love 		fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
19001a7b75aeSRobert Love 
1901*3a3b42bfSRobert Love 	if (unlikely(lport->tt.frame_send(lport, fp)))
19021a7b75aeSRobert Love 		goto err;
19031a7b75aeSRobert Love 
19041a7b75aeSRobert Love 	if (timer_msec)
19051a7b75aeSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
19061a7b75aeSRobert Love 	ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not first seq */
19071a7b75aeSRobert Love 
19081a7b75aeSRobert Love 	if (ep->f_ctl & FC_FC_SEQ_INIT)
19091a7b75aeSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
19101a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
19111a7b75aeSRobert Love 	return sp;
19121a7b75aeSRobert Love err:
19131a7b75aeSRobert Love 	rc = fc_exch_done_locked(ep);
19141a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
19151a7b75aeSRobert Love 	if (!rc)
19161a7b75aeSRobert Love 		fc_exch_delete(ep);
19171a7b75aeSRobert Love 	return NULL;
19181a7b75aeSRobert Love }
19191a7b75aeSRobert Love 
1920*3a3b42bfSRobert Love /**
1921*3a3b42bfSRobert Love  * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
1922*3a3b42bfSRobert Love  * @ep: The exchange to send the RRQ on
1923*3a3b42bfSRobert Love  *
192442e9a92fSRobert Love  * This tells the remote port to stop blocking the use of
192542e9a92fSRobert Love  * the exchange and the seq_cnt range.
192642e9a92fSRobert Love  */
192742e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *ep)
192842e9a92fSRobert Love {
1929*3a3b42bfSRobert Love 	struct fc_lport *lport;
193042e9a92fSRobert Love 	struct fc_els_rrq *rrq;
193142e9a92fSRobert Love 	struct fc_frame *fp;
193242e9a92fSRobert Love 	u32 did;
193342e9a92fSRobert Love 
1934*3a3b42bfSRobert Love 	lport = ep->lp;
193542e9a92fSRobert Love 
1936*3a3b42bfSRobert Love 	fp = fc_frame_alloc(lport, sizeof(*rrq));
193742e9a92fSRobert Love 	if (!fp)
1938a0cc1eccSVasu Dev 		goto retry;
1939a0cc1eccSVasu Dev 
194042e9a92fSRobert Love 	rrq = fc_frame_payload_get(fp, sizeof(*rrq));
194142e9a92fSRobert Love 	memset(rrq, 0, sizeof(*rrq));
194242e9a92fSRobert Love 	rrq->rrq_cmd = ELS_RRQ;
194342e9a92fSRobert Love 	hton24(rrq->rrq_s_id, ep->sid);
194442e9a92fSRobert Love 	rrq->rrq_ox_id = htons(ep->oxid);
194542e9a92fSRobert Love 	rrq->rrq_rx_id = htons(ep->rxid);
194642e9a92fSRobert Love 
194742e9a92fSRobert Love 	did = ep->did;
194842e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP)
194942e9a92fSRobert Love 		did = ep->sid;
195042e9a92fSRobert Love 
195142e9a92fSRobert Love 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1952*3a3b42bfSRobert Love 		       fc_host_port_id(lport->host), FC_TYPE_ELS,
195342e9a92fSRobert Love 		       FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
195442e9a92fSRobert Love 
1955*3a3b42bfSRobert Love 	if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
1956*3a3b42bfSRobert Love 			     lport->e_d_tov))
1957a0cc1eccSVasu Dev 		return;
1958a0cc1eccSVasu Dev 
1959a0cc1eccSVasu Dev retry:
1960a0cc1eccSVasu Dev 	spin_lock_bh(&ep->ex_lock);
1961a0cc1eccSVasu Dev 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1962a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
1963a0cc1eccSVasu Dev 		/* drop hold for rec qual */
1964a0cc1eccSVasu Dev 		fc_exch_release(ep);
196542e9a92fSRobert Love 		return;
196642e9a92fSRobert Love 	}
1967a0cc1eccSVasu Dev 	ep->esb_stat |= ESB_ST_REC_QUAL;
1968a0cc1eccSVasu Dev 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
1969a0cc1eccSVasu Dev 	spin_unlock_bh(&ep->ex_lock);
197042e9a92fSRobert Love }
197142e9a92fSRobert Love 
197242e9a92fSRobert Love 
1973*3a3b42bfSRobert Love /**
1974*3a3b42bfSRobert Love  * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
1975*3a3b42bfSRobert Love  * @sp: The sequence that the RRQ is on
1976*3a3b42bfSRobert Love  * @fp: The RRQ frame
197742e9a92fSRobert Love  */
197842e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
197942e9a92fSRobert Love {
19803f127ad9SVasu Dev 	struct fc_exch *ep = NULL;	/* request or subject exchange */
198142e9a92fSRobert Love 	struct fc_els_rrq *rp;
198242e9a92fSRobert Love 	u32 sid;
198342e9a92fSRobert Love 	u16 xid;
198442e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
198542e9a92fSRobert Love 
198642e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
198742e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
198842e9a92fSRobert Love 	if (!rp)
198942e9a92fSRobert Love 		goto reject;
199042e9a92fSRobert Love 
199142e9a92fSRobert Love 	/*
199242e9a92fSRobert Love 	 * lookup subject exchange.
199342e9a92fSRobert Love 	 */
199442e9a92fSRobert Love 	ep = fc_seq_exch(sp);
199542e9a92fSRobert Love 	sid = ntoh24(rp->rrq_s_id);		/* subject source */
199642e9a92fSRobert Love 	xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
199742e9a92fSRobert Love 	ep = fc_exch_find(ep->em, xid);
199842e9a92fSRobert Love 
199942e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
200042e9a92fSRobert Love 	if (!ep)
200142e9a92fSRobert Love 		goto reject;
200242e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
200342e9a92fSRobert Love 	if (ep->oxid != ntohs(rp->rrq_ox_id))
200442e9a92fSRobert Love 		goto unlock_reject;
200542e9a92fSRobert Love 	if (ep->rxid != ntohs(rp->rrq_rx_id) &&
200642e9a92fSRobert Love 	    ep->rxid != FC_XID_UNKNOWN)
200742e9a92fSRobert Love 		goto unlock_reject;
200842e9a92fSRobert Love 	explan = ELS_EXPL_SID;
200942e9a92fSRobert Love 	if (ep->sid != sid)
201042e9a92fSRobert Love 		goto unlock_reject;
201142e9a92fSRobert Love 
201242e9a92fSRobert Love 	/*
201342e9a92fSRobert Love 	 * Clear Recovery Qualifier state, and cancel timer if complete.
201442e9a92fSRobert Love 	 */
201542e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL) {
201642e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_REC_QUAL;
201742e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec qual */
201842e9a92fSRobert Love 	}
201942e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
202042e9a92fSRobert Love 		if (cancel_delayed_work(&ep->timeout_work))
202142e9a92fSRobert Love 			atomic_dec(&ep->ex_refcnt);	/* drop timer hold */
202242e9a92fSRobert Love 	}
202342e9a92fSRobert Love 
202442e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
202542e9a92fSRobert Love 
202642e9a92fSRobert Love 	/*
202742e9a92fSRobert Love 	 * Send LS_ACC.
202842e9a92fSRobert Love 	 */
202942e9a92fSRobert Love 	fc_seq_ls_acc(sp);
20303f127ad9SVasu Dev 	goto out;
203142e9a92fSRobert Love 
203242e9a92fSRobert Love unlock_reject:
203342e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
203442e9a92fSRobert Love reject:
203542e9a92fSRobert Love 	fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
20363f127ad9SVasu Dev out:
203742e9a92fSRobert Love 	fc_frame_free(fp);
20383f127ad9SVasu Dev 	if (ep)
20393f127ad9SVasu Dev 		fc_exch_release(ep);	/* drop hold from fc_exch_find */
204042e9a92fSRobert Love }
204142e9a92fSRobert Love 
2042*3a3b42bfSRobert Love /**
2043*3a3b42bfSRobert Love  * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
2044*3a3b42bfSRobert Love  * @lport: The local port to add the exchange manager to
2045*3a3b42bfSRobert Love  * @mp:	   The exchange manager to be added to the local port
2046*3a3b42bfSRobert Love  * @match: The match routine that indicates when this EM should be used
2047*3a3b42bfSRobert Love  */
204896316099SVasu Dev struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
204996316099SVasu Dev 					   struct fc_exch_mgr *mp,
205096316099SVasu Dev 					   bool (*match)(struct fc_frame *))
205196316099SVasu Dev {
205296316099SVasu Dev 	struct fc_exch_mgr_anchor *ema;
205396316099SVasu Dev 
205496316099SVasu Dev 	ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
205596316099SVasu Dev 	if (!ema)
205696316099SVasu Dev 		return ema;
205796316099SVasu Dev 
205896316099SVasu Dev 	ema->mp = mp;
205996316099SVasu Dev 	ema->match = match;
206096316099SVasu Dev 	/* add EM anchor to EM anchors list */
206196316099SVasu Dev 	list_add_tail(&ema->ema_list, &lport->ema_list);
206296316099SVasu Dev 	kref_get(&mp->kref);
206396316099SVasu Dev 	return ema;
206496316099SVasu Dev }
206596316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_add);
206696316099SVasu Dev 
2067*3a3b42bfSRobert Love /**
2068*3a3b42bfSRobert Love  * fc_exch_mgr_destroy() - Destroy an exchange manager
2069*3a3b42bfSRobert Love  * @kref: The reference to the EM to be destroyed
2070*3a3b42bfSRobert Love  */
207196316099SVasu Dev static void fc_exch_mgr_destroy(struct kref *kref)
207296316099SVasu Dev {
207396316099SVasu Dev 	struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
207496316099SVasu Dev 
207596316099SVasu Dev 	mempool_destroy(mp->ep_pool);
2076e4bc50beSVasu Dev 	free_percpu(mp->pool);
207796316099SVasu Dev 	kfree(mp);
207896316099SVasu Dev }
207996316099SVasu Dev 
2080*3a3b42bfSRobert Love /**
2081*3a3b42bfSRobert Love  * fc_exch_mgr_del() - Delete an EM from a local port's list
2082*3a3b42bfSRobert Love  * @ema: The exchange manager anchor identifying the EM to be deleted
2083*3a3b42bfSRobert Love  */
208496316099SVasu Dev void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
208596316099SVasu Dev {
208696316099SVasu Dev 	/* remove EM anchor from EM anchors list */
208796316099SVasu Dev 	list_del(&ema->ema_list);
208896316099SVasu Dev 	kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
208996316099SVasu Dev 	kfree(ema);
209096316099SVasu Dev }
209196316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_del);
209296316099SVasu Dev 
2093174e1ebfSChris Leech /**
2094*3a3b42bfSRobert Love  * fc_exch_mgr_list_clone() - Share all exchange manager objects
2095*3a3b42bfSRobert Love  * @src: Source lport to clone exchange managers from
2096*3a3b42bfSRobert Love  * @dst: New lport that takes references to all the exchange managers
2097174e1ebfSChris Leech  */
2098174e1ebfSChris Leech int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
2099174e1ebfSChris Leech {
2100174e1ebfSChris Leech 	struct fc_exch_mgr_anchor *ema, *tmp;
2101174e1ebfSChris Leech 
2102174e1ebfSChris Leech 	list_for_each_entry(ema, &src->ema_list, ema_list) {
2103174e1ebfSChris Leech 		if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
2104174e1ebfSChris Leech 			goto err;
2105174e1ebfSChris Leech 	}
2106174e1ebfSChris Leech 	return 0;
2107174e1ebfSChris Leech err:
2108174e1ebfSChris Leech 	list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
2109174e1ebfSChris Leech 		fc_exch_mgr_del(ema);
2110174e1ebfSChris Leech 	return -ENOMEM;
2111174e1ebfSChris Leech }
2112174e1ebfSChris Leech 
2113*3a3b42bfSRobert Love /**
2114*3a3b42bfSRobert Love  * fc_exch_mgr_alloc() - Allocate an exchange manager
2115*3a3b42bfSRobert Love  * @lport:   The local port that the new EM will be associated with
2116*3a3b42bfSRobert Love  * @class:   The default FC class for new exchanges
2117*3a3b42bfSRobert Love  * @min_xid: The minimum XID for exchanges from the new EM
2118*3a3b42bfSRobert Love  * @max_xid: The maximum XID for exchanges from the new EM
2119*3a3b42bfSRobert Love  * @match:   The match routine for the new EM
2120*3a3b42bfSRobert Love  */
2121*3a3b42bfSRobert Love struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
212242e9a92fSRobert Love 				      enum fc_class class,
212352ff878cSVasu Dev 				      u16 min_xid, u16 max_xid,
212452ff878cSVasu Dev 				      bool (*match)(struct fc_frame *))
212542e9a92fSRobert Love {
212642e9a92fSRobert Love 	struct fc_exch_mgr *mp;
2127e4bc50beSVasu Dev 	u16 pool_exch_range;
2128e4bc50beSVasu Dev 	size_t pool_size;
2129e4bc50beSVasu Dev 	unsigned int cpu;
2130e4bc50beSVasu Dev 	struct fc_exch_pool *pool;
213142e9a92fSRobert Love 
2132e4bc50beSVasu Dev 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
2133e4bc50beSVasu Dev 	    (min_xid & fc_cpu_mask) != 0) {
2134*3a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
213542e9a92fSRobert Love 			     min_xid, max_xid);
213642e9a92fSRobert Love 		return NULL;
213742e9a92fSRobert Love 	}
213842e9a92fSRobert Love 
213942e9a92fSRobert Love 	/*
2140b2f0091fSVasu Dev 	 * allocate memory for EM
214142e9a92fSRobert Love 	 */
2142b2f0091fSVasu Dev 	mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
214342e9a92fSRobert Love 	if (!mp)
214442e9a92fSRobert Love 		return NULL;
214542e9a92fSRobert Love 
214642e9a92fSRobert Love 	mp->class = class;
214742e9a92fSRobert Love 	/* adjust em exch xid range for offload */
214842e9a92fSRobert Love 	mp->min_xid = min_xid;
214942e9a92fSRobert Love 	mp->max_xid = max_xid;
215042e9a92fSRobert Love 
215142e9a92fSRobert Love 	mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
215242e9a92fSRobert Love 	if (!mp->ep_pool)
215342e9a92fSRobert Love 		goto free_mp;
215442e9a92fSRobert Love 
2155e4bc50beSVasu Dev 	/*
2156e4bc50beSVasu Dev 	 * Setup per cpu exch pool with entire exchange id range equally
2157e4bc50beSVasu Dev 	 * divided across all cpus. The exch pointers array memory is
2158e4bc50beSVasu Dev 	 * allocated for exch range per pool.
2159e4bc50beSVasu Dev 	 */
2160e4bc50beSVasu Dev 	pool_exch_range = (mp->max_xid - mp->min_xid + 1) / (fc_cpu_mask + 1);
2161e4bc50beSVasu Dev 	mp->pool_max_index = pool_exch_range - 1;
2162e4bc50beSVasu Dev 
2163e4bc50beSVasu Dev 	/*
2164e4bc50beSVasu Dev 	 * Allocate and initialize per cpu exch pool
2165e4bc50beSVasu Dev 	 */
2166e4bc50beSVasu Dev 	pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
2167e4bc50beSVasu Dev 	mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
2168e4bc50beSVasu Dev 	if (!mp->pool)
2169e4bc50beSVasu Dev 		goto free_mempool;
2170e4bc50beSVasu Dev 	for_each_possible_cpu(cpu) {
2171e4bc50beSVasu Dev 		pool = per_cpu_ptr(mp->pool, cpu);
2172e4bc50beSVasu Dev 		spin_lock_init(&pool->lock);
2173e4bc50beSVasu Dev 		INIT_LIST_HEAD(&pool->ex_list);
2174e4bc50beSVasu Dev 	}
2175e4bc50beSVasu Dev 
217652ff878cSVasu Dev 	kref_init(&mp->kref);
2177*3a3b42bfSRobert Love 	if (!fc_exch_mgr_add(lport, mp, match)) {
2178e4bc50beSVasu Dev 		free_percpu(mp->pool);
2179e4bc50beSVasu Dev 		goto free_mempool;
218052ff878cSVasu Dev 	}
218152ff878cSVasu Dev 
218252ff878cSVasu Dev 	/*
218352ff878cSVasu Dev 	 * Above kref_init() sets mp->kref to 1 and then
218452ff878cSVasu Dev 	 * call to fc_exch_mgr_add incremented mp->kref again,
218552ff878cSVasu Dev 	 * so adjust that extra increment.
218652ff878cSVasu Dev 	 */
218752ff878cSVasu Dev 	kref_put(&mp->kref, fc_exch_mgr_destroy);
218842e9a92fSRobert Love 	return mp;
218942e9a92fSRobert Love 
2190e4bc50beSVasu Dev free_mempool:
2191e4bc50beSVasu Dev 	mempool_destroy(mp->ep_pool);
219242e9a92fSRobert Love free_mp:
219342e9a92fSRobert Love 	kfree(mp);
219442e9a92fSRobert Love 	return NULL;
219542e9a92fSRobert Love }
219642e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_alloc);
219742e9a92fSRobert Love 
2198*3a3b42bfSRobert Love /**
2199*3a3b42bfSRobert Love  * fc_exch_mgr_free() - Free all exchange managers on a local port
2200*3a3b42bfSRobert Love  * @lport: The local port whose EMs are to be freed
2201*3a3b42bfSRobert Love  */
220252ff878cSVasu Dev void fc_exch_mgr_free(struct fc_lport *lport)
220342e9a92fSRobert Love {
220452ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema, *next;
220552ff878cSVasu Dev 
220652ff878cSVasu Dev 	list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
220752ff878cSVasu Dev 		fc_exch_mgr_del(ema);
220842e9a92fSRobert Love }
220942e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_free);
221042e9a92fSRobert Love 
2211*3a3b42bfSRobert Love /**
2212*3a3b42bfSRobert Love  * fc_exch_recv() - Handler for received frames
2213*3a3b42bfSRobert Love  * @lport: The local port the frame was received on
2214*3a3b42bfSRobert Love  * @fp:	   The received frame
221542e9a92fSRobert Love  */
2216*3a3b42bfSRobert Love void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
221742e9a92fSRobert Love {
221842e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
221952ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
222052ff878cSVasu Dev 	u32 f_ctl, found = 0;
222152ff878cSVasu Dev 	u16 oxid;
222242e9a92fSRobert Love 
222342e9a92fSRobert Love 	/* lport lock ? */
2224*3a3b42bfSRobert Love 	if (!lport || lport->state == LPORT_ST_DISABLED) {
2225*3a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "Receiving frames for an lport that "
22267414705eSRobert Love 			     "has not been initialized correctly\n");
222742e9a92fSRobert Love 		fc_frame_free(fp);
222842e9a92fSRobert Love 		return;
222942e9a92fSRobert Love 	}
223042e9a92fSRobert Love 
223152ff878cSVasu Dev 	f_ctl = ntoh24(fh->fh_f_ctl);
223252ff878cSVasu Dev 	oxid = ntohs(fh->fh_ox_id);
223352ff878cSVasu Dev 	if (f_ctl & FC_FC_EX_CTX) {
2234*3a3b42bfSRobert Love 		list_for_each_entry(ema, &lport->ema_list, ema_list) {
223552ff878cSVasu Dev 			if ((oxid >= ema->mp->min_xid) &&
223652ff878cSVasu Dev 			    (oxid <= ema->mp->max_xid)) {
223752ff878cSVasu Dev 				found = 1;
223852ff878cSVasu Dev 				break;
223952ff878cSVasu Dev 			}
224052ff878cSVasu Dev 		}
224152ff878cSVasu Dev 
224252ff878cSVasu Dev 		if (!found) {
2243*3a3b42bfSRobert Love 			FC_LPORT_DBG(lport, "Received response for out "
224452ff878cSVasu Dev 				     "of range oxid:%hx\n", oxid);
224552ff878cSVasu Dev 			fc_frame_free(fp);
224652ff878cSVasu Dev 			return;
224752ff878cSVasu Dev 		}
224852ff878cSVasu Dev 	} else
2249*3a3b42bfSRobert Love 		ema = list_entry(lport->ema_list.prev, typeof(*ema), ema_list);
225052ff878cSVasu Dev 
225142e9a92fSRobert Love 	/*
225242e9a92fSRobert Love 	 * If frame is marked invalid, just drop it.
225342e9a92fSRobert Love 	 */
225442e9a92fSRobert Love 	switch (fr_eof(fp)) {
225542e9a92fSRobert Love 	case FC_EOF_T:
225642e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
225742e9a92fSRobert Love 			skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
225842e9a92fSRobert Love 		/* fall through */
225942e9a92fSRobert Love 	case FC_EOF_N:
226042e9a92fSRobert Love 		if (fh->fh_type == FC_TYPE_BLS)
226152ff878cSVasu Dev 			fc_exch_recv_bls(ema->mp, fp);
226242e9a92fSRobert Love 		else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
226342e9a92fSRobert Love 			 FC_FC_EX_CTX)
226452ff878cSVasu Dev 			fc_exch_recv_seq_resp(ema->mp, fp);
226542e9a92fSRobert Love 		else if (f_ctl & FC_FC_SEQ_CTX)
226652ff878cSVasu Dev 			fc_exch_recv_resp(ema->mp, fp);
226742e9a92fSRobert Love 		else
2268*3a3b42bfSRobert Love 			fc_exch_recv_req(lport, ema->mp, fp);
226942e9a92fSRobert Love 		break;
227042e9a92fSRobert Love 	default:
2271*3a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
2272*3a3b42bfSRobert Love 			     fr_eof(fp));
227342e9a92fSRobert Love 		fc_frame_free(fp);
227442e9a92fSRobert Love 	}
227542e9a92fSRobert Love }
227642e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_recv);
227742e9a92fSRobert Love 
2278*3a3b42bfSRobert Love /**
2279*3a3b42bfSRobert Love  * fc_exch_init() - Initialize the exchange layer for a local port
2280*3a3b42bfSRobert Love  * @lport: The local port to initialize the exchange layer for
2281*3a3b42bfSRobert Love  */
2282*3a3b42bfSRobert Love int fc_exch_init(struct fc_lport *lport)
228342e9a92fSRobert Love {
2284*3a3b42bfSRobert Love 	if (!lport->tt.seq_start_next)
2285*3a3b42bfSRobert Love 		lport->tt.seq_start_next = fc_seq_start_next;
228642e9a92fSRobert Love 
2287*3a3b42bfSRobert Love 	if (!lport->tt.exch_seq_send)
2288*3a3b42bfSRobert Love 		lport->tt.exch_seq_send = fc_exch_seq_send;
228942e9a92fSRobert Love 
2290*3a3b42bfSRobert Love 	if (!lport->tt.seq_send)
2291*3a3b42bfSRobert Love 		lport->tt.seq_send = fc_seq_send;
229242e9a92fSRobert Love 
2293*3a3b42bfSRobert Love 	if (!lport->tt.seq_els_rsp_send)
2294*3a3b42bfSRobert Love 		lport->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
229542e9a92fSRobert Love 
2296*3a3b42bfSRobert Love 	if (!lport->tt.exch_done)
2297*3a3b42bfSRobert Love 		lport->tt.exch_done = fc_exch_done;
229842e9a92fSRobert Love 
2299*3a3b42bfSRobert Love 	if (!lport->tt.exch_mgr_reset)
2300*3a3b42bfSRobert Love 		lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
230142e9a92fSRobert Love 
2302*3a3b42bfSRobert Love 	if (!lport->tt.seq_exch_abort)
2303*3a3b42bfSRobert Love 		lport->tt.seq_exch_abort = fc_seq_exch_abort;
230442e9a92fSRobert Love 
230589f19a59SVasu Dev 	return 0;
230689f19a59SVasu Dev }
230789f19a59SVasu Dev EXPORT_SYMBOL(fc_exch_init);
230889f19a59SVasu Dev 
230989f19a59SVasu Dev /**
231089f19a59SVasu Dev  * fc_setup_exch_mgr() - Setup an exchange manager
231189f19a59SVasu Dev  */
231289f19a59SVasu Dev int fc_setup_exch_mgr()
231389f19a59SVasu Dev {
231489f19a59SVasu Dev 	fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
231589f19a59SVasu Dev 					 0, SLAB_HWCACHE_ALIGN, NULL);
231689f19a59SVasu Dev 	if (!fc_em_cachep)
231789f19a59SVasu Dev 		return -ENOMEM;
231889f19a59SVasu Dev 
2319e4bc50beSVasu Dev 	/*
2320e4bc50beSVasu Dev 	 * Initialize fc_cpu_mask and fc_cpu_order. The
2321e4bc50beSVasu Dev 	 * fc_cpu_mask is set for nr_cpu_ids rounded up
2322e4bc50beSVasu Dev 	 * to order of 2's * power and order is stored
2323e4bc50beSVasu Dev 	 * in fc_cpu_order as this is later required in
2324e4bc50beSVasu Dev 	 * mapping between an exch id and exch array index
2325e4bc50beSVasu Dev 	 * in per cpu exch pool.
2326e4bc50beSVasu Dev 	 *
2327e4bc50beSVasu Dev 	 * This round up is required to align fc_cpu_mask
2328e4bc50beSVasu Dev 	 * to exchange id's lower bits such that all incoming
2329e4bc50beSVasu Dev 	 * frames of an exchange gets delivered to the same
2330e4bc50beSVasu Dev 	 * cpu on which exchange originated by simple bitwise
2331e4bc50beSVasu Dev 	 * AND operation between fc_cpu_mask and exchange id.
2332e4bc50beSVasu Dev 	 */
2333e4bc50beSVasu Dev 	fc_cpu_mask = 1;
2334e4bc50beSVasu Dev 	fc_cpu_order = 0;
2335e4bc50beSVasu Dev 	while (fc_cpu_mask < nr_cpu_ids) {
2336e4bc50beSVasu Dev 		fc_cpu_mask <<= 1;
2337e4bc50beSVasu Dev 		fc_cpu_order++;
2338e4bc50beSVasu Dev 	}
2339e4bc50beSVasu Dev 	fc_cpu_mask--;
2340e4bc50beSVasu Dev 
234142e9a92fSRobert Love 	return 0;
234242e9a92fSRobert Love }
234342e9a92fSRobert Love 
2344*3a3b42bfSRobert Love /**
2345*3a3b42bfSRobert Love  * fc_destroy_exch_mgr() - Destroy an exchange manager
2346*3a3b42bfSRobert Love  */
2347*3a3b42bfSRobert Love void fc_destroy_exch_mgr()
234842e9a92fSRobert Love {
234942e9a92fSRobert Love 	kmem_cache_destroy(fc_em_cachep);
235042e9a92fSRobert Love }
2351