aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/scsi_lib.c
diff options
context:
space:
mode:
authorJeff Garzik <jeff@garzik.org>2007-10-29 17:15:22 -0400
committerJeff Garzik <jeff@garzik.org>2007-11-03 22:23:02 -0400
commita341cd0f6a0fde1f85fec9aa8f81f824ea4a3f92 (patch)
treee96b2ab04c94cb1a29d972b135dd6b2bdfac0f78 /drivers/scsi/scsi_lib.c
parentb4f555081fdd27d13e6ff39d455d5aefae9d2c0c (diff)
SCSI: add asynchronous event notification API
Originally based on a patch by Kristen Carlson Accardi @ Intel. Copious input from James Bottomley. Signed-off-by: Jeff Garzik <jgarzik@redhat.com>
Diffstat (limited to 'drivers/scsi/scsi_lib.c')
-rw-r--r--drivers/scsi/scsi_lib.c136
1 files changed, 136 insertions, 0 deletions
diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 88de771d3569..0e81e4cf8876 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -2115,6 +2115,142 @@ scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
2115EXPORT_SYMBOL(scsi_device_set_state); 2115EXPORT_SYMBOL(scsi_device_set_state);
2116 2116
2117/** 2117/**
2118 * sdev_evt_emit - emit a single SCSI device uevent
2119 * @sdev: associated SCSI device
2120 * @evt: event to emit
2121 *
2122 * Send a single uevent (scsi_event) to the associated scsi_device.
2123 */
2124static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
2125{
2126 int idx = 0;
2127 char *envp[3];
2128
2129 switch (evt->evt_type) {
2130 case SDEV_EVT_MEDIA_CHANGE:
2131 envp[idx++] = "SDEV_MEDIA_CHANGE=1";
2132 break;
2133
2134 default:
2135 /* do nothing */
2136 break;
2137 }
2138
2139 envp[idx++] = NULL;
2140
2141 kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
2142}
2143
2144/**
2145 * sdev_evt_thread - send a uevent for each scsi event
2146 * @work: work struct for scsi_device
2147 *
2148 * Dispatch queued events to their associated scsi_device kobjects
2149 * as uevents.
2150 */
2151void scsi_evt_thread(struct work_struct *work)
2152{
2153 struct scsi_device *sdev;
2154 LIST_HEAD(event_list);
2155
2156 sdev = container_of(work, struct scsi_device, event_work);
2157
2158 while (1) {
2159 struct scsi_event *evt;
2160 struct list_head *this, *tmp;
2161 unsigned long flags;
2162
2163 spin_lock_irqsave(&sdev->list_lock, flags);
2164 list_splice_init(&sdev->event_list, &event_list);
2165 spin_unlock_irqrestore(&sdev->list_lock, flags);
2166
2167 if (list_empty(&event_list))
2168 break;
2169
2170 list_for_each_safe(this, tmp, &event_list) {
2171 evt = list_entry(this, struct scsi_event, node);
2172 list_del(&evt->node);
2173 scsi_evt_emit(sdev, evt);
2174 kfree(evt);
2175 }
2176 }
2177}
2178
2179/**
2180 * sdev_evt_send - send asserted event to uevent thread
2181 * @sdev: scsi_device event occurred on
2182 * @evt: event to send
2183 *
2184 * Assert scsi device event asynchronously.
2185 */
2186void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
2187{
2188 unsigned long flags;
2189
2190 if (!test_bit(evt->evt_type, sdev->supported_events)) {
2191 kfree(evt);
2192 return;
2193 }
2194
2195 spin_lock_irqsave(&sdev->list_lock, flags);
2196 list_add_tail(&evt->node, &sdev->event_list);
2197 schedule_work(&sdev->event_work);
2198 spin_unlock_irqrestore(&sdev->list_lock, flags);
2199}
2200EXPORT_SYMBOL_GPL(sdev_evt_send);
2201
2202/**
2203 * sdev_evt_alloc - allocate a new scsi event
2204 * @evt_type: type of event to allocate
2205 * @gfpflags: GFP flags for allocation
2206 *
2207 * Allocates and returns a new scsi_event.
2208 */
2209struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
2210 gfp_t gfpflags)
2211{
2212 struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
2213 if (!evt)
2214 return NULL;
2215
2216 evt->evt_type = evt_type;
2217 INIT_LIST_HEAD(&evt->node);
2218
2219 /* evt_type-specific initialization, if any */
2220 switch (evt_type) {
2221 case SDEV_EVT_MEDIA_CHANGE:
2222 default:
2223 /* do nothing */
2224 break;
2225 }
2226
2227 return evt;
2228}
2229EXPORT_SYMBOL_GPL(sdev_evt_alloc);
2230
2231/**
2232 * sdev_evt_send_simple - send asserted event to uevent thread
2233 * @sdev: scsi_device event occurred on
2234 * @evt_type: type of event to send
2235 * @gfpflags: GFP flags for allocation
2236 *
2237 * Assert scsi device event asynchronously, given an event type.
2238 */
2239void sdev_evt_send_simple(struct scsi_device *sdev,
2240 enum scsi_device_event evt_type, gfp_t gfpflags)
2241{
2242 struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
2243 if (!evt) {
2244 sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
2245 evt_type);
2246 return;
2247 }
2248
2249 sdev_evt_send(sdev, evt);
2250}
2251EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
2252
2253/**
2118 * scsi_device_quiesce - Block user issued commands. 2254 * scsi_device_quiesce - Block user issued commands.
2119 * @sdev: scsi device to quiesce. 2255 * @sdev: scsi device to quiesce.
2120 * 2256 *