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