lpfc_bsg.c 97 KB

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