GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / libfc / fc_exch.c
blob1c65cbb53f7184041d4c570436f146dbb5ee4b27
1 /*
2 * Copyright(c) 2007 Intel Corporation. All rights reserved.
3 * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
4 * Copyright(c) 2008 Mike Christie
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 * Maintained at www.Open-FCoE.org
23 * Fibre Channel exchange and sequence handling.
26 #include <linux/timer.h>
27 #include <linux/slab.h>
28 #include <linux/err.h>
30 #include <scsi/fc/fc_fc2.h>
32 #include <scsi/libfc.h>
33 #include <scsi/fc_encode.h>
35 #include "fc_libfc.h"
37 u16 fc_cpu_mask; /* cpu mask for possible cpus */
38 EXPORT_SYMBOL(fc_cpu_mask);
39 static u16 fc_cpu_order; /* 2's power to represent total possible cpus */
40 static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
41 struct workqueue_struct *fc_exch_workqueue;
44 * Structure and function definitions for managing Fibre Channel Exchanges
45 * and Sequences.
47 * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
49 * fc_exch_mgr holds the exchange state for an N port
51 * fc_exch holds state for one exchange and links to its active sequence.
53 * fc_seq holds the state for an individual sequence.
56 /**
57 * struct fc_exch_pool - Per cpu exchange pool
58 * @next_index: Next possible free exchange index
59 * @total_exches: Total allocated exchanges
60 * @lock: Exch pool lock
61 * @ex_list: List of exchanges
63 * This structure manages per cpu exchanges in array of exchange pointers.
64 * This array is allocated followed by struct fc_exch_pool memory for
65 * assigned range of exchanges to per cpu pool.
67 struct fc_exch_pool {
68 u16 next_index;
69 u16 total_exches;
70 spinlock_t lock;
71 struct list_head ex_list;
74 /**
75 * struct fc_exch_mgr - The Exchange Manager (EM).
76 * @class: Default class for new sequences
77 * @kref: Reference counter
78 * @min_xid: Minimum exchange ID
79 * @max_xid: Maximum exchange ID
80 * @ep_pool: Reserved exchange pointers
81 * @pool_max_index: Max exch array index in exch pool
82 * @pool: Per cpu exch pool
83 * @stats: Statistics structure
85 * This structure is the center for creating exchanges and sequences.
86 * It manages the allocation of exchange IDs.
88 struct fc_exch_mgr {
89 enum fc_class class;
90 struct kref kref;
91 u16 min_xid;
92 u16 max_xid;
93 mempool_t *ep_pool;
94 u16 pool_max_index;
95 struct fc_exch_pool *pool;
97 struct {
98 atomic_t no_free_exch;
99 atomic_t no_free_exch_xid;
100 atomic_t xid_not_found;
101 atomic_t xid_busy;
102 atomic_t seq_not_found;
103 atomic_t non_bls_resp;
104 } stats;
106 #define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
109 * struct fc_exch_mgr_anchor - primary structure for list of EMs
110 * @ema_list: Exchange Manager Anchor list
111 * @mp: Exchange Manager associated with this anchor
112 * @match: Routine to determine if this anchor's EM should be used
114 * When walking the list of anchors the match routine will be called
115 * for each anchor to determine if that EM should be used. The last
116 * anchor in the list will always match to handle any exchanges not
117 * handled by other EMs. The non-default EMs would be added to the
118 * anchor list by HW that provides FCoE offloads.
120 struct fc_exch_mgr_anchor {
121 struct list_head ema_list;
122 struct fc_exch_mgr *mp;
123 bool (*match)(struct fc_frame *);
126 static void fc_exch_rrq(struct fc_exch *);
127 static void fc_seq_ls_acc(struct fc_frame *);
128 static void fc_seq_ls_rjt(struct fc_frame *, enum fc_els_rjt_reason,
129 enum fc_els_rjt_explan);
130 static void fc_exch_els_rec(struct fc_frame *);
131 static void fc_exch_els_rrq(struct fc_frame *);
135 * Locking notes:
137 * The EM code run in a per-CPU worker thread.
139 * To protect against concurrency between a worker thread code and timers,
140 * sequence allocation and deallocation must be locked.
141 * - exchange refcnt can be done atomicly without locks.
142 * - sequence allocation must be locked by exch lock.
143 * - If the EM pool lock and ex_lock must be taken at the same time, then the
144 * EM pool lock must be taken before the ex_lock.
148 * opcode names for debugging.
150 static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
153 * fc_exch_name_lookup() - Lookup name by opcode
154 * @op: Opcode to be looked up
155 * @table: Opcode/name table
156 * @max_index: Index not to be exceeded
158 * This routine is used to determine a human-readable string identifying
159 * a R_CTL opcode.
161 static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
162 unsigned int max_index)
164 const char *name = NULL;
166 if (op < max_index)
167 name = table[op];
168 if (!name)
169 name = "unknown";
170 return name;
174 * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
175 * @op: The opcode to be looked up
177 static const char *fc_exch_rctl_name(unsigned int op)
179 return fc_exch_name_lookup(op, fc_exch_rctl_names,
180 ARRAY_SIZE(fc_exch_rctl_names));
184 * fc_exch_hold() - Increment an exchange's reference count
185 * @ep: Echange to be held
187 static inline void fc_exch_hold(struct fc_exch *ep)
189 atomic_inc(&ep->ex_refcnt);
193 * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
194 * and determine SOF and EOF.
195 * @ep: The exchange to that will use the header
196 * @fp: The frame whose header is to be modified
197 * @f_ctl: F_CTL bits that will be used for the frame header
199 * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
200 * fh_seq_id, fh_seq_cnt and the SOF and EOF.
202 static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
203 u32 f_ctl)
205 struct fc_frame_header *fh = fc_frame_header_get(fp);
206 u16 fill;
208 fr_sof(fp) = ep->class;
209 if (ep->seq.cnt)
210 fr_sof(fp) = fc_sof_normal(ep->class);
212 if (f_ctl & FC_FC_END_SEQ) {
213 fr_eof(fp) = FC_EOF_T;
214 if (fc_sof_needs_ack(ep->class))
215 fr_eof(fp) = FC_EOF_N;
217 * From F_CTL.
218 * The number of fill bytes to make the length a 4-byte
219 * multiple is the low order 2-bits of the f_ctl.
220 * The fill itself will have been cleared by the frame
221 * allocation.
222 * After this, the length will be even, as expected by
223 * the transport.
225 fill = fr_len(fp) & 3;
226 if (fill) {
227 fill = 4 - fill;
228 /* TODO, this may be a problem with fragmented skb */
229 skb_put(fp_skb(fp), fill);
230 hton24(fh->fh_f_ctl, f_ctl | fill);
232 } else {
233 WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
234 fr_eof(fp) = FC_EOF_N;
238 * Initialize remainig fh fields
239 * from fc_fill_fc_hdr
241 fh->fh_ox_id = htons(ep->oxid);
242 fh->fh_rx_id = htons(ep->rxid);
243 fh->fh_seq_id = ep->seq.id;
244 fh->fh_seq_cnt = htons(ep->seq.cnt);
248 * fc_exch_release() - Decrement an exchange's reference count
249 * @ep: Exchange to be released
251 * If the reference count reaches zero and the exchange is complete,
252 * it is freed.
254 static void fc_exch_release(struct fc_exch *ep)
256 struct fc_exch_mgr *mp;
258 if (atomic_dec_and_test(&ep->ex_refcnt)) {
259 mp = ep->em;
260 if (ep->destructor)
261 ep->destructor(&ep->seq, ep->arg);
262 WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
263 mempool_free(ep, mp->ep_pool);
268 * fc_exch_done_locked() - Complete an exchange with the exchange lock held
269 * @ep: The exchange that is complete
271 static int fc_exch_done_locked(struct fc_exch *ep)
273 int rc = 1;
276 * We must check for completion in case there are two threads
277 * tyring to complete this. But the rrq code will reuse the
278 * ep, and in that case we only clear the resp and set it as
279 * complete, so it can be reused by the timer to send the rrq.
281 ep->resp = NULL;
282 if (ep->state & FC_EX_DONE)
283 return rc;
284 ep->esb_stat |= ESB_ST_COMPLETE;
286 if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
287 ep->state |= FC_EX_DONE;
288 if (cancel_delayed_work(&ep->timeout_work))
289 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
290 rc = 0;
292 return rc;
296 * fc_exch_ptr_get() - Return an exchange from an exchange pool
297 * @pool: Exchange Pool to get an exchange from
298 * @index: Index of the exchange within the pool
300 * Use the index to get an exchange from within an exchange pool. exches
301 * will point to an array of exchange pointers. The index will select
302 * the exchange within the array.
304 static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
305 u16 index)
307 struct fc_exch **exches = (struct fc_exch **)(pool + 1);
308 return exches[index];
312 * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
313 * @pool: The pool to assign the exchange to
314 * @index: The index in the pool where the exchange will be assigned
315 * @ep: The exchange to assign to the pool
317 static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
318 struct fc_exch *ep)
320 ((struct fc_exch **)(pool + 1))[index] = ep;
324 * fc_exch_delete() - Delete an exchange
325 * @ep: The exchange to be deleted
327 static void fc_exch_delete(struct fc_exch *ep)
329 struct fc_exch_pool *pool;
331 pool = ep->pool;
332 spin_lock_bh(&pool->lock);
333 WARN_ON(pool->total_exches <= 0);
334 pool->total_exches--;
335 fc_exch_ptr_set(pool, (ep->xid - ep->em->min_xid) >> fc_cpu_order,
336 NULL);
337 list_del(&ep->ex_list);
338 spin_unlock_bh(&pool->lock);
339 fc_exch_release(ep); /* drop hold for exch in mp */
343 * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
344 * the exchange lock held
345 * @ep: The exchange whose timer will start
346 * @timer_msec: The timeout period
348 * Used for upper level protocols to time out the exchange.
349 * The timer is cancelled when it fires or when the exchange completes.
351 static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
352 unsigned int timer_msec)
354 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
355 return;
357 FC_EXCH_DBG(ep, "Exchange timer armed\n");
359 if (queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
360 msecs_to_jiffies(timer_msec)))
361 fc_exch_hold(ep); /* hold for timer */
365 * fc_exch_timer_set() - Lock the exchange and set the timer
366 * @ep: The exchange whose timer will start
367 * @timer_msec: The timeout period
369 static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
371 spin_lock_bh(&ep->ex_lock);
372 fc_exch_timer_set_locked(ep, timer_msec);
373 spin_unlock_bh(&ep->ex_lock);
377 * fc_seq_send() - Send a frame using existing sequence/exchange pair
378 * @lport: The local port that the exchange will be sent on
379 * @sp: The sequence to be sent
380 * @fp: The frame to be sent on the exchange
382 static int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp,
383 struct fc_frame *fp)
385 struct fc_exch *ep;
386 struct fc_frame_header *fh = fc_frame_header_get(fp);
387 int error;
388 u32 f_ctl;
390 ep = fc_seq_exch(sp);
391 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
393 f_ctl = ntoh24(fh->fh_f_ctl);
394 fc_exch_setup_hdr(ep, fp, f_ctl);
395 fr_encaps(fp) = ep->encaps;
398 * update sequence count if this frame is carrying
399 * multiple FC frames when sequence offload is enabled
400 * by LLD.
402 if (fr_max_payload(fp))
403 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
404 fr_max_payload(fp));
405 else
406 sp->cnt++;
409 * Send the frame.
411 error = lport->tt.frame_send(lport, fp);
414 * Update the exchange and sequence flags,
415 * assuming all frames for the sequence have been sent.
416 * We can only be called to send once for each sequence.
418 spin_lock_bh(&ep->ex_lock);
419 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
420 if (f_ctl & FC_FC_SEQ_INIT)
421 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
422 spin_unlock_bh(&ep->ex_lock);
423 return error;
427 * fc_seq_alloc() - Allocate a sequence for a given exchange
428 * @ep: The exchange to allocate a new sequence for
429 * @seq_id: The sequence ID to be used
431 * We don't support multiple originated sequences on the same exchange.
432 * By implication, any previously originated sequence on this exchange
433 * is complete, and we reallocate the same sequence.
435 static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
437 struct fc_seq *sp;
439 sp = &ep->seq;
440 sp->ssb_stat = 0;
441 sp->cnt = 0;
442 sp->id = seq_id;
443 return sp;
447 * fc_seq_start_next_locked() - Allocate a new sequence on the same
448 * exchange as the supplied sequence
449 * @sp: The sequence/exchange to get a new sequence for
451 static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
453 struct fc_exch *ep = fc_seq_exch(sp);
455 sp = fc_seq_alloc(ep, ep->seq_id++);
456 FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
457 ep->f_ctl, sp->id);
458 return sp;
462 * fc_seq_start_next() - Lock the exchange and get a new sequence
463 * for a given sequence/exchange pair
464 * @sp: The sequence/exchange to get a new exchange for
466 static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
468 struct fc_exch *ep = fc_seq_exch(sp);
470 spin_lock_bh(&ep->ex_lock);
471 sp = fc_seq_start_next_locked(sp);
472 spin_unlock_bh(&ep->ex_lock);
474 return sp;
478 * fc_seq_exch_abort() - Abort an exchange and sequence
479 * @req_sp: The sequence to be aborted
480 * @timer_msec: The period of time to wait before aborting
482 * Generally called because of a timeout or an abort from the upper layer.
484 static int fc_seq_exch_abort(const struct fc_seq *req_sp,
485 unsigned int timer_msec)
487 struct fc_seq *sp;
488 struct fc_exch *ep;
489 struct fc_frame *fp;
490 int error;
492 ep = fc_seq_exch(req_sp);
494 spin_lock_bh(&ep->ex_lock);
495 if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
496 ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
497 spin_unlock_bh(&ep->ex_lock);
498 return -ENXIO;
502 * Send the abort on a new sequence if possible.
504 sp = fc_seq_start_next_locked(&ep->seq);
505 if (!sp) {
506 spin_unlock_bh(&ep->ex_lock);
507 return -ENOMEM;
510 ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
511 if (timer_msec)
512 fc_exch_timer_set_locked(ep, timer_msec);
513 spin_unlock_bh(&ep->ex_lock);
516 * If not logged into the fabric, don't send ABTS but leave
517 * sequence active until next timeout.
519 if (!ep->sid)
520 return 0;
523 * Send an abort for the sequence that timed out.
525 fp = fc_frame_alloc(ep->lp, 0);
526 if (fp) {
527 fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
528 FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
529 error = fc_seq_send(ep->lp, sp, fp);
530 } else
531 error = -ENOBUFS;
532 return error;
536 * fc_exch_timeout() - Handle exchange timer expiration
537 * @work: The work_struct identifying the exchange that timed out
539 static void fc_exch_timeout(struct work_struct *work)
541 struct fc_exch *ep = container_of(work, struct fc_exch,
542 timeout_work.work);
543 struct fc_seq *sp = &ep->seq;
544 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
545 void *arg;
546 u32 e_stat;
547 int rc = 1;
549 FC_EXCH_DBG(ep, "Exchange timed out\n");
551 spin_lock_bh(&ep->ex_lock);
552 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
553 goto unlock;
555 e_stat = ep->esb_stat;
556 if (e_stat & ESB_ST_COMPLETE) {
557 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
558 spin_unlock_bh(&ep->ex_lock);
559 if (e_stat & ESB_ST_REC_QUAL)
560 fc_exch_rrq(ep);
561 goto done;
562 } else {
563 resp = ep->resp;
564 arg = ep->arg;
565 ep->resp = NULL;
566 if (e_stat & ESB_ST_ABNORMAL)
567 rc = fc_exch_done_locked(ep);
568 spin_unlock_bh(&ep->ex_lock);
569 if (!rc)
570 fc_exch_delete(ep);
571 if (resp)
572 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
573 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
574 goto done;
576 unlock:
577 spin_unlock_bh(&ep->ex_lock);
578 done:
580 * This release matches the hold taken when the timer was set.
582 fc_exch_release(ep);
586 * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
587 * @lport: The local port that the exchange is for
588 * @mp: The exchange manager that will allocate the exchange
590 * Returns pointer to allocated fc_exch with exch lock held.
592 static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
593 struct fc_exch_mgr *mp)
595 struct fc_exch *ep;
596 unsigned int cpu;
597 u16 index;
598 struct fc_exch_pool *pool;
600 /* allocate memory for exchange */
601 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
602 if (!ep) {
603 atomic_inc(&mp->stats.no_free_exch);
604 goto out;
606 memset(ep, 0, sizeof(*ep));
608 cpu = get_cpu();
609 pool = per_cpu_ptr(mp->pool, cpu);
610 spin_lock_bh(&pool->lock);
611 put_cpu();
612 index = pool->next_index;
613 /* allocate new exch from pool */
614 while (fc_exch_ptr_get(pool, index)) {
615 index = index == mp->pool_max_index ? 0 : index + 1;
616 if (index == pool->next_index)
617 goto err;
619 pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
621 fc_exch_hold(ep); /* hold for exch in mp */
622 spin_lock_init(&ep->ex_lock);
624 * Hold exch lock for caller to prevent fc_exch_reset()
625 * from releasing exch while fc_exch_alloc() caller is
626 * still working on exch.
628 spin_lock_bh(&ep->ex_lock);
630 fc_exch_ptr_set(pool, index, ep);
631 list_add_tail(&ep->ex_list, &pool->ex_list);
632 fc_seq_alloc(ep, ep->seq_id++);
633 pool->total_exches++;
634 spin_unlock_bh(&pool->lock);
637 * update exchange
639 ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
640 ep->em = mp;
641 ep->pool = pool;
642 ep->lp = lport;
643 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
644 ep->rxid = FC_XID_UNKNOWN;
645 ep->class = mp->class;
646 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
647 out:
648 return ep;
649 err:
650 spin_unlock_bh(&pool->lock);
651 atomic_inc(&mp->stats.no_free_exch_xid);
652 mempool_free(ep, mp->ep_pool);
653 return NULL;
657 * fc_exch_alloc() - Allocate an exchange from an EM on a
658 * local port's list of EMs.
659 * @lport: The local port that will own the exchange
660 * @fp: The FC frame that the exchange will be for
662 * This function walks the list of exchange manager(EM)
663 * anchors to select an EM for a new exchange allocation. The
664 * EM is selected when a NULL match function pointer is encountered
665 * or when a call to a match function returns true.
667 static inline struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
668 struct fc_frame *fp)
670 struct fc_exch_mgr_anchor *ema;
672 list_for_each_entry(ema, &lport->ema_list, ema_list)
673 if (!ema->match || ema->match(fp))
674 return fc_exch_em_alloc(lport, ema->mp);
675 return NULL;
679 * fc_exch_find() - Lookup and hold an exchange
680 * @mp: The exchange manager to lookup the exchange from
681 * @xid: The XID of the exchange to look up
683 static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
685 struct fc_exch_pool *pool;
686 struct fc_exch *ep = NULL;
688 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
689 pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
690 spin_lock_bh(&pool->lock);
691 ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
692 if (ep) {
693 fc_exch_hold(ep);
694 WARN_ON(ep->xid != xid);
696 spin_unlock_bh(&pool->lock);
698 return ep;
703 * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
704 * the memory allocated for the related objects may be freed.
705 * @sp: The sequence that has completed
707 static void fc_exch_done(struct fc_seq *sp)
709 struct fc_exch *ep = fc_seq_exch(sp);
710 int rc;
712 spin_lock_bh(&ep->ex_lock);
713 rc = fc_exch_done_locked(ep);
714 spin_unlock_bh(&ep->ex_lock);
715 if (!rc)
716 fc_exch_delete(ep);
720 * fc_exch_resp() - Allocate a new exchange for a response frame
721 * @lport: The local port that the exchange was for
722 * @mp: The exchange manager to allocate the exchange from
723 * @fp: The response frame
725 * Sets the responder ID in the frame header.
727 static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
728 struct fc_exch_mgr *mp,
729 struct fc_frame *fp)
731 struct fc_exch *ep;
732 struct fc_frame_header *fh;
734 ep = fc_exch_alloc(lport, fp);
735 if (ep) {
736 ep->class = fc_frame_class(fp);
739 * Set EX_CTX indicating we're responding on this exchange.
741 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
742 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
743 fh = fc_frame_header_get(fp);
744 ep->sid = ntoh24(fh->fh_d_id);
745 ep->did = ntoh24(fh->fh_s_id);
746 ep->oid = ep->did;
749 * Allocated exchange has placed the XID in the
750 * originator field. Move it to the responder field,
751 * and set the originator XID from the frame.
753 ep->rxid = ep->xid;
754 ep->oxid = ntohs(fh->fh_ox_id);
755 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
756 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
757 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
759 fc_exch_hold(ep); /* hold for caller */
760 spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */
762 return ep;
766 * fc_seq_lookup_recip() - Find a sequence where the other end
767 * originated the sequence
768 * @lport: The local port that the frame was sent to
769 * @mp: The Exchange Manager to lookup the exchange from
770 * @fp: The frame associated with the sequence we're looking for
772 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
773 * on the ep that should be released by the caller.
775 static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
776 struct fc_exch_mgr *mp,
777 struct fc_frame *fp)
779 struct fc_frame_header *fh = fc_frame_header_get(fp);
780 struct fc_exch *ep = NULL;
781 struct fc_seq *sp = NULL;
782 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
783 u32 f_ctl;
784 u16 xid;
786 f_ctl = ntoh24(fh->fh_f_ctl);
787 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
790 * Lookup or create the exchange if we will be creating the sequence.
792 if (f_ctl & FC_FC_EX_CTX) {
793 xid = ntohs(fh->fh_ox_id); /* we originated exch */
794 ep = fc_exch_find(mp, xid);
795 if (!ep) {
796 atomic_inc(&mp->stats.xid_not_found);
797 reject = FC_RJT_OX_ID;
798 goto out;
800 if (ep->rxid == FC_XID_UNKNOWN)
801 ep->rxid = ntohs(fh->fh_rx_id);
802 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
803 reject = FC_RJT_OX_ID;
804 goto rel;
806 } else {
807 xid = ntohs(fh->fh_rx_id); /* we are the responder */
809 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
810 fc_frame_payload_op(fp) == ELS_TEST) {
811 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
812 xid = FC_XID_UNKNOWN;
816 * new sequence - find the exchange
818 ep = fc_exch_find(mp, xid);
819 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
820 if (ep) {
821 atomic_inc(&mp->stats.xid_busy);
822 reject = FC_RJT_RX_ID;
823 goto rel;
825 ep = fc_exch_resp(lport, mp, fp);
826 if (!ep) {
827 reject = FC_RJT_EXCH_EST;
828 goto out;
830 xid = ep->xid; /* get our XID */
831 } else if (!ep) {
832 atomic_inc(&mp->stats.xid_not_found);
833 reject = FC_RJT_RX_ID; /* XID not found */
834 goto out;
839 * At this point, we have the exchange held.
840 * Find or create the sequence.
842 if (fc_sof_is_init(fr_sof(fp))) {
843 sp = &ep->seq;
844 sp->ssb_stat |= SSB_ST_RESP;
845 sp->id = fh->fh_seq_id;
846 } else {
847 sp = &ep->seq;
848 if (sp->id != fh->fh_seq_id) {
849 atomic_inc(&mp->stats.seq_not_found);
850 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
851 goto rel;
854 WARN_ON(ep != fc_seq_exch(sp));
856 if (f_ctl & FC_FC_SEQ_INIT)
857 ep->esb_stat |= ESB_ST_SEQ_INIT;
859 fr_seq(fp) = sp;
860 out:
861 return reject;
862 rel:
863 fc_exch_done(&ep->seq);
864 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
865 return reject;
869 * fc_seq_lookup_orig() - Find a sequence where this end
870 * originated the sequence
871 * @mp: The Exchange Manager to lookup the exchange from
872 * @fp: The frame associated with the sequence we're looking for
874 * Does not hold the sequence for the caller.
876 static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
877 struct fc_frame *fp)
879 struct fc_frame_header *fh = fc_frame_header_get(fp);
880 struct fc_exch *ep;
881 struct fc_seq *sp = NULL;
882 u32 f_ctl;
883 u16 xid;
885 f_ctl = ntoh24(fh->fh_f_ctl);
886 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
887 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
888 ep = fc_exch_find(mp, xid);
889 if (!ep)
890 return NULL;
891 if (ep->seq.id == fh->fh_seq_id) {
893 * Save the RX_ID if we didn't previously know it.
895 sp = &ep->seq;
896 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
897 ep->rxid == FC_XID_UNKNOWN) {
898 ep->rxid = ntohs(fh->fh_rx_id);
901 fc_exch_release(ep);
902 return sp;
906 * fc_exch_set_addr() - Set the source and destination IDs for an exchange
907 * @ep: The exchange to set the addresses for
908 * @orig_id: The originator's ID
909 * @resp_id: The responder's ID
911 * Note this must be done before the first sequence of the exchange is sent.
913 static void fc_exch_set_addr(struct fc_exch *ep,
914 u32 orig_id, u32 resp_id)
916 ep->oid = orig_id;
917 if (ep->esb_stat & ESB_ST_RESP) {
918 ep->sid = resp_id;
919 ep->did = orig_id;
920 } else {
921 ep->sid = orig_id;
922 ep->did = resp_id;
927 * fc_seq_els_rsp_send() - Send an ELS response using infomation from
928 * the existing sequence/exchange.
929 * @fp: The received frame
930 * @els_cmd: The ELS command to be sent
931 * @els_data: The ELS data to be sent
933 * The received frame is not freed.
935 static void fc_seq_els_rsp_send(struct fc_frame *fp, enum fc_els_cmd els_cmd,
936 struct fc_seq_els_data *els_data)
938 switch (els_cmd) {
939 case ELS_LS_RJT:
940 fc_seq_ls_rjt(fp, els_data->reason, els_data->explan);
941 break;
942 case ELS_LS_ACC:
943 fc_seq_ls_acc(fp);
944 break;
945 case ELS_RRQ:
946 fc_exch_els_rrq(fp);
947 break;
948 case ELS_REC:
949 fc_exch_els_rec(fp);
950 break;
951 default:
952 FC_LPORT_DBG(fr_dev(fp), "Invalid ELS CMD:%x\n", els_cmd);
957 * fc_seq_send_last() - Send a sequence that is the last in the exchange
958 * @sp: The sequence that is to be sent
959 * @fp: The frame that will be sent on the sequence
960 * @rctl: The R_CTL information to be sent
961 * @fh_type: The frame header type
963 static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
964 enum fc_rctl rctl, enum fc_fh_type fh_type)
966 u32 f_ctl;
967 struct fc_exch *ep = fc_seq_exch(sp);
969 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
970 f_ctl |= ep->f_ctl;
971 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
972 fc_seq_send(ep->lp, sp, fp);
976 * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
977 * @sp: The sequence to send the ACK on
978 * @rx_fp: The received frame that is being acknoledged
980 * Send ACK_1 (or equiv.) indicating we received something.
982 static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
984 struct fc_frame *fp;
985 struct fc_frame_header *rx_fh;
986 struct fc_frame_header *fh;
987 struct fc_exch *ep = fc_seq_exch(sp);
988 struct fc_lport *lport = ep->lp;
989 unsigned int f_ctl;
992 * Don't send ACKs for class 3.
994 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
995 fp = fc_frame_alloc(lport, 0);
996 if (!fp)
997 return;
999 fh = fc_frame_header_get(fp);
1000 fh->fh_r_ctl = FC_RCTL_ACK_1;
1001 fh->fh_type = FC_TYPE_BLS;
1004 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
1005 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
1006 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1007 * Last ACK uses bits 7-6 (continue sequence),
1008 * bits 5-4 are meaningful (what kind of ACK to use).
1010 rx_fh = fc_frame_header_get(rx_fp);
1011 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1012 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1013 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
1014 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
1015 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1016 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1017 hton24(fh->fh_f_ctl, f_ctl);
1019 fc_exch_setup_hdr(ep, fp, f_ctl);
1020 fh->fh_seq_id = rx_fh->fh_seq_id;
1021 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1022 fh->fh_parm_offset = htonl(1); /* ack single frame */
1024 fr_sof(fp) = fr_sof(rx_fp);
1025 if (f_ctl & FC_FC_END_SEQ)
1026 fr_eof(fp) = FC_EOF_T;
1027 else
1028 fr_eof(fp) = FC_EOF_N;
1030 lport->tt.frame_send(lport, fp);
1035 * fc_exch_send_ba_rjt() - Send BLS Reject
1036 * @rx_fp: The frame being rejected
1037 * @reason: The reason the frame is being rejected
1038 * @explan: The explaination for the rejection
1040 * This is for rejecting BA_ABTS only.
1042 static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1043 enum fc_ba_rjt_reason reason,
1044 enum fc_ba_rjt_explan explan)
1046 struct fc_frame *fp;
1047 struct fc_frame_header *rx_fh;
1048 struct fc_frame_header *fh;
1049 struct fc_ba_rjt *rp;
1050 struct fc_lport *lport;
1051 unsigned int f_ctl;
1053 lport = fr_dev(rx_fp);
1054 fp = fc_frame_alloc(lport, sizeof(*rp));
1055 if (!fp)
1056 return;
1057 fh = fc_frame_header_get(fp);
1058 rx_fh = fc_frame_header_get(rx_fp);
1060 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
1062 rp = fc_frame_payload_get(fp, sizeof(*rp));
1063 rp->br_reason = reason;
1064 rp->br_explan = explan;
1067 * seq_id, cs_ctl, df_ctl and param/offset are zero.
1069 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
1070 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
1071 fh->fh_ox_id = rx_fh->fh_ox_id;
1072 fh->fh_rx_id = rx_fh->fh_rx_id;
1073 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1074 fh->fh_r_ctl = FC_RCTL_BA_RJT;
1075 fh->fh_type = FC_TYPE_BLS;
1078 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
1079 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
1080 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1081 * Last ACK uses bits 7-6 (continue sequence),
1082 * bits 5-4 are meaningful (what kind of ACK to use).
1083 * Always set LAST_SEQ, END_SEQ.
1085 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1086 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1087 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1088 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1089 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1090 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1091 f_ctl &= ~FC_FC_FIRST_SEQ;
1092 hton24(fh->fh_f_ctl, f_ctl);
1094 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1095 fr_eof(fp) = FC_EOF_T;
1096 if (fc_sof_needs_ack(fr_sof(fp)))
1097 fr_eof(fp) = FC_EOF_N;
1099 lport->tt.frame_send(lport, fp);
1103 * fc_exch_recv_abts() - Handle an incoming ABTS
1104 * @ep: The exchange the abort was on
1105 * @rx_fp: The ABTS frame
1107 * This would be for target mode usually, but could be due to lost
1108 * FCP transfer ready, confirm or RRQ. We always handle this as an
1109 * exchange abort, ignoring the parameter.
1111 static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1113 struct fc_frame *fp;
1114 struct fc_ba_acc *ap;
1115 struct fc_frame_header *fh;
1116 struct fc_seq *sp;
1118 if (!ep)
1119 goto reject;
1120 spin_lock_bh(&ep->ex_lock);
1121 if (ep->esb_stat & ESB_ST_COMPLETE) {
1122 spin_unlock_bh(&ep->ex_lock);
1123 goto reject;
1125 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1126 fc_exch_hold(ep); /* hold for REC_QUAL */
1127 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1128 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1130 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1131 if (!fp) {
1132 spin_unlock_bh(&ep->ex_lock);
1133 goto free;
1135 fh = fc_frame_header_get(fp);
1136 ap = fc_frame_payload_get(fp, sizeof(*ap));
1137 memset(ap, 0, sizeof(*ap));
1138 sp = &ep->seq;
1139 ap->ba_high_seq_cnt = htons(0xffff);
1140 if (sp->ssb_stat & SSB_ST_RESP) {
1141 ap->ba_seq_id = sp->id;
1142 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1143 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1144 ap->ba_low_seq_cnt = htons(sp->cnt);
1146 sp = fc_seq_start_next_locked(sp);
1147 spin_unlock_bh(&ep->ex_lock);
1148 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1149 fc_frame_free(rx_fp);
1150 return;
1152 reject:
1153 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1154 free:
1155 fc_frame_free(rx_fp);
1159 * fc_seq_assign() - Assign exchange and sequence for incoming request
1160 * @lport: The local port that received the request
1161 * @fp: The request frame
1163 * On success, the sequence pointer will be returned and also in fr_seq(@fp).
1165 static struct fc_seq *fc_seq_assign(struct fc_lport *lport, struct fc_frame *fp)
1167 struct fc_exch_mgr_anchor *ema;
1169 WARN_ON(lport != fr_dev(fp));
1170 WARN_ON(fr_seq(fp));
1171 fr_seq(fp) = NULL;
1173 list_for_each_entry(ema, &lport->ema_list, ema_list)
1174 if ((!ema->match || ema->match(fp)) &&
1175 fc_seq_lookup_recip(lport, ema->mp, fp) != FC_RJT_NONE)
1176 break;
1177 return fr_seq(fp);
1181 * fc_exch_recv_req() - Handler for an incoming request
1182 * @lport: The local port that received the request
1183 * @mp: The EM that the exchange is on
1184 * @fp: The request frame
1186 * This is used when the other end is originating the exchange
1187 * and the sequence.
1189 static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
1190 struct fc_frame *fp)
1192 struct fc_frame_header *fh = fc_frame_header_get(fp);
1193 struct fc_seq *sp = NULL;
1194 struct fc_exch *ep = NULL;
1195 enum fc_pf_rjt_reason reject;
1197 /* We can have the wrong fc_lport at this point with NPIV, which is a
1198 * problem now that we know a new exchange needs to be allocated
1200 lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
1201 if (!lport) {
1202 fc_frame_free(fp);
1203 return;
1205 fr_dev(fp) = lport;
1207 BUG_ON(fr_seq(fp));
1210 * If the RX_ID is 0xffff, don't allocate an exchange.
1211 * The upper-level protocol may request one later, if needed.
1213 if (fh->fh_rx_id == htons(FC_XID_UNKNOWN))
1214 return lport->tt.lport_recv(lport, fp);
1216 reject = fc_seq_lookup_recip(lport, mp, fp);
1217 if (reject == FC_RJT_NONE) {
1218 sp = fr_seq(fp); /* sequence will be held */
1219 ep = fc_seq_exch(sp);
1220 fc_seq_send_ack(sp, fp);
1221 ep->encaps = fr_encaps(fp);
1224 * Call the receive function.
1226 * The receive function may allocate a new sequence
1227 * over the old one, so we shouldn't change the
1228 * sequence after this.
1230 * The frame will be freed by the receive function.
1231 * If new exch resp handler is valid then call that
1232 * first.
1234 if (ep->resp)
1235 ep->resp(sp, fp, ep->arg);
1236 else
1237 lport->tt.lport_recv(lport, fp);
1238 fc_exch_release(ep); /* release from lookup */
1239 } else {
1240 FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
1241 reject);
1242 fc_frame_free(fp);
1247 * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
1248 * end is the originator of the sequence that is a
1249 * response to our initial exchange
1250 * @mp: The EM that the exchange is on
1251 * @fp: The response frame
1253 static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1255 struct fc_frame_header *fh = fc_frame_header_get(fp);
1256 struct fc_seq *sp;
1257 struct fc_exch *ep;
1258 enum fc_sof sof;
1259 u32 f_ctl;
1260 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1261 void *ex_resp_arg;
1262 int rc;
1264 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1265 if (!ep) {
1266 atomic_inc(&mp->stats.xid_not_found);
1267 goto out;
1269 if (ep->esb_stat & ESB_ST_COMPLETE) {
1270 atomic_inc(&mp->stats.xid_not_found);
1271 goto out;
1273 if (ep->rxid == FC_XID_UNKNOWN)
1274 ep->rxid = ntohs(fh->fh_rx_id);
1275 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1276 atomic_inc(&mp->stats.xid_not_found);
1277 goto rel;
1279 if (ep->did != ntoh24(fh->fh_s_id) &&
1280 ep->did != FC_FID_FLOGI) {
1281 atomic_inc(&mp->stats.xid_not_found);
1282 goto rel;
1284 sof = fr_sof(fp);
1285 sp = &ep->seq;
1286 if (fc_sof_is_init(sof)) {
1287 sp->ssb_stat |= SSB_ST_RESP;
1288 sp->id = fh->fh_seq_id;
1289 } else if (sp->id != fh->fh_seq_id) {
1290 atomic_inc(&mp->stats.seq_not_found);
1291 goto rel;
1294 f_ctl = ntoh24(fh->fh_f_ctl);
1295 fr_seq(fp) = sp;
1296 if (f_ctl & FC_FC_SEQ_INIT)
1297 ep->esb_stat |= ESB_ST_SEQ_INIT;
1299 if (fc_sof_needs_ack(sof))
1300 fc_seq_send_ack(sp, fp);
1301 resp = ep->resp;
1302 ex_resp_arg = ep->arg;
1304 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1305 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1306 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1307 spin_lock_bh(&ep->ex_lock);
1308 rc = fc_exch_done_locked(ep);
1309 WARN_ON(fc_seq_exch(sp) != ep);
1310 spin_unlock_bh(&ep->ex_lock);
1311 if (!rc)
1312 fc_exch_delete(ep);
1316 * Call the receive function.
1317 * The sequence is held (has a refcnt) for us,
1318 * but not for the receive function.
1320 * The receive function may allocate a new sequence
1321 * over the old one, so we shouldn't change the
1322 * sequence after this.
1324 * The frame will be freed by the receive function.
1325 * If new exch resp handler is valid then call that
1326 * first.
1328 if (resp)
1329 resp(sp, fp, ex_resp_arg);
1330 else
1331 fc_frame_free(fp);
1332 fc_exch_release(ep);
1333 return;
1334 rel:
1335 fc_exch_release(ep);
1336 out:
1337 fc_frame_free(fp);
1341 * fc_exch_recv_resp() - Handler for a sequence where other end is
1342 * responding to our sequence
1343 * @mp: The EM that the exchange is on
1344 * @fp: The response frame
1346 static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1348 struct fc_seq *sp;
1350 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
1352 if (!sp)
1353 atomic_inc(&mp->stats.xid_not_found);
1354 else
1355 atomic_inc(&mp->stats.non_bls_resp);
1357 fc_frame_free(fp);
1361 * fc_exch_abts_resp() - Handler for a response to an ABT
1362 * @ep: The exchange that the frame is on
1363 * @fp: The response frame
1365 * This response would be to an ABTS cancelling an exchange or sequence.
1366 * The response can be either BA_ACC or BA_RJT
1368 static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1370 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1371 void *ex_resp_arg;
1372 struct fc_frame_header *fh;
1373 struct fc_ba_acc *ap;
1374 struct fc_seq *sp;
1375 u16 low;
1376 u16 high;
1377 int rc = 1, has_rec = 0;
1379 fh = fc_frame_header_get(fp);
1380 FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
1381 fc_exch_rctl_name(fh->fh_r_ctl));
1383 if (cancel_delayed_work_sync(&ep->timeout_work))
1384 fc_exch_release(ep); /* release from pending timer hold */
1386 spin_lock_bh(&ep->ex_lock);
1387 switch (fh->fh_r_ctl) {
1388 case FC_RCTL_BA_ACC:
1389 ap = fc_frame_payload_get(fp, sizeof(*ap));
1390 if (!ap)
1391 break;
1394 * Decide whether to establish a Recovery Qualifier.
1395 * We do this if there is a non-empty SEQ_CNT range and
1396 * SEQ_ID is the same as the one we aborted.
1398 low = ntohs(ap->ba_low_seq_cnt);
1399 high = ntohs(ap->ba_high_seq_cnt);
1400 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1401 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1402 ap->ba_seq_id == ep->seq_id) && low != high) {
1403 ep->esb_stat |= ESB_ST_REC_QUAL;
1404 fc_exch_hold(ep); /* hold for recovery qualifier */
1405 has_rec = 1;
1407 break;
1408 case FC_RCTL_BA_RJT:
1409 break;
1410 default:
1411 break;
1414 resp = ep->resp;
1415 ex_resp_arg = ep->arg;
1417 /* do we need to do some other checks here. Can we reuse more of
1418 * fc_exch_recv_seq_resp
1420 sp = &ep->seq;
1422 * do we want to check END_SEQ as well as LAST_SEQ here?
1424 if (ep->fh_type != FC_TYPE_FCP &&
1425 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1426 rc = fc_exch_done_locked(ep);
1427 spin_unlock_bh(&ep->ex_lock);
1428 if (!rc)
1429 fc_exch_delete(ep);
1431 if (resp)
1432 resp(sp, fp, ex_resp_arg);
1433 else
1434 fc_frame_free(fp);
1436 if (has_rec)
1437 fc_exch_timer_set(ep, ep->r_a_tov);
1442 * fc_exch_recv_bls() - Handler for a BLS sequence
1443 * @mp: The EM that the exchange is on
1444 * @fp: The request frame
1446 * The BLS frame is always a sequence initiated by the remote side.
1447 * We may be either the originator or recipient of the exchange.
1449 static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1451 struct fc_frame_header *fh;
1452 struct fc_exch *ep;
1453 u32 f_ctl;
1455 fh = fc_frame_header_get(fp);
1456 f_ctl = ntoh24(fh->fh_f_ctl);
1457 fr_seq(fp) = NULL;
1459 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1460 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1461 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1462 spin_lock_bh(&ep->ex_lock);
1463 ep->esb_stat |= ESB_ST_SEQ_INIT;
1464 spin_unlock_bh(&ep->ex_lock);
1466 if (f_ctl & FC_FC_SEQ_CTX) {
1468 * A response to a sequence we initiated.
1469 * This should only be ACKs for class 2 or F.
1471 switch (fh->fh_r_ctl) {
1472 case FC_RCTL_ACK_1:
1473 case FC_RCTL_ACK_0:
1474 break;
1475 default:
1476 FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
1477 fh->fh_r_ctl,
1478 fc_exch_rctl_name(fh->fh_r_ctl));
1479 break;
1481 fc_frame_free(fp);
1482 } else {
1483 switch (fh->fh_r_ctl) {
1484 case FC_RCTL_BA_RJT:
1485 case FC_RCTL_BA_ACC:
1486 if (ep)
1487 fc_exch_abts_resp(ep, fp);
1488 else
1489 fc_frame_free(fp);
1490 break;
1491 case FC_RCTL_BA_ABTS:
1492 fc_exch_recv_abts(ep, fp);
1493 break;
1494 default: /* ignore junk */
1495 fc_frame_free(fp);
1496 break;
1499 if (ep)
1500 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1504 * fc_seq_ls_acc() - Accept sequence with LS_ACC
1505 * @rx_fp: The received frame, not freed here.
1507 * If this fails due to allocation or transmit congestion, assume the
1508 * originator will repeat the sequence.
1510 static void fc_seq_ls_acc(struct fc_frame *rx_fp)
1512 struct fc_lport *lport;
1513 struct fc_els_ls_acc *acc;
1514 struct fc_frame *fp;
1516 lport = fr_dev(rx_fp);
1517 fp = fc_frame_alloc(lport, sizeof(*acc));
1518 if (!fp)
1519 return;
1520 acc = fc_frame_payload_get(fp, sizeof(*acc));
1521 memset(acc, 0, sizeof(*acc));
1522 acc->la_cmd = ELS_LS_ACC;
1523 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1524 lport->tt.frame_send(lport, fp);
1528 * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
1529 * @rx_fp: The received frame, not freed here.
1530 * @reason: The reason the sequence is being rejected
1531 * @explan: The explanation for the rejection
1533 * If this fails due to allocation or transmit congestion, assume the
1534 * originator will repeat the sequence.
1536 static void fc_seq_ls_rjt(struct fc_frame *rx_fp, enum fc_els_rjt_reason reason,
1537 enum fc_els_rjt_explan explan)
1539 struct fc_lport *lport;
1540 struct fc_els_ls_rjt *rjt;
1541 struct fc_frame *fp;
1543 lport = fr_dev(rx_fp);
1544 fp = fc_frame_alloc(lport, sizeof(*rjt));
1545 if (!fp)
1546 return;
1547 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1548 memset(rjt, 0, sizeof(*rjt));
1549 rjt->er_cmd = ELS_LS_RJT;
1550 rjt->er_reason = reason;
1551 rjt->er_explan = explan;
1552 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1553 lport->tt.frame_send(lport, fp);
1557 * fc_exch_reset() - Reset an exchange
1558 * @ep: The exchange to be reset
1560 static void fc_exch_reset(struct fc_exch *ep)
1562 struct fc_seq *sp;
1563 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1564 void *arg;
1565 int rc = 1;
1567 spin_lock_bh(&ep->ex_lock);
1568 ep->state |= FC_EX_RST_CLEANUP;
1569 if (cancel_delayed_work(&ep->timeout_work))
1570 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1571 resp = ep->resp;
1572 ep->resp = NULL;
1573 if (ep->esb_stat & ESB_ST_REC_QUAL)
1574 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1575 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1576 arg = ep->arg;
1577 sp = &ep->seq;
1578 rc = fc_exch_done_locked(ep);
1579 spin_unlock_bh(&ep->ex_lock);
1580 if (!rc)
1581 fc_exch_delete(ep);
1583 if (resp)
1584 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1588 * fc_exch_pool_reset() - Reset a per cpu exchange pool
1589 * @lport: The local port that the exchange pool is on
1590 * @pool: The exchange pool to be reset
1591 * @sid: The source ID
1592 * @did: The destination ID
1594 * Resets a per cpu exches pool, releasing all of its sequences
1595 * and exchanges. If sid is non-zero then reset only exchanges
1596 * we sourced from the local port's FID. If did is non-zero then
1597 * only reset exchanges destined for the local port's FID.
1599 static void fc_exch_pool_reset(struct fc_lport *lport,
1600 struct fc_exch_pool *pool,
1601 u32 sid, u32 did)
1603 struct fc_exch *ep;
1604 struct fc_exch *next;
1606 spin_lock_bh(&pool->lock);
1607 restart:
1608 list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
1609 if ((lport == ep->lp) &&
1610 (sid == 0 || sid == ep->sid) &&
1611 (did == 0 || did == ep->did)) {
1612 fc_exch_hold(ep);
1613 spin_unlock_bh(&pool->lock);
1615 fc_exch_reset(ep);
1617 fc_exch_release(ep);
1618 spin_lock_bh(&pool->lock);
1621 * must restart loop incase while lock
1622 * was down multiple eps were released.
1624 goto restart;
1627 spin_unlock_bh(&pool->lock);
1631 * fc_exch_mgr_reset() - Reset all EMs of a local port
1632 * @lport: The local port whose EMs are to be reset
1633 * @sid: The source ID
1634 * @did: The destination ID
1636 * Reset all EMs associated with a given local port. Release all
1637 * sequences and exchanges. If sid is non-zero then reset only the
1638 * exchanges sent from the local port's FID. If did is non-zero then
1639 * reset only exchanges destined for the local port's FID.
1641 void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1643 struct fc_exch_mgr_anchor *ema;
1644 unsigned int cpu;
1646 list_for_each_entry(ema, &lport->ema_list, ema_list) {
1647 for_each_possible_cpu(cpu)
1648 fc_exch_pool_reset(lport,
1649 per_cpu_ptr(ema->mp->pool, cpu),
1650 sid, did);
1653 EXPORT_SYMBOL(fc_exch_mgr_reset);
1656 * fc_exch_lookup() - find an exchange
1657 * @lport: The local port
1658 * @xid: The exchange ID
1660 * Returns exchange pointer with hold for caller, or NULL if not found.
1662 static struct fc_exch *fc_exch_lookup(struct fc_lport *lport, u32 xid)
1664 struct fc_exch_mgr_anchor *ema;
1666 list_for_each_entry(ema, &lport->ema_list, ema_list)
1667 if (ema->mp->min_xid <= xid && xid <= ema->mp->max_xid)
1668 return fc_exch_find(ema->mp, xid);
1669 return NULL;
1673 * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
1674 * @rfp: The REC frame, not freed here.
1676 * Note that the requesting port may be different than the S_ID in the request.
1678 static void fc_exch_els_rec(struct fc_frame *rfp)
1680 struct fc_lport *lport;
1681 struct fc_frame *fp;
1682 struct fc_exch *ep;
1683 struct fc_els_rec *rp;
1684 struct fc_els_rec_acc *acc;
1685 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1686 enum fc_els_rjt_explan explan;
1687 u32 sid;
1688 u16 rxid;
1689 u16 oxid;
1691 lport = fr_dev(rfp);
1692 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1693 explan = ELS_EXPL_INV_LEN;
1694 if (!rp)
1695 goto reject;
1696 sid = ntoh24(rp->rec_s_id);
1697 rxid = ntohs(rp->rec_rx_id);
1698 oxid = ntohs(rp->rec_ox_id);
1700 ep = fc_exch_lookup(lport,
1701 sid == fc_host_port_id(lport->host) ? oxid : rxid);
1702 explan = ELS_EXPL_OXID_RXID;
1703 if (!ep)
1704 goto reject;
1705 if (ep->oid != sid || oxid != ep->oxid)
1706 goto rel;
1707 if (rxid != FC_XID_UNKNOWN && rxid != ep->rxid)
1708 goto rel;
1709 fp = fc_frame_alloc(lport, sizeof(*acc));
1710 if (!fp)
1711 goto out;
1713 acc = fc_frame_payload_get(fp, sizeof(*acc));
1714 memset(acc, 0, sizeof(*acc));
1715 acc->reca_cmd = ELS_LS_ACC;
1716 acc->reca_ox_id = rp->rec_ox_id;
1717 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1718 acc->reca_rx_id = htons(ep->rxid);
1719 if (ep->sid == ep->oid)
1720 hton24(acc->reca_rfid, ep->did);
1721 else
1722 hton24(acc->reca_rfid, ep->sid);
1723 acc->reca_fc4value = htonl(ep->seq.rec_data);
1724 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1725 ESB_ST_SEQ_INIT |
1726 ESB_ST_COMPLETE));
1727 fc_fill_reply_hdr(fp, rfp, FC_RCTL_ELS_REP, 0);
1728 lport->tt.frame_send(lport, fp);
1729 out:
1730 fc_exch_release(ep);
1731 return;
1733 rel:
1734 fc_exch_release(ep);
1735 reject:
1736 fc_seq_ls_rjt(rfp, reason, explan);
1740 * fc_exch_rrq_resp() - Handler for RRQ responses
1741 * @sp: The sequence that the RRQ is on
1742 * @fp: The RRQ frame
1743 * @arg: The exchange that the RRQ is on
1745 * TODO: fix error handler.
1747 static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1749 struct fc_exch *aborted_ep = arg;
1750 unsigned int op;
1752 if (IS_ERR(fp)) {
1753 int err = PTR_ERR(fp);
1755 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
1756 goto cleanup;
1757 FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
1758 "frame error %d\n", err);
1759 return;
1762 op = fc_frame_payload_op(fp);
1763 fc_frame_free(fp);
1765 switch (op) {
1766 case ELS_LS_RJT:
1767 FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
1768 /* fall through */
1769 case ELS_LS_ACC:
1770 goto cleanup;
1771 default:
1772 FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
1773 "for RRQ", op);
1774 return;
1777 cleanup:
1778 fc_exch_done(&aborted_ep->seq);
1779 /* drop hold for rec qual */
1780 fc_exch_release(aborted_ep);
1785 * fc_exch_seq_send() - Send a frame using a new exchange and sequence
1786 * @lport: The local port to send the frame on
1787 * @fp: The frame to be sent
1788 * @resp: The response handler for this request
1789 * @destructor: The destructor for the exchange
1790 * @arg: The argument to be passed to the response handler
1791 * @timer_msec: The timeout period for the exchange
1793 * The frame pointer with some of the header's fields must be
1794 * filled before calling this routine, those fields are:
1796 * - routing control
1797 * - FC port did
1798 * - FC port sid
1799 * - FC header type
1800 * - frame control
1801 * - parameter or relative offset
1803 static struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
1804 struct fc_frame *fp,
1805 void (*resp)(struct fc_seq *,
1806 struct fc_frame *fp,
1807 void *arg),
1808 void (*destructor)(struct fc_seq *,
1809 void *),
1810 void *arg, u32 timer_msec)
1812 struct fc_exch *ep;
1813 struct fc_seq *sp = NULL;
1814 struct fc_frame_header *fh;
1815 int rc = 1;
1817 ep = fc_exch_alloc(lport, fp);
1818 if (!ep) {
1819 fc_frame_free(fp);
1820 return NULL;
1822 ep->esb_stat |= ESB_ST_SEQ_INIT;
1823 fh = fc_frame_header_get(fp);
1824 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1825 ep->resp = resp;
1826 ep->destructor = destructor;
1827 ep->arg = arg;
1828 ep->r_a_tov = FC_DEF_R_A_TOV;
1829 ep->lp = lport;
1830 sp = &ep->seq;
1832 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1833 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1834 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1835 sp->cnt++;
1837 if (ep->xid <= lport->lro_xid && fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD)
1838 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1840 if (unlikely(lport->tt.frame_send(lport, fp)))
1841 goto err;
1843 if (timer_msec)
1844 fc_exch_timer_set_locked(ep, timer_msec);
1845 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1847 if (ep->f_ctl & FC_FC_SEQ_INIT)
1848 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1849 spin_unlock_bh(&ep->ex_lock);
1850 return sp;
1851 err:
1852 rc = fc_exch_done_locked(ep);
1853 spin_unlock_bh(&ep->ex_lock);
1854 if (!rc)
1855 fc_exch_delete(ep);
1856 return NULL;
1860 * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
1861 * @ep: The exchange to send the RRQ on
1863 * This tells the remote port to stop blocking the use of
1864 * the exchange and the seq_cnt range.
1866 static void fc_exch_rrq(struct fc_exch *ep)
1868 struct fc_lport *lport;
1869 struct fc_els_rrq *rrq;
1870 struct fc_frame *fp;
1871 u32 did;
1873 lport = ep->lp;
1875 fp = fc_frame_alloc(lport, sizeof(*rrq));
1876 if (!fp)
1877 goto retry;
1879 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1880 memset(rrq, 0, sizeof(*rrq));
1881 rrq->rrq_cmd = ELS_RRQ;
1882 hton24(rrq->rrq_s_id, ep->sid);
1883 rrq->rrq_ox_id = htons(ep->oxid);
1884 rrq->rrq_rx_id = htons(ep->rxid);
1886 did = ep->did;
1887 if (ep->esb_stat & ESB_ST_RESP)
1888 did = ep->sid;
1890 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1891 lport->port_id, FC_TYPE_ELS,
1892 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1894 if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
1895 lport->e_d_tov))
1896 return;
1898 retry:
1899 spin_lock_bh(&ep->ex_lock);
1900 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1901 spin_unlock_bh(&ep->ex_lock);
1902 /* drop hold for rec qual */
1903 fc_exch_release(ep);
1904 return;
1906 ep->esb_stat |= ESB_ST_REC_QUAL;
1907 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1908 spin_unlock_bh(&ep->ex_lock);
1912 * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
1913 * @fp: The RRQ frame, not freed here.
1915 static void fc_exch_els_rrq(struct fc_frame *fp)
1917 struct fc_lport *lport;
1918 struct fc_exch *ep = NULL; /* request or subject exchange */
1919 struct fc_els_rrq *rp;
1920 u32 sid;
1921 u16 xid;
1922 enum fc_els_rjt_explan explan;
1924 lport = fr_dev(fp);
1925 rp = fc_frame_payload_get(fp, sizeof(*rp));
1926 explan = ELS_EXPL_INV_LEN;
1927 if (!rp)
1928 goto reject;
1931 * lookup subject exchange.
1933 sid = ntoh24(rp->rrq_s_id); /* subject source */
1934 xid = fc_host_port_id(lport->host) == sid ?
1935 ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
1936 ep = fc_exch_lookup(lport, xid);
1937 explan = ELS_EXPL_OXID_RXID;
1938 if (!ep)
1939 goto reject;
1940 spin_lock_bh(&ep->ex_lock);
1941 if (ep->oxid != ntohs(rp->rrq_ox_id))
1942 goto unlock_reject;
1943 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
1944 ep->rxid != FC_XID_UNKNOWN)
1945 goto unlock_reject;
1946 explan = ELS_EXPL_SID;
1947 if (ep->sid != sid)
1948 goto unlock_reject;
1951 * Clear Recovery Qualifier state, and cancel timer if complete.
1953 if (ep->esb_stat & ESB_ST_REC_QUAL) {
1954 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1955 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
1957 if (ep->esb_stat & ESB_ST_COMPLETE) {
1958 if (cancel_delayed_work(&ep->timeout_work))
1959 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
1962 spin_unlock_bh(&ep->ex_lock);
1965 * Send LS_ACC.
1967 fc_seq_ls_acc(fp);
1968 goto out;
1970 unlock_reject:
1971 spin_unlock_bh(&ep->ex_lock);
1972 reject:
1973 fc_seq_ls_rjt(fp, ELS_RJT_LOGIC, explan);
1974 out:
1975 if (ep)
1976 fc_exch_release(ep); /* drop hold from fc_exch_find */
1980 * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
1981 * @lport: The local port to add the exchange manager to
1982 * @mp: The exchange manager to be added to the local port
1983 * @match: The match routine that indicates when this EM should be used
1985 struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
1986 struct fc_exch_mgr *mp,
1987 bool (*match)(struct fc_frame *))
1989 struct fc_exch_mgr_anchor *ema;
1991 ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
1992 if (!ema)
1993 return ema;
1995 ema->mp = mp;
1996 ema->match = match;
1997 /* add EM anchor to EM anchors list */
1998 list_add_tail(&ema->ema_list, &lport->ema_list);
1999 kref_get(&mp->kref);
2000 return ema;
2002 EXPORT_SYMBOL(fc_exch_mgr_add);
2005 * fc_exch_mgr_destroy() - Destroy an exchange manager
2006 * @kref: The reference to the EM to be destroyed
2008 static void fc_exch_mgr_destroy(struct kref *kref)
2010 struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
2012 mempool_destroy(mp->ep_pool);
2013 free_percpu(mp->pool);
2014 kfree(mp);
2018 * fc_exch_mgr_del() - Delete an EM from a local port's list
2019 * @ema: The exchange manager anchor identifying the EM to be deleted
2021 void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
2023 /* remove EM anchor from EM anchors list */
2024 list_del(&ema->ema_list);
2025 kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
2026 kfree(ema);
2028 EXPORT_SYMBOL(fc_exch_mgr_del);
2031 * fc_exch_mgr_list_clone() - Share all exchange manager objects
2032 * @src: Source lport to clone exchange managers from
2033 * @dst: New lport that takes references to all the exchange managers
2035 int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
2037 struct fc_exch_mgr_anchor *ema, *tmp;
2039 list_for_each_entry(ema, &src->ema_list, ema_list) {
2040 if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
2041 goto err;
2043 return 0;
2044 err:
2045 list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
2046 fc_exch_mgr_del(ema);
2047 return -ENOMEM;
2051 * fc_exch_mgr_alloc() - Allocate an exchange manager
2052 * @lport: The local port that the new EM will be associated with
2053 * @class: The default FC class for new exchanges
2054 * @min_xid: The minimum XID for exchanges from the new EM
2055 * @max_xid: The maximum XID for exchanges from the new EM
2056 * @match: The match routine for the new EM
2058 struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
2059 enum fc_class class,
2060 u16 min_xid, u16 max_xid,
2061 bool (*match)(struct fc_frame *))
2063 struct fc_exch_mgr *mp;
2064 u16 pool_exch_range;
2065 size_t pool_size;
2066 unsigned int cpu;
2067 struct fc_exch_pool *pool;
2069 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
2070 (min_xid & fc_cpu_mask) != 0) {
2071 FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
2072 min_xid, max_xid);
2073 return NULL;
2077 * allocate memory for EM
2079 mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
2080 if (!mp)
2081 return NULL;
2083 mp->class = class;
2084 /* adjust em exch xid range for offload */
2085 mp->min_xid = min_xid;
2086 mp->max_xid = max_xid;
2088 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
2089 if (!mp->ep_pool)
2090 goto free_mp;
2093 * Setup per cpu exch pool with entire exchange id range equally
2094 * divided across all cpus. The exch pointers array memory is
2095 * allocated for exch range per pool.
2097 pool_exch_range = (mp->max_xid - mp->min_xid + 1) / (fc_cpu_mask + 1);
2098 mp->pool_max_index = pool_exch_range - 1;
2101 * Allocate and initialize per cpu exch pool
2103 pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
2104 mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
2105 if (!mp->pool)
2106 goto free_mempool;
2107 for_each_possible_cpu(cpu) {
2108 pool = per_cpu_ptr(mp->pool, cpu);
2109 spin_lock_init(&pool->lock);
2110 INIT_LIST_HEAD(&pool->ex_list);
2113 kref_init(&mp->kref);
2114 if (!fc_exch_mgr_add(lport, mp, match)) {
2115 free_percpu(mp->pool);
2116 goto free_mempool;
2120 * Above kref_init() sets mp->kref to 1 and then
2121 * call to fc_exch_mgr_add incremented mp->kref again,
2122 * so adjust that extra increment.
2124 kref_put(&mp->kref, fc_exch_mgr_destroy);
2125 return mp;
2127 free_mempool:
2128 mempool_destroy(mp->ep_pool);
2129 free_mp:
2130 kfree(mp);
2131 return NULL;
2133 EXPORT_SYMBOL(fc_exch_mgr_alloc);
2136 * fc_exch_mgr_free() - Free all exchange managers on a local port
2137 * @lport: The local port whose EMs are to be freed
2139 void fc_exch_mgr_free(struct fc_lport *lport)
2141 struct fc_exch_mgr_anchor *ema, *next;
2143 flush_workqueue(fc_exch_workqueue);
2144 list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
2145 fc_exch_mgr_del(ema);
2147 EXPORT_SYMBOL(fc_exch_mgr_free);
2150 * fc_exch_recv() - Handler for received frames
2151 * @lport: The local port the frame was received on
2152 * @fp: The received frame
2154 void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
2156 struct fc_frame_header *fh = fc_frame_header_get(fp);
2157 struct fc_exch_mgr_anchor *ema;
2158 u32 f_ctl, found = 0;
2159 u16 oxid;
2161 /* lport lock ? */
2162 if (!lport || lport->state == LPORT_ST_DISABLED) {
2163 FC_LPORT_DBG(lport, "Receiving frames for an lport that "
2164 "has not been initialized correctly\n");
2165 fc_frame_free(fp);
2166 return;
2169 f_ctl = ntoh24(fh->fh_f_ctl);
2170 oxid = ntohs(fh->fh_ox_id);
2171 if (f_ctl & FC_FC_EX_CTX) {
2172 list_for_each_entry(ema, &lport->ema_list, ema_list) {
2173 if ((oxid >= ema->mp->min_xid) &&
2174 (oxid <= ema->mp->max_xid)) {
2175 found = 1;
2176 break;
2180 if (!found) {
2181 FC_LPORT_DBG(lport, "Received response for out "
2182 "of range oxid:%hx\n", oxid);
2183 fc_frame_free(fp);
2184 return;
2186 } else
2187 ema = list_entry(lport->ema_list.prev, typeof(*ema), ema_list);
2190 * If frame is marked invalid, just drop it.
2192 switch (fr_eof(fp)) {
2193 case FC_EOF_T:
2194 if (f_ctl & FC_FC_END_SEQ)
2195 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
2196 /* fall through */
2197 case FC_EOF_N:
2198 if (fh->fh_type == FC_TYPE_BLS)
2199 fc_exch_recv_bls(ema->mp, fp);
2200 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
2201 FC_FC_EX_CTX)
2202 fc_exch_recv_seq_resp(ema->mp, fp);
2203 else if (f_ctl & FC_FC_SEQ_CTX)
2204 fc_exch_recv_resp(ema->mp, fp);
2205 else /* no EX_CTX and no SEQ_CTX */
2206 fc_exch_recv_req(lport, ema->mp, fp);
2207 break;
2208 default:
2209 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
2210 fr_eof(fp));
2211 fc_frame_free(fp);
2214 EXPORT_SYMBOL(fc_exch_recv);
2217 * fc_exch_init() - Initialize the exchange layer for a local port
2218 * @lport: The local port to initialize the exchange layer for
2220 int fc_exch_init(struct fc_lport *lport)
2222 if (!lport->tt.seq_start_next)
2223 lport->tt.seq_start_next = fc_seq_start_next;
2225 if (!lport->tt.exch_seq_send)
2226 lport->tt.exch_seq_send = fc_exch_seq_send;
2228 if (!lport->tt.seq_send)
2229 lport->tt.seq_send = fc_seq_send;
2231 if (!lport->tt.seq_els_rsp_send)
2232 lport->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
2234 if (!lport->tt.exch_done)
2235 lport->tt.exch_done = fc_exch_done;
2237 if (!lport->tt.exch_mgr_reset)
2238 lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
2240 if (!lport->tt.seq_exch_abort)
2241 lport->tt.seq_exch_abort = fc_seq_exch_abort;
2243 if (!lport->tt.seq_assign)
2244 lport->tt.seq_assign = fc_seq_assign;
2246 return 0;
2248 EXPORT_SYMBOL(fc_exch_init);
2251 * fc_setup_exch_mgr() - Setup an exchange manager
2253 int fc_setup_exch_mgr()
2255 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
2256 0, SLAB_HWCACHE_ALIGN, NULL);
2257 if (!fc_em_cachep)
2258 return -ENOMEM;
2261 * Initialize fc_cpu_mask and fc_cpu_order. The
2262 * fc_cpu_mask is set for nr_cpu_ids rounded up
2263 * to order of 2's * power and order is stored
2264 * in fc_cpu_order as this is later required in
2265 * mapping between an exch id and exch array index
2266 * in per cpu exch pool.
2268 * This round up is required to align fc_cpu_mask
2269 * to exchange id's lower bits such that all incoming
2270 * frames of an exchange gets delivered to the same
2271 * cpu on which exchange originated by simple bitwise
2272 * AND operation between fc_cpu_mask and exchange id.
2274 fc_cpu_mask = 1;
2275 fc_cpu_order = 0;
2276 while (fc_cpu_mask < nr_cpu_ids) {
2277 fc_cpu_mask <<= 1;
2278 fc_cpu_order++;
2280 fc_cpu_mask--;
2282 fc_exch_workqueue = create_singlethread_workqueue("fc_exch_workqueue");
2283 if (!fc_exch_workqueue)
2284 return -ENOMEM;
2285 return 0;
2289 * fc_destroy_exch_mgr() - Destroy an exchange manager
2291 void fc_destroy_exch_mgr()
2293 destroy_workqueue(fc_exch_workqueue);
2294 kmem_cache_destroy(fc_em_cachep);