scsi_dh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <scsi/scsi_dh.h>
  26. #include "../scsi_priv.h"
  27. static DEFINE_SPINLOCK(list_lock);
  28. static LIST_HEAD(scsi_dh_list);
  29. static struct scsi_device_handler *get_device_handler(const char *name)
  30. {
  31. struct scsi_device_handler *tmp, *found = NULL;
  32. spin_lock(&list_lock);
  33. list_for_each_entry(tmp, &scsi_dh_list, list) {
  34. if (!strncmp(tmp->name, name, strlen(tmp->name))) {
  35. found = tmp;
  36. break;
  37. }
  38. }
  39. spin_unlock(&list_lock);
  40. return found;
  41. }
  42. /*
  43. * device_handler_match_function - Match a device handler to a device
  44. * @sdev - SCSI device to be tested
  45. *
  46. * Tests @sdev against the match function of all registered device_handler.
  47. * Returns the found device handler or NULL if not found.
  48. */
  49. static struct scsi_device_handler *
  50. device_handler_match_function(struct scsi_device *sdev)
  51. {
  52. struct scsi_device_handler *tmp_dh, *found_dh = NULL;
  53. spin_lock(&list_lock);
  54. list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
  55. if (tmp_dh->match && tmp_dh->match(sdev)) {
  56. found_dh = tmp_dh;
  57. break;
  58. }
  59. }
  60. spin_unlock(&list_lock);
  61. return found_dh;
  62. }
  63. /*
  64. * device_handler_match - Attach a device handler to a device
  65. * @scsi_dh - The device handler to match against or NULL
  66. * @sdev - SCSI device to be tested against @scsi_dh
  67. *
  68. * Tests @sdev against the device handler @scsi_dh or against
  69. * all registered device_handler if @scsi_dh == NULL.
  70. * Returns the found device handler or NULL if not found.
  71. */
  72. static struct scsi_device_handler *
  73. device_handler_match(struct scsi_device_handler *scsi_dh,
  74. struct scsi_device *sdev)
  75. {
  76. struct scsi_device_handler *found_dh;
  77. found_dh = device_handler_match_function(sdev);
  78. if (scsi_dh && found_dh != scsi_dh)
  79. found_dh = NULL;
  80. return found_dh;
  81. }
  82. /*
  83. * scsi_dh_handler_attach - Attach a device handler to a device
  84. * @sdev - SCSI device the device handler should attach to
  85. * @scsi_dh - The device handler to attach
  86. */
  87. static int scsi_dh_handler_attach(struct scsi_device *sdev,
  88. struct scsi_device_handler *scsi_dh)
  89. {
  90. int err = 0;
  91. if (sdev->scsi_dh_data) {
  92. if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
  93. err = -EBUSY;
  94. else
  95. kref_get(&sdev->scsi_dh_data->kref);
  96. } else if (scsi_dh->attach) {
  97. err = scsi_dh->attach(sdev);
  98. if (!err) {
  99. kref_init(&sdev->scsi_dh_data->kref);
  100. sdev->scsi_dh_data->sdev = sdev;
  101. }
  102. }
  103. return err;
  104. }
  105. static void __detach_handler (struct kref *kref)
  106. {
  107. struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref);
  108. scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev);
  109. }
  110. /*
  111. * scsi_dh_handler_detach - Detach a device handler from a device
  112. * @sdev - SCSI device the device handler should be detached from
  113. * @scsi_dh - Device handler to be detached
  114. *
  115. * Detach from a device handler. If a device handler is specified,
  116. * only detach if the currently attached handler matches @scsi_dh.
  117. */
  118. static void scsi_dh_handler_detach(struct scsi_device *sdev,
  119. struct scsi_device_handler *scsi_dh)
  120. {
  121. if (!sdev->scsi_dh_data)
  122. return;
  123. if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
  124. return;
  125. if (!scsi_dh)
  126. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  127. if (scsi_dh && scsi_dh->detach)
  128. kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
  129. }
  130. /*
  131. * Functions for sysfs attribute 'dh_state'
  132. */
  133. static ssize_t
  134. store_dh_state(struct device *dev, struct device_attribute *attr,
  135. const char *buf, size_t count)
  136. {
  137. struct scsi_device *sdev = to_scsi_device(dev);
  138. struct scsi_device_handler *scsi_dh;
  139. int err = -EINVAL;
  140. if (sdev->sdev_state == SDEV_CANCEL ||
  141. sdev->sdev_state == SDEV_DEL)
  142. return -ENODEV;
  143. if (!sdev->scsi_dh_data) {
  144. /*
  145. * Attach to a device handler
  146. */
  147. if (!(scsi_dh = get_device_handler(buf)))
  148. return err;
  149. err = scsi_dh_handler_attach(sdev, scsi_dh);
  150. } else {
  151. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  152. if (!strncmp(buf, "detach", 6)) {
  153. /*
  154. * Detach from a device handler
  155. */
  156. scsi_dh_handler_detach(sdev, scsi_dh);
  157. err = 0;
  158. } else if (!strncmp(buf, "activate", 8)) {
  159. /*
  160. * Activate a device handler
  161. */
  162. if (scsi_dh->activate)
  163. err = scsi_dh->activate(sdev, NULL, NULL);
  164. else
  165. err = 0;
  166. }
  167. }
  168. return err<0?err:count;
  169. }
  170. static ssize_t
  171. show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
  172. {
  173. struct scsi_device *sdev = to_scsi_device(dev);
  174. if (!sdev->scsi_dh_data)
  175. return snprintf(buf, 20, "detached\n");
  176. return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
  177. }
  178. static struct device_attribute scsi_dh_state_attr =
  179. __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
  180. store_dh_state);
  181. /*
  182. * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
  183. */
  184. static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
  185. {
  186. struct scsi_device *sdev;
  187. int err;
  188. if (!scsi_is_sdev_device(dev))
  189. return 0;
  190. sdev = to_scsi_device(dev);
  191. err = device_create_file(&sdev->sdev_gendev,
  192. &scsi_dh_state_attr);
  193. return 0;
  194. }
  195. /*
  196. * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
  197. */
  198. static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
  199. {
  200. struct scsi_device *sdev;
  201. if (!scsi_is_sdev_device(dev))
  202. return 0;
  203. sdev = to_scsi_device(dev);
  204. device_remove_file(&sdev->sdev_gendev,
  205. &scsi_dh_state_attr);
  206. return 0;
  207. }
  208. /*
  209. * scsi_dh_notifier - notifier chain callback
  210. */
  211. static int scsi_dh_notifier(struct notifier_block *nb,
  212. unsigned long action, void *data)
  213. {
  214. struct device *dev = data;
  215. struct scsi_device *sdev;
  216. int err = 0;
  217. struct scsi_device_handler *devinfo = NULL;
  218. if (!scsi_is_sdev_device(dev))
  219. return 0;
  220. sdev = to_scsi_device(dev);
  221. if (action == BUS_NOTIFY_ADD_DEVICE) {
  222. err = device_create_file(dev, &scsi_dh_state_attr);
  223. /* don't care about err */
  224. devinfo = device_handler_match(NULL, sdev);
  225. if (devinfo)
  226. err = scsi_dh_handler_attach(sdev, devinfo);
  227. } else if (action == BUS_NOTIFY_DEL_DEVICE) {
  228. device_remove_file(dev, &scsi_dh_state_attr);
  229. scsi_dh_handler_detach(sdev, NULL);
  230. }
  231. return err;
  232. }
  233. /*
  234. * scsi_dh_notifier_add - Callback for scsi_register_device_handler
  235. */
  236. static int scsi_dh_notifier_add(struct device *dev, void *data)
  237. {
  238. struct scsi_device_handler *scsi_dh = data;
  239. struct scsi_device *sdev;
  240. if (!scsi_is_sdev_device(dev))
  241. return 0;
  242. if (!get_device(dev))
  243. return 0;
  244. sdev = to_scsi_device(dev);
  245. if (device_handler_match(scsi_dh, sdev))
  246. scsi_dh_handler_attach(sdev, scsi_dh);
  247. put_device(dev);
  248. return 0;
  249. }
  250. /*
  251. * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
  252. */
  253. static int scsi_dh_notifier_remove(struct device *dev, void *data)
  254. {
  255. struct scsi_device_handler *scsi_dh = data;
  256. struct scsi_device *sdev;
  257. if (!scsi_is_sdev_device(dev))
  258. return 0;
  259. if (!get_device(dev))
  260. return 0;
  261. sdev = to_scsi_device(dev);
  262. scsi_dh_handler_detach(sdev, scsi_dh);
  263. put_device(dev);
  264. return 0;
  265. }
  266. /*
  267. * scsi_register_device_handler - register a device handler personality
  268. * module.
  269. * @scsi_dh - device handler to be registered.
  270. *
  271. * Returns 0 on success, -EBUSY if handler already registered.
  272. */
  273. int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
  274. {
  275. if (get_device_handler(scsi_dh->name))
  276. return -EBUSY;
  277. spin_lock(&list_lock);
  278. list_add(&scsi_dh->list, &scsi_dh_list);
  279. spin_unlock(&list_lock);
  280. bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
  281. printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
  282. return SCSI_DH_OK;
  283. }
  284. EXPORT_SYMBOL_GPL(scsi_register_device_handler);
  285. /*
  286. * scsi_unregister_device_handler - register a device handler personality
  287. * module.
  288. * @scsi_dh - device handler to be unregistered.
  289. *
  290. * Returns 0 on success, -ENODEV if handler not registered.
  291. */
  292. int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
  293. {
  294. if (!get_device_handler(scsi_dh->name))
  295. return -ENODEV;
  296. bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
  297. scsi_dh_notifier_remove);
  298. spin_lock(&list_lock);
  299. list_del(&scsi_dh->list);
  300. spin_unlock(&list_lock);
  301. printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
  302. return SCSI_DH_OK;
  303. }
  304. EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
  305. /*
  306. * scsi_dh_activate - activate the path associated with the scsi_device
  307. * corresponding to the given request queue.
  308. * Returns immediately without waiting for activation to be completed.
  309. * @q - Request queue that is associated with the scsi_device to be
  310. * activated.
  311. * @fn - Function to be called upon completion of the activation.
  312. * Function fn is called with data (below) and the error code.
  313. * Function fn may be called from the same calling context. So,
  314. * do not hold the lock in the caller which may be needed in fn.
  315. * @data - data passed to the function fn upon completion.
  316. *
  317. */
  318. int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
  319. {
  320. int err = 0;
  321. unsigned long flags;
  322. struct scsi_device *sdev;
  323. struct scsi_device_handler *scsi_dh = NULL;
  324. struct device *dev = NULL;
  325. spin_lock_irqsave(q->queue_lock, flags);
  326. sdev = q->queuedata;
  327. if (!sdev) {
  328. spin_unlock_irqrestore(q->queue_lock, flags);
  329. err = SCSI_DH_NOSYS;
  330. if (fn)
  331. fn(data, err);
  332. return err;
  333. }
  334. if (sdev->scsi_dh_data)
  335. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  336. dev = get_device(&sdev->sdev_gendev);
  337. if (!scsi_dh || !dev ||
  338. sdev->sdev_state == SDEV_CANCEL ||
  339. sdev->sdev_state == SDEV_DEL)
  340. err = SCSI_DH_NOSYS;
  341. if (sdev->sdev_state == SDEV_OFFLINE)
  342. err = SCSI_DH_DEV_OFFLINED;
  343. spin_unlock_irqrestore(q->queue_lock, flags);
  344. if (err) {
  345. if (fn)
  346. fn(data, err);
  347. goto out;
  348. }
  349. if (scsi_dh->activate)
  350. err = scsi_dh->activate(sdev, fn, data);
  351. out:
  352. put_device(dev);
  353. return err;
  354. }
  355. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  356. /*
  357. * scsi_dh_set_params - set the parameters for the device as per the
  358. * string specified in params.
  359. * @q - Request queue that is associated with the scsi_device for
  360. * which the parameters to be set.
  361. * @params - parameters in the following format
  362. * "no_of_params\0param1\0param2\0param3\0...\0"
  363. * for example, string for 2 parameters with value 10 and 21
  364. * is specified as "2\010\021\0".
  365. */
  366. int scsi_dh_set_params(struct request_queue *q, const char *params)
  367. {
  368. int err = -SCSI_DH_NOSYS;
  369. unsigned long flags;
  370. struct scsi_device *sdev;
  371. struct scsi_device_handler *scsi_dh = NULL;
  372. spin_lock_irqsave(q->queue_lock, flags);
  373. sdev = q->queuedata;
  374. if (sdev && sdev->scsi_dh_data)
  375. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  376. if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
  377. err = 0;
  378. spin_unlock_irqrestore(q->queue_lock, flags);
  379. if (err)
  380. return err;
  381. err = scsi_dh->set_params(sdev, params);
  382. put_device(&sdev->sdev_gendev);
  383. return err;
  384. }
  385. EXPORT_SYMBOL_GPL(scsi_dh_set_params);
  386. /*
  387. * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
  388. * the given name. FALSE(0) otherwise.
  389. * @name - name of the device handler.
  390. */
  391. int scsi_dh_handler_exist(const char *name)
  392. {
  393. return (get_device_handler(name) != NULL);
  394. }
  395. EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
  396. /*
  397. * scsi_dh_attach - Attach device handler
  398. * @sdev - sdev the handler should be attached to
  399. * @name - name of the handler to attach
  400. */
  401. int scsi_dh_attach(struct request_queue *q, const char *name)
  402. {
  403. unsigned long flags;
  404. struct scsi_device *sdev;
  405. struct scsi_device_handler *scsi_dh;
  406. int err = 0;
  407. scsi_dh = get_device_handler(name);
  408. if (!scsi_dh)
  409. return -EINVAL;
  410. spin_lock_irqsave(q->queue_lock, flags);
  411. sdev = q->queuedata;
  412. if (!sdev || !get_device(&sdev->sdev_gendev))
  413. err = -ENODEV;
  414. spin_unlock_irqrestore(q->queue_lock, flags);
  415. if (!err) {
  416. err = scsi_dh_handler_attach(sdev, scsi_dh);
  417. put_device(&sdev->sdev_gendev);
  418. }
  419. return err;
  420. }
  421. EXPORT_SYMBOL_GPL(scsi_dh_attach);
  422. /*
  423. * scsi_dh_detach - Detach device handler
  424. * @sdev - sdev the handler should be detached from
  425. *
  426. * This function will detach the device handler only
  427. * if the sdev is not part of the internal list, ie
  428. * if it has been attached manually.
  429. */
  430. void scsi_dh_detach(struct request_queue *q)
  431. {
  432. unsigned long flags;
  433. struct scsi_device *sdev;
  434. struct scsi_device_handler *scsi_dh = NULL;
  435. spin_lock_irqsave(q->queue_lock, flags);
  436. sdev = q->queuedata;
  437. if (!sdev || !get_device(&sdev->sdev_gendev))
  438. sdev = NULL;
  439. spin_unlock_irqrestore(q->queue_lock, flags);
  440. if (!sdev)
  441. return;
  442. if (sdev->scsi_dh_data) {
  443. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  444. scsi_dh_handler_detach(sdev, scsi_dh);
  445. }
  446. put_device(&sdev->sdev_gendev);
  447. }
  448. EXPORT_SYMBOL_GPL(scsi_dh_detach);
  449. static struct notifier_block scsi_dh_nb = {
  450. .notifier_call = scsi_dh_notifier
  451. };
  452. static int __init scsi_dh_init(void)
  453. {
  454. int r;
  455. r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
  456. if (!r)
  457. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  458. scsi_dh_sysfs_attr_add);
  459. return r;
  460. }
  461. static void __exit scsi_dh_exit(void)
  462. {
  463. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  464. scsi_dh_sysfs_attr_remove);
  465. bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
  466. }
  467. module_init(scsi_dh_init);
  468. module_exit(scsi_dh_exit);
  469. MODULE_DESCRIPTION("SCSI device handler");
  470. MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
  471. MODULE_LICENSE("GPL");