sd_zbc.c 26 KB

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