17996a778SMike Christie /* 27996a778SMike Christie * iSCSI lib functions 37996a778SMike Christie * 47996a778SMike Christie * Copyright (C) 2006 Red Hat, Inc. All rights reserved. 57996a778SMike Christie * Copyright (C) 2004 - 2006 Mike Christie 67996a778SMike Christie * Copyright (C) 2004 - 2005 Dmitry Yusupov 77996a778SMike Christie * Copyright (C) 2004 - 2005 Alex Aizman 87996a778SMike Christie * maintained by open-iscsi@googlegroups.com 97996a778SMike Christie * 107996a778SMike Christie * This program is free software; you can redistribute it and/or modify 117996a778SMike Christie * it under the terms of the GNU General Public License as published by 127996a778SMike Christie * the Free Software Foundation; either version 2 of the License, or 137996a778SMike Christie * (at your option) any later version. 147996a778SMike Christie * 157996a778SMike Christie * This program is distributed in the hope that it will be useful, 167996a778SMike Christie * but WITHOUT ANY WARRANTY; without even the implied warranty of 177996a778SMike Christie * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 187996a778SMike Christie * GNU General Public License for more details. 197996a778SMike Christie * 207996a778SMike Christie * You should have received a copy of the GNU General Public License 217996a778SMike Christie * along with this program; if not, write to the Free Software 227996a778SMike Christie * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 237996a778SMike Christie */ 247996a778SMike Christie #include <linux/types.h> 257996a778SMike Christie #include <linux/mutex.h> 267996a778SMike Christie #include <linux/kfifo.h> 277996a778SMike Christie #include <linux/delay.h> 287996a778SMike Christie #include <net/tcp.h> 297996a778SMike Christie #include <scsi/scsi_cmnd.h> 307996a778SMike Christie #include <scsi/scsi_device.h> 317996a778SMike Christie #include <scsi/scsi_eh.h> 327996a778SMike Christie #include <scsi/scsi_tcq.h> 337996a778SMike Christie #include <scsi/scsi_host.h> 347996a778SMike Christie #include <scsi/scsi.h> 357996a778SMike Christie #include <scsi/iscsi_proto.h> 367996a778SMike Christie #include <scsi/scsi_transport.h> 377996a778SMike Christie #include <scsi/scsi_transport_iscsi.h> 387996a778SMike Christie #include <scsi/libiscsi.h> 397996a778SMike Christie 407996a778SMike Christie struct iscsi_session * 417996a778SMike Christie class_to_transport_session(struct iscsi_cls_session *cls_session) 427996a778SMike Christie { 437996a778SMike Christie struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); 447996a778SMike Christie return iscsi_hostdata(shost->hostdata); 457996a778SMike Christie } 467996a778SMike Christie EXPORT_SYMBOL_GPL(class_to_transport_session); 477996a778SMike Christie 487996a778SMike Christie #define INVALID_SN_DELTA 0xffff 497996a778SMike Christie 507996a778SMike Christie int 517996a778SMike Christie iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) 527996a778SMike Christie { 537996a778SMike Christie uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn); 547996a778SMike Christie uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn); 557996a778SMike Christie 567996a778SMike Christie if (max_cmdsn < exp_cmdsn -1 && 577996a778SMike Christie max_cmdsn > exp_cmdsn - INVALID_SN_DELTA) 587996a778SMike Christie return ISCSI_ERR_MAX_CMDSN; 597996a778SMike Christie if (max_cmdsn > session->max_cmdsn || 607996a778SMike Christie max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA) 617996a778SMike Christie session->max_cmdsn = max_cmdsn; 627996a778SMike Christie if (exp_cmdsn > session->exp_cmdsn || 637996a778SMike Christie exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA) 647996a778SMike Christie session->exp_cmdsn = exp_cmdsn; 657996a778SMike Christie 667996a778SMike Christie return 0; 677996a778SMike Christie } 687996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn); 697996a778SMike Christie 707996a778SMike Christie void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask, 717996a778SMike Christie struct iscsi_data *hdr, 727996a778SMike Christie int transport_data_cnt) 737996a778SMike Christie { 747996a778SMike Christie struct iscsi_conn *conn = ctask->conn; 757996a778SMike Christie 767996a778SMike Christie memset(hdr, 0, sizeof(struct iscsi_data)); 777996a778SMike Christie hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG); 787996a778SMike Christie hdr->datasn = cpu_to_be32(ctask->unsol_datasn); 797996a778SMike Christie ctask->unsol_datasn++; 807996a778SMike Christie hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; 817996a778SMike Christie memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); 827996a778SMike Christie 837996a778SMike Christie hdr->itt = ctask->hdr->itt; 847996a778SMike Christie hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); 857996a778SMike Christie 867996a778SMike Christie hdr->offset = cpu_to_be32(ctask->total_length - 877996a778SMike Christie transport_data_cnt - 887996a778SMike Christie ctask->unsol_count); 897996a778SMike Christie 907996a778SMike Christie if (ctask->unsol_count > conn->max_xmit_dlength) { 917996a778SMike Christie hton24(hdr->dlength, conn->max_xmit_dlength); 927996a778SMike Christie ctask->data_count = conn->max_xmit_dlength; 937996a778SMike Christie hdr->flags = 0; 947996a778SMike Christie } else { 957996a778SMike Christie hton24(hdr->dlength, ctask->unsol_count); 967996a778SMike Christie ctask->data_count = ctask->unsol_count; 977996a778SMike Christie hdr->flags = ISCSI_FLAG_CMD_FINAL; 987996a778SMike Christie } 997996a778SMike Christie } 1007996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu); 1017996a778SMike Christie 1027996a778SMike Christie /** 1037996a778SMike Christie * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu 1047996a778SMike Christie * @ctask: iscsi cmd task 1057996a778SMike Christie * 1067996a778SMike Christie * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set 1077996a778SMike Christie * fields like dlength or final based on how much data it sends 1087996a778SMike Christie */ 1097996a778SMike Christie static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask) 1107996a778SMike Christie { 1117996a778SMike Christie struct iscsi_conn *conn = ctask->conn; 1127996a778SMike Christie struct iscsi_session *session = conn->session; 1137996a778SMike Christie struct iscsi_cmd *hdr = ctask->hdr; 1147996a778SMike Christie struct scsi_cmnd *sc = ctask->sc; 1157996a778SMike Christie 1167996a778SMike Christie hdr->opcode = ISCSI_OP_SCSI_CMD; 1177996a778SMike Christie hdr->flags = ISCSI_ATTR_SIMPLE; 1187996a778SMike Christie int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); 1197996a778SMike Christie hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) | 1207996a778SMike Christie (session->age << ISCSI_AGE_SHIFT); 1217996a778SMike Christie hdr->data_length = cpu_to_be32(sc->request_bufflen); 1227996a778SMike Christie hdr->cmdsn = cpu_to_be32(session->cmdsn); 1237996a778SMike Christie session->cmdsn++; 1247996a778SMike Christie hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); 1257996a778SMike Christie memcpy(hdr->cdb, sc->cmnd, sc->cmd_len); 1267996a778SMike Christie memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len); 1277996a778SMike Christie 1287996a778SMike Christie if (sc->sc_data_direction == DMA_TO_DEVICE) { 1297996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_WRITE; 1307996a778SMike Christie /* 1317996a778SMike Christie * Write counters: 1327996a778SMike Christie * 1337996a778SMike Christie * imm_count bytes to be sent right after 1347996a778SMike Christie * SCSI PDU Header 1357996a778SMike Christie * 1367996a778SMike Christie * unsol_count bytes(as Data-Out) to be sent 1377996a778SMike Christie * without R2T ack right after 1387996a778SMike Christie * immediate data 1397996a778SMike Christie * 1407996a778SMike Christie * r2t_data_count bytes to be sent via R2T ack's 1417996a778SMike Christie * 1427996a778SMike Christie * pad_count bytes to be sent as zero-padding 1437996a778SMike Christie */ 1447996a778SMike Christie ctask->imm_count = 0; 1457996a778SMike Christie ctask->unsol_count = 0; 1467996a778SMike Christie ctask->unsol_datasn = 0; 1477996a778SMike Christie 1487996a778SMike Christie if (session->imm_data_en) { 1497996a778SMike Christie if (ctask->total_length >= session->first_burst) 1507996a778SMike Christie ctask->imm_count = min(session->first_burst, 1517996a778SMike Christie conn->max_xmit_dlength); 1527996a778SMike Christie else 1537996a778SMike Christie ctask->imm_count = min(ctask->total_length, 1547996a778SMike Christie conn->max_xmit_dlength); 1557996a778SMike Christie hton24(ctask->hdr->dlength, ctask->imm_count); 1567996a778SMike Christie } else 1577996a778SMike Christie zero_data(ctask->hdr->dlength); 1587996a778SMike Christie 1597996a778SMike Christie if (!session->initial_r2t_en) 1607996a778SMike Christie ctask->unsol_count = min(session->first_burst, 1617996a778SMike Christie ctask->total_length) - ctask->imm_count; 1627996a778SMike Christie if (!ctask->unsol_count) 1637996a778SMike Christie /* No unsolicit Data-Out's */ 1647996a778SMike Christie ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL; 1657996a778SMike Christie } else { 1667996a778SMike Christie ctask->datasn = 0; 1677996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 1687996a778SMike Christie zero_data(hdr->dlength); 1697996a778SMike Christie 1707996a778SMike Christie if (sc->sc_data_direction == DMA_FROM_DEVICE) 1717996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_READ; 1727996a778SMike Christie } 1737996a778SMike Christie 1747996a778SMike Christie conn->scsicmd_pdus_cnt++; 1757996a778SMike Christie } 1767996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu); 1777996a778SMike Christie 1787996a778SMike Christie /** 1797996a778SMike Christie * iscsi_complete_command - return command back to scsi-ml 1807996a778SMike Christie * @session: iscsi session 1817996a778SMike Christie * @ctask: iscsi cmd task 1827996a778SMike Christie * 1837996a778SMike Christie * Must be called with session lock. 1847996a778SMike Christie * This function returns the scsi command to scsi-ml and returns 1857996a778SMike Christie * the cmd task to the pool of available cmd tasks. 1867996a778SMike Christie */ 1877996a778SMike Christie static void iscsi_complete_command(struct iscsi_session *session, 1887996a778SMike Christie struct iscsi_cmd_task *ctask) 1897996a778SMike Christie { 1907996a778SMike Christie struct scsi_cmnd *sc = ctask->sc; 1917996a778SMike Christie 192b6c395edSMike Christie ctask->state = ISCSI_TASK_COMPLETED; 1937996a778SMike Christie ctask->sc = NULL; 1947996a778SMike Christie list_del_init(&ctask->running); 1957996a778SMike Christie __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*)); 1967996a778SMike Christie sc->scsi_done(sc); 1977996a778SMike Christie } 1987996a778SMike Christie 1997996a778SMike Christie /** 2007996a778SMike Christie * iscsi_cmd_rsp - SCSI Command Response processing 2017996a778SMike Christie * @conn: iscsi connection 2027996a778SMike Christie * @hdr: iscsi header 2037996a778SMike Christie * @ctask: scsi command task 2047996a778SMike Christie * @data: cmd data buffer 2057996a778SMike Christie * @datalen: len of buffer 2067996a778SMike Christie * 2077996a778SMike Christie * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and 2087996a778SMike Christie * then completes the command and task. 2097996a778SMike Christie **/ 2107996a778SMike Christie static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 2117996a778SMike Christie struct iscsi_cmd_task *ctask, char *data, 2127996a778SMike Christie int datalen) 2137996a778SMike Christie { 2147996a778SMike Christie int rc; 2157996a778SMike Christie struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr; 2167996a778SMike Christie struct iscsi_session *session = conn->session; 2177996a778SMike Christie struct scsi_cmnd *sc = ctask->sc; 2187996a778SMike Christie 2197996a778SMike Christie rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr); 2207996a778SMike Christie if (rc) { 2217996a778SMike Christie sc->result = DID_ERROR << 16; 2227996a778SMike Christie goto out; 2237996a778SMike Christie } 2247996a778SMike Christie 2257996a778SMike Christie conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 2267996a778SMike Christie 2277996a778SMike Christie sc->result = (DID_OK << 16) | rhdr->cmd_status; 2287996a778SMike Christie 2297996a778SMike Christie if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { 2307996a778SMike Christie sc->result = DID_ERROR << 16; 2317996a778SMike Christie goto out; 2327996a778SMike Christie } 2337996a778SMike Christie 2347996a778SMike Christie if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { 2357996a778SMike Christie int senselen; 2367996a778SMike Christie 2377996a778SMike Christie if (datalen < 2) { 2387996a778SMike Christie invalid_datalen: 239be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: Got CHECK_CONDITION but " 240be2df72eSOr Gerlitz "invalid data buffer size of %d\n", datalen); 2417996a778SMike Christie sc->result = DID_BAD_TARGET << 16; 2427996a778SMike Christie goto out; 2437996a778SMike Christie } 2447996a778SMike Christie 2457996a778SMike Christie senselen = (data[0] << 8) | data[1]; 2467996a778SMike Christie if (datalen < senselen) 2477996a778SMike Christie goto invalid_datalen; 2487996a778SMike Christie 2497996a778SMike Christie memcpy(sc->sense_buffer, data + 2, 2507996a778SMike Christie min(senselen, SCSI_SENSE_BUFFERSIZE)); 2517996a778SMike Christie debug_scsi("copied %d bytes of sense\n", 2527996a778SMike Christie min(senselen, SCSI_SENSE_BUFFERSIZE)); 2537996a778SMike Christie } 2547996a778SMike Christie 2557996a778SMike Christie if (sc->sc_data_direction == DMA_TO_DEVICE) 2567996a778SMike Christie goto out; 2577996a778SMike Christie 2587996a778SMike Christie if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) { 2597996a778SMike Christie int res_count = be32_to_cpu(rhdr->residual_count); 2607996a778SMike Christie 2617996a778SMike Christie if (res_count > 0 && res_count <= sc->request_bufflen) 2627996a778SMike Christie sc->resid = res_count; 2637996a778SMike Christie else 2647996a778SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 2657996a778SMike Christie } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW) 2667996a778SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 2677996a778SMike Christie else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW) 2687996a778SMike Christie sc->resid = be32_to_cpu(rhdr->residual_count); 2697996a778SMike Christie 2707996a778SMike Christie out: 2717996a778SMike Christie debug_scsi("done [sc %lx res %d itt 0x%x]\n", 2727996a778SMike Christie (long)sc, sc->result, ctask->itt); 2737996a778SMike Christie conn->scsirsp_pdus_cnt++; 2747996a778SMike Christie 2757996a778SMike Christie iscsi_complete_command(conn->session, ctask); 2767996a778SMike Christie return rc; 2777996a778SMike Christie } 2787996a778SMike Christie 2797996a778SMike Christie /** 2807996a778SMike Christie * __iscsi_complete_pdu - complete pdu 2817996a778SMike Christie * @conn: iscsi conn 2827996a778SMike Christie * @hdr: iscsi header 2837996a778SMike Christie * @data: data buffer 2847996a778SMike Christie * @datalen: len of data buffer 2857996a778SMike Christie * 2867996a778SMike Christie * Completes pdu processing by freeing any resources allocated at 2877996a778SMike Christie * queuecommand or send generic. session lock must be held and verify 2887996a778SMike Christie * itt must have been called. 2897996a778SMike Christie */ 2907996a778SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 2917996a778SMike Christie char *data, int datalen) 2927996a778SMike Christie { 2937996a778SMike Christie struct iscsi_session *session = conn->session; 2947996a778SMike Christie int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; 2957996a778SMike Christie struct iscsi_cmd_task *ctask; 2967996a778SMike Christie struct iscsi_mgmt_task *mtask; 2977996a778SMike Christie uint32_t itt; 2987996a778SMike Christie 2997996a778SMike Christie if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) 3007996a778SMike Christie itt = hdr->itt & ISCSI_ITT_MASK; 3017996a778SMike Christie else 3027996a778SMike Christie itt = hdr->itt; 3037996a778SMike Christie 3047996a778SMike Christie if (itt < session->cmds_max) { 3057996a778SMike Christie ctask = session->cmds[itt]; 3067996a778SMike Christie 3077996a778SMike Christie debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n", 3087996a778SMike Christie opcode, conn->id, ctask->itt, datalen); 3097996a778SMike Christie 3107996a778SMike Christie switch(opcode) { 3117996a778SMike Christie case ISCSI_OP_SCSI_CMD_RSP: 3127996a778SMike Christie BUG_ON((void*)ctask != ctask->sc->SCp.ptr); 3137996a778SMike Christie rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data, 3147996a778SMike Christie datalen); 3157996a778SMike Christie break; 3167996a778SMike Christie case ISCSI_OP_SCSI_DATA_IN: 3177996a778SMike Christie BUG_ON((void*)ctask != ctask->sc->SCp.ptr); 3187996a778SMike Christie if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { 3197996a778SMike Christie conn->scsirsp_pdus_cnt++; 3207996a778SMike Christie iscsi_complete_command(session, ctask); 3217996a778SMike Christie } 3227996a778SMike Christie break; 3237996a778SMike Christie case ISCSI_OP_R2T: 3247996a778SMike Christie /* LLD handles this for now */ 3257996a778SMike Christie break; 3267996a778SMike Christie default: 3277996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 3287996a778SMike Christie break; 3297996a778SMike Christie } 3307996a778SMike Christie } else if (itt >= ISCSI_MGMT_ITT_OFFSET && 3317996a778SMike Christie itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) { 3327996a778SMike Christie mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET]; 3337996a778SMike Christie 3347996a778SMike Christie debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n", 3357996a778SMike Christie opcode, conn->id, mtask->itt, datalen); 3367996a778SMike Christie 3377996a778SMike Christie rc = iscsi_check_assign_cmdsn(session, 3387996a778SMike Christie (struct iscsi_nopin*)hdr); 3397996a778SMike Christie if (rc) 3408d2860b3SMike Christie goto done; 3417996a778SMike Christie 3428d2860b3SMike Christie switch(opcode) { 3438d2860b3SMike Christie case ISCSI_OP_LOGOUT_RSP: 3448d2860b3SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 3458d2860b3SMike Christie /* fall through */ 3468d2860b3SMike Christie case ISCSI_OP_LOGIN_RSP: 3478d2860b3SMike Christie case ISCSI_OP_TEXT_RSP: 3488d2860b3SMike Christie /* 3498d2860b3SMike Christie * login related PDU's exp_statsn is handled in 3508d2860b3SMike Christie * userspace 3518d2860b3SMike Christie */ 3527996a778SMike Christie rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen); 3537996a778SMike Christie list_del(&mtask->running); 3547996a778SMike Christie if (conn->login_mtask != mtask) 3557996a778SMike Christie __kfifo_put(session->mgmtpool.queue, 3567996a778SMike Christie (void*)&mtask, sizeof(void*)); 3577996a778SMike Christie break; 3587996a778SMike Christie case ISCSI_OP_SCSI_TMFUNC_RSP: 3597996a778SMike Christie if (datalen) { 3607996a778SMike Christie rc = ISCSI_ERR_PROTO; 3617996a778SMike Christie break; 3627996a778SMike Christie } 3638d2860b3SMike Christie 3648d2860b3SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 3657996a778SMike Christie conn->tmfrsp_pdus_cnt++; 3667996a778SMike Christie if (conn->tmabort_state == TMABORT_INITIAL) { 3677996a778SMike Christie conn->tmabort_state = 3687996a778SMike Christie ((struct iscsi_tm_rsp *)hdr)-> 3697996a778SMike Christie response == ISCSI_TMF_RSP_COMPLETE ? 3707996a778SMike Christie TMABORT_SUCCESS:TMABORT_FAILED; 3717996a778SMike Christie /* unblock eh_abort() */ 3727996a778SMike Christie wake_up(&conn->ehwait); 3737996a778SMike Christie } 3747996a778SMike Christie break; 3757996a778SMike Christie case ISCSI_OP_NOOP_IN: 3767996a778SMike Christie if (hdr->ttt != ISCSI_RESERVED_TAG) { 3777996a778SMike Christie rc = ISCSI_ERR_PROTO; 3787996a778SMike Christie break; 3797996a778SMike Christie } 3807996a778SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 3817996a778SMike Christie 3827996a778SMike Christie rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen); 3837996a778SMike Christie list_del(&mtask->running); 3847996a778SMike Christie if (conn->login_mtask != mtask) 3857996a778SMike Christie __kfifo_put(session->mgmtpool.queue, 3867996a778SMike Christie (void*)&mtask, sizeof(void*)); 3877996a778SMike Christie break; 3887996a778SMike Christie default: 3897996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 3907996a778SMike Christie break; 3917996a778SMike Christie } 3927996a778SMike Christie } else if (itt == ISCSI_RESERVED_TAG) { 3937996a778SMike Christie switch(opcode) { 3947996a778SMike Christie case ISCSI_OP_NOOP_IN: 3957996a778SMike Christie if (!datalen) { 3967996a778SMike Christie rc = iscsi_check_assign_cmdsn(session, 3977996a778SMike Christie (struct iscsi_nopin*)hdr); 3987996a778SMike Christie if (!rc && hdr->ttt != ISCSI_RESERVED_TAG) 3997996a778SMike Christie rc = iscsi_recv_pdu(conn->cls_conn, 4007996a778SMike Christie hdr, NULL, 0); 4017996a778SMike Christie } else 4027996a778SMike Christie rc = ISCSI_ERR_PROTO; 4037996a778SMike Christie break; 4047996a778SMike Christie case ISCSI_OP_REJECT: 4057996a778SMike Christie /* we need sth like iscsi_reject_rsp()*/ 4067996a778SMike Christie case ISCSI_OP_ASYNC_EVENT: 4078d2860b3SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 4087996a778SMike Christie /* we need sth like iscsi_async_event_rsp() */ 4097996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 4107996a778SMike Christie break; 4117996a778SMike Christie default: 4127996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 4137996a778SMike Christie break; 4147996a778SMike Christie } 4157996a778SMike Christie } else 4167996a778SMike Christie rc = ISCSI_ERR_BAD_ITT; 4177996a778SMike Christie 4188d2860b3SMike Christie done: 4197996a778SMike Christie return rc; 4207996a778SMike Christie } 4217996a778SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); 4227996a778SMike Christie 4237996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 4247996a778SMike Christie char *data, int datalen) 4257996a778SMike Christie { 4267996a778SMike Christie int rc; 4277996a778SMike Christie 4287996a778SMike Christie spin_lock(&conn->session->lock); 4297996a778SMike Christie rc = __iscsi_complete_pdu(conn, hdr, data, datalen); 4307996a778SMike Christie spin_unlock(&conn->session->lock); 4317996a778SMike Christie return rc; 4327996a778SMike Christie } 4337996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu); 4347996a778SMike Christie 4357996a778SMike Christie /* verify itt (itt encoding: age+cid+itt) */ 4367996a778SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 4377996a778SMike Christie uint32_t *ret_itt) 4387996a778SMike Christie { 4397996a778SMike Christie struct iscsi_session *session = conn->session; 4407996a778SMike Christie struct iscsi_cmd_task *ctask; 4417996a778SMike Christie uint32_t itt; 4427996a778SMike Christie 4437996a778SMike Christie if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) { 4447996a778SMike Christie if ((hdr->itt & ISCSI_AGE_MASK) != 4457996a778SMike Christie (session->age << ISCSI_AGE_SHIFT)) { 446be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: received itt %x expected " 4477996a778SMike Christie "session age (%x)\n", hdr->itt, 4487996a778SMike Christie session->age & ISCSI_AGE_MASK); 4497996a778SMike Christie return ISCSI_ERR_BAD_ITT; 4507996a778SMike Christie } 4517996a778SMike Christie 4527996a778SMike Christie if ((hdr->itt & ISCSI_CID_MASK) != 4537996a778SMike Christie (conn->id << ISCSI_CID_SHIFT)) { 454be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: received itt %x, expected " 4557996a778SMike Christie "CID (%x)\n", hdr->itt, conn->id); 4567996a778SMike Christie return ISCSI_ERR_BAD_ITT; 4577996a778SMike Christie } 4587996a778SMike Christie itt = hdr->itt & ISCSI_ITT_MASK; 4597996a778SMike Christie } else 4607996a778SMike Christie itt = hdr->itt; 4617996a778SMike Christie 4627996a778SMike Christie if (itt < session->cmds_max) { 4637996a778SMike Christie ctask = session->cmds[itt]; 4647996a778SMike Christie 4657996a778SMike Christie if (!ctask->sc) { 466be2df72eSOr Gerlitz printk(KERN_INFO "iscsi: dropping ctask with " 4677996a778SMike Christie "itt 0x%x\n", ctask->itt); 4687996a778SMike Christie /* force drop */ 4697996a778SMike Christie return ISCSI_ERR_NO_SCSI_CMD; 4707996a778SMike Christie } 4717996a778SMike Christie 4727996a778SMike Christie if (ctask->sc->SCp.phase != session->age) { 473be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: ctask's session age %d, " 4747996a778SMike Christie "expected %d\n", ctask->sc->SCp.phase, 4757996a778SMike Christie session->age); 4767996a778SMike Christie return ISCSI_ERR_SESSION_FAILED; 4777996a778SMike Christie } 4787996a778SMike Christie } 4797996a778SMike Christie 4807996a778SMike Christie *ret_itt = itt; 4817996a778SMike Christie return 0; 4827996a778SMike Christie } 4837996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt); 4847996a778SMike Christie 4857996a778SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) 4867996a778SMike Christie { 4877996a778SMike Christie struct iscsi_session *session = conn->session; 4887996a778SMike Christie unsigned long flags; 4897996a778SMike Christie 4907996a778SMike Christie spin_lock_irqsave(&session->lock, flags); 491656cffc9SMike Christie if (session->state == ISCSI_STATE_FAILED) { 492656cffc9SMike Christie spin_unlock_irqrestore(&session->lock, flags); 493656cffc9SMike Christie return; 494656cffc9SMike Christie } 495656cffc9SMike Christie 49667a61114SMike Christie if (conn->stop_stage == 0) 4977996a778SMike Christie session->state = ISCSI_STATE_FAILED; 4987996a778SMike Christie spin_unlock_irqrestore(&session->lock, flags); 4997996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 5007996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 5017996a778SMike Christie iscsi_conn_error(conn->cls_conn, err); 5027996a778SMike Christie } 5037996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure); 5047996a778SMike Christie 5057996a778SMike Christie /** 5067996a778SMike Christie * iscsi_data_xmit - xmit any command into the scheduled connection 5077996a778SMike Christie * @conn: iscsi connection 5087996a778SMike Christie * 5097996a778SMike Christie * Notes: 5107996a778SMike Christie * The function can return -EAGAIN in which case the caller must 5117996a778SMike Christie * re-schedule it again later or recover. '0' return code means 5127996a778SMike Christie * successful xmit. 5137996a778SMike Christie **/ 5147996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn) 5157996a778SMike Christie { 5167996a778SMike Christie struct iscsi_transport *tt; 5173219e529SMike Christie int rc = 0; 5187996a778SMike Christie 5197996a778SMike Christie if (unlikely(conn->suspend_tx)) { 5207996a778SMike Christie debug_scsi("conn %d Tx suspended!\n", conn->id); 5213219e529SMike Christie return -ENODATA; 5227996a778SMike Christie } 5237996a778SMike Christie tt = conn->session->tt; 5247996a778SMike Christie 5257996a778SMike Christie /* 5267996a778SMike Christie * Transmit in the following order: 5277996a778SMike Christie * 5287996a778SMike Christie * 1) un-finished xmit (ctask or mtask) 5297996a778SMike Christie * 2) immediate control PDUs 5307996a778SMike Christie * 3) write data 5317996a778SMike Christie * 4) SCSI commands 5327996a778SMike Christie * 5) non-immediate control PDUs 5337996a778SMike Christie * 5347996a778SMike Christie * No need to lock around __kfifo_get as long as 5357996a778SMike Christie * there's one producer and one consumer. 5367996a778SMike Christie */ 5377996a778SMike Christie 5387996a778SMike Christie BUG_ON(conn->ctask && conn->mtask); 5397996a778SMike Christie 5407996a778SMike Christie if (conn->ctask) { 5413219e529SMike Christie rc = tt->xmit_cmd_task(conn, conn->ctask); 5423219e529SMike Christie if (rc) 5437996a778SMike Christie goto again; 5447996a778SMike Christie /* done with this in-progress ctask */ 5457996a778SMike Christie conn->ctask = NULL; 5467996a778SMike Christie } 5477996a778SMike Christie if (conn->mtask) { 5483219e529SMike Christie rc = tt->xmit_mgmt_task(conn, conn->mtask); 5493219e529SMike Christie if (rc) 5507996a778SMike Christie goto again; 5517996a778SMike Christie /* done with this in-progress mtask */ 5527996a778SMike Christie conn->mtask = NULL; 5537996a778SMike Christie } 5547996a778SMike Christie 5557996a778SMike Christie /* process immediate first */ 5567996a778SMike Christie if (unlikely(__kfifo_len(conn->immqueue))) { 5577996a778SMike Christie while (__kfifo_get(conn->immqueue, (void*)&conn->mtask, 5587996a778SMike Christie sizeof(void*))) { 559994442e8SMike Christie spin_lock_bh(&conn->session->lock); 5607996a778SMike Christie list_add_tail(&conn->mtask->running, 5617996a778SMike Christie &conn->mgmt_run_list); 562994442e8SMike Christie spin_unlock_bh(&conn->session->lock); 5633219e529SMike Christie rc = tt->xmit_mgmt_task(conn, conn->mtask); 5643219e529SMike Christie if (rc) 5657996a778SMike Christie goto again; 5667996a778SMike Christie } 5677996a778SMike Christie /* done with this mtask */ 5687996a778SMike Christie conn->mtask = NULL; 5697996a778SMike Christie } 5707996a778SMike Christie 5717996a778SMike Christie /* process command queue */ 572b6c395edSMike Christie spin_lock_bh(&conn->session->lock); 573b6c395edSMike Christie while (!list_empty(&conn->xmitqueue)) { 5747996a778SMike Christie /* 5757996a778SMike Christie * iscsi tcp may readd the task to the xmitqueue to send 5767996a778SMike Christie * write data 5777996a778SMike Christie */ 578b6c395edSMike Christie conn->ctask = list_entry(conn->xmitqueue.next, 579b6c395edSMike Christie struct iscsi_cmd_task, running); 580b6c395edSMike Christie conn->ctask->state = ISCSI_TASK_RUNNING; 581b6c395edSMike Christie list_move_tail(conn->xmitqueue.next, &conn->run_list); 582994442e8SMike Christie spin_unlock_bh(&conn->session->lock); 583b6c395edSMike Christie 5843219e529SMike Christie rc = tt->xmit_cmd_task(conn, conn->ctask); 5853219e529SMike Christie if (rc) 5867996a778SMike Christie goto again; 587b6c395edSMike Christie spin_lock_bh(&conn->session->lock); 5887996a778SMike Christie } 589b6c395edSMike Christie spin_unlock_bh(&conn->session->lock); 5907996a778SMike Christie /* done with this ctask */ 5917996a778SMike Christie conn->ctask = NULL; 5927996a778SMike Christie 5937996a778SMike Christie /* process the rest control plane PDUs, if any */ 5947996a778SMike Christie if (unlikely(__kfifo_len(conn->mgmtqueue))) { 5957996a778SMike Christie while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask, 5967996a778SMike Christie sizeof(void*))) { 597994442e8SMike Christie spin_lock_bh(&conn->session->lock); 5987996a778SMike Christie list_add_tail(&conn->mtask->running, 5997996a778SMike Christie &conn->mgmt_run_list); 600994442e8SMike Christie spin_unlock_bh(&conn->session->lock); 6013219e529SMike Christie rc = tt->xmit_mgmt_task(conn, conn->mtask); 6023219e529SMike Christie if (rc) 6037996a778SMike Christie goto again; 6047996a778SMike Christie } 6057996a778SMike Christie /* done with this mtask */ 6067996a778SMike Christie conn->mtask = NULL; 6077996a778SMike Christie } 6087996a778SMike Christie 6093219e529SMike Christie return -ENODATA; 6107996a778SMike Christie 6117996a778SMike Christie again: 6127996a778SMike Christie if (unlikely(conn->suspend_tx)) 6133219e529SMike Christie return -ENODATA; 6147996a778SMike Christie 6153219e529SMike Christie return rc; 6167996a778SMike Christie } 6177996a778SMike Christie 6187996a778SMike Christie static void iscsi_xmitworker(void *data) 6197996a778SMike Christie { 6207996a778SMike Christie struct iscsi_conn *conn = data; 6213219e529SMike Christie int rc; 6227996a778SMike Christie /* 6237996a778SMike Christie * serialize Xmit worker on a per-connection basis. 6247996a778SMike Christie */ 6257996a778SMike Christie mutex_lock(&conn->xmitmutex); 6263219e529SMike Christie do { 6273219e529SMike Christie rc = iscsi_data_xmit(conn); 6283219e529SMike Christie } while (rc >= 0 || rc == -EAGAIN); 6297996a778SMike Christie mutex_unlock(&conn->xmitmutex); 6307996a778SMike Christie } 6317996a778SMike Christie 6327996a778SMike Christie enum { 6337996a778SMike Christie FAILURE_BAD_HOST = 1, 6347996a778SMike Christie FAILURE_SESSION_FAILED, 6357996a778SMike Christie FAILURE_SESSION_FREED, 6367996a778SMike Christie FAILURE_WINDOW_CLOSED, 6377996a778SMike Christie FAILURE_SESSION_TERMINATE, 638656cffc9SMike Christie FAILURE_SESSION_IN_RECOVERY, 6397996a778SMike Christie FAILURE_SESSION_RECOVERY_TIMEOUT, 6407996a778SMike Christie }; 6417996a778SMike Christie 6427996a778SMike Christie int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) 6437996a778SMike Christie { 6447996a778SMike Christie struct Scsi_Host *host; 6457996a778SMike Christie int reason = 0; 6467996a778SMike Christie struct iscsi_session *session; 6477996a778SMike Christie struct iscsi_conn *conn; 6487996a778SMike Christie struct iscsi_cmd_task *ctask = NULL; 6497996a778SMike Christie 6507996a778SMike Christie sc->scsi_done = done; 6517996a778SMike Christie sc->result = 0; 6527996a778SMike Christie 6537996a778SMike Christie host = sc->device->host; 6547996a778SMike Christie session = iscsi_hostdata(host->hostdata); 6557996a778SMike Christie 6567996a778SMike Christie spin_lock(&session->lock); 6577996a778SMike Christie 658656cffc9SMike Christie /* 659656cffc9SMike Christie * ISCSI_STATE_FAILED is a temp. state. The recovery 660656cffc9SMike Christie * code will decide what is best to do with command queued 661656cffc9SMike Christie * during this time 662656cffc9SMike Christie */ 663656cffc9SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN && 664656cffc9SMike Christie session->state != ISCSI_STATE_FAILED) { 665656cffc9SMike Christie /* 666656cffc9SMike Christie * to handle the race between when we set the recovery state 667656cffc9SMike Christie * and block the session we requeue here (commands could 668656cffc9SMike Christie * be entering our queuecommand while a block is starting 669656cffc9SMike Christie * up because the block code is not locked) 670656cffc9SMike Christie */ 671656cffc9SMike Christie if (session->state == ISCSI_STATE_IN_RECOVERY) { 672656cffc9SMike Christie reason = FAILURE_SESSION_IN_RECOVERY; 67367a61114SMike Christie goto reject; 6747996a778SMike Christie } 675656cffc9SMike Christie 676656cffc9SMike Christie if (session->state == ISCSI_STATE_RECOVERY_FAILED) 677656cffc9SMike Christie reason = FAILURE_SESSION_RECOVERY_TIMEOUT; 678656cffc9SMike Christie else if (session->state == ISCSI_STATE_TERMINATE) 679656cffc9SMike Christie reason = FAILURE_SESSION_TERMINATE; 680656cffc9SMike Christie else 6817996a778SMike Christie reason = FAILURE_SESSION_FREED; 6827996a778SMike Christie goto fault; 6837996a778SMike Christie } 6847996a778SMike Christie 6857996a778SMike Christie /* 6867996a778SMike Christie * Check for iSCSI window and take care of CmdSN wrap-around 6877996a778SMike Christie */ 6887996a778SMike Christie if ((int)(session->max_cmdsn - session->cmdsn) < 0) { 6897996a778SMike Christie reason = FAILURE_WINDOW_CLOSED; 6907996a778SMike Christie goto reject; 6917996a778SMike Christie } 6927996a778SMike Christie 6937996a778SMike Christie conn = session->leadconn; 6947996a778SMike Christie 6957996a778SMike Christie __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*)); 6967996a778SMike Christie sc->SCp.phase = session->age; 6977996a778SMike Christie sc->SCp.ptr = (char *)ctask; 6987996a778SMike Christie 699b6c395edSMike Christie ctask->state = ISCSI_TASK_PENDING; 7007996a778SMike Christie ctask->mtask = NULL; 7017996a778SMike Christie ctask->conn = conn; 7027996a778SMike Christie ctask->sc = sc; 7037996a778SMike Christie INIT_LIST_HEAD(&ctask->running); 7047996a778SMike Christie ctask->total_length = sc->request_bufflen; 7057996a778SMike Christie iscsi_prep_scsi_cmd_pdu(ctask); 7067996a778SMike Christie 7077996a778SMike Christie session->tt->init_cmd_task(ctask); 7087996a778SMike Christie 709b6c395edSMike Christie list_add_tail(&ctask->running, &conn->xmitqueue); 7107996a778SMike Christie debug_scsi( 7117996a778SMike Christie "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n", 7127996a778SMike Christie sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read", 7137996a778SMike Christie conn->id, (long)sc, ctask->itt, sc->request_bufflen, 7147996a778SMike Christie session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1); 7157996a778SMike Christie spin_unlock(&session->lock); 7167996a778SMike Christie 7177996a778SMike Christie scsi_queue_work(host, &conn->xmitwork); 7187996a778SMike Christie return 0; 7197996a778SMike Christie 7207996a778SMike Christie reject: 7217996a778SMike Christie spin_unlock(&session->lock); 7227996a778SMike Christie debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason); 7237996a778SMike Christie return SCSI_MLQUEUE_HOST_BUSY; 7247996a778SMike Christie 7257996a778SMike Christie fault: 7267996a778SMike Christie spin_unlock(&session->lock); 727be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n", 7287996a778SMike Christie sc->cmnd[0], reason); 7297996a778SMike Christie sc->result = (DID_NO_CONNECT << 16); 7307996a778SMike Christie sc->resid = sc->request_bufflen; 7317996a778SMike Christie sc->scsi_done(sc); 7327996a778SMike Christie return 0; 7337996a778SMike Christie } 7347996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand); 7357996a778SMike Christie 7367996a778SMike Christie int iscsi_change_queue_depth(struct scsi_device *sdev, int depth) 7377996a778SMike Christie { 7387996a778SMike Christie if (depth > ISCSI_MAX_CMD_PER_LUN) 7397996a778SMike Christie depth = ISCSI_MAX_CMD_PER_LUN; 7407996a778SMike Christie scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); 7417996a778SMike Christie return sdev->queue_depth; 7427996a778SMike Christie } 7437996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_change_queue_depth); 7447996a778SMike Christie 7457996a778SMike Christie static int 7467996a778SMike Christie iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 7477996a778SMike Christie char *data, uint32_t data_size) 7487996a778SMike Christie { 7497996a778SMike Christie struct iscsi_session *session = conn->session; 7507996a778SMike Christie struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; 7517996a778SMike Christie struct iscsi_mgmt_task *mtask; 7527996a778SMike Christie 7537996a778SMike Christie spin_lock_bh(&session->lock); 7547996a778SMike Christie if (session->state == ISCSI_STATE_TERMINATE) { 7557996a778SMike Christie spin_unlock_bh(&session->lock); 7567996a778SMike Christie return -EPERM; 7577996a778SMike Christie } 7587996a778SMike Christie if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) || 7597996a778SMike Christie hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) 7607996a778SMike Christie /* 7617996a778SMike Christie * Login and Text are sent serially, in 7627996a778SMike Christie * request-followed-by-response sequence. 7637996a778SMike Christie * Same mtask can be used. Same ITT must be used. 7647996a778SMike Christie * Note that login_mtask is preallocated at conn_create(). 7657996a778SMike Christie */ 7667996a778SMike Christie mtask = conn->login_mtask; 7677996a778SMike Christie else { 7687996a778SMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); 7697996a778SMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); 7707996a778SMike Christie 7718d2860b3SMike Christie nop->exp_statsn = cpu_to_be32(conn->exp_statsn); 7727996a778SMike Christie if (!__kfifo_get(session->mgmtpool.queue, 7737996a778SMike Christie (void*)&mtask, sizeof(void*))) { 7747996a778SMike Christie spin_unlock_bh(&session->lock); 7757996a778SMike Christie return -ENOSPC; 7767996a778SMike Christie } 7777996a778SMike Christie } 7787996a778SMike Christie 7797996a778SMike Christie /* 7808d2860b3SMike Christie * pre-format CmdSN for outgoing PDU. 7817996a778SMike Christie */ 7827996a778SMike Christie if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) { 7837996a778SMike Christie hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) | 7847996a778SMike Christie (session->age << ISCSI_AGE_SHIFT); 7857996a778SMike Christie nop->cmdsn = cpu_to_be32(session->cmdsn); 7867996a778SMike Christie if (conn->c_stage == ISCSI_CONN_STARTED && 7877996a778SMike Christie !(hdr->opcode & ISCSI_OP_IMMEDIATE)) 7887996a778SMike Christie session->cmdsn++; 7897996a778SMike Christie } else 7907996a778SMike Christie /* do not advance CmdSN */ 7917996a778SMike Christie nop->cmdsn = cpu_to_be32(session->cmdsn); 7927996a778SMike Christie 7937996a778SMike Christie if (data_size) { 7947996a778SMike Christie memcpy(mtask->data, data, data_size); 7957996a778SMike Christie mtask->data_count = data_size; 7967996a778SMike Christie } else 7977996a778SMike Christie mtask->data_count = 0; 7987996a778SMike Christie 7997996a778SMike Christie INIT_LIST_HEAD(&mtask->running); 8007996a778SMike Christie memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr)); 8017996a778SMike Christie if (session->tt->init_mgmt_task) 8027996a778SMike Christie session->tt->init_mgmt_task(conn, mtask, data, data_size); 8037996a778SMike Christie spin_unlock_bh(&session->lock); 8047996a778SMike Christie 8057996a778SMike Christie debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n", 8067996a778SMike Christie hdr->opcode, hdr->itt, data_size); 8077996a778SMike Christie 8087996a778SMike Christie /* 8097996a778SMike Christie * since send_pdu() could be called at least from two contexts, 8107996a778SMike Christie * we need to serialize __kfifo_put, so we don't have to take 8117996a778SMike Christie * additional lock on fast data-path 8127996a778SMike Christie */ 8137996a778SMike Christie if (hdr->opcode & ISCSI_OP_IMMEDIATE) 8147996a778SMike Christie __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*)); 8157996a778SMike Christie else 8167996a778SMike Christie __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*)); 8177996a778SMike Christie 8187996a778SMike Christie scsi_queue_work(session->host, &conn->xmitwork); 8197996a778SMike Christie return 0; 8207996a778SMike Christie } 8217996a778SMike Christie 8227996a778SMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, 8237996a778SMike Christie char *data, uint32_t data_size) 8247996a778SMike Christie { 8257996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 8267996a778SMike Christie int rc; 8277996a778SMike Christie 8287996a778SMike Christie mutex_lock(&conn->xmitmutex); 8297996a778SMike Christie rc = iscsi_conn_send_generic(conn, hdr, data, data_size); 8307996a778SMike Christie mutex_unlock(&conn->xmitmutex); 8317996a778SMike Christie 8327996a778SMike Christie return rc; 8337996a778SMike Christie } 8347996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); 8357996a778SMike Christie 8367996a778SMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) 8377996a778SMike Christie { 8387996a778SMike Christie struct iscsi_session *session = class_to_transport_session(cls_session); 8397996a778SMike Christie struct iscsi_conn *conn = session->leadconn; 8407996a778SMike Christie 8417996a778SMike Christie spin_lock_bh(&session->lock); 8427996a778SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 843656cffc9SMike Christie session->state = ISCSI_STATE_RECOVERY_FAILED; 8447996a778SMike Christie if (conn) 8457996a778SMike Christie wake_up(&conn->ehwait); 8467996a778SMike Christie } 8477996a778SMike Christie spin_unlock_bh(&session->lock); 8487996a778SMike Christie } 8497996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); 8507996a778SMike Christie 8517996a778SMike Christie int iscsi_eh_host_reset(struct scsi_cmnd *sc) 8527996a778SMike Christie { 8537996a778SMike Christie struct Scsi_Host *host = sc->device->host; 8547996a778SMike Christie struct iscsi_session *session = iscsi_hostdata(host->hostdata); 8557996a778SMike Christie struct iscsi_conn *conn = session->leadconn; 8567996a778SMike Christie int fail_session = 0; 8577996a778SMike Christie 8587996a778SMike Christie spin_lock_bh(&session->lock); 8597996a778SMike Christie if (session->state == ISCSI_STATE_TERMINATE) { 8607996a778SMike Christie failed: 8617996a778SMike Christie debug_scsi("failing host reset: session terminated " 8627996a778SMike Christie "[CID %d age %d]", conn->id, session->age); 8637996a778SMike Christie spin_unlock_bh(&session->lock); 8647996a778SMike Christie return FAILED; 8657996a778SMike Christie } 8667996a778SMike Christie 8677996a778SMike Christie if (sc->SCp.phase == session->age) { 8687996a778SMike Christie debug_scsi("failing connection CID %d due to SCSI host reset", 8697996a778SMike Christie conn->id); 8707996a778SMike Christie fail_session = 1; 8717996a778SMike Christie } 8727996a778SMike Christie spin_unlock_bh(&session->lock); 8737996a778SMike Christie 8747996a778SMike Christie /* 8757996a778SMike Christie * we drop the lock here but the leadconn cannot be destoyed while 8767996a778SMike Christie * we are in the scsi eh 8777996a778SMike Christie */ 878656cffc9SMike Christie if (fail_session) 8797996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 8807996a778SMike Christie 8817996a778SMike Christie debug_scsi("iscsi_eh_host_reset wait for relogin\n"); 8827996a778SMike Christie wait_event_interruptible(conn->ehwait, 8837996a778SMike Christie session->state == ISCSI_STATE_TERMINATE || 8847996a778SMike Christie session->state == ISCSI_STATE_LOGGED_IN || 885656cffc9SMike Christie session->state == ISCSI_STATE_RECOVERY_FAILED); 8867996a778SMike Christie if (signal_pending(current)) 8877996a778SMike Christie flush_signals(current); 8887996a778SMike Christie 8897996a778SMike Christie spin_lock_bh(&session->lock); 8907996a778SMike Christie if (session->state == ISCSI_STATE_LOGGED_IN) 891be2df72eSOr Gerlitz printk(KERN_INFO "iscsi: host reset succeeded\n"); 8927996a778SMike Christie else 8937996a778SMike Christie goto failed; 8947996a778SMike Christie spin_unlock_bh(&session->lock); 8957996a778SMike Christie 8967996a778SMike Christie return SUCCESS; 8977996a778SMike Christie } 8987996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_host_reset); 8997996a778SMike Christie 9007996a778SMike Christie static void iscsi_tmabort_timedout(unsigned long data) 9017996a778SMike Christie { 9027996a778SMike Christie struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data; 9037996a778SMike Christie struct iscsi_conn *conn = ctask->conn; 9047996a778SMike Christie struct iscsi_session *session = conn->session; 9057996a778SMike Christie 9067996a778SMike Christie spin_lock(&session->lock); 9077996a778SMike Christie if (conn->tmabort_state == TMABORT_INITIAL) { 9087996a778SMike Christie conn->tmabort_state = TMABORT_TIMEDOUT; 9097996a778SMike Christie debug_scsi("tmabort timedout [sc %p itt 0x%x]\n", 9107996a778SMike Christie ctask->sc, ctask->itt); 9117996a778SMike Christie /* unblock eh_abort() */ 9127996a778SMike Christie wake_up(&conn->ehwait); 9137996a778SMike Christie } 9147996a778SMike Christie spin_unlock(&session->lock); 9157996a778SMike Christie } 9167996a778SMike Christie 9177996a778SMike Christie /* must be called with the mutex lock */ 9187996a778SMike Christie static int iscsi_exec_abort_task(struct scsi_cmnd *sc, 9197996a778SMike Christie struct iscsi_cmd_task *ctask) 9207996a778SMike Christie { 9217996a778SMike Christie struct iscsi_conn *conn = ctask->conn; 9227996a778SMike Christie struct iscsi_session *session = conn->session; 9237996a778SMike Christie struct iscsi_tm *hdr = &conn->tmhdr; 9247996a778SMike Christie int rc; 9257996a778SMike Christie 9267996a778SMike Christie /* 9277996a778SMike Christie * ctask timed out but session is OK requests must be serialized. 9287996a778SMike Christie */ 9297996a778SMike Christie memset(hdr, 0, sizeof(struct iscsi_tm)); 9307996a778SMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 9317996a778SMike Christie hdr->flags = ISCSI_TM_FUNC_ABORT_TASK; 9327996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 9337996a778SMike Christie memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); 9347996a778SMike Christie hdr->rtt = ctask->hdr->itt; 9357996a778SMike Christie hdr->refcmdsn = ctask->hdr->cmdsn; 9367996a778SMike Christie 9377996a778SMike Christie rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr, 9387996a778SMike Christie NULL, 0); 9397996a778SMike Christie if (rc) { 9407996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 9417996a778SMike Christie debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc); 9427996a778SMike Christie return rc; 9437996a778SMike Christie } 9447996a778SMike Christie 9457996a778SMike Christie debug_scsi("abort sent [itt 0x%x]\n", ctask->itt); 9467996a778SMike Christie 9477996a778SMike Christie spin_lock_bh(&session->lock); 9487996a778SMike Christie ctask->mtask = (struct iscsi_mgmt_task *) 9497996a778SMike Christie session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) - 9507996a778SMike Christie ISCSI_MGMT_ITT_OFFSET]; 9517996a778SMike Christie 9527996a778SMike Christie if (conn->tmabort_state == TMABORT_INITIAL) { 9537996a778SMike Christie conn->tmfcmd_pdus_cnt++; 9547996a778SMike Christie conn->tmabort_timer.expires = 10*HZ + jiffies; 9557996a778SMike Christie conn->tmabort_timer.function = iscsi_tmabort_timedout; 9567996a778SMike Christie conn->tmabort_timer.data = (unsigned long)ctask; 9577996a778SMike Christie add_timer(&conn->tmabort_timer); 9587996a778SMike Christie debug_scsi("abort set timeout [itt 0x%x]", ctask->itt); 9597996a778SMike Christie } 9607996a778SMike Christie spin_unlock_bh(&session->lock); 9617996a778SMike Christie mutex_unlock(&conn->xmitmutex); 9627996a778SMike Christie 9637996a778SMike Christie /* 9647996a778SMike Christie * block eh thread until: 9657996a778SMike Christie * 9667996a778SMike Christie * 1) abort response 9677996a778SMike Christie * 2) abort timeout 9687996a778SMike Christie * 3) session is terminated or restarted or userspace has 9697996a778SMike Christie * given up on recovery 9707996a778SMike Christie */ 9717996a778SMike Christie wait_event_interruptible(conn->ehwait, 9727996a778SMike Christie sc->SCp.phase != session->age || 9737996a778SMike Christie session->state != ISCSI_STATE_LOGGED_IN || 974656cffc9SMike Christie conn->tmabort_state != TMABORT_INITIAL); 9757996a778SMike Christie if (signal_pending(current)) 9767996a778SMike Christie flush_signals(current); 9777996a778SMike Christie del_timer_sync(&conn->tmabort_timer); 9787996a778SMike Christie 9797996a778SMike Christie mutex_lock(&conn->xmitmutex); 9807996a778SMike Christie return 0; 9817996a778SMike Christie } 9827996a778SMike Christie 9837996a778SMike Christie /* 9847996a778SMike Christie * xmit mutex and session lock must be held 9857996a778SMike Christie */ 986b6c395edSMike Christie static struct iscsi_mgmt_task * 987b6c395edSMike Christie iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt) 988b6c395edSMike Christie { 989b6c395edSMike Christie int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*); 990b6c395edSMike Christie struct iscsi_mgmt_task *task; 991b6c395edSMike Christie 992b6c395edSMike Christie debug_scsi("searching %d tasks\n", nr_tasks); 993b6c395edSMike Christie 994b6c395edSMike Christie for (i = 0; i < nr_tasks; i++) { 995b6c395edSMike Christie __kfifo_get(fifo, (void*)&task, sizeof(void*)); 996b6c395edSMike Christie debug_scsi("check task %u\n", task->itt); 997b6c395edSMike Christie 998b6c395edSMike Christie if (task->itt == itt) { 999b6c395edSMike Christie debug_scsi("matched task\n"); 1000b6c395edSMike Christie return task; 10017996a778SMike Christie } 10027996a778SMike Christie 1003b6c395edSMike Christie __kfifo_put(fifo, (void*)&task, sizeof(void*)); 1004b6c395edSMike Christie } 1005b6c395edSMike Christie return NULL; 1006b6c395edSMike Christie } 10077996a778SMike Christie 10087996a778SMike Christie static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask) 10097996a778SMike Christie { 10107996a778SMike Christie struct iscsi_conn *conn = ctask->conn; 10117996a778SMike Christie struct iscsi_session *session = conn->session; 10127996a778SMike Christie 10137996a778SMike Christie if (!ctask->mtask) 10147996a778SMike Christie return -EINVAL; 10157996a778SMike Christie 10167996a778SMike Christie if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt)) 10177996a778SMike Christie list_del(&ctask->mtask->running); 10187996a778SMike Christie __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask, 10197996a778SMike Christie sizeof(void*)); 10207996a778SMike Christie ctask->mtask = NULL; 10217996a778SMike Christie return 0; 10227996a778SMike Christie } 10237996a778SMike Christie 10247996a778SMike Christie /* 10257996a778SMike Christie * session lock and xmitmutex must be held 10267996a778SMike Christie */ 10277996a778SMike Christie static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, 10287996a778SMike Christie int err) 10297996a778SMike Christie { 10307996a778SMike Christie struct scsi_cmnd *sc; 10317996a778SMike Christie 10327996a778SMike Christie conn->session->tt->cleanup_cmd_task(conn, ctask); 10337996a778SMike Christie iscsi_ctask_mtask_cleanup(ctask); 10347996a778SMike Christie 10357996a778SMike Christie sc = ctask->sc; 10367996a778SMike Christie if (!sc) 10377996a778SMike Christie return; 10387996a778SMike Christie sc->result = err; 10397996a778SMike Christie sc->resid = sc->request_bufflen; 10407996a778SMike Christie iscsi_complete_command(conn->session, ctask); 10417996a778SMike Christie } 10427996a778SMike Christie 10437996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc) 10447996a778SMike Christie { 10457996a778SMike Christie struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr; 10467996a778SMike Christie struct iscsi_conn *conn = ctask->conn; 10477996a778SMike Christie struct iscsi_session *session = conn->session; 10487996a778SMike Christie int rc; 10497996a778SMike Christie 10507996a778SMike Christie conn->eh_abort_cnt++; 10517996a778SMike Christie debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt); 10527996a778SMike Christie 10537996a778SMike Christie mutex_lock(&conn->xmitmutex); 10547996a778SMike Christie spin_lock_bh(&session->lock); 10557996a778SMike Christie 10567996a778SMike Christie /* 10577996a778SMike Christie * If we are not logged in or we have started a new session 10587996a778SMike Christie * then let the host reset code handle this 10597996a778SMike Christie */ 10607996a778SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN || 10617996a778SMike Christie sc->SCp.phase != session->age) 10627996a778SMike Christie goto failed; 10637996a778SMike Christie 10647996a778SMike Christie /* ctask completed before time out */ 10657996a778SMike Christie if (!ctask->sc) 10667996a778SMike Christie goto success; 10677996a778SMike Christie 10687996a778SMike Christie /* what should we do here ? */ 10697996a778SMike Christie if (conn->ctask == ctask) { 1070be2df72eSOr Gerlitz printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. " 1071be2df72eSOr Gerlitz "Failing abort\n", sc, ctask->itt); 10727996a778SMike Christie goto failed; 10737996a778SMike Christie } 10747996a778SMike Christie 1075b6c395edSMike Christie if (ctask->state == ISCSI_TASK_PENDING) 10767996a778SMike Christie goto success; 10777996a778SMike Christie 10787996a778SMike Christie conn->tmabort_state = TMABORT_INITIAL; 10797996a778SMike Christie 10807996a778SMike Christie spin_unlock_bh(&session->lock); 10817996a778SMike Christie rc = iscsi_exec_abort_task(sc, ctask); 10827996a778SMike Christie spin_lock_bh(&session->lock); 10837996a778SMike Christie 10847996a778SMike Christie iscsi_ctask_mtask_cleanup(ctask); 10857996a778SMike Christie if (rc || sc->SCp.phase != session->age || 10867996a778SMike Christie session->state != ISCSI_STATE_LOGGED_IN) 10877996a778SMike Christie goto failed; 10887996a778SMike Christie 10897996a778SMike Christie /* ctask completed before tmf abort response */ 10907996a778SMike Christie if (!ctask->sc) { 10917996a778SMike Christie debug_scsi("sc completed while abort in progress\n"); 10927996a778SMike Christie goto success; 10937996a778SMike Christie } 10947996a778SMike Christie 10957996a778SMike Christie if (conn->tmabort_state != TMABORT_SUCCESS) { 10967996a778SMike Christie spin_unlock_bh(&session->lock); 10977996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 10987996a778SMike Christie spin_lock_bh(&session->lock); 10997996a778SMike Christie goto failed; 11007996a778SMike Christie } 11017996a778SMike Christie 11027996a778SMike Christie success: 11037996a778SMike Christie debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt); 11047996a778SMike Christie spin_unlock_bh(&session->lock); 11057996a778SMike Christie 11067996a778SMike Christie /* 11077996a778SMike Christie * clean up task if aborted. we have the xmitmutex so grab 11087996a778SMike Christie * the recv lock as a writer 11097996a778SMike Christie */ 11107996a778SMike Christie write_lock_bh(conn->recv_lock); 11117996a778SMike Christie spin_lock(&session->lock); 11127996a778SMike Christie fail_command(conn, ctask, DID_ABORT << 16); 11137996a778SMike Christie spin_unlock(&session->lock); 11147996a778SMike Christie write_unlock_bh(conn->recv_lock); 11157996a778SMike Christie 11167996a778SMike Christie mutex_unlock(&conn->xmitmutex); 11177996a778SMike Christie return SUCCESS; 11187996a778SMike Christie 11197996a778SMike Christie failed: 11207996a778SMike Christie spin_unlock_bh(&session->lock); 11217996a778SMike Christie mutex_unlock(&conn->xmitmutex); 11227996a778SMike Christie 11237996a778SMike Christie debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt); 11247996a778SMike Christie return FAILED; 11257996a778SMike Christie } 11267996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort); 11277996a778SMike Christie 11287996a778SMike Christie int 11297996a778SMike Christie iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size) 11307996a778SMike Christie { 11317996a778SMike Christie int i; 11327996a778SMike Christie 11337996a778SMike Christie *items = kmalloc(max * sizeof(void*), GFP_KERNEL); 11347996a778SMike Christie if (*items == NULL) 11357996a778SMike Christie return -ENOMEM; 11367996a778SMike Christie 11377996a778SMike Christie q->max = max; 11387996a778SMike Christie q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL); 11397996a778SMike Christie if (q->pool == NULL) { 11407996a778SMike Christie kfree(*items); 11417996a778SMike Christie return -ENOMEM; 11427996a778SMike Christie } 11437996a778SMike Christie 11447996a778SMike Christie q->queue = kfifo_init((void*)q->pool, max * sizeof(void*), 11457996a778SMike Christie GFP_KERNEL, NULL); 11467996a778SMike Christie if (q->queue == ERR_PTR(-ENOMEM)) { 11477996a778SMike Christie kfree(q->pool); 11487996a778SMike Christie kfree(*items); 11497996a778SMike Christie return -ENOMEM; 11507996a778SMike Christie } 11517996a778SMike Christie 11527996a778SMike Christie for (i = 0; i < max; i++) { 11537996a778SMike Christie q->pool[i] = kmalloc(item_size, GFP_KERNEL); 11547996a778SMike Christie if (q->pool[i] == NULL) { 11557996a778SMike Christie int j; 11567996a778SMike Christie 11577996a778SMike Christie for (j = 0; j < i; j++) 11587996a778SMike Christie kfree(q->pool[j]); 11597996a778SMike Christie 11607996a778SMike Christie kfifo_free(q->queue); 11617996a778SMike Christie kfree(q->pool); 11627996a778SMike Christie kfree(*items); 11637996a778SMike Christie return -ENOMEM; 11647996a778SMike Christie } 11657996a778SMike Christie memset(q->pool[i], 0, item_size); 11667996a778SMike Christie (*items)[i] = q->pool[i]; 11677996a778SMike Christie __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*)); 11687996a778SMike Christie } 11697996a778SMike Christie return 0; 11707996a778SMike Christie } 11717996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init); 11727996a778SMike Christie 11737996a778SMike Christie void iscsi_pool_free(struct iscsi_queue *q, void **items) 11747996a778SMike Christie { 11757996a778SMike Christie int i; 11767996a778SMike Christie 11777996a778SMike Christie for (i = 0; i < q->max; i++) 11787996a778SMike Christie kfree(items[i]); 11797996a778SMike Christie kfree(q->pool); 11807996a778SMike Christie kfree(items); 11817996a778SMike Christie } 11827996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free); 11837996a778SMike Christie 11847996a778SMike Christie /* 11857996a778SMike Christie * iSCSI Session's hostdata organization: 11867996a778SMike Christie * 11877996a778SMike Christie * *------------------* <== hostdata_session(host->hostdata) 11887996a778SMike Christie * | ptr to class sess| 11897996a778SMike Christie * |------------------| <== iscsi_hostdata(host->hostdata) 11907996a778SMike Christie * | iscsi_session | 11917996a778SMike Christie * *------------------* 11927996a778SMike Christie */ 11937996a778SMike Christie 11947996a778SMike Christie #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \ 11957996a778SMike Christie _sz % sizeof(unsigned long)) 11967996a778SMike Christie 11977996a778SMike Christie #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata)) 11987996a778SMike Christie 11997996a778SMike Christie /** 12007996a778SMike Christie * iscsi_session_setup - create iscsi cls session and host and session 12017996a778SMike Christie * @scsit: scsi transport template 12027996a778SMike Christie * @iscsit: iscsi transport template 12037996a778SMike Christie * @initial_cmdsn: initial CmdSN 12047996a778SMike Christie * @hostno: host no allocated 12057996a778SMike Christie * 12067996a778SMike Christie * This can be used by software iscsi_transports that allocate 12077996a778SMike Christie * a session per scsi host. 12087996a778SMike Christie **/ 12097996a778SMike Christie struct iscsi_cls_session * 12107996a778SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit, 12117996a778SMike Christie struct scsi_transport_template *scsit, 12127996a778SMike Christie int cmd_task_size, int mgmt_task_size, 12137996a778SMike Christie uint32_t initial_cmdsn, uint32_t *hostno) 12147996a778SMike Christie { 12157996a778SMike Christie struct Scsi_Host *shost; 12167996a778SMike Christie struct iscsi_session *session; 12177996a778SMike Christie struct iscsi_cls_session *cls_session; 12187996a778SMike Christie int cmd_i; 12197996a778SMike Christie 12207996a778SMike Christie shost = scsi_host_alloc(iscsit->host_template, 12217996a778SMike Christie hostdata_privsize(sizeof(*session))); 12227996a778SMike Christie if (!shost) 12237996a778SMike Christie return NULL; 12247996a778SMike Christie 12257996a778SMike Christie shost->max_id = 1; 12267996a778SMike Christie shost->max_channel = 0; 12277996a778SMike Christie shost->max_lun = iscsit->max_lun; 12287996a778SMike Christie shost->max_cmd_len = iscsit->max_cmd_len; 12297996a778SMike Christie shost->transportt = scsit; 12307996a778SMike Christie shost->transportt->create_work_queue = 1; 12317996a778SMike Christie *hostno = shost->host_no; 12327996a778SMike Christie 12337996a778SMike Christie session = iscsi_hostdata(shost->hostdata); 12347996a778SMike Christie memset(session, 0, sizeof(struct iscsi_session)); 12357996a778SMike Christie session->host = shost; 12367996a778SMike Christie session->state = ISCSI_STATE_FREE; 12377996a778SMike Christie session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX; 12387996a778SMike Christie session->cmds_max = ISCSI_XMIT_CMDS_MAX; 12397996a778SMike Christie session->cmdsn = initial_cmdsn; 12407996a778SMike Christie session->exp_cmdsn = initial_cmdsn + 1; 12417996a778SMike Christie session->max_cmdsn = initial_cmdsn + 1; 12427996a778SMike Christie session->max_r2t = 1; 12437996a778SMike Christie session->tt = iscsit; 12447996a778SMike Christie 12457996a778SMike Christie /* initialize SCSI PDU commands pool */ 12467996a778SMike Christie if (iscsi_pool_init(&session->cmdpool, session->cmds_max, 12477996a778SMike Christie (void***)&session->cmds, 12487996a778SMike Christie cmd_task_size + sizeof(struct iscsi_cmd_task))) 12497996a778SMike Christie goto cmdpool_alloc_fail; 12507996a778SMike Christie 12517996a778SMike Christie /* pre-format cmds pool with ITT */ 12527996a778SMike Christie for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { 12537996a778SMike Christie struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; 12547996a778SMike Christie 12557996a778SMike Christie if (cmd_task_size) 12567996a778SMike Christie ctask->dd_data = &ctask[1]; 12577996a778SMike Christie ctask->itt = cmd_i; 1258b6c395edSMike Christie INIT_LIST_HEAD(&ctask->running); 12597996a778SMike Christie } 12607996a778SMike Christie 12617996a778SMike Christie spin_lock_init(&session->lock); 12627996a778SMike Christie INIT_LIST_HEAD(&session->connections); 12637996a778SMike Christie 12647996a778SMike Christie /* initialize immediate command pool */ 12657996a778SMike Christie if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max, 12667996a778SMike Christie (void***)&session->mgmt_cmds, 12677996a778SMike Christie mgmt_task_size + sizeof(struct iscsi_mgmt_task))) 12687996a778SMike Christie goto mgmtpool_alloc_fail; 12697996a778SMike Christie 12707996a778SMike Christie 12717996a778SMike Christie /* pre-format immediate cmds pool with ITT */ 12727996a778SMike Christie for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) { 12737996a778SMike Christie struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i]; 12747996a778SMike Christie 12757996a778SMike Christie if (mgmt_task_size) 12767996a778SMike Christie mtask->dd_data = &mtask[1]; 12777996a778SMike Christie mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i; 1278b6c395edSMike Christie INIT_LIST_HEAD(&mtask->running); 12797996a778SMike Christie } 12807996a778SMike Christie 12817996a778SMike Christie if (scsi_add_host(shost, NULL)) 12827996a778SMike Christie goto add_host_fail; 12837996a778SMike Christie 1284f53a88daSMike Christie if (!try_module_get(iscsit->owner)) 1285f53a88daSMike Christie goto cls_session_fail; 1286f53a88daSMike Christie 12876a8a0d36SMike Christie cls_session = iscsi_create_session(shost, iscsit, 0); 12887996a778SMike Christie if (!cls_session) 1289f53a88daSMike Christie goto module_put; 12907996a778SMike Christie *(unsigned long*)shost->hostdata = (unsigned long)cls_session; 12917996a778SMike Christie 12927996a778SMike Christie return cls_session; 12937996a778SMike Christie 1294f53a88daSMike Christie module_put: 1295f53a88daSMike Christie module_put(iscsit->owner); 12967996a778SMike Christie cls_session_fail: 12977996a778SMike Christie scsi_remove_host(shost); 12987996a778SMike Christie add_host_fail: 12997996a778SMike Christie iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds); 13007996a778SMike Christie mgmtpool_alloc_fail: 13017996a778SMike Christie iscsi_pool_free(&session->cmdpool, (void**)session->cmds); 13027996a778SMike Christie cmdpool_alloc_fail: 13037996a778SMike Christie scsi_host_put(shost); 13047996a778SMike Christie return NULL; 13057996a778SMike Christie } 13067996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup); 13077996a778SMike Christie 13087996a778SMike Christie /** 13097996a778SMike Christie * iscsi_session_teardown - destroy session, host, and cls_session 13107996a778SMike Christie * shost: scsi host 13117996a778SMike Christie * 13127996a778SMike Christie * This can be used by software iscsi_transports that allocate 13137996a778SMike Christie * a session per scsi host. 13147996a778SMike Christie **/ 13157996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session) 13167996a778SMike Christie { 13177996a778SMike Christie struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); 13187996a778SMike Christie struct iscsi_session *session = iscsi_hostdata(shost->hostdata); 13197996a778SMike Christie 13207996a778SMike Christie scsi_remove_host(shost); 13217996a778SMike Christie 13227996a778SMike Christie iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds); 13237996a778SMike Christie iscsi_pool_free(&session->cmdpool, (void**)session->cmds); 13247996a778SMike Christie 13257996a778SMike Christie iscsi_destroy_session(cls_session); 13267996a778SMike Christie scsi_host_put(shost); 1327f53a88daSMike Christie module_put(cls_session->transport->owner); 13287996a778SMike Christie } 13297996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown); 13307996a778SMike Christie 13317996a778SMike Christie /** 13327996a778SMike Christie * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn 13337996a778SMike Christie * @cls_session: iscsi_cls_session 13347996a778SMike Christie * @conn_idx: cid 13357996a778SMike Christie **/ 13367996a778SMike Christie struct iscsi_cls_conn * 13377996a778SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx) 13387996a778SMike Christie { 13397996a778SMike Christie struct iscsi_session *session = class_to_transport_session(cls_session); 13407996a778SMike Christie struct iscsi_conn *conn; 13417996a778SMike Christie struct iscsi_cls_conn *cls_conn; 1342d36ab6f3SMike Christie char *data; 13437996a778SMike Christie 13447996a778SMike Christie cls_conn = iscsi_create_conn(cls_session, conn_idx); 13457996a778SMike Christie if (!cls_conn) 13467996a778SMike Christie return NULL; 13477996a778SMike Christie conn = cls_conn->dd_data; 13487996a778SMike Christie memset(conn, 0, sizeof(*conn)); 13497996a778SMike Christie 13507996a778SMike Christie conn->session = session; 13517996a778SMike Christie conn->cls_conn = cls_conn; 13527996a778SMike Christie conn->c_stage = ISCSI_CONN_INITIAL_STAGE; 13537996a778SMike Christie conn->id = conn_idx; 13547996a778SMike Christie conn->exp_statsn = 0; 13557996a778SMike Christie conn->tmabort_state = TMABORT_INITIAL; 13567996a778SMike Christie INIT_LIST_HEAD(&conn->run_list); 13577996a778SMike Christie INIT_LIST_HEAD(&conn->mgmt_run_list); 1358b6c395edSMike Christie INIT_LIST_HEAD(&conn->xmitqueue); 13597996a778SMike Christie 13607996a778SMike Christie /* initialize general immediate & non-immediate PDU commands queue */ 13617996a778SMike Christie conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*), 13627996a778SMike Christie GFP_KERNEL, NULL); 13637996a778SMike Christie if (conn->immqueue == ERR_PTR(-ENOMEM)) 13647996a778SMike Christie goto immqueue_alloc_fail; 13657996a778SMike Christie 13667996a778SMike Christie conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*), 13677996a778SMike Christie GFP_KERNEL, NULL); 13687996a778SMike Christie if (conn->mgmtqueue == ERR_PTR(-ENOMEM)) 13697996a778SMike Christie goto mgmtqueue_alloc_fail; 13707996a778SMike Christie 13717996a778SMike Christie INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn); 13727996a778SMike Christie 13737996a778SMike Christie /* allocate login_mtask used for the login/text sequences */ 13747996a778SMike Christie spin_lock_bh(&session->lock); 13757996a778SMike Christie if (!__kfifo_get(session->mgmtpool.queue, 13767996a778SMike Christie (void*)&conn->login_mtask, 13777996a778SMike Christie sizeof(void*))) { 13787996a778SMike Christie spin_unlock_bh(&session->lock); 13797996a778SMike Christie goto login_mtask_alloc_fail; 13807996a778SMike Christie } 13817996a778SMike Christie spin_unlock_bh(&session->lock); 13827996a778SMike Christie 1383d36ab6f3SMike Christie data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL); 1384d36ab6f3SMike Christie if (!data) 1385d36ab6f3SMike Christie goto login_mtask_data_alloc_fail; 1386d36ab6f3SMike Christie conn->login_mtask->data = data; 1387d36ab6f3SMike Christie 13887996a778SMike Christie init_timer(&conn->tmabort_timer); 13897996a778SMike Christie mutex_init(&conn->xmitmutex); 13907996a778SMike Christie init_waitqueue_head(&conn->ehwait); 13917996a778SMike Christie 13927996a778SMike Christie return cls_conn; 13937996a778SMike Christie 1394d36ab6f3SMike Christie login_mtask_data_alloc_fail: 1395d36ab6f3SMike Christie __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask, 1396d36ab6f3SMike Christie sizeof(void*)); 13977996a778SMike Christie login_mtask_alloc_fail: 13987996a778SMike Christie kfifo_free(conn->mgmtqueue); 13997996a778SMike Christie mgmtqueue_alloc_fail: 14007996a778SMike Christie kfifo_free(conn->immqueue); 14017996a778SMike Christie immqueue_alloc_fail: 14027996a778SMike Christie iscsi_destroy_conn(cls_conn); 14037996a778SMike Christie return NULL; 14047996a778SMike Christie } 14057996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup); 14067996a778SMike Christie 14077996a778SMike Christie /** 14087996a778SMike Christie * iscsi_conn_teardown - teardown iscsi connection 14097996a778SMike Christie * cls_conn: iscsi class connection 14107996a778SMike Christie * 14117996a778SMike Christie * TODO: we may need to make this into a two step process 14127996a778SMike Christie * like scsi-mls remove + put host 14137996a778SMike Christie */ 14147996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) 14157996a778SMike Christie { 14167996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 14177996a778SMike Christie struct iscsi_session *session = conn->session; 14187996a778SMike Christie unsigned long flags; 14197996a778SMike Christie 14207996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 142167a61114SMike Christie mutex_lock(&conn->xmitmutex); 14227996a778SMike Christie if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE) { 14237996a778SMike Christie if (session->tt->suspend_conn_recv) 14247996a778SMike Christie session->tt->suspend_conn_recv(conn); 14257996a778SMike Christie 14267996a778SMike Christie session->tt->terminate_conn(conn); 14277996a778SMike Christie } 14287996a778SMike Christie 14297996a778SMike Christie spin_lock_bh(&session->lock); 14307996a778SMike Christie conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; 14317996a778SMike Christie if (session->leadconn == conn) { 14327996a778SMike Christie /* 14337996a778SMike Christie * leading connection? then give up on recovery. 14347996a778SMike Christie */ 14357996a778SMike Christie session->state = ISCSI_STATE_TERMINATE; 14367996a778SMike Christie wake_up(&conn->ehwait); 14377996a778SMike Christie } 14387996a778SMike Christie spin_unlock_bh(&session->lock); 14397996a778SMike Christie 14407996a778SMike Christie mutex_unlock(&conn->xmitmutex); 14417996a778SMike Christie 14427996a778SMike Christie /* 14437996a778SMike Christie * Block until all in-progress commands for this connection 14447996a778SMike Christie * time out or fail. 14457996a778SMike Christie */ 14467996a778SMike Christie for (;;) { 14477996a778SMike Christie spin_lock_irqsave(session->host->host_lock, flags); 14487996a778SMike Christie if (!session->host->host_busy) { /* OK for ERL == 0 */ 14497996a778SMike Christie spin_unlock_irqrestore(session->host->host_lock, flags); 14507996a778SMike Christie break; 14517996a778SMike Christie } 14527996a778SMike Christie spin_unlock_irqrestore(session->host->host_lock, flags); 14537996a778SMike Christie msleep_interruptible(500); 1454be2df72eSOr Gerlitz printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d " 1455be2df72eSOr Gerlitz "host_failed %d\n", session->host->host_busy, 1456be2df72eSOr Gerlitz session->host->host_failed); 14577996a778SMike Christie /* 14587996a778SMike Christie * force eh_abort() to unblock 14597996a778SMike Christie */ 14607996a778SMike Christie wake_up(&conn->ehwait); 14617996a778SMike Christie } 14627996a778SMike Christie 14637996a778SMike Christie spin_lock_bh(&session->lock); 1464d36ab6f3SMike Christie kfree(conn->login_mtask->data); 14657996a778SMike Christie __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask, 14667996a778SMike Christie sizeof(void*)); 14677996a778SMike Christie list_del(&conn->item); 14687996a778SMike Christie if (list_empty(&session->connections)) 14697996a778SMike Christie session->leadconn = NULL; 14707996a778SMike Christie if (session->leadconn && session->leadconn == conn) 14717996a778SMike Christie session->leadconn = container_of(session->connections.next, 14727996a778SMike Christie struct iscsi_conn, item); 14737996a778SMike Christie 14747996a778SMike Christie if (session->leadconn == NULL) 14757996a778SMike Christie /* no connections exits.. reset sequencing */ 14767996a778SMike Christie session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1; 14777996a778SMike Christie spin_unlock_bh(&session->lock); 14787996a778SMike Christie 14797996a778SMike Christie kfifo_free(conn->immqueue); 14807996a778SMike Christie kfifo_free(conn->mgmtqueue); 14817996a778SMike Christie 14827996a778SMike Christie iscsi_destroy_conn(cls_conn); 14837996a778SMike Christie } 14847996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown); 14857996a778SMike Christie 14867996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) 14877996a778SMike Christie { 14887996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 14897996a778SMike Christie struct iscsi_session *session = conn->session; 14907996a778SMike Christie 14917996a778SMike Christie if (session == NULL) { 14927996a778SMike Christie printk(KERN_ERR "iscsi: can't start unbound connection\n"); 14937996a778SMike Christie return -EPERM; 14947996a778SMike Christie } 14957996a778SMike Christie 14967996a778SMike Christie spin_lock_bh(&session->lock); 14977996a778SMike Christie conn->c_stage = ISCSI_CONN_STARTED; 14987996a778SMike Christie session->state = ISCSI_STATE_LOGGED_IN; 14997996a778SMike Christie 15007996a778SMike Christie switch(conn->stop_stage) { 15017996a778SMike Christie case STOP_CONN_RECOVER: 15027996a778SMike Christie /* 15037996a778SMike Christie * unblock eh_abort() if it is blocked. re-try all 15047996a778SMike Christie * commands after successful recovery 15057996a778SMike Christie */ 15067996a778SMike Christie conn->stop_stage = 0; 15077996a778SMike Christie conn->tmabort_state = TMABORT_INITIAL; 15087996a778SMike Christie session->age++; 15097996a778SMike Christie spin_unlock_bh(&session->lock); 15107996a778SMike Christie 15117996a778SMike Christie iscsi_unblock_session(session_to_cls(session)); 15127996a778SMike Christie wake_up(&conn->ehwait); 15137996a778SMike Christie return 0; 15147996a778SMike Christie case STOP_CONN_TERM: 15157996a778SMike Christie conn->stop_stage = 0; 15167996a778SMike Christie break; 15177996a778SMike Christie default: 15187996a778SMike Christie break; 15197996a778SMike Christie } 15207996a778SMike Christie spin_unlock_bh(&session->lock); 15217996a778SMike Christie 15227996a778SMike Christie return 0; 15237996a778SMike Christie } 15247996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start); 15257996a778SMike Christie 15267996a778SMike Christie static void 15277996a778SMike Christie flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn) 15287996a778SMike Christie { 15297996a778SMike Christie struct iscsi_mgmt_task *mtask, *tmp; 15307996a778SMike Christie 15317996a778SMike Christie /* handle pending */ 15327996a778SMike Christie while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) || 15337996a778SMike Christie __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) { 15347996a778SMike Christie if (mtask == conn->login_mtask) 15357996a778SMike Christie continue; 15367996a778SMike Christie debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt); 15377996a778SMike Christie __kfifo_put(session->mgmtpool.queue, (void*)&mtask, 15387996a778SMike Christie sizeof(void*)); 15397996a778SMike Christie } 15407996a778SMike Christie 15417996a778SMike Christie /* handle running */ 15427996a778SMike Christie list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) { 15437996a778SMike Christie debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt); 1544ed2abc7fSMike Christie list_del(&mtask->running); 1545ed2abc7fSMike Christie 15467996a778SMike Christie if (mtask == conn->login_mtask) 15477996a778SMike Christie continue; 1548ed2abc7fSMike Christie __kfifo_put(session->mgmtpool.queue, (void*)&mtask, 15497996a778SMike Christie sizeof(void*)); 15507996a778SMike Christie } 15517996a778SMike Christie 15527996a778SMike Christie conn->mtask = NULL; 15537996a778SMike Christie } 15547996a778SMike Christie 15557996a778SMike Christie /* Fail commands. Mutex and session lock held and recv side suspended */ 15567996a778SMike Christie static void fail_all_commands(struct iscsi_conn *conn) 15577996a778SMike Christie { 15587996a778SMike Christie struct iscsi_cmd_task *ctask, *tmp; 15597996a778SMike Christie 15607996a778SMike Christie /* flush pending */ 1561b6c395edSMike Christie list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) { 15627996a778SMike Christie debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc, 15637996a778SMike Christie ctask->itt); 15647996a778SMike Christie fail_command(conn, ctask, DID_BUS_BUSY << 16); 15657996a778SMike Christie } 15667996a778SMike Christie 15677996a778SMike Christie /* fail all other running */ 15687996a778SMike Christie list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) { 15697996a778SMike Christie debug_scsi("failing in progress sc %p itt 0x%x\n", 15707996a778SMike Christie ctask->sc, ctask->itt); 15717996a778SMike Christie fail_command(conn, ctask, DID_BUS_BUSY << 16); 15727996a778SMike Christie } 15737996a778SMike Christie 15747996a778SMike Christie conn->ctask = NULL; 15757996a778SMike Christie } 15767996a778SMike Christie 1577656cffc9SMike Christie static void iscsi_start_session_recovery(struct iscsi_session *session, 15787996a778SMike Christie struct iscsi_conn *conn, int flag) 15797996a778SMike Christie { 1580ed2abc7fSMike Christie int old_stop_stage; 1581ed2abc7fSMike Christie 15827996a778SMike Christie spin_lock_bh(&session->lock); 1583ed2abc7fSMike Christie if (conn->stop_stage == STOP_CONN_TERM) { 15847996a778SMike Christie spin_unlock_bh(&session->lock); 15857996a778SMike Christie return; 15867996a778SMike Christie } 1587ed2abc7fSMike Christie 1588ed2abc7fSMike Christie /* 1589ed2abc7fSMike Christie * When this is called for the in_login state, we only want to clean 159067a61114SMike Christie * up the login task and connection. We do not need to block and set 159167a61114SMike Christie * the recovery state again 1592ed2abc7fSMike Christie */ 159367a61114SMike Christie if (flag == STOP_CONN_TERM) 159467a61114SMike Christie session->state = ISCSI_STATE_TERMINATE; 159567a61114SMike Christie else if (conn->stop_stage != STOP_CONN_RECOVER) 159667a61114SMike Christie session->state = ISCSI_STATE_IN_RECOVERY; 1597ed2abc7fSMike Christie 1598ed2abc7fSMike Christie old_stop_stage = conn->stop_stage; 15997996a778SMike Christie conn->stop_stage = flag; 160067a61114SMike Christie conn->c_stage = ISCSI_CONN_STOPPED; 160167a61114SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 16027996a778SMike Christie spin_unlock_bh(&session->lock); 16037996a778SMike Christie 16047996a778SMike Christie if (session->tt->suspend_conn_recv) 16057996a778SMike Christie session->tt->suspend_conn_recv(conn); 16067996a778SMike Christie 16077996a778SMike Christie mutex_lock(&conn->xmitmutex); 16087996a778SMike Christie /* 16097996a778SMike Christie * for connection level recovery we should not calculate 16107996a778SMike Christie * header digest. conn->hdr_size used for optimization 16117996a778SMike Christie * in hdr_extract() and will be re-negotiated at 16127996a778SMike Christie * set_param() time. 16137996a778SMike Christie */ 16147996a778SMike Christie if (flag == STOP_CONN_RECOVER) { 16157996a778SMike Christie conn->hdrdgst_en = 0; 16167996a778SMike Christie conn->datadgst_en = 0; 1617656cffc9SMike Christie if (session->state == ISCSI_STATE_IN_RECOVERY && 161867a61114SMike Christie old_stop_stage != STOP_CONN_RECOVER) { 161967a61114SMike Christie debug_scsi("blocking session\n"); 16207996a778SMike Christie iscsi_block_session(session_to_cls(session)); 16217996a778SMike Christie } 162267a61114SMike Christie } 1623656cffc9SMike Christie 1624656cffc9SMike Christie session->tt->terminate_conn(conn); 1625656cffc9SMike Christie /* 1626656cffc9SMike Christie * flush queues. 1627656cffc9SMike Christie */ 1628656cffc9SMike Christie spin_lock_bh(&session->lock); 1629656cffc9SMike Christie fail_all_commands(conn); 1630656cffc9SMike Christie flush_control_queues(session, conn); 1631656cffc9SMike Christie spin_unlock_bh(&session->lock); 1632656cffc9SMike Christie 16337996a778SMike Christie mutex_unlock(&conn->xmitmutex); 16347996a778SMike Christie } 16357996a778SMike Christie 16367996a778SMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) 16377996a778SMike Christie { 16387996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 16397996a778SMike Christie struct iscsi_session *session = conn->session; 16407996a778SMike Christie 16417996a778SMike Christie switch (flag) { 16427996a778SMike Christie case STOP_CONN_RECOVER: 16437996a778SMike Christie case STOP_CONN_TERM: 16447996a778SMike Christie iscsi_start_session_recovery(session, conn, flag); 16458d2860b3SMike Christie break; 16467996a778SMike Christie default: 1647be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag); 16487996a778SMike Christie } 16497996a778SMike Christie } 16507996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop); 16517996a778SMike Christie 16527996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session, 16537996a778SMike Christie struct iscsi_cls_conn *cls_conn, int is_leading) 16547996a778SMike Christie { 16557996a778SMike Christie struct iscsi_session *session = class_to_transport_session(cls_session); 16567996a778SMike Christie struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data; 16577996a778SMike Christie 16587996a778SMike Christie /* lookup for existing connection */ 16597996a778SMike Christie spin_lock_bh(&session->lock); 16607996a778SMike Christie list_for_each_entry(tmp, &session->connections, item) { 16617996a778SMike Christie if (tmp == conn) { 16627996a778SMike Christie if (conn->c_stage != ISCSI_CONN_STOPPED || 16637996a778SMike Christie conn->stop_stage == STOP_CONN_TERM) { 1664be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: can't bind " 16657996a778SMike Christie "non-stopped connection (%d:%d)\n", 16667996a778SMike Christie conn->c_stage, conn->stop_stage); 16677996a778SMike Christie spin_unlock_bh(&session->lock); 16687996a778SMike Christie return -EIO; 16697996a778SMike Christie } 16707996a778SMike Christie break; 16717996a778SMike Christie } 16727996a778SMike Christie } 16737996a778SMike Christie if (tmp != conn) { 16747996a778SMike Christie /* bind new iSCSI connection to session */ 16757996a778SMike Christie conn->session = session; 16767996a778SMike Christie list_add(&conn->item, &session->connections); 16777996a778SMike Christie } 16787996a778SMike Christie spin_unlock_bh(&session->lock); 16797996a778SMike Christie 16807996a778SMike Christie if (is_leading) 16817996a778SMike Christie session->leadconn = conn; 16827996a778SMike Christie 16837996a778SMike Christie /* 16847996a778SMike Christie * Unblock xmitworker(), Login Phase will pass through. 16857996a778SMike Christie */ 16867996a778SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 16877996a778SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 16887996a778SMike Christie return 0; 16897996a778SMike Christie } 16907996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind); 16917996a778SMike Christie 1692a54a52caSMike Christie 1693a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn, 1694a54a52caSMike Christie enum iscsi_param param, char *buf, int buflen) 1695a54a52caSMike Christie { 1696a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 1697a54a52caSMike Christie struct iscsi_session *session = conn->session; 1698a54a52caSMike Christie uint32_t value; 1699a54a52caSMike Christie 1700a54a52caSMike Christie switch(param) { 1701a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 1702a54a52caSMike Christie sscanf(buf, "%d", &conn->max_recv_dlength); 1703a54a52caSMike Christie break; 1704a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 1705a54a52caSMike Christie sscanf(buf, "%d", &conn->max_xmit_dlength); 1706a54a52caSMike Christie break; 1707a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 1708a54a52caSMike Christie sscanf(buf, "%d", &conn->hdrdgst_en); 1709a54a52caSMike Christie break; 1710a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 1711a54a52caSMike Christie sscanf(buf, "%d", &conn->datadgst_en); 1712a54a52caSMike Christie break; 1713a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 1714a54a52caSMike Christie sscanf(buf, "%d", &session->initial_r2t_en); 1715a54a52caSMike Christie break; 1716a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 1717a54a52caSMike Christie sscanf(buf, "%d", &session->max_r2t); 1718a54a52caSMike Christie break; 1719a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 1720a54a52caSMike Christie sscanf(buf, "%d", &session->imm_data_en); 1721a54a52caSMike Christie break; 1722a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 1723a54a52caSMike Christie sscanf(buf, "%d", &session->first_burst); 1724a54a52caSMike Christie break; 1725a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 1726a54a52caSMike Christie sscanf(buf, "%d", &session->max_burst); 1727a54a52caSMike Christie break; 1728a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 1729a54a52caSMike Christie sscanf(buf, "%d", &session->pdu_inorder_en); 1730a54a52caSMike Christie break; 1731a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 1732a54a52caSMike Christie sscanf(buf, "%d", &session->dataseq_inorder_en); 1733a54a52caSMike Christie break; 1734a54a52caSMike Christie case ISCSI_PARAM_ERL: 1735a54a52caSMike Christie sscanf(buf, "%d", &session->erl); 1736a54a52caSMike Christie break; 1737a54a52caSMike Christie case ISCSI_PARAM_IFMARKER_EN: 1738a54a52caSMike Christie sscanf(buf, "%d", &value); 1739a54a52caSMike Christie BUG_ON(value); 1740a54a52caSMike Christie break; 1741a54a52caSMike Christie case ISCSI_PARAM_OFMARKER_EN: 1742a54a52caSMike Christie sscanf(buf, "%d", &value); 1743a54a52caSMike Christie BUG_ON(value); 1744a54a52caSMike Christie break; 1745a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 1746a54a52caSMike Christie sscanf(buf, "%u", &conn->exp_statsn); 1747a54a52caSMike Christie break; 1748a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 1749a54a52caSMike Christie /* this should not change between logins */ 1750a54a52caSMike Christie if (session->targetname) 1751a54a52caSMike Christie break; 1752a54a52caSMike Christie 1753a54a52caSMike Christie session->targetname = kstrdup(buf, GFP_KERNEL); 1754a54a52caSMike Christie if (!session->targetname) 1755a54a52caSMike Christie return -ENOMEM; 1756a54a52caSMike Christie break; 1757a54a52caSMike Christie case ISCSI_PARAM_TPGT: 1758a54a52caSMike Christie sscanf(buf, "%d", &session->tpgt); 1759a54a52caSMike Christie break; 1760a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 1761a54a52caSMike Christie sscanf(buf, "%d", &conn->persistent_port); 1762a54a52caSMike Christie break; 1763a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 1764a54a52caSMike Christie /* 1765a54a52caSMike Christie * this is the address returned in discovery so it should 1766a54a52caSMike Christie * not change between logins. 1767a54a52caSMike Christie */ 1768a54a52caSMike Christie if (conn->persistent_address) 1769a54a52caSMike Christie break; 1770a54a52caSMike Christie 1771a54a52caSMike Christie conn->persistent_address = kstrdup(buf, GFP_KERNEL); 1772a54a52caSMike Christie if (!conn->persistent_address) 1773a54a52caSMike Christie return -ENOMEM; 1774a54a52caSMike Christie break; 1775a54a52caSMike Christie default: 1776a54a52caSMike Christie return -ENOSYS; 1777a54a52caSMike Christie } 1778a54a52caSMike Christie 1779a54a52caSMike Christie return 0; 1780a54a52caSMike Christie } 1781a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param); 1782a54a52caSMike Christie 1783a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session, 1784a54a52caSMike Christie enum iscsi_param param, char *buf) 1785a54a52caSMike Christie { 1786a54a52caSMike Christie struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); 1787a54a52caSMike Christie struct iscsi_session *session = iscsi_hostdata(shost->hostdata); 1788a54a52caSMike Christie int len; 1789a54a52caSMike Christie 1790a54a52caSMike Christie switch(param) { 1791a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 1792a54a52caSMike Christie len = sprintf(buf, "%d\n", session->initial_r2t_en); 1793a54a52caSMike Christie break; 1794a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 1795a54a52caSMike Christie len = sprintf(buf, "%hu\n", session->max_r2t); 1796a54a52caSMike Christie break; 1797a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 1798a54a52caSMike Christie len = sprintf(buf, "%d\n", session->imm_data_en); 1799a54a52caSMike Christie break; 1800a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 1801a54a52caSMike Christie len = sprintf(buf, "%u\n", session->first_burst); 1802a54a52caSMike Christie break; 1803a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 1804a54a52caSMike Christie len = sprintf(buf, "%u\n", session->max_burst); 1805a54a52caSMike Christie break; 1806a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 1807a54a52caSMike Christie len = sprintf(buf, "%d\n", session->pdu_inorder_en); 1808a54a52caSMike Christie break; 1809a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 1810a54a52caSMike Christie len = sprintf(buf, "%d\n", session->dataseq_inorder_en); 1811a54a52caSMike Christie break; 1812a54a52caSMike Christie case ISCSI_PARAM_ERL: 1813a54a52caSMike Christie len = sprintf(buf, "%d\n", session->erl); 1814a54a52caSMike Christie break; 1815a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 1816a54a52caSMike Christie len = sprintf(buf, "%s\n", session->targetname); 1817a54a52caSMike Christie break; 1818a54a52caSMike Christie case ISCSI_PARAM_TPGT: 1819a54a52caSMike Christie len = sprintf(buf, "%d\n", session->tpgt); 1820a54a52caSMike Christie break; 1821a54a52caSMike Christie default: 1822a54a52caSMike Christie return -ENOSYS; 1823a54a52caSMike Christie } 1824a54a52caSMike Christie 1825a54a52caSMike Christie return len; 1826a54a52caSMike Christie } 1827a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param); 1828a54a52caSMike Christie 1829a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, 1830a54a52caSMike Christie enum iscsi_param param, char *buf) 1831a54a52caSMike Christie { 1832a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 1833a54a52caSMike Christie int len; 1834a54a52caSMike Christie 1835a54a52caSMike Christie switch(param) { 1836a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 1837a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->max_recv_dlength); 1838a54a52caSMike Christie break; 1839a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 1840a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->max_xmit_dlength); 1841a54a52caSMike Christie break; 1842a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 1843a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->hdrdgst_en); 1844a54a52caSMike Christie break; 1845a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 1846a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->datadgst_en); 1847a54a52caSMike Christie break; 1848a54a52caSMike Christie case ISCSI_PARAM_IFMARKER_EN: 1849a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->ifmarker_en); 1850a54a52caSMike Christie break; 1851a54a52caSMike Christie case ISCSI_PARAM_OFMARKER_EN: 1852a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->ofmarker_en); 1853a54a52caSMike Christie break; 1854a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 1855a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->exp_statsn); 1856a54a52caSMike Christie break; 1857a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 1858a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->persistent_port); 1859a54a52caSMike Christie break; 1860a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 1861a54a52caSMike Christie len = sprintf(buf, "%s\n", conn->persistent_address); 1862a54a52caSMike Christie break; 1863a54a52caSMike Christie default: 1864a54a52caSMike Christie return -ENOSYS; 1865a54a52caSMike Christie } 1866a54a52caSMike Christie 1867a54a52caSMike Christie return len; 1868a54a52caSMike Christie } 1869a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param); 1870a54a52caSMike Christie 18717996a778SMike Christie MODULE_AUTHOR("Mike Christie"); 18727996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions"); 18737996a778SMike Christie MODULE_LICENSE("GPL"); 1874