snic_io.c 14 KB

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