forked from Mujffar/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcam_xpt.c
4977 lines (4314 loc) · 126 KB
/
cam_xpt.c
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Implementation of the Common Access Method Transport (XPT) layer.
*
* Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
* Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification, immediately at the beginning of the file.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <sys/interrupt.h>
#include <sys/sbuf.h>
#include <sys/taskqueue.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/sysctl.h>
#include <sys/kthread.h>
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_periph.h>
#include <cam/cam_queue.h>
#include <cam/cam_sim.h>
#include <cam/cam_xpt.h>
#include <cam/cam_xpt_sim.h>
#include <cam/cam_xpt_periph.h>
#include <cam/cam_xpt_internal.h>
#include <cam/cam_debug.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>
#include <cam/scsi/scsi_pass.h>
#include <machine/md_var.h> /* geometry translation */
#include <machine/stdarg.h> /* for xpt_print below */
#include "opt_cam.h"
/*
* This is the maximum number of high powered commands (e.g. start unit)
* that can be outstanding at a particular time.
*/
#ifndef CAM_MAX_HIGHPOWER
#define CAM_MAX_HIGHPOWER 4
#endif
/* Datastructures internal to the xpt layer */
MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
/* Object for defering XPT actions to a taskqueue */
struct xpt_task {
struct task task;
void *data1;
uintptr_t data2;
};
typedef enum {
XPT_FLAG_OPEN = 0x01
} xpt_flags;
struct xpt_softc {
xpt_flags flags;
u_int32_t xpt_generation;
/* number of high powered commands that can go through right now */
STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
int num_highpower;
/* queue for handling async rescan requests. */
TAILQ_HEAD(, ccb_hdr) ccb_scanq;
int buses_to_config;
int buses_config_done;
/* Registered busses */
TAILQ_HEAD(,cam_eb) xpt_busses;
u_int bus_generation;
struct intr_config_hook *xpt_config_hook;
int boot_delay;
struct callout boot_callout;
struct mtx xpt_topo_lock;
struct mtx xpt_lock;
};
typedef enum {
DM_RET_COPY = 0x01,
DM_RET_FLAG_MASK = 0x0f,
DM_RET_NONE = 0x00,
DM_RET_STOP = 0x10,
DM_RET_DESCEND = 0x20,
DM_RET_ERROR = 0x30,
DM_RET_ACTION_MASK = 0xf0
} dev_match_ret;
typedef enum {
XPT_DEPTH_BUS,
XPT_DEPTH_TARGET,
XPT_DEPTH_DEVICE,
XPT_DEPTH_PERIPH
} xpt_traverse_depth;
struct xpt_traverse_config {
xpt_traverse_depth depth;
void *tr_func;
void *tr_arg;
};
typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
/* Transport layer configuration information */
static struct xpt_softc xsoftc;
TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
&xsoftc.boot_delay, 0, "Bus registration wait time");
/* Queues for our software interrupt handler */
typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
static cam_simq_t cam_simq;
static struct mtx cam_simq_lock;
/* Pointers to software interrupt handlers */
static void *cambio_ih;
struct cam_periph *xpt_periph;
static periph_init_t xpt_periph_init;
static struct periph_driver xpt_driver =
{
xpt_periph_init, "xpt",
TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
CAM_PERIPH_DRV_EARLY
};
PERIPHDRIVER_DECLARE(xpt, xpt_driver);
static d_open_t xptopen;
static d_close_t xptclose;
static d_ioctl_t xptioctl;
static struct cdevsw xpt_cdevsw = {
.d_version = D_VERSION,
.d_flags = 0,
.d_open = xptopen,
.d_close = xptclose,
.d_ioctl = xptioctl,
.d_name = "xpt",
};
/* Storage for debugging datastructures */
struct cam_path *cam_dpath;
u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
TUNABLE_INT("kern.cam.dflags", &cam_dflags);
SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
&cam_dflags, 0, "Enabled debug flags");
u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
&cam_debug_delay, 0, "Delay in us after each debug message");
/* Our boot-time initialization hook */
static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
static moduledata_t cam_moduledata = {
"cam",
cam_module_event_handler,
NULL
};
static int xpt_init(void *);
DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
MODULE_VERSION(cam, 1);
static void xpt_async_bcast(struct async_list *async_head,
u_int32_t async_code,
struct cam_path *path,
void *async_arg);
static path_id_t xptnextfreepathid(void);
static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
static union ccb *xpt_get_ccb(struct cam_ed *device);
static void xpt_run_dev_allocq(struct cam_ed *device);
static void xpt_run_devq(struct cam_devq *devq);
static timeout_t xpt_release_devq_timeout;
static void xpt_release_simq_timeout(void *arg) __unused;
static void xpt_release_bus(struct cam_eb *bus);
static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
int run_queue);
static struct cam_et*
xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
static void xpt_release_target(struct cam_et *target);
static struct cam_eb*
xpt_find_bus(path_id_t path_id);
static struct cam_et*
xpt_find_target(struct cam_eb *bus, target_id_t target_id);
static struct cam_ed*
xpt_find_device(struct cam_et *target, lun_id_t lun_id);
static void xpt_config(void *arg);
static xpt_devicefunc_t xptpassannouncefunc;
static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
static void xptpoll(struct cam_sim *sim);
static void camisr(void *);
static void camisr_runqueue(void *);
static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
u_int num_patterns, struct cam_eb *bus);
static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
u_int num_patterns,
struct cam_ed *device);
static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
u_int num_patterns,
struct cam_periph *periph);
static xpt_busfunc_t xptedtbusfunc;
static xpt_targetfunc_t xptedttargetfunc;
static xpt_devicefunc_t xptedtdevicefunc;
static xpt_periphfunc_t xptedtperiphfunc;
static xpt_pdrvfunc_t xptplistpdrvfunc;
static xpt_periphfunc_t xptplistperiphfunc;
static int xptedtmatch(struct ccb_dev_match *cdm);
static int xptperiphlistmatch(struct ccb_dev_match *cdm);
static int xptbustraverse(struct cam_eb *start_bus,
xpt_busfunc_t *tr_func, void *arg);
static int xpttargettraverse(struct cam_eb *bus,
struct cam_et *start_target,
xpt_targetfunc_t *tr_func, void *arg);
static int xptdevicetraverse(struct cam_et *target,
struct cam_ed *start_device,
xpt_devicefunc_t *tr_func, void *arg);
static int xptperiphtraverse(struct cam_ed *device,
struct cam_periph *start_periph,
xpt_periphfunc_t *tr_func, void *arg);
static int xptpdrvtraverse(struct periph_driver **start_pdrv,
xpt_pdrvfunc_t *tr_func, void *arg);
static int xptpdperiphtraverse(struct periph_driver **pdrv,
struct cam_periph *start_periph,
xpt_periphfunc_t *tr_func,
void *arg);
static xpt_busfunc_t xptdefbusfunc;
static xpt_targetfunc_t xptdeftargetfunc;
static xpt_devicefunc_t xptdefdevicefunc;
static xpt_periphfunc_t xptdefperiphfunc;
static void xpt_finishconfig_task(void *context, int pending);
static void xpt_dev_async_default(u_int32_t async_code,
struct cam_eb *bus,
struct cam_et *target,
struct cam_ed *device,
void *async_arg);
static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
struct cam_et *target,
lun_id_t lun_id);
static xpt_devicefunc_t xptsetasyncfunc;
static xpt_busfunc_t xptsetasyncbusfunc;
static cam_status xptregister(struct cam_periph *periph,
void *arg);
static __inline int periph_is_queued(struct cam_periph *periph);
static __inline int device_is_queued(struct cam_ed *device);
static __inline int
xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
{
int retval;
if ((dev->ccbq.queue.entries > 0) &&
(dev->ccbq.dev_openings > 0) &&
(dev->ccbq.queue.qfrozen_cnt == 0)) {
/*
* The priority of a device waiting for controller
* resources is that of the highest priority CCB
* enqueued.
*/
retval =
xpt_schedule_dev(&devq->send_queue,
&dev->devq_entry.pinfo,
CAMQ_GET_PRIO(&dev->ccbq.queue));
} else {
retval = 0;
}
return (retval);
}
static __inline int
periph_is_queued(struct cam_periph *periph)
{
return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
}
static __inline int
device_is_queued(struct cam_ed *device)
{
return (device->devq_entry.pinfo.index != CAM_UNQUEUED_INDEX);
}
static void
xpt_periph_init()
{
make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
}
static void
xptdone(struct cam_periph *periph, union ccb *done_ccb)
{
/* Caller will release the CCB */
wakeup(&done_ccb->ccb_h.cbfcnp);
}
static int
xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
{
/*
* Only allow read-write access.
*/
if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
return(EPERM);
/*
* We don't allow nonblocking access.
*/
if ((flags & O_NONBLOCK) != 0) {
printf("%s: can't do nonblocking access\n", devtoname(dev));
return(ENODEV);
}
/* Mark ourselves open */
mtx_lock(&xsoftc.xpt_lock);
xsoftc.flags |= XPT_FLAG_OPEN;
mtx_unlock(&xsoftc.xpt_lock);
return(0);
}
static int
xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
{
/* Mark ourselves closed */
mtx_lock(&xsoftc.xpt_lock);
xsoftc.flags &= ~XPT_FLAG_OPEN;
mtx_unlock(&xsoftc.xpt_lock);
return(0);
}
/*
* Don't automatically grab the xpt softc lock here even though this is going
* through the xpt device. The xpt device is really just a back door for
* accessing other devices and SIMs, so the right thing to do is to grab
* the appropriate SIM lock once the bus/SIM is located.
*/
static int
xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
{
int error;
error = 0;
switch(cmd) {
/*
* For the transport layer CAMIOCOMMAND ioctl, we really only want
* to accept CCB types that don't quite make sense to send through a
* passthrough driver. XPT_PATH_INQ is an exception to this, as stated
* in the CAM spec.
*/
case CAMIOCOMMAND: {
union ccb *ccb;
union ccb *inccb;
struct cam_eb *bus;
inccb = (union ccb *)addr;
bus = xpt_find_bus(inccb->ccb_h.path_id);
if (bus == NULL)
return (EINVAL);
switch (inccb->ccb_h.func_code) {
case XPT_SCAN_BUS:
case XPT_RESET_BUS:
if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
xpt_release_bus(bus);
return (EINVAL);
}
break;
case XPT_SCAN_TGT:
if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
xpt_release_bus(bus);
return (EINVAL);
}
break;
default:
break;
}
switch(inccb->ccb_h.func_code) {
case XPT_SCAN_BUS:
case XPT_RESET_BUS:
case XPT_PATH_INQ:
case XPT_ENG_INQ:
case XPT_SCAN_LUN:
case XPT_SCAN_TGT:
ccb = xpt_alloc_ccb();
CAM_SIM_LOCK(bus->sim);
/*
* Create a path using the bus, target, and lun the
* user passed in.
*/
if (xpt_create_path(&ccb->ccb_h.path, NULL,
inccb->ccb_h.path_id,
inccb->ccb_h.target_id,
inccb->ccb_h.target_lun) !=
CAM_REQ_CMP){
error = EINVAL;
CAM_SIM_UNLOCK(bus->sim);
xpt_free_ccb(ccb);
break;
}
/* Ensure all of our fields are correct */
xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
inccb->ccb_h.pinfo.priority);
xpt_merge_ccb(ccb, inccb);
ccb->ccb_h.cbfcnp = xptdone;
cam_periph_runccb(ccb, NULL, 0, 0, NULL);
bcopy(ccb, inccb, sizeof(union ccb));
xpt_free_path(ccb->ccb_h.path);
xpt_free_ccb(ccb);
CAM_SIM_UNLOCK(bus->sim);
break;
case XPT_DEBUG: {
union ccb ccb;
/*
* This is an immediate CCB, so it's okay to
* allocate it on the stack.
*/
CAM_SIM_LOCK(bus->sim);
/*
* Create a path using the bus, target, and lun the
* user passed in.
*/
if (xpt_create_path(&ccb.ccb_h.path, NULL,
inccb->ccb_h.path_id,
inccb->ccb_h.target_id,
inccb->ccb_h.target_lun) !=
CAM_REQ_CMP){
error = EINVAL;
CAM_SIM_UNLOCK(bus->sim);
break;
}
/* Ensure all of our fields are correct */
xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
inccb->ccb_h.pinfo.priority);
xpt_merge_ccb(&ccb, inccb);
ccb.ccb_h.cbfcnp = xptdone;
xpt_action(&ccb);
bcopy(&ccb, inccb, sizeof(union ccb));
xpt_free_path(ccb.ccb_h.path);
CAM_SIM_UNLOCK(bus->sim);
break;
}
case XPT_DEV_MATCH: {
struct cam_periph_map_info mapinfo;
struct cam_path *old_path;
/*
* We can't deal with physical addresses for this
* type of transaction.
*/
if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
CAM_DATA_VADDR) {
error = EINVAL;
break;
}
/*
* Save this in case the caller had it set to
* something in particular.
*/
old_path = inccb->ccb_h.path;
/*
* We really don't need a path for the matching
* code. The path is needed because of the
* debugging statements in xpt_action(). They
* assume that the CCB has a valid path.
*/
inccb->ccb_h.path = xpt_periph->path;
bzero(&mapinfo, sizeof(mapinfo));
/*
* Map the pattern and match buffers into kernel
* virtual address space.
*/
error = cam_periph_mapmem(inccb, &mapinfo);
if (error) {
inccb->ccb_h.path = old_path;
break;
}
/*
* This is an immediate CCB, we can send it on directly.
*/
CAM_SIM_LOCK(xpt_path_sim(xpt_periph->path));
xpt_action(inccb);
CAM_SIM_UNLOCK(xpt_path_sim(xpt_periph->path));
/*
* Map the buffers back into user space.
*/
cam_periph_unmapmem(inccb, &mapinfo);
inccb->ccb_h.path = old_path;
error = 0;
break;
}
default:
error = ENOTSUP;
break;
}
xpt_release_bus(bus);
break;
}
/*
* This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
* with the periphal driver name and unit name filled in. The other
* fields don't really matter as input. The passthrough driver name
* ("pass"), and unit number are passed back in the ccb. The current
* device generation number, and the index into the device peripheral
* driver list, and the status are also passed back. Note that
* since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
* we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
* (or rather should be) impossible for the device peripheral driver
* list to change since we look at the whole thing in one pass, and
* we do it with lock protection.
*
*/
case CAMGETPASSTHRU: {
union ccb *ccb;
struct cam_periph *periph;
struct periph_driver **p_drv;
char *name;
u_int unit;
u_int cur_generation;
int base_periph_found;
int splbreaknum;
ccb = (union ccb *)addr;
unit = ccb->cgdl.unit_number;
name = ccb->cgdl.periph_name;
/*
* Every 100 devices, we want to drop our lock protection to
* give the software interrupt handler a chance to run.
* Most systems won't run into this check, but this should
* avoid starvation in the software interrupt handler in
* large systems.
*/
splbreaknum = 100;
ccb = (union ccb *)addr;
base_periph_found = 0;
/*
* Sanity check -- make sure we don't get a null peripheral
* driver name.
*/
if (*ccb->cgdl.periph_name == '\0') {
error = EINVAL;
break;
}
/* Keep the list from changing while we traverse it */
xpt_lock_buses();
ptstartover:
cur_generation = xsoftc.xpt_generation;
/* first find our driver in the list of drivers */
for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
if (strcmp((*p_drv)->driver_name, name) == 0)
break;
if (*p_drv == NULL) {
xpt_unlock_buses();
ccb->ccb_h.status = CAM_REQ_CMP_ERR;
ccb->cgdl.status = CAM_GDEVLIST_ERROR;
*ccb->cgdl.periph_name = '\0';
ccb->cgdl.unit_number = 0;
error = ENOENT;
break;
}
/*
* Run through every peripheral instance of this driver
* and check to see whether it matches the unit passed
* in by the user. If it does, get out of the loops and
* find the passthrough driver associated with that
* peripheral driver.
*/
for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
periph = TAILQ_NEXT(periph, unit_links)) {
if (periph->unit_number == unit) {
break;
} else if (--splbreaknum == 0) {
xpt_unlock_buses();
xpt_lock_buses();
splbreaknum = 100;
if (cur_generation != xsoftc.xpt_generation)
goto ptstartover;
}
}
/*
* If we found the peripheral driver that the user passed
* in, go through all of the peripheral drivers for that
* particular device and look for a passthrough driver.
*/
if (periph != NULL) {
struct cam_ed *device;
int i;
base_periph_found = 1;
device = periph->path->device;
for (i = 0, periph = SLIST_FIRST(&device->periphs);
periph != NULL;
periph = SLIST_NEXT(periph, periph_links), i++) {
/*
* Check to see whether we have a
* passthrough device or not.
*/
if (strcmp(periph->periph_name, "pass") == 0) {
/*
* Fill in the getdevlist fields.
*/
strcpy(ccb->cgdl.periph_name,
periph->periph_name);
ccb->cgdl.unit_number =
periph->unit_number;
if (SLIST_NEXT(periph, periph_links))
ccb->cgdl.status =
CAM_GDEVLIST_MORE_DEVS;
else
ccb->cgdl.status =
CAM_GDEVLIST_LAST_DEVICE;
ccb->cgdl.generation =
device->generation;
ccb->cgdl.index = i;
/*
* Fill in some CCB header fields
* that the user may want.
*/
ccb->ccb_h.path_id =
periph->path->bus->path_id;
ccb->ccb_h.target_id =
periph->path->target->target_id;
ccb->ccb_h.target_lun =
periph->path->device->lun_id;
ccb->ccb_h.status = CAM_REQ_CMP;
break;
}
}
}
/*
* If the periph is null here, one of two things has
* happened. The first possibility is that we couldn't
* find the unit number of the particular peripheral driver
* that the user is asking about. e.g. the user asks for
* the passthrough driver for "da11". We find the list of
* "da" peripherals all right, but there is no unit 11.
* The other possibility is that we went through the list
* of peripheral drivers attached to the device structure,
* but didn't find one with the name "pass". Either way,
* we return ENOENT, since we couldn't find something.
*/
if (periph == NULL) {
ccb->ccb_h.status = CAM_REQ_CMP_ERR;
ccb->cgdl.status = CAM_GDEVLIST_ERROR;
*ccb->cgdl.periph_name = '\0';
ccb->cgdl.unit_number = 0;
error = ENOENT;
/*
* It is unfortunate that this is even necessary,
* but there are many, many clueless users out there.
* If this is true, the user is looking for the
* passthrough driver, but doesn't have one in his
* kernel.
*/
if (base_periph_found == 1) {
printf("xptioctl: pass driver is not in the "
"kernel\n");
printf("xptioctl: put \"device pass\" in "
"your kernel config file\n");
}
}
xpt_unlock_buses();
break;
}
default:
error = ENOTTY;
break;
}
return(error);
}
static int
cam_module_event_handler(module_t mod, int what, void *arg)
{
int error;
switch (what) {
case MOD_LOAD:
if ((error = xpt_init(NULL)) != 0)
return (error);
break;
case MOD_UNLOAD:
return EBUSY;
default:
return EOPNOTSUPP;
}
return 0;
}
static void
xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
{
if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
xpt_free_path(done_ccb->ccb_h.path);
xpt_free_ccb(done_ccb);
} else {
done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
}
xpt_release_boot();
}
/* thread to handle bus rescans */
static void
xpt_scanner_thread(void *dummy)
{
union ccb *ccb;
struct cam_sim *sim;
xpt_lock_buses();
for (;;) {
if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
"ccb_scanq", 0);
if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
xpt_unlock_buses();
sim = ccb->ccb_h.path->bus->sim;
CAM_SIM_LOCK(sim);
xpt_action(ccb);
CAM_SIM_UNLOCK(sim);
xpt_lock_buses();
}
}
}
void
xpt_rescan(union ccb *ccb)
{
struct ccb_hdr *hdr;
/* Prepare request */
if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
ccb->ccb_h.func_code = XPT_SCAN_BUS;
else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
ccb->ccb_h.func_code = XPT_SCAN_TGT;
else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
ccb->ccb_h.func_code = XPT_SCAN_LUN;
else {
xpt_print(ccb->ccb_h.path, "illegal scan path\n");
xpt_free_path(ccb->ccb_h.path);
xpt_free_ccb(ccb);
return;
}
ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
ccb->ccb_h.cbfcnp = xpt_rescan_done;
xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
/* Don't make duplicate entries for the same paths. */
xpt_lock_buses();
if (ccb->ccb_h.ppriv_ptr1 == NULL) {
TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
wakeup(&xsoftc.ccb_scanq);
xpt_unlock_buses();
xpt_print(ccb->ccb_h.path, "rescan already queued\n");
xpt_free_path(ccb->ccb_h.path);
xpt_free_ccb(ccb);
return;
}
}
}
TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
xsoftc.buses_to_config++;
wakeup(&xsoftc.ccb_scanq);
xpt_unlock_buses();
}
/* Functions accessed by the peripheral drivers */
static int
xpt_init(void *dummy)
{
struct cam_sim *xpt_sim;
struct cam_path *path;
struct cam_devq *devq;
cam_status status;
TAILQ_INIT(&xsoftc.xpt_busses);
TAILQ_INIT(&cam_simq);
TAILQ_INIT(&xsoftc.ccb_scanq);
STAILQ_INIT(&xsoftc.highpowerq);
xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
#ifdef CAM_BOOT_DELAY
/*
* Override this value at compile time to assist our users
* who don't use loader to boot a kernel.
*/
xsoftc.boot_delay = CAM_BOOT_DELAY;
#endif
/*
* The xpt layer is, itself, the equivelent of a SIM.
* Allow 16 ccbs in the ccb pool for it. This should
* give decent parallelism when we probe busses and
* perform other XPT functions.
*/
devq = cam_simq_alloc(16);
xpt_sim = cam_sim_alloc(xptaction,
xptpoll,
"xpt",
/*softc*/NULL,
/*unit*/0,
/*mtx*/&xsoftc.xpt_lock,
/*max_dev_transactions*/0,
/*max_tagged_dev_transactions*/0,
devq);
if (xpt_sim == NULL)
return (ENOMEM);
mtx_lock(&xsoftc.xpt_lock);
if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
mtx_unlock(&xsoftc.xpt_lock);
printf("xpt_init: xpt_bus_register failed with status %#x,"
" failing attach\n", status);
return (EINVAL);
}
/*
* Looking at the XPT from the SIM layer, the XPT is
* the equivelent of a peripheral driver. Allocate
* a peripheral driver entry for us.
*/
if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
CAM_TARGET_WILDCARD,
CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
mtx_unlock(&xsoftc.xpt_lock);
printf("xpt_init: xpt_create_path failed with status %#x,"
" failing attach\n", status);
return (EINVAL);
}
cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
path, NULL, 0, xpt_sim);
xpt_free_path(path);
mtx_unlock(&xsoftc.xpt_lock);
/* Install our software interrupt handlers */
swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
/*
* Register a callback for when interrupts are enabled.
*/
xsoftc.xpt_config_hook =
(struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
M_CAMXPT, M_NOWAIT | M_ZERO);
if (xsoftc.xpt_config_hook == NULL) {
printf("xpt_init: Cannot malloc config hook "
"- failing attach\n");
return (ENOMEM);
}
xsoftc.xpt_config_hook->ich_func = xpt_config;
if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
free (xsoftc.xpt_config_hook, M_CAMXPT);
printf("xpt_init: config_intrhook_establish failed "
"- failing attach\n");
}
return (0);
}
static cam_status
xptregister(struct cam_periph *periph, void *arg)
{
struct cam_sim *xpt_sim;
if (periph == NULL) {
printf("xptregister: periph was NULL!!\n");
return(CAM_REQ_CMP_ERR);
}
xpt_sim = (struct cam_sim *)arg;
xpt_sim->softc = periph;
xpt_periph = periph;
periph->softc = NULL;
return(CAM_REQ_CMP);
}
int32_t
xpt_add_periph(struct cam_periph *periph)
{
struct cam_ed *device;
int32_t status;
struct periph_list *periph_head;
mtx_assert(periph->sim->mtx, MA_OWNED);
device = periph->path->device;
periph_head = &device->periphs;
status = CAM_REQ_CMP;
if (device != NULL) {
/*
* Make room for this peripheral
* so it will fit in the queue
* when it's scheduled to run
*/
status = camq_resize(&device->drvq,
device->drvq.array_size + 1);
device->generation++;
SLIST_INSERT_HEAD(periph_head, periph, periph_links);