iPXE
sanboot.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 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 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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * @file
28  *
29  * SAN booting
30  *
31  */
32 
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <assert.h>
38 #include <ipxe/xfer.h>
39 #include <ipxe/open.h>
40 #include <ipxe/timer.h>
41 #include <ipxe/process.h>
42 #include <ipxe/iso9660.h>
43 #include <ipxe/dhcp.h>
44 #include <ipxe/settings.h>
45 #include <ipxe/quiesce.h>
46 #include <ipxe/sanboot.h>
47 
48 /**
49  * Timeout for block device commands (in ticks)
50  *
51  * Underlying devices should ideally never become totally stuck.
52  * However, if they do, then the blocking SAN APIs provide no means
53  * for the caller to cancel the operation, and the machine appears to
54  * hang. Use an overall timeout for all commands to avoid this
55  * problem and bounce timeout failures to the caller.
56  */
57 #define SAN_COMMAND_TIMEOUT ( 15 * TICKS_PER_SEC )
58 
59 /**
60  * Default number of times to retry commands
61  *
62  * We may need to retry commands. For example, the underlying
63  * connection may be closed by the SAN target due to an inactivity
64  * timeout, or the SAN target may return pointless "error" messages
65  * such as "SCSI power-on occurred".
66  */
67 #define SAN_DEFAULT_RETRIES 10
68 
69 /**
70  * Delay between reopening attempts
71  *
72  * Some SAN targets will always accept connections instantly and
73  * report a temporary unavailability by e.g. failing the TEST UNIT
74  * READY command. Avoid bombarding such targets by introducing a
75  * small delay between attempts.
76  */
77 #define SAN_REOPEN_DELAY_SECS 5
78 
79 /** List of SAN devices */
81 
82 /** Number of times to retry commands */
83 static unsigned long san_retries = SAN_DEFAULT_RETRIES;
84 
85 /**
86  * Find SAN device by drive number
87  *
88  * @v drive Drive number
89  * @ret sandev SAN device, or NULL
90  */
91 struct san_device * sandev_find ( unsigned int drive ) {
92  struct san_device *sandev;
93 
94  list_for_each_entry ( sandev, &san_devices, list ) {
95  if ( sandev->drive == drive )
96  return sandev;
97  }
98  return NULL;
99 }
100 
101 /**
102  * Find next SAN device by drive number
103  *
104  * @v drive Minimum drive number
105  * @ret sandev SAN device, or NULL
106  */
107 struct san_device * sandev_next ( unsigned int drive ) {
108  struct san_device *sandev;
109 
110  list_for_each_entry ( sandev, &san_devices, list ) {
111  if ( sandev->drive >= drive )
112  return sandev;
113  }
114  return NULL;
115 }
116 
117 /**
118  * Free SAN device
119  *
120  * @v refcnt Reference count
121  */
122 static void sandev_free ( struct refcnt *refcnt ) {
123  struct san_device *sandev =
124  container_of ( refcnt, struct san_device, refcnt );
125  unsigned int i;
126 
127  assert ( ! timer_running ( &sandev->timer ) );
128  assert ( ! sandev->active );
129  assert ( list_empty ( &sandev->opened ) );
130  for ( i = 0 ; i < sandev->paths ; i++ ) {
131  uri_put ( sandev->path[i].uri );
132  assert ( sandev->path[i].desc == NULL );
133  }
134  free ( sandev );
135 }
136 
137 /**
138  * Close SAN device command
139  *
140  * @v sandev SAN device
141  * @v rc Reason for close
142  */
143 static void sandev_command_close ( struct san_device *sandev, int rc ) {
144 
145  /* Stop timer */
146  stop_timer ( &sandev->timer );
147 
148  /* Restart interface */
149  intf_restart ( &sandev->command, rc );
150 
151  /* Record command status */
152  sandev->command_rc = rc;
153 }
154 
155 /**
156  * Record SAN device capacity
157  *
158  * @v sandev SAN device
159  * @v capacity SAN device capacity
160  */
161 static void sandev_command_capacity ( struct san_device *sandev,
162  struct block_device_capacity *capacity ) {
163 
164  /* Record raw capacity information */
165  memcpy ( &sandev->capacity, capacity, sizeof ( sandev->capacity ) );
166 }
167 
168 /** SAN device command interface operations */
171  INTF_OP ( block_capacity, struct san_device *,
173 };
174 
175 /** SAN device command interface descriptor */
178 
179 /**
180  * Handle SAN device command timeout
181  *
182  * @v retry Retry timer
183  */
185  int over __unused ) {
186  struct san_device *sandev =
187  container_of ( timer, struct san_device, timer );
188 
189  sandev_command_close ( sandev, -ETIMEDOUT );
190 }
191 
192 /**
193  * Open SAN path
194  *
195  * @v sanpath SAN path
196  * @ret rc Return status code
197  */
198 static int sanpath_open ( struct san_path *sanpath ) {
199  struct san_device *sandev = sanpath->sandev;
200  int rc;
201 
202  /* Sanity check */
203  list_check_contains_entry ( sanpath, &sandev->closed, list );
204 
205  /* Open interface */
206  if ( ( rc = xfer_open_uri ( &sanpath->block, sanpath->uri ) ) != 0 ) {
207  DBGC ( sandev->drive, "SAN %#02x.%d could not (re)open URI: "
208  "%s\n", sandev->drive, sanpath->index, strerror ( rc ) );
209  return rc;
210  }
211 
212  /* Update ACPI descriptor, if applicable */
213  if ( ! ( sandev->flags & SAN_NO_DESCRIBE ) ) {
214  if ( sanpath->desc )
215  acpi_del ( sanpath->desc );
216  sanpath->desc = acpi_describe ( &sanpath->block );
217  if ( sanpath->desc )
218  acpi_add ( sanpath->desc );
219  }
220 
221  /* Start process */
222  process_add ( &sanpath->process );
223 
224  /* Mark as opened */
225  list_del ( &sanpath->list );
226  list_add_tail ( &sanpath->list, &sandev->opened );
227 
228  /* Record as in progress */
229  sanpath->path_rc = -EINPROGRESS;
230 
231  return 0;
232 }
233 
234 /**
235  * Close SAN path
236  *
237  * @v sanpath SAN path
238  * @v rc Reason for close
239  */
240 static void sanpath_close ( struct san_path *sanpath, int rc ) {
241  struct san_device *sandev = sanpath->sandev;
242 
243  /* Record status */
244  sanpath->path_rc = rc;
245 
246  /* Mark as closed */
247  list_del ( &sanpath->list );
248  list_add_tail ( &sanpath->list, &sandev->closed );
249 
250  /* Stop process */
251  process_del ( &sanpath->process );
252 
253  /* Restart interfaces, avoiding potential loops */
254  if ( sanpath == sandev->active ) {
255  intfs_restart ( rc, &sandev->command, &sanpath->block, NULL );
256  sandev->active = NULL;
257  sandev_command_close ( sandev, rc );
258  } else {
259  intf_restart ( &sanpath->block, rc );
260  }
261 }
262 
263 /**
264  * Handle closure of underlying block device interface
265  *
266  * @v sanpath SAN path
267  * @v rc Reason for close
268  */
269 static void sanpath_block_close ( struct san_path *sanpath, int rc ) {
270  struct san_device *sandev = sanpath->sandev;
271 
272  /* Any closure is an error from our point of view */
273  if ( rc == 0 )
274  rc = -ENOTCONN;
275  DBGC ( sandev->drive, "SAN %#02x.%d closed: %s\n",
276  sandev->drive, sanpath->index, strerror ( rc ) );
277 
278  /* Close path */
279  sanpath_close ( sanpath, rc );
280 }
281 
282 /**
283  * Check flow control window
284  *
285  * @v sanpath SAN path
286  */
287 static size_t sanpath_block_window ( struct san_path *sanpath __unused ) {
288 
289  /* We are never ready to receive data via this interface.
290  * This prevents objects that support both block and stream
291  * interfaces from attempting to send us stream data.
292  */
293  return 0;
294 }
295 
296 /**
297  * SAN path process
298  *
299  * @v sanpath SAN path
300  */
301 static void sanpath_step ( struct san_path *sanpath ) {
302  struct san_device *sandev = sanpath->sandev;
303 
304  /* Ignore if we are already the active device */
305  if ( sanpath == sandev->active )
306  return;
307 
308  /* Wait until path has become available */
309  if ( ! xfer_window ( &sanpath->block ) )
310  return;
311 
312  /* Record status */
313  sanpath->path_rc = 0;
314 
315  /* Mark as active path or close as applicable */
316  if ( ! sandev->active ) {
317  DBGC ( sandev->drive, "SAN %#02x.%d is active\n",
318  sandev->drive, sanpath->index );
319  sandev->active = sanpath;
320  } else {
321  DBGC ( sandev->drive, "SAN %#02x.%d is available\n",
322  sandev->drive, sanpath->index );
323  sanpath_close ( sanpath, 0 );
324  }
325 }
326 
327 /** SAN path block interface operations */
332 };
333 
334 /** SAN path block interface descriptor */
337 
338 /** SAN path process descriptor */
341 
342 /**
343  * Restart SAN device interface
344  *
345  * @v sandev SAN device
346  * @v rc Reason for restart
347  */
348 static void sandev_restart ( struct san_device *sandev, int rc ) {
349  struct san_path *sanpath;
350 
351  /* Restart all block device interfaces */
352  while ( ( sanpath = list_first_entry ( &sandev->opened,
353  struct san_path, list ) ) ) {
354  sanpath_close ( sanpath, rc );
355  }
356 
357  /* Clear active path */
358  sandev->active = NULL;
359 
360  /* Close any outstanding command */
362 }
363 
364 /**
365  * (Re)open SAN device
366  *
367  * @v sandev SAN device
368  * @ret rc Return status code
369  *
370  * This function will block until the device is available.
371  */
372 int sandev_reopen ( struct san_device *sandev ) {
373  struct san_path *sanpath;
374  int rc;
375 
376  /* Unquiesce system */
377  unquiesce();
378 
379  /* Close any outstanding command and restart interfaces */
381  assert ( sandev->active == NULL );
382  assert ( list_empty ( &sandev->opened ) );
383 
384  /* Open all paths */
385  while ( ( sanpath = list_first_entry ( &sandev->closed,
386  struct san_path, list ) ) ) {
387  if ( ( rc = sanpath_open ( sanpath ) ) != 0 )
388  goto err_open;
389  }
390 
391  /* Wait for any device to become available, or for all devices
392  * to fail.
393  */
394  while ( sandev->active == NULL ) {
395  step();
396  if ( list_empty ( &sandev->opened ) ) {
397  /* Get status of the first device to be
398  * closed. Do this on the basis that earlier
399  * errors (e.g. "invalid IQN") are probably
400  * more interesting than later errors
401  * (e.g. "TCP timeout").
402  */
403  rc = -ENODEV;
404  list_for_each_entry ( sanpath, &sandev->closed, list ) {
405  rc = sanpath->path_rc;
406  break;
407  }
408  DBGC ( sandev->drive, "SAN %#02x never became "
409  "available: %s\n", sandev->drive,
410  strerror ( rc ) );
411  goto err_none;
412  }
413  }
414 
415  assert ( ! list_empty ( &sandev->opened ) );
416  return 0;
417 
418  err_none:
419  err_open:
420  sandev_restart ( sandev, rc );
421  return rc;
422 }
423 
424 /** SAN device read/write command parameters */
426  /** SAN device read/write operation */
427  int ( * block_rw ) ( struct interface *control, struct interface *data,
428  uint64_t lba, unsigned int count, void *buffer,
429  size_t len );
430  /** Data buffer */
431  void *buffer;
432  /** Starting LBA */
434  /** Block count */
435  unsigned int count;
436 };
437 
438 /** SAN device command parameters */
440  /** Read/write command parameters */
442 };
443 
444 /**
445  * Initiate SAN device read/write command
446  *
447  * @v sandev SAN device
448  * @v params Command parameters
449  * @ret rc Return status code
450  */
451 static int sandev_command_rw ( struct san_device *sandev,
452  const union san_command_params *params ) {
453  struct san_path *sanpath = sandev->active;
454  size_t len = ( params->rw.count * sandev->capacity.blksize );
455  int rc;
456 
457  /* Sanity check */
458  assert ( sanpath != NULL );
459 
460  /* Initiate read/write command */
461  if ( ( rc = params->rw.block_rw ( &sanpath->block, &sandev->command,
462  params->rw.lba, params->rw.count,
463  params->rw.buffer, len ) ) != 0 ) {
464  DBGC ( sandev->drive, "SAN %#02x.%d could not initiate "
465  "read/write: %s\n", sandev->drive, sanpath->index,
466  strerror ( rc ) );
467  return rc;
468  }
469 
470  return 0;
471 }
472 
473 /**
474  * Initiate SAN device read capacity command
475  *
476  * @v sandev SAN device
477  * @v params Command parameters
478  * @ret rc Return status code
479  */
480 static int
482  const union san_command_params *params __unused){
483  struct san_path *sanpath = sandev->active;
484  int rc;
485 
486  /* Sanity check */
487  assert ( sanpath != NULL );
488 
489  /* Initiate read capacity command */
490  if ( ( rc = block_read_capacity ( &sanpath->block,
491  &sandev->command ) ) != 0 ) {
492  DBGC ( sandev->drive, "SAN %#02x.%d could not initiate read "
493  "capacity: %s\n", sandev->drive, sanpath->index,
494  strerror ( rc ) );
495  return rc;
496  }
497 
498  return 0;
499 }
500 
501 /**
502  * Execute a single SAN device command and wait for completion
503  *
504  * @v sandev SAN device
505  * @v command Command
506  * @v params Command parameters (if required)
507  * @ret rc Return status code
508  */
509 static int
511  int ( * command ) ( struct san_device *sandev,
512  const union san_command_params *params ),
513  const union san_command_params *params ) {
514  unsigned int retries = 0;
515  int rc;
516 
517  /* Sanity check */
518  assert ( ! timer_running ( &sandev->timer ) );
519 
520  /* Unquiesce system */
521  unquiesce();
522 
523  /* (Re)try command */
524  do {
525 
526  /* Reopen block device if applicable */
527  if ( sandev_needs_reopen ( sandev ) &&
528  ( ( rc = sandev_reopen ( sandev ) ) != 0 ) ) {
529 
530  /* Delay reopening attempts */
532 
533  /* Retry opening indefinitely for multipath devices */
534  if ( sandev->paths <= 1 )
535  retries++;
536 
537  continue;
538  }
539 
540  /* Initiate command */
541  if ( ( rc = command ( sandev, params ) ) != 0 ) {
542  retries++;
543  continue;
544  }
545 
546  /* Start expiry timer */
548 
549  /* Wait for command to complete */
550  while ( timer_running ( &sandev->timer ) )
551  step();
552 
553  /* Check command status */
554  if ( ( rc = sandev->command_rc ) != 0 ) {
555  retries++;
556  continue;
557  }
558 
559  return 0;
560 
561  } while ( retries <= san_retries );
562 
563  /* Sanity check */
564  assert ( ! timer_running ( &sandev->timer ) );
565 
566  return rc;
567 }
568 
569 /**
570  * Reset SAN device
571  *
572  * @v sandev SAN device
573  * @ret rc Return status code
574  */
575 int sandev_reset ( struct san_device *sandev ) {
576  int rc;
577 
578  DBGC ( sandev->drive, "SAN %#02x reset\n", sandev->drive );
579 
580  /* Close and reopen underlying block device */
581  if ( ( rc = sandev_reopen ( sandev ) ) != 0 )
582  return rc;
583 
584  return 0;
585 }
586 
587 /**
588  * Read from or write to SAN device
589  *
590  * @v sandev SAN device
591  * @v lba Starting logical block address
592  * @v count Number of logical blocks
593  * @v buffer Data buffer
594  * @v block_rw Block read/write method
595  * @ret rc Return status code
596  */
597 static int sandev_rw ( struct san_device *sandev, uint64_t lba,
598  unsigned int count, void *buffer,
599  int ( * block_rw ) ( struct interface *control,
600  struct interface *data,
601  uint64_t lba, unsigned int count,
602  void *buffer, size_t len ) ) {
603  union san_command_params params;
604  unsigned int remaining;
605  size_t frag_len;
606  int rc;
607 
608  /* Initialise command parameters */
609  params.rw.block_rw = block_rw;
610  params.rw.buffer = buffer;
611  params.rw.lba = ( lba << sandev->blksize_shift );
612  params.rw.count = sandev->capacity.max_count;
613  remaining = ( count << sandev->blksize_shift );
614 
615  /* Read/write fragments */
616  while ( remaining ) {
617 
618  /* Determine fragment length */
619  if ( params.rw.count > remaining )
620  params.rw.count = remaining;
621 
622  /* Execute command */
623  if ( ( rc = sandev_command ( sandev, sandev_command_rw,
624  &params ) ) != 0 )
625  return rc;
626 
627  /* Move to next fragment */
628  frag_len = ( sandev->capacity.blksize * params.rw.count );
629  params.rw.buffer += frag_len;
630  params.rw.lba += params.rw.count;
631  remaining -= params.rw.count;
632  }
633 
634  return 0;
635 }
636 
637 /**
638  * Read from SAN device
639  *
640  * @v sandev SAN device
641  * @v lba Starting logical block address
642  * @v count Number of logical blocks
643  * @v buffer Data buffer
644  * @ret rc Return status code
645  */
646 int sandev_read ( struct san_device *sandev, uint64_t lba,
647  unsigned int count, void *buffer ) {
648  int rc;
649 
650  /* Read from device */
651  if ( ( rc = sandev_rw ( sandev, lba, count, buffer,
652  block_read ) ) != 0 )
653  return rc;
654 
655  return 0;
656 }
657 
658 /**
659  * Write to SAN device
660  *
661  * @v sandev SAN device
662  * @v lba Starting logical block address
663  * @v count Number of logical blocks
664  * @v buffer Data buffer
665  * @ret rc Return status code
666  */
667 int sandev_write ( struct san_device *sandev, uint64_t lba,
668  unsigned int count, void *buffer ) {
669  int rc;
670 
671  /* Write to device */
672  if ( ( rc = sandev_rw ( sandev, lba, count, buffer,
673  block_write ) ) != 0 )
674  return rc;
675 
676  /* Quiesce system. This is a heuristic designed to ensure
677  * that the system is quiesced before Windows starts up, since
678  * a Windows SAN boot will typically write a status flag to
679  * the disk as its last action before transferring control to
680  * the native drivers.
681  */
682  quiesce();
683 
684  return 0;
685 }
686 
687 /**
688  * Describe SAN device
689  *
690  * @v sandev SAN device
691  * @ret rc Return status code
692  *
693  * Allow connections to progress until all existent path descriptors
694  * are complete.
695  */
696 static int sandev_describe ( struct san_device *sandev ) {
697  struct san_path *sanpath;
698  struct acpi_descriptor *desc;
699  int rc;
700 
701  /* Wait for all paths to be either described or closed */
702  while ( 1 ) {
703 
704  /* Allow connections to progress */
705  step();
706 
707  /* Fail if any closed path has an incomplete descriptor */
708  list_for_each_entry ( sanpath, &sandev->closed, list ) {
709  desc = sanpath->desc;
710  if ( ! desc )
711  continue;
712  if ( ( rc = desc->model->complete ( desc ) ) != 0 ) {
713  DBGC ( sandev->drive, "SAN %#02x.%d could not "
714  "be described: %s\n", sandev->drive,
715  sanpath->index, strerror ( rc ) );
716  return rc;
717  }
718  }
719 
720  /* Succeed if no paths have an incomplete descriptor */
721  rc = 0;
722  list_for_each_entry ( sanpath, &sandev->opened, list ) {
723  desc = sanpath->desc;
724  if ( ! desc )
725  continue;
726  if ( ( rc = desc->model->complete ( desc ) ) != 0 )
727  break;
728  }
729  if ( rc == 0 )
730  return 0;
731  }
732 }
733 
734 /**
735  * Remove SAN device descriptors
736  *
737  * @v sandev SAN device
738  */
739 static void sandev_undescribe ( struct san_device *sandev ) {
740  struct san_path *sanpath;
741  unsigned int i;
742 
743  /* Remove all ACPI descriptors */
744  for ( i = 0 ; i < sandev->paths ; i++ ) {
745  sanpath = &sandev->path[i];
746  if ( sanpath->desc ) {
747  acpi_del ( sanpath->desc );
748  sanpath->desc = NULL;
749  }
750  }
751 }
752 
753 /**
754  * Configure SAN device as a CD-ROM, if applicable
755  *
756  * @v sandev SAN device
757  * @ret rc Return status code
758  *
759  * Both BIOS and UEFI require SAN devices to be accessed with a block
760  * size of 2048. While we could require the user to configure the
761  * block size appropriately, this is non-trivial and would impose a
762  * substantial learning effort on the user. Instead, we check for the
763  * presence of the ISO9660 primary volume descriptor and, if found,
764  * then we force a block size of 2048 and map read/write requests
765  * appropriately.
766  */
767 static int sandev_parse_iso9660 ( struct san_device *sandev ) {
768  static const struct iso9660_primary_descriptor_fixed primary_check = {
770  .id = ISO9660_ID,
771  };
772  union {
774  char bytes[ISO9660_BLKSIZE];
775  } *scratch;
776  unsigned int blksize;
777  unsigned int blksize_shift;
778  unsigned int lba;
779  unsigned int count;
780  int rc;
781 
782  /* Calculate required blocksize shift for potential CD-ROM access */
783  blksize = sandev->capacity.blksize;
784  blksize_shift = 0;
785  while ( blksize < ISO9660_BLKSIZE ) {
786  blksize <<= 1;
787  blksize_shift++;
788  }
789  if ( blksize > ISO9660_BLKSIZE ) {
790  /* Cannot be a CD-ROM. This is not an error. */
791  rc = 0;
792  goto invalid_blksize;
793  }
794  lba = ( ISO9660_PRIMARY_LBA << blksize_shift );
795  count = ( 1 << blksize_shift );
796 
797  /* Allocate scratch area */
798  scratch = malloc ( ISO9660_BLKSIZE );
799  if ( ! scratch ) {
800  rc = -ENOMEM;
801  goto err_alloc;
802  }
803 
804  /* Read primary volume descriptor */
805  if ( ( rc = sandev_read ( sandev, lba, count, scratch ) ) != 0 ) {
806  DBGC ( sandev->drive, "SAN %#02x could not read ISO9660 "
807  "primary volume descriptor: %s\n",
808  sandev->drive, strerror ( rc ) );
809  goto err_rw;
810  }
811 
812  /* Configure as CD-ROM if applicable */
813  if ( memcmp ( &scratch->primary.fixed, &primary_check,
814  sizeof ( primary_check ) ) == 0 ) {
815  DBGC ( sandev->drive, "SAN %#02x contains an ISO9660 "
816  "filesystem; treating as CD-ROM\n", sandev->drive );
817  sandev->blksize_shift = blksize_shift;
818  sandev->is_cdrom = 1;
819  }
820 
821  err_rw:
822  free ( scratch );
823  err_alloc:
824  invalid_blksize:
825  return rc;
826 }
827 
828 /**
829  * Allocate SAN device
830  *
831  * @v uris List of URIs
832  * @v count Number of URIs
833  * @v priv_size Size of private data
834  * @ret sandev SAN device, or NULL
835  */
836 struct san_device * alloc_sandev ( struct uri **uris, unsigned int count,
837  size_t priv_size ) {
838  struct san_device *sandev;
839  struct san_path *sanpath;
840  size_t size;
841  unsigned int i;
842 
843  /* Allocate and initialise structure */
844  size = ( sizeof ( *sandev ) + ( count * sizeof ( sandev->path[0] ) ) );
845  sandev = zalloc ( size + priv_size );
846  if ( ! sandev )
847  return NULL;
850  timer_init ( &sandev->timer, sandev_command_expired, &sandev->refcnt );
851  sandev->priv = ( ( ( void * ) sandev ) + size );
852  sandev->paths = count;
855  for ( i = 0 ; i < count ; i++ ) {
856  sanpath = &sandev->path[i];
857  sanpath->sandev = sandev;
858  sanpath->index = i;
859  sanpath->uri = uri_get ( uris[i] );
860  list_add_tail ( &sanpath->list, &sandev->closed );
861  intf_init ( &sanpath->block, &sanpath_block_desc,
862  &sandev->refcnt );
864  &sandev->refcnt );
865  sanpath->path_rc = -EINPROGRESS;
866  }
867 
868  return sandev;
869 }
870 
871 /**
872  * Register SAN device
873  *
874  * @v sandev SAN device
875  * @v drive Drive number
876  * @v flags Flags
877  * @ret rc Return status code
878  */
879 int register_sandev ( struct san_device *sandev, unsigned int drive,
880  unsigned int flags ) {
881  struct san_device *before;
882  int rc;
883 
884  /* Check that drive number is not in use */
885  if ( sandev_find ( drive ) != NULL ) {
886  DBGC ( sandev->drive, "SAN %#02x is already in use\n", drive );
887  rc = -EADDRINUSE;
888  goto err_in_use;
889  }
890 
891  /* Record drive number and flags */
892  sandev->drive = drive;
893  sandev->flags = flags;
894 
895  /* Check that device is capable of being opened (i.e. that all
896  * URIs are well-formed and that at least one path is
897  * working).
898  */
899  if ( ( rc = sandev_reopen ( sandev ) ) != 0 )
900  goto err_reopen;
901 
902  /* Describe device */
903  if ( ( rc = sandev_describe ( sandev ) ) != 0 )
904  goto err_describe;
905 
906  /* Read device capacity */
908  NULL ) ) != 0 )
909  goto err_capacity;
910 
911  /* Configure as a CD-ROM, if applicable */
912  if ( ( rc = sandev_parse_iso9660 ( sandev ) ) != 0 )
913  goto err_iso9660;
914 
915  /* Add to list of SAN devices, in drive order */
916  for_each_sandev ( before ) {
917  if ( before->drive > sandev->drive )
918  break;
919  }
920  list_add_tail ( &sandev->list, &before->list );
921  DBGC ( sandev->drive, "SAN %#02x registered\n", sandev->drive );
922 
923  return 0;
924 
925  list_del ( &sandev->list );
926  err_iso9660:
927  err_capacity:
928  err_describe:
929  err_reopen:
930  sandev_restart ( sandev, rc );
931  sandev_undescribe ( sandev );
932  err_in_use:
933  return rc;
934 }
935 
936 /**
937  * Unregister SAN device
938  *
939  * @v sandev SAN device
940  */
941 void unregister_sandev ( struct san_device *sandev ) {
942 
943  /* Sanity check */
944  assert ( ! timer_running ( &sandev->timer ) );
945 
946  /* Remove from list of SAN devices */
947  list_del ( &sandev->list );
948 
949  /* Shut down interfaces */
950  sandev_restart ( sandev, 0 );
951 
952  /* Remove ACPI descriptors */
953  sandev_undescribe ( sandev );
954 
955  DBGC ( sandev->drive, "SAN %#02x unregistered\n", sandev->drive );
956 }
957 
958 /** The "san-drive" setting */
959 const struct setting san_drive_setting __setting ( SETTING_SANBOOT_EXTRA,
960  san-drive ) = {
961  .name = "san-drive",
962  .description = "SAN drive number",
963  .tag = DHCP_EB_SAN_DRIVE,
964  .type = &setting_type_uint8,
965 };
966 
967 /**
968  * Get default SAN drive number
969  *
970  * @ret drive Default drive number
971  */
972 unsigned int san_default_drive ( void ) {
973  unsigned long drive;
974 
975  /* Use "san-drive" setting, if specified */
976  if ( fetch_uint_setting ( NULL, &san_drive_setting, &drive ) >= 0 )
977  return drive;
978 
979  /* Otherwise, default to booting from first hard disk */
980  return SAN_DEFAULT_DRIVE;
981 }
982 
983 /** The "san-retries" setting */
984 const struct setting san_retries_setting __setting ( SETTING_SANBOOT_EXTRA,
985  san-retries ) = {
986  .name = "san-retries",
987  .description = "SAN retry count",
988  .tag = DHCP_EB_SAN_RETRY,
989  .type = &setting_type_int8,
990 };
991 
992 /**
993  * Apply SAN boot settings
994  *
995  * @ret rc Return status code
996  */
997 static int sandev_apply ( void ) {
998 
999  /* Apply "san-retries" setting */
1000  if ( fetch_uint_setting ( NULL, &san_retries_setting,
1001  &san_retries ) < 0 ) {
1003  }
1004 
1005  return 0;
1006 }
1007 
1008 /** Settings applicator */
1009 struct settings_applicator sandev_applicator __settings_applicator = {
1010  .apply = sandev_apply,
1011 };
An ISO9660 Primary Volume Descriptor.
Definition: iso9660.h:27
A process.
Definition: process.h:17
An object interface operation.
Definition: interface.h:17
#define ECONNRESET
Connection reset.
Definition: errno.h:363
int sandev_reset(struct san_device *sandev)
Reset SAN device.
Definition: sanboot.c:575
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void xfer_window_changed(struct interface *intf)
Report change of flow control window.
Definition: xfer.c:146
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:249
static int sandev_command_read_capacity(struct san_device *sandev, const union san_command_params *params __unused)
Initiate SAN device read capacity command.
Definition: sanboot.c:481
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:343
static void sandev_command_capacity(struct san_device *sandev, struct block_device_capacity *capacity)
Record SAN device capacity.
Definition: sanboot.c:161
Dynamic Host Configuration Protocol.
#define SETTING_SANBOOT_EXTRA
SAN boot additional settings.
Definition: settings.h:74
unsigned int max_count
Maximum number of blocks per single transfer.
Definition: blockdev.h:23
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:205
void * buffer
Data buffer.
Definition: sanboot.c:431
SAN device command parameters.
Definition: sanboot.c:439
static struct interface_descriptor sandev_command_desc
SAN device command interface descriptor.
Definition: sanboot.c:176
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition: uri.h:194
#define DHCP_EB_SAN_RETRY
SAN retry count.
Definition: dhcp.h:465
struct san_path path[0]
SAN paths.
Definition: sanboot.h:100
static struct interface_operation sanpath_block_op[]
SAN path block interface operations.
Definition: sanboot.c:328
static void sandev_free(struct refcnt *refcnt)
Free SAN device.
Definition: sanboot.c:122
struct list_head list
List of open/closed paths.
Definition: sanboot.h:44
#define DHCP_EB_SAN_DRIVE
SAN drive number.
Definition: dhcp.h:478
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
Error codes.
A command-line command.
Definition: command.h:9
int command_rc
Command status.
Definition: sanboot.h:74
LIST_HEAD(san_devices)
List of SAN devices.
void block_capacity(struct interface *intf, struct block_device_capacity *capacity)
Report block device capacity.
Definition: blockdev.c:129
struct ib_cm_path primary
Primary path.
Definition: ib_mad.h:40
int xfer_open_uri(struct interface *intf, struct uri *uri)
Open URI.
Definition: open.c:67
uint16_t size
Buffer size.
Definition: dwmac.h:14
void unregister_sandev(struct san_device *sandev)
Unregister SAN device.
Definition: sanboot.c:941
#define EADDRINUSE
Address already in use.
Definition: errno.h:303
#define DBGC(...)
Definition: compiler.h:505
A process descriptor.
Definition: process.h:31
struct interface block
Underlying block device interface.
Definition: sanboot.h:47
A retry timer.
Definition: retry.h:21
int32_t before
Initial microcode version.
Definition: ucode.h:16
#define ISO9660_ID
ISO9660 identifier.
Definition: iso9660.h:42
unsigned long long uint64_t
Definition: stdint.h:13
unsigned int san_default_drive(void)
Get default SAN drive number.
Definition: sanboot.c:972
struct list_head san_devices
A settings applicator.
Definition: settings.h:251
iPXE timers
static int sandev_needs_reopen(struct san_device *sandev)
Check if SAN device needs to be reopened.
Definition: sanboot.h:255
uint32_t buffer
Buffer index (or NETVSC_RNDIS_NO_BUFFER)
Definition: netvsc.h:16
#define PROC_DESC_ONCE(object_type, process, _step)
Define a process descriptor for a process that runs only once.
Definition: process.h:97
struct settings_applicator sandev_applicator __settings_applicator
Settings applicator.
Definition: sanboot.c:1009
uint8_t drive
Drive number.
Definition: int13.h:16
struct list_head opened
List of opened SAN paths.
Definition: sanboot.h:96
void process_del(struct process *process)
Remove process from process list.
Definition: process.c:79
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition: xfer.c:116
void intfs_restart(int rc,...)
Shut down and restart multiple object interfaces.
Definition: interface.c:386
unsigned int index
Path index.
Definition: sanboot.h:40
struct interface command
Command interface.
Definition: sanboot.h:70
Data transfer interfaces.
A reference counter.
Definition: refcnt.h:26
A timer.
Definition: timer.h:28
const char * name
Name.
Definition: settings.h:28
void acpi_del(struct acpi_descriptor *desc)
Remove ACPI descriptor.
Definition: acpi.c:306
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition: list.h:333
#define ISO9660_TYPE_PRIMARY
ISO9660 Primary Volume Descriptor type.
Definition: iso9660.h:33
static void sandev_undescribe(struct san_device *sandev)
Remove SAN device descriptors.
Definition: sanboot.c:739
struct san_command_rw_params rw
Read/write command parameters.
Definition: sanboot.c:441
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Quiesce system.
int block_write(struct interface *control, struct interface *data, uint64_t lba, unsigned int count, void *buffer, size_t len)
Write to block device.
Definition: blockdev.c:78
#define SAN_REOPEN_DELAY_SECS
Delay between reopening attempts.
Definition: sanboot.c:77
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An object interface.
Definition: interface.h:124
void quiesce(void)
Quiesce system.
Definition: quiesce.c:36
A SAN path.
Definition: sanboot.h:36
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
An ISO9660 Primary Volume Descriptor (fixed portion)
Definition: iso9660.h:19
struct list_head list
List of ACPI descriptors for this model.
Definition: acpi.h:300
unsigned int drive
Drive number.
Definition: sanboot.h:65
ring len
Length.
Definition: dwmac.h:231
int register_sandev(struct san_device *sandev, unsigned int drive, unsigned int flags)
Register SAN device.
Definition: sanboot.c:879
static unsigned int count
Number of entries.
Definition: dwmac.h:225
static int sandev_describe(struct san_device *sandev)
Describe SAN device.
Definition: sanboot.c:696
unsigned int blksize_shift
Block size shift.
Definition: sanboot.h:84
ISO9660 CD-ROM specification.
static struct interface_descriptor sanpath_block_desc
SAN path block interface descriptor.
Definition: sanboot.c:335
#define EINPROGRESS
Operation in progress.
Definition: errno.h:418
void process_add(struct process *process)
Add process to process list.
Definition: process.c:59
#define ENOTCONN
The socket is not connected.
Definition: errno.h:569
Configuration settings.
static void sanpath_block_close(struct san_path *sanpath, int rc)
Handle closure of underlying block device interface.
Definition: sanboot.c:269
An object interface descriptor.
Definition: interface.h:55
uint8_t flags
Flags.
Definition: ena.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct list_head closed
List of closed SAN paths.
Definition: sanboot.h:98
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
int is_cdrom
Drive is a CD-ROM.
Definition: sanboot.h:86
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:661
static size_t sanpath_block_window(struct san_path *sanpath __unused)
Check flow control window.
Definition: sanboot.c:287
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
uint32_t control
Control.
Definition: myson.h:14
struct list_head list
List of SAN devices.
Definition: sanboot.h:62
int path_rc
Path status.
Definition: sanboot.h:51
#define ENODEV
No such device.
Definition: errno.h:509
SAN device read/write command parameters.
Definition: sanboot.c:425
A SAN device.
Definition: sanboot.h:58
uint64_t lba
Starting block number.
Definition: int13.h:22
Processes.
Data transfer interface opening.
Device should not be included in description tables.
Definition: sanboot.h:106
#define for_each_sandev(sandev)
Iterate over all SAN devices.
Definition: sanboot.h:196
int fetch_uint_setting(struct settings *settings, const struct setting *setting, unsigned long *value)
Fetch value of unsigned integer setting.
Definition: settings.c:1039
struct san_device * sandev_next(unsigned int drive)
Find next SAN device by drive number.
Definition: sanboot.c:107
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:145
static int sandev_rw(struct san_device *sandev, uint64_t lba, unsigned int count, void *buffer, int(*block_rw)(struct interface *control, struct interface *data, uint64_t lba, unsigned int count, void *buffer, size_t len))
Read from or write to SAN device.
Definition: sanboot.c:597
struct acpi_descriptor * acpi_describe(struct interface *intf)
Get object's ACPI descriptor.
Definition: acpi.c:320
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:620
int(* block_rw)(struct interface *control, struct interface *data, uint64_t lba, unsigned int count, void *buffer, size_t len)
SAN device read/write operation.
Definition: sanboot.c:427
unsigned int paths
Number of paths.
Definition: sanboot.h:92
A setting.
Definition: settings.h:23
static void sandev_command_expired(struct retry_timer *timer, int over __unused)
Handle SAN device command timeout.
Definition: sanboot.c:184
static int sandev_parse_iso9660(struct san_device *sandev)
Configure SAN device as a CD-ROM, if applicable.
Definition: sanboot.c:767
void start_timer_fixed(struct retry_timer *timer, unsigned long timeout)
Start timer with a specified timeout.
Definition: retry.c:64
static unsigned long san_retries
Number of times to retry commands.
Definition: sanboot.c:83
#define SAN_COMMAND_TIMEOUT
Timeout for block device commands (in ticks)
Definition: sanboot.c:57
unsigned int count
Block count.
Definition: sanboot.c:435
struct uri * uri
SAN device URI.
Definition: sanboot.h:42
struct block_device_capacity capacity
Raw block device capacity.
Definition: sanboot.h:77
uint8_t type
Descriptor type.
Definition: iso9660.h:21
static void sanpath_close(struct san_path *sanpath, int rc)
Close SAN path.
Definition: sanboot.c:240
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:80
An ACPI descriptor (used to construct ACPI tables)
Definition: acpi.h:294
void stop_timer(struct retry_timer *timer)
Stop timer.
Definition: retry.c:117
const struct setting san_drive_setting __setting(SETTING_SANBOOT_EXTRA, san-drive)
The "san-drive" setting.
struct process process
Process.
Definition: sanboot.h:49
Block device capacity.
Definition: blockdev.h:17
static int sandev_command(struct san_device *sandev, int(*command)(struct san_device *sandev, const union san_command_params *params), const union san_command_params *params)
Execute a single SAN device command and wait for completion.
Definition: sanboot.c:510
struct refcnt refcnt
Reference count.
Definition: sanboot.h:60
struct san_device * sandev_find(unsigned int drive)
Find SAN device by drive number.
Definition: sanboot.c:91
uint8_t block[3][8]
DES-encrypted blocks.
Definition: mschapv2.h:12
void * priv
Driver private data.
Definition: sanboot.h:89
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static struct interface_operation sandev_command_op[]
SAN device command interface operations.
Definition: sanboot.c:169
void step(void)
Single-step a single process.
Definition: process.c:98
static struct process_descriptor sanpath_process_desc
SAN path process descriptor.
Definition: sanboot.c:339
#define ISO9660_BLKSIZE
ISO9660 block size.
Definition: iso9660.h:16
uint64_t lba
Starting LBA.
Definition: sanboot.c:433
void sleep_fixed(unsigned int secs)
Sleep (uninterruptibly) for a fixed number of seconds.
Definition: timer.c:143
uint8_t data[48]
Additional event data.
Definition: ena.h:22
int block_read(struct interface *control, struct interface *data, uint64_t lba, unsigned int count, void *buffer, size_t len)
Read from block device.
Definition: blockdev.c:47
static void sandev_restart(struct san_device *sandev, int rc)
Restart SAN device interface.
Definition: sanboot.c:348
int(* apply)(void)
Apply updated settings.
Definition: settings.h:256
#define ISO9660_PRIMARY_LBA
ISO9660 Primary Volume Descriptor block address.
Definition: iso9660.h:36
A Uniform Resource Identifier.
Definition: uri.h:64
static void sandev_command_close(struct san_device *sandev, int rc)
Close SAN device command.
Definition: sanboot.c:143
uint32_t blksize
Cipher block size.
Definition: pccrr.h:14
struct san_path * active
Current active path.
Definition: sanboot.h:94
#define SAN_DEFAULT_DRIVE
Default SAN drive number.
Definition: sanboot.h:33
#define SAN_DEFAULT_RETRIES
Default number of times to retry commands.
Definition: sanboot.c:67
unsigned int flags
Flags.
Definition: sanboot.h:67
int sandev_read(struct san_device *sandev, uint64_t lba, unsigned int count, void *buffer)
Read from SAN device.
Definition: sanboot.c:646
int block_read_capacity(struct interface *control, struct interface *data)
Read block device capacity.
Definition: blockdev.c:105
struct retry_timer timer
Command timeout timer.
Definition: sanboot.h:72
static int sandev_command_rw(struct san_device *sandev, const union san_command_params *params)
Initiate SAN device read/write command.
Definition: sanboot.c:451
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:203
static int sandev_apply(void)
Apply SAN boot settings.
Definition: sanboot.c:997
iPXE sanboot API
uint8_t bytes[64]
Definition: ib_mad.h:16
#define list_check_contains_entry(entry, head, member)
Check list contains a specified entry.
Definition: list.h:549
static int sanpath_open(struct san_path *sanpath)
Open SAN path.
Definition: sanboot.c:198
void unquiesce(void)
Unquiesce system.
Definition: quiesce.c:46
struct acpi_descriptor * desc
ACPI descriptor (if applicable)
Definition: sanboot.h:54
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
void acpi_add(struct acpi_descriptor *desc)
Add ACPI descriptor.
Definition: acpi.c:294
int sandev_reopen(struct san_device *sandev)
(Re)open SAN device
Definition: sanboot.c:372
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
struct san_device * sandev
Containing SAN device.
Definition: sanboot.h:38
String functions.
static void sanpath_step(struct san_path *sanpath)
SAN path process.
Definition: sanboot.c:301
struct san_device * alloc_sandev(struct uri **uris, unsigned int count, size_t priv_size)
Allocate SAN device.
Definition: sanboot.c:836
size_t blksize
Block size.
Definition: blockdev.h:21
int sandev_write(struct san_device *sandev, uint64_t lba, unsigned int count, void *buffer)
Write to SAN device.
Definition: sanboot.c:667