scsi_transport_srp.c 27 KB

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