]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/qedi/qedi_main.c
ufs_truncate_blocks(): fix the case when size is in the last direct block
[karo-tx-linux.git] / drivers / scsi / qedi / qedi_main.c
1 /*
2  * QLogic iSCSI 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
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/kernel.h>
13 #include <linux/if_arp.h>
14 #include <scsi/iscsi_if.h>
15 #include <linux/inet.h>
16 #include <net/arp.h>
17 #include <linux/list.h>
18 #include <linux/kthread.h>
19 #include <linux/mm.h>
20 #include <linux/if_vlan.h>
21 #include <linux/cpu.h>
22
23 #include <scsi/scsi_cmnd.h>
24 #include <scsi/scsi_device.h>
25 #include <scsi/scsi_eh.h>
26 #include <scsi/scsi_host.h>
27 #include <scsi/scsi.h>
28
29 #include "qedi.h"
30 #include "qedi_gbl.h"
31 #include "qedi_iscsi.h"
32
33 static uint qedi_fw_debug;
34 module_param(qedi_fw_debug, uint, 0644);
35 MODULE_PARM_DESC(qedi_fw_debug, " Firmware debug level 0(default) to 3");
36
37 uint qedi_dbg_log = QEDI_LOG_WARN | QEDI_LOG_SCSI_TM;
38 module_param(qedi_dbg_log, uint, 0644);
39 MODULE_PARM_DESC(qedi_dbg_log, " Default debug level");
40
41 uint qedi_io_tracing;
42 module_param(qedi_io_tracing, uint, 0644);
43 MODULE_PARM_DESC(qedi_io_tracing,
44                  " Enable logging of SCSI requests/completions into trace buffer. (default off).");
45
46 const struct qed_iscsi_ops *qedi_ops;
47 static struct scsi_transport_template *qedi_scsi_transport;
48 static struct pci_driver qedi_pci_driver;
49 static DEFINE_PER_CPU(struct qedi_percpu_s, qedi_percpu);
50 static LIST_HEAD(qedi_udev_list);
51 /* Static function declaration */
52 static int qedi_alloc_global_queues(struct qedi_ctx *qedi);
53 static void qedi_free_global_queues(struct qedi_ctx *qedi);
54 static struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid);
55 static void qedi_reset_uio_rings(struct qedi_uio_dev *udev);
56 static void qedi_ll2_free_skbs(struct qedi_ctx *qedi);
57
58 static int qedi_iscsi_event_cb(void *context, u8 fw_event_code, void *fw_handle)
59 {
60         struct qedi_ctx *qedi;
61         struct qedi_endpoint *qedi_ep;
62         struct async_data *data;
63         int rval = 0;
64
65         if (!context || !fw_handle) {
66                 QEDI_ERR(NULL, "Recv event with ctx NULL\n");
67                 return -EINVAL;
68         }
69
70         qedi = (struct qedi_ctx *)context;
71         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
72                   "Recv Event %d fw_handle %p\n", fw_event_code, fw_handle);
73
74         data = (struct async_data *)fw_handle;
75         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
76                   "cid=0x%x tid=0x%x err-code=0x%x fw-dbg-param=0x%x\n",
77                    data->cid, data->itid, data->error_code,
78                    data->fw_debug_param);
79
80         qedi_ep = qedi->ep_tbl[data->cid];
81
82         if (!qedi_ep) {
83                 QEDI_WARN(&qedi->dbg_ctx,
84                           "Cannot process event, ep already disconnected, cid=0x%x\n",
85                            data->cid);
86                 WARN_ON(1);
87                 return -ENODEV;
88         }
89
90         switch (fw_event_code) {
91         case ISCSI_EVENT_TYPE_ASYN_CONNECT_COMPLETE:
92                 if (qedi_ep->state == EP_STATE_OFLDCONN_START)
93                         qedi_ep->state = EP_STATE_OFLDCONN_COMPL;
94
95                 wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
96                 break;
97         case ISCSI_EVENT_TYPE_ASYN_TERMINATE_DONE:
98                 qedi_ep->state = EP_STATE_DISCONN_COMPL;
99                 wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
100                 break;
101         case ISCSI_EVENT_TYPE_ISCSI_CONN_ERROR:
102                 qedi_process_iscsi_error(qedi_ep, data);
103                 break;
104         case ISCSI_EVENT_TYPE_ASYN_ABORT_RCVD:
105         case ISCSI_EVENT_TYPE_ASYN_SYN_RCVD:
106         case ISCSI_EVENT_TYPE_ASYN_MAX_RT_TIME:
107         case ISCSI_EVENT_TYPE_ASYN_MAX_RT_CNT:
108         case ISCSI_EVENT_TYPE_ASYN_MAX_KA_PROBES_CNT:
109         case ISCSI_EVENT_TYPE_ASYN_FIN_WAIT2:
110         case ISCSI_EVENT_TYPE_TCP_CONN_ERROR:
111                 qedi_process_tcp_error(qedi_ep, data);
112                 break;
113         default:
114                 QEDI_ERR(&qedi->dbg_ctx, "Recv Unknown Event %u\n",
115                          fw_event_code);
116         }
117
118         return rval;
119 }
120
121 static int qedi_uio_open(struct uio_info *uinfo, struct inode *inode)
122 {
123         struct qedi_uio_dev *udev = uinfo->priv;
124         struct qedi_ctx *qedi = udev->qedi;
125
126         if (!capable(CAP_NET_ADMIN))
127                 return -EPERM;
128
129         if (udev->uio_dev != -1)
130                 return -EBUSY;
131
132         rtnl_lock();
133         udev->uio_dev = iminor(inode);
134         qedi_reset_uio_rings(udev);
135         set_bit(UIO_DEV_OPENED, &qedi->flags);
136         rtnl_unlock();
137
138         return 0;
139 }
140
141 static int qedi_uio_close(struct uio_info *uinfo, struct inode *inode)
142 {
143         struct qedi_uio_dev *udev = uinfo->priv;
144         struct qedi_ctx *qedi = udev->qedi;
145
146         udev->uio_dev = -1;
147         clear_bit(UIO_DEV_OPENED, &qedi->flags);
148         qedi_ll2_free_skbs(qedi);
149         return 0;
150 }
151
152 static void __qedi_free_uio_rings(struct qedi_uio_dev *udev)
153 {
154         if (udev->ll2_ring) {
155                 free_page((unsigned long)udev->ll2_ring);
156                 udev->ll2_ring = NULL;
157         }
158
159         if (udev->ll2_buf) {
160                 free_pages((unsigned long)udev->ll2_buf, 2);
161                 udev->ll2_buf = NULL;
162         }
163 }
164
165 static void __qedi_free_uio(struct qedi_uio_dev *udev)
166 {
167         uio_unregister_device(&udev->qedi_uinfo);
168
169         __qedi_free_uio_rings(udev);
170
171         pci_dev_put(udev->pdev);
172         kfree(udev->uctrl);
173         kfree(udev);
174 }
175
176 static void qedi_free_uio(struct qedi_uio_dev *udev)
177 {
178         if (!udev)
179                 return;
180
181         list_del_init(&udev->list);
182         __qedi_free_uio(udev);
183 }
184
185 static void qedi_reset_uio_rings(struct qedi_uio_dev *udev)
186 {
187         struct qedi_ctx *qedi = NULL;
188         struct qedi_uio_ctrl *uctrl = NULL;
189
190         qedi = udev->qedi;
191         uctrl = udev->uctrl;
192
193         spin_lock_bh(&qedi->ll2_lock);
194         uctrl->host_rx_cons = 0;
195         uctrl->hw_rx_prod = 0;
196         uctrl->hw_rx_bd_prod = 0;
197         uctrl->host_rx_bd_cons = 0;
198
199         memset(udev->ll2_ring, 0, udev->ll2_ring_size);
200         memset(udev->ll2_buf, 0, udev->ll2_buf_size);
201         spin_unlock_bh(&qedi->ll2_lock);
202 }
203
204 static int __qedi_alloc_uio_rings(struct qedi_uio_dev *udev)
205 {
206         int rc = 0;
207
208         if (udev->ll2_ring || udev->ll2_buf)
209                 return rc;
210
211         /* Allocating memory for LL2 ring  */
212         udev->ll2_ring_size = QEDI_PAGE_SIZE;
213         udev->ll2_ring = (void *)get_zeroed_page(GFP_KERNEL | __GFP_COMP);
214         if (!udev->ll2_ring) {
215                 rc = -ENOMEM;
216                 goto exit_alloc_ring;
217         }
218
219         /* Allocating memory for Tx/Rx pkt buffer */
220         udev->ll2_buf_size = TX_RX_RING * LL2_SINGLE_BUF_SIZE;
221         udev->ll2_buf_size = QEDI_PAGE_ALIGN(udev->ll2_buf_size);
222         udev->ll2_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_COMP |
223                                                  __GFP_ZERO, 2);
224         if (!udev->ll2_buf) {
225                 rc = -ENOMEM;
226                 goto exit_alloc_buf;
227         }
228         return rc;
229
230 exit_alloc_buf:
231         free_page((unsigned long)udev->ll2_ring);
232         udev->ll2_ring = NULL;
233 exit_alloc_ring:
234         return rc;
235 }
236
237 static int qedi_alloc_uio_rings(struct qedi_ctx *qedi)
238 {
239         struct qedi_uio_dev *udev = NULL;
240         struct qedi_uio_ctrl *uctrl = NULL;
241         int rc = 0;
242
243         list_for_each_entry(udev, &qedi_udev_list, list) {
244                 if (udev->pdev == qedi->pdev) {
245                         udev->qedi = qedi;
246                         if (__qedi_alloc_uio_rings(udev)) {
247                                 udev->qedi = NULL;
248                                 return -ENOMEM;
249                         }
250                         qedi->udev = udev;
251                         return 0;
252                 }
253         }
254
255         udev = kzalloc(sizeof(*udev), GFP_KERNEL);
256         if (!udev) {
257                 rc = -ENOMEM;
258                 goto err_udev;
259         }
260
261         uctrl = kzalloc(sizeof(*uctrl), GFP_KERNEL);
262         if (!uctrl) {
263                 rc = -ENOMEM;
264                 goto err_uctrl;
265         }
266
267         udev->uio_dev = -1;
268
269         udev->qedi = qedi;
270         udev->pdev = qedi->pdev;
271         udev->uctrl = uctrl;
272
273         rc = __qedi_alloc_uio_rings(udev);
274         if (rc)
275                 goto err_uio_rings;
276
277         list_add(&udev->list, &qedi_udev_list);
278
279         pci_dev_get(udev->pdev);
280         qedi->udev = udev;
281
282         udev->tx_pkt = udev->ll2_buf;
283         udev->rx_pkt = udev->ll2_buf + LL2_SINGLE_BUF_SIZE;
284         return 0;
285
286  err_uio_rings:
287         kfree(uctrl);
288  err_uctrl:
289         kfree(udev);
290  err_udev:
291         return -ENOMEM;
292 }
293
294 static int qedi_init_uio(struct qedi_ctx *qedi)
295 {
296         struct qedi_uio_dev *udev = qedi->udev;
297         struct uio_info *uinfo;
298         int ret = 0;
299
300         if (!udev)
301                 return -ENOMEM;
302
303         uinfo = &udev->qedi_uinfo;
304
305         uinfo->mem[0].addr = (unsigned long)udev->uctrl;
306         uinfo->mem[0].size = sizeof(struct qedi_uio_ctrl);
307         uinfo->mem[0].memtype = UIO_MEM_LOGICAL;
308
309         uinfo->mem[1].addr = (unsigned long)udev->ll2_ring;
310         uinfo->mem[1].size = udev->ll2_ring_size;
311         uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
312
313         uinfo->mem[2].addr = (unsigned long)udev->ll2_buf;
314         uinfo->mem[2].size = udev->ll2_buf_size;
315         uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
316
317         uinfo->name = "qedi_uio";
318         uinfo->version = QEDI_MODULE_VERSION;
319         uinfo->irq = UIO_IRQ_CUSTOM;
320
321         uinfo->open = qedi_uio_open;
322         uinfo->release = qedi_uio_close;
323
324         if (udev->uio_dev == -1) {
325                 if (!uinfo->priv) {
326                         uinfo->priv = udev;
327
328                         ret = uio_register_device(&udev->pdev->dev, uinfo);
329                         if (ret) {
330                                 QEDI_ERR(&qedi->dbg_ctx,
331                                          "UIO registration failed\n");
332                         }
333                 }
334         }
335
336         return ret;
337 }
338
339 static int qedi_alloc_and_init_sb(struct qedi_ctx *qedi,
340                                   struct qed_sb_info *sb_info, u16 sb_id)
341 {
342         struct status_block *sb_virt;
343         dma_addr_t sb_phys;
344         int ret;
345
346         sb_virt = dma_alloc_coherent(&qedi->pdev->dev,
347                                      sizeof(struct status_block), &sb_phys,
348                                      GFP_KERNEL);
349         if (!sb_virt) {
350                 QEDI_ERR(&qedi->dbg_ctx,
351                          "Status block allocation failed for id = %d.\n",
352                           sb_id);
353                 return -ENOMEM;
354         }
355
356         ret = qedi_ops->common->sb_init(qedi->cdev, sb_info, sb_virt, sb_phys,
357                                        sb_id, QED_SB_TYPE_STORAGE);
358         if (ret) {
359                 QEDI_ERR(&qedi->dbg_ctx,
360                          "Status block initialization failed for id = %d.\n",
361                           sb_id);
362                 return ret;
363         }
364
365         return 0;
366 }
367
368 static void qedi_free_sb(struct qedi_ctx *qedi)
369 {
370         struct qed_sb_info *sb_info;
371         int id;
372
373         for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
374                 sb_info = &qedi->sb_array[id];
375                 if (sb_info->sb_virt)
376                         dma_free_coherent(&qedi->pdev->dev,
377                                           sizeof(*sb_info->sb_virt),
378                                           (void *)sb_info->sb_virt,
379                                           sb_info->sb_phys);
380         }
381 }
382
383 static void qedi_free_fp(struct qedi_ctx *qedi)
384 {
385         kfree(qedi->fp_array);
386         kfree(qedi->sb_array);
387 }
388
389 static void qedi_destroy_fp(struct qedi_ctx *qedi)
390 {
391         qedi_free_sb(qedi);
392         qedi_free_fp(qedi);
393 }
394
395 static int qedi_alloc_fp(struct qedi_ctx *qedi)
396 {
397         int ret = 0;
398
399         qedi->fp_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
400                                  sizeof(struct qedi_fastpath), GFP_KERNEL);
401         if (!qedi->fp_array) {
402                 QEDI_ERR(&qedi->dbg_ctx,
403                          "fastpath fp array allocation failed.\n");
404                 return -ENOMEM;
405         }
406
407         qedi->sb_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
408                                  sizeof(struct qed_sb_info), GFP_KERNEL);
409         if (!qedi->sb_array) {
410                 QEDI_ERR(&qedi->dbg_ctx,
411                          "fastpath sb array allocation failed.\n");
412                 ret = -ENOMEM;
413                 goto free_fp;
414         }
415
416         return ret;
417
418 free_fp:
419         qedi_free_fp(qedi);
420         return ret;
421 }
422
423 static void qedi_int_fp(struct qedi_ctx *qedi)
424 {
425         struct qedi_fastpath *fp;
426         int id;
427
428         memset(qedi->fp_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
429                sizeof(*qedi->fp_array));
430         memset(qedi->sb_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
431                sizeof(*qedi->sb_array));
432
433         for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
434                 fp = &qedi->fp_array[id];
435                 fp->sb_info = &qedi->sb_array[id];
436                 fp->sb_id = id;
437                 fp->qedi = qedi;
438                 snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
439                          "qedi", id);
440
441                 /* fp_array[i] ---- irq cookie
442                  * So init data which is needed in int ctx
443                  */
444         }
445 }
446
447 static int qedi_prepare_fp(struct qedi_ctx *qedi)
448 {
449         struct qedi_fastpath *fp;
450         int id, ret = 0;
451
452         ret = qedi_alloc_fp(qedi);
453         if (ret)
454                 goto err;
455
456         qedi_int_fp(qedi);
457
458         for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
459                 fp = &qedi->fp_array[id];
460                 ret = qedi_alloc_and_init_sb(qedi, fp->sb_info, fp->sb_id);
461                 if (ret) {
462                         QEDI_ERR(&qedi->dbg_ctx,
463                                  "SB allocation and initialization failed.\n");
464                         ret = -EIO;
465                         goto err_init;
466                 }
467         }
468
469         return 0;
470
471 err_init:
472         qedi_free_sb(qedi);
473         qedi_free_fp(qedi);
474 err:
475         return ret;
476 }
477
478 static int qedi_setup_cid_que(struct qedi_ctx *qedi)
479 {
480         int i;
481
482         qedi->cid_que.cid_que_base = kmalloc_array(qedi->max_active_conns,
483                                                    sizeof(u32), GFP_KERNEL);
484         if (!qedi->cid_que.cid_que_base)
485                 return -ENOMEM;
486
487         qedi->cid_que.conn_cid_tbl = kmalloc_array(qedi->max_active_conns,
488                                                    sizeof(struct qedi_conn *),
489                                                    GFP_KERNEL);
490         if (!qedi->cid_que.conn_cid_tbl) {
491                 kfree(qedi->cid_que.cid_que_base);
492                 qedi->cid_que.cid_que_base = NULL;
493                 return -ENOMEM;
494         }
495
496         qedi->cid_que.cid_que = (u32 *)qedi->cid_que.cid_que_base;
497         qedi->cid_que.cid_q_prod_idx = 0;
498         qedi->cid_que.cid_q_cons_idx = 0;
499         qedi->cid_que.cid_q_max_idx = qedi->max_active_conns;
500         qedi->cid_que.cid_free_cnt = qedi->max_active_conns;
501
502         for (i = 0; i < qedi->max_active_conns; i++) {
503                 qedi->cid_que.cid_que[i] = i;
504                 qedi->cid_que.conn_cid_tbl[i] = NULL;
505         }
506
507         return 0;
508 }
509
510 static void qedi_release_cid_que(struct qedi_ctx *qedi)
511 {
512         kfree(qedi->cid_que.cid_que_base);
513         qedi->cid_que.cid_que_base = NULL;
514
515         kfree(qedi->cid_que.conn_cid_tbl);
516         qedi->cid_que.conn_cid_tbl = NULL;
517 }
518
519 static int qedi_init_id_tbl(struct qedi_portid_tbl *id_tbl, u16 size,
520                             u16 start_id, u16 next)
521 {
522         id_tbl->start = start_id;
523         id_tbl->max = size;
524         id_tbl->next = next;
525         spin_lock_init(&id_tbl->lock);
526         id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
527         if (!id_tbl->table)
528                 return -ENOMEM;
529
530         return 0;
531 }
532
533 static void qedi_free_id_tbl(struct qedi_portid_tbl *id_tbl)
534 {
535         kfree(id_tbl->table);
536         id_tbl->table = NULL;
537 }
538
539 int qedi_alloc_id(struct qedi_portid_tbl *id_tbl, u16 id)
540 {
541         int ret = -1;
542
543         id -= id_tbl->start;
544         if (id >= id_tbl->max)
545                 return ret;
546
547         spin_lock(&id_tbl->lock);
548         if (!test_bit(id, id_tbl->table)) {
549                 set_bit(id, id_tbl->table);
550                 ret = 0;
551         }
552         spin_unlock(&id_tbl->lock);
553         return ret;
554 }
555
556 u16 qedi_alloc_new_id(struct qedi_portid_tbl *id_tbl)
557 {
558         u16 id;
559
560         spin_lock(&id_tbl->lock);
561         id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
562         if (id >= id_tbl->max) {
563                 id = QEDI_LOCAL_PORT_INVALID;
564                 if (id_tbl->next != 0) {
565                         id = find_first_zero_bit(id_tbl->table, id_tbl->next);
566                         if (id >= id_tbl->next)
567                                 id = QEDI_LOCAL_PORT_INVALID;
568                 }
569         }
570
571         if (id < id_tbl->max) {
572                 set_bit(id, id_tbl->table);
573                 id_tbl->next = (id + 1) & (id_tbl->max - 1);
574                 id += id_tbl->start;
575         }
576
577         spin_unlock(&id_tbl->lock);
578
579         return id;
580 }
581
582 void qedi_free_id(struct qedi_portid_tbl *id_tbl, u16 id)
583 {
584         if (id == QEDI_LOCAL_PORT_INVALID)
585                 return;
586
587         id -= id_tbl->start;
588         if (id >= id_tbl->max)
589                 return;
590
591         clear_bit(id, id_tbl->table);
592 }
593
594 static void qedi_cm_free_mem(struct qedi_ctx *qedi)
595 {
596         kfree(qedi->ep_tbl);
597         qedi->ep_tbl = NULL;
598         qedi_free_id_tbl(&qedi->lcl_port_tbl);
599 }
600
601 static int qedi_cm_alloc_mem(struct qedi_ctx *qedi)
602 {
603         u16 port_id;
604
605         qedi->ep_tbl = kzalloc((qedi->max_active_conns *
606                                 sizeof(struct qedi_endpoint *)), GFP_KERNEL);
607         if (!qedi->ep_tbl)
608                 return -ENOMEM;
609         port_id = prandom_u32() % QEDI_LOCAL_PORT_RANGE;
610         if (qedi_init_id_tbl(&qedi->lcl_port_tbl, QEDI_LOCAL_PORT_RANGE,
611                              QEDI_LOCAL_PORT_MIN, port_id)) {
612                 qedi_cm_free_mem(qedi);
613                 return -ENOMEM;
614         }
615
616         return 0;
617 }
618
619 static struct qedi_ctx *qedi_host_alloc(struct pci_dev *pdev)
620 {
621         struct Scsi_Host *shost;
622         struct qedi_ctx *qedi = NULL;
623
624         shost = iscsi_host_alloc(&qedi_host_template,
625                                  sizeof(struct qedi_ctx), 0);
626         if (!shost) {
627                 QEDI_ERR(NULL, "Could not allocate shost\n");
628                 goto exit_setup_shost;
629         }
630
631         shost->max_id = QEDI_MAX_ISCSI_CONNS_PER_HBA;
632         shost->max_channel = 0;
633         shost->max_lun = ~0;
634         shost->max_cmd_len = 16;
635         shost->transportt = qedi_scsi_transport;
636
637         qedi = iscsi_host_priv(shost);
638         memset(qedi, 0, sizeof(*qedi));
639         qedi->shost = shost;
640         qedi->dbg_ctx.host_no = shost->host_no;
641         qedi->pdev = pdev;
642         qedi->dbg_ctx.pdev = pdev;
643         qedi->max_active_conns = ISCSI_MAX_SESS_PER_HBA;
644         qedi->max_sqes = QEDI_SQ_SIZE;
645
646         if (shost_use_blk_mq(shost))
647                 shost->nr_hw_queues = MIN_NUM_CPUS_MSIX(qedi);
648
649         pci_set_drvdata(pdev, qedi);
650
651 exit_setup_shost:
652         return qedi;
653 }
654
655 static int qedi_ll2_rx(void *cookie, struct sk_buff *skb, u32 arg1, u32 arg2)
656 {
657         struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
658         struct qedi_uio_dev *udev;
659         struct qedi_uio_ctrl *uctrl;
660         struct skb_work_list *work;
661         u32 prod;
662
663         if (!qedi) {
664                 QEDI_ERR(NULL, "qedi is NULL\n");
665                 return -1;
666         }
667
668         if (!test_bit(UIO_DEV_OPENED, &qedi->flags)) {
669                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_UIO,
670                           "UIO DEV is not opened\n");
671                 kfree_skb(skb);
672                 return 0;
673         }
674
675         udev = qedi->udev;
676         uctrl = udev->uctrl;
677
678         work = kzalloc(sizeof(*work), GFP_ATOMIC);
679         if (!work) {
680                 QEDI_WARN(&qedi->dbg_ctx,
681                           "Could not allocate work so dropping frame.\n");
682                 kfree_skb(skb);
683                 return 0;
684         }
685
686         INIT_LIST_HEAD(&work->list);
687         work->skb = skb;
688
689         if (skb_vlan_tag_present(skb))
690                 work->vlan_id = skb_vlan_tag_get(skb);
691
692         if (work->vlan_id)
693                 __vlan_insert_tag(work->skb, htons(ETH_P_8021Q), work->vlan_id);
694
695         spin_lock_bh(&qedi->ll2_lock);
696         list_add_tail(&work->list, &qedi->ll2_skb_list);
697
698         ++uctrl->hw_rx_prod_cnt;
699         prod = (uctrl->hw_rx_prod + 1) % RX_RING;
700         if (prod != uctrl->host_rx_cons) {
701                 uctrl->hw_rx_prod = prod;
702                 spin_unlock_bh(&qedi->ll2_lock);
703                 wake_up_process(qedi->ll2_recv_thread);
704                 return 0;
705         }
706
707         spin_unlock_bh(&qedi->ll2_lock);
708         return 0;
709 }
710
711 /* map this skb to iscsiuio mmaped region */
712 static int qedi_ll2_process_skb(struct qedi_ctx *qedi, struct sk_buff *skb,
713                                 u16 vlan_id)
714 {
715         struct qedi_uio_dev *udev = NULL;
716         struct qedi_uio_ctrl *uctrl = NULL;
717         struct qedi_rx_bd rxbd;
718         struct qedi_rx_bd *p_rxbd;
719         u32 rx_bd_prod;
720         void *pkt;
721         int len = 0;
722
723         if (!qedi) {
724                 QEDI_ERR(NULL, "qedi is NULL\n");
725                 return -1;
726         }
727
728         udev = qedi->udev;
729         uctrl = udev->uctrl;
730         pkt = udev->rx_pkt + (uctrl->hw_rx_prod * LL2_SINGLE_BUF_SIZE);
731         len = min_t(u32, skb->len, (u32)LL2_SINGLE_BUF_SIZE);
732         memcpy(pkt, skb->data, len);
733
734         memset(&rxbd, 0, sizeof(rxbd));
735         rxbd.rx_pkt_index = uctrl->hw_rx_prod;
736         rxbd.rx_pkt_len = len;
737         rxbd.vlan_id = vlan_id;
738
739         uctrl->hw_rx_bd_prod = (uctrl->hw_rx_bd_prod + 1) % QEDI_NUM_RX_BD;
740         rx_bd_prod = uctrl->hw_rx_bd_prod;
741         p_rxbd = (struct qedi_rx_bd *)udev->ll2_ring;
742         p_rxbd += rx_bd_prod;
743
744         memcpy(p_rxbd, &rxbd, sizeof(rxbd));
745
746         /* notify the iscsiuio about new packet */
747         uio_event_notify(&udev->qedi_uinfo);
748
749         return 0;
750 }
751
752 static void qedi_ll2_free_skbs(struct qedi_ctx *qedi)
753 {
754         struct skb_work_list *work, *work_tmp;
755
756         spin_lock_bh(&qedi->ll2_lock);
757         list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list, list) {
758                 list_del(&work->list);
759                 if (work->skb)
760                         kfree_skb(work->skb);
761                 kfree(work);
762         }
763         spin_unlock_bh(&qedi->ll2_lock);
764 }
765
766 static int qedi_ll2_recv_thread(void *arg)
767 {
768         struct qedi_ctx *qedi = (struct qedi_ctx *)arg;
769         struct skb_work_list *work, *work_tmp;
770
771         set_user_nice(current, -20);
772
773         while (!kthread_should_stop()) {
774                 spin_lock_bh(&qedi->ll2_lock);
775                 list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list,
776                                          list) {
777                         list_del(&work->list);
778                         qedi_ll2_process_skb(qedi, work->skb, work->vlan_id);
779                         kfree_skb(work->skb);
780                         kfree(work);
781                 }
782                 set_current_state(TASK_INTERRUPTIBLE);
783                 spin_unlock_bh(&qedi->ll2_lock);
784                 schedule();
785         }
786
787         __set_current_state(TASK_RUNNING);
788         return 0;
789 }
790
791 static int qedi_set_iscsi_pf_param(struct qedi_ctx *qedi)
792 {
793         u8 num_sq_pages;
794         u32 log_page_size;
795         int rval = 0;
796
797         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "Min number of MSIX %d\n",
798                   MIN_NUM_CPUS_MSIX(qedi));
799
800         num_sq_pages = (MAX_OUSTANDING_TASKS_PER_CON * 8) / PAGE_SIZE;
801
802         qedi->num_queues = MIN_NUM_CPUS_MSIX(qedi);
803
804         memset(&qedi->pf_params.iscsi_pf_params, 0,
805                sizeof(qedi->pf_params.iscsi_pf_params));
806
807         qedi->p_cpuq = pci_alloc_consistent(qedi->pdev,
808                         qedi->num_queues * sizeof(struct qedi_glbl_q_params),
809                         &qedi->hw_p_cpuq);
810         if (!qedi->p_cpuq) {
811                 QEDI_ERR(&qedi->dbg_ctx, "pci_alloc_consistent fail\n");
812                 rval = -1;
813                 goto err_alloc_mem;
814         }
815
816         rval = qedi_alloc_global_queues(qedi);
817         if (rval) {
818                 QEDI_ERR(&qedi->dbg_ctx, "Global queue allocation failed.\n");
819                 rval = -1;
820                 goto err_alloc_mem;
821         }
822
823         qedi->pf_params.iscsi_pf_params.num_cons = QEDI_MAX_ISCSI_CONNS_PER_HBA;
824         qedi->pf_params.iscsi_pf_params.num_tasks = QEDI_MAX_ISCSI_TASK;
825         qedi->pf_params.iscsi_pf_params.half_way_close_timeout = 10;
826         qedi->pf_params.iscsi_pf_params.num_sq_pages_in_ring = num_sq_pages;
827         qedi->pf_params.iscsi_pf_params.num_r2tq_pages_in_ring = num_sq_pages;
828         qedi->pf_params.iscsi_pf_params.num_uhq_pages_in_ring = num_sq_pages;
829         qedi->pf_params.iscsi_pf_params.num_queues = qedi->num_queues;
830         qedi->pf_params.iscsi_pf_params.debug_mode = qedi_fw_debug;
831
832         for (log_page_size = 0 ; log_page_size < 32 ; log_page_size++) {
833                 if ((1 << log_page_size) == PAGE_SIZE)
834                         break;
835         }
836         qedi->pf_params.iscsi_pf_params.log_page_size = log_page_size;
837
838         qedi->pf_params.iscsi_pf_params.glbl_q_params_addr =
839                                                            (u64)qedi->hw_p_cpuq;
840
841         /* RQ BDQ initializations.
842          * rq_num_entries: suggested value for Initiator is 16 (4KB RQ)
843          * rqe_log_size: 8 for 256B RQE
844          */
845         qedi->pf_params.iscsi_pf_params.rqe_log_size = 8;
846         /* BDQ address and size */
847         qedi->pf_params.iscsi_pf_params.bdq_pbl_base_addr[BDQ_ID_RQ] =
848                                                         qedi->bdq_pbl_list_dma;
849         qedi->pf_params.iscsi_pf_params.bdq_pbl_num_entries[BDQ_ID_RQ] =
850                                                 qedi->bdq_pbl_list_num_entries;
851         qedi->pf_params.iscsi_pf_params.rq_buffer_size = QEDI_BDQ_BUF_SIZE;
852
853         /* cq_num_entries: num_tasks + rq_num_entries */
854         qedi->pf_params.iscsi_pf_params.cq_num_entries = 2048;
855
856         qedi->pf_params.iscsi_pf_params.gl_rq_pi = QEDI_PROTO_CQ_PROD_IDX;
857         qedi->pf_params.iscsi_pf_params.gl_cmd_pi = 1;
858         qedi->pf_params.iscsi_pf_params.ooo_enable = 1;
859
860 err_alloc_mem:
861         return rval;
862 }
863
864 /* Free DMA coherent memory for array of queue pointers we pass to qed */
865 static void qedi_free_iscsi_pf_param(struct qedi_ctx *qedi)
866 {
867         size_t size = 0;
868
869         if (qedi->p_cpuq) {
870                 size = qedi->num_queues * sizeof(struct qedi_glbl_q_params);
871                 pci_free_consistent(qedi->pdev, size, qedi->p_cpuq,
872                                     qedi->hw_p_cpuq);
873         }
874
875         qedi_free_global_queues(qedi);
876
877         kfree(qedi->global_queues);
878 }
879
880 static void qedi_link_update(void *dev, struct qed_link_output *link)
881 {
882         struct qedi_ctx *qedi = (struct qedi_ctx *)dev;
883
884         if (link->link_up) {
885                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "Link Up event.\n");
886                 atomic_set(&qedi->link_state, QEDI_LINK_UP);
887         } else {
888                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
889                           "Link Down event.\n");
890                 atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
891         }
892 }
893
894 static struct qed_iscsi_cb_ops qedi_cb_ops = {
895         {
896                 .link_update =          qedi_link_update,
897         }
898 };
899
900 static int qedi_queue_cqe(struct qedi_ctx *qedi, union iscsi_cqe *cqe,
901                           u16 que_idx, struct qedi_percpu_s *p)
902 {
903         struct qedi_work *qedi_work;
904         struct qedi_conn *q_conn;
905         struct iscsi_conn *conn;
906         struct qedi_cmd *qedi_cmd;
907         u32 iscsi_cid;
908         int rc = 0;
909
910         iscsi_cid  = cqe->cqe_common.conn_id;
911         q_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
912         if (!q_conn) {
913                 QEDI_WARN(&qedi->dbg_ctx,
914                           "Session no longer exists for cid=0x%x!!\n",
915                           iscsi_cid);
916                 return -1;
917         }
918         conn = q_conn->cls_conn->dd_data;
919
920         switch (cqe->cqe_common.cqe_type) {
921         case ISCSI_CQE_TYPE_SOLICITED:
922         case ISCSI_CQE_TYPE_SOLICITED_WITH_SENSE:
923                 qedi_cmd = qedi_get_cmd_from_tid(qedi, cqe->cqe_solicited.itid);
924                 if (!qedi_cmd) {
925                         rc = -1;
926                         break;
927                 }
928                 INIT_LIST_HEAD(&qedi_cmd->cqe_work.list);
929                 qedi_cmd->cqe_work.qedi = qedi;
930                 memcpy(&qedi_cmd->cqe_work.cqe, cqe, sizeof(union iscsi_cqe));
931                 qedi_cmd->cqe_work.que_idx = que_idx;
932                 qedi_cmd->cqe_work.is_solicited = true;
933                 list_add_tail(&qedi_cmd->cqe_work.list, &p->work_list);
934                 break;
935         case ISCSI_CQE_TYPE_UNSOLICITED:
936         case ISCSI_CQE_TYPE_DUMMY:
937         case ISCSI_CQE_TYPE_TASK_CLEANUP:
938                 qedi_work = kzalloc(sizeof(*qedi_work), GFP_ATOMIC);
939                 if (!qedi_work) {
940                         rc = -1;
941                         break;
942                 }
943                 INIT_LIST_HEAD(&qedi_work->list);
944                 qedi_work->qedi = qedi;
945                 memcpy(&qedi_work->cqe, cqe, sizeof(union iscsi_cqe));
946                 qedi_work->que_idx = que_idx;
947                 qedi_work->is_solicited = false;
948                 list_add_tail(&qedi_work->list, &p->work_list);
949                 break;
950         default:
951                 rc = -1;
952                 QEDI_ERR(&qedi->dbg_ctx, "FW Error cqe.\n");
953         }
954         return rc;
955 }
956
957 static bool qedi_process_completions(struct qedi_fastpath *fp)
958 {
959         struct qedi_ctx *qedi = fp->qedi;
960         struct qed_sb_info *sb_info = fp->sb_info;
961         struct status_block *sb = sb_info->sb_virt;
962         struct qedi_percpu_s *p = NULL;
963         struct global_queue *que;
964         u16 prod_idx;
965         unsigned long flags;
966         union iscsi_cqe *cqe;
967         int cpu;
968         int ret;
969
970         /* Get the current firmware producer index */
971         prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
972
973         if (prod_idx >= QEDI_CQ_SIZE)
974                 prod_idx = prod_idx % QEDI_CQ_SIZE;
975
976         que = qedi->global_queues[fp->sb_id];
977         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
978                   "Before: global queue=%p prod_idx=%d cons_idx=%d, sb_id=%d\n",
979                   que, prod_idx, que->cq_cons_idx, fp->sb_id);
980
981         qedi->intr_cpu = fp->sb_id;
982         cpu = smp_processor_id();
983         p = &per_cpu(qedi_percpu, cpu);
984
985         if (unlikely(!p->iothread))
986                 WARN_ON(1);
987
988         spin_lock_irqsave(&p->p_work_lock, flags);
989         while (que->cq_cons_idx != prod_idx) {
990                 cqe = &que->cq[que->cq_cons_idx];
991
992                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
993                           "cqe=%p prod_idx=%d cons_idx=%d.\n",
994                           cqe, prod_idx, que->cq_cons_idx);
995
996                 ret = qedi_queue_cqe(qedi, cqe, fp->sb_id, p);
997                 if (ret)
998                         continue;
999
1000                 que->cq_cons_idx++;
1001                 if (que->cq_cons_idx == QEDI_CQ_SIZE)
1002                         que->cq_cons_idx = 0;
1003         }
1004         wake_up_process(p->iothread);
1005         spin_unlock_irqrestore(&p->p_work_lock, flags);
1006
1007         return true;
1008 }
1009
1010 static bool qedi_fp_has_work(struct qedi_fastpath *fp)
1011 {
1012         struct qedi_ctx *qedi = fp->qedi;
1013         struct global_queue *que;
1014         struct qed_sb_info *sb_info = fp->sb_info;
1015         struct status_block *sb = sb_info->sb_virt;
1016         u16 prod_idx;
1017
1018         barrier();
1019
1020         /* Get the current firmware producer index */
1021         prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
1022
1023         /* Get the pointer to the global CQ this completion is on */
1024         que = qedi->global_queues[fp->sb_id];
1025
1026         /* prod idx wrap around uint16 */
1027         if (prod_idx >= QEDI_CQ_SIZE)
1028                 prod_idx = prod_idx % QEDI_CQ_SIZE;
1029
1030         return (que->cq_cons_idx != prod_idx);
1031 }
1032
1033 /* MSI-X fastpath handler code */
1034 static irqreturn_t qedi_msix_handler(int irq, void *dev_id)
1035 {
1036         struct qedi_fastpath *fp = dev_id;
1037         struct qedi_ctx *qedi = fp->qedi;
1038         bool wake_io_thread = true;
1039
1040         qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0);
1041
1042 process_again:
1043         wake_io_thread = qedi_process_completions(fp);
1044         if (wake_io_thread) {
1045                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1046                           "process already running\n");
1047         }
1048
1049         if (qedi_fp_has_work(fp) == 0)
1050                 qed_sb_update_sb_idx(fp->sb_info);
1051
1052         /* Check for more work */
1053         rmb();
1054
1055         if (qedi_fp_has_work(fp) == 0)
1056                 qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
1057         else
1058                 goto process_again;
1059
1060         return IRQ_HANDLED;
1061 }
1062
1063 /* simd handler for MSI/INTa */
1064 static void qedi_simd_int_handler(void *cookie)
1065 {
1066         /* Cookie is qedi_ctx struct */
1067         struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
1068
1069         QEDI_WARN(&qedi->dbg_ctx, "qedi=%p.\n", qedi);
1070 }
1071
1072 #define QEDI_SIMD_HANDLER_NUM           0
1073 static void qedi_sync_free_irqs(struct qedi_ctx *qedi)
1074 {
1075         int i;
1076
1077         if (qedi->int_info.msix_cnt) {
1078                 for (i = 0; i < qedi->int_info.used_cnt; i++) {
1079                         synchronize_irq(qedi->int_info.msix[i].vector);
1080                         irq_set_affinity_hint(qedi->int_info.msix[i].vector,
1081                                               NULL);
1082                         free_irq(qedi->int_info.msix[i].vector,
1083                                  &qedi->fp_array[i]);
1084                 }
1085         } else {
1086                 qedi_ops->common->simd_handler_clean(qedi->cdev,
1087                                                      QEDI_SIMD_HANDLER_NUM);
1088         }
1089
1090         qedi->int_info.used_cnt = 0;
1091         qedi_ops->common->set_fp_int(qedi->cdev, 0);
1092 }
1093
1094 static int qedi_request_msix_irq(struct qedi_ctx *qedi)
1095 {
1096         int i, rc, cpu;
1097
1098         cpu = cpumask_first(cpu_online_mask);
1099         for (i = 0; i < MIN_NUM_CPUS_MSIX(qedi); i++) {
1100                 rc = request_irq(qedi->int_info.msix[i].vector,
1101                                  qedi_msix_handler, 0, "qedi",
1102                                  &qedi->fp_array[i]);
1103
1104                 if (rc) {
1105                         QEDI_WARN(&qedi->dbg_ctx, "request_irq failed.\n");
1106                         qedi_sync_free_irqs(qedi);
1107                         return rc;
1108                 }
1109                 qedi->int_info.used_cnt++;
1110                 rc = irq_set_affinity_hint(qedi->int_info.msix[i].vector,
1111                                            get_cpu_mask(cpu));
1112                 cpu = cpumask_next(cpu, cpu_online_mask);
1113         }
1114
1115         return 0;
1116 }
1117
1118 static int qedi_setup_int(struct qedi_ctx *qedi)
1119 {
1120         int rc = 0;
1121
1122         rc = qedi_ops->common->set_fp_int(qedi->cdev, num_online_cpus());
1123         rc = qedi_ops->common->get_fp_int(qedi->cdev, &qedi->int_info);
1124         if (rc)
1125                 goto exit_setup_int;
1126
1127         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1128                   "Number of msix_cnt = 0x%x num of cpus = 0x%x\n",
1129                    qedi->int_info.msix_cnt, num_online_cpus());
1130
1131         if (qedi->int_info.msix_cnt) {
1132                 rc = qedi_request_msix_irq(qedi);
1133                 goto exit_setup_int;
1134         } else {
1135                 qedi_ops->common->simd_handler_config(qedi->cdev, &qedi,
1136                                                       QEDI_SIMD_HANDLER_NUM,
1137                                                       qedi_simd_int_handler);
1138                 qedi->int_info.used_cnt = 1;
1139         }
1140
1141 exit_setup_int:
1142         return rc;
1143 }
1144
1145 static void qedi_free_bdq(struct qedi_ctx *qedi)
1146 {
1147         int i;
1148
1149         if (qedi->bdq_pbl_list)
1150                 dma_free_coherent(&qedi->pdev->dev, PAGE_SIZE,
1151                                   qedi->bdq_pbl_list, qedi->bdq_pbl_list_dma);
1152
1153         if (qedi->bdq_pbl)
1154                 dma_free_coherent(&qedi->pdev->dev, qedi->bdq_pbl_mem_size,
1155                                   qedi->bdq_pbl, qedi->bdq_pbl_dma);
1156
1157         for (i = 0; i < QEDI_BDQ_NUM; i++) {
1158                 if (qedi->bdq[i].buf_addr) {
1159                         dma_free_coherent(&qedi->pdev->dev, QEDI_BDQ_BUF_SIZE,
1160                                           qedi->bdq[i].buf_addr,
1161                                           qedi->bdq[i].buf_dma);
1162                 }
1163         }
1164 }
1165
1166 static void qedi_free_global_queues(struct qedi_ctx *qedi)
1167 {
1168         int i;
1169         struct global_queue **gl = qedi->global_queues;
1170
1171         for (i = 0; i < qedi->num_queues; i++) {
1172                 if (!gl[i])
1173                         continue;
1174
1175                 if (gl[i]->cq)
1176                         dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_mem_size,
1177                                           gl[i]->cq, gl[i]->cq_dma);
1178                 if (gl[i]->cq_pbl)
1179                         dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_pbl_size,
1180                                           gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
1181
1182                 kfree(gl[i]);
1183         }
1184         qedi_free_bdq(qedi);
1185 }
1186
1187 static int qedi_alloc_bdq(struct qedi_ctx *qedi)
1188 {
1189         int i;
1190         struct scsi_bd *pbl;
1191         u64 *list;
1192         dma_addr_t page;
1193
1194         /* Alloc dma memory for BDQ buffers */
1195         for (i = 0; i < QEDI_BDQ_NUM; i++) {
1196                 qedi->bdq[i].buf_addr =
1197                                 dma_alloc_coherent(&qedi->pdev->dev,
1198                                                    QEDI_BDQ_BUF_SIZE,
1199                                                    &qedi->bdq[i].buf_dma,
1200                                                    GFP_KERNEL);
1201                 if (!qedi->bdq[i].buf_addr) {
1202                         QEDI_ERR(&qedi->dbg_ctx,
1203                                  "Could not allocate BDQ buffer %d.\n", i);
1204                         return -ENOMEM;
1205                 }
1206         }
1207
1208         /* Alloc dma memory for BDQ page buffer list */
1209         qedi->bdq_pbl_mem_size = QEDI_BDQ_NUM * sizeof(struct scsi_bd);
1210         qedi->bdq_pbl_mem_size = ALIGN(qedi->bdq_pbl_mem_size, PAGE_SIZE);
1211         qedi->rq_num_entries = qedi->bdq_pbl_mem_size / sizeof(struct scsi_bd);
1212
1213         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, "rq_num_entries = %d.\n",
1214                   qedi->rq_num_entries);
1215
1216         qedi->bdq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
1217                                            qedi->bdq_pbl_mem_size,
1218                                            &qedi->bdq_pbl_dma, GFP_KERNEL);
1219         if (!qedi->bdq_pbl) {
1220                 QEDI_ERR(&qedi->dbg_ctx, "Could not allocate BDQ PBL.\n");
1221                 return -ENOMEM;
1222         }
1223
1224         /*
1225          * Populate BDQ PBL with physical and virtual address of individual
1226          * BDQ buffers
1227          */
1228         pbl = (struct scsi_bd  *)qedi->bdq_pbl;
1229         for (i = 0; i < QEDI_BDQ_NUM; i++) {
1230                 pbl->address.hi =
1231                                 cpu_to_le32(QEDI_U64_HI(qedi->bdq[i].buf_dma));
1232                 pbl->address.lo =
1233                                 cpu_to_le32(QEDI_U64_LO(qedi->bdq[i].buf_dma));
1234                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1235                           "pbl [0x%p] pbl->address hi [0x%llx] lo [0x%llx], idx [%d]\n",
1236                           pbl, pbl->address.hi, pbl->address.lo, i);
1237                 pbl->opaque.hi = 0;
1238                 pbl->opaque.lo = cpu_to_le32(QEDI_U64_LO(i));
1239                 pbl++;
1240         }
1241
1242         /* Allocate list of PBL pages */
1243         qedi->bdq_pbl_list = dma_alloc_coherent(&qedi->pdev->dev,
1244                                                 PAGE_SIZE,
1245                                                 &qedi->bdq_pbl_list_dma,
1246                                                 GFP_KERNEL);
1247         if (!qedi->bdq_pbl_list) {
1248                 QEDI_ERR(&qedi->dbg_ctx,
1249                          "Could not allocate list of PBL pages.\n");
1250                 return -ENOMEM;
1251         }
1252         memset(qedi->bdq_pbl_list, 0, PAGE_SIZE);
1253
1254         /*
1255          * Now populate PBL list with pages that contain pointers to the
1256          * individual buffers.
1257          */
1258         qedi->bdq_pbl_list_num_entries = qedi->bdq_pbl_mem_size / PAGE_SIZE;
1259         list = (u64 *)qedi->bdq_pbl_list;
1260         page = qedi->bdq_pbl_list_dma;
1261         for (i = 0; i < qedi->bdq_pbl_list_num_entries; i++) {
1262                 *list = qedi->bdq_pbl_dma;
1263                 list++;
1264                 page += PAGE_SIZE;
1265         }
1266
1267         return 0;
1268 }
1269
1270 static int qedi_alloc_global_queues(struct qedi_ctx *qedi)
1271 {
1272         u32 *list;
1273         int i;
1274         int status = 0, rc;
1275         u32 *pbl;
1276         dma_addr_t page;
1277         int num_pages;
1278
1279         /*
1280          * Number of global queues (CQ / RQ). This should
1281          * be <= number of available MSIX vectors for the PF
1282          */
1283         if (!qedi->num_queues) {
1284                 QEDI_ERR(&qedi->dbg_ctx, "No MSI-X vectors available!\n");
1285                 return 1;
1286         }
1287
1288         /* Make sure we allocated the PBL that will contain the physical
1289          * addresses of our queues
1290          */
1291         if (!qedi->p_cpuq) {
1292                 status = 1;
1293                 goto mem_alloc_failure;
1294         }
1295
1296         qedi->global_queues = kzalloc((sizeof(struct global_queue *) *
1297                                        qedi->num_queues), GFP_KERNEL);
1298         if (!qedi->global_queues) {
1299                 QEDI_ERR(&qedi->dbg_ctx,
1300                          "Unable to allocate global queues array ptr memory\n");
1301                 return -ENOMEM;
1302         }
1303         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1304                   "qedi->global_queues=%p.\n", qedi->global_queues);
1305
1306         /* Allocate DMA coherent buffers for BDQ */
1307         rc = qedi_alloc_bdq(qedi);
1308         if (rc)
1309                 goto mem_alloc_failure;
1310
1311         /* Allocate a CQ and an associated PBL for each MSI-X
1312          * vector.
1313          */
1314         for (i = 0; i < qedi->num_queues; i++) {
1315                 qedi->global_queues[i] =
1316                                         kzalloc(sizeof(*qedi->global_queues[0]),
1317                                                 GFP_KERNEL);
1318                 if (!qedi->global_queues[i]) {
1319                         QEDI_ERR(&qedi->dbg_ctx,
1320                                  "Unable to allocation global queue %d.\n", i);
1321                         goto mem_alloc_failure;
1322                 }
1323
1324                 qedi->global_queues[i]->cq_mem_size =
1325                     (QEDI_CQ_SIZE + 8) * sizeof(union iscsi_cqe);
1326                 qedi->global_queues[i]->cq_mem_size =
1327                     (qedi->global_queues[i]->cq_mem_size +
1328                     (QEDI_PAGE_SIZE - 1));
1329
1330                 qedi->global_queues[i]->cq_pbl_size =
1331                     (qedi->global_queues[i]->cq_mem_size /
1332                     QEDI_PAGE_SIZE) * sizeof(void *);
1333                 qedi->global_queues[i]->cq_pbl_size =
1334                     (qedi->global_queues[i]->cq_pbl_size +
1335                     (QEDI_PAGE_SIZE - 1));
1336
1337                 qedi->global_queues[i]->cq =
1338                     dma_alloc_coherent(&qedi->pdev->dev,
1339                                        qedi->global_queues[i]->cq_mem_size,
1340                                        &qedi->global_queues[i]->cq_dma,
1341                                        GFP_KERNEL);
1342
1343                 if (!qedi->global_queues[i]->cq) {
1344                         QEDI_WARN(&qedi->dbg_ctx,
1345                                   "Could not allocate cq.\n");
1346                         status = -ENOMEM;
1347                         goto mem_alloc_failure;
1348                 }
1349                 memset(qedi->global_queues[i]->cq, 0,
1350                        qedi->global_queues[i]->cq_mem_size);
1351
1352                 qedi->global_queues[i]->cq_pbl =
1353                     dma_alloc_coherent(&qedi->pdev->dev,
1354                                        qedi->global_queues[i]->cq_pbl_size,
1355                                        &qedi->global_queues[i]->cq_pbl_dma,
1356                                        GFP_KERNEL);
1357
1358                 if (!qedi->global_queues[i]->cq_pbl) {
1359                         QEDI_WARN(&qedi->dbg_ctx,
1360                                   "Could not allocate cq PBL.\n");
1361                         status = -ENOMEM;
1362                         goto mem_alloc_failure;
1363                 }
1364                 memset(qedi->global_queues[i]->cq_pbl, 0,
1365                        qedi->global_queues[i]->cq_pbl_size);
1366
1367                 /* Create PBL */
1368                 num_pages = qedi->global_queues[i]->cq_mem_size /
1369                     QEDI_PAGE_SIZE;
1370                 page = qedi->global_queues[i]->cq_dma;
1371                 pbl = (u32 *)qedi->global_queues[i]->cq_pbl;
1372
1373                 while (num_pages--) {
1374                         *pbl = (u32)page;
1375                         pbl++;
1376                         *pbl = (u32)((u64)page >> 32);
1377                         pbl++;
1378                         page += QEDI_PAGE_SIZE;
1379                 }
1380         }
1381
1382         list = (u32 *)qedi->p_cpuq;
1383
1384         /*
1385          * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
1386          * CQ#1 PBL pointer, RQ#1 PBL pointer, etc.  Each PBL pointer points
1387          * to the physical address which contains an array of pointers to the
1388          * physical addresses of the specific queue pages.
1389          */
1390         for (i = 0; i < qedi->num_queues; i++) {
1391                 *list = (u32)qedi->global_queues[i]->cq_pbl_dma;
1392                 list++;
1393                 *list = (u32)((u64)qedi->global_queues[i]->cq_pbl_dma >> 32);
1394                 list++;
1395
1396                 *list = (u32)0;
1397                 list++;
1398                 *list = (u32)((u64)0 >> 32);
1399                 list++;
1400         }
1401
1402         return 0;
1403
1404 mem_alloc_failure:
1405         qedi_free_global_queues(qedi);
1406         return status;
1407 }
1408
1409 int qedi_alloc_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
1410 {
1411         int rval = 0;
1412         u32 *pbl;
1413         dma_addr_t page;
1414         int num_pages;
1415
1416         if (!ep)
1417                 return -EIO;
1418
1419         /* Calculate appropriate queue and PBL sizes */
1420         ep->sq_mem_size = QEDI_SQ_SIZE * sizeof(struct iscsi_wqe);
1421         ep->sq_mem_size += QEDI_PAGE_SIZE - 1;
1422
1423         ep->sq_pbl_size = (ep->sq_mem_size / QEDI_PAGE_SIZE) * sizeof(void *);
1424         ep->sq_pbl_size = ep->sq_pbl_size + QEDI_PAGE_SIZE;
1425
1426         ep->sq = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_mem_size,
1427                                     &ep->sq_dma, GFP_KERNEL);
1428         if (!ep->sq) {
1429                 QEDI_WARN(&qedi->dbg_ctx,
1430                           "Could not allocate send queue.\n");
1431                 rval = -ENOMEM;
1432                 goto out;
1433         }
1434         memset(ep->sq, 0, ep->sq_mem_size);
1435
1436         ep->sq_pbl = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_pbl_size,
1437                                         &ep->sq_pbl_dma, GFP_KERNEL);
1438         if (!ep->sq_pbl) {
1439                 QEDI_WARN(&qedi->dbg_ctx,
1440                           "Could not allocate send queue PBL.\n");
1441                 rval = -ENOMEM;
1442                 goto out_free_sq;
1443         }
1444         memset(ep->sq_pbl, 0, ep->sq_pbl_size);
1445
1446         /* Create PBL */
1447         num_pages = ep->sq_mem_size / QEDI_PAGE_SIZE;
1448         page = ep->sq_dma;
1449         pbl = (u32 *)ep->sq_pbl;
1450
1451         while (num_pages--) {
1452                 *pbl = (u32)page;
1453                 pbl++;
1454                 *pbl = (u32)((u64)page >> 32);
1455                 pbl++;
1456                 page += QEDI_PAGE_SIZE;
1457         }
1458
1459         return rval;
1460
1461 out_free_sq:
1462         dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
1463                           ep->sq_dma);
1464 out:
1465         return rval;
1466 }
1467
1468 void qedi_free_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
1469 {
1470         if (ep->sq_pbl)
1471                 dma_free_coherent(&qedi->pdev->dev, ep->sq_pbl_size, ep->sq_pbl,
1472                                   ep->sq_pbl_dma);
1473         if (ep->sq)
1474                 dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
1475                                   ep->sq_dma);
1476 }
1477
1478 int qedi_get_task_idx(struct qedi_ctx *qedi)
1479 {
1480         s16 tmp_idx;
1481
1482 again:
1483         tmp_idx = find_first_zero_bit(qedi->task_idx_map,
1484                                       MAX_ISCSI_TASK_ENTRIES);
1485
1486         if (tmp_idx >= MAX_ISCSI_TASK_ENTRIES) {
1487                 QEDI_ERR(&qedi->dbg_ctx, "FW task context pool is full.\n");
1488                 tmp_idx = -1;
1489                 goto err_idx;
1490         }
1491
1492         if (test_and_set_bit(tmp_idx, qedi->task_idx_map))
1493                 goto again;
1494
1495 err_idx:
1496         return tmp_idx;
1497 }
1498
1499 void qedi_clear_task_idx(struct qedi_ctx *qedi, int idx)
1500 {
1501         if (!test_and_clear_bit(idx, qedi->task_idx_map)) {
1502                 QEDI_ERR(&qedi->dbg_ctx,
1503                          "FW task context, already cleared, tid=0x%x\n", idx);
1504                 WARN_ON(1);
1505         }
1506 }
1507
1508 void qedi_update_itt_map(struct qedi_ctx *qedi, u32 tid, u32 proto_itt,
1509                          struct qedi_cmd *cmd)
1510 {
1511         qedi->itt_map[tid].itt = proto_itt;
1512         qedi->itt_map[tid].p_cmd = cmd;
1513
1514         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1515                   "update itt map tid=0x%x, with proto itt=0x%x\n", tid,
1516                   qedi->itt_map[tid].itt);
1517 }
1518
1519 void qedi_get_task_tid(struct qedi_ctx *qedi, u32 itt, s16 *tid)
1520 {
1521         u16 i;
1522
1523         for (i = 0; i < MAX_ISCSI_TASK_ENTRIES; i++) {
1524                 if (qedi->itt_map[i].itt == itt) {
1525                         *tid = i;
1526                         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1527                                   "Ref itt=0x%x, found at tid=0x%x\n",
1528                                   itt, *tid);
1529                         return;
1530                 }
1531         }
1532
1533         WARN_ON(1);
1534 }
1535
1536 void qedi_get_proto_itt(struct qedi_ctx *qedi, u32 tid, u32 *proto_itt)
1537 {
1538         *proto_itt = qedi->itt_map[tid].itt;
1539         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1540                   "Get itt map tid [0x%x with proto itt[0x%x]",
1541                   tid, *proto_itt);
1542 }
1543
1544 struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid)
1545 {
1546         struct qedi_cmd *cmd = NULL;
1547
1548         if (tid > MAX_ISCSI_TASK_ENTRIES)
1549                 return NULL;
1550
1551         cmd = qedi->itt_map[tid].p_cmd;
1552         if (cmd->task_id != tid)
1553                 return NULL;
1554
1555         qedi->itt_map[tid].p_cmd = NULL;
1556
1557         return cmd;
1558 }
1559
1560 static int qedi_alloc_itt(struct qedi_ctx *qedi)
1561 {
1562         qedi->itt_map = kcalloc(MAX_ISCSI_TASK_ENTRIES,
1563                                 sizeof(struct qedi_itt_map), GFP_KERNEL);
1564         if (!qedi->itt_map) {
1565                 QEDI_ERR(&qedi->dbg_ctx,
1566                          "Unable to allocate itt map array memory\n");
1567                 return -ENOMEM;
1568         }
1569         return 0;
1570 }
1571
1572 static void qedi_free_itt(struct qedi_ctx *qedi)
1573 {
1574         kfree(qedi->itt_map);
1575 }
1576
1577 static struct qed_ll2_cb_ops qedi_ll2_cb_ops = {
1578         .rx_cb = qedi_ll2_rx,
1579         .tx_cb = NULL,
1580 };
1581
1582 static int qedi_percpu_io_thread(void *arg)
1583 {
1584         struct qedi_percpu_s *p = arg;
1585         struct qedi_work *work, *tmp;
1586         unsigned long flags;
1587         LIST_HEAD(work_list);
1588
1589         set_user_nice(current, -20);
1590
1591         while (!kthread_should_stop()) {
1592                 spin_lock_irqsave(&p->p_work_lock, flags);
1593                 while (!list_empty(&p->work_list)) {
1594                         list_splice_init(&p->work_list, &work_list);
1595                         spin_unlock_irqrestore(&p->p_work_lock, flags);
1596
1597                         list_for_each_entry_safe(work, tmp, &work_list, list) {
1598                                 list_del_init(&work->list);
1599                                 qedi_fp_process_cqes(work);
1600                                 if (!work->is_solicited)
1601                                         kfree(work);
1602                         }
1603                         cond_resched();
1604                         spin_lock_irqsave(&p->p_work_lock, flags);
1605                 }
1606                 set_current_state(TASK_INTERRUPTIBLE);
1607                 spin_unlock_irqrestore(&p->p_work_lock, flags);
1608                 schedule();
1609         }
1610         __set_current_state(TASK_RUNNING);
1611
1612         return 0;
1613 }
1614
1615 static int qedi_cpu_online(unsigned int cpu)
1616 {
1617         struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
1618         struct task_struct *thread;
1619
1620         thread = kthread_create_on_node(qedi_percpu_io_thread, (void *)p,
1621                                         cpu_to_node(cpu),
1622                                         "qedi_thread/%d", cpu);
1623         if (IS_ERR(thread))
1624                 return PTR_ERR(thread);
1625
1626         kthread_bind(thread, cpu);
1627         p->iothread = thread;
1628         wake_up_process(thread);
1629         return 0;
1630 }
1631
1632 static int qedi_cpu_offline(unsigned int cpu)
1633 {
1634         struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
1635         struct qedi_work *work, *tmp;
1636         struct task_struct *thread;
1637
1638         spin_lock_bh(&p->p_work_lock);
1639         thread = p->iothread;
1640         p->iothread = NULL;
1641
1642         list_for_each_entry_safe(work, tmp, &p->work_list, list) {
1643                 list_del_init(&work->list);
1644                 qedi_fp_process_cqes(work);
1645                 if (!work->is_solicited)
1646                         kfree(work);
1647         }
1648
1649         spin_unlock_bh(&p->p_work_lock);
1650         if (thread)
1651                 kthread_stop(thread);
1652         return 0;
1653 }
1654
1655 void qedi_reset_host_mtu(struct qedi_ctx *qedi, u16 mtu)
1656 {
1657         struct qed_ll2_params params;
1658
1659         qedi_recover_all_conns(qedi);
1660
1661         qedi_ops->ll2->stop(qedi->cdev);
1662         qedi_ll2_free_skbs(qedi);
1663
1664         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "old MTU %u, new MTU %u\n",
1665                   qedi->ll2_mtu, mtu);
1666         memset(&params, 0, sizeof(params));
1667         qedi->ll2_mtu = mtu;
1668         params.mtu = qedi->ll2_mtu + IPV6_HDR_LEN + TCP_HDR_LEN;
1669         params.drop_ttl0_packets = 0;
1670         params.rx_vlan_stripping = 1;
1671         ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
1672         qedi_ops->ll2->start(qedi->cdev, &params);
1673 }
1674
1675 static void __qedi_remove(struct pci_dev *pdev, int mode)
1676 {
1677         struct qedi_ctx *qedi = pci_get_drvdata(pdev);
1678
1679         if (qedi->tmf_thread) {
1680                 flush_workqueue(qedi->tmf_thread);
1681                 destroy_workqueue(qedi->tmf_thread);
1682                 qedi->tmf_thread = NULL;
1683         }
1684
1685         if (qedi->offload_thread) {
1686                 flush_workqueue(qedi->offload_thread);
1687                 destroy_workqueue(qedi->offload_thread);
1688                 qedi->offload_thread = NULL;
1689         }
1690
1691 #ifdef CONFIG_DEBUG_FS
1692         qedi_dbg_host_exit(&qedi->dbg_ctx);
1693 #endif
1694         if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags))
1695                 qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
1696
1697         qedi_sync_free_irqs(qedi);
1698
1699         if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
1700                 qedi_ops->stop(qedi->cdev);
1701                 qedi_ops->ll2->stop(qedi->cdev);
1702         }
1703
1704         if (mode == QEDI_MODE_NORMAL)
1705                 qedi_free_iscsi_pf_param(qedi);
1706
1707         if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
1708                 qedi_ops->common->slowpath_stop(qedi->cdev);
1709                 qedi_ops->common->remove(qedi->cdev);
1710         }
1711
1712         qedi_destroy_fp(qedi);
1713
1714         if (mode == QEDI_MODE_NORMAL) {
1715                 qedi_release_cid_que(qedi);
1716                 qedi_cm_free_mem(qedi);
1717                 qedi_free_uio(qedi->udev);
1718                 qedi_free_itt(qedi);
1719
1720                 iscsi_host_remove(qedi->shost);
1721                 iscsi_host_free(qedi->shost);
1722
1723                 if (qedi->ll2_recv_thread) {
1724                         kthread_stop(qedi->ll2_recv_thread);
1725                         qedi->ll2_recv_thread = NULL;
1726                 }
1727                 qedi_ll2_free_skbs(qedi);
1728         }
1729 }
1730
1731 static int __qedi_probe(struct pci_dev *pdev, int mode)
1732 {
1733         struct qedi_ctx *qedi;
1734         struct qed_ll2_params params;
1735         u32 dp_module = 0;
1736         u8 dp_level = 0;
1737         bool is_vf = false;
1738         char host_buf[16];
1739         struct qed_link_params link_params;
1740         struct qed_slowpath_params sp_params;
1741         struct qed_probe_params qed_params;
1742         void *task_start, *task_end;
1743         int rc;
1744         u16 tmp;
1745
1746         if (mode != QEDI_MODE_RECOVERY) {
1747                 qedi = qedi_host_alloc(pdev);
1748                 if (!qedi) {
1749                         rc = -ENOMEM;
1750                         goto exit_probe;
1751                 }
1752         } else {
1753                 qedi = pci_get_drvdata(pdev);
1754         }
1755
1756         memset(&qed_params, 0, sizeof(qed_params));
1757         qed_params.protocol = QED_PROTOCOL_ISCSI;
1758         qed_params.dp_module = dp_module;
1759         qed_params.dp_level = dp_level;
1760         qed_params.is_vf = is_vf;
1761         qedi->cdev = qedi_ops->common->probe(pdev, &qed_params);
1762         if (!qedi->cdev) {
1763                 rc = -ENODEV;
1764                 QEDI_ERR(&qedi->dbg_ctx, "Cannot initialize hardware\n");
1765                 goto free_host;
1766         }
1767
1768         qedi->msix_count = MAX_NUM_MSIX_PF;
1769         atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
1770
1771         if (mode != QEDI_MODE_RECOVERY) {
1772                 rc = qedi_set_iscsi_pf_param(qedi);
1773                 if (rc) {
1774                         rc = -ENOMEM;
1775                         QEDI_ERR(&qedi->dbg_ctx,
1776                                  "Set iSCSI pf param fail\n");
1777                         goto free_host;
1778                 }
1779         }
1780
1781         qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
1782
1783         rc = qedi_prepare_fp(qedi);
1784         if (rc) {
1785                 QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath.\n");
1786                 goto free_pf_params;
1787         }
1788
1789         /* Start the Slowpath-process */
1790         memset(&sp_params, 0, sizeof(struct qed_slowpath_params));
1791         sp_params.int_mode = QED_INT_MODE_MSIX;
1792         sp_params.drv_major = QEDI_DRIVER_MAJOR_VER;
1793         sp_params.drv_minor = QEDI_DRIVER_MINOR_VER;
1794         sp_params.drv_rev = QEDI_DRIVER_REV_VER;
1795         sp_params.drv_eng = QEDI_DRIVER_ENG_VER;
1796         strlcpy(sp_params.name, "qedi iSCSI", QED_DRV_VER_STR_SIZE);
1797         rc = qedi_ops->common->slowpath_start(qedi->cdev, &sp_params);
1798         if (rc) {
1799                 QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath\n");
1800                 goto stop_hw;
1801         }
1802
1803         /* update_pf_params needs to be called before and after slowpath
1804          * start
1805          */
1806         qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
1807
1808         rc = qedi_setup_int(qedi);
1809         if (rc)
1810                 goto stop_iscsi_func;
1811
1812         qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
1813
1814         /* Learn information crucial for qedi to progress */
1815         rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
1816         if (rc)
1817                 goto stop_iscsi_func;
1818
1819         /* Record BDQ producer doorbell addresses */
1820         qedi->bdq_primary_prod = qedi->dev_info.primary_dbq_rq_addr;
1821         qedi->bdq_secondary_prod = qedi->dev_info.secondary_bdq_rq_addr;
1822         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1823                   "BDQ primary_prod=%p secondary_prod=%p.\n",
1824                   qedi->bdq_primary_prod,
1825                   qedi->bdq_secondary_prod);
1826
1827         /*
1828          * We need to write the number of BDs in the BDQ we've preallocated so
1829          * the f/w will do a prefetch and we'll get an unsolicited CQE when a
1830          * packet arrives.
1831          */
1832         qedi->bdq_prod_idx = QEDI_BDQ_NUM;
1833         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1834                   "Writing %d to primary and secondary BDQ doorbell registers.\n",
1835                   qedi->bdq_prod_idx);
1836         writew(qedi->bdq_prod_idx, qedi->bdq_primary_prod);
1837         tmp = readw(qedi->bdq_primary_prod);
1838         writew(qedi->bdq_prod_idx, qedi->bdq_secondary_prod);
1839         tmp = readw(qedi->bdq_secondary_prod);
1840
1841         ether_addr_copy(qedi->mac, qedi->dev_info.common.hw_mac);
1842         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "MAC address is %pM.\n",
1843                   qedi->mac);
1844
1845         sprintf(host_buf, "host_%d", qedi->shost->host_no);
1846         qedi_ops->common->set_id(qedi->cdev, host_buf, QEDI_MODULE_VERSION);
1847
1848         qedi_ops->register_ops(qedi->cdev, &qedi_cb_ops, qedi);
1849
1850         memset(&params, 0, sizeof(params));
1851         params.mtu = DEF_PATH_MTU + IPV6_HDR_LEN + TCP_HDR_LEN;
1852         qedi->ll2_mtu = DEF_PATH_MTU;
1853         params.drop_ttl0_packets = 0;
1854         params.rx_vlan_stripping = 1;
1855         ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
1856
1857         if (mode != QEDI_MODE_RECOVERY) {
1858                 /* set up rx path */
1859                 INIT_LIST_HEAD(&qedi->ll2_skb_list);
1860                 spin_lock_init(&qedi->ll2_lock);
1861                 /* start qedi context */
1862                 spin_lock_init(&qedi->hba_lock);
1863                 spin_lock_init(&qedi->task_idx_lock);
1864         }
1865         qedi_ops->ll2->register_cb_ops(qedi->cdev, &qedi_ll2_cb_ops, qedi);
1866         qedi_ops->ll2->start(qedi->cdev, &params);
1867
1868         if (mode != QEDI_MODE_RECOVERY) {
1869                 qedi->ll2_recv_thread = kthread_run(qedi_ll2_recv_thread,
1870                                                     (void *)qedi,
1871                                                     "qedi_ll2_thread");
1872         }
1873
1874         rc = qedi_ops->start(qedi->cdev, &qedi->tasks,
1875                              qedi, qedi_iscsi_event_cb);
1876         if (rc) {
1877                 rc = -ENODEV;
1878                 QEDI_ERR(&qedi->dbg_ctx, "Cannot start iSCSI function\n");
1879                 goto stop_slowpath;
1880         }
1881
1882         task_start = qedi_get_task_mem(&qedi->tasks, 0);
1883         task_end = qedi_get_task_mem(&qedi->tasks, MAX_TID_BLOCKS_ISCSI - 1);
1884         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1885                   "Task context start=%p, end=%p block_size=%u.\n",
1886                    task_start, task_end, qedi->tasks.size);
1887
1888         memset(&link_params, 0, sizeof(link_params));
1889         link_params.link_up = true;
1890         rc = qedi_ops->common->set_link(qedi->cdev, &link_params);
1891         if (rc) {
1892                 QEDI_WARN(&qedi->dbg_ctx, "Link set up failed.\n");
1893                 atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
1894         }
1895
1896 #ifdef CONFIG_DEBUG_FS
1897         qedi_dbg_host_init(&qedi->dbg_ctx, &qedi_debugfs_ops,
1898                            &qedi_dbg_fops);
1899 #endif
1900         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1901                   "QLogic FastLinQ iSCSI Module qedi %s, FW %d.%d.%d.%d\n",
1902                   QEDI_MODULE_VERSION, FW_MAJOR_VERSION, FW_MINOR_VERSION,
1903                   FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
1904
1905         if (mode == QEDI_MODE_NORMAL) {
1906                 if (iscsi_host_add(qedi->shost, &pdev->dev)) {
1907                         QEDI_ERR(&qedi->dbg_ctx,
1908                                  "Could not add iscsi host\n");
1909                         rc = -ENOMEM;
1910                         goto remove_host;
1911                 }
1912
1913                 /* Allocate uio buffers */
1914                 rc = qedi_alloc_uio_rings(qedi);
1915                 if (rc) {
1916                         QEDI_ERR(&qedi->dbg_ctx,
1917                                  "UIO alloc ring failed err=%d\n", rc);
1918                         goto remove_host;
1919                 }
1920
1921                 rc = qedi_init_uio(qedi);
1922                 if (rc) {
1923                         QEDI_ERR(&qedi->dbg_ctx,
1924                                  "UIO init failed, err=%d\n", rc);
1925                         goto free_uio;
1926                 }
1927
1928                 /* host the array on iscsi_conn */
1929                 rc = qedi_setup_cid_que(qedi);
1930                 if (rc) {
1931                         QEDI_ERR(&qedi->dbg_ctx,
1932                                  "Could not setup cid que\n");
1933                         goto free_uio;
1934                 }
1935
1936                 rc = qedi_cm_alloc_mem(qedi);
1937                 if (rc) {
1938                         QEDI_ERR(&qedi->dbg_ctx,
1939                                  "Could not alloc cm memory\n");
1940                         goto free_cid_que;
1941                 }
1942
1943                 rc = qedi_alloc_itt(qedi);
1944                 if (rc) {
1945                         QEDI_ERR(&qedi->dbg_ctx,
1946                                  "Could not alloc itt memory\n");
1947                         goto free_cid_que;
1948                 }
1949
1950                 sprintf(host_buf, "host_%d", qedi->shost->host_no);
1951                 qedi->tmf_thread = create_singlethread_workqueue(host_buf);
1952                 if (!qedi->tmf_thread) {
1953                         QEDI_ERR(&qedi->dbg_ctx,
1954                                  "Unable to start tmf thread!\n");
1955                         rc = -ENODEV;
1956                         goto free_cid_que;
1957                 }
1958
1959                 sprintf(host_buf, "qedi_ofld%d", qedi->shost->host_no);
1960                 qedi->offload_thread = create_workqueue(host_buf);
1961                 if (!qedi->offload_thread) {
1962                         QEDI_ERR(&qedi->dbg_ctx,
1963                                  "Unable to start offload thread!\n");
1964                         rc = -ENODEV;
1965                         goto free_cid_que;
1966                 }
1967
1968                 /* F/w needs 1st task context memory entry for performance */
1969                 set_bit(QEDI_RESERVE_TASK_ID, qedi->task_idx_map);
1970                 atomic_set(&qedi->num_offloads, 0);
1971         }
1972
1973         return 0;
1974
1975 free_cid_que:
1976         qedi_release_cid_que(qedi);
1977 free_uio:
1978         qedi_free_uio(qedi->udev);
1979 remove_host:
1980 #ifdef CONFIG_DEBUG_FS
1981         qedi_dbg_host_exit(&qedi->dbg_ctx);
1982 #endif
1983         iscsi_host_remove(qedi->shost);
1984 stop_iscsi_func:
1985         qedi_ops->stop(qedi->cdev);
1986 stop_slowpath:
1987         qedi_ops->common->slowpath_stop(qedi->cdev);
1988 stop_hw:
1989         qedi_ops->common->remove(qedi->cdev);
1990 free_pf_params:
1991         qedi_free_iscsi_pf_param(qedi);
1992 free_host:
1993         iscsi_host_free(qedi->shost);
1994 exit_probe:
1995         return rc;
1996 }
1997
1998 static int qedi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1999 {
2000         return __qedi_probe(pdev, QEDI_MODE_NORMAL);
2001 }
2002
2003 static void qedi_remove(struct pci_dev *pdev)
2004 {
2005         __qedi_remove(pdev, QEDI_MODE_NORMAL);
2006 }
2007
2008 static struct pci_device_id qedi_pci_tbl[] = {
2009         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165E) },
2010         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8084) },
2011         { 0 },
2012 };
2013 MODULE_DEVICE_TABLE(pci, qedi_pci_tbl);
2014
2015 static enum cpuhp_state qedi_cpuhp_state;
2016
2017 static struct pci_driver qedi_pci_driver = {
2018         .name = QEDI_MODULE_NAME,
2019         .id_table = qedi_pci_tbl,
2020         .probe = qedi_probe,
2021         .remove = qedi_remove,
2022 };
2023
2024 static int __init qedi_init(void)
2025 {
2026         struct qedi_percpu_s *p;
2027         int cpu, rc = 0;
2028
2029         qedi_ops = qed_get_iscsi_ops();
2030         if (!qedi_ops) {
2031                 QEDI_ERR(NULL, "Failed to get qed iSCSI operations\n");
2032                 return -EINVAL;
2033         }
2034
2035 #ifdef CONFIG_DEBUG_FS
2036         qedi_dbg_init("qedi");
2037 #endif
2038
2039         qedi_scsi_transport = iscsi_register_transport(&qedi_iscsi_transport);
2040         if (!qedi_scsi_transport) {
2041                 QEDI_ERR(NULL, "Could not register qedi transport");
2042                 rc = -ENOMEM;
2043                 goto exit_qedi_init_1;
2044         }
2045
2046         for_each_possible_cpu(cpu) {
2047                 p = &per_cpu(qedi_percpu, cpu);
2048                 INIT_LIST_HEAD(&p->work_list);
2049                 spin_lock_init(&p->p_work_lock);
2050                 p->iothread = NULL;
2051         }
2052
2053         rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/qedi:online",
2054                                qedi_cpu_online, qedi_cpu_offline);
2055         if (rc < 0)
2056                 goto exit_qedi_init_2;
2057         qedi_cpuhp_state = rc;
2058
2059         rc = pci_register_driver(&qedi_pci_driver);
2060         if (rc) {
2061                 QEDI_ERR(NULL, "Failed to register driver\n");
2062                 goto exit_qedi_hp;
2063         }
2064
2065         return 0;
2066
2067 exit_qedi_hp:
2068         cpuhp_remove_state(qedi_cpuhp_state);
2069 exit_qedi_init_2:
2070         iscsi_unregister_transport(&qedi_iscsi_transport);
2071 exit_qedi_init_1:
2072 #ifdef CONFIG_DEBUG_FS
2073         qedi_dbg_exit();
2074 #endif
2075         qed_put_iscsi_ops();
2076         return rc;
2077 }
2078
2079 static void __exit qedi_cleanup(void)
2080 {
2081         pci_unregister_driver(&qedi_pci_driver);
2082         cpuhp_remove_state(qedi_cpuhp_state);
2083         iscsi_unregister_transport(&qedi_iscsi_transport);
2084
2085 #ifdef CONFIG_DEBUG_FS
2086         qedi_dbg_exit();
2087 #endif
2088         qed_put_iscsi_ops();
2089 }
2090
2091 MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx iSCSI Module");
2092 MODULE_LICENSE("GPL");
2093 MODULE_AUTHOR("QLogic Corporation");
2094 MODULE_VERSION(QEDI_MODULE_VERSION);
2095 module_init(qedi_init);
2096 module_exit(qedi_cleanup);