scsi_transport_srp.c 25 KB

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