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