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