cxgb3i_iscsi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. *
  294. * Creates a new iSCSI session
  295. */
  296. static struct iscsi_cls_session *
  297. cxgb3i_session_create(struct iscsi_endpoint *ep, u16 cmds_max, u16 qdepth,
  298. u32 initial_cmdsn)
  299. {
  300. struct cxgb3i_endpoint *cep;
  301. struct cxgb3i_hba *hba;
  302. struct Scsi_Host *shost;
  303. struct iscsi_cls_session *cls_session;
  304. struct iscsi_session *session;
  305. if (!ep) {
  306. cxgb3i_log_error("%s, missing endpoint.\n", __func__);
  307. return NULL;
  308. }
  309. cep = ep->dd_data;
  310. hba = cep->hba;
  311. shost = hba->shost;
  312. cxgb3i_api_debug("ep 0x%p, cep 0x%p, hba 0x%p.\n", ep, cep, hba);
  313. BUG_ON(hba != iscsi_host_priv(shost));
  314. cls_session = iscsi_session_setup(&cxgb3i_iscsi_transport, shost,
  315. cmds_max,
  316. sizeof(struct iscsi_tcp_task) +
  317. sizeof(struct cxgb3i_task_data),
  318. initial_cmdsn, ISCSI_MAX_TARGET);
  319. if (!cls_session)
  320. return NULL;
  321. session = cls_session->dd_data;
  322. if (iscsi_tcp_r2tpool_alloc(session))
  323. goto remove_session;
  324. return cls_session;
  325. remove_session:
  326. iscsi_session_teardown(cls_session);
  327. return NULL;
  328. }
  329. /**
  330. * cxgb3i_session_destroy - destroys iscsi session
  331. * @cls_session: pointer to iscsi cls session
  332. *
  333. * Destroys an iSCSI session instance and releases its all resources held
  334. */
  335. static void cxgb3i_session_destroy(struct iscsi_cls_session *cls_session)
  336. {
  337. cxgb3i_api_debug("sess 0x%p.\n", cls_session);
  338. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  339. iscsi_session_teardown(cls_session);
  340. }
  341. /**
  342. * cxgb3i_conn_max_xmit_dlength -- check the max. xmit pdu segment size,
  343. * reduce it to be within the hardware limit if needed
  344. * @conn: iscsi connection
  345. */
  346. static inline int cxgb3i_conn_max_xmit_dlength(struct iscsi_conn *conn)
  347. {
  348. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  349. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  350. unsigned int max = max(512 * MAX_SKB_FRAGS, SKB_TX_HEADROOM);
  351. max = min(cconn->hba->snic->tx_max_size, max);
  352. if (conn->max_xmit_dlength)
  353. conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
  354. else
  355. conn->max_xmit_dlength = max;
  356. align_pdu_size(conn->max_xmit_dlength);
  357. cxgb3i_api_debug("conn 0x%p, max xmit %u.\n",
  358. conn, conn->max_xmit_dlength);
  359. return 0;
  360. }
  361. /**
  362. * cxgb3i_conn_max_recv_dlength -- check the max. recv pdu segment size against
  363. * the hardware limit
  364. * @conn: iscsi connection
  365. * return 0 if the value is valid, < 0 otherwise.
  366. */
  367. static inline int cxgb3i_conn_max_recv_dlength(struct iscsi_conn *conn)
  368. {
  369. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  370. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  371. unsigned int max = cconn->hba->snic->rx_max_size;
  372. align_pdu_size(max);
  373. if (conn->max_recv_dlength) {
  374. if (conn->max_recv_dlength > max) {
  375. cxgb3i_log_error("MaxRecvDataSegmentLength %u too big."
  376. " Need to be <= %u.\n",
  377. conn->max_recv_dlength, max);
  378. return -EINVAL;
  379. }
  380. conn->max_recv_dlength = min(conn->max_recv_dlength, max);
  381. align_pdu_size(conn->max_recv_dlength);
  382. } else
  383. conn->max_recv_dlength = max;
  384. cxgb3i_api_debug("conn 0x%p, max recv %u.\n",
  385. conn, conn->max_recv_dlength);
  386. return 0;
  387. }
  388. /**
  389. * cxgb3i_conn_create - create iscsi connection instance
  390. * @cls_session: pointer to iscsi cls session
  391. * @cid: iscsi cid
  392. *
  393. * Creates a new iSCSI connection instance for a given session
  394. */
  395. static struct iscsi_cls_conn *cxgb3i_conn_create(struct iscsi_cls_session
  396. *cls_session, u32 cid)
  397. {
  398. struct iscsi_cls_conn *cls_conn;
  399. struct iscsi_conn *conn;
  400. struct iscsi_tcp_conn *tcp_conn;
  401. struct cxgb3i_conn *cconn;
  402. cxgb3i_api_debug("sess 0x%p, cid %u.\n", cls_session, cid);
  403. cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
  404. if (!cls_conn)
  405. return NULL;
  406. conn = cls_conn->dd_data;
  407. tcp_conn = conn->dd_data;
  408. cconn = tcp_conn->dd_data;
  409. cconn->conn = conn;
  410. return cls_conn;
  411. }
  412. /**
  413. * cxgb3i_conn_bind - binds iscsi sess, conn and endpoint together
  414. * @cls_session: pointer to iscsi cls session
  415. * @cls_conn: pointer to iscsi cls conn
  416. * @transport_eph: 64-bit EP handle
  417. * @is_leading: leading connection on this session?
  418. *
  419. * Binds together an iSCSI session, an iSCSI connection and a
  420. * TCP connection. This routine returns error code if the TCP
  421. * connection does not belong on the device iSCSI sess/conn is bound
  422. */
  423. static int cxgb3i_conn_bind(struct iscsi_cls_session *cls_session,
  424. struct iscsi_cls_conn *cls_conn,
  425. u64 transport_eph, int is_leading)
  426. {
  427. struct iscsi_conn *conn = cls_conn->dd_data;
  428. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  429. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  430. struct cxgb3i_adapter *snic;
  431. struct iscsi_endpoint *ep;
  432. struct cxgb3i_endpoint *cep;
  433. struct s3_conn *c3cn;
  434. int err;
  435. ep = iscsi_lookup_endpoint(transport_eph);
  436. if (!ep)
  437. return -EINVAL;
  438. /* setup ddp pagesize */
  439. cep = ep->dd_data;
  440. c3cn = cep->c3cn;
  441. snic = cep->hba->snic;
  442. err = cxgb3i_setup_conn_host_pagesize(snic->tdev, c3cn->tid, 0);
  443. if (err < 0)
  444. return err;
  445. cxgb3i_api_debug("ep 0x%p, cls sess 0x%p, cls conn 0x%p.\n",
  446. ep, cls_session, cls_conn);
  447. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  448. if (err)
  449. return -EINVAL;
  450. /* calculate the tag idx bits needed for this conn based on cmds_max */
  451. cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
  452. cxgb3i_api_debug("session cmds_max 0x%x, bits %u.\n",
  453. conn->session->cmds_max, cconn->task_idx_bits);
  454. read_lock(&c3cn->callback_lock);
  455. c3cn->user_data = conn;
  456. cconn->hba = cep->hba;
  457. cconn->cep = cep;
  458. cep->cconn = cconn;
  459. read_unlock(&c3cn->callback_lock);
  460. cxgb3i_conn_max_xmit_dlength(conn);
  461. cxgb3i_conn_max_recv_dlength(conn);
  462. spin_lock_bh(&conn->session->lock);
  463. sprintf(conn->portal_address, NIPQUAD_FMT,
  464. NIPQUAD(c3cn->daddr.sin_addr.s_addr));
  465. conn->portal_port = ntohs(c3cn->daddr.sin_port);
  466. spin_unlock_bh(&conn->session->lock);
  467. /* init recv engine */
  468. iscsi_tcp_hdr_recv_prep(tcp_conn);
  469. return 0;
  470. }
  471. /**
  472. * cxgb3i_conn_get_param - return iscsi connection parameter to caller
  473. * @cls_conn: pointer to iscsi cls conn
  474. * @param: parameter type identifier
  475. * @buf: buffer pointer
  476. *
  477. * returns iSCSI connection parameters
  478. */
  479. static int cxgb3i_conn_get_param(struct iscsi_cls_conn *cls_conn,
  480. enum iscsi_param param, char *buf)
  481. {
  482. struct iscsi_conn *conn = cls_conn->dd_data;
  483. int len;
  484. cxgb3i_api_debug("cls_conn 0x%p, param %d.\n", cls_conn, param);
  485. switch (param) {
  486. case ISCSI_PARAM_CONN_PORT:
  487. spin_lock_bh(&conn->session->lock);
  488. len = sprintf(buf, "%hu\n", conn->portal_port);
  489. spin_unlock_bh(&conn->session->lock);
  490. break;
  491. case ISCSI_PARAM_CONN_ADDRESS:
  492. spin_lock_bh(&conn->session->lock);
  493. len = sprintf(buf, "%s\n", conn->portal_address);
  494. spin_unlock_bh(&conn->session->lock);
  495. break;
  496. default:
  497. return iscsi_conn_get_param(cls_conn, param, buf);
  498. }
  499. return len;
  500. }
  501. /**
  502. * cxgb3i_conn_set_param - set iscsi connection parameter
  503. * @cls_conn: pointer to iscsi cls conn
  504. * @param: parameter type identifier
  505. * @buf: buffer pointer
  506. * @buflen: buffer length
  507. *
  508. * set iSCSI connection parameters
  509. */
  510. static int cxgb3i_conn_set_param(struct iscsi_cls_conn *cls_conn,
  511. enum iscsi_param param, char *buf, int buflen)
  512. {
  513. struct iscsi_conn *conn = cls_conn->dd_data;
  514. struct iscsi_session *session = conn->session;
  515. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  516. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  517. struct cxgb3i_adapter *snic = cconn->hba->snic;
  518. struct s3_conn *c3cn = cconn->cep->c3cn;
  519. int value, err = 0;
  520. switch (param) {
  521. case ISCSI_PARAM_HDRDGST_EN:
  522. err = iscsi_set_param(cls_conn, param, buf, buflen);
  523. if (!err && conn->hdrdgst_en)
  524. err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
  525. conn->hdrdgst_en,
  526. conn->datadgst_en, 0);
  527. break;
  528. case ISCSI_PARAM_DATADGST_EN:
  529. err = iscsi_set_param(cls_conn, param, buf, buflen);
  530. if (!err && conn->datadgst_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_MAX_R2T:
  536. sscanf(buf, "%d", &value);
  537. if (value <= 0 || !is_power_of_2(value))
  538. return -EINVAL;
  539. if (session->max_r2t == value)
  540. break;
  541. iscsi_tcp_r2tpool_free(session);
  542. err = iscsi_set_param(cls_conn, param, buf, buflen);
  543. if (!err && iscsi_tcp_r2tpool_alloc(session))
  544. return -ENOMEM;
  545. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  546. err = iscsi_set_param(cls_conn, param, buf, buflen);
  547. if (!err)
  548. err = cxgb3i_conn_max_recv_dlength(conn);
  549. break;
  550. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  551. err = iscsi_set_param(cls_conn, param, buf, buflen);
  552. if (!err)
  553. err = cxgb3i_conn_max_xmit_dlength(conn);
  554. break;
  555. default:
  556. return iscsi_set_param(cls_conn, param, buf, buflen);
  557. }
  558. return err;
  559. }
  560. /**
  561. * cxgb3i_host_set_param - configure host (adapter) related parameters
  562. * @shost: scsi host pointer
  563. * @param: parameter type identifier
  564. * @buf: buffer pointer
  565. */
  566. static int cxgb3i_host_set_param(struct Scsi_Host *shost,
  567. enum iscsi_host_param param,
  568. char *buf, int buflen)
  569. {
  570. struct cxgb3i_hba *hba = iscsi_host_priv(shost);
  571. cxgb3i_api_debug("param %d, buf %s.\n", param, buf);
  572. switch (param) {
  573. case ISCSI_HOST_PARAM_IPADDRESS:
  574. {
  575. __be32 addr = in_aton(buf);
  576. cxgb3i_set_private_ipv4addr(hba->ndev, addr);
  577. return 0;
  578. }
  579. case ISCSI_HOST_PARAM_HWADDRESS:
  580. case ISCSI_HOST_PARAM_NETDEV_NAME:
  581. /* ignore */
  582. return 0;
  583. default:
  584. return iscsi_host_set_param(shost, param, buf, buflen);
  585. }
  586. }
  587. /**
  588. * cxgb3i_host_get_param - returns host (adapter) related parameters
  589. * @shost: scsi host pointer
  590. * @param: parameter type identifier
  591. * @buf: buffer pointer
  592. */
  593. static int cxgb3i_host_get_param(struct Scsi_Host *shost,
  594. enum iscsi_host_param param, char *buf)
  595. {
  596. struct cxgb3i_hba *hba = iscsi_host_priv(shost);
  597. int len = 0;
  598. cxgb3i_api_debug("hba %s, param %d.\n", hba->ndev->name, param);
  599. switch (param) {
  600. case ISCSI_HOST_PARAM_HWADDRESS:
  601. len = sysfs_format_mac(buf, hba->ndev->dev_addr, 6);
  602. break;
  603. case ISCSI_HOST_PARAM_NETDEV_NAME:
  604. len = sprintf(buf, "%s\n", hba->ndev->name);
  605. break;
  606. case ISCSI_HOST_PARAM_IPADDRESS:
  607. {
  608. __be32 addr;
  609. addr = cxgb3i_get_private_ipv4addr(hba->ndev);
  610. len = sprintf(buf, NIPQUAD_FMT, NIPQUAD(addr));
  611. break;
  612. }
  613. default:
  614. return iscsi_host_get_param(shost, param, buf);
  615. }
  616. return len;
  617. }
  618. /**
  619. * cxgb3i_conn_get_stats - returns iSCSI stats
  620. * @cls_conn: pointer to iscsi cls conn
  621. * @stats: pointer to iscsi statistic struct
  622. */
  623. static void cxgb3i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  624. struct iscsi_stats *stats)
  625. {
  626. struct iscsi_conn *conn = cls_conn->dd_data;
  627. stats->txdata_octets = conn->txdata_octets;
  628. stats->rxdata_octets = conn->rxdata_octets;
  629. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  630. stats->dataout_pdus = conn->dataout_pdus_cnt;
  631. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  632. stats->datain_pdus = conn->datain_pdus_cnt;
  633. stats->r2t_pdus = conn->r2t_pdus_cnt;
  634. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  635. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  636. stats->digest_err = 0;
  637. stats->timeout_err = 0;
  638. stats->custom_length = 1;
  639. strcpy(stats->custom[0].desc, "eh_abort_cnt");
  640. stats->custom[0].value = conn->eh_abort_cnt;
  641. }
  642. /**
  643. * cxgb3i_parse_itt - get the idx and age bits from a given tag
  644. * @conn: iscsi connection
  645. * @itt: itt tag
  646. * @idx: task index, filled in by this function
  647. * @age: session age, filled in by this function
  648. */
  649. static void cxgb3i_parse_itt(struct iscsi_conn *conn, itt_t itt,
  650. int *idx, int *age)
  651. {
  652. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  653. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  654. struct cxgb3i_adapter *snic = cconn->hba->snic;
  655. u32 tag = ntohl((__force u32) itt);
  656. u32 sw_bits;
  657. sw_bits = cxgb3i_tag_nonrsvd_bits(&snic->tag_format, tag);
  658. if (idx)
  659. *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
  660. if (age)
  661. *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
  662. cxgb3i_tag_debug("parse tag 0x%x/0x%x, sw 0x%x, itt 0x%x, age 0x%x.\n",
  663. tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
  664. age ? *age : 0xFF);
  665. }
  666. /**
  667. * cxgb3i_reserve_itt - generate tag for a give task
  668. * Try to set up ddp for a scsi read task.
  669. * @task: iscsi task
  670. * @hdr_itt: tag, filled in by this function
  671. */
  672. int cxgb3i_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
  673. {
  674. struct scsi_cmnd *sc = task->sc;
  675. struct iscsi_conn *conn = task->conn;
  676. struct iscsi_session *sess = conn->session;
  677. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  678. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  679. struct cxgb3i_adapter *snic = cconn->hba->snic;
  680. struct cxgb3i_tag_format *tformat = &snic->tag_format;
  681. u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
  682. u32 tag;
  683. int err = -EINVAL;
  684. if (sc &&
  685. (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
  686. cxgb3i_sw_tag_usable(tformat, sw_tag)) {
  687. struct s3_conn *c3cn = cconn->cep->c3cn;
  688. struct cxgb3i_gather_list *gl;
  689. gl = cxgb3i_ddp_make_gl(scsi_in(sc)->length,
  690. scsi_in(sc)->table.sgl,
  691. scsi_in(sc)->table.nents,
  692. snic->pdev,
  693. GFP_ATOMIC);
  694. if (gl) {
  695. tag = sw_tag;
  696. err = cxgb3i_ddp_tag_reserve(snic->tdev, c3cn->tid,
  697. tformat, &tag,
  698. gl, GFP_ATOMIC);
  699. if (err < 0)
  700. cxgb3i_ddp_release_gl(gl, snic->pdev);
  701. }
  702. }
  703. if (err < 0)
  704. tag = cxgb3i_set_non_ddp_tag(tformat, sw_tag);
  705. /* the itt need to sent in big-endian order */
  706. *hdr_itt = (__force itt_t)htonl(tag);
  707. cxgb3i_tag_debug("new tag 0x%x/0x%x (itt 0x%x, age 0x%x).\n",
  708. tag, *hdr_itt, task->itt, sess->age);
  709. return 0;
  710. }
  711. /**
  712. * cxgb3i_release_itt - release the tag for a given task
  713. * if the tag is a ddp tag, release the ddp setup
  714. * @task: iscsi task
  715. * @hdr_itt: tag
  716. */
  717. void cxgb3i_release_itt(struct iscsi_task *task, itt_t hdr_itt)
  718. {
  719. struct scsi_cmnd *sc = task->sc;
  720. struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
  721. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  722. struct cxgb3i_adapter *snic = cconn->hba->snic;
  723. struct cxgb3i_tag_format *tformat = &snic->tag_format;
  724. u32 tag = ntohl((__force u32)hdr_itt);
  725. cxgb3i_tag_debug("release tag 0x%x.\n", tag);
  726. if (sc &&
  727. (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
  728. cxgb3i_is_ddp_tag(tformat, tag))
  729. cxgb3i_ddp_tag_release(snic->tdev, tag);
  730. }
  731. /**
  732. * cxgb3i_host_template -- Scsi_Host_Template structure
  733. * used when registering with the scsi mid layer
  734. */
  735. static struct scsi_host_template cxgb3i_host_template = {
  736. .module = THIS_MODULE,
  737. .name = "Chelsio S3xx iSCSI Initiator",
  738. .proc_name = "cxgb3i",
  739. .queuecommand = iscsi_queuecommand,
  740. .change_queue_depth = iscsi_change_queue_depth,
  741. .can_queue = CXGB3I_SCSI_QDEPTH_DFLT - 1,
  742. .sg_tablesize = SG_ALL,
  743. .max_sectors = 0xFFFF,
  744. .cmd_per_lun = CXGB3I_SCSI_QDEPTH_DFLT,
  745. .eh_abort_handler = iscsi_eh_abort,
  746. .eh_device_reset_handler = iscsi_eh_device_reset,
  747. .eh_target_reset_handler = iscsi_eh_target_reset,
  748. .use_clustering = DISABLE_CLUSTERING,
  749. .this_id = -1,
  750. };
  751. static struct iscsi_transport cxgb3i_iscsi_transport = {
  752. .owner = THIS_MODULE,
  753. .name = "cxgb3i",
  754. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  755. | CAP_DATADGST | CAP_DIGEST_OFFLOAD |
  756. CAP_PADDING_OFFLOAD,
  757. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  758. ISCSI_MAX_XMIT_DLENGTH |
  759. ISCSI_HDRDGST_EN |
  760. ISCSI_DATADGST_EN |
  761. ISCSI_INITIAL_R2T_EN |
  762. ISCSI_MAX_R2T |
  763. ISCSI_IMM_DATA_EN |
  764. ISCSI_FIRST_BURST |
  765. ISCSI_MAX_BURST |
  766. ISCSI_PDU_INORDER_EN |
  767. ISCSI_DATASEQ_INORDER_EN |
  768. ISCSI_ERL |
  769. ISCSI_CONN_PORT |
  770. ISCSI_CONN_ADDRESS |
  771. ISCSI_EXP_STATSN |
  772. ISCSI_PERSISTENT_PORT |
  773. ISCSI_PERSISTENT_ADDRESS |
  774. ISCSI_TARGET_NAME | ISCSI_TPGT |
  775. ISCSI_USERNAME | ISCSI_PASSWORD |
  776. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  777. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  778. ISCSI_LU_RESET_TMO |
  779. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  780. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  781. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  782. ISCSI_HOST_INITIATOR_NAME | ISCSI_HOST_NETDEV_NAME,
  783. .get_host_param = cxgb3i_host_get_param,
  784. .set_host_param = cxgb3i_host_set_param,
  785. /* session management */
  786. .create_session = cxgb3i_session_create,
  787. .destroy_session = cxgb3i_session_destroy,
  788. .get_session_param = iscsi_session_get_param,
  789. /* connection management */
  790. .create_conn = cxgb3i_conn_create,
  791. .bind_conn = cxgb3i_conn_bind,
  792. .destroy_conn = iscsi_tcp_conn_teardown,
  793. .start_conn = iscsi_conn_start,
  794. .stop_conn = iscsi_conn_stop,
  795. .get_conn_param = cxgb3i_conn_get_param,
  796. .set_param = cxgb3i_conn_set_param,
  797. .get_stats = cxgb3i_conn_get_stats,
  798. /* pdu xmit req. from user space */
  799. .send_pdu = iscsi_conn_send_pdu,
  800. /* task */
  801. .init_task = iscsi_tcp_task_init,
  802. .xmit_task = iscsi_tcp_task_xmit,
  803. .cleanup_task = cxgb3i_conn_cleanup_task,
  804. /* pdu */
  805. .alloc_pdu = cxgb3i_conn_alloc_pdu,
  806. .init_pdu = cxgb3i_conn_init_pdu,
  807. .xmit_pdu = cxgb3i_conn_xmit_pdu,
  808. .parse_pdu_itt = cxgb3i_parse_itt,
  809. /* TCP connect/disconnect */
  810. .ep_connect = cxgb3i_ep_connect,
  811. .ep_poll = cxgb3i_ep_poll,
  812. .ep_disconnect = cxgb3i_ep_disconnect,
  813. /* Error recovery timeout call */
  814. .session_recovery_timedout = iscsi_session_recovery_timedout,
  815. };
  816. int cxgb3i_iscsi_init(void)
  817. {
  818. sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
  819. sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
  820. cxgb3i_log_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
  821. ISCSI_ITT_MASK, sw_tag_idx_bits,
  822. ISCSI_AGE_MASK, sw_tag_age_bits);
  823. cxgb3i_scsi_transport =
  824. iscsi_register_transport(&cxgb3i_iscsi_transport);
  825. if (!cxgb3i_scsi_transport) {
  826. cxgb3i_log_error("Could not register cxgb3i transport.\n");
  827. return -ENODEV;
  828. }
  829. cxgb3i_api_debug("cxgb3i transport 0x%p.\n", cxgb3i_scsi_transport);
  830. return 0;
  831. }
  832. void cxgb3i_iscsi_cleanup(void)
  833. {
  834. if (cxgb3i_scsi_transport) {
  835. cxgb3i_api_debug("cxgb3i transport 0x%p.\n",
  836. cxgb3i_scsi_transport);
  837. iscsi_unregister_transport(&cxgb3i_iscsi_transport);
  838. }
  839. }