142e9a92fSRobert Love /* 242e9a92fSRobert Love * Copyright(c) 2007 Intel Corporation. All rights reserved. 342e9a92fSRobert Love * Copyright(c) 2008 Red Hat, Inc. All rights reserved. 442e9a92fSRobert Love * Copyright(c) 2008 Mike Christie 542e9a92fSRobert Love * 642e9a92fSRobert Love * This program is free software; you can redistribute it and/or modify it 742e9a92fSRobert Love * under the terms and conditions of the GNU General Public License, 842e9a92fSRobert Love * version 2, as published by the Free Software Foundation. 942e9a92fSRobert Love * 1042e9a92fSRobert Love * This program is distributed in the hope it will be useful, but WITHOUT 1142e9a92fSRobert Love * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1242e9a92fSRobert Love * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1342e9a92fSRobert Love * more details. 1442e9a92fSRobert Love * 1542e9a92fSRobert Love * You should have received a copy of the GNU General Public License along with 1642e9a92fSRobert Love * this program; if not, write to the Free Software Foundation, Inc., 1742e9a92fSRobert Love * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 1842e9a92fSRobert Love * 1942e9a92fSRobert Love * Maintained at www.Open-FCoE.org 2042e9a92fSRobert Love */ 2142e9a92fSRobert Love 2242e9a92fSRobert Love /* 2342e9a92fSRobert Love * Fibre Channel exchange and sequence handling. 2442e9a92fSRobert Love */ 2542e9a92fSRobert Love 2642e9a92fSRobert Love #include <linux/timer.h> 2742e9a92fSRobert Love #include <linux/gfp.h> 2842e9a92fSRobert Love #include <linux/err.h> 2942e9a92fSRobert Love 3042e9a92fSRobert Love #include <scsi/fc/fc_fc2.h> 3142e9a92fSRobert Love 3242e9a92fSRobert Love #include <scsi/libfc.h> 3342e9a92fSRobert Love #include <scsi/fc_encode.h> 3442e9a92fSRobert Love 35e4bc50beSVasu Dev u16 fc_cpu_mask; /* cpu mask for possible cpus */ 36e4bc50beSVasu Dev EXPORT_SYMBOL(fc_cpu_mask); 37e4bc50beSVasu Dev static u16 fc_cpu_order; /* 2's power to represent total possible cpus */ 3842e9a92fSRobert Love static struct kmem_cache *fc_em_cachep; /* cache for exchanges */ 3942e9a92fSRobert Love 4042e9a92fSRobert Love /* 4142e9a92fSRobert Love * Structure and function definitions for managing Fibre Channel Exchanges 4242e9a92fSRobert Love * and Sequences. 4342e9a92fSRobert Love * 4442e9a92fSRobert Love * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq. 4542e9a92fSRobert Love * 4642e9a92fSRobert Love * fc_exch_mgr holds the exchange state for an N port 4742e9a92fSRobert Love * 4842e9a92fSRobert Love * fc_exch holds state for one exchange and links to its active sequence. 4942e9a92fSRobert Love * 5042e9a92fSRobert Love * fc_seq holds the state for an individual sequence. 5142e9a92fSRobert Love */ 5242e9a92fSRobert Love 5342e9a92fSRobert Love /* 54e4bc50beSVasu Dev * Per cpu exchange pool 55e4bc50beSVasu Dev * 56e4bc50beSVasu Dev * This structure manages per cpu exchanges in array of exchange pointers. 57e4bc50beSVasu Dev * This array is allocated followed by struct fc_exch_pool memory for 58e4bc50beSVasu Dev * assigned range of exchanges to per cpu pool. 59e4bc50beSVasu Dev */ 60e4bc50beSVasu Dev struct fc_exch_pool { 61e4bc50beSVasu Dev u16 next_index; /* next possible free exchange index */ 62e4bc50beSVasu Dev u16 total_exches; /* total allocated exchanges */ 63e4bc50beSVasu Dev spinlock_t lock; /* exch pool lock */ 64e4bc50beSVasu Dev struct list_head ex_list; /* allocated exchanges list */ 65e4bc50beSVasu Dev }; 66e4bc50beSVasu Dev 67e4bc50beSVasu Dev /* 6842e9a92fSRobert Love * Exchange manager. 6942e9a92fSRobert Love * 7042e9a92fSRobert Love * This structure is the center for creating exchanges and sequences. 7142e9a92fSRobert Love * It manages the allocation of exchange IDs. 7242e9a92fSRobert Love */ 7342e9a92fSRobert Love struct fc_exch_mgr { 7442e9a92fSRobert Love enum fc_class class; /* default class for sequences */ 7596316099SVasu Dev struct kref kref; /* exchange mgr reference count */ 7642e9a92fSRobert Love u16 min_xid; /* min exchange ID */ 7742e9a92fSRobert Love u16 max_xid; /* max exchange ID */ 7842e9a92fSRobert Love mempool_t *ep_pool; /* reserve ep's */ 79e4bc50beSVasu Dev u16 pool_max_index; /* max exch array index in exch pool */ 80e4bc50beSVasu Dev struct fc_exch_pool *pool; /* per cpu exch pool */ 8142e9a92fSRobert Love 8242e9a92fSRobert Love /* 8342e9a92fSRobert Love * currently exchange mgr stats are updated but not used. 8442e9a92fSRobert Love * either stats can be expose via sysfs or remove them 8542e9a92fSRobert Love * all together if not used XXX 8642e9a92fSRobert Love */ 8742e9a92fSRobert Love struct { 8842e9a92fSRobert Love atomic_t no_free_exch; 8942e9a92fSRobert Love atomic_t no_free_exch_xid; 9042e9a92fSRobert Love atomic_t xid_not_found; 9142e9a92fSRobert Love atomic_t xid_busy; 9242e9a92fSRobert Love atomic_t seq_not_found; 9342e9a92fSRobert Love atomic_t non_bls_resp; 9442e9a92fSRobert Love } stats; 9542e9a92fSRobert Love }; 9642e9a92fSRobert Love #define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq) 9742e9a92fSRobert Love 9896316099SVasu Dev struct fc_exch_mgr_anchor { 9996316099SVasu Dev struct list_head ema_list; 10096316099SVasu Dev struct fc_exch_mgr *mp; 10196316099SVasu Dev bool (*match)(struct fc_frame *); 10296316099SVasu Dev }; 10396316099SVasu Dev 10442e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *); 10542e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *); 10642e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason, 10742e9a92fSRobert Love enum fc_els_rjt_explan); 10842e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *); 10942e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *); 11042e9a92fSRobert Love 11142e9a92fSRobert Love /* 11242e9a92fSRobert Love * Internal implementation notes. 11342e9a92fSRobert Love * 11442e9a92fSRobert Love * The exchange manager is one by default in libfc but LLD may choose 11542e9a92fSRobert Love * to have one per CPU. The sequence manager is one per exchange manager 11642e9a92fSRobert Love * and currently never separated. 11742e9a92fSRobert Love * 11842e9a92fSRobert Love * Section 9.8 in FC-FS-2 specifies: "The SEQ_ID is a one-byte field 11942e9a92fSRobert Love * assigned by the Sequence Initiator that shall be unique for a specific 12042e9a92fSRobert Love * D_ID and S_ID pair while the Sequence is open." Note that it isn't 12142e9a92fSRobert Love * qualified by exchange ID, which one might think it would be. 12242e9a92fSRobert Love * In practice this limits the number of open sequences and exchanges to 256 12342e9a92fSRobert Love * per session. For most targets we could treat this limit as per exchange. 12442e9a92fSRobert Love * 12542e9a92fSRobert Love * The exchange and its sequence are freed when the last sequence is received. 12642e9a92fSRobert Love * It's possible for the remote port to leave an exchange open without 12742e9a92fSRobert Love * sending any sequences. 12842e9a92fSRobert Love * 12942e9a92fSRobert Love * Notes on reference counts: 13042e9a92fSRobert Love * 13142e9a92fSRobert Love * Exchanges are reference counted and exchange gets freed when the reference 13242e9a92fSRobert Love * count becomes zero. 13342e9a92fSRobert Love * 13442e9a92fSRobert Love * Timeouts: 13542e9a92fSRobert Love * Sequences are timed out for E_D_TOV and R_A_TOV. 13642e9a92fSRobert Love * 13742e9a92fSRobert Love * Sequence event handling: 13842e9a92fSRobert Love * 13942e9a92fSRobert Love * The following events may occur on initiator sequences: 14042e9a92fSRobert Love * 14142e9a92fSRobert Love * Send. 14242e9a92fSRobert Love * For now, the whole thing is sent. 14342e9a92fSRobert Love * Receive ACK 14442e9a92fSRobert Love * This applies only to class F. 14542e9a92fSRobert Love * The sequence is marked complete. 14642e9a92fSRobert Love * ULP completion. 14742e9a92fSRobert Love * The upper layer calls fc_exch_done() when done 14842e9a92fSRobert Love * with exchange and sequence tuple. 14942e9a92fSRobert Love * RX-inferred completion. 15042e9a92fSRobert Love * When we receive the next sequence on the same exchange, we can 15142e9a92fSRobert Love * retire the previous sequence ID. (XXX not implemented). 15242e9a92fSRobert Love * Timeout. 15342e9a92fSRobert Love * R_A_TOV frees the sequence ID. If we're waiting for ACK, 15442e9a92fSRobert Love * E_D_TOV causes abort and calls upper layer response handler 15542e9a92fSRobert Love * with FC_EX_TIMEOUT error. 15642e9a92fSRobert Love * Receive RJT 15742e9a92fSRobert Love * XXX defer. 15842e9a92fSRobert Love * Send ABTS 15942e9a92fSRobert Love * On timeout. 16042e9a92fSRobert Love * 16142e9a92fSRobert Love * The following events may occur on recipient sequences: 16242e9a92fSRobert Love * 16342e9a92fSRobert Love * Receive 16442e9a92fSRobert Love * Allocate sequence for first frame received. 16542e9a92fSRobert Love * Hold during receive handler. 16642e9a92fSRobert Love * Release when final frame received. 16742e9a92fSRobert Love * Keep status of last N of these for the ELS RES command. XXX TBD. 16842e9a92fSRobert Love * Receive ABTS 16942e9a92fSRobert Love * Deallocate sequence 17042e9a92fSRobert Love * Send RJT 17142e9a92fSRobert Love * Deallocate 17242e9a92fSRobert Love * 17342e9a92fSRobert Love * For now, we neglect conditions where only part of a sequence was 17442e9a92fSRobert Love * received or transmitted, or where out-of-order receipt is detected. 17542e9a92fSRobert Love */ 17642e9a92fSRobert Love 17742e9a92fSRobert Love /* 17842e9a92fSRobert Love * Locking notes: 17942e9a92fSRobert Love * 18042e9a92fSRobert Love * The EM code run in a per-CPU worker thread. 18142e9a92fSRobert Love * 18242e9a92fSRobert Love * To protect against concurrency between a worker thread code and timers, 18342e9a92fSRobert Love * sequence allocation and deallocation must be locked. 18442e9a92fSRobert Love * - exchange refcnt can be done atomicly without locks. 18542e9a92fSRobert Love * - sequence allocation must be locked by exch lock. 186b2f0091fSVasu Dev * - If the EM pool lock and ex_lock must be taken at the same time, then the 187b2f0091fSVasu Dev * EM pool lock must be taken before the ex_lock. 18842e9a92fSRobert Love */ 18942e9a92fSRobert Love 19042e9a92fSRobert Love /* 19142e9a92fSRobert Love * opcode names for debugging. 19242e9a92fSRobert Love */ 19342e9a92fSRobert Love static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT; 19442e9a92fSRobert Love 19542e9a92fSRobert Love #define FC_TABLE_SIZE(x) (sizeof(x) / sizeof(x[0])) 19642e9a92fSRobert Love 19742e9a92fSRobert Love static inline const char *fc_exch_name_lookup(unsigned int op, char **table, 19842e9a92fSRobert Love unsigned int max_index) 19942e9a92fSRobert Love { 20042e9a92fSRobert Love const char *name = NULL; 20142e9a92fSRobert Love 20242e9a92fSRobert Love if (op < max_index) 20342e9a92fSRobert Love name = table[op]; 20442e9a92fSRobert Love if (!name) 20542e9a92fSRobert Love name = "unknown"; 20642e9a92fSRobert Love return name; 20742e9a92fSRobert Love } 20842e9a92fSRobert Love 20942e9a92fSRobert Love static const char *fc_exch_rctl_name(unsigned int op) 21042e9a92fSRobert Love { 21142e9a92fSRobert Love return fc_exch_name_lookup(op, fc_exch_rctl_names, 21242e9a92fSRobert Love FC_TABLE_SIZE(fc_exch_rctl_names)); 21342e9a92fSRobert Love } 21442e9a92fSRobert Love 21542e9a92fSRobert Love /* 21642e9a92fSRobert Love * Hold an exchange - keep it from being freed. 21742e9a92fSRobert Love */ 21842e9a92fSRobert Love static void fc_exch_hold(struct fc_exch *ep) 21942e9a92fSRobert Love { 22042e9a92fSRobert Love atomic_inc(&ep->ex_refcnt); 22142e9a92fSRobert Love } 22242e9a92fSRobert Love 22342e9a92fSRobert Love /* 22442e9a92fSRobert Love * setup fc hdr by initializing few more FC header fields and sof/eof. 22542e9a92fSRobert Love * Initialized fields by this func: 22642e9a92fSRobert Love * - fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt 22742e9a92fSRobert Love * - sof and eof 22842e9a92fSRobert Love */ 22942e9a92fSRobert Love static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp, 23042e9a92fSRobert Love u32 f_ctl) 23142e9a92fSRobert Love { 23242e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 23342e9a92fSRobert Love u16 fill; 23442e9a92fSRobert Love 23542e9a92fSRobert Love fr_sof(fp) = ep->class; 23642e9a92fSRobert Love if (ep->seq.cnt) 23742e9a92fSRobert Love fr_sof(fp) = fc_sof_normal(ep->class); 23842e9a92fSRobert Love 23942e9a92fSRobert Love if (f_ctl & FC_FC_END_SEQ) { 24042e9a92fSRobert Love fr_eof(fp) = FC_EOF_T; 24142e9a92fSRobert Love if (fc_sof_needs_ack(ep->class)) 24242e9a92fSRobert Love fr_eof(fp) = FC_EOF_N; 24342e9a92fSRobert Love /* 24442e9a92fSRobert Love * Form f_ctl. 24542e9a92fSRobert Love * The number of fill bytes to make the length a 4-byte 24642e9a92fSRobert Love * multiple is the low order 2-bits of the f_ctl. 24742e9a92fSRobert Love * The fill itself will have been cleared by the frame 24842e9a92fSRobert Love * allocation. 24942e9a92fSRobert Love * After this, the length will be even, as expected by 25042e9a92fSRobert Love * the transport. 25142e9a92fSRobert Love */ 25242e9a92fSRobert Love fill = fr_len(fp) & 3; 25342e9a92fSRobert Love if (fill) { 25442e9a92fSRobert Love fill = 4 - fill; 25542e9a92fSRobert Love /* TODO, this may be a problem with fragmented skb */ 25642e9a92fSRobert Love skb_put(fp_skb(fp), fill); 25742e9a92fSRobert Love hton24(fh->fh_f_ctl, f_ctl | fill); 25842e9a92fSRobert Love } 25942e9a92fSRobert Love } else { 26042e9a92fSRobert Love WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */ 26142e9a92fSRobert Love fr_eof(fp) = FC_EOF_N; 26242e9a92fSRobert Love } 26342e9a92fSRobert Love 26442e9a92fSRobert Love /* 26542e9a92fSRobert Love * Initialize remainig fh fields 26642e9a92fSRobert Love * from fc_fill_fc_hdr 26742e9a92fSRobert Love */ 26842e9a92fSRobert Love fh->fh_ox_id = htons(ep->oxid); 26942e9a92fSRobert Love fh->fh_rx_id = htons(ep->rxid); 27042e9a92fSRobert Love fh->fh_seq_id = ep->seq.id; 27142e9a92fSRobert Love fh->fh_seq_cnt = htons(ep->seq.cnt); 27242e9a92fSRobert Love } 27342e9a92fSRobert Love 27442e9a92fSRobert Love /* 27542e9a92fSRobert Love * Release a reference to an exchange. 27642e9a92fSRobert Love * If the refcnt goes to zero and the exchange is complete, it is freed. 27742e9a92fSRobert Love */ 27842e9a92fSRobert Love static void fc_exch_release(struct fc_exch *ep) 27942e9a92fSRobert Love { 28042e9a92fSRobert Love struct fc_exch_mgr *mp; 28142e9a92fSRobert Love 28242e9a92fSRobert Love if (atomic_dec_and_test(&ep->ex_refcnt)) { 28342e9a92fSRobert Love mp = ep->em; 28442e9a92fSRobert Love if (ep->destructor) 28542e9a92fSRobert Love ep->destructor(&ep->seq, ep->arg); 286aa6cd29bSJulia Lawall WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE)); 28742e9a92fSRobert Love mempool_free(ep, mp->ep_pool); 28842e9a92fSRobert Love } 28942e9a92fSRobert Love } 29042e9a92fSRobert Love 29142e9a92fSRobert Love static int fc_exch_done_locked(struct fc_exch *ep) 29242e9a92fSRobert Love { 29342e9a92fSRobert Love int rc = 1; 29442e9a92fSRobert Love 29542e9a92fSRobert Love /* 29642e9a92fSRobert Love * We must check for completion in case there are two threads 29742e9a92fSRobert Love * tyring to complete this. But the rrq code will reuse the 29842e9a92fSRobert Love * ep, and in that case we only clear the resp and set it as 29942e9a92fSRobert Love * complete, so it can be reused by the timer to send the rrq. 30042e9a92fSRobert Love */ 30142e9a92fSRobert Love ep->resp = NULL; 30242e9a92fSRobert Love if (ep->state & FC_EX_DONE) 30342e9a92fSRobert Love return rc; 30442e9a92fSRobert Love ep->esb_stat |= ESB_ST_COMPLETE; 30542e9a92fSRobert Love 30642e9a92fSRobert Love if (!(ep->esb_stat & ESB_ST_REC_QUAL)) { 30742e9a92fSRobert Love ep->state |= FC_EX_DONE; 30842e9a92fSRobert Love if (cancel_delayed_work(&ep->timeout_work)) 30942e9a92fSRobert Love atomic_dec(&ep->ex_refcnt); /* drop hold for timer */ 31042e9a92fSRobert Love rc = 0; 31142e9a92fSRobert Love } 31242e9a92fSRobert Love return rc; 31342e9a92fSRobert Love } 31442e9a92fSRobert Love 315e4bc50beSVasu Dev static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool, 316e4bc50beSVasu Dev u16 index) 317e4bc50beSVasu Dev { 318e4bc50beSVasu Dev struct fc_exch **exches = (struct fc_exch **)(pool + 1); 319e4bc50beSVasu Dev return exches[index]; 320e4bc50beSVasu Dev } 321e4bc50beSVasu Dev 322e4bc50beSVasu Dev static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index, 323e4bc50beSVasu Dev struct fc_exch *ep) 324e4bc50beSVasu Dev { 325e4bc50beSVasu Dev ((struct fc_exch **)(pool + 1))[index] = ep; 326e4bc50beSVasu Dev } 327e4bc50beSVasu Dev 328b2f0091fSVasu Dev static void fc_exch_delete(struct fc_exch *ep) 32942e9a92fSRobert Love { 330b2f0091fSVasu Dev struct fc_exch_pool *pool; 33142e9a92fSRobert Love 332b2f0091fSVasu Dev pool = ep->pool; 333b2f0091fSVasu Dev spin_lock_bh(&pool->lock); 334b2f0091fSVasu Dev WARN_ON(pool->total_exches <= 0); 335b2f0091fSVasu Dev pool->total_exches--; 336b2f0091fSVasu Dev fc_exch_ptr_set(pool, (ep->xid - ep->em->min_xid) >> fc_cpu_order, 337b2f0091fSVasu Dev NULL); 33842e9a92fSRobert Love list_del(&ep->ex_list); 339b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 34042e9a92fSRobert Love fc_exch_release(ep); /* drop hold for exch in mp */ 34142e9a92fSRobert Love } 34242e9a92fSRobert Love 34342e9a92fSRobert Love /* 34442e9a92fSRobert Love * Internal version of fc_exch_timer_set - used with lock held. 34542e9a92fSRobert Love */ 34642e9a92fSRobert Love static inline void fc_exch_timer_set_locked(struct fc_exch *ep, 34742e9a92fSRobert Love unsigned int timer_msec) 34842e9a92fSRobert Love { 34942e9a92fSRobert Love if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) 35042e9a92fSRobert Love return; 35142e9a92fSRobert Love 352cd305ce4SRobert Love FC_EXCH_DBG(ep, "Exchange timer armed\n"); 3537414705eSRobert Love 35442e9a92fSRobert Love if (schedule_delayed_work(&ep->timeout_work, 35542e9a92fSRobert Love msecs_to_jiffies(timer_msec))) 35642e9a92fSRobert Love fc_exch_hold(ep); /* hold for timer */ 35742e9a92fSRobert Love } 35842e9a92fSRobert Love 35942e9a92fSRobert Love /* 36042e9a92fSRobert Love * Set timer for an exchange. 36142e9a92fSRobert Love * The time is a minimum delay in milliseconds until the timer fires. 36242e9a92fSRobert Love * Used for upper level protocols to time out the exchange. 36342e9a92fSRobert Love * The timer is cancelled when it fires or when the exchange completes. 36442e9a92fSRobert Love * Returns non-zero if a timer couldn't be allocated. 36542e9a92fSRobert Love */ 36642e9a92fSRobert Love static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec) 36742e9a92fSRobert Love { 36842e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 36942e9a92fSRobert Love fc_exch_timer_set_locked(ep, timer_msec); 37042e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 37142e9a92fSRobert Love } 37242e9a92fSRobert Love 373*1a7b75aeSRobert Love /** 374*1a7b75aeSRobert Love * send a frame using existing sequence and exchange. 375*1a7b75aeSRobert Love */ 376*1a7b75aeSRobert Love static int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, 377*1a7b75aeSRobert Love struct fc_frame *fp) 378*1a7b75aeSRobert Love { 379*1a7b75aeSRobert Love struct fc_exch *ep; 380*1a7b75aeSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 381*1a7b75aeSRobert Love int error; 382*1a7b75aeSRobert Love u32 f_ctl; 383*1a7b75aeSRobert Love 384*1a7b75aeSRobert Love ep = fc_seq_exch(sp); 385*1a7b75aeSRobert Love WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT); 386*1a7b75aeSRobert Love 387*1a7b75aeSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 388*1a7b75aeSRobert Love fc_exch_setup_hdr(ep, fp, f_ctl); 389*1a7b75aeSRobert Love 390*1a7b75aeSRobert Love /* 391*1a7b75aeSRobert Love * update sequence count if this frame is carrying 392*1a7b75aeSRobert Love * multiple FC frames when sequence offload is enabled 393*1a7b75aeSRobert Love * by LLD. 394*1a7b75aeSRobert Love */ 395*1a7b75aeSRobert Love if (fr_max_payload(fp)) 396*1a7b75aeSRobert Love sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)), 397*1a7b75aeSRobert Love fr_max_payload(fp)); 398*1a7b75aeSRobert Love else 399*1a7b75aeSRobert Love sp->cnt++; 400*1a7b75aeSRobert Love 401*1a7b75aeSRobert Love /* 402*1a7b75aeSRobert Love * Send the frame. 403*1a7b75aeSRobert Love */ 404*1a7b75aeSRobert Love error = lp->tt.frame_send(lp, fp); 405*1a7b75aeSRobert Love 406*1a7b75aeSRobert Love /* 407*1a7b75aeSRobert Love * Update the exchange and sequence flags, 408*1a7b75aeSRobert Love * assuming all frames for the sequence have been sent. 409*1a7b75aeSRobert Love * We can only be called to send once for each sequence. 410*1a7b75aeSRobert Love */ 411*1a7b75aeSRobert Love spin_lock_bh(&ep->ex_lock); 412*1a7b75aeSRobert Love ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */ 413*1a7b75aeSRobert Love if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT)) 414*1a7b75aeSRobert Love ep->esb_stat &= ~ESB_ST_SEQ_INIT; 415*1a7b75aeSRobert Love spin_unlock_bh(&ep->ex_lock); 416*1a7b75aeSRobert Love return error; 417*1a7b75aeSRobert Love } 418*1a7b75aeSRobert Love 419*1a7b75aeSRobert Love /** 420*1a7b75aeSRobert Love * fc_seq_alloc() - Allocate a sequence. 421*1a7b75aeSRobert Love * @ep: Exchange pointer 422*1a7b75aeSRobert Love * @seq_id: Sequence ID to allocate a sequence for 423*1a7b75aeSRobert Love * 424*1a7b75aeSRobert Love * We don't support multiple originated sequences on the same exchange. 425*1a7b75aeSRobert Love * By implication, any previously originated sequence on this exchange 426*1a7b75aeSRobert Love * is complete, and we reallocate the same sequence. 427*1a7b75aeSRobert Love */ 428*1a7b75aeSRobert Love static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id) 429*1a7b75aeSRobert Love { 430*1a7b75aeSRobert Love struct fc_seq *sp; 431*1a7b75aeSRobert Love 432*1a7b75aeSRobert Love sp = &ep->seq; 433*1a7b75aeSRobert Love sp->ssb_stat = 0; 434*1a7b75aeSRobert Love sp->cnt = 0; 435*1a7b75aeSRobert Love sp->id = seq_id; 436*1a7b75aeSRobert Love return sp; 437*1a7b75aeSRobert Love } 438*1a7b75aeSRobert Love 439*1a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp) 440*1a7b75aeSRobert Love { 441*1a7b75aeSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 442*1a7b75aeSRobert Love 443*1a7b75aeSRobert Love sp = fc_seq_alloc(ep, ep->seq_id++); 444*1a7b75aeSRobert Love FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n", 445*1a7b75aeSRobert Love ep->f_ctl, sp->id); 446*1a7b75aeSRobert Love return sp; 447*1a7b75aeSRobert Love } 448*1a7b75aeSRobert Love 449*1a7b75aeSRobert Love /** 450*1a7b75aeSRobert Love * Allocate a new sequence on the same exchange as the supplied sequence. 451*1a7b75aeSRobert Love * This will never return NULL. 452*1a7b75aeSRobert Love */ 453*1a7b75aeSRobert Love static struct fc_seq *fc_seq_start_next(struct fc_seq *sp) 454*1a7b75aeSRobert Love { 455*1a7b75aeSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 456*1a7b75aeSRobert Love 457*1a7b75aeSRobert Love spin_lock_bh(&ep->ex_lock); 458*1a7b75aeSRobert Love sp = fc_seq_start_next_locked(sp); 459*1a7b75aeSRobert Love spin_unlock_bh(&ep->ex_lock); 460*1a7b75aeSRobert Love 461*1a7b75aeSRobert Love return sp; 462*1a7b75aeSRobert Love } 463*1a7b75aeSRobert Love 464*1a7b75aeSRobert Love /** 465*1a7b75aeSRobert Love * This function is for seq_exch_abort function pointer in 466*1a7b75aeSRobert Love * struct libfc_function_template, see comment block on 467*1a7b75aeSRobert Love * seq_exch_abort for description of this function. 468*1a7b75aeSRobert Love */ 469*1a7b75aeSRobert Love static int fc_seq_exch_abort(const struct fc_seq *req_sp, 470*1a7b75aeSRobert Love unsigned int timer_msec) 47142e9a92fSRobert Love { 47242e9a92fSRobert Love struct fc_seq *sp; 47342e9a92fSRobert Love struct fc_exch *ep; 47442e9a92fSRobert Love struct fc_frame *fp; 47542e9a92fSRobert Love int error; 47642e9a92fSRobert Love 47742e9a92fSRobert Love ep = fc_seq_exch(req_sp); 47842e9a92fSRobert Love 47942e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 48042e9a92fSRobert Love if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) || 48142e9a92fSRobert Love ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) { 48242e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 48342e9a92fSRobert Love return -ENXIO; 48442e9a92fSRobert Love } 48542e9a92fSRobert Love 48642e9a92fSRobert Love /* 48742e9a92fSRobert Love * Send the abort on a new sequence if possible. 48842e9a92fSRobert Love */ 48942e9a92fSRobert Love sp = fc_seq_start_next_locked(&ep->seq); 49042e9a92fSRobert Love if (!sp) { 49142e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 49242e9a92fSRobert Love return -ENOMEM; 49342e9a92fSRobert Love } 49442e9a92fSRobert Love 49542e9a92fSRobert Love ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL; 49642e9a92fSRobert Love if (timer_msec) 49742e9a92fSRobert Love fc_exch_timer_set_locked(ep, timer_msec); 49842e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 49942e9a92fSRobert Love 50042e9a92fSRobert Love /* 50142e9a92fSRobert Love * If not logged into the fabric, don't send ABTS but leave 50242e9a92fSRobert Love * sequence active until next timeout. 50342e9a92fSRobert Love */ 50442e9a92fSRobert Love if (!ep->sid) 50542e9a92fSRobert Love return 0; 50642e9a92fSRobert Love 50742e9a92fSRobert Love /* 50842e9a92fSRobert Love * Send an abort for the sequence that timed out. 50942e9a92fSRobert Love */ 51042e9a92fSRobert Love fp = fc_frame_alloc(ep->lp, 0); 51142e9a92fSRobert Love if (fp) { 51242e9a92fSRobert Love fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid, 51342e9a92fSRobert Love FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0); 51442e9a92fSRobert Love error = fc_seq_send(ep->lp, sp, fp); 51542e9a92fSRobert Love } else 51642e9a92fSRobert Love error = -ENOBUFS; 51742e9a92fSRobert Love return error; 51842e9a92fSRobert Love } 51942e9a92fSRobert Love 52042e9a92fSRobert Love /* 52142e9a92fSRobert Love * Exchange timeout - handle exchange timer expiration. 52242e9a92fSRobert Love * The timer will have been cancelled before this is called. 52342e9a92fSRobert Love */ 52442e9a92fSRobert Love static void fc_exch_timeout(struct work_struct *work) 52542e9a92fSRobert Love { 52642e9a92fSRobert Love struct fc_exch *ep = container_of(work, struct fc_exch, 52742e9a92fSRobert Love timeout_work.work); 52842e9a92fSRobert Love struct fc_seq *sp = &ep->seq; 52942e9a92fSRobert Love void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg); 53042e9a92fSRobert Love void *arg; 53142e9a92fSRobert Love u32 e_stat; 53242e9a92fSRobert Love int rc = 1; 53342e9a92fSRobert Love 534cd305ce4SRobert Love FC_EXCH_DBG(ep, "Exchange timed out\n"); 535cd305ce4SRobert Love 53642e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 53742e9a92fSRobert Love if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) 53842e9a92fSRobert Love goto unlock; 53942e9a92fSRobert Love 54042e9a92fSRobert Love e_stat = ep->esb_stat; 54142e9a92fSRobert Love if (e_stat & ESB_ST_COMPLETE) { 54242e9a92fSRobert Love ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL; 543a0cc1eccSVasu Dev spin_unlock_bh(&ep->ex_lock); 54442e9a92fSRobert Love if (e_stat & ESB_ST_REC_QUAL) 54542e9a92fSRobert Love fc_exch_rrq(ep); 54642e9a92fSRobert Love goto done; 54742e9a92fSRobert Love } else { 54842e9a92fSRobert Love resp = ep->resp; 54942e9a92fSRobert Love arg = ep->arg; 55042e9a92fSRobert Love ep->resp = NULL; 55142e9a92fSRobert Love if (e_stat & ESB_ST_ABNORMAL) 55242e9a92fSRobert Love rc = fc_exch_done_locked(ep); 55342e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 55442e9a92fSRobert Love if (!rc) 555b2f0091fSVasu Dev fc_exch_delete(ep); 55642e9a92fSRobert Love if (resp) 55742e9a92fSRobert Love resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg); 55842e9a92fSRobert Love fc_seq_exch_abort(sp, 2 * ep->r_a_tov); 55942e9a92fSRobert Love goto done; 56042e9a92fSRobert Love } 56142e9a92fSRobert Love unlock: 56242e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 56342e9a92fSRobert Love done: 56442e9a92fSRobert Love /* 56542e9a92fSRobert Love * This release matches the hold taken when the timer was set. 56642e9a92fSRobert Love */ 56742e9a92fSRobert Love fc_exch_release(ep); 56842e9a92fSRobert Love } 56942e9a92fSRobert Love 57052ff878cSVasu Dev /** 57152ff878cSVasu Dev * fc_exch_em_alloc() - allocate an exchange from a specified EM. 57252ff878cSVasu Dev * @lport: ptr to the local port 57342e9a92fSRobert Love * @mp: ptr to the exchange manager 57442e9a92fSRobert Love * 575d7179680SVasu Dev * Returns pointer to allocated fc_exch with exch lock held. 57642e9a92fSRobert Love */ 57752ff878cSVasu Dev static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport, 578d7179680SVasu Dev struct fc_exch_mgr *mp) 57942e9a92fSRobert Love { 58042e9a92fSRobert Love struct fc_exch *ep; 581b2f0091fSVasu Dev unsigned int cpu; 582b2f0091fSVasu Dev u16 index; 583b2f0091fSVasu Dev struct fc_exch_pool *pool; 58442e9a92fSRobert Love 58542e9a92fSRobert Love /* allocate memory for exchange */ 58642e9a92fSRobert Love ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC); 58742e9a92fSRobert Love if (!ep) { 58842e9a92fSRobert Love atomic_inc(&mp->stats.no_free_exch); 58942e9a92fSRobert Love goto out; 59042e9a92fSRobert Love } 59142e9a92fSRobert Love memset(ep, 0, sizeof(*ep)); 59242e9a92fSRobert Love 593b2f0091fSVasu Dev cpu = smp_processor_id(); 594b2f0091fSVasu Dev pool = per_cpu_ptr(mp->pool, cpu); 595b2f0091fSVasu Dev spin_lock_bh(&pool->lock); 596b2f0091fSVasu Dev index = pool->next_index; 597b2f0091fSVasu Dev /* allocate new exch from pool */ 598b2f0091fSVasu Dev while (fc_exch_ptr_get(pool, index)) { 599b2f0091fSVasu Dev index = index == mp->pool_max_index ? 0 : index + 1; 600b2f0091fSVasu Dev if (index == pool->next_index) 60142e9a92fSRobert Love goto err; 60242e9a92fSRobert Love } 603b2f0091fSVasu Dev pool->next_index = index == mp->pool_max_index ? 0 : index + 1; 60442e9a92fSRobert Love 60542e9a92fSRobert Love fc_exch_hold(ep); /* hold for exch in mp */ 60642e9a92fSRobert Love spin_lock_init(&ep->ex_lock); 60742e9a92fSRobert Love /* 60842e9a92fSRobert Love * Hold exch lock for caller to prevent fc_exch_reset() 60942e9a92fSRobert Love * from releasing exch while fc_exch_alloc() caller is 61042e9a92fSRobert Love * still working on exch. 61142e9a92fSRobert Love */ 61242e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 61342e9a92fSRobert Love 614b2f0091fSVasu Dev fc_exch_ptr_set(pool, index, ep); 615b2f0091fSVasu Dev list_add_tail(&ep->ex_list, &pool->ex_list); 61642e9a92fSRobert Love fc_seq_alloc(ep, ep->seq_id++); 617b2f0091fSVasu Dev pool->total_exches++; 618b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 61942e9a92fSRobert Love 62042e9a92fSRobert Love /* 62142e9a92fSRobert Love * update exchange 62242e9a92fSRobert Love */ 623b2f0091fSVasu Dev ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid; 62442e9a92fSRobert Love ep->em = mp; 625b2f0091fSVasu Dev ep->pool = pool; 62652ff878cSVasu Dev ep->lp = lport; 62742e9a92fSRobert Love ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */ 62842e9a92fSRobert Love ep->rxid = FC_XID_UNKNOWN; 62942e9a92fSRobert Love ep->class = mp->class; 63042e9a92fSRobert Love INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout); 63142e9a92fSRobert Love out: 63242e9a92fSRobert Love return ep; 63342e9a92fSRobert Love err: 634b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 63542e9a92fSRobert Love atomic_inc(&mp->stats.no_free_exch_xid); 63642e9a92fSRobert Love mempool_free(ep, mp->ep_pool); 63742e9a92fSRobert Love return NULL; 63842e9a92fSRobert Love } 63952ff878cSVasu Dev 64052ff878cSVasu Dev /** 64152ff878cSVasu Dev * fc_exch_alloc() - allocate an exchange. 64252ff878cSVasu Dev * @lport: ptr to the local port 64352ff878cSVasu Dev * @fp: ptr to the FC frame 64452ff878cSVasu Dev * 64552ff878cSVasu Dev * This function walks the list of the exchange manager(EM) 64652ff878cSVasu Dev * anchors to select a EM for new exchange allocation. The 64752ff878cSVasu Dev * EM is selected having either a NULL match function pointer 64852ff878cSVasu Dev * or call to match function returning true. 64952ff878cSVasu Dev */ 650*1a7b75aeSRobert Love static struct fc_exch *fc_exch_alloc(struct fc_lport *lport, 651*1a7b75aeSRobert Love struct fc_frame *fp) 65252ff878cSVasu Dev { 65352ff878cSVasu Dev struct fc_exch_mgr_anchor *ema; 65452ff878cSVasu Dev struct fc_exch *ep; 65552ff878cSVasu Dev 65652ff878cSVasu Dev list_for_each_entry(ema, &lport->ema_list, ema_list) { 65752ff878cSVasu Dev if (!ema->match || ema->match(fp)) { 658d7179680SVasu Dev ep = fc_exch_em_alloc(lport, ema->mp); 65952ff878cSVasu Dev if (ep) 66052ff878cSVasu Dev return ep; 66152ff878cSVasu Dev } 66252ff878cSVasu Dev } 66352ff878cSVasu Dev return NULL; 66452ff878cSVasu Dev } 66542e9a92fSRobert Love 66642e9a92fSRobert Love /* 66742e9a92fSRobert Love * Lookup and hold an exchange. 66842e9a92fSRobert Love */ 66942e9a92fSRobert Love static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid) 67042e9a92fSRobert Love { 671b2f0091fSVasu Dev struct fc_exch_pool *pool; 67242e9a92fSRobert Love struct fc_exch *ep = NULL; 67342e9a92fSRobert Love 67442e9a92fSRobert Love if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) { 675b2f0091fSVasu Dev pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask); 676b2f0091fSVasu Dev spin_lock_bh(&pool->lock); 677b2f0091fSVasu Dev ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order); 67842e9a92fSRobert Love if (ep) { 67942e9a92fSRobert Love fc_exch_hold(ep); 68042e9a92fSRobert Love WARN_ON(ep->xid != xid); 68142e9a92fSRobert Love } 682b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 68342e9a92fSRobert Love } 68442e9a92fSRobert Love return ep; 68542e9a92fSRobert Love } 68642e9a92fSRobert Love 687*1a7b75aeSRobert Love 688*1a7b75aeSRobert Love /** 689*1a7b75aeSRobert Love * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and 690*1a7b75aeSRobert Love * the memory allocated for the related objects may be freed. 691*1a7b75aeSRobert Love * @sp: Sequence pointer 692*1a7b75aeSRobert Love */ 693*1a7b75aeSRobert Love static void fc_exch_done(struct fc_seq *sp) 69442e9a92fSRobert Love { 69542e9a92fSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 69642e9a92fSRobert Love int rc; 69742e9a92fSRobert Love 69842e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 69942e9a92fSRobert Love rc = fc_exch_done_locked(ep); 70042e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 70142e9a92fSRobert Love if (!rc) 702b2f0091fSVasu Dev fc_exch_delete(ep); 70342e9a92fSRobert Love } 70442e9a92fSRobert Love 70542e9a92fSRobert Love /* 70642e9a92fSRobert Love * Allocate a new exchange as responder. 70742e9a92fSRobert Love * Sets the responder ID in the frame header. 70842e9a92fSRobert Love */ 70952ff878cSVasu Dev static struct fc_exch *fc_exch_resp(struct fc_lport *lport, 71052ff878cSVasu Dev struct fc_exch_mgr *mp, 71152ff878cSVasu Dev struct fc_frame *fp) 71242e9a92fSRobert Love { 71342e9a92fSRobert Love struct fc_exch *ep; 71442e9a92fSRobert Love struct fc_frame_header *fh; 71542e9a92fSRobert Love 71652ff878cSVasu Dev ep = fc_exch_alloc(lport, fp); 71742e9a92fSRobert Love if (ep) { 71842e9a92fSRobert Love ep->class = fc_frame_class(fp); 71942e9a92fSRobert Love 72042e9a92fSRobert Love /* 72142e9a92fSRobert Love * Set EX_CTX indicating we're responding on this exchange. 72242e9a92fSRobert Love */ 72342e9a92fSRobert Love ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */ 72442e9a92fSRobert Love ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */ 72542e9a92fSRobert Love fh = fc_frame_header_get(fp); 72642e9a92fSRobert Love ep->sid = ntoh24(fh->fh_d_id); 72742e9a92fSRobert Love ep->did = ntoh24(fh->fh_s_id); 72842e9a92fSRobert Love ep->oid = ep->did; 72942e9a92fSRobert Love 73042e9a92fSRobert Love /* 73142e9a92fSRobert Love * Allocated exchange has placed the XID in the 73242e9a92fSRobert Love * originator field. Move it to the responder field, 73342e9a92fSRobert Love * and set the originator XID from the frame. 73442e9a92fSRobert Love */ 73542e9a92fSRobert Love ep->rxid = ep->xid; 73642e9a92fSRobert Love ep->oxid = ntohs(fh->fh_ox_id); 73742e9a92fSRobert Love ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT; 73842e9a92fSRobert Love if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0) 73942e9a92fSRobert Love ep->esb_stat &= ~ESB_ST_SEQ_INIT; 74042e9a92fSRobert Love 74142e9a92fSRobert Love fc_exch_hold(ep); /* hold for caller */ 74252ff878cSVasu Dev spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */ 74342e9a92fSRobert Love } 74442e9a92fSRobert Love return ep; 74542e9a92fSRobert Love } 74642e9a92fSRobert Love 74742e9a92fSRobert Love /* 74842e9a92fSRobert Love * Find a sequence for receive where the other end is originating the sequence. 74942e9a92fSRobert Love * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold 75042e9a92fSRobert Love * on the ep that should be released by the caller. 75142e9a92fSRobert Love */ 75252ff878cSVasu Dev static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport, 75352ff878cSVasu Dev struct fc_exch_mgr *mp, 754b2ab99c9SRobert Love struct fc_frame *fp) 75542e9a92fSRobert Love { 75642e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 75742e9a92fSRobert Love struct fc_exch *ep = NULL; 75842e9a92fSRobert Love struct fc_seq *sp = NULL; 75942e9a92fSRobert Love enum fc_pf_rjt_reason reject = FC_RJT_NONE; 76042e9a92fSRobert Love u32 f_ctl; 76142e9a92fSRobert Love u16 xid; 76242e9a92fSRobert Love 76342e9a92fSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 76442e9a92fSRobert Love WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0); 76542e9a92fSRobert Love 76642e9a92fSRobert Love /* 76742e9a92fSRobert Love * Lookup or create the exchange if we will be creating the sequence. 76842e9a92fSRobert Love */ 76942e9a92fSRobert Love if (f_ctl & FC_FC_EX_CTX) { 77042e9a92fSRobert Love xid = ntohs(fh->fh_ox_id); /* we originated exch */ 77142e9a92fSRobert Love ep = fc_exch_find(mp, xid); 77242e9a92fSRobert Love if (!ep) { 77342e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 77442e9a92fSRobert Love reject = FC_RJT_OX_ID; 77542e9a92fSRobert Love goto out; 77642e9a92fSRobert Love } 77742e9a92fSRobert Love if (ep->rxid == FC_XID_UNKNOWN) 77842e9a92fSRobert Love ep->rxid = ntohs(fh->fh_rx_id); 77942e9a92fSRobert Love else if (ep->rxid != ntohs(fh->fh_rx_id)) { 78042e9a92fSRobert Love reject = FC_RJT_OX_ID; 78142e9a92fSRobert Love goto rel; 78242e9a92fSRobert Love } 78342e9a92fSRobert Love } else { 78442e9a92fSRobert Love xid = ntohs(fh->fh_rx_id); /* we are the responder */ 78542e9a92fSRobert Love 78642e9a92fSRobert Love /* 78742e9a92fSRobert Love * Special case for MDS issuing an ELS TEST with a 78842e9a92fSRobert Love * bad rxid of 0. 78942e9a92fSRobert Love * XXX take this out once we do the proper reject. 79042e9a92fSRobert Love */ 79142e9a92fSRobert Love if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ && 79242e9a92fSRobert Love fc_frame_payload_op(fp) == ELS_TEST) { 79342e9a92fSRobert Love fh->fh_rx_id = htons(FC_XID_UNKNOWN); 79442e9a92fSRobert Love xid = FC_XID_UNKNOWN; 79542e9a92fSRobert Love } 79642e9a92fSRobert Love 79742e9a92fSRobert Love /* 79842e9a92fSRobert Love * new sequence - find the exchange 79942e9a92fSRobert Love */ 80042e9a92fSRobert Love ep = fc_exch_find(mp, xid); 80142e9a92fSRobert Love if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) { 80242e9a92fSRobert Love if (ep) { 80342e9a92fSRobert Love atomic_inc(&mp->stats.xid_busy); 80442e9a92fSRobert Love reject = FC_RJT_RX_ID; 80542e9a92fSRobert Love goto rel; 80642e9a92fSRobert Love } 80752ff878cSVasu Dev ep = fc_exch_resp(lport, mp, fp); 80842e9a92fSRobert Love if (!ep) { 80942e9a92fSRobert Love reject = FC_RJT_EXCH_EST; /* XXX */ 81042e9a92fSRobert Love goto out; 81142e9a92fSRobert Love } 81242e9a92fSRobert Love xid = ep->xid; /* get our XID */ 81342e9a92fSRobert Love } else if (!ep) { 81442e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 81542e9a92fSRobert Love reject = FC_RJT_RX_ID; /* XID not found */ 81642e9a92fSRobert Love goto out; 81742e9a92fSRobert Love } 81842e9a92fSRobert Love } 81942e9a92fSRobert Love 82042e9a92fSRobert Love /* 82142e9a92fSRobert Love * At this point, we have the exchange held. 82242e9a92fSRobert Love * Find or create the sequence. 82342e9a92fSRobert Love */ 82442e9a92fSRobert Love if (fc_sof_is_init(fr_sof(fp))) { 82542e9a92fSRobert Love sp = fc_seq_start_next(&ep->seq); 82642e9a92fSRobert Love if (!sp) { 82742e9a92fSRobert Love reject = FC_RJT_SEQ_XS; /* exchange shortage */ 82842e9a92fSRobert Love goto rel; 82942e9a92fSRobert Love } 83042e9a92fSRobert Love sp->id = fh->fh_seq_id; 83142e9a92fSRobert Love sp->ssb_stat |= SSB_ST_RESP; 83242e9a92fSRobert Love } else { 83342e9a92fSRobert Love sp = &ep->seq; 83442e9a92fSRobert Love if (sp->id != fh->fh_seq_id) { 83542e9a92fSRobert Love atomic_inc(&mp->stats.seq_not_found); 83642e9a92fSRobert Love reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */ 83742e9a92fSRobert Love goto rel; 83842e9a92fSRobert Love } 83942e9a92fSRobert Love } 84042e9a92fSRobert Love WARN_ON(ep != fc_seq_exch(sp)); 84142e9a92fSRobert Love 84242e9a92fSRobert Love if (f_ctl & FC_FC_SEQ_INIT) 84342e9a92fSRobert Love ep->esb_stat |= ESB_ST_SEQ_INIT; 84442e9a92fSRobert Love 84542e9a92fSRobert Love fr_seq(fp) = sp; 84642e9a92fSRobert Love out: 84742e9a92fSRobert Love return reject; 84842e9a92fSRobert Love rel: 84942e9a92fSRobert Love fc_exch_done(&ep->seq); 85042e9a92fSRobert Love fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */ 85142e9a92fSRobert Love return reject; 85242e9a92fSRobert Love } 85342e9a92fSRobert Love 85442e9a92fSRobert Love /* 85542e9a92fSRobert Love * Find the sequence for a frame being received. 85642e9a92fSRobert Love * We originated the sequence, so it should be found. 85742e9a92fSRobert Love * We may or may not have originated the exchange. 85842e9a92fSRobert Love * Does not hold the sequence for the caller. 85942e9a92fSRobert Love */ 86042e9a92fSRobert Love static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp, 86142e9a92fSRobert Love struct fc_frame *fp) 86242e9a92fSRobert Love { 86342e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 86442e9a92fSRobert Love struct fc_exch *ep; 86542e9a92fSRobert Love struct fc_seq *sp = NULL; 86642e9a92fSRobert Love u32 f_ctl; 86742e9a92fSRobert Love u16 xid; 86842e9a92fSRobert Love 86942e9a92fSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 87042e9a92fSRobert Love WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX); 87142e9a92fSRobert Love xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id); 87242e9a92fSRobert Love ep = fc_exch_find(mp, xid); 87342e9a92fSRobert Love if (!ep) 87442e9a92fSRobert Love return NULL; 87542e9a92fSRobert Love if (ep->seq.id == fh->fh_seq_id) { 87642e9a92fSRobert Love /* 87742e9a92fSRobert Love * Save the RX_ID if we didn't previously know it. 87842e9a92fSRobert Love */ 87942e9a92fSRobert Love sp = &ep->seq; 88042e9a92fSRobert Love if ((f_ctl & FC_FC_EX_CTX) != 0 && 88142e9a92fSRobert Love ep->rxid == FC_XID_UNKNOWN) { 88242e9a92fSRobert Love ep->rxid = ntohs(fh->fh_rx_id); 88342e9a92fSRobert Love } 88442e9a92fSRobert Love } 88542e9a92fSRobert Love fc_exch_release(ep); 88642e9a92fSRobert Love return sp; 88742e9a92fSRobert Love } 88842e9a92fSRobert Love 88942e9a92fSRobert Love /* 89042e9a92fSRobert Love * Set addresses for an exchange. 89142e9a92fSRobert Love * Note this must be done before the first sequence of the exchange is sent. 89242e9a92fSRobert Love */ 89342e9a92fSRobert Love static void fc_exch_set_addr(struct fc_exch *ep, 89442e9a92fSRobert Love u32 orig_id, u32 resp_id) 89542e9a92fSRobert Love { 89642e9a92fSRobert Love ep->oid = orig_id; 89742e9a92fSRobert Love if (ep->esb_stat & ESB_ST_RESP) { 89842e9a92fSRobert Love ep->sid = resp_id; 89942e9a92fSRobert Love ep->did = orig_id; 90042e9a92fSRobert Love } else { 90142e9a92fSRobert Love ep->sid = orig_id; 90242e9a92fSRobert Love ep->did = resp_id; 90342e9a92fSRobert Love } 90442e9a92fSRobert Love } 90542e9a92fSRobert Love 906*1a7b75aeSRobert Love /** 907*1a7b75aeSRobert Love * fc_seq_els_rsp_send() - Send ELS response using mainly infomation 908*1a7b75aeSRobert Love * in exchange and sequence in EM layer. 909*1a7b75aeSRobert Love * @sp: Sequence pointer 910*1a7b75aeSRobert Love * @els_cmd: ELS command 911*1a7b75aeSRobert Love * @els_data: ELS data 91242e9a92fSRobert Love */ 913*1a7b75aeSRobert Love static void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd, 91442e9a92fSRobert Love struct fc_seq_els_data *els_data) 91542e9a92fSRobert Love { 91642e9a92fSRobert Love switch (els_cmd) { 91742e9a92fSRobert Love case ELS_LS_RJT: 91842e9a92fSRobert Love fc_seq_ls_rjt(sp, els_data->reason, els_data->explan); 91942e9a92fSRobert Love break; 92042e9a92fSRobert Love case ELS_LS_ACC: 92142e9a92fSRobert Love fc_seq_ls_acc(sp); 92242e9a92fSRobert Love break; 92342e9a92fSRobert Love case ELS_RRQ: 92442e9a92fSRobert Love fc_exch_els_rrq(sp, els_data->fp); 92542e9a92fSRobert Love break; 92642e9a92fSRobert Love case ELS_REC: 92742e9a92fSRobert Love fc_exch_els_rec(sp, els_data->fp); 92842e9a92fSRobert Love break; 92942e9a92fSRobert Love default: 9307414705eSRobert Love FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd); 93142e9a92fSRobert Love } 93242e9a92fSRobert Love } 93342e9a92fSRobert Love 93442e9a92fSRobert Love /* 93542e9a92fSRobert Love * Send a sequence, which is also the last sequence in the exchange. 93642e9a92fSRobert Love */ 93742e9a92fSRobert Love static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp, 93842e9a92fSRobert Love enum fc_rctl rctl, enum fc_fh_type fh_type) 93942e9a92fSRobert Love { 94042e9a92fSRobert Love u32 f_ctl; 94142e9a92fSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 94242e9a92fSRobert Love 94342e9a92fSRobert Love f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT; 94442e9a92fSRobert Love f_ctl |= ep->f_ctl; 94542e9a92fSRobert Love fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0); 94642e9a92fSRobert Love fc_seq_send(ep->lp, sp, fp); 94742e9a92fSRobert Love } 94842e9a92fSRobert Love 94942e9a92fSRobert Love /* 95042e9a92fSRobert Love * Send ACK_1 (or equiv.) indicating we received something. 95142e9a92fSRobert Love * The frame we're acking is supplied. 95242e9a92fSRobert Love */ 95342e9a92fSRobert Love static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp) 95442e9a92fSRobert Love { 95542e9a92fSRobert Love struct fc_frame *fp; 95642e9a92fSRobert Love struct fc_frame_header *rx_fh; 95742e9a92fSRobert Love struct fc_frame_header *fh; 95842e9a92fSRobert Love struct fc_exch *ep = fc_seq_exch(sp); 95942e9a92fSRobert Love struct fc_lport *lp = ep->lp; 96042e9a92fSRobert Love unsigned int f_ctl; 96142e9a92fSRobert Love 96242e9a92fSRobert Love /* 96342e9a92fSRobert Love * Don't send ACKs for class 3. 96442e9a92fSRobert Love */ 96542e9a92fSRobert Love if (fc_sof_needs_ack(fr_sof(rx_fp))) { 96642e9a92fSRobert Love fp = fc_frame_alloc(lp, 0); 96742e9a92fSRobert Love if (!fp) 96842e9a92fSRobert Love return; 96942e9a92fSRobert Love 97042e9a92fSRobert Love fh = fc_frame_header_get(fp); 97142e9a92fSRobert Love fh->fh_r_ctl = FC_RCTL_ACK_1; 97242e9a92fSRobert Love fh->fh_type = FC_TYPE_BLS; 97342e9a92fSRobert Love 97442e9a92fSRobert Love /* 97542e9a92fSRobert Love * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22). 97642e9a92fSRobert Love * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT. 97742e9a92fSRobert Love * Bits 9-8 are meaningful (retransmitted or unidirectional). 97842e9a92fSRobert Love * Last ACK uses bits 7-6 (continue sequence), 97942e9a92fSRobert Love * bits 5-4 are meaningful (what kind of ACK to use). 98042e9a92fSRobert Love */ 98142e9a92fSRobert Love rx_fh = fc_frame_header_get(rx_fp); 98242e9a92fSRobert Love f_ctl = ntoh24(rx_fh->fh_f_ctl); 98342e9a92fSRobert Love f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX | 98442e9a92fSRobert Love FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ | 98542e9a92fSRobert Love FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT | 98642e9a92fSRobert Love FC_FC_RETX_SEQ | FC_FC_UNI_TX; 98742e9a92fSRobert Love f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX; 98842e9a92fSRobert Love hton24(fh->fh_f_ctl, f_ctl); 98942e9a92fSRobert Love 99042e9a92fSRobert Love fc_exch_setup_hdr(ep, fp, f_ctl); 99142e9a92fSRobert Love fh->fh_seq_id = rx_fh->fh_seq_id; 99242e9a92fSRobert Love fh->fh_seq_cnt = rx_fh->fh_seq_cnt; 99342e9a92fSRobert Love fh->fh_parm_offset = htonl(1); /* ack single frame */ 99442e9a92fSRobert Love 99542e9a92fSRobert Love fr_sof(fp) = fr_sof(rx_fp); 99642e9a92fSRobert Love if (f_ctl & FC_FC_END_SEQ) 99742e9a92fSRobert Love fr_eof(fp) = FC_EOF_T; 99842e9a92fSRobert Love else 99942e9a92fSRobert Love fr_eof(fp) = FC_EOF_N; 100042e9a92fSRobert Love 100142e9a92fSRobert Love (void) lp->tt.frame_send(lp, fp); 100242e9a92fSRobert Love } 100342e9a92fSRobert Love } 100442e9a92fSRobert Love 100542e9a92fSRobert Love /* 100642e9a92fSRobert Love * Send BLS Reject. 100742e9a92fSRobert Love * This is for rejecting BA_ABTS only. 100842e9a92fSRobert Love */ 1009b2ab99c9SRobert Love static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp, 1010b2ab99c9SRobert Love enum fc_ba_rjt_reason reason, 101142e9a92fSRobert Love enum fc_ba_rjt_explan explan) 101242e9a92fSRobert Love { 101342e9a92fSRobert Love struct fc_frame *fp; 101442e9a92fSRobert Love struct fc_frame_header *rx_fh; 101542e9a92fSRobert Love struct fc_frame_header *fh; 101642e9a92fSRobert Love struct fc_ba_rjt *rp; 101742e9a92fSRobert Love struct fc_lport *lp; 101842e9a92fSRobert Love unsigned int f_ctl; 101942e9a92fSRobert Love 102042e9a92fSRobert Love lp = fr_dev(rx_fp); 102142e9a92fSRobert Love fp = fc_frame_alloc(lp, sizeof(*rp)); 102242e9a92fSRobert Love if (!fp) 102342e9a92fSRobert Love return; 102442e9a92fSRobert Love fh = fc_frame_header_get(fp); 102542e9a92fSRobert Love rx_fh = fc_frame_header_get(rx_fp); 102642e9a92fSRobert Love 102742e9a92fSRobert Love memset(fh, 0, sizeof(*fh) + sizeof(*rp)); 102842e9a92fSRobert Love 102942e9a92fSRobert Love rp = fc_frame_payload_get(fp, sizeof(*rp)); 103042e9a92fSRobert Love rp->br_reason = reason; 103142e9a92fSRobert Love rp->br_explan = explan; 103242e9a92fSRobert Love 103342e9a92fSRobert Love /* 103442e9a92fSRobert Love * seq_id, cs_ctl, df_ctl and param/offset are zero. 103542e9a92fSRobert Love */ 103642e9a92fSRobert Love memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3); 103742e9a92fSRobert Love memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3); 10381d490ce3SJoe Eykholt fh->fh_ox_id = rx_fh->fh_ox_id; 10391d490ce3SJoe Eykholt fh->fh_rx_id = rx_fh->fh_rx_id; 104042e9a92fSRobert Love fh->fh_seq_cnt = rx_fh->fh_seq_cnt; 104142e9a92fSRobert Love fh->fh_r_ctl = FC_RCTL_BA_RJT; 104242e9a92fSRobert Love fh->fh_type = FC_TYPE_BLS; 104342e9a92fSRobert Love 104442e9a92fSRobert Love /* 104542e9a92fSRobert Love * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22). 104642e9a92fSRobert Love * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT. 104742e9a92fSRobert Love * Bits 9-8 are meaningful (retransmitted or unidirectional). 104842e9a92fSRobert Love * Last ACK uses bits 7-6 (continue sequence), 104942e9a92fSRobert Love * bits 5-4 are meaningful (what kind of ACK to use). 105042e9a92fSRobert Love * Always set LAST_SEQ, END_SEQ. 105142e9a92fSRobert Love */ 105242e9a92fSRobert Love f_ctl = ntoh24(rx_fh->fh_f_ctl); 105342e9a92fSRobert Love f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX | 105442e9a92fSRobert Love FC_FC_END_CONN | FC_FC_SEQ_INIT | 105542e9a92fSRobert Love FC_FC_RETX_SEQ | FC_FC_UNI_TX; 105642e9a92fSRobert Love f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX; 105742e9a92fSRobert Love f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ; 105842e9a92fSRobert Love f_ctl &= ~FC_FC_FIRST_SEQ; 105942e9a92fSRobert Love hton24(fh->fh_f_ctl, f_ctl); 106042e9a92fSRobert Love 106142e9a92fSRobert Love fr_sof(fp) = fc_sof_class(fr_sof(rx_fp)); 106242e9a92fSRobert Love fr_eof(fp) = FC_EOF_T; 106342e9a92fSRobert Love if (fc_sof_needs_ack(fr_sof(fp))) 106442e9a92fSRobert Love fr_eof(fp) = FC_EOF_N; 106542e9a92fSRobert Love 106642e9a92fSRobert Love (void) lp->tt.frame_send(lp, fp); 106742e9a92fSRobert Love } 106842e9a92fSRobert Love 106942e9a92fSRobert Love /* 107042e9a92fSRobert Love * Handle an incoming ABTS. This would be for target mode usually, 107142e9a92fSRobert Love * but could be due to lost FCP transfer ready, confirm or RRQ. 107242e9a92fSRobert Love * We always handle this as an exchange abort, ignoring the parameter. 107342e9a92fSRobert Love */ 107442e9a92fSRobert Love static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp) 107542e9a92fSRobert Love { 107642e9a92fSRobert Love struct fc_frame *fp; 107742e9a92fSRobert Love struct fc_ba_acc *ap; 107842e9a92fSRobert Love struct fc_frame_header *fh; 107942e9a92fSRobert Love struct fc_seq *sp; 108042e9a92fSRobert Love 108142e9a92fSRobert Love if (!ep) 108242e9a92fSRobert Love goto reject; 108342e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 108442e9a92fSRobert Love if (ep->esb_stat & ESB_ST_COMPLETE) { 108542e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 108642e9a92fSRobert Love goto reject; 108742e9a92fSRobert Love } 108842e9a92fSRobert Love if (!(ep->esb_stat & ESB_ST_REC_QUAL)) 108942e9a92fSRobert Love fc_exch_hold(ep); /* hold for REC_QUAL */ 109042e9a92fSRobert Love ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL; 109142e9a92fSRobert Love fc_exch_timer_set_locked(ep, ep->r_a_tov); 109242e9a92fSRobert Love 109342e9a92fSRobert Love fp = fc_frame_alloc(ep->lp, sizeof(*ap)); 109442e9a92fSRobert Love if (!fp) { 109542e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 109642e9a92fSRobert Love goto free; 109742e9a92fSRobert Love } 109842e9a92fSRobert Love fh = fc_frame_header_get(fp); 109942e9a92fSRobert Love ap = fc_frame_payload_get(fp, sizeof(*ap)); 110042e9a92fSRobert Love memset(ap, 0, sizeof(*ap)); 110142e9a92fSRobert Love sp = &ep->seq; 110242e9a92fSRobert Love ap->ba_high_seq_cnt = htons(0xffff); 110342e9a92fSRobert Love if (sp->ssb_stat & SSB_ST_RESP) { 110442e9a92fSRobert Love ap->ba_seq_id = sp->id; 110542e9a92fSRobert Love ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL; 110642e9a92fSRobert Love ap->ba_high_seq_cnt = fh->fh_seq_cnt; 110742e9a92fSRobert Love ap->ba_low_seq_cnt = htons(sp->cnt); 110842e9a92fSRobert Love } 1109a7e84f2bSVasu Dev sp = fc_seq_start_next_locked(sp); 111042e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 111142e9a92fSRobert Love fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS); 111242e9a92fSRobert Love fc_frame_free(rx_fp); 111342e9a92fSRobert Love return; 111442e9a92fSRobert Love 111542e9a92fSRobert Love reject: 111642e9a92fSRobert Love fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID); 111742e9a92fSRobert Love free: 111842e9a92fSRobert Love fc_frame_free(rx_fp); 111942e9a92fSRobert Love } 112042e9a92fSRobert Love 112142e9a92fSRobert Love /* 112242e9a92fSRobert Love * Handle receive where the other end is originating the sequence. 112342e9a92fSRobert Love */ 112442e9a92fSRobert Love static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp, 112542e9a92fSRobert Love struct fc_frame *fp) 112642e9a92fSRobert Love { 112742e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 112842e9a92fSRobert Love struct fc_seq *sp = NULL; 112942e9a92fSRobert Love struct fc_exch *ep = NULL; 113042e9a92fSRobert Love enum fc_sof sof; 113142e9a92fSRobert Love enum fc_eof eof; 113242e9a92fSRobert Love u32 f_ctl; 113342e9a92fSRobert Love enum fc_pf_rjt_reason reject; 113442e9a92fSRobert Love 113542e9a92fSRobert Love fr_seq(fp) = NULL; 113652ff878cSVasu Dev reject = fc_seq_lookup_recip(lp, mp, fp); 113742e9a92fSRobert Love if (reject == FC_RJT_NONE) { 113842e9a92fSRobert Love sp = fr_seq(fp); /* sequence will be held */ 113942e9a92fSRobert Love ep = fc_seq_exch(sp); 114042e9a92fSRobert Love sof = fr_sof(fp); 114142e9a92fSRobert Love eof = fr_eof(fp); 114242e9a92fSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 114342e9a92fSRobert Love fc_seq_send_ack(sp, fp); 114442e9a92fSRobert Love 114542e9a92fSRobert Love /* 114642e9a92fSRobert Love * Call the receive function. 114742e9a92fSRobert Love * 114842e9a92fSRobert Love * The receive function may allocate a new sequence 114942e9a92fSRobert Love * over the old one, so we shouldn't change the 115042e9a92fSRobert Love * sequence after this. 115142e9a92fSRobert Love * 115242e9a92fSRobert Love * The frame will be freed by the receive function. 115342e9a92fSRobert Love * If new exch resp handler is valid then call that 115442e9a92fSRobert Love * first. 115542e9a92fSRobert Love */ 115642e9a92fSRobert Love if (ep->resp) 115742e9a92fSRobert Love ep->resp(sp, fp, ep->arg); 115842e9a92fSRobert Love else 115942e9a92fSRobert Love lp->tt.lport_recv(lp, sp, fp); 116042e9a92fSRobert Love fc_exch_release(ep); /* release from lookup */ 116142e9a92fSRobert Love } else { 1162d459b7eaSRobert Love FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject); 116342e9a92fSRobert Love fc_frame_free(fp); 116442e9a92fSRobert Love } 116542e9a92fSRobert Love } 116642e9a92fSRobert Love 116742e9a92fSRobert Love /* 116842e9a92fSRobert Love * Handle receive where the other end is originating the sequence in 116942e9a92fSRobert Love * response to our exchange. 117042e9a92fSRobert Love */ 117142e9a92fSRobert Love static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp) 117242e9a92fSRobert Love { 117342e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 117442e9a92fSRobert Love struct fc_seq *sp; 117542e9a92fSRobert Love struct fc_exch *ep; 117642e9a92fSRobert Love enum fc_sof sof; 117742e9a92fSRobert Love u32 f_ctl; 117842e9a92fSRobert Love void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg); 117942e9a92fSRobert Love void *ex_resp_arg; 118042e9a92fSRobert Love int rc; 118142e9a92fSRobert Love 118242e9a92fSRobert Love ep = fc_exch_find(mp, ntohs(fh->fh_ox_id)); 118342e9a92fSRobert Love if (!ep) { 118442e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 118542e9a92fSRobert Love goto out; 118642e9a92fSRobert Love } 118730121d14SSteve Ma if (ep->esb_stat & ESB_ST_COMPLETE) { 118830121d14SSteve Ma atomic_inc(&mp->stats.xid_not_found); 118930121d14SSteve Ma goto out; 119030121d14SSteve Ma } 119142e9a92fSRobert Love if (ep->rxid == FC_XID_UNKNOWN) 119242e9a92fSRobert Love ep->rxid = ntohs(fh->fh_rx_id); 119342e9a92fSRobert Love if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) { 119442e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 119542e9a92fSRobert Love goto rel; 119642e9a92fSRobert Love } 119742e9a92fSRobert Love if (ep->did != ntoh24(fh->fh_s_id) && 119842e9a92fSRobert Love ep->did != FC_FID_FLOGI) { 119942e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 120042e9a92fSRobert Love goto rel; 120142e9a92fSRobert Love } 120242e9a92fSRobert Love sof = fr_sof(fp); 120342e9a92fSRobert Love if (fc_sof_is_init(sof)) { 120442e9a92fSRobert Love sp = fc_seq_start_next(&ep->seq); 120542e9a92fSRobert Love sp->id = fh->fh_seq_id; 120642e9a92fSRobert Love sp->ssb_stat |= SSB_ST_RESP; 120742e9a92fSRobert Love } else { 120842e9a92fSRobert Love sp = &ep->seq; 120942e9a92fSRobert Love if (sp->id != fh->fh_seq_id) { 121042e9a92fSRobert Love atomic_inc(&mp->stats.seq_not_found); 121142e9a92fSRobert Love goto rel; 121242e9a92fSRobert Love } 121342e9a92fSRobert Love } 121442e9a92fSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 121542e9a92fSRobert Love fr_seq(fp) = sp; 121642e9a92fSRobert Love if (f_ctl & FC_FC_SEQ_INIT) 121742e9a92fSRobert Love ep->esb_stat |= ESB_ST_SEQ_INIT; 121842e9a92fSRobert Love 121942e9a92fSRobert Love if (fc_sof_needs_ack(sof)) 122042e9a92fSRobert Love fc_seq_send_ack(sp, fp); 122142e9a92fSRobert Love resp = ep->resp; 122242e9a92fSRobert Love ex_resp_arg = ep->arg; 122342e9a92fSRobert Love 122442e9a92fSRobert Love if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T && 122542e9a92fSRobert Love (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) == 122642e9a92fSRobert Love (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) { 122742e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 122842e9a92fSRobert Love rc = fc_exch_done_locked(ep); 122942e9a92fSRobert Love WARN_ON(fc_seq_exch(sp) != ep); 123042e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 123142e9a92fSRobert Love if (!rc) 1232b2f0091fSVasu Dev fc_exch_delete(ep); 123342e9a92fSRobert Love } 123442e9a92fSRobert Love 123542e9a92fSRobert Love /* 123642e9a92fSRobert Love * Call the receive function. 123742e9a92fSRobert Love * The sequence is held (has a refcnt) for us, 123842e9a92fSRobert Love * but not for the receive function. 123942e9a92fSRobert Love * 124042e9a92fSRobert Love * The receive function may allocate a new sequence 124142e9a92fSRobert Love * over the old one, so we shouldn't change the 124242e9a92fSRobert Love * sequence after this. 124342e9a92fSRobert Love * 124442e9a92fSRobert Love * The frame will be freed by the receive function. 124542e9a92fSRobert Love * If new exch resp handler is valid then call that 124642e9a92fSRobert Love * first. 124742e9a92fSRobert Love */ 124842e9a92fSRobert Love if (resp) 124942e9a92fSRobert Love resp(sp, fp, ex_resp_arg); 125042e9a92fSRobert Love else 125142e9a92fSRobert Love fc_frame_free(fp); 125242e9a92fSRobert Love fc_exch_release(ep); 125342e9a92fSRobert Love return; 125442e9a92fSRobert Love rel: 125542e9a92fSRobert Love fc_exch_release(ep); 125642e9a92fSRobert Love out: 125742e9a92fSRobert Love fc_frame_free(fp); 125842e9a92fSRobert Love } 125942e9a92fSRobert Love 126042e9a92fSRobert Love /* 126142e9a92fSRobert Love * Handle receive for a sequence where other end is responding to our sequence. 126242e9a92fSRobert Love */ 126342e9a92fSRobert Love static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp) 126442e9a92fSRobert Love { 126542e9a92fSRobert Love struct fc_seq *sp; 126642e9a92fSRobert Love 126742e9a92fSRobert Love sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */ 1268d459b7eaSRobert Love 1269d459b7eaSRobert Love if (!sp) 127042e9a92fSRobert Love atomic_inc(&mp->stats.xid_not_found); 1271d459b7eaSRobert Love else 127242e9a92fSRobert Love atomic_inc(&mp->stats.non_bls_resp); 1273d459b7eaSRobert Love 127442e9a92fSRobert Love fc_frame_free(fp); 127542e9a92fSRobert Love } 127642e9a92fSRobert Love 127742e9a92fSRobert Love /* 127842e9a92fSRobert Love * Handle the response to an ABTS for exchange or sequence. 127942e9a92fSRobert Love * This can be BA_ACC or BA_RJT. 128042e9a92fSRobert Love */ 128142e9a92fSRobert Love static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp) 128242e9a92fSRobert Love { 128342e9a92fSRobert Love void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg); 128442e9a92fSRobert Love void *ex_resp_arg; 128542e9a92fSRobert Love struct fc_frame_header *fh; 128642e9a92fSRobert Love struct fc_ba_acc *ap; 128742e9a92fSRobert Love struct fc_seq *sp; 128842e9a92fSRobert Love u16 low; 128942e9a92fSRobert Love u16 high; 129042e9a92fSRobert Love int rc = 1, has_rec = 0; 129142e9a92fSRobert Love 129242e9a92fSRobert Love fh = fc_frame_header_get(fp); 12937414705eSRobert Love FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl, 12947414705eSRobert Love fc_exch_rctl_name(fh->fh_r_ctl)); 129542e9a92fSRobert Love 129642e9a92fSRobert Love if (cancel_delayed_work_sync(&ep->timeout_work)) 129742e9a92fSRobert Love fc_exch_release(ep); /* release from pending timer hold */ 129842e9a92fSRobert Love 129942e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 130042e9a92fSRobert Love switch (fh->fh_r_ctl) { 130142e9a92fSRobert Love case FC_RCTL_BA_ACC: 130242e9a92fSRobert Love ap = fc_frame_payload_get(fp, sizeof(*ap)); 130342e9a92fSRobert Love if (!ap) 130442e9a92fSRobert Love break; 130542e9a92fSRobert Love 130642e9a92fSRobert Love /* 130742e9a92fSRobert Love * Decide whether to establish a Recovery Qualifier. 130842e9a92fSRobert Love * We do this if there is a non-empty SEQ_CNT range and 130942e9a92fSRobert Love * SEQ_ID is the same as the one we aborted. 131042e9a92fSRobert Love */ 131142e9a92fSRobert Love low = ntohs(ap->ba_low_seq_cnt); 131242e9a92fSRobert Love high = ntohs(ap->ba_high_seq_cnt); 131342e9a92fSRobert Love if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 && 131442e9a92fSRobert Love (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL || 131542e9a92fSRobert Love ap->ba_seq_id == ep->seq_id) && low != high) { 131642e9a92fSRobert Love ep->esb_stat |= ESB_ST_REC_QUAL; 131742e9a92fSRobert Love fc_exch_hold(ep); /* hold for recovery qualifier */ 131842e9a92fSRobert Love has_rec = 1; 131942e9a92fSRobert Love } 132042e9a92fSRobert Love break; 132142e9a92fSRobert Love case FC_RCTL_BA_RJT: 132242e9a92fSRobert Love break; 132342e9a92fSRobert Love default: 132442e9a92fSRobert Love break; 132542e9a92fSRobert Love } 132642e9a92fSRobert Love 132742e9a92fSRobert Love resp = ep->resp; 132842e9a92fSRobert Love ex_resp_arg = ep->arg; 132942e9a92fSRobert Love 133042e9a92fSRobert Love /* do we need to do some other checks here. Can we reuse more of 133142e9a92fSRobert Love * fc_exch_recv_seq_resp 133242e9a92fSRobert Love */ 133342e9a92fSRobert Love sp = &ep->seq; 133442e9a92fSRobert Love /* 133542e9a92fSRobert Love * do we want to check END_SEQ as well as LAST_SEQ here? 133642e9a92fSRobert Love */ 133742e9a92fSRobert Love if (ep->fh_type != FC_TYPE_FCP && 133842e9a92fSRobert Love ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ) 133942e9a92fSRobert Love rc = fc_exch_done_locked(ep); 134042e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 134142e9a92fSRobert Love if (!rc) 1342b2f0091fSVasu Dev fc_exch_delete(ep); 134342e9a92fSRobert Love 134442e9a92fSRobert Love if (resp) 134542e9a92fSRobert Love resp(sp, fp, ex_resp_arg); 134642e9a92fSRobert Love else 134742e9a92fSRobert Love fc_frame_free(fp); 134842e9a92fSRobert Love 134942e9a92fSRobert Love if (has_rec) 135042e9a92fSRobert Love fc_exch_timer_set(ep, ep->r_a_tov); 135142e9a92fSRobert Love 135242e9a92fSRobert Love } 135342e9a92fSRobert Love 135442e9a92fSRobert Love /* 135542e9a92fSRobert Love * Receive BLS sequence. 135642e9a92fSRobert Love * This is always a sequence initiated by the remote side. 135742e9a92fSRobert Love * We may be either the originator or recipient of the exchange. 135842e9a92fSRobert Love */ 135942e9a92fSRobert Love static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp) 136042e9a92fSRobert Love { 136142e9a92fSRobert Love struct fc_frame_header *fh; 136242e9a92fSRobert Love struct fc_exch *ep; 136342e9a92fSRobert Love u32 f_ctl; 136442e9a92fSRobert Love 136542e9a92fSRobert Love fh = fc_frame_header_get(fp); 136642e9a92fSRobert Love f_ctl = ntoh24(fh->fh_f_ctl); 136742e9a92fSRobert Love fr_seq(fp) = NULL; 136842e9a92fSRobert Love 136942e9a92fSRobert Love ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ? 137042e9a92fSRobert Love ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id)); 137142e9a92fSRobert Love if (ep && (f_ctl & FC_FC_SEQ_INIT)) { 137242e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 137342e9a92fSRobert Love ep->esb_stat |= ESB_ST_SEQ_INIT; 137442e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 137542e9a92fSRobert Love } 137642e9a92fSRobert Love if (f_ctl & FC_FC_SEQ_CTX) { 137742e9a92fSRobert Love /* 137842e9a92fSRobert Love * A response to a sequence we initiated. 137942e9a92fSRobert Love * This should only be ACKs for class 2 or F. 138042e9a92fSRobert Love */ 138142e9a92fSRobert Love switch (fh->fh_r_ctl) { 138242e9a92fSRobert Love case FC_RCTL_ACK_1: 138342e9a92fSRobert Love case FC_RCTL_ACK_0: 138442e9a92fSRobert Love break; 138542e9a92fSRobert Love default: 13867414705eSRobert Love FC_EXCH_DBG(ep, "BLS rctl %x - %s received", 138742e9a92fSRobert Love fh->fh_r_ctl, 138842e9a92fSRobert Love fc_exch_rctl_name(fh->fh_r_ctl)); 138942e9a92fSRobert Love break; 139042e9a92fSRobert Love } 139142e9a92fSRobert Love fc_frame_free(fp); 139242e9a92fSRobert Love } else { 139342e9a92fSRobert Love switch (fh->fh_r_ctl) { 139442e9a92fSRobert Love case FC_RCTL_BA_RJT: 139542e9a92fSRobert Love case FC_RCTL_BA_ACC: 139642e9a92fSRobert Love if (ep) 139742e9a92fSRobert Love fc_exch_abts_resp(ep, fp); 139842e9a92fSRobert Love else 139942e9a92fSRobert Love fc_frame_free(fp); 140042e9a92fSRobert Love break; 140142e9a92fSRobert Love case FC_RCTL_BA_ABTS: 140242e9a92fSRobert Love fc_exch_recv_abts(ep, fp); 140342e9a92fSRobert Love break; 140442e9a92fSRobert Love default: /* ignore junk */ 140542e9a92fSRobert Love fc_frame_free(fp); 140642e9a92fSRobert Love break; 140742e9a92fSRobert Love } 140842e9a92fSRobert Love } 140942e9a92fSRobert Love if (ep) 141042e9a92fSRobert Love fc_exch_release(ep); /* release hold taken by fc_exch_find */ 141142e9a92fSRobert Love } 141242e9a92fSRobert Love 141342e9a92fSRobert Love /* 141442e9a92fSRobert Love * Accept sequence with LS_ACC. 141542e9a92fSRobert Love * If this fails due to allocation or transmit congestion, assume the 141642e9a92fSRobert Love * originator will repeat the sequence. 141742e9a92fSRobert Love */ 141842e9a92fSRobert Love static void fc_seq_ls_acc(struct fc_seq *req_sp) 141942e9a92fSRobert Love { 142042e9a92fSRobert Love struct fc_seq *sp; 142142e9a92fSRobert Love struct fc_els_ls_acc *acc; 142242e9a92fSRobert Love struct fc_frame *fp; 142342e9a92fSRobert Love 142442e9a92fSRobert Love sp = fc_seq_start_next(req_sp); 142542e9a92fSRobert Love fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc)); 142642e9a92fSRobert Love if (fp) { 142742e9a92fSRobert Love acc = fc_frame_payload_get(fp, sizeof(*acc)); 142842e9a92fSRobert Love memset(acc, 0, sizeof(*acc)); 142942e9a92fSRobert Love acc->la_cmd = ELS_LS_ACC; 143042e9a92fSRobert Love fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS); 143142e9a92fSRobert Love } 143242e9a92fSRobert Love } 143342e9a92fSRobert Love 143442e9a92fSRobert Love /* 143542e9a92fSRobert Love * Reject sequence with ELS LS_RJT. 143642e9a92fSRobert Love * If this fails due to allocation or transmit congestion, assume the 143742e9a92fSRobert Love * originator will repeat the sequence. 143842e9a92fSRobert Love */ 143942e9a92fSRobert Love static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason, 144042e9a92fSRobert Love enum fc_els_rjt_explan explan) 144142e9a92fSRobert Love { 144242e9a92fSRobert Love struct fc_seq *sp; 144342e9a92fSRobert Love struct fc_els_ls_rjt *rjt; 144442e9a92fSRobert Love struct fc_frame *fp; 144542e9a92fSRobert Love 144642e9a92fSRobert Love sp = fc_seq_start_next(req_sp); 144742e9a92fSRobert Love fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt)); 144842e9a92fSRobert Love if (fp) { 144942e9a92fSRobert Love rjt = fc_frame_payload_get(fp, sizeof(*rjt)); 145042e9a92fSRobert Love memset(rjt, 0, sizeof(*rjt)); 145142e9a92fSRobert Love rjt->er_cmd = ELS_LS_RJT; 145242e9a92fSRobert Love rjt->er_reason = reason; 145342e9a92fSRobert Love rjt->er_explan = explan; 145442e9a92fSRobert Love fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS); 145542e9a92fSRobert Love } 145642e9a92fSRobert Love } 145742e9a92fSRobert Love 145842e9a92fSRobert Love static void fc_exch_reset(struct fc_exch *ep) 145942e9a92fSRobert Love { 146042e9a92fSRobert Love struct fc_seq *sp; 146142e9a92fSRobert Love void (*resp)(struct fc_seq *, struct fc_frame *, void *); 146242e9a92fSRobert Love void *arg; 146342e9a92fSRobert Love int rc = 1; 146442e9a92fSRobert Love 146542e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 146642e9a92fSRobert Love ep->state |= FC_EX_RST_CLEANUP; 146742e9a92fSRobert Love /* 146842e9a92fSRobert Love * we really want to call del_timer_sync, but cannot due 146942e9a92fSRobert Love * to the lport calling with the lport lock held (some resp 147042e9a92fSRobert Love * functions can also grab the lport lock which could cause 147142e9a92fSRobert Love * a deadlock). 147242e9a92fSRobert Love */ 147342e9a92fSRobert Love if (cancel_delayed_work(&ep->timeout_work)) 147442e9a92fSRobert Love atomic_dec(&ep->ex_refcnt); /* drop hold for timer */ 147542e9a92fSRobert Love resp = ep->resp; 147642e9a92fSRobert Love ep->resp = NULL; 147742e9a92fSRobert Love if (ep->esb_stat & ESB_ST_REC_QUAL) 147842e9a92fSRobert Love atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */ 147942e9a92fSRobert Love ep->esb_stat &= ~ESB_ST_REC_QUAL; 148042e9a92fSRobert Love arg = ep->arg; 148142e9a92fSRobert Love sp = &ep->seq; 148242e9a92fSRobert Love rc = fc_exch_done_locked(ep); 148342e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 148442e9a92fSRobert Love if (!rc) 1485b2f0091fSVasu Dev fc_exch_delete(ep); 148642e9a92fSRobert Love 148742e9a92fSRobert Love if (resp) 148842e9a92fSRobert Love resp(sp, ERR_PTR(-FC_EX_CLOSED), arg); 148942e9a92fSRobert Love } 149042e9a92fSRobert Love 1491b2f0091fSVasu Dev /** 1492b2f0091fSVasu Dev * fc_exch_pool_reset() - Resets an per cpu exches pool. 1493b2f0091fSVasu Dev * @lport: ptr to the local port 1494b2f0091fSVasu Dev * @pool: ptr to the per cpu exches pool 1495b2f0091fSVasu Dev * @sid: source FC ID 1496b2f0091fSVasu Dev * @did: destination FC ID 1497b2f0091fSVasu Dev * 1498b2f0091fSVasu Dev * Resets an per cpu exches pool, releasing its all sequences 1499b2f0091fSVasu Dev * and exchanges. If sid is non-zero, then reset only exchanges 1500b2f0091fSVasu Dev * we sourced from that FID. If did is non-zero, reset only 1501b2f0091fSVasu Dev * exchanges destined to that FID. 150242e9a92fSRobert Love */ 1503b2f0091fSVasu Dev static void fc_exch_pool_reset(struct fc_lport *lport, 1504b2f0091fSVasu Dev struct fc_exch_pool *pool, 1505b2f0091fSVasu Dev u32 sid, u32 did) 150642e9a92fSRobert Love { 150742e9a92fSRobert Love struct fc_exch *ep; 150842e9a92fSRobert Love struct fc_exch *next; 150942e9a92fSRobert Love 1510b2f0091fSVasu Dev spin_lock_bh(&pool->lock); 151142e9a92fSRobert Love restart: 1512b2f0091fSVasu Dev list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) { 1513b2f0091fSVasu Dev if ((lport == ep->lp) && 151452ff878cSVasu Dev (sid == 0 || sid == ep->sid) && 151542e9a92fSRobert Love (did == 0 || did == ep->did)) { 151642e9a92fSRobert Love fc_exch_hold(ep); 1517b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 151842e9a92fSRobert Love 151942e9a92fSRobert Love fc_exch_reset(ep); 152042e9a92fSRobert Love 152142e9a92fSRobert Love fc_exch_release(ep); 1522b2f0091fSVasu Dev spin_lock_bh(&pool->lock); 152342e9a92fSRobert Love 152442e9a92fSRobert Love /* 152552ff878cSVasu Dev * must restart loop incase while lock 152652ff878cSVasu Dev * was down multiple eps were released. 152742e9a92fSRobert Love */ 152842e9a92fSRobert Love goto restart; 152942e9a92fSRobert Love } 153042e9a92fSRobert Love } 1531b2f0091fSVasu Dev spin_unlock_bh(&pool->lock); 1532b2f0091fSVasu Dev } 1533b2f0091fSVasu Dev 1534b2f0091fSVasu Dev /** 1535b2f0091fSVasu Dev * fc_exch_mgr_reset() - Resets all EMs of a lport 1536b2f0091fSVasu Dev * @lport: ptr to the local port 1537b2f0091fSVasu Dev * @sid: source FC ID 1538b2f0091fSVasu Dev * @did: destination FC ID 1539b2f0091fSVasu Dev * 1540b2f0091fSVasu Dev * Reset all EMs of a lport, releasing its all sequences and 1541b2f0091fSVasu Dev * exchanges. If sid is non-zero, then reset only exchanges 1542b2f0091fSVasu Dev * we sourced from that FID. If did is non-zero, reset only 1543b2f0091fSVasu Dev * exchanges destined to that FID. 1544b2f0091fSVasu Dev */ 1545b2f0091fSVasu Dev void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did) 1546b2f0091fSVasu Dev { 1547b2f0091fSVasu Dev struct fc_exch_mgr_anchor *ema; 1548b2f0091fSVasu Dev unsigned int cpu; 1549b2f0091fSVasu Dev 1550b2f0091fSVasu Dev list_for_each_entry(ema, &lport->ema_list, ema_list) { 1551b2f0091fSVasu Dev for_each_possible_cpu(cpu) 1552b2f0091fSVasu Dev fc_exch_pool_reset(lport, 1553b2f0091fSVasu Dev per_cpu_ptr(ema->mp->pool, cpu), 1554b2f0091fSVasu Dev sid, did); 155542e9a92fSRobert Love } 155652ff878cSVasu Dev } 155742e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_reset); 155842e9a92fSRobert Love 155942e9a92fSRobert Love /* 156042e9a92fSRobert Love * Handle incoming ELS REC - Read Exchange Concise. 156142e9a92fSRobert Love * Note that the requesting port may be different than the S_ID in the request. 156242e9a92fSRobert Love */ 156342e9a92fSRobert Love static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp) 156442e9a92fSRobert Love { 156542e9a92fSRobert Love struct fc_frame *fp; 156642e9a92fSRobert Love struct fc_exch *ep; 156742e9a92fSRobert Love struct fc_exch_mgr *em; 156842e9a92fSRobert Love struct fc_els_rec *rp; 156942e9a92fSRobert Love struct fc_els_rec_acc *acc; 157042e9a92fSRobert Love enum fc_els_rjt_reason reason = ELS_RJT_LOGIC; 157142e9a92fSRobert Love enum fc_els_rjt_explan explan; 157242e9a92fSRobert Love u32 sid; 157342e9a92fSRobert Love u16 rxid; 157442e9a92fSRobert Love u16 oxid; 157542e9a92fSRobert Love 157642e9a92fSRobert Love rp = fc_frame_payload_get(rfp, sizeof(*rp)); 157742e9a92fSRobert Love explan = ELS_EXPL_INV_LEN; 157842e9a92fSRobert Love if (!rp) 157942e9a92fSRobert Love goto reject; 158042e9a92fSRobert Love sid = ntoh24(rp->rec_s_id); 158142e9a92fSRobert Love rxid = ntohs(rp->rec_rx_id); 158242e9a92fSRobert Love oxid = ntohs(rp->rec_ox_id); 158342e9a92fSRobert Love 158442e9a92fSRobert Love /* 158542e9a92fSRobert Love * Currently it's hard to find the local S_ID from the exchange 158642e9a92fSRobert Love * manager. This will eventually be fixed, but for now it's easier 158742e9a92fSRobert Love * to lookup the subject exchange twice, once as if we were 158842e9a92fSRobert Love * the initiator, and then again if we weren't. 158942e9a92fSRobert Love */ 159042e9a92fSRobert Love em = fc_seq_exch(sp)->em; 159142e9a92fSRobert Love ep = fc_exch_find(em, oxid); 159242e9a92fSRobert Love explan = ELS_EXPL_OXID_RXID; 159342e9a92fSRobert Love if (ep && ep->oid == sid) { 159442e9a92fSRobert Love if (ep->rxid != FC_XID_UNKNOWN && 159542e9a92fSRobert Love rxid != FC_XID_UNKNOWN && 159642e9a92fSRobert Love ep->rxid != rxid) 159742e9a92fSRobert Love goto rel; 159842e9a92fSRobert Love } else { 159942e9a92fSRobert Love if (ep) 160042e9a92fSRobert Love fc_exch_release(ep); 160142e9a92fSRobert Love ep = NULL; 160242e9a92fSRobert Love if (rxid != FC_XID_UNKNOWN) 160342e9a92fSRobert Love ep = fc_exch_find(em, rxid); 160442e9a92fSRobert Love if (!ep) 160542e9a92fSRobert Love goto reject; 160642e9a92fSRobert Love } 160742e9a92fSRobert Love 160842e9a92fSRobert Love fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc)); 160942e9a92fSRobert Love if (!fp) { 161042e9a92fSRobert Love fc_exch_done(sp); 161142e9a92fSRobert Love goto out; 161242e9a92fSRobert Love } 161342e9a92fSRobert Love sp = fc_seq_start_next(sp); 161442e9a92fSRobert Love acc = fc_frame_payload_get(fp, sizeof(*acc)); 161542e9a92fSRobert Love memset(acc, 0, sizeof(*acc)); 161642e9a92fSRobert Love acc->reca_cmd = ELS_LS_ACC; 161742e9a92fSRobert Love acc->reca_ox_id = rp->rec_ox_id; 161842e9a92fSRobert Love memcpy(acc->reca_ofid, rp->rec_s_id, 3); 161942e9a92fSRobert Love acc->reca_rx_id = htons(ep->rxid); 162042e9a92fSRobert Love if (ep->sid == ep->oid) 162142e9a92fSRobert Love hton24(acc->reca_rfid, ep->did); 162242e9a92fSRobert Love else 162342e9a92fSRobert Love hton24(acc->reca_rfid, ep->sid); 162442e9a92fSRobert Love acc->reca_fc4value = htonl(ep->seq.rec_data); 162542e9a92fSRobert Love acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP | 162642e9a92fSRobert Love ESB_ST_SEQ_INIT | 162742e9a92fSRobert Love ESB_ST_COMPLETE)); 162842e9a92fSRobert Love sp = fc_seq_start_next(sp); 162942e9a92fSRobert Love fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS); 163042e9a92fSRobert Love out: 163142e9a92fSRobert Love fc_exch_release(ep); 163242e9a92fSRobert Love fc_frame_free(rfp); 163342e9a92fSRobert Love return; 163442e9a92fSRobert Love 163542e9a92fSRobert Love rel: 163642e9a92fSRobert Love fc_exch_release(ep); 163742e9a92fSRobert Love reject: 163842e9a92fSRobert Love fc_seq_ls_rjt(sp, reason, explan); 163942e9a92fSRobert Love fc_frame_free(rfp); 164042e9a92fSRobert Love } 164142e9a92fSRobert Love 164242e9a92fSRobert Love /* 164342e9a92fSRobert Love * Handle response from RRQ. 164442e9a92fSRobert Love * Not much to do here, really. 164542e9a92fSRobert Love * Should report errors. 164642e9a92fSRobert Love * 164742e9a92fSRobert Love * TODO: fix error handler. 164842e9a92fSRobert Love */ 164942e9a92fSRobert Love static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg) 165042e9a92fSRobert Love { 165142e9a92fSRobert Love struct fc_exch *aborted_ep = arg; 165242e9a92fSRobert Love unsigned int op; 165342e9a92fSRobert Love 165442e9a92fSRobert Love if (IS_ERR(fp)) { 165542e9a92fSRobert Love int err = PTR_ERR(fp); 165642e9a92fSRobert Love 165778342da3SVasu Dev if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT) 165842e9a92fSRobert Love goto cleanup; 16597414705eSRobert Love FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, " 16607414705eSRobert Love "frame error %d\n", err); 166142e9a92fSRobert Love return; 166242e9a92fSRobert Love } 166342e9a92fSRobert Love 166442e9a92fSRobert Love op = fc_frame_payload_op(fp); 166542e9a92fSRobert Love fc_frame_free(fp); 166642e9a92fSRobert Love 166742e9a92fSRobert Love switch (op) { 166842e9a92fSRobert Love case ELS_LS_RJT: 16697414705eSRobert Love FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ"); 167042e9a92fSRobert Love /* fall through */ 167142e9a92fSRobert Love case ELS_LS_ACC: 167242e9a92fSRobert Love goto cleanup; 167342e9a92fSRobert Love default: 16747414705eSRobert Love FC_EXCH_DBG(aborted_ep, "unexpected response op %x " 16757414705eSRobert Love "for RRQ", op); 167642e9a92fSRobert Love return; 167742e9a92fSRobert Love } 167842e9a92fSRobert Love 167942e9a92fSRobert Love cleanup: 168042e9a92fSRobert Love fc_exch_done(&aborted_ep->seq); 168142e9a92fSRobert Love /* drop hold for rec qual */ 168242e9a92fSRobert Love fc_exch_release(aborted_ep); 168342e9a92fSRobert Love } 168442e9a92fSRobert Love 1685*1a7b75aeSRobert Love 1686*1a7b75aeSRobert Love /** 1687*1a7b75aeSRobert Love * This function is for exch_seq_send function pointer in 1688*1a7b75aeSRobert Love * struct libfc_function_template, see comment block on 1689*1a7b75aeSRobert Love * exch_seq_send for description of this function. 1690*1a7b75aeSRobert Love */ 1691*1a7b75aeSRobert Love static struct fc_seq *fc_exch_seq_send(struct fc_lport *lp, 1692*1a7b75aeSRobert Love struct fc_frame *fp, 1693*1a7b75aeSRobert Love void (*resp)(struct fc_seq *, 1694*1a7b75aeSRobert Love struct fc_frame *fp, 1695*1a7b75aeSRobert Love void *arg), 1696*1a7b75aeSRobert Love void (*destructor)(struct fc_seq *, 1697*1a7b75aeSRobert Love void *), 1698*1a7b75aeSRobert Love void *arg, u32 timer_msec) 1699*1a7b75aeSRobert Love { 1700*1a7b75aeSRobert Love struct fc_exch *ep; 1701*1a7b75aeSRobert Love struct fc_seq *sp = NULL; 1702*1a7b75aeSRobert Love struct fc_frame_header *fh; 1703*1a7b75aeSRobert Love int rc = 1; 1704*1a7b75aeSRobert Love 1705*1a7b75aeSRobert Love ep = fc_exch_alloc(lp, fp); 1706*1a7b75aeSRobert Love if (!ep) { 1707*1a7b75aeSRobert Love fc_frame_free(fp); 1708*1a7b75aeSRobert Love return NULL; 1709*1a7b75aeSRobert Love } 1710*1a7b75aeSRobert Love ep->esb_stat |= ESB_ST_SEQ_INIT; 1711*1a7b75aeSRobert Love fh = fc_frame_header_get(fp); 1712*1a7b75aeSRobert Love fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id)); 1713*1a7b75aeSRobert Love ep->resp = resp; 1714*1a7b75aeSRobert Love ep->destructor = destructor; 1715*1a7b75aeSRobert Love ep->arg = arg; 1716*1a7b75aeSRobert Love ep->r_a_tov = FC_DEF_R_A_TOV; 1717*1a7b75aeSRobert Love ep->lp = lp; 1718*1a7b75aeSRobert Love sp = &ep->seq; 1719*1a7b75aeSRobert Love 1720*1a7b75aeSRobert Love ep->fh_type = fh->fh_type; /* save for possbile timeout handling */ 1721*1a7b75aeSRobert Love ep->f_ctl = ntoh24(fh->fh_f_ctl); 1722*1a7b75aeSRobert Love fc_exch_setup_hdr(ep, fp, ep->f_ctl); 1723*1a7b75aeSRobert Love sp->cnt++; 1724*1a7b75aeSRobert Love 1725*1a7b75aeSRobert Love if (ep->xid <= lp->lro_xid) 1726*1a7b75aeSRobert Love fc_fcp_ddp_setup(fr_fsp(fp), ep->xid); 1727*1a7b75aeSRobert Love 1728*1a7b75aeSRobert Love if (unlikely(lp->tt.frame_send(lp, fp))) 1729*1a7b75aeSRobert Love goto err; 1730*1a7b75aeSRobert Love 1731*1a7b75aeSRobert Love if (timer_msec) 1732*1a7b75aeSRobert Love fc_exch_timer_set_locked(ep, timer_msec); 1733*1a7b75aeSRobert Love ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */ 1734*1a7b75aeSRobert Love 1735*1a7b75aeSRobert Love if (ep->f_ctl & FC_FC_SEQ_INIT) 1736*1a7b75aeSRobert Love ep->esb_stat &= ~ESB_ST_SEQ_INIT; 1737*1a7b75aeSRobert Love spin_unlock_bh(&ep->ex_lock); 1738*1a7b75aeSRobert Love return sp; 1739*1a7b75aeSRobert Love err: 1740*1a7b75aeSRobert Love rc = fc_exch_done_locked(ep); 1741*1a7b75aeSRobert Love spin_unlock_bh(&ep->ex_lock); 1742*1a7b75aeSRobert Love if (!rc) 1743*1a7b75aeSRobert Love fc_exch_delete(ep); 1744*1a7b75aeSRobert Love return NULL; 1745*1a7b75aeSRobert Love } 1746*1a7b75aeSRobert Love 174742e9a92fSRobert Love /* 174842e9a92fSRobert Love * Send ELS RRQ - Reinstate Recovery Qualifier. 174942e9a92fSRobert Love * This tells the remote port to stop blocking the use of 175042e9a92fSRobert Love * the exchange and the seq_cnt range. 175142e9a92fSRobert Love */ 175242e9a92fSRobert Love static void fc_exch_rrq(struct fc_exch *ep) 175342e9a92fSRobert Love { 175442e9a92fSRobert Love struct fc_lport *lp; 175542e9a92fSRobert Love struct fc_els_rrq *rrq; 175642e9a92fSRobert Love struct fc_frame *fp; 175742e9a92fSRobert Love u32 did; 175842e9a92fSRobert Love 175942e9a92fSRobert Love lp = ep->lp; 176042e9a92fSRobert Love 176142e9a92fSRobert Love fp = fc_frame_alloc(lp, sizeof(*rrq)); 176242e9a92fSRobert Love if (!fp) 1763a0cc1eccSVasu Dev goto retry; 1764a0cc1eccSVasu Dev 176542e9a92fSRobert Love rrq = fc_frame_payload_get(fp, sizeof(*rrq)); 176642e9a92fSRobert Love memset(rrq, 0, sizeof(*rrq)); 176742e9a92fSRobert Love rrq->rrq_cmd = ELS_RRQ; 176842e9a92fSRobert Love hton24(rrq->rrq_s_id, ep->sid); 176942e9a92fSRobert Love rrq->rrq_ox_id = htons(ep->oxid); 177042e9a92fSRobert Love rrq->rrq_rx_id = htons(ep->rxid); 177142e9a92fSRobert Love 177242e9a92fSRobert Love did = ep->did; 177342e9a92fSRobert Love if (ep->esb_stat & ESB_ST_RESP) 177442e9a92fSRobert Love did = ep->sid; 177542e9a92fSRobert Love 177642e9a92fSRobert Love fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did, 177742e9a92fSRobert Love fc_host_port_id(lp->host), FC_TYPE_ELS, 177842e9a92fSRobert Love FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0); 177942e9a92fSRobert Love 1780a0cc1eccSVasu Dev if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov)) 1781a0cc1eccSVasu Dev return; 1782a0cc1eccSVasu Dev 1783a0cc1eccSVasu Dev retry: 1784a0cc1eccSVasu Dev spin_lock_bh(&ep->ex_lock); 1785a0cc1eccSVasu Dev if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) { 1786a0cc1eccSVasu Dev spin_unlock_bh(&ep->ex_lock); 1787a0cc1eccSVasu Dev /* drop hold for rec qual */ 1788a0cc1eccSVasu Dev fc_exch_release(ep); 178942e9a92fSRobert Love return; 179042e9a92fSRobert Love } 1791a0cc1eccSVasu Dev ep->esb_stat |= ESB_ST_REC_QUAL; 1792a0cc1eccSVasu Dev fc_exch_timer_set_locked(ep, ep->r_a_tov); 1793a0cc1eccSVasu Dev spin_unlock_bh(&ep->ex_lock); 179442e9a92fSRobert Love } 179542e9a92fSRobert Love 179642e9a92fSRobert Love 179742e9a92fSRobert Love /* 179842e9a92fSRobert Love * Handle incoming ELS RRQ - Reset Recovery Qualifier. 179942e9a92fSRobert Love */ 180042e9a92fSRobert Love static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp) 180142e9a92fSRobert Love { 18023f127ad9SVasu Dev struct fc_exch *ep = NULL; /* request or subject exchange */ 180342e9a92fSRobert Love struct fc_els_rrq *rp; 180442e9a92fSRobert Love u32 sid; 180542e9a92fSRobert Love u16 xid; 180642e9a92fSRobert Love enum fc_els_rjt_explan explan; 180742e9a92fSRobert Love 180842e9a92fSRobert Love rp = fc_frame_payload_get(fp, sizeof(*rp)); 180942e9a92fSRobert Love explan = ELS_EXPL_INV_LEN; 181042e9a92fSRobert Love if (!rp) 181142e9a92fSRobert Love goto reject; 181242e9a92fSRobert Love 181342e9a92fSRobert Love /* 181442e9a92fSRobert Love * lookup subject exchange. 181542e9a92fSRobert Love */ 181642e9a92fSRobert Love ep = fc_seq_exch(sp); 181742e9a92fSRobert Love sid = ntoh24(rp->rrq_s_id); /* subject source */ 181842e9a92fSRobert Love xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id); 181942e9a92fSRobert Love ep = fc_exch_find(ep->em, xid); 182042e9a92fSRobert Love 182142e9a92fSRobert Love explan = ELS_EXPL_OXID_RXID; 182242e9a92fSRobert Love if (!ep) 182342e9a92fSRobert Love goto reject; 182442e9a92fSRobert Love spin_lock_bh(&ep->ex_lock); 182542e9a92fSRobert Love if (ep->oxid != ntohs(rp->rrq_ox_id)) 182642e9a92fSRobert Love goto unlock_reject; 182742e9a92fSRobert Love if (ep->rxid != ntohs(rp->rrq_rx_id) && 182842e9a92fSRobert Love ep->rxid != FC_XID_UNKNOWN) 182942e9a92fSRobert Love goto unlock_reject; 183042e9a92fSRobert Love explan = ELS_EXPL_SID; 183142e9a92fSRobert Love if (ep->sid != sid) 183242e9a92fSRobert Love goto unlock_reject; 183342e9a92fSRobert Love 183442e9a92fSRobert Love /* 183542e9a92fSRobert Love * Clear Recovery Qualifier state, and cancel timer if complete. 183642e9a92fSRobert Love */ 183742e9a92fSRobert Love if (ep->esb_stat & ESB_ST_REC_QUAL) { 183842e9a92fSRobert Love ep->esb_stat &= ~ESB_ST_REC_QUAL; 183942e9a92fSRobert Love atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */ 184042e9a92fSRobert Love } 184142e9a92fSRobert Love if (ep->esb_stat & ESB_ST_COMPLETE) { 184242e9a92fSRobert Love if (cancel_delayed_work(&ep->timeout_work)) 184342e9a92fSRobert Love atomic_dec(&ep->ex_refcnt); /* drop timer hold */ 184442e9a92fSRobert Love } 184542e9a92fSRobert Love 184642e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 184742e9a92fSRobert Love 184842e9a92fSRobert Love /* 184942e9a92fSRobert Love * Send LS_ACC. 185042e9a92fSRobert Love */ 185142e9a92fSRobert Love fc_seq_ls_acc(sp); 18523f127ad9SVasu Dev goto out; 185342e9a92fSRobert Love 185442e9a92fSRobert Love unlock_reject: 185542e9a92fSRobert Love spin_unlock_bh(&ep->ex_lock); 185642e9a92fSRobert Love reject: 185742e9a92fSRobert Love fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan); 18583f127ad9SVasu Dev out: 185942e9a92fSRobert Love fc_frame_free(fp); 18603f127ad9SVasu Dev if (ep) 18613f127ad9SVasu Dev fc_exch_release(ep); /* drop hold from fc_exch_find */ 186242e9a92fSRobert Love } 186342e9a92fSRobert Love 186496316099SVasu Dev struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport, 186596316099SVasu Dev struct fc_exch_mgr *mp, 186696316099SVasu Dev bool (*match)(struct fc_frame *)) 186796316099SVasu Dev { 186896316099SVasu Dev struct fc_exch_mgr_anchor *ema; 186996316099SVasu Dev 187096316099SVasu Dev ema = kmalloc(sizeof(*ema), GFP_ATOMIC); 187196316099SVasu Dev if (!ema) 187296316099SVasu Dev return ema; 187396316099SVasu Dev 187496316099SVasu Dev ema->mp = mp; 187596316099SVasu Dev ema->match = match; 187696316099SVasu Dev /* add EM anchor to EM anchors list */ 187796316099SVasu Dev list_add_tail(&ema->ema_list, &lport->ema_list); 187896316099SVasu Dev kref_get(&mp->kref); 187996316099SVasu Dev return ema; 188096316099SVasu Dev } 188196316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_add); 188296316099SVasu Dev 188396316099SVasu Dev static void fc_exch_mgr_destroy(struct kref *kref) 188496316099SVasu Dev { 188596316099SVasu Dev struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref); 188696316099SVasu Dev 188796316099SVasu Dev mempool_destroy(mp->ep_pool); 1888e4bc50beSVasu Dev free_percpu(mp->pool); 188996316099SVasu Dev kfree(mp); 189096316099SVasu Dev } 189196316099SVasu Dev 189296316099SVasu Dev void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema) 189396316099SVasu Dev { 189496316099SVasu Dev /* remove EM anchor from EM anchors list */ 189596316099SVasu Dev list_del(&ema->ema_list); 189696316099SVasu Dev kref_put(&ema->mp->kref, fc_exch_mgr_destroy); 189796316099SVasu Dev kfree(ema); 189896316099SVasu Dev } 189996316099SVasu Dev EXPORT_SYMBOL(fc_exch_mgr_del); 190096316099SVasu Dev 190142e9a92fSRobert Love struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp, 190242e9a92fSRobert Love enum fc_class class, 190352ff878cSVasu Dev u16 min_xid, u16 max_xid, 190452ff878cSVasu Dev bool (*match)(struct fc_frame *)) 190542e9a92fSRobert Love { 190642e9a92fSRobert Love struct fc_exch_mgr *mp; 1907e4bc50beSVasu Dev u16 pool_exch_range; 1908e4bc50beSVasu Dev size_t pool_size; 1909e4bc50beSVasu Dev unsigned int cpu; 1910e4bc50beSVasu Dev struct fc_exch_pool *pool; 191142e9a92fSRobert Love 1912e4bc50beSVasu Dev if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN || 1913e4bc50beSVasu Dev (min_xid & fc_cpu_mask) != 0) { 19147414705eSRobert Love FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n", 191542e9a92fSRobert Love min_xid, max_xid); 191642e9a92fSRobert Love return NULL; 191742e9a92fSRobert Love } 191842e9a92fSRobert Love 191942e9a92fSRobert Love /* 1920b2f0091fSVasu Dev * allocate memory for EM 192142e9a92fSRobert Love */ 1922b2f0091fSVasu Dev mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC); 192342e9a92fSRobert Love if (!mp) 192442e9a92fSRobert Love return NULL; 192542e9a92fSRobert Love 192642e9a92fSRobert Love mp->class = class; 192742e9a92fSRobert Love /* adjust em exch xid range for offload */ 192842e9a92fSRobert Love mp->min_xid = min_xid; 192942e9a92fSRobert Love mp->max_xid = max_xid; 193042e9a92fSRobert Love 193142e9a92fSRobert Love mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep); 193242e9a92fSRobert Love if (!mp->ep_pool) 193342e9a92fSRobert Love goto free_mp; 193442e9a92fSRobert Love 1935e4bc50beSVasu Dev /* 1936e4bc50beSVasu Dev * Setup per cpu exch pool with entire exchange id range equally 1937e4bc50beSVasu Dev * divided across all cpus. The exch pointers array memory is 1938e4bc50beSVasu Dev * allocated for exch range per pool. 1939e4bc50beSVasu Dev */ 1940e4bc50beSVasu Dev pool_exch_range = (mp->max_xid - mp->min_xid + 1) / (fc_cpu_mask + 1); 1941e4bc50beSVasu Dev mp->pool_max_index = pool_exch_range - 1; 1942e4bc50beSVasu Dev 1943e4bc50beSVasu Dev /* 1944e4bc50beSVasu Dev * Allocate and initialize per cpu exch pool 1945e4bc50beSVasu Dev */ 1946e4bc50beSVasu Dev pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *); 1947e4bc50beSVasu Dev mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool)); 1948e4bc50beSVasu Dev if (!mp->pool) 1949e4bc50beSVasu Dev goto free_mempool; 1950e4bc50beSVasu Dev for_each_possible_cpu(cpu) { 1951e4bc50beSVasu Dev pool = per_cpu_ptr(mp->pool, cpu); 1952e4bc50beSVasu Dev spin_lock_init(&pool->lock); 1953e4bc50beSVasu Dev INIT_LIST_HEAD(&pool->ex_list); 1954e4bc50beSVasu Dev } 1955e4bc50beSVasu Dev 195652ff878cSVasu Dev kref_init(&mp->kref); 195752ff878cSVasu Dev if (!fc_exch_mgr_add(lp, mp, match)) { 1958e4bc50beSVasu Dev free_percpu(mp->pool); 1959e4bc50beSVasu Dev goto free_mempool; 196052ff878cSVasu Dev } 196152ff878cSVasu Dev 196252ff878cSVasu Dev /* 196352ff878cSVasu Dev * Above kref_init() sets mp->kref to 1 and then 196452ff878cSVasu Dev * call to fc_exch_mgr_add incremented mp->kref again, 196552ff878cSVasu Dev * so adjust that extra increment. 196652ff878cSVasu Dev */ 196752ff878cSVasu Dev kref_put(&mp->kref, fc_exch_mgr_destroy); 196842e9a92fSRobert Love return mp; 196942e9a92fSRobert Love 1970e4bc50beSVasu Dev free_mempool: 1971e4bc50beSVasu Dev mempool_destroy(mp->ep_pool); 197242e9a92fSRobert Love free_mp: 197342e9a92fSRobert Love kfree(mp); 197442e9a92fSRobert Love return NULL; 197542e9a92fSRobert Love } 197642e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_alloc); 197742e9a92fSRobert Love 197852ff878cSVasu Dev void fc_exch_mgr_free(struct fc_lport *lport) 197942e9a92fSRobert Love { 198052ff878cSVasu Dev struct fc_exch_mgr_anchor *ema, *next; 198152ff878cSVasu Dev 198252ff878cSVasu Dev list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list) 198352ff878cSVasu Dev fc_exch_mgr_del(ema); 198442e9a92fSRobert Love } 198542e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_mgr_free); 198642e9a92fSRobert Love 198742e9a92fSRobert Love /* 198842e9a92fSRobert Love * Receive a frame 198942e9a92fSRobert Love */ 199052ff878cSVasu Dev void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp) 199142e9a92fSRobert Love { 199242e9a92fSRobert Love struct fc_frame_header *fh = fc_frame_header_get(fp); 199352ff878cSVasu Dev struct fc_exch_mgr_anchor *ema; 199452ff878cSVasu Dev u32 f_ctl, found = 0; 199552ff878cSVasu Dev u16 oxid; 199642e9a92fSRobert Love 199742e9a92fSRobert Love /* lport lock ? */ 199852ff878cSVasu Dev if (!lp || lp->state == LPORT_ST_DISABLED) { 19997414705eSRobert Love FC_LPORT_DBG(lp, "Receiving frames for an lport that " 20007414705eSRobert Love "has not been initialized correctly\n"); 200142e9a92fSRobert Love fc_frame_free(fp); 200242e9a92fSRobert Love return; 200342e9a92fSRobert Love } 200442e9a92fSRobert Love 200552ff878cSVasu Dev f_ctl = ntoh24(fh->fh_f_ctl); 200652ff878cSVasu Dev oxid = ntohs(fh->fh_ox_id); 200752ff878cSVasu Dev if (f_ctl & FC_FC_EX_CTX) { 200852ff878cSVasu Dev list_for_each_entry(ema, &lp->ema_list, ema_list) { 200952ff878cSVasu Dev if ((oxid >= ema->mp->min_xid) && 201052ff878cSVasu Dev (oxid <= ema->mp->max_xid)) { 201152ff878cSVasu Dev found = 1; 201252ff878cSVasu Dev break; 201352ff878cSVasu Dev } 201452ff878cSVasu Dev } 201552ff878cSVasu Dev 201652ff878cSVasu Dev if (!found) { 201752ff878cSVasu Dev FC_LPORT_DBG(lp, "Received response for out " 201852ff878cSVasu Dev "of range oxid:%hx\n", oxid); 201952ff878cSVasu Dev fc_frame_free(fp); 202052ff878cSVasu Dev return; 202152ff878cSVasu Dev } 202252ff878cSVasu Dev } else 202352ff878cSVasu Dev ema = list_entry(lp->ema_list.prev, typeof(*ema), ema_list); 202452ff878cSVasu Dev 202542e9a92fSRobert Love /* 202642e9a92fSRobert Love * If frame is marked invalid, just drop it. 202742e9a92fSRobert Love */ 202842e9a92fSRobert Love switch (fr_eof(fp)) { 202942e9a92fSRobert Love case FC_EOF_T: 203042e9a92fSRobert Love if (f_ctl & FC_FC_END_SEQ) 203142e9a92fSRobert Love skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl)); 203242e9a92fSRobert Love /* fall through */ 203342e9a92fSRobert Love case FC_EOF_N: 203442e9a92fSRobert Love if (fh->fh_type == FC_TYPE_BLS) 203552ff878cSVasu Dev fc_exch_recv_bls(ema->mp, fp); 203642e9a92fSRobert Love else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) == 203742e9a92fSRobert Love FC_FC_EX_CTX) 203852ff878cSVasu Dev fc_exch_recv_seq_resp(ema->mp, fp); 203942e9a92fSRobert Love else if (f_ctl & FC_FC_SEQ_CTX) 204052ff878cSVasu Dev fc_exch_recv_resp(ema->mp, fp); 204142e9a92fSRobert Love else 204252ff878cSVasu Dev fc_exch_recv_req(lp, ema->mp, fp); 204342e9a92fSRobert Love break; 204442e9a92fSRobert Love default: 2045d459b7eaSRobert Love FC_LPORT_DBG(lp, "dropping invalid frame (eof %x)", fr_eof(fp)); 204642e9a92fSRobert Love fc_frame_free(fp); 204742e9a92fSRobert Love } 204842e9a92fSRobert Love } 204942e9a92fSRobert Love EXPORT_SYMBOL(fc_exch_recv); 205042e9a92fSRobert Love 205142e9a92fSRobert Love int fc_exch_init(struct fc_lport *lp) 205242e9a92fSRobert Love { 205342e9a92fSRobert Love if (!lp->tt.seq_start_next) 205442e9a92fSRobert Love lp->tt.seq_start_next = fc_seq_start_next; 205542e9a92fSRobert Love 205642e9a92fSRobert Love if (!lp->tt.exch_seq_send) 205742e9a92fSRobert Love lp->tt.exch_seq_send = fc_exch_seq_send; 205842e9a92fSRobert Love 205942e9a92fSRobert Love if (!lp->tt.seq_send) 206042e9a92fSRobert Love lp->tt.seq_send = fc_seq_send; 206142e9a92fSRobert Love 206242e9a92fSRobert Love if (!lp->tt.seq_els_rsp_send) 206342e9a92fSRobert Love lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send; 206442e9a92fSRobert Love 206542e9a92fSRobert Love if (!lp->tt.exch_done) 206642e9a92fSRobert Love lp->tt.exch_done = fc_exch_done; 206742e9a92fSRobert Love 206842e9a92fSRobert Love if (!lp->tt.exch_mgr_reset) 206942e9a92fSRobert Love lp->tt.exch_mgr_reset = fc_exch_mgr_reset; 207042e9a92fSRobert Love 207142e9a92fSRobert Love if (!lp->tt.seq_exch_abort) 207242e9a92fSRobert Love lp->tt.seq_exch_abort = fc_seq_exch_abort; 207342e9a92fSRobert Love 207489f19a59SVasu Dev return 0; 207589f19a59SVasu Dev } 207689f19a59SVasu Dev EXPORT_SYMBOL(fc_exch_init); 207789f19a59SVasu Dev 207889f19a59SVasu Dev /** 207989f19a59SVasu Dev * fc_setup_exch_mgr() - Setup an exchange manager 208089f19a59SVasu Dev */ 208189f19a59SVasu Dev int fc_setup_exch_mgr() 208289f19a59SVasu Dev { 208389f19a59SVasu Dev fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch), 208489f19a59SVasu Dev 0, SLAB_HWCACHE_ALIGN, NULL); 208589f19a59SVasu Dev if (!fc_em_cachep) 208689f19a59SVasu Dev return -ENOMEM; 208789f19a59SVasu Dev 2088e4bc50beSVasu Dev /* 2089e4bc50beSVasu Dev * Initialize fc_cpu_mask and fc_cpu_order. The 2090e4bc50beSVasu Dev * fc_cpu_mask is set for nr_cpu_ids rounded up 2091e4bc50beSVasu Dev * to order of 2's * power and order is stored 2092e4bc50beSVasu Dev * in fc_cpu_order as this is later required in 2093e4bc50beSVasu Dev * mapping between an exch id and exch array index 2094e4bc50beSVasu Dev * in per cpu exch pool. 2095e4bc50beSVasu Dev * 2096e4bc50beSVasu Dev * This round up is required to align fc_cpu_mask 2097e4bc50beSVasu Dev * to exchange id's lower bits such that all incoming 2098e4bc50beSVasu Dev * frames of an exchange gets delivered to the same 2099e4bc50beSVasu Dev * cpu on which exchange originated by simple bitwise 2100e4bc50beSVasu Dev * AND operation between fc_cpu_mask and exchange id. 2101e4bc50beSVasu Dev */ 2102e4bc50beSVasu Dev fc_cpu_mask = 1; 2103e4bc50beSVasu Dev fc_cpu_order = 0; 2104e4bc50beSVasu Dev while (fc_cpu_mask < nr_cpu_ids) { 2105e4bc50beSVasu Dev fc_cpu_mask <<= 1; 2106e4bc50beSVasu Dev fc_cpu_order++; 2107e4bc50beSVasu Dev } 2108e4bc50beSVasu Dev fc_cpu_mask--; 2109e4bc50beSVasu Dev 211042e9a92fSRobert Love return 0; 211142e9a92fSRobert Love } 211242e9a92fSRobert Love 211342e9a92fSRobert Love void fc_destroy_exch_mgr(void) 211442e9a92fSRobert Love { 211542e9a92fSRobert Love kmem_cache_destroy(fc_em_cachep); 211642e9a92fSRobert Love } 2117