lpfc_bsg.c 97 KB

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