blob: 5f9ae6e9897eac901093b2874af8fad4899e0a25 [file] [log] [blame]
Dupuis, Chad61d86582017-02-15 06:28:23 -08001/*
2 * QLogic FCoE Offload Driver
Chad Dupuis12d0b122017-05-31 06:33:49 -07003 * Copyright (c) 2016-2017 Cavium Inc.
Dupuis, Chad61d86582017-02-15 06:28:23 -08004 *
5 * This software is available under the terms of the GNU General Public License
6 * (GPL) Version 2, available from the file COPYING in the main directory of
7 * this source tree.
8 */
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/device.h>
14#include <linux/highmem.h>
15#include <linux/crc32.h>
16#include <linux/interrupt.h>
17#include <linux/list.h>
18#include <linux/kthread.h>
19#include <scsi/libfc.h>
20#include <scsi/scsi_host.h>
Chad Dupuisa3cd42a92017-08-15 10:08:17 -070021#include <scsi/fc_frame.h>
Dupuis, Chad61d86582017-02-15 06:28:23 -080022#include <linux/if_ether.h>
23#include <linux/if_vlan.h>
24#include <linux/cpu.h>
25#include "qedf.h"
Arnd Bergmannd9ea4632018-02-02 14:12:18 +010026#include "qedf_dbg.h"
Chad Dupuis5185b322017-05-31 06:33:48 -070027#include <uapi/linux/pci_regs.h>
Dupuis, Chad61d86582017-02-15 06:28:23 -080028
29const struct qed_fcoe_ops *qed_ops;
30
31static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id);
32static void qedf_remove(struct pci_dev *pdev);
33
Dupuis, Chad61d86582017-02-15 06:28:23 -080034/*
35 * Driver module parameters.
36 */
37static unsigned int qedf_dev_loss_tmo = 60;
38module_param_named(dev_loss_tmo, qedf_dev_loss_tmo, int, S_IRUGO);
39MODULE_PARM_DESC(dev_loss_tmo, " dev_loss_tmo setting for attached "
40 "remote ports (default 60)");
41
42uint qedf_debug = QEDF_LOG_INFO;
43module_param_named(debug, qedf_debug, uint, S_IRUGO);
Chad Dupuis0516abd2017-08-15 10:08:20 -070044MODULE_PARM_DESC(debug, " Debug mask. Pass '1' to enable default debugging"
Dupuis, Chad61d86582017-02-15 06:28:23 -080045 " mask");
46
Chad Dupuisc3ef86f2018-04-25 06:08:46 -070047static uint qedf_fipvlan_retries = 60;
Dupuis, Chad61d86582017-02-15 06:28:23 -080048module_param_named(fipvlan_retries, qedf_fipvlan_retries, int, S_IRUGO);
49MODULE_PARM_DESC(fipvlan_retries, " Number of FIP VLAN requests to attempt "
Chad Dupuisc3ef86f2018-04-25 06:08:46 -070050 "before giving up (default 60)");
Dupuis, Chad61d86582017-02-15 06:28:23 -080051
52static uint qedf_fallback_vlan = QEDF_FALLBACK_VLAN;
53module_param_named(fallback_vlan, qedf_fallback_vlan, int, S_IRUGO);
54MODULE_PARM_DESC(fallback_vlan, " VLAN ID to try if fip vlan request fails "
55 "(default 1002).");
56
Chad Dupuis84b2ba62018-04-25 06:08:52 -070057static int qedf_default_prio = -1;
Dupuis, Chad61d86582017-02-15 06:28:23 -080058module_param_named(default_prio, qedf_default_prio, int, S_IRUGO);
Chad Dupuis84b2ba62018-04-25 06:08:52 -070059MODULE_PARM_DESC(default_prio, " Override 802.1q priority for FIP and FCoE"
60 " traffic (value between 0 and 7, default 3).");
Dupuis, Chad61d86582017-02-15 06:28:23 -080061
62uint qedf_dump_frames;
63module_param_named(dump_frames, qedf_dump_frames, int, S_IRUGO | S_IWUSR);
64MODULE_PARM_DESC(dump_frames, " Print the skb data of FIP and FCoE frames "
65 "(default off)");
66
67static uint qedf_queue_depth;
68module_param_named(queue_depth, qedf_queue_depth, int, S_IRUGO);
69MODULE_PARM_DESC(queue_depth, " Sets the queue depth for all LUNs discovered "
70 "by the qedf driver. Default is 0 (use OS default).");
71
72uint qedf_io_tracing;
73module_param_named(io_tracing, qedf_io_tracing, int, S_IRUGO | S_IWUSR);
74MODULE_PARM_DESC(io_tracing, " Enable logging of SCSI requests/completions "
75 "into trace buffer. (default off).");
76
77static uint qedf_max_lun = MAX_FIBRE_LUNS;
78module_param_named(max_lun, qedf_max_lun, int, S_IRUGO);
79MODULE_PARM_DESC(max_lun, " Sets the maximum luns per target that the driver "
80 "supports. (default 0xffffffff)");
81
82uint qedf_link_down_tmo;
83module_param_named(link_down_tmo, qedf_link_down_tmo, int, S_IRUGO);
84MODULE_PARM_DESC(link_down_tmo, " Delays informing the fcoe transport that the "
85 "link is down by N seconds.");
86
87bool qedf_retry_delay;
88module_param_named(retry_delay, qedf_retry_delay, bool, S_IRUGO | S_IWUSR);
89MODULE_PARM_DESC(retry_delay, " Enable/disable handling of FCP_RSP IU retry "
90 "delay handling (default off).");
91
Chad Dupuisba17d372018-04-25 06:08:51 -070092static bool qedf_dcbx_no_wait;
93module_param_named(dcbx_no_wait, qedf_dcbx_no_wait, bool, S_IRUGO | S_IWUSR);
94MODULE_PARM_DESC(dcbx_no_wait, " Do not wait for DCBX convergence to start "
95 "sending FIP VLAN requests on link up (Default: off).");
96
Dupuis, Chad61d86582017-02-15 06:28:23 -080097static uint qedf_dp_module;
98module_param_named(dp_module, qedf_dp_module, uint, S_IRUGO);
99MODULE_PARM_DESC(dp_module, " bit flags control for verbose printk passed "
100 "qed module during probe.");
101
Chad Dupuis2b82a622017-05-31 06:33:54 -0700102static uint qedf_dp_level = QED_LEVEL_NOTICE;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800103module_param_named(dp_level, qedf_dp_level, uint, S_IRUGO);
104MODULE_PARM_DESC(dp_level, " printk verbosity control passed to qed module "
105 "during probe (0-3: 0 more verbose).");
106
107struct workqueue_struct *qedf_io_wq;
108
109static struct fcoe_percpu_s qedf_global;
110static DEFINE_SPINLOCK(qedf_global_lock);
111
112static struct kmem_cache *qedf_io_work_cache;
113
114void qedf_set_vlan_id(struct qedf_ctx *qedf, int vlan_id)
115{
116 qedf->vlan_id = vlan_id;
Chad Dupuis84b2ba62018-04-25 06:08:52 -0700117 qedf->vlan_id |= qedf->prio << VLAN_PRIO_SHIFT;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800118 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Setting vlan_id=%04x "
Chad Dupuis84b2ba62018-04-25 06:08:52 -0700119 "prio=%d.\n", vlan_id, qedf->prio);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800120}
121
122/* Returns true if we have a valid vlan, false otherwise */
123static bool qedf_initiate_fipvlan_req(struct qedf_ctx *qedf)
124{
125 int rc;
126
127 if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
128 QEDF_ERR(&(qedf->dbg_ctx), "Link not up.\n");
129 return false;
130 }
131
132 while (qedf->fipvlan_retries--) {
133 if (qedf->vlan_id > 0)
134 return true;
135 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
136 "Retry %d.\n", qedf->fipvlan_retries);
137 init_completion(&qedf->fipvlan_compl);
138 qedf_fcoe_send_vlan_req(qedf);
139 rc = wait_for_completion_timeout(&qedf->fipvlan_compl,
140 1 * HZ);
141 if (rc > 0) {
142 fcoe_ctlr_link_up(&qedf->ctlr);
143 return true;
144 }
145 }
146
147 return false;
148}
149
150static void qedf_handle_link_update(struct work_struct *work)
151{
152 struct qedf_ctx *qedf =
153 container_of(work, struct qedf_ctx, link_update.work);
154 int rc;
155
156 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Entered.\n");
157
158 if (atomic_read(&qedf->link_state) == QEDF_LINK_UP) {
159 rc = qedf_initiate_fipvlan_req(qedf);
160 if (rc)
161 return;
162 /*
163 * If we get here then we never received a repsonse to our
164 * fip vlan request so set the vlan_id to the default and
165 * tell FCoE that the link is up
166 */
167 QEDF_WARN(&(qedf->dbg_ctx), "Did not receive FIP VLAN "
168 "response, falling back to default VLAN %d.\n",
169 qedf_fallback_vlan);
Chad Dupuiscf291162017-08-15 10:08:18 -0700170 qedf_set_vlan_id(qedf, qedf_fallback_vlan);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800171
172 /*
173 * Zero out data_src_addr so we'll update it with the new
174 * lport port_id
175 */
176 eth_zero_addr(qedf->data_src_addr);
177 fcoe_ctlr_link_up(&qedf->ctlr);
178 } else if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
179 /*
180 * If we hit here and link_down_tmo_valid is still 1 it means
181 * that link_down_tmo timed out so set it to 0 to make sure any
182 * other readers have accurate state.
183 */
184 atomic_set(&qedf->link_down_tmo_valid, 0);
185 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
186 "Calling fcoe_ctlr_link_down().\n");
187 fcoe_ctlr_link_down(&qedf->ctlr);
188 qedf_wait_for_upload(qedf);
189 /* Reset the number of FIP VLAN retries */
190 qedf->fipvlan_retries = qedf_fipvlan_retries;
191 }
192}
193
Chad Dupuisa3cd42a92017-08-15 10:08:17 -0700194#define QEDF_FCOE_MAC_METHOD_GRANGED_MAC 1
195#define QEDF_FCOE_MAC_METHOD_FCF_MAP 2
196#define QEDF_FCOE_MAC_METHOD_FCOE_SET_MAC 3
197static void qedf_set_data_src_addr(struct qedf_ctx *qedf, struct fc_frame *fp)
198{
199 u8 *granted_mac;
200 struct fc_frame_header *fh = fc_frame_header_get(fp);
201 u8 fc_map[3];
202 int method = 0;
203
204 /* Get granted MAC address from FIP FLOGI payload */
205 granted_mac = fr_cb(fp)->granted_mac;
206
207 /*
208 * We set the source MAC for FCoE traffic based on the Granted MAC
209 * address from the switch.
210 *
211 * If granted_mac is non-zero, we used that.
212 * If the granted_mac is zeroed out, created the FCoE MAC based on
213 * the sel_fcf->fc_map and the d_id fo the FLOGI frame.
214 * If sel_fcf->fc_map is 0 then we use the default FCF-MAC plus the
215 * d_id of the FLOGI frame.
216 */
217 if (!is_zero_ether_addr(granted_mac)) {
218 ether_addr_copy(qedf->data_src_addr, granted_mac);
219 method = QEDF_FCOE_MAC_METHOD_GRANGED_MAC;
220 } else if (qedf->ctlr.sel_fcf->fc_map != 0) {
221 hton24(fc_map, qedf->ctlr.sel_fcf->fc_map);
222 qedf->data_src_addr[0] = fc_map[0];
223 qedf->data_src_addr[1] = fc_map[1];
224 qedf->data_src_addr[2] = fc_map[2];
225 qedf->data_src_addr[3] = fh->fh_d_id[0];
226 qedf->data_src_addr[4] = fh->fh_d_id[1];
227 qedf->data_src_addr[5] = fh->fh_d_id[2];
228 method = QEDF_FCOE_MAC_METHOD_FCF_MAP;
229 } else {
230 fc_fcoe_set_mac(qedf->data_src_addr, fh->fh_d_id);
231 method = QEDF_FCOE_MAC_METHOD_FCOE_SET_MAC;
232 }
233
234 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
235 "QEDF data_src_mac=%pM method=%d.\n", qedf->data_src_addr, method);
236}
237
Dupuis, Chad61d86582017-02-15 06:28:23 -0800238static void qedf_flogi_resp(struct fc_seq *seq, struct fc_frame *fp,
239 void *arg)
240{
241 struct fc_exch *exch = fc_seq_exch(seq);
242 struct fc_lport *lport = exch->lp;
243 struct qedf_ctx *qedf = lport_priv(lport);
244
245 if (!qedf) {
246 QEDF_ERR(NULL, "qedf is NULL.\n");
247 return;
248 }
249
250 /*
251 * If ERR_PTR is set then don't try to stat anything as it will cause
252 * a crash when we access fp.
253 */
254 if (IS_ERR(fp)) {
255 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
256 "fp has IS_ERR() set.\n");
257 goto skip_stat;
258 }
259
260 /* Log stats for FLOGI reject */
261 if (fc_frame_payload_op(fp) == ELS_LS_RJT)
262 qedf->flogi_failed++;
Chad Dupuisa3cd42a92017-08-15 10:08:17 -0700263 else if (fc_frame_payload_op(fp) == ELS_LS_ACC) {
264 /* Set the source MAC we will use for FCoE traffic */
265 qedf_set_data_src_addr(qedf, fp);
266 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800267
268 /* Complete flogi_compl so we can proceed to sending ADISCs */
269 complete(&qedf->flogi_compl);
270
271skip_stat:
272 /* Report response to libfc */
273 fc_lport_flogi_resp(seq, fp, lport);
274}
275
276static struct fc_seq *qedf_elsct_send(struct fc_lport *lport, u32 did,
277 struct fc_frame *fp, unsigned int op,
278 void (*resp)(struct fc_seq *,
279 struct fc_frame *,
280 void *),
281 void *arg, u32 timeout)
282{
283 struct qedf_ctx *qedf = lport_priv(lport);
284
285 /*
286 * Intercept FLOGI for statistic purposes. Note we use the resp
287 * callback to tell if this is really a flogi.
288 */
289 if (resp == fc_lport_flogi_resp) {
290 qedf->flogi_cnt++;
291 return fc_elsct_send(lport, did, fp, op, qedf_flogi_resp,
292 arg, timeout);
293 }
294
295 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
296}
297
298int qedf_send_flogi(struct qedf_ctx *qedf)
299{
300 struct fc_lport *lport;
301 struct fc_frame *fp;
302
303 lport = qedf->lport;
304
305 if (!lport->tt.elsct_send)
306 return -EINVAL;
307
308 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
309 if (!fp) {
310 QEDF_ERR(&(qedf->dbg_ctx), "fc_frame_alloc failed.\n");
311 return -ENOMEM;
312 }
313
314 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
315 "Sending FLOGI to reestablish session with switch.\n");
316 lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
317 ELS_FLOGI, qedf_flogi_resp, lport, lport->r_a_tov);
318
319 init_completion(&qedf->flogi_compl);
320
321 return 0;
322}
323
324struct qedf_tmp_rdata_item {
325 struct fc_rport_priv *rdata;
326 struct list_head list;
327};
328
329/*
330 * This function is called if link_down_tmo is in use. If we get a link up and
331 * link_down_tmo has not expired then use just FLOGI/ADISC to recover our
332 * sessions with targets. Otherwise, just call fcoe_ctlr_link_up().
333 */
334static void qedf_link_recovery(struct work_struct *work)
335{
336 struct qedf_ctx *qedf =
337 container_of(work, struct qedf_ctx, link_recovery.work);
338 struct qedf_rport *fcport;
339 struct fc_rport_priv *rdata;
340 struct qedf_tmp_rdata_item *rdata_item, *tmp_rdata_item;
341 bool rc;
342 int retries = 30;
343 int rval, i;
344 struct list_head rdata_login_list;
345
346 INIT_LIST_HEAD(&rdata_login_list);
347
348 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
349 "Link down tmo did not expire.\n");
350
351 /*
352 * Essentially reset the fcoe_ctlr here without affecting the state
353 * of the libfc structs.
354 */
355 qedf->ctlr.state = FIP_ST_LINK_WAIT;
356 fcoe_ctlr_link_down(&qedf->ctlr);
357
358 /*
359 * Bring the link up before we send the fipvlan request so libfcoe
360 * can select a new fcf in parallel
361 */
362 fcoe_ctlr_link_up(&qedf->ctlr);
363
364 /* Since the link when down and up to verify which vlan we're on */
365 qedf->fipvlan_retries = qedf_fipvlan_retries;
366 rc = qedf_initiate_fipvlan_req(qedf);
Chad Dupuiscf291162017-08-15 10:08:18 -0700367 /* If getting the VLAN fails, set the VLAN to the fallback one */
Dupuis, Chad61d86582017-02-15 06:28:23 -0800368 if (!rc)
Chad Dupuiscf291162017-08-15 10:08:18 -0700369 qedf_set_vlan_id(qedf, qedf_fallback_vlan);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800370
371 /*
372 * We need to wait for an FCF to be selected due to the
373 * fcoe_ctlr_link_up other the FLOGI will be rejected.
374 */
375 while (retries > 0) {
376 if (qedf->ctlr.sel_fcf) {
377 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
378 "FCF reselected, proceeding with FLOGI.\n");
379 break;
380 }
381 msleep(500);
382 retries--;
383 }
384
385 if (retries < 1) {
386 QEDF_ERR(&(qedf->dbg_ctx), "Exhausted retries waiting for "
387 "FCF selection.\n");
388 return;
389 }
390
391 rval = qedf_send_flogi(qedf);
392 if (rval)
393 return;
394
395 /* Wait for FLOGI completion before proceeding with sending ADISCs */
396 i = wait_for_completion_timeout(&qedf->flogi_compl,
397 qedf->lport->r_a_tov);
398 if (i == 0) {
399 QEDF_ERR(&(qedf->dbg_ctx), "FLOGI timed out.\n");
400 return;
401 }
402
403 /*
404 * Call lport->tt.rport_login which will cause libfc to send an
405 * ADISC since the rport is in state ready.
406 */
407 rcu_read_lock();
408 list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
409 rdata = fcport->rdata;
410 if (rdata == NULL)
411 continue;
412 rdata_item = kzalloc(sizeof(struct qedf_tmp_rdata_item),
413 GFP_ATOMIC);
414 if (!rdata_item)
415 continue;
416 if (kref_get_unless_zero(&rdata->kref)) {
417 rdata_item->rdata = rdata;
418 list_add(&rdata_item->list, &rdata_login_list);
419 } else
420 kfree(rdata_item);
421 }
422 rcu_read_unlock();
423 /*
424 * Do the fc_rport_login outside of the rcu lock so we don't take a
425 * mutex in an atomic context.
426 */
427 list_for_each_entry_safe(rdata_item, tmp_rdata_item, &rdata_login_list,
428 list) {
429 list_del(&rdata_item->list);
430 fc_rport_login(rdata_item->rdata);
431 kref_put(&rdata_item->rdata->kref, fc_rport_destroy);
432 kfree(rdata_item);
433 }
434}
435
436static void qedf_update_link_speed(struct qedf_ctx *qedf,
437 struct qed_link_output *link)
438{
439 struct fc_lport *lport = qedf->lport;
440
441 lport->link_speed = FC_PORTSPEED_UNKNOWN;
442 lport->link_supported_speeds = FC_PORTSPEED_UNKNOWN;
443
444 /* Set fc_host link speed */
445 switch (link->speed) {
446 case 10000:
447 lport->link_speed = FC_PORTSPEED_10GBIT;
448 break;
449 case 25000:
450 lport->link_speed = FC_PORTSPEED_25GBIT;
451 break;
452 case 40000:
453 lport->link_speed = FC_PORTSPEED_40GBIT;
454 break;
455 case 50000:
456 lport->link_speed = FC_PORTSPEED_50GBIT;
457 break;
458 case 100000:
459 lport->link_speed = FC_PORTSPEED_100GBIT;
460 break;
461 default:
462 lport->link_speed = FC_PORTSPEED_UNKNOWN;
463 break;
464 }
465
466 /*
467 * Set supported link speed by querying the supported
468 * capabilities of the link.
469 */
470 if (link->supported_caps & SUPPORTED_10000baseKR_Full)
471 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
472 if (link->supported_caps & SUPPORTED_25000baseKR_Full)
473 lport->link_supported_speeds |= FC_PORTSPEED_25GBIT;
474 if (link->supported_caps & SUPPORTED_40000baseLR4_Full)
475 lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
476 if (link->supported_caps & SUPPORTED_50000baseKR2_Full)
477 lport->link_supported_speeds |= FC_PORTSPEED_50GBIT;
478 if (link->supported_caps & SUPPORTED_100000baseKR4_Full)
479 lport->link_supported_speeds |= FC_PORTSPEED_100GBIT;
480 fc_host_supported_speeds(lport->host) = lport->link_supported_speeds;
481}
482
483static void qedf_link_update(void *dev, struct qed_link_output *link)
484{
485 struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
486
487 if (link->link_up) {
Chad Dupuis3f9de7f2018-04-25 06:08:55 -0700488 if (atomic_read(&qedf->link_state) == QEDF_LINK_UP) {
489 QEDF_INFO((&qedf->dbg_ctx), QEDF_LOG_DISC,
490 "Ignoring link up event as link is already up.\n");
491 return;
492 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800493 QEDF_ERR(&(qedf->dbg_ctx), "LINK UP (%d GB/s).\n",
494 link->speed / 1000);
495
496 /* Cancel any pending link down work */
497 cancel_delayed_work(&qedf->link_update);
498
499 atomic_set(&qedf->link_state, QEDF_LINK_UP);
500 qedf_update_link_speed(qedf, link);
501
Chad Dupuisba17d372018-04-25 06:08:51 -0700502 if (atomic_read(&qedf->dcbx) == QEDF_DCBX_DONE ||
503 qedf_dcbx_no_wait) {
Chad Dupuisf7e8d572017-05-31 06:33:59 -0700504 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
505 "DCBx done.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -0800506 if (atomic_read(&qedf->link_down_tmo_valid) > 0)
507 queue_delayed_work(qedf->link_update_wq,
508 &qedf->link_recovery, 0);
509 else
510 queue_delayed_work(qedf->link_update_wq,
511 &qedf->link_update, 0);
512 atomic_set(&qedf->link_down_tmo_valid, 0);
513 }
514
515 } else {
516 QEDF_ERR(&(qedf->dbg_ctx), "LINK DOWN.\n");
517
518 atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
519 atomic_set(&qedf->dcbx, QEDF_DCBX_PENDING);
520 /*
521 * Flag that we're waiting for the link to come back up before
522 * informing the fcoe layer of the event.
523 */
524 if (qedf_link_down_tmo > 0) {
525 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
526 "Starting link down tmo.\n");
527 atomic_set(&qedf->link_down_tmo_valid, 1);
528 }
Chad Dupuis84b2ba62018-04-25 06:08:52 -0700529 qedf->vlan_id = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800530 qedf_update_link_speed(qedf, link);
531 queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
532 qedf_link_down_tmo * HZ);
533 }
534}
535
536
537static void qedf_dcbx_handler(void *dev, struct qed_dcbx_get *get, u32 mib_type)
538{
539 struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
Chad Dupuis84b2ba62018-04-25 06:08:52 -0700540 u8 tmp_prio;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800541
542 QEDF_ERR(&(qedf->dbg_ctx), "DCBx event valid=%d enabled=%d fcoe "
543 "prio=%d.\n", get->operational.valid, get->operational.enabled,
544 get->operational.app_prio.fcoe);
545
546 if (get->operational.enabled && get->operational.valid) {
547 /* If DCBX was already negotiated on link up then just exit */
548 if (atomic_read(&qedf->dcbx) == QEDF_DCBX_DONE) {
549 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
550 "DCBX already set on link up.\n");
551 return;
552 }
553
554 atomic_set(&qedf->dcbx, QEDF_DCBX_DONE);
555
Chad Dupuis84b2ba62018-04-25 06:08:52 -0700556 /*
557 * Set the 8021q priority in the following manner:
558 *
559 * 1. If a modparam is set use that
560 * 2. If the value is not between 0..7 use the default
561 * 3. Use the priority we get from the DCBX app tag
562 */
563 tmp_prio = get->operational.app_prio.fcoe;
564 if (qedf_default_prio > -1)
565 qedf->prio = qedf_default_prio;
566 else if (tmp_prio < 0 || tmp_prio > 7) {
567 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
568 "FIP/FCoE prio %d out of range, setting to %d.\n",
569 tmp_prio, QEDF_DEFAULT_PRIO);
570 qedf->prio = QEDF_DEFAULT_PRIO;
571 } else
572 qedf->prio = tmp_prio;
573
Chad Dupuisba17d372018-04-25 06:08:51 -0700574 if (atomic_read(&qedf->link_state) == QEDF_LINK_UP &&
575 !qedf_dcbx_no_wait) {
Dupuis, Chad61d86582017-02-15 06:28:23 -0800576 if (atomic_read(&qedf->link_down_tmo_valid) > 0)
577 queue_delayed_work(qedf->link_update_wq,
578 &qedf->link_recovery, 0);
579 else
580 queue_delayed_work(qedf->link_update_wq,
581 &qedf->link_update, 0);
582 atomic_set(&qedf->link_down_tmo_valid, 0);
583 }
584 }
585
586}
587
588static u32 qedf_get_login_failures(void *cookie)
589{
590 struct qedf_ctx *qedf;
591
592 qedf = (struct qedf_ctx *)cookie;
593 return qedf->flogi_failed;
594}
595
596static struct qed_fcoe_cb_ops qedf_cb_ops = {
597 {
598 .link_update = qedf_link_update,
599 .dcbx_aen = qedf_dcbx_handler,
600 }
601};
602
603/*
604 * Various transport templates.
605 */
606
607static struct scsi_transport_template *qedf_fc_transport_template;
608static struct scsi_transport_template *qedf_fc_vport_transport_template;
609
610/*
611 * SCSI EH handlers
612 */
613static int qedf_eh_abort(struct scsi_cmnd *sc_cmd)
614{
615 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
616 struct fc_rport_libfc_priv *rp = rport->dd_data;
617 struct qedf_rport *fcport;
618 struct fc_lport *lport;
619 struct qedf_ctx *qedf;
620 struct qedf_ioreq *io_req;
621 int rc = FAILED;
622 int rval;
623
624 if (fc_remote_port_chkready(rport)) {
625 QEDF_ERR(NULL, "rport not ready\n");
626 goto out;
627 }
628
629 lport = shost_priv(sc_cmd->device->host);
630 qedf = (struct qedf_ctx *)lport_priv(lport);
631
632 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
633 QEDF_ERR(&(qedf->dbg_ctx), "link not ready.\n");
634 goto out;
635 }
636
637 fcport = (struct qedf_rport *)&rp[1];
638
639 io_req = (struct qedf_ioreq *)sc_cmd->SCp.ptr;
640 if (!io_req) {
641 QEDF_ERR(&(qedf->dbg_ctx), "io_req is NULL.\n");
642 rc = SUCCESS;
643 goto out;
644 }
645
646 if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
647 test_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags) ||
648 test_bit(QEDF_CMD_IN_ABORT, &io_req->flags)) {
649 QEDF_ERR(&(qedf->dbg_ctx), "io_req xid=0x%x already in "
650 "cleanup or abort processing or already "
651 "completed.\n", io_req->xid);
652 rc = SUCCESS;
653 goto out;
654 }
655
656 QEDF_ERR(&(qedf->dbg_ctx), "Aborting io_req sc_cmd=%p xid=0x%x "
657 "fp_idx=%d.\n", sc_cmd, io_req->xid, io_req->fp_idx);
658
659 if (qedf->stop_io_on_error) {
660 qedf_stop_all_io(qedf);
661 rc = SUCCESS;
662 goto out;
663 }
664
665 init_completion(&io_req->abts_done);
666 rval = qedf_initiate_abts(io_req, true);
667 if (rval) {
668 QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
669 goto out;
670 }
671
672 wait_for_completion(&io_req->abts_done);
673
674 if (io_req->event == QEDF_IOREQ_EV_ABORT_SUCCESS ||
675 io_req->event == QEDF_IOREQ_EV_ABORT_FAILED ||
676 io_req->event == QEDF_IOREQ_EV_CLEANUP_SUCCESS) {
677 /*
678 * If we get a reponse to the abort this is success from
679 * the perspective that all references to the command have
680 * been removed from the driver and firmware
681 */
682 rc = SUCCESS;
683 } else {
684 /* If the abort and cleanup failed then return a failure */
685 rc = FAILED;
686 }
687
688 if (rc == SUCCESS)
689 QEDF_ERR(&(qedf->dbg_ctx), "ABTS succeeded, xid=0x%x.\n",
690 io_req->xid);
691 else
692 QEDF_ERR(&(qedf->dbg_ctx), "ABTS failed, xid=0x%x.\n",
693 io_req->xid);
694
695out:
696 return rc;
697}
698
699static int qedf_eh_target_reset(struct scsi_cmnd *sc_cmd)
700{
701 QEDF_ERR(NULL, "TARGET RESET Issued...");
702 return qedf_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
703}
704
705static int qedf_eh_device_reset(struct scsi_cmnd *sc_cmd)
706{
707 QEDF_ERR(NULL, "LUN RESET Issued...\n");
708 return qedf_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
709}
710
711void qedf_wait_for_upload(struct qedf_ctx *qedf)
712{
713 while (1) {
714 if (atomic_read(&qedf->num_offloads))
715 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
716 "Waiting for all uploads to complete.\n");
717 else
718 break;
719 msleep(500);
720 }
721}
722
Chad Dupuis5cf446d2017-05-31 06:33:55 -0700723/* Performs soft reset of qedf_ctx by simulating a link down/up */
724static void qedf_ctx_soft_reset(struct fc_lport *lport)
Dupuis, Chad61d86582017-02-15 06:28:23 -0800725{
Dupuis, Chad61d86582017-02-15 06:28:23 -0800726 struct qedf_ctx *qedf;
727
Dupuis, Chad61d86582017-02-15 06:28:23 -0800728 if (lport->vport) {
729 QEDF_ERR(NULL, "Cannot issue host reset on NPIV port.\n");
Chad Dupuis5cf446d2017-05-31 06:33:55 -0700730 return;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800731 }
732
Chad Dupuis5cf446d2017-05-31 06:33:55 -0700733 qedf = lport_priv(lport);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800734
735 /* For host reset, essentially do a soft link up/down */
736 atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800737 queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
738 0);
739 qedf_wait_for_upload(qedf);
740 atomic_set(&qedf->link_state, QEDF_LINK_UP);
741 qedf->vlan_id = 0;
742 queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
743 0);
Chad Dupuis5cf446d2017-05-31 06:33:55 -0700744}
745
746/* Reset the host by gracefully logging out and then logging back in */
747static int qedf_eh_host_reset(struct scsi_cmnd *sc_cmd)
748{
749 struct fc_lport *lport;
750 struct qedf_ctx *qedf;
751
752 lport = shost_priv(sc_cmd->device->host);
753 qedf = lport_priv(lport);
754
755 if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN ||
756 test_bit(QEDF_UNLOADING, &qedf->flags))
757 return FAILED;
758
759 QEDF_ERR(&(qedf->dbg_ctx), "HOST RESET Issued...");
760
761 qedf_ctx_soft_reset(lport);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800762
763 return SUCCESS;
764}
765
766static int qedf_slave_configure(struct scsi_device *sdev)
767{
768 if (qedf_queue_depth) {
769 scsi_change_queue_depth(sdev, qedf_queue_depth);
770 }
771
772 return 0;
773}
774
775static struct scsi_host_template qedf_host_template = {
776 .module = THIS_MODULE,
777 .name = QEDF_MODULE_NAME,
778 .this_id = -1,
Chad Dupuisa7746f12017-05-31 06:34:00 -0700779 .cmd_per_lun = 32,
Dupuis, Chad61d86582017-02-15 06:28:23 -0800780 .use_clustering = ENABLE_CLUSTERING,
781 .max_sectors = 0xffff,
782 .queuecommand = qedf_queuecommand,
783 .shost_attrs = qedf_host_attrs,
784 .eh_abort_handler = qedf_eh_abort,
785 .eh_device_reset_handler = qedf_eh_device_reset, /* lun reset */
786 .eh_target_reset_handler = qedf_eh_target_reset, /* target reset */
787 .eh_host_reset_handler = qedf_eh_host_reset,
788 .slave_configure = qedf_slave_configure,
789 .dma_boundary = QED_HW_DMA_BOUNDARY,
790 .sg_tablesize = QEDF_MAX_BDS_PER_CMD,
791 .can_queue = FCOE_PARAMS_NUM_TASKS,
Chad Dupuis6088cfa2017-05-31 06:34:01 -0700792 .change_queue_depth = scsi_change_queue_depth,
Dupuis, Chad61d86582017-02-15 06:28:23 -0800793};
794
795static int qedf_get_paged_crc_eof(struct sk_buff *skb, int tlen)
796{
797 int rc;
798
799 spin_lock(&qedf_global_lock);
800 rc = fcoe_get_paged_crc_eof(skb, tlen, &qedf_global);
801 spin_unlock(&qedf_global_lock);
802
803 return rc;
804}
805
806static struct qedf_rport *qedf_fcport_lookup(struct qedf_ctx *qedf, u32 port_id)
807{
808 struct qedf_rport *fcport;
809 struct fc_rport_priv *rdata;
810
811 rcu_read_lock();
812 list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
813 rdata = fcport->rdata;
814 if (rdata == NULL)
815 continue;
816 if (rdata->ids.port_id == port_id) {
817 rcu_read_unlock();
818 return fcport;
819 }
820 }
821 rcu_read_unlock();
822
823 /* Return NULL to caller to let them know fcport was not found */
824 return NULL;
825}
826
827/* Transmits an ELS frame over an offloaded session */
828static int qedf_xmit_l2_frame(struct qedf_rport *fcport, struct fc_frame *fp)
829{
830 struct fc_frame_header *fh;
831 int rc = 0;
832
833 fh = fc_frame_header_get(fp);
834 if ((fh->fh_type == FC_TYPE_ELS) &&
835 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
836 switch (fc_frame_payload_op(fp)) {
837 case ELS_ADISC:
838 qedf_send_adisc(fcport, fp);
839 rc = 1;
840 break;
841 }
842 }
843
844 return rc;
845}
846
847/**
848 * qedf_xmit - qedf FCoE frame transmit function
849 *
850 */
851static int qedf_xmit(struct fc_lport *lport, struct fc_frame *fp)
852{
853 struct fc_lport *base_lport;
854 struct qedf_ctx *qedf;
855 struct ethhdr *eh;
856 struct fcoe_crc_eof *cp;
857 struct sk_buff *skb;
858 struct fc_frame_header *fh;
859 struct fcoe_hdr *hp;
860 u8 sof, eof;
861 u32 crc;
862 unsigned int hlen, tlen, elen;
863 int wlen;
864 struct fc_stats *stats;
865 struct fc_lport *tmp_lport;
866 struct fc_lport *vn_port = NULL;
867 struct qedf_rport *fcport;
868 int rc;
869 u16 vlan_tci = 0;
870
871 qedf = (struct qedf_ctx *)lport_priv(lport);
872
873 fh = fc_frame_header_get(fp);
874 skb = fp_skb(fp);
875
876 /* Filter out traffic to other NPIV ports on the same host */
877 if (lport->vport)
878 base_lport = shost_priv(vport_to_shost(lport->vport));
879 else
880 base_lport = lport;
881
882 /* Flag if the destination is the base port */
883 if (base_lport->port_id == ntoh24(fh->fh_d_id)) {
884 vn_port = base_lport;
885 } else {
886 /* Got through the list of vports attached to the base_lport
887 * and see if we have a match with the destination address.
888 */
889 list_for_each_entry(tmp_lport, &base_lport->vports, list) {
890 if (tmp_lport->port_id == ntoh24(fh->fh_d_id)) {
891 vn_port = tmp_lport;
892 break;
893 }
894 }
895 }
896 if (vn_port && ntoh24(fh->fh_d_id) != FC_FID_FLOGI) {
897 struct fc_rport_priv *rdata = NULL;
898
899 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
900 "Dropping FCoE frame to %06x.\n", ntoh24(fh->fh_d_id));
901 kfree_skb(skb);
902 rdata = fc_rport_lookup(lport, ntoh24(fh->fh_d_id));
903 if (rdata)
904 rdata->retries = lport->max_rport_retry_count;
905 return -EINVAL;
906 }
907 /* End NPIV filtering */
908
909 if (!qedf->ctlr.sel_fcf) {
910 kfree_skb(skb);
911 return 0;
912 }
913
914 if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
915 QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
916 kfree_skb(skb);
917 return 0;
918 }
919
920 if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
921 QEDF_WARN(&(qedf->dbg_ctx), "qedf link down\n");
922 kfree_skb(skb);
923 return 0;
924 }
925
926 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
927 if (fcoe_ctlr_els_send(&qedf->ctlr, lport, skb))
928 return 0;
929 }
930
931 /* Check to see if this needs to be sent on an offloaded session */
932 fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
933
934 if (fcport && test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
935 rc = qedf_xmit_l2_frame(fcport, fp);
936 /*
937 * If the frame was successfully sent over the middle path
938 * then do not try to also send it over the LL2 path
939 */
940 if (rc)
941 return 0;
942 }
943
944 sof = fr_sof(fp);
945 eof = fr_eof(fp);
946
947 elen = sizeof(struct ethhdr);
948 hlen = sizeof(struct fcoe_hdr);
949 tlen = sizeof(struct fcoe_crc_eof);
950 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
951
952 skb->ip_summed = CHECKSUM_NONE;
953 crc = fcoe_fc_crc(fp);
954
955 /* copy port crc and eof to the skb buff */
956 if (skb_is_nonlinear(skb)) {
957 skb_frag_t *frag;
958
959 if (qedf_get_paged_crc_eof(skb, tlen)) {
960 kfree_skb(skb);
961 return -ENOMEM;
962 }
963 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
964 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
965 } else {
Johannes Berg4df864c2017-06-16 14:29:21 +0200966 cp = skb_put(skb, tlen);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800967 }
968
969 memset(cp, 0, sizeof(*cp));
970 cp->fcoe_eof = eof;
971 cp->fcoe_crc32 = cpu_to_le32(~crc);
972 if (skb_is_nonlinear(skb)) {
973 kunmap_atomic(cp);
974 cp = NULL;
975 }
976
977
978 /* adjust skb network/transport offsets to match mac/fcoe/port */
979 skb_push(skb, elen + hlen);
980 skb_reset_mac_header(skb);
981 skb_reset_network_header(skb);
982 skb->mac_len = elen;
983 skb->protocol = htons(ETH_P_FCOE);
984
Chad Dupuiscf291162017-08-15 10:08:18 -0700985 /*
986 * Add VLAN tag to non-offload FCoE frame based on current stored VLAN
987 * for FIP/FCoE traffic.
988 */
Dupuis, Chad61d86582017-02-15 06:28:23 -0800989 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
990
991 /* fill up mac and fcoe headers */
992 eh = eth_hdr(skb);
993 eh->h_proto = htons(ETH_P_FCOE);
994 if (qedf->ctlr.map_dest)
995 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
996 else
997 /* insert GW address */
998 ether_addr_copy(eh->h_dest, qedf->ctlr.dest_addr);
999
1000 /* Set the source MAC address */
Chad Dupuisa3cd42a92017-08-15 10:08:17 -07001001 ether_addr_copy(eh->h_source, qedf->data_src_addr);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001002
1003 hp = (struct fcoe_hdr *)(eh + 1);
1004 memset(hp, 0, sizeof(*hp));
1005 if (FC_FCOE_VER)
1006 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1007 hp->fcoe_sof = sof;
1008
1009 /*update tx stats */
1010 stats = per_cpu_ptr(lport->stats, get_cpu());
1011 stats->TxFrames++;
1012 stats->TxWords += wlen;
1013 put_cpu();
1014
1015 /* Get VLAN ID from skb for printing purposes */
1016 __vlan_hwaccel_get_tag(skb, &vlan_tci);
1017
1018 /* send down to lld */
1019 fr_dev(fp) = lport;
1020 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame send: "
1021 "src=%06x dest=%06x r_ctl=%x type=%x vlan=%04x.\n",
1022 ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl, fh->fh_type,
1023 vlan_tci);
1024 if (qedf_dump_frames)
1025 print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
1026 1, skb->data, skb->len, false);
1027 qed_ops->ll2->start_xmit(qedf->cdev, skb);
1028
1029 return 0;
1030}
1031
1032static int qedf_alloc_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
1033{
1034 int rval = 0;
1035 u32 *pbl;
1036 dma_addr_t page;
1037 int num_pages;
1038
1039 /* Calculate appropriate queue and PBL sizes */
1040 fcport->sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
1041 fcport->sq_mem_size = ALIGN(fcport->sq_mem_size, QEDF_PAGE_SIZE);
1042 fcport->sq_pbl_size = (fcport->sq_mem_size / QEDF_PAGE_SIZE) *
1043 sizeof(void *);
1044 fcport->sq_pbl_size = fcport->sq_pbl_size + QEDF_PAGE_SIZE;
1045
Christophe JAILLETc4d6ffc2017-06-11 08:16:03 +02001046 fcport->sq = dma_zalloc_coherent(&qedf->pdev->dev,
1047 fcport->sq_mem_size, &fcport->sq_dma, GFP_KERNEL);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001048 if (!fcport->sq) {
Christophe JAILLET32eebb32017-06-11 08:16:04 +02001049 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08001050 rval = 1;
1051 goto out;
1052 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001053
Christophe JAILLETc4d6ffc2017-06-11 08:16:03 +02001054 fcport->sq_pbl = dma_zalloc_coherent(&qedf->pdev->dev,
Dupuis, Chad61d86582017-02-15 06:28:23 -08001055 fcport->sq_pbl_size, &fcport->sq_pbl_dma, GFP_KERNEL);
1056 if (!fcport->sq_pbl) {
Christophe JAILLET32eebb32017-06-11 08:16:04 +02001057 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue PBL.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08001058 rval = 1;
1059 goto out_free_sq;
1060 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001061
1062 /* Create PBL */
1063 num_pages = fcport->sq_mem_size / QEDF_PAGE_SIZE;
1064 page = fcport->sq_dma;
1065 pbl = (u32 *)fcport->sq_pbl;
1066
1067 while (num_pages--) {
1068 *pbl = U64_LO(page);
1069 pbl++;
1070 *pbl = U64_HI(page);
1071 pbl++;
1072 page += QEDF_PAGE_SIZE;
1073 }
1074
1075 return rval;
1076
1077out_free_sq:
1078 dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size, fcport->sq,
1079 fcport->sq_dma);
1080out:
1081 return rval;
1082}
1083
1084static void qedf_free_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
1085{
1086 if (fcport->sq_pbl)
1087 dma_free_coherent(&qedf->pdev->dev, fcport->sq_pbl_size,
1088 fcport->sq_pbl, fcport->sq_pbl_dma);
1089 if (fcport->sq)
1090 dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size,
1091 fcport->sq, fcport->sq_dma);
1092}
1093
1094static int qedf_offload_connection(struct qedf_ctx *qedf,
1095 struct qedf_rport *fcport)
1096{
1097 struct qed_fcoe_params_offload conn_info;
1098 u32 port_id;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001099 int rval;
1100 uint16_t total_sqe = (fcport->sq_mem_size / sizeof(struct fcoe_wqe));
1101
1102 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offloading connection "
1103 "portid=%06x.\n", fcport->rdata->ids.port_id);
1104 rval = qed_ops->acquire_conn(qedf->cdev, &fcport->handle,
1105 &fcport->fw_cid, &fcport->p_doorbell);
1106 if (rval) {
1107 QEDF_WARN(&(qedf->dbg_ctx), "Could not acquire connection "
1108 "for portid=%06x.\n", fcport->rdata->ids.port_id);
1109 rval = 1; /* For some reason qed returns 0 on failure here */
1110 goto out;
1111 }
1112
1113 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "portid=%06x "
1114 "fw_cid=%08x handle=%d.\n", fcport->rdata->ids.port_id,
1115 fcport->fw_cid, fcport->handle);
1116
1117 memset(&conn_info, 0, sizeof(struct qed_fcoe_params_offload));
1118
1119 /* Fill in the offload connection info */
1120 conn_info.sq_pbl_addr = fcport->sq_pbl_dma;
1121
1122 conn_info.sq_curr_page_addr = (dma_addr_t)(*(u64 *)fcport->sq_pbl);
1123 conn_info.sq_next_page_addr =
1124 (dma_addr_t)(*(u64 *)(fcport->sq_pbl + 8));
1125
1126 /* Need to use our FCoE MAC for the offload session */
Chad Dupuisa3cd42a92017-08-15 10:08:17 -07001127 ether_addr_copy(conn_info.src_mac, qedf->data_src_addr);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001128
1129 ether_addr_copy(conn_info.dst_mac, qedf->ctlr.dest_addr);
1130
1131 conn_info.tx_max_fc_pay_len = fcport->rdata->maxframe_size;
1132 conn_info.e_d_tov_timer_val = qedf->lport->e_d_tov / 20;
1133 conn_info.rec_tov_timer_val = 3; /* I think this is what E3 was */
1134 conn_info.rx_max_fc_pay_len = fcport->rdata->maxframe_size;
1135
1136 /* Set VLAN data */
1137 conn_info.vlan_tag = qedf->vlan_id <<
1138 FCOE_CONN_OFFLOAD_RAMROD_DATA_VLAN_ID_SHIFT;
1139 conn_info.vlan_tag |=
Chad Dupuis84b2ba62018-04-25 06:08:52 -07001140 qedf->prio << FCOE_CONN_OFFLOAD_RAMROD_DATA_PRIORITY_SHIFT;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001141 conn_info.flags |= (FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_MASK <<
1142 FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_SHIFT);
1143
1144 /* Set host port source id */
1145 port_id = fc_host_port_id(qedf->lport->host);
1146 fcport->sid = port_id;
1147 conn_info.s_id.addr_hi = (port_id & 0x000000FF);
1148 conn_info.s_id.addr_mid = (port_id & 0x0000FF00) >> 8;
1149 conn_info.s_id.addr_lo = (port_id & 0x00FF0000) >> 16;
1150
1151 conn_info.max_conc_seqs_c3 = fcport->rdata->max_seq;
1152
1153 /* Set remote port destination id */
1154 port_id = fcport->rdata->rport->port_id;
1155 conn_info.d_id.addr_hi = (port_id & 0x000000FF);
1156 conn_info.d_id.addr_mid = (port_id & 0x0000FF00) >> 8;
1157 conn_info.d_id.addr_lo = (port_id & 0x00FF0000) >> 16;
1158
1159 conn_info.def_q_idx = 0; /* Default index for send queue? */
1160
1161 /* Set FC-TAPE specific flags if needed */
1162 if (fcport->dev_type == QEDF_RPORT_TYPE_TAPE) {
1163 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN,
1164 "Enable CONF, REC for portid=%06x.\n",
1165 fcport->rdata->ids.port_id);
1166 conn_info.flags |= 1 <<
1167 FCOE_CONN_OFFLOAD_RAMROD_DATA_B_CONF_REQ_SHIFT;
1168 conn_info.flags |=
1169 ((fcport->rdata->sp_features & FC_SP_FT_SEQC) ? 1 : 0) <<
1170 FCOE_CONN_OFFLOAD_RAMROD_DATA_B_REC_VALID_SHIFT;
1171 }
1172
1173 rval = qed_ops->offload_conn(qedf->cdev, fcport->handle, &conn_info);
1174 if (rval) {
1175 QEDF_WARN(&(qedf->dbg_ctx), "Could not offload connection "
1176 "for portid=%06x.\n", fcport->rdata->ids.port_id);
1177 goto out_free_conn;
1178 } else
1179 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offload "
1180 "succeeded portid=%06x total_sqe=%d.\n",
1181 fcport->rdata->ids.port_id, total_sqe);
1182
1183 spin_lock_init(&fcport->rport_lock);
1184 atomic_set(&fcport->free_sqes, total_sqe);
1185 return 0;
1186out_free_conn:
1187 qed_ops->release_conn(qedf->cdev, fcport->handle);
1188out:
1189 return rval;
1190}
1191
1192#define QEDF_TERM_BUFF_SIZE 10
1193static void qedf_upload_connection(struct qedf_ctx *qedf,
1194 struct qedf_rport *fcport)
1195{
1196 void *term_params;
1197 dma_addr_t term_params_dma;
1198
1199 /* Term params needs to be a DMA coherent buffer as qed shared the
1200 * physical DMA address with the firmware. The buffer may be used in
1201 * the receive path so we may eventually have to move this.
1202 */
1203 term_params = dma_alloc_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE,
1204 &term_params_dma, GFP_KERNEL);
1205
1206 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Uploading connection "
1207 "port_id=%06x.\n", fcport->rdata->ids.port_id);
1208
1209 qed_ops->destroy_conn(qedf->cdev, fcport->handle, term_params_dma);
1210 qed_ops->release_conn(qedf->cdev, fcport->handle);
1211
1212 dma_free_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE, term_params,
1213 term_params_dma);
1214}
1215
1216static void qedf_cleanup_fcport(struct qedf_ctx *qedf,
1217 struct qedf_rport *fcport)
1218{
1219 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Cleaning up portid=%06x.\n",
1220 fcport->rdata->ids.port_id);
1221
1222 /* Flush any remaining i/o's before we upload the connection */
1223 qedf_flush_active_ios(fcport, -1);
1224
1225 if (test_and_clear_bit(QEDF_RPORT_SESSION_READY, &fcport->flags))
1226 qedf_upload_connection(qedf, fcport);
1227 qedf_free_sq(qedf, fcport);
1228 fcport->rdata = NULL;
1229 fcport->qedf = NULL;
1230}
1231
1232/**
1233 * This event_callback is called after successful completion of libfc
1234 * initiated target login. qedf can proceed with initiating the session
1235 * establishment.
1236 */
1237static void qedf_rport_event_handler(struct fc_lport *lport,
1238 struct fc_rport_priv *rdata,
1239 enum fc_rport_event event)
1240{
1241 struct qedf_ctx *qedf = lport_priv(lport);
1242 struct fc_rport *rport = rdata->rport;
1243 struct fc_rport_libfc_priv *rp;
1244 struct qedf_rport *fcport;
1245 u32 port_id;
1246 int rval;
1247 unsigned long flags;
1248
1249 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "event = %d, "
1250 "port_id = 0x%x\n", event, rdata->ids.port_id);
1251
1252 switch (event) {
1253 case RPORT_EV_READY:
1254 if (!rport) {
1255 QEDF_WARN(&(qedf->dbg_ctx), "rport is NULL.\n");
1256 break;
1257 }
1258
1259 rp = rport->dd_data;
1260 fcport = (struct qedf_rport *)&rp[1];
1261 fcport->qedf = qedf;
1262
1263 if (atomic_read(&qedf->num_offloads) >= QEDF_MAX_SESSIONS) {
1264 QEDF_ERR(&(qedf->dbg_ctx), "Not offloading "
1265 "portid=0x%x as max number of offloaded sessions "
1266 "reached.\n", rdata->ids.port_id);
1267 return;
1268 }
1269
1270 /*
1271 * Don't try to offload the session again. Can happen when we
1272 * get an ADISC
1273 */
1274 if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1275 QEDF_WARN(&(qedf->dbg_ctx), "Session already "
1276 "offloaded, portid=0x%x.\n",
1277 rdata->ids.port_id);
1278 return;
1279 }
1280
1281 if (rport->port_id == FC_FID_DIR_SERV) {
1282 /*
1283 * qedf_rport structure doesn't exist for
1284 * directory server.
1285 * We should not come here, as lport will
1286 * take care of fabric login
1287 */
1288 QEDF_WARN(&(qedf->dbg_ctx), "rport struct does not "
1289 "exist for dir server port_id=%x\n",
1290 rdata->ids.port_id);
1291 break;
1292 }
1293
1294 if (rdata->spp_type != FC_TYPE_FCP) {
1295 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
Colin Ian Kingacef2692017-07-03 20:21:39 +01001296 "Not offloading since spp type isn't FCP\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08001297 break;
1298 }
1299 if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
1300 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1301 "Not FCP target so not offloading\n");
1302 break;
1303 }
1304
1305 fcport->rdata = rdata;
1306 fcport->rport = rport;
1307
1308 rval = qedf_alloc_sq(qedf, fcport);
1309 if (rval) {
1310 qedf_cleanup_fcport(qedf, fcport);
1311 break;
1312 }
1313
1314 /* Set device type */
1315 if (rdata->flags & FC_RP_FLAGS_RETRY &&
1316 rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET &&
1317 !(rdata->ids.roles & FC_RPORT_ROLE_FCP_INITIATOR)) {
1318 fcport->dev_type = QEDF_RPORT_TYPE_TAPE;
1319 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1320 "portid=%06x is a TAPE device.\n",
1321 rdata->ids.port_id);
1322 } else {
1323 fcport->dev_type = QEDF_RPORT_TYPE_DISK;
1324 }
1325
1326 rval = qedf_offload_connection(qedf, fcport);
1327 if (rval) {
1328 qedf_cleanup_fcport(qedf, fcport);
1329 break;
1330 }
1331
1332 /* Add fcport to list of qedf_ctx list of offloaded ports */
1333 spin_lock_irqsave(&qedf->hba_lock, flags);
1334 list_add_rcu(&fcport->peers, &qedf->fcports);
1335 spin_unlock_irqrestore(&qedf->hba_lock, flags);
1336
1337 /*
1338 * Set the session ready bit to let everyone know that this
1339 * connection is ready for I/O
1340 */
1341 set_bit(QEDF_RPORT_SESSION_READY, &fcport->flags);
1342 atomic_inc(&qedf->num_offloads);
1343
1344 break;
1345 case RPORT_EV_LOGO:
1346 case RPORT_EV_FAILED:
1347 case RPORT_EV_STOP:
1348 port_id = rdata->ids.port_id;
1349 if (port_id == FC_FID_DIR_SERV)
1350 break;
1351
1352 if (!rport) {
1353 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1354 "port_id=%x - rport notcreated Yet!!\n", port_id);
1355 break;
1356 }
1357 rp = rport->dd_data;
1358 /*
1359 * Perform session upload. Note that rdata->peers is already
1360 * removed from disc->rports list before we get this event.
1361 */
1362 fcport = (struct qedf_rport *)&rp[1];
1363
1364 /* Only free this fcport if it is offloaded already */
1365 if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1366 set_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags);
1367 qedf_cleanup_fcport(qedf, fcport);
1368
1369 /*
1370 * Remove fcport to list of qedf_ctx list of offloaded
1371 * ports
1372 */
1373 spin_lock_irqsave(&qedf->hba_lock, flags);
1374 list_del_rcu(&fcport->peers);
1375 spin_unlock_irqrestore(&qedf->hba_lock, flags);
1376
1377 clear_bit(QEDF_RPORT_UPLOADING_CONNECTION,
1378 &fcport->flags);
1379 atomic_dec(&qedf->num_offloads);
1380 }
1381
1382 break;
1383
1384 case RPORT_EV_NONE:
1385 break;
1386 }
1387}
1388
1389static void qedf_abort_io(struct fc_lport *lport)
1390{
1391 /* NO-OP but need to fill in the template */
1392}
1393
1394static void qedf_fcp_cleanup(struct fc_lport *lport)
1395{
1396 /*
1397 * NO-OP but need to fill in template to prevent a NULL
1398 * function pointer dereference during link down. I/Os
1399 * will be flushed when port is uploaded.
1400 */
1401}
1402
1403static struct libfc_function_template qedf_lport_template = {
1404 .frame_send = qedf_xmit,
1405 .fcp_abort_io = qedf_abort_io,
1406 .fcp_cleanup = qedf_fcp_cleanup,
1407 .rport_event_callback = qedf_rport_event_handler,
1408 .elsct_send = qedf_elsct_send,
1409};
1410
1411static void qedf_fcoe_ctlr_setup(struct qedf_ctx *qedf)
1412{
1413 fcoe_ctlr_init(&qedf->ctlr, FIP_ST_AUTO);
1414
1415 qedf->ctlr.send = qedf_fip_send;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001416 qedf->ctlr.get_src_addr = qedf_get_src_mac;
1417 ether_addr_copy(qedf->ctlr.ctl_src_addr, qedf->mac);
1418}
1419
Chad Dupuis5185b322017-05-31 06:33:48 -07001420static void qedf_setup_fdmi(struct qedf_ctx *qedf)
1421{
1422 struct fc_lport *lport = qedf->lport;
1423 struct fc_host_attrs *fc_host = shost_to_fc_host(lport->host);
1424 u8 buf[8];
1425 int i, pos;
1426
1427 /*
1428 * fdmi_enabled needs to be set for libfc to execute FDMI registration.
1429 */
1430 lport->fdmi_enabled = 1;
1431
1432 /*
1433 * Setup the necessary fc_host attributes to that will be used to fill
1434 * in the FDMI information.
1435 */
1436
1437 /* Get the PCI-e Device Serial Number Capability */
1438 pos = pci_find_ext_capability(qedf->pdev, PCI_EXT_CAP_ID_DSN);
1439 if (pos) {
1440 pos += 4;
1441 for (i = 0; i < 8; i++)
1442 pci_read_config_byte(qedf->pdev, pos + i, &buf[i]);
1443
1444 snprintf(fc_host->serial_number,
1445 sizeof(fc_host->serial_number),
1446 "%02X%02X%02X%02X%02X%02X%02X%02X",
1447 buf[7], buf[6], buf[5], buf[4],
1448 buf[3], buf[2], buf[1], buf[0]);
1449 } else
1450 snprintf(fc_host->serial_number,
1451 sizeof(fc_host->serial_number), "Unknown");
1452
1453 snprintf(fc_host->manufacturer,
1454 sizeof(fc_host->manufacturer), "%s", "Cavium Inc.");
1455
1456 snprintf(fc_host->model, sizeof(fc_host->model), "%s", "QL41000");
1457
1458 snprintf(fc_host->model_description, sizeof(fc_host->model_description),
1459 "%s", "QLogic FastLinQ QL41000 Series 10/25/40/50GGbE Controller"
1460 "(FCoE)");
1461
1462 snprintf(fc_host->hardware_version, sizeof(fc_host->hardware_version),
1463 "Rev %d", qedf->pdev->revision);
1464
1465 snprintf(fc_host->driver_version, sizeof(fc_host->driver_version),
1466 "%s", QEDF_VERSION);
1467
1468 snprintf(fc_host->firmware_version, sizeof(fc_host->firmware_version),
1469 "%d.%d.%d.%d", FW_MAJOR_VERSION, FW_MINOR_VERSION,
1470 FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
1471}
1472
Dupuis, Chad61d86582017-02-15 06:28:23 -08001473static int qedf_lport_setup(struct qedf_ctx *qedf)
1474{
1475 struct fc_lport *lport = qedf->lport;
1476
1477 lport->link_up = 0;
1478 lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
1479 lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
1480 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1481 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
1482 lport->boot_time = jiffies;
1483 lport->e_d_tov = 2 * 1000;
1484 lport->r_a_tov = 10 * 1000;
1485
1486 /* Set NPIV support */
1487 lport->does_npiv = 1;
1488 fc_host_max_npiv_vports(lport->host) = QEDF_MAX_NPIV;
1489
1490 fc_set_wwnn(lport, qedf->wwnn);
1491 fc_set_wwpn(lport, qedf->wwpn);
1492
1493 fcoe_libfc_config(lport, &qedf->ctlr, &qedf_lport_template, 0);
1494
1495 /* Allocate the exchange manager */
1496 fc_exch_mgr_alloc(lport, FC_CLASS_3, qedf->max_scsi_xid + 1,
1497 qedf->max_els_xid, NULL);
1498
1499 if (fc_lport_init_stats(lport))
1500 return -ENOMEM;
1501
1502 /* Finish lport config */
1503 fc_lport_config(lport);
1504
1505 /* Set max frame size */
1506 fc_set_mfs(lport, QEDF_MFS);
1507 fc_host_maxframe_size(lport->host) = lport->mfs;
1508
1509 /* Set default dev_loss_tmo based on module parameter */
1510 fc_host_dev_loss_tmo(lport->host) = qedf_dev_loss_tmo;
1511
1512 /* Set symbolic node name */
1513 snprintf(fc_host_symbolic_name(lport->host), 256,
1514 "QLogic %s v%s", QEDF_MODULE_NAME, QEDF_VERSION);
1515
Chad Dupuis5185b322017-05-31 06:33:48 -07001516 qedf_setup_fdmi(qedf);
1517
Dupuis, Chad61d86582017-02-15 06:28:23 -08001518 return 0;
1519}
1520
1521/*
1522 * NPIV functions
1523 */
1524
1525static int qedf_vport_libfc_config(struct fc_vport *vport,
1526 struct fc_lport *lport)
1527{
1528 lport->link_up = 0;
1529 lport->qfull = 0;
1530 lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
1531 lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
1532 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1533 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
1534 lport->boot_time = jiffies;
1535 lport->e_d_tov = 2 * 1000;
1536 lport->r_a_tov = 10 * 1000;
1537 lport->does_npiv = 1; /* Temporary until we add NPIV support */
1538
1539 /* Allocate stats for vport */
1540 if (fc_lport_init_stats(lport))
1541 return -ENOMEM;
1542
1543 /* Finish lport config */
1544 fc_lport_config(lport);
1545
1546 /* offload related configuration */
1547 lport->crc_offload = 0;
1548 lport->seq_offload = 0;
1549 lport->lro_enabled = 0;
1550 lport->lro_xid = 0;
1551 lport->lso_max = 0;
1552
1553 return 0;
1554}
1555
1556static int qedf_vport_create(struct fc_vport *vport, bool disabled)
1557{
1558 struct Scsi_Host *shost = vport_to_shost(vport);
1559 struct fc_lport *n_port = shost_priv(shost);
1560 struct fc_lport *vn_port;
1561 struct qedf_ctx *base_qedf = lport_priv(n_port);
1562 struct qedf_ctx *vport_qedf;
1563
1564 char buf[32];
1565 int rc = 0;
1566
1567 rc = fcoe_validate_vport_create(vport);
1568 if (rc) {
1569 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1570 QEDF_WARN(&(base_qedf->dbg_ctx), "Failed to create vport, "
1571 "WWPN (0x%s) already exists.\n", buf);
1572 goto err1;
1573 }
1574
1575 if (atomic_read(&base_qedf->link_state) != QEDF_LINK_UP) {
1576 QEDF_WARN(&(base_qedf->dbg_ctx), "Cannot create vport "
1577 "because link is not up.\n");
1578 rc = -EIO;
1579 goto err1;
1580 }
1581
1582 vn_port = libfc_vport_create(vport, sizeof(struct qedf_ctx));
1583 if (!vn_port) {
1584 QEDF_WARN(&(base_qedf->dbg_ctx), "Could not create lport "
1585 "for vport.\n");
1586 rc = -ENOMEM;
1587 goto err1;
1588 }
1589
1590 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1591 QEDF_ERR(&(base_qedf->dbg_ctx), "Creating NPIV port, WWPN=%s.\n",
1592 buf);
1593
1594 /* Copy some fields from base_qedf */
1595 vport_qedf = lport_priv(vn_port);
1596 memcpy(vport_qedf, base_qedf, sizeof(struct qedf_ctx));
1597
1598 /* Set qedf data specific to this vport */
1599 vport_qedf->lport = vn_port;
1600 /* Use same hba_lock as base_qedf */
1601 vport_qedf->hba_lock = base_qedf->hba_lock;
1602 vport_qedf->pdev = base_qedf->pdev;
1603 vport_qedf->cmd_mgr = base_qedf->cmd_mgr;
1604 init_completion(&vport_qedf->flogi_compl);
1605 INIT_LIST_HEAD(&vport_qedf->fcports);
1606
1607 rc = qedf_vport_libfc_config(vport, vn_port);
1608 if (rc) {
1609 QEDF_ERR(&(base_qedf->dbg_ctx), "Could not allocate memory "
1610 "for lport stats.\n");
1611 goto err2;
1612 }
1613
1614 fc_set_wwnn(vn_port, vport->node_name);
1615 fc_set_wwpn(vn_port, vport->port_name);
1616 vport_qedf->wwnn = vn_port->wwnn;
1617 vport_qedf->wwpn = vn_port->wwpn;
1618
1619 vn_port->host->transportt = qedf_fc_vport_transport_template;
1620 vn_port->host->can_queue = QEDF_MAX_ELS_XID;
1621 vn_port->host->max_lun = qedf_max_lun;
1622 vn_port->host->sg_tablesize = QEDF_MAX_BDS_PER_CMD;
1623 vn_port->host->max_cmd_len = QEDF_MAX_CDB_LEN;
1624
1625 rc = scsi_add_host(vn_port->host, &vport->dev);
1626 if (rc) {
1627 QEDF_WARN(&(base_qedf->dbg_ctx), "Error adding Scsi_Host.\n");
1628 goto err2;
1629 }
1630
1631 /* Set default dev_loss_tmo based on module parameter */
1632 fc_host_dev_loss_tmo(vn_port->host) = qedf_dev_loss_tmo;
1633
1634 /* Init libfc stuffs */
1635 memcpy(&vn_port->tt, &qedf_lport_template,
1636 sizeof(qedf_lport_template));
1637 fc_exch_init(vn_port);
1638 fc_elsct_init(vn_port);
1639 fc_lport_init(vn_port);
1640 fc_disc_init(vn_port);
1641 fc_disc_config(vn_port, vn_port);
1642
1643
1644 /* Allocate the exchange manager */
1645 shost = vport_to_shost(vport);
1646 n_port = shost_priv(shost);
1647 fc_exch_mgr_list_clone(n_port, vn_port);
1648
1649 /* Set max frame size */
1650 fc_set_mfs(vn_port, QEDF_MFS);
1651
1652 fc_host_port_type(vn_port->host) = FC_PORTTYPE_UNKNOWN;
1653
1654 if (disabled) {
1655 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1656 } else {
1657 vn_port->boot_time = jiffies;
1658 fc_fabric_login(vn_port);
1659 fc_vport_setlink(vn_port);
1660 }
1661
1662 QEDF_INFO(&(base_qedf->dbg_ctx), QEDF_LOG_NPIV, "vn_port=%p.\n",
1663 vn_port);
1664
1665 /* Set up debug context for vport */
1666 vport_qedf->dbg_ctx.host_no = vn_port->host->host_no;
1667 vport_qedf->dbg_ctx.pdev = base_qedf->pdev;
1668
1669err2:
1670 scsi_host_put(vn_port->host);
1671err1:
1672 return rc;
1673}
1674
1675static int qedf_vport_destroy(struct fc_vport *vport)
1676{
1677 struct Scsi_Host *shost = vport_to_shost(vport);
1678 struct fc_lport *n_port = shost_priv(shost);
1679 struct fc_lport *vn_port = vport->dd_data;
Chad Dupuis4f4616c2018-04-25 06:09:02 -07001680 struct qedf_ctx *qedf = lport_priv(vn_port);
1681
1682 if (!qedf) {
1683 QEDF_ERR(NULL, "qedf is NULL.\n");
1684 goto out;
1685 }
1686
1687 /* Set unloading bit on vport qedf_ctx to prevent more I/O */
1688 set_bit(QEDF_UNLOADING, &qedf->flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001689
1690 mutex_lock(&n_port->lp_mutex);
1691 list_del(&vn_port->list);
1692 mutex_unlock(&n_port->lp_mutex);
1693
1694 fc_fabric_logoff(vn_port);
1695 fc_lport_destroy(vn_port);
1696
1697 /* Detach from scsi-ml */
1698 fc_remove_host(vn_port->host);
1699 scsi_remove_host(vn_port->host);
1700
1701 /*
1702 * Only try to release the exchange manager if the vn_port
1703 * configuration is complete.
1704 */
1705 if (vn_port->state == LPORT_ST_READY)
1706 fc_exch_mgr_free(vn_port);
1707
1708 /* Free memory used by statistical counters */
1709 fc_lport_free_stats(vn_port);
1710
1711 /* Release Scsi_Host */
1712 if (vn_port->host)
1713 scsi_host_put(vn_port->host);
1714
Chad Dupuis4f4616c2018-04-25 06:09:02 -07001715out:
Dupuis, Chad61d86582017-02-15 06:28:23 -08001716 return 0;
1717}
1718
1719static int qedf_vport_disable(struct fc_vport *vport, bool disable)
1720{
1721 struct fc_lport *lport = vport->dd_data;
1722
1723 if (disable) {
1724 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1725 fc_fabric_logoff(lport);
1726 } else {
1727 lport->boot_time = jiffies;
1728 fc_fabric_login(lport);
1729 fc_vport_setlink(lport);
1730 }
1731 return 0;
1732}
1733
1734/*
1735 * During removal we need to wait for all the vports associated with a port
1736 * to be destroyed so we avoid a race condition where libfc is still trying
1737 * to reap vports while the driver remove function has already reaped the
1738 * driver contexts associated with the physical port.
1739 */
1740static void qedf_wait_for_vport_destroy(struct qedf_ctx *qedf)
1741{
1742 struct fc_host_attrs *fc_host = shost_to_fc_host(qedf->lport->host);
1743
1744 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
1745 "Entered.\n");
1746 while (fc_host->npiv_vports_inuse > 0) {
1747 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
1748 "Waiting for all vports to be reaped.\n");
1749 msleep(1000);
1750 }
1751}
1752
1753/**
1754 * qedf_fcoe_reset - Resets the fcoe
1755 *
1756 * @shost: shost the reset is from
1757 *
1758 * Returns: always 0
1759 */
1760static int qedf_fcoe_reset(struct Scsi_Host *shost)
1761{
1762 struct fc_lport *lport = shost_priv(shost);
1763
Chad Dupuis5cf446d2017-05-31 06:33:55 -07001764 qedf_ctx_soft_reset(lport);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001765 return 0;
1766}
1767
1768static struct fc_host_statistics *qedf_fc_get_host_stats(struct Scsi_Host
1769 *shost)
1770{
1771 struct fc_host_statistics *qedf_stats;
1772 struct fc_lport *lport = shost_priv(shost);
1773 struct qedf_ctx *qedf = lport_priv(lport);
1774 struct qed_fcoe_stats *fw_fcoe_stats;
1775
1776 qedf_stats = fc_get_host_stats(shost);
1777
1778 /* We don't collect offload stats for specific NPIV ports */
1779 if (lport->vport)
1780 goto out;
1781
1782 fw_fcoe_stats = kmalloc(sizeof(struct qed_fcoe_stats), GFP_KERNEL);
1783 if (!fw_fcoe_stats) {
1784 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate memory for "
1785 "fw_fcoe_stats.\n");
1786 goto out;
1787 }
1788
1789 /* Query firmware for offload stats */
1790 qed_ops->get_stats(qedf->cdev, fw_fcoe_stats);
1791
1792 /*
1793 * The expectation is that we add our offload stats to the stats
1794 * being maintained by libfc each time the fc_get_host_status callback
1795 * is invoked. The additions are not carried over for each call to
1796 * the fc_get_host_stats callback.
1797 */
1798 qedf_stats->tx_frames += fw_fcoe_stats->fcoe_tx_data_pkt_cnt +
1799 fw_fcoe_stats->fcoe_tx_xfer_pkt_cnt +
1800 fw_fcoe_stats->fcoe_tx_other_pkt_cnt;
1801 qedf_stats->rx_frames += fw_fcoe_stats->fcoe_rx_data_pkt_cnt +
1802 fw_fcoe_stats->fcoe_rx_xfer_pkt_cnt +
1803 fw_fcoe_stats->fcoe_rx_other_pkt_cnt;
1804 qedf_stats->fcp_input_megabytes +=
1805 do_div(fw_fcoe_stats->fcoe_rx_byte_cnt, 1000000);
1806 qedf_stats->fcp_output_megabytes +=
1807 do_div(fw_fcoe_stats->fcoe_tx_byte_cnt, 1000000);
1808 qedf_stats->rx_words += fw_fcoe_stats->fcoe_rx_byte_cnt / 4;
1809 qedf_stats->tx_words += fw_fcoe_stats->fcoe_tx_byte_cnt / 4;
1810 qedf_stats->invalid_crc_count +=
1811 fw_fcoe_stats->fcoe_silent_drop_pkt_crc_error_cnt;
1812 qedf_stats->dumped_frames =
1813 fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
1814 qedf_stats->error_frames +=
1815 fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
1816 qedf_stats->fcp_input_requests += qedf->input_requests;
1817 qedf_stats->fcp_output_requests += qedf->output_requests;
1818 qedf_stats->fcp_control_requests += qedf->control_requests;
1819 qedf_stats->fcp_packet_aborts += qedf->packet_aborts;
1820 qedf_stats->fcp_frame_alloc_failures += qedf->alloc_failures;
1821
1822 kfree(fw_fcoe_stats);
1823out:
1824 return qedf_stats;
1825}
1826
1827static struct fc_function_template qedf_fc_transport_fn = {
1828 .show_host_node_name = 1,
1829 .show_host_port_name = 1,
1830 .show_host_supported_classes = 1,
1831 .show_host_supported_fc4s = 1,
1832 .show_host_active_fc4s = 1,
1833 .show_host_maxframe_size = 1,
1834
1835 .show_host_port_id = 1,
1836 .show_host_supported_speeds = 1,
1837 .get_host_speed = fc_get_host_speed,
1838 .show_host_speed = 1,
1839 .show_host_port_type = 1,
1840 .get_host_port_state = fc_get_host_port_state,
1841 .show_host_port_state = 1,
1842 .show_host_symbolic_name = 1,
1843
1844 /*
1845 * Tell FC transport to allocate enough space to store the backpointer
1846 * for the associate qedf_rport struct.
1847 */
1848 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
1849 sizeof(struct qedf_rport)),
1850 .show_rport_maxframe_size = 1,
1851 .show_rport_supported_classes = 1,
1852 .show_host_fabric_name = 1,
1853 .show_starget_node_name = 1,
1854 .show_starget_port_name = 1,
1855 .show_starget_port_id = 1,
1856 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
1857 .show_rport_dev_loss_tmo = 1,
1858 .get_fc_host_stats = qedf_fc_get_host_stats,
1859 .issue_fc_host_lip = qedf_fcoe_reset,
1860 .vport_create = qedf_vport_create,
1861 .vport_delete = qedf_vport_destroy,
1862 .vport_disable = qedf_vport_disable,
1863 .bsg_request = fc_lport_bsg_request,
1864};
1865
1866static struct fc_function_template qedf_fc_vport_transport_fn = {
1867 .show_host_node_name = 1,
1868 .show_host_port_name = 1,
1869 .show_host_supported_classes = 1,
1870 .show_host_supported_fc4s = 1,
1871 .show_host_active_fc4s = 1,
1872 .show_host_maxframe_size = 1,
1873 .show_host_port_id = 1,
1874 .show_host_supported_speeds = 1,
1875 .get_host_speed = fc_get_host_speed,
1876 .show_host_speed = 1,
1877 .show_host_port_type = 1,
1878 .get_host_port_state = fc_get_host_port_state,
1879 .show_host_port_state = 1,
1880 .show_host_symbolic_name = 1,
1881 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
1882 sizeof(struct qedf_rport)),
1883 .show_rport_maxframe_size = 1,
1884 .show_rport_supported_classes = 1,
1885 .show_host_fabric_name = 1,
1886 .show_starget_node_name = 1,
1887 .show_starget_port_name = 1,
1888 .show_starget_port_id = 1,
1889 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
1890 .show_rport_dev_loss_tmo = 1,
1891 .get_fc_host_stats = fc_get_host_stats,
1892 .issue_fc_host_lip = qedf_fcoe_reset,
1893 .bsg_request = fc_lport_bsg_request,
1894};
1895
1896static bool qedf_fp_has_work(struct qedf_fastpath *fp)
1897{
1898 struct qedf_ctx *qedf = fp->qedf;
1899 struct global_queue *que;
1900 struct qed_sb_info *sb_info = fp->sb_info;
Tomer Tayar21dd79e2017-12-27 19:30:06 +02001901 struct status_block_e4 *sb = sb_info->sb_virt;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001902 u16 prod_idx;
1903
1904 /* Get the pointer to the global CQ this completion is on */
1905 que = qedf->global_queues[fp->sb_id];
1906
1907 /* Be sure all responses have been written to PI */
1908 rmb();
1909
1910 /* Get the current firmware producer index */
1911 prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
1912
1913 return (que->cq_prod_idx != prod_idx);
1914}
1915
1916/*
1917 * Interrupt handler code.
1918 */
1919
1920/* Process completion queue and copy CQE contents for deferred processesing
1921 *
1922 * Return true if we should wake the I/O thread, false if not.
1923 */
1924static bool qedf_process_completions(struct qedf_fastpath *fp)
1925{
1926 struct qedf_ctx *qedf = fp->qedf;
1927 struct qed_sb_info *sb_info = fp->sb_info;
Tomer Tayar21dd79e2017-12-27 19:30:06 +02001928 struct status_block_e4 *sb = sb_info->sb_virt;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001929 struct global_queue *que;
1930 u16 prod_idx;
1931 struct fcoe_cqe *cqe;
1932 struct qedf_io_work *io_work;
1933 int num_handled = 0;
1934 unsigned int cpu;
1935 struct qedf_ioreq *io_req = NULL;
1936 u16 xid;
1937 u16 new_cqes;
1938 u32 comp_type;
1939
1940 /* Get the current firmware producer index */
1941 prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
1942
1943 /* Get the pointer to the global CQ this completion is on */
1944 que = qedf->global_queues[fp->sb_id];
1945
1946 /* Calculate the amount of new elements since last processing */
1947 new_cqes = (prod_idx >= que->cq_prod_idx) ?
1948 (prod_idx - que->cq_prod_idx) :
1949 0x10000 - que->cq_prod_idx + prod_idx;
1950
1951 /* Save producer index */
1952 que->cq_prod_idx = prod_idx;
1953
1954 while (new_cqes) {
1955 fp->completions++;
1956 num_handled++;
1957 cqe = &que->cq[que->cq_cons_idx];
1958
1959 comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
1960 FCOE_CQE_CQE_TYPE_MASK;
1961
1962 /*
1963 * Process unsolicited CQEs directly in the interrupt handler
1964 * sine we need the fastpath ID
1965 */
1966 if (comp_type == FCOE_UNSOLIC_CQE_TYPE) {
1967 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
1968 "Unsolicated CQE.\n");
1969 qedf_process_unsol_compl(qedf, fp->sb_id, cqe);
1970 /*
1971 * Don't add a work list item. Increment consumer
1972 * consumer index and move on.
1973 */
1974 goto inc_idx;
1975 }
1976
1977 xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
1978 io_req = &qedf->cmd_mgr->cmds[xid];
1979
1980 /*
1981 * Figure out which percpu thread we should queue this I/O
1982 * on.
1983 */
1984 if (!io_req)
1985 /* If there is not io_req assocated with this CQE
1986 * just queue it on CPU 0
1987 */
1988 cpu = 0;
1989 else {
1990 cpu = io_req->cpu;
1991 io_req->int_cpu = smp_processor_id();
1992 }
1993
1994 io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
1995 if (!io_work) {
1996 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
1997 "work for I/O completion.\n");
1998 continue;
1999 }
2000 memset(io_work, 0, sizeof(struct qedf_io_work));
2001
2002 INIT_WORK(&io_work->work, qedf_fp_io_handler);
2003
2004 /* Copy contents of CQE for deferred processing */
2005 memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
2006
2007 io_work->qedf = fp->qedf;
2008 io_work->fp = NULL; /* Only used for unsolicited frames */
2009
2010 queue_work_on(cpu, qedf_io_wq, &io_work->work);
2011
2012inc_idx:
2013 que->cq_cons_idx++;
2014 if (que->cq_cons_idx == fp->cq_num_entries)
2015 que->cq_cons_idx = 0;
2016 new_cqes--;
2017 }
2018
2019 return true;
2020}
2021
2022
2023/* MSI-X fastpath handler code */
2024static irqreturn_t qedf_msix_handler(int irq, void *dev_id)
2025{
2026 struct qedf_fastpath *fp = dev_id;
2027
2028 if (!fp) {
2029 QEDF_ERR(NULL, "fp is null.\n");
2030 return IRQ_HANDLED;
2031 }
2032 if (!fp->sb_info) {
2033 QEDF_ERR(NULL, "fp->sb_info in null.");
2034 return IRQ_HANDLED;
2035 }
2036
2037 /*
2038 * Disable interrupts for this status block while we process new
2039 * completions
2040 */
2041 qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
2042
2043 while (1) {
2044 qedf_process_completions(fp);
2045
2046 if (qedf_fp_has_work(fp) == 0) {
2047 /* Update the sb information */
2048 qed_sb_update_sb_idx(fp->sb_info);
2049
2050 /* Check for more work */
2051 rmb();
2052
2053 if (qedf_fp_has_work(fp) == 0) {
2054 /* Re-enable interrupts */
2055 qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
2056 return IRQ_HANDLED;
2057 }
2058 }
2059 }
2060
2061 /* Do we ever want to break out of above loop? */
2062 return IRQ_HANDLED;
2063}
2064
2065/* simd handler for MSI/INTa */
2066static void qedf_simd_int_handler(void *cookie)
2067{
2068 /* Cookie is qedf_ctx struct */
2069 struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
2070
2071 QEDF_WARN(&(qedf->dbg_ctx), "qedf=%p.\n", qedf);
2072}
2073
2074#define QEDF_SIMD_HANDLER_NUM 0
2075static void qedf_sync_free_irqs(struct qedf_ctx *qedf)
2076{
2077 int i;
2078
2079 if (qedf->int_info.msix_cnt) {
2080 for (i = 0; i < qedf->int_info.used_cnt; i++) {
2081 synchronize_irq(qedf->int_info.msix[i].vector);
2082 irq_set_affinity_hint(qedf->int_info.msix[i].vector,
2083 NULL);
2084 irq_set_affinity_notifier(qedf->int_info.msix[i].vector,
2085 NULL);
2086 free_irq(qedf->int_info.msix[i].vector,
2087 &qedf->fp_array[i]);
2088 }
2089 } else
2090 qed_ops->common->simd_handler_clean(qedf->cdev,
2091 QEDF_SIMD_HANDLER_NUM);
2092
2093 qedf->int_info.used_cnt = 0;
2094 qed_ops->common->set_fp_int(qedf->cdev, 0);
2095}
2096
2097static int qedf_request_msix_irq(struct qedf_ctx *qedf)
2098{
2099 int i, rc, cpu;
2100
2101 cpu = cpumask_first(cpu_online_mask);
2102 for (i = 0; i < qedf->num_queues; i++) {
2103 rc = request_irq(qedf->int_info.msix[i].vector,
2104 qedf_msix_handler, 0, "qedf", &qedf->fp_array[i]);
2105
2106 if (rc) {
2107 QEDF_WARN(&(qedf->dbg_ctx), "request_irq failed.\n");
2108 qedf_sync_free_irqs(qedf);
2109 return rc;
2110 }
2111
2112 qedf->int_info.used_cnt++;
2113 rc = irq_set_affinity_hint(qedf->int_info.msix[i].vector,
2114 get_cpu_mask(cpu));
2115 cpu = cpumask_next(cpu, cpu_online_mask);
2116 }
2117
2118 return 0;
2119}
2120
2121static int qedf_setup_int(struct qedf_ctx *qedf)
2122{
2123 int rc = 0;
2124
2125 /*
2126 * Learn interrupt configuration
2127 */
2128 rc = qed_ops->common->set_fp_int(qedf->cdev, num_online_cpus());
Chad Dupuis914fff12017-05-31 06:33:50 -07002129 if (rc <= 0)
2130 return 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002131
2132 rc = qed_ops->common->get_fp_int(qedf->cdev, &qedf->int_info);
2133 if (rc)
2134 return 0;
2135
2136 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of msix_cnt = "
2137 "0x%x num of cpus = 0x%x\n", qedf->int_info.msix_cnt,
2138 num_online_cpus());
2139
2140 if (qedf->int_info.msix_cnt)
2141 return qedf_request_msix_irq(qedf);
2142
2143 qed_ops->common->simd_handler_config(qedf->cdev, &qedf,
2144 QEDF_SIMD_HANDLER_NUM, qedf_simd_int_handler);
2145 qedf->int_info.used_cnt = 1;
2146
Chad Dupuis96673e12018-04-25 06:09:00 -07002147 QEDF_ERR(&qedf->dbg_ctx, "Only MSI-X supported. Failing probe.\n");
2148 return -EINVAL;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002149}
2150
2151/* Main function for libfc frame reception */
2152static void qedf_recv_frame(struct qedf_ctx *qedf,
2153 struct sk_buff *skb)
2154{
2155 u32 fr_len;
2156 struct fc_lport *lport;
2157 struct fc_frame_header *fh;
2158 struct fcoe_crc_eof crc_eof;
2159 struct fc_frame *fp;
2160 u8 *mac = NULL;
2161 u8 *dest_mac = NULL;
2162 struct fcoe_hdr *hp;
2163 struct qedf_rport *fcport;
Chad Dupuis384d5a92017-05-31 06:33:57 -07002164 struct fc_lport *vn_port;
2165 u32 f_ctl;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002166
2167 lport = qedf->lport;
2168 if (lport == NULL || lport->state == LPORT_ST_DISABLED) {
2169 QEDF_WARN(NULL, "Invalid lport struct or lport disabled.\n");
2170 kfree_skb(skb);
2171 return;
2172 }
2173
2174 if (skb_is_nonlinear(skb))
2175 skb_linearize(skb);
2176 mac = eth_hdr(skb)->h_source;
2177 dest_mac = eth_hdr(skb)->h_dest;
2178
2179 /* Pull the header */
2180 hp = (struct fcoe_hdr *)skb->data;
2181 fh = (struct fc_frame_header *) skb_transport_header(skb);
2182 skb_pull(skb, sizeof(struct fcoe_hdr));
2183 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
2184
2185 fp = (struct fc_frame *)skb;
2186 fc_frame_init(fp);
2187 fr_dev(fp) = lport;
2188 fr_sof(fp) = hp->fcoe_sof;
2189 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
2190 kfree_skb(skb);
2191 return;
2192 }
2193 fr_eof(fp) = crc_eof.fcoe_eof;
2194 fr_crc(fp) = crc_eof.fcoe_crc32;
2195 if (pskb_trim(skb, fr_len)) {
2196 kfree_skb(skb);
2197 return;
2198 }
2199
2200 fh = fc_frame_header_get(fp);
2201
Chad Dupuis384d5a92017-05-31 06:33:57 -07002202 /*
2203 * Invalid frame filters.
2204 */
2205
Dupuis, Chad61d86582017-02-15 06:28:23 -08002206 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
2207 fh->fh_type == FC_TYPE_FCP) {
2208 /* Drop FCP data. We dont this in L2 path */
2209 kfree_skb(skb);
2210 return;
2211 }
2212 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
2213 fh->fh_type == FC_TYPE_ELS) {
2214 switch (fc_frame_payload_op(fp)) {
2215 case ELS_LOGO:
2216 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
2217 /* drop non-FIP LOGO */
2218 kfree_skb(skb);
2219 return;
2220 }
2221 break;
2222 }
2223 }
2224
2225 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
2226 /* Drop incoming ABTS */
2227 kfree_skb(skb);
2228 return;
2229 }
2230
Chad Dupuis384d5a92017-05-31 06:33:57 -07002231 if (ntoh24(&dest_mac[3]) != ntoh24(fh->fh_d_id)) {
Chad Dupuisf7e8d572017-05-31 06:33:59 -07002232 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2233 "FC frame d_id mismatch with MAC %pM.\n", dest_mac);
Chad Dupuisf32803b2018-04-25 06:08:47 -07002234 kfree_skb(skb);
Chad Dupuis384d5a92017-05-31 06:33:57 -07002235 return;
2236 }
2237
2238 if (qedf->ctlr.state) {
2239 if (!ether_addr_equal(mac, qedf->ctlr.dest_addr)) {
Chad Dupuisf7e8d572017-05-31 06:33:59 -07002240 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2241 "Wrong source address: mac:%pM dest_addr:%pM.\n",
Chad Dupuis384d5a92017-05-31 06:33:57 -07002242 mac, qedf->ctlr.dest_addr);
2243 kfree_skb(skb);
2244 return;
2245 }
2246 }
2247
2248 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
2249
2250 /*
2251 * If the destination ID from the frame header does not match what we
2252 * have on record for lport and the search for a NPIV port came up
2253 * empty then this is not addressed to our port so simply drop it.
2254 */
2255 if (lport->port_id != ntoh24(fh->fh_d_id) && !vn_port) {
Chad Dupuisf7e8d572017-05-31 06:33:59 -07002256 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2257 "Dropping frame due to destination mismatch: lport->port_id=%x fh->d_id=%x.\n",
Chad Dupuis384d5a92017-05-31 06:33:57 -07002258 lport->port_id, ntoh24(fh->fh_d_id));
2259 kfree_skb(skb);
2260 return;
2261 }
2262
2263 f_ctl = ntoh24(fh->fh_f_ctl);
2264 if ((fh->fh_type == FC_TYPE_BLS) && (f_ctl & FC_FC_SEQ_CTX) &&
2265 (f_ctl & FC_FC_EX_CTX)) {
2266 /* Drop incoming ABTS response that has both SEQ/EX CTX set */
2267 kfree_skb(skb);
2268 return;
2269 }
2270
Dupuis, Chad61d86582017-02-15 06:28:23 -08002271 /*
2272 * If a connection is uploading, drop incoming FCoE frames as there
2273 * is a small window where we could try to return a frame while libfc
2274 * is trying to clean things up.
2275 */
2276
2277 /* Get fcport associated with d_id if it exists */
2278 fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
2279
2280 if (fcport && test_bit(QEDF_RPORT_UPLOADING_CONNECTION,
2281 &fcport->flags)) {
2282 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2283 "Connection uploading, dropping fp=%p.\n", fp);
2284 kfree_skb(skb);
2285 return;
2286 }
2287
2288 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame receive: "
2289 "skb=%p fp=%p src=%06x dest=%06x r_ctl=%x fh_type=%x.\n", skb, fp,
2290 ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl,
2291 fh->fh_type);
2292 if (qedf_dump_frames)
2293 print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
2294 1, skb->data, skb->len, false);
2295 fc_exch_recv(lport, fp);
2296}
2297
2298static void qedf_ll2_process_skb(struct work_struct *work)
2299{
2300 struct qedf_skb_work *skb_work =
2301 container_of(work, struct qedf_skb_work, work);
2302 struct qedf_ctx *qedf = skb_work->qedf;
2303 struct sk_buff *skb = skb_work->skb;
2304 struct ethhdr *eh;
2305
2306 if (!qedf) {
2307 QEDF_ERR(NULL, "qedf is NULL\n");
2308 goto err_out;
2309 }
2310
2311 eh = (struct ethhdr *)skb->data;
2312
2313 /* Undo VLAN encapsulation */
2314 if (eh->h_proto == htons(ETH_P_8021Q)) {
2315 memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
Johannes Bergaf728682017-06-16 14:29:22 +02002316 eh = skb_pull(skb, VLAN_HLEN);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002317 skb_reset_mac_header(skb);
2318 }
2319
2320 /*
2321 * Process either a FIP frame or FCoE frame based on the
2322 * protocol value. If it's not either just drop the
2323 * frame.
2324 */
2325 if (eh->h_proto == htons(ETH_P_FIP)) {
2326 qedf_fip_recv(qedf, skb);
2327 goto out;
2328 } else if (eh->h_proto == htons(ETH_P_FCOE)) {
2329 __skb_pull(skb, ETH_HLEN);
2330 qedf_recv_frame(qedf, skb);
2331 goto out;
2332 } else
2333 goto err_out;
2334
2335err_out:
2336 kfree_skb(skb);
2337out:
2338 kfree(skb_work);
2339 return;
2340}
2341
2342static int qedf_ll2_rx(void *cookie, struct sk_buff *skb,
2343 u32 arg1, u32 arg2)
2344{
2345 struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
2346 struct qedf_skb_work *skb_work;
2347
2348 skb_work = kzalloc(sizeof(struct qedf_skb_work), GFP_ATOMIC);
2349 if (!skb_work) {
2350 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate skb_work so "
2351 "dropping frame.\n");
2352 kfree_skb(skb);
2353 return 0;
2354 }
2355
2356 INIT_WORK(&skb_work->work, qedf_ll2_process_skb);
2357 skb_work->skb = skb;
2358 skb_work->qedf = qedf;
2359 queue_work(qedf->ll2_recv_wq, &skb_work->work);
2360
2361 return 0;
2362}
2363
2364static struct qed_ll2_cb_ops qedf_ll2_cb_ops = {
2365 .rx_cb = qedf_ll2_rx,
2366 .tx_cb = NULL,
2367};
2368
2369/* Main thread to process I/O completions */
2370void qedf_fp_io_handler(struct work_struct *work)
2371{
2372 struct qedf_io_work *io_work =
2373 container_of(work, struct qedf_io_work, work);
2374 u32 comp_type;
2375
2376 /*
2377 * Deferred part of unsolicited CQE sends
2378 * frame to libfc.
2379 */
2380 comp_type = (io_work->cqe.cqe_data >>
2381 FCOE_CQE_CQE_TYPE_SHIFT) &
2382 FCOE_CQE_CQE_TYPE_MASK;
2383 if (comp_type == FCOE_UNSOLIC_CQE_TYPE &&
2384 io_work->fp)
2385 fc_exch_recv(io_work->qedf->lport, io_work->fp);
2386 else
2387 qedf_process_cqe(io_work->qedf, &io_work->cqe);
2388
2389 kfree(io_work);
2390}
2391
2392static int qedf_alloc_and_init_sb(struct qedf_ctx *qedf,
2393 struct qed_sb_info *sb_info, u16 sb_id)
2394{
Tomer Tayar21dd79e2017-12-27 19:30:06 +02002395 struct status_block_e4 *sb_virt;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002396 dma_addr_t sb_phys;
2397 int ret;
2398
2399 sb_virt = dma_alloc_coherent(&qedf->pdev->dev,
Tomer Tayar21dd79e2017-12-27 19:30:06 +02002400 sizeof(struct status_block_e4), &sb_phys, GFP_KERNEL);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002401
2402 if (!sb_virt) {
2403 QEDF_ERR(&(qedf->dbg_ctx), "Status block allocation failed "
2404 "for id = %d.\n", sb_id);
2405 return -ENOMEM;
2406 }
2407
2408 ret = qed_ops->common->sb_init(qedf->cdev, sb_info, sb_virt, sb_phys,
2409 sb_id, QED_SB_TYPE_STORAGE);
2410
2411 if (ret) {
2412 QEDF_ERR(&(qedf->dbg_ctx), "Status block initialization "
2413 "failed for id = %d.\n", sb_id);
2414 return ret;
2415 }
2416
2417 return 0;
2418}
2419
2420static void qedf_free_sb(struct qedf_ctx *qedf, struct qed_sb_info *sb_info)
2421{
2422 if (sb_info->sb_virt)
2423 dma_free_coherent(&qedf->pdev->dev, sizeof(*sb_info->sb_virt),
2424 (void *)sb_info->sb_virt, sb_info->sb_phys);
2425}
2426
2427static void qedf_destroy_sb(struct qedf_ctx *qedf)
2428{
2429 int id;
2430 struct qedf_fastpath *fp = NULL;
2431
2432 for (id = 0; id < qedf->num_queues; id++) {
2433 fp = &(qedf->fp_array[id]);
2434 if (fp->sb_id == QEDF_SB_ID_NULL)
2435 break;
2436 qedf_free_sb(qedf, fp->sb_info);
2437 kfree(fp->sb_info);
2438 }
2439 kfree(qedf->fp_array);
2440}
2441
2442static int qedf_prepare_sb(struct qedf_ctx *qedf)
2443{
2444 int id;
2445 struct qedf_fastpath *fp;
2446 int ret;
2447
2448 qedf->fp_array =
2449 kcalloc(qedf->num_queues, sizeof(struct qedf_fastpath),
2450 GFP_KERNEL);
2451
2452 if (!qedf->fp_array) {
2453 QEDF_ERR(&(qedf->dbg_ctx), "fastpath array allocation "
2454 "failed.\n");
2455 return -ENOMEM;
2456 }
2457
2458 for (id = 0; id < qedf->num_queues; id++) {
2459 fp = &(qedf->fp_array[id]);
2460 fp->sb_id = QEDF_SB_ID_NULL;
2461 fp->sb_info = kcalloc(1, sizeof(*fp->sb_info), GFP_KERNEL);
2462 if (!fp->sb_info) {
2463 QEDF_ERR(&(qedf->dbg_ctx), "SB info struct "
2464 "allocation failed.\n");
2465 goto err;
2466 }
2467 ret = qedf_alloc_and_init_sb(qedf, fp->sb_info, id);
2468 if (ret) {
2469 QEDF_ERR(&(qedf->dbg_ctx), "SB allocation and "
2470 "initialization failed.\n");
2471 goto err;
2472 }
2473 fp->sb_id = id;
2474 fp->qedf = qedf;
2475 fp->cq_num_entries =
2476 qedf->global_queues[id]->cq_mem_size /
2477 sizeof(struct fcoe_cqe);
2478 }
2479err:
2480 return 0;
2481}
2482
2483void qedf_process_cqe(struct qedf_ctx *qedf, struct fcoe_cqe *cqe)
2484{
2485 u16 xid;
2486 struct qedf_ioreq *io_req;
2487 struct qedf_rport *fcport;
2488 u32 comp_type;
2489
2490 comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
2491 FCOE_CQE_CQE_TYPE_MASK;
2492
2493 xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
2494 io_req = &qedf->cmd_mgr->cmds[xid];
2495
2496 /* Completion not for a valid I/O anymore so just return */
2497 if (!io_req)
2498 return;
2499
2500 fcport = io_req->fcport;
2501
2502 if (fcport == NULL) {
2503 QEDF_ERR(&(qedf->dbg_ctx), "fcport is NULL.\n");
2504 return;
2505 }
2506
2507 /*
2508 * Check that fcport is offloaded. If it isn't then the spinlock
2509 * isn't valid and shouldn't be taken. We should just return.
2510 */
2511 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
2512 QEDF_ERR(&(qedf->dbg_ctx), "Session not offloaded yet.\n");
2513 return;
2514 }
2515
2516
2517 switch (comp_type) {
2518 case FCOE_GOOD_COMPLETION_CQE_TYPE:
2519 atomic_inc(&fcport->free_sqes);
2520 switch (io_req->cmd_type) {
2521 case QEDF_SCSI_CMD:
2522 qedf_scsi_completion(qedf, cqe, io_req);
2523 break;
2524 case QEDF_ELS:
2525 qedf_process_els_compl(qedf, cqe, io_req);
2526 break;
2527 case QEDF_TASK_MGMT_CMD:
2528 qedf_process_tmf_compl(qedf, cqe, io_req);
2529 break;
2530 case QEDF_SEQ_CLEANUP:
2531 qedf_process_seq_cleanup_compl(qedf, cqe, io_req);
2532 break;
2533 }
2534 break;
2535 case FCOE_ERROR_DETECTION_CQE_TYPE:
2536 atomic_inc(&fcport->free_sqes);
2537 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2538 "Error detect CQE.\n");
2539 qedf_process_error_detect(qedf, cqe, io_req);
2540 break;
2541 case FCOE_EXCH_CLEANUP_CQE_TYPE:
2542 atomic_inc(&fcport->free_sqes);
2543 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2544 "Cleanup CQE.\n");
2545 qedf_process_cleanup_compl(qedf, cqe, io_req);
2546 break;
2547 case FCOE_ABTS_CQE_TYPE:
2548 atomic_inc(&fcport->free_sqes);
2549 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2550 "Abort CQE.\n");
2551 qedf_process_abts_compl(qedf, cqe, io_req);
2552 break;
2553 case FCOE_DUMMY_CQE_TYPE:
2554 atomic_inc(&fcport->free_sqes);
2555 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2556 "Dummy CQE.\n");
2557 break;
2558 case FCOE_LOCAL_COMP_CQE_TYPE:
2559 atomic_inc(&fcport->free_sqes);
2560 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2561 "Local completion CQE.\n");
2562 break;
2563 case FCOE_WARNING_CQE_TYPE:
2564 atomic_inc(&fcport->free_sqes);
2565 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2566 "Warning CQE.\n");
2567 qedf_process_warning_compl(qedf, cqe, io_req);
2568 break;
2569 case MAX_FCOE_CQE_TYPE:
2570 atomic_inc(&fcport->free_sqes);
2571 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2572 "Max FCoE CQE.\n");
2573 break;
2574 default:
2575 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2576 "Default CQE.\n");
2577 break;
2578 }
2579}
2580
2581static void qedf_free_bdq(struct qedf_ctx *qedf)
2582{
2583 int i;
2584
2585 if (qedf->bdq_pbl_list)
2586 dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
2587 qedf->bdq_pbl_list, qedf->bdq_pbl_list_dma);
2588
2589 if (qedf->bdq_pbl)
2590 dma_free_coherent(&qedf->pdev->dev, qedf->bdq_pbl_mem_size,
2591 qedf->bdq_pbl, qedf->bdq_pbl_dma);
2592
2593 for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2594 if (qedf->bdq[i].buf_addr) {
2595 dma_free_coherent(&qedf->pdev->dev, QEDF_BDQ_BUF_SIZE,
2596 qedf->bdq[i].buf_addr, qedf->bdq[i].buf_dma);
2597 }
2598 }
2599}
2600
2601static void qedf_free_global_queues(struct qedf_ctx *qedf)
2602{
2603 int i;
2604 struct global_queue **gl = qedf->global_queues;
2605
2606 for (i = 0; i < qedf->num_queues; i++) {
2607 if (!gl[i])
2608 continue;
2609
2610 if (gl[i]->cq)
2611 dma_free_coherent(&qedf->pdev->dev,
2612 gl[i]->cq_mem_size, gl[i]->cq, gl[i]->cq_dma);
2613 if (gl[i]->cq_pbl)
2614 dma_free_coherent(&qedf->pdev->dev, gl[i]->cq_pbl_size,
2615 gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
2616
2617 kfree(gl[i]);
2618 }
2619
2620 qedf_free_bdq(qedf);
2621}
2622
2623static int qedf_alloc_bdq(struct qedf_ctx *qedf)
2624{
2625 int i;
2626 struct scsi_bd *pbl;
2627 u64 *list;
2628 dma_addr_t page;
2629
2630 /* Alloc dma memory for BDQ buffers */
2631 for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2632 qedf->bdq[i].buf_addr = dma_alloc_coherent(&qedf->pdev->dev,
2633 QEDF_BDQ_BUF_SIZE, &qedf->bdq[i].buf_dma, GFP_KERNEL);
2634 if (!qedf->bdq[i].buf_addr) {
2635 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ "
2636 "buffer %d.\n", i);
2637 return -ENOMEM;
2638 }
2639 }
2640
2641 /* Alloc dma memory for BDQ page buffer list */
2642 qedf->bdq_pbl_mem_size =
2643 QEDF_BDQ_SIZE * sizeof(struct scsi_bd);
2644 qedf->bdq_pbl_mem_size =
2645 ALIGN(qedf->bdq_pbl_mem_size, QEDF_PAGE_SIZE);
2646
2647 qedf->bdq_pbl = dma_alloc_coherent(&qedf->pdev->dev,
2648 qedf->bdq_pbl_mem_size, &qedf->bdq_pbl_dma, GFP_KERNEL);
2649 if (!qedf->bdq_pbl) {
2650 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ PBL.\n");
2651 return -ENOMEM;
2652 }
2653
2654 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
Joe Perchesfd2b18b2017-03-06 10:32:27 -08002655 "BDQ PBL addr=0x%p dma=%pad\n",
2656 qedf->bdq_pbl, &qedf->bdq_pbl_dma);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002657
2658 /*
2659 * Populate BDQ PBL with physical and virtual address of individual
2660 * BDQ buffers
2661 */
2662 pbl = (struct scsi_bd *)qedf->bdq_pbl;
2663 for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2664 pbl->address.hi = cpu_to_le32(U64_HI(qedf->bdq[i].buf_dma));
2665 pbl->address.lo = cpu_to_le32(U64_LO(qedf->bdq[i].buf_dma));
Tomer Tayarda090912017-12-27 19:30:07 +02002666 pbl->opaque.fcoe_opaque.hi = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002667 /* Opaque lo data is an index into the BDQ array */
Tomer Tayarda090912017-12-27 19:30:07 +02002668 pbl->opaque.fcoe_opaque.lo = cpu_to_le32(i);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002669 pbl++;
2670 }
2671
2672 /* Allocate list of PBL pages */
Christophe JAILLETc4d6ffc2017-06-11 08:16:03 +02002673 qedf->bdq_pbl_list = dma_zalloc_coherent(&qedf->pdev->dev,
Dupuis, Chad61d86582017-02-15 06:28:23 -08002674 QEDF_PAGE_SIZE, &qedf->bdq_pbl_list_dma, GFP_KERNEL);
2675 if (!qedf->bdq_pbl_list) {
Christophe JAILLET32eebb32017-06-11 08:16:04 +02002676 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate list of PBL pages.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08002677 return -ENOMEM;
2678 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08002679
2680 /*
2681 * Now populate PBL list with pages that contain pointers to the
2682 * individual buffers.
2683 */
2684 qedf->bdq_pbl_list_num_entries = qedf->bdq_pbl_mem_size /
2685 QEDF_PAGE_SIZE;
2686 list = (u64 *)qedf->bdq_pbl_list;
2687 page = qedf->bdq_pbl_list_dma;
2688 for (i = 0; i < qedf->bdq_pbl_list_num_entries; i++) {
2689 *list = qedf->bdq_pbl_dma;
2690 list++;
2691 page += QEDF_PAGE_SIZE;
2692 }
2693
2694 return 0;
2695}
2696
2697static int qedf_alloc_global_queues(struct qedf_ctx *qedf)
2698{
2699 u32 *list;
2700 int i;
2701 int status = 0, rc;
2702 u32 *pbl;
2703 dma_addr_t page;
2704 int num_pages;
2705
2706 /* Allocate and map CQs, RQs */
2707 /*
2708 * Number of global queues (CQ / RQ). This should
2709 * be <= number of available MSIX vectors for the PF
2710 */
2711 if (!qedf->num_queues) {
2712 QEDF_ERR(&(qedf->dbg_ctx), "No MSI-X vectors available!\n");
2713 return 1;
2714 }
2715
2716 /*
2717 * Make sure we allocated the PBL that will contain the physical
2718 * addresses of our queues
2719 */
2720 if (!qedf->p_cpuq) {
2721 status = 1;
2722 goto mem_alloc_failure;
2723 }
2724
2725 qedf->global_queues = kzalloc((sizeof(struct global_queue *)
2726 * qedf->num_queues), GFP_KERNEL);
2727 if (!qedf->global_queues) {
2728 QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate global "
2729 "queues array ptr memory\n");
2730 return -ENOMEM;
2731 }
2732 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2733 "qedf->global_queues=%p.\n", qedf->global_queues);
2734
2735 /* Allocate DMA coherent buffers for BDQ */
2736 rc = qedf_alloc_bdq(qedf);
2737 if (rc)
2738 goto mem_alloc_failure;
2739
2740 /* Allocate a CQ and an associated PBL for each MSI-X vector */
2741 for (i = 0; i < qedf->num_queues; i++) {
2742 qedf->global_queues[i] = kzalloc(sizeof(struct global_queue),
2743 GFP_KERNEL);
2744 if (!qedf->global_queues[i]) {
Christophe JAILLET3a240b22017-06-11 08:16:02 +02002745 QEDF_WARN(&(qedf->dbg_ctx), "Unable to allocate "
Dupuis, Chad61d86582017-02-15 06:28:23 -08002746 "global queue %d.\n", i);
Christophe JAILLET3a240b22017-06-11 08:16:02 +02002747 status = -ENOMEM;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002748 goto mem_alloc_failure;
2749 }
2750
2751 qedf->global_queues[i]->cq_mem_size =
2752 FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
2753 qedf->global_queues[i]->cq_mem_size =
2754 ALIGN(qedf->global_queues[i]->cq_mem_size, QEDF_PAGE_SIZE);
2755
2756 qedf->global_queues[i]->cq_pbl_size =
2757 (qedf->global_queues[i]->cq_mem_size /
2758 PAGE_SIZE) * sizeof(void *);
2759 qedf->global_queues[i]->cq_pbl_size =
2760 ALIGN(qedf->global_queues[i]->cq_pbl_size, QEDF_PAGE_SIZE);
2761
2762 qedf->global_queues[i]->cq =
Christophe JAILLETc4d6ffc2017-06-11 08:16:03 +02002763 dma_zalloc_coherent(&qedf->pdev->dev,
Dupuis, Chad61d86582017-02-15 06:28:23 -08002764 qedf->global_queues[i]->cq_mem_size,
2765 &qedf->global_queues[i]->cq_dma, GFP_KERNEL);
2766
2767 if (!qedf->global_queues[i]->cq) {
Christophe JAILLET32eebb32017-06-11 08:16:04 +02002768 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08002769 status = -ENOMEM;
2770 goto mem_alloc_failure;
2771 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08002772
2773 qedf->global_queues[i]->cq_pbl =
Christophe JAILLETc4d6ffc2017-06-11 08:16:03 +02002774 dma_zalloc_coherent(&qedf->pdev->dev,
Dupuis, Chad61d86582017-02-15 06:28:23 -08002775 qedf->global_queues[i]->cq_pbl_size,
2776 &qedf->global_queues[i]->cq_pbl_dma, GFP_KERNEL);
2777
2778 if (!qedf->global_queues[i]->cq_pbl) {
Christophe JAILLET32eebb32017-06-11 08:16:04 +02002779 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq PBL.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08002780 status = -ENOMEM;
2781 goto mem_alloc_failure;
2782 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08002783
2784 /* Create PBL */
2785 num_pages = qedf->global_queues[i]->cq_mem_size /
2786 QEDF_PAGE_SIZE;
2787 page = qedf->global_queues[i]->cq_dma;
2788 pbl = (u32 *)qedf->global_queues[i]->cq_pbl;
2789
2790 while (num_pages--) {
2791 *pbl = U64_LO(page);
2792 pbl++;
2793 *pbl = U64_HI(page);
2794 pbl++;
2795 page += QEDF_PAGE_SIZE;
2796 }
2797 /* Set the initial consumer index for cq */
2798 qedf->global_queues[i]->cq_cons_idx = 0;
2799 }
2800
2801 list = (u32 *)qedf->p_cpuq;
2802
2803 /*
2804 * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
2805 * CQ#1 PBL pointer, RQ#1 PBL pointer, etc. Each PBL pointer points
2806 * to the physical address which contains an array of pointers to
2807 * the physical addresses of the specific queue pages.
2808 */
2809 for (i = 0; i < qedf->num_queues; i++) {
2810 *list = U64_LO(qedf->global_queues[i]->cq_pbl_dma);
2811 list++;
2812 *list = U64_HI(qedf->global_queues[i]->cq_pbl_dma);
2813 list++;
2814 *list = U64_LO(0);
2815 list++;
2816 *list = U64_HI(0);
2817 list++;
2818 }
2819
2820 return 0;
2821
2822mem_alloc_failure:
2823 qedf_free_global_queues(qedf);
2824 return status;
2825}
2826
2827static int qedf_set_fcoe_pf_param(struct qedf_ctx *qedf)
2828{
2829 u8 sq_num_pbl_pages;
2830 u32 sq_mem_size;
2831 u32 cq_mem_size;
2832 u32 cq_num_entries;
2833 int rval;
2834
2835 /*
2836 * The number of completion queues/fastpath interrupts/status blocks
2837 * we allocation is the minimum off:
2838 *
2839 * Number of CPUs
Thomas Bogendoerfer722477c2017-07-25 11:19:21 +02002840 * Number allocated by qed for our PCI function
Dupuis, Chad61d86582017-02-15 06:28:23 -08002841 */
Thomas Bogendoerfer722477c2017-07-25 11:19:21 +02002842 qedf->num_queues = MIN_NUM_CPUS_MSIX(qedf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002843
2844 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of CQs is %d.\n",
2845 qedf->num_queues);
2846
2847 qedf->p_cpuq = pci_alloc_consistent(qedf->pdev,
2848 qedf->num_queues * sizeof(struct qedf_glbl_q_params),
2849 &qedf->hw_p_cpuq);
2850
2851 if (!qedf->p_cpuq) {
2852 QEDF_ERR(&(qedf->dbg_ctx), "pci_alloc_consistent failed.\n");
2853 return 1;
2854 }
2855
2856 rval = qedf_alloc_global_queues(qedf);
2857 if (rval) {
2858 QEDF_ERR(&(qedf->dbg_ctx), "Global queue allocation "
2859 "failed.\n");
2860 return 1;
2861 }
2862
2863 /* Calculate SQ PBL size in the same manner as in qedf_sq_alloc() */
2864 sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
2865 sq_mem_size = ALIGN(sq_mem_size, QEDF_PAGE_SIZE);
2866 sq_num_pbl_pages = (sq_mem_size / QEDF_PAGE_SIZE);
2867
2868 /* Calculate CQ num entries */
2869 cq_mem_size = FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
2870 cq_mem_size = ALIGN(cq_mem_size, QEDF_PAGE_SIZE);
2871 cq_num_entries = cq_mem_size / sizeof(struct fcoe_cqe);
2872
Christophe JAILLET32eebb32017-06-11 08:16:04 +02002873 memset(&(qedf->pf_params), 0, sizeof(qedf->pf_params));
Dupuis, Chad61d86582017-02-15 06:28:23 -08002874
2875 /* Setup the value for fcoe PF */
2876 qedf->pf_params.fcoe_pf_params.num_cons = QEDF_MAX_SESSIONS;
2877 qedf->pf_params.fcoe_pf_params.num_tasks = FCOE_PARAMS_NUM_TASKS;
2878 qedf->pf_params.fcoe_pf_params.glbl_q_params_addr =
2879 (u64)qedf->hw_p_cpuq;
2880 qedf->pf_params.fcoe_pf_params.sq_num_pbl_pages = sq_num_pbl_pages;
2881
2882 qedf->pf_params.fcoe_pf_params.rq_buffer_log_size = 0;
2883
2884 qedf->pf_params.fcoe_pf_params.cq_num_entries = cq_num_entries;
2885 qedf->pf_params.fcoe_pf_params.num_cqs = qedf->num_queues;
2886
2887 /* log_page_size: 12 for 4KB pages */
2888 qedf->pf_params.fcoe_pf_params.log_page_size = ilog2(QEDF_PAGE_SIZE);
2889
2890 qedf->pf_params.fcoe_pf_params.mtu = 9000;
2891 qedf->pf_params.fcoe_pf_params.gl_rq_pi = QEDF_FCOE_PARAMS_GL_RQ_PI;
2892 qedf->pf_params.fcoe_pf_params.gl_cmd_pi = QEDF_FCOE_PARAMS_GL_CMD_PI;
2893
2894 /* BDQ address and size */
2895 qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0] =
2896 qedf->bdq_pbl_list_dma;
2897 qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0] =
2898 qedf->bdq_pbl_list_num_entries;
2899 qedf->pf_params.fcoe_pf_params.rq_buffer_size = QEDF_BDQ_BUF_SIZE;
2900
2901 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2902 "bdq_list=%p bdq_pbl_list_dma=%llx bdq_pbl_list_entries=%d.\n",
2903 qedf->bdq_pbl_list,
2904 qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0],
2905 qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0]);
2906
2907 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2908 "cq_num_entries=%d.\n",
2909 qedf->pf_params.fcoe_pf_params.cq_num_entries);
2910
2911 return 0;
2912}
2913
2914/* Free DMA coherent memory for array of queue pointers we pass to qed */
2915static void qedf_free_fcoe_pf_param(struct qedf_ctx *qedf)
2916{
2917 size_t size = 0;
2918
2919 if (qedf->p_cpuq) {
2920 size = qedf->num_queues * sizeof(struct qedf_glbl_q_params);
2921 pci_free_consistent(qedf->pdev, size, qedf->p_cpuq,
2922 qedf->hw_p_cpuq);
2923 }
2924
2925 qedf_free_global_queues(qedf);
2926
2927 if (qedf->global_queues)
2928 kfree(qedf->global_queues);
2929}
2930
2931/*
2932 * PCI driver functions
2933 */
2934
2935static const struct pci_device_id qedf_pci_tbl[] = {
2936 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165c) },
2937 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8080) },
2938 {0}
2939};
2940MODULE_DEVICE_TABLE(pci, qedf_pci_tbl);
2941
2942static struct pci_driver qedf_pci_driver = {
2943 .name = QEDF_MODULE_NAME,
2944 .id_table = qedf_pci_tbl,
2945 .probe = qedf_probe,
2946 .remove = qedf_remove,
2947};
2948
2949static int __qedf_probe(struct pci_dev *pdev, int mode)
2950{
2951 int rc = -EINVAL;
2952 struct fc_lport *lport;
2953 struct qedf_ctx *qedf;
2954 struct Scsi_Host *host;
2955 bool is_vf = false;
2956 struct qed_ll2_params params;
2957 char host_buf[20];
2958 struct qed_link_params link_params;
2959 int status;
2960 void *task_start, *task_end;
2961 struct qed_slowpath_params slowpath_params;
2962 struct qed_probe_params qed_params;
2963 u16 tmp;
2964
2965 /*
2966 * When doing error recovery we didn't reap the lport so don't try
2967 * to reallocate it.
2968 */
2969 if (mode != QEDF_MODE_RECOVERY) {
2970 lport = libfc_host_alloc(&qedf_host_template,
2971 sizeof(struct qedf_ctx));
2972
2973 if (!lport) {
2974 QEDF_ERR(NULL, "Could not allocate lport.\n");
2975 rc = -ENOMEM;
2976 goto err0;
2977 }
2978
2979 /* Initialize qedf_ctx */
2980 qedf = lport_priv(lport);
2981 qedf->lport = lport;
2982 qedf->ctlr.lp = lport;
2983 qedf->pdev = pdev;
2984 qedf->dbg_ctx.pdev = pdev;
2985 qedf->dbg_ctx.host_no = lport->host->host_no;
2986 spin_lock_init(&qedf->hba_lock);
2987 INIT_LIST_HEAD(&qedf->fcports);
2988 qedf->curr_conn_id = QEDF_MAX_SESSIONS - 1;
2989 atomic_set(&qedf->num_offloads, 0);
2990 qedf->stop_io_on_error = false;
2991 pci_set_drvdata(pdev, qedf);
Chad Dupuis8eaf7df2017-03-23 06:58:47 -07002992 init_completion(&qedf->fipvlan_compl);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002993
2994 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO,
2995 "QLogic FastLinQ FCoE Module qedf %s, "
2996 "FW %d.%d.%d.%d\n", QEDF_VERSION,
2997 FW_MAJOR_VERSION, FW_MINOR_VERSION, FW_REVISION_VERSION,
2998 FW_ENGINEERING_VERSION);
2999 } else {
3000 /* Init pointers during recovery */
3001 qedf = pci_get_drvdata(pdev);
3002 lport = qedf->lport;
3003 }
3004
3005 host = lport->host;
3006
3007 /* Allocate mempool for qedf_io_work structs */
3008 qedf->io_mempool = mempool_create_slab_pool(QEDF_IO_WORK_MIN,
3009 qedf_io_work_cache);
3010 if (qedf->io_mempool == NULL) {
3011 QEDF_ERR(&(qedf->dbg_ctx), "qedf->io_mempool is NULL.\n");
3012 goto err1;
3013 }
3014 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO, "qedf->io_mempool=%p.\n",
3015 qedf->io_mempool);
3016
3017 sprintf(host_buf, "qedf_%u_link",
3018 qedf->lport->host->host_no);
Chad Dupuis428ca642017-08-15 10:08:19 -07003019 qedf->link_update_wq = create_workqueue(host_buf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003020 INIT_DELAYED_WORK(&qedf->link_update, qedf_handle_link_update);
3021 INIT_DELAYED_WORK(&qedf->link_recovery, qedf_link_recovery);
Chad Dupuis4b9b7fa2018-04-25 06:08:58 -07003022 INIT_DELAYED_WORK(&qedf->grcdump_work, qedf_wq_grcdump);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003023 qedf->fipvlan_retries = qedf_fipvlan_retries;
Chad Dupuis84b2ba62018-04-25 06:08:52 -07003024 /* Set a default prio in case DCBX doesn't converge */
Chad Dupuis65b7bec2018-04-25 06:08:59 -07003025 if (qedf_default_prio > -1) {
3026 /*
3027 * This is the case where we pass a modparam in so we want to
3028 * honor it even if dcbx doesn't converge.
3029 */
3030 qedf->prio = qedf_default_prio;
3031 } else
3032 qedf->prio = QEDF_DEFAULT_PRIO;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003033
3034 /*
3035 * Common probe. Takes care of basic hardware init and pci_*
3036 * functions.
3037 */
3038 memset(&qed_params, 0, sizeof(qed_params));
3039 qed_params.protocol = QED_PROTOCOL_FCOE;
3040 qed_params.dp_module = qedf_dp_module;
3041 qed_params.dp_level = qedf_dp_level;
3042 qed_params.is_vf = is_vf;
3043 qedf->cdev = qed_ops->common->probe(pdev, &qed_params);
3044 if (!qedf->cdev) {
3045 rc = -ENODEV;
3046 goto err1;
3047 }
3048
Thomas Bogendoerfer722477c2017-07-25 11:19:21 +02003049 /* Learn information crucial for qedf to progress */
3050 rc = qed_ops->fill_dev_info(qedf->cdev, &qedf->dev_info);
3051 if (rc) {
3052 QEDF_ERR(&(qedf->dbg_ctx), "Failed to dev info.\n");
3053 goto err1;
3054 }
3055
Dupuis, Chad61d86582017-02-15 06:28:23 -08003056 /* queue allocation code should come here
3057 * order should be
3058 * slowpath_start
3059 * status block allocation
3060 * interrupt registration (to get min number of queues)
3061 * set_fcoe_pf_param
3062 * qed_sp_fcoe_func_start
3063 */
3064 rc = qedf_set_fcoe_pf_param(qedf);
3065 if (rc) {
3066 QEDF_ERR(&(qedf->dbg_ctx), "Cannot set fcoe pf param.\n");
3067 goto err2;
3068 }
3069 qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
3070
Dupuis, Chad61d86582017-02-15 06:28:23 -08003071 /* Record BDQ producer doorbell addresses */
3072 qedf->bdq_primary_prod = qedf->dev_info.primary_dbq_rq_addr;
3073 qedf->bdq_secondary_prod = qedf->dev_info.secondary_bdq_rq_addr;
3074 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3075 "BDQ primary_prod=%p secondary_prod=%p.\n", qedf->bdq_primary_prod,
3076 qedf->bdq_secondary_prod);
3077
3078 qed_ops->register_ops(qedf->cdev, &qedf_cb_ops, qedf);
3079
3080 rc = qedf_prepare_sb(qedf);
3081 if (rc) {
3082
3083 QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
3084 goto err2;
3085 }
3086
3087 /* Start the Slowpath-process */
3088 slowpath_params.int_mode = QED_INT_MODE_MSIX;
3089 slowpath_params.drv_major = QEDF_DRIVER_MAJOR_VER;
3090 slowpath_params.drv_minor = QEDF_DRIVER_MINOR_VER;
3091 slowpath_params.drv_rev = QEDF_DRIVER_REV_VER;
3092 slowpath_params.drv_eng = QEDF_DRIVER_ENG_VER;
Kees Cookcd228742017-05-05 15:42:55 -07003093 strncpy(slowpath_params.name, "qedf", QED_DRV_VER_STR_SIZE);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003094 rc = qed_ops->common->slowpath_start(qedf->cdev, &slowpath_params);
3095 if (rc) {
3096 QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
3097 goto err2;
3098 }
3099
3100 /*
3101 * update_pf_params needs to be called before and after slowpath
3102 * start
3103 */
3104 qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
3105
3106 /* Setup interrupts */
3107 rc = qedf_setup_int(qedf);
3108 if (rc)
3109 goto err3;
3110
3111 rc = qed_ops->start(qedf->cdev, &qedf->tasks);
3112 if (rc) {
3113 QEDF_ERR(&(qedf->dbg_ctx), "Cannot start FCoE function.\n");
3114 goto err4;
3115 }
3116 task_start = qedf_get_task_mem(&qedf->tasks, 0);
3117 task_end = qedf_get_task_mem(&qedf->tasks, MAX_TID_BLOCKS_FCOE - 1);
3118 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Task context start=%p, "
3119 "end=%p block_size=%u.\n", task_start, task_end,
3120 qedf->tasks.size);
3121
3122 /*
3123 * We need to write the number of BDs in the BDQ we've preallocated so
3124 * the f/w will do a prefetch and we'll get an unsolicited CQE when a
3125 * packet arrives.
3126 */
3127 qedf->bdq_prod_idx = QEDF_BDQ_SIZE;
3128 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3129 "Writing %d to primary and secondary BDQ doorbell registers.\n",
3130 qedf->bdq_prod_idx);
3131 writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
3132 tmp = readw(qedf->bdq_primary_prod);
3133 writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
3134 tmp = readw(qedf->bdq_secondary_prod);
3135
3136 qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
3137
3138 /* Now that the dev_info struct has been filled in set the MAC
3139 * address
3140 */
3141 ether_addr_copy(qedf->mac, qedf->dev_info.common.hw_mac);
3142 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "MAC address is %pM.\n",
3143 qedf->mac);
3144
Chad Dupuis01fd76a2017-08-15 10:08:16 -07003145 /*
3146 * Set the WWNN and WWPN in the following way:
3147 *
3148 * If the info we get from qed is non-zero then use that to set the
3149 * WWPN and WWNN. Otherwise fall back to use fcoe_wwn_from_mac() based
3150 * on the MAC address.
3151 */
3152 if (qedf->dev_info.wwnn != 0 && qedf->dev_info.wwpn != 0) {
3153 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3154 "Setting WWPN and WWNN from qed dev_info.\n");
3155 qedf->wwnn = qedf->dev_info.wwnn;
3156 qedf->wwpn = qedf->dev_info.wwpn;
3157 } else {
3158 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3159 "Setting WWPN and WWNN using fcoe_wwn_from_mac().\n");
3160 qedf->wwnn = fcoe_wwn_from_mac(qedf->mac, 1, 0);
3161 qedf->wwpn = fcoe_wwn_from_mac(qedf->mac, 2, 0);
3162 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08003163 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "WWNN=%016llx "
3164 "WWPN=%016llx.\n", qedf->wwnn, qedf->wwpn);
3165
3166 sprintf(host_buf, "host_%d", host->host_no);
Mintz, Yuval712c3cb2017-05-23 09:41:28 +03003167 qed_ops->common->set_name(qedf->cdev, host_buf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003168
3169
3170 /* Set xid max values */
3171 qedf->max_scsi_xid = QEDF_MAX_SCSI_XID;
3172 qedf->max_els_xid = QEDF_MAX_ELS_XID;
3173
3174 /* Allocate cmd mgr */
3175 qedf->cmd_mgr = qedf_cmd_mgr_alloc(qedf);
3176 if (!qedf->cmd_mgr) {
3177 QEDF_ERR(&(qedf->dbg_ctx), "Failed to allocate cmd mgr.\n");
Wei Yongjune89cabf2018-01-17 12:42:41 +00003178 rc = -ENOMEM;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003179 goto err5;
3180 }
3181
3182 if (mode != QEDF_MODE_RECOVERY) {
3183 host->transportt = qedf_fc_transport_template;
3184 host->can_queue = QEDF_MAX_ELS_XID;
3185 host->max_lun = qedf_max_lun;
3186 host->max_cmd_len = QEDF_MAX_CDB_LEN;
3187 rc = scsi_add_host(host, &pdev->dev);
3188 if (rc)
3189 goto err6;
3190 }
3191
3192 memset(&params, 0, sizeof(params));
3193 params.mtu = 9000;
3194 ether_addr_copy(params.ll2_mac_address, qedf->mac);
3195
3196 /* Start LL2 processing thread */
3197 snprintf(host_buf, 20, "qedf_%d_ll2", host->host_no);
3198 qedf->ll2_recv_wq =
Chad Dupuis428ca642017-08-15 10:08:19 -07003199 create_workqueue(host_buf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003200 if (!qedf->ll2_recv_wq) {
3201 QEDF_ERR(&(qedf->dbg_ctx), "Failed to LL2 workqueue.\n");
Wei Yongjune89cabf2018-01-17 12:42:41 +00003202 rc = -ENOMEM;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003203 goto err7;
3204 }
3205
3206#ifdef CONFIG_DEBUG_FS
Arnd Bergmannd9ea4632018-02-02 14:12:18 +01003207 qedf_dbg_host_init(&(qedf->dbg_ctx), qedf_debugfs_ops,
3208 qedf_dbg_fops);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003209#endif
3210
3211 /* Start LL2 */
3212 qed_ops->ll2->register_cb_ops(qedf->cdev, &qedf_ll2_cb_ops, qedf);
3213 rc = qed_ops->ll2->start(qedf->cdev, &params);
3214 if (rc) {
3215 QEDF_ERR(&(qedf->dbg_ctx), "Could not start Light L2.\n");
3216 goto err7;
3217 }
3218 set_bit(QEDF_LL2_STARTED, &qedf->flags);
3219
Chad Dupuiscf291162017-08-15 10:08:18 -07003220 /* Set initial FIP/FCoE VLAN to NULL */
Dupuis, Chad61d86582017-02-15 06:28:23 -08003221 qedf->vlan_id = 0;
3222
3223 /*
3224 * No need to setup fcoe_ctlr or fc_lport objects during recovery since
3225 * they were not reaped during the unload process.
3226 */
3227 if (mode != QEDF_MODE_RECOVERY) {
3228 /* Setup imbedded fcoe controller */
3229 qedf_fcoe_ctlr_setup(qedf);
3230
3231 /* Setup lport */
3232 rc = qedf_lport_setup(qedf);
3233 if (rc) {
3234 QEDF_ERR(&(qedf->dbg_ctx),
3235 "qedf_lport_setup failed.\n");
3236 goto err7;
3237 }
3238 }
3239
3240 sprintf(host_buf, "qedf_%u_timer", qedf->lport->host->host_no);
3241 qedf->timer_work_queue =
Chad Dupuis428ca642017-08-15 10:08:19 -07003242 create_workqueue(host_buf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003243 if (!qedf->timer_work_queue) {
3244 QEDF_ERR(&(qedf->dbg_ctx), "Failed to start timer "
3245 "workqueue.\n");
Wei Yongjune89cabf2018-01-17 12:42:41 +00003246 rc = -ENOMEM;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003247 goto err7;
3248 }
3249
3250 /* DPC workqueue is not reaped during recovery unload */
3251 if (mode != QEDF_MODE_RECOVERY) {
3252 sprintf(host_buf, "qedf_%u_dpc",
3253 qedf->lport->host->host_no);
Chad Dupuis428ca642017-08-15 10:08:19 -07003254 qedf->dpc_wq = create_workqueue(host_buf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003255 }
3256
3257 /*
3258 * GRC dump and sysfs parameters are not reaped during the recovery
3259 * unload process.
3260 */
3261 if (mode != QEDF_MODE_RECOVERY) {
Chad Dupuis4b9b7fa2018-04-25 06:08:58 -07003262 qedf->grcdump_size =
3263 qed_ops->common->dbg_all_data_size(qedf->cdev);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003264 if (qedf->grcdump_size) {
3265 rc = qedf_alloc_grc_dump_buf(&qedf->grcdump,
3266 qedf->grcdump_size);
3267 if (rc) {
3268 QEDF_ERR(&(qedf->dbg_ctx),
3269 "GRC Dump buffer alloc failed.\n");
3270 qedf->grcdump = NULL;
3271 }
3272
3273 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3274 "grcdump: addr=%p, size=%u.\n",
3275 qedf->grcdump, qedf->grcdump_size);
3276 }
3277 qedf_create_sysfs_ctx_attr(qedf);
3278
3279 /* Initialize I/O tracing for this adapter */
3280 spin_lock_init(&qedf->io_trace_lock);
3281 qedf->io_trace_idx = 0;
3282 }
3283
3284 init_completion(&qedf->flogi_compl);
3285
3286 memset(&link_params, 0, sizeof(struct qed_link_params));
3287 link_params.link_up = true;
3288 status = qed_ops->common->set_link(qedf->cdev, &link_params);
3289 if (status)
3290 QEDF_WARN(&(qedf->dbg_ctx), "set_link failed.\n");
3291
3292 /* Start/restart discovery */
3293 if (mode == QEDF_MODE_RECOVERY)
3294 fcoe_ctlr_link_up(&qedf->ctlr);
3295 else
3296 fc_fabric_login(lport);
3297
3298 /* All good */
3299 return 0;
3300
3301err7:
3302 if (qedf->ll2_recv_wq)
3303 destroy_workqueue(qedf->ll2_recv_wq);
3304 fc_remove_host(qedf->lport->host);
3305 scsi_remove_host(qedf->lport->host);
3306#ifdef CONFIG_DEBUG_FS
3307 qedf_dbg_host_exit(&(qedf->dbg_ctx));
3308#endif
3309err6:
3310 qedf_cmd_mgr_free(qedf->cmd_mgr);
3311err5:
3312 qed_ops->stop(qedf->cdev);
3313err4:
3314 qedf_free_fcoe_pf_param(qedf);
3315 qedf_sync_free_irqs(qedf);
3316err3:
3317 qed_ops->common->slowpath_stop(qedf->cdev);
3318err2:
3319 qed_ops->common->remove(qedf->cdev);
3320err1:
3321 scsi_host_put(lport->host);
3322err0:
3323 return rc;
3324}
3325
3326static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3327{
3328 return __qedf_probe(pdev, QEDF_MODE_NORMAL);
3329}
3330
3331static void __qedf_remove(struct pci_dev *pdev, int mode)
3332{
3333 struct qedf_ctx *qedf;
3334
3335 if (!pdev) {
3336 QEDF_ERR(NULL, "pdev is NULL.\n");
3337 return;
3338 }
3339
3340 qedf = pci_get_drvdata(pdev);
3341
3342 /*
3343 * Prevent race where we're in board disable work and then try to
3344 * rmmod the module.
3345 */
3346 if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
3347 QEDF_ERR(&qedf->dbg_ctx, "Already removing PCI function.\n");
3348 return;
3349 }
3350
3351 if (mode != QEDF_MODE_RECOVERY)
3352 set_bit(QEDF_UNLOADING, &qedf->flags);
3353
3354 /* Logoff the fabric to upload all connections */
3355 if (mode == QEDF_MODE_RECOVERY)
3356 fcoe_ctlr_link_down(&qedf->ctlr);
3357 else
3358 fc_fabric_logoff(qedf->lport);
3359 qedf_wait_for_upload(qedf);
3360
3361#ifdef CONFIG_DEBUG_FS
3362 qedf_dbg_host_exit(&(qedf->dbg_ctx));
3363#endif
3364
3365 /* Stop any link update handling */
3366 cancel_delayed_work_sync(&qedf->link_update);
3367 destroy_workqueue(qedf->link_update_wq);
3368 qedf->link_update_wq = NULL;
3369
3370 if (qedf->timer_work_queue)
3371 destroy_workqueue(qedf->timer_work_queue);
3372
3373 /* Stop Light L2 */
3374 clear_bit(QEDF_LL2_STARTED, &qedf->flags);
3375 qed_ops->ll2->stop(qedf->cdev);
3376 if (qedf->ll2_recv_wq)
3377 destroy_workqueue(qedf->ll2_recv_wq);
3378
3379 /* Stop fastpath */
3380 qedf_sync_free_irqs(qedf);
3381 qedf_destroy_sb(qedf);
3382
3383 /*
3384 * During recovery don't destroy OS constructs that represent the
3385 * physical port.
3386 */
3387 if (mode != QEDF_MODE_RECOVERY) {
3388 qedf_free_grc_dump_buf(&qedf->grcdump);
3389 qedf_remove_sysfs_ctx_attr(qedf);
3390
3391 /* Remove all SCSI/libfc/libfcoe structures */
3392 fcoe_ctlr_destroy(&qedf->ctlr);
3393 fc_lport_destroy(qedf->lport);
3394 fc_remove_host(qedf->lport->host);
3395 scsi_remove_host(qedf->lport->host);
3396 }
3397
3398 qedf_cmd_mgr_free(qedf->cmd_mgr);
3399
3400 if (mode != QEDF_MODE_RECOVERY) {
3401 fc_exch_mgr_free(qedf->lport);
3402 fc_lport_free_stats(qedf->lport);
3403
3404 /* Wait for all vports to be reaped */
3405 qedf_wait_for_vport_destroy(qedf);
3406 }
3407
3408 /*
3409 * Now that all connections have been uploaded we can stop the
3410 * rest of the qed operations
3411 */
3412 qed_ops->stop(qedf->cdev);
3413
3414 if (mode != QEDF_MODE_RECOVERY) {
3415 if (qedf->dpc_wq) {
3416 /* Stop general DPC handling */
3417 destroy_workqueue(qedf->dpc_wq);
3418 qedf->dpc_wq = NULL;
3419 }
3420 }
3421
3422 /* Final shutdown for the board */
3423 qedf_free_fcoe_pf_param(qedf);
3424 if (mode != QEDF_MODE_RECOVERY) {
3425 qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
3426 pci_set_drvdata(pdev, NULL);
3427 }
3428 qed_ops->common->slowpath_stop(qedf->cdev);
3429 qed_ops->common->remove(qedf->cdev);
3430
3431 mempool_destroy(qedf->io_mempool);
3432
3433 /* Only reap the Scsi_host on a real removal */
3434 if (mode != QEDF_MODE_RECOVERY)
3435 scsi_host_put(qedf->lport->host);
3436}
3437
3438static void qedf_remove(struct pci_dev *pdev)
3439{
3440 /* Check to make sure this function wasn't already disabled */
3441 if (!atomic_read(&pdev->enable_cnt))
3442 return;
3443
3444 __qedf_remove(pdev, QEDF_MODE_NORMAL);
3445}
3446
Chad Dupuis4b9b7fa2018-04-25 06:08:58 -07003447void qedf_wq_grcdump(struct work_struct *work)
3448{
3449 struct qedf_ctx *qedf =
3450 container_of(work, struct qedf_ctx, grcdump_work.work);
3451
3452 QEDF_ERR(&(qedf->dbg_ctx), "Collecting GRC dump.\n");
3453 qedf_capture_grc_dump(qedf);
3454}
3455
Dupuis, Chad61d86582017-02-15 06:28:23 -08003456/*
3457 * Module Init/Remove
3458 */
3459
3460static int __init qedf_init(void)
3461{
3462 int ret;
3463
3464 /* If debug=1 passed, set the default log mask */
3465 if (qedf_debug == QEDF_LOG_DEFAULT)
3466 qedf_debug = QEDF_DEFAULT_LOG_MASK;
3467
Chad Dupuis84b2ba62018-04-25 06:08:52 -07003468 /*
3469 * Check that default prio for FIP/FCoE traffic is between 0..7 if a
3470 * value has been set
3471 */
3472 if (qedf_default_prio > -1)
3473 if (qedf_default_prio > 7) {
3474 qedf_default_prio = QEDF_DEFAULT_PRIO;
3475 QEDF_ERR(NULL, "FCoE/FIP priority out of range, resetting to %d.\n",
3476 QEDF_DEFAULT_PRIO);
3477 }
Chad Dupuisa93755c2018-04-25 06:08:50 -07003478
Dupuis, Chad61d86582017-02-15 06:28:23 -08003479 /* Print driver banner */
3480 QEDF_INFO(NULL, QEDF_LOG_INFO, "%s v%s.\n", QEDF_DESCR,
3481 QEDF_VERSION);
3482
3483 /* Create kmem_cache for qedf_io_work structs */
3484 qedf_io_work_cache = kmem_cache_create("qedf_io_work_cache",
3485 sizeof(struct qedf_io_work), 0, SLAB_HWCACHE_ALIGN, NULL);
3486 if (qedf_io_work_cache == NULL) {
3487 QEDF_ERR(NULL, "qedf_io_work_cache is NULL.\n");
3488 goto err1;
3489 }
3490 QEDF_INFO(NULL, QEDF_LOG_DISC, "qedf_io_work_cache=%p.\n",
3491 qedf_io_work_cache);
3492
3493 qed_ops = qed_get_fcoe_ops();
3494 if (!qed_ops) {
3495 QEDF_ERR(NULL, "Failed to get qed fcoe operations\n");
3496 goto err1;
3497 }
3498
3499#ifdef CONFIG_DEBUG_FS
3500 qedf_dbg_init("qedf");
3501#endif
3502
3503 qedf_fc_transport_template =
3504 fc_attach_transport(&qedf_fc_transport_fn);
3505 if (!qedf_fc_transport_template) {
3506 QEDF_ERR(NULL, "Could not register with FC transport\n");
3507 goto err2;
3508 }
3509
3510 qedf_fc_vport_transport_template =
3511 fc_attach_transport(&qedf_fc_vport_transport_fn);
3512 if (!qedf_fc_vport_transport_template) {
3513 QEDF_ERR(NULL, "Could not register vport template with FC "
3514 "transport\n");
3515 goto err3;
3516 }
3517
3518 qedf_io_wq = create_workqueue("qedf_io_wq");
3519 if (!qedf_io_wq) {
3520 QEDF_ERR(NULL, "Could not create qedf_io_wq.\n");
3521 goto err4;
3522 }
3523
3524 qedf_cb_ops.get_login_failures = qedf_get_login_failures;
3525
3526 ret = pci_register_driver(&qedf_pci_driver);
3527 if (ret) {
3528 QEDF_ERR(NULL, "Failed to register driver\n");
3529 goto err5;
3530 }
3531
3532 return 0;
3533
3534err5:
3535 destroy_workqueue(qedf_io_wq);
3536err4:
3537 fc_release_transport(qedf_fc_vport_transport_template);
3538err3:
3539 fc_release_transport(qedf_fc_transport_template);
3540err2:
3541#ifdef CONFIG_DEBUG_FS
3542 qedf_dbg_exit();
3543#endif
3544 qed_put_fcoe_ops();
3545err1:
3546 return -EINVAL;
3547}
3548
3549static void __exit qedf_cleanup(void)
3550{
3551 pci_unregister_driver(&qedf_pci_driver);
3552
3553 destroy_workqueue(qedf_io_wq);
3554
3555 fc_release_transport(qedf_fc_vport_transport_template);
3556 fc_release_transport(qedf_fc_transport_template);
3557#ifdef CONFIG_DEBUG_FS
3558 qedf_dbg_exit();
3559#endif
3560 qed_put_fcoe_ops();
3561
3562 kmem_cache_destroy(qedf_io_work_cache);
3563}
3564
3565MODULE_LICENSE("GPL");
3566MODULE_DESCRIPTION("QLogic QEDF 25/40/50/100Gb FCoE Driver");
3567MODULE_AUTHOR("QLogic Corporation");
3568MODULE_VERSION(QEDF_VERSION);
3569module_init(qedf_init);
3570module_exit(qedf_cleanup);