forked from priyaraut55/scst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscst_local.c
2190 lines (1776 loc) · 52.6 KB
/
scst_local.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
/*
* Copyright (C) 2008 - 2010 Richard Sharpe
* Copyright (C) 1992 Eric Youngdale
* Copyright (C) 2008 - 2016 Vladislav Bolkhovitin <[email protected]>
*
* Simulate a host adapter and an SCST target adapter back to back
*
* Based on the scsi_debug.c driver originally by Eric Youngdale and
* others, including D Gilbert et al
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/init.h>
#ifdef CONFIG_SCST_PROC
#include <linux/proc_fs.h>
#endif
#include <linux/moduleparam.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/completion.h>
#include <linux/spinlock.h>
#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_tcq.h>
#define LOG_PREFIX "scst_local"
/* SCST includes ... */
#ifdef INSIDE_KERNEL_TREE
#include <scst/scst.h>
#include <scst/scst_debug.h>
#else
#include <scst.h>
#include <scst_debug.h>
#endif
#ifndef RHEL_RELEASE_VERSION
#define RHEL_RELEASE_VERSION(maj, min) 0
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
#define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
#endif
#ifndef INSIDE_KERNEL_TREE
#if defined(CONFIG_HIGHMEM4G) || defined(CONFIG_HIGHMEM64G)
#warning HIGHMEM kernel configurations are not supported by this module, \
because nowadays it is not worth the effort. Consider changing \
VMSPLIT option or use a 64-bit configuration instead. See SCST core \
README file for details.
#endif
#endif
#ifdef CONFIG_SCST_DEBUG
#define SCST_LOCAL_DEFAULT_LOG_FLAGS (TRACE_FUNCTION | TRACE_PID | \
TRACE_LINE | TRACE_OUT_OF_MEM | TRACE_MGMT | TRACE_MGMT_DEBUG | \
TRACE_MINOR | TRACE_SPECIAL)
#else
# ifdef CONFIG_SCST_TRACING
#define SCST_LOCAL_DEFAULT_LOG_FLAGS (TRACE_OUT_OF_MEM | TRACE_MGMT | TRACE_PID | \
TRACE_SPECIAL)
# endif
#endif
#if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
#define trace_flag scst_local_trace_flag
static unsigned long scst_local_trace_flag = SCST_LOCAL_DEFAULT_LOG_FLAGS;
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
/*
* Provide some local definitions that are not provided for some earlier
* kernels so we operate over a wider range of kernels
*
* Some time before 2.6.24 scsi_sg_count, scsi_sglist and scsi_bufflen were
* not available. Make it available for 2.6.18 which is used still on some
* distros, like CentOS etc.
*/
#define scsi_sg_count(cmd) ((cmd)->use_sg)
#define scsi_sglist(cmd) ((struct scatterlist *)(cmd)->request_buffer)
#define scsi_bufflen(cmd) ((cmd)->request_bufflen)
#endif
#define SCST_LOCAL_VERSION "3.3"
static const char *scst_local_version_date = "20110901";
/* Some statistics */
static atomic_t num_aborts = ATOMIC_INIT(0);
static atomic_t num_dev_resets = ATOMIC_INIT(0);
static atomic_t num_target_resets = ATOMIC_INIT(0);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31) \
|| defined(RHEL_MAJOR) && RHEL_MAJOR -0 <= 5
static int scst_local_add_default_tgt = true;
#else
static bool scst_local_add_default_tgt = true;
#endif
module_param_named(add_default_tgt, scst_local_add_default_tgt, bool, S_IRUGO);
MODULE_PARM_DESC(add_default_tgt, "add (default) or not on start default "
"target scst_local_tgt with default session scst_local_host");
static struct workqueue_struct *aen_workqueue;
struct scst_aen_work_item {
struct list_head work_list_entry;
struct scst_aen *aen;
};
struct scst_local_tgt {
struct scst_tgt *scst_tgt;
struct list_head sessions_list; /* protected by scst_local_mutex */
struct list_head tgts_list_entry;
/* SCSI version descriptors */
uint16_t scsi_transport_version;
uint16_t phys_transport_version;
};
struct scst_local_sess {
struct scst_session *scst_sess;
unsigned int unregistering:1;
struct device dev;
struct Scsi_Host *shost;
struct scst_local_tgt *tgt;
int number;
struct mutex tr_id_mutex;
uint8_t *transport_id;
int transport_id_len;
struct work_struct aen_work;
spinlock_t aen_lock;
struct list_head aen_work_list; /* protected by aen_lock */
struct work_struct remove_work;
struct list_head sessions_list_entry;
};
#define to_scst_lcl_sess(d) \
container_of(d, struct scst_local_sess, dev)
static int __scst_local_add_adapter(struct scst_local_tgt *tgt,
const char *initiator_name, bool locked);
static int scst_local_add_adapter(struct scst_local_tgt *tgt,
const char *initiator_name);
static void scst_local_close_session_impl(struct scst_local_sess *sess,
bool async);
static void scst_local_remove_adapter(struct scst_local_sess *sess);
static int scst_local_add_target(const char *target_name,
struct scst_local_tgt **out_tgt);
static void __scst_local_remove_target(struct scst_local_tgt *tgt);
static void scst_local_remove_target(struct scst_local_tgt *tgt);
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
/*
* Maintains data that is needed during command processing ...
* We have a single element scatterlist in here in case the scst_cmnd
* we are given has a buffer, not a scatterlist, but we only need this for
* kernels less than 2.6.25.
*/
struct scst_local_tgt_specific {
struct scsi_cmnd *cmnd;
void (*done)(struct scsi_cmnd *);
struct scatterlist sgl;
};
/*
* We use a pool of objects maintaind by the kernel so that it is less
* likely to have to allocate them when we are in the data path.
*
* Note, we only need this for kernels in which we are likely to get non
* scatterlist requests.
*/
static struct kmem_cache *tgt_specific_pool;
#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)) */
static atomic_t scst_local_sess_num = ATOMIC_INIT(0);
static LIST_HEAD(scst_local_tgts_list);
static DEFINE_MUTEX(scst_local_mutex);
static DECLARE_RWSEM(scst_local_exit_rwsem);
MODULE_AUTHOR("Richard Sharpe, Vladislav Bolkhovitin + ideas from SCSI_DEBUG");
MODULE_DESCRIPTION("SCSI+SCST local adapter driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(SCST_LOCAL_VERSION);
static int scst_local_get_sas_transport_id(struct scst_local_sess *sess,
uint8_t **transport_id, int *len)
{
int res = 0;
int tr_id_size = 0;
uint8_t *tr_id = NULL;
TRACE_ENTRY();
tr_id_size = 24; /* A SAS TransportID */
tr_id = kzalloc(tr_id_size, GFP_KERNEL);
if (tr_id == NULL) {
PRINT_ERROR("Allocation of TransportID (size %d) failed",
tr_id_size);
res = -ENOMEM;
goto out;
}
tr_id[0] = 0x00 | SCSI_TRANSPORTID_PROTOCOLID_SAS;
/*
* Assemble a valid SAS address = 0x5OOUUIIR12345678 ... Does SCST
* have one?
*/
tr_id[4] = 0x5F;
tr_id[5] = 0xEE;
tr_id[6] = 0xDE;
tr_id[7] = 0x40 | ((sess->number >> 4) & 0x0F);
tr_id[8] = 0x0F | ((sess->number & 0x0F) << 4);
tr_id[9] = 0xAD;
tr_id[10] = 0xE0;
tr_id[11] = 0x50;
*transport_id = tr_id;
if (len)
*len = tr_id_size;
TRACE_DBG("Created tid '%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X'",
tr_id[4], tr_id[5], tr_id[6], tr_id[7],
tr_id[8], tr_id[9], tr_id[10], tr_id[11]);
out:
TRACE_EXIT_RES(res);
return res;
}
static int scst_local_get_initiator_port_transport_id(
struct scst_tgt *tgt, struct scst_session *scst_sess,
uint8_t **transport_id)
{
int res = 0;
struct scst_local_sess *sess;
TRACE_ENTRY();
if (scst_sess == NULL) {
res = SCSI_TRANSPORTID_PROTOCOLID_SAS;
goto out;
}
sess = scst_sess_get_tgt_priv(scst_sess);
mutex_lock(&sess->tr_id_mutex);
if (sess->transport_id == NULL)
res = scst_local_get_sas_transport_id(sess, transport_id, NULL);
else {
*transport_id = kmemdup(sess->transport_id,
sess->transport_id_len,
GFP_KERNEL);
if (*transport_id == NULL) {
PRINT_ERROR("Allocation of TransportID (size %d) failed",
sess->transport_id_len);
res = -ENOMEM;
}
}
mutex_unlock(&sess->tr_id_mutex);
out:
TRACE_EXIT_RES(res);
return res;
}
#ifdef CONFIG_SCST_PROC
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
static int scst_local_proc_info(struct Scsi_Host *host, char *buffer,
char **start, off_t offset, int length,
int inout)
{
int len, pos, begin;
TRACE_ENTRY();
if (inout == 1)
return -EACCES;
begin = 0;
pos = len = sprintf(buffer, "scst_local adapter driver, version "
"%s [%s]\nAborts=%d, Device Resets=%d, Target Resets=%d\n",
SCST_LOCAL_VERSION, scst_local_version_date,
atomic_read(&num_aborts), atomic_read(&num_dev_resets),
atomic_read(&num_target_resets));
if (pos < offset) {
len = 0;
begin = pos;
}
if (start)
*start = buffer + (offset - begin);
len -= (offset - begin);
if (len > length)
len = length;
TRACE_EXIT_RES(len);
return len;
}
#else
static int scst_local_show_info(struct seq_file *file, struct Scsi_Host *host)
{
seq_printf(file, "scst_local adapter driver, version "
"%s [%s]\nAborts=%d, Device Resets=%d, Target Resets=%d\n",
SCST_LOCAL_VERSION, scst_local_version_date,
atomic_read(&num_aborts), atomic_read(&num_dev_resets),
atomic_read(&num_target_resets));
return 0;
}
#endif
static const char *scst_local_info(struct Scsi_Host *shp)
{
static char scst_local_info_buf[256];
TRACE_ENTRY();
sprintf(scst_local_info_buf, "scst_local, version %s [%s], "
"Aborts: %d, Device Resets: %d, Target Resets: %d",
SCST_LOCAL_VERSION, scst_local_version_date,
atomic_read(&num_aborts), atomic_read(&num_dev_resets),
atomic_read(&num_target_resets));
TRACE_EXIT();
return scst_local_info_buf;
}
#else /* CONFIG_SCST_PROC */
/**
** Tgtt attributes
**/
static ssize_t scst_local_version_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
sprintf(buf, "%s/%s\n", SCST_LOCAL_VERSION, scst_local_version_date);
#ifdef CONFIG_SCST_EXTRACHECKS
strcat(buf, "EXTRACHECKS\n");
#endif
#ifdef CONFIG_SCST_TRACING
strcat(buf, "TRACING\n");
#endif
#ifdef CONFIG_SCST_DEBUG
strcat(buf, "DEBUG\n");
#endif
TRACE_EXIT();
return strlen(buf);
}
static struct kobj_attribute scst_local_version_attr =
__ATTR(version, S_IRUGO, scst_local_version_show, NULL);
static ssize_t scst_local_stats_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
return sprintf(buf,
"Aborts: %d, Device Resets: %d, Target Resets: %d\n",
atomic_read(&num_aborts), atomic_read(&num_dev_resets),
atomic_read(&num_target_resets));
}
static struct kobj_attribute scst_local_stats_attr =
__ATTR(stats, S_IRUGO, scst_local_stats_show, NULL);
static const struct attribute *scst_local_tgtt_attrs[] = {
&scst_local_version_attr.attr,
&scst_local_stats_attr.attr,
NULL,
};
/**
** Tgt attributes
**/
static ssize_t scst_local_scsi_transport_version_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
struct scst_tgt *scst_tgt;
struct scst_local_tgt *tgt;
ssize_t res = -ENOENT;
if (down_read_trylock(&scst_local_exit_rwsem) == 0)
goto out;
res = -E_TGT_PRIV_NOT_YET_SET;
scst_tgt = container_of(kobj, struct scst_tgt, tgt_kobj);
tgt = scst_tgt_get_tgt_priv(scst_tgt);
if (!tgt)
goto out_up;
if (tgt->scsi_transport_version != 0)
res = sprintf(buf, "0x%x\n%s", tgt->scsi_transport_version,
SCST_SYSFS_KEY_MARK "\n");
else
res = sprintf(buf, "0x%x\n", 0x0BE0); /* SAS */
out_up:
up_read(&scst_local_exit_rwsem);
out:
return res;
}
static ssize_t scst_local_scsi_transport_version_store(struct kobject *kobj,
struct kobj_attribute *attr, const char *buffer, size_t size)
{
ssize_t res = -ENOENT;
struct scst_tgt *scst_tgt;
struct scst_local_tgt *tgt;
unsigned long val;
if (down_read_trylock(&scst_local_exit_rwsem) == 0)
goto out;
res = -E_TGT_PRIV_NOT_YET_SET;
scst_tgt = container_of(kobj, struct scst_tgt, tgt_kobj);
tgt = scst_tgt_get_tgt_priv(scst_tgt);
if (!tgt)
goto out_up;
res = kstrtoul(buffer, 0, &val);
if (res != 0) {
PRINT_ERROR("strtoul() for %s failed: %zd", buffer, res);
goto out_up;
}
tgt->scsi_transport_version = val;
res = size;
out_up:
up_read(&scst_local_exit_rwsem);
out:
return res;
}
static struct kobj_attribute scst_local_scsi_transport_version_attr =
__ATTR(scsi_transport_version, S_IRUGO | S_IWUSR,
scst_local_scsi_transport_version_show,
scst_local_scsi_transport_version_store);
static ssize_t scst_local_phys_transport_version_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
struct scst_tgt *scst_tgt;
struct scst_local_tgt *tgt;
ssize_t res = -ENOENT;
if (down_read_trylock(&scst_local_exit_rwsem) == 0)
goto out;
res = -E_TGT_PRIV_NOT_YET_SET;
scst_tgt = container_of(kobj, struct scst_tgt, tgt_kobj);
tgt = scst_tgt_get_tgt_priv(scst_tgt);
if (!tgt)
goto out_up;
res = sprintf(buf, "0x%x\n%s", tgt->phys_transport_version,
(tgt->phys_transport_version != 0) ?
SCST_SYSFS_KEY_MARK "\n" : "");
out_up:
up_read(&scst_local_exit_rwsem);
out:
return res;
}
static ssize_t scst_local_phys_transport_version_store(struct kobject *kobj,
struct kobj_attribute *attr, const char *buffer, size_t size)
{
ssize_t res = -ENOENT;
struct scst_tgt *scst_tgt;
struct scst_local_tgt *tgt;
unsigned long val;
if (down_read_trylock(&scst_local_exit_rwsem) == 0)
goto out;
res = -E_TGT_PRIV_NOT_YET_SET;
scst_tgt = container_of(kobj, struct scst_tgt, tgt_kobj);
tgt = scst_tgt_get_tgt_priv(scst_tgt);
if (!tgt)
goto out_up;
res = kstrtoul(buffer, 0, &val);
if (res != 0) {
PRINT_ERROR("strtoul() for %s failed: %zd", buffer, res);
goto out_up;
}
tgt->phys_transport_version = val;
res = size;
out_up:
up_read(&scst_local_exit_rwsem);
out:
return res;
}
static struct kobj_attribute scst_local_phys_transport_version_attr =
__ATTR(phys_transport_version, S_IRUGO | S_IWUSR,
scst_local_phys_transport_version_show,
scst_local_phys_transport_version_store);
static const struct attribute *scst_local_tgt_attrs[] = {
&scst_local_scsi_transport_version_attr.attr,
&scst_local_phys_transport_version_attr.attr,
NULL,
};
/**
** Session attributes
**/
static ssize_t scst_local_transport_id_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
ssize_t res;
struct scst_session *scst_sess;
struct scst_local_sess *sess;
uint8_t *tr_id;
int tr_id_len, i;
if (down_read_trylock(&scst_local_exit_rwsem) == 0)
return -ENOENT;
scst_sess = container_of(kobj, struct scst_session, sess_kobj);
sess = scst_sess_get_tgt_priv(scst_sess);
mutex_lock(&sess->tr_id_mutex);
if (sess->transport_id != NULL) {
tr_id = sess->transport_id;
tr_id_len = sess->transport_id_len;
} else {
res = scst_local_get_sas_transport_id(sess, &tr_id, &tr_id_len);
if (res != 0)
goto out_unlock;
}
res = 0;
for (i = 0; i < tr_id_len; i++)
res += sprintf(&buf[res], "%c", tr_id[i]);
if (sess->transport_id == NULL)
kfree(tr_id);
out_unlock:
mutex_unlock(&sess->tr_id_mutex);
up_read(&scst_local_exit_rwsem);
return res;
}
static ssize_t scst_local_transport_id_store(struct kobject *kobj,
struct kobj_attribute *attr, const char *buffer, size_t size)
{
ssize_t res;
struct scst_session *scst_sess;
struct scst_local_sess *sess;
if (down_read_trylock(&scst_local_exit_rwsem) == 0)
return -ENOENT;
scst_sess = container_of(kobj, struct scst_session, sess_kobj);
sess = scst_sess_get_tgt_priv(scst_sess);
mutex_lock(&sess->tr_id_mutex);
kfree(sess->transport_id);
sess->transport_id = NULL;
sess->transport_id_len = 0;
if (size == 0)
goto out_res;
sess->transport_id = kzalloc(size, GFP_KERNEL);
if (sess->transport_id == NULL) {
PRINT_ERROR("Allocation of transport_id (size %zd) failed",
size);
res = -ENOMEM;
goto out_unlock;
}
sess->transport_id_len = size;
memcpy(sess->transport_id, buffer, sess->transport_id_len);
out_res:
res = size;
out_unlock:
mutex_unlock(&sess->tr_id_mutex);
up_read(&scst_local_exit_rwsem);
return res;
}
static struct kobj_attribute scst_local_transport_id_attr =
__ATTR(transport_id, S_IRUGO | S_IWUSR,
scst_local_transport_id_show,
scst_local_transport_id_store);
static const struct attribute *scst_local_sess_attrs[] = {
&scst_local_transport_id_attr.attr,
NULL,
};
static ssize_t scst_local_sysfs_add_target(const char *target_name, char *params)
{
int res;
struct scst_local_tgt *tgt;
char *param, *p;
TRACE_ENTRY();
if (down_read_trylock(&scst_local_exit_rwsem) == 0)
return -ENOENT;
res = scst_local_add_target(target_name, &tgt);
if (res != 0)
goto out_up;
while (1) {
param = scst_get_next_token_str(¶ms);
if (param == NULL)
break;
p = scst_get_next_lexem(¶m);
if (*p == '\0')
break;
if (strcasecmp("session_name", p) != 0) {
PRINT_ERROR("Unknown parameter %s", p);
res = -EINVAL;
goto out_remove;
}
p = scst_get_next_lexem(¶m);
if (*p == '\0') {
PRINT_ERROR("Wrong session name %s", p);
res = -EINVAL;
goto out_remove;
}
res = scst_local_add_adapter(tgt, p);
if (res != 0)
goto out_remove;
}
out_up:
up_read(&scst_local_exit_rwsem);
TRACE_EXIT_RES(res);
return res;
out_remove:
scst_local_remove_target(tgt);
goto out_up;
}
static ssize_t scst_local_sysfs_del_target(const char *target_name)
{
int res;
struct scst_local_tgt *tgt;
bool deleted = false;
TRACE_ENTRY();
if (down_read_trylock(&scst_local_exit_rwsem) == 0)
return -ENOENT;
mutex_lock(&scst_local_mutex);
list_for_each_entry(tgt, &scst_local_tgts_list, tgts_list_entry) {
if (strcmp(target_name, tgt->scst_tgt->tgt_name) == 0) {
__scst_local_remove_target(tgt);
deleted = true;
break;
}
}
mutex_unlock(&scst_local_mutex);
if (!deleted) {
PRINT_ERROR("Target %s not found", target_name);
res = -ENOENT;
goto out_up;
}
res = 0;
out_up:
up_read(&scst_local_exit_rwsem);
TRACE_EXIT_RES(res);
return res;
}
static ssize_t scst_local_sysfs_mgmt_cmd(char *buf)
{
ssize_t res = 0;
char *command, *target_name, *session_name;
struct scst_local_tgt *t, *tgt;
TRACE_ENTRY();
if (down_read_trylock(&scst_local_exit_rwsem) == 0)
return -ENOENT;
command = scst_get_next_lexem(&buf);
target_name = scst_get_next_lexem(&buf);
if (*target_name == '\0') {
PRINT_ERROR("%s", "Target name required");
res = -EINVAL;
goto out_up;
}
mutex_lock(&scst_local_mutex);
tgt = NULL;
list_for_each_entry(t, &scst_local_tgts_list, tgts_list_entry) {
if (strcmp(t->scst_tgt->tgt_name, target_name) == 0) {
tgt = t;
break;
}
}
if (tgt == NULL) {
PRINT_ERROR("Target %s not found", target_name);
res = -EINVAL;
goto out_unlock;
}
session_name = scst_get_next_lexem(&buf);
if (*session_name == '\0') {
PRINT_ERROR("%s", "Session name required");
res = -EINVAL;
goto out_unlock;
}
if (strcasecmp("add_session", command) == 0) {
res = __scst_local_add_adapter(tgt, session_name, true);
} else if (strcasecmp("del_session", command) == 0) {
struct scst_local_sess *s, *sess = NULL;
list_for_each_entry(s, &tgt->sessions_list,
sessions_list_entry) {
if (strcmp(s->scst_sess->initiator_name, session_name) == 0) {
sess = s;
break;
}
}
if (sess == NULL) {
PRINT_ERROR("Session %s not found (target %s)",
session_name, target_name);
res = -EINVAL;
goto out_unlock;
}
scst_local_close_session_impl(sess, false);
}
out_unlock:
mutex_unlock(&scst_local_mutex);
out_up:
up_read(&scst_local_exit_rwsem);
TRACE_EXIT_RES(res);
return res;
}
#endif /* CONFIG_SCST_PROC */
static int scst_local_abort(struct scsi_cmnd *SCpnt)
{
struct scst_local_sess *sess;
int ret;
DECLARE_COMPLETION_ONSTACK(dev_reset_completion);
TRACE_ENTRY();
sess = to_scst_lcl_sess(scsi_get_device(SCpnt->device->host));
ret = scst_rx_mgmt_fn_tag(sess->scst_sess, SCST_ABORT_TASK, SCpnt->tag,
false, &dev_reset_completion);
/* Now wait for the completion ... */
wait_for_completion_interruptible(&dev_reset_completion);
atomic_inc(&num_aborts);
if (ret == 0)
ret = SUCCESS;
TRACE_EXIT_RES(ret);
return ret;
}
static int scst_local_device_reset(struct scsi_cmnd *SCpnt)
{
struct scst_local_sess *sess;
struct scsi_lun lun;
int ret;
DECLARE_COMPLETION_ONSTACK(dev_reset_completion);
TRACE_ENTRY();
sess = to_scst_lcl_sess(scsi_get_device(SCpnt->device->host));
int_to_scsilun(SCpnt->device->lun, &lun);
ret = scst_rx_mgmt_fn_lun(sess->scst_sess, SCST_LUN_RESET,
lun.scsi_lun, sizeof(lun), false,
&dev_reset_completion);
/* Now wait for the completion ... */
wait_for_completion_interruptible(&dev_reset_completion);
atomic_inc(&num_dev_resets);
if (ret == 0)
ret = SUCCESS;
TRACE_EXIT_RES(ret);
return ret;
}
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 25))
static int scst_local_target_reset(struct scsi_cmnd *SCpnt)
{
struct scst_local_sess *sess;
struct scsi_lun lun;
int ret;
DECLARE_COMPLETION_ONSTACK(dev_reset_completion);
TRACE_ENTRY();
sess = to_scst_lcl_sess(scsi_get_device(SCpnt->device->host));
int_to_scsilun(SCpnt->device->lun, &lun);
ret = scst_rx_mgmt_fn_lun(sess->scst_sess, SCST_TARGET_RESET,
lun.scsi_lun, sizeof(lun), false,
&dev_reset_completion);
/* Now wait for the completion ... */
wait_for_completion_interruptible(&dev_reset_completion);
atomic_inc(&num_target_resets);
if (ret == 0)
ret = SUCCESS;
TRACE_EXIT_RES(ret);
return ret;
}
#endif
static void scst_local_copy_sense(struct scsi_cmnd *cmnd, struct scst_cmd *scst_cmnd)
{
int scst_cmnd_sense_len = scst_cmd_get_sense_buffer_len(scst_cmnd);
TRACE_ENTRY();
scst_cmnd_sense_len = min(scst_cmnd_sense_len, SCSI_SENSE_BUFFERSIZE);
memcpy(cmnd->sense_buffer, scst_cmd_get_sense_buffer(scst_cmnd),
scst_cmnd_sense_len);
TRACE_BUFFER("Sense set", cmnd->sense_buffer, scst_cmnd_sense_len);
TRACE_EXIT();
return;
}
/*
* Utility function to handle processing of done and allow
* easy insertion of error injection if desired
*/
static int scst_local_send_resp(struct scsi_cmnd *cmnd,
struct scst_cmd *scst_cmnd,
void (*done)(struct scsi_cmnd *),
int scsi_result)
{
int ret = 0;
TRACE_ENTRY();
if (scst_cmnd) {
/* The buffer isn't ours, so let's be safe and restore it */
scst_check_restore_sg_buff(scst_cmnd);
/* Simulate autosense by this driver */
if (unlikely(scst_sense_valid(scst_cmnd->sense)))
scst_local_copy_sense(cmnd, scst_cmnd);
}
cmnd->result = scsi_result;
done(cmnd);
TRACE_EXIT_RES(ret);
return ret;
}
/*
* This does the heavy lifting ... we pass all the commands on to the
* target driver and have it do its magic ...
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
static int scst_local_queuecommand(struct Scsi_Host *host,
struct scsi_cmnd *SCpnt)
#else
static int scst_local_queuecommand_lck(struct scsi_cmnd *SCpnt,
void (*done)(struct scsi_cmnd *))
__acquires(&h->host_lock)
__releases(&h->host_lock)
#endif
{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
struct scst_local_tgt_specific *tgt_specific = NULL;
#endif
struct scst_local_sess *sess;
struct scatterlist *sgl = NULL;
int sgl_count = 0;
struct scsi_lun lun;
struct scst_cmd *scst_cmd = NULL;
scst_data_direction dir;
TRACE_ENTRY();
TRACE_DBG("lun %lld, cmd: 0x%02X", (u64)SCpnt->device->lun,
SCpnt->cmnd[0]);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 37)
/*
* We save a pointer to the done routine in SCpnt->scsi_done and
* we save that as tgt specific stuff below.
*/
SCpnt->scsi_done = done;
#endif
sess = to_scst_lcl_sess(scsi_get_device(SCpnt->device->host));
if (sess->unregistering) {
SCpnt->result = DID_BAD_TARGET << 16;
SCpnt->scsi_done(SCpnt);
return 0;
}
scsi_set_resid(SCpnt, 0);
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
/*
* Allocate a tgt_specific_structure. We need this in case we need
* to construct a single element SGL.
*/
tgt_specific = kmem_cache_alloc(tgt_specific_pool, GFP_ATOMIC);
if (!tgt_specific) {
PRINT_ERROR("Unable to create tgt_specific (size %zu)",
sizeof(*tgt_specific));
return SCSI_MLQUEUE_HOST_BUSY;
}
tgt_specific->cmnd = SCpnt;
tgt_specific->done = done;
#endif
/*
* Tell the target that we have a command ... but first we need
* to get the LUN into a format that SCST understand
*
* NOTE! We need to call it with atomic parameter true to not
* get into mem alloc deadlock when mounting file systems over