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