scsi_dh.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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, NULL, NULL);
  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. err = device_create_file(dev, &scsi_dh_state_attr);
  263. /* don't care about err */
  264. devinfo = device_handler_match(NULL, sdev);
  265. if (devinfo)
  266. err = scsi_dh_handler_attach(sdev, devinfo);
  267. } else if (action == BUS_NOTIFY_DEL_DEVICE) {
  268. device_remove_file(dev, &scsi_dh_state_attr);
  269. scsi_dh_handler_detach(sdev, NULL);
  270. }
  271. return err;
  272. }
  273. /*
  274. * scsi_dh_notifier_add - Callback for scsi_register_device_handler
  275. */
  276. static int scsi_dh_notifier_add(struct device *dev, void *data)
  277. {
  278. struct scsi_device_handler *scsi_dh = data;
  279. struct scsi_device *sdev;
  280. if (!scsi_is_sdev_device(dev))
  281. return 0;
  282. if (!get_device(dev))
  283. return 0;
  284. sdev = to_scsi_device(dev);
  285. if (device_handler_match(scsi_dh, sdev))
  286. scsi_dh_handler_attach(sdev, scsi_dh);
  287. put_device(dev);
  288. return 0;
  289. }
  290. /*
  291. * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
  292. */
  293. static int scsi_dh_notifier_remove(struct device *dev, void *data)
  294. {
  295. struct scsi_device_handler *scsi_dh = data;
  296. struct scsi_device *sdev;
  297. if (!scsi_is_sdev_device(dev))
  298. return 0;
  299. if (!get_device(dev))
  300. return 0;
  301. sdev = to_scsi_device(dev);
  302. scsi_dh_handler_detach(sdev, scsi_dh);
  303. put_device(dev);
  304. return 0;
  305. }
  306. /*
  307. * scsi_register_device_handler - register a device handler personality
  308. * module.
  309. * @scsi_dh - device handler to be registered.
  310. *
  311. * Returns 0 on success, -EBUSY if handler already registered.
  312. */
  313. int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
  314. {
  315. if (get_device_handler(scsi_dh->name))
  316. return -EBUSY;
  317. spin_lock(&list_lock);
  318. list_add(&scsi_dh->list, &scsi_dh_list);
  319. spin_unlock(&list_lock);
  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. struct scsi_dh_devinfo_list *tmp, *pos;
  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. spin_lock(&list_lock);
  340. list_del(&scsi_dh->list);
  341. list_for_each_entry_safe(pos, tmp, &scsi_dh_dev_list, node) {
  342. if (pos->handler == scsi_dh) {
  343. list_del(&pos->node);
  344. kfree(pos);
  345. }
  346. }
  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. spin_lock_irqsave(q->queue_lock, flags);
  372. sdev = q->queuedata;
  373. if (sdev && sdev->scsi_dh_data)
  374. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  375. if (!scsi_dh || !get_device(&sdev->sdev_gendev))
  376. err = SCSI_DH_NOSYS;
  377. spin_unlock_irqrestore(q->queue_lock, flags);
  378. if (err)
  379. return err;
  380. if (scsi_dh->activate)
  381. err = scsi_dh->activate(sdev, fn, data);
  382. put_device(&sdev->sdev_gendev);
  383. return err;
  384. }
  385. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  386. /*
  387. * scsi_dh_set_params - set the parameters for the device as per the
  388. * string specified in params.
  389. * @q - Request queue that is associated with the scsi_device for
  390. * which the parameters to be set.
  391. * @params - parameters in the following format
  392. * "no_of_params\0param1\0param2\0param3\0...\0"
  393. * for example, string for 2 parameters with value 10 and 21
  394. * is specified as "2\010\021\0".
  395. */
  396. int scsi_dh_set_params(struct request_queue *q, const char *params)
  397. {
  398. int err = -SCSI_DH_NOSYS;
  399. unsigned long flags;
  400. struct scsi_device *sdev;
  401. struct scsi_device_handler *scsi_dh = NULL;
  402. spin_lock_irqsave(q->queue_lock, flags);
  403. sdev = q->queuedata;
  404. if (sdev && sdev->scsi_dh_data)
  405. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  406. if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
  407. err = 0;
  408. spin_unlock_irqrestore(q->queue_lock, flags);
  409. if (err)
  410. return err;
  411. err = scsi_dh->set_params(sdev, params);
  412. put_device(&sdev->sdev_gendev);
  413. return err;
  414. }
  415. EXPORT_SYMBOL_GPL(scsi_dh_set_params);
  416. /*
  417. * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
  418. * the given name. FALSE(0) otherwise.
  419. * @name - name of the device handler.
  420. */
  421. int scsi_dh_handler_exist(const char *name)
  422. {
  423. return (get_device_handler(name) != NULL);
  424. }
  425. EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
  426. /*
  427. * scsi_dh_handler_attach - Attach device handler
  428. * @sdev - sdev the handler should be attached to
  429. * @name - name of the handler to attach
  430. */
  431. int scsi_dh_attach(struct request_queue *q, const char *name)
  432. {
  433. unsigned long flags;
  434. struct scsi_device *sdev;
  435. struct scsi_device_handler *scsi_dh;
  436. int err = 0;
  437. scsi_dh = get_device_handler(name);
  438. if (!scsi_dh)
  439. return -EINVAL;
  440. spin_lock_irqsave(q->queue_lock, flags);
  441. sdev = q->queuedata;
  442. if (!sdev || !get_device(&sdev->sdev_gendev))
  443. err = -ENODEV;
  444. spin_unlock_irqrestore(q->queue_lock, flags);
  445. if (!err) {
  446. err = scsi_dh_handler_attach(sdev, scsi_dh);
  447. put_device(&sdev->sdev_gendev);
  448. }
  449. return err;
  450. }
  451. EXPORT_SYMBOL_GPL(scsi_dh_attach);
  452. /*
  453. * scsi_dh_handler_detach - Detach device handler
  454. * @sdev - sdev the handler should be detached from
  455. *
  456. * This function will detach the device handler only
  457. * if the sdev is not part of the internal list, ie
  458. * if it has been attached manually.
  459. */
  460. void scsi_dh_detach(struct request_queue *q)
  461. {
  462. unsigned long flags;
  463. struct scsi_device *sdev;
  464. struct scsi_device_handler *scsi_dh = NULL;
  465. spin_lock_irqsave(q->queue_lock, flags);
  466. sdev = q->queuedata;
  467. if (!sdev || !get_device(&sdev->sdev_gendev))
  468. sdev = NULL;
  469. spin_unlock_irqrestore(q->queue_lock, flags);
  470. if (!sdev)
  471. return;
  472. if (sdev->scsi_dh_data) {
  473. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  474. scsi_dh_handler_detach(sdev, scsi_dh);
  475. }
  476. put_device(&sdev->sdev_gendev);
  477. }
  478. EXPORT_SYMBOL_GPL(scsi_dh_detach);
  479. static struct notifier_block scsi_dh_nb = {
  480. .notifier_call = scsi_dh_notifier
  481. };
  482. static int __init scsi_dh_init(void)
  483. {
  484. int r;
  485. r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
  486. if (!r)
  487. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  488. scsi_dh_sysfs_attr_add);
  489. return r;
  490. }
  491. static void __exit scsi_dh_exit(void)
  492. {
  493. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  494. scsi_dh_sysfs_attr_remove);
  495. bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
  496. }
  497. module_init(scsi_dh_init);
  498. module_exit(scsi_dh_exit);
  499. MODULE_DESCRIPTION("SCSI device handler");
  500. MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
  501. MODULE_LICENSE("GPL");