scsi_dh.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. sdev->sdev_state == SDEV_CANCEL ||
  378. sdev->sdev_state == SDEV_DEL)
  379. err = SCSI_DH_NOSYS;
  380. if (sdev->sdev_state == SDEV_OFFLINE)
  381. err = SCSI_DH_DEV_OFFLINED;
  382. spin_unlock_irqrestore(q->queue_lock, flags);
  383. if (err) {
  384. if (fn)
  385. fn(data, err);
  386. return err;
  387. }
  388. if (scsi_dh->activate)
  389. err = scsi_dh->activate(sdev, fn, data);
  390. put_device(&sdev->sdev_gendev);
  391. return err;
  392. }
  393. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  394. /*
  395. * scsi_dh_set_params - set the parameters for the device as per the
  396. * string specified in params.
  397. * @q - Request queue that is associated with the scsi_device for
  398. * which the parameters to be set.
  399. * @params - parameters in the following format
  400. * "no_of_params\0param1\0param2\0param3\0...\0"
  401. * for example, string for 2 parameters with value 10 and 21
  402. * is specified as "2\010\021\0".
  403. */
  404. int scsi_dh_set_params(struct request_queue *q, const char *params)
  405. {
  406. int err = -SCSI_DH_NOSYS;
  407. unsigned long flags;
  408. struct scsi_device *sdev;
  409. struct scsi_device_handler *scsi_dh = NULL;
  410. spin_lock_irqsave(q->queue_lock, flags);
  411. sdev = q->queuedata;
  412. if (sdev && sdev->scsi_dh_data)
  413. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  414. if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
  415. err = 0;
  416. spin_unlock_irqrestore(q->queue_lock, flags);
  417. if (err)
  418. return err;
  419. err = scsi_dh->set_params(sdev, params);
  420. put_device(&sdev->sdev_gendev);
  421. return err;
  422. }
  423. EXPORT_SYMBOL_GPL(scsi_dh_set_params);
  424. /*
  425. * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
  426. * the given name. FALSE(0) otherwise.
  427. * @name - name of the device handler.
  428. */
  429. int scsi_dh_handler_exist(const char *name)
  430. {
  431. return (get_device_handler(name) != NULL);
  432. }
  433. EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
  434. /*
  435. * scsi_dh_handler_attach - Attach device handler
  436. * @sdev - sdev the handler should be attached to
  437. * @name - name of the handler to attach
  438. */
  439. int scsi_dh_attach(struct request_queue *q, const char *name)
  440. {
  441. unsigned long flags;
  442. struct scsi_device *sdev;
  443. struct scsi_device_handler *scsi_dh;
  444. int err = 0;
  445. scsi_dh = get_device_handler(name);
  446. if (!scsi_dh)
  447. return -EINVAL;
  448. spin_lock_irqsave(q->queue_lock, flags);
  449. sdev = q->queuedata;
  450. if (!sdev || !get_device(&sdev->sdev_gendev))
  451. err = -ENODEV;
  452. spin_unlock_irqrestore(q->queue_lock, flags);
  453. if (!err) {
  454. err = scsi_dh_handler_attach(sdev, scsi_dh);
  455. put_device(&sdev->sdev_gendev);
  456. }
  457. return err;
  458. }
  459. EXPORT_SYMBOL_GPL(scsi_dh_attach);
  460. /*
  461. * scsi_dh_handler_detach - Detach device handler
  462. * @sdev - sdev the handler should be detached from
  463. *
  464. * This function will detach the device handler only
  465. * if the sdev is not part of the internal list, ie
  466. * if it has been attached manually.
  467. */
  468. void scsi_dh_detach(struct request_queue *q)
  469. {
  470. unsigned long flags;
  471. struct scsi_device *sdev;
  472. struct scsi_device_handler *scsi_dh = NULL;
  473. spin_lock_irqsave(q->queue_lock, flags);
  474. sdev = q->queuedata;
  475. if (!sdev || !get_device(&sdev->sdev_gendev))
  476. sdev = NULL;
  477. spin_unlock_irqrestore(q->queue_lock, flags);
  478. if (!sdev)
  479. return;
  480. if (sdev->scsi_dh_data) {
  481. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  482. scsi_dh_handler_detach(sdev, scsi_dh);
  483. }
  484. put_device(&sdev->sdev_gendev);
  485. }
  486. EXPORT_SYMBOL_GPL(scsi_dh_detach);
  487. static struct notifier_block scsi_dh_nb = {
  488. .notifier_call = scsi_dh_notifier
  489. };
  490. static int __init scsi_dh_init(void)
  491. {
  492. int r;
  493. r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
  494. if (!r)
  495. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  496. scsi_dh_sysfs_attr_add);
  497. return r;
  498. }
  499. static void __exit scsi_dh_exit(void)
  500. {
  501. bus_for_each_dev(&scsi_bus_type, NULL, NULL,
  502. scsi_dh_sysfs_attr_remove);
  503. bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
  504. }
  505. module_init(scsi_dh_init);
  506. module_exit(scsi_dh_exit);
  507. MODULE_DESCRIPTION("SCSI device handler");
  508. MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
  509. MODULE_LICENSE("GPL");