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