snic_io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. pci_unmap_single(snic->pdev, buf->dma_addr, buf->len, PCI_DMA_TODEVICE);
  45. buf->os_buf = NULL;
  46. }
  47. static int
  48. snic_wq_cmpl_handler_cont(struct vnic_dev *vdev,
  49. struct cq_desc *cq_desc,
  50. u8 type,
  51. u16 q_num,
  52. u16 cmpl_idx,
  53. void *opaque)
  54. {
  55. struct snic *snic = svnic_dev_priv(vdev);
  56. unsigned long flags;
  57. SNIC_BUG_ON(q_num != 0);
  58. spin_lock_irqsave(&snic->wq_lock[q_num], flags);
  59. svnic_wq_service(&snic->wq[q_num],
  60. cq_desc,
  61. cmpl_idx,
  62. snic_wq_cmpl_frame_send,
  63. NULL);
  64. spin_unlock_irqrestore(&snic->wq_lock[q_num], flags);
  65. return 0;
  66. } /* end of snic_cmpl_handler_cont */
  67. int
  68. snic_wq_cmpl_handler(struct snic *snic, int work_to_do)
  69. {
  70. unsigned int work_done = 0;
  71. unsigned int i;
  72. snic->s_stats.misc.last_ack_time = jiffies;
  73. for (i = 0; i < snic->wq_count; i++) {
  74. work_done += svnic_cq_service(&snic->cq[i],
  75. work_to_do,
  76. snic_wq_cmpl_handler_cont,
  77. NULL);
  78. }
  79. return work_done;
  80. } /* end of snic_wq_cmpl_handler */
  81. void
  82. snic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
  83. {
  84. struct snic_host_req *req = buf->os_buf;
  85. struct snic *snic = svnic_dev_priv(wq->vdev);
  86. struct snic_req_info *rqi = NULL;
  87. unsigned long flags;
  88. pci_unmap_single(snic->pdev, buf->dma_addr, buf->len, PCI_DMA_TODEVICE);
  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. int
  117. snic_queue_wq_desc(struct snic *snic, void *os_buf, u16 len)
  118. {
  119. dma_addr_t pa = 0;
  120. unsigned long flags;
  121. struct snic_fw_stats *fwstats = &snic->s_stats.fw;
  122. long act_reqs;
  123. int q_num = 0;
  124. snic_print_desc(__func__, os_buf, len);
  125. /* Map request buffer */
  126. pa = pci_map_single(snic->pdev, os_buf, len, PCI_DMA_TODEVICE);
  127. if (pci_dma_mapping_error(snic->pdev, pa)) {
  128. SNIC_HOST_ERR(snic->shost, "qdesc: PCI DMA Mapping Fail.\n");
  129. return -ENOMEM;
  130. }
  131. q_num = snic_select_wq(snic);
  132. spin_lock_irqsave(&snic->wq_lock[q_num], flags);
  133. if (!svnic_wq_desc_avail(snic->wq)) {
  134. pci_unmap_single(snic->pdev, pa, len, PCI_DMA_TODEVICE);
  135. spin_unlock_irqrestore(&snic->wq_lock[q_num], flags);
  136. atomic64_inc(&snic->s_stats.misc.wq_alloc_fail);
  137. SNIC_DBG("host = %d, WQ is Full\n", snic->shost->host_no);
  138. return -ENOMEM;
  139. }
  140. snic_queue_wq_eth_desc(&snic->wq[q_num], os_buf, pa, len, 0, 0, 1);
  141. spin_unlock_irqrestore(&snic->wq_lock[q_num], flags);
  142. /* Update stats */
  143. act_reqs = atomic64_inc_return(&fwstats->actv_reqs);
  144. if (act_reqs > atomic64_read(&fwstats->max_actv_reqs))
  145. atomic64_set(&fwstats->max_actv_reqs, act_reqs);
  146. return 0;
  147. } /* end of snic_queue_wq_desc() */
  148. /*
  149. * snic_handle_untagged_req: Adds snic specific requests to spl_cmd_list.
  150. * Purpose : Used during driver unload to clean up the requests.
  151. */
  152. void
  153. snic_handle_untagged_req(struct snic *snic, struct snic_req_info *rqi)
  154. {
  155. unsigned long flags;
  156. INIT_LIST_HEAD(&rqi->list);
  157. spin_lock_irqsave(&snic->spl_cmd_lock, flags);
  158. list_add_tail(&rqi->list, &snic->spl_cmd_list);
  159. spin_unlock_irqrestore(&snic->spl_cmd_lock, flags);
  160. }
  161. /*
  162. * snic_req_init:
  163. * Allocates snic_req_info + snic_host_req + sgl data, and initializes.
  164. */
  165. struct snic_req_info *
  166. snic_req_init(struct snic *snic, int sg_cnt)
  167. {
  168. u8 typ;
  169. struct snic_req_info *rqi = NULL;
  170. typ = (sg_cnt <= SNIC_REQ_CACHE_DFLT_SGL) ?
  171. SNIC_REQ_CACHE_DFLT_SGL : SNIC_REQ_CACHE_MAX_SGL;
  172. rqi = mempool_alloc(snic->req_pool[typ], GFP_ATOMIC);
  173. if (!rqi) {
  174. atomic64_inc(&snic->s_stats.io.alloc_fail);
  175. SNIC_HOST_ERR(snic->shost,
  176. "Failed to allocate memory from snic req pool id = %d\n",
  177. typ);
  178. return rqi;
  179. }
  180. memset(rqi, 0, sizeof(*rqi));
  181. rqi->rq_pool_type = typ;
  182. rqi->start_time = jiffies;
  183. rqi->req = (struct snic_host_req *) (rqi + 1);
  184. rqi->req_len = sizeof(struct snic_host_req);
  185. rqi->snic = snic;
  186. rqi->req = (struct snic_host_req *)(rqi + 1);
  187. if (sg_cnt == 0)
  188. goto end;
  189. rqi->req_len += (sg_cnt * sizeof(struct snic_sg_desc));
  190. if (sg_cnt > atomic64_read(&snic->s_stats.io.max_sgl))
  191. atomic64_set(&snic->s_stats.io.max_sgl, sg_cnt);
  192. SNIC_BUG_ON(sg_cnt > SNIC_MAX_SG_DESC_CNT);
  193. atomic64_inc(&snic->s_stats.io.sgl_cnt[sg_cnt - 1]);
  194. end:
  195. memset(rqi->req, 0, rqi->req_len);
  196. /* pre initialization of init_ctx to support req_to_rqi */
  197. rqi->req->hdr.init_ctx = (ulong) rqi;
  198. SNIC_SCSI_DBG(snic->shost, "Req_alloc:rqi = %p allocatd.\n", rqi);
  199. return rqi;
  200. } /* end of snic_req_init */
  201. /*
  202. * snic_abort_req_init : Inits abort request.
  203. */
  204. struct snic_host_req *
  205. snic_abort_req_init(struct snic *snic, struct snic_req_info *rqi)
  206. {
  207. struct snic_host_req *req = NULL;
  208. SNIC_BUG_ON(!rqi);
  209. /* If abort to be issued second time, then reuse */
  210. if (rqi->abort_req)
  211. return rqi->abort_req;
  212. req = mempool_alloc(snic->req_pool[SNIC_REQ_TM_CACHE], GFP_ATOMIC);
  213. if (!req) {
  214. SNIC_HOST_ERR(snic->shost, "abts:Failed to alloc tm req.\n");
  215. WARN_ON_ONCE(1);
  216. return NULL;
  217. }
  218. rqi->abort_req = req;
  219. memset(req, 0, sizeof(struct snic_host_req));
  220. /* pre initialization of init_ctx to support req_to_rqi */
  221. req->hdr.init_ctx = (ulong) rqi;
  222. return req;
  223. } /* end of snic_abort_req_init */
  224. /*
  225. * snic_dr_req_init : Inits device reset req
  226. */
  227. struct snic_host_req *
  228. snic_dr_req_init(struct snic *snic, struct snic_req_info *rqi)
  229. {
  230. struct snic_host_req *req = NULL;
  231. SNIC_BUG_ON(!rqi);
  232. req = mempool_alloc(snic->req_pool[SNIC_REQ_TM_CACHE], GFP_ATOMIC);
  233. if (!req) {
  234. SNIC_HOST_ERR(snic->shost, "dr:Failed to alloc tm req.\n");
  235. WARN_ON_ONCE(1);
  236. return NULL;
  237. }
  238. SNIC_BUG_ON(rqi->dr_req != NULL);
  239. rqi->dr_req = req;
  240. memset(req, 0, sizeof(struct snic_host_req));
  241. /* pre initialization of init_ctx to support req_to_rqi */
  242. req->hdr.init_ctx = (ulong) rqi;
  243. return req;
  244. } /* end of snic_dr_req_init */
  245. /* frees snic_req_info and snic_host_req */
  246. void
  247. snic_req_free(struct snic *snic, struct snic_req_info *rqi)
  248. {
  249. SNIC_BUG_ON(rqi->req == rqi->abort_req);
  250. SNIC_BUG_ON(rqi->req == rqi->dr_req);
  251. SNIC_BUG_ON(rqi->sge_va != 0);
  252. SNIC_SCSI_DBG(snic->shost,
  253. "Req_free:rqi %p:ioreq %p:abt %p:dr %p\n",
  254. rqi, rqi->req, rqi->abort_req, rqi->dr_req);
  255. if (rqi->abort_req)
  256. mempool_free(rqi->abort_req, snic->req_pool[SNIC_REQ_TM_CACHE]);
  257. if (rqi->dr_req)
  258. mempool_free(rqi->dr_req, snic->req_pool[SNIC_REQ_TM_CACHE]);
  259. mempool_free(rqi, snic->req_pool[rqi->rq_pool_type]);
  260. }
  261. void
  262. snic_pci_unmap_rsp_buf(struct snic *snic, struct snic_req_info *rqi)
  263. {
  264. struct snic_sg_desc *sgd;
  265. sgd = req_to_sgl(rqi_to_req(rqi));
  266. SNIC_BUG_ON(sgd[0].addr == 0);
  267. pci_unmap_single(snic->pdev,
  268. le64_to_cpu(sgd[0].addr),
  269. le32_to_cpu(sgd[0].len),
  270. PCI_DMA_FROMDEVICE);
  271. }
  272. /*
  273. * snic_free_all_untagged_reqs: Walks through untagged reqs and frees them.
  274. */
  275. void
  276. snic_free_all_untagged_reqs(struct snic *snic)
  277. {
  278. struct snic_req_info *rqi;
  279. struct list_head *cur, *nxt;
  280. unsigned long flags;
  281. spin_lock_irqsave(&snic->spl_cmd_lock, flags);
  282. list_for_each_safe(cur, nxt, &snic->spl_cmd_list) {
  283. rqi = list_entry(cur, struct snic_req_info, list);
  284. list_del_init(&rqi->list);
  285. if (rqi->sge_va) {
  286. snic_pci_unmap_rsp_buf(snic, rqi);
  287. kfree((void *)rqi->sge_va);
  288. rqi->sge_va = 0;
  289. }
  290. snic_req_free(snic, rqi);
  291. }
  292. spin_unlock_irqrestore(&snic->spl_cmd_lock, flags);
  293. }
  294. /*
  295. * snic_release_untagged_req : Unlinks the untagged req and frees it.
  296. */
  297. void
  298. snic_release_untagged_req(struct snic *snic, struct snic_req_info *rqi)
  299. {
  300. unsigned long flags;
  301. spin_lock_irqsave(&snic->snic_lock, flags);
  302. if (snic->in_remove) {
  303. spin_unlock_irqrestore(&snic->snic_lock, flags);
  304. goto end;
  305. }
  306. spin_unlock_irqrestore(&snic->snic_lock, flags);
  307. spin_lock_irqsave(&snic->spl_cmd_lock, flags);
  308. if (list_empty(&rqi->list)) {
  309. spin_unlock_irqrestore(&snic->spl_cmd_lock, flags);
  310. goto end;
  311. }
  312. list_del_init(&rqi->list);
  313. spin_unlock_irqrestore(&snic->spl_cmd_lock, flags);
  314. snic_req_free(snic, rqi);
  315. end:
  316. return;
  317. }
  318. /* dump buf in hex fmt */
  319. void
  320. snic_hex_dump(char *pfx, char *data, int len)
  321. {
  322. SNIC_INFO("%s Dumping Data of Len = %d\n", pfx, len);
  323. print_hex_dump_bytes(pfx, DUMP_PREFIX_NONE, data, len);
  324. }
  325. #define LINE_BUFSZ 128 /* for snic_print_desc fn */
  326. static void
  327. snic_dump_desc(const char *fn, char *os_buf, int len)
  328. {
  329. struct snic_host_req *req = (struct snic_host_req *) os_buf;
  330. struct snic_fw_req *fwreq = (struct snic_fw_req *) os_buf;
  331. struct snic_req_info *rqi = NULL;
  332. char line[LINE_BUFSZ] = { '\0' };
  333. char *cmd_str = NULL;
  334. if (req->hdr.type >= SNIC_RSP_REPORT_TGTS_CMPL)
  335. rqi = (struct snic_req_info *) fwreq->hdr.init_ctx;
  336. else
  337. rqi = (struct snic_req_info *) req->hdr.init_ctx;
  338. SNIC_BUG_ON(rqi == NULL || rqi->req == NULL);
  339. switch (req->hdr.type) {
  340. case SNIC_REQ_REPORT_TGTS:
  341. cmd_str = "report-tgt : ";
  342. snprintf(line, LINE_BUFSZ, "SNIC_REQ_REPORT_TGTS :");
  343. break;
  344. case SNIC_REQ_ICMND:
  345. cmd_str = "icmnd : ";
  346. snprintf(line, LINE_BUFSZ, "SNIC_REQ_ICMND : 0x%x :",
  347. req->u.icmnd.cdb[0]);
  348. break;
  349. case SNIC_REQ_ITMF:
  350. cmd_str = "itmf : ";
  351. snprintf(line, LINE_BUFSZ, "SNIC_REQ_ITMF :");
  352. break;
  353. case SNIC_REQ_HBA_RESET:
  354. cmd_str = "hba reset :";
  355. snprintf(line, LINE_BUFSZ, "SNIC_REQ_HBA_RESET :");
  356. break;
  357. case SNIC_REQ_EXCH_VER:
  358. cmd_str = "exch ver : ";
  359. snprintf(line, LINE_BUFSZ, "SNIC_REQ_EXCH_VER :");
  360. break;
  361. case SNIC_REQ_TGT_INFO:
  362. cmd_str = "tgt info : ";
  363. break;
  364. case SNIC_RSP_REPORT_TGTS_CMPL:
  365. cmd_str = "report tgt cmpl : ";
  366. snprintf(line, LINE_BUFSZ, "SNIC_RSP_REPORT_TGTS_CMPL :");
  367. break;
  368. case SNIC_RSP_ICMND_CMPL:
  369. cmd_str = "icmnd_cmpl : ";
  370. snprintf(line, LINE_BUFSZ, "SNIC_RSP_ICMND_CMPL : 0x%x :",
  371. rqi->req->u.icmnd.cdb[0]);
  372. break;
  373. case SNIC_RSP_ITMF_CMPL:
  374. cmd_str = "itmf_cmpl : ";
  375. snprintf(line, LINE_BUFSZ, "SNIC_RSP_ITMF_CMPL :");
  376. break;
  377. case SNIC_RSP_HBA_RESET_CMPL:
  378. cmd_str = "hba_reset_cmpl : ";
  379. snprintf(line, LINE_BUFSZ, "SNIC_RSP_HBA_RESET_CMPL :");
  380. break;
  381. case SNIC_RSP_EXCH_VER_CMPL:
  382. cmd_str = "exch_ver_cmpl : ";
  383. snprintf(line, LINE_BUFSZ, "SNIC_RSP_EXCH_VER_CMPL :");
  384. break;
  385. case SNIC_MSG_ACK:
  386. cmd_str = "msg ack : ";
  387. snprintf(line, LINE_BUFSZ, "SNIC_MSG_ACK :");
  388. break;
  389. case SNIC_MSG_ASYNC_EVNOTIFY:
  390. cmd_str = "async notify : ";
  391. snprintf(line, LINE_BUFSZ, "SNIC_MSG_ASYNC_EVNOTIFY :");
  392. break;
  393. default:
  394. cmd_str = "unknown : ";
  395. SNIC_BUG_ON(1);
  396. break;
  397. }
  398. SNIC_INFO("%s:%s >>cmndid=%x:sg_cnt = %x:status = %x:ctx = %lx.\n",
  399. fn, line, req->hdr.cmnd_id, req->hdr.sg_cnt, req->hdr.status,
  400. req->hdr.init_ctx);
  401. /* Enable it, to dump byte stream */
  402. if (snic_log_level & 0x20)
  403. snic_hex_dump(cmd_str, os_buf, len);
  404. } /* end of __snic_print_desc */
  405. void
  406. snic_print_desc(const char *fn, char *os_buf, int len)
  407. {
  408. if (snic_log_level & SNIC_DESC_LOGGING)
  409. snic_dump_desc(fn, os_buf, len);
  410. }
  411. void
  412. snic_calc_io_process_time(struct snic *snic, struct snic_req_info *rqi)
  413. {
  414. u64 duration;
  415. duration = jiffies - rqi->start_time;
  416. if (duration > atomic64_read(&snic->s_stats.io.max_time))
  417. atomic64_set(&snic->s_stats.io.max_time, duration);
  418. }