Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SCSI device handler infrastruture. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the |
| 6 | * Free Software Foundation; either version 2 of the License, or (at your |
| 7 | * option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | * |
| 18 | * Copyright IBM Corporation, 2007 |
| 19 | * Authors: |
| 20 | * Chandra Seetharaman <sekharan@us.ibm.com> |
| 21 | * Mike Anderson <andmike@linux.vnet.ibm.com> |
| 22 | */ |
| 23 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Paul Gortmaker | acf3368f | 2011-05-27 09:47:43 -0400 | [diff] [blame] | 25 | #include <linux/module.h> |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 26 | #include <scsi/scsi_dh.h> |
| 27 | #include "../scsi_priv.h" |
| 28 | |
| 29 | static DEFINE_SPINLOCK(list_lock); |
| 30 | static LIST_HEAD(scsi_dh_list); |
| 31 | |
| 32 | static struct scsi_device_handler *get_device_handler(const char *name) |
| 33 | { |
| 34 | struct scsi_device_handler *tmp, *found = NULL; |
| 35 | |
| 36 | spin_lock(&list_lock); |
| 37 | list_for_each_entry(tmp, &scsi_dh_list, list) { |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 38 | if (!strncmp(tmp->name, name, strlen(tmp->name))) { |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 39 | found = tmp; |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | spin_unlock(&list_lock); |
| 44 | return found; |
| 45 | } |
| 46 | |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 47 | /* |
Hannes Reinecke | 6c3633d | 2011-08-24 10:51:15 +0200 | [diff] [blame] | 48 | * device_handler_match_function - Match a device handler to a device |
| 49 | * @sdev - SCSI device to be tested |
| 50 | * |
| 51 | * Tests @sdev against the match function of all registered device_handler. |
| 52 | * Returns the found device handler or NULL if not found. |
| 53 | */ |
| 54 | static struct scsi_device_handler * |
| 55 | device_handler_match_function(struct scsi_device *sdev) |
| 56 | { |
| 57 | struct scsi_device_handler *tmp_dh, *found_dh = NULL; |
| 58 | |
| 59 | spin_lock(&list_lock); |
| 60 | list_for_each_entry(tmp_dh, &scsi_dh_list, list) { |
| 61 | if (tmp_dh->match && tmp_dh->match(sdev)) { |
| 62 | found_dh = tmp_dh; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | spin_unlock(&list_lock); |
| 67 | return found_dh; |
| 68 | } |
| 69 | |
| 70 | /* |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 71 | * device_handler_match - Attach a device handler to a device |
| 72 | * @scsi_dh - The device handler to match against or NULL |
| 73 | * @sdev - SCSI device to be tested against @scsi_dh |
| 74 | * |
| 75 | * Tests @sdev against the device handler @scsi_dh or against |
| 76 | * all registered device_handler if @scsi_dh == NULL. |
| 77 | * Returns the found device handler or NULL if not found. |
| 78 | */ |
| 79 | static struct scsi_device_handler * |
| 80 | device_handler_match(struct scsi_device_handler *scsi_dh, |
| 81 | struct scsi_device *sdev) |
| 82 | { |
Hannes Reinecke | 6c3633d | 2011-08-24 10:51:15 +0200 | [diff] [blame] | 83 | struct scsi_device_handler *found_dh; |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 84 | |
Hannes Reinecke | 6c3633d | 2011-08-24 10:51:15 +0200 | [diff] [blame] | 85 | found_dh = device_handler_match_function(sdev); |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 86 | |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 87 | if (scsi_dh && found_dh != scsi_dh) |
| 88 | found_dh = NULL; |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 89 | |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 90 | return found_dh; |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* |
| 94 | * scsi_dh_handler_attach - Attach a device handler to a device |
| 95 | * @sdev - SCSI device the device handler should attach to |
| 96 | * @scsi_dh - The device handler to attach |
| 97 | */ |
| 98 | static int scsi_dh_handler_attach(struct scsi_device *sdev, |
| 99 | struct scsi_device_handler *scsi_dh) |
| 100 | { |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 101 | struct scsi_dh_data *d; |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 102 | |
Christoph Hellwig | 1f12ffa | 2014-09-13 20:08:44 -0700 | [diff] [blame] | 103 | if (!try_module_get(scsi_dh->module)) |
| 104 | return -EINVAL; |
Christoph Hellwig | 27c888f | 2014-09-13 19:41:16 -0700 | [diff] [blame] | 105 | |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 106 | d = scsi_dh->attach(sdev); |
| 107 | if (IS_ERR(d)) { |
| 108 | sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%ld)\n", |
| 109 | scsi_dh->name, PTR_ERR(d)); |
Christoph Hellwig | 1f12ffa | 2014-09-13 20:08:44 -0700 | [diff] [blame] | 110 | module_put(scsi_dh->module); |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 111 | return PTR_ERR(d); |
Chandra Seetharaman | 6c10db7 | 2009-06-26 19:30:06 -0700 | [diff] [blame] | 112 | } |
Christoph Hellwig | 1f12ffa | 2014-09-13 20:08:44 -0700 | [diff] [blame] | 113 | |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 114 | d->scsi_dh = scsi_dh; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 115 | d->sdev = sdev; |
| 116 | |
| 117 | spin_lock_irq(sdev->request_queue->queue_lock); |
| 118 | sdev->scsi_dh_data = d; |
| 119 | spin_unlock_irq(sdev->request_queue->queue_lock); |
| 120 | return 0; |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Christoph Hellwig | 1bab0de | 2015-08-27 14:16:54 +0200 | [diff] [blame^] | 123 | /* |
| 124 | * scsi_dh_handler_detach - Detach a device handler from a device |
| 125 | * @sdev - SCSI device the device handler should be detached from |
| 126 | */ |
| 127 | static void scsi_dh_handler_detach(struct scsi_device *sdev) |
Chandra Seetharaman | 6c10db7 | 2009-06-26 19:30:06 -0700 | [diff] [blame] | 128 | { |
Christoph Hellwig | 1bab0de | 2015-08-27 14:16:54 +0200 | [diff] [blame^] | 129 | struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data; |
Christoph Hellwig | 27c888f | 2014-09-13 19:41:16 -0700 | [diff] [blame] | 130 | struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh; |
| 131 | |
Mike Christie | 28072ad | 2015-01-28 03:46:53 -0600 | [diff] [blame] | 132 | scsi_dh->detach(sdev); |
| 133 | |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 134 | spin_lock_irq(sdev->request_queue->queue_lock); |
| 135 | sdev->scsi_dh_data = NULL; |
| 136 | spin_unlock_irq(sdev->request_queue->queue_lock); |
| 137 | |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 138 | sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", scsi_dh->name); |
Christoph Hellwig | 27c888f | 2014-09-13 19:41:16 -0700 | [diff] [blame] | 139 | module_put(scsi_dh->module); |
Chandra Seetharaman | 6c10db7 | 2009-06-26 19:30:06 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 142 | /* |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 143 | * Functions for sysfs attribute 'dh_state' |
| 144 | */ |
| 145 | static ssize_t |
| 146 | store_dh_state(struct device *dev, struct device_attribute *attr, |
| 147 | const char *buf, size_t count) |
| 148 | { |
| 149 | struct scsi_device *sdev = to_scsi_device(dev); |
| 150 | struct scsi_device_handler *scsi_dh; |
| 151 | int err = -EINVAL; |
| 152 | |
Hannes Reinecke | 6bc8d2a | 2011-08-24 10:51:17 +0200 | [diff] [blame] | 153 | if (sdev->sdev_state == SDEV_CANCEL || |
| 154 | sdev->sdev_state == SDEV_DEL) |
| 155 | return -ENODEV; |
| 156 | |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 157 | if (!sdev->scsi_dh_data) { |
| 158 | /* |
| 159 | * Attach to a device handler |
| 160 | */ |
| 161 | if (!(scsi_dh = get_device_handler(buf))) |
| 162 | return err; |
| 163 | err = scsi_dh_handler_attach(sdev, scsi_dh); |
| 164 | } else { |
| 165 | scsi_dh = sdev->scsi_dh_data->scsi_dh; |
| 166 | if (!strncmp(buf, "detach", 6)) { |
| 167 | /* |
| 168 | * Detach from a device handler |
| 169 | */ |
Christoph Hellwig | 1bab0de | 2015-08-27 14:16:54 +0200 | [diff] [blame^] | 170 | scsi_dh_handler_detach(sdev); |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 171 | err = 0; |
| 172 | } else if (!strncmp(buf, "activate", 8)) { |
| 173 | /* |
| 174 | * Activate a device handler |
| 175 | */ |
| 176 | if (scsi_dh->activate) |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 177 | err = scsi_dh->activate(sdev, NULL, NULL); |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 178 | else |
| 179 | err = 0; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return err<0?err:count; |
| 184 | } |
| 185 | |
| 186 | static ssize_t |
| 187 | show_dh_state(struct device *dev, struct device_attribute *attr, char *buf) |
| 188 | { |
| 189 | struct scsi_device *sdev = to_scsi_device(dev); |
| 190 | |
| 191 | if (!sdev->scsi_dh_data) |
| 192 | return snprintf(buf, 20, "detached\n"); |
| 193 | |
| 194 | return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name); |
| 195 | } |
| 196 | |
| 197 | static struct device_attribute scsi_dh_state_attr = |
| 198 | __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state, |
| 199 | store_dh_state); |
| 200 | |
| 201 | /* |
| 202 | * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh |
| 203 | */ |
| 204 | static int scsi_dh_sysfs_attr_add(struct device *dev, void *data) |
| 205 | { |
| 206 | struct scsi_device *sdev; |
| 207 | int err; |
| 208 | |
| 209 | if (!scsi_is_sdev_device(dev)) |
| 210 | return 0; |
| 211 | |
| 212 | sdev = to_scsi_device(dev); |
| 213 | |
| 214 | err = device_create_file(&sdev->sdev_gendev, |
| 215 | &scsi_dh_state_attr); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh |
| 222 | */ |
| 223 | static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data) |
| 224 | { |
| 225 | struct scsi_device *sdev; |
| 226 | |
| 227 | if (!scsi_is_sdev_device(dev)) |
| 228 | return 0; |
| 229 | |
| 230 | sdev = to_scsi_device(dev); |
| 231 | |
| 232 | device_remove_file(&sdev->sdev_gendev, |
| 233 | &scsi_dh_state_attr); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 239 | * scsi_dh_notifier - notifier chain callback |
| 240 | */ |
| 241 | static int scsi_dh_notifier(struct notifier_block *nb, |
| 242 | unsigned long action, void *data) |
| 243 | { |
| 244 | struct device *dev = data; |
| 245 | struct scsi_device *sdev; |
| 246 | int err = 0; |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 247 | struct scsi_device_handler *devinfo = NULL; |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 248 | |
| 249 | if (!scsi_is_sdev_device(dev)) |
| 250 | return 0; |
| 251 | |
| 252 | sdev = to_scsi_device(dev); |
| 253 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 254 | if (action == BUS_NOTIFY_ADD_DEVICE) { |
Chandra Seetharaman | 5917290 | 2009-09-11 10:20:35 -0700 | [diff] [blame] | 255 | err = device_create_file(dev, &scsi_dh_state_attr); |
| 256 | /* don't care about err */ |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 257 | devinfo = device_handler_match(NULL, sdev); |
Chandra Seetharaman | 5917290 | 2009-09-11 10:20:35 -0700 | [diff] [blame] | 258 | if (devinfo) |
| 259 | err = scsi_dh_handler_attach(sdev, devinfo); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 260 | } else if (action == BUS_NOTIFY_DEL_DEVICE) { |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 261 | device_remove_file(dev, &scsi_dh_state_attr); |
Christoph Hellwig | 1bab0de | 2015-08-27 14:16:54 +0200 | [diff] [blame^] | 262 | if (sdev->scsi_dh_data) |
| 263 | scsi_dh_handler_detach(sdev); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 264 | } |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 265 | return err; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * scsi_dh_notifier_add - Callback for scsi_register_device_handler |
| 270 | */ |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 271 | static int scsi_dh_notifier_add(struct device *dev, void *data) |
| 272 | { |
| 273 | struct scsi_device_handler *scsi_dh = data; |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 274 | struct scsi_device *sdev; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 275 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 276 | if (!scsi_is_sdev_device(dev)) |
| 277 | return 0; |
| 278 | |
| 279 | if (!get_device(dev)) |
| 280 | return 0; |
| 281 | |
| 282 | sdev = to_scsi_device(dev); |
| 283 | |
| 284 | if (device_handler_match(scsi_dh, sdev)) |
| 285 | scsi_dh_handler_attach(sdev, scsi_dh); |
| 286 | |
| 287 | put_device(dev); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler |
| 294 | */ |
| 295 | static int scsi_dh_notifier_remove(struct device *dev, void *data) |
| 296 | { |
| 297 | struct scsi_device_handler *scsi_dh = data; |
| 298 | struct scsi_device *sdev; |
| 299 | |
| 300 | if (!scsi_is_sdev_device(dev)) |
| 301 | return 0; |
| 302 | |
| 303 | if (!get_device(dev)) |
| 304 | return 0; |
| 305 | |
| 306 | sdev = to_scsi_device(dev); |
| 307 | |
Christoph Hellwig | 1bab0de | 2015-08-27 14:16:54 +0200 | [diff] [blame^] | 308 | if (sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh == scsi_dh) |
| 309 | scsi_dh_handler_detach(sdev); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 310 | |
| 311 | put_device(dev); |
| 312 | |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * scsi_register_device_handler - register a device handler personality |
| 318 | * module. |
| 319 | * @scsi_dh - device handler to be registered. |
| 320 | * |
| 321 | * Returns 0 on success, -EBUSY if handler already registered. |
| 322 | */ |
| 323 | int scsi_register_device_handler(struct scsi_device_handler *scsi_dh) |
| 324 | { |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 325 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 326 | if (get_device_handler(scsi_dh->name)) |
| 327 | return -EBUSY; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 328 | |
Christoph Hellwig | 1f12ffa | 2014-09-13 20:08:44 -0700 | [diff] [blame] | 329 | if (!scsi_dh->attach || !scsi_dh->detach) |
| 330 | return -EINVAL; |
| 331 | |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 332 | spin_lock(&list_lock); |
| 333 | list_add(&scsi_dh->list, &scsi_dh_list); |
| 334 | spin_unlock(&list_lock); |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 335 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 336 | bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add); |
| 337 | printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name); |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 338 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 339 | return SCSI_DH_OK; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 340 | } |
| 341 | EXPORT_SYMBOL_GPL(scsi_register_device_handler); |
| 342 | |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 343 | /* |
| 344 | * scsi_unregister_device_handler - register a device handler personality |
| 345 | * module. |
| 346 | * @scsi_dh - device handler to be unregistered. |
| 347 | * |
| 348 | * Returns 0 on success, -ENODEV if handler not registered. |
| 349 | */ |
| 350 | int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh) |
| 351 | { |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 352 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 353 | if (!get_device_handler(scsi_dh->name)) |
| 354 | return -ENODEV; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 355 | |
| 356 | bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 357 | scsi_dh_notifier_remove); |
| 358 | |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 359 | spin_lock(&list_lock); |
| 360 | list_del(&scsi_dh->list); |
| 361 | spin_unlock(&list_lock); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 362 | printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name); |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 363 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 364 | return SCSI_DH_OK; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 365 | } |
| 366 | EXPORT_SYMBOL_GPL(scsi_unregister_device_handler); |
| 367 | |
| 368 | /* |
| 369 | * scsi_dh_activate - activate the path associated with the scsi_device |
| 370 | * corresponding to the given request queue. |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 371 | * Returns immediately without waiting for activation to be completed. |
| 372 | * @q - Request queue that is associated with the scsi_device to be |
| 373 | * activated. |
| 374 | * @fn - Function to be called upon completion of the activation. |
| 375 | * Function fn is called with data (below) and the error code. |
| 376 | * Function fn may be called from the same calling context. So, |
| 377 | * do not hold the lock in the caller which may be needed in fn. |
| 378 | * @data - data passed to the function fn upon completion. |
| 379 | * |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 380 | */ |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 381 | int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data) |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 382 | { |
| 383 | int err = 0; |
| 384 | unsigned long flags; |
| 385 | struct scsi_device *sdev; |
| 386 | struct scsi_device_handler *scsi_dh = NULL; |
Mike Snitzer | 0b83935 | 2011-04-08 15:05:36 -0400 | [diff] [blame] | 387 | struct device *dev = NULL; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 388 | |
| 389 | spin_lock_irqsave(q->queue_lock, flags); |
| 390 | sdev = q->queuedata; |
Moger, Babu | a18a920 | 2011-10-26 14:29:38 -0400 | [diff] [blame] | 391 | if (!sdev) { |
| 392 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 393 | err = SCSI_DH_NOSYS; |
| 394 | if (fn) |
| 395 | fn(data, err); |
| 396 | return err; |
| 397 | } |
| 398 | |
| 399 | if (sdev->scsi_dh_data) |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 400 | scsi_dh = sdev->scsi_dh_data->scsi_dh; |
Mike Snitzer | 0b83935 | 2011-04-08 15:05:36 -0400 | [diff] [blame] | 401 | dev = get_device(&sdev->sdev_gendev); |
| 402 | if (!scsi_dh || !dev || |
Menny Hamburger | db42231 | 2010-12-16 14:57:07 -0500 | [diff] [blame] | 403 | sdev->sdev_state == SDEV_CANCEL || |
| 404 | sdev->sdev_state == SDEV_DEL) |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 405 | err = SCSI_DH_NOSYS; |
Menny Hamburger | db42231 | 2010-12-16 14:57:07 -0500 | [diff] [blame] | 406 | if (sdev->sdev_state == SDEV_OFFLINE) |
| 407 | err = SCSI_DH_DEV_OFFLINED; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 408 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 409 | |
Menny Hamburger | db42231 | 2010-12-16 14:57:07 -0500 | [diff] [blame] | 410 | if (err) { |
| 411 | if (fn) |
| 412 | fn(data, err); |
Mike Snitzer | 0b83935 | 2011-04-08 15:05:36 -0400 | [diff] [blame] | 413 | goto out; |
Menny Hamburger | db42231 | 2010-12-16 14:57:07 -0500 | [diff] [blame] | 414 | } |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 415 | |
| 416 | if (scsi_dh->activate) |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 417 | err = scsi_dh->activate(sdev, fn, data); |
Mike Snitzer | 0b83935 | 2011-04-08 15:05:36 -0400 | [diff] [blame] | 418 | out: |
| 419 | put_device(dev); |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 420 | return err; |
| 421 | } |
| 422 | EXPORT_SYMBOL_GPL(scsi_dh_activate); |
| 423 | |
| 424 | /* |
Chandra Seetharaman | 18ee70c | 2009-08-03 12:42:33 -0700 | [diff] [blame] | 425 | * scsi_dh_set_params - set the parameters for the device as per the |
| 426 | * string specified in params. |
| 427 | * @q - Request queue that is associated with the scsi_device for |
| 428 | * which the parameters to be set. |
| 429 | * @params - parameters in the following format |
| 430 | * "no_of_params\0param1\0param2\0param3\0...\0" |
| 431 | * for example, string for 2 parameters with value 10 and 21 |
| 432 | * is specified as "2\010\021\0". |
| 433 | */ |
| 434 | int scsi_dh_set_params(struct request_queue *q, const char *params) |
| 435 | { |
| 436 | int err = -SCSI_DH_NOSYS; |
| 437 | unsigned long flags; |
| 438 | struct scsi_device *sdev; |
| 439 | struct scsi_device_handler *scsi_dh = NULL; |
| 440 | |
| 441 | spin_lock_irqsave(q->queue_lock, flags); |
| 442 | sdev = q->queuedata; |
| 443 | if (sdev && sdev->scsi_dh_data) |
| 444 | scsi_dh = sdev->scsi_dh_data->scsi_dh; |
| 445 | if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev)) |
| 446 | err = 0; |
| 447 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 448 | |
| 449 | if (err) |
| 450 | return err; |
| 451 | err = scsi_dh->set_params(sdev, params); |
| 452 | put_device(&sdev->sdev_gendev); |
| 453 | return err; |
| 454 | } |
| 455 | EXPORT_SYMBOL_GPL(scsi_dh_set_params); |
| 456 | |
| 457 | /* |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 458 | * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for |
| 459 | * the given name. FALSE(0) otherwise. |
| 460 | * @name - name of the device handler. |
| 461 | */ |
| 462 | int scsi_dh_handler_exist(const char *name) |
| 463 | { |
| 464 | return (get_device_handler(name) != NULL); |
| 465 | } |
| 466 | EXPORT_SYMBOL_GPL(scsi_dh_handler_exist); |
| 467 | |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 468 | /* |
Hannes Reinecke | 2a9ab40 | 2011-08-24 10:51:14 +0200 | [diff] [blame] | 469 | * scsi_dh_attach - Attach device handler |
Mike Snitzer | 7e8a74b | 2012-06-26 14:32:03 -0400 | [diff] [blame] | 470 | * @q - Request queue that is associated with the scsi_device |
| 471 | * the handler should be attached to |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 472 | * @name - name of the handler to attach |
| 473 | */ |
| 474 | int scsi_dh_attach(struct request_queue *q, const char *name) |
| 475 | { |
| 476 | unsigned long flags; |
| 477 | struct scsi_device *sdev; |
| 478 | struct scsi_device_handler *scsi_dh; |
| 479 | int err = 0; |
| 480 | |
| 481 | scsi_dh = get_device_handler(name); |
| 482 | if (!scsi_dh) |
| 483 | return -EINVAL; |
| 484 | |
| 485 | spin_lock_irqsave(q->queue_lock, flags); |
| 486 | sdev = q->queuedata; |
| 487 | if (!sdev || !get_device(&sdev->sdev_gendev)) |
| 488 | err = -ENODEV; |
| 489 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 490 | |
Christoph Hellwig | 1bab0de | 2015-08-27 14:16:54 +0200 | [diff] [blame^] | 491 | if (err) |
| 492 | return err; |
| 493 | |
| 494 | if (sdev->scsi_dh_data) { |
| 495 | if (sdev->scsi_dh_data->scsi_dh != scsi_dh) |
| 496 | err = -EBUSY; |
| 497 | goto out_put_device; |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 498 | } |
Christoph Hellwig | 1bab0de | 2015-08-27 14:16:54 +0200 | [diff] [blame^] | 499 | |
| 500 | err = scsi_dh_handler_attach(sdev, scsi_dh); |
| 501 | |
| 502 | out_put_device: |
| 503 | put_device(&sdev->sdev_gendev); |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 504 | return err; |
| 505 | } |
| 506 | EXPORT_SYMBOL_GPL(scsi_dh_attach); |
| 507 | |
| 508 | /* |
Mike Snitzer | 7e8a74b | 2012-06-26 14:32:03 -0400 | [diff] [blame] | 509 | * scsi_dh_attached_handler_name - Get attached device handler's name |
| 510 | * @q - Request queue that is associated with the scsi_device |
| 511 | * that may have a device handler attached |
| 512 | * @gfp - the GFP mask used in the kmalloc() call when allocating memory |
| 513 | * |
| 514 | * Returns name of attached handler, NULL if no handler is attached. |
| 515 | * Caller must take care to free the returned string. |
| 516 | */ |
| 517 | const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp) |
| 518 | { |
| 519 | unsigned long flags; |
| 520 | struct scsi_device *sdev; |
| 521 | const char *handler_name = NULL; |
| 522 | |
| 523 | spin_lock_irqsave(q->queue_lock, flags); |
| 524 | sdev = q->queuedata; |
| 525 | if (!sdev || !get_device(&sdev->sdev_gendev)) |
| 526 | sdev = NULL; |
| 527 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 528 | |
| 529 | if (!sdev) |
| 530 | return NULL; |
| 531 | |
| 532 | if (sdev->scsi_dh_data) |
| 533 | handler_name = kstrdup(sdev->scsi_dh_data->scsi_dh->name, gfp); |
| 534 | |
| 535 | put_device(&sdev->sdev_gendev); |
| 536 | return handler_name; |
| 537 | } |
| 538 | EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name); |
| 539 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 540 | static struct notifier_block scsi_dh_nb = { |
| 541 | .notifier_call = scsi_dh_notifier |
| 542 | }; |
| 543 | |
| 544 | static int __init scsi_dh_init(void) |
| 545 | { |
| 546 | int r; |
| 547 | |
| 548 | r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb); |
| 549 | |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 550 | if (!r) |
| 551 | bus_for_each_dev(&scsi_bus_type, NULL, NULL, |
| 552 | scsi_dh_sysfs_attr_add); |
| 553 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 554 | return r; |
| 555 | } |
| 556 | |
| 557 | static void __exit scsi_dh_exit(void) |
| 558 | { |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 559 | bus_for_each_dev(&scsi_bus_type, NULL, NULL, |
| 560 | scsi_dh_sysfs_attr_remove); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 561 | bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb); |
| 562 | } |
| 563 | |
| 564 | module_init(scsi_dh_init); |
| 565 | module_exit(scsi_dh_exit); |
| 566 | |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 567 | MODULE_DESCRIPTION("SCSI device handler"); |
| 568 | MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>"); |
| 569 | MODULE_LICENSE("GPL"); |