scsi: sd_zbc: Move ZBC declarations to scsi_proto.h
[linux-block.git] / drivers / scsi / sd_zbc.c
CommitLineData
89d94756
HR
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
25#include <linux/blkdev.h>
26
27#include <asm/unaligned.h>
28
29#include <scsi/scsi.h>
30#include <scsi/scsi_cmnd.h>
89d94756
HR
31
32#include "sd.h"
89d94756
HR
33
34/**
35 * Convert a zone descriptor to a zone struct.
36 */
37static void sd_zbc_parse_report(struct scsi_disk *sdkp,
38 u8 *buf,
39 struct blk_zone *zone)
40{
41 struct scsi_device *sdp = sdkp->device;
42
43 memset(zone, 0, sizeof(struct blk_zone));
44
45 zone->type = buf[0] & 0x0f;
46 zone->cond = (buf[1] >> 4) & 0xf;
47 if (buf[1] & 0x01)
48 zone->reset = 1;
49 if (buf[1] & 0x02)
50 zone->non_seq = 1;
51
52 zone->len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
53 zone->start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
54 zone->wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
55 if (zone->type != ZBC_ZONE_TYPE_CONV &&
56 zone->cond == ZBC_ZONE_COND_FULL)
57 zone->wp = zone->start + zone->len;
58}
59
60/**
61 * Issue a REPORT ZONES scsi command.
62 */
63static int sd_zbc_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
64 unsigned int buflen, sector_t lba)
65{
66 struct scsi_device *sdp = sdkp->device;
67 const int timeout = sdp->request_queue->rq_timeout;
68 struct scsi_sense_hdr sshdr;
69 unsigned char cmd[16];
70 unsigned int rep_len;
71 int result;
72
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
80 result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
81 buf, buflen, &sshdr,
82 timeout, SD_MAX_RETRIES, NULL);
83 if (result) {
84 sd_printk(KERN_ERR, sdkp,
85 "REPORT ZONES lba %llu failed with %d/%d\n",
86 (unsigned long long)lba,
87 host_byte(result), driver_byte(result));
88 return -EIO;
89 }
90
91 rep_len = get_unaligned_be32(&buf[0]);
92 if (rep_len < 64) {
93 sd_printk(KERN_ERR, sdkp,
94 "REPORT ZONES report invalid length %u\n",
95 rep_len);
96 return -EIO;
97 }
98
99 return 0;
100}
101
102int sd_zbc_setup_report_cmnd(struct scsi_cmnd *cmd)
103{
104 struct request *rq = cmd->request;
105 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
106 sector_t lba, sector = blk_rq_pos(rq);
107 unsigned int nr_bytes = blk_rq_bytes(rq);
108 int ret;
109
110 WARN_ON(nr_bytes == 0);
111
112 if (!sd_is_zoned(sdkp))
113 /* Not a zoned device */
114 return BLKPREP_KILL;
115
116 ret = scsi_init_io(cmd);
117 if (ret != BLKPREP_OK)
118 return ret;
119
120 cmd->cmd_len = 16;
121 memset(cmd->cmnd, 0, cmd->cmd_len);
122 cmd->cmnd[0] = ZBC_IN;
123 cmd->cmnd[1] = ZI_REPORT_ZONES;
124 lba = sectors_to_logical(sdkp->device, sector);
125 put_unaligned_be64(lba, &cmd->cmnd[2]);
126 put_unaligned_be32(nr_bytes, &cmd->cmnd[10]);
127 /* Do partial report for speeding things up */
128 cmd->cmnd[14] = ZBC_REPORT_ZONE_PARTIAL;
129
130 cmd->sc_data_direction = DMA_FROM_DEVICE;
131 cmd->sdb.length = nr_bytes;
132 cmd->transfersize = sdkp->device->sector_size;
133 cmd->allowed = 0;
134
135 /*
136 * Report may return less bytes than requested. Make sure
137 * to report completion on the entire initial request.
138 */
139 rq->__data_len = nr_bytes;
140
141 return BLKPREP_OK;
142}
143
144static void sd_zbc_report_zones_complete(struct scsi_cmnd *scmd,
145 unsigned int good_bytes)
146{
147 struct request *rq = scmd->request;
148 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
149 struct sg_mapping_iter miter;
150 struct blk_zone_report_hdr hdr;
151 struct blk_zone zone;
152 unsigned int offset, bytes = 0;
153 unsigned long flags;
154 u8 *buf;
155
156 if (good_bytes < 64)
157 return;
158
159 memset(&hdr, 0, sizeof(struct blk_zone_report_hdr));
160
161 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
162 SG_MITER_TO_SG | SG_MITER_ATOMIC);
163
164 local_irq_save(flags);
165 while (sg_miter_next(&miter) && bytes < good_bytes) {
166
167 buf = miter.addr;
168 offset = 0;
169
170 if (bytes == 0) {
171 /* Set the report header */
172 hdr.nr_zones = min_t(unsigned int,
173 (good_bytes - 64) / 64,
174 get_unaligned_be32(&buf[0]) / 64);
175 memcpy(buf, &hdr, sizeof(struct blk_zone_report_hdr));
176 offset += 64;
177 bytes += 64;
178 }
179
180 /* Parse zone descriptors */
181 while (offset < miter.length && hdr.nr_zones) {
182 WARN_ON(offset > miter.length);
183 buf = miter.addr + offset;
184 sd_zbc_parse_report(sdkp, buf, &zone);
185 memcpy(buf, &zone, sizeof(struct blk_zone));
186 offset += 64;
187 bytes += 64;
188 hdr.nr_zones--;
189 }
190
191 if (!hdr.nr_zones)
192 break;
193
194 }
195 sg_miter_stop(&miter);
196 local_irq_restore(flags);
197}
198
199static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
200{
201 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
202}
203
204static inline unsigned int sd_zbc_zone_no(struct scsi_disk *sdkp,
205 sector_t sector)
206{
207 return sectors_to_logical(sdkp->device, sector) >> sdkp->zone_shift;
208}
209
210int sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd)
211{
212 struct request *rq = cmd->request;
213 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
214 sector_t sector = blk_rq_pos(rq);
215 sector_t block = sectors_to_logical(sdkp->device, sector);
89d94756
HR
216
217 if (!sd_is_zoned(sdkp))
218 /* Not a zoned device */
219 return BLKPREP_KILL;
220
221 if (sdkp->device->changed)
222 return BLKPREP_KILL;
223
224 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
225 /* Unaligned request */
226 return BLKPREP_KILL;
227
89d94756
HR
228 cmd->cmd_len = 16;
229 memset(cmd->cmnd, 0, cmd->cmd_len);
230 cmd->cmnd[0] = ZBC_OUT;
231 cmd->cmnd[1] = ZO_RESET_WRITE_POINTER;
232 put_unaligned_be64(block, &cmd->cmnd[2]);
233
234 rq->timeout = SD_TIMEOUT;
235 cmd->sc_data_direction = DMA_NONE;
236 cmd->transfersize = 0;
237 cmd->allowed = 0;
238
239 return BLKPREP_OK;
240}
241
a90dfdc2 242int sd_zbc_write_lock_zone(struct scsi_cmnd *cmd)
89d94756
HR
243{
244 struct request *rq = cmd->request;
245 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
246 sector_t sector = blk_rq_pos(rq);
247 sector_t zone_sectors = sd_zbc_zone_sectors(sdkp);
248 unsigned int zno = sd_zbc_zone_no(sdkp, sector);
249
250 /*
251 * Note: Checks of the alignment of the write command on
252 * logical blocks is done in sd.c
253 */
254
255 /* Do not allow zone boundaries crossing on host-managed drives */
256 if (blk_queue_zoned_model(sdkp->disk->queue) == BLK_ZONED_HM &&
257 (sector & (zone_sectors - 1)) + blk_rq_sectors(rq) > zone_sectors)
258 return BLKPREP_KILL;
259
260 /*
261 * Do not issue more than one write at a time per
262 * zone. This solves write ordering problems due to
263 * the unlocking of the request queue in the dispatch
264 * path in the non scsi-mq case. For scsi-mq, this
265 * also avoids potential write reordering when multiple
266 * threads running on different CPUs write to the same
267 * zone (with a synchronized sequential pattern).
268 */
269 if (sdkp->zones_wlock &&
270 test_and_set_bit(zno, sdkp->zones_wlock))
271 return BLKPREP_DEFER;
272
70e42fd0
DLM
273 WARN_ON_ONCE(cmd->flags & SCMD_ZONE_WRITE_LOCK);
274 cmd->flags |= SCMD_ZONE_WRITE_LOCK;
275
89d94756
HR
276 return BLKPREP_OK;
277}
278
a90dfdc2 279void sd_zbc_write_unlock_zone(struct scsi_cmnd *cmd)
89d94756 280{
a90dfdc2 281 struct request *rq = cmd->request;
89d94756
HR
282 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
283
70e42fd0 284 if (sdkp->zones_wlock && cmd->flags & SCMD_ZONE_WRITE_LOCK) {
89d94756
HR
285 unsigned int zno = sd_zbc_zone_no(sdkp, blk_rq_pos(rq));
286 WARN_ON_ONCE(!test_bit(zno, sdkp->zones_wlock));
70e42fd0 287 cmd->flags &= ~SCMD_ZONE_WRITE_LOCK;
89d94756
HR
288 clear_bit_unlock(zno, sdkp->zones_wlock);
289 smp_mb__after_atomic();
290 }
291}
292
89d94756
HR
293void sd_zbc_complete(struct scsi_cmnd *cmd,
294 unsigned int good_bytes,
295 struct scsi_sense_hdr *sshdr)
296{
297 int result = cmd->result;
298 struct request *rq = cmd->request;
299
300 switch (req_op(rq)) {
868ed5a5
DLM
301 case REQ_OP_ZONE_RESET:
302
303 if (result &&
304 sshdr->sense_key == ILLEGAL_REQUEST &&
305 sshdr->asc == 0x24)
306 /*
307 * INVALID FIELD IN CDB error: reset of a conventional
308 * zone was attempted. Nothing to worry about, so be
309 * quiet about the error.
310 */
311 rq->rq_flags |= RQF_QUIET;
312 break;
313
89d94756 314 case REQ_OP_WRITE:
02d26103 315 case REQ_OP_WRITE_ZEROES:
89d94756 316 case REQ_OP_WRITE_SAME:
89d94756 317
868ed5a5
DLM
318 if (result &&
319 sshdr->sense_key == ILLEGAL_REQUEST &&
320 sshdr->asc == 0x21)
89d94756
HR
321 /*
322 * INVALID ADDRESS FOR WRITE error: It is unlikely that
323 * retrying write requests failed with any kind of
324 * alignement error will result in success. So don't.
325 */
326 cmd->allowed = 0;
89d94756
HR
327 break;
328
329 case REQ_OP_ZONE_REPORT:
330
331 if (!result)
332 sd_zbc_report_zones_complete(cmd, good_bytes);
333 break;
334
335 }
336}
337
338/**
339 * Read zoned block device characteristics (VPD page B6).
340 */
341static int sd_zbc_read_zoned_characteristics(struct scsi_disk *sdkp,
342 unsigned char *buf)
343{
344
345 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
346 sd_printk(KERN_NOTICE, sdkp,
347 "Unconstrained-read check failed\n");
348 return -ENODEV;
349 }
350
351 if (sdkp->device->type != TYPE_ZBC) {
352 /* Host-aware */
353 sdkp->urswrz = 1;
354 sdkp->zones_optimal_open = get_unaligned_be64(&buf[8]);
355 sdkp->zones_optimal_nonseq = get_unaligned_be64(&buf[12]);
356 sdkp->zones_max_open = 0;
357 } else {
358 /* Host-managed */
359 sdkp->urswrz = buf[4] & 1;
360 sdkp->zones_optimal_open = 0;
361 sdkp->zones_optimal_nonseq = 0;
362 sdkp->zones_max_open = get_unaligned_be64(&buf[16]);
363 }
364
365 return 0;
366}
367
368/**
369 * Check reported capacity.
370 */
371static int sd_zbc_check_capacity(struct scsi_disk *sdkp,
372 unsigned char *buf)
373{
374 sector_t lba;
375 int ret;
376
377 if (sdkp->rc_basis != 0)
378 return 0;
379
380 /* Do a report zone to get the maximum LBA to check capacity */
381 ret = sd_zbc_report_zones(sdkp, buf, SD_BUF_SIZE, 0);
382 if (ret)
383 return ret;
384
385 /* The max_lba field is the capacity of this device */
386 lba = get_unaligned_be64(&buf[8]);
387 if (lba + 1 == sdkp->capacity)
388 return 0;
389
390 if (sdkp->first_scan)
391 sd_printk(KERN_WARNING, sdkp,
392 "Changing capacity from %llu to max LBA+1 %llu\n",
393 (unsigned long long)sdkp->capacity,
394 (unsigned long long)lba + 1);
395 sdkp->capacity = lba + 1;
396
397 return 0;
398}
399
400#define SD_ZBC_BUF_SIZE 131072
401
402static int sd_zbc_check_zone_size(struct scsi_disk *sdkp)
403{
404 u64 zone_blocks;
405 sector_t block = 0;
406 unsigned char *buf;
407 unsigned char *rec;
408 unsigned int buf_len;
409 unsigned int list_length;
410 int ret;
411 u8 same;
412
413 sdkp->zone_blocks = 0;
414
415 /* Get a buffer */
416 buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
417 if (!buf)
418 return -ENOMEM;
419
420 /* Do a report zone to get the same field */
421 ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0);
5f2808ff
AB
422 if (ret) {
423 zone_blocks = 0;
89d94756 424 goto out;
5f2808ff 425 }
89d94756
HR
426
427 same = buf[4] & 0x0f;
428 if (same > 0) {
429 rec = &buf[64];
430 zone_blocks = get_unaligned_be64(&rec[8]);
431 goto out;
432 }
433
434 /*
435 * Check the size of all zones: all zones must be of
436 * equal size, except the last zone which can be smaller
437 * than other zones.
438 */
439 do {
440
441 /* Parse REPORT ZONES header */
442 list_length = get_unaligned_be32(&buf[0]) + 64;
443 rec = buf + 64;
444 if (list_length < SD_ZBC_BUF_SIZE)
445 buf_len = list_length;
446 else
447 buf_len = SD_ZBC_BUF_SIZE;
448
449 /* Parse zone descriptors */
450 while (rec < buf + buf_len) {
451 zone_blocks = get_unaligned_be64(&rec[8]);
452 if (sdkp->zone_blocks == 0) {
453 sdkp->zone_blocks = zone_blocks;
454 } else if (zone_blocks != sdkp->zone_blocks &&
455 (block + zone_blocks < sdkp->capacity
456 || zone_blocks > sdkp->zone_blocks)) {
457 zone_blocks = 0;
458 goto out;
459 }
460 block += zone_blocks;
461 rec += 64;
462 }
463
464 if (block < sdkp->capacity) {
465 ret = sd_zbc_report_zones(sdkp, buf,
466 SD_ZBC_BUF_SIZE, block);
467 if (ret)
468 return ret;
469 }
470
471 } while (block < sdkp->capacity);
472
473 zone_blocks = sdkp->zone_blocks;
474
475out:
476 kfree(buf);
477
478 if (!zone_blocks) {
479 if (sdkp->first_scan)
480 sd_printk(KERN_NOTICE, sdkp,
481 "Devices with non constant zone "
482 "size are not supported\n");
483 return -ENODEV;
484 }
485
486 if (!is_power_of_2(zone_blocks)) {
487 if (sdkp->first_scan)
488 sd_printk(KERN_NOTICE, sdkp,
489 "Devices with non power of 2 zone "
490 "size are not supported\n");
491 return -ENODEV;
492 }
493
494 if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
495 if (sdkp->first_scan)
496 sd_printk(KERN_NOTICE, sdkp,
497 "Zone size too large\n");
498 return -ENODEV;
499 }
500
501 sdkp->zone_blocks = zone_blocks;
502
503 return 0;
504}
505
506static int sd_zbc_setup(struct scsi_disk *sdkp)
507{
508
509 /* chunk_sectors indicates the zone size */
510 blk_queue_chunk_sectors(sdkp->disk->queue,
511 logical_to_sectors(sdkp->device, sdkp->zone_blocks));
512 sdkp->zone_shift = ilog2(sdkp->zone_blocks);
513 sdkp->nr_zones = sdkp->capacity >> sdkp->zone_shift;
514 if (sdkp->capacity & (sdkp->zone_blocks - 1))
515 sdkp->nr_zones++;
516
517 if (!sdkp->zones_wlock) {
518 sdkp->zones_wlock = kcalloc(BITS_TO_LONGS(sdkp->nr_zones),
519 sizeof(unsigned long),
520 GFP_KERNEL);
521 if (!sdkp->zones_wlock)
522 return -ENOMEM;
523 }
524
525 return 0;
526}
527
528int sd_zbc_read_zones(struct scsi_disk *sdkp,
529 unsigned char *buf)
530{
f7053240 531 int ret;
89d94756
HR
532
533 if (!sd_is_zoned(sdkp))
534 /*
535 * Device managed or normal SCSI disk,
536 * no special handling required
537 */
538 return 0;
539
540
541 /* Get zoned block device characteristics */
542 ret = sd_zbc_read_zoned_characteristics(sdkp, buf);
543 if (ret)
544 goto err;
545
546 /*
547 * Check for unconstrained reads: host-managed devices with
548 * constrained reads (drives failing read after write pointer)
549 * are not supported.
550 */
551 if (!sdkp->urswrz) {
552 if (sdkp->first_scan)
553 sd_printk(KERN_NOTICE, sdkp,
554 "constrained reads devices are not supported\n");
555 ret = -ENODEV;
556 goto err;
557 }
558
559 /* Check capacity */
560 ret = sd_zbc_check_capacity(sdkp, buf);
561 if (ret)
562 goto err;
89d94756
HR
563
564 /*
565 * Check zone size: only devices with a constant zone size (except
566 * an eventual last runt zone) that is a power of 2 are supported.
567 */
568 ret = sd_zbc_check_zone_size(sdkp);
569 if (ret)
570 goto err;
571
572 /* The drive satisfies the kernel restrictions: set it up */
573 ret = sd_zbc_setup(sdkp);
574 if (ret)
575 goto err;
576
c6463c65
DLM
577 /* READ16/WRITE16 is mandatory for ZBC disks */
578 sdkp->device->use_16_for_rw = 1;
579 sdkp->device->use_10_for_rw = 0;
580
89d94756
HR
581 return 0;
582
583err:
584 sdkp->capacity = 0;
585
586 return ret;
587}
588
589void sd_zbc_remove(struct scsi_disk *sdkp)
590{
591 kfree(sdkp->zones_wlock);
592 sdkp->zones_wlock = NULL;
593}
594
595void sd_zbc_print_zones(struct scsi_disk *sdkp)
596{
597 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
598 return;
599
600 if (sdkp->capacity & (sdkp->zone_blocks - 1))
601 sd_printk(KERN_NOTICE, sdkp,
602 "%u zones of %u logical blocks + 1 runt zone\n",
603 sdkp->nr_zones - 1,
604 sdkp->zone_blocks);
605 else
606 sd_printk(KERN_NOTICE, sdkp,
607 "%u zones of %u logical blocks\n",
608 sdkp->nr_zones,
609 sdkp->zone_blocks);
610}