scsi_dh.c 14 KB

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