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 1927996a778SMike Christie ctask->sc = NULL; 1937996a778SMike Christie list_del_init(&ctask->running); 1947996a778SMike Christie __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*)); 1957996a778SMike Christie sc->scsi_done(sc); 1967996a778SMike Christie } 1977996a778SMike Christie 1987996a778SMike Christie /** 1997996a778SMike Christie * iscsi_cmd_rsp - SCSI Command Response processing 2007996a778SMike Christie * @conn: iscsi connection 2017996a778SMike Christie * @hdr: iscsi header 2027996a778SMike Christie * @ctask: scsi command task 2037996a778SMike Christie * @data: cmd data buffer 2047996a778SMike Christie * @datalen: len of buffer 2057996a778SMike Christie * 2067996a778SMike Christie * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and 2077996a778SMike Christie * then completes the command and task. 2087996a778SMike Christie **/ 2097996a778SMike Christie static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 2107996a778SMike Christie struct iscsi_cmd_task *ctask, char *data, 2117996a778SMike Christie int datalen) 2127996a778SMike Christie { 2137996a778SMike Christie int rc; 2147996a778SMike Christie struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr; 2157996a778SMike Christie struct iscsi_session *session = conn->session; 2167996a778SMike Christie struct scsi_cmnd *sc = ctask->sc; 2177996a778SMike Christie 2187996a778SMike Christie rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr); 2197996a778SMike Christie if (rc) { 2207996a778SMike Christie sc->result = DID_ERROR << 16; 2217996a778SMike Christie goto out; 2227996a778SMike Christie } 2237996a778SMike Christie 2247996a778SMike Christie conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; 2257996a778SMike Christie 2267996a778SMike Christie sc->result = (DID_OK << 16) | rhdr->cmd_status; 2277996a778SMike Christie 2287996a778SMike Christie if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { 2297996a778SMike Christie sc->result = DID_ERROR << 16; 2307996a778SMike Christie goto out; 2317996a778SMike Christie } 2327996a778SMike Christie 2337996a778SMike Christie if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { 2347996a778SMike Christie int senselen; 2357996a778SMike Christie 2367996a778SMike Christie if (datalen < 2) { 2377996a778SMike Christie invalid_datalen: 238be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: Got CHECK_CONDITION but " 239be2df72eSOr Gerlitz "invalid data buffer size of %d\n", datalen); 2407996a778SMike Christie sc->result = DID_BAD_TARGET << 16; 2417996a778SMike Christie goto out; 2427996a778SMike Christie } 2437996a778SMike Christie 2447996a778SMike Christie senselen = (data[0] << 8) | data[1]; 2457996a778SMike Christie if (datalen < senselen) 2467996a778SMike Christie goto invalid_datalen; 2477996a778SMike Christie 2487996a778SMike Christie memcpy(sc->sense_buffer, data + 2, 2497996a778SMike Christie min(senselen, SCSI_SENSE_BUFFERSIZE)); 2507996a778SMike Christie debug_scsi("copied %d bytes of sense\n", 2517996a778SMike Christie min(senselen, SCSI_SENSE_BUFFERSIZE)); 2527996a778SMike Christie } 2537996a778SMike Christie 2547996a778SMike Christie if (sc->sc_data_direction == DMA_TO_DEVICE) 2557996a778SMike Christie goto out; 2567996a778SMike Christie 2577996a778SMike Christie if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) { 2587996a778SMike Christie int res_count = be32_to_cpu(rhdr->residual_count); 2597996a778SMike Christie 2607996a778SMike Christie if (res_count > 0 && res_count <= sc->request_bufflen) 2617996a778SMike Christie sc->resid = res_count; 2627996a778SMike Christie else 2637996a778SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 2647996a778SMike Christie } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW) 2657996a778SMike Christie sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; 2667996a778SMike Christie else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW) 2677996a778SMike Christie sc->resid = be32_to_cpu(rhdr->residual_count); 2687996a778SMike Christie 2697996a778SMike Christie out: 2707996a778SMike Christie debug_scsi("done [sc %lx res %d itt 0x%x]\n", 2717996a778SMike Christie (long)sc, sc->result, ctask->itt); 2727996a778SMike Christie conn->scsirsp_pdus_cnt++; 2737996a778SMike Christie 2747996a778SMike Christie iscsi_complete_command(conn->session, ctask); 2757996a778SMike Christie return rc; 2767996a778SMike Christie } 2777996a778SMike Christie 2787996a778SMike Christie /** 2797996a778SMike Christie * __iscsi_complete_pdu - complete pdu 2807996a778SMike Christie * @conn: iscsi conn 2817996a778SMike Christie * @hdr: iscsi header 2827996a778SMike Christie * @data: data buffer 2837996a778SMike Christie * @datalen: len of data buffer 2847996a778SMike Christie * 2857996a778SMike Christie * Completes pdu processing by freeing any resources allocated at 2867996a778SMike Christie * queuecommand or send generic. session lock must be held and verify 2877996a778SMike Christie * itt must have been called. 2887996a778SMike Christie */ 2897996a778SMike Christie int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 2907996a778SMike Christie char *data, int datalen) 2917996a778SMike Christie { 2927996a778SMike Christie struct iscsi_session *session = conn->session; 2937996a778SMike Christie int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; 2947996a778SMike Christie struct iscsi_cmd_task *ctask; 2957996a778SMike Christie struct iscsi_mgmt_task *mtask; 2967996a778SMike Christie uint32_t itt; 2977996a778SMike Christie 2987996a778SMike Christie if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) 2997996a778SMike Christie itt = hdr->itt & ISCSI_ITT_MASK; 3007996a778SMike Christie else 3017996a778SMike Christie itt = hdr->itt; 3027996a778SMike Christie 3037996a778SMike Christie if (itt < session->cmds_max) { 3047996a778SMike Christie ctask = session->cmds[itt]; 3057996a778SMike Christie 3067996a778SMike Christie debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n", 3077996a778SMike Christie opcode, conn->id, ctask->itt, datalen); 3087996a778SMike Christie 3097996a778SMike Christie switch(opcode) { 3107996a778SMike Christie case ISCSI_OP_SCSI_CMD_RSP: 3117996a778SMike Christie BUG_ON((void*)ctask != ctask->sc->SCp.ptr); 3127996a778SMike Christie rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data, 3137996a778SMike Christie datalen); 3147996a778SMike Christie break; 3157996a778SMike Christie case ISCSI_OP_SCSI_DATA_IN: 3167996a778SMike Christie BUG_ON((void*)ctask != ctask->sc->SCp.ptr); 3177996a778SMike Christie if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { 3187996a778SMike Christie conn->scsirsp_pdus_cnt++; 3197996a778SMike Christie iscsi_complete_command(session, ctask); 3207996a778SMike Christie } 3217996a778SMike Christie break; 3227996a778SMike Christie case ISCSI_OP_R2T: 3237996a778SMike Christie /* LLD handles this for now */ 3247996a778SMike Christie break; 3257996a778SMike Christie default: 3267996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 3277996a778SMike Christie break; 3287996a778SMike Christie } 3297996a778SMike Christie } else if (itt >= ISCSI_MGMT_ITT_OFFSET && 3307996a778SMike Christie itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) { 3317996a778SMike Christie mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET]; 3327996a778SMike Christie 3337996a778SMike Christie debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n", 3347996a778SMike Christie opcode, conn->id, mtask->itt, datalen); 3357996a778SMike Christie 3367996a778SMike Christie rc = iscsi_check_assign_cmdsn(session, 3377996a778SMike Christie (struct iscsi_nopin*)hdr); 3387996a778SMike Christie if (rc) 3398d2860b3SMike Christie goto done; 3407996a778SMike Christie 3418d2860b3SMike Christie switch(opcode) { 3428d2860b3SMike Christie case ISCSI_OP_LOGOUT_RSP: 3438d2860b3SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 3448d2860b3SMike Christie /* fall through */ 3458d2860b3SMike Christie case ISCSI_OP_LOGIN_RSP: 3468d2860b3SMike Christie case ISCSI_OP_TEXT_RSP: 3478d2860b3SMike Christie /* 3488d2860b3SMike Christie * login related PDU's exp_statsn is handled in 3498d2860b3SMike Christie * userspace 3508d2860b3SMike Christie */ 3517996a778SMike Christie rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen); 3527996a778SMike Christie list_del(&mtask->running); 3537996a778SMike Christie if (conn->login_mtask != mtask) 3547996a778SMike Christie __kfifo_put(session->mgmtpool.queue, 3557996a778SMike Christie (void*)&mtask, sizeof(void*)); 3567996a778SMike Christie break; 3577996a778SMike Christie case ISCSI_OP_SCSI_TMFUNC_RSP: 3587996a778SMike Christie if (datalen) { 3597996a778SMike Christie rc = ISCSI_ERR_PROTO; 3607996a778SMike Christie break; 3617996a778SMike Christie } 3628d2860b3SMike Christie 3638d2860b3SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 3647996a778SMike Christie conn->tmfrsp_pdus_cnt++; 3657996a778SMike Christie if (conn->tmabort_state == TMABORT_INITIAL) { 3667996a778SMike Christie conn->tmabort_state = 3677996a778SMike Christie ((struct iscsi_tm_rsp *)hdr)-> 3687996a778SMike Christie response == ISCSI_TMF_RSP_COMPLETE ? 3697996a778SMike Christie TMABORT_SUCCESS:TMABORT_FAILED; 3707996a778SMike Christie /* unblock eh_abort() */ 3717996a778SMike Christie wake_up(&conn->ehwait); 3727996a778SMike Christie } 3737996a778SMike Christie break; 3747996a778SMike Christie case ISCSI_OP_NOOP_IN: 3757996a778SMike Christie if (hdr->ttt != ISCSI_RESERVED_TAG) { 3767996a778SMike Christie rc = ISCSI_ERR_PROTO; 3777996a778SMike Christie break; 3787996a778SMike Christie } 3797996a778SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 3807996a778SMike Christie 3817996a778SMike Christie rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen); 3827996a778SMike Christie list_del(&mtask->running); 3837996a778SMike Christie if (conn->login_mtask != mtask) 3847996a778SMike Christie __kfifo_put(session->mgmtpool.queue, 3857996a778SMike Christie (void*)&mtask, sizeof(void*)); 3867996a778SMike Christie break; 3877996a778SMike Christie default: 3887996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 3897996a778SMike Christie break; 3907996a778SMike Christie } 3917996a778SMike Christie } else if (itt == ISCSI_RESERVED_TAG) { 3927996a778SMike Christie switch(opcode) { 3937996a778SMike Christie case ISCSI_OP_NOOP_IN: 3947996a778SMike Christie if (!datalen) { 3957996a778SMike Christie rc = iscsi_check_assign_cmdsn(session, 3967996a778SMike Christie (struct iscsi_nopin*)hdr); 3977996a778SMike Christie if (!rc && hdr->ttt != ISCSI_RESERVED_TAG) 3987996a778SMike Christie rc = iscsi_recv_pdu(conn->cls_conn, 3997996a778SMike Christie hdr, NULL, 0); 4007996a778SMike Christie } else 4017996a778SMike Christie rc = ISCSI_ERR_PROTO; 4027996a778SMike Christie break; 4037996a778SMike Christie case ISCSI_OP_REJECT: 4047996a778SMike Christie /* we need sth like iscsi_reject_rsp()*/ 4057996a778SMike Christie case ISCSI_OP_ASYNC_EVENT: 4068d2860b3SMike Christie conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 4077996a778SMike Christie /* we need sth like iscsi_async_event_rsp() */ 4087996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 4097996a778SMike Christie break; 4107996a778SMike Christie default: 4117996a778SMike Christie rc = ISCSI_ERR_BAD_OPCODE; 4127996a778SMike Christie break; 4137996a778SMike Christie } 4147996a778SMike Christie } else 4157996a778SMike Christie rc = ISCSI_ERR_BAD_ITT; 4167996a778SMike Christie 4178d2860b3SMike Christie done: 4187996a778SMike Christie return rc; 4197996a778SMike Christie } 4207996a778SMike Christie EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); 4217996a778SMike Christie 4227996a778SMike Christie int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 4237996a778SMike Christie char *data, int datalen) 4247996a778SMike Christie { 4257996a778SMike Christie int rc; 4267996a778SMike Christie 4277996a778SMike Christie spin_lock(&conn->session->lock); 4287996a778SMike Christie rc = __iscsi_complete_pdu(conn, hdr, data, datalen); 4297996a778SMike Christie spin_unlock(&conn->session->lock); 4307996a778SMike Christie return rc; 4317996a778SMike Christie } 4327996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_complete_pdu); 4337996a778SMike Christie 4347996a778SMike Christie /* verify itt (itt encoding: age+cid+itt) */ 4357996a778SMike Christie int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 4367996a778SMike Christie uint32_t *ret_itt) 4377996a778SMike Christie { 4387996a778SMike Christie struct iscsi_session *session = conn->session; 4397996a778SMike Christie struct iscsi_cmd_task *ctask; 4407996a778SMike Christie uint32_t itt; 4417996a778SMike Christie 4427996a778SMike Christie if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) { 4437996a778SMike Christie if ((hdr->itt & ISCSI_AGE_MASK) != 4447996a778SMike Christie (session->age << ISCSI_AGE_SHIFT)) { 445be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: received itt %x expected " 4467996a778SMike Christie "session age (%x)\n", hdr->itt, 4477996a778SMike Christie session->age & ISCSI_AGE_MASK); 4487996a778SMike Christie return ISCSI_ERR_BAD_ITT; 4497996a778SMike Christie } 4507996a778SMike Christie 4517996a778SMike Christie if ((hdr->itt & ISCSI_CID_MASK) != 4527996a778SMike Christie (conn->id << ISCSI_CID_SHIFT)) { 453be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: received itt %x, expected " 4547996a778SMike Christie "CID (%x)\n", hdr->itt, conn->id); 4557996a778SMike Christie return ISCSI_ERR_BAD_ITT; 4567996a778SMike Christie } 4577996a778SMike Christie itt = hdr->itt & ISCSI_ITT_MASK; 4587996a778SMike Christie } else 4597996a778SMike Christie itt = hdr->itt; 4607996a778SMike Christie 4617996a778SMike Christie if (itt < session->cmds_max) { 4627996a778SMike Christie ctask = session->cmds[itt]; 4637996a778SMike Christie 4647996a778SMike Christie if (!ctask->sc) { 465be2df72eSOr Gerlitz printk(KERN_INFO "iscsi: dropping ctask with " 4667996a778SMike Christie "itt 0x%x\n", ctask->itt); 4677996a778SMike Christie /* force drop */ 4687996a778SMike Christie return ISCSI_ERR_NO_SCSI_CMD; 4697996a778SMike Christie } 4707996a778SMike Christie 4717996a778SMike Christie if (ctask->sc->SCp.phase != session->age) { 472be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: ctask's session age %d, " 4737996a778SMike Christie "expected %d\n", ctask->sc->SCp.phase, 4747996a778SMike Christie session->age); 4757996a778SMike Christie return ISCSI_ERR_SESSION_FAILED; 4767996a778SMike Christie } 4777996a778SMike Christie } 4787996a778SMike Christie 4797996a778SMike Christie *ret_itt = itt; 4807996a778SMike Christie return 0; 4817996a778SMike Christie } 4827996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_verify_itt); 4837996a778SMike Christie 4847996a778SMike Christie void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) 4857996a778SMike Christie { 4867996a778SMike Christie struct iscsi_session *session = conn->session; 4877996a778SMike Christie unsigned long flags; 4887996a778SMike Christie 4897996a778SMike Christie spin_lock_irqsave(&session->lock, flags); 490656cffc9SMike Christie if (session->state == ISCSI_STATE_FAILED) { 491656cffc9SMike Christie spin_unlock_irqrestore(&session->lock, flags); 492656cffc9SMike Christie return; 493656cffc9SMike Christie } 494656cffc9SMike Christie 49567a61114SMike Christie if (conn->stop_stage == 0) 4967996a778SMike Christie session->state = ISCSI_STATE_FAILED; 4977996a778SMike Christie spin_unlock_irqrestore(&session->lock, flags); 4987996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 4997996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 5007996a778SMike Christie iscsi_conn_error(conn->cls_conn, err); 5017996a778SMike Christie } 5027996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_failure); 5037996a778SMike Christie 5047996a778SMike Christie /** 5057996a778SMike Christie * iscsi_data_xmit - xmit any command into the scheduled connection 5067996a778SMike Christie * @conn: iscsi connection 5077996a778SMike Christie * 5087996a778SMike Christie * Notes: 5097996a778SMike Christie * The function can return -EAGAIN in which case the caller must 5107996a778SMike Christie * re-schedule it again later or recover. '0' return code means 5117996a778SMike Christie * successful xmit. 5127996a778SMike Christie **/ 5137996a778SMike Christie static int iscsi_data_xmit(struct iscsi_conn *conn) 5147996a778SMike Christie { 5157996a778SMike Christie struct iscsi_transport *tt; 5163219e529SMike Christie int rc = 0; 5177996a778SMike Christie 5187996a778SMike Christie if (unlikely(conn->suspend_tx)) { 5197996a778SMike Christie debug_scsi("conn %d Tx suspended!\n", conn->id); 5203219e529SMike Christie return -ENODATA; 5217996a778SMike Christie } 5227996a778SMike Christie tt = conn->session->tt; 5237996a778SMike Christie 5247996a778SMike Christie /* 5257996a778SMike Christie * Transmit in the following order: 5267996a778SMike Christie * 5277996a778SMike Christie * 1) un-finished xmit (ctask or mtask) 5287996a778SMike Christie * 2) immediate control PDUs 5297996a778SMike Christie * 3) write data 5307996a778SMike Christie * 4) SCSI commands 5317996a778SMike Christie * 5) non-immediate control PDUs 5327996a778SMike Christie * 5337996a778SMike Christie * No need to lock around __kfifo_get as long as 5347996a778SMike Christie * there's one producer and one consumer. 5357996a778SMike Christie */ 5367996a778SMike Christie 5377996a778SMike Christie BUG_ON(conn->ctask && conn->mtask); 5387996a778SMike Christie 5397996a778SMike Christie if (conn->ctask) { 5403219e529SMike Christie rc = tt->xmit_cmd_task(conn, conn->ctask); 5413219e529SMike Christie if (rc) 5427996a778SMike Christie goto again; 5437996a778SMike Christie /* done with this in-progress ctask */ 5447996a778SMike Christie conn->ctask = NULL; 5457996a778SMike Christie } 5467996a778SMike Christie if (conn->mtask) { 5473219e529SMike Christie rc = tt->xmit_mgmt_task(conn, conn->mtask); 5483219e529SMike Christie if (rc) 5497996a778SMike Christie goto again; 5507996a778SMike Christie /* done with this in-progress mtask */ 5517996a778SMike Christie conn->mtask = NULL; 5527996a778SMike Christie } 5537996a778SMike Christie 5547996a778SMike Christie /* process immediate first */ 5557996a778SMike Christie if (unlikely(__kfifo_len(conn->immqueue))) { 5567996a778SMike Christie while (__kfifo_get(conn->immqueue, (void*)&conn->mtask, 5577996a778SMike Christie sizeof(void*))) { 558994442e8SMike Christie spin_lock_bh(&conn->session->lock); 5597996a778SMike Christie list_add_tail(&conn->mtask->running, 5607996a778SMike Christie &conn->mgmt_run_list); 561994442e8SMike Christie spin_unlock_bh(&conn->session->lock); 5623219e529SMike Christie rc = tt->xmit_mgmt_task(conn, conn->mtask); 5633219e529SMike Christie if (rc) 5647996a778SMike Christie goto again; 5657996a778SMike Christie } 5667996a778SMike Christie /* done with this mtask */ 5677996a778SMike Christie conn->mtask = NULL; 5687996a778SMike Christie } 5697996a778SMike Christie 5707996a778SMike Christie /* process command queue */ 5717996a778SMike Christie while (__kfifo_get(conn->xmitqueue, (void*)&conn->ctask, 5727996a778SMike Christie sizeof(void*))) { 5737996a778SMike Christie /* 5747996a778SMike Christie * iscsi tcp may readd the task to the xmitqueue to send 5757996a778SMike Christie * write data 5767996a778SMike Christie */ 577994442e8SMike Christie spin_lock_bh(&conn->session->lock); 5787996a778SMike Christie if (list_empty(&conn->ctask->running)) 5797996a778SMike Christie list_add_tail(&conn->ctask->running, &conn->run_list); 580994442e8SMike Christie spin_unlock_bh(&conn->session->lock); 5813219e529SMike Christie rc = tt->xmit_cmd_task(conn, conn->ctask); 5823219e529SMike Christie if (rc) 5837996a778SMike Christie goto again; 5847996a778SMike Christie } 5857996a778SMike Christie /* done with this ctask */ 5867996a778SMike Christie conn->ctask = NULL; 5877996a778SMike Christie 5887996a778SMike Christie /* process the rest control plane PDUs, if any */ 5897996a778SMike Christie if (unlikely(__kfifo_len(conn->mgmtqueue))) { 5907996a778SMike Christie while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask, 5917996a778SMike Christie sizeof(void*))) { 592994442e8SMike Christie spin_lock_bh(&conn->session->lock); 5937996a778SMike Christie list_add_tail(&conn->mtask->running, 5947996a778SMike Christie &conn->mgmt_run_list); 595994442e8SMike Christie spin_unlock_bh(&conn->session->lock); 5963219e529SMike Christie rc = tt->xmit_mgmt_task(conn, conn->mtask); 5973219e529SMike Christie if (rc) 5987996a778SMike Christie goto again; 5997996a778SMike Christie } 6007996a778SMike Christie /* done with this mtask */ 6017996a778SMike Christie conn->mtask = NULL; 6027996a778SMike Christie } 6037996a778SMike Christie 6043219e529SMike Christie return -ENODATA; 6057996a778SMike Christie 6067996a778SMike Christie again: 6077996a778SMike Christie if (unlikely(conn->suspend_tx)) 6083219e529SMike Christie return -ENODATA; 6097996a778SMike Christie 6103219e529SMike Christie return rc; 6117996a778SMike Christie } 6127996a778SMike Christie 6137996a778SMike Christie static void iscsi_xmitworker(void *data) 6147996a778SMike Christie { 6157996a778SMike Christie struct iscsi_conn *conn = data; 6163219e529SMike Christie int rc; 6177996a778SMike Christie /* 6187996a778SMike Christie * serialize Xmit worker on a per-connection basis. 6197996a778SMike Christie */ 6207996a778SMike Christie mutex_lock(&conn->xmitmutex); 6213219e529SMike Christie do { 6223219e529SMike Christie rc = iscsi_data_xmit(conn); 6233219e529SMike Christie } while (rc >= 0 || rc == -EAGAIN); 6247996a778SMike Christie mutex_unlock(&conn->xmitmutex); 6257996a778SMike Christie } 6267996a778SMike Christie 6277996a778SMike Christie enum { 6287996a778SMike Christie FAILURE_BAD_HOST = 1, 6297996a778SMike Christie FAILURE_SESSION_FAILED, 6307996a778SMike Christie FAILURE_SESSION_FREED, 6317996a778SMike Christie FAILURE_WINDOW_CLOSED, 6327996a778SMike Christie FAILURE_SESSION_TERMINATE, 633656cffc9SMike Christie FAILURE_SESSION_IN_RECOVERY, 6347996a778SMike Christie FAILURE_SESSION_RECOVERY_TIMEOUT, 6357996a778SMike Christie }; 6367996a778SMike Christie 6377996a778SMike Christie int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) 6387996a778SMike Christie { 6397996a778SMike Christie struct Scsi_Host *host; 6407996a778SMike Christie int reason = 0; 6417996a778SMike Christie struct iscsi_session *session; 6427996a778SMike Christie struct iscsi_conn *conn; 6437996a778SMike Christie struct iscsi_cmd_task *ctask = NULL; 6447996a778SMike Christie 6457996a778SMike Christie sc->scsi_done = done; 6467996a778SMike Christie sc->result = 0; 6477996a778SMike Christie 6487996a778SMike Christie host = sc->device->host; 6497996a778SMike Christie session = iscsi_hostdata(host->hostdata); 6507996a778SMike Christie 6517996a778SMike Christie spin_lock(&session->lock); 6527996a778SMike Christie 653656cffc9SMike Christie /* 654656cffc9SMike Christie * ISCSI_STATE_FAILED is a temp. state. The recovery 655656cffc9SMike Christie * code will decide what is best to do with command queued 656656cffc9SMike Christie * during this time 657656cffc9SMike Christie */ 658656cffc9SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN && 659656cffc9SMike Christie session->state != ISCSI_STATE_FAILED) { 660656cffc9SMike Christie /* 661656cffc9SMike Christie * to handle the race between when we set the recovery state 662656cffc9SMike Christie * and block the session we requeue here (commands could 663656cffc9SMike Christie * be entering our queuecommand while a block is starting 664656cffc9SMike Christie * up because the block code is not locked) 665656cffc9SMike Christie */ 666656cffc9SMike Christie if (session->state == ISCSI_STATE_IN_RECOVERY) { 667656cffc9SMike Christie reason = FAILURE_SESSION_IN_RECOVERY; 66867a61114SMike Christie goto reject; 6697996a778SMike Christie } 670656cffc9SMike Christie 671656cffc9SMike Christie if (session->state == ISCSI_STATE_RECOVERY_FAILED) 672656cffc9SMike Christie reason = FAILURE_SESSION_RECOVERY_TIMEOUT; 673656cffc9SMike Christie else if (session->state == ISCSI_STATE_TERMINATE) 674656cffc9SMike Christie reason = FAILURE_SESSION_TERMINATE; 675656cffc9SMike Christie else 6767996a778SMike Christie reason = FAILURE_SESSION_FREED; 6777996a778SMike Christie goto fault; 6787996a778SMike Christie } 6797996a778SMike Christie 6807996a778SMike Christie /* 6817996a778SMike Christie * Check for iSCSI window and take care of CmdSN wrap-around 6827996a778SMike Christie */ 6837996a778SMike Christie if ((int)(session->max_cmdsn - session->cmdsn) < 0) { 6847996a778SMike Christie reason = FAILURE_WINDOW_CLOSED; 6857996a778SMike Christie goto reject; 6867996a778SMike Christie } 6877996a778SMike Christie 6887996a778SMike Christie conn = session->leadconn; 6897996a778SMike Christie 6907996a778SMike Christie __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*)); 6917996a778SMike Christie sc->SCp.phase = session->age; 6927996a778SMike Christie sc->SCp.ptr = (char *)ctask; 6937996a778SMike Christie 6947996a778SMike Christie ctask->mtask = NULL; 6957996a778SMike Christie ctask->conn = conn; 6967996a778SMike Christie ctask->sc = sc; 6977996a778SMike Christie INIT_LIST_HEAD(&ctask->running); 6987996a778SMike Christie ctask->total_length = sc->request_bufflen; 6997996a778SMike Christie iscsi_prep_scsi_cmd_pdu(ctask); 7007996a778SMike Christie 7017996a778SMike Christie session->tt->init_cmd_task(ctask); 7027996a778SMike Christie 7037996a778SMike Christie __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*)); 7047996a778SMike Christie debug_scsi( 7057996a778SMike Christie "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n", 7067996a778SMike Christie sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read", 7077996a778SMike Christie conn->id, (long)sc, ctask->itt, sc->request_bufflen, 7087996a778SMike Christie session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1); 7097996a778SMike Christie spin_unlock(&session->lock); 7107996a778SMike Christie 7117996a778SMike Christie scsi_queue_work(host, &conn->xmitwork); 7127996a778SMike Christie return 0; 7137996a778SMike Christie 7147996a778SMike Christie reject: 7157996a778SMike Christie spin_unlock(&session->lock); 7167996a778SMike Christie debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason); 7177996a778SMike Christie return SCSI_MLQUEUE_HOST_BUSY; 7187996a778SMike Christie 7197996a778SMike Christie fault: 7207996a778SMike Christie spin_unlock(&session->lock); 721be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n", 7227996a778SMike Christie sc->cmnd[0], reason); 7237996a778SMike Christie sc->result = (DID_NO_CONNECT << 16); 7247996a778SMike Christie sc->resid = sc->request_bufflen; 7257996a778SMike Christie sc->scsi_done(sc); 7267996a778SMike Christie return 0; 7277996a778SMike Christie } 7287996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_queuecommand); 7297996a778SMike Christie 7307996a778SMike Christie int iscsi_change_queue_depth(struct scsi_device *sdev, int depth) 7317996a778SMike Christie { 7327996a778SMike Christie if (depth > ISCSI_MAX_CMD_PER_LUN) 7337996a778SMike Christie depth = ISCSI_MAX_CMD_PER_LUN; 7347996a778SMike Christie scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); 7357996a778SMike Christie return sdev->queue_depth; 7367996a778SMike Christie } 7377996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_change_queue_depth); 7387996a778SMike Christie 7397996a778SMike Christie static int 7407996a778SMike Christie iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr, 7417996a778SMike Christie char *data, uint32_t data_size) 7427996a778SMike Christie { 7437996a778SMike Christie struct iscsi_session *session = conn->session; 7447996a778SMike Christie struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; 7457996a778SMike Christie struct iscsi_mgmt_task *mtask; 7467996a778SMike Christie 7477996a778SMike Christie spin_lock_bh(&session->lock); 7487996a778SMike Christie if (session->state == ISCSI_STATE_TERMINATE) { 7497996a778SMike Christie spin_unlock_bh(&session->lock); 7507996a778SMike Christie return -EPERM; 7517996a778SMike Christie } 7527996a778SMike Christie if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) || 7537996a778SMike Christie hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) 7547996a778SMike Christie /* 7557996a778SMike Christie * Login and Text are sent serially, in 7567996a778SMike Christie * request-followed-by-response sequence. 7577996a778SMike Christie * Same mtask can be used. Same ITT must be used. 7587996a778SMike Christie * Note that login_mtask is preallocated at conn_create(). 7597996a778SMike Christie */ 7607996a778SMike Christie mtask = conn->login_mtask; 7617996a778SMike Christie else { 7627996a778SMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); 7637996a778SMike Christie BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); 7647996a778SMike Christie 7658d2860b3SMike Christie nop->exp_statsn = cpu_to_be32(conn->exp_statsn); 7667996a778SMike Christie if (!__kfifo_get(session->mgmtpool.queue, 7677996a778SMike Christie (void*)&mtask, sizeof(void*))) { 7687996a778SMike Christie spin_unlock_bh(&session->lock); 7697996a778SMike Christie return -ENOSPC; 7707996a778SMike Christie } 7717996a778SMike Christie } 7727996a778SMike Christie 7737996a778SMike Christie /* 7748d2860b3SMike Christie * pre-format CmdSN for outgoing PDU. 7757996a778SMike Christie */ 7767996a778SMike Christie if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) { 7777996a778SMike Christie hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) | 7787996a778SMike Christie (session->age << ISCSI_AGE_SHIFT); 7797996a778SMike Christie nop->cmdsn = cpu_to_be32(session->cmdsn); 7807996a778SMike Christie if (conn->c_stage == ISCSI_CONN_STARTED && 7817996a778SMike Christie !(hdr->opcode & ISCSI_OP_IMMEDIATE)) 7827996a778SMike Christie session->cmdsn++; 7837996a778SMike Christie } else 7847996a778SMike Christie /* do not advance CmdSN */ 7857996a778SMike Christie nop->cmdsn = cpu_to_be32(session->cmdsn); 7867996a778SMike Christie 7877996a778SMike Christie if (data_size) { 7887996a778SMike Christie memcpy(mtask->data, data, data_size); 7897996a778SMike Christie mtask->data_count = data_size; 7907996a778SMike Christie } else 7917996a778SMike Christie mtask->data_count = 0; 7927996a778SMike Christie 7937996a778SMike Christie INIT_LIST_HEAD(&mtask->running); 7947996a778SMike Christie memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr)); 7957996a778SMike Christie if (session->tt->init_mgmt_task) 7967996a778SMike Christie session->tt->init_mgmt_task(conn, mtask, data, data_size); 7977996a778SMike Christie spin_unlock_bh(&session->lock); 7987996a778SMike Christie 7997996a778SMike Christie debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n", 8007996a778SMike Christie hdr->opcode, hdr->itt, data_size); 8017996a778SMike Christie 8027996a778SMike Christie /* 8037996a778SMike Christie * since send_pdu() could be called at least from two contexts, 8047996a778SMike Christie * we need to serialize __kfifo_put, so we don't have to take 8057996a778SMike Christie * additional lock on fast data-path 8067996a778SMike Christie */ 8077996a778SMike Christie if (hdr->opcode & ISCSI_OP_IMMEDIATE) 8087996a778SMike Christie __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*)); 8097996a778SMike Christie else 8107996a778SMike Christie __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*)); 8117996a778SMike Christie 8127996a778SMike Christie scsi_queue_work(session->host, &conn->xmitwork); 8137996a778SMike Christie return 0; 8147996a778SMike Christie } 8157996a778SMike Christie 8167996a778SMike Christie int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, 8177996a778SMike Christie char *data, uint32_t data_size) 8187996a778SMike Christie { 8197996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 8207996a778SMike Christie int rc; 8217996a778SMike Christie 8227996a778SMike Christie mutex_lock(&conn->xmitmutex); 8237996a778SMike Christie rc = iscsi_conn_send_generic(conn, hdr, data, data_size); 8247996a778SMike Christie mutex_unlock(&conn->xmitmutex); 8257996a778SMike Christie 8267996a778SMike Christie return rc; 8277996a778SMike Christie } 8287996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); 8297996a778SMike Christie 8307996a778SMike Christie void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) 8317996a778SMike Christie { 8327996a778SMike Christie struct iscsi_session *session = class_to_transport_session(cls_session); 8337996a778SMike Christie struct iscsi_conn *conn = session->leadconn; 8347996a778SMike Christie 8357996a778SMike Christie spin_lock_bh(&session->lock); 8367996a778SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN) { 837656cffc9SMike Christie session->state = ISCSI_STATE_RECOVERY_FAILED; 8387996a778SMike Christie if (conn) 8397996a778SMike Christie wake_up(&conn->ehwait); 8407996a778SMike Christie } 8417996a778SMike Christie spin_unlock_bh(&session->lock); 8427996a778SMike Christie } 8437996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); 8447996a778SMike Christie 8457996a778SMike Christie int iscsi_eh_host_reset(struct scsi_cmnd *sc) 8467996a778SMike Christie { 8477996a778SMike Christie struct Scsi_Host *host = sc->device->host; 8487996a778SMike Christie struct iscsi_session *session = iscsi_hostdata(host->hostdata); 8497996a778SMike Christie struct iscsi_conn *conn = session->leadconn; 8507996a778SMike Christie int fail_session = 0; 8517996a778SMike Christie 8527996a778SMike Christie spin_lock_bh(&session->lock); 8537996a778SMike Christie if (session->state == ISCSI_STATE_TERMINATE) { 8547996a778SMike Christie failed: 8557996a778SMike Christie debug_scsi("failing host reset: session terminated " 8567996a778SMike Christie "[CID %d age %d]", conn->id, session->age); 8577996a778SMike Christie spin_unlock_bh(&session->lock); 8587996a778SMike Christie return FAILED; 8597996a778SMike Christie } 8607996a778SMike Christie 8617996a778SMike Christie if (sc->SCp.phase == session->age) { 8627996a778SMike Christie debug_scsi("failing connection CID %d due to SCSI host reset", 8637996a778SMike Christie conn->id); 8647996a778SMike Christie fail_session = 1; 8657996a778SMike Christie } 8667996a778SMike Christie spin_unlock_bh(&session->lock); 8677996a778SMike Christie 8687996a778SMike Christie /* 8697996a778SMike Christie * we drop the lock here but the leadconn cannot be destoyed while 8707996a778SMike Christie * we are in the scsi eh 8717996a778SMike Christie */ 872656cffc9SMike Christie if (fail_session) 8737996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 8747996a778SMike Christie 8757996a778SMike Christie debug_scsi("iscsi_eh_host_reset wait for relogin\n"); 8767996a778SMike Christie wait_event_interruptible(conn->ehwait, 8777996a778SMike Christie session->state == ISCSI_STATE_TERMINATE || 8787996a778SMike Christie session->state == ISCSI_STATE_LOGGED_IN || 879656cffc9SMike Christie session->state == ISCSI_STATE_RECOVERY_FAILED); 8807996a778SMike Christie if (signal_pending(current)) 8817996a778SMike Christie flush_signals(current); 8827996a778SMike Christie 8837996a778SMike Christie spin_lock_bh(&session->lock); 8847996a778SMike Christie if (session->state == ISCSI_STATE_LOGGED_IN) 885be2df72eSOr Gerlitz printk(KERN_INFO "iscsi: host reset succeeded\n"); 8867996a778SMike Christie else 8877996a778SMike Christie goto failed; 8887996a778SMike Christie spin_unlock_bh(&session->lock); 8897996a778SMike Christie 8907996a778SMike Christie return SUCCESS; 8917996a778SMike Christie } 8927996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_host_reset); 8937996a778SMike Christie 8947996a778SMike Christie static void iscsi_tmabort_timedout(unsigned long data) 8957996a778SMike Christie { 8967996a778SMike Christie struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data; 8977996a778SMike Christie struct iscsi_conn *conn = ctask->conn; 8987996a778SMike Christie struct iscsi_session *session = conn->session; 8997996a778SMike Christie 9007996a778SMike Christie spin_lock(&session->lock); 9017996a778SMike Christie if (conn->tmabort_state == TMABORT_INITIAL) { 9027996a778SMike Christie conn->tmabort_state = TMABORT_TIMEDOUT; 9037996a778SMike Christie debug_scsi("tmabort timedout [sc %p itt 0x%x]\n", 9047996a778SMike Christie ctask->sc, ctask->itt); 9057996a778SMike Christie /* unblock eh_abort() */ 9067996a778SMike Christie wake_up(&conn->ehwait); 9077996a778SMike Christie } 9087996a778SMike Christie spin_unlock(&session->lock); 9097996a778SMike Christie } 9107996a778SMike Christie 9117996a778SMike Christie /* must be called with the mutex lock */ 9127996a778SMike Christie static int iscsi_exec_abort_task(struct scsi_cmnd *sc, 9137996a778SMike Christie struct iscsi_cmd_task *ctask) 9147996a778SMike Christie { 9157996a778SMike Christie struct iscsi_conn *conn = ctask->conn; 9167996a778SMike Christie struct iscsi_session *session = conn->session; 9177996a778SMike Christie struct iscsi_tm *hdr = &conn->tmhdr; 9187996a778SMike Christie int rc; 9197996a778SMike Christie 9207996a778SMike Christie /* 9217996a778SMike Christie * ctask timed out but session is OK requests must be serialized. 9227996a778SMike Christie */ 9237996a778SMike Christie memset(hdr, 0, sizeof(struct iscsi_tm)); 9247996a778SMike Christie hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; 9257996a778SMike Christie hdr->flags = ISCSI_TM_FUNC_ABORT_TASK; 9267996a778SMike Christie hdr->flags |= ISCSI_FLAG_CMD_FINAL; 9277996a778SMike Christie memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); 9287996a778SMike Christie hdr->rtt = ctask->hdr->itt; 9297996a778SMike Christie hdr->refcmdsn = ctask->hdr->cmdsn; 9307996a778SMike Christie 9317996a778SMike Christie rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr, 9327996a778SMike Christie NULL, 0); 9337996a778SMike Christie if (rc) { 9347996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 9357996a778SMike Christie debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc); 9367996a778SMike Christie return rc; 9377996a778SMike Christie } 9387996a778SMike Christie 9397996a778SMike Christie debug_scsi("abort sent [itt 0x%x]\n", ctask->itt); 9407996a778SMike Christie 9417996a778SMike Christie spin_lock_bh(&session->lock); 9427996a778SMike Christie ctask->mtask = (struct iscsi_mgmt_task *) 9437996a778SMike Christie session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) - 9447996a778SMike Christie ISCSI_MGMT_ITT_OFFSET]; 9457996a778SMike Christie 9467996a778SMike Christie if (conn->tmabort_state == TMABORT_INITIAL) { 9477996a778SMike Christie conn->tmfcmd_pdus_cnt++; 9487996a778SMike Christie conn->tmabort_timer.expires = 10*HZ + jiffies; 9497996a778SMike Christie conn->tmabort_timer.function = iscsi_tmabort_timedout; 9507996a778SMike Christie conn->tmabort_timer.data = (unsigned long)ctask; 9517996a778SMike Christie add_timer(&conn->tmabort_timer); 9527996a778SMike Christie debug_scsi("abort set timeout [itt 0x%x]", ctask->itt); 9537996a778SMike Christie } 9547996a778SMike Christie spin_unlock_bh(&session->lock); 9557996a778SMike Christie mutex_unlock(&conn->xmitmutex); 9567996a778SMike Christie 9577996a778SMike Christie /* 9587996a778SMike Christie * block eh thread until: 9597996a778SMike Christie * 9607996a778SMike Christie * 1) abort response 9617996a778SMike Christie * 2) abort timeout 9627996a778SMike Christie * 3) session is terminated or restarted or userspace has 9637996a778SMike Christie * given up on recovery 9647996a778SMike Christie */ 9657996a778SMike Christie wait_event_interruptible(conn->ehwait, 9667996a778SMike Christie sc->SCp.phase != session->age || 9677996a778SMike Christie session->state != ISCSI_STATE_LOGGED_IN || 968656cffc9SMike Christie conn->tmabort_state != TMABORT_INITIAL); 9697996a778SMike Christie if (signal_pending(current)) 9707996a778SMike Christie flush_signals(current); 9717996a778SMike Christie del_timer_sync(&conn->tmabort_timer); 9727996a778SMike Christie 9737996a778SMike Christie mutex_lock(&conn->xmitmutex); 9747996a778SMike Christie return 0; 9757996a778SMike Christie } 9767996a778SMike Christie 9777996a778SMike Christie /* 9787996a778SMike Christie * xmit mutex and session lock must be held 9797996a778SMike Christie */ 9807996a778SMike Christie #define iscsi_remove_task(tasktype) \ 9817996a778SMike Christie static struct iscsi_##tasktype * \ 9827996a778SMike Christie iscsi_remove_##tasktype(struct kfifo *fifo, uint32_t itt) \ 9837996a778SMike Christie { \ 9847996a778SMike Christie int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*); \ 9857996a778SMike Christie struct iscsi_##tasktype *task; \ 9867996a778SMike Christie \ 9877996a778SMike Christie debug_scsi("searching %d tasks\n", nr_tasks); \ 9887996a778SMike Christie \ 9897996a778SMike Christie for (i = 0; i < nr_tasks; i++) { \ 9907996a778SMike Christie __kfifo_get(fifo, (void*)&task, sizeof(void*)); \ 9917996a778SMike Christie debug_scsi("check task %u\n", task->itt); \ 9927996a778SMike Christie \ 9937996a778SMike Christie if (task->itt == itt) { \ 9947996a778SMike Christie debug_scsi("matched task\n"); \ 995040515f5SMike Christie return task; \ 9967996a778SMike Christie } \ 9977996a778SMike Christie \ 9987996a778SMike Christie __kfifo_put(fifo, (void*)&task, sizeof(void*)); \ 9997996a778SMike Christie } \ 10007996a778SMike Christie return NULL; \ 10017996a778SMike Christie } 10027996a778SMike Christie 10037996a778SMike Christie iscsi_remove_task(mgmt_task); 10047996a778SMike Christie iscsi_remove_task(cmd_task); 10057996a778SMike Christie 10067996a778SMike Christie static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask) 10077996a778SMike Christie { 10087996a778SMike Christie struct iscsi_conn *conn = ctask->conn; 10097996a778SMike Christie struct iscsi_session *session = conn->session; 10107996a778SMike Christie 10117996a778SMike Christie if (!ctask->mtask) 10127996a778SMike Christie return -EINVAL; 10137996a778SMike Christie 10147996a778SMike Christie if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt)) 10157996a778SMike Christie list_del(&ctask->mtask->running); 10167996a778SMike Christie __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask, 10177996a778SMike Christie sizeof(void*)); 10187996a778SMike Christie ctask->mtask = NULL; 10197996a778SMike Christie return 0; 10207996a778SMike Christie } 10217996a778SMike Christie 10227996a778SMike Christie /* 10237996a778SMike Christie * session lock and xmitmutex must be held 10247996a778SMike Christie */ 10257996a778SMike Christie static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, 10267996a778SMike Christie int err) 10277996a778SMike Christie { 10287996a778SMike Christie struct scsi_cmnd *sc; 10297996a778SMike Christie 10307996a778SMike Christie conn->session->tt->cleanup_cmd_task(conn, ctask); 10317996a778SMike Christie iscsi_ctask_mtask_cleanup(ctask); 10327996a778SMike Christie 10337996a778SMike Christie sc = ctask->sc; 10347996a778SMike Christie if (!sc) 10357996a778SMike Christie return; 10367996a778SMike Christie sc->result = err; 10377996a778SMike Christie sc->resid = sc->request_bufflen; 10387996a778SMike Christie iscsi_complete_command(conn->session, ctask); 10397996a778SMike Christie } 10407996a778SMike Christie 10417996a778SMike Christie int iscsi_eh_abort(struct scsi_cmnd *sc) 10427996a778SMike Christie { 10437996a778SMike Christie struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr; 10447996a778SMike Christie struct iscsi_conn *conn = ctask->conn; 10457996a778SMike Christie struct iscsi_session *session = conn->session; 10467996a778SMike Christie struct iscsi_cmd_task *pending_ctask; 10477996a778SMike Christie int rc; 10487996a778SMike Christie 10497996a778SMike Christie conn->eh_abort_cnt++; 10507996a778SMike Christie debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt); 10517996a778SMike Christie 10527996a778SMike Christie mutex_lock(&conn->xmitmutex); 10537996a778SMike Christie spin_lock_bh(&session->lock); 10547996a778SMike Christie 10557996a778SMike Christie /* 10567996a778SMike Christie * If we are not logged in or we have started a new session 10577996a778SMike Christie * then let the host reset code handle this 10587996a778SMike Christie */ 10597996a778SMike Christie if (session->state != ISCSI_STATE_LOGGED_IN || 10607996a778SMike Christie sc->SCp.phase != session->age) 10617996a778SMike Christie goto failed; 10627996a778SMike Christie 10637996a778SMike Christie /* ctask completed before time out */ 10647996a778SMike Christie if (!ctask->sc) 10657996a778SMike Christie goto success; 10667996a778SMike Christie 10677996a778SMike Christie /* what should we do here ? */ 10687996a778SMike Christie if (conn->ctask == ctask) { 1069be2df72eSOr Gerlitz printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. " 1070be2df72eSOr Gerlitz "Failing abort\n", sc, ctask->itt); 10717996a778SMike Christie goto failed; 10727996a778SMike Christie } 10737996a778SMike Christie 10747996a778SMike Christie /* check for the easy pending cmd abort */ 10757996a778SMike Christie pending_ctask = iscsi_remove_cmd_task(conn->xmitqueue, ctask->itt); 10767996a778SMike Christie if (pending_ctask) { 10777996a778SMike Christie /* iscsi_tcp queues write transfers on the xmitqueue */ 10787996a778SMike Christie if (list_empty(&pending_ctask->running)) { 10797996a778SMike Christie debug_scsi("found pending task\n"); 10807996a778SMike Christie goto success; 10817996a778SMike Christie } else 10827996a778SMike Christie __kfifo_put(conn->xmitqueue, (void*)&pending_ctask, 10837996a778SMike Christie sizeof(void*)); 10847996a778SMike Christie } 10857996a778SMike Christie 10867996a778SMike Christie conn->tmabort_state = TMABORT_INITIAL; 10877996a778SMike Christie 10887996a778SMike Christie spin_unlock_bh(&session->lock); 10897996a778SMike Christie rc = iscsi_exec_abort_task(sc, ctask); 10907996a778SMike Christie spin_lock_bh(&session->lock); 10917996a778SMike Christie 10927996a778SMike Christie iscsi_ctask_mtask_cleanup(ctask); 10937996a778SMike Christie if (rc || sc->SCp.phase != session->age || 10947996a778SMike Christie session->state != ISCSI_STATE_LOGGED_IN) 10957996a778SMike Christie goto failed; 10967996a778SMike Christie 10977996a778SMike Christie /* ctask completed before tmf abort response */ 10987996a778SMike Christie if (!ctask->sc) { 10997996a778SMike Christie debug_scsi("sc completed while abort in progress\n"); 11007996a778SMike Christie goto success; 11017996a778SMike Christie } 11027996a778SMike Christie 11037996a778SMike Christie if (conn->tmabort_state != TMABORT_SUCCESS) { 11047996a778SMike Christie spin_unlock_bh(&session->lock); 11057996a778SMike Christie iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 11067996a778SMike Christie spin_lock_bh(&session->lock); 11077996a778SMike Christie goto failed; 11087996a778SMike Christie } 11097996a778SMike Christie 11107996a778SMike Christie success: 11117996a778SMike Christie debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt); 11127996a778SMike Christie spin_unlock_bh(&session->lock); 11137996a778SMike Christie 11147996a778SMike Christie /* 11157996a778SMike Christie * clean up task if aborted. we have the xmitmutex so grab 11167996a778SMike Christie * the recv lock as a writer 11177996a778SMike Christie */ 11187996a778SMike Christie write_lock_bh(conn->recv_lock); 11197996a778SMike Christie spin_lock(&session->lock); 11207996a778SMike Christie fail_command(conn, ctask, DID_ABORT << 16); 11217996a778SMike Christie spin_unlock(&session->lock); 11227996a778SMike Christie write_unlock_bh(conn->recv_lock); 11237996a778SMike Christie 11247996a778SMike Christie mutex_unlock(&conn->xmitmutex); 11257996a778SMike Christie return SUCCESS; 11267996a778SMike Christie 11277996a778SMike Christie failed: 11287996a778SMike Christie spin_unlock_bh(&session->lock); 11297996a778SMike Christie mutex_unlock(&conn->xmitmutex); 11307996a778SMike Christie 11317996a778SMike Christie debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt); 11327996a778SMike Christie return FAILED; 11337996a778SMike Christie } 11347996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_eh_abort); 11357996a778SMike Christie 11367996a778SMike Christie int 11377996a778SMike Christie iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size) 11387996a778SMike Christie { 11397996a778SMike Christie int i; 11407996a778SMike Christie 11417996a778SMike Christie *items = kmalloc(max * sizeof(void*), GFP_KERNEL); 11427996a778SMike Christie if (*items == NULL) 11437996a778SMike Christie return -ENOMEM; 11447996a778SMike Christie 11457996a778SMike Christie q->max = max; 11467996a778SMike Christie q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL); 11477996a778SMike Christie if (q->pool == NULL) { 11487996a778SMike Christie kfree(*items); 11497996a778SMike Christie return -ENOMEM; 11507996a778SMike Christie } 11517996a778SMike Christie 11527996a778SMike Christie q->queue = kfifo_init((void*)q->pool, max * sizeof(void*), 11537996a778SMike Christie GFP_KERNEL, NULL); 11547996a778SMike Christie if (q->queue == ERR_PTR(-ENOMEM)) { 11557996a778SMike Christie kfree(q->pool); 11567996a778SMike Christie kfree(*items); 11577996a778SMike Christie return -ENOMEM; 11587996a778SMike Christie } 11597996a778SMike Christie 11607996a778SMike Christie for (i = 0; i < max; i++) { 11617996a778SMike Christie q->pool[i] = kmalloc(item_size, GFP_KERNEL); 11627996a778SMike Christie if (q->pool[i] == NULL) { 11637996a778SMike Christie int j; 11647996a778SMike Christie 11657996a778SMike Christie for (j = 0; j < i; j++) 11667996a778SMike Christie kfree(q->pool[j]); 11677996a778SMike Christie 11687996a778SMike Christie kfifo_free(q->queue); 11697996a778SMike Christie kfree(q->pool); 11707996a778SMike Christie kfree(*items); 11717996a778SMike Christie return -ENOMEM; 11727996a778SMike Christie } 11737996a778SMike Christie memset(q->pool[i], 0, item_size); 11747996a778SMike Christie (*items)[i] = q->pool[i]; 11757996a778SMike Christie __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*)); 11767996a778SMike Christie } 11777996a778SMike Christie return 0; 11787996a778SMike Christie } 11797996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_init); 11807996a778SMike Christie 11817996a778SMike Christie void iscsi_pool_free(struct iscsi_queue *q, void **items) 11827996a778SMike Christie { 11837996a778SMike Christie int i; 11847996a778SMike Christie 11857996a778SMike Christie for (i = 0; i < q->max; i++) 11867996a778SMike Christie kfree(items[i]); 11877996a778SMike Christie kfree(q->pool); 11887996a778SMike Christie kfree(items); 11897996a778SMike Christie } 11907996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_pool_free); 11917996a778SMike Christie 11927996a778SMike Christie /* 11937996a778SMike Christie * iSCSI Session's hostdata organization: 11947996a778SMike Christie * 11957996a778SMike Christie * *------------------* <== hostdata_session(host->hostdata) 11967996a778SMike Christie * | ptr to class sess| 11977996a778SMike Christie * |------------------| <== iscsi_hostdata(host->hostdata) 11987996a778SMike Christie * | iscsi_session | 11997996a778SMike Christie * *------------------* 12007996a778SMike Christie */ 12017996a778SMike Christie 12027996a778SMike Christie #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \ 12037996a778SMike Christie _sz % sizeof(unsigned long)) 12047996a778SMike Christie 12057996a778SMike Christie #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata)) 12067996a778SMike Christie 12077996a778SMike Christie /** 12087996a778SMike Christie * iscsi_session_setup - create iscsi cls session and host and session 12097996a778SMike Christie * @scsit: scsi transport template 12107996a778SMike Christie * @iscsit: iscsi transport template 12117996a778SMike Christie * @initial_cmdsn: initial CmdSN 12127996a778SMike Christie * @hostno: host no allocated 12137996a778SMike Christie * 12147996a778SMike Christie * This can be used by software iscsi_transports that allocate 12157996a778SMike Christie * a session per scsi host. 12167996a778SMike Christie **/ 12177996a778SMike Christie struct iscsi_cls_session * 12187996a778SMike Christie iscsi_session_setup(struct iscsi_transport *iscsit, 12197996a778SMike Christie struct scsi_transport_template *scsit, 12207996a778SMike Christie int cmd_task_size, int mgmt_task_size, 12217996a778SMike Christie uint32_t initial_cmdsn, uint32_t *hostno) 12227996a778SMike Christie { 12237996a778SMike Christie struct Scsi_Host *shost; 12247996a778SMike Christie struct iscsi_session *session; 12257996a778SMike Christie struct iscsi_cls_session *cls_session; 12267996a778SMike Christie int cmd_i; 12277996a778SMike Christie 12287996a778SMike Christie shost = scsi_host_alloc(iscsit->host_template, 12297996a778SMike Christie hostdata_privsize(sizeof(*session))); 12307996a778SMike Christie if (!shost) 12317996a778SMike Christie return NULL; 12327996a778SMike Christie 12337996a778SMike Christie shost->max_id = 1; 12347996a778SMike Christie shost->max_channel = 0; 12357996a778SMike Christie shost->max_lun = iscsit->max_lun; 12367996a778SMike Christie shost->max_cmd_len = iscsit->max_cmd_len; 12377996a778SMike Christie shost->transportt = scsit; 12387996a778SMike Christie shost->transportt->create_work_queue = 1; 12397996a778SMike Christie *hostno = shost->host_no; 12407996a778SMike Christie 12417996a778SMike Christie session = iscsi_hostdata(shost->hostdata); 12427996a778SMike Christie memset(session, 0, sizeof(struct iscsi_session)); 12437996a778SMike Christie session->host = shost; 12447996a778SMike Christie session->state = ISCSI_STATE_FREE; 12457996a778SMike Christie session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX; 12467996a778SMike Christie session->cmds_max = ISCSI_XMIT_CMDS_MAX; 12477996a778SMike Christie session->cmdsn = initial_cmdsn; 12487996a778SMike Christie session->exp_cmdsn = initial_cmdsn + 1; 12497996a778SMike Christie session->max_cmdsn = initial_cmdsn + 1; 12507996a778SMike Christie session->max_r2t = 1; 12517996a778SMike Christie session->tt = iscsit; 12527996a778SMike Christie 12537996a778SMike Christie /* initialize SCSI PDU commands pool */ 12547996a778SMike Christie if (iscsi_pool_init(&session->cmdpool, session->cmds_max, 12557996a778SMike Christie (void***)&session->cmds, 12567996a778SMike Christie cmd_task_size + sizeof(struct iscsi_cmd_task))) 12577996a778SMike Christie goto cmdpool_alloc_fail; 12587996a778SMike Christie 12597996a778SMike Christie /* pre-format cmds pool with ITT */ 12607996a778SMike Christie for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { 12617996a778SMike Christie struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; 12627996a778SMike Christie 12637996a778SMike Christie if (cmd_task_size) 12647996a778SMike Christie ctask->dd_data = &ctask[1]; 12657996a778SMike Christie ctask->itt = cmd_i; 12667996a778SMike Christie } 12677996a778SMike Christie 12687996a778SMike Christie spin_lock_init(&session->lock); 12697996a778SMike Christie INIT_LIST_HEAD(&session->connections); 12707996a778SMike Christie 12717996a778SMike Christie /* initialize immediate command pool */ 12727996a778SMike Christie if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max, 12737996a778SMike Christie (void***)&session->mgmt_cmds, 12747996a778SMike Christie mgmt_task_size + sizeof(struct iscsi_mgmt_task))) 12757996a778SMike Christie goto mgmtpool_alloc_fail; 12767996a778SMike Christie 12777996a778SMike Christie 12787996a778SMike Christie /* pre-format immediate cmds pool with ITT */ 12797996a778SMike Christie for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) { 12807996a778SMike Christie struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i]; 12817996a778SMike Christie 12827996a778SMike Christie if (mgmt_task_size) 12837996a778SMike Christie mtask->dd_data = &mtask[1]; 12847996a778SMike Christie mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i; 12857996a778SMike Christie } 12867996a778SMike Christie 12877996a778SMike Christie if (scsi_add_host(shost, NULL)) 12887996a778SMike Christie goto add_host_fail; 12897996a778SMike Christie 1290f53a88daSMike Christie if (!try_module_get(iscsit->owner)) 1291f53a88daSMike Christie goto cls_session_fail; 1292f53a88daSMike Christie 12936a8a0d36SMike Christie cls_session = iscsi_create_session(shost, iscsit, 0); 12947996a778SMike Christie if (!cls_session) 1295f53a88daSMike Christie goto module_put; 12967996a778SMike Christie *(unsigned long*)shost->hostdata = (unsigned long)cls_session; 12977996a778SMike Christie 12987996a778SMike Christie return cls_session; 12997996a778SMike Christie 1300f53a88daSMike Christie module_put: 1301f53a88daSMike Christie module_put(iscsit->owner); 13027996a778SMike Christie cls_session_fail: 13037996a778SMike Christie scsi_remove_host(shost); 13047996a778SMike Christie add_host_fail: 13057996a778SMike Christie iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds); 13067996a778SMike Christie mgmtpool_alloc_fail: 13077996a778SMike Christie iscsi_pool_free(&session->cmdpool, (void**)session->cmds); 13087996a778SMike Christie cmdpool_alloc_fail: 13097996a778SMike Christie scsi_host_put(shost); 13107996a778SMike Christie return NULL; 13117996a778SMike Christie } 13127996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_setup); 13137996a778SMike Christie 13147996a778SMike Christie /** 13157996a778SMike Christie * iscsi_session_teardown - destroy session, host, and cls_session 13167996a778SMike Christie * shost: scsi host 13177996a778SMike Christie * 13187996a778SMike Christie * This can be used by software iscsi_transports that allocate 13197996a778SMike Christie * a session per scsi host. 13207996a778SMike Christie **/ 13217996a778SMike Christie void iscsi_session_teardown(struct iscsi_cls_session *cls_session) 13227996a778SMike Christie { 13237996a778SMike Christie struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); 13247996a778SMike Christie struct iscsi_session *session = iscsi_hostdata(shost->hostdata); 13257996a778SMike Christie 13267996a778SMike Christie scsi_remove_host(shost); 13277996a778SMike Christie 13287996a778SMike Christie iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds); 13297996a778SMike Christie iscsi_pool_free(&session->cmdpool, (void**)session->cmds); 13307996a778SMike Christie 13317996a778SMike Christie iscsi_destroy_session(cls_session); 13327996a778SMike Christie scsi_host_put(shost); 1333f53a88daSMike Christie module_put(cls_session->transport->owner); 13347996a778SMike Christie } 13357996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_session_teardown); 13367996a778SMike Christie 13377996a778SMike Christie /** 13387996a778SMike Christie * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn 13397996a778SMike Christie * @cls_session: iscsi_cls_session 13407996a778SMike Christie * @conn_idx: cid 13417996a778SMike Christie **/ 13427996a778SMike Christie struct iscsi_cls_conn * 13437996a778SMike Christie iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx) 13447996a778SMike Christie { 13457996a778SMike Christie struct iscsi_session *session = class_to_transport_session(cls_session); 13467996a778SMike Christie struct iscsi_conn *conn; 13477996a778SMike Christie struct iscsi_cls_conn *cls_conn; 1348d36ab6f3SMike Christie char *data; 13497996a778SMike Christie 13507996a778SMike Christie cls_conn = iscsi_create_conn(cls_session, conn_idx); 13517996a778SMike Christie if (!cls_conn) 13527996a778SMike Christie return NULL; 13537996a778SMike Christie conn = cls_conn->dd_data; 13547996a778SMike Christie memset(conn, 0, sizeof(*conn)); 13557996a778SMike Christie 13567996a778SMike Christie conn->session = session; 13577996a778SMike Christie conn->cls_conn = cls_conn; 13587996a778SMike Christie conn->c_stage = ISCSI_CONN_INITIAL_STAGE; 13597996a778SMike Christie conn->id = conn_idx; 13607996a778SMike Christie conn->exp_statsn = 0; 13617996a778SMike Christie conn->tmabort_state = TMABORT_INITIAL; 13627996a778SMike Christie INIT_LIST_HEAD(&conn->run_list); 13637996a778SMike Christie INIT_LIST_HEAD(&conn->mgmt_run_list); 13647996a778SMike Christie 13657996a778SMike Christie /* initialize general xmit PDU commands queue */ 13667996a778SMike Christie conn->xmitqueue = kfifo_alloc(session->cmds_max * sizeof(void*), 13677996a778SMike Christie GFP_KERNEL, NULL); 13687996a778SMike Christie if (conn->xmitqueue == ERR_PTR(-ENOMEM)) 13697996a778SMike Christie goto xmitqueue_alloc_fail; 13707996a778SMike Christie 13717996a778SMike Christie /* initialize general immediate & non-immediate PDU commands queue */ 13727996a778SMike Christie conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*), 13737996a778SMike Christie GFP_KERNEL, NULL); 13747996a778SMike Christie if (conn->immqueue == ERR_PTR(-ENOMEM)) 13757996a778SMike Christie goto immqueue_alloc_fail; 13767996a778SMike Christie 13777996a778SMike Christie conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*), 13787996a778SMike Christie GFP_KERNEL, NULL); 13797996a778SMike Christie if (conn->mgmtqueue == ERR_PTR(-ENOMEM)) 13807996a778SMike Christie goto mgmtqueue_alloc_fail; 13817996a778SMike Christie 13827996a778SMike Christie INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn); 13837996a778SMike Christie 13847996a778SMike Christie /* allocate login_mtask used for the login/text sequences */ 13857996a778SMike Christie spin_lock_bh(&session->lock); 13867996a778SMike Christie if (!__kfifo_get(session->mgmtpool.queue, 13877996a778SMike Christie (void*)&conn->login_mtask, 13887996a778SMike Christie sizeof(void*))) { 13897996a778SMike Christie spin_unlock_bh(&session->lock); 13907996a778SMike Christie goto login_mtask_alloc_fail; 13917996a778SMike Christie } 13927996a778SMike Christie spin_unlock_bh(&session->lock); 13937996a778SMike Christie 1394d36ab6f3SMike Christie data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL); 1395d36ab6f3SMike Christie if (!data) 1396d36ab6f3SMike Christie goto login_mtask_data_alloc_fail; 1397d36ab6f3SMike Christie conn->login_mtask->data = data; 1398d36ab6f3SMike Christie 13997996a778SMike Christie init_timer(&conn->tmabort_timer); 14007996a778SMike Christie mutex_init(&conn->xmitmutex); 14017996a778SMike Christie init_waitqueue_head(&conn->ehwait); 14027996a778SMike Christie 14037996a778SMike Christie return cls_conn; 14047996a778SMike Christie 1405d36ab6f3SMike Christie login_mtask_data_alloc_fail: 1406d36ab6f3SMike Christie __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask, 1407d36ab6f3SMike Christie sizeof(void*)); 14087996a778SMike Christie login_mtask_alloc_fail: 14097996a778SMike Christie kfifo_free(conn->mgmtqueue); 14107996a778SMike Christie mgmtqueue_alloc_fail: 14117996a778SMike Christie kfifo_free(conn->immqueue); 14127996a778SMike Christie immqueue_alloc_fail: 14137996a778SMike Christie kfifo_free(conn->xmitqueue); 14147996a778SMike Christie xmitqueue_alloc_fail: 14157996a778SMike Christie iscsi_destroy_conn(cls_conn); 14167996a778SMike Christie return NULL; 14177996a778SMike Christie } 14187996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_setup); 14197996a778SMike Christie 14207996a778SMike Christie /** 14217996a778SMike Christie * iscsi_conn_teardown - teardown iscsi connection 14227996a778SMike Christie * cls_conn: iscsi class connection 14237996a778SMike Christie * 14247996a778SMike Christie * TODO: we may need to make this into a two step process 14257996a778SMike Christie * like scsi-mls remove + put host 14267996a778SMike Christie */ 14277996a778SMike Christie void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) 14287996a778SMike Christie { 14297996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 14307996a778SMike Christie struct iscsi_session *session = conn->session; 14317996a778SMike Christie unsigned long flags; 14327996a778SMike Christie 14337996a778SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 143467a61114SMike Christie mutex_lock(&conn->xmitmutex); 14357996a778SMike Christie if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE) { 14367996a778SMike Christie if (session->tt->suspend_conn_recv) 14377996a778SMike Christie session->tt->suspend_conn_recv(conn); 14387996a778SMike Christie 14397996a778SMike Christie session->tt->terminate_conn(conn); 14407996a778SMike Christie } 14417996a778SMike Christie 14427996a778SMike Christie spin_lock_bh(&session->lock); 14437996a778SMike Christie conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; 14447996a778SMike Christie if (session->leadconn == conn) { 14457996a778SMike Christie /* 14467996a778SMike Christie * leading connection? then give up on recovery. 14477996a778SMike Christie */ 14487996a778SMike Christie session->state = ISCSI_STATE_TERMINATE; 14497996a778SMike Christie wake_up(&conn->ehwait); 14507996a778SMike Christie } 14517996a778SMike Christie spin_unlock_bh(&session->lock); 14527996a778SMike Christie 14537996a778SMike Christie mutex_unlock(&conn->xmitmutex); 14547996a778SMike Christie 14557996a778SMike Christie /* 14567996a778SMike Christie * Block until all in-progress commands for this connection 14577996a778SMike Christie * time out or fail. 14587996a778SMike Christie */ 14597996a778SMike Christie for (;;) { 14607996a778SMike Christie spin_lock_irqsave(session->host->host_lock, flags); 14617996a778SMike Christie if (!session->host->host_busy) { /* OK for ERL == 0 */ 14627996a778SMike Christie spin_unlock_irqrestore(session->host->host_lock, flags); 14637996a778SMike Christie break; 14647996a778SMike Christie } 14657996a778SMike Christie spin_unlock_irqrestore(session->host->host_lock, flags); 14667996a778SMike Christie msleep_interruptible(500); 1467be2df72eSOr Gerlitz printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d " 1468be2df72eSOr Gerlitz "host_failed %d\n", session->host->host_busy, 1469be2df72eSOr Gerlitz session->host->host_failed); 14707996a778SMike Christie /* 14717996a778SMike Christie * force eh_abort() to unblock 14727996a778SMike Christie */ 14737996a778SMike Christie wake_up(&conn->ehwait); 14747996a778SMike Christie } 14757996a778SMike Christie 14767996a778SMike Christie spin_lock_bh(&session->lock); 1477d36ab6f3SMike Christie kfree(conn->login_mtask->data); 14787996a778SMike Christie __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask, 14797996a778SMike Christie sizeof(void*)); 14807996a778SMike Christie list_del(&conn->item); 14817996a778SMike Christie if (list_empty(&session->connections)) 14827996a778SMike Christie session->leadconn = NULL; 14837996a778SMike Christie if (session->leadconn && session->leadconn == conn) 14847996a778SMike Christie session->leadconn = container_of(session->connections.next, 14857996a778SMike Christie struct iscsi_conn, item); 14867996a778SMike Christie 14877996a778SMike Christie if (session->leadconn == NULL) 14887996a778SMike Christie /* no connections exits.. reset sequencing */ 14897996a778SMike Christie session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1; 14907996a778SMike Christie spin_unlock_bh(&session->lock); 14917996a778SMike Christie 14927996a778SMike Christie kfifo_free(conn->xmitqueue); 14937996a778SMike Christie kfifo_free(conn->immqueue); 14947996a778SMike Christie kfifo_free(conn->mgmtqueue); 14957996a778SMike Christie 14967996a778SMike Christie iscsi_destroy_conn(cls_conn); 14977996a778SMike Christie } 14987996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_teardown); 14997996a778SMike Christie 15007996a778SMike Christie int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) 15017996a778SMike Christie { 15027996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 15037996a778SMike Christie struct iscsi_session *session = conn->session; 15047996a778SMike Christie 15057996a778SMike Christie if (session == NULL) { 15067996a778SMike Christie printk(KERN_ERR "iscsi: can't start unbound connection\n"); 15077996a778SMike Christie return -EPERM; 15087996a778SMike Christie } 15097996a778SMike Christie 15107996a778SMike Christie spin_lock_bh(&session->lock); 15117996a778SMike Christie conn->c_stage = ISCSI_CONN_STARTED; 15127996a778SMike Christie session->state = ISCSI_STATE_LOGGED_IN; 15137996a778SMike Christie 15147996a778SMike Christie switch(conn->stop_stage) { 15157996a778SMike Christie case STOP_CONN_RECOVER: 15167996a778SMike Christie /* 15177996a778SMike Christie * unblock eh_abort() if it is blocked. re-try all 15187996a778SMike Christie * commands after successful recovery 15197996a778SMike Christie */ 15207996a778SMike Christie conn->stop_stage = 0; 15217996a778SMike Christie conn->tmabort_state = TMABORT_INITIAL; 15227996a778SMike Christie session->age++; 15237996a778SMike Christie spin_unlock_bh(&session->lock); 15247996a778SMike Christie 15257996a778SMike Christie iscsi_unblock_session(session_to_cls(session)); 15267996a778SMike Christie wake_up(&conn->ehwait); 15277996a778SMike Christie return 0; 15287996a778SMike Christie case STOP_CONN_TERM: 15297996a778SMike Christie conn->stop_stage = 0; 15307996a778SMike Christie break; 15317996a778SMike Christie default: 15327996a778SMike Christie break; 15337996a778SMike Christie } 15347996a778SMike Christie spin_unlock_bh(&session->lock); 15357996a778SMike Christie 15367996a778SMike Christie return 0; 15377996a778SMike Christie } 15387996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_start); 15397996a778SMike Christie 15407996a778SMike Christie static void 15417996a778SMike Christie flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn) 15427996a778SMike Christie { 15437996a778SMike Christie struct iscsi_mgmt_task *mtask, *tmp; 15447996a778SMike Christie 15457996a778SMike Christie /* handle pending */ 15467996a778SMike Christie while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) || 15477996a778SMike Christie __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) { 15487996a778SMike Christie if (mtask == conn->login_mtask) 15497996a778SMike Christie continue; 15507996a778SMike Christie debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt); 15517996a778SMike Christie __kfifo_put(session->mgmtpool.queue, (void*)&mtask, 15527996a778SMike Christie sizeof(void*)); 15537996a778SMike Christie } 15547996a778SMike Christie 15557996a778SMike Christie /* handle running */ 15567996a778SMike Christie list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) { 15577996a778SMike Christie debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt); 1558ed2abc7fSMike Christie list_del(&mtask->running); 1559ed2abc7fSMike Christie 15607996a778SMike Christie if (mtask == conn->login_mtask) 15617996a778SMike Christie continue; 1562ed2abc7fSMike Christie __kfifo_put(session->mgmtpool.queue, (void*)&mtask, 15637996a778SMike Christie sizeof(void*)); 15647996a778SMike Christie } 15657996a778SMike Christie 15667996a778SMike Christie conn->mtask = NULL; 15677996a778SMike Christie } 15687996a778SMike Christie 15697996a778SMike Christie /* Fail commands. Mutex and session lock held and recv side suspended */ 15707996a778SMike Christie static void fail_all_commands(struct iscsi_conn *conn) 15717996a778SMike Christie { 15727996a778SMike Christie struct iscsi_cmd_task *ctask, *tmp; 15737996a778SMike Christie 15747996a778SMike Christie /* flush pending */ 15757996a778SMike Christie while (__kfifo_get(conn->xmitqueue, (void*)&ctask, sizeof(void*))) { 15767996a778SMike Christie debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc, 15777996a778SMike Christie ctask->itt); 15787996a778SMike Christie fail_command(conn, ctask, DID_BUS_BUSY << 16); 15797996a778SMike Christie } 15807996a778SMike Christie 15817996a778SMike Christie /* fail all other running */ 15827996a778SMike Christie list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) { 15837996a778SMike Christie debug_scsi("failing in progress sc %p itt 0x%x\n", 15847996a778SMike Christie ctask->sc, ctask->itt); 15857996a778SMike Christie fail_command(conn, ctask, DID_BUS_BUSY << 16); 15867996a778SMike Christie } 15877996a778SMike Christie 15887996a778SMike Christie conn->ctask = NULL; 15897996a778SMike Christie } 15907996a778SMike Christie 1591656cffc9SMike Christie static void iscsi_start_session_recovery(struct iscsi_session *session, 15927996a778SMike Christie struct iscsi_conn *conn, int flag) 15937996a778SMike Christie { 1594ed2abc7fSMike Christie int old_stop_stage; 1595ed2abc7fSMike Christie 15967996a778SMike Christie spin_lock_bh(&session->lock); 1597ed2abc7fSMike Christie if (conn->stop_stage == STOP_CONN_TERM) { 15987996a778SMike Christie spin_unlock_bh(&session->lock); 15997996a778SMike Christie return; 16007996a778SMike Christie } 1601ed2abc7fSMike Christie 1602ed2abc7fSMike Christie /* 1603ed2abc7fSMike Christie * When this is called for the in_login state, we only want to clean 160467a61114SMike Christie * up the login task and connection. We do not need to block and set 160567a61114SMike Christie * the recovery state again 1606ed2abc7fSMike Christie */ 160767a61114SMike Christie if (flag == STOP_CONN_TERM) 160867a61114SMike Christie session->state = ISCSI_STATE_TERMINATE; 160967a61114SMike Christie else if (conn->stop_stage != STOP_CONN_RECOVER) 161067a61114SMike Christie session->state = ISCSI_STATE_IN_RECOVERY; 1611ed2abc7fSMike Christie 1612ed2abc7fSMike Christie old_stop_stage = conn->stop_stage; 16137996a778SMike Christie conn->stop_stage = flag; 161467a61114SMike Christie conn->c_stage = ISCSI_CONN_STOPPED; 161567a61114SMike Christie set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 16167996a778SMike Christie spin_unlock_bh(&session->lock); 16177996a778SMike Christie 16187996a778SMike Christie if (session->tt->suspend_conn_recv) 16197996a778SMike Christie session->tt->suspend_conn_recv(conn); 16207996a778SMike Christie 16217996a778SMike Christie mutex_lock(&conn->xmitmutex); 16227996a778SMike Christie /* 16237996a778SMike Christie * for connection level recovery we should not calculate 16247996a778SMike Christie * header digest. conn->hdr_size used for optimization 16257996a778SMike Christie * in hdr_extract() and will be re-negotiated at 16267996a778SMike Christie * set_param() time. 16277996a778SMike Christie */ 16287996a778SMike Christie if (flag == STOP_CONN_RECOVER) { 16297996a778SMike Christie conn->hdrdgst_en = 0; 16307996a778SMike Christie conn->datadgst_en = 0; 1631656cffc9SMike Christie if (session->state == ISCSI_STATE_IN_RECOVERY && 163267a61114SMike Christie old_stop_stage != STOP_CONN_RECOVER) { 163367a61114SMike Christie debug_scsi("blocking session\n"); 16347996a778SMike Christie iscsi_block_session(session_to_cls(session)); 16357996a778SMike Christie } 163667a61114SMike Christie } 1637656cffc9SMike Christie 1638656cffc9SMike Christie session->tt->terminate_conn(conn); 1639656cffc9SMike Christie /* 1640656cffc9SMike Christie * flush queues. 1641656cffc9SMike Christie */ 1642656cffc9SMike Christie spin_lock_bh(&session->lock); 1643656cffc9SMike Christie fail_all_commands(conn); 1644656cffc9SMike Christie flush_control_queues(session, conn); 1645656cffc9SMike Christie spin_unlock_bh(&session->lock); 1646656cffc9SMike Christie 16477996a778SMike Christie mutex_unlock(&conn->xmitmutex); 16487996a778SMike Christie } 16497996a778SMike Christie 16507996a778SMike Christie void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) 16517996a778SMike Christie { 16527996a778SMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 16537996a778SMike Christie struct iscsi_session *session = conn->session; 16547996a778SMike Christie 16557996a778SMike Christie switch (flag) { 16567996a778SMike Christie case STOP_CONN_RECOVER: 16577996a778SMike Christie case STOP_CONN_TERM: 16587996a778SMike Christie iscsi_start_session_recovery(session, conn, flag); 16598d2860b3SMike Christie break; 16607996a778SMike Christie default: 1661be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag); 16627996a778SMike Christie } 16637996a778SMike Christie } 16647996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_stop); 16657996a778SMike Christie 16667996a778SMike Christie int iscsi_conn_bind(struct iscsi_cls_session *cls_session, 16677996a778SMike Christie struct iscsi_cls_conn *cls_conn, int is_leading) 16687996a778SMike Christie { 16697996a778SMike Christie struct iscsi_session *session = class_to_transport_session(cls_session); 16707996a778SMike Christie struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data; 16717996a778SMike Christie 16727996a778SMike Christie /* lookup for existing connection */ 16737996a778SMike Christie spin_lock_bh(&session->lock); 16747996a778SMike Christie list_for_each_entry(tmp, &session->connections, item) { 16757996a778SMike Christie if (tmp == conn) { 16767996a778SMike Christie if (conn->c_stage != ISCSI_CONN_STOPPED || 16777996a778SMike Christie conn->stop_stage == STOP_CONN_TERM) { 1678be2df72eSOr Gerlitz printk(KERN_ERR "iscsi: can't bind " 16797996a778SMike Christie "non-stopped connection (%d:%d)\n", 16807996a778SMike Christie conn->c_stage, conn->stop_stage); 16817996a778SMike Christie spin_unlock_bh(&session->lock); 16827996a778SMike Christie return -EIO; 16837996a778SMike Christie } 16847996a778SMike Christie break; 16857996a778SMike Christie } 16867996a778SMike Christie } 16877996a778SMike Christie if (tmp != conn) { 16887996a778SMike Christie /* bind new iSCSI connection to session */ 16897996a778SMike Christie conn->session = session; 16907996a778SMike Christie list_add(&conn->item, &session->connections); 16917996a778SMike Christie } 16927996a778SMike Christie spin_unlock_bh(&session->lock); 16937996a778SMike Christie 16947996a778SMike Christie if (is_leading) 16957996a778SMike Christie session->leadconn = conn; 16967996a778SMike Christie 16977996a778SMike Christie /* 16987996a778SMike Christie * Unblock xmitworker(), Login Phase will pass through. 16997996a778SMike Christie */ 17007996a778SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); 17017996a778SMike Christie clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); 17027996a778SMike Christie return 0; 17037996a778SMike Christie } 17047996a778SMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_bind); 17057996a778SMike Christie 1706a54a52caSMike Christie 1707a54a52caSMike Christie int iscsi_set_param(struct iscsi_cls_conn *cls_conn, 1708a54a52caSMike Christie enum iscsi_param param, char *buf, int buflen) 1709a54a52caSMike Christie { 1710a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 1711a54a52caSMike Christie struct iscsi_session *session = conn->session; 1712a54a52caSMike Christie uint32_t value; 1713a54a52caSMike Christie 1714a54a52caSMike Christie switch(param) { 1715a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 1716a54a52caSMike Christie sscanf(buf, "%d", &conn->max_recv_dlength); 1717a54a52caSMike Christie break; 1718a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 1719a54a52caSMike Christie sscanf(buf, "%d", &conn->max_xmit_dlength); 1720a54a52caSMike Christie break; 1721a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 1722a54a52caSMike Christie sscanf(buf, "%d", &conn->hdrdgst_en); 1723a54a52caSMike Christie break; 1724a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 1725a54a52caSMike Christie sscanf(buf, "%d", &conn->datadgst_en); 1726a54a52caSMike Christie break; 1727a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 1728a54a52caSMike Christie sscanf(buf, "%d", &session->initial_r2t_en); 1729a54a52caSMike Christie break; 1730a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 1731a54a52caSMike Christie sscanf(buf, "%d", &session->max_r2t); 1732a54a52caSMike Christie break; 1733a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 1734a54a52caSMike Christie sscanf(buf, "%d", &session->imm_data_en); 1735a54a52caSMike Christie break; 1736a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 1737a54a52caSMike Christie sscanf(buf, "%d", &session->first_burst); 1738a54a52caSMike Christie break; 1739a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 1740a54a52caSMike Christie sscanf(buf, "%d", &session->max_burst); 1741a54a52caSMike Christie break; 1742a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 1743a54a52caSMike Christie sscanf(buf, "%d", &session->pdu_inorder_en); 1744a54a52caSMike Christie break; 1745a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 1746a54a52caSMike Christie sscanf(buf, "%d", &session->dataseq_inorder_en); 1747a54a52caSMike Christie break; 1748a54a52caSMike Christie case ISCSI_PARAM_ERL: 1749a54a52caSMike Christie sscanf(buf, "%d", &session->erl); 1750a54a52caSMike Christie break; 1751a54a52caSMike Christie case ISCSI_PARAM_IFMARKER_EN: 1752a54a52caSMike Christie sscanf(buf, "%d", &value); 1753a54a52caSMike Christie BUG_ON(value); 1754a54a52caSMike Christie break; 1755a54a52caSMike Christie case ISCSI_PARAM_OFMARKER_EN: 1756a54a52caSMike Christie sscanf(buf, "%d", &value); 1757a54a52caSMike Christie BUG_ON(value); 1758a54a52caSMike Christie break; 1759a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 1760a54a52caSMike Christie sscanf(buf, "%u", &conn->exp_statsn); 1761a54a52caSMike Christie break; 1762a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 1763a54a52caSMike Christie /* this should not change between logins */ 1764a54a52caSMike Christie if (session->targetname) 1765a54a52caSMike Christie break; 1766a54a52caSMike Christie 1767a54a52caSMike Christie session->targetname = kstrdup(buf, GFP_KERNEL); 1768a54a52caSMike Christie if (!session->targetname) 1769a54a52caSMike Christie return -ENOMEM; 1770a54a52caSMike Christie break; 1771a54a52caSMike Christie case ISCSI_PARAM_TPGT: 1772a54a52caSMike Christie sscanf(buf, "%d", &session->tpgt); 1773a54a52caSMike Christie break; 1774a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 1775a54a52caSMike Christie sscanf(buf, "%d", &conn->persistent_port); 1776a54a52caSMike Christie break; 1777a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 1778a54a52caSMike Christie /* 1779a54a52caSMike Christie * this is the address returned in discovery so it should 1780a54a52caSMike Christie * not change between logins. 1781a54a52caSMike Christie */ 1782a54a52caSMike Christie if (conn->persistent_address) 1783a54a52caSMike Christie break; 1784a54a52caSMike Christie 1785a54a52caSMike Christie conn->persistent_address = kstrdup(buf, GFP_KERNEL); 1786a54a52caSMike Christie if (!conn->persistent_address) 1787a54a52caSMike Christie return -ENOMEM; 1788a54a52caSMike Christie break; 1789a54a52caSMike Christie default: 1790a54a52caSMike Christie return -ENOSYS; 1791a54a52caSMike Christie } 1792a54a52caSMike Christie 1793a54a52caSMike Christie return 0; 1794a54a52caSMike Christie } 1795a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_set_param); 1796a54a52caSMike Christie 1797a54a52caSMike Christie int iscsi_session_get_param(struct iscsi_cls_session *cls_session, 1798a54a52caSMike Christie enum iscsi_param param, char *buf) 1799a54a52caSMike Christie { 1800a54a52caSMike Christie struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); 1801a54a52caSMike Christie struct iscsi_session *session = iscsi_hostdata(shost->hostdata); 1802a54a52caSMike Christie int len; 1803a54a52caSMike Christie 1804a54a52caSMike Christie switch(param) { 1805a54a52caSMike Christie case ISCSI_PARAM_INITIAL_R2T_EN: 1806a54a52caSMike Christie len = sprintf(buf, "%d\n", session->initial_r2t_en); 1807a54a52caSMike Christie break; 1808a54a52caSMike Christie case ISCSI_PARAM_MAX_R2T: 1809a54a52caSMike Christie len = sprintf(buf, "%hu\n", session->max_r2t); 1810a54a52caSMike Christie break; 1811a54a52caSMike Christie case ISCSI_PARAM_IMM_DATA_EN: 1812a54a52caSMike Christie len = sprintf(buf, "%d\n", session->imm_data_en); 1813a54a52caSMike Christie break; 1814a54a52caSMike Christie case ISCSI_PARAM_FIRST_BURST: 1815a54a52caSMike Christie len = sprintf(buf, "%u\n", session->first_burst); 1816a54a52caSMike Christie break; 1817a54a52caSMike Christie case ISCSI_PARAM_MAX_BURST: 1818a54a52caSMike Christie len = sprintf(buf, "%u\n", session->max_burst); 1819a54a52caSMike Christie break; 1820a54a52caSMike Christie case ISCSI_PARAM_PDU_INORDER_EN: 1821a54a52caSMike Christie len = sprintf(buf, "%d\n", session->pdu_inorder_en); 1822a54a52caSMike Christie break; 1823a54a52caSMike Christie case ISCSI_PARAM_DATASEQ_INORDER_EN: 1824a54a52caSMike Christie len = sprintf(buf, "%d\n", session->dataseq_inorder_en); 1825a54a52caSMike Christie break; 1826a54a52caSMike Christie case ISCSI_PARAM_ERL: 1827a54a52caSMike Christie len = sprintf(buf, "%d\n", session->erl); 1828a54a52caSMike Christie break; 1829a54a52caSMike Christie case ISCSI_PARAM_TARGET_NAME: 1830a54a52caSMike Christie len = sprintf(buf, "%s\n", session->targetname); 1831a54a52caSMike Christie break; 1832a54a52caSMike Christie case ISCSI_PARAM_TPGT: 1833a54a52caSMike Christie len = sprintf(buf, "%d\n", session->tpgt); 1834a54a52caSMike Christie break; 1835a54a52caSMike Christie default: 1836a54a52caSMike Christie return -ENOSYS; 1837a54a52caSMike Christie } 1838a54a52caSMike Christie 1839a54a52caSMike Christie return len; 1840a54a52caSMike Christie } 1841a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_session_get_param); 1842a54a52caSMike Christie 1843a54a52caSMike Christie int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, 1844a54a52caSMike Christie enum iscsi_param param, char *buf) 1845a54a52caSMike Christie { 1846a54a52caSMike Christie struct iscsi_conn *conn = cls_conn->dd_data; 1847a54a52caSMike Christie int len; 1848a54a52caSMike Christie 1849a54a52caSMike Christie switch(param) { 1850a54a52caSMike Christie case ISCSI_PARAM_MAX_RECV_DLENGTH: 1851a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->max_recv_dlength); 1852a54a52caSMike Christie break; 1853a54a52caSMike Christie case ISCSI_PARAM_MAX_XMIT_DLENGTH: 1854a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->max_xmit_dlength); 1855a54a52caSMike Christie break; 1856a54a52caSMike Christie case ISCSI_PARAM_HDRDGST_EN: 1857a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->hdrdgst_en); 1858a54a52caSMike Christie break; 1859a54a52caSMike Christie case ISCSI_PARAM_DATADGST_EN: 1860a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->datadgst_en); 1861a54a52caSMike Christie break; 1862a54a52caSMike Christie case ISCSI_PARAM_IFMARKER_EN: 1863a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->ifmarker_en); 1864a54a52caSMike Christie break; 1865a54a52caSMike Christie case ISCSI_PARAM_OFMARKER_EN: 1866a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->ofmarker_en); 1867a54a52caSMike Christie break; 1868a54a52caSMike Christie case ISCSI_PARAM_EXP_STATSN: 1869a54a52caSMike Christie len = sprintf(buf, "%u\n", conn->exp_statsn); 1870a54a52caSMike Christie break; 1871a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_PORT: 1872a54a52caSMike Christie len = sprintf(buf, "%d\n", conn->persistent_port); 1873a54a52caSMike Christie break; 1874a54a52caSMike Christie case ISCSI_PARAM_PERSISTENT_ADDRESS: 1875a54a52caSMike Christie len = sprintf(buf, "%s\n", conn->persistent_address); 1876a54a52caSMike Christie break; 1877a54a52caSMike Christie default: 1878a54a52caSMike Christie return -ENOSYS; 1879a54a52caSMike Christie } 1880a54a52caSMike Christie 1881a54a52caSMike Christie return len; 1882a54a52caSMike Christie } 1883a54a52caSMike Christie EXPORT_SYMBOL_GPL(iscsi_conn_get_param); 1884a54a52caSMike Christie 18857996a778SMike Christie MODULE_AUTHOR("Mike Christie"); 18867996a778SMike Christie MODULE_DESCRIPTION("iSCSI library functions"); 18877996a778SMike Christie MODULE_LICENSE("GPL"); 1888