snic_io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright 2014 Cisco Systems, Inc. All rights reserved.
  3. #include <linux/errno.h>
  4. #include <linux/pci.h>
  5. #include <linux/slab.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/workqueue.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/mempool.h>
  10. #include <scsi/scsi_tcq.h>
  11. #include "snic_io.h"
  12. #include "snic.h"
  13. #include "cq_enet_desc.h"
  14. #include "snic_fwint.h"
  15. static void
  16. snic_wq_cmpl_frame_send(struct vnic_wq *wq,
  17. struct cq_desc *cq_desc,
  18. struct vnic_wq_buf *buf,
  19. void *opaque)
  20. {
  21. struct snic *snic = svnic_dev_priv(wq->vdev);
  22. SNIC_BUG_ON(buf->os_buf == NULL);
  23. if (snic_log_level & SNIC_DESC_LOGGING)
  24. SNIC_HOST_INFO(snic->shost,
  25. "Ack received for snic_host_req %p.\n",
  26. buf->os_buf);
  27. SNIC_TRC(snic->shost->host_no, 0, 0,
  28. ((ulong)(buf->os_buf) - sizeof(struct snic_req_info)), 0, 0,
  29. 0);
  30. buf->os_buf = NULL;
  31. }
  32. static int
  33. snic_wq_cmpl_handler_cont(struct vnic_dev *vdev,
  34. struct cq_desc *cq_desc,
  35. u8 type,
  36. u16 q_num,
  37. u16 cmpl_idx,
  38. void *opaque)
  39. {
  40. struct snic *snic = svnic_dev_priv(vdev);
  41. unsigned long flags;
  42. SNIC_BUG_ON(q_num != 0);
  43. spin_lock_irqsave(&snic->wq_lock[q_num], flags);
  44. svnic_wq_service(&snic->wq[q_num],
  45. cq_desc,
  46. cmpl_idx,
  47. snic_wq_cmpl_frame_send,
  48. NULL);
  49. spin_unlock_irqrestore(&snic->wq_lock[q_num], flags);
  50. return 0;
  51. } /* end of snic_cmpl_handler_cont */
  52. int
  53. snic_wq_cmpl_handler(struct snic *snic, int work_to_do)
  54. {
  55. unsigned int work_done = 0;
  56. unsigned int i;
  57. snic->s_stats.misc.last_ack_time = jiffies;
  58. for (i = 0; i < snic->wq_count; i++) {
  59. work_done += svnic_cq_service(&snic->cq[i],
  60. work_to_do,
  61. snic_wq_cmpl_handler_cont,
  62. NULL);
  63. }
  64. return work_done;
  65. } /* end of snic_wq_cmpl_handler */
  66. void
  67. snic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
  68. {
  69. struct snic_host_req *req = buf->os_buf;
  70. struct snic *snic = svnic_dev_priv(wq->vdev);
  71. struct snic_req_info *rqi = NULL;
  72. unsigned long flags;
  73. dma_unmap_single(&snic->pdev->dev, buf->dma_addr, buf->len,
  74. DMA_TO_DEVICE);
  75. rqi = req_to_rqi(req);
  76. spin_lock_irqsave(&snic->spl_cmd_lock, flags);
  77. if (list_empty(&rqi->list)) {
  78. spin_unlock_irqrestore(&snic->spl_cmd_lock, flags);
  79. goto end;
  80. }
  81. SNIC_BUG_ON(rqi->list.next == NULL); /* if not added to spl_cmd_list */
  82. list_del_init(&rqi->list);
  83. spin_unlock_irqrestore(&snic->spl_cmd_lock, flags);
  84. if (rqi->sge_va) {
  85. snic_pci_unmap_rsp_buf(snic, rqi);
  86. kfree((void *)rqi->sge_va);
  87. rqi->sge_va = 0;
  88. }
  89. snic_req_free(snic, rqi);
  90. SNIC_HOST_INFO(snic->shost, "snic_free_wq_buf .. freed.\n");
  91. end:
  92. return;
  93. }
  94. /* Criteria to select work queue in multi queue mode */
  95. static int
  96. snic_select_wq(struct snic *snic)
  97. {
  98. /* No multi queue support for now */
  99. BUILD_BUG_ON(SNIC_WQ_MAX > 1);
  100. return 0;
  101. }
  102. static int
  103. snic_wqdesc_avail(struct snic *snic, int q_num, int req_type)
  104. {
  105. int nr_wqdesc = snic->config.wq_enet_desc_count;
  106. if (q_num > 0) {
  107. /*
  108. * Multi Queue case, additional care is required.
  109. * Per WQ active requests need to be maintained.
  110. */
  111. SNIC_HOST_INFO(snic->shost, "desc_avail: Multi Queue case.\n");
  112. SNIC_BUG_ON(q_num > 0);
  113. return -1;
  114. }
  115. nr_wqdesc -= atomic64_read(&snic->s_stats.fw.actv_reqs);
  116. return ((req_type == SNIC_REQ_HBA_RESET) ? nr_wqdesc : nr_wqdesc - 1);
  117. }
  118. int
  119. snic_queue_wq_desc(struct snic *snic, void *os_buf, u16 len)
  120. {
  121. dma_addr_t pa = 0;
  122. unsigned long flags;
  123. struct snic_fw_stats *fwstats = &snic->s_stats.fw;
  124. struct snic_host_req *req = (struct snic_host_req *) os_buf;
  125. long act_reqs;
  126. long desc_avail = 0;
  127. int q_num = 0;
  128. snic_print_desc(__func__, os_buf, len);
  129. /* Map request buffer */
  130. pa = dma_map_single(&snic->pdev->dev, os_buf, len, DMA_TO_DEVICE);
  131. if (dma_mapping_error(&snic->pdev->dev, pa)) {
  132. SNIC_HOST_ERR(snic->shost, "qdesc: PCI DMA Mapping Fail.\n");
  133. return -ENOMEM;
  134. }
  135. req->req_pa = (ulong)pa;
  136. q_num = snic_select_wq(snic);
  137. spin_lock_irqsave(&snic->wq_lock[q_num], flags);
  138. desc_avail = snic_wqdesc_avail(snic, q_num, req->hdr.type);
  139. if (desc_avail <= 0) {
  140. dma_unmap_single(&snic->pdev->dev, pa, len, DMA_TO_DEVICE);
  141. req->req_pa = 0;
  142. spin_unlock_irqrestore(&snic->wq_lock[q_num], flags);
  143. atomic64_inc(&snic->s_stats.misc.wq_alloc_fail);
  144. SNIC_DBG("host = %d, WQ is Full\n", snic->shost->host_no);
  145. return -ENOMEM;
  146. }
  147. snic_queue_wq_eth_desc(&snic->wq[q_num], os_buf, pa, len, 0, 0, 1);
  148. /*
  149. * Update stats
  150. * note: when multi queue enabled, fw actv_reqs should be per queue.
  151. */
  152. act_reqs = atomic64_inc_return(&fwstats->actv_reqs);
  153. spin_unlock_irqrestore(&snic->wq_lock[q_num], flags);
  154. if (act_reqs > atomic64_read(&fwstats->max_actv_reqs))
  155. atomic64_set(&fwstats->max_actv_reqs, act_reqs);
  156. return 0;
  157. } /* end of snic_queue_wq_desc() */
  158. /*
  159. * snic_handle_untagged_req: Adds snic specific requests to spl_cmd_list.
  160. * Purpose : Used during driver unload to clean up the requests.
  161. */
  162. void
  163. snic_handle_untagged_req(struct snic *snic, struct snic_req_info *rqi)
  164. {
  165. unsigned long flags;
  166. INIT_LIST_HEAD(&rqi->list);
  167. spin_lock_irqsave(&snic->spl_cmd_lock, flags);
  168. list_add_tail(&rqi->list, &snic->spl_cmd_list);
  169. spin_unlock_irqrestore(&snic->spl_cmd_lock, flags);
  170. }
  171. /*
  172. * snic_req_init:
  173. * Allocates snic_req_info + snic_host_req + sgl data, and initializes.
  174. */
  175. struct snic_req_info *
  176. snic_req_init(struct snic *snic, int sg_cnt)
  177. {
  178. u8 typ;
  179. struct snic_req_info *rqi = NULL;
  180. typ = (sg_cnt <= SNIC_REQ_CACHE_DFLT_SGL) ?
  181. SNIC_REQ_CACHE_DFLT_SGL : SNIC_REQ_CACHE_MAX_SGL;
  182. rqi = mempool_alloc(snic->req_pool[typ], GFP_ATOMIC);
  183. if (!rqi) {
  184. atomic64_inc(&snic->s_stats.io.alloc_fail);
  185. SNIC_HOST_ERR(snic->shost,
  186. "Failed to allocate memory from snic req pool id = %d\n",
  187. typ);
  188. return rqi;
  189. }
  190. memset(rqi, 0, sizeof(*rqi));
  191. rqi->rq_pool_type = typ;
  192. rqi->start_time = jiffies;
  193. rqi->req = (struct snic_host_req *) (rqi + 1);
  194. rqi->req_len = sizeof(struct snic_host_req);
  195. rqi->snic = snic;
  196. rqi->req = (struct snic_host_req *)(rqi + 1);
  197. if (sg_cnt == 0)
  198. goto end;
  199. rqi->req_len += (sg_cnt * sizeof(struct snic_sg_desc));
  200. if (sg_cnt > atomic64_read(&snic->s_stats.io.max_sgl))
  201. atomic64_set(&snic->s_stats.io.max_sgl, sg_cnt);
  202. SNIC_BUG_ON(sg_cnt > SNIC_MAX_SG_DESC_CNT);
  203. atomic64_inc(&snic->s_stats.io.sgl_cnt[sg_cnt - 1]);
  204. end:
  205. memset(rqi->req, 0, rqi->req_len);
  206. /* pre initialization of init_ctx to support req_to_rqi */
  207. rqi->req->hdr.init_ctx = (ulong) rqi;
  208. SNIC_SCSI_DBG(snic->shost, "Req_alloc:rqi = %p allocatd.\n", rqi);
  209. return rqi;
  210. } /* end of snic_req_init */
  211. /*
  212. * snic_abort_req_init : Inits abort request.
  213. */
  214. struct snic_host_req *
  215. snic_abort_req_init(struct snic *snic, struct snic_req_info *rqi)
  216. {
  217. struct snic_host_req *req = NULL;
  218. SNIC_BUG_ON(!rqi);
  219. /* If abort to be issued second time, then reuse */
  220. if (rqi->abort_req)
  221. return rqi->abort_req;
  222. req = mempool_alloc(snic->req_pool[SNIC_REQ_TM_CACHE], GFP_ATOMIC);
  223. if (!req) {
  224. SNIC_HOST_ERR(snic->shost, "abts:Failed to alloc tm req.\n");
  225. WARN_ON_ONCE(1);
  226. return NULL;
  227. }
  228. rqi->abort_req = req;
  229. memset(req, 0, sizeof(struct snic_host_req));
  230. /* pre initialization of init_ctx to support req_to_rqi */
  231. req->hdr.init_ctx = (ulong) rqi;
  232. return req;
  233. } /* end of snic_abort_req_init */
  234. /*
  235. * snic_dr_req_init : Inits device reset req
  236. */
  237. struct snic_host_req *
  238. snic_dr_req_init(struct snic *snic, struct snic_req_info *rqi)
  239. {
  240. struct snic_host_req *req = NULL;
  241. SNIC_BUG_ON(!rqi);
  242. req = mempool_alloc(snic->req_pool[SNIC_REQ_TM_CACHE], GFP_ATOMIC);
  243. if (!req) {
  244. SNIC_HOST_ERR(snic->shost, "dr:Failed to alloc tm req.\n");
  245. WARN_ON_ONCE(1);
  246. return NULL;
  247. }
  248. SNIC_BUG_ON(rqi->dr_req != NULL);
  249. rqi->dr_req = req;
  250. memset(req, 0, sizeof(struct snic_host_req));
  251. /* pre initialization of init_ctx to support req_to_rqi */
  252. req->hdr.init_ctx = (ulong) rqi;
  253. return req;
  254. } /* end of snic_dr_req_init */
  255. /* frees snic_req_info and snic_host_req */
  256. void
  257. snic_req_free(struct snic *snic, struct snic_req_info *rqi)
  258. {
  259. SNIC_BUG_ON(rqi->req == rqi->abort_req);
  260. SNIC_BUG_ON(rqi->req == rqi->dr_req);
  261. SNIC_BUG_ON(rqi->sge_va != 0);
  262. SNIC_SCSI_DBG(snic->shost,
  263. "Req_free:rqi %p:ioreq %p:abt %p:dr %p\n",
  264. rqi, rqi->req, rqi->abort_req, rqi->dr_req);
  265. if (rqi->abort_req) {
  266. if (rqi->abort_req->req_pa)
  267. dma_unmap_single(&snic->pdev->dev,
  268. rqi->abort_req->req_pa,
  269. sizeof(struct snic_host_req),
  270. DMA_TO_DEVICE);
  271. mempool_free(rqi->abort_req, snic->req_pool[SNIC_REQ_TM_CACHE]);
  272. }
  273. if (rqi->dr_req) {
  274. if (rqi->dr_req->req_pa)
  275. dma_unmap_single(&snic->pdev->dev,
  276. rqi->dr_req->req_pa,
  277. sizeof(struct snic_host_req),
  278. DMA_TO_DEVICE);
  279. mempool_free(rqi->dr_req, snic->req_pool[SNIC_REQ_TM_CACHE]);
  280. }
  281. if (rqi->req->req_pa)
  282. dma_unmap_single(&snic->pdev->dev,
  283. rqi->req->req_pa,
  284. rqi->req_len,
  285. DMA_TO_DEVICE);
  286. mempool_free(rqi, snic->req_pool[rqi->rq_pool_type]);
  287. }
  288. void
  289. snic_pci_unmap_rsp_buf(struct snic *snic, struct snic_req_info *rqi)
  290. {
  291. struct snic_sg_desc *sgd;
  292. sgd = req_to_sgl(rqi_to_req(rqi));
  293. SNIC_BUG_ON(sgd[0].addr == 0);
  294. dma_unmap_single(&snic->pdev->dev,
  295. le64_to_cpu(sgd[0].addr),
  296. le32_to_cpu(sgd[0].len),
  297. DMA_FROM_DEVICE);
  298. }
  299. /*
  300. * snic_free_all_untagged_reqs: Walks through untagged reqs and frees them.
  301. */
  302. void
  303. snic_free_all_untagged_reqs(struct snic *snic)
  304. {
  305. struct snic_req_info *rqi;
  306. struct list_head *cur, *nxt;
  307. unsigned long flags;
  308. spin_lock_irqsave(&snic->spl_cmd_lock, flags);
  309. list_for_each_safe(cur, nxt, &snic->spl_cmd_list) {
  310. rqi = list_entry(cur, struct snic_req_info, list);
  311. list_del_init(&rqi->list);
  312. if (rqi->sge_va) {
  313. snic_pci_unmap_rsp_buf(snic, rqi);
  314. kfree((void *)rqi->sge_va);
  315. rqi->sge_va = 0;
  316. }
  317. snic_req_free(snic, rqi);
  318. }
  319. spin_unlock_irqrestore(&snic->spl_cmd_lock, flags);
  320. }
  321. /*
  322. * snic_release_untagged_req : Unlinks the untagged req and frees it.
  323. */
  324. void
  325. snic_release_untagged_req(struct snic *snic, struct snic_req_info *rqi)
  326. {
  327. unsigned long flags;
  328. spin_lock_irqsave(&snic->snic_lock, flags);
  329. if (snic->in_remove) {
  330. spin_unlock_irqrestore(&snic->snic_lock, flags);
  331. goto end;
  332. }
  333. spin_unlock_irqrestore(&snic->snic_lock, flags);
  334. spin_lock_irqsave(&snic->spl_cmd_lock, flags);
  335. if (list_empty(&rqi->list)) {
  336. spin_unlock_irqrestore(&snic->spl_cmd_lock, flags);
  337. goto end;
  338. }
  339. list_del_init(&rqi->list);
  340. spin_unlock_irqrestore(&snic->spl_cmd_lock, flags);
  341. snic_req_free(snic, rqi);
  342. end:
  343. return;
  344. }
  345. /* dump buf in hex fmt */
  346. void
  347. snic_hex_dump(char *pfx, char *data, int len)
  348. {
  349. SNIC_INFO("%s Dumping Data of Len = %d\n", pfx, len);
  350. print_hex_dump_bytes(pfx, DUMP_PREFIX_NONE, data, len);
  351. }
  352. #define LINE_BUFSZ 128 /* for snic_print_desc fn */
  353. static void
  354. snic_dump_desc(const char *fn, char *os_buf, int len)
  355. {
  356. struct snic_host_req *req = (struct snic_host_req *) os_buf;
  357. struct snic_fw_req *fwreq = (struct snic_fw_req *) os_buf;
  358. struct snic_req_info *rqi = NULL;
  359. char line[LINE_BUFSZ] = { '\0' };
  360. char *cmd_str = NULL;
  361. if (req->hdr.type >= SNIC_RSP_REPORT_TGTS_CMPL)
  362. rqi = (struct snic_req_info *) fwreq->hdr.init_ctx;
  363. else
  364. rqi = (struct snic_req_info *) req->hdr.init_ctx;
  365. SNIC_BUG_ON(rqi == NULL || rqi->req == NULL);
  366. switch (req->hdr.type) {
  367. case SNIC_REQ_REPORT_TGTS:
  368. cmd_str = "report-tgt : ";
  369. snprintf(line, LINE_BUFSZ, "SNIC_REQ_REPORT_TGTS :");
  370. break;
  371. case SNIC_REQ_ICMND:
  372. cmd_str = "icmnd : ";
  373. snprintf(line, LINE_BUFSZ, "SNIC_REQ_ICMND : 0x%x :",
  374. req->u.icmnd.cdb[0]);
  375. break;
  376. case SNIC_REQ_ITMF:
  377. cmd_str = "itmf : ";
  378. snprintf(line, LINE_BUFSZ, "SNIC_REQ_ITMF :");
  379. break;
  380. case SNIC_REQ_HBA_RESET:
  381. cmd_str = "hba reset :";
  382. snprintf(line, LINE_BUFSZ, "SNIC_REQ_HBA_RESET :");
  383. break;
  384. case SNIC_REQ_EXCH_VER:
  385. cmd_str = "exch ver : ";
  386. snprintf(line, LINE_BUFSZ, "SNIC_REQ_EXCH_VER :");
  387. break;
  388. case SNIC_REQ_TGT_INFO:
  389. cmd_str = "tgt info : ";
  390. break;
  391. case SNIC_RSP_REPORT_TGTS_CMPL:
  392. cmd_str = "report tgt cmpl : ";
  393. snprintf(line, LINE_BUFSZ, "SNIC_RSP_REPORT_TGTS_CMPL :");
  394. break;
  395. case SNIC_RSP_ICMND_CMPL:
  396. cmd_str = "icmnd_cmpl : ";
  397. snprintf(line, LINE_BUFSZ, "SNIC_RSP_ICMND_CMPL : 0x%x :",
  398. rqi->req->u.icmnd.cdb[0]);
  399. break;
  400. case SNIC_RSP_ITMF_CMPL:
  401. cmd_str = "itmf_cmpl : ";
  402. snprintf(line, LINE_BUFSZ, "SNIC_RSP_ITMF_CMPL :");
  403. break;
  404. case SNIC_RSP_HBA_RESET_CMPL:
  405. cmd_str = "hba_reset_cmpl : ";
  406. snprintf(line, LINE_BUFSZ, "SNIC_RSP_HBA_RESET_CMPL :");
  407. break;
  408. case SNIC_RSP_EXCH_VER_CMPL:
  409. cmd_str = "exch_ver_cmpl : ";
  410. snprintf(line, LINE_BUFSZ, "SNIC_RSP_EXCH_VER_CMPL :");
  411. break;
  412. case SNIC_MSG_ACK:
  413. cmd_str = "msg ack : ";
  414. snprintf(line, LINE_BUFSZ, "SNIC_MSG_ACK :");
  415. break;
  416. case SNIC_MSG_ASYNC_EVNOTIFY:
  417. cmd_str = "async notify : ";
  418. snprintf(line, LINE_BUFSZ, "SNIC_MSG_ASYNC_EVNOTIFY :");
  419. break;
  420. default:
  421. cmd_str = "unknown : ";
  422. SNIC_BUG_ON(1);
  423. break;
  424. }
  425. SNIC_INFO("%s:%s >>cmndid=%x:sg_cnt = %x:status = %x:ctx = %lx.\n",
  426. fn, line, req->hdr.cmnd_id, req->hdr.sg_cnt, req->hdr.status,
  427. req->hdr.init_ctx);
  428. /* Enable it, to dump byte stream */
  429. if (snic_log_level & 0x20)
  430. snic_hex_dump(cmd_str, os_buf, len);
  431. } /* end of __snic_print_desc */
  432. void
  433. snic_print_desc(const char *fn, char *os_buf, int len)
  434. {
  435. if (snic_log_level & SNIC_DESC_LOGGING)
  436. snic_dump_desc(fn, os_buf, len);
  437. }
  438. void
  439. snic_calc_io_process_time(struct snic *snic, struct snic_req_info *rqi)
  440. {
  441. u64 duration;
  442. duration = jiffies - rqi->start_time;
  443. if (duration > atomic64_read(&snic->s_stats.io.max_time))
  444. atomic64_set(&snic->s_stats.io.max_time, duration);
  445. }