source: rtems/cpukit/libblock/include/rtems/flashdisk.h @ 9b4422a2

4.115
Last change on this file since 9b4422a2 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

  • Property mode set to 100644
File size: 17.9 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup RTEMSFDisk
5 *
6 * This file defines the interface to a flash disk block device.
7 */
8
9/*
10 * Copyright (C) 2007 Chris Johns
11 *
12 * The license and distribution terms for this file may be
13 * found in the file LICENSE in this distribution or at
14 * http://www.rtems.com/license/LICENSE.
15 */
16
17#if !defined (_RTEMS_FLASHDISK_H_)
18#define _RTEMS_FLASHDISK_H_
19
20#include <stdint.h>
21#include <sys/ioctl.h>
22
23#include <rtems.h>
24
25/**
26 * @defgroup RTEMSFDisk Flash Disk Device
27 *
28 * @ingroup rtems_blkdev
29 *
30 * Flash disk driver for RTEMS provides support for block based
31 * file systems on flash devices. The driver is not a flash file
32 * system nor does it try to compete with flash file systems. It
33 * currently does not journal how-ever block sequence numbering
34 * could be added to allow recovery of a past positions if
35 * a power down occurred while being updated.
36 *
37 * This flash driver provides block device support for most flash
38 * devices. The driver has been tested on NOR type devices such
39 * as the AMLV160 or M28W160. Support for NAND type devices may
40 * require driver changes to allow speedy recover of the block
41 * mapping data and to also handle the current use of word programming.
42 * Currently the page descriptors are stored in the first few pages
43 * of each segment.
44 *
45 * The driver supports devices, segments and pages. You provide
46 * to the driver the device descriptions as a table of device
47 * descriptors. Each device descriptor contain a table of
48 * segment descriptions or segment descriptors. The driver uses
49 * this information to manage the devices.
50 *
51 * A device is made up of segments. These are also called
52 * sectors or blocks. It is the smallest erasable part of a device.
53 * A device can have differing size segments at different
54 * offsets in the device. The segment descriptors support repeating
55 * segments that are continuous in the device. The driver breaks the
56 * segments up into pages. The first pages of a segment contain
57 * the page descriptors. A page descriptor hold the page flags,
58 * a CRC for the page of data and the block number the page
59 * holds. The block can appear in any order in the devices. A
60 * page is active if it hold a current block of data. If the
61 * used bit is set the page is counted as used. A page moves
62 * from erased to active to used then back to erased. If a block
63 * is written that is already in a page, the block is written to
64 * a new page the old page is flagged as used.
65 *
66 * At initialization time each segment's page descriptors are
67 * read into memory and scanned to determine the active pages,
68 * the used pages and the bad pages. If a segment has any erased
69 * pages it is queue on the available queue. If the segment has
70 * no erased pages it is queue on the used queue.
71 *
72 * The available queue is sorted from the least number available
73 * to the most number of available pages. A segment that has just
74 * been erased will placed at the end of the queue. A segment that
75 * has only a few available pages will be used sooner and once
76 * there are no available pages it is queued on the used queue.
77 * The used queue hold segments that have no available pages and
78 * is sorted from the least number of active pages to the most
79 * number of active pages.
80 *
81 * The driver is required to compact segments. Compacting takes
82 * the segment with the most number of available pages from the
83 * available queue then takes segments with the least number of
84 * active pages from the used queue until it has enough pages
85 * to fill the empty segment. As the active pages are moved
86 * they flagged as used and once the segment has only used pages
87 * it is erased.
88 *
89 * A flash block driver like this never knows if a page is not
90 * being used by the file-system. A typical file system is not
91 * design with the idea of erasing a block on a disk once it is
92 * not being used. The file-system will normally use a flag
93 * or a location as a marker to say that part of the disk is
94 * no longer in use. This means a number of blocks could be
95 * held in active pages but are no in use by the file system.
96 * The file system may also read blocks that have never been
97 * written to disk. This complicates the driver and may make
98 * the wear, usage and erase patterns harsher than a flash
99 * file system. The driver may also suffer from problems if
100 * power is lost.
101 *
102 * There are some flash disk specific IO control request types.
103 * To use open the device and issue the ioctl() call.
104 *
105 * @code
106 *  int fd = open ("/dev/flashdisk0", O_WRONLY, 0);
107 *  if (fd < 0)
108 *  {
109 *    printf ("driver open failed: %s\n", strerror (errno));
110 *    exit (1);
111 *  }
112 *  if (ioctl (fd, RTEMS_FDISK_IOCTL_ERASE_DISK) < 0)
113 *  {
114 *    printf ("driver erase failed: %s\n", strerror (errno));
115 *    exit (1);
116 *  }
117 *  close (fd);
118 * @endcode
119 *
120 * @{
121 */
122
123/**
124 * @brief The base name of the flash disks.
125 */
126#define RTEMS_FLASHDISK_DEVICE_BASE_NAME "/dev/fdd"
127
128#define RTEMS_FDISK_IOCTL_ERASE_DISK   _IO('B', 128)
129#define RTEMS_FDISK_IOCTL_COMPACT      _IO('B', 129)
130#define RTEMS_FDISK_IOCTL_ERASE_USED   _IO('B', 130)
131#define RTEMS_FDISK_IOCTL_MONITORING   _IO('B', 131)
132#define RTEMS_FDISK_IOCTL_INFO_LEVEL   _IO('B', 132)
133#define RTEMS_FDISK_IOCTL_PRINT_STATUS _IO('B', 133)
134
135/**
136 * @brief Flash Disk Monitoring Data allows a user to obtain
137 * the current status of the disk.
138 */
139typedef struct rtems_fdisk_monitor_data
140{
141  uint32_t block_size;
142  uint32_t block_count;
143  uint32_t unavail_blocks;
144  uint32_t device_count;
145  uint32_t segment_count;
146  uint32_t page_count;
147  uint32_t blocks_used;
148  uint32_t segs_available;
149  uint32_t segs_used;
150  uint32_t segs_failed;
151  uint32_t seg_erases;
152  uint32_t pages_desc;
153  uint32_t pages_active;
154  uint32_t pages_used;
155  uint32_t pages_bad;
156  uint32_t info_level;
157} rtems_fdisk_monitor_data;
158
159/**
160 * @brief Flash Segment Descriptor holds, number of continuous segments in the
161 * device of this type, the base segment number in the device, the address
162 * offset of the base segment in the device, and the size of segment.
163 *
164 * Typically this structure is part of a table of segments in the
165 * device which is referenced in the flash disk configuration table.
166 * The reference is kept in the driver and used all the time to
167 * manage the flash device, therefore it must always exist.
168 */
169typedef struct rtems_fdisk_segment_desc
170{
171  uint16_t count;    /**< Number of segments of this type in a row. */
172  uint16_t segment;  /**< The base segment number. */
173  uint32_t offset;   /**< Address offset of base segment in device. */
174  uint32_t size;     /**< Size of the segment in bytes. */
175} rtems_fdisk_segment_desc;
176
177/**
178 * @brief Return the number of kilo-bytes.
179 */
180#define RTEMS_FDISK_KBYTES(_k) (UINT32_C(1024) * (_k))
181
182/**
183 * Forward declaration of the device descriptor.
184 */
185struct rtems_fdisk_device_desc;
186
187/**
188 * @brief Flash Low Level driver handlers.
189 *
190 * Typically this structure is part of a table of handlers in the
191 * device which is referenced in the flash disk configuration table.
192 * The reference is kept in the driver and used all the time to
193 * manage the flash device, therefore it must always exist.
194 */
195typedef struct rtems_fdisk_driver_handlers
196{
197  /**
198   * Read data from the device into the buffer. Return an errno
199   * error number if the device cannot be read. A segment descriptor
200   * can describe more than one segment in a device if the device has
201   * repeating segments. The segment number is the device segment to
202   * access and the segment descriptor must reference the segment
203   * being requested. For example the segment number must resided in
204   * the range [base, base + count).
205   *
206   * @param sd The segment descriptor.
207   * @param device The device to read data from.
208   * @param segment The segment within the device to read.
209   * @param offset The offset in the segment to read.
210   * @param buffer The buffer to read the data into.
211   * @param size The amount of data to read.
212   * @retval 0 No error.
213   * @retval EIO The read did not complete.
214   */
215  int (*read) (const rtems_fdisk_segment_desc* sd,
216               uint32_t                        device,
217               uint32_t                        segment,
218               uint32_t                        offset,
219               void*                           buffer,
220               uint32_t                        size);
221
222  /**
223   * Write data from the buffer to the device. Return an errno
224   * error number if the device cannot be written to. A segment
225   * descriptor can describe more than segment in a device if the
226   * device has repeating segments. The segment number is the device
227   * segment to access and the segment descriptor must reference
228   * the segment being requested. For example the segment number must
229   * resided in the range [base, base + count).
230   *
231   * @param sd The segment descriptor.
232   * @param device The device to write data from.
233   * @param segment The segment within the device to write to.
234   * @param offset The offset in the segment to write.
235   * @param buffer The buffer to write the data from.
236   * @param size The amount of data to write.
237   * @retval 0 No error.
238   * @retval EIO The write did not complete or verify.
239   */
240  int (*write) (const rtems_fdisk_segment_desc* sd,
241                uint32_t                        device,
242                uint32_t                        segment,
243                uint32_t                        offset,
244                const void*                     buffer,
245                uint32_t                        size);
246
247  /**
248   * Blank a segment in the device. Return an errno error number
249   * if the device cannot be read or is not blank. A segment descriptor
250   * can describe more than segment in a device if the device has
251   * repeating segments. The segment number is the device segment to
252   * access and the segment descriptor must reference the segment
253   * being requested. For example the segment number must resided in
254   * the range [base, base + count).
255   *
256   * @param sd The segment descriptor.
257   * @param device The device to read data from.
258   * @param segment The segment within the device to read.
259   * @param offset The offset in the segment to checl.
260   * @param size The amount of data to check.
261   * @retval 0 No error.
262   * @retval EIO The segment is not blank.
263   */
264  int (*blank) (const rtems_fdisk_segment_desc* sd,
265                uint32_t                        device,
266                uint32_t                        segment,
267                uint32_t                        offset,
268                uint32_t                        size);
269
270  /**
271   * Verify data in the buffer to the data in the device. Return an
272   * errno error number if the device cannot be read. A segment
273   * descriptor can describe more than segment in a device if the
274   * device has repeating segments. The segment number is the
275   * segment to access and the segment descriptor must reference
276   * the device segment being requested. For example the segment number
277   * must resided in the range [base, base + count).
278   *
279   * @param sd The segment descriptor.
280   * @param device The device to verify data in.
281   * @param segment The segment within the device to verify.
282   * @param offset The offset in the segment to verify.
283   * @param buffer The buffer to verify the data in the device with.
284   * @param size The amount of data to verify.
285   * @retval 0 No error.
286   * @retval EIO The data did not verify.
287   */
288  int (*verify) (const rtems_fdisk_segment_desc* sd,
289                 uint32_t                        device,
290                 uint32_t                        segment,
291                 uint32_t                        offset,
292                 const void*                     buffer,
293                 uint32_t                        size);
294
295  /**
296   * Erase the segment. Return an errno error number if the
297   * segment cannot be erased. A segment descriptor can describe
298   * more than segment in a device if the device has repeating
299   * segments. The segment number is the device segment to access and
300   * the segment descriptor must reference the segment being requested.
301   *
302   * @param sd The segment descriptor.
303   * @param device The device to erase the segment of.
304   * @param segment The segment within the device to erase.
305   * @retval 0 No error.
306   * @retval EIO The segment was not erased.
307   */
308  int (*erase) (const rtems_fdisk_segment_desc* sd,
309                uint32_t                        device,
310                uint32_t                        segment);
311
312  /**
313   * Erase the device. Return an errno error number if the
314   * segment cannot be erased. A segment descriptor can describe
315   * more than segment in a device if the device has repeating
316   * segments. The segment number is the segment to access and
317   * the segment descriptor must reference the segment being requested.
318   *
319   * @param sd The segment descriptor.
320   * @param device The device to erase.
321   * @retval 0 No error.
322   * @retval EIO The device was not erased.
323   */
324  int (*erase_device) (const struct rtems_fdisk_device_desc* dd,
325                       uint32_t                              device);
326
327} rtems_fdisk_driver_handlers;
328
329/**
330 * @brief Flash Device Descriptor holds the segments in a device.
331 *
332 * The placing of the segments in a device decriptor allows the low level
333 * driver to share the segment descriptors for a number of devices.
334 *
335 * Typically this structure is part of a table of segments in the
336 * device which is referenced in the flash disk configuration table.
337 * The reference is kept in the driver and used all the time to
338 * manage the flash device, therefore it must always exist.
339 */
340typedef struct rtems_fdisk_device_desc
341{
342  uint32_t                           segment_count; /**< Number of segments. */
343  const rtems_fdisk_segment_desc*    segments;      /**< Array of segments. */
344  const rtems_fdisk_driver_handlers* flash_ops;     /**< Device handlers. */
345} rtems_fdisk_device_desc;
346
347/**
348 * @brief RTEMS Flash Disk configuration table used to initialise the
349 * driver.
350 *
351 * The unavailable blocks count is the number of blocks less than the
352 * available number of blocks the file system is given. This means there
353 * will always be that number of blocks available when the file system
354 * thinks the disk is full. The compaction code needs blocks to compact
355 * with so you will never be able to have all the blocks allocated to the
356 * file system and be able to full the disk.
357 *
358 * The compacting segment count is the number of segments that are
359 * moved into a new segment. A high number will mean more segments with
360 * low active page counts and high used page counts will be moved into
361 * avaliable pages how-ever this extends the compaction time due to
362 * time it takes the erase the pages. There is no pont making this number
363 * greater than the maximum number of pages in a segment.
364 *
365 * The available compacting segment count is the level when compaction occurs
366 * when writing. If you set this to 0 then compaction will fail because
367 * there will be no segments to compact into.
368 *
369 * The info level can be 0 for off with error, and abort messages allowed.
370 * Level 1 is warning messages, level 1 is informational messages, and level 3
371 * is debugging type prints. The info level can be turned off with a compile
372 * time directive on the command line to the compiler of:
373 *
374 *     -DRTEMS_FDISK_TRACE=0
375 */
376typedef struct rtems_flashdisk_config
377{
378  uint32_t                       block_size;     /**< The block size. */
379  uint32_t                       device_count;   /**< The number of devices. */
380  const rtems_fdisk_device_desc* devices;        /**< The device descriptions. */
381  uint32_t                       flags;          /**< Set of flags to control
382                                                      driver. */
383  /**
384   * Number of blocks not available to the file system.  This number must be
385   * greater than or equal to the number of blocks in the largest segment to
386   * avoid starvation of erased blocks.
387   */
388  uint32_t                       unavail_blocks;
389
390  uint32_t                       compact_segs;   /**< Max number of segs to
391                                                      compact in one pass. */
392  /**
393   * The number of segments when compaction occurs when writing.  In case the
394   * number of segments in the available queue is less than or equal to this
395   * number the compaction process will be triggered.  The available queue
396   * contains all segments with erased blocks.
397   */
398  uint32_t                       avail_compact_segs;
399  uint32_t                       info_level;     /**< Default info level. */
400} rtems_flashdisk_config;
401
402/*
403 * Driver flags.
404 */
405
406/**
407 * Leave the erasing of used segment to the background handler.
408 */
409#define RTEMS_FDISK_BACKGROUND_ERASE (1 << 0)
410
411/**
412 * Leave the compacting of of used segment to the background handler.
413 */
414#define RTEMS_FDISK_BACKGROUND_COMPACT (1 << 1)
415
416/**
417 * Check the pages during initialisation to see which pages are
418 * valid and which are not. This could slow down initialising the
419 * disk driver.
420 */
421#define RTEMS_FDISK_CHECK_PAGES (1 << 2)
422
423/**
424 * Blank check the flash device before writing to them. This is needed if
425 * you think you have a driver or device problem.
426 */
427#define RTEMS_FDISK_BLANK_CHECK_BEFORE_WRITE (1 << 3)
428
429/**
430 * Flash disk device driver initialization. Place in a table as the
431 * initialisation entry and remainder of the entries are the
432 * RTEMS block device generic handlers.
433 *
434 * @param major Flash disk major device number.
435 * @param minor Minor device number, not applicable.
436 * @param arg Initialization argument, not applicable.
437 * @return The rtems_device_driver is actually just
438 *         rtems_status_code.
439 */
440rtems_device_driver
441rtems_fdisk_initialize (rtems_device_major_number major,
442                        rtems_device_minor_number minor,
443                        void*                     arg);
444
445/**
446 * @brief External reference to the configuration. Please supply.
447 * Support is present in confdefs.h for providing this variable.
448 */
449extern const rtems_flashdisk_config rtems_flashdisk_configuration[];
450
451/**
452 * @brief External reference to the number of configurations. Please supply.
453 * Support is present in confdefs.h for providing this variable.
454 */
455extern uint32_t rtems_flashdisk_configuration_size;
456
457/** @} */
458
459#endif
Note: See TracBrowser for help on using the repository browser.