scsi_dh.c 15 KB

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