be_iscsi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /**
  2. * Copyright (C) 2005 - 2010 ServerEngines
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation. The full GNU General
  8. * Public License is included in this distribution in the file called COPYING.
  9. *
  10. * Written by: Jayamohan Kallickal (jayamohank@serverengines.com)
  11. *
  12. * Contact Information:
  13. * linux-drivers@serverengines.com
  14. *
  15. * ServerEngines
  16. * 209 N. Fair Oaks Ave
  17. * Sunnyvale, CA 94085
  18. *
  19. */
  20. #include <scsi/libiscsi.h>
  21. #include <scsi/scsi_transport_iscsi.h>
  22. #include <scsi/scsi_transport.h>
  23. #include <scsi/scsi_cmnd.h>
  24. #include <scsi/scsi_device.h>
  25. #include <scsi/scsi_host.h>
  26. #include <scsi/scsi.h>
  27. #include "be_iscsi.h"
  28. extern struct iscsi_transport beiscsi_iscsi_transport;
  29. /**
  30. * beiscsi_session_create - creates a new iscsi session
  31. * @cmds_max: max commands supported
  32. * @qdepth: max queue depth supported
  33. * @initial_cmdsn: initial iscsi CMDSN
  34. */
  35. struct iscsi_cls_session *beiscsi_session_create(struct iscsi_endpoint *ep,
  36. u16 cmds_max,
  37. u16 qdepth,
  38. u32 initial_cmdsn)
  39. {
  40. struct Scsi_Host *shost;
  41. struct beiscsi_endpoint *beiscsi_ep;
  42. struct iscsi_cls_session *cls_session;
  43. struct beiscsi_hba *phba;
  44. struct iscsi_session *sess;
  45. struct beiscsi_session *beiscsi_sess;
  46. struct beiscsi_io_task *io_task;
  47. SE_DEBUG(DBG_LVL_8, "In beiscsi_session_create\n");
  48. if (!ep) {
  49. SE_DEBUG(DBG_LVL_1, "beiscsi_session_create: invalid ep\n");
  50. return NULL;
  51. }
  52. beiscsi_ep = ep->dd_data;
  53. phba = beiscsi_ep->phba;
  54. shost = phba->shost;
  55. if (cmds_max > beiscsi_ep->phba->params.wrbs_per_cxn) {
  56. shost_printk(KERN_ERR, shost, "Cannot handle %d cmds."
  57. "Max cmds per session supported is %d. Using %d. "
  58. "\n", cmds_max,
  59. beiscsi_ep->phba->params.wrbs_per_cxn,
  60. beiscsi_ep->phba->params.wrbs_per_cxn);
  61. cmds_max = beiscsi_ep->phba->params.wrbs_per_cxn;
  62. }
  63. cls_session = iscsi_session_setup(&beiscsi_iscsi_transport,
  64. shost, cmds_max,
  65. sizeof(*beiscsi_sess),
  66. sizeof(*io_task),
  67. initial_cmdsn, ISCSI_MAX_TARGET);
  68. if (!cls_session)
  69. return NULL;
  70. sess = cls_session->dd_data;
  71. beiscsi_sess = sess->dd_data;
  72. beiscsi_sess->bhs_pool = pci_pool_create("beiscsi_bhs_pool",
  73. phba->pcidev,
  74. sizeof(struct be_cmd_bhs),
  75. 64, 0);
  76. if (!beiscsi_sess->bhs_pool)
  77. goto destroy_sess;
  78. return cls_session;
  79. destroy_sess:
  80. iscsi_session_teardown(cls_session);
  81. return NULL;
  82. }
  83. /**
  84. * beiscsi_session_destroy - destroys iscsi session
  85. * @cls_session: pointer to iscsi cls session
  86. *
  87. * Destroys iSCSI session instance and releases
  88. * resources allocated for it.
  89. */
  90. void beiscsi_session_destroy(struct iscsi_cls_session *cls_session)
  91. {
  92. struct iscsi_session *sess = cls_session->dd_data;
  93. struct beiscsi_session *beiscsi_sess = sess->dd_data;
  94. SE_DEBUG(DBG_LVL_8, "In beiscsi_session_destroy\n");
  95. pci_pool_destroy(beiscsi_sess->bhs_pool);
  96. iscsi_session_teardown(cls_session);
  97. }
  98. /**
  99. * beiscsi_conn_create - create an instance of iscsi connection
  100. * @cls_session: ptr to iscsi_cls_session
  101. * @cid: iscsi cid
  102. */
  103. struct iscsi_cls_conn *
  104. beiscsi_conn_create(struct iscsi_cls_session *cls_session, u32 cid)
  105. {
  106. struct beiscsi_hba *phba;
  107. struct Scsi_Host *shost;
  108. struct iscsi_cls_conn *cls_conn;
  109. struct beiscsi_conn *beiscsi_conn;
  110. struct iscsi_conn *conn;
  111. struct iscsi_session *sess;
  112. struct beiscsi_session *beiscsi_sess;
  113. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_create ,cid"
  114. "from iscsi layer=%d\n", cid);
  115. shost = iscsi_session_to_shost(cls_session);
  116. phba = iscsi_host_priv(shost);
  117. cls_conn = iscsi_conn_setup(cls_session, sizeof(*beiscsi_conn), cid);
  118. if (!cls_conn)
  119. return NULL;
  120. conn = cls_conn->dd_data;
  121. beiscsi_conn = conn->dd_data;
  122. beiscsi_conn->ep = NULL;
  123. beiscsi_conn->phba = phba;
  124. beiscsi_conn->conn = conn;
  125. sess = cls_session->dd_data;
  126. beiscsi_sess = sess->dd_data;
  127. beiscsi_conn->beiscsi_sess = beiscsi_sess;
  128. return cls_conn;
  129. }
  130. /**
  131. * beiscsi_bindconn_cid - Bind the beiscsi_conn with phba connection table
  132. * @beiscsi_conn: The pointer to beiscsi_conn structure
  133. * @phba: The phba instance
  134. * @cid: The cid to free
  135. */
  136. static int beiscsi_bindconn_cid(struct beiscsi_hba *phba,
  137. struct beiscsi_conn *beiscsi_conn,
  138. unsigned int cid)
  139. {
  140. if (phba->conn_table[cid]) {
  141. SE_DEBUG(DBG_LVL_1,
  142. "Connection table already occupied. Detected clash\n");
  143. return -EINVAL;
  144. } else {
  145. SE_DEBUG(DBG_LVL_8, "phba->conn_table[%d]=%p(beiscsi_conn)\n",
  146. cid, beiscsi_conn);
  147. phba->conn_table[cid] = beiscsi_conn;
  148. }
  149. return 0;
  150. }
  151. /**
  152. * beiscsi_conn_bind - Binds iscsi session/connection with TCP connection
  153. * @cls_session: pointer to iscsi cls session
  154. * @cls_conn: pointer to iscsi cls conn
  155. * @transport_fd: EP handle(64 bit)
  156. *
  157. * This function binds the TCP Conn with iSCSI Connection and Session.
  158. */
  159. int beiscsi_conn_bind(struct iscsi_cls_session *cls_session,
  160. struct iscsi_cls_conn *cls_conn,
  161. u64 transport_fd, int is_leading)
  162. {
  163. struct iscsi_conn *conn = cls_conn->dd_data;
  164. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  165. struct Scsi_Host *shost =
  166. (struct Scsi_Host *)iscsi_session_to_shost(cls_session);
  167. struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
  168. struct beiscsi_endpoint *beiscsi_ep;
  169. struct iscsi_endpoint *ep;
  170. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_bind\n");
  171. ep = iscsi_lookup_endpoint(transport_fd);
  172. if (!ep)
  173. return -EINVAL;
  174. beiscsi_ep = ep->dd_data;
  175. if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
  176. return -EINVAL;
  177. if (beiscsi_ep->phba != phba) {
  178. SE_DEBUG(DBG_LVL_8,
  179. "beiscsi_ep->hba=%p not equal to phba=%p\n",
  180. beiscsi_ep->phba, phba);
  181. return -EEXIST;
  182. }
  183. beiscsi_conn->beiscsi_conn_cid = beiscsi_ep->ep_cid;
  184. beiscsi_conn->ep = beiscsi_ep;
  185. beiscsi_ep->conn = beiscsi_conn;
  186. SE_DEBUG(DBG_LVL_8, "beiscsi_conn=%p conn=%p ep_cid=%d\n",
  187. beiscsi_conn, conn, beiscsi_ep->ep_cid);
  188. return beiscsi_bindconn_cid(phba, beiscsi_conn, beiscsi_ep->ep_cid);
  189. }
  190. /**
  191. * beiscsi_conn_get_param - get the iscsi parameter
  192. * @cls_conn: pointer to iscsi cls conn
  193. * @param: parameter type identifier
  194. * @buf: buffer pointer
  195. *
  196. * returns iscsi parameter
  197. */
  198. int beiscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  199. enum iscsi_param param, char *buf)
  200. {
  201. struct beiscsi_endpoint *beiscsi_ep;
  202. struct iscsi_conn *conn = cls_conn->dd_data;
  203. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  204. int len = 0;
  205. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_param, param= %d\n", param);
  206. beiscsi_ep = beiscsi_conn->ep;
  207. if (!beiscsi_ep) {
  208. SE_DEBUG(DBG_LVL_1,
  209. "In beiscsi_conn_get_param , no beiscsi_ep\n");
  210. return -ENODEV;
  211. }
  212. switch (param) {
  213. case ISCSI_PARAM_CONN_PORT:
  214. len = sprintf(buf, "%hu\n", beiscsi_ep->dst_tcpport);
  215. break;
  216. case ISCSI_PARAM_CONN_ADDRESS:
  217. if (beiscsi_ep->ip_type == BE2_IPV4)
  218. len = sprintf(buf, "%pI4\n", &beiscsi_ep->dst_addr);
  219. else
  220. len = sprintf(buf, "%pI6\n", &beiscsi_ep->dst6_addr);
  221. break;
  222. default:
  223. return iscsi_conn_get_param(cls_conn, param, buf);
  224. }
  225. return len;
  226. }
  227. int beiscsi_set_param(struct iscsi_cls_conn *cls_conn,
  228. enum iscsi_param param, char *buf, int buflen)
  229. {
  230. struct iscsi_conn *conn = cls_conn->dd_data;
  231. struct iscsi_session *session = conn->session;
  232. int ret;
  233. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_set_param, param= %d\n", param);
  234. ret = iscsi_set_param(cls_conn, param, buf, buflen);
  235. if (ret)
  236. return ret;
  237. /*
  238. * If userspace tried to set the value to higher than we can
  239. * support override here.
  240. */
  241. switch (param) {
  242. case ISCSI_PARAM_FIRST_BURST:
  243. if (session->first_burst > 8192)
  244. session->first_burst = 8192;
  245. break;
  246. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  247. if (conn->max_recv_dlength > 65536)
  248. conn->max_recv_dlength = 65536;
  249. break;
  250. case ISCSI_PARAM_MAX_BURST:
  251. if (session->max_burst > 262144)
  252. session->max_burst = 262144;
  253. break;
  254. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  255. if ((conn->max_xmit_dlength > 65536) ||
  256. (conn->max_xmit_dlength == 0))
  257. conn->max_xmit_dlength = 65536;
  258. default:
  259. return 0;
  260. }
  261. return 0;
  262. }
  263. /**
  264. * beiscsi_get_host_param - get the iscsi parameter
  265. * @shost: pointer to scsi_host structure
  266. * @param: parameter type identifier
  267. * @buf: buffer pointer
  268. *
  269. * returns host parameter
  270. */
  271. int beiscsi_get_host_param(struct Scsi_Host *shost,
  272. enum iscsi_host_param param, char *buf)
  273. {
  274. struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
  275. int len = 0;
  276. int status;
  277. SE_DEBUG(DBG_LVL_8, "In beiscsi_get_host_param, param= %d\n", param);
  278. switch (param) {
  279. case ISCSI_HOST_PARAM_HWADDRESS:
  280. status = beiscsi_get_macaddr(buf, phba);
  281. if (status < 0) {
  282. SE_DEBUG(DBG_LVL_1, "beiscsi_get_macaddr Failed\n");
  283. return status;
  284. }
  285. break;
  286. default:
  287. return iscsi_host_get_param(shost, param, buf);
  288. }
  289. return len;
  290. }
  291. int beiscsi_get_macaddr(char *buf, struct beiscsi_hba *phba)
  292. {
  293. struct be_cmd_resp_get_mac_addr *resp;
  294. struct be_mcc_wrb *wrb;
  295. unsigned int tag, wrb_num;
  296. unsigned short status, extd_status;
  297. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  298. int rc;
  299. if (phba->read_mac_address)
  300. return sysfs_format_mac(buf, phba->mac_address,
  301. ETH_ALEN);
  302. tag = be_cmd_get_mac_addr(phba);
  303. if (!tag) {
  304. SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
  305. return -EBUSY;
  306. } else
  307. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  308. phba->ctrl.mcc_numtag[tag]);
  309. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  310. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  311. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  312. if (status || extd_status) {
  313. SE_DEBUG(DBG_LVL_1, "Failed to get be_cmd_get_mac_addr"
  314. " status = %d extd_status = %d\n",
  315. status, extd_status);
  316. free_mcc_tag(&phba->ctrl, tag);
  317. return -EAGAIN;
  318. }
  319. wrb = queue_get_wrb(mccq, wrb_num);
  320. free_mcc_tag(&phba->ctrl, tag);
  321. resp = embedded_payload(wrb);
  322. memcpy(phba->mac_address, resp->mac_address, ETH_ALEN);
  323. rc = sysfs_format_mac(buf, phba->mac_address,
  324. ETH_ALEN);
  325. phba->read_mac_address = 1;
  326. return rc;
  327. }
  328. /**
  329. * beiscsi_conn_get_stats - get the iscsi stats
  330. * @cls_conn: pointer to iscsi cls conn
  331. * @stats: pointer to iscsi_stats structure
  332. *
  333. * returns iscsi stats
  334. */
  335. void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  336. struct iscsi_stats *stats)
  337. {
  338. struct iscsi_conn *conn = cls_conn->dd_data;
  339. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_stats\n");
  340. stats->txdata_octets = conn->txdata_octets;
  341. stats->rxdata_octets = conn->rxdata_octets;
  342. stats->dataout_pdus = conn->dataout_pdus_cnt;
  343. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  344. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  345. stats->datain_pdus = conn->datain_pdus_cnt;
  346. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  347. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  348. stats->r2t_pdus = conn->r2t_pdus_cnt;
  349. stats->digest_err = 0;
  350. stats->timeout_err = 0;
  351. stats->custom_length = 0;
  352. strcpy(stats->custom[0].desc, "eh_abort_cnt");
  353. stats->custom[0].value = conn->eh_abort_cnt;
  354. }
  355. /**
  356. * beiscsi_set_params_for_offld - get the parameters for offload
  357. * @beiscsi_conn: pointer to beiscsi_conn
  358. * @params: pointer to offload_params structure
  359. */
  360. static void beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
  361. struct beiscsi_offload_params *params)
  362. {
  363. struct iscsi_conn *conn = beiscsi_conn->conn;
  364. struct iscsi_session *session = conn->session;
  365. AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
  366. params, session->max_burst);
  367. AMAP_SET_BITS(struct amap_beiscsi_offload_params,
  368. max_send_data_segment_length, params,
  369. conn->max_xmit_dlength);
  370. AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
  371. params, session->first_burst);
  372. AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
  373. session->erl);
  374. AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
  375. conn->datadgst_en);
  376. AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
  377. conn->hdrdgst_en);
  378. AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
  379. session->initial_r2t_en);
  380. AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
  381. session->imm_data_en);
  382. AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
  383. (conn->exp_statsn - 1));
  384. }
  385. /**
  386. * beiscsi_conn_start - offload of session to chip
  387. * @cls_conn: pointer to beiscsi_conn
  388. */
  389. int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  390. {
  391. struct iscsi_conn *conn = cls_conn->dd_data;
  392. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  393. struct beiscsi_endpoint *beiscsi_ep;
  394. struct beiscsi_offload_params params;
  395. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_start\n");
  396. memset(&params, 0, sizeof(struct beiscsi_offload_params));
  397. beiscsi_ep = beiscsi_conn->ep;
  398. if (!beiscsi_ep)
  399. SE_DEBUG(DBG_LVL_1, "In beiscsi_conn_start , no beiscsi_ep\n");
  400. beiscsi_conn->login_in_progress = 0;
  401. beiscsi_set_params_for_offld(beiscsi_conn, &params);
  402. beiscsi_offload_connection(beiscsi_conn, &params);
  403. iscsi_conn_start(cls_conn);
  404. return 0;
  405. }
  406. /**
  407. * beiscsi_get_cid - Allocate a cid
  408. * @phba: The phba instance
  409. */
  410. static int beiscsi_get_cid(struct beiscsi_hba *phba)
  411. {
  412. unsigned short cid = 0xFFFF;
  413. if (!phba->avlbl_cids)
  414. return cid;
  415. cid = phba->cid_array[phba->cid_alloc++];
  416. if (phba->cid_alloc == phba->params.cxns_per_ctrl)
  417. phba->cid_alloc = 0;
  418. phba->avlbl_cids--;
  419. return cid;
  420. }
  421. /**
  422. * beiscsi_put_cid - Free the cid
  423. * @phba: The phba for which the cid is being freed
  424. * @cid: The cid to free
  425. */
  426. static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
  427. {
  428. phba->avlbl_cids++;
  429. phba->cid_array[phba->cid_free++] = cid;
  430. if (phba->cid_free == phba->params.cxns_per_ctrl)
  431. phba->cid_free = 0;
  432. }
  433. /**
  434. * beiscsi_free_ep - free endpoint
  435. * @ep: pointer to iscsi endpoint structure
  436. */
  437. static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
  438. {
  439. struct beiscsi_hba *phba = beiscsi_ep->phba;
  440. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  441. beiscsi_ep->phba = NULL;
  442. }
  443. /**
  444. * beiscsi_open_conn - Ask FW to open a TCP connection
  445. * @ep: endpoint to be used
  446. * @src_addr: The source IP address
  447. * @dst_addr: The Destination IP address
  448. *
  449. * Asks the FW to open a TCP connection
  450. */
  451. static int beiscsi_open_conn(struct iscsi_endpoint *ep,
  452. struct sockaddr *src_addr,
  453. struct sockaddr *dst_addr, int non_blocking)
  454. {
  455. struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
  456. struct beiscsi_hba *phba = beiscsi_ep->phba;
  457. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  458. struct be_mcc_wrb *wrb;
  459. struct tcp_connect_and_offload_out *ptcpcnct_out;
  460. unsigned short status, extd_status;
  461. struct be_dma_mem nonemb_cmd;
  462. unsigned int tag, wrb_num;
  463. int ret = -ENOMEM;
  464. SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn\n");
  465. beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
  466. if (beiscsi_ep->ep_cid == 0xFFFF) {
  467. SE_DEBUG(DBG_LVL_1, "No free cid available\n");
  468. return ret;
  469. }
  470. SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn, ep_cid=%d\n",
  471. beiscsi_ep->ep_cid);
  472. phba->ep_array[beiscsi_ep->ep_cid -
  473. phba->fw_config.iscsi_cid_start] = ep;
  474. if (beiscsi_ep->ep_cid > (phba->fw_config.iscsi_cid_start +
  475. phba->params.cxns_per_ctrl * 2)) {
  476. SE_DEBUG(DBG_LVL_1, "Failed in allocate iscsi cid\n");
  477. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  478. goto free_ep;
  479. }
  480. beiscsi_ep->cid_vld = 0;
  481. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  482. sizeof(struct tcp_connect_and_offload_in),
  483. &nonemb_cmd.dma);
  484. if (nonemb_cmd.va == NULL) {
  485. SE_DEBUG(DBG_LVL_1,
  486. "Failed to allocate memory for mgmt_open_connection"
  487. "\n");
  488. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  489. return -ENOMEM;
  490. }
  491. nonemb_cmd.size = sizeof(struct tcp_connect_and_offload_in);
  492. memset(nonemb_cmd.va, 0, nonemb_cmd.size);
  493. tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
  494. if (!tag) {
  495. SE_DEBUG(DBG_LVL_1,
  496. "mgmt_open_connection Failed for cid=%d\n",
  497. beiscsi_ep->ep_cid);
  498. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  499. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  500. nonemb_cmd.va, nonemb_cmd.dma);
  501. return -EAGAIN;
  502. } else {
  503. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  504. phba->ctrl.mcc_numtag[tag]);
  505. }
  506. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  507. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  508. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  509. if (status || extd_status) {
  510. SE_DEBUG(DBG_LVL_1, "mgmt_open_connection Failed"
  511. " status = %d extd_status = %d\n",
  512. status, extd_status);
  513. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  514. free_mcc_tag(&phba->ctrl, tag);
  515. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  516. nonemb_cmd.va, nonemb_cmd.dma);
  517. goto free_ep;
  518. } else {
  519. wrb = queue_get_wrb(mccq, wrb_num);
  520. free_mcc_tag(&phba->ctrl, tag);
  521. ptcpcnct_out = embedded_payload(wrb);
  522. beiscsi_ep = ep->dd_data;
  523. beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
  524. beiscsi_ep->cid_vld = 1;
  525. SE_DEBUG(DBG_LVL_8, "mgmt_open_connection Success\n");
  526. }
  527. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  528. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  529. nonemb_cmd.va, nonemb_cmd.dma);
  530. return 0;
  531. free_ep:
  532. beiscsi_free_ep(beiscsi_ep);
  533. return -EBUSY;
  534. }
  535. /**
  536. * beiscsi_ep_connect - Ask chip to create TCP Conn
  537. * @scsi_host: Pointer to scsi_host structure
  538. * @dst_addr: The IP address of Target
  539. * @non_blocking: blocking or non-blocking call
  540. *
  541. * This routines first asks chip to create a connection and then allocates an EP
  542. */
  543. struct iscsi_endpoint *
  544. beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  545. int non_blocking)
  546. {
  547. struct beiscsi_hba *phba;
  548. struct beiscsi_endpoint *beiscsi_ep;
  549. struct iscsi_endpoint *ep;
  550. int ret;
  551. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_connect\n");
  552. if (shost)
  553. phba = iscsi_host_priv(shost);
  554. else {
  555. ret = -ENXIO;
  556. SE_DEBUG(DBG_LVL_1, "shost is NULL\n");
  557. return ERR_PTR(ret);
  558. }
  559. if (phba->state != BE_ADAPTER_UP) {
  560. ret = -EBUSY;
  561. SE_DEBUG(DBG_LVL_1, "The Adapter state is Not UP\n");
  562. return ERR_PTR(ret);
  563. }
  564. ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
  565. if (!ep) {
  566. ret = -ENOMEM;
  567. return ERR_PTR(ret);
  568. }
  569. beiscsi_ep = ep->dd_data;
  570. beiscsi_ep->phba = phba;
  571. beiscsi_ep->openiscsi_ep = ep;
  572. ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
  573. if (ret) {
  574. SE_DEBUG(DBG_LVL_1, "Failed in beiscsi_open_conn\n");
  575. goto free_ep;
  576. }
  577. return ep;
  578. free_ep:
  579. iscsi_destroy_endpoint(ep);
  580. return ERR_PTR(ret);
  581. }
  582. /**
  583. * beiscsi_ep_poll - Poll to see if connection is established
  584. * @ep: endpoint to be used
  585. * @timeout_ms: timeout specified in millisecs
  586. *
  587. * Poll to see if TCP connection established
  588. */
  589. int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  590. {
  591. struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
  592. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_poll\n");
  593. if (beiscsi_ep->cid_vld == 1)
  594. return 1;
  595. else
  596. return 0;
  597. }
  598. /**
  599. * beiscsi_close_conn - Upload the connection
  600. * @ep: The iscsi endpoint
  601. * @flag: The type of connection closure
  602. */
  603. static int beiscsi_close_conn(struct beiscsi_endpoint *beiscsi_ep, int flag)
  604. {
  605. int ret = 0;
  606. unsigned int tag;
  607. struct beiscsi_hba *phba = beiscsi_ep->phba;
  608. tag = mgmt_upload_connection(phba, beiscsi_ep->ep_cid, flag);
  609. if (!tag) {
  610. SE_DEBUG(DBG_LVL_8, "upload failed for cid 0x%x\n",
  611. beiscsi_ep->ep_cid);
  612. ret = -EAGAIN;
  613. } else {
  614. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  615. phba->ctrl.mcc_numtag[tag]);
  616. free_mcc_tag(&phba->ctrl, tag);
  617. }
  618. return ret;
  619. }
  620. /**
  621. * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
  622. * @phba: The phba instance
  623. * @cid: The cid to free
  624. */
  625. static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba *phba,
  626. unsigned int cid)
  627. {
  628. if (phba->conn_table[cid])
  629. phba->conn_table[cid] = NULL;
  630. else {
  631. SE_DEBUG(DBG_LVL_8, "Connection table Not occupied.\n");
  632. return -EINVAL;
  633. }
  634. return 0;
  635. }
  636. /**
  637. * beiscsi_ep_disconnect - Tears down the TCP connection
  638. * @ep: endpoint to be used
  639. *
  640. * Tears down the TCP connection
  641. */
  642. void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
  643. {
  644. struct beiscsi_conn *beiscsi_conn;
  645. struct beiscsi_endpoint *beiscsi_ep;
  646. struct beiscsi_hba *phba;
  647. unsigned int tag;
  648. unsigned short savecfg_flag = CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH;
  649. beiscsi_ep = ep->dd_data;
  650. phba = beiscsi_ep->phba;
  651. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect for ep_cid = %d\n",
  652. beiscsi_ep->ep_cid);
  653. if (!beiscsi_ep->conn) {
  654. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect, no "
  655. "beiscsi_ep\n");
  656. return;
  657. }
  658. beiscsi_conn = beiscsi_ep->conn;
  659. iscsi_suspend_queue(beiscsi_conn->conn);
  660. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect ep_cid = %d\n",
  661. beiscsi_ep->ep_cid);
  662. tag = mgmt_invalidate_connection(phba, beiscsi_ep,
  663. beiscsi_ep->ep_cid, 1,
  664. savecfg_flag);
  665. if (!tag) {
  666. SE_DEBUG(DBG_LVL_1,
  667. "mgmt_invalidate_connection Failed for cid=%d\n",
  668. beiscsi_ep->ep_cid);
  669. } else {
  670. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  671. phba->ctrl.mcc_numtag[tag]);
  672. free_mcc_tag(&phba->ctrl, tag);
  673. }
  674. beiscsi_close_conn(beiscsi_ep, CONNECTION_UPLOAD_GRACEFUL);
  675. beiscsi_free_ep(beiscsi_ep);
  676. beiscsi_unbind_conn_to_cid(phba, beiscsi_ep->ep_cid);
  677. iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
  678. }