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