cxgb3i_iscsi.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /* cxgb3i_iscsi.c: Chelsio S3xx iSCSI driver.
  2. *
  3. * Copyright (c) 2008 Chelsio Communications, Inc.
  4. * Copyright (c) 2008 Mike Christie
  5. * Copyright (c) 2008 Red Hat, Inc. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation.
  10. *
  11. * Written by: Karen Xie (kxie@chelsio.com)
  12. */
  13. #include <linux/inet.h>
  14. #include <linux/crypto.h>
  15. #include <net/dst.h>
  16. #include <net/tcp.h>
  17. #include <scsi/scsi_cmnd.h>
  18. #include <scsi/scsi_device.h>
  19. #include <scsi/scsi_eh.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/scsi.h>
  22. #include <scsi/iscsi_proto.h>
  23. #include <scsi/libiscsi.h>
  24. #include <scsi/scsi_transport_iscsi.h>
  25. #include "cxgb3i.h"
  26. #include "cxgb3i_pdu.h"
  27. #ifdef __DEBUG_CXGB3I_TAG__
  28. #define cxgb3i_tag_debug cxgb3i_log_debug
  29. #else
  30. #define cxgb3i_tag_debug(fmt...)
  31. #endif
  32. #ifdef __DEBUG_CXGB3I_API__
  33. #define cxgb3i_api_debug cxgb3i_log_debug
  34. #else
  35. #define cxgb3i_api_debug(fmt...)
  36. #endif
  37. /*
  38. * align pdu size to multiple of 512 for better performance
  39. */
  40. #define align_pdu_size(n) do { n = (n) & (~511); } while (0)
  41. static struct scsi_transport_template *cxgb3i_scsi_transport;
  42. static struct scsi_host_template cxgb3i_host_template;
  43. static struct iscsi_transport cxgb3i_iscsi_transport;
  44. static unsigned char sw_tag_idx_bits;
  45. static unsigned char sw_tag_age_bits;
  46. static LIST_HEAD(cxgb3i_snic_list);
  47. static DEFINE_RWLOCK(cxgb3i_snic_rwlock);
  48. /**
  49. * cxgb3i_adpater_find_by_tdev - find the cxgb3i_adapter structure via t3cdev
  50. * @tdev: t3cdev pointer
  51. */
  52. struct cxgb3i_adapter *cxgb3i_adapter_find_by_tdev(struct t3cdev *tdev)
  53. {
  54. struct cxgb3i_adapter *snic;
  55. read_lock(&cxgb3i_snic_rwlock);
  56. list_for_each_entry(snic, &cxgb3i_snic_list, list_head) {
  57. if (snic->tdev == tdev) {
  58. read_unlock(&cxgb3i_snic_rwlock);
  59. return snic;
  60. }
  61. }
  62. read_unlock(&cxgb3i_snic_rwlock);
  63. return NULL;
  64. }
  65. static inline int adapter_update(struct cxgb3i_adapter *snic)
  66. {
  67. cxgb3i_log_info("snic 0x%p, t3dev 0x%p, updating.\n",
  68. snic, snic->tdev);
  69. return cxgb3i_adapter_ddp_info(snic->tdev, &snic->tag_format,
  70. &snic->tx_max_size,
  71. &snic->rx_max_size);
  72. }
  73. static int adapter_add(struct cxgb3i_adapter *snic)
  74. {
  75. struct t3cdev *t3dev = snic->tdev;
  76. struct adapter *adapter = tdev2adap(t3dev);
  77. int i, err;
  78. snic->pdev = adapter->pdev;
  79. snic->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
  80. err = cxgb3i_adapter_ddp_info(t3dev, &snic->tag_format,
  81. &snic->tx_max_size,
  82. &snic->rx_max_size);
  83. if (err < 0)
  84. return err;
  85. for_each_port(adapter, i) {
  86. snic->hba[i] = cxgb3i_hba_host_add(snic, adapter->port[i]);
  87. if (!snic->hba[i])
  88. return -EINVAL;
  89. }
  90. snic->hba_cnt = adapter->params.nports;
  91. /* add to the list */
  92. write_lock(&cxgb3i_snic_rwlock);
  93. list_add_tail(&snic->list_head, &cxgb3i_snic_list);
  94. write_unlock(&cxgb3i_snic_rwlock);
  95. cxgb3i_log_info("t3dev 0x%p open, snic 0x%p, %u scsi hosts added.\n",
  96. t3dev, snic, snic->hba_cnt);
  97. return 0;
  98. }
  99. /**
  100. * cxgb3i_adapter_open - init a s3 adapter structure and any h/w settings
  101. * @t3dev: t3cdev adapter
  102. */
  103. void cxgb3i_adapter_open(struct t3cdev *t3dev)
  104. {
  105. struct cxgb3i_adapter *snic = cxgb3i_adapter_find_by_tdev(t3dev);
  106. int err;
  107. if (snic)
  108. err = adapter_update(snic);
  109. else {
  110. snic = kzalloc(sizeof(*snic), GFP_KERNEL);
  111. if (snic) {
  112. spin_lock_init(&snic->lock);
  113. snic->tdev = t3dev;
  114. err = adapter_add(snic);
  115. } else
  116. err = -ENOMEM;
  117. }
  118. if (err < 0) {
  119. cxgb3i_log_info("snic 0x%p, f 0x%x, t3dev 0x%p open, err %d.\n",
  120. snic, snic ? snic->flags : 0, t3dev, err);
  121. if (snic) {
  122. snic->flags &= ~CXGB3I_ADAPTER_FLAG_RESET;
  123. cxgb3i_adapter_close(t3dev);
  124. }
  125. }
  126. }
  127. /**
  128. * cxgb3i_adapter_close - release the resources held and cleanup h/w settings
  129. * @t3dev: t3cdev adapter
  130. */
  131. void cxgb3i_adapter_close(struct t3cdev *t3dev)
  132. {
  133. struct cxgb3i_adapter *snic = cxgb3i_adapter_find_by_tdev(t3dev);
  134. int i;
  135. if (!snic || snic->flags & CXGB3I_ADAPTER_FLAG_RESET) {
  136. cxgb3i_log_info("t3dev 0x%p close, snic 0x%p, f 0x%x.\n",
  137. t3dev, snic, snic ? snic->flags : 0);
  138. return;
  139. }
  140. /* remove from the list */
  141. write_lock(&cxgb3i_snic_rwlock);
  142. list_del(&snic->list_head);
  143. write_unlock(&cxgb3i_snic_rwlock);
  144. for (i = 0; i < snic->hba_cnt; i++) {
  145. if (snic->hba[i]) {
  146. cxgb3i_hba_host_remove(snic->hba[i]);
  147. snic->hba[i] = NULL;
  148. }
  149. }
  150. cxgb3i_log_info("t3dev 0x%p close, snic 0x%p, %u scsi hosts removed.\n",
  151. t3dev, snic, snic->hba_cnt);
  152. kfree(snic);
  153. }
  154. /**
  155. * cxgb3i_hba_find_by_netdev - find the cxgb3i_hba structure via net_device
  156. * @t3dev: t3cdev adapter
  157. */
  158. static struct cxgb3i_hba *cxgb3i_hba_find_by_netdev(struct net_device *ndev)
  159. {
  160. struct cxgb3i_adapter *snic;
  161. int i;
  162. read_lock(&cxgb3i_snic_rwlock);
  163. list_for_each_entry(snic, &cxgb3i_snic_list, list_head) {
  164. for (i = 0; i < snic->hba_cnt; i++) {
  165. if (snic->hba[i]->ndev == ndev) {
  166. read_unlock(&cxgb3i_snic_rwlock);
  167. return snic->hba[i];
  168. }
  169. }
  170. }
  171. read_unlock(&cxgb3i_snic_rwlock);
  172. return NULL;
  173. }
  174. /**
  175. * cxgb3i_hba_host_add - register a new host with scsi/iscsi
  176. * @snic: the cxgb3i adapter
  177. * @ndev: associated net_device
  178. */
  179. struct cxgb3i_hba *cxgb3i_hba_host_add(struct cxgb3i_adapter *snic,
  180. struct net_device *ndev)
  181. {
  182. struct cxgb3i_hba *hba;
  183. struct Scsi_Host *shost;
  184. int err;
  185. shost = iscsi_host_alloc(&cxgb3i_host_template,
  186. sizeof(struct cxgb3i_hba), 1);
  187. if (!shost) {
  188. cxgb3i_log_info("snic 0x%p, ndev 0x%p, host_alloc failed.\n",
  189. snic, ndev);
  190. return NULL;
  191. }
  192. shost->transportt = cxgb3i_scsi_transport;
  193. shost->max_lun = CXGB3I_MAX_LUN;
  194. shost->max_id = CXGB3I_MAX_TARGET;
  195. shost->max_channel = 0;
  196. shost->max_cmd_len = 16;
  197. hba = iscsi_host_priv(shost);
  198. hba->snic = snic;
  199. hba->ndev = ndev;
  200. hba->shost = shost;
  201. pci_dev_get(snic->pdev);
  202. err = iscsi_host_add(shost, &snic->pdev->dev);
  203. if (err) {
  204. cxgb3i_log_info("snic 0x%p, ndev 0x%p, host_add failed.\n",
  205. snic, ndev);
  206. goto pci_dev_put;
  207. }
  208. cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
  209. shost, hba, shost->host_no);
  210. return hba;
  211. pci_dev_put:
  212. pci_dev_put(snic->pdev);
  213. scsi_host_put(shost);
  214. return NULL;
  215. }
  216. /**
  217. * cxgb3i_hba_host_remove - de-register the host with scsi/iscsi
  218. * @hba: the cxgb3i hba
  219. */
  220. void cxgb3i_hba_host_remove(struct cxgb3i_hba *hba)
  221. {
  222. cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
  223. hba->shost, hba, hba->shost->host_no);
  224. iscsi_host_remove(hba->shost);
  225. pci_dev_put(hba->snic->pdev);
  226. iscsi_host_free(hba->shost);
  227. }
  228. /**
  229. * cxgb3i_ep_connect - establish TCP connection to target portal
  230. * @shost: scsi host to use
  231. * @dst_addr: target IP address
  232. * @non_blocking: blocking or non-blocking call
  233. *
  234. * Initiates a TCP/IP connection to the dst_addr
  235. */
  236. static struct iscsi_endpoint *cxgb3i_ep_connect(struct Scsi_Host *shost,
  237. struct sockaddr *dst_addr,
  238. int non_blocking)
  239. {
  240. struct iscsi_endpoint *ep;
  241. struct cxgb3i_endpoint *cep;
  242. struct cxgb3i_hba *hba = NULL;
  243. struct s3_conn *c3cn = NULL;
  244. int err = 0;
  245. if (shost)
  246. hba = iscsi_host_priv(shost);
  247. cxgb3i_api_debug("shost 0x%p, hba 0x%p.\n", shost, hba);
  248. c3cn = cxgb3i_c3cn_create();
  249. if (!c3cn) {
  250. cxgb3i_log_info("ep connect OOM.\n");
  251. err = -ENOMEM;
  252. goto release_conn;
  253. }
  254. err = cxgb3i_c3cn_connect(hba ? hba->ndev : NULL, c3cn,
  255. (struct sockaddr_in *)dst_addr);
  256. if (err < 0) {
  257. cxgb3i_log_info("ep connect failed.\n");
  258. goto release_conn;
  259. }
  260. hba = cxgb3i_hba_find_by_netdev(c3cn->dst_cache->dev);
  261. if (!hba) {
  262. err = -ENOSPC;
  263. cxgb3i_log_info("NOT going through cxgbi device.\n");
  264. goto release_conn;
  265. }
  266. if (shost && hba != iscsi_host_priv(shost)) {
  267. err = -ENOSPC;
  268. cxgb3i_log_info("Could not connect through request host%u\n",
  269. shost->host_no);
  270. goto release_conn;
  271. }
  272. if (c3cn_is_closing(c3cn)) {
  273. err = -ENOSPC;
  274. cxgb3i_log_info("ep connect unable to connect.\n");
  275. goto release_conn;
  276. }
  277. ep = iscsi_create_endpoint(sizeof(*cep));
  278. if (!ep) {
  279. err = -ENOMEM;
  280. cxgb3i_log_info("iscsi alloc ep, OOM.\n");
  281. goto release_conn;
  282. }
  283. cep = ep->dd_data;
  284. cep->c3cn = c3cn;
  285. cep->hba = hba;
  286. cxgb3i_api_debug("ep 0x%p, 0x%p, c3cn 0x%p, hba 0x%p.\n",
  287. ep, cep, c3cn, hba);
  288. return ep;
  289. release_conn:
  290. cxgb3i_api_debug("conn 0x%p failed, release.\n", c3cn);
  291. if (c3cn)
  292. cxgb3i_c3cn_release(c3cn);
  293. return ERR_PTR(err);
  294. }
  295. /**
  296. * cxgb3i_ep_poll - polls for TCP connection establishement
  297. * @ep: TCP connection (endpoint) handle
  298. * @timeout_ms: timeout value in milli secs
  299. *
  300. * polls for TCP connect request to complete
  301. */
  302. static int cxgb3i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  303. {
  304. struct cxgb3i_endpoint *cep = ep->dd_data;
  305. struct s3_conn *c3cn = cep->c3cn;
  306. if (!c3cn_is_established(c3cn))
  307. return 0;
  308. cxgb3i_api_debug("ep 0x%p, c3cn 0x%p established.\n", ep, c3cn);
  309. return 1;
  310. }
  311. /**
  312. * cxgb3i_ep_disconnect - teardown TCP connection
  313. * @ep: TCP connection (endpoint) handle
  314. *
  315. * teardown TCP connection
  316. */
  317. static void cxgb3i_ep_disconnect(struct iscsi_endpoint *ep)
  318. {
  319. struct cxgb3i_endpoint *cep = ep->dd_data;
  320. struct cxgb3i_conn *cconn = cep->cconn;
  321. cxgb3i_api_debug("ep 0x%p, cep 0x%p.\n", ep, cep);
  322. if (cconn && cconn->conn) {
  323. /*
  324. * stop the xmit path so the xmit_pdu function is
  325. * not being called
  326. */
  327. iscsi_suspend_tx(cconn->conn);
  328. write_lock_bh(&cep->c3cn->callback_lock);
  329. cep->c3cn->user_data = NULL;
  330. cconn->cep = NULL;
  331. write_unlock_bh(&cep->c3cn->callback_lock);
  332. }
  333. cxgb3i_api_debug("ep 0x%p, cep 0x%p, release c3cn 0x%p.\n",
  334. ep, cep, cep->c3cn);
  335. cxgb3i_c3cn_release(cep->c3cn);
  336. iscsi_destroy_endpoint(ep);
  337. }
  338. /**
  339. * cxgb3i_session_create - create a new iscsi session
  340. * @cmds_max: max # of commands
  341. * @qdepth: scsi queue depth
  342. * @initial_cmdsn: initial iscsi CMDSN for this session
  343. *
  344. * Creates a new iSCSI session
  345. */
  346. static struct iscsi_cls_session *
  347. cxgb3i_session_create(struct iscsi_endpoint *ep, u16 cmds_max, u16 qdepth,
  348. u32 initial_cmdsn)
  349. {
  350. struct cxgb3i_endpoint *cep;
  351. struct cxgb3i_hba *hba;
  352. struct Scsi_Host *shost;
  353. struct iscsi_cls_session *cls_session;
  354. struct iscsi_session *session;
  355. if (!ep) {
  356. cxgb3i_log_error("%s, missing endpoint.\n", __func__);
  357. return NULL;
  358. }
  359. cep = ep->dd_data;
  360. hba = cep->hba;
  361. shost = hba->shost;
  362. cxgb3i_api_debug("ep 0x%p, cep 0x%p, hba 0x%p.\n", ep, cep, hba);
  363. BUG_ON(hba != iscsi_host_priv(shost));
  364. cls_session = iscsi_session_setup(&cxgb3i_iscsi_transport, shost,
  365. cmds_max,
  366. sizeof(struct iscsi_tcp_task) +
  367. sizeof(struct cxgb3i_task_data),
  368. initial_cmdsn, ISCSI_MAX_TARGET);
  369. if (!cls_session)
  370. return NULL;
  371. session = cls_session->dd_data;
  372. if (iscsi_tcp_r2tpool_alloc(session))
  373. goto remove_session;
  374. return cls_session;
  375. remove_session:
  376. iscsi_session_teardown(cls_session);
  377. return NULL;
  378. }
  379. /**
  380. * cxgb3i_session_destroy - destroys iscsi session
  381. * @cls_session: pointer to iscsi cls session
  382. *
  383. * Destroys an iSCSI session instance and releases its all resources held
  384. */
  385. static void cxgb3i_session_destroy(struct iscsi_cls_session *cls_session)
  386. {
  387. cxgb3i_api_debug("sess 0x%p.\n", cls_session);
  388. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  389. iscsi_session_teardown(cls_session);
  390. }
  391. /**
  392. * cxgb3i_conn_max_xmit_dlength -- calc the max. xmit pdu segment size
  393. * @conn: iscsi connection
  394. * check the max. xmit pdu payload, reduce it if needed
  395. */
  396. static inline int cxgb3i_conn_max_xmit_dlength(struct iscsi_conn *conn)
  397. {
  398. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  399. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  400. unsigned int max = max(512 * MAX_SKB_FRAGS, SKB_TX_HEADROOM);
  401. max = min(cconn->hba->snic->tx_max_size, max);
  402. if (conn->max_xmit_dlength)
  403. conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
  404. else
  405. conn->max_xmit_dlength = max;
  406. align_pdu_size(conn->max_xmit_dlength);
  407. cxgb3i_api_debug("conn 0x%p, max xmit %u.\n",
  408. conn, conn->max_xmit_dlength);
  409. return 0;
  410. }
  411. /**
  412. * cxgb3i_conn_max_recv_dlength -- check the max. recv pdu segment size
  413. * @conn: iscsi connection
  414. * return 0 if the value is valid, < 0 otherwise.
  415. */
  416. static inline int cxgb3i_conn_max_recv_dlength(struct iscsi_conn *conn)
  417. {
  418. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  419. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  420. unsigned int max = cconn->hba->snic->rx_max_size;
  421. align_pdu_size(max);
  422. if (conn->max_recv_dlength) {
  423. if (conn->max_recv_dlength > max) {
  424. cxgb3i_log_error("MaxRecvDataSegmentLength %u too big."
  425. " Need to be <= %u.\n",
  426. conn->max_recv_dlength, max);
  427. return -EINVAL;
  428. }
  429. conn->max_recv_dlength = min(conn->max_recv_dlength, max);
  430. align_pdu_size(conn->max_recv_dlength);
  431. } else
  432. conn->max_recv_dlength = max;
  433. cxgb3i_api_debug("conn 0x%p, max recv %u.\n",
  434. conn, conn->max_recv_dlength);
  435. return 0;
  436. }
  437. /**
  438. * cxgb3i_conn_create - create iscsi connection instance
  439. * @cls_session: pointer to iscsi cls session
  440. * @cid: iscsi cid
  441. *
  442. * Creates a new iSCSI connection instance for a given session
  443. */
  444. static struct iscsi_cls_conn *cxgb3i_conn_create(struct iscsi_cls_session
  445. *cls_session, u32 cid)
  446. {
  447. struct iscsi_cls_conn *cls_conn;
  448. struct iscsi_conn *conn;
  449. struct iscsi_tcp_conn *tcp_conn;
  450. struct cxgb3i_conn *cconn;
  451. cxgb3i_api_debug("sess 0x%p, cid %u.\n", cls_session, cid);
  452. cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
  453. if (!cls_conn)
  454. return NULL;
  455. conn = cls_conn->dd_data;
  456. tcp_conn = conn->dd_data;
  457. cconn = tcp_conn->dd_data;
  458. cconn->conn = conn;
  459. return cls_conn;
  460. }
  461. /**
  462. * cxgb3i_conn_bind - binds iscsi sess, conn and endpoint together
  463. * @cls_session: pointer to iscsi cls session
  464. * @cls_conn: pointer to iscsi cls conn
  465. * @transport_eph: 64-bit EP handle
  466. * @is_leading: leading connection on this session?
  467. *
  468. * Binds together an iSCSI session, an iSCSI connection and a
  469. * TCP connection. This routine returns error code if the TCP
  470. * connection does not belong on the device iSCSI sess/conn is bound
  471. */
  472. static int cxgb3i_conn_bind(struct iscsi_cls_session *cls_session,
  473. struct iscsi_cls_conn *cls_conn,
  474. u64 transport_eph, int is_leading)
  475. {
  476. struct iscsi_conn *conn = cls_conn->dd_data;
  477. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  478. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  479. struct cxgb3i_adapter *snic;
  480. struct iscsi_endpoint *ep;
  481. struct cxgb3i_endpoint *cep;
  482. struct s3_conn *c3cn;
  483. int err;
  484. ep = iscsi_lookup_endpoint(transport_eph);
  485. if (!ep)
  486. return -EINVAL;
  487. /* setup ddp pagesize */
  488. cep = ep->dd_data;
  489. c3cn = cep->c3cn;
  490. snic = cep->hba->snic;
  491. err = cxgb3i_setup_conn_host_pagesize(snic->tdev, c3cn->tid, 0);
  492. if (err < 0)
  493. return err;
  494. cxgb3i_api_debug("ep 0x%p, cls sess 0x%p, cls conn 0x%p.\n",
  495. ep, cls_session, cls_conn);
  496. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  497. if (err)
  498. return -EINVAL;
  499. /* calculate the tag idx bits needed for this conn based on cmds_max */
  500. cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
  501. cxgb3i_api_debug("session cmds_max 0x%x, bits %u.\n",
  502. conn->session->cmds_max, cconn->task_idx_bits);
  503. read_lock(&c3cn->callback_lock);
  504. c3cn->user_data = conn;
  505. cconn->hba = cep->hba;
  506. cconn->cep = cep;
  507. cep->cconn = cconn;
  508. read_unlock(&c3cn->callback_lock);
  509. cxgb3i_conn_max_xmit_dlength(conn);
  510. cxgb3i_conn_max_recv_dlength(conn);
  511. spin_lock_bh(&conn->session->lock);
  512. sprintf(conn->portal_address, NIPQUAD_FMT,
  513. NIPQUAD(c3cn->daddr.sin_addr.s_addr));
  514. conn->portal_port = ntohs(c3cn->daddr.sin_port);
  515. spin_unlock_bh(&conn->session->lock);
  516. /* init recv engine */
  517. iscsi_tcp_hdr_recv_prep(tcp_conn);
  518. return 0;
  519. }
  520. /**
  521. * cxgb3i_conn_get_param - return iscsi connection parameter to caller
  522. * @cls_conn: pointer to iscsi cls conn
  523. * @param: parameter type identifier
  524. * @buf: buffer pointer
  525. *
  526. * returns iSCSI connection parameters
  527. */
  528. static int cxgb3i_conn_get_param(struct iscsi_cls_conn *cls_conn,
  529. enum iscsi_param param, char *buf)
  530. {
  531. struct iscsi_conn *conn = cls_conn->dd_data;
  532. int len;
  533. cxgb3i_api_debug("cls_conn 0x%p, param %d.\n", cls_conn, param);
  534. switch (param) {
  535. case ISCSI_PARAM_CONN_PORT:
  536. spin_lock_bh(&conn->session->lock);
  537. len = sprintf(buf, "%hu\n", conn->portal_port);
  538. spin_unlock_bh(&conn->session->lock);
  539. break;
  540. case ISCSI_PARAM_CONN_ADDRESS:
  541. spin_lock_bh(&conn->session->lock);
  542. len = sprintf(buf, "%s\n", conn->portal_address);
  543. spin_unlock_bh(&conn->session->lock);
  544. break;
  545. default:
  546. return iscsi_conn_get_param(cls_conn, param, buf);
  547. }
  548. return len;
  549. }
  550. /**
  551. * cxgb3i_conn_set_param - set iscsi connection parameter
  552. * @cls_conn: pointer to iscsi cls conn
  553. * @param: parameter type identifier
  554. * @buf: buffer pointer
  555. * @buflen: buffer length
  556. *
  557. * set iSCSI connection parameters
  558. */
  559. static int cxgb3i_conn_set_param(struct iscsi_cls_conn *cls_conn,
  560. enum iscsi_param param, char *buf, int buflen)
  561. {
  562. struct iscsi_conn *conn = cls_conn->dd_data;
  563. struct iscsi_session *session = conn->session;
  564. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  565. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  566. struct cxgb3i_adapter *snic = cconn->hba->snic;
  567. struct s3_conn *c3cn = cconn->cep->c3cn;
  568. int value, err = 0;
  569. switch (param) {
  570. case ISCSI_PARAM_HDRDGST_EN:
  571. err = iscsi_set_param(cls_conn, param, buf, buflen);
  572. if (!err && conn->hdrdgst_en)
  573. err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
  574. conn->hdrdgst_en,
  575. conn->datadgst_en, 0);
  576. break;
  577. case ISCSI_PARAM_DATADGST_EN:
  578. err = iscsi_set_param(cls_conn, param, buf, buflen);
  579. if (!err && conn->datadgst_en)
  580. err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
  581. conn->hdrdgst_en,
  582. conn->datadgst_en, 0);
  583. break;
  584. case ISCSI_PARAM_MAX_R2T:
  585. sscanf(buf, "%d", &value);
  586. if (value <= 0 || !is_power_of_2(value))
  587. return -EINVAL;
  588. if (session->max_r2t == value)
  589. break;
  590. iscsi_tcp_r2tpool_free(session);
  591. err = iscsi_set_param(cls_conn, param, buf, buflen);
  592. if (!err && iscsi_tcp_r2tpool_alloc(session))
  593. return -ENOMEM;
  594. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  595. err = iscsi_set_param(cls_conn, param, buf, buflen);
  596. if (!err)
  597. err = cxgb3i_conn_max_recv_dlength(conn);
  598. break;
  599. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  600. err = iscsi_set_param(cls_conn, param, buf, buflen);
  601. if (!err)
  602. err = cxgb3i_conn_max_xmit_dlength(conn);
  603. break;
  604. default:
  605. return iscsi_set_param(cls_conn, param, buf, buflen);
  606. }
  607. return err;
  608. }
  609. /**
  610. * cxgb3i_host_set_param - configure host (adapter) related parameters
  611. * @shost: scsi host pointer
  612. * @param: parameter type identifier
  613. * @buf: buffer pointer
  614. */
  615. static int cxgb3i_host_set_param(struct Scsi_Host *shost,
  616. enum iscsi_host_param param,
  617. char *buf, int buflen)
  618. {
  619. struct cxgb3i_hba *hba = iscsi_host_priv(shost);
  620. cxgb3i_api_debug("param %d, buf %s.\n", param, buf);
  621. switch (param) {
  622. case ISCSI_HOST_PARAM_IPADDRESS:
  623. {
  624. __be32 addr = in_aton(buf);
  625. cxgb3i_set_private_ipv4addr(hba->ndev, addr);
  626. return 0;
  627. }
  628. case ISCSI_HOST_PARAM_HWADDRESS:
  629. case ISCSI_HOST_PARAM_NETDEV_NAME:
  630. /* ignore */
  631. return 0;
  632. default:
  633. return iscsi_host_set_param(shost, param, buf, buflen);
  634. }
  635. }
  636. /**
  637. * cxgb3i_host_get_param - returns host (adapter) related parameters
  638. * @shost: scsi host pointer
  639. * @param: parameter type identifier
  640. * @buf: buffer pointer
  641. */
  642. static int cxgb3i_host_get_param(struct Scsi_Host *shost,
  643. enum iscsi_host_param param, char *buf)
  644. {
  645. struct cxgb3i_hba *hba = iscsi_host_priv(shost);
  646. int len = 0;
  647. cxgb3i_api_debug("hba %s, param %d.\n", hba->ndev->name, param);
  648. switch (param) {
  649. case ISCSI_HOST_PARAM_HWADDRESS:
  650. len = sysfs_format_mac(buf, hba->ndev->dev_addr, 6);
  651. break;
  652. case ISCSI_HOST_PARAM_NETDEV_NAME:
  653. len = sprintf(buf, "%s\n", hba->ndev->name);
  654. break;
  655. case ISCSI_HOST_PARAM_IPADDRESS:
  656. {
  657. __be32 addr;
  658. addr = cxgb3i_get_private_ipv4addr(hba->ndev);
  659. len = sprintf(buf, NIPQUAD_FMT, NIPQUAD(addr));
  660. break;
  661. }
  662. default:
  663. return iscsi_host_get_param(shost, param, buf);
  664. }
  665. return len;
  666. }
  667. /**
  668. * cxgb3i_conn_get_stats - returns iSCSI stats
  669. * @cls_conn: pointer to iscsi cls conn
  670. * @stats: pointer to iscsi statistic struct
  671. */
  672. static void cxgb3i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  673. struct iscsi_stats *stats)
  674. {
  675. struct iscsi_conn *conn = cls_conn->dd_data;
  676. stats->txdata_octets = conn->txdata_octets;
  677. stats->rxdata_octets = conn->rxdata_octets;
  678. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  679. stats->dataout_pdus = conn->dataout_pdus_cnt;
  680. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  681. stats->datain_pdus = conn->datain_pdus_cnt;
  682. stats->r2t_pdus = conn->r2t_pdus_cnt;
  683. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  684. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  685. stats->digest_err = 0;
  686. stats->timeout_err = 0;
  687. stats->custom_length = 1;
  688. strcpy(stats->custom[0].desc, "eh_abort_cnt");
  689. stats->custom[0].value = conn->eh_abort_cnt;
  690. }
  691. /**
  692. * cxgb3i_parse_itt - get the idx and age bits from a given tag
  693. * @conn: iscsi connection
  694. * @itt: itt tag
  695. * @idx: task index, filled in by this function
  696. * @age: session age, filled in by this function
  697. */
  698. static void cxgb3i_parse_itt(struct iscsi_conn *conn, itt_t itt,
  699. int *idx, int *age)
  700. {
  701. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  702. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  703. struct cxgb3i_adapter *snic = cconn->hba->snic;
  704. u32 tag = ntohl((__force u32) itt);
  705. u32 sw_bits;
  706. sw_bits = cxgb3i_tag_nonrsvd_bits(&snic->tag_format, tag);
  707. if (idx)
  708. *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
  709. if (age)
  710. *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
  711. cxgb3i_tag_debug("parse tag 0x%x/0x%x, sw 0x%x, itt 0x%x, age 0x%x.\n",
  712. tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
  713. age ? *age : 0xFF);
  714. }
  715. /**
  716. * cxgb3i_reserve_itt - generate tag for a give task
  717. * @task: iscsi task
  718. * @hdr_itt: tag, filled in by this function
  719. * Set up ddp for scsi read tasks if possible.
  720. */
  721. int cxgb3i_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
  722. {
  723. struct scsi_cmnd *sc = task->sc;
  724. struct iscsi_conn *conn = task->conn;
  725. struct iscsi_session *sess = conn->session;
  726. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  727. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  728. struct cxgb3i_adapter *snic = cconn->hba->snic;
  729. struct cxgb3i_tag_format *tformat = &snic->tag_format;
  730. u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
  731. u32 tag;
  732. int err = -EINVAL;
  733. if (sc &&
  734. (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
  735. cxgb3i_sw_tag_usable(tformat, sw_tag)) {
  736. struct s3_conn *c3cn = cconn->cep->c3cn;
  737. struct cxgb3i_gather_list *gl;
  738. gl = cxgb3i_ddp_make_gl(scsi_in(sc)->length,
  739. scsi_in(sc)->table.sgl,
  740. scsi_in(sc)->table.nents,
  741. snic->pdev,
  742. GFP_ATOMIC);
  743. if (gl) {
  744. tag = sw_tag;
  745. err = cxgb3i_ddp_tag_reserve(snic->tdev, c3cn->tid,
  746. tformat, &tag,
  747. gl, GFP_ATOMIC);
  748. if (err < 0)
  749. cxgb3i_ddp_release_gl(gl, snic->pdev);
  750. }
  751. }
  752. if (err < 0)
  753. tag = cxgb3i_set_non_ddp_tag(tformat, sw_tag);
  754. /* the itt need to sent in big-endian order */
  755. *hdr_itt = (__force itt_t)htonl(tag);
  756. cxgb3i_tag_debug("new tag 0x%x/0x%x (itt 0x%x, age 0x%x).\n",
  757. tag, *hdr_itt, task->itt, sess->age);
  758. return 0;
  759. }
  760. /**
  761. * cxgb3i_release_itt - release the tag for a given task
  762. * @task: iscsi task
  763. * @hdr_itt: tag
  764. * If the tag is a ddp tag, release the ddp setup
  765. */
  766. void cxgb3i_release_itt(struct iscsi_task *task, itt_t hdr_itt)
  767. {
  768. struct scsi_cmnd *sc = task->sc;
  769. struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
  770. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  771. struct cxgb3i_adapter *snic = cconn->hba->snic;
  772. struct cxgb3i_tag_format *tformat = &snic->tag_format;
  773. u32 tag = ntohl((__force u32)hdr_itt);
  774. cxgb3i_tag_debug("release tag 0x%x.\n", tag);
  775. if (sc &&
  776. (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
  777. cxgb3i_is_ddp_tag(tformat, tag))
  778. cxgb3i_ddp_tag_release(snic->tdev, tag);
  779. }
  780. /**
  781. * cxgb3i_host_template -- Scsi_Host_Template structure
  782. * used when registering with the scsi mid layer
  783. */
  784. static struct scsi_host_template cxgb3i_host_template = {
  785. .module = THIS_MODULE,
  786. .name = "Chelsio S3xx iSCSI Initiator",
  787. .proc_name = "cxgb3i",
  788. .queuecommand = iscsi_queuecommand,
  789. .change_queue_depth = iscsi_change_queue_depth,
  790. .can_queue = CXGB3I_SCSI_HOST_QDEPTH,
  791. .sg_tablesize = SG_ALL,
  792. .max_sectors = 0xFFFF,
  793. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  794. .eh_abort_handler = iscsi_eh_abort,
  795. .eh_device_reset_handler = iscsi_eh_device_reset,
  796. .eh_target_reset_handler = iscsi_eh_target_reset,
  797. .target_alloc = iscsi_target_alloc,
  798. .use_clustering = DISABLE_CLUSTERING,
  799. .this_id = -1,
  800. };
  801. static struct iscsi_transport cxgb3i_iscsi_transport = {
  802. .owner = THIS_MODULE,
  803. .name = "cxgb3i",
  804. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  805. | CAP_DATADGST | CAP_DIGEST_OFFLOAD |
  806. CAP_PADDING_OFFLOAD,
  807. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  808. ISCSI_MAX_XMIT_DLENGTH |
  809. ISCSI_HDRDGST_EN |
  810. ISCSI_DATADGST_EN |
  811. ISCSI_INITIAL_R2T_EN |
  812. ISCSI_MAX_R2T |
  813. ISCSI_IMM_DATA_EN |
  814. ISCSI_FIRST_BURST |
  815. ISCSI_MAX_BURST |
  816. ISCSI_PDU_INORDER_EN |
  817. ISCSI_DATASEQ_INORDER_EN |
  818. ISCSI_ERL |
  819. ISCSI_CONN_PORT |
  820. ISCSI_CONN_ADDRESS |
  821. ISCSI_EXP_STATSN |
  822. ISCSI_PERSISTENT_PORT |
  823. ISCSI_PERSISTENT_ADDRESS |
  824. ISCSI_TARGET_NAME | ISCSI_TPGT |
  825. ISCSI_USERNAME | ISCSI_PASSWORD |
  826. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  827. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  828. ISCSI_LU_RESET_TMO |
  829. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  830. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  831. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  832. ISCSI_HOST_INITIATOR_NAME | ISCSI_HOST_NETDEV_NAME,
  833. .get_host_param = cxgb3i_host_get_param,
  834. .set_host_param = cxgb3i_host_set_param,
  835. /* session management */
  836. .create_session = cxgb3i_session_create,
  837. .destroy_session = cxgb3i_session_destroy,
  838. .get_session_param = iscsi_session_get_param,
  839. /* connection management */
  840. .create_conn = cxgb3i_conn_create,
  841. .bind_conn = cxgb3i_conn_bind,
  842. .destroy_conn = iscsi_tcp_conn_teardown,
  843. .start_conn = iscsi_conn_start,
  844. .stop_conn = iscsi_conn_stop,
  845. .get_conn_param = cxgb3i_conn_get_param,
  846. .set_param = cxgb3i_conn_set_param,
  847. .get_stats = cxgb3i_conn_get_stats,
  848. /* pdu xmit req. from user space */
  849. .send_pdu = iscsi_conn_send_pdu,
  850. /* task */
  851. .init_task = iscsi_tcp_task_init,
  852. .xmit_task = iscsi_tcp_task_xmit,
  853. .cleanup_task = cxgb3i_conn_cleanup_task,
  854. /* pdu */
  855. .alloc_pdu = cxgb3i_conn_alloc_pdu,
  856. .init_pdu = cxgb3i_conn_init_pdu,
  857. .xmit_pdu = cxgb3i_conn_xmit_pdu,
  858. .parse_pdu_itt = cxgb3i_parse_itt,
  859. /* TCP connect/disconnect */
  860. .ep_connect = cxgb3i_ep_connect,
  861. .ep_poll = cxgb3i_ep_poll,
  862. .ep_disconnect = cxgb3i_ep_disconnect,
  863. /* Error recovery timeout call */
  864. .session_recovery_timedout = iscsi_session_recovery_timedout,
  865. };
  866. int cxgb3i_iscsi_init(void)
  867. {
  868. sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
  869. sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
  870. cxgb3i_log_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
  871. ISCSI_ITT_MASK, sw_tag_idx_bits,
  872. ISCSI_AGE_MASK, sw_tag_age_bits);
  873. cxgb3i_scsi_transport =
  874. iscsi_register_transport(&cxgb3i_iscsi_transport);
  875. if (!cxgb3i_scsi_transport) {
  876. cxgb3i_log_error("Could not register cxgb3i transport.\n");
  877. return -ENODEV;
  878. }
  879. cxgb3i_api_debug("cxgb3i transport 0x%p.\n", cxgb3i_scsi_transport);
  880. return 0;
  881. }
  882. void cxgb3i_iscsi_cleanup(void)
  883. {
  884. if (cxgb3i_scsi_transport) {
  885. cxgb3i_api_debug("cxgb3i transport 0x%p.\n",
  886. cxgb3i_scsi_transport);
  887. iscsi_unregister_transport(&cxgb3i_iscsi_transport);
  888. }
  889. }