lpfc_bsg.c 98 KB

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