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> |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 25 | #include <scsi/scsi_dh.h> |
| 26 | #include "../scsi_priv.h" |
| 27 | |
| 28 | static DEFINE_SPINLOCK(list_lock); |
| 29 | static LIST_HEAD(scsi_dh_list); |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 30 | static int scsi_dh_list_idx = 1; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 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 | |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 47 | static struct scsi_device_handler *get_device_handler_by_idx(int idx) |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 48 | { |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 49 | struct scsi_device_handler *tmp, *found = NULL; |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 50 | |
| 51 | spin_lock(&list_lock); |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 52 | list_for_each_entry(tmp, &scsi_dh_list, list) { |
| 53 | if (tmp->idx == idx) { |
| 54 | found = tmp; |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 55 | break; |
| 56 | } |
| 57 | } |
| 58 | spin_unlock(&list_lock); |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 59 | return found; |
| 60 | } |
| 61 | |
| 62 | /* |
Hannes Reinecke | 6c3633d | 2011-08-24 10:51:15 +0200 | [diff] [blame] | 63 | * device_handler_match_function - Match a device handler to a device |
| 64 | * @sdev - SCSI device to be tested |
| 65 | * |
| 66 | * Tests @sdev against the match function of all registered device_handler. |
| 67 | * Returns the found device handler or NULL if not found. |
| 68 | */ |
| 69 | static struct scsi_device_handler * |
| 70 | device_handler_match_function(struct scsi_device *sdev) |
| 71 | { |
| 72 | struct scsi_device_handler *tmp_dh, *found_dh = NULL; |
| 73 | |
| 74 | spin_lock(&list_lock); |
| 75 | list_for_each_entry(tmp_dh, &scsi_dh_list, list) { |
| 76 | if (tmp_dh->match && tmp_dh->match(sdev)) { |
| 77 | found_dh = tmp_dh; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | spin_unlock(&list_lock); |
| 82 | return found_dh; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * device_handler_match_devlist - Match a device handler to a device |
| 87 | * @sdev - SCSI device to be tested |
| 88 | * |
| 89 | * Tests @sdev against all device_handler registered in the devlist. |
| 90 | * Returns the found device handler or NULL if not found. |
| 91 | */ |
| 92 | static struct scsi_device_handler * |
| 93 | device_handler_match_devlist(struct scsi_device *sdev) |
| 94 | { |
| 95 | int idx; |
| 96 | |
| 97 | idx = scsi_get_device_flags_keyed(sdev, sdev->vendor, sdev->model, |
| 98 | SCSI_DEVINFO_DH); |
| 99 | return get_device_handler_by_idx(idx); |
| 100 | } |
| 101 | |
| 102 | /* |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 103 | * device_handler_match - Attach a device handler to a device |
| 104 | * @scsi_dh - The device handler to match against or NULL |
| 105 | * @sdev - SCSI device to be tested against @scsi_dh |
| 106 | * |
| 107 | * Tests @sdev against the device handler @scsi_dh or against |
| 108 | * all registered device_handler if @scsi_dh == NULL. |
| 109 | * Returns the found device handler or NULL if not found. |
| 110 | */ |
| 111 | static struct scsi_device_handler * |
| 112 | device_handler_match(struct scsi_device_handler *scsi_dh, |
| 113 | struct scsi_device *sdev) |
| 114 | { |
Hannes Reinecke | 6c3633d | 2011-08-24 10:51:15 +0200 | [diff] [blame] | 115 | struct scsi_device_handler *found_dh; |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 116 | |
Hannes Reinecke | 6c3633d | 2011-08-24 10:51:15 +0200 | [diff] [blame] | 117 | found_dh = device_handler_match_function(sdev); |
| 118 | if (!found_dh) |
| 119 | found_dh = device_handler_match_devlist(sdev); |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 120 | |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 121 | if (scsi_dh && found_dh != scsi_dh) |
| 122 | found_dh = NULL; |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 123 | |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 124 | return found_dh; |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| 128 | * scsi_dh_handler_attach - Attach a device handler to a device |
| 129 | * @sdev - SCSI device the device handler should attach to |
| 130 | * @scsi_dh - The device handler to attach |
| 131 | */ |
| 132 | static int scsi_dh_handler_attach(struct scsi_device *sdev, |
| 133 | struct scsi_device_handler *scsi_dh) |
| 134 | { |
| 135 | int err = 0; |
| 136 | |
| 137 | if (sdev->scsi_dh_data) { |
| 138 | if (sdev->scsi_dh_data->scsi_dh != scsi_dh) |
| 139 | err = -EBUSY; |
Chandra Seetharaman | 6c10db7 | 2009-06-26 19:30:06 -0700 | [diff] [blame] | 140 | else |
| 141 | kref_get(&sdev->scsi_dh_data->kref); |
| 142 | } else if (scsi_dh->attach) { |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 143 | err = scsi_dh->attach(sdev); |
Chandra Seetharaman | 6c10db7 | 2009-06-26 19:30:06 -0700 | [diff] [blame] | 144 | if (!err) { |
| 145 | kref_init(&sdev->scsi_dh_data->kref); |
| 146 | sdev->scsi_dh_data->sdev = sdev; |
| 147 | } |
| 148 | } |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 149 | return err; |
| 150 | } |
| 151 | |
Chandra Seetharaman | 6c10db7 | 2009-06-26 19:30:06 -0700 | [diff] [blame] | 152 | static void __detach_handler (struct kref *kref) |
| 153 | { |
| 154 | struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref); |
| 155 | scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev); |
| 156 | } |
| 157 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 158 | /* |
| 159 | * scsi_dh_handler_detach - Detach a device handler from a device |
| 160 | * @sdev - SCSI device the device handler should be detached from |
| 161 | * @scsi_dh - Device handler to be detached |
| 162 | * |
| 163 | * Detach from a device handler. If a device handler is specified, |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 164 | * only detach if the currently attached handler matches @scsi_dh. |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 165 | */ |
| 166 | static void scsi_dh_handler_detach(struct scsi_device *sdev, |
| 167 | struct scsi_device_handler *scsi_dh) |
| 168 | { |
| 169 | if (!sdev->scsi_dh_data) |
| 170 | return; |
| 171 | |
| 172 | if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh) |
| 173 | return; |
| 174 | |
| 175 | if (!scsi_dh) |
| 176 | scsi_dh = sdev->scsi_dh_data->scsi_dh; |
| 177 | |
| 178 | if (scsi_dh && scsi_dh->detach) |
Chandra Seetharaman | 6c10db7 | 2009-06-26 19:30:06 -0700 | [diff] [blame] | 179 | kref_put(&sdev->scsi_dh_data->kref, __detach_handler); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 183 | * Functions for sysfs attribute 'dh_state' |
| 184 | */ |
| 185 | static ssize_t |
| 186 | store_dh_state(struct device *dev, struct device_attribute *attr, |
| 187 | const char *buf, size_t count) |
| 188 | { |
| 189 | struct scsi_device *sdev = to_scsi_device(dev); |
| 190 | struct scsi_device_handler *scsi_dh; |
| 191 | int err = -EINVAL; |
| 192 | |
Hannes Reinecke | 6bc8d2a | 2011-08-24 10:51:17 +0200 | [diff] [blame] | 193 | if (sdev->sdev_state == SDEV_CANCEL || |
| 194 | sdev->sdev_state == SDEV_DEL) |
| 195 | return -ENODEV; |
| 196 | |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 197 | if (!sdev->scsi_dh_data) { |
| 198 | /* |
| 199 | * Attach to a device handler |
| 200 | */ |
| 201 | if (!(scsi_dh = get_device_handler(buf))) |
| 202 | return err; |
| 203 | err = scsi_dh_handler_attach(sdev, scsi_dh); |
| 204 | } else { |
| 205 | scsi_dh = sdev->scsi_dh_data->scsi_dh; |
| 206 | if (!strncmp(buf, "detach", 6)) { |
| 207 | /* |
| 208 | * Detach from a device handler |
| 209 | */ |
| 210 | scsi_dh_handler_detach(sdev, scsi_dh); |
| 211 | err = 0; |
| 212 | } else if (!strncmp(buf, "activate", 8)) { |
| 213 | /* |
| 214 | * Activate a device handler |
| 215 | */ |
| 216 | if (scsi_dh->activate) |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 217 | err = scsi_dh->activate(sdev, NULL, NULL); |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 218 | else |
| 219 | err = 0; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return err<0?err:count; |
| 224 | } |
| 225 | |
| 226 | static ssize_t |
| 227 | show_dh_state(struct device *dev, struct device_attribute *attr, char *buf) |
| 228 | { |
| 229 | struct scsi_device *sdev = to_scsi_device(dev); |
| 230 | |
| 231 | if (!sdev->scsi_dh_data) |
| 232 | return snprintf(buf, 20, "detached\n"); |
| 233 | |
| 234 | return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name); |
| 235 | } |
| 236 | |
| 237 | static struct device_attribute scsi_dh_state_attr = |
| 238 | __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state, |
| 239 | store_dh_state); |
| 240 | |
| 241 | /* |
| 242 | * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh |
| 243 | */ |
| 244 | static int scsi_dh_sysfs_attr_add(struct device *dev, void *data) |
| 245 | { |
| 246 | struct scsi_device *sdev; |
| 247 | int err; |
| 248 | |
| 249 | if (!scsi_is_sdev_device(dev)) |
| 250 | return 0; |
| 251 | |
| 252 | sdev = to_scsi_device(dev); |
| 253 | |
| 254 | err = device_create_file(&sdev->sdev_gendev, |
| 255 | &scsi_dh_state_attr); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh |
| 262 | */ |
| 263 | static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data) |
| 264 | { |
| 265 | struct scsi_device *sdev; |
| 266 | |
| 267 | if (!scsi_is_sdev_device(dev)) |
| 268 | return 0; |
| 269 | |
| 270 | sdev = to_scsi_device(dev); |
| 271 | |
| 272 | device_remove_file(&sdev->sdev_gendev, |
| 273 | &scsi_dh_state_attr); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | /* |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 279 | * scsi_dh_notifier - notifier chain callback |
| 280 | */ |
| 281 | static int scsi_dh_notifier(struct notifier_block *nb, |
| 282 | unsigned long action, void *data) |
| 283 | { |
| 284 | struct device *dev = data; |
| 285 | struct scsi_device *sdev; |
| 286 | int err = 0; |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 287 | struct scsi_device_handler *devinfo = NULL; |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 288 | |
| 289 | if (!scsi_is_sdev_device(dev)) |
| 290 | return 0; |
| 291 | |
| 292 | sdev = to_scsi_device(dev); |
| 293 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 294 | if (action == BUS_NOTIFY_ADD_DEVICE) { |
Chandra Seetharaman | 5917290 | 2009-09-11 10:20:35 -0700 | [diff] [blame] | 295 | err = device_create_file(dev, &scsi_dh_state_attr); |
| 296 | /* don't care about err */ |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 297 | devinfo = device_handler_match(NULL, sdev); |
Chandra Seetharaman | 5917290 | 2009-09-11 10:20:35 -0700 | [diff] [blame] | 298 | if (devinfo) |
| 299 | err = scsi_dh_handler_attach(sdev, devinfo); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 300 | } else if (action == BUS_NOTIFY_DEL_DEVICE) { |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 301 | device_remove_file(dev, &scsi_dh_state_attr); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 302 | scsi_dh_handler_detach(sdev, NULL); |
| 303 | } |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 304 | return err; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * scsi_dh_notifier_add - Callback for scsi_register_device_handler |
| 309 | */ |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 310 | static int scsi_dh_notifier_add(struct device *dev, void *data) |
| 311 | { |
| 312 | struct scsi_device_handler *scsi_dh = data; |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 313 | struct scsi_device *sdev; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 314 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 315 | if (!scsi_is_sdev_device(dev)) |
| 316 | return 0; |
| 317 | |
| 318 | if (!get_device(dev)) |
| 319 | return 0; |
| 320 | |
| 321 | sdev = to_scsi_device(dev); |
| 322 | |
| 323 | if (device_handler_match(scsi_dh, sdev)) |
| 324 | scsi_dh_handler_attach(sdev, scsi_dh); |
| 325 | |
| 326 | put_device(dev); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler |
| 333 | */ |
| 334 | static int scsi_dh_notifier_remove(struct device *dev, void *data) |
| 335 | { |
| 336 | struct scsi_device_handler *scsi_dh = data; |
| 337 | struct scsi_device *sdev; |
| 338 | |
| 339 | if (!scsi_is_sdev_device(dev)) |
| 340 | return 0; |
| 341 | |
| 342 | if (!get_device(dev)) |
| 343 | return 0; |
| 344 | |
| 345 | sdev = to_scsi_device(dev); |
| 346 | |
| 347 | scsi_dh_handler_detach(sdev, scsi_dh); |
| 348 | |
| 349 | put_device(dev); |
| 350 | |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * scsi_register_device_handler - register a device handler personality |
| 356 | * module. |
| 357 | * @scsi_dh - device handler to be registered. |
| 358 | * |
| 359 | * Returns 0 on success, -EBUSY if handler already registered. |
| 360 | */ |
| 361 | int scsi_register_device_handler(struct scsi_device_handler *scsi_dh) |
| 362 | { |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 363 | int i; |
| 364 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 365 | if (get_device_handler(scsi_dh->name)) |
| 366 | return -EBUSY; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 367 | |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 368 | spin_lock(&list_lock); |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 369 | scsi_dh->idx = scsi_dh_list_idx++; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 370 | list_add(&scsi_dh->list, &scsi_dh_list); |
| 371 | spin_unlock(&list_lock); |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 372 | |
Hannes Reinecke | 6c3633d | 2011-08-24 10:51:15 +0200 | [diff] [blame] | 373 | for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) { |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 374 | scsi_dev_info_list_add_keyed(0, |
| 375 | scsi_dh->devlist[i].vendor, |
| 376 | scsi_dh->devlist[i].model, |
| 377 | NULL, |
| 378 | scsi_dh->idx, |
| 379 | SCSI_DEVINFO_DH); |
| 380 | } |
| 381 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 382 | bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add); |
| 383 | printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name); |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 384 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 385 | return SCSI_DH_OK; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 386 | } |
| 387 | EXPORT_SYMBOL_GPL(scsi_register_device_handler); |
| 388 | |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 389 | /* |
| 390 | * scsi_unregister_device_handler - register a device handler personality |
| 391 | * module. |
| 392 | * @scsi_dh - device handler to be unregistered. |
| 393 | * |
| 394 | * Returns 0 on success, -ENODEV if handler not registered. |
| 395 | */ |
| 396 | int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh) |
| 397 | { |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 398 | int i; |
Hannes Reinecke | 7c32c7a | 2008-07-17 16:53:33 -0700 | [diff] [blame] | 399 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 400 | if (!get_device_handler(scsi_dh->name)) |
| 401 | return -ENODEV; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 402 | |
| 403 | bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 404 | scsi_dh_notifier_remove); |
| 405 | |
Hannes Reinecke | 6c3633d | 2011-08-24 10:51:15 +0200 | [diff] [blame] | 406 | for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) { |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 407 | scsi_dev_info_list_del_keyed(scsi_dh->devlist[i].vendor, |
| 408 | scsi_dh->devlist[i].model, |
| 409 | SCSI_DEVINFO_DH); |
| 410 | } |
| 411 | |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 412 | spin_lock(&list_lock); |
| 413 | list_del(&scsi_dh->list); |
| 414 | spin_unlock(&list_lock); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 415 | printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name); |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 416 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 417 | return SCSI_DH_OK; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 418 | } |
| 419 | EXPORT_SYMBOL_GPL(scsi_unregister_device_handler); |
| 420 | |
| 421 | /* |
| 422 | * scsi_dh_activate - activate the path associated with the scsi_device |
| 423 | * corresponding to the given request queue. |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 424 | * Returns immediately without waiting for activation to be completed. |
| 425 | * @q - Request queue that is associated with the scsi_device to be |
| 426 | * activated. |
| 427 | * @fn - Function to be called upon completion of the activation. |
| 428 | * Function fn is called with data (below) and the error code. |
| 429 | * Function fn may be called from the same calling context. So, |
| 430 | * do not hold the lock in the caller which may be needed in fn. |
| 431 | * @data - data passed to the function fn upon completion. |
| 432 | * |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 433 | */ |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 434 | 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] | 435 | { |
| 436 | int err = 0; |
| 437 | unsigned long flags; |
| 438 | struct scsi_device *sdev; |
| 439 | struct scsi_device_handler *scsi_dh = NULL; |
Mike Snitzer | 0b83935 | 2011-04-08 15:05:36 -0400 | [diff] [blame] | 440 | struct device *dev = NULL; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 441 | |
| 442 | spin_lock_irqsave(q->queue_lock, flags); |
| 443 | sdev = q->queuedata; |
| 444 | if (sdev && sdev->scsi_dh_data) |
| 445 | scsi_dh = sdev->scsi_dh_data->scsi_dh; |
Mike Snitzer | 0b83935 | 2011-04-08 15:05:36 -0400 | [diff] [blame] | 446 | dev = get_device(&sdev->sdev_gendev); |
| 447 | if (!scsi_dh || !dev || |
Menny Hamburger | db42231 | 2010-12-16 14:57:07 -0500 | [diff] [blame] | 448 | sdev->sdev_state == SDEV_CANCEL || |
| 449 | sdev->sdev_state == SDEV_DEL) |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 450 | err = SCSI_DH_NOSYS; |
Menny Hamburger | db42231 | 2010-12-16 14:57:07 -0500 | [diff] [blame] | 451 | if (sdev->sdev_state == SDEV_OFFLINE) |
| 452 | err = SCSI_DH_DEV_OFFLINED; |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 453 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 454 | |
Menny Hamburger | db42231 | 2010-12-16 14:57:07 -0500 | [diff] [blame] | 455 | if (err) { |
| 456 | if (fn) |
| 457 | fn(data, err); |
Mike Snitzer | 0b83935 | 2011-04-08 15:05:36 -0400 | [diff] [blame] | 458 | goto out; |
Menny Hamburger | db42231 | 2010-12-16 14:57:07 -0500 | [diff] [blame] | 459 | } |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 460 | |
| 461 | if (scsi_dh->activate) |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 462 | err = scsi_dh->activate(sdev, fn, data); |
Mike Snitzer | 0b83935 | 2011-04-08 15:05:36 -0400 | [diff] [blame] | 463 | out: |
| 464 | put_device(dev); |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 465 | return err; |
| 466 | } |
| 467 | EXPORT_SYMBOL_GPL(scsi_dh_activate); |
| 468 | |
| 469 | /* |
Chandra Seetharaman | 18ee70c | 2009-08-03 12:42:33 -0700 | [diff] [blame] | 470 | * scsi_dh_set_params - set the parameters for the device as per the |
| 471 | * string specified in params. |
| 472 | * @q - Request queue that is associated with the scsi_device for |
| 473 | * which the parameters to be set. |
| 474 | * @params - parameters in the following format |
| 475 | * "no_of_params\0param1\0param2\0param3\0...\0" |
| 476 | * for example, string for 2 parameters with value 10 and 21 |
| 477 | * is specified as "2\010\021\0". |
| 478 | */ |
| 479 | int scsi_dh_set_params(struct request_queue *q, const char *params) |
| 480 | { |
| 481 | int err = -SCSI_DH_NOSYS; |
| 482 | unsigned long flags; |
| 483 | struct scsi_device *sdev; |
| 484 | struct scsi_device_handler *scsi_dh = NULL; |
| 485 | |
| 486 | spin_lock_irqsave(q->queue_lock, flags); |
| 487 | sdev = q->queuedata; |
| 488 | if (sdev && sdev->scsi_dh_data) |
| 489 | scsi_dh = sdev->scsi_dh_data->scsi_dh; |
| 490 | if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev)) |
| 491 | err = 0; |
| 492 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 493 | |
| 494 | if (err) |
| 495 | return err; |
| 496 | err = scsi_dh->set_params(sdev, params); |
| 497 | put_device(&sdev->sdev_gendev); |
| 498 | return err; |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(scsi_dh_set_params); |
| 501 | |
| 502 | /* |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 503 | * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for |
| 504 | * the given name. FALSE(0) otherwise. |
| 505 | * @name - name of the device handler. |
| 506 | */ |
| 507 | int scsi_dh_handler_exist(const char *name) |
| 508 | { |
| 509 | return (get_device_handler(name) != NULL); |
| 510 | } |
| 511 | EXPORT_SYMBOL_GPL(scsi_dh_handler_exist); |
| 512 | |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 513 | /* |
Hannes Reinecke | 2a9ab40 | 2011-08-24 10:51:14 +0200 | [diff] [blame] | 514 | * scsi_dh_attach - Attach device handler |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 515 | * @sdev - sdev the handler should be attached to |
| 516 | * @name - name of the handler to attach |
| 517 | */ |
| 518 | int scsi_dh_attach(struct request_queue *q, const char *name) |
| 519 | { |
| 520 | unsigned long flags; |
| 521 | struct scsi_device *sdev; |
| 522 | struct scsi_device_handler *scsi_dh; |
| 523 | int err = 0; |
| 524 | |
| 525 | scsi_dh = get_device_handler(name); |
| 526 | if (!scsi_dh) |
| 527 | return -EINVAL; |
| 528 | |
| 529 | spin_lock_irqsave(q->queue_lock, flags); |
| 530 | sdev = q->queuedata; |
| 531 | if (!sdev || !get_device(&sdev->sdev_gendev)) |
| 532 | err = -ENODEV; |
| 533 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 534 | |
| 535 | if (!err) { |
| 536 | err = scsi_dh_handler_attach(sdev, scsi_dh); |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 537 | put_device(&sdev->sdev_gendev); |
| 538 | } |
| 539 | return err; |
| 540 | } |
| 541 | EXPORT_SYMBOL_GPL(scsi_dh_attach); |
| 542 | |
| 543 | /* |
Hannes Reinecke | 2a9ab40 | 2011-08-24 10:51:14 +0200 | [diff] [blame] | 544 | * scsi_dh_detach - Detach device handler |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 545 | * @sdev - sdev the handler should be detached from |
| 546 | * |
| 547 | * This function will detach the device handler only |
| 548 | * if the sdev is not part of the internal list, ie |
| 549 | * if it has been attached manually. |
| 550 | */ |
| 551 | void scsi_dh_detach(struct request_queue *q) |
| 552 | { |
| 553 | unsigned long flags; |
| 554 | struct scsi_device *sdev; |
| 555 | struct scsi_device_handler *scsi_dh = NULL; |
| 556 | |
| 557 | spin_lock_irqsave(q->queue_lock, flags); |
| 558 | sdev = q->queuedata; |
| 559 | if (!sdev || !get_device(&sdev->sdev_gendev)) |
| 560 | sdev = NULL; |
| 561 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 562 | |
| 563 | if (!sdev) |
| 564 | return; |
| 565 | |
| 566 | if (sdev->scsi_dh_data) { |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 567 | scsi_dh = sdev->scsi_dh_data->scsi_dh; |
Chandra Seetharaman | 6c10db7 | 2009-06-26 19:30:06 -0700 | [diff] [blame] | 568 | scsi_dh_handler_detach(sdev, scsi_dh); |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 569 | } |
| 570 | put_device(&sdev->sdev_gendev); |
| 571 | } |
| 572 | EXPORT_SYMBOL_GPL(scsi_dh_detach); |
| 573 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 574 | static struct notifier_block scsi_dh_nb = { |
| 575 | .notifier_call = scsi_dh_notifier |
| 576 | }; |
| 577 | |
| 578 | static int __init scsi_dh_init(void) |
| 579 | { |
| 580 | int r; |
| 581 | |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 582 | r = scsi_dev_info_add_list(SCSI_DEVINFO_DH, "SCSI Device Handler"); |
| 583 | if (r) |
| 584 | return r; |
| 585 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 586 | r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb); |
| 587 | |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 588 | if (!r) |
| 589 | bus_for_each_dev(&scsi_bus_type, NULL, NULL, |
| 590 | scsi_dh_sysfs_attr_add); |
| 591 | |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 592 | return r; |
| 593 | } |
| 594 | |
| 595 | static void __exit scsi_dh_exit(void) |
| 596 | { |
Hannes Reinecke | 4c05ae5 | 2008-07-17 16:52:57 -0700 | [diff] [blame] | 597 | bus_for_each_dev(&scsi_bus_type, NULL, NULL, |
| 598 | scsi_dh_sysfs_attr_remove); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 599 | bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb); |
Peter Jones | 940d7fa | 2011-01-06 15:38:24 -0500 | [diff] [blame] | 600 | scsi_dev_info_remove_list(SCSI_DEVINFO_DH); |
Hannes Reinecke | 765cbc6 | 2008-07-17 16:52:51 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | module_init(scsi_dh_init); |
| 604 | module_exit(scsi_dh_exit); |
| 605 | |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 606 | MODULE_DESCRIPTION("SCSI device handler"); |
| 607 | MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>"); |
| 608 | MODULE_LICENSE("GPL"); |