lpfc_bsg.c 97 KB

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