blob: 40c34274bd81f96581c6b212c46fc5b934f93bdf [file] [log] [blame]
Robert Love42e9a922008-12-09 15:10:17 -08001/*
2 * Copyright(c) 2007 Intel Corporation. All rights reserved.
3 * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
4 * Copyright(c) 2008 Mike Christie
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Maintained at www.Open-FCoE.org
20 */
21
22/*
23 * Fibre Channel exchange and sequence handling.
24 */
25
26#include <linux/timer.h>
27#include <linux/gfp.h>
28#include <linux/err.h>
29
30#include <scsi/fc/fc_fc2.h>
31
32#include <scsi/libfc.h>
33#include <scsi/fc_encode.h>
34
Robert Love74147052009-06-10 15:31:10 -070035static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
Robert Love42e9a922008-12-09 15:10:17 -080036
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 */
Vasu Dev96316092009-07-29 17:05:00 -070058 struct kref kref; /* exchange mgr reference count */
Robert Love42e9a922008-12-09 15:10:17 -080059 spinlock_t em_lock; /* exchange manager lock,
60 must be taken before ex_lock */
Vasu Devd7179682009-07-29 17:05:21 -070061 u16 next_xid; /* next possible free exchange ID */
Robert Love42e9a922008-12-09 15:10:17 -080062 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 */
Robert Love42e9a922008-12-09 15:10:17 -080068 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
Vasu Dev96316092009-07-29 17:05:00 -070087struct fc_exch_mgr_anchor {
88 struct list_head ema_list;
89 struct fc_exch_mgr *mp;
90 bool (*match)(struct fc_frame *);
91};
92
Robert Love42e9a922008-12-09 15:10:17 -080093static 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);
Julia Lawallaa6cd292009-02-04 22:17:29 +0100277 WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
Robert Love42e9a922008-12-09 15:10:17 -0800278 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
Robert Lovecd305ce2009-08-25 13:58:37 -0700329 FC_EXCH_DBG(ep, "Exchange timer armed\n");
Robert Love74147052009-06-10 15:31:10 -0700330
Robert Love42e9a922008-12-09 15:10:17 -0800331 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
Robert Lovecd305ce2009-08-25 13:58:37 -0700415 FC_EXCH_DBG(ep, "Exchange timed out\n");
416
Robert Love42e9a922008-12-09 15:10:17 -0800417 spin_lock_bh(&ep->ex_lock);
418 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
419 goto unlock;
420
421 e_stat = ep->esb_stat;
422 if (e_stat & ESB_ST_COMPLETE) {
423 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
Vasu Deva0cc1ec2009-07-28 17:33:37 -0700424 spin_unlock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -0800425 if (e_stat & ESB_ST_REC_QUAL)
426 fc_exch_rrq(ep);
Robert Love42e9a922008-12-09 15:10:17 -0800427 goto done;
428 } else {
429 resp = ep->resp;
430 arg = ep->arg;
431 ep->resp = NULL;
432 if (e_stat & ESB_ST_ABNORMAL)
433 rc = fc_exch_done_locked(ep);
434 spin_unlock_bh(&ep->ex_lock);
435 if (!rc)
436 fc_exch_mgr_delete_ep(ep);
437 if (resp)
438 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
439 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
440 goto done;
441 }
442unlock:
443 spin_unlock_bh(&ep->ex_lock);
444done:
445 /*
446 * This release matches the hold taken when the timer was set.
447 */
448 fc_exch_release(ep);
449}
450
451/*
452 * Allocate a sequence.
453 *
454 * We don't support multiple originated sequences on the same exchange.
455 * By implication, any previously originated sequence on this exchange
456 * is complete, and we reallocate the same sequence.
457 */
458static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
459{
460 struct fc_seq *sp;
461
462 sp = &ep->seq;
463 sp->ssb_stat = 0;
464 sp->cnt = 0;
465 sp->id = seq_id;
466 return sp;
467}
468
Vasu Dev52ff8782009-07-29 17:05:10 -0700469/**
470 * fc_exch_em_alloc() - allocate an exchange from a specified EM.
471 * @lport: ptr to the local port
472 * @mp: ptr to the exchange manager
Robert Love42e9a922008-12-09 15:10:17 -0800473 *
Vasu Devd7179682009-07-29 17:05:21 -0700474 * Returns pointer to allocated fc_exch with exch lock held.
Robert Love42e9a922008-12-09 15:10:17 -0800475 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700476static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
Vasu Devd7179682009-07-29 17:05:21 -0700477 struct fc_exch_mgr *mp)
Robert Love42e9a922008-12-09 15:10:17 -0800478{
479 struct fc_exch *ep;
Vasu Devd7179682009-07-29 17:05:21 -0700480 u16 min, max, xid;
481
482 min = mp->min_xid;
483 max = mp->max_xid;
Robert Love42e9a922008-12-09 15:10:17 -0800484
485 /* allocate memory for exchange */
486 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
487 if (!ep) {
488 atomic_inc(&mp->stats.no_free_exch);
489 goto out;
490 }
491 memset(ep, 0, sizeof(*ep));
492
493 spin_lock_bh(&mp->em_lock);
Vasu Devd7179682009-07-29 17:05:21 -0700494 xid = mp->next_xid;
495 /* alloc a new xid */
496 while (mp->exches[xid - min]) {
497 xid = (xid == max) ? min : xid + 1;
498 if (xid == mp->next_xid)
Robert Love42e9a922008-12-09 15:10:17 -0800499 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800500 }
Vasu Devd7179682009-07-29 17:05:21 -0700501 mp->next_xid = (xid == max) ? min : xid + 1;
Robert Love42e9a922008-12-09 15:10:17 -0800502
503 fc_exch_hold(ep); /* hold for exch in mp */
504 spin_lock_init(&ep->ex_lock);
505 /*
506 * Hold exch lock for caller to prevent fc_exch_reset()
507 * from releasing exch while fc_exch_alloc() caller is
508 * still working on exch.
509 */
510 spin_lock_bh(&ep->ex_lock);
511
512 mp->exches[xid - mp->min_xid] = ep;
513 list_add_tail(&ep->ex_list, &mp->ex_list);
514 fc_seq_alloc(ep, ep->seq_id++);
515 mp->total_exches++;
516 spin_unlock_bh(&mp->em_lock);
517
518 /*
519 * update exchange
520 */
521 ep->oxid = ep->xid = xid;
522 ep->em = mp;
Vasu Dev52ff8782009-07-29 17:05:10 -0700523 ep->lp = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800524 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
525 ep->rxid = FC_XID_UNKNOWN;
526 ep->class = mp->class;
527 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
528out:
529 return ep;
530err:
531 spin_unlock_bh(&mp->em_lock);
532 atomic_inc(&mp->stats.no_free_exch_xid);
533 mempool_free(ep, mp->ep_pool);
534 return NULL;
535}
Vasu Dev52ff8782009-07-29 17:05:10 -0700536
537/**
538 * fc_exch_alloc() - allocate an exchange.
539 * @lport: ptr to the local port
540 * @fp: ptr to the FC frame
541 *
542 * This function walks the list of the exchange manager(EM)
543 * anchors to select a EM for new exchange allocation. The
544 * EM is selected having either a NULL match function pointer
545 * or call to match function returning true.
546 */
547struct fc_exch *fc_exch_alloc(struct fc_lport *lport, struct fc_frame *fp)
548{
549 struct fc_exch_mgr_anchor *ema;
550 struct fc_exch *ep;
551
552 list_for_each_entry(ema, &lport->ema_list, ema_list) {
553 if (!ema->match || ema->match(fp)) {
Vasu Devd7179682009-07-29 17:05:21 -0700554 ep = fc_exch_em_alloc(lport, ema->mp);
Vasu Dev52ff8782009-07-29 17:05:10 -0700555 if (ep)
556 return ep;
557 }
558 }
559 return NULL;
560}
Robert Love42e9a922008-12-09 15:10:17 -0800561EXPORT_SYMBOL(fc_exch_alloc);
562
563/*
564 * Lookup and hold an exchange.
565 */
566static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
567{
568 struct fc_exch *ep = NULL;
569
570 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
571 spin_lock_bh(&mp->em_lock);
572 ep = mp->exches[xid - mp->min_xid];
573 if (ep) {
574 fc_exch_hold(ep);
575 WARN_ON(ep->xid != xid);
576 }
577 spin_unlock_bh(&mp->em_lock);
578 }
579 return ep;
580}
581
582void fc_exch_done(struct fc_seq *sp)
583{
584 struct fc_exch *ep = fc_seq_exch(sp);
585 int rc;
586
587 spin_lock_bh(&ep->ex_lock);
588 rc = fc_exch_done_locked(ep);
589 spin_unlock_bh(&ep->ex_lock);
590 if (!rc)
591 fc_exch_mgr_delete_ep(ep);
592}
593EXPORT_SYMBOL(fc_exch_done);
594
595/*
596 * Allocate a new exchange as responder.
597 * Sets the responder ID in the frame header.
598 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700599static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
600 struct fc_exch_mgr *mp,
601 struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800602{
603 struct fc_exch *ep;
604 struct fc_frame_header *fh;
Robert Love42e9a922008-12-09 15:10:17 -0800605
Vasu Dev52ff8782009-07-29 17:05:10 -0700606 ep = fc_exch_alloc(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800607 if (ep) {
608 ep->class = fc_frame_class(fp);
609
610 /*
611 * Set EX_CTX indicating we're responding on this exchange.
612 */
613 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
614 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
615 fh = fc_frame_header_get(fp);
616 ep->sid = ntoh24(fh->fh_d_id);
617 ep->did = ntoh24(fh->fh_s_id);
618 ep->oid = ep->did;
619
620 /*
621 * Allocated exchange has placed the XID in the
622 * originator field. Move it to the responder field,
623 * and set the originator XID from the frame.
624 */
625 ep->rxid = ep->xid;
626 ep->oxid = ntohs(fh->fh_ox_id);
627 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
628 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
629 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
630
Robert Love42e9a922008-12-09 15:10:17 -0800631 fc_exch_hold(ep); /* hold for caller */
Vasu Dev52ff8782009-07-29 17:05:10 -0700632 spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */
Robert Love42e9a922008-12-09 15:10:17 -0800633 }
634 return ep;
635}
636
637/*
638 * Find a sequence for receive where the other end is originating the sequence.
639 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
640 * on the ep that should be released by the caller.
641 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700642static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
643 struct fc_exch_mgr *mp,
Robert Loveb2ab99c2009-02-27 10:55:50 -0800644 struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800645{
646 struct fc_frame_header *fh = fc_frame_header_get(fp);
647 struct fc_exch *ep = NULL;
648 struct fc_seq *sp = NULL;
649 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
650 u32 f_ctl;
651 u16 xid;
652
653 f_ctl = ntoh24(fh->fh_f_ctl);
654 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
655
656 /*
657 * Lookup or create the exchange if we will be creating the sequence.
658 */
659 if (f_ctl & FC_FC_EX_CTX) {
660 xid = ntohs(fh->fh_ox_id); /* we originated exch */
661 ep = fc_exch_find(mp, xid);
662 if (!ep) {
663 atomic_inc(&mp->stats.xid_not_found);
664 reject = FC_RJT_OX_ID;
665 goto out;
666 }
667 if (ep->rxid == FC_XID_UNKNOWN)
668 ep->rxid = ntohs(fh->fh_rx_id);
669 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
670 reject = FC_RJT_OX_ID;
671 goto rel;
672 }
673 } else {
674 xid = ntohs(fh->fh_rx_id); /* we are the responder */
675
676 /*
677 * Special case for MDS issuing an ELS TEST with a
678 * bad rxid of 0.
679 * XXX take this out once we do the proper reject.
680 */
681 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
682 fc_frame_payload_op(fp) == ELS_TEST) {
683 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
684 xid = FC_XID_UNKNOWN;
685 }
686
687 /*
688 * new sequence - find the exchange
689 */
690 ep = fc_exch_find(mp, xid);
691 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
692 if (ep) {
693 atomic_inc(&mp->stats.xid_busy);
694 reject = FC_RJT_RX_ID;
695 goto rel;
696 }
Vasu Dev52ff8782009-07-29 17:05:10 -0700697 ep = fc_exch_resp(lport, mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800698 if (!ep) {
699 reject = FC_RJT_EXCH_EST; /* XXX */
700 goto out;
701 }
702 xid = ep->xid; /* get our XID */
703 } else if (!ep) {
704 atomic_inc(&mp->stats.xid_not_found);
705 reject = FC_RJT_RX_ID; /* XID not found */
706 goto out;
707 }
708 }
709
710 /*
711 * At this point, we have the exchange held.
712 * Find or create the sequence.
713 */
714 if (fc_sof_is_init(fr_sof(fp))) {
715 sp = fc_seq_start_next(&ep->seq);
716 if (!sp) {
717 reject = FC_RJT_SEQ_XS; /* exchange shortage */
718 goto rel;
719 }
720 sp->id = fh->fh_seq_id;
721 sp->ssb_stat |= SSB_ST_RESP;
722 } else {
723 sp = &ep->seq;
724 if (sp->id != fh->fh_seq_id) {
725 atomic_inc(&mp->stats.seq_not_found);
726 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
727 goto rel;
728 }
729 }
730 WARN_ON(ep != fc_seq_exch(sp));
731
732 if (f_ctl & FC_FC_SEQ_INIT)
733 ep->esb_stat |= ESB_ST_SEQ_INIT;
734
735 fr_seq(fp) = sp;
736out:
737 return reject;
738rel:
739 fc_exch_done(&ep->seq);
740 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
741 return reject;
742}
743
744/*
745 * Find the sequence for a frame being received.
746 * We originated the sequence, so it should be found.
747 * We may or may not have originated the exchange.
748 * Does not hold the sequence for the caller.
749 */
750static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
751 struct fc_frame *fp)
752{
753 struct fc_frame_header *fh = fc_frame_header_get(fp);
754 struct fc_exch *ep;
755 struct fc_seq *sp = NULL;
756 u32 f_ctl;
757 u16 xid;
758
759 f_ctl = ntoh24(fh->fh_f_ctl);
760 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
761 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
762 ep = fc_exch_find(mp, xid);
763 if (!ep)
764 return NULL;
765 if (ep->seq.id == fh->fh_seq_id) {
766 /*
767 * Save the RX_ID if we didn't previously know it.
768 */
769 sp = &ep->seq;
770 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
771 ep->rxid == FC_XID_UNKNOWN) {
772 ep->rxid = ntohs(fh->fh_rx_id);
773 }
774 }
775 fc_exch_release(ep);
776 return sp;
777}
778
779/*
780 * Set addresses for an exchange.
781 * Note this must be done before the first sequence of the exchange is sent.
782 */
783static void fc_exch_set_addr(struct fc_exch *ep,
784 u32 orig_id, u32 resp_id)
785{
786 ep->oid = orig_id;
787 if (ep->esb_stat & ESB_ST_RESP) {
788 ep->sid = resp_id;
789 ep->did = orig_id;
790 } else {
791 ep->sid = orig_id;
792 ep->did = resp_id;
793 }
794}
795
796static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
797{
798 struct fc_exch *ep = fc_seq_exch(sp);
799
800 sp = fc_seq_alloc(ep, ep->seq_id++);
Robert Love74147052009-06-10 15:31:10 -0700801 FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
802 ep->f_ctl, sp->id);
Robert Love42e9a922008-12-09 15:10:17 -0800803 return sp;
804}
805/*
806 * Allocate a new sequence on the same exchange as the supplied sequence.
807 * This will never return NULL.
808 */
809struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
810{
811 struct fc_exch *ep = fc_seq_exch(sp);
812
813 spin_lock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -0800814 sp = fc_seq_start_next_locked(sp);
815 spin_unlock_bh(&ep->ex_lock);
816
817 return sp;
818}
819EXPORT_SYMBOL(fc_seq_start_next);
820
821int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
822{
823 struct fc_exch *ep;
824 struct fc_frame_header *fh = fc_frame_header_get(fp);
825 int error;
826 u32 f_ctl;
827
828 ep = fc_seq_exch(sp);
829 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
830
831 f_ctl = ntoh24(fh->fh_f_ctl);
832 fc_exch_setup_hdr(ep, fp, f_ctl);
833
834 /*
835 * update sequence count if this frame is carrying
836 * multiple FC frames when sequence offload is enabled
837 * by LLD.
838 */
839 if (fr_max_payload(fp))
840 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
841 fr_max_payload(fp));
842 else
843 sp->cnt++;
844
845 /*
846 * Send the frame.
847 */
848 error = lp->tt.frame_send(lp, fp);
849
850 /*
851 * Update the exchange and sequence flags,
852 * assuming all frames for the sequence have been sent.
853 * We can only be called to send once for each sequence.
854 */
855 spin_lock_bh(&ep->ex_lock);
856 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
857 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
858 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
859 spin_unlock_bh(&ep->ex_lock);
860 return error;
861}
862EXPORT_SYMBOL(fc_seq_send);
863
864void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
865 struct fc_seq_els_data *els_data)
866{
867 switch (els_cmd) {
868 case ELS_LS_RJT:
869 fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
870 break;
871 case ELS_LS_ACC:
872 fc_seq_ls_acc(sp);
873 break;
874 case ELS_RRQ:
875 fc_exch_els_rrq(sp, els_data->fp);
876 break;
877 case ELS_REC:
878 fc_exch_els_rec(sp, els_data->fp);
879 break;
880 default:
Robert Love74147052009-06-10 15:31:10 -0700881 FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
Robert Love42e9a922008-12-09 15:10:17 -0800882 }
883}
884EXPORT_SYMBOL(fc_seq_els_rsp_send);
885
886/*
887 * Send a sequence, which is also the last sequence in the exchange.
888 */
889static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
890 enum fc_rctl rctl, enum fc_fh_type fh_type)
891{
892 u32 f_ctl;
893 struct fc_exch *ep = fc_seq_exch(sp);
894
895 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
896 f_ctl |= ep->f_ctl;
897 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
898 fc_seq_send(ep->lp, sp, fp);
899}
900
901/*
902 * Send ACK_1 (or equiv.) indicating we received something.
903 * The frame we're acking is supplied.
904 */
905static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
906{
907 struct fc_frame *fp;
908 struct fc_frame_header *rx_fh;
909 struct fc_frame_header *fh;
910 struct fc_exch *ep = fc_seq_exch(sp);
911 struct fc_lport *lp = ep->lp;
912 unsigned int f_ctl;
913
914 /*
915 * Don't send ACKs for class 3.
916 */
917 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
918 fp = fc_frame_alloc(lp, 0);
919 if (!fp)
920 return;
921
922 fh = fc_frame_header_get(fp);
923 fh->fh_r_ctl = FC_RCTL_ACK_1;
924 fh->fh_type = FC_TYPE_BLS;
925
926 /*
927 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
928 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
929 * Bits 9-8 are meaningful (retransmitted or unidirectional).
930 * Last ACK uses bits 7-6 (continue sequence),
931 * bits 5-4 are meaningful (what kind of ACK to use).
932 */
933 rx_fh = fc_frame_header_get(rx_fp);
934 f_ctl = ntoh24(rx_fh->fh_f_ctl);
935 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
936 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
937 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
938 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
939 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
940 hton24(fh->fh_f_ctl, f_ctl);
941
942 fc_exch_setup_hdr(ep, fp, f_ctl);
943 fh->fh_seq_id = rx_fh->fh_seq_id;
944 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
945 fh->fh_parm_offset = htonl(1); /* ack single frame */
946
947 fr_sof(fp) = fr_sof(rx_fp);
948 if (f_ctl & FC_FC_END_SEQ)
949 fr_eof(fp) = FC_EOF_T;
950 else
951 fr_eof(fp) = FC_EOF_N;
952
953 (void) lp->tt.frame_send(lp, fp);
954 }
955}
956
957/*
958 * Send BLS Reject.
959 * This is for rejecting BA_ABTS only.
960 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800961static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
962 enum fc_ba_rjt_reason reason,
963 enum fc_ba_rjt_explan explan)
Robert Love42e9a922008-12-09 15:10:17 -0800964{
965 struct fc_frame *fp;
966 struct fc_frame_header *rx_fh;
967 struct fc_frame_header *fh;
968 struct fc_ba_rjt *rp;
969 struct fc_lport *lp;
970 unsigned int f_ctl;
971
972 lp = fr_dev(rx_fp);
973 fp = fc_frame_alloc(lp, sizeof(*rp));
974 if (!fp)
975 return;
976 fh = fc_frame_header_get(fp);
977 rx_fh = fc_frame_header_get(rx_fp);
978
979 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
980
981 rp = fc_frame_payload_get(fp, sizeof(*rp));
982 rp->br_reason = reason;
983 rp->br_explan = explan;
984
985 /*
986 * seq_id, cs_ctl, df_ctl and param/offset are zero.
987 */
988 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
989 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
990 fh->fh_ox_id = rx_fh->fh_rx_id;
991 fh->fh_rx_id = rx_fh->fh_ox_id;
992 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
993 fh->fh_r_ctl = FC_RCTL_BA_RJT;
994 fh->fh_type = FC_TYPE_BLS;
995
996 /*
997 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
998 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
999 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1000 * Last ACK uses bits 7-6 (continue sequence),
1001 * bits 5-4 are meaningful (what kind of ACK to use).
1002 * Always set LAST_SEQ, END_SEQ.
1003 */
1004 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1005 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1006 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1007 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1008 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1009 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1010 f_ctl &= ~FC_FC_FIRST_SEQ;
1011 hton24(fh->fh_f_ctl, f_ctl);
1012
1013 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1014 fr_eof(fp) = FC_EOF_T;
1015 if (fc_sof_needs_ack(fr_sof(fp)))
1016 fr_eof(fp) = FC_EOF_N;
1017
1018 (void) lp->tt.frame_send(lp, fp);
1019}
1020
1021/*
1022 * Handle an incoming ABTS. This would be for target mode usually,
1023 * but could be due to lost FCP transfer ready, confirm or RRQ.
1024 * We always handle this as an exchange abort, ignoring the parameter.
1025 */
1026static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1027{
1028 struct fc_frame *fp;
1029 struct fc_ba_acc *ap;
1030 struct fc_frame_header *fh;
1031 struct fc_seq *sp;
1032
1033 if (!ep)
1034 goto reject;
1035 spin_lock_bh(&ep->ex_lock);
1036 if (ep->esb_stat & ESB_ST_COMPLETE) {
1037 spin_unlock_bh(&ep->ex_lock);
1038 goto reject;
1039 }
1040 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1041 fc_exch_hold(ep); /* hold for REC_QUAL */
1042 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1043 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1044
1045 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1046 if (!fp) {
1047 spin_unlock_bh(&ep->ex_lock);
1048 goto free;
1049 }
1050 fh = fc_frame_header_get(fp);
1051 ap = fc_frame_payload_get(fp, sizeof(*ap));
1052 memset(ap, 0, sizeof(*ap));
1053 sp = &ep->seq;
1054 ap->ba_high_seq_cnt = htons(0xffff);
1055 if (sp->ssb_stat & SSB_ST_RESP) {
1056 ap->ba_seq_id = sp->id;
1057 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1058 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1059 ap->ba_low_seq_cnt = htons(sp->cnt);
1060 }
Vasu Deva7e84f22009-02-27 10:54:51 -08001061 sp = fc_seq_start_next_locked(sp);
Robert Love42e9a922008-12-09 15:10:17 -08001062 spin_unlock_bh(&ep->ex_lock);
1063 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1064 fc_frame_free(rx_fp);
1065 return;
1066
1067reject:
1068 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1069free:
1070 fc_frame_free(rx_fp);
1071}
1072
1073/*
1074 * Handle receive where the other end is originating the sequence.
1075 */
1076static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1077 struct fc_frame *fp)
1078{
1079 struct fc_frame_header *fh = fc_frame_header_get(fp);
1080 struct fc_seq *sp = NULL;
1081 struct fc_exch *ep = NULL;
1082 enum fc_sof sof;
1083 enum fc_eof eof;
1084 u32 f_ctl;
1085 enum fc_pf_rjt_reason reject;
1086
1087 fr_seq(fp) = NULL;
Vasu Dev52ff8782009-07-29 17:05:10 -07001088 reject = fc_seq_lookup_recip(lp, mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001089 if (reject == FC_RJT_NONE) {
1090 sp = fr_seq(fp); /* sequence will be held */
1091 ep = fc_seq_exch(sp);
1092 sof = fr_sof(fp);
1093 eof = fr_eof(fp);
1094 f_ctl = ntoh24(fh->fh_f_ctl);
1095 fc_seq_send_ack(sp, fp);
1096
1097 /*
1098 * Call the receive function.
1099 *
1100 * The receive function may allocate a new sequence
1101 * over the old one, so we shouldn't change the
1102 * sequence after this.
1103 *
1104 * The frame will be freed by the receive function.
1105 * If new exch resp handler is valid then call that
1106 * first.
1107 */
1108 if (ep->resp)
1109 ep->resp(sp, fp, ep->arg);
1110 else
1111 lp->tt.lport_recv(lp, sp, fp);
1112 fc_exch_release(ep); /* release from lookup */
1113 } else {
Robert Loved459b7e2009-07-29 17:05:05 -07001114 FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject);
Robert Love42e9a922008-12-09 15:10:17 -08001115 fc_frame_free(fp);
1116 }
1117}
1118
1119/*
1120 * Handle receive where the other end is originating the sequence in
1121 * response to our exchange.
1122 */
1123static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1124{
1125 struct fc_frame_header *fh = fc_frame_header_get(fp);
1126 struct fc_seq *sp;
1127 struct fc_exch *ep;
1128 enum fc_sof sof;
1129 u32 f_ctl;
1130 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1131 void *ex_resp_arg;
1132 int rc;
1133
1134 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1135 if (!ep) {
1136 atomic_inc(&mp->stats.xid_not_found);
1137 goto out;
1138 }
Steve Ma30121d12009-05-06 10:52:29 -07001139 if (ep->esb_stat & ESB_ST_COMPLETE) {
1140 atomic_inc(&mp->stats.xid_not_found);
1141 goto out;
1142 }
Robert Love42e9a922008-12-09 15:10:17 -08001143 if (ep->rxid == FC_XID_UNKNOWN)
1144 ep->rxid = ntohs(fh->fh_rx_id);
1145 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1146 atomic_inc(&mp->stats.xid_not_found);
1147 goto rel;
1148 }
1149 if (ep->did != ntoh24(fh->fh_s_id) &&
1150 ep->did != FC_FID_FLOGI) {
1151 atomic_inc(&mp->stats.xid_not_found);
1152 goto rel;
1153 }
1154 sof = fr_sof(fp);
1155 if (fc_sof_is_init(sof)) {
1156 sp = fc_seq_start_next(&ep->seq);
1157 sp->id = fh->fh_seq_id;
1158 sp->ssb_stat |= SSB_ST_RESP;
1159 } else {
1160 sp = &ep->seq;
1161 if (sp->id != fh->fh_seq_id) {
1162 atomic_inc(&mp->stats.seq_not_found);
1163 goto rel;
1164 }
1165 }
1166 f_ctl = ntoh24(fh->fh_f_ctl);
1167 fr_seq(fp) = sp;
1168 if (f_ctl & FC_FC_SEQ_INIT)
1169 ep->esb_stat |= ESB_ST_SEQ_INIT;
1170
1171 if (fc_sof_needs_ack(sof))
1172 fc_seq_send_ack(sp, fp);
1173 resp = ep->resp;
1174 ex_resp_arg = ep->arg;
1175
1176 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1177 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1178 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1179 spin_lock_bh(&ep->ex_lock);
1180 rc = fc_exch_done_locked(ep);
1181 WARN_ON(fc_seq_exch(sp) != ep);
1182 spin_unlock_bh(&ep->ex_lock);
1183 if (!rc)
1184 fc_exch_mgr_delete_ep(ep);
1185 }
1186
1187 /*
1188 * Call the receive function.
1189 * The sequence is held (has a refcnt) for us,
1190 * but not for the receive function.
1191 *
1192 * The receive function may allocate a new sequence
1193 * over the old one, so we shouldn't change the
1194 * sequence after this.
1195 *
1196 * The frame will be freed by the receive function.
1197 * If new exch resp handler is valid then call that
1198 * first.
1199 */
1200 if (resp)
1201 resp(sp, fp, ex_resp_arg);
1202 else
1203 fc_frame_free(fp);
1204 fc_exch_release(ep);
1205 return;
1206rel:
1207 fc_exch_release(ep);
1208out:
1209 fc_frame_free(fp);
1210}
1211
1212/*
1213 * Handle receive for a sequence where other end is responding to our sequence.
1214 */
1215static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1216{
1217 struct fc_seq *sp;
1218
1219 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
Robert Loved459b7e2009-07-29 17:05:05 -07001220
1221 if (!sp)
Robert Love42e9a922008-12-09 15:10:17 -08001222 atomic_inc(&mp->stats.xid_not_found);
Robert Loved459b7e2009-07-29 17:05:05 -07001223 else
Robert Love42e9a922008-12-09 15:10:17 -08001224 atomic_inc(&mp->stats.non_bls_resp);
Robert Loved459b7e2009-07-29 17:05:05 -07001225
Robert Love42e9a922008-12-09 15:10:17 -08001226 fc_frame_free(fp);
1227}
1228
1229/*
1230 * Handle the response to an ABTS for exchange or sequence.
1231 * This can be BA_ACC or BA_RJT.
1232 */
1233static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1234{
1235 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1236 void *ex_resp_arg;
1237 struct fc_frame_header *fh;
1238 struct fc_ba_acc *ap;
1239 struct fc_seq *sp;
1240 u16 low;
1241 u16 high;
1242 int rc = 1, has_rec = 0;
1243
1244 fh = fc_frame_header_get(fp);
Robert Love74147052009-06-10 15:31:10 -07001245 FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
1246 fc_exch_rctl_name(fh->fh_r_ctl));
Robert Love42e9a922008-12-09 15:10:17 -08001247
1248 if (cancel_delayed_work_sync(&ep->timeout_work))
1249 fc_exch_release(ep); /* release from pending timer hold */
1250
1251 spin_lock_bh(&ep->ex_lock);
1252 switch (fh->fh_r_ctl) {
1253 case FC_RCTL_BA_ACC:
1254 ap = fc_frame_payload_get(fp, sizeof(*ap));
1255 if (!ap)
1256 break;
1257
1258 /*
1259 * Decide whether to establish a Recovery Qualifier.
1260 * We do this if there is a non-empty SEQ_CNT range and
1261 * SEQ_ID is the same as the one we aborted.
1262 */
1263 low = ntohs(ap->ba_low_seq_cnt);
1264 high = ntohs(ap->ba_high_seq_cnt);
1265 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1266 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1267 ap->ba_seq_id == ep->seq_id) && low != high) {
1268 ep->esb_stat |= ESB_ST_REC_QUAL;
1269 fc_exch_hold(ep); /* hold for recovery qualifier */
1270 has_rec = 1;
1271 }
1272 break;
1273 case FC_RCTL_BA_RJT:
1274 break;
1275 default:
1276 break;
1277 }
1278
1279 resp = ep->resp;
1280 ex_resp_arg = ep->arg;
1281
1282 /* do we need to do some other checks here. Can we reuse more of
1283 * fc_exch_recv_seq_resp
1284 */
1285 sp = &ep->seq;
1286 /*
1287 * do we want to check END_SEQ as well as LAST_SEQ here?
1288 */
1289 if (ep->fh_type != FC_TYPE_FCP &&
1290 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1291 rc = fc_exch_done_locked(ep);
1292 spin_unlock_bh(&ep->ex_lock);
1293 if (!rc)
1294 fc_exch_mgr_delete_ep(ep);
1295
1296 if (resp)
1297 resp(sp, fp, ex_resp_arg);
1298 else
1299 fc_frame_free(fp);
1300
1301 if (has_rec)
1302 fc_exch_timer_set(ep, ep->r_a_tov);
1303
1304}
1305
1306/*
1307 * Receive BLS sequence.
1308 * This is always a sequence initiated by the remote side.
1309 * We may be either the originator or recipient of the exchange.
1310 */
1311static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1312{
1313 struct fc_frame_header *fh;
1314 struct fc_exch *ep;
1315 u32 f_ctl;
1316
1317 fh = fc_frame_header_get(fp);
1318 f_ctl = ntoh24(fh->fh_f_ctl);
1319 fr_seq(fp) = NULL;
1320
1321 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1322 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1323 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1324 spin_lock_bh(&ep->ex_lock);
1325 ep->esb_stat |= ESB_ST_SEQ_INIT;
1326 spin_unlock_bh(&ep->ex_lock);
1327 }
1328 if (f_ctl & FC_FC_SEQ_CTX) {
1329 /*
1330 * A response to a sequence we initiated.
1331 * This should only be ACKs for class 2 or F.
1332 */
1333 switch (fh->fh_r_ctl) {
1334 case FC_RCTL_ACK_1:
1335 case FC_RCTL_ACK_0:
1336 break;
1337 default:
Robert Love74147052009-06-10 15:31:10 -07001338 FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
1339 fh->fh_r_ctl,
1340 fc_exch_rctl_name(fh->fh_r_ctl));
Robert Love42e9a922008-12-09 15:10:17 -08001341 break;
1342 }
1343 fc_frame_free(fp);
1344 } else {
1345 switch (fh->fh_r_ctl) {
1346 case FC_RCTL_BA_RJT:
1347 case FC_RCTL_BA_ACC:
1348 if (ep)
1349 fc_exch_abts_resp(ep, fp);
1350 else
1351 fc_frame_free(fp);
1352 break;
1353 case FC_RCTL_BA_ABTS:
1354 fc_exch_recv_abts(ep, fp);
1355 break;
1356 default: /* ignore junk */
1357 fc_frame_free(fp);
1358 break;
1359 }
1360 }
1361 if (ep)
1362 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1363}
1364
1365/*
1366 * Accept sequence with LS_ACC.
1367 * If this fails due to allocation or transmit congestion, assume the
1368 * originator will repeat the sequence.
1369 */
1370static void fc_seq_ls_acc(struct fc_seq *req_sp)
1371{
1372 struct fc_seq *sp;
1373 struct fc_els_ls_acc *acc;
1374 struct fc_frame *fp;
1375
1376 sp = fc_seq_start_next(req_sp);
1377 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1378 if (fp) {
1379 acc = fc_frame_payload_get(fp, sizeof(*acc));
1380 memset(acc, 0, sizeof(*acc));
1381 acc->la_cmd = ELS_LS_ACC;
1382 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1383 }
1384}
1385
1386/*
1387 * Reject sequence with ELS LS_RJT.
1388 * If this fails due to allocation or transmit congestion, assume the
1389 * originator will repeat the sequence.
1390 */
1391static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
1392 enum fc_els_rjt_explan explan)
1393{
1394 struct fc_seq *sp;
1395 struct fc_els_ls_rjt *rjt;
1396 struct fc_frame *fp;
1397
1398 sp = fc_seq_start_next(req_sp);
1399 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
1400 if (fp) {
1401 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1402 memset(rjt, 0, sizeof(*rjt));
1403 rjt->er_cmd = ELS_LS_RJT;
1404 rjt->er_reason = reason;
1405 rjt->er_explan = explan;
1406 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1407 }
1408}
1409
1410static void fc_exch_reset(struct fc_exch *ep)
1411{
1412 struct fc_seq *sp;
1413 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1414 void *arg;
1415 int rc = 1;
1416
1417 spin_lock_bh(&ep->ex_lock);
1418 ep->state |= FC_EX_RST_CLEANUP;
1419 /*
1420 * we really want to call del_timer_sync, but cannot due
1421 * to the lport calling with the lport lock held (some resp
1422 * functions can also grab the lport lock which could cause
1423 * a deadlock).
1424 */
1425 if (cancel_delayed_work(&ep->timeout_work))
1426 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1427 resp = ep->resp;
1428 ep->resp = NULL;
1429 if (ep->esb_stat & ESB_ST_REC_QUAL)
1430 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1431 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1432 arg = ep->arg;
1433 sp = &ep->seq;
1434 rc = fc_exch_done_locked(ep);
1435 spin_unlock_bh(&ep->ex_lock);
1436 if (!rc)
1437 fc_exch_mgr_delete_ep(ep);
1438
1439 if (resp)
1440 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1441}
1442
1443/*
1444 * Reset an exchange manager, releasing all sequences and exchanges.
1445 * If sid is non-zero, reset only exchanges we source from that FID.
1446 * If did is non-zero, reset only exchanges destined to that FID.
1447 */
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001448void fc_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
Robert Love42e9a922008-12-09 15:10:17 -08001449{
1450 struct fc_exch *ep;
1451 struct fc_exch *next;
Vasu Dev52ff8782009-07-29 17:05:10 -07001452 struct fc_exch_mgr *mp;
1453 struct fc_exch_mgr_anchor *ema;
Robert Love42e9a922008-12-09 15:10:17 -08001454
Vasu Dev52ff8782009-07-29 17:05:10 -07001455 list_for_each_entry(ema, &lp->ema_list, ema_list) {
1456 mp = ema->mp;
1457 spin_lock_bh(&mp->em_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001458restart:
Vasu Dev52ff8782009-07-29 17:05:10 -07001459 list_for_each_entry_safe(ep, next, &mp->ex_list, ex_list) {
1460 if ((lp == ep->lp) &&
1461 (sid == 0 || sid == ep->sid) &&
1462 (did == 0 || did == ep->did)) {
1463 fc_exch_hold(ep);
1464 spin_unlock_bh(&mp->em_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001465
Vasu Dev52ff8782009-07-29 17:05:10 -07001466 fc_exch_reset(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001467
Vasu Dev52ff8782009-07-29 17:05:10 -07001468 fc_exch_release(ep);
1469 spin_lock_bh(&mp->em_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001470
Vasu Dev52ff8782009-07-29 17:05:10 -07001471 /*
1472 * must restart loop incase while lock
1473 * was down multiple eps were released.
1474 */
1475 goto restart;
1476 }
Robert Love42e9a922008-12-09 15:10:17 -08001477 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001478 spin_unlock_bh(&mp->em_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001479 }
Robert Love42e9a922008-12-09 15:10:17 -08001480}
1481EXPORT_SYMBOL(fc_exch_mgr_reset);
1482
1483/*
1484 * Handle incoming ELS REC - Read Exchange Concise.
1485 * Note that the requesting port may be different than the S_ID in the request.
1486 */
1487static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
1488{
1489 struct fc_frame *fp;
1490 struct fc_exch *ep;
1491 struct fc_exch_mgr *em;
1492 struct fc_els_rec *rp;
1493 struct fc_els_rec_acc *acc;
1494 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1495 enum fc_els_rjt_explan explan;
1496 u32 sid;
1497 u16 rxid;
1498 u16 oxid;
1499
1500 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1501 explan = ELS_EXPL_INV_LEN;
1502 if (!rp)
1503 goto reject;
1504 sid = ntoh24(rp->rec_s_id);
1505 rxid = ntohs(rp->rec_rx_id);
1506 oxid = ntohs(rp->rec_ox_id);
1507
1508 /*
1509 * Currently it's hard to find the local S_ID from the exchange
1510 * manager. This will eventually be fixed, but for now it's easier
1511 * to lookup the subject exchange twice, once as if we were
1512 * the initiator, and then again if we weren't.
1513 */
1514 em = fc_seq_exch(sp)->em;
1515 ep = fc_exch_find(em, oxid);
1516 explan = ELS_EXPL_OXID_RXID;
1517 if (ep && ep->oid == sid) {
1518 if (ep->rxid != FC_XID_UNKNOWN &&
1519 rxid != FC_XID_UNKNOWN &&
1520 ep->rxid != rxid)
1521 goto rel;
1522 } else {
1523 if (ep)
1524 fc_exch_release(ep);
1525 ep = NULL;
1526 if (rxid != FC_XID_UNKNOWN)
1527 ep = fc_exch_find(em, rxid);
1528 if (!ep)
1529 goto reject;
1530 }
1531
1532 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1533 if (!fp) {
1534 fc_exch_done(sp);
1535 goto out;
1536 }
1537 sp = fc_seq_start_next(sp);
1538 acc = fc_frame_payload_get(fp, sizeof(*acc));
1539 memset(acc, 0, sizeof(*acc));
1540 acc->reca_cmd = ELS_LS_ACC;
1541 acc->reca_ox_id = rp->rec_ox_id;
1542 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1543 acc->reca_rx_id = htons(ep->rxid);
1544 if (ep->sid == ep->oid)
1545 hton24(acc->reca_rfid, ep->did);
1546 else
1547 hton24(acc->reca_rfid, ep->sid);
1548 acc->reca_fc4value = htonl(ep->seq.rec_data);
1549 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1550 ESB_ST_SEQ_INIT |
1551 ESB_ST_COMPLETE));
1552 sp = fc_seq_start_next(sp);
1553 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1554out:
1555 fc_exch_release(ep);
1556 fc_frame_free(rfp);
1557 return;
1558
1559rel:
1560 fc_exch_release(ep);
1561reject:
1562 fc_seq_ls_rjt(sp, reason, explan);
1563 fc_frame_free(rfp);
1564}
1565
1566/*
1567 * Handle response from RRQ.
1568 * Not much to do here, really.
1569 * Should report errors.
1570 *
1571 * TODO: fix error handler.
1572 */
1573static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1574{
1575 struct fc_exch *aborted_ep = arg;
1576 unsigned int op;
1577
1578 if (IS_ERR(fp)) {
1579 int err = PTR_ERR(fp);
1580
Vasu Dev78342da2009-02-27 10:54:46 -08001581 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
Robert Love42e9a922008-12-09 15:10:17 -08001582 goto cleanup;
Robert Love74147052009-06-10 15:31:10 -07001583 FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
1584 "frame error %d\n", err);
Robert Love42e9a922008-12-09 15:10:17 -08001585 return;
1586 }
1587
1588 op = fc_frame_payload_op(fp);
1589 fc_frame_free(fp);
1590
1591 switch (op) {
1592 case ELS_LS_RJT:
Robert Love74147052009-06-10 15:31:10 -07001593 FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
Robert Love42e9a922008-12-09 15:10:17 -08001594 /* fall through */
1595 case ELS_LS_ACC:
1596 goto cleanup;
1597 default:
Robert Love74147052009-06-10 15:31:10 -07001598 FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
1599 "for RRQ", op);
Robert Love42e9a922008-12-09 15:10:17 -08001600 return;
1601 }
1602
1603cleanup:
1604 fc_exch_done(&aborted_ep->seq);
1605 /* drop hold for rec qual */
1606 fc_exch_release(aborted_ep);
1607}
1608
1609/*
1610 * Send ELS RRQ - Reinstate Recovery Qualifier.
1611 * This tells the remote port to stop blocking the use of
1612 * the exchange and the seq_cnt range.
1613 */
1614static void fc_exch_rrq(struct fc_exch *ep)
1615{
1616 struct fc_lport *lp;
1617 struct fc_els_rrq *rrq;
1618 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001619 u32 did;
1620
1621 lp = ep->lp;
1622
1623 fp = fc_frame_alloc(lp, sizeof(*rrq));
1624 if (!fp)
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001625 goto retry;
1626
Robert Love42e9a922008-12-09 15:10:17 -08001627 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1628 memset(rrq, 0, sizeof(*rrq));
1629 rrq->rrq_cmd = ELS_RRQ;
1630 hton24(rrq->rrq_s_id, ep->sid);
1631 rrq->rrq_ox_id = htons(ep->oxid);
1632 rrq->rrq_rx_id = htons(ep->rxid);
1633
1634 did = ep->did;
1635 if (ep->esb_stat & ESB_ST_RESP)
1636 did = ep->sid;
1637
1638 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1639 fc_host_port_id(lp->host), FC_TYPE_ELS,
1640 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1641
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001642 if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov))
1643 return;
1644
1645retry:
1646 spin_lock_bh(&ep->ex_lock);
1647 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1648 spin_unlock_bh(&ep->ex_lock);
1649 /* drop hold for rec qual */
1650 fc_exch_release(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001651 return;
1652 }
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001653 ep->esb_stat |= ESB_ST_REC_QUAL;
1654 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1655 spin_unlock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001656}
1657
1658
1659/*
1660 * Handle incoming ELS RRQ - Reset Recovery Qualifier.
1661 */
1662static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1663{
1664 struct fc_exch *ep; /* request or subject exchange */
1665 struct fc_els_rrq *rp;
1666 u32 sid;
1667 u16 xid;
1668 enum fc_els_rjt_explan explan;
1669
1670 rp = fc_frame_payload_get(fp, sizeof(*rp));
1671 explan = ELS_EXPL_INV_LEN;
1672 if (!rp)
1673 goto reject;
1674
1675 /*
1676 * lookup subject exchange.
1677 */
1678 ep = fc_seq_exch(sp);
1679 sid = ntoh24(rp->rrq_s_id); /* subject source */
1680 xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
1681 ep = fc_exch_find(ep->em, xid);
1682
1683 explan = ELS_EXPL_OXID_RXID;
1684 if (!ep)
1685 goto reject;
1686 spin_lock_bh(&ep->ex_lock);
1687 if (ep->oxid != ntohs(rp->rrq_ox_id))
1688 goto unlock_reject;
1689 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
1690 ep->rxid != FC_XID_UNKNOWN)
1691 goto unlock_reject;
1692 explan = ELS_EXPL_SID;
1693 if (ep->sid != sid)
1694 goto unlock_reject;
1695
1696 /*
1697 * Clear Recovery Qualifier state, and cancel timer if complete.
1698 */
1699 if (ep->esb_stat & ESB_ST_REC_QUAL) {
1700 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1701 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
1702 }
1703 if (ep->esb_stat & ESB_ST_COMPLETE) {
1704 if (cancel_delayed_work(&ep->timeout_work))
1705 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
1706 }
1707
1708 spin_unlock_bh(&ep->ex_lock);
1709
1710 /*
1711 * Send LS_ACC.
1712 */
1713 fc_seq_ls_acc(sp);
1714 fc_frame_free(fp);
1715 return;
1716
1717unlock_reject:
1718 spin_unlock_bh(&ep->ex_lock);
1719 fc_exch_release(ep); /* drop hold from fc_exch_find */
1720reject:
1721 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
1722 fc_frame_free(fp);
1723}
1724
Vasu Dev96316092009-07-29 17:05:00 -07001725struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
1726 struct fc_exch_mgr *mp,
1727 bool (*match)(struct fc_frame *))
1728{
1729 struct fc_exch_mgr_anchor *ema;
1730
1731 ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
1732 if (!ema)
1733 return ema;
1734
1735 ema->mp = mp;
1736 ema->match = match;
1737 /* add EM anchor to EM anchors list */
1738 list_add_tail(&ema->ema_list, &lport->ema_list);
1739 kref_get(&mp->kref);
1740 return ema;
1741}
1742EXPORT_SYMBOL(fc_exch_mgr_add);
1743
1744static void fc_exch_mgr_destroy(struct kref *kref)
1745{
1746 struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
1747
1748 /*
1749 * The total exch count must be zero
1750 * before freeing exchange manager.
1751 */
1752 WARN_ON(mp->total_exches != 0);
1753 mempool_destroy(mp->ep_pool);
1754 kfree(mp);
1755}
1756
1757void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
1758{
1759 /* remove EM anchor from EM anchors list */
1760 list_del(&ema->ema_list);
1761 kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
1762 kfree(ema);
1763}
1764EXPORT_SYMBOL(fc_exch_mgr_del);
1765
Robert Love42e9a922008-12-09 15:10:17 -08001766struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1767 enum fc_class class,
Vasu Dev52ff8782009-07-29 17:05:10 -07001768 u16 min_xid, u16 max_xid,
1769 bool (*match)(struct fc_frame *))
Robert Love42e9a922008-12-09 15:10:17 -08001770{
1771 struct fc_exch_mgr *mp;
1772 size_t len;
1773
Vasu Devd7179682009-07-29 17:05:21 -07001774 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
Robert Love74147052009-06-10 15:31:10 -07001775 FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
1776 min_xid, max_xid);
Robert Love42e9a922008-12-09 15:10:17 -08001777 return NULL;
1778 }
1779
1780 /*
1781 * Memory need for EM
1782 */
Robert Love42e9a922008-12-09 15:10:17 -08001783 len = (max_xid - min_xid + 1) * (sizeof(struct fc_exch *));
1784 len += sizeof(struct fc_exch_mgr);
1785
1786 mp = kzalloc(len, GFP_ATOMIC);
1787 if (!mp)
1788 return NULL;
1789
1790 mp->class = class;
1791 mp->total_exches = 0;
1792 mp->exches = (struct fc_exch **)(mp + 1);
Robert Love42e9a922008-12-09 15:10:17 -08001793 /* adjust em exch xid range for offload */
1794 mp->min_xid = min_xid;
1795 mp->max_xid = max_xid;
Vasu Devd7179682009-07-29 17:05:21 -07001796 mp->next_xid = min_xid;
Robert Love42e9a922008-12-09 15:10:17 -08001797
1798 INIT_LIST_HEAD(&mp->ex_list);
1799 spin_lock_init(&mp->em_lock);
1800
1801 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
1802 if (!mp->ep_pool)
1803 goto free_mp;
1804
Vasu Dev52ff8782009-07-29 17:05:10 -07001805 kref_init(&mp->kref);
1806 if (!fc_exch_mgr_add(lp, mp, match)) {
1807 mempool_destroy(mp->ep_pool);
1808 goto free_mp;
1809 }
1810
1811 /*
1812 * Above kref_init() sets mp->kref to 1 and then
1813 * call to fc_exch_mgr_add incremented mp->kref again,
1814 * so adjust that extra increment.
1815 */
1816 kref_put(&mp->kref, fc_exch_mgr_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001817 return mp;
1818
1819free_mp:
1820 kfree(mp);
1821 return NULL;
1822}
1823EXPORT_SYMBOL(fc_exch_mgr_alloc);
1824
Vasu Dev52ff8782009-07-29 17:05:10 -07001825void fc_exch_mgr_free(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001826{
Vasu Dev52ff8782009-07-29 17:05:10 -07001827 struct fc_exch_mgr_anchor *ema, *next;
1828
1829 list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
1830 fc_exch_mgr_del(ema);
Robert Love42e9a922008-12-09 15:10:17 -08001831}
1832EXPORT_SYMBOL(fc_exch_mgr_free);
1833
Robert Love42e9a922008-12-09 15:10:17 -08001834
1835struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
1836 struct fc_frame *fp,
1837 void (*resp)(struct fc_seq *,
1838 struct fc_frame *fp,
1839 void *arg),
1840 void (*destructor)(struct fc_seq *, void *),
1841 void *arg, u32 timer_msec)
1842{
1843 struct fc_exch *ep;
1844 struct fc_seq *sp = NULL;
1845 struct fc_frame_header *fh;
1846 int rc = 1;
1847
Vasu Dev52ff8782009-07-29 17:05:10 -07001848 ep = fc_exch_alloc(lp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001849 if (!ep) {
1850 fc_frame_free(fp);
1851 return NULL;
1852 }
1853 ep->esb_stat |= ESB_ST_SEQ_INIT;
1854 fh = fc_frame_header_get(fp);
1855 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1856 ep->resp = resp;
1857 ep->destructor = destructor;
1858 ep->arg = arg;
1859 ep->r_a_tov = FC_DEF_R_A_TOV;
1860 ep->lp = lp;
1861 sp = &ep->seq;
1862
1863 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1864 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1865 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1866 sp->cnt++;
1867
Vasu Devd7179682009-07-29 17:05:21 -07001868 if (ep->xid <= lp->lro_xid)
1869 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
Yi Zoub277d2a2009-02-27 14:07:21 -08001870
Robert Love42e9a922008-12-09 15:10:17 -08001871 if (unlikely(lp->tt.frame_send(lp, fp)))
1872 goto err;
1873
1874 if (timer_msec)
1875 fc_exch_timer_set_locked(ep, timer_msec);
1876 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1877
1878 if (ep->f_ctl & FC_FC_SEQ_INIT)
1879 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1880 spin_unlock_bh(&ep->ex_lock);
1881 return sp;
1882err:
1883 rc = fc_exch_done_locked(ep);
1884 spin_unlock_bh(&ep->ex_lock);
1885 if (!rc)
1886 fc_exch_mgr_delete_ep(ep);
1887 return NULL;
1888}
1889EXPORT_SYMBOL(fc_exch_seq_send);
1890
1891/*
1892 * Receive a frame
1893 */
Vasu Dev52ff8782009-07-29 17:05:10 -07001894void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001895{
1896 struct fc_frame_header *fh = fc_frame_header_get(fp);
Vasu Dev52ff8782009-07-29 17:05:10 -07001897 struct fc_exch_mgr_anchor *ema;
1898 u32 f_ctl, found = 0;
1899 u16 oxid;
Robert Love42e9a922008-12-09 15:10:17 -08001900
1901 /* lport lock ? */
Vasu Dev52ff8782009-07-29 17:05:10 -07001902 if (!lp || lp->state == LPORT_ST_DISABLED) {
Robert Love74147052009-06-10 15:31:10 -07001903 FC_LPORT_DBG(lp, "Receiving frames for an lport that "
1904 "has not been initialized correctly\n");
Robert Love42e9a922008-12-09 15:10:17 -08001905 fc_frame_free(fp);
1906 return;
1907 }
1908
Vasu Dev52ff8782009-07-29 17:05:10 -07001909 f_ctl = ntoh24(fh->fh_f_ctl);
1910 oxid = ntohs(fh->fh_ox_id);
1911 if (f_ctl & FC_FC_EX_CTX) {
1912 list_for_each_entry(ema, &lp->ema_list, ema_list) {
1913 if ((oxid >= ema->mp->min_xid) &&
1914 (oxid <= ema->mp->max_xid)) {
1915 found = 1;
1916 break;
1917 }
1918 }
1919
1920 if (!found) {
1921 FC_LPORT_DBG(lp, "Received response for out "
1922 "of range oxid:%hx\n", oxid);
1923 fc_frame_free(fp);
1924 return;
1925 }
1926 } else
1927 ema = list_entry(lp->ema_list.prev, typeof(*ema), ema_list);
1928
Robert Love42e9a922008-12-09 15:10:17 -08001929 /*
1930 * If frame is marked invalid, just drop it.
1931 */
Robert Love42e9a922008-12-09 15:10:17 -08001932 switch (fr_eof(fp)) {
1933 case FC_EOF_T:
1934 if (f_ctl & FC_FC_END_SEQ)
1935 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
1936 /* fall through */
1937 case FC_EOF_N:
1938 if (fh->fh_type == FC_TYPE_BLS)
Vasu Dev52ff8782009-07-29 17:05:10 -07001939 fc_exch_recv_bls(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001940 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
1941 FC_FC_EX_CTX)
Vasu Dev52ff8782009-07-29 17:05:10 -07001942 fc_exch_recv_seq_resp(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001943 else if (f_ctl & FC_FC_SEQ_CTX)
Vasu Dev52ff8782009-07-29 17:05:10 -07001944 fc_exch_recv_resp(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001945 else
Vasu Dev52ff8782009-07-29 17:05:10 -07001946 fc_exch_recv_req(lp, ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001947 break;
1948 default:
Robert Loved459b7e2009-07-29 17:05:05 -07001949 FC_LPORT_DBG(lp, "dropping invalid frame (eof %x)", fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -08001950 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001951 }
1952}
1953EXPORT_SYMBOL(fc_exch_recv);
1954
1955int fc_exch_init(struct fc_lport *lp)
1956{
Robert Love42e9a922008-12-09 15:10:17 -08001957 if (!lp->tt.seq_start_next)
1958 lp->tt.seq_start_next = fc_seq_start_next;
1959
1960 if (!lp->tt.exch_seq_send)
1961 lp->tt.exch_seq_send = fc_exch_seq_send;
1962
1963 if (!lp->tt.seq_send)
1964 lp->tt.seq_send = fc_seq_send;
1965
1966 if (!lp->tt.seq_els_rsp_send)
1967 lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
1968
1969 if (!lp->tt.exch_done)
1970 lp->tt.exch_done = fc_exch_done;
1971
1972 if (!lp->tt.exch_mgr_reset)
1973 lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
1974
1975 if (!lp->tt.seq_exch_abort)
1976 lp->tt.seq_exch_abort = fc_seq_exch_abort;
1977
1978 return 0;
1979}
1980EXPORT_SYMBOL(fc_exch_init);
1981
1982int fc_setup_exch_mgr(void)
1983{
1984 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
1985 0, SLAB_HWCACHE_ALIGN, NULL);
1986 if (!fc_em_cachep)
1987 return -ENOMEM;
1988 return 0;
1989}
1990
1991void fc_destroy_exch_mgr(void)
1992{
1993 kmem_cache_destroy(fc_em_cachep);
1994}