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