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