scsi_dh.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. struct scsi_dh_data *d;
  91. if (sdev->scsi_dh_data) {
  92. if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
  93. return -EBUSY;
  94. kref_get(&sdev->scsi_dh_data->kref);
  95. return 0;
  96. }
  97. if (!try_module_get(scsi_dh->module))
  98. return -EINVAL;
  99. d = scsi_dh->attach(sdev);
  100. if (IS_ERR(d)) {
  101. sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%ld)\n",
  102. scsi_dh->name, PTR_ERR(d));
  103. module_put(scsi_dh->module);
  104. return PTR_ERR(d);
  105. }
  106. d->scsi_dh = scsi_dh;
  107. kref_init(&d->kref);
  108. d->sdev = sdev;
  109. spin_lock_irq(sdev->request_queue->queue_lock);
  110. sdev->scsi_dh_data = d;
  111. spin_unlock_irq(sdev->request_queue->queue_lock);
  112. return 0;
  113. }
  114. static void __detach_handler (struct kref *kref)
  115. {
  116. struct scsi_dh_data *scsi_dh_data =
  117. container_of(kref, struct scsi_dh_data, kref);
  118. struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh;
  119. struct scsi_device *sdev = scsi_dh_data->sdev;
  120. scsi_dh->detach(sdev);
  121. spin_lock_irq(sdev->request_queue->queue_lock);
  122. sdev->scsi_dh_data = NULL;
  123. spin_unlock_irq(sdev->request_queue->queue_lock);
  124. sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", scsi_dh->name);
  125. module_put(scsi_dh->module);
  126. }
  127. /*
  128. * scsi_dh_handler_detach - Detach a device handler from a device
  129. * @sdev - SCSI device the device handler should be detached from
  130. * @scsi_dh - Device handler to be detached
  131. *
  132. * Detach from a device handler. If a device handler is specified,
  133. * only detach if the currently attached handler matches @scsi_dh.
  134. */
  135. static void scsi_dh_handler_detach(struct scsi_device *sdev,
  136. struct scsi_device_handler *scsi_dh)
  137. {
  138. if (!sdev->scsi_dh_data)
  139. return;
  140. if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
  141. return;
  142. if (!scsi_dh)
  143. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  144. if (scsi_dh)
  145. kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
  146. }
  147. /*
  148. * Functions for sysfs attribute 'dh_state'
  149. */
  150. static ssize_t
  151. store_dh_state(struct device *dev, struct device_attribute *attr,
  152. const char *buf, size_t count)
  153. {
  154. struct scsi_device *sdev = to_scsi_device(dev);
  155. struct scsi_device_handler *scsi_dh;
  156. int err = -EINVAL;
  157. if (sdev->sdev_state == SDEV_CANCEL ||
  158. sdev->sdev_state == SDEV_DEL)
  159. return -ENODEV;
  160. if (!sdev->scsi_dh_data) {
  161. /*
  162. * Attach to a device handler
  163. */
  164. if (!(scsi_dh = get_device_handler(buf)))
  165. return err;
  166. err = scsi_dh_handler_attach(sdev, scsi_dh);
  167. } else {
  168. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  169. if (!strncmp(buf, "detach", 6)) {
  170. /*
  171. * Detach from a device handler
  172. */
  173. scsi_dh_handler_detach(sdev, scsi_dh);
  174. err = 0;
  175. } else if (!strncmp(buf, "activate", 8)) {
  176. /*
  177. * Activate a device handler
  178. */
  179. if (scsi_dh->activate)
  180. err = scsi_dh->activate(sdev, NULL, NULL);
  181. else
  182. err = 0;
  183. }
  184. }
  185. return err<0?err:count;
  186. }
  187. static ssize_t
  188. show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
  189. {
  190. struct scsi_device *sdev = to_scsi_device(dev);
  191. if (!sdev->scsi_dh_data)
  192. return snprintf(buf, 20, "detached\n");
  193. return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
  194. }
  195. static struct device_attribute scsi_dh_state_attr =
  196. __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
  197. store_dh_state);
  198. /*
  199. * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
  200. */
  201. static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
  202. {
  203. struct scsi_device *sdev;
  204. int err;
  205. if (!scsi_is_sdev_device(dev))
  206. return 0;
  207. sdev = to_scsi_device(dev);
  208. err = device_create_file(&sdev->sdev_gendev,
  209. &scsi_dh_state_attr);
  210. return 0;
  211. }
  212. /*
  213. * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
  214. */
  215. static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
  216. {
  217. struct scsi_device *sdev;
  218. if (!scsi_is_sdev_device(dev))
  219. return 0;
  220. sdev = to_scsi_device(dev);
  221. device_remove_file(&sdev->sdev_gendev,
  222. &scsi_dh_state_attr);
  223. return 0;
  224. }
  225. /*
  226. * scsi_dh_notifier - notifier chain callback
  227. */
  228. static int scsi_dh_notifier(struct notifier_block *nb,
  229. unsigned long action, void *data)
  230. {
  231. struct device *dev = data;
  232. struct scsi_device *sdev;
  233. int err = 0;
  234. struct scsi_device_handler *devinfo = NULL;
  235. if (!scsi_is_sdev_device(dev))
  236. return 0;
  237. sdev = to_scsi_device(dev);
  238. if (action == BUS_NOTIFY_ADD_DEVICE) {
  239. err = device_create_file(dev, &scsi_dh_state_attr);
  240. /* don't care about err */
  241. devinfo = device_handler_match(NULL, sdev);
  242. if (devinfo)
  243. err = scsi_dh_handler_attach(sdev, devinfo);
  244. } else if (action == BUS_NOTIFY_DEL_DEVICE) {
  245. device_remove_file(dev, &scsi_dh_state_attr);
  246. scsi_dh_handler_detach(sdev, NULL);
  247. }
  248. return err;
  249. }
  250. /*
  251. * scsi_dh_notifier_add - Callback for scsi_register_device_handler
  252. */
  253. static int scsi_dh_notifier_add(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. if (device_handler_match(scsi_dh, sdev))
  263. scsi_dh_handler_attach(sdev, scsi_dh);
  264. put_device(dev);
  265. return 0;
  266. }
  267. /*
  268. * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
  269. */
  270. static int scsi_dh_notifier_remove(struct device *dev, void *data)
  271. {
  272. struct scsi_device_handler *scsi_dh = data;
  273. struct scsi_device *sdev;
  274. if (!scsi_is_sdev_device(dev))
  275. return 0;
  276. if (!get_device(dev))
  277. return 0;
  278. sdev = to_scsi_device(dev);
  279. scsi_dh_handler_detach(sdev, scsi_dh);
  280. put_device(dev);
  281. return 0;
  282. }
  283. /*
  284. * scsi_register_device_handler - register a device handler personality
  285. * module.
  286. * @scsi_dh - device handler to be registered.
  287. *
  288. * Returns 0 on success, -EBUSY if handler already registered.
  289. */
  290. int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
  291. {
  292. if (get_device_handler(scsi_dh->name))
  293. return -EBUSY;
  294. if (!scsi_dh->attach || !scsi_dh->detach)
  295. return -EINVAL;
  296. spin_lock(&list_lock);
  297. list_add(&scsi_dh->list, &scsi_dh_list);
  298. spin_unlock(&list_lock);
  299. bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
  300. printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
  301. return SCSI_DH_OK;
  302. }
  303. EXPORT_SYMBOL_GPL(scsi_register_device_handler);
  304. /*
  305. * scsi_unregister_device_handler - register a device handler personality
  306. * module.
  307. * @scsi_dh - device handler to be unregistered.
  308. *
  309. * Returns 0 on success, -ENODEV if handler not registered.
  310. */
  311. int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
  312. {
  313. if (!get_device_handler(scsi_dh->name))
  314. return -ENODEV;
  315. bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
  316. scsi_dh_notifier_remove);
  317. spin_lock(&list_lock);
  318. list_del(&scsi_dh->list);
  319. spin_unlock(&list_lock);
  320. printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
  321. return SCSI_DH_OK;
  322. }
  323. EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
  324. /*
  325. * scsi_dh_activate - activate the path associated with the scsi_device
  326. * corresponding to the given request queue.
  327. * Returns immediately without waiting for activation to be completed.
  328. * @q - Request queue that is associated with the scsi_device to be
  329. * activated.
  330. * @fn - Function to be called upon completion of the activation.
  331. * Function fn is called with data (below) and the error code.
  332. * Function fn may be called from the same calling context. So,
  333. * do not hold the lock in the caller which may be needed in fn.
  334. * @data - data passed to the function fn upon completion.
  335. *
  336. */
  337. int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
  338. {
  339. int err = 0;
  340. unsigned long flags;
  341. struct scsi_device *sdev;
  342. struct scsi_device_handler *scsi_dh = NULL;
  343. struct device *dev = NULL;
  344. spin_lock_irqsave(q->queue_lock, flags);
  345. sdev = q->queuedata;
  346. if (!sdev) {
  347. spin_unlock_irqrestore(q->queue_lock, flags);
  348. err = SCSI_DH_NOSYS;
  349. if (fn)
  350. fn(data, err);
  351. return err;
  352. }
  353. if (sdev->scsi_dh_data)
  354. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  355. dev = get_device(&sdev->sdev_gendev);
  356. if (!scsi_dh || !dev ||
  357. sdev->sdev_state == SDEV_CANCEL ||
  358. sdev->sdev_state == SDEV_DEL)
  359. err = SCSI_DH_NOSYS;
  360. if (sdev->sdev_state == SDEV_OFFLINE)
  361. err = SCSI_DH_DEV_OFFLINED;
  362. spin_unlock_irqrestore(q->queue_lock, flags);
  363. if (err) {
  364. if (fn)
  365. fn(data, err);
  366. goto out;
  367. }
  368. if (scsi_dh->activate)
  369. err = scsi_dh->activate(sdev, fn, data);
  370. out:
  371. put_device(dev);
  372. return err;
  373. }
  374. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  375. /*
  376. * scsi_dh_set_params - set the parameters for the device as per the
  377. * string specified in params.
  378. * @q - Request queue that is associated with the scsi_device for
  379. * which the parameters to be set.
  380. * @params - parameters in the following format
  381. * "no_of_params\0param1\0param2\0param3\0...\0"
  382. * for example, string for 2 parameters with value 10 and 21
  383. * is specified as "2\010\021\0".
  384. */
  385. int scsi_dh_set_params(struct request_queue *q, const char *params)
  386. {
  387. int err = -SCSI_DH_NOSYS;
  388. unsigned long flags;
  389. struct scsi_device *sdev;
  390. struct scsi_device_handler *scsi_dh = NULL;
  391. spin_lock_irqsave(q->queue_lock, flags);
  392. sdev = q->queuedata;
  393. if (sdev && sdev->scsi_dh_data)
  394. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  395. if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
  396. err = 0;
  397. spin_unlock_irqrestore(q->queue_lock, flags);
  398. if (err)
  399. return err;
  400. err = scsi_dh->set_params(sdev, params);
  401. put_device(&sdev->sdev_gendev);
  402. return err;
  403. }
  404. EXPORT_SYMBOL_GPL(scsi_dh_set_params);
  405. /*
  406. * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
  407. * the given name. FALSE(0) otherwise.
  408. * @name - name of the device handler.
  409. */
  410. int scsi_dh_handler_exist(const char *name)
  411. {
  412. return (get_device_handler(name) != NULL);
  413. }
  414. EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
  415. /*
  416. * scsi_dh_attach - Attach device handler
  417. * @q - Request queue that is associated with the scsi_device
  418. * the handler should be attached to
  419. * @name - name of the handler to attach
  420. */
  421. int scsi_dh_attach(struct request_queue *q, const char *name)
  422. {
  423. unsigned long flags;
  424. struct scsi_device *sdev;
  425. struct scsi_device_handler *scsi_dh;
  426. int err = 0;
  427. scsi_dh = get_device_handler(name);
  428. if (!scsi_dh)
  429. return -EINVAL;
  430. spin_lock_irqsave(q->queue_lock, flags);
  431. sdev = q->queuedata;
  432. if (!sdev || !get_device(&sdev->sdev_gendev))
  433. err = -ENODEV;
  434. spin_unlock_irqrestore(q->queue_lock, flags);
  435. if (!err) {
  436. err = scsi_dh_handler_attach(sdev, scsi_dh);
  437. put_device(&sdev->sdev_gendev);
  438. }
  439. return err;
  440. }
  441. EXPORT_SYMBOL_GPL(scsi_dh_attach);
  442. /*
  443. * scsi_dh_detach - Detach device handler
  444. * @q - Request queue that is associated with the scsi_device
  445. * the handler should be detached from
  446. *
  447. * This function will detach the device handler only
  448. * if the sdev is not part of the internal list, ie
  449. * if it has been attached manually.
  450. */
  451. void scsi_dh_detach(struct request_queue *q)
  452. {
  453. unsigned long flags;
  454. struct scsi_device *sdev;
  455. struct scsi_device_handler *scsi_dh = NULL;
  456. spin_lock_irqsave(q->queue_lock, flags);
  457. sdev = q->queuedata;
  458. if (!sdev || !get_device(&sdev->sdev_gendev))
  459. sdev = NULL;
  460. spin_unlock_irqrestore(q->queue_lock, flags);
  461. if (!sdev)
  462. return;
  463. if (sdev->scsi_dh_data) {
  464. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  465. scsi_dh_handler_detach(sdev, scsi_dh);
  466. }
  467. put_device(&sdev->sdev_gendev);
  468. }
  469. EXPORT_SYMBOL_GPL(scsi_dh_detach);
  470. /*
  471. * scsi_dh_attached_handler_name - Get attached device handler's name
  472. * @q - Request queue that is associated with the scsi_device
  473. * that may have a device handler attached
  474. * @gfp - the GFP mask used in the kmalloc() call when allocating memory
  475. *
  476. * Returns name of attached handler, NULL if no handler is attached.
  477. * Caller must take care to free the returned string.
  478. */
  479. const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
  480. {
  481. unsigned long flags;
  482. struct scsi_device *sdev;
  483. const char *handler_name = NULL;
  484. spin_lock_irqsave(q->queue_lock, flags);
  485. sdev = q->queuedata;
  486. if (!sdev || !get_device(&sdev->sdev_gendev))
  487. sdev = NULL;
  488. spin_unlock_irqrestore(q->queue_lock, flags);
  489. if (!sdev)
  490. return NULL;
  491. if (sdev->scsi_dh_data)
  492. handler_name = kstrdup(sdev->scsi_dh_data->scsi_dh->name, gfp);
  493. put_device(&sdev->sdev_gendev);
  494. return handler_name;
  495. }
  496. EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);
  497. static struct notifier_block scsi_dh_nb = {
  498. .notifier_call = scsi_dh_notifier
  499. };
  500. static int __init scsi_dh_init(void)
  501. {
  502. int r;
  503. r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
  504. if (!r)
  505. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  506. scsi_dh_sysfs_attr_add);
  507. return r;
  508. }
  509. static void __exit scsi_dh_exit(void)
  510. {
  511. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  512. scsi_dh_sysfs_attr_remove);
  513. bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
  514. }
  515. module_init(scsi_dh_init);
  516. module_exit(scsi_dh_exit);
  517. MODULE_DESCRIPTION("SCSI device handler");
  518. MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
  519. MODULE_LICENSE("GPL");