scsi_transport_srp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * SCSI RDMA (SRP) transport class
  3. *
  4. * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2 of the
  9. * License.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/err.h>
  25. #include <linux/slab.h>
  26. #include <linux/string.h>
  27. #include <scsi/scsi.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_transport.h>
  32. #include <scsi/scsi_transport_srp.h>
  33. #include "scsi_priv.h"
  34. struct srp_host_attrs {
  35. atomic_t next_port_id;
  36. };
  37. #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data)
  38. #define SRP_HOST_ATTRS 0
  39. #define SRP_RPORT_ATTRS 8
  40. struct srp_internal {
  41. struct scsi_transport_template t;
  42. struct srp_function_template *f;
  43. struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
  44. struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
  45. struct transport_container rport_attr_cont;
  46. };
  47. static int scsi_is_srp_rport(const struct device *dev);
  48. #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
  49. #define dev_to_rport(d) container_of(d, struct srp_rport, dev)
  50. #define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
  51. static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r)
  52. {
  53. return dev_to_shost(r->dev.parent);
  54. }
  55. static int find_child_rport(struct device *dev, void *data)
  56. {
  57. struct device **child = data;
  58. if (scsi_is_srp_rport(dev)) {
  59. WARN_ON_ONCE(*child);
  60. *child = dev;
  61. }
  62. return 0;
  63. }
  64. static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost)
  65. {
  66. struct device *child = NULL;
  67. WARN_ON_ONCE(device_for_each_child(&shost->shost_gendev, &child,
  68. find_child_rport) < 0);
  69. return child ? dev_to_rport(child) : NULL;
  70. }
  71. /**
  72. * srp_tmo_valid() - check timeout combination validity
  73. * @reconnect_delay: Reconnect delay in seconds.
  74. * @fast_io_fail_tmo: Fast I/O fail timeout in seconds.
  75. * @dev_loss_tmo: Device loss timeout in seconds.
  76. *
  77. * The combination of the timeout parameters must be such that SCSI commands
  78. * are finished in a reasonable time. Hence do not allow the fast I/O fail
  79. * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to
  80. * exceed that limit if failing I/O fast has been disabled. Furthermore, these
  81. * parameters must be such that multipath can detect failed paths timely.
  82. * Hence do not allow all three parameters to be disabled simultaneously.
  83. */
  84. int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, long dev_loss_tmo)
  85. {
  86. if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
  87. return -EINVAL;
  88. if (reconnect_delay == 0)
  89. return -EINVAL;
  90. if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
  91. return -EINVAL;
  92. if (fast_io_fail_tmo < 0 &&
  93. dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
  94. return -EINVAL;
  95. if (dev_loss_tmo >= LONG_MAX / HZ)
  96. return -EINVAL;
  97. if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
  98. fast_io_fail_tmo >= dev_loss_tmo)
  99. return -EINVAL;
  100. return 0;
  101. }
  102. EXPORT_SYMBOL_GPL(srp_tmo_valid);
  103. static int srp_host_setup(struct transport_container *tc, struct device *dev,
  104. struct device *cdev)
  105. {
  106. struct Scsi_Host *shost = dev_to_shost(dev);
  107. struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
  108. atomic_set(&srp_host->next_port_id, 0);
  109. return 0;
  110. }
  111. static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
  112. NULL, NULL);
  113. static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
  114. NULL, NULL, NULL);
  115. static ssize_t
  116. show_srp_rport_id(struct device *dev, struct device_attribute *attr,
  117. char *buf)
  118. {
  119. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  120. return sprintf(buf, "%16phC\n", rport->port_id);
  121. }
  122. static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
  123. static const struct {
  124. u32 value;
  125. char *name;
  126. } srp_rport_role_names[] = {
  127. {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
  128. {SRP_RPORT_ROLE_TARGET, "SRP Target"},
  129. };
  130. static ssize_t
  131. show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
  132. char *buf)
  133. {
  134. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  135. int i;
  136. char *name = NULL;
  137. for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
  138. if (srp_rport_role_names[i].value == rport->roles) {
  139. name = srp_rport_role_names[i].name;
  140. break;
  141. }
  142. return sprintf(buf, "%s\n", name ? : "unknown");
  143. }
  144. static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
  145. static ssize_t store_srp_rport_delete(struct device *dev,
  146. struct device_attribute *attr,
  147. const char *buf, size_t count)
  148. {
  149. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  150. struct Scsi_Host *shost = dev_to_shost(dev);
  151. struct srp_internal *i = to_srp_internal(shost->transportt);
  152. if (i->f->rport_delete) {
  153. i->f->rport_delete(rport);
  154. return count;
  155. } else {
  156. return -ENOSYS;
  157. }
  158. }
  159. static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
  160. static ssize_t show_srp_rport_state(struct device *dev,
  161. struct device_attribute *attr,
  162. char *buf)
  163. {
  164. static const char *const state_name[] = {
  165. [SRP_RPORT_RUNNING] = "running",
  166. [SRP_RPORT_BLOCKED] = "blocked",
  167. [SRP_RPORT_FAIL_FAST] = "fail-fast",
  168. [SRP_RPORT_LOST] = "lost",
  169. };
  170. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  171. enum srp_rport_state state = rport->state;
  172. return sprintf(buf, "%s\n",
  173. (unsigned)state < ARRAY_SIZE(state_name) ?
  174. state_name[state] : "???");
  175. }
  176. static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
  177. static ssize_t srp_show_tmo(char *buf, int tmo)
  178. {
  179. return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
  180. }
  181. int srp_parse_tmo(int *tmo, const char *buf)
  182. {
  183. int res = 0;
  184. if (strncmp(buf, "off", 3) != 0)
  185. res = kstrtoint(buf, 0, tmo);
  186. else
  187. *tmo = -1;
  188. return res;
  189. }
  190. EXPORT_SYMBOL(srp_parse_tmo);
  191. static ssize_t show_reconnect_delay(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  195. return srp_show_tmo(buf, rport->reconnect_delay);
  196. }
  197. static ssize_t store_reconnect_delay(struct device *dev,
  198. struct device_attribute *attr,
  199. const char *buf, const size_t count)
  200. {
  201. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  202. int res, delay;
  203. res = srp_parse_tmo(&delay, buf);
  204. if (res)
  205. goto out;
  206. res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
  207. rport->dev_loss_tmo);
  208. if (res)
  209. goto out;
  210. if (rport->reconnect_delay <= 0 && delay > 0 &&
  211. rport->state != SRP_RPORT_RUNNING) {
  212. queue_delayed_work(system_long_wq, &rport->reconnect_work,
  213. delay * HZ);
  214. } else if (delay <= 0) {
  215. cancel_delayed_work(&rport->reconnect_work);
  216. }
  217. rport->reconnect_delay = delay;
  218. res = count;
  219. out:
  220. return res;
  221. }
  222. static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
  223. store_reconnect_delay);
  224. static ssize_t show_failed_reconnects(struct device *dev,
  225. struct device_attribute *attr, char *buf)
  226. {
  227. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  228. return sprintf(buf, "%d\n", rport->failed_reconnects);
  229. }
  230. static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
  231. static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
  232. struct device_attribute *attr,
  233. char *buf)
  234. {
  235. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  236. return srp_show_tmo(buf, rport->fast_io_fail_tmo);
  237. }
  238. static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
  239. struct device_attribute *attr,
  240. const char *buf, size_t count)
  241. {
  242. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  243. int res;
  244. int fast_io_fail_tmo;
  245. res = srp_parse_tmo(&fast_io_fail_tmo, buf);
  246. if (res)
  247. goto out;
  248. res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
  249. rport->dev_loss_tmo);
  250. if (res)
  251. goto out;
  252. rport->fast_io_fail_tmo = fast_io_fail_tmo;
  253. res = count;
  254. out:
  255. return res;
  256. }
  257. static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
  258. show_srp_rport_fast_io_fail_tmo,
  259. store_srp_rport_fast_io_fail_tmo);
  260. static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
  261. struct device_attribute *attr,
  262. char *buf)
  263. {
  264. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  265. return srp_show_tmo(buf, rport->dev_loss_tmo);
  266. }
  267. static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
  268. struct device_attribute *attr,
  269. const char *buf, size_t count)
  270. {
  271. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  272. int res;
  273. int dev_loss_tmo;
  274. res = srp_parse_tmo(&dev_loss_tmo, buf);
  275. if (res)
  276. goto out;
  277. res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
  278. dev_loss_tmo);
  279. if (res)
  280. goto out;
  281. rport->dev_loss_tmo = dev_loss_tmo;
  282. res = count;
  283. out:
  284. return res;
  285. }
  286. static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
  287. show_srp_rport_dev_loss_tmo,
  288. store_srp_rport_dev_loss_tmo);
  289. static int srp_rport_set_state(struct srp_rport *rport,
  290. enum srp_rport_state new_state)
  291. {
  292. enum srp_rport_state old_state = rport->state;
  293. lockdep_assert_held(&rport->mutex);
  294. switch (new_state) {
  295. case SRP_RPORT_RUNNING:
  296. switch (old_state) {
  297. case SRP_RPORT_LOST:
  298. goto invalid;
  299. default:
  300. break;
  301. }
  302. break;
  303. case SRP_RPORT_BLOCKED:
  304. switch (old_state) {
  305. case SRP_RPORT_RUNNING:
  306. break;
  307. default:
  308. goto invalid;
  309. }
  310. break;
  311. case SRP_RPORT_FAIL_FAST:
  312. switch (old_state) {
  313. case SRP_RPORT_LOST:
  314. goto invalid;
  315. default:
  316. break;
  317. }
  318. break;
  319. case SRP_RPORT_LOST:
  320. break;
  321. }
  322. rport->state = new_state;
  323. return 0;
  324. invalid:
  325. return -EINVAL;
  326. }
  327. /**
  328. * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
  329. * @work: Work structure used for scheduling this operation.
  330. */
  331. static void srp_reconnect_work(struct work_struct *work)
  332. {
  333. struct srp_rport *rport = container_of(to_delayed_work(work),
  334. struct srp_rport, reconnect_work);
  335. struct Scsi_Host *shost = rport_to_shost(rport);
  336. int delay, res;
  337. res = srp_reconnect_rport(rport);
  338. if (res != 0) {
  339. shost_printk(KERN_ERR, shost,
  340. "reconnect attempt %d failed (%d)\n",
  341. ++rport->failed_reconnects, res);
  342. delay = rport->reconnect_delay *
  343. min(100, max(1, rport->failed_reconnects - 10));
  344. if (delay > 0)
  345. queue_delayed_work(system_long_wq,
  346. &rport->reconnect_work, delay * HZ);
  347. }
  348. }
  349. static void __rport_fail_io_fast(struct srp_rport *rport)
  350. {
  351. struct Scsi_Host *shost = rport_to_shost(rport);
  352. struct srp_internal *i;
  353. lockdep_assert_held(&rport->mutex);
  354. if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
  355. return;
  356. /*
  357. * Call scsi_target_block() to wait for ongoing shost->queuecommand()
  358. * calls before invoking i->f->terminate_rport_io().
  359. */
  360. scsi_target_block(rport->dev.parent);
  361. scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
  362. /* Involve the LLD if possible to terminate all I/O on the rport. */
  363. i = to_srp_internal(shost->transportt);
  364. if (i->f->terminate_rport_io)
  365. i->f->terminate_rport_io(rport);
  366. }
  367. /**
  368. * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
  369. * @work: Work structure used for scheduling this operation.
  370. */
  371. static void rport_fast_io_fail_timedout(struct work_struct *work)
  372. {
  373. struct srp_rport *rport = container_of(to_delayed_work(work),
  374. struct srp_rport, fast_io_fail_work);
  375. struct Scsi_Host *shost = rport_to_shost(rport);
  376. pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
  377. dev_name(&rport->dev), dev_name(&shost->shost_gendev));
  378. mutex_lock(&rport->mutex);
  379. if (rport->state == SRP_RPORT_BLOCKED)
  380. __rport_fail_io_fast(rport);
  381. mutex_unlock(&rport->mutex);
  382. }
  383. /**
  384. * rport_dev_loss_timedout() - device loss timeout handler
  385. * @work: Work structure used for scheduling this operation.
  386. */
  387. static void rport_dev_loss_timedout(struct work_struct *work)
  388. {
  389. struct srp_rport *rport = container_of(to_delayed_work(work),
  390. struct srp_rport, dev_loss_work);
  391. struct Scsi_Host *shost = rport_to_shost(rport);
  392. struct srp_internal *i = to_srp_internal(shost->transportt);
  393. pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
  394. dev_name(&rport->dev), dev_name(&shost->shost_gendev));
  395. mutex_lock(&rport->mutex);
  396. WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
  397. scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
  398. mutex_unlock(&rport->mutex);
  399. i->f->rport_delete(rport);
  400. }
  401. static void __srp_start_tl_fail_timers(struct srp_rport *rport)
  402. {
  403. struct Scsi_Host *shost = rport_to_shost(rport);
  404. int delay, fast_io_fail_tmo, dev_loss_tmo;
  405. lockdep_assert_held(&rport->mutex);
  406. delay = rport->reconnect_delay;
  407. fast_io_fail_tmo = rport->fast_io_fail_tmo;
  408. dev_loss_tmo = rport->dev_loss_tmo;
  409. pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
  410. rport->state);
  411. if (rport->state == SRP_RPORT_LOST)
  412. return;
  413. if (delay > 0)
  414. queue_delayed_work(system_long_wq, &rport->reconnect_work,
  415. 1UL * delay * HZ);
  416. if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) &&
  417. srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
  418. pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
  419. rport->state);
  420. scsi_target_block(&shost->shost_gendev);
  421. if (fast_io_fail_tmo >= 0)
  422. queue_delayed_work(system_long_wq,
  423. &rport->fast_io_fail_work,
  424. 1UL * fast_io_fail_tmo * HZ);
  425. if (dev_loss_tmo >= 0)
  426. queue_delayed_work(system_long_wq,
  427. &rport->dev_loss_work,
  428. 1UL * dev_loss_tmo * HZ);
  429. }
  430. }
  431. /**
  432. * srp_start_tl_fail_timers() - start the transport layer failure timers
  433. * @rport: SRP target port.
  434. *
  435. * Start the transport layer fast I/O failure and device loss timers. Do not
  436. * modify a timer that was already started.
  437. */
  438. void srp_start_tl_fail_timers(struct srp_rport *rport)
  439. {
  440. mutex_lock(&rport->mutex);
  441. __srp_start_tl_fail_timers(rport);
  442. mutex_unlock(&rport->mutex);
  443. }
  444. EXPORT_SYMBOL(srp_start_tl_fail_timers);
  445. /**
  446. * srp_reconnect_rport() - reconnect to an SRP target port
  447. * @rport: SRP target port.
  448. *
  449. * Blocks SCSI command queueing before invoking reconnect() such that
  450. * queuecommand() won't be invoked concurrently with reconnect() from outside
  451. * the SCSI EH. This is important since a reconnect() implementation may
  452. * reallocate resources needed by queuecommand().
  453. *
  454. * Notes:
  455. * - This function neither waits until outstanding requests have finished nor
  456. * tries to abort these. It is the responsibility of the reconnect()
  457. * function to finish outstanding commands before reconnecting to the target
  458. * port.
  459. * - It is the responsibility of the caller to ensure that the resources
  460. * reallocated by the reconnect() function won't be used while this function
  461. * is in progress. One possible strategy is to invoke this function from
  462. * the context of the SCSI EH thread only. Another possible strategy is to
  463. * lock the rport mutex inside each SCSI LLD callback that can be invoked by
  464. * the SCSI EH (the scsi_host_template.eh_*() functions and also the
  465. * scsi_host_template.queuecommand() function).
  466. */
  467. int srp_reconnect_rport(struct srp_rport *rport)
  468. {
  469. struct Scsi_Host *shost = rport_to_shost(rport);
  470. struct srp_internal *i = to_srp_internal(shost->transportt);
  471. struct scsi_device *sdev;
  472. int res;
  473. pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
  474. res = mutex_lock_interruptible(&rport->mutex);
  475. if (res)
  476. goto out;
  477. scsi_target_block(&shost->shost_gendev);
  478. res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
  479. pr_debug("%s (state %d): transport.reconnect() returned %d\n",
  480. dev_name(&shost->shost_gendev), rport->state, res);
  481. if (res == 0) {
  482. cancel_delayed_work(&rport->fast_io_fail_work);
  483. cancel_delayed_work(&rport->dev_loss_work);
  484. rport->failed_reconnects = 0;
  485. srp_rport_set_state(rport, SRP_RPORT_RUNNING);
  486. scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
  487. /*
  488. * If the SCSI error handler has offlined one or more devices,
  489. * invoking scsi_target_unblock() won't change the state of
  490. * these devices into running so do that explicitly.
  491. */
  492. shost_for_each_device(sdev, shost) {
  493. mutex_lock(&sdev->state_mutex);
  494. if (sdev->sdev_state == SDEV_OFFLINE)
  495. sdev->sdev_state = SDEV_RUNNING;
  496. mutex_unlock(&sdev->state_mutex);
  497. }
  498. } else if (rport->state == SRP_RPORT_RUNNING) {
  499. /*
  500. * srp_reconnect_rport() has been invoked with fast_io_fail
  501. * and dev_loss off. Mark the port as failed and start the TL
  502. * failure timers if these had not yet been started.
  503. */
  504. __rport_fail_io_fast(rport);
  505. scsi_target_unblock(&shost->shost_gendev,
  506. SDEV_TRANSPORT_OFFLINE);
  507. __srp_start_tl_fail_timers(rport);
  508. } else if (rport->state != SRP_RPORT_BLOCKED) {
  509. scsi_target_unblock(&shost->shost_gendev,
  510. SDEV_TRANSPORT_OFFLINE);
  511. }
  512. mutex_unlock(&rport->mutex);
  513. out:
  514. return res;
  515. }
  516. EXPORT_SYMBOL(srp_reconnect_rport);
  517. /**
  518. * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
  519. * @scmd: SCSI command.
  520. *
  521. * If a timeout occurs while an rport is in the blocked state, ask the SCSI
  522. * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
  523. * handle the timeout (BLK_EH_DONE).
  524. *
  525. * Note: This function is called from soft-IRQ context and with the request
  526. * queue lock held.
  527. */
  528. enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
  529. {
  530. struct scsi_device *sdev = scmd->device;
  531. struct Scsi_Host *shost = sdev->host;
  532. struct srp_internal *i = to_srp_internal(shost->transportt);
  533. struct srp_rport *rport = shost_to_rport(shost);
  534. pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
  535. return rport && rport->fast_io_fail_tmo < 0 &&
  536. rport->dev_loss_tmo < 0 &&
  537. i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
  538. BLK_EH_RESET_TIMER : BLK_EH_DONE;
  539. }
  540. EXPORT_SYMBOL(srp_timed_out);
  541. static void srp_rport_release(struct device *dev)
  542. {
  543. struct srp_rport *rport = dev_to_rport(dev);
  544. put_device(dev->parent);
  545. kfree(rport);
  546. }
  547. static int scsi_is_srp_rport(const struct device *dev)
  548. {
  549. return dev->release == srp_rport_release;
  550. }
  551. static int srp_rport_match(struct attribute_container *cont,
  552. struct device *dev)
  553. {
  554. struct Scsi_Host *shost;
  555. struct srp_internal *i;
  556. if (!scsi_is_srp_rport(dev))
  557. return 0;
  558. shost = dev_to_shost(dev->parent);
  559. if (!shost->transportt)
  560. return 0;
  561. if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
  562. return 0;
  563. i = to_srp_internal(shost->transportt);
  564. return &i->rport_attr_cont.ac == cont;
  565. }
  566. static int srp_host_match(struct attribute_container *cont, struct device *dev)
  567. {
  568. struct Scsi_Host *shost;
  569. struct srp_internal *i;
  570. if (!scsi_is_host_device(dev))
  571. return 0;
  572. shost = dev_to_shost(dev);
  573. if (!shost->transportt)
  574. return 0;
  575. if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
  576. return 0;
  577. i = to_srp_internal(shost->transportt);
  578. return &i->t.host_attrs.ac == cont;
  579. }
  580. /**
  581. * srp_rport_get() - increment rport reference count
  582. * @rport: SRP target port.
  583. */
  584. void srp_rport_get(struct srp_rport *rport)
  585. {
  586. get_device(&rport->dev);
  587. }
  588. EXPORT_SYMBOL(srp_rport_get);
  589. /**
  590. * srp_rport_put() - decrement rport reference count
  591. * @rport: SRP target port.
  592. */
  593. void srp_rport_put(struct srp_rport *rport)
  594. {
  595. put_device(&rport->dev);
  596. }
  597. EXPORT_SYMBOL(srp_rport_put);
  598. /**
  599. * srp_rport_add - add a SRP remote port to the device hierarchy
  600. * @shost: scsi host the remote port is connected to.
  601. * @ids: The port id for the remote port.
  602. *
  603. * Publishes a port to the rest of the system.
  604. */
  605. struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
  606. struct srp_rport_identifiers *ids)
  607. {
  608. struct srp_rport *rport;
  609. struct device *parent = &shost->shost_gendev;
  610. struct srp_internal *i = to_srp_internal(shost->transportt);
  611. int id, ret;
  612. rport = kzalloc(sizeof(*rport), GFP_KERNEL);
  613. if (!rport)
  614. return ERR_PTR(-ENOMEM);
  615. mutex_init(&rport->mutex);
  616. device_initialize(&rport->dev);
  617. rport->dev.parent = get_device(parent);
  618. rport->dev.release = srp_rport_release;
  619. memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
  620. rport->roles = ids->roles;
  621. if (i->f->reconnect)
  622. rport->reconnect_delay = i->f->reconnect_delay ?
  623. *i->f->reconnect_delay : 10;
  624. INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
  625. rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
  626. *i->f->fast_io_fail_tmo : 15;
  627. rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
  628. INIT_DELAYED_WORK(&rport->fast_io_fail_work,
  629. rport_fast_io_fail_timedout);
  630. INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
  631. id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
  632. dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
  633. transport_setup_device(&rport->dev);
  634. ret = device_add(&rport->dev);
  635. if (ret) {
  636. transport_destroy_device(&rport->dev);
  637. put_device(&rport->dev);
  638. return ERR_PTR(ret);
  639. }
  640. transport_add_device(&rport->dev);
  641. transport_configure_device(&rport->dev);
  642. return rport;
  643. }
  644. EXPORT_SYMBOL_GPL(srp_rport_add);
  645. /**
  646. * srp_rport_del - remove a SRP remote port
  647. * @rport: SRP remote port to remove
  648. *
  649. * Removes the specified SRP remote port.
  650. */
  651. void srp_rport_del(struct srp_rport *rport)
  652. {
  653. struct device *dev = &rport->dev;
  654. transport_remove_device(dev);
  655. device_del(dev);
  656. transport_destroy_device(dev);
  657. put_device(dev);
  658. }
  659. EXPORT_SYMBOL_GPL(srp_rport_del);
  660. static int do_srp_rport_del(struct device *dev, void *data)
  661. {
  662. if (scsi_is_srp_rport(dev))
  663. srp_rport_del(dev_to_rport(dev));
  664. return 0;
  665. }
  666. /**
  667. * srp_remove_host - tear down a Scsi_Host's SRP data structures
  668. * @shost: Scsi Host that is torn down
  669. *
  670. * Removes all SRP remote ports for a given Scsi_Host.
  671. * Must be called just before scsi_remove_host for SRP HBAs.
  672. */
  673. void srp_remove_host(struct Scsi_Host *shost)
  674. {
  675. device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
  676. }
  677. EXPORT_SYMBOL_GPL(srp_remove_host);
  678. /**
  679. * srp_stop_rport_timers - stop the transport layer recovery timers
  680. * @rport: SRP remote port for which to stop the timers.
  681. *
  682. * Must be called after srp_remove_host() and scsi_remove_host(). The caller
  683. * must hold a reference on the rport (rport->dev) and on the SCSI host
  684. * (rport->dev.parent).
  685. */
  686. void srp_stop_rport_timers(struct srp_rport *rport)
  687. {
  688. mutex_lock(&rport->mutex);
  689. if (rport->state == SRP_RPORT_BLOCKED)
  690. __rport_fail_io_fast(rport);
  691. srp_rport_set_state(rport, SRP_RPORT_LOST);
  692. mutex_unlock(&rport->mutex);
  693. cancel_delayed_work_sync(&rport->reconnect_work);
  694. cancel_delayed_work_sync(&rport->fast_io_fail_work);
  695. cancel_delayed_work_sync(&rport->dev_loss_work);
  696. }
  697. EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
  698. /**
  699. * srp_attach_transport - instantiate SRP transport template
  700. * @ft: SRP transport class function template
  701. */
  702. struct scsi_transport_template *
  703. srp_attach_transport(struct srp_function_template *ft)
  704. {
  705. int count;
  706. struct srp_internal *i;
  707. i = kzalloc(sizeof(*i), GFP_KERNEL);
  708. if (!i)
  709. return NULL;
  710. i->t.host_size = sizeof(struct srp_host_attrs);
  711. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  712. i->t.host_attrs.ac.class = &srp_host_class.class;
  713. i->t.host_attrs.ac.match = srp_host_match;
  714. i->host_attrs[0] = NULL;
  715. transport_container_register(&i->t.host_attrs);
  716. i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  717. i->rport_attr_cont.ac.class = &srp_rport_class.class;
  718. i->rport_attr_cont.ac.match = srp_rport_match;
  719. count = 0;
  720. i->rport_attrs[count++] = &dev_attr_port_id;
  721. i->rport_attrs[count++] = &dev_attr_roles;
  722. if (ft->has_rport_state) {
  723. i->rport_attrs[count++] = &dev_attr_state;
  724. i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
  725. i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
  726. }
  727. if (ft->reconnect) {
  728. i->rport_attrs[count++] = &dev_attr_reconnect_delay;
  729. i->rport_attrs[count++] = &dev_attr_failed_reconnects;
  730. }
  731. if (ft->rport_delete)
  732. i->rport_attrs[count++] = &dev_attr_delete;
  733. i->rport_attrs[count++] = NULL;
  734. BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
  735. transport_container_register(&i->rport_attr_cont);
  736. i->f = ft;
  737. return &i->t;
  738. }
  739. EXPORT_SYMBOL_GPL(srp_attach_transport);
  740. /**
  741. * srp_release_transport - release SRP transport template instance
  742. * @t: transport template instance
  743. */
  744. void srp_release_transport(struct scsi_transport_template *t)
  745. {
  746. struct srp_internal *i = to_srp_internal(t);
  747. transport_container_unregister(&i->t.host_attrs);
  748. transport_container_unregister(&i->rport_attr_cont);
  749. kfree(i);
  750. }
  751. EXPORT_SYMBOL_GPL(srp_release_transport);
  752. static __init int srp_transport_init(void)
  753. {
  754. int ret;
  755. ret = transport_class_register(&srp_host_class);
  756. if (ret)
  757. return ret;
  758. ret = transport_class_register(&srp_rport_class);
  759. if (ret)
  760. goto unregister_host_class;
  761. return 0;
  762. unregister_host_class:
  763. transport_class_unregister(&srp_host_class);
  764. return ret;
  765. }
  766. static void __exit srp_transport_exit(void)
  767. {
  768. transport_class_unregister(&srp_host_class);
  769. transport_class_unregister(&srp_rport_class);
  770. }
  771. MODULE_AUTHOR("FUJITA Tomonori");
  772. MODULE_DESCRIPTION("SRP Transport Attributes");
  773. MODULE_LICENSE("GPL");
  774. module_init(srp_transport_init);
  775. module_exit(srp_transport_exit);