xref: /linux/drivers/scsi/libfc/fc_exch.c (revision 78342da3682ec843e3e6301af5c723c88a46c408)
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 
3542e9a92fSRobert Love #define	  FC_DEF_R_A_TOV      (10 * 1000) /* resource allocation timeout */
3642e9a92fSRobert Love 
3742e9a92fSRobert Love /*
3842e9a92fSRobert Love  * fc_exch_debug can be set in debugger or at compile time to get more logs.
3942e9a92fSRobert Love  */
4042e9a92fSRobert Love static int fc_exch_debug;
4142e9a92fSRobert Love 
4242e9a92fSRobert Love #define FC_DEBUG_EXCH(fmt...)			\
4342e9a92fSRobert Love 	do {					\
4442e9a92fSRobert Love 		if (fc_exch_debug)		\
4542e9a92fSRobert Love 			FC_DBG(fmt);		\
4642e9a92fSRobert Love 	} while (0)
4742e9a92fSRobert Love 
4842e9a92fSRobert Love static struct kmem_cache *fc_em_cachep;	/* cache for exchanges */
4942e9a92fSRobert Love 
5042e9a92fSRobert Love /*
5142e9a92fSRobert Love  * Structure and function definitions for managing Fibre Channel Exchanges
5242e9a92fSRobert Love  * and Sequences.
5342e9a92fSRobert Love  *
5442e9a92fSRobert Love  * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
5542e9a92fSRobert Love  *
5642e9a92fSRobert Love  * fc_exch_mgr holds the exchange state for an N port
5742e9a92fSRobert Love  *
5842e9a92fSRobert Love  * fc_exch holds state for one exchange and links to its active sequence.
5942e9a92fSRobert Love  *
6042e9a92fSRobert Love  * fc_seq holds the state for an individual sequence.
6142e9a92fSRobert Love  */
6242e9a92fSRobert Love 
6342e9a92fSRobert Love /*
6442e9a92fSRobert Love  * Exchange manager.
6542e9a92fSRobert Love  *
6642e9a92fSRobert Love  * This structure is the center for creating exchanges and sequences.
6742e9a92fSRobert Love  * It manages the allocation of exchange IDs.
6842e9a92fSRobert Love  */
6942e9a92fSRobert Love struct fc_exch_mgr {
7042e9a92fSRobert Love 	enum fc_class	class;		/* default class for sequences */
7142e9a92fSRobert Love 	spinlock_t	em_lock;	/* exchange manager lock,
7242e9a92fSRobert Love 					   must be taken before ex_lock */
7342e9a92fSRobert Love 	u16		last_xid;	/* last allocated exchange ID */
7442e9a92fSRobert Love 	u16		min_xid;	/* min exchange ID */
7542e9a92fSRobert Love 	u16		max_xid;	/* max exchange ID */
7642e9a92fSRobert Love 	u16		max_read;	/* max exchange ID for read */
7742e9a92fSRobert Love 	u16		last_read;	/* last xid allocated for read */
7842e9a92fSRobert Love 	u32	total_exches;		/* total allocated exchanges */
7942e9a92fSRobert Love 	struct list_head	ex_list;	/* allocated exchanges list */
8042e9a92fSRobert Love 	struct fc_lport	*lp;		/* fc device instance */
8142e9a92fSRobert Love 	mempool_t	*ep_pool;	/* reserve ep's */
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 	struct fc_exch **exches;	/* for exch pointers indexed by xid */
9742e9a92fSRobert Love };
9842e9a92fSRobert Love #define	fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
9942e9a92fSRobert Love 
10042e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *);
10142e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *);
10242e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason,
10342e9a92fSRobert Love 			  enum fc_els_rjt_explan);
10442e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *);
10542e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *);
10642e9a92fSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp);
10742e9a92fSRobert Love 
10842e9a92fSRobert Love /*
10942e9a92fSRobert Love  * Internal implementation notes.
11042e9a92fSRobert Love  *
11142e9a92fSRobert Love  * The exchange manager is one by default in libfc but LLD may choose
11242e9a92fSRobert Love  * to have one per CPU. The sequence manager is one per exchange manager
11342e9a92fSRobert Love  * and currently never separated.
11442e9a92fSRobert Love  *
11542e9a92fSRobert Love  * Section 9.8 in FC-FS-2 specifies:  "The SEQ_ID is a one-byte field
11642e9a92fSRobert Love  * assigned by the Sequence Initiator that shall be unique for a specific
11742e9a92fSRobert Love  * D_ID and S_ID pair while the Sequence is open."   Note that it isn't
11842e9a92fSRobert Love  * qualified by exchange ID, which one might think it would be.
11942e9a92fSRobert Love  * In practice this limits the number of open sequences and exchanges to 256
12042e9a92fSRobert Love  * per session.	 For most targets we could treat this limit as per exchange.
12142e9a92fSRobert Love  *
12242e9a92fSRobert Love  * The exchange and its sequence are freed when the last sequence is received.
12342e9a92fSRobert Love  * It's possible for the remote port to leave an exchange open without
12442e9a92fSRobert Love  * sending any sequences.
12542e9a92fSRobert Love  *
12642e9a92fSRobert Love  * Notes on reference counts:
12742e9a92fSRobert Love  *
12842e9a92fSRobert Love  * Exchanges are reference counted and exchange gets freed when the reference
12942e9a92fSRobert Love  * count becomes zero.
13042e9a92fSRobert Love  *
13142e9a92fSRobert Love  * Timeouts:
13242e9a92fSRobert Love  * Sequences are timed out for E_D_TOV and R_A_TOV.
13342e9a92fSRobert Love  *
13442e9a92fSRobert Love  * Sequence event handling:
13542e9a92fSRobert Love  *
13642e9a92fSRobert Love  * The following events may occur on initiator sequences:
13742e9a92fSRobert Love  *
13842e9a92fSRobert Love  *	Send.
13942e9a92fSRobert Love  *	    For now, the whole thing is sent.
14042e9a92fSRobert Love  *	Receive ACK
14142e9a92fSRobert Love  *	    This applies only to class F.
14242e9a92fSRobert Love  *	    The sequence is marked complete.
14342e9a92fSRobert Love  *	ULP completion.
14442e9a92fSRobert Love  *	    The upper layer calls fc_exch_done() when done
14542e9a92fSRobert Love  *	    with exchange and sequence tuple.
14642e9a92fSRobert Love  *	RX-inferred completion.
14742e9a92fSRobert Love  *	    When we receive the next sequence on the same exchange, we can
14842e9a92fSRobert Love  *	    retire the previous sequence ID.  (XXX not implemented).
14942e9a92fSRobert Love  *	Timeout.
15042e9a92fSRobert Love  *	    R_A_TOV frees the sequence ID.  If we're waiting for ACK,
15142e9a92fSRobert Love  *	    E_D_TOV causes abort and calls upper layer response handler
15242e9a92fSRobert Love  *	    with FC_EX_TIMEOUT error.
15342e9a92fSRobert Love  *	Receive RJT
15442e9a92fSRobert Love  *	    XXX defer.
15542e9a92fSRobert Love  *	Send ABTS
15642e9a92fSRobert Love  *	    On timeout.
15742e9a92fSRobert Love  *
15842e9a92fSRobert Love  * The following events may occur on recipient sequences:
15942e9a92fSRobert Love  *
16042e9a92fSRobert Love  *	Receive
16142e9a92fSRobert Love  *	    Allocate sequence for first frame received.
16242e9a92fSRobert Love  *	    Hold during receive handler.
16342e9a92fSRobert Love  *	    Release when final frame received.
16442e9a92fSRobert Love  *	    Keep status of last N of these for the ELS RES command.  XXX TBD.
16542e9a92fSRobert Love  *	Receive ABTS
16642e9a92fSRobert Love  *	    Deallocate sequence
16742e9a92fSRobert Love  *	Send RJT
16842e9a92fSRobert Love  *	    Deallocate
16942e9a92fSRobert Love  *
17042e9a92fSRobert Love  * For now, we neglect conditions where only part of a sequence was
17142e9a92fSRobert Love  * received or transmitted, or where out-of-order receipt is detected.
17242e9a92fSRobert Love  */
17342e9a92fSRobert Love 
17442e9a92fSRobert Love /*
17542e9a92fSRobert Love  * Locking notes:
17642e9a92fSRobert Love  *
17742e9a92fSRobert Love  * The EM code run in a per-CPU worker thread.
17842e9a92fSRobert Love  *
17942e9a92fSRobert Love  * To protect against concurrency between a worker thread code and timers,
18042e9a92fSRobert Love  * sequence allocation and deallocation must be locked.
18142e9a92fSRobert Love  *  - exchange refcnt can be done atomicly without locks.
18242e9a92fSRobert Love  *  - sequence allocation must be locked by exch lock.
18342e9a92fSRobert Love  *  - If the em_lock and ex_lock must be taken at the same time, then the
18442e9a92fSRobert Love  *    em_lock must be taken before the ex_lock.
18542e9a92fSRobert Love  */
18642e9a92fSRobert Love 
18742e9a92fSRobert Love /*
18842e9a92fSRobert Love  * opcode names for debugging.
18942e9a92fSRobert Love  */
19042e9a92fSRobert Love static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
19142e9a92fSRobert Love 
19242e9a92fSRobert Love #define FC_TABLE_SIZE(x)   (sizeof(x) / sizeof(x[0]))
19342e9a92fSRobert Love 
19442e9a92fSRobert Love static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
19542e9a92fSRobert Love 					      unsigned int max_index)
19642e9a92fSRobert Love {
19742e9a92fSRobert Love 	const char *name = NULL;
19842e9a92fSRobert Love 
19942e9a92fSRobert Love 	if (op < max_index)
20042e9a92fSRobert Love 		name = table[op];
20142e9a92fSRobert Love 	if (!name)
20242e9a92fSRobert Love 		name = "unknown";
20342e9a92fSRobert Love 	return name;
20442e9a92fSRobert Love }
20542e9a92fSRobert Love 
20642e9a92fSRobert Love static const char *fc_exch_rctl_name(unsigned int op)
20742e9a92fSRobert Love {
20842e9a92fSRobert Love 	return fc_exch_name_lookup(op, fc_exch_rctl_names,
20942e9a92fSRobert Love 				   FC_TABLE_SIZE(fc_exch_rctl_names));
21042e9a92fSRobert Love }
21142e9a92fSRobert Love 
21242e9a92fSRobert Love /*
21342e9a92fSRobert Love  * Hold an exchange - keep it from being freed.
21442e9a92fSRobert Love  */
21542e9a92fSRobert Love static void fc_exch_hold(struct fc_exch *ep)
21642e9a92fSRobert Love {
21742e9a92fSRobert Love 	atomic_inc(&ep->ex_refcnt);
21842e9a92fSRobert Love }
21942e9a92fSRobert Love 
22042e9a92fSRobert Love /*
22142e9a92fSRobert Love  * setup fc hdr by initializing few more FC header fields and sof/eof.
22242e9a92fSRobert Love  * Initialized fields by this func:
22342e9a92fSRobert Love  *	- fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt
22442e9a92fSRobert Love  *	- sof and eof
22542e9a92fSRobert Love  */
22642e9a92fSRobert Love static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
22742e9a92fSRobert Love 			      u32 f_ctl)
22842e9a92fSRobert Love {
22942e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
23042e9a92fSRobert Love 	u16 fill;
23142e9a92fSRobert Love 
23242e9a92fSRobert Love 	fr_sof(fp) = ep->class;
23342e9a92fSRobert Love 	if (ep->seq.cnt)
23442e9a92fSRobert Love 		fr_sof(fp) = fc_sof_normal(ep->class);
23542e9a92fSRobert Love 
23642e9a92fSRobert Love 	if (f_ctl & FC_FC_END_SEQ) {
23742e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_T;
23842e9a92fSRobert Love 		if (fc_sof_needs_ack(ep->class))
23942e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
24042e9a92fSRobert Love 		/*
24142e9a92fSRobert Love 		 * Form f_ctl.
24242e9a92fSRobert Love 		 * The number of fill bytes to make the length a 4-byte
24342e9a92fSRobert Love 		 * multiple is the low order 2-bits of the f_ctl.
24442e9a92fSRobert Love 		 * The fill itself will have been cleared by the frame
24542e9a92fSRobert Love 		 * allocation.
24642e9a92fSRobert Love 		 * After this, the length will be even, as expected by
24742e9a92fSRobert Love 		 * the transport.
24842e9a92fSRobert Love 		 */
24942e9a92fSRobert Love 		fill = fr_len(fp) & 3;
25042e9a92fSRobert Love 		if (fill) {
25142e9a92fSRobert Love 			fill = 4 - fill;
25242e9a92fSRobert Love 			/* TODO, this may be a problem with fragmented skb */
25342e9a92fSRobert Love 			skb_put(fp_skb(fp), fill);
25442e9a92fSRobert Love 			hton24(fh->fh_f_ctl, f_ctl | fill);
25542e9a92fSRobert Love 		}
25642e9a92fSRobert Love 	} else {
25742e9a92fSRobert Love 		WARN_ON(fr_len(fp) % 4 != 0);	/* no pad to non last frame */
25842e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
25942e9a92fSRobert Love 	}
26042e9a92fSRobert Love 
26142e9a92fSRobert Love 	/*
26242e9a92fSRobert Love 	 * Initialize remainig fh fields
26342e9a92fSRobert Love 	 * from fc_fill_fc_hdr
26442e9a92fSRobert Love 	 */
26542e9a92fSRobert Love 	fh->fh_ox_id = htons(ep->oxid);
26642e9a92fSRobert Love 	fh->fh_rx_id = htons(ep->rxid);
26742e9a92fSRobert Love 	fh->fh_seq_id = ep->seq.id;
26842e9a92fSRobert Love 	fh->fh_seq_cnt = htons(ep->seq.cnt);
26942e9a92fSRobert Love }
27042e9a92fSRobert Love 
27142e9a92fSRobert Love 
27242e9a92fSRobert Love /*
27342e9a92fSRobert Love  * Release a reference to an exchange.
27442e9a92fSRobert Love  * If the refcnt goes to zero and the exchange is complete, it is freed.
27542e9a92fSRobert Love  */
27642e9a92fSRobert Love static void fc_exch_release(struct fc_exch *ep)
27742e9a92fSRobert Love {
27842e9a92fSRobert Love 	struct fc_exch_mgr *mp;
27942e9a92fSRobert Love 
28042e9a92fSRobert Love 	if (atomic_dec_and_test(&ep->ex_refcnt)) {
28142e9a92fSRobert Love 		mp = ep->em;
28242e9a92fSRobert Love 		if (ep->destructor)
28342e9a92fSRobert Love 			ep->destructor(&ep->seq, ep->arg);
28442e9a92fSRobert Love 		if (ep->lp->tt.exch_put)
28542e9a92fSRobert Love 			ep->lp->tt.exch_put(ep->lp, mp, ep->xid);
28642e9a92fSRobert Love 		WARN_ON(!ep->esb_stat & ESB_ST_COMPLETE);
28742e9a92fSRobert Love 		mempool_free(ep, mp->ep_pool);
28842e9a92fSRobert Love 	}
28942e9a92fSRobert Love }
29042e9a92fSRobert Love 
29142e9a92fSRobert Love static int fc_exch_done_locked(struct fc_exch *ep)
29242e9a92fSRobert Love {
29342e9a92fSRobert Love 	int rc = 1;
29442e9a92fSRobert Love 
29542e9a92fSRobert Love 	/*
29642e9a92fSRobert Love 	 * We must check for completion in case there are two threads
29742e9a92fSRobert Love 	 * tyring to complete this. But the rrq code will reuse the
29842e9a92fSRobert Love 	 * ep, and in that case we only clear the resp and set it as
29942e9a92fSRobert Love 	 * complete, so it can be reused by the timer to send the rrq.
30042e9a92fSRobert Love 	 */
30142e9a92fSRobert Love 	ep->resp = NULL;
30242e9a92fSRobert Love 	if (ep->state & FC_EX_DONE)
30342e9a92fSRobert Love 		return rc;
30442e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_COMPLETE;
30542e9a92fSRobert Love 
30642e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
30742e9a92fSRobert Love 		ep->state |= FC_EX_DONE;
30842e9a92fSRobert Love 		if (cancel_delayed_work(&ep->timeout_work))
30942e9a92fSRobert Love 			atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
31042e9a92fSRobert Love 		rc = 0;
31142e9a92fSRobert Love 	}
31242e9a92fSRobert Love 	return rc;
31342e9a92fSRobert Love }
31442e9a92fSRobert Love 
31542e9a92fSRobert Love static void fc_exch_mgr_delete_ep(struct fc_exch *ep)
31642e9a92fSRobert Love {
31742e9a92fSRobert Love 	struct fc_exch_mgr *mp;
31842e9a92fSRobert Love 
31942e9a92fSRobert Love 	mp = ep->em;
32042e9a92fSRobert Love 	spin_lock_bh(&mp->em_lock);
32142e9a92fSRobert Love 	WARN_ON(mp->total_exches <= 0);
32242e9a92fSRobert Love 	mp->total_exches--;
32342e9a92fSRobert Love 	mp->exches[ep->xid - mp->min_xid] = NULL;
32442e9a92fSRobert Love 	list_del(&ep->ex_list);
32542e9a92fSRobert Love 	spin_unlock_bh(&mp->em_lock);
32642e9a92fSRobert Love 	fc_exch_release(ep);	/* drop hold for exch in mp */
32742e9a92fSRobert Love }
32842e9a92fSRobert Love 
32942e9a92fSRobert Love /*
33042e9a92fSRobert Love  * Internal version of fc_exch_timer_set - used with lock held.
33142e9a92fSRobert Love  */
33242e9a92fSRobert Love static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
33342e9a92fSRobert Love 					    unsigned int timer_msec)
33442e9a92fSRobert Love {
33542e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
33642e9a92fSRobert Love 		return;
33742e9a92fSRobert Love 
33842e9a92fSRobert Love 	FC_DEBUG_EXCH("Exchange (%4x) timed out, notifying the upper layer\n",
33942e9a92fSRobert Love 		      ep->xid);
34042e9a92fSRobert Love 	if (schedule_delayed_work(&ep->timeout_work,
34142e9a92fSRobert Love 				  msecs_to_jiffies(timer_msec)))
34242e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for timer */
34342e9a92fSRobert Love }
34442e9a92fSRobert Love 
34542e9a92fSRobert Love /*
34642e9a92fSRobert Love  * Set timer for an exchange.
34742e9a92fSRobert Love  * The time is a minimum delay in milliseconds until the timer fires.
34842e9a92fSRobert Love  * Used for upper level protocols to time out the exchange.
34942e9a92fSRobert Love  * The timer is cancelled when it fires or when the exchange completes.
35042e9a92fSRobert Love  * Returns non-zero if a timer couldn't be allocated.
35142e9a92fSRobert Love  */
35242e9a92fSRobert Love static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
35342e9a92fSRobert Love {
35442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
35542e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, timer_msec);
35642e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
35742e9a92fSRobert Love }
35842e9a92fSRobert Love 
35942e9a92fSRobert Love int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec)
36042e9a92fSRobert Love {
36142e9a92fSRobert Love 	struct fc_seq *sp;
36242e9a92fSRobert Love 	struct fc_exch *ep;
36342e9a92fSRobert Love 	struct fc_frame *fp;
36442e9a92fSRobert Love 	int error;
36542e9a92fSRobert Love 
36642e9a92fSRobert Love 	ep = fc_seq_exch(req_sp);
36742e9a92fSRobert Love 
36842e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
36942e9a92fSRobert Love 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
37042e9a92fSRobert Love 	    ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
37142e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
37242e9a92fSRobert Love 		return -ENXIO;
37342e9a92fSRobert Love 	}
37442e9a92fSRobert Love 
37542e9a92fSRobert Love 	/*
37642e9a92fSRobert Love 	 * Send the abort on a new sequence if possible.
37742e9a92fSRobert Love 	 */
37842e9a92fSRobert Love 	sp = fc_seq_start_next_locked(&ep->seq);
37942e9a92fSRobert Love 	if (!sp) {
38042e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
38142e9a92fSRobert Love 		return -ENOMEM;
38242e9a92fSRobert Love 	}
38342e9a92fSRobert Love 
38442e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
38542e9a92fSRobert Love 	if (timer_msec)
38642e9a92fSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
38742e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
38842e9a92fSRobert Love 
38942e9a92fSRobert Love 	/*
39042e9a92fSRobert Love 	 * If not logged into the fabric, don't send ABTS but leave
39142e9a92fSRobert Love 	 * sequence active until next timeout.
39242e9a92fSRobert Love 	 */
39342e9a92fSRobert Love 	if (!ep->sid)
39442e9a92fSRobert Love 		return 0;
39542e9a92fSRobert Love 
39642e9a92fSRobert Love 	/*
39742e9a92fSRobert Love 	 * Send an abort for the sequence that timed out.
39842e9a92fSRobert Love 	 */
39942e9a92fSRobert Love 	fp = fc_frame_alloc(ep->lp, 0);
40042e9a92fSRobert Love 	if (fp) {
40142e9a92fSRobert Love 		fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
40242e9a92fSRobert Love 			       FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
40342e9a92fSRobert Love 		error = fc_seq_send(ep->lp, sp, fp);
40442e9a92fSRobert Love 	} else
40542e9a92fSRobert Love 		error = -ENOBUFS;
40642e9a92fSRobert Love 	return error;
40742e9a92fSRobert Love }
40842e9a92fSRobert Love EXPORT_SYMBOL(fc_seq_exch_abort);
40942e9a92fSRobert Love 
41042e9a92fSRobert Love /*
41142e9a92fSRobert Love  * Exchange timeout - handle exchange timer expiration.
41242e9a92fSRobert Love  * The timer will have been cancelled before this is called.
41342e9a92fSRobert Love  */
41442e9a92fSRobert Love static void fc_exch_timeout(struct work_struct *work)
41542e9a92fSRobert Love {
41642e9a92fSRobert Love 	struct fc_exch *ep = container_of(work, struct fc_exch,
41742e9a92fSRobert Love 					  timeout_work.work);
41842e9a92fSRobert Love 	struct fc_seq *sp = &ep->seq;
41942e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
42042e9a92fSRobert Love 	void *arg;
42142e9a92fSRobert Love 	u32 e_stat;
42242e9a92fSRobert Love 	int rc = 1;
42342e9a92fSRobert Love 
42442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
42542e9a92fSRobert Love 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
42642e9a92fSRobert Love 		goto unlock;
42742e9a92fSRobert Love 
42842e9a92fSRobert Love 	e_stat = ep->esb_stat;
42942e9a92fSRobert Love 	if (e_stat & ESB_ST_COMPLETE) {
43042e9a92fSRobert Love 		ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
43142e9a92fSRobert Love 		if (e_stat & ESB_ST_REC_QUAL)
43242e9a92fSRobert Love 			fc_exch_rrq(ep);
43342e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
43442e9a92fSRobert Love 		goto done;
43542e9a92fSRobert Love 	} else {
43642e9a92fSRobert Love 		resp = ep->resp;
43742e9a92fSRobert Love 		arg = ep->arg;
43842e9a92fSRobert Love 		ep->resp = NULL;
43942e9a92fSRobert Love 		if (e_stat & ESB_ST_ABNORMAL)
44042e9a92fSRobert Love 			rc = fc_exch_done_locked(ep);
44142e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
44242e9a92fSRobert Love 		if (!rc)
44342e9a92fSRobert Love 			fc_exch_mgr_delete_ep(ep);
44442e9a92fSRobert Love 		if (resp)
44542e9a92fSRobert Love 			resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
44642e9a92fSRobert Love 		fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
44742e9a92fSRobert Love 		goto done;
44842e9a92fSRobert Love 	}
44942e9a92fSRobert Love unlock:
45042e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
45142e9a92fSRobert Love done:
45242e9a92fSRobert Love 	/*
45342e9a92fSRobert Love 	 * This release matches the hold taken when the timer was set.
45442e9a92fSRobert Love 	 */
45542e9a92fSRobert Love 	fc_exch_release(ep);
45642e9a92fSRobert Love }
45742e9a92fSRobert Love 
45842e9a92fSRobert Love /*
45942e9a92fSRobert Love  * Allocate a sequence.
46042e9a92fSRobert Love  *
46142e9a92fSRobert Love  * We don't support multiple originated sequences on the same exchange.
46242e9a92fSRobert Love  * By implication, any previously originated sequence on this exchange
46342e9a92fSRobert Love  * is complete, and we reallocate the same sequence.
46442e9a92fSRobert Love  */
46542e9a92fSRobert Love static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
46642e9a92fSRobert Love {
46742e9a92fSRobert Love 	struct fc_seq *sp;
46842e9a92fSRobert Love 
46942e9a92fSRobert Love 	sp = &ep->seq;
47042e9a92fSRobert Love 	sp->ssb_stat = 0;
47142e9a92fSRobert Love 	sp->cnt = 0;
47242e9a92fSRobert Love 	sp->id = seq_id;
47342e9a92fSRobert Love 	return sp;
47442e9a92fSRobert Love }
47542e9a92fSRobert Love 
47642e9a92fSRobert Love /*
47742e9a92fSRobert Love  * fc_em_alloc_xid - returns an xid based on request type
47842e9a92fSRobert Love  * @lp : ptr to associated lport
47942e9a92fSRobert Love  * @fp : ptr to the assocated frame
48042e9a92fSRobert Love  *
48142e9a92fSRobert Love  * check the associated fc_fsp_pkt to get scsi command type and
48242e9a92fSRobert Love  * command direction to decide from which range this exch id
48342e9a92fSRobert Love  * will be allocated from.
48442e9a92fSRobert Love  *
48542e9a92fSRobert Love  * Returns : 0 or an valid xid
48642e9a92fSRobert Love  */
48742e9a92fSRobert Love static u16 fc_em_alloc_xid(struct fc_exch_mgr *mp, const struct fc_frame *fp)
48842e9a92fSRobert Love {
48942e9a92fSRobert Love 	u16 xid, min, max;
49042e9a92fSRobert Love 	u16 *plast;
49142e9a92fSRobert Love 	struct fc_exch *ep = NULL;
49242e9a92fSRobert Love 
49342e9a92fSRobert Love 	if (mp->max_read) {
49442e9a92fSRobert Love 		if (fc_frame_is_read(fp)) {
49542e9a92fSRobert Love 			min = mp->min_xid;
49642e9a92fSRobert Love 			max = mp->max_read;
49742e9a92fSRobert Love 			plast = &mp->last_read;
49842e9a92fSRobert Love 		} else {
49942e9a92fSRobert Love 			min = mp->max_read + 1;
50042e9a92fSRobert Love 			max = mp->max_xid;
50142e9a92fSRobert Love 			plast = &mp->last_xid;
50242e9a92fSRobert Love 		}
50342e9a92fSRobert Love 	} else {
50442e9a92fSRobert Love 		min = mp->min_xid;
50542e9a92fSRobert Love 		max = mp->max_xid;
50642e9a92fSRobert Love 		plast = &mp->last_xid;
50742e9a92fSRobert Love 	}
50842e9a92fSRobert Love 	xid = *plast;
50942e9a92fSRobert Love 	do {
51042e9a92fSRobert Love 		xid = (xid == max) ? min : xid + 1;
51142e9a92fSRobert Love 		ep = mp->exches[xid - mp->min_xid];
51242e9a92fSRobert Love 	} while ((ep != NULL) && (xid != *plast));
51342e9a92fSRobert Love 
51442e9a92fSRobert Love 	if (unlikely(ep))
51542e9a92fSRobert Love 		xid = 0;
51642e9a92fSRobert Love 	else
51742e9a92fSRobert Love 		*plast = xid;
51842e9a92fSRobert Love 
51942e9a92fSRobert Love 	return xid;
52042e9a92fSRobert Love }
52142e9a92fSRobert Love 
52242e9a92fSRobert Love /*
52342e9a92fSRobert Love  * fc_exch_alloc - allocate an exchange.
52442e9a92fSRobert Love  * @mp : ptr to the exchange manager
52542e9a92fSRobert Love  * @xid: input xid
52642e9a92fSRobert Love  *
52742e9a92fSRobert Love  * if xid is supplied zero then assign next free exchange ID
52842e9a92fSRobert Love  * from exchange manager, otherwise use supplied xid.
52942e9a92fSRobert Love  * Returns with exch lock held.
53042e9a92fSRobert Love  */
53142e9a92fSRobert Love struct fc_exch *fc_exch_alloc(struct fc_exch_mgr *mp,
53242e9a92fSRobert Love 			      struct fc_frame *fp, u16 xid)
53342e9a92fSRobert Love {
53442e9a92fSRobert Love 	struct fc_exch *ep;
53542e9a92fSRobert Love 
53642e9a92fSRobert Love 	/* allocate memory for exchange */
53742e9a92fSRobert Love 	ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
53842e9a92fSRobert Love 	if (!ep) {
53942e9a92fSRobert Love 		atomic_inc(&mp->stats.no_free_exch);
54042e9a92fSRobert Love 		goto out;
54142e9a92fSRobert Love 	}
54242e9a92fSRobert Love 	memset(ep, 0, sizeof(*ep));
54342e9a92fSRobert Love 
54442e9a92fSRobert Love 	spin_lock_bh(&mp->em_lock);
54542e9a92fSRobert Love 	/* alloc xid if input xid 0 */
54642e9a92fSRobert Love 	if (!xid) {
54742e9a92fSRobert Love 		/* alloc a new xid */
54842e9a92fSRobert Love 		xid = fc_em_alloc_xid(mp, fp);
54942e9a92fSRobert Love 		if (!xid) {
55042e9a92fSRobert Love 			printk(KERN_ERR "fc_em_alloc_xid() failed\n");
55142e9a92fSRobert Love 			goto err;
55242e9a92fSRobert Love 		}
55342e9a92fSRobert Love 	}
55442e9a92fSRobert Love 
55542e9a92fSRobert Love 	fc_exch_hold(ep);	/* hold for exch in mp */
55642e9a92fSRobert Love 	spin_lock_init(&ep->ex_lock);
55742e9a92fSRobert Love 	/*
55842e9a92fSRobert Love 	 * Hold exch lock for caller to prevent fc_exch_reset()
55942e9a92fSRobert Love 	 * from releasing exch	while fc_exch_alloc() caller is
56042e9a92fSRobert Love 	 * still working on exch.
56142e9a92fSRobert Love 	 */
56242e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
56342e9a92fSRobert Love 
56442e9a92fSRobert Love 	mp->exches[xid - mp->min_xid] = ep;
56542e9a92fSRobert Love 	list_add_tail(&ep->ex_list, &mp->ex_list);
56642e9a92fSRobert Love 	fc_seq_alloc(ep, ep->seq_id++);
56742e9a92fSRobert Love 	mp->total_exches++;
56842e9a92fSRobert Love 	spin_unlock_bh(&mp->em_lock);
56942e9a92fSRobert Love 
57042e9a92fSRobert Love 	/*
57142e9a92fSRobert Love 	 *  update exchange
57242e9a92fSRobert Love 	 */
57342e9a92fSRobert Love 	ep->oxid = ep->xid = xid;
57442e9a92fSRobert Love 	ep->em = mp;
57542e9a92fSRobert Love 	ep->lp = mp->lp;
57642e9a92fSRobert Love 	ep->f_ctl = FC_FC_FIRST_SEQ;	/* next seq is first seq */
57742e9a92fSRobert Love 	ep->rxid = FC_XID_UNKNOWN;
57842e9a92fSRobert Love 	ep->class = mp->class;
57942e9a92fSRobert Love 	INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
58042e9a92fSRobert Love out:
58142e9a92fSRobert Love 	return ep;
58242e9a92fSRobert Love err:
58342e9a92fSRobert Love 	spin_unlock_bh(&mp->em_lock);
58442e9a92fSRobert Love 	atomic_inc(&mp->stats.no_free_exch_xid);
58542e9a92fSRobert Love 	mempool_free(ep, mp->ep_pool);
58642e9a92fSRobert Love 	return NULL;
58742e9a92fSRobert Love }
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 {
59542e9a92fSRobert Love 	struct fc_exch *ep = NULL;
59642e9a92fSRobert Love 
59742e9a92fSRobert Love 	if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
59842e9a92fSRobert Love 		spin_lock_bh(&mp->em_lock);
59942e9a92fSRobert Love 		ep = mp->exches[xid - mp->min_xid];
60042e9a92fSRobert Love 		if (ep) {
60142e9a92fSRobert Love 			fc_exch_hold(ep);
60242e9a92fSRobert Love 			WARN_ON(ep->xid != xid);
60342e9a92fSRobert Love 		}
60442e9a92fSRobert Love 		spin_unlock_bh(&mp->em_lock);
60542e9a92fSRobert Love 	}
60642e9a92fSRobert Love 	return ep;
60742e9a92fSRobert Love }
60842e9a92fSRobert Love 
60942e9a92fSRobert Love void fc_exch_done(struct fc_seq *sp)
61042e9a92fSRobert Love {
61142e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
61242e9a92fSRobert Love 	int rc;
61342e9a92fSRobert Love 
61442e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
61542e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
61642e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
61742e9a92fSRobert Love 	if (!rc)
61842e9a92fSRobert Love 		fc_exch_mgr_delete_ep(ep);
61942e9a92fSRobert Love }
62042e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_done);
62142e9a92fSRobert Love 
62242e9a92fSRobert Love /*
62342e9a92fSRobert Love  * Allocate a new exchange as responder.
62442e9a92fSRobert Love  * Sets the responder ID in the frame header.
62542e9a92fSRobert Love  */
62642e9a92fSRobert Love static struct fc_exch *fc_exch_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
62742e9a92fSRobert Love {
62842e9a92fSRobert Love 	struct fc_exch *ep;
62942e9a92fSRobert Love 	struct fc_frame_header *fh;
63042e9a92fSRobert Love 	u16 rxid;
63142e9a92fSRobert Love 
63242e9a92fSRobert Love 	ep = mp->lp->tt.exch_get(mp->lp, fp);
63342e9a92fSRobert Love 	if (ep) {
63442e9a92fSRobert Love 		ep->class = fc_frame_class(fp);
63542e9a92fSRobert Love 
63642e9a92fSRobert Love 		/*
63742e9a92fSRobert Love 		 * Set EX_CTX indicating we're responding on this exchange.
63842e9a92fSRobert Love 		 */
63942e9a92fSRobert Love 		ep->f_ctl |= FC_FC_EX_CTX;	/* we're responding */
64042e9a92fSRobert Love 		ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not new */
64142e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
64242e9a92fSRobert Love 		ep->sid = ntoh24(fh->fh_d_id);
64342e9a92fSRobert Love 		ep->did = ntoh24(fh->fh_s_id);
64442e9a92fSRobert Love 		ep->oid = ep->did;
64542e9a92fSRobert Love 
64642e9a92fSRobert Love 		/*
64742e9a92fSRobert Love 		 * Allocated exchange has placed the XID in the
64842e9a92fSRobert Love 		 * originator field. Move it to the responder field,
64942e9a92fSRobert Love 		 * and set the originator XID from the frame.
65042e9a92fSRobert Love 		 */
65142e9a92fSRobert Love 		ep->rxid = ep->xid;
65242e9a92fSRobert Love 		ep->oxid = ntohs(fh->fh_ox_id);
65342e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
65442e9a92fSRobert Love 		if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
65542e9a92fSRobert Love 			ep->esb_stat &= ~ESB_ST_SEQ_INIT;
65642e9a92fSRobert Love 
65742e9a92fSRobert Love 		/*
65842e9a92fSRobert Love 		 * Set the responder ID in the frame header.
65942e9a92fSRobert Love 		 * The old one should've been 0xffff.
66042e9a92fSRobert Love 		 * If it isn't, don't assign one.
66142e9a92fSRobert Love 		 * Incoming basic link service frames may specify
66242e9a92fSRobert Love 		 * a referenced RX_ID.
66342e9a92fSRobert Love 		 */
66442e9a92fSRobert Love 		if (fh->fh_type != FC_TYPE_BLS) {
66542e9a92fSRobert Love 			rxid = ntohs(fh->fh_rx_id);
66642e9a92fSRobert Love 			WARN_ON(rxid != FC_XID_UNKNOWN);
66742e9a92fSRobert Love 			fh->fh_rx_id = htons(ep->rxid);
66842e9a92fSRobert Love 		}
66942e9a92fSRobert Love 		fc_exch_hold(ep);	/* hold for caller */
67042e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);	/* lock from exch_get */
67142e9a92fSRobert Love 	}
67242e9a92fSRobert Love 	return ep;
67342e9a92fSRobert Love }
67442e9a92fSRobert Love 
67542e9a92fSRobert Love /*
67642e9a92fSRobert Love  * Find a sequence for receive where the other end is originating the sequence.
67742e9a92fSRobert Love  * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
67842e9a92fSRobert Love  * on the ep that should be released by the caller.
67942e9a92fSRobert Love  */
68042e9a92fSRobert Love static enum fc_pf_rjt_reason
68142e9a92fSRobert Love fc_seq_lookup_recip(struct fc_exch_mgr *mp, struct fc_frame *fp)
68242e9a92fSRobert Love {
68342e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
68442e9a92fSRobert Love 	struct fc_exch *ep = NULL;
68542e9a92fSRobert Love 	struct fc_seq *sp = NULL;
68642e9a92fSRobert Love 	enum fc_pf_rjt_reason reject = FC_RJT_NONE;
68742e9a92fSRobert Love 	u32 f_ctl;
68842e9a92fSRobert Love 	u16 xid;
68942e9a92fSRobert Love 
69042e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
69142e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
69242e9a92fSRobert Love 
69342e9a92fSRobert Love 	/*
69442e9a92fSRobert Love 	 * Lookup or create the exchange if we will be creating the sequence.
69542e9a92fSRobert Love 	 */
69642e9a92fSRobert Love 	if (f_ctl & FC_FC_EX_CTX) {
69742e9a92fSRobert Love 		xid = ntohs(fh->fh_ox_id);	/* we originated exch */
69842e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
69942e9a92fSRobert Love 		if (!ep) {
70042e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
70142e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
70242e9a92fSRobert Love 			goto out;
70342e9a92fSRobert Love 		}
70442e9a92fSRobert Love 		if (ep->rxid == FC_XID_UNKNOWN)
70542e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
70642e9a92fSRobert Love 		else if (ep->rxid != ntohs(fh->fh_rx_id)) {
70742e9a92fSRobert Love 			reject = FC_RJT_OX_ID;
70842e9a92fSRobert Love 			goto rel;
70942e9a92fSRobert Love 		}
71042e9a92fSRobert Love 	} else {
71142e9a92fSRobert Love 		xid = ntohs(fh->fh_rx_id);	/* we are the responder */
71242e9a92fSRobert Love 
71342e9a92fSRobert Love 		/*
71442e9a92fSRobert Love 		 * Special case for MDS issuing an ELS TEST with a
71542e9a92fSRobert Love 		 * bad rxid of 0.
71642e9a92fSRobert Love 		 * XXX take this out once we do the proper reject.
71742e9a92fSRobert Love 		 */
71842e9a92fSRobert Love 		if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
71942e9a92fSRobert Love 		    fc_frame_payload_op(fp) == ELS_TEST) {
72042e9a92fSRobert Love 			fh->fh_rx_id = htons(FC_XID_UNKNOWN);
72142e9a92fSRobert Love 			xid = FC_XID_UNKNOWN;
72242e9a92fSRobert Love 		}
72342e9a92fSRobert Love 
72442e9a92fSRobert Love 		/*
72542e9a92fSRobert Love 		 * new sequence - find the exchange
72642e9a92fSRobert Love 		 */
72742e9a92fSRobert Love 		ep = fc_exch_find(mp, xid);
72842e9a92fSRobert Love 		if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
72942e9a92fSRobert Love 			if (ep) {
73042e9a92fSRobert Love 				atomic_inc(&mp->stats.xid_busy);
73142e9a92fSRobert Love 				reject = FC_RJT_RX_ID;
73242e9a92fSRobert Love 				goto rel;
73342e9a92fSRobert Love 			}
73442e9a92fSRobert Love 			ep = fc_exch_resp(mp, fp);
73542e9a92fSRobert Love 			if (!ep) {
73642e9a92fSRobert Love 				reject = FC_RJT_EXCH_EST;	/* XXX */
73742e9a92fSRobert Love 				goto out;
73842e9a92fSRobert Love 			}
73942e9a92fSRobert Love 			xid = ep->xid;	/* get our XID */
74042e9a92fSRobert Love 		} else if (!ep) {
74142e9a92fSRobert Love 			atomic_inc(&mp->stats.xid_not_found);
74242e9a92fSRobert Love 			reject = FC_RJT_RX_ID;	/* XID not found */
74342e9a92fSRobert Love 			goto out;
74442e9a92fSRobert Love 		}
74542e9a92fSRobert Love 	}
74642e9a92fSRobert Love 
74742e9a92fSRobert Love 	/*
74842e9a92fSRobert Love 	 * At this point, we have the exchange held.
74942e9a92fSRobert Love 	 * Find or create the sequence.
75042e9a92fSRobert Love 	 */
75142e9a92fSRobert Love 	if (fc_sof_is_init(fr_sof(fp))) {
75242e9a92fSRobert Love 		sp = fc_seq_start_next(&ep->seq);
75342e9a92fSRobert Love 		if (!sp) {
75442e9a92fSRobert Love 			reject = FC_RJT_SEQ_XS;	/* exchange shortage */
75542e9a92fSRobert Love 			goto rel;
75642e9a92fSRobert Love 		}
75742e9a92fSRobert Love 		sp->id = fh->fh_seq_id;
75842e9a92fSRobert Love 		sp->ssb_stat |= SSB_ST_RESP;
75942e9a92fSRobert Love 	} else {
76042e9a92fSRobert Love 		sp = &ep->seq;
76142e9a92fSRobert Love 		if (sp->id != fh->fh_seq_id) {
76242e9a92fSRobert Love 			atomic_inc(&mp->stats.seq_not_found);
76342e9a92fSRobert Love 			reject = FC_RJT_SEQ_ID;	/* sequence/exch should exist */
76442e9a92fSRobert Love 			goto rel;
76542e9a92fSRobert Love 		}
76642e9a92fSRobert Love 	}
76742e9a92fSRobert Love 	WARN_ON(ep != fc_seq_exch(sp));
76842e9a92fSRobert Love 
76942e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
77042e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
77142e9a92fSRobert Love 
77242e9a92fSRobert Love 	fr_seq(fp) = sp;
77342e9a92fSRobert Love out:
77442e9a92fSRobert Love 	return reject;
77542e9a92fSRobert Love rel:
77642e9a92fSRobert Love 	fc_exch_done(&ep->seq);
77742e9a92fSRobert Love 	fc_exch_release(ep);	/* hold from fc_exch_find/fc_exch_resp */
77842e9a92fSRobert Love 	return reject;
77942e9a92fSRobert Love }
78042e9a92fSRobert Love 
78142e9a92fSRobert Love /*
78242e9a92fSRobert Love  * Find the sequence for a frame being received.
78342e9a92fSRobert Love  * We originated the sequence, so it should be found.
78442e9a92fSRobert Love  * We may or may not have originated the exchange.
78542e9a92fSRobert Love  * Does not hold the sequence for the caller.
78642e9a92fSRobert Love  */
78742e9a92fSRobert Love static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
78842e9a92fSRobert Love 					 struct fc_frame *fp)
78942e9a92fSRobert Love {
79042e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
79142e9a92fSRobert Love 	struct fc_exch *ep;
79242e9a92fSRobert Love 	struct fc_seq *sp = NULL;
79342e9a92fSRobert Love 	u32 f_ctl;
79442e9a92fSRobert Love 	u16 xid;
79542e9a92fSRobert Love 
79642e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
79742e9a92fSRobert Love 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
79842e9a92fSRobert Love 	xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
79942e9a92fSRobert Love 	ep = fc_exch_find(mp, xid);
80042e9a92fSRobert Love 	if (!ep)
80142e9a92fSRobert Love 		return NULL;
80242e9a92fSRobert Love 	if (ep->seq.id == fh->fh_seq_id) {
80342e9a92fSRobert Love 		/*
80442e9a92fSRobert Love 		 * Save the RX_ID if we didn't previously know it.
80542e9a92fSRobert Love 		 */
80642e9a92fSRobert Love 		sp = &ep->seq;
80742e9a92fSRobert Love 		if ((f_ctl & FC_FC_EX_CTX) != 0 &&
80842e9a92fSRobert Love 		    ep->rxid == FC_XID_UNKNOWN) {
80942e9a92fSRobert Love 			ep->rxid = ntohs(fh->fh_rx_id);
81042e9a92fSRobert Love 		}
81142e9a92fSRobert Love 	}
81242e9a92fSRobert Love 	fc_exch_release(ep);
81342e9a92fSRobert Love 	return sp;
81442e9a92fSRobert Love }
81542e9a92fSRobert Love 
81642e9a92fSRobert Love /*
81742e9a92fSRobert Love  * Set addresses for an exchange.
81842e9a92fSRobert Love  * Note this must be done before the first sequence of the exchange is sent.
81942e9a92fSRobert Love  */
82042e9a92fSRobert Love static void fc_exch_set_addr(struct fc_exch *ep,
82142e9a92fSRobert Love 			     u32 orig_id, u32 resp_id)
82242e9a92fSRobert Love {
82342e9a92fSRobert Love 	ep->oid = orig_id;
82442e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP) {
82542e9a92fSRobert Love 		ep->sid = resp_id;
82642e9a92fSRobert Love 		ep->did = orig_id;
82742e9a92fSRobert Love 	} else {
82842e9a92fSRobert Love 		ep->sid = orig_id;
82942e9a92fSRobert Love 		ep->did = resp_id;
83042e9a92fSRobert Love 	}
83142e9a92fSRobert Love }
83242e9a92fSRobert Love 
83342e9a92fSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
83442e9a92fSRobert Love {
83542e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
83642e9a92fSRobert Love 
83742e9a92fSRobert Love 	sp = fc_seq_alloc(ep, ep->seq_id++);
83842e9a92fSRobert Love 	FC_DEBUG_EXCH("exch %4x f_ctl %6x seq %2x\n",
83942e9a92fSRobert Love 		      ep->xid, ep->f_ctl, sp->id);
84042e9a92fSRobert Love 	return sp;
84142e9a92fSRobert Love }
84242e9a92fSRobert Love /*
84342e9a92fSRobert Love  * Allocate a new sequence on the same exchange as the supplied sequence.
84442e9a92fSRobert Love  * This will never return NULL.
84542e9a92fSRobert Love  */
84642e9a92fSRobert Love struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
84742e9a92fSRobert Love {
84842e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
84942e9a92fSRobert Love 
85042e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
85142e9a92fSRobert Love 	WARN_ON((ep->esb_stat & ESB_ST_COMPLETE) != 0);
85242e9a92fSRobert Love 	sp = fc_seq_start_next_locked(sp);
85342e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
85442e9a92fSRobert Love 
85542e9a92fSRobert Love 	return sp;
85642e9a92fSRobert Love }
85742e9a92fSRobert Love EXPORT_SYMBOL(fc_seq_start_next);
85842e9a92fSRobert Love 
85942e9a92fSRobert Love int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
86042e9a92fSRobert Love {
86142e9a92fSRobert Love 	struct fc_exch *ep;
86242e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
86342e9a92fSRobert Love 	int error;
86442e9a92fSRobert Love 	u32	f_ctl;
86542e9a92fSRobert Love 
86642e9a92fSRobert Love 	ep = fc_seq_exch(sp);
86742e9a92fSRobert Love 	WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
86842e9a92fSRobert Love 
86942e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
87042e9a92fSRobert Love 	fc_exch_setup_hdr(ep, fp, f_ctl);
87142e9a92fSRobert Love 
87242e9a92fSRobert Love 	/*
87342e9a92fSRobert Love 	 * update sequence count if this frame is carrying
87442e9a92fSRobert Love 	 * multiple FC frames when sequence offload is enabled
87542e9a92fSRobert Love 	 * by LLD.
87642e9a92fSRobert Love 	 */
87742e9a92fSRobert Love 	if (fr_max_payload(fp))
87842e9a92fSRobert Love 		sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
87942e9a92fSRobert Love 					fr_max_payload(fp));
88042e9a92fSRobert Love 	else
88142e9a92fSRobert Love 		sp->cnt++;
88242e9a92fSRobert Love 
88342e9a92fSRobert Love 	/*
88442e9a92fSRobert Love 	 * Send the frame.
88542e9a92fSRobert Love 	 */
88642e9a92fSRobert Love 	error = lp->tt.frame_send(lp, fp);
88742e9a92fSRobert Love 
88842e9a92fSRobert Love 	/*
88942e9a92fSRobert Love 	 * Update the exchange and sequence flags,
89042e9a92fSRobert Love 	 * assuming all frames for the sequence have been sent.
89142e9a92fSRobert Love 	 * We can only be called to send once for each sequence.
89242e9a92fSRobert Love 	 */
89342e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
89442e9a92fSRobert Love 	ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ;	/* not first seq */
89542e9a92fSRobert Love 	if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
89642e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
89742e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
89842e9a92fSRobert Love 	return error;
89942e9a92fSRobert Love }
90042e9a92fSRobert Love EXPORT_SYMBOL(fc_seq_send);
90142e9a92fSRobert Love 
90242e9a92fSRobert Love void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
90342e9a92fSRobert Love 			 struct fc_seq_els_data *els_data)
90442e9a92fSRobert Love {
90542e9a92fSRobert Love 	switch (els_cmd) {
90642e9a92fSRobert Love 	case ELS_LS_RJT:
90742e9a92fSRobert Love 		fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
90842e9a92fSRobert Love 		break;
90942e9a92fSRobert Love 	case ELS_LS_ACC:
91042e9a92fSRobert Love 		fc_seq_ls_acc(sp);
91142e9a92fSRobert Love 		break;
91242e9a92fSRobert Love 	case ELS_RRQ:
91342e9a92fSRobert Love 		fc_exch_els_rrq(sp, els_data->fp);
91442e9a92fSRobert Love 		break;
91542e9a92fSRobert Love 	case ELS_REC:
91642e9a92fSRobert Love 		fc_exch_els_rec(sp, els_data->fp);
91742e9a92fSRobert Love 		break;
91842e9a92fSRobert Love 	default:
91942e9a92fSRobert Love 		FC_DBG("Invalid ELS CMD:%x\n", els_cmd);
92042e9a92fSRobert Love 	}
92142e9a92fSRobert Love }
92242e9a92fSRobert Love EXPORT_SYMBOL(fc_seq_els_rsp_send);
92342e9a92fSRobert Love 
92442e9a92fSRobert Love /*
92542e9a92fSRobert Love  * Send a sequence, which is also the last sequence in the exchange.
92642e9a92fSRobert Love  */
92742e9a92fSRobert Love static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
92842e9a92fSRobert Love 			     enum fc_rctl rctl, enum fc_fh_type fh_type)
92942e9a92fSRobert Love {
93042e9a92fSRobert Love 	u32 f_ctl;
93142e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
93242e9a92fSRobert Love 
93342e9a92fSRobert Love 	f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
93442e9a92fSRobert Love 	f_ctl |= ep->f_ctl;
93542e9a92fSRobert Love 	fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
93642e9a92fSRobert Love 	fc_seq_send(ep->lp, sp, fp);
93742e9a92fSRobert Love }
93842e9a92fSRobert Love 
93942e9a92fSRobert Love /*
94042e9a92fSRobert Love  * Send ACK_1 (or equiv.) indicating we received something.
94142e9a92fSRobert Love  * The frame we're acking is supplied.
94242e9a92fSRobert Love  */
94342e9a92fSRobert Love static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
94442e9a92fSRobert Love {
94542e9a92fSRobert Love 	struct fc_frame *fp;
94642e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
94742e9a92fSRobert Love 	struct fc_frame_header *fh;
94842e9a92fSRobert Love 	struct fc_exch *ep = fc_seq_exch(sp);
94942e9a92fSRobert Love 	struct fc_lport *lp = ep->lp;
95042e9a92fSRobert Love 	unsigned int f_ctl;
95142e9a92fSRobert Love 
95242e9a92fSRobert Love 	/*
95342e9a92fSRobert Love 	 * Don't send ACKs for class 3.
95442e9a92fSRobert Love 	 */
95542e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(rx_fp))) {
95642e9a92fSRobert Love 		fp = fc_frame_alloc(lp, 0);
95742e9a92fSRobert Love 		if (!fp)
95842e9a92fSRobert Love 			return;
95942e9a92fSRobert Love 
96042e9a92fSRobert Love 		fh = fc_frame_header_get(fp);
96142e9a92fSRobert Love 		fh->fh_r_ctl = FC_RCTL_ACK_1;
96242e9a92fSRobert Love 		fh->fh_type = FC_TYPE_BLS;
96342e9a92fSRobert Love 
96442e9a92fSRobert Love 		/*
96542e9a92fSRobert Love 		 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
96642e9a92fSRobert Love 		 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
96742e9a92fSRobert Love 		 * Bits 9-8 are meaningful (retransmitted or unidirectional).
96842e9a92fSRobert Love 		 * Last ACK uses bits 7-6 (continue sequence),
96942e9a92fSRobert Love 		 * bits 5-4 are meaningful (what kind of ACK to use).
97042e9a92fSRobert Love 		 */
97142e9a92fSRobert Love 		rx_fh = fc_frame_header_get(rx_fp);
97242e9a92fSRobert Love 		f_ctl = ntoh24(rx_fh->fh_f_ctl);
97342e9a92fSRobert Love 		f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
97442e9a92fSRobert Love 			FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
97542e9a92fSRobert Love 			FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
97642e9a92fSRobert Love 			FC_FC_RETX_SEQ | FC_FC_UNI_TX;
97742e9a92fSRobert Love 		f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
97842e9a92fSRobert Love 		hton24(fh->fh_f_ctl, f_ctl);
97942e9a92fSRobert Love 
98042e9a92fSRobert Love 		fc_exch_setup_hdr(ep, fp, f_ctl);
98142e9a92fSRobert Love 		fh->fh_seq_id = rx_fh->fh_seq_id;
98242e9a92fSRobert Love 		fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
98342e9a92fSRobert Love 		fh->fh_parm_offset = htonl(1);	/* ack single frame */
98442e9a92fSRobert Love 
98542e9a92fSRobert Love 		fr_sof(fp) = fr_sof(rx_fp);
98642e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
98742e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_T;
98842e9a92fSRobert Love 		else
98942e9a92fSRobert Love 			fr_eof(fp) = FC_EOF_N;
99042e9a92fSRobert Love 
99142e9a92fSRobert Love 		(void) lp->tt.frame_send(lp, fp);
99242e9a92fSRobert Love 	}
99342e9a92fSRobert Love }
99442e9a92fSRobert Love 
99542e9a92fSRobert Love /*
99642e9a92fSRobert Love  * Send BLS Reject.
99742e9a92fSRobert Love  * This is for rejecting BA_ABTS only.
99842e9a92fSRobert Love  */
99942e9a92fSRobert Love static void
100042e9a92fSRobert Love fc_exch_send_ba_rjt(struct fc_frame *rx_fp, enum fc_ba_rjt_reason reason,
100142e9a92fSRobert Love 		    enum fc_ba_rjt_explan explan)
100242e9a92fSRobert Love {
100342e9a92fSRobert Love 	struct fc_frame *fp;
100442e9a92fSRobert Love 	struct fc_frame_header *rx_fh;
100542e9a92fSRobert Love 	struct fc_frame_header *fh;
100642e9a92fSRobert Love 	struct fc_ba_rjt *rp;
100742e9a92fSRobert Love 	struct fc_lport *lp;
100842e9a92fSRobert Love 	unsigned int f_ctl;
100942e9a92fSRobert Love 
101042e9a92fSRobert Love 	lp = fr_dev(rx_fp);
101142e9a92fSRobert Love 	fp = fc_frame_alloc(lp, sizeof(*rp));
101242e9a92fSRobert Love 	if (!fp)
101342e9a92fSRobert Love 		return;
101442e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
101542e9a92fSRobert Love 	rx_fh = fc_frame_header_get(rx_fp);
101642e9a92fSRobert Love 
101742e9a92fSRobert Love 	memset(fh, 0, sizeof(*fh) + sizeof(*rp));
101842e9a92fSRobert Love 
101942e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
102042e9a92fSRobert Love 	rp->br_reason = reason;
102142e9a92fSRobert Love 	rp->br_explan = explan;
102242e9a92fSRobert Love 
102342e9a92fSRobert Love 	/*
102442e9a92fSRobert Love 	 * seq_id, cs_ctl, df_ctl and param/offset are zero.
102542e9a92fSRobert Love 	 */
102642e9a92fSRobert Love 	memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
102742e9a92fSRobert Love 	memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
102842e9a92fSRobert Love 	fh->fh_ox_id = rx_fh->fh_rx_id;
102942e9a92fSRobert Love 	fh->fh_rx_id = rx_fh->fh_ox_id;
103042e9a92fSRobert Love 	fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
103142e9a92fSRobert Love 	fh->fh_r_ctl = FC_RCTL_BA_RJT;
103242e9a92fSRobert Love 	fh->fh_type = FC_TYPE_BLS;
103342e9a92fSRobert Love 
103442e9a92fSRobert Love 	/*
103542e9a92fSRobert Love 	 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
103642e9a92fSRobert Love 	 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
103742e9a92fSRobert Love 	 * Bits 9-8 are meaningful (retransmitted or unidirectional).
103842e9a92fSRobert Love 	 * Last ACK uses bits 7-6 (continue sequence),
103942e9a92fSRobert Love 	 * bits 5-4 are meaningful (what kind of ACK to use).
104042e9a92fSRobert Love 	 * Always set LAST_SEQ, END_SEQ.
104142e9a92fSRobert Love 	 */
104242e9a92fSRobert Love 	f_ctl = ntoh24(rx_fh->fh_f_ctl);
104342e9a92fSRobert Love 	f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
104442e9a92fSRobert Love 		FC_FC_END_CONN | FC_FC_SEQ_INIT |
104542e9a92fSRobert Love 		FC_FC_RETX_SEQ | FC_FC_UNI_TX;
104642e9a92fSRobert Love 	f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
104742e9a92fSRobert Love 	f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
104842e9a92fSRobert Love 	f_ctl &= ~FC_FC_FIRST_SEQ;
104942e9a92fSRobert Love 	hton24(fh->fh_f_ctl, f_ctl);
105042e9a92fSRobert Love 
105142e9a92fSRobert Love 	fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
105242e9a92fSRobert Love 	fr_eof(fp) = FC_EOF_T;
105342e9a92fSRobert Love 	if (fc_sof_needs_ack(fr_sof(fp)))
105442e9a92fSRobert Love 		fr_eof(fp) = FC_EOF_N;
105542e9a92fSRobert Love 
105642e9a92fSRobert Love 	(void) lp->tt.frame_send(lp, fp);
105742e9a92fSRobert Love }
105842e9a92fSRobert Love 
105942e9a92fSRobert Love /*
106042e9a92fSRobert Love  * Handle an incoming ABTS.  This would be for target mode usually,
106142e9a92fSRobert Love  * but could be due to lost FCP transfer ready, confirm or RRQ.
106242e9a92fSRobert Love  * We always handle this as an exchange abort, ignoring the parameter.
106342e9a92fSRobert Love  */
106442e9a92fSRobert Love static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
106542e9a92fSRobert Love {
106642e9a92fSRobert Love 	struct fc_frame *fp;
106742e9a92fSRobert Love 	struct fc_ba_acc *ap;
106842e9a92fSRobert Love 	struct fc_frame_header *fh;
106942e9a92fSRobert Love 	struct fc_seq *sp;
107042e9a92fSRobert Love 
107142e9a92fSRobert Love 	if (!ep)
107242e9a92fSRobert Love 		goto reject;
107342e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
107442e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
107542e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
107642e9a92fSRobert Love 		goto reject;
107742e9a92fSRobert Love 	}
107842e9a92fSRobert Love 	if (!(ep->esb_stat & ESB_ST_REC_QUAL))
107942e9a92fSRobert Love 		fc_exch_hold(ep);		/* hold for REC_QUAL */
108042e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
108142e9a92fSRobert Love 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
108242e9a92fSRobert Love 
108342e9a92fSRobert Love 	fp = fc_frame_alloc(ep->lp, sizeof(*ap));
108442e9a92fSRobert Love 	if (!fp) {
108542e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
108642e9a92fSRobert Love 		goto free;
108742e9a92fSRobert Love 	}
108842e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
108942e9a92fSRobert Love 	ap = fc_frame_payload_get(fp, sizeof(*ap));
109042e9a92fSRobert Love 	memset(ap, 0, sizeof(*ap));
109142e9a92fSRobert Love 	sp = &ep->seq;
109242e9a92fSRobert Love 	ap->ba_high_seq_cnt = htons(0xffff);
109342e9a92fSRobert Love 	if (sp->ssb_stat & SSB_ST_RESP) {
109442e9a92fSRobert Love 		ap->ba_seq_id = sp->id;
109542e9a92fSRobert Love 		ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
109642e9a92fSRobert Love 		ap->ba_high_seq_cnt = fh->fh_seq_cnt;
109742e9a92fSRobert Love 		ap->ba_low_seq_cnt = htons(sp->cnt);
109842e9a92fSRobert Love 	}
109942e9a92fSRobert Love 	sp = fc_seq_start_next(sp);
110042e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
110142e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
110242e9a92fSRobert Love 	fc_frame_free(rx_fp);
110342e9a92fSRobert Love 	return;
110442e9a92fSRobert Love 
110542e9a92fSRobert Love reject:
110642e9a92fSRobert Love 	fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
110742e9a92fSRobert Love free:
110842e9a92fSRobert Love 	fc_frame_free(rx_fp);
110942e9a92fSRobert Love }
111042e9a92fSRobert Love 
111142e9a92fSRobert Love /*
111242e9a92fSRobert Love  * Handle receive where the other end is originating the sequence.
111342e9a92fSRobert Love  */
111442e9a92fSRobert Love static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
111542e9a92fSRobert Love 			     struct fc_frame *fp)
111642e9a92fSRobert Love {
111742e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
111842e9a92fSRobert Love 	struct fc_seq *sp = NULL;
111942e9a92fSRobert Love 	struct fc_exch *ep = NULL;
112042e9a92fSRobert Love 	enum fc_sof sof;
112142e9a92fSRobert Love 	enum fc_eof eof;
112242e9a92fSRobert Love 	u32 f_ctl;
112342e9a92fSRobert Love 	enum fc_pf_rjt_reason reject;
112442e9a92fSRobert Love 
112542e9a92fSRobert Love 	fr_seq(fp) = NULL;
112642e9a92fSRobert Love 	reject = fc_seq_lookup_recip(mp, fp);
112742e9a92fSRobert Love 	if (reject == FC_RJT_NONE) {
112842e9a92fSRobert Love 		sp = fr_seq(fp);	/* sequence will be held */
112942e9a92fSRobert Love 		ep = fc_seq_exch(sp);
113042e9a92fSRobert Love 		sof = fr_sof(fp);
113142e9a92fSRobert Love 		eof = fr_eof(fp);
113242e9a92fSRobert Love 		f_ctl = ntoh24(fh->fh_f_ctl);
113342e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
113442e9a92fSRobert Love 
113542e9a92fSRobert Love 		/*
113642e9a92fSRobert Love 		 * Call the receive function.
113742e9a92fSRobert Love 		 *
113842e9a92fSRobert Love 		 * The receive function may allocate a new sequence
113942e9a92fSRobert Love 		 * over the old one, so we shouldn't change the
114042e9a92fSRobert Love 		 * sequence after this.
114142e9a92fSRobert Love 		 *
114242e9a92fSRobert Love 		 * The frame will be freed by the receive function.
114342e9a92fSRobert Love 		 * If new exch resp handler is valid then call that
114442e9a92fSRobert Love 		 * first.
114542e9a92fSRobert Love 		 */
114642e9a92fSRobert Love 		if (ep->resp)
114742e9a92fSRobert Love 			ep->resp(sp, fp, ep->arg);
114842e9a92fSRobert Love 		else
114942e9a92fSRobert Love 			lp->tt.lport_recv(lp, sp, fp);
115042e9a92fSRobert Love 		fc_exch_release(ep);	/* release from lookup */
115142e9a92fSRobert Love 	} else {
115242e9a92fSRobert Love 		FC_DEBUG_EXCH("exch/seq lookup failed: reject %x\n", reject);
115342e9a92fSRobert Love 		fc_frame_free(fp);
115442e9a92fSRobert Love 	}
115542e9a92fSRobert Love }
115642e9a92fSRobert Love 
115742e9a92fSRobert Love /*
115842e9a92fSRobert Love  * Handle receive where the other end is originating the sequence in
115942e9a92fSRobert Love  * response to our exchange.
116042e9a92fSRobert Love  */
116142e9a92fSRobert Love static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
116242e9a92fSRobert Love {
116342e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
116442e9a92fSRobert Love 	struct fc_seq *sp;
116542e9a92fSRobert Love 	struct fc_exch *ep;
116642e9a92fSRobert Love 	enum fc_sof sof;
116742e9a92fSRobert Love 	u32 f_ctl;
116842e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
116942e9a92fSRobert Love 	void *ex_resp_arg;
117042e9a92fSRobert Love 	int rc;
117142e9a92fSRobert Love 
117242e9a92fSRobert Love 	ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
117342e9a92fSRobert Love 	if (!ep) {
117442e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
117542e9a92fSRobert Love 		goto out;
117642e9a92fSRobert Love 	}
117742e9a92fSRobert Love 	if (ep->rxid == FC_XID_UNKNOWN)
117842e9a92fSRobert Love 		ep->rxid = ntohs(fh->fh_rx_id);
117942e9a92fSRobert Love 	if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
118042e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
118142e9a92fSRobert Love 		goto rel;
118242e9a92fSRobert Love 	}
118342e9a92fSRobert Love 	if (ep->did != ntoh24(fh->fh_s_id) &&
118442e9a92fSRobert Love 	    ep->did != FC_FID_FLOGI) {
118542e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
118642e9a92fSRobert Love 		goto rel;
118742e9a92fSRobert Love 	}
118842e9a92fSRobert Love 	sof = fr_sof(fp);
118942e9a92fSRobert Love 	if (fc_sof_is_init(sof)) {
119042e9a92fSRobert Love 		sp = fc_seq_start_next(&ep->seq);
119142e9a92fSRobert Love 		sp->id = fh->fh_seq_id;
119242e9a92fSRobert Love 		sp->ssb_stat |= SSB_ST_RESP;
119342e9a92fSRobert Love 	} else {
119442e9a92fSRobert Love 		sp = &ep->seq;
119542e9a92fSRobert Love 		if (sp->id != fh->fh_seq_id) {
119642e9a92fSRobert Love 			atomic_inc(&mp->stats.seq_not_found);
119742e9a92fSRobert Love 			goto rel;
119842e9a92fSRobert Love 		}
119942e9a92fSRobert Love 	}
120042e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
120142e9a92fSRobert Love 	fr_seq(fp) = sp;
120242e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_INIT)
120342e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
120442e9a92fSRobert Love 
120542e9a92fSRobert Love 	if (fc_sof_needs_ack(sof))
120642e9a92fSRobert Love 		fc_seq_send_ack(sp, fp);
120742e9a92fSRobert Love 	resp = ep->resp;
120842e9a92fSRobert Love 	ex_resp_arg = ep->arg;
120942e9a92fSRobert Love 
121042e9a92fSRobert Love 	if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
121142e9a92fSRobert Love 	    (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
121242e9a92fSRobert Love 	    (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
121342e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
121442e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
121542e9a92fSRobert Love 		WARN_ON(fc_seq_exch(sp) != ep);
121642e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
121742e9a92fSRobert Love 		if (!rc)
121842e9a92fSRobert Love 			fc_exch_mgr_delete_ep(ep);
121942e9a92fSRobert Love 	}
122042e9a92fSRobert Love 
122142e9a92fSRobert Love 	/*
122242e9a92fSRobert Love 	 * Call the receive function.
122342e9a92fSRobert Love 	 * The sequence is held (has a refcnt) for us,
122442e9a92fSRobert Love 	 * but not for the receive function.
122542e9a92fSRobert Love 	 *
122642e9a92fSRobert Love 	 * The receive function may allocate a new sequence
122742e9a92fSRobert Love 	 * over the old one, so we shouldn't change the
122842e9a92fSRobert Love 	 * sequence after this.
122942e9a92fSRobert Love 	 *
123042e9a92fSRobert Love 	 * The frame will be freed by the receive function.
123142e9a92fSRobert Love 	 * If new exch resp handler is valid then call that
123242e9a92fSRobert Love 	 * first.
123342e9a92fSRobert Love 	 */
123442e9a92fSRobert Love 	if (resp)
123542e9a92fSRobert Love 		resp(sp, fp, ex_resp_arg);
123642e9a92fSRobert Love 	else
123742e9a92fSRobert Love 		fc_frame_free(fp);
123842e9a92fSRobert Love 	fc_exch_release(ep);
123942e9a92fSRobert Love 	return;
124042e9a92fSRobert Love rel:
124142e9a92fSRobert Love 	fc_exch_release(ep);
124242e9a92fSRobert Love out:
124342e9a92fSRobert Love 	fc_frame_free(fp);
124442e9a92fSRobert Love }
124542e9a92fSRobert Love 
124642e9a92fSRobert Love /*
124742e9a92fSRobert Love  * Handle receive for a sequence where other end is responding to our sequence.
124842e9a92fSRobert Love  */
124942e9a92fSRobert Love static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
125042e9a92fSRobert Love {
125142e9a92fSRobert Love 	struct fc_seq *sp;
125242e9a92fSRobert Love 
125342e9a92fSRobert Love 	sp = fc_seq_lookup_orig(mp, fp);	/* doesn't hold sequence */
125442e9a92fSRobert Love 	if (!sp) {
125542e9a92fSRobert Love 		atomic_inc(&mp->stats.xid_not_found);
125642e9a92fSRobert Love 		FC_DEBUG_EXCH("seq lookup failed\n");
125742e9a92fSRobert Love 	} else {
125842e9a92fSRobert Love 		atomic_inc(&mp->stats.non_bls_resp);
125942e9a92fSRobert Love 		FC_DEBUG_EXCH("non-BLS response to sequence");
126042e9a92fSRobert Love 	}
126142e9a92fSRobert Love 	fc_frame_free(fp);
126242e9a92fSRobert Love }
126342e9a92fSRobert Love 
126442e9a92fSRobert Love /*
126542e9a92fSRobert Love  * Handle the response to an ABTS for exchange or sequence.
126642e9a92fSRobert Love  * This can be BA_ACC or BA_RJT.
126742e9a92fSRobert Love  */
126842e9a92fSRobert Love static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
126942e9a92fSRobert Love {
127042e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
127142e9a92fSRobert Love 	void *ex_resp_arg;
127242e9a92fSRobert Love 	struct fc_frame_header *fh;
127342e9a92fSRobert Love 	struct fc_ba_acc *ap;
127442e9a92fSRobert Love 	struct fc_seq *sp;
127542e9a92fSRobert Love 	u16 low;
127642e9a92fSRobert Love 	u16 high;
127742e9a92fSRobert Love 	int rc = 1, has_rec = 0;
127842e9a92fSRobert Love 
127942e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
128042e9a92fSRobert Love 	FC_DEBUG_EXCH("exch: BLS rctl %x - %s\n",
128142e9a92fSRobert Love 		      fh->fh_r_ctl, fc_exch_rctl_name(fh->fh_r_ctl));
128242e9a92fSRobert Love 
128342e9a92fSRobert Love 	if (cancel_delayed_work_sync(&ep->timeout_work))
128442e9a92fSRobert Love 		fc_exch_release(ep);	/* release from pending timer hold */
128542e9a92fSRobert Love 
128642e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
128742e9a92fSRobert Love 	switch (fh->fh_r_ctl) {
128842e9a92fSRobert Love 	case FC_RCTL_BA_ACC:
128942e9a92fSRobert Love 		ap = fc_frame_payload_get(fp, sizeof(*ap));
129042e9a92fSRobert Love 		if (!ap)
129142e9a92fSRobert Love 			break;
129242e9a92fSRobert Love 
129342e9a92fSRobert Love 		/*
129442e9a92fSRobert Love 		 * Decide whether to establish a Recovery Qualifier.
129542e9a92fSRobert Love 		 * We do this if there is a non-empty SEQ_CNT range and
129642e9a92fSRobert Love 		 * SEQ_ID is the same as the one we aborted.
129742e9a92fSRobert Love 		 */
129842e9a92fSRobert Love 		low = ntohs(ap->ba_low_seq_cnt);
129942e9a92fSRobert Love 		high = ntohs(ap->ba_high_seq_cnt);
130042e9a92fSRobert Love 		if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
130142e9a92fSRobert Love 		    (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
130242e9a92fSRobert Love 		     ap->ba_seq_id == ep->seq_id) && low != high) {
130342e9a92fSRobert Love 			ep->esb_stat |= ESB_ST_REC_QUAL;
130442e9a92fSRobert Love 			fc_exch_hold(ep);  /* hold for recovery qualifier */
130542e9a92fSRobert Love 			has_rec = 1;
130642e9a92fSRobert Love 		}
130742e9a92fSRobert Love 		break;
130842e9a92fSRobert Love 	case FC_RCTL_BA_RJT:
130942e9a92fSRobert Love 		break;
131042e9a92fSRobert Love 	default:
131142e9a92fSRobert Love 		break;
131242e9a92fSRobert Love 	}
131342e9a92fSRobert Love 
131442e9a92fSRobert Love 	resp = ep->resp;
131542e9a92fSRobert Love 	ex_resp_arg = ep->arg;
131642e9a92fSRobert Love 
131742e9a92fSRobert Love 	/* do we need to do some other checks here. Can we reuse more of
131842e9a92fSRobert Love 	 * fc_exch_recv_seq_resp
131942e9a92fSRobert Love 	 */
132042e9a92fSRobert Love 	sp = &ep->seq;
132142e9a92fSRobert Love 	/*
132242e9a92fSRobert Love 	 * do we want to check END_SEQ as well as LAST_SEQ here?
132342e9a92fSRobert Love 	 */
132442e9a92fSRobert Love 	if (ep->fh_type != FC_TYPE_FCP &&
132542e9a92fSRobert Love 	    ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
132642e9a92fSRobert Love 		rc = fc_exch_done_locked(ep);
132742e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
132842e9a92fSRobert Love 	if (!rc)
132942e9a92fSRobert Love 		fc_exch_mgr_delete_ep(ep);
133042e9a92fSRobert Love 
133142e9a92fSRobert Love 	if (resp)
133242e9a92fSRobert Love 		resp(sp, fp, ex_resp_arg);
133342e9a92fSRobert Love 	else
133442e9a92fSRobert Love 		fc_frame_free(fp);
133542e9a92fSRobert Love 
133642e9a92fSRobert Love 	if (has_rec)
133742e9a92fSRobert Love 		fc_exch_timer_set(ep, ep->r_a_tov);
133842e9a92fSRobert Love 
133942e9a92fSRobert Love }
134042e9a92fSRobert Love 
134142e9a92fSRobert Love /*
134242e9a92fSRobert Love  * Receive BLS sequence.
134342e9a92fSRobert Love  * This is always a sequence initiated by the remote side.
134442e9a92fSRobert Love  * We may be either the originator or recipient of the exchange.
134542e9a92fSRobert Love  */
134642e9a92fSRobert Love static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
134742e9a92fSRobert Love {
134842e9a92fSRobert Love 	struct fc_frame_header *fh;
134942e9a92fSRobert Love 	struct fc_exch *ep;
135042e9a92fSRobert Love 	u32 f_ctl;
135142e9a92fSRobert Love 
135242e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
135342e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
135442e9a92fSRobert Love 	fr_seq(fp) = NULL;
135542e9a92fSRobert Love 
135642e9a92fSRobert Love 	ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
135742e9a92fSRobert Love 			  ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
135842e9a92fSRobert Love 	if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
135942e9a92fSRobert Love 		spin_lock_bh(&ep->ex_lock);
136042e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_SEQ_INIT;
136142e9a92fSRobert Love 		spin_unlock_bh(&ep->ex_lock);
136242e9a92fSRobert Love 	}
136342e9a92fSRobert Love 	if (f_ctl & FC_FC_SEQ_CTX) {
136442e9a92fSRobert Love 		/*
136542e9a92fSRobert Love 		 * A response to a sequence we initiated.
136642e9a92fSRobert Love 		 * This should only be ACKs for class 2 or F.
136742e9a92fSRobert Love 		 */
136842e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
136942e9a92fSRobert Love 		case FC_RCTL_ACK_1:
137042e9a92fSRobert Love 		case FC_RCTL_ACK_0:
137142e9a92fSRobert Love 			break;
137242e9a92fSRobert Love 		default:
137342e9a92fSRobert Love 			FC_DEBUG_EXCH("BLS rctl %x - %s received",
137442e9a92fSRobert Love 				      fh->fh_r_ctl,
137542e9a92fSRobert Love 				      fc_exch_rctl_name(fh->fh_r_ctl));
137642e9a92fSRobert Love 			break;
137742e9a92fSRobert Love 		}
137842e9a92fSRobert Love 		fc_frame_free(fp);
137942e9a92fSRobert Love 	} else {
138042e9a92fSRobert Love 		switch (fh->fh_r_ctl) {
138142e9a92fSRobert Love 		case FC_RCTL_BA_RJT:
138242e9a92fSRobert Love 		case FC_RCTL_BA_ACC:
138342e9a92fSRobert Love 			if (ep)
138442e9a92fSRobert Love 				fc_exch_abts_resp(ep, fp);
138542e9a92fSRobert Love 			else
138642e9a92fSRobert Love 				fc_frame_free(fp);
138742e9a92fSRobert Love 			break;
138842e9a92fSRobert Love 		case FC_RCTL_BA_ABTS:
138942e9a92fSRobert Love 			fc_exch_recv_abts(ep, fp);
139042e9a92fSRobert Love 			break;
139142e9a92fSRobert Love 		default:			/* ignore junk */
139242e9a92fSRobert Love 			fc_frame_free(fp);
139342e9a92fSRobert Love 			break;
139442e9a92fSRobert Love 		}
139542e9a92fSRobert Love 	}
139642e9a92fSRobert Love 	if (ep)
139742e9a92fSRobert Love 		fc_exch_release(ep);	/* release hold taken by fc_exch_find */
139842e9a92fSRobert Love }
139942e9a92fSRobert Love 
140042e9a92fSRobert Love /*
140142e9a92fSRobert Love  * Accept sequence with LS_ACC.
140242e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
140342e9a92fSRobert Love  * originator will repeat the sequence.
140442e9a92fSRobert Love  */
140542e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *req_sp)
140642e9a92fSRobert Love {
140742e9a92fSRobert Love 	struct fc_seq *sp;
140842e9a92fSRobert Love 	struct fc_els_ls_acc *acc;
140942e9a92fSRobert Love 	struct fc_frame *fp;
141042e9a92fSRobert Love 
141142e9a92fSRobert Love 	sp = fc_seq_start_next(req_sp);
141242e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
141342e9a92fSRobert Love 	if (fp) {
141442e9a92fSRobert Love 		acc = fc_frame_payload_get(fp, sizeof(*acc));
141542e9a92fSRobert Love 		memset(acc, 0, sizeof(*acc));
141642e9a92fSRobert Love 		acc->la_cmd = ELS_LS_ACC;
141742e9a92fSRobert Love 		fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
141842e9a92fSRobert Love 	}
141942e9a92fSRobert Love }
142042e9a92fSRobert Love 
142142e9a92fSRobert Love /*
142242e9a92fSRobert Love  * Reject sequence with ELS LS_RJT.
142342e9a92fSRobert Love  * If this fails due to allocation or transmit congestion, assume the
142442e9a92fSRobert Love  * originator will repeat the sequence.
142542e9a92fSRobert Love  */
142642e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
142742e9a92fSRobert Love 			  enum fc_els_rjt_explan explan)
142842e9a92fSRobert Love {
142942e9a92fSRobert Love 	struct fc_seq *sp;
143042e9a92fSRobert Love 	struct fc_els_ls_rjt *rjt;
143142e9a92fSRobert Love 	struct fc_frame *fp;
143242e9a92fSRobert Love 
143342e9a92fSRobert Love 	sp = fc_seq_start_next(req_sp);
143442e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
143542e9a92fSRobert Love 	if (fp) {
143642e9a92fSRobert Love 		rjt = fc_frame_payload_get(fp, sizeof(*rjt));
143742e9a92fSRobert Love 		memset(rjt, 0, sizeof(*rjt));
143842e9a92fSRobert Love 		rjt->er_cmd = ELS_LS_RJT;
143942e9a92fSRobert Love 		rjt->er_reason = reason;
144042e9a92fSRobert Love 		rjt->er_explan = explan;
144142e9a92fSRobert Love 		fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
144242e9a92fSRobert Love 	}
144342e9a92fSRobert Love }
144442e9a92fSRobert Love 
144542e9a92fSRobert Love static void fc_exch_reset(struct fc_exch *ep)
144642e9a92fSRobert Love {
144742e9a92fSRobert Love 	struct fc_seq *sp;
144842e9a92fSRobert Love 	void (*resp)(struct fc_seq *, struct fc_frame *, void *);
144942e9a92fSRobert Love 	void *arg;
145042e9a92fSRobert Love 	int rc = 1;
145142e9a92fSRobert Love 
145242e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
145342e9a92fSRobert Love 	ep->state |= FC_EX_RST_CLEANUP;
145442e9a92fSRobert Love 	/*
145542e9a92fSRobert Love 	 * we really want to call del_timer_sync, but cannot due
145642e9a92fSRobert Love 	 * to the lport calling with the lport lock held (some resp
145742e9a92fSRobert Love 	 * functions can also grab the lport lock which could cause
145842e9a92fSRobert Love 	 * a deadlock).
145942e9a92fSRobert Love 	 */
146042e9a92fSRobert Love 	if (cancel_delayed_work(&ep->timeout_work))
146142e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for timer */
146242e9a92fSRobert Love 	resp = ep->resp;
146342e9a92fSRobert Love 	ep->resp = NULL;
146442e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL)
146542e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec_qual */
146642e9a92fSRobert Love 	ep->esb_stat &= ~ESB_ST_REC_QUAL;
146742e9a92fSRobert Love 	arg = ep->arg;
146842e9a92fSRobert Love 	sp = &ep->seq;
146942e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
147042e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
147142e9a92fSRobert Love 	if (!rc)
147242e9a92fSRobert Love 		fc_exch_mgr_delete_ep(ep);
147342e9a92fSRobert Love 
147442e9a92fSRobert Love 	if (resp)
147542e9a92fSRobert Love 		resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
147642e9a92fSRobert Love }
147742e9a92fSRobert Love 
147842e9a92fSRobert Love /*
147942e9a92fSRobert Love  * Reset an exchange manager, releasing all sequences and exchanges.
148042e9a92fSRobert Love  * If sid is non-zero, reset only exchanges we source from that FID.
148142e9a92fSRobert Love  * If did is non-zero, reset only exchanges destined to that FID.
148242e9a92fSRobert Love  */
14831f6ff364SAbhijeet Joglekar void fc_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
148442e9a92fSRobert Love {
148542e9a92fSRobert Love 	struct fc_exch *ep;
148642e9a92fSRobert Love 	struct fc_exch *next;
14871f6ff364SAbhijeet Joglekar 	struct fc_exch_mgr *mp = lp->emp;
148842e9a92fSRobert Love 
148942e9a92fSRobert Love 	spin_lock_bh(&mp->em_lock);
149042e9a92fSRobert Love restart:
149142e9a92fSRobert Love 	list_for_each_entry_safe(ep, next, &mp->ex_list, ex_list) {
149242e9a92fSRobert Love 		if ((sid == 0 || sid == ep->sid) &&
149342e9a92fSRobert Love 		    (did == 0 || did == ep->did)) {
149442e9a92fSRobert Love 			fc_exch_hold(ep);
149542e9a92fSRobert Love 			spin_unlock_bh(&mp->em_lock);
149642e9a92fSRobert Love 
149742e9a92fSRobert Love 			fc_exch_reset(ep);
149842e9a92fSRobert Love 
149942e9a92fSRobert Love 			fc_exch_release(ep);
150042e9a92fSRobert Love 			spin_lock_bh(&mp->em_lock);
150142e9a92fSRobert Love 
150242e9a92fSRobert Love 			/*
150342e9a92fSRobert Love 			 * must restart loop incase while lock was down
150442e9a92fSRobert Love 			 * multiple eps were released.
150542e9a92fSRobert Love 			 */
150642e9a92fSRobert Love 			goto restart;
150742e9a92fSRobert Love 		}
150842e9a92fSRobert Love 	}
150942e9a92fSRobert Love 	spin_unlock_bh(&mp->em_lock);
151042e9a92fSRobert Love }
151142e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_reset);
151242e9a92fSRobert Love 
151342e9a92fSRobert Love /*
151442e9a92fSRobert Love  * Handle incoming ELS REC - Read Exchange Concise.
151542e9a92fSRobert Love  * Note that the requesting port may be different than the S_ID in the request.
151642e9a92fSRobert Love  */
151742e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
151842e9a92fSRobert Love {
151942e9a92fSRobert Love 	struct fc_frame *fp;
152042e9a92fSRobert Love 	struct fc_exch *ep;
152142e9a92fSRobert Love 	struct fc_exch_mgr *em;
152242e9a92fSRobert Love 	struct fc_els_rec *rp;
152342e9a92fSRobert Love 	struct fc_els_rec_acc *acc;
152442e9a92fSRobert Love 	enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
152542e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
152642e9a92fSRobert Love 	u32 sid;
152742e9a92fSRobert Love 	u16 rxid;
152842e9a92fSRobert Love 	u16 oxid;
152942e9a92fSRobert Love 
153042e9a92fSRobert Love 	rp = fc_frame_payload_get(rfp, sizeof(*rp));
153142e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
153242e9a92fSRobert Love 	if (!rp)
153342e9a92fSRobert Love 		goto reject;
153442e9a92fSRobert Love 	sid = ntoh24(rp->rec_s_id);
153542e9a92fSRobert Love 	rxid = ntohs(rp->rec_rx_id);
153642e9a92fSRobert Love 	oxid = ntohs(rp->rec_ox_id);
153742e9a92fSRobert Love 
153842e9a92fSRobert Love 	/*
153942e9a92fSRobert Love 	 * Currently it's hard to find the local S_ID from the exchange
154042e9a92fSRobert Love 	 * manager.  This will eventually be fixed, but for now it's easier
154142e9a92fSRobert Love 	 * to lookup the subject exchange twice, once as if we were
154242e9a92fSRobert Love 	 * the initiator, and then again if we weren't.
154342e9a92fSRobert Love 	 */
154442e9a92fSRobert Love 	em = fc_seq_exch(sp)->em;
154542e9a92fSRobert Love 	ep = fc_exch_find(em, oxid);
154642e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
154742e9a92fSRobert Love 	if (ep && ep->oid == sid) {
154842e9a92fSRobert Love 		if (ep->rxid != FC_XID_UNKNOWN &&
154942e9a92fSRobert Love 		    rxid != FC_XID_UNKNOWN &&
155042e9a92fSRobert Love 		    ep->rxid != rxid)
155142e9a92fSRobert Love 			goto rel;
155242e9a92fSRobert Love 	} else {
155342e9a92fSRobert Love 		if (ep)
155442e9a92fSRobert Love 			fc_exch_release(ep);
155542e9a92fSRobert Love 		ep = NULL;
155642e9a92fSRobert Love 		if (rxid != FC_XID_UNKNOWN)
155742e9a92fSRobert Love 			ep = fc_exch_find(em, rxid);
155842e9a92fSRobert Love 		if (!ep)
155942e9a92fSRobert Love 			goto reject;
156042e9a92fSRobert Love 	}
156142e9a92fSRobert Love 
156242e9a92fSRobert Love 	fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
156342e9a92fSRobert Love 	if (!fp) {
156442e9a92fSRobert Love 		fc_exch_done(sp);
156542e9a92fSRobert Love 		goto out;
156642e9a92fSRobert Love 	}
156742e9a92fSRobert Love 	sp = fc_seq_start_next(sp);
156842e9a92fSRobert Love 	acc = fc_frame_payload_get(fp, sizeof(*acc));
156942e9a92fSRobert Love 	memset(acc, 0, sizeof(*acc));
157042e9a92fSRobert Love 	acc->reca_cmd = ELS_LS_ACC;
157142e9a92fSRobert Love 	acc->reca_ox_id = rp->rec_ox_id;
157242e9a92fSRobert Love 	memcpy(acc->reca_ofid, rp->rec_s_id, 3);
157342e9a92fSRobert Love 	acc->reca_rx_id = htons(ep->rxid);
157442e9a92fSRobert Love 	if (ep->sid == ep->oid)
157542e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->did);
157642e9a92fSRobert Love 	else
157742e9a92fSRobert Love 		hton24(acc->reca_rfid, ep->sid);
157842e9a92fSRobert Love 	acc->reca_fc4value = htonl(ep->seq.rec_data);
157942e9a92fSRobert Love 	acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
158042e9a92fSRobert Love 						 ESB_ST_SEQ_INIT |
158142e9a92fSRobert Love 						 ESB_ST_COMPLETE));
158242e9a92fSRobert Love 	sp = fc_seq_start_next(sp);
158342e9a92fSRobert Love 	fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
158442e9a92fSRobert Love out:
158542e9a92fSRobert Love 	fc_exch_release(ep);
158642e9a92fSRobert Love 	fc_frame_free(rfp);
158742e9a92fSRobert Love 	return;
158842e9a92fSRobert Love 
158942e9a92fSRobert Love rel:
159042e9a92fSRobert Love 	fc_exch_release(ep);
159142e9a92fSRobert Love reject:
159242e9a92fSRobert Love 	fc_seq_ls_rjt(sp, reason, explan);
159342e9a92fSRobert Love 	fc_frame_free(rfp);
159442e9a92fSRobert Love }
159542e9a92fSRobert Love 
159642e9a92fSRobert Love /*
159742e9a92fSRobert Love  * Handle response from RRQ.
159842e9a92fSRobert Love  * Not much to do here, really.
159942e9a92fSRobert Love  * Should report errors.
160042e9a92fSRobert Love  *
160142e9a92fSRobert Love  * TODO: fix error handler.
160242e9a92fSRobert Love  */
160342e9a92fSRobert Love static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
160442e9a92fSRobert Love {
160542e9a92fSRobert Love 	struct fc_exch *aborted_ep = arg;
160642e9a92fSRobert Love 	unsigned int op;
160742e9a92fSRobert Love 
160842e9a92fSRobert Love 	if (IS_ERR(fp)) {
160942e9a92fSRobert Love 		int err = PTR_ERR(fp);
161042e9a92fSRobert Love 
1611*78342da3SVasu Dev 		if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
161242e9a92fSRobert Love 			goto cleanup;
161342e9a92fSRobert Love 		FC_DBG("Cannot process RRQ, because of frame error %d\n", err);
161442e9a92fSRobert Love 		return;
161542e9a92fSRobert Love 	}
161642e9a92fSRobert Love 
161742e9a92fSRobert Love 	op = fc_frame_payload_op(fp);
161842e9a92fSRobert Love 	fc_frame_free(fp);
161942e9a92fSRobert Love 
162042e9a92fSRobert Love 	switch (op) {
162142e9a92fSRobert Love 	case ELS_LS_RJT:
162242e9a92fSRobert Love 		FC_DBG("LS_RJT for RRQ");
162342e9a92fSRobert Love 		/* fall through */
162442e9a92fSRobert Love 	case ELS_LS_ACC:
162542e9a92fSRobert Love 		goto cleanup;
162642e9a92fSRobert Love 	default:
162742e9a92fSRobert Love 		FC_DBG("unexpected response op %x for RRQ", op);
162842e9a92fSRobert Love 		return;
162942e9a92fSRobert Love 	}
163042e9a92fSRobert Love 
163142e9a92fSRobert Love cleanup:
163242e9a92fSRobert Love 	fc_exch_done(&aborted_ep->seq);
163342e9a92fSRobert Love 	/* drop hold for rec qual */
163442e9a92fSRobert Love 	fc_exch_release(aborted_ep);
163542e9a92fSRobert Love }
163642e9a92fSRobert Love 
163742e9a92fSRobert Love /*
163842e9a92fSRobert Love  * Send ELS RRQ - Reinstate Recovery Qualifier.
163942e9a92fSRobert Love  * This tells the remote port to stop blocking the use of
164042e9a92fSRobert Love  * the exchange and the seq_cnt range.
164142e9a92fSRobert Love  */
164242e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *ep)
164342e9a92fSRobert Love {
164442e9a92fSRobert Love 	struct fc_lport *lp;
164542e9a92fSRobert Love 	struct fc_els_rrq *rrq;
164642e9a92fSRobert Love 	struct fc_frame *fp;
164742e9a92fSRobert Love 	struct fc_seq *rrq_sp;
164842e9a92fSRobert Love 	u32 did;
164942e9a92fSRobert Love 
165042e9a92fSRobert Love 	lp = ep->lp;
165142e9a92fSRobert Love 
165242e9a92fSRobert Love 	fp = fc_frame_alloc(lp, sizeof(*rrq));
165342e9a92fSRobert Love 	if (!fp)
165442e9a92fSRobert Love 		return;
165542e9a92fSRobert Love 	rrq = fc_frame_payload_get(fp, sizeof(*rrq));
165642e9a92fSRobert Love 	memset(rrq, 0, sizeof(*rrq));
165742e9a92fSRobert Love 	rrq->rrq_cmd = ELS_RRQ;
165842e9a92fSRobert Love 	hton24(rrq->rrq_s_id, ep->sid);
165942e9a92fSRobert Love 	rrq->rrq_ox_id = htons(ep->oxid);
166042e9a92fSRobert Love 	rrq->rrq_rx_id = htons(ep->rxid);
166142e9a92fSRobert Love 
166242e9a92fSRobert Love 	did = ep->did;
166342e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_RESP)
166442e9a92fSRobert Love 		did = ep->sid;
166542e9a92fSRobert Love 
166642e9a92fSRobert Love 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
166742e9a92fSRobert Love 		       fc_host_port_id(lp->host), FC_TYPE_ELS,
166842e9a92fSRobert Love 		       FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
166942e9a92fSRobert Love 
167042e9a92fSRobert Love 	rrq_sp = fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep,
167142e9a92fSRobert Love 				  lp->e_d_tov);
167242e9a92fSRobert Love 	if (!rrq_sp) {
167342e9a92fSRobert Love 		ep->esb_stat |= ESB_ST_REC_QUAL;
167442e9a92fSRobert Love 		fc_exch_timer_set_locked(ep, ep->r_a_tov);
167542e9a92fSRobert Love 		return;
167642e9a92fSRobert Love 	}
167742e9a92fSRobert Love }
167842e9a92fSRobert Love 
167942e9a92fSRobert Love 
168042e9a92fSRobert Love /*
168142e9a92fSRobert Love  * Handle incoming ELS RRQ - Reset Recovery Qualifier.
168242e9a92fSRobert Love  */
168342e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
168442e9a92fSRobert Love {
168542e9a92fSRobert Love 	struct fc_exch *ep;		/* request or subject exchange */
168642e9a92fSRobert Love 	struct fc_els_rrq *rp;
168742e9a92fSRobert Love 	u32 sid;
168842e9a92fSRobert Love 	u16 xid;
168942e9a92fSRobert Love 	enum fc_els_rjt_explan explan;
169042e9a92fSRobert Love 
169142e9a92fSRobert Love 	rp = fc_frame_payload_get(fp, sizeof(*rp));
169242e9a92fSRobert Love 	explan = ELS_EXPL_INV_LEN;
169342e9a92fSRobert Love 	if (!rp)
169442e9a92fSRobert Love 		goto reject;
169542e9a92fSRobert Love 
169642e9a92fSRobert Love 	/*
169742e9a92fSRobert Love 	 * lookup subject exchange.
169842e9a92fSRobert Love 	 */
169942e9a92fSRobert Love 	ep = fc_seq_exch(sp);
170042e9a92fSRobert Love 	sid = ntoh24(rp->rrq_s_id);		/* subject source */
170142e9a92fSRobert Love 	xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
170242e9a92fSRobert Love 	ep = fc_exch_find(ep->em, xid);
170342e9a92fSRobert Love 
170442e9a92fSRobert Love 	explan = ELS_EXPL_OXID_RXID;
170542e9a92fSRobert Love 	if (!ep)
170642e9a92fSRobert Love 		goto reject;
170742e9a92fSRobert Love 	spin_lock_bh(&ep->ex_lock);
170842e9a92fSRobert Love 	if (ep->oxid != ntohs(rp->rrq_ox_id))
170942e9a92fSRobert Love 		goto unlock_reject;
171042e9a92fSRobert Love 	if (ep->rxid != ntohs(rp->rrq_rx_id) &&
171142e9a92fSRobert Love 	    ep->rxid != FC_XID_UNKNOWN)
171242e9a92fSRobert Love 		goto unlock_reject;
171342e9a92fSRobert Love 	explan = ELS_EXPL_SID;
171442e9a92fSRobert Love 	if (ep->sid != sid)
171542e9a92fSRobert Love 		goto unlock_reject;
171642e9a92fSRobert Love 
171742e9a92fSRobert Love 	/*
171842e9a92fSRobert Love 	 * Clear Recovery Qualifier state, and cancel timer if complete.
171942e9a92fSRobert Love 	 */
172042e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_REC_QUAL) {
172142e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_REC_QUAL;
172242e9a92fSRobert Love 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec qual */
172342e9a92fSRobert Love 	}
172442e9a92fSRobert Love 	if (ep->esb_stat & ESB_ST_COMPLETE) {
172542e9a92fSRobert Love 		if (cancel_delayed_work(&ep->timeout_work))
172642e9a92fSRobert Love 			atomic_dec(&ep->ex_refcnt);	/* drop timer hold */
172742e9a92fSRobert Love 	}
172842e9a92fSRobert Love 
172942e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
173042e9a92fSRobert Love 
173142e9a92fSRobert Love 	/*
173242e9a92fSRobert Love 	 * Send LS_ACC.
173342e9a92fSRobert Love 	 */
173442e9a92fSRobert Love 	fc_seq_ls_acc(sp);
173542e9a92fSRobert Love 	fc_frame_free(fp);
173642e9a92fSRobert Love 	return;
173742e9a92fSRobert Love 
173842e9a92fSRobert Love unlock_reject:
173942e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
174042e9a92fSRobert Love 	fc_exch_release(ep);	/* drop hold from fc_exch_find */
174142e9a92fSRobert Love reject:
174242e9a92fSRobert Love 	fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
174342e9a92fSRobert Love 	fc_frame_free(fp);
174442e9a92fSRobert Love }
174542e9a92fSRobert Love 
174642e9a92fSRobert Love struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
174742e9a92fSRobert Love 				      enum fc_class class,
174842e9a92fSRobert Love 				      u16 min_xid, u16 max_xid)
174942e9a92fSRobert Love {
175042e9a92fSRobert Love 	struct fc_exch_mgr *mp;
175142e9a92fSRobert Love 	size_t len;
175242e9a92fSRobert Love 
175342e9a92fSRobert Love 	if (max_xid <= min_xid || min_xid == 0 || max_xid == FC_XID_UNKNOWN) {
175442e9a92fSRobert Love 		FC_DBG("Invalid min_xid 0x:%x and max_xid 0x:%x\n",
175542e9a92fSRobert Love 		       min_xid, max_xid);
175642e9a92fSRobert Love 		return NULL;
175742e9a92fSRobert Love 	}
175842e9a92fSRobert Love 
175942e9a92fSRobert Love 	/*
176042e9a92fSRobert Love 	 * Memory need for EM
176142e9a92fSRobert Love 	 */
176242e9a92fSRobert Love #define xid_ok(i, m1, m2) (((i) >= (m1)) && ((i) <= (m2)))
176342e9a92fSRobert Love 	len = (max_xid - min_xid + 1) * (sizeof(struct fc_exch *));
176442e9a92fSRobert Love 	len += sizeof(struct fc_exch_mgr);
176542e9a92fSRobert Love 
176642e9a92fSRobert Love 	mp = kzalloc(len, GFP_ATOMIC);
176742e9a92fSRobert Love 	if (!mp)
176842e9a92fSRobert Love 		return NULL;
176942e9a92fSRobert Love 
177042e9a92fSRobert Love 	mp->class = class;
177142e9a92fSRobert Love 	mp->total_exches = 0;
177242e9a92fSRobert Love 	mp->exches = (struct fc_exch **)(mp + 1);
177342e9a92fSRobert Love 	mp->lp = lp;
177442e9a92fSRobert Love 	/* adjust em exch xid range for offload */
177542e9a92fSRobert Love 	mp->min_xid = min_xid;
177642e9a92fSRobert Love 	mp->max_xid = max_xid;
177742e9a92fSRobert Love 	mp->last_xid = min_xid - 1;
177842e9a92fSRobert Love 	mp->max_read = 0;
177942e9a92fSRobert Love 	mp->last_read = 0;
178042e9a92fSRobert Love 	if (lp->lro_enabled && xid_ok(lp->lro_xid, min_xid, max_xid)) {
178142e9a92fSRobert Love 		mp->max_read = lp->lro_xid;
178242e9a92fSRobert Love 		mp->last_read = min_xid - 1;
178342e9a92fSRobert Love 		mp->last_xid = mp->max_read;
178442e9a92fSRobert Love 	} else {
178542e9a92fSRobert Love 		/* disable lro if no xid control over read */
178642e9a92fSRobert Love 		lp->lro_enabled = 0;
178742e9a92fSRobert Love 	}
178842e9a92fSRobert Love 
178942e9a92fSRobert Love 	INIT_LIST_HEAD(&mp->ex_list);
179042e9a92fSRobert Love 	spin_lock_init(&mp->em_lock);
179142e9a92fSRobert Love 
179242e9a92fSRobert Love 	mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
179342e9a92fSRobert Love 	if (!mp->ep_pool)
179442e9a92fSRobert Love 		goto free_mp;
179542e9a92fSRobert Love 
179642e9a92fSRobert Love 	return mp;
179742e9a92fSRobert Love 
179842e9a92fSRobert Love free_mp:
179942e9a92fSRobert Love 	kfree(mp);
180042e9a92fSRobert Love 	return NULL;
180142e9a92fSRobert Love }
180242e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_alloc);
180342e9a92fSRobert Love 
180442e9a92fSRobert Love void fc_exch_mgr_free(struct fc_exch_mgr *mp)
180542e9a92fSRobert Love {
180642e9a92fSRobert Love 	WARN_ON(!mp);
180742e9a92fSRobert Love 	/*
180842e9a92fSRobert Love 	 * The total exch count must be zero
180942e9a92fSRobert Love 	 * before freeing exchange manager.
181042e9a92fSRobert Love 	 */
181142e9a92fSRobert Love 	WARN_ON(mp->total_exches != 0);
181242e9a92fSRobert Love 	mempool_destroy(mp->ep_pool);
181342e9a92fSRobert Love 	kfree(mp);
181442e9a92fSRobert Love }
181542e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_free);
181642e9a92fSRobert Love 
181742e9a92fSRobert Love struct fc_exch *fc_exch_get(struct fc_lport *lp, struct fc_frame *fp)
181842e9a92fSRobert Love {
181942e9a92fSRobert Love 	if (!lp || !lp->emp)
182042e9a92fSRobert Love 		return NULL;
182142e9a92fSRobert Love 
182242e9a92fSRobert Love 	return fc_exch_alloc(lp->emp, fp, 0);
182342e9a92fSRobert Love }
182442e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_get);
182542e9a92fSRobert Love 
182642e9a92fSRobert Love struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
182742e9a92fSRobert Love 				struct fc_frame *fp,
182842e9a92fSRobert Love 				void (*resp)(struct fc_seq *,
182942e9a92fSRobert Love 					     struct fc_frame *fp,
183042e9a92fSRobert Love 					     void *arg),
183142e9a92fSRobert Love 				void (*destructor)(struct fc_seq *, void *),
183242e9a92fSRobert Love 				void *arg, u32 timer_msec)
183342e9a92fSRobert Love {
183442e9a92fSRobert Love 	struct fc_exch *ep;
183542e9a92fSRobert Love 	struct fc_seq *sp = NULL;
183642e9a92fSRobert Love 	struct fc_frame_header *fh;
183742e9a92fSRobert Love 	int rc = 1;
183842e9a92fSRobert Love 
183942e9a92fSRobert Love 	ep = lp->tt.exch_get(lp, fp);
184042e9a92fSRobert Love 	if (!ep) {
184142e9a92fSRobert Love 		fc_frame_free(fp);
184242e9a92fSRobert Love 		return NULL;
184342e9a92fSRobert Love 	}
184442e9a92fSRobert Love 	ep->esb_stat |= ESB_ST_SEQ_INIT;
184542e9a92fSRobert Love 	fh = fc_frame_header_get(fp);
184642e9a92fSRobert Love 	fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
184742e9a92fSRobert Love 	ep->resp = resp;
184842e9a92fSRobert Love 	ep->destructor = destructor;
184942e9a92fSRobert Love 	ep->arg = arg;
185042e9a92fSRobert Love 	ep->r_a_tov = FC_DEF_R_A_TOV;
185142e9a92fSRobert Love 	ep->lp = lp;
185242e9a92fSRobert Love 	sp = &ep->seq;
185342e9a92fSRobert Love 
185442e9a92fSRobert Love 	ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
185542e9a92fSRobert Love 	ep->f_ctl = ntoh24(fh->fh_f_ctl);
185642e9a92fSRobert Love 	fc_exch_setup_hdr(ep, fp, ep->f_ctl);
185742e9a92fSRobert Love 	sp->cnt++;
185842e9a92fSRobert Love 
185942e9a92fSRobert Love 	if (unlikely(lp->tt.frame_send(lp, fp)))
186042e9a92fSRobert Love 		goto err;
186142e9a92fSRobert Love 
186242e9a92fSRobert Love 	if (timer_msec)
186342e9a92fSRobert Love 		fc_exch_timer_set_locked(ep, timer_msec);
186442e9a92fSRobert Love 	ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not first seq */
186542e9a92fSRobert Love 
186642e9a92fSRobert Love 	if (ep->f_ctl & FC_FC_SEQ_INIT)
186742e9a92fSRobert Love 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
186842e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
186942e9a92fSRobert Love 	return sp;
187042e9a92fSRobert Love err:
187142e9a92fSRobert Love 	rc = fc_exch_done_locked(ep);
187242e9a92fSRobert Love 	spin_unlock_bh(&ep->ex_lock);
187342e9a92fSRobert Love 	if (!rc)
187442e9a92fSRobert Love 		fc_exch_mgr_delete_ep(ep);
187542e9a92fSRobert Love 	return NULL;
187642e9a92fSRobert Love }
187742e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_seq_send);
187842e9a92fSRobert Love 
187942e9a92fSRobert Love /*
188042e9a92fSRobert Love  * Receive a frame
188142e9a92fSRobert Love  */
188242e9a92fSRobert Love void fc_exch_recv(struct fc_lport *lp, struct fc_exch_mgr *mp,
188342e9a92fSRobert Love 		  struct fc_frame *fp)
188442e9a92fSRobert Love {
188542e9a92fSRobert Love 	struct fc_frame_header *fh = fc_frame_header_get(fp);
188642e9a92fSRobert Love 	u32 f_ctl;
188742e9a92fSRobert Love 
188842e9a92fSRobert Love 	/* lport lock ? */
188942e9a92fSRobert Love 	if (!lp || !mp || (lp->state == LPORT_ST_NONE)) {
189042e9a92fSRobert Love 		FC_DBG("fc_lport or EM is not allocated and configured");
189142e9a92fSRobert Love 		fc_frame_free(fp);
189242e9a92fSRobert Love 		return;
189342e9a92fSRobert Love 	}
189442e9a92fSRobert Love 
189542e9a92fSRobert Love 	/*
189642e9a92fSRobert Love 	 * If frame is marked invalid, just drop it.
189742e9a92fSRobert Love 	 */
189842e9a92fSRobert Love 	f_ctl = ntoh24(fh->fh_f_ctl);
189942e9a92fSRobert Love 	switch (fr_eof(fp)) {
190042e9a92fSRobert Love 	case FC_EOF_T:
190142e9a92fSRobert Love 		if (f_ctl & FC_FC_END_SEQ)
190242e9a92fSRobert Love 			skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
190342e9a92fSRobert Love 		/* fall through */
190442e9a92fSRobert Love 	case FC_EOF_N:
190542e9a92fSRobert Love 		if (fh->fh_type == FC_TYPE_BLS)
190642e9a92fSRobert Love 			fc_exch_recv_bls(mp, fp);
190742e9a92fSRobert Love 		else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
190842e9a92fSRobert Love 			 FC_FC_EX_CTX)
190942e9a92fSRobert Love 			fc_exch_recv_seq_resp(mp, fp);
191042e9a92fSRobert Love 		else if (f_ctl & FC_FC_SEQ_CTX)
191142e9a92fSRobert Love 			fc_exch_recv_resp(mp, fp);
191242e9a92fSRobert Love 		else
191342e9a92fSRobert Love 			fc_exch_recv_req(lp, mp, fp);
191442e9a92fSRobert Love 		break;
191542e9a92fSRobert Love 	default:
191642e9a92fSRobert Love 		FC_DBG("dropping invalid frame (eof %x)", fr_eof(fp));
191742e9a92fSRobert Love 		fc_frame_free(fp);
191842e9a92fSRobert Love 		break;
191942e9a92fSRobert Love 	}
192042e9a92fSRobert Love }
192142e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_recv);
192242e9a92fSRobert Love 
192342e9a92fSRobert Love int fc_exch_init(struct fc_lport *lp)
192442e9a92fSRobert Love {
192542e9a92fSRobert Love 	if (!lp->tt.exch_get) {
192642e9a92fSRobert Love 		/*
192742e9a92fSRobert Love 		 *  exch_put() should be NULL if
192842e9a92fSRobert Love 		 *  exch_get() is NULL
192942e9a92fSRobert Love 		 */
193042e9a92fSRobert Love 		WARN_ON(lp->tt.exch_put);
193142e9a92fSRobert Love 		lp->tt.exch_get = fc_exch_get;
193242e9a92fSRobert Love 	}
193342e9a92fSRobert Love 
193442e9a92fSRobert Love 	if (!lp->tt.seq_start_next)
193542e9a92fSRobert Love 		lp->tt.seq_start_next = fc_seq_start_next;
193642e9a92fSRobert Love 
193742e9a92fSRobert Love 	if (!lp->tt.exch_seq_send)
193842e9a92fSRobert Love 		lp->tt.exch_seq_send = fc_exch_seq_send;
193942e9a92fSRobert Love 
194042e9a92fSRobert Love 	if (!lp->tt.seq_send)
194142e9a92fSRobert Love 		lp->tt.seq_send = fc_seq_send;
194242e9a92fSRobert Love 
194342e9a92fSRobert Love 	if (!lp->tt.seq_els_rsp_send)
194442e9a92fSRobert Love 		lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
194542e9a92fSRobert Love 
194642e9a92fSRobert Love 	if (!lp->tt.exch_done)
194742e9a92fSRobert Love 		lp->tt.exch_done = fc_exch_done;
194842e9a92fSRobert Love 
194942e9a92fSRobert Love 	if (!lp->tt.exch_mgr_reset)
195042e9a92fSRobert Love 		lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
195142e9a92fSRobert Love 
195242e9a92fSRobert Love 	if (!lp->tt.seq_exch_abort)
195342e9a92fSRobert Love 		lp->tt.seq_exch_abort = fc_seq_exch_abort;
195442e9a92fSRobert Love 
195542e9a92fSRobert Love 	return 0;
195642e9a92fSRobert Love }
195742e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_init);
195842e9a92fSRobert Love 
195942e9a92fSRobert Love int fc_setup_exch_mgr(void)
196042e9a92fSRobert Love {
196142e9a92fSRobert Love 	fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
196242e9a92fSRobert Love 					 0, SLAB_HWCACHE_ALIGN, NULL);
196342e9a92fSRobert Love 	if (!fc_em_cachep)
196442e9a92fSRobert Love 		return -ENOMEM;
196542e9a92fSRobert Love 	return 0;
196642e9a92fSRobert Love }
196742e9a92fSRobert Love 
196842e9a92fSRobert Love void fc_destroy_exch_mgr(void)
196942e9a92fSRobert Love {
197042e9a92fSRobert Love 	kmem_cache_destroy(fc_em_cachep);
197142e9a92fSRobert Love }
1972