sd_zbc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 <asm/unaligned.h>
  15. #include <scsi/scsi.h>
  16. #include <scsi/scsi_cmnd.h>
  17. #include "sd.h"
  18. static unsigned int sd_zbc_get_zone_wp_offset(struct blk_zone *zone)
  19. {
  20. if (zone->type == ZBC_ZONE_TYPE_CONV)
  21. return 0;
  22. switch (zone->cond) {
  23. case BLK_ZONE_COND_IMP_OPEN:
  24. case BLK_ZONE_COND_EXP_OPEN:
  25. case BLK_ZONE_COND_CLOSED:
  26. return zone->wp - zone->start;
  27. case BLK_ZONE_COND_FULL:
  28. return zone->len;
  29. case BLK_ZONE_COND_EMPTY:
  30. case BLK_ZONE_COND_OFFLINE:
  31. case BLK_ZONE_COND_READONLY:
  32. default:
  33. /*
  34. * Offline and read-only zones do not have a valid
  35. * write pointer. Use 0 as for an empty zone.
  36. */
  37. return 0;
  38. }
  39. }
  40. static int sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
  41. unsigned int idx, report_zones_cb cb, void *data)
  42. {
  43. struct scsi_device *sdp = sdkp->device;
  44. struct blk_zone zone = { 0 };
  45. int ret;
  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. zone.len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
  53. zone.capacity = zone.len;
  54. zone.start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
  55. zone.wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
  56. if (zone.type != ZBC_ZONE_TYPE_CONV &&
  57. zone.cond == ZBC_ZONE_COND_FULL)
  58. zone.wp = zone.start + zone.len;
  59. ret = cb(&zone, idx, data);
  60. if (ret)
  61. return ret;
  62. if (sdkp->rev_wp_offset)
  63. sdkp->rev_wp_offset[idx] = sd_zbc_get_zone_wp_offset(&zone);
  64. return 0;
  65. }
  66. /**
  67. * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
  68. * @sdkp: The target disk
  69. * @buf: vmalloc-ed buffer to use for the reply
  70. * @buflen: the buffer size
  71. * @lba: Start LBA of the report
  72. * @partial: Do partial report
  73. *
  74. * For internal use during device validation.
  75. * Using partial=true can significantly speed up execution of a report zones
  76. * command because the disk does not have to count all possible report matching
  77. * zones and will only report the count of zones fitting in the command reply
  78. * buffer.
  79. */
  80. static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
  81. unsigned int buflen, sector_t lba,
  82. bool partial)
  83. {
  84. struct scsi_device *sdp = sdkp->device;
  85. const int timeout = sdp->request_queue->rq_timeout;
  86. struct scsi_sense_hdr sshdr;
  87. unsigned char cmd[16];
  88. unsigned int rep_len;
  89. int result;
  90. memset(cmd, 0, 16);
  91. cmd[0] = ZBC_IN;
  92. cmd[1] = ZI_REPORT_ZONES;
  93. put_unaligned_be64(lba, &cmd[2]);
  94. put_unaligned_be32(buflen, &cmd[10]);
  95. if (partial)
  96. cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
  97. result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  98. buf, buflen, &sshdr,
  99. timeout, SD_MAX_RETRIES, NULL);
  100. if (result) {
  101. sd_printk(KERN_ERR, sdkp,
  102. "REPORT ZONES start lba %llu failed\n", lba);
  103. sd_print_result(sdkp, "REPORT ZONES", result);
  104. if (driver_byte(result) == DRIVER_SENSE &&
  105. scsi_sense_valid(&sshdr))
  106. sd_print_sense_hdr(sdkp, &sshdr);
  107. return -EIO;
  108. }
  109. rep_len = get_unaligned_be32(&buf[0]);
  110. if (rep_len < 64) {
  111. sd_printk(KERN_ERR, sdkp,
  112. "REPORT ZONES report invalid length %u\n",
  113. rep_len);
  114. return -EIO;
  115. }
  116. return 0;
  117. }
  118. /**
  119. * Allocate a buffer for report zones reply.
  120. * @sdkp: The target disk
  121. * @nr_zones: Maximum number of zones to report
  122. * @buflen: Size of the buffer allocated
  123. *
  124. * Try to allocate a reply buffer for the number of requested zones.
  125. * The size of the buffer allocated may be smaller than requested to
  126. * satify the device constraint (max_hw_sectors, max_segments, etc).
  127. *
  128. * Return the address of the allocated buffer and update @buflen with
  129. * the size of the allocated buffer.
  130. */
  131. static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
  132. unsigned int nr_zones, size_t *buflen)
  133. {
  134. struct request_queue *q = sdkp->disk->queue;
  135. size_t bufsize;
  136. void *buf;
  137. /*
  138. * Report zone buffer size should be at most 64B times the number of
  139. * zones requested plus the 64B reply header, but should be aligned
  140. * to SECTOR_SIZE for ATA devices.
  141. * Make sure that this size does not exceed the hardware capabilities.
  142. * Furthermore, since the report zone command cannot be split, make
  143. * sure that the allocated buffer can always be mapped by limiting the
  144. * number of pages allocated to the HBA max segments limit.
  145. */
  146. nr_zones = min(nr_zones, sdkp->nr_zones);
  147. bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE);
  148. bufsize = min_t(size_t, bufsize,
  149. queue_max_hw_sectors(q) << SECTOR_SHIFT);
  150. bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
  151. while (bufsize >= SECTOR_SIZE) {
  152. buf = __vmalloc(bufsize,
  153. GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY);
  154. if (buf) {
  155. *buflen = bufsize;
  156. return buf;
  157. }
  158. bufsize = rounddown(bufsize >> 1, SECTOR_SIZE);
  159. }
  160. return NULL;
  161. }
  162. /**
  163. * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
  164. * @sdkp: The target disk
  165. */
  166. static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
  167. {
  168. return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
  169. }
  170. int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
  171. unsigned int nr_zones, report_zones_cb cb, void *data)
  172. {
  173. struct scsi_disk *sdkp = scsi_disk(disk);
  174. sector_t capacity = logical_to_sectors(sdkp->device, sdkp->capacity);
  175. unsigned int nr, i;
  176. unsigned char *buf;
  177. size_t offset, buflen = 0;
  178. int zone_idx = 0;
  179. int ret;
  180. if (!sd_is_zoned(sdkp))
  181. /* Not a zoned device */
  182. return -EOPNOTSUPP;
  183. if (!capacity)
  184. /* Device gone or invalid */
  185. return -ENODEV;
  186. buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen);
  187. if (!buf)
  188. return -ENOMEM;
  189. while (zone_idx < nr_zones && sector < capacity) {
  190. ret = sd_zbc_do_report_zones(sdkp, buf, buflen,
  191. sectors_to_logical(sdkp->device, sector), true);
  192. if (ret)
  193. goto out;
  194. offset = 0;
  195. nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64);
  196. if (!nr)
  197. break;
  198. for (i = 0; i < nr && zone_idx < nr_zones; i++) {
  199. offset += 64;
  200. ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx,
  201. cb, data);
  202. if (ret)
  203. goto out;
  204. zone_idx++;
  205. }
  206. sector += sd_zbc_zone_sectors(sdkp) * i;
  207. }
  208. ret = zone_idx;
  209. out:
  210. kvfree(buf);
  211. return ret;
  212. }
  213. static blk_status_t sd_zbc_cmnd_checks(struct scsi_cmnd *cmd)
  214. {
  215. struct request *rq = cmd->request;
  216. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  217. sector_t sector = blk_rq_pos(rq);
  218. if (!sd_is_zoned(sdkp))
  219. /* Not a zoned device */
  220. return BLK_STS_IOERR;
  221. if (sdkp->device->changed)
  222. return BLK_STS_IOERR;
  223. if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
  224. /* Unaligned request */
  225. return BLK_STS_IOERR;
  226. return BLK_STS_OK;
  227. }
  228. #define SD_ZBC_INVALID_WP_OFST (~0u)
  229. #define SD_ZBC_UPDATING_WP_OFST (SD_ZBC_INVALID_WP_OFST - 1)
  230. static int sd_zbc_update_wp_offset_cb(struct blk_zone *zone, unsigned int idx,
  231. void *data)
  232. {
  233. struct scsi_disk *sdkp = data;
  234. lockdep_assert_held(&sdkp->zones_wp_offset_lock);
  235. sdkp->zones_wp_offset[idx] = sd_zbc_get_zone_wp_offset(zone);
  236. return 0;
  237. }
  238. static void sd_zbc_update_wp_offset_workfn(struct work_struct *work)
  239. {
  240. struct scsi_disk *sdkp;
  241. unsigned int zno;
  242. int ret;
  243. sdkp = container_of(work, struct scsi_disk, zone_wp_offset_work);
  244. spin_lock_bh(&sdkp->zones_wp_offset_lock);
  245. for (zno = 0; zno < sdkp->nr_zones; zno++) {
  246. if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST)
  247. continue;
  248. spin_unlock_bh(&sdkp->zones_wp_offset_lock);
  249. ret = sd_zbc_do_report_zones(sdkp, sdkp->zone_wp_update_buf,
  250. SD_BUF_SIZE,
  251. zno * sdkp->zone_blocks, true);
  252. spin_lock_bh(&sdkp->zones_wp_offset_lock);
  253. if (!ret)
  254. sd_zbc_parse_report(sdkp, sdkp->zone_wp_update_buf + 64,
  255. zno, sd_zbc_update_wp_offset_cb,
  256. sdkp);
  257. }
  258. spin_unlock_bh(&sdkp->zones_wp_offset_lock);
  259. scsi_device_put(sdkp->device);
  260. }
  261. /**
  262. * sd_zbc_prepare_zone_append() - Prepare an emulated ZONE_APPEND command.
  263. * @cmd: the command to setup
  264. * @lba: the LBA to patch
  265. * @nr_blocks: the number of LBAs to be written
  266. *
  267. * Called from sd_setup_read_write_cmnd() for REQ_OP_ZONE_APPEND.
  268. * @sd_zbc_prepare_zone_append() handles the necessary zone wrote locking and
  269. * patching of the lba for an emulated ZONE_APPEND command.
  270. *
  271. * In case the cached write pointer offset is %SD_ZBC_INVALID_WP_OFST it will
  272. * schedule a REPORT ZONES command and return BLK_STS_IOERR.
  273. */
  274. blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba,
  275. unsigned int nr_blocks)
  276. {
  277. struct request *rq = cmd->request;
  278. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  279. unsigned int wp_offset, zno = blk_rq_zone_no(rq);
  280. blk_status_t ret;
  281. ret = sd_zbc_cmnd_checks(cmd);
  282. if (ret != BLK_STS_OK)
  283. return ret;
  284. if (!blk_rq_zone_is_seq(rq))
  285. return BLK_STS_IOERR;
  286. /* Unlock of the write lock will happen in sd_zbc_complete() */
  287. if (!blk_req_zone_write_trylock(rq))
  288. return BLK_STS_ZONE_RESOURCE;
  289. spin_lock_bh(&sdkp->zones_wp_offset_lock);
  290. wp_offset = sdkp->zones_wp_offset[zno];
  291. switch (wp_offset) {
  292. case SD_ZBC_INVALID_WP_OFST:
  293. /*
  294. * We are about to schedule work to update a zone write pointer
  295. * offset, which will cause the zone append command to be
  296. * requeued. So make sure that the scsi device does not go away
  297. * while the work is being processed.
  298. */
  299. if (scsi_device_get(sdkp->device)) {
  300. ret = BLK_STS_IOERR;
  301. break;
  302. }
  303. sdkp->zones_wp_offset[zno] = SD_ZBC_UPDATING_WP_OFST;
  304. schedule_work(&sdkp->zone_wp_offset_work);
  305. fallthrough;
  306. case SD_ZBC_UPDATING_WP_OFST:
  307. ret = BLK_STS_DEV_RESOURCE;
  308. break;
  309. default:
  310. wp_offset = sectors_to_logical(sdkp->device, wp_offset);
  311. if (wp_offset + nr_blocks > sdkp->zone_blocks) {
  312. ret = BLK_STS_IOERR;
  313. break;
  314. }
  315. *lba += wp_offset;
  316. }
  317. spin_unlock_bh(&sdkp->zones_wp_offset_lock);
  318. if (ret)
  319. blk_req_zone_write_unlock(rq);
  320. return ret;
  321. }
  322. /**
  323. * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations
  324. * can be RESET WRITE POINTER, OPEN, CLOSE or FINISH.
  325. * @cmd: the command to setup
  326. * @op: Operation to be performed
  327. * @all: All zones control
  328. *
  329. * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL,
  330. * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests.
  331. */
  332. blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
  333. unsigned char op, bool all)
  334. {
  335. struct request *rq = cmd->request;
  336. sector_t sector = blk_rq_pos(rq);
  337. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  338. sector_t block = sectors_to_logical(sdkp->device, sector);
  339. blk_status_t ret;
  340. ret = sd_zbc_cmnd_checks(cmd);
  341. if (ret != BLK_STS_OK)
  342. return ret;
  343. cmd->cmd_len = 16;
  344. memset(cmd->cmnd, 0, cmd->cmd_len);
  345. cmd->cmnd[0] = ZBC_OUT;
  346. cmd->cmnd[1] = op;
  347. if (all)
  348. cmd->cmnd[14] = 0x1;
  349. else
  350. put_unaligned_be64(block, &cmd->cmnd[2]);
  351. rq->timeout = SD_TIMEOUT;
  352. cmd->sc_data_direction = DMA_NONE;
  353. cmd->transfersize = 0;
  354. cmd->allowed = 0;
  355. return BLK_STS_OK;
  356. }
  357. static bool sd_zbc_need_zone_wp_update(struct request *rq)
  358. {
  359. switch (req_op(rq)) {
  360. case REQ_OP_ZONE_APPEND:
  361. case REQ_OP_ZONE_FINISH:
  362. case REQ_OP_ZONE_RESET:
  363. case REQ_OP_ZONE_RESET_ALL:
  364. return true;
  365. case REQ_OP_WRITE:
  366. case REQ_OP_WRITE_ZEROES:
  367. case REQ_OP_WRITE_SAME:
  368. return blk_rq_zone_is_seq(rq);
  369. default:
  370. return false;
  371. }
  372. }
  373. /**
  374. * sd_zbc_zone_wp_update - Update cached zone write pointer upon cmd completion
  375. * @cmd: Completed command
  376. * @good_bytes: Command reply bytes
  377. *
  378. * Called from sd_zbc_complete() to handle the update of the cached zone write
  379. * pointer value in case an update is needed.
  380. */
  381. static unsigned int sd_zbc_zone_wp_update(struct scsi_cmnd *cmd,
  382. unsigned int good_bytes)
  383. {
  384. int result = cmd->result;
  385. struct request *rq = cmd->request;
  386. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  387. unsigned int zno = blk_rq_zone_no(rq);
  388. enum req_opf op = req_op(rq);
  389. /*
  390. * If we got an error for a command that needs updating the write
  391. * pointer offset cache, we must mark the zone wp offset entry as
  392. * invalid to force an update from disk the next time a zone append
  393. * command is issued.
  394. */
  395. spin_lock_bh(&sdkp->zones_wp_offset_lock);
  396. if (result && op != REQ_OP_ZONE_RESET_ALL) {
  397. if (op == REQ_OP_ZONE_APPEND) {
  398. /* Force complete completion (no retry) */
  399. good_bytes = 0;
  400. scsi_set_resid(cmd, blk_rq_bytes(rq));
  401. }
  402. /*
  403. * Force an update of the zone write pointer offset on
  404. * the next zone append access.
  405. */
  406. if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST)
  407. sdkp->zones_wp_offset[zno] = SD_ZBC_INVALID_WP_OFST;
  408. goto unlock_wp_offset;
  409. }
  410. switch (op) {
  411. case REQ_OP_ZONE_APPEND:
  412. rq->__sector += sdkp->zones_wp_offset[zno];
  413. fallthrough;
  414. case REQ_OP_WRITE_ZEROES:
  415. case REQ_OP_WRITE_SAME:
  416. case REQ_OP_WRITE:
  417. if (sdkp->zones_wp_offset[zno] < sd_zbc_zone_sectors(sdkp))
  418. sdkp->zones_wp_offset[zno] +=
  419. good_bytes >> SECTOR_SHIFT;
  420. break;
  421. case REQ_OP_ZONE_RESET:
  422. sdkp->zones_wp_offset[zno] = 0;
  423. break;
  424. case REQ_OP_ZONE_FINISH:
  425. sdkp->zones_wp_offset[zno] = sd_zbc_zone_sectors(sdkp);
  426. break;
  427. case REQ_OP_ZONE_RESET_ALL:
  428. memset(sdkp->zones_wp_offset, 0,
  429. sdkp->nr_zones * sizeof(unsigned int));
  430. break;
  431. default:
  432. break;
  433. }
  434. unlock_wp_offset:
  435. spin_unlock_bh(&sdkp->zones_wp_offset_lock);
  436. return good_bytes;
  437. }
  438. /**
  439. * sd_zbc_complete - ZBC command post processing.
  440. * @cmd: Completed command
  441. * @good_bytes: Command reply bytes
  442. * @sshdr: command sense header
  443. *
  444. * Called from sd_done() to handle zone commands errors and updates to the
  445. * device queue zone write pointer offset cahce.
  446. */
  447. unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
  448. struct scsi_sense_hdr *sshdr)
  449. {
  450. int result = cmd->result;
  451. struct request *rq = cmd->request;
  452. if (op_is_zone_mgmt(req_op(rq)) &&
  453. result &&
  454. sshdr->sense_key == ILLEGAL_REQUEST &&
  455. sshdr->asc == 0x24) {
  456. /*
  457. * INVALID FIELD IN CDB error: a zone management command was
  458. * attempted on a conventional zone. Nothing to worry about,
  459. * so be quiet about the error.
  460. */
  461. rq->rq_flags |= RQF_QUIET;
  462. } else if (sd_zbc_need_zone_wp_update(rq))
  463. good_bytes = sd_zbc_zone_wp_update(cmd, good_bytes);
  464. if (req_op(rq) == REQ_OP_ZONE_APPEND)
  465. blk_req_zone_write_unlock(rq);
  466. return good_bytes;
  467. }
  468. /**
  469. * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
  470. * @sdkp: Target disk
  471. * @buf: Buffer where to store the VPD page data
  472. *
  473. * Read VPD page B6, get information and check that reads are unconstrained.
  474. */
  475. static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
  476. unsigned char *buf)
  477. {
  478. if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
  479. sd_printk(KERN_NOTICE, sdkp,
  480. "Read zoned characteristics VPD page failed\n");
  481. return -ENODEV;
  482. }
  483. if (sdkp->device->type != TYPE_ZBC) {
  484. /* Host-aware */
  485. sdkp->urswrz = 1;
  486. sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
  487. sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
  488. sdkp->zones_max_open = 0;
  489. } else {
  490. /* Host-managed */
  491. sdkp->urswrz = buf[4] & 1;
  492. sdkp->zones_optimal_open = 0;
  493. sdkp->zones_optimal_nonseq = 0;
  494. sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
  495. }
  496. /*
  497. * Check for unconstrained reads: host-managed devices with
  498. * constrained reads (drives failing read after write pointer)
  499. * are not supported.
  500. */
  501. if (!sdkp->urswrz) {
  502. if (sdkp->first_scan)
  503. sd_printk(KERN_NOTICE, sdkp,
  504. "constrained reads devices are not supported\n");
  505. return -ENODEV;
  506. }
  507. return 0;
  508. }
  509. /**
  510. * sd_zbc_check_capacity - Check the device capacity
  511. * @sdkp: Target disk
  512. * @buf: command buffer
  513. * @zblocks: zone size in number of blocks
  514. *
  515. * Get the device zone size and check that the device capacity as reported
  516. * by READ CAPACITY matches the max_lba value (plus one) of the report zones
  517. * command reply for devices with RC_BASIS == 0.
  518. *
  519. * Returns 0 upon success or an error code upon failure.
  520. */
  521. static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
  522. u32 *zblocks)
  523. {
  524. u64 zone_blocks;
  525. sector_t max_lba;
  526. unsigned char *rec;
  527. int ret;
  528. /* Do a report zone to get max_lba and the size of the first zone */
  529. ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);
  530. if (ret)
  531. return ret;
  532. if (sdkp->rc_basis == 0) {
  533. /* The max_lba field is the capacity of this device */
  534. max_lba = get_unaligned_be64(&buf[8]);
  535. if (sdkp->capacity != max_lba + 1) {
  536. if (sdkp->first_scan)
  537. sd_printk(KERN_WARNING, sdkp,
  538. "Changing capacity from %llu to max LBA+1 %llu\n",
  539. (unsigned long long)sdkp->capacity,
  540. (unsigned long long)max_lba + 1);
  541. sdkp->capacity = max_lba + 1;
  542. }
  543. }
  544. /* Get the size of the first reported zone */
  545. rec = buf + 64;
  546. zone_blocks = get_unaligned_be64(&rec[8]);
  547. if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
  548. if (sdkp->first_scan)
  549. sd_printk(KERN_NOTICE, sdkp,
  550. "Zone size too large\n");
  551. return -EFBIG;
  552. }
  553. *zblocks = zone_blocks;
  554. return 0;
  555. }
  556. static void sd_zbc_print_zones(struct scsi_disk *sdkp)
  557. {
  558. if (!sd_is_zoned(sdkp) || !sdkp->capacity)
  559. return;
  560. if (sdkp->capacity & (sdkp->zone_blocks - 1))
  561. sd_printk(KERN_NOTICE, sdkp,
  562. "%u zones of %u logical blocks + 1 runt zone\n",
  563. sdkp->nr_zones - 1,
  564. sdkp->zone_blocks);
  565. else
  566. sd_printk(KERN_NOTICE, sdkp,
  567. "%u zones of %u logical blocks\n",
  568. sdkp->nr_zones,
  569. sdkp->zone_blocks);
  570. }
  571. static int sd_zbc_init_disk(struct scsi_disk *sdkp)
  572. {
  573. sdkp->zones_wp_offset = NULL;
  574. spin_lock_init(&sdkp->zones_wp_offset_lock);
  575. sdkp->rev_wp_offset = NULL;
  576. mutex_init(&sdkp->rev_mutex);
  577. INIT_WORK(&sdkp->zone_wp_offset_work, sd_zbc_update_wp_offset_workfn);
  578. sdkp->zone_wp_update_buf = kzalloc(SD_BUF_SIZE, GFP_KERNEL);
  579. if (!sdkp->zone_wp_update_buf)
  580. return -ENOMEM;
  581. return 0;
  582. }
  583. void sd_zbc_release_disk(struct scsi_disk *sdkp)
  584. {
  585. kvfree(sdkp->zones_wp_offset);
  586. sdkp->zones_wp_offset = NULL;
  587. kfree(sdkp->zone_wp_update_buf);
  588. sdkp->zone_wp_update_buf = NULL;
  589. }
  590. static void sd_zbc_revalidate_zones_cb(struct gendisk *disk)
  591. {
  592. struct scsi_disk *sdkp = scsi_disk(disk);
  593. swap(sdkp->zones_wp_offset, sdkp->rev_wp_offset);
  594. }
  595. int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
  596. {
  597. struct gendisk *disk = sdkp->disk;
  598. struct request_queue *q = disk->queue;
  599. u32 zone_blocks = sdkp->rev_zone_blocks;
  600. unsigned int nr_zones = sdkp->rev_nr_zones;
  601. u32 max_append;
  602. int ret = 0;
  603. unsigned int flags;
  604. /*
  605. * For all zoned disks, initialize zone append emulation data if not
  606. * already done. This is necessary also for host-aware disks used as
  607. * regular disks due to the presence of partitions as these partitions
  608. * may be deleted and the disk zoned model changed back from
  609. * BLK_ZONED_NONE to BLK_ZONED_HA.
  610. */
  611. if (sd_is_zoned(sdkp) && !sdkp->zone_wp_update_buf) {
  612. ret = sd_zbc_init_disk(sdkp);
  613. if (ret)
  614. return ret;
  615. }
  616. /*
  617. * There is nothing to do for regular disks, including host-aware disks
  618. * that have partitions.
  619. */
  620. if (!blk_queue_is_zoned(q))
  621. return 0;
  622. /*
  623. * Make sure revalidate zones are serialized to ensure exclusive
  624. * updates of the scsi disk data.
  625. */
  626. mutex_lock(&sdkp->rev_mutex);
  627. if (sdkp->zone_blocks == zone_blocks &&
  628. sdkp->nr_zones == nr_zones &&
  629. disk->queue->nr_zones == nr_zones)
  630. goto unlock;
  631. flags = memalloc_noio_save();
  632. sdkp->zone_blocks = zone_blocks;
  633. sdkp->nr_zones = nr_zones;
  634. sdkp->rev_wp_offset = kvcalloc(nr_zones, sizeof(u32), GFP_KERNEL);
  635. if (!sdkp->rev_wp_offset) {
  636. ret = -ENOMEM;
  637. memalloc_noio_restore(flags);
  638. goto unlock;
  639. }
  640. ret = blk_revalidate_disk_zones(disk, sd_zbc_revalidate_zones_cb);
  641. memalloc_noio_restore(flags);
  642. kvfree(sdkp->rev_wp_offset);
  643. sdkp->rev_wp_offset = NULL;
  644. if (ret) {
  645. sdkp->zone_blocks = 0;
  646. sdkp->nr_zones = 0;
  647. sdkp->capacity = 0;
  648. goto unlock;
  649. }
  650. max_append = min_t(u32, logical_to_sectors(sdkp->device, zone_blocks),
  651. q->limits.max_segments << (PAGE_SHIFT - 9));
  652. max_append = min_t(u32, max_append, queue_max_hw_sectors(q));
  653. blk_queue_max_zone_append_sectors(q, max_append);
  654. sd_zbc_print_zones(sdkp);
  655. unlock:
  656. mutex_unlock(&sdkp->rev_mutex);
  657. return ret;
  658. }
  659. int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
  660. {
  661. struct gendisk *disk = sdkp->disk;
  662. struct request_queue *q = disk->queue;
  663. unsigned int nr_zones;
  664. u32 zone_blocks = 0;
  665. int ret;
  666. if (!sd_is_zoned(sdkp))
  667. /*
  668. * Device managed or normal SCSI disk,
  669. * no special handling required
  670. */
  671. return 0;
  672. /* Check zoned block device characteristics (unconstrained reads) */
  673. ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
  674. if (ret)
  675. goto err;
  676. /* Check the device capacity reported by report zones */
  677. ret = sd_zbc_check_capacity(sdkp, buf, &zone_blocks);
  678. if (ret != 0)
  679. goto err;
  680. /* The drive satisfies the kernel restrictions: set it up */
  681. blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
  682. blk_queue_required_elevator_features(q, ELEVATOR_F_ZBD_SEQ_WRITE);
  683. if (sdkp->zones_max_open == U32_MAX)
  684. blk_queue_max_open_zones(q, 0);
  685. else
  686. blk_queue_max_open_zones(q, sdkp->zones_max_open);
  687. blk_queue_max_active_zones(q, 0);
  688. nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
  689. /* READ16/WRITE16 is mandatory for ZBC disks */
  690. sdkp->device->use_16_for_rw = 1;
  691. sdkp->device->use_10_for_rw = 0;
  692. sdkp->rev_nr_zones = nr_zones;
  693. sdkp->rev_zone_blocks = zone_blocks;
  694. return 0;
  695. err:
  696. sdkp->capacity = 0;
  697. return ret;
  698. }