cxgb3i_iscsi.c 27 KB

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