scsi_dh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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
  138. kref_get(&sdev->scsi_dh_data->kref);
  139. } else if (scsi_dh->attach) {
  140. err = scsi_dh->attach(sdev);
  141. if (!err) {
  142. kref_init(&sdev->scsi_dh_data->kref);
  143. sdev->scsi_dh_data->sdev = sdev;
  144. }
  145. }
  146. return err;
  147. }
  148. static void __detach_handler (struct kref *kref)
  149. {
  150. struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref);
  151. scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev);
  152. }
  153. /*
  154. * scsi_dh_handler_detach - Detach a device handler from a device
  155. * @sdev - SCSI device the device handler should be detached from
  156. * @scsi_dh - Device handler to be detached
  157. *
  158. * Detach from a device handler. If a device handler is specified,
  159. * only detach if the currently attached handler matches @scsi_dh.
  160. */
  161. static void scsi_dh_handler_detach(struct scsi_device *sdev,
  162. struct scsi_device_handler *scsi_dh)
  163. {
  164. if (!sdev->scsi_dh_data)
  165. return;
  166. if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
  167. return;
  168. if (!scsi_dh)
  169. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  170. if (scsi_dh && scsi_dh->detach)
  171. kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
  172. }
  173. /*
  174. * Functions for sysfs attribute 'dh_state'
  175. */
  176. static ssize_t
  177. store_dh_state(struct device *dev, struct device_attribute *attr,
  178. const char *buf, size_t count)
  179. {
  180. struct scsi_device *sdev = to_scsi_device(dev);
  181. struct scsi_device_handler *scsi_dh;
  182. int err = -EINVAL;
  183. if (!sdev->scsi_dh_data) {
  184. /*
  185. * Attach to a device handler
  186. */
  187. if (!(scsi_dh = get_device_handler(buf)))
  188. return err;
  189. err = scsi_dh_handler_attach(sdev, scsi_dh);
  190. } else {
  191. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  192. if (!strncmp(buf, "detach", 6)) {
  193. /*
  194. * Detach from a device handler
  195. */
  196. scsi_dh_handler_detach(sdev, scsi_dh);
  197. err = 0;
  198. } else if (!strncmp(buf, "activate", 8)) {
  199. /*
  200. * Activate a device handler
  201. */
  202. if (scsi_dh->activate)
  203. err = scsi_dh->activate(sdev);
  204. else
  205. err = 0;
  206. }
  207. }
  208. return err<0?err:count;
  209. }
  210. static ssize_t
  211. show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
  212. {
  213. struct scsi_device *sdev = to_scsi_device(dev);
  214. if (!sdev->scsi_dh_data)
  215. return snprintf(buf, 20, "detached\n");
  216. return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
  217. }
  218. static struct device_attribute scsi_dh_state_attr =
  219. __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
  220. store_dh_state);
  221. /*
  222. * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
  223. */
  224. static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
  225. {
  226. struct scsi_device *sdev;
  227. int err;
  228. if (!scsi_is_sdev_device(dev))
  229. return 0;
  230. sdev = to_scsi_device(dev);
  231. err = device_create_file(&sdev->sdev_gendev,
  232. &scsi_dh_state_attr);
  233. return 0;
  234. }
  235. /*
  236. * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
  237. */
  238. static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
  239. {
  240. struct scsi_device *sdev;
  241. if (!scsi_is_sdev_device(dev))
  242. return 0;
  243. sdev = to_scsi_device(dev);
  244. device_remove_file(&sdev->sdev_gendev,
  245. &scsi_dh_state_attr);
  246. return 0;
  247. }
  248. /*
  249. * scsi_dh_notifier - notifier chain callback
  250. */
  251. static int scsi_dh_notifier(struct notifier_block *nb,
  252. unsigned long action, void *data)
  253. {
  254. struct device *dev = data;
  255. struct scsi_device *sdev;
  256. int err = 0;
  257. struct scsi_device_handler *devinfo = NULL;
  258. if (!scsi_is_sdev_device(dev))
  259. return 0;
  260. sdev = to_scsi_device(dev);
  261. if (action == BUS_NOTIFY_ADD_DEVICE) {
  262. devinfo = device_handler_match(NULL, sdev);
  263. if (!devinfo)
  264. goto out;
  265. err = scsi_dh_handler_attach(sdev, devinfo);
  266. if (!err)
  267. err = device_create_file(dev, &scsi_dh_state_attr);
  268. } else if (action == BUS_NOTIFY_DEL_DEVICE) {
  269. device_remove_file(dev, &scsi_dh_state_attr);
  270. scsi_dh_handler_detach(sdev, NULL);
  271. }
  272. out:
  273. return err;
  274. }
  275. /*
  276. * scsi_dh_notifier_add - Callback for scsi_register_device_handler
  277. */
  278. static int scsi_dh_notifier_add(struct device *dev, void *data)
  279. {
  280. struct scsi_device_handler *scsi_dh = data;
  281. struct scsi_device *sdev;
  282. if (!scsi_is_sdev_device(dev))
  283. return 0;
  284. if (!get_device(dev))
  285. return 0;
  286. sdev = to_scsi_device(dev);
  287. if (device_handler_match(scsi_dh, sdev))
  288. scsi_dh_handler_attach(sdev, scsi_dh);
  289. put_device(dev);
  290. return 0;
  291. }
  292. /*
  293. * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
  294. */
  295. static int scsi_dh_notifier_remove(struct device *dev, void *data)
  296. {
  297. struct scsi_device_handler *scsi_dh = data;
  298. struct scsi_device *sdev;
  299. if (!scsi_is_sdev_device(dev))
  300. return 0;
  301. if (!get_device(dev))
  302. return 0;
  303. sdev = to_scsi_device(dev);
  304. scsi_dh_handler_detach(sdev, scsi_dh);
  305. put_device(dev);
  306. return 0;
  307. }
  308. /*
  309. * scsi_register_device_handler - register a device handler personality
  310. * module.
  311. * @scsi_dh - device handler to be registered.
  312. *
  313. * Returns 0 on success, -EBUSY if handler already registered.
  314. */
  315. int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
  316. {
  317. if (get_device_handler(scsi_dh->name))
  318. return -EBUSY;
  319. spin_lock(&list_lock);
  320. list_add(&scsi_dh->list, &scsi_dh_list);
  321. spin_unlock(&list_lock);
  322. bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
  323. printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
  324. return SCSI_DH_OK;
  325. }
  326. EXPORT_SYMBOL_GPL(scsi_register_device_handler);
  327. /*
  328. * scsi_unregister_device_handler - register a device handler personality
  329. * module.
  330. * @scsi_dh - device handler to be unregistered.
  331. *
  332. * Returns 0 on success, -ENODEV if handler not registered.
  333. */
  334. int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
  335. {
  336. struct scsi_dh_devinfo_list *tmp, *pos;
  337. if (!get_device_handler(scsi_dh->name))
  338. return -ENODEV;
  339. bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
  340. scsi_dh_notifier_remove);
  341. spin_lock(&list_lock);
  342. list_del(&scsi_dh->list);
  343. list_for_each_entry_safe(pos, tmp, &scsi_dh_dev_list, node) {
  344. if (pos->handler == scsi_dh) {
  345. list_del(&pos->node);
  346. kfree(pos);
  347. }
  348. }
  349. spin_unlock(&list_lock);
  350. printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
  351. return SCSI_DH_OK;
  352. }
  353. EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
  354. /*
  355. * scsi_dh_activate - activate the path associated with the scsi_device
  356. * corresponding to the given request queue.
  357. * @q - Request queue that is associated with the scsi_device to be
  358. * activated.
  359. */
  360. int scsi_dh_activate(struct request_queue *q)
  361. {
  362. int err = 0;
  363. unsigned long flags;
  364. struct scsi_device *sdev;
  365. struct scsi_device_handler *scsi_dh = NULL;
  366. spin_lock_irqsave(q->queue_lock, flags);
  367. sdev = q->queuedata;
  368. if (sdev && sdev->scsi_dh_data)
  369. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  370. if (!scsi_dh || !get_device(&sdev->sdev_gendev))
  371. err = SCSI_DH_NOSYS;
  372. spin_unlock_irqrestore(q->queue_lock, flags);
  373. if (err)
  374. return err;
  375. if (scsi_dh->activate)
  376. err = scsi_dh->activate(sdev);
  377. put_device(&sdev->sdev_gendev);
  378. return err;
  379. }
  380. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  381. /*
  382. * scsi_dh_set_params - set the parameters for the device as per the
  383. * string specified in params.
  384. * @q - Request queue that is associated with the scsi_device for
  385. * which the parameters to be set.
  386. * @params - parameters in the following format
  387. * "no_of_params\0param1\0param2\0param3\0...\0"
  388. * for example, string for 2 parameters with value 10 and 21
  389. * is specified as "2\010\021\0".
  390. */
  391. int scsi_dh_set_params(struct request_queue *q, const char *params)
  392. {
  393. int err = -SCSI_DH_NOSYS;
  394. unsigned long flags;
  395. struct scsi_device *sdev;
  396. struct scsi_device_handler *scsi_dh = NULL;
  397. spin_lock_irqsave(q->queue_lock, flags);
  398. sdev = q->queuedata;
  399. if (sdev && sdev->scsi_dh_data)
  400. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  401. if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
  402. err = 0;
  403. spin_unlock_irqrestore(q->queue_lock, flags);
  404. if (err)
  405. return err;
  406. err = scsi_dh->set_params(sdev, params);
  407. put_device(&sdev->sdev_gendev);
  408. return err;
  409. }
  410. EXPORT_SYMBOL_GPL(scsi_dh_set_params);
  411. /*
  412. * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
  413. * the given name. FALSE(0) otherwise.
  414. * @name - name of the device handler.
  415. */
  416. int scsi_dh_handler_exist(const char *name)
  417. {
  418. return (get_device_handler(name) != NULL);
  419. }
  420. EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
  421. /*
  422. * scsi_dh_handler_attach - Attach device handler
  423. * @sdev - sdev the handler should be attached to
  424. * @name - name of the handler to attach
  425. */
  426. int scsi_dh_attach(struct request_queue *q, const char *name)
  427. {
  428. unsigned long flags;
  429. struct scsi_device *sdev;
  430. struct scsi_device_handler *scsi_dh;
  431. int err = 0;
  432. scsi_dh = get_device_handler(name);
  433. if (!scsi_dh)
  434. return -EINVAL;
  435. spin_lock_irqsave(q->queue_lock, flags);
  436. sdev = q->queuedata;
  437. if (!sdev || !get_device(&sdev->sdev_gendev))
  438. err = -ENODEV;
  439. spin_unlock_irqrestore(q->queue_lock, flags);
  440. if (!err) {
  441. err = scsi_dh_handler_attach(sdev, scsi_dh);
  442. put_device(&sdev->sdev_gendev);
  443. }
  444. return err;
  445. }
  446. EXPORT_SYMBOL_GPL(scsi_dh_attach);
  447. /*
  448. * scsi_dh_handler_detach - Detach device handler
  449. * @sdev - sdev the handler should be detached from
  450. *
  451. * This function will detach the device handler only
  452. * if the sdev is not part of the internal list, ie
  453. * if it has been attached manually.
  454. */
  455. void scsi_dh_detach(struct request_queue *q)
  456. {
  457. unsigned long flags;
  458. struct scsi_device *sdev;
  459. struct scsi_device_handler *scsi_dh = NULL;
  460. spin_lock_irqsave(q->queue_lock, flags);
  461. sdev = q->queuedata;
  462. if (!sdev || !get_device(&sdev->sdev_gendev))
  463. sdev = NULL;
  464. spin_unlock_irqrestore(q->queue_lock, flags);
  465. if (!sdev)
  466. return;
  467. if (sdev->scsi_dh_data) {
  468. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  469. scsi_dh_handler_detach(sdev, scsi_dh);
  470. }
  471. put_device(&sdev->sdev_gendev);
  472. }
  473. EXPORT_SYMBOL_GPL(scsi_dh_detach);
  474. static struct notifier_block scsi_dh_nb = {
  475. .notifier_call = scsi_dh_notifier
  476. };
  477. static int __init scsi_dh_init(void)
  478. {
  479. int r;
  480. r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
  481. if (!r)
  482. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  483. scsi_dh_sysfs_attr_add);
  484. return r;
  485. }
  486. static void __exit scsi_dh_exit(void)
  487. {
  488. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  489. scsi_dh_sysfs_attr_remove);
  490. bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
  491. }
  492. module_init(scsi_dh_init);
  493. module_exit(scsi_dh_exit);
  494. MODULE_DESCRIPTION("SCSI device handler");
  495. MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
  496. MODULE_LICENSE("GPL");