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