forked from wookey-project/libmassstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscsi.c
1778 lines (1461 loc) · 54.2 KB
/
scsi.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 2018 The wookey project team <[email protected]>
* - Ryad Benadjila
* - Arnauld Michelizza
* - Mathieu Renard
* - Philippe Thierry
* - Philippe Trebuchet
*
* This package is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* the Free Software Foundation; either version 2.1 of the License, or (at
* ur option) any later version.
*
* This package is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this package; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "libc/malloc.h"
#include "libc/stdio.h"
#include "libc/nostd.h"
#include "libc/string.h"
#include "api/scsi.h"
#include "libc/string.h"
#include "libc/queue.h"
#include "usb.h"
#include "usb_bbb.h"
#include "autoconf.h"
#include "libc/syscall.h"
#include "libc/arpa/inet.h"
#include "wookey_ipc.h"
#include "usb_control_mass_storage.h"
#include "scsi_cmd.h"
#include "scsi_resp.h"
#include "scsi_log.h"
#include "scsi_automaton.h"
#define SCSI_DEBUG CONFIG_USR_LIB_MASSSTORAGE_DEBUG
/*
* The SCSI stack context. This is a global variable, which means
* that the SCSI stack is not reentrant (not for scsi_context write access).
* As most micro-controlers are not multicore based, this should not be
* a problem.
*/
typedef enum {
SCSI_TRANSMIT_LINE_READY = 0,
SCSI_TRANSMIT_LINE_BUSY,
SCSI_TRANSMIT_LINE_ERROR,
} transmition_line_state_t;
typedef enum {
SCSI_DIRECTION_IDLE = 0,
SCSI_DIRECTION_SEND,
SCSI_DIRECTION_RECV,
} transmission_direction_t;
typedef struct {
volatile transmission_direction_t direction;
volatile transmition_line_state_t line_state;
volatile uint32_t size_to_process;
uint32_t addr;
uint32_t error;
queue_t *queue; /* used to avoid lock loop */
volatile bool queue_empty;
uint8_t *global_buf;
uint16_t global_buf_len;
uint32_t block_size;
uint32_t storage_size;
} scsi_context_t;
scsi_context_t scsi_ctx = {
.direction = SCSI_DIRECTION_IDLE,
.line_state = SCSI_TRANSMIT_LINE_READY,
.size_to_process = 0,
.addr = 0,
.error = 0,
.queue = NULL,
.queue_empty = true,
.global_buf = NULL,
.global_buf_len = 0,
.block_size = 0,
.storage_size = 0,
};
static volatile cdb_t queued_cdb = { 0 };
static void scsi_error(scsi_sense_key_t sensekey, uint8_t asc, uint8_t ascq)
{
#if SCSI_DEBUG
aprintf("%s: %s: status=%d\n", __func__, __func__, sensekey);
aprintf("%s: state -> Error\n", __func__);
#endif
scsi_ctx.error = (sensekey << 16 | asc << 8 | ascq);
/* returning status */
usb_bbb_send_csw(CSW_STATUS_FAILED, 0);
scsi_set_state(SCSI_IDLE);
}
/*********************************************************************
* Mutexes, protection against race conditions...
********************************************************************/
/*
* \brief entering a critical section
*
* During this critical section, any ISR is postponed to avoid any
* race condition on variables shared in write-access between ISR and
* main thread. See sys_lock() syscall API documentation.
*
* Critical sections must be as short as possible to avoid border
* effects such as latency increase and ISR queue overloading.
*/
static inline mbed_error_t enter_critical_section(void)
{
uint8_t ret;
ret = sys_lock(LOCK_ENTER); /* Enter in critical section */
if (ret != SYS_E_DONE) {
printf("%s: Error: failed entering critical section!\n", __func__);
return MBED_ERROR_BUSY;
}
return MBED_ERROR_NONE;
}
/*
* \brief leaving a critical section
*
* Reallow the execution of the previously postponed task ISR.
*/
static inline void leave_critical_section(void)
{
sys_lock(LOCK_EXIT); /* Exit from critical section, should not
fail */
return;
}
/********* About debugging and pretty printing **************/
#if SCSI_DEBUG
static void scsi_debug_dump_cmd(cdb_t * current_cdb, uint8_t scsi_cmd)
{
if (!current_cdb) {
return;
}
if (SCSI_DEBUG < 2) {
return;
}
switch (scsi_cmd) {
case SCSI_CMD_MODE_SENSE_10:
{
printf("mode_sense_10:\n");
printf("\treserved1 : %x\n",
current_cdb->payload.cdb10_mode_sense.LLBAA);
printf("\tLLBAA : %x\n",
current_cdb->payload.cdb10_mode_sense.LLBAA);
printf("\tDBD : %x\n",
current_cdb->payload.cdb10_mode_sense.DBD);
printf("\treserved2 : %x\n",
current_cdb->payload.cdb10_mode_sense.reserved2);
printf("\tPC : %x\n",
current_cdb->payload.cdb10_mode_sense.PC);
printf("\tpage_code : %x\n",
current_cdb->payload.cdb10_mode_sense.page_code);
printf("\tsub_page_code : %x\n",
current_cdb->payload.cdb10_mode_sense.sub_page_code);
printf("\treserved3 : %x\n",
current_cdb->payload.cdb10_mode_sense.reserved3);
printf("\tallocation_length : %x\n",
current_cdb->payload.cdb10_mode_sense.allocation_length);
printf("\tcontrol : %x\n",
current_cdb->payload.cdb10_mode_sense.control);
break;
}
case SCSI_CMD_MODE_SENSE_6:
{
printf("mode_sense_6:\n");
printf("\tLUN : %x\n",
current_cdb->payload.cdb6_mode_sense.LUN);
printf("\treserved4 : %x\n",
current_cdb->payload.cdb6_mode_sense.reserved4);
printf("\tDBD : %x\n",
current_cdb->payload.cdb6_mode_sense.DBD);
printf("\treserved3 : %x\n",
current_cdb->payload.cdb6_mode_sense.reserved3);
printf("\tPC : %x\n",
current_cdb->payload.cdb6_mode_sense.PC);
printf("\tpage_code : %x\n",
current_cdb->payload.cdb6_mode_sense.page_code);
printf("\treserved2 : %x\n",
current_cdb->payload.cdb6_mode_sense.reserved2);
printf("\tallocation_length : %x\n",
current_cdb->payload.cdb6_mode_sense.allocation_length);
printf("\tvendor_specific : %x\n",
current_cdb->payload.cdb6_mode_sense.vendor_specific);
printf("\treserved1 : %x\n",
current_cdb->payload.cdb6_mode_sense.reserved1);
printf("\tflag : %x\n",
current_cdb->payload.cdb6_mode_sense.flag);
printf("\tlink : %x\n",
current_cdb->payload.cdb6_mode_sense.link);
break;
}
case SCSI_CMD_INQUIRY:
{
printf("current_cdbuiry:\n");
printf("CMDDT: %x\n",
current_cdb->payload.cdb6_inquiry.CMDDT);
printf("EVPD: %x\n",
current_cdb->payload.cdb6_inquiry.EVPD);
printf("page_code: %x\n",
current_cdb->payload.cdb6_inquiry.page_code);
printf("allocation_len:%x\n",
ntohs(current_cdb->payload.cdb6_inquiry.
allocation_length));
printf("control : %x\n",
current_cdb->payload.cdb6_inquiry.control);
break;
}
default:
break;
}
}
#endif
/******** End of debugging and pretty printing **************/
/********* About various utility functions **************/
/*
* Is the current context compatible with data reception ?
*/
static inline bool scsi_is_ready_for_data_receive(void)
{
return ((scsi_ctx.direction == SCSI_DIRECTION_RECV
|| scsi_ctx.direction == SCSI_DIRECTION_IDLE)
&& scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY);
}
/*
* Is the current context compatible with data transmission ?
*/
static inline bool scsi_is_ready_for_data_send(void)
{
return ((scsi_ctx.direction == SCSI_DIRECTION_SEND
|| scsi_ctx.direction == SCSI_DIRECTION_IDLE)
&& scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY);
}
/*
* Request data of given size from BULK stack.
* This function is sending an asynchronous read request. The
* read terminaison is acknowledge by a trigger on scsi_data_available()
*/
void scsi_get_data(void *buffer, uint32_t size)
{
#if SCSI_DEBUG > 1
printf("%s: size: %d \n", __func__, size);
#endif
while (!scsi_is_ready_for_data_receive()) {
continue;
}
scsi_ctx.direction = SCSI_DIRECTION_RECV;
scsi_ctx.line_state = SCSI_TRANSMIT_LINE_BUSY;
scsi_ctx.addr = 0;
usb_bbb_read(buffer, size, 1);
}
/*
* Request to send data of given size into the BULK stack.
* This function is sending an asynchronous write request. The
* transmission terminaison is acknowledge by a trigger on scsi_data_sent()
*/
void scsi_send_data(void *data, uint32_t size)
{
#if SCSI_DEBUG > 1
printf("%s: size: %d \n", __func__, size);
#endif
scsi_ctx.direction = SCSI_DIRECTION_SEND;
scsi_ctx.line_state = SCSI_TRANSMIT_LINE_BUSY;
scsi_ctx.addr = 0;
usb_bbb_send(data, size, 2); /* FIXME HARCODED ENDPOINT */
}
/*
* Trigger on input data available
*/
static void scsi_data_available(uint32_t size)
{
#if SCSI_DEBUG > 1
/* this function is triggered, printing trigger events is done only
* on debug level 2 */
aprintf("%s: %d\n", __func__, size);
#endif
if (size >= scsi_ctx.size_to_process) {
scsi_ctx.size_to_process = 0;
} else {
scsi_ctx.size_to_process -= size;
}
scsi_ctx.line_state = SCSI_TRANSMIT_LINE_READY;
if (scsi_ctx.size_to_process == 0) {
usb_bbb_send_csw(CSW_STATUS_SUCCESS, 0);
scsi_ctx.direction = SCSI_DIRECTION_IDLE;
scsi_set_state(SCSI_IDLE);
}
}
/*
* Trigger on data sent by IP
*/
static void scsi_data_sent(void)
{
#if SCSI_DEBUG > 1
/* this function is triggered, printing trigger events is done only
* on debug level 2 */
aprintf("%s\n", __func__);
#endif
if (scsi_ctx.size_to_process > scsi_ctx.global_buf_len) {
scsi_ctx.size_to_process -= scsi_ctx.global_buf_len;
} else {
scsi_ctx.size_to_process = 0;
}
scsi_ctx.line_state = SCSI_TRANSMIT_LINE_READY;
if (scsi_ctx.size_to_process == 0) {
usb_bbb_send_csw(CSW_STATUS_SUCCESS, 0);
scsi_ctx.direction = SCSI_DIRECTION_IDLE;
scsi_set_state(SCSI_IDLE);
}
}
static void scsi_forge_mode_sense_response(u_mode_parameter * response,
uint8_t mode)
{
if (mode == SCSI_CMD_MODE_SENSE_6) {
memset(response, 0x0, sizeof(mode_parameter6_data_t));
/* We only send back the mode parameter header with no data */
response->mode6.header.mode_data_length = 3; /* The number of bytes that follow. */
response->mode6.header.medium_type = 0; /* The media type SBC. */
response->mode6.header.block_descriptor_length = 0; /* A block descriptor length of zero indicates that no block descriptors */
/* setting shortlba */
#if 0
/* FIXME: Caching is buggy right now... */
/* setting caching mode */
response->mode6.caching.SPF = 0;
response->mode6.caching.page_code = 0x08;
response->mode6.caching.page_length = 0x12;
response->mode6.caching.WCE = 0x1;
response->mode6.caching.RCD = 0x1;
response->mode6.caching.FSW = 0x1;
response->mode6.caching.DRA = 0x1;
response->mode6.caching.NV_DIS = 0x1;
#endif
} else if (mode == SCSI_CMD_MODE_SENSE_10) {
memset(response, 0x0, sizeof(mode_parameter10_data_t));
/* We only send back the mode parameter header with no data */
response->mode10.header.mode_data_length = 3; /* The number of bytes that follow. */
response->mode10.header.medium_type = 0; /* The media type SBC. */
response->mode10.header.block_descriptor_length = 0; /* A block descriptor length of zero indicates that no block descriptors */
response->mode10.header.longLBA = 0;
/* are included in the mode parameter list. */
#if 0
/* FIXME: Caching is buggy right now... */
/* setting caching mode */
response->mode10.caching.SPF = 0;
response->mode10.caching.page_code = 0x08;
response->mode10.caching.page_length = 0x12;
response->mode10.caching.WCE = 0x1;
response->mode10.caching.RCD = 0x1;
response->mode10.caching.FSW = 0x1;
response->mode10.caching.DRA = 0x1;
response->mode10.caching.NV_DIS = 0x1;
#endif
}
}
/************** End of utility functions **********************/
/*
* SCSI automaton implementation.
*
* The SCSI automaton is based on two main entry points:
* - scsi_parse_cdb, triggered by USB ISR when a SCSI command is received
* - scsi_exec_automaton, which effectively execute the SCSI command, in
* main thread mode
*
* scsi_parse_cdb enqueue received command in the command queue. This functions
* is as small as possible to reduce the ISR execution cost.
* scsi_exec_automaton check for the command queue and dequeue commands to
* execute them.
*
* All scsi_cmd_* procedures execute a given SCSI command. These procedures
* are executed through scsi_exec_automaton.
*/
extern volatile bool reset_requested;
/*
* Enqueue any received SCSI command
* this function is executed in a handler context when a command comes from USB.
*/
/*@ requires cdb_len <= sizeof(cdb_t); */
static void scsi_parse_cdb(uint8_t cdb[], uint8_t cdb_len)
{
if(reset_requested == true){
while(reset_requested == true){
continue;
}
return;
}
/* Only up to 16 bytes commands are supported: bigger commands are truncated,
* See cdb_t definition in scsi_cmd.h */
memcpy((void *) &queued_cdb, (void *) cdb, cdb_len);
scsi_ctx.queue_empty = false;
return;
}
/*
* Commands implementation.
*
* All commands implementation check the SCSI state automaton to validate that
* the transition is authorized, and then execute the command.
*/
/* SCSI_CMD_INQUIRY */
static void scsi_cmd_inquiry(scsi_state_t current_state, cdb_t * cdb)
{
inquiry_data_t response;
cdb6_inquiry_t *inq;
uint8_t next_state;
#if SCSI_DEBUG
printf("%s:\n", __func__);
#endif
/* Sanity check */
if (cdb == NULL) {
goto invalid_transition;
}
/* Sanity check and next state detection */
if (!scsi_is_valid_transition(current_state, cdb->operation)) {
goto invalid_transition;
}
next_state = scsi_next_state(current_state, cdb->operation);
scsi_set_state(next_state);
/* effective transition execution (if needed) */
inq = &(cdb->payload.cdb6_inquiry);
#if SCSI_DEBUG > 1
scsi_debug_dump_cmd(cdb, SCSI_CMD_INQUIRY);
#endif
/* sanitize received cmd in conformity with SCSI standard */
if (inq->EVPD == 0 && ntohs(inq->allocation_length) < 5) {
/* invalid: additional fields parameter can't be send */
goto invalid_cmd;
}
if (inq->EVPD == 1 && ntohs(inq->allocation_length) < 4) {
/* invalid: additional fields parameter can't be send */
goto invalid_cmd;
}
/* Most of support bits are set to 0
* version is 0 because the device does not claim conformance to any
* standard
*/
memset((void *) &response, 0, sizeof(response));
response.periph_device_type = 0x0; /* direct access block device */
response.RMB = 1; /* Removable media */
response.data_format = 2; /* < 2 obsoletes, > 2 reserved */
response.additional_len = sizeof(response) - 5; /* (36 - 5) bytes after this one remain */
response.additional_len = sizeof(response) - 5; /* (36 - 5) bytes after this one remain */
/* empty char must be set with spaces */
memset(response.vendor_info, 0x20, sizeof(response.vendor_info));
memcpy(response.vendor_info, CONFIG_USB_DEV_MANUFACTURER,
strlen(CONFIG_USB_DEV_MANUFACTURER));
/* empty char must be set with spaces */
memset(response.product_identification, 0x20,
sizeof(response.product_identification));
memcpy(response.product_identification, CONFIG_USB_DEV_PRODNAME,
strlen(CONFIG_USB_DEV_PRODNAME));
memcpy(response.product_revision, CONFIG_USB_DEV_REVISION,
strlen(CONFIG_USB_DEV_REVISION));
#if SCSI_DEBUG
printf("%s: %s\n", __func__, response.product_revision);
#endif
if (inq->allocation_length > 0) {
usb_bbb_send((uint8_t *) & response,
(ntohs(inq->allocation_length) <
sizeof(response)) ? ntohs(inq->
allocation_length) :
sizeof(response), 2);
} else {
printf("allocation length is 0\n");
}
return;
invalid_cmd:
printf("%s: malformed cmd\n", __func__);
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
invalid_transition:
printf("%s: invalid_transition\n", __func__);
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
}
static void scsi_cmd_prevent_allow_medium_removal(scsi_state_t current_state,
cdb_t * current_cdb)
{
uint8_t next_state;
#if SCSI_DEBUG
printf("%s\n", __func__);
#endif
/* Sanity check */
if (current_cdb == NULL) {
goto invalid_transition;
}
/* Sanity check and next state detection */
if (!scsi_is_valid_transition(current_state, current_cdb->operation)) {
goto invalid_transition;
}
next_state = scsi_next_state(current_state, current_cdb->operation);
scsi_set_state(next_state);
#if SCSI_DEBUG > 1
printf("%s: Prevent allow medium removal: %x\n", __func__,
current_cdb->payload.cdb10_prevent_allow_removal.prevent);
#endif
/* FIXME Add callback ? */
usb_bbb_send_csw(CSW_STATUS_SUCCESS, 0);
return;
/* effective transition execution (if needed) */
invalid_transition:
printf("%s: invalid_transition\n", __func__);
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
}
static void scsi_cmd_read_format_capacities(scsi_state_t current_state,
cdb_t * current_cdb)
{
uint8_t next_state;
#if SCSI_DEBUG
printf("%s\n", __func__);
#endif
/* Sanity check */
if (current_cdb == NULL) {
goto invalid_transition;
}
/* Sanity check and next state detection */
if (!scsi_is_valid_transition(current_state, current_cdb->operation)) {
goto invalid_transition;
}
next_state = scsi_next_state(current_state, current_cdb->operation);
scsi_set_state(next_state);
cdb12_read_format_capacities_t *rfc =
&(current_cdb->payload.cdb12_read_format_capacities);
capacity_list_t response = {
.list_header.reserved_1 = 0,
.list_header.reserved_2 = 0,
.list_header.capacity_list_length = 8,
.cur_max_capacity.number_of_blocks = htonl(scsi_ctx.storage_size - 1),
.cur_max_capacity.reserved = 0,
.cur_max_capacity.descriptor_code = FORMATTED_MEDIA,
.cur_max_capacity.block_length = htonl(scsi_ctx.block_size),
.num_format_descriptors = 1,
.formattable_descriptor.number_of_blocks =
htonl(scsi_ctx.storage_size - 1),
.formattable_descriptor.reserved = 0,
.formattable_descriptor.block_length = htonl(scsi_ctx.block_size)
};
/* we return only the current/max capacity descriptor, no formatable capacity descriptor, making the
* response size the following: */
uint32_t size =
sizeof(capacity_list_header_t) +
sizeof(curr_max_capacity_descriptor_t) + 1 +
sizeof(formattable_capacity_descriptor_t);
if (ntohs(rfc->allocation_length_msb) > 0) {
usb_bbb_send((uint8_t *) & response,
(ntohs(rfc->allocation_length_msb) <
size) ? rfc->allocation_length_msb : size, 2);
} else {
printf("allocation length is 0\n");
usb_bbb_send((uint8_t *) & response, size, 2);
}
return;
invalid_transition:
printf("%s: invalid_transition\n", __func__);
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
}
/*
* SCSI_CMD_READ_6
* INFO: this command is deprecated but is implemented for retrocompatibility
* with older Operating Systems
*/
static void scsi_cmd_read_data6(scsi_state_t current_state, cdb_t * current_cdb)
{
uint32_t num_sectors;
uint32_t total_num_sectors;
uint32_t rw_lba;
uint16_t rw_size;
uint64_t rw_addr;
uint8_t next_state;
mbed_error_t error;
#if SCSI_DEBUG
printf("%s\n", __func__);
#endif
/* Sanity check */
if (current_cdb == NULL) {
goto invalid_transition;
}
/* Sanity check and next state detection */
if (!scsi_is_valid_transition(current_state, current_cdb->operation)) {
goto invalid_transition;
}
next_state = scsi_next_state(current_state, current_cdb->operation);
/* entering READ state... */
scsi_set_state(next_state);
/* SCSI standard says that the host should not request READ10 cmd
* before requesting GET_CAPACITY cmd. In this very case, we have to
* send back INVALID to the host */
if (scsi_ctx.storage_size == 0) {
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
}
/* TODO: is the big endian is set only on the last 16 bytes of this
* unaligned field ? */
rw_lba = ntohs((uint16_t) current_cdb->payload.cdb6.logical_block)
+ (current_cdb->payload.cdb6.logical_block & 0x1f0000);
rw_size = current_cdb->payload.cdb6.transfer_blocks;
rw_addr = (uint64_t) scsi_ctx.block_size * (uint64_t) rw_lba;
/* initialize size_to_process. This variable will be upated during the
* active wait loop below by the USB BBB triggers (usb_data_sent for
* read, usb_data_available for write */
scsi_ctx.size_to_process = scsi_ctx.block_size * rw_size;
total_num_sectors = scsi_ctx.size_to_process / scsi_ctx.block_size;
#if SCSI_DEBUG > 1
printf("%s: sz %u, block_size: %u | num_sectors: %u\n", __func__,
scsi_ctx.size_to_process, scsi_ctx.block_size, total_num_sectors);
#endif
while (scsi_ctx.size_to_process > scsi_ctx.global_buf_len) {
/* There is more data to send that the buffer is able to process,
* data are sent in multiple chunks of buf_len size... */
/* INFO: num_sectors may be defined out of the loop */
num_sectors = scsi_ctx.global_buf_len / scsi_ctx.block_size;
error = scsi_storage_backend_read(rw_lba, num_sectors);
if (error == MBED_ERROR_RDERROR) {
scsi_error(SCSI_SENSE_MEDIUM_ERROR, ASC_UNRECOVERED_READ_ERROR,
ASCQ_NO_ADDITIONAL_SENSE);
goto end;
}
/* send data we have just read */
scsi_send_data(scsi_ctx.global_buf, scsi_ctx.global_buf_len);
/* increment read pointer */
rw_lba += scsi_ctx.global_buf_len / scsi_ctx.block_size;
/* active wait for data to be sent */
while (!scsi_is_ready_for_data_send()) {
continue;
}
}
/* Fractional residue */
if (scsi_ctx.size_to_process > 0) {
#if SCSI_DEBUG > 1
printf("%s: sending data residue to host.\n", __func__);
#endif
num_sectors = (scsi_ctx.size_to_process) / scsi_ctx.block_size;
error = scsi_storage_backend_read((uint32_t) rw_lba, num_sectors);
if (error == MBED_ERROR_RDERROR) {
scsi_error(SCSI_SENSE_MEDIUM_ERROR, ASC_UNRECOVERED_READ_ERROR,
ASCQ_NO_ADDITIONAL_SENSE);
goto end;
}
scsi_send_data(scsi_ctx.global_buf, scsi_ctx.size_to_process);
/* active wait for data to be sent */
while (!scsi_is_ready_for_data_send()) {
continue;
}
}
end:
return;
invalid_transition:
printf("%s: invalid_transition\n", __func__);
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
}
/* SCSI_CMD_READ_10 */
static void scsi_cmd_read_data10(scsi_state_t current_state,
cdb_t * current_cdb)
{
uint32_t num_sectors;
uint32_t total_num_sectors;
uint32_t rw_lba;
uint16_t rw_size;
uint64_t rw_addr;
uint8_t next_state;
mbed_error_t error;
#if SCSI_DEBUG
printf("%s\n", __func__);
#endif
/* Sanity check */
if (current_cdb == NULL) {
goto invalid_transition;
}
/* Sanity check and next state detection */
if (!scsi_is_valid_transition(current_state, current_cdb->operation)) {
goto invalid_transition;
}
next_state = scsi_next_state(current_state, current_cdb->operation);
/* SCSI standard says that the host should not request READ10 cmd
* before requesting GET_CAPACITY cmd. In this very case, we have to
* send back INVALID to the host */
if (scsi_ctx.storage_size == 0) {
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
}
/* entering READ state... */
scsi_set_state(next_state);
rw_lba = ntohl(current_cdb->payload.cdb10.logical_block);
rw_size = ntohs(current_cdb->payload.cdb10.transfer_blocks);
rw_addr = (uint64_t) scsi_ctx.block_size * (uint64_t) rw_lba;
/* initialize size_to_process. This variable will be upated during the
* active wait loop below by the USB BBB triggers (usb_data_sent for
* read, usb_data_available for write */
scsi_ctx.size_to_process = scsi_ctx.block_size * rw_size;
total_num_sectors = scsi_ctx.size_to_process / scsi_ctx.block_size;
#if SCSI_DEBUG > 1
printf("%s: sz %u, block_size: %u | num_sectors: %u\n", __func__,
scsi_ctx.size_to_process, scsi_ctx.block_size, total_num_sectors);
#endif
while (scsi_ctx.size_to_process > scsi_ctx.global_buf_len) {
/* There is more data to send that the buffer is able to process,
* data are sent in multiple chunks of buf_len size... */
/* INFO: num_sectors may be defined out of the loop */
num_sectors = scsi_ctx.global_buf_len / scsi_ctx.block_size;
error = scsi_storage_backend_read(rw_lba, num_sectors);
/* send data we have just read */
if (error == MBED_ERROR_RDERROR) {
scsi_error(SCSI_SENSE_MEDIUM_ERROR, ASC_UNRECOVERED_READ_ERROR,
ASCQ_NO_ADDITIONAL_SENSE);
goto end;
}
scsi_send_data(scsi_ctx.global_buf, scsi_ctx.global_buf_len);
/* increment read pointer */
rw_lba += scsi_ctx.global_buf_len / scsi_ctx.block_size;
/* active wait for data to be sent */
while (!scsi_is_ready_for_data_send()) {
continue;
}
}
/* Fractional residue */
if (scsi_ctx.size_to_process > 0) {
#if SCSI_DEBUG > 1
printf("%s: sending data residue to host.\n", __func__);
#endif
num_sectors = (scsi_ctx.size_to_process) / scsi_ctx.block_size;
error = scsi_storage_backend_read((uint32_t) rw_lba, num_sectors);
if (error == MBED_ERROR_RDERROR) {
scsi_error(SCSI_SENSE_MEDIUM_ERROR, ASC_UNRECOVERED_READ_ERROR,
ASCQ_NO_ADDITIONAL_SENSE);
goto end;
}
scsi_send_data(scsi_ctx.global_buf, scsi_ctx.size_to_process);
#if SCSI_DEBUG
#endif
/* active wait for data to be sent */
while (!scsi_is_ready_for_data_send()) {
continue;
}
}
end:
return;
invalid_transition:
printf("%s: invalid_transition\n", __func__);
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
}
/* SCSI_CMD_READ_CAPACITY_10 */
static void scsi_cmd_read_capacity10(scsi_state_t current_state,
cdb_t * current_cdb)
{
uint8_t next_state;
read_capacity10_parameter_data_t response;
uint8_t ret;
#if SCSI_DEBUG
printf("%s\n", __func__);
#endif
/* Sanity check */
if (current_cdb == NULL) {
goto invalid_transition;
}
/* Sanity check and next state detection */
if (!scsi_is_valid_transition(current_state, current_cdb->operation)) {
goto invalid_transition;
}
next_state = scsi_next_state(current_state, current_cdb->operation);
/* entering target state */
scsi_set_state(next_state);
/* let's get capacity from upper layer */
ret =
scsi_storage_backend_capacity(&(scsi_ctx.storage_size),
&(scsi_ctx.block_size));
if (ret != 0) {
/* unable to get back capacity from backend... */
scsi_error(SCSI_SENSE_MEDIUM_ERROR, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
}
/* what is expected is the _LAST_ LBA address ....
* See Working draft SCSI block cmd 5.10.2 READ CAPACITY (10) */
response.ret_lba = htonl(scsi_ctx.storage_size - 1);
response.ret_block_length = htonl(scsi_ctx.block_size);
usb_bbb_send((uint8_t *) & response,
sizeof(read_capacity10_parameter_data_t), 2);
return;
invalid_transition:
printf("%s: invalid_transition\n", __func__);
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
}
/* SCSI_CMD_READ_CAPACITY_16 */
static void scsi_cmd_read_capacity16(scsi_state_t current_state,
cdb_t * current_cdb)
{
uint8_t next_state;
read_capacity16_parameter_data_t response;
cdb16_read_capacity_16_t *rc16;
uint8_t ret;
#if SCSI_DEBUG
printf("%s\n", __func__);
#endif
/* Sanity check */
if (current_cdb == NULL) {
goto invalid_transition;
}
/* Sanity check and next state detection */
if (!scsi_is_valid_transition(current_state, current_cdb->operation)) {
goto invalid_transition;
}
next_state = scsi_next_state(current_state, current_cdb->operation);
/* entering target state */
scsi_set_state(next_state);
/* let's get capacity from upper layer */
ret =
scsi_storage_backend_capacity(&(scsi_ctx.storage_size),
&(scsi_ctx.block_size));
if (ret != 0) {
/* unable to get back capacity from backend... */
scsi_error(SCSI_SENSE_MEDIUM_ERROR, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
return;
}
/* get back cdb content from union */
rc16 = &(current_cdb->payload.cdb16_read_capacity);
/* what is expected is the _LAST_ LBA address ....
* See Working draft SCSI block cmd 5.10.2 READ CAPACITY (16) */
/* creating response... */
memset((void *) &response, 0x0, sizeof(read_capacity16_parameter_data_t));
response.ret_lba = (uint64_t) htonl(scsi_ctx.storage_size - 1);
response.ret_block_length = htonl(scsi_ctx.block_size);
response.prot_enable = 0; /* no prot_enable, protection associated fields
are disabled. */
response.rc_basis = 0x01; /* LBA is the LBA of the last logical block
on the logical unit. See Seagate SCSI
command ref., chap. 3.23.2 */
#if SCSI_DEBUG > 1
printf("%s: response[0]: %d response[1]: %d\n", __func__, response.ret_lba,
response.ret_block_length);
#endif
/* the amount of bytes sent in the response depends on the allocation
* length value set in the read_capacity16 cmd. If this value is null,
* no response should be sent.
* See Seagate SCSI command ref. chap. 3.23.2 */