blob: 7dc4ffa244304ada7bab9b8718af9ebc7066d6ab [file] [log] [blame]
Vasu Dev9b34ecf2009-03-17 11:42:13 -07001/*
Joe Eykholt97c83892009-03-17 11:42:40 -07002 * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2009 Intel Corporation. All rights reserved.
Vasu Dev9b34ecf2009-03-17 11:42:13 -07004 *
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
Joe Eykholt97c83892009-03-17 11:42:40 -070021#include <linux/types.h>
Vasu Dev9b34ecf2009-03-17 11:42:13 -070022#include <linux/module.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070023#include <linux/kernel.h>
24#include <linux/list.h>
25#include <linux/spinlock.h>
26#include <linux/timer.h>
Vasu Dev5e80f7f2009-03-17 11:42:18 -070027#include <linux/netdevice.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070028#include <linux/etherdevice.h>
29#include <linux/ethtool.h>
30#include <linux/if_ether.h>
31#include <linux/if_vlan.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070032#include <linux/errno.h>
33#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070035#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>
Joe Eykholte10f8c62010-07-20 15:20:30 -070042#include <scsi/fc/fc_fcp.h>
Vasu Dev5e80f7f2009-03-17 11:42:18 -070043
44#include <scsi/libfc.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070045#include <scsi/libfcoe.h>
Vasu Dev9b34ecf2009-03-17 11:42:13 -070046
Yi Zou21b7b2f2011-01-28 16:04:45 -080047#include "libfcoe.h"
48
Joe Eykholt97c83892009-03-17 11:42:40 -070049#define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
50#define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
51
Kees Cooke99e88a2017-10-16 14:43:17 -070052static void fcoe_ctlr_timeout(struct timer_list *);
Joe Eykholt42913652010-03-12 16:08:23 -080053static void fcoe_ctlr_timer_work(struct work_struct *);
Joe Eykholt97c83892009-03-17 11:42:40 -070054static void fcoe_ctlr_recv_work(struct work_struct *);
Joe Eykholt794d98e2010-11-30 16:19:56 -080055static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
Joe Eykholt97c83892009-03-17 11:42:40 -070056
Joe Eykholte10f8c62010-07-20 15:20:30 -070057static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
58static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
59static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
60static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
61
Hannes Reinecke9a6cf882016-07-19 13:49:40 +020062static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *, struct sk_buff *);
63
Joe Eykholt97c83892009-03-17 11:42:40 -070064static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
Joe Eykholte10f8c62010-07-20 15:20:30 -070065static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
66static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
67static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
Joe Eykholt97c83892009-03-17 11:42:40 -070068
Yi Zou0095a922011-01-28 16:05:00 -080069static const char * const fcoe_ctlr_states[] = {
Joe Eykholt9b651da2010-07-20 15:20:24 -070070 [FIP_ST_DISABLED] = "DISABLED",
71 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
72 [FIP_ST_AUTO] = "AUTO",
73 [FIP_ST_NON_FIP] = "NON_FIP",
74 [FIP_ST_ENABLED] = "ENABLED",
Joe Eykholte10f8c62010-07-20 15:20:30 -070075 [FIP_ST_VNMP_START] = "VNMP_START",
76 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
77 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
78 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
79 [FIP_ST_VNMP_UP] = "VNMP_UP",
Joe Eykholt9b651da2010-07-20 15:20:24 -070080};
81
82static const char *fcoe_ctlr_state(enum fip_state state)
83{
84 const char *cp = "unknown";
85
86 if (state < ARRAY_SIZE(fcoe_ctlr_states))
87 cp = fcoe_ctlr_states[state];
88 if (!cp)
89 cp = "unknown";
90 return cp;
91}
92
93/**
94 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
95 * @fip: The FCoE controller
96 * @state: The new state
97 */
98static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
99{
100 if (state == fip->state)
101 return;
102 if (fip->lp)
103 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
104 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
105 fip->state = state;
106}
107
Robert Love70b51aa2009-11-03 11:47:45 -0800108/**
109 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
110 * @fcf: The FCF to check
111 *
Joe Eykholt97c83892009-03-17 11:42:40 -0700112 * Return non-zero if FCF fcoe_size has been validated.
113 */
114static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
115{
116 return (fcf->flags & FIP_FL_SOL) != 0;
117}
118
Robert Love70b51aa2009-11-03 11:47:45 -0800119/**
120 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
121 * @fcf: The FCF to check
122 *
Joe Eykholt97c83892009-03-17 11:42:40 -0700123 * Return non-zero if the FCF is usable.
124 */
125static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
126{
127 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
128
129 return (fcf->flags & flags) == flags;
130}
131
132/**
Joe Eykholtcd229e42010-07-20 15:20:40 -0700133 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
134 * @fip: The FCoE controller
135 */
136static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
137{
138 if (fip->mode == FIP_MODE_VN2VN)
139 hton24(fip->dest_addr, FIP_VN_FC_MAP);
140 else
141 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
142 hton24(fip->dest_addr + 3, 0);
143 fip->map_dest = 1;
144}
145
146/**
Robert Love70b51aa2009-11-03 11:47:45 -0800147 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
148 * @fip: The FCoE controller to initialize
Joe Eykholt97c83892009-03-17 11:42:40 -0700149 */
Sedat Dilek8beb90a2019-02-15 13:19:20 +0100150void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_mode mode)
Joe Eykholt97c83892009-03-17 11:42:40 -0700151{
Joe Eykholt9b651da2010-07-20 15:20:24 -0700152 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700153 fip->mode = mode;
Hannes Reinecke9a6cf882016-07-19 13:49:40 +0200154 fip->fip_resp = false;
Joe Eykholt97c83892009-03-17 11:42:40 -0700155 INIT_LIST_HEAD(&fip->fcfs);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700156 mutex_init(&fip->ctlr_mutex);
Joe Eykholt794d98e2010-11-30 16:19:56 -0800157 spin_lock_init(&fip->ctlr_lock);
Joe Eykholt97c83892009-03-17 11:42:40 -0700158 fip->flogi_oxid = FC_XID_UNKNOWN;
Kees Cooke99e88a2017-10-16 14:43:17 -0700159 timer_setup(&fip->timer, fcoe_ctlr_timeout, 0);
Joe Eykholt42913652010-03-12 16:08:23 -0800160 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700161 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
162 skb_queue_head_init(&fip->fip_recv_list);
163}
164EXPORT_SYMBOL(fcoe_ctlr_init);
165
Robert Love9d348762013-09-05 07:47:27 +0000166/**
167 * fcoe_sysfs_fcf_add() - Add a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
168 * @new: The newly discovered FCF
169 *
170 * Called with fip->ctlr_mutex held
171 */
Robert Love8d55e502012-05-22 19:06:26 -0700172static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
173{
174 struct fcoe_ctlr *fip = new->fip;
Robert Love9d348762013-09-05 07:47:27 +0000175 struct fcoe_ctlr_device *ctlr_dev;
Bart Van Assche1c2c1b42013-08-14 15:42:46 +0000176 struct fcoe_fcf_device *temp, *fcf_dev;
177 int rc = -ENOMEM;
Robert Love8d55e502012-05-22 19:06:26 -0700178
179 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
180 new->fabric_name, new->fcf_mac);
181
Bart Van Assche1c2c1b42013-08-14 15:42:46 +0000182 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
183 if (!temp)
184 goto out;
185
Bart Van Assche1c2c1b42013-08-14 15:42:46 +0000186 temp->fabric_name = new->fabric_name;
187 temp->switch_name = new->switch_name;
188 temp->fc_map = new->fc_map;
189 temp->vfid = new->vfid;
190 memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
191 temp->priority = new->pri;
192 temp->fka_period = new->fka_period;
193 temp->selected = 0; /* default to unselected */
Robert Love8d55e502012-05-22 19:06:26 -0700194
Robert Love8d55e502012-05-22 19:06:26 -0700195 /*
Robert Love9d348762013-09-05 07:47:27 +0000196 * If ctlr_dev doesn't exist then it means we're a libfcoe user
197 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device.
198 * fnic would be an example of a driver with this behavior. In this
199 * case we want to add the fcoe_fcf to the fcoe_ctlr list, but we
200 * don't want to make sysfs changes.
Robert Love8d55e502012-05-22 19:06:26 -0700201 */
Robert Love8d55e502012-05-22 19:06:26 -0700202
Robert Love9d348762013-09-05 07:47:27 +0000203 ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
204 if (ctlr_dev) {
205 mutex_lock(&ctlr_dev->lock);
206 fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
207 if (unlikely(!fcf_dev)) {
208 rc = -ENOMEM;
Neil Horman55d0ac52013-10-08 23:43:58 +0000209 mutex_unlock(&ctlr_dev->lock);
Robert Love9d348762013-09-05 07:47:27 +0000210 goto out;
211 }
212
213 /*
214 * The fcoe_sysfs layer can return a CONNECTED fcf that
215 * has a priv (fcf was never deleted) or a CONNECTED fcf
216 * that doesn't have a priv (fcf was deleted). However,
217 * libfcoe will always delete FCFs before trying to add
218 * them. This is ensured because both recv_adv and
219 * age_fcfs are protected by the the fcoe_ctlr's mutex.
220 * This means that we should never get a FCF with a
221 * non-NULL priv pointer.
222 */
223 BUG_ON(fcf_dev->priv);
224
225 fcf_dev->priv = new;
226 new->fcf_dev = fcf_dev;
227 mutex_unlock(&ctlr_dev->lock);
228 }
Robert Love8d55e502012-05-22 19:06:26 -0700229
230 list_add(&new->list, &fip->fcfs);
231 fip->fcf_count++;
Bart Van Assche1c2c1b42013-08-14 15:42:46 +0000232 rc = 0;
233
Robert Love8d55e502012-05-22 19:06:26 -0700234out:
Bart Van Assche1c2c1b42013-08-14 15:42:46 +0000235 kfree(temp);
Robert Love8d55e502012-05-22 19:06:26 -0700236 return rc;
237}
238
Robert Love9d348762013-09-05 07:47:27 +0000239/**
240 * fcoe_sysfs_fcf_del() - Remove a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
241 * @new: The FCF to be removed
242 *
243 * Called with fip->ctlr_mutex held
244 */
Robert Love8d55e502012-05-22 19:06:26 -0700245static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
246{
247 struct fcoe_ctlr *fip = new->fip;
Robert Love9d348762013-09-05 07:47:27 +0000248 struct fcoe_ctlr_device *cdev;
Robert Love8d55e502012-05-22 19:06:26 -0700249 struct fcoe_fcf_device *fcf_dev;
250
251 list_del(&new->list);
252 fip->fcf_count--;
253
Robert Love9d348762013-09-05 07:47:27 +0000254 /*
255 * If ctlr_dev doesn't exist then it means we're a libfcoe user
256 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device
257 * or a fcoe_fcf_device.
258 *
259 * fnic would be an example of a driver with this behavior. In this
260 * case we want to remove the fcoe_fcf from the fcoe_ctlr list (above),
261 * but we don't want to make sysfs changes.
262 */
263 cdev = fcoe_ctlr_to_ctlr_dev(fip);
264 if (cdev) {
265 mutex_lock(&cdev->lock);
266 fcf_dev = fcoe_fcf_to_fcf_dev(new);
267 WARN_ON(!fcf_dev);
268 new->fcf_dev = NULL;
269 fcoe_fcf_device_delete(fcf_dev);
270 kfree(new);
271 mutex_unlock(&cdev->lock);
272 }
Robert Love8d55e502012-05-22 19:06:26 -0700273}
274
Joe Eykholt97c83892009-03-17 11:42:40 -0700275/**
Robert Love70b51aa2009-11-03 11:47:45 -0800276 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
277 * @fip: The FCoE controller whose FCFs are to be reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700278 *
279 * Called with &fcoe_ctlr lock held.
280 */
281static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
282{
283 struct fcoe_fcf *fcf;
284 struct fcoe_fcf *next;
285
286 fip->sel_fcf = NULL;
287 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
Robert Love8d55e502012-05-22 19:06:26 -0700288 fcoe_sysfs_fcf_del(fcf);
Joe Eykholt97c83892009-03-17 11:42:40 -0700289 }
Robert Love8d55e502012-05-22 19:06:26 -0700290 WARN_ON(fip->fcf_count);
291
Joe Eykholt97c83892009-03-17 11:42:40 -0700292 fip->sel_time = 0;
293}
294
295/**
Robert Love70b51aa2009-11-03 11:47:45 -0800296 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
297 * @fip: The FCoE controller to tear down
Joe Eykholt97c83892009-03-17 11:42:40 -0700298 *
299 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
300 *
301 * The receive handler will have been deleted before this to guarantee
302 * that no more recv_work will be scheduled.
303 *
304 * The timer routine will simply return once we set FIP_ST_DISABLED.
305 * This guarantees that no further timeouts or work will be scheduled.
306 */
307void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
308{
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700309 cancel_work_sync(&fip->recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -0800310 skb_queue_purge(&fip->fip_recv_list);
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700311
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700312 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt9b651da2010-07-20 15:20:24 -0700313 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -0700314 fcoe_ctlr_reset_fcfs(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700315 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700316 del_timer_sync(&fip->timer);
Joe Eykholt42913652010-03-12 16:08:23 -0800317 cancel_work_sync(&fip->timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700318}
319EXPORT_SYMBOL(fcoe_ctlr_destroy);
320
321/**
Joe Eykholt794d98e2010-11-30 16:19:56 -0800322 * fcoe_ctlr_announce() - announce new FCF selection
Joe Eykholt69316ee2010-11-30 16:19:40 -0800323 * @fip: The FCoE controller
324 *
325 * Also sets the destination MAC for FCoE and control packets
Joe Eykholt794d98e2010-11-30 16:19:56 -0800326 *
327 * Called with neither ctlr_mutex nor ctlr_lock held.
Joe Eykholt69316ee2010-11-30 16:19:40 -0800328 */
329static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
330{
Joe Eykholt794d98e2010-11-30 16:19:56 -0800331 struct fcoe_fcf *sel;
332 struct fcoe_fcf *fcf;
333
334 mutex_lock(&fip->ctlr_mutex);
335 spin_lock_bh(&fip->ctlr_lock);
336
337 kfree_skb(fip->flogi_req);
338 fip->flogi_req = NULL;
339 list_for_each_entry(fcf, &fip->fcfs, list)
340 fcf->flogi_sent = 0;
341
342 spin_unlock_bh(&fip->ctlr_lock);
343 sel = fip->sel_fcf;
Joe Eykholt69316ee2010-11-30 16:19:40 -0800344
Joe Perches6942df72013-09-02 03:32:33 +0000345 if (sel && ether_addr_equal(sel->fcf_mac, fip->dest_addr))
Joe Eykholt794d98e2010-11-30 16:19:56 -0800346 goto unlock;
Joe Eykholt69316ee2010-11-30 16:19:40 -0800347 if (!is_zero_ether_addr(fip->dest_addr)) {
348 printk(KERN_NOTICE "libfcoe: host%d: "
349 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
350 fip->lp->host->host_no, fip->dest_addr);
351 memset(fip->dest_addr, 0, ETH_ALEN);
352 }
353 if (sel) {
354 printk(KERN_INFO "libfcoe: host%d: FIP selected "
355 "Fibre-Channel Forwarder MAC %pM\n",
356 fip->lp->host->host_no, sel->fcf_mac);
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -0800357 memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
Joe Eykholt69316ee2010-11-30 16:19:40 -0800358 fip->map_dest = 0;
359 }
Joe Eykholt794d98e2010-11-30 16:19:56 -0800360unlock:
361 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt69316ee2010-11-30 16:19:40 -0800362}
363
364/**
Robert Love70b51aa2009-11-03 11:47:45 -0800365 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
366 * @fip: The FCoE controller to get the maximum FCoE size from
Joe Eykholt97c83892009-03-17 11:42:40 -0700367 *
368 * Returns the maximum packet size including the FCoE header and trailer,
369 * but not including any Ethernet or VLAN headers.
370 */
371static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
372{
373 /*
374 * Determine the max FCoE frame size allowed, including
375 * FCoE header and trailer.
376 * Note: lp->mfs is currently the payload size, not the frame size.
377 */
378 return fip->lp->mfs + sizeof(struct fc_frame_header) +
379 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
380}
381
382/**
Robert Love70b51aa2009-11-03 11:47:45 -0800383 * fcoe_ctlr_solicit() - Send a FIP solicitation
384 * @fip: The FCoE controller to send the solicitation on
385 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
Joe Eykholt97c83892009-03-17 11:42:40 -0700386 */
387static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
388{
389 struct sk_buff *skb;
390 struct fip_sol {
391 struct ethhdr eth;
392 struct fip_header fip;
393 struct {
394 struct fip_mac_desc mac;
395 struct fip_wwn_desc wwnn;
396 struct fip_size_desc size;
Yi Zou0095a922011-01-28 16:05:00 -0800397 } __packed desc;
398 } __packed * sol;
Joe Eykholt97c83892009-03-17 11:42:40 -0700399 u32 fcoe_size;
400
401 skb = dev_alloc_skb(sizeof(*sol));
402 if (!skb)
403 return;
404
405 sol = (struct fip_sol *)skb->data;
406
407 memset(sol, 0, sizeof(*sol));
408 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
409 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
410 sol->eth.h_proto = htons(ETH_P_FIP);
411
412 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
413 sol->fip.fip_op = htons(FIP_OP_DISC);
414 sol->fip.fip_subcode = FIP_SC_SOL;
415 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
416 sol->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000417 if (fip->spma)
418 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700419
420 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
421 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
422 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
423
424 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
425 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
426 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
427
428 fcoe_size = fcoe_ctlr_fcoe_size(fip);
429 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
430 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
431 sol->desc.size.fd_size = htons(fcoe_size);
432
433 skb_put(skb, sizeof(*sol));
Chris Leech0f491532009-05-06 10:52:18 -0700434 skb->protocol = htons(ETH_P_FIP);
john fastabend6f6c2aa2011-11-18 13:35:56 -0800435 skb->priority = fip->priority;
Joe Eykholt97c83892009-03-17 11:42:40 -0700436 skb_reset_mac_header(skb);
437 skb_reset_network_header(skb);
438 fip->send(fip, skb);
439
440 if (!fcf)
441 fip->sol_time = jiffies;
442}
443
444/**
Robert Love70b51aa2009-11-03 11:47:45 -0800445 * fcoe_ctlr_link_up() - Start FCoE controller
446 * @fip: The FCoE controller to start
Joe Eykholt97c83892009-03-17 11:42:40 -0700447 *
448 * Called from the LLD when the network link is ready.
449 */
450void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
451{
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700452 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700453 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700454 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700455 fc_linkup(fip->lp);
456 } else if (fip->state == FIP_ST_LINK_WAIT) {
Sedat Dilek8beb90a2019-02-15 13:19:20 +0100457 if (fip->mode == FIP_MODE_NON_FIP)
458 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
459 else
460 fcoe_ctlr_set_state(fip, FIP_ST_AUTO);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700461 switch (fip->mode) {
462 default:
463 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
464 /* fall-through */
465 case FIP_MODE_AUTO:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800466 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -0700467 /* fall-through */
468 case FIP_MODE_FABRIC:
469 case FIP_MODE_NON_FIP:
470 mutex_unlock(&fip->ctlr_mutex);
471 fc_linkup(fip->lp);
472 fcoe_ctlr_solicit(fip, NULL);
473 break;
474 case FIP_MODE_VN2VN:
475 fcoe_ctlr_vn_start(fip);
476 mutex_unlock(&fip->ctlr_mutex);
477 fc_linkup(fip->lp);
478 break;
479 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700480 } else
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700481 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700482}
483EXPORT_SYMBOL(fcoe_ctlr_link_up);
484
485/**
Robert Love70b51aa2009-11-03 11:47:45 -0800486 * fcoe_ctlr_reset() - Reset a FCoE controller
487 * @fip: The FCoE controller to reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700488 */
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800489static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700490{
Joe Eykholt97c83892009-03-17 11:42:40 -0700491 fcoe_ctlr_reset_fcfs(fip);
492 del_timer(&fip->timer);
Joe Eykholt97c83892009-03-17 11:42:40 -0700493 fip->ctlr_ka_time = 0;
494 fip->port_ka_time = 0;
495 fip->sol_time = 0;
496 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtcd229e42010-07-20 15:20:40 -0700497 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -0700498}
499
500/**
Robert Love70b51aa2009-11-03 11:47:45 -0800501 * fcoe_ctlr_link_down() - Stop a FCoE controller
502 * @fip: The FCoE controller to be stopped
Joe Eykholt97c83892009-03-17 11:42:40 -0700503 *
504 * Returns non-zero if the link was up and now isn't.
505 *
506 * Called from the LLD when the network link is not ready.
507 * There may be multiple calls while the link is down.
508 */
509int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
510{
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800511 int link_dropped;
512
513 LIBFCOE_FIP_DBG(fip, "link down.\n");
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700514 mutex_lock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800515 fcoe_ctlr_reset(fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800516 link_dropped = fip->state != FIP_ST_LINK_WAIT;
Joe Eykholt9b651da2010-07-20 15:20:24 -0700517 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700518 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800519
520 if (link_dropped)
521 fc_linkdown(fip->lp);
522 return link_dropped;
Joe Eykholt97c83892009-03-17 11:42:40 -0700523}
524EXPORT_SYMBOL(fcoe_ctlr_link_down);
525
526/**
Robert Love70b51aa2009-11-03 11:47:45 -0800527 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
528 * @fip: The FCoE controller to send the FKA on
529 * @lport: libfc fc_lport to send from
530 * @ports: 0 for controller keep-alive, 1 for port keep-alive
531 * @sa: The source MAC address
Joe Eykholt97c83892009-03-17 11:42:40 -0700532 *
533 * A controller keep-alive is sent every fka_period (typically 8 seconds).
534 * The source MAC is the native MAC address.
535 *
536 * A port keep-alive is sent every 90 seconds while logged in.
537 * The source MAC is the assigned mapped source address.
538 * The destination is the FCF's F-port.
539 */
Chris Leech11b56182009-11-03 11:46:29 -0800540static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
541 struct fc_lport *lport,
542 int ports, u8 *sa)
Joe Eykholt97c83892009-03-17 11:42:40 -0700543{
544 struct sk_buff *skb;
545 struct fip_kal {
546 struct ethhdr eth;
547 struct fip_header fip;
548 struct fip_mac_desc mac;
Yi Zou0095a922011-01-28 16:05:00 -0800549 } __packed * kal;
Joe Eykholt97c83892009-03-17 11:42:40 -0700550 struct fip_vn_desc *vn;
551 u32 len;
552 struct fc_lport *lp;
553 struct fcoe_fcf *fcf;
554
555 fcf = fip->sel_fcf;
556 lp = fip->lp;
Joe Eykholt281ae642010-06-11 16:43:33 -0700557 if (!fcf || (ports && !lp->port_id))
Joe Eykholt97c83892009-03-17 11:42:40 -0700558 return;
559
Yi Zoube276cb2009-11-03 11:50:16 -0800560 len = sizeof(*kal) + ports * sizeof(*vn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700561 skb = dev_alloc_skb(len);
562 if (!skb)
563 return;
564
565 kal = (struct fip_kal *)skb->data;
566 memset(kal, 0, len);
567 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
568 memcpy(kal->eth.h_source, sa, ETH_ALEN);
569 kal->eth.h_proto = htons(ETH_P_FIP);
570
571 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
572 kal->fip.fip_op = htons(FIP_OP_CTRL);
573 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
574 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
Robert Love70b51aa2009-11-03 11:47:45 -0800575 ports * sizeof(*vn)) / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700576 kal->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000577 if (fip->spma)
578 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700579
580 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
581 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
582 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700583 if (ports) {
584 vn = (struct fip_vn_desc *)(kal + 1);
585 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
586 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
Chris Leech11b56182009-11-03 11:46:29 -0800587 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Kaladhar Musunurufb831532010-05-07 15:18:57 -0700588 hton24(vn->fd_fc_id, lport->port_id);
589 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700590 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700591 skb_put(skb, len);
Chris Leech0f491532009-05-06 10:52:18 -0700592 skb->protocol = htons(ETH_P_FIP);
john fastabend6f6c2aa2011-11-18 13:35:56 -0800593 skb->priority = fip->priority;
Joe Eykholt97c83892009-03-17 11:42:40 -0700594 skb_reset_mac_header(skb);
595 skb_reset_network_header(skb);
596 fip->send(fip, skb);
597}
598
599/**
Robert Love70b51aa2009-11-03 11:47:45 -0800600 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
601 * @fip: The FCoE controller for the ELS frame
602 * @dtype: The FIP descriptor type for the frame
603 * @skb: The FCoE ELS frame including FC header but no FCoE headers
Joe Eykholte10f8c62010-07-20 15:20:30 -0700604 * @d_id: The destination port ID.
Joe Eykholt97c83892009-03-17 11:42:40 -0700605 *
606 * Returns non-zero error code on failure.
607 *
608 * The caller must check that the length is a multiple of 4.
609 *
610 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
611 * Headroom includes the FIP encapsulation description, FIP header, and
612 * Ethernet header. The tailroom is for the FIP MAC descriptor.
613 */
Chris Leech11b56182009-11-03 11:46:29 -0800614static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholte10f8c62010-07-20 15:20:30 -0700615 u8 dtype, struct sk_buff *skb, u32 d_id)
Joe Eykholt97c83892009-03-17 11:42:40 -0700616{
617 struct fip_encaps_head {
618 struct ethhdr eth;
619 struct fip_header fip;
620 struct fip_encaps encaps;
Yi Zou0095a922011-01-28 16:05:00 -0800621 } __packed * cap;
Joe Eykholt55543452010-07-20 15:20:35 -0700622 struct fc_frame_header *fh;
Joe Eykholt97c83892009-03-17 11:42:40 -0700623 struct fip_mac_desc *mac;
624 struct fcoe_fcf *fcf;
625 size_t dlen;
Yi Zou5a84bae2009-07-29 17:03:55 -0700626 u16 fip_flags;
Joe Eykholt55543452010-07-20 15:20:35 -0700627 u8 op;
Joe Eykholt97c83892009-03-17 11:42:40 -0700628
Joe Eykholt55543452010-07-20 15:20:35 -0700629 fh = (struct fc_frame_header *)skb->data;
630 op = *(u8 *)(fh + 1);
Joe Eykholt97c83892009-03-17 11:42:40 -0700631 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
Johannes Bergd58ff352017-06-16 14:29:23 +0200632 cap = skb_push(skb, sizeof(*cap));
Joe Eykholt97c83892009-03-17 11:42:40 -0700633 memset(cap, 0, sizeof(*cap));
Joe Eykholte10f8c62010-07-20 15:20:30 -0700634
635 if (lport->point_to_multipoint) {
636 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
637 return -ENODEV;
Joe Eykholt55543452010-07-20 15:20:35 -0700638 fip_flags = 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700639 } else {
640 fcf = fip->sel_fcf;
641 if (!fcf)
642 return -ENODEV;
643 fip_flags = fcf->flags;
644 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
645 FIP_FL_FPMA;
646 if (!fip_flags)
647 return -ENODEV;
648 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
649 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700650 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
651 cap->eth.h_proto = htons(ETH_P_FIP);
652
653 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
654 cap->fip.fip_op = htons(FIP_OP_LS);
Joe Eykholt55543452010-07-20 15:20:35 -0700655 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
656 cap->fip.fip_subcode = FIP_SC_REP;
657 else
658 cap->fip.fip_subcode = FIP_SC_REQ;
Yi Zou5a84bae2009-07-29 17:03:55 -0700659 cap->fip.fip_flags = htons(fip_flags);
Joe Eykholt97c83892009-03-17 11:42:40 -0700660
661 cap->encaps.fd_desc.fip_dtype = dtype;
662 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
663
Joe Eykholt55543452010-07-20 15:20:35 -0700664 if (op != ELS_LS_RJT) {
665 dlen += sizeof(*mac);
Johannes Bergb080db52017-06-16 14:29:19 +0200666 mac = skb_put_zero(skb, sizeof(*mac));
Joe Eykholt55543452010-07-20 15:20:35 -0700667 mac->fd_desc.fip_dtype = FIP_DT_MAC;
668 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
669 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
670 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
671 } else if (fip->mode == FIP_MODE_VN2VN) {
672 hton24(mac->fd_mac, FIP_VN_FC_MAP);
673 hton24(mac->fd_mac + 3, fip->port_id);
674 } else if (fip_flags & FIP_FL_SPMA) {
675 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
676 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
677 } else {
678 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
679 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
680 }
Robert Love593abc02010-04-09 14:22:17 -0700681 }
Joe Eykholt55543452010-07-20 15:20:35 -0700682 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700683
Chris Leech0f491532009-05-06 10:52:18 -0700684 skb->protocol = htons(ETH_P_FIP);
john fastabend6f6c2aa2011-11-18 13:35:56 -0800685 skb->priority = fip->priority;
Joe Eykholt97c83892009-03-17 11:42:40 -0700686 skb_reset_mac_header(skb);
687 skb_reset_network_header(skb);
688 return 0;
689}
690
691/**
692 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
693 * @fip: FCoE controller.
Chris Leech11b56182009-11-03 11:46:29 -0800694 * @lport: libfc fc_lport to send from
Joe Eykholt97c83892009-03-17 11:42:40 -0700695 * @skb: FCoE ELS frame including FC header but no FCoE headers.
696 *
697 * Returns a non-zero error code if the frame should not be sent.
698 * Returns zero if the caller should send the frame with FCoE encapsulation.
699 *
700 * The caller must check that the length is a multiple of 4.
701 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
Joe Eykholte10f8c62010-07-20 15:20:30 -0700702 * The the skb must also be an fc_frame.
Joe Eykholt794d98e2010-11-30 16:19:56 -0800703 *
704 * This is called from the lower-level driver with spinlocks held,
705 * so we must not take a mutex here.
Joe Eykholt97c83892009-03-17 11:42:40 -0700706 */
Chris Leech11b56182009-11-03 11:46:29 -0800707int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
708 struct sk_buff *skb)
Joe Eykholt97c83892009-03-17 11:42:40 -0700709{
Joe Eykholte10f8c62010-07-20 15:20:30 -0700710 struct fc_frame *fp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700711 struct fc_frame_header *fh;
712 u16 old_xid;
713 u8 op;
Chris Leech11b56182009-11-03 11:46:29 -0800714 u8 mac[ETH_ALEN];
Joe Eykholt97c83892009-03-17 11:42:40 -0700715
Joe Eykholte10f8c62010-07-20 15:20:30 -0700716 fp = container_of(skb, struct fc_frame, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -0700717 fh = (struct fc_frame_header *)skb->data;
718 op = *(u8 *)(fh + 1);
719
Joe Eykholte10f8c62010-07-20 15:20:30 -0700720 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700721 old_xid = fip->flogi_oxid;
722 fip->flogi_oxid = ntohs(fh->fh_ox_id);
723 if (fip->state == FIP_ST_AUTO) {
724 if (old_xid == FC_XID_UNKNOWN)
725 fip->flogi_count = 0;
726 fip->flogi_count++;
727 if (fip->flogi_count < 3)
728 goto drop;
Joe Eykholtcd229e42010-07-20 15:20:40 -0700729 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -0700730 return 0;
731 }
Joe Eykholt5f48f702009-05-06 10:52:12 -0700732 if (fip->state == FIP_ST_NON_FIP)
Joe Eykholtcd229e42010-07-20 15:20:40 -0700733 fcoe_ctlr_map_dest(fip);
Joe Eykholt5f48f702009-05-06 10:52:12 -0700734 }
735
736 if (fip->state == FIP_ST_NON_FIP)
737 return 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700738 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
Joe Eykholtf31f2a12009-11-03 11:48:32 -0800739 goto drop;
Joe Eykholt5f48f702009-05-06 10:52:12 -0700740 switch (op) {
741 case ELS_FLOGI:
Joe Eykholt97c83892009-03-17 11:42:40 -0700742 op = FIP_DT_FLOGI;
Joe Eykholt794d98e2010-11-30 16:19:56 -0800743 if (fip->mode == FIP_MODE_VN2VN)
744 break;
745 spin_lock_bh(&fip->ctlr_lock);
746 kfree_skb(fip->flogi_req);
747 fip->flogi_req = skb;
748 fip->flogi_req_send = 1;
749 spin_unlock_bh(&fip->ctlr_lock);
750 schedule_work(&fip->timer_work);
751 return -EINPROGRESS;
Joe Eykholt97c83892009-03-17 11:42:40 -0700752 case ELS_FDISC:
753 if (ntoh24(fh->fh_s_id))
754 return 0;
755 op = FIP_DT_FDISC;
756 break;
757 case ELS_LOGO:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700758 if (fip->mode == FIP_MODE_VN2VN) {
759 if (fip->state != FIP_ST_VNMP_UP)
Johannes Thumshirn63d0e3d2018-07-31 15:46:02 +0200760 goto drop;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700761 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
Johannes Thumshirn63d0e3d2018-07-31 15:46:02 +0200762 goto drop;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700763 } else {
764 if (fip->state != FIP_ST_ENABLED)
765 return 0;
766 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
767 return 0;
768 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700769 op = FIP_DT_LOGO;
770 break;
771 case ELS_LS_ACC:
Joe Eykholt97c83892009-03-17 11:42:40 -0700772 /*
Joe Eykholte10f8c62010-07-20 15:20:30 -0700773 * If non-FIP, we may have gotten an SID by accepting an FLOGI
Joe Eykholt97c83892009-03-17 11:42:40 -0700774 * from a point-to-point connection. Switch to using
775 * the source mac based on the SID. The destination
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300776 * MAC in this case would have been set by receiving the
Joe Eykholt97c83892009-03-17 11:42:40 -0700777 * FLOGI.
778 */
Joe Eykholte10f8c62010-07-20 15:20:30 -0700779 if (fip->state == FIP_ST_NON_FIP) {
780 if (fip->flogi_oxid == FC_XID_UNKNOWN)
781 return 0;
782 fip->flogi_oxid = FC_XID_UNKNOWN;
783 fc_fcoe_set_mac(mac, fh->fh_d_id);
784 fip->update_mac(lport, mac);
785 }
786 /* fall through */
787 case ELS_LS_RJT:
788 op = fr_encaps(fp);
789 if (op)
790 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700791 return 0;
792 default:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700793 if (fip->state != FIP_ST_ENABLED &&
794 fip->state != FIP_ST_VNMP_UP)
Joe Eykholt97c83892009-03-17 11:42:40 -0700795 goto drop;
796 return 0;
797 }
Joe Eykholte10f8c62010-07-20 15:20:30 -0700798 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
799 op, ntoh24(fh->fh_d_id));
800 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
Joe Eykholt97c83892009-03-17 11:42:40 -0700801 goto drop;
802 fip->send(fip, skb);
803 return -EINPROGRESS;
804drop:
Hannes Reineckec9596552016-10-13 15:10:58 +0200805 LIBFCOE_FIP_DBG(fip, "drop els_send op %u d_id %x\n",
806 op, ntoh24(fh->fh_d_id));
Johannes Thumshirn2d7d4fd2018-07-31 15:46:01 +0200807 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -0700808 return -EINVAL;
809}
810EXPORT_SYMBOL(fcoe_ctlr_els_send);
811
Robert Love70b51aa2009-11-03 11:47:45 -0800812/**
813 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
814 * @fip: The FCoE controller to free FCFs on
Joe Eykholt97c83892009-03-17 11:42:40 -0700815 *
Joe Eykholtf018b732010-03-12 16:08:55 -0800816 * Called with lock held and preemption disabled.
Joe Eykholt97c83892009-03-17 11:42:40 -0700817 *
Joe Eykholt8690cb82010-06-11 16:44:10 -0700818 * An FCF is considered old if we have missed two advertisements.
819 * That is, there have been no valid advertisement from it for 2.5
820 * times its keep-alive period.
Joe Eykholt97c83892009-03-17 11:42:40 -0700821 *
822 * In addition, determine the time when an FCF selection can occur.
Yi Zouf3da80e72009-11-20 14:55:08 -0800823 *
824 * Also, increment the MissDiscAdvCount when no advertisement is received
825 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
Joe Eykholt8690cb82010-06-11 16:44:10 -0700826 *
827 * Returns the time in jiffies for the next call.
Joe Eykholt97c83892009-03-17 11:42:40 -0700828 */
Joe Eykholt8690cb82010-06-11 16:44:10 -0700829static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700830{
831 struct fcoe_fcf *fcf;
832 struct fcoe_fcf *next;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700833 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
834 unsigned long deadline;
Joe Eykholt97c83892009-03-17 11:42:40 -0700835 unsigned long sel_time = 0;
Robert Love8d55e502012-05-22 19:06:26 -0700836 struct list_head del_list;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700837 struct fc_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -0700838
Robert Love8d55e502012-05-22 19:06:26 -0700839 INIT_LIST_HEAD(&del_list);
840
Vasu Dev1bd49b42012-05-25 10:26:43 -0700841 stats = per_cpu_ptr(fip->lp->stats, get_cpu());
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700842
Joe Eykholt97c83892009-03-17 11:42:40 -0700843 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700844 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
845 if (fip->sel_fcf == fcf) {
846 if (time_after(jiffies, deadline)) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700847 stats->MissDiscAdvCount++;
848 printk(KERN_INFO "libfcoe: host%d: "
849 "Missing Discovery Advertisement "
850 "for fab %16.16llx count %lld\n",
851 fip->lp->host->host_no, fcf->fabric_name,
852 stats->MissDiscAdvCount);
853 } else if (time_after(next_timer, deadline))
854 next_timer = deadline;
Yi Zouf3da80e72009-11-20 14:55:08 -0800855 }
Joe Eykholt8690cb82010-06-11 16:44:10 -0700856
857 deadline += fcf->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -0700858 if (time_after_eq(jiffies, deadline)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700859 if (fip->sel_fcf == fcf)
860 fip->sel_fcf = NULL;
Robert Love8d55e502012-05-22 19:06:26 -0700861 /*
862 * Move to delete list so we can call
863 * fcoe_sysfs_fcf_del (which can sleep)
864 * after the put_cpu().
865 */
Joe Eykholt97c83892009-03-17 11:42:40 -0700866 list_del(&fcf->list);
Robert Love8d55e502012-05-22 19:06:26 -0700867 list_add(&fcf->list, &del_list);
Joe Eykholtf018b732010-03-12 16:08:55 -0800868 stats->VLinkFailureCount++;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700869 } else {
870 if (time_after(next_timer, deadline))
871 next_timer = deadline;
872 if (fcoe_ctlr_mtu_valid(fcf) &&
873 (!sel_time || time_before(sel_time, fcf->time)))
874 sel_time = fcf->time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700875 }
876 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700877 put_cpu();
Robert Love8d55e502012-05-22 19:06:26 -0700878
879 list_for_each_entry_safe(fcf, next, &del_list, list) {
880 /* Removes fcf from current list */
881 fcoe_sysfs_fcf_del(fcf);
882 }
883
Joe Eykholtd99ee452010-06-11 16:44:15 -0700884 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700885 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
886 fip->sel_time = sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700887 }
Joe Eykholtd99ee452010-06-11 16:44:15 -0700888
Joe Eykholt8690cb82010-06-11 16:44:10 -0700889 return next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -0700890}
891
892/**
Robert Love70b51aa2009-11-03 11:47:45 -0800893 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800894 * @fip: The FCoE controller receiving the advertisement
Robert Love70b51aa2009-11-03 11:47:45 -0800895 * @skb: The received FIP advertisement frame
896 * @fcf: The resulting FCF entry
Joe Eykholt97c83892009-03-17 11:42:40 -0700897 *
898 * Returns zero on a valid parsed advertisement,
899 * otherwise returns non zero value.
900 */
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800901static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
902 struct sk_buff *skb, struct fcoe_fcf *fcf)
Joe Eykholt97c83892009-03-17 11:42:40 -0700903{
904 struct fip_header *fiph;
905 struct fip_desc *desc = NULL;
906 struct fip_wwn_desc *wwn;
907 struct fip_fab_desc *fab;
908 struct fip_fka_desc *fka;
909 unsigned long t;
910 size_t rlen;
911 size_t dlen;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700912 u32 desc_mask;
Joe Eykholt97c83892009-03-17 11:42:40 -0700913
914 memset(fcf, 0, sizeof(*fcf));
915 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
916
917 fiph = (struct fip_header *)skb->data;
918 fcf->flags = ntohs(fiph->fip_flags);
919
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700920 /*
921 * mask of required descriptors. validating each one clears its bit.
922 */
923 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
924 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
925
Joe Eykholt97c83892009-03-17 11:42:40 -0700926 rlen = ntohs(fiph->fip_dl_len) * 4;
927 if (rlen + sizeof(*fiph) > skb->len)
928 return -EINVAL;
929
930 desc = (struct fip_desc *)(fiph + 1);
931 while (rlen > 0) {
932 dlen = desc->fip_dlen * FIP_BPW;
933 if (dlen < sizeof(*desc) || dlen > rlen)
934 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700935 /* Drop Adv if there are duplicate critical descriptors */
936 if ((desc->fip_dtype < 32) &&
937 !(desc_mask & 1U << desc->fip_dtype)) {
938 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
939 "Descriptors in FIP adv\n");
940 return -EINVAL;
941 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700942 switch (desc->fip_dtype) {
943 case FIP_DT_PRI:
944 if (dlen != sizeof(struct fip_pri_desc))
945 goto len_err;
946 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700947 desc_mask &= ~BIT(FIP_DT_PRI);
Joe Eykholt97c83892009-03-17 11:42:40 -0700948 break;
949 case FIP_DT_MAC:
950 if (dlen != sizeof(struct fip_mac_desc))
951 goto len_err;
952 memcpy(fcf->fcf_mac,
953 ((struct fip_mac_desc *)desc)->fd_mac,
954 ETH_ALEN);
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -0800955 memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700956 if (!is_valid_ether_addr(fcf->fcf_mac)) {
Joe Eykholte10f8c62010-07-20 15:20:30 -0700957 LIBFCOE_FIP_DBG(fip,
958 "Invalid MAC addr %pM in FIP adv\n",
959 fcf->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -0700960 return -EINVAL;
961 }
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700962 desc_mask &= ~BIT(FIP_DT_MAC);
Joe Eykholt97c83892009-03-17 11:42:40 -0700963 break;
964 case FIP_DT_NAME:
965 if (dlen != sizeof(struct fip_wwn_desc))
966 goto len_err;
967 wwn = (struct fip_wwn_desc *)desc;
968 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700969 desc_mask &= ~BIT(FIP_DT_NAME);
Joe Eykholt97c83892009-03-17 11:42:40 -0700970 break;
971 case FIP_DT_FAB:
972 if (dlen != sizeof(struct fip_fab_desc))
973 goto len_err;
974 fab = (struct fip_fab_desc *)desc;
975 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
976 fcf->vfid = ntohs(fab->fd_vfid);
977 fcf->fc_map = ntoh24(fab->fd_map);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700978 desc_mask &= ~BIT(FIP_DT_FAB);
Joe Eykholt97c83892009-03-17 11:42:40 -0700979 break;
980 case FIP_DT_FKA:
981 if (dlen != sizeof(struct fip_fka_desc))
982 goto len_err;
983 fka = (struct fip_fka_desc *)desc;
Yi Zou8cdffdc2009-11-20 14:54:57 -0800984 if (fka->fd_flags & FIP_FKA_ADV_D)
985 fcf->fd_flags = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -0700986 t = ntohl(fka->fd_fka_period);
987 if (t >= FCOE_CTLR_MIN_FKA)
988 fcf->fka_period = msecs_to_jiffies(t);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700989 desc_mask &= ~BIT(FIP_DT_FKA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700990 break;
991 case FIP_DT_MAP_OUI:
992 case FIP_DT_FCOE_SIZE:
993 case FIP_DT_FLOGI:
994 case FIP_DT_FDISC:
995 case FIP_DT_LOGO:
996 case FIP_DT_ELP:
997 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800998 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -0700999 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -07001000 /* standard says ignore unknown descriptors >= 128 */
Hannes Reinecke6a551c12016-07-04 10:29:18 +02001001 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
Joe Eykholt97c83892009-03-17 11:42:40 -07001002 return -EINVAL;
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -07001003 break;
Joe Eykholt97c83892009-03-17 11:42:40 -07001004 }
1005 desc = (struct fip_desc *)((char *)desc + dlen);
1006 rlen -= dlen;
1007 }
1008 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
1009 return -EINVAL;
Vasu Dev240778e2010-07-20 15:21:33 -07001010 if (!fcf->switch_name)
Joe Eykholt97c83892009-03-17 11:42:40 -07001011 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001012 if (desc_mask) {
1013 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
1014 desc_mask);
1015 return -EINVAL;
1016 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001017 return 0;
1018
1019len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001020 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -07001021 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -07001022 return -EINVAL;
1023}
1024
1025/**
Robert Love70b51aa2009-11-03 11:47:45 -08001026 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
1027 * @fip: The FCoE controller receiving the advertisement
1028 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001029 */
1030static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1031{
1032 struct fcoe_fcf *fcf;
1033 struct fcoe_fcf new;
Joe Eykholt97c83892009-03-17 11:42:40 -07001034 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
1035 int first = 0;
1036 int mtu_valid;
Robert Love8d55e502012-05-22 19:06:26 -07001037 int found = 0;
1038 int rc = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001039
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001040 if (fcoe_ctlr_parse_adv(fip, skb, &new))
Joe Eykholt97c83892009-03-17 11:42:40 -07001041 return;
1042
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001043 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001044 first = list_empty(&fip->fcfs);
Joe Eykholt97c83892009-03-17 11:42:40 -07001045 list_for_each_entry(fcf, &fip->fcfs, list) {
1046 if (fcf->switch_name == new.switch_name &&
1047 fcf->fabric_name == new.fabric_name &&
1048 fcf->fc_map == new.fc_map &&
Joe Perches6942df72013-09-02 03:32:33 +00001049 ether_addr_equal(fcf->fcf_mac, new.fcf_mac)) {
Robert Love8d55e502012-05-22 19:06:26 -07001050 found = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001051 break;
1052 }
1053 }
1054 if (!found) {
1055 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1056 goto out;
1057
1058 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1059 if (!fcf)
1060 goto out;
1061
Joe Eykholt97c83892009-03-17 11:42:40 -07001062 memcpy(fcf, &new, sizeof(new));
Robert Love8d55e502012-05-22 19:06:26 -07001063 fcf->fip = fip;
1064 rc = fcoe_sysfs_fcf_add(fcf);
1065 if (rc) {
1066 printk(KERN_ERR "Failed to allocate sysfs instance "
1067 "for FCF, fab %16.16llx mac %pM\n",
1068 new.fabric_name, new.fcf_mac);
1069 kfree(fcf);
1070 goto out;
1071 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001072 } else {
1073 /*
Joe Eykholtc600fea2010-06-11 16:44:20 -07001074 * Update the FCF's keep-alive descriptor flags.
1075 * Other flag changes from new advertisements are
1076 * ignored after a solicited advertisement is
1077 * received and the FCF is selectable (usable).
Joe Eykholt97c83892009-03-17 11:42:40 -07001078 */
Joe Eykholtc600fea2010-06-11 16:44:20 -07001079 fcf->fd_flags = new.fd_flags;
1080 if (!fcoe_ctlr_fcf_usable(fcf))
1081 fcf->flags = new.flags;
1082
Joe Eykholtd99ee452010-06-11 16:44:15 -07001083 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001084 fip->ctlr_ka_time -= fcf->fka_period;
1085 fip->ctlr_ka_time += new.fka_period;
1086 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1087 mod_timer(&fip->timer, fip->ctlr_ka_time);
Joe Eykholtc600fea2010-06-11 16:44:20 -07001088 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001089 fcf->fka_period = new.fka_period;
1090 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1091 }
Robert Love8d55e502012-05-22 19:06:26 -07001092
Joe Eykholt97c83892009-03-17 11:42:40 -07001093 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1094 fcf->time = jiffies;
Joe Eykholt9069f5c2010-11-30 16:20:02 -08001095 if (!found)
1096 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1097 fcf->fabric_name, fcf->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -07001098
1099 /*
1100 * If this advertisement is not solicited and our max receive size
1101 * hasn't been verified, send a solicited advertisement.
1102 */
1103 if (!mtu_valid)
1104 fcoe_ctlr_solicit(fip, fcf);
1105
1106 /*
1107 * If its been a while since we did a solicit, and this is
1108 * the first advertisement we've received, do a multicast
1109 * solicitation to gather as many advertisements as we can
1110 * before selection occurs.
1111 */
1112 if (first && time_after(jiffies, fip->sol_time + sol_tov))
1113 fcoe_ctlr_solicit(fip, NULL);
1114
1115 /*
Joe Eykholt981c1152010-11-30 16:20:07 -08001116 * Put this FCF at the head of the list for priority among equals.
1117 * This helps in the case of an NPV switch which insists we use
1118 * the FCF that answers multicast solicitations, not the others that
1119 * are sending periodic multicast advertisements.
1120 */
Kirill A. Shutemov63ce2492011-04-01 16:06:09 -07001121 if (mtu_valid)
1122 list_move(&fcf->list, &fip->fcfs);
Joe Eykholt981c1152010-11-30 16:20:07 -08001123
1124 /*
Joe Eykholt97c83892009-03-17 11:42:40 -07001125 * If this is the first validated FCF, note the time and
1126 * set a timer to trigger selection.
1127 */
Usha Ketinenieac00c82016-02-29 03:36:52 -08001128 if (mtu_valid && !fip->sel_fcf && !fip->sel_time &&
1129 fcoe_ctlr_fcf_usable(fcf)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001130 fip->sel_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001131 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
Joe Eykholt97c83892009-03-17 11:42:40 -07001132 if (!timer_pending(&fip->timer) ||
1133 time_before(fip->sel_time, fip->timer.expires))
1134 mod_timer(&fip->timer, fip->sel_time);
1135 }
Robert Love8d55e502012-05-22 19:06:26 -07001136
Joe Eykholt97c83892009-03-17 11:42:40 -07001137out:
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001138 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001139}
1140
1141/**
Robert Love70b51aa2009-11-03 11:47:45 -08001142 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1143 * @fip: The FCoE controller which received the packet
1144 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001145 */
1146static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1147{
Robert Love70b51aa2009-11-03 11:47:45 -08001148 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -07001149 struct fip_header *fiph;
Chris Leech11b56182009-11-03 11:46:29 -08001150 struct fc_frame *fp = (struct fc_frame *)skb;
Joe Eykholt97c83892009-03-17 11:42:40 -07001151 struct fc_frame_header *fh = NULL;
1152 struct fip_desc *desc;
1153 struct fip_encaps *els;
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -08001154 struct fcoe_fcf *sel;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001155 struct fc_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001156 enum fip_desc_type els_dtype = 0;
1157 u8 els_op;
1158 u8 sub;
1159 u8 granted_mac[ETH_ALEN] = { 0 };
1160 size_t els_len = 0;
1161 size_t rlen;
1162 size_t dlen;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001163 u32 desc_mask = 0;
1164 u32 desc_cnt = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001165
1166 fiph = (struct fip_header *)skb->data;
1167 sub = fiph->fip_subcode;
1168 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1169 goto drop;
1170
1171 rlen = ntohs(fiph->fip_dl_len) * 4;
1172 if (rlen + sizeof(*fiph) > skb->len)
1173 goto drop;
1174
1175 desc = (struct fip_desc *)(fiph + 1);
1176 while (rlen > 0) {
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001177 desc_cnt++;
Joe Eykholt97c83892009-03-17 11:42:40 -07001178 dlen = desc->fip_dlen * FIP_BPW;
1179 if (dlen < sizeof(*desc) || dlen > rlen)
1180 goto drop;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001181 /* Drop ELS if there are duplicate critical descriptors */
1182 if (desc->fip_dtype < 32) {
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -08001183 if ((desc->fip_dtype != FIP_DT_MAC) &&
1184 (desc_mask & 1U << desc->fip_dtype)) {
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001185 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1186 "Descriptors in FIP ELS\n");
1187 goto drop;
1188 }
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001189 desc_mask |= (1 << desc->fip_dtype);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001190 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001191 switch (desc->fip_dtype) {
1192 case FIP_DT_MAC:
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -08001193 sel = fip->sel_fcf;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001194 if (desc_cnt == 1) {
1195 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1196 "received out of order\n");
1197 goto drop;
1198 }
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -08001199 /*
1200 * Some switch implementations send two MAC descriptors,
1201 * with first MAC(granted_mac) being the FPMA, and the
1202 * second one(fcoe_mac) is used as destination address
1203 * for sending/receiving FCoE packets. FIP traffic is
1204 * sent using fip_mac. For regular switches, both
1205 * fip_mac and fcoe_mac would be the same.
1206 */
1207 if (desc_cnt == 2)
1208 memcpy(granted_mac,
1209 ((struct fip_mac_desc *)desc)->fd_mac,
1210 ETH_ALEN);
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001211
Joe Eykholt97c83892009-03-17 11:42:40 -07001212 if (dlen != sizeof(struct fip_mac_desc))
1213 goto len_err;
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -08001214
1215 if ((desc_cnt == 3) && (sel))
1216 memcpy(sel->fcoe_mac,
1217 ((struct fip_mac_desc *)desc)->fd_mac,
1218 ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -07001219 break;
1220 case FIP_DT_FLOGI:
1221 case FIP_DT_FDISC:
1222 case FIP_DT_LOGO:
1223 case FIP_DT_ELP:
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001224 if (desc_cnt != 1) {
1225 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1226 "received out of order\n");
1227 goto drop;
1228 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001229 if (fh)
1230 goto drop;
1231 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1232 goto len_err;
1233 els_len = dlen - sizeof(*els);
1234 els = (struct fip_encaps *)desc;
1235 fh = (struct fc_frame_header *)(els + 1);
1236 els_dtype = desc->fip_dtype;
1237 break;
1238 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001239 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -07001240 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -07001241 /* standard says ignore unknown descriptors >= 128 */
Hannes Reinecke6a551c12016-07-04 10:29:18 +02001242 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
Joe Eykholt97c83892009-03-17 11:42:40 -07001243 goto drop;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001244 if (desc_cnt <= 2) {
1245 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1246 "received out of order\n");
1247 goto drop;
1248 }
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -07001249 break;
Joe Eykholt97c83892009-03-17 11:42:40 -07001250 }
1251 desc = (struct fip_desc *)((char *)desc + dlen);
1252 rlen -= dlen;
1253 }
1254
1255 if (!fh)
1256 goto drop;
1257 els_op = *(u8 *)(fh + 1);
1258
Joe Eykholte10f8c62010-07-20 15:20:30 -07001259 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
Joe Eykholt794d98e2010-11-30 16:19:56 -08001260 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1261 if (els_op == ELS_LS_ACC) {
1262 if (!is_valid_ether_addr(granted_mac)) {
1263 LIBFCOE_FIP_DBG(fip,
1264 "Invalid MAC address %pM in FIP ELS\n",
1265 granted_mac);
1266 goto drop;
1267 }
1268 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001269
Joe Eykholt794d98e2010-11-30 16:19:56 -08001270 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1271 fip->flogi_oxid = FC_XID_UNKNOWN;
1272 if (els_dtype == FIP_DT_FLOGI)
1273 fcoe_ctlr_announce(fip);
1274 }
1275 } else if (els_dtype == FIP_DT_FLOGI &&
1276 !fcoe_ctlr_flogi_retry(fip))
1277 goto drop; /* retrying FLOGI so drop reject */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001278 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001279
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001280 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1281 (!(1U << FIP_DT_MAC & desc_mask)))) {
1282 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1283 "in FIP ELS\n");
1284 goto drop;
1285 }
1286
Joe Eykholt97c83892009-03-17 11:42:40 -07001287 /*
1288 * Convert skb into an fc_frame containing only the ELS.
1289 */
1290 skb_pull(skb, (u8 *)fh - skb->data);
1291 skb_trim(skb, els_len);
1292 fp = (struct fc_frame *)skb;
1293 fc_frame_init(fp);
1294 fr_sof(fp) = FC_SOF_I3;
1295 fr_eof(fp) = FC_EOF_T;
Robert Love70b51aa2009-11-03 11:47:45 -08001296 fr_dev(fp) = lport;
Joe Eykholte10f8c62010-07-20 15:20:30 -07001297 fr_encaps(fp) = els_dtype;
Joe Eykholt97c83892009-03-17 11:42:40 -07001298
Vasu Dev1bd49b42012-05-25 10:26:43 -07001299 stats = per_cpu_ptr(lport->stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001300 stats->RxFrames++;
1301 stats->RxWords += skb->len / FIP_BPW;
Joe Eykholtf018b732010-03-12 16:08:55 -08001302 put_cpu();
Joe Eykholt97c83892009-03-17 11:42:40 -07001303
Robert Love70b51aa2009-11-03 11:47:45 -08001304 fc_exch_recv(lport, fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001305 return;
1306
1307len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001308 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -07001309 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -07001310drop:
1311 kfree_skb(skb);
1312}
1313
1314/**
Robert Love70b51aa2009-11-03 11:47:45 -08001315 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1316 * @fip: The FCoE controller that received the frame
1317 * @fh: The received FIP header
Joe Eykholt97c83892009-03-17 11:42:40 -07001318 *
1319 * There may be multiple VN_Port descriptors.
1320 * The overall length has already been checked.
1321 */
1322static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
Chad Dupuisfd37f662016-09-30 11:01:20 +02001323 struct sk_buff *skb)
Joe Eykholt97c83892009-03-17 11:42:40 -07001324{
1325 struct fip_desc *desc;
1326 struct fip_mac_desc *mp;
1327 struct fip_wwn_desc *wp;
1328 struct fip_vn_desc *vp;
1329 size_t rlen;
1330 size_t dlen;
1331 struct fcoe_fcf *fcf = fip->sel_fcf;
Robert Love70b51aa2009-11-03 11:47:45 -08001332 struct fc_lport *lport = fip->lp;
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001333 struct fc_lport *vn_port = NULL;
1334 u32 desc_mask;
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001335 int num_vlink_desc;
1336 int reset_phys_port = 0;
1337 struct fip_vn_desc **vlink_desc_arr = NULL;
Chad Dupuisfd37f662016-09-30 11:01:20 +02001338 struct fip_header *fh = (struct fip_header *)skb->data;
1339 struct ethhdr *eh = eth_hdr(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001340
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001341 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
Robert Loved29510a2010-05-07 15:18:30 -07001342
Chad Dupuisfd37f662016-09-30 11:01:20 +02001343 if (!fcf) {
Bhanu Prakash Gollapudib2593cb2013-02-04 23:00:20 -08001344 /*
1345 * We are yet to select best FCF, but we got CVL in the
1346 * meantime. reset the ctlr and let it rediscover the FCF
1347 */
Chad Dupuisfd37f662016-09-30 11:01:20 +02001348 LIBFCOE_FIP_DBG(fip, "Resetting fcoe_ctlr as FCF has not been "
1349 "selected yet\n");
Bhanu Prakash Gollapudib2593cb2013-02-04 23:00:20 -08001350 mutex_lock(&fip->ctlr_mutex);
1351 fcoe_ctlr_reset(fip);
1352 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001353 return;
Bhanu Prakash Gollapudib2593cb2013-02-04 23:00:20 -08001354 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001355
1356 /*
Chad Dupuisfd37f662016-09-30 11:01:20 +02001357 * If we've selected an FCF check that the CVL is from there to avoid
1358 * processing CVLs from an unexpected source. If it is from an
1359 * unexpected source drop it on the floor.
1360 */
1361 if (!ether_addr_equal(eh->h_source, fcf->fcf_mac)) {
1362 LIBFCOE_FIP_DBG(fip, "Dropping CVL due to source address "
1363 "mismatch with FCF src=%pM\n", eh->h_source);
1364 return;
1365 }
1366
1367 /*
1368 * If we haven't logged into the fabric but receive a CVL we should
1369 * reset everything and go back to solicitation.
1370 */
1371 if (!lport->port_id) {
1372 LIBFCOE_FIP_DBG(fip, "lport not logged in, resoliciting\n");
1373 mutex_lock(&fip->ctlr_mutex);
1374 fcoe_ctlr_reset(fip);
1375 mutex_unlock(&fip->ctlr_mutex);
1376 fc_lport_reset(fip->lp);
1377 fcoe_ctlr_solicit(fip, NULL);
1378 return;
1379 }
1380
1381 /*
Joe Eykholt97c83892009-03-17 11:42:40 -07001382 * mask of required descriptors. Validating each one clears its bit.
1383 */
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001384 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
Joe Eykholt97c83892009-03-17 11:42:40 -07001385
1386 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1387 desc = (struct fip_desc *)(fh + 1);
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001388
1389 /*
1390 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1391 * before determining max Vx_Port descriptor but a buggy FCF could have
Masahiro Yamada0cf2a842017-02-27 14:29:39 -08001392 * omitted either or both MAC Address and Name Identifier descriptors
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001393 */
1394 num_vlink_desc = rlen / sizeof(*vp);
1395 if (num_vlink_desc)
Kees Cook6da2ec52018-06-12 13:55:00 -07001396 vlink_desc_arr = kmalloc_array(num_vlink_desc, sizeof(vp),
1397 GFP_ATOMIC);
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001398 if (!vlink_desc_arr)
1399 return;
1400 num_vlink_desc = 0;
1401
Joe Eykholt97c83892009-03-17 11:42:40 -07001402 while (rlen >= sizeof(*desc)) {
1403 dlen = desc->fip_dlen * FIP_BPW;
1404 if (dlen > rlen)
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001405 goto err;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001406 /* Drop CVL if there are duplicate critical descriptors */
1407 if ((desc->fip_dtype < 32) &&
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001408 (desc->fip_dtype != FIP_DT_VN_ID) &&
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001409 !(desc_mask & 1U << desc->fip_dtype)) {
1410 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1411 "Descriptors in FIP CVL\n");
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001412 goto err;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001413 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001414 switch (desc->fip_dtype) {
1415 case FIP_DT_MAC:
1416 mp = (struct fip_mac_desc *)desc;
1417 if (dlen < sizeof(*mp))
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001418 goto err;
Joe Perches6942df72013-09-02 03:32:33 +00001419 if (!ether_addr_equal(mp->fd_mac, fcf->fcf_mac))
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001420 goto err;
Joe Eykholt97c83892009-03-17 11:42:40 -07001421 desc_mask &= ~BIT(FIP_DT_MAC);
1422 break;
1423 case FIP_DT_NAME:
1424 wp = (struct fip_wwn_desc *)desc;
1425 if (dlen < sizeof(*wp))
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001426 goto err;
Joe Eykholt97c83892009-03-17 11:42:40 -07001427 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001428 goto err;
Joe Eykholt97c83892009-03-17 11:42:40 -07001429 desc_mask &= ~BIT(FIP_DT_NAME);
1430 break;
1431 case FIP_DT_VN_ID:
1432 vp = (struct fip_vn_desc *)desc;
1433 if (dlen < sizeof(*vp))
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001434 goto err;
1435 vlink_desc_arr[num_vlink_desc++] = vp;
1436 vn_port = fc_vport_id_lookup(lport,
1437 ntoh24(vp->fd_fc_id));
1438 if (vn_port && (vn_port == lport)) {
1439 mutex_lock(&fip->ctlr_mutex);
Vasu Dev1bd49b42012-05-25 10:26:43 -07001440 per_cpu_ptr(lport->stats,
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001441 get_cpu())->VLinkFailureCount++;
1442 put_cpu();
1443 fcoe_ctlr_reset(fip);
1444 mutex_unlock(&fip->ctlr_mutex);
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001445 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001446 break;
1447 default:
1448 /* standard says ignore unknown descriptors >= 128 */
Hannes Reinecke6a551c12016-07-04 10:29:18 +02001449 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001450 goto err;
Joe Eykholt97c83892009-03-17 11:42:40 -07001451 break;
1452 }
1453 desc = (struct fip_desc *)((char *)desc + dlen);
1454 rlen -= dlen;
1455 }
1456
1457 /*
1458 * reset only if all required descriptors were present and valid.
1459 */
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001460 if (desc_mask)
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001461 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1462 desc_mask);
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001463 else if (!num_vlink_desc) {
1464 LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1465 /*
1466 * No Vx_Port description. Clear all NPIV ports,
1467 * followed by physical port
1468 */
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001469 mutex_lock(&fip->ctlr_mutex);
Vasu Dev1bd49b42012-05-25 10:26:43 -07001470 per_cpu_ptr(lport->stats, get_cpu())->VLinkFailureCount++;
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001471 put_cpu();
1472 fcoe_ctlr_reset(fip);
1473 mutex_unlock(&fip->ctlr_mutex);
1474
Bhanu Prakash Gollapudi14619ea2012-03-09 14:49:53 -08001475 mutex_lock(&lport->lp_mutex);
1476 list_for_each_entry(vn_port, &lport->vports, list)
1477 fc_lport_reset(vn_port);
1478 mutex_unlock(&lport->lp_mutex);
1479
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001480 fc_lport_reset(fip->lp);
1481 fcoe_ctlr_solicit(fip, NULL);
1482 } else {
1483 int i;
1484
1485 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1486 for (i = 0; i < num_vlink_desc; i++) {
1487 vp = vlink_desc_arr[i];
1488 vn_port = fc_vport_id_lookup(lport,
1489 ntoh24(vp->fd_fc_id));
1490 if (!vn_port)
1491 continue;
1492
1493 /*
1494 * 'port_id' is already validated, check MAC address and
1495 * wwpn
1496 */
Joe Perches6942df72013-09-02 03:32:33 +00001497 if (!ether_addr_equal(fip->get_src_addr(vn_port),
1498 vp->fd_mac) ||
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001499 get_unaligned_be64(&vp->fd_wwpn) !=
1500 vn_port->wwpn)
1501 continue;
1502
1503 if (vn_port == lport)
1504 /*
1505 * Physical port, defer processing till all
1506 * listed NPIV ports are cleared
1507 */
1508 reset_phys_port = 1;
1509 else /* NPIV port */
1510 fc_lport_reset(vn_port);
1511 }
1512
1513 if (reset_phys_port) {
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001514 fc_lport_reset(fip->lp);
1515 fcoe_ctlr_solicit(fip, NULL);
1516 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001517 }
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001518
1519err:
1520 kfree(vlink_desc_arr);
Joe Eykholt97c83892009-03-17 11:42:40 -07001521}
1522
1523/**
Robert Love70b51aa2009-11-03 11:47:45 -08001524 * fcoe_ctlr_recv() - Receive a FIP packet
1525 * @fip: The FCoE controller that received the packet
1526 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001527 *
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001528 * This may be called from either NET_RX_SOFTIRQ or IRQ.
Joe Eykholt97c83892009-03-17 11:42:40 -07001529 */
1530void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1531{
Neil Hormanc0866282013-08-03 10:45:53 +00001532 skb = skb_share_check(skb, GFP_ATOMIC);
1533 if (!skb)
1534 return;
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001535 skb_queue_tail(&fip->fip_recv_list, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001536 schedule_work(&fip->recv_work);
1537}
1538EXPORT_SYMBOL(fcoe_ctlr_recv);
1539
1540/**
Robert Love70b51aa2009-11-03 11:47:45 -08001541 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1542 * @fip: The FCoE controller that received the frame
1543 * @skb: The received FIP frame
Joe Eykholt97c83892009-03-17 11:42:40 -07001544 *
1545 * Returns non-zero if the frame is dropped.
1546 */
1547static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1548{
1549 struct fip_header *fiph;
1550 struct ethhdr *eh;
1551 enum fip_state state;
Hannes Reinecke9a6cf882016-07-19 13:49:40 +02001552 bool fip_vlan_resp = false;
Joe Eykholt97c83892009-03-17 11:42:40 -07001553 u16 op;
1554 u8 sub;
1555
1556 if (skb_linearize(skb))
1557 goto drop;
1558 if (skb->len < sizeof(*fiph))
1559 goto drop;
1560 eh = eth_hdr(skb);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001561 if (fip->mode == FIP_MODE_VN2VN) {
Joe Perches6942df72013-09-02 03:32:33 +00001562 if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1563 !ether_addr_equal(eh->h_dest, fcoe_all_vn2vn) &&
1564 !ether_addr_equal(eh->h_dest, fcoe_all_p2p))
Joe Eykholte10f8c62010-07-20 15:20:30 -07001565 goto drop;
Joe Perches6942df72013-09-02 03:32:33 +00001566 } else if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1567 !ether_addr_equal(eh->h_dest, fcoe_all_enode))
Joe Eykholt97c83892009-03-17 11:42:40 -07001568 goto drop;
1569 fiph = (struct fip_header *)skb->data;
1570 op = ntohs(fiph->fip_op);
1571 sub = fiph->fip_subcode;
1572
Joe Eykholt97c83892009-03-17 11:42:40 -07001573 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1574 goto drop;
1575 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1576 goto drop;
1577
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001578 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001579 state = fip->state;
1580 if (state == FIP_ST_AUTO) {
1581 fip->map_dest = 0;
Joe Eykholt9b651da2010-07-20 15:20:24 -07001582 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -07001583 state = FIP_ST_ENABLED;
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001584 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001585 }
Hannes Reinecke9a6cf882016-07-19 13:49:40 +02001586 fip_vlan_resp = fip->fip_resp;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001587 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001588
1589 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1590 return fcoe_ctlr_vn_recv(fip, skb);
1591
Hannes Reinecke9a6cf882016-07-19 13:49:40 +02001592 if (fip_vlan_resp && op == FIP_OP_VLAN) {
1593 LIBFCOE_FIP_DBG(fip, "fip vlan discovery\n");
1594 return fcoe_ctlr_vlan_recv(fip, skb);
1595 }
1596
Joe Eykholte10f8c62010-07-20 15:20:30 -07001597 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1598 state != FIP_ST_VNMP_CLAIM)
Joe Eykholt97c83892009-03-17 11:42:40 -07001599 goto drop;
1600
1601 if (op == FIP_OP_LS) {
1602 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1603 return 0;
1604 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07001605
1606 if (state != FIP_ST_ENABLED)
1607 goto drop;
1608
Joe Eykholt97c83892009-03-17 11:42:40 -07001609 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1610 fcoe_ctlr_recv_adv(fip, skb);
1611 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
Chad Dupuisfd37f662016-09-30 11:01:20 +02001612 fcoe_ctlr_recv_clr_vlink(fip, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001613 kfree_skb(skb);
1614 return 0;
1615drop:
1616 kfree_skb(skb);
1617 return -1;
1618}
1619
1620/**
Robert Love70b51aa2009-11-03 11:47:45 -08001621 * fcoe_ctlr_select() - Select the best FCF (if possible)
1622 * @fip: The FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001623 *
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001624 * Returns the selected FCF, or NULL if none are usable.
1625 *
Joe Eykholt97c83892009-03-17 11:42:40 -07001626 * If there are conflicting advertisements, no FCF can be chosen.
1627 *
Joe Eykholt794d98e2010-11-30 16:19:56 -08001628 * If there is already a selected FCF, this will choose a better one or
1629 * an equivalent one that hasn't already been sent a FLOGI.
1630 *
Joe Eykholt97c83892009-03-17 11:42:40 -07001631 * Called with lock held.
1632 */
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001633static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -07001634{
1635 struct fcoe_fcf *fcf;
Joe Eykholt794d98e2010-11-30 16:19:56 -08001636 struct fcoe_fcf *best = fip->sel_fcf;
Joe Eykholt97c83892009-03-17 11:42:40 -07001637
1638 list_for_each_entry(fcf, &fip->fcfs, list) {
Joe Eykholt9069f5c2010-11-30 16:20:02 -08001639 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1640 "VFID %d mac %pM map %x val %d "
1641 "sent %u pri %u\n",
1642 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1643 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1644 fcf->flogi_sent, fcf->pri);
Joe Eykholt97c83892009-03-17 11:42:40 -07001645 if (!fcoe_ctlr_fcf_usable(fcf)) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001646 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1647 "map %x %svalid %savailable\n",
1648 fcf->fabric_name, fcf->fc_map,
1649 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1650 (fcf->flags & FIP_FL_AVAIL) ?
1651 "" : "un");
Joe Eykholt97c83892009-03-17 11:42:40 -07001652 continue;
1653 }
Krishna Mohane6c10b72013-03-01 11:35:31 +00001654 if (!best || fcf->pri < best->pri || best->flogi_sent)
1655 best = fcf;
1656 if (fcf->fabric_name != best->fabric_name ||
1657 fcf->vfid != best->vfid ||
1658 fcf->fc_map != best->fc_map) {
Bhanu Prakash Gollapudi1f953b02013-01-28 11:43:03 -08001659 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1660 "or FC-MAP\n");
1661 return NULL;
1662 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001663 }
1664 fip->sel_fcf = best;
Joe Eykholtc47036a2010-11-30 16:19:46 -08001665 if (best) {
Joe Eykholt9069f5c2010-11-30 16:20:02 -08001666 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
Joe Eykholtc47036a2010-11-30 16:19:46 -08001667 fip->port_ka_time = jiffies +
1668 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1669 fip->ctlr_ka_time = jiffies + best->fka_period;
1670 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1671 mod_timer(&fip->timer, fip->ctlr_ka_time);
1672 }
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001673 return best;
Joe Eykholt97c83892009-03-17 11:42:40 -07001674}
1675
1676/**
Joe Eykholt794d98e2010-11-30 16:19:56 -08001677 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1678 * @fip: The FCoE controller
1679 *
1680 * Returns non-zero error if it could not be sent.
1681 *
1682 * Called with ctlr_mutex and ctlr_lock held.
1683 * Caller must verify that fip->sel_fcf is not NULL.
1684 */
1685static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1686{
1687 struct sk_buff *skb;
1688 struct sk_buff *skb_orig;
1689 struct fc_frame_header *fh;
1690 int error;
1691
1692 skb_orig = fip->flogi_req;
1693 if (!skb_orig)
1694 return -EINVAL;
1695
1696 /*
1697 * Clone and send the FLOGI request. If clone fails, use original.
1698 */
1699 skb = skb_clone(skb_orig, GFP_ATOMIC);
1700 if (!skb) {
1701 skb = skb_orig;
1702 fip->flogi_req = NULL;
1703 }
1704 fh = (struct fc_frame_header *)skb->data;
1705 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1706 ntoh24(fh->fh_d_id));
1707 if (error) {
1708 kfree_skb(skb);
1709 return error;
1710 }
1711 fip->send(fip, skb);
1712 fip->sel_fcf->flogi_sent = 1;
1713 return 0;
1714}
1715
1716/**
1717 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1718 * @fip: The FCoE controller
1719 *
1720 * Returns non-zero error code if there's no FLOGI request to retry or
1721 * no alternate FCF available.
1722 */
1723static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1724{
1725 struct fcoe_fcf *fcf;
1726 int error;
1727
1728 mutex_lock(&fip->ctlr_mutex);
1729 spin_lock_bh(&fip->ctlr_lock);
1730 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001731 fcf = fcoe_ctlr_select(fip);
Joe Eykholt794d98e2010-11-30 16:19:56 -08001732 if (!fcf || fcf->flogi_sent) {
1733 kfree_skb(fip->flogi_req);
1734 fip->flogi_req = NULL;
1735 error = -ENOENT;
1736 } else {
1737 fcoe_ctlr_solicit(fip, NULL);
1738 error = fcoe_ctlr_flogi_send_locked(fip);
1739 }
1740 spin_unlock_bh(&fip->ctlr_lock);
1741 mutex_unlock(&fip->ctlr_mutex);
1742 return error;
1743}
1744
1745
1746/**
1747 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1748 * @fip: The FCoE controller that timed out
1749 *
1750 * Done here because fcoe_ctlr_els_send() can't get mutex.
1751 *
1752 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1753 */
1754static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1755{
1756 struct fcoe_fcf *fcf;
1757
1758 spin_lock_bh(&fip->ctlr_lock);
1759 fcf = fip->sel_fcf;
1760 if (!fcf || !fip->flogi_req_send)
1761 goto unlock;
1762
1763 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1764
1765 /*
1766 * If this FLOGI is being sent due to a timeout retry
1767 * to the same FCF as before, select a different FCF if possible.
1768 */
1769 if (fcf->flogi_sent) {
1770 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001771 fcf = fcoe_ctlr_select(fip);
Joe Eykholt794d98e2010-11-30 16:19:56 -08001772 if (!fcf || fcf->flogi_sent) {
1773 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1774 list_for_each_entry(fcf, &fip->fcfs, list)
1775 fcf->flogi_sent = 0;
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001776 fcf = fcoe_ctlr_select(fip);
Joe Eykholt794d98e2010-11-30 16:19:56 -08001777 }
1778 }
1779 if (fcf) {
1780 fcoe_ctlr_flogi_send_locked(fip);
1781 fip->flogi_req_send = 0;
1782 } else /* XXX */
1783 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1784unlock:
1785 spin_unlock_bh(&fip->ctlr_lock);
1786}
1787
1788/**
Robert Love70b51aa2009-11-03 11:47:45 -08001789 * fcoe_ctlr_timeout() - FIP timeout handler
1790 * @arg: The FCoE controller that timed out
Joe Eykholt97c83892009-03-17 11:42:40 -07001791 */
Kees Cooke99e88a2017-10-16 14:43:17 -07001792static void fcoe_ctlr_timeout(struct timer_list *t)
Joe Eykholt97c83892009-03-17 11:42:40 -07001793{
Kees Cooke99e88a2017-10-16 14:43:17 -07001794 struct fcoe_ctlr *fip = from_timer(fip, t, timer);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001795
1796 schedule_work(&fip->timer_work);
1797}
1798
1799/**
1800 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1801 * @work: Handle to a FCoE controller
1802 *
1803 * Ages FCFs. Triggers FCF selection if possible.
1804 * Sends keep-alives and resets.
1805 */
1806static void fcoe_ctlr_timer_work(struct work_struct *work)
1807{
1808 struct fcoe_ctlr *fip;
1809 struct fc_lport *vport;
1810 u8 *mac;
1811 u8 reset = 0;
1812 u8 send_ctlr_ka = 0;
1813 u8 send_port_ka = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001814 struct fcoe_fcf *sel;
1815 struct fcoe_fcf *fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001816 unsigned long next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -07001817
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001818 fip = container_of(work, struct fcoe_ctlr, timer_work);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001819 if (fip->mode == FIP_MODE_VN2VN)
1820 return fcoe_ctlr_vn_timeout(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001821 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001822 if (fip->state == FIP_ST_DISABLED) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001823 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001824 return;
1825 }
1826
1827 fcf = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001828 next_timer = fcoe_ctlr_age_fcfs(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001829
1830 sel = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001831 if (!sel && fip->sel_time) {
1832 if (time_after_eq(jiffies, fip->sel_time)) {
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001833 sel = fcoe_ctlr_select(fip);
Joe Eykholt8690cb82010-06-11 16:44:10 -07001834 fip->sel_time = 0;
1835 } else if (time_after(next_timer, fip->sel_time))
1836 next_timer = fip->sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001837 }
1838
Joe Eykholt794d98e2010-11-30 16:19:56 -08001839 if (sel && fip->flogi_req_send)
1840 fcoe_ctlr_flogi_send(fip);
1841 else if (!sel && fcf)
1842 reset = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001843
Yi Zou8cdffdc2009-11-20 14:54:57 -08001844 if (sel && !sel->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001845 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1846 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001847 send_ctlr_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001848 }
1849 if (time_after(next_timer, fip->ctlr_ka_time))
1850 next_timer = fip->ctlr_ka_time;
1851
1852 if (time_after_eq(jiffies, fip->port_ka_time)) {
Bhanu Prakash Gollapudif47dd852010-01-21 10:16:00 -08001853 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001854 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001855 send_port_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001856 }
1857 if (time_after(next_timer, fip->port_ka_time))
1858 next_timer = fip->port_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001859 }
Joe Eykholt8690cb82010-06-11 16:44:10 -07001860 if (!list_empty(&fip->fcfs))
1861 mod_timer(&fip->timer, next_timer);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001862 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001863
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001864 if (reset) {
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001865 fc_lport_reset(fip->lp);
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001866 /* restart things with a solicitation */
1867 fcoe_ctlr_solicit(fip, NULL);
1868 }
Chris Leech11b56182009-11-03 11:46:29 -08001869
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001870 if (send_ctlr_ka)
Chris Leech11b56182009-11-03 11:46:29 -08001871 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001872
1873 if (send_port_ka) {
Chris Leech11b56182009-11-03 11:46:29 -08001874 mutex_lock(&fip->lp->lp_mutex);
1875 mac = fip->get_src_addr(fip->lp);
1876 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1877 list_for_each_entry(vport, &fip->lp->vports, list) {
1878 mac = fip->get_src_addr(vport);
1879 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1880 }
1881 mutex_unlock(&fip->lp->lp_mutex);
1882 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001883}
1884
1885/**
Robert Love70b51aa2009-11-03 11:47:45 -08001886 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1887 * @recv_work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001888 */
1889static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1890{
1891 struct fcoe_ctlr *fip;
1892 struct sk_buff *skb;
1893
1894 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001895 while ((skb = skb_dequeue(&fip->fip_recv_list)))
Joe Eykholt97c83892009-03-17 11:42:40 -07001896 fcoe_ctlr_recv_handler(fip, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001897}
1898
1899/**
Joe Eykholt386309ce2009-11-03 11:49:16 -08001900 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
Robert Love70b51aa2009-11-03 11:47:45 -08001901 * @fip: The FCoE controller
1902 * @fp: The FC frame to snoop
Joe Eykholt97c83892009-03-17 11:42:40 -07001903 *
1904 * Snoop potential response to FLOGI or even incoming FLOGI.
1905 *
1906 * The caller has checked that we are waiting for login as indicated
1907 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1908 *
1909 * The caller is responsible for freeing the frame.
Joe Eykholt386309ce2009-11-03 11:49:16 -08001910 * Fill in the granted_mac address.
Joe Eykholt97c83892009-03-17 11:42:40 -07001911 *
1912 * Return non-zero if the frame should not be delivered to libfc.
1913 */
Chris Leech11b56182009-11-03 11:46:29 -08001914int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholt386309ce2009-11-03 11:49:16 -08001915 struct fc_frame *fp)
Joe Eykholt97c83892009-03-17 11:42:40 -07001916{
1917 struct fc_frame_header *fh;
1918 u8 op;
Joe Eykholt386309ce2009-11-03 11:49:16 -08001919 u8 *sa;
Joe Eykholt97c83892009-03-17 11:42:40 -07001920
Joe Eykholt386309ce2009-11-03 11:49:16 -08001921 sa = eth_hdr(&fp->skb)->h_source;
Joe Eykholt97c83892009-03-17 11:42:40 -07001922 fh = fc_frame_header_get(fp);
1923 if (fh->fh_type != FC_TYPE_ELS)
1924 return 0;
1925
1926 op = fc_frame_payload_op(fp);
1927 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1928 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1929
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001930 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001931 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001932 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001933 return -EINVAL;
1934 }
Joe Eykholt9b651da2010-07-20 15:20:24 -07001935 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001936 LIBFCOE_FIP_DBG(fip,
1937 "received FLOGI LS_ACC using non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001938
1939 /*
1940 * FLOGI accepted.
1941 * If the src mac addr is FC_OUI-based, then we mark the
1942 * address_mode flag to use FC_OUI-based Ethernet DA.
1943 * Otherwise we use the FCoE gateway addr
1944 */
Joe Perches6942df72013-09-02 03:32:33 +00001945 if (ether_addr_equal(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
Joe Eykholtcd229e42010-07-20 15:20:40 -07001946 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001947 } else {
1948 memcpy(fip->dest_addr, sa, ETH_ALEN);
1949 fip->map_dest = 0;
1950 }
1951 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001952 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt386309ce2009-11-03 11:49:16 -08001953 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
Joe Eykholt97c83892009-03-17 11:42:40 -07001954 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1955 /*
1956 * Save source MAC for point-to-point responses.
1957 */
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001958 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001959 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1960 memcpy(fip->dest_addr, sa, ETH_ALEN);
1961 fip->map_dest = 0;
Joe Eykholte49bf612010-03-12 16:07:57 -08001962 if (fip->state == FIP_ST_AUTO)
1963 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1964 "Setting non-FIP mode\n");
Joe Eykholt9b651da2010-07-20 15:20:24 -07001965 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -07001966 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001967 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001968 }
1969 return 0;
1970}
1971EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1972
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001973/**
Robert Love70b51aa2009-11-03 11:47:45 -08001974 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1975 * @mac: The MAC address to convert
1976 * @scheme: The scheme to use when converting
1977 * @port: The port indicator for converting
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001978 *
1979 * Returns: u64 fc world wide name
1980 */
1981u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1982 unsigned int scheme, unsigned int port)
1983{
1984 u64 wwn;
1985 u64 host_mac;
1986
1987 /* The MAC is in NO, so flip only the low 48 bits */
1988 host_mac = ((u64) mac[0] << 40) |
1989 ((u64) mac[1] << 32) |
1990 ((u64) mac[2] << 24) |
1991 ((u64) mac[3] << 16) |
1992 ((u64) mac[4] << 8) |
1993 (u64) mac[5];
1994
1995 WARN_ON(host_mac >= (1ULL << 48));
1996 wwn = host_mac | ((u64) scheme << 60);
1997 switch (scheme) {
1998 case 1:
1999 WARN_ON(port != 0);
2000 break;
2001 case 2:
2002 WARN_ON(port >= 0xfff);
2003 wwn |= (u64) port << 48;
2004 break;
2005 default:
2006 WARN_ON(1);
2007 break;
2008 }
2009
2010 return wwn;
2011}
2012EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
2013
2014/**
Joe Eykholte10f8c62010-07-20 15:20:30 -07002015 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
2016 * @rdata: libfc remote port
2017 */
2018static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
2019{
2020 return (struct fcoe_rport *)(rdata + 1);
2021}
2022
2023/**
2024 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
2025 * @fip: The FCoE controller
2026 * @sub: sub-opcode for probe request, reply, or advertisement.
2027 * @dest: The destination Ethernet MAC address
2028 * @min_len: minimum size of the Ethernet payload to be sent
2029 */
2030static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
2031 enum fip_vn2vn_subcode sub,
2032 const u8 *dest, size_t min_len)
2033{
2034 struct sk_buff *skb;
Hannes Reineckeb3d30f42016-07-19 13:49:39 +02002035 struct fip_vn2vn_probe_frame {
Joe Eykholte10f8c62010-07-20 15:20:30 -07002036 struct ethhdr eth;
2037 struct fip_header fip;
2038 struct fip_mac_desc mac;
2039 struct fip_wwn_desc wwnn;
2040 struct fip_vn_desc vn;
Yi Zou0095a922011-01-28 16:05:00 -08002041 } __packed * frame;
Joe Eykholte10f8c62010-07-20 15:20:30 -07002042 struct fip_fc4_feat *ff;
2043 struct fip_size_desc *size;
2044 u32 fcp_feat;
2045 size_t len;
2046 size_t dlen;
2047
2048 len = sizeof(*frame);
2049 dlen = 0;
2050 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2051 dlen = sizeof(struct fip_fc4_feat) +
2052 sizeof(struct fip_size_desc);
2053 len += dlen;
2054 }
2055 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
2056 len = max(len, min_len + sizeof(struct ethhdr));
2057
2058 skb = dev_alloc_skb(len);
2059 if (!skb)
2060 return;
2061
Hannes Reineckeb3d30f42016-07-19 13:49:39 +02002062 frame = (struct fip_vn2vn_probe_frame *)skb->data;
Joe Eykholte10f8c62010-07-20 15:20:30 -07002063 memset(frame, 0, len);
2064 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
Yi Zoud227f022012-04-20 12:16:49 -07002065
2066 if (sub == FIP_SC_VN_BEACON) {
2067 hton24(frame->eth.h_source, FIP_VN_FC_MAP);
2068 hton24(frame->eth.h_source + 3, fip->port_id);
2069 } else {
2070 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2071 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07002072 frame->eth.h_proto = htons(ETH_P_FIP);
2073
2074 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2075 frame->fip.fip_op = htons(FIP_OP_VN2VN);
2076 frame->fip.fip_subcode = sub;
2077 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2078
2079 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2080 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2081 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2082
2083 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
2084 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
2085 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
2086
2087 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2088 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2089 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2090 hton24(frame->vn.fd_mac + 3, fip->port_id);
2091 hton24(frame->vn.fd_fc_id, fip->port_id);
2092 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2093
2094 /*
2095 * For claims, add FC-4 features.
2096 * TBD: Add interface to get fc-4 types and features from libfc.
2097 */
2098 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2099 ff = (struct fip_fc4_feat *)(frame + 1);
2100 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2101 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2102 ff->fd_fts = fip->lp->fcts;
2103
2104 fcp_feat = 0;
2105 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2106 fcp_feat |= FCP_FEAT_INIT;
2107 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2108 fcp_feat |= FCP_FEAT_TARG;
2109 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2110 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2111
2112 size = (struct fip_size_desc *)(ff + 1);
2113 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2114 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2115 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2116 }
2117
2118 skb_put(skb, len);
2119 skb->protocol = htons(ETH_P_FIP);
john fastabend6f6c2aa2011-11-18 13:35:56 -08002120 skb->priority = fip->priority;
Joe Eykholte10f8c62010-07-20 15:20:30 -07002121 skb_reset_mac_header(skb);
2122 skb_reset_network_header(skb);
2123
2124 fip->send(fip, skb);
2125}
2126
2127/**
2128 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2129 * @lport: The lport which is receiving the event
2130 * @rdata: remote port private data
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002131 * @event: The event that occurred
Joe Eykholte10f8c62010-07-20 15:20:30 -07002132 *
2133 * Locking Note: The rport lock must not be held when calling this function.
2134 */
2135static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2136 struct fc_rport_priv *rdata,
2137 enum fc_rport_event event)
2138{
2139 struct fcoe_ctlr *fip = lport->disc.priv;
2140 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2141
2142 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2143 rdata->ids.port_id, event);
2144
2145 mutex_lock(&fip->ctlr_mutex);
2146 switch (event) {
2147 case RPORT_EV_READY:
2148 frport->login_count = 0;
2149 break;
2150 case RPORT_EV_LOGO:
2151 case RPORT_EV_FAILED:
2152 case RPORT_EV_STOP:
2153 frport->login_count++;
2154 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2155 LIBFCOE_FIP_DBG(fip,
2156 "rport FLOGI limited port_id %6.6x\n",
2157 rdata->ids.port_id);
Hannes Reineckec96c7922016-10-18 10:01:44 +02002158 fc_rport_logoff(rdata);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002159 }
2160 break;
2161 default:
2162 break;
2163 }
2164 mutex_unlock(&fip->ctlr_mutex);
2165}
2166
2167static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2168 .event_callback = fcoe_ctlr_vn_rport_callback,
2169};
2170
2171/**
2172 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2173 * @fip: The FCoE controller
2174 *
2175 * Called with ctlr_mutex held.
2176 */
2177static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2178{
Mark Rustadd17efa02013-05-18 04:10:07 +00002179 struct fc_rport_priv *rdata;
2180
Hannes Reinecke0993ed92018-07-11 10:09:30 +02002181 mutex_lock(&lport->disc.disc_mutex);
Hannes Reineckea407c592016-09-30 11:01:15 +02002182 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2183 if (kref_get_unless_zero(&rdata->kref)) {
Hannes Reineckec96c7922016-10-18 10:01:44 +02002184 fc_rport_logoff(rdata);
Hannes Reinecke944ef962016-10-18 10:01:39 +02002185 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reineckea407c592016-09-30 11:01:15 +02002186 }
2187 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07002188 lport->disc.disc_callback = NULL;
2189 mutex_unlock(&lport->disc.disc_mutex);
2190}
2191
2192/**
2193 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2194 * @fip: The FCoE controller
2195 *
2196 * Called through the local port template for discovery.
2197 * Called without the ctlr_mutex held.
2198 */
2199static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2200{
2201 struct fcoe_ctlr *fip = lport->disc.priv;
2202
2203 mutex_lock(&fip->ctlr_mutex);
2204 fcoe_ctlr_disc_stop_locked(lport);
2205 mutex_unlock(&fip->ctlr_mutex);
2206}
2207
2208/**
2209 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2210 * @fip: The FCoE controller
2211 *
2212 * Called through the local port template for discovery.
2213 * Called without the ctlr_mutex held.
2214 */
2215static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2216{
2217 fcoe_ctlr_disc_stop(lport);
Hannes Reinecke5922a952016-10-18 10:01:46 +02002218 fc_rport_flush_queue();
Joe Eykholte10f8c62010-07-20 15:20:30 -07002219 synchronize_rcu();
2220}
2221
2222/**
2223 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2224 * @fip: The FCoE controller
2225 *
2226 * Called with fcoe_ctlr lock held.
2227 */
2228static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2229{
2230 unsigned long wait;
2231 u32 port_id;
2232
2233 fcoe_ctlr_disc_stop_locked(fip->lp);
2234
2235 /*
2236 * Get proposed port ID.
2237 * If this is the first try after link up, use any previous port_id.
2238 * If there was none, use the low bits of the port_name.
2239 * On subsequent tries, get the next random one.
2240 * Don't use reserved IDs, use another non-zero value, just as random.
2241 */
2242 port_id = fip->port_id;
2243 if (fip->probe_tries)
Akinobu Mita496f2f92012-12-17 16:04:23 -08002244 port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
Joe Eykholte10f8c62010-07-20 15:20:30 -07002245 else if (!port_id)
2246 port_id = fip->lp->wwpn & 0xffff;
2247 if (!port_id || port_id == 0xffff)
2248 port_id = 1;
2249 fip->port_id = port_id;
2250
2251 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2252 fip->probe_tries++;
Akinobu Mita3b60a642013-04-29 16:21:35 -07002253 wait = prandom_u32() % FIP_VN_PROBE_WAIT;
Joe Eykholte10f8c62010-07-20 15:20:30 -07002254 } else
2255 wait = FIP_VN_RLIM_INT;
2256 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2257 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2258}
2259
2260/**
2261 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2262 * @fip: The FCoE controller
2263 *
2264 * Called with fcoe_ctlr lock held.
2265 */
2266static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2267{
2268 fip->probe_tries = 0;
Akinobu Mita496f2f92012-12-17 16:04:23 -08002269 prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002270 fcoe_ctlr_vn_restart(fip);
2271}
2272
2273/**
2274 * fcoe_ctlr_vn_parse - parse probe request or response
2275 * @fip: The FCoE controller
2276 * @skb: incoming packet
2277 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2278 *
2279 * Returns non-zero error number on error.
2280 * Does not consume the packet.
2281 */
2282static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2283 struct sk_buff *skb,
2284 struct fc_rport_priv *rdata)
2285{
2286 struct fip_header *fiph;
2287 struct fip_desc *desc = NULL;
2288 struct fip_mac_desc *macd = NULL;
2289 struct fip_wwn_desc *wwn = NULL;
2290 struct fip_vn_desc *vn = NULL;
2291 struct fip_size_desc *size = NULL;
2292 struct fcoe_rport *frport;
2293 size_t rlen;
2294 size_t dlen;
2295 u32 desc_mask = 0;
2296 u32 dtype;
2297 u8 sub;
2298
2299 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2300 frport = fcoe_ctlr_rport(rdata);
2301
2302 fiph = (struct fip_header *)skb->data;
2303 frport->flags = ntohs(fiph->fip_flags);
2304
2305 sub = fiph->fip_subcode;
2306 switch (sub) {
2307 case FIP_SC_VN_PROBE_REQ:
2308 case FIP_SC_VN_PROBE_REP:
2309 case FIP_SC_VN_BEACON:
2310 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2311 BIT(FIP_DT_VN_ID);
2312 break;
2313 case FIP_SC_VN_CLAIM_NOTIFY:
2314 case FIP_SC_VN_CLAIM_REP:
2315 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2316 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2317 BIT(FIP_DT_FCOE_SIZE);
2318 break;
2319 default:
2320 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2321 return -EINVAL;
2322 }
2323
2324 rlen = ntohs(fiph->fip_dl_len) * 4;
2325 if (rlen + sizeof(*fiph) > skb->len)
2326 return -EINVAL;
2327
2328 desc = (struct fip_desc *)(fiph + 1);
2329 while (rlen > 0) {
2330 dlen = desc->fip_dlen * FIP_BPW;
2331 if (dlen < sizeof(*desc) || dlen > rlen)
2332 return -EINVAL;
2333
2334 dtype = desc->fip_dtype;
2335 if (dtype < 32) {
2336 if (!(desc_mask & BIT(dtype))) {
2337 LIBFCOE_FIP_DBG(fip,
2338 "unexpected or duplicated desc "
2339 "desc type %u in "
2340 "FIP VN2VN subtype %u\n",
2341 dtype, sub);
2342 return -EINVAL;
2343 }
2344 desc_mask &= ~BIT(dtype);
2345 }
2346
2347 switch (dtype) {
2348 case FIP_DT_MAC:
2349 if (dlen != sizeof(struct fip_mac_desc))
2350 goto len_err;
2351 macd = (struct fip_mac_desc *)desc;
2352 if (!is_valid_ether_addr(macd->fd_mac)) {
2353 LIBFCOE_FIP_DBG(fip,
2354 "Invalid MAC addr %pM in FIP VN2VN\n",
2355 macd->fd_mac);
2356 return -EINVAL;
2357 }
2358 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2359 break;
2360 case FIP_DT_NAME:
2361 if (dlen != sizeof(struct fip_wwn_desc))
2362 goto len_err;
2363 wwn = (struct fip_wwn_desc *)desc;
2364 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2365 break;
2366 case FIP_DT_VN_ID:
2367 if (dlen != sizeof(struct fip_vn_desc))
2368 goto len_err;
2369 vn = (struct fip_vn_desc *)desc;
2370 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2371 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2372 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
2373 break;
2374 case FIP_DT_FC4F:
2375 if (dlen != sizeof(struct fip_fc4_feat))
2376 goto len_err;
2377 break;
2378 case FIP_DT_FCOE_SIZE:
2379 if (dlen != sizeof(struct fip_size_desc))
2380 goto len_err;
2381 size = (struct fip_size_desc *)desc;
2382 frport->fcoe_len = ntohs(size->fd_size);
2383 break;
2384 default:
2385 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2386 "in FIP probe\n", dtype);
2387 /* standard says ignore unknown descriptors >= 128 */
Hannes Reinecke6a551c12016-07-04 10:29:18 +02002388 if (dtype < FIP_DT_NON_CRITICAL)
Joe Eykholte10f8c62010-07-20 15:20:30 -07002389 return -EINVAL;
2390 break;
2391 }
2392 desc = (struct fip_desc *)((char *)desc + dlen);
2393 rlen -= dlen;
2394 }
2395 return 0;
2396
2397len_err:
2398 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2399 dtype, dlen);
2400 return -EINVAL;
2401}
2402
2403/**
2404 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2405 * @fip: The FCoE controller
2406 *
2407 * Called with ctlr_mutex held.
2408 */
2409static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2410{
2411 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2412 fip->sol_time = jiffies;
2413}
2414
2415/**
2416 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2417 * @fip: The FCoE controller
2418 * @rdata: parsed remote port with frport from the probe request
2419 *
2420 * Called with ctlr_mutex held.
2421 */
2422static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2423 struct fc_rport_priv *rdata)
2424{
2425 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2426
2427 if (rdata->ids.port_id != fip->port_id)
2428 return;
2429
2430 switch (fip->state) {
2431 case FIP_ST_VNMP_CLAIM:
2432 case FIP_ST_VNMP_UP:
Hannes Reineckec9596552016-10-13 15:10:58 +02002433 LIBFCOE_FIP_DBG(fip, "vn_probe_req: send reply, state %x\n",
2434 fip->state);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002435 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2436 frport->enode_mac, 0);
2437 break;
2438 case FIP_ST_VNMP_PROBE1:
2439 case FIP_ST_VNMP_PROBE2:
2440 /*
2441 * Decide whether to reply to the Probe.
2442 * Our selected address is never a "recorded" one, so
2443 * only reply if our WWPN is greater and the
2444 * Probe's REC bit is not set.
2445 * If we don't reply, we will change our address.
2446 */
2447 if (fip->lp->wwpn > rdata->ids.port_name &&
2448 !(frport->flags & FIP_FL_REC_OR_P2P)) {
Hannes Reineckec9596552016-10-13 15:10:58 +02002449 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2450 "port_id collision\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07002451 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2452 frport->enode_mac, 0);
2453 break;
2454 }
2455 /* fall through */
2456 case FIP_ST_VNMP_START:
Hannes Reineckec9596552016-10-13 15:10:58 +02002457 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2458 "restart VN2VN negotiation\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07002459 fcoe_ctlr_vn_restart(fip);
2460 break;
2461 default:
Hannes Reineckec9596552016-10-13 15:10:58 +02002462 LIBFCOE_FIP_DBG(fip, "vn_probe_req: ignore state %x\n",
2463 fip->state);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002464 break;
2465 }
2466}
2467
2468/**
2469 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2470 * @fip: The FCoE controller
2471 * @rdata: parsed remote port with frport from the probe request
2472 *
2473 * Called with ctlr_mutex held.
2474 */
2475static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2476 struct fc_rport_priv *rdata)
2477{
2478 if (rdata->ids.port_id != fip->port_id)
2479 return;
2480 switch (fip->state) {
2481 case FIP_ST_VNMP_START:
2482 case FIP_ST_VNMP_PROBE1:
2483 case FIP_ST_VNMP_PROBE2:
2484 case FIP_ST_VNMP_CLAIM:
Hannes Reineckec9596552016-10-13 15:10:58 +02002485 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: restart state %x\n",
2486 fip->state);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002487 fcoe_ctlr_vn_restart(fip);
2488 break;
2489 case FIP_ST_VNMP_UP:
Hannes Reineckec9596552016-10-13 15:10:58 +02002490 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: send claim notify\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07002491 fcoe_ctlr_vn_send_claim(fip);
2492 break;
2493 default:
2494 break;
2495 }
2496}
2497
2498/**
2499 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2500 * @fip: The FCoE controller
2501 * @new: newly-parsed remote port with frport as a template for new rdata
2502 *
2503 * Called with ctlr_mutex held.
2504 */
2505static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2506{
2507 struct fc_lport *lport = fip->lp;
2508 struct fc_rport_priv *rdata;
2509 struct fc_rport_identifiers *ids;
2510 struct fcoe_rport *frport;
2511 u32 port_id;
2512
2513 port_id = new->ids.port_id;
2514 if (port_id == fip->port_id)
2515 return;
2516
2517 mutex_lock(&lport->disc.disc_mutex);
Hannes Reinecke25800642016-10-18 10:01:42 +02002518 rdata = fc_rport_create(lport, port_id);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002519 if (!rdata) {
2520 mutex_unlock(&lport->disc.disc_mutex);
2521 return;
2522 }
Hannes Reineckea407c592016-09-30 11:01:15 +02002523 mutex_lock(&rdata->rp_mutex);
2524 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002525
2526 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2527 rdata->disc_id = lport->disc.disc_id;
2528
2529 ids = &rdata->ids;
2530 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
Hannes Reineckea407c592016-09-30 11:01:15 +02002531 (ids->node_name != -1 && ids->node_name != new->ids.node_name)) {
2532 mutex_unlock(&rdata->rp_mutex);
Hannes Reineckec9596552016-10-13 15:10:58 +02002533 LIBFCOE_FIP_DBG(fip, "vn_add rport logoff %6.6x\n", port_id);
Hannes Reineckec96c7922016-10-18 10:01:44 +02002534 fc_rport_logoff(rdata);
Hannes Reineckea407c592016-09-30 11:01:15 +02002535 mutex_lock(&rdata->rp_mutex);
2536 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07002537 ids->port_name = new->ids.port_name;
2538 ids->node_name = new->ids.node_name;
Hannes Reineckea407c592016-09-30 11:01:15 +02002539 mutex_unlock(&rdata->rp_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002540
2541 frport = fcoe_ctlr_rport(rdata);
Hannes Reineckec9596552016-10-13 15:10:58 +02002542 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s state %d\n",
2543 port_id, frport->fcoe_len ? "old" : "new",
2544 rdata->rp_state);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002545 *frport = *fcoe_ctlr_rport(new);
2546 frport->time = 0;
2547}
2548
2549/**
2550 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2551 * @fip: The FCoE controller
2552 * @port_id: The port_id of the remote VN_node
2553 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2554 *
2555 * Returns non-zero error if no remote port found.
2556 */
2557static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2558{
2559 struct fc_lport *lport = fip->lp;
2560 struct fc_rport_priv *rdata;
2561 struct fcoe_rport *frport;
2562 int ret = -1;
2563
Hannes Reineckee87b7772016-10-18 10:01:40 +02002564 rdata = fc_rport_lookup(lport, port_id);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002565 if (rdata) {
2566 frport = fcoe_ctlr_rport(rdata);
2567 memcpy(mac, frport->enode_mac, ETH_ALEN);
2568 ret = 0;
Hannes Reinecke944ef962016-10-18 10:01:39 +02002569 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002570 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07002571 return ret;
2572}
2573
2574/**
2575 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2576 * @fip: The FCoE controller
2577 * @new: newly-parsed remote port with frport as a template for new rdata
2578 *
2579 * Called with ctlr_mutex held.
2580 */
2581static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2582 struct fc_rport_priv *new)
2583{
2584 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2585
2586 if (frport->flags & FIP_FL_REC_OR_P2P) {
Hannes Reineckec9596552016-10-13 15:10:58 +02002587 LIBFCOE_FIP_DBG(fip, "send probe req for P2P/REC\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07002588 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2589 return;
2590 }
2591 switch (fip->state) {
2592 case FIP_ST_VNMP_START:
2593 case FIP_ST_VNMP_PROBE1:
2594 case FIP_ST_VNMP_PROBE2:
Hannes Reineckec9596552016-10-13 15:10:58 +02002595 if (new->ids.port_id == fip->port_id) {
2596 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2597 "restart, state %d\n",
2598 fip->state);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002599 fcoe_ctlr_vn_restart(fip);
Hannes Reineckec9596552016-10-13 15:10:58 +02002600 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07002601 break;
2602 case FIP_ST_VNMP_CLAIM:
2603 case FIP_ST_VNMP_UP:
2604 if (new->ids.port_id == fip->port_id) {
2605 if (new->ids.port_name > fip->lp->wwpn) {
Hannes Reineckec9596552016-10-13 15:10:58 +02002606 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2607 "restart, port_id collision\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07002608 fcoe_ctlr_vn_restart(fip);
2609 break;
2610 }
Hannes Reineckec9596552016-10-13 15:10:58 +02002611 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2612 "send claim notify\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07002613 fcoe_ctlr_vn_send_claim(fip);
2614 break;
2615 }
Hannes Reineckec9596552016-10-13 15:10:58 +02002616 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: send reply to %x\n",
2617 new->ids.port_id);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002618 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2619 min((u32)frport->fcoe_len,
2620 fcoe_ctlr_fcoe_size(fip)));
2621 fcoe_ctlr_vn_add(fip, new);
2622 break;
2623 default:
Hannes Reineckec9596552016-10-13 15:10:58 +02002624 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2625 "ignoring claim from %x\n", new->ids.port_id);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002626 break;
2627 }
2628}
2629
2630/**
2631 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2632 * @fip: The FCoE controller that received the frame
2633 * @new: newly-parsed remote port with frport from the Claim Response
2634 *
2635 * Called with ctlr_mutex held.
2636 */
2637static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2638 struct fc_rport_priv *new)
2639{
2640 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2641 new->ids.port_id, fcoe_ctlr_state(fip->state));
2642 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2643 fcoe_ctlr_vn_add(fip, new);
2644}
2645
2646/**
2647 * fcoe_ctlr_vn_beacon() - handle received beacon.
2648 * @fip: The FCoE controller that received the frame
2649 * @new: newly-parsed remote port with frport from the Beacon
2650 *
2651 * Called with ctlr_mutex held.
2652 */
2653static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2654 struct fc_rport_priv *new)
2655{
2656 struct fc_lport *lport = fip->lp;
2657 struct fc_rport_priv *rdata;
2658 struct fcoe_rport *frport;
2659
2660 frport = fcoe_ctlr_rport(new);
2661 if (frport->flags & FIP_FL_REC_OR_P2P) {
Hannes Reineckec9596552016-10-13 15:10:58 +02002662 LIBFCOE_FIP_DBG(fip, "p2p beacon while in vn2vn mode\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07002663 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2664 return;
2665 }
Hannes Reineckee87b7772016-10-18 10:01:40 +02002666 rdata = fc_rport_lookup(lport, new->ids.port_id);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002667 if (rdata) {
2668 if (rdata->ids.node_name == new->ids.node_name &&
2669 rdata->ids.port_name == new->ids.port_name) {
2670 frport = fcoe_ctlr_rport(rdata);
Hannes Reineckec9596552016-10-13 15:10:58 +02002671 LIBFCOE_FIP_DBG(fip, "beacon from rport %x\n",
2672 rdata->ids.port_id);
2673 if (!frport->time && fip->state == FIP_ST_VNMP_UP) {
2674 LIBFCOE_FIP_DBG(fip, "beacon expired "
2675 "for rport %x\n",
2676 rdata->ids.port_id);
Hannes Reinecke05d7d3b2016-10-18 10:01:43 +02002677 fc_rport_login(rdata);
Hannes Reineckec9596552016-10-13 15:10:58 +02002678 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07002679 frport->time = jiffies;
2680 }
Hannes Reinecke944ef962016-10-18 10:01:39 +02002681 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002682 return;
2683 }
2684 if (fip->state != FIP_ST_VNMP_UP)
2685 return;
2686
2687 /*
2688 * Beacon from a new neighbor.
2689 * Send a claim notify if one hasn't been sent recently.
2690 * Don't add the neighbor yet.
2691 */
2692 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2693 new->ids.port_id);
2694 if (time_after(jiffies,
2695 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2696 fcoe_ctlr_vn_send_claim(fip);
2697}
2698
2699/**
2700 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2701 * @fip: The FCoE controller
2702 *
2703 * Called with ctlr_mutex held.
2704 * Called only in state FIP_ST_VNMP_UP.
2705 * Returns the soonest time for next age-out or a time far in the future.
2706 */
2707static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2708{
2709 struct fc_lport *lport = fip->lp;
2710 struct fc_rport_priv *rdata;
2711 struct fcoe_rport *frport;
2712 unsigned long next_time;
2713 unsigned long deadline;
2714
2715 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
Hannes Reinecke0993ed92018-07-11 10:09:30 +02002716 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002717 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
Hannes Reineckea407c592016-09-30 11:01:15 +02002718 if (!kref_get_unless_zero(&rdata->kref))
Joe Eykholte10f8c62010-07-20 15:20:30 -07002719 continue;
Hannes Reineckea407c592016-09-30 11:01:15 +02002720 frport = fcoe_ctlr_rport(rdata);
2721 if (!frport->time) {
Hannes Reinecke944ef962016-10-18 10:01:39 +02002722 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reineckea407c592016-09-30 11:01:15 +02002723 continue;
2724 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07002725 deadline = frport->time +
2726 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2727 if (time_after_eq(jiffies, deadline)) {
2728 frport->time = 0;
2729 LIBFCOE_FIP_DBG(fip,
2730 "port %16.16llx fc_id %6.6x beacon expired\n",
2731 rdata->ids.port_name, rdata->ids.port_id);
Hannes Reineckec96c7922016-10-18 10:01:44 +02002732 fc_rport_logoff(rdata);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002733 } else if (time_before(deadline, next_time))
2734 next_time = deadline;
Hannes Reinecke944ef962016-10-18 10:01:39 +02002735 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002736 }
Hannes Reinecke0993ed92018-07-11 10:09:30 +02002737 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002738 return next_time;
2739}
2740
2741/**
2742 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2743 * @fip: The FCoE controller that received the frame
2744 * @skb: The received FIP frame
2745 *
2746 * Returns non-zero if the frame is dropped.
2747 * Always consumes the frame.
2748 */
2749static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2750{
2751 struct fip_header *fiph;
2752 enum fip_vn2vn_subcode sub;
Kiran Patil2dc02ee2010-10-08 17:12:41 -07002753 struct {
Joe Eykholte10f8c62010-07-20 15:20:30 -07002754 struct fc_rport_priv rdata;
2755 struct fcoe_rport frport;
2756 } buf;
Hannes Reinecke5d5a51d2016-10-13 15:11:00 +02002757 int rc, vlan_id = 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -07002758
2759 fiph = (struct fip_header *)skb->data;
2760 sub = fiph->fip_subcode;
2761
Hannes Reinecke5d5a51d2016-10-13 15:11:00 +02002762 if (fip->lp->vlan)
2763 vlan_id = skb_vlan_tag_get_id(skb);
2764
2765 if (vlan_id && vlan_id != fip->lp->vlan) {
2766 LIBFCOE_FIP_DBG(fip, "vn_recv drop frame sub %x vlan %d\n",
2767 sub, vlan_id);
2768 rc = -EAGAIN;
2769 goto drop;
2770 }
2771
Joe Eykholte10f8c62010-07-20 15:20:30 -07002772 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2773 if (rc) {
2774 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2775 goto drop;
2776 }
2777
2778 mutex_lock(&fip->ctlr_mutex);
2779 switch (sub) {
2780 case FIP_SC_VN_PROBE_REQ:
2781 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2782 break;
2783 case FIP_SC_VN_PROBE_REP:
2784 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2785 break;
2786 case FIP_SC_VN_CLAIM_NOTIFY:
2787 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2788 break;
2789 case FIP_SC_VN_CLAIM_REP:
2790 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2791 break;
2792 case FIP_SC_VN_BEACON:
2793 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2794 break;
2795 default:
2796 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2797 rc = -1;
2798 break;
2799 }
2800 mutex_unlock(&fip->ctlr_mutex);
2801drop:
2802 kfree_skb(skb);
2803 return rc;
2804}
2805
2806/**
Hannes Reinecke9a6cf882016-07-19 13:49:40 +02002807 * fcoe_ctlr_vlan_parse - parse vlan discovery request or response
2808 * @fip: The FCoE controller
2809 * @skb: incoming packet
2810 * @rdata: buffer for resulting parsed VLAN entry plus fcoe_rport
2811 *
2812 * Returns non-zero error number on error.
2813 * Does not consume the packet.
2814 */
2815static int fcoe_ctlr_vlan_parse(struct fcoe_ctlr *fip,
2816 struct sk_buff *skb,
2817 struct fc_rport_priv *rdata)
2818{
2819 struct fip_header *fiph;
2820 struct fip_desc *desc = NULL;
2821 struct fip_mac_desc *macd = NULL;
2822 struct fip_wwn_desc *wwn = NULL;
2823 struct fcoe_rport *frport;
2824 size_t rlen;
2825 size_t dlen;
2826 u32 desc_mask = 0;
2827 u32 dtype;
2828 u8 sub;
2829
2830 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2831 frport = fcoe_ctlr_rport(rdata);
2832
2833 fiph = (struct fip_header *)skb->data;
2834 frport->flags = ntohs(fiph->fip_flags);
2835
2836 sub = fiph->fip_subcode;
2837 switch (sub) {
2838 case FIP_SC_VL_REQ:
2839 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
2840 break;
2841 default:
2842 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2843 return -EINVAL;
2844 }
2845
2846 rlen = ntohs(fiph->fip_dl_len) * 4;
2847 if (rlen + sizeof(*fiph) > skb->len)
2848 return -EINVAL;
2849
2850 desc = (struct fip_desc *)(fiph + 1);
2851 while (rlen > 0) {
2852 dlen = desc->fip_dlen * FIP_BPW;
2853 if (dlen < sizeof(*desc) || dlen > rlen)
2854 return -EINVAL;
2855
2856 dtype = desc->fip_dtype;
2857 if (dtype < 32) {
2858 if (!(desc_mask & BIT(dtype))) {
2859 LIBFCOE_FIP_DBG(fip,
2860 "unexpected or duplicated desc "
2861 "desc type %u in "
2862 "FIP VN2VN subtype %u\n",
2863 dtype, sub);
2864 return -EINVAL;
2865 }
2866 desc_mask &= ~BIT(dtype);
2867 }
2868
2869 switch (dtype) {
2870 case FIP_DT_MAC:
2871 if (dlen != sizeof(struct fip_mac_desc))
2872 goto len_err;
2873 macd = (struct fip_mac_desc *)desc;
2874 if (!is_valid_ether_addr(macd->fd_mac)) {
2875 LIBFCOE_FIP_DBG(fip,
2876 "Invalid MAC addr %pM in FIP VN2VN\n",
2877 macd->fd_mac);
2878 return -EINVAL;
2879 }
2880 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2881 break;
2882 case FIP_DT_NAME:
2883 if (dlen != sizeof(struct fip_wwn_desc))
2884 goto len_err;
2885 wwn = (struct fip_wwn_desc *)desc;
2886 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2887 break;
2888 default:
2889 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2890 "in FIP probe\n", dtype);
2891 /* standard says ignore unknown descriptors >= 128 */
2892 if (dtype < FIP_DT_NON_CRITICAL)
2893 return -EINVAL;
2894 break;
2895 }
2896 desc = (struct fip_desc *)((char *)desc + dlen);
2897 rlen -= dlen;
2898 }
2899 return 0;
2900
2901len_err:
2902 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2903 dtype, dlen);
2904 return -EINVAL;
2905}
2906
2907/**
2908 * fcoe_ctlr_vlan_send() - Send a FIP VLAN Notification
2909 * @fip: The FCoE controller
2910 * @sub: sub-opcode for vlan notification or vn2vn vlan notification
2911 * @dest: The destination Ethernet MAC address
2912 * @min_len: minimum size of the Ethernet payload to be sent
2913 */
2914static void fcoe_ctlr_vlan_send(struct fcoe_ctlr *fip,
2915 enum fip_vlan_subcode sub,
2916 const u8 *dest)
2917{
2918 struct sk_buff *skb;
2919 struct fip_vlan_notify_frame {
2920 struct ethhdr eth;
2921 struct fip_header fip;
2922 struct fip_mac_desc mac;
2923 struct fip_vlan_desc vlan;
2924 } __packed * frame;
2925 size_t len;
2926 size_t dlen;
2927
2928 len = sizeof(*frame);
2929 dlen = sizeof(frame->mac) + sizeof(frame->vlan);
2930 len = max(len, sizeof(struct ethhdr));
2931
2932 skb = dev_alloc_skb(len);
2933 if (!skb)
2934 return;
2935
2936 LIBFCOE_FIP_DBG(fip, "fip %s vlan notification, vlan %d\n",
2937 fip->mode == FIP_MODE_VN2VN ? "vn2vn" : "fcf",
2938 fip->lp->vlan);
2939
2940 frame = (struct fip_vlan_notify_frame *)skb->data;
2941 memset(frame, 0, len);
2942 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2943
2944 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2945 frame->eth.h_proto = htons(ETH_P_FIP);
2946
2947 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2948 frame->fip.fip_op = htons(FIP_OP_VLAN);
2949 frame->fip.fip_subcode = sub;
2950 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2951
2952 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2953 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2954 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2955
2956 frame->vlan.fd_desc.fip_dtype = FIP_DT_VLAN;
2957 frame->vlan.fd_desc.fip_dlen = sizeof(frame->vlan) / FIP_BPW;
2958 put_unaligned_be16(fip->lp->vlan, &frame->vlan.fd_vlan);
2959
2960 skb_put(skb, len);
2961 skb->protocol = htons(ETH_P_FIP);
2962 skb->priority = fip->priority;
2963 skb_reset_mac_header(skb);
2964 skb_reset_network_header(skb);
2965
2966 fip->send(fip, skb);
2967}
2968
2969/**
2970 * fcoe_ctlr_vlan_disk_reply() - send FIP VLAN Discovery Notification.
2971 * @fip: The FCoE controller
2972 *
2973 * Called with ctlr_mutex held.
2974 */
2975static void fcoe_ctlr_vlan_disc_reply(struct fcoe_ctlr *fip,
2976 struct fc_rport_priv *rdata)
2977{
2978 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2979 enum fip_vlan_subcode sub = FIP_SC_VL_NOTE;
2980
2981 if (fip->mode == FIP_MODE_VN2VN)
2982 sub = FIP_SC_VL_VN2VN_NOTE;
2983
2984 fcoe_ctlr_vlan_send(fip, sub, frport->enode_mac);
2985}
2986
2987/**
2988 * fcoe_ctlr_vlan_recv - vlan request receive handler for VN2VN mode.
2989 * @lport: The local port
2990 * @fp: The received frame
2991 *
2992 */
2993static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2994{
2995 struct fip_header *fiph;
2996 enum fip_vlan_subcode sub;
2997 struct {
2998 struct fc_rport_priv rdata;
2999 struct fcoe_rport frport;
3000 } buf;
3001 int rc;
3002
3003 fiph = (struct fip_header *)skb->data;
3004 sub = fiph->fip_subcode;
3005 rc = fcoe_ctlr_vlan_parse(fip, skb, &buf.rdata);
3006 if (rc) {
3007 LIBFCOE_FIP_DBG(fip, "vlan_recv vlan_parse error %d\n", rc);
3008 goto drop;
3009 }
3010 mutex_lock(&fip->ctlr_mutex);
3011 if (sub == FIP_SC_VL_REQ)
3012 fcoe_ctlr_vlan_disc_reply(fip, &buf.rdata);
3013 mutex_unlock(&fip->ctlr_mutex);
3014
3015drop:
Wei Yongjunea0a95d2016-07-25 07:00:33 +00003016 kfree_skb(skb);
Hannes Reinecke9a6cf882016-07-19 13:49:40 +02003017 return rc;
3018}
3019
3020/**
Joe Eykholte10f8c62010-07-20 15:20:30 -07003021 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
Joe Eykholt922611562010-07-20 15:21:12 -07003022 * @lport: The local port
3023 * @fp: The received frame
Joe Eykholte10f8c62010-07-20 15:20:30 -07003024 *
3025 * This should never be called since we don't see RSCNs or other
3026 * fabric-generated ELSes.
3027 */
Joe Eykholt922611562010-07-20 15:21:12 -07003028static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholte10f8c62010-07-20 15:20:30 -07003029{
3030 struct fc_seq_els_data rjt_data;
3031
Joe Eykholte10f8c62010-07-20 15:20:30 -07003032 rjt_data.reason = ELS_RJT_UNSUP;
3033 rjt_data.explan = ELS_EXPL_NONE;
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02003034 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholte10f8c62010-07-20 15:20:30 -07003035 fc_frame_free(fp);
3036}
3037
3038/**
3039 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
3040 * @fip: The FCoE controller
3041 *
3042 * This sets a flag indicating that remote ports should be created
3043 * and started for the peers we discover. We use the disc_callback
3044 * pointer as that flag. Peers already discovered are created here.
3045 *
3046 * The lport lock is held during this call. The callback must be done
3047 * later, without holding either the lport or discovery locks.
3048 * The fcoe_ctlr lock may also be held during this call.
3049 */
3050static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
3051 enum fc_disc_event),
3052 struct fc_lport *lport)
3053{
3054 struct fc_disc *disc = &lport->disc;
3055 struct fcoe_ctlr *fip = disc->priv;
3056
3057 mutex_lock(&disc->disc_mutex);
3058 disc->disc_callback = callback;
3059 disc->disc_id = (disc->disc_id + 2) | 1;
3060 disc->pending = 1;
3061 schedule_work(&fip->timer_work);
3062 mutex_unlock(&disc->disc_mutex);
3063}
3064
3065/**
3066 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
3067 * @fip: The FCoE controller
3068 *
3069 * Starts the FLOGI and PLOGI login process to each discovered rport for which
3070 * we've received at least one beacon.
3071 * Performs the discovery complete callback.
3072 */
3073static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
3074{
3075 struct fc_lport *lport = fip->lp;
3076 struct fc_disc *disc = &lport->disc;
3077 struct fc_rport_priv *rdata;
3078 struct fcoe_rport *frport;
3079 void (*callback)(struct fc_lport *, enum fc_disc_event);
3080
3081 mutex_lock(&disc->disc_mutex);
3082 callback = disc->pending ? disc->disc_callback : NULL;
3083 disc->pending = 0;
3084 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
Hannes Reineckea407c592016-09-30 11:01:15 +02003085 if (!kref_get_unless_zero(&rdata->kref))
3086 continue;
Joe Eykholte10f8c62010-07-20 15:20:30 -07003087 frport = fcoe_ctlr_rport(rdata);
3088 if (frport->time)
Hannes Reinecke05d7d3b2016-10-18 10:01:43 +02003089 fc_rport_login(rdata);
Hannes Reinecke944ef962016-10-18 10:01:39 +02003090 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholte10f8c62010-07-20 15:20:30 -07003091 }
Hannes Reinecke0993ed92018-07-11 10:09:30 +02003092 mutex_unlock(&disc->disc_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07003093 if (callback)
3094 callback(lport, DISC_EV_SUCCESS);
3095}
3096
3097/**
3098 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
3099 * @fip: The FCoE controller
3100 */
3101static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
3102{
3103 unsigned long next_time;
3104 u8 mac[ETH_ALEN];
3105 u32 new_port_id = 0;
3106
3107 mutex_lock(&fip->ctlr_mutex);
3108 switch (fip->state) {
3109 case FIP_ST_VNMP_START:
3110 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
Hannes Reineckec9596552016-10-13 15:10:58 +02003111 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 1st probe request\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07003112 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3113 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
3114 break;
3115 case FIP_ST_VNMP_PROBE1:
3116 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
Hannes Reineckec9596552016-10-13 15:10:58 +02003117 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 2nd probe request\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07003118 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3119 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3120 break;
3121 case FIP_ST_VNMP_PROBE2:
3122 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
3123 new_port_id = fip->port_id;
3124 hton24(mac, FIP_VN_FC_MAP);
3125 hton24(mac + 3, new_port_id);
Joe Eykholtcd229e42010-07-20 15:20:40 -07003126 fcoe_ctlr_map_dest(fip);
Joe Eykholte10f8c62010-07-20 15:20:30 -07003127 fip->update_mac(fip->lp, mac);
Hannes Reineckec9596552016-10-13 15:10:58 +02003128 LIBFCOE_FIP_DBG(fip, "vn_timeout: send claim notify\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07003129 fcoe_ctlr_vn_send_claim(fip);
3130 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3131 break;
3132 case FIP_ST_VNMP_CLAIM:
3133 /*
3134 * This may be invoked either by starting discovery so don't
3135 * go to the next state unless it's been long enough.
3136 */
3137 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3138 if (time_after_eq(jiffies, next_time)) {
3139 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
Hannes Reineckec9596552016-10-13 15:10:58 +02003140 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07003141 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3142 fcoe_all_vn2vn, 0);
3143 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3144 fip->port_ka_time = next_time;
3145 }
3146 fcoe_ctlr_vn_disc(fip);
3147 break;
3148 case FIP_ST_VNMP_UP:
3149 next_time = fcoe_ctlr_vn_age(fip);
3150 if (time_after_eq(jiffies, fip->port_ka_time)) {
Hannes Reineckec9596552016-10-13 15:10:58 +02003151 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -07003152 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3153 fcoe_all_vn2vn, 0);
3154 fip->port_ka_time = jiffies +
3155 msecs_to_jiffies(FIP_VN_BEACON_INT +
Akinobu Mita3b60a642013-04-29 16:21:35 -07003156 (prandom_u32() % FIP_VN_BEACON_FUZZ));
Joe Eykholte10f8c62010-07-20 15:20:30 -07003157 }
3158 if (time_before(fip->port_ka_time, next_time))
3159 next_time = fip->port_ka_time;
3160 break;
3161 case FIP_ST_LINK_WAIT:
3162 goto unlock;
3163 default:
Joe Perches11aa9902010-11-30 16:19:09 -08003164 WARN(1, "unexpected state %d\n", fip->state);
Joe Eykholte10f8c62010-07-20 15:20:30 -07003165 goto unlock;
3166 }
3167 mod_timer(&fip->timer, next_time);
3168unlock:
3169 mutex_unlock(&fip->ctlr_mutex);
3170
3171 /* If port ID is new, notify local port after dropping ctlr_mutex */
3172 if (new_port_id)
3173 fc_lport_set_local_id(fip->lp, new_port_id);
3174}
3175
3176/**
Robert Love0db0e372013-03-25 11:00:28 -07003177 * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
3178 * @lport: The local port to be (re)configured
3179 * @fip: The FCoE controller whose mode is changing
3180 * @fip_mode: The new fip mode
3181 *
3182 * Note that the we shouldn't be changing the libfc discovery settings
3183 * (fc_disc_config) while an lport is going through the libfc state
3184 * machine. The mode can only be changed when a fcoe_ctlr device is
3185 * disabled, so that should ensure that this routine is only called
3186 * when nothing is happening.
3187 */
Bart Van Assche41463a82013-08-14 15:41:22 +00003188static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
Hannes Reinecke1917d422016-07-04 10:29:19 +02003189 enum fip_mode fip_mode)
Robert Love0db0e372013-03-25 11:00:28 -07003190{
3191 void *priv;
3192
3193 WARN_ON(lport->state != LPORT_ST_RESET &&
3194 lport->state != LPORT_ST_DISABLED);
3195
3196 if (fip_mode == FIP_MODE_VN2VN) {
3197 lport->rport_priv_size = sizeof(struct fcoe_rport);
3198 lport->point_to_multipoint = 1;
3199 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
3200 lport->tt.disc_start = fcoe_ctlr_disc_start;
3201 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
3202 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
3203 priv = fip;
3204 } else {
3205 lport->rport_priv_size = 0;
3206 lport->point_to_multipoint = 0;
3207 lport->tt.disc_recv_req = NULL;
3208 lport->tt.disc_start = NULL;
3209 lport->tt.disc_stop = NULL;
3210 lport->tt.disc_stop_final = NULL;
3211 priv = lport;
3212 }
3213
3214 fc_disc_config(lport, priv);
3215}
3216
3217/**
Robert Love70b51aa2009-11-03 11:47:45 -08003218 * fcoe_libfc_config() - Sets up libfc related properties for local port
Robert Love8d55e502012-05-22 19:06:26 -07003219 * @lport: The local port to configure libfc for
3220 * @fip: The FCoE controller in use by the local port
3221 * @tt: The libfc function template
Joe Eykholte10f8c62010-07-20 15:20:30 -07003222 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
Vasu Dev5e80f7f2009-03-17 11:42:18 -07003223 *
3224 * Returns : 0 for success
3225 */
Joe Eykholte10f8c62010-07-20 15:20:30 -07003226int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
3227 const struct libfc_function_template *tt, int init_fcp)
Vasu Dev5e80f7f2009-03-17 11:42:18 -07003228{
3229 /* Set the function pointers set by the LLDD */
Robert Love70b51aa2009-11-03 11:47:45 -08003230 memcpy(&lport->tt, tt, sizeof(*tt));
Joe Eykholte10f8c62010-07-20 15:20:30 -07003231 if (init_fcp && fc_fcp_init(lport))
Vasu Dev5e80f7f2009-03-17 11:42:18 -07003232 return -ENOMEM;
Robert Love70b51aa2009-11-03 11:47:45 -08003233 fc_exch_init(lport);
3234 fc_elsct_init(lport);
3235 fc_lport_init(lport);
Robert Love08076192013-03-25 11:00:28 -07003236 fc_disc_init(lport);
Robert Love0db0e372013-03-25 11:00:28 -07003237 fcoe_ctlr_mode_set(lport, fip, fip->mode);
Vasu Dev5e80f7f2009-03-17 11:42:18 -07003238 return 0;
3239}
3240EXPORT_SYMBOL_GPL(fcoe_libfc_config);
Robert Love8d55e502012-05-22 19:06:26 -07003241
3242void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
3243{
3244 struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
3245 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
3246 struct fcoe_fcf *fcf;
3247
3248 mutex_lock(&fip->ctlr_mutex);
3249 mutex_lock(&ctlr_dev->lock);
3250
3251 fcf = fcoe_fcf_device_priv(fcf_dev);
3252 if (fcf)
3253 fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
3254 else
3255 fcf_dev->selected = 0;
3256
3257 mutex_unlock(&ctlr_dev->lock);
3258 mutex_unlock(&fip->ctlr_mutex);
3259}
3260EXPORT_SYMBOL(fcoe_fcf_get_selected);
3261
Robert Love435c8662012-11-27 06:53:35 +00003262void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
Robert Love8d55e502012-05-22 19:06:26 -07003263{
3264 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
Robert Love0db0e372013-03-25 11:00:28 -07003265 struct fc_lport *lport = ctlr->lp;
Robert Love8d55e502012-05-22 19:06:26 -07003266
3267 mutex_lock(&ctlr->ctlr_mutex);
Robert Love435c8662012-11-27 06:53:35 +00003268 switch (ctlr_dev->mode) {
3269 case FIP_CONN_TYPE_VN2VN:
3270 ctlr->mode = FIP_MODE_VN2VN;
Robert Love8d55e502012-05-22 19:06:26 -07003271 break;
Robert Love435c8662012-11-27 06:53:35 +00003272 case FIP_CONN_TYPE_FABRIC:
Robert Love8d55e502012-05-22 19:06:26 -07003273 default:
Robert Love435c8662012-11-27 06:53:35 +00003274 ctlr->mode = FIP_MODE_FABRIC;
Robert Love8d55e502012-05-22 19:06:26 -07003275 break;
3276 }
Robert Love435c8662012-11-27 06:53:35 +00003277
Robert Love8d55e502012-05-22 19:06:26 -07003278 mutex_unlock(&ctlr->ctlr_mutex);
Robert Love0db0e372013-03-25 11:00:28 -07003279
3280 fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
Robert Love8d55e502012-05-22 19:06:26 -07003281}
Robert Love435c8662012-11-27 06:53:35 +00003282EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);