]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/qedi/qedi_iscsi.c
Merge branch 'drm-next-4.12' of git://people.freedesktop.org/~agd5f/linux into drm...
[karo-tx-linux.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         .cmd_per_lun = 128,
63         .use_clustering = ENABLE_CLUSTERING,
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         if (vlan_en)
474                 hdrs += VLAN_LEN;
475
476         mss = pmtu - hdrs;
477
478         if (tcp_ts_en)
479                 mss -= TCP_OPTION_LEN;
480
481         if (!mss)
482                 mss = DEF_MSS;
483
484         return mss;
485 }
486
487 static int qedi_iscsi_offload_conn(struct qedi_endpoint *qedi_ep)
488 {
489         struct qedi_ctx *qedi = qedi_ep->qedi;
490         struct qed_iscsi_params_offload *conn_info;
491         int rval;
492         int i;
493
494         conn_info = kzalloc(sizeof(*conn_info), GFP_KERNEL);
495         if (!conn_info) {
496                 QEDI_ERR(&qedi->dbg_ctx,
497                          "Failed to allocate memory ep=%p\n", qedi_ep);
498                 return -ENOMEM;
499         }
500
501         ether_addr_copy(conn_info->src.mac, qedi_ep->src_mac);
502         ether_addr_copy(conn_info->dst.mac, qedi_ep->dst_mac);
503
504         conn_info->src.ip[0] = ntohl(qedi_ep->src_addr[0]);
505         conn_info->dst.ip[0] = ntohl(qedi_ep->dst_addr[0]);
506
507         if (qedi_ep->ip_type == TCP_IPV4) {
508                 conn_info->ip_version = 0;
509                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
510                           "After ntohl: src_addr=%pI4, dst_addr=%pI4\n",
511                           qedi_ep->src_addr, qedi_ep->dst_addr);
512         } else {
513                 for (i = 1; i < 4; i++) {
514                         conn_info->src.ip[i] = ntohl(qedi_ep->src_addr[i]);
515                         conn_info->dst.ip[i] = ntohl(qedi_ep->dst_addr[i]);
516                 }
517
518                 conn_info->ip_version = 1;
519                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
520                           "After ntohl: src_addr=%pI6, dst_addr=%pI6\n",
521                           qedi_ep->src_addr, qedi_ep->dst_addr);
522         }
523
524         conn_info->src.port = qedi_ep->src_port;
525         conn_info->dst.port = qedi_ep->dst_port;
526
527         conn_info->layer_code = ISCSI_SLOW_PATH_LAYER_CODE;
528         conn_info->sq_pbl_addr = qedi_ep->sq_pbl_dma;
529         conn_info->vlan_id = qedi_ep->vlan_id;
530
531         SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_TS_EN, 1);
532         SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_DA_EN, 1);
533         SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_DA_CNT_EN, 1);
534         SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_KA_EN, 1);
535
536         conn_info->default_cq = (qedi_ep->fw_cid % 8);
537
538         conn_info->ka_max_probe_cnt = DEF_KA_MAX_PROBE_COUNT;
539         conn_info->dup_ack_theshold = 3;
540         conn_info->rcv_wnd = 65535;
541         conn_info->cwnd = DEF_MAX_CWND;
542
543         conn_info->ss_thresh = 65535;
544         conn_info->srtt = 300;
545         conn_info->rtt_var = 150;
546         conn_info->flow_label = 0;
547         conn_info->ka_timeout = DEF_KA_TIMEOUT;
548         conn_info->ka_interval = DEF_KA_INTERVAL;
549         conn_info->max_rt_time = DEF_MAX_RT_TIME;
550         conn_info->ttl = DEF_TTL;
551         conn_info->tos_or_tc = DEF_TOS;
552         conn_info->remote_port = qedi_ep->dst_port;
553         conn_info->local_port = qedi_ep->src_port;
554
555         conn_info->mss = qedi_calc_mss(qedi_ep->pmtu,
556                                        (qedi_ep->ip_type == TCP_IPV6),
557                                        1, (qedi_ep->vlan_id != 0));
558
559         conn_info->rcv_wnd_scale = 4;
560         conn_info->ts_ticks_per_second = 1000;
561         conn_info->da_timeout_value = 200;
562         conn_info->ack_frequency = 2;
563
564         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
565                   "Default cq index [%d], mss [%d]\n",
566                   conn_info->default_cq, conn_info->mss);
567
568         rval = qedi_ops->offload_conn(qedi->cdev, qedi_ep->handle, conn_info);
569         if (rval)
570                 QEDI_ERR(&qedi->dbg_ctx, "offload_conn returned %d, ep=%p\n",
571                          rval, qedi_ep);
572
573         kfree(conn_info);
574         return rval;
575 }
576
577 static int qedi_conn_start(struct iscsi_cls_conn *cls_conn)
578 {
579         struct iscsi_conn *conn = cls_conn->dd_data;
580         struct qedi_conn *qedi_conn = conn->dd_data;
581         struct qedi_ctx *qedi;
582         int rval;
583
584         qedi = qedi_conn->qedi;
585
586         rval = qedi_iscsi_update_conn(qedi, qedi_conn);
587         if (rval) {
588                 iscsi_conn_printk(KERN_ALERT, conn,
589                                   "conn_start: FW oflload conn failed.\n");
590                 rval = -EINVAL;
591                 goto start_err;
592         }
593
594         clear_bit(QEDI_CONN_FW_CLEANUP, &qedi_conn->flags);
595         qedi_conn->abrt_conn = 0;
596
597         rval = iscsi_conn_start(cls_conn);
598         if (rval) {
599                 iscsi_conn_printk(KERN_ALERT, conn,
600                                   "iscsi_conn_start: FW oflload conn failed!!\n");
601         }
602
603 start_err:
604         return rval;
605 }
606
607 static void qedi_conn_destroy(struct iscsi_cls_conn *cls_conn)
608 {
609         struct iscsi_conn *conn = cls_conn->dd_data;
610         struct qedi_conn *qedi_conn = conn->dd_data;
611         struct Scsi_Host *shost;
612         struct qedi_ctx *qedi;
613
614         shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
615         qedi = iscsi_host_priv(shost);
616
617         qedi_conn_free_login_resources(qedi, qedi_conn);
618         iscsi_conn_teardown(cls_conn);
619 }
620
621 static int qedi_ep_get_param(struct iscsi_endpoint *ep,
622                              enum iscsi_param param, char *buf)
623 {
624         struct qedi_endpoint *qedi_ep = ep->dd_data;
625         int len;
626
627         if (!qedi_ep)
628                 return -ENOTCONN;
629
630         switch (param) {
631         case ISCSI_PARAM_CONN_PORT:
632                 len = sprintf(buf, "%hu\n", qedi_ep->dst_port);
633                 break;
634         case ISCSI_PARAM_CONN_ADDRESS:
635                 if (qedi_ep->ip_type == TCP_IPV4)
636                         len = sprintf(buf, "%pI4\n", qedi_ep->dst_addr);
637                 else
638                         len = sprintf(buf, "%pI6\n", qedi_ep->dst_addr);
639                 break;
640         default:
641                 return -ENOTCONN;
642         }
643
644         return len;
645 }
646
647 static int qedi_host_get_param(struct Scsi_Host *shost,
648                                enum iscsi_host_param param, char *buf)
649 {
650         struct qedi_ctx *qedi;
651         int len;
652
653         qedi = iscsi_host_priv(shost);
654
655         switch (param) {
656         case ISCSI_HOST_PARAM_HWADDRESS:
657                 len = sysfs_format_mac(buf, qedi->mac, 6);
658                 break;
659         case ISCSI_HOST_PARAM_NETDEV_NAME:
660                 len = sprintf(buf, "host%d\n", shost->host_no);
661                 break;
662         case ISCSI_HOST_PARAM_IPADDRESS:
663                 if (qedi->ip_type == TCP_IPV4)
664                         len = sprintf(buf, "%pI4\n", qedi->src_ip);
665                 else
666                         len = sprintf(buf, "%pI6\n", qedi->src_ip);
667                 break;
668         default:
669                 return iscsi_host_get_param(shost, param, buf);
670         }
671
672         return len;
673 }
674
675 static void qedi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
676                                 struct iscsi_stats *stats)
677 {
678         struct iscsi_conn *conn = cls_conn->dd_data;
679         struct qed_iscsi_stats iscsi_stats;
680         struct Scsi_Host *shost;
681         struct qedi_ctx *qedi;
682
683         shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
684         qedi = iscsi_host_priv(shost);
685         qedi_ops->get_stats(qedi->cdev, &iscsi_stats);
686
687         conn->txdata_octets = iscsi_stats.iscsi_tx_bytes_cnt;
688         conn->rxdata_octets = iscsi_stats.iscsi_rx_bytes_cnt;
689         conn->dataout_pdus_cnt = (uint32_t)iscsi_stats.iscsi_tx_data_pdu_cnt;
690         conn->datain_pdus_cnt = (uint32_t)iscsi_stats.iscsi_rx_data_pdu_cnt;
691         conn->r2t_pdus_cnt = (uint32_t)iscsi_stats.iscsi_rx_r2t_pdu_cnt;
692
693         stats->txdata_octets = conn->txdata_octets;
694         stats->rxdata_octets = conn->rxdata_octets;
695         stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
696         stats->dataout_pdus = conn->dataout_pdus_cnt;
697         stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
698         stats->datain_pdus = conn->datain_pdus_cnt;
699         stats->r2t_pdus = conn->r2t_pdus_cnt;
700         stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
701         stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
702         stats->digest_err = 0;
703         stats->timeout_err = 0;
704         strcpy(stats->custom[0].desc, "eh_abort_cnt");
705         stats->custom[0].value = conn->eh_abort_cnt;
706         stats->custom_length = 1;
707 }
708
709 static void qedi_iscsi_prep_generic_pdu_bd(struct qedi_conn *qedi_conn)
710 {
711         struct scsi_sge *bd_tbl;
712
713         bd_tbl = (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
714
715         bd_tbl->sge_addr.hi =
716                 (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
717         bd_tbl->sge_addr.lo = (u32)qedi_conn->gen_pdu.req_dma_addr;
718         bd_tbl->sge_len = qedi_conn->gen_pdu.req_wr_ptr -
719                                 qedi_conn->gen_pdu.req_buf;
720         bd_tbl = (struct scsi_sge  *)qedi_conn->gen_pdu.resp_bd_tbl;
721         bd_tbl->sge_addr.hi =
722                         (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
723         bd_tbl->sge_addr.lo = (u32)qedi_conn->gen_pdu.resp_dma_addr;
724         bd_tbl->sge_len = ISCSI_DEF_MAX_RECV_SEG_LEN;
725 }
726
727 static int qedi_iscsi_send_generic_request(struct iscsi_task *task)
728 {
729         struct qedi_cmd *cmd = task->dd_data;
730         struct qedi_conn *qedi_conn = cmd->conn;
731         char *buf;
732         int data_len;
733         int rc = 0;
734
735         qedi_iscsi_prep_generic_pdu_bd(qedi_conn);
736         switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
737         case ISCSI_OP_LOGIN:
738                 qedi_send_iscsi_login(qedi_conn, task);
739                 break;
740         case ISCSI_OP_NOOP_OUT:
741                 data_len = qedi_conn->gen_pdu.req_buf_size;
742                 buf = qedi_conn->gen_pdu.req_buf;
743                 if (data_len)
744                         rc = qedi_send_iscsi_nopout(qedi_conn, task,
745                                                     buf, data_len, 1);
746                 else
747                         rc = qedi_send_iscsi_nopout(qedi_conn, task,
748                                                     NULL, 0, 1);
749                 break;
750         case ISCSI_OP_LOGOUT:
751                 rc = qedi_send_iscsi_logout(qedi_conn, task);
752                 break;
753         case ISCSI_OP_SCSI_TMFUNC:
754                 rc = qedi_iscsi_abort_work(qedi_conn, task);
755                 break;
756         case ISCSI_OP_TEXT:
757                 rc = qedi_send_iscsi_text(qedi_conn, task);
758                 break;
759         default:
760                 iscsi_conn_printk(KERN_ALERT, qedi_conn->cls_conn->dd_data,
761                                   "unsupported op 0x%x\n", task->hdr->opcode);
762         }
763
764         return rc;
765 }
766
767 static int qedi_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
768 {
769         struct qedi_conn *qedi_conn = conn->dd_data;
770         struct qedi_cmd *cmd = task->dd_data;
771
772         memset(qedi_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
773
774         qedi_conn->gen_pdu.req_buf_size = task->data_count;
775
776         if (task->data_count) {
777                 memcpy(qedi_conn->gen_pdu.req_buf, task->data,
778                        task->data_count);
779                 qedi_conn->gen_pdu.req_wr_ptr =
780                         qedi_conn->gen_pdu.req_buf + task->data_count;
781         }
782
783         cmd->conn = conn->dd_data;
784         cmd->scsi_cmd = NULL;
785         return qedi_iscsi_send_generic_request(task);
786 }
787
788 static int qedi_task_xmit(struct iscsi_task *task)
789 {
790         struct iscsi_conn *conn = task->conn;
791         struct qedi_conn *qedi_conn = conn->dd_data;
792         struct qedi_cmd *cmd = task->dd_data;
793         struct scsi_cmnd *sc = task->sc;
794
795         cmd->state = 0;
796         cmd->task = NULL;
797         cmd->use_slowpath = false;
798         cmd->conn = qedi_conn;
799         cmd->task = task;
800         cmd->io_cmd_in_list = false;
801         INIT_LIST_HEAD(&cmd->io_cmd);
802
803         if (!sc)
804                 return qedi_mtask_xmit(conn, task);
805
806         cmd->scsi_cmd = sc;
807         return qedi_iscsi_send_ioreq(task);
808 }
809
810 static struct iscsi_endpoint *
811 qedi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
812                 int non_blocking)
813 {
814         struct qedi_ctx *qedi;
815         struct iscsi_endpoint *ep;
816         struct qedi_endpoint *qedi_ep;
817         struct sockaddr_in *addr;
818         struct sockaddr_in6 *addr6;
819         struct qed_dev *cdev  =  NULL;
820         struct qedi_uio_dev *udev = NULL;
821         struct iscsi_path path_req;
822         u32 msg_type = ISCSI_KEVENT_IF_DOWN;
823         u32 iscsi_cid = QEDI_CID_RESERVED;
824         u16 len = 0;
825         char *buf = NULL;
826         int ret;
827
828         if (!shost) {
829                 ret = -ENXIO;
830                 QEDI_ERR(NULL, "shost is NULL\n");
831                 return ERR_PTR(ret);
832         }
833
834         if (qedi_do_not_recover) {
835                 ret = -ENOMEM;
836                 return ERR_PTR(ret);
837         }
838
839         qedi = iscsi_host_priv(shost);
840         cdev = qedi->cdev;
841         udev = qedi->udev;
842
843         if (test_bit(QEDI_IN_OFFLINE, &qedi->flags) ||
844             test_bit(QEDI_IN_RECOVERY, &qedi->flags)) {
845                 ret = -ENOMEM;
846                 return ERR_PTR(ret);
847         }
848
849         ep = iscsi_create_endpoint(sizeof(struct qedi_endpoint));
850         if (!ep) {
851                 QEDI_ERR(&qedi->dbg_ctx, "endpoint create fail\n");
852                 ret = -ENOMEM;
853                 return ERR_PTR(ret);
854         }
855         qedi_ep = ep->dd_data;
856         memset(qedi_ep, 0, sizeof(struct qedi_endpoint));
857         qedi_ep->state = EP_STATE_IDLE;
858         qedi_ep->iscsi_cid = (u32)-1;
859         qedi_ep->qedi = qedi;
860
861         if (dst_addr->sa_family == AF_INET) {
862                 addr = (struct sockaddr_in *)dst_addr;
863                 memcpy(qedi_ep->dst_addr, &addr->sin_addr.s_addr,
864                        sizeof(struct in_addr));
865                 qedi_ep->dst_port = ntohs(addr->sin_port);
866                 qedi_ep->ip_type = TCP_IPV4;
867                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
868                           "dst_addr=%pI4, dst_port=%u\n",
869                           qedi_ep->dst_addr, qedi_ep->dst_port);
870         } else if (dst_addr->sa_family == AF_INET6) {
871                 addr6 = (struct sockaddr_in6 *)dst_addr;
872                 memcpy(qedi_ep->dst_addr, &addr6->sin6_addr,
873                        sizeof(struct in6_addr));
874                 qedi_ep->dst_port = ntohs(addr6->sin6_port);
875                 qedi_ep->ip_type = TCP_IPV6;
876                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
877                           "dst_addr=%pI6, dst_port=%u\n",
878                           qedi_ep->dst_addr, qedi_ep->dst_port);
879         } else {
880                 QEDI_ERR(&qedi->dbg_ctx, "Invalid endpoint\n");
881         }
882
883         if (atomic_read(&qedi->link_state) != QEDI_LINK_UP) {
884                 QEDI_WARN(&qedi->dbg_ctx, "qedi link down\n");
885                 ret = -ENXIO;
886                 goto ep_conn_exit;
887         }
888
889         ret = qedi_alloc_sq(qedi, qedi_ep);
890         if (ret)
891                 goto ep_conn_exit;
892
893         ret = qedi_ops->acquire_conn(qedi->cdev, &qedi_ep->handle,
894                                      &qedi_ep->fw_cid, &qedi_ep->p_doorbell);
895
896         if (ret) {
897                 QEDI_ERR(&qedi->dbg_ctx, "Could not acquire connection\n");
898                 ret = -ENXIO;
899                 goto ep_free_sq;
900         }
901
902         iscsi_cid = qedi_ep->handle;
903         qedi_ep->iscsi_cid = iscsi_cid;
904
905         init_waitqueue_head(&qedi_ep->ofld_wait);
906         init_waitqueue_head(&qedi_ep->tcp_ofld_wait);
907         qedi_ep->state = EP_STATE_OFLDCONN_START;
908         qedi->ep_tbl[iscsi_cid] = qedi_ep;
909
910         buf = (char *)&path_req;
911         len = sizeof(path_req);
912         memset(&path_req, 0, len);
913
914         msg_type = ISCSI_KEVENT_PATH_REQ;
915         path_req.handle = (u64)qedi_ep->iscsi_cid;
916         path_req.pmtu = qedi->ll2_mtu;
917         qedi_ep->pmtu = qedi->ll2_mtu;
918         if (qedi_ep->ip_type == TCP_IPV4) {
919                 memcpy(&path_req.dst.v4_addr, &qedi_ep->dst_addr,
920                        sizeof(struct in_addr));
921                 path_req.ip_addr_len = 4;
922         } else {
923                 memcpy(&path_req.dst.v6_addr, &qedi_ep->dst_addr,
924                        sizeof(struct in6_addr));
925                 path_req.ip_addr_len = 16;
926         }
927
928         ret = iscsi_offload_mesg(shost, &qedi_iscsi_transport, msg_type, buf,
929                                  len);
930         if (ret) {
931                 QEDI_ERR(&qedi->dbg_ctx,
932                          "iscsi_offload_mesg() failed for cid=0x%x ret=%d\n",
933                          iscsi_cid, ret);
934                 goto ep_rel_conn;
935         }
936
937         atomic_inc(&qedi->num_offloads);
938         return ep;
939
940 ep_rel_conn:
941         qedi->ep_tbl[iscsi_cid] = NULL;
942         ret = qedi_ops->release_conn(qedi->cdev, qedi_ep->handle);
943         if (ret)
944                 QEDI_WARN(&qedi->dbg_ctx, "release_conn returned %d\n",
945                           ret);
946 ep_free_sq:
947         qedi_free_sq(qedi, qedi_ep);
948 ep_conn_exit:
949         iscsi_destroy_endpoint(ep);
950         return ERR_PTR(ret);
951 }
952
953 static int qedi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
954 {
955         struct qedi_endpoint *qedi_ep;
956         int ret = 0;
957
958         if (qedi_do_not_recover)
959                 return 1;
960
961         qedi_ep = ep->dd_data;
962         if (qedi_ep->state == EP_STATE_IDLE ||
963             qedi_ep->state == EP_STATE_OFLDCONN_FAILED)
964                 return -1;
965
966         if (qedi_ep->state == EP_STATE_OFLDCONN_COMPL)
967                 ret = 1;
968
969         ret = wait_event_interruptible_timeout(qedi_ep->ofld_wait,
970                                                QEDI_OFLD_WAIT_STATE(qedi_ep),
971                                                msecs_to_jiffies(timeout_ms));
972
973         if (qedi_ep->state == EP_STATE_OFLDCONN_FAILED)
974                 ret = -1;
975
976         if (ret > 0)
977                 return 1;
978         else if (!ret)
979                 return 0;
980         else
981                 return ret;
982 }
983
984 static void qedi_cleanup_active_cmd_list(struct qedi_conn *qedi_conn)
985 {
986         struct qedi_cmd *cmd, *cmd_tmp;
987
988         list_for_each_entry_safe(cmd, cmd_tmp, &qedi_conn->active_cmd_list,
989                                  io_cmd) {
990                 list_del_init(&cmd->io_cmd);
991                 qedi_conn->active_cmd_count--;
992         }
993 }
994
995 static void qedi_ep_disconnect(struct iscsi_endpoint *ep)
996 {
997         struct qedi_endpoint *qedi_ep;
998         struct qedi_conn *qedi_conn = NULL;
999         struct iscsi_conn *conn = NULL;
1000         struct qedi_ctx *qedi;
1001         int ret = 0;
1002         int wait_delay = 20 * HZ;
1003         int abrt_conn = 0;
1004         int count = 10;
1005
1006         qedi_ep = ep->dd_data;
1007         qedi = qedi_ep->qedi;
1008
1009         flush_work(&qedi_ep->offload_work);
1010
1011         if (qedi_ep->conn) {
1012                 qedi_conn = qedi_ep->conn;
1013                 conn = qedi_conn->cls_conn->dd_data;
1014                 iscsi_suspend_queue(conn);
1015                 abrt_conn = qedi_conn->abrt_conn;
1016
1017                 while (count--) {
1018                         if (!test_bit(QEDI_CONN_FW_CLEANUP,
1019                                       &qedi_conn->flags)) {
1020                                 break;
1021                         }
1022                         msleep(1000);
1023                 }
1024
1025                 if (test_bit(QEDI_IN_RECOVERY, &qedi->flags)) {
1026                         if (qedi_do_not_recover) {
1027                                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1028                                           "Do not recover cid=0x%x\n",
1029                                           qedi_ep->iscsi_cid);
1030                                 goto ep_exit_recover;
1031                         }
1032                         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1033                                   "Reset recovery cid=0x%x, qedi_ep=%p, state=0x%x\n",
1034                                   qedi_ep->iscsi_cid, qedi_ep, qedi_ep->state);
1035                         qedi_cleanup_active_cmd_list(qedi_conn);
1036                         goto ep_release_conn;
1037                 }
1038         }
1039
1040         if (qedi_do_not_recover)
1041                 goto ep_exit_recover;
1042
1043         switch (qedi_ep->state) {
1044         case EP_STATE_OFLDCONN_START:
1045                 goto ep_release_conn;
1046         case EP_STATE_OFLDCONN_FAILED:
1047                         break;
1048         case EP_STATE_OFLDCONN_COMPL:
1049                 if (unlikely(!qedi_conn))
1050                         break;
1051
1052                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1053                           "Active cmd count=%d, abrt_conn=%d, ep state=0x%x, cid=0x%x, qedi_conn=%p\n",
1054                           qedi_conn->active_cmd_count, abrt_conn,
1055                           qedi_ep->state,
1056                           qedi_ep->iscsi_cid,
1057                           qedi_ep->conn
1058                           );
1059
1060                 if (!qedi_conn->active_cmd_count)
1061                         abrt_conn = 0;
1062                 else
1063                         abrt_conn = 1;
1064
1065                 if (abrt_conn)
1066                         qedi_clearsq(qedi, qedi_conn, NULL);
1067                 break;
1068         default:
1069                 break;
1070         }
1071
1072         qedi_ep->state = EP_STATE_DISCONN_START;
1073         ret = qedi_ops->destroy_conn(qedi->cdev, qedi_ep->handle, abrt_conn);
1074         if (ret) {
1075                 QEDI_WARN(&qedi->dbg_ctx,
1076                           "destroy_conn failed returned %d\n", ret);
1077         } else {
1078                 ret = wait_event_interruptible_timeout(
1079                                         qedi_ep->tcp_ofld_wait,
1080                                         (qedi_ep->state !=
1081                                          EP_STATE_DISCONN_START),
1082                                         wait_delay);
1083                 if ((ret <= 0) || (qedi_ep->state == EP_STATE_DISCONN_START)) {
1084                         QEDI_WARN(&qedi->dbg_ctx,
1085                                   "Destroy conn timedout or interrupted, ret=%d, delay=%d, cid=0x%x\n",
1086                                   ret, wait_delay, qedi_ep->iscsi_cid);
1087                 }
1088         }
1089
1090 ep_release_conn:
1091         ret = qedi_ops->release_conn(qedi->cdev, qedi_ep->handle);
1092         if (ret)
1093                 QEDI_WARN(&qedi->dbg_ctx,
1094                           "release_conn returned %d, cid=0x%x\n",
1095                           ret, qedi_ep->iscsi_cid);
1096 ep_exit_recover:
1097         qedi_ep->state = EP_STATE_IDLE;
1098         qedi->ep_tbl[qedi_ep->iscsi_cid] = NULL;
1099         qedi->cid_que.conn_cid_tbl[qedi_ep->iscsi_cid] = NULL;
1100         qedi_free_id(&qedi->lcl_port_tbl, qedi_ep->src_port);
1101         qedi_free_sq(qedi, qedi_ep);
1102
1103         if (qedi_conn)
1104                 qedi_conn->ep = NULL;
1105
1106         qedi_ep->conn = NULL;
1107         qedi_ep->qedi = NULL;
1108         atomic_dec(&qedi->num_offloads);
1109
1110         iscsi_destroy_endpoint(ep);
1111 }
1112
1113 static int qedi_data_avail(struct qedi_ctx *qedi, u16 vlanid)
1114 {
1115         struct qed_dev *cdev = qedi->cdev;
1116         struct qedi_uio_dev *udev;
1117         struct qedi_uio_ctrl *uctrl;
1118         struct sk_buff *skb;
1119         u32 len;
1120         int rc = 0;
1121
1122         udev = qedi->udev;
1123         if (!udev) {
1124                 QEDI_ERR(&qedi->dbg_ctx, "udev is NULL.\n");
1125                 return -EINVAL;
1126         }
1127
1128         uctrl = (struct qedi_uio_ctrl *)udev->uctrl;
1129         if (!uctrl) {
1130                 QEDI_ERR(&qedi->dbg_ctx, "uctlr is NULL.\n");
1131                 return -EINVAL;
1132         }
1133
1134         len = uctrl->host_tx_pkt_len;
1135         if (!len) {
1136                 QEDI_ERR(&qedi->dbg_ctx, "Invalid len %u\n", len);
1137                 return -EINVAL;
1138         }
1139
1140         skb = alloc_skb(len, GFP_ATOMIC);
1141         if (!skb) {
1142                 QEDI_ERR(&qedi->dbg_ctx, "alloc_skb failed\n");
1143                 return -EINVAL;
1144         }
1145
1146         skb_put(skb, len);
1147         memcpy(skb->data, udev->tx_pkt, len);
1148         skb->ip_summed = CHECKSUM_NONE;
1149
1150         if (vlanid)
1151                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
1152
1153         rc = qedi_ops->ll2->start_xmit(cdev, skb);
1154         if (rc) {
1155                 QEDI_ERR(&qedi->dbg_ctx, "ll2 start_xmit returned %d\n",
1156                          rc);
1157                 kfree_skb(skb);
1158         }
1159
1160         uctrl->host_tx_pkt_len = 0;
1161         uctrl->hw_tx_cons++;
1162
1163         return rc;
1164 }
1165
1166 static void qedi_offload_work(struct work_struct *work)
1167 {
1168         struct qedi_endpoint *qedi_ep =
1169                 container_of(work, struct qedi_endpoint, offload_work);
1170         struct qedi_ctx *qedi;
1171         int wait_delay = 20 * HZ;
1172         int ret;
1173
1174         qedi = qedi_ep->qedi;
1175
1176         ret = qedi_iscsi_offload_conn(qedi_ep);
1177         if (ret) {
1178                 QEDI_ERR(&qedi->dbg_ctx,
1179                          "offload error: iscsi_cid=%u, qedi_ep=%p, ret=%d\n",
1180                          qedi_ep->iscsi_cid, qedi_ep, ret);
1181                 qedi_ep->state = EP_STATE_OFLDCONN_FAILED;
1182                 return;
1183         }
1184
1185         ret = wait_event_interruptible_timeout(qedi_ep->tcp_ofld_wait,
1186                                                (qedi_ep->state ==
1187                                                EP_STATE_OFLDCONN_COMPL),
1188                                                wait_delay);
1189         if ((ret <= 0) || (qedi_ep->state != EP_STATE_OFLDCONN_COMPL)) {
1190                 qedi_ep->state = EP_STATE_OFLDCONN_FAILED;
1191                 QEDI_ERR(&qedi->dbg_ctx,
1192                          "Offload conn TIMEOUT iscsi_cid=%u, qedi_ep=%p\n",
1193                          qedi_ep->iscsi_cid, qedi_ep);
1194         }
1195 }
1196
1197 static int qedi_set_path(struct Scsi_Host *shost, struct iscsi_path *path_data)
1198 {
1199         struct qedi_ctx *qedi;
1200         struct qedi_endpoint *qedi_ep;
1201         int ret = 0;
1202         u32 iscsi_cid;
1203         u16 port_id = 0;
1204
1205         if (!shost) {
1206                 ret = -ENXIO;
1207                 QEDI_ERR(NULL, "shost is NULL\n");
1208                 return ret;
1209         }
1210
1211         if (strcmp(shost->hostt->proc_name, "qedi")) {
1212                 ret = -ENXIO;
1213                 QEDI_ERR(NULL, "shost %s is invalid\n",
1214                          shost->hostt->proc_name);
1215                 return ret;
1216         }
1217
1218         qedi = iscsi_host_priv(shost);
1219         if (path_data->handle == QEDI_PATH_HANDLE) {
1220                 ret = qedi_data_avail(qedi, path_data->vlan_id);
1221                 goto set_path_exit;
1222         }
1223
1224         iscsi_cid = (u32)path_data->handle;
1225         qedi_ep = qedi->ep_tbl[iscsi_cid];
1226         QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1227                   "iscsi_cid=0x%x, qedi_ep=%p\n", iscsi_cid, qedi_ep);
1228
1229         if (!is_valid_ether_addr(&path_data->mac_addr[0])) {
1230                 QEDI_NOTICE(&qedi->dbg_ctx, "dst mac NOT VALID\n");
1231                 ret = -EIO;
1232                 goto set_path_exit;
1233         }
1234
1235         ether_addr_copy(&qedi_ep->src_mac[0], &qedi->mac[0]);
1236         ether_addr_copy(&qedi_ep->dst_mac[0], &path_data->mac_addr[0]);
1237
1238         qedi_ep->vlan_id = path_data->vlan_id;
1239         if (path_data->pmtu < DEF_PATH_MTU) {
1240                 qedi_ep->pmtu = qedi->ll2_mtu;
1241                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1242                           "MTU cannot be %u, using default MTU %u\n",
1243                            path_data->pmtu, qedi_ep->pmtu);
1244         }
1245
1246         if (path_data->pmtu != qedi->ll2_mtu) {
1247                 if (path_data->pmtu > JUMBO_MTU) {
1248                         ret = -EINVAL;
1249                         QEDI_ERR(NULL, "Invalid MTU %u\n", path_data->pmtu);
1250                         goto set_path_exit;
1251                 }
1252
1253                 qedi_reset_host_mtu(qedi, path_data->pmtu);
1254                 qedi_ep->pmtu = qedi->ll2_mtu;
1255         }
1256
1257         port_id = qedi_ep->src_port;
1258         if (port_id >= QEDI_LOCAL_PORT_MIN &&
1259             port_id < QEDI_LOCAL_PORT_MAX) {
1260                 if (qedi_alloc_id(&qedi->lcl_port_tbl, port_id))
1261                         port_id = 0;
1262         } else {
1263                 port_id = 0;
1264         }
1265
1266         if (!port_id) {
1267                 port_id = qedi_alloc_new_id(&qedi->lcl_port_tbl);
1268                 if (port_id == QEDI_LOCAL_PORT_INVALID) {
1269                         QEDI_ERR(&qedi->dbg_ctx,
1270                                  "Failed to allocate port id for iscsi_cid=0x%x\n",
1271                                  iscsi_cid);
1272                         ret = -ENOMEM;
1273                         goto set_path_exit;
1274                 }
1275         }
1276
1277         qedi_ep->src_port = port_id;
1278
1279         if (qedi_ep->ip_type == TCP_IPV4) {
1280                 memcpy(&qedi_ep->src_addr[0], &path_data->src.v4_addr,
1281                        sizeof(struct in_addr));
1282                 memcpy(&qedi->src_ip[0], &path_data->src.v4_addr,
1283                        sizeof(struct in_addr));
1284                 qedi->ip_type = TCP_IPV4;
1285
1286                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1287                           "src addr:port=%pI4:%u, dst addr:port=%pI4:%u\n",
1288                           qedi_ep->src_addr, qedi_ep->src_port,
1289                           qedi_ep->dst_addr, qedi_ep->dst_port);
1290         } else {
1291                 memcpy(&qedi_ep->src_addr[0], &path_data->src.v6_addr,
1292                        sizeof(struct in6_addr));
1293                 memcpy(&qedi->src_ip[0], &path_data->src.v6_addr,
1294                        sizeof(struct in6_addr));
1295                 qedi->ip_type = TCP_IPV6;
1296
1297                 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1298                           "src addr:port=%pI6:%u, dst addr:port=%pI6:%u\n",
1299                           qedi_ep->src_addr, qedi_ep->src_port,
1300                           qedi_ep->dst_addr, qedi_ep->dst_port);
1301         }
1302
1303         INIT_WORK(&qedi_ep->offload_work, qedi_offload_work);
1304         queue_work(qedi->offload_thread, &qedi_ep->offload_work);
1305
1306         ret = 0;
1307
1308 set_path_exit:
1309         return ret;
1310 }
1311
1312 static umode_t qedi_attr_is_visible(int param_type, int param)
1313 {
1314         switch (param_type) {
1315         case ISCSI_HOST_PARAM:
1316                 switch (param) {
1317                 case ISCSI_HOST_PARAM_NETDEV_NAME:
1318                 case ISCSI_HOST_PARAM_HWADDRESS:
1319                 case ISCSI_HOST_PARAM_IPADDRESS:
1320                         return 0444;
1321                 default:
1322                         return 0;
1323                 }
1324         case ISCSI_PARAM:
1325                 switch (param) {
1326                 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1327                 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1328                 case ISCSI_PARAM_HDRDGST_EN:
1329                 case ISCSI_PARAM_DATADGST_EN:
1330                 case ISCSI_PARAM_CONN_ADDRESS:
1331                 case ISCSI_PARAM_CONN_PORT:
1332                 case ISCSI_PARAM_EXP_STATSN:
1333                 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1334                 case ISCSI_PARAM_PERSISTENT_PORT:
1335                 case ISCSI_PARAM_PING_TMO:
1336                 case ISCSI_PARAM_RECV_TMO:
1337                 case ISCSI_PARAM_INITIAL_R2T_EN:
1338                 case ISCSI_PARAM_MAX_R2T:
1339                 case ISCSI_PARAM_IMM_DATA_EN:
1340                 case ISCSI_PARAM_FIRST_BURST:
1341                 case ISCSI_PARAM_MAX_BURST:
1342                 case ISCSI_PARAM_PDU_INORDER_EN:
1343                 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1344                 case ISCSI_PARAM_ERL:
1345                 case ISCSI_PARAM_TARGET_NAME:
1346                 case ISCSI_PARAM_TPGT:
1347                 case ISCSI_PARAM_USERNAME:
1348                 case ISCSI_PARAM_PASSWORD:
1349                 case ISCSI_PARAM_USERNAME_IN:
1350                 case ISCSI_PARAM_PASSWORD_IN:
1351                 case ISCSI_PARAM_FAST_ABORT:
1352                 case ISCSI_PARAM_ABORT_TMO:
1353                 case ISCSI_PARAM_LU_RESET_TMO:
1354                 case ISCSI_PARAM_TGT_RESET_TMO:
1355                 case ISCSI_PARAM_IFACE_NAME:
1356                 case ISCSI_PARAM_INITIATOR_NAME:
1357                 case ISCSI_PARAM_BOOT_ROOT:
1358                 case ISCSI_PARAM_BOOT_NIC:
1359                 case ISCSI_PARAM_BOOT_TARGET:
1360                         return 0444;
1361                 default:
1362                         return 0;
1363                 }
1364         }
1365
1366         return 0;
1367 }
1368
1369 static void qedi_cleanup_task(struct iscsi_task *task)
1370 {
1371         if (!task->sc || task->state == ISCSI_TASK_PENDING) {
1372                 QEDI_INFO(NULL, QEDI_LOG_IO, "Returning ref_cnt=%d\n",
1373                           refcount_read(&task->refcount));
1374                 return;
1375         }
1376
1377         qedi_iscsi_unmap_sg_list(task->dd_data);
1378 }
1379
1380 struct iscsi_transport qedi_iscsi_transport = {
1381         .owner = THIS_MODULE,
1382         .name = QEDI_MODULE_NAME,
1383         .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_MULTI_R2T | CAP_DATADGST |
1384                 CAP_DATA_PATH_OFFLOAD | CAP_TEXT_NEGO,
1385         .create_session = qedi_session_create,
1386         .destroy_session = qedi_session_destroy,
1387         .create_conn = qedi_conn_create,
1388         .bind_conn = qedi_conn_bind,
1389         .start_conn = qedi_conn_start,
1390         .stop_conn = iscsi_conn_stop,
1391         .destroy_conn = qedi_conn_destroy,
1392         .set_param = iscsi_set_param,
1393         .get_ep_param = qedi_ep_get_param,
1394         .get_conn_param = iscsi_conn_get_param,
1395         .get_session_param = iscsi_session_get_param,
1396         .get_host_param = qedi_host_get_param,
1397         .send_pdu = iscsi_conn_send_pdu,
1398         .get_stats = qedi_conn_get_stats,
1399         .xmit_task = qedi_task_xmit,
1400         .cleanup_task = qedi_cleanup_task,
1401         .session_recovery_timedout = iscsi_session_recovery_timedout,
1402         .ep_connect = qedi_ep_connect,
1403         .ep_poll = qedi_ep_poll,
1404         .ep_disconnect = qedi_ep_disconnect,
1405         .set_path = qedi_set_path,
1406         .attr_is_visible = qedi_attr_is_visible,
1407 };
1408
1409 void qedi_start_conn_recovery(struct qedi_ctx *qedi,
1410                               struct qedi_conn *qedi_conn)
1411 {
1412         struct iscsi_cls_session *cls_sess;
1413         struct iscsi_cls_conn *cls_conn;
1414         struct iscsi_conn *conn;
1415
1416         cls_conn = qedi_conn->cls_conn;
1417         conn = cls_conn->dd_data;
1418         cls_sess = iscsi_conn_to_session(cls_conn);
1419
1420         if (iscsi_is_session_online(cls_sess)) {
1421                 qedi_conn->abrt_conn = 1;
1422                 QEDI_ERR(&qedi->dbg_ctx,
1423                          "Failing connection, state=0x%x, cid=0x%x\n",
1424                          conn->session->state, qedi_conn->iscsi_conn_id);
1425                 iscsi_conn_failure(qedi_conn->cls_conn->dd_data,
1426                                    ISCSI_ERR_CONN_FAILED);
1427         }
1428 }
1429
1430 static const struct {
1431         enum iscsi_error_types error_code;
1432         char *err_string;
1433 } qedi_iscsi_error[] = {
1434         { ISCSI_STATUS_NONE,
1435           "tcp_error none"
1436         },
1437         { ISCSI_CONN_ERROR_TASK_CID_MISMATCH,
1438           "task cid mismatch"
1439         },
1440         { ISCSI_CONN_ERROR_TASK_NOT_VALID,
1441           "invalid task"
1442         },
1443         { ISCSI_CONN_ERROR_RQ_RING_IS_FULL,
1444           "rq ring full"
1445         },
1446         { ISCSI_CONN_ERROR_CMDQ_RING_IS_FULL,
1447           "cmdq ring full"
1448         },
1449         { ISCSI_CONN_ERROR_HQE_CACHING_FAILED,
1450           "sge caching failed"
1451         },
1452         { ISCSI_CONN_ERROR_HEADER_DIGEST_ERROR,
1453           "hdr digest error"
1454         },
1455         { ISCSI_CONN_ERROR_LOCAL_COMPLETION_ERROR,
1456           "local cmpl error"
1457         },
1458         { ISCSI_CONN_ERROR_DATA_OVERRUN,
1459           "invalid task"
1460         },
1461         { ISCSI_CONN_ERROR_OUT_OF_SGES_ERROR,
1462           "out of sge error"
1463         },
1464         { ISCSI_CONN_ERROR_TCP_SEG_PROC_IP_OPTIONS_ERROR,
1465           "tcp seg ip options error"
1466         },
1467         { ISCSI_CONN_ERROR_TCP_IP_FRAGMENT_ERROR,
1468           "tcp ip fragment error"
1469         },
1470         { ISCSI_CONN_ERROR_PROTOCOL_ERR_AHS_LEN,
1471           "AHS len protocol error"
1472         },
1473         { ISCSI_CONN_ERROR_PROTOCOL_ERR_ITT_OUT_OF_RANGE,
1474           "itt out of range error"
1475         },
1476         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SEG_LEN_EXCEEDS_PDU_SIZE,
1477           "data seg more than pdu size"
1478         },
1479         { ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_OPCODE,
1480           "invalid opcode"
1481         },
1482         { ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_OPCODE_BEFORE_UPDATE,
1483           "invalid opcode before update"
1484         },
1485         { ISCSI_CONN_ERROR_UNVALID_NOPIN_DSL,
1486           "unexpected opcode"
1487         },
1488         { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_CARRIES_NO_DATA,
1489           "r2t carries no data"
1490         },
1491         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SN,
1492           "data sn error"
1493         },
1494         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_IN_TTT,
1495           "data TTT error"
1496         },
1497         { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_TTT,
1498           "r2t TTT error"
1499         },
1500         { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_BUFFER_OFFSET,
1501           "buffer offset error"
1502         },
1503         { ISCSI_CONN_ERROR_PROTOCOL_ERR_BUFFER_OFFSET_OOO,
1504           "buffer offset ooo"
1505         },
1506         { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_SN,
1507           "data seg len 0"
1508         },
1509         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_0,
1510           "data xer len error"
1511         },
1512         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_1,
1513           "data xer len1 error"
1514         },
1515         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_2,
1516           "data xer len2 error"
1517         },
1518         { ISCSI_CONN_ERROR_PROTOCOL_ERR_LUN,
1519           "protocol lun error"
1520         },
1521         { ISCSI_CONN_ERROR_PROTOCOL_ERR_F_BIT_ZERO,
1522           "f bit zero error"
1523         },
1524         { ISCSI_CONN_ERROR_PROTOCOL_ERR_EXP_STAT_SN,
1525           "exp stat sn error"
1526         },
1527         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DSL_NOT_ZERO,
1528           "dsl not zero error"
1529         },
1530         { ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_DSL,
1531           "invalid dsl"
1532         },
1533         { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SEG_LEN_TOO_BIG,
1534           "data seg len too big"
1535         },
1536         { ISCSI_CONN_ERROR_PROTOCOL_ERR_OUTSTANDING_R2T_COUNT,
1537           "outstanding r2t count error"
1538         },
1539         { ISCSI_CONN_ERROR_SENSE_DATA_LENGTH,
1540           "sense datalen error"
1541         },
1542 };
1543
1544 char *qedi_get_iscsi_error(enum iscsi_error_types err_code)
1545 {
1546         int i;
1547         char *msg = NULL;
1548
1549         for (i = 0; i < ARRAY_SIZE(qedi_iscsi_error); i++) {
1550                 if (qedi_iscsi_error[i].error_code == err_code) {
1551                         msg = qedi_iscsi_error[i].err_string;
1552                         break;
1553                 }
1554         }
1555         return msg;
1556 }
1557
1558 void qedi_process_iscsi_error(struct qedi_endpoint *ep, struct async_data *data)
1559 {
1560         struct qedi_conn *qedi_conn;
1561         struct qedi_ctx *qedi;
1562         char warn_notice[] = "iscsi_warning";
1563         char error_notice[] = "iscsi_error";
1564         char unknown_msg[] = "Unknown error";
1565         char *message;
1566         int need_recovery = 0;
1567         u32 err_mask = 0;
1568         char *msg;
1569
1570         if (!ep)
1571                 return;
1572
1573         qedi_conn = ep->conn;
1574         if (!qedi_conn)
1575                 return;
1576
1577         qedi = ep->qedi;
1578
1579         QEDI_ERR(&qedi->dbg_ctx, "async event iscsi error:0x%x\n",
1580                  data->error_code);
1581
1582         if (err_mask) {
1583                 need_recovery = 0;
1584                 message = warn_notice;
1585         } else {
1586                 need_recovery = 1;
1587                 message = error_notice;
1588         }
1589
1590         msg = qedi_get_iscsi_error(data->error_code);
1591         if (!msg) {
1592                 need_recovery = 0;
1593                 msg = unknown_msg;
1594         }
1595
1596         iscsi_conn_printk(KERN_ALERT,
1597                           qedi_conn->cls_conn->dd_data,
1598                           "qedi: %s - %s\n", message, msg);
1599
1600         if (need_recovery)
1601                 qedi_start_conn_recovery(qedi_conn->qedi, qedi_conn);
1602 }
1603
1604 void qedi_process_tcp_error(struct qedi_endpoint *ep, struct async_data *data)
1605 {
1606         struct qedi_conn *qedi_conn;
1607
1608         if (!ep)
1609                 return;
1610
1611         qedi_conn = ep->conn;
1612         if (!qedi_conn)
1613                 return;
1614
1615         QEDI_ERR(&ep->qedi->dbg_ctx, "async event TCP error:0x%x\n",
1616                  data->error_code);
1617
1618         qedi_start_conn_recovery(qedi_conn->qedi, qedi_conn);
1619 }