xref: /linux/drivers/scsi/libfc/fc_exch.c (revision 3f127ad97a985d43b3cdf4b644e77a775b6035d4)
142e9a92fSRobert Love /*
242e9a92fSRobert Love  * Copyright(c) 2007 Intel Corporation. All rights reserved.
342e9a92fSRobert Love  * Copyright(c) 2008 Red Hat, Inc.  All rights reserved.
442e9a92fSRobert Love  * Copyright(c) 2008 Mike Christie
542e9a92fSRobert Love  *
642e9a92fSRobert Love  * This program is free software; you can redistribute it and/or modify it
742e9a92fSRobert Love  * under the terms and conditions of the GNU General Public License,
842e9a92fSRobert Love  * version 2, as published by the Free Software Foundation.
942e9a92fSRobert Love  *
1042e9a92fSRobert Love  * This program is distributed in the hope it will be useful, but WITHOUT
1142e9a92fSRobert Love  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1242e9a92fSRobert Love  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1342e9a92fSRobert Love  * more details.
1442e9a92fSRobert Love  *
1542e9a92fSRobert Love  * You should have received a copy of the GNU General Public License along with
1642e9a92fSRobert Love  * this program; if not, write to the Free Software Foundation, Inc.,
1742e9a92fSRobert Love  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
1842e9a92fSRobert Love  *
1942e9a92fSRobert Love  * Maintained at www.Open-FCoE.org
2042e9a92fSRobert Love  */
2142e9a92fSRobert Love 
2242e9a92fSRobert Love /*
2342e9a92fSRobert Love  * Fibre Channel exchange and sequence handling.
2442e9a92fSRobert Love  */
2542e9a92fSRobert Love 
2642e9a92fSRobert Love #include <linux/timer.h>
2742e9a92fSRobert Love #include <linux/gfp.h>
2842e9a92fSRobert Love #include <linux/err.h>
2942e9a92fSRobert Love 
3042e9a92fSRobert Love #include <scsi/fc/fc_fc2.h>
3142e9a92fSRobert Love 
3242e9a92fSRobert Love #include <scsi/libfc.h>
3342e9a92fSRobert Love #include <scsi/fc_encode.h>
3442e9a92fSRobert Love 
35e4bc50beSVasu Dev u16	fc_cpu_mask;		/* cpu mask for possible cpus */
36e4bc50beSVasu Dev EXPORT_SYMBOL(fc_cpu_mask);
37e4bc50beSVasu Dev static u16	fc_cpu_order;	/* 2's power to represent total possible cpus */
3842e9a92fSRobert Love static struct kmem_cache *fc_em_cachep;        /* cache for exchanges */
3942e9a92fSRobert Love 
4042e9a92fSRobert Love /*
4142e9a92fSRobert Love  * Structure and function definitions for managing Fibre Channel Exchanges
4242e9a92fSRobert Love  * and Sequences.
4342e9a92fSRobert Love  *
4442e9a92fSRobert Love  * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
4542e9a92fSRobert Love  *
4642e9a92fSRobert Love  * fc_exch_mgr holds the exchange state for an N port
4742e9a92fSRobert Love  *
4842e9a92fSRobert Love  * fc_exch holds state for one exchange and links to its active sequence.
4942e9a92fSRobert Love  *
5042e9a92fSRobert Love  * fc_seq holds the state for an individual sequence.
5142e9a92fSRobert Love  */
5242e9a92fSRobert Love 
5342e9a92fSRobert Love /*
54e4bc50beSVasu Dev  * Per cpu exchange pool
55e4bc50beSVasu Dev  *
56e4bc50beSVasu Dev  * This structure manages per cpu exchanges in array of exchange pointers.
57e4bc50beSVasu Dev  * This array is allocated followed by struct fc_exch_pool memory for
58e4bc50beSVasu Dev  * assigned range of exchanges to per cpu pool.
59e4bc50beSVasu Dev  */
60e4bc50beSVasu Dev struct fc_exch_pool {
61e4bc50beSVasu Dev 	u16		next_index;	/* next possible free exchange index */
62e4bc50beSVasu Dev 	u16		total_exches;	/* total allocated exchanges */
63e4bc50beSVasu Dev 	spinlock_t	lock;		/* exch pool lock */
64e4bc50beSVasu Dev 	struct list_head	ex_list;	/* allocated exchanges list */
65e4bc50beSVasu Dev };
66e4bc50beSVasu Dev 
67e4bc50beSVasu Dev /*
6842e9a92fSRobert Love  * Exchange manager.
6942e9a92fSRobert Love  *
7042e9a92fSRobert Love  * This structure is the center for creating exchanges and sequences.
7142e9a92fSRobert Love  * It manages the allocation of exchange IDs.
7242e9a92fSRobert Love  */
7342e9a92fSRobert Love struct fc_exch_mgr {
7442e9a92fSRobert Love 	enum fc_class	class;		/* default class for sequences */
7596316099SVasu Dev 	struct kref	kref;		/* exchange mgr reference count */
7642e9a92fSRobert Love 	u16		min_xid;	/* min exchange ID */
7742e9a92fSRobert Love 	u16		max_xid;	/* max exchange ID */
7842e9a92fSRobert Love 	struct list_head	ex_list;	/* allocated exchanges list */
7942e9a92fSRobert Love 	mempool_t	*ep_pool;	/* reserve ep's */
80e4bc50beSVasu Dev 	u16		pool_max_index;	/* max exch array index in exch pool */
81e4bc50beSVasu Dev 	struct fc_exch_pool *pool;	/* per cpu exch pool */
8242e9a92fSRobert Love 
8342e9a92fSRobert Love 	/*
8442e9a92fSRobert Love 	 * currently exchange mgr stats are updated but not used.
8542e9a92fSRobert Love 	 * either stats can be expose via sysfs or remove them
8642e9a92fSRobert Love 	 * all together if not used XXX
8742e9a92fSRobert Love 	 */
8842e9a92fSRobert Love 	struct {
8942e9a92fSRobert Love 		atomic_t no_free_exch;
9042e9a92fSRobert Love 		atomic_t no_free_exch_xid;
9142e9a92fSRobert Love 		atomic_t xid_not_found;
9242e9a92fSRobert Love 		atomic_t xid_busy;
9342e9a92fSRobert Love 		atomic_t seq_not_found;
9442e9a92fSRobert Love 		atomic_t non_bls_resp;
9542e9a92fSRobert Love 	} stats;
9642e9a92fSRobert Love };
9742e9a92fSRobert Love #define	fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
9842e9a92fSRobert Love 
9996316099SVasu Dev struct fc_exch_mgr_anchor {
10096316099SVasu Dev 	struct list_head ema_list;
10196316099SVasu Dev 	struct fc_exch_mgr *mp;
10296316099SVasu Dev 	bool (*match)(struct fc_frame *);
10396316099SVasu Dev };
10496316099SVasu Dev 
10542e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *);
10642e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *);
10742e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason,
10842e9a92fSRobert Love 			  enum fc_els_rjt_explan);
10942e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *);
11042e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *);
11142e9a92fSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp);
11242e9a92fSRobert Love 
11342e9a92fSRobert Love /*
11442e9a92fSRobert Love  * Internal implementation notes.
11542e9a92fSRobert Love  *
11642e9a92fSRobert Love  * The exchange manager is one by default in libfc but LLD may choose
11742e9a92fSRobert Love  * to have one per CPU. The sequence manager is one per exchange manager
11842e9a92fSRobert Love  * and currently never separated.
11942e9a92fSRobert Love  *
12042e9a92fSRobert Love  * Section 9.8 in FC-FS-2 specifies:  "The SEQ_ID is a one-byte field
12142e9a92fSRobert Love  * assigned by the Sequence Initiator that shall be unique for a specific
12242e9a92fSRobert Love  * D_ID and S_ID pair while the Sequence is open."   Note that it isn't
12342e9a92fSRobert Love  * qualified by exchange ID, which one might think it would be.
12442e9a92fSRobert Love  * In practice this limits the number of open sequences and exchanges to 256
12542e9a92fSRobert Love  * per session.	 For most targets we could treat this limit as per exchange.
12642e9a92fSRobert Love  *
12742e9a92fSRobert Love  * The exchange and its sequence are freed when the last sequence is received.
12842e9a92fSRobert Love  * It's possible for the remote port to leave an exchange open without
12942e9a92fSRobert Love  * sending any sequences.
13042e9a92fSRobert Love  *
13142e9a92fSRobert Love  * Notes on reference counts:
13242e9a92fSRobert Love  *
13342e9a92fSRobert Love  * Exchanges are reference counted and exchange gets freed when the reference
13442e9a92fSRobert Love  * count becomes zero.
13542e9a92fSRobert Love  *
13642e9a92fSRobert Love  * Timeouts:
13742e9a92fSRobert Love  * Sequences are timed out for E_D_TOV and R_A_TOV.
13842e9a92fSRobert Love  *
13942e9a92fSRobert Love  * Sequence event handling:
14042e9a92fSRobert Love  *
14142e9a92fSRobert Love  * The following events may occur on initiator sequences:
14242e9a92fSRobert Love  *
14342e9a92fSRobert Love  *	Send.
14442e9a92fSRobert Love  *	    For now, the whole thing is sent.
14542e9a92fSRobert Love  *	Receive ACK
14642e9a92fSRobert Love  *	    This applies only to class F.
14742e9a92fSRobert Love  *	    The sequence is marked complete.
14842e9a92fSRobert Love  *	ULP completion.
14942e9a92fSRobert Love  *	    The upper layer calls fc_exch_done() when done
15042e9a92fSRobert Love  *	    with exchange and sequence tuple.
15142e9a92fSRobert Love  *	RX-inferred completion.
15242e9a92fSRobert Love  *	    When we receive the next sequence on the same exchange, we can
15342e9a92fSRobert Love  *	    retire the previous sequence ID.  (XXX not implemented).
15442e9a92fSRobert Love  *	Timeout.
15542e9a92fSRobert Love  *	    R_A_TOV frees the sequence ID.  If we're waiting for ACK,
15642e9a92fSRobert Love  *	    E_D_TOV causes abort and calls upper layer response handler
15742e9a92fSRobert Love  *	    with FC_EX_TIMEOUT error.
15842e9a92fSRobert Love  *	Receive RJT
15942e9a92fSRobert Love  *	    XXX defer.
16042e9a92fSRobert Love  *	Send ABTS
16142e9a92fSRobert Love  *	    On timeout.
16242e9a92fSRobert Love  *
16342e9a92fSRobert Love  * The following events may occur on recipient sequences:
16442e9a92fSRobert Love  *
16542e9a92fSRobert Love  *	Receive
16642e9a92fSRobert Love  *	    Allocate sequence for first frame received.
16742e9a92fSRobert Love  *	    Hold during receive handler.
16842e9a92fSRobert Love  *	    Release when final frame received.
16942e9a92fSRobert Love  *	    Keep status of last N of these for the ELS RES command.  XXX TBD.
17042e9a92fSRobert Love  *	Receive ABTS
17142e9a92fSRobert Love  *	    Deallocate sequence
17242e9a92fSRobert Love  *	Send RJT
17342e9a92fSRobert Love  *	    Deallocate
17442e9a92fSRobert Love  *
17542e9a92fSRobert Love  * For now, we neglect conditions where only part of a sequence was
17642e9a92fSRobert Love  * received or transmitted, or where out-of-order receipt is detected.
17742e9a92fSRobert Love  */
17842e9a92fSRobert Love 
17942e9a92fSRobert Love /*
18042e9a92fSRobert Love  * Locking notes:
18142e9a92fSRobert Love  *
18242e9a92fSRobert Love  * The EM code run in a per-CPU worker thread.
18342e9a92fSRobert Love  *
18442e9a92fSRobert Love  * To protect against concurrency between a worker thread code and timers,
18542e9a92fSRobert Love  * sequence allocation and deallocation must be locked.
18642e9a92fSRobert Love  *  - exchange refcnt can be done atomicly without locks.
18742e9a92fSRobert Love  *  - sequence allocation must be locked by exch lock.
188b2f0091fSVasu Dev  *  - If the EM pool lock and ex_lock must be taken at the same time, then the
189b2f0091fSVasu Dev  *    EM pool lock must be taken before the ex_lock.
19042e9a92fSRobert Love  */
19142e9a92fSRobert Love 
19242e9a92fSRobert Love /*
19342e9a92fSRobert Love  * opcode names for debugging.
19442e9a92fSRobert Love  */
19542e9a92fSRobert Love static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
19642e9a92fSRobert Love 
19742e9a92fSRobert Love #define FC_TABLE_SIZE(x)   (sizeof(x) / sizeof(x[0]))
19842e9a92fSRobert Love 
19942e9a92fSRobert Love static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
20042e9a92fSRobert Love 					      unsigned int max_index)
20142e9a92fSRobert Love {
20242e9a92fSRobert Love 	const char *name = NULL;
20342e9a92fSRobert Love 
20442e9a92fSRobert Love 	if (op < max_index)
20542e9a92fSRobert Love 		name = table[op];
20642e9a92fSRobert Love 	if (!name)
20742e9a92fSRobert Love 		name = "unknown";
20842e9a92fSRobert Love 	return name;
20942e9a92fSRobert Love }
21042e9a92fSRobert Love 
21142e9a92fSRobert Love static const char *fc_exch_rctl_name(unsigned int op)
21242e9a92fSRobert Love {
21342e9a92fSRobert Love 	return fc_exch_name_lookup(op, fc_exch_rctl_names,
21442e9a92fSRobert Love 				   FC_TABLE_SIZE(fc_exch_rctl_names));
21542e9a92fSRobert Love }
21642e9a92fSRobert Love 
21742e9a92fSRobert Love /*
21842e9a92fSRobert Love  * Hold an exchange - keep it from being freed.
21942e9a92fSRobert Love  */
22042e9a92fSRobert Love static void fc_exch_hold(struct fc_exch *ep)
22142e9a92fSRobert Love {
22242e9a92fSRobert Love 	atomic_inc(&ep->ex_refcnt);
22342e9a92fSRobert Love }
22442e9a92fSRobert Love 
22542e9a92fSRobert Love /*
22642e9a92fSRobert Love  * setup fc hdr by initializing few more FC header fields and sof/eof.
22742e9a92fSRobert Love  * Initialized fields by this func:
22842e9a92fSRobert Love  *	- fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt
22942e9a92fSRobert Love  *	- sof and eof
23042e9a92fSRobert Love  */
23142e9a92fSRobert Love static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
23242e9a92fSRobert Love 			      u32 f_ctl)
23342e9a92fSRobert Love {
23442e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
23542e9a92fSRobert Love 	u16 fill;
23642e9a92fSRobert Love 
23742e9a92fSRobert Love 	fr_sof(fp) = ep->class;
23842e9a92fSRobert Love 	if (ep->seq.cnt)
23942e9a92fSRobert Love 		fr_sof(fp) = fc_sof_normal(ep->class);
24042e9a92fSRobert Love 
24142e9a92fSRobert Love 	if (f_ctl & FC_FC_END_SEQ) {
24242e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_T;
24342e9a92fSRobert Love 		if (fc_sof_needs_ack(ep->class))
24442e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
24542e9a92fSRobert Love 		/*
24642e9a92fSRobert Love 		 * Form f_ctl.
24742e9a92fSRobert Love 		 * The number of fill bytes to make the length a 4-byte
24842e9a92fSRobert Love 		 * multiple is the low order 2-bits of the f_ctl.
24942e9a92fSRobert Love 		 * The fill itself will have been cleared by the frame
25042e9a92fSRobert Love 		 * allocation.
25142e9a92fSRobert Love 		 * After this, the length will be even, as expected by
25242e9a92fSRobert Love 		 * the transport.
25342e9a92fSRobert Love 		 */
25442e9a92fSRobert Love 		fill = fr_len(fp) & 3;
25542e9a92fSRobert Love 		if (fill) {
25642e9a92fSRobert Love 			fill = 4 - fill;
25742e9a92fSRobert Love 			/* TODO, this may be a problem with fragmented skb */
25842e9a92fSRobert Love 			skb_put(fp_skb(fp), fill);
25942e9a92fSRobert Love 			hton24(fh->fh_f_ctl, f_ctl | fill);
26042e9a92fSRobert Love 		}
26142e9a92fSRobert Love 	} else {
26242e9a92fSRobert Love 		WARN_ON(fr_len(fp) % 4 != 0);	/* no pad to non last frame */
26342e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
26442e9a92fSRobert Love 	}
26542e9a92fSRobert Love 
26642e9a92fSRobert Love 	/*
26742e9a92fSRobert Love 	 * Initialize remainig fh fields
26842e9a92fSRobert Love 	 * from fc_fill_fc_hdr
26942e9a92fSRobert Love 	 */
27042e9a92fSRobert Love 	fh->fh_ox_id = htons(ep->oxid);
27142e9a92fSRobert Love 	fh->fh_rx_id = htons(ep->rxid);
27242e9a92fSRobert Love 	fh->fh_seq_id = ep->seq.id;
27342e9a92fSRobert Love 	fh->fh_seq_cnt = htons(ep->seq.cnt);
27442e9a92fSRobert Love }
27542e9a92fSRobert Love 
27642e9a92fSRobert Love 
27742e9a92fSRobert Love /*
27842e9a92fSRobert Love  * Release a reference to an exchange.
27942e9a92fSRobert Love  * If the refcnt goes to zero and the exchange is complete, it is freed.
28042e9a92fSRobert Love  */
28142e9a92fSRobert Love static void fc_exch_release(struct fc_exch *ep)
28242e9a92fSRobert Love {
28342e9a92fSRobert Love 	struct fc_exch_mgr *mp;
28442e9a92fSRobert Love 
28542e9a92fSRobert Love 	if (atomic_dec_and_test(&ep->ex_refcnt)) {
28642e9a92fSRobert Love 		mp = ep->em;
28742e9a92fSRobert Love 		if (ep->destructor)
28842e9a92fSRobert Love 			ep->destructor(&ep->seq, ep->arg);
289aa6cd29bSJulia Lawall 		WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
29042e9a92fSRobert Love 		mempool_free(ep, mp->ep_pool);
29142e9a92fSRobert Love 	}
29242e9a92fSRobert Love }
29342e9a92fSRobert Love 
29442e9a92fSRobert Love static int fc_exch_done_locked(struct fc_exch *ep)
29542e9a92fSRobert Love {
29642e9a92fSRobert Love 	int rc = 1;
29742e9a92fSRobert Love 
29842e9a92fSRobert Love 	/*
29942e9a92fSRobert Love 	 * We must check for completion in case there are two threads
30042e9a92fSRobert Love 	 * tyring to complete this. But the rrq code will reuse the
30142e9a92fSRobert Love 	 * ep, and in that case we only clear the resp and set it as
30242e9a92fSRobert Love 	 * complete, so it can be reused by the timer to send the rrq.
30342e9a92fSRobert Love 	 */
30442e9a92fSRobert Love 	ep->resp = NULL;
30542e9a92fSRobert Love 	if (ep->state & FC_EX_DONE)
30642e9a92fSRobert Love 		return rc;
30742e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_COMPLETE;
30842e9a92fSRobert Love 
30942e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
31042e9a92fSRobert Love 		ep->state |= FC_EX_DONE;
31142e9a92fSRobert Love 		if (cancel_delayed_work(&ep->timeout_work))
31242e9a92fSRobert Love 			atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
31342e9a92fSRobert Love 		rc = 0;
31442e9a92fSRobert Love 	}
31542e9a92fSRobert Love 	return rc;
31642e9a92fSRobert Love }
31742e9a92fSRobert Love 
318e4bc50beSVasu Dev static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
319e4bc50beSVasu Dev 					      u16 index)
320e4bc50beSVasu Dev {
321e4bc50beSVasu Dev 	struct fc_exch **exches = (struct fc_exch **)(pool + 1);
322e4bc50beSVasu Dev 	return exches[index];
323e4bc50beSVasu Dev }
324e4bc50beSVasu Dev 
325e4bc50beSVasu Dev static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
326e4bc50beSVasu Dev 				   struct fc_exch *ep)
327e4bc50beSVasu Dev {
328e4bc50beSVasu Dev 	((struct fc_exch **)(pool + 1))[index] = ep;
329e4bc50beSVasu Dev }
330e4bc50beSVasu Dev 
331b2f0091fSVasu Dev static void fc_exch_delete(struct fc_exch *ep)
33242e9a92fSRobert Love {
333b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
33442e9a92fSRobert Love 
335b2f0091fSVasu Dev 	pool = ep->pool;
336b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
337b2f0091fSVasu Dev 	WARN_ON(pool->total_exches <= 0);
338b2f0091fSVasu Dev 	pool->total_exches--;
339b2f0091fSVasu Dev 	fc_exch_ptr_set(pool, (ep->xid - ep->em->min_xid) >> fc_cpu_order,
340b2f0091fSVasu Dev 			NULL);
34142e9a92fSRobert Love 	list_del(&ep->ex_list);
342b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
34342e9a92fSRobert Love 	fc_exch_release(ep);	/* drop hold for exch in mp */
34442e9a92fSRobert Love }
34542e9a92fSRobert Love 
34642e9a92fSRobert Love /*
34742e9a92fSRobert Love  * Internal version of fc_exch_timer_set - used with lock held.
34842e9a92fSRobert Love  */
34942e9a92fSRobert Love static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
35042e9a92fSRobert Love 					    unsigned int timer_msec)
35142e9a92fSRobert Love {
35242e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
35342e9a92fSRobert Love 		return;
35442e9a92fSRobert Love 
355cd305ce4SRobert Love 	FC_EXCH_DBG(ep, "Exchange timer armed\n");
3567414705eSRobert Love 
35742e9a92fSRobert Love 	if (schedule_delayed_work(&ep->timeout_work,
35842e9a92fSRobert Love 				  msecs_to_jiffies(timer_msec)))
35942e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for timer */
36042e9a92fSRobert Love }
36142e9a92fSRobert Love 
36242e9a92fSRobert Love /*
36342e9a92fSRobert Love  * Set timer for an exchange.
36442e9a92fSRobert Love  * The time is a minimum delay in milliseconds until the timer fires.
36542e9a92fSRobert Love  * Used for upper level protocols to time out the exchange.
36642e9a92fSRobert Love  * The timer is cancelled when it fires or when the exchange completes.
36742e9a92fSRobert Love  * Returns non-zero if a timer couldn't be allocated.
36842e9a92fSRobert Love  */
36942e9a92fSRobert Love static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
37042e9a92fSRobert Love {
37142e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
37242e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, timer_msec);
37342e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
37442e9a92fSRobert Love }
37542e9a92fSRobert Love 
37642e9a92fSRobert Love int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec)
37742e9a92fSRobert Love {
37842e9a92fSRobert Love 	struct fc_seq *sp;
37942e9a92fSRobert Love 	struct fc_exch *ep;
38042e9a92fSRobert Love 	struct fc_frame *fp;
38142e9a92fSRobert Love 	int error;
38242e9a92fSRobert Love 
38342e9a92fSRobert Love 	ep = fc_seq_exch(req_sp);
38442e9a92fSRobert Love 
38542e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
38642e9a92fSRobert Love 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
38742e9a92fSRobert Love 	    ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
38842e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
38942e9a92fSRobert Love 		return -ENXIO;
39042e9a92fSRobert Love 	}
39142e9a92fSRobert Love 
39242e9a92fSRobert Love 	/*
39342e9a92fSRobert Love 	 * Send the abort on a new sequence if possible.
39442e9a92fSRobert Love 	 */
39542e9a92fSRobert Love 	sp = fc_seq_start_next_locked(&ep->seq);
39642e9a92fSRobert Love 	if (!sp) {
39742e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
39842e9a92fSRobert Love 		return -ENOMEM;
39942e9a92fSRobert Love 	}
40042e9a92fSRobert Love 
40142e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
40242e9a92fSRobert Love 	if (timer_msec)
40342e9a92fSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
40442e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
40542e9a92fSRobert Love 
40642e9a92fSRobert Love 	/*
40742e9a92fSRobert Love 	 * If not logged into the fabric, don't send ABTS but leave
40842e9a92fSRobert Love 	 * sequence active until next timeout.
40942e9a92fSRobert Love 	 */
41042e9a92fSRobert Love 	if (!ep->sid)
41142e9a92fSRobert Love 		return 0;
41242e9a92fSRobert Love 
41342e9a92fSRobert Love 	/*
41442e9a92fSRobert Love 	 * Send an abort for the sequence that timed out.
41542e9a92fSRobert Love 	 */
41642e9a92fSRobert Love 	fp = fc_frame_alloc(ep->lp, 0);
41742e9a92fSRobert Love 	if (fp) {
41842e9a92fSRobert Love 		fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
41942e9a92fSRobert Love 			       FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
42042e9a92fSRobert Love 		error = fc_seq_send(ep->lp, sp, fp);
42142e9a92fSRobert Love 	} else
42242e9a92fSRobert Love 		error = -ENOBUFS;
42342e9a92fSRobert Love 	return error;
42442e9a92fSRobert Love }
42542e9a92fSRobert Love 
42642e9a92fSRobert Love /*
42742e9a92fSRobert Love  * Exchange timeout - handle exchange timer expiration.
42842e9a92fSRobert Love  * The timer will have been cancelled before this is called.
42942e9a92fSRobert Love  */
43042e9a92fSRobert Love static void fc_exch_timeout(struct work_struct *work)
43142e9a92fSRobert Love {
43242e9a92fSRobert Love 	struct fc_exch *ep = container_of(work, struct fc_exch,
43342e9a92fSRobert Love 					  timeout_work.work);
43442e9a92fSRobert Love 	struct fc_seq *sp = &ep->seq;
43542e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
43642e9a92fSRobert Love 	void *arg;
43742e9a92fSRobert Love 	u32 e_stat;
43842e9a92fSRobert Love 	int rc = 1;
43942e9a92fSRobert Love 
440cd305ce4SRobert Love 	FC_EXCH_DBG(ep, "Exchange timed out\n");
441cd305ce4SRobert Love 
44242e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
44342e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
44442e9a92fSRobert Love 		goto unlock;
44542e9a92fSRobert Love 
44642e9a92fSRobert Love 	e_stat = ep->esb_stat;
44742e9a92fSRobert Love 	if (e_stat & ESB_ST_COMPLETE) {
44842e9a92fSRobert Love 		ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
449a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
45042e9a92fSRobert Love 		if (e_stat & ESB_ST_REC_QUAL)
45142e9a92fSRobert Love 			fc_exch_rrq(ep);
45242e9a92fSRobert Love 		goto done;
45342e9a92fSRobert Love 	} else {
45442e9a92fSRobert Love 		resp = ep->resp;
45542e9a92fSRobert Love 		arg = ep->arg;
45642e9a92fSRobert Love 		ep->resp = NULL;
45742e9a92fSRobert Love 		if (e_stat & ESB_ST_ABNORMAL)
45842e9a92fSRobert Love 			rc = fc_exch_done_locked(ep);
45942e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
46042e9a92fSRobert Love 		if (!rc)
461b2f0091fSVasu Dev 			fc_exch_delete(ep);
46242e9a92fSRobert Love 		if (resp)
46342e9a92fSRobert Love 			resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
46442e9a92fSRobert Love 		fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
46542e9a92fSRobert Love 		goto done;
46642e9a92fSRobert Love 	}
46742e9a92fSRobert Love unlock:
46842e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
46942e9a92fSRobert Love done:
47042e9a92fSRobert Love 	/*
47142e9a92fSRobert Love 	 * This release matches the hold taken when the timer was set.
47242e9a92fSRobert Love 	 */
47342e9a92fSRobert Love 	fc_exch_release(ep);
47442e9a92fSRobert Love }
47542e9a92fSRobert Love 
47642e9a92fSRobert Love /*
47742e9a92fSRobert Love  * Allocate a sequence.
47842e9a92fSRobert Love  *
47942e9a92fSRobert Love  * We don't support multiple originated sequences on the same exchange.
48042e9a92fSRobert Love  * By implication, any previously originated sequence on this exchange
48142e9a92fSRobert Love  * is complete, and we reallocate the same sequence.
48242e9a92fSRobert Love  */
48342e9a92fSRobert Love static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
48442e9a92fSRobert Love {
48542e9a92fSRobert Love 	struct fc_seq *sp;
48642e9a92fSRobert Love 
48742e9a92fSRobert Love 	sp = &ep->seq;
48842e9a92fSRobert Love 	sp->ssb_stat = 0;
48942e9a92fSRobert Love 	sp->cnt = 0;
49042e9a92fSRobert Love 	sp->id = seq_id;
49142e9a92fSRobert Love 	return sp;
49242e9a92fSRobert Love }
49342e9a92fSRobert Love 
49452ff878cSVasu Dev /**
49552ff878cSVasu Dev  * fc_exch_em_alloc() - allocate an exchange from a specified EM.
49652ff878cSVasu Dev  * @lport:	ptr to the local port
49742e9a92fSRobert Love  * @mp:		ptr to the exchange manager
49842e9a92fSRobert Love  *
499d7179680SVasu Dev  * Returns pointer to allocated fc_exch with exch lock held.
50042e9a92fSRobert Love  */
50152ff878cSVasu Dev static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
502d7179680SVasu Dev 					struct fc_exch_mgr *mp)
50342e9a92fSRobert Love {
50442e9a92fSRobert Love 	struct fc_exch *ep;
505b2f0091fSVasu Dev 	unsigned int cpu;
506b2f0091fSVasu Dev 	u16 index;
507b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
50842e9a92fSRobert Love 
50942e9a92fSRobert Love 	/* allocate memory for exchange */
51042e9a92fSRobert Love 	ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
51142e9a92fSRobert Love 	if (!ep) {
51242e9a92fSRobert Love 		atomic_inc(&mp->stats.no_free_exch);
51342e9a92fSRobert Love 		goto out;
51442e9a92fSRobert Love 	}
51542e9a92fSRobert Love 	memset(ep, 0, sizeof(*ep));
51642e9a92fSRobert Love 
517b2f0091fSVasu Dev 	cpu = smp_processor_id();
518b2f0091fSVasu Dev 	pool = per_cpu_ptr(mp->pool, cpu);
519b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
520b2f0091fSVasu Dev 	index = pool->next_index;
521b2f0091fSVasu Dev 	/* allocate new exch from pool */
522b2f0091fSVasu Dev 	while (fc_exch_ptr_get(pool, index)) {
523b2f0091fSVasu Dev 		index = index == mp->pool_max_index ? 0 : index + 1;
524b2f0091fSVasu Dev 		if (index == pool->next_index)
52542e9a92fSRobert Love 			goto err;
52642e9a92fSRobert Love 	}
527b2f0091fSVasu Dev 	pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
52842e9a92fSRobert Love 
52942e9a92fSRobert Love 	fc_exch_hold(ep);	/* hold for exch in mp */
53042e9a92fSRobert Love 	spin_lock_init(&ep->ex_lock);
53142e9a92fSRobert Love 	/*
53242e9a92fSRobert Love 	 * Hold exch lock for caller to prevent fc_exch_reset()
53342e9a92fSRobert Love 	 * from releasing exch	while fc_exch_alloc() caller is
53442e9a92fSRobert Love 	 * still working on exch.
53542e9a92fSRobert Love 	 */
53642e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
53742e9a92fSRobert Love 
538b2f0091fSVasu Dev 	fc_exch_ptr_set(pool, index, ep);
539b2f0091fSVasu Dev 	list_add_tail(&ep->ex_list, &pool->ex_list);
54042e9a92fSRobert Love 	fc_seq_alloc(ep, ep->seq_id++);
541b2f0091fSVasu Dev 	pool->total_exches++;
542b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
54342e9a92fSRobert Love 
54442e9a92fSRobert Love 	/*
54542e9a92fSRobert Love 	 *  update exchange
54642e9a92fSRobert Love 	 */
547b2f0091fSVasu Dev 	ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
54842e9a92fSRobert Love 	ep->em = mp;
549b2f0091fSVasu Dev 	ep->pool = pool;
55052ff878cSVasu Dev 	ep->lp = lport;
55142e9a92fSRobert Love 	ep->f_ctl = FC_FC_FIRST_SEQ;	/* next seq is first seq */
55242e9a92fSRobert Love 	ep->rxid = FC_XID_UNKNOWN;
55342e9a92fSRobert Love 	ep->class = mp->class;
55442e9a92fSRobert Love 	INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
55542e9a92fSRobert Love out:
55642e9a92fSRobert Love 	return ep;
55742e9a92fSRobert Love err:
558b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
55942e9a92fSRobert Love 	atomic_inc(&mp->stats.no_free_exch_xid);
56042e9a92fSRobert Love 	mempool_free(ep, mp->ep_pool);
56142e9a92fSRobert Love 	return NULL;
56242e9a92fSRobert Love }
56352ff878cSVasu Dev 
56452ff878cSVasu Dev /**
56552ff878cSVasu Dev  * fc_exch_alloc() - allocate an exchange.
56652ff878cSVasu Dev  * @lport:	ptr to the local port
56752ff878cSVasu Dev  * @fp:		ptr to the FC frame
56852ff878cSVasu Dev  *
56952ff878cSVasu Dev  * This function walks the list of the exchange manager(EM)
57052ff878cSVasu Dev  * anchors to select a EM for new exchange allocation. The
57152ff878cSVasu Dev  * EM is selected having either a NULL match function pointer
57252ff878cSVasu Dev  * or call to match function returning true.
57352ff878cSVasu Dev  */
57452ff878cSVasu Dev struct fc_exch *fc_exch_alloc(struct fc_lport *lport, struct fc_frame *fp)
57552ff878cSVasu Dev {
57652ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
57752ff878cSVasu Dev 	struct fc_exch *ep;
57852ff878cSVasu Dev 
57952ff878cSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
58052ff878cSVasu Dev 		if (!ema->match || ema->match(fp)) {
581d7179680SVasu Dev 			ep = fc_exch_em_alloc(lport, ema->mp);
58252ff878cSVasu Dev 			if (ep)
58352ff878cSVasu Dev 				return ep;
58452ff878cSVasu Dev 		}
58552ff878cSVasu Dev 	}
58652ff878cSVasu Dev 	return NULL;
58752ff878cSVasu Dev }
58842e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_alloc);
58942e9a92fSRobert Love 
59042e9a92fSRobert Love /*
59142e9a92fSRobert Love  * Lookup and hold an exchange.
59242e9a92fSRobert Love  */
59342e9a92fSRobert Love static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
59442e9a92fSRobert Love {
595b2f0091fSVasu Dev 	struct fc_exch_pool *pool;
59642e9a92fSRobert Love 	struct fc_exch *ep = NULL;
59742e9a92fSRobert Love 
59842e9a92fSRobert Love 	if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
599b2f0091fSVasu Dev 		pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
600b2f0091fSVasu Dev 		spin_lock_bh(&pool->lock);
601b2f0091fSVasu Dev 		ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
60242e9a92fSRobert Love 		if (ep) {
60342e9a92fSRobert Love 			fc_exch_hold(ep);
60442e9a92fSRobert Love 			WARN_ON(ep->xid != xid);
60542e9a92fSRobert Love 		}
606b2f0091fSVasu Dev 		spin_unlock_bh(&pool->lock);
60742e9a92fSRobert Love 	}
60842e9a92fSRobert Love 	return ep;
60942e9a92fSRobert Love }
61042e9a92fSRobert Love 
61142e9a92fSRobert Love void fc_exch_done(struct fc_seq *sp)
61242e9a92fSRobert Love {
61342e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
61442e9a92fSRobert Love 	int rc;
61542e9a92fSRobert Love 
61642e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
61742e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
61842e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
61942e9a92fSRobert Love 	if (!rc)
620b2f0091fSVasu Dev 		fc_exch_delete(ep);
62142e9a92fSRobert Love }
62242e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_done);
62342e9a92fSRobert Love 
62442e9a92fSRobert Love /*
62542e9a92fSRobert Love  * Allocate a new exchange as responder.
62642e9a92fSRobert Love  * Sets the responder ID in the frame header.
62742e9a92fSRobert Love  */
62852ff878cSVasu Dev static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
62952ff878cSVasu Dev 				    struct fc_exch_mgr *mp,
63052ff878cSVasu Dev 				    struct fc_frame *fp)
63142e9a92fSRobert Love {
63242e9a92fSRobert Love 	struct fc_exch *ep;
63342e9a92fSRobert Love 	struct fc_frame_header *fh;
63442e9a92fSRobert Love 
63552ff878cSVasu Dev 	ep = fc_exch_alloc(lport, fp);
63642e9a92fSRobert Love 	if (ep) {
63742e9a92fSRobert Love 		ep->class = fc_frame_class(fp);
63842e9a92fSRobert Love 
63942e9a92fSRobert Love 		/*
64042e9a92fSRobert Love 		 * Set EX_CTX indicating we're responding on this exchange.
64142e9a92fSRobert Love 		 */
64242e9a92fSRobert Love 		ep->f_ctl |= FC_FC_EX_CTX;	/* we're responding */
64342e9a92fSRobert Love 		ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not new */
64442e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
64542e9a92fSRobert Love 		ep->sid = ntoh24(fh->fh_d_id);
64642e9a92fSRobert Love 		ep->did = ntoh24(fh->fh_s_id);
64742e9a92fSRobert Love 		ep->oid = ep->did;
64842e9a92fSRobert Love 
64942e9a92fSRobert Love 		/*
65042e9a92fSRobert Love 		 * Allocated exchange has placed the XID in the
65142e9a92fSRobert Love 		 * originator field. Move it to the responder field,
65242e9a92fSRobert Love 		 * and set the originator XID from the frame.
65342e9a92fSRobert Love 		 */
65442e9a92fSRobert Love 		ep->rxid = ep->xid;
65542e9a92fSRobert Love 		ep->oxid = ntohs(fh->fh_ox_id);
65642e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
65742e9a92fSRobert Love 		if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
65842e9a92fSRobert Love 			ep->esb_stat &= ~ESB_ST_SEQ_INIT;
65942e9a92fSRobert Love 
66042e9a92fSRobert Love 		fc_exch_hold(ep);	/* hold for caller */
66152ff878cSVasu Dev 		spin_unlock_bh(&ep->ex_lock);	/* lock from fc_exch_alloc */
66242e9a92fSRobert Love 	}
66342e9a92fSRobert Love 	return ep;
66442e9a92fSRobert Love }
66542e9a92fSRobert Love 
66642e9a92fSRobert Love /*
66742e9a92fSRobert Love  * Find a sequence for receive where the other end is originating the sequence.
66842e9a92fSRobert Love  * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
66942e9a92fSRobert Love  * on the ep that should be released by the caller.
67042e9a92fSRobert Love  */
67152ff878cSVasu Dev static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
67252ff878cSVasu Dev 						 struct fc_exch_mgr *mp,
673b2ab99c9SRobert Love 						 struct fc_frame *fp)
67442e9a92fSRobert Love {
67542e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
67642e9a92fSRobert Love 	struct fc_exch *ep = NULL;
67742e9a92fSRobert Love 	struct fc_seq *sp = NULL;
67842e9a92fSRobert Love 	enum fc_pf_rjt_reason reject = FC_RJT_NONE;
67942e9a92fSRobert Love 	u32 f_ctl;
68042e9a92fSRobert Love 	u16 xid;
68142e9a92fSRobert Love 
68242e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
68342e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
68442e9a92fSRobert Love 
68542e9a92fSRobert Love 	/*
68642e9a92fSRobert Love 	 * Lookup or create the exchange if we will be creating the sequence.
68742e9a92fSRobert Love 	 */
68842e9a92fSRobert Love 	if (f_ctl & FC_FC_EX_CTX) {
68942e9a92fSRobert Love 		xid = ntohs(fh->fh_ox_id);	/* we originated exch */
69042e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
69142e9a92fSRobert Love 		if (!ep) {
69242e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
69342e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
69442e9a92fSRobert Love 			goto out;
69542e9a92fSRobert Love 		}
69642e9a92fSRobert Love 		if (ep->rxid == FC_XID_UNKNOWN)
69742e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
69842e9a92fSRobert Love 		else if (ep->rxid != ntohs(fh->fh_rx_id)) {
69942e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
70042e9a92fSRobert Love 			goto rel;
70142e9a92fSRobert Love 		}
70242e9a92fSRobert Love 	} else {
70342e9a92fSRobert Love 		xid = ntohs(fh->fh_rx_id);	/* we are the responder */
70442e9a92fSRobert Love 
70542e9a92fSRobert Love 		/*
70642e9a92fSRobert Love 		 * Special case for MDS issuing an ELS TEST with a
70742e9a92fSRobert Love 		 * bad rxid of 0.
70842e9a92fSRobert Love 		 * XXX take this out once we do the proper reject.
70942e9a92fSRobert Love 		 */
71042e9a92fSRobert Love 		if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
71142e9a92fSRobert Love 		    fc_frame_payload_op(fp) == ELS_TEST) {
71242e9a92fSRobert Love 			fh->fh_rx_id = htons(FC_XID_UNKNOWN);
71342e9a92fSRobert Love 			xid = FC_XID_UNKNOWN;
71442e9a92fSRobert Love 		}
71542e9a92fSRobert Love 
71642e9a92fSRobert Love 		/*
71742e9a92fSRobert Love 		 * new sequence - find the exchange
71842e9a92fSRobert Love 		 */
71942e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
72042e9a92fSRobert Love 		if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
72142e9a92fSRobert Love 			if (ep) {
72242e9a92fSRobert Love 				atomic_inc(&mp->stats.xid_busy);
72342e9a92fSRobert Love 				reject = FC_RJT_RX_ID;
72442e9a92fSRobert Love 				goto rel;
72542e9a92fSRobert Love 			}
72652ff878cSVasu Dev 			ep = fc_exch_resp(lport, mp, fp);
72742e9a92fSRobert Love 			if (!ep) {
72842e9a92fSRobert Love 				reject = FC_RJT_EXCH_EST;	/* XXX */
72942e9a92fSRobert Love 				goto out;
73042e9a92fSRobert Love 			}
73142e9a92fSRobert Love 			xid = ep->xid;	/* get our XID */
73242e9a92fSRobert Love 		} else if (!ep) {
73342e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
73442e9a92fSRobert Love 			reject = FC_RJT_RX_ID;	/* XID not found */
73542e9a92fSRobert Love 			goto out;
73642e9a92fSRobert Love 		}
73742e9a92fSRobert Love 	}
73842e9a92fSRobert Love 
73942e9a92fSRobert Love 	/*
74042e9a92fSRobert Love 	 * At this point, we have the exchange held.
74142e9a92fSRobert Love 	 * Find or create the sequence.
74242e9a92fSRobert Love 	 */
74342e9a92fSRobert Love 	if (fc_sof_is_init(fr_sof(fp))) {
74442e9a92fSRobert Love 		sp = fc_seq_start_next(&ep->seq);
74542e9a92fSRobert Love 		if (!sp) {
74642e9a92fSRobert Love 			reject = FC_RJT_SEQ_XS;	/* exchange shortage */
74742e9a92fSRobert Love 			goto rel;
74842e9a92fSRobert Love 		}
74942e9a92fSRobert Love 		sp->id = fh->fh_seq_id;
75042e9a92fSRobert Love 		sp->ssb_stat |= SSB_ST_RESP;
75142e9a92fSRobert Love 	} else {
75242e9a92fSRobert Love 		sp = &ep->seq;
75342e9a92fSRobert Love 		if (sp->id != fh->fh_seq_id) {
75442e9a92fSRobert Love 			atomic_inc(&mp->stats.seq_not_found);
75542e9a92fSRobert Love 			reject = FC_RJT_SEQ_ID;	/* sequence/exch should exist */
75642e9a92fSRobert Love 			goto rel;
75742e9a92fSRobert Love 		}
75842e9a92fSRobert Love 	}
75942e9a92fSRobert Love 	WARN_ON(ep != fc_seq_exch(sp));
76042e9a92fSRobert Love 
76142e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
76242e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
76342e9a92fSRobert Love 
76442e9a92fSRobert Love 	fr_seq(fp) = sp;
76542e9a92fSRobert Love out:
76642e9a92fSRobert Love 	return reject;
76742e9a92fSRobert Love rel:
76842e9a92fSRobert Love 	fc_exch_done(&ep->seq);
76942e9a92fSRobert Love 	fc_exch_release(ep);	/* hold from fc_exch_find/fc_exch_resp */
77042e9a92fSRobert Love 	return reject;
77142e9a92fSRobert Love }
77242e9a92fSRobert Love 
77342e9a92fSRobert Love /*
77442e9a92fSRobert Love  * Find the sequence for a frame being received.
77542e9a92fSRobert Love  * We originated the sequence, so it should be found.
77642e9a92fSRobert Love  * We may or may not have originated the exchange.
77742e9a92fSRobert Love  * Does not hold the sequence for the caller.
77842e9a92fSRobert Love  */
77942e9a92fSRobert Love static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
78042e9a92fSRobert Love 					 struct fc_frame *fp)
78142e9a92fSRobert Love {
78242e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
78342e9a92fSRobert Love 	struct fc_exch *ep;
78442e9a92fSRobert Love 	struct fc_seq *sp = NULL;
78542e9a92fSRobert Love 	u32 f_ctl;
78642e9a92fSRobert Love 	u16 xid;
78742e9a92fSRobert Love 
78842e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
78942e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
79042e9a92fSRobert Love 	xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
79142e9a92fSRobert Love 	ep = fc_exch_find(mp, xid);
79242e9a92fSRobert Love 	if (!ep)
79342e9a92fSRobert Love 		return NULL;
79442e9a92fSRobert Love 	if (ep->seq.id == fh->fh_seq_id) {
79542e9a92fSRobert Love 		/*
79642e9a92fSRobert Love 		 * Save the RX_ID if we didn't previously know it.
79742e9a92fSRobert Love 		 */
79842e9a92fSRobert Love 		sp = &ep->seq;
79942e9a92fSRobert Love 		if ((f_ctl & FC_FC_EX_CTX) != 0 &&
80042e9a92fSRobert Love 		    ep->rxid == FC_XID_UNKNOWN) {
80142e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
80242e9a92fSRobert Love 		}
80342e9a92fSRobert Love 	}
80442e9a92fSRobert Love 	fc_exch_release(ep);
80542e9a92fSRobert Love 	return sp;
80642e9a92fSRobert Love }
80742e9a92fSRobert Love 
80842e9a92fSRobert Love /*
80942e9a92fSRobert Love  * Set addresses for an exchange.
81042e9a92fSRobert Love  * Note this must be done before the first sequence of the exchange is sent.
81142e9a92fSRobert Love  */
81242e9a92fSRobert Love static void fc_exch_set_addr(struct fc_exch *ep,
81342e9a92fSRobert Love 			     u32 orig_id, u32 resp_id)
81442e9a92fSRobert Love {
81542e9a92fSRobert Love 	ep->oid = orig_id;
81642e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP) {
81742e9a92fSRobert Love 		ep->sid = resp_id;
81842e9a92fSRobert Love 		ep->did = orig_id;
81942e9a92fSRobert Love 	} else {
82042e9a92fSRobert Love 		ep->sid = orig_id;
82142e9a92fSRobert Love 		ep->did = resp_id;
82242e9a92fSRobert Love 	}
82342e9a92fSRobert Love }
82442e9a92fSRobert Love 
82542e9a92fSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
82642e9a92fSRobert Love {
82742e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
82842e9a92fSRobert Love 
82942e9a92fSRobert Love 	sp = fc_seq_alloc(ep, ep->seq_id++);
8307414705eSRobert Love 	FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
8317414705eSRobert Love 		    ep->f_ctl, sp->id);
83242e9a92fSRobert Love 	return sp;
83342e9a92fSRobert Love }
83442e9a92fSRobert Love /*
83542e9a92fSRobert Love  * Allocate a new sequence on the same exchange as the supplied sequence.
83642e9a92fSRobert Love  * This will never return NULL.
83742e9a92fSRobert Love  */
83842e9a92fSRobert Love struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
83942e9a92fSRobert Love {
84042e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
84142e9a92fSRobert Love 
84242e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
84342e9a92fSRobert Love 	sp = fc_seq_start_next_locked(sp);
84442e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
84542e9a92fSRobert Love 
84642e9a92fSRobert Love 	return sp;
84742e9a92fSRobert Love }
84842e9a92fSRobert Love EXPORT_SYMBOL(fc_seq_start_next);
84942e9a92fSRobert Love 
85042e9a92fSRobert Love int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
85142e9a92fSRobert Love {
85242e9a92fSRobert Love 	struct fc_exch *ep;
85342e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
85442e9a92fSRobert Love 	int error;
85542e9a92fSRobert Love 	u32	f_ctl;
85642e9a92fSRobert Love 
85742e9a92fSRobert Love 	ep = fc_seq_exch(sp);
85842e9a92fSRobert Love 	WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
85942e9a92fSRobert Love 
86042e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
86142e9a92fSRobert Love 	fc_exch_setup_hdr(ep, fp, f_ctl);
86242e9a92fSRobert Love 
86342e9a92fSRobert Love 	/*
86442e9a92fSRobert Love 	 * update sequence count if this frame is carrying
86542e9a92fSRobert Love 	 * multiple FC frames when sequence offload is enabled
86642e9a92fSRobert Love 	 * by LLD.
86742e9a92fSRobert Love 	 */
86842e9a92fSRobert Love 	if (fr_max_payload(fp))
86942e9a92fSRobert Love 		sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
87042e9a92fSRobert Love 					fr_max_payload(fp));
87142e9a92fSRobert Love 	else
87242e9a92fSRobert Love 		sp->cnt++;
87342e9a92fSRobert Love 
87442e9a92fSRobert Love 	/*
87542e9a92fSRobert Love 	 * Send the frame.
87642e9a92fSRobert Love 	 */
87742e9a92fSRobert Love 	error = lp->tt.frame_send(lp, fp);
87842e9a92fSRobert Love 
87942e9a92fSRobert Love 	/*
88042e9a92fSRobert Love 	 * Update the exchange and sequence flags,
88142e9a92fSRobert Love 	 * assuming all frames for the sequence have been sent.
88242e9a92fSRobert Love 	 * We can only be called to send once for each sequence.
88342e9a92fSRobert Love 	 */
88442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
88542e9a92fSRobert Love 	ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ;	/* not first seq */
88642e9a92fSRobert Love 	if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
88742e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
88842e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
88942e9a92fSRobert Love 	return error;
89042e9a92fSRobert Love }
89142e9a92fSRobert Love EXPORT_SYMBOL(fc_seq_send);
89242e9a92fSRobert Love 
89342e9a92fSRobert Love void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
89442e9a92fSRobert Love 			 struct fc_seq_els_data *els_data)
89542e9a92fSRobert Love {
89642e9a92fSRobert Love 	switch (els_cmd) {
89742e9a92fSRobert Love 	case ELS_LS_RJT:
89842e9a92fSRobert Love 		fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
89942e9a92fSRobert Love 		break;
90042e9a92fSRobert Love 	case ELS_LS_ACC:
90142e9a92fSRobert Love 		fc_seq_ls_acc(sp);
90242e9a92fSRobert Love 		break;
90342e9a92fSRobert Love 	case ELS_RRQ:
90442e9a92fSRobert Love 		fc_exch_els_rrq(sp, els_data->fp);
90542e9a92fSRobert Love 		break;
90642e9a92fSRobert Love 	case ELS_REC:
90742e9a92fSRobert Love 		fc_exch_els_rec(sp, els_data->fp);
90842e9a92fSRobert Love 		break;
90942e9a92fSRobert Love 	default:
9107414705eSRobert Love 		FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
91142e9a92fSRobert Love 	}
91242e9a92fSRobert Love }
91342e9a92fSRobert Love EXPORT_SYMBOL(fc_seq_els_rsp_send);
91442e9a92fSRobert Love 
91542e9a92fSRobert Love /*
91642e9a92fSRobert Love  * Send a sequence, which is also the last sequence in the exchange.
91742e9a92fSRobert Love  */
91842e9a92fSRobert Love static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
91942e9a92fSRobert Love 			     enum fc_rctl rctl, enum fc_fh_type fh_type)
92042e9a92fSRobert Love {
92142e9a92fSRobert Love 	u32 f_ctl;
92242e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
92342e9a92fSRobert Love 
92442e9a92fSRobert Love 	f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
92542e9a92fSRobert Love 	f_ctl |= ep->f_ctl;
92642e9a92fSRobert Love 	fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
92742e9a92fSRobert Love 	fc_seq_send(ep->lp, sp, fp);
92842e9a92fSRobert Love }
92942e9a92fSRobert Love 
93042e9a92fSRobert Love /*
93142e9a92fSRobert Love  * Send ACK_1 (or equiv.) indicating we received something.
93242e9a92fSRobert Love  * The frame we're acking is supplied.
93342e9a92fSRobert Love  */
93442e9a92fSRobert Love static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
93542e9a92fSRobert Love {
93642e9a92fSRobert Love 	struct fc_frame *fp;
93742e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
93842e9a92fSRobert Love 	struct fc_frame_header *fh;
93942e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
94042e9a92fSRobert Love 	struct fc_lport *lp = ep->lp;
94142e9a92fSRobert Love 	unsigned int f_ctl;
94242e9a92fSRobert Love 
94342e9a92fSRobert Love 	/*
94442e9a92fSRobert Love 	 * Don't send ACKs for class 3.
94542e9a92fSRobert Love 	 */
94642e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(rx_fp))) {
94742e9a92fSRobert Love 		fp = fc_frame_alloc(lp, 0);
94842e9a92fSRobert Love 		if (!fp)
94942e9a92fSRobert Love 			return;
95042e9a92fSRobert Love 
95142e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
95242e9a92fSRobert Love 		fh->fh_r_ctl = FC_RCTL_ACK_1;
95342e9a92fSRobert Love 		fh->fh_type = FC_TYPE_BLS;
95442e9a92fSRobert Love 
95542e9a92fSRobert Love 		/*
95642e9a92fSRobert Love 		 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
95742e9a92fSRobert Love 		 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
95842e9a92fSRobert Love 		 * Bits 9-8 are meaningful (retransmitted or unidirectional).
95942e9a92fSRobert Love 		 * Last ACK uses bits 7-6 (continue sequence),
96042e9a92fSRobert Love 		 * bits 5-4 are meaningful (what kind of ACK to use).
96142e9a92fSRobert Love 		 */
96242e9a92fSRobert Love 		rx_fh = fc_frame_header_get(rx_fp);
96342e9a92fSRobert Love 		f_ctl = ntoh24(rx_fh->fh_f_ctl);
96442e9a92fSRobert Love 		f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
96542e9a92fSRobert Love 			FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
96642e9a92fSRobert Love 			FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
96742e9a92fSRobert Love 			FC_FC_RETX_SEQ | FC_FC_UNI_TX;
96842e9a92fSRobert Love 		f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
96942e9a92fSRobert Love 		hton24(fh->fh_f_ctl, f_ctl);
97042e9a92fSRobert Love 
97142e9a92fSRobert Love 		fc_exch_setup_hdr(ep, fp, f_ctl);
97242e9a92fSRobert Love 		fh->fh_seq_id = rx_fh->fh_seq_id;
97342e9a92fSRobert Love 		fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
97442e9a92fSRobert Love 		fh->fh_parm_offset = htonl(1);	/* ack single frame */
97542e9a92fSRobert Love 
97642e9a92fSRobert Love 		fr_sof(fp) = fr_sof(rx_fp);
97742e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
97842e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_T;
97942e9a92fSRobert Love 		else
98042e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
98142e9a92fSRobert Love 
98242e9a92fSRobert Love 		(void) lp->tt.frame_send(lp, fp);
98342e9a92fSRobert Love 	}
98442e9a92fSRobert Love }
98542e9a92fSRobert Love 
98642e9a92fSRobert Love /*
98742e9a92fSRobert Love  * Send BLS Reject.
98842e9a92fSRobert Love  * This is for rejecting BA_ABTS only.
98942e9a92fSRobert Love  */
990b2ab99c9SRobert Love static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
991b2ab99c9SRobert Love 				enum fc_ba_rjt_reason reason,
99242e9a92fSRobert Love 				enum fc_ba_rjt_explan explan)
99342e9a92fSRobert Love {
99442e9a92fSRobert Love 	struct fc_frame *fp;
99542e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
99642e9a92fSRobert Love 	struct fc_frame_header *fh;
99742e9a92fSRobert Love 	struct fc_ba_rjt *rp;
99842e9a92fSRobert Love 	struct fc_lport *lp;
99942e9a92fSRobert Love 	unsigned int f_ctl;
100042e9a92fSRobert Love 
100142e9a92fSRobert Love 	lp = fr_dev(rx_fp);
100242e9a92fSRobert Love 	fp = fc_frame_alloc(lp, sizeof(*rp));
100342e9a92fSRobert Love 	if (!fp)
100442e9a92fSRobert Love 		return;
100542e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
100642e9a92fSRobert Love 	rx_fh = fc_frame_header_get(rx_fp);
100742e9a92fSRobert Love 
100842e9a92fSRobert Love 	memset(fh, 0, sizeof(*fh) + sizeof(*rp));
100942e9a92fSRobert Love 
101042e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
101142e9a92fSRobert Love 	rp->br_reason = reason;
101242e9a92fSRobert Love 	rp->br_explan = explan;
101342e9a92fSRobert Love 
101442e9a92fSRobert Love 	/*
101542e9a92fSRobert Love 	 * seq_id, cs_ctl, df_ctl and param/offset are zero.
101642e9a92fSRobert Love 	 */
101742e9a92fSRobert Love 	memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
101842e9a92fSRobert Love 	memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
10191d490ce3SJoe Eykholt 	fh->fh_ox_id = rx_fh->fh_ox_id;
10201d490ce3SJoe Eykholt 	fh->fh_rx_id = rx_fh->fh_rx_id;
102142e9a92fSRobert Love 	fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
102242e9a92fSRobert Love 	fh->fh_r_ctl = FC_RCTL_BA_RJT;
102342e9a92fSRobert Love 	fh->fh_type = FC_TYPE_BLS;
102442e9a92fSRobert Love 
102542e9a92fSRobert Love 	/*
102642e9a92fSRobert Love 	 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
102742e9a92fSRobert Love 	 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
102842e9a92fSRobert Love 	 * Bits 9-8 are meaningful (retransmitted or unidirectional).
102942e9a92fSRobert Love 	 * Last ACK uses bits 7-6 (continue sequence),
103042e9a92fSRobert Love 	 * bits 5-4 are meaningful (what kind of ACK to use).
103142e9a92fSRobert Love 	 * Always set LAST_SEQ, END_SEQ.
103242e9a92fSRobert Love 	 */
103342e9a92fSRobert Love 	f_ctl = ntoh24(rx_fh->fh_f_ctl);
103442e9a92fSRobert Love 	f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
103542e9a92fSRobert Love 		FC_FC_END_CONN | FC_FC_SEQ_INIT |
103642e9a92fSRobert Love 		FC_FC_RETX_SEQ | FC_FC_UNI_TX;
103742e9a92fSRobert Love 	f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
103842e9a92fSRobert Love 	f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
103942e9a92fSRobert Love 	f_ctl &= ~FC_FC_FIRST_SEQ;
104042e9a92fSRobert Love 	hton24(fh->fh_f_ctl, f_ctl);
104142e9a92fSRobert Love 
104242e9a92fSRobert Love 	fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
104342e9a92fSRobert Love 	fr_eof(fp) = FC_EOF_T;
104442e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(fp)))
104542e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
104642e9a92fSRobert Love 
104742e9a92fSRobert Love 	(void) lp->tt.frame_send(lp, fp);
104842e9a92fSRobert Love }
104942e9a92fSRobert Love 
105042e9a92fSRobert Love /*
105142e9a92fSRobert Love  * Handle an incoming ABTS.  This would be for target mode usually,
105242e9a92fSRobert Love  * but could be due to lost FCP transfer ready, confirm or RRQ.
105342e9a92fSRobert Love  * We always handle this as an exchange abort, ignoring the parameter.
105442e9a92fSRobert Love  */
105542e9a92fSRobert Love static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
105642e9a92fSRobert Love {
105742e9a92fSRobert Love 	struct fc_frame *fp;
105842e9a92fSRobert Love 	struct fc_ba_acc *ap;
105942e9a92fSRobert Love 	struct fc_frame_header *fh;
106042e9a92fSRobert Love 	struct fc_seq *sp;
106142e9a92fSRobert Love 
106242e9a92fSRobert Love 	if (!ep)
106342e9a92fSRobert Love 		goto reject;
106442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
106542e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
106642e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
106742e9a92fSRobert Love 		goto reject;
106842e9a92fSRobert Love 	}
106942e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL))
107042e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for REC_QUAL */
107142e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
107242e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
107342e9a92fSRobert Love 
107442e9a92fSRobert Love 	fp = fc_frame_alloc(ep->lp, sizeof(*ap));
107542e9a92fSRobert Love 	if (!fp) {
107642e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
107742e9a92fSRobert Love 		goto free;
107842e9a92fSRobert Love 	}
107942e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
108042e9a92fSRobert Love 	ap = fc_frame_payload_get(fp, sizeof(*ap));
108142e9a92fSRobert Love 	memset(ap, 0, sizeof(*ap));
108242e9a92fSRobert Love 	sp = &ep->seq;
108342e9a92fSRobert Love 	ap->ba_high_seq_cnt = htons(0xffff);
108442e9a92fSRobert Love 	if (sp->ssb_stat & SSB_ST_RESP) {
108542e9a92fSRobert Love 		ap->ba_seq_id = sp->id;
108642e9a92fSRobert Love 		ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
108742e9a92fSRobert Love 		ap->ba_high_seq_cnt = fh->fh_seq_cnt;
108842e9a92fSRobert Love 		ap->ba_low_seq_cnt = htons(sp->cnt);
108942e9a92fSRobert Love 	}
1090a7e84f2bSVasu Dev 	sp = fc_seq_start_next_locked(sp);
109142e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
109242e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
109342e9a92fSRobert Love 	fc_frame_free(rx_fp);
109442e9a92fSRobert Love 	return;
109542e9a92fSRobert Love 
109642e9a92fSRobert Love reject:
109742e9a92fSRobert Love 	fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
109842e9a92fSRobert Love free:
109942e9a92fSRobert Love 	fc_frame_free(rx_fp);
110042e9a92fSRobert Love }
110142e9a92fSRobert Love 
110242e9a92fSRobert Love /*
110342e9a92fSRobert Love  * Handle receive where the other end is originating the sequence.
110442e9a92fSRobert Love  */
110542e9a92fSRobert Love static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
110642e9a92fSRobert Love 			     struct fc_frame *fp)
110742e9a92fSRobert Love {
110842e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
110942e9a92fSRobert Love 	struct fc_seq *sp = NULL;
111042e9a92fSRobert Love 	struct fc_exch *ep = NULL;
111142e9a92fSRobert Love 	enum fc_sof sof;
111242e9a92fSRobert Love 	enum fc_eof eof;
111342e9a92fSRobert Love 	u32 f_ctl;
111442e9a92fSRobert Love 	enum fc_pf_rjt_reason reject;
111542e9a92fSRobert Love 
111642e9a92fSRobert Love 	fr_seq(fp) = NULL;
111752ff878cSVasu Dev 	reject = fc_seq_lookup_recip(lp, mp, fp);
111842e9a92fSRobert Love 	if (reject == FC_RJT_NONE) {
111942e9a92fSRobert Love 		sp = fr_seq(fp);	/* sequence will be held */
112042e9a92fSRobert Love 		ep = fc_seq_exch(sp);
112142e9a92fSRobert Love 		sof = fr_sof(fp);
112242e9a92fSRobert Love 		eof = fr_eof(fp);
112342e9a92fSRobert Love 		f_ctl = ntoh24(fh->fh_f_ctl);
112442e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
112542e9a92fSRobert Love 
112642e9a92fSRobert Love 		/*
112742e9a92fSRobert Love 		 * Call the receive function.
112842e9a92fSRobert Love 		 *
112942e9a92fSRobert Love 		 * The receive function may allocate a new sequence
113042e9a92fSRobert Love 		 * over the old one, so we shouldn't change the
113142e9a92fSRobert Love 		 * sequence after this.
113242e9a92fSRobert Love 		 *
113342e9a92fSRobert Love 		 * The frame will be freed by the receive function.
113442e9a92fSRobert Love 		 * If new exch resp handler is valid then call that
113542e9a92fSRobert Love 		 * first.
113642e9a92fSRobert Love 		 */
113742e9a92fSRobert Love 		if (ep->resp)
113842e9a92fSRobert Love 			ep->resp(sp, fp, ep->arg);
113942e9a92fSRobert Love 		else
114042e9a92fSRobert Love 			lp->tt.lport_recv(lp, sp, fp);
114142e9a92fSRobert Love 		fc_exch_release(ep);	/* release from lookup */
114242e9a92fSRobert Love 	} else {
1143d459b7eaSRobert Love 		FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject);
114442e9a92fSRobert Love 		fc_frame_free(fp);
114542e9a92fSRobert Love 	}
114642e9a92fSRobert Love }
114742e9a92fSRobert Love 
114842e9a92fSRobert Love /*
114942e9a92fSRobert Love  * Handle receive where the other end is originating the sequence in
115042e9a92fSRobert Love  * response to our exchange.
115142e9a92fSRobert Love  */
115242e9a92fSRobert Love static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
115342e9a92fSRobert Love {
115442e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
115542e9a92fSRobert Love 	struct fc_seq *sp;
115642e9a92fSRobert Love 	struct fc_exch *ep;
115742e9a92fSRobert Love 	enum fc_sof sof;
115842e9a92fSRobert Love 	u32 f_ctl;
115942e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
116042e9a92fSRobert Love 	void *ex_resp_arg;
116142e9a92fSRobert Love 	int rc;
116242e9a92fSRobert Love 
116342e9a92fSRobert Love 	ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
116442e9a92fSRobert Love 	if (!ep) {
116542e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
116642e9a92fSRobert Love 		goto out;
116742e9a92fSRobert Love 	}
116830121d14SSteve Ma 	if (ep->esb_stat & ESB_ST_COMPLETE) {
116930121d14SSteve Ma 		atomic_inc(&mp->stats.xid_not_found);
117030121d14SSteve Ma 		goto out;
117130121d14SSteve Ma 	}
117242e9a92fSRobert Love 	if (ep->rxid == FC_XID_UNKNOWN)
117342e9a92fSRobert Love 		ep->rxid = ntohs(fh->fh_rx_id);
117442e9a92fSRobert Love 	if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
117542e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
117642e9a92fSRobert Love 		goto rel;
117742e9a92fSRobert Love 	}
117842e9a92fSRobert Love 	if (ep->did != ntoh24(fh->fh_s_id) &&
117942e9a92fSRobert Love 	    ep->did != FC_FID_FLOGI) {
118042e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
118142e9a92fSRobert Love 		goto rel;
118242e9a92fSRobert Love 	}
118342e9a92fSRobert Love 	sof = fr_sof(fp);
118442e9a92fSRobert Love 	if (fc_sof_is_init(sof)) {
118542e9a92fSRobert Love 		sp = fc_seq_start_next(&ep->seq);
118642e9a92fSRobert Love 		sp->id = fh->fh_seq_id;
118742e9a92fSRobert Love 		sp->ssb_stat |= SSB_ST_RESP;
118842e9a92fSRobert Love 	} else {
118942e9a92fSRobert Love 		sp = &ep->seq;
119042e9a92fSRobert Love 		if (sp->id != fh->fh_seq_id) {
119142e9a92fSRobert Love 			atomic_inc(&mp->stats.seq_not_found);
119242e9a92fSRobert Love 			goto rel;
119342e9a92fSRobert Love 		}
119442e9a92fSRobert Love 	}
119542e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
119642e9a92fSRobert Love 	fr_seq(fp) = sp;
119742e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
119842e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
119942e9a92fSRobert Love 
120042e9a92fSRobert Love 	if (fc_sof_needs_ack(sof))
120142e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
120242e9a92fSRobert Love 	resp = ep->resp;
120342e9a92fSRobert Love 	ex_resp_arg = ep->arg;
120442e9a92fSRobert Love 
120542e9a92fSRobert Love 	if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
120642e9a92fSRobert Love 	    (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
120742e9a92fSRobert Love 	    (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
120842e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
120942e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
121042e9a92fSRobert Love 		WARN_ON(fc_seq_exch(sp) != ep);
121142e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
121242e9a92fSRobert Love 		if (!rc)
1213b2f0091fSVasu Dev 			fc_exch_delete(ep);
121442e9a92fSRobert Love 	}
121542e9a92fSRobert Love 
121642e9a92fSRobert Love 	/*
121742e9a92fSRobert Love 	 * Call the receive function.
121842e9a92fSRobert Love 	 * The sequence is held (has a refcnt) for us,
121942e9a92fSRobert Love 	 * but not for the receive function.
122042e9a92fSRobert Love 	 *
122142e9a92fSRobert Love 	 * The receive function may allocate a new sequence
122242e9a92fSRobert Love 	 * over the old one, so we shouldn't change the
122342e9a92fSRobert Love 	 * sequence after this.
122442e9a92fSRobert Love 	 *
122542e9a92fSRobert Love 	 * The frame will be freed by the receive function.
122642e9a92fSRobert Love 	 * If new exch resp handler is valid then call that
122742e9a92fSRobert Love 	 * first.
122842e9a92fSRobert Love 	 */
122942e9a92fSRobert Love 	if (resp)
123042e9a92fSRobert Love 		resp(sp, fp, ex_resp_arg);
123142e9a92fSRobert Love 	else
123242e9a92fSRobert Love 		fc_frame_free(fp);
123342e9a92fSRobert Love 	fc_exch_release(ep);
123442e9a92fSRobert Love 	return;
123542e9a92fSRobert Love rel:
123642e9a92fSRobert Love 	fc_exch_release(ep);
123742e9a92fSRobert Love out:
123842e9a92fSRobert Love 	fc_frame_free(fp);
123942e9a92fSRobert Love }
124042e9a92fSRobert Love 
124142e9a92fSRobert Love /*
124242e9a92fSRobert Love  * Handle receive for a sequence where other end is responding to our sequence.
124342e9a92fSRobert Love  */
124442e9a92fSRobert Love static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
124542e9a92fSRobert Love {
124642e9a92fSRobert Love 	struct fc_seq *sp;
124742e9a92fSRobert Love 
124842e9a92fSRobert Love 	sp = fc_seq_lookup_orig(mp, fp);	/* doesn't hold sequence */
1249d459b7eaSRobert Love 
1250d459b7eaSRobert Love 	if (!sp)
125142e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
1252d459b7eaSRobert Love 	else
125342e9a92fSRobert Love 		atomic_inc(&mp->stats.non_bls_resp);
1254d459b7eaSRobert Love 
125542e9a92fSRobert Love 	fc_frame_free(fp);
125642e9a92fSRobert Love }
125742e9a92fSRobert Love 
125842e9a92fSRobert Love /*
125942e9a92fSRobert Love  * Handle the response to an ABTS for exchange or sequence.
126042e9a92fSRobert Love  * This can be BA_ACC or BA_RJT.
126142e9a92fSRobert Love  */
126242e9a92fSRobert Love static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
126342e9a92fSRobert Love {
126442e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
126542e9a92fSRobert Love 	void *ex_resp_arg;
126642e9a92fSRobert Love 	struct fc_frame_header *fh;
126742e9a92fSRobert Love 	struct fc_ba_acc *ap;
126842e9a92fSRobert Love 	struct fc_seq *sp;
126942e9a92fSRobert Love 	u16 low;
127042e9a92fSRobert Love 	u16 high;
127142e9a92fSRobert Love 	int rc = 1, has_rec = 0;
127242e9a92fSRobert Love 
127342e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
12747414705eSRobert Love 	FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
12757414705eSRobert Love 		    fc_exch_rctl_name(fh->fh_r_ctl));
127642e9a92fSRobert Love 
127742e9a92fSRobert Love 	if (cancel_delayed_work_sync(&ep->timeout_work))
127842e9a92fSRobert Love 		fc_exch_release(ep);	/* release from pending timer hold */
127942e9a92fSRobert Love 
128042e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
128142e9a92fSRobert Love 	switch (fh->fh_r_ctl) {
128242e9a92fSRobert Love 	case FC_RCTL_BA_ACC:
128342e9a92fSRobert Love 		ap = fc_frame_payload_get(fp, sizeof(*ap));
128442e9a92fSRobert Love 		if (!ap)
128542e9a92fSRobert Love 			break;
128642e9a92fSRobert Love 
128742e9a92fSRobert Love 		/*
128842e9a92fSRobert Love 		 * Decide whether to establish a Recovery Qualifier.
128942e9a92fSRobert Love 		 * We do this if there is a non-empty SEQ_CNT range and
129042e9a92fSRobert Love 		 * SEQ_ID is the same as the one we aborted.
129142e9a92fSRobert Love 		 */
129242e9a92fSRobert Love 		low = ntohs(ap->ba_low_seq_cnt);
129342e9a92fSRobert Love 		high = ntohs(ap->ba_high_seq_cnt);
129442e9a92fSRobert Love 		if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
129542e9a92fSRobert Love 		    (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
129642e9a92fSRobert Love 		     ap->ba_seq_id == ep->seq_id) && low != high) {
129742e9a92fSRobert Love 			ep->esb_stat |= ESB_ST_REC_QUAL;
129842e9a92fSRobert Love 			fc_exch_hold(ep);  /* hold for recovery qualifier */
129942e9a92fSRobert Love 			has_rec = 1;
130042e9a92fSRobert Love 		}
130142e9a92fSRobert Love 		break;
130242e9a92fSRobert Love 	case FC_RCTL_BA_RJT:
130342e9a92fSRobert Love 		break;
130442e9a92fSRobert Love 	default:
130542e9a92fSRobert Love 		break;
130642e9a92fSRobert Love 	}
130742e9a92fSRobert Love 
130842e9a92fSRobert Love 	resp = ep->resp;
130942e9a92fSRobert Love 	ex_resp_arg = ep->arg;
131042e9a92fSRobert Love 
131142e9a92fSRobert Love 	/* do we need to do some other checks here. Can we reuse more of
131242e9a92fSRobert Love 	 * fc_exch_recv_seq_resp
131342e9a92fSRobert Love 	 */
131442e9a92fSRobert Love 	sp = &ep->seq;
131542e9a92fSRobert Love 	/*
131642e9a92fSRobert Love 	 * do we want to check END_SEQ as well as LAST_SEQ here?
131742e9a92fSRobert Love 	 */
131842e9a92fSRobert Love 	if (ep->fh_type != FC_TYPE_FCP &&
131942e9a92fSRobert Love 	    ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
132042e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
132142e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
132242e9a92fSRobert Love 	if (!rc)
1323b2f0091fSVasu Dev 		fc_exch_delete(ep);
132442e9a92fSRobert Love 
132542e9a92fSRobert Love 	if (resp)
132642e9a92fSRobert Love 		resp(sp, fp, ex_resp_arg);
132742e9a92fSRobert Love 	else
132842e9a92fSRobert Love 		fc_frame_free(fp);
132942e9a92fSRobert Love 
133042e9a92fSRobert Love 	if (has_rec)
133142e9a92fSRobert Love 		fc_exch_timer_set(ep, ep->r_a_tov);
133242e9a92fSRobert Love 
133342e9a92fSRobert Love }
133442e9a92fSRobert Love 
133542e9a92fSRobert Love /*
133642e9a92fSRobert Love  * Receive BLS sequence.
133742e9a92fSRobert Love  * This is always a sequence initiated by the remote side.
133842e9a92fSRobert Love  * We may be either the originator or recipient of the exchange.
133942e9a92fSRobert Love  */
134042e9a92fSRobert Love static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
134142e9a92fSRobert Love {
134242e9a92fSRobert Love 	struct fc_frame_header *fh;
134342e9a92fSRobert Love 	struct fc_exch *ep;
134442e9a92fSRobert Love 	u32 f_ctl;
134542e9a92fSRobert Love 
134642e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
134742e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
134842e9a92fSRobert Love 	fr_seq(fp) = NULL;
134942e9a92fSRobert Love 
135042e9a92fSRobert Love 	ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
135142e9a92fSRobert Love 			  ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
135242e9a92fSRobert Love 	if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
135342e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
135442e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
135542e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
135642e9a92fSRobert Love 	}
135742e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_CTX) {
135842e9a92fSRobert Love 		/*
135942e9a92fSRobert Love 		 * A response to a sequence we initiated.
136042e9a92fSRobert Love 		 * This should only be ACKs for class 2 or F.
136142e9a92fSRobert Love 		 */
136242e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
136342e9a92fSRobert Love 		case FC_RCTL_ACK_1:
136442e9a92fSRobert Love 		case FC_RCTL_ACK_0:
136542e9a92fSRobert Love 			break;
136642e9a92fSRobert Love 		default:
13677414705eSRobert Love 			FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
136842e9a92fSRobert Love 				    fh->fh_r_ctl,
136942e9a92fSRobert Love 				    fc_exch_rctl_name(fh->fh_r_ctl));
137042e9a92fSRobert Love 			break;
137142e9a92fSRobert Love 		}
137242e9a92fSRobert Love 		fc_frame_free(fp);
137342e9a92fSRobert Love 	} else {
137442e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
137542e9a92fSRobert Love 		case FC_RCTL_BA_RJT:
137642e9a92fSRobert Love 		case FC_RCTL_BA_ACC:
137742e9a92fSRobert Love 			if (ep)
137842e9a92fSRobert Love 				fc_exch_abts_resp(ep, fp);
137942e9a92fSRobert Love 			else
138042e9a92fSRobert Love 				fc_frame_free(fp);
138142e9a92fSRobert Love 			break;
138242e9a92fSRobert Love 		case FC_RCTL_BA_ABTS:
138342e9a92fSRobert Love 			fc_exch_recv_abts(ep, fp);
138442e9a92fSRobert Love 			break;
138542e9a92fSRobert Love 		default:			/* ignore junk */
138642e9a92fSRobert Love 			fc_frame_free(fp);
138742e9a92fSRobert Love 			break;
138842e9a92fSRobert Love 		}
138942e9a92fSRobert Love 	}
139042e9a92fSRobert Love 	if (ep)
139142e9a92fSRobert Love 		fc_exch_release(ep);	/* release hold taken by fc_exch_find */
139242e9a92fSRobert Love }
139342e9a92fSRobert Love 
139442e9a92fSRobert Love /*
139542e9a92fSRobert Love  * Accept sequence with LS_ACC.
139642e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
139742e9a92fSRobert Love  * originator will repeat the sequence.
139842e9a92fSRobert Love  */
139942e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *req_sp)
140042e9a92fSRobert Love {
140142e9a92fSRobert Love 	struct fc_seq *sp;
140242e9a92fSRobert Love 	struct fc_els_ls_acc *acc;
140342e9a92fSRobert Love 	struct fc_frame *fp;
140442e9a92fSRobert Love 
140542e9a92fSRobert Love 	sp = fc_seq_start_next(req_sp);
140642e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
140742e9a92fSRobert Love 	if (fp) {
140842e9a92fSRobert Love 		acc = fc_frame_payload_get(fp, sizeof(*acc));
140942e9a92fSRobert Love 		memset(acc, 0, sizeof(*acc));
141042e9a92fSRobert Love 		acc->la_cmd = ELS_LS_ACC;
141142e9a92fSRobert Love 		fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
141242e9a92fSRobert Love 	}
141342e9a92fSRobert Love }
141442e9a92fSRobert Love 
141542e9a92fSRobert Love /*
141642e9a92fSRobert Love  * Reject sequence with ELS LS_RJT.
141742e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
141842e9a92fSRobert Love  * originator will repeat the sequence.
141942e9a92fSRobert Love  */
142042e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
142142e9a92fSRobert Love 			  enum fc_els_rjt_explan explan)
142242e9a92fSRobert Love {
142342e9a92fSRobert Love 	struct fc_seq *sp;
142442e9a92fSRobert Love 	struct fc_els_ls_rjt *rjt;
142542e9a92fSRobert Love 	struct fc_frame *fp;
142642e9a92fSRobert Love 
142742e9a92fSRobert Love 	sp = fc_seq_start_next(req_sp);
142842e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
142942e9a92fSRobert Love 	if (fp) {
143042e9a92fSRobert Love 		rjt = fc_frame_payload_get(fp, sizeof(*rjt));
143142e9a92fSRobert Love 		memset(rjt, 0, sizeof(*rjt));
143242e9a92fSRobert Love 		rjt->er_cmd = ELS_LS_RJT;
143342e9a92fSRobert Love 		rjt->er_reason = reason;
143442e9a92fSRobert Love 		rjt->er_explan = explan;
143542e9a92fSRobert Love 		fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
143642e9a92fSRobert Love 	}
143742e9a92fSRobert Love }
143842e9a92fSRobert Love 
143942e9a92fSRobert Love static void fc_exch_reset(struct fc_exch *ep)
144042e9a92fSRobert Love {
144142e9a92fSRobert Love 	struct fc_seq *sp;
144242e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *, void *);
144342e9a92fSRobert Love 	void *arg;
144442e9a92fSRobert Love 	int rc = 1;
144542e9a92fSRobert Love 
144642e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
144742e9a92fSRobert Love 	ep->state |= FC_EX_RST_CLEANUP;
144842e9a92fSRobert Love 	/*
144942e9a92fSRobert Love 	 * we really want to call del_timer_sync, but cannot due
145042e9a92fSRobert Love 	 * to the lport calling with the lport lock held (some resp
145142e9a92fSRobert Love 	 * functions can also grab the lport lock which could cause
145242e9a92fSRobert Love 	 * a deadlock).
145342e9a92fSRobert Love 	 */
145442e9a92fSRobert Love 	if (cancel_delayed_work(&ep->timeout_work))
145542e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for timer */
145642e9a92fSRobert Love 	resp = ep->resp;
145742e9a92fSRobert Love 	ep->resp = NULL;
145842e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL)
145942e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec_qual */
146042e9a92fSRobert Love 	ep->esb_stat &= ~ESB_ST_REC_QUAL;
146142e9a92fSRobert Love 	arg = ep->arg;
146242e9a92fSRobert Love 	sp = &ep->seq;
146342e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
146442e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
146542e9a92fSRobert Love 	if (!rc)
1466b2f0091fSVasu Dev 		fc_exch_delete(ep);
146742e9a92fSRobert Love 
146842e9a92fSRobert Love 	if (resp)
146942e9a92fSRobert Love 		resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
147042e9a92fSRobert Love }
147142e9a92fSRobert Love 
1472b2f0091fSVasu Dev /**
1473b2f0091fSVasu Dev  * fc_exch_pool_reset() - Resets an per cpu exches pool.
1474b2f0091fSVasu Dev  * @lport:	ptr to the local port
1475b2f0091fSVasu Dev  * @pool:	ptr to the per cpu exches pool
1476b2f0091fSVasu Dev  * @sid:	source FC ID
1477b2f0091fSVasu Dev  * @did:	destination FC ID
1478b2f0091fSVasu Dev  *
1479b2f0091fSVasu Dev  * Resets an per cpu exches pool, releasing its all sequences
1480b2f0091fSVasu Dev  * and exchanges. If sid is non-zero, then reset only exchanges
1481b2f0091fSVasu Dev  * we sourced from that FID. If did is non-zero, reset only
1482b2f0091fSVasu Dev  * exchanges destined to that FID.
148342e9a92fSRobert Love  */
1484b2f0091fSVasu Dev static void fc_exch_pool_reset(struct fc_lport *lport,
1485b2f0091fSVasu Dev 			       struct fc_exch_pool *pool,
1486b2f0091fSVasu Dev 			       u32 sid, u32 did)
148742e9a92fSRobert Love {
148842e9a92fSRobert Love 	struct fc_exch *ep;
148942e9a92fSRobert Love 	struct fc_exch *next;
149042e9a92fSRobert Love 
1491b2f0091fSVasu Dev 	spin_lock_bh(&pool->lock);
149242e9a92fSRobert Love restart:
1493b2f0091fSVasu Dev 	list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
1494b2f0091fSVasu Dev 		if ((lport == ep->lp) &&
149552ff878cSVasu Dev 		    (sid == 0 || sid == ep->sid) &&
149642e9a92fSRobert Love 		    (did == 0 || did == ep->did)) {
149742e9a92fSRobert Love 			fc_exch_hold(ep);
1498b2f0091fSVasu Dev 			spin_unlock_bh(&pool->lock);
149942e9a92fSRobert Love 
150042e9a92fSRobert Love 			fc_exch_reset(ep);
150142e9a92fSRobert Love 
150242e9a92fSRobert Love 			fc_exch_release(ep);
1503b2f0091fSVasu Dev 			spin_lock_bh(&pool->lock);
150442e9a92fSRobert Love 
150542e9a92fSRobert Love 			/*
150652ff878cSVasu Dev 			 * must restart loop incase while lock
150752ff878cSVasu Dev 			 * was down multiple eps were released.
150842e9a92fSRobert Love 			 */
150942e9a92fSRobert Love 			goto restart;
151042e9a92fSRobert Love 		}
151142e9a92fSRobert Love 	}
1512b2f0091fSVasu Dev 	spin_unlock_bh(&pool->lock);
1513b2f0091fSVasu Dev }
1514b2f0091fSVasu Dev 
1515b2f0091fSVasu Dev /**
1516b2f0091fSVasu Dev  * fc_exch_mgr_reset() - Resets all EMs of a lport
1517b2f0091fSVasu Dev  * @lport:	ptr to the local port
1518b2f0091fSVasu Dev  * @sid:	source FC ID
1519b2f0091fSVasu Dev  * @did:	destination FC ID
1520b2f0091fSVasu Dev  *
1521b2f0091fSVasu Dev  * Reset all EMs of a lport, releasing its all sequences and
1522b2f0091fSVasu Dev  * exchanges. If sid is non-zero, then reset only exchanges
1523b2f0091fSVasu Dev  * we sourced from that FID. If did is non-zero, reset only
1524b2f0091fSVasu Dev  * exchanges destined to that FID.
1525b2f0091fSVasu Dev  */
1526b2f0091fSVasu Dev void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1527b2f0091fSVasu Dev {
1528b2f0091fSVasu Dev 	struct fc_exch_mgr_anchor *ema;
1529b2f0091fSVasu Dev 	unsigned int cpu;
1530b2f0091fSVasu Dev 
1531b2f0091fSVasu Dev 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
1532b2f0091fSVasu Dev 		for_each_possible_cpu(cpu)
1533b2f0091fSVasu Dev 			fc_exch_pool_reset(lport,
1534b2f0091fSVasu Dev 					   per_cpu_ptr(ema->mp->pool, cpu),
1535b2f0091fSVasu Dev 					   sid, did);
153642e9a92fSRobert Love 	}
153752ff878cSVasu Dev }
153842e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_reset);
153942e9a92fSRobert Love 
154042e9a92fSRobert Love /*
154142e9a92fSRobert Love  * Handle incoming ELS REC - Read Exchange Concise.
154242e9a92fSRobert Love  * Note that the requesting port may be different than the S_ID in the request.
154342e9a92fSRobert Love  */
154442e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
154542e9a92fSRobert Love {
154642e9a92fSRobert Love 	struct fc_frame *fp;
154742e9a92fSRobert Love 	struct fc_exch *ep;
154842e9a92fSRobert Love 	struct fc_exch_mgr *em;
154942e9a92fSRobert Love 	struct fc_els_rec *rp;
155042e9a92fSRobert Love 	struct fc_els_rec_acc *acc;
155142e9a92fSRobert Love 	enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
155242e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
155342e9a92fSRobert Love 	u32 sid;
155442e9a92fSRobert Love 	u16 rxid;
155542e9a92fSRobert Love 	u16 oxid;
155642e9a92fSRobert Love 
155742e9a92fSRobert Love 	rp = fc_frame_payload_get(rfp, sizeof(*rp));
155842e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
155942e9a92fSRobert Love 	if (!rp)
156042e9a92fSRobert Love 		goto reject;
156142e9a92fSRobert Love 	sid = ntoh24(rp->rec_s_id);
156242e9a92fSRobert Love 	rxid = ntohs(rp->rec_rx_id);
156342e9a92fSRobert Love 	oxid = ntohs(rp->rec_ox_id);
156442e9a92fSRobert Love 
156542e9a92fSRobert Love 	/*
156642e9a92fSRobert Love 	 * Currently it's hard to find the local S_ID from the exchange
156742e9a92fSRobert Love 	 * manager.  This will eventually be fixed, but for now it's easier
156842e9a92fSRobert Love 	 * to lookup the subject exchange twice, once as if we were
156942e9a92fSRobert Love 	 * the initiator, and then again if we weren't.
157042e9a92fSRobert Love 	 */
157142e9a92fSRobert Love 	em = fc_seq_exch(sp)->em;
157242e9a92fSRobert Love 	ep = fc_exch_find(em, oxid);
157342e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
157442e9a92fSRobert Love 	if (ep && ep->oid == sid) {
157542e9a92fSRobert Love 		if (ep->rxid != FC_XID_UNKNOWN &&
157642e9a92fSRobert Love 		    rxid != FC_XID_UNKNOWN &&
157742e9a92fSRobert Love 		    ep->rxid != rxid)
157842e9a92fSRobert Love 			goto rel;
157942e9a92fSRobert Love 	} else {
158042e9a92fSRobert Love 		if (ep)
158142e9a92fSRobert Love 			fc_exch_release(ep);
158242e9a92fSRobert Love 		ep = NULL;
158342e9a92fSRobert Love 		if (rxid != FC_XID_UNKNOWN)
158442e9a92fSRobert Love 			ep = fc_exch_find(em, rxid);
158542e9a92fSRobert Love 		if (!ep)
158642e9a92fSRobert Love 			goto reject;
158742e9a92fSRobert Love 	}
158842e9a92fSRobert Love 
158942e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
159042e9a92fSRobert Love 	if (!fp) {
159142e9a92fSRobert Love 		fc_exch_done(sp);
159242e9a92fSRobert Love 		goto out;
159342e9a92fSRobert Love 	}
159442e9a92fSRobert Love 	sp = fc_seq_start_next(sp);
159542e9a92fSRobert Love 	acc = fc_frame_payload_get(fp, sizeof(*acc));
159642e9a92fSRobert Love 	memset(acc, 0, sizeof(*acc));
159742e9a92fSRobert Love 	acc->reca_cmd = ELS_LS_ACC;
159842e9a92fSRobert Love 	acc->reca_ox_id = rp->rec_ox_id;
159942e9a92fSRobert Love 	memcpy(acc->reca_ofid, rp->rec_s_id, 3);
160042e9a92fSRobert Love 	acc->reca_rx_id = htons(ep->rxid);
160142e9a92fSRobert Love 	if (ep->sid == ep->oid)
160242e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->did);
160342e9a92fSRobert Love 	else
160442e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->sid);
160542e9a92fSRobert Love 	acc->reca_fc4value = htonl(ep->seq.rec_data);
160642e9a92fSRobert Love 	acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
160742e9a92fSRobert Love 						 ESB_ST_SEQ_INIT |
160842e9a92fSRobert Love 						 ESB_ST_COMPLETE));
160942e9a92fSRobert Love 	sp = fc_seq_start_next(sp);
161042e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
161142e9a92fSRobert Love out:
161242e9a92fSRobert Love 	fc_exch_release(ep);
161342e9a92fSRobert Love 	fc_frame_free(rfp);
161442e9a92fSRobert Love 	return;
161542e9a92fSRobert Love 
161642e9a92fSRobert Love rel:
161742e9a92fSRobert Love 	fc_exch_release(ep);
161842e9a92fSRobert Love reject:
161942e9a92fSRobert Love 	fc_seq_ls_rjt(sp, reason, explan);
162042e9a92fSRobert Love 	fc_frame_free(rfp);
162142e9a92fSRobert Love }
162242e9a92fSRobert Love 
162342e9a92fSRobert Love /*
162442e9a92fSRobert Love  * Handle response from RRQ.
162542e9a92fSRobert Love  * Not much to do here, really.
162642e9a92fSRobert Love  * Should report errors.
162742e9a92fSRobert Love  *
162842e9a92fSRobert Love  * TODO: fix error handler.
162942e9a92fSRobert Love  */
163042e9a92fSRobert Love static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
163142e9a92fSRobert Love {
163242e9a92fSRobert Love 	struct fc_exch *aborted_ep = arg;
163342e9a92fSRobert Love 	unsigned int op;
163442e9a92fSRobert Love 
163542e9a92fSRobert Love 	if (IS_ERR(fp)) {
163642e9a92fSRobert Love 		int err = PTR_ERR(fp);
163742e9a92fSRobert Love 
163878342da3SVasu Dev 		if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
163942e9a92fSRobert Love 			goto cleanup;
16407414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
16417414705eSRobert Love 			    "frame error %d\n", err);
164242e9a92fSRobert Love 		return;
164342e9a92fSRobert Love 	}
164442e9a92fSRobert Love 
164542e9a92fSRobert Love 	op = fc_frame_payload_op(fp);
164642e9a92fSRobert Love 	fc_frame_free(fp);
164742e9a92fSRobert Love 
164842e9a92fSRobert Love 	switch (op) {
164942e9a92fSRobert Love 	case ELS_LS_RJT:
16507414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
165142e9a92fSRobert Love 		/* fall through */
165242e9a92fSRobert Love 	case ELS_LS_ACC:
165342e9a92fSRobert Love 		goto cleanup;
165442e9a92fSRobert Love 	default:
16557414705eSRobert Love 		FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
16567414705eSRobert Love 			    "for RRQ", op);
165742e9a92fSRobert Love 		return;
165842e9a92fSRobert Love 	}
165942e9a92fSRobert Love 
166042e9a92fSRobert Love cleanup:
166142e9a92fSRobert Love 	fc_exch_done(&aborted_ep->seq);
166242e9a92fSRobert Love 	/* drop hold for rec qual */
166342e9a92fSRobert Love 	fc_exch_release(aborted_ep);
166442e9a92fSRobert Love }
166542e9a92fSRobert Love 
166642e9a92fSRobert Love /*
166742e9a92fSRobert Love  * Send ELS RRQ - Reinstate Recovery Qualifier.
166842e9a92fSRobert Love  * This tells the remote port to stop blocking the use of
166942e9a92fSRobert Love  * the exchange and the seq_cnt range.
167042e9a92fSRobert Love  */
167142e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *ep)
167242e9a92fSRobert Love {
167342e9a92fSRobert Love 	struct fc_lport *lp;
167442e9a92fSRobert Love 	struct fc_els_rrq *rrq;
167542e9a92fSRobert Love 	struct fc_frame *fp;
167642e9a92fSRobert Love 	u32 did;
167742e9a92fSRobert Love 
167842e9a92fSRobert Love 	lp = ep->lp;
167942e9a92fSRobert Love 
168042e9a92fSRobert Love 	fp = fc_frame_alloc(lp, sizeof(*rrq));
168142e9a92fSRobert Love 	if (!fp)
1682a0cc1eccSVasu Dev 		goto retry;
1683a0cc1eccSVasu Dev 
168442e9a92fSRobert Love 	rrq = fc_frame_payload_get(fp, sizeof(*rrq));
168542e9a92fSRobert Love 	memset(rrq, 0, sizeof(*rrq));
168642e9a92fSRobert Love 	rrq->rrq_cmd = ELS_RRQ;
168742e9a92fSRobert Love 	hton24(rrq->rrq_s_id, ep->sid);
168842e9a92fSRobert Love 	rrq->rrq_ox_id = htons(ep->oxid);
168942e9a92fSRobert Love 	rrq->rrq_rx_id = htons(ep->rxid);
169042e9a92fSRobert Love 
169142e9a92fSRobert Love 	did = ep->did;
169242e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP)
169342e9a92fSRobert Love 		did = ep->sid;
169442e9a92fSRobert Love 
169542e9a92fSRobert Love 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
169642e9a92fSRobert Love 		       fc_host_port_id(lp->host), FC_TYPE_ELS,
169742e9a92fSRobert Love 		       FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
169842e9a92fSRobert Love 
1699a0cc1eccSVasu Dev 	if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov))
1700a0cc1eccSVasu Dev 		return;
1701a0cc1eccSVasu Dev 
1702a0cc1eccSVasu Dev retry:
1703a0cc1eccSVasu Dev 	spin_lock_bh(&ep->ex_lock);
1704a0cc1eccSVasu Dev 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1705a0cc1eccSVasu Dev 		spin_unlock_bh(&ep->ex_lock);
1706a0cc1eccSVasu Dev 		/* drop hold for rec qual */
1707a0cc1eccSVasu Dev 		fc_exch_release(ep);
170842e9a92fSRobert Love 		return;
170942e9a92fSRobert Love 	}
1710a0cc1eccSVasu Dev 	ep->esb_stat |= ESB_ST_REC_QUAL;
1711a0cc1eccSVasu Dev 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
1712a0cc1eccSVasu Dev 	spin_unlock_bh(&ep->ex_lock);
171342e9a92fSRobert Love }
171442e9a92fSRobert Love 
171542e9a92fSRobert Love 
171642e9a92fSRobert Love /*
171742e9a92fSRobert Love  * Handle incoming ELS RRQ - Reset Recovery Qualifier.
171842e9a92fSRobert Love  */
171942e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
172042e9a92fSRobert Love {
1721*3f127ad9SVasu Dev 	struct fc_exch *ep = NULL;	/* request or subject exchange */
172242e9a92fSRobert Love 	struct fc_els_rrq *rp;
172342e9a92fSRobert Love 	u32 sid;
172442e9a92fSRobert Love 	u16 xid;
172542e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
172642e9a92fSRobert Love 
172742e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
172842e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
172942e9a92fSRobert Love 	if (!rp)
173042e9a92fSRobert Love 		goto reject;
173142e9a92fSRobert Love 
173242e9a92fSRobert Love 	/*
173342e9a92fSRobert Love 	 * lookup subject exchange.
173442e9a92fSRobert Love 	 */
173542e9a92fSRobert Love 	ep = fc_seq_exch(sp);
173642e9a92fSRobert Love 	sid = ntoh24(rp->rrq_s_id);		/* subject source */
173742e9a92fSRobert Love 	xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
173842e9a92fSRobert Love 	ep = fc_exch_find(ep->em, xid);
173942e9a92fSRobert Love 
174042e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
174142e9a92fSRobert Love 	if (!ep)
174242e9a92fSRobert Love 		goto reject;
174342e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
174442e9a92fSRobert Love 	if (ep->oxid != ntohs(rp->rrq_ox_id))
174542e9a92fSRobert Love 		goto unlock_reject;
174642e9a92fSRobert Love 	if (ep->rxid != ntohs(rp->rrq_rx_id) &&
174742e9a92fSRobert Love 	    ep->rxid != FC_XID_UNKNOWN)
174842e9a92fSRobert Love 		goto unlock_reject;
174942e9a92fSRobert Love 	explan = ELS_EXPL_SID;
175042e9a92fSRobert Love 	if (ep->sid != sid)
175142e9a92fSRobert Love 		goto unlock_reject;
175242e9a92fSRobert Love 
175342e9a92fSRobert Love 	/*
175442e9a92fSRobert Love 	 * Clear Recovery Qualifier state, and cancel timer if complete.
175542e9a92fSRobert Love 	 */
175642e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL) {
175742e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_REC_QUAL;
175842e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec qual */
175942e9a92fSRobert Love 	}
176042e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
176142e9a92fSRobert Love 		if (cancel_delayed_work(&ep->timeout_work))
176242e9a92fSRobert Love 			atomic_dec(&ep->ex_refcnt);	/* drop timer hold */
176342e9a92fSRobert Love 	}
176442e9a92fSRobert Love 
176542e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
176642e9a92fSRobert Love 
176742e9a92fSRobert Love 	/*
176842e9a92fSRobert Love 	 * Send LS_ACC.
176942e9a92fSRobert Love 	 */
177042e9a92fSRobert Love 	fc_seq_ls_acc(sp);
1771*3f127ad9SVasu Dev 	goto out;
177242e9a92fSRobert Love 
177342e9a92fSRobert Love unlock_reject:
177442e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
177542e9a92fSRobert Love reject:
177642e9a92fSRobert Love 	fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
1777*3f127ad9SVasu Dev out:
177842e9a92fSRobert Love 	fc_frame_free(fp);
1779*3f127ad9SVasu Dev 	if (ep)
1780*3f127ad9SVasu Dev 		fc_exch_release(ep);	/* drop hold from fc_exch_find */
178142e9a92fSRobert Love }
178242e9a92fSRobert Love 
178396316099SVasu Dev struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
178496316099SVasu Dev 					   struct fc_exch_mgr *mp,
178596316099SVasu Dev 					   bool (*match)(struct fc_frame *))
178696316099SVasu Dev {
178796316099SVasu Dev 	struct fc_exch_mgr_anchor *ema;
178896316099SVasu Dev 
178996316099SVasu Dev 	ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
179096316099SVasu Dev 	if (!ema)
179196316099SVasu Dev 		return ema;
179296316099SVasu Dev 
179396316099SVasu Dev 	ema->mp = mp;
179496316099SVasu Dev 	ema->match = match;
179596316099SVasu Dev 	/* add EM anchor to EM anchors list */
179696316099SVasu Dev 	list_add_tail(&ema->ema_list, &lport->ema_list);
179796316099SVasu Dev 	kref_get(&mp->kref);
179896316099SVasu Dev 	return ema;
179996316099SVasu Dev }
180096316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_add);
180196316099SVasu Dev 
180296316099SVasu Dev static void fc_exch_mgr_destroy(struct kref *kref)
180396316099SVasu Dev {
180496316099SVasu Dev 	struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
180596316099SVasu Dev 
180696316099SVasu Dev 	mempool_destroy(mp->ep_pool);
1807e4bc50beSVasu Dev 	free_percpu(mp->pool);
180896316099SVasu Dev 	kfree(mp);
180996316099SVasu Dev }
181096316099SVasu Dev 
181196316099SVasu Dev void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
181296316099SVasu Dev {
181396316099SVasu Dev 	/* remove EM anchor from EM anchors list */
181496316099SVasu Dev 	list_del(&ema->ema_list);
181596316099SVasu Dev 	kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
181696316099SVasu Dev 	kfree(ema);
181796316099SVasu Dev }
181896316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_del);
181996316099SVasu Dev 
182042e9a92fSRobert Love struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
182142e9a92fSRobert Love 				      enum fc_class class,
182252ff878cSVasu Dev 				      u16 min_xid, u16 max_xid,
182352ff878cSVasu Dev 				      bool (*match)(struct fc_frame *))
182442e9a92fSRobert Love {
182542e9a92fSRobert Love 	struct fc_exch_mgr *mp;
1826e4bc50beSVasu Dev 	u16 pool_exch_range;
1827e4bc50beSVasu Dev 	size_t pool_size;
1828e4bc50beSVasu Dev 	unsigned int cpu;
1829e4bc50beSVasu Dev 	struct fc_exch_pool *pool;
183042e9a92fSRobert Love 
1831e4bc50beSVasu Dev 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
1832e4bc50beSVasu Dev 	    (min_xid & fc_cpu_mask) != 0) {
18337414705eSRobert Love 		FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
183442e9a92fSRobert Love 			     min_xid, max_xid);
183542e9a92fSRobert Love 		return NULL;
183642e9a92fSRobert Love 	}
183742e9a92fSRobert Love 
183842e9a92fSRobert Love 	/*
1839b2f0091fSVasu Dev 	 * allocate memory for EM
184042e9a92fSRobert Love 	 */
1841b2f0091fSVasu Dev 	mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
184242e9a92fSRobert Love 	if (!mp)
184342e9a92fSRobert Love 		return NULL;
184442e9a92fSRobert Love 
184542e9a92fSRobert Love 	mp->class = class;
184642e9a92fSRobert Love 	/* adjust em exch xid range for offload */
184742e9a92fSRobert Love 	mp->min_xid = min_xid;
184842e9a92fSRobert Love 	mp->max_xid = max_xid;
184942e9a92fSRobert Love 
185042e9a92fSRobert Love 	mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
185142e9a92fSRobert Love 	if (!mp->ep_pool)
185242e9a92fSRobert Love 		goto free_mp;
185342e9a92fSRobert Love 
1854e4bc50beSVasu Dev 	/*
1855e4bc50beSVasu Dev 	 * Setup per cpu exch pool with entire exchange id range equally
1856e4bc50beSVasu Dev 	 * divided across all cpus. The exch pointers array memory is
1857e4bc50beSVasu Dev 	 * allocated for exch range per pool.
1858e4bc50beSVasu Dev 	 */
1859e4bc50beSVasu Dev 	pool_exch_range = (mp->max_xid - mp->min_xid + 1) / (fc_cpu_mask + 1);
1860e4bc50beSVasu Dev 	mp->pool_max_index = pool_exch_range - 1;
1861e4bc50beSVasu Dev 
1862e4bc50beSVasu Dev 	/*
1863e4bc50beSVasu Dev 	 * Allocate and initialize per cpu exch pool
1864e4bc50beSVasu Dev 	 */
1865e4bc50beSVasu Dev 	pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
1866e4bc50beSVasu Dev 	mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
1867e4bc50beSVasu Dev 	if (!mp->pool)
1868e4bc50beSVasu Dev 		goto free_mempool;
1869e4bc50beSVasu Dev 	for_each_possible_cpu(cpu) {
1870e4bc50beSVasu Dev 		pool = per_cpu_ptr(mp->pool, cpu);
1871e4bc50beSVasu Dev 		spin_lock_init(&pool->lock);
1872e4bc50beSVasu Dev 		INIT_LIST_HEAD(&pool->ex_list);
1873e4bc50beSVasu Dev 	}
1874e4bc50beSVasu Dev 
187552ff878cSVasu Dev 	kref_init(&mp->kref);
187652ff878cSVasu Dev 	if (!fc_exch_mgr_add(lp, mp, match)) {
1877e4bc50beSVasu Dev 		free_percpu(mp->pool);
1878e4bc50beSVasu Dev 		goto free_mempool;
187952ff878cSVasu Dev 	}
188052ff878cSVasu Dev 
188152ff878cSVasu Dev 	/*
188252ff878cSVasu Dev 	 * Above kref_init() sets mp->kref to 1 and then
188352ff878cSVasu Dev 	 * call to fc_exch_mgr_add incremented mp->kref again,
188452ff878cSVasu Dev 	 * so adjust that extra increment.
188552ff878cSVasu Dev 	 */
188652ff878cSVasu Dev 	kref_put(&mp->kref, fc_exch_mgr_destroy);
188742e9a92fSRobert Love 	return mp;
188842e9a92fSRobert Love 
1889e4bc50beSVasu Dev free_mempool:
1890e4bc50beSVasu Dev 	mempool_destroy(mp->ep_pool);
189142e9a92fSRobert Love free_mp:
189242e9a92fSRobert Love 	kfree(mp);
189342e9a92fSRobert Love 	return NULL;
189442e9a92fSRobert Love }
189542e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_alloc);
189642e9a92fSRobert Love 
189752ff878cSVasu Dev void fc_exch_mgr_free(struct fc_lport *lport)
189842e9a92fSRobert Love {
189952ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema, *next;
190052ff878cSVasu Dev 
190152ff878cSVasu Dev 	list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
190252ff878cSVasu Dev 		fc_exch_mgr_del(ema);
190342e9a92fSRobert Love }
190442e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_free);
190542e9a92fSRobert Love 
190642e9a92fSRobert Love 
190742e9a92fSRobert Love struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
190842e9a92fSRobert Love 				struct fc_frame *fp,
190942e9a92fSRobert Love 				void (*resp)(struct fc_seq *,
191042e9a92fSRobert Love 					     struct fc_frame *fp,
191142e9a92fSRobert Love 					     void *arg),
191242e9a92fSRobert Love 				void (*destructor)(struct fc_seq *, void *),
191342e9a92fSRobert Love 				void *arg, u32 timer_msec)
191442e9a92fSRobert Love {
191542e9a92fSRobert Love 	struct fc_exch *ep;
191642e9a92fSRobert Love 	struct fc_seq *sp = NULL;
191742e9a92fSRobert Love 	struct fc_frame_header *fh;
191842e9a92fSRobert Love 	int rc = 1;
191942e9a92fSRobert Love 
192052ff878cSVasu Dev 	ep = fc_exch_alloc(lp, fp);
192142e9a92fSRobert Love 	if (!ep) {
192242e9a92fSRobert Love 		fc_frame_free(fp);
192342e9a92fSRobert Love 		return NULL;
192442e9a92fSRobert Love 	}
192542e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT;
192642e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
192742e9a92fSRobert Love 	fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
192842e9a92fSRobert Love 	ep->resp = resp;
192942e9a92fSRobert Love 	ep->destructor = destructor;
193042e9a92fSRobert Love 	ep->arg = arg;
193142e9a92fSRobert Love 	ep->r_a_tov = FC_DEF_R_A_TOV;
193242e9a92fSRobert Love 	ep->lp = lp;
193342e9a92fSRobert Love 	sp = &ep->seq;
193442e9a92fSRobert Love 
193542e9a92fSRobert Love 	ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
193642e9a92fSRobert Love 	ep->f_ctl = ntoh24(fh->fh_f_ctl);
193742e9a92fSRobert Love 	fc_exch_setup_hdr(ep, fp, ep->f_ctl);
193842e9a92fSRobert Love 	sp->cnt++;
193942e9a92fSRobert Love 
1940d7179680SVasu Dev 	if (ep->xid <= lp->lro_xid)
1941b277d2aaSYi Zou 		fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1942b277d2aaSYi Zou 
194342e9a92fSRobert Love 	if (unlikely(lp->tt.frame_send(lp, fp)))
194442e9a92fSRobert Love 		goto err;
194542e9a92fSRobert Love 
194642e9a92fSRobert Love 	if (timer_msec)
194742e9a92fSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
194842e9a92fSRobert Love 	ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not first seq */
194942e9a92fSRobert Love 
195042e9a92fSRobert Love 	if (ep->f_ctl & FC_FC_SEQ_INIT)
195142e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
195242e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
195342e9a92fSRobert Love 	return sp;
195442e9a92fSRobert Love err:
195542e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
195642e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
195742e9a92fSRobert Love 	if (!rc)
1958b2f0091fSVasu Dev 		fc_exch_delete(ep);
195942e9a92fSRobert Love 	return NULL;
196042e9a92fSRobert Love }
196142e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_seq_send);
196242e9a92fSRobert Love 
196342e9a92fSRobert Love /*
196442e9a92fSRobert Love  * Receive a frame
196542e9a92fSRobert Love  */
196652ff878cSVasu Dev void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp)
196742e9a92fSRobert Love {
196842e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
196952ff878cSVasu Dev 	struct fc_exch_mgr_anchor *ema;
197052ff878cSVasu Dev 	u32 f_ctl, found = 0;
197152ff878cSVasu Dev 	u16 oxid;
197242e9a92fSRobert Love 
197342e9a92fSRobert Love 	/* lport lock ? */
197452ff878cSVasu Dev 	if (!lp || lp->state == LPORT_ST_DISABLED) {
19757414705eSRobert Love 		FC_LPORT_DBG(lp, "Receiving frames for an lport that "
19767414705eSRobert Love 			     "has not been initialized correctly\n");
197742e9a92fSRobert Love 		fc_frame_free(fp);
197842e9a92fSRobert Love 		return;
197942e9a92fSRobert Love 	}
198042e9a92fSRobert Love 
198152ff878cSVasu Dev 	f_ctl = ntoh24(fh->fh_f_ctl);
198252ff878cSVasu Dev 	oxid = ntohs(fh->fh_ox_id);
198352ff878cSVasu Dev 	if (f_ctl & FC_FC_EX_CTX) {
198452ff878cSVasu Dev 		list_for_each_entry(ema, &lp->ema_list, ema_list) {
198552ff878cSVasu Dev 			if ((oxid >= ema->mp->min_xid) &&
198652ff878cSVasu Dev 			    (oxid <= ema->mp->max_xid)) {
198752ff878cSVasu Dev 				found = 1;
198852ff878cSVasu Dev 				break;
198952ff878cSVasu Dev 			}
199052ff878cSVasu Dev 		}
199152ff878cSVasu Dev 
199252ff878cSVasu Dev 		if (!found) {
199352ff878cSVasu Dev 			FC_LPORT_DBG(lp, "Received response for out "
199452ff878cSVasu Dev 				     "of range oxid:%hx\n", oxid);
199552ff878cSVasu Dev 			fc_frame_free(fp);
199652ff878cSVasu Dev 			return;
199752ff878cSVasu Dev 		}
199852ff878cSVasu Dev 	} else
199952ff878cSVasu Dev 		ema = list_entry(lp->ema_list.prev, typeof(*ema), ema_list);
200052ff878cSVasu Dev 
200142e9a92fSRobert Love 	/*
200242e9a92fSRobert Love 	 * If frame is marked invalid, just drop it.
200342e9a92fSRobert Love 	 */
200442e9a92fSRobert Love 	switch (fr_eof(fp)) {
200542e9a92fSRobert Love 	case FC_EOF_T:
200642e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
200742e9a92fSRobert Love 			skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
200842e9a92fSRobert Love 		/* fall through */
200942e9a92fSRobert Love 	case FC_EOF_N:
201042e9a92fSRobert Love 		if (fh->fh_type == FC_TYPE_BLS)
201152ff878cSVasu Dev 			fc_exch_recv_bls(ema->mp, fp);
201242e9a92fSRobert Love 		else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
201342e9a92fSRobert Love 			 FC_FC_EX_CTX)
201452ff878cSVasu Dev 			fc_exch_recv_seq_resp(ema->mp, fp);
201542e9a92fSRobert Love 		else if (f_ctl & FC_FC_SEQ_CTX)
201652ff878cSVasu Dev 			fc_exch_recv_resp(ema->mp, fp);
201742e9a92fSRobert Love 		else
201852ff878cSVasu Dev 			fc_exch_recv_req(lp, ema->mp, fp);
201942e9a92fSRobert Love 		break;
202042e9a92fSRobert Love 	default:
2021d459b7eaSRobert Love 		FC_LPORT_DBG(lp, "dropping invalid frame (eof %x)", fr_eof(fp));
202242e9a92fSRobert Love 		fc_frame_free(fp);
202342e9a92fSRobert Love 	}
202442e9a92fSRobert Love }
202542e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_recv);
202642e9a92fSRobert Love 
202742e9a92fSRobert Love int fc_exch_init(struct fc_lport *lp)
202842e9a92fSRobert Love {
202942e9a92fSRobert Love 	if (!lp->tt.seq_start_next)
203042e9a92fSRobert Love 		lp->tt.seq_start_next = fc_seq_start_next;
203142e9a92fSRobert Love 
203242e9a92fSRobert Love 	if (!lp->tt.exch_seq_send)
203342e9a92fSRobert Love 		lp->tt.exch_seq_send = fc_exch_seq_send;
203442e9a92fSRobert Love 
203542e9a92fSRobert Love 	if (!lp->tt.seq_send)
203642e9a92fSRobert Love 		lp->tt.seq_send = fc_seq_send;
203742e9a92fSRobert Love 
203842e9a92fSRobert Love 	if (!lp->tt.seq_els_rsp_send)
203942e9a92fSRobert Love 		lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
204042e9a92fSRobert Love 
204142e9a92fSRobert Love 	if (!lp->tt.exch_done)
204242e9a92fSRobert Love 		lp->tt.exch_done = fc_exch_done;
204342e9a92fSRobert Love 
204442e9a92fSRobert Love 	if (!lp->tt.exch_mgr_reset)
204542e9a92fSRobert Love 		lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
204642e9a92fSRobert Love 
204742e9a92fSRobert Love 	if (!lp->tt.seq_exch_abort)
204842e9a92fSRobert Love 		lp->tt.seq_exch_abort = fc_seq_exch_abort;
204942e9a92fSRobert Love 
205089f19a59SVasu Dev 	return 0;
205189f19a59SVasu Dev }
205289f19a59SVasu Dev EXPORT_SYMBOL(fc_exch_init);
205389f19a59SVasu Dev 
205489f19a59SVasu Dev /**
205589f19a59SVasu Dev  * fc_setup_exch_mgr() - Setup an exchange manager
205689f19a59SVasu Dev  */
205789f19a59SVasu Dev int fc_setup_exch_mgr()
205889f19a59SVasu Dev {
205989f19a59SVasu Dev 	fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
206089f19a59SVasu Dev 					 0, SLAB_HWCACHE_ALIGN, NULL);
206189f19a59SVasu Dev 	if (!fc_em_cachep)
206289f19a59SVasu Dev 		return -ENOMEM;
206389f19a59SVasu Dev 
2064e4bc50beSVasu Dev 	/*
2065e4bc50beSVasu Dev 	 * Initialize fc_cpu_mask and fc_cpu_order. The
2066e4bc50beSVasu Dev 	 * fc_cpu_mask is set for nr_cpu_ids rounded up
2067e4bc50beSVasu Dev 	 * to order of 2's * power and order is stored
2068e4bc50beSVasu Dev 	 * in fc_cpu_order as this is later required in
2069e4bc50beSVasu Dev 	 * mapping between an exch id and exch array index
2070e4bc50beSVasu Dev 	 * in per cpu exch pool.
2071e4bc50beSVasu Dev 	 *
2072e4bc50beSVasu Dev 	 * This round up is required to align fc_cpu_mask
2073e4bc50beSVasu Dev 	 * to exchange id's lower bits such that all incoming
2074e4bc50beSVasu Dev 	 * frames of an exchange gets delivered to the same
2075e4bc50beSVasu Dev 	 * cpu on which exchange originated by simple bitwise
2076e4bc50beSVasu Dev 	 * AND operation between fc_cpu_mask and exchange id.
2077e4bc50beSVasu Dev 	 */
2078e4bc50beSVasu Dev 	fc_cpu_mask = 1;
2079e4bc50beSVasu Dev 	fc_cpu_order = 0;
2080e4bc50beSVasu Dev 	while (fc_cpu_mask < nr_cpu_ids) {
2081e4bc50beSVasu Dev 		fc_cpu_mask <<= 1;
2082e4bc50beSVasu Dev 		fc_cpu_order++;
2083e4bc50beSVasu Dev 	}
2084e4bc50beSVasu Dev 	fc_cpu_mask--;
2085e4bc50beSVasu Dev 
208642e9a92fSRobert Love 	return 0;
208742e9a92fSRobert Love }
208842e9a92fSRobert Love 
208942e9a92fSRobert Love void fc_destroy_exch_mgr(void)
209042e9a92fSRobert Love {
209142e9a92fSRobert Love 	kmem_cache_destroy(fc_em_cachep);
209242e9a92fSRobert Love }
2093