blob: 659bb05287f3c80b90695142f199c5dedb84ce81 [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 *);
Robert Love42e9a922008-12-09 15:10:17 -0800110
111/*
112 * Internal implementation notes.
113 *
114 * The exchange manager is one by default in libfc but LLD may choose
115 * to have one per CPU. The sequence manager is one per exchange manager
116 * and currently never separated.
117 *
118 * Section 9.8 in FC-FS-2 specifies: "The SEQ_ID is a one-byte field
119 * assigned by the Sequence Initiator that shall be unique for a specific
120 * D_ID and S_ID pair while the Sequence is open." Note that it isn't
121 * qualified by exchange ID, which one might think it would be.
122 * In practice this limits the number of open sequences and exchanges to 256
123 * per session. For most targets we could treat this limit as per exchange.
124 *
125 * The exchange and its sequence are freed when the last sequence is received.
126 * It's possible for the remote port to leave an exchange open without
127 * sending any sequences.
128 *
129 * Notes on reference counts:
130 *
131 * Exchanges are reference counted and exchange gets freed when the reference
132 * count becomes zero.
133 *
134 * Timeouts:
135 * Sequences are timed out for E_D_TOV and R_A_TOV.
136 *
137 * Sequence event handling:
138 *
139 * The following events may occur on initiator sequences:
140 *
141 * Send.
142 * For now, the whole thing is sent.
143 * Receive ACK
144 * This applies only to class F.
145 * The sequence is marked complete.
146 * ULP completion.
147 * The upper layer calls fc_exch_done() when done
148 * with exchange and sequence tuple.
149 * RX-inferred completion.
150 * When we receive the next sequence on the same exchange, we can
151 * retire the previous sequence ID. (XXX not implemented).
152 * Timeout.
153 * R_A_TOV frees the sequence ID. If we're waiting for ACK,
154 * E_D_TOV causes abort and calls upper layer response handler
155 * with FC_EX_TIMEOUT error.
156 * Receive RJT
157 * XXX defer.
158 * Send ABTS
159 * On timeout.
160 *
161 * The following events may occur on recipient sequences:
162 *
163 * Receive
164 * Allocate sequence for first frame received.
165 * Hold during receive handler.
166 * Release when final frame received.
167 * Keep status of last N of these for the ELS RES command. XXX TBD.
168 * Receive ABTS
169 * Deallocate sequence
170 * Send RJT
171 * Deallocate
172 *
173 * For now, we neglect conditions where only part of a sequence was
174 * received or transmitted, or where out-of-order receipt is detected.
175 */
176
177/*
178 * Locking notes:
179 *
180 * The EM code run in a per-CPU worker thread.
181 *
182 * To protect against concurrency between a worker thread code and timers,
183 * sequence allocation and deallocation must be locked.
184 * - exchange refcnt can be done atomicly without locks.
185 * - sequence allocation must be locked by exch lock.
Vasu Devb2f00912009-08-25 13:58:53 -0700186 * - If the EM pool lock and ex_lock must be taken at the same time, then the
187 * EM pool lock must be taken before the ex_lock.
Robert Love42e9a922008-12-09 15:10:17 -0800188 */
189
190/*
191 * opcode names for debugging.
192 */
193static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
194
195#define FC_TABLE_SIZE(x) (sizeof(x) / sizeof(x[0]))
196
197static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
198 unsigned int max_index)
199{
200 const char *name = NULL;
201
202 if (op < max_index)
203 name = table[op];
204 if (!name)
205 name = "unknown";
206 return name;
207}
208
209static const char *fc_exch_rctl_name(unsigned int op)
210{
211 return fc_exch_name_lookup(op, fc_exch_rctl_names,
212 FC_TABLE_SIZE(fc_exch_rctl_names));
213}
214
215/*
216 * Hold an exchange - keep it from being freed.
217 */
218static void fc_exch_hold(struct fc_exch *ep)
219{
220 atomic_inc(&ep->ex_refcnt);
221}
222
223/*
224 * setup fc hdr by initializing few more FC header fields and sof/eof.
225 * Initialized fields by this func:
226 * - fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt
227 * - sof and eof
228 */
229static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
230 u32 f_ctl)
231{
232 struct fc_frame_header *fh = fc_frame_header_get(fp);
233 u16 fill;
234
235 fr_sof(fp) = ep->class;
236 if (ep->seq.cnt)
237 fr_sof(fp) = fc_sof_normal(ep->class);
238
239 if (f_ctl & FC_FC_END_SEQ) {
240 fr_eof(fp) = FC_EOF_T;
241 if (fc_sof_needs_ack(ep->class))
242 fr_eof(fp) = FC_EOF_N;
243 /*
244 * Form f_ctl.
245 * The number of fill bytes to make the length a 4-byte
246 * multiple is the low order 2-bits of the f_ctl.
247 * The fill itself will have been cleared by the frame
248 * allocation.
249 * After this, the length will be even, as expected by
250 * the transport.
251 */
252 fill = fr_len(fp) & 3;
253 if (fill) {
254 fill = 4 - fill;
255 /* TODO, this may be a problem with fragmented skb */
256 skb_put(fp_skb(fp), fill);
257 hton24(fh->fh_f_ctl, f_ctl | fill);
258 }
259 } else {
260 WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
261 fr_eof(fp) = FC_EOF_N;
262 }
263
264 /*
265 * Initialize remainig fh fields
266 * from fc_fill_fc_hdr
267 */
268 fh->fh_ox_id = htons(ep->oxid);
269 fh->fh_rx_id = htons(ep->rxid);
270 fh->fh_seq_id = ep->seq.id;
271 fh->fh_seq_cnt = htons(ep->seq.cnt);
272}
273
Robert Love42e9a922008-12-09 15:10:17 -0800274/*
275 * Release a reference to an exchange.
276 * If the refcnt goes to zero and the exchange is complete, it is freed.
277 */
278static void fc_exch_release(struct fc_exch *ep)
279{
280 struct fc_exch_mgr *mp;
281
282 if (atomic_dec_and_test(&ep->ex_refcnt)) {
283 mp = ep->em;
284 if (ep->destructor)
285 ep->destructor(&ep->seq, ep->arg);
Julia Lawallaa6cd292009-02-04 22:17:29 +0100286 WARN_ON(!(ep->esb_stat & ESB_ST_COMPLETE));
Robert Love42e9a922008-12-09 15:10:17 -0800287 mempool_free(ep, mp->ep_pool);
288 }
289}
290
291static int fc_exch_done_locked(struct fc_exch *ep)
292{
293 int rc = 1;
294
295 /*
296 * We must check for completion in case there are two threads
297 * tyring to complete this. But the rrq code will reuse the
298 * ep, and in that case we only clear the resp and set it as
299 * complete, so it can be reused by the timer to send the rrq.
300 */
301 ep->resp = NULL;
302 if (ep->state & FC_EX_DONE)
303 return rc;
304 ep->esb_stat |= ESB_ST_COMPLETE;
305
306 if (!(ep->esb_stat & ESB_ST_REC_QUAL)) {
307 ep->state |= FC_EX_DONE;
308 if (cancel_delayed_work(&ep->timeout_work))
309 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
310 rc = 0;
311 }
312 return rc;
313}
314
Vasu Deve4bc50b2009-08-25 13:58:47 -0700315static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
316 u16 index)
317{
318 struct fc_exch **exches = (struct fc_exch **)(pool + 1);
319 return exches[index];
320}
321
322static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
323 struct fc_exch *ep)
324{
325 ((struct fc_exch **)(pool + 1))[index] = ep;
326}
327
Vasu Devb2f00912009-08-25 13:58:53 -0700328static void fc_exch_delete(struct fc_exch *ep)
Robert Love42e9a922008-12-09 15:10:17 -0800329{
Vasu Devb2f00912009-08-25 13:58:53 -0700330 struct fc_exch_pool *pool;
Robert Love42e9a922008-12-09 15:10:17 -0800331
Vasu Devb2f00912009-08-25 13:58:53 -0700332 pool = ep->pool;
333 spin_lock_bh(&pool->lock);
334 WARN_ON(pool->total_exches <= 0);
335 pool->total_exches--;
336 fc_exch_ptr_set(pool, (ep->xid - ep->em->min_xid) >> fc_cpu_order,
337 NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800338 list_del(&ep->ex_list);
Vasu Devb2f00912009-08-25 13:58:53 -0700339 spin_unlock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -0800340 fc_exch_release(ep); /* drop hold for exch in mp */
341}
342
343/*
344 * Internal version of fc_exch_timer_set - used with lock held.
345 */
346static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
347 unsigned int timer_msec)
348{
349 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
350 return;
351
Robert Lovecd305ce2009-08-25 13:58:37 -0700352 FC_EXCH_DBG(ep, "Exchange timer armed\n");
Robert Love74147052009-06-10 15:31:10 -0700353
Robert Love42e9a922008-12-09 15:10:17 -0800354 if (schedule_delayed_work(&ep->timeout_work,
355 msecs_to_jiffies(timer_msec)))
356 fc_exch_hold(ep); /* hold for timer */
357}
358
359/*
360 * Set timer for an exchange.
361 * The time is a minimum delay in milliseconds until the timer fires.
362 * Used for upper level protocols to time out the exchange.
363 * The timer is cancelled when it fires or when the exchange completes.
364 * Returns non-zero if a timer couldn't be allocated.
365 */
366static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
367{
368 spin_lock_bh(&ep->ex_lock);
369 fc_exch_timer_set_locked(ep, timer_msec);
370 spin_unlock_bh(&ep->ex_lock);
371}
372
Robert Love1a7b75a2009-11-03 11:45:47 -0800373/**
374 * send a frame using existing sequence and exchange.
375 */
376static int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp,
377 struct fc_frame *fp)
378{
379 struct fc_exch *ep;
380 struct fc_frame_header *fh = fc_frame_header_get(fp);
381 int error;
382 u32 f_ctl;
383
384 ep = fc_seq_exch(sp);
385 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
386
387 f_ctl = ntoh24(fh->fh_f_ctl);
388 fc_exch_setup_hdr(ep, fp, f_ctl);
389
390 /*
391 * update sequence count if this frame is carrying
392 * multiple FC frames when sequence offload is enabled
393 * by LLD.
394 */
395 if (fr_max_payload(fp))
396 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
397 fr_max_payload(fp));
398 else
399 sp->cnt++;
400
401 /*
402 * Send the frame.
403 */
404 error = lp->tt.frame_send(lp, fp);
405
406 /*
407 * Update the exchange and sequence flags,
408 * assuming all frames for the sequence have been sent.
409 * We can only be called to send once for each sequence.
410 */
411 spin_lock_bh(&ep->ex_lock);
412 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
413 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
414 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
415 spin_unlock_bh(&ep->ex_lock);
416 return error;
417}
418
419/**
420 * fc_seq_alloc() - Allocate a sequence.
421 * @ep: Exchange pointer
422 * @seq_id: Sequence ID to allocate a sequence for
423 *
424 * We don't support multiple originated sequences on the same exchange.
425 * By implication, any previously originated sequence on this exchange
426 * is complete, and we reallocate the same sequence.
427 */
428static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
429{
430 struct fc_seq *sp;
431
432 sp = &ep->seq;
433 sp->ssb_stat = 0;
434 sp->cnt = 0;
435 sp->id = seq_id;
436 return sp;
437}
438
439static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
440{
441 struct fc_exch *ep = fc_seq_exch(sp);
442
443 sp = fc_seq_alloc(ep, ep->seq_id++);
444 FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
445 ep->f_ctl, sp->id);
446 return sp;
447}
448
449/**
450 * Allocate a new sequence on the same exchange as the supplied sequence.
451 * This will never return NULL.
452 */
453static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
454{
455 struct fc_exch *ep = fc_seq_exch(sp);
456
457 spin_lock_bh(&ep->ex_lock);
458 sp = fc_seq_start_next_locked(sp);
459 spin_unlock_bh(&ep->ex_lock);
460
461 return sp;
462}
463
464/**
465 * This function is for seq_exch_abort function pointer in
466 * struct libfc_function_template, see comment block on
467 * seq_exch_abort for description of this function.
468 */
469static int fc_seq_exch_abort(const struct fc_seq *req_sp,
470 unsigned int timer_msec)
Robert Love42e9a922008-12-09 15:10:17 -0800471{
472 struct fc_seq *sp;
473 struct fc_exch *ep;
474 struct fc_frame *fp;
475 int error;
476
477 ep = fc_seq_exch(req_sp);
478
479 spin_lock_bh(&ep->ex_lock);
480 if (ep->esb_stat & (ESB_ST_COMPLETE | ESB_ST_ABNORMAL) ||
481 ep->state & (FC_EX_DONE | FC_EX_RST_CLEANUP)) {
482 spin_unlock_bh(&ep->ex_lock);
483 return -ENXIO;
484 }
485
486 /*
487 * Send the abort on a new sequence if possible.
488 */
489 sp = fc_seq_start_next_locked(&ep->seq);
490 if (!sp) {
491 spin_unlock_bh(&ep->ex_lock);
492 return -ENOMEM;
493 }
494
495 ep->esb_stat |= ESB_ST_SEQ_INIT | ESB_ST_ABNORMAL;
496 if (timer_msec)
497 fc_exch_timer_set_locked(ep, timer_msec);
498 spin_unlock_bh(&ep->ex_lock);
499
500 /*
501 * If not logged into the fabric, don't send ABTS but leave
502 * sequence active until next timeout.
503 */
504 if (!ep->sid)
505 return 0;
506
507 /*
508 * Send an abort for the sequence that timed out.
509 */
510 fp = fc_frame_alloc(ep->lp, 0);
511 if (fp) {
512 fc_fill_fc_hdr(fp, FC_RCTL_BA_ABTS, ep->did, ep->sid,
513 FC_TYPE_BLS, FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
514 error = fc_seq_send(ep->lp, sp, fp);
515 } else
516 error = -ENOBUFS;
517 return error;
518}
Robert Love42e9a922008-12-09 15:10:17 -0800519
520/*
521 * Exchange timeout - handle exchange timer expiration.
522 * The timer will have been cancelled before this is called.
523 */
524static void fc_exch_timeout(struct work_struct *work)
525{
526 struct fc_exch *ep = container_of(work, struct fc_exch,
527 timeout_work.work);
528 struct fc_seq *sp = &ep->seq;
529 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
530 void *arg;
531 u32 e_stat;
532 int rc = 1;
533
Robert Lovecd305ce2009-08-25 13:58:37 -0700534 FC_EXCH_DBG(ep, "Exchange timed out\n");
535
Robert Love42e9a922008-12-09 15:10:17 -0800536 spin_lock_bh(&ep->ex_lock);
537 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE))
538 goto unlock;
539
540 e_stat = ep->esb_stat;
541 if (e_stat & ESB_ST_COMPLETE) {
542 ep->esb_stat = e_stat & ~ESB_ST_REC_QUAL;
Vasu Deva0cc1ec2009-07-28 17:33:37 -0700543 spin_unlock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -0800544 if (e_stat & ESB_ST_REC_QUAL)
545 fc_exch_rrq(ep);
Robert Love42e9a922008-12-09 15:10:17 -0800546 goto done;
547 } else {
548 resp = ep->resp;
549 arg = ep->arg;
550 ep->resp = NULL;
551 if (e_stat & ESB_ST_ABNORMAL)
552 rc = fc_exch_done_locked(ep);
553 spin_unlock_bh(&ep->ex_lock);
554 if (!rc)
Vasu Devb2f00912009-08-25 13:58:53 -0700555 fc_exch_delete(ep);
Robert Love42e9a922008-12-09 15:10:17 -0800556 if (resp)
557 resp(sp, ERR_PTR(-FC_EX_TIMEOUT), arg);
558 fc_seq_exch_abort(sp, 2 * ep->r_a_tov);
559 goto done;
560 }
561unlock:
562 spin_unlock_bh(&ep->ex_lock);
563done:
564 /*
565 * This release matches the hold taken when the timer was set.
566 */
567 fc_exch_release(ep);
568}
569
Vasu Dev52ff8782009-07-29 17:05:10 -0700570/**
571 * fc_exch_em_alloc() - allocate an exchange from a specified EM.
572 * @lport: ptr to the local port
573 * @mp: ptr to the exchange manager
Robert Love42e9a922008-12-09 15:10:17 -0800574 *
Vasu Devd7179682009-07-29 17:05:21 -0700575 * Returns pointer to allocated fc_exch with exch lock held.
Robert Love42e9a922008-12-09 15:10:17 -0800576 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700577static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
Vasu Devd7179682009-07-29 17:05:21 -0700578 struct fc_exch_mgr *mp)
Robert Love42e9a922008-12-09 15:10:17 -0800579{
580 struct fc_exch *ep;
Vasu Devb2f00912009-08-25 13:58:53 -0700581 unsigned int cpu;
582 u16 index;
583 struct fc_exch_pool *pool;
Robert Love42e9a922008-12-09 15:10:17 -0800584
585 /* allocate memory for exchange */
586 ep = mempool_alloc(mp->ep_pool, GFP_ATOMIC);
587 if (!ep) {
588 atomic_inc(&mp->stats.no_free_exch);
589 goto out;
590 }
591 memset(ep, 0, sizeof(*ep));
592
Vasu Devb2f00912009-08-25 13:58:53 -0700593 cpu = smp_processor_id();
594 pool = per_cpu_ptr(mp->pool, cpu);
595 spin_lock_bh(&pool->lock);
596 index = pool->next_index;
597 /* allocate new exch from pool */
598 while (fc_exch_ptr_get(pool, index)) {
599 index = index == mp->pool_max_index ? 0 : index + 1;
600 if (index == pool->next_index)
Robert Love42e9a922008-12-09 15:10:17 -0800601 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800602 }
Vasu Devb2f00912009-08-25 13:58:53 -0700603 pool->next_index = index == mp->pool_max_index ? 0 : index + 1;
Robert Love42e9a922008-12-09 15:10:17 -0800604
605 fc_exch_hold(ep); /* hold for exch in mp */
606 spin_lock_init(&ep->ex_lock);
607 /*
608 * Hold exch lock for caller to prevent fc_exch_reset()
609 * from releasing exch while fc_exch_alloc() caller is
610 * still working on exch.
611 */
612 spin_lock_bh(&ep->ex_lock);
613
Vasu Devb2f00912009-08-25 13:58:53 -0700614 fc_exch_ptr_set(pool, index, ep);
615 list_add_tail(&ep->ex_list, &pool->ex_list);
Robert Love42e9a922008-12-09 15:10:17 -0800616 fc_seq_alloc(ep, ep->seq_id++);
Vasu Devb2f00912009-08-25 13:58:53 -0700617 pool->total_exches++;
618 spin_unlock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -0800619
620 /*
621 * update exchange
622 */
Vasu Devb2f00912009-08-25 13:58:53 -0700623 ep->oxid = ep->xid = (index << fc_cpu_order | cpu) + mp->min_xid;
Robert Love42e9a922008-12-09 15:10:17 -0800624 ep->em = mp;
Vasu Devb2f00912009-08-25 13:58:53 -0700625 ep->pool = pool;
Vasu Dev52ff8782009-07-29 17:05:10 -0700626 ep->lp = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800627 ep->f_ctl = FC_FC_FIRST_SEQ; /* next seq is first seq */
628 ep->rxid = FC_XID_UNKNOWN;
629 ep->class = mp->class;
630 INIT_DELAYED_WORK(&ep->timeout_work, fc_exch_timeout);
631out:
632 return ep;
633err:
Vasu Devb2f00912009-08-25 13:58:53 -0700634 spin_unlock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -0800635 atomic_inc(&mp->stats.no_free_exch_xid);
636 mempool_free(ep, mp->ep_pool);
637 return NULL;
638}
Vasu Dev52ff8782009-07-29 17:05:10 -0700639
640/**
641 * fc_exch_alloc() - allocate an exchange.
642 * @lport: ptr to the local port
643 * @fp: ptr to the FC frame
644 *
645 * This function walks the list of the exchange manager(EM)
646 * anchors to select a EM for new exchange allocation. The
647 * EM is selected having either a NULL match function pointer
648 * or call to match function returning true.
649 */
Robert Love1a7b75a2009-11-03 11:45:47 -0800650static struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
651 struct fc_frame *fp)
Vasu Dev52ff8782009-07-29 17:05:10 -0700652{
653 struct fc_exch_mgr_anchor *ema;
654 struct fc_exch *ep;
655
656 list_for_each_entry(ema, &lport->ema_list, ema_list) {
657 if (!ema->match || ema->match(fp)) {
Vasu Devd7179682009-07-29 17:05:21 -0700658 ep = fc_exch_em_alloc(lport, ema->mp);
Vasu Dev52ff8782009-07-29 17:05:10 -0700659 if (ep)
660 return ep;
661 }
662 }
663 return NULL;
664}
Robert Love42e9a922008-12-09 15:10:17 -0800665
666/*
667 * Lookup and hold an exchange.
668 */
669static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
670{
Vasu Devb2f00912009-08-25 13:58:53 -0700671 struct fc_exch_pool *pool;
Robert Love42e9a922008-12-09 15:10:17 -0800672 struct fc_exch *ep = NULL;
673
674 if ((xid >= mp->min_xid) && (xid <= mp->max_xid)) {
Vasu Devb2f00912009-08-25 13:58:53 -0700675 pool = per_cpu_ptr(mp->pool, xid & fc_cpu_mask);
676 spin_lock_bh(&pool->lock);
677 ep = fc_exch_ptr_get(pool, (xid - mp->min_xid) >> fc_cpu_order);
Robert Love42e9a922008-12-09 15:10:17 -0800678 if (ep) {
679 fc_exch_hold(ep);
680 WARN_ON(ep->xid != xid);
681 }
Vasu Devb2f00912009-08-25 13:58:53 -0700682 spin_unlock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -0800683 }
684 return ep;
685}
686
Robert Love1a7b75a2009-11-03 11:45:47 -0800687
688/**
689 * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
690 * the memory allocated for the related objects may be freed.
691 * @sp: Sequence pointer
692 */
693static void fc_exch_done(struct fc_seq *sp)
Robert Love42e9a922008-12-09 15:10:17 -0800694{
695 struct fc_exch *ep = fc_seq_exch(sp);
696 int rc;
697
698 spin_lock_bh(&ep->ex_lock);
699 rc = fc_exch_done_locked(ep);
700 spin_unlock_bh(&ep->ex_lock);
701 if (!rc)
Vasu Devb2f00912009-08-25 13:58:53 -0700702 fc_exch_delete(ep);
Robert Love42e9a922008-12-09 15:10:17 -0800703}
Robert Love42e9a922008-12-09 15:10:17 -0800704
705/*
706 * Allocate a new exchange as responder.
707 * Sets the responder ID in the frame header.
708 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700709static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
710 struct fc_exch_mgr *mp,
711 struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800712{
713 struct fc_exch *ep;
714 struct fc_frame_header *fh;
Robert Love42e9a922008-12-09 15:10:17 -0800715
Vasu Dev52ff8782009-07-29 17:05:10 -0700716 ep = fc_exch_alloc(lport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800717 if (ep) {
718 ep->class = fc_frame_class(fp);
719
720 /*
721 * Set EX_CTX indicating we're responding on this exchange.
722 */
723 ep->f_ctl |= FC_FC_EX_CTX; /* we're responding */
724 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not new */
725 fh = fc_frame_header_get(fp);
726 ep->sid = ntoh24(fh->fh_d_id);
727 ep->did = ntoh24(fh->fh_s_id);
728 ep->oid = ep->did;
729
730 /*
731 * Allocated exchange has placed the XID in the
732 * originator field. Move it to the responder field,
733 * and set the originator XID from the frame.
734 */
735 ep->rxid = ep->xid;
736 ep->oxid = ntohs(fh->fh_ox_id);
737 ep->esb_stat |= ESB_ST_RESP | ESB_ST_SEQ_INIT;
738 if ((ntoh24(fh->fh_f_ctl) & FC_FC_SEQ_INIT) == 0)
739 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
740
Robert Love42e9a922008-12-09 15:10:17 -0800741 fc_exch_hold(ep); /* hold for caller */
Vasu Dev52ff8782009-07-29 17:05:10 -0700742 spin_unlock_bh(&ep->ex_lock); /* lock from fc_exch_alloc */
Robert Love42e9a922008-12-09 15:10:17 -0800743 }
744 return ep;
745}
746
747/*
748 * Find a sequence for receive where the other end is originating the sequence.
749 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
750 * on the ep that should be released by the caller.
751 */
Vasu Dev52ff8782009-07-29 17:05:10 -0700752static enum fc_pf_rjt_reason fc_seq_lookup_recip(struct fc_lport *lport,
753 struct fc_exch_mgr *mp,
Robert Loveb2ab99c2009-02-27 10:55:50 -0800754 struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800755{
756 struct fc_frame_header *fh = fc_frame_header_get(fp);
757 struct fc_exch *ep = NULL;
758 struct fc_seq *sp = NULL;
759 enum fc_pf_rjt_reason reject = FC_RJT_NONE;
760 u32 f_ctl;
761 u16 xid;
762
763 f_ctl = ntoh24(fh->fh_f_ctl);
764 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != 0);
765
766 /*
767 * Lookup or create the exchange if we will be creating the sequence.
768 */
769 if (f_ctl & FC_FC_EX_CTX) {
770 xid = ntohs(fh->fh_ox_id); /* we originated exch */
771 ep = fc_exch_find(mp, xid);
772 if (!ep) {
773 atomic_inc(&mp->stats.xid_not_found);
774 reject = FC_RJT_OX_ID;
775 goto out;
776 }
777 if (ep->rxid == FC_XID_UNKNOWN)
778 ep->rxid = ntohs(fh->fh_rx_id);
779 else if (ep->rxid != ntohs(fh->fh_rx_id)) {
780 reject = FC_RJT_OX_ID;
781 goto rel;
782 }
783 } else {
784 xid = ntohs(fh->fh_rx_id); /* we are the responder */
785
786 /*
787 * Special case for MDS issuing an ELS TEST with a
788 * bad rxid of 0.
789 * XXX take this out once we do the proper reject.
790 */
791 if (xid == 0 && fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
792 fc_frame_payload_op(fp) == ELS_TEST) {
793 fh->fh_rx_id = htons(FC_XID_UNKNOWN);
794 xid = FC_XID_UNKNOWN;
795 }
796
797 /*
798 * new sequence - find the exchange
799 */
800 ep = fc_exch_find(mp, xid);
801 if ((f_ctl & FC_FC_FIRST_SEQ) && fc_sof_is_init(fr_sof(fp))) {
802 if (ep) {
803 atomic_inc(&mp->stats.xid_busy);
804 reject = FC_RJT_RX_ID;
805 goto rel;
806 }
Vasu Dev52ff8782009-07-29 17:05:10 -0700807 ep = fc_exch_resp(lport, mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800808 if (!ep) {
809 reject = FC_RJT_EXCH_EST; /* XXX */
810 goto out;
811 }
812 xid = ep->xid; /* get our XID */
813 } else if (!ep) {
814 atomic_inc(&mp->stats.xid_not_found);
815 reject = FC_RJT_RX_ID; /* XID not found */
816 goto out;
817 }
818 }
819
820 /*
821 * At this point, we have the exchange held.
822 * Find or create the sequence.
823 */
824 if (fc_sof_is_init(fr_sof(fp))) {
825 sp = fc_seq_start_next(&ep->seq);
826 if (!sp) {
827 reject = FC_RJT_SEQ_XS; /* exchange shortage */
828 goto rel;
829 }
830 sp->id = fh->fh_seq_id;
831 sp->ssb_stat |= SSB_ST_RESP;
832 } else {
833 sp = &ep->seq;
834 if (sp->id != fh->fh_seq_id) {
835 atomic_inc(&mp->stats.seq_not_found);
836 reject = FC_RJT_SEQ_ID; /* sequence/exch should exist */
837 goto rel;
838 }
839 }
840 WARN_ON(ep != fc_seq_exch(sp));
841
842 if (f_ctl & FC_FC_SEQ_INIT)
843 ep->esb_stat |= ESB_ST_SEQ_INIT;
844
845 fr_seq(fp) = sp;
846out:
847 return reject;
848rel:
849 fc_exch_done(&ep->seq);
850 fc_exch_release(ep); /* hold from fc_exch_find/fc_exch_resp */
851 return reject;
852}
853
854/*
855 * Find the sequence for a frame being received.
856 * We originated the sequence, so it should be found.
857 * We may or may not have originated the exchange.
858 * Does not hold the sequence for the caller.
859 */
860static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
861 struct fc_frame *fp)
862{
863 struct fc_frame_header *fh = fc_frame_header_get(fp);
864 struct fc_exch *ep;
865 struct fc_seq *sp = NULL;
866 u32 f_ctl;
867 u16 xid;
868
869 f_ctl = ntoh24(fh->fh_f_ctl);
870 WARN_ON((f_ctl & FC_FC_SEQ_CTX) != FC_FC_SEQ_CTX);
871 xid = ntohs((f_ctl & FC_FC_EX_CTX) ? fh->fh_ox_id : fh->fh_rx_id);
872 ep = fc_exch_find(mp, xid);
873 if (!ep)
874 return NULL;
875 if (ep->seq.id == fh->fh_seq_id) {
876 /*
877 * Save the RX_ID if we didn't previously know it.
878 */
879 sp = &ep->seq;
880 if ((f_ctl & FC_FC_EX_CTX) != 0 &&
881 ep->rxid == FC_XID_UNKNOWN) {
882 ep->rxid = ntohs(fh->fh_rx_id);
883 }
884 }
885 fc_exch_release(ep);
886 return sp;
887}
888
889/*
890 * Set addresses for an exchange.
891 * Note this must be done before the first sequence of the exchange is sent.
892 */
893static void fc_exch_set_addr(struct fc_exch *ep,
894 u32 orig_id, u32 resp_id)
895{
896 ep->oid = orig_id;
897 if (ep->esb_stat & ESB_ST_RESP) {
898 ep->sid = resp_id;
899 ep->did = orig_id;
900 } else {
901 ep->sid = orig_id;
902 ep->did = resp_id;
903 }
904}
905
Robert Love1a7b75a2009-11-03 11:45:47 -0800906/**
907 * fc_seq_els_rsp_send() - Send ELS response using mainly infomation
908 * in exchange and sequence in EM layer.
909 * @sp: Sequence pointer
910 * @els_cmd: ELS command
911 * @els_data: ELS data
Robert Love42e9a922008-12-09 15:10:17 -0800912 */
Robert Love1a7b75a2009-11-03 11:45:47 -0800913static void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
914 struct fc_seq_els_data *els_data)
Robert Love42e9a922008-12-09 15:10:17 -0800915{
916 switch (els_cmd) {
917 case ELS_LS_RJT:
918 fc_seq_ls_rjt(sp, els_data->reason, els_data->explan);
919 break;
920 case ELS_LS_ACC:
921 fc_seq_ls_acc(sp);
922 break;
923 case ELS_RRQ:
924 fc_exch_els_rrq(sp, els_data->fp);
925 break;
926 case ELS_REC:
927 fc_exch_els_rec(sp, els_data->fp);
928 break;
929 default:
Robert Love74147052009-06-10 15:31:10 -0700930 FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
Robert Love42e9a922008-12-09 15:10:17 -0800931 }
932}
Robert Love42e9a922008-12-09 15:10:17 -0800933
934/*
935 * Send a sequence, which is also the last sequence in the exchange.
936 */
937static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
938 enum fc_rctl rctl, enum fc_fh_type fh_type)
939{
940 u32 f_ctl;
941 struct fc_exch *ep = fc_seq_exch(sp);
942
943 f_ctl = FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
944 f_ctl |= ep->f_ctl;
945 fc_fill_fc_hdr(fp, rctl, ep->did, ep->sid, fh_type, f_ctl, 0);
946 fc_seq_send(ep->lp, sp, fp);
947}
948
949/*
950 * Send ACK_1 (or equiv.) indicating we received something.
951 * The frame we're acking is supplied.
952 */
953static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
954{
955 struct fc_frame *fp;
956 struct fc_frame_header *rx_fh;
957 struct fc_frame_header *fh;
958 struct fc_exch *ep = fc_seq_exch(sp);
959 struct fc_lport *lp = ep->lp;
960 unsigned int f_ctl;
961
962 /*
963 * Don't send ACKs for class 3.
964 */
965 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
966 fp = fc_frame_alloc(lp, 0);
967 if (!fp)
968 return;
969
970 fh = fc_frame_header_get(fp);
971 fh->fh_r_ctl = FC_RCTL_ACK_1;
972 fh->fh_type = FC_TYPE_BLS;
973
974 /*
975 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
976 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
977 * Bits 9-8 are meaningful (retransmitted or unidirectional).
978 * Last ACK uses bits 7-6 (continue sequence),
979 * bits 5-4 are meaningful (what kind of ACK to use).
980 */
981 rx_fh = fc_frame_header_get(rx_fp);
982 f_ctl = ntoh24(rx_fh->fh_f_ctl);
983 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
984 FC_FC_FIRST_SEQ | FC_FC_LAST_SEQ |
985 FC_FC_END_SEQ | FC_FC_END_CONN | FC_FC_SEQ_INIT |
986 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
987 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
988 hton24(fh->fh_f_ctl, f_ctl);
989
990 fc_exch_setup_hdr(ep, fp, f_ctl);
991 fh->fh_seq_id = rx_fh->fh_seq_id;
992 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
993 fh->fh_parm_offset = htonl(1); /* ack single frame */
994
995 fr_sof(fp) = fr_sof(rx_fp);
996 if (f_ctl & FC_FC_END_SEQ)
997 fr_eof(fp) = FC_EOF_T;
998 else
999 fr_eof(fp) = FC_EOF_N;
1000
1001 (void) lp->tt.frame_send(lp, fp);
1002 }
1003}
1004
1005/*
1006 * Send BLS Reject.
1007 * This is for rejecting BA_ABTS only.
1008 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001009static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1010 enum fc_ba_rjt_reason reason,
1011 enum fc_ba_rjt_explan explan)
Robert Love42e9a922008-12-09 15:10:17 -08001012{
1013 struct fc_frame *fp;
1014 struct fc_frame_header *rx_fh;
1015 struct fc_frame_header *fh;
1016 struct fc_ba_rjt *rp;
1017 struct fc_lport *lp;
1018 unsigned int f_ctl;
1019
1020 lp = fr_dev(rx_fp);
1021 fp = fc_frame_alloc(lp, sizeof(*rp));
1022 if (!fp)
1023 return;
1024 fh = fc_frame_header_get(fp);
1025 rx_fh = fc_frame_header_get(rx_fp);
1026
1027 memset(fh, 0, sizeof(*fh) + sizeof(*rp));
1028
1029 rp = fc_frame_payload_get(fp, sizeof(*rp));
1030 rp->br_reason = reason;
1031 rp->br_explan = explan;
1032
1033 /*
1034 * seq_id, cs_ctl, df_ctl and param/offset are zero.
1035 */
1036 memcpy(fh->fh_s_id, rx_fh->fh_d_id, 3);
1037 memcpy(fh->fh_d_id, rx_fh->fh_s_id, 3);
Joe Eykholt1d490ce2009-08-25 14:04:03 -07001038 fh->fh_ox_id = rx_fh->fh_ox_id;
1039 fh->fh_rx_id = rx_fh->fh_rx_id;
Robert Love42e9a922008-12-09 15:10:17 -08001040 fh->fh_seq_cnt = rx_fh->fh_seq_cnt;
1041 fh->fh_r_ctl = FC_RCTL_BA_RJT;
1042 fh->fh_type = FC_TYPE_BLS;
1043
1044 /*
1045 * Form f_ctl by inverting EX_CTX and SEQ_CTX (bits 23, 22).
1046 * Echo FIRST_SEQ, LAST_SEQ, END_SEQ, END_CONN, SEQ_INIT.
1047 * Bits 9-8 are meaningful (retransmitted or unidirectional).
1048 * Last ACK uses bits 7-6 (continue sequence),
1049 * bits 5-4 are meaningful (what kind of ACK to use).
1050 * Always set LAST_SEQ, END_SEQ.
1051 */
1052 f_ctl = ntoh24(rx_fh->fh_f_ctl);
1053 f_ctl &= FC_FC_EX_CTX | FC_FC_SEQ_CTX |
1054 FC_FC_END_CONN | FC_FC_SEQ_INIT |
1055 FC_FC_RETX_SEQ | FC_FC_UNI_TX;
1056 f_ctl ^= FC_FC_EX_CTX | FC_FC_SEQ_CTX;
1057 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1058 f_ctl &= ~FC_FC_FIRST_SEQ;
1059 hton24(fh->fh_f_ctl, f_ctl);
1060
1061 fr_sof(fp) = fc_sof_class(fr_sof(rx_fp));
1062 fr_eof(fp) = FC_EOF_T;
1063 if (fc_sof_needs_ack(fr_sof(fp)))
1064 fr_eof(fp) = FC_EOF_N;
1065
1066 (void) lp->tt.frame_send(lp, fp);
1067}
1068
1069/*
1070 * Handle an incoming ABTS. This would be for target mode usually,
1071 * but could be due to lost FCP transfer ready, confirm or RRQ.
1072 * We always handle this as an exchange abort, ignoring the parameter.
1073 */
1074static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1075{
1076 struct fc_frame *fp;
1077 struct fc_ba_acc *ap;
1078 struct fc_frame_header *fh;
1079 struct fc_seq *sp;
1080
1081 if (!ep)
1082 goto reject;
1083 spin_lock_bh(&ep->ex_lock);
1084 if (ep->esb_stat & ESB_ST_COMPLETE) {
1085 spin_unlock_bh(&ep->ex_lock);
1086 goto reject;
1087 }
1088 if (!(ep->esb_stat & ESB_ST_REC_QUAL))
1089 fc_exch_hold(ep); /* hold for REC_QUAL */
1090 ep->esb_stat |= ESB_ST_ABNORMAL | ESB_ST_REC_QUAL;
1091 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1092
1093 fp = fc_frame_alloc(ep->lp, sizeof(*ap));
1094 if (!fp) {
1095 spin_unlock_bh(&ep->ex_lock);
1096 goto free;
1097 }
1098 fh = fc_frame_header_get(fp);
1099 ap = fc_frame_payload_get(fp, sizeof(*ap));
1100 memset(ap, 0, sizeof(*ap));
1101 sp = &ep->seq;
1102 ap->ba_high_seq_cnt = htons(0xffff);
1103 if (sp->ssb_stat & SSB_ST_RESP) {
1104 ap->ba_seq_id = sp->id;
1105 ap->ba_seq_id_val = FC_BA_SEQ_ID_VAL;
1106 ap->ba_high_seq_cnt = fh->fh_seq_cnt;
1107 ap->ba_low_seq_cnt = htons(sp->cnt);
1108 }
Vasu Deva7e84f22009-02-27 10:54:51 -08001109 sp = fc_seq_start_next_locked(sp);
Robert Love42e9a922008-12-09 15:10:17 -08001110 spin_unlock_bh(&ep->ex_lock);
1111 fc_seq_send_last(sp, fp, FC_RCTL_BA_ACC, FC_TYPE_BLS);
1112 fc_frame_free(rx_fp);
1113 return;
1114
1115reject:
1116 fc_exch_send_ba_rjt(rx_fp, FC_BA_RJT_UNABLE, FC_BA_RJT_INV_XID);
1117free:
1118 fc_frame_free(rx_fp);
1119}
1120
1121/*
1122 * Handle receive where the other end is originating the sequence.
1123 */
1124static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1125 struct fc_frame *fp)
1126{
1127 struct fc_frame_header *fh = fc_frame_header_get(fp);
1128 struct fc_seq *sp = NULL;
1129 struct fc_exch *ep = NULL;
1130 enum fc_sof sof;
1131 enum fc_eof eof;
1132 u32 f_ctl;
1133 enum fc_pf_rjt_reason reject;
1134
1135 fr_seq(fp) = NULL;
Vasu Dev52ff8782009-07-29 17:05:10 -07001136 reject = fc_seq_lookup_recip(lp, mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001137 if (reject == FC_RJT_NONE) {
1138 sp = fr_seq(fp); /* sequence will be held */
1139 ep = fc_seq_exch(sp);
1140 sof = fr_sof(fp);
1141 eof = fr_eof(fp);
1142 f_ctl = ntoh24(fh->fh_f_ctl);
1143 fc_seq_send_ack(sp, fp);
1144
1145 /*
1146 * Call the receive function.
1147 *
1148 * The receive function may allocate a new sequence
1149 * over the old one, so we shouldn't change the
1150 * sequence after this.
1151 *
1152 * The frame will be freed by the receive function.
1153 * If new exch resp handler is valid then call that
1154 * first.
1155 */
1156 if (ep->resp)
1157 ep->resp(sp, fp, ep->arg);
1158 else
1159 lp->tt.lport_recv(lp, sp, fp);
1160 fc_exch_release(ep); /* release from lookup */
1161 } else {
Robert Loved459b7e2009-07-29 17:05:05 -07001162 FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject);
Robert Love42e9a922008-12-09 15:10:17 -08001163 fc_frame_free(fp);
1164 }
1165}
1166
1167/*
1168 * Handle receive where the other end is originating the sequence in
1169 * response to our exchange.
1170 */
1171static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1172{
1173 struct fc_frame_header *fh = fc_frame_header_get(fp);
1174 struct fc_seq *sp;
1175 struct fc_exch *ep;
1176 enum fc_sof sof;
1177 u32 f_ctl;
1178 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1179 void *ex_resp_arg;
1180 int rc;
1181
1182 ep = fc_exch_find(mp, ntohs(fh->fh_ox_id));
1183 if (!ep) {
1184 atomic_inc(&mp->stats.xid_not_found);
1185 goto out;
1186 }
Steve Ma30121d12009-05-06 10:52:29 -07001187 if (ep->esb_stat & ESB_ST_COMPLETE) {
1188 atomic_inc(&mp->stats.xid_not_found);
1189 goto out;
1190 }
Robert Love42e9a922008-12-09 15:10:17 -08001191 if (ep->rxid == FC_XID_UNKNOWN)
1192 ep->rxid = ntohs(fh->fh_rx_id);
1193 if (ep->sid != 0 && ep->sid != ntoh24(fh->fh_d_id)) {
1194 atomic_inc(&mp->stats.xid_not_found);
1195 goto rel;
1196 }
1197 if (ep->did != ntoh24(fh->fh_s_id) &&
1198 ep->did != FC_FID_FLOGI) {
1199 atomic_inc(&mp->stats.xid_not_found);
1200 goto rel;
1201 }
1202 sof = fr_sof(fp);
1203 if (fc_sof_is_init(sof)) {
1204 sp = fc_seq_start_next(&ep->seq);
1205 sp->id = fh->fh_seq_id;
1206 sp->ssb_stat |= SSB_ST_RESP;
1207 } else {
1208 sp = &ep->seq;
1209 if (sp->id != fh->fh_seq_id) {
1210 atomic_inc(&mp->stats.seq_not_found);
1211 goto rel;
1212 }
1213 }
1214 f_ctl = ntoh24(fh->fh_f_ctl);
1215 fr_seq(fp) = sp;
1216 if (f_ctl & FC_FC_SEQ_INIT)
1217 ep->esb_stat |= ESB_ST_SEQ_INIT;
1218
1219 if (fc_sof_needs_ack(sof))
1220 fc_seq_send_ack(sp, fp);
1221 resp = ep->resp;
1222 ex_resp_arg = ep->arg;
1223
1224 if (fh->fh_type != FC_TYPE_FCP && fr_eof(fp) == FC_EOF_T &&
1225 (f_ctl & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1226 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1227 spin_lock_bh(&ep->ex_lock);
1228 rc = fc_exch_done_locked(ep);
1229 WARN_ON(fc_seq_exch(sp) != ep);
1230 spin_unlock_bh(&ep->ex_lock);
1231 if (!rc)
Vasu Devb2f00912009-08-25 13:58:53 -07001232 fc_exch_delete(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001233 }
1234
1235 /*
1236 * Call the receive function.
1237 * The sequence is held (has a refcnt) for us,
1238 * but not for the receive function.
1239 *
1240 * The receive function may allocate a new sequence
1241 * over the old one, so we shouldn't change the
1242 * sequence after this.
1243 *
1244 * The frame will be freed by the receive function.
1245 * If new exch resp handler is valid then call that
1246 * first.
1247 */
1248 if (resp)
1249 resp(sp, fp, ex_resp_arg);
1250 else
1251 fc_frame_free(fp);
1252 fc_exch_release(ep);
1253 return;
1254rel:
1255 fc_exch_release(ep);
1256out:
1257 fc_frame_free(fp);
1258}
1259
1260/*
1261 * Handle receive for a sequence where other end is responding to our sequence.
1262 */
1263static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1264{
1265 struct fc_seq *sp;
1266
1267 sp = fc_seq_lookup_orig(mp, fp); /* doesn't hold sequence */
Robert Loved459b7e2009-07-29 17:05:05 -07001268
1269 if (!sp)
Robert Love42e9a922008-12-09 15:10:17 -08001270 atomic_inc(&mp->stats.xid_not_found);
Robert Loved459b7e2009-07-29 17:05:05 -07001271 else
Robert Love42e9a922008-12-09 15:10:17 -08001272 atomic_inc(&mp->stats.non_bls_resp);
Robert Loved459b7e2009-07-29 17:05:05 -07001273
Robert Love42e9a922008-12-09 15:10:17 -08001274 fc_frame_free(fp);
1275}
1276
1277/*
1278 * Handle the response to an ABTS for exchange or sequence.
1279 * This can be BA_ACC or BA_RJT.
1280 */
1281static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1282{
1283 void (*resp)(struct fc_seq *, struct fc_frame *fp, void *arg);
1284 void *ex_resp_arg;
1285 struct fc_frame_header *fh;
1286 struct fc_ba_acc *ap;
1287 struct fc_seq *sp;
1288 u16 low;
1289 u16 high;
1290 int rc = 1, has_rec = 0;
1291
1292 fh = fc_frame_header_get(fp);
Robert Love74147052009-06-10 15:31:10 -07001293 FC_EXCH_DBG(ep, "exch: BLS rctl %x - %s\n", fh->fh_r_ctl,
1294 fc_exch_rctl_name(fh->fh_r_ctl));
Robert Love42e9a922008-12-09 15:10:17 -08001295
1296 if (cancel_delayed_work_sync(&ep->timeout_work))
1297 fc_exch_release(ep); /* release from pending timer hold */
1298
1299 spin_lock_bh(&ep->ex_lock);
1300 switch (fh->fh_r_ctl) {
1301 case FC_RCTL_BA_ACC:
1302 ap = fc_frame_payload_get(fp, sizeof(*ap));
1303 if (!ap)
1304 break;
1305
1306 /*
1307 * Decide whether to establish a Recovery Qualifier.
1308 * We do this if there is a non-empty SEQ_CNT range and
1309 * SEQ_ID is the same as the one we aborted.
1310 */
1311 low = ntohs(ap->ba_low_seq_cnt);
1312 high = ntohs(ap->ba_high_seq_cnt);
1313 if ((ep->esb_stat & ESB_ST_REC_QUAL) == 0 &&
1314 (ap->ba_seq_id_val != FC_BA_SEQ_ID_VAL ||
1315 ap->ba_seq_id == ep->seq_id) && low != high) {
1316 ep->esb_stat |= ESB_ST_REC_QUAL;
1317 fc_exch_hold(ep); /* hold for recovery qualifier */
1318 has_rec = 1;
1319 }
1320 break;
1321 case FC_RCTL_BA_RJT:
1322 break;
1323 default:
1324 break;
1325 }
1326
1327 resp = ep->resp;
1328 ex_resp_arg = ep->arg;
1329
1330 /* do we need to do some other checks here. Can we reuse more of
1331 * fc_exch_recv_seq_resp
1332 */
1333 sp = &ep->seq;
1334 /*
1335 * do we want to check END_SEQ as well as LAST_SEQ here?
1336 */
1337 if (ep->fh_type != FC_TYPE_FCP &&
1338 ntoh24(fh->fh_f_ctl) & FC_FC_LAST_SEQ)
1339 rc = fc_exch_done_locked(ep);
1340 spin_unlock_bh(&ep->ex_lock);
1341 if (!rc)
Vasu Devb2f00912009-08-25 13:58:53 -07001342 fc_exch_delete(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001343
1344 if (resp)
1345 resp(sp, fp, ex_resp_arg);
1346 else
1347 fc_frame_free(fp);
1348
1349 if (has_rec)
1350 fc_exch_timer_set(ep, ep->r_a_tov);
1351
1352}
1353
1354/*
1355 * Receive BLS sequence.
1356 * This is always a sequence initiated by the remote side.
1357 * We may be either the originator or recipient of the exchange.
1358 */
1359static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1360{
1361 struct fc_frame_header *fh;
1362 struct fc_exch *ep;
1363 u32 f_ctl;
1364
1365 fh = fc_frame_header_get(fp);
1366 f_ctl = ntoh24(fh->fh_f_ctl);
1367 fr_seq(fp) = NULL;
1368
1369 ep = fc_exch_find(mp, (f_ctl & FC_FC_EX_CTX) ?
1370 ntohs(fh->fh_ox_id) : ntohs(fh->fh_rx_id));
1371 if (ep && (f_ctl & FC_FC_SEQ_INIT)) {
1372 spin_lock_bh(&ep->ex_lock);
1373 ep->esb_stat |= ESB_ST_SEQ_INIT;
1374 spin_unlock_bh(&ep->ex_lock);
1375 }
1376 if (f_ctl & FC_FC_SEQ_CTX) {
1377 /*
1378 * A response to a sequence we initiated.
1379 * This should only be ACKs for class 2 or F.
1380 */
1381 switch (fh->fh_r_ctl) {
1382 case FC_RCTL_ACK_1:
1383 case FC_RCTL_ACK_0:
1384 break;
1385 default:
Robert Love74147052009-06-10 15:31:10 -07001386 FC_EXCH_DBG(ep, "BLS rctl %x - %s received",
1387 fh->fh_r_ctl,
1388 fc_exch_rctl_name(fh->fh_r_ctl));
Robert Love42e9a922008-12-09 15:10:17 -08001389 break;
1390 }
1391 fc_frame_free(fp);
1392 } else {
1393 switch (fh->fh_r_ctl) {
1394 case FC_RCTL_BA_RJT:
1395 case FC_RCTL_BA_ACC:
1396 if (ep)
1397 fc_exch_abts_resp(ep, fp);
1398 else
1399 fc_frame_free(fp);
1400 break;
1401 case FC_RCTL_BA_ABTS:
1402 fc_exch_recv_abts(ep, fp);
1403 break;
1404 default: /* ignore junk */
1405 fc_frame_free(fp);
1406 break;
1407 }
1408 }
1409 if (ep)
1410 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1411}
1412
1413/*
1414 * Accept sequence with LS_ACC.
1415 * If this fails due to allocation or transmit congestion, assume the
1416 * originator will repeat the sequence.
1417 */
1418static void fc_seq_ls_acc(struct fc_seq *req_sp)
1419{
1420 struct fc_seq *sp;
1421 struct fc_els_ls_acc *acc;
1422 struct fc_frame *fp;
1423
1424 sp = fc_seq_start_next(req_sp);
1425 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1426 if (fp) {
1427 acc = fc_frame_payload_get(fp, sizeof(*acc));
1428 memset(acc, 0, sizeof(*acc));
1429 acc->la_cmd = ELS_LS_ACC;
1430 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1431 }
1432}
1433
1434/*
1435 * Reject sequence with ELS LS_RJT.
1436 * If this fails due to allocation or transmit congestion, assume the
1437 * originator will repeat the sequence.
1438 */
1439static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
1440 enum fc_els_rjt_explan explan)
1441{
1442 struct fc_seq *sp;
1443 struct fc_els_ls_rjt *rjt;
1444 struct fc_frame *fp;
1445
1446 sp = fc_seq_start_next(req_sp);
1447 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*rjt));
1448 if (fp) {
1449 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1450 memset(rjt, 0, sizeof(*rjt));
1451 rjt->er_cmd = ELS_LS_RJT;
1452 rjt->er_reason = reason;
1453 rjt->er_explan = explan;
1454 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1455 }
1456}
1457
1458static void fc_exch_reset(struct fc_exch *ep)
1459{
1460 struct fc_seq *sp;
1461 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
1462 void *arg;
1463 int rc = 1;
1464
1465 spin_lock_bh(&ep->ex_lock);
1466 ep->state |= FC_EX_RST_CLEANUP;
1467 /*
1468 * we really want to call del_timer_sync, but cannot due
1469 * to the lport calling with the lport lock held (some resp
1470 * functions can also grab the lport lock which could cause
1471 * a deadlock).
1472 */
1473 if (cancel_delayed_work(&ep->timeout_work))
1474 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1475 resp = ep->resp;
1476 ep->resp = NULL;
1477 if (ep->esb_stat & ESB_ST_REC_QUAL)
1478 atomic_dec(&ep->ex_refcnt); /* drop hold for rec_qual */
1479 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1480 arg = ep->arg;
1481 sp = &ep->seq;
1482 rc = fc_exch_done_locked(ep);
1483 spin_unlock_bh(&ep->ex_lock);
1484 if (!rc)
Vasu Devb2f00912009-08-25 13:58:53 -07001485 fc_exch_delete(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001486
1487 if (resp)
1488 resp(sp, ERR_PTR(-FC_EX_CLOSED), arg);
1489}
1490
Vasu Devb2f00912009-08-25 13:58:53 -07001491/**
1492 * fc_exch_pool_reset() - Resets an per cpu exches pool.
1493 * @lport: ptr to the local port
1494 * @pool: ptr to the per cpu exches pool
1495 * @sid: source FC ID
1496 * @did: destination FC ID
1497 *
1498 * Resets an per cpu exches pool, releasing its all sequences
1499 * and exchanges. If sid is non-zero, then reset only exchanges
1500 * we sourced from that FID. If did is non-zero, reset only
1501 * exchanges destined to that FID.
Robert Love42e9a922008-12-09 15:10:17 -08001502 */
Vasu Devb2f00912009-08-25 13:58:53 -07001503static void fc_exch_pool_reset(struct fc_lport *lport,
1504 struct fc_exch_pool *pool,
1505 u32 sid, u32 did)
Robert Love42e9a922008-12-09 15:10:17 -08001506{
1507 struct fc_exch *ep;
1508 struct fc_exch *next;
Robert Love42e9a922008-12-09 15:10:17 -08001509
Vasu Devb2f00912009-08-25 13:58:53 -07001510 spin_lock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -08001511restart:
Vasu Devb2f00912009-08-25 13:58:53 -07001512 list_for_each_entry_safe(ep, next, &pool->ex_list, ex_list) {
1513 if ((lport == ep->lp) &&
1514 (sid == 0 || sid == ep->sid) &&
1515 (did == 0 || did == ep->did)) {
1516 fc_exch_hold(ep);
1517 spin_unlock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -08001518
Vasu Devb2f00912009-08-25 13:58:53 -07001519 fc_exch_reset(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001520
Vasu Devb2f00912009-08-25 13:58:53 -07001521 fc_exch_release(ep);
1522 spin_lock_bh(&pool->lock);
Robert Love42e9a922008-12-09 15:10:17 -08001523
Vasu Devb2f00912009-08-25 13:58:53 -07001524 /*
1525 * must restart loop incase while lock
1526 * was down multiple eps were released.
1527 */
1528 goto restart;
Robert Love42e9a922008-12-09 15:10:17 -08001529 }
Vasu Devb2f00912009-08-25 13:58:53 -07001530 }
1531 spin_unlock_bh(&pool->lock);
1532}
1533
1534/**
1535 * fc_exch_mgr_reset() - Resets all EMs of a lport
1536 * @lport: ptr to the local port
1537 * @sid: source FC ID
1538 * @did: destination FC ID
1539 *
1540 * Reset all EMs of a lport, releasing its all sequences and
1541 * exchanges. If sid is non-zero, then reset only exchanges
1542 * we sourced from that FID. If did is non-zero, reset only
1543 * exchanges destined to that FID.
1544 */
1545void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1546{
1547 struct fc_exch_mgr_anchor *ema;
1548 unsigned int cpu;
1549
1550 list_for_each_entry(ema, &lport->ema_list, ema_list) {
1551 for_each_possible_cpu(cpu)
1552 fc_exch_pool_reset(lport,
1553 per_cpu_ptr(ema->mp->pool, cpu),
1554 sid, did);
Robert Love42e9a922008-12-09 15:10:17 -08001555 }
Robert Love42e9a922008-12-09 15:10:17 -08001556}
1557EXPORT_SYMBOL(fc_exch_mgr_reset);
1558
1559/*
1560 * Handle incoming ELS REC - Read Exchange Concise.
1561 * Note that the requesting port may be different than the S_ID in the request.
1562 */
1563static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
1564{
1565 struct fc_frame *fp;
1566 struct fc_exch *ep;
1567 struct fc_exch_mgr *em;
1568 struct fc_els_rec *rp;
1569 struct fc_els_rec_acc *acc;
1570 enum fc_els_rjt_reason reason = ELS_RJT_LOGIC;
1571 enum fc_els_rjt_explan explan;
1572 u32 sid;
1573 u16 rxid;
1574 u16 oxid;
1575
1576 rp = fc_frame_payload_get(rfp, sizeof(*rp));
1577 explan = ELS_EXPL_INV_LEN;
1578 if (!rp)
1579 goto reject;
1580 sid = ntoh24(rp->rec_s_id);
1581 rxid = ntohs(rp->rec_rx_id);
1582 oxid = ntohs(rp->rec_ox_id);
1583
1584 /*
1585 * Currently it's hard to find the local S_ID from the exchange
1586 * manager. This will eventually be fixed, but for now it's easier
1587 * to lookup the subject exchange twice, once as if we were
1588 * the initiator, and then again if we weren't.
1589 */
1590 em = fc_seq_exch(sp)->em;
1591 ep = fc_exch_find(em, oxid);
1592 explan = ELS_EXPL_OXID_RXID;
1593 if (ep && ep->oid == sid) {
1594 if (ep->rxid != FC_XID_UNKNOWN &&
1595 rxid != FC_XID_UNKNOWN &&
1596 ep->rxid != rxid)
1597 goto rel;
1598 } else {
1599 if (ep)
1600 fc_exch_release(ep);
1601 ep = NULL;
1602 if (rxid != FC_XID_UNKNOWN)
1603 ep = fc_exch_find(em, rxid);
1604 if (!ep)
1605 goto reject;
1606 }
1607
1608 fp = fc_frame_alloc(fc_seq_exch(sp)->lp, sizeof(*acc));
1609 if (!fp) {
1610 fc_exch_done(sp);
1611 goto out;
1612 }
1613 sp = fc_seq_start_next(sp);
1614 acc = fc_frame_payload_get(fp, sizeof(*acc));
1615 memset(acc, 0, sizeof(*acc));
1616 acc->reca_cmd = ELS_LS_ACC;
1617 acc->reca_ox_id = rp->rec_ox_id;
1618 memcpy(acc->reca_ofid, rp->rec_s_id, 3);
1619 acc->reca_rx_id = htons(ep->rxid);
1620 if (ep->sid == ep->oid)
1621 hton24(acc->reca_rfid, ep->did);
1622 else
1623 hton24(acc->reca_rfid, ep->sid);
1624 acc->reca_fc4value = htonl(ep->seq.rec_data);
1625 acc->reca_e_stat = htonl(ep->esb_stat & (ESB_ST_RESP |
1626 ESB_ST_SEQ_INIT |
1627 ESB_ST_COMPLETE));
1628 sp = fc_seq_start_next(sp);
1629 fc_seq_send_last(sp, fp, FC_RCTL_ELS_REP, FC_TYPE_ELS);
1630out:
1631 fc_exch_release(ep);
1632 fc_frame_free(rfp);
1633 return;
1634
1635rel:
1636 fc_exch_release(ep);
1637reject:
1638 fc_seq_ls_rjt(sp, reason, explan);
1639 fc_frame_free(rfp);
1640}
1641
1642/*
1643 * Handle response from RRQ.
1644 * Not much to do here, really.
1645 * Should report errors.
1646 *
1647 * TODO: fix error handler.
1648 */
1649static void fc_exch_rrq_resp(struct fc_seq *sp, struct fc_frame *fp, void *arg)
1650{
1651 struct fc_exch *aborted_ep = arg;
1652 unsigned int op;
1653
1654 if (IS_ERR(fp)) {
1655 int err = PTR_ERR(fp);
1656
Vasu Dev78342da2009-02-27 10:54:46 -08001657 if (err == -FC_EX_CLOSED || err == -FC_EX_TIMEOUT)
Robert Love42e9a922008-12-09 15:10:17 -08001658 goto cleanup;
Robert Love74147052009-06-10 15:31:10 -07001659 FC_EXCH_DBG(aborted_ep, "Cannot process RRQ, "
1660 "frame error %d\n", err);
Robert Love42e9a922008-12-09 15:10:17 -08001661 return;
1662 }
1663
1664 op = fc_frame_payload_op(fp);
1665 fc_frame_free(fp);
1666
1667 switch (op) {
1668 case ELS_LS_RJT:
Robert Love74147052009-06-10 15:31:10 -07001669 FC_EXCH_DBG(aborted_ep, "LS_RJT for RRQ");
Robert Love42e9a922008-12-09 15:10:17 -08001670 /* fall through */
1671 case ELS_LS_ACC:
1672 goto cleanup;
1673 default:
Robert Love74147052009-06-10 15:31:10 -07001674 FC_EXCH_DBG(aborted_ep, "unexpected response op %x "
1675 "for RRQ", op);
Robert Love42e9a922008-12-09 15:10:17 -08001676 return;
1677 }
1678
1679cleanup:
1680 fc_exch_done(&aborted_ep->seq);
1681 /* drop hold for rec qual */
1682 fc_exch_release(aborted_ep);
1683}
1684
Robert Love1a7b75a2009-11-03 11:45:47 -08001685
1686/**
1687 * This function is for exch_seq_send function pointer in
1688 * struct libfc_function_template, see comment block on
1689 * exch_seq_send for description of this function.
1690 */
1691static struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
1692 struct fc_frame *fp,
1693 void (*resp)(struct fc_seq *,
1694 struct fc_frame *fp,
1695 void *arg),
1696 void (*destructor)(struct fc_seq *,
1697 void *),
1698 void *arg, u32 timer_msec)
1699{
1700 struct fc_exch *ep;
1701 struct fc_seq *sp = NULL;
1702 struct fc_frame_header *fh;
1703 int rc = 1;
1704
1705 ep = fc_exch_alloc(lp, fp);
1706 if (!ep) {
1707 fc_frame_free(fp);
1708 return NULL;
1709 }
1710 ep->esb_stat |= ESB_ST_SEQ_INIT;
1711 fh = fc_frame_header_get(fp);
1712 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1713 ep->resp = resp;
1714 ep->destructor = destructor;
1715 ep->arg = arg;
1716 ep->r_a_tov = FC_DEF_R_A_TOV;
1717 ep->lp = lp;
1718 sp = &ep->seq;
1719
1720 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1721 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1722 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1723 sp->cnt++;
1724
1725 if (ep->xid <= lp->lro_xid)
1726 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1727
1728 if (unlikely(lp->tt.frame_send(lp, fp)))
1729 goto err;
1730
1731 if (timer_msec)
1732 fc_exch_timer_set_locked(ep, timer_msec);
1733 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1734
1735 if (ep->f_ctl & FC_FC_SEQ_INIT)
1736 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1737 spin_unlock_bh(&ep->ex_lock);
1738 return sp;
1739err:
1740 rc = fc_exch_done_locked(ep);
1741 spin_unlock_bh(&ep->ex_lock);
1742 if (!rc)
1743 fc_exch_delete(ep);
1744 return NULL;
1745}
1746
Robert Love42e9a922008-12-09 15:10:17 -08001747/*
1748 * Send ELS RRQ - Reinstate Recovery Qualifier.
1749 * This tells the remote port to stop blocking the use of
1750 * the exchange and the seq_cnt range.
1751 */
1752static void fc_exch_rrq(struct fc_exch *ep)
1753{
1754 struct fc_lport *lp;
1755 struct fc_els_rrq *rrq;
1756 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001757 u32 did;
1758
1759 lp = ep->lp;
1760
1761 fp = fc_frame_alloc(lp, sizeof(*rrq));
1762 if (!fp)
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001763 goto retry;
1764
Robert Love42e9a922008-12-09 15:10:17 -08001765 rrq = fc_frame_payload_get(fp, sizeof(*rrq));
1766 memset(rrq, 0, sizeof(*rrq));
1767 rrq->rrq_cmd = ELS_RRQ;
1768 hton24(rrq->rrq_s_id, ep->sid);
1769 rrq->rrq_ox_id = htons(ep->oxid);
1770 rrq->rrq_rx_id = htons(ep->rxid);
1771
1772 did = ep->did;
1773 if (ep->esb_stat & ESB_ST_RESP)
1774 did = ep->sid;
1775
1776 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1777 fc_host_port_id(lp->host), FC_TYPE_ELS,
1778 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1779
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001780 if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov))
1781 return;
1782
1783retry:
1784 spin_lock_bh(&ep->ex_lock);
1785 if (ep->state & (FC_EX_RST_CLEANUP | FC_EX_DONE)) {
1786 spin_unlock_bh(&ep->ex_lock);
1787 /* drop hold for rec qual */
1788 fc_exch_release(ep);
Robert Love42e9a922008-12-09 15:10:17 -08001789 return;
1790 }
Vasu Deva0cc1ec2009-07-28 17:33:37 -07001791 ep->esb_stat |= ESB_ST_REC_QUAL;
1792 fc_exch_timer_set_locked(ep, ep->r_a_tov);
1793 spin_unlock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001794}
1795
1796
1797/*
1798 * Handle incoming ELS RRQ - Reset Recovery Qualifier.
1799 */
1800static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1801{
Vasu Dev3f127ad2009-10-21 16:27:33 -07001802 struct fc_exch *ep = NULL; /* request or subject exchange */
Robert Love42e9a922008-12-09 15:10:17 -08001803 struct fc_els_rrq *rp;
1804 u32 sid;
1805 u16 xid;
1806 enum fc_els_rjt_explan explan;
1807
1808 rp = fc_frame_payload_get(fp, sizeof(*rp));
1809 explan = ELS_EXPL_INV_LEN;
1810 if (!rp)
1811 goto reject;
1812
1813 /*
1814 * lookup subject exchange.
1815 */
1816 ep = fc_seq_exch(sp);
1817 sid = ntoh24(rp->rrq_s_id); /* subject source */
1818 xid = ep->did == sid ? ntohs(rp->rrq_ox_id) : ntohs(rp->rrq_rx_id);
1819 ep = fc_exch_find(ep->em, xid);
1820
1821 explan = ELS_EXPL_OXID_RXID;
1822 if (!ep)
1823 goto reject;
1824 spin_lock_bh(&ep->ex_lock);
1825 if (ep->oxid != ntohs(rp->rrq_ox_id))
1826 goto unlock_reject;
1827 if (ep->rxid != ntohs(rp->rrq_rx_id) &&
1828 ep->rxid != FC_XID_UNKNOWN)
1829 goto unlock_reject;
1830 explan = ELS_EXPL_SID;
1831 if (ep->sid != sid)
1832 goto unlock_reject;
1833
1834 /*
1835 * Clear Recovery Qualifier state, and cancel timer if complete.
1836 */
1837 if (ep->esb_stat & ESB_ST_REC_QUAL) {
1838 ep->esb_stat &= ~ESB_ST_REC_QUAL;
1839 atomic_dec(&ep->ex_refcnt); /* drop hold for rec qual */
1840 }
1841 if (ep->esb_stat & ESB_ST_COMPLETE) {
1842 if (cancel_delayed_work(&ep->timeout_work))
1843 atomic_dec(&ep->ex_refcnt); /* drop timer hold */
1844 }
1845
1846 spin_unlock_bh(&ep->ex_lock);
1847
1848 /*
1849 * Send LS_ACC.
1850 */
1851 fc_seq_ls_acc(sp);
Vasu Dev3f127ad2009-10-21 16:27:33 -07001852 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001853
1854unlock_reject:
1855 spin_unlock_bh(&ep->ex_lock);
Robert Love42e9a922008-12-09 15:10:17 -08001856reject:
1857 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
Vasu Dev3f127ad2009-10-21 16:27:33 -07001858out:
Robert Love42e9a922008-12-09 15:10:17 -08001859 fc_frame_free(fp);
Vasu Dev3f127ad2009-10-21 16:27:33 -07001860 if (ep)
1861 fc_exch_release(ep); /* drop hold from fc_exch_find */
Robert Love42e9a922008-12-09 15:10:17 -08001862}
1863
Vasu Dev96316092009-07-29 17:05:00 -07001864struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
1865 struct fc_exch_mgr *mp,
1866 bool (*match)(struct fc_frame *))
1867{
1868 struct fc_exch_mgr_anchor *ema;
1869
1870 ema = kmalloc(sizeof(*ema), GFP_ATOMIC);
1871 if (!ema)
1872 return ema;
1873
1874 ema->mp = mp;
1875 ema->match = match;
1876 /* add EM anchor to EM anchors list */
1877 list_add_tail(&ema->ema_list, &lport->ema_list);
1878 kref_get(&mp->kref);
1879 return ema;
1880}
1881EXPORT_SYMBOL(fc_exch_mgr_add);
1882
1883static void fc_exch_mgr_destroy(struct kref *kref)
1884{
1885 struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
1886
Vasu Dev96316092009-07-29 17:05:00 -07001887 mempool_destroy(mp->ep_pool);
Vasu Deve4bc50b2009-08-25 13:58:47 -07001888 free_percpu(mp->pool);
Vasu Dev96316092009-07-29 17:05:00 -07001889 kfree(mp);
1890}
1891
1892void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
1893{
1894 /* remove EM anchor from EM anchors list */
1895 list_del(&ema->ema_list);
1896 kref_put(&ema->mp->kref, fc_exch_mgr_destroy);
1897 kfree(ema);
1898}
1899EXPORT_SYMBOL(fc_exch_mgr_del);
1900
Robert Love42e9a922008-12-09 15:10:17 -08001901struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1902 enum fc_class class,
Vasu Dev52ff8782009-07-29 17:05:10 -07001903 u16 min_xid, u16 max_xid,
1904 bool (*match)(struct fc_frame *))
Robert Love42e9a922008-12-09 15:10:17 -08001905{
1906 struct fc_exch_mgr *mp;
Vasu Deve4bc50b2009-08-25 13:58:47 -07001907 u16 pool_exch_range;
1908 size_t pool_size;
1909 unsigned int cpu;
1910 struct fc_exch_pool *pool;
Robert Love42e9a922008-12-09 15:10:17 -08001911
Vasu Deve4bc50b2009-08-25 13:58:47 -07001912 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
1913 (min_xid & fc_cpu_mask) != 0) {
Robert Love74147052009-06-10 15:31:10 -07001914 FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
1915 min_xid, max_xid);
Robert Love42e9a922008-12-09 15:10:17 -08001916 return NULL;
1917 }
1918
1919 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001920 * allocate memory for EM
Robert Love42e9a922008-12-09 15:10:17 -08001921 */
Vasu Devb2f00912009-08-25 13:58:53 -07001922 mp = kzalloc(sizeof(struct fc_exch_mgr), GFP_ATOMIC);
Robert Love42e9a922008-12-09 15:10:17 -08001923 if (!mp)
1924 return NULL;
1925
1926 mp->class = class;
Robert Love42e9a922008-12-09 15:10:17 -08001927 /* adjust em exch xid range for offload */
1928 mp->min_xid = min_xid;
1929 mp->max_xid = max_xid;
Robert Love42e9a922008-12-09 15:10:17 -08001930
1931 mp->ep_pool = mempool_create_slab_pool(2, fc_em_cachep);
1932 if (!mp->ep_pool)
1933 goto free_mp;
1934
Vasu Deve4bc50b2009-08-25 13:58:47 -07001935 /*
1936 * Setup per cpu exch pool with entire exchange id range equally
1937 * divided across all cpus. The exch pointers array memory is
1938 * allocated for exch range per pool.
1939 */
1940 pool_exch_range = (mp->max_xid - mp->min_xid + 1) / (fc_cpu_mask + 1);
1941 mp->pool_max_index = pool_exch_range - 1;
1942
1943 /*
1944 * Allocate and initialize per cpu exch pool
1945 */
1946 pool_size = sizeof(*pool) + pool_exch_range * sizeof(struct fc_exch *);
1947 mp->pool = __alloc_percpu(pool_size, __alignof__(struct fc_exch_pool));
1948 if (!mp->pool)
1949 goto free_mempool;
1950 for_each_possible_cpu(cpu) {
1951 pool = per_cpu_ptr(mp->pool, cpu);
1952 spin_lock_init(&pool->lock);
1953 INIT_LIST_HEAD(&pool->ex_list);
1954 }
1955
Vasu Dev52ff8782009-07-29 17:05:10 -07001956 kref_init(&mp->kref);
1957 if (!fc_exch_mgr_add(lp, mp, match)) {
Vasu Deve4bc50b2009-08-25 13:58:47 -07001958 free_percpu(mp->pool);
1959 goto free_mempool;
Vasu Dev52ff8782009-07-29 17:05:10 -07001960 }
1961
1962 /*
1963 * Above kref_init() sets mp->kref to 1 and then
1964 * call to fc_exch_mgr_add incremented mp->kref again,
1965 * so adjust that extra increment.
1966 */
1967 kref_put(&mp->kref, fc_exch_mgr_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001968 return mp;
1969
Vasu Deve4bc50b2009-08-25 13:58:47 -07001970free_mempool:
1971 mempool_destroy(mp->ep_pool);
Robert Love42e9a922008-12-09 15:10:17 -08001972free_mp:
1973 kfree(mp);
1974 return NULL;
1975}
1976EXPORT_SYMBOL(fc_exch_mgr_alloc);
1977
Vasu Dev52ff8782009-07-29 17:05:10 -07001978void fc_exch_mgr_free(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -08001979{
Vasu Dev52ff8782009-07-29 17:05:10 -07001980 struct fc_exch_mgr_anchor *ema, *next;
1981
1982 list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
1983 fc_exch_mgr_del(ema);
Robert Love42e9a922008-12-09 15:10:17 -08001984}
1985EXPORT_SYMBOL(fc_exch_mgr_free);
1986
Robert Love42e9a922008-12-09 15:10:17 -08001987/*
1988 * Receive a frame
1989 */
Vasu Dev52ff8782009-07-29 17:05:10 -07001990void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001991{
1992 struct fc_frame_header *fh = fc_frame_header_get(fp);
Vasu Dev52ff8782009-07-29 17:05:10 -07001993 struct fc_exch_mgr_anchor *ema;
1994 u32 f_ctl, found = 0;
1995 u16 oxid;
Robert Love42e9a922008-12-09 15:10:17 -08001996
1997 /* lport lock ? */
Vasu Dev52ff8782009-07-29 17:05:10 -07001998 if (!lp || lp->state == LPORT_ST_DISABLED) {
Robert Love74147052009-06-10 15:31:10 -07001999 FC_LPORT_DBG(lp, "Receiving frames for an lport that "
2000 "has not been initialized correctly\n");
Robert Love42e9a922008-12-09 15:10:17 -08002001 fc_frame_free(fp);
2002 return;
2003 }
2004
Vasu Dev52ff8782009-07-29 17:05:10 -07002005 f_ctl = ntoh24(fh->fh_f_ctl);
2006 oxid = ntohs(fh->fh_ox_id);
2007 if (f_ctl & FC_FC_EX_CTX) {
2008 list_for_each_entry(ema, &lp->ema_list, ema_list) {
2009 if ((oxid >= ema->mp->min_xid) &&
2010 (oxid <= ema->mp->max_xid)) {
2011 found = 1;
2012 break;
2013 }
2014 }
2015
2016 if (!found) {
2017 FC_LPORT_DBG(lp, "Received response for out "
2018 "of range oxid:%hx\n", oxid);
2019 fc_frame_free(fp);
2020 return;
2021 }
2022 } else
2023 ema = list_entry(lp->ema_list.prev, typeof(*ema), ema_list);
2024
Robert Love42e9a922008-12-09 15:10:17 -08002025 /*
2026 * If frame is marked invalid, just drop it.
2027 */
Robert Love42e9a922008-12-09 15:10:17 -08002028 switch (fr_eof(fp)) {
2029 case FC_EOF_T:
2030 if (f_ctl & FC_FC_END_SEQ)
2031 skb_trim(fp_skb(fp), fr_len(fp) - FC_FC_FILL(f_ctl));
2032 /* fall through */
2033 case FC_EOF_N:
2034 if (fh->fh_type == FC_TYPE_BLS)
Vasu Dev52ff8782009-07-29 17:05:10 -07002035 fc_exch_recv_bls(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08002036 else if ((f_ctl & (FC_FC_EX_CTX | FC_FC_SEQ_CTX)) ==
2037 FC_FC_EX_CTX)
Vasu Dev52ff8782009-07-29 17:05:10 -07002038 fc_exch_recv_seq_resp(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08002039 else if (f_ctl & FC_FC_SEQ_CTX)
Vasu Dev52ff8782009-07-29 17:05:10 -07002040 fc_exch_recv_resp(ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08002041 else
Vasu Dev52ff8782009-07-29 17:05:10 -07002042 fc_exch_recv_req(lp, ema->mp, fp);
Robert Love42e9a922008-12-09 15:10:17 -08002043 break;
2044 default:
Robert Loved459b7e2009-07-29 17:05:05 -07002045 FC_LPORT_DBG(lp, "dropping invalid frame (eof %x)", fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -08002046 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08002047 }
2048}
2049EXPORT_SYMBOL(fc_exch_recv);
2050
2051int fc_exch_init(struct fc_lport *lp)
2052{
Robert Love42e9a922008-12-09 15:10:17 -08002053 if (!lp->tt.seq_start_next)
2054 lp->tt.seq_start_next = fc_seq_start_next;
2055
2056 if (!lp->tt.exch_seq_send)
2057 lp->tt.exch_seq_send = fc_exch_seq_send;
2058
2059 if (!lp->tt.seq_send)
2060 lp->tt.seq_send = fc_seq_send;
2061
2062 if (!lp->tt.seq_els_rsp_send)
2063 lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
2064
2065 if (!lp->tt.exch_done)
2066 lp->tt.exch_done = fc_exch_done;
2067
2068 if (!lp->tt.exch_mgr_reset)
2069 lp->tt.exch_mgr_reset = fc_exch_mgr_reset;
2070
2071 if (!lp->tt.seq_exch_abort)
2072 lp->tt.seq_exch_abort = fc_seq_exch_abort;
2073
Vasu Dev89f19a52009-10-21 16:27:28 -07002074 return 0;
2075}
2076EXPORT_SYMBOL(fc_exch_init);
2077
2078/**
2079 * fc_setup_exch_mgr() - Setup an exchange manager
2080 */
2081int fc_setup_exch_mgr()
2082{
2083 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
2084 0, SLAB_HWCACHE_ALIGN, NULL);
2085 if (!fc_em_cachep)
2086 return -ENOMEM;
2087
Vasu Deve4bc50b2009-08-25 13:58:47 -07002088 /*
2089 * Initialize fc_cpu_mask and fc_cpu_order. The
2090 * fc_cpu_mask is set for nr_cpu_ids rounded up
2091 * to order of 2's * power and order is stored
2092 * in fc_cpu_order as this is later required in
2093 * mapping between an exch id and exch array index
2094 * in per cpu exch pool.
2095 *
2096 * This round up is required to align fc_cpu_mask
2097 * to exchange id's lower bits such that all incoming
2098 * frames of an exchange gets delivered to the same
2099 * cpu on which exchange originated by simple bitwise
2100 * AND operation between fc_cpu_mask and exchange id.
2101 */
2102 fc_cpu_mask = 1;
2103 fc_cpu_order = 0;
2104 while (fc_cpu_mask < nr_cpu_ids) {
2105 fc_cpu_mask <<= 1;
2106 fc_cpu_order++;
2107 }
2108 fc_cpu_mask--;
2109
Robert Love42e9a922008-12-09 15:10:17 -08002110 return 0;
2111}
Robert Love42e9a922008-12-09 15:10:17 -08002112
2113void fc_destroy_exch_mgr(void)
2114{
2115 kmem_cache_destroy(fc_em_cachep);
2116}