scsi_dh.c 15 KB

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