lpfc_bsg.c 97 KB

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