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