blob: 375c536cbc688f0e5b9467ca65e9885b96ea130d [file] [log] [blame]
Yi Zoufdecf312011-01-28 16:04:55 -08001/*
2 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#include <linux/types.h>
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/netdevice.h>
25#include <linux/errno.h>
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -080026#include <linux/crc32.h>
Yi Zoufdecf312011-01-28 16:04:55 -080027#include <scsi/libfcoe.h>
28
29#include "libfcoe.h"
30
Yi Zoue01efc32011-01-28 16:05:06 -080031MODULE_AUTHOR("Open-FCoE.org");
32MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs");
33MODULE_LICENSE("GPL v2");
34
Yi Zoufdecf312011-01-28 16:04:55 -080035static int fcoe_transport_create(const char *, struct kernel_param *);
36static int fcoe_transport_destroy(const char *, struct kernel_param *);
37static int fcoe_transport_show(char *buffer, const struct kernel_param *kp);
38static struct fcoe_transport *fcoe_transport_lookup(struct net_device *device);
39static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *device);
40static int fcoe_transport_enable(const char *, struct kernel_param *);
41static int fcoe_transport_disable(const char *, struct kernel_param *);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -080042static int libfcoe_device_notification(struct notifier_block *notifier,
43 ulong event, void *ptr);
Yi Zoufdecf312011-01-28 16:04:55 -080044
45static LIST_HEAD(fcoe_transports);
Yi Zoufdecf312011-01-28 16:04:55 -080046static DEFINE_MUTEX(ft_mutex);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -080047static LIST_HEAD(fcoe_netdevs);
48static DEFINE_MUTEX(fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -080049
Yi Zoue01efc32011-01-28 16:05:06 -080050unsigned int libfcoe_debug_logging;
51module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
52MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
53
Yi Zoufdecf312011-01-28 16:04:55 -080054module_param_call(show, NULL, fcoe_transport_show, NULL, S_IRUSR);
55__MODULE_PARM_TYPE(show, "string");
56MODULE_PARM_DESC(show, " Show attached FCoE transports");
57
58module_param_call(create, fcoe_transport_create, NULL,
59 (void *)FIP_MODE_FABRIC, S_IWUSR);
60__MODULE_PARM_TYPE(create, "string");
Masanari Iidabd0a1d62015-06-04 00:54:34 +090061MODULE_PARM_DESC(create, " Creates fcoe instance on an ethernet interface");
Yi Zoufdecf312011-01-28 16:04:55 -080062
63module_param_call(create_vn2vn, fcoe_transport_create, NULL,
64 (void *)FIP_MODE_VN2VN, S_IWUSR);
65__MODULE_PARM_TYPE(create_vn2vn, "string");
66MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance "
67 "on an Ethernet interface");
68
69module_param_call(destroy, fcoe_transport_destroy, NULL, NULL, S_IWUSR);
70__MODULE_PARM_TYPE(destroy, "string");
Masanari Iidabd0a1d62015-06-04 00:54:34 +090071MODULE_PARM_DESC(destroy, " Destroys fcoe instance on an ethernet interface");
Yi Zoufdecf312011-01-28 16:04:55 -080072
73module_param_call(enable, fcoe_transport_enable, NULL, NULL, S_IWUSR);
74__MODULE_PARM_TYPE(enable, "string");
Masanari Iidabd0a1d62015-06-04 00:54:34 +090075MODULE_PARM_DESC(enable, " Enables fcoe on an ethernet interface.");
Yi Zoufdecf312011-01-28 16:04:55 -080076
77module_param_call(disable, fcoe_transport_disable, NULL, NULL, S_IWUSR);
78__MODULE_PARM_TYPE(disable, "string");
Masanari Iidabd0a1d62015-06-04 00:54:34 +090079MODULE_PARM_DESC(disable, " Disables fcoe on an ethernet interface.");
Yi Zoufdecf312011-01-28 16:04:55 -080080
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -080081/* notification function for packets from net device */
82static struct notifier_block libfcoe_notifier = {
83 .notifier_call = libfcoe_device_notification,
84};
85
Johannes Thumshirn0b924e52016-08-19 15:33:37 +020086static const struct {
87 u32 fc_port_speed;
88#define SPEED_2000 2000
89#define SPEED_4000 4000
90#define SPEED_8000 8000
91#define SPEED_16000 16000
92#define SPEED_32000 32000
93 u32 eth_port_speed;
94} fcoe_port_speed_mapping[] = {
95 { FC_PORTSPEED_1GBIT, SPEED_1000 },
96 { FC_PORTSPEED_2GBIT, SPEED_2000 },
97 { FC_PORTSPEED_4GBIT, SPEED_4000 },
98 { FC_PORTSPEED_8GBIT, SPEED_8000 },
99 { FC_PORTSPEED_10GBIT, SPEED_10000 },
100 { FC_PORTSPEED_16GBIT, SPEED_16000 },
101 { FC_PORTSPEED_20GBIT, SPEED_20000 },
102 { FC_PORTSPEED_25GBIT, SPEED_25000 },
103 { FC_PORTSPEED_32GBIT, SPEED_32000 },
104 { FC_PORTSPEED_40GBIT, SPEED_40000 },
105 { FC_PORTSPEED_50GBIT, SPEED_50000 },
106 { FC_PORTSPEED_100GBIT, SPEED_100000 },
107};
108
109static inline u32 eth2fc_speed(u32 eth_port_speed)
110{
111 int i;
112
Vincent Stehlé182d3f82016-09-21 22:54:07 +0200113 for (i = 0; i < ARRAY_SIZE(fcoe_port_speed_mapping); i++) {
Johannes Thumshirn0b924e52016-08-19 15:33:37 +0200114 if (fcoe_port_speed_mapping[i].eth_port_speed == eth_port_speed)
115 return fcoe_port_speed_mapping[i].fc_port_speed;
116 }
117
118 return FC_PORTSPEED_UNKNOWN;
119}
120
Yi Zou03702682012-12-06 06:23:58 +0000121/**
122 * fcoe_link_speed_update() - Update the supported and actual link speeds
123 * @lport: The local port to update speeds for
124 *
125 * Returns: 0 if the ethtool query was successful
126 * -1 if the ethtool query failed
127 */
128int fcoe_link_speed_update(struct fc_lport *lport)
129{
130 struct net_device *netdev = fcoe_get_netdev(lport);
David Decotigny008eb732016-02-24 10:58:06 -0800131 struct ethtool_link_ksettings ecmd;
Yi Zou03702682012-12-06 06:23:58 +0000132
David Decotigny008eb732016-02-24 10:58:06 -0800133 if (!__ethtool_get_link_ksettings(netdev, &ecmd)) {
Chris Leechb8d23dc2014-09-25 11:55:42 -0700134 lport->link_supported_speeds &= ~(FC_PORTSPEED_1GBIT |
135 FC_PORTSPEED_10GBIT |
136 FC_PORTSPEED_20GBIT |
137 FC_PORTSPEED_40GBIT);
138
David Decotigny008eb732016-02-24 10:58:06 -0800139 if (ecmd.link_modes.supported[0] & (
140 SUPPORTED_1000baseT_Half |
141 SUPPORTED_1000baseT_Full |
142 SUPPORTED_1000baseKX_Full))
Yi Zou03702682012-12-06 06:23:58 +0000143 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Chris Leechb8d23dc2014-09-25 11:55:42 -0700144
David Decotigny008eb732016-02-24 10:58:06 -0800145 if (ecmd.link_modes.supported[0] & (
146 SUPPORTED_10000baseT_Full |
147 SUPPORTED_10000baseKX4_Full |
148 SUPPORTED_10000baseKR_Full |
149 SUPPORTED_10000baseR_FEC))
Chris Leechb8d23dc2014-09-25 11:55:42 -0700150 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
151
David Decotigny008eb732016-02-24 10:58:06 -0800152 if (ecmd.link_modes.supported[0] & (
153 SUPPORTED_20000baseMLD2_Full |
154 SUPPORTED_20000baseKR2_Full))
Chris Leechb8d23dc2014-09-25 11:55:42 -0700155 lport->link_supported_speeds |= FC_PORTSPEED_20GBIT;
156
David Decotigny008eb732016-02-24 10:58:06 -0800157 if (ecmd.link_modes.supported[0] & (
158 SUPPORTED_40000baseKR4_Full |
159 SUPPORTED_40000baseCR4_Full |
160 SUPPORTED_40000baseSR4_Full |
161 SUPPORTED_40000baseLR4_Full))
Chris Leechb8d23dc2014-09-25 11:55:42 -0700162 lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
163
Johannes Thumshirn0b924e52016-08-19 15:33:37 +0200164 lport->link_speed = eth2fc_speed(ecmd.base.speed);
Yi Zou03702682012-12-06 06:23:58 +0000165 return 0;
166 }
167 return -1;
168}
169EXPORT_SYMBOL_GPL(fcoe_link_speed_update);
170
Yi Zou57c27282012-12-06 06:24:13 +0000171/**
172 * __fcoe_get_lesb() - Get the Link Error Status Block (LESB) for a given lport
173 * @lport: The local port to update speeds for
174 * @fc_lesb: Pointer to the LESB to be filled up
175 * @netdev: Pointer to the netdev that is associated with the lport
176 *
177 * Note, the Link Error Status Block (LESB) for FCoE is defined in FC-BB-6
178 * Clause 7.11 in v1.04.
179 */
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -0700180void __fcoe_get_lesb(struct fc_lport *lport,
181 struct fc_els_lesb *fc_lesb,
182 struct net_device *netdev)
183{
184 unsigned int cpu;
185 u32 lfc, vlfc, mdac;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700186 struct fc_stats *stats;
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -0700187 struct fcoe_fc_els_lesb *lesb;
188 struct rtnl_link_stats64 temp;
189
190 lfc = 0;
191 vlfc = 0;
192 mdac = 0;
193 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
194 memset(lesb, 0, sizeof(*lesb));
195 for_each_possible_cpu(cpu) {
Vasu Dev1bd49b42012-05-25 10:26:43 -0700196 stats = per_cpu_ptr(lport->stats, cpu);
197 lfc += stats->LinkFailureCount;
198 vlfc += stats->VLinkFailureCount;
199 mdac += stats->MissDiscAdvCount;
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -0700200 }
201 lesb->lesb_link_fail = htonl(lfc);
202 lesb->lesb_vlink_fail = htonl(vlfc);
203 lesb->lesb_miss_fka = htonl(mdac);
204 lesb->lesb_fcs_error =
205 htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
206}
207EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
208
Yi Zou57c27282012-12-06 06:24:13 +0000209/**
210 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
211 * @lport: the local port
212 * @fc_lesb: the link error status block
213 */
214void fcoe_get_lesb(struct fc_lport *lport,
215 struct fc_els_lesb *fc_lesb)
216{
217 struct net_device *netdev = fcoe_get_netdev(lport);
218
219 __fcoe_get_lesb(lport, fc_lesb, netdev);
220}
221EXPORT_SYMBOL_GPL(fcoe_get_lesb);
222
223/**
224 * fcoe_ctlr_get_lesb() - Get the Link Error Status Block (LESB) for a given
225 * fcoe controller device
226 * @ctlr_dev: The given fcoe controller device
227 *
228 */
229void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *ctlr_dev)
230{
231 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
232 struct net_device *netdev = fcoe_get_netdev(fip->lp);
Yi Zou418a8cfe2013-05-18 06:28:17 +0000233 struct fc_els_lesb *fc_lesb;
Yi Zou57c27282012-12-06 06:24:13 +0000234
Yi Zou418a8cfe2013-05-18 06:28:17 +0000235 fc_lesb = (struct fc_els_lesb *)(&ctlr_dev->lesb);
236 __fcoe_get_lesb(fip->lp, fc_lesb, netdev);
Yi Zou57c27282012-12-06 06:24:13 +0000237}
238EXPORT_SYMBOL_GPL(fcoe_ctlr_get_lesb);
239
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -0700240void fcoe_wwn_to_str(u64 wwn, char *buf, int len)
241{
242 u8 wwpn[8];
243
244 u64_to_wwn(wwn, wwpn);
245 snprintf(buf, len, "%02x%02x%02x%02x%02x%02x%02x%02x",
246 wwpn[0], wwpn[1], wwpn[2], wwpn[3],
247 wwpn[4], wwpn[5], wwpn[6], wwpn[7]);
248}
249EXPORT_SYMBOL_GPL(fcoe_wwn_to_str);
250
251/**
252 * fcoe_validate_vport_create() - Validate a vport before creating it
253 * @vport: NPIV port to be created
254 *
255 * This routine is meant to add validation for a vport before creating it
256 * via fcoe_vport_create().
257 * Current validations are:
258 * - WWPN supplied is unique for given lport
259 */
260int fcoe_validate_vport_create(struct fc_vport *vport)
261{
262 struct Scsi_Host *shost = vport_to_shost(vport);
263 struct fc_lport *n_port = shost_priv(shost);
264 struct fc_lport *vn_port;
265 int rc = 0;
266 char buf[32];
267
268 mutex_lock(&n_port->lp_mutex);
269
270 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
271 /* Check if the wwpn is not same as that of the lport */
272 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
273 LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
274 "base port WWPN\n", buf);
275 rc = -EINVAL;
276 goto out;
277 }
278
279 /* Check if there is any existing vport with same wwpn */
280 list_for_each_entry(vn_port, &n_port->vports, list) {
281 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
282 LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
283 "already exists\n", buf);
284 rc = -EINVAL;
285 break;
286 }
287 }
288out:
289 mutex_unlock(&n_port->lp_mutex);
290 return rc;
291}
292EXPORT_SYMBOL_GPL(fcoe_validate_vport_create);
293
294/**
295 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
296 * @netdev: the associated net device
297 * @wwn: the output WWN
298 * @type: the type of WWN (WWPN or WWNN)
299 *
300 * Returns: 0 for success
301 */
302int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
303{
304 const struct net_device_ops *ops = netdev->netdev_ops;
305
306 if (ops->ndo_fcoe_get_wwn)
307 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
308 return -EINVAL;
309}
310EXPORT_SYMBOL_GPL(fcoe_get_wwn);
311
Yi Zoufdecf312011-01-28 16:04:55 -0800312/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800313 * fcoe_fc_crc() - Calculates the CRC for a given frame
314 * @fp: The frame to be checksumed
315 *
316 * This uses crc32() routine to calculate the CRC for a frame
317 *
318 * Return: The 32 bit CRC value
319 */
320u32 fcoe_fc_crc(struct fc_frame *fp)
321{
322 struct sk_buff *skb = fp_skb(fp);
323 struct skb_frag_struct *frag;
324 unsigned char *data;
325 unsigned long off, len, clen;
326 u32 crc;
327 unsigned i;
328
329 crc = crc32(~0, skb->data, skb_headlen(skb));
330
331 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
332 frag = &skb_shinfo(skb)->frags[i];
333 off = frag->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000334 len = skb_frag_size(frag);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800335 while (len > 0) {
336 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
Ian Campbell165c68d2011-08-24 22:28:15 +0000337 data = kmap_atomic(
Cong Wang77dfce02011-11-25 23:14:23 +0800338 skb_frag_page(frag) + (off >> PAGE_SHIFT));
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800339 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
Cong Wang77dfce02011-11-25 23:14:23 +0800340 kunmap_atomic(data);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800341 off += clen;
342 len -= clen;
343 }
344 }
345 return crc;
346}
347EXPORT_SYMBOL_GPL(fcoe_fc_crc);
348
349/**
350 * fcoe_start_io() - Start FCoE I/O
351 * @skb: The packet to be transmitted
352 *
353 * This routine is called from the net device to start transmitting
354 * FCoE packets.
355 *
356 * Returns: 0 for success
357 */
358int fcoe_start_io(struct sk_buff *skb)
359{
360 struct sk_buff *nskb;
361 int rc;
362
363 nskb = skb_clone(skb, GFP_ATOMIC);
364 if (!nskb)
365 return -ENOMEM;
366 rc = dev_queue_xmit(nskb);
367 if (rc != 0)
368 return rc;
369 kfree_skb(skb);
370 return 0;
371}
372EXPORT_SYMBOL_GPL(fcoe_start_io);
373
374
375/**
376 * fcoe_clean_pending_queue() - Dequeue a skb and free it
377 * @lport: The local port to dequeue a skb on
378 */
379void fcoe_clean_pending_queue(struct fc_lport *lport)
380{
381 struct fcoe_port *port = lport_priv(lport);
382 struct sk_buff *skb;
383
384 spin_lock_bh(&port->fcoe_pending_queue.lock);
385 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
386 spin_unlock_bh(&port->fcoe_pending_queue.lock);
387 kfree_skb(skb);
388 spin_lock_bh(&port->fcoe_pending_queue.lock);
389 }
390 spin_unlock_bh(&port->fcoe_pending_queue.lock);
391}
392EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
393
394/**
395 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
396 * @lport: The local port whose backlog is to be cleared
397 *
398 * This empties the wait_queue, dequeues the head of the wait_queue queue
399 * and calls fcoe_start_io() for each packet. If all skb have been
400 * transmitted it returns the qlen. If an error occurs it restores
401 * wait_queue (to try again later) and returns -1.
402 *
403 * The wait_queue is used when the skb transmit fails. The failed skb
404 * will go in the wait_queue which will be emptied by the timer function or
405 * by the next skb transmit.
406 */
407void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
408{
409 struct fcoe_port *port = lport_priv(lport);
410 int rc;
411
412 spin_lock_bh(&port->fcoe_pending_queue.lock);
413
414 if (skb)
415 __skb_queue_tail(&port->fcoe_pending_queue, skb);
416
417 if (port->fcoe_pending_queue_active)
418 goto out;
419 port->fcoe_pending_queue_active = 1;
420
421 while (port->fcoe_pending_queue.qlen) {
422 /* keep qlen > 0 until fcoe_start_io succeeds */
423 port->fcoe_pending_queue.qlen++;
424 skb = __skb_dequeue(&port->fcoe_pending_queue);
425
426 spin_unlock_bh(&port->fcoe_pending_queue.lock);
427 rc = fcoe_start_io(skb);
428 spin_lock_bh(&port->fcoe_pending_queue.lock);
429
430 if (rc) {
431 __skb_queue_head(&port->fcoe_pending_queue, skb);
432 /* undo temporary increment above */
433 port->fcoe_pending_queue.qlen--;
434 break;
435 }
436 /* undo temporary increment above */
437 port->fcoe_pending_queue.qlen--;
438 }
439
440 if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
441 lport->qfull = 0;
442 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
443 mod_timer(&port->timer, jiffies + 2);
444 port->fcoe_pending_queue_active = 0;
445out:
446 if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
447 lport->qfull = 1;
448 spin_unlock_bh(&port->fcoe_pending_queue.lock);
449}
450EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
451
452/**
453 * fcoe_queue_timer() - The fcoe queue timer
454 * @lport: The local port
455 *
456 * Calls fcoe_check_wait_queue on timeout
457 */
458void fcoe_queue_timer(ulong lport)
459{
460 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
461}
462EXPORT_SYMBOL_GPL(fcoe_queue_timer);
463
464/**
465 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
466 * @skb: The packet to be transmitted
467 * @tlen: The total length of the trailer
468 * @fps: The fcoe context
469 *
470 * This routine allocates a page for frame trailers. The page is re-used if
471 * there is enough room left on it for the current trailer. If there isn't
472 * enough buffer left a new page is allocated for the trailer. Reference to
473 * the page from this function as well as the skbs using the page fragments
474 * ensure that the page is freed at the appropriate time.
475 *
476 * Returns: 0 for success
477 */
478int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
479 struct fcoe_percpu_s *fps)
480{
481 struct page *page;
482
483 page = fps->crc_eof_page;
484 if (!page) {
485 page = alloc_page(GFP_ATOMIC);
486 if (!page)
487 return -ENOMEM;
488
489 fps->crc_eof_page = page;
490 fps->crc_eof_offset = 0;
491 }
492
493 get_page(page);
494 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
495 fps->crc_eof_offset, tlen);
496 skb->len += tlen;
497 skb->data_len += tlen;
498 skb->truesize += tlen;
499 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
500
501 if (fps->crc_eof_offset >= PAGE_SIZE) {
502 fps->crc_eof_page = NULL;
503 fps->crc_eof_offset = 0;
504 put_page(page);
505 }
506
507 return 0;
508}
509EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
510
511/**
Yi Zoufdecf312011-01-28 16:04:55 -0800512 * fcoe_transport_lookup - find an fcoe transport that matches a netdev
513 * @netdev: The netdev to look for from all attached transports
514 *
515 * Returns : ptr to the fcoe transport that supports this netdev or NULL
516 * if not found.
517 *
518 * The ft_mutex should be held when this is called
519 */
520static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
521{
522 struct fcoe_transport *ft = NULL;
523
524 list_for_each_entry(ft, &fcoe_transports, list)
525 if (ft->match && ft->match(netdev))
526 return ft;
527 return NULL;
528}
529
530/**
531 * fcoe_transport_attach - Attaches an FCoE transport
532 * @ft: The fcoe transport to be attached
533 *
534 * Returns : 0 for success
535 */
536int fcoe_transport_attach(struct fcoe_transport *ft)
537{
538 int rc = 0;
539
540 mutex_lock(&ft_mutex);
541 if (ft->attached) {
542 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
543 ft->name);
544 rc = -EEXIST;
545 goto out_attach;
546 }
547
548 /* Add default transport to the tail */
549 if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
550 list_add(&ft->list, &fcoe_transports);
551 else
552 list_add_tail(&ft->list, &fcoe_transports);
553
554 ft->attached = true;
555 LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
556
557out_attach:
558 mutex_unlock(&ft_mutex);
559 return rc;
560}
561EXPORT_SYMBOL(fcoe_transport_attach);
562
563/**
Yi Zou4ef7fb12011-04-01 16:06:30 -0700564 * fcoe_transport_detach - Detaches an FCoE transport
Yi Zoufdecf312011-01-28 16:04:55 -0800565 * @ft: The fcoe transport to be attached
566 *
567 * Returns : 0 for success
568 */
569int fcoe_transport_detach(struct fcoe_transport *ft)
570{
571 int rc = 0;
Yi Zou69922fc2011-04-01 16:06:19 -0700572 struct fcoe_netdev_mapping *nm = NULL, *tmp;
Yi Zoufdecf312011-01-28 16:04:55 -0800573
574 mutex_lock(&ft_mutex);
575 if (!ft->attached) {
576 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
577 ft->name);
578 rc = -ENODEV;
579 goto out_attach;
580 }
581
Yi Zou69922fc2011-04-01 16:06:19 -0700582 /* remove netdev mapping for this transport as it is going away */
583 mutex_lock(&fn_mutex);
584 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
585 if (nm->ft == ft) {
586 LIBFCOE_TRANSPORT_DBG("transport %s going away, "
587 "remove its netdev mapping for %s\n",
588 ft->name, nm->netdev->name);
589 list_del(&nm->list);
590 kfree(nm);
591 }
592 }
593 mutex_unlock(&fn_mutex);
594
Yi Zoufdecf312011-01-28 16:04:55 -0800595 list_del(&ft->list);
596 ft->attached = false;
597 LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
598
599out_attach:
600 mutex_unlock(&ft_mutex);
601 return rc;
602
603}
604EXPORT_SYMBOL(fcoe_transport_detach);
605
606static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
607{
608 int i, j;
609 struct fcoe_transport *ft = NULL;
610
611 i = j = sprintf(buffer, "Attached FCoE transports:");
612 mutex_lock(&ft_mutex);
613 list_for_each_entry(ft, &fcoe_transports, list) {
Yi Zoua01a5a52011-04-01 16:06:25 -0700614 if (i >= PAGE_SIZE - IFNAMSIZ)
Yi Zoufdecf312011-01-28 16:04:55 -0800615 break;
Yi Zoua01a5a52011-04-01 16:06:25 -0700616 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
Yi Zoufdecf312011-01-28 16:04:55 -0800617 }
618 mutex_unlock(&ft_mutex);
619 if (i == j)
620 i += snprintf(&buffer[i], IFNAMSIZ, "none");
621 return i;
622}
623
624static int __init fcoe_transport_init(void)
625{
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800626 register_netdevice_notifier(&libfcoe_notifier);
Yi Zoufdecf312011-01-28 16:04:55 -0800627 return 0;
628}
629
Mark Rustad87098bd2012-06-06 11:59:48 -0700630static int fcoe_transport_exit(void)
Yi Zoufdecf312011-01-28 16:04:55 -0800631{
632 struct fcoe_transport *ft;
633
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800634 unregister_netdevice_notifier(&libfcoe_notifier);
Yi Zoufdecf312011-01-28 16:04:55 -0800635 mutex_lock(&ft_mutex);
636 list_for_each_entry(ft, &fcoe_transports, list)
637 printk(KERN_ERR "FCoE transport %s is still attached!\n",
638 ft->name);
639 mutex_unlock(&ft_mutex);
640 return 0;
641}
642
643
644static int fcoe_add_netdev_mapping(struct net_device *netdev,
645 struct fcoe_transport *ft)
646{
647 struct fcoe_netdev_mapping *nm;
648
649 nm = kmalloc(sizeof(*nm), GFP_KERNEL);
650 if (!nm) {
651 printk(KERN_ERR "Unable to allocate netdev_mapping");
652 return -ENOMEM;
653 }
654
655 nm->netdev = netdev;
656 nm->ft = ft;
657
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800658 mutex_lock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800659 list_add(&nm->list, &fcoe_netdevs);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800660 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800661 return 0;
662}
663
664
665static void fcoe_del_netdev_mapping(struct net_device *netdev)
666{
667 struct fcoe_netdev_mapping *nm = NULL, *tmp;
668
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800669 mutex_lock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800670 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
671 if (nm->netdev == netdev) {
672 list_del(&nm->list);
673 kfree(nm);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800674 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800675 return;
676 }
677 }
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800678 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800679}
680
681
682/**
683 * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
684 * it was created
685 *
686 * Returns : ptr to the fcoe transport that supports this netdev or NULL
687 * if not found.
688 *
689 * The ft_mutex should be held when this is called
690 */
691static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
692{
693 struct fcoe_transport *ft = NULL;
694 struct fcoe_netdev_mapping *nm;
695
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800696 mutex_lock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800697 list_for_each_entry(nm, &fcoe_netdevs, list) {
698 if (netdev == nm->netdev) {
699 ft = nm->ft;
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800700 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800701 return ft;
702 }
703 }
704
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800705 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800706 return NULL;
707}
708
709/**
710 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
711 * @buffer: The name of the net device
712 *
713 * Returns: NULL or a ptr to net_device
714 */
715static struct net_device *fcoe_if_to_netdev(const char *buffer)
716{
717 char *cp;
718 char ifname[IFNAMSIZ + 2];
719
720 if (buffer) {
721 strlcpy(ifname, buffer, IFNAMSIZ);
722 cp = ifname + strlen(ifname);
723 while (--cp >= ifname && *cp == '\n')
724 *cp = '\0';
725 return dev_get_by_name(&init_net, ifname);
726 }
727 return NULL;
728}
729
730/**
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800731 * libfcoe_device_notification() - Handler for net device events
732 * @notifier: The context of the notification
733 * @event: The type of event
734 * @ptr: The net device that the event was on
735 *
736 * This function is called by the Ethernet driver in case of link change event.
737 *
738 * Returns: 0 for success
739 */
740static int libfcoe_device_notification(struct notifier_block *notifier,
741 ulong event, void *ptr)
742{
Jiri Pirko351638e2013-05-28 01:30:21 +0000743 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800744
745 switch (event) {
746 case NETDEV_UNREGISTER:
Robert Loveb99fbf62012-02-10 17:17:59 -0800747 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
748 netdev->name);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800749 fcoe_del_netdev_mapping(netdev);
750 break;
751 }
752 return NOTIFY_OK;
753}
754
Robert Love6a891b02012-11-27 06:53:30 +0000755ssize_t fcoe_ctlr_create_store(struct bus_type *bus,
756 const char *buf, size_t count)
757{
758 struct net_device *netdev = NULL;
759 struct fcoe_transport *ft = NULL;
Robert Love6a891b02012-11-27 06:53:30 +0000760 int rc = 0;
761 int err;
762
763 mutex_lock(&ft_mutex);
764
765 netdev = fcoe_if_to_netdev(buf);
766 if (!netdev) {
767 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
768 rc = -ENODEV;
769 goto out_nodev;
770 }
771
772 ft = fcoe_netdev_map_lookup(netdev);
773 if (ft) {
774 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
775 "FCoE instance on %s.\n",
776 ft->name, netdev->name);
777 rc = -EEXIST;
778 goto out_putdev;
779 }
780
781 ft = fcoe_transport_lookup(netdev);
782 if (!ft) {
783 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
784 netdev->name);
785 rc = -ENODEV;
786 goto out_putdev;
787 }
788
789 /* pass to transport create */
790 err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
791 if (err) {
792 fcoe_del_netdev_mapping(netdev);
793 rc = -ENOMEM;
794 goto out_putdev;
795 }
796
797 err = fcoe_add_netdev_mapping(netdev, ft);
798 if (err) {
799 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
800 "for FCoE transport %s for %s.\n",
801 ft->name, netdev->name);
802 rc = -ENODEV;
803 goto out_putdev;
804 }
805
Robert Lovea2ceb1f2013-06-11 07:28:19 +0000806 LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
807 ft->name, netdev->name);
Robert Love6a891b02012-11-27 06:53:30 +0000808
809out_putdev:
810 dev_put(netdev);
811out_nodev:
812 mutex_unlock(&ft_mutex);
813 if (rc)
814 return rc;
815 return count;
816}
817
818ssize_t fcoe_ctlr_destroy_store(struct bus_type *bus,
819 const char *buf, size_t count)
820{
821 int rc = -ENODEV;
822 struct net_device *netdev = NULL;
823 struct fcoe_transport *ft = NULL;
824
825 mutex_lock(&ft_mutex);
826
827 netdev = fcoe_if_to_netdev(buf);
828 if (!netdev) {
829 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
830 goto out_nodev;
831 }
832
833 ft = fcoe_netdev_map_lookup(netdev);
834 if (!ft) {
835 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
836 netdev->name);
837 goto out_putdev;
838 }
839
840 /* pass to transport destroy */
841 rc = ft->destroy(netdev);
842 if (rc)
843 goto out_putdev;
844
845 fcoe_del_netdev_mapping(netdev);
846 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
847 ft->name, (rc) ? "failed" : "succeeded",
848 netdev->name);
849 rc = count; /* required for successful return */
850out_putdev:
851 dev_put(netdev);
852out_nodev:
853 mutex_unlock(&ft_mutex);
854 return rc;
855}
856EXPORT_SYMBOL(fcoe_ctlr_destroy_store);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800857
858/**
Yi Zoufdecf312011-01-28 16:04:55 -0800859 * fcoe_transport_create() - Create a fcoe interface
860 * @buffer: The name of the Ethernet interface to create on
861 * @kp: The associated kernel param
862 *
863 * Called from sysfs. This holds the ft_mutex while calling the
864 * registered fcoe transport's create function.
865 *
866 * Returns: 0 for success
867 */
868static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
869{
870 int rc = -ENODEV;
871 struct net_device *netdev = NULL;
872 struct fcoe_transport *ft = NULL;
873 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
874
Robert Loveb3960af2011-04-01 16:05:53 -0700875 mutex_lock(&ft_mutex);
876
Yi Zoufdecf312011-01-28 16:04:55 -0800877 netdev = fcoe_if_to_netdev(buffer);
878 if (!netdev) {
879 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
880 goto out_nodev;
881 }
882
883 ft = fcoe_netdev_map_lookup(netdev);
884 if (ft) {
885 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
886 "FCoE instance on %s.\n",
887 ft->name, netdev->name);
888 rc = -EEXIST;
889 goto out_putdev;
890 }
891
892 ft = fcoe_transport_lookup(netdev);
893 if (!ft) {
894 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
895 netdev->name);
896 goto out_putdev;
897 }
898
899 rc = fcoe_add_netdev_mapping(netdev, ft);
900 if (rc) {
901 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
902 "for FCoE transport %s for %s.\n",
903 ft->name, netdev->name);
904 goto out_putdev;
905 }
906
907 /* pass to transport create */
908 rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
909 if (rc)
910 fcoe_del_netdev_mapping(netdev);
911
912 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
913 ft->name, (rc) ? "failed" : "succeeded",
914 netdev->name);
915
916out_putdev:
917 dev_put(netdev);
918out_nodev:
919 mutex_unlock(&ft_mutex);
Robert Loveb3960af2011-04-01 16:05:53 -0700920 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -0800921}
922
923/**
924 * fcoe_transport_destroy() - Destroy a FCoE interface
925 * @buffer: The name of the Ethernet interface to be destroyed
926 * @kp: The associated kernel parameter
927 *
928 * Called from sysfs. This holds the ft_mutex while calling the
929 * registered fcoe transport's destroy function.
930 *
931 * Returns: 0 for success
932 */
933static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp)
934{
935 int rc = -ENODEV;
936 struct net_device *netdev = NULL;
937 struct fcoe_transport *ft = NULL;
938
Robert Loveb3960af2011-04-01 16:05:53 -0700939 mutex_lock(&ft_mutex);
940
Yi Zoufdecf312011-01-28 16:04:55 -0800941 netdev = fcoe_if_to_netdev(buffer);
942 if (!netdev) {
943 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
944 goto out_nodev;
945 }
946
947 ft = fcoe_netdev_map_lookup(netdev);
948 if (!ft) {
949 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
950 netdev->name);
951 goto out_putdev;
952 }
953
954 /* pass to transport destroy */
955 rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
956 fcoe_del_netdev_mapping(netdev);
957 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
958 ft->name, (rc) ? "failed" : "succeeded",
959 netdev->name);
960
961out_putdev:
962 dev_put(netdev);
963out_nodev:
964 mutex_unlock(&ft_mutex);
Robert Loveb3960af2011-04-01 16:05:53 -0700965 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -0800966}
967
968/**
969 * fcoe_transport_disable() - Disables a FCoE interface
970 * @buffer: The name of the Ethernet interface to be disabled
971 * @kp: The associated kernel parameter
972 *
973 * Called from sysfs.
974 *
975 * Returns: 0 for success
976 */
977static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
978{
979 int rc = -ENODEV;
980 struct net_device *netdev = NULL;
981 struct fcoe_transport *ft = NULL;
982
Robert Loveb3960af2011-04-01 16:05:53 -0700983 mutex_lock(&ft_mutex);
984
Yi Zoufdecf312011-01-28 16:04:55 -0800985 netdev = fcoe_if_to_netdev(buffer);
986 if (!netdev)
987 goto out_nodev;
988
989 ft = fcoe_netdev_map_lookup(netdev);
990 if (!ft)
991 goto out_putdev;
992
993 rc = ft->disable ? ft->disable(netdev) : -ENODEV;
994
995out_putdev:
996 dev_put(netdev);
997out_nodev:
998 mutex_unlock(&ft_mutex);
Al Viro4f670ff2012-12-07 14:10:14 +0000999 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -08001000}
1001
1002/**
1003 * fcoe_transport_enable() - Enables a FCoE interface
1004 * @buffer: The name of the Ethernet interface to be enabled
1005 * @kp: The associated kernel parameter
1006 *
1007 * Called from sysfs.
1008 *
1009 * Returns: 0 for success
1010 */
1011static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
1012{
1013 int rc = -ENODEV;
1014 struct net_device *netdev = NULL;
1015 struct fcoe_transport *ft = NULL;
1016
Robert Loveb3960af2011-04-01 16:05:53 -07001017 mutex_lock(&ft_mutex);
1018
Yi Zoufdecf312011-01-28 16:04:55 -08001019 netdev = fcoe_if_to_netdev(buffer);
1020 if (!netdev)
1021 goto out_nodev;
1022
1023 ft = fcoe_netdev_map_lookup(netdev);
1024 if (!ft)
1025 goto out_putdev;
1026
1027 rc = ft->enable ? ft->enable(netdev) : -ENODEV;
1028
1029out_putdev:
1030 dev_put(netdev);
1031out_nodev:
1032 mutex_unlock(&ft_mutex);
Robert Loveb3960af2011-04-01 16:05:53 -07001033 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -08001034}
1035
1036/**
1037 * libfcoe_init() - Initialization routine for libfcoe.ko
1038 */
1039static int __init libfcoe_init(void)
1040{
Robert Love9a74e882012-05-22 19:06:21 -07001041 int rc = 0;
Yi Zoufdecf312011-01-28 16:04:55 -08001042
Robert Love9a74e882012-05-22 19:06:21 -07001043 rc = fcoe_transport_init();
1044 if (rc)
1045 return rc;
1046
1047 rc = fcoe_sysfs_setup();
1048 if (rc)
1049 fcoe_transport_exit();
1050
1051 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -08001052}
1053module_init(libfcoe_init);
1054
1055/**
1056 * libfcoe_exit() - Tear down libfcoe.ko
1057 */
1058static void __exit libfcoe_exit(void)
1059{
Robert Love9a74e882012-05-22 19:06:21 -07001060 fcoe_sysfs_teardown();
Yi Zoufdecf312011-01-28 16:04:55 -08001061 fcoe_transport_exit();
1062}
1063module_exit(libfcoe_exit);