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