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