blob: fac607ca228e2b34aef7eea9c31e7096bc81bc39 [file] [log] [blame]
Hank Janssenab057782009-07-13 15:19:28 -07001/*
2 *
3 * Copyright (c) 2009, Microsoft Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 *
22 */
23
24
25#ifndef _STORVSC_API_H_
26#define _STORVSC_API_H_
27
K. Y. Srinivasanced01b02011-03-28 09:33:29 -070028#include <linux/kernel.h>
K. Y. Srinivasan604df372011-05-10 07:54:07 -070029#include <linux/wait.h>
K. Y. Srinivasan036bbed2011-03-23 10:50:20 -070030#include "vstorage.h"
Greg Kroah-Hartman447fc672010-05-05 22:43:02 -070031#include "vmbus_api.h"
K. Y. Srinivasan0724e5f2011-04-26 09:20:18 -070032#include "vmbus.h"
Hank Janssenab057782009-07-13 15:19:28 -070033
Bill Pemberton454f18a2009-07-27 16:47:24 -040034/* Defines */
Hank Janssen15dd1c92010-08-05 19:30:31 +000035#define STORVSC_RING_BUFFER_SIZE (20*PAGE_SIZE)
Greg Kroah-Hartman9bcd7862009-08-28 16:19:17 -070036#define BLKVSC_RING_BUFFER_SIZE (20*PAGE_SIZE)
Hank Janssenab057782009-07-13 15:19:28 -070037
Hank Janssen15dd1c92010-08-05 19:30:31 +000038#define STORVSC_MAX_IO_REQUESTS 128
Hank Janssenab057782009-07-13 15:19:28 -070039
Bill Pemberton454f18a2009-07-27 16:47:24 -040040/*
41 * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
42 * reality, the path/target is not used (ie always set to 0) so our
43 * scsi host adapter essentially has 1 bus with 1 target that contains
44 * up to 256 luns.
45 */
Hank Janssenab057782009-07-13 15:19:28 -070046#define STORVSC_MAX_LUNS_PER_TARGET 64
Greg Kroah-Hartman9bcd7862009-08-28 16:19:17 -070047#define STORVSC_MAX_TARGETS 1
Hank Janssenab057782009-07-13 15:19:28 -070048#define STORVSC_MAX_CHANNELS 1
49
Nicolas Palix0b3f6832009-07-29 14:10:19 +020050struct hv_storvsc_request;
Hank Janssenab057782009-07-13 15:19:28 -070051
Bill Pemberton454f18a2009-07-27 16:47:24 -040052/* Matches Windows-end */
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -070053enum storvsc_request_type{
Hank Janssenab057782009-07-13 15:19:28 -070054 WRITE_TYPE,
55 READ_TYPE,
56 UNKNOWN_TYPE,
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -070057};
Hank Janssenab057782009-07-13 15:19:28 -070058
K. Y. Srinivasanbf587a12011-03-28 09:33:26 -070059
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -070060struct hv_storvsc_request {
K. Y. Srinivasanbf587a12011-03-28 09:33:26 -070061 struct hv_storvsc_request *request;
62 struct hv_device *device;
63
64 /* Synchronize the request/response if needed */
K. Y. Srinivasan939584652011-03-28 09:33:30 -070065 struct completion wait_event;
K. Y. Srinivasanbf587a12011-03-28 09:33:26 -070066
K. Y. Srinivasan237ecd62011-03-28 09:33:39 -070067 unsigned char *sense_buffer;
K. Y. Srinivasanefb5a662011-03-28 09:33:40 -070068 void *context;
K. Y. Srinivasan0bb71122011-03-28 09:33:41 -070069 void (*on_io_completion)(struct hv_storvsc_request *request);
K. Y. Srinivasane9e936c2011-03-28 09:33:43 -070070 struct hv_multipage_buffer data_buffer;
K. Y. Srinivasan237ecd62011-03-28 09:33:39 -070071
K. Y. Srinivasanbf587a12011-03-28 09:33:26 -070072 struct vstor_packet vstor_packet;
73};
74
Hank Janssenab057782009-07-13 15:19:28 -070075
Bill Pemberton454f18a2009-07-27 16:47:24 -040076/* Represents the block vsc driver */
K. Y. Srinivasan2e795052011-05-10 07:53:52 -070077struct storvsc_driver {
Hank Janssen8a046022010-12-06 12:26:45 -080078 struct hv_driver base;
Hank Janssenab057782009-07-13 15:19:28 -070079
Bill Pemberton454f18a2009-07-27 16:47:24 -040080 /* Set by caller (in bytes) */
Hank Janssen8a046022010-12-06 12:26:45 -080081 u32 ring_buffer_size;
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -070082};
Hank Janssenab057782009-07-13 15:19:28 -070083
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -070084struct storvsc_device_info {
K. Y. Srinivasan85cf3a92011-05-10 07:56:00 -070085 u32 ring_buffer_size;
Hank Janssen8a046022010-12-06 12:26:45 -080086 unsigned int port_number;
87 unsigned char path_id;
88 unsigned char target_id;
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -070089};
Hank Janssenab057782009-07-13 15:19:28 -070090
K. Y. Srinivasan1f91bca2011-04-22 14:55:25 -070091struct storvsc_major_info {
92 int major;
93 int index;
94 bool do_register;
95 char *devname;
96 char *diskname;
97};
98
K. Y. Srinivasanf24eeb02011-03-23 10:50:21 -070099/* A storvsc device is a device object that contains a vmbus channel */
100struct storvsc_device {
101 struct hv_device *device;
102
103 /* 0 indicates the device is being destroyed */
104 atomic_t ref_count;
105
K. Y. Srinivasan604df372011-05-10 07:54:07 -0700106 bool drain_notify;
K. Y. Srinivasanf24eeb02011-03-23 10:50:21 -0700107 atomic_t num_outstanding_req;
108
K. Y. Srinivasan604df372011-05-10 07:54:07 -0700109 wait_queue_head_t waiting_to_drain;
110
K. Y. Srinivasanf24eeb02011-03-23 10:50:21 -0700111 /*
112 * Each unique Port/Path/Target represents 1 channel ie scsi
113 * controller. In reality, the pathid, targetid is always 0
114 * and the port is set by us
115 */
116 unsigned int port_number;
117 unsigned char path_id;
118 unsigned char target_id;
119
K. Y. Srinivasanf24eeb02011-03-23 10:50:21 -0700120 /* Used for vsc/vsp channel reset process */
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700121 struct hv_storvsc_request init_request;
122 struct hv_storvsc_request reset_request;
K. Y. Srinivasanf24eeb02011-03-23 10:50:21 -0700123};
124
K. Y. Srinivasan852c16d2011-03-23 10:50:31 -0700125
126/* Get the stordevice object iff exists and its refcount > 1 */
127static inline struct storvsc_device *get_stor_device(struct hv_device *device)
128{
129 struct storvsc_device *stor_device;
130
131 stor_device = (struct storvsc_device *)device->ext;
132 if (stor_device && atomic_read(&stor_device->ref_count) > 1)
133 atomic_inc(&stor_device->ref_count);
134 else
135 stor_device = NULL;
136
137 return stor_device;
138}
139
K. Y. Srinivasan1604b1b2011-03-23 10:50:32 -0700140
141static inline void put_stor_device(struct hv_device *device)
142{
143 struct storvsc_device *stor_device;
144
145 stor_device = (struct storvsc_device *)device->ext;
146
147 atomic_dec(&stor_device->ref_count);
148}
149
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700150static inline struct storvsc_driver *hvdr_to_stordr(struct hv_driver *d)
K. Y. Srinivasanced01b02011-03-28 09:33:29 -0700151{
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700152 return container_of(d, struct storvsc_driver, base);
K. Y. Srinivasanced01b02011-03-28 09:33:29 -0700153}
154
K. Y. Srinivasan0724e5f2011-04-26 09:20:18 -0700155static inline
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700156struct storvsc_driver *drv_to_stordrv(struct device_driver *d)
K. Y. Srinivasan0724e5f2011-04-26 09:20:18 -0700157{
158 struct hv_driver *hvdrv = drv_to_hv_drv(d);
159 return hvdr_to_stordr(hvdrv);
160}
161
K. Y. Srinivasan604df372011-05-10 07:54:07 -0700162static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
163{
164 dev->drain_notify = true;
165 wait_event(dev->waiting_to_drain,
166 atomic_read(&dev->num_outstanding_req) == 0);
167 dev->drain_notify = false;
168}
169
Bill Pemberton454f18a2009-07-27 16:47:24 -0400170/* Interface */
171
K. Y. Srinivasan2ac5dad2011-05-10 07:53:57 -0700172int storvsc_dev_add(struct hv_device *device,
K. Y. Srinivasanafa3f3d2011-03-23 10:50:23 -0700173 void *additional_info);
K. Y. Srinivasancb706b042011-05-10 07:53:58 -0700174int storvsc_dev_remove(struct hv_device *device);
K. Y. Srinivasanafa3f3d2011-03-23 10:50:23 -0700175
K. Y. Srinivasanbb465922011-05-10 07:54:02 -0700176int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasanafa3f3d2011-03-23 10:50:23 -0700177 struct hv_storvsc_request *request);
K. Y. Srinivasanafa3f3d2011-03-23 10:50:23 -0700178
K. Y. Srinivasan43c51f72011-05-10 07:54:06 -0700179int storvsc_get_major_info(struct storvsc_device_info *device_info,
K. Y. Srinivasan1f91bca2011-04-22 14:55:25 -0700180 struct storvsc_major_info *major_info);
K. Y. Srinivasanafa3f3d2011-03-23 10:50:23 -0700181
Bill Pemberton454f18a2009-07-27 16:47:24 -0400182#endif /* _STORVSC_API_H_ */