cxgb3i_iscsi.c 25 KB

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