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