|
|
1.1 root 1: /*
2: * Copyright (C) 2013 Free Software Foundation
3: *
4: * This program is free software ; you can redistribute it and/or modify
5: * it under the terms of the GNU General Public License as published by
6: * the Free Software Foundation ; either version 2 of the License, or
7: * (at your option) any later version.
8: *
9: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY ; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with the program ; if not, write to the Free Software
16: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17: */
18:
19: #include <ahci.h>
20: #include <kern/assert.h>
21: #include <linux/kernel.h>
22: #include <linux/types.h>
23: #include <linux/pci.h>
24: #include <linux/fs.h>
25: #include <linux/bios32.h>
26: #include <linux/major.h>
27: #include <linux/hdreg.h>
28: #include <linux/genhd.h>
29: #include <asm/io.h>
30:
31: #define MAJOR_NR SCSI_DISK_MAJOR
32: #include <linux/blk.h>
33:
34: /* Standard AHCI BAR for mmio */
35: #define AHCI_PCI_BAR 5
36:
37: /* minor: 2 bits for device number, 6 bits for partition number. */
38:
1.1.1.2 ! root 39: #define MAX_PORTS 8
! 40: #define PARTN_BITS 5
1.1 root 41: #define PARTN_MASK ((1<<PARTN_BITS)-1)
42:
43: /* We need to use one DMA scatter element per physical page.
44: * ll_rw_block creates at most 8 buffer heads */
45: /* See MAX_BUF */
46: #define PRDTL_SIZE 8
47:
48: #define WAIT_MAX (1*HZ) /* Wait at most 1s for requests completion */
49:
50: /* AHCI standard structures */
51:
52: struct ahci_prdt {
53: u32 dba; /* Data base address */
54: u32 dbau; /* upper 32bit */
55: u32 rsv0; /* Reserved */
56:
57: u32 dbc; /* Byte count bits 0-21,
58: * bit31 interrupt on completion. */
59: };
60:
61: struct ahci_cmd_tbl {
62: u8 cfis[64];
63: u8 acmd[16];
64: u8 rsv[48];
65:
66: struct ahci_prdt prdtl[PRDTL_SIZE];
67: };
68:
69: struct ahci_command {
70: u32 opts; /* Command options */
71:
72: u32 prdbc; /* Physical Region Descriptor byte count */
73:
74: u32 ctba; /* Command Table Descriptor Base Address */
75: u32 ctbau; /* upper 32bit */
76:
77: u32 rsv1[4]; /* Reserved */
78: };
79:
80: struct ahci_fis_dma {
81: u8 fis_type;
82: u8 flags;
83: u8 rsved[2];
84: u64 id;
85: u32 rsvd;
86: u32 offset;
87: u32 count;
88: u32 resvd;
89: };
90:
91: struct ahci_fis_pio {
92: u8 fis_type;
93: u8 flags;
94: u8 status;
95: u8 error;
96:
97: u8 lba0;
98: u8 lba1;
99: u8 lba2;
100: u8 device;
101:
102: u8 lba3;
103: u8 lba4;
104: u8 lba5;
105: u8 rsv2;
106:
107: u8 countl;
108: u8 counth;
109: u8 rsv3;
110: u8 e_status;
111:
112: u16 tc; /* Transfer Count */
113: u8 rsv4[2];
114: };
115:
116: struct ahci_fis_d2h {
117: u8 fis_type;
118: u8 flags;
119: u8 status;
120: u8 error;
121:
122: u8 lba0;
123: u8 lba1;
124: u8 lba2;
125: u8 device;
126:
127: u8 lba3;
128: u8 lba4;
129: u8 lba5;
130: u8 rsv2;
131:
132: u8 countl;
133: u8 counth;
134: u8 rsv3[2];
135:
136: u8 rsv4[4];
137: };
138:
139: struct ahci_fis_dev {
140: u8 rsvd[8];
141: };
142:
143: struct ahci_fis_h2d {
144: u8 fis_type;
145: u8 flags;
146: u8 command;
147: u8 featurel;
148:
149: u8 lba0;
150: u8 lba1;
151: u8 lba2;
152: u8 device;
153:
154: u8 lba3;
155: u8 lba4;
156: u8 lba5;
157: u8 featureh;
158:
159: u8 countl;
160: u8 counth;
161: u8 icc;
162: u8 control;
163:
164: u8 rsv1[4];
165: };
166:
167: struct ahci_fis_data {
168: u8 fis_type;
169: u8 flags;
170: u8 rsv1[2];
171: u32 data1[];
172: };
173:
174: struct ahci_fis {
175: struct ahci_fis_dma dma_fis;
176: u8 pad0[4];
177:
178: struct ahci_fis_pio pio_fis;
179: u8 pad1[12];
180:
181: struct ahci_fis_d2h d2h_fis;
182: u8 pad2[4];
183:
184: struct ahci_fis_dev dev_fis;
185:
186: u8 ufis[64];
187:
188: u8 rsv[0x100 - 0xa0];
189: };
190:
191: struct ahci_port {
192: u32 clb; /* Command List Base address */
193: u32 clbu; /* upper 32bit */
194: u32 fb; /* FIS Base */
195: u32 fbu; /* upper 32bit */
196: u32 is; /* Interrupt Status */
197: u32 ie; /* Interrupt Enable */
198: u32 cmd; /* Command and Status */
199: u32 rsv0; /* Reserved */
200: u32 tfd; /* Task File Data */
201: u32 sig; /* Signature */
202: u32 ssts; /* SATA Status */
203: u32 sctl; /* SATA Control */
204: u32 serr; /* SATA Error */
205: u32 sact; /* SATA Active */
206: u32 ci; /* Command Issue */
207: u32 sntf; /* SATA Notification */
208: u32 fbs; /* FIS-based switch control */
209: u8 rsv1[0x70 - 0x44]; /* Reserved */
210: u8 vendor[0x80 - 0x70]; /* Vendor-specific */
211: };
212:
213: struct ahci_host {
214: u32 cap; /* Host capabilities */
215: u32 ghc; /* Global Host Control */
216: u32 is; /* Interrupt Status */
217: u32 pi; /* Port Implemented */
218: u32 v; /* Version */
219: u32 ccc_ctl; /* Command Completion Coalescing control */
220: u32 ccc_pts; /* Command Completion Coalescing ports */
221: u32 em_loc; /* Enclosure Management location */
222: u32 em_ctrl; /* Enclosure Management control */
223: u32 cap2; /* Host capabilities extended */
224: u32 bohc; /* BIOS/OS Handoff Control and status */
225: u8 rsv[0xa0 - 0x2c]; /* Reserved */
226: u8 vendor[0x100 - 0xa0]; /* Vendor-specific */
227: struct ahci_port ports[]; /* Up to 32 ports */
228: };
229:
230: /* Our own data */
231:
232: static struct port {
233: /* memory-mapped regions */
234: const volatile struct ahci_host *ahci_host;
235: const volatile struct ahci_port *ahci_port;
236:
237: /* host-memory buffers */
238: struct ahci_command *command;
239: struct ahci_fis *fis;
240: struct ahci_cmd_tbl *prdtl;
241:
1.1.1.2 ! root 242: struct hd_driveid id;
! 243: unsigned is_cd;
1.1 root 244: unsigned long long capacity; /* Nr of sectors */
245: u32 status; /* interrupt status */
246: unsigned cls; /* Command list maximum size.
247: We currently only use 1. */
248: struct wait_queue *q; /* IRQ wait queue */
249: struct hd_struct *part; /* drive partition table */
250: unsigned lba48; /* Whether LBA48 is supported */
251: unsigned identify; /* Whether we are just identifying
252: at boot */
253: struct gendisk *gd;
254: } ports[MAX_PORTS];
255:
256:
257: /* do_request() gets called by the block layer to push a request to the disk.
258: We just push one, and when an interrupt tells it's over, we call do_request()
259: ourself again to push the next request, etc. */
260:
261: /* Request completed, either successfully or with an error */
262: static void ahci_end_request(int uptodate)
263: {
264: struct request *rq = CURRENT;
265: struct buffer_head *bh;
266:
267: rq->errors = 0;
268: if (!uptodate) {
1.1.1.2 ! root 269: if (!rq->quiet)
! 270: printk("end_request: I/O error, dev %s, sector %lu\n",
! 271: kdevname(rq->rq_dev), rq->sector);
1.1 root 272: }
273:
274: for (bh = rq->bh; bh; )
275: {
276: struct buffer_head *next = bh->b_reqnext;
277: bh->b_reqnext = NULL;
278: mark_buffer_uptodate (bh, uptodate);
279: unlock_buffer (bh);
280: bh = next;
281: }
282:
283: CURRENT = rq->next;
284: if (rq->sem != NULL)
285: up(rq->sem);
286: rq->rq_status = RQ_INACTIVE;
287: wake_up(&wait_for_request);
288: }
289:
290: /* Push the request to the controler port */
1.1.1.2 ! root 291: static void ahci_do_port_request(struct port *port, unsigned long long sector, struct request *rq)
1.1 root 292: {
293: struct ahci_command *command = port->command;
294: struct ahci_cmd_tbl *prdtl = port->prdtl;
295: struct ahci_fis_h2d *fis_h2d;
296: unsigned slot = 0;
297: struct buffer_head *bh;
298: unsigned i;
299:
300: rq->rq_status = RQ_SCSI_BUSY;
301:
302: /* Shouldn't ever happen: the block glue is limited at 8 blocks */
303: assert(rq->nr_sectors < 0x10000);
304:
305: fis_h2d = (void*) &prdtl[slot].cfis;
306: fis_h2d->fis_type = FIS_TYPE_REG_H2D;
307: fis_h2d->flags = 128;
308: if (port->lba48)
309: if (rq->cmd == READ)
310: fis_h2d->command = WIN_READDMA_EXT;
311: else
312: fis_h2d->command = WIN_WRITEDMA_EXT;
313: else
314: if (rq->cmd == READ)
315: fis_h2d->command = WIN_READDMA;
316: else
317: fis_h2d->command = WIN_WRITEDMA;
318:
319: fis_h2d->device = 1<<6; /* LBA */
320:
321: fis_h2d->lba0 = sector;
322: fis_h2d->lba1 = sector >> 8;
323: fis_h2d->lba2 = sector >> 16;
324:
325: fis_h2d->lba3 = sector >> 24;
1.1.1.2 ! root 326: fis_h2d->lba4 = sector >> 32;
! 327: fis_h2d->lba5 = sector >> 40;
1.1 root 328:
329: fis_h2d->countl = rq->nr_sectors;
330: fis_h2d->counth = rq->nr_sectors >> 8;
331:
332: command[slot].opts = sizeof(*fis_h2d) / sizeof(u32);
333:
334: if (rq->cmd == WRITE)
335: command[slot].opts |= AHCI_CMD_WRITE;
336:
337: for (i = 0, bh = rq->bh; bh; i++, bh = bh->b_reqnext)
338: {
339: assert(i < PRDTL_SIZE);
340: assert((((unsigned long) bh->b_data) & ~PAGE_MASK) ==
341: (((unsigned long) bh->b_data + bh->b_size - 1) & ~PAGE_MASK));
342: prdtl[slot].prdtl[i].dbau = 0;
343: prdtl[slot].prdtl[i].dba = vmtophys(bh->b_data);
344: prdtl[slot].prdtl[i].dbc = bh->b_size - 1;
345: }
346:
347: command[slot].opts |= i << 16;
348:
349: /* Make sure main memory buffers are up to date */
350: mb();
351:
352: /* Issue command */
353: writel(1 << slot, &port->ahci_port->ci);
354:
355: /* TODO: IRQ timeout handler */
356: }
357:
358: /* Called by block core to push a request */
359: /* TODO: ideally, would have one request queue per port */
360: /* TODO: ideally, would use tags to process several requests at a time */
361: static void ahci_do_request() /* invoked with cli() */
362: {
363: struct request *rq;
364: unsigned minor, unit;
1.1.1.2 ! root 365: unsigned long long block, blockend;
1.1 root 366: struct port *port;
367:
368: rq = CURRENT;
369: if (!rq)
370: return;
371:
372: if (rq->rq_status != RQ_ACTIVE)
373: /* Current one is already ongoing, let the interrupt handler
374: * push the new one when the current one is finished. */
375: return;
376:
377: if (MAJOR(rq->rq_dev) != MAJOR_NR) {
378: printk("bad ahci major %u\n", MAJOR(rq->rq_dev));
379: goto kill_rq;
380: }
381:
382: minor = MINOR(rq->rq_dev);
383: unit = minor >> PARTN_BITS;
384: if (unit > MAX_PORTS) {
385: printk("bad ahci unit %u\n", unit);
386: goto kill_rq;
387: }
388:
389: port = &ports[unit];
390:
391: /* Compute start sector */
392: block = rq->sector;
393: block += port->part[minor & PARTN_MASK].start_sect;
394:
395: /* And check end */
396: blockend = block + rq->nr_sectors;
397: if (blockend < block) {
1.1.1.2 ! root 398: if (!rq->quiet)
! 399: printk("bad blockend %lu vs %lu\n", (unsigned long) blockend, (unsigned long) block);
1.1 root 400: goto kill_rq;
401: }
402: if (blockend > port->capacity) {
1.1.1.2 ! root 403: if (!rq->quiet)
! 404: {
! 405: printk("offset for %u was %lu\n", minor, port->part[minor & PARTN_MASK].start_sect);
! 406: printk("bad access: block %lu, count= %lu\n", (unsigned long) blockend, (unsigned long) port->capacity);
! 407: }
1.1 root 408: goto kill_rq;
409: }
410:
411: /* Push this to the port */
412: ahci_do_port_request(port, block, rq);
413: return;
414:
415: kill_rq:
416: ahci_end_request(0);
417: }
418:
419: /* The given port got an interrupt, terminate the current request if any */
420: static void ahci_port_interrupt(struct port *port, u32 status)
421: {
422: unsigned slot = 0;
423:
424: if (readl(&port->ahci_port->ci) & (1 << slot)) {
425: /* Command still pending */
426: return;
427: }
428:
429: if (port->identify) {
430: port->status = status;
431: wake_up(&port->q);
432: return;
433: }
434:
435: if (!CURRENT || CURRENT->rq_status != RQ_SCSI_BUSY) {
436: /* No request currently running */
437: return;
438: }
439:
440: if (status & (PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_IF_NONFATAL)) {
441: printk("ahci error %x %x\n", status, readl(&port->ahci_port->tfd));
442: ahci_end_request(0);
443: return;
444: }
445:
446: ahci_end_request(1);
447: }
448:
449: /* Start of IRQ handler. Iterate over all ports for this host */
450: static void ahci_interrupt (int irq, void *host, struct pt_regs *regs)
451: {
452: struct port *port;
453: struct ahci_host *ahci_host = host;
454: u32 irq_mask;
455: u32 status;
456:
457: irq_mask = readl(&ahci_host->is);
458:
459: if (!irq_mask)
460: return;
461:
462: for (port = &ports[0]; port < &ports[MAX_PORTS]; port++) {
463: if (port->ahci_host == ahci_host && (irq_mask & (1 << (port->ahci_port - ahci_host->ports)))) {
464: status = readl(&port->ahci_port->is);
465: /* Clear interrupt before possibly triggering others */
466: writel(status, &port->ahci_port->is);
467: ahci_port_interrupt (port, status);
468: }
469: }
470:
471: if (CURRENT)
472: /* Still some requests, queue another one */
473: ahci_do_request();
474:
475: /* Clear host after clearing ports */
476: writel(irq_mask, &ahci_host->is);
477:
478: /* unlock */
479: }
480:
481: static int ahci_ioctl (struct inode *inode, struct file *file,
482: unsigned int cmd, unsigned long arg)
483: {
484: int major, unit;
485:
486: if (!inode || !inode->i_rdev)
487: return -EINVAL;
488:
489: major = MAJOR(inode->i_rdev);
490: if (major != MAJOR_NR)
491: return -ENOTTY;
492:
493: unit = DEVICE_NR(inode->i_rdev);
494: if (unit >= MAX_PORTS)
495: return -EINVAL;
496:
497: switch (cmd) {
498: case BLKRRPART:
499: if (!suser()) return -EACCES;
500: if (!ports[unit].gd)
501: return -EINVAL;
502: resetup_one_dev(ports[unit].gd, unit);
503: return 0;
504: default:
505: return -EPERM;
506: }
507: }
508:
509: static int ahci_open (struct inode *inode, struct file *file)
510: {
511: int target;
512:
513: if (MAJOR(inode->i_rdev) != MAJOR_NR)
514: return -ENXIO;
515:
516: target = MINOR(inode->i_rdev) >> PARTN_BITS;
517: if (target >= MAX_PORTS)
518: return -ENXIO;
519:
520: if (!ports[target].ahci_port)
521: return -ENXIO;
522:
523: return 0;
524: }
525:
526: static void ahci_release (struct inode *inode, struct file *file)
527: {
528: }
529:
530: static int ahci_fsync (struct inode *inode, struct file *file)
531: {
532: printk("fsync\n");
533: return -ENOSYS;
534: }
535:
536: static struct file_operations ahci_fops = {
537: .lseek = NULL,
538: .read = block_read,
539: .write = block_write,
540: .readdir = NULL,
541: .select = NULL,
542: .ioctl = ahci_ioctl,
543: .mmap = NULL,
544: .open = ahci_open,
545: .release = ahci_release,
546: .fsync = ahci_fsync,
547: .fasync = NULL,
548: .check_media_change = NULL,
549: .revalidate = NULL,
550: };
551:
552: /* Disk timed out while processing identify, interrupt ahci_probe_port */
553: static void identify_timeout(unsigned long data)
554: {
555: struct port *port = (void*) data;
556:
557: wake_up(&port->q);
558: }
559:
560: static struct timer_list identify_timer = { .function = identify_timeout };
561:
1.1.1.2 ! root 562: static int ahci_identify(const volatile struct ahci_host *ahci_host, const volatile struct ahci_port *ahci_port, struct port *port, unsigned cmd)
1.1 root 563: {
564: struct hd_driveid id;
565: struct ahci_fis_h2d *fis_h2d;
1.1.1.2 ! root 566: struct ahci_command *command = port->command;
! 567: struct ahci_cmd_tbl *prdtl = port->prdtl;
! 568: unsigned long flags;
1.1 root 569: unsigned slot;
570: unsigned long first_part;
571: unsigned long long timeout;
1.1.1.2 ! root 572: int ret = 0;
1.1 root 573:
574: /* Identify device */
575: /* TODO: make this a request */
576: slot = 0;
577:
578: fis_h2d = (void*) &prdtl[slot].cfis;
579: fis_h2d->fis_type = FIS_TYPE_REG_H2D;
580: fis_h2d->flags = 128;
1.1.1.2 ! root 581: fis_h2d->command = cmd;
1.1 root 582: fis_h2d->device = 0;
583:
584: /* Fetch the 512 identify data */
585: memset(&id, 0, sizeof(id));
586:
587: command[slot].opts = sizeof(*fis_h2d) / sizeof(u32);
588:
589: first_part = PAGE_ALIGN((unsigned long) &id) - (unsigned long) &id;
590:
591: if (first_part && first_part < sizeof(id)) {
592: /* split over two pages */
593:
594: command[slot].opts |= (2 << 16);
595:
596: prdtl[slot].prdtl[0].dbau = 0;
597: prdtl[slot].prdtl[0].dba = vmtophys((void*) &id);
598: prdtl[slot].prdtl[0].dbc = first_part - 1;
599: prdtl[slot].prdtl[1].dbau = 0;
600: prdtl[slot].prdtl[1].dba = vmtophys((void*) &id + first_part);
601: prdtl[slot].prdtl[1].dbc = sizeof(id) - first_part - 1;
602: }
603: else
604: {
605: command[slot].opts |= (1 << 16);
606:
607: prdtl[slot].prdtl[0].dbau = 0;
608: prdtl[slot].prdtl[0].dba = vmtophys((void*) &id);
609: prdtl[slot].prdtl[0].dbc = sizeof(id) - 1;
610: }
611:
612: timeout = jiffies + WAIT_MAX;
613: while (readl(&ahci_port->tfd) & (BUSY_STAT | DRQ_STAT))
614: if (jiffies > timeout) {
615: printk("sd%u: timeout waiting for ready\n", port-ports);
616: port->ahci_host = NULL;
617: port->ahci_port = NULL;
1.1.1.2 ! root 618: return 3;
1.1 root 619: }
620:
621: save_flags(flags);
622: cli();
623:
624: port->identify = 1;
625: port->status = 0;
626:
627: /* Issue command */
628: mb();
629: writel(1 << slot, &ahci_port->ci);
630:
631: timeout = jiffies + WAIT_MAX;
632: identify_timer.expires = timeout;
633: identify_timer.data = (unsigned long) port;
634: add_timer(&identify_timer);
635: while (!port->status) {
636: if (jiffies >= timeout) {
637: printk("sd%u: timeout waiting for ready\n", port-ports);
638: port->ahci_host = NULL;
639: port->ahci_port = NULL;
640: del_timer(&identify_timer);
1.1.1.2 ! root 641: return 3;
1.1 root 642: }
643: sleep_on(&port->q);
644: }
645: del_timer(&identify_timer);
646: restore_flags(flags);
647:
1.1.1.2 ! root 648: if ((port->status & PORT_IRQ_TF_ERR) || readl(&ahci_port->is) & PORT_IRQ_TF_ERR)
1.1 root 649: {
1.1.1.2 ! root 650: /* Identify error */
1.1 root 651: port->capacity = 0;
652: port->lba48 = 0;
1.1.1.2 ! root 653: ret = 2;
1.1 root 654: } else {
1.1.1.2 ! root 655: memcpy(&port->id, &id, sizeof(id));
! 656: port->is_cd = 0;
! 657:
1.1 root 658: ide_fixstring(id.model, sizeof(id.model), 1);
659: ide_fixstring(id.fw_rev, sizeof(id.fw_rev), 1);
660: ide_fixstring(id.serial_no, sizeof(id.serial_no), 1);
1.1.1.2 ! root 661: if (cmd == WIN_PIDENTIFY)
! 662: {
! 663: unsigned char type = (id.config >> 8) & 0x1f;
! 664:
! 665: printk("sd%u: %s, ATAPI ", port - ports, id.model);
! 666: if (type == 5)
! 667: {
! 668: printk("unsupported CDROM drive\n");
! 669: port->is_cd = 1;
! 670: port->lba48 = 0;
! 671: port->capacity = 0;
! 672: }
! 673: else
! 674: {
! 675: printk("unsupported type %d\n", type);
! 676: port->lba48 = 0;
! 677: port->capacity = 0;
! 678: return 2;
! 679: }
! 680: return 0;
! 681: }
! 682:
1.1 root 683: if (id.command_set_2 & (1U<<10))
684: {
685: port->lba48 = 1;
686: port->capacity = id.lba_capacity_2;
687: if (port->capacity >= (1ULL << 32))
688: {
689: port->capacity = (1ULL << 32) - 1;
690: printk("Warning: truncating disk size to 2TiB\n");
691: }
692: }
693: else
694: {
695: port->lba48 = 0;
696: port->capacity = id.lba_capacity;
697: if (port->capacity > (1ULL << 24))
698: {
699: port->capacity = (1ULL << 24);
700: printk("Warning: truncating disk size to 128GiB\n");
701: }
702: }
703: if (port->capacity/2048 >= 10240)
704: printk("sd%u: %s, %uGB w/%dkB Cache\n", port - ports, id.model, (unsigned) (port->capacity/(2048*1024)), id.buf_size/2);
705: else
706: printk("sd%u: %s, %uMB w/%dkB Cache\n", port - ports, id.model, (unsigned) (port->capacity/2048), id.buf_size/2);
707: }
708: port->identify = 0;
1.1.1.2 ! root 709:
! 710: return ret;
! 711: }
! 712:
! 713: /* Probe one AHCI port */
! 714: static void ahci_probe_port(const volatile struct ahci_host *ahci_host, const volatile struct ahci_port *ahci_port)
! 715: {
! 716: struct port *port;
! 717: void *mem;
! 718: unsigned cls = ((readl(&ahci_host->cap) >> 8) & 0x1f) + 1;
! 719: struct ahci_command *command;
! 720: struct ahci_fis *fis;
! 721: struct ahci_cmd_tbl *prdtl;
! 722: vm_size_t size =
! 723: cls * sizeof(*command)
! 724: + sizeof(*fis)
! 725: + cls * sizeof(*prdtl);
! 726: unsigned i;
! 727: unsigned long long timeout;
! 728:
! 729: for (i = 0; i < MAX_PORTS; i++) {
! 730: if (!ports[i].ahci_port)
! 731: break;
! 732: }
! 733: if (i == MAX_PORTS)
! 734: return;
! 735: port = &ports[i];
! 736:
! 737: /* Has to be 1K-aligned */
! 738: mem = vmalloc (size);
! 739: if (!mem)
! 740: return;
! 741: assert (!(((unsigned long) mem) & (1024-1)));
! 742: memset (mem, 0, size);
! 743:
! 744: port->ahci_host = ahci_host;
! 745: port->ahci_port = ahci_port;
! 746: port->cls = cls;
! 747:
! 748: port->command = command = mem;
! 749: port->fis = fis = (void*) command + cls * sizeof(*command);
! 750: port->prdtl = prdtl = (void*) fis + sizeof(*fis);
! 751:
! 752: /* Stop commands */
! 753: writel(readl(&ahci_port->cmd) & ~PORT_CMD_START, &ahci_port->cmd);
! 754: timeout = jiffies + WAIT_MAX;
! 755: while (readl(&ahci_port->cmd) & PORT_CMD_LIST_ON)
! 756: if (jiffies > timeout) {
! 757: printk("sd%u: timeout waiting for list completion\n", port-ports);
! 758: port->ahci_host = NULL;
! 759: port->ahci_port = NULL;
! 760: return;
! 761: }
! 762:
! 763: writel(readl(&ahci_port->cmd) & ~PORT_CMD_FIS_RX, &ahci_port->cmd);
! 764: timeout = jiffies + WAIT_MAX;
! 765: while (readl(&ahci_port->cmd) & PORT_CMD_FIS_ON)
! 766: if (jiffies > timeout) {
! 767: printk("sd%u: timeout waiting for FIS completion\n", port-ports);
! 768: port->ahci_host = NULL;
! 769: port->ahci_port = NULL;
! 770: return;
! 771: }
! 772:
! 773: /* We don't support 64bit */
! 774: /* Point controller to our buffers */
! 775: writel(0, &ahci_port->clbu);
! 776: writel(vmtophys((void*) command), &ahci_port->clb);
! 777: writel(0, &ahci_port->fbu);
! 778: writel(vmtophys((void*) fis), &ahci_port->fb);
! 779:
! 780: /* Clear any previous interrupts */
! 781: writel(readl(&ahci_port->is), &ahci_port->is);
! 782: writel(1 << (ahci_port - ahci_host->ports), &ahci_host->is);
! 783:
! 784: /* And activate them */
! 785: writel(DEF_PORT_IRQ, &ahci_port->ie);
! 786: writel(readl(&ahci_host->ghc) | HOST_IRQ_EN, &ahci_host->ghc);
! 787:
! 788: for (i = 0; i < cls; i++)
! 789: {
! 790: command[i].ctbau = 0;
! 791: command[i].ctba = vmtophys((void*) &prdtl[i]);
! 792: }
! 793:
! 794: /* Start commands */
! 795: timeout = jiffies + WAIT_MAX;
! 796: while (readl(&ahci_port->cmd) & PORT_CMD_LIST_ON)
! 797: if (jiffies > timeout) {
! 798: printk("sd%u: timeout waiting for list completion\n", port-ports);
! 799: port->ahci_host = NULL;
! 800: port->ahci_port = NULL;
! 801: return;
! 802: }
! 803:
! 804: writel(readl(&ahci_port->cmd) | PORT_CMD_FIS_RX | PORT_CMD_START, &ahci_port->cmd);
! 805:
! 806: if (ahci_identify(ahci_host, ahci_port, port, WIN_IDENTIFY) >= 2)
! 807: /* Try ATAPI */
! 808: ahci_identify(ahci_host, ahci_port, port, WIN_PIDENTIFY);
1.1 root 809: }
810:
811: /* Probe one AHCI PCI device */
812: static void ahci_probe_dev(unsigned char bus, unsigned char device)
813: {
814: unsigned char hdrtype;
815: unsigned char dev, fun;
816: const volatile struct ahci_host *ahci_host;
817: const volatile struct ahci_port *ahci_port;
818: unsigned nports, n, i;
819: unsigned port_map;
820: unsigned bar;
821: unsigned char irq;
822:
823: dev = PCI_SLOT(device);
824: fun = PCI_FUNC(device);
825:
826: /* Get configuration */
827: if (pcibios_read_config_byte(bus, device, PCI_HEADER_TYPE, &hdrtype) != PCIBIOS_SUCCESSFUL) {
828: printk("ahci: %02u:%02u.%u: Can not read configuration", bus, dev, fun);
829: return;
830: }
831:
832: if (hdrtype != 0) {
833: printk("ahci: %02u:%02u.%u: Unknown hdrtype %d\n", bus, dev, fun, hdrtype);
834: return;
835: }
836:
837: if (pcibios_read_config_dword(bus, device, PCI_BASE_ADDRESS_5, &bar) != PCIBIOS_SUCCESSFUL) {
838: printk("ahci: %02u:%02u.%u: Can not read BAR 5", bus, dev, fun);
839: return;
840: }
841: if (bar & 0x01) {
842: printk("ahci: %02u:%02u.%u: BAR 5 is I/O?!", bus, dev, fun);
843: return;
844: }
845: bar &= ~0x0f;
846:
847: if (pcibios_read_config_byte(bus, device, PCI_INTERRUPT_LINE, &irq) != PCIBIOS_SUCCESSFUL) {
848: printk("ahci: %02u:%02u.%u: Can not read IRQ", bus, dev, fun);
849: return;
850: }
851:
852: printk("AHCI SATA %02u:%02u.%u BAR 0x%x IRQ %u\n", bus, dev, fun, bar, irq);
853:
854: /* Map mmio */
855: ahci_host = vremap(bar, 0x2000);
856:
857: /* Request IRQ */
858: if (request_irq(irq, &ahci_interrupt, SA_SHIRQ, "ahci", (void*) ahci_host)) {
859: printk("ahci: %02u:%02u.%u: Can not get irq %u\n", bus, dev, fun, irq);
860: return;
861: }
862:
863: nports = (readl(&ahci_host->cap) & 0x1f) + 1;
864: port_map = readl(&ahci_host->pi);
865:
866: for (n = 0, i = 0; i < AHCI_MAX_PORTS; i++)
867: if (port_map & (1U << i))
868: n++;
869:
870: if (nports != n) {
1.1.1.2 ! root 871: printk("ahci: %02u:%02u.%u: Odd number of ports %u, assuming %u is correct\n", bus, dev, fun, n, nports);
1.1 root 872: port_map = 0;
873: }
874: if (!port_map) {
875: port_map = (1U << nports) - 1;
876: }
877:
878: for (i = 0; i < AHCI_MAX_PORTS; i++) {
879: u32 ssts;
880:
881: if (!(port_map & (1U << i)))
882: continue;
883:
884: ahci_port = &ahci_host->ports[i];
885:
886: ssts = readl(&ahci_port->ssts);
887: if ((ssts & 0xf) != 0x3)
888: /* Device not present */
889: continue;
890: if (((ssts >> 8) & 0xf) != 0x1)
891: /* Device down */
892: continue;
893:
894: /* OK! Probe this port */
895: ahci_probe_port(ahci_host, ahci_port);
896: }
897: }
898:
899: /* genhd callback to set size of disks */
900: static void ahci_geninit(struct gendisk *gd)
901: {
902: unsigned unit;
903: struct port *port;
904:
905: for (unit = 0; unit < gd->nr_real; unit++) {
906: port = &ports[unit];
907: port->part[0].nr_sects = port->capacity;
1.1.1.2 ! root 908: if (!port->part[0].nr_sects)
! 909: port->part[0].nr_sects = -1;
1.1 root 910: }
911: }
912:
913: /* Probe all AHCI PCI devices */
914: void ahci_probe_pci(void)
915: {
916: unsigned char bus, device;
917: unsigned short index;
918: int ret;
919: unsigned nports, unit, nminors;
920: struct port *port;
921: struct gendisk *gd, **gdp;
922: int *bs;
923:
924: for (index = 0;
925: (ret = pcibios_find_class(PCI_CLASS_STORAGE_SATA_AHCI, index, &bus, &device)) == PCIBIOS_SUCCESSFUL;
926: index++)
927: {
928: /* Note: this prevents from also having a SCSI controler.
929: * It shouldn't harm too much until we have proper hardware
930: * enumeration.
931: */
932: if (register_blkdev(MAJOR_NR, "sd", &ahci_fops) < 0)
933: printk("could not register ahci\n");
934: ahci_probe_dev(bus, device);
935: }
936:
937: for (nports = 0, port = &ports[0]; port < &ports[MAX_PORTS]; port++)
938: if (port->ahci_port)
939: nports++;
940:
941: nminors = nports * (1<<PARTN_BITS);
942:
943: gd = kmalloc(sizeof(*gd), GFP_KERNEL);
944: gd->sizes = kmalloc(nminors * sizeof(*gd->sizes), GFP_KERNEL);
945: gd->part = kmalloc(nminors * sizeof(*gd->part), GFP_KERNEL);
946: bs = kmalloc(nminors * sizeof(*bs), GFP_KERNEL);
947:
948: blksize_size[MAJOR_NR] = bs;
949: for (unit = 0; unit < nminors; unit++)
950: /* We prefer to transfer whole pages */
951: *bs++ = PAGE_SIZE;
952:
953: memset(gd->part, 0, nminors * sizeof(*gd->part));
954:
955: for (unit = 0; unit < nports; unit++) {
956: ports[unit].gd = gd;
957: ports[unit].part = &gd->part[unit << PARTN_BITS];
958: }
959:
960: gd->major = MAJOR_NR;
961: gd->major_name = "sd";
962: gd->minor_shift = PARTN_BITS;
963: gd->max_p = 1<<PARTN_BITS;
964: gd->max_nr = nports;
965: gd->nr_real = nports;
966: gd->init = ahci_geninit;
967: gd->next = NULL;
968:
969: for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
970: ;
971: *gdp = gd;
972:
973: blk_dev[MAJOR_NR].request_fn = ahci_do_request;
974: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.