Sridhar Parasuram | c3946bb | 2015-02-02 10:34:50 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2013-2015 The Linux Foundation. All rights reserved. |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <debug.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <endian.h> |
| 32 | #include <string.h> |
| 33 | #include <utp.h> |
Channagoud Kadabi | 5890d9b | 2015-01-29 13:04:04 -0800 | [diff] [blame] | 34 | #include <rpmb.h> |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 35 | |
| 36 | int ucs_do_scsi_cmd(struct ufs_dev *dev, struct scsi_req_build_type *req) |
| 37 | { |
| 38 | struct upiu_req_build_type req_upiu; |
Sridhar Parasuram | 782645e | 2015-02-06 16:47:00 -0800 | [diff] [blame] | 39 | struct upiu_basic_resp_hdr resp_upiu; |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 40 | |
| 41 | memset(&req_upiu, 0 , sizeof(struct upiu_req_build_type)); |
| 42 | |
| 43 | req_upiu.cmd_set_type = UPIU_SCSI_CMD_SET; |
| 44 | req_upiu.trans_type = UPIU_TYPE_COMMAND; |
| 45 | req_upiu.data_buffer_addr = req->data_buffer_addr; |
| 46 | req_upiu.expected_data_len = req->data_len; |
| 47 | req_upiu.data_seg_len = 0; |
| 48 | req_upiu.ehs_len = 0; |
| 49 | req_upiu.flags = req->flags; |
| 50 | req_upiu.lun = req->lun; |
| 51 | req_upiu.query_mgmt_func = 0; |
| 52 | req_upiu.cdb = req->cdb; |
| 53 | req_upiu.cmd_type = UTRD_SCSCI_CMD; |
| 54 | req_upiu.dd = req->dd; |
| 55 | req_upiu.resp_ptr = &resp_upiu; |
| 56 | req_upiu.resp_len = sizeof(resp_upiu); |
| 57 | req_upiu.timeout_msecs = UTP_GENERIC_CMD_TIMEOUT; |
| 58 | |
| 59 | if (utp_enqueue_upiu(dev, &req_upiu)) |
| 60 | { |
Sundarajan Srinivasan | 2fcaa98 | 2013-10-31 17:44:02 -0700 | [diff] [blame] | 61 | dprintf(CRITICAL, "ucs_do_scsi_cmd: enqueue failed\n"); |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 62 | return -UFS_FAILURE; |
| 63 | } |
| 64 | |
| 65 | if (resp_upiu.status != SCSI_STATUS_GOOD) |
| 66 | { |
| 67 | if (resp_upiu.status == SCSI_STATUS_CHK_COND && (*((uint8_t *)(req->cdb)) != SCSI_CMD_SENSE_REQ)) |
| 68 | { |
Sridhar Parasuram | 782645e | 2015-02-06 16:47:00 -0800 | [diff] [blame] | 69 | dprintf(CRITICAL, "Data segment length: %x\n", BE16(resp_upiu.data_seg_len)); |
| 70 | if (BE16(resp_upiu.data_seg_len)) |
| 71 | { |
| 72 | dprintf(CRITICAL, "SCSI Request failed and we have sense data\n"); |
| 73 | dprintf(CRITICAL, "Sense Data Length/Response Code: 0x%x/0x%x\n", BE16(resp_upiu.sense_length), BE16(resp_upiu.sense_response_code)); |
| 74 | parse_sense_key(resp_upiu.sense_data[0]); |
| 75 | dprintf(CRITICAL, "Sense Buffer (HEX): 0x%x 0x%x 0x%x 0x%x\n", BE32(resp_upiu.sense_data[0]), BE32(resp_upiu.sense_data[1]), BE32(resp_upiu.sense_data[2]), BE32(resp_upiu.sense_data[3])); |
| 76 | } |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Sundarajan Srinivasan | 2fcaa98 | 2013-10-31 17:44:02 -0700 | [diff] [blame] | 79 | dprintf(CRITICAL, "ucs_do_scsi_cmd failed status = %x\n", resp_upiu.status); |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 80 | return -UFS_FAILURE; |
| 81 | } |
| 82 | |
| 83 | return UFS_SUCCESS; |
| 84 | } |
| 85 | |
Sridhar Parasuram | 782645e | 2015-02-06 16:47:00 -0800 | [diff] [blame] | 86 | int parse_sense_key(uint32_t sense_data) |
| 87 | { |
| 88 | uint32_t key = BE32(sense_data) >> 24; |
| 89 | dprintf(CRITICAL, "Sense Key: 0x%x\n", key); |
| 90 | switch(key) |
| 91 | { |
| 92 | case 0x0: |
| 93 | dprintf(INFO, "NO SENSE: No information available to be reported\n"); |
| 94 | break; |
| 95 | case 0x1: |
| 96 | dprintf(INFO, "RECOVERED ERROR: Additional sense buffer bytes indicate further details\n"); |
| 97 | break; |
| 98 | case 0x2: |
| 99 | dprintf(INFO, "NOT READY: Logical Unit Not Ready and cannot be accessed at this time\n"); |
| 100 | break; |
| 101 | case 0x3: |
| 102 | dprintf(INFO, "MEDIUM ERROR: Last command unsuccessful due to non-recoverable error condition\n"); |
| 103 | break; |
| 104 | case 0x4: |
| 105 | dprintf(INFO, "HARDWARE ERROR: Target detected a non-recoverable hardware error\n"); |
| 106 | break; |
| 107 | case 0x5: |
| 108 | dprintf(INFO, "ILLEGAL REQUEST: Illegal parameter in the command descriptor block in the command sent\n"); |
| 109 | break; |
| 110 | case 0x6: |
| 111 | dprintf(INFO, "UNIT ATTENTION: Unit has been reset/unexpectedly power on/removable media has changed\n"); |
| 112 | break; |
| 113 | case 0x7: |
| 114 | dprintf(INFO, "DATA PROTECT: Read/Write operation attempted on a block that is protected from this operation\n"); |
| 115 | break; |
| 116 | case 0x8: |
| 117 | dprintf(INFO, "BLANK CHECK: Target encountered blank or unformatted media while reading or writing\n"); |
| 118 | break; |
| 119 | case 0x9: |
| 120 | dprintf(INFO, "VENDOR SPECIFIC: Vendor specific error or exceptional conditions\n"); |
| 121 | break; |
| 122 | case 0xB: |
| 123 | dprintf(INFO, "ABORTED COMMAND: Target aborted the execution of the command\n"); |
| 124 | break; |
| 125 | case 0xD: |
| 126 | dprintf(INFO, "VOLUME OVERFLOW: Buffered peripheral device has reached the end of partition\n"); |
| 127 | break; |
| 128 | case 0xE: |
| 129 | dprintf(INFO, "MISCOMPARE: Source data did not match the data read from the media\n"); |
| 130 | break; |
| 131 | default: |
| 132 | dprintf(INFO, "INVALID sense key\n"); |
| 133 | } |
| 134 | return key; |
| 135 | } |
| 136 | |
Sridhar Parasuram | b48afc7 | 2014-10-23 20:01:56 -0700 | [diff] [blame] | 137 | int ucs_do_scsi_rpmb_read(struct ufs_dev *dev, uint32_t *req_buf, uint32_t blk_cnt, |
| 138 | uint32_t *resp_buf, uint32_t *resp_len) |
| 139 | { |
| 140 | // validate input parameters |
| 141 | ASSERT(req_buf); |
| 142 | ASSERT(resp_buf); |
| 143 | ASSERT(resp_len); |
| 144 | |
| 145 | STACKBUF_DMA_ALIGN(cdb, sizeof(struct scsi_sec_protocol_cdb)); |
| 146 | struct scsi_req_build_type req_upiu; |
| 147 | struct scsi_sec_protocol_cdb *cdb_out_param, *cdb_in_param; |
| 148 | uint32_t blks_remaining; |
| 149 | uint32_t blks_to_transfer; |
| 150 | uint64_t bytes_to_transfer; |
Sridhar Parasuram | b48afc7 | 2014-10-23 20:01:56 -0700 | [diff] [blame] | 151 | uint64_t max_size; |
| 152 | blks_remaining = blk_cnt; |
| 153 | blks_to_transfer = blks_remaining; |
| 154 | bytes_to_transfer = blks_to_transfer * RPMB_FRAME_SIZE; |
| 155 | |
| 156 | // check if total bytes to transfer exceed max supported size |
| 157 | max_size = dev->rpmb_rw_size * RPMB_FRAME_SIZE * blk_cnt; |
| 158 | if (bytes_to_transfer > max_size) |
| 159 | { |
| 160 | dprintf(CRITICAL, "RPMB request transfer size %llu greater than max transfer size %llu\n", bytes_to_transfer, max_size); |
| 161 | return -UFS_FAILURE; |
| 162 | } |
| 163 | #ifdef DEBUG_UFS |
| 164 | dprintf(INFO, "rpmb_read: req_buf: 0x%x blk_count: 0x%x\n", *req_buf, blk_cnt); |
| 165 | dprintf(INFO, "rpmb_read: bytes_to_transfer: 0x%x blks_to_transfer: 0x%x\n", |
| 166 | bytes_to_transfer, blks_to_transfer); |
| 167 | #endif |
| 168 | // send the request |
| 169 | cdb_out_param = (struct scsi_sec_protocol_cdb*) cdb; |
| 170 | memset(cdb_out_param, 0, sizeof(struct scsi_sec_protocol_cdb)); |
| 171 | |
| 172 | cdb_out_param->opcode = SCSI_CMD_SECPROT_OUT; |
| 173 | cdb_out_param->cdb1 = SCSI_SEC_PROT; |
| 174 | cdb_out_param->sec_protocol_specific = BE16(SCSI_SEC_UFS_PROT_ID); |
| 175 | cdb_out_param->alloc_tlen = BE32(bytes_to_transfer); |
| 176 | |
| 177 | // Flush CDB to memory |
| 178 | dsb(); |
| 179 | arch_clean_invalidate_cache_range((addr_t) cdb_out_param, sizeof(struct scsi_sec_protocol_cdb)); |
| 180 | |
| 181 | memset(&req_upiu, 0, sizeof(struct scsi_req_build_type)); |
| 182 | |
| 183 | req_upiu.cdb = (addr_t) cdb_out_param; |
Sridhar Parasuram | c02d107 | 2014-11-06 12:55:42 -0800 | [diff] [blame] | 184 | req_upiu.data_buffer_addr = (addr_t) req_buf; |
Sridhar Parasuram | b48afc7 | 2014-10-23 20:01:56 -0700 | [diff] [blame] | 185 | req_upiu.data_len = bytes_to_transfer; |
| 186 | req_upiu.flags = UPIU_FLAGS_WRITE; |
| 187 | req_upiu.lun = UFS_WLUN_RPMB; |
| 188 | req_upiu.dd = UTRD_TARGET_TO_SYSTEM; |
| 189 | |
| 190 | #ifdef DEBUG_UFS |
| 191 | dprintf(INFO, "Sending RPMB Read request\n"); |
| 192 | #endif |
| 193 | if (ucs_do_scsi_cmd(dev, &req_upiu)) |
| 194 | { |
| 195 | dprintf(CRITICAL, "%s:%d ucs_do_scsi_rpmb_read: failed\n", __func__, __LINE__); |
| 196 | return -UFS_FAILURE; |
| 197 | } |
| 198 | #ifdef DEBUG_UFS |
| 199 | dprintf(INFO, "Sending RPMB Read request complete\n"); |
| 200 | #endif |
| 201 | // read the response |
| 202 | cdb_in_param = (struct scsi_sec_protocol_cdb*) cdb; |
| 203 | memset(cdb_in_param, 0, sizeof(struct scsi_sec_protocol_cdb)); |
| 204 | |
| 205 | cdb_in_param->opcode = SCSI_CMD_SECPROT_IN; |
| 206 | cdb_in_param->cdb1 = SCSI_SEC_PROT; |
| 207 | cdb_in_param->sec_protocol_specific = BE16(SCSI_SEC_UFS_PROT_ID); |
| 208 | cdb_in_param->alloc_tlen = BE32(bytes_to_transfer); |
| 209 | |
| 210 | // Flush CDB to memory |
| 211 | dsb(); |
| 212 | arch_clean_invalidate_cache_range((addr_t) cdb_in_param, sizeof(struct scsi_sec_protocol_cdb)); |
| 213 | |
| 214 | memset(&req_upiu, 0, sizeof(struct scsi_req_build_type)); |
| 215 | |
| 216 | req_upiu.cdb = (addr_t) cdb_in_param; |
Sridhar Parasuram | c02d107 | 2014-11-06 12:55:42 -0800 | [diff] [blame] | 217 | req_upiu.data_buffer_addr = (addr_t) resp_buf; |
Sridhar Parasuram | b48afc7 | 2014-10-23 20:01:56 -0700 | [diff] [blame] | 218 | req_upiu.data_len = bytes_to_transfer; |
| 219 | req_upiu.flags = UPIU_FLAGS_READ; |
| 220 | req_upiu.lun = UFS_WLUN_RPMB; |
| 221 | req_upiu.dd = UTRD_SYSTEM_TO_TARGET; |
| 222 | |
| 223 | #ifdef DEBUG_UFS |
| 224 | dprintf(INFO, "Sending RPMB Read response\n"); |
| 225 | #endif |
| 226 | if (ucs_do_scsi_cmd(dev, &req_upiu)) |
| 227 | { |
| 228 | dprintf(CRITICAL, "%s:%d ucs_do_scsi_rpmb_read: failed\n", __func__, __LINE__); |
| 229 | return -UFS_FAILURE; |
| 230 | } |
| 231 | #ifdef DEBUG_UFS |
| 232 | dprintf(SPEW, "Sending RPMB Read response complete\n"); |
| 233 | #endif |
| 234 | *resp_len = bytes_to_transfer; |
| 235 | return UFS_SUCCESS; |
| 236 | } |
| 237 | |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 238 | int ucs_do_scsi_read(struct ufs_dev *dev, struct scsi_rdwr_req *req) |
| 239 | { |
| 240 | STACKBUF_DMA_ALIGN(cdb, sizeof(struct scsi_rdwr_cdb)); |
| 241 | struct scsi_req_build_type req_upiu; |
| 242 | struct scsi_rdwr_cdb *cdb_param; |
| 243 | uint32_t blks_remaining; |
| 244 | uint16_t blks_to_transfer; |
| 245 | uint64_t bytes_to_transfer; |
| 246 | uint32_t start_blk; |
| 247 | uint32_t buf; |
| 248 | |
| 249 | blks_remaining = req->num_blocks; |
| 250 | buf = req->data_buffer_base; |
| 251 | start_blk = req->start_lba; |
| 252 | |
| 253 | cdb_param = (struct scsi_rdwr_cdb*) cdb; |
| 254 | while (blks_remaining) |
| 255 | { |
| 256 | if (blks_remaining <= SCSI_MAX_DATA_TRANS_BLK_LEN) |
| 257 | { |
| 258 | blks_to_transfer = blks_remaining; |
| 259 | blks_remaining = 0; |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | blks_to_transfer = SCSI_MAX_DATA_TRANS_BLK_LEN; |
| 264 | blks_remaining -= SCSI_MAX_DATA_TRANS_BLK_LEN; |
| 265 | } |
| 266 | |
| 267 | bytes_to_transfer = blks_to_transfer * UFS_DEFAULT_SECTORE_SIZE; |
| 268 | |
| 269 | memset(cdb_param, 0, sizeof(struct scsi_rdwr_cdb)); |
| 270 | cdb_param->opcode = SCSI_CMD_READ10; |
| 271 | cdb_param->cdb1 = SCSI_READ_WRITE_10_CDB1(0, 0, 1, 0); |
| 272 | cdb_param->lba = BE32(start_blk); |
| 273 | cdb_param->trans_len = BE16(blks_to_transfer); |
| 274 | |
| 275 | |
| 276 | dsb(); |
| 277 | arch_clean_invalidate_cache_range((addr_t) cdb_param, sizeof(struct scsi_rdwr_cdb)); |
| 278 | |
| 279 | memset(&req_upiu, 0 , sizeof(struct scsi_req_build_type)); |
| 280 | |
| 281 | req_upiu.cdb = (addr_t) cdb_param; |
| 282 | req_upiu.data_buffer_addr = buf; |
| 283 | req_upiu.data_len = bytes_to_transfer; |
| 284 | req_upiu.flags = UPIU_FLAGS_READ; |
| 285 | req_upiu.lun = req->lun; |
| 286 | req_upiu.dd = UTRD_TARGET_TO_SYSTEM; |
| 287 | |
| 288 | if (ucs_do_scsi_cmd(dev, &req_upiu)) |
| 289 | { |
Sundarajan Srinivasan | 2fcaa98 | 2013-10-31 17:44:02 -0700 | [diff] [blame] | 290 | dprintf(CRITICAL, "ucs_do_scsi_read: failed\n"); |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 291 | return -UFS_FAILURE; |
| 292 | } |
| 293 | |
| 294 | buf += bytes_to_transfer; |
| 295 | start_blk += SCSI_MAX_DATA_TRANS_BLK_LEN; |
| 296 | } |
| 297 | |
| 298 | return UFS_SUCCESS; |
| 299 | } |
| 300 | |
| 301 | int ucs_do_scsi_write(struct ufs_dev *dev, struct scsi_rdwr_req *req) |
| 302 | { |
| 303 | struct scsi_req_build_type req_upiu; |
| 304 | STACKBUF_DMA_ALIGN(cdb, sizeof(struct scsi_rdwr_cdb)); |
| 305 | struct scsi_rdwr_cdb *cdb_param; |
| 306 | uint32_t blks_remaining; |
| 307 | uint16_t blks_to_transfer; |
| 308 | uint64_t bytes_to_transfer; |
| 309 | uint32_t start_blk; |
| 310 | uint32_t buf; |
| 311 | |
| 312 | blks_remaining = req->num_blocks; |
| 313 | buf = req->data_buffer_base; |
| 314 | start_blk = req->start_lba; |
| 315 | |
| 316 | cdb_param = (struct scsi_rdwr_cdb*) cdb; |
| 317 | while (blks_remaining) |
| 318 | { |
| 319 | if (blks_remaining <= SCSI_MAX_DATA_TRANS_BLK_LEN) |
| 320 | { |
| 321 | blks_to_transfer = blks_remaining; |
| 322 | blks_remaining = 0; |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | blks_to_transfer = SCSI_MAX_DATA_TRANS_BLK_LEN; |
| 327 | blks_remaining -= SCSI_MAX_DATA_TRANS_BLK_LEN; |
| 328 | } |
| 329 | |
| 330 | bytes_to_transfer = blks_to_transfer * UFS_DEFAULT_SECTORE_SIZE; |
| 331 | |
| 332 | memset(cdb_param, 0, sizeof(struct scsi_rdwr_cdb)); |
| 333 | cdb_param->opcode = SCSI_CMD_WRITE10; |
| 334 | cdb_param->cdb1 = SCSI_READ_WRITE_10_CDB1(0, 0, 1, 0); |
| 335 | cdb_param->lba = BE32(start_blk); |
| 336 | cdb_param->trans_len = BE16(blks_to_transfer); |
| 337 | |
| 338 | /* Flush cdb to memory. */ |
| 339 | dsb(); |
| 340 | arch_clean_invalidate_cache_range((addr_t) cdb_param, sizeof(struct scsi_rdwr_cdb)); |
| 341 | |
| 342 | memset(&req_upiu, 0 , sizeof(struct scsi_req_build_type)); |
| 343 | |
| 344 | req_upiu.cdb = (addr_t) cdb_param; |
| 345 | req_upiu.data_buffer_addr = buf; |
| 346 | req_upiu.data_len = bytes_to_transfer; |
| 347 | req_upiu.flags = UPIU_FLAGS_WRITE; |
| 348 | req_upiu.lun = req->lun; |
| 349 | req_upiu.dd = UTRD_SYSTEM_TO_TARGET; |
| 350 | |
| 351 | if (ucs_do_scsi_cmd(dev, &req_upiu)) |
| 352 | { |
Sundarajan Srinivasan | 2fcaa98 | 2013-10-31 17:44:02 -0700 | [diff] [blame] | 353 | dprintf(CRITICAL, "ucs_do_scsi_write: failed\n"); |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 354 | return -UFS_FAILURE; |
| 355 | } |
| 356 | |
| 357 | buf += bytes_to_transfer; |
| 358 | start_blk += SCSI_MAX_DATA_TRANS_BLK_LEN; |
| 359 | } |
| 360 | |
| 361 | return UFS_SUCCESS; |
| 362 | } |
| 363 | |
Sundarajan Srinivasan | 54380a3 | 2013-10-25 17:38:38 -0700 | [diff] [blame] | 364 | int ucs_do_scsi_unmap(struct ufs_dev *dev, struct scsi_unmap_req *req) |
| 365 | { |
| 366 | STACKBUF_DMA_ALIGN(cdb_param, SCSI_CDB_PARAM_LEN); |
| 367 | STACKBUF_DMA_ALIGN(param, sizeof(struct unmap_param_list)); |
| 368 | struct scsi_req_build_type req_upiu; |
| 369 | struct unmap_param_list *param_list; |
| 370 | struct unmap_blk_desc *blk_desc; |
| 371 | |
| 372 | param_list = (struct unmap_param_list *)param; |
Sridhar Parasuram | c3946bb | 2015-02-02 10:34:50 -0800 | [diff] [blame] | 373 | |
| 374 | // data length = size of unmap block descriptor struct (n-1) - size of data length field. |
| 375 | param_list->data_len = ((sizeof(struct unmap_param_list) - 1) - 1) << 0x8; |
Sundarajan Srinivasan | 54380a3 | 2013-10-25 17:38:38 -0700 | [diff] [blame] | 376 | |
| 377 | param_list->blk_desc_data_len = sizeof(struct unmap_blk_desc) << 0x8; |
| 378 | |
| 379 | |
| 380 | blk_desc = &(param_list->blk_desc); |
| 381 | |
| 382 | blk_desc->lba = BE64(req->start_lba); |
| 383 | blk_desc->num_blks = BE32(req->num_blocks); |
| 384 | |
| 385 | memset((void*)cdb_param, 0, SCSI_CDB_PARAM_LEN); |
| 386 | cdb_param[0] = SCSI_CMD_UNMAP; |
| 387 | cdb_param[1] = 0; /*ANCHOR = 0 for UFS*/ |
| 388 | cdb_param[6] = 0; /*Group No = 0*/ |
| 389 | cdb_param[7] = 0; /* Param list length is 1, we erase 1 contiguous blk*/ |
| 390 | cdb_param[8] = sizeof(struct unmap_param_list); |
| 391 | cdb_param[9] = 0; |
| 392 | |
| 393 | /* Flush cdb to memory. */ |
| 394 | dsb(); |
| 395 | arch_invalidate_cache_range((addr_t) cdb_param, SCSI_CDB_PARAM_LEN); |
Channagoud Kadabi | e2dd5a7 | 2015-02-13 20:10:47 -0800 | [diff] [blame] | 396 | arch_invalidate_cache_range((addr_t) param, sizeof(struct unmap_param_list)); |
Sundarajan Srinivasan | 54380a3 | 2013-10-25 17:38:38 -0700 | [diff] [blame] | 397 | |
| 398 | memset((void*)&req_upiu, 0 , sizeof(struct scsi_req_build_type)); |
| 399 | |
| 400 | req_upiu.cdb = (addr_t) cdb_param; |
| 401 | req_upiu.data_buffer_addr = (addr_t) param; |
| 402 | req_upiu.data_len = sizeof(struct unmap_param_list); |
| 403 | req_upiu.flags = UPIU_FLAGS_WRITE; |
Sundarajan Srinivasan | 332ce6a | 2013-12-04 17:27:46 -0800 | [diff] [blame] | 404 | req_upiu.lun = req->lun; |
Sundarajan Srinivasan | 54380a3 | 2013-10-25 17:38:38 -0700 | [diff] [blame] | 405 | req_upiu.dd = UTRD_SYSTEM_TO_TARGET; |
| 406 | |
| 407 | if (ucs_do_scsi_cmd(dev, &req_upiu)) |
| 408 | { |
| 409 | dprintf(CRITICAL, "Failed to send SCSI unmap command \n"); |
| 410 | return -UFS_FAILURE; |
| 411 | } |
| 412 | |
| 413 | /* Flush buffer. */ |
| 414 | arch_invalidate_cache_range((addr_t) param, SCSI_INQUIRY_LEN); |
| 415 | |
| 416 | return UFS_SUCCESS; |
| 417 | } |
| 418 | |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 419 | int ucs_scsi_send_inquiry(struct ufs_dev *dev) |
| 420 | { |
| 421 | STACKBUF_DMA_ALIGN(cdb_param, SCSI_CDB_PARAM_LEN); |
| 422 | STACKBUF_DMA_ALIGN(param, SCSI_INQUIRY_LEN); |
| 423 | struct scsi_req_build_type req_upiu; |
| 424 | |
| 425 | memset(cdb_param, 0, SCSI_CDB_PARAM_LEN); |
| 426 | cdb_param[0] = SCSI_CMD_INQUIRY; |
| 427 | cdb_param[3] = sizeof(param)>> 8; |
| 428 | cdb_param[4] = sizeof(param); |
| 429 | |
| 430 | /* Flush cdb to memory. */ |
| 431 | dsb(); |
Channagoud Kadabi | cf3afe4 | 2015-08-07 16:08:08 -0700 | [diff] [blame] | 432 | arch_clean_invalidate_cache_range((addr_t) cdb_param, SCSI_CDB_PARAM_LEN); |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 433 | |
| 434 | memset(&req_upiu, 0 , sizeof(struct scsi_req_build_type)); |
| 435 | |
| 436 | req_upiu.cdb = (addr_t) cdb_param; |
| 437 | req_upiu.data_buffer_addr = (addr_t) param; |
| 438 | req_upiu.data_len = SCSI_INQUIRY_LEN; |
| 439 | req_upiu.flags = UPIU_FLAGS_READ; |
| 440 | req_upiu.lun = 0; |
| 441 | req_upiu.dd = UTRD_TARGET_TO_SYSTEM; |
| 442 | |
| 443 | if (ucs_do_scsi_cmd(dev, &req_upiu)) |
| 444 | { |
| 445 | dprintf(CRITICAL, "Failed to send SCSI inquiry\n"); |
| 446 | return -UFS_FAILURE; |
| 447 | } |
| 448 | |
| 449 | /* Flush buffer. */ |
| 450 | arch_invalidate_cache_range((addr_t) param, SCSI_INQUIRY_LEN); |
| 451 | |
| 452 | return UFS_SUCCESS; |
| 453 | } |
| 454 | |
Sundarajan Srinivasan | 54380a3 | 2013-10-25 17:38:38 -0700 | [diff] [blame] | 455 | void dump_sense_buffer(uint8_t *buf, int buf_len) |
| 456 | { |
| 457 | int index=0; |
| 458 | |
| 459 | dprintf(CRITICAL,"----Sense buffer----\n"); |
| 460 | for(index=0; index < buf_len; index++) |
| 461 | dprintf(CRITICAL,"buf[%d] = %x\n",index, buf[index]); |
| 462 | |
| 463 | dprintf(CRITICAL,"----end of buffer---\n"); |
| 464 | } |
| 465 | |
Channagoud Kadabi | 5890d9b | 2015-01-29 13:04:04 -0800 | [diff] [blame] | 466 | int ucs_do_request_sense(struct ufs_dev *dev, uint8_t lun) |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 467 | { |
| 468 | STACKBUF_DMA_ALIGN(cdb, sizeof(struct scsi_sense_cdb)); |
| 469 | struct scsi_req_build_type req_upiu; |
| 470 | struct scsi_sense_cdb *cdb_param; |
| 471 | uint8_t buf[SCSI_SENSE_BUF_LEN]; |
| 472 | |
Sridhar Parasuram | c02d107 | 2014-11-06 12:55:42 -0800 | [diff] [blame] | 473 | cdb_param = (struct scsi_sense_cdb *) cdb; |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 474 | |
| 475 | memset(cdb, 0, sizeof(struct scsi_sense_cdb)); |
| 476 | |
| 477 | cdb_param->opcode = SCSI_CMD_SENSE_REQ; |
| 478 | cdb_param->alloc_len = SCSI_SENSE_BUF_LEN; |
| 479 | |
| 480 | /* Flush cdb to memory. */ |
| 481 | dsb(); |
Channagoud Kadabi | cf3afe4 | 2015-08-07 16:08:08 -0700 | [diff] [blame] | 482 | arch_clean_invalidate_cache_range((addr_t) cdb_param, SCSI_CDB_PARAM_LEN); |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 483 | |
| 484 | memset(&req_upiu, 0 , sizeof(struct scsi_req_build_type)); |
| 485 | |
| 486 | req_upiu.cdb = (addr_t) cdb_param; |
| 487 | req_upiu.data_buffer_addr = (addr_t) buf; |
| 488 | req_upiu.data_len = SCSI_SENSE_BUF_LEN; |
| 489 | req_upiu.flags = UPIU_FLAGS_READ; |
Channagoud Kadabi | 5890d9b | 2015-01-29 13:04:04 -0800 | [diff] [blame] | 490 | req_upiu.lun = lun; |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 491 | req_upiu.dd = UTRD_TARGET_TO_SYSTEM; |
| 492 | |
| 493 | if (ucs_do_scsi_cmd(dev, &req_upiu)) |
| 494 | { |
Sundarajan Srinivasan | 2fcaa98 | 2013-10-31 17:44:02 -0700 | [diff] [blame] | 495 | dprintf(CRITICAL, "ucs_do_request_sense: failed\n"); |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 496 | return -UFS_FAILURE; |
| 497 | } |
| 498 | |
| 499 | /* Flush buffer. */ |
| 500 | arch_invalidate_cache_range((addr_t) buf, SCSI_INQUIRY_LEN); |
| 501 | |
Channagoud Kadabi | 5890d9b | 2015-01-29 13:04:04 -0800 | [diff] [blame] | 502 | #if DEBUG_UFS |
Sundarajan Srinivasan | 54380a3 | 2013-10-25 17:38:38 -0700 | [diff] [blame] | 503 | dump_sense_buffer(buf, SCSI_SENSE_BUF_LEN); |
Channagoud Kadabi | 5890d9b | 2015-01-29 13:04:04 -0800 | [diff] [blame] | 504 | #endif |
Sundarajan Srinivasan | 54380a3 | 2013-10-25 17:38:38 -0700 | [diff] [blame] | 505 | |
Deepa Dinamani | ab9c2b9 | 2013-09-12 11:30:58 -0700 | [diff] [blame] | 506 | return UFS_SUCCESS; |
| 507 | } |