Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / block / ata.c
1 /*
2  * Copyright (C) 2006 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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <errno.h>
27 #include <byteswap.h>
28 #include <ipxe/list.h>
29 #include <ipxe/interface.h>
30 #include <ipxe/blockdev.h>
31 #include <ipxe/edd.h>
32 #include <ipxe/ata.h>
33
34 /** @file
35  *
36  * ATA block device
37  *
38  */
39
40 /******************************************************************************
41  *
42  * Interface methods
43  *
44  ******************************************************************************
45  */
46
47 /**
48  * Issue ATA command
49  *
50  * @v control           ATA control interface
51  * @v data              ATA data interface
52  * @v command           ATA command
53  * @ret tag             Command tag, or negative error
54  */
55 int ata_command ( struct interface *control, struct interface *data,
56                   struct ata_cmd *command ) {
57         struct interface *dest;
58         ata_command_TYPE ( void * ) *op =
59                 intf_get_dest_op ( control, ata_command, &dest );
60         void *object = intf_object ( dest );
61         int tag;
62
63         if ( op ) {
64                 tag = op ( object, data, command );
65         } else {
66                 /* Default is to fail to issue the command */
67                 tag = -EOPNOTSUPP;
68         }
69
70         intf_put ( dest );
71         return tag;
72 }
73
74 /******************************************************************************
75  *
76  * ATA devices and commands
77  *
78  ******************************************************************************
79  */
80
81 /** List of all ATA commands */
82 static LIST_HEAD ( ata_commands );
83
84 /** An ATA device */
85 struct ata_device {
86         /** Reference count */
87         struct refcnt refcnt;
88         /** Block control interface */
89         struct interface block;
90         /** ATA control interface */
91         struct interface ata;
92
93         /** Device number
94          *
95          * Must be ATA_DEV_MASTER or ATA_DEV_SLAVE.
96          */
97         unsigned int device;
98         /** Maximum number of blocks per single transfer */
99         unsigned int max_count;
100         /** Device uses LBA48 extended addressing */
101         int lba48;
102 };
103
104 /** An ATA command */
105 struct ata_command {
106         /** Reference count */
107         struct refcnt refcnt;
108         /** ATA device */
109         struct ata_device *atadev;
110         /** List of ATA commands */
111         struct list_head list;
112
113         /** Block data interface */
114         struct interface block;
115         /** ATA data interface */
116         struct interface ata;
117
118         /** Command type */
119         struct ata_command_type *type;
120         /** Command tag */
121         uint32_t tag;
122
123         /** Private data */
124         uint8_t priv[0];
125 };
126
127 /** An ATA command type */
128 struct ata_command_type {
129         /** Name */
130         const char *name;
131         /** Additional working space */
132         size_t priv_len;
133         /** Command for non-LBA48-capable devices */
134         uint8_t cmd_lba;
135         /** Command for LBA48-capable devices */
136         uint8_t cmd_lba48;
137         /**
138          * Calculate data-in buffer
139          *
140          * @v atacmd            ATA command
141          * @v buffer            Available buffer
142          * @v len               Available buffer length
143          * @ret data_in         Data-in buffer
144          * @ret data_in_len     Data-in buffer length
145          */
146         void ( * data_in ) ( struct ata_command *atacmd, userptr_t buffer,
147                              size_t len, userptr_t *data_in,
148                              size_t *data_in_len );
149         /**
150          * Calculate data-out buffer
151          *
152          *
153          * @v atacmd            ATA command
154          * @v buffer            Available buffer
155          * @v len               Available buffer length
156          * @ret data_out        Data-out buffer
157          * @ret data_out_len    Data-out buffer length
158          */
159         void ( * data_out ) ( struct ata_command *atacmd, userptr_t buffer,
160                               size_t len, userptr_t *data_out,
161                               size_t *data_out_len );
162         /**
163          * Handle ATA command completion
164          *
165          * @v atacmd            ATA command
166          * @v rc                Reason for completion
167          */
168         void ( * done ) ( struct ata_command *atacmd, int rc );
169 };
170
171 /**
172  * Get reference to ATA device
173  *
174  * @v atadev            ATA device
175  * @ret atadev          ATA device
176  */
177 static inline __attribute__ (( always_inline )) struct ata_device *
178 atadev_get ( struct ata_device *atadev ) {
179         ref_get ( &atadev->refcnt );
180         return atadev;
181 }
182
183 /**
184  * Drop reference to ATA device
185  *
186  * @v atadev            ATA device
187  */
188 static inline __attribute__ (( always_inline )) void
189 atadev_put ( struct ata_device *atadev ) {
190         ref_put ( &atadev->refcnt );
191 }
192
193 /**
194  * Get reference to ATA command
195  *
196  * @v atacmd            ATA command
197  * @ret atacmd          ATA command
198  */
199 static inline __attribute__ (( always_inline )) struct ata_command *
200 atacmd_get ( struct ata_command *atacmd ) {
201         ref_get ( &atacmd->refcnt );
202         return atacmd;
203 }
204
205 /**
206  * Drop reference to ATA command
207  *
208  * @v atacmd            ATA command
209  */
210 static inline __attribute__ (( always_inline )) void
211 atacmd_put ( struct ata_command *atacmd ) {
212         ref_put ( &atacmd->refcnt );
213 }
214
215 /**
216  * Get ATA command private data
217  *
218  * @v atacmd            ATA command
219  * @ret priv            Private data
220  */
221 static inline __attribute__ (( always_inline )) void *
222 atacmd_priv ( struct ata_command *atacmd ) {
223         return atacmd->priv;
224 }
225
226 /**
227  * Free ATA command
228  *
229  * @v refcnt            Reference count
230  */
231 static void atacmd_free ( struct refcnt *refcnt ) {
232         struct ata_command *atacmd =
233                 container_of ( refcnt, struct ata_command, refcnt );
234
235         /* Remove from list of commands */
236         list_del ( &atacmd->list );
237         atadev_put ( atacmd->atadev );
238
239         /* Free command */
240         free ( atacmd );
241 }
242
243 /**
244  * Close ATA command
245  *
246  * @v atacmd            ATA command
247  * @v rc                Reason for close
248  */
249 static void atacmd_close ( struct ata_command *atacmd, int rc ) {
250         struct ata_device *atadev = atacmd->atadev;
251
252         if ( rc != 0 ) {
253                 DBGC ( atadev, "ATA %p tag %08x closed: %s\n",
254                        atadev, atacmd->tag, strerror ( rc ) );
255         }
256
257         /* Shut down interfaces */
258         intf_shutdown ( &atacmd->ata, rc );
259         intf_shutdown ( &atacmd->block, rc );
260 }
261
262 /**
263  * Handle ATA command completion
264  *
265  * @v atacmd            ATA command
266  * @v rc                Reason for close
267  */
268 static void atacmd_done ( struct ata_command *atacmd, int rc ) {
269
270         /* Hand over to the command completion handler */
271         atacmd->type->done ( atacmd, rc );
272 }
273
274 /**
275  * Use provided data buffer for ATA command
276  *
277  * @v atacmd            ATA command
278  * @v buffer            Available buffer
279  * @v len               Available buffer length
280  * @ret data            Data buffer
281  * @ret data_len        Data buffer length
282  */
283 static void atacmd_data_buffer ( struct ata_command *atacmd __unused,
284                                  userptr_t buffer, size_t len,
285                                  userptr_t *data, size_t *data_len ) {
286         *data = buffer;
287         *data_len = len;
288 }
289
290 /**
291  * Use no data buffer for ATA command
292  *
293  * @v atacmd            ATA command
294  * @v buffer            Available buffer
295  * @v len               Available buffer length
296  * @ret data            Data buffer
297  * @ret data_len        Data buffer length
298  */
299 static void atacmd_data_none ( struct ata_command *atacmd __unused,
300                                userptr_t buffer __unused, size_t len __unused,
301                                userptr_t *data __unused,
302                                size_t *data_len __unused ) {
303         /* Nothing to do */
304 }
305
306 /**
307  * Use private data buffer for ATA command
308  *
309  * @v atacmd            ATA command
310  * @v buffer            Available buffer
311  * @v len               Available buffer length
312  * @ret data            Data buffer
313  * @ret data_len        Data buffer length
314  */
315 static void atacmd_data_priv ( struct ata_command *atacmd,
316                                userptr_t buffer __unused, size_t len __unused,
317                                userptr_t *data, size_t *data_len ) {
318         *data = virt_to_user ( atacmd_priv ( atacmd ) );
319         *data_len = atacmd->type->priv_len;
320 }
321
322 /** ATA READ command type */
323 static struct ata_command_type atacmd_read = {
324         .name = "READ",
325         .cmd_lba = ATA_CMD_READ,
326         .cmd_lba48 = ATA_CMD_READ_EXT,
327         .data_in = atacmd_data_buffer,
328         .data_out = atacmd_data_none,
329         .done = atacmd_close,
330 };
331
332 /** ATA WRITE command type */
333 static struct ata_command_type atacmd_write = {
334         .name = "WRITE",
335         .cmd_lba = ATA_CMD_WRITE,
336         .cmd_lba48 = ATA_CMD_WRITE_EXT,
337         .data_in = atacmd_data_none,
338         .data_out = atacmd_data_buffer,
339         .done = atacmd_close,
340 };
341
342 /** ATA IDENTIFY private data */
343 struct ata_identify_private {
344         /** Identity data */
345         struct ata_identity identity;
346 };
347
348 /**
349  * Return ATA model string (for debugging)
350  *
351  * @v identify          ATA identity data
352  * @ret model           Model string
353  */
354 static const char * ata_model ( struct ata_identity *identity ) {
355         static union {
356                 uint16_t words[ sizeof ( identity->model ) / 2 ];
357                 char text[ sizeof ( identity->model ) + 1 /* NUL */ ];
358         } buf;
359         unsigned int i;
360
361         for ( i = 0 ; i < ( sizeof ( identity->model ) / 2 ) ; i++ )
362                 buf.words[i] = bswap_16 ( identity->model[i] );
363
364         return buf.text;
365 }
366
367 /**
368  * Handle ATA IDENTIFY command completion
369  *
370  * @v atacmd            ATA command
371  * @v rc                Reason for completion
372  */
373 static void atacmd_identify_done ( struct ata_command *atacmd, int rc ) {
374         struct ata_device *atadev = atacmd->atadev;
375         struct ata_identify_private *priv = atacmd_priv ( atacmd );
376         struct ata_identity *identity = &priv->identity;
377         struct block_device_capacity capacity;
378
379         /* Close if command failed */
380         if ( rc != 0 ) {
381                 atacmd_close ( atacmd, rc );
382                 return;
383         }
384
385         /* Extract capacity */
386         if ( identity->supports_lba48 & cpu_to_le16 ( ATA_SUPPORTS_LBA48 ) ) {
387                 atadev->lba48 = 1;
388                 capacity.blocks = le64_to_cpu ( identity->lba48_sectors );
389         } else {
390                 capacity.blocks = le32_to_cpu ( identity->lba_sectors );
391         }
392         capacity.blksize = ATA_SECTOR_SIZE;
393         capacity.max_count = atadev->max_count;
394         DBGC ( atadev, "ATA %p is a %s\n", atadev, ata_model ( identity ) );
395         DBGC ( atadev, "ATA %p has %#llx blocks (%ld MB) and uses %s\n",
396                atadev, capacity.blocks,
397                ( ( signed long ) ( capacity.blocks >> 11 ) ),
398                ( atadev->lba48 ? "LBA48" : "LBA" ) );
399
400         /* Return capacity to caller */
401         block_capacity ( &atacmd->block, &capacity );
402
403         /* Close command */
404         atacmd_close ( atacmd, 0 );
405 }
406
407 /** ATA IDENTITY command type */
408 static struct ata_command_type atacmd_identify = {
409         .name = "IDENTIFY",
410         .priv_len = sizeof ( struct ata_identify_private ),
411         .cmd_lba = ATA_CMD_IDENTIFY,
412         .cmd_lba48 = ATA_CMD_IDENTIFY,
413         .data_in = atacmd_data_priv,
414         .data_out = atacmd_data_none,
415         .done = atacmd_identify_done,
416 };
417
418 /** ATA command block interface operations */
419 static struct interface_operation atacmd_block_op[] = {
420         INTF_OP ( intf_close, struct ata_command *, atacmd_close ),
421 };
422
423 /** ATA command block interface descriptor */
424 static struct interface_descriptor atacmd_block_desc =
425         INTF_DESC_PASSTHRU ( struct ata_command, block,
426                              atacmd_block_op, ata );
427
428 /** ATA command ATA interface operations */
429 static struct interface_operation atacmd_ata_op[] = {
430         INTF_OP ( intf_close, struct ata_command *, atacmd_done ),
431 };
432
433 /** ATA command ATA interface descriptor */
434 static struct interface_descriptor atacmd_ata_desc =
435         INTF_DESC_PASSTHRU ( struct ata_command, ata,
436                              atacmd_ata_op, block );
437
438 /**
439  * Create ATA command
440  *
441  * @v atadev            ATA device
442  * @v block             Block data interface
443  * @v type              ATA command type
444  * @v lba               Starting logical block address
445  * @v count             Number of blocks to transfer
446  * @v buffer            Data buffer
447  * @v len               Length of data buffer
448  * @ret rc              Return status code
449  */
450 static int atadev_command ( struct ata_device *atadev,
451                             struct interface *block,
452                             struct ata_command_type *type,
453                             uint64_t lba, unsigned int count,
454                             userptr_t buffer, size_t len ) {
455         struct ata_command *atacmd;
456         struct ata_cmd command;
457         int tag;
458         int rc;
459
460         /* Allocate and initialise structure */
461         atacmd = zalloc ( sizeof ( *atacmd ) + type->priv_len );
462         if ( ! atacmd ) {
463                 rc = -ENOMEM;
464                 goto err_zalloc;
465         }
466         ref_init ( &atacmd->refcnt, atacmd_free );
467         intf_init ( &atacmd->block, &atacmd_block_desc, &atacmd->refcnt );
468         intf_init ( &atacmd->ata, &atacmd_ata_desc,
469                     &atacmd->refcnt );
470         atacmd->atadev = atadev_get ( atadev );
471         list_add ( &atacmd->list, &ata_commands );
472         atacmd->type = type;
473
474         /* Sanity check */
475         if ( len != ( count * ATA_SECTOR_SIZE ) ) {
476                 DBGC ( atadev, "ATA %p tag %08x buffer length mismatch (count "
477                        "%d len %zd)\n", atadev, atacmd->tag, count, len );
478                 rc = -EINVAL;
479                 goto err_len;
480         }
481
482         /* Construct command */
483         memset ( &command, 0, sizeof ( command ) );
484         command.cb.lba.native = lba;
485         command.cb.count.native = count;
486         command.cb.device = ( atadev->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA );
487         command.cb.lba48 = atadev->lba48;
488         if ( ! atadev->lba48 )
489                 command.cb.device |= command.cb.lba.bytes.low_prev;
490         command.cb.cmd_stat =
491                 ( atadev->lba48 ? type->cmd_lba48 : type->cmd_lba );
492         type->data_in ( atacmd, buffer, len,
493                         &command.data_in, &command.data_in_len );
494         type->data_out ( atacmd, buffer, len,
495                          &command.data_out, &command.data_out_len );
496
497         /* Issue command */
498         if ( ( tag = ata_command ( &atadev->ata, &atacmd->ata,
499                                    &command ) ) < 0 ) {
500                 rc = tag;
501                 DBGC ( atadev, "ATA %p tag %08x could not issue command: %s\n",
502                        atadev, atacmd->tag, strerror ( rc ) );
503                 goto err_command;
504         }
505         atacmd->tag = tag;
506
507         DBGC2 ( atadev, "ATA %p tag %08x %s cmd %02x dev %02x LBA%s %08llx "
508                 "count %04x\n", atadev, atacmd->tag, atacmd->type->name,
509                 command.cb.cmd_stat, command.cb.device,
510                 ( command.cb.lba48 ? "48" : "" ),
511                 ( unsigned long long ) command.cb.lba.native,
512                 command.cb.count.native );
513
514         /* Attach to parent interface, mortalise self, and return */
515         intf_plug_plug ( &atacmd->block, block );
516         ref_put ( &atacmd->refcnt );
517         return 0;
518
519  err_command:
520  err_len:
521         atacmd_close ( atacmd, rc );
522         ref_put ( &atacmd->refcnt );
523  err_zalloc:
524         return rc;
525 }
526
527 /**
528  * Issue ATA block read
529  *
530  * @v atadev            ATA device
531  * @v block             Block data interface
532  * @v lba               Starting logical block address
533  * @v count             Number of blocks to transfer
534  * @v buffer            Data buffer
535  * @v len               Length of data buffer
536  * @ret rc              Return status code
537
538  */
539 static int atadev_read ( struct ata_device *atadev,
540                          struct interface *block,
541                          uint64_t lba, unsigned int count,
542                          userptr_t buffer, size_t len ) {
543         return atadev_command ( atadev, block, &atacmd_read,
544                                 lba, count, buffer, len );
545 }
546
547 /**
548  * Issue ATA block write
549  *
550  * @v atadev            ATA device
551  * @v block             Block data interface
552  * @v lba               Starting logical block address
553  * @v count             Number of blocks to transfer
554  * @v buffer            Data buffer
555  * @v len               Length of data buffer
556  * @ret rc              Return status code
557  */
558 static int atadev_write ( struct ata_device *atadev,
559                           struct interface *block,
560                           uint64_t lba, unsigned int count,
561                           userptr_t buffer, size_t len ) {
562         return atadev_command ( atadev, block, &atacmd_write,
563                                 lba, count, buffer, len );
564 }
565
566 /**
567  * Read ATA device capacity
568  *
569  * @v atadev            ATA device
570  * @v block             Block data interface
571  * @ret rc              Return status code
572  */
573 static int atadev_read_capacity ( struct ata_device *atadev,
574                                   struct interface *block ) {
575         struct ata_identity *identity;
576
577         assert ( atacmd_identify.priv_len == sizeof ( *identity ) );
578         assert ( atacmd_identify.priv_len == ATA_SECTOR_SIZE );
579         return atadev_command ( atadev, block, &atacmd_identify,
580                                 0, 1, UNULL, ATA_SECTOR_SIZE );
581 }
582
583 /**
584  * Close ATA device
585  *
586  * @v atadev            ATA device
587  * @v rc                Reason for close
588  */
589 static void atadev_close ( struct ata_device *atadev, int rc ) {
590         struct ata_command *atacmd;
591         struct ata_command *tmp;
592
593         /* Shut down interfaces */
594         intf_shutdown ( &atadev->block, rc );
595         intf_shutdown ( &atadev->ata, rc );
596
597         /* Shut down any remaining commands */
598         list_for_each_entry_safe ( atacmd, tmp, &ata_commands, list ) {
599                 if ( atacmd->atadev != atadev )
600                         continue;
601                 atacmd_get ( atacmd );
602                 atacmd_close ( atacmd, rc );
603                 atacmd_put ( atacmd );
604         }
605 }
606
607 /**
608  * Describe ATA device using EDD
609  *
610  * @v atadev            ATA device
611  * @v type              EDD interface type
612  * @v path              EDD device path
613  * @ret rc              Return status code
614  */
615 static int atadev_edd_describe ( struct ata_device *atadev,
616                                  struct edd_interface_type *type,
617                                  union edd_device_path *path ) {
618
619         type->type = cpu_to_le64 ( EDD_INTF_TYPE_ATA );
620         path->ata.slave = ( ( atadev->device == ATA_DEV_SLAVE ) ? 0x01 : 0x00 );
621         return 0;
622 }
623
624 /** ATA device block interface operations */
625 static struct interface_operation atadev_block_op[] = {
626         INTF_OP ( block_read, struct ata_device *, atadev_read ),
627         INTF_OP ( block_write, struct ata_device *, atadev_write ),
628         INTF_OP ( block_read_capacity, struct ata_device *,
629                   atadev_read_capacity ),
630         INTF_OP ( intf_close, struct ata_device *, atadev_close ),
631         INTF_OP ( edd_describe, struct ata_device *, atadev_edd_describe ),
632 };
633
634 /** ATA device block interface descriptor */
635 static struct interface_descriptor atadev_block_desc =
636         INTF_DESC_PASSTHRU ( struct ata_device, block,
637                              atadev_block_op, ata );
638
639 /** ATA device ATA interface operations */
640 static struct interface_operation atadev_ata_op[] = {
641         INTF_OP ( intf_close, struct ata_device *, atadev_close ),
642 };
643
644 /** ATA device ATA interface descriptor */
645 static struct interface_descriptor atadev_ata_desc =
646         INTF_DESC_PASSTHRU ( struct ata_device, ata,
647                              atadev_ata_op, block );
648
649 /**
650  * Open ATA device
651  *
652  * @v block             Block control interface
653  * @v ata               ATA control interface
654  * @v device            ATA device number
655  * @v max_count         Maximum number of blocks per single transfer
656  * @ret rc              Return status code
657  */
658 int ata_open ( struct interface *block, struct interface *ata,
659                unsigned int device, unsigned int max_count ) {
660         struct ata_device *atadev;
661
662         /* Allocate and initialise structure */
663         atadev = zalloc ( sizeof ( *atadev ) );
664         if ( ! atadev )
665                 return -ENOMEM;
666         ref_init ( &atadev->refcnt, NULL );
667         intf_init ( &atadev->block, &atadev_block_desc, &atadev->refcnt );
668         intf_init ( &atadev->ata, &atadev_ata_desc, &atadev->refcnt );
669         atadev->device = device;
670         atadev->max_count = max_count;
671
672         /* Attach to ATA and parent and interfaces, mortalise self,
673          * and return
674          */
675         intf_plug_plug ( &atadev->ata, ata );
676         intf_plug_plug ( &atadev->block, block );
677         ref_put ( &atadev->refcnt );
678         return 0;
679 }