Commit | Line | Data |
---|---|---|
9b34ecff | 1 | /* |
97c8389d JE |
2 | * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved. |
3 | * Copyright (c) 2009 Intel Corporation. All rights reserved. | |
9b34ecff VD |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms and conditions of the GNU General Public License, | |
7 | * version 2, as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | * | |
18 | * Maintained at www.Open-FCoE.org | |
19 | */ | |
20 | ||
97c8389d | 21 | #include <linux/types.h> |
9b34ecff | 22 | #include <linux/module.h> |
97c8389d JE |
23 | #include <linux/kernel.h> |
24 | #include <linux/list.h> | |
25 | #include <linux/spinlock.h> | |
26 | #include <linux/timer.h> | |
5e80f7f7 | 27 | #include <linux/netdevice.h> |
97c8389d JE |
28 | #include <linux/etherdevice.h> |
29 | #include <linux/ethtool.h> | |
30 | #include <linux/if_ether.h> | |
31 | #include <linux/if_vlan.h> | |
97c8389d JE |
32 | #include <linux/errno.h> |
33 | #include <linux/bitops.h> | |
5a0e3ad6 | 34 | #include <linux/slab.h> |
97c8389d JE |
35 | #include <net/rtnetlink.h> |
36 | ||
37 | #include <scsi/fc/fc_els.h> | |
38 | #include <scsi/fc/fc_fs.h> | |
39 | #include <scsi/fc/fc_fip.h> | |
40 | #include <scsi/fc/fc_encaps.h> | |
41 | #include <scsi/fc/fc_fcoe.h> | |
e10f8c66 | 42 | #include <scsi/fc/fc_fcp.h> |
5e80f7f7 VD |
43 | |
44 | #include <scsi/libfc.h> | |
97c8389d | 45 | #include <scsi/libfcoe.h> |
9b34ecff | 46 | |
21b7b2f5 YZ |
47 | #include "libfcoe.h" |
48 | ||
97c8389d JE |
49 | #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */ |
50 | #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */ | |
51 | ||
52 | static void fcoe_ctlr_timeout(unsigned long); | |
42913657 | 53 | static void fcoe_ctlr_timer_work(struct work_struct *); |
97c8389d | 54 | static void fcoe_ctlr_recv_work(struct work_struct *); |
794d98e7 | 55 | static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *); |
97c8389d | 56 | |
e10f8c66 JE |
57 | static void fcoe_ctlr_vn_start(struct fcoe_ctlr *); |
58 | static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *); | |
59 | static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *); | |
60 | static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *); | |
61 | ||
97c8389d | 62 | static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS; |
e10f8c66 JE |
63 | static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS; |
64 | static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS; | |
65 | static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS; | |
97c8389d | 66 | |
0095a921 | 67 | static const char * const fcoe_ctlr_states[] = { |
9b651da9 JE |
68 | [FIP_ST_DISABLED] = "DISABLED", |
69 | [FIP_ST_LINK_WAIT] = "LINK_WAIT", | |
70 | [FIP_ST_AUTO] = "AUTO", | |
71 | [FIP_ST_NON_FIP] = "NON_FIP", | |
72 | [FIP_ST_ENABLED] = "ENABLED", | |
e10f8c66 JE |
73 | [FIP_ST_VNMP_START] = "VNMP_START", |
74 | [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1", | |
75 | [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2", | |
76 | [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM", | |
77 | [FIP_ST_VNMP_UP] = "VNMP_UP", | |
9b651da9 JE |
78 | }; |
79 | ||
80 | static const char *fcoe_ctlr_state(enum fip_state state) | |
81 | { | |
82 | const char *cp = "unknown"; | |
83 | ||
84 | if (state < ARRAY_SIZE(fcoe_ctlr_states)) | |
85 | cp = fcoe_ctlr_states[state]; | |
86 | if (!cp) | |
87 | cp = "unknown"; | |
88 | return cp; | |
89 | } | |
90 | ||
91 | /** | |
92 | * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state. | |
93 | * @fip: The FCoE controller | |
94 | * @state: The new state | |
95 | */ | |
96 | static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state) | |
97 | { | |
98 | if (state == fip->state) | |
99 | return; | |
100 | if (fip->lp) | |
101 | LIBFCOE_FIP_DBG(fip, "state %s -> %s\n", | |
102 | fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state)); | |
103 | fip->state = state; | |
104 | } | |
105 | ||
70b51aab RL |
106 | /** |
107 | * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid | |
108 | * @fcf: The FCF to check | |
109 | * | |
97c8389d JE |
110 | * Return non-zero if FCF fcoe_size has been validated. |
111 | */ | |
112 | static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf) | |
113 | { | |
114 | return (fcf->flags & FIP_FL_SOL) != 0; | |
115 | } | |
116 | ||
70b51aab RL |
117 | /** |
118 | * fcoe_ctlr_fcf_usable() - Check if a FCF is usable | |
119 | * @fcf: The FCF to check | |
120 | * | |
97c8389d JE |
121 | * Return non-zero if the FCF is usable. |
122 | */ | |
123 | static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf) | |
124 | { | |
125 | u16 flags = FIP_FL_SOL | FIP_FL_AVAIL; | |
126 | ||
127 | return (fcf->flags & flags) == flags; | |
128 | } | |
129 | ||
cd229e42 JE |
130 | /** |
131 | * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses | |
132 | * @fip: The FCoE controller | |
133 | */ | |
134 | static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip) | |
135 | { | |
136 | if (fip->mode == FIP_MODE_VN2VN) | |
137 | hton24(fip->dest_addr, FIP_VN_FC_MAP); | |
138 | else | |
139 | hton24(fip->dest_addr, FIP_DEF_FC_MAP); | |
140 | hton24(fip->dest_addr + 3, 0); | |
141 | fip->map_dest = 1; | |
142 | } | |
143 | ||
97c8389d | 144 | /** |
70b51aab RL |
145 | * fcoe_ctlr_init() - Initialize the FCoE Controller instance |
146 | * @fip: The FCoE controller to initialize | |
97c8389d | 147 | */ |
3d902ac0 | 148 | void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode) |
97c8389d | 149 | { |
9b651da9 | 150 | fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT); |
3d902ac0 | 151 | fip->mode = mode; |
97c8389d | 152 | INIT_LIST_HEAD(&fip->fcfs); |
fdb068c6 | 153 | mutex_init(&fip->ctlr_mutex); |
794d98e7 | 154 | spin_lock_init(&fip->ctlr_lock); |
97c8389d JE |
155 | fip->flogi_oxid = FC_XID_UNKNOWN; |
156 | setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip); | |
42913657 | 157 | INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work); |
97c8389d JE |
158 | INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work); |
159 | skb_queue_head_init(&fip->fip_recv_list); | |
160 | } | |
161 | EXPORT_SYMBOL(fcoe_ctlr_init); | |
162 | ||
8d55e507 RL |
163 | static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new) |
164 | { | |
165 | struct fcoe_ctlr *fip = new->fip; | |
166 | struct fcoe_ctlr_device *ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip); | |
167 | struct fcoe_fcf_device temp, *fcf_dev; | |
168 | int rc = 0; | |
169 | ||
170 | LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n", | |
171 | new->fabric_name, new->fcf_mac); | |
172 | ||
173 | mutex_lock(&ctlr_dev->lock); | |
174 | ||
175 | temp.fabric_name = new->fabric_name; | |
176 | temp.switch_name = new->switch_name; | |
177 | temp.fc_map = new->fc_map; | |
178 | temp.vfid = new->vfid; | |
179 | memcpy(temp.mac, new->fcf_mac, ETH_ALEN); | |
180 | temp.priority = new->pri; | |
181 | temp.fka_period = new->fka_period; | |
182 | temp.selected = 0; /* default to unselected */ | |
183 | ||
184 | fcf_dev = fcoe_fcf_device_add(ctlr_dev, &temp); | |
185 | if (unlikely(!fcf_dev)) { | |
186 | rc = -ENOMEM; | |
187 | goto out; | |
188 | } | |
189 | ||
190 | /* | |
191 | * The fcoe_sysfs layer can return a CONNECTED fcf that | |
192 | * has a priv (fcf was never deleted) or a CONNECTED fcf | |
193 | * that doesn't have a priv (fcf was deleted). However, | |
194 | * libfcoe will always delete FCFs before trying to add | |
195 | * them. This is ensured because both recv_adv and | |
196 | * age_fcfs are protected by the the fcoe_ctlr's mutex. | |
197 | * This means that we should never get a FCF with a | |
198 | * non-NULL priv pointer. | |
199 | */ | |
200 | BUG_ON(fcf_dev->priv); | |
201 | ||
202 | fcf_dev->priv = new; | |
203 | new->fcf_dev = fcf_dev; | |
204 | ||
205 | list_add(&new->list, &fip->fcfs); | |
206 | fip->fcf_count++; | |
207 | ||
208 | out: | |
209 | mutex_unlock(&ctlr_dev->lock); | |
210 | return rc; | |
211 | } | |
212 | ||
213 | static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new) | |
214 | { | |
215 | struct fcoe_ctlr *fip = new->fip; | |
216 | struct fcoe_ctlr_device *ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip); | |
217 | struct fcoe_fcf_device *fcf_dev; | |
218 | ||
219 | list_del(&new->list); | |
220 | fip->fcf_count--; | |
221 | ||
222 | mutex_lock(&ctlr_dev->lock); | |
223 | ||
224 | fcf_dev = fcoe_fcf_to_fcf_dev(new); | |
225 | WARN_ON(!fcf_dev); | |
226 | new->fcf_dev = NULL; | |
227 | fcoe_fcf_device_delete(fcf_dev); | |
228 | kfree(new); | |
229 | ||
230 | mutex_unlock(&ctlr_dev->lock); | |
231 | } | |
232 | ||
97c8389d | 233 | /** |
70b51aab RL |
234 | * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller |
235 | * @fip: The FCoE controller whose FCFs are to be reset | |
97c8389d JE |
236 | * |
237 | * Called with &fcoe_ctlr lock held. | |
238 | */ | |
239 | static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip) | |
240 | { | |
241 | struct fcoe_fcf *fcf; | |
242 | struct fcoe_fcf *next; | |
243 | ||
244 | fip->sel_fcf = NULL; | |
245 | list_for_each_entry_safe(fcf, next, &fip->fcfs, list) { | |
8d55e507 | 246 | fcoe_sysfs_fcf_del(fcf); |
97c8389d | 247 | } |
8d55e507 RL |
248 | WARN_ON(fip->fcf_count); |
249 | ||
97c8389d JE |
250 | fip->sel_time = 0; |
251 | } | |
252 | ||
253 | /** | |
70b51aab RL |
254 | * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller |
255 | * @fip: The FCoE controller to tear down | |
97c8389d JE |
256 | * |
257 | * This is called by FCoE drivers before freeing the &fcoe_ctlr. | |
258 | * | |
259 | * The receive handler will have been deleted before this to guarantee | |
260 | * that no more recv_work will be scheduled. | |
261 | * | |
262 | * The timer routine will simply return once we set FIP_ST_DISABLED. | |
263 | * This guarantees that no further timeouts or work will be scheduled. | |
264 | */ | |
265 | void fcoe_ctlr_destroy(struct fcoe_ctlr *fip) | |
266 | { | |
a4b7cfae | 267 | cancel_work_sync(&fip->recv_work); |
1f4aed81 | 268 | skb_queue_purge(&fip->fip_recv_list); |
a4b7cfae | 269 | |
fdb068c6 | 270 | mutex_lock(&fip->ctlr_mutex); |
9b651da9 | 271 | fcoe_ctlr_set_state(fip, FIP_ST_DISABLED); |
97c8389d | 272 | fcoe_ctlr_reset_fcfs(fip); |
fdb068c6 | 273 | mutex_unlock(&fip->ctlr_mutex); |
97c8389d | 274 | del_timer_sync(&fip->timer); |
42913657 | 275 | cancel_work_sync(&fip->timer_work); |
97c8389d JE |
276 | } |
277 | EXPORT_SYMBOL(fcoe_ctlr_destroy); | |
278 | ||
69316ee2 | 279 | /** |
794d98e7 | 280 | * fcoe_ctlr_announce() - announce new FCF selection |
69316ee2 JE |
281 | * @fip: The FCoE controller |
282 | * | |
283 | * Also sets the destination MAC for FCoE and control packets | |
794d98e7 JE |
284 | * |
285 | * Called with neither ctlr_mutex nor ctlr_lock held. | |
69316ee2 JE |
286 | */ |
287 | static void fcoe_ctlr_announce(struct fcoe_ctlr *fip) | |
288 | { | |
794d98e7 JE |
289 | struct fcoe_fcf *sel; |
290 | struct fcoe_fcf *fcf; | |
291 | ||
292 | mutex_lock(&fip->ctlr_mutex); | |
293 | spin_lock_bh(&fip->ctlr_lock); | |
294 | ||
295 | kfree_skb(fip->flogi_req); | |
296 | fip->flogi_req = NULL; | |
297 | list_for_each_entry(fcf, &fip->fcfs, list) | |
298 | fcf->flogi_sent = 0; | |
299 | ||
300 | spin_unlock_bh(&fip->ctlr_lock); | |
301 | sel = fip->sel_fcf; | |
69316ee2 JE |
302 | |
303 | if (sel && !compare_ether_addr(sel->fcf_mac, fip->dest_addr)) | |
794d98e7 | 304 | goto unlock; |
69316ee2 JE |
305 | if (!is_zero_ether_addr(fip->dest_addr)) { |
306 | printk(KERN_NOTICE "libfcoe: host%d: " | |
307 | "FIP Fibre-Channel Forwarder MAC %pM deselected\n", | |
308 | fip->lp->host->host_no, fip->dest_addr); | |
309 | memset(fip->dest_addr, 0, ETH_ALEN); | |
310 | } | |
311 | if (sel) { | |
312 | printk(KERN_INFO "libfcoe: host%d: FIP selected " | |
313 | "Fibre-Channel Forwarder MAC %pM\n", | |
314 | fip->lp->host->host_no, sel->fcf_mac); | |
81c11dd2 | 315 | memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN); |
69316ee2 JE |
316 | fip->map_dest = 0; |
317 | } | |
794d98e7 JE |
318 | unlock: |
319 | mutex_unlock(&fip->ctlr_mutex); | |
69316ee2 JE |
320 | } |
321 | ||
97c8389d | 322 | /** |
70b51aab RL |
323 | * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port |
324 | * @fip: The FCoE controller to get the maximum FCoE size from | |
97c8389d JE |
325 | * |
326 | * Returns the maximum packet size including the FCoE header and trailer, | |
327 | * but not including any Ethernet or VLAN headers. | |
328 | */ | |
329 | static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip) | |
330 | { | |
331 | /* | |
332 | * Determine the max FCoE frame size allowed, including | |
333 | * FCoE header and trailer. | |
334 | * Note: lp->mfs is currently the payload size, not the frame size. | |
335 | */ | |
336 | return fip->lp->mfs + sizeof(struct fc_frame_header) + | |
337 | sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof); | |
338 | } | |
339 | ||
340 | /** | |
70b51aab RL |
341 | * fcoe_ctlr_solicit() - Send a FIP solicitation |
342 | * @fip: The FCoE controller to send the solicitation on | |
343 | * @fcf: The destination FCF (if NULL, a multicast solicitation is sent) | |
97c8389d JE |
344 | */ |
345 | static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf) | |
346 | { | |
347 | struct sk_buff *skb; | |
348 | struct fip_sol { | |
349 | struct ethhdr eth; | |
350 | struct fip_header fip; | |
351 | struct { | |
352 | struct fip_mac_desc mac; | |
353 | struct fip_wwn_desc wwnn; | |
354 | struct fip_size_desc size; | |
0095a921 YZ |
355 | } __packed desc; |
356 | } __packed * sol; | |
97c8389d JE |
357 | u32 fcoe_size; |
358 | ||
359 | skb = dev_alloc_skb(sizeof(*sol)); | |
360 | if (!skb) | |
361 | return; | |
362 | ||
363 | sol = (struct fip_sol *)skb->data; | |
364 | ||
365 | memset(sol, 0, sizeof(*sol)); | |
366 | memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN); | |
367 | memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN); | |
368 | sol->eth.h_proto = htons(ETH_P_FIP); | |
369 | ||
370 | sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER); | |
371 | sol->fip.fip_op = htons(FIP_OP_DISC); | |
372 | sol->fip.fip_subcode = FIP_SC_SOL; | |
373 | sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW); | |
374 | sol->fip.fip_flags = htons(FIP_FL_FPMA); | |
184dd345 VD |
375 | if (fip->spma) |
376 | sol->fip.fip_flags |= htons(FIP_FL_SPMA); | |
97c8389d JE |
377 | |
378 | sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC; | |
379 | sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW; | |
380 | memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN); | |
381 | ||
382 | sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME; | |
383 | sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW; | |
384 | put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn); | |
385 | ||
386 | fcoe_size = fcoe_ctlr_fcoe_size(fip); | |
387 | sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE; | |
388 | sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW; | |
389 | sol->desc.size.fd_size = htons(fcoe_size); | |
390 | ||
391 | skb_put(skb, sizeof(*sol)); | |
0f491539 | 392 | skb->protocol = htons(ETH_P_FIP); |
6f6c2aa3 | 393 | skb->priority = fip->priority; |
97c8389d JE |
394 | skb_reset_mac_header(skb); |
395 | skb_reset_network_header(skb); | |
396 | fip->send(fip, skb); | |
397 | ||
398 | if (!fcf) | |
399 | fip->sol_time = jiffies; | |
400 | } | |
401 | ||
402 | /** | |
70b51aab RL |
403 | * fcoe_ctlr_link_up() - Start FCoE controller |
404 | * @fip: The FCoE controller to start | |
97c8389d JE |
405 | * |
406 | * Called from the LLD when the network link is ready. | |
407 | */ | |
408 | void fcoe_ctlr_link_up(struct fcoe_ctlr *fip) | |
409 | { | |
fdb068c6 | 410 | mutex_lock(&fip->ctlr_mutex); |
97c8389d | 411 | if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) { |
fdb068c6 | 412 | mutex_unlock(&fip->ctlr_mutex); |
97c8389d JE |
413 | fc_linkup(fip->lp); |
414 | } else if (fip->state == FIP_ST_LINK_WAIT) { | |
9b651da9 | 415 | fcoe_ctlr_set_state(fip, fip->mode); |
e10f8c66 JE |
416 | switch (fip->mode) { |
417 | default: | |
418 | LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode); | |
419 | /* fall-through */ | |
420 | case FIP_MODE_AUTO: | |
0f51c2e5 | 421 | LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n"); |
e10f8c66 JE |
422 | /* fall-through */ |
423 | case FIP_MODE_FABRIC: | |
424 | case FIP_MODE_NON_FIP: | |
425 | mutex_unlock(&fip->ctlr_mutex); | |
426 | fc_linkup(fip->lp); | |
427 | fcoe_ctlr_solicit(fip, NULL); | |
428 | break; | |
429 | case FIP_MODE_VN2VN: | |
430 | fcoe_ctlr_vn_start(fip); | |
431 | mutex_unlock(&fip->ctlr_mutex); | |
432 | fc_linkup(fip->lp); | |
433 | break; | |
434 | } | |
97c8389d | 435 | } else |
fdb068c6 | 436 | mutex_unlock(&fip->ctlr_mutex); |
97c8389d JE |
437 | } |
438 | EXPORT_SYMBOL(fcoe_ctlr_link_up); | |
439 | ||
440 | /** | |
70b51aab RL |
441 | * fcoe_ctlr_reset() - Reset a FCoE controller |
442 | * @fip: The FCoE controller to reset | |
97c8389d | 443 | */ |
dd42dac4 | 444 | static void fcoe_ctlr_reset(struct fcoe_ctlr *fip) |
97c8389d | 445 | { |
97c8389d JE |
446 | fcoe_ctlr_reset_fcfs(fip); |
447 | del_timer(&fip->timer); | |
97c8389d JE |
448 | fip->ctlr_ka_time = 0; |
449 | fip->port_ka_time = 0; | |
450 | fip->sol_time = 0; | |
451 | fip->flogi_oxid = FC_XID_UNKNOWN; | |
cd229e42 | 452 | fcoe_ctlr_map_dest(fip); |
97c8389d JE |
453 | } |
454 | ||
455 | /** | |
70b51aab RL |
456 | * fcoe_ctlr_link_down() - Stop a FCoE controller |
457 | * @fip: The FCoE controller to be stopped | |
97c8389d JE |
458 | * |
459 | * Returns non-zero if the link was up and now isn't. | |
460 | * | |
461 | * Called from the LLD when the network link is not ready. | |
462 | * There may be multiple calls while the link is down. | |
463 | */ | |
464 | int fcoe_ctlr_link_down(struct fcoe_ctlr *fip) | |
465 | { | |
dd42dac4 JE |
466 | int link_dropped; |
467 | ||
468 | LIBFCOE_FIP_DBG(fip, "link down.\n"); | |
fdb068c6 | 469 | mutex_lock(&fip->ctlr_mutex); |
dd42dac4 | 470 | fcoe_ctlr_reset(fip); |
42913657 | 471 | link_dropped = fip->state != FIP_ST_LINK_WAIT; |
9b651da9 | 472 | fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT); |
fdb068c6 | 473 | mutex_unlock(&fip->ctlr_mutex); |
dd42dac4 JE |
474 | |
475 | if (link_dropped) | |
476 | fc_linkdown(fip->lp); | |
477 | return link_dropped; | |
97c8389d JE |
478 | } |
479 | EXPORT_SYMBOL(fcoe_ctlr_link_down); | |
480 | ||
481 | /** | |
70b51aab RL |
482 | * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF |
483 | * @fip: The FCoE controller to send the FKA on | |
484 | * @lport: libfc fc_lport to send from | |
485 | * @ports: 0 for controller keep-alive, 1 for port keep-alive | |
486 | * @sa: The source MAC address | |
97c8389d JE |
487 | * |
488 | * A controller keep-alive is sent every fka_period (typically 8 seconds). | |
489 | * The source MAC is the native MAC address. | |
490 | * | |
491 | * A port keep-alive is sent every 90 seconds while logged in. | |
492 | * The source MAC is the assigned mapped source address. | |
493 | * The destination is the FCF's F-port. | |
494 | */ | |
11b56188 CL |
495 | static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip, |
496 | struct fc_lport *lport, | |
497 | int ports, u8 *sa) | |
97c8389d JE |
498 | { |
499 | struct sk_buff *skb; | |
500 | struct fip_kal { | |
501 | struct ethhdr eth; | |
502 | struct fip_header fip; | |
503 | struct fip_mac_desc mac; | |
0095a921 | 504 | } __packed * kal; |
97c8389d JE |
505 | struct fip_vn_desc *vn; |
506 | u32 len; | |
507 | struct fc_lport *lp; | |
508 | struct fcoe_fcf *fcf; | |
509 | ||
510 | fcf = fip->sel_fcf; | |
511 | lp = fip->lp; | |
281ae642 | 512 | if (!fcf || (ports && !lp->port_id)) |
97c8389d JE |
513 | return; |
514 | ||
be276cbe | 515 | len = sizeof(*kal) + ports * sizeof(*vn); |
97c8389d JE |
516 | skb = dev_alloc_skb(len); |
517 | if (!skb) | |
518 | return; | |
519 | ||
520 | kal = (struct fip_kal *)skb->data; | |
521 | memset(kal, 0, len); | |
522 | memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN); | |
523 | memcpy(kal->eth.h_source, sa, ETH_ALEN); | |
524 | kal->eth.h_proto = htons(ETH_P_FIP); | |
525 | ||
526 | kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER); | |
527 | kal->fip.fip_op = htons(FIP_OP_CTRL); | |
528 | kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE; | |
529 | kal->fip.fip_dl_len = htons((sizeof(kal->mac) + | |
70b51aab | 530 | ports * sizeof(*vn)) / FIP_BPW); |
97c8389d | 531 | kal->fip.fip_flags = htons(FIP_FL_FPMA); |
184dd345 VD |
532 | if (fip->spma) |
533 | kal->fip.fip_flags |= htons(FIP_FL_SPMA); | |
97c8389d JE |
534 | |
535 | kal->mac.fd_desc.fip_dtype = FIP_DT_MAC; | |
536 | kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW; | |
537 | memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN); | |
97c8389d JE |
538 | if (ports) { |
539 | vn = (struct fip_vn_desc *)(kal + 1); | |
540 | vn->fd_desc.fip_dtype = FIP_DT_VN_ID; | |
541 | vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW; | |
11b56188 | 542 | memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN); |
fb83153d KM |
543 | hton24(vn->fd_fc_id, lport->port_id); |
544 | put_unaligned_be64(lport->wwpn, &vn->fd_wwpn); | |
97c8389d | 545 | } |
97c8389d | 546 | skb_put(skb, len); |
0f491539 | 547 | skb->protocol = htons(ETH_P_FIP); |
6f6c2aa3 | 548 | skb->priority = fip->priority; |
97c8389d JE |
549 | skb_reset_mac_header(skb); |
550 | skb_reset_network_header(skb); | |
551 | fip->send(fip, skb); | |
552 | } | |
553 | ||
554 | /** | |
70b51aab RL |
555 | * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it |
556 | * @fip: The FCoE controller for the ELS frame | |
557 | * @dtype: The FIP descriptor type for the frame | |
558 | * @skb: The FCoE ELS frame including FC header but no FCoE headers | |
e10f8c66 | 559 | * @d_id: The destination port ID. |
97c8389d JE |
560 | * |
561 | * Returns non-zero error code on failure. | |
562 | * | |
563 | * The caller must check that the length is a multiple of 4. | |
564 | * | |
565 | * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes). | |
566 | * Headroom includes the FIP encapsulation description, FIP header, and | |
567 | * Ethernet header. The tailroom is for the FIP MAC descriptor. | |
568 | */ | |
11b56188 | 569 | static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport, |
e10f8c66 | 570 | u8 dtype, struct sk_buff *skb, u32 d_id) |
97c8389d JE |
571 | { |
572 | struct fip_encaps_head { | |
573 | struct ethhdr eth; | |
574 | struct fip_header fip; | |
575 | struct fip_encaps encaps; | |
0095a921 | 576 | } __packed * cap; |
5554345b | 577 | struct fc_frame_header *fh; |
97c8389d JE |
578 | struct fip_mac_desc *mac; |
579 | struct fcoe_fcf *fcf; | |
580 | size_t dlen; | |
5a84baea | 581 | u16 fip_flags; |
5554345b | 582 | u8 op; |
97c8389d | 583 | |
5554345b JE |
584 | fh = (struct fc_frame_header *)skb->data; |
585 | op = *(u8 *)(fh + 1); | |
97c8389d JE |
586 | dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */ |
587 | cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap)); | |
97c8389d | 588 | memset(cap, 0, sizeof(*cap)); |
e10f8c66 JE |
589 | |
590 | if (lport->point_to_multipoint) { | |
591 | if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest)) | |
592 | return -ENODEV; | |
5554345b | 593 | fip_flags = 0; |
e10f8c66 JE |
594 | } else { |
595 | fcf = fip->sel_fcf; | |
596 | if (!fcf) | |
597 | return -ENODEV; | |
598 | fip_flags = fcf->flags; | |
599 | fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : | |
600 | FIP_FL_FPMA; | |
601 | if (!fip_flags) | |
602 | return -ENODEV; | |
603 | memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN); | |
604 | } | |
97c8389d JE |
605 | memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN); |
606 | cap->eth.h_proto = htons(ETH_P_FIP); | |
607 | ||
608 | cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER); | |
609 | cap->fip.fip_op = htons(FIP_OP_LS); | |
5554345b JE |
610 | if (op == ELS_LS_ACC || op == ELS_LS_RJT) |
611 | cap->fip.fip_subcode = FIP_SC_REP; | |
612 | else | |
613 | cap->fip.fip_subcode = FIP_SC_REQ; | |
5a84baea | 614 | cap->fip.fip_flags = htons(fip_flags); |
97c8389d JE |
615 | |
616 | cap->encaps.fd_desc.fip_dtype = dtype; | |
617 | cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW; | |
618 | ||
5554345b JE |
619 | if (op != ELS_LS_RJT) { |
620 | dlen += sizeof(*mac); | |
621 | mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac)); | |
622 | memset(mac, 0, sizeof(*mac)); | |
623 | mac->fd_desc.fip_dtype = FIP_DT_MAC; | |
624 | mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW; | |
625 | if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) { | |
626 | memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN); | |
627 | } else if (fip->mode == FIP_MODE_VN2VN) { | |
628 | hton24(mac->fd_mac, FIP_VN_FC_MAP); | |
629 | hton24(mac->fd_mac + 3, fip->port_id); | |
630 | } else if (fip_flags & FIP_FL_SPMA) { | |
631 | LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n"); | |
632 | memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN); | |
633 | } else { | |
634 | LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n"); | |
635 | /* FPMA only FLOGI. Must leave the MAC desc zeroed. */ | |
636 | } | |
593abc07 | 637 | } |
5554345b | 638 | cap->fip.fip_dl_len = htons(dlen / FIP_BPW); |
97c8389d | 639 | |
0f491539 | 640 | skb->protocol = htons(ETH_P_FIP); |
6f6c2aa3 | 641 | skb->priority = fip->priority; |
97c8389d JE |
642 | skb_reset_mac_header(skb); |
643 | skb_reset_network_header(skb); | |
644 | return 0; | |
645 | } | |
646 | ||
647 | /** | |
648 | * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate. | |
649 | * @fip: FCoE controller. | |
11b56188 | 650 | * @lport: libfc fc_lport to send from |
97c8389d JE |
651 | * @skb: FCoE ELS frame including FC header but no FCoE headers. |
652 | * | |
653 | * Returns a non-zero error code if the frame should not be sent. | |
654 | * Returns zero if the caller should send the frame with FCoE encapsulation. | |
655 | * | |
656 | * The caller must check that the length is a multiple of 4. | |
657 | * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes). | |
e10f8c66 | 658 | * The the skb must also be an fc_frame. |
794d98e7 JE |
659 | * |
660 | * This is called from the lower-level driver with spinlocks held, | |
661 | * so we must not take a mutex here. | |
97c8389d | 662 | */ |
11b56188 CL |
663 | int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport, |
664 | struct sk_buff *skb) | |
97c8389d | 665 | { |
e10f8c66 | 666 | struct fc_frame *fp; |
97c8389d JE |
667 | struct fc_frame_header *fh; |
668 | u16 old_xid; | |
669 | u8 op; | |
11b56188 | 670 | u8 mac[ETH_ALEN]; |
97c8389d | 671 | |
e10f8c66 | 672 | fp = container_of(skb, struct fc_frame, skb); |
97c8389d JE |
673 | fh = (struct fc_frame_header *)skb->data; |
674 | op = *(u8 *)(fh + 1); | |
675 | ||
e10f8c66 | 676 | if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) { |
97c8389d JE |
677 | old_xid = fip->flogi_oxid; |
678 | fip->flogi_oxid = ntohs(fh->fh_ox_id); | |
679 | if (fip->state == FIP_ST_AUTO) { | |
680 | if (old_xid == FC_XID_UNKNOWN) | |
681 | fip->flogi_count = 0; | |
682 | fip->flogi_count++; | |
683 | if (fip->flogi_count < 3) | |
684 | goto drop; | |
cd229e42 | 685 | fcoe_ctlr_map_dest(fip); |
97c8389d JE |
686 | return 0; |
687 | } | |
5f48f70e | 688 | if (fip->state == FIP_ST_NON_FIP) |
cd229e42 | 689 | fcoe_ctlr_map_dest(fip); |
5f48f70e JE |
690 | } |
691 | ||
692 | if (fip->state == FIP_ST_NON_FIP) | |
693 | return 0; | |
e10f8c66 | 694 | if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN) |
f31f2a1c | 695 | goto drop; |
5f48f70e JE |
696 | switch (op) { |
697 | case ELS_FLOGI: | |
97c8389d | 698 | op = FIP_DT_FLOGI; |
794d98e7 JE |
699 | if (fip->mode == FIP_MODE_VN2VN) |
700 | break; | |
701 | spin_lock_bh(&fip->ctlr_lock); | |
702 | kfree_skb(fip->flogi_req); | |
703 | fip->flogi_req = skb; | |
704 | fip->flogi_req_send = 1; | |
705 | spin_unlock_bh(&fip->ctlr_lock); | |
706 | schedule_work(&fip->timer_work); | |
707 | return -EINPROGRESS; | |
97c8389d JE |
708 | case ELS_FDISC: |
709 | if (ntoh24(fh->fh_s_id)) | |
710 | return 0; | |
711 | op = FIP_DT_FDISC; | |
712 | break; | |
713 | case ELS_LOGO: | |
e10f8c66 JE |
714 | if (fip->mode == FIP_MODE_VN2VN) { |
715 | if (fip->state != FIP_ST_VNMP_UP) | |
716 | return -EINVAL; | |
717 | if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI) | |
718 | return -EINVAL; | |
719 | } else { | |
720 | if (fip->state != FIP_ST_ENABLED) | |
721 | return 0; | |
722 | if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI) | |
723 | return 0; | |
724 | } | |
97c8389d JE |
725 | op = FIP_DT_LOGO; |
726 | break; | |
727 | case ELS_LS_ACC: | |
97c8389d | 728 | /* |
e10f8c66 | 729 | * If non-FIP, we may have gotten an SID by accepting an FLOGI |
97c8389d JE |
730 | * from a point-to-point connection. Switch to using |
731 | * the source mac based on the SID. The destination | |
25985edc | 732 | * MAC in this case would have been set by receiving the |
97c8389d JE |
733 | * FLOGI. |
734 | */ | |
e10f8c66 JE |
735 | if (fip->state == FIP_ST_NON_FIP) { |
736 | if (fip->flogi_oxid == FC_XID_UNKNOWN) | |
737 | return 0; | |
738 | fip->flogi_oxid = FC_XID_UNKNOWN; | |
739 | fc_fcoe_set_mac(mac, fh->fh_d_id); | |
740 | fip->update_mac(lport, mac); | |
741 | } | |
742 | /* fall through */ | |
743 | case ELS_LS_RJT: | |
744 | op = fr_encaps(fp); | |
745 | if (op) | |
746 | break; | |
97c8389d JE |
747 | return 0; |
748 | default: | |
e10f8c66 JE |
749 | if (fip->state != FIP_ST_ENABLED && |
750 | fip->state != FIP_ST_VNMP_UP) | |
97c8389d JE |
751 | goto drop; |
752 | return 0; | |
753 | } | |
e10f8c66 JE |
754 | LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n", |
755 | op, ntoh24(fh->fh_d_id)); | |
756 | if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id))) | |
97c8389d JE |
757 | goto drop; |
758 | fip->send(fip, skb); | |
759 | return -EINPROGRESS; | |
760 | drop: | |
761 | kfree_skb(skb); | |
762 | return -EINVAL; | |
763 | } | |
764 | EXPORT_SYMBOL(fcoe_ctlr_els_send); | |
765 | ||
70b51aab RL |
766 | /** |
767 | * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller | |
768 | * @fip: The FCoE controller to free FCFs on | |
97c8389d | 769 | * |
f018b73a | 770 | * Called with lock held and preemption disabled. |
97c8389d | 771 | * |
8690cb83 JE |
772 | * An FCF is considered old if we have missed two advertisements. |
773 | * That is, there have been no valid advertisement from it for 2.5 | |
774 | * times its keep-alive period. | |
97c8389d JE |
775 | * |
776 | * In addition, determine the time when an FCF selection can occur. | |
f3da80e7 YZ |
777 | * |
778 | * Also, increment the MissDiscAdvCount when no advertisement is received | |
779 | * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB). | |
8690cb83 JE |
780 | * |
781 | * Returns the time in jiffies for the next call. | |
97c8389d | 782 | */ |
8690cb83 | 783 | static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip) |
97c8389d JE |
784 | { |
785 | struct fcoe_fcf *fcf; | |
786 | struct fcoe_fcf *next; | |
8690cb83 JE |
787 | unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD); |
788 | unsigned long deadline; | |
97c8389d | 789 | unsigned long sel_time = 0; |
8d55e507 | 790 | struct list_head del_list; |
1bd49b48 | 791 | struct fc_stats *stats; |
97c8389d | 792 | |
8d55e507 RL |
793 | INIT_LIST_HEAD(&del_list); |
794 | ||
1bd49b48 | 795 | stats = per_cpu_ptr(fip->lp->stats, get_cpu()); |
fdb068c6 | 796 | |
97c8389d | 797 | list_for_each_entry_safe(fcf, next, &fip->fcfs, list) { |
8690cb83 JE |
798 | deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2; |
799 | if (fip->sel_fcf == fcf) { | |
800 | if (time_after(jiffies, deadline)) { | |
8690cb83 JE |
801 | stats->MissDiscAdvCount++; |
802 | printk(KERN_INFO "libfcoe: host%d: " | |
803 | "Missing Discovery Advertisement " | |
804 | "for fab %16.16llx count %lld\n", | |
805 | fip->lp->host->host_no, fcf->fabric_name, | |
806 | stats->MissDiscAdvCount); | |
807 | } else if (time_after(next_timer, deadline)) | |
808 | next_timer = deadline; | |
f3da80e7 | 809 | } |
8690cb83 JE |
810 | |
811 | deadline += fcf->fka_period; | |
d99ee45b | 812 | if (time_after_eq(jiffies, deadline)) { |
97c8389d JE |
813 | if (fip->sel_fcf == fcf) |
814 | fip->sel_fcf = NULL; | |
8d55e507 RL |
815 | /* |
816 | * Move to delete list so we can call | |
817 | * fcoe_sysfs_fcf_del (which can sleep) | |
818 | * after the put_cpu(). | |
819 | */ | |
97c8389d | 820 | list_del(&fcf->list); |
8d55e507 | 821 | list_add(&fcf->list, &del_list); |
f018b73a | 822 | stats->VLinkFailureCount++; |
8690cb83 JE |
823 | } else { |
824 | if (time_after(next_timer, deadline)) | |
825 | next_timer = deadline; | |
826 | if (fcoe_ctlr_mtu_valid(fcf) && | |
827 | (!sel_time || time_before(sel_time, fcf->time))) | |
828 | sel_time = fcf->time; | |
97c8389d JE |
829 | } |
830 | } | |
fdb068c6 | 831 | put_cpu(); |
8d55e507 RL |
832 | |
833 | list_for_each_entry_safe(fcf, next, &del_list, list) { | |
834 | /* Removes fcf from current list */ | |
835 | fcoe_sysfs_fcf_del(fcf); | |
836 | } | |
837 | ||
d99ee45b | 838 | if (sel_time && !fip->sel_fcf && !fip->sel_time) { |
97c8389d JE |
839 | sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY); |
840 | fip->sel_time = sel_time; | |
97c8389d | 841 | } |
d99ee45b | 842 | |
8690cb83 | 843 | return next_timer; |
97c8389d JE |
844 | } |
845 | ||
846 | /** | |
70b51aab | 847 | * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry |
0f51c2e5 | 848 | * @fip: The FCoE controller receiving the advertisement |
70b51aab RL |
849 | * @skb: The received FIP advertisement frame |
850 | * @fcf: The resulting FCF entry | |
97c8389d JE |
851 | * |
852 | * Returns zero on a valid parsed advertisement, | |
853 | * otherwise returns non zero value. | |
854 | */ | |
0f51c2e5 JE |
855 | static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip, |
856 | struct sk_buff *skb, struct fcoe_fcf *fcf) | |
97c8389d JE |
857 | { |
858 | struct fip_header *fiph; | |
859 | struct fip_desc *desc = NULL; | |
860 | struct fip_wwn_desc *wwn; | |
861 | struct fip_fab_desc *fab; | |
862 | struct fip_fka_desc *fka; | |
863 | unsigned long t; | |
864 | size_t rlen; | |
865 | size_t dlen; | |
0a9c5d34 | 866 | u32 desc_mask; |
97c8389d JE |
867 | |
868 | memset(fcf, 0, sizeof(*fcf)); | |
869 | fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA); | |
870 | ||
871 | fiph = (struct fip_header *)skb->data; | |
872 | fcf->flags = ntohs(fiph->fip_flags); | |
873 | ||
0a9c5d34 BPG |
874 | /* |
875 | * mask of required descriptors. validating each one clears its bit. | |
876 | */ | |
877 | desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | | |
878 | BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA); | |
879 | ||
97c8389d JE |
880 | rlen = ntohs(fiph->fip_dl_len) * 4; |
881 | if (rlen + sizeof(*fiph) > skb->len) | |
882 | return -EINVAL; | |
883 | ||
884 | desc = (struct fip_desc *)(fiph + 1); | |
885 | while (rlen > 0) { | |
886 | dlen = desc->fip_dlen * FIP_BPW; | |
887 | if (dlen < sizeof(*desc) || dlen > rlen) | |
888 | return -EINVAL; | |
0a9c5d34 BPG |
889 | /* Drop Adv if there are duplicate critical descriptors */ |
890 | if ((desc->fip_dtype < 32) && | |
891 | !(desc_mask & 1U << desc->fip_dtype)) { | |
892 | LIBFCOE_FIP_DBG(fip, "Duplicate Critical " | |
893 | "Descriptors in FIP adv\n"); | |
894 | return -EINVAL; | |
895 | } | |
97c8389d JE |
896 | switch (desc->fip_dtype) { |
897 | case FIP_DT_PRI: | |
898 | if (dlen != sizeof(struct fip_pri_desc)) | |
899 | goto len_err; | |
900 | fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri; | |
0a9c5d34 | 901 | desc_mask &= ~BIT(FIP_DT_PRI); |
97c8389d JE |
902 | break; |
903 | case FIP_DT_MAC: | |
904 | if (dlen != sizeof(struct fip_mac_desc)) | |
905 | goto len_err; | |
906 | memcpy(fcf->fcf_mac, | |
907 | ((struct fip_mac_desc *)desc)->fd_mac, | |
908 | ETH_ALEN); | |
81c11dd2 | 909 | memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN); |
97c8389d | 910 | if (!is_valid_ether_addr(fcf->fcf_mac)) { |
e10f8c66 JE |
911 | LIBFCOE_FIP_DBG(fip, |
912 | "Invalid MAC addr %pM in FIP adv\n", | |
913 | fcf->fcf_mac); | |
97c8389d JE |
914 | return -EINVAL; |
915 | } | |
0a9c5d34 | 916 | desc_mask &= ~BIT(FIP_DT_MAC); |
97c8389d JE |
917 | break; |
918 | case FIP_DT_NAME: | |
919 | if (dlen != sizeof(struct fip_wwn_desc)) | |
920 | goto len_err; | |
921 | wwn = (struct fip_wwn_desc *)desc; | |
922 | fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn); | |
0a9c5d34 | 923 | desc_mask &= ~BIT(FIP_DT_NAME); |
97c8389d JE |
924 | break; |
925 | case FIP_DT_FAB: | |
926 | if (dlen != sizeof(struct fip_fab_desc)) | |
927 | goto len_err; | |
928 | fab = (struct fip_fab_desc *)desc; | |
929 | fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn); | |
930 | fcf->vfid = ntohs(fab->fd_vfid); | |
931 | fcf->fc_map = ntoh24(fab->fd_map); | |
0a9c5d34 | 932 | desc_mask &= ~BIT(FIP_DT_FAB); |
97c8389d JE |
933 | break; |
934 | case FIP_DT_FKA: | |
935 | if (dlen != sizeof(struct fip_fka_desc)) | |
936 | goto len_err; | |
937 | fka = (struct fip_fka_desc *)desc; | |
8cdffdcc YZ |
938 | if (fka->fd_flags & FIP_FKA_ADV_D) |
939 | fcf->fd_flags = 1; | |
97c8389d JE |
940 | t = ntohl(fka->fd_fka_period); |
941 | if (t >= FCOE_CTLR_MIN_FKA) | |
942 | fcf->fka_period = msecs_to_jiffies(t); | |
0a9c5d34 | 943 | desc_mask &= ~BIT(FIP_DT_FKA); |
97c8389d JE |
944 | break; |
945 | case FIP_DT_MAP_OUI: | |
946 | case FIP_DT_FCOE_SIZE: | |
947 | case FIP_DT_FLOGI: | |
948 | case FIP_DT_FDISC: | |
949 | case FIP_DT_LOGO: | |
950 | case FIP_DT_ELP: | |
951 | default: | |
0f51c2e5 | 952 | LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x " |
650bd12b | 953 | "in FIP adv\n", desc->fip_dtype); |
97c8389d JE |
954 | /* standard says ignore unknown descriptors >= 128 */ |
955 | if (desc->fip_dtype < FIP_DT_VENDOR_BASE) | |
956 | return -EINVAL; | |
1508f3ec | 957 | break; |
97c8389d JE |
958 | } |
959 | desc = (struct fip_desc *)((char *)desc + dlen); | |
960 | rlen -= dlen; | |
961 | } | |
962 | if (!fcf->fc_map || (fcf->fc_map & 0x10000)) | |
963 | return -EINVAL; | |
240778e8 | 964 | if (!fcf->switch_name) |
97c8389d | 965 | return -EINVAL; |
0a9c5d34 BPG |
966 | if (desc_mask) { |
967 | LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n", | |
968 | desc_mask); | |
969 | return -EINVAL; | |
970 | } | |
97c8389d JE |
971 | return 0; |
972 | ||
973 | len_err: | |
0f51c2e5 | 974 | LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n", |
650bd12b | 975 | desc->fip_dtype, dlen); |
97c8389d JE |
976 | return -EINVAL; |
977 | } | |
978 | ||
979 | /** | |
70b51aab RL |
980 | * fcoe_ctlr_recv_adv() - Handle an incoming advertisement |
981 | * @fip: The FCoE controller receiving the advertisement | |
982 | * @skb: The received FIP packet | |
97c8389d JE |
983 | */ |
984 | static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb) | |
985 | { | |
986 | struct fcoe_fcf *fcf; | |
987 | struct fcoe_fcf new; | |
97c8389d JE |
988 | unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV); |
989 | int first = 0; | |
990 | int mtu_valid; | |
8d55e507 RL |
991 | int found = 0; |
992 | int rc = 0; | |
97c8389d | 993 | |
0f51c2e5 | 994 | if (fcoe_ctlr_parse_adv(fip, skb, &new)) |
97c8389d JE |
995 | return; |
996 | ||
fdb068c6 | 997 | mutex_lock(&fip->ctlr_mutex); |
97c8389d | 998 | first = list_empty(&fip->fcfs); |
97c8389d JE |
999 | list_for_each_entry(fcf, &fip->fcfs, list) { |
1000 | if (fcf->switch_name == new.switch_name && | |
1001 | fcf->fabric_name == new.fabric_name && | |
1002 | fcf->fc_map == new.fc_map && | |
1003 | compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) { | |
8d55e507 | 1004 | found = 1; |
97c8389d JE |
1005 | break; |
1006 | } | |
1007 | } | |
1008 | if (!found) { | |
1009 | if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT) | |
1010 | goto out; | |
1011 | ||
1012 | fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC); | |
1013 | if (!fcf) | |
1014 | goto out; | |
1015 | ||
97c8389d | 1016 | memcpy(fcf, &new, sizeof(new)); |
8d55e507 RL |
1017 | fcf->fip = fip; |
1018 | rc = fcoe_sysfs_fcf_add(fcf); | |
1019 | if (rc) { | |
1020 | printk(KERN_ERR "Failed to allocate sysfs instance " | |
1021 | "for FCF, fab %16.16llx mac %pM\n", | |
1022 | new.fabric_name, new.fcf_mac); | |
1023 | kfree(fcf); | |
1024 | goto out; | |
1025 | } | |
97c8389d JE |
1026 | } else { |
1027 | /* | |
c600fea2 JE |
1028 | * Update the FCF's keep-alive descriptor flags. |
1029 | * Other flag changes from new advertisements are | |
1030 | * ignored after a solicited advertisement is | |
1031 | * received and the FCF is selectable (usable). | |
97c8389d | 1032 | */ |
c600fea2 JE |
1033 | fcf->fd_flags = new.fd_flags; |
1034 | if (!fcoe_ctlr_fcf_usable(fcf)) | |
1035 | fcf->flags = new.flags; | |
1036 | ||
d99ee45b | 1037 | if (fcf == fip->sel_fcf && !fcf->fd_flags) { |
97c8389d JE |
1038 | fip->ctlr_ka_time -= fcf->fka_period; |
1039 | fip->ctlr_ka_time += new.fka_period; | |
1040 | if (time_before(fip->ctlr_ka_time, fip->timer.expires)) | |
1041 | mod_timer(&fip->timer, fip->ctlr_ka_time); | |
c600fea2 | 1042 | } |
97c8389d JE |
1043 | fcf->fka_period = new.fka_period; |
1044 | memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN); | |
1045 | } | |
8d55e507 | 1046 | |
97c8389d JE |
1047 | mtu_valid = fcoe_ctlr_mtu_valid(fcf); |
1048 | fcf->time = jiffies; | |
9069f5c4 JE |
1049 | if (!found) |
1050 | LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n", | |
1051 | fcf->fabric_name, fcf->fcf_mac); | |
97c8389d JE |
1052 | |
1053 | /* | |
1054 | * If this advertisement is not solicited and our max receive size | |
1055 | * hasn't been verified, send a solicited advertisement. | |
1056 | */ | |
1057 | if (!mtu_valid) | |
1058 | fcoe_ctlr_solicit(fip, fcf); | |
1059 | ||
1060 | /* | |
1061 | * If its been a while since we did a solicit, and this is | |
1062 | * the first advertisement we've received, do a multicast | |
1063 | * solicitation to gather as many advertisements as we can | |
1064 | * before selection occurs. | |
1065 | */ | |
1066 | if (first && time_after(jiffies, fip->sol_time + sol_tov)) | |
1067 | fcoe_ctlr_solicit(fip, NULL); | |
1068 | ||
981c1154 JE |
1069 | /* |
1070 | * Put this FCF at the head of the list for priority among equals. | |
1071 | * This helps in the case of an NPV switch which insists we use | |
1072 | * the FCF that answers multicast solicitations, not the others that | |
1073 | * are sending periodic multicast advertisements. | |
1074 | */ | |
63ce2499 KS |
1075 | if (mtu_valid) |
1076 | list_move(&fcf->list, &fip->fcfs); | |
981c1154 | 1077 | |
97c8389d JE |
1078 | /* |
1079 | * If this is the first validated FCF, note the time and | |
1080 | * set a timer to trigger selection. | |
1081 | */ | |
d99ee45b | 1082 | if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) { |
97c8389d | 1083 | fip->sel_time = jiffies + |
70b51aab | 1084 | msecs_to_jiffies(FCOE_CTLR_START_DELAY); |
97c8389d JE |
1085 | if (!timer_pending(&fip->timer) || |
1086 | time_before(fip->sel_time, fip->timer.expires)) | |
1087 | mod_timer(&fip->timer, fip->sel_time); | |
1088 | } | |
8d55e507 | 1089 | |
97c8389d | 1090 | out: |
fdb068c6 | 1091 | mutex_unlock(&fip->ctlr_mutex); |
97c8389d JE |
1092 | } |
1093 | ||
1094 | /** | |
70b51aab RL |
1095 | * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame |
1096 | * @fip: The FCoE controller which received the packet | |
1097 | * @skb: The received FIP packet | |
97c8389d JE |
1098 | */ |
1099 | static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb) | |
1100 | { | |
70b51aab | 1101 | struct fc_lport *lport = fip->lp; |
97c8389d | 1102 | struct fip_header *fiph; |
11b56188 | 1103 | struct fc_frame *fp = (struct fc_frame *)skb; |
97c8389d JE |
1104 | struct fc_frame_header *fh = NULL; |
1105 | struct fip_desc *desc; | |
1106 | struct fip_encaps *els; | |
81c11dd2 | 1107 | struct fcoe_fcf *sel; |
1bd49b48 | 1108 | struct fc_stats *stats; |
97c8389d JE |
1109 | enum fip_desc_type els_dtype = 0; |
1110 | u8 els_op; | |
1111 | u8 sub; | |
1112 | u8 granted_mac[ETH_ALEN] = { 0 }; | |
1113 | size_t els_len = 0; | |
1114 | size_t rlen; | |
1115 | size_t dlen; | |
be61331d BPG |
1116 | u32 desc_mask = 0; |
1117 | u32 desc_cnt = 0; | |
97c8389d JE |
1118 | |
1119 | fiph = (struct fip_header *)skb->data; | |
1120 | sub = fiph->fip_subcode; | |
1121 | if (sub != FIP_SC_REQ && sub != FIP_SC_REP) | |
1122 | goto drop; | |
1123 | ||
1124 | rlen = ntohs(fiph->fip_dl_len) * 4; | |
1125 | if (rlen + sizeof(*fiph) > skb->len) | |
1126 | goto drop; | |
1127 | ||
1128 | desc = (struct fip_desc *)(fiph + 1); | |
1129 | while (rlen > 0) { | |
be61331d | 1130 | desc_cnt++; |
97c8389d JE |
1131 | dlen = desc->fip_dlen * FIP_BPW; |
1132 | if (dlen < sizeof(*desc) || dlen > rlen) | |
1133 | goto drop; | |
0a9c5d34 BPG |
1134 | /* Drop ELS if there are duplicate critical descriptors */ |
1135 | if (desc->fip_dtype < 32) { | |
81c11dd2 BPG |
1136 | if ((desc->fip_dtype != FIP_DT_MAC) && |
1137 | (desc_mask & 1U << desc->fip_dtype)) { | |
0a9c5d34 BPG |
1138 | LIBFCOE_FIP_DBG(fip, "Duplicate Critical " |
1139 | "Descriptors in FIP ELS\n"); | |
1140 | goto drop; | |
1141 | } | |
be61331d | 1142 | desc_mask |= (1 << desc->fip_dtype); |
0a9c5d34 | 1143 | } |
97c8389d JE |
1144 | switch (desc->fip_dtype) { |
1145 | case FIP_DT_MAC: | |
81c11dd2 | 1146 | sel = fip->sel_fcf; |
be61331d BPG |
1147 | if (desc_cnt == 1) { |
1148 | LIBFCOE_FIP_DBG(fip, "FIP descriptors " | |
1149 | "received out of order\n"); | |
1150 | goto drop; | |
1151 | } | |
81c11dd2 BPG |
1152 | /* |
1153 | * Some switch implementations send two MAC descriptors, | |
1154 | * with first MAC(granted_mac) being the FPMA, and the | |
1155 | * second one(fcoe_mac) is used as destination address | |
1156 | * for sending/receiving FCoE packets. FIP traffic is | |
1157 | * sent using fip_mac. For regular switches, both | |
1158 | * fip_mac and fcoe_mac would be the same. | |
1159 | */ | |
1160 | if (desc_cnt == 2) | |
1161 | memcpy(granted_mac, | |
1162 | ((struct fip_mac_desc *)desc)->fd_mac, | |
1163 | ETH_ALEN); | |
be61331d | 1164 | |
97c8389d JE |
1165 | if (dlen != sizeof(struct fip_mac_desc)) |
1166 | goto len_err; | |
81c11dd2 BPG |
1167 | |
1168 | if ((desc_cnt == 3) && (sel)) | |
1169 | memcpy(sel->fcoe_mac, | |
1170 | ((struct fip_mac_desc *)desc)->fd_mac, | |
1171 | ETH_ALEN); | |
97c8389d JE |
1172 | break; |
1173 | case FIP_DT_FLOGI: | |
1174 | case FIP_DT_FDISC: | |
1175 | case FIP_DT_LOGO: | |
1176 | case FIP_DT_ELP: | |
be61331d BPG |
1177 | if (desc_cnt != 1) { |
1178 | LIBFCOE_FIP_DBG(fip, "FIP descriptors " | |
1179 | "received out of order\n"); | |
1180 | goto drop; | |
1181 | } | |
97c8389d JE |
1182 | if (fh) |
1183 | goto drop; | |
1184 | if (dlen < sizeof(*els) + sizeof(*fh) + 1) | |
1185 | goto len_err; | |
1186 | els_len = dlen - sizeof(*els); | |
1187 | els = (struct fip_encaps *)desc; | |
1188 | fh = (struct fc_frame_header *)(els + 1); | |
1189 | els_dtype = desc->fip_dtype; | |
1190 | break; | |
1191 | default: | |
0f51c2e5 | 1192 | LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x " |
650bd12b | 1193 | "in FIP adv\n", desc->fip_dtype); |
97c8389d JE |
1194 | /* standard says ignore unknown descriptors >= 128 */ |
1195 | if (desc->fip_dtype < FIP_DT_VENDOR_BASE) | |
1196 | goto drop; | |
be61331d BPG |
1197 | if (desc_cnt <= 2) { |
1198 | LIBFCOE_FIP_DBG(fip, "FIP descriptors " | |
1199 | "received out of order\n"); | |
1200 | goto drop; | |
1201 | } | |
1508f3ec | 1202 | break; |
97c8389d JE |
1203 | } |
1204 | desc = (struct fip_desc *)((char *)desc + dlen); | |
1205 | rlen -= dlen; | |
1206 | } | |
1207 | ||
1208 | if (!fh) | |
1209 | goto drop; | |
1210 | els_op = *(u8 *)(fh + 1); | |
1211 | ||
e10f8c66 | 1212 | if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) && |
794d98e7 JE |
1213 | sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) { |
1214 | if (els_op == ELS_LS_ACC) { | |
1215 | if (!is_valid_ether_addr(granted_mac)) { | |
1216 | LIBFCOE_FIP_DBG(fip, | |
1217 | "Invalid MAC address %pM in FIP ELS\n", | |
1218 | granted_mac); | |
1219 | goto drop; | |
1220 | } | |
1221 | memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN); | |
e10f8c66 | 1222 | |
794d98e7 JE |
1223 | if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) { |
1224 | fip->flogi_oxid = FC_XID_UNKNOWN; | |
1225 | if (els_dtype == FIP_DT_FLOGI) | |
1226 | fcoe_ctlr_announce(fip); | |
1227 | } | |
1228 | } else if (els_dtype == FIP_DT_FLOGI && | |
1229 | !fcoe_ctlr_flogi_retry(fip)) | |
1230 | goto drop; /* retrying FLOGI so drop reject */ | |
e10f8c66 | 1231 | } |
97c8389d | 1232 | |
be61331d BPG |
1233 | if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) && |
1234 | (!(1U << FIP_DT_MAC & desc_mask)))) { | |
1235 | LIBFCOE_FIP_DBG(fip, "Missing critical descriptors " | |
1236 | "in FIP ELS\n"); | |
1237 | goto drop; | |
1238 | } | |
1239 | ||
97c8389d JE |
1240 | /* |
1241 | * Convert skb into an fc_frame containing only the ELS. | |
1242 | */ | |
1243 | skb_pull(skb, (u8 *)fh - skb->data); | |
1244 | skb_trim(skb, els_len); | |
1245 | fp = (struct fc_frame *)skb; | |
1246 | fc_frame_init(fp); | |
1247 | fr_sof(fp) = FC_SOF_I3; | |
1248 | fr_eof(fp) = FC_EOF_T; | |
70b51aab | 1249 | fr_dev(fp) = lport; |
e10f8c66 | 1250 | fr_encaps(fp) = els_dtype; |
97c8389d | 1251 | |
1bd49b48 | 1252 | stats = per_cpu_ptr(lport->stats, get_cpu()); |
97c8389d JE |
1253 | stats->RxFrames++; |
1254 | stats->RxWords += skb->len / FIP_BPW; | |
f018b73a | 1255 | put_cpu(); |
97c8389d | 1256 | |
70b51aab | 1257 | fc_exch_recv(lport, fp); |
97c8389d JE |
1258 | return; |
1259 | ||
1260 | len_err: | |
0f51c2e5 | 1261 | LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n", |
650bd12b | 1262 | desc->fip_dtype, dlen); |
97c8389d JE |
1263 | drop: |
1264 | kfree_skb(skb); | |
1265 | } | |
1266 | ||
1267 | /** | |
70b51aab RL |
1268 | * fcoe_ctlr_recv_els() - Handle an incoming link reset frame |
1269 | * @fip: The FCoE controller that received the frame | |
1270 | * @fh: The received FIP header | |
97c8389d JE |
1271 | * |
1272 | * There may be multiple VN_Port descriptors. | |
1273 | * The overall length has already been checked. | |
1274 | */ | |
1275 | static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip, | |
70b51aab | 1276 | struct fip_header *fh) |
97c8389d JE |
1277 | { |
1278 | struct fip_desc *desc; | |
1279 | struct fip_mac_desc *mp; | |
1280 | struct fip_wwn_desc *wp; | |
1281 | struct fip_vn_desc *vp; | |
1282 | size_t rlen; | |
1283 | size_t dlen; | |
1284 | struct fcoe_fcf *fcf = fip->sel_fcf; | |
70b51aab | 1285 | struct fc_lport *lport = fip->lp; |
5550fda7 BPG |
1286 | struct fc_lport *vn_port = NULL; |
1287 | u32 desc_mask; | |
c051ad2e BPG |
1288 | int num_vlink_desc; |
1289 | int reset_phys_port = 0; | |
1290 | struct fip_vn_desc **vlink_desc_arr = NULL; | |
97c8389d | 1291 | |
0f51c2e5 | 1292 | LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n"); |
d29510a2 | 1293 | |
7b2787ec | 1294 | if (!fcf || !lport->port_id) |
97c8389d JE |
1295 | return; |
1296 | ||
1297 | /* | |
1298 | * mask of required descriptors. Validating each one clears its bit. | |
1299 | */ | |
c051ad2e | 1300 | desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME); |
97c8389d JE |
1301 | |
1302 | rlen = ntohs(fh->fip_dl_len) * FIP_BPW; | |
1303 | desc = (struct fip_desc *)(fh + 1); | |
c051ad2e BPG |
1304 | |
1305 | /* | |
1306 | * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen' | |
1307 | * before determining max Vx_Port descriptor but a buggy FCF could have | |
1308 | * omited either or both MAC Address and Name Identifier descriptors | |
1309 | */ | |
1310 | num_vlink_desc = rlen / sizeof(*vp); | |
1311 | if (num_vlink_desc) | |
1312 | vlink_desc_arr = kmalloc(sizeof(vp) * num_vlink_desc, | |
1313 | GFP_ATOMIC); | |
1314 | if (!vlink_desc_arr) | |
1315 | return; | |
1316 | num_vlink_desc = 0; | |
1317 | ||
97c8389d JE |
1318 | while (rlen >= sizeof(*desc)) { |
1319 | dlen = desc->fip_dlen * FIP_BPW; | |
1320 | if (dlen > rlen) | |
c051ad2e | 1321 | goto err; |
0a9c5d34 BPG |
1322 | /* Drop CVL if there are duplicate critical descriptors */ |
1323 | if ((desc->fip_dtype < 32) && | |
c051ad2e | 1324 | (desc->fip_dtype != FIP_DT_VN_ID) && |
0a9c5d34 BPG |
1325 | !(desc_mask & 1U << desc->fip_dtype)) { |
1326 | LIBFCOE_FIP_DBG(fip, "Duplicate Critical " | |
1327 | "Descriptors in FIP CVL\n"); | |
c051ad2e | 1328 | goto err; |
0a9c5d34 | 1329 | } |
97c8389d JE |
1330 | switch (desc->fip_dtype) { |
1331 | case FIP_DT_MAC: | |
1332 | mp = (struct fip_mac_desc *)desc; | |
1333 | if (dlen < sizeof(*mp)) | |
c051ad2e | 1334 | goto err; |
97c8389d | 1335 | if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac)) |
c051ad2e | 1336 | goto err; |
97c8389d JE |
1337 | desc_mask &= ~BIT(FIP_DT_MAC); |
1338 | break; | |
1339 | case FIP_DT_NAME: | |
1340 | wp = (struct fip_wwn_desc *)desc; | |
1341 | if (dlen < sizeof(*wp)) | |
c051ad2e | 1342 | goto err; |
97c8389d | 1343 | if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name) |
c051ad2e | 1344 | goto err; |
97c8389d JE |
1345 | desc_mask &= ~BIT(FIP_DT_NAME); |
1346 | break; | |
1347 | case FIP_DT_VN_ID: | |
1348 | vp = (struct fip_vn_desc *)desc; | |
1349 | if (dlen < sizeof(*vp)) | |
c051ad2e BPG |
1350 | goto err; |
1351 | vlink_desc_arr[num_vlink_desc++] = vp; | |
1352 | vn_port = fc_vport_id_lookup(lport, | |
1353 | ntoh24(vp->fd_fc_id)); | |
1354 | if (vn_port && (vn_port == lport)) { | |
1355 | mutex_lock(&fip->ctlr_mutex); | |
1bd49b48 | 1356 | per_cpu_ptr(lport->stats, |
c051ad2e BPG |
1357 | get_cpu())->VLinkFailureCount++; |
1358 | put_cpu(); | |
1359 | fcoe_ctlr_reset(fip); | |
1360 | mutex_unlock(&fip->ctlr_mutex); | |
5550fda7 | 1361 | } |
97c8389d JE |
1362 | break; |
1363 | default: | |
1364 | /* standard says ignore unknown descriptors >= 128 */ | |
1365 | if (desc->fip_dtype < FIP_DT_VENDOR_BASE) | |
c051ad2e | 1366 | goto err; |
97c8389d JE |
1367 | break; |
1368 | } | |
1369 | desc = (struct fip_desc *)((char *)desc + dlen); | |
1370 | rlen -= dlen; | |
1371 | } | |
1372 | ||
1373 | /* | |
1374 | * reset only if all required descriptors were present and valid. | |
1375 | */ | |
c051ad2e | 1376 | if (desc_mask) |
0f51c2e5 JE |
1377 | LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n", |
1378 | desc_mask); | |
c051ad2e BPG |
1379 | else if (!num_vlink_desc) { |
1380 | LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n"); | |
1381 | /* | |
1382 | * No Vx_Port description. Clear all NPIV ports, | |
1383 | * followed by physical port | |
1384 | */ | |
c051ad2e | 1385 | mutex_lock(&fip->ctlr_mutex); |
1bd49b48 | 1386 | per_cpu_ptr(lport->stats, get_cpu())->VLinkFailureCount++; |
c051ad2e BPG |
1387 | put_cpu(); |
1388 | fcoe_ctlr_reset(fip); | |
1389 | mutex_unlock(&fip->ctlr_mutex); | |
1390 | ||
14619ea6 BPG |
1391 | mutex_lock(&lport->lp_mutex); |
1392 | list_for_each_entry(vn_port, &lport->vports, list) | |
1393 | fc_lport_reset(vn_port); | |
1394 | mutex_unlock(&lport->lp_mutex); | |
1395 | ||
c051ad2e BPG |
1396 | fc_lport_reset(fip->lp); |
1397 | fcoe_ctlr_solicit(fip, NULL); | |
97c8389d | 1398 | } else { |
c051ad2e | 1399 | int i; |
dd42dac4 | 1400 | |
c051ad2e BPG |
1401 | LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n"); |
1402 | for (i = 0; i < num_vlink_desc; i++) { | |
1403 | vp = vlink_desc_arr[i]; | |
1404 | vn_port = fc_vport_id_lookup(lport, | |
1405 | ntoh24(vp->fd_fc_id)); | |
1406 | if (!vn_port) | |
1407 | continue; | |
1408 | ||
1409 | /* | |
1410 | * 'port_id' is already validated, check MAC address and | |
1411 | * wwpn | |
1412 | */ | |
1413 | if (compare_ether_addr(fip->get_src_addr(vn_port), | |
1414 | vp->fd_mac) != 0 || | |
1415 | get_unaligned_be64(&vp->fd_wwpn) != | |
1416 | vn_port->wwpn) | |
1417 | continue; | |
1418 | ||
1419 | if (vn_port == lport) | |
1420 | /* | |
1421 | * Physical port, defer processing till all | |
1422 | * listed NPIV ports are cleared | |
1423 | */ | |
1424 | reset_phys_port = 1; | |
1425 | else /* NPIV port */ | |
1426 | fc_lport_reset(vn_port); | |
1427 | } | |
5550fda7 | 1428 | |
c051ad2e | 1429 | if (reset_phys_port) { |
5550fda7 BPG |
1430 | fc_lport_reset(fip->lp); |
1431 | fcoe_ctlr_solicit(fip, NULL); | |
1432 | } | |
97c8389d | 1433 | } |
c051ad2e BPG |
1434 | |
1435 | err: | |
1436 | kfree(vlink_desc_arr); | |
97c8389d JE |
1437 | } |
1438 | ||
1439 | /** | |
70b51aab RL |
1440 | * fcoe_ctlr_recv() - Receive a FIP packet |
1441 | * @fip: The FCoE controller that received the packet | |
1442 | * @skb: The received FIP packet | |
97c8389d | 1443 | * |
1f4aed81 | 1444 | * This may be called from either NET_RX_SOFTIRQ or IRQ. |
97c8389d JE |
1445 | */ |
1446 | void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb) | |
1447 | { | |
1f4aed81 | 1448 | skb_queue_tail(&fip->fip_recv_list, skb); |
97c8389d JE |
1449 | schedule_work(&fip->recv_work); |
1450 | } | |
1451 | EXPORT_SYMBOL(fcoe_ctlr_recv); | |
1452 | ||
1453 | /** | |
70b51aab RL |
1454 | * fcoe_ctlr_recv_handler() - Receive a FIP frame |
1455 | * @fip: The FCoE controller that received the frame | |
1456 | * @skb: The received FIP frame | |
97c8389d JE |
1457 | * |
1458 | * Returns non-zero if the frame is dropped. | |
1459 | */ | |
1460 | static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb) | |
1461 | { | |
1462 | struct fip_header *fiph; | |
1463 | struct ethhdr *eh; | |
1464 | enum fip_state state; | |
1465 | u16 op; | |
1466 | u8 sub; | |
1467 | ||
1468 | if (skb_linearize(skb)) | |
1469 | goto drop; | |
1470 | if (skb->len < sizeof(*fiph)) | |
1471 | goto drop; | |
1472 | eh = eth_hdr(skb); | |
e10f8c66 JE |
1473 | if (fip->mode == FIP_MODE_VN2VN) { |
1474 | if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) && | |
1475 | compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) && | |
1476 | compare_ether_addr(eh->h_dest, fcoe_all_p2p)) | |
1477 | goto drop; | |
1478 | } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) && | |
1479 | compare_ether_addr(eh->h_dest, fcoe_all_enode)) | |
97c8389d JE |
1480 | goto drop; |
1481 | fiph = (struct fip_header *)skb->data; | |
1482 | op = ntohs(fiph->fip_op); | |
1483 | sub = fiph->fip_subcode; | |
1484 | ||
97c8389d JE |
1485 | if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER) |
1486 | goto drop; | |
1487 | if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len) | |
1488 | goto drop; | |
1489 | ||
fdb068c6 | 1490 | mutex_lock(&fip->ctlr_mutex); |
97c8389d JE |
1491 | state = fip->state; |
1492 | if (state == FIP_ST_AUTO) { | |
1493 | fip->map_dest = 0; | |
9b651da9 | 1494 | fcoe_ctlr_set_state(fip, FIP_ST_ENABLED); |
97c8389d | 1495 | state = FIP_ST_ENABLED; |
0f51c2e5 | 1496 | LIBFCOE_FIP_DBG(fip, "Using FIP mode\n"); |
97c8389d | 1497 | } |
fdb068c6 | 1498 | mutex_unlock(&fip->ctlr_mutex); |
e10f8c66 JE |
1499 | |
1500 | if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN) | |
1501 | return fcoe_ctlr_vn_recv(fip, skb); | |
1502 | ||
1503 | if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP && | |
1504 | state != FIP_ST_VNMP_CLAIM) | |
97c8389d JE |
1505 | goto drop; |
1506 | ||
1507 | if (op == FIP_OP_LS) { | |
1508 | fcoe_ctlr_recv_els(fip, skb); /* consumes skb */ | |
1509 | return 0; | |
1510 | } | |
e10f8c66 JE |
1511 | |
1512 | if (state != FIP_ST_ENABLED) | |
1513 | goto drop; | |
1514 | ||
97c8389d JE |
1515 | if (op == FIP_OP_DISC && sub == FIP_SC_ADV) |
1516 | fcoe_ctlr_recv_adv(fip, skb); | |
1517 | else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) | |
1518 | fcoe_ctlr_recv_clr_vlink(fip, fiph); | |
1519 | kfree_skb(skb); | |
1520 | return 0; | |
1521 | drop: | |
1522 | kfree_skb(skb); | |
1523 | return -1; | |
1524 | } | |
1525 | ||
1526 | /** | |
70b51aab RL |
1527 | * fcoe_ctlr_select() - Select the best FCF (if possible) |
1528 | * @fip: The FCoE controller | |
97c8389d | 1529 | * |
ba9cd5d0 JE |
1530 | * Returns the selected FCF, or NULL if none are usable. |
1531 | * | |
97c8389d JE |
1532 | * If there are conflicting advertisements, no FCF can be chosen. |
1533 | * | |
794d98e7 JE |
1534 | * If there is already a selected FCF, this will choose a better one or |
1535 | * an equivalent one that hasn't already been sent a FLOGI. | |
1536 | * | |
97c8389d JE |
1537 | * Called with lock held. |
1538 | */ | |
ba9cd5d0 | 1539 | static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip) |
97c8389d JE |
1540 | { |
1541 | struct fcoe_fcf *fcf; | |
794d98e7 | 1542 | struct fcoe_fcf *best = fip->sel_fcf; |
b69ae0ae JE |
1543 | struct fcoe_fcf *first; |
1544 | ||
1545 | first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list); | |
97c8389d JE |
1546 | |
1547 | list_for_each_entry(fcf, &fip->fcfs, list) { | |
9069f5c4 JE |
1548 | LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx " |
1549 | "VFID %d mac %pM map %x val %d " | |
1550 | "sent %u pri %u\n", | |
1551 | fcf->fabric_name, fcf->vfid, fcf->fcf_mac, | |
1552 | fcf->fc_map, fcoe_ctlr_mtu_valid(fcf), | |
1553 | fcf->flogi_sent, fcf->pri); | |
b69ae0ae JE |
1554 | if (fcf->fabric_name != first->fabric_name || |
1555 | fcf->vfid != first->vfid || | |
1556 | fcf->fc_map != first->fc_map) { | |
1557 | LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, " | |
1558 | "or FC-MAP\n"); | |
1559 | return NULL; | |
1560 | } | |
794d98e7 JE |
1561 | if (fcf->flogi_sent) |
1562 | continue; | |
97c8389d | 1563 | if (!fcoe_ctlr_fcf_usable(fcf)) { |
9f8f3aa6 CL |
1564 | LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx " |
1565 | "map %x %svalid %savailable\n", | |
1566 | fcf->fabric_name, fcf->fc_map, | |
1567 | (fcf->flags & FIP_FL_SOL) ? "" : "in", | |
1568 | (fcf->flags & FIP_FL_AVAIL) ? | |
1569 | "" : "un"); | |
97c8389d JE |
1570 | continue; |
1571 | } | |
794d98e7 | 1572 | if (!best || fcf->pri < best->pri || best->flogi_sent) |
97c8389d JE |
1573 | best = fcf; |
1574 | } | |
1575 | fip->sel_fcf = best; | |
c47036a7 | 1576 | if (best) { |
9069f5c4 | 1577 | LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac); |
c47036a7 JE |
1578 | fip->port_ka_time = jiffies + |
1579 | msecs_to_jiffies(FIP_VN_KA_PERIOD); | |
1580 | fip->ctlr_ka_time = jiffies + best->fka_period; | |
1581 | if (time_before(fip->ctlr_ka_time, fip->timer.expires)) | |
1582 | mod_timer(&fip->timer, fip->ctlr_ka_time); | |
1583 | } | |
ba9cd5d0 | 1584 | return best; |
97c8389d JE |
1585 | } |
1586 | ||
794d98e7 JE |
1587 | /** |
1588 | * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF | |
1589 | * @fip: The FCoE controller | |
1590 | * | |
1591 | * Returns non-zero error if it could not be sent. | |
1592 | * | |
1593 | * Called with ctlr_mutex and ctlr_lock held. | |
1594 | * Caller must verify that fip->sel_fcf is not NULL. | |
1595 | */ | |
1596 | static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip) | |
1597 | { | |
1598 | struct sk_buff *skb; | |
1599 | struct sk_buff *skb_orig; | |
1600 | struct fc_frame_header *fh; | |
1601 | int error; | |
1602 | ||
1603 | skb_orig = fip->flogi_req; | |
1604 | if (!skb_orig) | |
1605 | return -EINVAL; | |
1606 | ||
1607 | /* | |
1608 | * Clone and send the FLOGI request. If clone fails, use original. | |
1609 | */ | |
1610 | skb = skb_clone(skb_orig, GFP_ATOMIC); | |
1611 | if (!skb) { | |
1612 | skb = skb_orig; | |
1613 | fip->flogi_req = NULL; | |
1614 | } | |
1615 | fh = (struct fc_frame_header *)skb->data; | |
1616 | error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb, | |
1617 | ntoh24(fh->fh_d_id)); | |
1618 | if (error) { | |
1619 | kfree_skb(skb); | |
1620 | return error; | |
1621 | } | |
1622 | fip->send(fip, skb); | |
1623 | fip->sel_fcf->flogi_sent = 1; | |
1624 | return 0; | |
1625 | } | |
1626 | ||
1627 | /** | |
1628 | * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible | |
1629 | * @fip: The FCoE controller | |
1630 | * | |
1631 | * Returns non-zero error code if there's no FLOGI request to retry or | |
1632 | * no alternate FCF available. | |
1633 | */ | |
1634 | static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip) | |
1635 | { | |
1636 | struct fcoe_fcf *fcf; | |
1637 | int error; | |
1638 | ||
1639 | mutex_lock(&fip->ctlr_mutex); | |
1640 | spin_lock_bh(&fip->ctlr_lock); | |
1641 | LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n"); | |
ba9cd5d0 | 1642 | fcf = fcoe_ctlr_select(fip); |
794d98e7 JE |
1643 | if (!fcf || fcf->flogi_sent) { |
1644 | kfree_skb(fip->flogi_req); | |
1645 | fip->flogi_req = NULL; | |
1646 | error = -ENOENT; | |
1647 | } else { | |
1648 | fcoe_ctlr_solicit(fip, NULL); | |
1649 | error = fcoe_ctlr_flogi_send_locked(fip); | |
1650 | } | |
1651 | spin_unlock_bh(&fip->ctlr_lock); | |
1652 | mutex_unlock(&fip->ctlr_mutex); | |
1653 | return error; | |
1654 | } | |
1655 | ||
1656 | ||
1657 | /** | |
1658 | * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI. | |
1659 | * @fip: The FCoE controller that timed out | |
1660 | * | |
1661 | * Done here because fcoe_ctlr_els_send() can't get mutex. | |
1662 | * | |
1663 | * Called with ctlr_mutex held. The caller must not hold ctlr_lock. | |
1664 | */ | |
1665 | static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip) | |
1666 | { | |
1667 | struct fcoe_fcf *fcf; | |
1668 | ||
1669 | spin_lock_bh(&fip->ctlr_lock); | |
1670 | fcf = fip->sel_fcf; | |
1671 | if (!fcf || !fip->flogi_req_send) | |
1672 | goto unlock; | |
1673 | ||
1674 | LIBFCOE_FIP_DBG(fip, "sending FLOGI\n"); | |
1675 | ||
1676 | /* | |
1677 | * If this FLOGI is being sent due to a timeout retry | |
1678 | * to the same FCF as before, select a different FCF if possible. | |
1679 | */ | |
1680 | if (fcf->flogi_sent) { | |
1681 | LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n"); | |
ba9cd5d0 | 1682 | fcf = fcoe_ctlr_select(fip); |
794d98e7 JE |
1683 | if (!fcf || fcf->flogi_sent) { |
1684 | LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n"); | |
1685 | list_for_each_entry(fcf, &fip->fcfs, list) | |
1686 | fcf->flogi_sent = 0; | |
ba9cd5d0 | 1687 | fcf = fcoe_ctlr_select(fip); |
794d98e7 JE |
1688 | } |
1689 | } | |
1690 | if (fcf) { | |
1691 | fcoe_ctlr_flogi_send_locked(fip); | |
1692 | fip->flogi_req_send = 0; | |
1693 | } else /* XXX */ | |
1694 | LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n"); | |
1695 | unlock: | |
1696 | spin_unlock_bh(&fip->ctlr_lock); | |
1697 | } | |
1698 | ||
97c8389d | 1699 | /** |
70b51aab RL |
1700 | * fcoe_ctlr_timeout() - FIP timeout handler |
1701 | * @arg: The FCoE controller that timed out | |
97c8389d JE |
1702 | */ |
1703 | static void fcoe_ctlr_timeout(unsigned long arg) | |
1704 | { | |
1705 | struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg; | |
fdb068c6 JE |
1706 | |
1707 | schedule_work(&fip->timer_work); | |
1708 | } | |
1709 | ||
1710 | /** | |
1711 | * fcoe_ctlr_timer_work() - Worker thread function for timer work | |
1712 | * @work: Handle to a FCoE controller | |
1713 | * | |
1714 | * Ages FCFs. Triggers FCF selection if possible. | |
1715 | * Sends keep-alives and resets. | |
1716 | */ | |
1717 | static void fcoe_ctlr_timer_work(struct work_struct *work) | |
1718 | { | |
1719 | struct fcoe_ctlr *fip; | |
1720 | struct fc_lport *vport; | |
1721 | u8 *mac; | |
1722 | u8 reset = 0; | |
1723 | u8 send_ctlr_ka = 0; | |
1724 | u8 send_port_ka = 0; | |
97c8389d JE |
1725 | struct fcoe_fcf *sel; |
1726 | struct fcoe_fcf *fcf; | |
8690cb83 | 1727 | unsigned long next_timer; |
97c8389d | 1728 | |
fdb068c6 | 1729 | fip = container_of(work, struct fcoe_ctlr, timer_work); |
e10f8c66 JE |
1730 | if (fip->mode == FIP_MODE_VN2VN) |
1731 | return fcoe_ctlr_vn_timeout(fip); | |
fdb068c6 | 1732 | mutex_lock(&fip->ctlr_mutex); |
97c8389d | 1733 | if (fip->state == FIP_ST_DISABLED) { |
fdb068c6 | 1734 | mutex_unlock(&fip->ctlr_mutex); |
97c8389d JE |
1735 | return; |
1736 | } | |
1737 | ||
1738 | fcf = fip->sel_fcf; | |
8690cb83 | 1739 | next_timer = fcoe_ctlr_age_fcfs(fip); |
97c8389d JE |
1740 | |
1741 | sel = fip->sel_fcf; | |
8690cb83 JE |
1742 | if (!sel && fip->sel_time) { |
1743 | if (time_after_eq(jiffies, fip->sel_time)) { | |
ba9cd5d0 | 1744 | sel = fcoe_ctlr_select(fip); |
8690cb83 JE |
1745 | fip->sel_time = 0; |
1746 | } else if (time_after(next_timer, fip->sel_time)) | |
1747 | next_timer = fip->sel_time; | |
97c8389d JE |
1748 | } |
1749 | ||
794d98e7 JE |
1750 | if (sel && fip->flogi_req_send) |
1751 | fcoe_ctlr_flogi_send(fip); | |
1752 | else if (!sel && fcf) | |
1753 | reset = 1; | |
97c8389d | 1754 | |
8cdffdcc | 1755 | if (sel && !sel->fd_flags) { |
97c8389d JE |
1756 | if (time_after_eq(jiffies, fip->ctlr_ka_time)) { |
1757 | fip->ctlr_ka_time = jiffies + sel->fka_period; | |
fdb068c6 | 1758 | send_ctlr_ka = 1; |
97c8389d JE |
1759 | } |
1760 | if (time_after(next_timer, fip->ctlr_ka_time)) | |
1761 | next_timer = fip->ctlr_ka_time; | |
1762 | ||
1763 | if (time_after_eq(jiffies, fip->port_ka_time)) { | |
f47dd855 | 1764 | fip->port_ka_time = jiffies + |
70b51aab | 1765 | msecs_to_jiffies(FIP_VN_KA_PERIOD); |
fdb068c6 | 1766 | send_port_ka = 1; |
97c8389d JE |
1767 | } |
1768 | if (time_after(next_timer, fip->port_ka_time)) | |
1769 | next_timer = fip->port_ka_time; | |
97c8389d | 1770 | } |
8690cb83 JE |
1771 | if (!list_empty(&fip->fcfs)) |
1772 | mod_timer(&fip->timer, next_timer); | |
fdb068c6 | 1773 | mutex_unlock(&fip->ctlr_mutex); |
97c8389d | 1774 | |
516a6486 | 1775 | if (reset) { |
dd42dac4 | 1776 | fc_lport_reset(fip->lp); |
516a6486 BPG |
1777 | /* restart things with a solicitation */ |
1778 | fcoe_ctlr_solicit(fip, NULL); | |
1779 | } | |
11b56188 | 1780 | |
fdb068c6 | 1781 | if (send_ctlr_ka) |
11b56188 | 1782 | fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr); |
fdb068c6 JE |
1783 | |
1784 | if (send_port_ka) { | |
11b56188 CL |
1785 | mutex_lock(&fip->lp->lp_mutex); |
1786 | mac = fip->get_src_addr(fip->lp); | |
1787 | fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac); | |
1788 | list_for_each_entry(vport, &fip->lp->vports, list) { | |
1789 | mac = fip->get_src_addr(vport); | |
1790 | fcoe_ctlr_send_keep_alive(fip, vport, 1, mac); | |
1791 | } | |
1792 | mutex_unlock(&fip->lp->lp_mutex); | |
1793 | } | |
97c8389d JE |
1794 | } |
1795 | ||
1796 | /** | |
70b51aab RL |
1797 | * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames |
1798 | * @recv_work: Handle to a FCoE controller | |
97c8389d JE |
1799 | */ |
1800 | static void fcoe_ctlr_recv_work(struct work_struct *recv_work) | |
1801 | { | |
1802 | struct fcoe_ctlr *fip; | |
1803 | struct sk_buff *skb; | |
1804 | ||
1805 | fip = container_of(recv_work, struct fcoe_ctlr, recv_work); | |
1f4aed81 | 1806 | while ((skb = skb_dequeue(&fip->fip_recv_list))) |
97c8389d | 1807 | fcoe_ctlr_recv_handler(fip, skb); |
97c8389d JE |
1808 | } |
1809 | ||
1810 | /** | |
386309ce | 1811 | * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response |
70b51aab RL |
1812 | * @fip: The FCoE controller |
1813 | * @fp: The FC frame to snoop | |
97c8389d JE |
1814 | * |
1815 | * Snoop potential response to FLOGI or even incoming FLOGI. | |
1816 | * | |
1817 | * The caller has checked that we are waiting for login as indicated | |
1818 | * by fip->flogi_oxid != FC_XID_UNKNOWN. | |
1819 | * | |
1820 | * The caller is responsible for freeing the frame. | |
386309ce | 1821 | * Fill in the granted_mac address. |
97c8389d JE |
1822 | * |
1823 | * Return non-zero if the frame should not be delivered to libfc. | |
1824 | */ | |
11b56188 | 1825 | int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport, |
386309ce | 1826 | struct fc_frame *fp) |
97c8389d JE |
1827 | { |
1828 | struct fc_frame_header *fh; | |
1829 | u8 op; | |
386309ce | 1830 | u8 *sa; |
97c8389d | 1831 | |
386309ce | 1832 | sa = eth_hdr(&fp->skb)->h_source; |
97c8389d JE |
1833 | fh = fc_frame_header_get(fp); |
1834 | if (fh->fh_type != FC_TYPE_ELS) | |
1835 | return 0; | |
1836 | ||
1837 | op = fc_frame_payload_op(fp); | |
1838 | if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP && | |
1839 | fip->flogi_oxid == ntohs(fh->fh_ox_id)) { | |
1840 | ||
fdb068c6 | 1841 | mutex_lock(&fip->ctlr_mutex); |
97c8389d | 1842 | if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) { |
fdb068c6 | 1843 | mutex_unlock(&fip->ctlr_mutex); |
97c8389d JE |
1844 | return -EINVAL; |
1845 | } | |
9b651da9 | 1846 | fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP); |
0f51c2e5 JE |
1847 | LIBFCOE_FIP_DBG(fip, |
1848 | "received FLOGI LS_ACC using non-FIP mode\n"); | |
97c8389d JE |
1849 | |
1850 | /* | |
1851 | * FLOGI accepted. | |
1852 | * If the src mac addr is FC_OUI-based, then we mark the | |
1853 | * address_mode flag to use FC_OUI-based Ethernet DA. | |
1854 | * Otherwise we use the FCoE gateway addr | |
1855 | */ | |
1856 | if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) { | |
cd229e42 | 1857 | fcoe_ctlr_map_dest(fip); |
97c8389d JE |
1858 | } else { |
1859 | memcpy(fip->dest_addr, sa, ETH_ALEN); | |
1860 | fip->map_dest = 0; | |
1861 | } | |
1862 | fip->flogi_oxid = FC_XID_UNKNOWN; | |
fdb068c6 | 1863 | mutex_unlock(&fip->ctlr_mutex); |
386309ce | 1864 | fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id); |
97c8389d JE |
1865 | } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) { |
1866 | /* | |
1867 | * Save source MAC for point-to-point responses. | |
1868 | */ | |
fdb068c6 | 1869 | mutex_lock(&fip->ctlr_mutex); |
97c8389d JE |
1870 | if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) { |
1871 | memcpy(fip->dest_addr, sa, ETH_ALEN); | |
1872 | fip->map_dest = 0; | |
e49bf614 JE |
1873 | if (fip->state == FIP_ST_AUTO) |
1874 | LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. " | |
1875 | "Setting non-FIP mode\n"); | |
9b651da9 | 1876 | fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP); |
97c8389d | 1877 | } |
fdb068c6 | 1878 | mutex_unlock(&fip->ctlr_mutex); |
97c8389d JE |
1879 | } |
1880 | return 0; | |
1881 | } | |
1882 | EXPORT_SYMBOL(fcoe_ctlr_recv_flogi); | |
1883 | ||
5e80f7f7 | 1884 | /** |
70b51aab RL |
1885 | * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN |
1886 | * @mac: The MAC address to convert | |
1887 | * @scheme: The scheme to use when converting | |
1888 | * @port: The port indicator for converting | |
5e80f7f7 VD |
1889 | * |
1890 | * Returns: u64 fc world wide name | |
1891 | */ | |
1892 | u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN], | |
1893 | unsigned int scheme, unsigned int port) | |
1894 | { | |
1895 | u64 wwn; | |
1896 | u64 host_mac; | |
1897 | ||
1898 | /* The MAC is in NO, so flip only the low 48 bits */ | |
1899 | host_mac = ((u64) mac[0] << 40) | | |
1900 | ((u64) mac[1] << 32) | | |
1901 | ((u64) mac[2] << 24) | | |
1902 | ((u64) mac[3] << 16) | | |
1903 | ((u64) mac[4] << 8) | | |
1904 | (u64) mac[5]; | |
1905 | ||
1906 | WARN_ON(host_mac >= (1ULL << 48)); | |
1907 | wwn = host_mac | ((u64) scheme << 60); | |
1908 | switch (scheme) { | |
1909 | case 1: | |
1910 | WARN_ON(port != 0); | |
1911 | break; | |
1912 | case 2: | |
1913 | WARN_ON(port >= 0xfff); | |
1914 | wwn |= (u64) port << 48; | |
1915 | break; | |
1916 | default: | |
1917 | WARN_ON(1); | |
1918 | break; | |
1919 | } | |
1920 | ||
1921 | return wwn; | |
1922 | } | |
1923 | EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac); | |
1924 | ||
e10f8c66 JE |
1925 | /** |
1926 | * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv | |
1927 | * @rdata: libfc remote port | |
1928 | */ | |
1929 | static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata) | |
1930 | { | |
1931 | return (struct fcoe_rport *)(rdata + 1); | |
1932 | } | |
1933 | ||
1934 | /** | |
1935 | * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply. | |
1936 | * @fip: The FCoE controller | |
1937 | * @sub: sub-opcode for probe request, reply, or advertisement. | |
1938 | * @dest: The destination Ethernet MAC address | |
1939 | * @min_len: minimum size of the Ethernet payload to be sent | |
1940 | */ | |
1941 | static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip, | |
1942 | enum fip_vn2vn_subcode sub, | |
1943 | const u8 *dest, size_t min_len) | |
1944 | { | |
1945 | struct sk_buff *skb; | |
1946 | struct fip_frame { | |
1947 | struct ethhdr eth; | |
1948 | struct fip_header fip; | |
1949 | struct fip_mac_desc mac; | |
1950 | struct fip_wwn_desc wwnn; | |
1951 | struct fip_vn_desc vn; | |
0095a921 | 1952 | } __packed * frame; |
e10f8c66 JE |
1953 | struct fip_fc4_feat *ff; |
1954 | struct fip_size_desc *size; | |
1955 | u32 fcp_feat; | |
1956 | size_t len; | |
1957 | size_t dlen; | |
1958 | ||
1959 | len = sizeof(*frame); | |
1960 | dlen = 0; | |
1961 | if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) { | |
1962 | dlen = sizeof(struct fip_fc4_feat) + | |
1963 | sizeof(struct fip_size_desc); | |
1964 | len += dlen; | |
1965 | } | |
1966 | dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn); | |
1967 | len = max(len, min_len + sizeof(struct ethhdr)); | |
1968 | ||
1969 | skb = dev_alloc_skb(len); | |
1970 | if (!skb) | |
1971 | return; | |
1972 | ||
1973 | frame = (struct fip_frame *)skb->data; | |
1974 | memset(frame, 0, len); | |
1975 | memcpy(frame->eth.h_dest, dest, ETH_ALEN); | |
d227f029 YZ |
1976 | |
1977 | if (sub == FIP_SC_VN_BEACON) { | |
1978 | hton24(frame->eth.h_source, FIP_VN_FC_MAP); | |
1979 | hton24(frame->eth.h_source + 3, fip->port_id); | |
1980 | } else { | |
1981 | memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN); | |
1982 | } | |
e10f8c66 JE |
1983 | frame->eth.h_proto = htons(ETH_P_FIP); |
1984 | ||
1985 | frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER); | |
1986 | frame->fip.fip_op = htons(FIP_OP_VN2VN); | |
1987 | frame->fip.fip_subcode = sub; | |
1988 | frame->fip.fip_dl_len = htons(dlen / FIP_BPW); | |
1989 | ||
1990 | frame->mac.fd_desc.fip_dtype = FIP_DT_MAC; | |
1991 | frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW; | |
1992 | memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN); | |
1993 | ||
1994 | frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME; | |
1995 | frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW; | |
1996 | put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn); | |
1997 | ||
1998 | frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID; | |
1999 | frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW; | |
2000 | hton24(frame->vn.fd_mac, FIP_VN_FC_MAP); | |
2001 | hton24(frame->vn.fd_mac + 3, fip->port_id); | |
2002 | hton24(frame->vn.fd_fc_id, fip->port_id); | |
2003 | put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn); | |
2004 | ||
2005 | /* | |
2006 | * For claims, add FC-4 features. | |
2007 | * TBD: Add interface to get fc-4 types and features from libfc. | |
2008 | */ | |
2009 | if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) { | |
2010 | ff = (struct fip_fc4_feat *)(frame + 1); | |
2011 | ff->fd_desc.fip_dtype = FIP_DT_FC4F; | |
2012 | ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW; | |
2013 | ff->fd_fts = fip->lp->fcts; | |
2014 | ||
2015 | fcp_feat = 0; | |
2016 | if (fip->lp->service_params & FCP_SPPF_INIT_FCN) | |
2017 | fcp_feat |= FCP_FEAT_INIT; | |
2018 | if (fip->lp->service_params & FCP_SPPF_TARG_FCN) | |
2019 | fcp_feat |= FCP_FEAT_TARG; | |
2020 | fcp_feat <<= (FC_TYPE_FCP * 4) % 32; | |
2021 | ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat); | |
2022 | ||
2023 | size = (struct fip_size_desc *)(ff + 1); | |
2024 | size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE; | |
2025 | size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW; | |
2026 | size->fd_size = htons(fcoe_ctlr_fcoe_size(fip)); | |
2027 | } | |
2028 | ||
2029 | skb_put(skb, len); | |
2030 | skb->protocol = htons(ETH_P_FIP); | |
6f6c2aa3 | 2031 | skb->priority = fip->priority; |
e10f8c66 JE |
2032 | skb_reset_mac_header(skb); |
2033 | skb_reset_network_header(skb); | |
2034 | ||
2035 | fip->send(fip, skb); | |
2036 | } | |
2037 | ||
2038 | /** | |
2039 | * fcoe_ctlr_vn_rport_callback - Event handler for rport events. | |
2040 | * @lport: The lport which is receiving the event | |
2041 | * @rdata: remote port private data | |
25985edc | 2042 | * @event: The event that occurred |
e10f8c66 JE |
2043 | * |
2044 | * Locking Note: The rport lock must not be held when calling this function. | |
2045 | */ | |
2046 | static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport, | |
2047 | struct fc_rport_priv *rdata, | |
2048 | enum fc_rport_event event) | |
2049 | { | |
2050 | struct fcoe_ctlr *fip = lport->disc.priv; | |
2051 | struct fcoe_rport *frport = fcoe_ctlr_rport(rdata); | |
2052 | ||
2053 | LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n", | |
2054 | rdata->ids.port_id, event); | |
2055 | ||
2056 | mutex_lock(&fip->ctlr_mutex); | |
2057 | switch (event) { | |
2058 | case RPORT_EV_READY: | |
2059 | frport->login_count = 0; | |
2060 | break; | |
2061 | case RPORT_EV_LOGO: | |
2062 | case RPORT_EV_FAILED: | |
2063 | case RPORT_EV_STOP: | |
2064 | frport->login_count++; | |
2065 | if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) { | |
2066 | LIBFCOE_FIP_DBG(fip, | |
2067 | "rport FLOGI limited port_id %6.6x\n", | |
2068 | rdata->ids.port_id); | |
2069 | lport->tt.rport_logoff(rdata); | |
2070 | } | |
2071 | break; | |
2072 | default: | |
2073 | break; | |
2074 | } | |
2075 | mutex_unlock(&fip->ctlr_mutex); | |
2076 | } | |
2077 | ||
2078 | static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = { | |
2079 | .event_callback = fcoe_ctlr_vn_rport_callback, | |
2080 | }; | |
2081 | ||
2082 | /** | |
2083 | * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode | |
2084 | * @fip: The FCoE controller | |
2085 | * | |
2086 | * Called with ctlr_mutex held. | |
2087 | */ | |
2088 | static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport) | |
2089 | { | |
2090 | mutex_lock(&lport->disc.disc_mutex); | |
2091 | lport->disc.disc_callback = NULL; | |
2092 | mutex_unlock(&lport->disc.disc_mutex); | |
2093 | } | |
2094 | ||
2095 | /** | |
2096 | * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode | |
2097 | * @fip: The FCoE controller | |
2098 | * | |
2099 | * Called through the local port template for discovery. | |
2100 | * Called without the ctlr_mutex held. | |
2101 | */ | |
2102 | static void fcoe_ctlr_disc_stop(struct fc_lport *lport) | |
2103 | { | |
2104 | struct fcoe_ctlr *fip = lport->disc.priv; | |
2105 | ||
2106 | mutex_lock(&fip->ctlr_mutex); | |
2107 | fcoe_ctlr_disc_stop_locked(lport); | |
2108 | mutex_unlock(&fip->ctlr_mutex); | |
2109 | } | |
2110 | ||
2111 | /** | |
2112 | * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode | |
2113 | * @fip: The FCoE controller | |
2114 | * | |
2115 | * Called through the local port template for discovery. | |
2116 | * Called without the ctlr_mutex held. | |
2117 | */ | |
2118 | static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport) | |
2119 | { | |
2120 | fcoe_ctlr_disc_stop(lport); | |
2121 | lport->tt.rport_flush_queue(); | |
2122 | synchronize_rcu(); | |
2123 | } | |
2124 | ||
2125 | /** | |
2126 | * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id | |
2127 | * @fip: The FCoE controller | |
2128 | * | |
2129 | * Called with fcoe_ctlr lock held. | |
2130 | */ | |
2131 | static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip) | |
2132 | { | |
2133 | unsigned long wait; | |
2134 | u32 port_id; | |
2135 | ||
2136 | fcoe_ctlr_disc_stop_locked(fip->lp); | |
2137 | ||
2138 | /* | |
2139 | * Get proposed port ID. | |
2140 | * If this is the first try after link up, use any previous port_id. | |
2141 | * If there was none, use the low bits of the port_name. | |
2142 | * On subsequent tries, get the next random one. | |
2143 | * Don't use reserved IDs, use another non-zero value, just as random. | |
2144 | */ | |
2145 | port_id = fip->port_id; | |
2146 | if (fip->probe_tries) | |
2147 | port_id = prandom32(&fip->rnd_state) & 0xffff; | |
2148 | else if (!port_id) | |
2149 | port_id = fip->lp->wwpn & 0xffff; | |
2150 | if (!port_id || port_id == 0xffff) | |
2151 | port_id = 1; | |
2152 | fip->port_id = port_id; | |
2153 | ||
2154 | if (fip->probe_tries < FIP_VN_RLIM_COUNT) { | |
2155 | fip->probe_tries++; | |
2156 | wait = random32() % FIP_VN_PROBE_WAIT; | |
2157 | } else | |
2158 | wait = FIP_VN_RLIM_INT; | |
2159 | mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait)); | |
2160 | fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START); | |
2161 | } | |
2162 | ||
2163 | /** | |
2164 | * fcoe_ctlr_vn_start() - Start in VN2VN mode | |
2165 | * @fip: The FCoE controller | |
2166 | * | |
2167 | * Called with fcoe_ctlr lock held. | |
2168 | */ | |
2169 | static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip) | |
2170 | { | |
2171 | fip->probe_tries = 0; | |
2172 | prandom32_seed(&fip->rnd_state, fip->lp->wwpn); | |
2173 | fcoe_ctlr_vn_restart(fip); | |
2174 | } | |
2175 | ||
2176 | /** | |
2177 | * fcoe_ctlr_vn_parse - parse probe request or response | |
2178 | * @fip: The FCoE controller | |
2179 | * @skb: incoming packet | |
2180 | * @rdata: buffer for resulting parsed VN entry plus fcoe_rport | |
2181 | * | |
2182 | * Returns non-zero error number on error. | |
2183 | * Does not consume the packet. | |
2184 | */ | |
2185 | static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip, | |
2186 | struct sk_buff *skb, | |
2187 | struct fc_rport_priv *rdata) | |
2188 | { | |
2189 | struct fip_header *fiph; | |
2190 | struct fip_desc *desc = NULL; | |
2191 | struct fip_mac_desc *macd = NULL; | |
2192 | struct fip_wwn_desc *wwn = NULL; | |
2193 | struct fip_vn_desc *vn = NULL; | |
2194 | struct fip_size_desc *size = NULL; | |
2195 | struct fcoe_rport *frport; | |
2196 | size_t rlen; | |
2197 | size_t dlen; | |
2198 | u32 desc_mask = 0; | |
2199 | u32 dtype; | |
2200 | u8 sub; | |
2201 | ||
2202 | memset(rdata, 0, sizeof(*rdata) + sizeof(*frport)); | |
2203 | frport = fcoe_ctlr_rport(rdata); | |
2204 | ||
2205 | fiph = (struct fip_header *)skb->data; | |
2206 | frport->flags = ntohs(fiph->fip_flags); | |
2207 | ||
2208 | sub = fiph->fip_subcode; | |
2209 | switch (sub) { | |
2210 | case FIP_SC_VN_PROBE_REQ: | |
2211 | case FIP_SC_VN_PROBE_REP: | |
2212 | case FIP_SC_VN_BEACON: | |
2213 | desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | | |
2214 | BIT(FIP_DT_VN_ID); | |
2215 | break; | |
2216 | case FIP_SC_VN_CLAIM_NOTIFY: | |
2217 | case FIP_SC_VN_CLAIM_REP: | |
2218 | desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | | |
2219 | BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) | | |
2220 | BIT(FIP_DT_FCOE_SIZE); | |
2221 | break; | |
2222 | default: | |
2223 | LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub); | |
2224 | return -EINVAL; | |
2225 | } | |
2226 | ||
2227 | rlen = ntohs(fiph->fip_dl_len) * 4; | |
2228 | if (rlen + sizeof(*fiph) > skb->len) | |
2229 | return -EINVAL; | |
2230 | ||
2231 | desc = (struct fip_desc *)(fiph + 1); | |
2232 | while (rlen > 0) { | |
2233 | dlen = desc->fip_dlen * FIP_BPW; | |
2234 | if (dlen < sizeof(*desc) || dlen > rlen) | |
2235 | return -EINVAL; | |
2236 | ||
2237 | dtype = desc->fip_dtype; | |
2238 | if (dtype < 32) { | |
2239 | if (!(desc_mask & BIT(dtype))) { | |
2240 | LIBFCOE_FIP_DBG(fip, | |
2241 | "unexpected or duplicated desc " | |
2242 | "desc type %u in " | |
2243 | "FIP VN2VN subtype %u\n", | |
2244 | dtype, sub); | |
2245 | return -EINVAL; | |
2246 | } | |
2247 | desc_mask &= ~BIT(dtype); | |
2248 | } | |
2249 | ||
2250 | switch (dtype) { | |
2251 | case FIP_DT_MAC: | |
2252 | if (dlen != sizeof(struct fip_mac_desc)) | |
2253 | goto len_err; | |
2254 | macd = (struct fip_mac_desc *)desc; | |
2255 | if (!is_valid_ether_addr(macd->fd_mac)) { | |
2256 | LIBFCOE_FIP_DBG(fip, | |
2257 | "Invalid MAC addr %pM in FIP VN2VN\n", | |
2258 | macd->fd_mac); | |
2259 | return -EINVAL; | |
2260 | } | |
2261 | memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN); | |
2262 | break; | |
2263 | case FIP_DT_NAME: | |
2264 | if (dlen != sizeof(struct fip_wwn_desc)) | |
2265 | goto len_err; | |
2266 | wwn = (struct fip_wwn_desc *)desc; | |
2267 | rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn); | |
2268 | break; | |
2269 | case FIP_DT_VN_ID: | |
2270 | if (dlen != sizeof(struct fip_vn_desc)) | |
2271 | goto len_err; | |
2272 | vn = (struct fip_vn_desc *)desc; | |
2273 | memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN); | |
2274 | rdata->ids.port_id = ntoh24(vn->fd_fc_id); | |
2275 | rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn); | |
2276 | break; | |
2277 | case FIP_DT_FC4F: | |
2278 | if (dlen != sizeof(struct fip_fc4_feat)) | |
2279 | goto len_err; | |
2280 | break; | |
2281 | case FIP_DT_FCOE_SIZE: | |
2282 | if (dlen != sizeof(struct fip_size_desc)) | |
2283 | goto len_err; | |
2284 | size = (struct fip_size_desc *)desc; | |
2285 | frport->fcoe_len = ntohs(size->fd_size); | |
2286 | break; | |
2287 | default: | |
2288 | LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x " | |
2289 | "in FIP probe\n", dtype); | |
2290 | /* standard says ignore unknown descriptors >= 128 */ | |
2291 | if (dtype < FIP_DT_VENDOR_BASE) | |
2292 | return -EINVAL; | |
2293 | break; | |
2294 | } | |
2295 | desc = (struct fip_desc *)((char *)desc + dlen); | |
2296 | rlen -= dlen; | |
2297 | } | |
2298 | return 0; | |
2299 | ||
2300 | len_err: | |
2301 | LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n", | |
2302 | dtype, dlen); | |
2303 | return -EINVAL; | |
2304 | } | |
2305 | ||
2306 | /** | |
2307 | * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification. | |
2308 | * @fip: The FCoE controller | |
2309 | * | |
2310 | * Called with ctlr_mutex held. | |
2311 | */ | |
2312 | static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip) | |
2313 | { | |
2314 | fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0); | |
2315 | fip->sol_time = jiffies; | |
2316 | } | |
2317 | ||
2318 | /** | |
2319 | * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request. | |
2320 | * @fip: The FCoE controller | |
2321 | * @rdata: parsed remote port with frport from the probe request | |
2322 | * | |
2323 | * Called with ctlr_mutex held. | |
2324 | */ | |
2325 | static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip, | |
2326 | struct fc_rport_priv *rdata) | |
2327 | { | |
2328 | struct fcoe_rport *frport = fcoe_ctlr_rport(rdata); | |
2329 | ||
2330 | if (rdata->ids.port_id != fip->port_id) | |
2331 | return; | |
2332 | ||
2333 | switch (fip->state) { | |
2334 | case FIP_ST_VNMP_CLAIM: | |
2335 | case FIP_ST_VNMP_UP: | |
2336 | fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP, | |
2337 | frport->enode_mac, 0); | |
2338 | break; | |
2339 | case FIP_ST_VNMP_PROBE1: | |
2340 | case FIP_ST_VNMP_PROBE2: | |
2341 | /* | |
2342 | * Decide whether to reply to the Probe. | |
2343 | * Our selected address is never a "recorded" one, so | |
2344 | * only reply if our WWPN is greater and the | |
2345 | * Probe's REC bit is not set. | |
2346 | * If we don't reply, we will change our address. | |
2347 | */ | |
2348 | if (fip->lp->wwpn > rdata->ids.port_name && | |
2349 | !(frport->flags & FIP_FL_REC_OR_P2P)) { | |
2350 | fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP, | |
2351 | frport->enode_mac, 0); | |
2352 | break; | |
2353 | } | |
2354 | /* fall through */ | |
2355 | case FIP_ST_VNMP_START: | |
2356 | fcoe_ctlr_vn_restart(fip); | |
2357 | break; | |
2358 | default: | |
2359 | break; | |
2360 | } | |
2361 | } | |
2362 | ||
2363 | /** | |
2364 | * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply. | |
2365 | * @fip: The FCoE controller | |
2366 | * @rdata: parsed remote port with frport from the probe request | |
2367 | * | |
2368 | * Called with ctlr_mutex held. | |
2369 | */ | |
2370 | static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip, | |
2371 | struct fc_rport_priv *rdata) | |
2372 | { | |
2373 | if (rdata->ids.port_id != fip->port_id) | |
2374 | return; | |
2375 | switch (fip->state) { | |
2376 | case FIP_ST_VNMP_START: | |
2377 | case FIP_ST_VNMP_PROBE1: | |
2378 | case FIP_ST_VNMP_PROBE2: | |
2379 | case FIP_ST_VNMP_CLAIM: | |
2380 | fcoe_ctlr_vn_restart(fip); | |
2381 | break; | |
2382 | case FIP_ST_VNMP_UP: | |
2383 | fcoe_ctlr_vn_send_claim(fip); | |
2384 | break; | |
2385 | default: | |
2386 | break; | |
2387 | } | |
2388 | } | |
2389 | ||
2390 | /** | |
2391 | * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply. | |
2392 | * @fip: The FCoE controller | |
2393 | * @new: newly-parsed remote port with frport as a template for new rdata | |
2394 | * | |
2395 | * Called with ctlr_mutex held. | |
2396 | */ | |
2397 | static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new) | |
2398 | { | |
2399 | struct fc_lport *lport = fip->lp; | |
2400 | struct fc_rport_priv *rdata; | |
2401 | struct fc_rport_identifiers *ids; | |
2402 | struct fcoe_rport *frport; | |
2403 | u32 port_id; | |
2404 | ||
2405 | port_id = new->ids.port_id; | |
2406 | if (port_id == fip->port_id) | |
2407 | return; | |
2408 | ||
2409 | mutex_lock(&lport->disc.disc_mutex); | |
2410 | rdata = lport->tt.rport_create(lport, port_id); | |
2411 | if (!rdata) { | |
2412 | mutex_unlock(&lport->disc.disc_mutex); | |
2413 | return; | |
2414 | } | |
2415 | ||
2416 | rdata->ops = &fcoe_ctlr_vn_rport_ops; | |
2417 | rdata->disc_id = lport->disc.disc_id; | |
2418 | ||
2419 | ids = &rdata->ids; | |
2420 | if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) || | |
2421 | (ids->node_name != -1 && ids->node_name != new->ids.node_name)) | |
2422 | lport->tt.rport_logoff(rdata); | |
2423 | ids->port_name = new->ids.port_name; | |
2424 | ids->node_name = new->ids.node_name; | |
2425 | mutex_unlock(&lport->disc.disc_mutex); | |
2426 | ||
2427 | frport = fcoe_ctlr_rport(rdata); | |
2428 | LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n", | |
2429 | port_id, frport->fcoe_len ? "old" : "new"); | |
2430 | *frport = *fcoe_ctlr_rport(new); | |
2431 | frport->time = 0; | |
2432 | } | |
2433 | ||
2434 | /** | |
2435 | * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address | |
2436 | * @fip: The FCoE controller | |
2437 | * @port_id: The port_id of the remote VN_node | |
2438 | * @mac: buffer which will hold the VN_NODE destination MAC address, if found. | |
2439 | * | |
2440 | * Returns non-zero error if no remote port found. | |
2441 | */ | |
2442 | static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac) | |
2443 | { | |
2444 | struct fc_lport *lport = fip->lp; | |
2445 | struct fc_rport_priv *rdata; | |
2446 | struct fcoe_rport *frport; | |
2447 | int ret = -1; | |
2448 | ||
2449 | rcu_read_lock(); | |
2450 | rdata = lport->tt.rport_lookup(lport, port_id); | |
2451 | if (rdata) { | |
2452 | frport = fcoe_ctlr_rport(rdata); | |
2453 | memcpy(mac, frport->enode_mac, ETH_ALEN); | |
2454 | ret = 0; | |
2455 | } | |
2456 | rcu_read_unlock(); | |
2457 | return ret; | |
2458 | } | |
2459 | ||
2460 | /** | |
2461 | * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification | |
2462 | * @fip: The FCoE controller | |
2463 | * @new: newly-parsed remote port with frport as a template for new rdata | |
2464 | * | |
2465 | * Called with ctlr_mutex held. | |
2466 | */ | |
2467 | static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip, | |
2468 | struct fc_rport_priv *new) | |
2469 | { | |
2470 | struct fcoe_rport *frport = fcoe_ctlr_rport(new); | |
2471 | ||
2472 | if (frport->flags & FIP_FL_REC_OR_P2P) { | |
2473 | fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0); | |
2474 | return; | |
2475 | } | |
2476 | switch (fip->state) { | |
2477 | case FIP_ST_VNMP_START: | |
2478 | case FIP_ST_VNMP_PROBE1: | |
2479 | case FIP_ST_VNMP_PROBE2: | |
2480 | if (new->ids.port_id == fip->port_id) | |
2481 | fcoe_ctlr_vn_restart(fip); | |
2482 | break; | |
2483 | case FIP_ST_VNMP_CLAIM: | |
2484 | case FIP_ST_VNMP_UP: | |
2485 | if (new->ids.port_id == fip->port_id) { | |
2486 | if (new->ids.port_name > fip->lp->wwpn) { | |
2487 | fcoe_ctlr_vn_restart(fip); | |
2488 | break; | |
2489 | } | |
2490 | fcoe_ctlr_vn_send_claim(fip); | |
2491 | break; | |
2492 | } | |
2493 | fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac, | |
2494 | min((u32)frport->fcoe_len, | |
2495 | fcoe_ctlr_fcoe_size(fip))); | |
2496 | fcoe_ctlr_vn_add(fip, new); | |
2497 | break; | |
2498 | default: | |
2499 | break; | |
2500 | } | |
2501 | } | |
2502 | ||
2503 | /** | |
2504 | * fcoe_ctlr_vn_claim_resp() - handle received Claim Response | |
2505 | * @fip: The FCoE controller that received the frame | |
2506 | * @new: newly-parsed remote port with frport from the Claim Response | |
2507 | * | |
2508 | * Called with ctlr_mutex held. | |
2509 | */ | |
2510 | static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip, | |
2511 | struct fc_rport_priv *new) | |
2512 | { | |
2513 | LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n", | |
2514 | new->ids.port_id, fcoe_ctlr_state(fip->state)); | |
2515 | if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM) | |
2516 | fcoe_ctlr_vn_add(fip, new); | |
2517 | } | |
2518 | ||
2519 | /** | |
2520 | * fcoe_ctlr_vn_beacon() - handle received beacon. | |
2521 | * @fip: The FCoE controller that received the frame | |
2522 | * @new: newly-parsed remote port with frport from the Beacon | |
2523 | * | |
2524 | * Called with ctlr_mutex held. | |
2525 | */ | |
2526 | static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip, | |
2527 | struct fc_rport_priv *new) | |
2528 | { | |
2529 | struct fc_lport *lport = fip->lp; | |
2530 | struct fc_rport_priv *rdata; | |
2531 | struct fcoe_rport *frport; | |
2532 | ||
2533 | frport = fcoe_ctlr_rport(new); | |
2534 | if (frport->flags & FIP_FL_REC_OR_P2P) { | |
2535 | fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0); | |
2536 | return; | |
2537 | } | |
2538 | mutex_lock(&lport->disc.disc_mutex); | |
2539 | rdata = lport->tt.rport_lookup(lport, new->ids.port_id); | |
2540 | if (rdata) | |
2541 | kref_get(&rdata->kref); | |
2542 | mutex_unlock(&lport->disc.disc_mutex); | |
2543 | if (rdata) { | |
2544 | if (rdata->ids.node_name == new->ids.node_name && | |
2545 | rdata->ids.port_name == new->ids.port_name) { | |
2546 | frport = fcoe_ctlr_rport(rdata); | |
2547 | if (!frport->time && fip->state == FIP_ST_VNMP_UP) | |
2548 | lport->tt.rport_login(rdata); | |
2549 | frport->time = jiffies; | |
2550 | } | |
2551 | kref_put(&rdata->kref, lport->tt.rport_destroy); | |
2552 | return; | |
2553 | } | |
2554 | if (fip->state != FIP_ST_VNMP_UP) | |
2555 | return; | |
2556 | ||
2557 | /* | |
2558 | * Beacon from a new neighbor. | |
2559 | * Send a claim notify if one hasn't been sent recently. | |
2560 | * Don't add the neighbor yet. | |
2561 | */ | |
2562 | LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n", | |
2563 | new->ids.port_id); | |
2564 | if (time_after(jiffies, | |
2565 | fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT))) | |
2566 | fcoe_ctlr_vn_send_claim(fip); | |
2567 | } | |
2568 | ||
2569 | /** | |
2570 | * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons | |
2571 | * @fip: The FCoE controller | |
2572 | * | |
2573 | * Called with ctlr_mutex held. | |
2574 | * Called only in state FIP_ST_VNMP_UP. | |
2575 | * Returns the soonest time for next age-out or a time far in the future. | |
2576 | */ | |
2577 | static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip) | |
2578 | { | |
2579 | struct fc_lport *lport = fip->lp; | |
2580 | struct fc_rport_priv *rdata; | |
2581 | struct fcoe_rport *frport; | |
2582 | unsigned long next_time; | |
2583 | unsigned long deadline; | |
2584 | ||
2585 | next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10); | |
2586 | mutex_lock(&lport->disc.disc_mutex); | |
2587 | list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) { | |
2588 | frport = fcoe_ctlr_rport(rdata); | |
2589 | if (!frport->time) | |
2590 | continue; | |
2591 | deadline = frport->time + | |
2592 | msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10); | |
2593 | if (time_after_eq(jiffies, deadline)) { | |
2594 | frport->time = 0; | |
2595 | LIBFCOE_FIP_DBG(fip, | |
2596 | "port %16.16llx fc_id %6.6x beacon expired\n", | |
2597 | rdata->ids.port_name, rdata->ids.port_id); | |
2598 | lport->tt.rport_logoff(rdata); | |
2599 | } else if (time_before(deadline, next_time)) | |
2600 | next_time = deadline; | |
2601 | } | |
2602 | mutex_unlock(&lport->disc.disc_mutex); | |
2603 | return next_time; | |
2604 | } | |
2605 | ||
2606 | /** | |
2607 | * fcoe_ctlr_vn_recv() - Receive a FIP frame | |
2608 | * @fip: The FCoE controller that received the frame | |
2609 | * @skb: The received FIP frame | |
2610 | * | |
2611 | * Returns non-zero if the frame is dropped. | |
2612 | * Always consumes the frame. | |
2613 | */ | |
2614 | static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb) | |
2615 | { | |
2616 | struct fip_header *fiph; | |
2617 | enum fip_vn2vn_subcode sub; | |
2dc02ee5 | 2618 | struct { |
e10f8c66 JE |
2619 | struct fc_rport_priv rdata; |
2620 | struct fcoe_rport frport; | |
2621 | } buf; | |
2622 | int rc; | |
2623 | ||
2624 | fiph = (struct fip_header *)skb->data; | |
2625 | sub = fiph->fip_subcode; | |
2626 | ||
2627 | rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata); | |
2628 | if (rc) { | |
2629 | LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc); | |
2630 | goto drop; | |
2631 | } | |
2632 | ||
2633 | mutex_lock(&fip->ctlr_mutex); | |
2634 | switch (sub) { | |
2635 | case FIP_SC_VN_PROBE_REQ: | |
2636 | fcoe_ctlr_vn_probe_req(fip, &buf.rdata); | |
2637 | break; | |
2638 | case FIP_SC_VN_PROBE_REP: | |
2639 | fcoe_ctlr_vn_probe_reply(fip, &buf.rdata); | |
2640 | break; | |
2641 | case FIP_SC_VN_CLAIM_NOTIFY: | |
2642 | fcoe_ctlr_vn_claim_notify(fip, &buf.rdata); | |
2643 | break; | |
2644 | case FIP_SC_VN_CLAIM_REP: | |
2645 | fcoe_ctlr_vn_claim_resp(fip, &buf.rdata); | |
2646 | break; | |
2647 | case FIP_SC_VN_BEACON: | |
2648 | fcoe_ctlr_vn_beacon(fip, &buf.rdata); | |
2649 | break; | |
2650 | default: | |
2651 | LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub); | |
2652 | rc = -1; | |
2653 | break; | |
2654 | } | |
2655 | mutex_unlock(&fip->ctlr_mutex); | |
2656 | drop: | |
2657 | kfree_skb(skb); | |
2658 | return rc; | |
2659 | } | |
2660 | ||
2661 | /** | |
2662 | * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode. | |
92261156 JE |
2663 | * @lport: The local port |
2664 | * @fp: The received frame | |
e10f8c66 JE |
2665 | * |
2666 | * This should never be called since we don't see RSCNs or other | |
2667 | * fabric-generated ELSes. | |
2668 | */ | |
92261156 | 2669 | static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp) |
e10f8c66 JE |
2670 | { |
2671 | struct fc_seq_els_data rjt_data; | |
2672 | ||
e10f8c66 JE |
2673 | rjt_data.reason = ELS_RJT_UNSUP; |
2674 | rjt_data.explan = ELS_EXPL_NONE; | |
92261156 | 2675 | lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data); |
e10f8c66 JE |
2676 | fc_frame_free(fp); |
2677 | } | |
2678 | ||
2679 | /** | |
2680 | * fcoe_ctlr_disc_recv - start discovery for VN2VN mode. | |
2681 | * @fip: The FCoE controller | |
2682 | * | |
2683 | * This sets a flag indicating that remote ports should be created | |
2684 | * and started for the peers we discover. We use the disc_callback | |
2685 | * pointer as that flag. Peers already discovered are created here. | |
2686 | * | |
2687 | * The lport lock is held during this call. The callback must be done | |
2688 | * later, without holding either the lport or discovery locks. | |
2689 | * The fcoe_ctlr lock may also be held during this call. | |
2690 | */ | |
2691 | static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *, | |
2692 | enum fc_disc_event), | |
2693 | struct fc_lport *lport) | |
2694 | { | |
2695 | struct fc_disc *disc = &lport->disc; | |
2696 | struct fcoe_ctlr *fip = disc->priv; | |
2697 | ||
2698 | mutex_lock(&disc->disc_mutex); | |
2699 | disc->disc_callback = callback; | |
2700 | disc->disc_id = (disc->disc_id + 2) | 1; | |
2701 | disc->pending = 1; | |
2702 | schedule_work(&fip->timer_work); | |
2703 | mutex_unlock(&disc->disc_mutex); | |
2704 | } | |
2705 | ||
2706 | /** | |
2707 | * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state. | |
2708 | * @fip: The FCoE controller | |
2709 | * | |
2710 | * Starts the FLOGI and PLOGI login process to each discovered rport for which | |
2711 | * we've received at least one beacon. | |
2712 | * Performs the discovery complete callback. | |
2713 | */ | |
2714 | static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip) | |
2715 | { | |
2716 | struct fc_lport *lport = fip->lp; | |
2717 | struct fc_disc *disc = &lport->disc; | |
2718 | struct fc_rport_priv *rdata; | |
2719 | struct fcoe_rport *frport; | |
2720 | void (*callback)(struct fc_lport *, enum fc_disc_event); | |
2721 | ||
2722 | mutex_lock(&disc->disc_mutex); | |
2723 | callback = disc->pending ? disc->disc_callback : NULL; | |
2724 | disc->pending = 0; | |
2725 | list_for_each_entry_rcu(rdata, &disc->rports, peers) { | |
2726 | frport = fcoe_ctlr_rport(rdata); | |
2727 | if (frport->time) | |
2728 | lport->tt.rport_login(rdata); | |
2729 | } | |
2730 | mutex_unlock(&disc->disc_mutex); | |
2731 | if (callback) | |
2732 | callback(lport, DISC_EV_SUCCESS); | |
2733 | } | |
2734 | ||
2735 | /** | |
2736 | * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode. | |
2737 | * @fip: The FCoE controller | |
2738 | */ | |
2739 | static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip) | |
2740 | { | |
2741 | unsigned long next_time; | |
2742 | u8 mac[ETH_ALEN]; | |
2743 | u32 new_port_id = 0; | |
2744 | ||
2745 | mutex_lock(&fip->ctlr_mutex); | |
2746 | switch (fip->state) { | |
2747 | case FIP_ST_VNMP_START: | |
2748 | fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1); | |
2749 | fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0); | |
2750 | next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT); | |
2751 | break; | |
2752 | case FIP_ST_VNMP_PROBE1: | |
2753 | fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2); | |
2754 | fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0); | |
2755 | next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT); | |
2756 | break; | |
2757 | case FIP_ST_VNMP_PROBE2: | |
2758 | fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM); | |
2759 | new_port_id = fip->port_id; | |
2760 | hton24(mac, FIP_VN_FC_MAP); | |
2761 | hton24(mac + 3, new_port_id); | |
cd229e42 | 2762 | fcoe_ctlr_map_dest(fip); |
e10f8c66 JE |
2763 | fip->update_mac(fip->lp, mac); |
2764 | fcoe_ctlr_vn_send_claim(fip); | |
2765 | next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT); | |
2766 | break; | |
2767 | case FIP_ST_VNMP_CLAIM: | |
2768 | /* | |
2769 | * This may be invoked either by starting discovery so don't | |
2770 | * go to the next state unless it's been long enough. | |
2771 | */ | |
2772 | next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT); | |
2773 | if (time_after_eq(jiffies, next_time)) { | |
2774 | fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP); | |
2775 | fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON, | |
2776 | fcoe_all_vn2vn, 0); | |
2777 | next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT); | |
2778 | fip->port_ka_time = next_time; | |
2779 | } | |
2780 | fcoe_ctlr_vn_disc(fip); | |
2781 | break; | |
2782 | case FIP_ST_VNMP_UP: | |
2783 | next_time = fcoe_ctlr_vn_age(fip); | |
2784 | if (time_after_eq(jiffies, fip->port_ka_time)) { | |
2785 | fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON, | |
2786 | fcoe_all_vn2vn, 0); | |
2787 | fip->port_ka_time = jiffies + | |
2788 | msecs_to_jiffies(FIP_VN_BEACON_INT + | |
2789 | (random32() % FIP_VN_BEACON_FUZZ)); | |
2790 | } | |
2791 | if (time_before(fip->port_ka_time, next_time)) | |
2792 | next_time = fip->port_ka_time; | |
2793 | break; | |
2794 | case FIP_ST_LINK_WAIT: | |
2795 | goto unlock; | |
2796 | default: | |
11aa9900 | 2797 | WARN(1, "unexpected state %d\n", fip->state); |
e10f8c66 JE |
2798 | goto unlock; |
2799 | } | |
2800 | mod_timer(&fip->timer, next_time); | |
2801 | unlock: | |
2802 | mutex_unlock(&fip->ctlr_mutex); | |
2803 | ||
2804 | /* If port ID is new, notify local port after dropping ctlr_mutex */ | |
2805 | if (new_port_id) | |
2806 | fc_lport_set_local_id(fip->lp, new_port_id); | |
2807 | } | |
2808 | ||
5e80f7f7 | 2809 | /** |
70b51aab | 2810 | * fcoe_libfc_config() - Sets up libfc related properties for local port |
8d55e507 RL |
2811 | * @lport: The local port to configure libfc for |
2812 | * @fip: The FCoE controller in use by the local port | |
2813 | * @tt: The libfc function template | |
e10f8c66 | 2814 | * @init_fcp: If non-zero, the FCP portion of libfc should be initialized |
5e80f7f7 VD |
2815 | * |
2816 | * Returns : 0 for success | |
2817 | */ | |
e10f8c66 JE |
2818 | int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip, |
2819 | const struct libfc_function_template *tt, int init_fcp) | |
5e80f7f7 VD |
2820 | { |
2821 | /* Set the function pointers set by the LLDD */ | |
70b51aab | 2822 | memcpy(&lport->tt, tt, sizeof(*tt)); |
e10f8c66 | 2823 | if (init_fcp && fc_fcp_init(lport)) |
5e80f7f7 | 2824 | return -ENOMEM; |
70b51aab RL |
2825 | fc_exch_init(lport); |
2826 | fc_elsct_init(lport); | |
2827 | fc_lport_init(lport); | |
e10f8c66 JE |
2828 | if (fip->mode == FIP_MODE_VN2VN) |
2829 | lport->rport_priv_size = sizeof(struct fcoe_rport); | |
70b51aab | 2830 | fc_rport_init(lport); |
e10f8c66 JE |
2831 | if (fip->mode == FIP_MODE_VN2VN) { |
2832 | lport->point_to_multipoint = 1; | |
2833 | lport->tt.disc_recv_req = fcoe_ctlr_disc_recv; | |
2834 | lport->tt.disc_start = fcoe_ctlr_disc_start; | |
2835 | lport->tt.disc_stop = fcoe_ctlr_disc_stop; | |
2836 | lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final; | |
2837 | mutex_init(&lport->disc.disc_mutex); | |
2838 | INIT_LIST_HEAD(&lport->disc.rports); | |
2839 | lport->disc.priv = fip; | |
2840 | } else { | |
2841 | fc_disc_init(lport); | |
2842 | } | |
5e80f7f7 VD |
2843 | return 0; |
2844 | } | |
2845 | EXPORT_SYMBOL_GPL(fcoe_libfc_config); | |
8d55e507 RL |
2846 | |
2847 | void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev) | |
2848 | { | |
2849 | struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev); | |
2850 | struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev); | |
2851 | struct fcoe_fcf *fcf; | |
2852 | ||
2853 | mutex_lock(&fip->ctlr_mutex); | |
2854 | mutex_lock(&ctlr_dev->lock); | |
2855 | ||
2856 | fcf = fcoe_fcf_device_priv(fcf_dev); | |
2857 | if (fcf) | |
2858 | fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0; | |
2859 | else | |
2860 | fcf_dev->selected = 0; | |
2861 | ||
2862 | mutex_unlock(&ctlr_dev->lock); | |
2863 | mutex_unlock(&fip->ctlr_mutex); | |
2864 | } | |
2865 | EXPORT_SYMBOL(fcoe_fcf_get_selected); | |
2866 | ||
2867 | void fcoe_ctlr_get_fip_mode(struct fcoe_ctlr_device *ctlr_dev) | |
2868 | { | |
2869 | struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev); | |
2870 | ||
2871 | mutex_lock(&ctlr->ctlr_mutex); | |
2872 | switch (ctlr->mode) { | |
2873 | case FIP_MODE_FABRIC: | |
2874 | ctlr_dev->mode = FIP_CONN_TYPE_FABRIC; | |
2875 | break; | |
2876 | case FIP_MODE_VN2VN: | |
2877 | ctlr_dev->mode = FIP_CONN_TYPE_VN2VN; | |
2878 | break; | |
2879 | default: | |
2880 | ctlr_dev->mode = FIP_CONN_TYPE_UNKNOWN; | |
2881 | break; | |
2882 | } | |
2883 | mutex_unlock(&ctlr->ctlr_mutex); | |
2884 | } | |
2885 | EXPORT_SYMBOL(fcoe_ctlr_get_fip_mode); |