cxgb3i_iscsi.c 26 KB

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