be_iscsi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 status = 0;
  276. SE_DEBUG(DBG_LVL_8, "In beiscsi_get_host_param, param= %d\n", param);
  277. switch (param) {
  278. case ISCSI_HOST_PARAM_HWADDRESS:
  279. status = beiscsi_get_macaddr(buf, phba);
  280. if (status < 0) {
  281. SE_DEBUG(DBG_LVL_1, "beiscsi_get_macaddr Failed\n");
  282. return status;
  283. }
  284. break;
  285. default:
  286. return iscsi_host_get_param(shost, param, buf);
  287. }
  288. return status;
  289. }
  290. int beiscsi_get_macaddr(char *buf, struct beiscsi_hba *phba)
  291. {
  292. struct be_cmd_resp_get_mac_addr *resp;
  293. struct be_mcc_wrb *wrb;
  294. unsigned int tag, wrb_num;
  295. unsigned short status, extd_status;
  296. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  297. int rc;
  298. if (phba->read_mac_address)
  299. return sysfs_format_mac(buf, phba->mac_address,
  300. ETH_ALEN);
  301. tag = be_cmd_get_mac_addr(phba);
  302. if (!tag) {
  303. SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
  304. return -EBUSY;
  305. } else
  306. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  307. phba->ctrl.mcc_numtag[tag]);
  308. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  309. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  310. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  311. if (status || extd_status) {
  312. SE_DEBUG(DBG_LVL_1, "Failed to get be_cmd_get_mac_addr"
  313. " status = %d extd_status = %d\n",
  314. status, extd_status);
  315. free_mcc_tag(&phba->ctrl, tag);
  316. return -EAGAIN;
  317. }
  318. wrb = queue_get_wrb(mccq, wrb_num);
  319. free_mcc_tag(&phba->ctrl, tag);
  320. resp = embedded_payload(wrb);
  321. memcpy(phba->mac_address, resp->mac_address, ETH_ALEN);
  322. rc = sysfs_format_mac(buf, phba->mac_address,
  323. ETH_ALEN);
  324. phba->read_mac_address = 1;
  325. return rc;
  326. }
  327. /**
  328. * beiscsi_conn_get_stats - get the iscsi stats
  329. * @cls_conn: pointer to iscsi cls conn
  330. * @stats: pointer to iscsi_stats structure
  331. *
  332. * returns iscsi stats
  333. */
  334. void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  335. struct iscsi_stats *stats)
  336. {
  337. struct iscsi_conn *conn = cls_conn->dd_data;
  338. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_stats\n");
  339. stats->txdata_octets = conn->txdata_octets;
  340. stats->rxdata_octets = conn->rxdata_octets;
  341. stats->dataout_pdus = conn->dataout_pdus_cnt;
  342. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  343. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  344. stats->datain_pdus = conn->datain_pdus_cnt;
  345. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  346. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  347. stats->r2t_pdus = conn->r2t_pdus_cnt;
  348. stats->digest_err = 0;
  349. stats->timeout_err = 0;
  350. stats->custom_length = 0;
  351. strcpy(stats->custom[0].desc, "eh_abort_cnt");
  352. stats->custom[0].value = conn->eh_abort_cnt;
  353. }
  354. /**
  355. * beiscsi_set_params_for_offld - get the parameters for offload
  356. * @beiscsi_conn: pointer to beiscsi_conn
  357. * @params: pointer to offload_params structure
  358. */
  359. static void beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
  360. struct beiscsi_offload_params *params)
  361. {
  362. struct iscsi_conn *conn = beiscsi_conn->conn;
  363. struct iscsi_session *session = conn->session;
  364. AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
  365. params, session->max_burst);
  366. AMAP_SET_BITS(struct amap_beiscsi_offload_params,
  367. max_send_data_segment_length, params,
  368. conn->max_xmit_dlength);
  369. AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
  370. params, session->first_burst);
  371. AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
  372. session->erl);
  373. AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
  374. conn->datadgst_en);
  375. AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
  376. conn->hdrdgst_en);
  377. AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
  378. session->initial_r2t_en);
  379. AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
  380. session->imm_data_en);
  381. AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
  382. (conn->exp_statsn - 1));
  383. }
  384. /**
  385. * beiscsi_conn_start - offload of session to chip
  386. * @cls_conn: pointer to beiscsi_conn
  387. */
  388. int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  389. {
  390. struct iscsi_conn *conn = cls_conn->dd_data;
  391. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  392. struct beiscsi_endpoint *beiscsi_ep;
  393. struct beiscsi_offload_params params;
  394. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_start\n");
  395. memset(&params, 0, sizeof(struct beiscsi_offload_params));
  396. beiscsi_ep = beiscsi_conn->ep;
  397. if (!beiscsi_ep)
  398. SE_DEBUG(DBG_LVL_1, "In beiscsi_conn_start , no beiscsi_ep\n");
  399. beiscsi_conn->login_in_progress = 0;
  400. beiscsi_set_params_for_offld(beiscsi_conn, &params);
  401. beiscsi_offload_connection(beiscsi_conn, &params);
  402. iscsi_conn_start(cls_conn);
  403. return 0;
  404. }
  405. /**
  406. * beiscsi_get_cid - Allocate a cid
  407. * @phba: The phba instance
  408. */
  409. static int beiscsi_get_cid(struct beiscsi_hba *phba)
  410. {
  411. unsigned short cid = 0xFFFF;
  412. if (!phba->avlbl_cids)
  413. return cid;
  414. cid = phba->cid_array[phba->cid_alloc++];
  415. if (phba->cid_alloc == phba->params.cxns_per_ctrl)
  416. phba->cid_alloc = 0;
  417. phba->avlbl_cids--;
  418. return cid;
  419. }
  420. /**
  421. * beiscsi_put_cid - Free the cid
  422. * @phba: The phba for which the cid is being freed
  423. * @cid: The cid to free
  424. */
  425. static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
  426. {
  427. phba->avlbl_cids++;
  428. phba->cid_array[phba->cid_free++] = cid;
  429. if (phba->cid_free == phba->params.cxns_per_ctrl)
  430. phba->cid_free = 0;
  431. }
  432. /**
  433. * beiscsi_free_ep - free endpoint
  434. * @ep: pointer to iscsi endpoint structure
  435. */
  436. static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
  437. {
  438. struct beiscsi_hba *phba = beiscsi_ep->phba;
  439. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  440. beiscsi_ep->phba = NULL;
  441. }
  442. /**
  443. * beiscsi_open_conn - Ask FW to open a TCP connection
  444. * @ep: endpoint to be used
  445. * @src_addr: The source IP address
  446. * @dst_addr: The Destination IP address
  447. *
  448. * Asks the FW to open a TCP connection
  449. */
  450. static int beiscsi_open_conn(struct iscsi_endpoint *ep,
  451. struct sockaddr *src_addr,
  452. struct sockaddr *dst_addr, int non_blocking)
  453. {
  454. struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
  455. struct beiscsi_hba *phba = beiscsi_ep->phba;
  456. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  457. struct be_mcc_wrb *wrb;
  458. struct tcp_connect_and_offload_out *ptcpcnct_out;
  459. unsigned short status, extd_status;
  460. struct be_dma_mem nonemb_cmd;
  461. unsigned int tag, wrb_num;
  462. int ret = -ENOMEM;
  463. SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn\n");
  464. beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
  465. if (beiscsi_ep->ep_cid == 0xFFFF) {
  466. SE_DEBUG(DBG_LVL_1, "No free cid available\n");
  467. return ret;
  468. }
  469. SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn, ep_cid=%d\n",
  470. beiscsi_ep->ep_cid);
  471. phba->ep_array[beiscsi_ep->ep_cid -
  472. phba->fw_config.iscsi_cid_start] = ep;
  473. if (beiscsi_ep->ep_cid > (phba->fw_config.iscsi_cid_start +
  474. phba->params.cxns_per_ctrl * 2)) {
  475. SE_DEBUG(DBG_LVL_1, "Failed in allocate iscsi cid\n");
  476. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  477. goto free_ep;
  478. }
  479. beiscsi_ep->cid_vld = 0;
  480. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  481. sizeof(struct tcp_connect_and_offload_in),
  482. &nonemb_cmd.dma);
  483. if (nonemb_cmd.va == NULL) {
  484. SE_DEBUG(DBG_LVL_1,
  485. "Failed to allocate memory for mgmt_open_connection"
  486. "\n");
  487. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  488. return -ENOMEM;
  489. }
  490. nonemb_cmd.size = sizeof(struct tcp_connect_and_offload_in);
  491. memset(nonemb_cmd.va, 0, nonemb_cmd.size);
  492. tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
  493. if (!tag) {
  494. SE_DEBUG(DBG_LVL_1,
  495. "mgmt_open_connection Failed for cid=%d\n",
  496. beiscsi_ep->ep_cid);
  497. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  498. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  499. nonemb_cmd.va, nonemb_cmd.dma);
  500. return -EAGAIN;
  501. } else {
  502. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  503. phba->ctrl.mcc_numtag[tag]);
  504. }
  505. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  506. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  507. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  508. if (status || extd_status) {
  509. SE_DEBUG(DBG_LVL_1, "mgmt_open_connection Failed"
  510. " status = %d extd_status = %d\n",
  511. status, extd_status);
  512. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  513. free_mcc_tag(&phba->ctrl, tag);
  514. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  515. nonemb_cmd.va, nonemb_cmd.dma);
  516. goto free_ep;
  517. } else {
  518. wrb = queue_get_wrb(mccq, wrb_num);
  519. free_mcc_tag(&phba->ctrl, tag);
  520. ptcpcnct_out = embedded_payload(wrb);
  521. beiscsi_ep = ep->dd_data;
  522. beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
  523. beiscsi_ep->cid_vld = 1;
  524. SE_DEBUG(DBG_LVL_8, "mgmt_open_connection Success\n");
  525. }
  526. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  527. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  528. nonemb_cmd.va, nonemb_cmd.dma);
  529. return 0;
  530. free_ep:
  531. beiscsi_free_ep(beiscsi_ep);
  532. return -EBUSY;
  533. }
  534. /**
  535. * beiscsi_ep_connect - Ask chip to create TCP Conn
  536. * @scsi_host: Pointer to scsi_host structure
  537. * @dst_addr: The IP address of Target
  538. * @non_blocking: blocking or non-blocking call
  539. *
  540. * This routines first asks chip to create a connection and then allocates an EP
  541. */
  542. struct iscsi_endpoint *
  543. beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  544. int non_blocking)
  545. {
  546. struct beiscsi_hba *phba;
  547. struct beiscsi_endpoint *beiscsi_ep;
  548. struct iscsi_endpoint *ep;
  549. int ret;
  550. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_connect\n");
  551. if (shost)
  552. phba = iscsi_host_priv(shost);
  553. else {
  554. ret = -ENXIO;
  555. SE_DEBUG(DBG_LVL_1, "shost is NULL\n");
  556. return ERR_PTR(ret);
  557. }
  558. if (phba->state != BE_ADAPTER_UP) {
  559. ret = -EBUSY;
  560. SE_DEBUG(DBG_LVL_1, "The Adapter state is Not UP\n");
  561. return ERR_PTR(ret);
  562. }
  563. ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
  564. if (!ep) {
  565. ret = -ENOMEM;
  566. return ERR_PTR(ret);
  567. }
  568. beiscsi_ep = ep->dd_data;
  569. beiscsi_ep->phba = phba;
  570. beiscsi_ep->openiscsi_ep = ep;
  571. ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
  572. if (ret) {
  573. SE_DEBUG(DBG_LVL_1, "Failed in beiscsi_open_conn\n");
  574. goto free_ep;
  575. }
  576. return ep;
  577. free_ep:
  578. iscsi_destroy_endpoint(ep);
  579. return ERR_PTR(ret);
  580. }
  581. /**
  582. * beiscsi_ep_poll - Poll to see if connection is established
  583. * @ep: endpoint to be used
  584. * @timeout_ms: timeout specified in millisecs
  585. *
  586. * Poll to see if TCP connection established
  587. */
  588. int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  589. {
  590. struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
  591. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_poll\n");
  592. if (beiscsi_ep->cid_vld == 1)
  593. return 1;
  594. else
  595. return 0;
  596. }
  597. /**
  598. * beiscsi_close_conn - Upload the connection
  599. * @ep: The iscsi endpoint
  600. * @flag: The type of connection closure
  601. */
  602. static int beiscsi_close_conn(struct beiscsi_endpoint *beiscsi_ep, int flag)
  603. {
  604. int ret = 0;
  605. unsigned int tag;
  606. struct beiscsi_hba *phba = beiscsi_ep->phba;
  607. tag = mgmt_upload_connection(phba, beiscsi_ep->ep_cid, flag);
  608. if (!tag) {
  609. SE_DEBUG(DBG_LVL_8, "upload failed for cid 0x%x\n",
  610. beiscsi_ep->ep_cid);
  611. ret = -EAGAIN;
  612. } else {
  613. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  614. phba->ctrl.mcc_numtag[tag]);
  615. free_mcc_tag(&phba->ctrl, tag);
  616. }
  617. return ret;
  618. }
  619. /**
  620. * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
  621. * @phba: The phba instance
  622. * @cid: The cid to free
  623. */
  624. static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba *phba,
  625. unsigned int cid)
  626. {
  627. if (phba->conn_table[cid])
  628. phba->conn_table[cid] = NULL;
  629. else {
  630. SE_DEBUG(DBG_LVL_8, "Connection table Not occupied.\n");
  631. return -EINVAL;
  632. }
  633. return 0;
  634. }
  635. /**
  636. * beiscsi_ep_disconnect - Tears down the TCP connection
  637. * @ep: endpoint to be used
  638. *
  639. * Tears down the TCP connection
  640. */
  641. void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
  642. {
  643. struct beiscsi_conn *beiscsi_conn;
  644. struct beiscsi_endpoint *beiscsi_ep;
  645. struct beiscsi_hba *phba;
  646. unsigned int tag;
  647. unsigned short savecfg_flag = CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH;
  648. beiscsi_ep = ep->dd_data;
  649. phba = beiscsi_ep->phba;
  650. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect for ep_cid = %d\n",
  651. beiscsi_ep->ep_cid);
  652. if (!beiscsi_ep->conn) {
  653. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect, no "
  654. "beiscsi_ep\n");
  655. return;
  656. }
  657. beiscsi_conn = beiscsi_ep->conn;
  658. iscsi_suspend_queue(beiscsi_conn->conn);
  659. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect ep_cid = %d\n",
  660. beiscsi_ep->ep_cid);
  661. tag = mgmt_invalidate_connection(phba, beiscsi_ep,
  662. beiscsi_ep->ep_cid, 1,
  663. savecfg_flag);
  664. if (!tag) {
  665. SE_DEBUG(DBG_LVL_1,
  666. "mgmt_invalidate_connection Failed for cid=%d\n",
  667. beiscsi_ep->ep_cid);
  668. } else {
  669. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  670. phba->ctrl.mcc_numtag[tag]);
  671. free_mcc_tag(&phba->ctrl, tag);
  672. }
  673. beiscsi_close_conn(beiscsi_ep, CONNECTION_UPLOAD_GRACEFUL);
  674. beiscsi_free_ep(beiscsi_ep);
  675. beiscsi_unbind_conn_to_cid(phba, beiscsi_ep->ep_cid);
  676. iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
  677. }