scsi_transport_srp.c 25 KB

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