scsi_transport_srp.c 26 KB

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