scsi_dh.c 13 KB

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