blob: 7af9bceb8aa9efa467c3ca58ed4da1694ec7d79c [file] [log] [blame]
Robert Love42e9a922008-12-09 15:10:17 -08001/*
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
5 *
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.
9 *
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.
14 *
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.
18 *
19 * Maintained at www.Open-FCoE.org
20 */
21
22/*
23 * Fibre Channel exchange and sequence handling.
24 */
25
26#include <linux/timer.h>
27#include <linux/gfp.h>
28#include <linux/err.h>
29
30#include <scsi/fc/fc_fc2.h>
31
32#include <scsi/libfc.h>
33#include <scsi/fc_encode.h>
34
Robert Love42e9a922008-12-09 15:10:17 -080035/*
36 * fc_exch_debug can be set in debugger or at compile time to get more logs.
37 */
38static int fc_exch_debug;
39
40#define FC_DEBUG_EXCH(fmt...) \
41 do { \
42 if (fc_exch_debug) \
43 FC_DBG(fmt); \
44 } while (0)
45
46static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
47
48/*
49 * Structure and function definitions for managing Fibre Channel Exchanges
50 * and Sequences.
51 *
52 * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
53 *
54 * fc_exch_mgr holds the exchange state for an N port
55 *
56 * fc_exch holds state for one exchange and links to its active sequence.
57 *
58 * fc_seq holds the state for an individual sequence.
59 */
60
61/*
62 * Exchange manager.
63 *
64 * This structure is the center for creating exchanges and sequences.
65 * It manages the allocation of exchange IDs.
66 */
67struct fc_exch_mgr {
68 enum fc_class class; /* default class for sequences */
69 spinlock_t em_lock; /* exchange manager lock,
70 must be taken before ex_lock */
71 u16 last_xid; /* last allocated exchange ID */
72 u16 min_xid; /* min exchange ID */
73 u16 max_xid; /* max exchange ID */
74 u16 max_read; /* max exchange ID for read */
75 u16 last_read; /* last xid allocated for read */
76 u32 total_exches; /* total allocated exchanges */
77 struct list_head ex_list; /* allocated exchanges list */
78 struct fc_lport *lp; /* fc device instance */
79 mempool_t *ep_pool; /* reserve ep's */
80
81 /*
82 * currently exchange mgr stats are updated but not used.
83 * either stats can be expose via sysfs or remove them
84 * all together if not used XXX
85 */
86 struct {
87 atomic_t no_free_exch;
88 atomic_t no_free_exch_xid;
89 atomic_t xid_not_found;
90 atomic_t xid_busy;
91 atomic_t seq_not_found;
92 atomic_t non_bls_resp;
93 } stats;
94 struct fc_exch **exches; /* for exch pointers indexed by xid */
95};
96#define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
97
98static void fc_exch_rrq(struct fc_exch *);
99static void fc_seq_ls_acc(struct fc_seq *);
100static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason,
101 enum fc_els_rjt_explan);
102static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *);
103static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *);
104static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp);
105
106/*
107 * Internal implementation notes.
108 *
109 * The exchange manager is one by default in libfc but LLD may choose
110 * to have one per CPU. The sequence manager is one per exchange manager
111 * and currently never separated.
112 *
113 * Section 9.8 in FC-FS-2 specifies: "The SEQ_ID is a one-byte field
114 * assigned by the Sequence Initiator that shall be unique for a specific
115 * D_ID and S_ID pair while the Sequence is open." Note that it isn't
116 * qualified by exchange ID, which one might think it would be.
117 * In practice this limits the number of open sequences and exchanges to 256
118 * per session. For most targets we could treat this limit as per exchange.
119 *
120 * The exchange and its sequence are freed when the last sequence is received.
121 * It's possible for the remote port to leave an exchange open without
122 * sending any sequences.
123 *
124 * Notes on reference counts:
125 *
126 * Exchanges are reference counted and exchange gets freed when the reference
127 * count becomes zero.
128 *
129 * Timeouts:
130 * Sequences are timed out for E_D_TOV and R_A_TOV.
131 *
132 * Sequence event handling:
133 *
134 * The following events may occur on initiator sequences:
135 *
136 * Send.
137 * For now, the whole thing is sent.
138 * Receive ACK
139 * This applies only to class F.
140 * The sequence is marked complete.
141 * ULP completion.
142 * The upper layer calls fc_exch_done() when done
143 * with exchange and sequence tuple.
144 * RX-inferred completion.
145 * When we receive the next sequence on the same exchange, we can
146 * retire the previous sequence ID. (XXX not implemented).
147 * Timeout.
148 * R_A_TOV frees the sequence ID. If we're waiting for ACK,
149 * E_D_TOV causes abort and calls upper layer response handler
150 * with FC_EX_TIMEOUT error.
151 * Receive RJT
152 * XXX defer.
153 * Send ABTS
154 * On timeout.
155 *
156 * The following events may occur on recipient sequences:
157 *
158 * Receive
159 * Allocate sequence for first frame received.
160 * Hold during receive handler.
161 * Release when final frame received.
162 * Keep status of last N of these for the ELS RES command. XXX TBD.
163 * Receive ABTS
164 * Deallocate sequence
165 * Send RJT
166 * Deallocate
167 *
168 * For now, we neglect conditions where only part of a sequence was
169 * received or transmitted, or where out-of-order receipt is detected.
170 */
171
172/*
173 * Locking notes:
174 *
175 * The EM code run in a per-CPU worker thread.
176 *
177 * To protect against concurrency between a worker thread code and timers,
178 * sequence allocation and deallocation must be locked.
179 * - exchange refcnt can be done atomicly without locks.
180 * - sequence allocation must be locked by exch lock.
181 * - If the em_lock and ex_lock must be taken at the same time, then the
182 * em_lock must be taken before the ex_lock.
183 */
184
185/*
186 * opcode names for debugging.
187 */
188static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
189
190#define FC_TABLE_SIZE(x) (sizeof(x) / sizeof(x[0]))
191
192static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
193 unsigned int max_index)
194{
195 const char *name = NULL;
196
197 if (op < max_index)
198 name = table[op];
199 if (!name)
200 name = "unknown";
201 return name;
202}
203
204static const char *fc_exch_rctl_name(unsigned int op)
205{
206 return fc_exch_name_lookup(op, fc_exch_rctl_names,
207 FC_TABLE_SIZE(fc_exch_rctl_names));
208}
209
210/*
211 * Hold an exchange - keep it from being freed.
212 */
213static void fc_exch_hold(struct fc_exch *ep)
214{
215 atomic_inc(&ep->ex_refcnt);
216}
217
218/*
219 * setup fc hdr by initializing few more FC header fields and sof/eof.
220 * Initialized fields by this func:
221 * - fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt
222 * - sof and eof
223 */
224static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
225 u32 f_ctl)
226{
227 struct fc_frame_header *fh = fc_frame_header_get(fp);
228 u16 fill;
229
230 fr_sof(fp) = ep->class;
231 if (ep->seq.cnt)
232 fr_sof(fp) = fc_sof_normal(ep->class);
233
234 if (f_ctl & FC_FC_END_SEQ) {
235 fr_eof(fp) = FC_EOF_T;
236 if (fc_sof_needs_ack(ep->class))
237 fr_eof(fp) = FC_EOF_N;
238 /*
239 * Form f_ctl.
240 * The number of fill bytes to make the length a 4-byte
241 * multiple is the low order 2-bits of the f_ctl.
242 * The fill itself will have been cleared by the frame
243 * allocation.
244 * After this, the length will be even, as expected by
245 * the transport.
246 */
247 fill = fr_len(fp) & 3;
248 if (fill) {
249 fill = 4 - fill;
250 /* TODO, this may be a problem with fragmented skb */
251 skb_put(fp_skb(fp), fill);
252 hton24(fh->fh_f_ctl, f_ctl | fill);
253 }
254 } else {
255 WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
256 fr_eof(fp) = FC_EOF_N;
257 }
258
259 /*
260 * Initialize remainig fh fields
261 * from fc_fill_fc_hdr
262 */
263 fh->fh_ox_id = htons(ep->oxid);
264 fh->fh_rx_id = htons(ep->rxid);
265 fh->fh_seq_id = ep->seq.id;
266 fh->fh_seq_cnt = htons(ep->seq.cnt);
267}
268
269
270/*
271 * Release a reference to an exchange.
272 * If the refcnt goes to zero and the exchange is complete, it is freed.
273 */
274static void fc_exch_release(struct fc_exch *ep)
275{
276 struct fc_exch_mgr *mp;
277
278 if (atomic_dec_and_test(&ep->ex_refcnt)) {
279 mp = ep->em;
280 if (ep->destructor)
281 ep->destructor(&ep->seq, ep->arg);
282 if (ep->lp->tt.exch_put)
283 ep->lp->tt.exch_put(ep->lp, mp, ep->xid);
Julia Lawallaa6cd292009-02-04 22:17:29 +0100284 WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
Robert Love42e9a922008-12-09 15:10:17 -0800285 mempool_free(ep, mp->ep_pool);
286 }
287}
288
289static int fc_exch_done_locked(struct fc_exch *ep)
290{
291 int rc = 1;
292
293 /*
294 * We must check for completion in case there are two threads
295 * tyring to complete this. But the rrq code will reuse the
296 * ep, and in that case we only clear the resp and set it as
297 * complete, so it can be reused by the timer to send the rrq.
298 */
299 ep->resp = NULL;
300 if (ep->state & FC_EX_DONE)
301 return rc;
302 ep->esb_stat |= ESB_ST_COMPLETE;
303
304 if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
305 ep->state |= FC_EX_DONE;
306 if (cancel_delayed_work(&ep->timeout_work))
307 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
308 rc = 0;
309 }
310 return rc;
311}
312
313static void fc_exch_mgr_delete_ep(struct fc_exch *ep)
314{
315 struct fc_exch_mgr *mp;
316
317 mp = ep->em;
318 spin_lock_bh(&mp->em_lock);
319 WARN_ON(mp->total_exches <= 0);
320 mp->total_exches--;
321 mp->exches[ep->xid - mp->min_xid] = NULL;
322 list_del(&ep->ex_list);
323 spin_unlock_bh(&mp->em_lock);
324 fc_exch_release(ep); /* drop hold for exch in mp */
325}
326
327/*
328 * Internal version of fc_exch_timer_set - used with lock held.
329 */
330static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
331 unsigned int timer_msec)
332{
333 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
334 return;
335
336 FC_DEBUG_EXCH("Exchange (%4x) timed out, notifying the upper layer\n",
337 ep->xid);
338 if (schedule_delayed_work(&ep->timeout_work,
339 msecs_to_jiffies(timer_msec)))
340 fc_exch_hold(ep); /* hold for timer */
341}
342
343/*
344 * Set timer for an exchange.
345 * The time is a minimum delay in milliseconds until the timer fires.
346 * Used for upper level protocols to time out the exchange.
347 * The timer is cancelled when it fires or when the exchange completes.
348 * Returns non-zero if a timer couldn't be allocated.
349 */
350static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
351{
352 spin_lock_bh(&ep->ex_lock);
353 fc_exch_timer_set_locked(ep, timer_msec);
354 spin_unlock_bh(&ep->ex_lock);
355}
356
357int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec)
358{
359 struct fc_seq *sp;
360 struct fc_exch *ep;
361 struct fc_frame *fp;
362 int error;
363
364 ep = fc_seq_exch(req_sp);
365
366 spin_lock_bh(&ep->ex_lock);
367 if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
368 ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
369 spin_unlock_bh(&ep->ex_lock);
370 return -ENXIO;
371 }
372
373 /*
374 * Send the abort on a new sequence if possible.
375 */
376 sp = fc_seq_start_next_locked(&ep->seq);
377 if (!sp) {
378 spin_unlock_bh(&ep->ex_lock);
379 return -ENOMEM;
380 }
381
382 ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
383 if (timer_msec)
384 fc_exch_timer_set_locked(ep, timer_msec);
385 spin_unlock_bh(&ep->ex_lock);
386
387 /*
388 * If not logged into the fabric, don't send ABTS but leave
389 * sequence active until next timeout.
390 */
391 if (!ep->sid)
392 return 0;
393
394 /*
395 * Send an abort for the sequence that timed out.
396 */
397 fp = fc_frame_alloc(ep->lp, 0);
398 if (fp) {
399 fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
400 FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
401 error = fc_seq_send(ep->lp, sp, fp);
402 } else
403 error = -ENOBUFS;
404 return error;
405}
406EXPORT_SYMBOL(fc_seq_exch_abort);
407
408/*
409 * Exchange timeout - handle exchange timer expiration.
410 * The timer will have been cancelled before this is called.
411 */
412static void fc_exch_timeout(struct work_struct *work)
413{
414 struct fc_exch *ep = container_of(work, struct fc_exch,
415 timeout_work.work);
416 struct fc_seq *sp = &ep->seq;
417 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
418 void *arg;
419 u32 e_stat;
420 int rc = 1;
421
422 spin_lock_bh(&ep->ex_lock);
423 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
424 goto unlock;
425
426 e_stat = ep->esb_stat;
427 if (e_stat & ESB_ST_COMPLETE) {
428 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
429 if (e_stat & ESB_ST_REC_QUAL)
430 fc_exch_rrq(ep);
431 spin_unlock_bh(&ep->ex_lock);
432 goto done;
433 } else {
434 resp = ep->resp;
435 arg = ep->arg;
436 ep->resp = NULL;
437 if (e_stat & ESB_ST_ABNORMAL)
438 rc = fc_exch_done_locked(ep);
439 spin_unlock_bh(&ep->ex_lock);
440 if (!rc)
441 fc_exch_mgr_delete_ep(ep);
442 if (resp)
443 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
444 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
445 goto done;
446 }
447unlock:
448 spin_unlock_bh(&ep->ex_lock);
449done:
450 /*
451 * This release matches the hold taken when the timer was set.
452 */
453 fc_exch_release(ep);
454}
455
456/*
457 * Allocate a sequence.
458 *
459 * We don't support multiple originated sequences on the same exchange.
460 * By implication, any previously originated sequence on this exchange
461 * is complete, and we reallocate the same sequence.
462 */
463static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
464{
465 struct fc_seq *sp;
466
467 sp = &ep->seq;
468 sp->ssb_stat = 0;
469 sp->cnt = 0;
470 sp->id = seq_id;
471 return sp;
472}
473
474/*
475 * fc_em_alloc_xid - returns an xid based on request type
476 * @lp : ptr to associated lport
477 * @fp : ptr to the assocated frame
478 *
479 * check the associated fc_fsp_pkt to get scsi command type and
480 * command direction to decide from which range this exch id
481 * will be allocated from.
482 *
483 * Returns : 0 or an valid xid
484 */
485static u16 fc_em_alloc_xid(struct fc_exch_mgr *mp, const struct fc_frame *fp)
486{
487 u16 xid, min, max;
488 u16 *plast;
489 struct fc_exch *ep = NULL;
490
491 if (mp->max_read) {
Yi Zoub277d2a2009-02-27 14:07:21 -0800492 if (fc_fcp_is_read(fr_fsp(fp))) {
Robert Love42e9a922008-12-09 15:10:17 -0800493 min = mp->min_xid;
494 max = mp->max_read;
495 plast = &mp->last_read;
496 } else {
497 min = mp->max_read + 1;
498 max = mp->max_xid;
499 plast = &mp->last_xid;
500 }
501 } else {
502 min = mp->min_xid;
503 max = mp->max_xid;
504 plast = &mp->last_xid;
505 }
506 xid = *plast;
507 do {
508 xid = (xid == max) ? min : xid + 1;
509 ep = mp->exches[xid - mp->min_xid];
510 } while ((ep != NULL) && (xid != *plast));
511
512 if (unlikely(ep))
513 xid = 0;
514 else
515 *plast = xid;
516
517 return xid;
518}
519
520/*
521 * fc_exch_alloc - allocate an exchange.
522 * @mp : ptr to the exchange manager
523 * @xid: input xid
524 *
525 * if xid is supplied zero then assign next free exchange ID
526 * from exchange manager, otherwise use supplied xid.
527 * Returns with exch lock held.
528 */
529struct fc_exch *fc_exch_alloc(struct fc_exch_mgr *mp,
530 struct fc_frame *fp, u16 xid)
531{
532 struct fc_exch *ep;
533
534 /* allocate memory for exchange */
535 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
536 if (!ep) {
537 atomic_inc(&mp->stats.no_free_exch);
538 goto out;
539 }
540 memset(ep, 0, sizeof(*ep));
541
542 spin_lock_bh(&mp->em_lock);
543 /* alloc xid if input xid 0 */
544 if (!xid) {
545 /* alloc a new xid */
546 xid = fc_em_alloc_xid(mp, fp);
547 if (!xid) {
548 printk(KERN_ERR "fc_em_alloc_xid() failed\n");
549 goto err;
550 }
551 }
552
553 fc_exch_hold(ep); /* hold for exch in mp */
554 spin_lock_init(&ep->ex_lock);
555 /*
556 * Hold exch lock for caller to prevent fc_exch_reset()
557 * from releasing exch while fc_exch_alloc() caller is
558 * still working on exch.
559 */
560 spin_lock_bh(&ep->ex_lock);
561
562 mp->exches[xid - mp->min_xid] = ep;
563 list_add_tail(&ep->ex_list, &mp->ex_list);
564 fc_seq_alloc(ep, ep->seq_id++);
565 mp->total_exches++;
566 spin_unlock_bh(&mp->em_lock);
567
568 /*
569 * update exchange
570 */
571 ep->oxid = ep->xid = xid;
572 ep->em = mp;
573 ep->lp = mp->lp;
574 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
575 ep->rxid = FC_XID_UNKNOWN;
576 ep->class = mp->class;
577 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
578out:
579 return ep;
580err:
581 spin_unlock_bh(&mp->em_lock);
582 atomic_inc(&mp->stats.no_free_exch_xid);
583 mempool_free(ep, mp->ep_pool);
584 return NULL;
585}
586EXPORT_SYMBOL(fc_exch_alloc);
587
588/*
589 * Lookup and hold an exchange.
590 */
591static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
592{
593 struct fc_exch *ep = NULL;
594
595 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
596 spin_lock_bh(&mp->em_lock);
597 ep = mp->exches[xid - mp->min_xid];
598 if (ep) {
599 fc_exch_hold(ep);
600 WARN_ON(ep->xid != xid);
601 }
602 spin_unlock_bh(&mp->em_lock);
603 }
604 return ep;
605}
606
607void fc_exch_done(struct fc_seq *sp)
608{
609 struct fc_exch *ep = fc_seq_exch(sp);
610 int rc;
611
612 spin_lock_bh(&ep->ex_lock);
613 rc = fc_exch_done_locked(ep);
614 spin_unlock_bh(&ep->ex_lock);
615 if (!rc)
616 fc_exch_mgr_delete_ep(ep);
617}
618EXPORT_SYMBOL(fc_exch_done);
619
620/*
621 * Allocate a new exchange as responder.
622 * Sets the responder ID in the frame header.
623 */
624static struct fc_exch *fc_exch_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
625{
626 struct fc_exch *ep;
627 struct fc_frame_header *fh;
Robert Love42e9a922008-12-09 15:10:17 -0800628
629 ep = mp->lp->tt.exch_get(mp->lp, fp);
630 if (ep) {
631 ep->class = fc_frame_class(fp);
632
633 /*
634 * Set EX_CTX indicating we're responding on this exchange.
635 */
636 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
637 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
638 fh = fc_frame_header_get(fp);
639 ep->sid = ntoh24(fh->fh_d_id);
640 ep->did = ntoh24(fh->fh_s_id);
641 ep->oid = ep->did;
642
643 /*
644 * Allocated exchange has placed the XID in the
645 * originator field. Move it to the responder field,
646 * and set the originator XID from the frame.
647 */
648 ep->rxid = ep->xid;
649 ep->oxid = ntohs(fh->fh_ox_id);
650 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
651 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
652 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
653
Robert Love42e9a922008-12-09 15:10:17 -0800654 fc_exch_hold(ep); /* hold for caller */
655 spin_unlock_bh(&ep->ex_lock); /* lock from exch_get */
656 }
657 return ep;
658}
659
660/*
661 * Find a sequence for receive where the other end is originating the sequence.
662 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
663 * on the ep that should be released by the caller.
664 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800665static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_exch_mgr *mp,
666 struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800667{
668 struct fc_frame_header *fh = fc_frame_header_get(fp);
669 struct fc_exch *ep = NULL;
670 struct fc_seq *sp = NULL;
671 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
672 u32 f_ctl;
673 u16 xid;
674
675 f_ctl = ntoh24(fh->fh_f_ctl);
676 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
677
678 /*
679 * Lookup or create the exchange if we will be creating the sequence.
680 */
681 if (f_ctl & FC_FC_EX_CTX) {
682 xid = ntohs(fh->fh_ox_id); /* we originated exch */
683 ep = fc_exch_find(mp, xid);
684 if (!ep) {
685 atomic_inc(&mp->stats.xid_not_found);
686 reject = FC_RJT_OX_ID;
687 goto out;
688 }
689 if (ep->rxid == FC_XID_UNKNOWN)
690 ep->rxid = ntohs(fh->fh_rx_id);
691 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
692 reject = FC_RJT_OX_ID;
693 goto rel;
694 }
695 } else {
696 xid = ntohs(fh->fh_rx_id); /* we are the responder */
697
698 /*
699 * Special case for MDS issuing an ELS TEST with a
700 * bad rxid of 0.
701 * XXX take this out once we do the proper reject.
702 */
703 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
704 fc_frame_payload_op(fp) == ELS_TEST) {
705 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
706 xid = FC_XID_UNKNOWN;
707 }
708
709 /*
710 * new sequence - find the exchange
711 */
712 ep = fc_exch_find(mp, xid);
713 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
714 if (ep) {
715 atomic_inc(&mp->stats.xid_busy);
716 reject = FC_RJT_RX_ID;
717 goto rel;
718 }
719 ep = fc_exch_resp(mp, fp);
720 if (!ep) {
721 reject = FC_RJT_EXCH_EST; /* XXX */
722 goto out;
723 }
724 xid = ep->xid; /* get our XID */
725 } else if (!ep) {
726 atomic_inc(&mp->stats.xid_not_found);
727 reject = FC_RJT_RX_ID; /* XID not found */
728 goto out;
729 }
730 }
731
732 /*
733 * At this point, we have the exchange held.
734 * Find or create the sequence.
735 */
736 if (fc_sof_is_init(fr_sof(fp))) {
737 sp = fc_seq_start_next(&ep->seq);
738 if (!sp) {
739 reject = FC_RJT_SEQ_XS; /* exchange shortage */
740 goto rel;
741 }
742 sp->id = fh->fh_seq_id;
743 sp->ssb_stat |= SSB_ST_RESP;
744 } else {
745 sp = &ep->seq;
746 if (sp->id != fh->fh_seq_id) {
747 atomic_inc(&mp->stats.seq_not_found);
748 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
749 goto rel;
750 }
751 }
752 WARN_ON(ep != fc_seq_exch(sp));
753
754 if (f_ctl & FC_FC_SEQ_INIT)
755 ep->esb_stat |= ESB_ST_SEQ_INIT;
756
757 fr_seq(fp) = sp;
758out:
759 return reject;
760rel:
761 fc_exch_done(&ep->seq);
762 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
763 return reject;
764}
765
766/*
767 * Find the sequence for a frame being received.
768 * We originated the sequence, so it should be found.
769 * We may or may not have originated the exchange.
770 * Does not hold the sequence for the caller.
771 */
772static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
773 struct fc_frame *fp)
774{
775 struct fc_frame_header *fh = fc_frame_header_get(fp);
776 struct fc_exch *ep;
777 struct fc_seq *sp = NULL;
778 u32 f_ctl;
779 u16 xid;
780
781 f_ctl = ntoh24(fh->fh_f_ctl);
782 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
783 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
784 ep = fc_exch_find(mp, xid);
785 if (!ep)
786 return NULL;
787 if (ep->seq.id == fh->fh_seq_id) {
788 /*
789 * Save the RX_ID if we didn't previously know it.
790 */
791 sp = &ep->seq;
792 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
793 ep->rxid == FC_XID_UNKNOWN) {
794 ep->rxid = ntohs(fh->fh_rx_id);
795 }
796 }
797 fc_exch_release(ep);
798 return sp;
799}
800
801/*
802 * Set addresses for an exchange.
803 * Note this must be done before the first sequence of the exchange is sent.
804 */
805static void fc_exch_set_addr(struct fc_exch *ep,
806 u32 orig_id, u32 resp_id)
807{
808 ep->oid = orig_id;
809 if (ep->esb_stat & ESB_ST_RESP) {
810 ep->sid = resp_id;
811 ep->did = orig_id;
812 } else {
813 ep->sid = orig_id;
814 ep->did = resp_id;
815 }
816}
817
818static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
819{
820 struct fc_exch *ep = fc_seq_exch(sp);
821
822 sp = fc_seq_alloc(ep, ep->seq_id++);
823 FC_DEBUG_EXCH("exch %4x f_ctl %6x seq %2x\n",
824 ep->xid, ep->f_ctl, sp->id);
825 return sp;
826}
827/*
828 * Allocate a new sequence on the same exchange as the supplied sequence.
829 * This will never return NULL.
830 */
831struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
832{
833 struct fc_exch *ep = fc_seq_exch(sp);
834
835 spin_lock_bh(&ep->ex_lock);
836 WARN_ON((ep->esb_stat & ESB_ST_COMPLETE) != 0);
837 sp = fc_seq_start_next_locked(sp);
838 spin_unlock_bh(&ep->ex_lock);
839
840 return sp;
841}
842EXPORT_SYMBOL(fc_seq_start_next);
843
844int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
845{
846 struct fc_exch *ep;
847 struct fc_frame_header *fh = fc_frame_header_get(fp);
848 int error;
849 u32 f_ctl;
850
851 ep = fc_seq_exch(sp);
852 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
853
854 f_ctl = ntoh24(fh->fh_f_ctl);
855 fc_exch_setup_hdr(ep, fp, f_ctl);
856
857 /*
858 * update sequence count if this frame is carrying
859 * multiple FC frames when sequence offload is enabled
860 * by LLD.
861 */
862 if (fr_max_payload(fp))
863 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
864 fr_max_payload(fp));
865 else
866 sp->cnt++;
867
868 /*
869 * Send the frame.
870 */
871 error = lp->tt.frame_send(lp, fp);
872
873 /*
874 * Update the exchange and sequence flags,
875 * assuming all frames for the sequence have been sent.
876 * We can only be called to send once for each sequence.
877 */
878 spin_lock_bh(&ep->ex_lock);
879 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
880 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
881 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
882 spin_unlock_bh(&ep->ex_lock);
883 return error;
884}
885EXPORT_SYMBOL(fc_seq_send);
886
887void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
888 struct fc_seq_els_data *els_data)
889{
890 switch (els_cmd) {
891 case ELS_LS_RJT:
892 fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
893 break;
894 case ELS_LS_ACC:
895 fc_seq_ls_acc(sp);
896 break;
897 case ELS_RRQ:
898 fc_exch_els_rrq(sp, els_data->fp);
899 break;
900 case ELS_REC:
901 fc_exch_els_rec(sp, els_data->fp);
902 break;
903 default:
904 FC_DBG("Invalid ELS CMD:%x\n", els_cmd);
905 }
906}
907EXPORT_SYMBOL(fc_seq_els_rsp_send);
908
909/*
910 * Send a sequence, which is also the last sequence in the exchange.
911 */
912static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
913 enum fc_rctl rctl, enum fc_fh_type fh_type)
914{
915 u32 f_ctl;
916 struct fc_exch *ep = fc_seq_exch(sp);
917
918 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
919 f_ctl |= ep->f_ctl;
920 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
921 fc_seq_send(ep->lp, sp, fp);
922}
923
924/*
925 * Send ACK_1 (or equiv.) indicating we received something.
926 * The frame we're acking is supplied.
927 */
928static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
929{
930 struct fc_frame *fp;
931 struct fc_frame_header *rx_fh;
932 struct fc_frame_header *fh;
933 struct fc_exch *ep = fc_seq_exch(sp);
934 struct fc_lport *lp = ep->lp;
935 unsigned int f_ctl;
936
937 /*
938 * Don't send ACKs for class 3.
939 */
940 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
941 fp = fc_frame_alloc(lp, 0);
942 if (!fp)
943 return;
944
945 fh = fc_frame_header_get(fp);
946 fh->fh_r_ctl = FC_RCTL_ACK_1;
947 fh->fh_type = FC_TYPE_BLS;
948
949 /*
950 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
951 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
952 * Bits 9-8 are meaningful (retransmitted or unidirectional).
953 * Last ACK uses bits 7-6 (continue sequence),
954 * bits 5-4 are meaningful (what kind of ACK to use).
955 */
956 rx_fh = fc_frame_header_get(rx_fp);
957 f_ctl = ntoh24(rx_fh->fh_f_ctl);
958 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
959 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
960 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
961 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
962 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
963 hton24(fh->fh_f_ctl, f_ctl);
964
965 fc_exch_setup_hdr(ep, fp, f_ctl);
966 fh->fh_seq_id = rx_fh->fh_seq_id;
967 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
968 fh->fh_parm_offset = htonl(1); /* ack single frame */
969
970 fr_sof(fp) = fr_sof(rx_fp);
971 if (f_ctl & FC_FC_END_SEQ)
972 fr_eof(fp) = FC_EOF_T;
973 else
974 fr_eof(fp) = FC_EOF_N;
975
976 (void) lp->tt.frame_send(lp, fp);
977 }
978}
979
980/*
981 * Send BLS Reject.
982 * This is for rejecting BA_ABTS only.
983 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800984static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
985 enum fc_ba_rjt_reason reason,
986 enum fc_ba_rjt_explan explan)
Robert Love42e9a922008-12-09 15:10:17 -0800987{
988 struct fc_frame *fp;
989 struct fc_frame_header *rx_fh;
990 struct fc_frame_header *fh;
991 struct fc_ba_rjt *rp;
992 struct fc_lport *lp;
993 unsigned int f_ctl;
994
995 lp = fr_dev(rx_fp);
996 fp = fc_frame_alloc(lp, sizeof(*rp));
997 if (!fp)
998 return;
999 fh = fc_frame_header_get(fp);
1000 rx_fh = fc_frame_header_get(rx_fp);
1001
1002 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
1003
1004 rp = fc_frame_payload_get(fp, sizeof(*rp));
1005 rp->br_reason = reason;
1006 rp->br_explan = explan;
1007
1008 /*
1009 * seq_id, cs_ctl, df_ctl and param/offset are zero.
1010 */
1011 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
1012 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
1013 fh->fh_ox_id = rx_fh->fh_rx_id;
1014 fh->fh_rx_id = rx_fh->fh_ox_id;
1015 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1016 fh->fh_r_ctl = FC_RCTL_BA_RJT;
1017 fh->fh_type = FC_TYPE_BLS;
1018
1019 /*
1020 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
1021 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
1022 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1023 * Last ACK uses bits 7-6 (continue sequence),
1024 * bits 5-4 are meaningful (what kind of ACK to use).
1025 * Always set LAST_SEQ, END_SEQ.
1026 */
1027 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1028 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1029 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1030 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1031 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1032 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1033 f_ctl &= ~FC_FC_FIRST_SEQ;
1034 hton24(fh->fh_f_ctl, f_ctl);
1035
1036 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1037 fr_eof(fp) = FC_EOF_T;
1038 if (fc_sof_needs_ack(fr_sof(fp)))
1039 fr_eof(fp) = FC_EOF_N;
1040
1041 (void) lp->tt.frame_send(lp, fp);
1042}
1043
1044/*
1045 * Handle an incoming ABTS. This would be for target mode usually,
1046 * but could be due to lost FCP transfer ready, confirm or RRQ.
1047 * We always handle this as an exchange abort, ignoring the parameter.
1048 */
1049static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1050{
1051 struct fc_frame *fp;
1052 struct fc_ba_acc *ap;
1053 struct fc_frame_header *fh;
1054 struct fc_seq *sp;
1055
1056 if (!ep)
1057 goto reject;
1058 spin_lock_bh(&ep->ex_lock);
1059 if (ep->esb_stat & ESB_ST_COMPLETE) {
1060 spin_unlock_bh(&ep->ex_lock);
1061 goto reject;
1062 }
1063 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1064 fc_exch_hold(ep); /* hold for REC_QUAL */
1065 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1066 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1067
1068 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1069 if (!fp) {
1070 spin_unlock_bh(&ep->ex_lock);
1071 goto free;
1072 }
1073 fh = fc_frame_header_get(fp);
1074 ap = fc_frame_payload_get(fp, sizeof(*ap));
1075 memset(ap, 0, sizeof(*ap));
1076 sp = &ep->seq;
1077 ap->ba_high_seq_cnt = htons(0xffff);
1078 if (sp->ssb_stat & SSB_ST_RESP) {
1079 ap->ba_seq_id = sp->id;
1080 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1081 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1082 ap->ba_low_seq_cnt = htons(sp->cnt);
1083 }
Vasu Deva7e84f22009-02-27 10:54:51 -08001084 sp = fc_seq_start_next_locked(sp);
Robert Love42e9a922008-12-09 15:10:17 -08001085 spin_unlock_bh(&ep->ex_lock);
1086 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1087 fc_frame_free(rx_fp);
1088 return;
1089
1090reject:
1091 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1092free:
1093 fc_frame_free(rx_fp);
1094}
1095
1096/*
1097 * Handle receive where the other end is originating the sequence.
1098 */
1099static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1100 struct fc_frame *fp)
1101{
1102 struct fc_frame_header *fh = fc_frame_header_get(fp);
1103 struct fc_seq *sp = NULL;
1104 struct fc_exch *ep = NULL;
1105 enum fc_sof sof;
1106 enum fc_eof eof;
1107 u32 f_ctl;
1108 enum fc_pf_rjt_reason reject;
1109
1110 fr_seq(fp) = NULL;
1111 reject = fc_seq_lookup_recip(mp, fp);
1112 if (reject == FC_RJT_NONE) {
1113 sp = fr_seq(fp); /* sequence will be held */
1114 ep = fc_seq_exch(sp);
1115 sof = fr_sof(fp);
1116 eof = fr_eof(fp);
1117 f_ctl = ntoh24(fh->fh_f_ctl);
1118 fc_seq_send_ack(sp, fp);
1119
1120 /*
1121 * Call the receive function.
1122 *
1123 * The receive function may allocate a new sequence
1124 * over the old one, so we shouldn't change the
1125 * sequence after this.
1126 *
1127 * The frame will be freed by the receive function.
1128 * If new exch resp handler is valid then call that
1129 * first.
1130 */
1131 if (ep->resp)
1132 ep->resp(sp, fp, ep->arg);
1133 else
1134 lp->tt.lport_recv(lp, sp, fp);
1135 fc_exch_release(ep); /* release from lookup */
1136 } else {
1137 FC_DEBUG_EXCH("exch/seq lookup failed: reject %x\n", reject);
1138 fc_frame_free(fp);
1139 }
1140}
1141
1142/*
1143 * Handle receive where the other end is originating the sequence in
1144 * response to our exchange.
1145 */
1146static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1147{
1148 struct fc_frame_header *fh = fc_frame_header_get(fp);
1149 struct fc_seq *sp;
1150 struct fc_exch *ep;
1151 enum fc_sof sof;
1152 u32 f_ctl;
1153 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1154 void *ex_resp_arg;
1155 int rc;
1156
1157 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1158 if (!ep) {
1159 atomic_inc(&mp->stats.xid_not_found);
1160 goto out;
1161 }
Steve Ma30121d12009-05-06 10:52:29 -07001162 if (ep->esb_stat & ESB_ST_COMPLETE) {
1163 atomic_inc(&mp->stats.xid_not_found);
1164 goto out;
1165 }
Robert Love42e9a922008-12-09 15:10:17 -08001166 if (ep->rxid == FC_XID_UNKNOWN)
1167 ep->rxid = ntohs(fh->fh_rx_id);
1168 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1169 atomic_inc(&mp->stats.xid_not_found);
1170 goto rel;
1171 }
1172 if (ep->did != ntoh24(fh->fh_s_id) &&
1173 ep->did != FC_FID_FLOGI) {
1174 atomic_inc(&mp->stats.xid_not_found);
1175 goto rel;
1176 }
1177 sof = fr_sof(fp);
1178 if (fc_sof_is_init(sof)) {
1179 sp = fc_seq_start_next(&ep->seq);
1180 sp->id = fh->fh_seq_id;
1181 sp->ssb_stat |= SSB_ST_RESP;
1182 } else {
1183 sp = &ep->seq;
1184 if (sp->id != fh->fh_seq_id) {
1185 atomic_inc(&mp->stats.seq_not_found);
1186 goto rel;
1187 }
1188 }
1189 f_ctl = ntoh24(fh->fh_f_ctl);
1190 fr_seq(fp) = sp;
1191 if (f_ctl & FC_FC_SEQ_INIT)
1192 ep->esb_stat |= ESB_ST_SEQ_INIT;
1193
1194 if (fc_sof_needs_ack(sof))
1195 fc_seq_send_ack(sp, fp);
1196 resp = ep->resp;
1197 ex_resp_arg = ep->arg;
1198
1199 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1200 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1201 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1202 spin_lock_bh(&ep->ex_lock);
1203 rc = fc_exch_done_locked(ep);
1204 WARN_ON(fc_seq_exch(sp) != ep);
1205 spin_unlock_bh(&ep->ex_lock);
1206 if (!rc)
1207 fc_exch_mgr_delete_ep(ep);
1208 }
1209
1210 /*
1211 * Call the receive function.
1212 * The sequence is held (has a refcnt) for us,
1213 * but not for the receive function.
1214 *
1215 * The receive function may allocate a new sequence
1216 * over the old one, so we shouldn't change the
1217 * sequence after this.
1218 *
1219 * The frame will be freed by the receive function.
1220 * If new exch resp handler is valid then call that
1221 * first.
1222 */
1223 if (resp)
1224 resp(sp, fp, ex_resp_arg);
1225 else
1226 fc_frame_free(fp);
1227 fc_exch_release(ep);
1228 return;
1229rel:
1230 fc_exch_release(ep);
1231out:
1232 fc_frame_free(fp);
1233}
1234
1235/*
1236 * Handle receive for a sequence where other end is responding to our sequence.
1237 */
1238static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1239{
1240 struct fc_seq *sp;
1241
1242 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
1243 if (!sp) {
1244 atomic_inc(&mp->stats.xid_not_found);
1245 FC_DEBUG_EXCH("seq lookup failed\n");
1246 } else {
1247 atomic_inc(&mp->stats.non_bls_resp);
1248 FC_DEBUG_EXCH("non-BLS response to sequence");
1249 }
1250 fc_frame_free(fp);
1251}
1252
1253/*
1254 * Handle the response to an ABTS for exchange or sequence.
1255 * This can be BA_ACC or BA_RJT.
1256 */
1257static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1258{
1259 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1260 void *ex_resp_arg;
1261 struct fc_frame_header *fh;
1262 struct fc_ba_acc *ap;
1263 struct fc_seq *sp;
1264 u16 low;
1265 u16 high;
1266 int rc = 1, has_rec = 0;
1267
1268 fh = fc_frame_header_get(fp);
1269 FC_DEBUG_EXCH("exch: BLS rctl %x - %s\n",
1270 fh->fh_r_ctl, fc_exch_rctl_name(fh->fh_r_ctl));
1271
1272 if (cancel_delayed_work_sync(&ep->timeout_work))
1273 fc_exch_release(ep); /* release from pending timer hold */
1274
1275 spin_lock_bh(&ep->ex_lock);
1276 switch (fh->fh_r_ctl) {
1277 case FC_RCTL_BA_ACC:
1278 ap = fc_frame_payload_get(fp, sizeof(*ap));
1279 if (!ap)
1280 break;
1281
1282 /*
1283 * Decide whether to establish a Recovery Qualifier.
1284 * We do this if there is a non-empty SEQ_CNT range and
1285 * SEQ_ID is the same as the one we aborted.
1286 */
1287 low = ntohs(ap->ba_low_seq_cnt);
1288 high = ntohs(ap->ba_high_seq_cnt);
1289 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1290 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1291 ap->ba_seq_id == ep->seq_id) && low != high) {
1292 ep->esb_stat |= ESB_ST_REC_QUAL;
1293 fc_exch_hold(ep); /* hold for recovery qualifier */
1294 has_rec = 1;
1295 }
1296 break;
1297 case FC_RCTL_BA_RJT:
1298 break;
1299 default:
1300 break;
1301 }
1302
1303 resp = ep->resp;
1304 ex_resp_arg = ep->arg;
1305
1306 /* do we need to do some other checks here. Can we reuse more of
1307 * fc_exch_recv_seq_resp
1308 */
1309 sp = &ep->seq;
1310 /*
1311 * do we want to check END_SEQ as well as LAST_SEQ here?
1312 */
1313 if (ep->fh_type != FC_TYPE_FCP &&
1314 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1315 rc = fc_exch_done_locked(ep);
1316 spin_unlock_bh(&ep->ex_lock);
1317 if (!rc)
1318 fc_exch_mgr_delete_ep(ep);
1319
1320 if (resp)
1321 resp(sp, fp, ex_resp_arg);
1322 else
1323 fc_frame_free(fp);
1324
1325 if (has_rec)
1326 fc_exch_timer_set(ep, ep->r_a_tov);
1327
1328}
1329
1330/*
1331 * Receive BLS sequence.
1332 * This is always a sequence initiated by the remote side.
1333 * We may be either the originator or recipient of the exchange.
1334 */
1335static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1336{
1337 struct fc_frame_header *fh;
1338 struct fc_exch *ep;
1339 u32 f_ctl;
1340
1341 fh = fc_frame_header_get(fp);
1342 f_ctl = ntoh24(fh->fh_f_ctl);
1343 fr_seq(fp) = NULL;
1344
1345 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1346 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1347 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1348 spin_lock_bh(&ep->ex_lock);
1349 ep->esb_stat |= ESB_ST_SEQ_INIT;
1350 spin_unlock_bh(&ep->ex_lock);
1351 }
1352 if (f_ctl & FC_FC_SEQ_CTX) {
1353 /*
1354 * A response to a sequence we initiated.
1355 * This should only be ACKs for class 2 or F.
1356 */
1357 switch (fh->fh_r_ctl) {
1358 case FC_RCTL_ACK_1:
1359 case FC_RCTL_ACK_0:
1360 break;
1361 default:
1362 FC_DEBUG_EXCH("BLS rctl %x - %s received",
1363 fh->fh_r_ctl,
1364 fc_exch_rctl_name(fh->fh_r_ctl));
1365 break;
1366 }
1367 fc_frame_free(fp);
1368 } else {
1369 switch (fh->fh_r_ctl) {
1370 case FC_RCTL_BA_RJT:
1371 case FC_RCTL_BA_ACC:
1372 if (ep)
1373 fc_exch_abts_resp(ep, fp);
1374 else
1375 fc_frame_free(fp);
1376 break;
1377 case FC_RCTL_BA_ABTS:
1378 fc_exch_recv_abts(ep, fp);
1379 break;
1380 default: /* ignore junk */
1381 fc_frame_free(fp);
1382 break;
1383 }
1384 }
1385 if (ep)
1386 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1387}
1388
1389/*
1390 * Accept sequence with LS_ACC.
1391 * If this fails due to allocation or transmit congestion, assume the
1392 * originator will repeat the sequence.
1393 */
1394static void fc_seq_ls_acc(struct fc_seq *req_sp)
1395{
1396 struct fc_seq *sp;
1397 struct fc_els_ls_acc *acc;
1398 struct fc_frame *fp;
1399
1400 sp = fc_seq_start_next(req_sp);
1401 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1402 if (fp) {
1403 acc = fc_frame_payload_get(fp, sizeof(*acc));
1404 memset(acc, 0, sizeof(*acc));
1405 acc->la_cmd = ELS_LS_ACC;
1406 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1407 }
1408}
1409
1410/*
1411 * Reject sequence with ELS LS_RJT.
1412 * If this fails due to allocation or transmit congestion, assume the
1413 * originator will repeat the sequence.
1414 */
1415static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
1416 enum fc_els_rjt_explan explan)
1417{
1418 struct fc_seq *sp;
1419 struct fc_els_ls_rjt *rjt;
1420 struct fc_frame *fp;
1421
1422 sp = fc_seq_start_next(req_sp);
1423 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
1424 if (fp) {
1425 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1426 memset(rjt, 0, sizeof(*rjt));
1427 rjt->er_cmd = ELS_LS_RJT;
1428 rjt->er_reason = reason;
1429 rjt->er_explan = explan;
1430 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1431 }
1432}
1433
1434static void fc_exch_reset(struct fc_exch *ep)
1435{
1436 struct fc_seq *sp;
1437 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1438 void *arg;
1439 int rc = 1;
1440
1441 spin_lock_bh(&ep->ex_lock);
1442 ep->state |= FC_EX_RST_CLEANUP;
1443 /*
1444 * we really want to call del_timer_sync, but cannot due
1445 * to the lport calling with the lport lock held (some resp
1446 * functions can also grab the lport lock which could cause
1447 * a deadlock).
1448 */
1449 if (cancel_delayed_work(&ep->timeout_work))
1450 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1451 resp = ep->resp;
1452 ep->resp = NULL;
1453 if (ep->esb_stat & ESB_ST_REC_QUAL)
1454 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1455 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1456 arg = ep->arg;
1457 sp = &ep->seq;
1458 rc = fc_exch_done_locked(ep);
1459 spin_unlock_bh(&ep->ex_lock);
1460 if (!rc)
1461 fc_exch_mgr_delete_ep(ep);
1462
1463 if (resp)
1464 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1465}
1466
1467/*
1468 * Reset an exchange manager, releasing all sequences and exchanges.
1469 * If sid is non-zero, reset only exchanges we source from that FID.
1470 * If did is non-zero, reset only exchanges destined to that FID.
1471 */
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001472void fc_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
Robert Love42e9a922008-12-09 15:10:17 -08001473{
1474 struct fc_exch *ep;
1475 struct fc_exch *next;
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001476 struct fc_exch_mgr *mp = lp->emp;
Robert Love42e9a922008-12-09 15:10:17 -08001477
1478 spin_lock_bh(&mp->em_lock);
1479restart:
1480 list_for_each_entry_safe(ep, next, &mp->ex_list, ex_list) {
1481 if ((sid == 0 || sid == ep->sid) &&
1482 (did == 0 || did == ep->did)) {
1483 fc_exch_hold(ep);
1484 spin_unlock_bh(&mp->em_lock);
1485
1486 fc_exch_reset(ep);
1487
1488 fc_exch_release(ep);
1489 spin_lock_bh(&mp->em_lock);
1490
1491 /*
1492 * must restart loop incase while lock was down
1493 * multiple eps were released.
1494 */
1495 goto restart;
1496 }
1497 }
1498 spin_unlock_bh(&mp->em_lock);
1499}
1500EXPORT_SYMBOL(fc_exch_mgr_reset);
1501
1502/*
1503 * Handle incoming ELS REC - Read Exchange Concise.
1504 * Note that the requesting port may be different than the S_ID in the request.
1505 */
1506static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
1507{
1508 struct fc_frame *fp;
1509 struct fc_exch *ep;
1510 struct fc_exch_mgr *em;
1511 struct fc_els_rec *rp;
1512 struct fc_els_rec_acc *acc;
1513 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1514 enum fc_els_rjt_explan explan;
1515 u32 sid;
1516 u16 rxid;
1517 u16 oxid;
1518
1519 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1520 explan = ELS_EXPL_INV_LEN;
1521 if (!rp)
1522 goto reject;
1523 sid = ntoh24(rp->rec_s_id);
1524 rxid = ntohs(rp->rec_rx_id);
1525 oxid = ntohs(rp->rec_ox_id);
1526
1527 /*
1528 * Currently it's hard to find the local S_ID from the exchange
1529 * manager. This will eventually be fixed, but for now it's easier
1530 * to lookup the subject exchange twice, once as if we were
1531 * the initiator, and then again if we weren't.
1532 */
1533 em = fc_seq_exch(sp)->em;
1534 ep = fc_exch_find(em, oxid);
1535 explan = ELS_EXPL_OXID_RXID;
1536 if (ep && ep->oid == sid) {
1537 if (ep->rxid != FC_XID_UNKNOWN &&
1538 rxid != FC_XID_UNKNOWN &&
1539 ep->rxid != rxid)
1540 goto rel;
1541 } else {
1542 if (ep)
1543 fc_exch_release(ep);
1544 ep = NULL;
1545 if (rxid != FC_XID_UNKNOWN)
1546 ep = fc_exch_find(em, rxid);
1547 if (!ep)
1548 goto reject;
1549 }
1550
1551 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1552 if (!fp) {
1553 fc_exch_done(sp);
1554 goto out;
1555 }
1556 sp = fc_seq_start_next(sp);
1557 acc = fc_frame_payload_get(fp, sizeof(*acc));
1558 memset(acc, 0, sizeof(*acc));
1559 acc->reca_cmd = ELS_LS_ACC;
1560 acc->reca_ox_id = rp->rec_ox_id;
1561 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1562 acc->reca_rx_id = htons(ep->rxid);
1563 if (ep->sid == ep->oid)
1564 hton24(acc->reca_rfid, ep->did);
1565 else
1566 hton24(acc->reca_rfid, ep->sid);
1567 acc->reca_fc4value = htonl(ep->seq.rec_data);
1568 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1569 ESB_ST_SEQ_INIT |
1570 ESB_ST_COMPLETE));
1571 sp = fc_seq_start_next(sp);
1572 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1573out:
1574 fc_exch_release(ep);
1575 fc_frame_free(rfp);
1576 return;
1577
1578rel:
1579 fc_exch_release(ep);
1580reject:
1581 fc_seq_ls_rjt(sp, reason, explan);
1582 fc_frame_free(rfp);
1583}
1584
1585/*
1586 * Handle response from RRQ.
1587 * Not much to do here, really.
1588 * Should report errors.
1589 *
1590 * TODO: fix error handler.
1591 */
1592static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1593{
1594 struct fc_exch *aborted_ep = arg;
1595 unsigned int op;
1596
1597 if (IS_ERR(fp)) {
1598 int err = PTR_ERR(fp);
1599
Vasu Dev78342da2009-02-27 10:54:46 -08001600 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
Robert Love42e9a922008-12-09 15:10:17 -08001601 goto cleanup;
1602 FC_DBG("Cannot process RRQ, because of frame error %d\n", err);
1603 return;
1604 }
1605
1606 op = fc_frame_payload_op(fp);
1607 fc_frame_free(fp);
1608
1609 switch (op) {
1610 case ELS_LS_RJT:
1611 FC_DBG("LS_RJT for RRQ");
1612 /* fall through */
1613 case ELS_LS_ACC:
1614 goto cleanup;
1615 default:
1616 FC_DBG("unexpected response op %x for RRQ", op);
1617 return;
1618 }
1619
1620cleanup:
1621 fc_exch_done(&aborted_ep->seq);
1622 /* drop hold for rec qual */
1623 fc_exch_release(aborted_ep);
1624}
1625
1626/*
1627 * Send ELS RRQ - Reinstate Recovery Qualifier.
1628 * This tells the remote port to stop blocking the use of
1629 * the exchange and the seq_cnt range.
1630 */
1631static void fc_exch_rrq(struct fc_exch *ep)
1632{
1633 struct fc_lport *lp;
1634 struct fc_els_rrq *rrq;
1635 struct fc_frame *fp;
1636 struct fc_seq *rrq_sp;
1637 u32 did;
1638
1639 lp = ep->lp;
1640
1641 fp = fc_frame_alloc(lp, sizeof(*rrq));
1642 if (!fp)
1643 return;
1644 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1645 memset(rrq, 0, sizeof(*rrq));
1646 rrq->rrq_cmd = ELS_RRQ;
1647 hton24(rrq->rrq_s_id, ep->sid);
1648 rrq->rrq_ox_id = htons(ep->oxid);
1649 rrq->rrq_rx_id = htons(ep->rxid);
1650
1651 did = ep->did;
1652 if (ep->esb_stat & ESB_ST_RESP)
1653 did = ep->sid;
1654
1655 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1656 fc_host_port_id(lp->host), FC_TYPE_ELS,
1657 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1658
1659 rrq_sp = fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep,
1660 lp->e_d_tov);
1661 if (!rrq_sp) {
1662 ep->esb_stat |= ESB_ST_REC_QUAL;
1663 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1664 return;
1665 }
1666}
1667
1668
1669/*
1670 * Handle incoming ELS RRQ - Reset Recovery Qualifier.
1671 */
1672static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1673{
1674 struct fc_exch *ep; /* request or subject exchange */
1675 struct fc_els_rrq *rp;
1676 u32 sid;
1677 u16 xid;
1678 enum fc_els_rjt_explan explan;
1679
1680 rp = fc_frame_payload_get(fp, sizeof(*rp));
1681 explan = ELS_EXPL_INV_LEN;
1682 if (!rp)
1683 goto reject;
1684
1685 /*
1686 * lookup subject exchange.
1687 */
1688 ep = fc_seq_exch(sp);
1689 sid = ntoh24(rp->rrq_s_id); /* subject source */
1690 xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
1691 ep = fc_exch_find(ep->em, xid);
1692
1693 explan = ELS_EXPL_OXID_RXID;
1694 if (!ep)
1695 goto reject;
1696 spin_lock_bh(&ep->ex_lock);
1697 if (ep->oxid != ntohs(rp->rrq_ox_id))
1698 goto unlock_reject;
1699 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
1700 ep->rxid != FC_XID_UNKNOWN)
1701 goto unlock_reject;
1702 explan = ELS_EXPL_SID;
1703 if (ep->sid != sid)
1704 goto unlock_reject;
1705
1706 /*
1707 * Clear Recovery Qualifier state, and cancel timer if complete.
1708 */
1709 if (ep->esb_stat & ESB_ST_REC_QUAL) {
1710 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1711 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
1712 }
1713 if (ep->esb_stat & ESB_ST_COMPLETE) {
1714 if (cancel_delayed_work(&ep->timeout_work))
1715 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
1716 }
1717
1718 spin_unlock_bh(&ep->ex_lock);
1719
1720 /*
1721 * Send LS_ACC.
1722 */
1723 fc_seq_ls_acc(sp);
1724 fc_frame_free(fp);
1725 return;
1726
1727unlock_reject:
1728 spin_unlock_bh(&ep->ex_lock);
1729 fc_exch_release(ep); /* drop hold from fc_exch_find */
1730reject:
1731 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
1732 fc_frame_free(fp);
1733}
1734
1735struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1736 enum fc_class class,
1737 u16 min_xid, u16 max_xid)
1738{
1739 struct fc_exch_mgr *mp;
1740 size_t len;
1741
1742 if (max_xid <= min_xid || min_xid == 0 || max_xid == FC_XID_UNKNOWN) {
1743 FC_DBG("Invalid min_xid 0x:%x and max_xid 0x:%x\n",
1744 min_xid, max_xid);
1745 return NULL;
1746 }
1747
1748 /*
1749 * Memory need for EM
1750 */
1751#define xid_ok(i, m1, m2) (((i) >= (m1)) && ((i) <= (m2)))
1752 len = (max_xid - min_xid + 1) * (sizeof(struct fc_exch *));
1753 len += sizeof(struct fc_exch_mgr);
1754
1755 mp = kzalloc(len, GFP_ATOMIC);
1756 if (!mp)
1757 return NULL;
1758
1759 mp->class = class;
1760 mp->total_exches = 0;
1761 mp->exches = (struct fc_exch **)(mp + 1);
1762 mp->lp = lp;
1763 /* adjust em exch xid range for offload */
1764 mp->min_xid = min_xid;
1765 mp->max_xid = max_xid;
1766 mp->last_xid = min_xid - 1;
1767 mp->max_read = 0;
1768 mp->last_read = 0;
1769 if (lp->lro_enabled && xid_ok(lp->lro_xid, min_xid, max_xid)) {
1770 mp->max_read = lp->lro_xid;
1771 mp->last_read = min_xid - 1;
1772 mp->last_xid = mp->max_read;
1773 } else {
1774 /* disable lro if no xid control over read */
1775 lp->lro_enabled = 0;
1776 }
1777
1778 INIT_LIST_HEAD(&mp->ex_list);
1779 spin_lock_init(&mp->em_lock);
1780
1781 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
1782 if (!mp->ep_pool)
1783 goto free_mp;
1784
1785 return mp;
1786
1787free_mp:
1788 kfree(mp);
1789 return NULL;
1790}
1791EXPORT_SYMBOL(fc_exch_mgr_alloc);
1792
1793void fc_exch_mgr_free(struct fc_exch_mgr *mp)
1794{
1795 WARN_ON(!mp);
1796 /*
1797 * The total exch count must be zero
1798 * before freeing exchange manager.
1799 */
1800 WARN_ON(mp->total_exches != 0);
1801 mempool_destroy(mp->ep_pool);
1802 kfree(mp);
1803}
1804EXPORT_SYMBOL(fc_exch_mgr_free);
1805
1806struct fc_exch *fc_exch_get(struct fc_lport *lp, struct fc_frame *fp)
1807{
1808 if (!lp || !lp->emp)
1809 return NULL;
1810
1811 return fc_exch_alloc(lp->emp, fp, 0);
1812}
1813EXPORT_SYMBOL(fc_exch_get);
1814
1815struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
1816 struct fc_frame *fp,
1817 void (*resp)(struct fc_seq *,
1818 struct fc_frame *fp,
1819 void *arg),
1820 void (*destructor)(struct fc_seq *, void *),
1821 void *arg, u32 timer_msec)
1822{
1823 struct fc_exch *ep;
1824 struct fc_seq *sp = NULL;
1825 struct fc_frame_header *fh;
1826 int rc = 1;
1827
1828 ep = lp->tt.exch_get(lp, fp);
1829 if (!ep) {
1830 fc_frame_free(fp);
1831 return NULL;
1832 }
1833 ep->esb_stat |= ESB_ST_SEQ_INIT;
1834 fh = fc_frame_header_get(fp);
1835 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1836 ep->resp = resp;
1837 ep->destructor = destructor;
1838 ep->arg = arg;
1839 ep->r_a_tov = FC_DEF_R_A_TOV;
1840 ep->lp = lp;
1841 sp = &ep->seq;
1842
1843 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1844 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1845 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1846 sp->cnt++;
1847
Yi Zoub277d2a2009-02-27 14:07:21 -08001848 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1849
Robert Love42e9a922008-12-09 15:10:17 -08001850 if (unlikely(lp->tt.frame_send(lp, fp)))
1851 goto err;
1852
1853 if (timer_msec)
1854 fc_exch_timer_set_locked(ep, timer_msec);
1855 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1856
1857 if (ep->f_ctl & FC_FC_SEQ_INIT)
1858 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1859 spin_unlock_bh(&ep->ex_lock);
1860 return sp;
1861err:
1862 rc = fc_exch_done_locked(ep);
1863 spin_unlock_bh(&ep->ex_lock);
1864 if (!rc)
1865 fc_exch_mgr_delete_ep(ep);
1866 return NULL;
1867}
1868EXPORT_SYMBOL(fc_exch_seq_send);
1869
1870/*
1871 * Receive a frame
1872 */
1873void fc_exch_recv(struct fc_lport *lp, struct fc_exch_mgr *mp,
1874 struct fc_frame *fp)
1875{
1876 struct fc_frame_header *fh = fc_frame_header_get(fp);
1877 u32 f_ctl;
1878
1879 /* lport lock ? */
1880 if (!lp || !mp || (lp->state == LPORT_ST_NONE)) {
1881 FC_DBG("fc_lport or EM is not allocated and configured");
1882 fc_frame_free(fp);
1883 return;
1884 }
1885
1886 /*
1887 * If frame is marked invalid, just drop it.
1888 */
1889 f_ctl = ntoh24(fh->fh_f_ctl);
1890 switch (fr_eof(fp)) {
1891 case FC_EOF_T:
1892 if (f_ctl & FC_FC_END_SEQ)
1893 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
1894 /* fall through */
1895 case FC_EOF_N:
1896 if (fh->fh_type == FC_TYPE_BLS)
1897 fc_exch_recv_bls(mp, fp);
1898 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
1899 FC_FC_EX_CTX)
1900 fc_exch_recv_seq_resp(mp, fp);
1901 else if (f_ctl & FC_FC_SEQ_CTX)
1902 fc_exch_recv_resp(mp, fp);
1903 else
1904 fc_exch_recv_req(lp, mp, fp);
1905 break;
1906 default:
1907 FC_DBG("dropping invalid frame (eof %x)", fr_eof(fp));
1908 fc_frame_free(fp);
1909 break;
1910 }
1911}
1912EXPORT_SYMBOL(fc_exch_recv);
1913
1914int fc_exch_init(struct fc_lport *lp)
1915{
1916 if (!lp->tt.exch_get) {
1917 /*
1918 * exch_put() should be NULL if
1919 * exch_get() is NULL
1920 */
1921 WARN_ON(lp->tt.exch_put);
1922 lp->tt.exch_get = fc_exch_get;
1923 }
1924
1925 if (!lp->tt.seq_start_next)
1926 lp->tt.seq_start_next = fc_seq_start_next;
1927
1928 if (!lp->tt.exch_seq_send)
1929 lp->tt.exch_seq_send = fc_exch_seq_send;
1930
1931 if (!lp->tt.seq_send)
1932 lp->tt.seq_send = fc_seq_send;
1933
1934 if (!lp->tt.seq_els_rsp_send)
1935 lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
1936
1937 if (!lp->tt.exch_done)
1938 lp->tt.exch_done = fc_exch_done;
1939
1940 if (!lp->tt.exch_mgr_reset)
1941 lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
1942
1943 if (!lp->tt.seq_exch_abort)
1944 lp->tt.seq_exch_abort = fc_seq_exch_abort;
1945
1946 return 0;
1947}
1948EXPORT_SYMBOL(fc_exch_init);
1949
1950int fc_setup_exch_mgr(void)
1951{
1952 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
1953 0, SLAB_HWCACHE_ALIGN, NULL);
1954 if (!fc_em_cachep)
1955 return -ENOMEM;
1956 return 0;
1957}
1958
1959void fc_destroy_exch_mgr(void)
1960{
1961 kmem_cache_destroy(fc_em_cachep);
1962}