static int netvsc_close(struct net_device *net) { struct net_device_context *net_device_ctx = netdev_priv(net); struct netvsc_device *nvdev = net_device_ctx->nvdev; int ret = 0; u32 aread, i, msec = 10, retry = 0, retry_max = 20; struct vmbus_channel *chn; netif_tx_disable(net); /* No need to close rndis filter if it is removed already */ if (!nvdev) goto out; ret = rndis_filter_close(nvdev); if (ret != 0) { netdev_err(net, "unable to close device (ret %d).\n", ret); return ret; } /* Ensure pending bytes in ring are read */ while (true) { aread = 0; for (i = 0; i < nvdev->num_chn; i++) { chn = nvdev->chan_table[i].channel; if (!chn) continue; aread = hv_get_bytes_to_read(&chn->inbound); if (aread) break; aread = hv_get_bytes_to_read(&chn->outbound); if (aread) break; } retry++; if (retry > retry_max || aread == 0) break; msleep(msec); if (msec < 1000) msec *= 2; } if (aread) { netdev_err(net, "Ring buffer not empty after closing rndis\n"); ret = -ETIMEDOUT; } out: return ret; }
u32 hv_end_read(struct hv_ring_buffer_info *rbi) { rbi->ring_buffer->interrupt_mask = 0; virt_mb(); /* * Now check to see if the ring buffer is still empty. * If it is not, we raced and we need to process new * incoming messages. */ return hv_get_bytes_to_read(rbi); }
int hv_ringbuffer_read(struct vmbus_channel *channel, void *buffer, u32 buflen, u32 *buffer_actual_len, u64 *requestid, bool raw) { u32 bytes_avail_toread; u32 next_read_location = 0; u64 prev_indices = 0; struct vmpacket_descriptor desc; u32 offset; u32 packetlen; int ret = 0; struct hv_ring_buffer_info *inring_info = &channel->inbound; if (buflen <= 0) return -EINVAL; *buffer_actual_len = 0; *requestid = 0; bytes_avail_toread = hv_get_bytes_to_read(inring_info); /* Make sure there is something to read */ if (bytes_avail_toread < sizeof(desc)) { /* * No error is set when there is even no header, drivers are * supposed to analyze buffer_actual_len. */ return ret; } next_read_location = hv_get_next_read_location(inring_info); next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc, sizeof(desc), next_read_location); offset = raw ? 0 : (desc.offset8 << 3); packetlen = (desc.len8 << 3) - offset; *buffer_actual_len = packetlen; *requestid = desc.trans_id; if (bytes_avail_toread < packetlen + offset) return -EAGAIN; if (packetlen > buflen) return -ENOBUFS; next_read_location = hv_get_next_readlocation_withoffset(inring_info, offset); next_read_location = hv_copyfrom_ringbuffer(inring_info, buffer, packetlen, next_read_location); next_read_location = hv_copyfrom_ringbuffer(inring_info, &prev_indices, sizeof(u64), next_read_location); /* * Make sure all reads are done before we update the read index since * the writer may start writing to the read area once the read index * is updated. */ virt_mb(); /* Update the read index */ hv_set_next_read_location(inring_info, next_read_location); hv_signal_on_read(channel); return ret; }
int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer, u32 buflen, u32 *buffer_actual_len, u64 *requestid, bool *signal, bool raw) { u32 bytes_avail_toread; u32 next_read_location = 0; u64 prev_indices = 0; unsigned long flags; struct vmpacket_descriptor desc; u32 offset; u32 packetlen; int ret = 0; if (buflen <= 0) return -EINVAL; spin_lock_irqsave(&inring_info->ring_lock, flags); *buffer_actual_len = 0; *requestid = 0; bytes_avail_toread = hv_get_bytes_to_read(inring_info); /* Make sure there is something to read */ if (bytes_avail_toread < sizeof(desc)) { /* * No error is set when there is even no header, drivers are * supposed to analyze buffer_actual_len. */ goto out_unlock; } next_read_location = hv_get_next_read_location(inring_info); next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc, sizeof(desc), next_read_location); offset = raw ? 0 : (desc.offset8 << 3); packetlen = (desc.len8 << 3) - offset; *buffer_actual_len = packetlen; *requestid = desc.trans_id; if (bytes_avail_toread < packetlen + offset) { ret = -EAGAIN; goto out_unlock; } if (packetlen > buflen) { ret = -ENOBUFS; goto out_unlock; } next_read_location = hv_get_next_readlocation_withoffset(inring_info, offset); next_read_location = hv_copyfrom_ringbuffer(inring_info, buffer, packetlen, next_read_location); next_read_location = hv_copyfrom_ringbuffer(inring_info, &prev_indices, sizeof(u64), next_read_location); /* * Make sure all reads are done before we update the read index since * the writer may start writing to the read area once the read index * is updated. */ mb(); /* Update the read index */ hv_set_next_read_location(inring_info, next_read_location); *signal = hv_need_to_signal_on_read(inring_info); out_unlock: spin_unlock_irqrestore(&inring_info->ring_lock, flags); return ret; }