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