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