be_iscsi.c 20 KB

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