lpfc_bsg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2009 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or *
  9. * modify it under the terms of version 2 of the GNU General *
  10. * Public License as published by the Free Software Foundation. *
  11. * This program is distributed in the hope that it will be useful. *
  12. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  13. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  14. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  15. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  16. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  17. * more details, a copy of which can be found in the file COPYING *
  18. * included with this package. *
  19. *******************************************************************/
  20. #include <linux/interrupt.h>
  21. #include <linux/mempool.h>
  22. #include <linux/pci.h>
  23. #include <scsi/scsi.h>
  24. #include <scsi/scsi_host.h>
  25. #include <scsi/scsi_transport_fc.h>
  26. #include <scsi/scsi_bsg_fc.h>
  27. #include "lpfc_hw4.h"
  28. #include "lpfc_hw.h"
  29. #include "lpfc_sli.h"
  30. #include "lpfc_sli4.h"
  31. #include "lpfc_nl.h"
  32. #include "lpfc_disc.h"
  33. #include "lpfc_scsi.h"
  34. #include "lpfc.h"
  35. #include "lpfc_logmsg.h"
  36. #include "lpfc_crtn.h"
  37. #include "lpfc_vport.h"
  38. #include "lpfc_version.h"
  39. /**
  40. * lpfc_bsg_rport_ct - send a CT command from a bsg request
  41. * @job: fc_bsg_job to handle
  42. */
  43. static int
  44. lpfc_bsg_rport_ct(struct fc_bsg_job *job)
  45. {
  46. struct Scsi_Host *shost = job->shost;
  47. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  48. struct lpfc_hba *phba = vport->phba;
  49. struct lpfc_rport_data *rdata = job->rport->dd_data;
  50. struct lpfc_nodelist *ndlp = rdata->pnode;
  51. struct ulp_bde64 *bpl = NULL;
  52. uint32_t timeout;
  53. struct lpfc_iocbq *cmdiocbq = NULL;
  54. struct lpfc_iocbq *rspiocbq = NULL;
  55. IOCB_t *cmd;
  56. IOCB_t *rsp;
  57. struct lpfc_dmabuf *bmp = NULL;
  58. int request_nseg;
  59. int reply_nseg;
  60. struct scatterlist *sgel = NULL;
  61. int numbde;
  62. dma_addr_t busaddr;
  63. int rc = 0;
  64. /* in case no data is transferred */
  65. job->reply->reply_payload_rcv_len = 0;
  66. if (!lpfc_nlp_get(ndlp)) {
  67. job->reply->result = -ENODEV;
  68. return 0;
  69. }
  70. if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
  71. rc = -ENODEV;
  72. goto free_ndlp_exit;
  73. }
  74. spin_lock_irq(shost->host_lock);
  75. cmdiocbq = lpfc_sli_get_iocbq(phba);
  76. if (!cmdiocbq) {
  77. rc = -ENOMEM;
  78. spin_unlock_irq(shost->host_lock);
  79. goto free_ndlp_exit;
  80. }
  81. cmd = &cmdiocbq->iocb;
  82. rspiocbq = lpfc_sli_get_iocbq(phba);
  83. if (!rspiocbq) {
  84. rc = -ENOMEM;
  85. goto free_cmdiocbq;
  86. }
  87. spin_unlock_irq(shost->host_lock);
  88. rsp = &rspiocbq->iocb;
  89. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  90. if (!bmp) {
  91. rc = -ENOMEM;
  92. spin_lock_irq(shost->host_lock);
  93. goto free_rspiocbq;
  94. }
  95. spin_lock_irq(shost->host_lock);
  96. bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
  97. if (!bmp->virt) {
  98. rc = -ENOMEM;
  99. goto free_bmp;
  100. }
  101. spin_unlock_irq(shost->host_lock);
  102. INIT_LIST_HEAD(&bmp->list);
  103. bpl = (struct ulp_bde64 *) bmp->virt;
  104. request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
  105. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  106. for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
  107. busaddr = sg_dma_address(sgel);
  108. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  109. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  110. bpl->tus.w = cpu_to_le32(bpl->tus.w);
  111. bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
  112. bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
  113. bpl++;
  114. }
  115. reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
  116. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  117. for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
  118. busaddr = sg_dma_address(sgel);
  119. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  120. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  121. bpl->tus.w = cpu_to_le32(bpl->tus.w);
  122. bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
  123. bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
  124. bpl++;
  125. }
  126. cmd->un.genreq64.bdl.ulpIoTag32 = 0;
  127. cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
  128. cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
  129. cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  130. cmd->un.genreq64.bdl.bdeSize =
  131. (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
  132. cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
  133. cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
  134. cmd->un.genreq64.w5.hcsw.Dfctl = 0;
  135. cmd->un.genreq64.w5.hcsw.Rctl = FC_UNSOL_CTL;
  136. cmd->un.genreq64.w5.hcsw.Type = FC_COMMON_TRANSPORT_ULP;
  137. cmd->ulpBdeCount = 1;
  138. cmd->ulpLe = 1;
  139. cmd->ulpClass = CLASS3;
  140. cmd->ulpContext = ndlp->nlp_rpi;
  141. cmd->ulpOwner = OWN_CHIP;
  142. cmdiocbq->vport = phba->pport;
  143. cmdiocbq->context1 = NULL;
  144. cmdiocbq->context2 = NULL;
  145. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  146. timeout = phba->fc_ratov * 2;
  147. job->dd_data = cmdiocbq;
  148. rc = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq, rspiocbq,
  149. timeout + LPFC_DRVR_TIMEOUT);
  150. if (rc != IOCB_TIMEDOUT) {
  151. pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
  152. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  153. pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
  154. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  155. }
  156. if (rc == IOCB_TIMEDOUT) {
  157. lpfc_sli_release_iocbq(phba, rspiocbq);
  158. rc = -EACCES;
  159. goto free_ndlp_exit;
  160. }
  161. if (rc != IOCB_SUCCESS) {
  162. rc = -EACCES;
  163. goto free_outdmp;
  164. }
  165. if (rsp->ulpStatus) {
  166. if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  167. switch (rsp->un.ulpWord[4] & 0xff) {
  168. case IOERR_SEQUENCE_TIMEOUT:
  169. rc = -ETIMEDOUT;
  170. break;
  171. case IOERR_INVALID_RPI:
  172. rc = -EFAULT;
  173. break;
  174. default:
  175. rc = -EACCES;
  176. break;
  177. }
  178. goto free_outdmp;
  179. }
  180. } else
  181. job->reply->reply_payload_rcv_len =
  182. rsp->un.genreq64.bdl.bdeSize;
  183. free_outdmp:
  184. spin_lock_irq(shost->host_lock);
  185. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  186. free_bmp:
  187. kfree(bmp);
  188. free_rspiocbq:
  189. lpfc_sli_release_iocbq(phba, rspiocbq);
  190. free_cmdiocbq:
  191. lpfc_sli_release_iocbq(phba, cmdiocbq);
  192. spin_unlock_irq(shost->host_lock);
  193. free_ndlp_exit:
  194. lpfc_nlp_put(ndlp);
  195. /* make error code available to userspace */
  196. job->reply->result = rc;
  197. /* complete the job back to userspace */
  198. job->job_done(job);
  199. return 0;
  200. }
  201. /**
  202. * lpfc_bsg_rport_els - send an ELS command from a bsg request
  203. * @job: fc_bsg_job to handle
  204. */
  205. static int
  206. lpfc_bsg_rport_els(struct fc_bsg_job *job)
  207. {
  208. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  209. struct lpfc_hba *phba = vport->phba;
  210. struct lpfc_rport_data *rdata = job->rport->dd_data;
  211. struct lpfc_nodelist *ndlp = rdata->pnode;
  212. uint32_t elscmd;
  213. uint32_t cmdsize;
  214. uint32_t rspsize;
  215. struct lpfc_iocbq *rspiocbq;
  216. struct lpfc_iocbq *cmdiocbq;
  217. IOCB_t *rsp;
  218. uint16_t rpi = 0;
  219. struct lpfc_dmabuf *pcmd;
  220. struct lpfc_dmabuf *prsp;
  221. struct lpfc_dmabuf *pbuflist = NULL;
  222. struct ulp_bde64 *bpl;
  223. int iocb_status;
  224. int request_nseg;
  225. int reply_nseg;
  226. struct scatterlist *sgel = NULL;
  227. int numbde;
  228. dma_addr_t busaddr;
  229. int rc = 0;
  230. /* in case no data is transferred */
  231. job->reply->reply_payload_rcv_len = 0;
  232. if (!lpfc_nlp_get(ndlp)) {
  233. rc = -ENODEV;
  234. goto out;
  235. }
  236. elscmd = job->request->rqst_data.r_els.els_code;
  237. cmdsize = job->request_payload.payload_len;
  238. rspsize = job->reply_payload.payload_len;
  239. rspiocbq = lpfc_sli_get_iocbq(phba);
  240. if (!rspiocbq) {
  241. lpfc_nlp_put(ndlp);
  242. rc = -ENOMEM;
  243. goto out;
  244. }
  245. rsp = &rspiocbq->iocb;
  246. rpi = ndlp->nlp_rpi;
  247. cmdiocbq = lpfc_prep_els_iocb(phba->pport, 1, cmdsize, 0, ndlp,
  248. ndlp->nlp_DID, elscmd);
  249. if (!cmdiocbq) {
  250. lpfc_sli_release_iocbq(phba, rspiocbq);
  251. return -EIO;
  252. }
  253. job->dd_data = cmdiocbq;
  254. pcmd = (struct lpfc_dmabuf *) cmdiocbq->context2;
  255. prsp = (struct lpfc_dmabuf *) pcmd->list.next;
  256. lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
  257. kfree(pcmd);
  258. lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
  259. kfree(prsp);
  260. cmdiocbq->context2 = NULL;
  261. pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
  262. bpl = (struct ulp_bde64 *) pbuflist->virt;
  263. request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
  264. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  265. for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
  266. busaddr = sg_dma_address(sgel);
  267. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  268. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  269. bpl->tus.w = cpu_to_le32(bpl->tus.w);
  270. bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
  271. bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
  272. bpl++;
  273. }
  274. reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
  275. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  276. for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
  277. busaddr = sg_dma_address(sgel);
  278. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  279. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  280. bpl->tus.w = cpu_to_le32(bpl->tus.w);
  281. bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
  282. bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
  283. bpl++;
  284. }
  285. cmdiocbq->iocb.un.elsreq64.bdl.bdeSize =
  286. (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
  287. cmdiocbq->iocb.ulpContext = rpi;
  288. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  289. cmdiocbq->context1 = NULL;
  290. cmdiocbq->context2 = NULL;
  291. iocb_status = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
  292. rspiocbq, (phba->fc_ratov * 2)
  293. + LPFC_DRVR_TIMEOUT);
  294. /* release the new ndlp once the iocb completes */
  295. lpfc_nlp_put(ndlp);
  296. if (iocb_status != IOCB_TIMEDOUT) {
  297. pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
  298. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  299. pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
  300. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  301. }
  302. if (iocb_status == IOCB_SUCCESS) {
  303. if (rsp->ulpStatus == IOSTAT_SUCCESS) {
  304. job->reply->reply_payload_rcv_len =
  305. rsp->un.elsreq64.bdl.bdeSize;
  306. rc = 0;
  307. } else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
  308. struct fc_bsg_ctels_reply *els_reply;
  309. /* LS_RJT data returned in word 4 */
  310. uint8_t *rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
  311. els_reply = &job->reply->reply_data.ctels_reply;
  312. job->reply->result = 0;
  313. els_reply->status = FC_CTELS_STATUS_REJECT;
  314. els_reply->rjt_data.action = rjt_data[0];
  315. els_reply->rjt_data.reason_code = rjt_data[1];
  316. els_reply->rjt_data.reason_explanation = rjt_data[2];
  317. els_reply->rjt_data.vendor_unique = rjt_data[3];
  318. } else
  319. rc = -EIO;
  320. } else
  321. rc = -EIO;
  322. if (iocb_status != IOCB_TIMEDOUT)
  323. lpfc_els_free_iocb(phba, cmdiocbq);
  324. lpfc_sli_release_iocbq(phba, rspiocbq);
  325. out:
  326. /* make error code available to userspace */
  327. job->reply->result = rc;
  328. /* complete the job back to userspace */
  329. job->job_done(job);
  330. return 0;
  331. }
  332. struct lpfc_ct_event {
  333. struct list_head node;
  334. int ref;
  335. wait_queue_head_t wq;
  336. /* Event type and waiter identifiers */
  337. uint32_t type_mask;
  338. uint32_t req_id;
  339. uint32_t reg_id;
  340. /* next two flags are here for the auto-delete logic */
  341. unsigned long wait_time_stamp;
  342. int waiting;
  343. /* seen and not seen events */
  344. struct list_head events_to_get;
  345. struct list_head events_to_see;
  346. };
  347. struct event_data {
  348. struct list_head node;
  349. uint32_t type;
  350. uint32_t immed_dat;
  351. void *data;
  352. uint32_t len;
  353. };
  354. static struct lpfc_ct_event *
  355. lpfc_ct_event_new(int ev_reg_id, uint32_t ev_req_id)
  356. {
  357. struct lpfc_ct_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  358. if (!evt)
  359. return NULL;
  360. INIT_LIST_HEAD(&evt->events_to_get);
  361. INIT_LIST_HEAD(&evt->events_to_see);
  362. evt->req_id = ev_req_id;
  363. evt->reg_id = ev_reg_id;
  364. evt->wait_time_stamp = jiffies;
  365. init_waitqueue_head(&evt->wq);
  366. return evt;
  367. }
  368. static void
  369. lpfc_ct_event_free(struct lpfc_ct_event *evt)
  370. {
  371. struct event_data *ed;
  372. list_del(&evt->node);
  373. while (!list_empty(&evt->events_to_get)) {
  374. ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
  375. list_del(&ed->node);
  376. kfree(ed->data);
  377. kfree(ed);
  378. }
  379. while (!list_empty(&evt->events_to_see)) {
  380. ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
  381. list_del(&ed->node);
  382. kfree(ed->data);
  383. kfree(ed);
  384. }
  385. kfree(evt);
  386. }
  387. static inline void
  388. lpfc_ct_event_ref(struct lpfc_ct_event *evt)
  389. {
  390. evt->ref++;
  391. }
  392. static inline void
  393. lpfc_ct_event_unref(struct lpfc_ct_event *evt)
  394. {
  395. if (--evt->ref < 0)
  396. lpfc_ct_event_free(evt);
  397. }
  398. #define SLI_CT_ELX_LOOPBACK 0x10
  399. enum ELX_LOOPBACK_CMD {
  400. ELX_LOOPBACK_XRI_SETUP,
  401. ELX_LOOPBACK_DATA,
  402. };
  403. /**
  404. * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
  405. * @phba:
  406. * @pring:
  407. * @piocbq:
  408. *
  409. * This function is called when an unsolicited CT command is received. It
  410. * forwards the event to any processes registerd to receive CT events.
  411. */
  412. void
  413. lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  414. struct lpfc_iocbq *piocbq)
  415. {
  416. uint32_t evt_req_id = 0;
  417. uint32_t cmd;
  418. uint32_t len;
  419. struct lpfc_dmabuf *dmabuf = NULL;
  420. struct lpfc_ct_event *evt;
  421. struct event_data *evt_dat = NULL;
  422. struct lpfc_iocbq *iocbq;
  423. size_t offset = 0;
  424. struct list_head head;
  425. struct ulp_bde64 *bde;
  426. dma_addr_t dma_addr;
  427. int i;
  428. struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
  429. struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
  430. struct lpfc_hbq_entry *hbqe;
  431. struct lpfc_sli_ct_request *ct_req;
  432. INIT_LIST_HEAD(&head);
  433. list_add_tail(&head, &piocbq->list);
  434. if (piocbq->iocb.ulpBdeCount == 0 ||
  435. piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
  436. goto error_ct_unsol_exit;
  437. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
  438. dmabuf = bdeBuf1;
  439. else {
  440. dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
  441. piocbq->iocb.un.cont64[0].addrLow);
  442. dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
  443. }
  444. ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
  445. evt_req_id = ct_req->FsType;
  446. cmd = ct_req->CommandResponse.bits.CmdRsp;
  447. len = ct_req->CommandResponse.bits.Size;
  448. if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
  449. lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
  450. mutex_lock(&phba->ct_event_mutex);
  451. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  452. if (evt->req_id != evt_req_id)
  453. continue;
  454. lpfc_ct_event_ref(evt);
  455. evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
  456. if (!evt_dat) {
  457. lpfc_ct_event_unref(evt);
  458. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  459. "2614 Memory allocation failed for "
  460. "CT event\n");
  461. break;
  462. }
  463. mutex_unlock(&phba->ct_event_mutex);
  464. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  465. /* take accumulated byte count from the last iocbq */
  466. iocbq = list_entry(head.prev, typeof(*iocbq), list);
  467. evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
  468. } else {
  469. list_for_each_entry(iocbq, &head, list) {
  470. for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
  471. evt_dat->len +=
  472. iocbq->iocb.un.cont64[i].tus.f.bdeSize;
  473. }
  474. }
  475. evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
  476. if (!evt_dat->data) {
  477. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  478. "2615 Memory allocation failed for "
  479. "CT event data, size %d\n",
  480. evt_dat->len);
  481. kfree(evt_dat);
  482. mutex_lock(&phba->ct_event_mutex);
  483. lpfc_ct_event_unref(evt);
  484. mutex_unlock(&phba->ct_event_mutex);
  485. goto error_ct_unsol_exit;
  486. }
  487. list_for_each_entry(iocbq, &head, list) {
  488. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  489. bdeBuf1 = iocbq->context2;
  490. bdeBuf2 = iocbq->context3;
  491. }
  492. for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
  493. int size = 0;
  494. if (phba->sli3_options &
  495. LPFC_SLI3_HBQ_ENABLED) {
  496. if (i == 0) {
  497. hbqe = (struct lpfc_hbq_entry *)
  498. &iocbq->iocb.un.ulpWord[0];
  499. size = hbqe->bde.tus.f.bdeSize;
  500. dmabuf = bdeBuf1;
  501. } else if (i == 1) {
  502. hbqe = (struct lpfc_hbq_entry *)
  503. &iocbq->iocb.unsli3.
  504. sli3Words[4];
  505. size = hbqe->bde.tus.f.bdeSize;
  506. dmabuf = bdeBuf2;
  507. }
  508. if ((offset + size) > evt_dat->len)
  509. size = evt_dat->len - offset;
  510. } else {
  511. size = iocbq->iocb.un.cont64[i].
  512. tus.f.bdeSize;
  513. bde = &iocbq->iocb.un.cont64[i];
  514. dma_addr = getPaddr(bde->addrHigh,
  515. bde->addrLow);
  516. dmabuf = lpfc_sli_ringpostbuf_get(phba,
  517. pring, dma_addr);
  518. }
  519. if (!dmabuf) {
  520. lpfc_printf_log(phba, KERN_ERR,
  521. LOG_LIBDFC, "2616 No dmabuf "
  522. "found for iocbq 0x%p\n",
  523. iocbq);
  524. kfree(evt_dat->data);
  525. kfree(evt_dat);
  526. mutex_lock(&phba->ct_event_mutex);
  527. lpfc_ct_event_unref(evt);
  528. mutex_unlock(&phba->ct_event_mutex);
  529. goto error_ct_unsol_exit;
  530. }
  531. memcpy((char *)(evt_dat->data) + offset,
  532. dmabuf->virt, size);
  533. offset += size;
  534. if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
  535. !(phba->sli3_options &
  536. LPFC_SLI3_HBQ_ENABLED)) {
  537. lpfc_sli_ringpostbuf_put(phba, pring,
  538. dmabuf);
  539. } else {
  540. switch (cmd) {
  541. case ELX_LOOPBACK_XRI_SETUP:
  542. if (!(phba->sli3_options &
  543. LPFC_SLI3_HBQ_ENABLED))
  544. lpfc_post_buffer(phba,
  545. pring,
  546. 1);
  547. else
  548. lpfc_in_buf_free(phba,
  549. dmabuf);
  550. break;
  551. default:
  552. if (!(phba->sli3_options &
  553. LPFC_SLI3_HBQ_ENABLED))
  554. lpfc_post_buffer(phba,
  555. pring,
  556. 1);
  557. break;
  558. }
  559. }
  560. }
  561. }
  562. mutex_lock(&phba->ct_event_mutex);
  563. if (phba->sli_rev == LPFC_SLI_REV4) {
  564. evt_dat->immed_dat = phba->ctx_idx;
  565. phba->ctx_idx = (phba->ctx_idx + 1) % 64;
  566. phba->ct_ctx[evt_dat->immed_dat].oxid =
  567. piocbq->iocb.ulpContext;
  568. phba->ct_ctx[evt_dat->immed_dat].SID =
  569. piocbq->iocb.un.rcvels.remoteID;
  570. } else
  571. evt_dat->immed_dat = piocbq->iocb.ulpContext;
  572. evt_dat->type = FC_REG_CT_EVENT;
  573. list_add(&evt_dat->node, &evt->events_to_see);
  574. wake_up_interruptible(&evt->wq);
  575. lpfc_ct_event_unref(evt);
  576. if (evt_req_id == SLI_CT_ELX_LOOPBACK)
  577. break;
  578. }
  579. mutex_unlock(&phba->ct_event_mutex);
  580. error_ct_unsol_exit:
  581. if (!list_empty(&head))
  582. list_del(&head);
  583. return;
  584. }
  585. /**
  586. * lpfc_bsg_set_event - process a SET_EVENT bsg vendor command
  587. * @job: SET_EVENT fc_bsg_job
  588. */
  589. static int
  590. lpfc_bsg_set_event(struct fc_bsg_job *job)
  591. {
  592. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  593. struct lpfc_hba *phba = vport->phba;
  594. struct set_ct_event *event_req;
  595. struct lpfc_ct_event *evt;
  596. int rc = 0;
  597. if (job->request_len <
  598. sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
  599. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  600. "2612 Received SET_CT_EVENT below minimum "
  601. "size\n");
  602. return -EINVAL;
  603. }
  604. event_req = (struct set_ct_event *)
  605. job->request->rqst_data.h_vendor.vendor_cmd;
  606. mutex_lock(&phba->ct_event_mutex);
  607. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  608. if (evt->reg_id == event_req->ev_reg_id) {
  609. lpfc_ct_event_ref(evt);
  610. evt->wait_time_stamp = jiffies;
  611. break;
  612. }
  613. }
  614. mutex_unlock(&phba->ct_event_mutex);
  615. if (&evt->node == &phba->ct_ev_waiters) {
  616. /* no event waiting struct yet - first call */
  617. evt = lpfc_ct_event_new(event_req->ev_reg_id,
  618. event_req->ev_req_id);
  619. if (!evt) {
  620. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  621. "2617 Failed allocation of event "
  622. "waiter\n");
  623. return -ENOMEM;
  624. }
  625. mutex_lock(&phba->ct_event_mutex);
  626. list_add(&evt->node, &phba->ct_ev_waiters);
  627. lpfc_ct_event_ref(evt);
  628. mutex_unlock(&phba->ct_event_mutex);
  629. }
  630. evt->waiting = 1;
  631. if (wait_event_interruptible(evt->wq,
  632. !list_empty(&evt->events_to_see))) {
  633. mutex_lock(&phba->ct_event_mutex);
  634. lpfc_ct_event_unref(evt); /* release ref */
  635. lpfc_ct_event_unref(evt); /* delete */
  636. mutex_unlock(&phba->ct_event_mutex);
  637. rc = -EINTR;
  638. goto set_event_out;
  639. }
  640. evt->wait_time_stamp = jiffies;
  641. evt->waiting = 0;
  642. mutex_lock(&phba->ct_event_mutex);
  643. list_move(evt->events_to_see.prev, &evt->events_to_get);
  644. lpfc_ct_event_unref(evt); /* release ref */
  645. mutex_unlock(&phba->ct_event_mutex);
  646. set_event_out:
  647. /* set_event carries no reply payload */
  648. job->reply->reply_payload_rcv_len = 0;
  649. /* make error code available to userspace */
  650. job->reply->result = rc;
  651. /* complete the job back to userspace */
  652. job->job_done(job);
  653. return 0;
  654. }
  655. /**
  656. * lpfc_bsg_get_event - process a GET_EVENT bsg vendor command
  657. * @job: GET_EVENT fc_bsg_job
  658. */
  659. static int
  660. lpfc_bsg_get_event(struct fc_bsg_job *job)
  661. {
  662. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  663. struct lpfc_hba *phba = vport->phba;
  664. struct get_ct_event *event_req;
  665. struct get_ct_event_reply *event_reply;
  666. struct lpfc_ct_event *evt;
  667. struct event_data *evt_dat = NULL;
  668. int rc = 0;
  669. if (job->request_len <
  670. sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
  671. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  672. "2613 Received GET_CT_EVENT request below "
  673. "minimum size\n");
  674. return -EINVAL;
  675. }
  676. event_req = (struct get_ct_event *)
  677. job->request->rqst_data.h_vendor.vendor_cmd;
  678. event_reply = (struct get_ct_event_reply *)
  679. job->reply->reply_data.vendor_reply.vendor_rsp;
  680. mutex_lock(&phba->ct_event_mutex);
  681. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  682. if (evt->reg_id == event_req->ev_reg_id) {
  683. if (list_empty(&evt->events_to_get))
  684. break;
  685. lpfc_ct_event_ref(evt);
  686. evt->wait_time_stamp = jiffies;
  687. evt_dat = list_entry(evt->events_to_get.prev,
  688. struct event_data, node);
  689. list_del(&evt_dat->node);
  690. break;
  691. }
  692. }
  693. mutex_unlock(&phba->ct_event_mutex);
  694. if (!evt_dat) {
  695. job->reply->reply_payload_rcv_len = 0;
  696. rc = -ENOENT;
  697. goto error_get_event_exit;
  698. }
  699. if (evt_dat->len > job->reply_payload.payload_len) {
  700. evt_dat->len = job->reply_payload.payload_len;
  701. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  702. "2618 Truncated event data at %d "
  703. "bytes\n",
  704. job->reply_payload.payload_len);
  705. }
  706. event_reply->immed_data = evt_dat->immed_dat;
  707. if (evt_dat->len > 0)
  708. job->reply->reply_payload_rcv_len =
  709. sg_copy_from_buffer(job->reply_payload.sg_list,
  710. job->reply_payload.sg_cnt,
  711. evt_dat->data, evt_dat->len);
  712. else
  713. job->reply->reply_payload_rcv_len = 0;
  714. rc = 0;
  715. if (evt_dat)
  716. kfree(evt_dat->data);
  717. kfree(evt_dat);
  718. mutex_lock(&phba->ct_event_mutex);
  719. lpfc_ct_event_unref(evt);
  720. mutex_unlock(&phba->ct_event_mutex);
  721. error_get_event_exit:
  722. /* make error code available to userspace */
  723. job->reply->result = rc;
  724. /* complete the job back to userspace */
  725. job->job_done(job);
  726. return rc;
  727. }
  728. /**
  729. * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
  730. * @job: fc_bsg_job to handle
  731. */
  732. static int
  733. lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
  734. {
  735. int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
  736. switch (command) {
  737. case LPFC_BSG_VENDOR_SET_CT_EVENT:
  738. return lpfc_bsg_set_event(job);
  739. break;
  740. case LPFC_BSG_VENDOR_GET_CT_EVENT:
  741. return lpfc_bsg_get_event(job);
  742. break;
  743. default:
  744. return -EINVAL;
  745. }
  746. }
  747. /**
  748. * lpfc_bsg_request - handle a bsg request from the FC transport
  749. * @job: fc_bsg_job to handle
  750. */
  751. int
  752. lpfc_bsg_request(struct fc_bsg_job *job)
  753. {
  754. uint32_t msgcode;
  755. int rc = -EINVAL;
  756. msgcode = job->request->msgcode;
  757. switch (msgcode) {
  758. case FC_BSG_HST_VENDOR:
  759. rc = lpfc_bsg_hst_vendor(job);
  760. break;
  761. case FC_BSG_RPT_ELS:
  762. rc = lpfc_bsg_rport_els(job);
  763. break;
  764. case FC_BSG_RPT_CT:
  765. rc = lpfc_bsg_rport_ct(job);
  766. break;
  767. default:
  768. break;
  769. }
  770. return rc;
  771. }
  772. /**
  773. * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
  774. * @job: fc_bsg_job that has timed out
  775. *
  776. * This function just aborts the job's IOCB. The aborted IOCB will return to
  777. * the waiting function which will handle passing the error back to userspace
  778. */
  779. int
  780. lpfc_bsg_timeout(struct fc_bsg_job *job)
  781. {
  782. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  783. struct lpfc_hba *phba = vport->phba;
  784. struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)job->dd_data;
  785. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  786. if (cmdiocb)
  787. lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
  788. return 0;
  789. }