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