Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/cam/scsi/scsi_enc.c
39478 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2000 Matthew Jacob
5
* All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions, and the following disclaimer,
12
* without modification, immediately at the beginning of the file.
13
* 2. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
15
*
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
* SUCH DAMAGE.
27
*/
28
29
#include <sys/param.h>
30
31
#include <sys/conf.h>
32
#include <sys/errno.h>
33
#include <sys/fcntl.h>
34
#include <sys/kernel.h>
35
#include <sys/kthread.h>
36
#include <sys/lock.h>
37
#include <sys/malloc.h>
38
#include <sys/mutex.h>
39
#include <sys/proc.h>
40
#include <sys/queue.h>
41
#include <sys/sbuf.h>
42
#include <sys/stdarg.h>
43
#include <sys/sx.h>
44
#include <sys/sysent.h>
45
#include <sys/systm.h>
46
#include <sys/sysctl.h>
47
#include <sys/types.h>
48
49
#include <cam/cam.h>
50
#include <cam/cam_ccb.h>
51
#include <cam/cam_debug.h>
52
#include <cam/cam_periph.h>
53
#include <cam/cam_xpt_periph.h>
54
55
#include <cam/scsi/scsi_all.h>
56
#include <cam/scsi/scsi_message.h>
57
#include <cam/scsi/scsi_enc.h>
58
#include <cam/scsi/scsi_enc_internal.h>
59
60
#include "opt_ses.h"
61
62
MALLOC_DEFINE(M_SCSIENC, "SCSI ENC", "SCSI ENC buffers");
63
64
/* Enclosure type independent driver */
65
66
static d_open_t enc_open;
67
static d_close_t enc_close;
68
static d_ioctl_t enc_ioctl;
69
static periph_init_t enc_init;
70
static periph_ctor_t enc_ctor;
71
static periph_oninv_t enc_oninvalidate;
72
static periph_dtor_t enc_dtor;
73
74
static void enc_async(void *, uint32_t, struct cam_path *, void *);
75
static enctyp enc_type(struct ccb_getdev *);
76
77
SYSCTL_NODE(_kern_cam, OID_AUTO, enc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
78
"CAM Enclosure Services driver");
79
80
#if defined(DEBUG) || defined(ENC_DEBUG)
81
int enc_verbose = 1;
82
#else
83
int enc_verbose = 0;
84
#endif
85
SYSCTL_INT(_kern_cam_enc, OID_AUTO, verbose, CTLFLAG_RWTUN,
86
&enc_verbose, 0, "Enable verbose logging");
87
88
const char *elm_type_names[] = ELM_TYPE_NAMES;
89
CTASSERT(nitems(elm_type_names) - 1 == ELMTYP_LAST);
90
91
static struct periph_driver encdriver = {
92
enc_init, "ses",
93
TAILQ_HEAD_INITIALIZER(encdriver.units), /* generation */ 0
94
};
95
96
PERIPHDRIVER_DECLARE(enc, encdriver);
97
98
static struct cdevsw enc_cdevsw = {
99
.d_version = D_VERSION,
100
.d_open = enc_open,
101
.d_close = enc_close,
102
.d_ioctl = enc_ioctl,
103
.d_name = "ses",
104
.d_flags = D_TRACKCLOSE,
105
};
106
107
static void
108
enc_init(void)
109
{
110
cam_status status;
111
112
/*
113
* Install a global async callback. This callback will
114
* receive async callbacks like "new device found".
115
*/
116
status = xpt_register_async(AC_FOUND_DEVICE, enc_async, NULL, NULL);
117
118
if (status != CAM_REQ_CMP) {
119
printf("enc: Failed to attach master async callback "
120
"due to status 0x%x!\n", status);
121
}
122
}
123
124
static void
125
enc_devgonecb(void *arg)
126
{
127
struct cam_periph *periph;
128
struct enc_softc *enc;
129
struct mtx *mtx;
130
int i;
131
132
periph = (struct cam_periph *)arg;
133
mtx = cam_periph_mtx(periph);
134
mtx_lock(mtx);
135
enc = (struct enc_softc *)periph->softc;
136
137
/*
138
* When we get this callback, we will get no more close calls from
139
* devfs. So if we have any dangling opens, we need to release the
140
* reference held for that particular context.
141
*/
142
for (i = 0; i < enc->open_count; i++)
143
cam_periph_release_locked(periph);
144
145
enc->open_count = 0;
146
147
/*
148
* Release the reference held for the device node, it is gone now.
149
*/
150
cam_periph_release_locked(periph);
151
152
/*
153
* We reference the lock directly here, instead of using
154
* cam_periph_unlock(). The reason is that the final call to
155
* cam_periph_release_locked() above could result in the periph
156
* getting freed. If that is the case, dereferencing the periph
157
* with a cam_periph_unlock() call would cause a page fault.
158
*/
159
mtx_unlock(mtx);
160
}
161
162
static void
163
enc_oninvalidate(struct cam_periph *periph)
164
{
165
struct enc_softc *enc;
166
167
enc = periph->softc;
168
169
enc->enc_flags |= ENC_FLAG_INVALID;
170
171
/* If the sub-driver has an invalidate routine, call it */
172
if (enc->enc_vec.softc_invalidate != NULL)
173
enc->enc_vec.softc_invalidate(enc);
174
175
/*
176
* Unregister any async callbacks.
177
*/
178
xpt_register_async(0, enc_async, periph, periph->path);
179
180
/*
181
* Shutdown our daemon.
182
*/
183
enc->enc_flags |= ENC_FLAG_SHUTDOWN;
184
if (enc->enc_daemon != NULL) {
185
/* Signal the ses daemon to terminate. */
186
wakeup(enc->enc_daemon);
187
}
188
callout_drain(&enc->status_updater);
189
190
destroy_dev_sched_cb(enc->enc_dev, enc_devgonecb, periph);
191
}
192
193
static void
194
enc_dtor(struct cam_periph *periph)
195
{
196
struct enc_softc *enc;
197
198
enc = periph->softc;
199
200
/* If the sub-driver has a cleanup routine, call it */
201
if (enc->enc_vec.softc_cleanup != NULL)
202
enc->enc_vec.softc_cleanup(enc);
203
204
cam_periph_release_boot(periph);
205
206
ENC_FREE(enc);
207
}
208
209
static void
210
enc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
211
{
212
struct cam_periph *periph;
213
214
periph = (struct cam_periph *)callback_arg;
215
216
switch(code) {
217
case AC_FOUND_DEVICE:
218
{
219
struct ccb_getdev *cgd;
220
cam_status status;
221
path_id_t path_id;
222
223
cgd = (struct ccb_getdev *)arg;
224
if (arg == NULL) {
225
break;
226
}
227
228
if (enc_type(cgd) == ENC_NONE) {
229
/*
230
* Schedule announcement of the ENC bindings for
231
* this device if it is managed by a SEP.
232
*/
233
path_id = xpt_path_path_id(path);
234
xpt_lock_buses();
235
TAILQ_FOREACH(periph, &encdriver.units, unit_links) {
236
struct enc_softc *softc;
237
238
softc = (struct enc_softc *)periph->softc;
239
240
/* Check this SEP is ready. */
241
if (softc == NULL || (softc->enc_flags &
242
ENC_FLAG_INITIALIZED) == 0 ||
243
softc->enc_vec.device_found == NULL)
244
continue;
245
246
/* Check this SEP may manage this device. */
247
if (xpt_path_path_id(periph->path) != path_id &&
248
(softc->enc_type != ENC_SEMB_SES ||
249
cgd->protocol != PROTO_ATA))
250
continue;
251
252
softc->enc_vec.device_found(softc);
253
}
254
xpt_unlock_buses();
255
return;
256
}
257
258
status = cam_periph_alloc(enc_ctor, enc_oninvalidate,
259
enc_dtor, NULL, "ses", CAM_PERIPH_BIO,
260
path, enc_async, AC_FOUND_DEVICE, cgd);
261
262
if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
263
printf("enc_async: Unable to probe new device due to "
264
"status 0x%x\n", status);
265
}
266
break;
267
}
268
default:
269
cam_periph_async(periph, code, path, arg);
270
break;
271
}
272
}
273
274
static int
275
enc_open(struct cdev *dev, int flags, int fmt, struct thread *td)
276
{
277
struct cam_periph *periph;
278
struct enc_softc *softc;
279
int error = 0;
280
281
periph = (struct cam_periph *)dev->si_drv1;
282
if (cam_periph_acquire(periph) != 0)
283
return (ENXIO);
284
285
cam_periph_lock(periph);
286
287
softc = (struct enc_softc *)periph->softc;
288
289
if ((softc->enc_flags & ENC_FLAG_INITIALIZED) == 0) {
290
error = ENXIO;
291
goto out;
292
}
293
if (softc->enc_flags & ENC_FLAG_INVALID) {
294
error = ENXIO;
295
goto out;
296
}
297
out:
298
if (error != 0)
299
cam_periph_release_locked(periph);
300
else
301
softc->open_count++;
302
303
cam_periph_unlock(periph);
304
305
return (error);
306
}
307
308
static int
309
enc_close(struct cdev *dev, int flag, int fmt, struct thread *td)
310
{
311
struct cam_periph *periph;
312
struct enc_softc *enc;
313
struct mtx *mtx;
314
315
periph = (struct cam_periph *)dev->si_drv1;
316
mtx = cam_periph_mtx(periph);
317
mtx_lock(mtx);
318
319
enc = periph->softc;
320
enc->open_count--;
321
322
cam_periph_release_locked(periph);
323
324
/*
325
* We reference the lock directly here, instead of using
326
* cam_periph_unlock(). The reason is that the call to
327
* cam_periph_release_locked() above could result in the periph
328
* getting freed. If that is the case, dereferencing the periph
329
* with a cam_periph_unlock() call would cause a page fault.
330
*
331
* cam_periph_release() avoids this problem using the same method,
332
* but we're manually acquiring and dropping the lock here to
333
* protect the open count and avoid another lock acquisition and
334
* release.
335
*/
336
mtx_unlock(mtx);
337
338
return (0);
339
}
340
341
int
342
enc_error(union ccb *ccb, uint32_t cflags, uint32_t sflags)
343
{
344
345
return (cam_periph_error(ccb, cflags, sflags));
346
}
347
348
static int
349
enc_ioctl(struct cdev *dev, u_long cmd, caddr_t arg_addr, int flag,
350
struct thread *td)
351
{
352
struct cam_periph *periph;
353
enc_softc_t *enc;
354
enc_cache_t *cache;
355
void *addr;
356
int error, i;
357
358
#ifdef COMPAT_FREEBSD32
359
if (SV_PROC_FLAG(td->td_proc, SV_ILP32))
360
return (ENOTTY);
361
#endif
362
363
if (arg_addr)
364
addr = *((caddr_t *) arg_addr);
365
else
366
addr = NULL;
367
368
periph = (struct cam_periph *)dev->si_drv1;
369
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering encioctl\n"));
370
371
cam_periph_lock(periph);
372
enc = (struct enc_softc *)periph->softc;
373
cache = &enc->enc_cache;
374
375
/*
376
* Now check to see whether we're initialized or not.
377
* This actually should never fail as we're not supposed
378
* to get past enc_open w/o successfully initializing
379
* things.
380
*/
381
if ((enc->enc_flags & ENC_FLAG_INITIALIZED) == 0) {
382
cam_periph_unlock(periph);
383
return (ENXIO);
384
}
385
cam_periph_unlock(periph);
386
387
error = 0;
388
389
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
390
("trying to do ioctl %#lx\n", cmd));
391
392
/*
393
* If this command can change the device's state,
394
* we must have the device open for writing.
395
*
396
* For commands that get information about the
397
* device- we don't need to lock the peripheral
398
* if we aren't running a command. The periph
399
* also can't go away while a user process has
400
* it open.
401
*/
402
switch (cmd) {
403
case ENCIOC_GETNELM:
404
case ENCIOC_GETELMMAP:
405
case ENCIOC_GETENCSTAT:
406
case ENCIOC_GETELMSTAT:
407
case ENCIOC_GETELMDESC:
408
case ENCIOC_GETELMDEVNAMES:
409
case ENCIOC_GETENCNAME:
410
case ENCIOC_GETENCID:
411
break;
412
default:
413
if ((flag & FWRITE) == 0) {
414
return (EBADF);
415
}
416
}
417
418
/*
419
* XXX The values read here are only valid for the current
420
* configuration generation. We need these ioctls
421
* to also pass in/out a generation number.
422
*/
423
sx_slock(&enc->enc_cache_lock);
424
switch (cmd) {
425
case ENCIOC_GETNELM:
426
error = copyout(&cache->nelms, addr, sizeof (cache->nelms));
427
break;
428
429
case ENCIOC_GETELMMAP: {
430
encioc_element_t *uelm;
431
432
for (uelm = addr, i = 0; i != cache->nelms; i++) {
433
encioc_element_t kelm;
434
kelm.elm_idx = i;
435
kelm.elm_subenc_id = cache->elm_map[i].subenclosure;
436
kelm.elm_type = cache->elm_map[i].elm_type;
437
error = copyout(&kelm, &uelm[i], sizeof(kelm));
438
if (error)
439
break;
440
}
441
break;
442
}
443
case ENCIOC_GETENCSTAT: {
444
error = copyout(&cache->enc_status, addr,
445
sizeof(cache->enc_status));
446
break;
447
}
448
case ENCIOC_SETENCSTAT: {
449
encioc_enc_status_t tmp;
450
451
error = copyin(addr, &tmp, sizeof(tmp));
452
if (error)
453
break;
454
cam_periph_lock(periph);
455
error = enc->enc_vec.set_enc_status(enc, tmp, 1);
456
cam_periph_unlock(periph);
457
break;
458
}
459
case ENCIOC_GETSTRING:
460
case ENCIOC_SETSTRING:
461
case ENCIOC_GETENCNAME:
462
case ENCIOC_GETENCID: {
463
encioc_string_t sstr;
464
465
if (enc->enc_vec.handle_string == NULL) {
466
error = EINVAL;
467
break;
468
}
469
error = copyin(addr, &sstr, sizeof(sstr));
470
if (error)
471
break;
472
cam_periph_lock(periph);
473
error = enc->enc_vec.handle_string(enc, &sstr, cmd);
474
cam_periph_unlock(periph);
475
if (error == 0 || error == ENOMEM)
476
(void)copyout(&sstr.bufsiz,
477
&((encioc_string_t *)addr)->bufsiz,
478
sizeof(sstr.bufsiz));
479
break;
480
}
481
case ENCIOC_GETELMSTAT: {
482
encioc_elm_status_t elms;
483
484
error = copyin(addr, &elms, sizeof(elms));
485
if (error)
486
break;
487
if (elms.elm_idx >= cache->nelms) {
488
error = EINVAL;
489
break;
490
}
491
cam_periph_lock(periph);
492
error = enc->enc_vec.get_elm_status(enc, &elms, 1);
493
cam_periph_unlock(periph);
494
if (error)
495
break;
496
error = copyout(&elms, addr, sizeof(elms));
497
break;
498
}
499
case ENCIOC_GETELMDESC: {
500
encioc_elm_desc_t elmd;
501
502
error = copyin(addr, &elmd, sizeof(elmd));
503
if (error)
504
break;
505
if (elmd.elm_idx >= cache->nelms) {
506
error = EINVAL;
507
break;
508
}
509
if (enc->enc_vec.get_elm_desc != NULL) {
510
error = enc->enc_vec.get_elm_desc(enc, &elmd);
511
if (error)
512
break;
513
} else
514
elmd.elm_desc_len = 0;
515
error = copyout(&elmd, addr, sizeof(elmd));
516
break;
517
}
518
case ENCIOC_GETELMDEVNAMES: {
519
encioc_elm_devnames_t elmdn;
520
521
if (enc->enc_vec.get_elm_devnames == NULL) {
522
error = EINVAL;
523
break;
524
}
525
error = copyin(addr, &elmdn, sizeof(elmdn));
526
if (error)
527
break;
528
if (elmdn.elm_idx >= cache->nelms) {
529
error = EINVAL;
530
break;
531
}
532
cam_periph_lock(periph);
533
error = (*enc->enc_vec.get_elm_devnames)(enc, &elmdn);
534
cam_periph_unlock(periph);
535
if (error)
536
break;
537
error = copyout(&elmdn, addr, sizeof(elmdn));
538
break;
539
}
540
case ENCIOC_SETELMSTAT: {
541
encioc_elm_status_t elms;
542
543
error = copyin(addr, &elms, sizeof(elms));
544
if (error)
545
break;
546
547
if (elms.elm_idx >= cache->nelms) {
548
error = EINVAL;
549
break;
550
}
551
cam_periph_lock(periph);
552
error = enc->enc_vec.set_elm_status(enc, &elms, 1);
553
cam_periph_unlock(periph);
554
555
break;
556
}
557
case ENCIOC_INIT:
558
559
cam_periph_lock(periph);
560
error = enc->enc_vec.init_enc(enc);
561
cam_periph_unlock(periph);
562
break;
563
564
default:
565
cam_periph_lock(periph);
566
error = cam_periph_ioctl(periph, cmd, arg_addr, enc_error);
567
cam_periph_unlock(periph);
568
break;
569
}
570
sx_sunlock(&enc->enc_cache_lock);
571
return (error);
572
}
573
574
int
575
enc_runcmd(struct enc_softc *enc, char *cdb, int cdbl, char *dptr, int *dlenp)
576
{
577
int error, dlen, tdlen;
578
ccb_flags ddf;
579
union ccb *ccb;
580
581
CAM_DEBUG(enc->periph->path, CAM_DEBUG_TRACE,
582
("entering enc_runcmd\n"));
583
if (dptr) {
584
if ((dlen = *dlenp) < 0) {
585
dlen = -dlen;
586
ddf = CAM_DIR_OUT;
587
} else {
588
ddf = CAM_DIR_IN;
589
}
590
} else {
591
dlen = 0;
592
ddf = CAM_DIR_NONE;
593
}
594
595
if (cdbl > IOCDBLEN) {
596
cdbl = IOCDBLEN;
597
}
598
599
ccb = cam_periph_getccb(enc->periph, CAM_PRIORITY_NORMAL);
600
if (enc->enc_type == ENC_SEMB_SES || enc->enc_type == ENC_SEMB_SAFT) {
601
tdlen = min(dlen, 1020);
602
tdlen = (tdlen + 3) & ~3;
603
cam_fill_ataio(&ccb->ataio, 0, NULL, ddf, 0, dptr, tdlen,
604
30 * 1000);
605
if (cdb[0] == RECEIVE_DIAGNOSTIC)
606
ata_28bit_cmd(&ccb->ataio,
607
ATA_SEP_ATTN, cdb[2], 0x02, tdlen / 4);
608
else if (cdb[0] == SEND_DIAGNOSTIC)
609
ata_28bit_cmd(&ccb->ataio,
610
ATA_SEP_ATTN, dlen > 0 ? dptr[0] : 0,
611
0x82, tdlen / 4);
612
else if (cdb[0] == READ_BUFFER)
613
ata_28bit_cmd(&ccb->ataio,
614
ATA_SEP_ATTN, cdb[2], 0x00, tdlen / 4);
615
else
616
ata_28bit_cmd(&ccb->ataio,
617
ATA_SEP_ATTN, dlen > 0 ? dptr[0] : 0,
618
0x80, tdlen / 4);
619
} else {
620
tdlen = dlen;
621
cam_fill_csio(&ccb->csio, 0, NULL, ddf, MSG_SIMPLE_Q_TAG,
622
dptr, dlen, sizeof (struct scsi_sense_data), cdbl,
623
60 * 1000);
624
bcopy(cdb, ccb->csio.cdb_io.cdb_bytes, cdbl);
625
}
626
627
error = cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL);
628
if (error) {
629
if (dptr) {
630
*dlenp = dlen;
631
}
632
} else {
633
if (dptr) {
634
if (ccb->ccb_h.func_code == XPT_ATA_IO)
635
*dlenp = ccb->ataio.resid;
636
else
637
*dlenp = ccb->csio.resid;
638
*dlenp += tdlen - dlen;
639
}
640
}
641
xpt_release_ccb(ccb);
642
CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE,
643
("exiting enc_runcmd: *dlenp = %d\n", *dlenp));
644
return (error);
645
}
646
647
void
648
enc_log(struct enc_softc *enc, const char *fmt, ...)
649
{
650
va_list ap;
651
652
printf("%s%d: ", enc->periph->periph_name, enc->periph->unit_number);
653
va_start(ap, fmt);
654
vprintf(fmt, ap);
655
va_end(ap);
656
}
657
658
/*
659
* The code after this point runs on many platforms,
660
* so forgive the slightly awkward and nonconforming
661
* appearance.
662
*/
663
664
/*
665
* Is this a device that supports enclosure services?
666
*
667
* It's a pretty simple ruleset- if it is device type
668
* 0x0D (13), it's an ENCLOSURE device.
669
*/
670
671
#define SAFTE_START 44
672
#define SAFTE_END 50
673
#define SAFTE_LEN SAFTE_END-SAFTE_START
674
675
static enctyp
676
enc_type(struct ccb_getdev *cgd)
677
{
678
int buflen;
679
unsigned char *iqd;
680
681
if (cgd->protocol == PROTO_SEMB) {
682
iqd = (unsigned char *)&cgd->ident_data;
683
if (STRNCMP(iqd + 43, "S-E-S", 5) == 0)
684
return (ENC_SEMB_SES);
685
else if (STRNCMP(iqd + 43, "SAF-TE", 6) == 0)
686
return (ENC_SEMB_SAFT);
687
return (ENC_NONE);
688
689
} else if (cgd->protocol != PROTO_SCSI)
690
return (ENC_NONE);
691
692
iqd = (unsigned char *)&cgd->inq_data;
693
buflen = min(sizeof(cgd->inq_data),
694
SID_ADDITIONAL_LENGTH(&cgd->inq_data));
695
696
if ((iqd[0] & 0x1f) == T_ENCLOSURE)
697
return (ENC_SES);
698
699
#ifdef SES_ENABLE_PASSTHROUGH
700
if ((iqd[6] & 0x40) && (iqd[2] & 0x7) >= 2) {
701
/*
702
* PassThrough Device.
703
*/
704
return (ENC_SES_PASSTHROUGH);
705
}
706
#endif
707
708
/*
709
* The comparison is short for a reason-
710
* some vendors were chopping it short.
711
*/
712
713
if (buflen < SAFTE_END - 2) {
714
return (ENC_NONE);
715
}
716
717
if (STRNCMP((char *)&iqd[SAFTE_START], "SAF-TE", SAFTE_LEN - 2) == 0) {
718
return (ENC_SAFT);
719
}
720
return (ENC_NONE);
721
}
722
723
/*================== Enclosure Monitoring/Processing Daemon ==================*/
724
/**
725
* \brief Queue an update request for a given action, if needed.
726
*
727
* \param enc SES softc to queue the request for.
728
* \param action Action requested.
729
*/
730
void
731
enc_update_request(enc_softc_t *enc, uint32_t action)
732
{
733
if ((enc->pending_actions & (0x1 << action)) == 0) {
734
enc->pending_actions |= (0x1 << action);
735
ENC_DLOG(enc, "%s: queing requested action %d\n",
736
__func__, action);
737
if (enc->current_action == ENC_UPDATE_NONE)
738
wakeup(enc->enc_daemon);
739
} else {
740
ENC_DLOG(enc, "%s: ignoring requested action %d - "
741
"Already queued\n", __func__, action);
742
}
743
}
744
745
/**
746
* \brief Invoke the handler of the highest priority pending
747
* state in the SES state machine.
748
*
749
* \param enc The SES instance invoking the state machine.
750
*/
751
static void
752
enc_fsm_step(enc_softc_t *enc)
753
{
754
union ccb *ccb;
755
uint8_t *buf;
756
struct enc_fsm_state *cur_state;
757
int error;
758
uint32_t xfer_len;
759
760
ENC_DLOG(enc, "%s enter %p\n", __func__, enc);
761
762
enc->current_action = ffs(enc->pending_actions) - 1;
763
enc->pending_actions &= ~(0x1 << enc->current_action);
764
765
cur_state = &enc->enc_fsm_states[enc->current_action];
766
767
buf = NULL;
768
if (cur_state->buf_size != 0) {
769
cam_periph_unlock(enc->periph);
770
buf = malloc(cur_state->buf_size, M_SCSIENC, M_WAITOK|M_ZERO);
771
cam_periph_lock(enc->periph);
772
}
773
774
error = 0;
775
ccb = NULL;
776
if (cur_state->fill != NULL) {
777
ccb = cam_periph_getccb(enc->periph, CAM_PRIORITY_NORMAL);
778
779
error = cur_state->fill(enc, cur_state, ccb, buf);
780
if (error != 0)
781
goto done;
782
783
error = cam_periph_runccb(ccb, cur_state->error,
784
ENC_CFLAGS,
785
ENC_FLAGS|SF_QUIET_IR, NULL);
786
}
787
788
if (ccb != NULL) {
789
if (ccb->ccb_h.func_code == XPT_ATA_IO)
790
xfer_len = ccb->ataio.dxfer_len - ccb->ataio.resid;
791
else
792
xfer_len = ccb->csio.dxfer_len - ccb->csio.resid;
793
} else
794
xfer_len = 0;
795
796
cam_periph_unlock(enc->periph);
797
cur_state->done(enc, cur_state, ccb, &buf, error, xfer_len);
798
cam_periph_lock(enc->periph);
799
800
done:
801
ENC_DLOG(enc, "%s exit - result %d\n", __func__, error);
802
ENC_FREE_AND_NULL(buf);
803
if (ccb != NULL)
804
xpt_release_ccb(ccb);
805
}
806
807
/**
808
* \invariant Called with cam_periph mutex held.
809
*/
810
static void
811
enc_status_updater(void *arg)
812
{
813
enc_softc_t *enc;
814
815
enc = arg;
816
if (enc->enc_vec.poll_status != NULL)
817
enc->enc_vec.poll_status(enc);
818
}
819
820
static void
821
enc_daemon(void *arg)
822
{
823
enc_softc_t *enc;
824
825
enc = arg;
826
827
cam_periph_lock(enc->periph);
828
while ((enc->enc_flags & ENC_FLAG_SHUTDOWN) == 0) {
829
if (enc->pending_actions == 0) {
830
/*
831
* Reset callout and msleep, or
832
* issue timed task completion
833
* status command.
834
*/
835
enc->current_action = ENC_UPDATE_NONE;
836
837
/*
838
* We've been through our state machine at least
839
* once. Allow the transition to userland.
840
*/
841
cam_periph_release_boot(enc->periph);
842
843
callout_reset_sbt(&enc->status_updater, 60 * SBT_1S, 0,
844
enc_status_updater, enc, C_PREL(1));
845
846
cam_periph_sleep(enc->periph, enc->enc_daemon,
847
PUSER, "idle", 0);
848
} else {
849
enc_fsm_step(enc);
850
}
851
}
852
enc->enc_daemon = NULL;
853
cam_periph_unlock(enc->periph);
854
cam_periph_release(enc->periph);
855
kproc_exit(0);
856
}
857
858
static int
859
enc_kproc_init(enc_softc_t *enc)
860
{
861
int result;
862
863
callout_init_mtx(&enc->status_updater, cam_periph_mtx(enc->periph), 0);
864
865
if (cam_periph_acquire(enc->periph) != 0)
866
return (ENXIO);
867
868
result = kproc_create(enc_daemon, enc, &enc->enc_daemon, /*flags*/0,
869
/*stackpgs*/0, "enc_daemon%d",
870
enc->periph->unit_number);
871
if (result == 0) {
872
/* Do an initial load of all page data. */
873
cam_periph_lock(enc->periph);
874
enc->enc_vec.poll_status(enc);
875
cam_periph_unlock(enc->periph);
876
} else
877
cam_periph_release(enc->periph);
878
return (result);
879
}
880
881
static cam_status
882
enc_ctor(struct cam_periph *periph, void *arg)
883
{
884
cam_status status = CAM_REQ_CMP_ERR;
885
int err;
886
enc_softc_t *enc;
887
struct ccb_getdev *cgd;
888
char *tname;
889
struct make_dev_args args;
890
struct sbuf sb;
891
892
cgd = (struct ccb_getdev *)arg;
893
if (cgd == NULL) {
894
printf("enc_ctor: no getdev CCB, can't register device\n");
895
goto out;
896
}
897
898
enc = ENC_MALLOCZ(sizeof(*enc));
899
if (enc == NULL) {
900
printf("enc_ctor: Unable to probe new device. "
901
"Unable to allocate enc\n");
902
goto out;
903
}
904
enc->periph = periph;
905
enc->current_action = ENC_UPDATE_INVALID;
906
907
enc->enc_type = enc_type(cgd);
908
sx_init(&enc->enc_cache_lock, "enccache");
909
910
switch (enc->enc_type) {
911
case ENC_SES:
912
case ENC_SES_PASSTHROUGH:
913
case ENC_SEMB_SES:
914
err = ses_softc_init(enc);
915
break;
916
case ENC_SAFT:
917
case ENC_SEMB_SAFT:
918
err = safte_softc_init(enc);
919
break;
920
case ENC_NONE:
921
default:
922
ENC_FREE(enc);
923
return (CAM_REQ_CMP_ERR);
924
}
925
926
if (err) {
927
xpt_print(periph->path, "error %d initializing\n", err);
928
goto out;
929
}
930
931
/*
932
* Hold off userland until we have made at least one pass
933
* through our state machine so that physical path data is
934
* present.
935
*/
936
if (enc->enc_vec.poll_status != NULL)
937
cam_periph_hold_boot(periph);
938
939
/*
940
* The softc field is set only once the enc is fully initialized
941
* so that we can rely on this field to detect partially
942
* initialized periph objects in the AC_FOUND_DEVICE handler.
943
*/
944
periph->softc = enc;
945
946
cam_periph_unlock(periph);
947
if (enc->enc_vec.poll_status != NULL) {
948
err = enc_kproc_init(enc);
949
if (err) {
950
xpt_print(periph->path,
951
"error %d starting enc_daemon\n", err);
952
goto out;
953
}
954
}
955
956
/*
957
* Acquire a reference to the periph before we create the devfs
958
* instance for it. We'll release this reference once the devfs
959
* instance has been freed.
960
*/
961
if (cam_periph_acquire(periph) != 0) {
962
xpt_print(periph->path, "%s: lost periph during "
963
"registration!\n", __func__);
964
cam_periph_lock(periph);
965
966
return (CAM_REQ_CMP_ERR);
967
}
968
969
make_dev_args_init(&args);
970
args.mda_devsw = &enc_cdevsw;
971
args.mda_unit = periph->unit_number;
972
args.mda_uid = UID_ROOT;
973
args.mda_gid = GID_OPERATOR;
974
args.mda_mode = 0600;
975
args.mda_si_drv1 = periph;
976
err = make_dev_s(&args, &enc->enc_dev, "%s%d", periph->periph_name,
977
periph->unit_number);
978
cam_periph_lock(periph);
979
if (err != 0) {
980
cam_periph_release_locked(periph);
981
return (CAM_REQ_CMP_ERR);
982
}
983
984
enc->enc_flags |= ENC_FLAG_INITIALIZED;
985
986
/*
987
* Add an async callback so that we get notified if this
988
* device goes away.
989
*/
990
xpt_register_async(AC_LOST_DEVICE, enc_async, periph, periph->path);
991
992
switch (enc->enc_type) {
993
default:
994
case ENC_NONE:
995
tname = "No ENC device";
996
break;
997
case ENC_SES:
998
tname = "SES Device";
999
break;
1000
case ENC_SES_PASSTHROUGH:
1001
tname = "SES Passthrough Device";
1002
break;
1003
case ENC_SAFT:
1004
tname = "SAF-TE Device";
1005
break;
1006
case ENC_SEMB_SES:
1007
tname = "SEMB SES Device";
1008
break;
1009
case ENC_SEMB_SAFT:
1010
tname = "SEMB SAF-TE Device";
1011
break;
1012
}
1013
1014
sbuf_new(&sb, enc->announce_buf, ENC_ANNOUNCE_SZ, SBUF_FIXEDLEN);
1015
xpt_announce_periph_sbuf(periph, &sb, tname);
1016
sbuf_finish(&sb);
1017
sbuf_putbuf(&sb);
1018
1019
status = CAM_REQ_CMP;
1020
1021
out:
1022
if (status != CAM_REQ_CMP)
1023
enc_dtor(periph);
1024
return (status);
1025
}
1026
1027