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