1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2015 Netflix, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * Derived from ata_da.c:
28 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
29 */
30
31 #include <sys/param.h>
32
33 #ifdef _KERNEL
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/bio.h>
37 #include <sys/sysctl.h>
38 #include <sys/taskqueue.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/conf.h>
42 #include <sys/devicestat.h>
43 #include <sys/eventhandler.h>
44 #include <sys/malloc.h>
45 #include <sys/cons.h>
46 #include <sys/power.h>
47 #include <sys/proc.h>
48 #include <sys/reboot.h>
49 #include <sys/sbuf.h>
50 #include <geom/geom.h>
51 #include <geom/geom_disk.h>
52 #endif /* _KERNEL */
53
54 #ifndef _KERNEL
55 #include <stdio.h>
56 #include <string.h>
57 #endif /* _KERNEL */
58
59 #include <cam/cam.h>
60 #include <cam/cam_ccb.h>
61 #include <cam/cam_periph.h>
62 #include <cam/cam_xpt_periph.h>
63 #include <cam/cam_sim.h>
64 #include <cam/cam_iosched.h>
65
66 #include <cam/nvme/nvme_all.h>
67
68 typedef enum {
69 NDA_STATE_NORMAL
70 } nda_state;
71
72 typedef enum {
73 NDA_FLAG_OPEN = 0x0001,
74 NDA_FLAG_DIRTY = 0x0002,
75 NDA_FLAG_SCTX_INIT = 0x0004,
76 } nda_flags;
77 #define NDA_FLAG_STRING \
78 "\020" \
79 "\001OPEN" \
80 "\002DIRTY" \
81 "\003SCTX_INIT"
82
83 typedef enum {
84 NDA_Q_4K = 0x01,
85 NDA_Q_NONE = 0x00,
86 } nda_quirks;
87
88 #define NDA_Q_BIT_STRING \
89 "\020" \
90 "\001Bit 0"
91
92 typedef enum {
93 NDA_CCB_BUFFER_IO = 0x01,
94 NDA_CCB_DUMP = 0x02,
95 NDA_CCB_TRIM = 0x03,
96 NDA_CCB_PASS = 0x04,
97 NDA_CCB_TYPE_MASK = 0x0F,
98 } nda_ccb_state;
99
100 /* Offsets into our private area for storing information */
101 #define ccb_state ccb_h.ppriv_field0
102 #define ccb_bp ccb_h.ppriv_ptr1 /* For NDA_CCB_BUFFER_IO */
103 #define ccb_trim ccb_h.ppriv_ptr1 /* For NDA_CCB_TRIM */
104
105 struct nda_softc {
106 struct cam_iosched_softc *cam_iosched;
107 int outstanding_cmds; /* Number of active commands */
108 int refcount; /* Active xpt_action() calls */
109 nda_state state;
110 nda_flags flags;
111 nda_quirks quirks;
112 int unmappedio;
113 quad_t deletes;
114 uint32_t nsid; /* Namespace ID for this nda device */
115 struct disk *disk;
116 struct task sysctl_task;
117 struct sysctl_ctx_list sysctl_ctx;
118 struct sysctl_oid *sysctl_tree;
119 uint64_t trim_count;
120 uint64_t trim_ranges;
121 uint64_t trim_lbas;
122 #ifdef CAM_TEST_FAILURE
123 int force_read_error;
124 int force_write_error;
125 int periodic_read_error;
126 int periodic_read_count;
127 #endif
128 #ifdef CAM_IO_STATS
129 struct sysctl_ctx_list sysctl_stats_ctx;
130 struct sysctl_oid *sysctl_stats_tree;
131 u_int timeouts;
132 u_int errors;
133 u_int invalidations;
134 #endif
135 };
136
137 struct nda_trim_request {
138 struct nvme_dsm_range dsm[NVME_MAX_DSM_TRIM / sizeof(struct nvme_dsm_range)];
139 TAILQ_HEAD(, bio) bps;
140 };
141 _Static_assert(NVME_MAX_DSM_TRIM % sizeof(struct nvme_dsm_range) == 0,
142 "NVME_MAX_DSM_TRIM must be an integral number of ranges");
143
144 /* Need quirk table */
145
146 static disk_ioctl_t ndaioctl;
147 static disk_strategy_t ndastrategy;
148 static dumper_t ndadump;
149 static periph_init_t ndainit;
150 static void ndaasync(void *callback_arg, uint32_t code,
151 struct cam_path *path, void *arg);
152 static void ndasysctlinit(void *context, int pending);
153 static int ndaflagssysctl(SYSCTL_HANDLER_ARGS);
154 static periph_ctor_t ndaregister;
155 static periph_dtor_t ndacleanup;
156 static periph_start_t ndastart;
157 static periph_oninv_t ndaoninvalidate;
158 static void ndadone(struct cam_periph *periph,
159 union ccb *done_ccb);
160 static int ndaerror(union ccb *ccb, uint32_t cam_flags,
161 uint32_t sense_flags);
162 static void ndashutdown(void *arg, int howto);
163 static void ndasuspend(void *arg, enum power_stype stype);
164
165 #ifndef NDA_DEFAULT_SEND_ORDERED
166 #define NDA_DEFAULT_SEND_ORDERED 1
167 #endif
168 #ifndef NDA_DEFAULT_TIMEOUT
169 #define NDA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
170 #endif
171 #ifndef NDA_DEFAULT_RETRY
172 #define NDA_DEFAULT_RETRY 4
173 #endif
174 #ifndef NDA_MAX_TRIM_ENTRIES
175 #define NDA_MAX_TRIM_ENTRIES (NVME_MAX_DSM_TRIM / sizeof(struct nvme_dsm_range))/* Number of DSM trims to use, max 256 */
176 #endif
177
178 static SYSCTL_NODE(_kern_cam, OID_AUTO, nda, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
179 "CAM Direct Access Disk driver");
180
181 //static int nda_retry_count = NDA_DEFAULT_RETRY;
182 static int nda_send_ordered = NDA_DEFAULT_SEND_ORDERED;
183 static int nda_default_timeout = NDA_DEFAULT_TIMEOUT;
184 static int nda_max_trim_entries = NDA_MAX_TRIM_ENTRIES;
185 static int nda_enable_biospeedup = 1;
186 static int nda_nvd_compat = 1;
187 SYSCTL_INT(_kern_cam_nda, OID_AUTO, max_trim, CTLFLAG_RDTUN,
188 &nda_max_trim_entries, NDA_MAX_TRIM_ENTRIES,
189 "Maximum number of BIO_DELETE to send down as a DSM TRIM.");
190 SYSCTL_INT(_kern_cam_nda, OID_AUTO, enable_biospeedup, CTLFLAG_RDTUN,
191 &nda_enable_biospeedup, 0, "Enable BIO_SPEEDUP processing.");
192 SYSCTL_INT(_kern_cam_nda, OID_AUTO, nvd_compat, CTLFLAG_RDTUN,
193 &nda_nvd_compat, 1, "Enable creation of nvd aliases.");
194
195 /*
196 * All NVMe media is non-rotational, so all nvme device instances
197 * share this to implement the sysctl.
198 */
199 static int nda_rotating_media = 0;
200
201 static struct periph_driver ndadriver =
202 {
203 ndainit, "nda",
204 TAILQ_HEAD_INITIALIZER(ndadriver.units), /* generation */ 0
205 };
206
207 PERIPHDRIVER_DECLARE(nda, ndadriver);
208
209 static MALLOC_DEFINE(M_NVMEDA, "nvme_da", "nvme_da buffers");
210
211 /*
212 * nice wrappers. Maybe these belong in nvme_all.c instead of
213 * here, but this is the only place that uses these. Should
214 * we ever grow another NVME periph, we should move them
215 * all there wholesale.
216 */
217
218 static void
nda_nvme_flush(struct nda_softc * softc,struct ccb_nvmeio * nvmeio)219 nda_nvme_flush(struct nda_softc *softc, struct ccb_nvmeio *nvmeio)
220 {
221 cam_fill_nvmeio(nvmeio,
222 0, /* retries */
223 ndadone, /* cbfcnp */
224 CAM_DIR_NONE, /* flags */
225 NULL, /* data_ptr */
226 0, /* dxfer_len */
227 nda_default_timeout * 1000); /* timeout 30s */
228 nvme_ns_flush_cmd(&nvmeio->cmd, softc->nsid);
229 }
230
231 static void
nda_nvme_trim(struct nda_softc * softc,struct ccb_nvmeio * nvmeio,void * payload,uint32_t num_ranges)232 nda_nvme_trim(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
233 void *payload, uint32_t num_ranges)
234 {
235 cam_fill_nvmeio(nvmeio,
236 0, /* retries */
237 ndadone, /* cbfcnp */
238 CAM_DIR_OUT, /* flags */
239 payload, /* data_ptr */
240 num_ranges * sizeof(struct nvme_dsm_range), /* dxfer_len */
241 nda_default_timeout * 1000); /* timeout 30s */
242 nvme_ns_trim_cmd(&nvmeio->cmd, softc->nsid, num_ranges);
243 }
244
245 static void
nda_nvme_write(struct nda_softc * softc,struct ccb_nvmeio * nvmeio,void * payload,uint64_t lba,uint32_t len,uint32_t count)246 nda_nvme_write(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
247 void *payload, uint64_t lba, uint32_t len, uint32_t count)
248 {
249 cam_fill_nvmeio(nvmeio,
250 0, /* retries */
251 ndadone, /* cbfcnp */
252 CAM_DIR_OUT, /* flags */
253 payload, /* data_ptr */
254 len, /* dxfer_len */
255 nda_default_timeout * 1000); /* timeout 30s */
256 nvme_ns_write_cmd(&nvmeio->cmd, softc->nsid, lba, count);
257 }
258
259 static void
nda_nvme_rw_bio(struct nda_softc * softc,struct ccb_nvmeio * nvmeio,struct bio * bp,uint32_t rwcmd)260 nda_nvme_rw_bio(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
261 struct bio *bp, uint32_t rwcmd)
262 {
263 int flags = rwcmd == NVME_OPC_READ ? CAM_DIR_IN : CAM_DIR_OUT;
264 void *payload;
265 uint64_t lba;
266 uint32_t count;
267
268 if (bp->bio_flags & BIO_UNMAPPED) {
269 flags |= CAM_DATA_BIO;
270 payload = bp;
271 } else {
272 payload = bp->bio_data;
273 }
274
275 lba = bp->bio_pblkno;
276 count = bp->bio_bcount / softc->disk->d_sectorsize;
277
278 cam_fill_nvmeio(nvmeio,
279 0, /* retries */
280 ndadone, /* cbfcnp */
281 flags, /* flags */
282 payload, /* data_ptr */
283 bp->bio_bcount, /* dxfer_len */
284 nda_default_timeout * 1000); /* timeout 30s */
285 nvme_ns_rw_cmd(&nvmeio->cmd, rwcmd, softc->nsid, lba, count);
286 }
287
288 static int
ndaopen(struct disk * dp)289 ndaopen(struct disk *dp)
290 {
291 struct cam_periph *periph;
292 struct nda_softc *softc;
293 int error;
294
295 periph = (struct cam_periph *)dp->d_drv1;
296 if (cam_periph_acquire(periph) != 0) {
297 return(ENXIO);
298 }
299
300 cam_periph_lock(periph);
301 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
302 cam_periph_unlock(periph);
303 cam_periph_release(periph);
304 return (error);
305 }
306
307 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
308 ("ndaopen\n"));
309
310 softc = (struct nda_softc *)periph->softc;
311 softc->flags |= NDA_FLAG_OPEN;
312
313 cam_periph_unhold(periph);
314 cam_periph_unlock(periph);
315 return (0);
316 }
317
318 static int
ndaclose(struct disk * dp)319 ndaclose(struct disk *dp)
320 {
321 struct cam_periph *periph;
322 struct nda_softc *softc;
323 union ccb *ccb;
324 int error;
325
326 periph = (struct cam_periph *)dp->d_drv1;
327 softc = (struct nda_softc *)periph->softc;
328 cam_periph_lock(periph);
329
330 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
331 ("ndaclose\n"));
332
333 if ((softc->flags & NDA_FLAG_DIRTY) != 0 &&
334 (periph->flags & CAM_PERIPH_INVALID) == 0 &&
335 cam_periph_hold(periph, PRIBIO) == 0) {
336 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
337 nda_nvme_flush(softc, &ccb->nvmeio);
338 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
339 /*sense_flags*/0, softc->disk->d_devstat);
340
341 if (error != 0)
342 xpt_print(periph->path, "Synchronize cache failed\n");
343 else
344 softc->flags &= ~NDA_FLAG_DIRTY;
345 xpt_release_ccb(ccb);
346 cam_periph_unhold(periph);
347 }
348
349 softc->flags &= ~NDA_FLAG_OPEN;
350
351 while (softc->refcount != 0)
352 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "ndaclose", 1);
353 KASSERT(softc->outstanding_cmds == 0,
354 ("nda %d outstanding commands", softc->outstanding_cmds));
355 cam_periph_unlock(periph);
356 cam_periph_release(periph);
357 return (0);
358 }
359
360 static void
ndaschedule(struct cam_periph * periph)361 ndaschedule(struct cam_periph *periph)
362 {
363 struct nda_softc *softc = (struct nda_softc *)periph->softc;
364
365 if (softc->state != NDA_STATE_NORMAL)
366 return;
367
368 cam_iosched_schedule(softc->cam_iosched, periph);
369 }
370
371 static int
ndaioctl(struct disk * dp,u_long cmd,void * data,int fflag,struct thread * td)372 ndaioctl(struct disk *dp, u_long cmd, void *data, int fflag,
373 struct thread *td)
374 {
375 struct cam_periph *periph;
376
377 periph = (struct cam_periph *)dp->d_drv1;
378
379 switch (cmd) {
380 case NVME_IO_TEST:
381 case NVME_BIO_TEST:
382 /*
383 * These don't map well to the underlying CCBs, so
384 * they are usupported via CAM.
385 */
386 return (ENOTTY);
387 case NVME_GET_NSID:
388 {
389 struct nvme_get_nsid *gnsid = (struct nvme_get_nsid *)data;
390 struct ccb_pathinq cpi;
391
392 xpt_path_inq(&cpi, periph->path);
393 strncpy(gnsid->cdev, cpi.xport_specific.nvme.dev_name,
394 sizeof(gnsid->cdev));
395 gnsid->nsid = cpi.xport_specific.nvme.nsid;
396 return (0);
397 }
398 case NVME_PASSTHROUGH_CMD:
399 {
400 struct nvme_pt_command *pt;
401 union ccb *ccb;
402 struct cam_periph_map_info mapinfo;
403 u_int maxmap = dp->d_maxsize;
404 int error;
405
406 /*
407 * Create a NVME_IO CCB to do the passthrough command.
408 */
409 pt = (struct nvme_pt_command *)data;
410 ccb = xpt_alloc_ccb();
411 xpt_setup_ccb(&ccb->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
412 ccb->ccb_state = NDA_CCB_PASS;
413 cam_fill_nvmeio(&ccb->nvmeio,
414 0, /* Retries */
415 ndadone,
416 (pt->is_read ? CAM_DIR_IN : CAM_DIR_OUT) | CAM_DATA_VADDR,
417 pt->buf,
418 pt->len,
419 nda_default_timeout * 1000);
420 memcpy(&ccb->nvmeio.cmd, &pt->cmd, sizeof(pt->cmd));
421
422 /*
423 * Wire the user memory in this request for the I/O
424 */
425 memset(&mapinfo, 0, sizeof(mapinfo));
426 error = cam_periph_mapmem(ccb, &mapinfo, maxmap);
427 if (error)
428 goto out;
429
430 /*
431 * Lock the periph and run the command.
432 */
433 cam_periph_lock(periph);
434 cam_periph_runccb(ccb, NULL, CAM_RETRY_SELTO,
435 SF_RETRY_UA | SF_NO_PRINT, NULL);
436
437 /*
438 * Tear down mapping and return status.
439 */
440 cam_periph_unlock(periph);
441 error = cam_periph_unmapmem(ccb, &mapinfo);
442 if (!cam_ccb_success(ccb))
443 error = EIO;
444 out:
445 cam_periph_lock(periph);
446 xpt_release_ccb(ccb);
447 cam_periph_unlock(periph);
448 return (error);
449 }
450 default:
451 break;
452 }
453 return (ENOTTY);
454 }
455
456 /*
457 * Actually translate the requested transfer into one the physical driver
458 * can understand. The transfer is described by a buf and will include
459 * only one physical transfer.
460 */
461 static void
ndastrategy(struct bio * bp)462 ndastrategy(struct bio *bp)
463 {
464 struct cam_periph *periph;
465 struct nda_softc *softc;
466
467 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
468 softc = (struct nda_softc *)periph->softc;
469
470 cam_periph_lock(periph);
471
472 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastrategy(%p)\n", bp));
473
474 /*
475 * If the device has been made invalid, error out
476 */
477 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
478 cam_periph_unlock(periph);
479 biofinish(bp, NULL, ENXIO);
480 return;
481 }
482
483 if (bp->bio_cmd == BIO_DELETE)
484 softc->deletes++;
485
486 /*
487 * Place it in the queue of disk activities for this disk
488 */
489 cam_iosched_queue_work(softc->cam_iosched, bp);
490
491 /*
492 * Schedule ourselves for performing the work.
493 */
494 ndaschedule(periph);
495 cam_periph_unlock(periph);
496
497 return;
498 }
499
500 static int
ndadump(void * arg,void * virtual,off_t offset,size_t length)501 ndadump(void *arg, void *virtual, off_t offset, size_t length)
502 {
503 struct cam_periph *periph;
504 struct nda_softc *softc;
505 u_int secsize;
506 struct ccb_nvmeio nvmeio;
507 struct disk *dp;
508 uint64_t lba;
509 uint32_t count;
510 int error = 0;
511
512 dp = arg;
513 periph = dp->d_drv1;
514 softc = (struct nda_softc *)periph->softc;
515 secsize = softc->disk->d_sectorsize;
516 lba = offset / secsize;
517 count = length / secsize;
518
519 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
520 return (ENXIO);
521
522 /* xpt_get_ccb returns a zero'd allocation for the ccb, mimic that here */
523 memset(&nvmeio, 0, sizeof(nvmeio));
524 if (length > 0) {
525 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
526 nvmeio.ccb_state = NDA_CCB_DUMP;
527 nda_nvme_write(softc, &nvmeio, virtual, lba, length, count);
528 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
529 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
530 if (error != 0)
531 printf("Aborting dump due to I/O error %d.\n", error);
532
533 return (error);
534 }
535
536 /* Flush */
537 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
538
539 nvmeio.ccb_state = NDA_CCB_DUMP;
540 nda_nvme_flush(softc, &nvmeio);
541 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
542 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
543 if (error != 0)
544 xpt_print(periph->path, "flush cmd failed\n");
545 return (error);
546 }
547
548 static void
ndainit(void)549 ndainit(void)
550 {
551 cam_status status;
552
553 /*
554 * Install a global async callback. This callback will
555 * receive async callbacks like "new device found".
556 */
557 status = xpt_register_async(AC_FOUND_DEVICE, ndaasync, NULL, NULL);
558
559 if (status != CAM_REQ_CMP) {
560 printf("nda: Failed to attach master async callback "
561 "due to status 0x%x!\n", status);
562 } else if (nda_send_ordered) {
563 /* Register our event handlers */
564 if ((EVENTHANDLER_REGISTER(power_suspend, ndasuspend,
565 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
566 printf("ndainit: power event registration failed!\n");
567 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ndashutdown,
568 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
569 printf("ndainit: shutdown event registration failed!\n");
570 }
571 }
572
573 /*
574 * Callback from GEOM, called when it has finished cleaning up its
575 * resources.
576 */
577 static void
ndadiskgonecb(struct disk * dp)578 ndadiskgonecb(struct disk *dp)
579 {
580 struct cam_periph *periph;
581
582 periph = (struct cam_periph *)dp->d_drv1;
583
584 cam_periph_release(periph);
585 }
586
587 static void
ndaoninvalidate(struct cam_periph * periph)588 ndaoninvalidate(struct cam_periph *periph)
589 {
590 struct nda_softc *softc;
591
592 softc = (struct nda_softc *)periph->softc;
593
594 /*
595 * De-register any async callbacks.
596 */
597 xpt_register_async(0, ndaasync, periph, periph->path);
598 #ifdef CAM_IO_STATS
599 softc->invalidations++;
600 #endif
601
602 /*
603 * Return all queued I/O with ENXIO. Transactions may be queued up here
604 * for retry (since we are called while there's other transactions
605 * pending). Any requests in the hardware will drain before ndacleanup
606 * is called.
607 */
608 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
609
610 /*
611 * Tell GEOM that we've gone away, we'll get a callback when it is
612 * done cleaning up its resources.
613 */
614 disk_gone(softc->disk);
615 }
616
617 static void
ndacleanup(struct cam_periph * periph)618 ndacleanup(struct cam_periph *periph)
619 {
620 struct nda_softc *softc;
621
622 softc = (struct nda_softc *)periph->softc;
623
624 cam_periph_unlock(periph);
625
626 cam_iosched_fini(softc->cam_iosched);
627
628 /*
629 * If we can't free the sysctl tree, oh well...
630 */
631 if ((softc->flags & NDA_FLAG_SCTX_INIT) != 0) {
632 #ifdef CAM_IO_STATS
633 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
634 xpt_print(periph->path,
635 "can't remove sysctl stats context\n");
636 #endif
637 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
638 xpt_print(periph->path,
639 "can't remove sysctl context\n");
640 }
641
642 disk_destroy(softc->disk);
643 free(softc, M_DEVBUF);
644 cam_periph_lock(periph);
645 }
646
647 static void
ndasetgeom(struct nda_softc * softc,struct cam_periph * periph)648 ndasetgeom(struct nda_softc *softc, struct cam_periph *periph)
649 {
650 struct disk *disk = softc->disk;
651 struct ccb_pathinq cpi;
652 const struct nvme_namespace_data *nsd;
653 const struct nvme_controller_data *cd;
654 uint8_t flbas_fmt, lbads, vwc_present;
655
656 nsd = nvme_get_identify_ns(periph);
657 cd = nvme_get_identify_cntrl(periph);
658
659 flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, nsd->flbas);
660 lbads = NVMEV(NVME_NS_DATA_LBAF_LBADS, nsd->lbaf[flbas_fmt]);
661 disk->d_sectorsize = 1 << lbads;
662 disk->d_mediasize = (off_t)(disk->d_sectorsize * nsd->nsze);
663 disk->d_delmaxsize = disk->d_mediasize;
664 disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
665 if (nvme_ctrlr_has_dataset_mgmt(cd))
666 disk->d_flags |= DISKFLAG_CANDELETE;
667 vwc_present = NVMEV(NVME_CTRLR_DATA_VWC_PRESENT, cd->vwc);
668 if (vwc_present)
669 disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
670 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
671 disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
672 softc->unmappedio = 1;
673 }
674 }
675
676 static void
ndaasync(void * callback_arg,uint32_t code,struct cam_path * path,void * arg)677 ndaasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
678 {
679 struct cam_periph *periph = callback_arg;
680 struct nda_softc *softc;
681
682 switch (code) {
683 case AC_FOUND_DEVICE:
684 {
685 struct ccb_getdev *cgd;
686 cam_status status;
687
688 cgd = (struct ccb_getdev *)arg;
689 if (cgd == NULL)
690 break;
691
692 if (cgd->protocol != PROTO_NVME)
693 break;
694
695 /*
696 * Allocate a peripheral instance for
697 * this device and start the probe
698 * process.
699 */
700 status = cam_periph_alloc(ndaregister, ndaoninvalidate,
701 ndacleanup, ndastart,
702 "nda", CAM_PERIPH_BIO,
703 path, ndaasync,
704 AC_FOUND_DEVICE, cgd);
705
706 if (status != CAM_REQ_CMP
707 && status != CAM_REQ_INPROG)
708 printf("ndaasync: Unable to attach to new device "
709 "due to status 0x%x\n", status);
710 break;
711 }
712 case AC_GETDEV_CHANGED:
713 {
714 int error;
715
716 softc = periph->softc;
717 ndasetgeom(softc, periph);
718 error = disk_resize(softc->disk, M_NOWAIT);
719 if (error != 0) {
720 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
721 break;
722 }
723 break;
724
725 }
726 case AC_ADVINFO_CHANGED:
727 {
728 uintptr_t buftype;
729
730 softc = periph->softc;
731 buftype = (uintptr_t)arg;
732 if (buftype == CDAI_TYPE_PHYS_PATH) {
733 disk_attr_changed(softc->disk, "GEOM::physpath",
734 M_NOWAIT);
735 }
736 break;
737 }
738 case AC_LOST_DEVICE:
739 default:
740 break;
741 }
742 cam_periph_async(periph, code, path, arg);
743 }
744
745 static void
ndasysctlinit(void * context,int pending)746 ndasysctlinit(void *context, int pending)
747 {
748 struct cam_periph *periph;
749 struct nda_softc *softc;
750 char tmpstr[32], tmpstr2[16];
751
752 periph = (struct cam_periph *)context;
753
754 /* periph was held for us when this task was enqueued */
755 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
756 cam_periph_release(periph);
757 return;
758 }
759
760 softc = (struct nda_softc *)periph->softc;
761 snprintf(tmpstr, sizeof(tmpstr), "CAM NDA unit %d", periph->unit_number);
762 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
763
764 sysctl_ctx_init(&softc->sysctl_ctx);
765 softc->flags |= NDA_FLAG_SCTX_INIT;
766 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
767 SYSCTL_STATIC_CHILDREN(_kern_cam_nda), OID_AUTO, tmpstr2,
768 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr, "device_index");
769 if (softc->sysctl_tree == NULL) {
770 printf("ndasysctlinit: unable to allocate sysctl tree\n");
771 cam_periph_release(periph);
772 return;
773 }
774
775 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
776 OID_AUTO, "unmapped_io", CTLFLAG_RD,
777 &softc->unmappedio, 0, "Unmapped I/O leaf");
778
779 SYSCTL_ADD_QUAD(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
780 OID_AUTO, "deletes", CTLFLAG_RD,
781 &softc->deletes, "Number of BIO_DELETE requests");
782
783 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
784 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
785 "trim_count", CTLFLAG_RD, &softc->trim_count,
786 "Total number of unmap/dsm commands sent");
787 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
788 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
789 "trim_ranges", CTLFLAG_RD, &softc->trim_ranges,
790 "Total number of ranges in unmap/dsm commands");
791 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
792 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
793 "trim_lbas", CTLFLAG_RD, &softc->trim_lbas,
794 "Total lbas in the unmap/dsm commands sent");
795
796 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
797 OID_AUTO, "rotating", CTLFLAG_RD, &nda_rotating_media, 1,
798 "Rotating media");
799
800 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
801 OID_AUTO, "flags", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
802 softc, 0, ndaflagssysctl, "A",
803 "Flags for drive");
804
805 #ifdef CAM_IO_STATS
806 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
807 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
808 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Statistics");
809 if (softc->sysctl_stats_tree == NULL) {
810 printf("ndasysctlinit: unable to allocate sysctl tree for stats\n");
811 cam_periph_release(periph);
812 return;
813 }
814 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
815 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
816 OID_AUTO, "timeouts", CTLFLAG_RD,
817 &softc->timeouts, 0,
818 "Device timeouts reported by the SIM");
819 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
820 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
821 OID_AUTO, "errors", CTLFLAG_RD,
822 &softc->errors, 0,
823 "Transport errors reported by the SIM.");
824 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
825 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
826 OID_AUTO, "pack_invalidations", CTLFLAG_RD,
827 &softc->invalidations, 0,
828 "Device pack invalidations.");
829 #endif
830
831 #ifdef CAM_TEST_FAILURE
832 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
833 OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
834 periph, 0, cam_periph_invalidate_sysctl, "I",
835 "Write 1 to invalidate the drive immediately");
836 #endif
837
838 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
839 softc->sysctl_tree);
840
841 cam_periph_release(periph);
842 }
843
844 static int
ndaflagssysctl(SYSCTL_HANDLER_ARGS)845 ndaflagssysctl(SYSCTL_HANDLER_ARGS)
846 {
847 struct sbuf sbuf;
848 struct nda_softc *softc = arg1;
849 int error;
850
851 sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
852 if (softc->flags != 0)
853 sbuf_printf(&sbuf, "0x%b", (unsigned)softc->flags, NDA_FLAG_STRING);
854 else
855 sbuf_putc(&sbuf, '0');
856 error = sbuf_finish(&sbuf);
857 sbuf_delete(&sbuf);
858
859 return (error);
860 }
861
862 static int
ndagetattr(struct bio * bp)863 ndagetattr(struct bio *bp)
864 {
865 int ret;
866 struct cam_periph *periph;
867
868 if (g_handleattr_int(bp, "GEOM::canspeedup", nda_enable_biospeedup))
869 return (EJUSTRETURN);
870
871 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
872 cam_periph_lock(periph);
873 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
874 periph->path);
875 cam_periph_unlock(periph);
876 if (ret == 0)
877 bp->bio_completed = bp->bio_length;
878 return ret;
879 }
880
881 static cam_status
ndaregister(struct cam_periph * periph,void * arg)882 ndaregister(struct cam_periph *periph, void *arg)
883 {
884 struct nda_softc *softc;
885 struct disk *disk;
886 struct ccb_pathinq cpi;
887 const struct nvme_namespace_data *nsd;
888 const struct nvme_controller_data *cd;
889 char announce_buf[80];
890 u_int maxio;
891 int quirks;
892
893 nsd = nvme_get_identify_ns(periph);
894 cd = nvme_get_identify_cntrl(periph);
895
896 softc = (struct nda_softc *)malloc(sizeof(*softc), M_DEVBUF,
897 M_NOWAIT | M_ZERO);
898
899 if (softc == NULL) {
900 printf("ndaregister: Unable to probe new device. "
901 "Unable to allocate softc\n");
902 return(CAM_REQ_CMP_ERR);
903 }
904
905 /* ident_data parsing */
906
907 periph->softc = softc;
908 softc->quirks = NDA_Q_NONE;
909 xpt_path_inq(&cpi, periph->path);
910 TASK_INIT(&softc->sysctl_task, 0, ndasysctlinit, periph);
911
912 /*
913 * The name space ID is the lun, save it for later I/O
914 */
915 softc->nsid = (uint32_t)xpt_path_lun_id(periph->path);
916
917 /*
918 * Register this media as a disk
919 */
920 (void)cam_periph_acquire(periph);
921 cam_periph_unlock(periph);
922 snprintf(announce_buf, sizeof(announce_buf),
923 "kern.cam.nda.%d.quirks", periph->unit_number);
924 quirks = softc->quirks;
925 TUNABLE_INT_FETCH(announce_buf, &quirks);
926 softc->quirks = quirks;
927 softc->disk = disk = disk_alloc();
928 disk->d_rotation_rate = DISK_RR_NON_ROTATING;
929 disk->d_open = ndaopen;
930 disk->d_close = ndaclose;
931 disk->d_strategy = ndastrategy;
932 disk->d_ioctl = ndaioctl;
933 disk->d_getattr = ndagetattr;
934 if (cam_sim_pollable(periph->sim))
935 disk->d_dump = ndadump;
936 disk->d_gone = ndadiskgonecb;
937 disk->d_name = "nda";
938 disk->d_drv1 = periph;
939 disk->d_unit = periph->unit_number;
940 maxio = cpi.maxio; /* Honor max I/O size of SIM */
941 if (maxio == 0)
942 maxio = DFLTPHYS; /* traditional default */
943 else if (maxio > maxphys)
944 maxio = maxphys; /* for safety */
945 disk->d_maxsize = maxio;
946 ndasetgeom(softc, periph);
947
948 /*
949 * d_ident and d_descr are both far bigger than the length of either
950 * the serial or model number strings.
951 */
952 cam_strvis_flag(disk->d_descr, cd->mn, NVME_MODEL_NUMBER_LENGTH,
953 sizeof(disk->d_descr), CAM_STRVIS_FLAG_NONASCII_SPC);
954
955 cam_strvis_flag(disk->d_ident, cd->sn, NVME_SERIAL_NUMBER_LENGTH,
956 sizeof(disk->d_ident), CAM_STRVIS_FLAG_NONASCII_SPC);
957
958 disk->d_hba_vendor = cpi.hba_vendor;
959 disk->d_hba_device = cpi.hba_device;
960 disk->d_hba_subvendor = cpi.hba_subvendor;
961 disk->d_hba_subdevice = cpi.hba_subdevice;
962 snprintf(disk->d_attachment, sizeof(disk->d_attachment),
963 "%s%d", cpi.dev_name, cpi.unit_number);
964 if (NVMEV(NVME_NS_DATA_NSFEAT_NPVALID, nsd->nsfeat) != 0 &&
965 nsd->npwg != 0)
966 disk->d_stripesize = ((nsd->npwg + 1) * disk->d_sectorsize);
967 else
968 disk->d_stripesize = nsd->noiob * disk->d_sectorsize;
969 disk->d_stripeoffset = 0;
970 disk->d_devstat = devstat_new_entry(periph->periph_name,
971 periph->unit_number, disk->d_sectorsize,
972 DEVSTAT_ALL_SUPPORTED,
973 DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport),
974 DEVSTAT_PRIORITY_DISK);
975
976 if (cam_iosched_init(&softc->cam_iosched, periph, disk,
977 ndaschedule) != 0) {
978 printf("ndaregister: Unable to probe new device. "
979 "Unable to allocate iosched memory\n");
980 free(softc, M_DEVBUF);
981 return(CAM_REQ_CMP_ERR);
982 }
983 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
984
985 /*
986 * Add alias for older nvd drives to ease transition.
987 */
988 if (nda_nvd_compat)
989 disk_add_alias(disk, "nvd");
990
991 cam_periph_lock(periph);
992
993 snprintf(announce_buf, sizeof(announce_buf),
994 "%juMB (%ju %u byte sectors)",
995 (uintmax_t)((uintmax_t)disk->d_mediasize / (1024*1024)),
996 (uintmax_t)disk->d_mediasize / disk->d_sectorsize,
997 disk->d_sectorsize);
998 xpt_announce_periph(periph, announce_buf);
999 xpt_announce_quirks(periph, softc->quirks, NDA_Q_BIT_STRING);
1000
1001 /*
1002 * Create our sysctl variables, now that we know
1003 * we have successfully attached.
1004 */
1005 if (cam_periph_acquire(periph) == 0)
1006 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1007
1008 /*
1009 * Register for device going away and info about the drive
1010 * changing (though with NVMe, it can't)
1011 */
1012 xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED | AC_GETDEV_CHANGED,
1013 ndaasync, periph, periph->path);
1014
1015 softc->state = NDA_STATE_NORMAL;
1016
1017 /*
1018 * We'll release this reference once GEOM calls us back via
1019 * ndadiskgonecb(), telling us that our provider has been freed.
1020 */
1021 if (cam_periph_acquire(periph) == 0)
1022 disk_create(softc->disk, DISK_VERSION);
1023
1024 cam_periph_release_locked(periph);
1025 return(CAM_REQ_CMP);
1026 }
1027
1028 static void
ndastart(struct cam_periph * periph,union ccb * start_ccb)1029 ndastart(struct cam_periph *periph, union ccb *start_ccb)
1030 {
1031 struct nda_softc *softc = (struct nda_softc *)periph->softc;
1032 struct ccb_nvmeio *nvmeio = &start_ccb->nvmeio;
1033
1034 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart\n"));
1035
1036 switch (softc->state) {
1037 case NDA_STATE_NORMAL:
1038 {
1039 struct bio *bp;
1040
1041 bp = cam_iosched_next_bio(softc->cam_iosched);
1042 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart: bio %p\n", bp));
1043 if (bp == NULL) {
1044 xpt_release_ccb(start_ccb);
1045 break;
1046 }
1047
1048 switch (bp->bio_cmd) {
1049 case BIO_WRITE:
1050 softc->flags |= NDA_FLAG_DIRTY;
1051 /* FALLTHROUGH */
1052 case BIO_READ:
1053 {
1054 #ifdef CAM_TEST_FAILURE
1055 int fail = 0;
1056
1057 /*
1058 * Support the failure ioctls. If the command is a
1059 * read, and there are pending forced read errors, or
1060 * if a write and pending write errors, then fail this
1061 * operation with EIO. This is useful for testing
1062 * purposes. Also, support having every Nth read fail.
1063 *
1064 * This is a rather blunt tool.
1065 */
1066 if (bp->bio_cmd == BIO_READ) {
1067 if (softc->force_read_error) {
1068 softc->force_read_error--;
1069 fail = 1;
1070 }
1071 if (softc->periodic_read_error > 0) {
1072 if (++softc->periodic_read_count >=
1073 softc->periodic_read_error) {
1074 softc->periodic_read_count = 0;
1075 fail = 1;
1076 }
1077 }
1078 } else {
1079 if (softc->force_write_error) {
1080 softc->force_write_error--;
1081 fail = 1;
1082 }
1083 }
1084 if (fail) {
1085 biofinish(bp, NULL, EIO);
1086 xpt_release_ccb(start_ccb);
1087 ndaschedule(periph);
1088 return;
1089 }
1090 #endif
1091 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
1092 round_page(bp->bio_bcount + bp->bio_ma_offset) /
1093 PAGE_SIZE == bp->bio_ma_n,
1094 ("Short bio %p", bp));
1095 nda_nvme_rw_bio(softc, &start_ccb->nvmeio, bp, bp->bio_cmd == BIO_READ ?
1096 NVME_OPC_READ : NVME_OPC_WRITE);
1097 break;
1098 }
1099 case BIO_DELETE:
1100 {
1101 struct nvme_dsm_range *dsm_range, *dsm_end;
1102 struct nda_trim_request *trim;
1103 struct bio *bp1;
1104 int ents;
1105 uint32_t totalcount = 0, ranges = 0;
1106
1107 trim = malloc(sizeof(*trim), M_NVMEDA, M_ZERO | M_NOWAIT);
1108 if (trim == NULL) {
1109 /*
1110 * We have to drop the periph lock when
1111 * returning ENOMEM. g_io_deliver treats these
1112 * request differently and will recursively call
1113 * the start routine which causes us to get into
1114 * ndastrategy with the periph lock held,
1115 * leading to a panic when its acquired again.
1116 */
1117 cam_periph_unlock(periph);
1118 biofinish(bp, NULL, ENOMEM);
1119 cam_periph_lock(periph);
1120 xpt_release_ccb(start_ccb);
1121 ndaschedule(periph);
1122 return;
1123 }
1124 TAILQ_INIT(&trim->bps);
1125 bp1 = bp;
1126 ents = min(nitems(trim->dsm), nda_max_trim_entries);
1127 ents = max(ents, 1);
1128 dsm_range = trim->dsm;
1129 dsm_end = dsm_range + ents;
1130 do {
1131 TAILQ_INSERT_TAIL(&trim->bps, bp1, bio_queue);
1132 dsm_range->length =
1133 htole32(bp1->bio_bcount / softc->disk->d_sectorsize);
1134 dsm_range->starting_lba =
1135 htole64(bp1->bio_offset / softc->disk->d_sectorsize);
1136 ranges++;
1137 totalcount += dsm_range->length;
1138 dsm_range++;
1139 if (dsm_range >= dsm_end)
1140 break;
1141 bp1 = cam_iosched_next_trim(softc->cam_iosched);
1142 /* XXX -- Could collapse adjacent ranges, but we don't for now */
1143 /* XXX -- Could limit based on total payload size */
1144 } while (bp1 != NULL);
1145 start_ccb->ccb_trim = trim;
1146 nda_nvme_trim(softc, &start_ccb->nvmeio, trim->dsm,
1147 dsm_range - trim->dsm);
1148 start_ccb->ccb_state = NDA_CCB_TRIM;
1149 softc->trim_count++;
1150 softc->trim_ranges += ranges;
1151 softc->trim_lbas += totalcount;
1152 /*
1153 * Note: We can have multiple TRIMs in flight, so we don't call
1154 * cam_iosched_submit_trim(softc->cam_iosched);
1155 * since that forces the I/O scheduler to only schedule one at a time.
1156 * On NVMe drives, this is a performance disaster.
1157 */
1158 goto out;
1159 }
1160 case BIO_FLUSH:
1161 nda_nvme_flush(softc, nvmeio);
1162 break;
1163 default:
1164 biofinish(bp, NULL, EOPNOTSUPP);
1165 xpt_release_ccb(start_ccb);
1166 ndaschedule(periph);
1167 return;
1168 }
1169 start_ccb->ccb_state = NDA_CCB_BUFFER_IO;
1170 start_ccb->ccb_bp = bp;
1171 out:
1172 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1173 softc->outstanding_cmds++;
1174 softc->refcount++; /* For submission only */
1175 cam_periph_unlock(periph);
1176 xpt_action(start_ccb);
1177 cam_periph_lock(periph);
1178 softc->refcount--; /* Submission done */
1179
1180 /* May have more work to do, so ensure we stay scheduled */
1181 ndaschedule(periph);
1182 break;
1183 }
1184 }
1185 }
1186
1187 static void
ndadone(struct cam_periph * periph,union ccb * done_ccb)1188 ndadone(struct cam_periph *periph, union ccb *done_ccb)
1189 {
1190 struct nda_softc *softc;
1191 struct ccb_nvmeio *nvmeio = &done_ccb->nvmeio;
1192 struct cam_path *path;
1193 int state;
1194
1195 softc = (struct nda_softc *)periph->softc;
1196 path = done_ccb->ccb_h.path;
1197
1198 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("ndadone\n"));
1199
1200 state = nvmeio->ccb_state & NDA_CCB_TYPE_MASK;
1201 switch (state) {
1202 case NDA_CCB_BUFFER_IO:
1203 case NDA_CCB_TRIM:
1204 {
1205 int error;
1206
1207 cam_periph_lock(periph);
1208 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1209 error = ndaerror(done_ccb, 0, 0);
1210 if (error == ERESTART) {
1211 /* A retry was scheduled, so just return. */
1212 cam_periph_unlock(periph);
1213 return;
1214 }
1215 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1216 cam_release_devq(path,
1217 /*relsim_flags*/0,
1218 /*reduction*/0,
1219 /*timeout*/0,
1220 /*getcount_only*/0);
1221 } else {
1222 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1223 panic("REQ_CMP with QFRZN");
1224 error = 0;
1225 }
1226 if (state == NDA_CCB_BUFFER_IO) {
1227 struct bio *bp;
1228
1229 bp = (struct bio *)done_ccb->ccb_bp;
1230 bp->bio_error = error;
1231 if (error != 0) {
1232 bp->bio_resid = bp->bio_bcount;
1233 bp->bio_flags |= BIO_ERROR;
1234 } else {
1235 bp->bio_resid = 0;
1236 }
1237 softc->outstanding_cmds--;
1238
1239 /*
1240 * We need to call cam_iosched before we call biodone so that we
1241 * don't measure any activity that happens in the completion
1242 * routine, which in the case of sendfile can be quite
1243 * extensive.
1244 */
1245 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
1246 xpt_release_ccb(done_ccb);
1247 ndaschedule(periph);
1248 cam_periph_unlock(periph);
1249 biodone(bp);
1250 } else { /* state == NDA_CCB_TRIM */
1251 struct nda_trim_request *trim;
1252 struct bio *bp1, *bp2;
1253 TAILQ_HEAD(, bio) queue;
1254
1255 trim = nvmeio->ccb_trim;
1256 TAILQ_INIT(&queue);
1257 TAILQ_CONCAT(&queue, &trim->bps, bio_queue);
1258 free(trim, M_NVMEDA);
1259
1260 /*
1261 * Since we can have multiple trims in flight, we don't
1262 * need to call this here.
1263 * cam_iosched_trim_done(softc->cam_iosched);
1264 */
1265 /*
1266 * The the I/O scheduler that we're finishing the I/O
1267 * so we can keep book. The first one we pass in the CCB
1268 * which has the timing information. The rest we pass in NULL
1269 * so we can keep proper counts.
1270 */
1271 bp1 = TAILQ_FIRST(&queue);
1272 cam_iosched_bio_complete(softc->cam_iosched, bp1, done_ccb);
1273 xpt_release_ccb(done_ccb);
1274 softc->outstanding_cmds--;
1275 ndaschedule(periph);
1276 cam_periph_unlock(periph);
1277 while ((bp2 = TAILQ_FIRST(&queue)) != NULL) {
1278 TAILQ_REMOVE(&queue, bp2, bio_queue);
1279 bp2->bio_error = error;
1280 if (error != 0) {
1281 bp2->bio_flags |= BIO_ERROR;
1282 bp2->bio_resid = bp1->bio_bcount;
1283 } else
1284 bp2->bio_resid = 0;
1285 if (bp1 != bp2)
1286 cam_iosched_bio_complete(softc->cam_iosched, bp2, NULL);
1287 biodone(bp2);
1288 }
1289 }
1290 return;
1291 }
1292 case NDA_CCB_DUMP:
1293 /* No-op. We're polling */
1294 return;
1295 case NDA_CCB_PASS:
1296 /* NVME_PASSTHROUGH_CMD runs this CCB and releases it */
1297 return;
1298 default:
1299 break;
1300 }
1301 xpt_release_ccb(done_ccb);
1302 }
1303
1304 static int
ndaerror(union ccb * ccb,uint32_t cam_flags,uint32_t sense_flags)1305 ndaerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
1306 {
1307 #ifdef CAM_IO_STATS
1308 struct nda_softc *softc;
1309 struct cam_periph *periph;
1310
1311 periph = xpt_path_periph(ccb->ccb_h.path);
1312 softc = (struct nda_softc *)periph->softc;
1313 #endif
1314
1315 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1316 case CAM_CMD_TIMEOUT:
1317 #ifdef CAM_IO_STATS
1318 softc->timeouts++;
1319 #endif
1320 break;
1321 case CAM_REQ_CMP_ERR:
1322 case CAM_NVME_STATUS_ERROR:
1323 #ifdef CAM_IO_STATS
1324 softc->errors++;
1325 #endif
1326 break;
1327 default:
1328 break;
1329 }
1330
1331 return(cam_periph_error(ccb, cam_flags, sense_flags));
1332 }
1333
1334 /*
1335 * Step through all NDA peripheral drivers, and if the device is still open,
1336 * sync the disk cache to physical media.
1337 */
1338 static void
ndaflush(void)1339 ndaflush(void)
1340 {
1341 struct cam_periph *periph;
1342 struct nda_softc *softc;
1343 union ccb *ccb;
1344 int error;
1345
1346 CAM_PERIPH_FOREACH(periph, &ndadriver) {
1347 softc = (struct nda_softc *)periph->softc;
1348
1349 if (SCHEDULER_STOPPED()) {
1350 /*
1351 * If we panicked with the lock held or the periph is not
1352 * open, do not recurse. Otherwise, call ndadump since
1353 * that avoids the sleeping cam_periph_getccb does if no
1354 * CCBs are available.
1355 */
1356 if (!cam_periph_owned(periph) &&
1357 (softc->flags & NDA_FLAG_OPEN)) {
1358 ndadump(softc->disk, NULL, 0, 0);
1359 }
1360 continue;
1361 }
1362
1363 /*
1364 * We only sync the cache if the drive is still open
1365 */
1366 cam_periph_lock(periph);
1367 if ((softc->flags & NDA_FLAG_OPEN) == 0) {
1368 cam_periph_unlock(periph);
1369 continue;
1370 }
1371
1372 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1373 nda_nvme_flush(softc, &ccb->nvmeio);
1374 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
1375 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1376 softc->disk->d_devstat);
1377 if (error != 0)
1378 xpt_print(periph->path, "Synchronize cache failed\n");
1379 xpt_release_ccb(ccb);
1380 cam_periph_unlock(periph);
1381 }
1382 }
1383
1384 static void
ndashutdown(void * arg,int howto)1385 ndashutdown(void *arg, int howto)
1386 {
1387
1388 if ((howto & RB_NOSYNC) != 0)
1389 return;
1390
1391 ndaflush();
1392 }
1393
1394 static void
ndasuspend(void * arg,enum power_stype stype)1395 ndasuspend(void *arg, enum power_stype stype)
1396 {
1397
1398 ndaflush();
1399 }
1400