blob: ee6031e24c14e44bbe27207e98c1e6cf90c67ab3 [file] [log] [blame]
Robert Love42e9a922008-12-09 15:10:17 -08001/*
2 * Copyright(c) 2007 Intel Corporation. All rights reserved.
3 * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
4 * Copyright(c) 2008 Mike Christie
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Maintained at www.Open-FCoE.org
20 */
21
22/*
23 * Fibre Channel exchange and sequence handling.
24 */
25
26#include <linux/timer.h>
27#include <linux/gfp.h>
28#include <linux/err.h>
29
30#include <scsi/fc/fc_fc2.h>
31
32#include <scsi/libfc.h>
33#include <scsi/fc_encode.h>
34
Robert Love8866a5d2009-11-03 11:45:58 -080035#include "fc_libfc.h"
36
Vasu Deve4bc50b2009-08-25 13:58:47 -070037u16 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 */
Robert Love74147052009-06-10 15:31:10 -070040static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
Robert Love42e9a922008-12-09 15:10:17 -080041
42/*
43 * Structure and function definitions for managing Fibre Channel Exchanges
44 * and Sequences.
45 *
46 * The three primary structures used here are fc_exch_mgr, fc_exch, and fc_seq.
47 *
48 * fc_exch_mgr holds the exchange state for an N port
49 *
50 * fc_exch holds state for one exchange and links to its active sequence.
51 *
52 * fc_seq holds the state for an individual sequence.
53 */
54
55/*
Vasu Deve4bc50b2009-08-25 13:58:47 -070056 * Per cpu exchange pool
57 *
58 * This structure manages per cpu exchanges in array of exchange pointers.
59 * This array is allocated followed by struct fc_exch_pool memory for
60 * assigned range of exchanges to per cpu pool.
61 */
62struct fc_exch_pool {
63 u16 next_index; /* next possible free exchange index */
64 u16 total_exches; /* total allocated exchanges */
65 spinlock_t lock; /* exch pool lock */
66 struct list_head ex_list; /* allocated exchanges list */
67};
68
69/*
Robert Love42e9a922008-12-09 15:10:17 -080070 * Exchange manager.
71 *
72 * This structure is the center for creating exchanges and sequences.
73 * It manages the allocation of exchange IDs.
74 */
75struct fc_exch_mgr {
76 enum fc_class class; /* default class for sequences */
Vasu Dev96316092009-07-29 17:05:00 -070077 struct kref kref; /* exchange mgr reference count */
Robert Love42e9a922008-12-09 15:10:17 -080078 u16 min_xid; /* min exchange ID */
79 u16 max_xid; /* max exchange ID */
Robert Love42e9a922008-12-09 15:10:17 -080080 mempool_t *ep_pool; /* reserve ep's */
Vasu Deve4bc50b2009-08-25 13:58:47 -070081 u16 pool_max_index; /* max exch array index in exch pool */
82 struct fc_exch_pool *pool; /* per cpu exch pool */
Robert Love42e9a922008-12-09 15:10:17 -080083
84 /*
85 * currently exchange mgr stats are updated but not used.
86 * either stats can be expose via sysfs or remove them
87 * all together if not used XXX
88 */
89 struct {
90 atomic_t no_free_exch;
91 atomic_t no_free_exch_xid;
92 atomic_t xid_not_found;
93 atomic_t xid_busy;
94 atomic_t seq_not_found;
95 atomic_t non_bls_resp;
96 } stats;
Robert Love42e9a922008-12-09 15:10:17 -080097};
98#define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
99
Vasu Dev96316092009-07-29 17:05:00 -0700100struct fc_exch_mgr_anchor {
101 struct list_head ema_list;
102 struct fc_exch_mgr *mp;
103 bool (*match)(struct fc_frame *);
104};
105
Robert Love42e9a922008-12-09 15:10:17 -0800106static void fc_exch_rrq(struct fc_exch *);
107static void fc_seq_ls_acc(struct fc_seq *);
108static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason,
109 enum fc_els_rjt_explan);
110static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *);
111static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -0800112
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.
Vasu Devb2f00912009-08-25 13:58:53 -0700188 * - 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.
Robert Love42e9a922008-12-09 15:10:17 -0800190 */
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
Robert Love42e9a922008-12-09 15:10:17 -0800276/*
277 * Release a reference to an exchange.
278 * If the refcnt goes to zero and the exchange is complete, it is freed.
279 */
280static void fc_exch_release(struct fc_exch *ep)
281{
282 struct fc_exch_mgr *mp;
283
284 if (atomic_dec_and_test(&ep->ex_refcnt)) {
285 mp = ep->em;
286 if (ep->destructor)
287 ep->destructor(&ep->seq, ep->arg);
Julia Lawallaa6cd292009-02-04 22:17:29 +0100288 WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
Robert Love42e9a922008-12-09 15:10:17 -0800289 mempool_free(ep, mp->ep_pool);
290 }
291}
292
293static int fc_exch_done_locked(struct fc_exch *ep)
294{
295 int rc = 1;
296
297 /*
298 * We must check for completion in case there are two threads
299 * tyring to complete this. But the rrq code will reuse the
300 * ep, and in that case we only clear the resp and set it as
301 * complete, so it can be reused by the timer to send the rrq.
302 */
303 ep->resp = NULL;
304 if (ep->state & FC_EX_DONE)
305 return rc;
306 ep->esb_stat |= ESB_ST_COMPLETE;
307
308 if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
309 ep->state |= FC_EX_DONE;
310 if (cancel_delayed_work(&ep->timeout_work))
311 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
312 rc = 0;
313 }
314 return rc;
315}
316
Vasu Deve4bc50b2009-08-25 13:58:47 -0700317static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
318 u16 index)
319{
320 struct fc_exch **exches = (struct fc_exch **)(pool + 1);
321 return exches[index];
322}
323
324static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
325 struct fc_exch *ep)
326{
327 ((struct fc_exch **)(pool + 1))[index] = ep;
328}
329
Vasu Devb2f00912009-08-25 13:58:53 -0700330static void fc_exch_delete(struct fc_exch *ep)
Robert Love42e9a922008-12-09 15:10:17 -0800331{
Vasu Devb2f00912009-08-25 13:58:53 -0700332 struct fc_exch_pool *pool;
Robert Love42e9a922008-12-09 15:10:17 -0800333
Vasu Devb2f00912009-08-25 13:58:53 -0700334 pool = ep->pool;
335 spin_lock_bh(&pool->lock);
336 WARN_ON(pool->total_exches <= 0);
337 pool->total_exches--;
338 fc_exch_ptr_set(pool, (ep->xid - ep->em->min_xid) >> fc_cpu_order,
339 NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800340 list_del(&ep->ex_list);
Vasu Devb2f00912009-08-25 13:58:53 -0700341 spin_unlock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -0800342 fc_exch_release(ep); /* drop hold for exch in mp */
343}
344
345/*
346 * Internal version of fc_exch_timer_set - used with lock held.
347 */
348static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
349 unsigned int timer_msec)
350{
351 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
352 return;
353
Robert Lovecd305ce2009-08-25 13:58:37 -0700354 FC_EXCH_DBG(ep, "Exchange timer armed\n");
Robert Love74147052009-06-10 15:31:10 -0700355
Robert Love42e9a922008-12-09 15:10:17 -0800356 if (schedule_delayed_work(&ep->timeout_work,
357 msecs_to_jiffies(timer_msec)))
358 fc_exch_hold(ep); /* hold for timer */
359}
360
361/*
362 * Set timer for an exchange.
363 * The time is a minimum delay in milliseconds until the timer fires.
364 * Used for upper level protocols to time out the exchange.
365 * The timer is cancelled when it fires or when the exchange completes.
366 * Returns non-zero if a timer couldn't be allocated.
367 */
368static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
369{
370 spin_lock_bh(&ep->ex_lock);
371 fc_exch_timer_set_locked(ep, timer_msec);
372 spin_unlock_bh(&ep->ex_lock);
373}
374
Robert Love1a7b75a2009-11-03 11:45:47 -0800375/**
376 * send a frame using existing sequence and exchange.
377 */
378static int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp,
379 struct fc_frame *fp)
380{
381 struct fc_exch *ep;
382 struct fc_frame_header *fh = fc_frame_header_get(fp);
383 int error;
384 u32 f_ctl;
385
386 ep = fc_seq_exch(sp);
387 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
388
389 f_ctl = ntoh24(fh->fh_f_ctl);
390 fc_exch_setup_hdr(ep, fp, f_ctl);
391
392 /*
393 * update sequence count if this frame is carrying
394 * multiple FC frames when sequence offload is enabled
395 * by LLD.
396 */
397 if (fr_max_payload(fp))
398 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
399 fr_max_payload(fp));
400 else
401 sp->cnt++;
402
403 /*
404 * Send the frame.
405 */
406 error = lp->tt.frame_send(lp, fp);
407
408 /*
409 * Update the exchange and sequence flags,
410 * assuming all frames for the sequence have been sent.
411 * We can only be called to send once for each sequence.
412 */
413 spin_lock_bh(&ep->ex_lock);
414 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
415 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
416 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
417 spin_unlock_bh(&ep->ex_lock);
418 return error;
419}
420
421/**
422 * fc_seq_alloc() - Allocate a sequence.
423 * @ep: Exchange pointer
424 * @seq_id: Sequence ID to allocate a sequence for
425 *
426 * We don't support multiple originated sequences on the same exchange.
427 * By implication, any previously originated sequence on this exchange
428 * is complete, and we reallocate the same sequence.
429 */
430static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
431{
432 struct fc_seq *sp;
433
434 sp = &ep->seq;
435 sp->ssb_stat = 0;
436 sp->cnt = 0;
437 sp->id = seq_id;
438 return sp;
439}
440
441static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
442{
443 struct fc_exch *ep = fc_seq_exch(sp);
444
445 sp = fc_seq_alloc(ep, ep->seq_id++);
446 FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
447 ep->f_ctl, sp->id);
448 return sp;
449}
450
451/**
452 * Allocate a new sequence on the same exchange as the supplied sequence.
453 * This will never return NULL.
454 */
455static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
456{
457 struct fc_exch *ep = fc_seq_exch(sp);
458
459 spin_lock_bh(&ep->ex_lock);
460 sp = fc_seq_start_next_locked(sp);
461 spin_unlock_bh(&ep->ex_lock);
462
463 return sp;
464}
465
466/**
467 * This function is for seq_exch_abort function pointer in
468 * struct libfc_function_template, see comment block on
469 * seq_exch_abort for description of this function.
470 */
471static int fc_seq_exch_abort(const struct fc_seq *req_sp,
472 unsigned int timer_msec)
Robert Love42e9a922008-12-09 15:10:17 -0800473{
474 struct fc_seq *sp;
475 struct fc_exch *ep;
476 struct fc_frame *fp;
477 int error;
478
479 ep = fc_seq_exch(req_sp);
480
481 spin_lock_bh(&ep->ex_lock);
482 if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
483 ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
484 spin_unlock_bh(&ep->ex_lock);
485 return -ENXIO;
486 }
487
488 /*
489 * Send the abort on a new sequence if possible.
490 */
491 sp = fc_seq_start_next_locked(&ep->seq);
492 if (!sp) {
493 spin_unlock_bh(&ep->ex_lock);
494 return -ENOMEM;
495 }
496
497 ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
498 if (timer_msec)
499 fc_exch_timer_set_locked(ep, timer_msec);
500 spin_unlock_bh(&ep->ex_lock);
501
502 /*
503 * If not logged into the fabric, don't send ABTS but leave
504 * sequence active until next timeout.
505 */
506 if (!ep->sid)
507 return 0;
508
509 /*
510 * Send an abort for the sequence that timed out.
511 */
512 fp = fc_frame_alloc(ep->lp, 0);
513 if (fp) {
514 fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
515 FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
516 error = fc_seq_send(ep->lp, sp, fp);
517 } else
518 error = -ENOBUFS;
519 return error;
520}
Robert Love42e9a922008-12-09 15:10:17 -0800521
522/*
523 * Exchange timeout - handle exchange timer expiration.
524 * The timer will have been cancelled before this is called.
525 */
526static void fc_exch_timeout(struct work_struct *work)
527{
528 struct fc_exch *ep = container_of(work, struct fc_exch,
529 timeout_work.work);
530 struct fc_seq *sp = &ep->seq;
531 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
532 void *arg;
533 u32 e_stat;
534 int rc = 1;
535
Robert Lovecd305ce2009-08-25 13:58:37 -0700536 FC_EXCH_DBG(ep, "Exchange timed out\n");
537
Robert Love42e9a922008-12-09 15:10:17 -0800538 spin_lock_bh(&ep->ex_lock);
539 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
540 goto unlock;
541
542 e_stat = ep->esb_stat;
543 if (e_stat & ESB_ST_COMPLETE) {
544 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
Vasu Deva0cc1ec2009-07-28 17:33:37 -0700545 spin_unlock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -0800546 if (e_stat & ESB_ST_REC_QUAL)
547 fc_exch_rrq(ep);
Robert Love42e9a922008-12-09 15:10:17 -0800548 goto done;
549 } else {
550 resp = ep->resp;
551 arg = ep->arg;
552 ep->resp = NULL;
553 if (e_stat & ESB_ST_ABNORMAL)
554 rc = fc_exch_done_locked(ep);
555 spin_unlock_bh(&ep->ex_lock);
556 if (!rc)
Vasu Devb2f00912009-08-25 13:58:53 -0700557 fc_exch_delete(ep);
Robert Love42e9a922008-12-09 15:10:17 -0800558 if (resp)
559 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
560 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
561 goto done;
562 }
563unlock:
564 spin_unlock_bh(&ep->ex_lock);
565done:
566 /*
567 * This release matches the hold taken when the timer was set.
568 */
569 fc_exch_release(ep);
570}
571
Vasu Dev52ff8782009-07-29 17:05:10 -0700572/**
573 * fc_exch_em_alloc() - allocate an exchange from a specified EM.
574 * @lport: ptr to the local port
575 * @mp: ptr to the exchange manager
Robert Love42e9a922008-12-09 15:10:17 -0800576 *
Vasu Devd7179682009-07-29 17:05:21 -0700577 * Returns pointer to allocated fc_exch with exch lock held.
Robert Love42e9a922008-12-09 15:10:17 -0800578 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700579static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
Vasu Devd7179682009-07-29 17:05:21 -0700580 struct fc_exch_mgr *mp)
Robert Love42e9a922008-12-09 15:10:17 -0800581{
582 struct fc_exch *ep;
Vasu Devb2f00912009-08-25 13:58:53 -0700583 unsigned int cpu;
584 u16 index;
585 struct fc_exch_pool *pool;
Robert Love42e9a922008-12-09 15:10:17 -0800586
587 /* allocate memory for exchange */
588 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
589 if (!ep) {
590 atomic_inc(&mp->stats.no_free_exch);
591 goto out;
592 }
593 memset(ep, 0, sizeof(*ep));
594
Vasu Devb2f00912009-08-25 13:58:53 -0700595 cpu = smp_processor_id();
596 pool = per_cpu_ptr(mp->pool, cpu);
597 spin_lock_bh(&pool->lock);
598 index = pool->next_index;
599 /* allocate new exch from pool */
600 while (fc_exch_ptr_get(pool, index)) {
601 index = index == mp->pool_max_index ? 0 : index + 1;
602 if (index == pool->next_index)
Robert Love42e9a922008-12-09 15:10:17 -0800603 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800604 }
Vasu Devb2f00912009-08-25 13:58:53 -0700605 pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
Robert Love42e9a922008-12-09 15:10:17 -0800606
607 fc_exch_hold(ep); /* hold for exch in mp */
608 spin_lock_init(&ep->ex_lock);
609 /*
610 * Hold exch lock for caller to prevent fc_exch_reset()
611 * from releasing exch while fc_exch_alloc() caller is
612 * still working on exch.
613 */
614 spin_lock_bh(&ep->ex_lock);
615
Vasu Devb2f00912009-08-25 13:58:53 -0700616 fc_exch_ptr_set(pool, index, ep);
617 list_add_tail(&ep->ex_list, &pool->ex_list);
Robert Love42e9a922008-12-09 15:10:17 -0800618 fc_seq_alloc(ep, ep->seq_id++);
Vasu Devb2f00912009-08-25 13:58:53 -0700619 pool->total_exches++;
620 spin_unlock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -0800621
622 /*
623 * update exchange
624 */
Vasu Devb2f00912009-08-25 13:58:53 -0700625 ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
Robert Love42e9a922008-12-09 15:10:17 -0800626 ep->em = mp;
Vasu Devb2f00912009-08-25 13:58:53 -0700627 ep->pool = pool;
Vasu Dev52ff8782009-07-29 17:05:10 -0700628 ep->lp = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800629 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
630 ep->rxid = FC_XID_UNKNOWN;
631 ep->class = mp->class;
632 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
633out:
634 return ep;
635err:
Vasu Devb2f00912009-08-25 13:58:53 -0700636 spin_unlock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -0800637 atomic_inc(&mp->stats.no_free_exch_xid);
638 mempool_free(ep, mp->ep_pool);
639 return NULL;
640}
Vasu Dev52ff8782009-07-29 17:05:10 -0700641
642/**
643 * fc_exch_alloc() - allocate an exchange.
644 * @lport: ptr to the local port
645 * @fp: ptr to the FC frame
646 *
647 * This function walks the list of the exchange manager(EM)
648 * anchors to select a EM for new exchange allocation. The
649 * EM is selected having either a NULL match function pointer
650 * or call to match function returning true.
651 */
Robert Love1a7b75a2009-11-03 11:45:47 -0800652static struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
653 struct fc_frame *fp)
Vasu Dev52ff8782009-07-29 17:05:10 -0700654{
655 struct fc_exch_mgr_anchor *ema;
656 struct fc_exch *ep;
657
658 list_for_each_entry(ema, &lport->ema_list, ema_list) {
659 if (!ema->match || ema->match(fp)) {
Vasu Devd7179682009-07-29 17:05:21 -0700660 ep = fc_exch_em_alloc(lport, ema->mp);
Vasu Dev52ff8782009-07-29 17:05:10 -0700661 if (ep)
662 return ep;
663 }
664 }
665 return NULL;
666}
Robert Love42e9a922008-12-09 15:10:17 -0800667
668/*
669 * Lookup and hold an exchange.
670 */
671static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
672{
Vasu Devb2f00912009-08-25 13:58:53 -0700673 struct fc_exch_pool *pool;
Robert Love42e9a922008-12-09 15:10:17 -0800674 struct fc_exch *ep = NULL;
675
676 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
Vasu Devb2f00912009-08-25 13:58:53 -0700677 pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
678 spin_lock_bh(&pool->lock);
679 ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
Robert Love42e9a922008-12-09 15:10:17 -0800680 if (ep) {
681 fc_exch_hold(ep);
682 WARN_ON(ep->xid != xid);
683 }
Vasu Devb2f00912009-08-25 13:58:53 -0700684 spin_unlock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -0800685 }
686 return ep;
687}
688
Robert Love1a7b75a2009-11-03 11:45:47 -0800689
690/**
691 * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
692 * the memory allocated for the related objects may be freed.
693 * @sp: Sequence pointer
694 */
695static void fc_exch_done(struct fc_seq *sp)
Robert Love42e9a922008-12-09 15:10:17 -0800696{
697 struct fc_exch *ep = fc_seq_exch(sp);
698 int rc;
699
700 spin_lock_bh(&ep->ex_lock);
701 rc = fc_exch_done_locked(ep);
702 spin_unlock_bh(&ep->ex_lock);
703 if (!rc)
Vasu Devb2f00912009-08-25 13:58:53 -0700704 fc_exch_delete(ep);
Robert Love42e9a922008-12-09 15:10:17 -0800705}
Robert Love42e9a922008-12-09 15:10:17 -0800706
707/*
708 * Allocate a new exchange as responder.
709 * Sets the responder ID in the frame header.
710 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700711static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
712 struct fc_exch_mgr *mp,
713 struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800714{
715 struct fc_exch *ep;
716 struct fc_frame_header *fh;
Robert Love42e9a922008-12-09 15:10:17 -0800717
Vasu Dev52ff8782009-07-29 17:05:10 -0700718 ep = fc_exch_alloc(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800719 if (ep) {
720 ep->class = fc_frame_class(fp);
721
722 /*
723 * Set EX_CTX indicating we're responding on this exchange.
724 */
725 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
726 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
727 fh = fc_frame_header_get(fp);
728 ep->sid = ntoh24(fh->fh_d_id);
729 ep->did = ntoh24(fh->fh_s_id);
730 ep->oid = ep->did;
731
732 /*
733 * Allocated exchange has placed the XID in the
734 * originator field. Move it to the responder field,
735 * and set the originator XID from the frame.
736 */
737 ep->rxid = ep->xid;
738 ep->oxid = ntohs(fh->fh_ox_id);
739 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
740 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
741 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
742
Robert Love42e9a922008-12-09 15:10:17 -0800743 fc_exch_hold(ep); /* hold for caller */
Vasu Dev52ff8782009-07-29 17:05:10 -0700744 spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */
Robert Love42e9a922008-12-09 15:10:17 -0800745 }
746 return ep;
747}
748
749/*
750 * Find a sequence for receive where the other end is originating the sequence.
751 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
752 * on the ep that should be released by the caller.
753 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700754static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
755 struct fc_exch_mgr *mp,
Robert Loveb2ab99c2009-02-27 10:55:50 -0800756 struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800757{
758 struct fc_frame_header *fh = fc_frame_header_get(fp);
759 struct fc_exch *ep = NULL;
760 struct fc_seq *sp = NULL;
761 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
762 u32 f_ctl;
763 u16 xid;
764
765 f_ctl = ntoh24(fh->fh_f_ctl);
766 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
767
768 /*
769 * Lookup or create the exchange if we will be creating the sequence.
770 */
771 if (f_ctl & FC_FC_EX_CTX) {
772 xid = ntohs(fh->fh_ox_id); /* we originated exch */
773 ep = fc_exch_find(mp, xid);
774 if (!ep) {
775 atomic_inc(&mp->stats.xid_not_found);
776 reject = FC_RJT_OX_ID;
777 goto out;
778 }
779 if (ep->rxid == FC_XID_UNKNOWN)
780 ep->rxid = ntohs(fh->fh_rx_id);
781 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
782 reject = FC_RJT_OX_ID;
783 goto rel;
784 }
785 } else {
786 xid = ntohs(fh->fh_rx_id); /* we are the responder */
787
788 /*
789 * Special case for MDS issuing an ELS TEST with a
790 * bad rxid of 0.
791 * XXX take this out once we do the proper reject.
792 */
793 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
794 fc_frame_payload_op(fp) == ELS_TEST) {
795 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
796 xid = FC_XID_UNKNOWN;
797 }
798
799 /*
800 * new sequence - find the exchange
801 */
802 ep = fc_exch_find(mp, xid);
803 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
804 if (ep) {
805 atomic_inc(&mp->stats.xid_busy);
806 reject = FC_RJT_RX_ID;
807 goto rel;
808 }
Vasu Dev52ff8782009-07-29 17:05:10 -0700809 ep = fc_exch_resp(lport, mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800810 if (!ep) {
811 reject = FC_RJT_EXCH_EST; /* XXX */
812 goto out;
813 }
814 xid = ep->xid; /* get our XID */
815 } else if (!ep) {
816 atomic_inc(&mp->stats.xid_not_found);
817 reject = FC_RJT_RX_ID; /* XID not found */
818 goto out;
819 }
820 }
821
822 /*
823 * At this point, we have the exchange held.
824 * Find or create the sequence.
825 */
826 if (fc_sof_is_init(fr_sof(fp))) {
827 sp = fc_seq_start_next(&ep->seq);
828 if (!sp) {
829 reject = FC_RJT_SEQ_XS; /* exchange shortage */
830 goto rel;
831 }
832 sp->id = fh->fh_seq_id;
833 sp->ssb_stat |= SSB_ST_RESP;
834 } else {
835 sp = &ep->seq;
836 if (sp->id != fh->fh_seq_id) {
837 atomic_inc(&mp->stats.seq_not_found);
838 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
839 goto rel;
840 }
841 }
842 WARN_ON(ep != fc_seq_exch(sp));
843
844 if (f_ctl & FC_FC_SEQ_INIT)
845 ep->esb_stat |= ESB_ST_SEQ_INIT;
846
847 fr_seq(fp) = sp;
848out:
849 return reject;
850rel:
851 fc_exch_done(&ep->seq);
852 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
853 return reject;
854}
855
856/*
857 * Find the sequence for a frame being received.
858 * We originated the sequence, so it should be found.
859 * We may or may not have originated the exchange.
860 * Does not hold the sequence for the caller.
861 */
862static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
863 struct fc_frame *fp)
864{
865 struct fc_frame_header *fh = fc_frame_header_get(fp);
866 struct fc_exch *ep;
867 struct fc_seq *sp = NULL;
868 u32 f_ctl;
869 u16 xid;
870
871 f_ctl = ntoh24(fh->fh_f_ctl);
872 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
873 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
874 ep = fc_exch_find(mp, xid);
875 if (!ep)
876 return NULL;
877 if (ep->seq.id == fh->fh_seq_id) {
878 /*
879 * Save the RX_ID if we didn't previously know it.
880 */
881 sp = &ep->seq;
882 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
883 ep->rxid == FC_XID_UNKNOWN) {
884 ep->rxid = ntohs(fh->fh_rx_id);
885 }
886 }
887 fc_exch_release(ep);
888 return sp;
889}
890
891/*
892 * Set addresses for an exchange.
893 * Note this must be done before the first sequence of the exchange is sent.
894 */
895static void fc_exch_set_addr(struct fc_exch *ep,
896 u32 orig_id, u32 resp_id)
897{
898 ep->oid = orig_id;
899 if (ep->esb_stat & ESB_ST_RESP) {
900 ep->sid = resp_id;
901 ep->did = orig_id;
902 } else {
903 ep->sid = orig_id;
904 ep->did = resp_id;
905 }
906}
907
Robert Love1a7b75a2009-11-03 11:45:47 -0800908/**
909 * fc_seq_els_rsp_send() - Send ELS response using mainly infomation
910 * in exchange and sequence in EM layer.
911 * @sp: Sequence pointer
912 * @els_cmd: ELS command
913 * @els_data: ELS data
Robert Love42e9a922008-12-09 15:10:17 -0800914 */
Robert Love1a7b75a2009-11-03 11:45:47 -0800915static void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
916 struct fc_seq_els_data *els_data)
Robert Love42e9a922008-12-09 15:10:17 -0800917{
918 switch (els_cmd) {
919 case ELS_LS_RJT:
920 fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
921 break;
922 case ELS_LS_ACC:
923 fc_seq_ls_acc(sp);
924 break;
925 case ELS_RRQ:
926 fc_exch_els_rrq(sp, els_data->fp);
927 break;
928 case ELS_REC:
929 fc_exch_els_rec(sp, els_data->fp);
930 break;
931 default:
Robert Love74147052009-06-10 15:31:10 -0700932 FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
Robert Love42e9a922008-12-09 15:10:17 -0800933 }
934}
Robert Love42e9a922008-12-09 15:10:17 -0800935
936/*
937 * Send a sequence, which is also the last sequence in the exchange.
938 */
939static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
940 enum fc_rctl rctl, enum fc_fh_type fh_type)
941{
942 u32 f_ctl;
943 struct fc_exch *ep = fc_seq_exch(sp);
944
945 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
946 f_ctl |= ep->f_ctl;
947 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
948 fc_seq_send(ep->lp, sp, fp);
949}
950
951/*
952 * Send ACK_1 (or equiv.) indicating we received something.
953 * The frame we're acking is supplied.
954 */
955static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
956{
957 struct fc_frame *fp;
958 struct fc_frame_header *rx_fh;
959 struct fc_frame_header *fh;
960 struct fc_exch *ep = fc_seq_exch(sp);
961 struct fc_lport *lp = ep->lp;
962 unsigned int f_ctl;
963
964 /*
965 * Don't send ACKs for class 3.
966 */
967 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
968 fp = fc_frame_alloc(lp, 0);
969 if (!fp)
970 return;
971
972 fh = fc_frame_header_get(fp);
973 fh->fh_r_ctl = FC_RCTL_ACK_1;
974 fh->fh_type = FC_TYPE_BLS;
975
976 /*
977 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
978 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
979 * Bits 9-8 are meaningful (retransmitted or unidirectional).
980 * Last ACK uses bits 7-6 (continue sequence),
981 * bits 5-4 are meaningful (what kind of ACK to use).
982 */
983 rx_fh = fc_frame_header_get(rx_fp);
984 f_ctl = ntoh24(rx_fh->fh_f_ctl);
985 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
986 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
987 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
988 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
989 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
990 hton24(fh->fh_f_ctl, f_ctl);
991
992 fc_exch_setup_hdr(ep, fp, f_ctl);
993 fh->fh_seq_id = rx_fh->fh_seq_id;
994 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
995 fh->fh_parm_offset = htonl(1); /* ack single frame */
996
997 fr_sof(fp) = fr_sof(rx_fp);
998 if (f_ctl & FC_FC_END_SEQ)
999 fr_eof(fp) = FC_EOF_T;
1000 else
1001 fr_eof(fp) = FC_EOF_N;
1002
1003 (void) lp->tt.frame_send(lp, fp);
1004 }
1005}
1006
1007/*
1008 * Send BLS Reject.
1009 * This is for rejecting BA_ABTS only.
1010 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001011static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1012 enum fc_ba_rjt_reason reason,
1013 enum fc_ba_rjt_explan explan)
Robert Love42e9a922008-12-09 15:10:17 -08001014{
1015 struct fc_frame *fp;
1016 struct fc_frame_header *rx_fh;
1017 struct fc_frame_header *fh;
1018 struct fc_ba_rjt *rp;
1019 struct fc_lport *lp;
1020 unsigned int f_ctl;
1021
1022 lp = fr_dev(rx_fp);
1023 fp = fc_frame_alloc(lp, sizeof(*rp));
1024 if (!fp)
1025 return;
1026 fh = fc_frame_header_get(fp);
1027 rx_fh = fc_frame_header_get(rx_fp);
1028
1029 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
1030
1031 rp = fc_frame_payload_get(fp, sizeof(*rp));
1032 rp->br_reason = reason;
1033 rp->br_explan = explan;
1034
1035 /*
1036 * seq_id, cs_ctl, df_ctl and param/offset are zero.
1037 */
1038 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
1039 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
Joe Eykholt1d490ce2009-08-25 14:04:03 -07001040 fh->fh_ox_id = rx_fh->fh_ox_id;
1041 fh->fh_rx_id = rx_fh->fh_rx_id;
Robert Love42e9a922008-12-09 15:10:17 -08001042 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1043 fh->fh_r_ctl = FC_RCTL_BA_RJT;
1044 fh->fh_type = FC_TYPE_BLS;
1045
1046 /*
1047 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
1048 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
1049 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1050 * Last ACK uses bits 7-6 (continue sequence),
1051 * bits 5-4 are meaningful (what kind of ACK to use).
1052 * Always set LAST_SEQ, END_SEQ.
1053 */
1054 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1055 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1056 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1057 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1058 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1059 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1060 f_ctl &= ~FC_FC_FIRST_SEQ;
1061 hton24(fh->fh_f_ctl, f_ctl);
1062
1063 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1064 fr_eof(fp) = FC_EOF_T;
1065 if (fc_sof_needs_ack(fr_sof(fp)))
1066 fr_eof(fp) = FC_EOF_N;
1067
1068 (void) lp->tt.frame_send(lp, fp);
1069}
1070
1071/*
1072 * Handle an incoming ABTS. This would be for target mode usually,
1073 * but could be due to lost FCP transfer ready, confirm or RRQ.
1074 * We always handle this as an exchange abort, ignoring the parameter.
1075 */
1076static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1077{
1078 struct fc_frame *fp;
1079 struct fc_ba_acc *ap;
1080 struct fc_frame_header *fh;
1081 struct fc_seq *sp;
1082
1083 if (!ep)
1084 goto reject;
1085 spin_lock_bh(&ep->ex_lock);
1086 if (ep->esb_stat & ESB_ST_COMPLETE) {
1087 spin_unlock_bh(&ep->ex_lock);
1088 goto reject;
1089 }
1090 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1091 fc_exch_hold(ep); /* hold for REC_QUAL */
1092 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1093 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1094
1095 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1096 if (!fp) {
1097 spin_unlock_bh(&ep->ex_lock);
1098 goto free;
1099 }
1100 fh = fc_frame_header_get(fp);
1101 ap = fc_frame_payload_get(fp, sizeof(*ap));
1102 memset(ap, 0, sizeof(*ap));
1103 sp = &ep->seq;
1104 ap->ba_high_seq_cnt = htons(0xffff);
1105 if (sp->ssb_stat & SSB_ST_RESP) {
1106 ap->ba_seq_id = sp->id;
1107 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1108 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1109 ap->ba_low_seq_cnt = htons(sp->cnt);
1110 }
Vasu Deva7e84f22009-02-27 10:54:51 -08001111 sp = fc_seq_start_next_locked(sp);
Robert Love42e9a922008-12-09 15:10:17 -08001112 spin_unlock_bh(&ep->ex_lock);
1113 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1114 fc_frame_free(rx_fp);
1115 return;
1116
1117reject:
1118 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1119free:
1120 fc_frame_free(rx_fp);
1121}
1122
1123/*
1124 * Handle receive where the other end is originating the sequence.
1125 */
1126static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1127 struct fc_frame *fp)
1128{
1129 struct fc_frame_header *fh = fc_frame_header_get(fp);
1130 struct fc_seq *sp = NULL;
1131 struct fc_exch *ep = NULL;
1132 enum fc_sof sof;
1133 enum fc_eof eof;
1134 u32 f_ctl;
1135 enum fc_pf_rjt_reason reject;
1136
1137 fr_seq(fp) = NULL;
Vasu Dev52ff8782009-07-29 17:05:10 -07001138 reject = fc_seq_lookup_recip(lp, mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001139 if (reject == FC_RJT_NONE) {
1140 sp = fr_seq(fp); /* sequence will be held */
1141 ep = fc_seq_exch(sp);
1142 sof = fr_sof(fp);
1143 eof = fr_eof(fp);
1144 f_ctl = ntoh24(fh->fh_f_ctl);
1145 fc_seq_send_ack(sp, fp);
1146
1147 /*
1148 * Call the receive function.
1149 *
1150 * The receive function may allocate a new sequence
1151 * over the old one, so we shouldn't change the
1152 * sequence after this.
1153 *
1154 * The frame will be freed by the receive function.
1155 * If new exch resp handler is valid then call that
1156 * first.
1157 */
1158 if (ep->resp)
1159 ep->resp(sp, fp, ep->arg);
1160 else
1161 lp->tt.lport_recv(lp, sp, fp);
1162 fc_exch_release(ep); /* release from lookup */
1163 } else {
Robert Loved459b7e2009-07-29 17:05:05 -07001164 FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject);
Robert Love42e9a922008-12-09 15:10:17 -08001165 fc_frame_free(fp);
1166 }
1167}
1168
1169/*
1170 * Handle receive where the other end is originating the sequence in
1171 * response to our exchange.
1172 */
1173static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1174{
1175 struct fc_frame_header *fh = fc_frame_header_get(fp);
1176 struct fc_seq *sp;
1177 struct fc_exch *ep;
1178 enum fc_sof sof;
1179 u32 f_ctl;
1180 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1181 void *ex_resp_arg;
1182 int rc;
1183
1184 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1185 if (!ep) {
1186 atomic_inc(&mp->stats.xid_not_found);
1187 goto out;
1188 }
Steve Ma30121d12009-05-06 10:52:29 -07001189 if (ep->esb_stat & ESB_ST_COMPLETE) {
1190 atomic_inc(&mp->stats.xid_not_found);
1191 goto out;
1192 }
Robert Love42e9a922008-12-09 15:10:17 -08001193 if (ep->rxid == FC_XID_UNKNOWN)
1194 ep->rxid = ntohs(fh->fh_rx_id);
1195 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1196 atomic_inc(&mp->stats.xid_not_found);
1197 goto rel;
1198 }
1199 if (ep->did != ntoh24(fh->fh_s_id) &&
1200 ep->did != FC_FID_FLOGI) {
1201 atomic_inc(&mp->stats.xid_not_found);
1202 goto rel;
1203 }
1204 sof = fr_sof(fp);
1205 if (fc_sof_is_init(sof)) {
1206 sp = fc_seq_start_next(&ep->seq);
1207 sp->id = fh->fh_seq_id;
1208 sp->ssb_stat |= SSB_ST_RESP;
1209 } else {
1210 sp = &ep->seq;
1211 if (sp->id != fh->fh_seq_id) {
1212 atomic_inc(&mp->stats.seq_not_found);
1213 goto rel;
1214 }
1215 }
1216 f_ctl = ntoh24(fh->fh_f_ctl);
1217 fr_seq(fp) = sp;
1218 if (f_ctl & FC_FC_SEQ_INIT)
1219 ep->esb_stat |= ESB_ST_SEQ_INIT;
1220
1221 if (fc_sof_needs_ack(sof))
1222 fc_seq_send_ack(sp, fp);
1223 resp = ep->resp;
1224 ex_resp_arg = ep->arg;
1225
1226 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1227 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1228 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1229 spin_lock_bh(&ep->ex_lock);
1230 rc = fc_exch_done_locked(ep);
1231 WARN_ON(fc_seq_exch(sp) != ep);
1232 spin_unlock_bh(&ep->ex_lock);
1233 if (!rc)
Vasu Devb2f00912009-08-25 13:58:53 -07001234 fc_exch_delete(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001235 }
1236
1237 /*
1238 * Call the receive function.
1239 * The sequence is held (has a refcnt) for us,
1240 * but not for the receive function.
1241 *
1242 * The receive function may allocate a new sequence
1243 * over the old one, so we shouldn't change the
1244 * sequence after this.
1245 *
1246 * The frame will be freed by the receive function.
1247 * If new exch resp handler is valid then call that
1248 * first.
1249 */
1250 if (resp)
1251 resp(sp, fp, ex_resp_arg);
1252 else
1253 fc_frame_free(fp);
1254 fc_exch_release(ep);
1255 return;
1256rel:
1257 fc_exch_release(ep);
1258out:
1259 fc_frame_free(fp);
1260}
1261
1262/*
1263 * Handle receive for a sequence where other end is responding to our sequence.
1264 */
1265static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1266{
1267 struct fc_seq *sp;
1268
1269 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
Robert Loved459b7e2009-07-29 17:05:05 -07001270
1271 if (!sp)
Robert Love42e9a922008-12-09 15:10:17 -08001272 atomic_inc(&mp->stats.xid_not_found);
Robert Loved459b7e2009-07-29 17:05:05 -07001273 else
Robert Love42e9a922008-12-09 15:10:17 -08001274 atomic_inc(&mp->stats.non_bls_resp);
Robert Loved459b7e2009-07-29 17:05:05 -07001275
Robert Love42e9a922008-12-09 15:10:17 -08001276 fc_frame_free(fp);
1277}
1278
1279/*
1280 * Handle the response to an ABTS for exchange or sequence.
1281 * This can be BA_ACC or BA_RJT.
1282 */
1283static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1284{
1285 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1286 void *ex_resp_arg;
1287 struct fc_frame_header *fh;
1288 struct fc_ba_acc *ap;
1289 struct fc_seq *sp;
1290 u16 low;
1291 u16 high;
1292 int rc = 1, has_rec = 0;
1293
1294 fh = fc_frame_header_get(fp);
Robert Love74147052009-06-10 15:31:10 -07001295 FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
1296 fc_exch_rctl_name(fh->fh_r_ctl));
Robert Love42e9a922008-12-09 15:10:17 -08001297
1298 if (cancel_delayed_work_sync(&ep->timeout_work))
1299 fc_exch_release(ep); /* release from pending timer hold */
1300
1301 spin_lock_bh(&ep->ex_lock);
1302 switch (fh->fh_r_ctl) {
1303 case FC_RCTL_BA_ACC:
1304 ap = fc_frame_payload_get(fp, sizeof(*ap));
1305 if (!ap)
1306 break;
1307
1308 /*
1309 * Decide whether to establish a Recovery Qualifier.
1310 * We do this if there is a non-empty SEQ_CNT range and
1311 * SEQ_ID is the same as the one we aborted.
1312 */
1313 low = ntohs(ap->ba_low_seq_cnt);
1314 high = ntohs(ap->ba_high_seq_cnt);
1315 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1316 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1317 ap->ba_seq_id == ep->seq_id) && low != high) {
1318 ep->esb_stat |= ESB_ST_REC_QUAL;
1319 fc_exch_hold(ep); /* hold for recovery qualifier */
1320 has_rec = 1;
1321 }
1322 break;
1323 case FC_RCTL_BA_RJT:
1324 break;
1325 default:
1326 break;
1327 }
1328
1329 resp = ep->resp;
1330 ex_resp_arg = ep->arg;
1331
1332 /* do we need to do some other checks here. Can we reuse more of
1333 * fc_exch_recv_seq_resp
1334 */
1335 sp = &ep->seq;
1336 /*
1337 * do we want to check END_SEQ as well as LAST_SEQ here?
1338 */
1339 if (ep->fh_type != FC_TYPE_FCP &&
1340 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1341 rc = fc_exch_done_locked(ep);
1342 spin_unlock_bh(&ep->ex_lock);
1343 if (!rc)
Vasu Devb2f00912009-08-25 13:58:53 -07001344 fc_exch_delete(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001345
1346 if (resp)
1347 resp(sp, fp, ex_resp_arg);
1348 else
1349 fc_frame_free(fp);
1350
1351 if (has_rec)
1352 fc_exch_timer_set(ep, ep->r_a_tov);
1353
1354}
1355
1356/*
1357 * Receive BLS sequence.
1358 * This is always a sequence initiated by the remote side.
1359 * We may be either the originator or recipient of the exchange.
1360 */
1361static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1362{
1363 struct fc_frame_header *fh;
1364 struct fc_exch *ep;
1365 u32 f_ctl;
1366
1367 fh = fc_frame_header_get(fp);
1368 f_ctl = ntoh24(fh->fh_f_ctl);
1369 fr_seq(fp) = NULL;
1370
1371 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1372 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1373 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1374 spin_lock_bh(&ep->ex_lock);
1375 ep->esb_stat |= ESB_ST_SEQ_INIT;
1376 spin_unlock_bh(&ep->ex_lock);
1377 }
1378 if (f_ctl & FC_FC_SEQ_CTX) {
1379 /*
1380 * A response to a sequence we initiated.
1381 * This should only be ACKs for class 2 or F.
1382 */
1383 switch (fh->fh_r_ctl) {
1384 case FC_RCTL_ACK_1:
1385 case FC_RCTL_ACK_0:
1386 break;
1387 default:
Robert Love74147052009-06-10 15:31:10 -07001388 FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
1389 fh->fh_r_ctl,
1390 fc_exch_rctl_name(fh->fh_r_ctl));
Robert Love42e9a922008-12-09 15:10:17 -08001391 break;
1392 }
1393 fc_frame_free(fp);
1394 } else {
1395 switch (fh->fh_r_ctl) {
1396 case FC_RCTL_BA_RJT:
1397 case FC_RCTL_BA_ACC:
1398 if (ep)
1399 fc_exch_abts_resp(ep, fp);
1400 else
1401 fc_frame_free(fp);
1402 break;
1403 case FC_RCTL_BA_ABTS:
1404 fc_exch_recv_abts(ep, fp);
1405 break;
1406 default: /* ignore junk */
1407 fc_frame_free(fp);
1408 break;
1409 }
1410 }
1411 if (ep)
1412 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1413}
1414
1415/*
1416 * Accept sequence with LS_ACC.
1417 * If this fails due to allocation or transmit congestion, assume the
1418 * originator will repeat the sequence.
1419 */
1420static void fc_seq_ls_acc(struct fc_seq *req_sp)
1421{
1422 struct fc_seq *sp;
1423 struct fc_els_ls_acc *acc;
1424 struct fc_frame *fp;
1425
1426 sp = fc_seq_start_next(req_sp);
1427 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1428 if (fp) {
1429 acc = fc_frame_payload_get(fp, sizeof(*acc));
1430 memset(acc, 0, sizeof(*acc));
1431 acc->la_cmd = ELS_LS_ACC;
1432 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1433 }
1434}
1435
1436/*
1437 * Reject sequence with ELS LS_RJT.
1438 * If this fails due to allocation or transmit congestion, assume the
1439 * originator will repeat the sequence.
1440 */
1441static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
1442 enum fc_els_rjt_explan explan)
1443{
1444 struct fc_seq *sp;
1445 struct fc_els_ls_rjt *rjt;
1446 struct fc_frame *fp;
1447
1448 sp = fc_seq_start_next(req_sp);
1449 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
1450 if (fp) {
1451 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1452 memset(rjt, 0, sizeof(*rjt));
1453 rjt->er_cmd = ELS_LS_RJT;
1454 rjt->er_reason = reason;
1455 rjt->er_explan = explan;
1456 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1457 }
1458}
1459
1460static void fc_exch_reset(struct fc_exch *ep)
1461{
1462 struct fc_seq *sp;
1463 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1464 void *arg;
1465 int rc = 1;
1466
1467 spin_lock_bh(&ep->ex_lock);
1468 ep->state |= FC_EX_RST_CLEANUP;
1469 /*
1470 * we really want to call del_timer_sync, but cannot due
1471 * to the lport calling with the lport lock held (some resp
1472 * functions can also grab the lport lock which could cause
1473 * a deadlock).
1474 */
1475 if (cancel_delayed_work(&ep->timeout_work))
1476 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1477 resp = ep->resp;
1478 ep->resp = NULL;
1479 if (ep->esb_stat & ESB_ST_REC_QUAL)
1480 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1481 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1482 arg = ep->arg;
1483 sp = &ep->seq;
1484 rc = fc_exch_done_locked(ep);
1485 spin_unlock_bh(&ep->ex_lock);
1486 if (!rc)
Vasu Devb2f00912009-08-25 13:58:53 -07001487 fc_exch_delete(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001488
1489 if (resp)
1490 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1491}
1492
Vasu Devb2f00912009-08-25 13:58:53 -07001493/**
1494 * fc_exch_pool_reset() - Resets an per cpu exches pool.
1495 * @lport: ptr to the local port
1496 * @pool: ptr to the per cpu exches pool
1497 * @sid: source FC ID
1498 * @did: destination FC ID
1499 *
1500 * Resets an per cpu exches pool, releasing its all sequences
1501 * and exchanges. If sid is non-zero, then reset only exchanges
1502 * we sourced from that FID. If did is non-zero, reset only
1503 * exchanges destined to that FID.
Robert Love42e9a922008-12-09 15:10:17 -08001504 */
Vasu Devb2f00912009-08-25 13:58:53 -07001505static void fc_exch_pool_reset(struct fc_lport *lport,
1506 struct fc_exch_pool *pool,
1507 u32 sid, u32 did)
Robert Love42e9a922008-12-09 15:10:17 -08001508{
1509 struct fc_exch *ep;
1510 struct fc_exch *next;
Robert Love42e9a922008-12-09 15:10:17 -08001511
Vasu Devb2f00912009-08-25 13:58:53 -07001512 spin_lock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -08001513restart:
Vasu Devb2f00912009-08-25 13:58:53 -07001514 list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
1515 if ((lport == ep->lp) &&
1516 (sid == 0 || sid == ep->sid) &&
1517 (did == 0 || did == ep->did)) {
1518 fc_exch_hold(ep);
1519 spin_unlock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -08001520
Vasu Devb2f00912009-08-25 13:58:53 -07001521 fc_exch_reset(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001522
Vasu Devb2f00912009-08-25 13:58:53 -07001523 fc_exch_release(ep);
1524 spin_lock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -08001525
Vasu Devb2f00912009-08-25 13:58:53 -07001526 /*
1527 * must restart loop incase while lock
1528 * was down multiple eps were released.
1529 */
1530 goto restart;
Robert Love42e9a922008-12-09 15:10:17 -08001531 }
Vasu Devb2f00912009-08-25 13:58:53 -07001532 }
1533 spin_unlock_bh(&pool->lock);
1534}
1535
1536/**
1537 * fc_exch_mgr_reset() - Resets all EMs of a lport
1538 * @lport: ptr to the local port
1539 * @sid: source FC ID
1540 * @did: destination FC ID
1541 *
1542 * Reset all EMs of a lport, releasing its all sequences and
1543 * exchanges. If sid is non-zero, then reset only exchanges
1544 * we sourced from that FID. If did is non-zero, reset only
1545 * exchanges destined to that FID.
1546 */
1547void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1548{
1549 struct fc_exch_mgr_anchor *ema;
1550 unsigned int cpu;
1551
1552 list_for_each_entry(ema, &lport->ema_list, ema_list) {
1553 for_each_possible_cpu(cpu)
1554 fc_exch_pool_reset(lport,
1555 per_cpu_ptr(ema->mp->pool, cpu),
1556 sid, did);
Robert Love42e9a922008-12-09 15:10:17 -08001557 }
Robert Love42e9a922008-12-09 15:10:17 -08001558}
1559EXPORT_SYMBOL(fc_exch_mgr_reset);
1560
1561/*
1562 * Handle incoming ELS REC - Read Exchange Concise.
1563 * Note that the requesting port may be different than the S_ID in the request.
1564 */
1565static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
1566{
1567 struct fc_frame *fp;
1568 struct fc_exch *ep;
1569 struct fc_exch_mgr *em;
1570 struct fc_els_rec *rp;
1571 struct fc_els_rec_acc *acc;
1572 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1573 enum fc_els_rjt_explan explan;
1574 u32 sid;
1575 u16 rxid;
1576 u16 oxid;
1577
1578 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1579 explan = ELS_EXPL_INV_LEN;
1580 if (!rp)
1581 goto reject;
1582 sid = ntoh24(rp->rec_s_id);
1583 rxid = ntohs(rp->rec_rx_id);
1584 oxid = ntohs(rp->rec_ox_id);
1585
1586 /*
1587 * Currently it's hard to find the local S_ID from the exchange
1588 * manager. This will eventually be fixed, but for now it's easier
1589 * to lookup the subject exchange twice, once as if we were
1590 * the initiator, and then again if we weren't.
1591 */
1592 em = fc_seq_exch(sp)->em;
1593 ep = fc_exch_find(em, oxid);
1594 explan = ELS_EXPL_OXID_RXID;
1595 if (ep && ep->oid == sid) {
1596 if (ep->rxid != FC_XID_UNKNOWN &&
1597 rxid != FC_XID_UNKNOWN &&
1598 ep->rxid != rxid)
1599 goto rel;
1600 } else {
1601 if (ep)
1602 fc_exch_release(ep);
1603 ep = NULL;
1604 if (rxid != FC_XID_UNKNOWN)
1605 ep = fc_exch_find(em, rxid);
1606 if (!ep)
1607 goto reject;
1608 }
1609
1610 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1611 if (!fp) {
1612 fc_exch_done(sp);
1613 goto out;
1614 }
1615 sp = fc_seq_start_next(sp);
1616 acc = fc_frame_payload_get(fp, sizeof(*acc));
1617 memset(acc, 0, sizeof(*acc));
1618 acc->reca_cmd = ELS_LS_ACC;
1619 acc->reca_ox_id = rp->rec_ox_id;
1620 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1621 acc->reca_rx_id = htons(ep->rxid);
1622 if (ep->sid == ep->oid)
1623 hton24(acc->reca_rfid, ep->did);
1624 else
1625 hton24(acc->reca_rfid, ep->sid);
1626 acc->reca_fc4value = htonl(ep->seq.rec_data);
1627 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1628 ESB_ST_SEQ_INIT |
1629 ESB_ST_COMPLETE));
1630 sp = fc_seq_start_next(sp);
1631 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1632out:
1633 fc_exch_release(ep);
1634 fc_frame_free(rfp);
1635 return;
1636
1637rel:
1638 fc_exch_release(ep);
1639reject:
1640 fc_seq_ls_rjt(sp, reason, explan);
1641 fc_frame_free(rfp);
1642}
1643
1644/*
1645 * Handle response from RRQ.
1646 * Not much to do here, really.
1647 * Should report errors.
1648 *
1649 * TODO: fix error handler.
1650 */
1651static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1652{
1653 struct fc_exch *aborted_ep = arg;
1654 unsigned int op;
1655
1656 if (IS_ERR(fp)) {
1657 int err = PTR_ERR(fp);
1658
Vasu Dev78342da2009-02-27 10:54:46 -08001659 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
Robert Love42e9a922008-12-09 15:10:17 -08001660 goto cleanup;
Robert Love74147052009-06-10 15:31:10 -07001661 FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
1662 "frame error %d\n", err);
Robert Love42e9a922008-12-09 15:10:17 -08001663 return;
1664 }
1665
1666 op = fc_frame_payload_op(fp);
1667 fc_frame_free(fp);
1668
1669 switch (op) {
1670 case ELS_LS_RJT:
Robert Love74147052009-06-10 15:31:10 -07001671 FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
Robert Love42e9a922008-12-09 15:10:17 -08001672 /* fall through */
1673 case ELS_LS_ACC:
1674 goto cleanup;
1675 default:
Robert Love74147052009-06-10 15:31:10 -07001676 FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
1677 "for RRQ", op);
Robert Love42e9a922008-12-09 15:10:17 -08001678 return;
1679 }
1680
1681cleanup:
1682 fc_exch_done(&aborted_ep->seq);
1683 /* drop hold for rec qual */
1684 fc_exch_release(aborted_ep);
1685}
1686
Robert Love1a7b75a2009-11-03 11:45:47 -08001687
1688/**
1689 * This function is for exch_seq_send function pointer in
1690 * struct libfc_function_template, see comment block on
1691 * exch_seq_send for description of this function.
1692 */
1693static struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
1694 struct fc_frame *fp,
1695 void (*resp)(struct fc_seq *,
1696 struct fc_frame *fp,
1697 void *arg),
1698 void (*destructor)(struct fc_seq *,
1699 void *),
1700 void *arg, u32 timer_msec)
1701{
1702 struct fc_exch *ep;
1703 struct fc_seq *sp = NULL;
1704 struct fc_frame_header *fh;
1705 int rc = 1;
1706
1707 ep = fc_exch_alloc(lp, fp);
1708 if (!ep) {
1709 fc_frame_free(fp);
1710 return NULL;
1711 }
1712 ep->esb_stat |= ESB_ST_SEQ_INIT;
1713 fh = fc_frame_header_get(fp);
1714 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1715 ep->resp = resp;
1716 ep->destructor = destructor;
1717 ep->arg = arg;
1718 ep->r_a_tov = FC_DEF_R_A_TOV;
1719 ep->lp = lp;
1720 sp = &ep->seq;
1721
1722 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1723 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1724 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1725 sp->cnt++;
1726
1727 if (ep->xid <= lp->lro_xid)
1728 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1729
1730 if (unlikely(lp->tt.frame_send(lp, fp)))
1731 goto err;
1732
1733 if (timer_msec)
1734 fc_exch_timer_set_locked(ep, timer_msec);
1735 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1736
1737 if (ep->f_ctl & FC_FC_SEQ_INIT)
1738 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1739 spin_unlock_bh(&ep->ex_lock);
1740 return sp;
1741err:
1742 rc = fc_exch_done_locked(ep);
1743 spin_unlock_bh(&ep->ex_lock);
1744 if (!rc)
1745 fc_exch_delete(ep);
1746 return NULL;
1747}
1748
Robert Love42e9a922008-12-09 15:10:17 -08001749/*
1750 * Send ELS RRQ - Reinstate Recovery Qualifier.
1751 * This tells the remote port to stop blocking the use of
1752 * the exchange and the seq_cnt range.
1753 */
1754static void fc_exch_rrq(struct fc_exch *ep)
1755{
1756 struct fc_lport *lp;
1757 struct fc_els_rrq *rrq;
1758 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001759 u32 did;
1760
1761 lp = ep->lp;
1762
1763 fp = fc_frame_alloc(lp, sizeof(*rrq));
1764 if (!fp)
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001765 goto retry;
1766
Robert Love42e9a922008-12-09 15:10:17 -08001767 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1768 memset(rrq, 0, sizeof(*rrq));
1769 rrq->rrq_cmd = ELS_RRQ;
1770 hton24(rrq->rrq_s_id, ep->sid);
1771 rrq->rrq_ox_id = htons(ep->oxid);
1772 rrq->rrq_rx_id = htons(ep->rxid);
1773
1774 did = ep->did;
1775 if (ep->esb_stat & ESB_ST_RESP)
1776 did = ep->sid;
1777
1778 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1779 fc_host_port_id(lp->host), FC_TYPE_ELS,
1780 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1781
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001782 if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov))
1783 return;
1784
1785retry:
1786 spin_lock_bh(&ep->ex_lock);
1787 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1788 spin_unlock_bh(&ep->ex_lock);
1789 /* drop hold for rec qual */
1790 fc_exch_release(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001791 return;
1792 }
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001793 ep->esb_stat |= ESB_ST_REC_QUAL;
1794 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1795 spin_unlock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001796}
1797
1798
1799/*
1800 * Handle incoming ELS RRQ - Reset Recovery Qualifier.
1801 */
1802static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1803{
Vasu Dev3f127ad2009-10-21 16:27:33 -07001804 struct fc_exch *ep = NULL; /* request or subject exchange */
Robert Love42e9a922008-12-09 15:10:17 -08001805 struct fc_els_rrq *rp;
1806 u32 sid;
1807 u16 xid;
1808 enum fc_els_rjt_explan explan;
1809
1810 rp = fc_frame_payload_get(fp, sizeof(*rp));
1811 explan = ELS_EXPL_INV_LEN;
1812 if (!rp)
1813 goto reject;
1814
1815 /*
1816 * lookup subject exchange.
1817 */
1818 ep = fc_seq_exch(sp);
1819 sid = ntoh24(rp->rrq_s_id); /* subject source */
1820 xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
1821 ep = fc_exch_find(ep->em, xid);
1822
1823 explan = ELS_EXPL_OXID_RXID;
1824 if (!ep)
1825 goto reject;
1826 spin_lock_bh(&ep->ex_lock);
1827 if (ep->oxid != ntohs(rp->rrq_ox_id))
1828 goto unlock_reject;
1829 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
1830 ep->rxid != FC_XID_UNKNOWN)
1831 goto unlock_reject;
1832 explan = ELS_EXPL_SID;
1833 if (ep->sid != sid)
1834 goto unlock_reject;
1835
1836 /*
1837 * Clear Recovery Qualifier state, and cancel timer if complete.
1838 */
1839 if (ep->esb_stat & ESB_ST_REC_QUAL) {
1840 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1841 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
1842 }
1843 if (ep->esb_stat & ESB_ST_COMPLETE) {
1844 if (cancel_delayed_work(&ep->timeout_work))
1845 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
1846 }
1847
1848 spin_unlock_bh(&ep->ex_lock);
1849
1850 /*
1851 * Send LS_ACC.
1852 */
1853 fc_seq_ls_acc(sp);
Vasu Dev3f127ad2009-10-21 16:27:33 -07001854 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001855
1856unlock_reject:
1857 spin_unlock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001858reject:
1859 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
Vasu Dev3f127ad2009-10-21 16:27:33 -07001860out:
Robert Love42e9a922008-12-09 15:10:17 -08001861 fc_frame_free(fp);
Vasu Dev3f127ad2009-10-21 16:27:33 -07001862 if (ep)
1863 fc_exch_release(ep); /* drop hold from fc_exch_find */
Robert Love42e9a922008-12-09 15:10:17 -08001864}
1865
Vasu Dev96316092009-07-29 17:05:00 -07001866struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
1867 struct fc_exch_mgr *mp,
1868 bool (*match)(struct fc_frame *))
1869{
1870 struct fc_exch_mgr_anchor *ema;
1871
1872 ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
1873 if (!ema)
1874 return ema;
1875
1876 ema->mp = mp;
1877 ema->match = match;
1878 /* add EM anchor to EM anchors list */
1879 list_add_tail(&ema->ema_list, &lport->ema_list);
1880 kref_get(&mp->kref);
1881 return ema;
1882}
1883EXPORT_SYMBOL(fc_exch_mgr_add);
1884
1885static void fc_exch_mgr_destroy(struct kref *kref)
1886{
1887 struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
1888
Vasu Dev96316092009-07-29 17:05:00 -07001889 mempool_destroy(mp->ep_pool);
Vasu Deve4bc50b2009-08-25 13:58:47 -07001890 free_percpu(mp->pool);
Vasu Dev96316092009-07-29 17:05:00 -07001891 kfree(mp);
1892}
1893
1894void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
1895{
1896 /* remove EM anchor from EM anchors list */
1897 list_del(&ema->ema_list);
1898 kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
1899 kfree(ema);
1900}
1901EXPORT_SYMBOL(fc_exch_mgr_del);
1902
Robert Love42e9a922008-12-09 15:10:17 -08001903struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1904 enum fc_class class,
Vasu Dev52ff8782009-07-29 17:05:10 -07001905 u16 min_xid, u16 max_xid,
1906 bool (*match)(struct fc_frame *))
Robert Love42e9a922008-12-09 15:10:17 -08001907{
1908 struct fc_exch_mgr *mp;
Vasu Deve4bc50b2009-08-25 13:58:47 -07001909 u16 pool_exch_range;
1910 size_t pool_size;
1911 unsigned int cpu;
1912 struct fc_exch_pool *pool;
Robert Love42e9a922008-12-09 15:10:17 -08001913
Vasu Deve4bc50b2009-08-25 13:58:47 -07001914 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
1915 (min_xid & fc_cpu_mask) != 0) {
Robert Love74147052009-06-10 15:31:10 -07001916 FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
1917 min_xid, max_xid);
Robert Love42e9a922008-12-09 15:10:17 -08001918 return NULL;
1919 }
1920
1921 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001922 * allocate memory for EM
Robert Love42e9a922008-12-09 15:10:17 -08001923 */
Vasu Devb2f00912009-08-25 13:58:53 -07001924 mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
Robert Love42e9a922008-12-09 15:10:17 -08001925 if (!mp)
1926 return NULL;
1927
1928 mp->class = class;
Robert Love42e9a922008-12-09 15:10:17 -08001929 /* adjust em exch xid range for offload */
1930 mp->min_xid = min_xid;
1931 mp->max_xid = max_xid;
Robert Love42e9a922008-12-09 15:10:17 -08001932
1933 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
1934 if (!mp->ep_pool)
1935 goto free_mp;
1936
Vasu Deve4bc50b2009-08-25 13:58:47 -07001937 /*
1938 * Setup per cpu exch pool with entire exchange id range equally
1939 * divided across all cpus. The exch pointers array memory is
1940 * allocated for exch range per pool.
1941 */
1942 pool_exch_range = (mp->max_xid - mp->min_xid + 1) / (fc_cpu_mask + 1);
1943 mp->pool_max_index = pool_exch_range - 1;
1944
1945 /*
1946 * Allocate and initialize per cpu exch pool
1947 */
1948 pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
1949 mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
1950 if (!mp->pool)
1951 goto free_mempool;
1952 for_each_possible_cpu(cpu) {
1953 pool = per_cpu_ptr(mp->pool, cpu);
1954 spin_lock_init(&pool->lock);
1955 INIT_LIST_HEAD(&pool->ex_list);
1956 }
1957
Vasu Dev52ff8782009-07-29 17:05:10 -07001958 kref_init(&mp->kref);
1959 if (!fc_exch_mgr_add(lp, mp, match)) {
Vasu Deve4bc50b2009-08-25 13:58:47 -07001960 free_percpu(mp->pool);
1961 goto free_mempool;
Vasu Dev52ff8782009-07-29 17:05:10 -07001962 }
1963
1964 /*
1965 * Above kref_init() sets mp->kref to 1 and then
1966 * call to fc_exch_mgr_add incremented mp->kref again,
1967 * so adjust that extra increment.
1968 */
1969 kref_put(&mp->kref, fc_exch_mgr_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001970 return mp;
1971
Vasu Deve4bc50b2009-08-25 13:58:47 -07001972free_mempool:
1973 mempool_destroy(mp->ep_pool);
Robert Love42e9a922008-12-09 15:10:17 -08001974free_mp:
1975 kfree(mp);
1976 return NULL;
1977}
1978EXPORT_SYMBOL(fc_exch_mgr_alloc);
1979
Vasu Dev52ff8782009-07-29 17:05:10 -07001980void fc_exch_mgr_free(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001981{
Vasu Dev52ff8782009-07-29 17:05:10 -07001982 struct fc_exch_mgr_anchor *ema, *next;
1983
1984 list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
1985 fc_exch_mgr_del(ema);
Robert Love42e9a922008-12-09 15:10:17 -08001986}
1987EXPORT_SYMBOL(fc_exch_mgr_free);
1988
Robert Love42e9a922008-12-09 15:10:17 -08001989/*
1990 * Receive a frame
1991 */
Vasu Dev52ff8782009-07-29 17:05:10 -07001992void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001993{
1994 struct fc_frame_header *fh = fc_frame_header_get(fp);
Vasu Dev52ff8782009-07-29 17:05:10 -07001995 struct fc_exch_mgr_anchor *ema;
1996 u32 f_ctl, found = 0;
1997 u16 oxid;
Robert Love42e9a922008-12-09 15:10:17 -08001998
1999 /* lport lock ? */
Vasu Dev52ff8782009-07-29 17:05:10 -07002000 if (!lp || lp->state == LPORT_ST_DISABLED) {
Robert Love74147052009-06-10 15:31:10 -07002001 FC_LPORT_DBG(lp, "Receiving frames for an lport that "
2002 "has not been initialized correctly\n");
Robert Love42e9a922008-12-09 15:10:17 -08002003 fc_frame_free(fp);
2004 return;
2005 }
2006
Vasu Dev52ff8782009-07-29 17:05:10 -07002007 f_ctl = ntoh24(fh->fh_f_ctl);
2008 oxid = ntohs(fh->fh_ox_id);
2009 if (f_ctl & FC_FC_EX_CTX) {
2010 list_for_each_entry(ema, &lp->ema_list, ema_list) {
2011 if ((oxid >= ema->mp->min_xid) &&
2012 (oxid <= ema->mp->max_xid)) {
2013 found = 1;
2014 break;
2015 }
2016 }
2017
2018 if (!found) {
2019 FC_LPORT_DBG(lp, "Received response for out "
2020 "of range oxid:%hx\n", oxid);
2021 fc_frame_free(fp);
2022 return;
2023 }
2024 } else
2025 ema = list_entry(lp->ema_list.prev, typeof(*ema), ema_list);
2026
Robert Love42e9a922008-12-09 15:10:17 -08002027 /*
2028 * If frame is marked invalid, just drop it.
2029 */
Robert Love42e9a922008-12-09 15:10:17 -08002030 switch (fr_eof(fp)) {
2031 case FC_EOF_T:
2032 if (f_ctl & FC_FC_END_SEQ)
2033 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
2034 /* fall through */
2035 case FC_EOF_N:
2036 if (fh->fh_type == FC_TYPE_BLS)
Vasu Dev52ff8782009-07-29 17:05:10 -07002037 fc_exch_recv_bls(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08002038 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
2039 FC_FC_EX_CTX)
Vasu Dev52ff8782009-07-29 17:05:10 -07002040 fc_exch_recv_seq_resp(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08002041 else if (f_ctl & FC_FC_SEQ_CTX)
Vasu Dev52ff8782009-07-29 17:05:10 -07002042 fc_exch_recv_resp(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08002043 else
Vasu Dev52ff8782009-07-29 17:05:10 -07002044 fc_exch_recv_req(lp, ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08002045 break;
2046 default:
Robert Loved459b7e2009-07-29 17:05:05 -07002047 FC_LPORT_DBG(lp, "dropping invalid frame (eof %x)", fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -08002048 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08002049 }
2050}
2051EXPORT_SYMBOL(fc_exch_recv);
2052
2053int fc_exch_init(struct fc_lport *lp)
2054{
Robert Love42e9a922008-12-09 15:10:17 -08002055 if (!lp->tt.seq_start_next)
2056 lp->tt.seq_start_next = fc_seq_start_next;
2057
2058 if (!lp->tt.exch_seq_send)
2059 lp->tt.exch_seq_send = fc_exch_seq_send;
2060
2061 if (!lp->tt.seq_send)
2062 lp->tt.seq_send = fc_seq_send;
2063
2064 if (!lp->tt.seq_els_rsp_send)
2065 lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
2066
2067 if (!lp->tt.exch_done)
2068 lp->tt.exch_done = fc_exch_done;
2069
2070 if (!lp->tt.exch_mgr_reset)
2071 lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
2072
2073 if (!lp->tt.seq_exch_abort)
2074 lp->tt.seq_exch_abort = fc_seq_exch_abort;
2075
Vasu Dev89f19a52009-10-21 16:27:28 -07002076 return 0;
2077}
2078EXPORT_SYMBOL(fc_exch_init);
2079
2080/**
2081 * fc_setup_exch_mgr() - Setup an exchange manager
2082 */
2083int fc_setup_exch_mgr()
2084{
2085 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
2086 0, SLAB_HWCACHE_ALIGN, NULL);
2087 if (!fc_em_cachep)
2088 return -ENOMEM;
2089
Vasu Deve4bc50b2009-08-25 13:58:47 -07002090 /*
2091 * Initialize fc_cpu_mask and fc_cpu_order. The
2092 * fc_cpu_mask is set for nr_cpu_ids rounded up
2093 * to order of 2's * power and order is stored
2094 * in fc_cpu_order as this is later required in
2095 * mapping between an exch id and exch array index
2096 * in per cpu exch pool.
2097 *
2098 * This round up is required to align fc_cpu_mask
2099 * to exchange id's lower bits such that all incoming
2100 * frames of an exchange gets delivered to the same
2101 * cpu on which exchange originated by simple bitwise
2102 * AND operation between fc_cpu_mask and exchange id.
2103 */
2104 fc_cpu_mask = 1;
2105 fc_cpu_order = 0;
2106 while (fc_cpu_mask < nr_cpu_ids) {
2107 fc_cpu_mask <<= 1;
2108 fc_cpu_order++;
2109 }
2110 fc_cpu_mask--;
2111
Robert Love42e9a922008-12-09 15:10:17 -08002112 return 0;
2113}
Robert Love42e9a922008-12-09 15:10:17 -08002114
2115void fc_destroy_exch_mgr(void)
2116{
2117 kmem_cache_destroy(fc_em_cachep);
2118}