sd_zbc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SCSI Zoned Block commands
  4. *
  5. * Copyright (C) 2014-2015 SUSE Linux GmbH
  6. * Written by: Hannes Reinecke <hare@suse.de>
  7. * Modified by: Damien Le Moal <damien.lemoal@hgst.com>
  8. * Modified by: Shaun Tancheff <shaun.tancheff@seagate.com>
  9. */
  10. #include <linux/blkdev.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/sched/mm.h>
  13. #include <linux/mutex.h>
  14. #include <linux/unaligned.h>
  15. #include <scsi/scsi.h>
  16. #include <scsi/scsi_cmnd.h>
  17. #include "sd.h"
  18. #define CREATE_TRACE_POINTS
  19. #include "sd_trace.h"
  20. /* Whether or not a SCSI zone descriptor describes a gap zone. */
  21. static bool sd_zbc_is_gap_zone(const u8 buf[64])
  22. {
  23. return (buf[0] & 0xf) == ZBC_ZONE_TYPE_GAP;
  24. }
  25. /**
  26. * sd_zbc_parse_report - Parse a SCSI zone descriptor
  27. * @sdkp: SCSI disk pointer.
  28. * @buf: SCSI zone descriptor.
  29. * @idx: Index of the zone relative to the first zone reported by the current
  30. * sd_zbc_report_zones() call.
  31. * @args: report zones arguments (callback, etc)
  32. *
  33. * Return: Value returned by @cb.
  34. *
  35. * Convert a SCSI zone descriptor into struct blk_zone format. Additionally,
  36. * call @cb(blk_zone, @data).
  37. */
  38. static int sd_zbc_parse_report(struct scsi_disk *sdkp, const u8 buf[64],
  39. unsigned int idx, struct blk_report_zones_args *args)
  40. {
  41. struct scsi_device *sdp = sdkp->device;
  42. struct blk_zone zone = { 0 };
  43. sector_t start_lba, gran;
  44. if (WARN_ON_ONCE(sd_zbc_is_gap_zone(buf)))
  45. return -EINVAL;
  46. zone.type = buf[0] & 0x0f;
  47. zone.cond = (buf[1] >> 4) & 0xf;
  48. if (buf[1] & 0x01)
  49. zone.reset = 1;
  50. if (buf[1] & 0x02)
  51. zone.non_seq = 1;
  52. start_lba = get_unaligned_be64(&buf[16]);
  53. zone.start = logical_to_sectors(sdp, start_lba);
  54. zone.capacity = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
  55. zone.len = zone.capacity;
  56. if (sdkp->zone_starting_lba_gran) {
  57. gran = logical_to_sectors(sdp, sdkp->zone_starting_lba_gran);
  58. if (zone.len > gran) {
  59. sd_printk(KERN_ERR, sdkp,
  60. "Invalid zone at LBA %llu with capacity %llu and length %llu; granularity = %llu\n",
  61. start_lba,
  62. sectors_to_logical(sdp, zone.capacity),
  63. sectors_to_logical(sdp, zone.len),
  64. sectors_to_logical(sdp, gran));
  65. return -EINVAL;
  66. }
  67. /*
  68. * Use the starting LBA granularity instead of the zone length
  69. * obtained from the REPORT ZONES command.
  70. */
  71. zone.len = gran;
  72. }
  73. if (zone.cond == ZBC_ZONE_COND_FULL)
  74. zone.wp = zone.start + zone.len;
  75. else
  76. zone.wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
  77. return disk_report_zone(sdkp->disk, &zone, idx, args);
  78. }
  79. /**
  80. * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
  81. * @sdkp: The target disk
  82. * @buf: vmalloc-ed buffer to use for the reply
  83. * @buflen: the buffer size
  84. * @lba: Start LBA of the report
  85. * @partial: Do partial report
  86. *
  87. * For internal use during device validation.
  88. * Using partial=true can significantly speed up execution of a report zones
  89. * command because the disk does not have to count all possible report matching
  90. * zones and will only report the count of zones fitting in the command reply
  91. * buffer.
  92. */
  93. static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
  94. unsigned int buflen, sector_t lba,
  95. bool partial)
  96. {
  97. struct scsi_device *sdp = sdkp->device;
  98. const int timeout = sdp->request_queue->rq_timeout;
  99. struct scsi_sense_hdr sshdr;
  100. const struct scsi_exec_args exec_args = {
  101. .sshdr = &sshdr,
  102. };
  103. unsigned char cmd[16];
  104. unsigned int rep_len;
  105. int result;
  106. memset(cmd, 0, 16);
  107. cmd[0] = ZBC_IN;
  108. cmd[1] = ZI_REPORT_ZONES;
  109. put_unaligned_be64(lba, &cmd[2]);
  110. put_unaligned_be32(buflen, &cmd[10]);
  111. if (partial)
  112. cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
  113. result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, buf, buflen,
  114. timeout, SD_MAX_RETRIES, &exec_args);
  115. if (result) {
  116. sd_printk(KERN_ERR, sdkp,
  117. "REPORT ZONES start lba %llu failed\n", lba);
  118. sd_print_result(sdkp, "REPORT ZONES", result);
  119. if (result > 0 && scsi_sense_valid(&sshdr))
  120. sd_print_sense_hdr(sdkp, &sshdr);
  121. return -EIO;
  122. }
  123. rep_len = get_unaligned_be32(&buf[0]);
  124. if (rep_len < 64) {
  125. sd_printk(KERN_ERR, sdkp,
  126. "REPORT ZONES report invalid length %u\n",
  127. rep_len);
  128. return -EIO;
  129. }
  130. return 0;
  131. }
  132. /**
  133. * sd_zbc_alloc_report_buffer() - Allocate a buffer for report zones reply.
  134. * @sdkp: The target disk
  135. * @nr_zones: Maximum number of zones to report
  136. * @buflen: Size of the buffer allocated
  137. *
  138. * Try to allocate a reply buffer for the number of requested zones.
  139. * The size of the buffer allocated may be smaller than requested to
  140. * satify the device constraint (max_hw_sectors, max_segments, etc).
  141. *
  142. * Return the address of the allocated buffer and update @buflen with
  143. * the size of the allocated buffer.
  144. */
  145. static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
  146. unsigned int nr_zones, size_t *buflen)
  147. {
  148. struct request_queue *q = sdkp->disk->queue;
  149. unsigned int max_segments;
  150. size_t bufsize;
  151. void *buf;
  152. /*
  153. * Report zone buffer size should be at most 64B times the number of
  154. * zones requested plus the 64B reply header, but should be aligned
  155. * to SECTOR_SIZE for ATA devices.
  156. * Make sure that this size does not exceed the hardware capabilities.
  157. * Furthermore, since the report zone command cannot be split, make
  158. * sure that the allocated buffer can always be mapped by limiting the
  159. * number of pages allocated to the HBA max segments limit.
  160. * Since max segments can be larger than the max inline bio vectors,
  161. * further limit the allocated buffer to BIO_MAX_INLINE_VECS.
  162. */
  163. nr_zones = min(nr_zones, sdkp->zone_info.nr_zones);
  164. bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE);
  165. bufsize = min_t(size_t, bufsize,
  166. queue_max_hw_sectors(q) << SECTOR_SHIFT);
  167. max_segments = min(BIO_MAX_INLINE_VECS, queue_max_segments(q));
  168. bufsize = min_t(size_t, bufsize, max_segments << PAGE_SHIFT);
  169. while (bufsize >= SECTOR_SIZE) {
  170. buf = kvzalloc(bufsize, GFP_KERNEL | __GFP_NORETRY);
  171. if (buf) {
  172. *buflen = bufsize;
  173. return buf;
  174. }
  175. bufsize = rounddown(bufsize >> 1, SECTOR_SIZE);
  176. }
  177. return NULL;
  178. }
  179. /**
  180. * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
  181. * @sdkp: The target disk
  182. */
  183. static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
  184. {
  185. return logical_to_sectors(sdkp->device, sdkp->zone_info.zone_blocks);
  186. }
  187. /**
  188. * sd_zbc_report_zones - SCSI .report_zones() callback.
  189. * @disk: Disk to report zones for.
  190. * @sector: Start sector.
  191. * @nr_zones: Maximum number of zones to report.
  192. * @args: Callback arguments.
  193. *
  194. * Called by the block layer to iterate over zone information. See also the
  195. * disk->fops->report_zones() calls in block/blk-zoned.c.
  196. */
  197. int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
  198. unsigned int nr_zones,
  199. struct blk_report_zones_args *args)
  200. {
  201. struct scsi_disk *sdkp = scsi_disk(disk);
  202. sector_t lba = sectors_to_logical(sdkp->device, sector);
  203. unsigned int nr, i;
  204. unsigned char *buf;
  205. u64 zone_length, start_lba;
  206. size_t offset, buflen = 0;
  207. int zone_idx = 0;
  208. int ret;
  209. if (sdkp->device->type != TYPE_ZBC)
  210. /* Not a zoned device */
  211. return -EOPNOTSUPP;
  212. if (!sdkp->capacity)
  213. /* Device gone or invalid */
  214. return -ENODEV;
  215. buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen);
  216. if (!buf)
  217. return -ENOMEM;
  218. while (zone_idx < nr_zones && lba < sdkp->capacity) {
  219. ret = sd_zbc_do_report_zones(sdkp, buf, buflen, lba, true);
  220. if (ret)
  221. goto out;
  222. offset = 0;
  223. nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64);
  224. if (!nr)
  225. break;
  226. for (i = 0; i < nr && zone_idx < nr_zones; i++) {
  227. offset += 64;
  228. start_lba = get_unaligned_be64(&buf[offset + 16]);
  229. zone_length = get_unaligned_be64(&buf[offset + 8]);
  230. if ((zone_idx == 0 &&
  231. (lba < start_lba ||
  232. lba >= start_lba + zone_length)) ||
  233. (zone_idx > 0 && start_lba != lba) ||
  234. start_lba + zone_length < start_lba) {
  235. sd_printk(KERN_ERR, sdkp,
  236. "Zone %d at LBA %llu is invalid: %llu + %llu\n",
  237. zone_idx, lba, start_lba, zone_length);
  238. ret = -EINVAL;
  239. goto out;
  240. }
  241. lba = start_lba + zone_length;
  242. if (sd_zbc_is_gap_zone(&buf[offset])) {
  243. if (sdkp->zone_starting_lba_gran)
  244. continue;
  245. sd_printk(KERN_ERR, sdkp,
  246. "Gap zone without constant LBA offsets\n");
  247. ret = -EINVAL;
  248. goto out;
  249. }
  250. ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx,
  251. args);
  252. if (ret)
  253. goto out;
  254. zone_idx++;
  255. }
  256. }
  257. ret = zone_idx;
  258. out:
  259. kvfree(buf);
  260. return ret;
  261. }
  262. static blk_status_t sd_zbc_cmnd_checks(struct scsi_cmnd *cmd)
  263. {
  264. struct request *rq = scsi_cmd_to_rq(cmd);
  265. struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
  266. sector_t sector = blk_rq_pos(rq);
  267. if (sdkp->device->type != TYPE_ZBC)
  268. /* Not a zoned device */
  269. return BLK_STS_IOERR;
  270. if (sdkp->device->changed)
  271. return BLK_STS_IOERR;
  272. if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
  273. /* Unaligned request */
  274. return BLK_STS_IOERR;
  275. return BLK_STS_OK;
  276. }
  277. /**
  278. * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations
  279. * can be RESET WRITE POINTER, OPEN, CLOSE or FINISH.
  280. * @cmd: the command to setup
  281. * @op: Operation to be performed
  282. * @all: All zones control
  283. *
  284. * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL,
  285. * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests.
  286. */
  287. blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
  288. unsigned char op, bool all)
  289. {
  290. struct request *rq = scsi_cmd_to_rq(cmd);
  291. sector_t sector = blk_rq_pos(rq);
  292. struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
  293. sector_t block = sectors_to_logical(sdkp->device, sector);
  294. blk_status_t ret;
  295. ret = sd_zbc_cmnd_checks(cmd);
  296. if (ret != BLK_STS_OK)
  297. return ret;
  298. cmd->cmd_len = 16;
  299. memset(cmd->cmnd, 0, cmd->cmd_len);
  300. cmd->cmnd[0] = ZBC_OUT;
  301. cmd->cmnd[1] = op;
  302. if (all)
  303. cmd->cmnd[14] = 0x1;
  304. else
  305. put_unaligned_be64(block, &cmd->cmnd[2]);
  306. rq->timeout = SD_TIMEOUT;
  307. cmd->sc_data_direction = DMA_NONE;
  308. cmd->transfersize = 0;
  309. cmd->allowed = 0;
  310. return BLK_STS_OK;
  311. }
  312. /**
  313. * sd_zbc_complete - ZBC command post processing.
  314. * @cmd: Completed command
  315. * @good_bytes: Command reply bytes
  316. * @sshdr: command sense header
  317. *
  318. * Called from sd_done() to handle zone commands errors and updates to the
  319. * device queue zone write pointer offset cahce.
  320. */
  321. unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
  322. struct scsi_sense_hdr *sshdr)
  323. {
  324. int result = cmd->result;
  325. struct request *rq = scsi_cmd_to_rq(cmd);
  326. if (op_is_zone_mgmt(req_op(rq)) &&
  327. result &&
  328. sshdr->sense_key == ILLEGAL_REQUEST &&
  329. sshdr->asc == 0x24) {
  330. /*
  331. * INVALID FIELD IN CDB error: a zone management command was
  332. * attempted on a conventional zone. Nothing to worry about,
  333. * so be quiet about the error.
  334. */
  335. rq->rq_flags |= RQF_QUIET;
  336. }
  337. return good_bytes;
  338. }
  339. /**
  340. * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
  341. * @sdkp: Target disk
  342. * @buf: Buffer where to store the VPD page data
  343. *
  344. * Read VPD page B6, get information and check that reads are unconstrained.
  345. */
  346. static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
  347. unsigned char *buf)
  348. {
  349. u64 zone_starting_lba_gran;
  350. if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
  351. sd_printk(KERN_NOTICE, sdkp,
  352. "Read zoned characteristics VPD page failed\n");
  353. return -ENODEV;
  354. }
  355. if (sdkp->device->type != TYPE_ZBC) {
  356. /* Host-aware */
  357. sdkp->urswrz = 1;
  358. sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
  359. sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
  360. sdkp->zones_max_open = 0;
  361. return 0;
  362. }
  363. /* Host-managed */
  364. sdkp->urswrz = buf[4] & 1;
  365. sdkp->zones_optimal_open = 0;
  366. sdkp->zones_optimal_nonseq = 0;
  367. sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
  368. /* Check zone alignment method */
  369. switch (buf[23] & 0xf) {
  370. case 0:
  371. case ZBC_CONSTANT_ZONE_LENGTH:
  372. /* Use zone length */
  373. break;
  374. case ZBC_CONSTANT_ZONE_START_OFFSET:
  375. zone_starting_lba_gran = get_unaligned_be64(&buf[24]);
  376. if (zone_starting_lba_gran == 0 ||
  377. !is_power_of_2(zone_starting_lba_gran) ||
  378. logical_to_sectors(sdkp->device, zone_starting_lba_gran) >
  379. UINT_MAX) {
  380. sd_printk(KERN_ERR, sdkp,
  381. "Invalid zone starting LBA granularity %llu\n",
  382. zone_starting_lba_gran);
  383. return -ENODEV;
  384. }
  385. sdkp->zone_starting_lba_gran = zone_starting_lba_gran;
  386. break;
  387. default:
  388. sd_printk(KERN_ERR, sdkp, "Invalid zone alignment method\n");
  389. return -ENODEV;
  390. }
  391. /*
  392. * Check for unconstrained reads: host-managed devices with
  393. * constrained reads (drives failing read after write pointer)
  394. * are not supported.
  395. */
  396. if (!sdkp->urswrz) {
  397. if (sdkp->first_scan)
  398. sd_printk(KERN_NOTICE, sdkp,
  399. "constrained reads devices are not supported\n");
  400. return -ENODEV;
  401. }
  402. return 0;
  403. }
  404. /**
  405. * sd_zbc_check_capacity - Check the device capacity
  406. * @sdkp: Target disk
  407. * @buf: command buffer
  408. * @zblocks: zone size in logical blocks
  409. *
  410. * Get the device zone size and check that the device capacity as reported
  411. * by READ CAPACITY matches the max_lba value (plus one) of the report zones
  412. * command reply for devices with RC_BASIS == 0.
  413. *
  414. * Returns 0 upon success or an error code upon failure.
  415. */
  416. static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
  417. u32 *zblocks)
  418. {
  419. u64 zone_blocks;
  420. sector_t max_lba;
  421. unsigned char *rec;
  422. int ret;
  423. /* Do a report zone to get max_lba and the size of the first zone */
  424. ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);
  425. if (ret)
  426. return ret;
  427. if (sdkp->rc_basis == 0) {
  428. /* The max_lba field is the capacity of this device */
  429. max_lba = get_unaligned_be64(&buf[8]);
  430. if (sdkp->capacity != max_lba + 1) {
  431. if (sdkp->first_scan)
  432. sd_printk(KERN_WARNING, sdkp,
  433. "Changing capacity from %llu to max LBA+1 %llu\n",
  434. (unsigned long long)sdkp->capacity,
  435. (unsigned long long)max_lba + 1);
  436. sdkp->capacity = max_lba + 1;
  437. }
  438. }
  439. if (sdkp->zone_starting_lba_gran == 0) {
  440. /* Get the size of the first reported zone */
  441. rec = buf + 64;
  442. zone_blocks = get_unaligned_be64(&rec[8]);
  443. if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
  444. if (sdkp->first_scan)
  445. sd_printk(KERN_NOTICE, sdkp,
  446. "Zone size too large\n");
  447. return -EFBIG;
  448. }
  449. } else {
  450. zone_blocks = sdkp->zone_starting_lba_gran;
  451. }
  452. if (!is_power_of_2(zone_blocks)) {
  453. sd_printk(KERN_ERR, sdkp,
  454. "Zone size %llu is not a power of two.\n",
  455. zone_blocks);
  456. return -EINVAL;
  457. }
  458. *zblocks = zone_blocks;
  459. return 0;
  460. }
  461. static void sd_zbc_print_zones(struct scsi_disk *sdkp)
  462. {
  463. if (sdkp->device->type != TYPE_ZBC || !sdkp->capacity)
  464. return;
  465. if (sdkp->capacity & (sdkp->zone_info.zone_blocks - 1))
  466. sd_printk(KERN_NOTICE, sdkp,
  467. "%u zones of %u logical blocks + 1 runt zone\n",
  468. sdkp->zone_info.nr_zones - 1,
  469. sdkp->zone_info.zone_blocks);
  470. else
  471. sd_printk(KERN_NOTICE, sdkp,
  472. "%u zones of %u logical blocks\n",
  473. sdkp->zone_info.nr_zones,
  474. sdkp->zone_info.zone_blocks);
  475. }
  476. /*
  477. * Call blk_revalidate_disk_zones() if any of the zoned disk properties have
  478. * changed that make it necessary to call that function. Called by
  479. * sd_revalidate_disk() after the gendisk capacity has been set.
  480. */
  481. int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
  482. {
  483. struct gendisk *disk = sdkp->disk;
  484. struct request_queue *q = disk->queue;
  485. u32 zone_blocks = sdkp->early_zone_info.zone_blocks;
  486. unsigned int nr_zones = sdkp->early_zone_info.nr_zones;
  487. unsigned int flags;
  488. int ret;
  489. /*
  490. * There is nothing to do for regular disks, including host-aware disks
  491. * that have partitions.
  492. */
  493. if (!blk_queue_is_zoned(q))
  494. return 0;
  495. if (sdkp->zone_info.zone_blocks == zone_blocks &&
  496. sdkp->zone_info.nr_zones == nr_zones &&
  497. disk->nr_zones == nr_zones)
  498. return 0;
  499. sdkp->zone_info.zone_blocks = zone_blocks;
  500. sdkp->zone_info.nr_zones = nr_zones;
  501. flags = memalloc_noio_save();
  502. ret = blk_revalidate_disk_zones(disk);
  503. memalloc_noio_restore(flags);
  504. if (ret) {
  505. sdkp->zone_info = (struct zoned_disk_info){ };
  506. sdkp->capacity = 0;
  507. return ret;
  508. }
  509. sd_zbc_print_zones(sdkp);
  510. return 0;
  511. }
  512. /**
  513. * sd_zbc_read_zones - Read zone information and update the request queue
  514. * @sdkp: SCSI disk pointer.
  515. * @lim: queue limits to read into
  516. * @buf: 512 byte buffer used for storing SCSI command output.
  517. *
  518. * Read zone information and update the request queue zone characteristics and
  519. * also the zoned device information in *sdkp. Called by sd_revalidate_disk()
  520. * before the gendisk capacity has been set.
  521. */
  522. int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,
  523. u8 buf[SD_BUF_SIZE])
  524. {
  525. unsigned int nr_zones;
  526. u32 zone_blocks = 0;
  527. int ret;
  528. if (sdkp->device->type != TYPE_ZBC)
  529. return 0;
  530. lim->features |= BLK_FEAT_ZONED;
  531. /*
  532. * Per ZBC and ZAC specifications, writes in sequential write required
  533. * zones of host-managed devices must be aligned to the device physical
  534. * block size.
  535. */
  536. lim->zone_write_granularity = sdkp->physical_block_size;
  537. /* READ16/WRITE16/SYNC16 is mandatory for ZBC devices */
  538. sdkp->device->use_16_for_rw = 1;
  539. sdkp->device->use_10_for_rw = 0;
  540. sdkp->device->use_16_for_sync = 1;
  541. /* Check zoned block device characteristics (unconstrained reads) */
  542. ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
  543. if (ret)
  544. goto err;
  545. /* Check the device capacity reported by report zones */
  546. ret = sd_zbc_check_capacity(sdkp, buf, &zone_blocks);
  547. if (ret != 0)
  548. goto err;
  549. nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
  550. sdkp->early_zone_info.nr_zones = nr_zones;
  551. sdkp->early_zone_info.zone_blocks = zone_blocks;
  552. /* The drive satisfies the kernel restrictions: set it up */
  553. if (sdkp->zones_max_open == U32_MAX)
  554. lim->max_open_zones = 0;
  555. else
  556. lim->max_open_zones = sdkp->zones_max_open;
  557. lim->max_active_zones = 0;
  558. lim->chunk_sectors = logical_to_sectors(sdkp->device, zone_blocks);
  559. return 0;
  560. err:
  561. sdkp->capacity = 0;
  562. return ret;
  563. }