lpfc_bsg.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2009-2010 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 <linux/delay.h>
  24. #include <scsi/scsi.h>
  25. #include <scsi/scsi_host.h>
  26. #include <scsi/scsi_transport_fc.h>
  27. #include <scsi/scsi_bsg_fc.h>
  28. #include <scsi/fc/fc_fs.h>
  29. #include "lpfc_hw4.h"
  30. #include "lpfc_hw.h"
  31. #include "lpfc_sli.h"
  32. #include "lpfc_sli4.h"
  33. #include "lpfc_nl.h"
  34. #include "lpfc_bsg.h"
  35. #include "lpfc_disc.h"
  36. #include "lpfc_scsi.h"
  37. #include "lpfc.h"
  38. #include "lpfc_logmsg.h"
  39. #include "lpfc_crtn.h"
  40. #include "lpfc_vport.h"
  41. #include "lpfc_version.h"
  42. struct lpfc_bsg_event {
  43. struct list_head node;
  44. struct kref kref;
  45. wait_queue_head_t wq;
  46. /* Event type and waiter identifiers */
  47. uint32_t type_mask;
  48. uint32_t req_id;
  49. uint32_t reg_id;
  50. /* next two flags are here for the auto-delete logic */
  51. unsigned long wait_time_stamp;
  52. int waiting;
  53. /* seen and not seen events */
  54. struct list_head events_to_get;
  55. struct list_head events_to_see;
  56. /* job waiting for this event to finish */
  57. struct fc_bsg_job *set_job;
  58. };
  59. struct lpfc_bsg_iocb {
  60. struct lpfc_iocbq *cmdiocbq;
  61. struct lpfc_iocbq *rspiocbq;
  62. struct lpfc_dmabuf *bmp;
  63. struct lpfc_nodelist *ndlp;
  64. /* job waiting for this iocb to finish */
  65. struct fc_bsg_job *set_job;
  66. };
  67. struct lpfc_bsg_mbox {
  68. LPFC_MBOXQ_t *pmboxq;
  69. MAILBOX_t *mb;
  70. /* job waiting for this mbox command to finish */
  71. struct fc_bsg_job *set_job;
  72. };
  73. #define TYPE_EVT 1
  74. #define TYPE_IOCB 2
  75. #define TYPE_MBOX 3
  76. struct bsg_job_data {
  77. uint32_t type;
  78. union {
  79. struct lpfc_bsg_event *evt;
  80. struct lpfc_bsg_iocb iocb;
  81. struct lpfc_bsg_mbox mbox;
  82. } context_un;
  83. };
  84. struct event_data {
  85. struct list_head node;
  86. uint32_t type;
  87. uint32_t immed_dat;
  88. void *data;
  89. uint32_t len;
  90. };
  91. #define BUF_SZ_4K 4096
  92. #define SLI_CT_ELX_LOOPBACK 0x10
  93. enum ELX_LOOPBACK_CMD {
  94. ELX_LOOPBACK_XRI_SETUP,
  95. ELX_LOOPBACK_DATA,
  96. };
  97. #define ELX_LOOPBACK_HEADER_SZ \
  98. (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
  99. struct lpfc_dmabufext {
  100. struct lpfc_dmabuf dma;
  101. uint32_t size;
  102. uint32_t flag;
  103. };
  104. /**
  105. * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
  106. * @phba: Pointer to HBA context object.
  107. * @cmdiocbq: Pointer to command iocb.
  108. * @rspiocbq: Pointer to response iocb.
  109. *
  110. * This function is the completion handler for iocbs issued using
  111. * lpfc_bsg_send_mgmt_cmd function. This function is called by the
  112. * ring event handler function without any lock held. This function
  113. * can be called from both worker thread context and interrupt
  114. * context. This function also can be called from another thread which
  115. * cleans up the SLI layer objects.
  116. * This function copies the contents of the response iocb to the
  117. * response iocb memory object provided by the caller of
  118. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  119. * sleeps for the iocb completion.
  120. **/
  121. static void
  122. lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
  123. struct lpfc_iocbq *cmdiocbq,
  124. struct lpfc_iocbq *rspiocbq)
  125. {
  126. unsigned long iflags;
  127. struct bsg_job_data *dd_data;
  128. struct fc_bsg_job *job;
  129. IOCB_t *rsp;
  130. struct lpfc_dmabuf *bmp;
  131. struct lpfc_nodelist *ndlp;
  132. struct lpfc_bsg_iocb *iocb;
  133. unsigned long flags;
  134. int rc = 0;
  135. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  136. dd_data = cmdiocbq->context1;
  137. if (!dd_data) {
  138. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  139. return;
  140. }
  141. iocb = &dd_data->context_un.iocb;
  142. job = iocb->set_job;
  143. job->dd_data = NULL; /* so timeout handler does not reply */
  144. spin_lock_irqsave(&phba->hbalock, iflags);
  145. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  146. if (cmdiocbq->context2 && rspiocbq)
  147. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  148. &rspiocbq->iocb, sizeof(IOCB_t));
  149. spin_unlock_irqrestore(&phba->hbalock, iflags);
  150. bmp = iocb->bmp;
  151. rspiocbq = iocb->rspiocbq;
  152. rsp = &rspiocbq->iocb;
  153. ndlp = iocb->ndlp;
  154. pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
  155. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  156. pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
  157. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  158. if (rsp->ulpStatus) {
  159. if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  160. switch (rsp->un.ulpWord[4] & 0xff) {
  161. case IOERR_SEQUENCE_TIMEOUT:
  162. rc = -ETIMEDOUT;
  163. break;
  164. case IOERR_INVALID_RPI:
  165. rc = -EFAULT;
  166. break;
  167. default:
  168. rc = -EACCES;
  169. break;
  170. }
  171. } else
  172. rc = -EACCES;
  173. } else
  174. job->reply->reply_payload_rcv_len =
  175. rsp->un.genreq64.bdl.bdeSize;
  176. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  177. lpfc_sli_release_iocbq(phba, rspiocbq);
  178. lpfc_sli_release_iocbq(phba, cmdiocbq);
  179. lpfc_nlp_put(ndlp);
  180. kfree(bmp);
  181. kfree(dd_data);
  182. /* make error code available to userspace */
  183. job->reply->result = rc;
  184. /* complete the job back to userspace */
  185. job->job_done(job);
  186. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  187. return;
  188. }
  189. /**
  190. * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
  191. * @job: fc_bsg_job to handle
  192. **/
  193. static int
  194. lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
  195. {
  196. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  197. struct lpfc_hba *phba = vport->phba;
  198. struct lpfc_rport_data *rdata = job->rport->dd_data;
  199. struct lpfc_nodelist *ndlp = rdata->pnode;
  200. struct ulp_bde64 *bpl = NULL;
  201. uint32_t timeout;
  202. struct lpfc_iocbq *cmdiocbq = NULL;
  203. struct lpfc_iocbq *rspiocbq = NULL;
  204. IOCB_t *cmd;
  205. IOCB_t *rsp;
  206. struct lpfc_dmabuf *bmp = NULL;
  207. int request_nseg;
  208. int reply_nseg;
  209. struct scatterlist *sgel = NULL;
  210. int numbde;
  211. dma_addr_t busaddr;
  212. struct bsg_job_data *dd_data;
  213. uint32_t creg_val;
  214. int rc = 0;
  215. /* in case no data is transferred */
  216. job->reply->reply_payload_rcv_len = 0;
  217. /* allocate our bsg tracking structure */
  218. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  219. if (!dd_data) {
  220. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  221. "2733 Failed allocation of dd_data\n");
  222. rc = -ENOMEM;
  223. goto no_dd_data;
  224. }
  225. if (!lpfc_nlp_get(ndlp)) {
  226. rc = -ENODEV;
  227. goto no_ndlp;
  228. }
  229. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  230. if (!bmp) {
  231. rc = -ENOMEM;
  232. goto free_ndlp;
  233. }
  234. if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
  235. rc = -ENODEV;
  236. goto free_bmp;
  237. }
  238. cmdiocbq = lpfc_sli_get_iocbq(phba);
  239. if (!cmdiocbq) {
  240. rc = -ENOMEM;
  241. goto free_bmp;
  242. }
  243. cmd = &cmdiocbq->iocb;
  244. rspiocbq = lpfc_sli_get_iocbq(phba);
  245. if (!rspiocbq) {
  246. rc = -ENOMEM;
  247. goto free_cmdiocbq;
  248. }
  249. rsp = &rspiocbq->iocb;
  250. bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
  251. if (!bmp->virt) {
  252. rc = -ENOMEM;
  253. goto free_rspiocbq;
  254. }
  255. INIT_LIST_HEAD(&bmp->list);
  256. bpl = (struct ulp_bde64 *) bmp->virt;
  257. request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
  258. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  259. for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
  260. busaddr = sg_dma_address(sgel);
  261. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  262. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  263. bpl->tus.w = cpu_to_le32(bpl->tus.w);
  264. bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
  265. bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
  266. bpl++;
  267. }
  268. reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
  269. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  270. for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
  271. busaddr = sg_dma_address(sgel);
  272. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  273. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  274. bpl->tus.w = cpu_to_le32(bpl->tus.w);
  275. bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
  276. bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
  277. bpl++;
  278. }
  279. cmd->un.genreq64.bdl.ulpIoTag32 = 0;
  280. cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
  281. cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
  282. cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  283. cmd->un.genreq64.bdl.bdeSize =
  284. (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
  285. cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
  286. cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
  287. cmd->un.genreq64.w5.hcsw.Dfctl = 0;
  288. cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
  289. cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
  290. cmd->ulpBdeCount = 1;
  291. cmd->ulpLe = 1;
  292. cmd->ulpClass = CLASS3;
  293. cmd->ulpContext = ndlp->nlp_rpi;
  294. cmd->ulpOwner = OWN_CHIP;
  295. cmdiocbq->vport = phba->pport;
  296. cmdiocbq->context3 = bmp;
  297. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  298. timeout = phba->fc_ratov * 2;
  299. cmd->ulpTimeout = timeout;
  300. cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
  301. cmdiocbq->context1 = dd_data;
  302. cmdiocbq->context2 = rspiocbq;
  303. dd_data->type = TYPE_IOCB;
  304. dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
  305. dd_data->context_un.iocb.rspiocbq = rspiocbq;
  306. dd_data->context_un.iocb.set_job = job;
  307. dd_data->context_un.iocb.bmp = bmp;
  308. dd_data->context_un.iocb.ndlp = ndlp;
  309. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  310. creg_val = readl(phba->HCregaddr);
  311. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  312. writel(creg_val, phba->HCregaddr);
  313. readl(phba->HCregaddr); /* flush */
  314. }
  315. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
  316. if (rc == IOCB_SUCCESS)
  317. return 0; /* done for now */
  318. /* iocb failed so cleanup */
  319. pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
  320. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  321. pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
  322. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  323. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  324. free_rspiocbq:
  325. lpfc_sli_release_iocbq(phba, rspiocbq);
  326. free_cmdiocbq:
  327. lpfc_sli_release_iocbq(phba, cmdiocbq);
  328. free_bmp:
  329. kfree(bmp);
  330. free_ndlp:
  331. lpfc_nlp_put(ndlp);
  332. no_ndlp:
  333. kfree(dd_data);
  334. no_dd_data:
  335. /* make error code available to userspace */
  336. job->reply->result = rc;
  337. job->dd_data = NULL;
  338. return rc;
  339. }
  340. /**
  341. * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
  342. * @phba: Pointer to HBA context object.
  343. * @cmdiocbq: Pointer to command iocb.
  344. * @rspiocbq: Pointer to response iocb.
  345. *
  346. * This function is the completion handler for iocbs issued using
  347. * lpfc_bsg_rport_els_cmp function. This function is called by the
  348. * ring event handler function without any lock held. This function
  349. * can be called from both worker thread context and interrupt
  350. * context. This function also can be called from other thread which
  351. * cleans up the SLI layer objects.
  352. * This function copies the contents of the response iocb to the
  353. * response iocb memory object provided by the caller of
  354. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  355. * sleeps for the iocb completion.
  356. **/
  357. static void
  358. lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
  359. struct lpfc_iocbq *cmdiocbq,
  360. struct lpfc_iocbq *rspiocbq)
  361. {
  362. struct bsg_job_data *dd_data;
  363. struct fc_bsg_job *job;
  364. IOCB_t *rsp;
  365. struct lpfc_nodelist *ndlp;
  366. struct lpfc_dmabuf *pbuflist = NULL;
  367. struct fc_bsg_ctels_reply *els_reply;
  368. uint8_t *rjt_data;
  369. unsigned long flags;
  370. int rc = 0;
  371. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  372. dd_data = cmdiocbq->context1;
  373. /* normal completion and timeout crossed paths, already done */
  374. if (!dd_data) {
  375. spin_unlock_irqrestore(&phba->hbalock, flags);
  376. return;
  377. }
  378. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  379. if (cmdiocbq->context2 && rspiocbq)
  380. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  381. &rspiocbq->iocb, sizeof(IOCB_t));
  382. job = dd_data->context_un.iocb.set_job;
  383. cmdiocbq = dd_data->context_un.iocb.cmdiocbq;
  384. rspiocbq = dd_data->context_un.iocb.rspiocbq;
  385. rsp = &rspiocbq->iocb;
  386. ndlp = dd_data->context_un.iocb.ndlp;
  387. pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
  388. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  389. pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
  390. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  391. if (job->reply->result == -EAGAIN)
  392. rc = -EAGAIN;
  393. else if (rsp->ulpStatus == IOSTAT_SUCCESS)
  394. job->reply->reply_payload_rcv_len =
  395. rsp->un.elsreq64.bdl.bdeSize;
  396. else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
  397. job->reply->reply_payload_rcv_len =
  398. sizeof(struct fc_bsg_ctels_reply);
  399. /* LS_RJT data returned in word 4 */
  400. rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
  401. els_reply = &job->reply->reply_data.ctels_reply;
  402. els_reply->status = FC_CTELS_STATUS_REJECT;
  403. els_reply->rjt_data.action = rjt_data[3];
  404. els_reply->rjt_data.reason_code = rjt_data[2];
  405. els_reply->rjt_data.reason_explanation = rjt_data[1];
  406. els_reply->rjt_data.vendor_unique = rjt_data[0];
  407. } else
  408. rc = -EIO;
  409. pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
  410. lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
  411. lpfc_sli_release_iocbq(phba, rspiocbq);
  412. lpfc_sli_release_iocbq(phba, cmdiocbq);
  413. lpfc_nlp_put(ndlp);
  414. kfree(dd_data);
  415. /* make error code available to userspace */
  416. job->reply->result = rc;
  417. job->dd_data = NULL;
  418. /* complete the job back to userspace */
  419. job->job_done(job);
  420. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  421. return;
  422. }
  423. /**
  424. * lpfc_bsg_rport_els - send an ELS command from a bsg request
  425. * @job: fc_bsg_job to handle
  426. **/
  427. static int
  428. lpfc_bsg_rport_els(struct fc_bsg_job *job)
  429. {
  430. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  431. struct lpfc_hba *phba = vport->phba;
  432. struct lpfc_rport_data *rdata = job->rport->dd_data;
  433. struct lpfc_nodelist *ndlp = rdata->pnode;
  434. uint32_t elscmd;
  435. uint32_t cmdsize;
  436. uint32_t rspsize;
  437. struct lpfc_iocbq *rspiocbq;
  438. struct lpfc_iocbq *cmdiocbq;
  439. IOCB_t *rsp;
  440. uint16_t rpi = 0;
  441. struct lpfc_dmabuf *pcmd;
  442. struct lpfc_dmabuf *prsp;
  443. struct lpfc_dmabuf *pbuflist = NULL;
  444. struct ulp_bde64 *bpl;
  445. int request_nseg;
  446. int reply_nseg;
  447. struct scatterlist *sgel = NULL;
  448. int numbde;
  449. dma_addr_t busaddr;
  450. struct bsg_job_data *dd_data;
  451. uint32_t creg_val;
  452. int rc = 0;
  453. /* in case no data is transferred */
  454. job->reply->reply_payload_rcv_len = 0;
  455. /* allocate our bsg tracking structure */
  456. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  457. if (!dd_data) {
  458. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  459. "2735 Failed allocation of dd_data\n");
  460. rc = -ENOMEM;
  461. goto no_dd_data;
  462. }
  463. if (!lpfc_nlp_get(ndlp)) {
  464. rc = -ENODEV;
  465. goto free_dd_data;
  466. }
  467. elscmd = job->request->rqst_data.r_els.els_code;
  468. cmdsize = job->request_payload.payload_len;
  469. rspsize = job->reply_payload.payload_len;
  470. rspiocbq = lpfc_sli_get_iocbq(phba);
  471. if (!rspiocbq) {
  472. lpfc_nlp_put(ndlp);
  473. rc = -ENOMEM;
  474. goto free_dd_data;
  475. }
  476. rsp = &rspiocbq->iocb;
  477. rpi = ndlp->nlp_rpi;
  478. cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
  479. ndlp->nlp_DID, elscmd);
  480. if (!cmdiocbq) {
  481. rc = -EIO;
  482. goto free_rspiocbq;
  483. }
  484. /* prep els iocb set context1 to the ndlp, context2 to the command
  485. * dmabuf, context3 holds the data dmabuf
  486. */
  487. pcmd = (struct lpfc_dmabuf *) cmdiocbq->context2;
  488. prsp = (struct lpfc_dmabuf *) pcmd->list.next;
  489. lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
  490. kfree(pcmd);
  491. lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
  492. kfree(prsp);
  493. cmdiocbq->context2 = NULL;
  494. pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
  495. bpl = (struct ulp_bde64 *) pbuflist->virt;
  496. request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
  497. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  498. for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
  499. busaddr = sg_dma_address(sgel);
  500. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  501. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  502. bpl->tus.w = cpu_to_le32(bpl->tus.w);
  503. bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
  504. bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
  505. bpl++;
  506. }
  507. reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
  508. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  509. for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
  510. busaddr = sg_dma_address(sgel);
  511. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  512. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  513. bpl->tus.w = cpu_to_le32(bpl->tus.w);
  514. bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
  515. bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
  516. bpl++;
  517. }
  518. cmdiocbq->iocb.un.elsreq64.bdl.bdeSize =
  519. (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
  520. cmdiocbq->iocb.ulpContext = rpi;
  521. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  522. cmdiocbq->context1 = NULL;
  523. cmdiocbq->context2 = NULL;
  524. cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
  525. cmdiocbq->context1 = dd_data;
  526. cmdiocbq->context2 = rspiocbq;
  527. dd_data->type = TYPE_IOCB;
  528. dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
  529. dd_data->context_un.iocb.rspiocbq = rspiocbq;
  530. dd_data->context_un.iocb.set_job = job;
  531. dd_data->context_un.iocb.bmp = NULL;;
  532. dd_data->context_un.iocb.ndlp = ndlp;
  533. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  534. creg_val = readl(phba->HCregaddr);
  535. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  536. writel(creg_val, phba->HCregaddr);
  537. readl(phba->HCregaddr); /* flush */
  538. }
  539. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
  540. lpfc_nlp_put(ndlp);
  541. if (rc == IOCB_SUCCESS)
  542. return 0; /* done for now */
  543. pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
  544. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  545. pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
  546. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  547. lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
  548. lpfc_sli_release_iocbq(phba, cmdiocbq);
  549. free_rspiocbq:
  550. lpfc_sli_release_iocbq(phba, rspiocbq);
  551. free_dd_data:
  552. kfree(dd_data);
  553. no_dd_data:
  554. /* make error code available to userspace */
  555. job->reply->result = rc;
  556. job->dd_data = NULL;
  557. return rc;
  558. }
  559. /**
  560. * lpfc_bsg_event_free - frees an allocated event structure
  561. * @kref: Pointer to a kref.
  562. *
  563. * Called from kref_put. Back cast the kref into an event structure address.
  564. * Free any events to get, delete associated nodes, free any events to see,
  565. * free any data then free the event itself.
  566. **/
  567. static void
  568. lpfc_bsg_event_free(struct kref *kref)
  569. {
  570. struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
  571. kref);
  572. struct event_data *ed;
  573. list_del(&evt->node);
  574. while (!list_empty(&evt->events_to_get)) {
  575. ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
  576. list_del(&ed->node);
  577. kfree(ed->data);
  578. kfree(ed);
  579. }
  580. while (!list_empty(&evt->events_to_see)) {
  581. ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
  582. list_del(&ed->node);
  583. kfree(ed->data);
  584. kfree(ed);
  585. }
  586. kfree(evt);
  587. }
  588. /**
  589. * lpfc_bsg_event_ref - increments the kref for an event
  590. * @evt: Pointer to an event structure.
  591. **/
  592. static inline void
  593. lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
  594. {
  595. kref_get(&evt->kref);
  596. }
  597. /**
  598. * lpfc_bsg_event_unref - Uses kref_put to free an event structure
  599. * @evt: Pointer to an event structure.
  600. **/
  601. static inline void
  602. lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
  603. {
  604. kref_put(&evt->kref, lpfc_bsg_event_free);
  605. }
  606. /**
  607. * lpfc_bsg_event_new - allocate and initialize a event structure
  608. * @ev_mask: Mask of events.
  609. * @ev_reg_id: Event reg id.
  610. * @ev_req_id: Event request id.
  611. **/
  612. static struct lpfc_bsg_event *
  613. lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
  614. {
  615. struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  616. if (!evt)
  617. return NULL;
  618. INIT_LIST_HEAD(&evt->events_to_get);
  619. INIT_LIST_HEAD(&evt->events_to_see);
  620. evt->type_mask = ev_mask;
  621. evt->req_id = ev_req_id;
  622. evt->reg_id = ev_reg_id;
  623. evt->wait_time_stamp = jiffies;
  624. init_waitqueue_head(&evt->wq);
  625. kref_init(&evt->kref);
  626. return evt;
  627. }
  628. /**
  629. * diag_cmd_data_free - Frees an lpfc dma buffer extension
  630. * @phba: Pointer to HBA context object.
  631. * @mlist: Pointer to an lpfc dma buffer extension.
  632. **/
  633. static int
  634. diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
  635. {
  636. struct lpfc_dmabufext *mlast;
  637. struct pci_dev *pcidev;
  638. struct list_head head, *curr, *next;
  639. if ((!mlist) || (!lpfc_is_link_up(phba) &&
  640. (phba->link_flag & LS_LOOPBACK_MODE))) {
  641. return 0;
  642. }
  643. pcidev = phba->pcidev;
  644. list_add_tail(&head, &mlist->dma.list);
  645. list_for_each_safe(curr, next, &head) {
  646. mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
  647. if (mlast->dma.virt)
  648. dma_free_coherent(&pcidev->dev,
  649. mlast->size,
  650. mlast->dma.virt,
  651. mlast->dma.phys);
  652. kfree(mlast);
  653. }
  654. return 0;
  655. }
  656. /**
  657. * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
  658. * @phba:
  659. * @pring:
  660. * @piocbq:
  661. *
  662. * This function is called when an unsolicited CT command is received. It
  663. * forwards the event to any processes registered to receive CT events.
  664. **/
  665. int
  666. lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  667. struct lpfc_iocbq *piocbq)
  668. {
  669. uint32_t evt_req_id = 0;
  670. uint32_t cmd;
  671. uint32_t len;
  672. struct lpfc_dmabuf *dmabuf = NULL;
  673. struct lpfc_bsg_event *evt;
  674. struct event_data *evt_dat = NULL;
  675. struct lpfc_iocbq *iocbq;
  676. size_t offset = 0;
  677. struct list_head head;
  678. struct ulp_bde64 *bde;
  679. dma_addr_t dma_addr;
  680. int i;
  681. struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
  682. struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
  683. struct lpfc_hbq_entry *hbqe;
  684. struct lpfc_sli_ct_request *ct_req;
  685. struct fc_bsg_job *job = NULL;
  686. unsigned long flags;
  687. int size = 0;
  688. INIT_LIST_HEAD(&head);
  689. list_add_tail(&head, &piocbq->list);
  690. if (piocbq->iocb.ulpBdeCount == 0 ||
  691. piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
  692. goto error_ct_unsol_exit;
  693. if (phba->link_state == LPFC_HBA_ERROR ||
  694. (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
  695. goto error_ct_unsol_exit;
  696. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
  697. dmabuf = bdeBuf1;
  698. else {
  699. dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
  700. piocbq->iocb.un.cont64[0].addrLow);
  701. dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
  702. }
  703. if (dmabuf == NULL)
  704. goto error_ct_unsol_exit;
  705. ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
  706. evt_req_id = ct_req->FsType;
  707. cmd = ct_req->CommandResponse.bits.CmdRsp;
  708. len = ct_req->CommandResponse.bits.Size;
  709. if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
  710. lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
  711. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  712. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  713. if (!(evt->type_mask & FC_REG_CT_EVENT) ||
  714. evt->req_id != evt_req_id)
  715. continue;
  716. lpfc_bsg_event_ref(evt);
  717. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  718. evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
  719. if (evt_dat == NULL) {
  720. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  721. lpfc_bsg_event_unref(evt);
  722. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  723. "2614 Memory allocation failed for "
  724. "CT event\n");
  725. break;
  726. }
  727. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  728. /* take accumulated byte count from the last iocbq */
  729. iocbq = list_entry(head.prev, typeof(*iocbq), list);
  730. evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
  731. } else {
  732. list_for_each_entry(iocbq, &head, list) {
  733. for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
  734. evt_dat->len +=
  735. iocbq->iocb.un.cont64[i].tus.f.bdeSize;
  736. }
  737. }
  738. evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
  739. if (evt_dat->data == NULL) {
  740. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  741. "2615 Memory allocation failed for "
  742. "CT event data, size %d\n",
  743. evt_dat->len);
  744. kfree(evt_dat);
  745. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  746. lpfc_bsg_event_unref(evt);
  747. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  748. goto error_ct_unsol_exit;
  749. }
  750. list_for_each_entry(iocbq, &head, list) {
  751. size = 0;
  752. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  753. bdeBuf1 = iocbq->context2;
  754. bdeBuf2 = iocbq->context3;
  755. }
  756. for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
  757. if (phba->sli3_options &
  758. LPFC_SLI3_HBQ_ENABLED) {
  759. if (i == 0) {
  760. hbqe = (struct lpfc_hbq_entry *)
  761. &iocbq->iocb.un.ulpWord[0];
  762. size = hbqe->bde.tus.f.bdeSize;
  763. dmabuf = bdeBuf1;
  764. } else if (i == 1) {
  765. hbqe = (struct lpfc_hbq_entry *)
  766. &iocbq->iocb.unsli3.
  767. sli3Words[4];
  768. size = hbqe->bde.tus.f.bdeSize;
  769. dmabuf = bdeBuf2;
  770. }
  771. if ((offset + size) > evt_dat->len)
  772. size = evt_dat->len - offset;
  773. } else {
  774. size = iocbq->iocb.un.cont64[i].
  775. tus.f.bdeSize;
  776. bde = &iocbq->iocb.un.cont64[i];
  777. dma_addr = getPaddr(bde->addrHigh,
  778. bde->addrLow);
  779. dmabuf = lpfc_sli_ringpostbuf_get(phba,
  780. pring, dma_addr);
  781. }
  782. if (!dmabuf) {
  783. lpfc_printf_log(phba, KERN_ERR,
  784. LOG_LIBDFC, "2616 No dmabuf "
  785. "found for iocbq 0x%p\n",
  786. iocbq);
  787. kfree(evt_dat->data);
  788. kfree(evt_dat);
  789. spin_lock_irqsave(&phba->ct_ev_lock,
  790. flags);
  791. lpfc_bsg_event_unref(evt);
  792. spin_unlock_irqrestore(
  793. &phba->ct_ev_lock, flags);
  794. goto error_ct_unsol_exit;
  795. }
  796. memcpy((char *)(evt_dat->data) + offset,
  797. dmabuf->virt, size);
  798. offset += size;
  799. if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
  800. !(phba->sli3_options &
  801. LPFC_SLI3_HBQ_ENABLED)) {
  802. lpfc_sli_ringpostbuf_put(phba, pring,
  803. dmabuf);
  804. } else {
  805. switch (cmd) {
  806. case ELX_LOOPBACK_DATA:
  807. diag_cmd_data_free(phba,
  808. (struct lpfc_dmabufext *)
  809. dmabuf);
  810. break;
  811. case ELX_LOOPBACK_XRI_SETUP:
  812. if ((phba->sli_rev ==
  813. LPFC_SLI_REV2) ||
  814. (phba->sli3_options &
  815. LPFC_SLI3_HBQ_ENABLED
  816. )) {
  817. lpfc_in_buf_free(phba,
  818. dmabuf);
  819. } else {
  820. lpfc_post_buffer(phba,
  821. pring,
  822. 1);
  823. }
  824. break;
  825. default:
  826. if (!(phba->sli3_options &
  827. LPFC_SLI3_HBQ_ENABLED))
  828. lpfc_post_buffer(phba,
  829. pring,
  830. 1);
  831. break;
  832. }
  833. }
  834. }
  835. }
  836. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  837. if (phba->sli_rev == LPFC_SLI_REV4) {
  838. evt_dat->immed_dat = phba->ctx_idx;
  839. phba->ctx_idx = (phba->ctx_idx + 1) % 64;
  840. phba->ct_ctx[evt_dat->immed_dat].oxid =
  841. piocbq->iocb.ulpContext;
  842. phba->ct_ctx[evt_dat->immed_dat].SID =
  843. piocbq->iocb.un.rcvels.remoteID;
  844. } else
  845. evt_dat->immed_dat = piocbq->iocb.ulpContext;
  846. evt_dat->type = FC_REG_CT_EVENT;
  847. list_add(&evt_dat->node, &evt->events_to_see);
  848. if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
  849. wake_up_interruptible(&evt->wq);
  850. lpfc_bsg_event_unref(evt);
  851. break;
  852. }
  853. list_move(evt->events_to_see.prev, &evt->events_to_get);
  854. lpfc_bsg_event_unref(evt);
  855. job = evt->set_job;
  856. evt->set_job = NULL;
  857. if (job) {
  858. job->reply->reply_payload_rcv_len = size;
  859. /* make error code available to userspace */
  860. job->reply->result = 0;
  861. job->dd_data = NULL;
  862. /* complete the job back to userspace */
  863. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  864. job->job_done(job);
  865. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  866. }
  867. }
  868. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  869. error_ct_unsol_exit:
  870. if (!list_empty(&head))
  871. list_del(&head);
  872. if (evt_req_id == SLI_CT_ELX_LOOPBACK)
  873. return 0;
  874. return 1;
  875. }
  876. /**
  877. * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
  878. * @job: SET_EVENT fc_bsg_job
  879. **/
  880. static int
  881. lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
  882. {
  883. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  884. struct lpfc_hba *phba = vport->phba;
  885. struct set_ct_event *event_req;
  886. struct lpfc_bsg_event *evt;
  887. int rc = 0;
  888. struct bsg_job_data *dd_data = NULL;
  889. uint32_t ev_mask;
  890. unsigned long flags;
  891. if (job->request_len <
  892. sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
  893. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  894. "2612 Received SET_CT_EVENT below minimum "
  895. "size\n");
  896. rc = -EINVAL;
  897. goto job_error;
  898. }
  899. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  900. if (dd_data == NULL) {
  901. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  902. "2734 Failed allocation of dd_data\n");
  903. rc = -ENOMEM;
  904. goto job_error;
  905. }
  906. event_req = (struct set_ct_event *)
  907. job->request->rqst_data.h_vendor.vendor_cmd;
  908. ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
  909. FC_REG_EVENT_MASK);
  910. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  911. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  912. if (evt->reg_id == event_req->ev_reg_id) {
  913. lpfc_bsg_event_ref(evt);
  914. evt->wait_time_stamp = jiffies;
  915. break;
  916. }
  917. }
  918. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  919. if (&evt->node == &phba->ct_ev_waiters) {
  920. /* no event waiting struct yet - first call */
  921. evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
  922. event_req->ev_req_id);
  923. if (!evt) {
  924. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  925. "2617 Failed allocation of event "
  926. "waiter\n");
  927. rc = -ENOMEM;
  928. goto job_error;
  929. }
  930. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  931. list_add(&evt->node, &phba->ct_ev_waiters);
  932. lpfc_bsg_event_ref(evt);
  933. evt->wait_time_stamp = jiffies;
  934. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  935. }
  936. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  937. evt->waiting = 1;
  938. dd_data->type = TYPE_EVT;
  939. dd_data->context_un.evt = evt;
  940. evt->set_job = job; /* for unsolicited command */
  941. job->dd_data = dd_data; /* for fc transport timeout callback*/
  942. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  943. return 0; /* call job done later */
  944. job_error:
  945. if (dd_data != NULL)
  946. kfree(dd_data);
  947. job->dd_data = NULL;
  948. return rc;
  949. }
  950. /**
  951. * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
  952. * @job: GET_EVENT fc_bsg_job
  953. **/
  954. static int
  955. lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
  956. {
  957. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  958. struct lpfc_hba *phba = vport->phba;
  959. struct get_ct_event *event_req;
  960. struct get_ct_event_reply *event_reply;
  961. struct lpfc_bsg_event *evt;
  962. struct event_data *evt_dat = NULL;
  963. unsigned long flags;
  964. uint32_t rc = 0;
  965. if (job->request_len <
  966. sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
  967. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  968. "2613 Received GET_CT_EVENT request below "
  969. "minimum size\n");
  970. rc = -EINVAL;
  971. goto job_error;
  972. }
  973. event_req = (struct get_ct_event *)
  974. job->request->rqst_data.h_vendor.vendor_cmd;
  975. event_reply = (struct get_ct_event_reply *)
  976. job->reply->reply_data.vendor_reply.vendor_rsp;
  977. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  978. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  979. if (evt->reg_id == event_req->ev_reg_id) {
  980. if (list_empty(&evt->events_to_get))
  981. break;
  982. lpfc_bsg_event_ref(evt);
  983. evt->wait_time_stamp = jiffies;
  984. evt_dat = list_entry(evt->events_to_get.prev,
  985. struct event_data, node);
  986. list_del(&evt_dat->node);
  987. break;
  988. }
  989. }
  990. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  991. /* The app may continue to ask for event data until it gets
  992. * an error indicating that there isn't anymore
  993. */
  994. if (evt_dat == NULL) {
  995. job->reply->reply_payload_rcv_len = 0;
  996. rc = -ENOENT;
  997. goto job_error;
  998. }
  999. if (evt_dat->len > job->request_payload.payload_len) {
  1000. evt_dat->len = job->request_payload.payload_len;
  1001. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1002. "2618 Truncated event data at %d "
  1003. "bytes\n",
  1004. job->request_payload.payload_len);
  1005. }
  1006. event_reply->type = evt_dat->type;
  1007. event_reply->immed_data = evt_dat->immed_dat;
  1008. if (evt_dat->len > 0)
  1009. job->reply->reply_payload_rcv_len =
  1010. sg_copy_from_buffer(job->request_payload.sg_list,
  1011. job->request_payload.sg_cnt,
  1012. evt_dat->data, evt_dat->len);
  1013. else
  1014. job->reply->reply_payload_rcv_len = 0;
  1015. if (evt_dat) {
  1016. kfree(evt_dat->data);
  1017. kfree(evt_dat);
  1018. }
  1019. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1020. lpfc_bsg_event_unref(evt);
  1021. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1022. job->dd_data = NULL;
  1023. job->reply->result = 0;
  1024. job->job_done(job);
  1025. return 0;
  1026. job_error:
  1027. job->dd_data = NULL;
  1028. job->reply->result = rc;
  1029. return rc;
  1030. }
  1031. /**
  1032. * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
  1033. * @phba: Pointer to HBA context object.
  1034. * @cmdiocbq: Pointer to command iocb.
  1035. * @rspiocbq: Pointer to response iocb.
  1036. *
  1037. * This function is the completion handler for iocbs issued using
  1038. * lpfc_issue_ct_rsp_cmp function. This function is called by the
  1039. * ring event handler function without any lock held. This function
  1040. * can be called from both worker thread context and interrupt
  1041. * context. This function also can be called from other thread which
  1042. * cleans up the SLI layer objects.
  1043. * This function copy the contents of the response iocb to the
  1044. * response iocb memory object provided by the caller of
  1045. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  1046. * sleeps for the iocb completion.
  1047. **/
  1048. static void
  1049. lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
  1050. struct lpfc_iocbq *cmdiocbq,
  1051. struct lpfc_iocbq *rspiocbq)
  1052. {
  1053. struct bsg_job_data *dd_data;
  1054. struct fc_bsg_job *job;
  1055. IOCB_t *rsp;
  1056. struct lpfc_dmabuf *bmp;
  1057. struct lpfc_nodelist *ndlp;
  1058. unsigned long flags;
  1059. int rc = 0;
  1060. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1061. dd_data = cmdiocbq->context1;
  1062. /* normal completion and timeout crossed paths, already done */
  1063. if (!dd_data) {
  1064. spin_unlock_irqrestore(&phba->hbalock, flags);
  1065. return;
  1066. }
  1067. job = dd_data->context_un.iocb.set_job;
  1068. bmp = dd_data->context_un.iocb.bmp;
  1069. rsp = &rspiocbq->iocb;
  1070. ndlp = dd_data->context_un.iocb.ndlp;
  1071. pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
  1072. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1073. if (rsp->ulpStatus) {
  1074. if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  1075. switch (rsp->un.ulpWord[4] & 0xff) {
  1076. case IOERR_SEQUENCE_TIMEOUT:
  1077. rc = -ETIMEDOUT;
  1078. break;
  1079. case IOERR_INVALID_RPI:
  1080. rc = -EFAULT;
  1081. break;
  1082. default:
  1083. rc = -EACCES;
  1084. break;
  1085. }
  1086. } else
  1087. rc = -EACCES;
  1088. } else
  1089. job->reply->reply_payload_rcv_len =
  1090. rsp->un.genreq64.bdl.bdeSize;
  1091. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  1092. lpfc_sli_release_iocbq(phba, cmdiocbq);
  1093. lpfc_nlp_put(ndlp);
  1094. kfree(bmp);
  1095. kfree(dd_data);
  1096. /* make error code available to userspace */
  1097. job->reply->result = rc;
  1098. job->dd_data = NULL;
  1099. /* complete the job back to userspace */
  1100. job->job_done(job);
  1101. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1102. return;
  1103. }
  1104. /**
  1105. * lpfc_issue_ct_rsp - issue a ct response
  1106. * @phba: Pointer to HBA context object.
  1107. * @job: Pointer to the job object.
  1108. * @tag: tag index value into the ports context exchange array.
  1109. * @bmp: Pointer to a dma buffer descriptor.
  1110. * @num_entry: Number of enties in the bde.
  1111. **/
  1112. static int
  1113. lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
  1114. struct lpfc_dmabuf *bmp, int num_entry)
  1115. {
  1116. IOCB_t *icmd;
  1117. struct lpfc_iocbq *ctiocb = NULL;
  1118. int rc = 0;
  1119. struct lpfc_nodelist *ndlp = NULL;
  1120. struct bsg_job_data *dd_data;
  1121. uint32_t creg_val;
  1122. /* allocate our bsg tracking structure */
  1123. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  1124. if (!dd_data) {
  1125. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1126. "2736 Failed allocation of dd_data\n");
  1127. rc = -ENOMEM;
  1128. goto no_dd_data;
  1129. }
  1130. /* Allocate buffer for command iocb */
  1131. ctiocb = lpfc_sli_get_iocbq(phba);
  1132. if (!ctiocb) {
  1133. rc = ENOMEM;
  1134. goto no_ctiocb;
  1135. }
  1136. icmd = &ctiocb->iocb;
  1137. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  1138. icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
  1139. icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
  1140. icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  1141. icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
  1142. icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
  1143. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  1144. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
  1145. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
  1146. /* Fill in rest of iocb */
  1147. icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
  1148. icmd->ulpBdeCount = 1;
  1149. icmd->ulpLe = 1;
  1150. icmd->ulpClass = CLASS3;
  1151. if (phba->sli_rev == LPFC_SLI_REV4) {
  1152. /* Do not issue unsol response if oxid not marked as valid */
  1153. if (!(phba->ct_ctx[tag].flags & UNSOL_VALID)) {
  1154. rc = IOCB_ERROR;
  1155. goto issue_ct_rsp_exit;
  1156. }
  1157. icmd->ulpContext = phba->ct_ctx[tag].oxid;
  1158. ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
  1159. if (!ndlp) {
  1160. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  1161. "2721 ndlp null for oxid %x SID %x\n",
  1162. icmd->ulpContext,
  1163. phba->ct_ctx[tag].SID);
  1164. rc = IOCB_ERROR;
  1165. goto issue_ct_rsp_exit;
  1166. }
  1167. icmd->un.ulpWord[3] = ndlp->nlp_rpi;
  1168. /* The exchange is done, mark the entry as invalid */
  1169. phba->ct_ctx[tag].flags &= ~UNSOL_VALID;
  1170. } else
  1171. icmd->ulpContext = (ushort) tag;
  1172. icmd->ulpTimeout = phba->fc_ratov * 2;
  1173. /* Xmit CT response on exchange <xid> */
  1174. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  1175. "2722 Xmit CT response on exchange x%x Data: x%x x%x\n",
  1176. icmd->ulpContext, icmd->ulpIoTag, phba->link_state);
  1177. ctiocb->iocb_cmpl = NULL;
  1178. ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
  1179. ctiocb->vport = phba->pport;
  1180. ctiocb->context3 = bmp;
  1181. ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
  1182. ctiocb->context1 = dd_data;
  1183. ctiocb->context2 = NULL;
  1184. dd_data->type = TYPE_IOCB;
  1185. dd_data->context_un.iocb.cmdiocbq = ctiocb;
  1186. dd_data->context_un.iocb.rspiocbq = NULL;
  1187. dd_data->context_un.iocb.set_job = job;
  1188. dd_data->context_un.iocb.bmp = bmp;
  1189. dd_data->context_un.iocb.ndlp = ndlp;
  1190. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  1191. creg_val = readl(phba->HCregaddr);
  1192. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  1193. writel(creg_val, phba->HCregaddr);
  1194. readl(phba->HCregaddr); /* flush */
  1195. }
  1196. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  1197. if (rc == IOCB_SUCCESS)
  1198. return 0; /* done for now */
  1199. issue_ct_rsp_exit:
  1200. lpfc_sli_release_iocbq(phba, ctiocb);
  1201. no_ctiocb:
  1202. kfree(dd_data);
  1203. no_dd_data:
  1204. return rc;
  1205. }
  1206. /**
  1207. * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
  1208. * @job: SEND_MGMT_RESP fc_bsg_job
  1209. **/
  1210. static int
  1211. lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
  1212. {
  1213. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  1214. struct lpfc_hba *phba = vport->phba;
  1215. struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
  1216. job->request->rqst_data.h_vendor.vendor_cmd;
  1217. struct ulp_bde64 *bpl;
  1218. struct lpfc_dmabuf *bmp = NULL;
  1219. struct scatterlist *sgel = NULL;
  1220. int request_nseg;
  1221. int numbde;
  1222. dma_addr_t busaddr;
  1223. uint32_t tag = mgmt_resp->tag;
  1224. unsigned long reqbfrcnt =
  1225. (unsigned long)job->request_payload.payload_len;
  1226. int rc = 0;
  1227. /* in case no data is transferred */
  1228. job->reply->reply_payload_rcv_len = 0;
  1229. if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
  1230. rc = -ERANGE;
  1231. goto send_mgmt_rsp_exit;
  1232. }
  1233. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  1234. if (!bmp) {
  1235. rc = -ENOMEM;
  1236. goto send_mgmt_rsp_exit;
  1237. }
  1238. bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
  1239. if (!bmp->virt) {
  1240. rc = -ENOMEM;
  1241. goto send_mgmt_rsp_free_bmp;
  1242. }
  1243. INIT_LIST_HEAD(&bmp->list);
  1244. bpl = (struct ulp_bde64 *) bmp->virt;
  1245. request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
  1246. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1247. for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
  1248. busaddr = sg_dma_address(sgel);
  1249. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1250. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  1251. bpl->tus.w = cpu_to_le32(bpl->tus.w);
  1252. bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
  1253. bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
  1254. bpl++;
  1255. }
  1256. rc = lpfc_issue_ct_rsp(phba, job, tag, bmp, request_nseg);
  1257. if (rc == IOCB_SUCCESS)
  1258. return 0; /* done for now */
  1259. /* TBD need to handle a timeout */
  1260. pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
  1261. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1262. rc = -EACCES;
  1263. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  1264. send_mgmt_rsp_free_bmp:
  1265. kfree(bmp);
  1266. send_mgmt_rsp_exit:
  1267. /* make error code available to userspace */
  1268. job->reply->result = rc;
  1269. job->dd_data = NULL;
  1270. return rc;
  1271. }
  1272. /**
  1273. * lpfc_bsg_diag_mode - process a LPFC_BSG_VENDOR_DIAG_MODE bsg vendor command
  1274. * @job: LPFC_BSG_VENDOR_DIAG_MODE
  1275. *
  1276. * This function is responsible for placing a port into diagnostic loopback
  1277. * mode in order to perform a diagnostic loopback test.
  1278. * All new scsi requests are blocked, a small delay is used to allow the
  1279. * scsi requests to complete then the link is brought down. If the link is
  1280. * is placed in loopback mode then scsi requests are again allowed
  1281. * so the scsi mid-layer doesn't give up on the port.
  1282. * All of this is done in-line.
  1283. */
  1284. static int
  1285. lpfc_bsg_diag_mode(struct fc_bsg_job *job)
  1286. {
  1287. struct Scsi_Host *shost = job->shost;
  1288. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  1289. struct lpfc_hba *phba = vport->phba;
  1290. struct diag_mode_set *loopback_mode;
  1291. struct lpfc_sli *psli = &phba->sli;
  1292. struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
  1293. uint32_t link_flags;
  1294. uint32_t timeout;
  1295. struct lpfc_vport **vports;
  1296. LPFC_MBOXQ_t *pmboxq;
  1297. int mbxstatus;
  1298. int i = 0;
  1299. int rc = 0;
  1300. /* no data to return just the return code */
  1301. job->reply->reply_payload_rcv_len = 0;
  1302. if (job->request_len <
  1303. sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_set)) {
  1304. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1305. "2738 Received DIAG MODE request below minimum "
  1306. "size\n");
  1307. rc = -EINVAL;
  1308. goto job_error;
  1309. }
  1310. loopback_mode = (struct diag_mode_set *)
  1311. job->request->rqst_data.h_vendor.vendor_cmd;
  1312. link_flags = loopback_mode->type;
  1313. timeout = loopback_mode->timeout;
  1314. if ((phba->link_state == LPFC_HBA_ERROR) ||
  1315. (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
  1316. (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
  1317. rc = -EACCES;
  1318. goto job_error;
  1319. }
  1320. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1321. if (!pmboxq) {
  1322. rc = -ENOMEM;
  1323. goto job_error;
  1324. }
  1325. vports = lpfc_create_vport_work_array(phba);
  1326. if (vports) {
  1327. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1328. shost = lpfc_shost_from_vport(vports[i]);
  1329. scsi_block_requests(shost);
  1330. }
  1331. lpfc_destroy_vport_work_array(phba, vports);
  1332. } else {
  1333. shost = lpfc_shost_from_vport(phba->pport);
  1334. scsi_block_requests(shost);
  1335. }
  1336. while (pring->txcmplq_cnt) {
  1337. if (i++ > 500) /* wait up to 5 seconds */
  1338. break;
  1339. msleep(10);
  1340. }
  1341. memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  1342. pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
  1343. pmboxq->u.mb.mbxOwner = OWN_HOST;
  1344. mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
  1345. if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
  1346. /* wait for link down before proceeding */
  1347. i = 0;
  1348. while (phba->link_state != LPFC_LINK_DOWN) {
  1349. if (i++ > timeout) {
  1350. rc = -ETIMEDOUT;
  1351. goto loopback_mode_exit;
  1352. }
  1353. msleep(10);
  1354. }
  1355. memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  1356. if (link_flags == INTERNAL_LOOP_BACK)
  1357. pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
  1358. else
  1359. pmboxq->u.mb.un.varInitLnk.link_flags =
  1360. FLAGS_TOPOLOGY_MODE_LOOP;
  1361. pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
  1362. pmboxq->u.mb.mbxOwner = OWN_HOST;
  1363. mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
  1364. LPFC_MBOX_TMO);
  1365. if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
  1366. rc = -ENODEV;
  1367. else {
  1368. phba->link_flag |= LS_LOOPBACK_MODE;
  1369. /* wait for the link attention interrupt */
  1370. msleep(100);
  1371. i = 0;
  1372. while (phba->link_state != LPFC_HBA_READY) {
  1373. if (i++ > timeout) {
  1374. rc = -ETIMEDOUT;
  1375. break;
  1376. }
  1377. msleep(10);
  1378. }
  1379. }
  1380. } else
  1381. rc = -ENODEV;
  1382. loopback_mode_exit:
  1383. vports = lpfc_create_vport_work_array(phba);
  1384. if (vports) {
  1385. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1386. shost = lpfc_shost_from_vport(vports[i]);
  1387. scsi_unblock_requests(shost);
  1388. }
  1389. lpfc_destroy_vport_work_array(phba, vports);
  1390. } else {
  1391. shost = lpfc_shost_from_vport(phba->pport);
  1392. scsi_unblock_requests(shost);
  1393. }
  1394. /*
  1395. * Let SLI layer release mboxq if mbox command completed after timeout.
  1396. */
  1397. if (mbxstatus != MBX_TIMEOUT)
  1398. mempool_free(pmboxq, phba->mbox_mem_pool);
  1399. job_error:
  1400. /* make error code available to userspace */
  1401. job->reply->result = rc;
  1402. /* complete the job back to userspace if no error */
  1403. if (rc == 0)
  1404. job->job_done(job);
  1405. return rc;
  1406. }
  1407. /**
  1408. * lpfcdiag_loop_self_reg - obtains a remote port login id
  1409. * @phba: Pointer to HBA context object
  1410. * @rpi: Pointer to a remote port login id
  1411. *
  1412. * This function obtains a remote port login id so the diag loopback test
  1413. * can send and receive its own unsolicited CT command.
  1414. **/
  1415. static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t * rpi)
  1416. {
  1417. LPFC_MBOXQ_t *mbox;
  1418. struct lpfc_dmabuf *dmabuff;
  1419. int status;
  1420. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1421. if (!mbox)
  1422. return ENOMEM;
  1423. status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
  1424. (uint8_t *)&phba->pport->fc_sparam, mbox, 0);
  1425. if (status) {
  1426. mempool_free(mbox, phba->mbox_mem_pool);
  1427. return ENOMEM;
  1428. }
  1429. dmabuff = (struct lpfc_dmabuf *) mbox->context1;
  1430. mbox->context1 = NULL;
  1431. status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  1432. if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
  1433. lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
  1434. kfree(dmabuff);
  1435. if (status != MBX_TIMEOUT)
  1436. mempool_free(mbox, phba->mbox_mem_pool);
  1437. return ENODEV;
  1438. }
  1439. *rpi = mbox->u.mb.un.varWords[0];
  1440. lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
  1441. kfree(dmabuff);
  1442. mempool_free(mbox, phba->mbox_mem_pool);
  1443. return 0;
  1444. }
  1445. /**
  1446. * lpfcdiag_loop_self_unreg - unregs from the rpi
  1447. * @phba: Pointer to HBA context object
  1448. * @rpi: Remote port login id
  1449. *
  1450. * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
  1451. **/
  1452. static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
  1453. {
  1454. LPFC_MBOXQ_t *mbox;
  1455. int status;
  1456. /* Allocate mboxq structure */
  1457. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1458. if (mbox == NULL)
  1459. return ENOMEM;
  1460. lpfc_unreg_login(phba, 0, rpi, mbox);
  1461. status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  1462. if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
  1463. if (status != MBX_TIMEOUT)
  1464. mempool_free(mbox, phba->mbox_mem_pool);
  1465. return EIO;
  1466. }
  1467. mempool_free(mbox, phba->mbox_mem_pool);
  1468. return 0;
  1469. }
  1470. /**
  1471. * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
  1472. * @phba: Pointer to HBA context object
  1473. * @rpi: Remote port login id
  1474. * @txxri: Pointer to transmit exchange id
  1475. * @rxxri: Pointer to response exchabge id
  1476. *
  1477. * This function obtains the transmit and receive ids required to send
  1478. * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
  1479. * flags are used to the unsolicted response handler is able to process
  1480. * the ct command sent on the same port.
  1481. **/
  1482. static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
  1483. uint16_t *txxri, uint16_t * rxxri)
  1484. {
  1485. struct lpfc_bsg_event *evt;
  1486. struct lpfc_iocbq *cmdiocbq, *rspiocbq;
  1487. IOCB_t *cmd, *rsp;
  1488. struct lpfc_dmabuf *dmabuf;
  1489. struct ulp_bde64 *bpl = NULL;
  1490. struct lpfc_sli_ct_request *ctreq = NULL;
  1491. int ret_val = 0;
  1492. unsigned long flags;
  1493. *txxri = 0;
  1494. *rxxri = 0;
  1495. evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
  1496. SLI_CT_ELX_LOOPBACK);
  1497. if (!evt)
  1498. return ENOMEM;
  1499. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1500. list_add(&evt->node, &phba->ct_ev_waiters);
  1501. lpfc_bsg_event_ref(evt);
  1502. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1503. cmdiocbq = lpfc_sli_get_iocbq(phba);
  1504. rspiocbq = lpfc_sli_get_iocbq(phba);
  1505. dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  1506. if (dmabuf) {
  1507. dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
  1508. INIT_LIST_HEAD(&dmabuf->list);
  1509. bpl = (struct ulp_bde64 *) dmabuf->virt;
  1510. memset(bpl, 0, sizeof(*bpl));
  1511. ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
  1512. bpl->addrHigh =
  1513. le32_to_cpu(putPaddrHigh(dmabuf->phys + sizeof(*bpl)));
  1514. bpl->addrLow =
  1515. le32_to_cpu(putPaddrLow(dmabuf->phys + sizeof(*bpl)));
  1516. bpl->tus.f.bdeFlags = 0;
  1517. bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
  1518. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1519. }
  1520. if (cmdiocbq == NULL || rspiocbq == NULL ||
  1521. dmabuf == NULL || bpl == NULL || ctreq == NULL) {
  1522. ret_val = ENOMEM;
  1523. goto err_get_xri_exit;
  1524. }
  1525. cmd = &cmdiocbq->iocb;
  1526. rsp = &rspiocbq->iocb;
  1527. memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
  1528. ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
  1529. ctreq->RevisionId.bits.InId = 0;
  1530. ctreq->FsType = SLI_CT_ELX_LOOPBACK;
  1531. ctreq->FsSubType = 0;
  1532. ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
  1533. ctreq->CommandResponse.bits.Size = 0;
  1534. cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
  1535. cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
  1536. cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  1537. cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
  1538. cmd->un.xseq64.w5.hcsw.Fctl = LA;
  1539. cmd->un.xseq64.w5.hcsw.Dfctl = 0;
  1540. cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
  1541. cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
  1542. cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
  1543. cmd->ulpBdeCount = 1;
  1544. cmd->ulpLe = 1;
  1545. cmd->ulpClass = CLASS3;
  1546. cmd->ulpContext = rpi;
  1547. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  1548. cmdiocbq->vport = phba->pport;
  1549. ret_val = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
  1550. rspiocbq,
  1551. (phba->fc_ratov * 2)
  1552. + LPFC_DRVR_TIMEOUT);
  1553. if (ret_val)
  1554. goto err_get_xri_exit;
  1555. *txxri = rsp->ulpContext;
  1556. evt->waiting = 1;
  1557. evt->wait_time_stamp = jiffies;
  1558. ret_val = wait_event_interruptible_timeout(
  1559. evt->wq, !list_empty(&evt->events_to_see),
  1560. ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
  1561. if (list_empty(&evt->events_to_see))
  1562. ret_val = (ret_val) ? EINTR : ETIMEDOUT;
  1563. else {
  1564. ret_val = IOCB_SUCCESS;
  1565. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1566. list_move(evt->events_to_see.prev, &evt->events_to_get);
  1567. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1568. *rxxri = (list_entry(evt->events_to_get.prev,
  1569. typeof(struct event_data),
  1570. node))->immed_dat;
  1571. }
  1572. evt->waiting = 0;
  1573. err_get_xri_exit:
  1574. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1575. lpfc_bsg_event_unref(evt); /* release ref */
  1576. lpfc_bsg_event_unref(evt); /* delete */
  1577. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1578. if (dmabuf) {
  1579. if (dmabuf->virt)
  1580. lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
  1581. kfree(dmabuf);
  1582. }
  1583. if (cmdiocbq && (ret_val != IOCB_TIMEDOUT))
  1584. lpfc_sli_release_iocbq(phba, cmdiocbq);
  1585. if (rspiocbq)
  1586. lpfc_sli_release_iocbq(phba, rspiocbq);
  1587. return ret_val;
  1588. }
  1589. /**
  1590. * diag_cmd_data_alloc - fills in a bde struct with dma buffers
  1591. * @phba: Pointer to HBA context object
  1592. * @bpl: Pointer to 64 bit bde structure
  1593. * @size: Number of bytes to process
  1594. * @nocopydata: Flag to copy user data into the allocated buffer
  1595. *
  1596. * This function allocates page size buffers and populates an lpfc_dmabufext.
  1597. * If allowed the user data pointed to with indataptr is copied into the kernel
  1598. * memory. The chained list of page size buffers is returned.
  1599. **/
  1600. static struct lpfc_dmabufext *
  1601. diag_cmd_data_alloc(struct lpfc_hba *phba,
  1602. struct ulp_bde64 *bpl, uint32_t size,
  1603. int nocopydata)
  1604. {
  1605. struct lpfc_dmabufext *mlist = NULL;
  1606. struct lpfc_dmabufext *dmp;
  1607. int cnt, offset = 0, i = 0;
  1608. struct pci_dev *pcidev;
  1609. pcidev = phba->pcidev;
  1610. while (size) {
  1611. /* We get chunks of 4K */
  1612. if (size > BUF_SZ_4K)
  1613. cnt = BUF_SZ_4K;
  1614. else
  1615. cnt = size;
  1616. /* allocate struct lpfc_dmabufext buffer header */
  1617. dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
  1618. if (!dmp)
  1619. goto out;
  1620. INIT_LIST_HEAD(&dmp->dma.list);
  1621. /* Queue it to a linked list */
  1622. if (mlist)
  1623. list_add_tail(&dmp->dma.list, &mlist->dma.list);
  1624. else
  1625. mlist = dmp;
  1626. /* allocate buffer */
  1627. dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
  1628. cnt,
  1629. &(dmp->dma.phys),
  1630. GFP_KERNEL);
  1631. if (!dmp->dma.virt)
  1632. goto out;
  1633. dmp->size = cnt;
  1634. if (nocopydata) {
  1635. bpl->tus.f.bdeFlags = 0;
  1636. pci_dma_sync_single_for_device(phba->pcidev,
  1637. dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
  1638. } else {
  1639. memset((uint8_t *)dmp->dma.virt, 0, cnt);
  1640. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1641. }
  1642. /* build buffer ptr list for IOCB */
  1643. bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
  1644. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
  1645. bpl->tus.f.bdeSize = (ushort) cnt;
  1646. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1647. bpl++;
  1648. i++;
  1649. offset += cnt;
  1650. size -= cnt;
  1651. }
  1652. mlist->flag = i;
  1653. return mlist;
  1654. out:
  1655. diag_cmd_data_free(phba, mlist);
  1656. return NULL;
  1657. }
  1658. /**
  1659. * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
  1660. * @phba: Pointer to HBA context object
  1661. * @rxxri: Receive exchange id
  1662. * @len: Number of data bytes
  1663. *
  1664. * This function allocates and posts a data buffer of sufficient size to recieve
  1665. * an unsolicted CT command.
  1666. **/
  1667. static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
  1668. size_t len)
  1669. {
  1670. struct lpfc_sli *psli = &phba->sli;
  1671. struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
  1672. struct lpfc_iocbq *cmdiocbq;
  1673. IOCB_t *cmd = NULL;
  1674. struct list_head head, *curr, *next;
  1675. struct lpfc_dmabuf *rxbmp;
  1676. struct lpfc_dmabuf *dmp;
  1677. struct lpfc_dmabuf *mp[2] = {NULL, NULL};
  1678. struct ulp_bde64 *rxbpl = NULL;
  1679. uint32_t num_bde;
  1680. struct lpfc_dmabufext *rxbuffer = NULL;
  1681. int ret_val = 0;
  1682. int i = 0;
  1683. cmdiocbq = lpfc_sli_get_iocbq(phba);
  1684. rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  1685. if (rxbmp != NULL) {
  1686. rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
  1687. INIT_LIST_HEAD(&rxbmp->list);
  1688. rxbpl = (struct ulp_bde64 *) rxbmp->virt;
  1689. rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
  1690. }
  1691. if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
  1692. ret_val = ENOMEM;
  1693. goto err_post_rxbufs_exit;
  1694. }
  1695. /* Queue buffers for the receive exchange */
  1696. num_bde = (uint32_t)rxbuffer->flag;
  1697. dmp = &rxbuffer->dma;
  1698. cmd = &cmdiocbq->iocb;
  1699. i = 0;
  1700. INIT_LIST_HEAD(&head);
  1701. list_add_tail(&head, &dmp->list);
  1702. list_for_each_safe(curr, next, &head) {
  1703. mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
  1704. list_del(curr);
  1705. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  1706. mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
  1707. cmd->un.quexri64cx.buff.bde.addrHigh =
  1708. putPaddrHigh(mp[i]->phys);
  1709. cmd->un.quexri64cx.buff.bde.addrLow =
  1710. putPaddrLow(mp[i]->phys);
  1711. cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
  1712. ((struct lpfc_dmabufext *)mp[i])->size;
  1713. cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
  1714. cmd->ulpCommand = CMD_QUE_XRI64_CX;
  1715. cmd->ulpPU = 0;
  1716. cmd->ulpLe = 1;
  1717. cmd->ulpBdeCount = 1;
  1718. cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
  1719. } else {
  1720. cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
  1721. cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
  1722. cmd->un.cont64[i].tus.f.bdeSize =
  1723. ((struct lpfc_dmabufext *)mp[i])->size;
  1724. cmd->ulpBdeCount = ++i;
  1725. if ((--num_bde > 0) && (i < 2))
  1726. continue;
  1727. cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
  1728. cmd->ulpLe = 1;
  1729. }
  1730. cmd->ulpClass = CLASS3;
  1731. cmd->ulpContext = rxxri;
  1732. ret_val = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
  1733. if (ret_val == IOCB_ERROR) {
  1734. diag_cmd_data_free(phba,
  1735. (struct lpfc_dmabufext *)mp[0]);
  1736. if (mp[1])
  1737. diag_cmd_data_free(phba,
  1738. (struct lpfc_dmabufext *)mp[1]);
  1739. dmp = list_entry(next, struct lpfc_dmabuf, list);
  1740. ret_val = EIO;
  1741. goto err_post_rxbufs_exit;
  1742. }
  1743. lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
  1744. if (mp[1]) {
  1745. lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
  1746. mp[1] = NULL;
  1747. }
  1748. /* The iocb was freed by lpfc_sli_issue_iocb */
  1749. cmdiocbq = lpfc_sli_get_iocbq(phba);
  1750. if (!cmdiocbq) {
  1751. dmp = list_entry(next, struct lpfc_dmabuf, list);
  1752. ret_val = EIO;
  1753. goto err_post_rxbufs_exit;
  1754. }
  1755. cmd = &cmdiocbq->iocb;
  1756. i = 0;
  1757. }
  1758. list_del(&head);
  1759. err_post_rxbufs_exit:
  1760. if (rxbmp) {
  1761. if (rxbmp->virt)
  1762. lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
  1763. kfree(rxbmp);
  1764. }
  1765. if (cmdiocbq)
  1766. lpfc_sli_release_iocbq(phba, cmdiocbq);
  1767. return ret_val;
  1768. }
  1769. /**
  1770. * lpfc_bsg_diag_test - with a port in loopback issues a Ct cmd to itself
  1771. * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
  1772. *
  1773. * This function receives a user data buffer to be transmitted and received on
  1774. * the same port, the link must be up and in loopback mode prior
  1775. * to being called.
  1776. * 1. A kernel buffer is allocated to copy the user data into.
  1777. * 2. The port registers with "itself".
  1778. * 3. The transmit and receive exchange ids are obtained.
  1779. * 4. The receive exchange id is posted.
  1780. * 5. A new els loopback event is created.
  1781. * 6. The command and response iocbs are allocated.
  1782. * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
  1783. *
  1784. * This function is meant to be called n times while the port is in loopback
  1785. * so it is the apps responsibility to issue a reset to take the port out
  1786. * of loopback mode.
  1787. **/
  1788. static int
  1789. lpfc_bsg_diag_test(struct fc_bsg_job *job)
  1790. {
  1791. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  1792. struct lpfc_hba *phba = vport->phba;
  1793. struct diag_mode_test *diag_mode;
  1794. struct lpfc_bsg_event *evt;
  1795. struct event_data *evdat;
  1796. struct lpfc_sli *psli = &phba->sli;
  1797. uint32_t size;
  1798. uint32_t full_size;
  1799. size_t segment_len = 0, segment_offset = 0, current_offset = 0;
  1800. uint16_t rpi;
  1801. struct lpfc_iocbq *cmdiocbq, *rspiocbq;
  1802. IOCB_t *cmd, *rsp;
  1803. struct lpfc_sli_ct_request *ctreq;
  1804. struct lpfc_dmabuf *txbmp;
  1805. struct ulp_bde64 *txbpl = NULL;
  1806. struct lpfc_dmabufext *txbuffer = NULL;
  1807. struct list_head head;
  1808. struct lpfc_dmabuf *curr;
  1809. uint16_t txxri, rxxri;
  1810. uint32_t num_bde;
  1811. uint8_t *ptr = NULL, *rx_databuf = NULL;
  1812. int rc = 0;
  1813. unsigned long flags;
  1814. void *dataout = NULL;
  1815. uint32_t total_mem;
  1816. /* in case no data is returned return just the return code */
  1817. job->reply->reply_payload_rcv_len = 0;
  1818. if (job->request_len <
  1819. sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
  1820. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1821. "2739 Received DIAG TEST request below minimum "
  1822. "size\n");
  1823. rc = -EINVAL;
  1824. goto loopback_test_exit;
  1825. }
  1826. if (job->request_payload.payload_len !=
  1827. job->reply_payload.payload_len) {
  1828. rc = -EINVAL;
  1829. goto loopback_test_exit;
  1830. }
  1831. diag_mode = (struct diag_mode_test *)
  1832. job->request->rqst_data.h_vendor.vendor_cmd;
  1833. if ((phba->link_state == LPFC_HBA_ERROR) ||
  1834. (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
  1835. (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
  1836. rc = -EACCES;
  1837. goto loopback_test_exit;
  1838. }
  1839. if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
  1840. rc = -EACCES;
  1841. goto loopback_test_exit;
  1842. }
  1843. size = job->request_payload.payload_len;
  1844. full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
  1845. if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
  1846. rc = -ERANGE;
  1847. goto loopback_test_exit;
  1848. }
  1849. if (size >= BUF_SZ_4K) {
  1850. /*
  1851. * Allocate memory for ioctl data. If buffer is bigger than 64k,
  1852. * then we allocate 64k and re-use that buffer over and over to
  1853. * xfer the whole block. This is because Linux kernel has a
  1854. * problem allocating more than 120k of kernel space memory. Saw
  1855. * problem with GET_FCPTARGETMAPPING...
  1856. */
  1857. if (size <= (64 * 1024))
  1858. total_mem = size;
  1859. else
  1860. total_mem = 64 * 1024;
  1861. } else
  1862. /* Allocate memory for ioctl data */
  1863. total_mem = BUF_SZ_4K;
  1864. dataout = kmalloc(total_mem, GFP_KERNEL);
  1865. if (dataout == NULL) {
  1866. rc = -ENOMEM;
  1867. goto loopback_test_exit;
  1868. }
  1869. ptr = dataout;
  1870. ptr += ELX_LOOPBACK_HEADER_SZ;
  1871. sg_copy_to_buffer(job->request_payload.sg_list,
  1872. job->request_payload.sg_cnt,
  1873. ptr, size);
  1874. rc = lpfcdiag_loop_self_reg(phba, &rpi);
  1875. if (rc) {
  1876. rc = -ENOMEM;
  1877. goto loopback_test_exit;
  1878. }
  1879. rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
  1880. if (rc) {
  1881. lpfcdiag_loop_self_unreg(phba, rpi);
  1882. rc = -ENOMEM;
  1883. goto loopback_test_exit;
  1884. }
  1885. rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
  1886. if (rc) {
  1887. lpfcdiag_loop_self_unreg(phba, rpi);
  1888. rc = -ENOMEM;
  1889. goto loopback_test_exit;
  1890. }
  1891. evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
  1892. SLI_CT_ELX_LOOPBACK);
  1893. if (!evt) {
  1894. lpfcdiag_loop_self_unreg(phba, rpi);
  1895. rc = -ENOMEM;
  1896. goto loopback_test_exit;
  1897. }
  1898. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1899. list_add(&evt->node, &phba->ct_ev_waiters);
  1900. lpfc_bsg_event_ref(evt);
  1901. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1902. cmdiocbq = lpfc_sli_get_iocbq(phba);
  1903. rspiocbq = lpfc_sli_get_iocbq(phba);
  1904. txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  1905. if (txbmp) {
  1906. txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
  1907. INIT_LIST_HEAD(&txbmp->list);
  1908. txbpl = (struct ulp_bde64 *) txbmp->virt;
  1909. if (txbpl)
  1910. txbuffer = diag_cmd_data_alloc(phba,
  1911. txbpl, full_size, 0);
  1912. }
  1913. if (!cmdiocbq || !rspiocbq || !txbmp || !txbpl || !txbuffer) {
  1914. rc = -ENOMEM;
  1915. goto err_loopback_test_exit;
  1916. }
  1917. cmd = &cmdiocbq->iocb;
  1918. rsp = &rspiocbq->iocb;
  1919. INIT_LIST_HEAD(&head);
  1920. list_add_tail(&head, &txbuffer->dma.list);
  1921. list_for_each_entry(curr, &head, list) {
  1922. segment_len = ((struct lpfc_dmabufext *)curr)->size;
  1923. if (current_offset == 0) {
  1924. ctreq = curr->virt;
  1925. memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
  1926. ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
  1927. ctreq->RevisionId.bits.InId = 0;
  1928. ctreq->FsType = SLI_CT_ELX_LOOPBACK;
  1929. ctreq->FsSubType = 0;
  1930. ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
  1931. ctreq->CommandResponse.bits.Size = size;
  1932. segment_offset = ELX_LOOPBACK_HEADER_SZ;
  1933. } else
  1934. segment_offset = 0;
  1935. BUG_ON(segment_offset >= segment_len);
  1936. memcpy(curr->virt + segment_offset,
  1937. ptr + current_offset,
  1938. segment_len - segment_offset);
  1939. current_offset += segment_len - segment_offset;
  1940. BUG_ON(current_offset > size);
  1941. }
  1942. list_del(&head);
  1943. /* Build the XMIT_SEQUENCE iocb */
  1944. num_bde = (uint32_t)txbuffer->flag;
  1945. cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
  1946. cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
  1947. cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  1948. cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
  1949. cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
  1950. cmd->un.xseq64.w5.hcsw.Dfctl = 0;
  1951. cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
  1952. cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
  1953. cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
  1954. cmd->ulpBdeCount = 1;
  1955. cmd->ulpLe = 1;
  1956. cmd->ulpClass = CLASS3;
  1957. cmd->ulpContext = txxri;
  1958. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  1959. cmdiocbq->vport = phba->pport;
  1960. rc = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq, rspiocbq,
  1961. (phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT);
  1962. if ((rc != IOCB_SUCCESS) || (rsp->ulpStatus != IOCB_SUCCESS)) {
  1963. rc = -EIO;
  1964. goto err_loopback_test_exit;
  1965. }
  1966. evt->waiting = 1;
  1967. rc = wait_event_interruptible_timeout(
  1968. evt->wq, !list_empty(&evt->events_to_see),
  1969. ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
  1970. evt->waiting = 0;
  1971. if (list_empty(&evt->events_to_see))
  1972. rc = (rc) ? -EINTR : -ETIMEDOUT;
  1973. else {
  1974. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1975. list_move(evt->events_to_see.prev, &evt->events_to_get);
  1976. evdat = list_entry(evt->events_to_get.prev,
  1977. typeof(*evdat), node);
  1978. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1979. rx_databuf = evdat->data;
  1980. if (evdat->len != full_size) {
  1981. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  1982. "1603 Loopback test did not receive expected "
  1983. "data length. actual length 0x%x expected "
  1984. "length 0x%x\n",
  1985. evdat->len, full_size);
  1986. rc = -EIO;
  1987. } else if (rx_databuf == NULL)
  1988. rc = -EIO;
  1989. else {
  1990. rc = IOCB_SUCCESS;
  1991. /* skip over elx loopback header */
  1992. rx_databuf += ELX_LOOPBACK_HEADER_SZ;
  1993. job->reply->reply_payload_rcv_len =
  1994. sg_copy_from_buffer(job->reply_payload.sg_list,
  1995. job->reply_payload.sg_cnt,
  1996. rx_databuf, size);
  1997. job->reply->reply_payload_rcv_len = size;
  1998. }
  1999. }
  2000. err_loopback_test_exit:
  2001. lpfcdiag_loop_self_unreg(phba, rpi);
  2002. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2003. lpfc_bsg_event_unref(evt); /* release ref */
  2004. lpfc_bsg_event_unref(evt); /* delete */
  2005. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2006. if (cmdiocbq != NULL)
  2007. lpfc_sli_release_iocbq(phba, cmdiocbq);
  2008. if (rspiocbq != NULL)
  2009. lpfc_sli_release_iocbq(phba, rspiocbq);
  2010. if (txbmp != NULL) {
  2011. if (txbpl != NULL) {
  2012. if (txbuffer != NULL)
  2013. diag_cmd_data_free(phba, txbuffer);
  2014. lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
  2015. }
  2016. kfree(txbmp);
  2017. }
  2018. loopback_test_exit:
  2019. kfree(dataout);
  2020. /* make error code available to userspace */
  2021. job->reply->result = rc;
  2022. job->dd_data = NULL;
  2023. /* complete the job back to userspace if no error */
  2024. if (rc == 0)
  2025. job->job_done(job);
  2026. return rc;
  2027. }
  2028. /**
  2029. * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
  2030. * @job: GET_DFC_REV fc_bsg_job
  2031. **/
  2032. static int
  2033. lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
  2034. {
  2035. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  2036. struct lpfc_hba *phba = vport->phba;
  2037. struct get_mgmt_rev *event_req;
  2038. struct get_mgmt_rev_reply *event_reply;
  2039. int rc = 0;
  2040. if (job->request_len <
  2041. sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
  2042. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2043. "2740 Received GET_DFC_REV request below "
  2044. "minimum size\n");
  2045. rc = -EINVAL;
  2046. goto job_error;
  2047. }
  2048. event_req = (struct get_mgmt_rev *)
  2049. job->request->rqst_data.h_vendor.vendor_cmd;
  2050. event_reply = (struct get_mgmt_rev_reply *)
  2051. job->reply->reply_data.vendor_reply.vendor_rsp;
  2052. if (job->reply_len <
  2053. sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
  2054. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2055. "2741 Received GET_DFC_REV reply below "
  2056. "minimum size\n");
  2057. rc = -EINVAL;
  2058. goto job_error;
  2059. }
  2060. event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
  2061. event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
  2062. job_error:
  2063. job->reply->result = rc;
  2064. if (rc == 0)
  2065. job->job_done(job);
  2066. return rc;
  2067. }
  2068. /**
  2069. * lpfc_bsg_wake_mbox_wait - lpfc_bsg_issue_mbox mbox completion handler
  2070. * @phba: Pointer to HBA context object.
  2071. * @pmboxq: Pointer to mailbox command.
  2072. *
  2073. * This is completion handler function for mailbox commands issued from
  2074. * lpfc_bsg_issue_mbox function. This function is called by the
  2075. * mailbox event handler function with no lock held. This function
  2076. * will wake up thread waiting on the wait queue pointed by context1
  2077. * of the mailbox.
  2078. **/
  2079. void
  2080. lpfc_bsg_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  2081. {
  2082. struct bsg_job_data *dd_data;
  2083. MAILBOX_t *pmb;
  2084. MAILBOX_t *mb;
  2085. struct fc_bsg_job *job;
  2086. uint32_t size;
  2087. unsigned long flags;
  2088. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2089. dd_data = pmboxq->context1;
  2090. if (!dd_data) {
  2091. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2092. return;
  2093. }
  2094. pmb = &dd_data->context_un.mbox.pmboxq->u.mb;
  2095. mb = dd_data->context_un.mbox.mb;
  2096. job = dd_data->context_un.mbox.set_job;
  2097. memcpy(mb, pmb, sizeof(*pmb));
  2098. size = job->request_payload.payload_len;
  2099. job->reply->reply_payload_rcv_len =
  2100. sg_copy_from_buffer(job->reply_payload.sg_list,
  2101. job->reply_payload.sg_cnt,
  2102. mb, size);
  2103. job->reply->result = 0;
  2104. dd_data->context_un.mbox.set_job = NULL;
  2105. job->dd_data = NULL;
  2106. job->job_done(job);
  2107. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2108. mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
  2109. kfree(mb);
  2110. kfree(dd_data);
  2111. return;
  2112. }
  2113. /**
  2114. * lpfc_bsg_check_cmd_access - test for a supported mailbox command
  2115. * @phba: Pointer to HBA context object.
  2116. * @mb: Pointer to a mailbox object.
  2117. * @vport: Pointer to a vport object.
  2118. *
  2119. * Some commands require the port to be offline, some may not be called from
  2120. * the application.
  2121. **/
  2122. static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
  2123. MAILBOX_t *mb, struct lpfc_vport *vport)
  2124. {
  2125. /* return negative error values for bsg job */
  2126. switch (mb->mbxCommand) {
  2127. /* Offline only */
  2128. case MBX_INIT_LINK:
  2129. case MBX_DOWN_LINK:
  2130. case MBX_CONFIG_LINK:
  2131. case MBX_CONFIG_RING:
  2132. case MBX_RESET_RING:
  2133. case MBX_UNREG_LOGIN:
  2134. case MBX_CLEAR_LA:
  2135. case MBX_DUMP_CONTEXT:
  2136. case MBX_RUN_DIAGS:
  2137. case MBX_RESTART:
  2138. case MBX_SET_MASK:
  2139. if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
  2140. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2141. "2743 Command 0x%x is illegal in on-line "
  2142. "state\n",
  2143. mb->mbxCommand);
  2144. return -EPERM;
  2145. }
  2146. case MBX_WRITE_NV:
  2147. case MBX_WRITE_VPARMS:
  2148. case MBX_LOAD_SM:
  2149. case MBX_READ_NV:
  2150. case MBX_READ_CONFIG:
  2151. case MBX_READ_RCONFIG:
  2152. case MBX_READ_STATUS:
  2153. case MBX_READ_XRI:
  2154. case MBX_READ_REV:
  2155. case MBX_READ_LNK_STAT:
  2156. case MBX_DUMP_MEMORY:
  2157. case MBX_DOWN_LOAD:
  2158. case MBX_UPDATE_CFG:
  2159. case MBX_KILL_BOARD:
  2160. case MBX_LOAD_AREA:
  2161. case MBX_LOAD_EXP_ROM:
  2162. case MBX_BEACON:
  2163. case MBX_DEL_LD_ENTRY:
  2164. case MBX_SET_DEBUG:
  2165. case MBX_WRITE_WWN:
  2166. case MBX_SLI4_CONFIG:
  2167. case MBX_READ_EVENT_LOG_STATUS:
  2168. case MBX_WRITE_EVENT_LOG:
  2169. case MBX_PORT_CAPABILITIES:
  2170. case MBX_PORT_IOV_CONTROL:
  2171. break;
  2172. case MBX_SET_VARIABLE:
  2173. case MBX_RUN_BIU_DIAG64:
  2174. case MBX_READ_EVENT_LOG:
  2175. case MBX_READ_SPARM64:
  2176. case MBX_READ_LA:
  2177. case MBX_READ_LA64:
  2178. case MBX_REG_LOGIN:
  2179. case MBX_REG_LOGIN64:
  2180. case MBX_CONFIG_PORT:
  2181. case MBX_RUN_BIU_DIAG:
  2182. default:
  2183. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2184. "2742 Unknown Command 0x%x\n",
  2185. mb->mbxCommand);
  2186. return -EPERM;
  2187. }
  2188. return 0; /* ok */
  2189. }
  2190. /**
  2191. * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
  2192. * @phba: Pointer to HBA context object.
  2193. * @mb: Pointer to a mailbox object.
  2194. * @vport: Pointer to a vport object.
  2195. *
  2196. * Allocate a tracking object, mailbox command memory, get a mailbox
  2197. * from the mailbox pool, copy the caller mailbox command.
  2198. *
  2199. * If offline and the sli is active we need to poll for the command (port is
  2200. * being reset) and com-plete the job, otherwise issue the mailbox command and
  2201. * let our completion handler finish the command.
  2202. **/
  2203. static uint32_t
  2204. lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
  2205. struct lpfc_vport *vport)
  2206. {
  2207. LPFC_MBOXQ_t *pmboxq;
  2208. MAILBOX_t *pmb;
  2209. MAILBOX_t *mb;
  2210. struct bsg_job_data *dd_data;
  2211. uint32_t size;
  2212. int rc = 0;
  2213. /* allocate our bsg tracking structure */
  2214. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  2215. if (!dd_data) {
  2216. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2217. "2727 Failed allocation of dd_data\n");
  2218. return -ENOMEM;
  2219. }
  2220. mb = kzalloc(PAGE_SIZE, GFP_KERNEL);
  2221. if (!mb) {
  2222. kfree(dd_data);
  2223. return -ENOMEM;
  2224. }
  2225. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2226. if (!pmboxq) {
  2227. kfree(dd_data);
  2228. kfree(mb);
  2229. return -ENOMEM;
  2230. }
  2231. size = job->request_payload.payload_len;
  2232. job->reply->reply_payload_rcv_len =
  2233. sg_copy_to_buffer(job->request_payload.sg_list,
  2234. job->request_payload.sg_cnt,
  2235. mb, size);
  2236. rc = lpfc_bsg_check_cmd_access(phba, mb, vport);
  2237. if (rc != 0) {
  2238. kfree(dd_data);
  2239. kfree(mb);
  2240. mempool_free(pmboxq, phba->mbox_mem_pool);
  2241. return rc; /* must be negative */
  2242. }
  2243. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  2244. pmb = &pmboxq->u.mb;
  2245. memcpy(pmb, mb, sizeof(*pmb));
  2246. pmb->mbxOwner = OWN_HOST;
  2247. pmboxq->context1 = NULL;
  2248. pmboxq->vport = vport;
  2249. if ((vport->fc_flag & FC_OFFLINE_MODE) ||
  2250. (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
  2251. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
  2252. if (rc != MBX_SUCCESS) {
  2253. if (rc != MBX_TIMEOUT) {
  2254. kfree(dd_data);
  2255. kfree(mb);
  2256. mempool_free(pmboxq, phba->mbox_mem_pool);
  2257. }
  2258. return (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
  2259. }
  2260. memcpy(mb, pmb, sizeof(*pmb));
  2261. job->reply->reply_payload_rcv_len =
  2262. sg_copy_from_buffer(job->reply_payload.sg_list,
  2263. job->reply_payload.sg_cnt,
  2264. mb, size);
  2265. kfree(dd_data);
  2266. kfree(mb);
  2267. mempool_free(pmboxq, phba->mbox_mem_pool);
  2268. /* not waiting mbox already done */
  2269. return 0;
  2270. }
  2271. /* setup wake call as IOCB callback */
  2272. pmboxq->mbox_cmpl = lpfc_bsg_wake_mbox_wait;
  2273. /* setup context field to pass wait_queue pointer to wake function */
  2274. pmboxq->context1 = dd_data;
  2275. dd_data->type = TYPE_MBOX;
  2276. dd_data->context_un.mbox.pmboxq = pmboxq;
  2277. dd_data->context_un.mbox.mb = mb;
  2278. dd_data->context_un.mbox.set_job = job;
  2279. job->dd_data = dd_data;
  2280. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  2281. if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
  2282. kfree(dd_data);
  2283. kfree(mb);
  2284. mempool_free(pmboxq, phba->mbox_mem_pool);
  2285. return -EIO;
  2286. }
  2287. return 1;
  2288. }
  2289. /**
  2290. * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
  2291. * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
  2292. **/
  2293. static int
  2294. lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
  2295. {
  2296. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  2297. struct lpfc_hba *phba = vport->phba;
  2298. int rc = 0;
  2299. /* in case no data is transferred */
  2300. job->reply->reply_payload_rcv_len = 0;
  2301. if (job->request_len <
  2302. sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
  2303. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2304. "2737 Received MBOX_REQ request below "
  2305. "minimum size\n");
  2306. rc = -EINVAL;
  2307. goto job_error;
  2308. }
  2309. if (job->request_payload.payload_len != PAGE_SIZE) {
  2310. rc = -EINVAL;
  2311. goto job_error;
  2312. }
  2313. if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
  2314. rc = -EAGAIN;
  2315. goto job_error;
  2316. }
  2317. rc = lpfc_bsg_issue_mbox(phba, job, vport);
  2318. job_error:
  2319. if (rc == 0) {
  2320. /* job done */
  2321. job->reply->result = 0;
  2322. job->dd_data = NULL;
  2323. job->job_done(job);
  2324. } else if (rc == 1)
  2325. /* job submitted, will complete later*/
  2326. rc = 0; /* return zero, no error */
  2327. else {
  2328. /* some error occurred */
  2329. job->reply->result = rc;
  2330. job->dd_data = NULL;
  2331. }
  2332. return rc;
  2333. }
  2334. /**
  2335. * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
  2336. * @job: fc_bsg_job to handle
  2337. **/
  2338. static int
  2339. lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
  2340. {
  2341. int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
  2342. int rc;
  2343. switch (command) {
  2344. case LPFC_BSG_VENDOR_SET_CT_EVENT:
  2345. rc = lpfc_bsg_hba_set_event(job);
  2346. break;
  2347. case LPFC_BSG_VENDOR_GET_CT_EVENT:
  2348. rc = lpfc_bsg_hba_get_event(job);
  2349. break;
  2350. case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
  2351. rc = lpfc_bsg_send_mgmt_rsp(job);
  2352. break;
  2353. case LPFC_BSG_VENDOR_DIAG_MODE:
  2354. rc = lpfc_bsg_diag_mode(job);
  2355. break;
  2356. case LPFC_BSG_VENDOR_DIAG_TEST:
  2357. rc = lpfc_bsg_diag_test(job);
  2358. break;
  2359. case LPFC_BSG_VENDOR_GET_MGMT_REV:
  2360. rc = lpfc_bsg_get_dfc_rev(job);
  2361. break;
  2362. case LPFC_BSG_VENDOR_MBOX:
  2363. rc = lpfc_bsg_mbox_cmd(job);
  2364. break;
  2365. default:
  2366. rc = -EINVAL;
  2367. job->reply->reply_payload_rcv_len = 0;
  2368. /* make error code available to userspace */
  2369. job->reply->result = rc;
  2370. break;
  2371. }
  2372. return rc;
  2373. }
  2374. /**
  2375. * lpfc_bsg_request - handle a bsg request from the FC transport
  2376. * @job: fc_bsg_job to handle
  2377. **/
  2378. int
  2379. lpfc_bsg_request(struct fc_bsg_job *job)
  2380. {
  2381. uint32_t msgcode;
  2382. int rc;
  2383. msgcode = job->request->msgcode;
  2384. switch (msgcode) {
  2385. case FC_BSG_HST_VENDOR:
  2386. rc = lpfc_bsg_hst_vendor(job);
  2387. break;
  2388. case FC_BSG_RPT_ELS:
  2389. rc = lpfc_bsg_rport_els(job);
  2390. break;
  2391. case FC_BSG_RPT_CT:
  2392. rc = lpfc_bsg_send_mgmt_cmd(job);
  2393. break;
  2394. default:
  2395. rc = -EINVAL;
  2396. job->reply->reply_payload_rcv_len = 0;
  2397. /* make error code available to userspace */
  2398. job->reply->result = rc;
  2399. break;
  2400. }
  2401. return rc;
  2402. }
  2403. /**
  2404. * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
  2405. * @job: fc_bsg_job that has timed out
  2406. *
  2407. * This function just aborts the job's IOCB. The aborted IOCB will return to
  2408. * the waiting function which will handle passing the error back to userspace
  2409. **/
  2410. int
  2411. lpfc_bsg_timeout(struct fc_bsg_job *job)
  2412. {
  2413. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  2414. struct lpfc_hba *phba = vport->phba;
  2415. struct lpfc_iocbq *cmdiocb;
  2416. struct lpfc_bsg_event *evt;
  2417. struct lpfc_bsg_iocb *iocb;
  2418. struct lpfc_bsg_mbox *mbox;
  2419. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  2420. struct bsg_job_data *dd_data;
  2421. unsigned long flags;
  2422. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2423. dd_data = (struct bsg_job_data *)job->dd_data;
  2424. /* timeout and completion crossed paths if no dd_data */
  2425. if (!dd_data) {
  2426. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2427. return 0;
  2428. }
  2429. switch (dd_data->type) {
  2430. case TYPE_IOCB:
  2431. iocb = &dd_data->context_un.iocb;
  2432. cmdiocb = iocb->cmdiocbq;
  2433. /* hint to completion handler that the job timed out */
  2434. job->reply->result = -EAGAIN;
  2435. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2436. /* this will call our completion handler */
  2437. spin_lock_irq(&phba->hbalock);
  2438. lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
  2439. spin_unlock_irq(&phba->hbalock);
  2440. break;
  2441. case TYPE_EVT:
  2442. evt = dd_data->context_un.evt;
  2443. /* this event has no job anymore */
  2444. evt->set_job = NULL;
  2445. job->dd_data = NULL;
  2446. job->reply->reply_payload_rcv_len = 0;
  2447. /* Return -EAGAIN which is our way of signallying the
  2448. * app to retry.
  2449. */
  2450. job->reply->result = -EAGAIN;
  2451. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2452. job->job_done(job);
  2453. break;
  2454. case TYPE_MBOX:
  2455. mbox = &dd_data->context_un.mbox;
  2456. /* this mbox has no job anymore */
  2457. mbox->set_job = NULL;
  2458. job->dd_data = NULL;
  2459. job->reply->reply_payload_rcv_len = 0;
  2460. job->reply->result = -EAGAIN;
  2461. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2462. job->job_done(job);
  2463. break;
  2464. default:
  2465. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2466. break;
  2467. }
  2468. /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
  2469. * otherwise an error message will be displayed on the console
  2470. * so always return success (zero)
  2471. */
  2472. return 0;
  2473. }