1 /* visorchannel_funcs.c
3 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
19 * This provides Supervisor channel communication primitives, which are
20 * independent of the mechanism used to access the channel data. All channel
21 * data is accessed using the memregion abstraction. (memregion has both
22 * a CM2 implementation and a direct memory implementation.)
26 #include "visorchannel.h"
27 #include <linux/uuid.h>
29 #define MYDRVNAME "visorchannel"
32 struct memregion *memregion; /* from visor_memregion_create() */
33 struct channel_header chan_hdr;
36 BOOL needs_lock; /* channel creator knows if more than one
37 * thread will be inserting or removing */
38 spinlock_t insert_lock; /* protect head writes in chan_hdr */
39 spinlock_t remove_lock; /* protect tail writes in chan_hdr */
42 struct signal_queue_header req_queue;
43 struct signal_queue_header rsp_queue;
44 struct signal_queue_header event_queue;
45 struct signal_queue_header ack_queue;
49 /* Creates the struct visorchannel abstraction for a data area in memory,
50 * but does NOT modify this data area.
52 static struct visorchannel *
53 visorchannel_create_guts(HOSTADDRESS physaddr, ulong channel_bytes,
54 struct visorchannel *parent, ulong off, uuid_le guid,
57 struct visorchannel *p = NULL;
60 p = kmalloc(sizeof(*p), GFP_KERNEL|__GFP_NORETRY);
66 p->needs_lock = needs_lock;
67 spin_lock_init(&p->insert_lock);
68 spin_lock_init(&p->remove_lock);
70 /* prepare chan_hdr (abstraction to read/write channel memory) */
73 visor_memregion_create(physaddr,
74 sizeof(struct channel_header));
77 visor_memregion_create_overlapped(parent->memregion,
78 off, sizeof(struct channel_header));
83 if (visor_memregion_read(p->memregion, 0, &p->chan_hdr,
84 sizeof(struct channel_header)) < 0) {
88 if (channel_bytes == 0)
89 /* we had better be a CLIENT of this channel */
90 channel_bytes = (ulong)p->chan_hdr.size;
91 if (uuid_le_cmp(guid, NULL_UUID_LE) == 0)
92 /* we had better be a CLIENT of this channel */
93 guid = p->chan_hdr.chtype;
94 if (visor_memregion_resize(p->memregion, channel_bytes) < 0) {
98 p->size = channel_bytes;
106 visorchannel_destroy(p);
113 struct visorchannel *
114 visorchannel_create(HOSTADDRESS physaddr, ulong channel_bytes, uuid_le guid)
116 return visorchannel_create_guts(physaddr, channel_bytes, NULL, 0, guid,
119 EXPORT_SYMBOL_GPL(visorchannel_create);
121 struct visorchannel *
122 visorchannel_create_with_lock(HOSTADDRESS physaddr, ulong channel_bytes,
125 return visorchannel_create_guts(physaddr, channel_bytes, NULL, 0, guid,
128 EXPORT_SYMBOL_GPL(visorchannel_create_with_lock);
130 struct visorchannel *
131 visorchannel_create_overlapped(ulong channel_bytes,
132 struct visorchannel *parent, ulong off,
135 return visorchannel_create_guts(0, channel_bytes, parent, off, guid,
138 EXPORT_SYMBOL_GPL(visorchannel_create_overlapped);
140 struct visorchannel *
141 visorchannel_create_overlapped_with_lock(ulong channel_bytes,
142 struct visorchannel *parent, ulong off,
145 return visorchannel_create_guts(0, channel_bytes, parent, off, guid,
148 EXPORT_SYMBOL_GPL(visorchannel_create_overlapped_with_lock);
151 visorchannel_destroy(struct visorchannel *channel)
155 if (channel->memregion) {
156 visor_memregion_destroy(channel->memregion);
157 channel->memregion = NULL;
161 EXPORT_SYMBOL_GPL(visorchannel_destroy);
164 visorchannel_get_physaddr(struct visorchannel *channel)
166 return visor_memregion_get_physaddr(channel->memregion);
168 EXPORT_SYMBOL_GPL(visorchannel_get_physaddr);
171 visorchannel_get_nbytes(struct visorchannel *channel)
173 return channel->size;
175 EXPORT_SYMBOL_GPL(visorchannel_get_nbytes);
178 visorchannel_uuid_id(uuid_le *guid, char *s)
180 sprintf(s, "%pUL", guid);
183 EXPORT_SYMBOL_GPL(visorchannel_uuid_id);
186 visorchannel_id(struct visorchannel *channel, char *s)
188 return visorchannel_uuid_id(&channel->guid, s);
190 EXPORT_SYMBOL_GPL(visorchannel_id);
193 visorchannel_zoneid(struct visorchannel *channel, char *s)
195 return visorchannel_uuid_id(&channel->chan_hdr.zone_uuid, s);
197 EXPORT_SYMBOL_GPL(visorchannel_zoneid);
200 visorchannel_get_clientpartition(struct visorchannel *channel)
202 return channel->chan_hdr.partition_handle;
204 EXPORT_SYMBOL_GPL(visorchannel_get_clientpartition);
207 visorchannel_get_uuid(struct visorchannel *channel)
209 return channel->guid;
211 EXPORT_SYMBOL_GPL(visorchannel_get_uuid);
214 visorchannel_get_memregion(struct visorchannel *channel)
216 return channel->memregion;
218 EXPORT_SYMBOL_GPL(visorchannel_get_memregion);
221 visorchannel_read(struct visorchannel *channel, ulong offset,
222 void *local, ulong nbytes)
224 int rc = visor_memregion_read(channel->memregion, offset,
226 if ((rc >= 0) && (offset == 0) &&
227 (nbytes >= sizeof(struct channel_header))) {
228 memcpy(&channel->chan_hdr, local,
229 sizeof(struct channel_header));
233 EXPORT_SYMBOL_GPL(visorchannel_read);
236 visorchannel_write(struct visorchannel *channel, ulong offset,
237 void *local, ulong nbytes)
239 if (offset == 0 && nbytes >= sizeof(struct channel_header))
240 memcpy(&channel->chan_hdr, local,
241 sizeof(struct channel_header));
242 return visor_memregion_write(channel->memregion, offset, local, nbytes);
244 EXPORT_SYMBOL_GPL(visorchannel_write);
247 visorchannel_clear(struct visorchannel *channel, ulong offset, u8 ch,
253 u8 *buf = vmalloc(bufsize);
258 memset(buf, ch, bufsize);
260 ulong thisbytes = bufsize;
263 if (nbytes < thisbytes)
265 x = visor_memregion_write(channel->memregion, offset + written,
271 written += thisbytes;
283 EXPORT_SYMBOL_GPL(visorchannel_clear);
286 visorchannel_get_header(struct visorchannel *channel)
288 return (void __iomem *)&channel->chan_hdr;
290 EXPORT_SYMBOL_GPL(visorchannel_get_header);
292 /** Return offset of a specific SIGNAL_QUEUE_HEADER from the beginning of a
295 #define SIG_QUEUE_OFFSET(chan_hdr, q) \
296 ((chan_hdr)->ch_space_offset + \
297 ((q) * sizeof(struct signal_queue_header)))
299 /** Return offset of a specific queue entry (data) from the beginning of a
302 #define SIG_DATA_OFFSET(chan_hdr, q, sig_hdr, slot) \
303 (SIG_QUEUE_OFFSET(chan_hdr, q) + (sig_hdr)->sig_base_offset + \
304 ((slot) * (sig_hdr)->signal_size))
306 /** Write the contents of a specific field within a SIGNAL_QUEUE_HEADER back
309 #define SIG_WRITE_FIELD(channel, queue, sig_hdr, FIELD) \
310 (visor_memregion_write(channel->memregion, \
311 SIG_QUEUE_OFFSET(&channel->chan_hdr, queue)+ \
312 offsetof(struct signal_queue_header, FIELD),\
313 &((sig_hdr)->FIELD), \
314 sizeof((sig_hdr)->FIELD)) >= 0)
317 sig_read_header(struct visorchannel *channel, u32 queue,
318 struct signal_queue_header *sig_hdr)
322 if (channel->chan_hdr.ch_space_offset < sizeof(struct channel_header))
325 /* Read the appropriate SIGNAL_QUEUE_HEADER into local memory. */
327 if (visor_memregion_read(channel->memregion,
328 SIG_QUEUE_OFFSET(&channel->chan_hdr, queue),
330 sizeof(struct signal_queue_header)) < 0) {
339 sig_do_data(struct visorchannel *channel, u32 queue,
340 struct signal_queue_header *sig_hdr, u32 slot, void *data,
344 int signal_data_offset = SIG_DATA_OFFSET(&channel->chan_hdr, queue,
347 if (visor_memregion_write(channel->memregion,
349 data, sig_hdr->signal_size) < 0) {
353 if (visor_memregion_read(channel->memregion, signal_data_offset,
354 data, sig_hdr->signal_size) < 0) {
364 sig_read_data(struct visorchannel *channel, u32 queue,
365 struct signal_queue_header *sig_hdr, u32 slot, void *data)
367 return sig_do_data(channel, queue, sig_hdr, slot, data, FALSE);
371 sig_write_data(struct visorchannel *channel, u32 queue,
372 struct signal_queue_header *sig_hdr, u32 slot, void *data)
374 return sig_do_data(channel, queue, sig_hdr, slot, data, TRUE);
377 static inline unsigned char
378 safe_sig_queue_validate(struct signal_queue_header *psafe_sqh,
379 struct signal_queue_header *punsafe_sqh,
380 u32 *phead, u32 *ptail)
382 if ((*phead >= psafe_sqh->max_slots) ||
383 (*ptail >= psafe_sqh->max_slots)) {
384 /* Choose 0 or max, maybe based on current tail value */
388 /* Sync with client as necessary */
389 punsafe_sqh->head = *phead;
390 punsafe_sqh->tail = *ptail;
395 } /* end safe_sig_queue_validate */
398 signalremove_inner(struct visorchannel *channel, u32 queue, void *msg)
400 struct signal_queue_header sig_hdr;
402 if (!sig_read_header(channel, queue, &sig_hdr))
404 if (sig_hdr.head == sig_hdr.tail)
405 return FALSE; /* no signals to remove */
407 sig_hdr.tail = (sig_hdr.tail + 1) % sig_hdr.max_slots;
408 if (!sig_read_data(channel, queue, &sig_hdr, sig_hdr.tail, msg)) {
411 sig_hdr.num_received++;
413 /* For each data field in SIGNAL_QUEUE_HEADER that was modified,
414 * update host memory.
416 mb(); /* required for channel synch */
417 if (!SIG_WRITE_FIELD(channel, queue, &sig_hdr, tail))
419 if (!SIG_WRITE_FIELD(channel, queue, &sig_hdr, num_received))
425 visorchannel_signalremove(struct visorchannel *channel, u32 queue, void *msg)
429 if (channel->needs_lock) {
430 spin_lock(&channel->remove_lock);
431 rc = signalremove_inner(channel, queue, msg);
432 spin_unlock(&channel->remove_lock);
434 rc = signalremove_inner(channel, queue, msg);
439 EXPORT_SYMBOL_GPL(visorchannel_signalremove);
442 signalinsert_inner(struct visorchannel *channel, u32 queue, void *msg)
444 struct signal_queue_header sig_hdr;
446 if (!sig_read_header(channel, queue, &sig_hdr))
449 sig_hdr.head = ((sig_hdr.head + 1) % sig_hdr.max_slots);
450 if (sig_hdr.head == sig_hdr.tail) {
451 sig_hdr.num_overflows++;
452 visor_memregion_write(channel->memregion,
453 SIG_QUEUE_OFFSET(&channel->chan_hdr,
455 offsetof(struct signal_queue_header,
457 &(sig_hdr.num_overflows),
458 sizeof(sig_hdr.num_overflows));
462 if (!sig_write_data(channel, queue, &sig_hdr, sig_hdr.head, msg))
467 /* For each data field in SIGNAL_QUEUE_HEADER that was modified,
468 * update host memory.
470 mb(); /* required for channel synch */
471 if (!SIG_WRITE_FIELD(channel, queue, &sig_hdr, head))
473 if (!SIG_WRITE_FIELD(channel, queue, &sig_hdr, num_sent)) {
481 visorchannel_signalinsert(struct visorchannel *channel, u32 queue, void *msg)
485 if (channel->needs_lock) {
486 spin_lock(&channel->insert_lock);
487 rc = signalinsert_inner(channel, queue, msg);
488 spin_unlock(&channel->insert_lock);
490 rc = signalinsert_inner(channel, queue, msg);
495 EXPORT_SYMBOL_GPL(visorchannel_signalinsert);
498 visorchannel_signalqueue_slots_avail(struct visorchannel *channel, u32 queue)
500 struct signal_queue_header sig_hdr;
501 u32 slots_avail, slots_used;
504 if (!sig_read_header(channel, queue, &sig_hdr))
509 head = head + sig_hdr.max_slots;
510 slots_used = (head - tail);
511 slots_avail = sig_hdr.max_signals - slots_used;
512 return (int)slots_avail;
514 EXPORT_SYMBOL_GPL(visorchannel_signalqueue_slots_avail);
517 visorchannel_signalqueue_max_slots(struct visorchannel *channel, u32 queue)
519 struct signal_queue_header sig_hdr;
521 if (!sig_read_header(channel, queue, &sig_hdr))
523 return (int)sig_hdr.max_signals;
525 EXPORT_SYMBOL_GPL(visorchannel_signalqueue_max_slots);
528 sigqueue_debug(struct signal_queue_header *q, int which, struct seq_file *seq)
530 seq_printf(seq, "Signal Queue #%d\n", which);
531 seq_printf(seq, " VersionId = %lu\n", (ulong)q->version);
532 seq_printf(seq, " Type = %lu\n", (ulong)q->chtype);
533 seq_printf(seq, " oSignalBase = %llu\n",
534 (long long)q->sig_base_offset);
535 seq_printf(seq, " SignalSize = %lu\n", (ulong)q->signal_size);
536 seq_printf(seq, " MaxSignalSlots = %lu\n",
537 (ulong)q->max_slots);
538 seq_printf(seq, " MaxSignals = %lu\n", (ulong)q->max_signals);
539 seq_printf(seq, " FeatureFlags = %-16.16Lx\n",
540 (long long)q->features);
541 seq_printf(seq, " NumSignalsSent = %llu\n",
542 (long long)q->num_sent);
543 seq_printf(seq, " NumSignalsReceived = %llu\n",
544 (long long)q->num_received);
545 seq_printf(seq, " NumOverflows = %llu\n",
546 (long long)q->num_overflows);
547 seq_printf(seq, " Head = %lu\n", (ulong)q->head);
548 seq_printf(seq, " Tail = %lu\n", (ulong)q->tail);
552 visorchannel_debug(struct visorchannel *channel, int num_queues,
553 struct seq_file *seq, u32 off)
555 HOSTADDRESS addr = 0;
556 ulong nbytes = 0, nbytes_region = 0;
557 struct memregion *memregion = NULL;
558 struct channel_header hdr;
559 struct channel_header *phdr = &hdr;
565 memregion = channel->memregion;
569 addr = visor_memregion_get_physaddr(memregion);
570 nbytes_region = visor_memregion_get_nbytes(memregion);
571 errcode = visorchannel_read(channel, off,
572 phdr, sizeof(struct channel_header));
575 "Read of channel header failed with errcode=%d)\n",
578 phdr = &channel->chan_hdr;
579 seq_puts(seq, "(following data may be stale)\n");
584 nbytes = (ulong)(phdr->size);
585 seq_printf(seq, "--- Begin channel @0x%-16.16Lx for 0x%lx bytes (region=0x%lx bytes) ---\n",
586 addr + off, nbytes, nbytes_region);
587 seq_printf(seq, "Type = %pUL\n", &phdr->chtype);
588 seq_printf(seq, "ZoneGuid = %pUL\n", &phdr->zone_uuid);
589 seq_printf(seq, "Signature = 0x%-16.16Lx\n",
590 (long long)phdr->signature);
591 seq_printf(seq, "LegacyState = %lu\n", (ulong)phdr->legacy_state);
592 seq_printf(seq, "SrvState = %lu\n", (ulong)phdr->srv_state);
593 seq_printf(seq, "CliStateBoot = %lu\n", (ulong)phdr->cli_state_boot);
594 seq_printf(seq, "CliStateOS = %lu\n", (ulong)phdr->cli_state_os);
595 seq_printf(seq, "HeaderSize = %lu\n", (ulong)phdr->header_size);
596 seq_printf(seq, "Size = %llu\n", (long long)phdr->size);
597 seq_printf(seq, "Features = 0x%-16.16llx\n",
598 (long long)phdr->features);
599 seq_printf(seq, "PartitionHandle = 0x%-16.16llx\n",
600 (long long)phdr->partition_handle);
601 seq_printf(seq, "Handle = 0x%-16.16llx\n",
602 (long long)phdr->handle);
603 seq_printf(seq, "VersionId = %lu\n", (ulong)phdr->version_id);
604 seq_printf(seq, "oChannelSpace = %llu\n",
605 (long long)phdr->ch_space_offset);
606 if ((phdr->ch_space_offset == 0) || (errcode < 0))
609 for (i = 0; i < num_queues; i++) {
610 struct signal_queue_header q;
612 errcode = visorchannel_read(channel,
614 phdr->ch_space_offset +
619 "failed to read signal queue #%d from channel @0x%-16.16Lx errcode=%d\n",
623 sigqueue_debug(&q, i, seq);
625 seq_printf(seq, "--- End channel @0x%-16.16Lx for 0x%lx bytes ---\n",
628 EXPORT_SYMBOL_GPL(visorchannel_debug);
631 visorchannel_dump_section(struct visorchannel *chan, char *s,
632 int off, int len, struct seq_file *seq)
634 char *buf, *tbuf, *fmtbuf;
639 fmtbufsize = 100 * COVQ(len, 16);
640 buf = kmalloc(len, GFP_KERNEL|__GFP_NORETRY);
643 fmtbuf = kmalloc(fmtbufsize, GFP_KERNEL|__GFP_NORETRY);
647 errcode = visorchannel_read(chan, off, buf, len);
650 seq_printf(seq, "channel %s:\n", s);
653 i = (len < 16) ? len : 16;
654 hex_dump_to_buffer(tbuf, i, 16, 1, fmtbuf, fmtbufsize, TRUE);
655 seq_printf(seq, "%s\n", fmtbuf);
665 EXPORT_SYMBOL_GPL(visorchannel_dump_section);