scsi_transport_srp.c 23 KB

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