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