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