be_cmds.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. * Contact Information:
  11. * linux-drivers@serverengines.com
  12. *
  13. * ServerEngines
  14. * 209 N. Fair Oaks Ave
  15. * Sunnyvale, CA 94085
  16. */
  17. #include "be.h"
  18. #include "be_mgmt.h"
  19. #include "be_main.h"
  20. void be_mcc_notify(struct beiscsi_hba *phba)
  21. {
  22. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  23. u32 val = 0;
  24. val |= mccq->id & DB_MCCQ_RING_ID_MASK;
  25. val |= 1 << DB_MCCQ_NUM_POSTED_SHIFT;
  26. iowrite32(val, phba->db_va + DB_MCCQ_OFFSET);
  27. }
  28. unsigned int alloc_mcc_tag(struct beiscsi_hba *phba)
  29. {
  30. unsigned int tag = 0;
  31. if (phba->ctrl.mcc_tag_available) {
  32. tag = phba->ctrl.mcc_tag[phba->ctrl.mcc_alloc_index];
  33. phba->ctrl.mcc_tag[phba->ctrl.mcc_alloc_index] = 0;
  34. phba->ctrl.mcc_numtag[tag] = 0;
  35. }
  36. if (tag) {
  37. phba->ctrl.mcc_tag_available--;
  38. if (phba->ctrl.mcc_alloc_index == (MAX_MCC_CMD - 1))
  39. phba->ctrl.mcc_alloc_index = 0;
  40. else
  41. phba->ctrl.mcc_alloc_index++;
  42. }
  43. return tag;
  44. }
  45. void free_mcc_tag(struct be_ctrl_info *ctrl, unsigned int tag)
  46. {
  47. spin_lock(&ctrl->mbox_lock);
  48. tag = tag & 0x000000FF;
  49. ctrl->mcc_tag[ctrl->mcc_free_index] = tag;
  50. if (ctrl->mcc_free_index == (MAX_MCC_CMD - 1))
  51. ctrl->mcc_free_index = 0;
  52. else
  53. ctrl->mcc_free_index++;
  54. ctrl->mcc_tag_available++;
  55. spin_unlock(&ctrl->mbox_lock);
  56. }
  57. bool is_link_state_evt(u32 trailer)
  58. {
  59. return (((trailer >> ASYNC_TRAILER_EVENT_CODE_SHIFT) &
  60. ASYNC_TRAILER_EVENT_CODE_MASK) ==
  61. ASYNC_EVENT_CODE_LINK_STATE);
  62. }
  63. static inline bool be_mcc_compl_is_new(struct be_mcc_compl *compl)
  64. {
  65. if (compl->flags != 0) {
  66. compl->flags = le32_to_cpu(compl->flags);
  67. WARN_ON((compl->flags & CQE_FLAGS_VALID_MASK) == 0);
  68. return true;
  69. } else
  70. return false;
  71. }
  72. static inline void be_mcc_compl_use(struct be_mcc_compl *compl)
  73. {
  74. compl->flags = 0;
  75. }
  76. static int be_mcc_compl_process(struct be_ctrl_info *ctrl,
  77. struct be_mcc_compl *compl)
  78. {
  79. u16 compl_status, extd_status;
  80. be_dws_le_to_cpu(compl, 4);
  81. compl_status = (compl->status >> CQE_STATUS_COMPL_SHIFT) &
  82. CQE_STATUS_COMPL_MASK;
  83. if (compl_status != MCC_STATUS_SUCCESS) {
  84. extd_status = (compl->status >> CQE_STATUS_EXTD_SHIFT) &
  85. CQE_STATUS_EXTD_MASK;
  86. dev_err(&ctrl->pdev->dev,
  87. "error in cmd completion: status(compl/extd)=%d/%d\n",
  88. compl_status, extd_status);
  89. return -1;
  90. }
  91. return 0;
  92. }
  93. int be_mcc_compl_process_isr(struct be_ctrl_info *ctrl,
  94. struct be_mcc_compl *compl)
  95. {
  96. u16 compl_status, extd_status;
  97. unsigned short tag;
  98. be_dws_le_to_cpu(compl, 4);
  99. compl_status = (compl->status >> CQE_STATUS_COMPL_SHIFT) &
  100. CQE_STATUS_COMPL_MASK;
  101. /* The ctrl.mcc_numtag[tag] is filled with
  102. * [31] = valid, [30:24] = Rsvd, [23:16] = wrb, [15:8] = extd_status,
  103. * [7:0] = compl_status
  104. */
  105. tag = (compl->tag0 & 0x000000FF);
  106. extd_status = (compl->status >> CQE_STATUS_EXTD_SHIFT) &
  107. CQE_STATUS_EXTD_MASK;
  108. ctrl->mcc_numtag[tag] = 0x80000000;
  109. ctrl->mcc_numtag[tag] |= (compl->tag0 & 0x00FF0000);
  110. ctrl->mcc_numtag[tag] |= (extd_status & 0x000000FF) << 8;
  111. ctrl->mcc_numtag[tag] |= (compl_status & 0x000000FF);
  112. wake_up_interruptible(&ctrl->mcc_wait[tag]);
  113. return 0;
  114. }
  115. static struct be_mcc_compl *be_mcc_compl_get(struct beiscsi_hba *phba)
  116. {
  117. struct be_queue_info *mcc_cq = &phba->ctrl.mcc_obj.cq;
  118. struct be_mcc_compl *compl = queue_tail_node(mcc_cq);
  119. if (be_mcc_compl_is_new(compl)) {
  120. queue_tail_inc(mcc_cq);
  121. return compl;
  122. }
  123. return NULL;
  124. }
  125. static void be2iscsi_fail_session(struct iscsi_cls_session *cls_session)
  126. {
  127. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
  128. }
  129. void beiscsi_async_link_state_process(struct beiscsi_hba *phba,
  130. struct be_async_event_link_state *evt)
  131. {
  132. switch (evt->port_link_status) {
  133. case ASYNC_EVENT_LINK_DOWN:
  134. SE_DEBUG(DBG_LVL_1, "Link Down on Physical Port %d \n",
  135. evt->physical_port);
  136. phba->state |= BE_ADAPTER_LINK_DOWN;
  137. iscsi_host_for_each_session(phba->shost,
  138. be2iscsi_fail_session);
  139. break;
  140. case ASYNC_EVENT_LINK_UP:
  141. phba->state = BE_ADAPTER_UP;
  142. SE_DEBUG(DBG_LVL_1, "Link UP on Physical Port %d \n",
  143. evt->physical_port);
  144. break;
  145. default:
  146. SE_DEBUG(DBG_LVL_1, "Unexpected Async Notification %d on"
  147. "Physical Port %d \n",
  148. evt->port_link_status,
  149. evt->physical_port);
  150. }
  151. }
  152. static void beiscsi_cq_notify(struct beiscsi_hba *phba, u16 qid, bool arm,
  153. u16 num_popped)
  154. {
  155. u32 val = 0;
  156. val |= qid & DB_CQ_RING_ID_MASK;
  157. if (arm)
  158. val |= 1 << DB_CQ_REARM_SHIFT;
  159. val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
  160. iowrite32(val, phba->db_va + DB_CQ_OFFSET);
  161. }
  162. int beiscsi_process_mcc(struct beiscsi_hba *phba)
  163. {
  164. struct be_mcc_compl *compl;
  165. int num = 0, status = 0;
  166. struct be_ctrl_info *ctrl = &phba->ctrl;
  167. spin_lock_bh(&phba->ctrl.mcc_cq_lock);
  168. while ((compl = be_mcc_compl_get(phba))) {
  169. if (compl->flags & CQE_FLAGS_ASYNC_MASK) {
  170. /* Interpret flags as an async trailer */
  171. if (is_link_state_evt(compl->flags))
  172. /* Interpret compl as a async link evt */
  173. beiscsi_async_link_state_process(phba,
  174. (struct be_async_event_link_state *) compl);
  175. else
  176. SE_DEBUG(DBG_LVL_1,
  177. " Unsupported Async Event, flags"
  178. " = 0x%08x \n", compl->flags);
  179. } else if (compl->flags & CQE_FLAGS_COMPLETED_MASK) {
  180. status = be_mcc_compl_process(ctrl, compl);
  181. atomic_dec(&phba->ctrl.mcc_obj.q.used);
  182. }
  183. be_mcc_compl_use(compl);
  184. num++;
  185. }
  186. if (num)
  187. beiscsi_cq_notify(phba, phba->ctrl.mcc_obj.cq.id, true, num);
  188. spin_unlock_bh(&phba->ctrl.mcc_cq_lock);
  189. return status;
  190. }
  191. /* Wait till no more pending mcc requests are present */
  192. static int be_mcc_wait_compl(struct beiscsi_hba *phba)
  193. {
  194. int i, status;
  195. for (i = 0; i < mcc_timeout; i++) {
  196. status = beiscsi_process_mcc(phba);
  197. if (status)
  198. return status;
  199. if (atomic_read(&phba->ctrl.mcc_obj.q.used) == 0)
  200. break;
  201. udelay(100);
  202. }
  203. if (i == mcc_timeout) {
  204. dev_err(&phba->pcidev->dev, "mccq poll timed out\n");
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. /* Notify MCC requests and wait for completion */
  210. int be_mcc_notify_wait(struct beiscsi_hba *phba)
  211. {
  212. be_mcc_notify(phba);
  213. return be_mcc_wait_compl(phba);
  214. }
  215. static int be_mbox_db_ready_wait(struct be_ctrl_info *ctrl)
  216. {
  217. #define long_delay 2000
  218. void __iomem *db = ctrl->db + MPU_MAILBOX_DB_OFFSET;
  219. int cnt = 0, wait = 5; /* in usecs */
  220. u32 ready;
  221. do {
  222. ready = ioread32(db) & MPU_MAILBOX_DB_RDY_MASK;
  223. if (ready)
  224. break;
  225. if (cnt > 6000000) {
  226. dev_err(&ctrl->pdev->dev, "mbox_db poll timed out\n");
  227. return -1;
  228. }
  229. if (cnt > 50) {
  230. wait = long_delay;
  231. mdelay(long_delay / 1000);
  232. } else
  233. udelay(wait);
  234. cnt += wait;
  235. } while (true);
  236. return 0;
  237. }
  238. int be_mbox_notify(struct be_ctrl_info *ctrl)
  239. {
  240. int status;
  241. u32 val = 0;
  242. void __iomem *db = ctrl->db + MPU_MAILBOX_DB_OFFSET;
  243. struct be_dma_mem *mbox_mem = &ctrl->mbox_mem;
  244. struct be_mcc_mailbox *mbox = mbox_mem->va;
  245. struct be_mcc_compl *compl = &mbox->compl;
  246. val &= ~MPU_MAILBOX_DB_RDY_MASK;
  247. val |= MPU_MAILBOX_DB_HI_MASK;
  248. val |= (upper_32_bits(mbox_mem->dma) >> 2) << 2;
  249. iowrite32(val, db);
  250. status = be_mbox_db_ready_wait(ctrl);
  251. if (status != 0) {
  252. SE_DEBUG(DBG_LVL_1, " be_mbox_db_ready_wait failed 1\n");
  253. return status;
  254. }
  255. val = 0;
  256. val &= ~MPU_MAILBOX_DB_RDY_MASK;
  257. val &= ~MPU_MAILBOX_DB_HI_MASK;
  258. val |= (u32) (mbox_mem->dma >> 4) << 2;
  259. iowrite32(val, db);
  260. status = be_mbox_db_ready_wait(ctrl);
  261. if (status != 0) {
  262. SE_DEBUG(DBG_LVL_1, " be_mbox_db_ready_wait failed 2\n");
  263. return status;
  264. }
  265. if (be_mcc_compl_is_new(compl)) {
  266. status = be_mcc_compl_process(ctrl, &mbox->compl);
  267. be_mcc_compl_use(compl);
  268. if (status) {
  269. SE_DEBUG(DBG_LVL_1, "After be_mcc_compl_process \n");
  270. return status;
  271. }
  272. } else {
  273. dev_err(&ctrl->pdev->dev, "invalid mailbox completion\n");
  274. return -1;
  275. }
  276. return 0;
  277. }
  278. /*
  279. * Insert the mailbox address into the doorbell in two steps
  280. * Polls on the mbox doorbell till a command completion (or a timeout) occurs
  281. */
  282. static int be_mbox_notify_wait(struct beiscsi_hba *phba)
  283. {
  284. int status;
  285. u32 val = 0;
  286. void __iomem *db = phba->ctrl.db + MPU_MAILBOX_DB_OFFSET;
  287. struct be_dma_mem *mbox_mem = &phba->ctrl.mbox_mem;
  288. struct be_mcc_mailbox *mbox = mbox_mem->va;
  289. struct be_mcc_compl *compl = &mbox->compl;
  290. struct be_ctrl_info *ctrl = &phba->ctrl;
  291. val |= MPU_MAILBOX_DB_HI_MASK;
  292. /* at bits 2 - 31 place mbox dma addr msb bits 34 - 63 */
  293. val |= (upper_32_bits(mbox_mem->dma) >> 2) << 2;
  294. iowrite32(val, db);
  295. /* wait for ready to be set */
  296. status = be_mbox_db_ready_wait(ctrl);
  297. if (status != 0)
  298. return status;
  299. val = 0;
  300. /* at bits 2 - 31 place mbox dma addr lsb bits 4 - 33 */
  301. val |= (u32)(mbox_mem->dma >> 4) << 2;
  302. iowrite32(val, db);
  303. status = be_mbox_db_ready_wait(ctrl);
  304. if (status != 0)
  305. return status;
  306. /* A cq entry has been made now */
  307. if (be_mcc_compl_is_new(compl)) {
  308. status = be_mcc_compl_process(ctrl, &mbox->compl);
  309. be_mcc_compl_use(compl);
  310. if (status)
  311. return status;
  312. } else {
  313. dev_err(&phba->pcidev->dev, "invalid mailbox completion\n");
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. void be_wrb_hdr_prepare(struct be_mcc_wrb *wrb, int payload_len,
  319. bool embedded, u8 sge_cnt)
  320. {
  321. if (embedded)
  322. wrb->embedded |= MCC_WRB_EMBEDDED_MASK;
  323. else
  324. wrb->embedded |= (sge_cnt & MCC_WRB_SGE_CNT_MASK) <<
  325. MCC_WRB_SGE_CNT_SHIFT;
  326. wrb->payload_length = payload_len;
  327. be_dws_cpu_to_le(wrb, 8);
  328. }
  329. void be_cmd_hdr_prepare(struct be_cmd_req_hdr *req_hdr,
  330. u8 subsystem, u8 opcode, int cmd_len)
  331. {
  332. req_hdr->opcode = opcode;
  333. req_hdr->subsystem = subsystem;
  334. req_hdr->request_length = cpu_to_le32(cmd_len - sizeof(*req_hdr));
  335. }
  336. static void be_cmd_page_addrs_prepare(struct phys_addr *pages, u32 max_pages,
  337. struct be_dma_mem *mem)
  338. {
  339. int i, buf_pages;
  340. u64 dma = (u64) mem->dma;
  341. buf_pages = min(PAGES_4K_SPANNED(mem->va, mem->size), max_pages);
  342. for (i = 0; i < buf_pages; i++) {
  343. pages[i].lo = cpu_to_le32(dma & 0xFFFFFFFF);
  344. pages[i].hi = cpu_to_le32(upper_32_bits(dma));
  345. dma += PAGE_SIZE_4K;
  346. }
  347. }
  348. static u32 eq_delay_to_mult(u32 usec_delay)
  349. {
  350. #define MAX_INTR_RATE 651042
  351. const u32 round = 10;
  352. u32 multiplier;
  353. if (usec_delay == 0)
  354. multiplier = 0;
  355. else {
  356. u32 interrupt_rate = 1000000 / usec_delay;
  357. if (interrupt_rate == 0)
  358. multiplier = 1023;
  359. else {
  360. multiplier = (MAX_INTR_RATE - interrupt_rate) * round;
  361. multiplier /= interrupt_rate;
  362. multiplier = (multiplier + round / 2) / round;
  363. multiplier = min(multiplier, (u32) 1023);
  364. }
  365. }
  366. return multiplier;
  367. }
  368. struct be_mcc_wrb *wrb_from_mbox(struct be_dma_mem *mbox_mem)
  369. {
  370. return &((struct be_mcc_mailbox *)(mbox_mem->va))->wrb;
  371. }
  372. struct be_mcc_wrb *wrb_from_mccq(struct beiscsi_hba *phba)
  373. {
  374. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  375. struct be_mcc_wrb *wrb;
  376. BUG_ON(atomic_read(&mccq->used) >= mccq->len);
  377. wrb = queue_head_node(mccq);
  378. memset(wrb, 0, sizeof(*wrb));
  379. wrb->tag0 = (mccq->head & 0x000000FF) << 16;
  380. queue_head_inc(mccq);
  381. atomic_inc(&mccq->used);
  382. return wrb;
  383. }
  384. int beiscsi_cmd_eq_create(struct be_ctrl_info *ctrl,
  385. struct be_queue_info *eq, int eq_delay)
  386. {
  387. struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
  388. struct be_cmd_req_eq_create *req = embedded_payload(wrb);
  389. struct be_cmd_resp_eq_create *resp = embedded_payload(wrb);
  390. struct be_dma_mem *q_mem = &eq->dma_mem;
  391. int status;
  392. SE_DEBUG(DBG_LVL_8, "In beiscsi_cmd_eq_create\n");
  393. spin_lock(&ctrl->mbox_lock);
  394. memset(wrb, 0, sizeof(*wrb));
  395. be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
  396. be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_COMMON,
  397. OPCODE_COMMON_EQ_CREATE, sizeof(*req));
  398. req->num_pages = cpu_to_le16(PAGES_4K_SPANNED(q_mem->va, q_mem->size));
  399. AMAP_SET_BITS(struct amap_eq_context, func, req->context,
  400. PCI_FUNC(ctrl->pdev->devfn));
  401. AMAP_SET_BITS(struct amap_eq_context, valid, req->context, 1);
  402. AMAP_SET_BITS(struct amap_eq_context, size, req->context, 0);
  403. AMAP_SET_BITS(struct amap_eq_context, count, req->context,
  404. __ilog2_u32(eq->len / 256));
  405. AMAP_SET_BITS(struct amap_eq_context, delaymult, req->context,
  406. eq_delay_to_mult(eq_delay));
  407. be_dws_cpu_to_le(req->context, sizeof(req->context));
  408. be_cmd_page_addrs_prepare(req->pages, ARRAY_SIZE(req->pages), q_mem);
  409. status = be_mbox_notify(ctrl);
  410. if (!status) {
  411. eq->id = le16_to_cpu(resp->eq_id);
  412. eq->created = true;
  413. }
  414. spin_unlock(&ctrl->mbox_lock);
  415. return status;
  416. }
  417. int be_cmd_fw_initialize(struct be_ctrl_info *ctrl)
  418. {
  419. struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
  420. int status;
  421. u8 *endian_check;
  422. SE_DEBUG(DBG_LVL_8, "In be_cmd_fw_initialize\n");
  423. spin_lock(&ctrl->mbox_lock);
  424. memset(wrb, 0, sizeof(*wrb));
  425. endian_check = (u8 *) wrb;
  426. *endian_check++ = 0xFF;
  427. *endian_check++ = 0x12;
  428. *endian_check++ = 0x34;
  429. *endian_check++ = 0xFF;
  430. *endian_check++ = 0xFF;
  431. *endian_check++ = 0x56;
  432. *endian_check++ = 0x78;
  433. *endian_check++ = 0xFF;
  434. be_dws_cpu_to_le(wrb, sizeof(*wrb));
  435. status = be_mbox_notify(ctrl);
  436. if (status)
  437. SE_DEBUG(DBG_LVL_1, "be_cmd_fw_initialize Failed \n");
  438. spin_unlock(&ctrl->mbox_lock);
  439. return status;
  440. }
  441. int beiscsi_cmd_cq_create(struct be_ctrl_info *ctrl,
  442. struct be_queue_info *cq, struct be_queue_info *eq,
  443. bool sol_evts, bool no_delay, int coalesce_wm)
  444. {
  445. struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
  446. struct be_cmd_req_cq_create *req = embedded_payload(wrb);
  447. struct be_cmd_resp_cq_create *resp = embedded_payload(wrb);
  448. struct be_dma_mem *q_mem = &cq->dma_mem;
  449. void *ctxt = &req->context;
  450. int status;
  451. SE_DEBUG(DBG_LVL_8, "In beiscsi_cmd_cq_create \n");
  452. spin_lock(&ctrl->mbox_lock);
  453. memset(wrb, 0, sizeof(*wrb));
  454. be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
  455. be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_COMMON,
  456. OPCODE_COMMON_CQ_CREATE, sizeof(*req));
  457. if (!q_mem->va)
  458. SE_DEBUG(DBG_LVL_1, "uninitialized q_mem->va\n");
  459. req->num_pages = cpu_to_le16(PAGES_4K_SPANNED(q_mem->va, q_mem->size));
  460. AMAP_SET_BITS(struct amap_cq_context, coalescwm, ctxt, coalesce_wm);
  461. AMAP_SET_BITS(struct amap_cq_context, nodelay, ctxt, no_delay);
  462. AMAP_SET_BITS(struct amap_cq_context, count, ctxt,
  463. __ilog2_u32(cq->len / 256));
  464. AMAP_SET_BITS(struct amap_cq_context, valid, ctxt, 1);
  465. AMAP_SET_BITS(struct amap_cq_context, solevent, ctxt, sol_evts);
  466. AMAP_SET_BITS(struct amap_cq_context, eventable, ctxt, 1);
  467. AMAP_SET_BITS(struct amap_cq_context, eqid, ctxt, eq->id);
  468. AMAP_SET_BITS(struct amap_cq_context, armed, ctxt, 1);
  469. AMAP_SET_BITS(struct amap_cq_context, func, ctxt,
  470. PCI_FUNC(ctrl->pdev->devfn));
  471. be_dws_cpu_to_le(ctxt, sizeof(req->context));
  472. be_cmd_page_addrs_prepare(req->pages, ARRAY_SIZE(req->pages), q_mem);
  473. status = be_mbox_notify(ctrl);
  474. if (!status) {
  475. cq->id = le16_to_cpu(resp->cq_id);
  476. cq->created = true;
  477. } else
  478. SE_DEBUG(DBG_LVL_1, "In be_cmd_cq_create, status=ox%08x \n",
  479. status);
  480. spin_unlock(&ctrl->mbox_lock);
  481. return status;
  482. }
  483. static u32 be_encoded_q_len(int q_len)
  484. {
  485. u32 len_encoded = fls(q_len); /* log2(len) + 1 */
  486. if (len_encoded == 16)
  487. len_encoded = 0;
  488. return len_encoded;
  489. }
  490. int beiscsi_cmd_mccq_create(struct beiscsi_hba *phba,
  491. struct be_queue_info *mccq,
  492. struct be_queue_info *cq)
  493. {
  494. struct be_mcc_wrb *wrb;
  495. struct be_cmd_req_mcc_create *req;
  496. struct be_dma_mem *q_mem = &mccq->dma_mem;
  497. struct be_ctrl_info *ctrl;
  498. void *ctxt;
  499. int status;
  500. spin_lock(&phba->ctrl.mbox_lock);
  501. ctrl = &phba->ctrl;
  502. wrb = wrb_from_mbox(&ctrl->mbox_mem);
  503. req = embedded_payload(wrb);
  504. ctxt = &req->context;
  505. be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
  506. be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_COMMON,
  507. OPCODE_COMMON_MCC_CREATE, sizeof(*req));
  508. req->num_pages = PAGES_4K_SPANNED(q_mem->va, q_mem->size);
  509. AMAP_SET_BITS(struct amap_mcc_context, fid, ctxt,
  510. PCI_FUNC(phba->pcidev->devfn));
  511. AMAP_SET_BITS(struct amap_mcc_context, valid, ctxt, 1);
  512. AMAP_SET_BITS(struct amap_mcc_context, ring_size, ctxt,
  513. be_encoded_q_len(mccq->len));
  514. AMAP_SET_BITS(struct amap_mcc_context, cq_id, ctxt, cq->id);
  515. be_dws_cpu_to_le(ctxt, sizeof(req->context));
  516. be_cmd_page_addrs_prepare(req->pages, ARRAY_SIZE(req->pages), q_mem);
  517. status = be_mbox_notify_wait(phba);
  518. if (!status) {
  519. struct be_cmd_resp_mcc_create *resp = embedded_payload(wrb);
  520. mccq->id = le16_to_cpu(resp->id);
  521. mccq->created = true;
  522. }
  523. spin_unlock(&phba->ctrl.mbox_lock);
  524. return status;
  525. }
  526. int beiscsi_cmd_q_destroy(struct be_ctrl_info *ctrl, struct be_queue_info *q,
  527. int queue_type)
  528. {
  529. struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
  530. struct be_cmd_req_q_destroy *req = embedded_payload(wrb);
  531. u8 subsys = 0, opcode = 0;
  532. int status;
  533. SE_DEBUG(DBG_LVL_8, "In beiscsi_cmd_q_destroy \n");
  534. spin_lock(&ctrl->mbox_lock);
  535. memset(wrb, 0, sizeof(*wrb));
  536. be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
  537. switch (queue_type) {
  538. case QTYPE_EQ:
  539. subsys = CMD_SUBSYSTEM_COMMON;
  540. opcode = OPCODE_COMMON_EQ_DESTROY;
  541. break;
  542. case QTYPE_CQ:
  543. subsys = CMD_SUBSYSTEM_COMMON;
  544. opcode = OPCODE_COMMON_CQ_DESTROY;
  545. break;
  546. case QTYPE_MCCQ:
  547. subsys = CMD_SUBSYSTEM_COMMON;
  548. opcode = OPCODE_COMMON_MCC_DESTROY;
  549. break;
  550. case QTYPE_WRBQ:
  551. subsys = CMD_SUBSYSTEM_ISCSI;
  552. opcode = OPCODE_COMMON_ISCSI_WRBQ_DESTROY;
  553. break;
  554. case QTYPE_DPDUQ:
  555. subsys = CMD_SUBSYSTEM_ISCSI;
  556. opcode = OPCODE_COMMON_ISCSI_DEFQ_DESTROY;
  557. break;
  558. case QTYPE_SGL:
  559. subsys = CMD_SUBSYSTEM_ISCSI;
  560. opcode = OPCODE_COMMON_ISCSI_CFG_REMOVE_SGL_PAGES;
  561. break;
  562. default:
  563. spin_unlock(&ctrl->mbox_lock);
  564. BUG();
  565. return -1;
  566. }
  567. be_cmd_hdr_prepare(&req->hdr, subsys, opcode, sizeof(*req));
  568. if (queue_type != QTYPE_SGL)
  569. req->id = cpu_to_le16(q->id);
  570. status = be_mbox_notify(ctrl);
  571. spin_unlock(&ctrl->mbox_lock);
  572. return status;
  573. }
  574. int be_cmd_create_default_pdu_queue(struct be_ctrl_info *ctrl,
  575. struct be_queue_info *cq,
  576. struct be_queue_info *dq, int length,
  577. int entry_size)
  578. {
  579. struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
  580. struct be_defq_create_req *req = embedded_payload(wrb);
  581. struct be_dma_mem *q_mem = &dq->dma_mem;
  582. void *ctxt = &req->context;
  583. int status;
  584. SE_DEBUG(DBG_LVL_8, "In be_cmd_create_default_pdu_queue\n");
  585. spin_lock(&ctrl->mbox_lock);
  586. memset(wrb, 0, sizeof(*wrb));
  587. be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
  588. be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
  589. OPCODE_COMMON_ISCSI_DEFQ_CREATE, sizeof(*req));
  590. req->num_pages = PAGES_4K_SPANNED(q_mem->va, q_mem->size);
  591. AMAP_SET_BITS(struct amap_be_default_pdu_context, rx_pdid, ctxt, 0);
  592. AMAP_SET_BITS(struct amap_be_default_pdu_context, rx_pdid_valid, ctxt,
  593. 1);
  594. AMAP_SET_BITS(struct amap_be_default_pdu_context, pci_func_id, ctxt,
  595. PCI_FUNC(ctrl->pdev->devfn));
  596. AMAP_SET_BITS(struct amap_be_default_pdu_context, ring_size, ctxt,
  597. be_encoded_q_len(length / sizeof(struct phys_addr)));
  598. AMAP_SET_BITS(struct amap_be_default_pdu_context, default_buffer_size,
  599. ctxt, entry_size);
  600. AMAP_SET_BITS(struct amap_be_default_pdu_context, cq_id_recv, ctxt,
  601. cq->id);
  602. be_dws_cpu_to_le(ctxt, sizeof(req->context));
  603. be_cmd_page_addrs_prepare(req->pages, ARRAY_SIZE(req->pages), q_mem);
  604. status = be_mbox_notify(ctrl);
  605. if (!status) {
  606. struct be_defq_create_resp *resp = embedded_payload(wrb);
  607. dq->id = le16_to_cpu(resp->id);
  608. dq->created = true;
  609. }
  610. spin_unlock(&ctrl->mbox_lock);
  611. return status;
  612. }
  613. int be_cmd_wrbq_create(struct be_ctrl_info *ctrl, struct be_dma_mem *q_mem,
  614. struct be_queue_info *wrbq)
  615. {
  616. struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
  617. struct be_wrbq_create_req *req = embedded_payload(wrb);
  618. struct be_wrbq_create_resp *resp = embedded_payload(wrb);
  619. int status;
  620. spin_lock(&ctrl->mbox_lock);
  621. memset(wrb, 0, sizeof(*wrb));
  622. be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
  623. be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
  624. OPCODE_COMMON_ISCSI_WRBQ_CREATE, sizeof(*req));
  625. req->num_pages = PAGES_4K_SPANNED(q_mem->va, q_mem->size);
  626. be_cmd_page_addrs_prepare(req->pages, ARRAY_SIZE(req->pages), q_mem);
  627. status = be_mbox_notify(ctrl);
  628. if (!status) {
  629. wrbq->id = le16_to_cpu(resp->cid);
  630. wrbq->created = true;
  631. }
  632. spin_unlock(&ctrl->mbox_lock);
  633. return status;
  634. }
  635. int be_cmd_iscsi_post_sgl_pages(struct be_ctrl_info *ctrl,
  636. struct be_dma_mem *q_mem,
  637. u32 page_offset, u32 num_pages)
  638. {
  639. struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
  640. struct be_post_sgl_pages_req *req = embedded_payload(wrb);
  641. int status;
  642. unsigned int curr_pages;
  643. u32 internal_page_offset = 0;
  644. u32 temp_num_pages = num_pages;
  645. if (num_pages == 0xff)
  646. num_pages = 1;
  647. spin_lock(&ctrl->mbox_lock);
  648. do {
  649. memset(wrb, 0, sizeof(*wrb));
  650. be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
  651. be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
  652. OPCODE_COMMON_ISCSI_CFG_POST_SGL_PAGES,
  653. sizeof(*req));
  654. curr_pages = BE_NUMBER_OF_FIELD(struct be_post_sgl_pages_req,
  655. pages);
  656. req->num_pages = min(num_pages, curr_pages);
  657. req->page_offset = page_offset;
  658. be_cmd_page_addrs_prepare(req->pages, req->num_pages, q_mem);
  659. q_mem->dma = q_mem->dma + (req->num_pages * PAGE_SIZE);
  660. internal_page_offset += req->num_pages;
  661. page_offset += req->num_pages;
  662. num_pages -= req->num_pages;
  663. if (temp_num_pages == 0xff)
  664. req->num_pages = temp_num_pages;
  665. status = be_mbox_notify(ctrl);
  666. if (status) {
  667. SE_DEBUG(DBG_LVL_1,
  668. "FW CMD to map iscsi frags failed.\n");
  669. goto error;
  670. }
  671. } while (num_pages > 0);
  672. error:
  673. spin_unlock(&ctrl->mbox_lock);
  674. if (status != 0)
  675. beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
  676. return status;
  677. }