blob: 324589a5cc03bdd9df5357e2cc5ad13696b6a2e6 [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 */
61 u16 last_xid; /* last allocated exchange ID */
62 u16 min_xid; /* min exchange ID */
63 u16 max_xid; /* max exchange ID */
64 u16 max_read; /* max exchange ID for read */
65 u16 last_read; /* last xid allocated for read */
66 u32 total_exches; /* total allocated exchanges */
67 struct list_head ex_list; /* allocated exchanges list */
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 Love74147052009-06-10 15:31:10 -0700329 FC_EXCH_DBG(ep, "Exchange timed out, notifying the upper layer\n");
330
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
415 spin_lock_bh(&ep->ex_lock);
416 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
417 goto unlock;
418
419 e_stat = ep->esb_stat;
420 if (e_stat & ESB_ST_COMPLETE) {
421 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
Vasu Deva0cc1ec2009-07-28 17:33:37 -0700422 spin_unlock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -0800423 if (e_stat & ESB_ST_REC_QUAL)
424 fc_exch_rrq(ep);
Robert Love42e9a922008-12-09 15:10:17 -0800425 goto done;
426 } else {
427 resp = ep->resp;
428 arg = ep->arg;
429 ep->resp = NULL;
430 if (e_stat & ESB_ST_ABNORMAL)
431 rc = fc_exch_done_locked(ep);
432 spin_unlock_bh(&ep->ex_lock);
433 if (!rc)
434 fc_exch_mgr_delete_ep(ep);
435 if (resp)
436 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
437 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
438 goto done;
439 }
440unlock:
441 spin_unlock_bh(&ep->ex_lock);
442done:
443 /*
444 * This release matches the hold taken when the timer was set.
445 */
446 fc_exch_release(ep);
447}
448
449/*
450 * Allocate a sequence.
451 *
452 * We don't support multiple originated sequences on the same exchange.
453 * By implication, any previously originated sequence on this exchange
454 * is complete, and we reallocate the same sequence.
455 */
456static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
457{
458 struct fc_seq *sp;
459
460 sp = &ep->seq;
461 sp->ssb_stat = 0;
462 sp->cnt = 0;
463 sp->id = seq_id;
464 return sp;
465}
466
467/*
468 * fc_em_alloc_xid - returns an xid based on request type
469 * @lp : ptr to associated lport
470 * @fp : ptr to the assocated frame
471 *
472 * check the associated fc_fsp_pkt to get scsi command type and
473 * command direction to decide from which range this exch id
474 * will be allocated from.
475 *
476 * Returns : 0 or an valid xid
477 */
478static u16 fc_em_alloc_xid(struct fc_exch_mgr *mp, const struct fc_frame *fp)
479{
480 u16 xid, min, max;
481 u16 *plast;
482 struct fc_exch *ep = NULL;
483
484 if (mp->max_read) {
Yi Zoub277d2a2009-02-27 14:07:21 -0800485 if (fc_fcp_is_read(fr_fsp(fp))) {
Robert Love42e9a922008-12-09 15:10:17 -0800486 min = mp->min_xid;
487 max = mp->max_read;
488 plast = &mp->last_read;
489 } else {
490 min = mp->max_read + 1;
491 max = mp->max_xid;
492 plast = &mp->last_xid;
493 }
494 } else {
495 min = mp->min_xid;
496 max = mp->max_xid;
497 plast = &mp->last_xid;
498 }
499 xid = *plast;
500 do {
501 xid = (xid == max) ? min : xid + 1;
502 ep = mp->exches[xid - mp->min_xid];
503 } while ((ep != NULL) && (xid != *plast));
504
505 if (unlikely(ep))
506 xid = 0;
507 else
508 *plast = xid;
509
510 return xid;
511}
512
Vasu Dev52ff8782009-07-29 17:05:10 -0700513/**
514 * fc_exch_em_alloc() - allocate an exchange from a specified EM.
515 * @lport: ptr to the local port
516 * @mp: ptr to the exchange manager
517 * @fp: ptr to the FC frame
518 * @xid: input xid
Robert Love42e9a922008-12-09 15:10:17 -0800519 *
520 * if xid is supplied zero then assign next free exchange ID
521 * from exchange manager, otherwise use supplied xid.
522 * Returns with exch lock held.
523 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700524static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
525 struct fc_exch_mgr *mp,
526 struct fc_frame *fp, u16 xid)
Robert Love42e9a922008-12-09 15:10:17 -0800527{
528 struct fc_exch *ep;
529
530 /* allocate memory for exchange */
531 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
532 if (!ep) {
533 atomic_inc(&mp->stats.no_free_exch);
534 goto out;
535 }
536 memset(ep, 0, sizeof(*ep));
537
538 spin_lock_bh(&mp->em_lock);
539 /* alloc xid if input xid 0 */
540 if (!xid) {
541 /* alloc a new xid */
542 xid = fc_em_alloc_xid(mp, fp);
543 if (!xid) {
Robert Love74147052009-06-10 15:31:10 -0700544 printk(KERN_WARNING "libfc: Failed to allocate an exhange\n");
Robert Love42e9a922008-12-09 15:10:17 -0800545 goto err;
546 }
547 }
548
549 fc_exch_hold(ep); /* hold for exch in mp */
550 spin_lock_init(&ep->ex_lock);
551 /*
552 * Hold exch lock for caller to prevent fc_exch_reset()
553 * from releasing exch while fc_exch_alloc() caller is
554 * still working on exch.
555 */
556 spin_lock_bh(&ep->ex_lock);
557
558 mp->exches[xid - mp->min_xid] = ep;
559 list_add_tail(&ep->ex_list, &mp->ex_list);
560 fc_seq_alloc(ep, ep->seq_id++);
561 mp->total_exches++;
562 spin_unlock_bh(&mp->em_lock);
563
564 /*
565 * update exchange
566 */
567 ep->oxid = ep->xid = xid;
568 ep->em = mp;
Vasu Dev52ff8782009-07-29 17:05:10 -0700569 ep->lp = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800570 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
571 ep->rxid = FC_XID_UNKNOWN;
572 ep->class = mp->class;
573 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
574out:
575 return ep;
576err:
577 spin_unlock_bh(&mp->em_lock);
578 atomic_inc(&mp->stats.no_free_exch_xid);
579 mempool_free(ep, mp->ep_pool);
580 return NULL;
581}
Vasu Dev52ff8782009-07-29 17:05:10 -0700582
583/**
584 * fc_exch_alloc() - allocate an exchange.
585 * @lport: ptr to the local port
586 * @fp: ptr to the FC frame
587 *
588 * This function walks the list of the exchange manager(EM)
589 * anchors to select a EM for new exchange allocation. The
590 * EM is selected having either a NULL match function pointer
591 * or call to match function returning true.
592 */
593struct fc_exch *fc_exch_alloc(struct fc_lport *lport, struct fc_frame *fp)
594{
595 struct fc_exch_mgr_anchor *ema;
596 struct fc_exch *ep;
597
598 list_for_each_entry(ema, &lport->ema_list, ema_list) {
599 if (!ema->match || ema->match(fp)) {
600 ep = fc_exch_em_alloc(lport, ema->mp, fp, 0);
601 if (ep)
602 return ep;
603 }
604 }
605 return NULL;
606}
Robert Love42e9a922008-12-09 15:10:17 -0800607EXPORT_SYMBOL(fc_exch_alloc);
608
609/*
610 * Lookup and hold an exchange.
611 */
612static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
613{
614 struct fc_exch *ep = NULL;
615
616 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
617 spin_lock_bh(&mp->em_lock);
618 ep = mp->exches[xid - mp->min_xid];
619 if (ep) {
620 fc_exch_hold(ep);
621 WARN_ON(ep->xid != xid);
622 }
623 spin_unlock_bh(&mp->em_lock);
624 }
625 return ep;
626}
627
628void fc_exch_done(struct fc_seq *sp)
629{
630 struct fc_exch *ep = fc_seq_exch(sp);
631 int rc;
632
633 spin_lock_bh(&ep->ex_lock);
634 rc = fc_exch_done_locked(ep);
635 spin_unlock_bh(&ep->ex_lock);
636 if (!rc)
637 fc_exch_mgr_delete_ep(ep);
638}
639EXPORT_SYMBOL(fc_exch_done);
640
641/*
642 * Allocate a new exchange as responder.
643 * Sets the responder ID in the frame header.
644 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700645static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
646 struct fc_exch_mgr *mp,
647 struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800648{
649 struct fc_exch *ep;
650 struct fc_frame_header *fh;
Robert Love42e9a922008-12-09 15:10:17 -0800651
Vasu Dev52ff8782009-07-29 17:05:10 -0700652 ep = fc_exch_alloc(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800653 if (ep) {
654 ep->class = fc_frame_class(fp);
655
656 /*
657 * Set EX_CTX indicating we're responding on this exchange.
658 */
659 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
660 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
661 fh = fc_frame_header_get(fp);
662 ep->sid = ntoh24(fh->fh_d_id);
663 ep->did = ntoh24(fh->fh_s_id);
664 ep->oid = ep->did;
665
666 /*
667 * Allocated exchange has placed the XID in the
668 * originator field. Move it to the responder field,
669 * and set the originator XID from the frame.
670 */
671 ep->rxid = ep->xid;
672 ep->oxid = ntohs(fh->fh_ox_id);
673 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
674 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
675 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
676
Robert Love42e9a922008-12-09 15:10:17 -0800677 fc_exch_hold(ep); /* hold for caller */
Vasu Dev52ff8782009-07-29 17:05:10 -0700678 spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */
Robert Love42e9a922008-12-09 15:10:17 -0800679 }
680 return ep;
681}
682
683/*
684 * Find a sequence for receive where the other end is originating the sequence.
685 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
686 * on the ep that should be released by the caller.
687 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700688static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
689 struct fc_exch_mgr *mp,
Robert Loveb2ab99c2009-02-27 10:55:50 -0800690 struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800691{
692 struct fc_frame_header *fh = fc_frame_header_get(fp);
693 struct fc_exch *ep = NULL;
694 struct fc_seq *sp = NULL;
695 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
696 u32 f_ctl;
697 u16 xid;
698
699 f_ctl = ntoh24(fh->fh_f_ctl);
700 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
701
702 /*
703 * Lookup or create the exchange if we will be creating the sequence.
704 */
705 if (f_ctl & FC_FC_EX_CTX) {
706 xid = ntohs(fh->fh_ox_id); /* we originated exch */
707 ep = fc_exch_find(mp, xid);
708 if (!ep) {
709 atomic_inc(&mp->stats.xid_not_found);
710 reject = FC_RJT_OX_ID;
711 goto out;
712 }
713 if (ep->rxid == FC_XID_UNKNOWN)
714 ep->rxid = ntohs(fh->fh_rx_id);
715 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
716 reject = FC_RJT_OX_ID;
717 goto rel;
718 }
719 } else {
720 xid = ntohs(fh->fh_rx_id); /* we are the responder */
721
722 /*
723 * Special case for MDS issuing an ELS TEST with a
724 * bad rxid of 0.
725 * XXX take this out once we do the proper reject.
726 */
727 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
728 fc_frame_payload_op(fp) == ELS_TEST) {
729 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
730 xid = FC_XID_UNKNOWN;
731 }
732
733 /*
734 * new sequence - find the exchange
735 */
736 ep = fc_exch_find(mp, xid);
737 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
738 if (ep) {
739 atomic_inc(&mp->stats.xid_busy);
740 reject = FC_RJT_RX_ID;
741 goto rel;
742 }
Vasu Dev52ff8782009-07-29 17:05:10 -0700743 ep = fc_exch_resp(lport, mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800744 if (!ep) {
745 reject = FC_RJT_EXCH_EST; /* XXX */
746 goto out;
747 }
748 xid = ep->xid; /* get our XID */
749 } else if (!ep) {
750 atomic_inc(&mp->stats.xid_not_found);
751 reject = FC_RJT_RX_ID; /* XID not found */
752 goto out;
753 }
754 }
755
756 /*
757 * At this point, we have the exchange held.
758 * Find or create the sequence.
759 */
760 if (fc_sof_is_init(fr_sof(fp))) {
761 sp = fc_seq_start_next(&ep->seq);
762 if (!sp) {
763 reject = FC_RJT_SEQ_XS; /* exchange shortage */
764 goto rel;
765 }
766 sp->id = fh->fh_seq_id;
767 sp->ssb_stat |= SSB_ST_RESP;
768 } else {
769 sp = &ep->seq;
770 if (sp->id != fh->fh_seq_id) {
771 atomic_inc(&mp->stats.seq_not_found);
772 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
773 goto rel;
774 }
775 }
776 WARN_ON(ep != fc_seq_exch(sp));
777
778 if (f_ctl & FC_FC_SEQ_INIT)
779 ep->esb_stat |= ESB_ST_SEQ_INIT;
780
781 fr_seq(fp) = sp;
782out:
783 return reject;
784rel:
785 fc_exch_done(&ep->seq);
786 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
787 return reject;
788}
789
790/*
791 * Find the sequence for a frame being received.
792 * We originated the sequence, so it should be found.
793 * We may or may not have originated the exchange.
794 * Does not hold the sequence for the caller.
795 */
796static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
797 struct fc_frame *fp)
798{
799 struct fc_frame_header *fh = fc_frame_header_get(fp);
800 struct fc_exch *ep;
801 struct fc_seq *sp = NULL;
802 u32 f_ctl;
803 u16 xid;
804
805 f_ctl = ntoh24(fh->fh_f_ctl);
806 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
807 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
808 ep = fc_exch_find(mp, xid);
809 if (!ep)
810 return NULL;
811 if (ep->seq.id == fh->fh_seq_id) {
812 /*
813 * Save the RX_ID if we didn't previously know it.
814 */
815 sp = &ep->seq;
816 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
817 ep->rxid == FC_XID_UNKNOWN) {
818 ep->rxid = ntohs(fh->fh_rx_id);
819 }
820 }
821 fc_exch_release(ep);
822 return sp;
823}
824
825/*
826 * Set addresses for an exchange.
827 * Note this must be done before the first sequence of the exchange is sent.
828 */
829static void fc_exch_set_addr(struct fc_exch *ep,
830 u32 orig_id, u32 resp_id)
831{
832 ep->oid = orig_id;
833 if (ep->esb_stat & ESB_ST_RESP) {
834 ep->sid = resp_id;
835 ep->did = orig_id;
836 } else {
837 ep->sid = orig_id;
838 ep->did = resp_id;
839 }
840}
841
842static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
843{
844 struct fc_exch *ep = fc_seq_exch(sp);
845
846 sp = fc_seq_alloc(ep, ep->seq_id++);
Robert Love74147052009-06-10 15:31:10 -0700847 FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
848 ep->f_ctl, sp->id);
Robert Love42e9a922008-12-09 15:10:17 -0800849 return sp;
850}
851/*
852 * Allocate a new sequence on the same exchange as the supplied sequence.
853 * This will never return NULL.
854 */
855struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
856{
857 struct fc_exch *ep = fc_seq_exch(sp);
858
859 spin_lock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -0800860 sp = fc_seq_start_next_locked(sp);
861 spin_unlock_bh(&ep->ex_lock);
862
863 return sp;
864}
865EXPORT_SYMBOL(fc_seq_start_next);
866
867int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
868{
869 struct fc_exch *ep;
870 struct fc_frame_header *fh = fc_frame_header_get(fp);
871 int error;
872 u32 f_ctl;
873
874 ep = fc_seq_exch(sp);
875 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
876
877 f_ctl = ntoh24(fh->fh_f_ctl);
878 fc_exch_setup_hdr(ep, fp, f_ctl);
879
880 /*
881 * update sequence count if this frame is carrying
882 * multiple FC frames when sequence offload is enabled
883 * by LLD.
884 */
885 if (fr_max_payload(fp))
886 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
887 fr_max_payload(fp));
888 else
889 sp->cnt++;
890
891 /*
892 * Send the frame.
893 */
894 error = lp->tt.frame_send(lp, fp);
895
896 /*
897 * Update the exchange and sequence flags,
898 * assuming all frames for the sequence have been sent.
899 * We can only be called to send once for each sequence.
900 */
901 spin_lock_bh(&ep->ex_lock);
902 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
903 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
904 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
905 spin_unlock_bh(&ep->ex_lock);
906 return error;
907}
908EXPORT_SYMBOL(fc_seq_send);
909
910void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
911 struct fc_seq_els_data *els_data)
912{
913 switch (els_cmd) {
914 case ELS_LS_RJT:
915 fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
916 break;
917 case ELS_LS_ACC:
918 fc_seq_ls_acc(sp);
919 break;
920 case ELS_RRQ:
921 fc_exch_els_rrq(sp, els_data->fp);
922 break;
923 case ELS_REC:
924 fc_exch_els_rec(sp, els_data->fp);
925 break;
926 default:
Robert Love74147052009-06-10 15:31:10 -0700927 FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
Robert Love42e9a922008-12-09 15:10:17 -0800928 }
929}
930EXPORT_SYMBOL(fc_seq_els_rsp_send);
931
932/*
933 * Send a sequence, which is also the last sequence in the exchange.
934 */
935static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
936 enum fc_rctl rctl, enum fc_fh_type fh_type)
937{
938 u32 f_ctl;
939 struct fc_exch *ep = fc_seq_exch(sp);
940
941 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
942 f_ctl |= ep->f_ctl;
943 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
944 fc_seq_send(ep->lp, sp, fp);
945}
946
947/*
948 * Send ACK_1 (or equiv.) indicating we received something.
949 * The frame we're acking is supplied.
950 */
951static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
952{
953 struct fc_frame *fp;
954 struct fc_frame_header *rx_fh;
955 struct fc_frame_header *fh;
956 struct fc_exch *ep = fc_seq_exch(sp);
957 struct fc_lport *lp = ep->lp;
958 unsigned int f_ctl;
959
960 /*
961 * Don't send ACKs for class 3.
962 */
963 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
964 fp = fc_frame_alloc(lp, 0);
965 if (!fp)
966 return;
967
968 fh = fc_frame_header_get(fp);
969 fh->fh_r_ctl = FC_RCTL_ACK_1;
970 fh->fh_type = FC_TYPE_BLS;
971
972 /*
973 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
974 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
975 * Bits 9-8 are meaningful (retransmitted or unidirectional).
976 * Last ACK uses bits 7-6 (continue sequence),
977 * bits 5-4 are meaningful (what kind of ACK to use).
978 */
979 rx_fh = fc_frame_header_get(rx_fp);
980 f_ctl = ntoh24(rx_fh->fh_f_ctl);
981 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
982 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
983 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
984 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
985 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
986 hton24(fh->fh_f_ctl, f_ctl);
987
988 fc_exch_setup_hdr(ep, fp, f_ctl);
989 fh->fh_seq_id = rx_fh->fh_seq_id;
990 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
991 fh->fh_parm_offset = htonl(1); /* ack single frame */
992
993 fr_sof(fp) = fr_sof(rx_fp);
994 if (f_ctl & FC_FC_END_SEQ)
995 fr_eof(fp) = FC_EOF_T;
996 else
997 fr_eof(fp) = FC_EOF_N;
998
999 (void) lp->tt.frame_send(lp, fp);
1000 }
1001}
1002
1003/*
1004 * Send BLS Reject.
1005 * This is for rejecting BA_ABTS only.
1006 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001007static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1008 enum fc_ba_rjt_reason reason,
1009 enum fc_ba_rjt_explan explan)
Robert Love42e9a922008-12-09 15:10:17 -08001010{
1011 struct fc_frame *fp;
1012 struct fc_frame_header *rx_fh;
1013 struct fc_frame_header *fh;
1014 struct fc_ba_rjt *rp;
1015 struct fc_lport *lp;
1016 unsigned int f_ctl;
1017
1018 lp = fr_dev(rx_fp);
1019 fp = fc_frame_alloc(lp, sizeof(*rp));
1020 if (!fp)
1021 return;
1022 fh = fc_frame_header_get(fp);
1023 rx_fh = fc_frame_header_get(rx_fp);
1024
1025 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
1026
1027 rp = fc_frame_payload_get(fp, sizeof(*rp));
1028 rp->br_reason = reason;
1029 rp->br_explan = explan;
1030
1031 /*
1032 * seq_id, cs_ctl, df_ctl and param/offset are zero.
1033 */
1034 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
1035 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
1036 fh->fh_ox_id = rx_fh->fh_rx_id;
1037 fh->fh_rx_id = rx_fh->fh_ox_id;
1038 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1039 fh->fh_r_ctl = FC_RCTL_BA_RJT;
1040 fh->fh_type = FC_TYPE_BLS;
1041
1042 /*
1043 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
1044 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
1045 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1046 * Last ACK uses bits 7-6 (continue sequence),
1047 * bits 5-4 are meaningful (what kind of ACK to use).
1048 * Always set LAST_SEQ, END_SEQ.
1049 */
1050 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1051 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1052 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1053 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1054 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1055 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1056 f_ctl &= ~FC_FC_FIRST_SEQ;
1057 hton24(fh->fh_f_ctl, f_ctl);
1058
1059 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1060 fr_eof(fp) = FC_EOF_T;
1061 if (fc_sof_needs_ack(fr_sof(fp)))
1062 fr_eof(fp) = FC_EOF_N;
1063
1064 (void) lp->tt.frame_send(lp, fp);
1065}
1066
1067/*
1068 * Handle an incoming ABTS. This would be for target mode usually,
1069 * but could be due to lost FCP transfer ready, confirm or RRQ.
1070 * We always handle this as an exchange abort, ignoring the parameter.
1071 */
1072static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1073{
1074 struct fc_frame *fp;
1075 struct fc_ba_acc *ap;
1076 struct fc_frame_header *fh;
1077 struct fc_seq *sp;
1078
1079 if (!ep)
1080 goto reject;
1081 spin_lock_bh(&ep->ex_lock);
1082 if (ep->esb_stat & ESB_ST_COMPLETE) {
1083 spin_unlock_bh(&ep->ex_lock);
1084 goto reject;
1085 }
1086 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1087 fc_exch_hold(ep); /* hold for REC_QUAL */
1088 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1089 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1090
1091 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1092 if (!fp) {
1093 spin_unlock_bh(&ep->ex_lock);
1094 goto free;
1095 }
1096 fh = fc_frame_header_get(fp);
1097 ap = fc_frame_payload_get(fp, sizeof(*ap));
1098 memset(ap, 0, sizeof(*ap));
1099 sp = &ep->seq;
1100 ap->ba_high_seq_cnt = htons(0xffff);
1101 if (sp->ssb_stat & SSB_ST_RESP) {
1102 ap->ba_seq_id = sp->id;
1103 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1104 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1105 ap->ba_low_seq_cnt = htons(sp->cnt);
1106 }
Vasu Deva7e84f22009-02-27 10:54:51 -08001107 sp = fc_seq_start_next_locked(sp);
Robert Love42e9a922008-12-09 15:10:17 -08001108 spin_unlock_bh(&ep->ex_lock);
1109 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1110 fc_frame_free(rx_fp);
1111 return;
1112
1113reject:
1114 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1115free:
1116 fc_frame_free(rx_fp);
1117}
1118
1119/*
1120 * Handle receive where the other end is originating the sequence.
1121 */
1122static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1123 struct fc_frame *fp)
1124{
1125 struct fc_frame_header *fh = fc_frame_header_get(fp);
1126 struct fc_seq *sp = NULL;
1127 struct fc_exch *ep = NULL;
1128 enum fc_sof sof;
1129 enum fc_eof eof;
1130 u32 f_ctl;
1131 enum fc_pf_rjt_reason reject;
1132
1133 fr_seq(fp) = NULL;
Vasu Dev52ff8782009-07-29 17:05:10 -07001134 reject = fc_seq_lookup_recip(lp, mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001135 if (reject == FC_RJT_NONE) {
1136 sp = fr_seq(fp); /* sequence will be held */
1137 ep = fc_seq_exch(sp);
1138 sof = fr_sof(fp);
1139 eof = fr_eof(fp);
1140 f_ctl = ntoh24(fh->fh_f_ctl);
1141 fc_seq_send_ack(sp, fp);
1142
1143 /*
1144 * Call the receive function.
1145 *
1146 * The receive function may allocate a new sequence
1147 * over the old one, so we shouldn't change the
1148 * sequence after this.
1149 *
1150 * The frame will be freed by the receive function.
1151 * If new exch resp handler is valid then call that
1152 * first.
1153 */
1154 if (ep->resp)
1155 ep->resp(sp, fp, ep->arg);
1156 else
1157 lp->tt.lport_recv(lp, sp, fp);
1158 fc_exch_release(ep); /* release from lookup */
1159 } else {
Robert Loved459b7e2009-07-29 17:05:05 -07001160 FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject);
Robert Love42e9a922008-12-09 15:10:17 -08001161 fc_frame_free(fp);
1162 }
1163}
1164
1165/*
1166 * Handle receive where the other end is originating the sequence in
1167 * response to our exchange.
1168 */
1169static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1170{
1171 struct fc_frame_header *fh = fc_frame_header_get(fp);
1172 struct fc_seq *sp;
1173 struct fc_exch *ep;
1174 enum fc_sof sof;
1175 u32 f_ctl;
1176 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1177 void *ex_resp_arg;
1178 int rc;
1179
1180 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1181 if (!ep) {
1182 atomic_inc(&mp->stats.xid_not_found);
1183 goto out;
1184 }
Steve Ma30121d12009-05-06 10:52:29 -07001185 if (ep->esb_stat & ESB_ST_COMPLETE) {
1186 atomic_inc(&mp->stats.xid_not_found);
1187 goto out;
1188 }
Robert Love42e9a922008-12-09 15:10:17 -08001189 if (ep->rxid == FC_XID_UNKNOWN)
1190 ep->rxid = ntohs(fh->fh_rx_id);
1191 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1192 atomic_inc(&mp->stats.xid_not_found);
1193 goto rel;
1194 }
1195 if (ep->did != ntoh24(fh->fh_s_id) &&
1196 ep->did != FC_FID_FLOGI) {
1197 atomic_inc(&mp->stats.xid_not_found);
1198 goto rel;
1199 }
1200 sof = fr_sof(fp);
1201 if (fc_sof_is_init(sof)) {
1202 sp = fc_seq_start_next(&ep->seq);
1203 sp->id = fh->fh_seq_id;
1204 sp->ssb_stat |= SSB_ST_RESP;
1205 } else {
1206 sp = &ep->seq;
1207 if (sp->id != fh->fh_seq_id) {
1208 atomic_inc(&mp->stats.seq_not_found);
1209 goto rel;
1210 }
1211 }
1212 f_ctl = ntoh24(fh->fh_f_ctl);
1213 fr_seq(fp) = sp;
1214 if (f_ctl & FC_FC_SEQ_INIT)
1215 ep->esb_stat |= ESB_ST_SEQ_INIT;
1216
1217 if (fc_sof_needs_ack(sof))
1218 fc_seq_send_ack(sp, fp);
1219 resp = ep->resp;
1220 ex_resp_arg = ep->arg;
1221
1222 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1223 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1224 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1225 spin_lock_bh(&ep->ex_lock);
1226 rc = fc_exch_done_locked(ep);
1227 WARN_ON(fc_seq_exch(sp) != ep);
1228 spin_unlock_bh(&ep->ex_lock);
1229 if (!rc)
1230 fc_exch_mgr_delete_ep(ep);
1231 }
1232
1233 /*
1234 * Call the receive function.
1235 * The sequence is held (has a refcnt) for us,
1236 * but not for the receive function.
1237 *
1238 * The receive function may allocate a new sequence
1239 * over the old one, so we shouldn't change the
1240 * sequence after this.
1241 *
1242 * The frame will be freed by the receive function.
1243 * If new exch resp handler is valid then call that
1244 * first.
1245 */
1246 if (resp)
1247 resp(sp, fp, ex_resp_arg);
1248 else
1249 fc_frame_free(fp);
1250 fc_exch_release(ep);
1251 return;
1252rel:
1253 fc_exch_release(ep);
1254out:
1255 fc_frame_free(fp);
1256}
1257
1258/*
1259 * Handle receive for a sequence where other end is responding to our sequence.
1260 */
1261static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1262{
1263 struct fc_seq *sp;
1264
1265 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
Robert Loved459b7e2009-07-29 17:05:05 -07001266
1267 if (!sp)
Robert Love42e9a922008-12-09 15:10:17 -08001268 atomic_inc(&mp->stats.xid_not_found);
Robert Loved459b7e2009-07-29 17:05:05 -07001269 else
Robert Love42e9a922008-12-09 15:10:17 -08001270 atomic_inc(&mp->stats.non_bls_resp);
Robert Loved459b7e2009-07-29 17:05:05 -07001271
Robert Love42e9a922008-12-09 15:10:17 -08001272 fc_frame_free(fp);
1273}
1274
1275/*
1276 * Handle the response to an ABTS for exchange or sequence.
1277 * This can be BA_ACC or BA_RJT.
1278 */
1279static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1280{
1281 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1282 void *ex_resp_arg;
1283 struct fc_frame_header *fh;
1284 struct fc_ba_acc *ap;
1285 struct fc_seq *sp;
1286 u16 low;
1287 u16 high;
1288 int rc = 1, has_rec = 0;
1289
1290 fh = fc_frame_header_get(fp);
Robert Love74147052009-06-10 15:31:10 -07001291 FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
1292 fc_exch_rctl_name(fh->fh_r_ctl));
Robert Love42e9a922008-12-09 15:10:17 -08001293
1294 if (cancel_delayed_work_sync(&ep->timeout_work))
1295 fc_exch_release(ep); /* release from pending timer hold */
1296
1297 spin_lock_bh(&ep->ex_lock);
1298 switch (fh->fh_r_ctl) {
1299 case FC_RCTL_BA_ACC:
1300 ap = fc_frame_payload_get(fp, sizeof(*ap));
1301 if (!ap)
1302 break;
1303
1304 /*
1305 * Decide whether to establish a Recovery Qualifier.
1306 * We do this if there is a non-empty SEQ_CNT range and
1307 * SEQ_ID is the same as the one we aborted.
1308 */
1309 low = ntohs(ap->ba_low_seq_cnt);
1310 high = ntohs(ap->ba_high_seq_cnt);
1311 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1312 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1313 ap->ba_seq_id == ep->seq_id) && low != high) {
1314 ep->esb_stat |= ESB_ST_REC_QUAL;
1315 fc_exch_hold(ep); /* hold for recovery qualifier */
1316 has_rec = 1;
1317 }
1318 break;
1319 case FC_RCTL_BA_RJT:
1320 break;
1321 default:
1322 break;
1323 }
1324
1325 resp = ep->resp;
1326 ex_resp_arg = ep->arg;
1327
1328 /* do we need to do some other checks here. Can we reuse more of
1329 * fc_exch_recv_seq_resp
1330 */
1331 sp = &ep->seq;
1332 /*
1333 * do we want to check END_SEQ as well as LAST_SEQ here?
1334 */
1335 if (ep->fh_type != FC_TYPE_FCP &&
1336 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1337 rc = fc_exch_done_locked(ep);
1338 spin_unlock_bh(&ep->ex_lock);
1339 if (!rc)
1340 fc_exch_mgr_delete_ep(ep);
1341
1342 if (resp)
1343 resp(sp, fp, ex_resp_arg);
1344 else
1345 fc_frame_free(fp);
1346
1347 if (has_rec)
1348 fc_exch_timer_set(ep, ep->r_a_tov);
1349
1350}
1351
1352/*
1353 * Receive BLS sequence.
1354 * This is always a sequence initiated by the remote side.
1355 * We may be either the originator or recipient of the exchange.
1356 */
1357static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1358{
1359 struct fc_frame_header *fh;
1360 struct fc_exch *ep;
1361 u32 f_ctl;
1362
1363 fh = fc_frame_header_get(fp);
1364 f_ctl = ntoh24(fh->fh_f_ctl);
1365 fr_seq(fp) = NULL;
1366
1367 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1368 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1369 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1370 spin_lock_bh(&ep->ex_lock);
1371 ep->esb_stat |= ESB_ST_SEQ_INIT;
1372 spin_unlock_bh(&ep->ex_lock);
1373 }
1374 if (f_ctl & FC_FC_SEQ_CTX) {
1375 /*
1376 * A response to a sequence we initiated.
1377 * This should only be ACKs for class 2 or F.
1378 */
1379 switch (fh->fh_r_ctl) {
1380 case FC_RCTL_ACK_1:
1381 case FC_RCTL_ACK_0:
1382 break;
1383 default:
Robert Love74147052009-06-10 15:31:10 -07001384 FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
1385 fh->fh_r_ctl,
1386 fc_exch_rctl_name(fh->fh_r_ctl));
Robert Love42e9a922008-12-09 15:10:17 -08001387 break;
1388 }
1389 fc_frame_free(fp);
1390 } else {
1391 switch (fh->fh_r_ctl) {
1392 case FC_RCTL_BA_RJT:
1393 case FC_RCTL_BA_ACC:
1394 if (ep)
1395 fc_exch_abts_resp(ep, fp);
1396 else
1397 fc_frame_free(fp);
1398 break;
1399 case FC_RCTL_BA_ABTS:
1400 fc_exch_recv_abts(ep, fp);
1401 break;
1402 default: /* ignore junk */
1403 fc_frame_free(fp);
1404 break;
1405 }
1406 }
1407 if (ep)
1408 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1409}
1410
1411/*
1412 * Accept sequence with LS_ACC.
1413 * If this fails due to allocation or transmit congestion, assume the
1414 * originator will repeat the sequence.
1415 */
1416static void fc_seq_ls_acc(struct fc_seq *req_sp)
1417{
1418 struct fc_seq *sp;
1419 struct fc_els_ls_acc *acc;
1420 struct fc_frame *fp;
1421
1422 sp = fc_seq_start_next(req_sp);
1423 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1424 if (fp) {
1425 acc = fc_frame_payload_get(fp, sizeof(*acc));
1426 memset(acc, 0, sizeof(*acc));
1427 acc->la_cmd = ELS_LS_ACC;
1428 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1429 }
1430}
1431
1432/*
1433 * Reject sequence with ELS LS_RJT.
1434 * If this fails due to allocation or transmit congestion, assume the
1435 * originator will repeat the sequence.
1436 */
1437static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
1438 enum fc_els_rjt_explan explan)
1439{
1440 struct fc_seq *sp;
1441 struct fc_els_ls_rjt *rjt;
1442 struct fc_frame *fp;
1443
1444 sp = fc_seq_start_next(req_sp);
1445 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
1446 if (fp) {
1447 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1448 memset(rjt, 0, sizeof(*rjt));
1449 rjt->er_cmd = ELS_LS_RJT;
1450 rjt->er_reason = reason;
1451 rjt->er_explan = explan;
1452 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1453 }
1454}
1455
1456static void fc_exch_reset(struct fc_exch *ep)
1457{
1458 struct fc_seq *sp;
1459 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1460 void *arg;
1461 int rc = 1;
1462
1463 spin_lock_bh(&ep->ex_lock);
1464 ep->state |= FC_EX_RST_CLEANUP;
1465 /*
1466 * we really want to call del_timer_sync, but cannot due
1467 * to the lport calling with the lport lock held (some resp
1468 * functions can also grab the lport lock which could cause
1469 * a deadlock).
1470 */
1471 if (cancel_delayed_work(&ep->timeout_work))
1472 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1473 resp = ep->resp;
1474 ep->resp = NULL;
1475 if (ep->esb_stat & ESB_ST_REC_QUAL)
1476 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1477 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1478 arg = ep->arg;
1479 sp = &ep->seq;
1480 rc = fc_exch_done_locked(ep);
1481 spin_unlock_bh(&ep->ex_lock);
1482 if (!rc)
1483 fc_exch_mgr_delete_ep(ep);
1484
1485 if (resp)
1486 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1487}
1488
1489/*
1490 * Reset an exchange manager, releasing all sequences and exchanges.
1491 * If sid is non-zero, reset only exchanges we source from that FID.
1492 * If did is non-zero, reset only exchanges destined to that FID.
1493 */
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001494void fc_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
Robert Love42e9a922008-12-09 15:10:17 -08001495{
1496 struct fc_exch *ep;
1497 struct fc_exch *next;
Vasu Dev52ff8782009-07-29 17:05:10 -07001498 struct fc_exch_mgr *mp;
1499 struct fc_exch_mgr_anchor *ema;
Robert Love42e9a922008-12-09 15:10:17 -08001500
Vasu Dev52ff8782009-07-29 17:05:10 -07001501 list_for_each_entry(ema, &lp->ema_list, ema_list) {
1502 mp = ema->mp;
1503 spin_lock_bh(&mp->em_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001504restart:
Vasu Dev52ff8782009-07-29 17:05:10 -07001505 list_for_each_entry_safe(ep, next, &mp->ex_list, ex_list) {
1506 if ((lp == ep->lp) &&
1507 (sid == 0 || sid == ep->sid) &&
1508 (did == 0 || did == ep->did)) {
1509 fc_exch_hold(ep);
1510 spin_unlock_bh(&mp->em_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001511
Vasu Dev52ff8782009-07-29 17:05:10 -07001512 fc_exch_reset(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001513
Vasu Dev52ff8782009-07-29 17:05:10 -07001514 fc_exch_release(ep);
1515 spin_lock_bh(&mp->em_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001516
Vasu Dev52ff8782009-07-29 17:05:10 -07001517 /*
1518 * must restart loop incase while lock
1519 * was down multiple eps were released.
1520 */
1521 goto restart;
1522 }
Robert Love42e9a922008-12-09 15:10:17 -08001523 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001524 spin_unlock_bh(&mp->em_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001525 }
Robert Love42e9a922008-12-09 15:10:17 -08001526}
1527EXPORT_SYMBOL(fc_exch_mgr_reset);
1528
1529/*
1530 * Handle incoming ELS REC - Read Exchange Concise.
1531 * Note that the requesting port may be different than the S_ID in the request.
1532 */
1533static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
1534{
1535 struct fc_frame *fp;
1536 struct fc_exch *ep;
1537 struct fc_exch_mgr *em;
1538 struct fc_els_rec *rp;
1539 struct fc_els_rec_acc *acc;
1540 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1541 enum fc_els_rjt_explan explan;
1542 u32 sid;
1543 u16 rxid;
1544 u16 oxid;
1545
1546 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1547 explan = ELS_EXPL_INV_LEN;
1548 if (!rp)
1549 goto reject;
1550 sid = ntoh24(rp->rec_s_id);
1551 rxid = ntohs(rp->rec_rx_id);
1552 oxid = ntohs(rp->rec_ox_id);
1553
1554 /*
1555 * Currently it's hard to find the local S_ID from the exchange
1556 * manager. This will eventually be fixed, but for now it's easier
1557 * to lookup the subject exchange twice, once as if we were
1558 * the initiator, and then again if we weren't.
1559 */
1560 em = fc_seq_exch(sp)->em;
1561 ep = fc_exch_find(em, oxid);
1562 explan = ELS_EXPL_OXID_RXID;
1563 if (ep && ep->oid == sid) {
1564 if (ep->rxid != FC_XID_UNKNOWN &&
1565 rxid != FC_XID_UNKNOWN &&
1566 ep->rxid != rxid)
1567 goto rel;
1568 } else {
1569 if (ep)
1570 fc_exch_release(ep);
1571 ep = NULL;
1572 if (rxid != FC_XID_UNKNOWN)
1573 ep = fc_exch_find(em, rxid);
1574 if (!ep)
1575 goto reject;
1576 }
1577
1578 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1579 if (!fp) {
1580 fc_exch_done(sp);
1581 goto out;
1582 }
1583 sp = fc_seq_start_next(sp);
1584 acc = fc_frame_payload_get(fp, sizeof(*acc));
1585 memset(acc, 0, sizeof(*acc));
1586 acc->reca_cmd = ELS_LS_ACC;
1587 acc->reca_ox_id = rp->rec_ox_id;
1588 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1589 acc->reca_rx_id = htons(ep->rxid);
1590 if (ep->sid == ep->oid)
1591 hton24(acc->reca_rfid, ep->did);
1592 else
1593 hton24(acc->reca_rfid, ep->sid);
1594 acc->reca_fc4value = htonl(ep->seq.rec_data);
1595 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1596 ESB_ST_SEQ_INIT |
1597 ESB_ST_COMPLETE));
1598 sp = fc_seq_start_next(sp);
1599 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1600out:
1601 fc_exch_release(ep);
1602 fc_frame_free(rfp);
1603 return;
1604
1605rel:
1606 fc_exch_release(ep);
1607reject:
1608 fc_seq_ls_rjt(sp, reason, explan);
1609 fc_frame_free(rfp);
1610}
1611
1612/*
1613 * Handle response from RRQ.
1614 * Not much to do here, really.
1615 * Should report errors.
1616 *
1617 * TODO: fix error handler.
1618 */
1619static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1620{
1621 struct fc_exch *aborted_ep = arg;
1622 unsigned int op;
1623
1624 if (IS_ERR(fp)) {
1625 int err = PTR_ERR(fp);
1626
Vasu Dev78342da2009-02-27 10:54:46 -08001627 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
Robert Love42e9a922008-12-09 15:10:17 -08001628 goto cleanup;
Robert Love74147052009-06-10 15:31:10 -07001629 FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
1630 "frame error %d\n", err);
Robert Love42e9a922008-12-09 15:10:17 -08001631 return;
1632 }
1633
1634 op = fc_frame_payload_op(fp);
1635 fc_frame_free(fp);
1636
1637 switch (op) {
1638 case ELS_LS_RJT:
Robert Love74147052009-06-10 15:31:10 -07001639 FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
Robert Love42e9a922008-12-09 15:10:17 -08001640 /* fall through */
1641 case ELS_LS_ACC:
1642 goto cleanup;
1643 default:
Robert Love74147052009-06-10 15:31:10 -07001644 FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
1645 "for RRQ", op);
Robert Love42e9a922008-12-09 15:10:17 -08001646 return;
1647 }
1648
1649cleanup:
1650 fc_exch_done(&aborted_ep->seq);
1651 /* drop hold for rec qual */
1652 fc_exch_release(aborted_ep);
1653}
1654
1655/*
1656 * Send ELS RRQ - Reinstate Recovery Qualifier.
1657 * This tells the remote port to stop blocking the use of
1658 * the exchange and the seq_cnt range.
1659 */
1660static void fc_exch_rrq(struct fc_exch *ep)
1661{
1662 struct fc_lport *lp;
1663 struct fc_els_rrq *rrq;
1664 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001665 u32 did;
1666
1667 lp = ep->lp;
1668
1669 fp = fc_frame_alloc(lp, sizeof(*rrq));
1670 if (!fp)
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001671 goto retry;
1672
Robert Love42e9a922008-12-09 15:10:17 -08001673 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1674 memset(rrq, 0, sizeof(*rrq));
1675 rrq->rrq_cmd = ELS_RRQ;
1676 hton24(rrq->rrq_s_id, ep->sid);
1677 rrq->rrq_ox_id = htons(ep->oxid);
1678 rrq->rrq_rx_id = htons(ep->rxid);
1679
1680 did = ep->did;
1681 if (ep->esb_stat & ESB_ST_RESP)
1682 did = ep->sid;
1683
1684 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1685 fc_host_port_id(lp->host), FC_TYPE_ELS,
1686 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1687
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001688 if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov))
1689 return;
1690
1691retry:
1692 spin_lock_bh(&ep->ex_lock);
1693 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1694 spin_unlock_bh(&ep->ex_lock);
1695 /* drop hold for rec qual */
1696 fc_exch_release(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001697 return;
1698 }
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001699 ep->esb_stat |= ESB_ST_REC_QUAL;
1700 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1701 spin_unlock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001702}
1703
1704
1705/*
1706 * Handle incoming ELS RRQ - Reset Recovery Qualifier.
1707 */
1708static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1709{
1710 struct fc_exch *ep; /* request or subject exchange */
1711 struct fc_els_rrq *rp;
1712 u32 sid;
1713 u16 xid;
1714 enum fc_els_rjt_explan explan;
1715
1716 rp = fc_frame_payload_get(fp, sizeof(*rp));
1717 explan = ELS_EXPL_INV_LEN;
1718 if (!rp)
1719 goto reject;
1720
1721 /*
1722 * lookup subject exchange.
1723 */
1724 ep = fc_seq_exch(sp);
1725 sid = ntoh24(rp->rrq_s_id); /* subject source */
1726 xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
1727 ep = fc_exch_find(ep->em, xid);
1728
1729 explan = ELS_EXPL_OXID_RXID;
1730 if (!ep)
1731 goto reject;
1732 spin_lock_bh(&ep->ex_lock);
1733 if (ep->oxid != ntohs(rp->rrq_ox_id))
1734 goto unlock_reject;
1735 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
1736 ep->rxid != FC_XID_UNKNOWN)
1737 goto unlock_reject;
1738 explan = ELS_EXPL_SID;
1739 if (ep->sid != sid)
1740 goto unlock_reject;
1741
1742 /*
1743 * Clear Recovery Qualifier state, and cancel timer if complete.
1744 */
1745 if (ep->esb_stat & ESB_ST_REC_QUAL) {
1746 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1747 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
1748 }
1749 if (ep->esb_stat & ESB_ST_COMPLETE) {
1750 if (cancel_delayed_work(&ep->timeout_work))
1751 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
1752 }
1753
1754 spin_unlock_bh(&ep->ex_lock);
1755
1756 /*
1757 * Send LS_ACC.
1758 */
1759 fc_seq_ls_acc(sp);
1760 fc_frame_free(fp);
1761 return;
1762
1763unlock_reject:
1764 spin_unlock_bh(&ep->ex_lock);
1765 fc_exch_release(ep); /* drop hold from fc_exch_find */
1766reject:
1767 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
1768 fc_frame_free(fp);
1769}
1770
Vasu Dev96316092009-07-29 17:05:00 -07001771struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
1772 struct fc_exch_mgr *mp,
1773 bool (*match)(struct fc_frame *))
1774{
1775 struct fc_exch_mgr_anchor *ema;
1776
1777 ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
1778 if (!ema)
1779 return ema;
1780
1781 ema->mp = mp;
1782 ema->match = match;
1783 /* add EM anchor to EM anchors list */
1784 list_add_tail(&ema->ema_list, &lport->ema_list);
1785 kref_get(&mp->kref);
1786 return ema;
1787}
1788EXPORT_SYMBOL(fc_exch_mgr_add);
1789
1790static void fc_exch_mgr_destroy(struct kref *kref)
1791{
1792 struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
1793
1794 /*
1795 * The total exch count must be zero
1796 * before freeing exchange manager.
1797 */
1798 WARN_ON(mp->total_exches != 0);
1799 mempool_destroy(mp->ep_pool);
1800 kfree(mp);
1801}
1802
1803void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
1804{
1805 /* remove EM anchor from EM anchors list */
1806 list_del(&ema->ema_list);
1807 kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
1808 kfree(ema);
1809}
1810EXPORT_SYMBOL(fc_exch_mgr_del);
1811
Robert Love42e9a922008-12-09 15:10:17 -08001812struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1813 enum fc_class class,
Vasu Dev52ff8782009-07-29 17:05:10 -07001814 u16 min_xid, u16 max_xid,
1815 bool (*match)(struct fc_frame *))
Robert Love42e9a922008-12-09 15:10:17 -08001816{
1817 struct fc_exch_mgr *mp;
1818 size_t len;
1819
1820 if (max_xid <= min_xid || min_xid == 0 || max_xid == FC_XID_UNKNOWN) {
Robert Love74147052009-06-10 15:31:10 -07001821 FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
1822 min_xid, max_xid);
Robert Love42e9a922008-12-09 15:10:17 -08001823 return NULL;
1824 }
1825
1826 /*
1827 * Memory need for EM
1828 */
1829#define xid_ok(i, m1, m2) (((i) >= (m1)) && ((i) <= (m2)))
1830 len = (max_xid - min_xid + 1) * (sizeof(struct fc_exch *));
1831 len += sizeof(struct fc_exch_mgr);
1832
1833 mp = kzalloc(len, GFP_ATOMIC);
1834 if (!mp)
1835 return NULL;
1836
1837 mp->class = class;
1838 mp->total_exches = 0;
1839 mp->exches = (struct fc_exch **)(mp + 1);
Robert Love42e9a922008-12-09 15:10:17 -08001840 /* adjust em exch xid range for offload */
1841 mp->min_xid = min_xid;
1842 mp->max_xid = max_xid;
1843 mp->last_xid = min_xid - 1;
1844 mp->max_read = 0;
1845 mp->last_read = 0;
1846 if (lp->lro_enabled && xid_ok(lp->lro_xid, min_xid, max_xid)) {
1847 mp->max_read = lp->lro_xid;
1848 mp->last_read = min_xid - 1;
1849 mp->last_xid = mp->max_read;
1850 } else {
1851 /* disable lro if no xid control over read */
1852 lp->lro_enabled = 0;
1853 }
1854
1855 INIT_LIST_HEAD(&mp->ex_list);
1856 spin_lock_init(&mp->em_lock);
1857
1858 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
1859 if (!mp->ep_pool)
1860 goto free_mp;
1861
Vasu Dev52ff8782009-07-29 17:05:10 -07001862 kref_init(&mp->kref);
1863 if (!fc_exch_mgr_add(lp, mp, match)) {
1864 mempool_destroy(mp->ep_pool);
1865 goto free_mp;
1866 }
1867
1868 /*
1869 * Above kref_init() sets mp->kref to 1 and then
1870 * call to fc_exch_mgr_add incremented mp->kref again,
1871 * so adjust that extra increment.
1872 */
1873 kref_put(&mp->kref, fc_exch_mgr_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001874 return mp;
1875
1876free_mp:
1877 kfree(mp);
1878 return NULL;
1879}
1880EXPORT_SYMBOL(fc_exch_mgr_alloc);
1881
Vasu Dev52ff8782009-07-29 17:05:10 -07001882void fc_exch_mgr_free(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001883{
Vasu Dev52ff8782009-07-29 17:05:10 -07001884 struct fc_exch_mgr_anchor *ema, *next;
1885
1886 list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
1887 fc_exch_mgr_del(ema);
Robert Love42e9a922008-12-09 15:10:17 -08001888}
1889EXPORT_SYMBOL(fc_exch_mgr_free);
1890
Robert Love42e9a922008-12-09 15:10:17 -08001891
1892struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
1893 struct fc_frame *fp,
1894 void (*resp)(struct fc_seq *,
1895 struct fc_frame *fp,
1896 void *arg),
1897 void (*destructor)(struct fc_seq *, void *),
1898 void *arg, u32 timer_msec)
1899{
1900 struct fc_exch *ep;
1901 struct fc_seq *sp = NULL;
1902 struct fc_frame_header *fh;
1903 int rc = 1;
1904
Vasu Dev52ff8782009-07-29 17:05:10 -07001905 ep = fc_exch_alloc(lp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001906 if (!ep) {
1907 fc_frame_free(fp);
1908 return NULL;
1909 }
1910 ep->esb_stat |= ESB_ST_SEQ_INIT;
1911 fh = fc_frame_header_get(fp);
1912 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1913 ep->resp = resp;
1914 ep->destructor = destructor;
1915 ep->arg = arg;
1916 ep->r_a_tov = FC_DEF_R_A_TOV;
1917 ep->lp = lp;
1918 sp = &ep->seq;
1919
1920 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1921 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1922 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1923 sp->cnt++;
1924
Yi Zoub277d2a2009-02-27 14:07:21 -08001925 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1926
Robert Love42e9a922008-12-09 15:10:17 -08001927 if (unlikely(lp->tt.frame_send(lp, fp)))
1928 goto err;
1929
1930 if (timer_msec)
1931 fc_exch_timer_set_locked(ep, timer_msec);
1932 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1933
1934 if (ep->f_ctl & FC_FC_SEQ_INIT)
1935 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1936 spin_unlock_bh(&ep->ex_lock);
1937 return sp;
1938err:
1939 rc = fc_exch_done_locked(ep);
1940 spin_unlock_bh(&ep->ex_lock);
1941 if (!rc)
1942 fc_exch_mgr_delete_ep(ep);
1943 return NULL;
1944}
1945EXPORT_SYMBOL(fc_exch_seq_send);
1946
1947/*
1948 * Receive a frame
1949 */
Vasu Dev52ff8782009-07-29 17:05:10 -07001950void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001951{
1952 struct fc_frame_header *fh = fc_frame_header_get(fp);
Vasu Dev52ff8782009-07-29 17:05:10 -07001953 struct fc_exch_mgr_anchor *ema;
1954 u32 f_ctl, found = 0;
1955 u16 oxid;
Robert Love42e9a922008-12-09 15:10:17 -08001956
1957 /* lport lock ? */
Vasu Dev52ff8782009-07-29 17:05:10 -07001958 if (!lp || lp->state == LPORT_ST_DISABLED) {
Robert Love74147052009-06-10 15:31:10 -07001959 FC_LPORT_DBG(lp, "Receiving frames for an lport that "
1960 "has not been initialized correctly\n");
Robert Love42e9a922008-12-09 15:10:17 -08001961 fc_frame_free(fp);
1962 return;
1963 }
1964
Vasu Dev52ff8782009-07-29 17:05:10 -07001965 f_ctl = ntoh24(fh->fh_f_ctl);
1966 oxid = ntohs(fh->fh_ox_id);
1967 if (f_ctl & FC_FC_EX_CTX) {
1968 list_for_each_entry(ema, &lp->ema_list, ema_list) {
1969 if ((oxid >= ema->mp->min_xid) &&
1970 (oxid <= ema->mp->max_xid)) {
1971 found = 1;
1972 break;
1973 }
1974 }
1975
1976 if (!found) {
1977 FC_LPORT_DBG(lp, "Received response for out "
1978 "of range oxid:%hx\n", oxid);
1979 fc_frame_free(fp);
1980 return;
1981 }
1982 } else
1983 ema = list_entry(lp->ema_list.prev, typeof(*ema), ema_list);
1984
Robert Love42e9a922008-12-09 15:10:17 -08001985 /*
1986 * If frame is marked invalid, just drop it.
1987 */
Robert Love42e9a922008-12-09 15:10:17 -08001988 switch (fr_eof(fp)) {
1989 case FC_EOF_T:
1990 if (f_ctl & FC_FC_END_SEQ)
1991 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
1992 /* fall through */
1993 case FC_EOF_N:
1994 if (fh->fh_type == FC_TYPE_BLS)
Vasu Dev52ff8782009-07-29 17:05:10 -07001995 fc_exch_recv_bls(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001996 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
1997 FC_FC_EX_CTX)
Vasu Dev52ff8782009-07-29 17:05:10 -07001998 fc_exch_recv_seq_resp(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001999 else if (f_ctl & FC_FC_SEQ_CTX)
Vasu Dev52ff8782009-07-29 17:05:10 -07002000 fc_exch_recv_resp(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08002001 else
Vasu Dev52ff8782009-07-29 17:05:10 -07002002 fc_exch_recv_req(lp, ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08002003 break;
2004 default:
Robert Loved459b7e2009-07-29 17:05:05 -07002005 FC_LPORT_DBG(lp, "dropping invalid frame (eof %x)", fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -08002006 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08002007 }
2008}
2009EXPORT_SYMBOL(fc_exch_recv);
2010
2011int fc_exch_init(struct fc_lport *lp)
2012{
Robert Love42e9a922008-12-09 15:10:17 -08002013 if (!lp->tt.seq_start_next)
2014 lp->tt.seq_start_next = fc_seq_start_next;
2015
2016 if (!lp->tt.exch_seq_send)
2017 lp->tt.exch_seq_send = fc_exch_seq_send;
2018
2019 if (!lp->tt.seq_send)
2020 lp->tt.seq_send = fc_seq_send;
2021
2022 if (!lp->tt.seq_els_rsp_send)
2023 lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
2024
2025 if (!lp->tt.exch_done)
2026 lp->tt.exch_done = fc_exch_done;
2027
2028 if (!lp->tt.exch_mgr_reset)
2029 lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
2030
2031 if (!lp->tt.seq_exch_abort)
2032 lp->tt.seq_exch_abort = fc_seq_exch_abort;
2033
2034 return 0;
2035}
2036EXPORT_SYMBOL(fc_exch_init);
2037
2038int fc_setup_exch_mgr(void)
2039{
2040 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
2041 0, SLAB_HWCACHE_ALIGN, NULL);
2042 if (!fc_em_cachep)
2043 return -ENOMEM;
2044 return 0;
2045}
2046
2047void fc_destroy_exch_mgr(void)
2048{
2049 kmem_cache_destroy(fc_em_cachep);
2050}