Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright(c) 2007 Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright(c) 2008 Red Hat, Inc.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright(c) 2008 Mike Christie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Maintained at www.Open-FCoE.org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * Fibre Channel exchange and sequence handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/log2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <scsi/fc/fc_fc2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <scsi/libfc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <scsi/fc_encode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "fc_libfc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) u16	fc_cpu_mask;		/* cpu mask for possible cpus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) EXPORT_SYMBOL(fc_cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) static u16	fc_cpu_order;	/* 2's power to represent total possible cpus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) static struct kmem_cache *fc_em_cachep;	       /* cache for exchanges */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) static struct workqueue_struct *fc_exch_workqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * Structure and function definitions for managing Fibre Channel Exchanges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * and Sequences.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * fc_exch_mgr holds the exchange state for an N port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * fc_exch holds state for one exchange and links to its active sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * fc_seq holds the state for an individual sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * struct fc_exch_pool - Per cpu exchange pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * @next_index:	  Next possible free exchange index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * @total_exches: Total allocated exchanges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * @lock:	  Exch pool lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * @ex_list:	  List of exchanges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * @left:	  Cache of free slot in exch array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * @right:	  Cache of free slot in exch array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * This structure manages per cpu exchanges in array of exchange pointers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * This array is allocated followed by struct fc_exch_pool memory for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * assigned range of exchanges to per cpu pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) struct fc_exch_pool {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	spinlock_t	 lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	struct list_head ex_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	u16		 next_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	u16		 total_exches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	u16		 left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	u16		 right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) } ____cacheline_aligned_in_smp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * struct fc_exch_mgr - The Exchange Manager (EM).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * @class:	    Default class for new sequences
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * @kref:	    Reference counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * @min_xid:	    Minimum exchange ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  * @max_xid:	    Maximum exchange ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  * @ep_pool:	    Reserved exchange pointers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  * @pool_max_index: Max exch array index in exch pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * @pool:	    Per cpu exch pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * @lport:	    Local exchange port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * @stats:	    Statistics structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * This structure is the center for creating exchanges and sequences.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * It manages the allocation of exchange IDs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) struct fc_exch_mgr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	struct fc_exch_pool __percpu *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	mempool_t	*ep_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	struct fc_lport	*lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	enum fc_class	class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	struct kref	kref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	u16		min_xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	u16		max_xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	u16		pool_max_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		atomic_t no_free_exch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		atomic_t no_free_exch_xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		atomic_t xid_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		atomic_t xid_busy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		atomic_t seq_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		atomic_t non_bls_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	} stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  * struct fc_exch_mgr_anchor - primary structure for list of EMs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * @ema_list: Exchange Manager Anchor list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  * @mp:	      Exchange Manager associated with this anchor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  * @match:    Routine to determine if this anchor's EM should be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  * When walking the list of anchors the match routine will be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  * for each anchor to determine if that EM should be used. The last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  * anchor in the list will always match to handle any exchanges not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113)  * handled by other EMs. The non-default EMs would be added to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  * anchor list by HW that provides offloads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) struct fc_exch_mgr_anchor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	struct list_head ema_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	struct fc_exch_mgr *mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	bool (*match)(struct fc_frame *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) static void fc_exch_rrq(struct fc_exch *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) static void fc_seq_ls_acc(struct fc_frame *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) static void fc_seq_ls_rjt(struct fc_frame *, enum fc_els_rjt_reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 			  enum fc_els_rjt_explan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) static void fc_exch_els_rec(struct fc_frame *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) static void fc_exch_els_rrq(struct fc_frame *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130)  * Internal implementation notes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132)  * The exchange manager is one by default in libfc but LLD may choose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133)  * to have one per CPU. The sequence manager is one per exchange manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  * and currently never separated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136)  * Section 9.8 in FC-FS-2 specifies:  "The SEQ_ID is a one-byte field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137)  * assigned by the Sequence Initiator that shall be unique for a specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  * D_ID and S_ID pair while the Sequence is open."   Note that it isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  * qualified by exchange ID, which one might think it would be.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  * In practice this limits the number of open sequences and exchanges to 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  * per session.	 For most targets we could treat this limit as per exchange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143)  * The exchange and its sequence are freed when the last sequence is received.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144)  * It's possible for the remote port to leave an exchange open without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145)  * sending any sequences.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  * Notes on reference counts:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  * Exchanges are reference counted and exchange gets freed when the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  * count becomes zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * Timeouts:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  * Sequences are timed out for E_D_TOV and R_A_TOV.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155)  * Sequence event handling:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157)  * The following events may occur on initiator sequences:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159)  *	Send.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160)  *	    For now, the whole thing is sent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161)  *	Receive ACK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162)  *	    This applies only to class F.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163)  *	    The sequence is marked complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  *	ULP completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  *	    The upper layer calls fc_exch_done() when done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  *	    with exchange and sequence tuple.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  *	RX-inferred completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  *	    When we receive the next sequence on the same exchange, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  *	    retire the previous sequence ID.  (XXX not implemented).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  *	Timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  *	    R_A_TOV frees the sequence ID.  If we're waiting for ACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172)  *	    E_D_TOV causes abort and calls upper layer response handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173)  *	    with FC_EX_TIMEOUT error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174)  *	Receive RJT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175)  *	    XXX defer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176)  *	Send ABTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  *	    On timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179)  * The following events may occur on recipient sequences:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  *	Receive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  *	    Allocate sequence for first frame received.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183)  *	    Hold during receive handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184)  *	    Release when final frame received.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185)  *	    Keep status of last N of these for the ELS RES command.  XXX TBD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186)  *	Receive ABTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187)  *	    Deallocate sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188)  *	Send RJT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189)  *	    Deallocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191)  * For now, we neglect conditions where only part of a sequence was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192)  * received or transmitted, or where out-of-order receipt is detected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196)  * Locking notes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  * The EM code run in a per-CPU worker thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  * To protect against concurrency between a worker thread code and timers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  * sequence allocation and deallocation must be locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  *  - exchange refcnt can be done atomicly without locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  *  - sequence allocation must be locked by exch lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  *  - If the EM pool lock and ex_lock must be taken at the same time, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  *    EM pool lock must be taken before the ex_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  * opcode names for debugging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  * fc_exch_name_lookup() - Lookup name by opcode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  * @op:	       Opcode to be looked up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  * @table:     Opcode/name table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  * @max_index: Index not to be exceeded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219)  * This routine is used to determine a human-readable string identifying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220)  * a R_CTL opcode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 					      unsigned int max_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	const char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	if (op < max_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		name = table[op];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		name = "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235)  * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236)  * @op: The opcode to be looked up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) static const char *fc_exch_rctl_name(unsigned int op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	return fc_exch_name_lookup(op, fc_exch_rctl_names,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 				   ARRAY_SIZE(fc_exch_rctl_names));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245)  * fc_exch_hold() - Increment an exchange's reference count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246)  * @ep: Echange to be held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) static inline void fc_exch_hold(struct fc_exch *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	atomic_inc(&ep->ex_refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254)  * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255)  *			 and determine SOF and EOF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256)  * @ep:	   The exchange to that will use the header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257)  * @fp:	   The frame whose header is to be modified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258)  * @f_ctl: F_CTL bits that will be used for the frame header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260)  * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261)  * fh_seq_id, fh_seq_cnt and the SOF and EOF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 			      u32 f_ctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	struct fc_frame_header *fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	u16 fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	fr_sof(fp) = ep->class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	if (ep->seq.cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		fr_sof(fp) = fc_sof_normal(ep->class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	if (f_ctl & FC_FC_END_SEQ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		fr_eof(fp) = FC_EOF_T;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		if (fc_sof_needs_ack(ep->class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			fr_eof(fp) = FC_EOF_N;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		 * From F_CTL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		 * The number of fill bytes to make the length a 4-byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		 * multiple is the low order 2-bits of the f_ctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		 * The fill itself will have been cleared by the frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		 * allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		 * After this, the length will be even, as expected by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		 * the transport.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		fill = fr_len(fp) & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		if (fill) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 			fill = 4 - fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 			/* TODO, this may be a problem with fragmented skb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 			skb_put(fp_skb(fp), fill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 			hton24(fh->fh_f_ctl, f_ctl | fill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		WARN_ON(fr_len(fp) % 4 != 0);	/* no pad to non last frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		fr_eof(fp) = FC_EOF_N;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	/* Initialize remaining fh fields from fc_fill_fc_hdr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	fh->fh_ox_id = htons(ep->oxid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	fh->fh_rx_id = htons(ep->rxid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	fh->fh_seq_id = ep->seq.id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	fh->fh_seq_cnt = htons(ep->seq.cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306)  * fc_exch_release() - Decrement an exchange's reference count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307)  * @ep: Exchange to be released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309)  * If the reference count reaches zero and the exchange is complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310)  * it is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) static void fc_exch_release(struct fc_exch *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	struct fc_exch_mgr *mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	if (atomic_dec_and_test(&ep->ex_refcnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		mp = ep->em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		if (ep->destructor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 			ep->destructor(&ep->seq, ep->arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		mempool_free(ep, mp->ep_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326)  * fc_exch_timer_cancel() - cancel exch timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  * @ep:		The exchange whose timer to be canceled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static inline void fc_exch_timer_cancel(struct fc_exch *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	if (cancel_delayed_work(&ep->timeout_work)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		FC_EXCH_DBG(ep, "Exchange timer canceled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  *				the exchange lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  * @ep:		The exchange whose timer will start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  * @timer_msec: The timeout period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343)  * Used for upper level protocols to time out the exchange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344)  * The timer is cancelled when it fires or when the exchange completes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 					    unsigned int timer_msec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	FC_EXCH_DBG(ep, "Exchange timer armed : %d msecs\n", timer_msec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	fc_exch_hold(ep);		/* hold for timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	if (!queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 				msecs_to_jiffies(timer_msec))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		FC_EXCH_DBG(ep, "Exchange already queued\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363)  * fc_exch_timer_set() - Lock the exchange and set the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364)  * @ep:		The exchange whose timer will start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365)  * @timer_msec: The timeout period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	fc_exch_timer_set_locked(ep, timer_msec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375)  * fc_exch_done_locked() - Complete an exchange with the exchange lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376)  * @ep: The exchange that is complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  * Note: May sleep if invoked from outside a response handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) static int fc_exch_done_locked(struct fc_exch *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	int rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	 * We must check for completion in case there are two threads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	 * tyring to complete this. But the rrq code will reuse the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	 * ep, and in that case we only clear the resp and set it as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	 * complete, so it can be reused by the timer to send the rrq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	if (ep->state & FC_EX_DONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	ep->esb_stat |= ESB_ST_COMPLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		ep->state |= FC_EX_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		fc_exch_timer_cancel(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) static struct fc_exch fc_quarantine_exch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405)  * fc_exch_ptr_get() - Return an exchange from an exchange pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  * @pool:  Exchange Pool to get an exchange from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  * @index: Index of the exchange within the pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409)  * Use the index to get an exchange from within an exchange pool. exches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410)  * will point to an array of exchange pointers. The index will select
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411)  * the exchange within the array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 					      u16 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	struct fc_exch **exches = (struct fc_exch **)(pool + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	return exches[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  * @pool:  The pool to assign the exchange to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423)  * @index: The index in the pool where the exchange will be assigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424)  * @ep:	   The exchange to assign to the pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 				   struct fc_exch *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	((struct fc_exch **)(pool + 1))[index] = ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433)  * fc_exch_delete() - Delete an exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434)  * @ep: The exchange to be deleted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) static void fc_exch_delete(struct fc_exch *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	struct fc_exch_pool *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	u16 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	pool = ep->pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	spin_lock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	WARN_ON(pool->total_exches <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	pool->total_exches--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	/* update cache of free slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	index = (ep->xid - ep->em->min_xid) >> fc_cpu_order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	if (!(ep->state & FC_EX_QUARANTINE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		if (pool->left == FC_XID_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 			pool->left = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		else if (pool->right == FC_XID_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 			pool->right = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			pool->next_index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		fc_exch_ptr_set(pool, index, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		fc_exch_ptr_set(pool, index, &fc_quarantine_exch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	list_del(&ep->ex_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	spin_unlock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	fc_exch_release(ep);	/* drop hold for exch in mp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) static int fc_seq_send_locked(struct fc_lport *lport, struct fc_seq *sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 			      struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	struct fc_frame_header *fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	int error = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	u32 f_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	u8 fh_type = fh->fh_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	ep = fc_seq_exch(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	WARN_ON(!(ep->esb_stat & ESB_ST_SEQ_INIT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	f_ctl = ntoh24(fh->fh_f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	fc_exch_setup_hdr(ep, fp, f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	fr_encaps(fp) = ep->encaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	 * update sequence count if this frame is carrying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	 * multiple FC frames when sequence offload is enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	 * by LLD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	if (fr_max_payload(fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 					fr_max_payload(fp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		sp->cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	 * Send the frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	error = lport->tt.frame_send(lport, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	if (fh_type == FC_TYPE_BLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	 * Update the exchange and sequence flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	 * assuming all frames for the sequence have been sent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	 * We can only be called to send once for each sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ;	/* not first seq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	if (f_ctl & FC_FC_SEQ_INIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518)  * fc_seq_send() - Send a frame using existing sequence/exchange pair
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519)  * @lport: The local port that the exchange will be sent on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520)  * @sp:	   The sequence to be sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521)  * @fp:	   The frame to be sent on the exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523)  * Note: The frame will be freed either by a direct call to fc_frame_free(fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524)  * or indirectly by calling libfc_function_template.frame_send().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp, struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	ep = fc_seq_exch(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	error = fc_seq_send_locked(lport, sp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) EXPORT_SYMBOL(fc_seq_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539)  * fc_seq_alloc() - Allocate a sequence for a given exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540)  * @ep:	    The exchange to allocate a new sequence for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541)  * @seq_id: The sequence ID to be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543)  * We don't support multiple originated sequences on the same exchange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544)  * By implication, any previously originated sequence on this exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545)  * is complete, and we reallocate the same sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	struct fc_seq *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	sp = &ep->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	sp->ssb_stat = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	sp->cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	sp->id = seq_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	return sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559)  * fc_seq_start_next_locked() - Allocate a new sequence on the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560)  *				exchange as the supplied sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561)  * @sp: The sequence/exchange to get a new sequence for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	struct fc_exch *ep = fc_seq_exch(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	sp = fc_seq_alloc(ep, ep->seq_id++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		    ep->f_ctl, sp->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	return sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574)  * fc_seq_start_next() - Lock the exchange and get a new sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575)  *			 for a given sequence/exchange pair
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576)  * @sp: The sequence/exchange to get a new exchange for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	struct fc_exch *ep = fc_seq_exch(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	sp = fc_seq_start_next_locked(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	return sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) EXPORT_SYMBOL(fc_seq_start_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591)  * Set the response handler for the exchange associated with a sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593)  * Note: May sleep if invoked from outside a response handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) void fc_seq_set_resp(struct fc_seq *sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		     void (*resp)(struct fc_seq *, struct fc_frame *, void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		     void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	struct fc_exch *ep = fc_seq_exch(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	while (ep->resp_active && ep->resp_task != current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		prepare_to_wait(&ep->resp_wq, &wait, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	finish_wait(&ep->resp_wq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	ep->resp = resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	ep->arg = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) EXPORT_SYMBOL(fc_seq_set_resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619)  * fc_exch_abort_locked() - Abort an exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620)  * @ep:	The exchange to be aborted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621)  * @timer_msec: The period of time to wait before aborting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623)  * Abort an exchange and sequence. Generally called because of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624)  * exchange timeout or an abort from the upper layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626)  * A timer_msec can be specified for abort timeout, if non-zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627)  * timer_msec value is specified then exchange resp handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628)  * will be called with timeout error if no response to abort.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630)  * Locking notes:  Called with exch lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632)  * Return value: 0 on success else error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) static int fc_exch_abort_locked(struct fc_exch *ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 				unsigned int timer_msec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	struct fc_seq *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	struct fc_frame *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	FC_EXCH_DBG(ep, "exch: abort, time %d msecs\n", timer_msec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	    ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		FC_EXCH_DBG(ep, "exch: already completed esb %x state %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 			    ep->esb_stat, ep->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	 * Send the abort on a new sequence if possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	sp = fc_seq_start_next_locked(&ep->seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	if (!sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (timer_msec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		fc_exch_timer_set_locked(ep, timer_msec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	if (ep->sid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		 * Send an abort for the sequence that timed out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		fp = fc_frame_alloc(ep->lp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		if (fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 			ep->esb_stat |= ESB_ST_SEQ_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 			fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 				       FC_TYPE_BLS, FC_FC_END_SEQ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 				       FC_FC_SEQ_INIT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 			error = fc_seq_send_locked(ep->lp, sp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 			error = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		 * If not logged into the fabric, don't send ABTS but leave
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		 * sequence active until next timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	ep->esb_stat |= ESB_ST_ABNORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685)  * fc_seq_exch_abort() - Abort an exchange and sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686)  * @req_sp:	The sequence to be aborted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687)  * @timer_msec: The period of time to wait before aborting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689)  * Generally called because of a timeout or an abort from the upper layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691)  * Return value: 0 on success else error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	ep = fc_seq_exch(req_sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	error = fc_exch_abort_locked(ep, timer_msec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706)  * fc_invoke_resp() - invoke ep->resp()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707)  * @ep:	   The exchange to be operated on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708)  * @fp:	   The frame pointer to pass through to ->resp()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709)  * @sp:	   The sequence pointer to pass through to ->resp()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711)  * Notes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712)  * It is assumed that after initialization finished (this means the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713)  * first unlock of ex_lock after fc_exch_alloc()) ep->resp and ep->arg are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714)  * modified only via fc_seq_set_resp(). This guarantees that none of these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715)  * two variables changes if ep->resp_active > 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717)  * If an fc_seq_set_resp() call is busy modifying ep->resp and ep->arg when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718)  * this function is invoked, the first spin_lock_bh() call in this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719)  * will wait until fc_seq_set_resp() has finished modifying these variables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721)  * Since fc_exch_done() invokes fc_seq_set_resp() it is guaranteed that that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722)  * ep->resp() won't be invoked after fc_exch_done() has returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724)  * The response handler itself may invoke fc_exch_done(), which will clear the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725)  * ep->resp pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727)  * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728)  * Returns true if and only if ep->resp has been invoked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) static bool fc_invoke_resp(struct fc_exch *ep, struct fc_seq *sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 			   struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	void *arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	bool res = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	ep->resp_active++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (ep->resp_task != current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		ep->resp_task = !ep->resp_task ? current : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	resp = ep->resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	arg = ep->arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	if (resp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		resp(sp, fp, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		res = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	if (--ep->resp_active == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		ep->resp_task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	if (ep->resp_active == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		wake_up(&ep->resp_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  * fc_exch_timeout() - Handle exchange timer expiration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763)  * @work: The work_struct identifying the exchange that timed out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) static void fc_exch_timeout(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	struct fc_exch *ep = container_of(work, struct fc_exch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 					  timeout_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	struct fc_seq *sp = &ep->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	u32 e_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	int rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	FC_EXCH_DBG(ep, "Exchange timed out state %x\n", ep->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	e_stat = ep->esb_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (e_stat & ESB_ST_COMPLETE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		if (e_stat & ESB_ST_REC_QUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			fc_exch_rrq(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		if (e_stat & ESB_ST_ABNORMAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 			rc = fc_exch_done_locked(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 			fc_exch_delete(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		fc_invoke_resp(ep, sp, ERR_PTR(-FC_EX_TIMEOUT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		fc_seq_set_resp(sp, NULL, ep->arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	 * This release matches the hold taken when the timer was set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807)  * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808)  * @lport: The local port that the exchange is for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809)  * @mp:	   The exchange manager that will allocate the exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811)  * Returns pointer to allocated fc_exch with exch lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 					struct fc_exch_mgr *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	u16 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	struct fc_exch_pool *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	/* allocate memory for exchange */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		atomic_inc(&mp->stats.no_free_exch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	memset(ep, 0, sizeof(*ep));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	cpu = get_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	pool = per_cpu_ptr(mp->pool, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	spin_lock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	put_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	/* peek cache of free slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	if (pool->left != FC_XID_UNKNOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		if (!WARN_ON(fc_exch_ptr_get(pool, pool->left))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			index = pool->left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			pool->left = FC_XID_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			goto hit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	if (pool->right != FC_XID_UNKNOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		if (!WARN_ON(fc_exch_ptr_get(pool, pool->right))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			index = pool->right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			pool->right = FC_XID_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 			goto hit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	index = pool->next_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	/* allocate new exch from pool */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	while (fc_exch_ptr_get(pool, index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		index = index == mp->pool_max_index ? 0 : index + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		if (index == pool->next_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) hit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	fc_exch_hold(ep);	/* hold for exch in mp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	spin_lock_init(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	 * Hold exch lock for caller to prevent fc_exch_reset()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	 * from releasing exch	while fc_exch_alloc() caller is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	 * still working on exch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	fc_exch_ptr_set(pool, index, ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	list_add_tail(&ep->ex_list, &pool->ex_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	fc_seq_alloc(ep, ep->seq_id++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	pool->total_exches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	spin_unlock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	 *  update exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	ep->em = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	ep->pool = pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	ep->lp = lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	ep->f_ctl = FC_FC_FIRST_SEQ;	/* next seq is first seq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	ep->rxid = FC_XID_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	ep->class = mp->class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	ep->resp_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	init_waitqueue_head(&ep->resp_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	return ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	spin_unlock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	atomic_inc(&mp->stats.no_free_exch_xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	mempool_free(ep, mp->ep_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897)  * fc_exch_alloc() - Allocate an exchange from an EM on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898)  *		     local port's list of EMs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899)  * @lport: The local port that will own the exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900)  * @fp:	   The FC frame that the exchange will be for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  * This function walks the list of exchange manager(EM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903)  * anchors to select an EM for a new exchange allocation. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904)  * EM is selected when a NULL match function pointer is encountered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905)  * or when a call to a match function returns true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) static struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 				     struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	struct fc_exch_mgr_anchor *ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		if (!ema->match || ema->match(fp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 			ep = fc_exch_em_alloc(lport, ema->mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 			if (ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 				return ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924)  * fc_exch_find() - Lookup and hold an exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925)  * @mp:	 The exchange manager to lookup the exchange from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926)  * @xid: The XID of the exchange to look up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	struct fc_lport *lport = mp->lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	struct fc_exch_pool *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct fc_exch *ep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	u16 cpu = xid & fc_cpu_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (xid == FC_XID_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		pr_err("host%u: lport %6.6x: xid %d invalid CPU %d\n:",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		       lport->host->host_no, lport->port_id, xid, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		pool = per_cpu_ptr(mp->pool, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		spin_lock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		if (ep == &fc_quarantine_exch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 			FC_LPORT_DBG(lport, "xid %x quarantined\n", xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			ep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		if (ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			WARN_ON(ep->xid != xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 			fc_exch_hold(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		spin_unlock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	return ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963)  * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964)  *		    the memory allocated for the related objects may be freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965)  * @sp: The sequence that has completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967)  * Note: May sleep if invoked from outside a response handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) void fc_exch_done(struct fc_seq *sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	struct fc_exch *ep = fc_seq_exch(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	rc = fc_exch_done_locked(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	fc_seq_set_resp(sp, NULL, ep->arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		fc_exch_delete(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) EXPORT_SYMBOL(fc_exch_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985)  * fc_exch_resp() - Allocate a new exchange for a response frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986)  * @lport: The local port that the exchange was for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987)  * @mp:	   The exchange manager to allocate the exchange from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988)  * @fp:	   The response frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990)  * Sets the responder ID in the frame header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 				    struct fc_exch_mgr *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 				    struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	struct fc_frame_header *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	ep = fc_exch_alloc(lport, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	if (ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		ep->class = fc_frame_class(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		 * Set EX_CTX indicating we're responding on this exchange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		ep->f_ctl |= FC_FC_EX_CTX;	/* we're responding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not new */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		ep->sid = ntoh24(fh->fh_d_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		ep->did = ntoh24(fh->fh_s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		ep->oid = ep->did;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		 * Allocated exchange has placed the XID in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		 * originator field. Move it to the responder field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		 * and set the originator XID from the frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		ep->rxid = ep->xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		ep->oxid = ntohs(fh->fh_ox_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			ep->esb_stat &= ~ESB_ST_SEQ_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		fc_exch_hold(ep);	/* hold for caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		spin_unlock_bh(&ep->ex_lock);	/* lock from fc_exch_alloc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	return ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)  * fc_seq_lookup_recip() - Find a sequence where the other end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  *			   originated the sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)  * @lport: The local port that the frame was sent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)  * @mp:	   The Exchange Manager to lookup the exchange from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)  * @fp:	   The frame associated with the sequence we're looking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)  * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)  * on the ep that should be released by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 						 struct fc_exch_mgr *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 						 struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	struct fc_frame_header *fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	struct fc_exch *ep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	struct fc_seq *sp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	enum fc_pf_rjt_reason reject = FC_RJT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	u32 f_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	u16 xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	f_ctl = ntoh24(fh->fh_f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	 * Lookup or create the exchange if we will be creating the sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	if (f_ctl & FC_FC_EX_CTX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		xid = ntohs(fh->fh_ox_id);	/* we originated exch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		ep = fc_exch_find(mp, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			atomic_inc(&mp->stats.xid_not_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 			reject = FC_RJT_OX_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		if (ep->rxid == FC_XID_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 			ep->rxid = ntohs(fh->fh_rx_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		else if (ep->rxid != ntohs(fh->fh_rx_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 			reject = FC_RJT_OX_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			goto rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		xid = ntohs(fh->fh_rx_id);	/* we are the responder */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		 * Special case for MDS issuing an ELS TEST with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		 * bad rxid of 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		 * XXX take this out once we do the proper reject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		    fc_frame_payload_op(fp) == ELS_TEST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			fh->fh_rx_id = htons(FC_XID_UNKNOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			xid = FC_XID_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		 * new sequence - find the exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		ep = fc_exch_find(mp, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 			if (ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 				atomic_inc(&mp->stats.xid_busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 				reject = FC_RJT_RX_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 				goto rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			ep = fc_exch_resp(lport, mp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 				reject = FC_RJT_EXCH_EST;	/* XXX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 			xid = ep->xid;	/* get our XID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		} else if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			atomic_inc(&mp->stats.xid_not_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			reject = FC_RJT_RX_ID;	/* XID not found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	 * At this point, we have the exchange held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	 * Find or create the sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	if (fc_sof_is_init(fr_sof(fp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		sp = &ep->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		sp->ssb_stat |= SSB_ST_RESP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		sp->id = fh->fh_seq_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		sp = &ep->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		if (sp->id != fh->fh_seq_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 			atomic_inc(&mp->stats.seq_not_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 			if (f_ctl & FC_FC_END_SEQ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 				 * Update sequence_id based on incoming last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 				 * frame of sequence exchange. This is needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 				 * for FC target where DDP has been used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 				 * on target where, stack is indicated only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 				 * about last frame's (payload _header) header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 				 * Whereas "seq_id" which is part of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 				 * frame_header is allocated by initiator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 				 * which is totally different from "seq_id"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 				 * allocated when XFER_RDY was sent by target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 				 * To avoid false -ve which results into not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 				 * sending RSP, hence write request on other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 				 * end never finishes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 				sp->ssb_stat |= SSB_ST_RESP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 				sp->id = fh->fh_seq_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 				spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 				/* sequence/exch should exist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 				reject = FC_RJT_SEQ_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 				goto rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	WARN_ON(ep != fc_seq_exch(sp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	if (f_ctl & FC_FC_SEQ_INIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		ep->esb_stat |= ESB_ST_SEQ_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	fr_seq(fp) = sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	return reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) rel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	fc_exch_done(&ep->seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	fc_exch_release(ep);	/* hold from fc_exch_find/fc_exch_resp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	return reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)  * fc_seq_lookup_orig() - Find a sequence where this end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)  *			  originated the sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165)  * @mp:	   The Exchange Manager to lookup the exchange from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)  * @fp:	   The frame associated with the sequence we're looking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)  * Does not hold the sequence for the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 					 struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	struct fc_frame_header *fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	struct fc_seq *sp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	u32 f_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	u16 xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	f_ctl = ntoh24(fh->fh_f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	ep = fc_exch_find(mp, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	if (ep->seq.id == fh->fh_seq_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		 * Save the RX_ID if we didn't previously know it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		sp = &ep->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		if ((f_ctl & FC_FC_EX_CTX) != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		    ep->rxid == FC_XID_UNKNOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			ep->rxid = ntohs(fh->fh_rx_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	return sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)  * fc_exch_set_addr() - Set the source and destination IDs for an exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)  * @ep:	     The exchange to set the addresses for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)  * @orig_id: The originator's ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)  * @resp_id: The responder's ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)  * Note this must be done before the first sequence of the exchange is sent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) static void fc_exch_set_addr(struct fc_exch *ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 			     u32 orig_id, u32 resp_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	ep->oid = orig_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	if (ep->esb_stat & ESB_ST_RESP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		ep->sid = resp_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		ep->did = orig_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		ep->sid = orig_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		ep->did = resp_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)  * fc_seq_els_rsp_send() - Send an ELS response using information from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)  *			   the existing sequence/exchange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)  * @fp:	      The received frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)  * @els_cmd:  The ELS command to be sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)  * @els_data: The ELS data to be sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)  * The received frame is not freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) void fc_seq_els_rsp_send(struct fc_frame *fp, enum fc_els_cmd els_cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 			 struct fc_seq_els_data *els_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	switch (els_cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	case ELS_LS_RJT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		fc_seq_ls_rjt(fp, els_data->reason, els_data->explan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	case ELS_LS_ACC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		fc_seq_ls_acc(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	case ELS_RRQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		fc_exch_els_rrq(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	case ELS_REC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		fc_exch_els_rec(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		FC_LPORT_DBG(fr_dev(fp), "Invalid ELS CMD:%x\n", els_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) EXPORT_SYMBOL_GPL(fc_seq_els_rsp_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)  * fc_seq_send_last() - Send a sequence that is the last in the exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)  * @sp:	     The sequence that is to be sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)  * @fp:	     The frame that will be sent on the sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)  * @rctl:    The R_CTL information to be sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)  * @fh_type: The frame header type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 			     enum fc_rctl rctl, enum fc_fh_type fh_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	u32 f_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	struct fc_exch *ep = fc_seq_exch(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	f_ctl |= ep->f_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	fc_seq_send_locked(ep->lp, sp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)  * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)  * @sp:	   The sequence to send the ACK on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)  * @rx_fp: The received frame that is being acknoledged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)  * Send ACK_1 (or equiv.) indicating we received something.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	struct fc_frame *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	struct fc_frame_header *rx_fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	struct fc_frame_header *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	struct fc_exch *ep = fc_seq_exch(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	struct fc_lport *lport = ep->lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	unsigned int f_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	 * Don't send ACKs for class 3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	if (fc_sof_needs_ack(fr_sof(rx_fp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		fp = fc_frame_alloc(lport, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			FC_EXCH_DBG(ep, "Drop ACK request, out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		fh->fh_r_ctl = FC_RCTL_ACK_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		fh->fh_type = FC_TYPE_BLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		 * Bits 9-8 are meaningful (retransmitted or unidirectional).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		 * Last ACK uses bits 7-6 (continue sequence),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		 * bits 5-4 are meaningful (what kind of ACK to use).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		rx_fh = fc_frame_header_get(rx_fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		f_ctl = ntoh24(rx_fh->fh_f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 			FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 			FC_FC_RETX_SEQ | FC_FC_UNI_TX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		hton24(fh->fh_f_ctl, f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		fc_exch_setup_hdr(ep, fp, f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		fh->fh_seq_id = rx_fh->fh_seq_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		fh->fh_parm_offset = htonl(1);	/* ack single frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		fr_sof(fp) = fr_sof(rx_fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		if (f_ctl & FC_FC_END_SEQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 			fr_eof(fp) = FC_EOF_T;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 			fr_eof(fp) = FC_EOF_N;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		lport->tt.frame_send(lport, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)  * fc_exch_send_ba_rjt() - Send BLS Reject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)  * @rx_fp:  The frame being rejected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334)  * @reason: The reason the frame is being rejected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)  * @explan: The explanation for the rejection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)  * This is for rejecting BA_ABTS only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 				enum fc_ba_rjt_reason reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 				enum fc_ba_rjt_explan explan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	struct fc_frame *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	struct fc_frame_header *rx_fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	struct fc_frame_header *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	struct fc_ba_rjt *rp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	struct fc_seq *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	struct fc_lport *lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	unsigned int f_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	lport = fr_dev(rx_fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	sp = fr_seq(rx_fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	fp = fc_frame_alloc(lport, sizeof(*rp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		FC_EXCH_DBG(fc_seq_exch(sp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			     "Drop BA_RJT request, out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	rx_fh = fc_frame_header_get(rx_fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	memset(fh, 0, sizeof(*fh) + sizeof(*rp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	rp = fc_frame_payload_get(fp, sizeof(*rp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	rp->br_reason = reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	rp->br_explan = explan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	 * seq_id, cs_ctl, df_ctl and param/offset are zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	fh->fh_ox_id = rx_fh->fh_ox_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	fh->fh_rx_id = rx_fh->fh_rx_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	fh->fh_r_ctl = FC_RCTL_BA_RJT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	fh->fh_type = FC_TYPE_BLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	 * Bits 9-8 are meaningful (retransmitted or unidirectional).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	 * Last ACK uses bits 7-6 (continue sequence),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	 * bits 5-4 are meaningful (what kind of ACK to use).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	 * Always set LAST_SEQ, END_SEQ.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	f_ctl = ntoh24(rx_fh->fh_f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		FC_FC_END_CONN | FC_FC_SEQ_INIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		FC_FC_RETX_SEQ | FC_FC_UNI_TX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	f_ctl &= ~FC_FC_FIRST_SEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	hton24(fh->fh_f_ctl, f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	fr_eof(fp) = FC_EOF_T;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	if (fc_sof_needs_ack(fr_sof(fp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		fr_eof(fp) = FC_EOF_N;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	lport->tt.frame_send(lport, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405)  * fc_exch_recv_abts() - Handle an incoming ABTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)  * @ep:	   The exchange the abort was on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)  * @rx_fp: The ABTS frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)  * This would be for target mode usually, but could be due to lost
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)  * FCP transfer ready, confirm or RRQ. We always handle this as an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)  * exchange abort, ignoring the parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	struct fc_frame *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	struct fc_ba_acc *ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	struct fc_frame_header *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	struct fc_seq *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	FC_EXCH_DBG(ep, "exch: ABTS received\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	fp = fc_frame_alloc(ep->lp, sizeof(*ap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		FC_EXCH_DBG(ep, "Drop ABTS request, out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	if (ep->esb_stat & ESB_ST_COMPLETE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		FC_EXCH_DBG(ep, "exch: ABTS rejected, exchange complete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		ep->esb_stat |= ESB_ST_REC_QUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		fc_exch_hold(ep);		/* hold for REC_QUAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	ap = fc_frame_payload_get(fp, sizeof(*ap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	memset(ap, 0, sizeof(*ap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	sp = &ep->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	ap->ba_high_seq_cnt = htons(0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	if (sp->ssb_stat & SSB_ST_RESP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 		ap->ba_seq_id = sp->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		ap->ba_high_seq_cnt = fh->fh_seq_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 		ap->ba_low_seq_cnt = htons(sp->cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	sp = fc_seq_start_next_locked(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	ep->esb_stat |= ESB_ST_ABNORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	fc_frame_free(rx_fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) reject:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468)  * fc_seq_assign() - Assign exchange and sequence for incoming request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)  * @lport: The local port that received the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)  * @fp:    The request frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)  * On success, the sequence pointer will be returned and also in fr_seq(@fp).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473)  * A reference will be held on the exchange/sequence for the caller, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)  * must call fc_seq_release().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) struct fc_seq *fc_seq_assign(struct fc_lport *lport, struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	struct fc_exch_mgr_anchor *ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	WARN_ON(lport != fr_dev(fp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	WARN_ON(fr_seq(fp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	fr_seq(fp) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	list_for_each_entry(ema, &lport->ema_list, ema_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		if ((!ema->match || ema->match(fp)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		    fc_seq_lookup_recip(lport, ema->mp, fp) == FC_RJT_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	return fr_seq(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) EXPORT_SYMBOL(fc_seq_assign);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)  * fc_seq_release() - Release the hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494)  * @sp:    The sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) void fc_seq_release(struct fc_seq *sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	fc_exch_release(fc_seq_exch(sp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) EXPORT_SYMBOL(fc_seq_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)  * fc_exch_recv_req() - Handler for an incoming request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504)  * @lport: The local port that received the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)  * @mp:	   The EM that the exchange is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  * @fp:	   The request frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)  * This is used when the other end is originating the exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)  * and the sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 			     struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	struct fc_frame_header *fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	struct fc_seq *sp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	struct fc_exch *ep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	enum fc_pf_rjt_reason reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	/* We can have the wrong fc_lport at this point with NPIV, which is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	 * problem now that we know a new exchange needs to be allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	if (!lport) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	fr_dev(fp) = lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	BUG_ON(fr_seq(fp));		/* XXX remove later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	 * If the RX_ID is 0xffff, don't allocate an exchange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	 * The upper-level protocol may request one later, if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	if (fh->fh_rx_id == htons(FC_XID_UNKNOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 		return fc_lport_recv(lport, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	reject = fc_seq_lookup_recip(lport, mp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	if (reject == FC_RJT_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		sp = fr_seq(fp);	/* sequence will be held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		ep = fc_seq_exch(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		fc_seq_send_ack(sp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		ep->encaps = fr_encaps(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		 * Call the receive function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		 * The receive function may allocate a new sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		 * over the old one, so we shouldn't change the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		 * sequence after this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		 * The frame will be freed by the receive function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		 * If new exch resp handler is valid then call that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		 * first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		if (!fc_invoke_resp(ep, sp, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 			fc_lport_recv(lport, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		fc_exch_release(ep);	/* release from lookup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 			     reject);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567)  * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568)  *			     end is the originator of the sequence that is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)  *			     response to our initial exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570)  * @mp: The EM that the exchange is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571)  * @fp: The response frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	struct fc_frame_header *fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	struct fc_seq *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	enum fc_sof sof;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	u32 f_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		atomic_inc(&mp->stats.xid_not_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	if (ep->esb_stat & ESB_ST_COMPLETE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		atomic_inc(&mp->stats.xid_not_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		goto rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	if (ep->rxid == FC_XID_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		ep->rxid = ntohs(fh->fh_rx_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		atomic_inc(&mp->stats.xid_not_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		goto rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	if (ep->did != ntoh24(fh->fh_s_id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	    ep->did != FC_FID_FLOGI) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		atomic_inc(&mp->stats.xid_not_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		goto rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	sof = fr_sof(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	sp = &ep->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	if (fc_sof_is_init(sof)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		sp->ssb_stat |= SSB_ST_RESP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 		sp->id = fh->fh_seq_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	f_ctl = ntoh24(fh->fh_f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	fr_seq(fp) = sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	if (f_ctl & FC_FC_SEQ_INIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		ep->esb_stat |= ESB_ST_SEQ_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	if (fc_sof_needs_ack(sof))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		fc_seq_send_ack(sp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	    (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	    (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		rc = fc_exch_done_locked(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		WARN_ON(fc_seq_exch(sp) != ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 			fc_exch_delete(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 			FC_EXCH_DBG(ep, "ep is completed already,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 					"hence skip calling the resp\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 			goto skip_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	 * Call the receive function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	 * The sequence is held (has a refcnt) for us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	 * but not for the receive function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	 * The receive function may allocate a new sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	 * over the old one, so we shouldn't change the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	 * sequence after this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	 * The frame will be freed by the receive function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	 * If new exch resp handler is valid then call that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	 * first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	if (!fc_invoke_resp(ep, sp, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) skip_resp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) rel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662)  * fc_exch_recv_resp() - Handler for a sequence where other end is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)  *			 responding to our sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664)  * @mp: The EM that the exchange is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665)  * @fp: The response frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	struct fc_seq *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	sp = fc_seq_lookup_orig(mp, fp);	/* doesn't hold sequence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	if (!sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		atomic_inc(&mp->stats.xid_not_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		atomic_inc(&mp->stats.non_bls_resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)  * fc_exch_abts_resp() - Handler for a response to an ABT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683)  * @ep: The exchange that the frame is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)  * @fp: The response frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)  * This response would be to an ABTS cancelling an exchange or sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)  * The response can be either BA_ACC or BA_RJT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	struct fc_frame_header *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	struct fc_ba_acc *ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	struct fc_seq *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	u16 low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	u16 high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	int rc = 1, has_rec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		    fc_exch_rctl_name(fh->fh_r_ctl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	if (cancel_delayed_work_sync(&ep->timeout_work)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		FC_EXCH_DBG(ep, "Exchange timer canceled due to ABTS response\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		fc_exch_release(ep);	/* release from pending timer hold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	switch (fh->fh_r_ctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	case FC_RCTL_BA_ACC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		ap = fc_frame_payload_get(fp, sizeof(*ap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		if (!ap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		 * Decide whether to establish a Recovery Qualifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		 * We do this if there is a non-empty SEQ_CNT range and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		 * SEQ_ID is the same as the one we aborted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		low = ntohs(ap->ba_low_seq_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		high = ntohs(ap->ba_high_seq_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		    (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		     ap->ba_seq_id == ep->seq_id) && low != high) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 			ep->esb_stat |= ESB_ST_REC_QUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 			fc_exch_hold(ep);  /* hold for recovery qualifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 			has_rec = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	case FC_RCTL_BA_RJT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	/* do we need to do some other checks here. Can we reuse more of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	 * fc_exch_recv_seq_resp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	sp = &ep->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	 * do we want to check END_SEQ as well as LAST_SEQ here?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	if (ep->fh_type != FC_TYPE_FCP &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	    ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		rc = fc_exch_done_locked(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	fc_exch_hold(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		fc_exch_delete(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	if (!fc_invoke_resp(ep, sp, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	if (has_rec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		fc_exch_timer_set(ep, ep->r_a_tov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758)  * fc_exch_recv_bls() - Handler for a BLS sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)  * @mp: The EM that the exchange is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760)  * @fp: The request frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762)  * The BLS frame is always a sequence initiated by the remote side.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763)  * We may be either the originator or recipient of the exchange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	struct fc_frame_header *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	u32 f_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	f_ctl = ntoh24(fh->fh_f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	fr_seq(fp) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 			  ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		ep->esb_stat |= ESB_ST_SEQ_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 		spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	if (f_ctl & FC_FC_SEQ_CTX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		 * A response to a sequence we initiated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		 * This should only be ACKs for class 2 or F.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		switch (fh->fh_r_ctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		case FC_RCTL_ACK_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		case FC_RCTL_ACK_0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 			if (ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 				FC_EXCH_DBG(ep, "BLS rctl %x - %s received\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 					    fh->fh_r_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 					    fc_exch_rctl_name(fh->fh_r_ctl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		switch (fh->fh_r_ctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		case FC_RCTL_BA_RJT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 		case FC_RCTL_BA_ACC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 			if (ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 				fc_exch_abts_resp(ep, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 				fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		case FC_RCTL_BA_ABTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 			if (ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 				fc_exch_recv_abts(ep, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 				fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		default:			/* ignore junk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 			fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	if (ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		fc_exch_release(ep);	/* release hold taken by fc_exch_find */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)  * fc_seq_ls_acc() - Accept sequence with LS_ACC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825)  * @rx_fp: The received frame, not freed here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)  * If this fails due to allocation or transmit congestion, assume the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828)  * originator will repeat the sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) static void fc_seq_ls_acc(struct fc_frame *rx_fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	struct fc_lport *lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	struct fc_els_ls_acc *acc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	struct fc_frame *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	struct fc_seq *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	lport = fr_dev(rx_fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	sp = fr_seq(rx_fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	fp = fc_frame_alloc(lport, sizeof(*acc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		FC_EXCH_DBG(fc_seq_exch(sp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 			    "exch: drop LS_ACC, out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	acc = fc_frame_payload_get(fp, sizeof(*acc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	memset(acc, 0, sizeof(*acc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	acc->la_cmd = ELS_LS_ACC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	lport->tt.frame_send(lport, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853)  * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854)  * @rx_fp: The received frame, not freed here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)  * @reason: The reason the sequence is being rejected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856)  * @explan: The explanation for the rejection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858)  * If this fails due to allocation or transmit congestion, assume the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859)  * originator will repeat the sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) static void fc_seq_ls_rjt(struct fc_frame *rx_fp, enum fc_els_rjt_reason reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 			  enum fc_els_rjt_explan explan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	struct fc_lport *lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	struct fc_els_ls_rjt *rjt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	struct fc_frame *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	struct fc_seq *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	lport = fr_dev(rx_fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	sp = fr_seq(rx_fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	fp = fc_frame_alloc(lport, sizeof(*rjt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		FC_EXCH_DBG(fc_seq_exch(sp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 			    "exch: drop LS_ACC, out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	rjt = fc_frame_payload_get(fp, sizeof(*rjt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	memset(rjt, 0, sizeof(*rjt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	rjt->er_cmd = ELS_LS_RJT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	rjt->er_reason = reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	rjt->er_explan = explan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	lport->tt.frame_send(lport, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)  * fc_exch_reset() - Reset an exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)  * @ep: The exchange to be reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890)  * Note: May sleep if invoked from outside a response handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) static void fc_exch_reset(struct fc_exch *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	struct fc_seq *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	int rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	ep->state |= FC_EX_RST_CLEANUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	fc_exch_timer_cancel(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	if (ep->esb_stat & ESB_ST_REC_QUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec_qual */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	ep->esb_stat &= ~ESB_ST_REC_QUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	sp = &ep->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	rc = fc_exch_done_locked(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	fc_exch_hold(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 		fc_exch_delete(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		FC_EXCH_DBG(ep, "ep is completed already,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 				"hence skip calling the resp\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 		goto skip_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	fc_invoke_resp(ep, sp, ERR_PTR(-FC_EX_CLOSED));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) skip_resp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	fc_seq_set_resp(sp, NULL, ep->arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924)  * fc_exch_pool_reset() - Reset a per cpu exchange pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)  * @lport: The local port that the exchange pool is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926)  * @pool:  The exchange pool to be reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927)  * @sid:   The source ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928)  * @did:   The destination ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)  * Resets a per cpu exches pool, releasing all of its sequences
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931)  * and exchanges. If sid is non-zero then reset only exchanges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932)  * we sourced from the local port's FID. If did is non-zero then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933)  * only reset exchanges destined for the local port's FID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) static void fc_exch_pool_reset(struct fc_lport *lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 			       struct fc_exch_pool *pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 			       u32 sid, u32 did)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	struct fc_exch *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	spin_lock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 		if ((lport == ep->lp) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 		    (sid == 0 || sid == ep->sid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		    (did == 0 || did == ep->did)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 			fc_exch_hold(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 			spin_unlock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 			fc_exch_reset(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 			fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 			spin_lock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 			 * must restart loop incase while lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 			 * was down multiple eps were released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 			goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	pool->next_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	pool->left = FC_XID_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	pool->right = FC_XID_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	spin_unlock_bh(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970)  * fc_exch_mgr_reset() - Reset all EMs of a local port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)  * @lport: The local port whose EMs are to be reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972)  * @sid:   The source ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973)  * @did:   The destination ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975)  * Reset all EMs associated with a given local port. Release all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976)  * sequences and exchanges. If sid is non-zero then reset only the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977)  * exchanges sent from the local port's FID. If did is non-zero then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978)  * reset only exchanges destined for the local port's FID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	struct fc_exch_mgr_anchor *ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		for_each_possible_cpu(cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 			fc_exch_pool_reset(lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 					   per_cpu_ptr(ema->mp->pool, cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 					   sid, did);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) EXPORT_SYMBOL(fc_exch_mgr_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995)  * fc_exch_lookup() - find an exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996)  * @lport: The local port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997)  * @xid: The exchange ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999)  * Returns exchange pointer with hold for caller, or NULL if not found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) static struct fc_exch *fc_exch_lookup(struct fc_lport *lport, u32 xid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	struct fc_exch_mgr_anchor *ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	list_for_each_entry(ema, &lport->ema_list, ema_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		if (ema->mp->min_xid <= xid && xid <= ema->mp->max_xid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 			return fc_exch_find(ema->mp, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012)  * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013)  * @rfp: The REC frame, not freed here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015)  * Note that the requesting port may be different than the S_ID in the request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) static void fc_exch_els_rec(struct fc_frame *rfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	struct fc_lport *lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	struct fc_frame *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	struct fc_els_rec *rp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	struct fc_els_rec_acc *acc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	enum fc_els_rjt_explan explan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	u32 sid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	u16 xid, rxid, oxid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	lport = fr_dev(rfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	rp = fc_frame_payload_get(rfp, sizeof(*rp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	explan = ELS_EXPL_INV_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	if (!rp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	sid = ntoh24(rp->rec_s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	rxid = ntohs(rp->rec_rx_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	oxid = ntohs(rp->rec_ox_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	explan = ELS_EXPL_OXID_RXID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	if (sid == fc_host_port_id(lport->host))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		xid = oxid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		xid = rxid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	if (xid == FC_XID_UNKNOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		FC_LPORT_DBG(lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 			     "REC request from %x: invalid rxid %x oxid %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 			     sid, rxid, oxid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 		goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	ep = fc_exch_lookup(lport, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		FC_LPORT_DBG(lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 			     "REC request from %x: rxid %x oxid %x not found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 			     sid, rxid, oxid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	FC_EXCH_DBG(ep, "REC request from %x: rxid %x oxid %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		    sid, rxid, oxid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	if (ep->oid != sid || oxid != ep->oxid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 		goto rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	if (rxid != FC_XID_UNKNOWN && rxid != ep->rxid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 		goto rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	fp = fc_frame_alloc(lport, sizeof(*acc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		FC_EXCH_DBG(ep, "Drop REC request, out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	acc = fc_frame_payload_get(fp, sizeof(*acc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	memset(acc, 0, sizeof(*acc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	acc->reca_cmd = ELS_LS_ACC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	acc->reca_ox_id = rp->rec_ox_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	memcpy(acc->reca_ofid, rp->rec_s_id, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	acc->reca_rx_id = htons(ep->rxid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	if (ep->sid == ep->oid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		hton24(acc->reca_rfid, ep->did);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		hton24(acc->reca_rfid, ep->sid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	acc->reca_fc4value = htonl(ep->seq.rec_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 						 ESB_ST_SEQ_INIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 						 ESB_ST_COMPLETE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	fc_fill_reply_hdr(fp, rfp, FC_RCTL_ELS_REP, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	lport->tt.frame_send(lport, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 	fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) rel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) reject:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	fc_seq_ls_rjt(rfp, reason, explan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095)  * fc_exch_rrq_resp() - Handler for RRQ responses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096)  * @sp:	 The sequence that the RRQ is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097)  * @fp:	 The RRQ frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098)  * @arg: The exchange that the RRQ is on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100)  * TODO: fix error handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	struct fc_exch *aborted_ep = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	unsigned int op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	if (IS_ERR(fp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		int err = PTR_ERR(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 		if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 			    "frame error %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	op = fc_frame_payload_op(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	switch (op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	case ELS_LS_RJT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 		FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	case ELS_LS_ACC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 		FC_EXCH_DBG(aborted_ep, "unexpected response op %x for RRQ\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 			    op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	fc_exch_done(&aborted_ep->seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	/* drop hold for rec qual */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	fc_exch_release(aborted_ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140)  * fc_exch_seq_send() - Send a frame using a new exchange and sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141)  * @lport:	The local port to send the frame on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142)  * @fp:		The frame to be sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143)  * @resp:	The response handler for this request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144)  * @destructor: The destructor for the exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145)  * @arg:	The argument to be passed to the response handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146)  * @timer_msec: The timeout period for the exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148)  * The exchange response handler is set in this routine to resp()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149)  * function pointer. It can be called in two scenarios: if a timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)  * occurs or if a response frame is received for the exchange. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151)  * fc_frame pointer in response handler will also indicate timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152)  * as error using IS_ERR related macros.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154)  * The exchange destructor handler is also set in this routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155)  * The destructor handler is invoked by EM layer when exchange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156)  * is about to free, this can be used by caller to free its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157)  * resources along with exchange free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159)  * The arg is passed back to resp and destructor handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161)  * The timeout value (in msec) for an exchange is set if non zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162)  * timer_msec argument is specified. The timer is canceled when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163)  * it fires or when the exchange is done. The exchange timeout handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164)  * is registered by EM layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166)  * The frame pointer with some of the header's fields must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167)  * filled before calling this routine, those fields are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169)  * - routing control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170)  * - FC port did
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171)  * - FC port sid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172)  * - FC header type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173)  * - frame control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174)  * - parameter or relative offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 				struct fc_frame *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 				void (*resp)(struct fc_seq *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 					     struct fc_frame *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 					     void *arg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 				void (*destructor)(struct fc_seq *, void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 				void *arg, u32 timer_msec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	struct fc_exch *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	struct fc_seq *sp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	struct fc_frame_header *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	struct fc_fcp_pkt *fsp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	int rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	ep = fc_exch_alloc(lport, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	ep->esb_stat |= ESB_ST_SEQ_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	ep->resp = resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	ep->destructor = destructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	ep->arg = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	ep->r_a_tov = lport->r_a_tov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	ep->lp = lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	sp = &ep->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	ep->f_ctl = ntoh24(fh->fh_f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	fc_exch_setup_hdr(ep, fp, ep->f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	sp->cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	if (ep->xid <= lport->lro_xid && fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 		fsp = fr_fsp(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 		fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	if (unlikely(lport->tt.frame_send(lport, fp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	if (timer_msec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 		fc_exch_timer_set_locked(ep, timer_msec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	ep->f_ctl &= ~FC_FC_FIRST_SEQ;	/* not first seq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	if (ep->f_ctl & FC_FC_SEQ_INIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 		ep->esb_stat &= ~ESB_ST_SEQ_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	return sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	if (fsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 		fc_fcp_ddp_done(fsp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	rc = fc_exch_done_locked(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 		fc_exch_delete(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) EXPORT_SYMBOL(fc_exch_seq_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238)  * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239)  * @ep: The exchange to send the RRQ on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241)  * This tells the remote port to stop blocking the use of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242)  * the exchange and the seq_cnt range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) static void fc_exch_rrq(struct fc_exch *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	struct fc_lport *lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 	struct fc_els_rrq *rrq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	struct fc_frame *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	u32 did;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	lport = ep->lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	fp = fc_frame_alloc(lport, sizeof(*rrq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	rrq = fc_frame_payload_get(fp, sizeof(*rrq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	memset(rrq, 0, sizeof(*rrq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	rrq->rrq_cmd = ELS_RRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	hton24(rrq->rrq_s_id, ep->sid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 	rrq->rrq_ox_id = htons(ep->oxid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	rrq->rrq_rx_id = htons(ep->rxid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 	did = ep->did;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 	if (ep->esb_stat & ESB_ST_RESP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		did = ep->sid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		       lport->port_id, FC_TYPE_ELS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 		       FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 	if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 			     lport->e_d_tov))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 	FC_EXCH_DBG(ep, "exch: RRQ send failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 		spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 		/* drop hold for rec qual */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 		fc_exch_release(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	ep->esb_stat |= ESB_ST_REC_QUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	fc_exch_timer_set_locked(ep, ep->r_a_tov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291)  * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292)  * @fp: The RRQ frame, not freed here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) static void fc_exch_els_rrq(struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	struct fc_lport *lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	struct fc_exch *ep = NULL;	/* request or subject exchange */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	struct fc_els_rrq *rp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	u32 sid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	u16 xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	enum fc_els_rjt_explan explan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	lport = fr_dev(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	rp = fc_frame_payload_get(fp, sizeof(*rp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 	explan = ELS_EXPL_INV_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	if (!rp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 		goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	 * lookup subject exchange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 	sid = ntoh24(rp->rrq_s_id);		/* subject source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	xid = fc_host_port_id(lport->host) == sid ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 			ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 	ep = fc_exch_lookup(lport, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	explan = ELS_EXPL_OXID_RXID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 		goto reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	spin_lock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	FC_EXCH_DBG(ep, "RRQ request from %x: xid %x rxid %x oxid %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 		    sid, xid, ntohs(rp->rrq_rx_id), ntohs(rp->rrq_ox_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	if (ep->oxid != ntohs(rp->rrq_ox_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 		goto unlock_reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	if (ep->rxid != ntohs(rp->rrq_rx_id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	    ep->rxid != FC_XID_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 		goto unlock_reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	explan = ELS_EXPL_SID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 	if (ep->sid != sid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 		goto unlock_reject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	 * Clear Recovery Qualifier state, and cancel timer if complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	if (ep->esb_stat & ESB_ST_REC_QUAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 		ep->esb_stat &= ~ESB_ST_REC_QUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 		atomic_dec(&ep->ex_refcnt);	/* drop hold for rec qual */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	if (ep->esb_stat & ESB_ST_COMPLETE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 		fc_exch_timer_cancel(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	 * Send LS_ACC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	fc_seq_ls_acc(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) unlock_reject:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	spin_unlock_bh(&ep->ex_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) reject:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 	fc_seq_ls_rjt(fp, ELS_RJT_LOGIC, explan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 	if (ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 		fc_exch_release(ep);	/* drop hold from fc_exch_find */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359)  * fc_exch_update_stats() - update exches stats to lport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360)  * @lport: The local port to update exchange manager stats
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) void fc_exch_update_stats(struct fc_lport *lport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	struct fc_host_statistics *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	struct fc_exch_mgr_anchor *ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	struct fc_exch_mgr *mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	st = &lport->host_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 		mp = ema->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 		st->fc_no_free_exch += atomic_read(&mp->stats.no_free_exch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 		st->fc_no_free_exch_xid +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 				atomic_read(&mp->stats.no_free_exch_xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		st->fc_xid_not_found += atomic_read(&mp->stats.xid_not_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		st->fc_xid_busy += atomic_read(&mp->stats.xid_busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 		st->fc_seq_not_found += atomic_read(&mp->stats.seq_not_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 		st->fc_non_bls_resp += atomic_read(&mp->stats.non_bls_resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) EXPORT_SYMBOL(fc_exch_update_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384)  * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385)  * @lport: The local port to add the exchange manager to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386)  * @mp:	   The exchange manager to be added to the local port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387)  * @match: The match routine that indicates when this EM should be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 					   struct fc_exch_mgr *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 					   bool (*match)(struct fc_frame *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	struct fc_exch_mgr_anchor *ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 	ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	if (!ema)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		return ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	ema->mp = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 	ema->match = match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 	/* add EM anchor to EM anchors list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 	list_add_tail(&ema->ema_list, &lport->ema_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 	kref_get(&mp->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	return ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) EXPORT_SYMBOL(fc_exch_mgr_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409)  * fc_exch_mgr_destroy() - Destroy an exchange manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410)  * @kref: The reference to the EM to be destroyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) static void fc_exch_mgr_destroy(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	mempool_destroy(mp->ep_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	free_percpu(mp->pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	kfree(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422)  * fc_exch_mgr_del() - Delete an EM from a local port's list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423)  * @ema: The exchange manager anchor identifying the EM to be deleted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	/* remove EM anchor from EM anchors list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 	list_del(&ema->ema_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 	kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	kfree(ema);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) EXPORT_SYMBOL(fc_exch_mgr_del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435)  * fc_exch_mgr_list_clone() - Share all exchange manager objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436)  * @src: Source lport to clone exchange managers from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437)  * @dst: New lport that takes references to all the exchange managers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	struct fc_exch_mgr_anchor *ema, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	list_for_each_entry(ema, &src->ema_list, ema_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 		if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 	list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 		fc_exch_mgr_del(ema);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) EXPORT_SYMBOL(fc_exch_mgr_list_clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456)  * fc_exch_mgr_alloc() - Allocate an exchange manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457)  * @lport:   The local port that the new EM will be associated with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458)  * @class:   The default FC class for new exchanges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459)  * @min_xid: The minimum XID for exchanges from the new EM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460)  * @max_xid: The maximum XID for exchanges from the new EM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461)  * @match:   The match routine for the new EM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 				      enum fc_class class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 				      u16 min_xid, u16 max_xid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 				      bool (*match)(struct fc_frame *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	struct fc_exch_mgr *mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	u16 pool_exch_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 	size_t pool_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	struct fc_exch_pool *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	    (min_xid & fc_cpu_mask) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 		FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 			     min_xid, max_xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	 * allocate memory for EM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	if (!mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	mp->class = class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	mp->lport = lport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	/* adjust em exch xid range for offload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	mp->min_xid = min_xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493)        /* reduce range so per cpu pool fits into PCPU_MIN_UNIT_SIZE pool */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	pool_exch_range = (PCPU_MIN_UNIT_SIZE - sizeof(*pool)) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 		sizeof(struct fc_exch *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	if ((max_xid - min_xid + 1) / (fc_cpu_mask + 1) > pool_exch_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 		mp->max_xid = pool_exch_range * (fc_cpu_mask + 1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 			min_xid - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 		mp->max_xid = max_xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 		pool_exch_range = (mp->max_xid - mp->min_xid + 1) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 			(fc_cpu_mask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 	if (!mp->ep_pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 		goto free_mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	 * Setup per cpu exch pool with entire exchange id range equally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	 * divided across all cpus. The exch pointers array memory is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	 * allocated for exch range per pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 	mp->pool_max_index = pool_exch_range - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	 * Allocate and initialize per cpu exch pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 	pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 	mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 	if (!mp->pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 		goto free_mempool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 		pool = per_cpu_ptr(mp->pool, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 		pool->next_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 		pool->left = FC_XID_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 		pool->right = FC_XID_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 		spin_lock_init(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 		INIT_LIST_HEAD(&pool->ex_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	kref_init(&mp->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	if (!fc_exch_mgr_add(lport, mp, match)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 		free_percpu(mp->pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 		goto free_mempool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	 * Above kref_init() sets mp->kref to 1 and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	 * call to fc_exch_mgr_add incremented mp->kref again,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 	 * so adjust that extra increment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	kref_put(&mp->kref, fc_exch_mgr_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	return mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) free_mempool:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	mempool_destroy(mp->ep_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) free_mp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	kfree(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) EXPORT_SYMBOL(fc_exch_mgr_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555)  * fc_exch_mgr_free() - Free all exchange managers on a local port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556)  * @lport: The local port whose EMs are to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) void fc_exch_mgr_free(struct fc_lport *lport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 	struct fc_exch_mgr_anchor *ema, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	flush_workqueue(fc_exch_workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 		fc_exch_mgr_del(ema);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) EXPORT_SYMBOL(fc_exch_mgr_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569)  * fc_find_ema() - Lookup and return appropriate Exchange Manager Anchor depending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570)  * upon 'xid'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571)  * @f_ctl: f_ctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572)  * @lport: The local port the frame was received on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573)  * @fh: The received frame header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) static struct fc_exch_mgr_anchor *fc_find_ema(u32 f_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 					      struct fc_lport *lport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 					      struct fc_frame_header *fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 	struct fc_exch_mgr_anchor *ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	u16 xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 	if (f_ctl & FC_FC_EX_CTX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 		xid = ntohs(fh->fh_ox_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 		xid = ntohs(fh->fh_rx_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 		if (xid == FC_XID_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 			return list_entry(lport->ema_list.prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 					  typeof(*ema), ema_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	list_for_each_entry(ema, &lport->ema_list, ema_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 		if ((xid >= ema->mp->min_xid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 		    (xid <= ema->mp->max_xid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 			return ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599)  * fc_exch_recv() - Handler for received frames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600)  * @lport: The local port the frame was received on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601)  * @fp:	The received frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	struct fc_frame_header *fh = fc_frame_header_get(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	struct fc_exch_mgr_anchor *ema;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	u32 f_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 	/* lport lock ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	if (!lport || lport->state == LPORT_ST_DISABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 		FC_LIBFC_DBG("Receiving frames for an lport that "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 			     "has not been initialized correctly\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	f_ctl = ntoh24(fh->fh_f_ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 	ema = fc_find_ema(f_ctl, lport, fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 	if (!ema) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 		FC_LPORT_DBG(lport, "Unable to find Exchange Manager Anchor,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 				    "fc_ctl <0x%x>, xid <0x%x>\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 				     f_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 				     (f_ctl & FC_FC_EX_CTX) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 				     ntohs(fh->fh_ox_id) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 				     ntohs(fh->fh_rx_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	 * If frame is marked invalid, just drop it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 	switch (fr_eof(fp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	case FC_EOF_T:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 		if (f_ctl & FC_FC_END_SEQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 			skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 	case FC_EOF_N:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 		if (fh->fh_type == FC_TYPE_BLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 			fc_exch_recv_bls(ema->mp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 		else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 			 FC_FC_EX_CTX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 			fc_exch_recv_seq_resp(ema->mp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 		else if (f_ctl & FC_FC_SEQ_CTX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 			fc_exch_recv_resp(ema->mp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 		else	/* no EX_CTX and no SEQ_CTX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 			fc_exch_recv_req(lport, ema->mp, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 		FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 			     fr_eof(fp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 		fc_frame_free(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) EXPORT_SYMBOL(fc_exch_recv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658)  * fc_exch_init() - Initialize the exchange layer for a local port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659)  * @lport: The local port to initialize the exchange layer for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) int fc_exch_init(struct fc_lport *lport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 	if (!lport->tt.exch_mgr_reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 		lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) EXPORT_SYMBOL(fc_exch_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671)  * fc_setup_exch_mgr() - Setup an exchange manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) int fc_setup_exch_mgr(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 	fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 					 0, SLAB_HWCACHE_ALIGN, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 	if (!fc_em_cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 	 * Initialize fc_cpu_mask and fc_cpu_order. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 	 * fc_cpu_mask is set for nr_cpu_ids rounded up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	 * to order of 2's * power and order is stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	 * in fc_cpu_order as this is later required in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 	 * mapping between an exch id and exch array index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	 * in per cpu exch pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	 * This round up is required to align fc_cpu_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 	 * to exchange id's lower bits such that all incoming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	 * frames of an exchange gets delivered to the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 	 * cpu on which exchange originated by simple bitwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 	 * AND operation between fc_cpu_mask and exchange id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	fc_cpu_order = ilog2(roundup_pow_of_two(nr_cpu_ids));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	fc_cpu_mask = (1 << fc_cpu_order) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	fc_exch_workqueue = create_singlethread_workqueue("fc_exch_workqueue");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	if (!fc_exch_workqueue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 	kmem_cache_destroy(fc_em_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707)  * fc_destroy_exch_mgr() - Destroy an exchange manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) void fc_destroy_exch_mgr(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	destroy_workqueue(fc_exch_workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	kmem_cache_destroy(fc_em_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) }