Linux-libre 4.19.20-gnu
[librecmc/linux-libre.git] / drivers / scsi / qedi / qedi_iscsi.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/blkdev.h>
11 #include <linux/etherdevice.h>
12 #include <linux/if_ether.h>
13 #include <linux/if_vlan.h>
14 #include <scsi/scsi_tcq.h>
15
16 #include "qedi.h"
17 #include "qedi_iscsi.h"
18 #include "qedi_gbl.h"
19
20 int qedi_recover_all_conns(struct qedi_ctx *qedi)
21 {
22         struct qedi_conn *qedi_conn;
23         int i;
24
25         for (i = 0; i < qedi->max_active_conns; i++) {
26                 qedi_conn = qedi_get_conn_from_id(qedi, i);
27                 if (!qedi_conn)
28                         continue;
29
30                 qedi_start_conn_recovery(qedi, qedi_conn);
31         }
32
33         return SUCCESS;
34 }
35
36 static int qedi_eh_host_reset(struct scsi_cmnd *cmd)
37 {
38         struct Scsi_Host *shost = cmd->device->host;
39         struct qedi_ctx *qedi;
40
41         qedi = iscsi_host_priv(shost);
42
43         return qedi_recover_all_conns(qedi);
44 }
45
46 struct scsi_host_template qedi_host_template = {
47         .module = THIS_MODULE,
48         .name = "QLogic QEDI 25/40/100Gb iSCSI Initiator Driver",
49         .proc_name = QEDI_MODULE_NAME,
50         .queuecommand = iscsi_queuecommand,
51         .eh_timed_out = iscsi_eh_cmd_timed_out,
52         .eh_abort_handler = iscsi_eh_abort,
53         .eh_device_reset_handler = iscsi_eh_device_reset,
54         .eh_target_reset_handler = iscsi_eh_recover_target,
55         .eh_host_reset_handler = qedi_eh_host_reset,
56         .target_alloc = iscsi_target_alloc,
57         .change_queue_depth = scsi_change_queue_depth,
58         .can_queue = QEDI_MAX_ISCSI_TASK,
59         .this_id = -1,
60         .sg_tablesize = QEDI_ISCSI_MAX_BDS_PER_CMD,
61         .max_sectors = 0xffff,
62         .dma_boundary = QEDI_HW_DMA_BOUNDARY,
63         .cmd_per_lun = 128,
64         .use_clustering = ENABLE_CLUSTERING,
65         .shost_attrs = qedi_shost_attrs,
66 };
67
68 static void qedi_conn_free_login_resources(struct qedi_ctx *qedi,
69                                            struct qedi_conn *qedi_conn)
70 {
71         if (qedi_conn->gen_pdu.resp_bd_tbl) {
72                 dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
73                                   qedi_conn->gen_pdu.resp_bd_tbl,
74                                   qedi_conn->gen_pdu.resp_bd_dma);
75                 qedi_conn->gen_pdu.resp_bd_tbl = NULL;
76         }
77
78         if (qedi_conn->gen_pdu.req_bd_tbl) {
79                 dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
80                                   qedi_conn->gen_pdu.req_bd_tbl,
81                                   qedi_conn->gen_pdu.req_bd_dma);
82                 qedi_conn->gen_pdu.req_bd_tbl = NULL;
83         }
84
85         if (qedi_conn->gen_pdu.resp_buf) {
86                 dma_free_coherent(&qedi->pdev->dev,
87                                   ISCSI_DEF_MAX_RECV_SEG_LEN,
88                                   qedi_conn->gen_pdu.resp_buf,
89                                   qedi_conn->gen_pdu.resp_dma_addr);
90                 qedi_conn->gen_pdu.resp_buf = NULL;
91         }
92
93         if (qedi_conn->gen_pdu.req_buf) {
94                 dma_free_coherent(&qedi->pdev->dev,
95                                   ISCSI_DEF_MAX_RECV_SEG_LEN,
96                                   qedi_conn->gen_pdu.req_buf,
97                                   qedi_conn->gen_pdu.req_dma_addr);
98                 qedi_conn->gen_pdu.req_buf = NULL;
99         }
100 }
101
102 static int qedi_conn_alloc_login_resources(struct qedi_ctx *qedi,
103                                            struct qedi_conn *qedi_conn)
104 {
105         qedi_conn->gen_pdu.req_buf =
106                 dma_alloc_coherent(&qedi->pdev->dev,
107                                    ISCSI_DEF_MAX_RECV_SEG_LEN,
108                                    &qedi_conn->gen_pdu.req_dma_addr,
109                                    GFP_KERNEL);
110         if (!qedi_conn->gen_pdu.req_buf)
111                 goto login_req_buf_failure;
112
113         qedi_conn->gen_pdu.req_buf_size = 0;
114         qedi_conn->gen_pdu.req_wr_ptr = qedi_conn->gen_pdu.req_buf;
115
116         qedi_conn->gen_pdu.resp_buf =
117                 dma_alloc_coherent(&qedi->pdev->dev,
118                                    ISCSI_DEF_MAX_RECV_SEG_LEN,
119                                    &qedi_conn->gen_pdu.resp_dma_addr,
120                                    GFP_KERNEL);
121         if (!qedi_conn->gen_pdu.resp_buf)
122                 goto login_resp_buf_failure;
123
124         qedi_conn->gen_pdu.resp_buf_size = ISCSI_DEF_MAX_RECV_SEG_LEN;
125         qedi_conn->gen_pdu.resp_wr_ptr = qedi_conn->gen_pdu.resp_buf;
126
127         qedi_conn->gen_pdu.req_bd_tbl =
128                 dma_alloc_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
129                                    &qedi_conn->gen_pdu.req_bd_dma, GFP_KERNEL);
130         if (!qedi_conn->gen_pdu.req_bd_tbl)
131                 goto login_req_bd_tbl_failure;
132
133         qedi_conn->gen_pdu.resp_bd_tbl =
134                 dma_alloc_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
135                                    &qedi_conn->gen_pdu.resp_bd_dma,
136                                    GFP_KERNEL);
137         if (!qedi_conn->gen_pdu.resp_bd_tbl)
138                 goto login_resp_bd_tbl_failure;
139
140         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SESS,
141                   "Allocation successful, cid=0x%x\n",
142                   qedi_conn->iscsi_conn_id);
143         return 0;
144
145 login_resp_bd_tbl_failure:
146         dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
147                           qedi_conn->gen_pdu.req_bd_tbl,
148                           qedi_conn->gen_pdu.req_bd_dma);
149         qedi_conn->gen_pdu.req_bd_tbl = NULL;
150
151 login_req_bd_tbl_failure:
152         dma_free_coherent(&qedi->pdev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
153                           qedi_conn->gen_pdu.resp_buf,
154                           qedi_conn->gen_pdu.resp_dma_addr);
155         qedi_conn->gen_pdu.resp_buf = NULL;
156 login_resp_buf_failure:
157         dma_free_coherent(&qedi->pdev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
158                           qedi_conn->gen_pdu.req_buf,
159                           qedi_conn->gen_pdu.req_dma_addr);
160         qedi_conn->gen_pdu.req_buf = NULL;
161 login_req_buf_failure:
162         iscsi_conn_printk(KERN_ERR, qedi_conn->cls_conn->dd_data,
163                           "login resource alloc failed!!\n");
164         return -ENOMEM;
165 }
166
167 static void qedi_destroy_cmd_pool(struct qedi_ctx *qedi,
168                                   struct iscsi_session *session)
169 {
170         int i;
171
172         for (i = 0; i < session->cmds_max; i++) {
173                 struct iscsi_task *task = session->cmds[i];
174                 struct qedi_cmd *cmd = task->dd_data;
175
176                 if (cmd->io_tbl.sge_tbl)
177                         dma_free_coherent(&qedi->pdev->dev,
178                                           QEDI_ISCSI_MAX_BDS_PER_CMD *
179                                           sizeof(struct scsi_sge),
180                                           cmd->io_tbl.sge_tbl,
181                                           cmd->io_tbl.sge_tbl_dma);
182
183                 if (cmd->sense_buffer)
184                         dma_free_coherent(&qedi->pdev->dev,
185                                           SCSI_SENSE_BUFFERSIZE,
186                                           cmd->sense_buffer,
187                                           cmd->sense_buffer_dma);
188         }
189 }
190
191 static int qedi_alloc_sget(struct qedi_ctx *qedi, struct iscsi_session *session,
192                            struct qedi_cmd *cmd)
193 {
194         struct qedi_io_bdt *io = &cmd->io_tbl;
195         struct scsi_sge *sge;
196
197         io->sge_tbl = dma_alloc_coherent(&qedi->pdev->dev,
198                                          QEDI_ISCSI_MAX_BDS_PER_CMD *
199                                          sizeof(*sge),
200                                          &io->sge_tbl_dma, GFP_KERNEL);
201         if (!io->sge_tbl) {
202                 iscsi_session_printk(KERN_ERR, session,
203                                      "Could not allocate BD table.\n");
204                 return -ENOMEM;
205         }
206
207         io->sge_valid = 0;
208         return 0;
209 }
210
211 static int qedi_setup_cmd_pool(struct qedi_ctx *qedi,
212                                struct iscsi_session *session)
213 {
214         int i;
215
216         for (i = 0; i < session->cmds_max; i++) {
217                 struct iscsi_task *task = session->cmds[i];
218                 struct qedi_cmd *cmd = task->dd_data;
219
220                 task->hdr = &cmd->hdr;
221                 task->hdr_max = sizeof(struct iscsi_hdr);
222
223                 if (qedi_alloc_sget(qedi, session, cmd))
224                         goto free_sgets;
225
226                 cmd->sense_buffer = dma_alloc_coherent(&qedi->pdev->dev,
227                                                        SCSI_SENSE_BUFFERSIZE,
228                                                        &cmd->sense_buffer_dma,
229                                                        GFP_KERNEL);
230                 if (!cmd->sense_buffer)
231                         goto free_sgets;
232         }
233
234         return 0;
235
236 free_sgets:
237         qedi_destroy_cmd_pool(qedi, session);
238         return -ENOMEM;
239 }
240
241 static struct iscsi_cls_session *
242 qedi_session_create(struct iscsi_endpoint *ep, u16 cmds_max,
243                     u16 qdepth, uint32_t initial_cmdsn)
244 {
245         struct Scsi_Host *shost;
246         struct iscsi_cls_session *cls_session;
247         struct qedi_ctx *qedi;
248         struct qedi_endpoint *qedi_ep;
249
250         if (!ep)
251                 return NULL;
252
253         qedi_ep = ep->dd_data;
254         shost = qedi_ep->qedi->shost;
255         qedi = iscsi_host_priv(shost);
256
257         if (cmds_max > qedi->max_sqes)
258                 cmds_max = qedi->max_sqes;
259         else if (cmds_max < QEDI_SQ_WQES_MIN)
260                 cmds_max = QEDI_SQ_WQES_MIN;
261
262         cls_session = iscsi_session_setup(&qedi_iscsi_transport, shost,
263                                           cmds_max, 0, sizeof(struct qedi_cmd),
264                                           initial_cmdsn, ISCSI_MAX_TARGET);
265         if (!cls_session) {
266                 QEDI_ERR(&qedi->dbg_ctx,
267                          "Failed to setup session for ep=%p\n", qedi_ep);
268                 return NULL;
269         }
270
271         if (qedi_setup_cmd_pool(qedi, cls_session->dd_data)) {
272                 QEDI_ERR(&qedi->dbg_ctx,
273                          "Failed to setup cmd pool for ep=%p\n", qedi_ep);
274                 goto session_teardown;
275         }
276
277         return cls_session;
278
279 session_teardown:
280         iscsi_session_teardown(cls_session);
281         return NULL;
282 }
283
284 static void qedi_session_destroy(struct iscsi_cls_session *cls_session)
285 {
286         struct iscsi_session *session = cls_session->dd_data;
287         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
288         struct qedi_ctx *qedi = iscsi_host_priv(shost);
289
290         qedi_destroy_cmd_pool(qedi, session);
291         iscsi_session_teardown(cls_session);
292 }
293
294 static struct iscsi_cls_conn *
295 qedi_conn_create(struct iscsi_cls_session *cls_session, uint32_t cid)
296 {
297         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
298         struct qedi_ctx *qedi = iscsi_host_priv(shost);
299         struct iscsi_cls_conn *cls_conn;
300         struct qedi_conn *qedi_conn;
301         struct iscsi_conn *conn;
302
303         cls_conn = iscsi_conn_setup(cls_session, sizeof(*qedi_conn),
304                                     cid);
305         if (!cls_conn) {
306                 QEDI_ERR(&qedi->dbg_ctx,
307                          "conn_new: iscsi conn setup failed, cid=0x%x, cls_sess=%p!\n",
308                          cid, cls_session);
309                 return NULL;
310         }
311
312         conn = cls_conn->dd_data;
313         qedi_conn = conn->dd_data;
314         qedi_conn->cls_conn = cls_conn;
315         qedi_conn->qedi = qedi;
316         qedi_conn->ep = NULL;
317         qedi_conn->active_cmd_count = 0;
318         INIT_LIST_HEAD(&qedi_conn->active_cmd_list);
319         spin_lock_init(&qedi_conn->list_lock);
320
321         if (qedi_conn_alloc_login_resources(qedi, qedi_conn)) {
322                 iscsi_conn_printk(KERN_ALERT, conn,
323                                   "conn_new: login resc alloc failed, cid=0x%x, cls_sess=%p!!\n",
324                                    cid, cls_session);
325                 goto free_conn;
326         }
327
328         return cls_conn;
329
330 free_conn:
331         iscsi_conn_teardown(cls_conn);
332         return NULL;
333 }
334
335 void qedi_mark_device_missing(struct iscsi_cls_session *cls_session)
336 {
337         iscsi_block_session(cls_session);
338 }
339
340 void qedi_mark_device_available(struct iscsi_cls_session *cls_session)
341 {
342         iscsi_unblock_session(cls_session);
343 }
344
345 static int qedi_bind_conn_to_iscsi_cid(struct qedi_ctx *qedi,
346                                        struct qedi_conn *qedi_conn)
347 {
348         u32 iscsi_cid = qedi_conn->iscsi_conn_id;
349
350         if (qedi->cid_que.conn_cid_tbl[iscsi_cid]) {
351                 iscsi_conn_printk(KERN_ALERT, qedi_conn->cls_conn->dd_data,
352                                   "conn bind - entry #%d not free\n",
353                                   iscsi_cid);
354                 return -EBUSY;
355         }
356
357         qedi->cid_que.conn_cid_tbl[iscsi_cid] = qedi_conn;
358         return 0;
359 }
360
361 struct qedi_conn *qedi_get_conn_from_id(struct qedi_ctx *qedi, u32 iscsi_cid)
362 {
363         if (!qedi->cid_que.conn_cid_tbl) {
364                 QEDI_ERR(&qedi->dbg_ctx, "missing conn<->cid table\n");
365                 return NULL;
366
367         } else if (iscsi_cid >= qedi->max_active_conns) {
368                 QEDI_ERR(&qedi->dbg_ctx, "wrong cid #%d\n", iscsi_cid);
369                 return NULL;
370         }
371         return qedi->cid_que.conn_cid_tbl[iscsi_cid];
372 }
373
374 static int qedi_conn_bind(struct iscsi_cls_session *cls_session,
375                           struct iscsi_cls_conn *cls_conn,
376                           u64 transport_fd, int is_leading)
377 {
378         struct iscsi_conn *conn = cls_conn->dd_data;
379         struct qedi_conn *qedi_conn = conn->dd_data;
380         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
381         struct qedi_ctx *qedi = iscsi_host_priv(shost);
382         struct qedi_endpoint *qedi_ep;
383         struct iscsi_endpoint *ep;
384
385         ep = iscsi_lookup_endpoint(transport_fd);
386         if (!ep)
387                 return -EINVAL;
388
389         qedi_ep = ep->dd_data;
390         if ((qedi_ep->state == EP_STATE_TCP_FIN_RCVD) ||
391             (qedi_ep->state == EP_STATE_TCP_RST_RCVD))
392                 return -EINVAL;
393
394         if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
395                 return -EINVAL;
396
397         qedi_ep->conn = qedi_conn;
398         qedi_conn->ep = qedi_ep;
399         qedi_conn->iscsi_conn_id = qedi_ep->iscsi_cid;
400         qedi_conn->fw_cid = qedi_ep->fw_cid;
401         qedi_conn->cmd_cleanup_req = 0;
402         qedi_conn->cmd_cleanup_cmpl = 0;
403
404         if (qedi_bind_conn_to_iscsi_cid(qedi, qedi_conn))
405                 return -EINVAL;
406
407         spin_lock_init(&qedi_conn->tmf_work_lock);
408         INIT_LIST_HEAD(&qedi_conn->tmf_work_list);
409         init_waitqueue_head(&qedi_conn->wait_queue);
410         return 0;
411 }
412
413 static int qedi_iscsi_update_conn(struct qedi_ctx *qedi,
414                                   struct qedi_conn *qedi_conn)
415 {
416         struct qed_iscsi_params_update *conn_info;
417         struct iscsi_cls_conn *cls_conn = qedi_conn->cls_conn;
418         struct iscsi_conn *conn = cls_conn->dd_data;
419         struct qedi_endpoint *qedi_ep;
420         int rval;
421
422         qedi_ep = qedi_conn->ep;
423
424         conn_info = kzalloc(sizeof(*conn_info), GFP_KERNEL);
425         if (!conn_info) {
426                 QEDI_ERR(&qedi->dbg_ctx, "memory alloc failed\n");
427                 return -ENOMEM;
428         }
429
430         conn_info->update_flag = 0;
431
432         if (conn->hdrdgst_en)
433                 SET_FIELD(conn_info->update_flag,
434                           ISCSI_CONN_UPDATE_RAMROD_PARAMS_HD_EN, true);
435         if (conn->datadgst_en)
436                 SET_FIELD(conn_info->update_flag,
437                           ISCSI_CONN_UPDATE_RAMROD_PARAMS_DD_EN, true);
438         if (conn->session->initial_r2t_en)
439                 SET_FIELD(conn_info->update_flag,
440                           ISCSI_CONN_UPDATE_RAMROD_PARAMS_INITIAL_R2T,
441                           true);
442         if (conn->session->imm_data_en)
443                 SET_FIELD(conn_info->update_flag,
444                           ISCSI_CONN_UPDATE_RAMROD_PARAMS_IMMEDIATE_DATA,
445                           true);
446
447         conn_info->max_seq_size = conn->session->max_burst;
448         conn_info->max_recv_pdu_length = conn->max_recv_dlength;
449         conn_info->max_send_pdu_length = conn->max_xmit_dlength;
450         conn_info->first_seq_length = conn->session->first_burst;
451         conn_info->exp_stat_sn = conn->exp_statsn;
452
453         rval = qedi_ops->update_conn(qedi->cdev, qedi_ep->handle,
454                                      conn_info);
455         if (rval) {
456                 rval = -ENXIO;
457                 QEDI_ERR(&qedi->dbg_ctx, "Could not update connection\n");
458         }
459
460         kfree(conn_info);
461         return rval;
462 }
463
464 static u16 qedi_calc_mss(u16 pmtu, u8 is_ipv6, u8 tcp_ts_en, u8 vlan_en)
465 {
466         u16 mss = 0;
467         u16 hdrs = TCP_HDR_LEN;
468
469         if (is_ipv6)
470                 hdrs += IPV6_HDR_LEN;
471         else
472                 hdrs += IPV4_HDR_LEN;
473
474         mss = pmtu - hdrs;
475
476         if (!mss)
477                 mss = DEF_MSS;
478
479         return mss;
480 }
481
482 static int qedi_iscsi_offload_conn(struct qedi_endpoint *qedi_ep)
483 {
484         struct qedi_ctx *qedi = qedi_ep->qedi;
485         struct qed_iscsi_params_offload *conn_info;
486         int rval;
487         int i;
488
489         conn_info = kzalloc(sizeof(*conn_info), GFP_KERNEL);
490         if (!conn_info) {
491                 QEDI_ERR(&qedi->dbg_ctx,
492                          "Failed to allocate memory ep=%p\n", qedi_ep);
493                 return -ENOMEM;
494         }
495
496         ether_addr_copy(conn_info->src.mac, qedi_ep->src_mac);
497         ether_addr_copy(conn_info->dst.mac, qedi_ep->dst_mac);
498
499         conn_info->src.ip[0] = ntohl(qedi_ep->src_addr[0]);
500         conn_info->dst.ip[0] = ntohl(qedi_ep->dst_addr[0]);
501
502         if (qedi_ep->ip_type == TCP_IPV4) {
503                 conn_info->ip_version = 0;
504                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
505                           "After ntohl: src_addr=%pI4, dst_addr=%pI4\n",
506                           qedi_ep->src_addr, qedi_ep->dst_addr);
507         } else {
508                 for (i = 1; i < 4; i++) {
509                         conn_info->src.ip[i] = ntohl(qedi_ep->src_addr[i]);
510                         conn_info->dst.ip[i] = ntohl(qedi_ep->dst_addr[i]);
511                 }
512
513                 conn_info->ip_version = 1;
514                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
515                           "After ntohl: src_addr=%pI6, dst_addr=%pI6\n",
516                           qedi_ep->src_addr, qedi_ep->dst_addr);
517         }
518
519         conn_info->src.port = qedi_ep->src_port;
520         conn_info->dst.port = qedi_ep->dst_port;
521
522         conn_info->layer_code = ISCSI_SLOW_PATH_LAYER_CODE;
523         conn_info->sq_pbl_addr = qedi_ep->sq_pbl_dma;
524         conn_info->vlan_id = qedi_ep->vlan_id;
525
526         SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_TS_EN, 1);
527         SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_DA_EN, 1);
528         SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_DA_CNT_EN, 1);
529         SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_KA_EN, 1);
530
531         conn_info->default_cq = (qedi_ep->fw_cid % qedi->num_queues);
532
533         conn_info->ka_max_probe_cnt = DEF_KA_MAX_PROBE_COUNT;
534         conn_info->dup_ack_theshold = 3;
535         conn_info->rcv_wnd = 65535;
536
537         conn_info->ss_thresh = 65535;
538         conn_info->srtt = 300;
539         conn_info->rtt_var = 150;
540         conn_info->flow_label = 0;
541         conn_info->ka_timeout = DEF_KA_TIMEOUT;
542         conn_info->ka_interval = DEF_KA_INTERVAL;
543         conn_info->max_rt_time = DEF_MAX_RT_TIME;
544         conn_info->ttl = DEF_TTL;
545         conn_info->tos_or_tc = DEF_TOS;
546         conn_info->remote_port = qedi_ep->dst_port;
547         conn_info->local_port = qedi_ep->src_port;
548
549         conn_info->mss = qedi_calc_mss(qedi_ep->pmtu,
550                                        (qedi_ep->ip_type == TCP_IPV6),
551                                        1, (qedi_ep->vlan_id != 0));
552
553         conn_info->cwnd = DEF_MAX_CWND * conn_info->mss;
554         conn_info->rcv_wnd_scale = 4;
555         conn_info->da_timeout_value = 200;
556         conn_info->ack_frequency = 2;
557
558         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
559                   "Default cq index [%d], mss [%d]\n",
560                   conn_info->default_cq, conn_info->mss);
561
562         rval = qedi_ops->offload_conn(qedi->cdev, qedi_ep->handle, conn_info);
563         if (rval)
564                 QEDI_ERR(&qedi->dbg_ctx, "offload_conn returned %d, ep=%p\n",
565                          rval, qedi_ep);
566
567         kfree(conn_info);
568         return rval;
569 }
570
571 static int qedi_conn_start(struct iscsi_cls_conn *cls_conn)
572 {
573         struct iscsi_conn *conn = cls_conn->dd_data;
574         struct qedi_conn *qedi_conn = conn->dd_data;
575         struct qedi_ctx *qedi;
576         int rval;
577
578         qedi = qedi_conn->qedi;
579
580         rval = qedi_iscsi_update_conn(qedi, qedi_conn);
581         if (rval) {
582                 iscsi_conn_printk(KERN_ALERT, conn,
583                                   "conn_start: FW oflload conn failed.\n");
584                 rval = -EINVAL;
585                 goto start_err;
586         }
587
588         clear_bit(QEDI_CONN_FW_CLEANUP, &qedi_conn->flags);
589         qedi_conn->abrt_conn = 0;
590
591         rval = iscsi_conn_start(cls_conn);
592         if (rval) {
593                 iscsi_conn_printk(KERN_ALERT, conn,
594                                   "iscsi_conn_start: FW oflload conn failed!!\n");
595         }
596
597 start_err:
598         return rval;
599 }
600
601 static void qedi_conn_destroy(struct iscsi_cls_conn *cls_conn)
602 {
603         struct iscsi_conn *conn = cls_conn->dd_data;
604         struct qedi_conn *qedi_conn = conn->dd_data;
605         struct Scsi_Host *shost;
606         struct qedi_ctx *qedi;
607
608         shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
609         qedi = iscsi_host_priv(shost);
610
611         qedi_conn_free_login_resources(qedi, qedi_conn);
612         iscsi_conn_teardown(cls_conn);
613 }
614
615 static int qedi_ep_get_param(struct iscsi_endpoint *ep,
616                              enum iscsi_param param, char *buf)
617 {
618         struct qedi_endpoint *qedi_ep = ep->dd_data;
619         int len;
620
621         if (!qedi_ep)
622                 return -ENOTCONN;
623
624         switch (param) {
625         case ISCSI_PARAM_CONN_PORT:
626                 len = sprintf(buf, "%hu\n", qedi_ep->dst_port);
627                 break;
628         case ISCSI_PARAM_CONN_ADDRESS:
629                 if (qedi_ep->ip_type == TCP_IPV4)
630                         len = sprintf(buf, "%pI4\n", qedi_ep->dst_addr);
631                 else
632                         len = sprintf(buf, "%pI6\n", qedi_ep->dst_addr);
633                 break;
634         default:
635                 return -ENOTCONN;
636         }
637
638         return len;
639 }
640
641 static int qedi_host_get_param(struct Scsi_Host *shost,
642                                enum iscsi_host_param param, char *buf)
643 {
644         struct qedi_ctx *qedi;
645         int len;
646
647         qedi = iscsi_host_priv(shost);
648
649         switch (param) {
650         case ISCSI_HOST_PARAM_HWADDRESS:
651                 len = sysfs_format_mac(buf, qedi->mac, 6);
652                 break;
653         case ISCSI_HOST_PARAM_NETDEV_NAME:
654                 len = sprintf(buf, "host%d\n", shost->host_no);
655                 break;
656         case ISCSI_HOST_PARAM_IPADDRESS:
657                 if (qedi->ip_type == TCP_IPV4)
658                         len = sprintf(buf, "%pI4\n", qedi->src_ip);
659                 else
660                         len = sprintf(buf, "%pI6\n", qedi->src_ip);
661                 break;
662         default:
663                 return iscsi_host_get_param(shost, param, buf);
664         }
665
666         return len;
667 }
668
669 static void qedi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
670                                 struct iscsi_stats *stats)
671 {
672         struct iscsi_conn *conn = cls_conn->dd_data;
673         struct qed_iscsi_stats iscsi_stats;
674         struct Scsi_Host *shost;
675         struct qedi_ctx *qedi;
676
677         shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
678         qedi = iscsi_host_priv(shost);
679         qedi_ops->get_stats(qedi->cdev, &iscsi_stats);
680
681         conn->txdata_octets = iscsi_stats.iscsi_tx_bytes_cnt;
682         conn->rxdata_octets = iscsi_stats.iscsi_rx_bytes_cnt;
683         conn->dataout_pdus_cnt = (uint32_t)iscsi_stats.iscsi_tx_data_pdu_cnt;
684         conn->datain_pdus_cnt = (uint32_t)iscsi_stats.iscsi_rx_data_pdu_cnt;
685         conn->r2t_pdus_cnt = (uint32_t)iscsi_stats.iscsi_rx_r2t_pdu_cnt;
686
687         stats->txdata_octets = conn->txdata_octets;
688         stats->rxdata_octets = conn->rxdata_octets;
689         stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
690         stats->dataout_pdus = conn->dataout_pdus_cnt;
691         stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
692         stats->datain_pdus = conn->datain_pdus_cnt;
693         stats->r2t_pdus = conn->r2t_pdus_cnt;
694         stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
695         stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
696         stats->digest_err = 0;
697         stats->timeout_err = 0;
698         strcpy(stats->custom[0].desc, "eh_abort_cnt");
699         stats->custom[0].value = conn->eh_abort_cnt;
700         stats->custom_length = 1;
701 }
702
703 static void qedi_iscsi_prep_generic_pdu_bd(struct qedi_conn *qedi_conn)
704 {
705         struct scsi_sge *bd_tbl;
706
707         bd_tbl = (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
708
709         bd_tbl->sge_addr.hi =
710                 (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
711         bd_tbl->sge_addr.lo = (u32)qedi_conn->gen_pdu.req_dma_addr;
712         bd_tbl->sge_len = qedi_conn->gen_pdu.req_wr_ptr -
713                                 qedi_conn->gen_pdu.req_buf;
714         bd_tbl = (struct scsi_sge  *)qedi_conn->gen_pdu.resp_bd_tbl;
715         bd_tbl->sge_addr.hi =
716                         (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
717         bd_tbl->sge_addr.lo = (u32)qedi_conn->gen_pdu.resp_dma_addr;
718         bd_tbl->sge_len = ISCSI_DEF_MAX_RECV_SEG_LEN;
719 }
720
721 static int qedi_iscsi_send_generic_request(struct iscsi_task *task)
722 {
723         struct qedi_cmd *cmd = task->dd_data;
724         struct qedi_conn *qedi_conn = cmd->conn;
725         char *buf;
726         int data_len;
727         int rc = 0;
728
729         qedi_iscsi_prep_generic_pdu_bd(qedi_conn);
730         switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
731         case ISCSI_OP_LOGIN:
732                 qedi_send_iscsi_login(qedi_conn, task);
733                 break;
734         case ISCSI_OP_NOOP_OUT:
735                 data_len = qedi_conn->gen_pdu.req_buf_size;
736                 buf = qedi_conn->gen_pdu.req_buf;
737                 if (data_len)
738                         rc = qedi_send_iscsi_nopout(qedi_conn, task,
739                                                     buf, data_len, 1);
740                 else
741                         rc = qedi_send_iscsi_nopout(qedi_conn, task,
742                                                     NULL, 0, 1);
743                 break;
744         case ISCSI_OP_LOGOUT:
745                 rc = qedi_send_iscsi_logout(qedi_conn, task);
746                 break;
747         case ISCSI_OP_SCSI_TMFUNC:
748                 rc = qedi_iscsi_abort_work(qedi_conn, task);
749                 break;
750         case ISCSI_OP_TEXT:
751                 rc = qedi_send_iscsi_text(qedi_conn, task);
752                 break;
753         default:
754                 iscsi_conn_printk(KERN_ALERT, qedi_conn->cls_conn->dd_data,
755                                   "unsupported op 0x%x\n", task->hdr->opcode);
756         }
757
758         return rc;
759 }
760
761 static int qedi_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
762 {
763         struct qedi_conn *qedi_conn = conn->dd_data;
764         struct qedi_cmd *cmd = task->dd_data;
765
766         memset(qedi_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
767
768         qedi_conn->gen_pdu.req_buf_size = task->data_count;
769
770         if (task->data_count) {
771                 memcpy(qedi_conn->gen_pdu.req_buf, task->data,
772                        task->data_count);
773                 qedi_conn->gen_pdu.req_wr_ptr =
774                         qedi_conn->gen_pdu.req_buf + task->data_count;
775         }
776
777         cmd->conn = conn->dd_data;
778         cmd->scsi_cmd = NULL;
779         return qedi_iscsi_send_generic_request(task);
780 }
781
782 static int qedi_task_xmit(struct iscsi_task *task)
783 {
784         struct iscsi_conn *conn = task->conn;
785         struct qedi_conn *qedi_conn = conn->dd_data;
786         struct qedi_cmd *cmd = task->dd_data;
787         struct scsi_cmnd *sc = task->sc;
788
789         cmd->state = 0;
790         cmd->task = NULL;
791         cmd->use_slowpath = false;
792         cmd->conn = qedi_conn;
793         cmd->task = task;
794         cmd->io_cmd_in_list = false;
795         INIT_LIST_HEAD(&cmd->io_cmd);
796
797         if (!sc)
798                 return qedi_mtask_xmit(conn, task);
799
800         cmd->scsi_cmd = sc;
801         return qedi_iscsi_send_ioreq(task);
802 }
803
804 static struct iscsi_endpoint *
805 qedi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
806                 int non_blocking)
807 {
808         struct qedi_ctx *qedi;
809         struct iscsi_endpoint *ep;
810         struct qedi_endpoint *qedi_ep;
811         struct sockaddr_in *addr;
812         struct sockaddr_in6 *addr6;
813         struct qed_dev *cdev  =  NULL;
814         struct qedi_uio_dev *udev = NULL;
815         struct iscsi_path path_req;
816         u32 msg_type = ISCSI_KEVENT_IF_DOWN;
817         u32 iscsi_cid = QEDI_CID_RESERVED;
818         u16 len = 0;
819         char *buf = NULL;
820         int ret, tmp;
821
822         if (!shost) {
823                 ret = -ENXIO;
824                 QEDI_ERR(NULL, "shost is NULL\n");
825                 return ERR_PTR(ret);
826         }
827
828         if (qedi_do_not_recover) {
829                 ret = -ENOMEM;
830                 return ERR_PTR(ret);
831         }
832
833         qedi = iscsi_host_priv(shost);
834         cdev = qedi->cdev;
835         udev = qedi->udev;
836
837         if (test_bit(QEDI_IN_OFFLINE, &qedi->flags) ||
838             test_bit(QEDI_IN_RECOVERY, &qedi->flags)) {
839                 ret = -ENOMEM;
840                 return ERR_PTR(ret);
841         }
842
843         ep = iscsi_create_endpoint(sizeof(struct qedi_endpoint));
844         if (!ep) {
845                 QEDI_ERR(&qedi->dbg_ctx, "endpoint create fail\n");
846                 ret = -ENOMEM;
847                 return ERR_PTR(ret);
848         }
849         qedi_ep = ep->dd_data;
850         memset(qedi_ep, 0, sizeof(struct qedi_endpoint));
851         qedi_ep->state = EP_STATE_IDLE;
852         qedi_ep->iscsi_cid = (u32)-1;
853         qedi_ep->qedi = qedi;
854
855         if (dst_addr->sa_family == AF_INET) {
856                 addr = (struct sockaddr_in *)dst_addr;
857                 memcpy(qedi_ep->dst_addr, &addr->sin_addr.s_addr,
858                        sizeof(struct in_addr));
859                 qedi_ep->dst_port = ntohs(addr->sin_port);
860                 qedi_ep->ip_type = TCP_IPV4;
861                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
862                           "dst_addr=%pI4, dst_port=%u\n",
863                           qedi_ep->dst_addr, qedi_ep->dst_port);
864         } else if (dst_addr->sa_family == AF_INET6) {
865                 addr6 = (struct sockaddr_in6 *)dst_addr;
866                 memcpy(qedi_ep->dst_addr, &addr6->sin6_addr,
867                        sizeof(struct in6_addr));
868                 qedi_ep->dst_port = ntohs(addr6->sin6_port);
869                 qedi_ep->ip_type = TCP_IPV6;
870                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
871                           "dst_addr=%pI6, dst_port=%u\n",
872                           qedi_ep->dst_addr, qedi_ep->dst_port);
873         } else {
874                 QEDI_ERR(&qedi->dbg_ctx, "Invalid endpoint\n");
875         }
876
877         if (atomic_read(&qedi->link_state) != QEDI_LINK_UP) {
878                 QEDI_WARN(&qedi->dbg_ctx, "qedi link down\n");
879                 ret = -ENXIO;
880                 goto ep_conn_exit;
881         }
882
883         ret = qedi_alloc_sq(qedi, qedi_ep);
884         if (ret)
885                 goto ep_conn_exit;
886
887         ret = qedi_ops->acquire_conn(qedi->cdev, &qedi_ep->handle,
888                                      &qedi_ep->fw_cid, &qedi_ep->p_doorbell);
889
890         if (ret) {
891                 QEDI_ERR(&qedi->dbg_ctx, "Could not acquire connection\n");
892                 ret = -ENXIO;
893                 goto ep_free_sq;
894         }
895
896         iscsi_cid = qedi_ep->handle;
897         qedi_ep->iscsi_cid = iscsi_cid;
898
899         init_waitqueue_head(&qedi_ep->ofld_wait);
900         init_waitqueue_head(&qedi_ep->tcp_ofld_wait);
901         qedi_ep->state = EP_STATE_OFLDCONN_START;
902         qedi->ep_tbl[iscsi_cid] = qedi_ep;
903
904         buf = (char *)&path_req;
905         len = sizeof(path_req);
906         memset(&path_req, 0, len);
907
908         msg_type = ISCSI_KEVENT_PATH_REQ;
909         path_req.handle = (u64)qedi_ep->iscsi_cid;
910         path_req.pmtu = qedi->ll2_mtu;
911         qedi_ep->pmtu = qedi->ll2_mtu;
912         if (qedi_ep->ip_type == TCP_IPV4) {
913                 memcpy(&path_req.dst.v4_addr, &qedi_ep->dst_addr,
914                        sizeof(struct in_addr));
915                 path_req.ip_addr_len = 4;
916         } else {
917                 memcpy(&path_req.dst.v6_addr, &qedi_ep->dst_addr,
918                        sizeof(struct in6_addr));
919                 path_req.ip_addr_len = 16;
920         }
921
922         ret = iscsi_offload_mesg(shost, &qedi_iscsi_transport, msg_type, buf,
923                                  len);
924         if (ret) {
925                 QEDI_ERR(&qedi->dbg_ctx,
926                          "iscsi_offload_mesg() failed for cid=0x%x ret=%d\n",
927                          iscsi_cid, ret);
928                 goto ep_rel_conn;
929         }
930
931         atomic_inc(&qedi->num_offloads);
932         return ep;
933
934 ep_rel_conn:
935         qedi->ep_tbl[iscsi_cid] = NULL;
936         tmp = qedi_ops->release_conn(qedi->cdev, qedi_ep->handle);
937         if (tmp)
938                 QEDI_WARN(&qedi->dbg_ctx, "release_conn returned %d\n",
939                           tmp);
940 ep_free_sq:
941         qedi_free_sq(qedi, qedi_ep);
942 ep_conn_exit:
943         iscsi_destroy_endpoint(ep);
944         return ERR_PTR(ret);
945 }
946
947 static int qedi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
948 {
949         struct qedi_endpoint *qedi_ep;
950         int ret = 0;
951
952         if (qedi_do_not_recover)
953                 return 1;
954
955         qedi_ep = ep->dd_data;
956         if (qedi_ep->state == EP_STATE_IDLE ||
957             qedi_ep->state == EP_STATE_OFLDCONN_FAILED)
958                 return -1;
959
960         if (qedi_ep->state == EP_STATE_OFLDCONN_COMPL)
961                 ret = 1;
962
963         ret = wait_event_interruptible_timeout(qedi_ep->ofld_wait,
964                                                QEDI_OFLD_WAIT_STATE(qedi_ep),
965                                                msecs_to_jiffies(timeout_ms));
966
967         if (qedi_ep->state == EP_STATE_OFLDCONN_FAILED)
968                 ret = -1;
969
970         if (ret > 0)
971                 return 1;
972         else if (!ret)
973                 return 0;
974         else
975                 return ret;
976 }
977
978 static void qedi_cleanup_active_cmd_list(struct qedi_conn *qedi_conn)
979 {
980         struct qedi_cmd *cmd, *cmd_tmp;
981
982         list_for_each_entry_safe(cmd, cmd_tmp, &qedi_conn->active_cmd_list,
983                                  io_cmd) {
984                 list_del_init(&cmd->io_cmd);
985                 qedi_conn->active_cmd_count--;
986         }
987 }
988
989 static void qedi_ep_disconnect(struct iscsi_endpoint *ep)
990 {
991         struct qedi_endpoint *qedi_ep;
992         struct qedi_conn *qedi_conn = NULL;
993         struct iscsi_conn *conn = NULL;
994         struct qedi_ctx *qedi;
995         int ret = 0;
996         int wait_delay = 20 * HZ;
997         int abrt_conn = 0;
998         int count = 10;
999
1000         qedi_ep = ep->dd_data;
1001         qedi = qedi_ep->qedi;
1002
1003         flush_work(&qedi_ep->offload_work);
1004
1005         if (qedi_ep->conn) {
1006                 qedi_conn = qedi_ep->conn;
1007                 conn = qedi_conn->cls_conn->dd_data;
1008                 iscsi_suspend_queue(conn);
1009                 abrt_conn = qedi_conn->abrt_conn;
1010
1011                 while (count--) {
1012                         if (!test_bit(QEDI_CONN_FW_CLEANUP,
1013                                       &qedi_conn->flags)) {
1014                                 break;
1015                         }
1016                         msleep(1000);
1017                 }
1018
1019                 if (test_bit(QEDI_IN_RECOVERY, &qedi->flags)) {
1020                         if (qedi_do_not_recover) {
1021                                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1022                                           "Do not recover cid=0x%x\n",
1023                                           qedi_ep->iscsi_cid);
1024                                 goto ep_exit_recover;
1025                         }
1026                         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1027                                   "Reset recovery cid=0x%x, qedi_ep=%p, state=0x%x\n",
1028                                   qedi_ep->iscsi_cid, qedi_ep, qedi_ep->state);
1029                         qedi_cleanup_active_cmd_list(qedi_conn);
1030                         goto ep_release_conn;
1031                 }
1032         }
1033
1034         if (qedi_do_not_recover)
1035                 goto ep_exit_recover;
1036
1037         switch (qedi_ep->state) {
1038         case EP_STATE_OFLDCONN_START:
1039                 goto ep_release_conn;
1040         case EP_STATE_OFLDCONN_FAILED:
1041                         break;
1042         case EP_STATE_OFLDCONN_COMPL:
1043                 if (unlikely(!qedi_conn))
1044                         break;
1045
1046                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1047                           "Active cmd count=%d, abrt_conn=%d, ep state=0x%x, cid=0x%x, qedi_conn=%p\n",
1048                           qedi_conn->active_cmd_count, abrt_conn,
1049                           qedi_ep->state,
1050                           qedi_ep->iscsi_cid,
1051                           qedi_ep->conn
1052                           );
1053
1054                 if (!qedi_conn->active_cmd_count)
1055                         abrt_conn = 0;
1056                 else
1057                         abrt_conn = 1;
1058
1059                 if (abrt_conn)
1060                         qedi_clearsq(qedi, qedi_conn, NULL);
1061                 break;
1062         default:
1063                 break;
1064         }
1065
1066         qedi_ep->state = EP_STATE_DISCONN_START;
1067         ret = qedi_ops->destroy_conn(qedi->cdev, qedi_ep->handle, abrt_conn);
1068         if (ret) {
1069                 QEDI_WARN(&qedi->dbg_ctx,
1070                           "destroy_conn failed returned %d\n", ret);
1071         } else {
1072                 ret = wait_event_interruptible_timeout(
1073                                         qedi_ep->tcp_ofld_wait,
1074                                         (qedi_ep->state !=
1075                                          EP_STATE_DISCONN_START),
1076                                         wait_delay);
1077                 if ((ret <= 0) || (qedi_ep->state == EP_STATE_DISCONN_START)) {
1078                         QEDI_WARN(&qedi->dbg_ctx,
1079                                   "Destroy conn timedout or interrupted, ret=%d, delay=%d, cid=0x%x\n",
1080                                   ret, wait_delay, qedi_ep->iscsi_cid);
1081                 }
1082         }
1083
1084 ep_release_conn:
1085         ret = qedi_ops->release_conn(qedi->cdev, qedi_ep->handle);
1086         if (ret)
1087                 QEDI_WARN(&qedi->dbg_ctx,
1088                           "release_conn returned %d, cid=0x%x\n",
1089                           ret, qedi_ep->iscsi_cid);
1090 ep_exit_recover:
1091         qedi_ep->state = EP_STATE_IDLE;
1092         qedi->ep_tbl[qedi_ep->iscsi_cid] = NULL;
1093         qedi->cid_que.conn_cid_tbl[qedi_ep->iscsi_cid] = NULL;
1094         qedi_free_id(&qedi->lcl_port_tbl, qedi_ep->src_port);
1095         qedi_free_sq(qedi, qedi_ep);
1096
1097         if (qedi_conn)
1098                 qedi_conn->ep = NULL;
1099
1100         qedi_ep->conn = NULL;
1101         qedi_ep->qedi = NULL;
1102         atomic_dec(&qedi->num_offloads);
1103
1104         iscsi_destroy_endpoint(ep);
1105 }
1106
1107 static int qedi_data_avail(struct qedi_ctx *qedi, u16 vlanid)
1108 {
1109         struct qed_dev *cdev = qedi->cdev;
1110         struct qedi_uio_dev *udev;
1111         struct qedi_uio_ctrl *uctrl;
1112         struct sk_buff *skb;
1113         u32 len;
1114         int rc = 0;
1115
1116         udev = qedi->udev;
1117         if (!udev) {
1118                 QEDI_ERR(&qedi->dbg_ctx, "udev is NULL.\n");
1119                 return -EINVAL;
1120         }
1121
1122         uctrl = (struct qedi_uio_ctrl *)udev->uctrl;
1123         if (!uctrl) {
1124                 QEDI_ERR(&qedi->dbg_ctx, "uctlr is NULL.\n");
1125                 return -EINVAL;
1126         }
1127
1128         len = uctrl->host_tx_pkt_len;
1129         if (!len) {
1130                 QEDI_ERR(&qedi->dbg_ctx, "Invalid len %u\n", len);
1131                 return -EINVAL;
1132         }
1133
1134         skb = alloc_skb(len, GFP_ATOMIC);
1135         if (!skb) {
1136                 QEDI_ERR(&qedi->dbg_ctx, "alloc_skb failed\n");
1137                 return -EINVAL;
1138         }
1139
1140         skb_put(skb, len);
1141         memcpy(skb->data, udev->tx_pkt, len);
1142         skb->ip_summed = CHECKSUM_NONE;
1143
1144         if (vlanid)
1145                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
1146
1147         rc = qedi_ops->ll2->start_xmit(cdev, skb, 0);
1148         if (rc) {
1149                 QEDI_ERR(&qedi->dbg_ctx, "ll2 start_xmit returned %d\n",
1150                          rc);
1151                 kfree_skb(skb);
1152         }
1153
1154         uctrl->host_tx_pkt_len = 0;
1155         uctrl->hw_tx_cons++;
1156
1157         return rc;
1158 }
1159
1160 static void qedi_offload_work(struct work_struct *work)
1161 {
1162         struct qedi_endpoint *qedi_ep =
1163                 container_of(work, struct qedi_endpoint, offload_work);
1164         struct qedi_ctx *qedi;
1165         int wait_delay = 20 * HZ;
1166         int ret;
1167
1168         qedi = qedi_ep->qedi;
1169
1170         ret = qedi_iscsi_offload_conn(qedi_ep);
1171         if (ret) {
1172                 QEDI_ERR(&qedi->dbg_ctx,
1173                          "offload error: iscsi_cid=%u, qedi_ep=%p, ret=%d\n",
1174                          qedi_ep->iscsi_cid, qedi_ep, ret);
1175                 qedi_ep->state = EP_STATE_OFLDCONN_FAILED;
1176                 return;
1177         }
1178
1179         ret = wait_event_interruptible_timeout(qedi_ep->tcp_ofld_wait,
1180                                                (qedi_ep->state ==
1181                                                EP_STATE_OFLDCONN_COMPL),
1182                                                wait_delay);
1183         if ((ret <= 0) || (qedi_ep->state != EP_STATE_OFLDCONN_COMPL)) {
1184                 qedi_ep->state = EP_STATE_OFLDCONN_FAILED;
1185                 QEDI_ERR(&qedi->dbg_ctx,
1186                          "Offload conn TIMEOUT iscsi_cid=%u, qedi_ep=%p\n",
1187                          qedi_ep->iscsi_cid, qedi_ep);
1188         }
1189 }
1190
1191 static int qedi_set_path(struct Scsi_Host *shost, struct iscsi_path *path_data)
1192 {
1193         struct qedi_ctx *qedi;
1194         struct qedi_endpoint *qedi_ep;
1195         int ret = 0;
1196         u32 iscsi_cid;
1197         u16 port_id = 0;
1198
1199         if (!shost) {
1200                 ret = -ENXIO;
1201                 QEDI_ERR(NULL, "shost is NULL\n");
1202                 return ret;
1203         }
1204
1205         if (strcmp(shost->hostt->proc_name, "qedi")) {
1206                 ret = -ENXIO;
1207                 QEDI_ERR(NULL, "shost %s is invalid\n",
1208                          shost->hostt->proc_name);
1209                 return ret;
1210         }
1211
1212         qedi = iscsi_host_priv(shost);
1213         if (path_data->handle == QEDI_PATH_HANDLE) {
1214                 ret = qedi_data_avail(qedi, path_data->vlan_id);
1215                 goto set_path_exit;
1216         }
1217
1218         iscsi_cid = (u32)path_data->handle;
1219         qedi_ep = qedi->ep_tbl[iscsi_cid];
1220         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1221                   "iscsi_cid=0x%x, qedi_ep=%p\n", iscsi_cid, qedi_ep);
1222         if (!qedi_ep) {
1223                 ret = -EINVAL;
1224                 goto set_path_exit;
1225         }
1226
1227         if (!is_valid_ether_addr(&path_data->mac_addr[0])) {
1228                 QEDI_NOTICE(&qedi->dbg_ctx, "dst mac NOT VALID\n");
1229                 ret = -EIO;
1230                 goto set_path_exit;
1231         }
1232
1233         ether_addr_copy(&qedi_ep->src_mac[0], &qedi->mac[0]);
1234         ether_addr_copy(&qedi_ep->dst_mac[0], &path_data->mac_addr[0]);
1235
1236         qedi_ep->vlan_id = path_data->vlan_id;
1237         if (path_data->pmtu < DEF_PATH_MTU) {
1238                 qedi_ep->pmtu = qedi->ll2_mtu;
1239                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1240                           "MTU cannot be %u, using default MTU %u\n",
1241                            path_data->pmtu, qedi_ep->pmtu);
1242         }
1243
1244         if (path_data->pmtu != qedi->ll2_mtu) {
1245                 if (path_data->pmtu > JUMBO_MTU) {
1246                         ret = -EINVAL;
1247                         QEDI_ERR(NULL, "Invalid MTU %u\n", path_data->pmtu);
1248                         goto set_path_exit;
1249                 }
1250
1251                 qedi_reset_host_mtu(qedi, path_data->pmtu);
1252                 qedi_ep->pmtu = qedi->ll2_mtu;
1253         }
1254
1255         port_id = qedi_ep->src_port;
1256         if (port_id >= QEDI_LOCAL_PORT_MIN &&
1257             port_id < QEDI_LOCAL_PORT_MAX) {
1258                 if (qedi_alloc_id(&qedi->lcl_port_tbl, port_id))
1259                         port_id = 0;
1260         } else {
1261                 port_id = 0;
1262         }
1263
1264         if (!port_id) {
1265                 port_id = qedi_alloc_new_id(&qedi->lcl_port_tbl);
1266                 if (port_id == QEDI_LOCAL_PORT_INVALID) {
1267                         QEDI_ERR(&qedi->dbg_ctx,
1268                                  "Failed to allocate port id for iscsi_cid=0x%x\n",
1269                                  iscsi_cid);
1270                         ret = -ENOMEM;
1271                         goto set_path_exit;
1272                 }
1273         }
1274
1275         qedi_ep->src_port = port_id;
1276
1277         if (qedi_ep->ip_type == TCP_IPV4) {
1278                 memcpy(&qedi_ep->src_addr[0], &path_data->src.v4_addr,
1279                        sizeof(struct in_addr));
1280                 memcpy(&qedi->src_ip[0], &path_data->src.v4_addr,
1281                        sizeof(struct in_addr));
1282                 qedi->ip_type = TCP_IPV4;
1283
1284                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1285                           "src addr:port=%pI4:%u, dst addr:port=%pI4:%u\n",
1286                           qedi_ep->src_addr, qedi_ep->src_port,
1287                           qedi_ep->dst_addr, qedi_ep->dst_port);
1288         } else {
1289                 memcpy(&qedi_ep->src_addr[0], &path_data->src.v6_addr,
1290                        sizeof(struct in6_addr));
1291                 memcpy(&qedi->src_ip[0], &path_data->src.v6_addr,
1292                        sizeof(struct in6_addr));
1293                 qedi->ip_type = TCP_IPV6;
1294
1295                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1296                           "src addr:port=%pI6:%u, dst addr:port=%pI6:%u\n",
1297                           qedi_ep->src_addr, qedi_ep->src_port,
1298                           qedi_ep->dst_addr, qedi_ep->dst_port);
1299         }
1300
1301         INIT_WORK(&qedi_ep->offload_work, qedi_offload_work);
1302         queue_work(qedi->offload_thread, &qedi_ep->offload_work);
1303
1304         ret = 0;
1305
1306 set_path_exit:
1307         return ret;
1308 }
1309
1310 static umode_t qedi_attr_is_visible(int param_type, int param)
1311 {
1312         switch (param_type) {
1313         case ISCSI_HOST_PARAM:
1314                 switch (param) {
1315                 case ISCSI_HOST_PARAM_NETDEV_NAME:
1316                 case ISCSI_HOST_PARAM_HWADDRESS:
1317                 case ISCSI_HOST_PARAM_IPADDRESS:
1318                         return 0444;
1319                 default:
1320                         return 0;
1321                 }
1322         case ISCSI_PARAM:
1323                 switch (param) {
1324                 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1325                 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1326                 case ISCSI_PARAM_HDRDGST_EN:
1327                 case ISCSI_PARAM_DATADGST_EN:
1328                 case ISCSI_PARAM_CONN_ADDRESS:
1329                 case ISCSI_PARAM_CONN_PORT:
1330                 case ISCSI_PARAM_EXP_STATSN:
1331                 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1332                 case ISCSI_PARAM_PERSISTENT_PORT:
1333                 case ISCSI_PARAM_PING_TMO:
1334                 case ISCSI_PARAM_RECV_TMO:
1335                 case ISCSI_PARAM_INITIAL_R2T_EN:
1336                 case ISCSI_PARAM_MAX_R2T:
1337                 case ISCSI_PARAM_IMM_DATA_EN:
1338                 case ISCSI_PARAM_FIRST_BURST:
1339                 case ISCSI_PARAM_MAX_BURST:
1340                 case ISCSI_PARAM_PDU_INORDER_EN:
1341                 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1342                 case ISCSI_PARAM_ERL:
1343                 case ISCSI_PARAM_TARGET_NAME:
1344                 case ISCSI_PARAM_TPGT:
1345                 case ISCSI_PARAM_USERNAME:
1346                 case ISCSI_PARAM_PASSWORD:
1347                 case ISCSI_PARAM_USERNAME_IN:
1348                 case ISCSI_PARAM_PASSWORD_IN:
1349                 case ISCSI_PARAM_FAST_ABORT:
1350                 case ISCSI_PARAM_ABORT_TMO:
1351                 case ISCSI_PARAM_LU_RESET_TMO:
1352                 case ISCSI_PARAM_TGT_RESET_TMO:
1353                 case ISCSI_PARAM_IFACE_NAME:
1354                 case ISCSI_PARAM_INITIATOR_NAME:
1355                 case ISCSI_PARAM_BOOT_ROOT:
1356                 case ISCSI_PARAM_BOOT_NIC:
1357                 case ISCSI_PARAM_BOOT_TARGET:
1358                         return 0444;
1359                 default:
1360                         return 0;
1361                 }
1362         }
1363
1364         return 0;
1365 }
1366
1367 static void qedi_cleanup_task(struct iscsi_task *task)
1368 {
1369         if (!task->sc || task->state == ISCSI_TASK_PENDING) {
1370                 QEDI_INFO(NULL, QEDI_LOG_IO, "Returning ref_cnt=%d\n",
1371                           refcount_read(&task->refcount));
1372                 return;
1373         }
1374
1375         qedi_iscsi_unmap_sg_list(task->dd_data);
1376 }
1377
1378 struct iscsi_transport qedi_iscsi_transport = {
1379         .owner = THIS_MODULE,
1380         .name = QEDI_MODULE_NAME,
1381         .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_MULTI_R2T | CAP_DATADGST |
1382                 CAP_DATA_PATH_OFFLOAD | CAP_TEXT_NEGO,
1383         .create_session = qedi_session_create,
1384         .destroy_session = qedi_session_destroy,
1385         .create_conn = qedi_conn_create,
1386         .bind_conn = qedi_conn_bind,
1387         .start_conn = qedi_conn_start,
1388         .stop_conn = iscsi_conn_stop,
1389         .destroy_conn = qedi_conn_destroy,
1390         .set_param = iscsi_set_param,
1391         .get_ep_param = qedi_ep_get_param,
1392         .get_conn_param = iscsi_conn_get_param,
1393         .get_session_param = iscsi_session_get_param,
1394         .get_host_param = qedi_host_get_param,
1395         .send_pdu = iscsi_conn_send_pdu,
1396         .get_stats = qedi_conn_get_stats,
1397         .xmit_task = qedi_task_xmit,
1398         .cleanup_task = qedi_cleanup_task,
1399         .session_recovery_timedout = iscsi_session_recovery_timedout,
1400         .ep_connect = qedi_ep_connect,
1401         .ep_poll = qedi_ep_poll,
1402         .ep_disconnect = qedi_ep_disconnect,
1403         .set_path = qedi_set_path,
1404         .attr_is_visible = qedi_attr_is_visible,
1405 };
1406
1407 void qedi_start_conn_recovery(struct qedi_ctx *qedi,
1408                               struct qedi_conn *qedi_conn)
1409 {
1410         struct iscsi_cls_session *cls_sess;
1411         struct iscsi_cls_conn *cls_conn;
1412         struct iscsi_conn *conn;
1413
1414         cls_conn = qedi_conn->cls_conn;
1415         conn = cls_conn->dd_data;
1416         cls_sess = iscsi_conn_to_session(cls_conn);
1417
1418         if (iscsi_is_session_online(cls_sess)) {
1419                 qedi_conn->abrt_conn = 1;
1420                 QEDI_ERR(&qedi->dbg_ctx,
1421                          "Failing connection, state=0x%x, cid=0x%x\n",
1422                          conn->session->state, qedi_conn->iscsi_conn_id);
1423                 iscsi_conn_failure(qedi_conn->cls_conn->dd_data,
1424                                    ISCSI_ERR_CONN_FAILED);
1425         }
1426 }
1427
1428 static const struct {
1429         enum iscsi_error_types error_code;
1430         char *err_string;
1431 } qedi_iscsi_error[] = {
1432         { ISCSI_STATUS_NONE,
1433           "tcp_error none"
1434         },
1435         { ISCSI_CONN_ERROR_TASK_CID_MISMATCH,
1436           "task cid mismatch"
1437         },
1438         { ISCSI_CONN_ERROR_TASK_NOT_VALID,
1439           "invalid task"
1440         },
1441         { ISCSI_CONN_ERROR_RQ_RING_IS_FULL,
1442           "rq ring full"
1443         },
1444         { ISCSI_CONN_ERROR_CMDQ_RING_IS_FULL,
1445           "cmdq ring full"
1446         },
1447         { ISCSI_CONN_ERROR_HQE_CACHING_FAILED,
1448           "sge caching failed"
1449         },
1450         { ISCSI_CONN_ERROR_HEADER_DIGEST_ERROR,
1451           "hdr digest error"
1452         },
1453         { ISCSI_CONN_ERROR_LOCAL_COMPLETION_ERROR,
1454           "local cmpl error"
1455         },
1456         { ISCSI_CONN_ERROR_DATA_OVERRUN,
1457           "invalid task"
1458         },
1459         { ISCSI_CONN_ERROR_OUT_OF_SGES_ERROR,
1460           "out of sge error"
1461         },
1462         { ISCSI_CONN_ERROR_TCP_IP_FRAGMENT_ERROR,
1463           "tcp ip fragment error"
1464         },
1465         { ISCSI_CONN_ERROR_PROTOCOL_ERR_AHS_LEN,
1466           "AHS len protocol error"
1467         },
1468         { ISCSI_CONN_ERROR_PROTOCOL_ERR_ITT_OUT_OF_RANGE,
1469           "itt out of range error"
1470         },
1471         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SEG_LEN_EXCEEDS_PDU_SIZE,
1472           "data seg more than pdu size"
1473         },
1474         { ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_OPCODE,
1475           "invalid opcode"
1476         },
1477         { ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_OPCODE_BEFORE_UPDATE,
1478           "invalid opcode before update"
1479         },
1480         { ISCSI_CONN_ERROR_UNVALID_NOPIN_DSL,
1481           "unexpected opcode"
1482         },
1483         { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_CARRIES_NO_DATA,
1484           "r2t carries no data"
1485         },
1486         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SN,
1487           "data sn error"
1488         },
1489         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_IN_TTT,
1490           "data TTT error"
1491         },
1492         { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_TTT,
1493           "r2t TTT error"
1494         },
1495         { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_BUFFER_OFFSET,
1496           "buffer offset error"
1497         },
1498         { ISCSI_CONN_ERROR_PROTOCOL_ERR_BUFFER_OFFSET_OOO,
1499           "buffer offset ooo"
1500         },
1501         { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_SN,
1502           "data seg len 0"
1503         },
1504         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_0,
1505           "data xer len error"
1506         },
1507         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_1,
1508           "data xer len1 error"
1509         },
1510         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_2,
1511           "data xer len2 error"
1512         },
1513         { ISCSI_CONN_ERROR_PROTOCOL_ERR_LUN,
1514           "protocol lun error"
1515         },
1516         { ISCSI_CONN_ERROR_PROTOCOL_ERR_F_BIT_ZERO,
1517           "f bit zero error"
1518         },
1519         { ISCSI_CONN_ERROR_PROTOCOL_ERR_EXP_STAT_SN,
1520           "exp stat sn error"
1521         },
1522         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DSL_NOT_ZERO,
1523           "dsl not zero error"
1524         },
1525         { ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_DSL,
1526           "invalid dsl"
1527         },
1528         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SEG_LEN_TOO_BIG,
1529           "data seg len too big"
1530         },
1531         { ISCSI_CONN_ERROR_PROTOCOL_ERR_OUTSTANDING_R2T_COUNT,
1532           "outstanding r2t count error"
1533         },
1534         { ISCSI_CONN_ERROR_SENSE_DATA_LENGTH,
1535           "sense datalen error"
1536         },
1537 };
1538
1539 char *qedi_get_iscsi_error(enum iscsi_error_types err_code)
1540 {
1541         int i;
1542         char *msg = NULL;
1543
1544         for (i = 0; i < ARRAY_SIZE(qedi_iscsi_error); i++) {
1545                 if (qedi_iscsi_error[i].error_code == err_code) {
1546                         msg = qedi_iscsi_error[i].err_string;
1547                         break;
1548                 }
1549         }
1550         return msg;
1551 }
1552
1553 void qedi_process_iscsi_error(struct qedi_endpoint *ep,
1554                               struct iscsi_eqe_data *data)
1555 {
1556         struct qedi_conn *qedi_conn;
1557         struct qedi_ctx *qedi;
1558         char warn_notice[] = "iscsi_warning";
1559         char error_notice[] = "iscsi_error";
1560         char unknown_msg[] = "Unknown error";
1561         char *message;
1562         int need_recovery = 0;
1563         u32 err_mask = 0;
1564         char *msg;
1565
1566         if (!ep)
1567                 return;
1568
1569         qedi_conn = ep->conn;
1570         if (!qedi_conn)
1571                 return;
1572
1573         qedi = ep->qedi;
1574
1575         QEDI_ERR(&qedi->dbg_ctx, "async event iscsi error:0x%x\n",
1576                  data->error_code);
1577
1578         if (err_mask) {
1579                 need_recovery = 0;
1580                 message = warn_notice;
1581         } else {
1582                 need_recovery = 1;
1583                 message = error_notice;
1584         }
1585
1586         msg = qedi_get_iscsi_error(data->error_code);
1587         if (!msg) {
1588                 need_recovery = 0;
1589                 msg = unknown_msg;
1590         }
1591
1592         iscsi_conn_printk(KERN_ALERT,
1593                           qedi_conn->cls_conn->dd_data,
1594                           "qedi: %s - %s\n", message, msg);
1595
1596         if (need_recovery)
1597                 qedi_start_conn_recovery(qedi_conn->qedi, qedi_conn);
1598 }
1599
1600 void qedi_process_tcp_error(struct qedi_endpoint *ep,
1601                             struct iscsi_eqe_data *data)
1602 {
1603         struct qedi_conn *qedi_conn;
1604
1605         if (!ep)
1606                 return;
1607
1608         qedi_conn = ep->conn;
1609         if (!qedi_conn)
1610                 return;
1611
1612         QEDI_ERR(&ep->qedi->dbg_ctx, "async event TCP error:0x%x\n",
1613                  data->error_code);
1614
1615         qedi_start_conn_recovery(qedi_conn->qedi, qedi_conn);
1616 }