6 #include <linux/kernel.h>
7 #include <linux/export.h>
8 #include <linux/slab.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
15 #include <linux/list.h>
17 #include <linux/bitmap.h>
19 #include <asm/hypervisor.h>
20 #include <asm/iommu.h>
25 #define DRV_MODULE_NAME "ldc"
26 #define PFX DRV_MODULE_NAME ": "
27 #define DRV_MODULE_VERSION "1.1"
28 #define DRV_MODULE_RELDATE "July 22, 2008"
32 #define LDC_PACKET_SIZE 64
54 #define LDC_CTRL_MSK 0x0f
58 #define LDC_FRAG_MASK 0xc0
59 #define LDC_START 0x40
81 { .major = 1, .minor = 0 },
84 #define LDC_DEFAULT_MTU (4 * LDC_PACKET_SIZE)
85 #define LDC_DEFAULT_NUM_ENTRIES (PAGE_SIZE / LDC_PACKET_SIZE)
146 #define LDC_HS_CLOSED 0x00
147 #define LDC_HS_OPEN 0x01
148 #define LDC_HS_GOTVERS 0x02
149 #define LDC_HS_SENTRTR 0x03
150 #define LDC_HS_GOTRTR 0x04
151 #define LDC_HS_COMPLETE 0x10
154 #define LDC_FLAG_ALLOCED_QUEUES 0x01
155 #define LDC_FLAG_REGISTERED_QUEUES 0x02
156 #define LDC_FLAG_REGISTERED_IRQS 0x04
157 #define LDC_FLAG_RESET 0x10
162 #define LDC_IRQ_NAME_MAX 32
171 #define ldcdbg(TYPE, f, a...) \
172 do { if (lp->cfg.debug & LDC_DEBUG_##TYPE) \
173 printk(KERN_INFO PFX "ID[%lu] " f, lp->id, ## a); \
176 static const char *state_to_str(
u8 state)
197 state_to_str(lp->
state),
198 state_to_str(state));
203 static unsigned long __advance(
unsigned long off,
unsigned long num_entries)
212 static unsigned long rx_advance(
struct ldc_channel *lp,
unsigned long off)
217 static unsigned long tx_advance(
struct ldc_channel *lp,
unsigned long off)
223 unsigned long *new_tail)
228 t = tx_advance(lp, lp->
tx_tail);
245 static unsigned long head_for_data(
struct ldc_channel *lp)
254 unsigned long limit,
tail, new_tail, diff;
257 limit = head_for_data(lp);
259 new_tail = tx_advance(lp, tail);
260 if (new_tail == limit)
263 if (limit > new_tail)
264 diff = limit - new_tail;
271 if (diff * mss < size)
278 unsigned long *new_tail)
283 h = head_for_data(lp);
284 t = tx_advance(lp, lp->
tx_tail);
294 static int set_tx_tail(
struct ldc_channel *lp,
unsigned long tail)
296 unsigned long orig_tail = lp->
tx_tail;
300 while (limit-- > 0) {
326 while (limit-- > 0) {
344 unsigned long new_tail)
348 return set_tx_tail(lp, new_tail);
354 unsigned long *new_tail)
356 struct ldc_packet *p = handshake_get_tx_packet(lp, new_tail);
373 unsigned long new_tail;
377 ldcdbg(HS,
"SEND VER INFO maj[%u] min[%u]\n",
381 ver,
sizeof(*ver), &new_tail);
383 int err = send_tx_packet(lp, p, new_tail);
391 static int send_version_nack(
struct ldc_channel *lp,
396 unsigned long new_tail;
402 &ver,
sizeof(ver), &new_tail);
404 ldcdbg(HS,
"SEND VER NACK maj[%u] min[%u]\n",
407 return send_tx_packet(lp, p, new_tail);
412 static int send_version_ack(
struct ldc_channel *lp,
416 unsigned long new_tail;
419 vp,
sizeof(*vp), &new_tail);
421 ldcdbg(HS,
"SEND VER ACK maj[%u] min[%u]\n",
424 return send_tx_packet(lp, p, new_tail);
432 unsigned long new_tail;
441 ldcdbg(HS,
"SEND RTS env[0x%x] seqid[0x%x]\n",
444 return send_tx_packet(lp, p, new_tail);
452 unsigned long new_tail;
460 ldcdbg(HS,
"SEND RTR env[0x%x] seqid[0x%x]\n",
463 return send_tx_packet(lp, p, new_tail);
471 unsigned long new_tail;
480 ldcdbg(HS,
"SEND RDX env[0x%x] seqid[0x%x] ackid[0x%x]\n",
483 return send_tx_packet(lp, p, new_tail);
491 unsigned long new_tail;
494 p = data_get_tx_packet(lp, &new_tail);
504 ldcdbg(HS,
"SEND DATA NACK type[0x%x] ctl[0x%x] seq[0x%x] ack[0x%x]\n",
507 err = send_tx_packet(lp, p, new_tail);
516 unsigned long hv_err;
526 "sun4v_ldc_tx_qconf(%lx,%lx,%lx) failed, err=%lu\n",
535 "sun4v_ldc_tx_get_state(%lx,...) failed, err=%lu\n",
541 "sun4v_ldc_rx_qconf(%lx,%lx,%lx) failed, err=%lu\n",
553 "sun4v_ldc_rx_get_state(%lx,...) failed, err=%lu\n",
566 if (v->
major <= major) {
579 ldcdbg(HS,
"GOT VERSION INFO major[%x] minor[%x]\n",
587 vap = find_by_major(vp->
major);
589 err = send_version_nack(lp, 0, 0);
591 err = send_version_nack(lp, vap->
major, vap->
minor);
596 err = send_version_ack(lp, &ver);
603 return ldc_abort(lp);
610 ldcdbg(HS,
"GOT VERSION ACK major[%x] minor[%x]\n",
616 return ldc_abort(lp);
622 return ldc_abort(lp);
630 unsigned long new_tail;
633 return ldc_abort(lp);
635 vap = find_by_major(vp->
major);
637 return ldc_abort(lp);
643 return ldc_abort(lp);
645 return send_tx_packet(lp, p, new_tail);
657 return process_ver_info(lp, vp);
660 return process_ver_ack(lp, vp);
663 return process_ver_nack(lp, vp);
666 return ldc_abort(lp);
673 ldcdbg(HS,
"GOT RTS stype[%x] seqid[%x] env[%x]\n",
679 return ldc_abort(lp);
685 return ldc_abort(lp);
693 ldcdbg(HS,
"GOT RTR stype[%x] seqid[%x] env[%x]\n",
698 return ldc_abort(lp);
716 ldcdbg(HS,
"GOT RDX stype[%x] seqid[%x] env[%x] ackid[%x]\n",
720 !(rx_seq_ok(lp, p->
seqid)))
721 return ldc_abort(lp);
731 static int process_control_frame(
struct ldc_channel *lp,
736 return process_version(lp, p);
739 return process_rts(lp, p);
742 return process_rtr(lp, p);
745 return process_rdx(lp, p);
748 return ldc_abort(lp);
752 static int process_error_frame(
struct ldc_channel *lp,
755 return ldc_abort(lp);
758 static int process_data_ack(
struct ldc_channel *lp,
762 u32 ackid = ack->
u.
r.ackid;
767 head = tx_advance(lp, head);
769 if (p->
seqid == ackid) {
774 return ldc_abort(lp);
780 static void send_events(
struct ldc_channel *lp,
unsigned int event_mask)
793 unsigned long orig_state,
flags;
794 unsigned int event_mask;
808 ldcdbg(
RX,
"RX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n",
863 err = process_control_frame(lp, p);
874 err = process_error_frame(lp, p);
891 err = __set_rx_head(lp,
new);
893 (
void) ldc_abort(lp);
897 goto handshake_complete;
901 spin_unlock_irqrestore(&lp->
lock, flags);
903 send_events(lp, event_mask);
911 unsigned long flags, orig_state;
912 unsigned int event_mask = 0;
926 ldcdbg(
TX,
" TX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n",
937 spin_unlock_irqrestore(&lp->
lock, flags);
939 send_events(lp, event_mask);
953 static int __ldc_channel_exists(
unsigned long id)
965 static int alloc_queue(
const char *
name,
unsigned long num_entries,
977 "size=%lu order=%lu\n", name, size, order);
989 static void free_queue(
unsigned long num_entries,
struct ldc_packet *q)
1003 #define LDC_IOTABLE_SIZE (8 * 1024)
1007 unsigned long sz, num_tsb_entries, tsbsize,
order;
1010 unsigned long hv_err;
1018 sz = num_tsb_entries / 8;
1019 sz = (sz + 7
UL) & ~7
UL;
1021 if (!iommu->
arena.map) {
1026 iommu->
arena.limit = num_tsb_entries;
1035 "size=%lu order=%lu\n", tsbsize, order);
1047 goto out_free_table;
1062 static void ldc_iommu_release(
struct ldc_channel *lp)
1065 unsigned long num_tsb_entries, tsbsize,
order;
1069 num_tsb_entries = iommu->
arena.limit;
1086 unsigned long dummy1, dummy2, hv_err;
1098 switch (cfgp->
mode) {
1106 mss = LDC_PACKET_SIZE - 8;
1111 mss = LDC_PACKET_SIZE - 8 - 8;
1127 if (__ldc_channel_exists(
id))
1141 err = ldc_iommu_init(lp);
1156 goto out_free_iommu;
1172 goto out_free_mssbuf;
1184 INIT_HLIST_NODE(&lp->
list);
1185 hlist_add_head(&lp->
list, &ldc_channel_list);
1198 ldc_iommu_release(lp);
1204 return ERR_PTR(err);
1226 hlist_del(&lp->
list);
1230 ldc_iommu_release(lp);
1243 unsigned long hv_err,
flags;
1305 spin_unlock_irqrestore(&lp->
lock, flags);
1321 spin_unlock_irqrestore(&lp->
lock, flags);
1329 unsigned long flags;
1342 err = start_handshake(lp);
1344 spin_unlock_irqrestore(&lp->
lock, flags);
1352 unsigned long hv_err,
flags;
1385 spin_unlock_irqrestore(&lp->
lock, flags);
1398 spin_unlock_irqrestore(&lp->
lock, flags);
1410 static int write_raw(
struct ldc_channel *lp,
const void *
buf,
unsigned int size)
1413 unsigned long new_tail;
1416 if (size > LDC_PACKET_SIZE)
1419 p = data_get_tx_packet(lp, &new_tail);
1425 err = send_tx_packet(lp, p, new_tail);
1432 static int read_raw(
struct ldc_channel *lp,
void *buf,
unsigned int size)
1435 unsigned long hv_err,
new;
1438 if (size < LDC_PACKET_SIZE)
1446 return ldc_abort(lp);
1456 memcpy(buf, p, LDC_PACKET_SIZE);
1458 new = rx_advance(lp, lp->
rx_head);
1461 err = __set_rx_head(lp,
new);
1475 static int write_nonraw(
struct ldc_channel *lp,
const void *buf,
1478 unsigned long hv_err,
tail;
1479 unsigned int copied;
1489 return ldc_abort(lp);
1491 if (!tx_has_space_for(lp, size))
1497 while (copied < size) {
1508 data_len = size - copied;
1509 if (data_len > lp->
mss)
1514 p->
env = (data_len |
1516 (data_len == size - copied ?
LDC_STOP : 0));
1520 ldcdbg(
DATA,
"SENT DATA [%02x:%02x:%02x:%02x:%08x]\n",
1527 memcpy(data, buf, data_len);
1531 tail = tx_advance(lp, tail);
1534 err = set_tx_tail(lp, tail);
1551 err = send_data_nack(lp, p);
1555 err = __set_rx_head(lp, lp->
rx_tail);
1557 return ldc_abort(lp);
1565 int err = process_data_ack(lp, p);
1570 return ldc_abort(lp);
1575 static int rx_data_wait(
struct ldc_channel *lp,
unsigned long cur_head)
1577 unsigned long dummy;
1580 ldcdbg(
DATA,
"DATA WAIT cur_head[%lx] rx_head[%lx] rx_tail[%lx]\n",
1582 while (limit-- > 0) {
1583 unsigned long hv_err;
1590 return ldc_abort(lp);
1596 if (cur_head != lp->
rx_tail) {
1598 "head[%lx] tail[%lx] chan_state[%lx]\n",
1608 static int rx_set_head(
struct ldc_channel *lp,
unsigned long head)
1610 int err = __set_rx_head(lp, head);
1613 return ldc_abort(lp);
1621 unsigned long new_tail;
1624 p = data_get_tx_packet(lp, &new_tail);
1628 memset(p, 0,
sizeof(*p));
1635 err = send_tx_packet(lp, p, new_tail);
1641 static int read_nonraw(
struct ldc_channel *lp,
void *buf,
unsigned int size)
1644 unsigned long hv_err,
new;
1652 return ldc_abort(lp);
1671 ldcdbg(
RX,
"RX read pkt[%02x:%02x:%02x:%02x:%08x:%08x] "
1682 err = rx_bad_seq(lp, p, first_frag);
1688 err = process_control_frame(lp, p);
1697 new = rx_advance(lp,
new);
1701 err = data_ack_nack(lp, p);
1706 new = rx_advance(lp,
new);
1707 err = rx_set_head(lp,
new);
1731 new = rx_advance(lp,
new);
1733 err = rx_set_head(lp,
new);
1743 if (pkt_len > size - copied) {
1761 new = rx_advance(lp,
new);
1774 err = rx_data_wait(lp,
new);
1781 err = rx_set_head(lp,
new);
1783 if (err && first_frag)
1796 .write = write_nonraw,
1797 .read = read_nonraw,
1800 static int write_stream(
struct ldc_channel *lp,
const void *buf,
1803 if (size > lp->
cfg.mtu)
1805 return write_nonraw(lp, buf, size);
1808 static int read_stream(
struct ldc_channel *lp,
void *buf,
unsigned int size)
1811 int err = read_nonraw(lp, lp->
mssbuf, lp->
cfg.mtu);
1830 .write = write_stream,
1831 .read = read_stream,
1836 unsigned long flags;
1850 err = lp->
mops->write(lp, buf, size);
1852 spin_unlock_irqrestore(&lp->
lock, flags);
1860 unsigned long flags;
1874 err = lp->
mops->read(lp, buf, size);
1876 spin_unlock_irqrestore(&lp->
lock, flags);
1882 static long arena_alloc(
struct ldc_iommu *iommu,
unsigned long npages)
1888 limit = arena->
limit;
1889 start = arena->
hint;
1913 #define COOKIE_PGSZ_CODE 0xf000000000000000ULL
1914 #define COOKIE_PGSZ_CODE_SHIFT 60ULL
1916 static u64 pagesize_code(
void)
1920 case (8ULL * 1024ULL):
1922 case (64ULL * 1024ULL):
1924 case (512ULL * 1024ULL):
1926 case (4ULL * 1024ULL * 1024ULL):
1928 case (32ULL * 1024ULL * 1024ULL):
1930 case (256ULL * 1024ULL * 1024ULL):
1942 static u64 cookie_to_index(
u64 cookie,
unsigned long *shift)
1948 *shift = szcode * 3;
1950 return (cookie >> (13ULL + (szcode * 3ULL)));
1954 unsigned long npages)
1958 entry = arena_alloc(iommu, npages);
1965 static u64 perm_to_mte(
unsigned int map_perm)
1969 mte_base = pagesize_code();
1978 if (map_perm & LDC_MAP_R)
1986 if (map_perm & LDC_MAP_R)
1988 if (map_perm & LDC_MAP_W)
1995 static int pages_in_region(
unsigned long base,
long len)
2002 len -= (
new - base);
2019 static void fill_cookies(
struct cookie_state *
sp,
unsigned long pa,
2020 unsigned long off,
unsigned long len)
2023 unsigned long tlen,
new = pa +
PAGE_SIZE;
2030 tlen = PAGE_SIZE - off;
2034 this_cookie = make_cookie(sp->
pte_idx,
2035 pagesize_code(), off);
2040 sp->
cookies[sp->
nc - 1].cookie_size += tlen;
2042 sp->
cookies[sp->
nc].cookie_addr = this_cookie;
2060 if ((sg->
offset | len) & (8
UL - 1))
2063 return pages_in_region(base + sg->
offset, len);
2066 static int sg_count_pages(
struct scatterlist *sg,
int num_sg)
2072 for (i = 0; i < num_sg; i++) {
2073 int err = sg_count_one(sg + i);
2085 unsigned int map_perm)
2087 unsigned long i, npages,
flags;
2096 err = sg_count_pages(sg, num_sg);
2107 base = alloc_npages(iommu, npages);
2108 spin_unlock_irqrestore(&iommu->
lock, flags);
2115 state.
mte_base = perm_to_mte(map_perm);
2120 for (i = 0; i < num_sg; i++)
2129 void *buf,
unsigned int len,
2131 unsigned int map_perm)
2133 unsigned long npages, pa,
flags;
2142 if ((pa | len) & (8
UL - 1))
2145 npages = pages_in_region(pa, len);
2150 base = alloc_npages(iommu, npages);
2151 spin_unlock_irqrestore(&iommu->
lock, flags);
2158 state.
mte_base = perm_to_mte(map_perm);
2162 fill_cookies(&state, (pa &
PAGE_MASK), (pa & ~PAGE_MASK), len);
2169 static void free_npages(
unsigned long id,
struct ldc_iommu *iommu,
2173 unsigned long i, shift,
index, npages;
2177 index = cookie_to_index(cookie, &shift);
2181 (index + npages) > arena->
limit);
2183 for (i = 0; i < npages; i++) {
2196 unsigned long flags;
2200 for (i = 0; i < ncookies; i++) {
2204 free_npages(lp->
id, iommu, addr, size);
2206 spin_unlock_irqrestore(&iommu->
lock, flags);
2211 void *buf,
unsigned int len,
unsigned long offset,
2214 unsigned int orig_len;
2225 if ((ra | len | offset) & (8
UL - 1)) {
2227 "ra[%lx] len[%x] offset[%lx]\n",
2228 lp->
id, ra, len, offset);
2240 for (i = 0; i < ncookies; i++) {
2243 unsigned long actual_len;
2246 unsigned long this_off =
offset;
2248 if (this_off > this_len)
2249 this_off = this_len;
2252 this_len -= this_off;
2255 cookie_raddr += this_off;
2262 unsigned long hv_err;
2266 this_len, &actual_len);
2278 cookie_raddr += actual_len;
2281 if (actual_len == this_len)
2284 this_len -= actual_len;
2296 return orig_len - len;
2302 unsigned int map_perm)
2307 if (len & (8
UL - 1))
2314 err =
ldc_map_single(lp, buf, len, cookies, *ncookies, map_perm);
2317 return ERR_PTR(err);
2333 static int __init ldc_init(
void)
2335 unsigned long major, minor;