scsi_dh.c 15 KB

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