1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>. 5 * Copyright (c) Intel Corporation. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * * Neither the name of Intel Corporation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "scsi_internal.h" 36 37 #include "spdk/endian.h" 38 39 struct spdk_scsi_port * 40 spdk_scsi_port_create(uint64_t id, uint16_t index, const char *name) 41 { 42 struct spdk_scsi_port *port; 43 44 port = calloc(1, sizeof(struct spdk_scsi_port)); 45 46 if (!port) { 47 return NULL; 48 } 49 50 if (spdk_scsi_port_construct(port, id, index, name) != 0) { 51 spdk_scsi_port_free(&port); 52 return NULL; 53 } 54 55 return port; 56 } 57 58 void 59 spdk_scsi_port_free(struct spdk_scsi_port **pport) 60 { 61 struct spdk_scsi_port *port; 62 63 if (!pport) { 64 return; 65 } 66 67 port = *pport; 68 *pport = NULL; 69 free(port); 70 } 71 72 int 73 spdk_scsi_port_construct(struct spdk_scsi_port *port, uint64_t id, uint16_t index, 74 const char *name) 75 { 76 if (strlen(name) >= sizeof(port->name)) { 77 SPDK_ERRLOG("port name too long\n"); 78 return -1; 79 } 80 81 port->is_used = 1; 82 port->id = id; 83 port->index = index; 84 snprintf(port->name, sizeof(port->name), "%s", name); 85 return 0; 86 } 87 88 void 89 spdk_scsi_port_destruct(struct spdk_scsi_port *port) 90 { 91 memset(port, 0, sizeof(struct spdk_scsi_port)); 92 } 93 94 const char * 95 spdk_scsi_port_get_name(const struct spdk_scsi_port *port) 96 { 97 return port->name; 98 } 99 100 /* 101 * spc3r23 7.5.4.6 iSCSI initiator port TransportID, 102 * using code format 0x01. 103 */ 104 void 105 spdk_scsi_port_set_iscsi_transport_id(struct spdk_scsi_port *port, char *iscsi_name, 106 uint64_t isid) 107 { 108 struct spdk_scsi_iscsi_transport_id *data; 109 uint32_t len; 110 char *name; 111 112 memset(port->transport_id, 0, sizeof(port->transport_id)); 113 port->transport_id_len = 0; 114 115 data = (struct spdk_scsi_iscsi_transport_id *)port->transport_id; 116 117 data->protocol_id = (uint8_t)SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI; 118 data->format = 0x1; 119 120 name = data->name; 121 len = snprintf(name, SPDK_SCSI_MAX_TRANSPORT_ID_LENGTH - sizeof(*data), 122 "%s,i,0x%12.12" PRIx64, iscsi_name, isid); 123 do { 124 name[len++] = '\0'; 125 } while (len & 3); 126 127 if (len < 20) { 128 SPDK_ERRLOG("The length of Transport ID should >= 20 bytes\n"); 129 return; 130 } 131 132 to_be16(&data->additional_len, len); 133 port->transport_id_len = len + sizeof(*data); 134 } 135