scsi_transport_srp.c 25 KB

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