scsi_dh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. spin_lock_irqsave(q->queue_lock, flags);
  333. sdev = q->queuedata;
  334. if (sdev && sdev->scsi_dh_data)
  335. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  336. if (!scsi_dh || !get_device(&sdev->sdev_gendev) ||
  337. sdev->sdev_state == SDEV_CANCEL ||
  338. sdev->sdev_state == SDEV_DEL)
  339. err = SCSI_DH_NOSYS;
  340. if (sdev->sdev_state == SDEV_OFFLINE)
  341. err = SCSI_DH_DEV_OFFLINED;
  342. spin_unlock_irqrestore(q->queue_lock, flags);
  343. if (err) {
  344. if (fn)
  345. fn(data, err);
  346. return err;
  347. }
  348. if (scsi_dh->activate)
  349. err = scsi_dh->activate(sdev, fn, data);
  350. put_device(&sdev->sdev_gendev);
  351. return err;
  352. }
  353. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  354. /*
  355. * scsi_dh_set_params - set the parameters for the device as per the
  356. * string specified in params.
  357. * @q - Request queue that is associated with the scsi_device for
  358. * which the parameters to be set.
  359. * @params - parameters in the following format
  360. * "no_of_params\0param1\0param2\0param3\0...\0"
  361. * for example, string for 2 parameters with value 10 and 21
  362. * is specified as "2\010\021\0".
  363. */
  364. int scsi_dh_set_params(struct request_queue *q, const char *params)
  365. {
  366. int err = -SCSI_DH_NOSYS;
  367. unsigned long flags;
  368. struct scsi_device *sdev;
  369. struct scsi_device_handler *scsi_dh = NULL;
  370. spin_lock_irqsave(q->queue_lock, flags);
  371. sdev = q->queuedata;
  372. if (sdev && sdev->scsi_dh_data)
  373. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  374. if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
  375. err = 0;
  376. spin_unlock_irqrestore(q->queue_lock, flags);
  377. if (err)
  378. return err;
  379. err = scsi_dh->set_params(sdev, params);
  380. put_device(&sdev->sdev_gendev);
  381. return err;
  382. }
  383. EXPORT_SYMBOL_GPL(scsi_dh_set_params);
  384. /*
  385. * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
  386. * the given name. FALSE(0) otherwise.
  387. * @name - name of the device handler.
  388. */
  389. int scsi_dh_handler_exist(const char *name)
  390. {
  391. return (get_device_handler(name) != NULL);
  392. }
  393. EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
  394. /*
  395. * scsi_dh_handler_attach - Attach device handler
  396. * @sdev - sdev the handler should be attached to
  397. * @name - name of the handler to attach
  398. */
  399. int scsi_dh_attach(struct request_queue *q, const char *name)
  400. {
  401. unsigned long flags;
  402. struct scsi_device *sdev;
  403. struct scsi_device_handler *scsi_dh;
  404. int err = 0;
  405. scsi_dh = get_device_handler(name);
  406. if (!scsi_dh)
  407. return -EINVAL;
  408. spin_lock_irqsave(q->queue_lock, flags);
  409. sdev = q->queuedata;
  410. if (!sdev || !get_device(&sdev->sdev_gendev))
  411. err = -ENODEV;
  412. spin_unlock_irqrestore(q->queue_lock, flags);
  413. if (!err) {
  414. err = scsi_dh_handler_attach(sdev, scsi_dh);
  415. put_device(&sdev->sdev_gendev);
  416. }
  417. return err;
  418. }
  419. EXPORT_SYMBOL_GPL(scsi_dh_attach);
  420. /*
  421. * scsi_dh_handler_detach - Detach device handler
  422. * @sdev - sdev the handler should be detached from
  423. *
  424. * This function will detach the device handler only
  425. * if the sdev is not part of the internal list, ie
  426. * if it has been attached manually.
  427. */
  428. void scsi_dh_detach(struct request_queue *q)
  429. {
  430. unsigned long flags;
  431. struct scsi_device *sdev;
  432. struct scsi_device_handler *scsi_dh = NULL;
  433. spin_lock_irqsave(q->queue_lock, flags);
  434. sdev = q->queuedata;
  435. if (!sdev || !get_device(&sdev->sdev_gendev))
  436. sdev = NULL;
  437. spin_unlock_irqrestore(q->queue_lock, flags);
  438. if (!sdev)
  439. return;
  440. if (sdev->scsi_dh_data) {
  441. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  442. scsi_dh_handler_detach(sdev, scsi_dh);
  443. }
  444. put_device(&sdev->sdev_gendev);
  445. }
  446. EXPORT_SYMBOL_GPL(scsi_dh_detach);
  447. static struct notifier_block scsi_dh_nb = {
  448. .notifier_call = scsi_dh_notifier
  449. };
  450. static int __init scsi_dh_init(void)
  451. {
  452. int r;
  453. r = scsi_dev_info_add_list(SCSI_DEVINFO_DH, "SCSI Device Handler");
  454. if (r)
  455. return r;
  456. r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
  457. if (!r)
  458. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  459. scsi_dh_sysfs_attr_add);
  460. return r;
  461. }
  462. static void __exit scsi_dh_exit(void)
  463. {
  464. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  465. scsi_dh_sysfs_attr_remove);
  466. bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
  467. scsi_dev_info_remove_list(SCSI_DEVINFO_DH);
  468. }
  469. module_init(scsi_dh_init);
  470. module_exit(scsi_dh_exit);
  471. MODULE_DESCRIPTION("SCSI device handler");
  472. MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
  473. MODULE_LICENSE("GPL");