blob: a527b9b1f9af60797cad44281ed563350fe65f1a [file] [log] [blame]
Dupuis, Chad61d86582017-02-15 06:28:23 -08001/*
2 * QLogic FCoE Offload Driver
Chad Dupuis5d1c8b52018-04-25 06:09:04 -07003 * Copyright (c) 2016-2018 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
Dupuis, Chad61d86582017-02-15 06:28:23 -0800127 while (qedf->fipvlan_retries--) {
Saurav Kashyap76dbf4f2019-03-26 00:38:42 -0700128 /* This is to catch if link goes down during fipvlan retries */
129 if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
130 QEDF_ERR(&qedf->dbg_ctx, "Link not up.\n");
131 return false;
132 }
133
Dupuis, Chad61d86582017-02-15 06:28:23 -0800134 if (qedf->vlan_id > 0)
135 return true;
Saurav Kashyap76dbf4f2019-03-26 00:38:42 -0700136
Dupuis, Chad61d86582017-02-15 06:28:23 -0800137 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
138 "Retry %d.\n", qedf->fipvlan_retries);
139 init_completion(&qedf->fipvlan_compl);
140 qedf_fcoe_send_vlan_req(qedf);
141 rc = wait_for_completion_timeout(&qedf->fipvlan_compl,
142 1 * HZ);
Saurav Kashyap76dbf4f2019-03-26 00:38:42 -0700143 if (rc > 0 &&
144 (atomic_read(&qedf->link_state) == QEDF_LINK_UP)) {
Dupuis, Chad61d86582017-02-15 06:28:23 -0800145 fcoe_ctlr_link_up(&qedf->ctlr);
146 return true;
147 }
148 }
149
150 return false;
151}
152
153static void qedf_handle_link_update(struct work_struct *work)
154{
155 struct qedf_ctx *qedf =
156 container_of(work, struct qedf_ctx, link_update.work);
157 int rc;
158
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700159 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC, "Entered. link_state=%d.\n",
160 atomic_read(&qedf->link_state));
Dupuis, Chad61d86582017-02-15 06:28:23 -0800161
162 if (atomic_read(&qedf->link_state) == QEDF_LINK_UP) {
163 rc = qedf_initiate_fipvlan_req(qedf);
164 if (rc)
165 return;
Saurav Kashyap76dbf4f2019-03-26 00:38:42 -0700166
167 if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
168 qedf->vlan_id = 0;
169 return;
170 }
171
Dupuis, Chad61d86582017-02-15 06:28:23 -0800172 /*
173 * If we get here then we never received a repsonse to our
174 * fip vlan request so set the vlan_id to the default and
175 * tell FCoE that the link is up
176 */
177 QEDF_WARN(&(qedf->dbg_ctx), "Did not receive FIP VLAN "
178 "response, falling back to default VLAN %d.\n",
179 qedf_fallback_vlan);
Chad Dupuiscf291162017-08-15 10:08:18 -0700180 qedf_set_vlan_id(qedf, qedf_fallback_vlan);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800181
182 /*
183 * Zero out data_src_addr so we'll update it with the new
184 * lport port_id
185 */
186 eth_zero_addr(qedf->data_src_addr);
187 fcoe_ctlr_link_up(&qedf->ctlr);
188 } else if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
189 /*
190 * If we hit here and link_down_tmo_valid is still 1 it means
191 * that link_down_tmo timed out so set it to 0 to make sure any
192 * other readers have accurate state.
193 */
194 atomic_set(&qedf->link_down_tmo_valid, 0);
195 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
196 "Calling fcoe_ctlr_link_down().\n");
197 fcoe_ctlr_link_down(&qedf->ctlr);
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700198 if (qedf_wait_for_upload(qedf) == false)
199 QEDF_ERR(&qedf->dbg_ctx,
200 "Could not upload all sessions.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -0800201 /* Reset the number of FIP VLAN retries */
202 qedf->fipvlan_retries = qedf_fipvlan_retries;
203 }
204}
205
Chad Dupuisa3cd42a92017-08-15 10:08:17 -0700206#define QEDF_FCOE_MAC_METHOD_GRANGED_MAC 1
207#define QEDF_FCOE_MAC_METHOD_FCF_MAP 2
208#define QEDF_FCOE_MAC_METHOD_FCOE_SET_MAC 3
209static void qedf_set_data_src_addr(struct qedf_ctx *qedf, struct fc_frame *fp)
210{
211 u8 *granted_mac;
212 struct fc_frame_header *fh = fc_frame_header_get(fp);
213 u8 fc_map[3];
214 int method = 0;
215
216 /* Get granted MAC address from FIP FLOGI payload */
217 granted_mac = fr_cb(fp)->granted_mac;
218
219 /*
220 * We set the source MAC for FCoE traffic based on the Granted MAC
221 * address from the switch.
222 *
223 * If granted_mac is non-zero, we used that.
224 * If the granted_mac is zeroed out, created the FCoE MAC based on
225 * the sel_fcf->fc_map and the d_id fo the FLOGI frame.
226 * If sel_fcf->fc_map is 0 then we use the default FCF-MAC plus the
227 * d_id of the FLOGI frame.
228 */
229 if (!is_zero_ether_addr(granted_mac)) {
230 ether_addr_copy(qedf->data_src_addr, granted_mac);
231 method = QEDF_FCOE_MAC_METHOD_GRANGED_MAC;
232 } else if (qedf->ctlr.sel_fcf->fc_map != 0) {
233 hton24(fc_map, qedf->ctlr.sel_fcf->fc_map);
234 qedf->data_src_addr[0] = fc_map[0];
235 qedf->data_src_addr[1] = fc_map[1];
236 qedf->data_src_addr[2] = fc_map[2];
237 qedf->data_src_addr[3] = fh->fh_d_id[0];
238 qedf->data_src_addr[4] = fh->fh_d_id[1];
239 qedf->data_src_addr[5] = fh->fh_d_id[2];
240 method = QEDF_FCOE_MAC_METHOD_FCF_MAP;
241 } else {
242 fc_fcoe_set_mac(qedf->data_src_addr, fh->fh_d_id);
243 method = QEDF_FCOE_MAC_METHOD_FCOE_SET_MAC;
244 }
245
246 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
247 "QEDF data_src_mac=%pM method=%d.\n", qedf->data_src_addr, method);
248}
249
Dupuis, Chad61d86582017-02-15 06:28:23 -0800250static void qedf_flogi_resp(struct fc_seq *seq, struct fc_frame *fp,
251 void *arg)
252{
253 struct fc_exch *exch = fc_seq_exch(seq);
254 struct fc_lport *lport = exch->lp;
255 struct qedf_ctx *qedf = lport_priv(lport);
256
257 if (!qedf) {
258 QEDF_ERR(NULL, "qedf is NULL.\n");
259 return;
260 }
261
262 /*
263 * If ERR_PTR is set then don't try to stat anything as it will cause
264 * a crash when we access fp.
265 */
266 if (IS_ERR(fp)) {
267 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
268 "fp has IS_ERR() set.\n");
269 goto skip_stat;
270 }
271
272 /* Log stats for FLOGI reject */
273 if (fc_frame_payload_op(fp) == ELS_LS_RJT)
274 qedf->flogi_failed++;
Chad Dupuisa3cd42a92017-08-15 10:08:17 -0700275 else if (fc_frame_payload_op(fp) == ELS_LS_ACC) {
276 /* Set the source MAC we will use for FCoE traffic */
277 qedf_set_data_src_addr(qedf, fp);
278 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800279
280 /* Complete flogi_compl so we can proceed to sending ADISCs */
281 complete(&qedf->flogi_compl);
282
283skip_stat:
284 /* Report response to libfc */
285 fc_lport_flogi_resp(seq, fp, lport);
286}
287
288static struct fc_seq *qedf_elsct_send(struct fc_lport *lport, u32 did,
289 struct fc_frame *fp, unsigned int op,
290 void (*resp)(struct fc_seq *,
291 struct fc_frame *,
292 void *),
293 void *arg, u32 timeout)
294{
295 struct qedf_ctx *qedf = lport_priv(lport);
296
297 /*
298 * Intercept FLOGI for statistic purposes. Note we use the resp
299 * callback to tell if this is really a flogi.
300 */
301 if (resp == fc_lport_flogi_resp) {
302 qedf->flogi_cnt++;
303 return fc_elsct_send(lport, did, fp, op, qedf_flogi_resp,
304 arg, timeout);
305 }
306
307 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
308}
309
310int qedf_send_flogi(struct qedf_ctx *qedf)
311{
312 struct fc_lport *lport;
313 struct fc_frame *fp;
314
315 lport = qedf->lport;
316
317 if (!lport->tt.elsct_send)
318 return -EINVAL;
319
320 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
321 if (!fp) {
322 QEDF_ERR(&(qedf->dbg_ctx), "fc_frame_alloc failed.\n");
323 return -ENOMEM;
324 }
325
326 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
327 "Sending FLOGI to reestablish session with switch.\n");
328 lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
329 ELS_FLOGI, qedf_flogi_resp, lport, lport->r_a_tov);
330
331 init_completion(&qedf->flogi_compl);
332
333 return 0;
334}
335
336struct qedf_tmp_rdata_item {
337 struct fc_rport_priv *rdata;
338 struct list_head list;
339};
340
341/*
342 * This function is called if link_down_tmo is in use. If we get a link up and
343 * link_down_tmo has not expired then use just FLOGI/ADISC to recover our
344 * sessions with targets. Otherwise, just call fcoe_ctlr_link_up().
345 */
346static void qedf_link_recovery(struct work_struct *work)
347{
348 struct qedf_ctx *qedf =
349 container_of(work, struct qedf_ctx, link_recovery.work);
350 struct qedf_rport *fcport;
351 struct fc_rport_priv *rdata;
352 struct qedf_tmp_rdata_item *rdata_item, *tmp_rdata_item;
353 bool rc;
354 int retries = 30;
355 int rval, i;
356 struct list_head rdata_login_list;
357
358 INIT_LIST_HEAD(&rdata_login_list);
359
360 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
361 "Link down tmo did not expire.\n");
362
363 /*
364 * Essentially reset the fcoe_ctlr here without affecting the state
365 * of the libfc structs.
366 */
367 qedf->ctlr.state = FIP_ST_LINK_WAIT;
368 fcoe_ctlr_link_down(&qedf->ctlr);
369
370 /*
371 * Bring the link up before we send the fipvlan request so libfcoe
372 * can select a new fcf in parallel
373 */
374 fcoe_ctlr_link_up(&qedf->ctlr);
375
376 /* Since the link when down and up to verify which vlan we're on */
377 qedf->fipvlan_retries = qedf_fipvlan_retries;
378 rc = qedf_initiate_fipvlan_req(qedf);
Chad Dupuiscf291162017-08-15 10:08:18 -0700379 /* If getting the VLAN fails, set the VLAN to the fallback one */
Dupuis, Chad61d86582017-02-15 06:28:23 -0800380 if (!rc)
Chad Dupuiscf291162017-08-15 10:08:18 -0700381 qedf_set_vlan_id(qedf, qedf_fallback_vlan);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800382
383 /*
384 * We need to wait for an FCF to be selected due to the
385 * fcoe_ctlr_link_up other the FLOGI will be rejected.
386 */
387 while (retries > 0) {
388 if (qedf->ctlr.sel_fcf) {
389 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
390 "FCF reselected, proceeding with FLOGI.\n");
391 break;
392 }
393 msleep(500);
394 retries--;
395 }
396
397 if (retries < 1) {
398 QEDF_ERR(&(qedf->dbg_ctx), "Exhausted retries waiting for "
399 "FCF selection.\n");
400 return;
401 }
402
403 rval = qedf_send_flogi(qedf);
404 if (rval)
405 return;
406
407 /* Wait for FLOGI completion before proceeding with sending ADISCs */
408 i = wait_for_completion_timeout(&qedf->flogi_compl,
409 qedf->lport->r_a_tov);
410 if (i == 0) {
411 QEDF_ERR(&(qedf->dbg_ctx), "FLOGI timed out.\n");
412 return;
413 }
414
415 /*
416 * Call lport->tt.rport_login which will cause libfc to send an
417 * ADISC since the rport is in state ready.
418 */
419 rcu_read_lock();
420 list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
421 rdata = fcport->rdata;
422 if (rdata == NULL)
423 continue;
424 rdata_item = kzalloc(sizeof(struct qedf_tmp_rdata_item),
425 GFP_ATOMIC);
426 if (!rdata_item)
427 continue;
428 if (kref_get_unless_zero(&rdata->kref)) {
429 rdata_item->rdata = rdata;
430 list_add(&rdata_item->list, &rdata_login_list);
431 } else
432 kfree(rdata_item);
433 }
434 rcu_read_unlock();
435 /*
436 * Do the fc_rport_login outside of the rcu lock so we don't take a
437 * mutex in an atomic context.
438 */
439 list_for_each_entry_safe(rdata_item, tmp_rdata_item, &rdata_login_list,
440 list) {
441 list_del(&rdata_item->list);
442 fc_rport_login(rdata_item->rdata);
443 kref_put(&rdata_item->rdata->kref, fc_rport_destroy);
444 kfree(rdata_item);
445 }
446}
447
448static void qedf_update_link_speed(struct qedf_ctx *qedf,
449 struct qed_link_output *link)
450{
451 struct fc_lport *lport = qedf->lport;
452
453 lport->link_speed = FC_PORTSPEED_UNKNOWN;
454 lport->link_supported_speeds = FC_PORTSPEED_UNKNOWN;
455
456 /* Set fc_host link speed */
457 switch (link->speed) {
458 case 10000:
459 lport->link_speed = FC_PORTSPEED_10GBIT;
460 break;
461 case 25000:
462 lport->link_speed = FC_PORTSPEED_25GBIT;
463 break;
464 case 40000:
465 lport->link_speed = FC_PORTSPEED_40GBIT;
466 break;
467 case 50000:
468 lport->link_speed = FC_PORTSPEED_50GBIT;
469 break;
470 case 100000:
471 lport->link_speed = FC_PORTSPEED_100GBIT;
472 break;
473 default:
474 lport->link_speed = FC_PORTSPEED_UNKNOWN;
475 break;
476 }
477
478 /*
479 * Set supported link speed by querying the supported
480 * capabilities of the link.
481 */
482 if (link->supported_caps & SUPPORTED_10000baseKR_Full)
483 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
484 if (link->supported_caps & SUPPORTED_25000baseKR_Full)
485 lport->link_supported_speeds |= FC_PORTSPEED_25GBIT;
486 if (link->supported_caps & SUPPORTED_40000baseLR4_Full)
487 lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
488 if (link->supported_caps & SUPPORTED_50000baseKR2_Full)
489 lport->link_supported_speeds |= FC_PORTSPEED_50GBIT;
490 if (link->supported_caps & SUPPORTED_100000baseKR4_Full)
491 lport->link_supported_speeds |= FC_PORTSPEED_100GBIT;
492 fc_host_supported_speeds(lport->host) = lport->link_supported_speeds;
493}
494
495static void qedf_link_update(void *dev, struct qed_link_output *link)
496{
497 struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
498
499 if (link->link_up) {
Chad Dupuis3f9de7f2018-04-25 06:08:55 -0700500 if (atomic_read(&qedf->link_state) == QEDF_LINK_UP) {
501 QEDF_INFO((&qedf->dbg_ctx), QEDF_LOG_DISC,
502 "Ignoring link up event as link is already up.\n");
503 return;
504 }
Dupuis, Chad61d86582017-02-15 06:28:23 -0800505 QEDF_ERR(&(qedf->dbg_ctx), "LINK UP (%d GB/s).\n",
506 link->speed / 1000);
507
508 /* Cancel any pending link down work */
509 cancel_delayed_work(&qedf->link_update);
510
511 atomic_set(&qedf->link_state, QEDF_LINK_UP);
512 qedf_update_link_speed(qedf, link);
513
Chad Dupuisba17d372018-04-25 06:08:51 -0700514 if (atomic_read(&qedf->dcbx) == QEDF_DCBX_DONE ||
515 qedf_dcbx_no_wait) {
Chad Dupuisf7e8d572017-05-31 06:33:59 -0700516 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
517 "DCBx done.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -0800518 if (atomic_read(&qedf->link_down_tmo_valid) > 0)
519 queue_delayed_work(qedf->link_update_wq,
520 &qedf->link_recovery, 0);
521 else
522 queue_delayed_work(qedf->link_update_wq,
523 &qedf->link_update, 0);
524 atomic_set(&qedf->link_down_tmo_valid, 0);
525 }
526
527 } else {
528 QEDF_ERR(&(qedf->dbg_ctx), "LINK DOWN.\n");
529
530 atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
531 atomic_set(&qedf->dcbx, QEDF_DCBX_PENDING);
532 /*
533 * Flag that we're waiting for the link to come back up before
534 * informing the fcoe layer of the event.
535 */
536 if (qedf_link_down_tmo > 0) {
537 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
538 "Starting link down tmo.\n");
539 atomic_set(&qedf->link_down_tmo_valid, 1);
540 }
Chad Dupuis84b2ba62018-04-25 06:08:52 -0700541 qedf->vlan_id = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800542 qedf_update_link_speed(qedf, link);
543 queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
544 qedf_link_down_tmo * HZ);
545 }
546}
547
548
549static void qedf_dcbx_handler(void *dev, struct qed_dcbx_get *get, u32 mib_type)
550{
551 struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
Chad Dupuis84b2ba62018-04-25 06:08:52 -0700552 u8 tmp_prio;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800553
554 QEDF_ERR(&(qedf->dbg_ctx), "DCBx event valid=%d enabled=%d fcoe "
555 "prio=%d.\n", get->operational.valid, get->operational.enabled,
556 get->operational.app_prio.fcoe);
557
558 if (get->operational.enabled && get->operational.valid) {
559 /* If DCBX was already negotiated on link up then just exit */
560 if (atomic_read(&qedf->dcbx) == QEDF_DCBX_DONE) {
561 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
562 "DCBX already set on link up.\n");
563 return;
564 }
565
566 atomic_set(&qedf->dcbx, QEDF_DCBX_DONE);
567
Chad Dupuis84b2ba62018-04-25 06:08:52 -0700568 /*
569 * Set the 8021q priority in the following manner:
570 *
571 * 1. If a modparam is set use that
572 * 2. If the value is not between 0..7 use the default
573 * 3. Use the priority we get from the DCBX app tag
574 */
575 tmp_prio = get->operational.app_prio.fcoe;
576 if (qedf_default_prio > -1)
577 qedf->prio = qedf_default_prio;
578 else if (tmp_prio < 0 || tmp_prio > 7) {
579 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
580 "FIP/FCoE prio %d out of range, setting to %d.\n",
581 tmp_prio, QEDF_DEFAULT_PRIO);
582 qedf->prio = QEDF_DEFAULT_PRIO;
583 } else
584 qedf->prio = tmp_prio;
585
Chad Dupuisba17d372018-04-25 06:08:51 -0700586 if (atomic_read(&qedf->link_state) == QEDF_LINK_UP &&
587 !qedf_dcbx_no_wait) {
Dupuis, Chad61d86582017-02-15 06:28:23 -0800588 if (atomic_read(&qedf->link_down_tmo_valid) > 0)
589 queue_delayed_work(qedf->link_update_wq,
590 &qedf->link_recovery, 0);
591 else
592 queue_delayed_work(qedf->link_update_wq,
593 &qedf->link_update, 0);
594 atomic_set(&qedf->link_down_tmo_valid, 0);
595 }
596 }
597
598}
599
600static u32 qedf_get_login_failures(void *cookie)
601{
602 struct qedf_ctx *qedf;
603
604 qedf = (struct qedf_ctx *)cookie;
605 return qedf->flogi_failed;
606}
607
608static struct qed_fcoe_cb_ops qedf_cb_ops = {
609 {
610 .link_update = qedf_link_update,
611 .dcbx_aen = qedf_dcbx_handler,
Chad Dupuis8673daf2018-05-22 00:28:44 -0700612 .get_generic_tlv_data = qedf_get_generic_tlv_data,
Chad Dupuis642a0b32018-05-22 00:28:43 -0700613 .get_protocol_tlv_data = qedf_get_protocol_tlv_data,
Dupuis, Chad61d86582017-02-15 06:28:23 -0800614 }
615};
616
617/*
618 * Various transport templates.
619 */
620
621static struct scsi_transport_template *qedf_fc_transport_template;
622static struct scsi_transport_template *qedf_fc_vport_transport_template;
623
624/*
625 * SCSI EH handlers
626 */
627static int qedf_eh_abort(struct scsi_cmnd *sc_cmd)
628{
629 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
Dupuis, Chad61d86582017-02-15 06:28:23 -0800630 struct fc_lport *lport;
631 struct qedf_ctx *qedf;
632 struct qedf_ioreq *io_req;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700633 struct fc_rport_libfc_priv *rp = rport->dd_data;
634 struct fc_rport_priv *rdata;
635 struct qedf_rport *fcport = NULL;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800636 int rc = FAILED;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700637 int wait_count = 100;
638 int refcount = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800639 int rval;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700640 int got_ref = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800641
642 lport = shost_priv(sc_cmd->device->host);
643 qedf = (struct qedf_ctx *)lport_priv(lport);
644
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700645 /* rport and tgt are allocated together, so tgt should be non-NULL */
646 fcport = (struct qedf_rport *)&rp[1];
647 rdata = fcport->rdata;
648 if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
649 QEDF_ERR(&qedf->dbg_ctx, "stale rport, sc_cmd=%p\n", sc_cmd);
650 rc = 1;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800651 goto out;
652 }
653
Dupuis, Chad61d86582017-02-15 06:28:23 -0800654
655 io_req = (struct qedf_ioreq *)sc_cmd->SCp.ptr;
656 if (!io_req) {
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700657 QEDF_ERR(&qedf->dbg_ctx,
658 "sc_cmd not queued with lld, sc_cmd=%p op=0x%02x, port_id=%06x\n",
659 sc_cmd, sc_cmd->cmnd[0],
660 rdata->ids.port_id);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800661 rc = SUCCESS;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700662 goto drop_rdata_kref;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800663 }
664
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700665 rval = kref_get_unless_zero(&io_req->refcount); /* ID: 005 */
666 if (rval)
667 got_ref = 1;
668
669 /* If we got a valid io_req, confirm it belongs to this sc_cmd. */
670 if (!rval || io_req->sc_cmd != sc_cmd) {
671 QEDF_ERR(&qedf->dbg_ctx,
672 "Freed/Incorrect io_req, io_req->sc_cmd=%p, sc_cmd=%p, port_id=%06x, bailing out.\n",
673 io_req->sc_cmd, sc_cmd, rdata->ids.port_id);
674
675 goto drop_rdata_kref;
676 }
677
678 if (fc_remote_port_chkready(rport)) {
679 refcount = kref_read(&io_req->refcount);
680 QEDF_ERR(&qedf->dbg_ctx,
681 "rport not ready, io_req=%p, xid=0x%x sc_cmd=%p op=0x%02x, refcount=%d, port_id=%06x\n",
682 io_req, io_req->xid, sc_cmd, sc_cmd->cmnd[0],
683 refcount, rdata->ids.port_id);
684
685 goto drop_rdata_kref;
686 }
687
688 rc = fc_block_scsi_eh(sc_cmd);
689 if (rc)
690 goto drop_rdata_kref;
691
692 if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
693 QEDF_ERR(&qedf->dbg_ctx,
694 "Connection uploading, xid=0x%x., port_id=%06x\n",
695 io_req->xid, rdata->ids.port_id);
696 while (io_req->sc_cmd && (wait_count != 0)) {
697 msleep(100);
698 wait_count--;
699 }
700 if (wait_count) {
701 QEDF_ERR(&qedf->dbg_ctx, "ABTS succeeded\n");
702 rc = SUCCESS;
703 } else {
704 QEDF_ERR(&qedf->dbg_ctx, "ABTS failed\n");
705 rc = FAILED;
706 }
707 goto drop_rdata_kref;
708 }
709
710 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
711 QEDF_ERR(&qedf->dbg_ctx, "link not ready.\n");
712 goto drop_rdata_kref;
713 }
714
715 QEDF_ERR(&qedf->dbg_ctx,
716 "Aborting io_req=%p sc_cmd=%p xid=0x%x fp_idx=%d, port_id=%06x.\n",
717 io_req, sc_cmd, io_req->xid, io_req->fp_idx,
718 rdata->ids.port_id);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800719
720 if (qedf->stop_io_on_error) {
721 qedf_stop_all_io(qedf);
722 rc = SUCCESS;
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700723 goto drop_rdata_kref;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800724 }
725
726 init_completion(&io_req->abts_done);
727 rval = qedf_initiate_abts(io_req, true);
728 if (rval) {
729 QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700730 /*
731 * If we fail to queue the ABTS then return this command to
732 * the SCSI layer as it will own and free the xid
733 */
734 rc = SUCCESS;
735 qedf_scsi_done(qedf, io_req, DID_ERROR);
736 goto drop_rdata_kref;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800737 }
738
739 wait_for_completion(&io_req->abts_done);
740
741 if (io_req->event == QEDF_IOREQ_EV_ABORT_SUCCESS ||
742 io_req->event == QEDF_IOREQ_EV_ABORT_FAILED ||
743 io_req->event == QEDF_IOREQ_EV_CLEANUP_SUCCESS) {
744 /*
745 * If we get a reponse to the abort this is success from
746 * the perspective that all references to the command have
747 * been removed from the driver and firmware
748 */
749 rc = SUCCESS;
750 } else {
751 /* If the abort and cleanup failed then return a failure */
752 rc = FAILED;
753 }
754
755 if (rc == SUCCESS)
756 QEDF_ERR(&(qedf->dbg_ctx), "ABTS succeeded, xid=0x%x.\n",
757 io_req->xid);
758 else
759 QEDF_ERR(&(qedf->dbg_ctx), "ABTS failed, xid=0x%x.\n",
760 io_req->xid);
761
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700762drop_rdata_kref:
763 kref_put(&rdata->kref, fc_rport_destroy);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800764out:
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700765 if (got_ref)
766 kref_put(&io_req->refcount, qedf_release_cmd);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800767 return rc;
768}
769
770static int qedf_eh_target_reset(struct scsi_cmnd *sc_cmd)
771{
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700772 QEDF_ERR(NULL, "%d:0:%d:%lld: TARGET RESET Issued...",
773 sc_cmd->device->host->host_no, sc_cmd->device->id,
774 sc_cmd->device->lun);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800775 return qedf_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
776}
777
778static int qedf_eh_device_reset(struct scsi_cmnd *sc_cmd)
779{
Saurav Kashyap69ef2c62019-03-26 00:38:38 -0700780 QEDF_ERR(NULL, "%d:0:%d:%lld: LUN RESET Issued... ",
781 sc_cmd->device->host->host_no, sc_cmd->device->id,
782 sc_cmd->device->lun);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800783 return qedf_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
784}
785
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700786bool qedf_wait_for_upload(struct qedf_ctx *qedf)
Dupuis, Chad61d86582017-02-15 06:28:23 -0800787{
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700788 struct qedf_rport *fcport = NULL;
789
Dupuis, Chad61d86582017-02-15 06:28:23 -0800790 while (1) {
791 if (atomic_read(&qedf->num_offloads))
792 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
793 "Waiting for all uploads to complete.\n");
794 else
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700795 return true;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800796 msleep(500);
797 }
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700798
799 rcu_read_lock();
800 list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
801 if (fcport && test_bit(QEDF_RPORT_SESSION_READY,
802 &fcport->flags)) {
803 if (fcport->rdata)
804 QEDF_ERR(&qedf->dbg_ctx,
805 "Waiting for fcport %p portid=%06x.\n",
806 fcport, fcport->rdata->ids.port_id);
807 } else {
808 QEDF_ERR(&qedf->dbg_ctx,
809 "Waiting for fcport %p.\n", fcport);
810 }
811 }
812 rcu_read_unlock();
813 return false;
814
Dupuis, Chad61d86582017-02-15 06:28:23 -0800815}
816
Chad Dupuis5cf446d2017-05-31 06:33:55 -0700817/* Performs soft reset of qedf_ctx by simulating a link down/up */
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700818void qedf_ctx_soft_reset(struct fc_lport *lport)
Dupuis, Chad61d86582017-02-15 06:28:23 -0800819{
Dupuis, Chad61d86582017-02-15 06:28:23 -0800820 struct qedf_ctx *qedf;
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700821 struct qed_link_output if_link;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800822
Dupuis, Chad61d86582017-02-15 06:28:23 -0800823 if (lport->vport) {
824 QEDF_ERR(NULL, "Cannot issue host reset on NPIV port.\n");
Chad Dupuis5cf446d2017-05-31 06:33:55 -0700825 return;
Dupuis, Chad61d86582017-02-15 06:28:23 -0800826 }
827
Chad Dupuis5cf446d2017-05-31 06:33:55 -0700828 qedf = lport_priv(lport);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800829
830 /* For host reset, essentially do a soft link up/down */
831 atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700832 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
833 "Queuing link down work.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -0800834 queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
835 0);
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700836
837 if (qedf_wait_for_upload(qedf) == false) {
838 QEDF_ERR(&qedf->dbg_ctx, "Could not upload all sessions.\n");
839 WARN_ON(atomic_read(&qedf->num_offloads));
840 }
841
842 /* Before setting link up query physical link state */
843 qed_ops->common->get_link(qedf->cdev, &if_link);
844 /* Bail if the physical link is not up */
845 if (!if_link.link_up) {
846 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
847 "Physical link is not up.\n");
848 return;
849 }
850 /* Flush and wait to make sure link down is processed */
851 flush_delayed_work(&qedf->link_update);
852 msleep(500);
853
Dupuis, Chad61d86582017-02-15 06:28:23 -0800854 atomic_set(&qedf->link_state, QEDF_LINK_UP);
855 qedf->vlan_id = 0;
Chad Dupuisa66c6cd22019-03-26 00:38:47 -0700856 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
857 "Queue link up work.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -0800858 queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
859 0);
Chad Dupuis5cf446d2017-05-31 06:33:55 -0700860}
861
862/* Reset the host by gracefully logging out and then logging back in */
863static int qedf_eh_host_reset(struct scsi_cmnd *sc_cmd)
864{
865 struct fc_lport *lport;
866 struct qedf_ctx *qedf;
867
868 lport = shost_priv(sc_cmd->device->host);
869 qedf = lport_priv(lport);
870
871 if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN ||
872 test_bit(QEDF_UNLOADING, &qedf->flags))
873 return FAILED;
874
875 QEDF_ERR(&(qedf->dbg_ctx), "HOST RESET Issued...");
876
877 qedf_ctx_soft_reset(lport);
Dupuis, Chad61d86582017-02-15 06:28:23 -0800878
879 return SUCCESS;
880}
881
882static int qedf_slave_configure(struct scsi_device *sdev)
883{
884 if (qedf_queue_depth) {
885 scsi_change_queue_depth(sdev, qedf_queue_depth);
886 }
887
888 return 0;
889}
890
891static struct scsi_host_template qedf_host_template = {
892 .module = THIS_MODULE,
893 .name = QEDF_MODULE_NAME,
894 .this_id = -1,
Chad Dupuisa7746f12017-05-31 06:34:00 -0700895 .cmd_per_lun = 32,
Dupuis, Chad61d86582017-02-15 06:28:23 -0800896 .max_sectors = 0xffff,
897 .queuecommand = qedf_queuecommand,
898 .shost_attrs = qedf_host_attrs,
899 .eh_abort_handler = qedf_eh_abort,
900 .eh_device_reset_handler = qedf_eh_device_reset, /* lun reset */
901 .eh_target_reset_handler = qedf_eh_target_reset, /* target reset */
902 .eh_host_reset_handler = qedf_eh_host_reset,
903 .slave_configure = qedf_slave_configure,
904 .dma_boundary = QED_HW_DMA_BOUNDARY,
905 .sg_tablesize = QEDF_MAX_BDS_PER_CMD,
906 .can_queue = FCOE_PARAMS_NUM_TASKS,
Chad Dupuis6088cfa2017-05-31 06:34:01 -0700907 .change_queue_depth = scsi_change_queue_depth,
Dupuis, Chad61d86582017-02-15 06:28:23 -0800908};
909
910static int qedf_get_paged_crc_eof(struct sk_buff *skb, int tlen)
911{
912 int rc;
913
914 spin_lock(&qedf_global_lock);
915 rc = fcoe_get_paged_crc_eof(skb, tlen, &qedf_global);
916 spin_unlock(&qedf_global_lock);
917
918 return rc;
919}
920
921static struct qedf_rport *qedf_fcport_lookup(struct qedf_ctx *qedf, u32 port_id)
922{
923 struct qedf_rport *fcport;
924 struct fc_rport_priv *rdata;
925
926 rcu_read_lock();
927 list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
928 rdata = fcport->rdata;
929 if (rdata == NULL)
930 continue;
931 if (rdata->ids.port_id == port_id) {
932 rcu_read_unlock();
933 return fcport;
934 }
935 }
936 rcu_read_unlock();
937
938 /* Return NULL to caller to let them know fcport was not found */
939 return NULL;
940}
941
942/* Transmits an ELS frame over an offloaded session */
943static int qedf_xmit_l2_frame(struct qedf_rport *fcport, struct fc_frame *fp)
944{
945 struct fc_frame_header *fh;
946 int rc = 0;
947
948 fh = fc_frame_header_get(fp);
949 if ((fh->fh_type == FC_TYPE_ELS) &&
950 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
951 switch (fc_frame_payload_op(fp)) {
952 case ELS_ADISC:
953 qedf_send_adisc(fcport, fp);
954 rc = 1;
955 break;
956 }
957 }
958
959 return rc;
960}
961
962/**
963 * qedf_xmit - qedf FCoE frame transmit function
964 *
965 */
966static int qedf_xmit(struct fc_lport *lport, struct fc_frame *fp)
967{
968 struct fc_lport *base_lport;
969 struct qedf_ctx *qedf;
970 struct ethhdr *eh;
971 struct fcoe_crc_eof *cp;
972 struct sk_buff *skb;
973 struct fc_frame_header *fh;
974 struct fcoe_hdr *hp;
975 u8 sof, eof;
976 u32 crc;
977 unsigned int hlen, tlen, elen;
978 int wlen;
979 struct fc_stats *stats;
980 struct fc_lport *tmp_lport;
981 struct fc_lport *vn_port = NULL;
982 struct qedf_rport *fcport;
983 int rc;
984 u16 vlan_tci = 0;
985
986 qedf = (struct qedf_ctx *)lport_priv(lport);
987
988 fh = fc_frame_header_get(fp);
989 skb = fp_skb(fp);
990
991 /* Filter out traffic to other NPIV ports on the same host */
992 if (lport->vport)
993 base_lport = shost_priv(vport_to_shost(lport->vport));
994 else
995 base_lport = lport;
996
997 /* Flag if the destination is the base port */
998 if (base_lport->port_id == ntoh24(fh->fh_d_id)) {
999 vn_port = base_lport;
1000 } else {
1001 /* Got through the list of vports attached to the base_lport
1002 * and see if we have a match with the destination address.
1003 */
1004 list_for_each_entry(tmp_lport, &base_lport->vports, list) {
1005 if (tmp_lport->port_id == ntoh24(fh->fh_d_id)) {
1006 vn_port = tmp_lport;
1007 break;
1008 }
1009 }
1010 }
1011 if (vn_port && ntoh24(fh->fh_d_id) != FC_FID_FLOGI) {
1012 struct fc_rport_priv *rdata = NULL;
1013
1014 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
1015 "Dropping FCoE frame to %06x.\n", ntoh24(fh->fh_d_id));
1016 kfree_skb(skb);
1017 rdata = fc_rport_lookup(lport, ntoh24(fh->fh_d_id));
Hannes Reinecke4262d352019-03-26 00:38:43 -07001018 if (rdata) {
Dupuis, Chad61d86582017-02-15 06:28:23 -08001019 rdata->retries = lport->max_rport_retry_count;
Hannes Reinecke4262d352019-03-26 00:38:43 -07001020 kref_put(&rdata->kref, fc_rport_destroy);
1021 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001022 return -EINVAL;
1023 }
1024 /* End NPIV filtering */
1025
1026 if (!qedf->ctlr.sel_fcf) {
1027 kfree_skb(skb);
1028 return 0;
1029 }
1030
1031 if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
1032 QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
1033 kfree_skb(skb);
1034 return 0;
1035 }
1036
1037 if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
1038 QEDF_WARN(&(qedf->dbg_ctx), "qedf link down\n");
1039 kfree_skb(skb);
1040 return 0;
1041 }
1042
1043 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
1044 if (fcoe_ctlr_els_send(&qedf->ctlr, lport, skb))
1045 return 0;
1046 }
1047
1048 /* Check to see if this needs to be sent on an offloaded session */
1049 fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
1050
1051 if (fcport && test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1052 rc = qedf_xmit_l2_frame(fcport, fp);
1053 /*
1054 * If the frame was successfully sent over the middle path
1055 * then do not try to also send it over the LL2 path
1056 */
1057 if (rc)
1058 return 0;
1059 }
1060
1061 sof = fr_sof(fp);
1062 eof = fr_eof(fp);
1063
1064 elen = sizeof(struct ethhdr);
1065 hlen = sizeof(struct fcoe_hdr);
1066 tlen = sizeof(struct fcoe_crc_eof);
1067 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1068
1069 skb->ip_summed = CHECKSUM_NONE;
1070 crc = fcoe_fc_crc(fp);
1071
1072 /* copy port crc and eof to the skb buff */
1073 if (skb_is_nonlinear(skb)) {
1074 skb_frag_t *frag;
1075
1076 if (qedf_get_paged_crc_eof(skb, tlen)) {
1077 kfree_skb(skb);
1078 return -ENOMEM;
1079 }
1080 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1081 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
1082 } else {
Johannes Berg4df864c2017-06-16 14:29:21 +02001083 cp = skb_put(skb, tlen);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001084 }
1085
1086 memset(cp, 0, sizeof(*cp));
1087 cp->fcoe_eof = eof;
1088 cp->fcoe_crc32 = cpu_to_le32(~crc);
1089 if (skb_is_nonlinear(skb)) {
1090 kunmap_atomic(cp);
1091 cp = NULL;
1092 }
1093
1094
1095 /* adjust skb network/transport offsets to match mac/fcoe/port */
1096 skb_push(skb, elen + hlen);
1097 skb_reset_mac_header(skb);
1098 skb_reset_network_header(skb);
1099 skb->mac_len = elen;
1100 skb->protocol = htons(ETH_P_FCOE);
1101
Chad Dupuiscf291162017-08-15 10:08:18 -07001102 /*
1103 * Add VLAN tag to non-offload FCoE frame based on current stored VLAN
1104 * for FIP/FCoE traffic.
1105 */
Dupuis, Chad61d86582017-02-15 06:28:23 -08001106 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
1107
1108 /* fill up mac and fcoe headers */
1109 eh = eth_hdr(skb);
1110 eh->h_proto = htons(ETH_P_FCOE);
1111 if (qedf->ctlr.map_dest)
1112 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1113 else
1114 /* insert GW address */
1115 ether_addr_copy(eh->h_dest, qedf->ctlr.dest_addr);
1116
1117 /* Set the source MAC address */
Chad Dupuisa3cd42a92017-08-15 10:08:17 -07001118 ether_addr_copy(eh->h_source, qedf->data_src_addr);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001119
1120 hp = (struct fcoe_hdr *)(eh + 1);
1121 memset(hp, 0, sizeof(*hp));
1122 if (FC_FCOE_VER)
1123 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1124 hp->fcoe_sof = sof;
1125
1126 /*update tx stats */
1127 stats = per_cpu_ptr(lport->stats, get_cpu());
1128 stats->TxFrames++;
1129 stats->TxWords += wlen;
1130 put_cpu();
1131
1132 /* Get VLAN ID from skb for printing purposes */
1133 __vlan_hwaccel_get_tag(skb, &vlan_tci);
1134
1135 /* send down to lld */
1136 fr_dev(fp) = lport;
1137 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame send: "
1138 "src=%06x dest=%06x r_ctl=%x type=%x vlan=%04x.\n",
1139 ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl, fh->fh_type,
1140 vlan_tci);
1141 if (qedf_dump_frames)
1142 print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
1143 1, skb->data, skb->len, false);
Sudarsana Reddy Kallurucac6f692018-05-05 18:43:02 -07001144 qed_ops->ll2->start_xmit(qedf->cdev, skb, 0);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001145
1146 return 0;
1147}
1148
1149static int qedf_alloc_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
1150{
1151 int rval = 0;
1152 u32 *pbl;
1153 dma_addr_t page;
1154 int num_pages;
1155
1156 /* Calculate appropriate queue and PBL sizes */
1157 fcport->sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
1158 fcport->sq_mem_size = ALIGN(fcport->sq_mem_size, QEDF_PAGE_SIZE);
1159 fcport->sq_pbl_size = (fcport->sq_mem_size / QEDF_PAGE_SIZE) *
1160 sizeof(void *);
1161 fcport->sq_pbl_size = fcport->sq_pbl_size + QEDF_PAGE_SIZE;
1162
Luis Chamberlain750afb02019-01-04 09:23:09 +01001163 fcport->sq = dma_alloc_coherent(&qedf->pdev->dev, fcport->sq_mem_size,
1164 &fcport->sq_dma, GFP_KERNEL);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001165 if (!fcport->sq) {
Christophe JAILLET32eebb32017-06-11 08:16:04 +02001166 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08001167 rval = 1;
1168 goto out;
1169 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001170
Luis Chamberlain750afb02019-01-04 09:23:09 +01001171 fcport->sq_pbl = dma_alloc_coherent(&qedf->pdev->dev,
1172 fcport->sq_pbl_size,
1173 &fcport->sq_pbl_dma, GFP_KERNEL);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001174 if (!fcport->sq_pbl) {
Christophe JAILLET32eebb32017-06-11 08:16:04 +02001175 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue PBL.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08001176 rval = 1;
1177 goto out_free_sq;
1178 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001179
1180 /* Create PBL */
1181 num_pages = fcport->sq_mem_size / QEDF_PAGE_SIZE;
1182 page = fcport->sq_dma;
1183 pbl = (u32 *)fcport->sq_pbl;
1184
1185 while (num_pages--) {
1186 *pbl = U64_LO(page);
1187 pbl++;
1188 *pbl = U64_HI(page);
1189 pbl++;
1190 page += QEDF_PAGE_SIZE;
1191 }
1192
1193 return rval;
1194
1195out_free_sq:
1196 dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size, fcport->sq,
1197 fcport->sq_dma);
1198out:
1199 return rval;
1200}
1201
1202static void qedf_free_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
1203{
1204 if (fcport->sq_pbl)
1205 dma_free_coherent(&qedf->pdev->dev, fcport->sq_pbl_size,
1206 fcport->sq_pbl, fcport->sq_pbl_dma);
1207 if (fcport->sq)
1208 dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size,
1209 fcport->sq, fcport->sq_dma);
1210}
1211
1212static int qedf_offload_connection(struct qedf_ctx *qedf,
1213 struct qedf_rport *fcport)
1214{
1215 struct qed_fcoe_params_offload conn_info;
1216 u32 port_id;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001217 int rval;
1218 uint16_t total_sqe = (fcport->sq_mem_size / sizeof(struct fcoe_wqe));
1219
1220 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offloading connection "
1221 "portid=%06x.\n", fcport->rdata->ids.port_id);
1222 rval = qed_ops->acquire_conn(qedf->cdev, &fcport->handle,
1223 &fcport->fw_cid, &fcport->p_doorbell);
1224 if (rval) {
1225 QEDF_WARN(&(qedf->dbg_ctx), "Could not acquire connection "
1226 "for portid=%06x.\n", fcport->rdata->ids.port_id);
1227 rval = 1; /* For some reason qed returns 0 on failure here */
1228 goto out;
1229 }
1230
1231 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "portid=%06x "
1232 "fw_cid=%08x handle=%d.\n", fcport->rdata->ids.port_id,
1233 fcport->fw_cid, fcport->handle);
1234
1235 memset(&conn_info, 0, sizeof(struct qed_fcoe_params_offload));
1236
1237 /* Fill in the offload connection info */
1238 conn_info.sq_pbl_addr = fcport->sq_pbl_dma;
1239
1240 conn_info.sq_curr_page_addr = (dma_addr_t)(*(u64 *)fcport->sq_pbl);
1241 conn_info.sq_next_page_addr =
1242 (dma_addr_t)(*(u64 *)(fcport->sq_pbl + 8));
1243
1244 /* Need to use our FCoE MAC for the offload session */
Chad Dupuisa3cd42a92017-08-15 10:08:17 -07001245 ether_addr_copy(conn_info.src_mac, qedf->data_src_addr);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001246
1247 ether_addr_copy(conn_info.dst_mac, qedf->ctlr.dest_addr);
1248
1249 conn_info.tx_max_fc_pay_len = fcport->rdata->maxframe_size;
1250 conn_info.e_d_tov_timer_val = qedf->lport->e_d_tov / 20;
1251 conn_info.rec_tov_timer_val = 3; /* I think this is what E3 was */
1252 conn_info.rx_max_fc_pay_len = fcport->rdata->maxframe_size;
1253
1254 /* Set VLAN data */
1255 conn_info.vlan_tag = qedf->vlan_id <<
1256 FCOE_CONN_OFFLOAD_RAMROD_DATA_VLAN_ID_SHIFT;
1257 conn_info.vlan_tag |=
Chad Dupuis84b2ba62018-04-25 06:08:52 -07001258 qedf->prio << FCOE_CONN_OFFLOAD_RAMROD_DATA_PRIORITY_SHIFT;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001259 conn_info.flags |= (FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_MASK <<
1260 FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_SHIFT);
1261
1262 /* Set host port source id */
1263 port_id = fc_host_port_id(qedf->lport->host);
1264 fcport->sid = port_id;
1265 conn_info.s_id.addr_hi = (port_id & 0x000000FF);
1266 conn_info.s_id.addr_mid = (port_id & 0x0000FF00) >> 8;
1267 conn_info.s_id.addr_lo = (port_id & 0x00FF0000) >> 16;
1268
1269 conn_info.max_conc_seqs_c3 = fcport->rdata->max_seq;
1270
1271 /* Set remote port destination id */
1272 port_id = fcport->rdata->rport->port_id;
1273 conn_info.d_id.addr_hi = (port_id & 0x000000FF);
1274 conn_info.d_id.addr_mid = (port_id & 0x0000FF00) >> 8;
1275 conn_info.d_id.addr_lo = (port_id & 0x00FF0000) >> 16;
1276
1277 conn_info.def_q_idx = 0; /* Default index for send queue? */
1278
1279 /* Set FC-TAPE specific flags if needed */
1280 if (fcport->dev_type == QEDF_RPORT_TYPE_TAPE) {
1281 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN,
1282 "Enable CONF, REC for portid=%06x.\n",
1283 fcport->rdata->ids.port_id);
1284 conn_info.flags |= 1 <<
1285 FCOE_CONN_OFFLOAD_RAMROD_DATA_B_CONF_REQ_SHIFT;
1286 conn_info.flags |=
1287 ((fcport->rdata->sp_features & FC_SP_FT_SEQC) ? 1 : 0) <<
1288 FCOE_CONN_OFFLOAD_RAMROD_DATA_B_REC_VALID_SHIFT;
1289 }
1290
1291 rval = qed_ops->offload_conn(qedf->cdev, fcport->handle, &conn_info);
1292 if (rval) {
1293 QEDF_WARN(&(qedf->dbg_ctx), "Could not offload connection "
1294 "for portid=%06x.\n", fcport->rdata->ids.port_id);
1295 goto out_free_conn;
1296 } else
1297 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offload "
1298 "succeeded portid=%06x total_sqe=%d.\n",
1299 fcport->rdata->ids.port_id, total_sqe);
1300
1301 spin_lock_init(&fcport->rport_lock);
1302 atomic_set(&fcport->free_sqes, total_sqe);
1303 return 0;
1304out_free_conn:
1305 qed_ops->release_conn(qedf->cdev, fcport->handle);
1306out:
1307 return rval;
1308}
1309
1310#define QEDF_TERM_BUFF_SIZE 10
1311static void qedf_upload_connection(struct qedf_ctx *qedf,
1312 struct qedf_rport *fcport)
1313{
1314 void *term_params;
1315 dma_addr_t term_params_dma;
1316
1317 /* Term params needs to be a DMA coherent buffer as qed shared the
1318 * physical DMA address with the firmware. The buffer may be used in
1319 * the receive path so we may eventually have to move this.
1320 */
1321 term_params = dma_alloc_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE,
1322 &term_params_dma, GFP_KERNEL);
1323
1324 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Uploading connection "
1325 "port_id=%06x.\n", fcport->rdata->ids.port_id);
1326
1327 qed_ops->destroy_conn(qedf->cdev, fcport->handle, term_params_dma);
1328 qed_ops->release_conn(qedf->cdev, fcport->handle);
1329
1330 dma_free_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE, term_params,
1331 term_params_dma);
1332}
1333
1334static void qedf_cleanup_fcport(struct qedf_ctx *qedf,
1335 struct qedf_rport *fcport)
1336{
Hannes Reinecke56efc302019-03-26 00:38:49 -07001337 struct fc_rport_priv *rdata = fcport->rdata;
1338
Dupuis, Chad61d86582017-02-15 06:28:23 -08001339 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Cleaning up portid=%06x.\n",
1340 fcport->rdata->ids.port_id);
1341
1342 /* Flush any remaining i/o's before we upload the connection */
1343 qedf_flush_active_ios(fcport, -1);
1344
1345 if (test_and_clear_bit(QEDF_RPORT_SESSION_READY, &fcport->flags))
1346 qedf_upload_connection(qedf, fcport);
1347 qedf_free_sq(qedf, fcport);
1348 fcport->rdata = NULL;
1349 fcport->qedf = NULL;
Hannes Reinecke56efc302019-03-26 00:38:49 -07001350 kref_put(&rdata->kref, fc_rport_destroy);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001351}
1352
1353/**
1354 * This event_callback is called after successful completion of libfc
1355 * initiated target login. qedf can proceed with initiating the session
1356 * establishment.
1357 */
1358static void qedf_rport_event_handler(struct fc_lport *lport,
1359 struct fc_rport_priv *rdata,
1360 enum fc_rport_event event)
1361{
1362 struct qedf_ctx *qedf = lport_priv(lport);
1363 struct fc_rport *rport = rdata->rport;
1364 struct fc_rport_libfc_priv *rp;
1365 struct qedf_rport *fcport;
1366 u32 port_id;
1367 int rval;
1368 unsigned long flags;
1369
1370 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "event = %d, "
1371 "port_id = 0x%x\n", event, rdata->ids.port_id);
1372
1373 switch (event) {
1374 case RPORT_EV_READY:
1375 if (!rport) {
1376 QEDF_WARN(&(qedf->dbg_ctx), "rport is NULL.\n");
1377 break;
1378 }
1379
1380 rp = rport->dd_data;
1381 fcport = (struct qedf_rport *)&rp[1];
1382 fcport->qedf = qedf;
1383
1384 if (atomic_read(&qedf->num_offloads) >= QEDF_MAX_SESSIONS) {
1385 QEDF_ERR(&(qedf->dbg_ctx), "Not offloading "
1386 "portid=0x%x as max number of offloaded sessions "
1387 "reached.\n", rdata->ids.port_id);
1388 return;
1389 }
1390
1391 /*
1392 * Don't try to offload the session again. Can happen when we
1393 * get an ADISC
1394 */
1395 if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1396 QEDF_WARN(&(qedf->dbg_ctx), "Session already "
1397 "offloaded, portid=0x%x.\n",
1398 rdata->ids.port_id);
1399 return;
1400 }
1401
1402 if (rport->port_id == FC_FID_DIR_SERV) {
1403 /*
1404 * qedf_rport structure doesn't exist for
1405 * directory server.
1406 * We should not come here, as lport will
1407 * take care of fabric login
1408 */
1409 QEDF_WARN(&(qedf->dbg_ctx), "rport struct does not "
1410 "exist for dir server port_id=%x\n",
1411 rdata->ids.port_id);
1412 break;
1413 }
1414
1415 if (rdata->spp_type != FC_TYPE_FCP) {
1416 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
Colin Ian Kingacef2692017-07-03 20:21:39 +01001417 "Not offloading since spp type isn't FCP\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08001418 break;
1419 }
1420 if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
1421 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1422 "Not FCP target so not offloading\n");
1423 break;
1424 }
1425
Hannes Reinecke56efc302019-03-26 00:38:49 -07001426 /* Initial reference held on entry, so this can't fail */
1427 kref_get(&rdata->kref);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001428 fcport->rdata = rdata;
1429 fcport->rport = rport;
1430
1431 rval = qedf_alloc_sq(qedf, fcport);
1432 if (rval) {
1433 qedf_cleanup_fcport(qedf, fcport);
1434 break;
1435 }
1436
1437 /* Set device type */
1438 if (rdata->flags & FC_RP_FLAGS_RETRY &&
1439 rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET &&
1440 !(rdata->ids.roles & FC_RPORT_ROLE_FCP_INITIATOR)) {
1441 fcport->dev_type = QEDF_RPORT_TYPE_TAPE;
1442 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1443 "portid=%06x is a TAPE device.\n",
1444 rdata->ids.port_id);
1445 } else {
1446 fcport->dev_type = QEDF_RPORT_TYPE_DISK;
1447 }
1448
1449 rval = qedf_offload_connection(qedf, fcport);
1450 if (rval) {
1451 qedf_cleanup_fcport(qedf, fcport);
1452 break;
1453 }
1454
1455 /* Add fcport to list of qedf_ctx list of offloaded ports */
1456 spin_lock_irqsave(&qedf->hba_lock, flags);
1457 list_add_rcu(&fcport->peers, &qedf->fcports);
1458 spin_unlock_irqrestore(&qedf->hba_lock, flags);
1459
1460 /*
1461 * Set the session ready bit to let everyone know that this
1462 * connection is ready for I/O
1463 */
1464 set_bit(QEDF_RPORT_SESSION_READY, &fcport->flags);
1465 atomic_inc(&qedf->num_offloads);
1466
1467 break;
1468 case RPORT_EV_LOGO:
1469 case RPORT_EV_FAILED:
1470 case RPORT_EV_STOP:
1471 port_id = rdata->ids.port_id;
1472 if (port_id == FC_FID_DIR_SERV)
1473 break;
1474
1475 if (!rport) {
1476 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1477 "port_id=%x - rport notcreated Yet!!\n", port_id);
1478 break;
1479 }
1480 rp = rport->dd_data;
1481 /*
1482 * Perform session upload. Note that rdata->peers is already
1483 * removed from disc->rports list before we get this event.
1484 */
1485 fcport = (struct qedf_rport *)&rp[1];
1486
Hannes Reinecke78a8ab32019-03-26 00:38:45 -07001487 spin_lock_irqsave(&fcport->rport_lock, flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001488 /* Only free this fcport if it is offloaded already */
Shyam Sundar5d5e55652019-03-26 00:38:37 -07001489 if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) &&
1490 !test_bit(QEDF_RPORT_UPLOADING_CONNECTION,
1491 &fcport->flags)) {
1492 set_bit(QEDF_RPORT_UPLOADING_CONNECTION,
1493 &fcport->flags);
Hannes Reinecke78a8ab32019-03-26 00:38:45 -07001494 spin_unlock_irqrestore(&fcport->rport_lock, flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001495 qedf_cleanup_fcport(qedf, fcport);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001496 /*
1497 * Remove fcport to list of qedf_ctx list of offloaded
1498 * ports
1499 */
1500 spin_lock_irqsave(&qedf->hba_lock, flags);
1501 list_del_rcu(&fcport->peers);
1502 spin_unlock_irqrestore(&qedf->hba_lock, flags);
1503
1504 clear_bit(QEDF_RPORT_UPLOADING_CONNECTION,
1505 &fcport->flags);
1506 atomic_dec(&qedf->num_offloads);
Hannes Reinecke78a8ab32019-03-26 00:38:45 -07001507 } else {
1508 spin_unlock_irqrestore(&fcport->rport_lock, flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001509 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001510 break;
1511
1512 case RPORT_EV_NONE:
1513 break;
1514 }
1515}
1516
1517static void qedf_abort_io(struct fc_lport *lport)
1518{
1519 /* NO-OP but need to fill in the template */
1520}
1521
1522static void qedf_fcp_cleanup(struct fc_lport *lport)
1523{
1524 /*
1525 * NO-OP but need to fill in template to prevent a NULL
1526 * function pointer dereference during link down. I/Os
1527 * will be flushed when port is uploaded.
1528 */
1529}
1530
1531static struct libfc_function_template qedf_lport_template = {
1532 .frame_send = qedf_xmit,
1533 .fcp_abort_io = qedf_abort_io,
1534 .fcp_cleanup = qedf_fcp_cleanup,
1535 .rport_event_callback = qedf_rport_event_handler,
1536 .elsct_send = qedf_elsct_send,
1537};
1538
1539static void qedf_fcoe_ctlr_setup(struct qedf_ctx *qedf)
1540{
Sedat Dilek8beb90a2019-02-15 13:19:20 +01001541 fcoe_ctlr_init(&qedf->ctlr, FIP_MODE_AUTO);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001542
1543 qedf->ctlr.send = qedf_fip_send;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001544 qedf->ctlr.get_src_addr = qedf_get_src_mac;
1545 ether_addr_copy(qedf->ctlr.ctl_src_addr, qedf->mac);
1546}
1547
Chad Dupuis5185b322017-05-31 06:33:48 -07001548static void qedf_setup_fdmi(struct qedf_ctx *qedf)
1549{
1550 struct fc_lport *lport = qedf->lport;
1551 struct fc_host_attrs *fc_host = shost_to_fc_host(lport->host);
1552 u8 buf[8];
1553 int i, pos;
1554
1555 /*
1556 * fdmi_enabled needs to be set for libfc to execute FDMI registration.
1557 */
1558 lport->fdmi_enabled = 1;
1559
1560 /*
1561 * Setup the necessary fc_host attributes to that will be used to fill
1562 * in the FDMI information.
1563 */
1564
1565 /* Get the PCI-e Device Serial Number Capability */
1566 pos = pci_find_ext_capability(qedf->pdev, PCI_EXT_CAP_ID_DSN);
1567 if (pos) {
1568 pos += 4;
1569 for (i = 0; i < 8; i++)
1570 pci_read_config_byte(qedf->pdev, pos + i, &buf[i]);
1571
1572 snprintf(fc_host->serial_number,
1573 sizeof(fc_host->serial_number),
1574 "%02X%02X%02X%02X%02X%02X%02X%02X",
1575 buf[7], buf[6], buf[5], buf[4],
1576 buf[3], buf[2], buf[1], buf[0]);
1577 } else
1578 snprintf(fc_host->serial_number,
1579 sizeof(fc_host->serial_number), "Unknown");
1580
1581 snprintf(fc_host->manufacturer,
1582 sizeof(fc_host->manufacturer), "%s", "Cavium Inc.");
1583
1584 snprintf(fc_host->model, sizeof(fc_host->model), "%s", "QL41000");
1585
1586 snprintf(fc_host->model_description, sizeof(fc_host->model_description),
1587 "%s", "QLogic FastLinQ QL41000 Series 10/25/40/50GGbE Controller"
1588 "(FCoE)");
1589
1590 snprintf(fc_host->hardware_version, sizeof(fc_host->hardware_version),
1591 "Rev %d", qedf->pdev->revision);
1592
1593 snprintf(fc_host->driver_version, sizeof(fc_host->driver_version),
1594 "%s", QEDF_VERSION);
1595
1596 snprintf(fc_host->firmware_version, sizeof(fc_host->firmware_version),
1597 "%d.%d.%d.%d", FW_MAJOR_VERSION, FW_MINOR_VERSION,
1598 FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
1599}
1600
Dupuis, Chad61d86582017-02-15 06:28:23 -08001601static int qedf_lport_setup(struct qedf_ctx *qedf)
1602{
1603 struct fc_lport *lport = qedf->lport;
1604
1605 lport->link_up = 0;
1606 lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
1607 lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
1608 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1609 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
1610 lport->boot_time = jiffies;
1611 lport->e_d_tov = 2 * 1000;
1612 lport->r_a_tov = 10 * 1000;
1613
1614 /* Set NPIV support */
1615 lport->does_npiv = 1;
1616 fc_host_max_npiv_vports(lport->host) = QEDF_MAX_NPIV;
1617
1618 fc_set_wwnn(lport, qedf->wwnn);
1619 fc_set_wwpn(lport, qedf->wwpn);
1620
Saurav Kashyapf4eecabe72019-04-21 22:44:56 -07001621 if (fcoe_libfc_config(lport, &qedf->ctlr, &qedf_lport_template, 0)) {
1622 QEDF_ERR(&qedf->dbg_ctx,
1623 "fcoe_libfc_config failed.\n");
1624 return -ENOMEM;
1625 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08001626
1627 /* Allocate the exchange manager */
Chad Dupuis650ce642019-03-26 00:38:34 -07001628 fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_PARAMS_NUM_TASKS,
1629 0xfffe, NULL);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001630
1631 if (fc_lport_init_stats(lport))
1632 return -ENOMEM;
1633
1634 /* Finish lport config */
1635 fc_lport_config(lport);
1636
1637 /* Set max frame size */
1638 fc_set_mfs(lport, QEDF_MFS);
1639 fc_host_maxframe_size(lport->host) = lport->mfs;
1640
1641 /* Set default dev_loss_tmo based on module parameter */
1642 fc_host_dev_loss_tmo(lport->host) = qedf_dev_loss_tmo;
1643
1644 /* Set symbolic node name */
1645 snprintf(fc_host_symbolic_name(lport->host), 256,
1646 "QLogic %s v%s", QEDF_MODULE_NAME, QEDF_VERSION);
1647
Chad Dupuis5185b322017-05-31 06:33:48 -07001648 qedf_setup_fdmi(qedf);
1649
Dupuis, Chad61d86582017-02-15 06:28:23 -08001650 return 0;
1651}
1652
1653/*
1654 * NPIV functions
1655 */
1656
1657static int qedf_vport_libfc_config(struct fc_vport *vport,
1658 struct fc_lport *lport)
1659{
1660 lport->link_up = 0;
1661 lport->qfull = 0;
1662 lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
1663 lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
1664 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1665 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
1666 lport->boot_time = jiffies;
1667 lport->e_d_tov = 2 * 1000;
1668 lport->r_a_tov = 10 * 1000;
1669 lport->does_npiv = 1; /* Temporary until we add NPIV support */
1670
1671 /* Allocate stats for vport */
1672 if (fc_lport_init_stats(lport))
1673 return -ENOMEM;
1674
1675 /* Finish lport config */
1676 fc_lport_config(lport);
1677
1678 /* offload related configuration */
1679 lport->crc_offload = 0;
1680 lport->seq_offload = 0;
1681 lport->lro_enabled = 0;
1682 lport->lro_xid = 0;
1683 lport->lso_max = 0;
1684
1685 return 0;
1686}
1687
1688static int qedf_vport_create(struct fc_vport *vport, bool disabled)
1689{
1690 struct Scsi_Host *shost = vport_to_shost(vport);
1691 struct fc_lport *n_port = shost_priv(shost);
1692 struct fc_lport *vn_port;
1693 struct qedf_ctx *base_qedf = lport_priv(n_port);
1694 struct qedf_ctx *vport_qedf;
1695
1696 char buf[32];
1697 int rc = 0;
1698
1699 rc = fcoe_validate_vport_create(vport);
1700 if (rc) {
1701 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1702 QEDF_WARN(&(base_qedf->dbg_ctx), "Failed to create vport, "
1703 "WWPN (0x%s) already exists.\n", buf);
1704 goto err1;
1705 }
1706
1707 if (atomic_read(&base_qedf->link_state) != QEDF_LINK_UP) {
1708 QEDF_WARN(&(base_qedf->dbg_ctx), "Cannot create vport "
1709 "because link is not up.\n");
1710 rc = -EIO;
1711 goto err1;
1712 }
1713
1714 vn_port = libfc_vport_create(vport, sizeof(struct qedf_ctx));
1715 if (!vn_port) {
1716 QEDF_WARN(&(base_qedf->dbg_ctx), "Could not create lport "
1717 "for vport.\n");
1718 rc = -ENOMEM;
1719 goto err1;
1720 }
1721
1722 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1723 QEDF_ERR(&(base_qedf->dbg_ctx), "Creating NPIV port, WWPN=%s.\n",
1724 buf);
1725
1726 /* Copy some fields from base_qedf */
1727 vport_qedf = lport_priv(vn_port);
1728 memcpy(vport_qedf, base_qedf, sizeof(struct qedf_ctx));
1729
1730 /* Set qedf data specific to this vport */
1731 vport_qedf->lport = vn_port;
1732 /* Use same hba_lock as base_qedf */
1733 vport_qedf->hba_lock = base_qedf->hba_lock;
1734 vport_qedf->pdev = base_qedf->pdev;
1735 vport_qedf->cmd_mgr = base_qedf->cmd_mgr;
1736 init_completion(&vport_qedf->flogi_compl);
1737 INIT_LIST_HEAD(&vport_qedf->fcports);
1738
1739 rc = qedf_vport_libfc_config(vport, vn_port);
1740 if (rc) {
1741 QEDF_ERR(&(base_qedf->dbg_ctx), "Could not allocate memory "
1742 "for lport stats.\n");
1743 goto err2;
1744 }
1745
1746 fc_set_wwnn(vn_port, vport->node_name);
1747 fc_set_wwpn(vn_port, vport->port_name);
1748 vport_qedf->wwnn = vn_port->wwnn;
1749 vport_qedf->wwpn = vn_port->wwpn;
1750
1751 vn_port->host->transportt = qedf_fc_vport_transport_template;
Chad Dupuis650ce642019-03-26 00:38:34 -07001752 vn_port->host->can_queue = FCOE_PARAMS_NUM_TASKS;
Dupuis, Chad61d86582017-02-15 06:28:23 -08001753 vn_port->host->max_lun = qedf_max_lun;
1754 vn_port->host->sg_tablesize = QEDF_MAX_BDS_PER_CMD;
1755 vn_port->host->max_cmd_len = QEDF_MAX_CDB_LEN;
1756
1757 rc = scsi_add_host(vn_port->host, &vport->dev);
1758 if (rc) {
1759 QEDF_WARN(&(base_qedf->dbg_ctx), "Error adding Scsi_Host.\n");
1760 goto err2;
1761 }
1762
1763 /* Set default dev_loss_tmo based on module parameter */
1764 fc_host_dev_loss_tmo(vn_port->host) = qedf_dev_loss_tmo;
1765
1766 /* Init libfc stuffs */
1767 memcpy(&vn_port->tt, &qedf_lport_template,
1768 sizeof(qedf_lport_template));
1769 fc_exch_init(vn_port);
1770 fc_elsct_init(vn_port);
1771 fc_lport_init(vn_port);
1772 fc_disc_init(vn_port);
1773 fc_disc_config(vn_port, vn_port);
1774
1775
1776 /* Allocate the exchange manager */
1777 shost = vport_to_shost(vport);
1778 n_port = shost_priv(shost);
1779 fc_exch_mgr_list_clone(n_port, vn_port);
1780
1781 /* Set max frame size */
1782 fc_set_mfs(vn_port, QEDF_MFS);
1783
1784 fc_host_port_type(vn_port->host) = FC_PORTTYPE_UNKNOWN;
1785
1786 if (disabled) {
1787 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1788 } else {
1789 vn_port->boot_time = jiffies;
1790 fc_fabric_login(vn_port);
1791 fc_vport_setlink(vn_port);
1792 }
1793
1794 QEDF_INFO(&(base_qedf->dbg_ctx), QEDF_LOG_NPIV, "vn_port=%p.\n",
1795 vn_port);
1796
1797 /* Set up debug context for vport */
1798 vport_qedf->dbg_ctx.host_no = vn_port->host->host_no;
1799 vport_qedf->dbg_ctx.pdev = base_qedf->pdev;
1800
1801err2:
1802 scsi_host_put(vn_port->host);
1803err1:
1804 return rc;
1805}
1806
1807static int qedf_vport_destroy(struct fc_vport *vport)
1808{
1809 struct Scsi_Host *shost = vport_to_shost(vport);
1810 struct fc_lport *n_port = shost_priv(shost);
1811 struct fc_lport *vn_port = vport->dd_data;
Chad Dupuis4f4616c2018-04-25 06:09:02 -07001812 struct qedf_ctx *qedf = lport_priv(vn_port);
1813
1814 if (!qedf) {
1815 QEDF_ERR(NULL, "qedf is NULL.\n");
1816 goto out;
1817 }
1818
1819 /* Set unloading bit on vport qedf_ctx to prevent more I/O */
1820 set_bit(QEDF_UNLOADING, &qedf->flags);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001821
1822 mutex_lock(&n_port->lp_mutex);
1823 list_del(&vn_port->list);
1824 mutex_unlock(&n_port->lp_mutex);
1825
1826 fc_fabric_logoff(vn_port);
1827 fc_lport_destroy(vn_port);
1828
1829 /* Detach from scsi-ml */
1830 fc_remove_host(vn_port->host);
1831 scsi_remove_host(vn_port->host);
1832
1833 /*
1834 * Only try to release the exchange manager if the vn_port
1835 * configuration is complete.
1836 */
1837 if (vn_port->state == LPORT_ST_READY)
1838 fc_exch_mgr_free(vn_port);
1839
1840 /* Free memory used by statistical counters */
1841 fc_lport_free_stats(vn_port);
1842
1843 /* Release Scsi_Host */
1844 if (vn_port->host)
1845 scsi_host_put(vn_port->host);
1846
Chad Dupuis4f4616c2018-04-25 06:09:02 -07001847out:
Dupuis, Chad61d86582017-02-15 06:28:23 -08001848 return 0;
1849}
1850
1851static int qedf_vport_disable(struct fc_vport *vport, bool disable)
1852{
1853 struct fc_lport *lport = vport->dd_data;
1854
1855 if (disable) {
1856 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1857 fc_fabric_logoff(lport);
1858 } else {
1859 lport->boot_time = jiffies;
1860 fc_fabric_login(lport);
1861 fc_vport_setlink(lport);
1862 }
1863 return 0;
1864}
1865
1866/*
1867 * During removal we need to wait for all the vports associated with a port
1868 * to be destroyed so we avoid a race condition where libfc is still trying
1869 * to reap vports while the driver remove function has already reaped the
1870 * driver contexts associated with the physical port.
1871 */
1872static void qedf_wait_for_vport_destroy(struct qedf_ctx *qedf)
1873{
1874 struct fc_host_attrs *fc_host = shost_to_fc_host(qedf->lport->host);
1875
1876 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
1877 "Entered.\n");
1878 while (fc_host->npiv_vports_inuse > 0) {
1879 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
1880 "Waiting for all vports to be reaped.\n");
1881 msleep(1000);
1882 }
1883}
1884
1885/**
1886 * qedf_fcoe_reset - Resets the fcoe
1887 *
1888 * @shost: shost the reset is from
1889 *
1890 * Returns: always 0
1891 */
1892static int qedf_fcoe_reset(struct Scsi_Host *shost)
1893{
1894 struct fc_lport *lport = shost_priv(shost);
1895
Chad Dupuis5cf446d2017-05-31 06:33:55 -07001896 qedf_ctx_soft_reset(lport);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001897 return 0;
1898}
1899
1900static struct fc_host_statistics *qedf_fc_get_host_stats(struct Scsi_Host
1901 *shost)
1902{
1903 struct fc_host_statistics *qedf_stats;
1904 struct fc_lport *lport = shost_priv(shost);
1905 struct qedf_ctx *qedf = lport_priv(lport);
1906 struct qed_fcoe_stats *fw_fcoe_stats;
1907
1908 qedf_stats = fc_get_host_stats(shost);
1909
1910 /* We don't collect offload stats for specific NPIV ports */
1911 if (lport->vport)
1912 goto out;
1913
1914 fw_fcoe_stats = kmalloc(sizeof(struct qed_fcoe_stats), GFP_KERNEL);
1915 if (!fw_fcoe_stats) {
1916 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate memory for "
1917 "fw_fcoe_stats.\n");
1918 goto out;
1919 }
1920
Chad Dupuis642a0b32018-05-22 00:28:43 -07001921 mutex_lock(&qedf->stats_mutex);
1922
Dupuis, Chad61d86582017-02-15 06:28:23 -08001923 /* Query firmware for offload stats */
1924 qed_ops->get_stats(qedf->cdev, fw_fcoe_stats);
1925
1926 /*
1927 * The expectation is that we add our offload stats to the stats
1928 * being maintained by libfc each time the fc_get_host_status callback
1929 * is invoked. The additions are not carried over for each call to
1930 * the fc_get_host_stats callback.
1931 */
1932 qedf_stats->tx_frames += fw_fcoe_stats->fcoe_tx_data_pkt_cnt +
1933 fw_fcoe_stats->fcoe_tx_xfer_pkt_cnt +
1934 fw_fcoe_stats->fcoe_tx_other_pkt_cnt;
1935 qedf_stats->rx_frames += fw_fcoe_stats->fcoe_rx_data_pkt_cnt +
1936 fw_fcoe_stats->fcoe_rx_xfer_pkt_cnt +
1937 fw_fcoe_stats->fcoe_rx_other_pkt_cnt;
1938 qedf_stats->fcp_input_megabytes +=
1939 do_div(fw_fcoe_stats->fcoe_rx_byte_cnt, 1000000);
1940 qedf_stats->fcp_output_megabytes +=
1941 do_div(fw_fcoe_stats->fcoe_tx_byte_cnt, 1000000);
1942 qedf_stats->rx_words += fw_fcoe_stats->fcoe_rx_byte_cnt / 4;
1943 qedf_stats->tx_words += fw_fcoe_stats->fcoe_tx_byte_cnt / 4;
1944 qedf_stats->invalid_crc_count +=
1945 fw_fcoe_stats->fcoe_silent_drop_pkt_crc_error_cnt;
1946 qedf_stats->dumped_frames =
1947 fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
1948 qedf_stats->error_frames +=
1949 fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
1950 qedf_stats->fcp_input_requests += qedf->input_requests;
1951 qedf_stats->fcp_output_requests += qedf->output_requests;
1952 qedf_stats->fcp_control_requests += qedf->control_requests;
1953 qedf_stats->fcp_packet_aborts += qedf->packet_aborts;
1954 qedf_stats->fcp_frame_alloc_failures += qedf->alloc_failures;
1955
Chad Dupuis642a0b32018-05-22 00:28:43 -07001956 mutex_unlock(&qedf->stats_mutex);
Dupuis, Chad61d86582017-02-15 06:28:23 -08001957 kfree(fw_fcoe_stats);
1958out:
1959 return qedf_stats;
1960}
1961
1962static struct fc_function_template qedf_fc_transport_fn = {
1963 .show_host_node_name = 1,
1964 .show_host_port_name = 1,
1965 .show_host_supported_classes = 1,
1966 .show_host_supported_fc4s = 1,
1967 .show_host_active_fc4s = 1,
1968 .show_host_maxframe_size = 1,
1969
1970 .show_host_port_id = 1,
1971 .show_host_supported_speeds = 1,
1972 .get_host_speed = fc_get_host_speed,
1973 .show_host_speed = 1,
1974 .show_host_port_type = 1,
1975 .get_host_port_state = fc_get_host_port_state,
1976 .show_host_port_state = 1,
1977 .show_host_symbolic_name = 1,
1978
1979 /*
1980 * Tell FC transport to allocate enough space to store the backpointer
1981 * for the associate qedf_rport struct.
1982 */
1983 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
1984 sizeof(struct qedf_rport)),
1985 .show_rport_maxframe_size = 1,
1986 .show_rport_supported_classes = 1,
1987 .show_host_fabric_name = 1,
1988 .show_starget_node_name = 1,
1989 .show_starget_port_name = 1,
1990 .show_starget_port_id = 1,
1991 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
1992 .show_rport_dev_loss_tmo = 1,
1993 .get_fc_host_stats = qedf_fc_get_host_stats,
1994 .issue_fc_host_lip = qedf_fcoe_reset,
1995 .vport_create = qedf_vport_create,
1996 .vport_delete = qedf_vport_destroy,
1997 .vport_disable = qedf_vport_disable,
1998 .bsg_request = fc_lport_bsg_request,
1999};
2000
2001static struct fc_function_template qedf_fc_vport_transport_fn = {
2002 .show_host_node_name = 1,
2003 .show_host_port_name = 1,
2004 .show_host_supported_classes = 1,
2005 .show_host_supported_fc4s = 1,
2006 .show_host_active_fc4s = 1,
2007 .show_host_maxframe_size = 1,
2008 .show_host_port_id = 1,
2009 .show_host_supported_speeds = 1,
2010 .get_host_speed = fc_get_host_speed,
2011 .show_host_speed = 1,
2012 .show_host_port_type = 1,
2013 .get_host_port_state = fc_get_host_port_state,
2014 .show_host_port_state = 1,
2015 .show_host_symbolic_name = 1,
2016 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2017 sizeof(struct qedf_rport)),
2018 .show_rport_maxframe_size = 1,
2019 .show_rport_supported_classes = 1,
2020 .show_host_fabric_name = 1,
2021 .show_starget_node_name = 1,
2022 .show_starget_port_name = 1,
2023 .show_starget_port_id = 1,
2024 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2025 .show_rport_dev_loss_tmo = 1,
2026 .get_fc_host_stats = fc_get_host_stats,
2027 .issue_fc_host_lip = qedf_fcoe_reset,
2028 .bsg_request = fc_lport_bsg_request,
2029};
2030
2031static bool qedf_fp_has_work(struct qedf_fastpath *fp)
2032{
2033 struct qedf_ctx *qedf = fp->qedf;
2034 struct global_queue *que;
2035 struct qed_sb_info *sb_info = fp->sb_info;
Tomer Tayar21dd79e2017-12-27 19:30:06 +02002036 struct status_block_e4 *sb = sb_info->sb_virt;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002037 u16 prod_idx;
2038
2039 /* Get the pointer to the global CQ this completion is on */
2040 que = qedf->global_queues[fp->sb_id];
2041
2042 /* Be sure all responses have been written to PI */
2043 rmb();
2044
2045 /* Get the current firmware producer index */
2046 prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
2047
2048 return (que->cq_prod_idx != prod_idx);
2049}
2050
2051/*
2052 * Interrupt handler code.
2053 */
2054
2055/* Process completion queue and copy CQE contents for deferred processesing
2056 *
2057 * Return true if we should wake the I/O thread, false if not.
2058 */
2059static bool qedf_process_completions(struct qedf_fastpath *fp)
2060{
2061 struct qedf_ctx *qedf = fp->qedf;
2062 struct qed_sb_info *sb_info = fp->sb_info;
Tomer Tayar21dd79e2017-12-27 19:30:06 +02002063 struct status_block_e4 *sb = sb_info->sb_virt;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002064 struct global_queue *que;
2065 u16 prod_idx;
2066 struct fcoe_cqe *cqe;
2067 struct qedf_io_work *io_work;
2068 int num_handled = 0;
2069 unsigned int cpu;
2070 struct qedf_ioreq *io_req = NULL;
2071 u16 xid;
2072 u16 new_cqes;
2073 u32 comp_type;
2074
2075 /* Get the current firmware producer index */
2076 prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
2077
2078 /* Get the pointer to the global CQ this completion is on */
2079 que = qedf->global_queues[fp->sb_id];
2080
2081 /* Calculate the amount of new elements since last processing */
2082 new_cqes = (prod_idx >= que->cq_prod_idx) ?
2083 (prod_idx - que->cq_prod_idx) :
2084 0x10000 - que->cq_prod_idx + prod_idx;
2085
2086 /* Save producer index */
2087 que->cq_prod_idx = prod_idx;
2088
2089 while (new_cqes) {
2090 fp->completions++;
2091 num_handled++;
2092 cqe = &que->cq[que->cq_cons_idx];
2093
2094 comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
2095 FCOE_CQE_CQE_TYPE_MASK;
2096
2097 /*
2098 * Process unsolicited CQEs directly in the interrupt handler
2099 * sine we need the fastpath ID
2100 */
2101 if (comp_type == FCOE_UNSOLIC_CQE_TYPE) {
2102 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
2103 "Unsolicated CQE.\n");
2104 qedf_process_unsol_compl(qedf, fp->sb_id, cqe);
2105 /*
2106 * Don't add a work list item. Increment consumer
2107 * consumer index and move on.
2108 */
2109 goto inc_idx;
2110 }
2111
2112 xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
2113 io_req = &qedf->cmd_mgr->cmds[xid];
2114
2115 /*
2116 * Figure out which percpu thread we should queue this I/O
2117 * on.
2118 */
2119 if (!io_req)
2120 /* If there is not io_req assocated with this CQE
2121 * just queue it on CPU 0
2122 */
2123 cpu = 0;
2124 else {
2125 cpu = io_req->cpu;
2126 io_req->int_cpu = smp_processor_id();
2127 }
2128
2129 io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
2130 if (!io_work) {
2131 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
2132 "work for I/O completion.\n");
2133 continue;
2134 }
2135 memset(io_work, 0, sizeof(struct qedf_io_work));
2136
2137 INIT_WORK(&io_work->work, qedf_fp_io_handler);
2138
2139 /* Copy contents of CQE for deferred processing */
2140 memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
2141
2142 io_work->qedf = fp->qedf;
2143 io_work->fp = NULL; /* Only used for unsolicited frames */
2144
2145 queue_work_on(cpu, qedf_io_wq, &io_work->work);
2146
2147inc_idx:
2148 que->cq_cons_idx++;
2149 if (que->cq_cons_idx == fp->cq_num_entries)
2150 que->cq_cons_idx = 0;
2151 new_cqes--;
2152 }
2153
2154 return true;
2155}
2156
2157
2158/* MSI-X fastpath handler code */
2159static irqreturn_t qedf_msix_handler(int irq, void *dev_id)
2160{
2161 struct qedf_fastpath *fp = dev_id;
2162
2163 if (!fp) {
2164 QEDF_ERR(NULL, "fp is null.\n");
2165 return IRQ_HANDLED;
2166 }
2167 if (!fp->sb_info) {
2168 QEDF_ERR(NULL, "fp->sb_info in null.");
2169 return IRQ_HANDLED;
2170 }
2171
2172 /*
2173 * Disable interrupts for this status block while we process new
2174 * completions
2175 */
2176 qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
2177
2178 while (1) {
2179 qedf_process_completions(fp);
2180
2181 if (qedf_fp_has_work(fp) == 0) {
2182 /* Update the sb information */
2183 qed_sb_update_sb_idx(fp->sb_info);
2184
2185 /* Check for more work */
2186 rmb();
2187
2188 if (qedf_fp_has_work(fp) == 0) {
2189 /* Re-enable interrupts */
2190 qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
2191 return IRQ_HANDLED;
2192 }
2193 }
2194 }
2195
2196 /* Do we ever want to break out of above loop? */
2197 return IRQ_HANDLED;
2198}
2199
2200/* simd handler for MSI/INTa */
2201static void qedf_simd_int_handler(void *cookie)
2202{
2203 /* Cookie is qedf_ctx struct */
2204 struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
2205
2206 QEDF_WARN(&(qedf->dbg_ctx), "qedf=%p.\n", qedf);
2207}
2208
2209#define QEDF_SIMD_HANDLER_NUM 0
2210static void qedf_sync_free_irqs(struct qedf_ctx *qedf)
2211{
2212 int i;
2213
2214 if (qedf->int_info.msix_cnt) {
2215 for (i = 0; i < qedf->int_info.used_cnt; i++) {
2216 synchronize_irq(qedf->int_info.msix[i].vector);
2217 irq_set_affinity_hint(qedf->int_info.msix[i].vector,
2218 NULL);
2219 irq_set_affinity_notifier(qedf->int_info.msix[i].vector,
2220 NULL);
2221 free_irq(qedf->int_info.msix[i].vector,
2222 &qedf->fp_array[i]);
2223 }
2224 } else
2225 qed_ops->common->simd_handler_clean(qedf->cdev,
2226 QEDF_SIMD_HANDLER_NUM);
2227
2228 qedf->int_info.used_cnt = 0;
2229 qed_ops->common->set_fp_int(qedf->cdev, 0);
2230}
2231
2232static int qedf_request_msix_irq(struct qedf_ctx *qedf)
2233{
2234 int i, rc, cpu;
2235
2236 cpu = cpumask_first(cpu_online_mask);
2237 for (i = 0; i < qedf->num_queues; i++) {
2238 rc = request_irq(qedf->int_info.msix[i].vector,
2239 qedf_msix_handler, 0, "qedf", &qedf->fp_array[i]);
2240
2241 if (rc) {
2242 QEDF_WARN(&(qedf->dbg_ctx), "request_irq failed.\n");
2243 qedf_sync_free_irqs(qedf);
2244 return rc;
2245 }
2246
2247 qedf->int_info.used_cnt++;
2248 rc = irq_set_affinity_hint(qedf->int_info.msix[i].vector,
2249 get_cpu_mask(cpu));
2250 cpu = cpumask_next(cpu, cpu_online_mask);
2251 }
2252
2253 return 0;
2254}
2255
2256static int qedf_setup_int(struct qedf_ctx *qedf)
2257{
2258 int rc = 0;
2259
2260 /*
2261 * Learn interrupt configuration
2262 */
2263 rc = qed_ops->common->set_fp_int(qedf->cdev, num_online_cpus());
Chad Dupuis914fff12017-05-31 06:33:50 -07002264 if (rc <= 0)
2265 return 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002266
2267 rc = qed_ops->common->get_fp_int(qedf->cdev, &qedf->int_info);
2268 if (rc)
2269 return 0;
2270
2271 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of msix_cnt = "
2272 "0x%x num of cpus = 0x%x\n", qedf->int_info.msix_cnt,
2273 num_online_cpus());
2274
2275 if (qedf->int_info.msix_cnt)
2276 return qedf_request_msix_irq(qedf);
2277
2278 qed_ops->common->simd_handler_config(qedf->cdev, &qedf,
2279 QEDF_SIMD_HANDLER_NUM, qedf_simd_int_handler);
2280 qedf->int_info.used_cnt = 1;
2281
Chad Dupuis078d7192019-04-21 22:44:50 -07002282 QEDF_ERR(&qedf->dbg_ctx,
2283 "Cannot load driver due to a lack of MSI-X vectors.\n");
Chad Dupuis96673e12018-04-25 06:09:00 -07002284 return -EINVAL;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002285}
2286
2287/* Main function for libfc frame reception */
2288static void qedf_recv_frame(struct qedf_ctx *qedf,
2289 struct sk_buff *skb)
2290{
2291 u32 fr_len;
2292 struct fc_lport *lport;
2293 struct fc_frame_header *fh;
2294 struct fcoe_crc_eof crc_eof;
2295 struct fc_frame *fp;
2296 u8 *mac = NULL;
2297 u8 *dest_mac = NULL;
2298 struct fcoe_hdr *hp;
2299 struct qedf_rport *fcport;
Chad Dupuis384d5a92017-05-31 06:33:57 -07002300 struct fc_lport *vn_port;
2301 u32 f_ctl;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002302
2303 lport = qedf->lport;
2304 if (lport == NULL || lport->state == LPORT_ST_DISABLED) {
2305 QEDF_WARN(NULL, "Invalid lport struct or lport disabled.\n");
2306 kfree_skb(skb);
2307 return;
2308 }
2309
2310 if (skb_is_nonlinear(skb))
2311 skb_linearize(skb);
2312 mac = eth_hdr(skb)->h_source;
2313 dest_mac = eth_hdr(skb)->h_dest;
2314
2315 /* Pull the header */
2316 hp = (struct fcoe_hdr *)skb->data;
2317 fh = (struct fc_frame_header *) skb_transport_header(skb);
2318 skb_pull(skb, sizeof(struct fcoe_hdr));
2319 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
2320
2321 fp = (struct fc_frame *)skb;
2322 fc_frame_init(fp);
2323 fr_dev(fp) = lport;
2324 fr_sof(fp) = hp->fcoe_sof;
2325 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
2326 kfree_skb(skb);
2327 return;
2328 }
2329 fr_eof(fp) = crc_eof.fcoe_eof;
2330 fr_crc(fp) = crc_eof.fcoe_crc32;
2331 if (pskb_trim(skb, fr_len)) {
2332 kfree_skb(skb);
2333 return;
2334 }
2335
2336 fh = fc_frame_header_get(fp);
2337
Chad Dupuis384d5a92017-05-31 06:33:57 -07002338 /*
2339 * Invalid frame filters.
2340 */
2341
Dupuis, Chad61d86582017-02-15 06:28:23 -08002342 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
2343 fh->fh_type == FC_TYPE_FCP) {
2344 /* Drop FCP data. We dont this in L2 path */
2345 kfree_skb(skb);
2346 return;
2347 }
2348 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
2349 fh->fh_type == FC_TYPE_ELS) {
2350 switch (fc_frame_payload_op(fp)) {
2351 case ELS_LOGO:
2352 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
2353 /* drop non-FIP LOGO */
2354 kfree_skb(skb);
2355 return;
2356 }
2357 break;
2358 }
2359 }
2360
2361 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
2362 /* Drop incoming ABTS */
2363 kfree_skb(skb);
2364 return;
2365 }
2366
Chad Dupuis384d5a92017-05-31 06:33:57 -07002367 if (ntoh24(&dest_mac[3]) != ntoh24(fh->fh_d_id)) {
Chad Dupuisf7e8d572017-05-31 06:33:59 -07002368 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2369 "FC frame d_id mismatch with MAC %pM.\n", dest_mac);
Chad Dupuisf32803b2018-04-25 06:08:47 -07002370 kfree_skb(skb);
Chad Dupuis384d5a92017-05-31 06:33:57 -07002371 return;
2372 }
2373
2374 if (qedf->ctlr.state) {
2375 if (!ether_addr_equal(mac, qedf->ctlr.dest_addr)) {
Chad Dupuisf7e8d572017-05-31 06:33:59 -07002376 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2377 "Wrong source address: mac:%pM dest_addr:%pM.\n",
Chad Dupuis384d5a92017-05-31 06:33:57 -07002378 mac, qedf->ctlr.dest_addr);
2379 kfree_skb(skb);
2380 return;
2381 }
2382 }
2383
2384 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
2385
2386 /*
2387 * If the destination ID from the frame header does not match what we
2388 * have on record for lport and the search for a NPIV port came up
2389 * empty then this is not addressed to our port so simply drop it.
2390 */
2391 if (lport->port_id != ntoh24(fh->fh_d_id) && !vn_port) {
Chad Dupuisf7e8d572017-05-31 06:33:59 -07002392 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2393 "Dropping frame due to destination mismatch: lport->port_id=%x fh->d_id=%x.\n",
Chad Dupuis384d5a92017-05-31 06:33:57 -07002394 lport->port_id, ntoh24(fh->fh_d_id));
2395 kfree_skb(skb);
2396 return;
2397 }
2398
2399 f_ctl = ntoh24(fh->fh_f_ctl);
2400 if ((fh->fh_type == FC_TYPE_BLS) && (f_ctl & FC_FC_SEQ_CTX) &&
2401 (f_ctl & FC_FC_EX_CTX)) {
2402 /* Drop incoming ABTS response that has both SEQ/EX CTX set */
2403 kfree_skb(skb);
2404 return;
2405 }
2406
Dupuis, Chad61d86582017-02-15 06:28:23 -08002407 /*
2408 * If a connection is uploading, drop incoming FCoE frames as there
2409 * is a small window where we could try to return a frame while libfc
2410 * is trying to clean things up.
2411 */
2412
2413 /* Get fcport associated with d_id if it exists */
2414 fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
2415
2416 if (fcport && test_bit(QEDF_RPORT_UPLOADING_CONNECTION,
2417 &fcport->flags)) {
2418 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2419 "Connection uploading, dropping fp=%p.\n", fp);
2420 kfree_skb(skb);
2421 return;
2422 }
2423
2424 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame receive: "
2425 "skb=%p fp=%p src=%06x dest=%06x r_ctl=%x fh_type=%x.\n", skb, fp,
2426 ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl,
2427 fh->fh_type);
2428 if (qedf_dump_frames)
2429 print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
2430 1, skb->data, skb->len, false);
2431 fc_exch_recv(lport, fp);
2432}
2433
2434static void qedf_ll2_process_skb(struct work_struct *work)
2435{
2436 struct qedf_skb_work *skb_work =
2437 container_of(work, struct qedf_skb_work, work);
2438 struct qedf_ctx *qedf = skb_work->qedf;
2439 struct sk_buff *skb = skb_work->skb;
2440 struct ethhdr *eh;
2441
2442 if (!qedf) {
2443 QEDF_ERR(NULL, "qedf is NULL\n");
2444 goto err_out;
2445 }
2446
2447 eh = (struct ethhdr *)skb->data;
2448
2449 /* Undo VLAN encapsulation */
2450 if (eh->h_proto == htons(ETH_P_8021Q)) {
2451 memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
Johannes Bergaf728682017-06-16 14:29:22 +02002452 eh = skb_pull(skb, VLAN_HLEN);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002453 skb_reset_mac_header(skb);
2454 }
2455
2456 /*
2457 * Process either a FIP frame or FCoE frame based on the
2458 * protocol value. If it's not either just drop the
2459 * frame.
2460 */
2461 if (eh->h_proto == htons(ETH_P_FIP)) {
2462 qedf_fip_recv(qedf, skb);
2463 goto out;
2464 } else if (eh->h_proto == htons(ETH_P_FCOE)) {
2465 __skb_pull(skb, ETH_HLEN);
2466 qedf_recv_frame(qedf, skb);
2467 goto out;
2468 } else
2469 goto err_out;
2470
2471err_out:
2472 kfree_skb(skb);
2473out:
2474 kfree(skb_work);
2475 return;
2476}
2477
2478static int qedf_ll2_rx(void *cookie, struct sk_buff *skb,
2479 u32 arg1, u32 arg2)
2480{
2481 struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
2482 struct qedf_skb_work *skb_work;
2483
Saurav Kashyap76dbf4f2019-03-26 00:38:42 -07002484 if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
2485 QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
2486 "Dropping frame as link state is down.\n");
2487 kfree_skb(skb);
2488 return 0;
2489 }
2490
Dupuis, Chad61d86582017-02-15 06:28:23 -08002491 skb_work = kzalloc(sizeof(struct qedf_skb_work), GFP_ATOMIC);
2492 if (!skb_work) {
2493 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate skb_work so "
2494 "dropping frame.\n");
2495 kfree_skb(skb);
2496 return 0;
2497 }
2498
2499 INIT_WORK(&skb_work->work, qedf_ll2_process_skb);
2500 skb_work->skb = skb;
2501 skb_work->qedf = qedf;
2502 queue_work(qedf->ll2_recv_wq, &skb_work->work);
2503
2504 return 0;
2505}
2506
2507static struct qed_ll2_cb_ops qedf_ll2_cb_ops = {
2508 .rx_cb = qedf_ll2_rx,
2509 .tx_cb = NULL,
2510};
2511
2512/* Main thread to process I/O completions */
2513void qedf_fp_io_handler(struct work_struct *work)
2514{
2515 struct qedf_io_work *io_work =
2516 container_of(work, struct qedf_io_work, work);
2517 u32 comp_type;
2518
2519 /*
2520 * Deferred part of unsolicited CQE sends
2521 * frame to libfc.
2522 */
2523 comp_type = (io_work->cqe.cqe_data >>
2524 FCOE_CQE_CQE_TYPE_SHIFT) &
2525 FCOE_CQE_CQE_TYPE_MASK;
2526 if (comp_type == FCOE_UNSOLIC_CQE_TYPE &&
2527 io_work->fp)
2528 fc_exch_recv(io_work->qedf->lport, io_work->fp);
2529 else
2530 qedf_process_cqe(io_work->qedf, &io_work->cqe);
2531
2532 kfree(io_work);
2533}
2534
2535static int qedf_alloc_and_init_sb(struct qedf_ctx *qedf,
2536 struct qed_sb_info *sb_info, u16 sb_id)
2537{
Tomer Tayar21dd79e2017-12-27 19:30:06 +02002538 struct status_block_e4 *sb_virt;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002539 dma_addr_t sb_phys;
2540 int ret;
2541
2542 sb_virt = dma_alloc_coherent(&qedf->pdev->dev,
Tomer Tayar21dd79e2017-12-27 19:30:06 +02002543 sizeof(struct status_block_e4), &sb_phys, GFP_KERNEL);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002544
2545 if (!sb_virt) {
2546 QEDF_ERR(&(qedf->dbg_ctx), "Status block allocation failed "
2547 "for id = %d.\n", sb_id);
2548 return -ENOMEM;
2549 }
2550
2551 ret = qed_ops->common->sb_init(qedf->cdev, sb_info, sb_virt, sb_phys,
2552 sb_id, QED_SB_TYPE_STORAGE);
2553
2554 if (ret) {
2555 QEDF_ERR(&(qedf->dbg_ctx), "Status block initialization "
2556 "failed for id = %d.\n", sb_id);
2557 return ret;
2558 }
2559
2560 return 0;
2561}
2562
2563static void qedf_free_sb(struct qedf_ctx *qedf, struct qed_sb_info *sb_info)
2564{
2565 if (sb_info->sb_virt)
2566 dma_free_coherent(&qedf->pdev->dev, sizeof(*sb_info->sb_virt),
2567 (void *)sb_info->sb_virt, sb_info->sb_phys);
2568}
2569
2570static void qedf_destroy_sb(struct qedf_ctx *qedf)
2571{
2572 int id;
2573 struct qedf_fastpath *fp = NULL;
2574
2575 for (id = 0; id < qedf->num_queues; id++) {
2576 fp = &(qedf->fp_array[id]);
2577 if (fp->sb_id == QEDF_SB_ID_NULL)
2578 break;
2579 qedf_free_sb(qedf, fp->sb_info);
2580 kfree(fp->sb_info);
2581 }
2582 kfree(qedf->fp_array);
2583}
2584
2585static int qedf_prepare_sb(struct qedf_ctx *qedf)
2586{
2587 int id;
2588 struct qedf_fastpath *fp;
2589 int ret;
2590
2591 qedf->fp_array =
2592 kcalloc(qedf->num_queues, sizeof(struct qedf_fastpath),
2593 GFP_KERNEL);
2594
2595 if (!qedf->fp_array) {
2596 QEDF_ERR(&(qedf->dbg_ctx), "fastpath array allocation "
2597 "failed.\n");
2598 return -ENOMEM;
2599 }
2600
2601 for (id = 0; id < qedf->num_queues; id++) {
2602 fp = &(qedf->fp_array[id]);
2603 fp->sb_id = QEDF_SB_ID_NULL;
2604 fp->sb_info = kcalloc(1, sizeof(*fp->sb_info), GFP_KERNEL);
2605 if (!fp->sb_info) {
2606 QEDF_ERR(&(qedf->dbg_ctx), "SB info struct "
2607 "allocation failed.\n");
2608 goto err;
2609 }
2610 ret = qedf_alloc_and_init_sb(qedf, fp->sb_info, id);
2611 if (ret) {
2612 QEDF_ERR(&(qedf->dbg_ctx), "SB allocation and "
2613 "initialization failed.\n");
2614 goto err;
2615 }
2616 fp->sb_id = id;
2617 fp->qedf = qedf;
2618 fp->cq_num_entries =
2619 qedf->global_queues[id]->cq_mem_size /
2620 sizeof(struct fcoe_cqe);
2621 }
2622err:
2623 return 0;
2624}
2625
2626void qedf_process_cqe(struct qedf_ctx *qedf, struct fcoe_cqe *cqe)
2627{
2628 u16 xid;
2629 struct qedf_ioreq *io_req;
2630 struct qedf_rport *fcport;
2631 u32 comp_type;
2632
2633 comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
2634 FCOE_CQE_CQE_TYPE_MASK;
2635
2636 xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
2637 io_req = &qedf->cmd_mgr->cmds[xid];
2638
2639 /* Completion not for a valid I/O anymore so just return */
2640 if (!io_req)
2641 return;
2642
2643 fcport = io_req->fcport;
2644
2645 if (fcport == NULL) {
2646 QEDF_ERR(&(qedf->dbg_ctx), "fcport is NULL.\n");
2647 return;
2648 }
2649
2650 /*
2651 * Check that fcport is offloaded. If it isn't then the spinlock
2652 * isn't valid and shouldn't be taken. We should just return.
2653 */
2654 if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
2655 QEDF_ERR(&(qedf->dbg_ctx), "Session not offloaded yet.\n");
2656 return;
2657 }
2658
2659
2660 switch (comp_type) {
2661 case FCOE_GOOD_COMPLETION_CQE_TYPE:
2662 atomic_inc(&fcport->free_sqes);
2663 switch (io_req->cmd_type) {
2664 case QEDF_SCSI_CMD:
2665 qedf_scsi_completion(qedf, cqe, io_req);
2666 break;
2667 case QEDF_ELS:
2668 qedf_process_els_compl(qedf, cqe, io_req);
2669 break;
2670 case QEDF_TASK_MGMT_CMD:
2671 qedf_process_tmf_compl(qedf, cqe, io_req);
2672 break;
2673 case QEDF_SEQ_CLEANUP:
2674 qedf_process_seq_cleanup_compl(qedf, cqe, io_req);
2675 break;
2676 }
2677 break;
2678 case FCOE_ERROR_DETECTION_CQE_TYPE:
2679 atomic_inc(&fcport->free_sqes);
2680 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2681 "Error detect CQE.\n");
2682 qedf_process_error_detect(qedf, cqe, io_req);
2683 break;
2684 case FCOE_EXCH_CLEANUP_CQE_TYPE:
2685 atomic_inc(&fcport->free_sqes);
2686 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2687 "Cleanup CQE.\n");
2688 qedf_process_cleanup_compl(qedf, cqe, io_req);
2689 break;
2690 case FCOE_ABTS_CQE_TYPE:
2691 atomic_inc(&fcport->free_sqes);
2692 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2693 "Abort CQE.\n");
2694 qedf_process_abts_compl(qedf, cqe, io_req);
2695 break;
2696 case FCOE_DUMMY_CQE_TYPE:
2697 atomic_inc(&fcport->free_sqes);
2698 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2699 "Dummy CQE.\n");
2700 break;
2701 case FCOE_LOCAL_COMP_CQE_TYPE:
2702 atomic_inc(&fcport->free_sqes);
2703 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2704 "Local completion CQE.\n");
2705 break;
2706 case FCOE_WARNING_CQE_TYPE:
2707 atomic_inc(&fcport->free_sqes);
2708 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2709 "Warning CQE.\n");
2710 qedf_process_warning_compl(qedf, cqe, io_req);
2711 break;
2712 case MAX_FCOE_CQE_TYPE:
2713 atomic_inc(&fcport->free_sqes);
2714 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2715 "Max FCoE CQE.\n");
2716 break;
2717 default:
2718 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2719 "Default CQE.\n");
2720 break;
2721 }
2722}
2723
2724static void qedf_free_bdq(struct qedf_ctx *qedf)
2725{
2726 int i;
2727
2728 if (qedf->bdq_pbl_list)
2729 dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
2730 qedf->bdq_pbl_list, qedf->bdq_pbl_list_dma);
2731
2732 if (qedf->bdq_pbl)
2733 dma_free_coherent(&qedf->pdev->dev, qedf->bdq_pbl_mem_size,
2734 qedf->bdq_pbl, qedf->bdq_pbl_dma);
2735
2736 for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2737 if (qedf->bdq[i].buf_addr) {
2738 dma_free_coherent(&qedf->pdev->dev, QEDF_BDQ_BUF_SIZE,
2739 qedf->bdq[i].buf_addr, qedf->bdq[i].buf_dma);
2740 }
2741 }
2742}
2743
2744static void qedf_free_global_queues(struct qedf_ctx *qedf)
2745{
2746 int i;
2747 struct global_queue **gl = qedf->global_queues;
2748
2749 for (i = 0; i < qedf->num_queues; i++) {
2750 if (!gl[i])
2751 continue;
2752
2753 if (gl[i]->cq)
2754 dma_free_coherent(&qedf->pdev->dev,
2755 gl[i]->cq_mem_size, gl[i]->cq, gl[i]->cq_dma);
2756 if (gl[i]->cq_pbl)
2757 dma_free_coherent(&qedf->pdev->dev, gl[i]->cq_pbl_size,
2758 gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
2759
2760 kfree(gl[i]);
2761 }
2762
2763 qedf_free_bdq(qedf);
2764}
2765
2766static int qedf_alloc_bdq(struct qedf_ctx *qedf)
2767{
2768 int i;
2769 struct scsi_bd *pbl;
2770 u64 *list;
2771 dma_addr_t page;
2772
2773 /* Alloc dma memory for BDQ buffers */
2774 for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2775 qedf->bdq[i].buf_addr = dma_alloc_coherent(&qedf->pdev->dev,
2776 QEDF_BDQ_BUF_SIZE, &qedf->bdq[i].buf_dma, GFP_KERNEL);
2777 if (!qedf->bdq[i].buf_addr) {
2778 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ "
2779 "buffer %d.\n", i);
2780 return -ENOMEM;
2781 }
2782 }
2783
2784 /* Alloc dma memory for BDQ page buffer list */
2785 qedf->bdq_pbl_mem_size =
2786 QEDF_BDQ_SIZE * sizeof(struct scsi_bd);
2787 qedf->bdq_pbl_mem_size =
2788 ALIGN(qedf->bdq_pbl_mem_size, QEDF_PAGE_SIZE);
2789
2790 qedf->bdq_pbl = dma_alloc_coherent(&qedf->pdev->dev,
2791 qedf->bdq_pbl_mem_size, &qedf->bdq_pbl_dma, GFP_KERNEL);
2792 if (!qedf->bdq_pbl) {
2793 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ PBL.\n");
2794 return -ENOMEM;
2795 }
2796
2797 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
Joe Perchesfd2b18b2017-03-06 10:32:27 -08002798 "BDQ PBL addr=0x%p dma=%pad\n",
2799 qedf->bdq_pbl, &qedf->bdq_pbl_dma);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002800
2801 /*
2802 * Populate BDQ PBL with physical and virtual address of individual
2803 * BDQ buffers
2804 */
2805 pbl = (struct scsi_bd *)qedf->bdq_pbl;
2806 for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2807 pbl->address.hi = cpu_to_le32(U64_HI(qedf->bdq[i].buf_dma));
2808 pbl->address.lo = cpu_to_le32(U64_LO(qedf->bdq[i].buf_dma));
Tomer Tayarda090912017-12-27 19:30:07 +02002809 pbl->opaque.fcoe_opaque.hi = 0;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002810 /* Opaque lo data is an index into the BDQ array */
Tomer Tayarda090912017-12-27 19:30:07 +02002811 pbl->opaque.fcoe_opaque.lo = cpu_to_le32(i);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002812 pbl++;
2813 }
2814
2815 /* Allocate list of PBL pages */
Luis Chamberlain750afb02019-01-04 09:23:09 +01002816 qedf->bdq_pbl_list = dma_alloc_coherent(&qedf->pdev->dev,
2817 QEDF_PAGE_SIZE,
2818 &qedf->bdq_pbl_list_dma,
2819 GFP_KERNEL);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002820 if (!qedf->bdq_pbl_list) {
Christophe JAILLET32eebb32017-06-11 08:16:04 +02002821 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate list of PBL pages.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08002822 return -ENOMEM;
2823 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08002824
2825 /*
2826 * Now populate PBL list with pages that contain pointers to the
2827 * individual buffers.
2828 */
2829 qedf->bdq_pbl_list_num_entries = qedf->bdq_pbl_mem_size /
2830 QEDF_PAGE_SIZE;
2831 list = (u64 *)qedf->bdq_pbl_list;
2832 page = qedf->bdq_pbl_list_dma;
2833 for (i = 0; i < qedf->bdq_pbl_list_num_entries; i++) {
2834 *list = qedf->bdq_pbl_dma;
2835 list++;
2836 page += QEDF_PAGE_SIZE;
2837 }
2838
2839 return 0;
2840}
2841
2842static int qedf_alloc_global_queues(struct qedf_ctx *qedf)
2843{
2844 u32 *list;
2845 int i;
2846 int status = 0, rc;
2847 u32 *pbl;
2848 dma_addr_t page;
2849 int num_pages;
2850
2851 /* Allocate and map CQs, RQs */
2852 /*
2853 * Number of global queues (CQ / RQ). This should
2854 * be <= number of available MSIX vectors for the PF
2855 */
2856 if (!qedf->num_queues) {
2857 QEDF_ERR(&(qedf->dbg_ctx), "No MSI-X vectors available!\n");
2858 return 1;
2859 }
2860
2861 /*
2862 * Make sure we allocated the PBL that will contain the physical
2863 * addresses of our queues
2864 */
2865 if (!qedf->p_cpuq) {
2866 status = 1;
2867 goto mem_alloc_failure;
2868 }
2869
2870 qedf->global_queues = kzalloc((sizeof(struct global_queue *)
2871 * qedf->num_queues), GFP_KERNEL);
2872 if (!qedf->global_queues) {
2873 QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate global "
2874 "queues array ptr memory\n");
2875 return -ENOMEM;
2876 }
2877 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2878 "qedf->global_queues=%p.\n", qedf->global_queues);
2879
2880 /* Allocate DMA coherent buffers for BDQ */
2881 rc = qedf_alloc_bdq(qedf);
2882 if (rc)
2883 goto mem_alloc_failure;
2884
2885 /* Allocate a CQ and an associated PBL for each MSI-X vector */
2886 for (i = 0; i < qedf->num_queues; i++) {
2887 qedf->global_queues[i] = kzalloc(sizeof(struct global_queue),
2888 GFP_KERNEL);
2889 if (!qedf->global_queues[i]) {
Christophe JAILLET3a240b22017-06-11 08:16:02 +02002890 QEDF_WARN(&(qedf->dbg_ctx), "Unable to allocate "
Dupuis, Chad61d86582017-02-15 06:28:23 -08002891 "global queue %d.\n", i);
Christophe JAILLET3a240b22017-06-11 08:16:02 +02002892 status = -ENOMEM;
Dupuis, Chad61d86582017-02-15 06:28:23 -08002893 goto mem_alloc_failure;
2894 }
2895
2896 qedf->global_queues[i]->cq_mem_size =
2897 FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
2898 qedf->global_queues[i]->cq_mem_size =
2899 ALIGN(qedf->global_queues[i]->cq_mem_size, QEDF_PAGE_SIZE);
2900
2901 qedf->global_queues[i]->cq_pbl_size =
2902 (qedf->global_queues[i]->cq_mem_size /
2903 PAGE_SIZE) * sizeof(void *);
2904 qedf->global_queues[i]->cq_pbl_size =
2905 ALIGN(qedf->global_queues[i]->cq_pbl_size, QEDF_PAGE_SIZE);
2906
2907 qedf->global_queues[i]->cq =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002908 dma_alloc_coherent(&qedf->pdev->dev,
2909 qedf->global_queues[i]->cq_mem_size,
2910 &qedf->global_queues[i]->cq_dma,
2911 GFP_KERNEL);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002912
2913 if (!qedf->global_queues[i]->cq) {
Christophe JAILLET32eebb32017-06-11 08:16:04 +02002914 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08002915 status = -ENOMEM;
2916 goto mem_alloc_failure;
2917 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08002918
2919 qedf->global_queues[i]->cq_pbl =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002920 dma_alloc_coherent(&qedf->pdev->dev,
2921 qedf->global_queues[i]->cq_pbl_size,
2922 &qedf->global_queues[i]->cq_pbl_dma,
2923 GFP_KERNEL);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002924
2925 if (!qedf->global_queues[i]->cq_pbl) {
Christophe JAILLET32eebb32017-06-11 08:16:04 +02002926 QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq PBL.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08002927 status = -ENOMEM;
2928 goto mem_alloc_failure;
2929 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08002930
2931 /* Create PBL */
2932 num_pages = qedf->global_queues[i]->cq_mem_size /
2933 QEDF_PAGE_SIZE;
2934 page = qedf->global_queues[i]->cq_dma;
2935 pbl = (u32 *)qedf->global_queues[i]->cq_pbl;
2936
2937 while (num_pages--) {
2938 *pbl = U64_LO(page);
2939 pbl++;
2940 *pbl = U64_HI(page);
2941 pbl++;
2942 page += QEDF_PAGE_SIZE;
2943 }
2944 /* Set the initial consumer index for cq */
2945 qedf->global_queues[i]->cq_cons_idx = 0;
2946 }
2947
2948 list = (u32 *)qedf->p_cpuq;
2949
2950 /*
2951 * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
2952 * CQ#1 PBL pointer, RQ#1 PBL pointer, etc. Each PBL pointer points
2953 * to the physical address which contains an array of pointers to
2954 * the physical addresses of the specific queue pages.
2955 */
2956 for (i = 0; i < qedf->num_queues; i++) {
2957 *list = U64_LO(qedf->global_queues[i]->cq_pbl_dma);
2958 list++;
2959 *list = U64_HI(qedf->global_queues[i]->cq_pbl_dma);
2960 list++;
2961 *list = U64_LO(0);
2962 list++;
2963 *list = U64_HI(0);
2964 list++;
2965 }
2966
2967 return 0;
2968
2969mem_alloc_failure:
2970 qedf_free_global_queues(qedf);
2971 return status;
2972}
2973
2974static int qedf_set_fcoe_pf_param(struct qedf_ctx *qedf)
2975{
2976 u8 sq_num_pbl_pages;
2977 u32 sq_mem_size;
2978 u32 cq_mem_size;
2979 u32 cq_num_entries;
2980 int rval;
2981
2982 /*
2983 * The number of completion queues/fastpath interrupts/status blocks
2984 * we allocation is the minimum off:
2985 *
2986 * Number of CPUs
Thomas Bogendoerfer722477c2017-07-25 11:19:21 +02002987 * Number allocated by qed for our PCI function
Dupuis, Chad61d86582017-02-15 06:28:23 -08002988 */
Thomas Bogendoerfer722477c2017-07-25 11:19:21 +02002989 qedf->num_queues = MIN_NUM_CPUS_MSIX(qedf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002990
2991 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of CQs is %d.\n",
2992 qedf->num_queues);
2993
Christoph Hellwig332d84f2018-10-10 20:04:15 +02002994 qedf->p_cpuq = dma_alloc_coherent(&qedf->pdev->dev,
Dupuis, Chad61d86582017-02-15 06:28:23 -08002995 qedf->num_queues * sizeof(struct qedf_glbl_q_params),
Christoph Hellwig332d84f2018-10-10 20:04:15 +02002996 &qedf->hw_p_cpuq, GFP_KERNEL);
Dupuis, Chad61d86582017-02-15 06:28:23 -08002997
2998 if (!qedf->p_cpuq) {
Christoph Hellwig332d84f2018-10-10 20:04:15 +02002999 QEDF_ERR(&(qedf->dbg_ctx), "dma_alloc_coherent failed.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08003000 return 1;
3001 }
3002
3003 rval = qedf_alloc_global_queues(qedf);
3004 if (rval) {
3005 QEDF_ERR(&(qedf->dbg_ctx), "Global queue allocation "
3006 "failed.\n");
3007 return 1;
3008 }
3009
3010 /* Calculate SQ PBL size in the same manner as in qedf_sq_alloc() */
3011 sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
3012 sq_mem_size = ALIGN(sq_mem_size, QEDF_PAGE_SIZE);
3013 sq_num_pbl_pages = (sq_mem_size / QEDF_PAGE_SIZE);
3014
3015 /* Calculate CQ num entries */
3016 cq_mem_size = FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
3017 cq_mem_size = ALIGN(cq_mem_size, QEDF_PAGE_SIZE);
3018 cq_num_entries = cq_mem_size / sizeof(struct fcoe_cqe);
3019
Christophe JAILLET32eebb32017-06-11 08:16:04 +02003020 memset(&(qedf->pf_params), 0, sizeof(qedf->pf_params));
Dupuis, Chad61d86582017-02-15 06:28:23 -08003021
3022 /* Setup the value for fcoe PF */
3023 qedf->pf_params.fcoe_pf_params.num_cons = QEDF_MAX_SESSIONS;
3024 qedf->pf_params.fcoe_pf_params.num_tasks = FCOE_PARAMS_NUM_TASKS;
3025 qedf->pf_params.fcoe_pf_params.glbl_q_params_addr =
3026 (u64)qedf->hw_p_cpuq;
3027 qedf->pf_params.fcoe_pf_params.sq_num_pbl_pages = sq_num_pbl_pages;
3028
3029 qedf->pf_params.fcoe_pf_params.rq_buffer_log_size = 0;
3030
3031 qedf->pf_params.fcoe_pf_params.cq_num_entries = cq_num_entries;
3032 qedf->pf_params.fcoe_pf_params.num_cqs = qedf->num_queues;
3033
3034 /* log_page_size: 12 for 4KB pages */
3035 qedf->pf_params.fcoe_pf_params.log_page_size = ilog2(QEDF_PAGE_SIZE);
3036
3037 qedf->pf_params.fcoe_pf_params.mtu = 9000;
3038 qedf->pf_params.fcoe_pf_params.gl_rq_pi = QEDF_FCOE_PARAMS_GL_RQ_PI;
3039 qedf->pf_params.fcoe_pf_params.gl_cmd_pi = QEDF_FCOE_PARAMS_GL_CMD_PI;
3040
3041 /* BDQ address and size */
3042 qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0] =
3043 qedf->bdq_pbl_list_dma;
3044 qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0] =
3045 qedf->bdq_pbl_list_num_entries;
3046 qedf->pf_params.fcoe_pf_params.rq_buffer_size = QEDF_BDQ_BUF_SIZE;
3047
3048 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3049 "bdq_list=%p bdq_pbl_list_dma=%llx bdq_pbl_list_entries=%d.\n",
3050 qedf->bdq_pbl_list,
3051 qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0],
3052 qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0]);
3053
3054 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3055 "cq_num_entries=%d.\n",
3056 qedf->pf_params.fcoe_pf_params.cq_num_entries);
3057
3058 return 0;
3059}
3060
3061/* Free DMA coherent memory for array of queue pointers we pass to qed */
3062static void qedf_free_fcoe_pf_param(struct qedf_ctx *qedf)
3063{
3064 size_t size = 0;
3065
3066 if (qedf->p_cpuq) {
3067 size = qedf->num_queues * sizeof(struct qedf_glbl_q_params);
Christoph Hellwig332d84f2018-10-10 20:04:15 +02003068 dma_free_coherent(&qedf->pdev->dev, size, qedf->p_cpuq,
Dupuis, Chad61d86582017-02-15 06:28:23 -08003069 qedf->hw_p_cpuq);
3070 }
3071
3072 qedf_free_global_queues(qedf);
3073
Thomas Meyerf3e46ac2018-12-02 21:52:11 +01003074 kfree(qedf->global_queues);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003075}
3076
3077/*
3078 * PCI driver functions
3079 */
3080
3081static const struct pci_device_id qedf_pci_tbl[] = {
3082 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165c) },
3083 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8080) },
3084 {0}
3085};
3086MODULE_DEVICE_TABLE(pci, qedf_pci_tbl);
3087
3088static struct pci_driver qedf_pci_driver = {
3089 .name = QEDF_MODULE_NAME,
3090 .id_table = qedf_pci_tbl,
3091 .probe = qedf_probe,
3092 .remove = qedf_remove,
3093};
3094
3095static int __qedf_probe(struct pci_dev *pdev, int mode)
3096{
3097 int rc = -EINVAL;
3098 struct fc_lport *lport;
3099 struct qedf_ctx *qedf;
3100 struct Scsi_Host *host;
3101 bool is_vf = false;
3102 struct qed_ll2_params params;
3103 char host_buf[20];
3104 struct qed_link_params link_params;
3105 int status;
3106 void *task_start, *task_end;
3107 struct qed_slowpath_params slowpath_params;
3108 struct qed_probe_params qed_params;
3109 u16 tmp;
3110
3111 /*
3112 * When doing error recovery we didn't reap the lport so don't try
3113 * to reallocate it.
3114 */
3115 if (mode != QEDF_MODE_RECOVERY) {
3116 lport = libfc_host_alloc(&qedf_host_template,
3117 sizeof(struct qedf_ctx));
3118
3119 if (!lport) {
3120 QEDF_ERR(NULL, "Could not allocate lport.\n");
3121 rc = -ENOMEM;
3122 goto err0;
3123 }
3124
Chad Dupuis0cbd0072019-03-26 00:38:41 -07003125 fc_disc_init(lport);
3126
Dupuis, Chad61d86582017-02-15 06:28:23 -08003127 /* Initialize qedf_ctx */
3128 qedf = lport_priv(lport);
3129 qedf->lport = lport;
3130 qedf->ctlr.lp = lport;
3131 qedf->pdev = pdev;
3132 qedf->dbg_ctx.pdev = pdev;
3133 qedf->dbg_ctx.host_no = lport->host->host_no;
3134 spin_lock_init(&qedf->hba_lock);
3135 INIT_LIST_HEAD(&qedf->fcports);
3136 qedf->curr_conn_id = QEDF_MAX_SESSIONS - 1;
3137 atomic_set(&qedf->num_offloads, 0);
3138 qedf->stop_io_on_error = false;
3139 pci_set_drvdata(pdev, qedf);
Chad Dupuis8eaf7df2017-03-23 06:58:47 -07003140 init_completion(&qedf->fipvlan_compl);
Chad Dupuis642a0b32018-05-22 00:28:43 -07003141 mutex_init(&qedf->stats_mutex);
Saurav Kashyap69ef2c62019-03-26 00:38:38 -07003142 mutex_init(&qedf->flush_mutex);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003143
3144 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO,
3145 "QLogic FastLinQ FCoE Module qedf %s, "
3146 "FW %d.%d.%d.%d\n", QEDF_VERSION,
3147 FW_MAJOR_VERSION, FW_MINOR_VERSION, FW_REVISION_VERSION,
3148 FW_ENGINEERING_VERSION);
3149 } else {
3150 /* Init pointers during recovery */
3151 qedf = pci_get_drvdata(pdev);
3152 lport = qedf->lport;
3153 }
3154
3155 host = lport->host;
3156
3157 /* Allocate mempool for qedf_io_work structs */
3158 qedf->io_mempool = mempool_create_slab_pool(QEDF_IO_WORK_MIN,
3159 qedf_io_work_cache);
3160 if (qedf->io_mempool == NULL) {
3161 QEDF_ERR(&(qedf->dbg_ctx), "qedf->io_mempool is NULL.\n");
3162 goto err1;
3163 }
3164 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO, "qedf->io_mempool=%p.\n",
3165 qedf->io_mempool);
3166
3167 sprintf(host_buf, "qedf_%u_link",
3168 qedf->lport->host->host_no);
Chad Dupuis428ca642017-08-15 10:08:19 -07003169 qedf->link_update_wq = create_workqueue(host_buf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003170 INIT_DELAYED_WORK(&qedf->link_update, qedf_handle_link_update);
3171 INIT_DELAYED_WORK(&qedf->link_recovery, qedf_link_recovery);
Chad Dupuis4b9b7fa2018-04-25 06:08:58 -07003172 INIT_DELAYED_WORK(&qedf->grcdump_work, qedf_wq_grcdump);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003173 qedf->fipvlan_retries = qedf_fipvlan_retries;
Chad Dupuis84b2ba62018-04-25 06:08:52 -07003174 /* Set a default prio in case DCBX doesn't converge */
Chad Dupuis65b7bec2018-04-25 06:08:59 -07003175 if (qedf_default_prio > -1) {
3176 /*
3177 * This is the case where we pass a modparam in so we want to
3178 * honor it even if dcbx doesn't converge.
3179 */
3180 qedf->prio = qedf_default_prio;
3181 } else
3182 qedf->prio = QEDF_DEFAULT_PRIO;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003183
3184 /*
3185 * Common probe. Takes care of basic hardware init and pci_*
3186 * functions.
3187 */
3188 memset(&qed_params, 0, sizeof(qed_params));
3189 qed_params.protocol = QED_PROTOCOL_FCOE;
3190 qed_params.dp_module = qedf_dp_module;
3191 qed_params.dp_level = qedf_dp_level;
3192 qed_params.is_vf = is_vf;
3193 qedf->cdev = qed_ops->common->probe(pdev, &qed_params);
3194 if (!qedf->cdev) {
3195 rc = -ENODEV;
3196 goto err1;
3197 }
3198
Thomas Bogendoerfer722477c2017-07-25 11:19:21 +02003199 /* Learn information crucial for qedf to progress */
3200 rc = qed_ops->fill_dev_info(qedf->cdev, &qedf->dev_info);
3201 if (rc) {
3202 QEDF_ERR(&(qedf->dbg_ctx), "Failed to dev info.\n");
3203 goto err1;
3204 }
3205
Dupuis, Chad61d86582017-02-15 06:28:23 -08003206 /* queue allocation code should come here
3207 * order should be
3208 * slowpath_start
3209 * status block allocation
3210 * interrupt registration (to get min number of queues)
3211 * set_fcoe_pf_param
3212 * qed_sp_fcoe_func_start
3213 */
3214 rc = qedf_set_fcoe_pf_param(qedf);
3215 if (rc) {
3216 QEDF_ERR(&(qedf->dbg_ctx), "Cannot set fcoe pf param.\n");
3217 goto err2;
3218 }
3219 qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
3220
Dupuis, Chad61d86582017-02-15 06:28:23 -08003221 /* Record BDQ producer doorbell addresses */
3222 qedf->bdq_primary_prod = qedf->dev_info.primary_dbq_rq_addr;
3223 qedf->bdq_secondary_prod = qedf->dev_info.secondary_bdq_rq_addr;
3224 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3225 "BDQ primary_prod=%p secondary_prod=%p.\n", qedf->bdq_primary_prod,
3226 qedf->bdq_secondary_prod);
3227
3228 qed_ops->register_ops(qedf->cdev, &qedf_cb_ops, qedf);
3229
3230 rc = qedf_prepare_sb(qedf);
3231 if (rc) {
3232
3233 QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
3234 goto err2;
3235 }
3236
3237 /* Start the Slowpath-process */
3238 slowpath_params.int_mode = QED_INT_MODE_MSIX;
3239 slowpath_params.drv_major = QEDF_DRIVER_MAJOR_VER;
3240 slowpath_params.drv_minor = QEDF_DRIVER_MINOR_VER;
3241 slowpath_params.drv_rev = QEDF_DRIVER_REV_VER;
3242 slowpath_params.drv_eng = QEDF_DRIVER_ENG_VER;
Kees Cookcd228742017-05-05 15:42:55 -07003243 strncpy(slowpath_params.name, "qedf", QED_DRV_VER_STR_SIZE);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003244 rc = qed_ops->common->slowpath_start(qedf->cdev, &slowpath_params);
3245 if (rc) {
3246 QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
3247 goto err2;
3248 }
3249
3250 /*
3251 * update_pf_params needs to be called before and after slowpath
3252 * start
3253 */
3254 qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
3255
3256 /* Setup interrupts */
3257 rc = qedf_setup_int(qedf);
3258 if (rc)
3259 goto err3;
3260
3261 rc = qed_ops->start(qedf->cdev, &qedf->tasks);
3262 if (rc) {
3263 QEDF_ERR(&(qedf->dbg_ctx), "Cannot start FCoE function.\n");
3264 goto err4;
3265 }
3266 task_start = qedf_get_task_mem(&qedf->tasks, 0);
3267 task_end = qedf_get_task_mem(&qedf->tasks, MAX_TID_BLOCKS_FCOE - 1);
3268 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Task context start=%p, "
3269 "end=%p block_size=%u.\n", task_start, task_end,
3270 qedf->tasks.size);
3271
3272 /*
3273 * We need to write the number of BDs in the BDQ we've preallocated so
3274 * the f/w will do a prefetch and we'll get an unsolicited CQE when a
3275 * packet arrives.
3276 */
3277 qedf->bdq_prod_idx = QEDF_BDQ_SIZE;
3278 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3279 "Writing %d to primary and secondary BDQ doorbell registers.\n",
3280 qedf->bdq_prod_idx);
3281 writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
3282 tmp = readw(qedf->bdq_primary_prod);
3283 writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
3284 tmp = readw(qedf->bdq_secondary_prod);
3285
3286 qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
3287
3288 /* Now that the dev_info struct has been filled in set the MAC
3289 * address
3290 */
3291 ether_addr_copy(qedf->mac, qedf->dev_info.common.hw_mac);
3292 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "MAC address is %pM.\n",
3293 qedf->mac);
3294
Chad Dupuis01fd76a2017-08-15 10:08:16 -07003295 /*
3296 * Set the WWNN and WWPN in the following way:
3297 *
3298 * If the info we get from qed is non-zero then use that to set the
3299 * WWPN and WWNN. Otherwise fall back to use fcoe_wwn_from_mac() based
3300 * on the MAC address.
3301 */
3302 if (qedf->dev_info.wwnn != 0 && qedf->dev_info.wwpn != 0) {
3303 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3304 "Setting WWPN and WWNN from qed dev_info.\n");
3305 qedf->wwnn = qedf->dev_info.wwnn;
3306 qedf->wwpn = qedf->dev_info.wwpn;
3307 } else {
3308 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3309 "Setting WWPN and WWNN using fcoe_wwn_from_mac().\n");
3310 qedf->wwnn = fcoe_wwn_from_mac(qedf->mac, 1, 0);
3311 qedf->wwpn = fcoe_wwn_from_mac(qedf->mac, 2, 0);
3312 }
Dupuis, Chad61d86582017-02-15 06:28:23 -08003313 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "WWNN=%016llx "
3314 "WWPN=%016llx.\n", qedf->wwnn, qedf->wwpn);
3315
3316 sprintf(host_buf, "host_%d", host->host_no);
Mintz, Yuval712c3cb2017-05-23 09:41:28 +03003317 qed_ops->common->set_name(qedf->cdev, host_buf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003318
Dupuis, Chad61d86582017-02-15 06:28:23 -08003319 /* Allocate cmd mgr */
3320 qedf->cmd_mgr = qedf_cmd_mgr_alloc(qedf);
3321 if (!qedf->cmd_mgr) {
3322 QEDF_ERR(&(qedf->dbg_ctx), "Failed to allocate cmd mgr.\n");
Wei Yongjune89cabf2018-01-17 12:42:41 +00003323 rc = -ENOMEM;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003324 goto err5;
3325 }
3326
3327 if (mode != QEDF_MODE_RECOVERY) {
3328 host->transportt = qedf_fc_transport_template;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003329 host->max_lun = qedf_max_lun;
3330 host->max_cmd_len = QEDF_MAX_CDB_LEN;
Chad Dupuis650ce642019-03-26 00:38:34 -07003331 host->can_queue = FCOE_PARAMS_NUM_TASKS;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003332 rc = scsi_add_host(host, &pdev->dev);
3333 if (rc)
3334 goto err6;
3335 }
3336
3337 memset(&params, 0, sizeof(params));
3338 params.mtu = 9000;
3339 ether_addr_copy(params.ll2_mac_address, qedf->mac);
3340
3341 /* Start LL2 processing thread */
3342 snprintf(host_buf, 20, "qedf_%d_ll2", host->host_no);
3343 qedf->ll2_recv_wq =
Chad Dupuis428ca642017-08-15 10:08:19 -07003344 create_workqueue(host_buf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003345 if (!qedf->ll2_recv_wq) {
3346 QEDF_ERR(&(qedf->dbg_ctx), "Failed to LL2 workqueue.\n");
Wei Yongjune89cabf2018-01-17 12:42:41 +00003347 rc = -ENOMEM;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003348 goto err7;
3349 }
3350
3351#ifdef CONFIG_DEBUG_FS
Arnd Bergmannd9ea4632018-02-02 14:12:18 +01003352 qedf_dbg_host_init(&(qedf->dbg_ctx), qedf_debugfs_ops,
3353 qedf_dbg_fops);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003354#endif
3355
3356 /* Start LL2 */
3357 qed_ops->ll2->register_cb_ops(qedf->cdev, &qedf_ll2_cb_ops, qedf);
3358 rc = qed_ops->ll2->start(qedf->cdev, &params);
3359 if (rc) {
3360 QEDF_ERR(&(qedf->dbg_ctx), "Could not start Light L2.\n");
3361 goto err7;
3362 }
3363 set_bit(QEDF_LL2_STARTED, &qedf->flags);
3364
Chad Dupuiscf291162017-08-15 10:08:18 -07003365 /* Set initial FIP/FCoE VLAN to NULL */
Dupuis, Chad61d86582017-02-15 06:28:23 -08003366 qedf->vlan_id = 0;
3367
3368 /*
3369 * No need to setup fcoe_ctlr or fc_lport objects during recovery since
3370 * they were not reaped during the unload process.
3371 */
3372 if (mode != QEDF_MODE_RECOVERY) {
3373 /* Setup imbedded fcoe controller */
3374 qedf_fcoe_ctlr_setup(qedf);
3375
3376 /* Setup lport */
3377 rc = qedf_lport_setup(qedf);
3378 if (rc) {
3379 QEDF_ERR(&(qedf->dbg_ctx),
3380 "qedf_lport_setup failed.\n");
3381 goto err7;
3382 }
3383 }
3384
3385 sprintf(host_buf, "qedf_%u_timer", qedf->lport->host->host_no);
3386 qedf->timer_work_queue =
Chad Dupuis428ca642017-08-15 10:08:19 -07003387 create_workqueue(host_buf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003388 if (!qedf->timer_work_queue) {
3389 QEDF_ERR(&(qedf->dbg_ctx), "Failed to start timer "
3390 "workqueue.\n");
Wei Yongjune89cabf2018-01-17 12:42:41 +00003391 rc = -ENOMEM;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003392 goto err7;
3393 }
3394
3395 /* DPC workqueue is not reaped during recovery unload */
3396 if (mode != QEDF_MODE_RECOVERY) {
3397 sprintf(host_buf, "qedf_%u_dpc",
3398 qedf->lport->host->host_no);
Chad Dupuis428ca642017-08-15 10:08:19 -07003399 qedf->dpc_wq = create_workqueue(host_buf);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003400 }
3401
3402 /*
3403 * GRC dump and sysfs parameters are not reaped during the recovery
3404 * unload process.
3405 */
3406 if (mode != QEDF_MODE_RECOVERY) {
Chad Dupuis4b9b7fa2018-04-25 06:08:58 -07003407 qedf->grcdump_size =
3408 qed_ops->common->dbg_all_data_size(qedf->cdev);
Dupuis, Chad61d86582017-02-15 06:28:23 -08003409 if (qedf->grcdump_size) {
3410 rc = qedf_alloc_grc_dump_buf(&qedf->grcdump,
3411 qedf->grcdump_size);
3412 if (rc) {
3413 QEDF_ERR(&(qedf->dbg_ctx),
3414 "GRC Dump buffer alloc failed.\n");
3415 qedf->grcdump = NULL;
3416 }
3417
3418 QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3419 "grcdump: addr=%p, size=%u.\n",
3420 qedf->grcdump, qedf->grcdump_size);
3421 }
3422 qedf_create_sysfs_ctx_attr(qedf);
3423
3424 /* Initialize I/O tracing for this adapter */
3425 spin_lock_init(&qedf->io_trace_lock);
3426 qedf->io_trace_idx = 0;
3427 }
3428
3429 init_completion(&qedf->flogi_compl);
3430
Saurav Kashyap6ac17472018-07-05 07:01:33 -07003431 status = qed_ops->common->update_drv_state(qedf->cdev, true);
3432 if (status)
3433 QEDF_ERR(&(qedf->dbg_ctx),
3434 "Failed to send drv state to MFW.\n");
3435
Dupuis, Chad61d86582017-02-15 06:28:23 -08003436 memset(&link_params, 0, sizeof(struct qed_link_params));
3437 link_params.link_up = true;
3438 status = qed_ops->common->set_link(qedf->cdev, &link_params);
3439 if (status)
3440 QEDF_WARN(&(qedf->dbg_ctx), "set_link failed.\n");
3441
3442 /* Start/restart discovery */
3443 if (mode == QEDF_MODE_RECOVERY)
3444 fcoe_ctlr_link_up(&qedf->ctlr);
3445 else
3446 fc_fabric_login(lport);
3447
3448 /* All good */
3449 return 0;
3450
3451err7:
3452 if (qedf->ll2_recv_wq)
3453 destroy_workqueue(qedf->ll2_recv_wq);
3454 fc_remove_host(qedf->lport->host);
3455 scsi_remove_host(qedf->lport->host);
3456#ifdef CONFIG_DEBUG_FS
3457 qedf_dbg_host_exit(&(qedf->dbg_ctx));
3458#endif
3459err6:
3460 qedf_cmd_mgr_free(qedf->cmd_mgr);
3461err5:
3462 qed_ops->stop(qedf->cdev);
3463err4:
3464 qedf_free_fcoe_pf_param(qedf);
3465 qedf_sync_free_irqs(qedf);
3466err3:
3467 qed_ops->common->slowpath_stop(qedf->cdev);
3468err2:
3469 qed_ops->common->remove(qedf->cdev);
3470err1:
3471 scsi_host_put(lport->host);
3472err0:
3473 return rc;
3474}
3475
3476static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3477{
3478 return __qedf_probe(pdev, QEDF_MODE_NORMAL);
3479}
3480
3481static void __qedf_remove(struct pci_dev *pdev, int mode)
3482{
3483 struct qedf_ctx *qedf;
Saurav Kashyap6ac17472018-07-05 07:01:33 -07003484 int rc;
Dupuis, Chad61d86582017-02-15 06:28:23 -08003485
3486 if (!pdev) {
3487 QEDF_ERR(NULL, "pdev is NULL.\n");
3488 return;
3489 }
3490
3491 qedf = pci_get_drvdata(pdev);
3492
3493 /*
3494 * Prevent race where we're in board disable work and then try to
3495 * rmmod the module.
3496 */
3497 if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
3498 QEDF_ERR(&qedf->dbg_ctx, "Already removing PCI function.\n");
3499 return;
3500 }
3501
3502 if (mode != QEDF_MODE_RECOVERY)
3503 set_bit(QEDF_UNLOADING, &qedf->flags);
3504
3505 /* Logoff the fabric to upload all connections */
3506 if (mode == QEDF_MODE_RECOVERY)
3507 fcoe_ctlr_link_down(&qedf->ctlr);
3508 else
3509 fc_fabric_logoff(qedf->lport);
Chad Dupuisa66c6cd22019-03-26 00:38:47 -07003510
3511 if (qedf_wait_for_upload(qedf) == false)
3512 QEDF_ERR(&qedf->dbg_ctx, "Could not upload all sessions.\n");
Dupuis, Chad61d86582017-02-15 06:28:23 -08003513
3514#ifdef CONFIG_DEBUG_FS
3515 qedf_dbg_host_exit(&(qedf->dbg_ctx));
3516#endif
3517
3518 /* Stop any link update handling */
3519 cancel_delayed_work_sync(&qedf->link_update);
3520 destroy_workqueue(qedf->link_update_wq);
3521 qedf->link_update_wq = NULL;
3522
3523 if (qedf->timer_work_queue)
3524 destroy_workqueue(qedf->timer_work_queue);
3525
3526 /* Stop Light L2 */
3527 clear_bit(QEDF_LL2_STARTED, &qedf->flags);
3528 qed_ops->ll2->stop(qedf->cdev);
3529 if (qedf->ll2_recv_wq)
3530 destroy_workqueue(qedf->ll2_recv_wq);
3531
3532 /* Stop fastpath */
3533 qedf_sync_free_irqs(qedf);
3534 qedf_destroy_sb(qedf);
3535
3536 /*
3537 * During recovery don't destroy OS constructs that represent the
3538 * physical port.
3539 */
3540 if (mode != QEDF_MODE_RECOVERY) {
3541 qedf_free_grc_dump_buf(&qedf->grcdump);
3542 qedf_remove_sysfs_ctx_attr(qedf);
3543
3544 /* Remove all SCSI/libfc/libfcoe structures */
3545 fcoe_ctlr_destroy(&qedf->ctlr);
3546 fc_lport_destroy(qedf->lport);
3547 fc_remove_host(qedf->lport->host);
3548 scsi_remove_host(qedf->lport->host);
3549 }
3550
3551 qedf_cmd_mgr_free(qedf->cmd_mgr);
3552
3553 if (mode != QEDF_MODE_RECOVERY) {
3554 fc_exch_mgr_free(qedf->lport);
3555 fc_lport_free_stats(qedf->lport);
3556
3557 /* Wait for all vports to be reaped */
3558 qedf_wait_for_vport_destroy(qedf);
3559 }
3560
3561 /*
3562 * Now that all connections have been uploaded we can stop the
3563 * rest of the qed operations
3564 */
3565 qed_ops->stop(qedf->cdev);
3566
3567 if (mode != QEDF_MODE_RECOVERY) {
3568 if (qedf->dpc_wq) {
3569 /* Stop general DPC handling */
3570 destroy_workqueue(qedf->dpc_wq);
3571 qedf->dpc_wq = NULL;
3572 }
3573 }
3574
3575 /* Final shutdown for the board */
3576 qedf_free_fcoe_pf_param(qedf);
3577 if (mode != QEDF_MODE_RECOVERY) {
3578 qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
3579 pci_set_drvdata(pdev, NULL);
3580 }
Saurav Kashyap6ac17472018-07-05 07:01:33 -07003581
3582 rc = qed_ops->common->update_drv_state(qedf->cdev, false);
3583 if (rc)
3584 QEDF_ERR(&(qedf->dbg_ctx),
3585 "Failed to send drv state to MFW.\n");
3586
Dupuis, Chad61d86582017-02-15 06:28:23 -08003587 qed_ops->common->slowpath_stop(qedf->cdev);
3588 qed_ops->common->remove(qedf->cdev);
3589
3590 mempool_destroy(qedf->io_mempool);
3591
3592 /* Only reap the Scsi_host on a real removal */
3593 if (mode != QEDF_MODE_RECOVERY)
3594 scsi_host_put(qedf->lport->host);
3595}
3596
3597static void qedf_remove(struct pci_dev *pdev)
3598{
3599 /* Check to make sure this function wasn't already disabled */
3600 if (!atomic_read(&pdev->enable_cnt))
3601 return;
3602
3603 __qedf_remove(pdev, QEDF_MODE_NORMAL);
3604}
3605
Chad Dupuis4b9b7fa2018-04-25 06:08:58 -07003606void qedf_wq_grcdump(struct work_struct *work)
3607{
3608 struct qedf_ctx *qedf =
3609 container_of(work, struct qedf_ctx, grcdump_work.work);
3610
3611 QEDF_ERR(&(qedf->dbg_ctx), "Collecting GRC dump.\n");
3612 qedf_capture_grc_dump(qedf);
3613}
3614
Dupuis, Chad61d86582017-02-15 06:28:23 -08003615/*
Chad Dupuis642a0b32018-05-22 00:28:43 -07003616 * Protocol TLV handler
3617 */
3618void qedf_get_protocol_tlv_data(void *dev, void *data)
3619{
3620 struct qedf_ctx *qedf = dev;
3621 struct qed_mfw_tlv_fcoe *fcoe = data;
3622 struct fc_lport *lport = qedf->lport;
3623 struct Scsi_Host *host = lport->host;
3624 struct fc_host_attrs *fc_host = shost_to_fc_host(host);
3625 struct fc_host_statistics *hst;
3626
3627 /* Force a refresh of the fc_host stats including offload stats */
3628 hst = qedf_fc_get_host_stats(host);
3629
3630 fcoe->qos_pri_set = true;
3631 fcoe->qos_pri = 3; /* Hard coded to 3 in driver */
3632
3633 fcoe->ra_tov_set = true;
3634 fcoe->ra_tov = lport->r_a_tov;
3635
3636 fcoe->ed_tov_set = true;
3637 fcoe->ed_tov = lport->e_d_tov;
3638
3639 fcoe->npiv_state_set = true;
3640 fcoe->npiv_state = 1; /* NPIV always enabled */
3641
3642 fcoe->num_npiv_ids_set = true;
3643 fcoe->num_npiv_ids = fc_host->npiv_vports_inuse;
3644
3645 /* Certain attributes we only want to set if we've selected an FCF */
3646 if (qedf->ctlr.sel_fcf) {
3647 fcoe->switch_name_set = true;
3648 u64_to_wwn(qedf->ctlr.sel_fcf->switch_name, fcoe->switch_name);
3649 }
3650
3651 fcoe->port_state_set = true;
3652 /* For qedf we're either link down or fabric attach */
3653 if (lport->link_up)
3654 fcoe->port_state = QED_MFW_TLV_PORT_STATE_FABRIC;
3655 else
3656 fcoe->port_state = QED_MFW_TLV_PORT_STATE_OFFLINE;
3657
3658 fcoe->link_failures_set = true;
3659 fcoe->link_failures = (u16)hst->link_failure_count;
3660
3661 fcoe->fcoe_txq_depth_set = true;
3662 fcoe->fcoe_rxq_depth_set = true;
3663 fcoe->fcoe_rxq_depth = FCOE_PARAMS_NUM_TASKS;
3664 fcoe->fcoe_txq_depth = FCOE_PARAMS_NUM_TASKS;
3665
3666 fcoe->fcoe_rx_frames_set = true;
3667 fcoe->fcoe_rx_frames = hst->rx_frames;
3668
3669 fcoe->fcoe_tx_frames_set = true;
3670 fcoe->fcoe_tx_frames = hst->tx_frames;
3671
3672 fcoe->fcoe_rx_bytes_set = true;
3673 fcoe->fcoe_rx_bytes = hst->fcp_input_megabytes * 1000000;
3674
3675 fcoe->fcoe_tx_bytes_set = true;
3676 fcoe->fcoe_tx_bytes = hst->fcp_output_megabytes * 1000000;
3677
3678 fcoe->crc_count_set = true;
3679 fcoe->crc_count = hst->invalid_crc_count;
3680
3681 fcoe->tx_abts_set = true;
3682 fcoe->tx_abts = hst->fcp_packet_aborts;
3683
3684 fcoe->tx_lun_rst_set = true;
3685 fcoe->tx_lun_rst = qedf->lun_resets;
3686
3687 fcoe->abort_task_sets_set = true;
3688 fcoe->abort_task_sets = qedf->packet_aborts;
3689
3690 fcoe->scsi_busy_set = true;
3691 fcoe->scsi_busy = qedf->busy;
3692
3693 fcoe->scsi_tsk_full_set = true;
3694 fcoe->scsi_tsk_full = qedf->task_set_fulls;
3695}
3696
Chad Dupuis8673daf2018-05-22 00:28:44 -07003697/* Generic TLV data callback */
3698void qedf_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
3699{
3700 struct qedf_ctx *qedf;
3701
3702 if (!dev) {
3703 QEDF_INFO(NULL, QEDF_LOG_EVT,
3704 "dev is NULL so ignoring get_generic_tlv_data request.\n");
3705 return;
3706 }
3707 qedf = (struct qedf_ctx *)dev;
3708
3709 memset(data, 0, sizeof(struct qed_generic_tlvs));
3710 ether_addr_copy(data->mac[0], qedf->mac);
3711}
3712
Chad Dupuis642a0b32018-05-22 00:28:43 -07003713/*
Dupuis, Chad61d86582017-02-15 06:28:23 -08003714 * Module Init/Remove
3715 */
3716
3717static int __init qedf_init(void)
3718{
3719 int ret;
3720
3721 /* If debug=1 passed, set the default log mask */
3722 if (qedf_debug == QEDF_LOG_DEFAULT)
3723 qedf_debug = QEDF_DEFAULT_LOG_MASK;
3724
Chad Dupuis84b2ba62018-04-25 06:08:52 -07003725 /*
3726 * Check that default prio for FIP/FCoE traffic is between 0..7 if a
3727 * value has been set
3728 */
3729 if (qedf_default_prio > -1)
3730 if (qedf_default_prio > 7) {
3731 qedf_default_prio = QEDF_DEFAULT_PRIO;
3732 QEDF_ERR(NULL, "FCoE/FIP priority out of range, resetting to %d.\n",
3733 QEDF_DEFAULT_PRIO);
3734 }
Chad Dupuisa93755c2018-04-25 06:08:50 -07003735
Dupuis, Chad61d86582017-02-15 06:28:23 -08003736 /* Print driver banner */
3737 QEDF_INFO(NULL, QEDF_LOG_INFO, "%s v%s.\n", QEDF_DESCR,
3738 QEDF_VERSION);
3739
3740 /* Create kmem_cache for qedf_io_work structs */
3741 qedf_io_work_cache = kmem_cache_create("qedf_io_work_cache",
3742 sizeof(struct qedf_io_work), 0, SLAB_HWCACHE_ALIGN, NULL);
3743 if (qedf_io_work_cache == NULL) {
3744 QEDF_ERR(NULL, "qedf_io_work_cache is NULL.\n");
3745 goto err1;
3746 }
3747 QEDF_INFO(NULL, QEDF_LOG_DISC, "qedf_io_work_cache=%p.\n",
3748 qedf_io_work_cache);
3749
3750 qed_ops = qed_get_fcoe_ops();
3751 if (!qed_ops) {
3752 QEDF_ERR(NULL, "Failed to get qed fcoe operations\n");
3753 goto err1;
3754 }
3755
3756#ifdef CONFIG_DEBUG_FS
3757 qedf_dbg_init("qedf");
3758#endif
3759
3760 qedf_fc_transport_template =
3761 fc_attach_transport(&qedf_fc_transport_fn);
3762 if (!qedf_fc_transport_template) {
3763 QEDF_ERR(NULL, "Could not register with FC transport\n");
3764 goto err2;
3765 }
3766
3767 qedf_fc_vport_transport_template =
3768 fc_attach_transport(&qedf_fc_vport_transport_fn);
3769 if (!qedf_fc_vport_transport_template) {
3770 QEDF_ERR(NULL, "Could not register vport template with FC "
3771 "transport\n");
3772 goto err3;
3773 }
3774
3775 qedf_io_wq = create_workqueue("qedf_io_wq");
3776 if (!qedf_io_wq) {
3777 QEDF_ERR(NULL, "Could not create qedf_io_wq.\n");
3778 goto err4;
3779 }
3780
3781 qedf_cb_ops.get_login_failures = qedf_get_login_failures;
3782
3783 ret = pci_register_driver(&qedf_pci_driver);
3784 if (ret) {
3785 QEDF_ERR(NULL, "Failed to register driver\n");
3786 goto err5;
3787 }
3788
3789 return 0;
3790
3791err5:
3792 destroy_workqueue(qedf_io_wq);
3793err4:
3794 fc_release_transport(qedf_fc_vport_transport_template);
3795err3:
3796 fc_release_transport(qedf_fc_transport_template);
3797err2:
3798#ifdef CONFIG_DEBUG_FS
3799 qedf_dbg_exit();
3800#endif
3801 qed_put_fcoe_ops();
3802err1:
3803 return -EINVAL;
3804}
3805
3806static void __exit qedf_cleanup(void)
3807{
3808 pci_unregister_driver(&qedf_pci_driver);
3809
3810 destroy_workqueue(qedf_io_wq);
3811
3812 fc_release_transport(qedf_fc_vport_transport_template);
3813 fc_release_transport(qedf_fc_transport_template);
3814#ifdef CONFIG_DEBUG_FS
3815 qedf_dbg_exit();
3816#endif
3817 qed_put_fcoe_ops();
3818
3819 kmem_cache_destroy(qedf_io_work_cache);
3820}
3821
3822MODULE_LICENSE("GPL");
3823MODULE_DESCRIPTION("QLogic QEDF 25/40/50/100Gb FCoE Driver");
3824MODULE_AUTHOR("QLogic Corporation");
3825MODULE_VERSION(QEDF_VERSION);
3826module_init(qedf_init);
3827module_exit(qedf_cleanup);