1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* SMP - Serial Management Protocol Device Driver
*
* The SMP driver provides user programs access to SAS Serial Management
* Protocol devices by providing ioctl interface.
*/
#include <sys/modctl.h>
#include <sys/file.h>
#include <sys/scsi/scsi.h>
#include <sys/scsi/targets/smp.h>
#include <sys/sdt.h>
/*
* Standard entrypoints
*/
static int smp_attach(dev_info_t *, ddi_attach_cmd_t);
static int smp_detach(dev_info_t *, ddi_detach_cmd_t);
static int smp_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int smp_probe(dev_info_t *);
static int smp_open(dev_t *, int, int, cred_t *);
static int smp_close(dev_t, int, int, cred_t *);
static int smp_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
/*
* Configuration routines
*/
static int smp_do_attach(dev_info_t *);
static int smp_do_detach(dev_info_t *);
/*
* Command handle routing
*/
static int smp_handle_func(dev_t, intptr_t, int, cred_t *, int *);
/*
* Logging/debugging routines
*/
static void smp_log(smp_state_t *, int, const char *, ...);
int smp_retry_times = SMP_DEFAULT_RETRY_TIMES;
static struct cb_ops smp_cb_ops = {
smp_open, /* open */
smp_close, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
nodev, /* read */
nodev, /* write */
smp_ioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
nochpoll, /* poll */
ddi_prop_op, /* cb_prop_op */
0, /* streamtab */
D_MP | D_NEW | D_HOTPLUG /* Driver compatibility flag */
};
static struct dev_ops smp_dev_ops = {
DEVO_REV, /* devo_rev, */
0, /* refcnt */
smp_getinfo, /* info */
nulldev, /* identify */
smp_probe, /* probe */
smp_attach, /* attach */
smp_detach, /* detach */
nodev, /* reset */
&smp_cb_ops, /* driver operations */
(struct bus_ops *)0, /* bus operations */
NULL, /* power */
ddi_quiesce_not_needed, /* quiesce */
};
static void *smp_soft_state = NULL;
static struct modldrv modldrv = {
&mod_driverops, "smp device driver", &smp_dev_ops
};
static struct modlinkage modlinkage = {
MODREV_1, &modldrv, NULL
};
int
_init(void)
{
int err;
if ((err = ddi_soft_state_init(&smp_soft_state,
sizeof (smp_state_t), SMP_ESTIMATED_NUM_DEVS)) != 0) {
return (err);
}
if ((err = mod_install(&modlinkage)) != 0) {
ddi_soft_state_fini(&smp_soft_state);
}
return (err);
}
int
_fini(void)
{
int err;
if ((err = mod_remove(&modlinkage)) == 0) {
ddi_soft_state_fini(&smp_soft_state);
}
return (err);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
/*
* smp_attach()
* attach(9e) entrypoint.
*/
static int
smp_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int err;
switch (cmd) {
case DDI_ATTACH:
err = smp_do_attach(dip);
break;
case DDI_RESUME:
err = DDI_SUCCESS;
break;
default:
err = DDI_FAILURE;
break;
}
if (err != DDI_SUCCESS) {
smp_log(NULL, CE_NOTE, "!smp_attach(), "
"device unit-address @%s failed",
ddi_get_name_addr(dip));
}
return (err);
}
/*
* smp_do_attach()
* handle the nitty details of attach.
*/
static int
smp_do_attach(dev_info_t *dip)
{
int instance;
struct smp_device *smp_devp;
smp_state_t *smp_state;
instance = ddi_get_instance(dip);
smp_devp = ddi_get_driver_private(dip);
ASSERT(smp_devp != NULL);
DTRACE_PROBE2(smp__attach__detach, int, instance, char *,
ddi_get_name_addr(dip));
if (ddi_soft_state_zalloc(smp_soft_state, instance) != DDI_SUCCESS) {
smp_log(NULL, CE_NOTE,
"!smp_do_attach: failed to allocate softstate, "
"device unit-address @%s", ddi_get_name_addr(dip));
return (DDI_FAILURE);
}
smp_state = ddi_get_soft_state(smp_soft_state, instance);
smp_state->smp_dev = smp_devp;
/*
* For simplicity, the minor number == the instance number
*/
if (ddi_create_minor_node(dip, "smp", S_IFCHR,
instance, DDI_NT_SMP, NULL) == DDI_FAILURE) {
smp_log(smp_state, CE_NOTE,
"!smp_do_attach: minor node creation failed, "
"device unit-address @%s", ddi_get_name_addr(dip));
ddi_soft_state_free(smp_soft_state, instance);
return (DDI_FAILURE);
}
mutex_init(&smp_state->smp_mutex, NULL, MUTEX_DRIVER, NULL);
smp_state->smp_open_flag = SMP_CLOSED;
ddi_report_dev(dip);
return (DDI_SUCCESS);
}
/*
* smp_detach()
* detach(9E) entrypoint
*/
static int
smp_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
int instance;
smp_state_t *smp_state;
instance = ddi_get_instance(dip);
smp_state = ddi_get_soft_state(smp_soft_state, instance);
if (smp_state == NULL) {
smp_log(NULL, CE_NOTE,
"!smp_detach: failed, no softstate found (%d), "
"device unit-address @%s",
instance, ddi_get_name_addr(dip));
return (DDI_FAILURE);
}
switch (cmd) {
case DDI_DETACH:
return (smp_do_detach(dip));
case DDI_SUSPEND:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/*
* smp_do_detach()
* detach the driver, tearing down resources.
*/
static int
smp_do_detach(dev_info_t *dip)
{
int instance;
smp_state_t *smp_state;
instance = ddi_get_instance(dip);
smp_state = ddi_get_soft_state(smp_soft_state, instance);
DTRACE_PROBE2(smp__attach__detach, int, instance, char *,
ddi_get_name_addr(dip));
mutex_destroy(&smp_state->smp_mutex);
ddi_soft_state_free(smp_soft_state, instance);
ddi_remove_minor_node(dip, NULL);
return (DDI_SUCCESS);
}
static int
smp_probe(dev_info_t *dip)
{
struct smp_device *smpdevp;
smpdevp = ddi_get_driver_private(dip);
return (sas_hba_probe_smp(smpdevp));
}
/*
* smp_getinfo()
* getinfo(9e) entrypoint.
*/
/*ARGSUSED*/
static int
smp_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
dev_t dev;
smp_state_t *smp_state;
int instance, error;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
dev = (dev_t)arg;
instance = getminor(dev);
if ((smp_state = ddi_get_soft_state(smp_soft_state, instance))
== NULL)
return (DDI_FAILURE);
*result = (void *) smp_state->smp_dev->dip;
error = DDI_SUCCESS;
break;
case DDI_INFO_DEVT2INSTANCE:
dev = (dev_t)arg;
instance = getminor(dev);
*result = (void *)(uintptr_t)instance;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
}
return (error);
}
/*ARGSUSED*/
static int
smp_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
{
smp_state_t *smp_state;
int instance;
int rv = 0;
instance = getminor(*dev_p);
if ((smp_state = ddi_get_soft_state(smp_soft_state, instance))
== NULL) {
return (ENXIO);
}
mutex_enter(&smp_state->smp_mutex);
if (flag & FEXCL) {
if (smp_state->smp_open_flag != SMP_CLOSED) {
rv = EBUSY;
} else {
smp_state->smp_open_flag = SMP_EXOPENED;
}
} else {
if (smp_state->smp_open_flag == SMP_EXOPENED) {
rv = EBUSY;
} else {
smp_state->smp_open_flag = SMP_SOPENED;
}
}
mutex_exit(&smp_state->smp_mutex);
return (rv);
}
/*ARGSUSED*/
static int
smp_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
{
smp_state_t *smp_state;
int instance;
int rv = 0;
instance = getminor(dev);
if ((smp_state = ddi_get_soft_state(smp_soft_state, instance))
== NULL) {
return (ENXIO);
}
mutex_enter(&smp_state->smp_mutex);
if (smp_state->smp_open_flag == SMP_CLOSED) {
smp_log(smp_state, CE_NOTE, "!smp device is already in close");
} else {
smp_state->smp_open_flag = SMP_CLOSED;
}
mutex_exit(&smp_state->smp_mutex);
return (rv);
}
/*ARGSUSED*/
static int
smp_handle_func(dev_t dev,
intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
{
usmp_cmd_t usmp_cmd_data, *usmp_cmd = &usmp_cmd_data;
smp_pkt_t smp_pkt_data, *smp_pkt = &smp_pkt_data;
smp_state_t *smp_state;
int instance, retrycount;
cred_t *cr;
uint64_t cmd_flags = 0;
int rval = 0;
#ifdef _MULTI_DATAMODEL
usmp_cmd32_t usmp_cmd32_data, *usmp_cmd32 = &usmp_cmd32_data;
#endif
/* require PRIV_SYS_DEVICES privilege */
cr = ddi_get_cred();
if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
return (EPERM);
}
bzero(smp_pkt, sizeof (smp_pkt_t));
instance = getminor(dev);
if ((smp_state = ddi_get_soft_state(smp_soft_state, instance))
== NULL) {
return (ENXIO);
}
#ifdef _MULTI_DATAMODEL
switch (ddi_model_convert_from(flag & FMODELS)) {
case DDI_MODEL_ILP32:
if (ddi_copyin((void *)arg, usmp_cmd32, sizeof (usmp_cmd32_t),
flag)) {
return (EFAULT);
}
usmp_cmd32tousmp_cmd(usmp_cmd32, usmp_cmd);
break;
case DDI_MODEL_NONE:
if (ddi_copyin((void *)arg, usmp_cmd, sizeof (usmp_cmd_t),
flag)) {
return (EFAULT);
}
break;
}
#else /* ! _MULTI_DATAMODEL */
if (ddi_copyin((void *)arg, usmp_cmd, sizeof (usmp_cmd_t), flag)) {
return (EFAULT);
}
#endif /* _MULTI_DATAMODEL */
if ((usmp_cmd->usmp_reqsize < SMP_MIN_REQUEST_SIZE) ||
(usmp_cmd->usmp_reqsize > SMP_MAX_REQUEST_SIZE) ||
(usmp_cmd->usmp_rspsize < SMP_MIN_RESPONSE_SIZE) ||
(usmp_cmd->usmp_rspsize > SMP_MAX_RESPONSE_SIZE)) {
rval = EINVAL;
goto done;
}
smp_pkt->pkt_reqsize = usmp_cmd->usmp_reqsize;
smp_pkt->pkt_rspsize = usmp_cmd->usmp_rspsize;
/* allocate memory space for smp request and response frame in kernel */
smp_pkt->pkt_req = kmem_zalloc((size_t)usmp_cmd->usmp_reqsize,
KM_SLEEP);
cmd_flags |= SMP_FLAG_REQBUF;
smp_pkt->pkt_rsp = kmem_zalloc((size_t)usmp_cmd->usmp_rspsize,
KM_SLEEP);
cmd_flags |= SMP_FLAG_RSPBUF;
/* copy smp request frame to kernel space */
if (ddi_copyin(usmp_cmd->usmp_req, smp_pkt->pkt_req,
(size_t)usmp_cmd->usmp_reqsize, flag) != 0) {
rval = EFAULT;
goto done;
}
DTRACE_PROBE1(smp__transport__start, caddr_t, smp_pkt->pkt_req);
smp_pkt->pkt_address = &smp_state->smp_dev->smp_addr;
smp_pkt->pkt_reason = 0;
if (usmp_cmd->usmp_timeout <= 0) {
smp_pkt->pkt_timeout = SMP_DEFAULT_TIMEOUT;
} else {
smp_pkt->pkt_timeout = usmp_cmd->usmp_timeout;
}
/* call sas_smp_transport entry and send smp_pkt to HBA driver */
cmd_flags |= SMP_FLAG_XFER;
for (retrycount = 0; retrycount <= smp_retry_times; retrycount++) {
if (sas_smp_transport(smp_pkt) == DDI_SUCCESS) {
rval = DDI_SUCCESS;
break;
}
switch (smp_pkt->pkt_reason) {
case EAGAIN:
if (retrycount < smp_retry_times) {
smp_pkt->pkt_reason = 0;
bzero(smp_pkt->pkt_rsp,
(size_t)usmp_cmd->usmp_rspsize);
delay(drv_usectohz(10000)); /* 10 ms */
continue;
} else {
smp_log(smp_state, CE_NOTE,
"!sas_smp_transport failed, pkt_reason %d",
smp_pkt->pkt_reason);
rval = smp_pkt->pkt_reason;
goto copyout;
}
default:
smp_log(smp_state, CE_NOTE,
"!sas_smp_transport failed, pkt_reason %d",
smp_pkt->pkt_reason);
rval = smp_pkt->pkt_reason;
goto copyout;
}
}
copyout:
/* copy out smp response to user process */
if (ddi_copyout(smp_pkt->pkt_rsp, usmp_cmd->usmp_rsp,
(size_t)usmp_cmd->usmp_rspsize, flag) != 0) {
rval = EFAULT;
}
done:
if ((cmd_flags & SMP_FLAG_XFER) != 0) {
DTRACE_PROBE2(smp__transport__done, caddr_t, smp_pkt->pkt_rsp,
uchar_t, smp_pkt->pkt_reason);
}
if ((cmd_flags & SMP_FLAG_REQBUF) != 0) {
kmem_free(smp_pkt->pkt_req, smp_pkt->pkt_reqsize);
}
if ((cmd_flags & SMP_FLAG_RSPBUF) != 0) {
kmem_free(smp_pkt->pkt_rsp, smp_pkt->pkt_rspsize);
}
return (rval);
}
/*ARGSUSED*/
static int
smp_ioctl(dev_t dev,
int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
{
int rval = 0;
switch (cmd) {
case USMPFUNC:
/*
* The response payload is valid only if return value is 0
* or EOVERFLOW.
*/
rval = smp_handle_func(dev, arg, flag, cred_p, rval_p);
break;
default:
rval = EINVAL;
}
return (rval);
}
static void
smp_log(smp_state_t *smp_state, int level, const char *fmt, ...)
{
va_list ap;
char buf[256];
dev_info_t *dip;
if (smp_state == (smp_state_t *)NULL) {
dip = NULL;
} else {
dip = smp_state->smp_dev->dip;
}
va_start(ap, fmt);
(void) vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
scsi_log(dip, "smp", level, "%s", buf);
}
|