1 /* 2 * Virtio-SCSI implementation for s390 machine loader for qemu 3 * 4 * Copyright 2015 IBM Corp. 5 * Author: Eugene "jno" Dvurechenski <jno@linux.vnet.ibm.com> 6 * 7 * This work is licensed under the terms of the GNU GPL, version 2 or (at 8 * your option) any later version. See the COPYING file in the top-level 9 * directory. 10 */ 11 12 #include "s390-ccw.h" 13 #include "virtio.h" 14 #include "scsi.h" 15 #include "virtio-scsi.h" 16 17 static ScsiDevice default_scsi_device; 18 static VirtioScsiCmdReq req; 19 static VirtioScsiCmdResp resp; 20 21 static uint8_t scsi_inquiry_std_response[256]; 22 static ScsiInquiryEvpdPages scsi_inquiry_evpd_pages_response; 23 static ScsiInquiryEvpdBl scsi_inquiry_evpd_bl_response; 24 25 static inline void vs_assert(bool term, const char **msgs) 26 { 27 if (!term) { 28 int i = 0; 29 30 sclp_print("\n! "); 31 while (msgs[i]) { 32 sclp_print(msgs[i++]); 33 } 34 panic(" !\n"); 35 } 36 } 37 38 static void virtio_scsi_verify_response(VirtioScsiCmdResp *resp, 39 const char *title) 40 { 41 const char *mr[] = { 42 title, ": response ", virtio_scsi_response_msg(resp), 0 43 }; 44 const char *ms[] = { 45 title, 46 CDB_STATUS_VALID(resp->status) ? ": " : ": invalid ", 47 scsi_cdb_status_msg(resp->status), 48 resp->status == CDB_STATUS_CHECK_CONDITION ? " " : 0, 49 resp->sense_len ? scsi_cdb_asc_msg(resp->sense) 50 : "no sense data", 51 scsi_sense_response(resp->sense) == 0x70 ? ", sure" : "?", 52 0 53 }; 54 55 vs_assert(resp->response == VIRTIO_SCSI_S_OK, mr); 56 vs_assert(resp->status == CDB_STATUS_GOOD, ms); 57 } 58 59 static void prepare_request(VDev *vdev, const void *cdb, int cdb_size, 60 void *data, uint32_t data_size) 61 { 62 const ScsiDevice *sdev = vdev->scsi_device; 63 64 memset(&req, 0, sizeof(req)); 65 req.lun = make_lun(sdev->channel, sdev->target, sdev->lun); 66 memcpy(&req.cdb, cdb, cdb_size); 67 68 memset(&resp, 0, sizeof(resp)); 69 resp.status = 0xff; /* set invalid */ 70 resp.response = 0xff; /* */ 71 72 if (data && data_size) { 73 memset(data, 0, data_size); 74 } 75 } 76 77 static inline void vs_io_assert(bool term, const char *msg) 78 { 79 if (!term) { 80 virtio_scsi_verify_response(&resp, msg); 81 } 82 } 83 84 static void vs_run(const char *title, VirtioCmd *cmd, VDev *vdev, 85 const void *cdb, int cdb_size, 86 void *data, uint32_t data_size) 87 { 88 prepare_request(vdev, cdb, cdb_size, data, data_size); 89 vs_io_assert(virtio_run(vdev, VR_REQUEST, cmd) == 0, title); 90 } 91 92 /* SCSI protocol implementation routines */ 93 94 static bool scsi_inquiry(VDev *vdev, uint8_t evpd, uint8_t page, 95 void *data, uint32_t data_size) 96 { 97 ScsiCdbInquiry cdb = { 98 .command = 0x12, 99 .b1 = evpd, 100 .b2 = page, 101 .alloc_len = data_size < 65535 ? data_size : 65535, 102 }; 103 VirtioCmd inquiry[] = { 104 { &req, sizeof(req), VRING_DESC_F_NEXT }, 105 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT }, 106 { data, data_size, VRING_DESC_F_WRITE }, 107 }; 108 109 vs_run("inquiry", inquiry, vdev, &cdb, sizeof(cdb), data, data_size); 110 111 return virtio_scsi_response_ok(&resp); 112 } 113 114 static bool scsi_test_unit_ready(VDev *vdev) 115 { 116 ScsiCdbTestUnitReady cdb = { 117 .command = 0x00, 118 }; 119 VirtioCmd test_unit_ready[] = { 120 { &req, sizeof(req), VRING_DESC_F_NEXT }, 121 { &resp, sizeof(resp), VRING_DESC_F_WRITE }, 122 }; 123 124 prepare_request(vdev, &cdb, sizeof(cdb), 0, 0); 125 virtio_run(vdev, VR_REQUEST, test_unit_ready); /* ignore errors here */ 126 127 return virtio_scsi_response_ok(&resp); 128 } 129 130 static bool scsi_report_luns(VDev *vdev, void *data, uint32_t data_size) 131 { 132 ScsiCdbReportLuns cdb = { 133 .command = 0xa0, 134 .select_report = 0x02, /* REPORT ALL */ 135 .alloc_len = data_size, 136 }; 137 VirtioCmd report_luns[] = { 138 { &req, sizeof(req), VRING_DESC_F_NEXT }, 139 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT }, 140 { data, data_size, VRING_DESC_F_WRITE }, 141 }; 142 143 vs_run("report luns", report_luns, 144 vdev, &cdb, sizeof(cdb), data, data_size); 145 146 return virtio_scsi_response_ok(&resp); 147 } 148 149 static bool scsi_read_10(VDev *vdev, 150 ulong sector, int sectors, void *data, 151 unsigned int data_size) 152 { 153 ScsiCdbRead10 cdb = { 154 .command = 0x28, 155 .lba = sector, 156 .xfer_length = sectors, 157 }; 158 VirtioCmd read_10[] = { 159 { &req, sizeof(req), VRING_DESC_F_NEXT }, 160 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT }, 161 { data, data_size, VRING_DESC_F_WRITE }, 162 }; 163 164 debug_print_int("read_10 sector", sector); 165 debug_print_int("read_10 sectors", sectors); 166 167 vs_run("read(10)", read_10, vdev, &cdb, sizeof(cdb), data, data_size); 168 169 return virtio_scsi_response_ok(&resp); 170 } 171 172 static bool scsi_read_capacity(VDev *vdev, 173 void *data, uint32_t data_size) 174 { 175 ScsiCdbReadCapacity16 cdb = { 176 .command = 0x9e, /* SERVICE_ACTION_IN_16 */ 177 .service_action = 0x10, /* SA_READ_CAPACITY */ 178 .alloc_len = data_size, 179 }; 180 VirtioCmd read_capacity_16[] = { 181 { &req, sizeof(req), VRING_DESC_F_NEXT }, 182 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT }, 183 { data, data_size, VRING_DESC_F_WRITE }, 184 }; 185 186 vs_run("read capacity", read_capacity_16, 187 vdev, &cdb, sizeof(cdb), data, data_size); 188 189 return virtio_scsi_response_ok(&resp); 190 } 191 192 /* virtio-scsi routines */ 193 194 static void virtio_scsi_locate_device(VDev *vdev) 195 { 196 const uint16_t channel = 0; /* again, it's what QEMU does */ 197 uint16_t target; 198 static uint8_t data[16 + 8 * 63]; 199 ScsiLunReport *r = (void *) data; 200 ScsiDevice *sdev = vdev->scsi_device; 201 int i, luns; 202 203 /* QEMU has hardcoded channel #0 in many places. 204 * If this hardcoded value is ever changed, we'll need to add code for 205 * vdev->config.scsi.max_channel != 0 here. 206 */ 207 debug_print_int("config.scsi.max_channel", vdev->config.scsi.max_channel); 208 debug_print_int("config.scsi.max_target ", vdev->config.scsi.max_target); 209 debug_print_int("config.scsi.max_lun ", vdev->config.scsi.max_lun); 210 debug_print_int("config.scsi.max_sectors", vdev->config.scsi.max_sectors); 211 212 if (vdev->scsi_device_selected) { 213 sdev->channel = vdev->selected_scsi_device.channel; 214 sdev->target = vdev->selected_scsi_device.target; 215 sdev->lun = vdev->selected_scsi_device.lun; 216 217 IPL_check(sdev->channel == 0, "non-zero channel requested"); 218 IPL_check(sdev->target <= vdev->config.scsi.max_target, "target# high"); 219 IPL_check(sdev->lun <= vdev->config.scsi.max_lun, "LUN# high"); 220 return; 221 } 222 223 for (target = 0; target <= vdev->config.scsi.max_target; target++) { 224 sdev->channel = channel; 225 sdev->target = target; /* sdev->lun will be 0 here */ 226 if (!scsi_report_luns(vdev, data, sizeof(data))) { 227 if (resp.response == VIRTIO_SCSI_S_BAD_TARGET) { 228 continue; 229 } 230 print_int("target", target); 231 virtio_scsi_verify_response(&resp, "SCSI cannot report LUNs"); 232 } 233 if (r->lun_list_len == 0) { 234 print_int("no LUNs for target", target); 235 continue; 236 } 237 luns = r->lun_list_len / 8; 238 debug_print_int("LUNs reported", luns); 239 if (luns == 1) { 240 /* There is no ",lun=#" arg for -device or ",lun=0" given. 241 * Hence, the only LUN reported. 242 * Usually, it's 0. 243 */ 244 sdev->lun = r->lun[0].v16[0]; /* it's returned this way */ 245 debug_print_int("Have to use LUN", sdev->lun); 246 return; /* we have to use this device */ 247 } 248 for (i = 0; i < luns; i++) { 249 if (r->lun[i].v64) { 250 /* Look for non-zero LUN - we have where to choose from */ 251 sdev->lun = r->lun[i].v16[0]; 252 debug_print_int("Will use LUN", sdev->lun); 253 return; /* we have found a device */ 254 } 255 } 256 } 257 panic("\n! Cannot locate virtio-scsi device !\n"); 258 } 259 260 int virtio_scsi_read_many(VDev *vdev, 261 ulong sector, void *load_addr, int sec_num) 262 { 263 int sector_count; 264 int f = vdev->blk_factor; 265 unsigned int data_size; 266 unsigned int max_transfer = MIN_NON_ZERO(vdev->config.scsi.max_sectors, 267 vdev->max_transfer); 268 269 do { 270 sector_count = MIN_NON_ZERO(sec_num, max_transfer); 271 data_size = sector_count * virtio_get_block_size() * f; 272 if (!scsi_read_10(vdev, sector * f, sector_count * f, load_addr, 273 data_size)) { 274 virtio_scsi_verify_response(&resp, "virtio-scsi:read_many"); 275 } 276 load_addr += data_size; 277 sector += sector_count; 278 sec_num -= sector_count; 279 } while (sec_num > 0); 280 281 return 0; 282 } 283 284 static bool virtio_scsi_inquiry_response_is_cdrom(void *data) 285 { 286 const ScsiInquiryStd *response = data; 287 const int resp_data_fmt = response->b3 & 0x0f; 288 int i; 289 290 IPL_check(resp_data_fmt == 2, "Wrong INQUIRY response format"); 291 if (resp_data_fmt != 2) { 292 return false; /* cannot decode */ 293 } 294 295 if ((response->peripheral_qdt & 0x1f) == SCSI_INQ_RDT_CDROM) { 296 return true; 297 } 298 299 for (i = 0; i < sizeof(response->prod_id); i++) { 300 if (response->prod_id[i] != QEMU_CDROM_SIGNATURE[i]) { 301 return false; 302 } 303 } 304 return true; 305 } 306 307 static void scsi_parse_capacity_report(void *data, 308 uint64_t *last_lba, uint32_t *lb_len) 309 { 310 ScsiReadCapacity16Data *p = data; 311 312 if (last_lba) { 313 *last_lba = p->ret_lba; 314 } 315 316 if (lb_len) { 317 *lb_len = p->lb_len; 318 } 319 } 320 321 void virtio_scsi_setup(VDev *vdev) 322 { 323 int retry_test_unit_ready = 3; 324 uint8_t data[256]; 325 uint32_t data_size = sizeof(data); 326 ScsiInquiryEvpdPages *evpd = &scsi_inquiry_evpd_pages_response; 327 ScsiInquiryEvpdBl *evpd_bl = &scsi_inquiry_evpd_bl_response; 328 int i; 329 330 vdev->scsi_device = &default_scsi_device; 331 virtio_scsi_locate_device(vdev); 332 333 /* We have to "ping" the device before it becomes readable */ 334 while (!scsi_test_unit_ready(vdev)) { 335 336 if (!virtio_scsi_response_ok(&resp)) { 337 uint8_t code = resp.sense[0] & SCSI_SENSE_CODE_MASK; 338 uint8_t sense_key = resp.sense[2] & SCSI_SENSE_KEY_MASK; 339 340 IPL_assert(resp.sense_len != 0, "virtio-scsi:setup: no SENSE data"); 341 342 IPL_assert(retry_test_unit_ready && code == 0x70 && 343 sense_key == SCSI_SENSE_KEY_UNIT_ATTENTION, 344 "virtio-scsi:setup: cannot retry"); 345 346 /* retry on CHECK_CONDITION/UNIT_ATTENTION as it 347 * may not designate a real error, but it may be 348 * a result of device reset, etc. 349 */ 350 retry_test_unit_ready--; 351 sleep(1); 352 continue; 353 } 354 355 virtio_scsi_verify_response(&resp, "virtio-scsi:setup"); 356 } 357 358 /* read and cache SCSI INQUIRY response */ 359 if (!scsi_inquiry(vdev, 360 SCSI_INQUIRY_STANDARD, 361 SCSI_INQUIRY_STANDARD_NONE, 362 scsi_inquiry_std_response, 363 sizeof(scsi_inquiry_std_response))) { 364 virtio_scsi_verify_response(&resp, "virtio-scsi:setup:inquiry"); 365 } 366 367 if (virtio_scsi_inquiry_response_is_cdrom(scsi_inquiry_std_response)) { 368 sclp_print("SCSI CD-ROM detected.\n"); 369 vdev->is_cdrom = true; 370 vdev->scsi_block_size = VIRTIO_ISO_BLOCK_SIZE; 371 } 372 373 if (!scsi_inquiry(vdev, 374 SCSI_INQUIRY_EVPD, 375 SCSI_INQUIRY_EVPD_SUPPORTED_PAGES, 376 evpd, 377 sizeof(*evpd))) { 378 virtio_scsi_verify_response(&resp, "virtio-scsi:setup:supported_pages"); 379 } 380 381 debug_print_int("EVPD length", evpd->page_length); 382 383 for (i = 0; i <= evpd->page_length; i++) { 384 debug_print_int("supported EVPD page", evpd->byte[i]); 385 386 if (evpd->byte[i] != SCSI_INQUIRY_EVPD_BLOCK_LIMITS) { 387 continue; 388 } 389 390 if (!scsi_inquiry(vdev, 391 SCSI_INQUIRY_EVPD, 392 SCSI_INQUIRY_EVPD_BLOCK_LIMITS, 393 evpd_bl, 394 sizeof(*evpd_bl))) { 395 virtio_scsi_verify_response(&resp, "virtio-scsi:setup:blocklimits"); 396 } 397 398 debug_print_int("max transfer", evpd_bl->max_transfer); 399 vdev->max_transfer = evpd_bl->max_transfer; 400 } 401 402 /* 403 * The host sg driver will often be unhappy with particularly large 404 * I/Os that exceed the block iovec limits. Let's enforce something 405 * reasonable, despite what the device configuration tells us. 406 */ 407 408 vdev->max_transfer = MIN_NON_ZERO(VIRTIO_SCSI_MAX_SECTORS, 409 vdev->max_transfer); 410 411 if (!scsi_read_capacity(vdev, data, data_size)) { 412 virtio_scsi_verify_response(&resp, "virtio-scsi:setup:read_capacity"); 413 } 414 scsi_parse_capacity_report(data, &vdev->scsi_last_block, 415 (uint32_t *) &vdev->scsi_block_size); 416 } 417