xref: /linux/drivers/scsi/libfc/fc_exch.c (revision 7b2787ec15b9d1c2f716da61b0eec21a3f5e6520)
142e9a92fSRobert Love /*
242e9a92fSRobert Love  * Copyright(c) 2007 Intel Corporation. All rights reserved.
342e9a92fSRobert Love  * Copyright(c) 2008 Red Hat, Inc.  All rights reserved.
442e9a92fSRobert Love  * Copyright(c) 2008 Mike Christie
542e9a92fSRobert Love  *
642e9a92fSRobert Love  * This program is free software; you can redistribute it and/or modify it
742e9a92fSRobert Love  * under the terms and conditions of the GNU General Public License,
842e9a92fSRobert Love  * version 2, as published by the Free Software Foundation.
942e9a92fSRobert Love  *
1042e9a92fSRobert Love  * This program is distributed in the hope it will be useful, but WITHOUT
1142e9a92fSRobert Love  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1242e9a92fSRobert Love  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1342e9a92fSRobert Love  * more details.
1442e9a92fSRobert Love  *
1542e9a92fSRobert Love  * You should have received a copy of the GNU General Public License along with
1642e9a92fSRobert Love  * this program; if not, write to the Free Software Foundation, Inc.,
1742e9a92fSRobert Love  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
1842e9a92fSRobert Love  *
1942e9a92fSRobert Love  * Maintained at www.Open-FCoE.org
2042e9a92fSRobert Love  */
2142e9a92fSRobert Love 
2242e9a92fSRobert Love /*
2342e9a92fSRobert Love  * Fibre Channel exchange and sequence handling.
2442e9a92fSRobert Love  */
2542e9a92fSRobert Love 
2642e9a92fSRobert Love #include <linux/timer.h>
275a0e3ad6STejun Heo #include <linux/slab.h>
2842e9a92fSRobert Love #include <linux/err.h>
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 */
414ae1e19fSVasu Dev struct workqueue_struct *fc_exch_workqueue;
4242e9a92fSRobert Love 
4342e9a92fSRobert Love /*
4442e9a92fSRobert Love  * Structure and function definitions for managing Fibre Channel Exchanges
4542e9a92fSRobert Love  * and Sequences.
4642e9a92fSRobert Love  *
4742e9a92fSRobert Love  * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
4842e9a92fSRobert Love  *
4942e9a92fSRobert Love  * fc_exch_mgr holds the exchange state for an N port
5042e9a92fSRobert Love  *
5142e9a92fSRobert Love  * fc_exch holds state for one exchange and links to its active sequence.
5242e9a92fSRobert Love  *
5342e9a92fSRobert Love  * fc_seq holds the state for an individual sequence.
5442e9a92fSRobert Love  */
5542e9a92fSRobert Love 
563a3b42bfSRobert Love /**
573a3b42bfSRobert Love  * struct fc_exch_pool - Per cpu exchange pool
583a3b42bfSRobert Love  * @next_index:	  Next possible free exchange index
593a3b42bfSRobert Love  * @total_exches: Total allocated exchanges
603a3b42bfSRobert Love  * @lock:	  Exch pool lock
613a3b42bfSRobert Love  * @ex_list:	  List of exchanges
62e4bc50beSVasu Dev  *
63e4bc50beSVasu Dev  * This structure manages per cpu exchanges in array of exchange pointers.
64e4bc50beSVasu Dev  * This array is allocated followed by struct fc_exch_pool memory for
65e4bc50beSVasu Dev  * assigned range of exchanges to per cpu pool.
66e4bc50beSVasu Dev  */
67e4bc50beSVasu Dev struct fc_exch_pool {
683a3b42bfSRobert Love 	u16		 next_index;
693a3b42bfSRobert Love 	u16		 total_exches;
703a3b42bfSRobert Love 	spinlock_t	 lock;
713a3b42bfSRobert Love 	struct list_head ex_list;
72e4bc50beSVasu Dev };
73e4bc50beSVasu Dev 
743a3b42bfSRobert Love /**
753a3b42bfSRobert Love  * struct fc_exch_mgr - The Exchange Manager (EM).
763a3b42bfSRobert Love  * @class:	    Default class for new sequences
773a3b42bfSRobert Love  * @kref:	    Reference counter
783a3b42bfSRobert Love  * @min_xid:	    Minimum exchange ID
793a3b42bfSRobert Love  * @max_xid:	    Maximum exchange ID
803a3b42bfSRobert Love  * @ep_pool:	    Reserved exchange pointers
813a3b42bfSRobert Love  * @pool_max_index: Max exch array index in exch pool
823a3b42bfSRobert Love  * @pool:	    Per cpu exch pool
833a3b42bfSRobert Love  * @stats:	    Statistics structure
8442e9a92fSRobert Love  *
8542e9a92fSRobert Love  * This structure is the center for creating exchanges and sequences.
8642e9a92fSRobert Love  * It manages the allocation of exchange IDs.
8742e9a92fSRobert Love  */
8842e9a92fSRobert Love struct fc_exch_mgr {
893a3b42bfSRobert Love 	enum fc_class	class;
903a3b42bfSRobert Love 	struct kref	kref;
913a3b42bfSRobert Love 	u16		min_xid;
923a3b42bfSRobert Love 	u16		max_xid;
933a3b42bfSRobert Love 	mempool_t	*ep_pool;
943a3b42bfSRobert Love 	u16		pool_max_index;
953a3b42bfSRobert Love 	struct fc_exch_pool *pool;
9642e9a92fSRobert Love 
9742e9a92fSRobert Love 	/*
9842e9a92fSRobert Love 	 * currently exchange mgr stats are updated but not used.
9942e9a92fSRobert Love 	 * either stats can be expose via sysfs or remove them
10042e9a92fSRobert Love 	 * all together if not used XXX
10142e9a92fSRobert Love 	 */
10242e9a92fSRobert Love 	struct {
10342e9a92fSRobert Love 		atomic_t no_free_exch;
10442e9a92fSRobert Love 		atomic_t no_free_exch_xid;
10542e9a92fSRobert Love 		atomic_t xid_not_found;
10642e9a92fSRobert Love 		atomic_t xid_busy;
10742e9a92fSRobert Love 		atomic_t seq_not_found;
10842e9a92fSRobert Love 		atomic_t non_bls_resp;
10942e9a92fSRobert Love 	} stats;
11042e9a92fSRobert Love };
11142e9a92fSRobert Love #define	fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
11242e9a92fSRobert Love 
1133a3b42bfSRobert Love /**
1143a3b42bfSRobert Love  * struct fc_exch_mgr_anchor - primary structure for list of EMs
1153a3b42bfSRobert Love  * @ema_list: Exchange Manager Anchor list
1163a3b42bfSRobert Love  * @mp:	      Exchange Manager associated with this anchor
1173a3b42bfSRobert Love  * @match:    Routine to determine if this anchor's EM should be used
1183a3b42bfSRobert Love  *
1193a3b42bfSRobert Love  * When walking the list of anchors the match routine will be called
1203a3b42bfSRobert Love  * for each anchor to determine if that EM should be used. The last
1213a3b42bfSRobert Love  * anchor in the list will always match to handle any exchanges not
1223a3b42bfSRobert Love  * handled by other EMs. The non-default EMs would be added to the
1233a3b42bfSRobert Love  * anchor list by HW that provides FCoE offloads.
1243a3b42bfSRobert Love  */
12596316099SVasu Dev struct fc_exch_mgr_anchor {
12696316099SVasu Dev 	struct list_head ema_list;
12796316099SVasu Dev 	struct fc_exch_mgr *mp;
12896316099SVasu Dev 	bool (*match)(struct fc_frame *);
12996316099SVasu Dev };
13096316099SVasu Dev 
13142e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *);
13242e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *);
13342e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason,
13442e9a92fSRobert Love 			  enum fc_els_rjt_explan);
13542e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *);
13642e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *);
13742e9a92fSRobert Love 
13842e9a92fSRobert Love /*
13942e9a92fSRobert Love  * Internal implementation notes.
14042e9a92fSRobert Love  *
14142e9a92fSRobert Love  * The exchange manager is one by default in libfc but LLD may choose
14242e9a92fSRobert Love  * to have one per CPU. The sequence manager is one per exchange manager
14342e9a92fSRobert Love  * and currently never separated.
14442e9a92fSRobert Love  *
14542e9a92fSRobert Love  * Section 9.8 in FC-FS-2 specifies:  "The SEQ_ID is a one-byte field
14642e9a92fSRobert Love  * assigned by the Sequence Initiator that shall be unique for a specific
14742e9a92fSRobert Love  * D_ID and S_ID pair while the Sequence is open."   Note that it isn't
14842e9a92fSRobert Love  * qualified by exchange ID, which one might think it would be.
14942e9a92fSRobert Love  * In practice this limits the number of open sequences and exchanges to 256
15042e9a92fSRobert Love  * per session.	 For most targets we could treat this limit as per exchange.
15142e9a92fSRobert Love  *
15242e9a92fSRobert Love  * The exchange and its sequence are freed when the last sequence is received.
15342e9a92fSRobert Love  * It's possible for the remote port to leave an exchange open without
15442e9a92fSRobert Love  * sending any sequences.
15542e9a92fSRobert Love  *
15642e9a92fSRobert Love  * Notes on reference counts:
15742e9a92fSRobert Love  *
15842e9a92fSRobert Love  * Exchanges are reference counted and exchange gets freed when the reference
15942e9a92fSRobert Love  * count becomes zero.
16042e9a92fSRobert Love  *
16142e9a92fSRobert Love  * Timeouts:
16242e9a92fSRobert Love  * Sequences are timed out for E_D_TOV and R_A_TOV.
16342e9a92fSRobert Love  *
16442e9a92fSRobert Love  * Sequence event handling:
16542e9a92fSRobert Love  *
16642e9a92fSRobert Love  * The following events may occur on initiator sequences:
16742e9a92fSRobert Love  *
16842e9a92fSRobert Love  *	Send.
16942e9a92fSRobert Love  *	    For now, the whole thing is sent.
17042e9a92fSRobert Love  *	Receive ACK
17142e9a92fSRobert Love  *	    This applies only to class F.
17242e9a92fSRobert Love  *	    The sequence is marked complete.
17342e9a92fSRobert Love  *	ULP completion.
17442e9a92fSRobert Love  *	    The upper layer calls fc_exch_done() when done
17542e9a92fSRobert Love  *	    with exchange and sequence tuple.
17642e9a92fSRobert Love  *	RX-inferred completion.
17742e9a92fSRobert Love  *	    When we receive the next sequence on the same exchange, we can
17842e9a92fSRobert Love  *	    retire the previous sequence ID.  (XXX not implemented).
17942e9a92fSRobert Love  *	Timeout.
18042e9a92fSRobert Love  *	    R_A_TOV frees the sequence ID.  If we're waiting for ACK,
18142e9a92fSRobert Love  *	    E_D_TOV causes abort and calls upper layer response handler
18242e9a92fSRobert Love  *	    with FC_EX_TIMEOUT error.
18342e9a92fSRobert Love  *	Receive RJT
18442e9a92fSRobert Love  *	    XXX defer.
18542e9a92fSRobert Love  *	Send ABTS
18642e9a92fSRobert Love  *	    On timeout.
18742e9a92fSRobert Love  *
18842e9a92fSRobert Love  * The following events may occur on recipient sequences:
18942e9a92fSRobert Love  *
19042e9a92fSRobert Love  *	Receive
19142e9a92fSRobert Love  *	    Allocate sequence for first frame received.
19242e9a92fSRobert Love  *	    Hold during receive handler.
19342e9a92fSRobert Love  *	    Release when final frame received.
19442e9a92fSRobert Love  *	    Keep status of last N of these for the ELS RES command.  XXX TBD.
19542e9a92fSRobert Love  *	Receive ABTS
19642e9a92fSRobert Love  *	    Deallocate sequence
19742e9a92fSRobert Love  *	Send RJT
19842e9a92fSRobert Love  *	    Deallocate
19942e9a92fSRobert Love  *
20042e9a92fSRobert Love  * For now, we neglect conditions where only part of a sequence was
20142e9a92fSRobert Love  * received or transmitted, or where out-of-order receipt is detected.
20242e9a92fSRobert Love  */
20342e9a92fSRobert Love 
20442e9a92fSRobert Love /*
20542e9a92fSRobert Love  * Locking notes:
20642e9a92fSRobert Love  *
20742e9a92fSRobert Love  * The EM code run in a per-CPU worker thread.
20842e9a92fSRobert Love  *
20942e9a92fSRobert Love  * To protect against concurrency between a worker thread code and timers,
21042e9a92fSRobert Love  * sequence allocation and deallocation must be locked.
21142e9a92fSRobert Love  *  - exchange refcnt can be done atomicly without locks.
21242e9a92fSRobert Love  *  - sequence allocation must be locked by exch lock.
213b2f0091fSVasu Dev  *  - If the EM pool lock and ex_lock must be taken at the same time, then the
214b2f0091fSVasu Dev  *    EM pool lock must be taken before the ex_lock.
21542e9a92fSRobert Love  */
21642e9a92fSRobert Love 
21742e9a92fSRobert Love /*
21842e9a92fSRobert Love  * opcode names for debugging.
21942e9a92fSRobert Love  */
22042e9a92fSRobert Love static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
22142e9a92fSRobert Love 
22242e9a92fSRobert Love #define FC_TABLE_SIZE(x)   (sizeof(x) / sizeof(x[0]))
22342e9a92fSRobert Love 
2243a3b42bfSRobert Love /**
2253a3b42bfSRobert Love  * fc_exch_name_lookup() - Lookup name by opcode
2263a3b42bfSRobert Love  * @op:	       Opcode to be looked up
2273a3b42bfSRobert Love  * @table:     Opcode/name table
2283a3b42bfSRobert Love  * @max_index: Index not to be exceeded
2293a3b42bfSRobert Love  *
2303a3b42bfSRobert Love  * This routine is used to determine a human-readable string identifying
2313a3b42bfSRobert Love  * a R_CTL opcode.
2323a3b42bfSRobert Love  */
23342e9a92fSRobert Love static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
23442e9a92fSRobert Love 					      unsigned int max_index)
23542e9a92fSRobert Love {
23642e9a92fSRobert Love 	const char *name = NULL;
23742e9a92fSRobert Love 
23842e9a92fSRobert Love 	if (op < max_index)
23942e9a92fSRobert Love 		name = table[op];
24042e9a92fSRobert Love 	if (!name)
24142e9a92fSRobert Love 		name = "unknown";
24242e9a92fSRobert Love 	return name;
24342e9a92fSRobert Love }
24442e9a92fSRobert Love 
2453a3b42bfSRobert Love /**
2463a3b42bfSRobert Love  * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
2473a3b42bfSRobert Love  * @op: The opcode to be looked up
2483a3b42bfSRobert Love  */
24942e9a92fSRobert Love static const char *fc_exch_rctl_name(unsigned int op)
25042e9a92fSRobert Love {
25142e9a92fSRobert Love 	return fc_exch_name_lookup(op, fc_exch_rctl_names,
25242e9a92fSRobert Love 				   FC_TABLE_SIZE(fc_exch_rctl_names));
25342e9a92fSRobert Love }
25442e9a92fSRobert Love 
2553a3b42bfSRobert Love /**
2563a3b42bfSRobert Love  * fc_exch_hold() - Increment an exchange's reference count
2573a3b42bfSRobert Love  * @ep: Echange to be held
25842e9a92fSRobert Love  */
2593a3b42bfSRobert Love static inline void fc_exch_hold(struct fc_exch *ep)
26042e9a92fSRobert Love {
26142e9a92fSRobert Love 	atomic_inc(&ep->ex_refcnt);
26242e9a92fSRobert Love }
26342e9a92fSRobert Love 
2643a3b42bfSRobert Love /**
2653a3b42bfSRobert Love  * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
2663a3b42bfSRobert Love  *			 and determine SOF and EOF.
2673a3b42bfSRobert Love  * @ep:	   The exchange to that will use the header
2683a3b42bfSRobert Love  * @fp:	   The frame whose header is to be modified
2693a3b42bfSRobert Love  * @f_ctl: F_CTL bits that will be used for the frame header
2703a3b42bfSRobert Love  *
2713a3b42bfSRobert Love  * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
2723a3b42bfSRobert Love  * fh_seq_id, fh_seq_cnt and the SOF and EOF.
27342e9a92fSRobert Love  */
27442e9a92fSRobert Love static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
27542e9a92fSRobert Love 			      u32 f_ctl)
27642e9a92fSRobert Love {
27742e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
27842e9a92fSRobert Love 	u16 fill;
27942e9a92fSRobert Love 
28042e9a92fSRobert Love 	fr_sof(fp) = ep->class;
28142e9a92fSRobert Love 	if (ep->seq.cnt)
28242e9a92fSRobert Love 		fr_sof(fp) = fc_sof_normal(ep->class);
28342e9a92fSRobert Love 
28442e9a92fSRobert Love 	if (f_ctl & FC_FC_END_SEQ) {
28542e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_T;
28642e9a92fSRobert Love 		if (fc_sof_needs_ack(ep->class))
28742e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
28842e9a92fSRobert Love 		/*
2893a3b42bfSRobert Love 		 * From F_CTL.
29042e9a92fSRobert Love 		 * The number of fill bytes to make the length a 4-byte
29142e9a92fSRobert Love 		 * multiple is the low order 2-bits of the f_ctl.
29242e9a92fSRobert Love 		 * The fill itself will have been cleared by the frame
29342e9a92fSRobert Love 		 * allocation.
29442e9a92fSRobert Love 		 * After this, the length will be even, as expected by
29542e9a92fSRobert Love 		 * the transport.
29642e9a92fSRobert Love 		 */
29742e9a92fSRobert Love 		fill = fr_len(fp) & 3;
29842e9a92fSRobert Love 		if (fill) {
29942e9a92fSRobert Love 			fill = 4 - fill;
30042e9a92fSRobert Love 			/* TODO, this may be a problem with fragmented skb */
30142e9a92fSRobert Love 			skb_put(fp_skb(fp), fill);
30242e9a92fSRobert Love 			hton24(fh->fh_f_ctl, f_ctl | fill);
30342e9a92fSRobert Love 		}
30442e9a92fSRobert Love 	} else {
30542e9a92fSRobert Love 		WARN_ON(fr_len(fp) % 4 != 0);	/* no pad to non last frame */
30642e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
30742e9a92fSRobert Love 	}
30842e9a92fSRobert Love 
30942e9a92fSRobert Love 	/*
31042e9a92fSRobert Love 	 * Initialize remainig fh fields
31142e9a92fSRobert Love 	 * from fc_fill_fc_hdr
31242e9a92fSRobert Love 	 */
31342e9a92fSRobert Love 	fh->fh_ox_id = htons(ep->oxid);
31442e9a92fSRobert Love 	fh->fh_rx_id = htons(ep->rxid);
31542e9a92fSRobert Love 	fh->fh_seq_id = ep->seq.id;
31642e9a92fSRobert Love 	fh->fh_seq_cnt = htons(ep->seq.cnt);
31742e9a92fSRobert Love }
31842e9a92fSRobert Love 
3193a3b42bfSRobert Love /**
3203a3b42bfSRobert Love  * fc_exch_release() - Decrement an exchange's reference count
3213a3b42bfSRobert Love  * @ep: Exchange to be released
3223a3b42bfSRobert Love  *
3233a3b42bfSRobert Love  * If the reference count reaches zero and the exchange is complete,
3243a3b42bfSRobert Love  * it is freed.
32542e9a92fSRobert Love  */
32642e9a92fSRobert Love static void fc_exch_release(struct fc_exch *ep)
32742e9a92fSRobert Love {
32842e9a92fSRobert Love 	struct fc_exch_mgr *mp;
32942e9a92fSRobert Love 
33042e9a92fSRobert Love 	if (atomic_dec_and_test(&ep->ex_refcnt)) {
33142e9a92fSRobert Love 		mp = ep->em;
33242e9a92fSRobert Love 		if (ep->destructor)
33342e9a92fSRobert Love 			ep->destructor(&ep->seq, ep->arg);
334aa6cd29bSJulia Lawall 		WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
33542e9a92fSRobert Love 		mempool_free(ep, mp->ep_pool);
33642e9a92fSRobert Love 	}
33742e9a92fSRobert Love }
33842e9a92fSRobert Love 
3393a3b42bfSRobert Love /**
3403a3b42bfSRobert Love  * fc_exch_done_locked() - Complete an exchange with the exchange lock held
3413a3b42bfSRobert Love  * @ep: The exchange that is complete
3423a3b42bfSRobert Love  */
34342e9a92fSRobert Love static int fc_exch_done_locked(struct fc_exch *ep)
34442e9a92fSRobert Love {
34542e9a92fSRobert Love 	int rc = 1;
34642e9a92fSRobert Love 
34742e9a92fSRobert Love 	/*
34842e9a92fSRobert Love 	 * We must check for completion in case there are two threads
34942e9a92fSRobert Love 	 * tyring to complete this. But the rrq code will reuse the
35042e9a92fSRobert Love 	 * ep, and in that case we only clear the resp and set it as
35142e9a92fSRobert Love 	 * complete, so it can be reused by the timer to send the rrq.
35242e9a92fSRobert Love 	 */
35342e9a92fSRobert Love 	ep->resp = NULL;
35442e9a92fSRobert Love 	if (ep->state & FC_EX_DONE)
35542e9a92fSRobert Love 		return rc;
35642e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_COMPLETE;
35742e9a92fSRobert Love 
35842e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
35942e9a92fSRobert Love 		ep->state |= FC_EX_DONE;
36042e9a92fSRobert Love 		if (cancel_delayed_work(&ep->timeout_work))
36142e9a92fSRobert Love 			atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
36242e9a92fSRobert Love 		rc = 0;
36342e9a92fSRobert Love 	}
36442e9a92fSRobert Love 	return rc;
36542e9a92fSRobert Love }
36642e9a92fSRobert Love 
3673a3b42bfSRobert Love /**
3683a3b42bfSRobert Love  * fc_exch_ptr_get() - Return an exchange from an exchange pool
3693a3b42bfSRobert Love  * @pool:  Exchange Pool to get an exchange from
3703a3b42bfSRobert Love  * @index: Index of the exchange within the pool
3713a3b42bfSRobert Love  *
3723a3b42bfSRobert Love  * Use the index to get an exchange from within an exchange pool. exches
3733a3b42bfSRobert Love  * will point to an array of exchange pointers. The index will select
3743a3b42bfSRobert Love  * the exchange within the array.
3753a3b42bfSRobert Love  */
376e4bc50beSVasu Dev static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
377e4bc50beSVasu Dev 					      u16 index)
378e4bc50beSVasu Dev {
379e4bc50beSVasu Dev 	struct fc_exch **exches = (struct fc_exch **)(pool + 1);
380e4bc50beSVasu Dev 	return exches[index];
381e4bc50beSVasu Dev }
382e4bc50beSVasu Dev 
3833a3b42bfSRobert Love /**
3843a3b42bfSRobert Love  * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
3853a3b42bfSRobert Love  * @pool:  The pool to assign the exchange to
3863a3b42bfSRobert Love  * @index: The index in the pool where the exchange will be assigned
3873a3b42bfSRobert Love  * @ep:	   The exchange to assign to the pool
3883a3b42bfSRobert Love  */
389e4bc50beSVasu Dev static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
390e4bc50beSVasu Dev 				   struct fc_exch *ep)
391e4bc50beSVasu Dev {
392e4bc50beSVasu Dev 	((struct fc_exch **)(pool + 1))[index] = ep;
393e4bc50beSVasu Dev }
394e4bc50beSVasu Dev 
3953a3b42bfSRobert Love /**
3963a3b42bfSRobert Love  * fc_exch_delete() - Delete an exchange
3973a3b42bfSRobert Love  * @ep: The exchange to be deleted
3983a3b42bfSRobert Love  */
399b2f0091fSVasu Dev static void fc_exch_delete(struct fc_exch *ep)
40042e9a92fSRobert Love {
401b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
40242e9a92fSRobert Love 
403b2f0091fSVasu Dev 	pool = ep->pool;
404b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
405b2f0091fSVasu Dev 	WARN_ON(pool->total_exches <= 0);
406b2f0091fSVasu Dev 	pool->total_exches--;
407b2f0091fSVasu Dev 	fc_exch_ptr_set(pool, (ep->xid - ep->em->min_xid) >> fc_cpu_order,
408b2f0091fSVasu Dev 			NULL);
40942e9a92fSRobert Love 	list_del(&ep->ex_list);
410b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
41142e9a92fSRobert Love 	fc_exch_release(ep);	/* drop hold for exch in mp */
41242e9a92fSRobert Love }
41342e9a92fSRobert Love 
4143a3b42bfSRobert Love /**
4153a3b42bfSRobert Love  * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
4163a3b42bfSRobert Love  *				the exchange lock held
4173a3b42bfSRobert Love  * @ep:		The exchange whose timer will start
4183a3b42bfSRobert Love  * @timer_msec: The timeout period
4193a3b42bfSRobert Love  *
4203a3b42bfSRobert Love  * Used for upper level protocols to time out the exchange.
4213a3b42bfSRobert Love  * The timer is cancelled when it fires or when the exchange completes.
42242e9a92fSRobert Love  */
42342e9a92fSRobert Love static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
42442e9a92fSRobert Love 					    unsigned int timer_msec)
42542e9a92fSRobert Love {
42642e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
42742e9a92fSRobert Love 		return;
42842e9a92fSRobert Love 
429cd305ce4SRobert Love 	FC_EXCH_DBG(ep, "Exchange timer armed\n");
4307414705eSRobert Love 
4314ae1e19fSVasu Dev 	if (queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
43242e9a92fSRobert Love 			       msecs_to_jiffies(timer_msec)))
43342e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for timer */
43442e9a92fSRobert Love }
43542e9a92fSRobert Love 
4363a3b42bfSRobert Love /**
4373a3b42bfSRobert Love  * fc_exch_timer_set() - Lock the exchange and set the timer
4383a3b42bfSRobert Love  * @ep:		The exchange whose timer will start
4393a3b42bfSRobert Love  * @timer_msec: The timeout period
44042e9a92fSRobert Love  */
44142e9a92fSRobert Love static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
44242e9a92fSRobert Love {
44342e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
44442e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, timer_msec);
44542e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
44642e9a92fSRobert Love }
44742e9a92fSRobert Love 
4481a7b75aeSRobert Love /**
4493a3b42bfSRobert Love  * fc_seq_send() - Send a frame using existing sequence/exchange pair
4503a3b42bfSRobert Love  * @lport: The local port that the exchange will be sent on
4513a3b42bfSRobert Love  * @sp:	   The sequence to be sent
4523a3b42bfSRobert Love  * @fp:	   The frame to be sent on the exchange
4531a7b75aeSRobert Love  */
4543a3b42bfSRobert Love static int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp,
4551a7b75aeSRobert Love 		       struct fc_frame *fp)
4561a7b75aeSRobert Love {
4571a7b75aeSRobert Love 	struct fc_exch *ep;
4581a7b75aeSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
4591a7b75aeSRobert Love 	int error;
4601a7b75aeSRobert Love 	u32 f_ctl;
4611a7b75aeSRobert Love 
4621a7b75aeSRobert Love 	ep = fc_seq_exch(sp);
4631a7b75aeSRobert Love 	WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
4641a7b75aeSRobert Love 
4651a7b75aeSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
4661a7b75aeSRobert Love 	fc_exch_setup_hdr(ep, fp, f_ctl);
4671a7b75aeSRobert Love 
4681a7b75aeSRobert Love 	/*
4691a7b75aeSRobert Love 	 * update sequence count if this frame is carrying
4701a7b75aeSRobert Love 	 * multiple FC frames when sequence offload is enabled
4711a7b75aeSRobert Love 	 * by LLD.
4721a7b75aeSRobert Love 	 */
4731a7b75aeSRobert Love 	if (fr_max_payload(fp))
4741a7b75aeSRobert Love 		sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
4751a7b75aeSRobert Love 					fr_max_payload(fp));
4761a7b75aeSRobert Love 	else
4771a7b75aeSRobert Love 		sp->cnt++;
4781a7b75aeSRobert Love 
4791a7b75aeSRobert Love 	/*
4801a7b75aeSRobert Love 	 * Send the frame.
4811a7b75aeSRobert Love 	 */
4823a3b42bfSRobert Love 	error = lport->tt.frame_send(lport, fp);
4831a7b75aeSRobert Love 
4841a7b75aeSRobert Love 	/*
4851a7b75aeSRobert Love 	 * Update the exchange and sequence flags,
4861a7b75aeSRobert Love 	 * assuming all frames for the sequence have been sent.
4871a7b75aeSRobert Love 	 * We can only be called to send once for each sequence.
4881a7b75aeSRobert Love 	 */
4891a7b75aeSRobert Love 	spin_lock_bh(&ep->ex_lock);
4901a7b75aeSRobert Love 	ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ;	/* not first seq */
491cc3593d3SJoe Eykholt 	if (f_ctl & FC_FC_SEQ_INIT)
4921a7b75aeSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
4931a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
4941a7b75aeSRobert Love 	return error;
4951a7b75aeSRobert Love }
4961a7b75aeSRobert Love 
4971a7b75aeSRobert Love /**
4983a3b42bfSRobert Love  * fc_seq_alloc() - Allocate a sequence for a given exchange
4993a3b42bfSRobert Love  * @ep:	    The exchange to allocate a new sequence for
5003a3b42bfSRobert Love  * @seq_id: The sequence ID to be used
5011a7b75aeSRobert Love  *
5021a7b75aeSRobert Love  * We don't support multiple originated sequences on the same exchange.
5031a7b75aeSRobert Love  * By implication, any previously originated sequence on this exchange
5041a7b75aeSRobert Love  * is complete, and we reallocate the same sequence.
5051a7b75aeSRobert Love  */
5061a7b75aeSRobert Love static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
5071a7b75aeSRobert Love {
5081a7b75aeSRobert Love 	struct fc_seq *sp;
5091a7b75aeSRobert Love 
5101a7b75aeSRobert Love 	sp = &ep->seq;
5111a7b75aeSRobert Love 	sp->ssb_stat = 0;
5121a7b75aeSRobert Love 	sp->cnt = 0;
5131a7b75aeSRobert Love 	sp->id = seq_id;
5141a7b75aeSRobert Love 	return sp;
5151a7b75aeSRobert Love }
5161a7b75aeSRobert Love 
5173a3b42bfSRobert Love /**
5183a3b42bfSRobert Love  * fc_seq_start_next_locked() - Allocate a new sequence on the same
5193a3b42bfSRobert Love  *				exchange as the supplied sequence
5203a3b42bfSRobert Love  * @sp: The sequence/exchange to get a new sequence for
5213a3b42bfSRobert Love  */
5221a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
5231a7b75aeSRobert Love {
5241a7b75aeSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
5251a7b75aeSRobert Love 
5261a7b75aeSRobert Love 	sp = fc_seq_alloc(ep, ep->seq_id++);
5271a7b75aeSRobert Love 	FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
5281a7b75aeSRobert Love 		    ep->f_ctl, sp->id);
5291a7b75aeSRobert Love 	return sp;
5301a7b75aeSRobert Love }
5311a7b75aeSRobert Love 
5321a7b75aeSRobert Love /**
5333a3b42bfSRobert Love  * fc_seq_start_next() - Lock the exchange and get a new sequence
5343a3b42bfSRobert Love  *			 for a given sequence/exchange pair
5353a3b42bfSRobert Love  * @sp: The sequence/exchange to get a new exchange for
5361a7b75aeSRobert Love  */
5371a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
5381a7b75aeSRobert Love {
5391a7b75aeSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
5401a7b75aeSRobert Love 
5411a7b75aeSRobert Love 	spin_lock_bh(&ep->ex_lock);
5421a7b75aeSRobert Love 	sp = fc_seq_start_next_locked(sp);
5431a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
5441a7b75aeSRobert Love 
5451a7b75aeSRobert Love 	return sp;
5461a7b75aeSRobert Love }
5471a7b75aeSRobert Love 
5481a7b75aeSRobert Love /**
5493a3b42bfSRobert Love  * fc_seq_exch_abort() - Abort an exchange and sequence
5503a3b42bfSRobert Love  * @req_sp:	The sequence to be aborted
5513a3b42bfSRobert Love  * @timer_msec: The period of time to wait before aborting
5523a3b42bfSRobert Love  *
5533a3b42bfSRobert Love  * Generally called because of a timeout or an abort from the upper layer.
5541a7b75aeSRobert Love  */
5551a7b75aeSRobert Love static int fc_seq_exch_abort(const struct fc_seq *req_sp,
5561a7b75aeSRobert Love 			     unsigned int timer_msec)
55742e9a92fSRobert Love {
55842e9a92fSRobert Love 	struct fc_seq *sp;
55942e9a92fSRobert Love 	struct fc_exch *ep;
56042e9a92fSRobert Love 	struct fc_frame *fp;
56142e9a92fSRobert Love 	int error;
56242e9a92fSRobert Love 
56342e9a92fSRobert Love 	ep = fc_seq_exch(req_sp);
56442e9a92fSRobert Love 
56542e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
56642e9a92fSRobert Love 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
56742e9a92fSRobert Love 	    ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
56842e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
56942e9a92fSRobert Love 		return -ENXIO;
57042e9a92fSRobert Love 	}
57142e9a92fSRobert Love 
57242e9a92fSRobert Love 	/*
57342e9a92fSRobert Love 	 * Send the abort on a new sequence if possible.
57442e9a92fSRobert Love 	 */
57542e9a92fSRobert Love 	sp = fc_seq_start_next_locked(&ep->seq);
57642e9a92fSRobert Love 	if (!sp) {
57742e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
57842e9a92fSRobert Love 		return -ENOMEM;
57942e9a92fSRobert Love 	}
58042e9a92fSRobert Love 
58142e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
58242e9a92fSRobert Love 	if (timer_msec)
58342e9a92fSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
58442e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
58542e9a92fSRobert Love 
58642e9a92fSRobert Love 	/*
58742e9a92fSRobert Love 	 * If not logged into the fabric, don't send ABTS but leave
58842e9a92fSRobert Love 	 * sequence active until next timeout.
58942e9a92fSRobert Love 	 */
59042e9a92fSRobert Love 	if (!ep->sid)
59142e9a92fSRobert Love 		return 0;
59242e9a92fSRobert Love 
59342e9a92fSRobert Love 	/*
59442e9a92fSRobert Love 	 * Send an abort for the sequence that timed out.
59542e9a92fSRobert Love 	 */
59642e9a92fSRobert Love 	fp = fc_frame_alloc(ep->lp, 0);
59742e9a92fSRobert Love 	if (fp) {
59842e9a92fSRobert Love 		fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
59942e9a92fSRobert Love 			       FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
60042e9a92fSRobert Love 		error = fc_seq_send(ep->lp, sp, fp);
60142e9a92fSRobert Love 	} else
60242e9a92fSRobert Love 		error = -ENOBUFS;
60342e9a92fSRobert Love 	return error;
60442e9a92fSRobert Love }
60542e9a92fSRobert Love 
6063a3b42bfSRobert Love /**
6073a3b42bfSRobert Love  * fc_exch_timeout() - Handle exchange timer expiration
6083a3b42bfSRobert Love  * @work: The work_struct identifying the exchange that timed out
60942e9a92fSRobert Love  */
61042e9a92fSRobert Love static void fc_exch_timeout(struct work_struct *work)
61142e9a92fSRobert Love {
61242e9a92fSRobert Love 	struct fc_exch *ep = container_of(work, struct fc_exch,
61342e9a92fSRobert Love 					  timeout_work.work);
61442e9a92fSRobert Love 	struct fc_seq *sp = &ep->seq;
61542e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
61642e9a92fSRobert Love 	void *arg;
61742e9a92fSRobert Love 	u32 e_stat;
61842e9a92fSRobert Love 	int rc = 1;
61942e9a92fSRobert Love 
620cd305ce4SRobert Love 	FC_EXCH_DBG(ep, "Exchange timed out\n");
621cd305ce4SRobert Love 
62242e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
62342e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
62442e9a92fSRobert Love 		goto unlock;
62542e9a92fSRobert Love 
62642e9a92fSRobert Love 	e_stat = ep->esb_stat;
62742e9a92fSRobert Love 	if (e_stat & ESB_ST_COMPLETE) {
62842e9a92fSRobert Love 		ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
629a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
63042e9a92fSRobert Love 		if (e_stat & ESB_ST_REC_QUAL)
63142e9a92fSRobert Love 			fc_exch_rrq(ep);
63242e9a92fSRobert Love 		goto done;
63342e9a92fSRobert Love 	} else {
63442e9a92fSRobert Love 		resp = ep->resp;
63542e9a92fSRobert Love 		arg = ep->arg;
63642e9a92fSRobert Love 		ep->resp = NULL;
63742e9a92fSRobert Love 		if (e_stat & ESB_ST_ABNORMAL)
63842e9a92fSRobert Love 			rc = fc_exch_done_locked(ep);
63942e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
64042e9a92fSRobert Love 		if (!rc)
641b2f0091fSVasu Dev 			fc_exch_delete(ep);
64242e9a92fSRobert Love 		if (resp)
64342e9a92fSRobert Love 			resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
64442e9a92fSRobert Love 		fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
64542e9a92fSRobert Love 		goto done;
64642e9a92fSRobert Love 	}
64742e9a92fSRobert Love unlock:
64842e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
64942e9a92fSRobert Love done:
65042e9a92fSRobert Love 	/*
65142e9a92fSRobert Love 	 * This release matches the hold taken when the timer was set.
65242e9a92fSRobert Love 	 */
65342e9a92fSRobert Love 	fc_exch_release(ep);
65442e9a92fSRobert Love }
65542e9a92fSRobert Love 
65652ff878cSVasu Dev /**
6573a3b42bfSRobert Love  * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
6583a3b42bfSRobert Love  * @lport: The local port that the exchange is for
6593a3b42bfSRobert Love  * @mp:	   The exchange manager that will allocate the exchange
66042e9a92fSRobert Love  *
661d7179680SVasu Dev  * Returns pointer to allocated fc_exch with exch lock held.
66242e9a92fSRobert Love  */
66352ff878cSVasu Dev static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
664d7179680SVasu Dev 					struct fc_exch_mgr *mp)
66542e9a92fSRobert Love {
66642e9a92fSRobert Love 	struct fc_exch *ep;
667b2f0091fSVasu Dev 	unsigned int cpu;
668b2f0091fSVasu Dev 	u16 index;
669b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
67042e9a92fSRobert Love 
67142e9a92fSRobert Love 	/* allocate memory for exchange */
67242e9a92fSRobert Love 	ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
67342e9a92fSRobert Love 	if (!ep) {
67442e9a92fSRobert Love 		atomic_inc(&mp->stats.no_free_exch);
67542e9a92fSRobert Love 		goto out;
67642e9a92fSRobert Love 	}
67742e9a92fSRobert Love 	memset(ep, 0, sizeof(*ep));
67842e9a92fSRobert Love 
679f018b73aSJoe Eykholt 	cpu = get_cpu();
680b2f0091fSVasu Dev 	pool = per_cpu_ptr(mp->pool, cpu);
681b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
682f018b73aSJoe Eykholt 	put_cpu();
683b2f0091fSVasu Dev 	index = pool->next_index;
684b2f0091fSVasu Dev 	/* allocate new exch from pool */
685b2f0091fSVasu Dev 	while (fc_exch_ptr_get(pool, index)) {
686b2f0091fSVasu Dev 		index = index == mp->pool_max_index ? 0 : index + 1;
687b2f0091fSVasu Dev 		if (index == pool->next_index)
68842e9a92fSRobert Love 			goto err;
68942e9a92fSRobert Love 	}
690b2f0091fSVasu Dev 	pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
69142e9a92fSRobert Love 
69242e9a92fSRobert Love 	fc_exch_hold(ep);	/* hold for exch in mp */
69342e9a92fSRobert Love 	spin_lock_init(&ep->ex_lock);
69442e9a92fSRobert Love 	/*
69542e9a92fSRobert Love 	 * Hold exch lock for caller to prevent fc_exch_reset()
69642e9a92fSRobert Love 	 * from releasing exch	while fc_exch_alloc() caller is
69742e9a92fSRobert Love 	 * still working on exch.
69842e9a92fSRobert Love 	 */
69942e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
70042e9a92fSRobert Love 
701b2f0091fSVasu Dev 	fc_exch_ptr_set(pool, index, ep);
702b2f0091fSVasu Dev 	list_add_tail(&ep->ex_list, &pool->ex_list);
70342e9a92fSRobert Love 	fc_seq_alloc(ep, ep->seq_id++);
704b2f0091fSVasu Dev 	pool->total_exches++;
705b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
70642e9a92fSRobert Love 
70742e9a92fSRobert Love 	/*
70842e9a92fSRobert Love 	 *  update exchange
70942e9a92fSRobert Love 	 */
710b2f0091fSVasu Dev 	ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
71142e9a92fSRobert Love 	ep->em = mp;
712b2f0091fSVasu Dev 	ep->pool = pool;
71352ff878cSVasu Dev 	ep->lp = lport;
71442e9a92fSRobert Love 	ep->f_ctl = FC_FC_FIRST_SEQ;	/* next seq is first seq */
71542e9a92fSRobert Love 	ep->rxid = FC_XID_UNKNOWN;
71642e9a92fSRobert Love 	ep->class = mp->class;
71742e9a92fSRobert Love 	INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
71842e9a92fSRobert Love out:
71942e9a92fSRobert Love 	return ep;
72042e9a92fSRobert Love err:
721b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
72242e9a92fSRobert Love 	atomic_inc(&mp->stats.no_free_exch_xid);
72342e9a92fSRobert Love 	mempool_free(ep, mp->ep_pool);
72442e9a92fSRobert Love 	return NULL;
72542e9a92fSRobert Love }
72652ff878cSVasu Dev 
72752ff878cSVasu Dev /**
7283a3b42bfSRobert Love  * fc_exch_alloc() - Allocate an exchange from an EM on a
7293a3b42bfSRobert Love  *		     local port's list of EMs.
7303a3b42bfSRobert Love  * @lport: The local port that will own the exchange
7313a3b42bfSRobert Love  * @fp:	   The FC frame that the exchange will be for
73252ff878cSVasu Dev  *
7333a3b42bfSRobert Love  * This function walks the list of exchange manager(EM)
7343a3b42bfSRobert Love  * anchors to select an EM for a new exchange allocation. The
7353a3b42bfSRobert Love  * EM is selected when a NULL match function pointer is encountered
7363a3b42bfSRobert Love  * or when a call to a match function returns true.
73752ff878cSVasu Dev  */
7383e22760dSVasu Dev static inline struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
7391a7b75aeSRobert Love 					    struct fc_frame *fp)
74052ff878cSVasu Dev {
74152ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
74252ff878cSVasu Dev 
7433e22760dSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list)
7443e22760dSVasu Dev 		if (!ema->match || ema->match(fp))
7453e22760dSVasu Dev 			return fc_exch_em_alloc(lport, ema->mp);
74652ff878cSVasu Dev 	return NULL;
74752ff878cSVasu Dev }
74842e9a92fSRobert Love 
7493a3b42bfSRobert Love /**
7503a3b42bfSRobert Love  * fc_exch_find() - Lookup and hold an exchange
7513a3b42bfSRobert Love  * @mp:	 The exchange manager to lookup the exchange from
7523a3b42bfSRobert Love  * @xid: The XID of the exchange to look up
75342e9a92fSRobert Love  */
75442e9a92fSRobert Love static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
75542e9a92fSRobert Love {
756b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
75742e9a92fSRobert Love 	struct fc_exch *ep = NULL;
75842e9a92fSRobert Love 
75942e9a92fSRobert Love 	if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
760b2f0091fSVasu Dev 		pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
761b2f0091fSVasu Dev 		spin_lock_bh(&pool->lock);
762b2f0091fSVasu Dev 		ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
76342e9a92fSRobert Love 		if (ep) {
76442e9a92fSRobert Love 			fc_exch_hold(ep);
76542e9a92fSRobert Love 			WARN_ON(ep->xid != xid);
76642e9a92fSRobert Love 		}
767b2f0091fSVasu Dev 		spin_unlock_bh(&pool->lock);
76842e9a92fSRobert Love 	}
76942e9a92fSRobert Love 	return ep;
77042e9a92fSRobert Love }
77142e9a92fSRobert Love 
7721a7b75aeSRobert Love 
7731a7b75aeSRobert Love /**
7741a7b75aeSRobert Love  * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
7751a7b75aeSRobert Love  *		    the memory allocated for the related objects may be freed.
7763a3b42bfSRobert Love  * @sp: The sequence that has completed
7771a7b75aeSRobert Love  */
7781a7b75aeSRobert Love static void fc_exch_done(struct fc_seq *sp)
77942e9a92fSRobert Love {
78042e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
78142e9a92fSRobert Love 	int rc;
78242e9a92fSRobert Love 
78342e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
78442e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
78542e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
78642e9a92fSRobert Love 	if (!rc)
787b2f0091fSVasu Dev 		fc_exch_delete(ep);
78842e9a92fSRobert Love }
78942e9a92fSRobert Love 
7903a3b42bfSRobert Love /**
7913a3b42bfSRobert Love  * fc_exch_resp() - Allocate a new exchange for a response frame
7923a3b42bfSRobert Love  * @lport: The local port that the exchange was for
7933a3b42bfSRobert Love  * @mp:	   The exchange manager to allocate the exchange from
7943a3b42bfSRobert Love  * @fp:	   The response frame
7953a3b42bfSRobert Love  *
79642e9a92fSRobert Love  * Sets the responder ID in the frame header.
79742e9a92fSRobert Love  */
79852ff878cSVasu Dev static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
79952ff878cSVasu Dev 				    struct fc_exch_mgr *mp,
80052ff878cSVasu Dev 				    struct fc_frame *fp)
80142e9a92fSRobert Love {
80242e9a92fSRobert Love 	struct fc_exch *ep;
80342e9a92fSRobert Love 	struct fc_frame_header *fh;
80442e9a92fSRobert Love 
80552ff878cSVasu Dev 	ep = fc_exch_alloc(lport, fp);
80642e9a92fSRobert Love 	if (ep) {
80742e9a92fSRobert Love 		ep->class = fc_frame_class(fp);
80842e9a92fSRobert Love 
80942e9a92fSRobert Love 		/*
81042e9a92fSRobert Love 		 * Set EX_CTX indicating we're responding on this exchange.
81142e9a92fSRobert Love 		 */
81242e9a92fSRobert Love 		ep->f_ctl |= FC_FC_EX_CTX;	/* we're responding */
81342e9a92fSRobert Love 		ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not new */
81442e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
81542e9a92fSRobert Love 		ep->sid = ntoh24(fh->fh_d_id);
81642e9a92fSRobert Love 		ep->did = ntoh24(fh->fh_s_id);
81742e9a92fSRobert Love 		ep->oid = ep->did;
81842e9a92fSRobert Love 
81942e9a92fSRobert Love 		/*
82042e9a92fSRobert Love 		 * Allocated exchange has placed the XID in the
82142e9a92fSRobert Love 		 * originator field. Move it to the responder field,
82242e9a92fSRobert Love 		 * and set the originator XID from the frame.
82342e9a92fSRobert Love 		 */
82442e9a92fSRobert Love 		ep->rxid = ep->xid;
82542e9a92fSRobert Love 		ep->oxid = ntohs(fh->fh_ox_id);
82642e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
82742e9a92fSRobert Love 		if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
82842e9a92fSRobert Love 			ep->esb_stat &= ~ESB_ST_SEQ_INIT;
82942e9a92fSRobert Love 
83042e9a92fSRobert Love 		fc_exch_hold(ep);	/* hold for caller */
83152ff878cSVasu Dev 		spin_unlock_bh(&ep->ex_lock);	/* lock from fc_exch_alloc */
83242e9a92fSRobert Love 	}
83342e9a92fSRobert Love 	return ep;
83442e9a92fSRobert Love }
83542e9a92fSRobert Love 
8363a3b42bfSRobert Love /**
8373a3b42bfSRobert Love  * fc_seq_lookup_recip() - Find a sequence where the other end
8383a3b42bfSRobert Love  *			   originated the sequence
8393a3b42bfSRobert Love  * @lport: The local port that the frame was sent to
8403a3b42bfSRobert Love  * @mp:	   The Exchange Manager to lookup the exchange from
8413a3b42bfSRobert Love  * @fp:	   The frame associated with the sequence we're looking for
8423a3b42bfSRobert Love  *
84342e9a92fSRobert Love  * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
84442e9a92fSRobert Love  * on the ep that should be released by the caller.
84542e9a92fSRobert Love  */
84652ff878cSVasu Dev static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
84752ff878cSVasu Dev 						 struct fc_exch_mgr *mp,
848b2ab99c9SRobert Love 						 struct fc_frame *fp)
84942e9a92fSRobert Love {
85042e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
85142e9a92fSRobert Love 	struct fc_exch *ep = NULL;
85242e9a92fSRobert Love 	struct fc_seq *sp = NULL;
85342e9a92fSRobert Love 	enum fc_pf_rjt_reason reject = FC_RJT_NONE;
85442e9a92fSRobert Love 	u32 f_ctl;
85542e9a92fSRobert Love 	u16 xid;
85642e9a92fSRobert Love 
85742e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
85842e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
85942e9a92fSRobert Love 
86042e9a92fSRobert Love 	/*
86142e9a92fSRobert Love 	 * Lookup or create the exchange if we will be creating the sequence.
86242e9a92fSRobert Love 	 */
86342e9a92fSRobert Love 	if (f_ctl & FC_FC_EX_CTX) {
86442e9a92fSRobert Love 		xid = ntohs(fh->fh_ox_id);	/* we originated exch */
86542e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
86642e9a92fSRobert Love 		if (!ep) {
86742e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
86842e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
86942e9a92fSRobert Love 			goto out;
87042e9a92fSRobert Love 		}
87142e9a92fSRobert Love 		if (ep->rxid == FC_XID_UNKNOWN)
87242e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
87342e9a92fSRobert Love 		else if (ep->rxid != ntohs(fh->fh_rx_id)) {
87442e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
87542e9a92fSRobert Love 			goto rel;
87642e9a92fSRobert Love 		}
87742e9a92fSRobert Love 	} else {
87842e9a92fSRobert Love 		xid = ntohs(fh->fh_rx_id);	/* we are the responder */
87942e9a92fSRobert Love 
88042e9a92fSRobert Love 		/*
88142e9a92fSRobert Love 		 * Special case for MDS issuing an ELS TEST with a
88242e9a92fSRobert Love 		 * bad rxid of 0.
88342e9a92fSRobert Love 		 * XXX take this out once we do the proper reject.
88442e9a92fSRobert Love 		 */
88542e9a92fSRobert Love 		if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
88642e9a92fSRobert Love 		    fc_frame_payload_op(fp) == ELS_TEST) {
88742e9a92fSRobert Love 			fh->fh_rx_id = htons(FC_XID_UNKNOWN);
88842e9a92fSRobert Love 			xid = FC_XID_UNKNOWN;
88942e9a92fSRobert Love 		}
89042e9a92fSRobert Love 
89142e9a92fSRobert Love 		/*
89242e9a92fSRobert Love 		 * new sequence - find the exchange
89342e9a92fSRobert Love 		 */
89442e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
89542e9a92fSRobert Love 		if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
89642e9a92fSRobert Love 			if (ep) {
89742e9a92fSRobert Love 				atomic_inc(&mp->stats.xid_busy);
89842e9a92fSRobert Love 				reject = FC_RJT_RX_ID;
89942e9a92fSRobert Love 				goto rel;
90042e9a92fSRobert Love 			}
90152ff878cSVasu Dev 			ep = fc_exch_resp(lport, mp, fp);
90242e9a92fSRobert Love 			if (!ep) {
90342e9a92fSRobert Love 				reject = FC_RJT_EXCH_EST;	/* XXX */
90442e9a92fSRobert Love 				goto out;
90542e9a92fSRobert Love 			}
90642e9a92fSRobert Love 			xid = ep->xid;	/* get our XID */
90742e9a92fSRobert Love 		} else if (!ep) {
90842e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
90942e9a92fSRobert Love 			reject = FC_RJT_RX_ID;	/* XID not found */
91042e9a92fSRobert Love 			goto out;
91142e9a92fSRobert Love 		}
91242e9a92fSRobert Love 	}
91342e9a92fSRobert Love 
91442e9a92fSRobert Love 	/*
91542e9a92fSRobert Love 	 * At this point, we have the exchange held.
91642e9a92fSRobert Love 	 * Find or create the sequence.
91742e9a92fSRobert Love 	 */
91842e9a92fSRobert Love 	if (fc_sof_is_init(fr_sof(fp))) {
919a104c844SVasu Dev 		sp = &ep->seq;
92042e9a92fSRobert Love 		sp->ssb_stat |= SSB_ST_RESP;
921b3667f91SJoe Eykholt 		sp->id = fh->fh_seq_id;
92242e9a92fSRobert Love 	} else {
92342e9a92fSRobert Love 		sp = &ep->seq;
92442e9a92fSRobert Love 		if (sp->id != fh->fh_seq_id) {
92542e9a92fSRobert Love 			atomic_inc(&mp->stats.seq_not_found);
92642e9a92fSRobert Love 			reject = FC_RJT_SEQ_ID;	/* sequence/exch should exist */
92742e9a92fSRobert Love 			goto rel;
92842e9a92fSRobert Love 		}
92942e9a92fSRobert Love 	}
93042e9a92fSRobert Love 	WARN_ON(ep != fc_seq_exch(sp));
93142e9a92fSRobert Love 
93242e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
93342e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
93442e9a92fSRobert Love 
93542e9a92fSRobert Love 	fr_seq(fp) = sp;
93642e9a92fSRobert Love out:
93742e9a92fSRobert Love 	return reject;
93842e9a92fSRobert Love rel:
93942e9a92fSRobert Love 	fc_exch_done(&ep->seq);
94042e9a92fSRobert Love 	fc_exch_release(ep);	/* hold from fc_exch_find/fc_exch_resp */
94142e9a92fSRobert Love 	return reject;
94242e9a92fSRobert Love }
94342e9a92fSRobert Love 
9443a3b42bfSRobert Love /**
9453a3b42bfSRobert Love  * fc_seq_lookup_orig() - Find a sequence where this end
9463a3b42bfSRobert Love  *			  originated the sequence
9473a3b42bfSRobert Love  * @mp:	   The Exchange Manager to lookup the exchange from
9483a3b42bfSRobert Love  * @fp:	   The frame associated with the sequence we're looking for
9493a3b42bfSRobert Love  *
95042e9a92fSRobert Love  * Does not hold the sequence for the caller.
95142e9a92fSRobert Love  */
95242e9a92fSRobert Love static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
95342e9a92fSRobert Love 					 struct fc_frame *fp)
95442e9a92fSRobert Love {
95542e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
95642e9a92fSRobert Love 	struct fc_exch *ep;
95742e9a92fSRobert Love 	struct fc_seq *sp = NULL;
95842e9a92fSRobert Love 	u32 f_ctl;
95942e9a92fSRobert Love 	u16 xid;
96042e9a92fSRobert Love 
96142e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
96242e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
96342e9a92fSRobert Love 	xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
96442e9a92fSRobert Love 	ep = fc_exch_find(mp, xid);
96542e9a92fSRobert Love 	if (!ep)
96642e9a92fSRobert Love 		return NULL;
96742e9a92fSRobert Love 	if (ep->seq.id == fh->fh_seq_id) {
96842e9a92fSRobert Love 		/*
96942e9a92fSRobert Love 		 * Save the RX_ID if we didn't previously know it.
97042e9a92fSRobert Love 		 */
97142e9a92fSRobert Love 		sp = &ep->seq;
97242e9a92fSRobert Love 		if ((f_ctl & FC_FC_EX_CTX) != 0 &&
97342e9a92fSRobert Love 		    ep->rxid == FC_XID_UNKNOWN) {
97442e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
97542e9a92fSRobert Love 		}
97642e9a92fSRobert Love 	}
97742e9a92fSRobert Love 	fc_exch_release(ep);
97842e9a92fSRobert Love 	return sp;
97942e9a92fSRobert Love }
98042e9a92fSRobert Love 
9813a3b42bfSRobert Love /**
9823a3b42bfSRobert Love  * fc_exch_set_addr() - Set the source and destination IDs for an exchange
9833a3b42bfSRobert Love  * @ep:	     The exchange to set the addresses for
9843a3b42bfSRobert Love  * @orig_id: The originator's ID
9853a3b42bfSRobert Love  * @resp_id: The responder's ID
9863a3b42bfSRobert Love  *
98742e9a92fSRobert Love  * Note this must be done before the first sequence of the exchange is sent.
98842e9a92fSRobert Love  */
98942e9a92fSRobert Love static void fc_exch_set_addr(struct fc_exch *ep,
99042e9a92fSRobert Love 			     u32 orig_id, u32 resp_id)
99142e9a92fSRobert Love {
99242e9a92fSRobert Love 	ep->oid = orig_id;
99342e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP) {
99442e9a92fSRobert Love 		ep->sid = resp_id;
99542e9a92fSRobert Love 		ep->did = orig_id;
99642e9a92fSRobert Love 	} else {
99742e9a92fSRobert Love 		ep->sid = orig_id;
99842e9a92fSRobert Love 		ep->did = resp_id;
99942e9a92fSRobert Love 	}
100042e9a92fSRobert Love }
100142e9a92fSRobert Love 
10021a7b75aeSRobert Love /**
10033a3b42bfSRobert Love  * fc_seq_els_rsp_send() - Send an ELS response using infomation from
10043a3b42bfSRobert Love  *			   the existing sequence/exchange.
10053a3b42bfSRobert Love  * @sp:	      The sequence/exchange to get information from
10063a3b42bfSRobert Love  * @els_cmd:  The ELS command to be sent
10073a3b42bfSRobert Love  * @els_data: The ELS data to be sent
100842e9a92fSRobert Love  */
10091a7b75aeSRobert Love static void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
101042e9a92fSRobert Love 				struct fc_seq_els_data *els_data)
101142e9a92fSRobert Love {
101242e9a92fSRobert Love 	switch (els_cmd) {
101342e9a92fSRobert Love 	case ELS_LS_RJT:
101442e9a92fSRobert Love 		fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
101542e9a92fSRobert Love 		break;
101642e9a92fSRobert Love 	case ELS_LS_ACC:
101742e9a92fSRobert Love 		fc_seq_ls_acc(sp);
101842e9a92fSRobert Love 		break;
101942e9a92fSRobert Love 	case ELS_RRQ:
102042e9a92fSRobert Love 		fc_exch_els_rrq(sp, els_data->fp);
102142e9a92fSRobert Love 		break;
102242e9a92fSRobert Love 	case ELS_REC:
102342e9a92fSRobert Love 		fc_exch_els_rec(sp, els_data->fp);
102442e9a92fSRobert Love 		break;
102542e9a92fSRobert Love 	default:
10267414705eSRobert Love 		FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
102742e9a92fSRobert Love 	}
102842e9a92fSRobert Love }
102942e9a92fSRobert Love 
10303a3b42bfSRobert Love /**
10313a3b42bfSRobert Love  * fc_seq_send_last() - Send a sequence that is the last in the exchange
10323a3b42bfSRobert Love  * @sp:	     The sequence that is to be sent
10333a3b42bfSRobert Love  * @fp:	     The frame that will be sent on the sequence
10343a3b42bfSRobert Love  * @rctl:    The R_CTL information to be sent
10353a3b42bfSRobert Love  * @fh_type: The frame header type
103642e9a92fSRobert Love  */
103742e9a92fSRobert Love static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
103842e9a92fSRobert Love 			     enum fc_rctl rctl, enum fc_fh_type fh_type)
103942e9a92fSRobert Love {
104042e9a92fSRobert Love 	u32 f_ctl;
104142e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
104242e9a92fSRobert Love 
104342e9a92fSRobert Love 	f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
104442e9a92fSRobert Love 	f_ctl |= ep->f_ctl;
104542e9a92fSRobert Love 	fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
104642e9a92fSRobert Love 	fc_seq_send(ep->lp, sp, fp);
104742e9a92fSRobert Love }
104842e9a92fSRobert Love 
10493a3b42bfSRobert Love /**
10503a3b42bfSRobert Love  * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
10513a3b42bfSRobert Love  * @sp:	   The sequence to send the ACK on
10523a3b42bfSRobert Love  * @rx_fp: The received frame that is being acknoledged
10533a3b42bfSRobert Love  *
105442e9a92fSRobert Love  * Send ACK_1 (or equiv.) indicating we received something.
105542e9a92fSRobert Love  */
105642e9a92fSRobert Love static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
105742e9a92fSRobert Love {
105842e9a92fSRobert Love 	struct fc_frame *fp;
105942e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
106042e9a92fSRobert Love 	struct fc_frame_header *fh;
106142e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
10623a3b42bfSRobert Love 	struct fc_lport *lport = ep->lp;
106342e9a92fSRobert Love 	unsigned int f_ctl;
106442e9a92fSRobert Love 
106542e9a92fSRobert Love 	/*
106642e9a92fSRobert Love 	 * Don't send ACKs for class 3.
106742e9a92fSRobert Love 	 */
106842e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(rx_fp))) {
10693a3b42bfSRobert Love 		fp = fc_frame_alloc(lport, 0);
107042e9a92fSRobert Love 		if (!fp)
107142e9a92fSRobert Love 			return;
107242e9a92fSRobert Love 
107342e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
107442e9a92fSRobert Love 		fh->fh_r_ctl = FC_RCTL_ACK_1;
107542e9a92fSRobert Love 		fh->fh_type = FC_TYPE_BLS;
107642e9a92fSRobert Love 
107742e9a92fSRobert Love 		/*
107842e9a92fSRobert Love 		 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
107942e9a92fSRobert Love 		 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
108042e9a92fSRobert Love 		 * Bits 9-8 are meaningful (retransmitted or unidirectional).
108142e9a92fSRobert Love 		 * Last ACK uses bits 7-6 (continue sequence),
108242e9a92fSRobert Love 		 * bits 5-4 are meaningful (what kind of ACK to use).
108342e9a92fSRobert Love 		 */
108442e9a92fSRobert Love 		rx_fh = fc_frame_header_get(rx_fp);
108542e9a92fSRobert Love 		f_ctl = ntoh24(rx_fh->fh_f_ctl);
108642e9a92fSRobert Love 		f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
108742e9a92fSRobert Love 			FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
108842e9a92fSRobert Love 			FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
108942e9a92fSRobert Love 			FC_FC_RETX_SEQ | FC_FC_UNI_TX;
109042e9a92fSRobert Love 		f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
109142e9a92fSRobert Love 		hton24(fh->fh_f_ctl, f_ctl);
109242e9a92fSRobert Love 
109342e9a92fSRobert Love 		fc_exch_setup_hdr(ep, fp, f_ctl);
109442e9a92fSRobert Love 		fh->fh_seq_id = rx_fh->fh_seq_id;
109542e9a92fSRobert Love 		fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
109642e9a92fSRobert Love 		fh->fh_parm_offset = htonl(1);	/* ack single frame */
109742e9a92fSRobert Love 
109842e9a92fSRobert Love 		fr_sof(fp) = fr_sof(rx_fp);
109942e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
110042e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_T;
110142e9a92fSRobert Love 		else
110242e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
110342e9a92fSRobert Love 
11043a3b42bfSRobert Love 		lport->tt.frame_send(lport, fp);
110542e9a92fSRobert Love 	}
110642e9a92fSRobert Love }
110742e9a92fSRobert Love 
11083a3b42bfSRobert Love /**
11093a3b42bfSRobert Love  * fc_exch_send_ba_rjt() - Send BLS Reject
11103a3b42bfSRobert Love  * @rx_fp:  The frame being rejected
11113a3b42bfSRobert Love  * @reason: The reason the frame is being rejected
11123a3b42bfSRobert Love  * @explan: The explaination for the rejection
11133a3b42bfSRobert Love  *
111442e9a92fSRobert Love  * This is for rejecting BA_ABTS only.
111542e9a92fSRobert Love  */
1116b2ab99c9SRobert Love static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1117b2ab99c9SRobert Love 				enum fc_ba_rjt_reason reason,
111842e9a92fSRobert Love 				enum fc_ba_rjt_explan explan)
111942e9a92fSRobert Love {
112042e9a92fSRobert Love 	struct fc_frame *fp;
112142e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
112242e9a92fSRobert Love 	struct fc_frame_header *fh;
112342e9a92fSRobert Love 	struct fc_ba_rjt *rp;
11243a3b42bfSRobert Love 	struct fc_lport *lport;
112542e9a92fSRobert Love 	unsigned int f_ctl;
112642e9a92fSRobert Love 
11273a3b42bfSRobert Love 	lport = fr_dev(rx_fp);
11283a3b42bfSRobert Love 	fp = fc_frame_alloc(lport, sizeof(*rp));
112942e9a92fSRobert Love 	if (!fp)
113042e9a92fSRobert Love 		return;
113142e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
113242e9a92fSRobert Love 	rx_fh = fc_frame_header_get(rx_fp);
113342e9a92fSRobert Love 
113442e9a92fSRobert Love 	memset(fh, 0, sizeof(*fh) + sizeof(*rp));
113542e9a92fSRobert Love 
113642e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
113742e9a92fSRobert Love 	rp->br_reason = reason;
113842e9a92fSRobert Love 	rp->br_explan = explan;
113942e9a92fSRobert Love 
114042e9a92fSRobert Love 	/*
114142e9a92fSRobert Love 	 * seq_id, cs_ctl, df_ctl and param/offset are zero.
114242e9a92fSRobert Love 	 */
114342e9a92fSRobert Love 	memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
114442e9a92fSRobert Love 	memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
11451d490ce3SJoe Eykholt 	fh->fh_ox_id = rx_fh->fh_ox_id;
11461d490ce3SJoe Eykholt 	fh->fh_rx_id = rx_fh->fh_rx_id;
114742e9a92fSRobert Love 	fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
114842e9a92fSRobert Love 	fh->fh_r_ctl = FC_RCTL_BA_RJT;
114942e9a92fSRobert Love 	fh->fh_type = FC_TYPE_BLS;
115042e9a92fSRobert Love 
115142e9a92fSRobert Love 	/*
115242e9a92fSRobert Love 	 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
115342e9a92fSRobert Love 	 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
115442e9a92fSRobert Love 	 * Bits 9-8 are meaningful (retransmitted or unidirectional).
115542e9a92fSRobert Love 	 * Last ACK uses bits 7-6 (continue sequence),
115642e9a92fSRobert Love 	 * bits 5-4 are meaningful (what kind of ACK to use).
115742e9a92fSRobert Love 	 * Always set LAST_SEQ, END_SEQ.
115842e9a92fSRobert Love 	 */
115942e9a92fSRobert Love 	f_ctl = ntoh24(rx_fh->fh_f_ctl);
116042e9a92fSRobert Love 	f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
116142e9a92fSRobert Love 		FC_FC_END_CONN | FC_FC_SEQ_INIT |
116242e9a92fSRobert Love 		FC_FC_RETX_SEQ | FC_FC_UNI_TX;
116342e9a92fSRobert Love 	f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
116442e9a92fSRobert Love 	f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
116542e9a92fSRobert Love 	f_ctl &= ~FC_FC_FIRST_SEQ;
116642e9a92fSRobert Love 	hton24(fh->fh_f_ctl, f_ctl);
116742e9a92fSRobert Love 
116842e9a92fSRobert Love 	fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
116942e9a92fSRobert Love 	fr_eof(fp) = FC_EOF_T;
117042e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(fp)))
117142e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
117242e9a92fSRobert Love 
11733a3b42bfSRobert Love 	lport->tt.frame_send(lport, fp);
117442e9a92fSRobert Love }
117542e9a92fSRobert Love 
11763a3b42bfSRobert Love /**
11773a3b42bfSRobert Love  * fc_exch_recv_abts() - Handle an incoming ABTS
11783a3b42bfSRobert Love  * @ep:	   The exchange the abort was on
11793a3b42bfSRobert Love  * @rx_fp: The ABTS frame
11803a3b42bfSRobert Love  *
11813a3b42bfSRobert Love  * This would be for target mode usually, but could be due to lost
11823a3b42bfSRobert Love  * FCP transfer ready, confirm or RRQ. We always handle this as an
11833a3b42bfSRobert Love  * exchange abort, ignoring the parameter.
118442e9a92fSRobert Love  */
118542e9a92fSRobert Love static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
118642e9a92fSRobert Love {
118742e9a92fSRobert Love 	struct fc_frame *fp;
118842e9a92fSRobert Love 	struct fc_ba_acc *ap;
118942e9a92fSRobert Love 	struct fc_frame_header *fh;
119042e9a92fSRobert Love 	struct fc_seq *sp;
119142e9a92fSRobert Love 
119242e9a92fSRobert Love 	if (!ep)
119342e9a92fSRobert Love 		goto reject;
119442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
119542e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
119642e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
119742e9a92fSRobert Love 		goto reject;
119842e9a92fSRobert Love 	}
119942e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL))
120042e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for REC_QUAL */
120142e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
120242e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
120342e9a92fSRobert Love 
120442e9a92fSRobert Love 	fp = fc_frame_alloc(ep->lp, sizeof(*ap));
120542e9a92fSRobert Love 	if (!fp) {
120642e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
120742e9a92fSRobert Love 		goto free;
120842e9a92fSRobert Love 	}
120942e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
121042e9a92fSRobert Love 	ap = fc_frame_payload_get(fp, sizeof(*ap));
121142e9a92fSRobert Love 	memset(ap, 0, sizeof(*ap));
121242e9a92fSRobert Love 	sp = &ep->seq;
121342e9a92fSRobert Love 	ap->ba_high_seq_cnt = htons(0xffff);
121442e9a92fSRobert Love 	if (sp->ssb_stat & SSB_ST_RESP) {
121542e9a92fSRobert Love 		ap->ba_seq_id = sp->id;
121642e9a92fSRobert Love 		ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
121742e9a92fSRobert Love 		ap->ba_high_seq_cnt = fh->fh_seq_cnt;
121842e9a92fSRobert Love 		ap->ba_low_seq_cnt = htons(sp->cnt);
121942e9a92fSRobert Love 	}
1220a7e84f2bSVasu Dev 	sp = fc_seq_start_next_locked(sp);
122142e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
122242e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
122342e9a92fSRobert Love 	fc_frame_free(rx_fp);
122442e9a92fSRobert Love 	return;
122542e9a92fSRobert Love 
122642e9a92fSRobert Love reject:
122742e9a92fSRobert Love 	fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
122842e9a92fSRobert Love free:
122942e9a92fSRobert Love 	fc_frame_free(rx_fp);
123042e9a92fSRobert Love }
123142e9a92fSRobert Love 
12323a3b42bfSRobert Love /**
12333a3b42bfSRobert Love  * fc_exch_recv_req() - Handler for an incoming request where is other
12343a3b42bfSRobert Love  *			end is originating the sequence
12353a3b42bfSRobert Love  * @lport: The local port that received the request
12363a3b42bfSRobert Love  * @mp:	   The EM that the exchange is on
12373a3b42bfSRobert Love  * @fp:	   The request frame
123842e9a92fSRobert Love  */
12393a3b42bfSRobert Love static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
124042e9a92fSRobert Love 			     struct fc_frame *fp)
124142e9a92fSRobert Love {
124242e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
124342e9a92fSRobert Love 	struct fc_seq *sp = NULL;
124442e9a92fSRobert Love 	struct fc_exch *ep = NULL;
124542e9a92fSRobert Love 	enum fc_pf_rjt_reason reject;
124642e9a92fSRobert Love 
1247174e1ebfSChris Leech 	/* We can have the wrong fc_lport at this point with NPIV, which is a
1248174e1ebfSChris Leech 	 * problem now that we know a new exchange needs to be allocated
1249174e1ebfSChris Leech 	 */
12503a3b42bfSRobert Love 	lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
12513a3b42bfSRobert Love 	if (!lport) {
1252174e1ebfSChris Leech 		fc_frame_free(fp);
1253174e1ebfSChris Leech 		return;
1254174e1ebfSChris Leech 	}
1255174e1ebfSChris Leech 
125642e9a92fSRobert Love 	fr_seq(fp) = NULL;
12573a3b42bfSRobert Love 	reject = fc_seq_lookup_recip(lport, mp, fp);
125842e9a92fSRobert Love 	if (reject == FC_RJT_NONE) {
125942e9a92fSRobert Love 		sp = fr_seq(fp);	/* sequence will be held */
126042e9a92fSRobert Love 		ep = fc_seq_exch(sp);
126142e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
126242e9a92fSRobert Love 
126342e9a92fSRobert Love 		/*
126442e9a92fSRobert Love 		 * Call the receive function.
126542e9a92fSRobert Love 		 *
126642e9a92fSRobert Love 		 * The receive function may allocate a new sequence
126742e9a92fSRobert Love 		 * over the old one, so we shouldn't change the
126842e9a92fSRobert Love 		 * sequence after this.
126942e9a92fSRobert Love 		 *
127042e9a92fSRobert Love 		 * The frame will be freed by the receive function.
127142e9a92fSRobert Love 		 * If new exch resp handler is valid then call that
127242e9a92fSRobert Love 		 * first.
127342e9a92fSRobert Love 		 */
127442e9a92fSRobert Love 		if (ep->resp)
127542e9a92fSRobert Love 			ep->resp(sp, fp, ep->arg);
127642e9a92fSRobert Love 		else
12773a3b42bfSRobert Love 			lport->tt.lport_recv(lport, sp, fp);
127842e9a92fSRobert Love 		fc_exch_release(ep);	/* release from lookup */
127942e9a92fSRobert Love 	} else {
12803a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
12813a3b42bfSRobert Love 			     reject);
128242e9a92fSRobert Love 		fc_frame_free(fp);
128342e9a92fSRobert Love 	}
128442e9a92fSRobert Love }
128542e9a92fSRobert Love 
12863a3b42bfSRobert Love /**
12873a3b42bfSRobert Love  * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
12883a3b42bfSRobert Love  *			     end is the originator of the sequence that is a
12893a3b42bfSRobert Love  *			     response to our initial exchange
12903a3b42bfSRobert Love  * @mp: The EM that the exchange is on
12913a3b42bfSRobert Love  * @fp: The response frame
129242e9a92fSRobert Love  */
129342e9a92fSRobert Love static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
129442e9a92fSRobert Love {
129542e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
129642e9a92fSRobert Love 	struct fc_seq *sp;
129742e9a92fSRobert Love 	struct fc_exch *ep;
129842e9a92fSRobert Love 	enum fc_sof sof;
129942e9a92fSRobert Love 	u32 f_ctl;
130042e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
130142e9a92fSRobert Love 	void *ex_resp_arg;
130242e9a92fSRobert Love 	int rc;
130342e9a92fSRobert Love 
130442e9a92fSRobert Love 	ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
130542e9a92fSRobert Love 	if (!ep) {
130642e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
130742e9a92fSRobert Love 		goto out;
130842e9a92fSRobert Love 	}
130930121d14SSteve Ma 	if (ep->esb_stat & ESB_ST_COMPLETE) {
131030121d14SSteve Ma 		atomic_inc(&mp->stats.xid_not_found);
131130121d14SSteve Ma 		goto out;
131230121d14SSteve Ma 	}
131342e9a92fSRobert Love 	if (ep->rxid == FC_XID_UNKNOWN)
131442e9a92fSRobert Love 		ep->rxid = ntohs(fh->fh_rx_id);
131542e9a92fSRobert Love 	if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
131642e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
131742e9a92fSRobert Love 		goto rel;
131842e9a92fSRobert Love 	}
131942e9a92fSRobert Love 	if (ep->did != ntoh24(fh->fh_s_id) &&
132042e9a92fSRobert Love 	    ep->did != FC_FID_FLOGI) {
132142e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
132242e9a92fSRobert Love 		goto rel;
132342e9a92fSRobert Love 	}
132442e9a92fSRobert Love 	sof = fr_sof(fp);
132542e9a92fSRobert Love 	sp = &ep->seq;
1326b3667f91SJoe Eykholt 	if (fc_sof_is_init(sof)) {
1327a104c844SVasu Dev 		sp->ssb_stat |= SSB_ST_RESP;
1328b3667f91SJoe Eykholt 		sp->id = fh->fh_seq_id;
1329b3667f91SJoe Eykholt 	} else if (sp->id != fh->fh_seq_id) {
133042e9a92fSRobert Love 		atomic_inc(&mp->stats.seq_not_found);
133142e9a92fSRobert Love 		goto rel;
133242e9a92fSRobert Love 	}
1333a104c844SVasu Dev 
133442e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
133542e9a92fSRobert Love 	fr_seq(fp) = sp;
133642e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
133742e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
133842e9a92fSRobert Love 
133942e9a92fSRobert Love 	if (fc_sof_needs_ack(sof))
134042e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
134142e9a92fSRobert Love 	resp = ep->resp;
134242e9a92fSRobert Love 	ex_resp_arg = ep->arg;
134342e9a92fSRobert Love 
134442e9a92fSRobert Love 	if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
134542e9a92fSRobert Love 	    (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
134642e9a92fSRobert Love 	    (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
134742e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
134842e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
134942e9a92fSRobert Love 		WARN_ON(fc_seq_exch(sp) != ep);
135042e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
135142e9a92fSRobert Love 		if (!rc)
1352b2f0091fSVasu Dev 			fc_exch_delete(ep);
135342e9a92fSRobert Love 	}
135442e9a92fSRobert Love 
135542e9a92fSRobert Love 	/*
135642e9a92fSRobert Love 	 * Call the receive function.
135742e9a92fSRobert Love 	 * The sequence is held (has a refcnt) for us,
135842e9a92fSRobert Love 	 * but not for the receive function.
135942e9a92fSRobert Love 	 *
136042e9a92fSRobert Love 	 * The receive function may allocate a new sequence
136142e9a92fSRobert Love 	 * over the old one, so we shouldn't change the
136242e9a92fSRobert Love 	 * sequence after this.
136342e9a92fSRobert Love 	 *
136442e9a92fSRobert Love 	 * The frame will be freed by the receive function.
136542e9a92fSRobert Love 	 * If new exch resp handler is valid then call that
136642e9a92fSRobert Love 	 * first.
136742e9a92fSRobert Love 	 */
136842e9a92fSRobert Love 	if (resp)
136942e9a92fSRobert Love 		resp(sp, fp, ex_resp_arg);
137042e9a92fSRobert Love 	else
137142e9a92fSRobert Love 		fc_frame_free(fp);
137242e9a92fSRobert Love 	fc_exch_release(ep);
137342e9a92fSRobert Love 	return;
137442e9a92fSRobert Love rel:
137542e9a92fSRobert Love 	fc_exch_release(ep);
137642e9a92fSRobert Love out:
137742e9a92fSRobert Love 	fc_frame_free(fp);
137842e9a92fSRobert Love }
137942e9a92fSRobert Love 
13803a3b42bfSRobert Love /**
13813a3b42bfSRobert Love  * fc_exch_recv_resp() - Handler for a sequence where other end is
13823a3b42bfSRobert Love  *			 responding to our sequence
13833a3b42bfSRobert Love  * @mp: The EM that the exchange is on
13843a3b42bfSRobert Love  * @fp: The response frame
138542e9a92fSRobert Love  */
138642e9a92fSRobert Love static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
138742e9a92fSRobert Love {
138842e9a92fSRobert Love 	struct fc_seq *sp;
138942e9a92fSRobert Love 
139042e9a92fSRobert Love 	sp = fc_seq_lookup_orig(mp, fp);	/* doesn't hold sequence */
1391d459b7eaSRobert Love 
1392d459b7eaSRobert Love 	if (!sp)
139342e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
1394d459b7eaSRobert Love 	else
139542e9a92fSRobert Love 		atomic_inc(&mp->stats.non_bls_resp);
1396d459b7eaSRobert Love 
139742e9a92fSRobert Love 	fc_frame_free(fp);
139842e9a92fSRobert Love }
139942e9a92fSRobert Love 
14003a3b42bfSRobert Love /**
14013a3b42bfSRobert Love  * fc_exch_abts_resp() - Handler for a response to an ABT
14023a3b42bfSRobert Love  * @ep: The exchange that the frame is on
14033a3b42bfSRobert Love  * @fp: The response frame
14043a3b42bfSRobert Love  *
14053a3b42bfSRobert Love  * This response would be to an ABTS cancelling an exchange or sequence.
14063a3b42bfSRobert Love  * The response can be either BA_ACC or BA_RJT
140742e9a92fSRobert Love  */
140842e9a92fSRobert Love static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
140942e9a92fSRobert Love {
141042e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
141142e9a92fSRobert Love 	void *ex_resp_arg;
141242e9a92fSRobert Love 	struct fc_frame_header *fh;
141342e9a92fSRobert Love 	struct fc_ba_acc *ap;
141442e9a92fSRobert Love 	struct fc_seq *sp;
141542e9a92fSRobert Love 	u16 low;
141642e9a92fSRobert Love 	u16 high;
141742e9a92fSRobert Love 	int rc = 1, has_rec = 0;
141842e9a92fSRobert Love 
141942e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
14207414705eSRobert Love 	FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
14217414705eSRobert Love 		    fc_exch_rctl_name(fh->fh_r_ctl));
142242e9a92fSRobert Love 
142342e9a92fSRobert Love 	if (cancel_delayed_work_sync(&ep->timeout_work))
142442e9a92fSRobert Love 		fc_exch_release(ep);	/* release from pending timer hold */
142542e9a92fSRobert Love 
142642e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
142742e9a92fSRobert Love 	switch (fh->fh_r_ctl) {
142842e9a92fSRobert Love 	case FC_RCTL_BA_ACC:
142942e9a92fSRobert Love 		ap = fc_frame_payload_get(fp, sizeof(*ap));
143042e9a92fSRobert Love 		if (!ap)
143142e9a92fSRobert Love 			break;
143242e9a92fSRobert Love 
143342e9a92fSRobert Love 		/*
143442e9a92fSRobert Love 		 * Decide whether to establish a Recovery Qualifier.
143542e9a92fSRobert Love 		 * We do this if there is a non-empty SEQ_CNT range and
143642e9a92fSRobert Love 		 * SEQ_ID is the same as the one we aborted.
143742e9a92fSRobert Love 		 */
143842e9a92fSRobert Love 		low = ntohs(ap->ba_low_seq_cnt);
143942e9a92fSRobert Love 		high = ntohs(ap->ba_high_seq_cnt);
144042e9a92fSRobert Love 		if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
144142e9a92fSRobert Love 		    (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
144242e9a92fSRobert Love 		     ap->ba_seq_id == ep->seq_id) && low != high) {
144342e9a92fSRobert Love 			ep->esb_stat |= ESB_ST_REC_QUAL;
144442e9a92fSRobert Love 			fc_exch_hold(ep);  /* hold for recovery qualifier */
144542e9a92fSRobert Love 			has_rec = 1;
144642e9a92fSRobert Love 		}
144742e9a92fSRobert Love 		break;
144842e9a92fSRobert Love 	case FC_RCTL_BA_RJT:
144942e9a92fSRobert Love 		break;
145042e9a92fSRobert Love 	default:
145142e9a92fSRobert Love 		break;
145242e9a92fSRobert Love 	}
145342e9a92fSRobert Love 
145442e9a92fSRobert Love 	resp = ep->resp;
145542e9a92fSRobert Love 	ex_resp_arg = ep->arg;
145642e9a92fSRobert Love 
145742e9a92fSRobert Love 	/* do we need to do some other checks here. Can we reuse more of
145842e9a92fSRobert Love 	 * fc_exch_recv_seq_resp
145942e9a92fSRobert Love 	 */
146042e9a92fSRobert Love 	sp = &ep->seq;
146142e9a92fSRobert Love 	/*
146242e9a92fSRobert Love 	 * do we want to check END_SEQ as well as LAST_SEQ here?
146342e9a92fSRobert Love 	 */
146442e9a92fSRobert Love 	if (ep->fh_type != FC_TYPE_FCP &&
146542e9a92fSRobert Love 	    ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
146642e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
146742e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
146842e9a92fSRobert Love 	if (!rc)
1469b2f0091fSVasu Dev 		fc_exch_delete(ep);
147042e9a92fSRobert Love 
147142e9a92fSRobert Love 	if (resp)
147242e9a92fSRobert Love 		resp(sp, fp, ex_resp_arg);
147342e9a92fSRobert Love 	else
147442e9a92fSRobert Love 		fc_frame_free(fp);
147542e9a92fSRobert Love 
147642e9a92fSRobert Love 	if (has_rec)
147742e9a92fSRobert Love 		fc_exch_timer_set(ep, ep->r_a_tov);
147842e9a92fSRobert Love 
147942e9a92fSRobert Love }
148042e9a92fSRobert Love 
14813a3b42bfSRobert Love /**
14823a3b42bfSRobert Love  * fc_exch_recv_bls() - Handler for a BLS sequence
14833a3b42bfSRobert Love  * @mp: The EM that the exchange is on
14843a3b42bfSRobert Love  * @fp: The request frame
14853a3b42bfSRobert Love  *
14863a3b42bfSRobert Love  * The BLS frame is always a sequence initiated by the remote side.
148742e9a92fSRobert Love  * We may be either the originator or recipient of the exchange.
148842e9a92fSRobert Love  */
148942e9a92fSRobert Love static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
149042e9a92fSRobert Love {
149142e9a92fSRobert Love 	struct fc_frame_header *fh;
149242e9a92fSRobert Love 	struct fc_exch *ep;
149342e9a92fSRobert Love 	u32 f_ctl;
149442e9a92fSRobert Love 
149542e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
149642e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
149742e9a92fSRobert Love 	fr_seq(fp) = NULL;
149842e9a92fSRobert Love 
149942e9a92fSRobert Love 	ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
150042e9a92fSRobert Love 			  ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
150142e9a92fSRobert Love 	if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
150242e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
150342e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
150442e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
150542e9a92fSRobert Love 	}
150642e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_CTX) {
150742e9a92fSRobert Love 		/*
150842e9a92fSRobert Love 		 * A response to a sequence we initiated.
150942e9a92fSRobert Love 		 * This should only be ACKs for class 2 or F.
151042e9a92fSRobert Love 		 */
151142e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
151242e9a92fSRobert Love 		case FC_RCTL_ACK_1:
151342e9a92fSRobert Love 		case FC_RCTL_ACK_0:
151442e9a92fSRobert Love 			break;
151542e9a92fSRobert Love 		default:
15167414705eSRobert Love 			FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
151742e9a92fSRobert Love 				    fh->fh_r_ctl,
151842e9a92fSRobert Love 				    fc_exch_rctl_name(fh->fh_r_ctl));
151942e9a92fSRobert Love 			break;
152042e9a92fSRobert Love 		}
152142e9a92fSRobert Love 		fc_frame_free(fp);
152242e9a92fSRobert Love 	} else {
152342e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
152442e9a92fSRobert Love 		case FC_RCTL_BA_RJT:
152542e9a92fSRobert Love 		case FC_RCTL_BA_ACC:
152642e9a92fSRobert Love 			if (ep)
152742e9a92fSRobert Love 				fc_exch_abts_resp(ep, fp);
152842e9a92fSRobert Love 			else
152942e9a92fSRobert Love 				fc_frame_free(fp);
153042e9a92fSRobert Love 			break;
153142e9a92fSRobert Love 		case FC_RCTL_BA_ABTS:
153242e9a92fSRobert Love 			fc_exch_recv_abts(ep, fp);
153342e9a92fSRobert Love 			break;
153442e9a92fSRobert Love 		default:			/* ignore junk */
153542e9a92fSRobert Love 			fc_frame_free(fp);
153642e9a92fSRobert Love 			break;
153742e9a92fSRobert Love 		}
153842e9a92fSRobert Love 	}
153942e9a92fSRobert Love 	if (ep)
154042e9a92fSRobert Love 		fc_exch_release(ep);	/* release hold taken by fc_exch_find */
154142e9a92fSRobert Love }
154242e9a92fSRobert Love 
15433a3b42bfSRobert Love /**
15443a3b42bfSRobert Love  * fc_seq_ls_acc() - Accept sequence with LS_ACC
15453a3b42bfSRobert Love  * @req_sp: The request sequence
15463a3b42bfSRobert Love  *
154742e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
154842e9a92fSRobert Love  * originator will repeat the sequence.
154942e9a92fSRobert Love  */
155042e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *req_sp)
155142e9a92fSRobert Love {
155242e9a92fSRobert Love 	struct fc_seq *sp;
155342e9a92fSRobert Love 	struct fc_els_ls_acc *acc;
155442e9a92fSRobert Love 	struct fc_frame *fp;
155542e9a92fSRobert Love 
155642e9a92fSRobert Love 	sp = fc_seq_start_next(req_sp);
155742e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
155842e9a92fSRobert Love 	if (fp) {
155942e9a92fSRobert Love 		acc = fc_frame_payload_get(fp, sizeof(*acc));
156042e9a92fSRobert Love 		memset(acc, 0, sizeof(*acc));
156142e9a92fSRobert Love 		acc->la_cmd = ELS_LS_ACC;
156242e9a92fSRobert Love 		fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
156342e9a92fSRobert Love 	}
156442e9a92fSRobert Love }
156542e9a92fSRobert Love 
15663a3b42bfSRobert Love /**
15673a3b42bfSRobert Love  * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
15683a3b42bfSRobert Love  * @req_sp: The request sequence
15693a3b42bfSRobert Love  * @reason: The reason the sequence is being rejected
15703a3b42bfSRobert Love  * @explan: The explaination for the rejection
15713a3b42bfSRobert Love  *
157242e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
157342e9a92fSRobert Love  * originator will repeat the sequence.
157442e9a92fSRobert Love  */
157542e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
157642e9a92fSRobert Love 			  enum fc_els_rjt_explan explan)
157742e9a92fSRobert Love {
157842e9a92fSRobert Love 	struct fc_seq *sp;
157942e9a92fSRobert Love 	struct fc_els_ls_rjt *rjt;
158042e9a92fSRobert Love 	struct fc_frame *fp;
158142e9a92fSRobert Love 
158242e9a92fSRobert Love 	sp = fc_seq_start_next(req_sp);
158342e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
158442e9a92fSRobert Love 	if (fp) {
158542e9a92fSRobert Love 		rjt = fc_frame_payload_get(fp, sizeof(*rjt));
158642e9a92fSRobert Love 		memset(rjt, 0, sizeof(*rjt));
158742e9a92fSRobert Love 		rjt->er_cmd = ELS_LS_RJT;
158842e9a92fSRobert Love 		rjt->er_reason = reason;
158942e9a92fSRobert Love 		rjt->er_explan = explan;
159042e9a92fSRobert Love 		fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
159142e9a92fSRobert Love 	}
159242e9a92fSRobert Love }
159342e9a92fSRobert Love 
15943a3b42bfSRobert Love /**
15953a3b42bfSRobert Love  * fc_exch_reset() - Reset an exchange
15963a3b42bfSRobert Love  * @ep: The exchange to be reset
15973a3b42bfSRobert Love  */
159842e9a92fSRobert Love static void fc_exch_reset(struct fc_exch *ep)
159942e9a92fSRobert Love {
160042e9a92fSRobert Love 	struct fc_seq *sp;
160142e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *, void *);
160242e9a92fSRobert Love 	void *arg;
160342e9a92fSRobert Love 	int rc = 1;
160442e9a92fSRobert Love 
160542e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
160642e9a92fSRobert Love 	ep->state |= FC_EX_RST_CLEANUP;
160742e9a92fSRobert Love 	if (cancel_delayed_work(&ep->timeout_work))
160842e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for timer */
160942e9a92fSRobert Love 	resp = ep->resp;
161042e9a92fSRobert Love 	ep->resp = NULL;
161142e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL)
161242e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec_qual */
161342e9a92fSRobert Love 	ep->esb_stat &= ~ESB_ST_REC_QUAL;
161442e9a92fSRobert Love 	arg = ep->arg;
161542e9a92fSRobert Love 	sp = &ep->seq;
161642e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
161742e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
161842e9a92fSRobert Love 	if (!rc)
1619b2f0091fSVasu Dev 		fc_exch_delete(ep);
162042e9a92fSRobert Love 
162142e9a92fSRobert Love 	if (resp)
162242e9a92fSRobert Love 		resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
162342e9a92fSRobert Love }
162442e9a92fSRobert Love 
1625b2f0091fSVasu Dev /**
16263a3b42bfSRobert Love  * fc_exch_pool_reset() - Reset a per cpu exchange pool
16273a3b42bfSRobert Love  * @lport: The local port that the exchange pool is on
16283a3b42bfSRobert Love  * @pool:  The exchange pool to be reset
16293a3b42bfSRobert Love  * @sid:   The source ID
16303a3b42bfSRobert Love  * @did:   The destination ID
1631b2f0091fSVasu Dev  *
16323a3b42bfSRobert Love  * Resets a per cpu exches pool, releasing all of its sequences
16333a3b42bfSRobert Love  * and exchanges. If sid is non-zero then reset only exchanges
16343a3b42bfSRobert Love  * we sourced from the local port's FID. If did is non-zero then
16353a3b42bfSRobert Love  * only reset exchanges destined for the local port's FID.
163642e9a92fSRobert Love  */
1637b2f0091fSVasu Dev static void fc_exch_pool_reset(struct fc_lport *lport,
1638b2f0091fSVasu Dev 			       struct fc_exch_pool *pool,
1639b2f0091fSVasu Dev 			       u32 sid, u32 did)
164042e9a92fSRobert Love {
164142e9a92fSRobert Love 	struct fc_exch *ep;
164242e9a92fSRobert Love 	struct fc_exch *next;
164342e9a92fSRobert Love 
1644b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
164542e9a92fSRobert Love restart:
1646b2f0091fSVasu Dev 	list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
1647b2f0091fSVasu Dev 		if ((lport == ep->lp) &&
164852ff878cSVasu Dev 		    (sid == 0 || sid == ep->sid) &&
164942e9a92fSRobert Love 		    (did == 0 || did == ep->did)) {
165042e9a92fSRobert Love 			fc_exch_hold(ep);
1651b2f0091fSVasu Dev 			spin_unlock_bh(&pool->lock);
165242e9a92fSRobert Love 
165342e9a92fSRobert Love 			fc_exch_reset(ep);
165442e9a92fSRobert Love 
165542e9a92fSRobert Love 			fc_exch_release(ep);
1656b2f0091fSVasu Dev 			spin_lock_bh(&pool->lock);
165742e9a92fSRobert Love 
165842e9a92fSRobert Love 			/*
165952ff878cSVasu Dev 			 * must restart loop incase while lock
166052ff878cSVasu Dev 			 * was down multiple eps were released.
166142e9a92fSRobert Love 			 */
166242e9a92fSRobert Love 			goto restart;
166342e9a92fSRobert Love 		}
166442e9a92fSRobert Love 	}
1665b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
1666b2f0091fSVasu Dev }
1667b2f0091fSVasu Dev 
1668b2f0091fSVasu Dev /**
16693a3b42bfSRobert Love  * fc_exch_mgr_reset() - Reset all EMs of a local port
16703a3b42bfSRobert Love  * @lport: The local port whose EMs are to be reset
16713a3b42bfSRobert Love  * @sid:   The source ID
16723a3b42bfSRobert Love  * @did:   The destination ID
1673b2f0091fSVasu Dev  *
16743a3b42bfSRobert Love  * Reset all EMs associated with a given local port. Release all
16753a3b42bfSRobert Love  * sequences and exchanges. If sid is non-zero then reset only the
16763a3b42bfSRobert Love  * exchanges sent from the local port's FID. If did is non-zero then
16773a3b42bfSRobert Love  * reset only exchanges destined for the local port's FID.
1678b2f0091fSVasu Dev  */
1679b2f0091fSVasu Dev void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1680b2f0091fSVasu Dev {
1681b2f0091fSVasu Dev 	struct fc_exch_mgr_anchor *ema;
1682b2f0091fSVasu Dev 	unsigned int cpu;
1683b2f0091fSVasu Dev 
1684b2f0091fSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
1685b2f0091fSVasu Dev 		for_each_possible_cpu(cpu)
1686b2f0091fSVasu Dev 			fc_exch_pool_reset(lport,
1687b2f0091fSVasu Dev 					   per_cpu_ptr(ema->mp->pool, cpu),
1688b2f0091fSVasu Dev 					   sid, did);
168942e9a92fSRobert Love 	}
169052ff878cSVasu Dev }
169142e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_reset);
169242e9a92fSRobert Love 
16933a3b42bfSRobert Love /**
16943a3b42bfSRobert Love  * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
16953a3b42bfSRobert Love  * @sp:	 The sequence the REC is on
16963a3b42bfSRobert Love  * @rfp: The REC frame
16973a3b42bfSRobert Love  *
169842e9a92fSRobert Love  * Note that the requesting port may be different than the S_ID in the request.
169942e9a92fSRobert Love  */
170042e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
170142e9a92fSRobert Love {
170242e9a92fSRobert Love 	struct fc_frame *fp;
170342e9a92fSRobert Love 	struct fc_exch *ep;
170442e9a92fSRobert Love 	struct fc_exch_mgr *em;
170542e9a92fSRobert Love 	struct fc_els_rec *rp;
170642e9a92fSRobert Love 	struct fc_els_rec_acc *acc;
170742e9a92fSRobert Love 	enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
170842e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
170942e9a92fSRobert Love 	u32 sid;
171042e9a92fSRobert Love 	u16 rxid;
171142e9a92fSRobert Love 	u16 oxid;
171242e9a92fSRobert Love 
171342e9a92fSRobert Love 	rp = fc_frame_payload_get(rfp, sizeof(*rp));
171442e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
171542e9a92fSRobert Love 	if (!rp)
171642e9a92fSRobert Love 		goto reject;
171742e9a92fSRobert Love 	sid = ntoh24(rp->rec_s_id);
171842e9a92fSRobert Love 	rxid = ntohs(rp->rec_rx_id);
171942e9a92fSRobert Love 	oxid = ntohs(rp->rec_ox_id);
172042e9a92fSRobert Love 
172142e9a92fSRobert Love 	/*
172242e9a92fSRobert Love 	 * Currently it's hard to find the local S_ID from the exchange
172342e9a92fSRobert Love 	 * manager.  This will eventually be fixed, but for now it's easier
172442e9a92fSRobert Love 	 * to lookup the subject exchange twice, once as if we were
172542e9a92fSRobert Love 	 * the initiator, and then again if we weren't.
172642e9a92fSRobert Love 	 */
172742e9a92fSRobert Love 	em = fc_seq_exch(sp)->em;
172842e9a92fSRobert Love 	ep = fc_exch_find(em, oxid);
172942e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
173042e9a92fSRobert Love 	if (ep && ep->oid == sid) {
173142e9a92fSRobert Love 		if (ep->rxid != FC_XID_UNKNOWN &&
173242e9a92fSRobert Love 		    rxid != FC_XID_UNKNOWN &&
173342e9a92fSRobert Love 		    ep->rxid != rxid)
173442e9a92fSRobert Love 			goto rel;
173542e9a92fSRobert Love 	} else {
173642e9a92fSRobert Love 		if (ep)
173742e9a92fSRobert Love 			fc_exch_release(ep);
173842e9a92fSRobert Love 		ep = NULL;
173942e9a92fSRobert Love 		if (rxid != FC_XID_UNKNOWN)
174042e9a92fSRobert Love 			ep = fc_exch_find(em, rxid);
174142e9a92fSRobert Love 		if (!ep)
174242e9a92fSRobert Love 			goto reject;
174342e9a92fSRobert Love 	}
174442e9a92fSRobert Love 
174542e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
174642e9a92fSRobert Love 	if (!fp) {
174742e9a92fSRobert Love 		fc_exch_done(sp);
174842e9a92fSRobert Love 		goto out;
174942e9a92fSRobert Love 	}
175042e9a92fSRobert Love 	acc = fc_frame_payload_get(fp, sizeof(*acc));
175142e9a92fSRobert Love 	memset(acc, 0, sizeof(*acc));
175242e9a92fSRobert Love 	acc->reca_cmd = ELS_LS_ACC;
175342e9a92fSRobert Love 	acc->reca_ox_id = rp->rec_ox_id;
175442e9a92fSRobert Love 	memcpy(acc->reca_ofid, rp->rec_s_id, 3);
175542e9a92fSRobert Love 	acc->reca_rx_id = htons(ep->rxid);
175642e9a92fSRobert Love 	if (ep->sid == ep->oid)
175742e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->did);
175842e9a92fSRobert Love 	else
175942e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->sid);
176042e9a92fSRobert Love 	acc->reca_fc4value = htonl(ep->seq.rec_data);
176142e9a92fSRobert Love 	acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
176242e9a92fSRobert Love 						 ESB_ST_SEQ_INIT |
176342e9a92fSRobert Love 						 ESB_ST_COMPLETE));
176442e9a92fSRobert Love 	sp = fc_seq_start_next(sp);
176542e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
176642e9a92fSRobert Love out:
176742e9a92fSRobert Love 	fc_exch_release(ep);
176842e9a92fSRobert Love 	fc_frame_free(rfp);
176942e9a92fSRobert Love 	return;
177042e9a92fSRobert Love 
177142e9a92fSRobert Love rel:
177242e9a92fSRobert Love 	fc_exch_release(ep);
177342e9a92fSRobert Love reject:
177442e9a92fSRobert Love 	fc_seq_ls_rjt(sp, reason, explan);
177542e9a92fSRobert Love 	fc_frame_free(rfp);
177642e9a92fSRobert Love }
177742e9a92fSRobert Love 
17783a3b42bfSRobert Love /**
17793a3b42bfSRobert Love  * fc_exch_rrq_resp() - Handler for RRQ responses
17803a3b42bfSRobert Love  * @sp:	 The sequence that the RRQ is on
17813a3b42bfSRobert Love  * @fp:	 The RRQ frame
17823a3b42bfSRobert Love  * @arg: The exchange that the RRQ is on
178342e9a92fSRobert Love  *
178442e9a92fSRobert Love  * TODO: fix error handler.
178542e9a92fSRobert Love  */
178642e9a92fSRobert Love static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
178742e9a92fSRobert Love {
178842e9a92fSRobert Love 	struct fc_exch *aborted_ep = arg;
178942e9a92fSRobert Love 	unsigned int op;
179042e9a92fSRobert Love 
179142e9a92fSRobert Love 	if (IS_ERR(fp)) {
179242e9a92fSRobert Love 		int err = PTR_ERR(fp);
179342e9a92fSRobert Love 
179478342da3SVasu Dev 		if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
179542e9a92fSRobert Love 			goto cleanup;
17967414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
17977414705eSRobert Love 			    "frame error %d\n", err);
179842e9a92fSRobert Love 		return;
179942e9a92fSRobert Love 	}
180042e9a92fSRobert Love 
180142e9a92fSRobert Love 	op = fc_frame_payload_op(fp);
180242e9a92fSRobert Love 	fc_frame_free(fp);
180342e9a92fSRobert Love 
180442e9a92fSRobert Love 	switch (op) {
180542e9a92fSRobert Love 	case ELS_LS_RJT:
18067414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
180742e9a92fSRobert Love 		/* fall through */
180842e9a92fSRobert Love 	case ELS_LS_ACC:
180942e9a92fSRobert Love 		goto cleanup;
181042e9a92fSRobert Love 	default:
18117414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
18127414705eSRobert Love 			    "for RRQ", op);
181342e9a92fSRobert Love 		return;
181442e9a92fSRobert Love 	}
181542e9a92fSRobert Love 
181642e9a92fSRobert Love cleanup:
181742e9a92fSRobert Love 	fc_exch_done(&aborted_ep->seq);
181842e9a92fSRobert Love 	/* drop hold for rec qual */
181942e9a92fSRobert Love 	fc_exch_release(aborted_ep);
182042e9a92fSRobert Love }
182142e9a92fSRobert Love 
18221a7b75aeSRobert Love 
18231a7b75aeSRobert Love /**
18243a3b42bfSRobert Love  * fc_exch_seq_send() - Send a frame using a new exchange and sequence
18253a3b42bfSRobert Love  * @lport:	The local port to send the frame on
18263a3b42bfSRobert Love  * @fp:		The frame to be sent
18273a3b42bfSRobert Love  * @resp:	The response handler for this request
18283a3b42bfSRobert Love  * @destructor: The destructor for the exchange
18293a3b42bfSRobert Love  * @arg:	The argument to be passed to the response handler
18303a3b42bfSRobert Love  * @timer_msec: The timeout period for the exchange
18313a3b42bfSRobert Love  *
18323a3b42bfSRobert Love  * The frame pointer with some of the header's fields must be
18333a3b42bfSRobert Love  * filled before calling this routine, those fields are:
18343a3b42bfSRobert Love  *
18353a3b42bfSRobert Love  * - routing control
18363a3b42bfSRobert Love  * - FC port did
18373a3b42bfSRobert Love  * - FC port sid
18383a3b42bfSRobert Love  * - FC header type
18393a3b42bfSRobert Love  * - frame control
18403a3b42bfSRobert Love  * - parameter or relative offset
18411a7b75aeSRobert Love  */
18423a3b42bfSRobert Love static struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
18431a7b75aeSRobert Love 				       struct fc_frame *fp,
18441a7b75aeSRobert Love 				       void (*resp)(struct fc_seq *,
18451a7b75aeSRobert Love 						    struct fc_frame *fp,
18461a7b75aeSRobert Love 						    void *arg),
18471a7b75aeSRobert Love 				       void (*destructor)(struct fc_seq *,
18481a7b75aeSRobert Love 							  void *),
18491a7b75aeSRobert Love 				       void *arg, u32 timer_msec)
18501a7b75aeSRobert Love {
18511a7b75aeSRobert Love 	struct fc_exch *ep;
18521a7b75aeSRobert Love 	struct fc_seq *sp = NULL;
18531a7b75aeSRobert Love 	struct fc_frame_header *fh;
18541a7b75aeSRobert Love 	int rc = 1;
18551a7b75aeSRobert Love 
18563a3b42bfSRobert Love 	ep = fc_exch_alloc(lport, fp);
18571a7b75aeSRobert Love 	if (!ep) {
18581a7b75aeSRobert Love 		fc_frame_free(fp);
18591a7b75aeSRobert Love 		return NULL;
18601a7b75aeSRobert Love 	}
18611a7b75aeSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT;
18621a7b75aeSRobert Love 	fh = fc_frame_header_get(fp);
18631a7b75aeSRobert Love 	fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
18641a7b75aeSRobert Love 	ep->resp = resp;
18651a7b75aeSRobert Love 	ep->destructor = destructor;
18661a7b75aeSRobert Love 	ep->arg = arg;
18671a7b75aeSRobert Love 	ep->r_a_tov = FC_DEF_R_A_TOV;
18683a3b42bfSRobert Love 	ep->lp = lport;
18691a7b75aeSRobert Love 	sp = &ep->seq;
18701a7b75aeSRobert Love 
18711a7b75aeSRobert Love 	ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
18721a7b75aeSRobert Love 	ep->f_ctl = ntoh24(fh->fh_f_ctl);
18731a7b75aeSRobert Love 	fc_exch_setup_hdr(ep, fp, ep->f_ctl);
18741a7b75aeSRobert Love 	sp->cnt++;
18751a7b75aeSRobert Love 
187610897ae7SVasu Dev 	if (ep->xid <= lport->lro_xid && fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD)
18771a7b75aeSRobert Love 		fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
18781a7b75aeSRobert Love 
18793a3b42bfSRobert Love 	if (unlikely(lport->tt.frame_send(lport, fp)))
18801a7b75aeSRobert Love 		goto err;
18811a7b75aeSRobert Love 
18821a7b75aeSRobert Love 	if (timer_msec)
18831a7b75aeSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
18841a7b75aeSRobert Love 	ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not first seq */
18851a7b75aeSRobert Love 
18861a7b75aeSRobert Love 	if (ep->f_ctl & FC_FC_SEQ_INIT)
18871a7b75aeSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
18881a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
18891a7b75aeSRobert Love 	return sp;
18901a7b75aeSRobert Love err:
18911a7b75aeSRobert Love 	rc = fc_exch_done_locked(ep);
18921a7b75aeSRobert Love 	spin_unlock_bh(&ep->ex_lock);
18931a7b75aeSRobert Love 	if (!rc)
18941a7b75aeSRobert Love 		fc_exch_delete(ep);
18951a7b75aeSRobert Love 	return NULL;
18961a7b75aeSRobert Love }
18971a7b75aeSRobert Love 
18983a3b42bfSRobert Love /**
18993a3b42bfSRobert Love  * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
19003a3b42bfSRobert Love  * @ep: The exchange to send the RRQ on
19013a3b42bfSRobert Love  *
190242e9a92fSRobert Love  * This tells the remote port to stop blocking the use of
190342e9a92fSRobert Love  * the exchange and the seq_cnt range.
190442e9a92fSRobert Love  */
190542e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *ep)
190642e9a92fSRobert Love {
19073a3b42bfSRobert Love 	struct fc_lport *lport;
190842e9a92fSRobert Love 	struct fc_els_rrq *rrq;
190942e9a92fSRobert Love 	struct fc_frame *fp;
191042e9a92fSRobert Love 	u32 did;
191142e9a92fSRobert Love 
19123a3b42bfSRobert Love 	lport = ep->lp;
191342e9a92fSRobert Love 
19143a3b42bfSRobert Love 	fp = fc_frame_alloc(lport, sizeof(*rrq));
191542e9a92fSRobert Love 	if (!fp)
1916a0cc1eccSVasu Dev 		goto retry;
1917a0cc1eccSVasu Dev 
191842e9a92fSRobert Love 	rrq = fc_frame_payload_get(fp, sizeof(*rrq));
191942e9a92fSRobert Love 	memset(rrq, 0, sizeof(*rrq));
192042e9a92fSRobert Love 	rrq->rrq_cmd = ELS_RRQ;
192142e9a92fSRobert Love 	hton24(rrq->rrq_s_id, ep->sid);
192242e9a92fSRobert Love 	rrq->rrq_ox_id = htons(ep->oxid);
192342e9a92fSRobert Love 	rrq->rrq_rx_id = htons(ep->rxid);
192442e9a92fSRobert Love 
192542e9a92fSRobert Love 	did = ep->did;
192642e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP)
192742e9a92fSRobert Love 		did = ep->sid;
192842e9a92fSRobert Love 
192942e9a92fSRobert Love 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1930*7b2787ecSRobert Love 		       lport->port_id, FC_TYPE_ELS,
193142e9a92fSRobert Love 		       FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
193242e9a92fSRobert Love 
19333a3b42bfSRobert Love 	if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
19343a3b42bfSRobert Love 			     lport->e_d_tov))
1935a0cc1eccSVasu Dev 		return;
1936a0cc1eccSVasu Dev 
1937a0cc1eccSVasu Dev retry:
1938a0cc1eccSVasu Dev 	spin_lock_bh(&ep->ex_lock);
1939a0cc1eccSVasu Dev 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1940a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
1941a0cc1eccSVasu Dev 		/* drop hold for rec qual */
1942a0cc1eccSVasu Dev 		fc_exch_release(ep);
194342e9a92fSRobert Love 		return;
194442e9a92fSRobert Love 	}
1945a0cc1eccSVasu Dev 	ep->esb_stat |= ESB_ST_REC_QUAL;
1946a0cc1eccSVasu Dev 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
1947a0cc1eccSVasu Dev 	spin_unlock_bh(&ep->ex_lock);
194842e9a92fSRobert Love }
194942e9a92fSRobert Love 
195042e9a92fSRobert Love 
19513a3b42bfSRobert Love /**
19523a3b42bfSRobert Love  * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
19533a3b42bfSRobert Love  * @sp: The sequence that the RRQ is on
19543a3b42bfSRobert Love  * @fp: The RRQ frame
195542e9a92fSRobert Love  */
195642e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
195742e9a92fSRobert Love {
19583f127ad9SVasu Dev 	struct fc_exch *ep = NULL;	/* request or subject exchange */
195942e9a92fSRobert Love 	struct fc_els_rrq *rp;
196042e9a92fSRobert Love 	u32 sid;
196142e9a92fSRobert Love 	u16 xid;
196242e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
196342e9a92fSRobert Love 
196442e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
196542e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
196642e9a92fSRobert Love 	if (!rp)
196742e9a92fSRobert Love 		goto reject;
196842e9a92fSRobert Love 
196942e9a92fSRobert Love 	/*
197042e9a92fSRobert Love 	 * lookup subject exchange.
197142e9a92fSRobert Love 	 */
197242e9a92fSRobert Love 	ep = fc_seq_exch(sp);
197342e9a92fSRobert Love 	sid = ntoh24(rp->rrq_s_id);		/* subject source */
197442e9a92fSRobert Love 	xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
197542e9a92fSRobert Love 	ep = fc_exch_find(ep->em, xid);
197642e9a92fSRobert Love 
197742e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
197842e9a92fSRobert Love 	if (!ep)
197942e9a92fSRobert Love 		goto reject;
198042e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
198142e9a92fSRobert Love 	if (ep->oxid != ntohs(rp->rrq_ox_id))
198242e9a92fSRobert Love 		goto unlock_reject;
198342e9a92fSRobert Love 	if (ep->rxid != ntohs(rp->rrq_rx_id) &&
198442e9a92fSRobert Love 	    ep->rxid != FC_XID_UNKNOWN)
198542e9a92fSRobert Love 		goto unlock_reject;
198642e9a92fSRobert Love 	explan = ELS_EXPL_SID;
198742e9a92fSRobert Love 	if (ep->sid != sid)
198842e9a92fSRobert Love 		goto unlock_reject;
198942e9a92fSRobert Love 
199042e9a92fSRobert Love 	/*
199142e9a92fSRobert Love 	 * Clear Recovery Qualifier state, and cancel timer if complete.
199242e9a92fSRobert Love 	 */
199342e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL) {
199442e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_REC_QUAL;
199542e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec qual */
199642e9a92fSRobert Love 	}
199742e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
199842e9a92fSRobert Love 		if (cancel_delayed_work(&ep->timeout_work))
199942e9a92fSRobert Love 			atomic_dec(&ep->ex_refcnt);	/* drop timer hold */
200042e9a92fSRobert Love 	}
200142e9a92fSRobert Love 
200242e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
200342e9a92fSRobert Love 
200442e9a92fSRobert Love 	/*
200542e9a92fSRobert Love 	 * Send LS_ACC.
200642e9a92fSRobert Love 	 */
200742e9a92fSRobert Love 	fc_seq_ls_acc(sp);
20083f127ad9SVasu Dev 	goto out;
200942e9a92fSRobert Love 
201042e9a92fSRobert Love unlock_reject:
201142e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
201242e9a92fSRobert Love reject:
201342e9a92fSRobert Love 	fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
20143f127ad9SVasu Dev out:
201542e9a92fSRobert Love 	fc_frame_free(fp);
20163f127ad9SVasu Dev 	if (ep)
20173f127ad9SVasu Dev 		fc_exch_release(ep);	/* drop hold from fc_exch_find */
201842e9a92fSRobert Love }
201942e9a92fSRobert Love 
20203a3b42bfSRobert Love /**
20213a3b42bfSRobert Love  * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
20223a3b42bfSRobert Love  * @lport: The local port to add the exchange manager to
20233a3b42bfSRobert Love  * @mp:	   The exchange manager to be added to the local port
20243a3b42bfSRobert Love  * @match: The match routine that indicates when this EM should be used
20253a3b42bfSRobert Love  */
202696316099SVasu Dev struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
202796316099SVasu Dev 					   struct fc_exch_mgr *mp,
202896316099SVasu Dev 					   bool (*match)(struct fc_frame *))
202996316099SVasu Dev {
203096316099SVasu Dev 	struct fc_exch_mgr_anchor *ema;
203196316099SVasu Dev 
203296316099SVasu Dev 	ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
203396316099SVasu Dev 	if (!ema)
203496316099SVasu Dev 		return ema;
203596316099SVasu Dev 
203696316099SVasu Dev 	ema->mp = mp;
203796316099SVasu Dev 	ema->match = match;
203896316099SVasu Dev 	/* add EM anchor to EM anchors list */
203996316099SVasu Dev 	list_add_tail(&ema->ema_list, &lport->ema_list);
204096316099SVasu Dev 	kref_get(&mp->kref);
204196316099SVasu Dev 	return ema;
204296316099SVasu Dev }
204396316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_add);
204496316099SVasu Dev 
20453a3b42bfSRobert Love /**
20463a3b42bfSRobert Love  * fc_exch_mgr_destroy() - Destroy an exchange manager
20473a3b42bfSRobert Love  * @kref: The reference to the EM to be destroyed
20483a3b42bfSRobert Love  */
204996316099SVasu Dev static void fc_exch_mgr_destroy(struct kref *kref)
205096316099SVasu Dev {
205196316099SVasu Dev 	struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
205296316099SVasu Dev 
205396316099SVasu Dev 	mempool_destroy(mp->ep_pool);
2054e4bc50beSVasu Dev 	free_percpu(mp->pool);
205596316099SVasu Dev 	kfree(mp);
205696316099SVasu Dev }
205796316099SVasu Dev 
20583a3b42bfSRobert Love /**
20593a3b42bfSRobert Love  * fc_exch_mgr_del() - Delete an EM from a local port's list
20603a3b42bfSRobert Love  * @ema: The exchange manager anchor identifying the EM to be deleted
20613a3b42bfSRobert Love  */
206296316099SVasu Dev void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
206396316099SVasu Dev {
206496316099SVasu Dev 	/* remove EM anchor from EM anchors list */
206596316099SVasu Dev 	list_del(&ema->ema_list);
206696316099SVasu Dev 	kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
206796316099SVasu Dev 	kfree(ema);
206896316099SVasu Dev }
206996316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_del);
207096316099SVasu Dev 
2071174e1ebfSChris Leech /**
20723a3b42bfSRobert Love  * fc_exch_mgr_list_clone() - Share all exchange manager objects
20733a3b42bfSRobert Love  * @src: Source lport to clone exchange managers from
20743a3b42bfSRobert Love  * @dst: New lport that takes references to all the exchange managers
2075174e1ebfSChris Leech  */
2076174e1ebfSChris Leech int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
2077174e1ebfSChris Leech {
2078174e1ebfSChris Leech 	struct fc_exch_mgr_anchor *ema, *tmp;
2079174e1ebfSChris Leech 
2080174e1ebfSChris Leech 	list_for_each_entry(ema, &src->ema_list, ema_list) {
2081174e1ebfSChris Leech 		if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
2082174e1ebfSChris Leech 			goto err;
2083174e1ebfSChris Leech 	}
2084174e1ebfSChris Leech 	return 0;
2085174e1ebfSChris Leech err:
2086174e1ebfSChris Leech 	list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
2087174e1ebfSChris Leech 		fc_exch_mgr_del(ema);
2088174e1ebfSChris Leech 	return -ENOMEM;
2089174e1ebfSChris Leech }
2090174e1ebfSChris Leech 
20913a3b42bfSRobert Love /**
20923a3b42bfSRobert Love  * fc_exch_mgr_alloc() - Allocate an exchange manager
20933a3b42bfSRobert Love  * @lport:   The local port that the new EM will be associated with
20943a3b42bfSRobert Love  * @class:   The default FC class for new exchanges
20953a3b42bfSRobert Love  * @min_xid: The minimum XID for exchanges from the new EM
20963a3b42bfSRobert Love  * @max_xid: The maximum XID for exchanges from the new EM
20973a3b42bfSRobert Love  * @match:   The match routine for the new EM
20983a3b42bfSRobert Love  */
20993a3b42bfSRobert Love struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
210042e9a92fSRobert Love 				      enum fc_class class,
210152ff878cSVasu Dev 				      u16 min_xid, u16 max_xid,
210252ff878cSVasu Dev 				      bool (*match)(struct fc_frame *))
210342e9a92fSRobert Love {
210442e9a92fSRobert Love 	struct fc_exch_mgr *mp;
2105e4bc50beSVasu Dev 	u16 pool_exch_range;
2106e4bc50beSVasu Dev 	size_t pool_size;
2107e4bc50beSVasu Dev 	unsigned int cpu;
2108e4bc50beSVasu Dev 	struct fc_exch_pool *pool;
210942e9a92fSRobert Love 
2110e4bc50beSVasu Dev 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
2111e4bc50beSVasu Dev 	    (min_xid & fc_cpu_mask) != 0) {
21123a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
211342e9a92fSRobert Love 			     min_xid, max_xid);
211442e9a92fSRobert Love 		return NULL;
211542e9a92fSRobert Love 	}
211642e9a92fSRobert Love 
211742e9a92fSRobert Love 	/*
2118b2f0091fSVasu Dev 	 * allocate memory for EM
211942e9a92fSRobert Love 	 */
2120b2f0091fSVasu Dev 	mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
212142e9a92fSRobert Love 	if (!mp)
212242e9a92fSRobert Love 		return NULL;
212342e9a92fSRobert Love 
212442e9a92fSRobert Love 	mp->class = class;
212542e9a92fSRobert Love 	/* adjust em exch xid range for offload */
212642e9a92fSRobert Love 	mp->min_xid = min_xid;
212742e9a92fSRobert Love 	mp->max_xid = max_xid;
212842e9a92fSRobert Love 
212942e9a92fSRobert Love 	mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
213042e9a92fSRobert Love 	if (!mp->ep_pool)
213142e9a92fSRobert Love 		goto free_mp;
213242e9a92fSRobert Love 
2133e4bc50beSVasu Dev 	/*
2134e4bc50beSVasu Dev 	 * Setup per cpu exch pool with entire exchange id range equally
2135e4bc50beSVasu Dev 	 * divided across all cpus. The exch pointers array memory is
2136e4bc50beSVasu Dev 	 * allocated for exch range per pool.
2137e4bc50beSVasu Dev 	 */
2138e4bc50beSVasu Dev 	pool_exch_range = (mp->max_xid - mp->min_xid + 1) / (fc_cpu_mask + 1);
2139e4bc50beSVasu Dev 	mp->pool_max_index = pool_exch_range - 1;
2140e4bc50beSVasu Dev 
2141e4bc50beSVasu Dev 	/*
2142e4bc50beSVasu Dev 	 * Allocate and initialize per cpu exch pool
2143e4bc50beSVasu Dev 	 */
2144e4bc50beSVasu Dev 	pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
2145e4bc50beSVasu Dev 	mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
2146e4bc50beSVasu Dev 	if (!mp->pool)
2147e4bc50beSVasu Dev 		goto free_mempool;
2148e4bc50beSVasu Dev 	for_each_possible_cpu(cpu) {
2149e4bc50beSVasu Dev 		pool = per_cpu_ptr(mp->pool, cpu);
2150e4bc50beSVasu Dev 		spin_lock_init(&pool->lock);
2151e4bc50beSVasu Dev 		INIT_LIST_HEAD(&pool->ex_list);
2152e4bc50beSVasu Dev 	}
2153e4bc50beSVasu Dev 
215452ff878cSVasu Dev 	kref_init(&mp->kref);
21553a3b42bfSRobert Love 	if (!fc_exch_mgr_add(lport, mp, match)) {
2156e4bc50beSVasu Dev 		free_percpu(mp->pool);
2157e4bc50beSVasu Dev 		goto free_mempool;
215852ff878cSVasu Dev 	}
215952ff878cSVasu Dev 
216052ff878cSVasu Dev 	/*
216152ff878cSVasu Dev 	 * Above kref_init() sets mp->kref to 1 and then
216252ff878cSVasu Dev 	 * call to fc_exch_mgr_add incremented mp->kref again,
216352ff878cSVasu Dev 	 * so adjust that extra increment.
216452ff878cSVasu Dev 	 */
216552ff878cSVasu Dev 	kref_put(&mp->kref, fc_exch_mgr_destroy);
216642e9a92fSRobert Love 	return mp;
216742e9a92fSRobert Love 
2168e4bc50beSVasu Dev free_mempool:
2169e4bc50beSVasu Dev 	mempool_destroy(mp->ep_pool);
217042e9a92fSRobert Love free_mp:
217142e9a92fSRobert Love 	kfree(mp);
217242e9a92fSRobert Love 	return NULL;
217342e9a92fSRobert Love }
217442e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_alloc);
217542e9a92fSRobert Love 
21763a3b42bfSRobert Love /**
21773a3b42bfSRobert Love  * fc_exch_mgr_free() - Free all exchange managers on a local port
21783a3b42bfSRobert Love  * @lport: The local port whose EMs are to be freed
21793a3b42bfSRobert Love  */
218052ff878cSVasu Dev void fc_exch_mgr_free(struct fc_lport *lport)
218142e9a92fSRobert Love {
218252ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema, *next;
218352ff878cSVasu Dev 
21844ae1e19fSVasu Dev 	flush_workqueue(fc_exch_workqueue);
218552ff878cSVasu Dev 	list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
218652ff878cSVasu Dev 		fc_exch_mgr_del(ema);
218742e9a92fSRobert Love }
218842e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_free);
218942e9a92fSRobert Love 
21903a3b42bfSRobert Love /**
21913a3b42bfSRobert Love  * fc_exch_recv() - Handler for received frames
21923a3b42bfSRobert Love  * @lport: The local port the frame was received on
21933a3b42bfSRobert Love  * @fp:	   The received frame
219442e9a92fSRobert Love  */
21953a3b42bfSRobert Love void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
219642e9a92fSRobert Love {
219742e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
219852ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
219952ff878cSVasu Dev 	u32 f_ctl, found = 0;
220052ff878cSVasu Dev 	u16 oxid;
220142e9a92fSRobert Love 
220242e9a92fSRobert Love 	/* lport lock ? */
22033a3b42bfSRobert Love 	if (!lport || lport->state == LPORT_ST_DISABLED) {
22043a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "Receiving frames for an lport that "
22057414705eSRobert Love 			     "has not been initialized correctly\n");
220642e9a92fSRobert Love 		fc_frame_free(fp);
220742e9a92fSRobert Love 		return;
220842e9a92fSRobert Love 	}
220942e9a92fSRobert Love 
221052ff878cSVasu Dev 	f_ctl = ntoh24(fh->fh_f_ctl);
221152ff878cSVasu Dev 	oxid = ntohs(fh->fh_ox_id);
221252ff878cSVasu Dev 	if (f_ctl & FC_FC_EX_CTX) {
22133a3b42bfSRobert Love 		list_for_each_entry(ema, &lport->ema_list, ema_list) {
221452ff878cSVasu Dev 			if ((oxid >= ema->mp->min_xid) &&
221552ff878cSVasu Dev 			    (oxid <= ema->mp->max_xid)) {
221652ff878cSVasu Dev 				found = 1;
221752ff878cSVasu Dev 				break;
221852ff878cSVasu Dev 			}
221952ff878cSVasu Dev 		}
222052ff878cSVasu Dev 
222152ff878cSVasu Dev 		if (!found) {
22223a3b42bfSRobert Love 			FC_LPORT_DBG(lport, "Received response for out "
222352ff878cSVasu Dev 				     "of range oxid:%hx\n", oxid);
222452ff878cSVasu Dev 			fc_frame_free(fp);
222552ff878cSVasu Dev 			return;
222652ff878cSVasu Dev 		}
222752ff878cSVasu Dev 	} else
22283a3b42bfSRobert Love 		ema = list_entry(lport->ema_list.prev, typeof(*ema), ema_list);
222952ff878cSVasu Dev 
223042e9a92fSRobert Love 	/*
223142e9a92fSRobert Love 	 * If frame is marked invalid, just drop it.
223242e9a92fSRobert Love 	 */
223342e9a92fSRobert Love 	switch (fr_eof(fp)) {
223442e9a92fSRobert Love 	case FC_EOF_T:
223542e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
223642e9a92fSRobert Love 			skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
223742e9a92fSRobert Love 		/* fall through */
223842e9a92fSRobert Love 	case FC_EOF_N:
223942e9a92fSRobert Love 		if (fh->fh_type == FC_TYPE_BLS)
224052ff878cSVasu Dev 			fc_exch_recv_bls(ema->mp, fp);
224142e9a92fSRobert Love 		else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
224242e9a92fSRobert Love 			 FC_FC_EX_CTX)
224352ff878cSVasu Dev 			fc_exch_recv_seq_resp(ema->mp, fp);
224442e9a92fSRobert Love 		else if (f_ctl & FC_FC_SEQ_CTX)
224552ff878cSVasu Dev 			fc_exch_recv_resp(ema->mp, fp);
224642e9a92fSRobert Love 		else
22473a3b42bfSRobert Love 			fc_exch_recv_req(lport, ema->mp, fp);
224842e9a92fSRobert Love 		break;
224942e9a92fSRobert Love 	default:
22503a3b42bfSRobert Love 		FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
22513a3b42bfSRobert Love 			     fr_eof(fp));
225242e9a92fSRobert Love 		fc_frame_free(fp);
225342e9a92fSRobert Love 	}
225442e9a92fSRobert Love }
225542e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_recv);
225642e9a92fSRobert Love 
22573a3b42bfSRobert Love /**
22583a3b42bfSRobert Love  * fc_exch_init() - Initialize the exchange layer for a local port
22593a3b42bfSRobert Love  * @lport: The local port to initialize the exchange layer for
22603a3b42bfSRobert Love  */
22613a3b42bfSRobert Love int fc_exch_init(struct fc_lport *lport)
226242e9a92fSRobert Love {
22633a3b42bfSRobert Love 	if (!lport->tt.seq_start_next)
22643a3b42bfSRobert Love 		lport->tt.seq_start_next = fc_seq_start_next;
226542e9a92fSRobert Love 
22663a3b42bfSRobert Love 	if (!lport->tt.exch_seq_send)
22673a3b42bfSRobert Love 		lport->tt.exch_seq_send = fc_exch_seq_send;
226842e9a92fSRobert Love 
22693a3b42bfSRobert Love 	if (!lport->tt.seq_send)
22703a3b42bfSRobert Love 		lport->tt.seq_send = fc_seq_send;
227142e9a92fSRobert Love 
22723a3b42bfSRobert Love 	if (!lport->tt.seq_els_rsp_send)
22733a3b42bfSRobert Love 		lport->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
227442e9a92fSRobert Love 
22753a3b42bfSRobert Love 	if (!lport->tt.exch_done)
22763a3b42bfSRobert Love 		lport->tt.exch_done = fc_exch_done;
227742e9a92fSRobert Love 
22783a3b42bfSRobert Love 	if (!lport->tt.exch_mgr_reset)
22793a3b42bfSRobert Love 		lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
228042e9a92fSRobert Love 
22813a3b42bfSRobert Love 	if (!lport->tt.seq_exch_abort)
22823a3b42bfSRobert Love 		lport->tt.seq_exch_abort = fc_seq_exch_abort;
228342e9a92fSRobert Love 
228489f19a59SVasu Dev 	return 0;
228589f19a59SVasu Dev }
228689f19a59SVasu Dev EXPORT_SYMBOL(fc_exch_init);
228789f19a59SVasu Dev 
228889f19a59SVasu Dev /**
228989f19a59SVasu Dev  * fc_setup_exch_mgr() - Setup an exchange manager
229089f19a59SVasu Dev  */
229189f19a59SVasu Dev int fc_setup_exch_mgr()
229289f19a59SVasu Dev {
229389f19a59SVasu Dev 	fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
229489f19a59SVasu Dev 					 0, SLAB_HWCACHE_ALIGN, NULL);
229589f19a59SVasu Dev 	if (!fc_em_cachep)
229689f19a59SVasu Dev 		return -ENOMEM;
229789f19a59SVasu Dev 
2298e4bc50beSVasu Dev 	/*
2299e4bc50beSVasu Dev 	 * Initialize fc_cpu_mask and fc_cpu_order. The
2300e4bc50beSVasu Dev 	 * fc_cpu_mask is set for nr_cpu_ids rounded up
2301e4bc50beSVasu Dev 	 * to order of 2's * power and order is stored
2302e4bc50beSVasu Dev 	 * in fc_cpu_order as this is later required in
2303e4bc50beSVasu Dev 	 * mapping between an exch id and exch array index
2304e4bc50beSVasu Dev 	 * in per cpu exch pool.
2305e4bc50beSVasu Dev 	 *
2306e4bc50beSVasu Dev 	 * This round up is required to align fc_cpu_mask
2307e4bc50beSVasu Dev 	 * to exchange id's lower bits such that all incoming
2308e4bc50beSVasu Dev 	 * frames of an exchange gets delivered to the same
2309e4bc50beSVasu Dev 	 * cpu on which exchange originated by simple bitwise
2310e4bc50beSVasu Dev 	 * AND operation between fc_cpu_mask and exchange id.
2311e4bc50beSVasu Dev 	 */
2312e4bc50beSVasu Dev 	fc_cpu_mask = 1;
2313e4bc50beSVasu Dev 	fc_cpu_order = 0;
2314e4bc50beSVasu Dev 	while (fc_cpu_mask < nr_cpu_ids) {
2315e4bc50beSVasu Dev 		fc_cpu_mask <<= 1;
2316e4bc50beSVasu Dev 		fc_cpu_order++;
2317e4bc50beSVasu Dev 	}
2318e4bc50beSVasu Dev 	fc_cpu_mask--;
2319e4bc50beSVasu Dev 
23204ae1e19fSVasu Dev 	fc_exch_workqueue = create_singlethread_workqueue("fc_exch_workqueue");
23214ae1e19fSVasu Dev 	if (!fc_exch_workqueue)
23224ae1e19fSVasu Dev 		return -ENOMEM;
232342e9a92fSRobert Love 	return 0;
232442e9a92fSRobert Love }
232542e9a92fSRobert Love 
23263a3b42bfSRobert Love /**
23273a3b42bfSRobert Love  * fc_destroy_exch_mgr() - Destroy an exchange manager
23283a3b42bfSRobert Love  */
23293a3b42bfSRobert Love void fc_destroy_exch_mgr()
233042e9a92fSRobert Love {
23314ae1e19fSVasu Dev 	destroy_workqueue(fc_exch_workqueue);
233242e9a92fSRobert Love 	kmem_cache_destroy(fc_em_cachep);
233342e9a92fSRobert Love }
2334