diff options
Diffstat (limited to 'drivers/scsi/sd.c')
-rw-r--r-- | drivers/scsi/sd.c | 291 |
1 files changed, 228 insertions, 63 deletions
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 0c63947d8a9d..e5e7d7856454 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c | |||
@@ -99,8 +99,7 @@ static void scsi_disk_release(struct device *cdev); | |||
99 | static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *); | 99 | static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *); |
100 | static void sd_print_result(struct scsi_disk *, int); | 100 | static void sd_print_result(struct scsi_disk *, int); |
101 | 101 | ||
102 | static DEFINE_IDR(sd_index_idr); | 102 | static DEFINE_IDA(sd_index_ida); |
103 | static DEFINE_SPINLOCK(sd_index_lock); | ||
104 | 103 | ||
105 | /* This semaphore is used to mediate the 0->1 reference get in the | 104 | /* This semaphore is used to mediate the 0->1 reference get in the |
106 | * face of object destruction (i.e. we can't allow a get on an | 105 | * face of object destruction (i.e. we can't allow a get on an |
@@ -234,6 +233,24 @@ sd_show_allow_restart(struct device *dev, struct device_attribute *attr, | |||
234 | return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart); | 233 | return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart); |
235 | } | 234 | } |
236 | 235 | ||
236 | static ssize_t | ||
237 | sd_show_protection_type(struct device *dev, struct device_attribute *attr, | ||
238 | char *buf) | ||
239 | { | ||
240 | struct scsi_disk *sdkp = to_scsi_disk(dev); | ||
241 | |||
242 | return snprintf(buf, 20, "%u\n", sdkp->protection_type); | ||
243 | } | ||
244 | |||
245 | static ssize_t | ||
246 | sd_show_app_tag_own(struct device *dev, struct device_attribute *attr, | ||
247 | char *buf) | ||
248 | { | ||
249 | struct scsi_disk *sdkp = to_scsi_disk(dev); | ||
250 | |||
251 | return snprintf(buf, 20, "%u\n", sdkp->ATO); | ||
252 | } | ||
253 | |||
237 | static struct device_attribute sd_disk_attrs[] = { | 254 | static struct device_attribute sd_disk_attrs[] = { |
238 | __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type, | 255 | __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type, |
239 | sd_store_cache_type), | 256 | sd_store_cache_type), |
@@ -242,6 +259,8 @@ static struct device_attribute sd_disk_attrs[] = { | |||
242 | sd_store_allow_restart), | 259 | sd_store_allow_restart), |
243 | __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop, | 260 | __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop, |
244 | sd_store_manage_start_stop), | 261 | sd_store_manage_start_stop), |
262 | __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL), | ||
263 | __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL), | ||
245 | __ATTR_NULL, | 264 | __ATTR_NULL, |
246 | }; | 265 | }; |
247 | 266 | ||
@@ -354,7 +373,9 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq) | |||
354 | struct scsi_cmnd *SCpnt; | 373 | struct scsi_cmnd *SCpnt; |
355 | struct scsi_device *sdp = q->queuedata; | 374 | struct scsi_device *sdp = q->queuedata; |
356 | struct gendisk *disk = rq->rq_disk; | 375 | struct gendisk *disk = rq->rq_disk; |
376 | struct scsi_disk *sdkp; | ||
357 | sector_t block = rq->sector; | 377 | sector_t block = rq->sector; |
378 | sector_t threshold; | ||
358 | unsigned int this_count = rq->nr_sectors; | 379 | unsigned int this_count = rq->nr_sectors; |
359 | unsigned int timeout = sdp->timeout; | 380 | unsigned int timeout = sdp->timeout; |
360 | int ret; | 381 | int ret; |
@@ -370,6 +391,7 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq) | |||
370 | if (ret != BLKPREP_OK) | 391 | if (ret != BLKPREP_OK) |
371 | goto out; | 392 | goto out; |
372 | SCpnt = rq->special; | 393 | SCpnt = rq->special; |
394 | sdkp = scsi_disk(disk); | ||
373 | 395 | ||
374 | /* from here on until we're complete, any goto out | 396 | /* from here on until we're complete, any goto out |
375 | * is used for a killable error condition */ | 397 | * is used for a killable error condition */ |
@@ -401,13 +423,21 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq) | |||
401 | } | 423 | } |
402 | 424 | ||
403 | /* | 425 | /* |
404 | * Some devices (some sdcards for one) don't like it if the | 426 | * Some SD card readers can't handle multi-sector accesses which touch |
405 | * last sector gets read in a larger then 1 sector read. | 427 | * the last one or two hardware sectors. Split accesses as needed. |
406 | */ | 428 | */ |
407 | if (unlikely(sdp->last_sector_bug && | 429 | threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS * |
408 | rq->nr_sectors > sdp->sector_size / 512 && | 430 | (sdp->sector_size / 512); |
409 | block + this_count == get_capacity(disk))) | 431 | |
410 | this_count -= sdp->sector_size / 512; | 432 | if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) { |
433 | if (block < threshold) { | ||
434 | /* Access up to the threshold but not beyond */ | ||
435 | this_count = threshold - block; | ||
436 | } else { | ||
437 | /* Access only a single hardware sector */ | ||
438 | this_count = sdp->sector_size / 512; | ||
439 | } | ||
440 | } | ||
411 | 441 | ||
412 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n", | 442 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n", |
413 | (unsigned long long)block)); | 443 | (unsigned long long)block)); |
@@ -459,6 +489,11 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq) | |||
459 | } | 489 | } |
460 | SCpnt->cmnd[0] = WRITE_6; | 490 | SCpnt->cmnd[0] = WRITE_6; |
461 | SCpnt->sc_data_direction = DMA_TO_DEVICE; | 491 | SCpnt->sc_data_direction = DMA_TO_DEVICE; |
492 | |||
493 | if (blk_integrity_rq(rq) && | ||
494 | sd_dif_prepare(rq, block, sdp->sector_size) == -EIO) | ||
495 | goto out; | ||
496 | |||
462 | } else if (rq_data_dir(rq) == READ) { | 497 | } else if (rq_data_dir(rq) == READ) { |
463 | SCpnt->cmnd[0] = READ_6; | 498 | SCpnt->cmnd[0] = READ_6; |
464 | SCpnt->sc_data_direction = DMA_FROM_DEVICE; | 499 | SCpnt->sc_data_direction = DMA_FROM_DEVICE; |
@@ -473,8 +508,12 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq) | |||
473 | "writing" : "reading", this_count, | 508 | "writing" : "reading", this_count, |
474 | rq->nr_sectors)); | 509 | rq->nr_sectors)); |
475 | 510 | ||
476 | SCpnt->cmnd[1] = 0; | 511 | /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */ |
477 | 512 | if (scsi_host_dif_capable(sdp->host, sdkp->protection_type)) | |
513 | SCpnt->cmnd[1] = 1 << 5; | ||
514 | else | ||
515 | SCpnt->cmnd[1] = 0; | ||
516 | |||
478 | if (block > 0xffffffff) { | 517 | if (block > 0xffffffff) { |
479 | SCpnt->cmnd[0] += READ_16 - READ_6; | 518 | SCpnt->cmnd[0] += READ_16 - READ_6; |
480 | SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0; | 519 | SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0; |
@@ -492,6 +531,7 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq) | |||
492 | SCpnt->cmnd[13] = (unsigned char) this_count & 0xff; | 531 | SCpnt->cmnd[13] = (unsigned char) this_count & 0xff; |
493 | SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0; | 532 | SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0; |
494 | } else if ((this_count > 0xff) || (block > 0x1fffff) || | 533 | } else if ((this_count > 0xff) || (block > 0x1fffff) || |
534 | scsi_device_protection(SCpnt->device) || | ||
495 | SCpnt->device->use_10_for_rw) { | 535 | SCpnt->device->use_10_for_rw) { |
496 | if (this_count > 0xffff) | 536 | if (this_count > 0xffff) |
497 | this_count = 0xffff; | 537 | this_count = 0xffff; |
@@ -526,6 +566,10 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq) | |||
526 | } | 566 | } |
527 | SCpnt->sdb.length = this_count * sdp->sector_size; | 567 | SCpnt->sdb.length = this_count * sdp->sector_size; |
528 | 568 | ||
569 | /* If DIF or DIX is enabled, tell HBA how to handle request */ | ||
570 | if (sdkp->protection_type || scsi_prot_sg_count(SCpnt)) | ||
571 | sd_dif_op(SCpnt, sdkp->protection_type, scsi_prot_sg_count(SCpnt)); | ||
572 | |||
529 | /* | 573 | /* |
530 | * We shouldn't disconnect in the middle of a sector, so with a dumb | 574 | * We shouldn't disconnect in the middle of a sector, so with a dumb |
531 | * host adapter, it's safe to assume that we can at least transfer | 575 | * host adapter, it's safe to assume that we can at least transfer |
@@ -920,6 +964,48 @@ static struct block_device_operations sd_fops = { | |||
920 | .revalidate_disk = sd_revalidate_disk, | 964 | .revalidate_disk = sd_revalidate_disk, |
921 | }; | 965 | }; |
922 | 966 | ||
967 | static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd) | ||
968 | { | ||
969 | u64 start_lba = scmd->request->sector; | ||
970 | u64 end_lba = scmd->request->sector + (scsi_bufflen(scmd) / 512); | ||
971 | u64 bad_lba; | ||
972 | int info_valid; | ||
973 | |||
974 | if (!blk_fs_request(scmd->request)) | ||
975 | return 0; | ||
976 | |||
977 | info_valid = scsi_get_sense_info_fld(scmd->sense_buffer, | ||
978 | SCSI_SENSE_BUFFERSIZE, | ||
979 | &bad_lba); | ||
980 | if (!info_valid) | ||
981 | return 0; | ||
982 | |||
983 | if (scsi_bufflen(scmd) <= scmd->device->sector_size) | ||
984 | return 0; | ||
985 | |||
986 | if (scmd->device->sector_size < 512) { | ||
987 | /* only legitimate sector_size here is 256 */ | ||
988 | start_lba <<= 1; | ||
989 | end_lba <<= 1; | ||
990 | } else { | ||
991 | /* be careful ... don't want any overflows */ | ||
992 | u64 factor = scmd->device->sector_size / 512; | ||
993 | do_div(start_lba, factor); | ||
994 | do_div(end_lba, factor); | ||
995 | } | ||
996 | |||
997 | /* The bad lba was reported incorrectly, we have no idea where | ||
998 | * the error is. | ||
999 | */ | ||
1000 | if (bad_lba < start_lba || bad_lba >= end_lba) | ||
1001 | return 0; | ||
1002 | |||
1003 | /* This computation should always be done in terms of | ||
1004 | * the resolution of the device's medium. | ||
1005 | */ | ||
1006 | return (bad_lba - start_lba) * scmd->device->sector_size; | ||
1007 | } | ||
1008 | |||
923 | /** | 1009 | /** |
924 | * sd_done - bottom half handler: called when the lower level | 1010 | * sd_done - bottom half handler: called when the lower level |
925 | * driver has completed (successfully or otherwise) a scsi command. | 1011 | * driver has completed (successfully or otherwise) a scsi command. |
@@ -930,15 +1016,10 @@ static struct block_device_operations sd_fops = { | |||
930 | static int sd_done(struct scsi_cmnd *SCpnt) | 1016 | static int sd_done(struct scsi_cmnd *SCpnt) |
931 | { | 1017 | { |
932 | int result = SCpnt->result; | 1018 | int result = SCpnt->result; |
933 | unsigned int xfer_size = scsi_bufflen(SCpnt); | 1019 | unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt); |
934 | unsigned int good_bytes = result ? 0 : xfer_size; | ||
935 | u64 start_lba = SCpnt->request->sector; | ||
936 | u64 end_lba = SCpnt->request->sector + (xfer_size / 512); | ||
937 | u64 bad_lba; | ||
938 | struct scsi_sense_hdr sshdr; | 1020 | struct scsi_sense_hdr sshdr; |
939 | int sense_valid = 0; | 1021 | int sense_valid = 0; |
940 | int sense_deferred = 0; | 1022 | int sense_deferred = 0; |
941 | int info_valid; | ||
942 | 1023 | ||
943 | if (result) { | 1024 | if (result) { |
944 | sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr); | 1025 | sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr); |
@@ -963,36 +1044,7 @@ static int sd_done(struct scsi_cmnd *SCpnt) | |||
963 | switch (sshdr.sense_key) { | 1044 | switch (sshdr.sense_key) { |
964 | case HARDWARE_ERROR: | 1045 | case HARDWARE_ERROR: |
965 | case MEDIUM_ERROR: | 1046 | case MEDIUM_ERROR: |
966 | if (!blk_fs_request(SCpnt->request)) | 1047 | good_bytes = sd_completed_bytes(SCpnt); |
967 | goto out; | ||
968 | info_valid = scsi_get_sense_info_fld(SCpnt->sense_buffer, | ||
969 | SCSI_SENSE_BUFFERSIZE, | ||
970 | &bad_lba); | ||
971 | if (!info_valid) | ||
972 | goto out; | ||
973 | if (xfer_size <= SCpnt->device->sector_size) | ||
974 | goto out; | ||
975 | if (SCpnt->device->sector_size < 512) { | ||
976 | /* only legitimate sector_size here is 256 */ | ||
977 | start_lba <<= 1; | ||
978 | end_lba <<= 1; | ||
979 | } else { | ||
980 | /* be careful ... don't want any overflows */ | ||
981 | u64 factor = SCpnt->device->sector_size / 512; | ||
982 | do_div(start_lba, factor); | ||
983 | do_div(end_lba, factor); | ||
984 | } | ||
985 | |||
986 | if (bad_lba < start_lba || bad_lba >= end_lba) | ||
987 | /* the bad lba was reported incorrectly, we have | ||
988 | * no idea where the error is | ||
989 | */ | ||
990 | goto out; | ||
991 | |||
992 | /* This computation should always be done in terms of | ||
993 | * the resolution of the device's medium. | ||
994 | */ | ||
995 | good_bytes = (bad_lba - start_lba)*SCpnt->device->sector_size; | ||
996 | break; | 1048 | break; |
997 | case RECOVERED_ERROR: | 1049 | case RECOVERED_ERROR: |
998 | case NO_SENSE: | 1050 | case NO_SENSE: |
@@ -1002,10 +1054,23 @@ static int sd_done(struct scsi_cmnd *SCpnt) | |||
1002 | scsi_print_sense("sd", SCpnt); | 1054 | scsi_print_sense("sd", SCpnt); |
1003 | SCpnt->result = 0; | 1055 | SCpnt->result = 0; |
1004 | memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); | 1056 | memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); |
1005 | good_bytes = xfer_size; | 1057 | good_bytes = scsi_bufflen(SCpnt); |
1058 | break; | ||
1059 | case ABORTED_COMMAND: | ||
1060 | if (sshdr.asc == 0x10) { /* DIF: Disk detected corruption */ | ||
1061 | scsi_print_result(SCpnt); | ||
1062 | scsi_print_sense("sd", SCpnt); | ||
1063 | good_bytes = sd_completed_bytes(SCpnt); | ||
1064 | } | ||
1006 | break; | 1065 | break; |
1007 | case ILLEGAL_REQUEST: | 1066 | case ILLEGAL_REQUEST: |
1008 | if (SCpnt->device->use_10_for_rw && | 1067 | if (sshdr.asc == 0x10) { /* DIX: HBA detected corruption */ |
1068 | scsi_print_result(SCpnt); | ||
1069 | scsi_print_sense("sd", SCpnt); | ||
1070 | good_bytes = sd_completed_bytes(SCpnt); | ||
1071 | } | ||
1072 | if (!scsi_device_protection(SCpnt->device) && | ||
1073 | SCpnt->device->use_10_for_rw && | ||
1009 | (SCpnt->cmnd[0] == READ_10 || | 1074 | (SCpnt->cmnd[0] == READ_10 || |
1010 | SCpnt->cmnd[0] == WRITE_10)) | 1075 | SCpnt->cmnd[0] == WRITE_10)) |
1011 | SCpnt->device->use_10_for_rw = 0; | 1076 | SCpnt->device->use_10_for_rw = 0; |
@@ -1018,6 +1083,9 @@ static int sd_done(struct scsi_cmnd *SCpnt) | |||
1018 | break; | 1083 | break; |
1019 | } | 1084 | } |
1020 | out: | 1085 | out: |
1086 | if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt)) | ||
1087 | sd_dif_complete(SCpnt, good_bytes); | ||
1088 | |||
1021 | return good_bytes; | 1089 | return good_bytes; |
1022 | } | 1090 | } |
1023 | 1091 | ||
@@ -1165,6 +1233,49 @@ sd_spinup_disk(struct scsi_disk *sdkp) | |||
1165 | } | 1233 | } |
1166 | } | 1234 | } |
1167 | 1235 | ||
1236 | |||
1237 | /* | ||
1238 | * Determine whether disk supports Data Integrity Field. | ||
1239 | */ | ||
1240 | void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer) | ||
1241 | { | ||
1242 | struct scsi_device *sdp = sdkp->device; | ||
1243 | u8 type; | ||
1244 | |||
1245 | if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0) | ||
1246 | type = 0; | ||
1247 | else | ||
1248 | type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */ | ||
1249 | |||
1250 | switch (type) { | ||
1251 | case SD_DIF_TYPE0_PROTECTION: | ||
1252 | sdkp->protection_type = 0; | ||
1253 | break; | ||
1254 | |||
1255 | case SD_DIF_TYPE1_PROTECTION: | ||
1256 | case SD_DIF_TYPE3_PROTECTION: | ||
1257 | sdkp->protection_type = type; | ||
1258 | break; | ||
1259 | |||
1260 | case SD_DIF_TYPE2_PROTECTION: | ||
1261 | sd_printk(KERN_ERR, sdkp, "formatted with DIF Type 2 " \ | ||
1262 | "protection which is currently unsupported. " \ | ||
1263 | "Disabling disk!\n"); | ||
1264 | goto disable; | ||
1265 | |||
1266 | default: | ||
1267 | sd_printk(KERN_ERR, sdkp, "formatted with unknown " \ | ||
1268 | "protection type %d. Disabling disk!\n", type); | ||
1269 | goto disable; | ||
1270 | } | ||
1271 | |||
1272 | return; | ||
1273 | |||
1274 | disable: | ||
1275 | sdkp->protection_type = 0; | ||
1276 | sdkp->capacity = 0; | ||
1277 | } | ||
1278 | |||
1168 | /* | 1279 | /* |
1169 | * read disk capacity | 1280 | * read disk capacity |
1170 | */ | 1281 | */ |
@@ -1174,7 +1285,8 @@ sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer) | |||
1174 | unsigned char cmd[16]; | 1285 | unsigned char cmd[16]; |
1175 | int the_result, retries; | 1286 | int the_result, retries; |
1176 | int sector_size = 0; | 1287 | int sector_size = 0; |
1177 | int longrc = 0; | 1288 | /* Force READ CAPACITY(16) when PROTECT=1 */ |
1289 | int longrc = scsi_device_protection(sdkp->device) ? 1 : 0; | ||
1178 | struct scsi_sense_hdr sshdr; | 1290 | struct scsi_sense_hdr sshdr; |
1179 | int sense_valid = 0; | 1291 | int sense_valid = 0; |
1180 | struct scsi_device *sdp = sdkp->device; | 1292 | struct scsi_device *sdp = sdkp->device; |
@@ -1186,8 +1298,8 @@ repeat: | |||
1186 | memset((void *) cmd, 0, 16); | 1298 | memset((void *) cmd, 0, 16); |
1187 | cmd[0] = SERVICE_ACTION_IN; | 1299 | cmd[0] = SERVICE_ACTION_IN; |
1188 | cmd[1] = SAI_READ_CAPACITY_16; | 1300 | cmd[1] = SAI_READ_CAPACITY_16; |
1189 | cmd[13] = 12; | 1301 | cmd[13] = 13; |
1190 | memset((void *) buffer, 0, 12); | 1302 | memset((void *) buffer, 0, 13); |
1191 | } else { | 1303 | } else { |
1192 | cmd[0] = READ_CAPACITY; | 1304 | cmd[0] = READ_CAPACITY; |
1193 | memset((void *) &cmd[1], 0, 9); | 1305 | memset((void *) &cmd[1], 0, 9); |
@@ -1195,7 +1307,7 @@ repeat: | |||
1195 | } | 1307 | } |
1196 | 1308 | ||
1197 | the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, | 1309 | the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, |
1198 | buffer, longrc ? 12 : 8, &sshdr, | 1310 | buffer, longrc ? 13 : 8, &sshdr, |
1199 | SD_TIMEOUT, SD_MAX_RETRIES); | 1311 | SD_TIMEOUT, SD_MAX_RETRIES); |
1200 | 1312 | ||
1201 | if (media_not_present(sdkp, &sshdr)) | 1313 | if (media_not_present(sdkp, &sshdr)) |
@@ -1270,6 +1382,8 @@ repeat: | |||
1270 | 1382 | ||
1271 | sector_size = (buffer[8] << 24) | | 1383 | sector_size = (buffer[8] << 24) | |
1272 | (buffer[9] << 16) | (buffer[10] << 8) | buffer[11]; | 1384 | (buffer[9] << 16) | (buffer[10] << 8) | buffer[11]; |
1385 | |||
1386 | sd_read_protection_type(sdkp, buffer); | ||
1273 | } | 1387 | } |
1274 | 1388 | ||
1275 | /* Some devices return the total number of sectors, not the | 1389 | /* Some devices return the total number of sectors, not the |
@@ -1531,6 +1645,52 @@ defaults: | |||
1531 | sdkp->DPOFUA = 0; | 1645 | sdkp->DPOFUA = 0; |
1532 | } | 1646 | } |
1533 | 1647 | ||
1648 | /* | ||
1649 | * The ATO bit indicates whether the DIF application tag is available | ||
1650 | * for use by the operating system. | ||
1651 | */ | ||
1652 | void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer) | ||
1653 | { | ||
1654 | int res, offset; | ||
1655 | struct scsi_device *sdp = sdkp->device; | ||
1656 | struct scsi_mode_data data; | ||
1657 | struct scsi_sense_hdr sshdr; | ||
1658 | |||
1659 | if (sdp->type != TYPE_DISK) | ||
1660 | return; | ||
1661 | |||
1662 | if (sdkp->protection_type == 0) | ||
1663 | return; | ||
1664 | |||
1665 | res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT, | ||
1666 | SD_MAX_RETRIES, &data, &sshdr); | ||
1667 | |||
1668 | if (!scsi_status_is_good(res) || !data.header_length || | ||
1669 | data.length < 6) { | ||
1670 | sd_printk(KERN_WARNING, sdkp, | ||
1671 | "getting Control mode page failed, assume no ATO\n"); | ||
1672 | |||
1673 | if (scsi_sense_valid(&sshdr)) | ||
1674 | sd_print_sense_hdr(sdkp, &sshdr); | ||
1675 | |||
1676 | return; | ||
1677 | } | ||
1678 | |||
1679 | offset = data.header_length + data.block_descriptor_length; | ||
1680 | |||
1681 | if ((buffer[offset] & 0x3f) != 0x0a) { | ||
1682 | sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n"); | ||
1683 | return; | ||
1684 | } | ||
1685 | |||
1686 | if ((buffer[offset + 5] & 0x80) == 0) | ||
1687 | return; | ||
1688 | |||
1689 | sdkp->ATO = 1; | ||
1690 | |||
1691 | return; | ||
1692 | } | ||
1693 | |||
1534 | /** | 1694 | /** |
1535 | * sd_revalidate_disk - called the first time a new disk is seen, | 1695 | * sd_revalidate_disk - called the first time a new disk is seen, |
1536 | * performs disk spin up, read_capacity, etc. | 1696 | * performs disk spin up, read_capacity, etc. |
@@ -1567,6 +1727,7 @@ static int sd_revalidate_disk(struct gendisk *disk) | |||
1567 | sdkp->write_prot = 0; | 1727 | sdkp->write_prot = 0; |
1568 | sdkp->WCE = 0; | 1728 | sdkp->WCE = 0; |
1569 | sdkp->RCD = 0; | 1729 | sdkp->RCD = 0; |
1730 | sdkp->ATO = 0; | ||
1570 | 1731 | ||
1571 | sd_spinup_disk(sdkp); | 1732 | sd_spinup_disk(sdkp); |
1572 | 1733 | ||
@@ -1578,6 +1739,7 @@ static int sd_revalidate_disk(struct gendisk *disk) | |||
1578 | sd_read_capacity(sdkp, buffer); | 1739 | sd_read_capacity(sdkp, buffer); |
1579 | sd_read_write_protect_flag(sdkp, buffer); | 1740 | sd_read_write_protect_flag(sdkp, buffer); |
1580 | sd_read_cache_type(sdkp, buffer); | 1741 | sd_read_cache_type(sdkp, buffer); |
1742 | sd_read_app_tag_own(sdkp, buffer); | ||
1581 | } | 1743 | } |
1582 | 1744 | ||
1583 | /* | 1745 | /* |
@@ -1643,18 +1805,20 @@ static int sd_probe(struct device *dev) | |||
1643 | if (!gd) | 1805 | if (!gd) |
1644 | goto out_free; | 1806 | goto out_free; |
1645 | 1807 | ||
1646 | if (!idr_pre_get(&sd_index_idr, GFP_KERNEL)) | 1808 | do { |
1647 | goto out_put; | 1809 | if (!ida_pre_get(&sd_index_ida, GFP_KERNEL)) |
1810 | goto out_put; | ||
1648 | 1811 | ||
1649 | spin_lock(&sd_index_lock); | 1812 | error = ida_get_new(&sd_index_ida, &index); |
1650 | error = idr_get_new(&sd_index_idr, NULL, &index); | 1813 | } while (error == -EAGAIN); |
1651 | spin_unlock(&sd_index_lock); | ||
1652 | 1814 | ||
1653 | if (index >= SD_MAX_DISKS) | ||
1654 | error = -EBUSY; | ||
1655 | if (error) | 1815 | if (error) |
1656 | goto out_put; | 1816 | goto out_put; |
1657 | 1817 | ||
1818 | error = -EBUSY; | ||
1819 | if (index >= SD_MAX_DISKS) | ||
1820 | goto out_free_index; | ||
1821 | |||
1658 | sdkp->device = sdp; | 1822 | sdkp->device = sdp; |
1659 | sdkp->driver = &sd_template; | 1823 | sdkp->driver = &sd_template; |
1660 | sdkp->disk = gd; | 1824 | sdkp->disk = gd; |
@@ -1675,7 +1839,7 @@ static int sd_probe(struct device *dev) | |||
1675 | strncpy(sdkp->dev.bus_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE); | 1839 | strncpy(sdkp->dev.bus_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE); |
1676 | 1840 | ||
1677 | if (device_add(&sdkp->dev)) | 1841 | if (device_add(&sdkp->dev)) |
1678 | goto out_put; | 1842 | goto out_free_index; |
1679 | 1843 | ||
1680 | get_device(&sdp->sdev_gendev); | 1844 | get_device(&sdp->sdev_gendev); |
1681 | 1845 | ||
@@ -1711,12 +1875,15 @@ static int sd_probe(struct device *dev) | |||
1711 | 1875 | ||
1712 | dev_set_drvdata(dev, sdkp); | 1876 | dev_set_drvdata(dev, sdkp); |
1713 | add_disk(gd); | 1877 | add_disk(gd); |
1878 | sd_dif_config_host(sdkp); | ||
1714 | 1879 | ||
1715 | sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n", | 1880 | sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n", |
1716 | sdp->removable ? "removable " : ""); | 1881 | sdp->removable ? "removable " : ""); |
1717 | 1882 | ||
1718 | return 0; | 1883 | return 0; |
1719 | 1884 | ||
1885 | out_free_index: | ||
1886 | ida_remove(&sd_index_ida, index); | ||
1720 | out_put: | 1887 | out_put: |
1721 | put_disk(gd); | 1888 | put_disk(gd); |
1722 | out_free: | 1889 | out_free: |
@@ -1766,9 +1933,7 @@ static void scsi_disk_release(struct device *dev) | |||
1766 | struct scsi_disk *sdkp = to_scsi_disk(dev); | 1933 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
1767 | struct gendisk *disk = sdkp->disk; | 1934 | struct gendisk *disk = sdkp->disk; |
1768 | 1935 | ||
1769 | spin_lock(&sd_index_lock); | 1936 | ida_remove(&sd_index_ida, sdkp->index); |
1770 | idr_remove(&sd_index_idr, sdkp->index); | ||
1771 | spin_unlock(&sd_index_lock); | ||
1772 | 1937 | ||
1773 | disk->private_data = NULL; | 1938 | disk->private_data = NULL; |
1774 | put_disk(disk); | 1939 | put_disk(disk); |