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