cxgb3i_iscsi.c 27 KB

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