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