sd_zbc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. * SCSI Zoned Block commands
  3. *
  4. * Copyright (C) 2014-2015 SUSE Linux GmbH
  5. * Written by: Hannes Reinecke <hare@suse.de>
  6. * Modified by: Damien Le Moal <damien.lemoal@hgst.com>
  7. * Modified by: Shaun Tancheff <shaun.tancheff@seagate.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; see the file COPYING. If not, write to
  20. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  21. * USA.
  22. *
  23. */
  24. #include <linux/blkdev.h>
  25. #include <asm/unaligned.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include "sd.h"
  29. /**
  30. * sd_zbc_parse_report - Convert a zone descriptor to a struct blk_zone,
  31. * @sdkp: The disk the report originated from
  32. * @buf: Address of the report zone descriptor
  33. * @zone: the destination zone structure
  34. *
  35. * All LBA sized values are converted to 512B sectors unit.
  36. */
  37. static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
  38. struct blk_zone *zone)
  39. {
  40. struct scsi_device *sdp = sdkp->device;
  41. memset(zone, 0, sizeof(struct blk_zone));
  42. zone->type = buf[0] & 0x0f;
  43. zone->cond = (buf[1] >> 4) & 0xf;
  44. if (buf[1] & 0x01)
  45. zone->reset = 1;
  46. if (buf[1] & 0x02)
  47. zone->non_seq = 1;
  48. zone->len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
  49. zone->start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
  50. zone->wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
  51. if (zone->type != ZBC_ZONE_TYPE_CONV &&
  52. zone->cond == ZBC_ZONE_COND_FULL)
  53. zone->wp = zone->start + zone->len;
  54. }
  55. /**
  56. * sd_zbc_report_zones - Issue a REPORT ZONES scsi command.
  57. * @sdkp: The target disk
  58. * @buf: Buffer to use for the reply
  59. * @buflen: the buffer size
  60. * @lba: Start LBA of the report
  61. *
  62. * For internal use during device validation.
  63. */
  64. static int sd_zbc_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
  65. unsigned int buflen, sector_t lba)
  66. {
  67. struct scsi_device *sdp = sdkp->device;
  68. const int timeout = sdp->request_queue->rq_timeout;
  69. struct scsi_sense_hdr sshdr;
  70. unsigned char cmd[16];
  71. unsigned int rep_len;
  72. int result;
  73. memset(cmd, 0, 16);
  74. cmd[0] = ZBC_IN;
  75. cmd[1] = ZI_REPORT_ZONES;
  76. put_unaligned_be64(lba, &cmd[2]);
  77. put_unaligned_be32(buflen, &cmd[10]);
  78. memset(buf, 0, buflen);
  79. result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  80. buf, buflen, &sshdr,
  81. timeout, SD_MAX_RETRIES, NULL);
  82. if (result) {
  83. sd_printk(KERN_ERR, sdkp,
  84. "REPORT ZONES lba %llu failed with %d/%d\n",
  85. (unsigned long long)lba,
  86. host_byte(result), driver_byte(result));
  87. return -EIO;
  88. }
  89. rep_len = get_unaligned_be32(&buf[0]);
  90. if (rep_len < 64) {
  91. sd_printk(KERN_ERR, sdkp,
  92. "REPORT ZONES report invalid length %u\n",
  93. rep_len);
  94. return -EIO;
  95. }
  96. return 0;
  97. }
  98. /**
  99. * sd_zbc_setup_report_cmnd - Prepare a REPORT ZONES scsi command
  100. * @cmd: The command to setup
  101. *
  102. * Call in sd_init_command() for a REQ_OP_ZONE_REPORT request.
  103. */
  104. int sd_zbc_setup_report_cmnd(struct scsi_cmnd *cmd)
  105. {
  106. struct request *rq = cmd->request;
  107. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  108. sector_t lba, sector = blk_rq_pos(rq);
  109. unsigned int nr_bytes = blk_rq_bytes(rq);
  110. int ret;
  111. WARN_ON(nr_bytes == 0);
  112. if (!sd_is_zoned(sdkp))
  113. /* Not a zoned device */
  114. return BLKPREP_KILL;
  115. ret = scsi_init_io(cmd);
  116. if (ret != BLKPREP_OK)
  117. return ret;
  118. cmd->cmd_len = 16;
  119. memset(cmd->cmnd, 0, cmd->cmd_len);
  120. cmd->cmnd[0] = ZBC_IN;
  121. cmd->cmnd[1] = ZI_REPORT_ZONES;
  122. lba = sectors_to_logical(sdkp->device, sector);
  123. put_unaligned_be64(lba, &cmd->cmnd[2]);
  124. put_unaligned_be32(nr_bytes, &cmd->cmnd[10]);
  125. /* Do partial report for speeding things up */
  126. cmd->cmnd[14] = ZBC_REPORT_ZONE_PARTIAL;
  127. cmd->sc_data_direction = DMA_FROM_DEVICE;
  128. cmd->sdb.length = nr_bytes;
  129. cmd->transfersize = sdkp->device->sector_size;
  130. cmd->allowed = 0;
  131. return BLKPREP_OK;
  132. }
  133. /**
  134. * sd_zbc_report_zones_complete - Process a REPORT ZONES scsi command reply.
  135. * @scmd: The completed report zones command
  136. * @good_bytes: reply size in bytes
  137. *
  138. * Convert all reported zone descriptors to struct blk_zone. The conversion
  139. * is done in-place, directly in the request specified sg buffer.
  140. */
  141. static void sd_zbc_report_zones_complete(struct scsi_cmnd *scmd,
  142. unsigned int good_bytes)
  143. {
  144. struct request *rq = scmd->request;
  145. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  146. struct sg_mapping_iter miter;
  147. struct blk_zone_report_hdr hdr;
  148. struct blk_zone zone;
  149. unsigned int offset, bytes = 0;
  150. unsigned long flags;
  151. u8 *buf;
  152. if (good_bytes < 64)
  153. return;
  154. memset(&hdr, 0, sizeof(struct blk_zone_report_hdr));
  155. sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
  156. SG_MITER_TO_SG | SG_MITER_ATOMIC);
  157. local_irq_save(flags);
  158. while (sg_miter_next(&miter) && bytes < good_bytes) {
  159. buf = miter.addr;
  160. offset = 0;
  161. if (bytes == 0) {
  162. /* Set the report header */
  163. hdr.nr_zones = min_t(unsigned int,
  164. (good_bytes - 64) / 64,
  165. get_unaligned_be32(&buf[0]) / 64);
  166. memcpy(buf, &hdr, sizeof(struct blk_zone_report_hdr));
  167. offset += 64;
  168. bytes += 64;
  169. }
  170. /* Parse zone descriptors */
  171. while (offset < miter.length && hdr.nr_zones) {
  172. WARN_ON(offset > miter.length);
  173. buf = miter.addr + offset;
  174. sd_zbc_parse_report(sdkp, buf, &zone);
  175. memcpy(buf, &zone, sizeof(struct blk_zone));
  176. offset += 64;
  177. bytes += 64;
  178. hdr.nr_zones--;
  179. }
  180. if (!hdr.nr_zones)
  181. break;
  182. }
  183. sg_miter_stop(&miter);
  184. local_irq_restore(flags);
  185. }
  186. /**
  187. * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
  188. * @sdkp: The target disk
  189. */
  190. static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
  191. {
  192. return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
  193. }
  194. /**
  195. * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command.
  196. * @cmd: the command to setup
  197. *
  198. * Called from sd_init_command() for a REQ_OP_ZONE_RESET request.
  199. */
  200. int sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd)
  201. {
  202. struct request *rq = cmd->request;
  203. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  204. sector_t sector = blk_rq_pos(rq);
  205. sector_t block = sectors_to_logical(sdkp->device, sector);
  206. if (!sd_is_zoned(sdkp))
  207. /* Not a zoned device */
  208. return BLKPREP_KILL;
  209. if (sdkp->device->changed)
  210. return BLKPREP_KILL;
  211. if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
  212. /* Unaligned request */
  213. return BLKPREP_KILL;
  214. cmd->cmd_len = 16;
  215. memset(cmd->cmnd, 0, cmd->cmd_len);
  216. cmd->cmnd[0] = ZBC_OUT;
  217. cmd->cmnd[1] = ZO_RESET_WRITE_POINTER;
  218. put_unaligned_be64(block, &cmd->cmnd[2]);
  219. rq->timeout = SD_TIMEOUT;
  220. cmd->sc_data_direction = DMA_NONE;
  221. cmd->transfersize = 0;
  222. cmd->allowed = 0;
  223. return BLKPREP_OK;
  224. }
  225. /**
  226. * sd_zbc_complete - ZBC command post processing.
  227. * @cmd: Completed command
  228. * @good_bytes: Command reply bytes
  229. * @sshdr: command sense header
  230. *
  231. * Called from sd_done(). Process report zones reply and handle reset zone
  232. * and write commands errors.
  233. */
  234. void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
  235. struct scsi_sense_hdr *sshdr)
  236. {
  237. int result = cmd->result;
  238. struct request *rq = cmd->request;
  239. switch (req_op(rq)) {
  240. case REQ_OP_ZONE_RESET:
  241. if (result &&
  242. sshdr->sense_key == ILLEGAL_REQUEST &&
  243. sshdr->asc == 0x24)
  244. /*
  245. * INVALID FIELD IN CDB error: reset of a conventional
  246. * zone was attempted. Nothing to worry about, so be
  247. * quiet about the error.
  248. */
  249. rq->rq_flags |= RQF_QUIET;
  250. break;
  251. case REQ_OP_WRITE:
  252. case REQ_OP_WRITE_ZEROES:
  253. case REQ_OP_WRITE_SAME:
  254. break;
  255. case REQ_OP_ZONE_REPORT:
  256. if (!result)
  257. sd_zbc_report_zones_complete(cmd, good_bytes);
  258. break;
  259. }
  260. }
  261. /**
  262. * sd_zbc_read_zoned_characteristics - Read zoned block device characteristics
  263. * @sdkp: Target disk
  264. * @buf: Buffer where to store the VPD page data
  265. *
  266. * Read VPD page B6.
  267. */
  268. static int sd_zbc_read_zoned_characteristics(struct scsi_disk *sdkp,
  269. unsigned char *buf)
  270. {
  271. if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
  272. sd_printk(KERN_NOTICE, sdkp,
  273. "Unconstrained-read check failed\n");
  274. return -ENODEV;
  275. }
  276. if (sdkp->device->type != TYPE_ZBC) {
  277. /* Host-aware */
  278. sdkp->urswrz = 1;
  279. sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
  280. sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
  281. sdkp->zones_max_open = 0;
  282. } else {
  283. /* Host-managed */
  284. sdkp->urswrz = buf[4] & 1;
  285. sdkp->zones_optimal_open = 0;
  286. sdkp->zones_optimal_nonseq = 0;
  287. sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
  288. }
  289. return 0;
  290. }
  291. /**
  292. * sd_zbc_check_capacity - Check reported capacity.
  293. * @sdkp: Target disk
  294. * @buf: Buffer to use for commands
  295. *
  296. * ZBC drive may report only the capacity of the first conventional zones at
  297. * LBA 0. This is indicated by the RC_BASIS field of the read capacity reply.
  298. * Check this here. If the disk reported only its conventional zones capacity,
  299. * get the total capacity by doing a report zones.
  300. */
  301. static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf)
  302. {
  303. sector_t lba;
  304. int ret;
  305. if (sdkp->rc_basis != 0)
  306. return 0;
  307. /* Do a report zone to get the maximum LBA to check capacity */
  308. ret = sd_zbc_report_zones(sdkp, buf, SD_BUF_SIZE, 0);
  309. if (ret)
  310. return ret;
  311. /* The max_lba field is the capacity of this device */
  312. lba = get_unaligned_be64(&buf[8]);
  313. if (lba + 1 == sdkp->capacity)
  314. return 0;
  315. if (sdkp->first_scan)
  316. sd_printk(KERN_WARNING, sdkp,
  317. "Changing capacity from %llu to max LBA+1 %llu\n",
  318. (unsigned long long)sdkp->capacity,
  319. (unsigned long long)lba + 1);
  320. sdkp->capacity = lba + 1;
  321. return 0;
  322. }
  323. #define SD_ZBC_BUF_SIZE 131072U
  324. /**
  325. * sd_zbc_check_zone_size - Check the device zone sizes
  326. * @sdkp: Target disk
  327. *
  328. * Check that all zones of the device are equal. The last zone can however
  329. * be smaller. The zone size must also be a power of two number of LBAs.
  330. *
  331. * Returns the zone size in number of blocks upon success or an error code
  332. * upon failure.
  333. */
  334. static s64 sd_zbc_check_zone_size(struct scsi_disk *sdkp)
  335. {
  336. u64 zone_blocks = 0;
  337. sector_t block = 0;
  338. unsigned char *buf;
  339. unsigned char *rec;
  340. unsigned int buf_len;
  341. unsigned int list_length;
  342. s64 ret;
  343. u8 same;
  344. /* Get a buffer */
  345. buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
  346. if (!buf)
  347. return -ENOMEM;
  348. /* Do a report zone to get the same field */
  349. ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0);
  350. if (ret)
  351. goto out_free;
  352. same = buf[4] & 0x0f;
  353. if (same > 0) {
  354. rec = &buf[64];
  355. zone_blocks = get_unaligned_be64(&rec[8]);
  356. goto out;
  357. }
  358. /*
  359. * Check the size of all zones: all zones must be of
  360. * equal size, except the last zone which can be smaller
  361. * than other zones.
  362. */
  363. do {
  364. /* Parse REPORT ZONES header */
  365. list_length = get_unaligned_be32(&buf[0]) + 64;
  366. rec = buf + 64;
  367. buf_len = min(list_length, SD_ZBC_BUF_SIZE);
  368. /* Parse zone descriptors */
  369. while (rec < buf + buf_len) {
  370. u64 this_zone_blocks = get_unaligned_be64(&rec[8]);
  371. if (zone_blocks == 0) {
  372. zone_blocks = this_zone_blocks;
  373. } else if (this_zone_blocks != zone_blocks &&
  374. (block + this_zone_blocks < sdkp->capacity
  375. || this_zone_blocks > zone_blocks)) {
  376. zone_blocks = 0;
  377. goto out;
  378. }
  379. block += this_zone_blocks;
  380. rec += 64;
  381. }
  382. if (block < sdkp->capacity) {
  383. ret = sd_zbc_report_zones(sdkp, buf,
  384. SD_ZBC_BUF_SIZE, block);
  385. if (ret)
  386. goto out_free;
  387. }
  388. } while (block < sdkp->capacity);
  389. out:
  390. if (!zone_blocks) {
  391. if (sdkp->first_scan)
  392. sd_printk(KERN_NOTICE, sdkp,
  393. "Devices with non constant zone "
  394. "size are not supported\n");
  395. ret = -ENODEV;
  396. } else if (!is_power_of_2(zone_blocks)) {
  397. if (sdkp->first_scan)
  398. sd_printk(KERN_NOTICE, sdkp,
  399. "Devices with non power of 2 zone "
  400. "size are not supported\n");
  401. ret = -ENODEV;
  402. } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
  403. if (sdkp->first_scan)
  404. sd_printk(KERN_NOTICE, sdkp,
  405. "Zone size too large\n");
  406. ret = -ENODEV;
  407. } else {
  408. ret = zone_blocks;
  409. }
  410. out_free:
  411. kfree(buf);
  412. return ret;
  413. }
  414. /**
  415. * sd_zbc_alloc_zone_bitmap - Allocate a zone bitmap (one bit per zone).
  416. * @nr_zones: Number of zones to allocate space for.
  417. * @numa_node: NUMA node to allocate the memory from.
  418. */
  419. static inline unsigned long *
  420. sd_zbc_alloc_zone_bitmap(u32 nr_zones, int numa_node)
  421. {
  422. return kcalloc_node(BITS_TO_LONGS(nr_zones), sizeof(unsigned long),
  423. GFP_KERNEL, numa_node);
  424. }
  425. /**
  426. * sd_zbc_get_seq_zones - Parse report zones reply to identify sequential zones
  427. * @sdkp: disk used
  428. * @buf: report reply buffer
  429. * @buflen: length of @buf
  430. * @zone_shift: logarithm base 2 of the number of blocks in a zone
  431. * @seq_zones_bitmap: bitmap of sequential zones to set
  432. *
  433. * Parse reported zone descriptors in @buf to identify sequential zones and
  434. * set the reported zone bit in @seq_zones_bitmap accordingly.
  435. * Since read-only and offline zones cannot be written, do not
  436. * mark them as sequential in the bitmap.
  437. * Return the LBA after the last zone reported.
  438. */
  439. static sector_t sd_zbc_get_seq_zones(struct scsi_disk *sdkp, unsigned char *buf,
  440. unsigned int buflen, u32 zone_shift,
  441. unsigned long *seq_zones_bitmap)
  442. {
  443. sector_t lba, next_lba = sdkp->capacity;
  444. unsigned int buf_len, list_length;
  445. unsigned char *rec;
  446. u8 type, cond;
  447. list_length = get_unaligned_be32(&buf[0]) + 64;
  448. buf_len = min(list_length, buflen);
  449. rec = buf + 64;
  450. while (rec < buf + buf_len) {
  451. type = rec[0] & 0x0f;
  452. cond = (rec[1] >> 4) & 0xf;
  453. lba = get_unaligned_be64(&rec[16]);
  454. if (type != ZBC_ZONE_TYPE_CONV &&
  455. cond != ZBC_ZONE_COND_READONLY &&
  456. cond != ZBC_ZONE_COND_OFFLINE)
  457. set_bit(lba >> zone_shift, seq_zones_bitmap);
  458. next_lba = lba + get_unaligned_be64(&rec[8]);
  459. rec += 64;
  460. }
  461. return next_lba;
  462. }
  463. /**
  464. * sd_zbc_setup_seq_zones_bitmap - Initialize a seq zone bitmap.
  465. * @sdkp: target disk
  466. * @zone_shift: logarithm base 2 of the number of blocks in a zone
  467. * @nr_zones: number of zones to set up a seq zone bitmap for
  468. *
  469. * Allocate a zone bitmap and initialize it by identifying sequential zones.
  470. */
  471. static unsigned long *
  472. sd_zbc_setup_seq_zones_bitmap(struct scsi_disk *sdkp, u32 zone_shift,
  473. u32 nr_zones)
  474. {
  475. struct request_queue *q = sdkp->disk->queue;
  476. unsigned long *seq_zones_bitmap;
  477. sector_t lba = 0;
  478. unsigned char *buf;
  479. int ret = -ENOMEM;
  480. seq_zones_bitmap = sd_zbc_alloc_zone_bitmap(nr_zones, q->node);
  481. if (!seq_zones_bitmap)
  482. return ERR_PTR(-ENOMEM);
  483. buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
  484. if (!buf)
  485. goto out;
  486. while (lba < sdkp->capacity) {
  487. ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, lba);
  488. if (ret)
  489. goto out;
  490. lba = sd_zbc_get_seq_zones(sdkp, buf, SD_ZBC_BUF_SIZE,
  491. zone_shift, seq_zones_bitmap);
  492. }
  493. if (lba != sdkp->capacity) {
  494. /* Something went wrong */
  495. ret = -EIO;
  496. }
  497. out:
  498. kfree(buf);
  499. if (ret) {
  500. kfree(seq_zones_bitmap);
  501. return ERR_PTR(ret);
  502. }
  503. return seq_zones_bitmap;
  504. }
  505. static void sd_zbc_cleanup(struct scsi_disk *sdkp)
  506. {
  507. struct request_queue *q = sdkp->disk->queue;
  508. kfree(q->seq_zones_bitmap);
  509. q->seq_zones_bitmap = NULL;
  510. kfree(q->seq_zones_wlock);
  511. q->seq_zones_wlock = NULL;
  512. q->nr_zones = 0;
  513. }
  514. static int sd_zbc_setup(struct scsi_disk *sdkp, u32 zone_blocks)
  515. {
  516. struct request_queue *q = sdkp->disk->queue;
  517. u32 zone_shift = ilog2(zone_blocks);
  518. u32 nr_zones;
  519. int ret;
  520. /* chunk_sectors indicates the zone size */
  521. blk_queue_chunk_sectors(q,
  522. logical_to_sectors(sdkp->device, zone_blocks));
  523. nr_zones = round_up(sdkp->capacity, zone_blocks) >> zone_shift;
  524. /*
  525. * Initialize the device request queue information if the number
  526. * of zones changed.
  527. */
  528. if (nr_zones != sdkp->nr_zones || nr_zones != q->nr_zones) {
  529. unsigned long *seq_zones_wlock = NULL, *seq_zones_bitmap = NULL;
  530. size_t zone_bitmap_size;
  531. if (nr_zones) {
  532. seq_zones_wlock = sd_zbc_alloc_zone_bitmap(nr_zones,
  533. q->node);
  534. if (!seq_zones_wlock) {
  535. ret = -ENOMEM;
  536. goto err;
  537. }
  538. seq_zones_bitmap = sd_zbc_setup_seq_zones_bitmap(sdkp,
  539. zone_shift, nr_zones);
  540. if (IS_ERR(seq_zones_bitmap)) {
  541. ret = PTR_ERR(seq_zones_bitmap);
  542. kfree(seq_zones_wlock);
  543. goto err;
  544. }
  545. }
  546. zone_bitmap_size = BITS_TO_LONGS(nr_zones) *
  547. sizeof(unsigned long);
  548. blk_mq_freeze_queue(q);
  549. if (q->nr_zones != nr_zones) {
  550. /* READ16/WRITE16 is mandatory for ZBC disks */
  551. sdkp->device->use_16_for_rw = 1;
  552. sdkp->device->use_10_for_rw = 0;
  553. sdkp->zone_blocks = zone_blocks;
  554. sdkp->zone_shift = zone_shift;
  555. sdkp->nr_zones = nr_zones;
  556. q->nr_zones = nr_zones;
  557. swap(q->seq_zones_wlock, seq_zones_wlock);
  558. swap(q->seq_zones_bitmap, seq_zones_bitmap);
  559. } else if (memcmp(q->seq_zones_bitmap, seq_zones_bitmap,
  560. zone_bitmap_size) != 0) {
  561. memcpy(q->seq_zones_bitmap, seq_zones_bitmap,
  562. zone_bitmap_size);
  563. }
  564. blk_mq_unfreeze_queue(q);
  565. kfree(seq_zones_wlock);
  566. kfree(seq_zones_bitmap);
  567. }
  568. return 0;
  569. err:
  570. sd_zbc_cleanup(sdkp);
  571. return ret;
  572. }
  573. int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
  574. {
  575. int64_t zone_blocks;
  576. int ret;
  577. if (!sd_is_zoned(sdkp))
  578. /*
  579. * Device managed or normal SCSI disk,
  580. * no special handling required
  581. */
  582. return 0;
  583. /* Get zoned block device characteristics */
  584. ret = sd_zbc_read_zoned_characteristics(sdkp, buf);
  585. if (ret)
  586. goto err;
  587. /*
  588. * Check for unconstrained reads: host-managed devices with
  589. * constrained reads (drives failing read after write pointer)
  590. * are not supported.
  591. */
  592. if (!sdkp->urswrz) {
  593. if (sdkp->first_scan)
  594. sd_printk(KERN_NOTICE, sdkp,
  595. "constrained reads devices are not supported\n");
  596. ret = -ENODEV;
  597. goto err;
  598. }
  599. /* Check capacity */
  600. ret = sd_zbc_check_capacity(sdkp, buf);
  601. if (ret)
  602. goto err;
  603. /*
  604. * Check zone size: only devices with a constant zone size (except
  605. * an eventual last runt zone) that is a power of 2 are supported.
  606. */
  607. zone_blocks = sd_zbc_check_zone_size(sdkp);
  608. ret = -EFBIG;
  609. if (zone_blocks != (u32)zone_blocks)
  610. goto err;
  611. ret = zone_blocks;
  612. if (ret < 0)
  613. goto err;
  614. /* The drive satisfies the kernel restrictions: set it up */
  615. ret = sd_zbc_setup(sdkp, zone_blocks);
  616. if (ret)
  617. goto err;
  618. return 0;
  619. err:
  620. sdkp->capacity = 0;
  621. sd_zbc_cleanup(sdkp);
  622. return ret;
  623. }
  624. void sd_zbc_remove(struct scsi_disk *sdkp)
  625. {
  626. sd_zbc_cleanup(sdkp);
  627. }
  628. void sd_zbc_print_zones(struct scsi_disk *sdkp)
  629. {
  630. if (!sd_is_zoned(sdkp) || !sdkp->capacity)
  631. return;
  632. if (sdkp->capacity & (sdkp->zone_blocks - 1))
  633. sd_printk(KERN_NOTICE, sdkp,
  634. "%u zones of %u logical blocks + 1 runt zone\n",
  635. sdkp->nr_zones - 1,
  636. sdkp->zone_blocks);
  637. else
  638. sd_printk(KERN_NOTICE, sdkp,
  639. "%u zones of %u logical blocks\n",
  640. sdkp->nr_zones,
  641. sdkp->zone_blocks);
  642. }