[SCSI] libfc: fix mem leak in fc_exch_recv_seq_resp()
[linux-2.6/x86.git] / drivers / scsi / libfc / fc_exch.c
blobd0df1b2faf25184b3fc3752e6a23429736312bdf
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;
98 * currently exchange mgr stats are updated but not used.
99 * either stats can be expose via sysfs or remove them
100 * all together if not used XXX
102 struct {
103 atomic_t no_free_exch;
104 atomic_t no_free_exch_xid;
105 atomic_t xid_not_found;
106 atomic_t xid_busy;
107 atomic_t seq_not_found;
108 atomic_t non_bls_resp;
109 } stats;
113 * struct fc_exch_mgr_anchor - primary structure for list of EMs
114 * @ema_list: Exchange Manager Anchor list
115 * @mp: Exchange Manager associated with this anchor
116 * @match: Routine to determine if this anchor's EM should be used
118 * When walking the list of anchors the match routine will be called
119 * for each anchor to determine if that EM should be used. The last
120 * anchor in the list will always match to handle any exchanges not
121 * handled by other EMs. The non-default EMs would be added to the
122 * anchor list by HW that provides FCoE offloads.
124 struct fc_exch_mgr_anchor {
125 struct list_head ema_list;
126 struct fc_exch_mgr *mp;
127 bool (*match)(struct fc_frame *);
130 static void fc_exch_rrq(struct fc_exch *);
131 static void fc_seq_ls_acc(struct fc_frame *);
132 static void fc_seq_ls_rjt(struct fc_frame *, enum fc_els_rjt_reason,
133 enum fc_els_rjt_explan);
134 static void fc_exch_els_rec(struct fc_frame *);
135 static void fc_exch_els_rrq(struct fc_frame *);
138 * Internal implementation notes.
140 * The exchange manager is one by default in libfc but LLD may choose
141 * to have one per CPU. The sequence manager is one per exchange manager
142 * and currently never separated.
144 * Section 9.8 in FC-FS-2 specifies: "The SEQ_ID is a one-byte field
145 * assigned by the Sequence Initiator that shall be unique for a specific
146 * D_ID and S_ID pair while the Sequence is open." Note that it isn't
147 * qualified by exchange ID, which one might think it would be.
148 * In practice this limits the number of open sequences and exchanges to 256
149 * per session. For most targets we could treat this limit as per exchange.
151 * The exchange and its sequence are freed when the last sequence is received.
152 * It's possible for the remote port to leave an exchange open without
153 * sending any sequences.
155 * Notes on reference counts:
157 * Exchanges are reference counted and exchange gets freed when the reference
158 * count becomes zero.
160 * Timeouts:
161 * Sequences are timed out for E_D_TOV and R_A_TOV.
163 * Sequence event handling:
165 * The following events may occur on initiator sequences:
167 * Send.
168 * For now, the whole thing is sent.
169 * Receive ACK
170 * This applies only to class F.
171 * The sequence is marked complete.
172 * ULP completion.
173 * The upper layer calls fc_exch_done() when done
174 * with exchange and sequence tuple.
175 * RX-inferred completion.
176 * When we receive the next sequence on the same exchange, we can
177 * retire the previous sequence ID. (XXX not implemented).
178 * Timeout.
179 * R_A_TOV frees the sequence ID. If we're waiting for ACK,
180 * E_D_TOV causes abort and calls upper layer response handler
181 * with FC_EX_TIMEOUT error.
182 * Receive RJT
183 * XXX defer.
184 * Send ABTS
185 * On timeout.
187 * The following events may occur on recipient sequences:
189 * Receive
190 * Allocate sequence for first frame received.
191 * Hold during receive handler.
192 * Release when final frame received.
193 * Keep status of last N of these for the ELS RES command. XXX TBD.
194 * Receive ABTS
195 * Deallocate sequence
196 * Send RJT
197 * Deallocate
199 * For now, we neglect conditions where only part of a sequence was
200 * received or transmitted, or where out-of-order receipt is detected.
204 * Locking notes:
206 * The EM code run in a per-CPU worker thread.
208 * To protect against concurrency between a worker thread code and timers,
209 * sequence allocation and deallocation must be locked.
210 * - exchange refcnt can be done atomicly without locks.
211 * - sequence allocation must be locked by exch lock.
212 * - If the EM pool lock and ex_lock must be taken at the same time, then the
213 * EM pool lock must be taken before the ex_lock.
217 * opcode names for debugging.
219 static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
222 * fc_exch_name_lookup() - Lookup name by opcode
223 * @op: Opcode to be looked up
224 * @table: Opcode/name table
225 * @max_index: Index not to be exceeded
227 * This routine is used to determine a human-readable string identifying
228 * a R_CTL opcode.
230 static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
231 unsigned int max_index)
233 const char *name = NULL;
235 if (op < max_index)
236 name = table[op];
237 if (!name)
238 name = "unknown";
239 return name;
243 * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
244 * @op: The opcode to be looked up
246 static const char *fc_exch_rctl_name(unsigned int op)
248 return fc_exch_name_lookup(op, fc_exch_rctl_names,
249 ARRAY_SIZE(fc_exch_rctl_names));
253 * fc_exch_hold() - Increment an exchange's reference count
254 * @ep: Echange to be held
256 static inline void fc_exch_hold(struct fc_exch *ep)
258 atomic_inc(&ep->ex_refcnt);
262 * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
263 * and determine SOF and EOF.
264 * @ep: The exchange to that will use the header
265 * @fp: The frame whose header is to be modified
266 * @f_ctl: F_CTL bits that will be used for the frame header
268 * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
269 * fh_seq_id, fh_seq_cnt and the SOF and EOF.
271 static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
272 u32 f_ctl)
274 struct fc_frame_header *fh = fc_frame_header_get(fp);
275 u16 fill;
277 fr_sof(fp) = ep->class;
278 if (ep->seq.cnt)
279 fr_sof(fp) = fc_sof_normal(ep->class);
281 if (f_ctl & FC_FC_END_SEQ) {
282 fr_eof(fp) = FC_EOF_T;
283 if (fc_sof_needs_ack(ep->class))
284 fr_eof(fp) = FC_EOF_N;
286 * From F_CTL.
287 * The number of fill bytes to make the length a 4-byte
288 * multiple is the low order 2-bits of the f_ctl.
289 * The fill itself will have been cleared by the frame
290 * allocation.
291 * After this, the length will be even, as expected by
292 * the transport.
294 fill = fr_len(fp) & 3;
295 if (fill) {
296 fill = 4 - fill;
297 /* TODO, this may be a problem with fragmented skb */
298 skb_put(fp_skb(fp), fill);
299 hton24(fh->fh_f_ctl, f_ctl | fill);
301 } else {
302 WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
303 fr_eof(fp) = FC_EOF_N;
307 * Initialize remainig fh fields
308 * from fc_fill_fc_hdr
310 fh->fh_ox_id = htons(ep->oxid);
311 fh->fh_rx_id = htons(ep->rxid);
312 fh->fh_seq_id = ep->seq.id;
313 fh->fh_seq_cnt = htons(ep->seq.cnt);
317 * fc_exch_release() - Decrement an exchange's reference count
318 * @ep: Exchange to be released
320 * If the reference count reaches zero and the exchange is complete,
321 * it is freed.
323 static void fc_exch_release(struct fc_exch *ep)
325 struct fc_exch_mgr *mp;
327 if (atomic_dec_and_test(&ep->ex_refcnt)) {
328 mp = ep->em;
329 if (ep->destructor)
330 ep->destructor(&ep->seq, ep->arg);
331 WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
332 mempool_free(ep, mp->ep_pool);
337 * fc_exch_done_locked() - Complete an exchange with the exchange lock held
338 * @ep: The exchange that is complete
340 static int fc_exch_done_locked(struct fc_exch *ep)
342 int rc = 1;
345 * We must check for completion in case there are two threads
346 * tyring to complete this. But the rrq code will reuse the
347 * ep, and in that case we only clear the resp and set it as
348 * complete, so it can be reused by the timer to send the rrq.
350 ep->resp = NULL;
351 if (ep->state & FC_EX_DONE)
352 return rc;
353 ep->esb_stat |= ESB_ST_COMPLETE;
355 if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
356 ep->state |= FC_EX_DONE;
357 if (cancel_delayed_work(&ep->timeout_work))
358 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
359 rc = 0;
361 return rc;
365 * fc_exch_ptr_get() - Return an exchange from an exchange pool
366 * @pool: Exchange Pool to get an exchange from
367 * @index: Index of the exchange within the pool
369 * Use the index to get an exchange from within an exchange pool. exches
370 * will point to an array of exchange pointers. The index will select
371 * the exchange within the array.
373 static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
374 u16 index)
376 struct fc_exch **exches = (struct fc_exch **)(pool + 1);
377 return exches[index];
381 * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
382 * @pool: The pool to assign the exchange to
383 * @index: The index in the pool where the exchange will be assigned
384 * @ep: The exchange to assign to the pool
386 static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
387 struct fc_exch *ep)
389 ((struct fc_exch **)(pool + 1))[index] = ep;
393 * fc_exch_delete() - Delete an exchange
394 * @ep: The exchange to be deleted
396 static void fc_exch_delete(struct fc_exch *ep)
398 struct fc_exch_pool *pool;
400 pool = ep->pool;
401 spin_lock_bh(&pool->lock);
402 WARN_ON(pool->total_exches <= 0);
403 pool->total_exches--;
404 fc_exch_ptr_set(pool, (ep->xid - ep->em->min_xid) >> fc_cpu_order,
405 NULL);
406 list_del(&ep->ex_list);
407 spin_unlock_bh(&pool->lock);
408 fc_exch_release(ep); /* drop hold for exch in mp */
412 * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
413 * the exchange lock held
414 * @ep: The exchange whose timer will start
415 * @timer_msec: The timeout period
417 * Used for upper level protocols to time out the exchange.
418 * The timer is cancelled when it fires or when the exchange completes.
420 static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
421 unsigned int timer_msec)
423 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
424 return;
426 FC_EXCH_DBG(ep, "Exchange timer armed\n");
428 if (queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
429 msecs_to_jiffies(timer_msec)))
430 fc_exch_hold(ep); /* hold for timer */
434 * fc_exch_timer_set() - Lock the exchange and set the timer
435 * @ep: The exchange whose timer will start
436 * @timer_msec: The timeout period
438 static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
440 spin_lock_bh(&ep->ex_lock);
441 fc_exch_timer_set_locked(ep, timer_msec);
442 spin_unlock_bh(&ep->ex_lock);
446 * fc_seq_send() - Send a frame using existing sequence/exchange pair
447 * @lport: The local port that the exchange will be sent on
448 * @sp: The sequence to be sent
449 * @fp: The frame to be sent on the exchange
451 static int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp,
452 struct fc_frame *fp)
454 struct fc_exch *ep;
455 struct fc_frame_header *fh = fc_frame_header_get(fp);
456 int error;
457 u32 f_ctl;
459 ep = fc_seq_exch(sp);
460 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
462 f_ctl = ntoh24(fh->fh_f_ctl);
463 fc_exch_setup_hdr(ep, fp, f_ctl);
464 fr_encaps(fp) = ep->encaps;
467 * update sequence count if this frame is carrying
468 * multiple FC frames when sequence offload is enabled
469 * by LLD.
471 if (fr_max_payload(fp))
472 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
473 fr_max_payload(fp));
474 else
475 sp->cnt++;
478 * Send the frame.
480 error = lport->tt.frame_send(lport, fp);
483 * Update the exchange and sequence flags,
484 * assuming all frames for the sequence have been sent.
485 * We can only be called to send once for each sequence.
487 spin_lock_bh(&ep->ex_lock);
488 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
489 if (f_ctl & FC_FC_SEQ_INIT)
490 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
491 spin_unlock_bh(&ep->ex_lock);
492 return error;
496 * fc_seq_alloc() - Allocate a sequence for a given exchange
497 * @ep: The exchange to allocate a new sequence for
498 * @seq_id: The sequence ID to be used
500 * We don't support multiple originated sequences on the same exchange.
501 * By implication, any previously originated sequence on this exchange
502 * is complete, and we reallocate the same sequence.
504 static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
506 struct fc_seq *sp;
508 sp = &ep->seq;
509 sp->ssb_stat = 0;
510 sp->cnt = 0;
511 sp->id = seq_id;
512 return sp;
516 * fc_seq_start_next_locked() - Allocate a new sequence on the same
517 * exchange as the supplied sequence
518 * @sp: The sequence/exchange to get a new sequence for
520 static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
522 struct fc_exch *ep = fc_seq_exch(sp);
524 sp = fc_seq_alloc(ep, ep->seq_id++);
525 FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
526 ep->f_ctl, sp->id);
527 return sp;
531 * fc_seq_start_next() - Lock the exchange and get a new sequence
532 * for a given sequence/exchange pair
533 * @sp: The sequence/exchange to get a new exchange for
535 static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
537 struct fc_exch *ep = fc_seq_exch(sp);
539 spin_lock_bh(&ep->ex_lock);
540 sp = fc_seq_start_next_locked(sp);
541 spin_unlock_bh(&ep->ex_lock);
543 return sp;
547 * fc_seq_exch_abort() - Abort an exchange and sequence
548 * @req_sp: The sequence to be aborted
549 * @timer_msec: The period of time to wait before aborting
551 * Generally called because of a timeout or an abort from the upper layer.
553 static int fc_seq_exch_abort(const struct fc_seq *req_sp,
554 unsigned int timer_msec)
556 struct fc_seq *sp;
557 struct fc_exch *ep;
558 struct fc_frame *fp;
559 int error;
561 ep = fc_seq_exch(req_sp);
563 spin_lock_bh(&ep->ex_lock);
564 if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
565 ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
566 spin_unlock_bh(&ep->ex_lock);
567 return -ENXIO;
571 * Send the abort on a new sequence if possible.
573 sp = fc_seq_start_next_locked(&ep->seq);
574 if (!sp) {
575 spin_unlock_bh(&ep->ex_lock);
576 return -ENOMEM;
579 ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
580 if (timer_msec)
581 fc_exch_timer_set_locked(ep, timer_msec);
582 spin_unlock_bh(&ep->ex_lock);
585 * If not logged into the fabric, don't send ABTS but leave
586 * sequence active until next timeout.
588 if (!ep->sid)
589 return 0;
592 * Send an abort for the sequence that timed out.
594 fp = fc_frame_alloc(ep->lp, 0);
595 if (fp) {
596 fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
597 FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
598 error = fc_seq_send(ep->lp, sp, fp);
599 } else
600 error = -ENOBUFS;
601 return error;
605 * fc_exch_timeout() - Handle exchange timer expiration
606 * @work: The work_struct identifying the exchange that timed out
608 static void fc_exch_timeout(struct work_struct *work)
610 struct fc_exch *ep = container_of(work, struct fc_exch,
611 timeout_work.work);
612 struct fc_seq *sp = &ep->seq;
613 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
614 void *arg;
615 u32 e_stat;
616 int rc = 1;
618 FC_EXCH_DBG(ep, "Exchange timed out\n");
620 spin_lock_bh(&ep->ex_lock);
621 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
622 goto unlock;
624 e_stat = ep->esb_stat;
625 if (e_stat & ESB_ST_COMPLETE) {
626 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
627 spin_unlock_bh(&ep->ex_lock);
628 if (e_stat & ESB_ST_REC_QUAL)
629 fc_exch_rrq(ep);
630 goto done;
631 } else {
632 resp = ep->resp;
633 arg = ep->arg;
634 ep->resp = NULL;
635 if (e_stat & ESB_ST_ABNORMAL)
636 rc = fc_exch_done_locked(ep);
637 spin_unlock_bh(&ep->ex_lock);
638 if (!rc)
639 fc_exch_delete(ep);
640 if (resp)
641 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
642 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
643 goto done;
645 unlock:
646 spin_unlock_bh(&ep->ex_lock);
647 done:
649 * This release matches the hold taken when the timer was set.
651 fc_exch_release(ep);
655 * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
656 * @lport: The local port that the exchange is for
657 * @mp: The exchange manager that will allocate the exchange
659 * Returns pointer to allocated fc_exch with exch lock held.
661 static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
662 struct fc_exch_mgr *mp)
664 struct fc_exch *ep;
665 unsigned int cpu;
666 u16 index;
667 struct fc_exch_pool *pool;
669 /* allocate memory for exchange */
670 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
671 if (!ep) {
672 atomic_inc(&mp->stats.no_free_exch);
673 goto out;
675 memset(ep, 0, sizeof(*ep));
677 cpu = get_cpu();
678 pool = per_cpu_ptr(mp->pool, cpu);
679 spin_lock_bh(&pool->lock);
680 put_cpu();
681 index = pool->next_index;
682 /* allocate new exch from pool */
683 while (fc_exch_ptr_get(pool, index)) {
684 index = index == mp->pool_max_index ? 0 : index + 1;
685 if (index == pool->next_index)
686 goto err;
688 pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
690 fc_exch_hold(ep); /* hold for exch in mp */
691 spin_lock_init(&ep->ex_lock);
693 * Hold exch lock for caller to prevent fc_exch_reset()
694 * from releasing exch while fc_exch_alloc() caller is
695 * still working on exch.
697 spin_lock_bh(&ep->ex_lock);
699 fc_exch_ptr_set(pool, index, ep);
700 list_add_tail(&ep->ex_list, &pool->ex_list);
701 fc_seq_alloc(ep, ep->seq_id++);
702 pool->total_exches++;
703 spin_unlock_bh(&pool->lock);
706 * update exchange
708 ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
709 ep->em = mp;
710 ep->pool = pool;
711 ep->lp = lport;
712 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
713 ep->rxid = FC_XID_UNKNOWN;
714 ep->class = mp->class;
715 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
716 out:
717 return ep;
718 err:
719 spin_unlock_bh(&pool->lock);
720 atomic_inc(&mp->stats.no_free_exch_xid);
721 mempool_free(ep, mp->ep_pool);
722 return NULL;
726 * fc_exch_alloc() - Allocate an exchange from an EM on a
727 * local port's list of EMs.
728 * @lport: The local port that will own the exchange
729 * @fp: The FC frame that the exchange will be for
731 * This function walks the list of exchange manager(EM)
732 * anchors to select an EM for a new exchange allocation. The
733 * EM is selected when a NULL match function pointer is encountered
734 * or when a call to a match function returns true.
736 static inline struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
737 struct fc_frame *fp)
739 struct fc_exch_mgr_anchor *ema;
741 list_for_each_entry(ema, &lport->ema_list, ema_list)
742 if (!ema->match || ema->match(fp))
743 return fc_exch_em_alloc(lport, ema->mp);
744 return NULL;
748 * fc_exch_find() - Lookup and hold an exchange
749 * @mp: The exchange manager to lookup the exchange from
750 * @xid: The XID of the exchange to look up
752 static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
754 struct fc_exch_pool *pool;
755 struct fc_exch *ep = NULL;
757 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
758 pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
759 spin_lock_bh(&pool->lock);
760 ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
761 if (ep) {
762 fc_exch_hold(ep);
763 WARN_ON(ep->xid != xid);
765 spin_unlock_bh(&pool->lock);
767 return ep;
772 * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
773 * the memory allocated for the related objects may be freed.
774 * @sp: The sequence that has completed
776 static void fc_exch_done(struct fc_seq *sp)
778 struct fc_exch *ep = fc_seq_exch(sp);
779 int rc;
781 spin_lock_bh(&ep->ex_lock);
782 rc = fc_exch_done_locked(ep);
783 spin_unlock_bh(&ep->ex_lock);
784 if (!rc)
785 fc_exch_delete(ep);
789 * fc_exch_resp() - Allocate a new exchange for a response frame
790 * @lport: The local port that the exchange was for
791 * @mp: The exchange manager to allocate the exchange from
792 * @fp: The response frame
794 * Sets the responder ID in the frame header.
796 static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
797 struct fc_exch_mgr *mp,
798 struct fc_frame *fp)
800 struct fc_exch *ep;
801 struct fc_frame_header *fh;
803 ep = fc_exch_alloc(lport, fp);
804 if (ep) {
805 ep->class = fc_frame_class(fp);
808 * Set EX_CTX indicating we're responding on this exchange.
810 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
811 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
812 fh = fc_frame_header_get(fp);
813 ep->sid = ntoh24(fh->fh_d_id);
814 ep->did = ntoh24(fh->fh_s_id);
815 ep->oid = ep->did;
818 * Allocated exchange has placed the XID in the
819 * originator field. Move it to the responder field,
820 * and set the originator XID from the frame.
822 ep->rxid = ep->xid;
823 ep->oxid = ntohs(fh->fh_ox_id);
824 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
825 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
826 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
828 fc_exch_hold(ep); /* hold for caller */
829 spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */
831 return ep;
835 * fc_seq_lookup_recip() - Find a sequence where the other end
836 * originated the sequence
837 * @lport: The local port that the frame was sent to
838 * @mp: The Exchange Manager to lookup the exchange from
839 * @fp: The frame associated with the sequence we're looking for
841 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
842 * on the ep that should be released by the caller.
844 static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
845 struct fc_exch_mgr *mp,
846 struct fc_frame *fp)
848 struct fc_frame_header *fh = fc_frame_header_get(fp);
849 struct fc_exch *ep = NULL;
850 struct fc_seq *sp = NULL;
851 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
852 u32 f_ctl;
853 u16 xid;
855 f_ctl = ntoh24(fh->fh_f_ctl);
856 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
859 * Lookup or create the exchange if we will be creating the sequence.
861 if (f_ctl & FC_FC_EX_CTX) {
862 xid = ntohs(fh->fh_ox_id); /* we originated exch */
863 ep = fc_exch_find(mp, xid);
864 if (!ep) {
865 atomic_inc(&mp->stats.xid_not_found);
866 reject = FC_RJT_OX_ID;
867 goto out;
869 if (ep->rxid == FC_XID_UNKNOWN)
870 ep->rxid = ntohs(fh->fh_rx_id);
871 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
872 reject = FC_RJT_OX_ID;
873 goto rel;
875 } else {
876 xid = ntohs(fh->fh_rx_id); /* we are the responder */
879 * Special case for MDS issuing an ELS TEST with a
880 * bad rxid of 0.
881 * XXX take this out once we do the proper reject.
883 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
884 fc_frame_payload_op(fp) == ELS_TEST) {
885 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
886 xid = FC_XID_UNKNOWN;
890 * new sequence - find the exchange
892 ep = fc_exch_find(mp, xid);
893 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
894 if (ep) {
895 atomic_inc(&mp->stats.xid_busy);
896 reject = FC_RJT_RX_ID;
897 goto rel;
899 ep = fc_exch_resp(lport, mp, fp);
900 if (!ep) {
901 reject = FC_RJT_EXCH_EST; /* XXX */
902 goto out;
904 xid = ep->xid; /* get our XID */
905 } else if (!ep) {
906 atomic_inc(&mp->stats.xid_not_found);
907 reject = FC_RJT_RX_ID; /* XID not found */
908 goto out;
913 * At this point, we have the exchange held.
914 * Find or create the sequence.
916 if (fc_sof_is_init(fr_sof(fp))) {
917 sp = &ep->seq;
918 sp->ssb_stat |= SSB_ST_RESP;
919 sp->id = fh->fh_seq_id;
920 } else {
921 sp = &ep->seq;
922 if (sp->id != fh->fh_seq_id) {
923 atomic_inc(&mp->stats.seq_not_found);
924 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
925 goto rel;
928 WARN_ON(ep != fc_seq_exch(sp));
930 if (f_ctl & FC_FC_SEQ_INIT)
931 ep->esb_stat |= ESB_ST_SEQ_INIT;
933 fr_seq(fp) = sp;
934 out:
935 return reject;
936 rel:
937 fc_exch_done(&ep->seq);
938 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
939 return reject;
943 * fc_seq_lookup_orig() - Find a sequence where this end
944 * originated the sequence
945 * @mp: The Exchange Manager to lookup the exchange from
946 * @fp: The frame associated with the sequence we're looking for
948 * Does not hold the sequence for the caller.
950 static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
951 struct fc_frame *fp)
953 struct fc_frame_header *fh = fc_frame_header_get(fp);
954 struct fc_exch *ep;
955 struct fc_seq *sp = NULL;
956 u32 f_ctl;
957 u16 xid;
959 f_ctl = ntoh24(fh->fh_f_ctl);
960 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
961 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
962 ep = fc_exch_find(mp, xid);
963 if (!ep)
964 return NULL;
965 if (ep->seq.id == fh->fh_seq_id) {
967 * Save the RX_ID if we didn't previously know it.
969 sp = &ep->seq;
970 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
971 ep->rxid == FC_XID_UNKNOWN) {
972 ep->rxid = ntohs(fh->fh_rx_id);
975 fc_exch_release(ep);
976 return sp;
980 * fc_exch_set_addr() - Set the source and destination IDs for an exchange
981 * @ep: The exchange to set the addresses for
982 * @orig_id: The originator's ID
983 * @resp_id: The responder's ID
985 * Note this must be done before the first sequence of the exchange is sent.
987 static void fc_exch_set_addr(struct fc_exch *ep,
988 u32 orig_id, u32 resp_id)
990 ep->oid = orig_id;
991 if (ep->esb_stat & ESB_ST_RESP) {
992 ep->sid = resp_id;
993 ep->did = orig_id;
994 } else {
995 ep->sid = orig_id;
996 ep->did = resp_id;
1001 * fc_seq_els_rsp_send() - Send an ELS response using infomation from
1002 * the existing sequence/exchange.
1003 * @fp: The received frame
1004 * @els_cmd: The ELS command to be sent
1005 * @els_data: The ELS data to be sent
1007 * The received frame is not freed.
1009 static void fc_seq_els_rsp_send(struct fc_frame *fp, enum fc_els_cmd els_cmd,
1010 struct fc_seq_els_data *els_data)
1012 switch (els_cmd) {
1013 case ELS_LS_RJT:
1014 fc_seq_ls_rjt(fp, els_data->reason, els_data->explan);
1015 break;
1016 case ELS_LS_ACC:
1017 fc_seq_ls_acc(fp);
1018 break;
1019 case ELS_RRQ:
1020 fc_exch_els_rrq(fp);
1021 break;
1022 case ELS_REC:
1023 fc_exch_els_rec(fp);
1024 break;
1025 default:
1026 FC_LPORT_DBG(fr_dev(fp), "Invalid ELS CMD:%x\n", els_cmd);
1031 * fc_seq_send_last() - Send a sequence that is the last in the exchange
1032 * @sp: The sequence that is to be sent
1033 * @fp: The frame that will be sent on the sequence
1034 * @rctl: The R_CTL information to be sent
1035 * @fh_type: The frame header type
1037 static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
1038 enum fc_rctl rctl, enum fc_fh_type fh_type)
1040 u32 f_ctl;
1041 struct fc_exch *ep = fc_seq_exch(sp);
1043 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1044 f_ctl |= ep->f_ctl;
1045 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
1046 fc_seq_send(ep->lp, sp, fp);
1050 * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
1051 * @sp: The sequence to send the ACK on
1052 * @rx_fp: The received frame that is being acknoledged
1054 * Send ACK_1 (or equiv.) indicating we received something.
1056 static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
1058 struct fc_frame *fp;
1059 struct fc_frame_header *rx_fh;
1060 struct fc_frame_header *fh;
1061 struct fc_exch *ep = fc_seq_exch(sp);
1062 struct fc_lport *lport = ep->lp;
1063 unsigned int f_ctl;
1066 * Don't send ACKs for class 3.
1068 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
1069 fp = fc_frame_alloc(lport, 0);
1070 if (!fp)
1071 return;
1073 fh = fc_frame_header_get(fp);
1074 fh->fh_r_ctl = FC_RCTL_ACK_1;
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).
1084 rx_fh = fc_frame_header_get(rx_fp);
1085 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1086 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1087 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
1088 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
1089 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1090 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1091 hton24(fh->fh_f_ctl, f_ctl);
1093 fc_exch_setup_hdr(ep, fp, f_ctl);
1094 fh->fh_seq_id = rx_fh->fh_seq_id;
1095 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1096 fh->fh_parm_offset = htonl(1); /* ack single frame */
1098 fr_sof(fp) = fr_sof(rx_fp);
1099 if (f_ctl & FC_FC_END_SEQ)
1100 fr_eof(fp) = FC_EOF_T;
1101 else
1102 fr_eof(fp) = FC_EOF_N;
1104 lport->tt.frame_send(lport, fp);
1109 * fc_exch_send_ba_rjt() - Send BLS Reject
1110 * @rx_fp: The frame being rejected
1111 * @reason: The reason the frame is being rejected
1112 * @explan: The explaination for the rejection
1114 * This is for rejecting BA_ABTS only.
1116 static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1117 enum fc_ba_rjt_reason reason,
1118 enum fc_ba_rjt_explan explan)
1120 struct fc_frame *fp;
1121 struct fc_frame_header *rx_fh;
1122 struct fc_frame_header *fh;
1123 struct fc_ba_rjt *rp;
1124 struct fc_lport *lport;
1125 unsigned int f_ctl;
1127 lport = fr_dev(rx_fp);
1128 fp = fc_frame_alloc(lport, sizeof(*rp));
1129 if (!fp)
1130 return;
1131 fh = fc_frame_header_get(fp);
1132 rx_fh = fc_frame_header_get(rx_fp);
1134 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
1136 rp = fc_frame_payload_get(fp, sizeof(*rp));
1137 rp->br_reason = reason;
1138 rp->br_explan = explan;
1141 * seq_id, cs_ctl, df_ctl and param/offset are zero.
1143 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
1144 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
1145 fh->fh_ox_id = rx_fh->fh_ox_id;
1146 fh->fh_rx_id = rx_fh->fh_rx_id;
1147 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1148 fh->fh_r_ctl = FC_RCTL_BA_RJT;
1149 fh->fh_type = FC_TYPE_BLS;
1152 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
1153 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
1154 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1155 * Last ACK uses bits 7-6 (continue sequence),
1156 * bits 5-4 are meaningful (what kind of ACK to use).
1157 * Always set LAST_SEQ, END_SEQ.
1159 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1160 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1161 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1162 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1163 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1164 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1165 f_ctl &= ~FC_FC_FIRST_SEQ;
1166 hton24(fh->fh_f_ctl, f_ctl);
1168 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1169 fr_eof(fp) = FC_EOF_T;
1170 if (fc_sof_needs_ack(fr_sof(fp)))
1171 fr_eof(fp) = FC_EOF_N;
1173 lport->tt.frame_send(lport, fp);
1177 * fc_exch_recv_abts() - Handle an incoming ABTS
1178 * @ep: The exchange the abort was on
1179 * @rx_fp: The ABTS frame
1181 * This would be for target mode usually, but could be due to lost
1182 * FCP transfer ready, confirm or RRQ. We always handle this as an
1183 * exchange abort, ignoring the parameter.
1185 static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1187 struct fc_frame *fp;
1188 struct fc_ba_acc *ap;
1189 struct fc_frame_header *fh;
1190 struct fc_seq *sp;
1192 if (!ep)
1193 goto reject;
1194 spin_lock_bh(&ep->ex_lock);
1195 if (ep->esb_stat & ESB_ST_COMPLETE) {
1196 spin_unlock_bh(&ep->ex_lock);
1197 goto reject;
1199 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1200 fc_exch_hold(ep); /* hold for REC_QUAL */
1201 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1202 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1204 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1205 if (!fp) {
1206 spin_unlock_bh(&ep->ex_lock);
1207 goto free;
1209 fh = fc_frame_header_get(fp);
1210 ap = fc_frame_payload_get(fp, sizeof(*ap));
1211 memset(ap, 0, sizeof(*ap));
1212 sp = &ep->seq;
1213 ap->ba_high_seq_cnt = htons(0xffff);
1214 if (sp->ssb_stat & SSB_ST_RESP) {
1215 ap->ba_seq_id = sp->id;
1216 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1217 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1218 ap->ba_low_seq_cnt = htons(sp->cnt);
1220 sp = fc_seq_start_next_locked(sp);
1221 spin_unlock_bh(&ep->ex_lock);
1222 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1223 fc_frame_free(rx_fp);
1224 return;
1226 reject:
1227 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1228 free:
1229 fc_frame_free(rx_fp);
1233 * fc_seq_assign() - Assign exchange and sequence for incoming request
1234 * @lport: The local port that received the request
1235 * @fp: The request frame
1237 * On success, the sequence pointer will be returned and also in fr_seq(@fp).
1239 static struct fc_seq *fc_seq_assign(struct fc_lport *lport, struct fc_frame *fp)
1241 struct fc_exch_mgr_anchor *ema;
1243 WARN_ON(lport != fr_dev(fp));
1244 WARN_ON(fr_seq(fp));
1245 fr_seq(fp) = NULL;
1247 list_for_each_entry(ema, &lport->ema_list, ema_list)
1248 if ((!ema->match || ema->match(fp)) &&
1249 fc_seq_lookup_recip(lport, ema->mp, fp) != FC_RJT_NONE)
1250 break;
1251 return fr_seq(fp);
1255 * fc_exch_recv_req() - Handler for an incoming request
1256 * @lport: The local port that received the request
1257 * @mp: The EM that the exchange is on
1258 * @fp: The request frame
1260 * This is used when the other end is originating the exchange
1261 * and the sequence.
1263 static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
1264 struct fc_frame *fp)
1266 struct fc_frame_header *fh = fc_frame_header_get(fp);
1267 struct fc_seq *sp = NULL;
1268 struct fc_exch *ep = NULL;
1269 enum fc_pf_rjt_reason reject;
1271 /* We can have the wrong fc_lport at this point with NPIV, which is a
1272 * problem now that we know a new exchange needs to be allocated
1274 lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
1275 if (!lport) {
1276 fc_frame_free(fp);
1277 return;
1279 fr_dev(fp) = lport;
1281 BUG_ON(fr_seq(fp)); /* XXX remove later */
1284 * If the RX_ID is 0xffff, don't allocate an exchange.
1285 * The upper-level protocol may request one later, if needed.
1287 if (fh->fh_rx_id == htons(FC_XID_UNKNOWN))
1288 return lport->tt.lport_recv(lport, fp);
1290 reject = fc_seq_lookup_recip(lport, mp, fp);
1291 if (reject == FC_RJT_NONE) {
1292 sp = fr_seq(fp); /* sequence will be held */
1293 ep = fc_seq_exch(sp);
1294 fc_seq_send_ack(sp, fp);
1295 ep->encaps = fr_encaps(fp);
1298 * Call the receive function.
1300 * The receive function may allocate a new sequence
1301 * over the old one, so we shouldn't change the
1302 * sequence after this.
1304 * The frame will be freed by the receive function.
1305 * If new exch resp handler is valid then call that
1306 * first.
1308 if (ep->resp)
1309 ep->resp(sp, fp, ep->arg);
1310 else
1311 lport->tt.lport_recv(lport, fp);
1312 fc_exch_release(ep); /* release from lookup */
1313 } else {
1314 FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
1315 reject);
1316 fc_frame_free(fp);
1321 * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
1322 * end is the originator of the sequence that is a
1323 * response to our initial exchange
1324 * @mp: The EM that the exchange is on
1325 * @fp: The response frame
1327 static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1329 struct fc_frame_header *fh = fc_frame_header_get(fp);
1330 struct fc_seq *sp;
1331 struct fc_exch *ep;
1332 enum fc_sof sof;
1333 u32 f_ctl;
1334 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1335 void *ex_resp_arg;
1336 int rc;
1338 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1339 if (!ep) {
1340 atomic_inc(&mp->stats.xid_not_found);
1341 goto out;
1343 if (ep->esb_stat & ESB_ST_COMPLETE) {
1344 atomic_inc(&mp->stats.xid_not_found);
1345 goto rel;
1347 if (ep->rxid == FC_XID_UNKNOWN)
1348 ep->rxid = ntohs(fh->fh_rx_id);
1349 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1350 atomic_inc(&mp->stats.xid_not_found);
1351 goto rel;
1353 if (ep->did != ntoh24(fh->fh_s_id) &&
1354 ep->did != FC_FID_FLOGI) {
1355 atomic_inc(&mp->stats.xid_not_found);
1356 goto rel;
1358 sof = fr_sof(fp);
1359 sp = &ep->seq;
1360 if (fc_sof_is_init(sof)) {
1361 sp->ssb_stat |= SSB_ST_RESP;
1362 sp->id = fh->fh_seq_id;
1363 } else if (sp->id != fh->fh_seq_id) {
1364 atomic_inc(&mp->stats.seq_not_found);
1365 goto rel;
1368 f_ctl = ntoh24(fh->fh_f_ctl);
1369 fr_seq(fp) = sp;
1370 if (f_ctl & FC_FC_SEQ_INIT)
1371 ep->esb_stat |= ESB_ST_SEQ_INIT;
1373 if (fc_sof_needs_ack(sof))
1374 fc_seq_send_ack(sp, fp);
1375 resp = ep->resp;
1376 ex_resp_arg = ep->arg;
1378 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1379 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1380 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1381 spin_lock_bh(&ep->ex_lock);
1382 rc = fc_exch_done_locked(ep);
1383 WARN_ON(fc_seq_exch(sp) != ep);
1384 spin_unlock_bh(&ep->ex_lock);
1385 if (!rc)
1386 fc_exch_delete(ep);
1390 * Call the receive function.
1391 * The sequence is held (has a refcnt) for us,
1392 * but not for the receive function.
1394 * The receive function may allocate a new sequence
1395 * over the old one, so we shouldn't change the
1396 * sequence after this.
1398 * The frame will be freed by the receive function.
1399 * If new exch resp handler is valid then call that
1400 * first.
1402 if (resp)
1403 resp(sp, fp, ex_resp_arg);
1404 else
1405 fc_frame_free(fp);
1406 fc_exch_release(ep);
1407 return;
1408 rel:
1409 fc_exch_release(ep);
1410 out:
1411 fc_frame_free(fp);
1415 * fc_exch_recv_resp() - Handler for a sequence where other end is
1416 * responding to our sequence
1417 * @mp: The EM that the exchange is on
1418 * @fp: The response frame
1420 static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1422 struct fc_seq *sp;
1424 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
1426 if (!sp)
1427 atomic_inc(&mp->stats.xid_not_found);
1428 else
1429 atomic_inc(&mp->stats.non_bls_resp);
1431 fc_frame_free(fp);
1435 * fc_exch_abts_resp() - Handler for a response to an ABT
1436 * @ep: The exchange that the frame is on
1437 * @fp: The response frame
1439 * This response would be to an ABTS cancelling an exchange or sequence.
1440 * The response can be either BA_ACC or BA_RJT
1442 static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1444 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1445 void *ex_resp_arg;
1446 struct fc_frame_header *fh;
1447 struct fc_ba_acc *ap;
1448 struct fc_seq *sp;
1449 u16 low;
1450 u16 high;
1451 int rc = 1, has_rec = 0;
1453 fh = fc_frame_header_get(fp);
1454 FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
1455 fc_exch_rctl_name(fh->fh_r_ctl));
1457 if (cancel_delayed_work_sync(&ep->timeout_work))
1458 fc_exch_release(ep); /* release from pending timer hold */
1460 spin_lock_bh(&ep->ex_lock);
1461 switch (fh->fh_r_ctl) {
1462 case FC_RCTL_BA_ACC:
1463 ap = fc_frame_payload_get(fp, sizeof(*ap));
1464 if (!ap)
1465 break;
1468 * Decide whether to establish a Recovery Qualifier.
1469 * We do this if there is a non-empty SEQ_CNT range and
1470 * SEQ_ID is the same as the one we aborted.
1472 low = ntohs(ap->ba_low_seq_cnt);
1473 high = ntohs(ap->ba_high_seq_cnt);
1474 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1475 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1476 ap->ba_seq_id == ep->seq_id) && low != high) {
1477 ep->esb_stat |= ESB_ST_REC_QUAL;
1478 fc_exch_hold(ep); /* hold for recovery qualifier */
1479 has_rec = 1;
1481 break;
1482 case FC_RCTL_BA_RJT:
1483 break;
1484 default:
1485 break;
1488 resp = ep->resp;
1489 ex_resp_arg = ep->arg;
1491 /* do we need to do some other checks here. Can we reuse more of
1492 * fc_exch_recv_seq_resp
1494 sp = &ep->seq;
1496 * do we want to check END_SEQ as well as LAST_SEQ here?
1498 if (ep->fh_type != FC_TYPE_FCP &&
1499 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1500 rc = fc_exch_done_locked(ep);
1501 spin_unlock_bh(&ep->ex_lock);
1502 if (!rc)
1503 fc_exch_delete(ep);
1505 if (resp)
1506 resp(sp, fp, ex_resp_arg);
1507 else
1508 fc_frame_free(fp);
1510 if (has_rec)
1511 fc_exch_timer_set(ep, ep->r_a_tov);
1516 * fc_exch_recv_bls() - Handler for a BLS sequence
1517 * @mp: The EM that the exchange is on
1518 * @fp: The request frame
1520 * The BLS frame is always a sequence initiated by the remote side.
1521 * We may be either the originator or recipient of the exchange.
1523 static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1525 struct fc_frame_header *fh;
1526 struct fc_exch *ep;
1527 u32 f_ctl;
1529 fh = fc_frame_header_get(fp);
1530 f_ctl = ntoh24(fh->fh_f_ctl);
1531 fr_seq(fp) = NULL;
1533 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1534 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1535 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1536 spin_lock_bh(&ep->ex_lock);
1537 ep->esb_stat |= ESB_ST_SEQ_INIT;
1538 spin_unlock_bh(&ep->ex_lock);
1540 if (f_ctl & FC_FC_SEQ_CTX) {
1542 * A response to a sequence we initiated.
1543 * This should only be ACKs for class 2 or F.
1545 switch (fh->fh_r_ctl) {
1546 case FC_RCTL_ACK_1:
1547 case FC_RCTL_ACK_0:
1548 break;
1549 default:
1550 FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
1551 fh->fh_r_ctl,
1552 fc_exch_rctl_name(fh->fh_r_ctl));
1553 break;
1555 fc_frame_free(fp);
1556 } else {
1557 switch (fh->fh_r_ctl) {
1558 case FC_RCTL_BA_RJT:
1559 case FC_RCTL_BA_ACC:
1560 if (ep)
1561 fc_exch_abts_resp(ep, fp);
1562 else
1563 fc_frame_free(fp);
1564 break;
1565 case FC_RCTL_BA_ABTS:
1566 fc_exch_recv_abts(ep, fp);
1567 break;
1568 default: /* ignore junk */
1569 fc_frame_free(fp);
1570 break;
1573 if (ep)
1574 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1578 * fc_seq_ls_acc() - Accept sequence with LS_ACC
1579 * @rx_fp: The received frame, not freed here.
1581 * If this fails due to allocation or transmit congestion, assume the
1582 * originator will repeat the sequence.
1584 static void fc_seq_ls_acc(struct fc_frame *rx_fp)
1586 struct fc_lport *lport;
1587 struct fc_els_ls_acc *acc;
1588 struct fc_frame *fp;
1590 lport = fr_dev(rx_fp);
1591 fp = fc_frame_alloc(lport, sizeof(*acc));
1592 if (!fp)
1593 return;
1594 acc = fc_frame_payload_get(fp, sizeof(*acc));
1595 memset(acc, 0, sizeof(*acc));
1596 acc->la_cmd = ELS_LS_ACC;
1597 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1598 lport->tt.frame_send(lport, fp);
1602 * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
1603 * @rx_fp: The received frame, not freed here.
1604 * @reason: The reason the sequence is being rejected
1605 * @explan: The explanation for the rejection
1607 * If this fails due to allocation or transmit congestion, assume the
1608 * originator will repeat the sequence.
1610 static void fc_seq_ls_rjt(struct fc_frame *rx_fp, enum fc_els_rjt_reason reason,
1611 enum fc_els_rjt_explan explan)
1613 struct fc_lport *lport;
1614 struct fc_els_ls_rjt *rjt;
1615 struct fc_frame *fp;
1617 lport = fr_dev(rx_fp);
1618 fp = fc_frame_alloc(lport, sizeof(*rjt));
1619 if (!fp)
1620 return;
1621 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1622 memset(rjt, 0, sizeof(*rjt));
1623 rjt->er_cmd = ELS_LS_RJT;
1624 rjt->er_reason = reason;
1625 rjt->er_explan = explan;
1626 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1627 lport->tt.frame_send(lport, fp);
1631 * fc_exch_reset() - Reset an exchange
1632 * @ep: The exchange to be reset
1634 static void fc_exch_reset(struct fc_exch *ep)
1636 struct fc_seq *sp;
1637 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1638 void *arg;
1639 int rc = 1;
1641 spin_lock_bh(&ep->ex_lock);
1642 ep->state |= FC_EX_RST_CLEANUP;
1643 if (cancel_delayed_work(&ep->timeout_work))
1644 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1645 resp = ep->resp;
1646 ep->resp = NULL;
1647 if (ep->esb_stat & ESB_ST_REC_QUAL)
1648 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1649 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1650 arg = ep->arg;
1651 sp = &ep->seq;
1652 rc = fc_exch_done_locked(ep);
1653 spin_unlock_bh(&ep->ex_lock);
1654 if (!rc)
1655 fc_exch_delete(ep);
1657 if (resp)
1658 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1662 * fc_exch_pool_reset() - Reset a per cpu exchange pool
1663 * @lport: The local port that the exchange pool is on
1664 * @pool: The exchange pool to be reset
1665 * @sid: The source ID
1666 * @did: The destination ID
1668 * Resets a per cpu exches pool, releasing all of its sequences
1669 * and exchanges. If sid is non-zero then reset only exchanges
1670 * we sourced from the local port's FID. If did is non-zero then
1671 * only reset exchanges destined for the local port's FID.
1673 static void fc_exch_pool_reset(struct fc_lport *lport,
1674 struct fc_exch_pool *pool,
1675 u32 sid, u32 did)
1677 struct fc_exch *ep;
1678 struct fc_exch *next;
1680 spin_lock_bh(&pool->lock);
1681 restart:
1682 list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
1683 if ((lport == ep->lp) &&
1684 (sid == 0 || sid == ep->sid) &&
1685 (did == 0 || did == ep->did)) {
1686 fc_exch_hold(ep);
1687 spin_unlock_bh(&pool->lock);
1689 fc_exch_reset(ep);
1691 fc_exch_release(ep);
1692 spin_lock_bh(&pool->lock);
1695 * must restart loop incase while lock
1696 * was down multiple eps were released.
1698 goto restart;
1701 spin_unlock_bh(&pool->lock);
1705 * fc_exch_mgr_reset() - Reset all EMs of a local port
1706 * @lport: The local port whose EMs are to be reset
1707 * @sid: The source ID
1708 * @did: The destination ID
1710 * Reset all EMs associated with a given local port. Release all
1711 * sequences and exchanges. If sid is non-zero then reset only the
1712 * exchanges sent from the local port's FID. If did is non-zero then
1713 * reset only exchanges destined for the local port's FID.
1715 void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1717 struct fc_exch_mgr_anchor *ema;
1718 unsigned int cpu;
1720 list_for_each_entry(ema, &lport->ema_list, ema_list) {
1721 for_each_possible_cpu(cpu)
1722 fc_exch_pool_reset(lport,
1723 per_cpu_ptr(ema->mp->pool, cpu),
1724 sid, did);
1727 EXPORT_SYMBOL(fc_exch_mgr_reset);
1730 * fc_exch_lookup() - find an exchange
1731 * @lport: The local port
1732 * @xid: The exchange ID
1734 * Returns exchange pointer with hold for caller, or NULL if not found.
1736 static struct fc_exch *fc_exch_lookup(struct fc_lport *lport, u32 xid)
1738 struct fc_exch_mgr_anchor *ema;
1740 list_for_each_entry(ema, &lport->ema_list, ema_list)
1741 if (ema->mp->min_xid <= xid && xid <= ema->mp->max_xid)
1742 return fc_exch_find(ema->mp, xid);
1743 return NULL;
1747 * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
1748 * @rfp: The REC frame, not freed here.
1750 * Note that the requesting port may be different than the S_ID in the request.
1752 static void fc_exch_els_rec(struct fc_frame *rfp)
1754 struct fc_lport *lport;
1755 struct fc_frame *fp;
1756 struct fc_exch *ep;
1757 struct fc_els_rec *rp;
1758 struct fc_els_rec_acc *acc;
1759 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1760 enum fc_els_rjt_explan explan;
1761 u32 sid;
1762 u16 rxid;
1763 u16 oxid;
1765 lport = fr_dev(rfp);
1766 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1767 explan = ELS_EXPL_INV_LEN;
1768 if (!rp)
1769 goto reject;
1770 sid = ntoh24(rp->rec_s_id);
1771 rxid = ntohs(rp->rec_rx_id);
1772 oxid = ntohs(rp->rec_ox_id);
1774 ep = fc_exch_lookup(lport,
1775 sid == fc_host_port_id(lport->host) ? oxid : rxid);
1776 explan = ELS_EXPL_OXID_RXID;
1777 if (!ep)
1778 goto reject;
1779 if (ep->oid != sid || oxid != ep->oxid)
1780 goto rel;
1781 if (rxid != FC_XID_UNKNOWN && rxid != ep->rxid)
1782 goto rel;
1783 fp = fc_frame_alloc(lport, sizeof(*acc));
1784 if (!fp)
1785 goto out;
1787 acc = fc_frame_payload_get(fp, sizeof(*acc));
1788 memset(acc, 0, sizeof(*acc));
1789 acc->reca_cmd = ELS_LS_ACC;
1790 acc->reca_ox_id = rp->rec_ox_id;
1791 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1792 acc->reca_rx_id = htons(ep->rxid);
1793 if (ep->sid == ep->oid)
1794 hton24(acc->reca_rfid, ep->did);
1795 else
1796 hton24(acc->reca_rfid, ep->sid);
1797 acc->reca_fc4value = htonl(ep->seq.rec_data);
1798 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1799 ESB_ST_SEQ_INIT |
1800 ESB_ST_COMPLETE));
1801 fc_fill_reply_hdr(fp, rfp, FC_RCTL_ELS_REP, 0);
1802 lport->tt.frame_send(lport, fp);
1803 out:
1804 fc_exch_release(ep);
1805 return;
1807 rel:
1808 fc_exch_release(ep);
1809 reject:
1810 fc_seq_ls_rjt(rfp, reason, explan);
1814 * fc_exch_rrq_resp() - Handler for RRQ responses
1815 * @sp: The sequence that the RRQ is on
1816 * @fp: The RRQ frame
1817 * @arg: The exchange that the RRQ is on
1819 * TODO: fix error handler.
1821 static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1823 struct fc_exch *aborted_ep = arg;
1824 unsigned int op;
1826 if (IS_ERR(fp)) {
1827 int err = PTR_ERR(fp);
1829 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
1830 goto cleanup;
1831 FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
1832 "frame error %d\n", err);
1833 return;
1836 op = fc_frame_payload_op(fp);
1837 fc_frame_free(fp);
1839 switch (op) {
1840 case ELS_LS_RJT:
1841 FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
1842 /* fall through */
1843 case ELS_LS_ACC:
1844 goto cleanup;
1845 default:
1846 FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
1847 "for RRQ", op);
1848 return;
1851 cleanup:
1852 fc_exch_done(&aborted_ep->seq);
1853 /* drop hold for rec qual */
1854 fc_exch_release(aborted_ep);
1859 * fc_exch_seq_send() - Send a frame using a new exchange and sequence
1860 * @lport: The local port to send the frame on
1861 * @fp: The frame to be sent
1862 * @resp: The response handler for this request
1863 * @destructor: The destructor for the exchange
1864 * @arg: The argument to be passed to the response handler
1865 * @timer_msec: The timeout period for the exchange
1867 * The frame pointer with some of the header's fields must be
1868 * filled before calling this routine, those fields are:
1870 * - routing control
1871 * - FC port did
1872 * - FC port sid
1873 * - FC header type
1874 * - frame control
1875 * - parameter or relative offset
1877 static struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
1878 struct fc_frame *fp,
1879 void (*resp)(struct fc_seq *,
1880 struct fc_frame *fp,
1881 void *arg),
1882 void (*destructor)(struct fc_seq *,
1883 void *),
1884 void *arg, u32 timer_msec)
1886 struct fc_exch *ep;
1887 struct fc_seq *sp = NULL;
1888 struct fc_frame_header *fh;
1889 int rc = 1;
1891 ep = fc_exch_alloc(lport, fp);
1892 if (!ep) {
1893 fc_frame_free(fp);
1894 return NULL;
1896 ep->esb_stat |= ESB_ST_SEQ_INIT;
1897 fh = fc_frame_header_get(fp);
1898 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1899 ep->resp = resp;
1900 ep->destructor = destructor;
1901 ep->arg = arg;
1902 ep->r_a_tov = FC_DEF_R_A_TOV;
1903 ep->lp = lport;
1904 sp = &ep->seq;
1906 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1907 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1908 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1909 sp->cnt++;
1911 if (ep->xid <= lport->lro_xid && fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD)
1912 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1914 if (unlikely(lport->tt.frame_send(lport, fp)))
1915 goto err;
1917 if (timer_msec)
1918 fc_exch_timer_set_locked(ep, timer_msec);
1919 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1921 if (ep->f_ctl & FC_FC_SEQ_INIT)
1922 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1923 spin_unlock_bh(&ep->ex_lock);
1924 return sp;
1925 err:
1926 rc = fc_exch_done_locked(ep);
1927 spin_unlock_bh(&ep->ex_lock);
1928 if (!rc)
1929 fc_exch_delete(ep);
1930 return NULL;
1934 * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
1935 * @ep: The exchange to send the RRQ on
1937 * This tells the remote port to stop blocking the use of
1938 * the exchange and the seq_cnt range.
1940 static void fc_exch_rrq(struct fc_exch *ep)
1942 struct fc_lport *lport;
1943 struct fc_els_rrq *rrq;
1944 struct fc_frame *fp;
1945 u32 did;
1947 lport = ep->lp;
1949 fp = fc_frame_alloc(lport, sizeof(*rrq));
1950 if (!fp)
1951 goto retry;
1953 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1954 memset(rrq, 0, sizeof(*rrq));
1955 rrq->rrq_cmd = ELS_RRQ;
1956 hton24(rrq->rrq_s_id, ep->sid);
1957 rrq->rrq_ox_id = htons(ep->oxid);
1958 rrq->rrq_rx_id = htons(ep->rxid);
1960 did = ep->did;
1961 if (ep->esb_stat & ESB_ST_RESP)
1962 did = ep->sid;
1964 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1965 lport->port_id, FC_TYPE_ELS,
1966 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1968 if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
1969 lport->e_d_tov))
1970 return;
1972 retry:
1973 spin_lock_bh(&ep->ex_lock);
1974 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1975 spin_unlock_bh(&ep->ex_lock);
1976 /* drop hold for rec qual */
1977 fc_exch_release(ep);
1978 return;
1980 ep->esb_stat |= ESB_ST_REC_QUAL;
1981 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1982 spin_unlock_bh(&ep->ex_lock);
1986 * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
1987 * @fp: The RRQ frame, not freed here.
1989 static void fc_exch_els_rrq(struct fc_frame *fp)
1991 struct fc_lport *lport;
1992 struct fc_exch *ep = NULL; /* request or subject exchange */
1993 struct fc_els_rrq *rp;
1994 u32 sid;
1995 u16 xid;
1996 enum fc_els_rjt_explan explan;
1998 lport = fr_dev(fp);
1999 rp = fc_frame_payload_get(fp, sizeof(*rp));
2000 explan = ELS_EXPL_INV_LEN;
2001 if (!rp)
2002 goto reject;
2005 * lookup subject exchange.
2007 sid = ntoh24(rp->rrq_s_id); /* subject source */
2008 xid = fc_host_port_id(lport->host) == sid ?
2009 ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
2010 ep = fc_exch_lookup(lport, xid);
2011 explan = ELS_EXPL_OXID_RXID;
2012 if (!ep)
2013 goto reject;
2014 spin_lock_bh(&ep->ex_lock);
2015 if (ep->oxid != ntohs(rp->rrq_ox_id))
2016 goto unlock_reject;
2017 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
2018 ep->rxid != FC_XID_UNKNOWN)
2019 goto unlock_reject;
2020 explan = ELS_EXPL_SID;
2021 if (ep->sid != sid)
2022 goto unlock_reject;
2025 * Clear Recovery Qualifier state, and cancel timer if complete.
2027 if (ep->esb_stat & ESB_ST_REC_QUAL) {
2028 ep->esb_stat &= ~ESB_ST_REC_QUAL;
2029 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
2031 if (ep->esb_stat & ESB_ST_COMPLETE) {
2032 if (cancel_delayed_work(&ep->timeout_work))
2033 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
2036 spin_unlock_bh(&ep->ex_lock);
2039 * Send LS_ACC.
2041 fc_seq_ls_acc(fp);
2042 goto out;
2044 unlock_reject:
2045 spin_unlock_bh(&ep->ex_lock);
2046 reject:
2047 fc_seq_ls_rjt(fp, ELS_RJT_LOGIC, explan);
2048 out:
2049 if (ep)
2050 fc_exch_release(ep); /* drop hold from fc_exch_find */
2054 * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
2055 * @lport: The local port to add the exchange manager to
2056 * @mp: The exchange manager to be added to the local port
2057 * @match: The match routine that indicates when this EM should be used
2059 struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
2060 struct fc_exch_mgr *mp,
2061 bool (*match)(struct fc_frame *))
2063 struct fc_exch_mgr_anchor *ema;
2065 ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
2066 if (!ema)
2067 return ema;
2069 ema->mp = mp;
2070 ema->match = match;
2071 /* add EM anchor to EM anchors list */
2072 list_add_tail(&ema->ema_list, &lport->ema_list);
2073 kref_get(&mp->kref);
2074 return ema;
2076 EXPORT_SYMBOL(fc_exch_mgr_add);
2079 * fc_exch_mgr_destroy() - Destroy an exchange manager
2080 * @kref: The reference to the EM to be destroyed
2082 static void fc_exch_mgr_destroy(struct kref *kref)
2084 struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
2086 mempool_destroy(mp->ep_pool);
2087 free_percpu(mp->pool);
2088 kfree(mp);
2092 * fc_exch_mgr_del() - Delete an EM from a local port's list
2093 * @ema: The exchange manager anchor identifying the EM to be deleted
2095 void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
2097 /* remove EM anchor from EM anchors list */
2098 list_del(&ema->ema_list);
2099 kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
2100 kfree(ema);
2102 EXPORT_SYMBOL(fc_exch_mgr_del);
2105 * fc_exch_mgr_list_clone() - Share all exchange manager objects
2106 * @src: Source lport to clone exchange managers from
2107 * @dst: New lport that takes references to all the exchange managers
2109 int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
2111 struct fc_exch_mgr_anchor *ema, *tmp;
2113 list_for_each_entry(ema, &src->ema_list, ema_list) {
2114 if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
2115 goto err;
2117 return 0;
2118 err:
2119 list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
2120 fc_exch_mgr_del(ema);
2121 return -ENOMEM;
2125 * fc_exch_mgr_alloc() - Allocate an exchange manager
2126 * @lport: The local port that the new EM will be associated with
2127 * @class: The default FC class for new exchanges
2128 * @min_xid: The minimum XID for exchanges from the new EM
2129 * @max_xid: The maximum XID for exchanges from the new EM
2130 * @match: The match routine for the new EM
2132 struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
2133 enum fc_class class,
2134 u16 min_xid, u16 max_xid,
2135 bool (*match)(struct fc_frame *))
2137 struct fc_exch_mgr *mp;
2138 u16 pool_exch_range;
2139 size_t pool_size;
2140 unsigned int cpu;
2141 struct fc_exch_pool *pool;
2143 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
2144 (min_xid & fc_cpu_mask) != 0) {
2145 FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
2146 min_xid, max_xid);
2147 return NULL;
2151 * allocate memory for EM
2153 mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
2154 if (!mp)
2155 return NULL;
2157 mp->class = class;
2158 /* adjust em exch xid range for offload */
2159 mp->min_xid = min_xid;
2160 mp->max_xid = max_xid;
2162 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
2163 if (!mp->ep_pool)
2164 goto free_mp;
2167 * Setup per cpu exch pool with entire exchange id range equally
2168 * divided across all cpus. The exch pointers array memory is
2169 * allocated for exch range per pool.
2171 pool_exch_range = (mp->max_xid - mp->min_xid + 1) / (fc_cpu_mask + 1);
2172 mp->pool_max_index = pool_exch_range - 1;
2175 * Allocate and initialize per cpu exch pool
2177 pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
2178 mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
2179 if (!mp->pool)
2180 goto free_mempool;
2181 for_each_possible_cpu(cpu) {
2182 pool = per_cpu_ptr(mp->pool, cpu);
2183 spin_lock_init(&pool->lock);
2184 INIT_LIST_HEAD(&pool->ex_list);
2187 kref_init(&mp->kref);
2188 if (!fc_exch_mgr_add(lport, mp, match)) {
2189 free_percpu(mp->pool);
2190 goto free_mempool;
2194 * Above kref_init() sets mp->kref to 1 and then
2195 * call to fc_exch_mgr_add incremented mp->kref again,
2196 * so adjust that extra increment.
2198 kref_put(&mp->kref, fc_exch_mgr_destroy);
2199 return mp;
2201 free_mempool:
2202 mempool_destroy(mp->ep_pool);
2203 free_mp:
2204 kfree(mp);
2205 return NULL;
2207 EXPORT_SYMBOL(fc_exch_mgr_alloc);
2210 * fc_exch_mgr_free() - Free all exchange managers on a local port
2211 * @lport: The local port whose EMs are to be freed
2213 void fc_exch_mgr_free(struct fc_lport *lport)
2215 struct fc_exch_mgr_anchor *ema, *next;
2217 flush_workqueue(fc_exch_workqueue);
2218 list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
2219 fc_exch_mgr_del(ema);
2221 EXPORT_SYMBOL(fc_exch_mgr_free);
2224 * fc_exch_recv() - Handler for received frames
2225 * @lport: The local port the frame was received on
2226 * @fp: The received frame
2228 void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
2230 struct fc_frame_header *fh = fc_frame_header_get(fp);
2231 struct fc_exch_mgr_anchor *ema;
2232 u32 f_ctl, found = 0;
2233 u16 oxid;
2235 /* lport lock ? */
2236 if (!lport || lport->state == LPORT_ST_DISABLED) {
2237 FC_LPORT_DBG(lport, "Receiving frames for an lport that "
2238 "has not been initialized correctly\n");
2239 fc_frame_free(fp);
2240 return;
2243 f_ctl = ntoh24(fh->fh_f_ctl);
2244 oxid = ntohs(fh->fh_ox_id);
2245 if (f_ctl & FC_FC_EX_CTX) {
2246 list_for_each_entry(ema, &lport->ema_list, ema_list) {
2247 if ((oxid >= ema->mp->min_xid) &&
2248 (oxid <= ema->mp->max_xid)) {
2249 found = 1;
2250 break;
2254 if (!found) {
2255 FC_LPORT_DBG(lport, "Received response for out "
2256 "of range oxid:%hx\n", oxid);
2257 fc_frame_free(fp);
2258 return;
2260 } else
2261 ema = list_entry(lport->ema_list.prev, typeof(*ema), ema_list);
2264 * If frame is marked invalid, just drop it.
2266 switch (fr_eof(fp)) {
2267 case FC_EOF_T:
2268 if (f_ctl & FC_FC_END_SEQ)
2269 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
2270 /* fall through */
2271 case FC_EOF_N:
2272 if (fh->fh_type == FC_TYPE_BLS)
2273 fc_exch_recv_bls(ema->mp, fp);
2274 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
2275 FC_FC_EX_CTX)
2276 fc_exch_recv_seq_resp(ema->mp, fp);
2277 else if (f_ctl & FC_FC_SEQ_CTX)
2278 fc_exch_recv_resp(ema->mp, fp);
2279 else /* no EX_CTX and no SEQ_CTX */
2280 fc_exch_recv_req(lport, ema->mp, fp);
2281 break;
2282 default:
2283 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
2284 fr_eof(fp));
2285 fc_frame_free(fp);
2288 EXPORT_SYMBOL(fc_exch_recv);
2291 * fc_exch_init() - Initialize the exchange layer for a local port
2292 * @lport: The local port to initialize the exchange layer for
2294 int fc_exch_init(struct fc_lport *lport)
2296 if (!lport->tt.seq_start_next)
2297 lport->tt.seq_start_next = fc_seq_start_next;
2299 if (!lport->tt.exch_seq_send)
2300 lport->tt.exch_seq_send = fc_exch_seq_send;
2302 if (!lport->tt.seq_send)
2303 lport->tt.seq_send = fc_seq_send;
2305 if (!lport->tt.seq_els_rsp_send)
2306 lport->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
2308 if (!lport->tt.exch_done)
2309 lport->tt.exch_done = fc_exch_done;
2311 if (!lport->tt.exch_mgr_reset)
2312 lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
2314 if (!lport->tt.seq_exch_abort)
2315 lport->tt.seq_exch_abort = fc_seq_exch_abort;
2317 if (!lport->tt.seq_assign)
2318 lport->tt.seq_assign = fc_seq_assign;
2320 return 0;
2322 EXPORT_SYMBOL(fc_exch_init);
2325 * fc_setup_exch_mgr() - Setup an exchange manager
2327 int fc_setup_exch_mgr()
2329 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
2330 0, SLAB_HWCACHE_ALIGN, NULL);
2331 if (!fc_em_cachep)
2332 return -ENOMEM;
2335 * Initialize fc_cpu_mask and fc_cpu_order. The
2336 * fc_cpu_mask is set for nr_cpu_ids rounded up
2337 * to order of 2's * power and order is stored
2338 * in fc_cpu_order as this is later required in
2339 * mapping between an exch id and exch array index
2340 * in per cpu exch pool.
2342 * This round up is required to align fc_cpu_mask
2343 * to exchange id's lower bits such that all incoming
2344 * frames of an exchange gets delivered to the same
2345 * cpu on which exchange originated by simple bitwise
2346 * AND operation between fc_cpu_mask and exchange id.
2348 fc_cpu_mask = 1;
2349 fc_cpu_order = 0;
2350 while (fc_cpu_mask < nr_cpu_ids) {
2351 fc_cpu_mask <<= 1;
2352 fc_cpu_order++;
2354 fc_cpu_mask--;
2356 fc_exch_workqueue = create_singlethread_workqueue("fc_exch_workqueue");
2357 if (!fc_exch_workqueue)
2358 return -ENOMEM;
2359 return 0;
2363 * fc_destroy_exch_mgr() - Destroy an exchange manager
2365 void fc_destroy_exch_mgr()
2367 destroy_workqueue(fc_exch_workqueue);
2368 kmem_cache_destroy(fc_em_cachep);