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 * without modification, immediately at the beginning of the file.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * derived from ata_xpt.c: Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
28 */
29
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/endian.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/time.h>
39 #include <sys/conf.h>
40 #include <sys/fcntl.h>
41 #include <sys/sbuf.h>
42
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/sysctl.h>
46
47 #include <cam/cam.h>
48 #include <cam/cam_ccb.h>
49 #include <cam/cam_queue.h>
50 #include <cam/cam_periph.h>
51 #include <cam/cam_sim.h>
52 #include <cam/cam_xpt.h>
53 #include <cam/cam_xpt_sim.h>
54 #include <cam/cam_xpt_periph.h>
55 #include <cam/cam_xpt_internal.h>
56 #include <cam/cam_debug.h>
57
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_message.h>
60 #include <cam/nvme/nvme_all.h>
61 #include <machine/stdarg.h> /* for xpt_print below */
62 #include "opt_cam.h"
63
64 struct nvme_quirk_entry {
65 u_int quirks;
66 #define CAM_QUIRK_MAXTAGS 1
67 u_int mintags;
68 u_int maxtags;
69 };
70
71 /* Not even sure why we need this */
72 static periph_init_t nvme_probe_periph_init;
73
74 static struct periph_driver nvme_probe_driver =
75 {
76 nvme_probe_periph_init, "nvme_probe",
77 TAILQ_HEAD_INITIALIZER(nvme_probe_driver.units), /* generation */ 0,
78 CAM_PERIPH_DRV_EARLY
79 };
80
81 PERIPHDRIVER_DECLARE(nvme_probe, nvme_probe_driver);
82
83 typedef enum {
84 NVME_PROBE_IDENTIFY_CD,
85 NVME_PROBE_IDENTIFY_NS,
86 NVME_PROBE_DONE,
87 NVME_PROBE_INVALID
88 } nvme_probe_action;
89
90 static char *nvme_probe_action_text[] = {
91 "NVME_PROBE_IDENTIFY_CD",
92 "NVME_PROBE_IDENTIFY_NS",
93 "NVME_PROBE_DONE",
94 "NVME_PROBE_INVALID"
95 };
96
97 #define NVME_PROBE_SET_ACTION(softc, newaction) \
98 do { \
99 char **text; \
100 text = nvme_probe_action_text; \
101 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
102 ("Probe %s to %s\n", text[(softc)->action], \
103 text[(newaction)])); \
104 (softc)->action = (newaction); \
105 } while(0)
106
107 typedef enum {
108 NVME_PROBE_NO_ANNOUNCE = 0x04
109 } nvme_probe_flags;
110
111 typedef struct {
112 TAILQ_HEAD(, ccb_hdr) request_ccbs;
113 union {
114 struct nvme_controller_data cd;
115 struct nvme_namespace_data ns;
116 };
117 nvme_probe_action action;
118 nvme_probe_flags flags;
119 int restart;
120 struct cam_periph *periph;
121 } nvme_probe_softc;
122
123 static struct nvme_quirk_entry nvme_quirk_table[] =
124 {
125 {
126 // {
127 // T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
128 // /*vendor*/"*", /*product*/"*", /*revision*/"*"
129 // },
130 .quirks = 0, .mintags = 0, .maxtags = 0
131 },
132 };
133
134 static const int nvme_quirk_table_size =
135 sizeof(nvme_quirk_table) / sizeof(*nvme_quirk_table);
136
137 static cam_status nvme_probe_register(struct cam_periph *periph,
138 void *arg);
139 static void nvme_probe_schedule(struct cam_periph *nvme_probe_periph);
140 static void nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb);
141 static void nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb);
142 static void nvme_probe_cleanup(struct cam_periph *periph);
143 //static void nvme_find_quirk(struct cam_ed *device);
144 static void nvme_scan_lun(struct cam_periph *periph,
145 struct cam_path *path, cam_flags flags,
146 union ccb *ccb);
147 static struct cam_ed *
148 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target,
149 lun_id_t lun_id);
150 static void nvme_device_transport(struct cam_path *path);
151 static void nvme_dev_async(u_int32_t async_code,
152 struct cam_eb *bus,
153 struct cam_et *target,
154 struct cam_ed *device,
155 void *async_arg);
156 static void nvme_action(union ccb *start_ccb);
157 static void nvme_announce_periph(struct cam_periph *periph);
158 static void nvme_proto_announce(struct cam_ed *device);
159 static void nvme_proto_denounce(struct cam_ed *device);
160 static void nvme_proto_debug_out(union ccb *ccb);
161
162 static struct xpt_xport_ops nvme_xport_ops = {
163 .alloc_device = nvme_alloc_device,
164 .action = nvme_action,
165 .async = nvme_dev_async,
166 .announce = nvme_announce_periph,
167 };
168 #define NVME_XPT_XPORT(x, X) \
169 static struct xpt_xport nvme_xport_ ## x = { \
170 .xport = XPORT_ ## X, \
171 .name = #x, \
172 .ops = &nvme_xport_ops, \
173 }; \
174 CAM_XPT_XPORT(nvme_xport_ ## x);
175
176 NVME_XPT_XPORT(nvme, NVME);
177
178 #undef NVME_XPT_XPORT
179
180 static struct xpt_proto_ops nvme_proto_ops = {
181 .announce = nvme_proto_announce,
182 .denounce = nvme_proto_denounce,
183 .debug_out = nvme_proto_debug_out,
184 };
185 static struct xpt_proto nvme_proto = {
186 .proto = PROTO_NVME,
187 .name = "nvme",
188 .ops = &nvme_proto_ops,
189 };
190 CAM_XPT_PROTO(nvme_proto);
191
192 static void
nvme_probe_periph_init(void)193 nvme_probe_periph_init(void)
194 {
195 }
196
197 static cam_status
nvme_probe_register(struct cam_periph * periph,void * arg)198 nvme_probe_register(struct cam_periph *periph, void *arg)
199 {
200 union ccb *request_ccb; /* CCB representing the probe request */
201 nvme_probe_softc *softc;
202
203 request_ccb = (union ccb *)arg;
204 if (request_ccb == NULL) {
205 printf("nvme_probe_register: no probe CCB, "
206 "can't register device\n");
207 return(CAM_REQ_CMP_ERR);
208 }
209
210 softc = (nvme_probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
211
212 if (softc == NULL) {
213 printf("nvme_probe_register: Unable to probe new device. "
214 "Unable to allocate softc\n");
215 return(CAM_REQ_CMP_ERR);
216 }
217 TAILQ_INIT(&softc->request_ccbs);
218 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
219 periph_links.tqe);
220 softc->flags = 0;
221 periph->softc = softc;
222 softc->periph = periph;
223 softc->action = NVME_PROBE_INVALID;
224 if (cam_periph_acquire(periph) != 0)
225 return (CAM_REQ_CMP_ERR);
226
227 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
228
229 // nvme_device_transport(periph->path);
230 nvme_probe_schedule(periph);
231
232 return(CAM_REQ_CMP);
233 }
234
235 static void
nvme_probe_schedule(struct cam_periph * periph)236 nvme_probe_schedule(struct cam_periph *periph)
237 {
238 union ccb *ccb;
239 nvme_probe_softc *softc;
240
241 softc = (nvme_probe_softc *)periph->softc;
242 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
243
244 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
245
246 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
247 softc->flags |= NVME_PROBE_NO_ANNOUNCE;
248 else
249 softc->flags &= ~NVME_PROBE_NO_ANNOUNCE;
250
251 xpt_schedule(periph, CAM_PRIORITY_XPT);
252 }
253
254 static void
nvme_probe_start(struct cam_periph * periph,union ccb * start_ccb)255 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb)
256 {
257 struct ccb_nvmeio *nvmeio;
258 nvme_probe_softc *softc;
259 lun_id_t lun;
260
261 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_start\n"));
262
263 softc = (nvme_probe_softc *)periph->softc;
264 nvmeio = &start_ccb->nvmeio;
265 lun = xpt_path_lun_id(periph->path);
266
267 if (softc->restart) {
268 softc->restart = 0;
269 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
270 }
271
272 switch (softc->action) {
273 case NVME_PROBE_IDENTIFY_CD:
274 cam_fill_nvmeadmin(nvmeio,
275 0, /* retries */
276 nvme_probe_done, /* cbfcnp */
277 CAM_DIR_IN, /* flags */
278 (uint8_t *)&softc->cd, /* data_ptr */
279 sizeof(softc->cd), /* dxfer_len */
280 30 * 1000); /* timeout 30s */
281 nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, 0,
282 1, 0, 0, 0, 0, 0);
283 break;
284 case NVME_PROBE_IDENTIFY_NS:
285 cam_fill_nvmeadmin(nvmeio,
286 0, /* retries */
287 nvme_probe_done, /* cbfcnp */
288 CAM_DIR_IN, /* flags */
289 (uint8_t *)&softc->ns, /* data_ptr */
290 sizeof(softc->ns), /* dxfer_len */
291 30 * 1000); /* timeout 30s */
292 nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, lun,
293 0, 0, 0, 0, 0, 0);
294 break;
295 default:
296 panic("nvme_probe_start: invalid action state 0x%x\n", softc->action);
297 }
298 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
299 xpt_action(start_ccb);
300 }
301
302 static void
nvme_probe_done(struct cam_periph * periph,union ccb * done_ccb)303 nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb)
304 {
305 struct nvme_namespace_data *nvme_data;
306 struct nvme_controller_data *nvme_cdata;
307 nvme_probe_softc *softc;
308 struct cam_path *path;
309 struct scsi_vpd_device_id *did;
310 struct scsi_vpd_id_descriptor *idd;
311 u_int32_t priority;
312 int found = 1, e, g, len;
313
314 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_done\n"));
315
316 softc = (nvme_probe_softc *)periph->softc;
317 path = done_ccb->ccb_h.path;
318 priority = done_ccb->ccb_h.pinfo.priority;
319
320 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
321 if (cam_periph_error(done_ccb,
322 0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0
323 ) == ERESTART) {
324 out:
325 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
326 cam_release_devq(path, 0, 0, 0, FALSE);
327 return;
328 }
329 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
330 /* Don't wedge the queue */
331 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
332 }
333
334 /*
335 * If we get to this point, we got an error status back
336 * from the inquiry and the error status doesn't require
337 * automatically retrying the command. Therefore, the
338 * inquiry failed. If we had inquiry information before
339 * for this device, but this latest inquiry command failed,
340 * the device has probably gone away. If this device isn't
341 * already marked unconfigured, notify the peripheral
342 * drivers that this device is no more.
343 */
344 device_fail: if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
345 xpt_async(AC_LOST_DEVICE, path, NULL);
346 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_INVALID);
347 found = 0;
348 goto done;
349 }
350 if (softc->restart)
351 goto done;
352 switch (softc->action) {
353 case NVME_PROBE_IDENTIFY_CD:
354 nvme_controller_data_swapbytes(&softc->cd);
355
356 nvme_cdata = path->device->nvme_cdata;
357 if (nvme_cdata == NULL) {
358 nvme_cdata = malloc(sizeof(*nvme_cdata), M_CAMXPT,
359 M_NOWAIT);
360 if (nvme_cdata == NULL) {
361 xpt_print(path, "Can't allocate memory");
362 goto device_fail;
363 }
364 }
365 bcopy(&softc->cd, nvme_cdata, sizeof(*nvme_cdata));
366 path->device->nvme_cdata = nvme_cdata;
367
368 /* Save/update serial number. */
369 if (path->device->serial_num != NULL) {
370 free(path->device->serial_num, M_CAMXPT);
371 path->device->serial_num = NULL;
372 path->device->serial_num_len = 0;
373 }
374 path->device->serial_num = (u_int8_t *)
375 malloc(NVME_SERIAL_NUMBER_LENGTH + 1, M_CAMXPT, M_NOWAIT);
376 if (path->device->serial_num != NULL) {
377 cam_strvis_flag(path->device->serial_num,
378 nvme_cdata->sn, sizeof(nvme_cdata->sn),
379 NVME_SERIAL_NUMBER_LENGTH + 1,
380 CAM_STRVIS_FLAG_NONASCII_SPC);
381
382 path->device->serial_num_len =
383 strlen(path->device->serial_num);
384 }
385
386 // nvme_find_quirk(path->device);
387 nvme_device_transport(path);
388 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_NS);
389 xpt_release_ccb(done_ccb);
390 xpt_schedule(periph, priority);
391 goto out;
392 case NVME_PROBE_IDENTIFY_NS:
393 nvme_namespace_data_swapbytes(&softc->ns);
394
395 /* Check that the namespace exists. */
396 if (softc->ns.nsze == 0)
397 goto device_fail;
398
399 nvme_data = path->device->nvme_data;
400 if (nvme_data == NULL) {
401 nvme_data = malloc(sizeof(*nvme_data), M_CAMXPT,
402 M_NOWAIT);
403 if (nvme_data == NULL) {
404 xpt_print(path, "Can't allocate memory");
405 goto device_fail;
406 }
407 }
408 bcopy(&softc->ns, nvme_data, sizeof(*nvme_data));
409 path->device->nvme_data = nvme_data;
410
411 /* Save/update device_id based on NGUID and/or EUI64. */
412 if (path->device->device_id != NULL) {
413 free(path->device->device_id, M_CAMXPT);
414 path->device->device_id = NULL;
415 path->device->device_id_len = 0;
416 }
417 len = 0;
418 for (g = 0; g < sizeof(nvme_data->nguid); g++) {
419 if (nvme_data->nguid[g] != 0)
420 break;
421 }
422 if (g < sizeof(nvme_data->nguid))
423 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
424 for (e = 0; e < sizeof(nvme_data->eui64); e++) {
425 if (nvme_data->eui64[e] != 0)
426 break;
427 }
428 if (e < sizeof(nvme_data->eui64))
429 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
430 if (len > 0) {
431 path->device->device_id = (u_int8_t *)
432 malloc(SVPD_DEVICE_ID_HDR_LEN + len,
433 M_CAMXPT, M_NOWAIT);
434 }
435 if (path->device->device_id != NULL) {
436 did = (struct scsi_vpd_device_id *)path->device->device_id;
437 did->device = SID_QUAL_LU_CONNECTED | T_DIRECT;
438 did->page_code = SVPD_DEVICE_ID;
439 scsi_ulto2b(len, did->length);
440 idd = (struct scsi_vpd_id_descriptor *)(did + 1);
441 if (g < sizeof(nvme_data->nguid)) {
442 idd->proto_codeset = SVPD_ID_CODESET_BINARY;
443 idd->id_type = SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_EUI64;
444 idd->length = 16;
445 bcopy(nvme_data->nguid, idd->identifier, 16);
446 idd = (struct scsi_vpd_id_descriptor *)
447 &idd->identifier[16];
448 }
449 if (e < sizeof(nvme_data->eui64)) {
450 idd->proto_codeset = SVPD_ID_CODESET_BINARY;
451 idd->id_type = SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_EUI64;
452 idd->length = 8;
453 bcopy(nvme_data->eui64, idd->identifier, 8);
454 }
455 path->device->device_id_len = SVPD_DEVICE_ID_HDR_LEN + len;
456 }
457
458 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
459 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
460 xpt_acquire_device(path->device);
461 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
462 xpt_action(done_ccb);
463 xpt_async(AC_FOUND_DEVICE, path, done_ccb);
464 }
465 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_DONE);
466 break;
467 default:
468 panic("nvme_probe_done: invalid action state 0x%x\n", softc->action);
469 }
470 done:
471 if (softc->restart) {
472 softc->restart = 0;
473 xpt_release_ccb(done_ccb);
474 nvme_probe_schedule(periph);
475 goto out;
476 }
477 xpt_release_ccb(done_ccb);
478 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
479 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
480 TAILQ_REMOVE(&softc->request_ccbs,
481 &done_ccb->ccb_h, periph_links.tqe);
482 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
483 xpt_done(done_ccb);
484 }
485 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
486 cam_release_devq(path, 0, 0, 0, FALSE);
487 cam_periph_invalidate(periph);
488 cam_periph_release_locked(periph);
489 }
490
491 static void
nvme_probe_cleanup(struct cam_periph * periph)492 nvme_probe_cleanup(struct cam_periph *periph)
493 {
494
495 free(periph->softc, M_CAMXPT);
496 }
497
498 #if 0
499 /* XXX should be used, don't delete */
500 static void
501 nvme_find_quirk(struct cam_ed *device)
502 {
503 struct nvme_quirk_entry *quirk;
504 caddr_t match;
505
506 match = cam_quirkmatch((caddr_t)&device->nvme_data,
507 (caddr_t)nvme_quirk_table,
508 nvme_quirk_table_size,
509 sizeof(*nvme_quirk_table), nvme_identify_match);
510
511 if (match == NULL)
512 panic("xpt_find_quirk: device didn't match wildcard entry!!");
513
514 quirk = (struct nvme_quirk_entry *)match;
515 device->quirk = quirk;
516 if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
517 device->mintags = quirk->mintags;
518 device->maxtags = quirk->maxtags;
519 }
520 }
521 #endif
522
523 static void
nvme_scan_lun(struct cam_periph * periph,struct cam_path * path,cam_flags flags,union ccb * request_ccb)524 nvme_scan_lun(struct cam_periph *periph, struct cam_path *path,
525 cam_flags flags, union ccb *request_ccb)
526 {
527 struct ccb_pathinq cpi;
528 cam_status status;
529 struct cam_periph *old_periph;
530 int lock;
531
532 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n"));
533
534 xpt_path_inq(&cpi, path);
535
536 if (cpi.ccb_h.status != CAM_REQ_CMP) {
537 if (request_ccb != NULL) {
538 request_ccb->ccb_h.status = cpi.ccb_h.status;
539 xpt_done(request_ccb);
540 }
541 return;
542 }
543
544 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
545 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n"));
546 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
547 xpt_done(request_ccb);
548 return;
549 }
550
551 lock = (xpt_path_owned(path) == 0);
552 if (lock)
553 xpt_path_lock(path);
554 if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) {
555 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
556 nvme_probe_softc *softc;
557
558 softc = (nvme_probe_softc *)old_periph->softc;
559 TAILQ_INSERT_TAIL(&softc->request_ccbs,
560 &request_ccb->ccb_h, periph_links.tqe);
561 softc->restart = 1;
562 CAM_DEBUG(path, CAM_DEBUG_TRACE,
563 ("restarting nvme_probe device\n"));
564 } else {
565 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
566 CAM_DEBUG(path, CAM_DEBUG_TRACE,
567 ("Failing to restart nvme_probe device\n"));
568 xpt_done(request_ccb);
569 }
570 } else {
571 CAM_DEBUG(path, CAM_DEBUG_TRACE,
572 ("Adding nvme_probe device\n"));
573 status = cam_periph_alloc(nvme_probe_register, NULL, nvme_probe_cleanup,
574 nvme_probe_start, "nvme_probe",
575 CAM_PERIPH_BIO,
576 request_ccb->ccb_h.path, NULL, 0,
577 request_ccb);
578
579 if (status != CAM_REQ_CMP) {
580 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
581 "returned an error, can't continue probe\n");
582 request_ccb->ccb_h.status = status;
583 xpt_done(request_ccb);
584 }
585 }
586 if (lock)
587 xpt_path_unlock(path);
588 }
589
590 static struct cam_ed *
nvme_alloc_device(struct cam_eb * bus,struct cam_et * target,lun_id_t lun_id)591 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
592 {
593 struct nvme_quirk_entry *quirk;
594 struct cam_ed *device;
595
596 device = xpt_alloc_device(bus, target, lun_id);
597 if (device == NULL)
598 return (NULL);
599
600 /*
601 * Take the default quirk entry until we have inquiry
602 * data from nvme and can determine a better quirk to use.
603 */
604 quirk = &nvme_quirk_table[nvme_quirk_table_size - 1];
605 device->quirk = (void *)quirk;
606 device->mintags = 0;
607 device->maxtags = 0;
608 device->inq_flags = 0;
609 device->queue_flags = 0;
610 device->device_id = NULL;
611 device->device_id_len = 0;
612 device->serial_num = NULL;
613 device->serial_num_len = 0;
614 return (device);
615 }
616
617 static void
nvme_device_transport(struct cam_path * path)618 nvme_device_transport(struct cam_path *path)
619 {
620 struct ccb_pathinq cpi;
621 struct ccb_trans_settings cts;
622 /* XXX get data from nvme namespace and other info ??? */
623
624 /* Get transport information from the SIM */
625 xpt_path_inq(&cpi, path);
626
627 path->device->transport = cpi.transport;
628 path->device->transport_version = cpi.transport_version;
629
630 path->device->protocol = cpi.protocol;
631 path->device->protocol_version = cpi.protocol_version;
632
633 /* Tell the controller what we think */
634 memset(&cts, 0, sizeof(cts));
635 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
636 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
637 cts.type = CTS_TYPE_CURRENT_SETTINGS;
638 cts.transport = path->device->transport;
639 cts.transport_version = path->device->transport_version;
640 cts.protocol = path->device->protocol;
641 cts.protocol_version = path->device->protocol_version;
642 cts.proto_specific.valid = 0;
643 cts.xport_specific.valid = 0;
644 xpt_action((union ccb *)&cts);
645 }
646
647 static void
nvme_dev_advinfo(union ccb * start_ccb)648 nvme_dev_advinfo(union ccb *start_ccb)
649 {
650 struct cam_ed *device;
651 struct ccb_dev_advinfo *cdai;
652 off_t amt;
653
654 xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
655 start_ccb->ccb_h.status = CAM_REQ_INVALID;
656 device = start_ccb->ccb_h.path->device;
657 cdai = &start_ccb->cdai;
658 switch(cdai->buftype) {
659 case CDAI_TYPE_SCSI_DEVID:
660 if (cdai->flags & CDAI_FLAG_STORE)
661 return;
662 cdai->provsiz = device->device_id_len;
663 if (device->device_id_len == 0)
664 break;
665 amt = device->device_id_len;
666 if (cdai->provsiz > cdai->bufsiz)
667 amt = cdai->bufsiz;
668 memcpy(cdai->buf, device->device_id, amt);
669 break;
670 case CDAI_TYPE_SERIAL_NUM:
671 if (cdai->flags & CDAI_FLAG_STORE)
672 return;
673 cdai->provsiz = device->serial_num_len;
674 if (device->serial_num_len == 0)
675 break;
676 amt = device->serial_num_len;
677 if (cdai->provsiz > cdai->bufsiz)
678 amt = cdai->bufsiz;
679 memcpy(cdai->buf, device->serial_num, amt);
680 break;
681 case CDAI_TYPE_PHYS_PATH:
682 if (cdai->flags & CDAI_FLAG_STORE) {
683 if (device->physpath != NULL) {
684 free(device->physpath, M_CAMXPT);
685 device->physpath = NULL;
686 device->physpath_len = 0;
687 }
688 /* Clear existing buffer if zero length */
689 if (cdai->bufsiz == 0)
690 break;
691 device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
692 if (device->physpath == NULL) {
693 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
694 return;
695 }
696 device->physpath_len = cdai->bufsiz;
697 memcpy(device->physpath, cdai->buf, cdai->bufsiz);
698 } else {
699 cdai->provsiz = device->physpath_len;
700 if (device->physpath_len == 0)
701 break;
702 amt = device->physpath_len;
703 if (cdai->provsiz > cdai->bufsiz)
704 amt = cdai->bufsiz;
705 memcpy(cdai->buf, device->physpath, amt);
706 }
707 break;
708 case CDAI_TYPE_NVME_CNTRL:
709 if (cdai->flags & CDAI_FLAG_STORE)
710 return;
711 amt = sizeof(struct nvme_controller_data);
712 cdai->provsiz = amt;
713 if (amt > cdai->bufsiz)
714 amt = cdai->bufsiz;
715 memcpy(cdai->buf, device->nvme_cdata, amt);
716 break;
717 case CDAI_TYPE_NVME_NS:
718 if (cdai->flags & CDAI_FLAG_STORE)
719 return;
720 amt = sizeof(struct nvme_namespace_data);
721 cdai->provsiz = amt;
722 if (amt > cdai->bufsiz)
723 amt = cdai->bufsiz;
724 memcpy(cdai->buf, device->nvme_data, amt);
725 break;
726 default:
727 return;
728 }
729 start_ccb->ccb_h.status = CAM_REQ_CMP;
730
731 if (cdai->flags & CDAI_FLAG_STORE) {
732 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
733 (void *)(uintptr_t)cdai->buftype);
734 }
735 }
736
737 static void
nvme_action(union ccb * start_ccb)738 nvme_action(union ccb *start_ccb)
739 {
740 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
741 ("nvme_action: func= %#x\n", start_ccb->ccb_h.func_code));
742
743 switch (start_ccb->ccb_h.func_code) {
744 case XPT_SCAN_BUS:
745 case XPT_SCAN_TGT:
746 case XPT_SCAN_LUN:
747 nvme_scan_lun(start_ccb->ccb_h.path->periph,
748 start_ccb->ccb_h.path, start_ccb->crcn.flags,
749 start_ccb);
750 break;
751 case XPT_DEV_ADVINFO:
752 nvme_dev_advinfo(start_ccb);
753 break;
754
755 default:
756 xpt_action_default(start_ccb);
757 break;
758 }
759 }
760
761 /*
762 * Handle any per-device event notifications that require action by the XPT.
763 */
764 static void
nvme_dev_async(u_int32_t async_code,struct cam_eb * bus,struct cam_et * target,struct cam_ed * device,void * async_arg)765 nvme_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
766 struct cam_ed *device, void *async_arg)
767 {
768
769 /*
770 * We only need to handle events for real devices.
771 */
772 if (target->target_id == CAM_TARGET_WILDCARD
773 || device->lun_id == CAM_LUN_WILDCARD)
774 return;
775
776 if (async_code == AC_LOST_DEVICE &&
777 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
778 device->flags |= CAM_DEV_UNCONFIGURED;
779 xpt_release_device(device);
780 }
781 }
782
783 static void
nvme_announce_periph(struct cam_periph * periph)784 nvme_announce_periph(struct cam_periph *periph)
785 {
786 struct ccb_pathinq cpi;
787 struct ccb_trans_settings cts;
788 struct cam_path *path = periph->path;
789 struct ccb_trans_settings_nvme *nvmex;
790 struct sbuf sb;
791 char buffer[120];
792
793 cam_periph_assert(periph, MA_OWNED);
794
795 /* Ask the SIM for connection details */
796 memset(&cts, 0, sizeof(cts));
797 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
798 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
799 cts.type = CTS_TYPE_CURRENT_SETTINGS;
800 xpt_action((union ccb*)&cts);
801 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
802 return;
803 nvmex = &cts.xport_specific.nvme;
804
805 /* Ask the SIM for its base transfer speed */
806 xpt_path_inq(&cpi, periph->path);
807 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
808 sbuf_printf(&sb, "%s%d: nvme version %d.%d",
809 periph->periph_name, periph->unit_number,
810 NVME_MAJOR(nvmex->spec),
811 NVME_MINOR(nvmex->spec));
812 if (nvmex->valid & CTS_NVME_VALID_LINK)
813 sbuf_printf(&sb, " x%d (max x%d) lanes PCIe Gen%d (max Gen%d) link",
814 nvmex->lanes, nvmex->max_lanes,
815 nvmex->speed, nvmex->max_speed);
816 sbuf_printf(&sb, "\n");
817 sbuf_finish(&sb);
818 sbuf_putbuf(&sb);
819 }
820
821 static void
nvme_proto_announce(struct cam_ed * device)822 nvme_proto_announce(struct cam_ed *device)
823 {
824 struct sbuf sb;
825 char buffer[120];
826
827 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
828 nvme_print_ident(device->nvme_cdata, device->nvme_data, &sb);
829 sbuf_finish(&sb);
830 sbuf_putbuf(&sb);
831 }
832
833 static void
nvme_proto_denounce(struct cam_ed * device)834 nvme_proto_denounce(struct cam_ed *device)
835 {
836
837 nvme_proto_announce(device);
838 }
839
840 static void
nvme_proto_debug_out(union ccb * ccb)841 nvme_proto_debug_out(union ccb *ccb)
842 {
843 char cdb_str[(sizeof(struct nvme_command) * 3) + 1];
844
845 if (ccb->ccb_h.func_code != XPT_NVME_IO &&
846 ccb->ccb_h.func_code != XPT_NVME_ADMIN)
847 return;
848
849 CAM_DEBUG(ccb->ccb_h.path,
850 CAM_DEBUG_CDB,("%s. NCB: %s\n", nvme_op_string(&ccb->nvmeio.cmd,
851 ccb->ccb_h.func_code == XPT_NVME_ADMIN),
852 nvme_cmd_string(&ccb->nvmeio.cmd, cdb_str, sizeof(cdb_str))));
853 }
854