142e9a92fSRobert Love /* 242e9a92fSRobert Love * Copyright(c) 2007 Intel Corporation. All rights reserved. 342e9a92fSRobert Love * Copyright(c) 2008 Red Hat, Inc. All rights reserved. 442e9a92fSRobert Love * Copyright(c) 2008 Mike Christie 542e9a92fSRobert Love * 642e9a92fSRobert Love * This program is free software; you can redistribute it and/or modify it 742e9a92fSRobert Love * under the terms and conditions of the GNU General Public License, 842e9a92fSRobert Love * version 2, as published by the Free Software Foundation. 942e9a92fSRobert Love * 1042e9a92fSRobert Love * This program is distributed in the hope it will be useful, but WITHOUT 1142e9a92fSRobert Love * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1242e9a92fSRobert Love * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1342e9a92fSRobert Love * more details. 1442e9a92fSRobert Love * 1542e9a92fSRobert Love * You should have received a copy of the GNU General Public License along with 1642e9a92fSRobert Love * this program; if not, write to the Free Software Foundation, Inc., 1742e9a92fSRobert Love * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 1842e9a92fSRobert Love * 1942e9a92fSRobert Love * Maintained at www.Open-FCoE.org 2042e9a92fSRobert Love */ 2142e9a92fSRobert Love 2242e9a92fSRobert Love /* 2342e9a92fSRobert Love * Fibre Channel exchange and sequence handling. 2442e9a92fSRobert Love */ 2542e9a92fSRobert Love 2642e9a92fSRobert Love #include <linux/timer.h> 2742e9a92fSRobert Love #include <linux/gfp.h> 2842e9a92fSRobert Love #include <linux/err.h> 2942e9a92fSRobert Love 3042e9a92fSRobert Love #include <scsi/fc/fc_fc2.h> 3142e9a92fSRobert Love 3242e9a92fSRobert Love #include <scsi/libfc.h> 3342e9a92fSRobert Love #include <scsi/fc_encode.h> 3442e9a92fSRobert Love 358866a5d9SRobert Love #include "fc_libfc.h" 368866a5d9SRobert Love 37e4bc50beSVasu Dev u16 fc_cpu_mask; /* cpu mask for possible cpus */ 38e4bc50beSVasu Dev EXPORT_SYMBOL(fc_cpu_mask); 39e4bc50beSVasu Dev static u16 fc_cpu_order; /* 2's power to represent total possible cpus */ 4042e9a92fSRobert Love static struct kmem_cache *fc_em_cachep; /* cache for exchanges */ 4142e9a92fSRobert Love 4242e9a92fSRobert Love /* 4342e9a92fSRobert Love * Structure and function definitions for managing Fibre Channel Exchanges 4442e9a92fSRobert Love * and Sequences. 4542e9a92fSRobert Love * 4642e9a92fSRobert Love * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq. 4742e9a92fSRobert Love * 4842e9a92fSRobert Love * fc_exch_mgr holds the exchange state for an N port 4942e9a92fSRobert Love * 5042e9a92fSRobert Love * fc_exch holds state for one exchange and links to its active sequence. 5142e9a92fSRobert Love * 5242e9a92fSRobert Love * fc_seq holds the state for an individual sequence. 5342e9a92fSRobert Love */ 5442e9a92fSRobert Love 5542e9a92fSRobert Love /* 56e4bc50beSVasu Dev * Per cpu exchange pool 57e4bc50beSVasu Dev * 58e4bc50beSVasu Dev * This structure manages per cpu exchanges in array of exchange pointers. 59e4bc50beSVasu Dev * This array is allocated followed by struct fc_exch_pool memory for 60e4bc50beSVasu Dev * assigned range of exchanges to per cpu pool. 61e4bc50beSVasu Dev */ 62e4bc50beSVasu Dev struct fc_exch_pool { 63e4bc50beSVasu Dev u16 next_index; /* next possible free exchange index */ 64e4bc50beSVasu Dev u16 total_exches; /* total allocated exchanges */ 65e4bc50beSVasu Dev spinlock_t lock; /* exch pool lock */ 66e4bc50beSVasu Dev struct list_head ex_list; /* allocated exchanges list */ 67e4bc50beSVasu Dev }; 68e4bc50beSVasu Dev 69e4bc50beSVasu Dev /* 7042e9a92fSRobert Love * Exchange manager. 7142e9a92fSRobert Love * 7242e9a92fSRobert Love * This structure is the center for creating exchanges and sequences. 7342e9a92fSRobert Love * It manages the allocation of exchange IDs. 7442e9a92fSRobert Love */ 7542e9a92fSRobert Love struct fc_exch_mgr { 7642e9a92fSRobert Love enum fc_class class; /* default class for sequences */ 7796316099SVasu Dev struct kref kref; /* exchange mgr reference count */ 7842e9a92fSRobert Love u16 min_xid; /* min exchange ID */ 7942e9a92fSRobert Love u16 max_xid; /* max exchange ID */ 8042e9a92fSRobert Love mempool_t *ep_pool; /* reserve ep's */ 81e4bc50beSVasu Dev u16 pool_max_index; /* max exch array index in exch pool */ 82e4bc50beSVasu Dev struct fc_exch_pool *pool; /* per cpu exch pool */ 8342e9a92fSRobert Love 8442e9a92fSRobert Love /* 8542e9a92fSRobert Love * currently exchange mgr stats are updated but not used. 8642e9a92fSRobert Love * either stats can be expose via sysfs or remove them 8742e9a92fSRobert Love * all together if not used XXX 8842e9a92fSRobert Love */ 8942e9a92fSRobert Love struct { 9042e9a92fSRobert Love atomic_t no_free_exch; 9142e9a92fSRobert Love atomic_t no_free_exch_xid; 9242e9a92fSRobert Love atomic_t xid_not_found; 9342e9a92fSRobert Love atomic_t xid_busy; 9442e9a92fSRobert Love atomic_t seq_not_found; 9542e9a92fSRobert Love atomic_t non_bls_resp; 9642e9a92fSRobert Love } stats; 9742e9a92fSRobert Love }; 9842e9a92fSRobert Love #define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq) 9942e9a92fSRobert Love 10096316099SVasu Dev struct fc_exch_mgr_anchor { 10196316099SVasu Dev struct list_head ema_list; 10296316099SVasu Dev struct fc_exch_mgr *mp; 10396316099SVasu Dev bool (*match)(struct fc_frame *); 10496316099SVasu Dev }; 10596316099SVasu Dev 10642e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *); 10742e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *); 10842e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason, 10942e9a92fSRobert Love enum fc_els_rjt_explan); 11042e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *); 11142e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *); 11242e9a92fSRobert Love 11342e9a92fSRobert Love /* 11442e9a92fSRobert Love * Internal implementation notes. 11542e9a92fSRobert Love * 11642e9a92fSRobert Love * The exchange manager is one by default in libfc but LLD may choose 11742e9a92fSRobert Love * to have one per CPU. The sequence manager is one per exchange manager 11842e9a92fSRobert Love * and currently never separated. 11942e9a92fSRobert Love * 12042e9a92fSRobert Love * Section 9.8 in FC-FS-2 specifies: "The SEQ_ID is a one-byte field 12142e9a92fSRobert Love * assigned by the Sequence Initiator that shall be unique for a specific 12242e9a92fSRobert Love * D_ID and S_ID pair while the Sequence is open." Note that it isn't 12342e9a92fSRobert Love * qualified by exchange ID, which one might think it would be. 12442e9a92fSRobert Love * In practice this limits the number of open sequences and exchanges to 256 12542e9a92fSRobert Love * per session. For most targets we could treat this limit as per exchange. 12642e9a92fSRobert Love * 12742e9a92fSRobert Love * The exchange and its sequence are freed when the last sequence is received. 12842e9a92fSRobert Love * It's possible for the remote port to leave an exchange open without 12942e9a92fSRobert Love * sending any sequences. 13042e9a92fSRobert Love * 13142e9a92fSRobert Love * Notes on reference counts: 13242e9a92fSRobert Love * 13342e9a92fSRobert Love * Exchanges are reference counted and exchange gets freed when the reference 13442e9a92fSRobert Love * count becomes zero. 13542e9a92fSRobert Love * 13642e9a92fSRobert Love * Timeouts: 13742e9a92fSRobert Love * Sequences are timed out for E_D_TOV and R_A_TOV. 13842e9a92fSRobert Love * 13942e9a92fSRobert Love * Sequence event handling: 14042e9a92fSRobert Love * 14142e9a92fSRobert Love * The following events may occur on initiator sequences: 14242e9a92fSRobert Love * 14342e9a92fSRobert Love * Send. 14442e9a92fSRobert Love * For now, the whole thing is sent. 14542e9a92fSRobert Love * Receive ACK 14642e9a92fSRobert Love * This applies only to class F. 14742e9a92fSRobert Love * The sequence is marked complete. 14842e9a92fSRobert Love * ULP completion. 14942e9a92fSRobert Love * The upper layer calls fc_exch_done() when done 15042e9a92fSRobert Love * with exchange and sequence tuple. 15142e9a92fSRobert Love * RX-inferred completion. 15242e9a92fSRobert Love * When we receive the next sequence on the same exchange, we can 15342e9a92fSRobert Love * retire the previous sequence ID. (XXX not implemented). 15442e9a92fSRobert Love * Timeout. 15542e9a92fSRobert Love * R_A_TOV frees the sequence ID. If we're waiting for ACK, 15642e9a92fSRobert Love * E_D_TOV causes abort and calls upper layer response handler 15742e9a92fSRobert Love * with FC_EX_TIMEOUT error. 15842e9a92fSRobert Love * Receive RJT 15942e9a92fSRobert Love * XXX defer. 16042e9a92fSRobert Love * Send ABTS 16142e9a92fSRobert Love * On timeout. 16242e9a92fSRobert Love * 16342e9a92fSRobert Love * The following events may occur on recipient sequences: 16442e9a92fSRobert Love * 16542e9a92fSRobert Love * Receive 16642e9a92fSRobert Love * Allocate sequence for first frame received. 16742e9a92fSRobert Love * Hold during receive handler. 16842e9a92fSRobert Love * Release when final frame received. 16942e9a92fSRobert Love * Keep status of last N of these for the ELS RES command. XXX TBD. 17042e9a92fSRobert Love * Receive ABTS 17142e9a92fSRobert Love * Deallocate sequence 17242e9a92fSRobert Love * Send RJT 17342e9a92fSRobert Love * Deallocate 17442e9a92fSRobert Love * 17542e9a92fSRobert Love * For now, we neglect conditions where only part of a sequence was 17642e9a92fSRobert Love * received or transmitted, or where out-of-order receipt is detected. 17742e9a92fSRobert Love */ 17842e9a92fSRobert Love 17942e9a92fSRobert Love /* 18042e9a92fSRobert Love * Locking notes: 18142e9a92fSRobert Love * 18242e9a92fSRobert Love * The EM code run in a per-CPU worker thread. 18342e9a92fSRobert Love * 18442e9a92fSRobert Love * To protect against concurrency between a worker thread code and timers, 18542e9a92fSRobert Love * sequence allocation and deallocation must be locked. 18642e9a92fSRobert Love * - exchange refcnt can be done atomicly without locks. 18742e9a92fSRobert Love * - sequence allocation must be locked by exch lock. 188b2f0091fSVasu Dev * - If the EM pool lock and ex_lock must be taken at the same time, then the 189b2f0091fSVasu Dev * EM pool lock must be taken before the ex_lock. 19042e9a92fSRobert Love */ 19142e9a92fSRobert Love 19242e9a92fSRobert Love /* 19342e9a92fSRobert Love * opcode names for debugging. 19442e9a92fSRobert Love */ 19542e9a92fSRobert Love static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT; 19642e9a92fSRobert Love 19742e9a92fSRobert Love #define FC_TABLE_SIZE(x) (sizeof(x) / sizeof(x[0])) 19842e9a92fSRobert Love 19942e9a92fSRobert Love static inline const char *fc_exch_name_lookup(unsigned int op, char **table, 20042e9a92fSRobert Love unsigned int max_index) 20142e9a92fSRobert Love { 20242e9a92fSRobert Love const char *name = NULL; 20342e9a92fSRobert Love 20442e9a92fSRobert Love if (op < max_index) 20542e9a92fSRobert Love name = table[op]; 20642e9a92fSRobert Love if (!name) 20742e9a92fSRobert Love name = "unknown"; 20842e9a92fSRobert Love return name; 20942e9a92fSRobert Love } 21042e9a92fSRobert Love 21142e9a92fSRobert Love static const char *fc_exch_rctl_name(unsigned int op) 21242e9a92fSRobert Love { 21342e9a92fSRobert Love return fc_exch_name_lookup(op, fc_exch_rctl_names, 21442e9a92fSRobert Love FC_TABLE_SIZE(fc_exch_rctl_names)); 21542e9a92fSRobert Love } 21642e9a92fSRobert Love 21742e9a92fSRobert Love /* 21842e9a92fSRobert Love * Hold an exchange - keep it from being freed. 21942e9a92fSRobert Love */ 22042e9a92fSRobert Love static void fc_exch_hold(struct fc_exch *ep) 22142e9a92fSRobert Love { 22242e9a92fSRobert Love atomic_inc(&ep->ex_refcnt); 22342e9a92fSRobert Love } 22442e9a92fSRobert Love 22542e9a92fSRobert Love /* 22642e9a92fSRobert Love * setup fc hdr by initializing few more FC header fields and sof/eof. 22742e9a92fSRobert Love * Initialized fields by this func: 22842e9a92fSRobert Love * - fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt 22942e9a92fSRobert Love * - sof and eof 23042e9a92fSRobert Love */ 23142e9a92fSRobert Love static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp, 23242e9a92fSRobert Love u32 f_ctl) 23342e9a92fSRobert Love { 23442e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 23542e9a92fSRobert Love u16 fill; 23642e9a92fSRobert Love 23742e9a92fSRobert Love fr_sof(fp) = ep->class; 23842e9a92fSRobert Love if (ep->seq.cnt) 23942e9a92fSRobert Love fr_sof(fp) = fc_sof_normal(ep->class); 24042e9a92fSRobert Love 24142e9a92fSRobert Love if (f_ctl & FC_FC_END_SEQ) { 24242e9a92fSRobert Love fr_eof(fp) = FC_EOF_T; 24342e9a92fSRobert Love if (fc_sof_needs_ack(ep->class)) 24442e9a92fSRobert Love fr_eof(fp) = FC_EOF_N; 24542e9a92fSRobert Love /* 24642e9a92fSRobert Love * Form f_ctl. 24742e9a92fSRobert Love * The number of fill bytes to make the length a 4-byte 24842e9a92fSRobert Love * multiple is the low order 2-bits of the f_ctl. 24942e9a92fSRobert Love * The fill itself will have been cleared by the frame 25042e9a92fSRobert Love * allocation. 25142e9a92fSRobert Love * After this, the length will be even, as expected by 25242e9a92fSRobert Love * the transport. 25342e9a92fSRobert Love */ 25442e9a92fSRobert Love fill = fr_len(fp) & 3; 25542e9a92fSRobert Love if (fill) { 25642e9a92fSRobert Love fill = 4 - fill; 25742e9a92fSRobert Love /* TODO, this may be a problem with fragmented skb */ 25842e9a92fSRobert Love skb_put(fp_skb(fp), fill); 25942e9a92fSRobert Love hton24(fh->fh_f_ctl, f_ctl | fill); 26042e9a92fSRobert Love } 26142e9a92fSRobert Love } else { 26242e9a92fSRobert Love WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */ 26342e9a92fSRobert Love fr_eof(fp) = FC_EOF_N; 26442e9a92fSRobert Love } 26542e9a92fSRobert Love 26642e9a92fSRobert Love /* 26742e9a92fSRobert Love * Initialize remainig fh fields 26842e9a92fSRobert Love * from fc_fill_fc_hdr 26942e9a92fSRobert Love */ 27042e9a92fSRobert Love fh->fh_ox_id = htons(ep->oxid); 27142e9a92fSRobert Love fh->fh_rx_id = htons(ep->rxid); 27242e9a92fSRobert Love fh->fh_seq_id = ep->seq.id; 27342e9a92fSRobert Love fh->fh_seq_cnt = htons(ep->seq.cnt); 27442e9a92fSRobert Love } 27542e9a92fSRobert Love 27642e9a92fSRobert Love /* 27742e9a92fSRobert Love * Release a reference to an exchange. 27842e9a92fSRobert Love * If the refcnt goes to zero and the exchange is complete, it is freed. 27942e9a92fSRobert Love */ 28042e9a92fSRobert Love static void fc_exch_release(struct fc_exch *ep) 28142e9a92fSRobert Love { 28242e9a92fSRobert Love struct fc_exch_mgr *mp; 28342e9a92fSRobert Love 28442e9a92fSRobert Love if (atomic_dec_and_test(&ep->ex_refcnt)) { 28542e9a92fSRobert Love mp = ep->em; 28642e9a92fSRobert Love if (ep->destructor) 28742e9a92fSRobert Love ep->destructor(&ep->seq, ep->arg); 288aa6cd29bSJulia Lawall WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE)); 28942e9a92fSRobert Love mempool_free(ep, mp->ep_pool); 29042e9a92fSRobert Love } 29142e9a92fSRobert Love } 29242e9a92fSRobert Love 29342e9a92fSRobert Love static int fc_exch_done_locked(struct fc_exch *ep) 29442e9a92fSRobert Love { 29542e9a92fSRobert Love int rc = 1; 29642e9a92fSRobert Love 29742e9a92fSRobert Love /* 29842e9a92fSRobert Love * We must check for completion in case there are two threads 29942e9a92fSRobert Love * tyring to complete this. But the rrq code will reuse the 30042e9a92fSRobert Love * ep, and in that case we only clear the resp and set it as 30142e9a92fSRobert Love * complete, so it can be reused by the timer to send the rrq. 30242e9a92fSRobert Love */ 30342e9a92fSRobert Love ep->resp = NULL; 30442e9a92fSRobert Love if (ep->state & FC_EX_DONE) 30542e9a92fSRobert Love return rc; 30642e9a92fSRobert Love ep->esb_stat |= ESB_ST_COMPLETE; 30742e9a92fSRobert Love 30842e9a92fSRobert Love if (!(ep->esb_stat & ESB_ST_REC_QUAL)) { 30942e9a92fSRobert Love ep->state |= FC_EX_DONE; 31042e9a92fSRobert Love if (cancel_delayed_work(&ep->timeout_work)) 31142e9a92fSRobert Love atomic_dec(&ep->ex_refcnt); /* drop hold for timer */ 31242e9a92fSRobert Love rc = 0; 31342e9a92fSRobert Love } 31442e9a92fSRobert Love return rc; 31542e9a92fSRobert Love } 31642e9a92fSRobert Love 317e4bc50beSVasu Dev static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool, 318e4bc50beSVasu Dev u16 index) 319e4bc50beSVasu Dev { 320e4bc50beSVasu Dev struct fc_exch **exches = (struct fc_exch **)(pool + 1); 321e4bc50beSVasu Dev return exches[index]; 322e4bc50beSVasu Dev } 323e4bc50beSVasu Dev 324e4bc50beSVasu Dev static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index, 325e4bc50beSVasu Dev struct fc_exch *ep) 326e4bc50beSVasu Dev { 327e4bc50beSVasu Dev ((struct fc_exch **)(pool + 1))[index] = ep; 328e4bc50beSVasu Dev } 329e4bc50beSVasu Dev 330b2f0091fSVasu Dev static void fc_exch_delete(struct fc_exch *ep) 33142e9a92fSRobert Love { 332b2f0091fSVasu Dev struct fc_exch_pool *pool; 33342e9a92fSRobert Love 334b2f0091fSVasu Dev pool = ep->pool; 335b2f0091fSVasu Dev spin_lock_bh(&pool->lock); 336b2f0091fSVasu Dev WARN_ON(pool->total_exches <= 0); 337b2f0091fSVasu Dev pool->total_exches--; 338b2f0091fSVasu Dev fc_exch_ptr_set(pool, (ep->xid - ep->em->min_xid) >> fc_cpu_order, 339b2f0091fSVasu Dev NULL); 34042e9a92fSRobert Love list_del(&ep->ex_list); 341b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 34242e9a92fSRobert Love fc_exch_release(ep); /* drop hold for exch in mp */ 34342e9a92fSRobert Love } 34442e9a92fSRobert Love 34542e9a92fSRobert Love /* 34642e9a92fSRobert Love * Internal version of fc_exch_timer_set - used with lock held. 34742e9a92fSRobert Love */ 34842e9a92fSRobert Love static inline void fc_exch_timer_set_locked(struct fc_exch *ep, 34942e9a92fSRobert Love unsigned int timer_msec) 35042e9a92fSRobert Love { 35142e9a92fSRobert Love if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) 35242e9a92fSRobert Love return; 35342e9a92fSRobert Love 354cd305ce4SRobert Love FC_EXCH_DBG(ep, "Exchange timer armed\n"); 3557414705eSRobert Love 35642e9a92fSRobert Love if (schedule_delayed_work(&ep->timeout_work, 35742e9a92fSRobert Love msecs_to_jiffies(timer_msec))) 35842e9a92fSRobert Love fc_exch_hold(ep); /* hold for timer */ 35942e9a92fSRobert Love } 36042e9a92fSRobert Love 36142e9a92fSRobert Love /* 36242e9a92fSRobert Love * Set timer for an exchange. 36342e9a92fSRobert Love * The time is a minimum delay in milliseconds until the timer fires. 36442e9a92fSRobert Love * Used for upper level protocols to time out the exchange. 36542e9a92fSRobert Love * The timer is cancelled when it fires or when the exchange completes. 36642e9a92fSRobert Love * Returns non-zero if a timer couldn't be allocated. 36742e9a92fSRobert Love */ 36842e9a92fSRobert Love static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec) 36942e9a92fSRobert Love { 37042e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 37142e9a92fSRobert Love fc_exch_timer_set_locked(ep, timer_msec); 37242e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 37342e9a92fSRobert Love } 37442e9a92fSRobert Love 3751a7b75aeSRobert Love /** 3761a7b75aeSRobert Love * send a frame using existing sequence and exchange. 3771a7b75aeSRobert Love */ 3781a7b75aeSRobert Love static int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, 3791a7b75aeSRobert Love struct fc_frame *fp) 3801a7b75aeSRobert Love { 3811a7b75aeSRobert Love struct fc_exch *ep; 3821a7b75aeSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 3831a7b75aeSRobert Love int error; 3841a7b75aeSRobert Love u32 f_ctl; 3851a7b75aeSRobert Love 3861a7b75aeSRobert Love ep = fc_seq_exch(sp); 3871a7b75aeSRobert Love WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT); 3881a7b75aeSRobert Love 3891a7b75aeSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 3901a7b75aeSRobert Love fc_exch_setup_hdr(ep, fp, f_ctl); 3911a7b75aeSRobert Love 3921a7b75aeSRobert Love /* 3931a7b75aeSRobert Love * update sequence count if this frame is carrying 3941a7b75aeSRobert Love * multiple FC frames when sequence offload is enabled 3951a7b75aeSRobert Love * by LLD. 3961a7b75aeSRobert Love */ 3971a7b75aeSRobert Love if (fr_max_payload(fp)) 3981a7b75aeSRobert Love sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)), 3991a7b75aeSRobert Love fr_max_payload(fp)); 4001a7b75aeSRobert Love else 4011a7b75aeSRobert Love sp->cnt++; 4021a7b75aeSRobert Love 4031a7b75aeSRobert Love /* 4041a7b75aeSRobert Love * Send the frame. 4051a7b75aeSRobert Love */ 4061a7b75aeSRobert Love error = lp->tt.frame_send(lp, fp); 4071a7b75aeSRobert Love 4081a7b75aeSRobert Love /* 4091a7b75aeSRobert Love * Update the exchange and sequence flags, 4101a7b75aeSRobert Love * assuming all frames for the sequence have been sent. 4111a7b75aeSRobert Love * We can only be called to send once for each sequence. 4121a7b75aeSRobert Love */ 4131a7b75aeSRobert Love spin_lock_bh(&ep->ex_lock); 4141a7b75aeSRobert Love ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */ 4151a7b75aeSRobert Love if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT)) 4161a7b75aeSRobert Love ep->esb_stat &= ~ESB_ST_SEQ_INIT; 4171a7b75aeSRobert Love spin_unlock_bh(&ep->ex_lock); 4181a7b75aeSRobert Love return error; 4191a7b75aeSRobert Love } 4201a7b75aeSRobert Love 4211a7b75aeSRobert Love /** 4221a7b75aeSRobert Love * fc_seq_alloc() - Allocate a sequence. 4231a7b75aeSRobert Love * @ep: Exchange pointer 4241a7b75aeSRobert Love * @seq_id: Sequence ID to allocate a sequence for 4251a7b75aeSRobert Love * 4261a7b75aeSRobert Love * We don't support multiple originated sequences on the same exchange. 4271a7b75aeSRobert Love * By implication, any previously originated sequence on this exchange 4281a7b75aeSRobert Love * is complete, and we reallocate the same sequence. 4291a7b75aeSRobert Love */ 4301a7b75aeSRobert Love static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id) 4311a7b75aeSRobert Love { 4321a7b75aeSRobert Love struct fc_seq *sp; 4331a7b75aeSRobert Love 4341a7b75aeSRobert Love sp = &ep->seq; 4351a7b75aeSRobert Love sp->ssb_stat = 0; 4361a7b75aeSRobert Love sp->cnt = 0; 4371a7b75aeSRobert Love sp->id = seq_id; 4381a7b75aeSRobert Love return sp; 4391a7b75aeSRobert Love } 4401a7b75aeSRobert Love 4411a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp) 4421a7b75aeSRobert Love { 4431a7b75aeSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 4441a7b75aeSRobert Love 4451a7b75aeSRobert Love sp = fc_seq_alloc(ep, ep->seq_id++); 4461a7b75aeSRobert Love FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n", 4471a7b75aeSRobert Love ep->f_ctl, sp->id); 4481a7b75aeSRobert Love return sp; 4491a7b75aeSRobert Love } 4501a7b75aeSRobert Love 4511a7b75aeSRobert Love /** 4521a7b75aeSRobert Love * Allocate a new sequence on the same exchange as the supplied sequence. 4531a7b75aeSRobert Love * This will never return NULL. 4541a7b75aeSRobert Love */ 4551a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next(struct fc_seq *sp) 4561a7b75aeSRobert Love { 4571a7b75aeSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 4581a7b75aeSRobert Love 4591a7b75aeSRobert Love spin_lock_bh(&ep->ex_lock); 4601a7b75aeSRobert Love sp = fc_seq_start_next_locked(sp); 4611a7b75aeSRobert Love spin_unlock_bh(&ep->ex_lock); 4621a7b75aeSRobert Love 4631a7b75aeSRobert Love return sp; 4641a7b75aeSRobert Love } 4651a7b75aeSRobert Love 4661a7b75aeSRobert Love /** 4671a7b75aeSRobert Love * This function is for seq_exch_abort function pointer in 4681a7b75aeSRobert Love * struct libfc_function_template, see comment block on 4691a7b75aeSRobert Love * seq_exch_abort for description of this function. 4701a7b75aeSRobert Love */ 4711a7b75aeSRobert Love static int fc_seq_exch_abort(const struct fc_seq *req_sp, 4721a7b75aeSRobert Love unsigned int timer_msec) 47342e9a92fSRobert Love { 47442e9a92fSRobert Love struct fc_seq *sp; 47542e9a92fSRobert Love struct fc_exch *ep; 47642e9a92fSRobert Love struct fc_frame *fp; 47742e9a92fSRobert Love int error; 47842e9a92fSRobert Love 47942e9a92fSRobert Love ep = fc_seq_exch(req_sp); 48042e9a92fSRobert Love 48142e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 48242e9a92fSRobert Love if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) || 48342e9a92fSRobert Love ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) { 48442e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 48542e9a92fSRobert Love return -ENXIO; 48642e9a92fSRobert Love } 48742e9a92fSRobert Love 48842e9a92fSRobert Love /* 48942e9a92fSRobert Love * Send the abort on a new sequence if possible. 49042e9a92fSRobert Love */ 49142e9a92fSRobert Love sp = fc_seq_start_next_locked(&ep->seq); 49242e9a92fSRobert Love if (!sp) { 49342e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 49442e9a92fSRobert Love return -ENOMEM; 49542e9a92fSRobert Love } 49642e9a92fSRobert Love 49742e9a92fSRobert Love ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL; 49842e9a92fSRobert Love if (timer_msec) 49942e9a92fSRobert Love fc_exch_timer_set_locked(ep, timer_msec); 50042e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 50142e9a92fSRobert Love 50242e9a92fSRobert Love /* 50342e9a92fSRobert Love * If not logged into the fabric, don't send ABTS but leave 50442e9a92fSRobert Love * sequence active until next timeout. 50542e9a92fSRobert Love */ 50642e9a92fSRobert Love if (!ep->sid) 50742e9a92fSRobert Love return 0; 50842e9a92fSRobert Love 50942e9a92fSRobert Love /* 51042e9a92fSRobert Love * Send an abort for the sequence that timed out. 51142e9a92fSRobert Love */ 51242e9a92fSRobert Love fp = fc_frame_alloc(ep->lp, 0); 51342e9a92fSRobert Love if (fp) { 51442e9a92fSRobert Love fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid, 51542e9a92fSRobert Love FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0); 51642e9a92fSRobert Love error = fc_seq_send(ep->lp, sp, fp); 51742e9a92fSRobert Love } else 51842e9a92fSRobert Love error = -ENOBUFS; 51942e9a92fSRobert Love return error; 52042e9a92fSRobert Love } 52142e9a92fSRobert Love 52242e9a92fSRobert Love /* 52342e9a92fSRobert Love * Exchange timeout - handle exchange timer expiration. 52442e9a92fSRobert Love * The timer will have been cancelled before this is called. 52542e9a92fSRobert Love */ 52642e9a92fSRobert Love static void fc_exch_timeout(struct work_struct *work) 52742e9a92fSRobert Love { 52842e9a92fSRobert Love struct fc_exch *ep = container_of(work, struct fc_exch, 52942e9a92fSRobert Love timeout_work.work); 53042e9a92fSRobert Love struct fc_seq *sp = &ep->seq; 53142e9a92fSRobert Love void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg); 53242e9a92fSRobert Love void *arg; 53342e9a92fSRobert Love u32 e_stat; 53442e9a92fSRobert Love int rc = 1; 53542e9a92fSRobert Love 536cd305ce4SRobert Love FC_EXCH_DBG(ep, "Exchange timed out\n"); 537cd305ce4SRobert Love 53842e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 53942e9a92fSRobert Love if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) 54042e9a92fSRobert Love goto unlock; 54142e9a92fSRobert Love 54242e9a92fSRobert Love e_stat = ep->esb_stat; 54342e9a92fSRobert Love if (e_stat & ESB_ST_COMPLETE) { 54442e9a92fSRobert Love ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL; 545a0cc1eccSVasu Dev spin_unlock_bh(&ep->ex_lock); 54642e9a92fSRobert Love if (e_stat & ESB_ST_REC_QUAL) 54742e9a92fSRobert Love fc_exch_rrq(ep); 54842e9a92fSRobert Love goto done; 54942e9a92fSRobert Love } else { 55042e9a92fSRobert Love resp = ep->resp; 55142e9a92fSRobert Love arg = ep->arg; 55242e9a92fSRobert Love ep->resp = NULL; 55342e9a92fSRobert Love if (e_stat & ESB_ST_ABNORMAL) 55442e9a92fSRobert Love rc = fc_exch_done_locked(ep); 55542e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 55642e9a92fSRobert Love if (!rc) 557b2f0091fSVasu Dev fc_exch_delete(ep); 55842e9a92fSRobert Love if (resp) 55942e9a92fSRobert Love resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg); 56042e9a92fSRobert Love fc_seq_exch_abort(sp, 2 * ep->r_a_tov); 56142e9a92fSRobert Love goto done; 56242e9a92fSRobert Love } 56342e9a92fSRobert Love unlock: 56442e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 56542e9a92fSRobert Love done: 56642e9a92fSRobert Love /* 56742e9a92fSRobert Love * This release matches the hold taken when the timer was set. 56842e9a92fSRobert Love */ 56942e9a92fSRobert Love fc_exch_release(ep); 57042e9a92fSRobert Love } 57142e9a92fSRobert Love 57252ff878cSVasu Dev /** 57352ff878cSVasu Dev * fc_exch_em_alloc() - allocate an exchange from a specified EM. 57452ff878cSVasu Dev * @lport: ptr to the local port 57542e9a92fSRobert Love * @mp: ptr to the exchange manager 57642e9a92fSRobert Love * 577d7179680SVasu Dev * Returns pointer to allocated fc_exch with exch lock held. 57842e9a92fSRobert Love */ 57952ff878cSVasu Dev static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport, 580d7179680SVasu Dev struct fc_exch_mgr *mp) 58142e9a92fSRobert Love { 58242e9a92fSRobert Love struct fc_exch *ep; 583b2f0091fSVasu Dev unsigned int cpu; 584b2f0091fSVasu Dev u16 index; 585b2f0091fSVasu Dev struct fc_exch_pool *pool; 58642e9a92fSRobert Love 58742e9a92fSRobert Love /* allocate memory for exchange */ 58842e9a92fSRobert Love ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC); 58942e9a92fSRobert Love if (!ep) { 59042e9a92fSRobert Love atomic_inc(&mp->stats.no_free_exch); 59142e9a92fSRobert Love goto out; 59242e9a92fSRobert Love } 59342e9a92fSRobert Love memset(ep, 0, sizeof(*ep)); 59442e9a92fSRobert Love 595b2f0091fSVasu Dev cpu = smp_processor_id(); 596b2f0091fSVasu Dev pool = per_cpu_ptr(mp->pool, cpu); 597b2f0091fSVasu Dev spin_lock_bh(&pool->lock); 598b2f0091fSVasu Dev index = pool->next_index; 599b2f0091fSVasu Dev /* allocate new exch from pool */ 600b2f0091fSVasu Dev while (fc_exch_ptr_get(pool, index)) { 601b2f0091fSVasu Dev index = index == mp->pool_max_index ? 0 : index + 1; 602b2f0091fSVasu Dev if (index == pool->next_index) 60342e9a92fSRobert Love goto err; 60442e9a92fSRobert Love } 605b2f0091fSVasu Dev pool->next_index = index == mp->pool_max_index ? 0 : index + 1; 60642e9a92fSRobert Love 60742e9a92fSRobert Love fc_exch_hold(ep); /* hold for exch in mp */ 60842e9a92fSRobert Love spin_lock_init(&ep->ex_lock); 60942e9a92fSRobert Love /* 61042e9a92fSRobert Love * Hold exch lock for caller to prevent fc_exch_reset() 61142e9a92fSRobert Love * from releasing exch while fc_exch_alloc() caller is 61242e9a92fSRobert Love * still working on exch. 61342e9a92fSRobert Love */ 61442e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 61542e9a92fSRobert Love 616b2f0091fSVasu Dev fc_exch_ptr_set(pool, index, ep); 617b2f0091fSVasu Dev list_add_tail(&ep->ex_list, &pool->ex_list); 61842e9a92fSRobert Love fc_seq_alloc(ep, ep->seq_id++); 619b2f0091fSVasu Dev pool->total_exches++; 620b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 62142e9a92fSRobert Love 62242e9a92fSRobert Love /* 62342e9a92fSRobert Love * update exchange 62442e9a92fSRobert Love */ 625b2f0091fSVasu Dev ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid; 62642e9a92fSRobert Love ep->em = mp; 627b2f0091fSVasu Dev ep->pool = pool; 62852ff878cSVasu Dev ep->lp = lport; 62942e9a92fSRobert Love ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */ 63042e9a92fSRobert Love ep->rxid = FC_XID_UNKNOWN; 63142e9a92fSRobert Love ep->class = mp->class; 63242e9a92fSRobert Love INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout); 63342e9a92fSRobert Love out: 63442e9a92fSRobert Love return ep; 63542e9a92fSRobert Love err: 636b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 63742e9a92fSRobert Love atomic_inc(&mp->stats.no_free_exch_xid); 63842e9a92fSRobert Love mempool_free(ep, mp->ep_pool); 63942e9a92fSRobert Love return NULL; 64042e9a92fSRobert Love } 64152ff878cSVasu Dev 64252ff878cSVasu Dev /** 64352ff878cSVasu Dev * fc_exch_alloc() - allocate an exchange. 64452ff878cSVasu Dev * @lport: ptr to the local port 64552ff878cSVasu Dev * @fp: ptr to the FC frame 64652ff878cSVasu Dev * 64752ff878cSVasu Dev * This function walks the list of the exchange manager(EM) 64852ff878cSVasu Dev * anchors to select a EM for new exchange allocation. The 64952ff878cSVasu Dev * EM is selected having either a NULL match function pointer 65052ff878cSVasu Dev * or call to match function returning true. 65152ff878cSVasu Dev */ 6521a7b75aeSRobert Love static struct fc_exch *fc_exch_alloc(struct fc_lport *lport, 6531a7b75aeSRobert Love struct fc_frame *fp) 65452ff878cSVasu Dev { 65552ff878cSVasu Dev struct fc_exch_mgr_anchor *ema; 65652ff878cSVasu Dev struct fc_exch *ep; 65752ff878cSVasu Dev 65852ff878cSVasu Dev list_for_each_entry(ema, &lport->ema_list, ema_list) { 65952ff878cSVasu Dev if (!ema->match || ema->match(fp)) { 660d7179680SVasu Dev ep = fc_exch_em_alloc(lport, ema->mp); 66152ff878cSVasu Dev if (ep) 66252ff878cSVasu Dev return ep; 66352ff878cSVasu Dev } 66452ff878cSVasu Dev } 66552ff878cSVasu Dev return NULL; 66652ff878cSVasu Dev } 66742e9a92fSRobert Love 66842e9a92fSRobert Love /* 66942e9a92fSRobert Love * Lookup and hold an exchange. 67042e9a92fSRobert Love */ 67142e9a92fSRobert Love static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid) 67242e9a92fSRobert Love { 673b2f0091fSVasu Dev struct fc_exch_pool *pool; 67442e9a92fSRobert Love struct fc_exch *ep = NULL; 67542e9a92fSRobert Love 67642e9a92fSRobert Love if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) { 677b2f0091fSVasu Dev pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask); 678b2f0091fSVasu Dev spin_lock_bh(&pool->lock); 679b2f0091fSVasu Dev ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order); 68042e9a92fSRobert Love if (ep) { 68142e9a92fSRobert Love fc_exch_hold(ep); 68242e9a92fSRobert Love WARN_ON(ep->xid != xid); 68342e9a92fSRobert Love } 684b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 68542e9a92fSRobert Love } 68642e9a92fSRobert Love return ep; 68742e9a92fSRobert Love } 68842e9a92fSRobert Love 6891a7b75aeSRobert Love 6901a7b75aeSRobert Love /** 6911a7b75aeSRobert Love * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and 6921a7b75aeSRobert Love * the memory allocated for the related objects may be freed. 6931a7b75aeSRobert Love * @sp: Sequence pointer 6941a7b75aeSRobert Love */ 6951a7b75aeSRobert Love static void fc_exch_done(struct fc_seq *sp) 69642e9a92fSRobert Love { 69742e9a92fSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 69842e9a92fSRobert Love int rc; 69942e9a92fSRobert Love 70042e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 70142e9a92fSRobert Love rc = fc_exch_done_locked(ep); 70242e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 70342e9a92fSRobert Love if (!rc) 704b2f0091fSVasu Dev fc_exch_delete(ep); 70542e9a92fSRobert Love } 70642e9a92fSRobert Love 70742e9a92fSRobert Love /* 70842e9a92fSRobert Love * Allocate a new exchange as responder. 70942e9a92fSRobert Love * Sets the responder ID in the frame header. 71042e9a92fSRobert Love */ 71152ff878cSVasu Dev static struct fc_exch *fc_exch_resp(struct fc_lport *lport, 71252ff878cSVasu Dev struct fc_exch_mgr *mp, 71352ff878cSVasu Dev struct fc_frame *fp) 71442e9a92fSRobert Love { 71542e9a92fSRobert Love struct fc_exch *ep; 71642e9a92fSRobert Love struct fc_frame_header *fh; 71742e9a92fSRobert Love 71852ff878cSVasu Dev ep = fc_exch_alloc(lport, fp); 71942e9a92fSRobert Love if (ep) { 72042e9a92fSRobert Love ep->class = fc_frame_class(fp); 72142e9a92fSRobert Love 72242e9a92fSRobert Love /* 72342e9a92fSRobert Love * Set EX_CTX indicating we're responding on this exchange. 72442e9a92fSRobert Love */ 72542e9a92fSRobert Love ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */ 72642e9a92fSRobert Love ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */ 72742e9a92fSRobert Love fh = fc_frame_header_get(fp); 72842e9a92fSRobert Love ep->sid = ntoh24(fh->fh_d_id); 72942e9a92fSRobert Love ep->did = ntoh24(fh->fh_s_id); 73042e9a92fSRobert Love ep->oid = ep->did; 73142e9a92fSRobert Love 73242e9a92fSRobert Love /* 73342e9a92fSRobert Love * Allocated exchange has placed the XID in the 73442e9a92fSRobert Love * originator field. Move it to the responder field, 73542e9a92fSRobert Love * and set the originator XID from the frame. 73642e9a92fSRobert Love */ 73742e9a92fSRobert Love ep->rxid = ep->xid; 73842e9a92fSRobert Love ep->oxid = ntohs(fh->fh_ox_id); 73942e9a92fSRobert Love ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT; 74042e9a92fSRobert Love if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0) 74142e9a92fSRobert Love ep->esb_stat &= ~ESB_ST_SEQ_INIT; 74242e9a92fSRobert Love 74342e9a92fSRobert Love fc_exch_hold(ep); /* hold for caller */ 74452ff878cSVasu Dev spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */ 74542e9a92fSRobert Love } 74642e9a92fSRobert Love return ep; 74742e9a92fSRobert Love } 74842e9a92fSRobert Love 74942e9a92fSRobert Love /* 75042e9a92fSRobert Love * Find a sequence for receive where the other end is originating the sequence. 75142e9a92fSRobert Love * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold 75242e9a92fSRobert Love * on the ep that should be released by the caller. 75342e9a92fSRobert Love */ 75452ff878cSVasu Dev static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport, 75552ff878cSVasu Dev struct fc_exch_mgr *mp, 756b2ab99c9SRobert Love struct fc_frame *fp) 75742e9a92fSRobert Love { 75842e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 75942e9a92fSRobert Love struct fc_exch *ep = NULL; 76042e9a92fSRobert Love struct fc_seq *sp = NULL; 76142e9a92fSRobert Love enum fc_pf_rjt_reason reject = FC_RJT_NONE; 76242e9a92fSRobert Love u32 f_ctl; 76342e9a92fSRobert Love u16 xid; 76442e9a92fSRobert Love 76542e9a92fSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 76642e9a92fSRobert Love WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0); 76742e9a92fSRobert Love 76842e9a92fSRobert Love /* 76942e9a92fSRobert Love * Lookup or create the exchange if we will be creating the sequence. 77042e9a92fSRobert Love */ 77142e9a92fSRobert Love if (f_ctl & FC_FC_EX_CTX) { 77242e9a92fSRobert Love xid = ntohs(fh->fh_ox_id); /* we originated exch */ 77342e9a92fSRobert Love ep = fc_exch_find(mp, xid); 77442e9a92fSRobert Love if (!ep) { 77542e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 77642e9a92fSRobert Love reject = FC_RJT_OX_ID; 77742e9a92fSRobert Love goto out; 77842e9a92fSRobert Love } 77942e9a92fSRobert Love if (ep->rxid == FC_XID_UNKNOWN) 78042e9a92fSRobert Love ep->rxid = ntohs(fh->fh_rx_id); 78142e9a92fSRobert Love else if (ep->rxid != ntohs(fh->fh_rx_id)) { 78242e9a92fSRobert Love reject = FC_RJT_OX_ID; 78342e9a92fSRobert Love goto rel; 78442e9a92fSRobert Love } 78542e9a92fSRobert Love } else { 78642e9a92fSRobert Love xid = ntohs(fh->fh_rx_id); /* we are the responder */ 78742e9a92fSRobert Love 78842e9a92fSRobert Love /* 78942e9a92fSRobert Love * Special case for MDS issuing an ELS TEST with a 79042e9a92fSRobert Love * bad rxid of 0. 79142e9a92fSRobert Love * XXX take this out once we do the proper reject. 79242e9a92fSRobert Love */ 79342e9a92fSRobert Love if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ && 79442e9a92fSRobert Love fc_frame_payload_op(fp) == ELS_TEST) { 79542e9a92fSRobert Love fh->fh_rx_id = htons(FC_XID_UNKNOWN); 79642e9a92fSRobert Love xid = FC_XID_UNKNOWN; 79742e9a92fSRobert Love } 79842e9a92fSRobert Love 79942e9a92fSRobert Love /* 80042e9a92fSRobert Love * new sequence - find the exchange 80142e9a92fSRobert Love */ 80242e9a92fSRobert Love ep = fc_exch_find(mp, xid); 80342e9a92fSRobert Love if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) { 80442e9a92fSRobert Love if (ep) { 80542e9a92fSRobert Love atomic_inc(&mp->stats.xid_busy); 80642e9a92fSRobert Love reject = FC_RJT_RX_ID; 80742e9a92fSRobert Love goto rel; 80842e9a92fSRobert Love } 80952ff878cSVasu Dev ep = fc_exch_resp(lport, mp, fp); 81042e9a92fSRobert Love if (!ep) { 81142e9a92fSRobert Love reject = FC_RJT_EXCH_EST; /* XXX */ 81242e9a92fSRobert Love goto out; 81342e9a92fSRobert Love } 81442e9a92fSRobert Love xid = ep->xid; /* get our XID */ 81542e9a92fSRobert Love } else if (!ep) { 81642e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 81742e9a92fSRobert Love reject = FC_RJT_RX_ID; /* XID not found */ 81842e9a92fSRobert Love goto out; 81942e9a92fSRobert Love } 82042e9a92fSRobert Love } 82142e9a92fSRobert Love 82242e9a92fSRobert Love /* 82342e9a92fSRobert Love * At this point, we have the exchange held. 82442e9a92fSRobert Love * Find or create the sequence. 82542e9a92fSRobert Love */ 82642e9a92fSRobert Love if (fc_sof_is_init(fr_sof(fp))) { 82742e9a92fSRobert Love sp = fc_seq_start_next(&ep->seq); 82842e9a92fSRobert Love if (!sp) { 82942e9a92fSRobert Love reject = FC_RJT_SEQ_XS; /* exchange shortage */ 83042e9a92fSRobert Love goto rel; 83142e9a92fSRobert Love } 83242e9a92fSRobert Love sp->id = fh->fh_seq_id; 83342e9a92fSRobert Love sp->ssb_stat |= SSB_ST_RESP; 83442e9a92fSRobert Love } else { 83542e9a92fSRobert Love sp = &ep->seq; 83642e9a92fSRobert Love if (sp->id != fh->fh_seq_id) { 83742e9a92fSRobert Love atomic_inc(&mp->stats.seq_not_found); 83842e9a92fSRobert Love reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */ 83942e9a92fSRobert Love goto rel; 84042e9a92fSRobert Love } 84142e9a92fSRobert Love } 84242e9a92fSRobert Love WARN_ON(ep != fc_seq_exch(sp)); 84342e9a92fSRobert Love 84442e9a92fSRobert Love if (f_ctl & FC_FC_SEQ_INIT) 84542e9a92fSRobert Love ep->esb_stat |= ESB_ST_SEQ_INIT; 84642e9a92fSRobert Love 84742e9a92fSRobert Love fr_seq(fp) = sp; 84842e9a92fSRobert Love out: 84942e9a92fSRobert Love return reject; 85042e9a92fSRobert Love rel: 85142e9a92fSRobert Love fc_exch_done(&ep->seq); 85242e9a92fSRobert Love fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */ 85342e9a92fSRobert Love return reject; 85442e9a92fSRobert Love } 85542e9a92fSRobert Love 85642e9a92fSRobert Love /* 85742e9a92fSRobert Love * Find the sequence for a frame being received. 85842e9a92fSRobert Love * We originated the sequence, so it should be found. 85942e9a92fSRobert Love * We may or may not have originated the exchange. 86042e9a92fSRobert Love * Does not hold the sequence for the caller. 86142e9a92fSRobert Love */ 86242e9a92fSRobert Love static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp, 86342e9a92fSRobert Love struct fc_frame *fp) 86442e9a92fSRobert Love { 86542e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 86642e9a92fSRobert Love struct fc_exch *ep; 86742e9a92fSRobert Love struct fc_seq *sp = NULL; 86842e9a92fSRobert Love u32 f_ctl; 86942e9a92fSRobert Love u16 xid; 87042e9a92fSRobert Love 87142e9a92fSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 87242e9a92fSRobert Love WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX); 87342e9a92fSRobert Love xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id); 87442e9a92fSRobert Love ep = fc_exch_find(mp, xid); 87542e9a92fSRobert Love if (!ep) 87642e9a92fSRobert Love return NULL; 87742e9a92fSRobert Love if (ep->seq.id == fh->fh_seq_id) { 87842e9a92fSRobert Love /* 87942e9a92fSRobert Love * Save the RX_ID if we didn't previously know it. 88042e9a92fSRobert Love */ 88142e9a92fSRobert Love sp = &ep->seq; 88242e9a92fSRobert Love if ((f_ctl & FC_FC_EX_CTX) != 0 && 88342e9a92fSRobert Love ep->rxid == FC_XID_UNKNOWN) { 88442e9a92fSRobert Love ep->rxid = ntohs(fh->fh_rx_id); 88542e9a92fSRobert Love } 88642e9a92fSRobert Love } 88742e9a92fSRobert Love fc_exch_release(ep); 88842e9a92fSRobert Love return sp; 88942e9a92fSRobert Love } 89042e9a92fSRobert Love 89142e9a92fSRobert Love /* 89242e9a92fSRobert Love * Set addresses for an exchange. 89342e9a92fSRobert Love * Note this must be done before the first sequence of the exchange is sent. 89442e9a92fSRobert Love */ 89542e9a92fSRobert Love static void fc_exch_set_addr(struct fc_exch *ep, 89642e9a92fSRobert Love u32 orig_id, u32 resp_id) 89742e9a92fSRobert Love { 89842e9a92fSRobert Love ep->oid = orig_id; 89942e9a92fSRobert Love if (ep->esb_stat & ESB_ST_RESP) { 90042e9a92fSRobert Love ep->sid = resp_id; 90142e9a92fSRobert Love ep->did = orig_id; 90242e9a92fSRobert Love } else { 90342e9a92fSRobert Love ep->sid = orig_id; 90442e9a92fSRobert Love ep->did = resp_id; 90542e9a92fSRobert Love } 90642e9a92fSRobert Love } 90742e9a92fSRobert Love 9081a7b75aeSRobert Love /** 9091a7b75aeSRobert Love * fc_seq_els_rsp_send() - Send ELS response using mainly infomation 9101a7b75aeSRobert Love * in exchange and sequence in EM layer. 9111a7b75aeSRobert Love * @sp: Sequence pointer 9121a7b75aeSRobert Love * @els_cmd: ELS command 9131a7b75aeSRobert Love * @els_data: ELS data 91442e9a92fSRobert Love */ 9151a7b75aeSRobert Love static void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd, 91642e9a92fSRobert Love struct fc_seq_els_data *els_data) 91742e9a92fSRobert Love { 91842e9a92fSRobert Love switch (els_cmd) { 91942e9a92fSRobert Love case ELS_LS_RJT: 92042e9a92fSRobert Love fc_seq_ls_rjt(sp, els_data->reason, els_data->explan); 92142e9a92fSRobert Love break; 92242e9a92fSRobert Love case ELS_LS_ACC: 92342e9a92fSRobert Love fc_seq_ls_acc(sp); 92442e9a92fSRobert Love break; 92542e9a92fSRobert Love case ELS_RRQ: 92642e9a92fSRobert Love fc_exch_els_rrq(sp, els_data->fp); 92742e9a92fSRobert Love break; 92842e9a92fSRobert Love case ELS_REC: 92942e9a92fSRobert Love fc_exch_els_rec(sp, els_data->fp); 93042e9a92fSRobert Love break; 93142e9a92fSRobert Love default: 9327414705eSRobert Love FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd); 93342e9a92fSRobert Love } 93442e9a92fSRobert Love } 93542e9a92fSRobert Love 93642e9a92fSRobert Love /* 93742e9a92fSRobert Love * Send a sequence, which is also the last sequence in the exchange. 93842e9a92fSRobert Love */ 93942e9a92fSRobert Love static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp, 94042e9a92fSRobert Love enum fc_rctl rctl, enum fc_fh_type fh_type) 94142e9a92fSRobert Love { 94242e9a92fSRobert Love u32 f_ctl; 94342e9a92fSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 94442e9a92fSRobert Love 94542e9a92fSRobert Love f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT; 94642e9a92fSRobert Love f_ctl |= ep->f_ctl; 94742e9a92fSRobert Love fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0); 94842e9a92fSRobert Love fc_seq_send(ep->lp, sp, fp); 94942e9a92fSRobert Love } 95042e9a92fSRobert Love 95142e9a92fSRobert Love /* 95242e9a92fSRobert Love * Send ACK_1 (or equiv.) indicating we received something. 95342e9a92fSRobert Love * The frame we're acking is supplied. 95442e9a92fSRobert Love */ 95542e9a92fSRobert Love static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp) 95642e9a92fSRobert Love { 95742e9a92fSRobert Love struct fc_frame *fp; 95842e9a92fSRobert Love struct fc_frame_header *rx_fh; 95942e9a92fSRobert Love struct fc_frame_header *fh; 96042e9a92fSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 96142e9a92fSRobert Love struct fc_lport *lp = ep->lp; 96242e9a92fSRobert Love unsigned int f_ctl; 96342e9a92fSRobert Love 96442e9a92fSRobert Love /* 96542e9a92fSRobert Love * Don't send ACKs for class 3. 96642e9a92fSRobert Love */ 96742e9a92fSRobert Love if (fc_sof_needs_ack(fr_sof(rx_fp))) { 96842e9a92fSRobert Love fp = fc_frame_alloc(lp, 0); 96942e9a92fSRobert Love if (!fp) 97042e9a92fSRobert Love return; 97142e9a92fSRobert Love 97242e9a92fSRobert Love fh = fc_frame_header_get(fp); 97342e9a92fSRobert Love fh->fh_r_ctl = FC_RCTL_ACK_1; 97442e9a92fSRobert Love fh->fh_type = FC_TYPE_BLS; 97542e9a92fSRobert Love 97642e9a92fSRobert Love /* 97742e9a92fSRobert Love * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22). 97842e9a92fSRobert Love * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT. 97942e9a92fSRobert Love * Bits 9-8 are meaningful (retransmitted or unidirectional). 98042e9a92fSRobert Love * Last ACK uses bits 7-6 (continue sequence), 98142e9a92fSRobert Love * bits 5-4 are meaningful (what kind of ACK to use). 98242e9a92fSRobert Love */ 98342e9a92fSRobert Love rx_fh = fc_frame_header_get(rx_fp); 98442e9a92fSRobert Love f_ctl = ntoh24(rx_fh->fh_f_ctl); 98542e9a92fSRobert Love f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX | 98642e9a92fSRobert Love FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ | 98742e9a92fSRobert Love FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT | 98842e9a92fSRobert Love FC_FC_RETX_SEQ | FC_FC_UNI_TX; 98942e9a92fSRobert Love f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX; 99042e9a92fSRobert Love hton24(fh->fh_f_ctl, f_ctl); 99142e9a92fSRobert Love 99242e9a92fSRobert Love fc_exch_setup_hdr(ep, fp, f_ctl); 99342e9a92fSRobert Love fh->fh_seq_id = rx_fh->fh_seq_id; 99442e9a92fSRobert Love fh->fh_seq_cnt = rx_fh->fh_seq_cnt; 99542e9a92fSRobert Love fh->fh_parm_offset = htonl(1); /* ack single frame */ 99642e9a92fSRobert Love 99742e9a92fSRobert Love fr_sof(fp) = fr_sof(rx_fp); 99842e9a92fSRobert Love if (f_ctl & FC_FC_END_SEQ) 99942e9a92fSRobert Love fr_eof(fp) = FC_EOF_T; 100042e9a92fSRobert Love else 100142e9a92fSRobert Love fr_eof(fp) = FC_EOF_N; 100242e9a92fSRobert Love 100342e9a92fSRobert Love (void) lp->tt.frame_send(lp, fp); 100442e9a92fSRobert Love } 100542e9a92fSRobert Love } 100642e9a92fSRobert Love 100742e9a92fSRobert Love /* 100842e9a92fSRobert Love * Send BLS Reject. 100942e9a92fSRobert Love * This is for rejecting BA_ABTS only. 101042e9a92fSRobert Love */ 1011b2ab99c9SRobert Love static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp, 1012b2ab99c9SRobert Love enum fc_ba_rjt_reason reason, 101342e9a92fSRobert Love enum fc_ba_rjt_explan explan) 101442e9a92fSRobert Love { 101542e9a92fSRobert Love struct fc_frame *fp; 101642e9a92fSRobert Love struct fc_frame_header *rx_fh; 101742e9a92fSRobert Love struct fc_frame_header *fh; 101842e9a92fSRobert Love struct fc_ba_rjt *rp; 101942e9a92fSRobert Love struct fc_lport *lp; 102042e9a92fSRobert Love unsigned int f_ctl; 102142e9a92fSRobert Love 102242e9a92fSRobert Love lp = fr_dev(rx_fp); 102342e9a92fSRobert Love fp = fc_frame_alloc(lp, sizeof(*rp)); 102442e9a92fSRobert Love if (!fp) 102542e9a92fSRobert Love return; 102642e9a92fSRobert Love fh = fc_frame_header_get(fp); 102742e9a92fSRobert Love rx_fh = fc_frame_header_get(rx_fp); 102842e9a92fSRobert Love 102942e9a92fSRobert Love memset(fh, 0, sizeof(*fh) + sizeof(*rp)); 103042e9a92fSRobert Love 103142e9a92fSRobert Love rp = fc_frame_payload_get(fp, sizeof(*rp)); 103242e9a92fSRobert Love rp->br_reason = reason; 103342e9a92fSRobert Love rp->br_explan = explan; 103442e9a92fSRobert Love 103542e9a92fSRobert Love /* 103642e9a92fSRobert Love * seq_id, cs_ctl, df_ctl and param/offset are zero. 103742e9a92fSRobert Love */ 103842e9a92fSRobert Love memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3); 103942e9a92fSRobert Love memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3); 10401d490ce3SJoe Eykholt fh->fh_ox_id = rx_fh->fh_ox_id; 10411d490ce3SJoe Eykholt fh->fh_rx_id = rx_fh->fh_rx_id; 104242e9a92fSRobert Love fh->fh_seq_cnt = rx_fh->fh_seq_cnt; 104342e9a92fSRobert Love fh->fh_r_ctl = FC_RCTL_BA_RJT; 104442e9a92fSRobert Love fh->fh_type = FC_TYPE_BLS; 104542e9a92fSRobert Love 104642e9a92fSRobert Love /* 104742e9a92fSRobert Love * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22). 104842e9a92fSRobert Love * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT. 104942e9a92fSRobert Love * Bits 9-8 are meaningful (retransmitted or unidirectional). 105042e9a92fSRobert Love * Last ACK uses bits 7-6 (continue sequence), 105142e9a92fSRobert Love * bits 5-4 are meaningful (what kind of ACK to use). 105242e9a92fSRobert Love * Always set LAST_SEQ, END_SEQ. 105342e9a92fSRobert Love */ 105442e9a92fSRobert Love f_ctl = ntoh24(rx_fh->fh_f_ctl); 105542e9a92fSRobert Love f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX | 105642e9a92fSRobert Love FC_FC_END_CONN | FC_FC_SEQ_INIT | 105742e9a92fSRobert Love FC_FC_RETX_SEQ | FC_FC_UNI_TX; 105842e9a92fSRobert Love f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX; 105942e9a92fSRobert Love f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ; 106042e9a92fSRobert Love f_ctl &= ~FC_FC_FIRST_SEQ; 106142e9a92fSRobert Love hton24(fh->fh_f_ctl, f_ctl); 106242e9a92fSRobert Love 106342e9a92fSRobert Love fr_sof(fp) = fc_sof_class(fr_sof(rx_fp)); 106442e9a92fSRobert Love fr_eof(fp) = FC_EOF_T; 106542e9a92fSRobert Love if (fc_sof_needs_ack(fr_sof(fp))) 106642e9a92fSRobert Love fr_eof(fp) = FC_EOF_N; 106742e9a92fSRobert Love 106842e9a92fSRobert Love (void) lp->tt.frame_send(lp, fp); 106942e9a92fSRobert Love } 107042e9a92fSRobert Love 107142e9a92fSRobert Love /* 107242e9a92fSRobert Love * Handle an incoming ABTS. This would be for target mode usually, 107342e9a92fSRobert Love * but could be due to lost FCP transfer ready, confirm or RRQ. 107442e9a92fSRobert Love * We always handle this as an exchange abort, ignoring the parameter. 107542e9a92fSRobert Love */ 107642e9a92fSRobert Love static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp) 107742e9a92fSRobert Love { 107842e9a92fSRobert Love struct fc_frame *fp; 107942e9a92fSRobert Love struct fc_ba_acc *ap; 108042e9a92fSRobert Love struct fc_frame_header *fh; 108142e9a92fSRobert Love struct fc_seq *sp; 108242e9a92fSRobert Love 108342e9a92fSRobert Love if (!ep) 108442e9a92fSRobert Love goto reject; 108542e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 108642e9a92fSRobert Love if (ep->esb_stat & ESB_ST_COMPLETE) { 108742e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 108842e9a92fSRobert Love goto reject; 108942e9a92fSRobert Love } 109042e9a92fSRobert Love if (!(ep->esb_stat & ESB_ST_REC_QUAL)) 109142e9a92fSRobert Love fc_exch_hold(ep); /* hold for REC_QUAL */ 109242e9a92fSRobert Love ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL; 109342e9a92fSRobert Love fc_exch_timer_set_locked(ep, ep->r_a_tov); 109442e9a92fSRobert Love 109542e9a92fSRobert Love fp = fc_frame_alloc(ep->lp, sizeof(*ap)); 109642e9a92fSRobert Love if (!fp) { 109742e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 109842e9a92fSRobert Love goto free; 109942e9a92fSRobert Love } 110042e9a92fSRobert Love fh = fc_frame_header_get(fp); 110142e9a92fSRobert Love ap = fc_frame_payload_get(fp, sizeof(*ap)); 110242e9a92fSRobert Love memset(ap, 0, sizeof(*ap)); 110342e9a92fSRobert Love sp = &ep->seq; 110442e9a92fSRobert Love ap->ba_high_seq_cnt = htons(0xffff); 110542e9a92fSRobert Love if (sp->ssb_stat & SSB_ST_RESP) { 110642e9a92fSRobert Love ap->ba_seq_id = sp->id; 110742e9a92fSRobert Love ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL; 110842e9a92fSRobert Love ap->ba_high_seq_cnt = fh->fh_seq_cnt; 110942e9a92fSRobert Love ap->ba_low_seq_cnt = htons(sp->cnt); 111042e9a92fSRobert Love } 1111a7e84f2bSVasu Dev sp = fc_seq_start_next_locked(sp); 111242e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 111342e9a92fSRobert Love fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS); 111442e9a92fSRobert Love fc_frame_free(rx_fp); 111542e9a92fSRobert Love return; 111642e9a92fSRobert Love 111742e9a92fSRobert Love reject: 111842e9a92fSRobert Love fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID); 111942e9a92fSRobert Love free: 112042e9a92fSRobert Love fc_frame_free(rx_fp); 112142e9a92fSRobert Love } 112242e9a92fSRobert Love 112342e9a92fSRobert Love /* 112442e9a92fSRobert Love * Handle receive where the other end is originating the sequence. 112542e9a92fSRobert Love */ 112642e9a92fSRobert Love static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp, 112742e9a92fSRobert Love struct fc_frame *fp) 112842e9a92fSRobert Love { 112942e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 113042e9a92fSRobert Love struct fc_seq *sp = NULL; 113142e9a92fSRobert Love struct fc_exch *ep = NULL; 113242e9a92fSRobert Love enum fc_sof sof; 113342e9a92fSRobert Love enum fc_eof eof; 113442e9a92fSRobert Love u32 f_ctl; 113542e9a92fSRobert Love enum fc_pf_rjt_reason reject; 113642e9a92fSRobert Love 1137*174e1ebfSChris Leech /* We can have the wrong fc_lport at this point with NPIV, which is a 1138*174e1ebfSChris Leech * problem now that we know a new exchange needs to be allocated 1139*174e1ebfSChris Leech */ 1140*174e1ebfSChris Leech lp = fc_vport_id_lookup(lp, ntoh24(fh->fh_d_id)); 1141*174e1ebfSChris Leech if (!lp) { 1142*174e1ebfSChris Leech fc_frame_free(fp); 1143*174e1ebfSChris Leech return; 1144*174e1ebfSChris Leech } 1145*174e1ebfSChris Leech 114642e9a92fSRobert Love fr_seq(fp) = NULL; 114752ff878cSVasu Dev reject = fc_seq_lookup_recip(lp, mp, fp); 114842e9a92fSRobert Love if (reject == FC_RJT_NONE) { 114942e9a92fSRobert Love sp = fr_seq(fp); /* sequence will be held */ 115042e9a92fSRobert Love ep = fc_seq_exch(sp); 115142e9a92fSRobert Love sof = fr_sof(fp); 115242e9a92fSRobert Love eof = fr_eof(fp); 115342e9a92fSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 115442e9a92fSRobert Love fc_seq_send_ack(sp, fp); 115542e9a92fSRobert Love 115642e9a92fSRobert Love /* 115742e9a92fSRobert Love * Call the receive function. 115842e9a92fSRobert Love * 115942e9a92fSRobert Love * The receive function may allocate a new sequence 116042e9a92fSRobert Love * over the old one, so we shouldn't change the 116142e9a92fSRobert Love * sequence after this. 116242e9a92fSRobert Love * 116342e9a92fSRobert Love * The frame will be freed by the receive function. 116442e9a92fSRobert Love * If new exch resp handler is valid then call that 116542e9a92fSRobert Love * first. 116642e9a92fSRobert Love */ 116742e9a92fSRobert Love if (ep->resp) 116842e9a92fSRobert Love ep->resp(sp, fp, ep->arg); 116942e9a92fSRobert Love else 117042e9a92fSRobert Love lp->tt.lport_recv(lp, sp, fp); 117142e9a92fSRobert Love fc_exch_release(ep); /* release from lookup */ 117242e9a92fSRobert Love } else { 1173d459b7eaSRobert Love FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject); 117442e9a92fSRobert Love fc_frame_free(fp); 117542e9a92fSRobert Love } 117642e9a92fSRobert Love } 117742e9a92fSRobert Love 117842e9a92fSRobert Love /* 117942e9a92fSRobert Love * Handle receive where the other end is originating the sequence in 118042e9a92fSRobert Love * response to our exchange. 118142e9a92fSRobert Love */ 118242e9a92fSRobert Love static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp) 118342e9a92fSRobert Love { 118442e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 118542e9a92fSRobert Love struct fc_seq *sp; 118642e9a92fSRobert Love struct fc_exch *ep; 118742e9a92fSRobert Love enum fc_sof sof; 118842e9a92fSRobert Love u32 f_ctl; 118942e9a92fSRobert Love void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg); 119042e9a92fSRobert Love void *ex_resp_arg; 119142e9a92fSRobert Love int rc; 119242e9a92fSRobert Love 119342e9a92fSRobert Love ep = fc_exch_find(mp, ntohs(fh->fh_ox_id)); 119442e9a92fSRobert Love if (!ep) { 119542e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 119642e9a92fSRobert Love goto out; 119742e9a92fSRobert Love } 119830121d14SSteve Ma if (ep->esb_stat & ESB_ST_COMPLETE) { 119930121d14SSteve Ma atomic_inc(&mp->stats.xid_not_found); 120030121d14SSteve Ma goto out; 120130121d14SSteve Ma } 120242e9a92fSRobert Love if (ep->rxid == FC_XID_UNKNOWN) 120342e9a92fSRobert Love ep->rxid = ntohs(fh->fh_rx_id); 120442e9a92fSRobert Love if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) { 120542e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 120642e9a92fSRobert Love goto rel; 120742e9a92fSRobert Love } 120842e9a92fSRobert Love if (ep->did != ntoh24(fh->fh_s_id) && 120942e9a92fSRobert Love ep->did != FC_FID_FLOGI) { 121042e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 121142e9a92fSRobert Love goto rel; 121242e9a92fSRobert Love } 121342e9a92fSRobert Love sof = fr_sof(fp); 121442e9a92fSRobert Love if (fc_sof_is_init(sof)) { 121542e9a92fSRobert Love sp = fc_seq_start_next(&ep->seq); 121642e9a92fSRobert Love sp->id = fh->fh_seq_id; 121742e9a92fSRobert Love sp->ssb_stat |= SSB_ST_RESP; 121842e9a92fSRobert Love } else { 121942e9a92fSRobert Love sp = &ep->seq; 122042e9a92fSRobert Love if (sp->id != fh->fh_seq_id) { 122142e9a92fSRobert Love atomic_inc(&mp->stats.seq_not_found); 122242e9a92fSRobert Love goto rel; 122342e9a92fSRobert Love } 122442e9a92fSRobert Love } 122542e9a92fSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 122642e9a92fSRobert Love fr_seq(fp) = sp; 122742e9a92fSRobert Love if (f_ctl & FC_FC_SEQ_INIT) 122842e9a92fSRobert Love ep->esb_stat |= ESB_ST_SEQ_INIT; 122942e9a92fSRobert Love 123042e9a92fSRobert Love if (fc_sof_needs_ack(sof)) 123142e9a92fSRobert Love fc_seq_send_ack(sp, fp); 123242e9a92fSRobert Love resp = ep->resp; 123342e9a92fSRobert Love ex_resp_arg = ep->arg; 123442e9a92fSRobert Love 123542e9a92fSRobert Love if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T && 123642e9a92fSRobert Love (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) == 123742e9a92fSRobert Love (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) { 123842e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 123942e9a92fSRobert Love rc = fc_exch_done_locked(ep); 124042e9a92fSRobert Love WARN_ON(fc_seq_exch(sp) != ep); 124142e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 124242e9a92fSRobert Love if (!rc) 1243b2f0091fSVasu Dev fc_exch_delete(ep); 124442e9a92fSRobert Love } 124542e9a92fSRobert Love 124642e9a92fSRobert Love /* 124742e9a92fSRobert Love * Call the receive function. 124842e9a92fSRobert Love * The sequence is held (has a refcnt) for us, 124942e9a92fSRobert Love * but not for the receive function. 125042e9a92fSRobert Love * 125142e9a92fSRobert Love * The receive function may allocate a new sequence 125242e9a92fSRobert Love * over the old one, so we shouldn't change the 125342e9a92fSRobert Love * sequence after this. 125442e9a92fSRobert Love * 125542e9a92fSRobert Love * The frame will be freed by the receive function. 125642e9a92fSRobert Love * If new exch resp handler is valid then call that 125742e9a92fSRobert Love * first. 125842e9a92fSRobert Love */ 125942e9a92fSRobert Love if (resp) 126042e9a92fSRobert Love resp(sp, fp, ex_resp_arg); 126142e9a92fSRobert Love else 126242e9a92fSRobert Love fc_frame_free(fp); 126342e9a92fSRobert Love fc_exch_release(ep); 126442e9a92fSRobert Love return; 126542e9a92fSRobert Love rel: 126642e9a92fSRobert Love fc_exch_release(ep); 126742e9a92fSRobert Love out: 126842e9a92fSRobert Love fc_frame_free(fp); 126942e9a92fSRobert Love } 127042e9a92fSRobert Love 127142e9a92fSRobert Love /* 127242e9a92fSRobert Love * Handle receive for a sequence where other end is responding to our sequence. 127342e9a92fSRobert Love */ 127442e9a92fSRobert Love static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp) 127542e9a92fSRobert Love { 127642e9a92fSRobert Love struct fc_seq *sp; 127742e9a92fSRobert Love 127842e9a92fSRobert Love sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */ 1279d459b7eaSRobert Love 1280d459b7eaSRobert Love if (!sp) 128142e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 1282d459b7eaSRobert Love else 128342e9a92fSRobert Love atomic_inc(&mp->stats.non_bls_resp); 1284d459b7eaSRobert Love 128542e9a92fSRobert Love fc_frame_free(fp); 128642e9a92fSRobert Love } 128742e9a92fSRobert Love 128842e9a92fSRobert Love /* 128942e9a92fSRobert Love * Handle the response to an ABTS for exchange or sequence. 129042e9a92fSRobert Love * This can be BA_ACC or BA_RJT. 129142e9a92fSRobert Love */ 129242e9a92fSRobert Love static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp) 129342e9a92fSRobert Love { 129442e9a92fSRobert Love void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg); 129542e9a92fSRobert Love void *ex_resp_arg; 129642e9a92fSRobert Love struct fc_frame_header *fh; 129742e9a92fSRobert Love struct fc_ba_acc *ap; 129842e9a92fSRobert Love struct fc_seq *sp; 129942e9a92fSRobert Love u16 low; 130042e9a92fSRobert Love u16 high; 130142e9a92fSRobert Love int rc = 1, has_rec = 0; 130242e9a92fSRobert Love 130342e9a92fSRobert Love fh = fc_frame_header_get(fp); 13047414705eSRobert Love FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl, 13057414705eSRobert Love fc_exch_rctl_name(fh->fh_r_ctl)); 130642e9a92fSRobert Love 130742e9a92fSRobert Love if (cancel_delayed_work_sync(&ep->timeout_work)) 130842e9a92fSRobert Love fc_exch_release(ep); /* release from pending timer hold */ 130942e9a92fSRobert Love 131042e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 131142e9a92fSRobert Love switch (fh->fh_r_ctl) { 131242e9a92fSRobert Love case FC_RCTL_BA_ACC: 131342e9a92fSRobert Love ap = fc_frame_payload_get(fp, sizeof(*ap)); 131442e9a92fSRobert Love if (!ap) 131542e9a92fSRobert Love break; 131642e9a92fSRobert Love 131742e9a92fSRobert Love /* 131842e9a92fSRobert Love * Decide whether to establish a Recovery Qualifier. 131942e9a92fSRobert Love * We do this if there is a non-empty SEQ_CNT range and 132042e9a92fSRobert Love * SEQ_ID is the same as the one we aborted. 132142e9a92fSRobert Love */ 132242e9a92fSRobert Love low = ntohs(ap->ba_low_seq_cnt); 132342e9a92fSRobert Love high = ntohs(ap->ba_high_seq_cnt); 132442e9a92fSRobert Love if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 && 132542e9a92fSRobert Love (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL || 132642e9a92fSRobert Love ap->ba_seq_id == ep->seq_id) && low != high) { 132742e9a92fSRobert Love ep->esb_stat |= ESB_ST_REC_QUAL; 132842e9a92fSRobert Love fc_exch_hold(ep); /* hold for recovery qualifier */ 132942e9a92fSRobert Love has_rec = 1; 133042e9a92fSRobert Love } 133142e9a92fSRobert Love break; 133242e9a92fSRobert Love case FC_RCTL_BA_RJT: 133342e9a92fSRobert Love break; 133442e9a92fSRobert Love default: 133542e9a92fSRobert Love break; 133642e9a92fSRobert Love } 133742e9a92fSRobert Love 133842e9a92fSRobert Love resp = ep->resp; 133942e9a92fSRobert Love ex_resp_arg = ep->arg; 134042e9a92fSRobert Love 134142e9a92fSRobert Love /* do we need to do some other checks here. Can we reuse more of 134242e9a92fSRobert Love * fc_exch_recv_seq_resp 134342e9a92fSRobert Love */ 134442e9a92fSRobert Love sp = &ep->seq; 134542e9a92fSRobert Love /* 134642e9a92fSRobert Love * do we want to check END_SEQ as well as LAST_SEQ here? 134742e9a92fSRobert Love */ 134842e9a92fSRobert Love if (ep->fh_type != FC_TYPE_FCP && 134942e9a92fSRobert Love ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ) 135042e9a92fSRobert Love rc = fc_exch_done_locked(ep); 135142e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 135242e9a92fSRobert Love if (!rc) 1353b2f0091fSVasu Dev fc_exch_delete(ep); 135442e9a92fSRobert Love 135542e9a92fSRobert Love if (resp) 135642e9a92fSRobert Love resp(sp, fp, ex_resp_arg); 135742e9a92fSRobert Love else 135842e9a92fSRobert Love fc_frame_free(fp); 135942e9a92fSRobert Love 136042e9a92fSRobert Love if (has_rec) 136142e9a92fSRobert Love fc_exch_timer_set(ep, ep->r_a_tov); 136242e9a92fSRobert Love 136342e9a92fSRobert Love } 136442e9a92fSRobert Love 136542e9a92fSRobert Love /* 136642e9a92fSRobert Love * Receive BLS sequence. 136742e9a92fSRobert Love * This is always a sequence initiated by the remote side. 136842e9a92fSRobert Love * We may be either the originator or recipient of the exchange. 136942e9a92fSRobert Love */ 137042e9a92fSRobert Love static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp) 137142e9a92fSRobert Love { 137242e9a92fSRobert Love struct fc_frame_header *fh; 137342e9a92fSRobert Love struct fc_exch *ep; 137442e9a92fSRobert Love u32 f_ctl; 137542e9a92fSRobert Love 137642e9a92fSRobert Love fh = fc_frame_header_get(fp); 137742e9a92fSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 137842e9a92fSRobert Love fr_seq(fp) = NULL; 137942e9a92fSRobert Love 138042e9a92fSRobert Love ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ? 138142e9a92fSRobert Love ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id)); 138242e9a92fSRobert Love if (ep && (f_ctl & FC_FC_SEQ_INIT)) { 138342e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 138442e9a92fSRobert Love ep->esb_stat |= ESB_ST_SEQ_INIT; 138542e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 138642e9a92fSRobert Love } 138742e9a92fSRobert Love if (f_ctl & FC_FC_SEQ_CTX) { 138842e9a92fSRobert Love /* 138942e9a92fSRobert Love * A response to a sequence we initiated. 139042e9a92fSRobert Love * This should only be ACKs for class 2 or F. 139142e9a92fSRobert Love */ 139242e9a92fSRobert Love switch (fh->fh_r_ctl) { 139342e9a92fSRobert Love case FC_RCTL_ACK_1: 139442e9a92fSRobert Love case FC_RCTL_ACK_0: 139542e9a92fSRobert Love break; 139642e9a92fSRobert Love default: 13977414705eSRobert Love FC_EXCH_DBG(ep, "BLS rctl %x - %s received", 139842e9a92fSRobert Love fh->fh_r_ctl, 139942e9a92fSRobert Love fc_exch_rctl_name(fh->fh_r_ctl)); 140042e9a92fSRobert Love break; 140142e9a92fSRobert Love } 140242e9a92fSRobert Love fc_frame_free(fp); 140342e9a92fSRobert Love } else { 140442e9a92fSRobert Love switch (fh->fh_r_ctl) { 140542e9a92fSRobert Love case FC_RCTL_BA_RJT: 140642e9a92fSRobert Love case FC_RCTL_BA_ACC: 140742e9a92fSRobert Love if (ep) 140842e9a92fSRobert Love fc_exch_abts_resp(ep, fp); 140942e9a92fSRobert Love else 141042e9a92fSRobert Love fc_frame_free(fp); 141142e9a92fSRobert Love break; 141242e9a92fSRobert Love case FC_RCTL_BA_ABTS: 141342e9a92fSRobert Love fc_exch_recv_abts(ep, fp); 141442e9a92fSRobert Love break; 141542e9a92fSRobert Love default: /* ignore junk */ 141642e9a92fSRobert Love fc_frame_free(fp); 141742e9a92fSRobert Love break; 141842e9a92fSRobert Love } 141942e9a92fSRobert Love } 142042e9a92fSRobert Love if (ep) 142142e9a92fSRobert Love fc_exch_release(ep); /* release hold taken by fc_exch_find */ 142242e9a92fSRobert Love } 142342e9a92fSRobert Love 142442e9a92fSRobert Love /* 142542e9a92fSRobert Love * Accept sequence with LS_ACC. 142642e9a92fSRobert Love * If this fails due to allocation or transmit congestion, assume the 142742e9a92fSRobert Love * originator will repeat the sequence. 142842e9a92fSRobert Love */ 142942e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *req_sp) 143042e9a92fSRobert Love { 143142e9a92fSRobert Love struct fc_seq *sp; 143242e9a92fSRobert Love struct fc_els_ls_acc *acc; 143342e9a92fSRobert Love struct fc_frame *fp; 143442e9a92fSRobert Love 143542e9a92fSRobert Love sp = fc_seq_start_next(req_sp); 143642e9a92fSRobert Love fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc)); 143742e9a92fSRobert Love if (fp) { 143842e9a92fSRobert Love acc = fc_frame_payload_get(fp, sizeof(*acc)); 143942e9a92fSRobert Love memset(acc, 0, sizeof(*acc)); 144042e9a92fSRobert Love acc->la_cmd = ELS_LS_ACC; 144142e9a92fSRobert Love fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS); 144242e9a92fSRobert Love } 144342e9a92fSRobert Love } 144442e9a92fSRobert Love 144542e9a92fSRobert Love /* 144642e9a92fSRobert Love * Reject sequence with ELS LS_RJT. 144742e9a92fSRobert Love * If this fails due to allocation or transmit congestion, assume the 144842e9a92fSRobert Love * originator will repeat the sequence. 144942e9a92fSRobert Love */ 145042e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason, 145142e9a92fSRobert Love enum fc_els_rjt_explan explan) 145242e9a92fSRobert Love { 145342e9a92fSRobert Love struct fc_seq *sp; 145442e9a92fSRobert Love struct fc_els_ls_rjt *rjt; 145542e9a92fSRobert Love struct fc_frame *fp; 145642e9a92fSRobert Love 145742e9a92fSRobert Love sp = fc_seq_start_next(req_sp); 145842e9a92fSRobert Love fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt)); 145942e9a92fSRobert Love if (fp) { 146042e9a92fSRobert Love rjt = fc_frame_payload_get(fp, sizeof(*rjt)); 146142e9a92fSRobert Love memset(rjt, 0, sizeof(*rjt)); 146242e9a92fSRobert Love rjt->er_cmd = ELS_LS_RJT; 146342e9a92fSRobert Love rjt->er_reason = reason; 146442e9a92fSRobert Love rjt->er_explan = explan; 146542e9a92fSRobert Love fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS); 146642e9a92fSRobert Love } 146742e9a92fSRobert Love } 146842e9a92fSRobert Love 146942e9a92fSRobert Love static void fc_exch_reset(struct fc_exch *ep) 147042e9a92fSRobert Love { 147142e9a92fSRobert Love struct fc_seq *sp; 147242e9a92fSRobert Love void (*resp)(struct fc_seq *, struct fc_frame *, void *); 147342e9a92fSRobert Love void *arg; 147442e9a92fSRobert Love int rc = 1; 147542e9a92fSRobert Love 147642e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 147742e9a92fSRobert Love ep->state |= FC_EX_RST_CLEANUP; 147842e9a92fSRobert Love /* 147942e9a92fSRobert Love * we really want to call del_timer_sync, but cannot due 148042e9a92fSRobert Love * to the lport calling with the lport lock held (some resp 148142e9a92fSRobert Love * functions can also grab the lport lock which could cause 148242e9a92fSRobert Love * a deadlock). 148342e9a92fSRobert Love */ 148442e9a92fSRobert Love if (cancel_delayed_work(&ep->timeout_work)) 148542e9a92fSRobert Love atomic_dec(&ep->ex_refcnt); /* drop hold for timer */ 148642e9a92fSRobert Love resp = ep->resp; 148742e9a92fSRobert Love ep->resp = NULL; 148842e9a92fSRobert Love if (ep->esb_stat & ESB_ST_REC_QUAL) 148942e9a92fSRobert Love atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */ 149042e9a92fSRobert Love ep->esb_stat &= ~ESB_ST_REC_QUAL; 149142e9a92fSRobert Love arg = ep->arg; 149242e9a92fSRobert Love sp = &ep->seq; 149342e9a92fSRobert Love rc = fc_exch_done_locked(ep); 149442e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 149542e9a92fSRobert Love if (!rc) 1496b2f0091fSVasu Dev fc_exch_delete(ep); 149742e9a92fSRobert Love 149842e9a92fSRobert Love if (resp) 149942e9a92fSRobert Love resp(sp, ERR_PTR(-FC_EX_CLOSED), arg); 150042e9a92fSRobert Love } 150142e9a92fSRobert Love 1502b2f0091fSVasu Dev /** 1503b2f0091fSVasu Dev * fc_exch_pool_reset() - Resets an per cpu exches pool. 1504b2f0091fSVasu Dev * @lport: ptr to the local port 1505b2f0091fSVasu Dev * @pool: ptr to the per cpu exches pool 1506b2f0091fSVasu Dev * @sid: source FC ID 1507b2f0091fSVasu Dev * @did: destination FC ID 1508b2f0091fSVasu Dev * 1509b2f0091fSVasu Dev * Resets an per cpu exches pool, releasing its all sequences 1510b2f0091fSVasu Dev * and exchanges. If sid is non-zero, then reset only exchanges 1511b2f0091fSVasu Dev * we sourced from that FID. If did is non-zero, reset only 1512b2f0091fSVasu Dev * exchanges destined to that FID. 151342e9a92fSRobert Love */ 1514b2f0091fSVasu Dev static void fc_exch_pool_reset(struct fc_lport *lport, 1515b2f0091fSVasu Dev struct fc_exch_pool *pool, 1516b2f0091fSVasu Dev u32 sid, u32 did) 151742e9a92fSRobert Love { 151842e9a92fSRobert Love struct fc_exch *ep; 151942e9a92fSRobert Love struct fc_exch *next; 152042e9a92fSRobert Love 1521b2f0091fSVasu Dev spin_lock_bh(&pool->lock); 152242e9a92fSRobert Love restart: 1523b2f0091fSVasu Dev list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) { 1524b2f0091fSVasu Dev if ((lport == ep->lp) && 152552ff878cSVasu Dev (sid == 0 || sid == ep->sid) && 152642e9a92fSRobert Love (did == 0 || did == ep->did)) { 152742e9a92fSRobert Love fc_exch_hold(ep); 1528b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 152942e9a92fSRobert Love 153042e9a92fSRobert Love fc_exch_reset(ep); 153142e9a92fSRobert Love 153242e9a92fSRobert Love fc_exch_release(ep); 1533b2f0091fSVasu Dev spin_lock_bh(&pool->lock); 153442e9a92fSRobert Love 153542e9a92fSRobert Love /* 153652ff878cSVasu Dev * must restart loop incase while lock 153752ff878cSVasu Dev * was down multiple eps were released. 153842e9a92fSRobert Love */ 153942e9a92fSRobert Love goto restart; 154042e9a92fSRobert Love } 154142e9a92fSRobert Love } 1542b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 1543b2f0091fSVasu Dev } 1544b2f0091fSVasu Dev 1545b2f0091fSVasu Dev /** 1546b2f0091fSVasu Dev * fc_exch_mgr_reset() - Resets all EMs of a lport 1547b2f0091fSVasu Dev * @lport: ptr to the local port 1548b2f0091fSVasu Dev * @sid: source FC ID 1549b2f0091fSVasu Dev * @did: destination FC ID 1550b2f0091fSVasu Dev * 1551b2f0091fSVasu Dev * Reset all EMs of a lport, releasing its all sequences and 1552b2f0091fSVasu Dev * exchanges. If sid is non-zero, then reset only exchanges 1553b2f0091fSVasu Dev * we sourced from that FID. If did is non-zero, reset only 1554b2f0091fSVasu Dev * exchanges destined to that FID. 1555b2f0091fSVasu Dev */ 1556b2f0091fSVasu Dev void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did) 1557b2f0091fSVasu Dev { 1558b2f0091fSVasu Dev struct fc_exch_mgr_anchor *ema; 1559b2f0091fSVasu Dev unsigned int cpu; 1560b2f0091fSVasu Dev 1561b2f0091fSVasu Dev list_for_each_entry(ema, &lport->ema_list, ema_list) { 1562b2f0091fSVasu Dev for_each_possible_cpu(cpu) 1563b2f0091fSVasu Dev fc_exch_pool_reset(lport, 1564b2f0091fSVasu Dev per_cpu_ptr(ema->mp->pool, cpu), 1565b2f0091fSVasu Dev sid, did); 156642e9a92fSRobert Love } 156752ff878cSVasu Dev } 156842e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_reset); 156942e9a92fSRobert Love 157042e9a92fSRobert Love /* 157142e9a92fSRobert Love * Handle incoming ELS REC - Read Exchange Concise. 157242e9a92fSRobert Love * Note that the requesting port may be different than the S_ID in the request. 157342e9a92fSRobert Love */ 157442e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp) 157542e9a92fSRobert Love { 157642e9a92fSRobert Love struct fc_frame *fp; 157742e9a92fSRobert Love struct fc_exch *ep; 157842e9a92fSRobert Love struct fc_exch_mgr *em; 157942e9a92fSRobert Love struct fc_els_rec *rp; 158042e9a92fSRobert Love struct fc_els_rec_acc *acc; 158142e9a92fSRobert Love enum fc_els_rjt_reason reason = ELS_RJT_LOGIC; 158242e9a92fSRobert Love enum fc_els_rjt_explan explan; 158342e9a92fSRobert Love u32 sid; 158442e9a92fSRobert Love u16 rxid; 158542e9a92fSRobert Love u16 oxid; 158642e9a92fSRobert Love 158742e9a92fSRobert Love rp = fc_frame_payload_get(rfp, sizeof(*rp)); 158842e9a92fSRobert Love explan = ELS_EXPL_INV_LEN; 158942e9a92fSRobert Love if (!rp) 159042e9a92fSRobert Love goto reject; 159142e9a92fSRobert Love sid = ntoh24(rp->rec_s_id); 159242e9a92fSRobert Love rxid = ntohs(rp->rec_rx_id); 159342e9a92fSRobert Love oxid = ntohs(rp->rec_ox_id); 159442e9a92fSRobert Love 159542e9a92fSRobert Love /* 159642e9a92fSRobert Love * Currently it's hard to find the local S_ID from the exchange 159742e9a92fSRobert Love * manager. This will eventually be fixed, but for now it's easier 159842e9a92fSRobert Love * to lookup the subject exchange twice, once as if we were 159942e9a92fSRobert Love * the initiator, and then again if we weren't. 160042e9a92fSRobert Love */ 160142e9a92fSRobert Love em = fc_seq_exch(sp)->em; 160242e9a92fSRobert Love ep = fc_exch_find(em, oxid); 160342e9a92fSRobert Love explan = ELS_EXPL_OXID_RXID; 160442e9a92fSRobert Love if (ep && ep->oid == sid) { 160542e9a92fSRobert Love if (ep->rxid != FC_XID_UNKNOWN && 160642e9a92fSRobert Love rxid != FC_XID_UNKNOWN && 160742e9a92fSRobert Love ep->rxid != rxid) 160842e9a92fSRobert Love goto rel; 160942e9a92fSRobert Love } else { 161042e9a92fSRobert Love if (ep) 161142e9a92fSRobert Love fc_exch_release(ep); 161242e9a92fSRobert Love ep = NULL; 161342e9a92fSRobert Love if (rxid != FC_XID_UNKNOWN) 161442e9a92fSRobert Love ep = fc_exch_find(em, rxid); 161542e9a92fSRobert Love if (!ep) 161642e9a92fSRobert Love goto reject; 161742e9a92fSRobert Love } 161842e9a92fSRobert Love 161942e9a92fSRobert Love fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc)); 162042e9a92fSRobert Love if (!fp) { 162142e9a92fSRobert Love fc_exch_done(sp); 162242e9a92fSRobert Love goto out; 162342e9a92fSRobert Love } 162442e9a92fSRobert Love sp = fc_seq_start_next(sp); 162542e9a92fSRobert Love acc = fc_frame_payload_get(fp, sizeof(*acc)); 162642e9a92fSRobert Love memset(acc, 0, sizeof(*acc)); 162742e9a92fSRobert Love acc->reca_cmd = ELS_LS_ACC; 162842e9a92fSRobert Love acc->reca_ox_id = rp->rec_ox_id; 162942e9a92fSRobert Love memcpy(acc->reca_ofid, rp->rec_s_id, 3); 163042e9a92fSRobert Love acc->reca_rx_id = htons(ep->rxid); 163142e9a92fSRobert Love if (ep->sid == ep->oid) 163242e9a92fSRobert Love hton24(acc->reca_rfid, ep->did); 163342e9a92fSRobert Love else 163442e9a92fSRobert Love hton24(acc->reca_rfid, ep->sid); 163542e9a92fSRobert Love acc->reca_fc4value = htonl(ep->seq.rec_data); 163642e9a92fSRobert Love acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP | 163742e9a92fSRobert Love ESB_ST_SEQ_INIT | 163842e9a92fSRobert Love ESB_ST_COMPLETE)); 163942e9a92fSRobert Love sp = fc_seq_start_next(sp); 164042e9a92fSRobert Love fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS); 164142e9a92fSRobert Love out: 164242e9a92fSRobert Love fc_exch_release(ep); 164342e9a92fSRobert Love fc_frame_free(rfp); 164442e9a92fSRobert Love return; 164542e9a92fSRobert Love 164642e9a92fSRobert Love rel: 164742e9a92fSRobert Love fc_exch_release(ep); 164842e9a92fSRobert Love reject: 164942e9a92fSRobert Love fc_seq_ls_rjt(sp, reason, explan); 165042e9a92fSRobert Love fc_frame_free(rfp); 165142e9a92fSRobert Love } 165242e9a92fSRobert Love 165342e9a92fSRobert Love /* 165442e9a92fSRobert Love * Handle response from RRQ. 165542e9a92fSRobert Love * Not much to do here, really. 165642e9a92fSRobert Love * Should report errors. 165742e9a92fSRobert Love * 165842e9a92fSRobert Love * TODO: fix error handler. 165942e9a92fSRobert Love */ 166042e9a92fSRobert Love static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg) 166142e9a92fSRobert Love { 166242e9a92fSRobert Love struct fc_exch *aborted_ep = arg; 166342e9a92fSRobert Love unsigned int op; 166442e9a92fSRobert Love 166542e9a92fSRobert Love if (IS_ERR(fp)) { 166642e9a92fSRobert Love int err = PTR_ERR(fp); 166742e9a92fSRobert Love 166878342da3SVasu Dev if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT) 166942e9a92fSRobert Love goto cleanup; 16707414705eSRobert Love FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, " 16717414705eSRobert Love "frame error %d\n", err); 167242e9a92fSRobert Love return; 167342e9a92fSRobert Love } 167442e9a92fSRobert Love 167542e9a92fSRobert Love op = fc_frame_payload_op(fp); 167642e9a92fSRobert Love fc_frame_free(fp); 167742e9a92fSRobert Love 167842e9a92fSRobert Love switch (op) { 167942e9a92fSRobert Love case ELS_LS_RJT: 16807414705eSRobert Love FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ"); 168142e9a92fSRobert Love /* fall through */ 168242e9a92fSRobert Love case ELS_LS_ACC: 168342e9a92fSRobert Love goto cleanup; 168442e9a92fSRobert Love default: 16857414705eSRobert Love FC_EXCH_DBG(aborted_ep, "unexpected response op %x " 16867414705eSRobert Love "for RRQ", op); 168742e9a92fSRobert Love return; 168842e9a92fSRobert Love } 168942e9a92fSRobert Love 169042e9a92fSRobert Love cleanup: 169142e9a92fSRobert Love fc_exch_done(&aborted_ep->seq); 169242e9a92fSRobert Love /* drop hold for rec qual */ 169342e9a92fSRobert Love fc_exch_release(aborted_ep); 169442e9a92fSRobert Love } 169542e9a92fSRobert Love 16961a7b75aeSRobert Love 16971a7b75aeSRobert Love /** 16981a7b75aeSRobert Love * This function is for exch_seq_send function pointer in 16991a7b75aeSRobert Love * struct libfc_function_template, see comment block on 17001a7b75aeSRobert Love * exch_seq_send for description of this function. 17011a7b75aeSRobert Love */ 17021a7b75aeSRobert Love static struct fc_seq *fc_exch_seq_send(struct fc_lport *lp, 17031a7b75aeSRobert Love struct fc_frame *fp, 17041a7b75aeSRobert Love void (*resp)(struct fc_seq *, 17051a7b75aeSRobert Love struct fc_frame *fp, 17061a7b75aeSRobert Love void *arg), 17071a7b75aeSRobert Love void (*destructor)(struct fc_seq *, 17081a7b75aeSRobert Love void *), 17091a7b75aeSRobert Love void *arg, u32 timer_msec) 17101a7b75aeSRobert Love { 17111a7b75aeSRobert Love struct fc_exch *ep; 17121a7b75aeSRobert Love struct fc_seq *sp = NULL; 17131a7b75aeSRobert Love struct fc_frame_header *fh; 17141a7b75aeSRobert Love int rc = 1; 17151a7b75aeSRobert Love 17161a7b75aeSRobert Love ep = fc_exch_alloc(lp, fp); 17171a7b75aeSRobert Love if (!ep) { 17181a7b75aeSRobert Love fc_frame_free(fp); 17191a7b75aeSRobert Love return NULL; 17201a7b75aeSRobert Love } 17211a7b75aeSRobert Love ep->esb_stat |= ESB_ST_SEQ_INIT; 17221a7b75aeSRobert Love fh = fc_frame_header_get(fp); 17231a7b75aeSRobert Love fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id)); 17241a7b75aeSRobert Love ep->resp = resp; 17251a7b75aeSRobert Love ep->destructor = destructor; 17261a7b75aeSRobert Love ep->arg = arg; 17271a7b75aeSRobert Love ep->r_a_tov = FC_DEF_R_A_TOV; 17281a7b75aeSRobert Love ep->lp = lp; 17291a7b75aeSRobert Love sp = &ep->seq; 17301a7b75aeSRobert Love 17311a7b75aeSRobert Love ep->fh_type = fh->fh_type; /* save for possbile timeout handling */ 17321a7b75aeSRobert Love ep->f_ctl = ntoh24(fh->fh_f_ctl); 17331a7b75aeSRobert Love fc_exch_setup_hdr(ep, fp, ep->f_ctl); 17341a7b75aeSRobert Love sp->cnt++; 17351a7b75aeSRobert Love 17361a7b75aeSRobert Love if (ep->xid <= lp->lro_xid) 17371a7b75aeSRobert Love fc_fcp_ddp_setup(fr_fsp(fp), ep->xid); 17381a7b75aeSRobert Love 17391a7b75aeSRobert Love if (unlikely(lp->tt.frame_send(lp, fp))) 17401a7b75aeSRobert Love goto err; 17411a7b75aeSRobert Love 17421a7b75aeSRobert Love if (timer_msec) 17431a7b75aeSRobert Love fc_exch_timer_set_locked(ep, timer_msec); 17441a7b75aeSRobert Love ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */ 17451a7b75aeSRobert Love 17461a7b75aeSRobert Love if (ep->f_ctl & FC_FC_SEQ_INIT) 17471a7b75aeSRobert Love ep->esb_stat &= ~ESB_ST_SEQ_INIT; 17481a7b75aeSRobert Love spin_unlock_bh(&ep->ex_lock); 17491a7b75aeSRobert Love return sp; 17501a7b75aeSRobert Love err: 17511a7b75aeSRobert Love rc = fc_exch_done_locked(ep); 17521a7b75aeSRobert Love spin_unlock_bh(&ep->ex_lock); 17531a7b75aeSRobert Love if (!rc) 17541a7b75aeSRobert Love fc_exch_delete(ep); 17551a7b75aeSRobert Love return NULL; 17561a7b75aeSRobert Love } 17571a7b75aeSRobert Love 175842e9a92fSRobert Love /* 175942e9a92fSRobert Love * Send ELS RRQ - Reinstate Recovery Qualifier. 176042e9a92fSRobert Love * This tells the remote port to stop blocking the use of 176142e9a92fSRobert Love * the exchange and the seq_cnt range. 176242e9a92fSRobert Love */ 176342e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *ep) 176442e9a92fSRobert Love { 176542e9a92fSRobert Love struct fc_lport *lp; 176642e9a92fSRobert Love struct fc_els_rrq *rrq; 176742e9a92fSRobert Love struct fc_frame *fp; 176842e9a92fSRobert Love u32 did; 176942e9a92fSRobert Love 177042e9a92fSRobert Love lp = ep->lp; 177142e9a92fSRobert Love 177242e9a92fSRobert Love fp = fc_frame_alloc(lp, sizeof(*rrq)); 177342e9a92fSRobert Love if (!fp) 1774a0cc1eccSVasu Dev goto retry; 1775a0cc1eccSVasu Dev 177642e9a92fSRobert Love rrq = fc_frame_payload_get(fp, sizeof(*rrq)); 177742e9a92fSRobert Love memset(rrq, 0, sizeof(*rrq)); 177842e9a92fSRobert Love rrq->rrq_cmd = ELS_RRQ; 177942e9a92fSRobert Love hton24(rrq->rrq_s_id, ep->sid); 178042e9a92fSRobert Love rrq->rrq_ox_id = htons(ep->oxid); 178142e9a92fSRobert Love rrq->rrq_rx_id = htons(ep->rxid); 178242e9a92fSRobert Love 178342e9a92fSRobert Love did = ep->did; 178442e9a92fSRobert Love if (ep->esb_stat & ESB_ST_RESP) 178542e9a92fSRobert Love did = ep->sid; 178642e9a92fSRobert Love 178742e9a92fSRobert Love fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did, 178842e9a92fSRobert Love fc_host_port_id(lp->host), FC_TYPE_ELS, 178942e9a92fSRobert Love FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0); 179042e9a92fSRobert Love 1791a0cc1eccSVasu Dev if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov)) 1792a0cc1eccSVasu Dev return; 1793a0cc1eccSVasu Dev 1794a0cc1eccSVasu Dev retry: 1795a0cc1eccSVasu Dev spin_lock_bh(&ep->ex_lock); 1796a0cc1eccSVasu Dev if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) { 1797a0cc1eccSVasu Dev spin_unlock_bh(&ep->ex_lock); 1798a0cc1eccSVasu Dev /* drop hold for rec qual */ 1799a0cc1eccSVasu Dev fc_exch_release(ep); 180042e9a92fSRobert Love return; 180142e9a92fSRobert Love } 1802a0cc1eccSVasu Dev ep->esb_stat |= ESB_ST_REC_QUAL; 1803a0cc1eccSVasu Dev fc_exch_timer_set_locked(ep, ep->r_a_tov); 1804a0cc1eccSVasu Dev spin_unlock_bh(&ep->ex_lock); 180542e9a92fSRobert Love } 180642e9a92fSRobert Love 180742e9a92fSRobert Love 180842e9a92fSRobert Love /* 180942e9a92fSRobert Love * Handle incoming ELS RRQ - Reset Recovery Qualifier. 181042e9a92fSRobert Love */ 181142e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp) 181242e9a92fSRobert Love { 18133f127ad9SVasu Dev struct fc_exch *ep = NULL; /* request or subject exchange */ 181442e9a92fSRobert Love struct fc_els_rrq *rp; 181542e9a92fSRobert Love u32 sid; 181642e9a92fSRobert Love u16 xid; 181742e9a92fSRobert Love enum fc_els_rjt_explan explan; 181842e9a92fSRobert Love 181942e9a92fSRobert Love rp = fc_frame_payload_get(fp, sizeof(*rp)); 182042e9a92fSRobert Love explan = ELS_EXPL_INV_LEN; 182142e9a92fSRobert Love if (!rp) 182242e9a92fSRobert Love goto reject; 182342e9a92fSRobert Love 182442e9a92fSRobert Love /* 182542e9a92fSRobert Love * lookup subject exchange. 182642e9a92fSRobert Love */ 182742e9a92fSRobert Love ep = fc_seq_exch(sp); 182842e9a92fSRobert Love sid = ntoh24(rp->rrq_s_id); /* subject source */ 182942e9a92fSRobert Love xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id); 183042e9a92fSRobert Love ep = fc_exch_find(ep->em, xid); 183142e9a92fSRobert Love 183242e9a92fSRobert Love explan = ELS_EXPL_OXID_RXID; 183342e9a92fSRobert Love if (!ep) 183442e9a92fSRobert Love goto reject; 183542e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 183642e9a92fSRobert Love if (ep->oxid != ntohs(rp->rrq_ox_id)) 183742e9a92fSRobert Love goto unlock_reject; 183842e9a92fSRobert Love if (ep->rxid != ntohs(rp->rrq_rx_id) && 183942e9a92fSRobert Love ep->rxid != FC_XID_UNKNOWN) 184042e9a92fSRobert Love goto unlock_reject; 184142e9a92fSRobert Love explan = ELS_EXPL_SID; 184242e9a92fSRobert Love if (ep->sid != sid) 184342e9a92fSRobert Love goto unlock_reject; 184442e9a92fSRobert Love 184542e9a92fSRobert Love /* 184642e9a92fSRobert Love * Clear Recovery Qualifier state, and cancel timer if complete. 184742e9a92fSRobert Love */ 184842e9a92fSRobert Love if (ep->esb_stat & ESB_ST_REC_QUAL) { 184942e9a92fSRobert Love ep->esb_stat &= ~ESB_ST_REC_QUAL; 185042e9a92fSRobert Love atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */ 185142e9a92fSRobert Love } 185242e9a92fSRobert Love if (ep->esb_stat & ESB_ST_COMPLETE) { 185342e9a92fSRobert Love if (cancel_delayed_work(&ep->timeout_work)) 185442e9a92fSRobert Love atomic_dec(&ep->ex_refcnt); /* drop timer hold */ 185542e9a92fSRobert Love } 185642e9a92fSRobert Love 185742e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 185842e9a92fSRobert Love 185942e9a92fSRobert Love /* 186042e9a92fSRobert Love * Send LS_ACC. 186142e9a92fSRobert Love */ 186242e9a92fSRobert Love fc_seq_ls_acc(sp); 18633f127ad9SVasu Dev goto out; 186442e9a92fSRobert Love 186542e9a92fSRobert Love unlock_reject: 186642e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 186742e9a92fSRobert Love reject: 186842e9a92fSRobert Love fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan); 18693f127ad9SVasu Dev out: 187042e9a92fSRobert Love fc_frame_free(fp); 18713f127ad9SVasu Dev if (ep) 18723f127ad9SVasu Dev fc_exch_release(ep); /* drop hold from fc_exch_find */ 187342e9a92fSRobert Love } 187442e9a92fSRobert Love 187596316099SVasu Dev struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport, 187696316099SVasu Dev struct fc_exch_mgr *mp, 187796316099SVasu Dev bool (*match)(struct fc_frame *)) 187896316099SVasu Dev { 187996316099SVasu Dev struct fc_exch_mgr_anchor *ema; 188096316099SVasu Dev 188196316099SVasu Dev ema = kmalloc(sizeof(*ema), GFP_ATOMIC); 188296316099SVasu Dev if (!ema) 188396316099SVasu Dev return ema; 188496316099SVasu Dev 188596316099SVasu Dev ema->mp = mp; 188696316099SVasu Dev ema->match = match; 188796316099SVasu Dev /* add EM anchor to EM anchors list */ 188896316099SVasu Dev list_add_tail(&ema->ema_list, &lport->ema_list); 188996316099SVasu Dev kref_get(&mp->kref); 189096316099SVasu Dev return ema; 189196316099SVasu Dev } 189296316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_add); 189396316099SVasu Dev 189496316099SVasu Dev static void fc_exch_mgr_destroy(struct kref *kref) 189596316099SVasu Dev { 189696316099SVasu Dev struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref); 189796316099SVasu Dev 189896316099SVasu Dev mempool_destroy(mp->ep_pool); 1899e4bc50beSVasu Dev free_percpu(mp->pool); 190096316099SVasu Dev kfree(mp); 190196316099SVasu Dev } 190296316099SVasu Dev 190396316099SVasu Dev void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema) 190496316099SVasu Dev { 190596316099SVasu Dev /* remove EM anchor from EM anchors list */ 190696316099SVasu Dev list_del(&ema->ema_list); 190796316099SVasu Dev kref_put(&ema->mp->kref, fc_exch_mgr_destroy); 190896316099SVasu Dev kfree(ema); 190996316099SVasu Dev } 191096316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_del); 191196316099SVasu Dev 1912*174e1ebfSChris Leech /** 1913*174e1ebfSChris Leech * fc_exch_mgr_list_clone() - share all exchange manager objects 1914*174e1ebfSChris Leech * @src: source lport to clone exchange managers from 1915*174e1ebfSChris Leech * @dst: new lport that takes references to all the exchange managers 1916*174e1ebfSChris Leech */ 1917*174e1ebfSChris Leech int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst) 1918*174e1ebfSChris Leech { 1919*174e1ebfSChris Leech struct fc_exch_mgr_anchor *ema, *tmp; 1920*174e1ebfSChris Leech 1921*174e1ebfSChris Leech list_for_each_entry(ema, &src->ema_list, ema_list) { 1922*174e1ebfSChris Leech if (!fc_exch_mgr_add(dst, ema->mp, ema->match)) 1923*174e1ebfSChris Leech goto err; 1924*174e1ebfSChris Leech } 1925*174e1ebfSChris Leech return 0; 1926*174e1ebfSChris Leech err: 1927*174e1ebfSChris Leech list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list) 1928*174e1ebfSChris Leech fc_exch_mgr_del(ema); 1929*174e1ebfSChris Leech return -ENOMEM; 1930*174e1ebfSChris Leech } 1931*174e1ebfSChris Leech 193242e9a92fSRobert Love struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp, 193342e9a92fSRobert Love enum fc_class class, 193452ff878cSVasu Dev u16 min_xid, u16 max_xid, 193552ff878cSVasu Dev bool (*match)(struct fc_frame *)) 193642e9a92fSRobert Love { 193742e9a92fSRobert Love struct fc_exch_mgr *mp; 1938e4bc50beSVasu Dev u16 pool_exch_range; 1939e4bc50beSVasu Dev size_t pool_size; 1940e4bc50beSVasu Dev unsigned int cpu; 1941e4bc50beSVasu Dev struct fc_exch_pool *pool; 194242e9a92fSRobert Love 1943e4bc50beSVasu Dev if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN || 1944e4bc50beSVasu Dev (min_xid & fc_cpu_mask) != 0) { 19457414705eSRobert Love FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n", 194642e9a92fSRobert Love min_xid, max_xid); 194742e9a92fSRobert Love return NULL; 194842e9a92fSRobert Love } 194942e9a92fSRobert Love 195042e9a92fSRobert Love /* 1951b2f0091fSVasu Dev * allocate memory for EM 195242e9a92fSRobert Love */ 1953b2f0091fSVasu Dev mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC); 195442e9a92fSRobert Love if (!mp) 195542e9a92fSRobert Love return NULL; 195642e9a92fSRobert Love 195742e9a92fSRobert Love mp->class = class; 195842e9a92fSRobert Love /* adjust em exch xid range for offload */ 195942e9a92fSRobert Love mp->min_xid = min_xid; 196042e9a92fSRobert Love mp->max_xid = max_xid; 196142e9a92fSRobert Love 196242e9a92fSRobert Love mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep); 196342e9a92fSRobert Love if (!mp->ep_pool) 196442e9a92fSRobert Love goto free_mp; 196542e9a92fSRobert Love 1966e4bc50beSVasu Dev /* 1967e4bc50beSVasu Dev * Setup per cpu exch pool with entire exchange id range equally 1968e4bc50beSVasu Dev * divided across all cpus. The exch pointers array memory is 1969e4bc50beSVasu Dev * allocated for exch range per pool. 1970e4bc50beSVasu Dev */ 1971e4bc50beSVasu Dev pool_exch_range = (mp->max_xid - mp->min_xid + 1) / (fc_cpu_mask + 1); 1972e4bc50beSVasu Dev mp->pool_max_index = pool_exch_range - 1; 1973e4bc50beSVasu Dev 1974e4bc50beSVasu Dev /* 1975e4bc50beSVasu Dev * Allocate and initialize per cpu exch pool 1976e4bc50beSVasu Dev */ 1977e4bc50beSVasu Dev pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *); 1978e4bc50beSVasu Dev mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool)); 1979e4bc50beSVasu Dev if (!mp->pool) 1980e4bc50beSVasu Dev goto free_mempool; 1981e4bc50beSVasu Dev for_each_possible_cpu(cpu) { 1982e4bc50beSVasu Dev pool = per_cpu_ptr(mp->pool, cpu); 1983e4bc50beSVasu Dev spin_lock_init(&pool->lock); 1984e4bc50beSVasu Dev INIT_LIST_HEAD(&pool->ex_list); 1985e4bc50beSVasu Dev } 1986e4bc50beSVasu Dev 198752ff878cSVasu Dev kref_init(&mp->kref); 198852ff878cSVasu Dev if (!fc_exch_mgr_add(lp, mp, match)) { 1989e4bc50beSVasu Dev free_percpu(mp->pool); 1990e4bc50beSVasu Dev goto free_mempool; 199152ff878cSVasu Dev } 199252ff878cSVasu Dev 199352ff878cSVasu Dev /* 199452ff878cSVasu Dev * Above kref_init() sets mp->kref to 1 and then 199552ff878cSVasu Dev * call to fc_exch_mgr_add incremented mp->kref again, 199652ff878cSVasu Dev * so adjust that extra increment. 199752ff878cSVasu Dev */ 199852ff878cSVasu Dev kref_put(&mp->kref, fc_exch_mgr_destroy); 199942e9a92fSRobert Love return mp; 200042e9a92fSRobert Love 2001e4bc50beSVasu Dev free_mempool: 2002e4bc50beSVasu Dev mempool_destroy(mp->ep_pool); 200342e9a92fSRobert Love free_mp: 200442e9a92fSRobert Love kfree(mp); 200542e9a92fSRobert Love return NULL; 200642e9a92fSRobert Love } 200742e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_alloc); 200842e9a92fSRobert Love 200952ff878cSVasu Dev void fc_exch_mgr_free(struct fc_lport *lport) 201042e9a92fSRobert Love { 201152ff878cSVasu Dev struct fc_exch_mgr_anchor *ema, *next; 201252ff878cSVasu Dev 201352ff878cSVasu Dev list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list) 201452ff878cSVasu Dev fc_exch_mgr_del(ema); 201542e9a92fSRobert Love } 201642e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_free); 201742e9a92fSRobert Love 201842e9a92fSRobert Love /* 201942e9a92fSRobert Love * Receive a frame 202042e9a92fSRobert Love */ 202152ff878cSVasu Dev void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp) 202242e9a92fSRobert Love { 202342e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 202452ff878cSVasu Dev struct fc_exch_mgr_anchor *ema; 202552ff878cSVasu Dev u32 f_ctl, found = 0; 202652ff878cSVasu Dev u16 oxid; 202742e9a92fSRobert Love 202842e9a92fSRobert Love /* lport lock ? */ 202952ff878cSVasu Dev if (!lp || lp->state == LPORT_ST_DISABLED) { 20307414705eSRobert Love FC_LPORT_DBG(lp, "Receiving frames for an lport that " 20317414705eSRobert Love "has not been initialized correctly\n"); 203242e9a92fSRobert Love fc_frame_free(fp); 203342e9a92fSRobert Love return; 203442e9a92fSRobert Love } 203542e9a92fSRobert Love 203652ff878cSVasu Dev f_ctl = ntoh24(fh->fh_f_ctl); 203752ff878cSVasu Dev oxid = ntohs(fh->fh_ox_id); 203852ff878cSVasu Dev if (f_ctl & FC_FC_EX_CTX) { 203952ff878cSVasu Dev list_for_each_entry(ema, &lp->ema_list, ema_list) { 204052ff878cSVasu Dev if ((oxid >= ema->mp->min_xid) && 204152ff878cSVasu Dev (oxid <= ema->mp->max_xid)) { 204252ff878cSVasu Dev found = 1; 204352ff878cSVasu Dev break; 204452ff878cSVasu Dev } 204552ff878cSVasu Dev } 204652ff878cSVasu Dev 204752ff878cSVasu Dev if (!found) { 204852ff878cSVasu Dev FC_LPORT_DBG(lp, "Received response for out " 204952ff878cSVasu Dev "of range oxid:%hx\n", oxid); 205052ff878cSVasu Dev fc_frame_free(fp); 205152ff878cSVasu Dev return; 205252ff878cSVasu Dev } 205352ff878cSVasu Dev } else 205452ff878cSVasu Dev ema = list_entry(lp->ema_list.prev, typeof(*ema), ema_list); 205552ff878cSVasu Dev 205642e9a92fSRobert Love /* 205742e9a92fSRobert Love * If frame is marked invalid, just drop it. 205842e9a92fSRobert Love */ 205942e9a92fSRobert Love switch (fr_eof(fp)) { 206042e9a92fSRobert Love case FC_EOF_T: 206142e9a92fSRobert Love if (f_ctl & FC_FC_END_SEQ) 206242e9a92fSRobert Love skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl)); 206342e9a92fSRobert Love /* fall through */ 206442e9a92fSRobert Love case FC_EOF_N: 206542e9a92fSRobert Love if (fh->fh_type == FC_TYPE_BLS) 206652ff878cSVasu Dev fc_exch_recv_bls(ema->mp, fp); 206742e9a92fSRobert Love else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) == 206842e9a92fSRobert Love FC_FC_EX_CTX) 206952ff878cSVasu Dev fc_exch_recv_seq_resp(ema->mp, fp); 207042e9a92fSRobert Love else if (f_ctl & FC_FC_SEQ_CTX) 207152ff878cSVasu Dev fc_exch_recv_resp(ema->mp, fp); 207242e9a92fSRobert Love else 207352ff878cSVasu Dev fc_exch_recv_req(lp, ema->mp, fp); 207442e9a92fSRobert Love break; 207542e9a92fSRobert Love default: 2076d459b7eaSRobert Love FC_LPORT_DBG(lp, "dropping invalid frame (eof %x)", fr_eof(fp)); 207742e9a92fSRobert Love fc_frame_free(fp); 207842e9a92fSRobert Love } 207942e9a92fSRobert Love } 208042e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_recv); 208142e9a92fSRobert Love 208242e9a92fSRobert Love int fc_exch_init(struct fc_lport *lp) 208342e9a92fSRobert Love { 208442e9a92fSRobert Love if (!lp->tt.seq_start_next) 208542e9a92fSRobert Love lp->tt.seq_start_next = fc_seq_start_next; 208642e9a92fSRobert Love 208742e9a92fSRobert Love if (!lp->tt.exch_seq_send) 208842e9a92fSRobert Love lp->tt.exch_seq_send = fc_exch_seq_send; 208942e9a92fSRobert Love 209042e9a92fSRobert Love if (!lp->tt.seq_send) 209142e9a92fSRobert Love lp->tt.seq_send = fc_seq_send; 209242e9a92fSRobert Love 209342e9a92fSRobert Love if (!lp->tt.seq_els_rsp_send) 209442e9a92fSRobert Love lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send; 209542e9a92fSRobert Love 209642e9a92fSRobert Love if (!lp->tt.exch_done) 209742e9a92fSRobert Love lp->tt.exch_done = fc_exch_done; 209842e9a92fSRobert Love 209942e9a92fSRobert Love if (!lp->tt.exch_mgr_reset) 210042e9a92fSRobert Love lp->tt.exch_mgr_reset = fc_exch_mgr_reset; 210142e9a92fSRobert Love 210242e9a92fSRobert Love if (!lp->tt.seq_exch_abort) 210342e9a92fSRobert Love lp->tt.seq_exch_abort = fc_seq_exch_abort; 210442e9a92fSRobert Love 210589f19a59SVasu Dev return 0; 210689f19a59SVasu Dev } 210789f19a59SVasu Dev EXPORT_SYMBOL(fc_exch_init); 210889f19a59SVasu Dev 210989f19a59SVasu Dev /** 211089f19a59SVasu Dev * fc_setup_exch_mgr() - Setup an exchange manager 211189f19a59SVasu Dev */ 211289f19a59SVasu Dev int fc_setup_exch_mgr() 211389f19a59SVasu Dev { 211489f19a59SVasu Dev fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch), 211589f19a59SVasu Dev 0, SLAB_HWCACHE_ALIGN, NULL); 211689f19a59SVasu Dev if (!fc_em_cachep) 211789f19a59SVasu Dev return -ENOMEM; 211889f19a59SVasu Dev 2119e4bc50beSVasu Dev /* 2120e4bc50beSVasu Dev * Initialize fc_cpu_mask and fc_cpu_order. The 2121e4bc50beSVasu Dev * fc_cpu_mask is set for nr_cpu_ids rounded up 2122e4bc50beSVasu Dev * to order of 2's * power and order is stored 2123e4bc50beSVasu Dev * in fc_cpu_order as this is later required in 2124e4bc50beSVasu Dev * mapping between an exch id and exch array index 2125e4bc50beSVasu Dev * in per cpu exch pool. 2126e4bc50beSVasu Dev * 2127e4bc50beSVasu Dev * This round up is required to align fc_cpu_mask 2128e4bc50beSVasu Dev * to exchange id's lower bits such that all incoming 2129e4bc50beSVasu Dev * frames of an exchange gets delivered to the same 2130e4bc50beSVasu Dev * cpu on which exchange originated by simple bitwise 2131e4bc50beSVasu Dev * AND operation between fc_cpu_mask and exchange id. 2132e4bc50beSVasu Dev */ 2133e4bc50beSVasu Dev fc_cpu_mask = 1; 2134e4bc50beSVasu Dev fc_cpu_order = 0; 2135e4bc50beSVasu Dev while (fc_cpu_mask < nr_cpu_ids) { 2136e4bc50beSVasu Dev fc_cpu_mask <<= 1; 2137e4bc50beSVasu Dev fc_cpu_order++; 2138e4bc50beSVasu Dev } 2139e4bc50beSVasu Dev fc_cpu_mask--; 2140e4bc50beSVasu Dev 214142e9a92fSRobert Love return 0; 214242e9a92fSRobert Love } 214342e9a92fSRobert Love 214442e9a92fSRobert Love void fc_destroy_exch_mgr(void) 214542e9a92fSRobert Love { 214642e9a92fSRobert Love kmem_cache_destroy(fc_em_cachep); 214742e9a92fSRobert Love } 2148