cxgb3i_iscsi.c 25 KB

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