iPXE
usbblk.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdint.h>
28#include <stdlib.h>
29#include <string.h>
30#include <errno.h>
31#include <ipxe/usb.h>
32#include <ipxe/scsi.h>
33#include <ipxe/xfer.h>
34#include <ipxe/uri.h>
35#include <ipxe/open.h>
36#include <ipxe/efi/efi_path.h>
37#include "usbblk.h"
38
39/** @file
40 *
41 * USB mass storage driver
42 *
43 */
44
45static void usbblk_stop ( struct usbblk_device *usbblk, int rc );
46
47/** List of USB block devices */
48static LIST_HEAD ( usbblk_devices );
49
50/******************************************************************************
51 *
52 * Endpoint management
53 *
54 ******************************************************************************
55 */
56
57/**
58 * Open endpoints
59 *
60 * @v usbblk USB block device
61 * @ret rc Return status code
62 */
63static int usbblk_open ( struct usbblk_device *usbblk ) {
64 struct usb_device *usb = usbblk->func->usb;
65 unsigned int interface = usbblk->func->interface[0];
66 int rc;
67
68 /* Sanity checks */
69 assert ( ! usbblk->in.open );
70 assert ( ! usbblk->out.open );
71
72 /* Issue reset */
73 if ( ( rc = usb_control ( usb, USBBLK_RESET, 0, interface,
74 NULL, 0 ) ) != 0 ) {
75 DBGC ( usbblk, "USBBLK %s could not issue reset: %s\n",
76 usbblk->func->name, strerror ( rc ) );
77 goto err_reset;
78 }
79
80 /* Open bulk OUT endpoint */
81 if ( ( rc = usb_endpoint_open ( &usbblk->out ) ) != 0 ) {
82 DBGC ( usbblk, "USBBLK %s could not open bulk OUT: %s\n",
83 usbblk->func->name, strerror ( rc ) );
84 goto err_open_out;
85 }
86
87 /* Clear any bulk OUT halt condition */
88 if ( ( rc = usb_endpoint_clear_halt ( &usbblk->out ) ) != 0 ) {
89 DBGC ( usbblk, "USBBLK %s could not reset bulk OUT: %s\n",
90 usbblk->func->name, strerror ( rc ) );
91 goto err_clear_out;
92 }
93
94 /* Open bulk IN endpoint */
95 if ( ( rc = usb_endpoint_open ( &usbblk->in ) ) != 0 ) {
96 DBGC ( usbblk, "USBBLK %s could not open bulk IN: %s\n",
97 usbblk->func->name, strerror ( rc ) );
98 goto err_open_in;
99 }
100
101 /* Clear any bulk IN halt condition */
102 if ( ( rc = usb_endpoint_clear_halt ( &usbblk->in ) ) != 0 ) {
103 DBGC ( usbblk, "USBBLK %s could not reset bulk IN: %s\n",
104 usbblk->func->name, strerror ( rc ) );
105 goto err_clear_in;
106 }
107
108 return 0;
109
110 err_clear_in:
111 usb_endpoint_close ( &usbblk->in );
112 err_open_in:
113 err_clear_out:
114 usb_endpoint_close ( &usbblk->out );
115 err_open_out:
116 err_reset:
117 return rc;
118}
119
120/**
121 * Close endpoints
122 *
123 * @v usbblk USB block device
124 */
125static void usbblk_close ( struct usbblk_device *usbblk ) {
126
127 /* Close bulk OUT endpoint */
128 if ( usbblk->out.open )
129 usb_endpoint_close ( &usbblk->out );
130
131 /* Close bulk IN endpoint */
132 if ( usbblk->in.open )
133 usb_endpoint_close ( &usbblk->in );
134}
135
136/******************************************************************************
137 *
138 * Bulk OUT endpoint
139 *
140 ******************************************************************************
141 */
142
143/**
144 * Issue bulk OUT command
145 *
146 * @v usbblk USB block device
147 * @ret rc Return status code
148 */
149static int usbblk_out_command ( struct usbblk_device *usbblk ) {
150 struct usbblk_command *cmd = &usbblk->cmd;
151 struct usbblk_command_wrapper *wrapper;
152 struct io_buffer *iobuf;
153 int rc;
154
155 /* Sanity checks */
156 assert ( cmd->tag );
157 assert ( ! ( cmd->scsi.data_in.len && cmd->scsi.data_out.len ) );
158
159 /* Allocate I/O buffer */
160 iobuf = alloc_iob ( sizeof ( *wrapper ) );
161 if ( ! iobuf ) {
162 rc = -ENOMEM;
163 goto err_alloc;
164 }
165
166 /* Populate command */
167 wrapper = iob_put ( iobuf, sizeof ( *wrapper ) );
168 memset ( wrapper, 0, sizeof ( *wrapper ) );
170 wrapper->tag = cmd->tag; /* non-endian */
171 if ( cmd->scsi.data_out.len ) {
172 wrapper->len = cpu_to_le32 ( cmd->scsi.data_out.len );
173 } else {
174 wrapper->len = cpu_to_le32 ( cmd->scsi.data_in.len );
175 wrapper->flags = USB_DIR_IN;
176 }
177 wrapper->lun = ntohs ( cmd->scsi.lun.u16[0] );
178 wrapper->cblen = sizeof ( wrapper->cb );
179 memcpy ( wrapper->cb, &cmd->scsi.cdb, sizeof ( wrapper->cb ) );
180
181 /* Issue command */
182 if ( ( rc = usb_stream ( &usbblk->out, iobuf, 0 ) ) != 0 ) {
183 DBGC ( usbblk, "USBBLK %s bulk OUT could not issue command: "
184 "%s\n", usbblk->func->name, strerror ( rc ) );
185 goto err_stream;
186 }
187
188 return 0;
189
190 err_stream:
191 free_iob ( iobuf );
192 err_alloc:
193 return rc;
194}
195
196/**
197 * Send bulk OUT data block
198 *
199 * @v usbblk USB block device
200 * @ret rc Return status code
201 */
202static int usbblk_out_data ( struct usbblk_device *usbblk ) {
203 struct usbblk_command *cmd = &usbblk->cmd;
204 struct io_buffer *iobuf;
205 size_t len;
206 int rc;
207
208 /* Calculate length */
209 assert ( cmd->tag );
210 assert ( cmd->scsi.data_out.len != 0 );
211 assert ( cmd->offset < cmd->scsi.data_out.len );
212 len = ( cmd->scsi.data_out.len - cmd->offset );
213 if ( len > USBBLK_MAX_LEN )
215 assert ( ( len % usbblk->out.mtu ) == 0 );
216
217 /* Allocate I/O buffer */
218 iobuf = alloc_iob ( len );
219 if ( ! iobuf ) {
220 rc = -ENOMEM;
221 goto err_alloc;
222 }
223
224 /* Populate I/O buffer */
225 if ( ( rc = xferbuf_read ( &cmd->scsi.data_out, cmd->offset,
226 iob_put ( iobuf, len ), len ) ) != 0 )
227 goto err_read;
228
229 /* Send data */
230 if ( ( rc = usb_stream ( &usbblk->out, iobuf, 0 ) ) != 0 ) {
231 DBGC ( usbblk, "USBBLK %s bulk OUT could not send data: %s\n",
232 usbblk->func->name, strerror ( rc ) );
233 goto err_stream;
234 }
235
236 /* Consume data */
237 cmd->offset += len;
238
239 return 0;
240
241 err_stream:
242 err_read:
243 free_iob ( iobuf );
244 err_alloc:
245 return rc;
246}
247
248/**
249 * Refill bulk OUT endpoint
250 *
251 * @v usbblk USB block device
252 * @ret rc Return status code
253 */
254static int usbblk_out_refill ( struct usbblk_device *usbblk ) {
255 struct usbblk_command *cmd = &usbblk->cmd;
256 int rc;
257
258 /* Sanity checks */
259 assert ( cmd->tag );
260
261 /* Refill endpoint */
262 while ( ( cmd->offset < cmd->scsi.data_out.len ) &&
263 ( usbblk->out.fill < USBBLK_MAX_FILL ) ) {
264 if ( ( rc = usbblk_out_data ( usbblk ) ) != 0 )
265 return rc;
266 }
267
268 return 0;
269}
270
271/**
272 * Complete bulk OUT transfer
273 *
274 * @v ep USB endpoint
275 * @v iobuf I/O buffer
276 * @v rc Completion status code
277 */
278static void usbblk_out_complete ( struct usb_endpoint *ep,
279 struct io_buffer *iobuf, int rc ) {
280 struct usbblk_device *usbblk =
281 container_of ( ep, struct usbblk_device, out );
282 struct usbblk_command *cmd = &usbblk->cmd;
283
284 /* Ignore cancellations after closing endpoint */
285 if ( ! ep->open )
286 goto drop;
287
288 /* Sanity check */
289 assert ( cmd->tag );
290
291 /* Check for failures */
292 if ( rc != 0 ) {
293 DBGC ( usbblk, "USBBLK %s bulk OUT failed: %s\n",
294 usbblk->func->name, strerror ( rc ) );
295 goto err;
296 }
297
298 /* Trigger refill process, if applicable */
299 if ( cmd->offset < cmd->scsi.data_out.len )
300 process_add ( &usbblk->process );
301
302 drop:
303 /* Free I/O buffer */
304 free_iob ( iobuf );
305
306 return;
307
308 err:
309 free_iob ( iobuf );
310 usbblk_stop ( usbblk, rc );
311}
312
313/** Bulk OUT endpoint operations */
317
318/******************************************************************************
319 *
320 * Bulk IN endpoint
321 *
322 ******************************************************************************
323 */
324
325/**
326 * Handle bulk IN data block
327 *
328 * @v usbblk USB block device
329 * @v data Data block
330 * @v len Length of data
331 * @ret rc Return status code
332 */
333static int usbblk_in_data ( struct usbblk_device *usbblk, const void *data,
334 size_t len ) {
335 struct usbblk_command *cmd = &usbblk->cmd;
336 int rc;
337
338 /* Sanity checks */
339 assert ( cmd->tag );
340 assert ( cmd->scsi.data_in.len != 0 );
341 assert ( cmd->offset <= cmd->scsi.data_in.len );
342 assert ( len <= ( cmd->scsi.data_in.len - cmd->offset ) );
343
344 /* Store data */
345 if ( ( rc = xferbuf_write ( &cmd->scsi.data_in, cmd->offset,
346 data, len ) ) != 0 )
347 return rc;
348 cmd->offset += len;
349
350 return 0;
351}
352
353/**
354 * Handle bulk IN status
355 *
356 * @v usbblk USB block device
357 * @v data Status data
358 * @v len Length of status data
359 * @ret rc Return status code
360 */
361static int usbblk_in_status ( struct usbblk_device *usbblk, const void *data,
362 size_t len ) {
363 struct usbblk_command *cmd = &usbblk->cmd;
364 const struct usbblk_status_wrapper *stat;
365
366 /* Sanity checks */
367 assert ( cmd->tag );
368
369 /* Validate length */
370 if ( len < sizeof ( *stat ) ) {
371 DBGC ( usbblk, "USBBLK %s bulk IN malformed status:\n",
372 usbblk->func->name );
373 DBGC_HDA ( usbblk, 0, data, len );
374 return -EIO;
375 }
376 stat = data;
377
378 /* Validate signature */
379 if ( stat->signature != cpu_to_le32 ( USBBLK_STATUS_SIGNATURE ) ) {
380 DBGC ( usbblk, "USBBLK %s bulk IN invalid signature %08x:\n",
381 usbblk->func->name, le32_to_cpu ( stat->signature ) );
382 DBGC_HDA ( usbblk, 0, stat, sizeof ( *stat ) );
383 return -EIO;
384 }
385
386 /* Validate tag */
387 if ( stat->tag != cmd->tag ) {
388 DBGC ( usbblk, "USBBLK %s bulk IN tag mismatch (got %08x, "
389 "expected %08x):\n",
390 usbblk->func->name, stat->tag, cmd->tag );
391 DBGC_HDA ( usbblk, 0, stat, sizeof ( *stat ) );
392 return -EIO;
393 }
394
395 /* Check status */
396 if ( stat->status ) {
397 DBGC ( usbblk, "USBBLK %s bulk IN status %02x:\n",
398 usbblk->func->name, stat->status );
399 DBGC_HDA ( usbblk, 0, stat, sizeof ( *stat ) );
400 return -EIO;
401 }
402
403 /* Check for residual data */
404 if ( stat->residue ) {
405 DBGC ( usbblk, "USBBLK %s bulk IN residue %#x:\n",
406 usbblk->func->name, le32_to_cpu ( stat->residue ) );
407 return -EIO;
408 }
409
410 /* Mark command as complete */
411 usbblk_stop ( usbblk, 0 );
412
413 return 0;
414}
415
416/**
417 * Refill bulk IN endpoint
418 *
419 * @v usbblk USB block device
420 * @ret rc Return status code
421 */
422static int usbblk_in_refill ( struct usbblk_device *usbblk ) {
423 struct usbblk_command *cmd = &usbblk->cmd;
425 size_t remaining;
426 unsigned int max;
427 int rc;
428
429 /* Sanity checks */
430 assert ( cmd->tag );
431
432 /* Calculate maximum required refill */
433 remaining = sizeof ( *stat );
434 if ( cmd->scsi.data_in.len ) {
435 assert ( cmd->offset <= cmd->scsi.data_in.len );
436 remaining += ( cmd->scsi.data_in.len - cmd->offset );
437 }
438 max = ( ( remaining + USBBLK_MAX_LEN - 1 ) / USBBLK_MAX_LEN );
439
440 /* Refill bulk IN endpoint */
441 if ( ( rc = usb_refill_limit ( &usbblk->in, max ) ) != 0 )
442 return rc;
443
444 return 0;
445}
446
447/**
448 * Complete bulk IN transfer
449 *
450 * @v ep USB endpoint
451 * @v iobuf I/O buffer
452 * @v rc Completion status code
453 */
454static void usbblk_in_complete ( struct usb_endpoint *ep,
455 struct io_buffer *iobuf, int rc ) {
456 struct usbblk_device *usbblk =
457 container_of ( ep, struct usbblk_device, in );
458 struct usbblk_command *cmd = &usbblk->cmd;
459 size_t remaining;
460 size_t len;
461
462 /* Ignore cancellations after closing endpoint */
463 if ( ! ep->open )
464 goto drop;
465
466 /* Sanity check */
467 assert ( cmd->tag );
468
469 /* Handle errors */
470 if ( rc != 0 ) {
471 DBGC ( usbblk, "USBBLK %s bulk IN failed: %s\n",
472 usbblk->func->name, strerror ( rc ) );
473 goto err;
474 }
475
476 /* Trigger refill process */
477 process_add ( &usbblk->process );
478
479 /* Handle data portion, if any */
480 if ( cmd->scsi.data_in.len ) {
481 assert ( cmd->offset <= cmd->scsi.data_in.len );
482 remaining = ( cmd->scsi.data_in.len - cmd->offset );
483 len = iob_len ( iobuf );
484 if ( len > remaining )
485 len = remaining;
486 if ( len ) {
487 if ( ( rc = usbblk_in_data ( usbblk, iobuf->data,
488 len ) ) != 0 )
489 goto err;
490 iob_pull ( iobuf, len );
491 }
492 }
493
494 /* Handle status portion, if any */
495 len = iob_len ( iobuf );
496 if ( len ) {
497 if ( ( rc = usbblk_in_status ( usbblk, iobuf->data,
498 len ) ) != 0 )
499 goto err;
500 }
501
502 drop:
503 /* Free I/O buffer */
504 free_iob ( iobuf );
505
506 return;
507
508 err:
509 free_iob ( iobuf );
510 usbblk_stop ( usbblk, rc );
511}
512
513/** Bulk IN endpoint operations */
517
518/******************************************************************************
519 *
520 * Refill process
521 *
522 ******************************************************************************
523 */
524
525/**
526 * Refill endpoints
527 *
528 * @v usbblk USB block device
529 */
530static void usbblk_step ( struct usbblk_device *usbblk ) {
531
532 /* Refill bulk OUT endpoint */
533 usbblk_out_refill ( usbblk );
534
535 /* Refill bulk IN endpoint */
536 usbblk_in_refill ( usbblk );
537}
538
539/** Refill process descriptor */
542
543/******************************************************************************
544 *
545 * SCSI command management
546 *
547 ******************************************************************************
548 */
549
550/** Next command tag */
552
553/**
554 * Stop SCSI command
555 *
556 * @v usbblk USB block device
557 * @v rc Reason for stop
558 */
559static void usbblk_stop ( struct usbblk_device *usbblk, int rc ) {
560
561 /* Stop process */
562 process_del ( &usbblk->process );
563
564 /* Reset command */
565 memset ( &usbblk->cmd, 0, sizeof ( usbblk->cmd ) );
566
567 /* Close endpoints if an error occurred */
568 if ( rc != 0 ) {
569 DBGC ( usbblk, "USBBLK %s closing for error recovery\n",
570 usbblk->func->name );
571 usbblk_close ( usbblk );
572 }
573
574 /* Terminate command */
575 intf_restart ( &usbblk->data, rc );
576}
577
578/**
579 * Start new SCSI command
580 *
581 * @v usbblk USB block device
582 * @v scsicmd SCSI command
583 * @ret rc Return status code
584 */
585static int usbblk_start ( struct usbblk_device *usbblk,
586 struct scsi_cmd *scsicmd ) {
587 struct usbblk_command *cmd = &usbblk->cmd;
588 int rc;
589
590 /* Fail if command is in progress */
591 if ( cmd->tag ) {
592 rc = -EBUSY;
593 DBGC ( usbblk, "USBBLK %s cannot support multiple commands\n",
594 usbblk->func->name );
595 goto err_busy;
596 }
597
598 /* Refuse bidirectional commands */
599 if ( scsicmd->data_in.len && scsicmd->data_out.len ) {
600 rc = -EOPNOTSUPP;
601 DBGC ( usbblk, "USBBLK %s cannot support bidirectional "
602 "commands\n", usbblk->func->name );
603 goto err_bidirectional;
604 }
605
606 /* Sanity checks */
607 assert ( ! process_running ( &usbblk->process ) );
608 assert ( cmd->offset == 0 );
609
610 /* Initialise command */
611 memcpy ( &cmd->scsi, scsicmd, sizeof ( cmd->scsi ) );
612 cmd->tag = ( USBBLK_TAG_MAGIC | ++usbblk_tag );
613
614 /* Issue bulk OUT command */
615 if ( ( rc = usbblk_out_command ( usbblk ) ) != 0 )
616 goto err_command;
617
618 /* Start refill process */
619 process_add ( &usbblk->process );
620
621 return 0;
622
623 err_command:
624 memset ( &usbblk->cmd, 0, sizeof ( usbblk->cmd ) );
625 err_bidirectional:
626 err_busy:
627 return rc;
628}
629
630/******************************************************************************
631 *
632 * SCSI interfaces
633 *
634 ******************************************************************************
635 */
636
637/** SCSI data interface operations */
641
642/** SCSI data interface descriptor */
645
646/**
647 * Check SCSI command flow-control window
648 *
649 * @v usbblk USB block device
650 * @ret len Length of window
651 */
652static size_t usbblk_scsi_window ( struct usbblk_device *usbblk ) {
653 struct usbblk_command *cmd = &usbblk->cmd;
654
655 /* Allow a single command if no command is currently in progress */
656 return ( cmd->tag ? 0 : 1 );
657}
658
659/**
660 * Issue SCSI command
661 *
662 * @v usbblk USB block device
663 * @v data SCSI data interface
664 * @v scsicmd SCSI command
665 * @ret tag Command tag, or negative error
666 */
667static int usbblk_scsi_command ( struct usbblk_device *usbblk,
668 struct interface *data,
669 struct scsi_cmd *scsicmd ) {
670 struct usbblk_command *cmd = &usbblk->cmd;
671 int rc;
672
673 /* (Re)open endpoints if needed */
674 if ( ( ! usbblk->in.open ) && ( ( rc = usbblk_open ( usbblk ) ) != 0 ) )
675 goto err_open;
676
677 /* Start new command */
678 if ( ( rc = usbblk_start ( usbblk, scsicmd ) ) != 0 )
679 goto err_start;
680
681 /* Attach to parent interface and return */
682 intf_plug_plug ( &usbblk->data, data );
683 return cmd->tag;
684
685 usbblk_stop ( usbblk, rc );
686 err_start:
687 usbblk_close ( usbblk );
688 err_open:
689 return rc;
690}
691
692/**
693 * Close SCSI interface
694 *
695 * @v usbblk USB block device
696 * @v rc Reason for close
697 */
698static void usbblk_scsi_close ( struct usbblk_device *usbblk, int rc ) {
699
700 /* Restart interfaces */
701 intfs_restart ( rc, &usbblk->scsi, &usbblk->data, NULL );
702
703 /* Stop any in-progress command */
704 usbblk_stop ( usbblk, rc );
705
706 /* Close endpoints */
707 usbblk_close ( usbblk );
708
709 /* Flag as closed */
710 usbblk->opened = 0;
711}
712
713/**
714 * Describe as an EFI device path
715 *
716 * @v usbblk USB block device
717 * @ret path EFI device path, or NULL on error
718 */
721
722 return efi_usb_path ( usbblk->func );
723}
724
725/** SCSI command interface operations */
733
734/** SCSI command interface descriptor */
737
738/******************************************************************************
739 *
740 * SAN device interface
741 *
742 ******************************************************************************
743 */
744
745/**
746 * Find USB block device
747 *
748 * @v name USB block device name
749 * @ret usbblk USB block device, or NULL
750 */
751static struct usbblk_device * usbblk_find ( const char *name ) {
752 struct usbblk_device *usbblk;
753
754 /* Look for matching device */
755 list_for_each_entry ( usbblk, &usbblk_devices, list ) {
756 if ( strcmp ( usbblk->func->name, name ) == 0 )
757 return usbblk;
758 }
759
760 return NULL;
761}
762
763/**
764 * Open USB block device URI
765 *
766 * @v parent Parent interface
767 * @v uri URI
768 * @ret rc Return status code
769 */
770static int usbblk_open_uri ( struct interface *parent, struct uri *uri ) {
771 static struct scsi_lun lun;
772 struct usbblk_device *usbblk;
773 int rc;
774
775 /* Sanity check */
776 if ( ! uri->opaque )
777 return -EINVAL;
778
779 /* Find matching device */
780 usbblk = usbblk_find ( uri->opaque );
781 if ( ! usbblk )
782 return -ENOENT;
783
784 /* Fail if device is already open */
785 if ( usbblk->opened )
786 return -EBUSY;
787
788 /* Open SCSI device */
789 if ( ( rc = scsi_open ( parent, &usbblk->scsi, &lun ) ) != 0 ) {
790 DBGC ( usbblk, "USBBLK %s could not open SCSI device: %s\n",
791 usbblk->func->name, strerror ( rc ) );
792 return rc;
793 }
794
795 /* Mark as opened */
796 usbblk->opened = 1;
797
798 return 0;
799}
800
801/** USB block device URI opener */
802struct uri_opener usbblk_uri_opener __uri_opener = {
803 .scheme = "usb",
804 .open = usbblk_open_uri,
805};
806
807/******************************************************************************
808 *
809 * USB interface
810 *
811 ******************************************************************************
812 */
813
814/**
815 * Probe device
816 *
817 * @v func USB function
818 * @v config Configuration descriptor
819 * @ret rc Return status code
820 */
821static int usbblk_probe ( struct usb_function *func,
822 struct usb_configuration_descriptor *config ) {
823 struct usb_device *usb = func->usb;
824 struct usbblk_device *usbblk;
826 int rc;
827
828 /* Allocate and initialise structure */
829 usbblk = zalloc ( sizeof ( *usbblk ) );
830 if ( ! usbblk ) {
831 rc = -ENOMEM;
832 goto err_alloc;
833 }
834 usbblk->func = func;
835 usb_endpoint_init ( &usbblk->out, usb, &usbblk_out_operations );
836 usb_endpoint_init ( &usbblk->in, usb, &usbblk_in_operations );
838 intf_init ( &usbblk->scsi, &usbblk_scsi_desc, &usbblk->refcnt );
839 intf_init ( &usbblk->data, &usbblk_data_desc, &usbblk->refcnt );
841 &usbblk->refcnt );
842
843 /* Locate interface descriptor */
844 desc = usb_interface_descriptor ( config, func->interface[0], 0 );
845 if ( ! desc ) {
846 DBGC ( usbblk, "USBBLK %s missing interface descriptor\n",
847 usbblk->func->name );
848 rc = -ENOENT;
849 goto err_desc;
850 }
851
852 /* Describe endpoints */
853 if ( ( rc = usb_endpoint_described ( &usbblk->out, config, desc,
854 USB_BULK_OUT, 0 ) ) != 0 ) {
855 DBGC ( usbblk, "USBBLK %s could not describe bulk OUT: %s\n",
856 usbblk->func->name, strerror ( rc ) );
857 goto err_out;
858 }
859 if ( ( rc = usb_endpoint_described ( &usbblk->in, config, desc,
860 USB_BULK_IN, 0 ) ) != 0 ) {
861 DBGC ( usbblk, "USBBLK %s could not describe bulk IN: %s\n",
862 usbblk->func->name, strerror ( rc ) );
863 goto err_in;
864 }
865
866 /* Add to list of devices */
867 list_add_tail ( &usbblk->list, &usbblk_devices );
868
869 usb_func_set_drvdata ( func, usbblk );
870 return 0;
871
872 err_in:
873 err_out:
874 err_desc:
875 ref_put ( &usbblk->refcnt );
876 err_alloc:
877 return rc;
878}
879
880/**
881 * Remove device
882 *
883 * @v func USB function
884 */
885static void usbblk_remove ( struct usb_function *func ) {
886 struct usbblk_device *usbblk = usb_func_get_drvdata ( func );
887
888 /* Remove from list of devices */
889 list_del ( &usbblk->list );
890
891 /* Close all interfaces */
892 usbblk_scsi_close ( usbblk, -ENODEV );
893
894 /* Shut down interfaces */
895 intfs_shutdown ( -ENODEV, &usbblk->scsi, &usbblk->data, NULL );
896
897 /* Drop reference */
898 ref_put ( &usbblk->refcnt );
899}
900
901/** Mass storage class device IDs */
902static struct usb_device_id usbblk_ids[] = {
903 USB_ID ( 0xffff, 0xffff, "usbblk", "USB block device", 0 ),
904};
905
906/** Mass storage driver */
907struct usb_driver usbblk_driver __usb_driver = {
908 .ids = usbblk_ids,
909 .id_count = ( sizeof ( usbblk_ids ) / sizeof ( usbblk_ids[0] ) ),
912 .score = USB_SCORE_NORMAL,
913 .probe = usbblk_probe,
914 .remove = usbblk_remove,
915};
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
__be32 out[4]
Definition CIB_PRM.h:8
__be32 in[4]
Definition CIB_PRM.h:7
struct golan_eqe_cmd cmd
Definition CIB_PRM.h:1
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned short uint16_t
Definition stdint.h:11
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
const char * name
Definition ath9k_hw.c:1986
#define max(x, y)
Definition ath.h:41
uint32_t stat
Completion status.
Definition dwmac.h:1
ring len
Length.
Definition dwmac.h:226
uint8_t lun
Logical Unit Number.
Definition edd.h:3
EFI_DEVICE_PATH_PROTOCOL * efi_usb_path(struct usb_function *func)
Construct EFI device path for USB function.
Definition efi_path.c:726
EFI_DEVICE_PATH_PROTOCOL * efi_describe(struct interface *intf)
Describe object as an EFI device path.
Definition efi_path.c:949
EFI device paths.
uint8_t data[48]
Additional event data.
Definition ena.h:11
struct ena_llq_option desc
Descriptor counts.
Definition ena.h:9
Error codes.
#define DBGC(...)
Definition compiler.h:530
#define DBGC_HDA(...)
Definition compiler.h:531
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define ENOENT
No such file or directory.
Definition errno.h:558
#define EINVAL
Invalid argument.
Definition errno.h:472
#define EOPNOTSUPP
Operation not supported on socket.
Definition errno.h:648
#define ENOMEM
Not enough space.
Definition errno.h:578
#define EIO
Input/output error.
Definition errno.h:477
#define EBUSY
Device or resource busy.
Definition errno.h:382
#define ENODEV
No such device.
Definition errno.h:553
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
#define le32_to_cpu(value)
Definition byteswap.h:114
#define cpu_to_le32(value)
Definition byteswap.h:108
#define ntohs(value)
Definition byteswap.h:137
#define EFI_INTF_OP
Definition efi.h:381
Universal Serial Bus (USB).
#define USB_BULK_OUT
Bulk OUT endpoint (internal) type.
Definition usb.h:296
#define USB_DIR_IN
Data transfer is from device to host.
Definition usb.h:98
static void usb_endpoint_init(struct usb_endpoint *ep, struct usb_device *usb, struct usb_endpoint_driver_operations *driver)
Initialise USB endpoint.
Definition usb.h:550
#define __usb_driver
Declare a USB driver.
Definition usb.h:1463
static void usb_refill_init(struct usb_endpoint *ep, size_t reserve, size_t len, unsigned int max)
Initialise USB endpoint refill.
Definition usb.h:627
#define USB_CLASS_ID(base, subclass, protocol)
Construct USB class ID.
Definition usb.h:1411
#define USB_BULK_IN
Bulk IN endpoint (internal) type.
Definition usb.h:299
#define USB_ID(_vendor, _product, _name, _description, _data)
Definition usb.h:1383
static void usb_func_set_drvdata(struct usb_function *func, void *priv)
Set USB function driver private data.
Definition usb.h:717
@ USB_SCORE_NORMAL
Normal driver.
Definition usb.h:1475
static void * usb_func_get_drvdata(struct usb_function *func)
Get USB function driver private data.
Definition usb.h:728
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
void intfs_restart(int rc,...)
Shut down and restart multiple object interfaces.
Definition interface.c:387
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition interface.c:250
void intfs_shutdown(int rc,...)
Shut down multiple object interfaces.
Definition interface.c:327
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition interface.c:108
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition interface.c:344
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition interface.h:81
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition interface.h:204
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition interface.h:33
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition iobuf.c:153
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition iobuf.c:131
#define iob_put(iobuf, len)
Definition iobuf.h:185
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition iobuf.h:220
#define iob_pull(iobuf, len)
Definition iobuf.h:167
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition list.h:94
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432
#define list_del(list)
Delete an entry from a list.
Definition list.h:120
#define LIST_HEAD(list)
Declare a static list head.
Definition list.h:38
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:718
Data transfer interface opening.
#define __uri_opener
Register a URI opener.
Definition open.h:68
void process_del(struct process *process)
Remove process from process list.
Definition process.c:80
void process_add(struct process *process)
Add process to process list.
Definition process.c:60
static int process_running(struct process *process)
Check if process is running.
Definition process.h:176
static void process_init_stopped(struct process *process, struct process_descriptor *desc, struct refcnt *refcnt)
Initialise process without adding to process list.
Definition process.h:146
#define PROC_DESC(object_type, process, _step)
Define a process descriptor.
Definition process.h:83
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107
int scsi_open(struct interface *block, struct interface *scsi, struct scsi_lun *lun)
Open SCSI device.
Definition scsi.c:987
SCSI devices.
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition DevicePath.h:45
An object interface descriptor.
Definition interface.h:56
An object interface operation.
Definition interface.h:18
An object interface.
Definition interface.h:125
A persistent I/O buffer.
Definition iobuf.h:98
void * data
Start of data.
Definition iobuf.h:113
A process descriptor.
Definition process.h:32
A process.
Definition process.h:18
A SCSI command information unit.
Definition scsi.h:250
struct xfer_buffer data_in
Data-in buffer.
Definition scsi.h:258
struct xfer_buffer data_out
Data-out buffer.
Definition scsi.h:256
A SCSI command.
Definition scsi.c:263
A SCSI LUN.
Definition scsi.h:237
A URI opener.
Definition open.h:48
A Uniform Resource Identifier.
Definition uri.h:65
const char * opaque
Opaque part.
Definition uri.h:71
A USB configuration descriptor.
Definition usb.h:210
A USB device ID.
Definition usb.h:1371
A USB device.
Definition usb.h:733
A USB driver.
Definition usb.h:1429
USB endpoint driver operations.
Definition usb.h:499
A USB endpoint.
Definition usb.h:414
size_t mtu
Maximum transfer size.
Definition usb.h:422
unsigned int fill
Buffer fill level.
Definition usb.h:431
int open
Endpoint is open.
Definition usb.h:429
A USB function.
Definition usb.h:684
struct usb_device * usb
USB device.
Definition usb.h:688
uint8_t interface[0]
List of interface numbers.
Definition usb.h:707
const char * name
Name.
Definition usb.h:686
A USB interface descriptor.
Definition usb.h:245
Command block wrapper.
Definition usbblk.h:32
uint8_t lun
LUN.
Definition usbblk.h:42
uint8_t flags
Flags.
Definition usbblk.h:40
uint8_t cblen
Command block length.
Definition usbblk.h:44
uint32_t len
Data transfer length.
Definition usbblk.h:38
uint32_t tag
Tag.
Definition usbblk.h:36
uint8_t cb[16]
Command block.
Definition usbblk.h:46
uint32_t signature
Signature.
Definition usbblk.h:34
A USB mass storage command.
Definition usbblk.h:68
A USB mass storage device.
Definition usbblk.h:78
struct usb_endpoint in
Bulk IN endpoint.
Definition usbblk.h:89
struct process process
Command process.
Definition usbblk.h:96
struct usb_endpoint out
Bulk OUT endpoint.
Definition usbblk.h:87
struct refcnt refcnt
Reference count.
Definition usbblk.h:80
int opened
Device opened flag.
Definition usbblk.h:98
struct usb_function * func
USB function.
Definition usbblk.h:85
struct list_head list
List of devices.
Definition usbblk.h:82
struct usbblk_command cmd
Current command (if any).
Definition usbblk.h:101
struct interface scsi
SCSI command-issuing interface.
Definition usbblk.h:92
struct interface data
SCSI data interface.
Definition usbblk.h:94
Command status wrapper.
Definition usbblk.h:53
size_t len
Size of data.
Definition xferbuf.h:25
Uniform Resource Identifiers.
int usb_endpoint_open(struct usb_endpoint *ep)
Open USB endpoint.
Definition usb.c:293
void usb_endpoint_close(struct usb_endpoint *ep)
Close USB endpoint.
Definition usb.c:399
int usb_endpoint_clear_halt(struct usb_endpoint *ep)
Clear endpoint halt (if applicable).
Definition usb.c:371
int usb_endpoint_described(struct usb_endpoint *ep, struct usb_configuration_descriptor *config, struct usb_interface_descriptor *interface, unsigned int type, unsigned int index)
Describe USB endpoint from device configuration.
Definition usb.c:241
int usb_control(struct usb_device *usb, unsigned int request, unsigned int value, unsigned int index, void *data, size_t len)
Issue USB control transaction.
Definition usb.c:783
int usb_refill_limit(struct usb_endpoint *ep, unsigned int max)
Refill endpoint up to specified limit.
Definition usb.c:660
struct usb_interface_descriptor * usb_interface_descriptor(struct usb_configuration_descriptor *config, unsigned int interface, unsigned int alternate)
Locate USB interface descriptor.
Definition usb.c:143
int usb_stream(struct usb_endpoint *ep, struct io_buffer *iobuf, int terminate)
Enqueue USB stream transfer.
Definition usb.c:545
static int usbblk_probe(struct usb_function *func, struct usb_configuration_descriptor *config)
Probe device.
Definition usbblk.c:821
static void usbblk_stop(struct usbblk_device *usbblk, int rc)
Stop SCSI command.
Definition usbblk.c:559
static int usbblk_scsi_command(struct usbblk_device *usbblk, struct interface *data, struct scsi_cmd *scsicmd)
Issue SCSI command.
Definition usbblk.c:667
static int usbblk_open(struct usbblk_device *usbblk)
Open endpoints.
Definition usbblk.c:63
static uint16_t usbblk_tag
Next command tag.
Definition usbblk.c:551
static void usbblk_in_complete(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete bulk IN transfer.
Definition usbblk.c:454
static struct interface_operation usbblk_data_operations[]
SCSI data interface operations.
Definition usbblk.c:638
static void usbblk_step(struct usbblk_device *usbblk)
Refill endpoints.
Definition usbblk.c:530
static int usbblk_in_refill(struct usbblk_device *usbblk)
Refill bulk IN endpoint.
Definition usbblk.c:422
static int usbblk_in_status(struct usbblk_device *usbblk, const void *data, size_t len)
Handle bulk IN status.
Definition usbblk.c:361
static struct usb_device_id usbblk_ids[]
Mass storage class device IDs.
Definition usbblk.c:902
static struct interface_descriptor usbblk_scsi_desc
SCSI command interface descriptor.
Definition usbblk.c:735
static void usbblk_remove(struct usb_function *func)
Remove device.
Definition usbblk.c:885
static int usbblk_open_uri(struct interface *parent, struct uri *uri)
Open USB block device URI.
Definition usbblk.c:770
static struct usb_endpoint_driver_operations usbblk_in_operations
Bulk IN endpoint operations.
Definition usbblk.c:514
static size_t usbblk_scsi_window(struct usbblk_device *usbblk)
Check SCSI command flow-control window.
Definition usbblk.c:652
static void usbblk_scsi_close(struct usbblk_device *usbblk, int rc)
Close SCSI interface.
Definition usbblk.c:698
static void usbblk_out_complete(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete bulk OUT transfer.
Definition usbblk.c:278
static int usbblk_out_data(struct usbblk_device *usbblk)
Send bulk OUT data block.
Definition usbblk.c:202
static int usbblk_start(struct usbblk_device *usbblk, struct scsi_cmd *scsicmd)
Start new SCSI command.
Definition usbblk.c:585
static EFI_DEVICE_PATH_PROTOCOL * usbblk_efi_describe(struct usbblk_device *usbblk)
Describe as an EFI device path.
Definition usbblk.c:720
static int usbblk_out_command(struct usbblk_device *usbblk)
Issue bulk OUT command.
Definition usbblk.c:149
static int usbblk_out_refill(struct usbblk_device *usbblk)
Refill bulk OUT endpoint.
Definition usbblk.c:254
static struct usb_endpoint_driver_operations usbblk_out_operations
Bulk OUT endpoint operations.
Definition usbblk.c:314
static void usbblk_close(struct usbblk_device *usbblk)
Close endpoints.
Definition usbblk.c:125
static struct process_descriptor usbblk_process_desc
Refill process descriptor.
Definition usbblk.c:540
static struct interface_descriptor usbblk_data_desc
SCSI data interface descriptor.
Definition usbblk.c:643
static struct interface_operation usbblk_scsi_operations[]
SCSI command interface operations.
Definition usbblk.c:726
static int usbblk_in_data(struct usbblk_device *usbblk, const void *data, size_t len)
Handle bulk IN data block.
Definition usbblk.c:333
static struct usbblk_device * usbblk_find(const char *name)
Find USB block device.
Definition usbblk.c:751
USB mass storage driver.
#define USBBLK_COMMAND_SIGNATURE
Command block wrapper signature.
Definition usbblk.h:50
#define USBBLK_STATUS_SIGNATURE
Command status wrapper signature.
Definition usbblk.h:65
#define USBBLK_MAX_LEN
Maximum length of USB data block.
Definition usbblk.h:114
#define USBBLK_RESET
Mass storage reset command.
Definition usbblk.h:28
#define USBBLK_MAX_FILL
Maximum endpoint fill level.
Definition usbblk.h:120
#define USB_PROTOCOL_MSC_BULK
Bulk-only transport protocol.
Definition usbblk.h:25
#define USB_CLASS_MSC
Mass storage class code.
Definition usbblk.h:19
#define USB_SUBCLASS_MSC_SCSI
SCSI command set subclass code.
Definition usbblk.h:22
#define USBBLK_TAG_MAGIC
Command tag magic.
Definition usbblk.h:108
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition xfer.c:117
Data transfer interfaces.
int xferbuf_read(struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
Read from data transfer buffer.
Definition xferbuf.c:166
int xferbuf_write(struct xfer_buffer *xferbuf, size_t offset, const void *data, size_t len)
Write to data transfer buffer.
Definition xferbuf.c:133