be_main.c 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136
  1. /**
  2. * Copyright (C) 2005 - 2010 ServerEngines
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation. The full GNU General
  8. * Public License is included in this distribution in the file called COPYING.
  9. *
  10. * Written by: Jayamohan Kallickal (jayamohank@serverengines.com)
  11. *
  12. * Contact Information:
  13. * linux-drivers@serverengines.com
  14. *
  15. * ServerEngines
  16. * 209 N. Fair Oaks Ave
  17. * Sunnyvale, CA 94085
  18. *
  19. */
  20. #include <linux/reboot.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/pci.h>
  26. #include <linux/string.h>
  27. #include <linux/kernel.h>
  28. #include <linux/semaphore.h>
  29. #include <scsi/libiscsi.h>
  30. #include <scsi/scsi_transport_iscsi.h>
  31. #include <scsi/scsi_transport.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi.h>
  36. #include "be_main.h"
  37. #include "be_iscsi.h"
  38. #include "be_mgmt.h"
  39. static unsigned int be_iopoll_budget = 10;
  40. static unsigned int be_max_phys_size = 64;
  41. static unsigned int enable_msix = 1;
  42. static unsigned int gcrashmode = 0;
  43. static unsigned int num_hba = 0;
  44. MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
  45. MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
  46. MODULE_AUTHOR("ServerEngines Corporation");
  47. MODULE_LICENSE("GPL");
  48. module_param(be_iopoll_budget, int, 0);
  49. module_param(enable_msix, int, 0);
  50. module_param(be_max_phys_size, uint, S_IRUGO);
  51. MODULE_PARM_DESC(be_max_phys_size, "Maximum Size (In Kilobytes) of physically"
  52. "contiguous memory that can be allocated."
  53. "Range is 16 - 128");
  54. static int beiscsi_slave_configure(struct scsi_device *sdev)
  55. {
  56. blk_queue_max_segment_size(sdev->request_queue, 65536);
  57. return 0;
  58. }
  59. static int beiscsi_eh_abort(struct scsi_cmnd *sc)
  60. {
  61. struct iscsi_cls_session *cls_session;
  62. struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
  63. struct beiscsi_io_task *aborted_io_task;
  64. struct iscsi_conn *conn;
  65. struct beiscsi_conn *beiscsi_conn;
  66. struct beiscsi_hba *phba;
  67. struct iscsi_session *session;
  68. struct invalidate_command_table *inv_tbl;
  69. struct be_dma_mem nonemb_cmd;
  70. unsigned int cid, tag, num_invalidate;
  71. cls_session = starget_to_session(scsi_target(sc->device));
  72. session = cls_session->dd_data;
  73. spin_lock_bh(&session->lock);
  74. if (!aborted_task || !aborted_task->sc) {
  75. /* we raced */
  76. spin_unlock_bh(&session->lock);
  77. return SUCCESS;
  78. }
  79. aborted_io_task = aborted_task->dd_data;
  80. if (!aborted_io_task->scsi_cmnd) {
  81. /* raced or invalid command */
  82. spin_unlock_bh(&session->lock);
  83. return SUCCESS;
  84. }
  85. spin_unlock_bh(&session->lock);
  86. conn = aborted_task->conn;
  87. beiscsi_conn = conn->dd_data;
  88. phba = beiscsi_conn->phba;
  89. /* invalidate iocb */
  90. cid = beiscsi_conn->beiscsi_conn_cid;
  91. inv_tbl = phba->inv_tbl;
  92. memset(inv_tbl, 0x0, sizeof(*inv_tbl));
  93. inv_tbl->cid = cid;
  94. inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
  95. num_invalidate = 1;
  96. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  97. sizeof(struct invalidate_commands_params_in),
  98. &nonemb_cmd.dma);
  99. if (nonemb_cmd.va == NULL) {
  100. SE_DEBUG(DBG_LVL_1,
  101. "Failed to allocate memory for"
  102. "mgmt_invalidate_icds\n");
  103. return FAILED;
  104. }
  105. nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
  106. tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
  107. cid, &nonemb_cmd);
  108. if (!tag) {
  109. shost_printk(KERN_WARNING, phba->shost,
  110. "mgmt_invalidate_icds could not be"
  111. " submitted\n");
  112. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  113. nonemb_cmd.va, nonemb_cmd.dma);
  114. return FAILED;
  115. } else {
  116. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  117. phba->ctrl.mcc_numtag[tag]);
  118. free_mcc_tag(&phba->ctrl, tag);
  119. }
  120. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  121. nonemb_cmd.va, nonemb_cmd.dma);
  122. return iscsi_eh_abort(sc);
  123. }
  124. static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
  125. {
  126. struct iscsi_task *abrt_task;
  127. struct beiscsi_io_task *abrt_io_task;
  128. struct iscsi_conn *conn;
  129. struct beiscsi_conn *beiscsi_conn;
  130. struct beiscsi_hba *phba;
  131. struct iscsi_session *session;
  132. struct iscsi_cls_session *cls_session;
  133. struct invalidate_command_table *inv_tbl;
  134. struct be_dma_mem nonemb_cmd;
  135. unsigned int cid, tag, i, num_invalidate;
  136. int rc = FAILED;
  137. /* invalidate iocbs */
  138. cls_session = starget_to_session(scsi_target(sc->device));
  139. session = cls_session->dd_data;
  140. spin_lock_bh(&session->lock);
  141. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  142. goto unlock;
  143. conn = session->leadconn;
  144. beiscsi_conn = conn->dd_data;
  145. phba = beiscsi_conn->phba;
  146. cid = beiscsi_conn->beiscsi_conn_cid;
  147. inv_tbl = phba->inv_tbl;
  148. memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
  149. num_invalidate = 0;
  150. for (i = 0; i < conn->session->cmds_max; i++) {
  151. abrt_task = conn->session->cmds[i];
  152. abrt_io_task = abrt_task->dd_data;
  153. if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
  154. continue;
  155. if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
  156. continue;
  157. inv_tbl->cid = cid;
  158. inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
  159. num_invalidate++;
  160. inv_tbl++;
  161. }
  162. spin_unlock_bh(&session->lock);
  163. inv_tbl = phba->inv_tbl;
  164. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  165. sizeof(struct invalidate_commands_params_in),
  166. &nonemb_cmd.dma);
  167. if (nonemb_cmd.va == NULL) {
  168. SE_DEBUG(DBG_LVL_1,
  169. "Failed to allocate memory for"
  170. "mgmt_invalidate_icds\n");
  171. return FAILED;
  172. }
  173. nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
  174. memset(nonemb_cmd.va, 0, nonemb_cmd.size);
  175. tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
  176. cid, &nonemb_cmd);
  177. if (!tag) {
  178. shost_printk(KERN_WARNING, phba->shost,
  179. "mgmt_invalidate_icds could not be"
  180. " submitted\n");
  181. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  182. nonemb_cmd.va, nonemb_cmd.dma);
  183. return FAILED;
  184. } else {
  185. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  186. phba->ctrl.mcc_numtag[tag]);
  187. free_mcc_tag(&phba->ctrl, tag);
  188. }
  189. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  190. nonemb_cmd.va, nonemb_cmd.dma);
  191. return iscsi_eh_device_reset(sc);
  192. unlock:
  193. spin_unlock_bh(&session->lock);
  194. return rc;
  195. }
  196. /*------------------- PCI Driver operations and data ----------------- */
  197. static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
  198. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
  199. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
  200. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
  201. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
  202. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
  203. { 0 }
  204. };
  205. MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
  206. static struct scsi_host_template beiscsi_sht = {
  207. .module = THIS_MODULE,
  208. .name = "ServerEngines 10Gbe open-iscsi Initiator Driver",
  209. .proc_name = DRV_NAME,
  210. .queuecommand = iscsi_queuecommand,
  211. .change_queue_depth = iscsi_change_queue_depth,
  212. .slave_configure = beiscsi_slave_configure,
  213. .target_alloc = iscsi_target_alloc,
  214. .eh_abort_handler = beiscsi_eh_abort,
  215. .eh_device_reset_handler = beiscsi_eh_device_reset,
  216. .eh_target_reset_handler = iscsi_eh_session_reset,
  217. .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
  218. .can_queue = BE2_IO_DEPTH,
  219. .this_id = -1,
  220. .max_sectors = BEISCSI_MAX_SECTORS,
  221. .cmd_per_lun = BEISCSI_CMD_PER_LUN,
  222. .use_clustering = ENABLE_CLUSTERING,
  223. };
  224. static struct scsi_transport_template *beiscsi_scsi_transport;
  225. static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
  226. {
  227. struct beiscsi_hba *phba;
  228. struct Scsi_Host *shost;
  229. shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
  230. if (!shost) {
  231. dev_err(&pcidev->dev, "beiscsi_hba_alloc -"
  232. "iscsi_host_alloc failed\n");
  233. return NULL;
  234. }
  235. shost->dma_boundary = pcidev->dma_mask;
  236. shost->max_id = BE2_MAX_SESSIONS;
  237. shost->max_channel = 0;
  238. shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
  239. shost->max_lun = BEISCSI_NUM_MAX_LUN;
  240. shost->transportt = beiscsi_scsi_transport;
  241. phba = iscsi_host_priv(shost);
  242. memset(phba, 0, sizeof(*phba));
  243. phba->shost = shost;
  244. phba->pcidev = pci_dev_get(pcidev);
  245. pci_set_drvdata(pcidev, phba);
  246. if (iscsi_host_add(shost, &phba->pcidev->dev))
  247. goto free_devices;
  248. return phba;
  249. free_devices:
  250. pci_dev_put(phba->pcidev);
  251. iscsi_host_free(phba->shost);
  252. return NULL;
  253. }
  254. static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
  255. {
  256. if (phba->csr_va) {
  257. iounmap(phba->csr_va);
  258. phba->csr_va = NULL;
  259. }
  260. if (phba->db_va) {
  261. iounmap(phba->db_va);
  262. phba->db_va = NULL;
  263. }
  264. if (phba->pci_va) {
  265. iounmap(phba->pci_va);
  266. phba->pci_va = NULL;
  267. }
  268. }
  269. static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
  270. struct pci_dev *pcidev)
  271. {
  272. u8 __iomem *addr;
  273. int pcicfg_reg;
  274. addr = ioremap_nocache(pci_resource_start(pcidev, 2),
  275. pci_resource_len(pcidev, 2));
  276. if (addr == NULL)
  277. return -ENOMEM;
  278. phba->ctrl.csr = addr;
  279. phba->csr_va = addr;
  280. phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
  281. addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
  282. if (addr == NULL)
  283. goto pci_map_err;
  284. phba->ctrl.db = addr;
  285. phba->db_va = addr;
  286. phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
  287. if (phba->generation == BE_GEN2)
  288. pcicfg_reg = 1;
  289. else
  290. pcicfg_reg = 0;
  291. addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
  292. pci_resource_len(pcidev, pcicfg_reg));
  293. if (addr == NULL)
  294. goto pci_map_err;
  295. phba->ctrl.pcicfg = addr;
  296. phba->pci_va = addr;
  297. phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
  298. return 0;
  299. pci_map_err:
  300. beiscsi_unmap_pci_function(phba);
  301. return -ENOMEM;
  302. }
  303. static int beiscsi_enable_pci(struct pci_dev *pcidev)
  304. {
  305. int ret;
  306. ret = pci_enable_device(pcidev);
  307. if (ret) {
  308. dev_err(&pcidev->dev, "beiscsi_enable_pci - enable device "
  309. "failed. Returning -ENODEV\n");
  310. return ret;
  311. }
  312. pci_set_master(pcidev);
  313. if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
  314. ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
  315. if (ret) {
  316. dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
  317. pci_disable_device(pcidev);
  318. return ret;
  319. }
  320. }
  321. return 0;
  322. }
  323. static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
  324. {
  325. struct be_ctrl_info *ctrl = &phba->ctrl;
  326. struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
  327. struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
  328. int status = 0;
  329. ctrl->pdev = pdev;
  330. status = beiscsi_map_pci_bars(phba, pdev);
  331. if (status)
  332. return status;
  333. mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
  334. mbox_mem_alloc->va = pci_alloc_consistent(pdev,
  335. mbox_mem_alloc->size,
  336. &mbox_mem_alloc->dma);
  337. if (!mbox_mem_alloc->va) {
  338. beiscsi_unmap_pci_function(phba);
  339. status = -ENOMEM;
  340. return status;
  341. }
  342. mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
  343. mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
  344. mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
  345. memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
  346. spin_lock_init(&ctrl->mbox_lock);
  347. spin_lock_init(&phba->ctrl.mcc_lock);
  348. spin_lock_init(&phba->ctrl.mcc_cq_lock);
  349. return status;
  350. }
  351. static void beiscsi_get_params(struct beiscsi_hba *phba)
  352. {
  353. phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
  354. - (phba->fw_config.iscsi_cid_count
  355. + BE2_TMFS
  356. + BE2_NOPOUT_REQ));
  357. phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
  358. phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
  359. phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;;
  360. phba->params.num_sge_per_io = BE2_SGE;
  361. phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
  362. phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
  363. phba->params.eq_timer = 64;
  364. phba->params.num_eq_entries =
  365. (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
  366. + BE2_TMFS) / 512) + 1) * 512;
  367. phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
  368. ? 1024 : phba->params.num_eq_entries;
  369. SE_DEBUG(DBG_LVL_8, "phba->params.num_eq_entries=%d\n",
  370. phba->params.num_eq_entries);
  371. phba->params.num_cq_entries =
  372. (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
  373. + BE2_TMFS) / 512) + 1) * 512;
  374. phba->params.wrbs_per_cxn = 256;
  375. }
  376. static void hwi_ring_eq_db(struct beiscsi_hba *phba,
  377. unsigned int id, unsigned int clr_interrupt,
  378. unsigned int num_processed,
  379. unsigned char rearm, unsigned char event)
  380. {
  381. u32 val = 0;
  382. val |= id & DB_EQ_RING_ID_MASK;
  383. if (rearm)
  384. val |= 1 << DB_EQ_REARM_SHIFT;
  385. if (clr_interrupt)
  386. val |= 1 << DB_EQ_CLR_SHIFT;
  387. if (event)
  388. val |= 1 << DB_EQ_EVNT_SHIFT;
  389. val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
  390. iowrite32(val, phba->db_va + DB_EQ_OFFSET);
  391. }
  392. /**
  393. * be_isr_mcc - The isr routine of the driver.
  394. * @irq: Not used
  395. * @dev_id: Pointer to host adapter structure
  396. */
  397. static irqreturn_t be_isr_mcc(int irq, void *dev_id)
  398. {
  399. struct beiscsi_hba *phba;
  400. struct be_eq_entry *eqe = NULL;
  401. struct be_queue_info *eq;
  402. struct be_queue_info *mcc;
  403. unsigned int num_eq_processed;
  404. struct be_eq_obj *pbe_eq;
  405. unsigned long flags;
  406. pbe_eq = dev_id;
  407. eq = &pbe_eq->q;
  408. phba = pbe_eq->phba;
  409. mcc = &phba->ctrl.mcc_obj.cq;
  410. eqe = queue_tail_node(eq);
  411. if (!eqe)
  412. SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
  413. num_eq_processed = 0;
  414. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  415. & EQE_VALID_MASK) {
  416. if (((eqe->dw[offsetof(struct amap_eq_entry,
  417. resource_id) / 32] &
  418. EQE_RESID_MASK) >> 16) == mcc->id) {
  419. spin_lock_irqsave(&phba->isr_lock, flags);
  420. phba->todo_mcc_cq = 1;
  421. spin_unlock_irqrestore(&phba->isr_lock, flags);
  422. }
  423. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  424. queue_tail_inc(eq);
  425. eqe = queue_tail_node(eq);
  426. num_eq_processed++;
  427. }
  428. if (phba->todo_mcc_cq)
  429. queue_work(phba->wq, &phba->work_cqs);
  430. if (num_eq_processed)
  431. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
  432. return IRQ_HANDLED;
  433. }
  434. /**
  435. * be_isr_msix - The isr routine of the driver.
  436. * @irq: Not used
  437. * @dev_id: Pointer to host adapter structure
  438. */
  439. static irqreturn_t be_isr_msix(int irq, void *dev_id)
  440. {
  441. struct beiscsi_hba *phba;
  442. struct be_eq_entry *eqe = NULL;
  443. struct be_queue_info *eq;
  444. struct be_queue_info *cq;
  445. unsigned int num_eq_processed;
  446. struct be_eq_obj *pbe_eq;
  447. unsigned long flags;
  448. pbe_eq = dev_id;
  449. eq = &pbe_eq->q;
  450. cq = pbe_eq->cq;
  451. eqe = queue_tail_node(eq);
  452. if (!eqe)
  453. SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
  454. phba = pbe_eq->phba;
  455. num_eq_processed = 0;
  456. if (blk_iopoll_enabled) {
  457. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  458. & EQE_VALID_MASK) {
  459. if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
  460. blk_iopoll_sched(&pbe_eq->iopoll);
  461. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  462. queue_tail_inc(eq);
  463. eqe = queue_tail_node(eq);
  464. num_eq_processed++;
  465. }
  466. if (num_eq_processed)
  467. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
  468. return IRQ_HANDLED;
  469. } else {
  470. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  471. & EQE_VALID_MASK) {
  472. spin_lock_irqsave(&phba->isr_lock, flags);
  473. phba->todo_cq = 1;
  474. spin_unlock_irqrestore(&phba->isr_lock, flags);
  475. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  476. queue_tail_inc(eq);
  477. eqe = queue_tail_node(eq);
  478. num_eq_processed++;
  479. }
  480. if (phba->todo_cq)
  481. queue_work(phba->wq, &phba->work_cqs);
  482. if (num_eq_processed)
  483. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
  484. return IRQ_HANDLED;
  485. }
  486. }
  487. /**
  488. * be_isr - The isr routine of the driver.
  489. * @irq: Not used
  490. * @dev_id: Pointer to host adapter structure
  491. */
  492. static irqreturn_t be_isr(int irq, void *dev_id)
  493. {
  494. struct beiscsi_hba *phba;
  495. struct hwi_controller *phwi_ctrlr;
  496. struct hwi_context_memory *phwi_context;
  497. struct be_eq_entry *eqe = NULL;
  498. struct be_queue_info *eq;
  499. struct be_queue_info *cq;
  500. struct be_queue_info *mcc;
  501. unsigned long flags, index;
  502. unsigned int num_mcceq_processed, num_ioeq_processed;
  503. struct be_ctrl_info *ctrl;
  504. struct be_eq_obj *pbe_eq;
  505. int isr;
  506. phba = dev_id;
  507. ctrl = &phba->ctrl;;
  508. isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
  509. (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
  510. if (!isr)
  511. return IRQ_NONE;
  512. phwi_ctrlr = phba->phwi_ctrlr;
  513. phwi_context = phwi_ctrlr->phwi_ctxt;
  514. pbe_eq = &phwi_context->be_eq[0];
  515. eq = &phwi_context->be_eq[0].q;
  516. mcc = &phba->ctrl.mcc_obj.cq;
  517. index = 0;
  518. eqe = queue_tail_node(eq);
  519. if (!eqe)
  520. SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
  521. num_ioeq_processed = 0;
  522. num_mcceq_processed = 0;
  523. if (blk_iopoll_enabled) {
  524. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  525. & EQE_VALID_MASK) {
  526. if (((eqe->dw[offsetof(struct amap_eq_entry,
  527. resource_id) / 32] &
  528. EQE_RESID_MASK) >> 16) == mcc->id) {
  529. spin_lock_irqsave(&phba->isr_lock, flags);
  530. phba->todo_mcc_cq = 1;
  531. spin_unlock_irqrestore(&phba->isr_lock, flags);
  532. num_mcceq_processed++;
  533. } else {
  534. if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
  535. blk_iopoll_sched(&pbe_eq->iopoll);
  536. num_ioeq_processed++;
  537. }
  538. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  539. queue_tail_inc(eq);
  540. eqe = queue_tail_node(eq);
  541. }
  542. if (num_ioeq_processed || num_mcceq_processed) {
  543. if (phba->todo_mcc_cq)
  544. queue_work(phba->wq, &phba->work_cqs);
  545. if ((num_mcceq_processed) && (!num_ioeq_processed))
  546. hwi_ring_eq_db(phba, eq->id, 0,
  547. (num_ioeq_processed +
  548. num_mcceq_processed) , 1, 1);
  549. else
  550. hwi_ring_eq_db(phba, eq->id, 0,
  551. (num_ioeq_processed +
  552. num_mcceq_processed), 0, 1);
  553. return IRQ_HANDLED;
  554. } else
  555. return IRQ_NONE;
  556. } else {
  557. cq = &phwi_context->be_cq[0];
  558. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  559. & EQE_VALID_MASK) {
  560. if (((eqe->dw[offsetof(struct amap_eq_entry,
  561. resource_id) / 32] &
  562. EQE_RESID_MASK) >> 16) != cq->id) {
  563. spin_lock_irqsave(&phba->isr_lock, flags);
  564. phba->todo_mcc_cq = 1;
  565. spin_unlock_irqrestore(&phba->isr_lock, flags);
  566. } else {
  567. spin_lock_irqsave(&phba->isr_lock, flags);
  568. phba->todo_cq = 1;
  569. spin_unlock_irqrestore(&phba->isr_lock, flags);
  570. }
  571. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  572. queue_tail_inc(eq);
  573. eqe = queue_tail_node(eq);
  574. num_ioeq_processed++;
  575. }
  576. if (phba->todo_cq || phba->todo_mcc_cq)
  577. queue_work(phba->wq, &phba->work_cqs);
  578. if (num_ioeq_processed) {
  579. hwi_ring_eq_db(phba, eq->id, 0,
  580. num_ioeq_processed, 1, 1);
  581. return IRQ_HANDLED;
  582. } else
  583. return IRQ_NONE;
  584. }
  585. }
  586. static int beiscsi_init_irqs(struct beiscsi_hba *phba)
  587. {
  588. struct pci_dev *pcidev = phba->pcidev;
  589. struct hwi_controller *phwi_ctrlr;
  590. struct hwi_context_memory *phwi_context;
  591. int ret, msix_vec, i, j;
  592. char desc[32];
  593. phwi_ctrlr = phba->phwi_ctrlr;
  594. phwi_context = phwi_ctrlr->phwi_ctxt;
  595. if (phba->msix_enabled) {
  596. for (i = 0; i < phba->num_cpus; i++) {
  597. sprintf(desc, "beiscsi_msix_%04x", i);
  598. msix_vec = phba->msix_entries[i].vector;
  599. ret = request_irq(msix_vec, be_isr_msix, 0, desc,
  600. &phwi_context->be_eq[i]);
  601. if (ret) {
  602. shost_printk(KERN_ERR, phba->shost,
  603. "beiscsi_init_irqs-Failed to"
  604. "register msix for i = %d\n", i);
  605. if (!i)
  606. return ret;
  607. goto free_msix_irqs;
  608. }
  609. }
  610. msix_vec = phba->msix_entries[i].vector;
  611. ret = request_irq(msix_vec, be_isr_mcc, 0, "beiscsi_msix_mcc",
  612. &phwi_context->be_eq[i]);
  613. if (ret) {
  614. shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
  615. "Failed to register beiscsi_msix_mcc\n");
  616. i++;
  617. goto free_msix_irqs;
  618. }
  619. } else {
  620. ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
  621. "beiscsi", phba);
  622. if (ret) {
  623. shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
  624. "Failed to register irq\\n");
  625. return ret;
  626. }
  627. }
  628. return 0;
  629. free_msix_irqs:
  630. for (j = i - 1; j == 0; j++)
  631. free_irq(msix_vec, &phwi_context->be_eq[j]);
  632. return ret;
  633. }
  634. static void hwi_ring_cq_db(struct beiscsi_hba *phba,
  635. unsigned int id, unsigned int num_processed,
  636. unsigned char rearm, unsigned char event)
  637. {
  638. u32 val = 0;
  639. val |= id & DB_CQ_RING_ID_MASK;
  640. if (rearm)
  641. val |= 1 << DB_CQ_REARM_SHIFT;
  642. val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
  643. iowrite32(val, phba->db_va + DB_CQ_OFFSET);
  644. }
  645. static unsigned int
  646. beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
  647. struct beiscsi_hba *phba,
  648. unsigned short cid,
  649. struct pdu_base *ppdu,
  650. unsigned long pdu_len,
  651. void *pbuffer, unsigned long buf_len)
  652. {
  653. struct iscsi_conn *conn = beiscsi_conn->conn;
  654. struct iscsi_session *session = conn->session;
  655. struct iscsi_task *task;
  656. struct beiscsi_io_task *io_task;
  657. struct iscsi_hdr *login_hdr;
  658. switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
  659. PDUBASE_OPCODE_MASK) {
  660. case ISCSI_OP_NOOP_IN:
  661. pbuffer = NULL;
  662. buf_len = 0;
  663. break;
  664. case ISCSI_OP_ASYNC_EVENT:
  665. break;
  666. case ISCSI_OP_REJECT:
  667. WARN_ON(!pbuffer);
  668. WARN_ON(!(buf_len == 48));
  669. SE_DEBUG(DBG_LVL_1, "In ISCSI_OP_REJECT\n");
  670. break;
  671. case ISCSI_OP_LOGIN_RSP:
  672. case ISCSI_OP_TEXT_RSP:
  673. task = conn->login_task;
  674. io_task = task->dd_data;
  675. login_hdr = (struct iscsi_hdr *)ppdu;
  676. login_hdr->itt = io_task->libiscsi_itt;
  677. break;
  678. default:
  679. shost_printk(KERN_WARNING, phba->shost,
  680. "Unrecognized opcode 0x%x in async msg\n",
  681. (ppdu->
  682. dw[offsetof(struct amap_pdu_base, opcode) / 32]
  683. & PDUBASE_OPCODE_MASK));
  684. return 1;
  685. }
  686. spin_lock_bh(&session->lock);
  687. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
  688. spin_unlock_bh(&session->lock);
  689. return 0;
  690. }
  691. static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
  692. {
  693. struct sgl_handle *psgl_handle;
  694. if (phba->io_sgl_hndl_avbl) {
  695. SE_DEBUG(DBG_LVL_8,
  696. "In alloc_io_sgl_handle,io_sgl_alloc_index=%d\n",
  697. phba->io_sgl_alloc_index);
  698. psgl_handle = phba->io_sgl_hndl_base[phba->
  699. io_sgl_alloc_index];
  700. phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
  701. phba->io_sgl_hndl_avbl--;
  702. if (phba->io_sgl_alloc_index == (phba->params.
  703. ios_per_ctrl - 1))
  704. phba->io_sgl_alloc_index = 0;
  705. else
  706. phba->io_sgl_alloc_index++;
  707. } else
  708. psgl_handle = NULL;
  709. return psgl_handle;
  710. }
  711. static void
  712. free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  713. {
  714. SE_DEBUG(DBG_LVL_8, "In free_,io_sgl_free_index=%d\n",
  715. phba->io_sgl_free_index);
  716. if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
  717. /*
  718. * this can happen if clean_task is called on a task that
  719. * failed in xmit_task or alloc_pdu.
  720. */
  721. SE_DEBUG(DBG_LVL_8,
  722. "Double Free in IO SGL io_sgl_free_index=%d,"
  723. "value there=%p\n", phba->io_sgl_free_index,
  724. phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
  725. return;
  726. }
  727. phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
  728. phba->io_sgl_hndl_avbl++;
  729. if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
  730. phba->io_sgl_free_index = 0;
  731. else
  732. phba->io_sgl_free_index++;
  733. }
  734. /**
  735. * alloc_wrb_handle - To allocate a wrb handle
  736. * @phba: The hba pointer
  737. * @cid: The cid to use for allocation
  738. *
  739. * This happens under session_lock until submission to chip
  740. */
  741. struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
  742. {
  743. struct hwi_wrb_context *pwrb_context;
  744. struct hwi_controller *phwi_ctrlr;
  745. struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
  746. phwi_ctrlr = phba->phwi_ctrlr;
  747. pwrb_context = &phwi_ctrlr->wrb_context[cid];
  748. if (pwrb_context->wrb_handles_available >= 2) {
  749. pwrb_handle = pwrb_context->pwrb_handle_base[
  750. pwrb_context->alloc_index];
  751. pwrb_context->wrb_handles_available--;
  752. if (pwrb_context->alloc_index ==
  753. (phba->params.wrbs_per_cxn - 1))
  754. pwrb_context->alloc_index = 0;
  755. else
  756. pwrb_context->alloc_index++;
  757. pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
  758. pwrb_context->alloc_index];
  759. pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
  760. } else
  761. pwrb_handle = NULL;
  762. return pwrb_handle;
  763. }
  764. /**
  765. * free_wrb_handle - To free the wrb handle back to pool
  766. * @phba: The hba pointer
  767. * @pwrb_context: The context to free from
  768. * @pwrb_handle: The wrb_handle to free
  769. *
  770. * This happens under session_lock until submission to chip
  771. */
  772. static void
  773. free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
  774. struct wrb_handle *pwrb_handle)
  775. {
  776. pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
  777. pwrb_context->wrb_handles_available++;
  778. if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
  779. pwrb_context->free_index = 0;
  780. else
  781. pwrb_context->free_index++;
  782. SE_DEBUG(DBG_LVL_8,
  783. "FREE WRB: pwrb_handle=%p free_index=0x%x"
  784. "wrb_handles_available=%d\n",
  785. pwrb_handle, pwrb_context->free_index,
  786. pwrb_context->wrb_handles_available);
  787. }
  788. static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
  789. {
  790. struct sgl_handle *psgl_handle;
  791. if (phba->eh_sgl_hndl_avbl) {
  792. psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
  793. phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
  794. SE_DEBUG(DBG_LVL_8, "mgmt_sgl_alloc_index=%d=0x%x\n",
  795. phba->eh_sgl_alloc_index, phba->eh_sgl_alloc_index);
  796. phba->eh_sgl_hndl_avbl--;
  797. if (phba->eh_sgl_alloc_index ==
  798. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
  799. 1))
  800. phba->eh_sgl_alloc_index = 0;
  801. else
  802. phba->eh_sgl_alloc_index++;
  803. } else
  804. psgl_handle = NULL;
  805. return psgl_handle;
  806. }
  807. void
  808. free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  809. {
  810. SE_DEBUG(DBG_LVL_8, "In free_mgmt_sgl_handle,eh_sgl_free_index=%d\n",
  811. phba->eh_sgl_free_index);
  812. if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
  813. /*
  814. * this can happen if clean_task is called on a task that
  815. * failed in xmit_task or alloc_pdu.
  816. */
  817. SE_DEBUG(DBG_LVL_8,
  818. "Double Free in eh SGL ,eh_sgl_free_index=%d\n",
  819. phba->eh_sgl_free_index);
  820. return;
  821. }
  822. phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
  823. phba->eh_sgl_hndl_avbl++;
  824. if (phba->eh_sgl_free_index ==
  825. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
  826. phba->eh_sgl_free_index = 0;
  827. else
  828. phba->eh_sgl_free_index++;
  829. }
  830. static void
  831. be_complete_io(struct beiscsi_conn *beiscsi_conn,
  832. struct iscsi_task *task, struct sol_cqe *psol)
  833. {
  834. struct beiscsi_io_task *io_task = task->dd_data;
  835. struct be_status_bhs *sts_bhs =
  836. (struct be_status_bhs *)io_task->cmd_bhs;
  837. struct iscsi_conn *conn = beiscsi_conn->conn;
  838. unsigned int sense_len;
  839. unsigned char *sense;
  840. u32 resid = 0, exp_cmdsn, max_cmdsn;
  841. u8 rsp, status, flags;
  842. exp_cmdsn = (psol->
  843. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  844. & SOL_EXP_CMD_SN_MASK);
  845. max_cmdsn = ((psol->
  846. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  847. & SOL_EXP_CMD_SN_MASK) +
  848. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  849. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  850. rsp = ((psol->dw[offsetof(struct amap_sol_cqe, i_resp) / 32]
  851. & SOL_RESP_MASK) >> 16);
  852. status = ((psol->dw[offsetof(struct amap_sol_cqe, i_sts) / 32]
  853. & SOL_STS_MASK) >> 8);
  854. flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  855. & SOL_FLAGS_MASK) >> 24) | 0x80;
  856. task->sc->result = (DID_OK << 16) | status;
  857. if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
  858. task->sc->result = DID_ERROR << 16;
  859. goto unmap;
  860. }
  861. /* bidi not initially supported */
  862. if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
  863. resid = (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) /
  864. 32] & SOL_RES_CNT_MASK);
  865. if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
  866. task->sc->result = DID_ERROR << 16;
  867. if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  868. scsi_set_resid(task->sc, resid);
  869. if (!status && (scsi_bufflen(task->sc) - resid <
  870. task->sc->underflow))
  871. task->sc->result = DID_ERROR << 16;
  872. }
  873. }
  874. if (status == SAM_STAT_CHECK_CONDITION) {
  875. unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
  876. sense = sts_bhs->sense_info + sizeof(unsigned short);
  877. sense_len = cpu_to_be16(*slen);
  878. memcpy(task->sc->sense_buffer, sense,
  879. min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
  880. }
  881. if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ) {
  882. if (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
  883. & SOL_RES_CNT_MASK)
  884. conn->rxdata_octets += (psol->
  885. dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
  886. & SOL_RES_CNT_MASK);
  887. }
  888. unmap:
  889. scsi_dma_unmap(io_task->scsi_cmnd);
  890. iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
  891. }
  892. static void
  893. be_complete_logout(struct beiscsi_conn *beiscsi_conn,
  894. struct iscsi_task *task, struct sol_cqe *psol)
  895. {
  896. struct iscsi_logout_rsp *hdr;
  897. struct beiscsi_io_task *io_task = task->dd_data;
  898. struct iscsi_conn *conn = beiscsi_conn->conn;
  899. hdr = (struct iscsi_logout_rsp *)task->hdr;
  900. hdr->opcode = ISCSI_OP_LOGOUT_RSP;
  901. hdr->t2wait = 5;
  902. hdr->t2retain = 0;
  903. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  904. & SOL_FLAGS_MASK) >> 24) | 0x80;
  905. hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
  906. 32] & SOL_RESP_MASK);
  907. hdr->exp_cmdsn = cpu_to_be32(psol->
  908. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  909. & SOL_EXP_CMD_SN_MASK);
  910. hdr->max_cmdsn = be32_to_cpu((psol->
  911. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  912. & SOL_EXP_CMD_SN_MASK) +
  913. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  914. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  915. hdr->dlength[0] = 0;
  916. hdr->dlength[1] = 0;
  917. hdr->dlength[2] = 0;
  918. hdr->hlength = 0;
  919. hdr->itt = io_task->libiscsi_itt;
  920. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  921. }
  922. static void
  923. be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
  924. struct iscsi_task *task, struct sol_cqe *psol)
  925. {
  926. struct iscsi_tm_rsp *hdr;
  927. struct iscsi_conn *conn = beiscsi_conn->conn;
  928. struct beiscsi_io_task *io_task = task->dd_data;
  929. hdr = (struct iscsi_tm_rsp *)task->hdr;
  930. hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
  931. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  932. & SOL_FLAGS_MASK) >> 24) | 0x80;
  933. hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
  934. 32] & SOL_RESP_MASK);
  935. hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
  936. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
  937. hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
  938. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
  939. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  940. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  941. hdr->itt = io_task->libiscsi_itt;
  942. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  943. }
  944. static void
  945. hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
  946. struct beiscsi_hba *phba, struct sol_cqe *psol)
  947. {
  948. struct hwi_wrb_context *pwrb_context;
  949. struct wrb_handle *pwrb_handle = NULL;
  950. struct hwi_controller *phwi_ctrlr;
  951. struct iscsi_task *task;
  952. struct beiscsi_io_task *io_task;
  953. struct iscsi_conn *conn = beiscsi_conn->conn;
  954. struct iscsi_session *session = conn->session;
  955. phwi_ctrlr = phba->phwi_ctrlr;
  956. pwrb_context = &phwi_ctrlr->wrb_context[((psol->
  957. dw[offsetof(struct amap_sol_cqe, cid) / 32] &
  958. SOL_CID_MASK) >> 6) -
  959. phba->fw_config.iscsi_cid_start];
  960. pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
  961. dw[offsetof(struct amap_sol_cqe, wrb_index) /
  962. 32] & SOL_WRB_INDEX_MASK) >> 16)];
  963. task = pwrb_handle->pio_handle;
  964. io_task = task->dd_data;
  965. spin_lock(&phba->mgmt_sgl_lock);
  966. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  967. spin_unlock(&phba->mgmt_sgl_lock);
  968. spin_lock_bh(&session->lock);
  969. free_wrb_handle(phba, pwrb_context, pwrb_handle);
  970. spin_unlock_bh(&session->lock);
  971. }
  972. static void
  973. be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
  974. struct iscsi_task *task, struct sol_cqe *psol)
  975. {
  976. struct iscsi_nopin *hdr;
  977. struct iscsi_conn *conn = beiscsi_conn->conn;
  978. struct beiscsi_io_task *io_task = task->dd_data;
  979. hdr = (struct iscsi_nopin *)task->hdr;
  980. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  981. & SOL_FLAGS_MASK) >> 24) | 0x80;
  982. hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
  983. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
  984. hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
  985. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
  986. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  987. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  988. hdr->opcode = ISCSI_OP_NOOP_IN;
  989. hdr->itt = io_task->libiscsi_itt;
  990. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  991. }
  992. static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
  993. struct beiscsi_hba *phba, struct sol_cqe *psol)
  994. {
  995. struct hwi_wrb_context *pwrb_context;
  996. struct wrb_handle *pwrb_handle;
  997. struct iscsi_wrb *pwrb = NULL;
  998. struct hwi_controller *phwi_ctrlr;
  999. struct iscsi_task *task;
  1000. unsigned int type;
  1001. struct iscsi_conn *conn = beiscsi_conn->conn;
  1002. struct iscsi_session *session = conn->session;
  1003. phwi_ctrlr = phba->phwi_ctrlr;
  1004. pwrb_context = &phwi_ctrlr->wrb_context[((psol->dw[offsetof
  1005. (struct amap_sol_cqe, cid) / 32]
  1006. & SOL_CID_MASK) >> 6) -
  1007. phba->fw_config.iscsi_cid_start];
  1008. pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
  1009. dw[offsetof(struct amap_sol_cqe, wrb_index) /
  1010. 32] & SOL_WRB_INDEX_MASK) >> 16)];
  1011. task = pwrb_handle->pio_handle;
  1012. pwrb = pwrb_handle->pwrb;
  1013. type = (pwrb->dw[offsetof(struct amap_iscsi_wrb, type) / 32] &
  1014. WRB_TYPE_MASK) >> 28;
  1015. spin_lock_bh(&session->lock);
  1016. switch (type) {
  1017. case HWH_TYPE_IO:
  1018. case HWH_TYPE_IO_RD:
  1019. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
  1020. ISCSI_OP_NOOP_OUT)
  1021. be_complete_nopin_resp(beiscsi_conn, task, psol);
  1022. else
  1023. be_complete_io(beiscsi_conn, task, psol);
  1024. break;
  1025. case HWH_TYPE_LOGOUT:
  1026. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  1027. be_complete_logout(beiscsi_conn, task, psol);
  1028. else
  1029. be_complete_tmf(beiscsi_conn, task, psol);
  1030. break;
  1031. case HWH_TYPE_LOGIN:
  1032. SE_DEBUG(DBG_LVL_1,
  1033. "\t\t No HWH_TYPE_LOGIN Expected in hwi_complete_cmd"
  1034. "- Solicited path\n");
  1035. break;
  1036. case HWH_TYPE_NOP:
  1037. be_complete_nopin_resp(beiscsi_conn, task, psol);
  1038. break;
  1039. default:
  1040. shost_printk(KERN_WARNING, phba->shost,
  1041. "In hwi_complete_cmd, unknown type = %d"
  1042. "wrb_index 0x%x CID 0x%x\n", type,
  1043. ((psol->dw[offsetof(struct amap_iscsi_wrb,
  1044. type) / 32] & SOL_WRB_INDEX_MASK) >> 16),
  1045. ((psol->dw[offsetof(struct amap_sol_cqe,
  1046. cid) / 32] & SOL_CID_MASK) >> 6));
  1047. break;
  1048. }
  1049. spin_unlock_bh(&session->lock);
  1050. }
  1051. static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
  1052. *pasync_ctx, unsigned int is_header,
  1053. unsigned int host_write_ptr)
  1054. {
  1055. if (is_header)
  1056. return &pasync_ctx->async_entry[host_write_ptr].
  1057. header_busy_list;
  1058. else
  1059. return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
  1060. }
  1061. static struct async_pdu_handle *
  1062. hwi_get_async_handle(struct beiscsi_hba *phba,
  1063. struct beiscsi_conn *beiscsi_conn,
  1064. struct hwi_async_pdu_context *pasync_ctx,
  1065. struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
  1066. {
  1067. struct be_bus_address phys_addr;
  1068. struct list_head *pbusy_list;
  1069. struct async_pdu_handle *pasync_handle = NULL;
  1070. int buffer_len = 0;
  1071. unsigned char buffer_index = -1;
  1072. unsigned char is_header = 0;
  1073. phys_addr.u.a32.address_lo =
  1074. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_lo) / 32] -
  1075. ((pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
  1076. & PDUCQE_DPL_MASK) >> 16);
  1077. phys_addr.u.a32.address_hi =
  1078. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_hi) / 32];
  1079. phys_addr.u.a64.address =
  1080. *((unsigned long long *)(&phys_addr.u.a64.address));
  1081. switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
  1082. & PDUCQE_CODE_MASK) {
  1083. case UNSOL_HDR_NOTIFY:
  1084. is_header = 1;
  1085. pbusy_list = hwi_get_async_busy_list(pasync_ctx, 1,
  1086. (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1087. index) / 32] & PDUCQE_INDEX_MASK));
  1088. buffer_len = (unsigned int)(phys_addr.u.a64.address -
  1089. pasync_ctx->async_header.pa_base.u.a64.address);
  1090. buffer_index = buffer_len /
  1091. pasync_ctx->async_header.buffer_size;
  1092. break;
  1093. case UNSOL_DATA_NOTIFY:
  1094. pbusy_list = hwi_get_async_busy_list(pasync_ctx, 0, (pdpdu_cqe->
  1095. dw[offsetof(struct amap_i_t_dpdu_cqe,
  1096. index) / 32] & PDUCQE_INDEX_MASK));
  1097. buffer_len = (unsigned long)(phys_addr.u.a64.address -
  1098. pasync_ctx->async_data.pa_base.u.
  1099. a64.address);
  1100. buffer_index = buffer_len / pasync_ctx->async_data.buffer_size;
  1101. break;
  1102. default:
  1103. pbusy_list = NULL;
  1104. shost_printk(KERN_WARNING, phba->shost,
  1105. "Unexpected code=%d\n",
  1106. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1107. code) / 32] & PDUCQE_CODE_MASK);
  1108. return NULL;
  1109. }
  1110. WARN_ON(!(buffer_index <= pasync_ctx->async_data.num_entries));
  1111. WARN_ON(list_empty(pbusy_list));
  1112. list_for_each_entry(pasync_handle, pbusy_list, link) {
  1113. WARN_ON(pasync_handle->consumed);
  1114. if (pasync_handle->index == buffer_index)
  1115. break;
  1116. }
  1117. WARN_ON(!pasync_handle);
  1118. pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
  1119. phba->fw_config.iscsi_cid_start;
  1120. pasync_handle->is_header = is_header;
  1121. pasync_handle->buffer_len = ((pdpdu_cqe->
  1122. dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
  1123. & PDUCQE_DPL_MASK) >> 16);
  1124. *pcq_index = (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1125. index) / 32] & PDUCQE_INDEX_MASK);
  1126. return pasync_handle;
  1127. }
  1128. static unsigned int
  1129. hwi_update_async_writables(struct hwi_async_pdu_context *pasync_ctx,
  1130. unsigned int is_header, unsigned int cq_index)
  1131. {
  1132. struct list_head *pbusy_list;
  1133. struct async_pdu_handle *pasync_handle;
  1134. unsigned int num_entries, writables = 0;
  1135. unsigned int *pep_read_ptr, *pwritables;
  1136. if (is_header) {
  1137. pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
  1138. pwritables = &pasync_ctx->async_header.writables;
  1139. num_entries = pasync_ctx->async_header.num_entries;
  1140. } else {
  1141. pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
  1142. pwritables = &pasync_ctx->async_data.writables;
  1143. num_entries = pasync_ctx->async_data.num_entries;
  1144. }
  1145. while ((*pep_read_ptr) != cq_index) {
  1146. (*pep_read_ptr)++;
  1147. *pep_read_ptr = (*pep_read_ptr) % num_entries;
  1148. pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
  1149. *pep_read_ptr);
  1150. if (writables == 0)
  1151. WARN_ON(list_empty(pbusy_list));
  1152. if (!list_empty(pbusy_list)) {
  1153. pasync_handle = list_entry(pbusy_list->next,
  1154. struct async_pdu_handle,
  1155. link);
  1156. WARN_ON(!pasync_handle);
  1157. pasync_handle->consumed = 1;
  1158. }
  1159. writables++;
  1160. }
  1161. if (!writables) {
  1162. SE_DEBUG(DBG_LVL_1,
  1163. "Duplicate notification received - index 0x%x!!\n",
  1164. cq_index);
  1165. WARN_ON(1);
  1166. }
  1167. *pwritables = *pwritables + writables;
  1168. return 0;
  1169. }
  1170. static unsigned int hwi_free_async_msg(struct beiscsi_hba *phba,
  1171. unsigned int cri)
  1172. {
  1173. struct hwi_controller *phwi_ctrlr;
  1174. struct hwi_async_pdu_context *pasync_ctx;
  1175. struct async_pdu_handle *pasync_handle, *tmp_handle;
  1176. struct list_head *plist;
  1177. unsigned int i = 0;
  1178. phwi_ctrlr = phba->phwi_ctrlr;
  1179. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1180. plist = &pasync_ctx->async_entry[cri].wait_queue.list;
  1181. list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
  1182. list_del(&pasync_handle->link);
  1183. if (i == 0) {
  1184. list_add_tail(&pasync_handle->link,
  1185. &pasync_ctx->async_header.free_list);
  1186. pasync_ctx->async_header.free_entries++;
  1187. i++;
  1188. } else {
  1189. list_add_tail(&pasync_handle->link,
  1190. &pasync_ctx->async_data.free_list);
  1191. pasync_ctx->async_data.free_entries++;
  1192. i++;
  1193. }
  1194. }
  1195. INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
  1196. pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
  1197. pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
  1198. return 0;
  1199. }
  1200. static struct phys_addr *
  1201. hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
  1202. unsigned int is_header, unsigned int host_write_ptr)
  1203. {
  1204. struct phys_addr *pasync_sge = NULL;
  1205. if (is_header)
  1206. pasync_sge = pasync_ctx->async_header.ring_base;
  1207. else
  1208. pasync_sge = pasync_ctx->async_data.ring_base;
  1209. return pasync_sge + host_write_ptr;
  1210. }
  1211. static void hwi_post_async_buffers(struct beiscsi_hba *phba,
  1212. unsigned int is_header)
  1213. {
  1214. struct hwi_controller *phwi_ctrlr;
  1215. struct hwi_async_pdu_context *pasync_ctx;
  1216. struct async_pdu_handle *pasync_handle;
  1217. struct list_head *pfree_link, *pbusy_list;
  1218. struct phys_addr *pasync_sge;
  1219. unsigned int ring_id, num_entries;
  1220. unsigned int host_write_num;
  1221. unsigned int writables;
  1222. unsigned int i = 0;
  1223. u32 doorbell = 0;
  1224. phwi_ctrlr = phba->phwi_ctrlr;
  1225. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1226. if (is_header) {
  1227. num_entries = pasync_ctx->async_header.num_entries;
  1228. writables = min(pasync_ctx->async_header.writables,
  1229. pasync_ctx->async_header.free_entries);
  1230. pfree_link = pasync_ctx->async_header.free_list.next;
  1231. host_write_num = pasync_ctx->async_header.host_write_ptr;
  1232. ring_id = phwi_ctrlr->default_pdu_hdr.id;
  1233. } else {
  1234. num_entries = pasync_ctx->async_data.num_entries;
  1235. writables = min(pasync_ctx->async_data.writables,
  1236. pasync_ctx->async_data.free_entries);
  1237. pfree_link = pasync_ctx->async_data.free_list.next;
  1238. host_write_num = pasync_ctx->async_data.host_write_ptr;
  1239. ring_id = phwi_ctrlr->default_pdu_data.id;
  1240. }
  1241. writables = (writables / 8) * 8;
  1242. if (writables) {
  1243. for (i = 0; i < writables; i++) {
  1244. pbusy_list =
  1245. hwi_get_async_busy_list(pasync_ctx, is_header,
  1246. host_write_num);
  1247. pasync_handle =
  1248. list_entry(pfree_link, struct async_pdu_handle,
  1249. link);
  1250. WARN_ON(!pasync_handle);
  1251. pasync_handle->consumed = 0;
  1252. pfree_link = pfree_link->next;
  1253. pasync_sge = hwi_get_ring_address(pasync_ctx,
  1254. is_header, host_write_num);
  1255. pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
  1256. pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
  1257. list_move(&pasync_handle->link, pbusy_list);
  1258. host_write_num++;
  1259. host_write_num = host_write_num % num_entries;
  1260. }
  1261. if (is_header) {
  1262. pasync_ctx->async_header.host_write_ptr =
  1263. host_write_num;
  1264. pasync_ctx->async_header.free_entries -= writables;
  1265. pasync_ctx->async_header.writables -= writables;
  1266. pasync_ctx->async_header.busy_entries += writables;
  1267. } else {
  1268. pasync_ctx->async_data.host_write_ptr = host_write_num;
  1269. pasync_ctx->async_data.free_entries -= writables;
  1270. pasync_ctx->async_data.writables -= writables;
  1271. pasync_ctx->async_data.busy_entries += writables;
  1272. }
  1273. doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
  1274. doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
  1275. doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
  1276. doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
  1277. << DB_DEF_PDU_CQPROC_SHIFT;
  1278. iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
  1279. }
  1280. }
  1281. static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
  1282. struct beiscsi_conn *beiscsi_conn,
  1283. struct i_t_dpdu_cqe *pdpdu_cqe)
  1284. {
  1285. struct hwi_controller *phwi_ctrlr;
  1286. struct hwi_async_pdu_context *pasync_ctx;
  1287. struct async_pdu_handle *pasync_handle = NULL;
  1288. unsigned int cq_index = -1;
  1289. phwi_ctrlr = phba->phwi_ctrlr;
  1290. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1291. pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
  1292. pdpdu_cqe, &cq_index);
  1293. BUG_ON(pasync_handle->is_header != 0);
  1294. if (pasync_handle->consumed == 0)
  1295. hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
  1296. cq_index);
  1297. hwi_free_async_msg(phba, pasync_handle->cri);
  1298. hwi_post_async_buffers(phba, pasync_handle->is_header);
  1299. }
  1300. static unsigned int
  1301. hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
  1302. struct beiscsi_hba *phba,
  1303. struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
  1304. {
  1305. struct list_head *plist;
  1306. struct async_pdu_handle *pasync_handle;
  1307. void *phdr = NULL;
  1308. unsigned int hdr_len = 0, buf_len = 0;
  1309. unsigned int status, index = 0, offset = 0;
  1310. void *pfirst_buffer = NULL;
  1311. unsigned int num_buf = 0;
  1312. plist = &pasync_ctx->async_entry[cri].wait_queue.list;
  1313. list_for_each_entry(pasync_handle, plist, link) {
  1314. if (index == 0) {
  1315. phdr = pasync_handle->pbuffer;
  1316. hdr_len = pasync_handle->buffer_len;
  1317. } else {
  1318. buf_len = pasync_handle->buffer_len;
  1319. if (!num_buf) {
  1320. pfirst_buffer = pasync_handle->pbuffer;
  1321. num_buf++;
  1322. }
  1323. memcpy(pfirst_buffer + offset,
  1324. pasync_handle->pbuffer, buf_len);
  1325. offset = buf_len;
  1326. }
  1327. index++;
  1328. }
  1329. status = beiscsi_process_async_pdu(beiscsi_conn, phba,
  1330. (beiscsi_conn->beiscsi_conn_cid -
  1331. phba->fw_config.iscsi_cid_start),
  1332. phdr, hdr_len, pfirst_buffer,
  1333. buf_len);
  1334. if (status == 0)
  1335. hwi_free_async_msg(phba, cri);
  1336. return 0;
  1337. }
  1338. static unsigned int
  1339. hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
  1340. struct beiscsi_hba *phba,
  1341. struct async_pdu_handle *pasync_handle)
  1342. {
  1343. struct hwi_async_pdu_context *pasync_ctx;
  1344. struct hwi_controller *phwi_ctrlr;
  1345. unsigned int bytes_needed = 0, status = 0;
  1346. unsigned short cri = pasync_handle->cri;
  1347. struct pdu_base *ppdu;
  1348. phwi_ctrlr = phba->phwi_ctrlr;
  1349. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1350. list_del(&pasync_handle->link);
  1351. if (pasync_handle->is_header) {
  1352. pasync_ctx->async_header.busy_entries--;
  1353. if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
  1354. hwi_free_async_msg(phba, cri);
  1355. BUG();
  1356. }
  1357. pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
  1358. pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
  1359. pasync_ctx->async_entry[cri].wait_queue.hdr_len =
  1360. (unsigned short)pasync_handle->buffer_len;
  1361. list_add_tail(&pasync_handle->link,
  1362. &pasync_ctx->async_entry[cri].wait_queue.list);
  1363. ppdu = pasync_handle->pbuffer;
  1364. bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
  1365. data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
  1366. 0xFFFF0000) | ((be16_to_cpu((ppdu->
  1367. dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
  1368. & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
  1369. if (status == 0) {
  1370. pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
  1371. bytes_needed;
  1372. if (bytes_needed == 0)
  1373. status = hwi_fwd_async_msg(beiscsi_conn, phba,
  1374. pasync_ctx, cri);
  1375. }
  1376. } else {
  1377. pasync_ctx->async_data.busy_entries--;
  1378. if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
  1379. list_add_tail(&pasync_handle->link,
  1380. &pasync_ctx->async_entry[cri].wait_queue.
  1381. list);
  1382. pasync_ctx->async_entry[cri].wait_queue.
  1383. bytes_received +=
  1384. (unsigned short)pasync_handle->buffer_len;
  1385. if (pasync_ctx->async_entry[cri].wait_queue.
  1386. bytes_received >=
  1387. pasync_ctx->async_entry[cri].wait_queue.
  1388. bytes_needed)
  1389. status = hwi_fwd_async_msg(beiscsi_conn, phba,
  1390. pasync_ctx, cri);
  1391. }
  1392. }
  1393. return status;
  1394. }
  1395. static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
  1396. struct beiscsi_hba *phba,
  1397. struct i_t_dpdu_cqe *pdpdu_cqe)
  1398. {
  1399. struct hwi_controller *phwi_ctrlr;
  1400. struct hwi_async_pdu_context *pasync_ctx;
  1401. struct async_pdu_handle *pasync_handle = NULL;
  1402. unsigned int cq_index = -1;
  1403. phwi_ctrlr = phba->phwi_ctrlr;
  1404. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1405. pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
  1406. pdpdu_cqe, &cq_index);
  1407. if (pasync_handle->consumed == 0)
  1408. hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
  1409. cq_index);
  1410. hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
  1411. hwi_post_async_buffers(phba, pasync_handle->is_header);
  1412. }
  1413. static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
  1414. {
  1415. struct be_queue_info *mcc_cq;
  1416. struct be_mcc_compl *mcc_compl;
  1417. unsigned int num_processed = 0;
  1418. mcc_cq = &phba->ctrl.mcc_obj.cq;
  1419. mcc_compl = queue_tail_node(mcc_cq);
  1420. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1421. while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
  1422. if (num_processed >= 32) {
  1423. hwi_ring_cq_db(phba, mcc_cq->id,
  1424. num_processed, 0, 0);
  1425. num_processed = 0;
  1426. }
  1427. if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
  1428. /* Interpret flags as an async trailer */
  1429. if (is_link_state_evt(mcc_compl->flags))
  1430. /* Interpret compl as a async link evt */
  1431. beiscsi_async_link_state_process(phba,
  1432. (struct be_async_event_link_state *) mcc_compl);
  1433. else
  1434. SE_DEBUG(DBG_LVL_1,
  1435. " Unsupported Async Event, flags"
  1436. " = 0x%08x\n", mcc_compl->flags);
  1437. } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
  1438. be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
  1439. atomic_dec(&phba->ctrl.mcc_obj.q.used);
  1440. }
  1441. mcc_compl->flags = 0;
  1442. queue_tail_inc(mcc_cq);
  1443. mcc_compl = queue_tail_node(mcc_cq);
  1444. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1445. num_processed++;
  1446. }
  1447. if (num_processed > 0)
  1448. hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
  1449. }
  1450. static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
  1451. {
  1452. struct be_queue_info *cq;
  1453. struct sol_cqe *sol;
  1454. struct dmsg_cqe *dmsg;
  1455. unsigned int num_processed = 0;
  1456. unsigned int tot_nump = 0;
  1457. struct beiscsi_conn *beiscsi_conn;
  1458. struct beiscsi_endpoint *beiscsi_ep;
  1459. struct iscsi_endpoint *ep;
  1460. struct beiscsi_hba *phba;
  1461. cq = pbe_eq->cq;
  1462. sol = queue_tail_node(cq);
  1463. phba = pbe_eq->phba;
  1464. while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
  1465. CQE_VALID_MASK) {
  1466. be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
  1467. ep = phba->ep_array[(u32) ((sol->
  1468. dw[offsetof(struct amap_sol_cqe, cid) / 32] &
  1469. SOL_CID_MASK) >> 6) -
  1470. phba->fw_config.iscsi_cid_start];
  1471. beiscsi_ep = ep->dd_data;
  1472. beiscsi_conn = beiscsi_ep->conn;
  1473. if (num_processed >= 32) {
  1474. hwi_ring_cq_db(phba, cq->id,
  1475. num_processed, 0, 0);
  1476. tot_nump += num_processed;
  1477. num_processed = 0;
  1478. }
  1479. switch ((u32) sol->dw[offsetof(struct amap_sol_cqe, code) /
  1480. 32] & CQE_CODE_MASK) {
  1481. case SOL_CMD_COMPLETE:
  1482. hwi_complete_cmd(beiscsi_conn, phba, sol);
  1483. break;
  1484. case DRIVERMSG_NOTIFY:
  1485. SE_DEBUG(DBG_LVL_8, "Received DRIVERMSG_NOTIFY\n");
  1486. dmsg = (struct dmsg_cqe *)sol;
  1487. hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
  1488. break;
  1489. case UNSOL_HDR_NOTIFY:
  1490. SE_DEBUG(DBG_LVL_8, "Received UNSOL_HDR_ NOTIFY\n");
  1491. hwi_process_default_pdu_ring(beiscsi_conn, phba,
  1492. (struct i_t_dpdu_cqe *)sol);
  1493. break;
  1494. case UNSOL_DATA_NOTIFY:
  1495. SE_DEBUG(DBG_LVL_8, "Received UNSOL_DATA_NOTIFY\n");
  1496. hwi_process_default_pdu_ring(beiscsi_conn, phba,
  1497. (struct i_t_dpdu_cqe *)sol);
  1498. break;
  1499. case CXN_INVALIDATE_INDEX_NOTIFY:
  1500. case CMD_INVALIDATED_NOTIFY:
  1501. case CXN_INVALIDATE_NOTIFY:
  1502. SE_DEBUG(DBG_LVL_1,
  1503. "Ignoring CQ Error notification for cmd/cxn"
  1504. "invalidate\n");
  1505. break;
  1506. case SOL_CMD_KILLED_DATA_DIGEST_ERR:
  1507. case CMD_KILLED_INVALID_STATSN_RCVD:
  1508. case CMD_KILLED_INVALID_R2T_RCVD:
  1509. case CMD_CXN_KILLED_LUN_INVALID:
  1510. case CMD_CXN_KILLED_ICD_INVALID:
  1511. case CMD_CXN_KILLED_ITT_INVALID:
  1512. case CMD_CXN_KILLED_SEQ_OUTOFORDER:
  1513. case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
  1514. SE_DEBUG(DBG_LVL_1,
  1515. "CQ Error notification for cmd.. "
  1516. "code %d cid 0x%x\n",
  1517. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1518. 32] & CQE_CODE_MASK,
  1519. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1520. 32] & SOL_CID_MASK));
  1521. break;
  1522. case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
  1523. SE_DEBUG(DBG_LVL_1,
  1524. "Digest error on def pdu ring, dropping..\n");
  1525. hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
  1526. (struct i_t_dpdu_cqe *) sol);
  1527. break;
  1528. case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
  1529. case CXN_KILLED_BURST_LEN_MISMATCH:
  1530. case CXN_KILLED_AHS_RCVD:
  1531. case CXN_KILLED_HDR_DIGEST_ERR:
  1532. case CXN_KILLED_UNKNOWN_HDR:
  1533. case CXN_KILLED_STALE_ITT_TTT_RCVD:
  1534. case CXN_KILLED_INVALID_ITT_TTT_RCVD:
  1535. case CXN_KILLED_TIMED_OUT:
  1536. case CXN_KILLED_FIN_RCVD:
  1537. case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
  1538. case CXN_KILLED_BAD_WRB_INDEX_ERROR:
  1539. case CXN_KILLED_OVER_RUN_RESIDUAL:
  1540. case CXN_KILLED_UNDER_RUN_RESIDUAL:
  1541. case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
  1542. SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset CID "
  1543. "0x%x...\n",
  1544. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1545. 32] & CQE_CODE_MASK,
  1546. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1547. 32] & CQE_CID_MASK));
  1548. iscsi_conn_failure(beiscsi_conn->conn,
  1549. ISCSI_ERR_CONN_FAILED);
  1550. break;
  1551. case CXN_KILLED_RST_SENT:
  1552. case CXN_KILLED_RST_RCVD:
  1553. SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset"
  1554. "received/sent on CID 0x%x...\n",
  1555. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1556. 32] & CQE_CODE_MASK,
  1557. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1558. 32] & CQE_CID_MASK));
  1559. iscsi_conn_failure(beiscsi_conn->conn,
  1560. ISCSI_ERR_CONN_FAILED);
  1561. break;
  1562. default:
  1563. SE_DEBUG(DBG_LVL_1, "CQ Error Invalid code= %d "
  1564. "received on CID 0x%x...\n",
  1565. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1566. 32] & CQE_CODE_MASK,
  1567. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1568. 32] & CQE_CID_MASK));
  1569. break;
  1570. }
  1571. AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
  1572. queue_tail_inc(cq);
  1573. sol = queue_tail_node(cq);
  1574. num_processed++;
  1575. }
  1576. if (num_processed > 0) {
  1577. tot_nump += num_processed;
  1578. hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
  1579. }
  1580. return tot_nump;
  1581. }
  1582. void beiscsi_process_all_cqs(struct work_struct *work)
  1583. {
  1584. unsigned long flags;
  1585. struct hwi_controller *phwi_ctrlr;
  1586. struct hwi_context_memory *phwi_context;
  1587. struct be_eq_obj *pbe_eq;
  1588. struct beiscsi_hba *phba =
  1589. container_of(work, struct beiscsi_hba, work_cqs);
  1590. phwi_ctrlr = phba->phwi_ctrlr;
  1591. phwi_context = phwi_ctrlr->phwi_ctxt;
  1592. if (phba->msix_enabled)
  1593. pbe_eq = &phwi_context->be_eq[phba->num_cpus];
  1594. else
  1595. pbe_eq = &phwi_context->be_eq[0];
  1596. if (phba->todo_mcc_cq) {
  1597. spin_lock_irqsave(&phba->isr_lock, flags);
  1598. phba->todo_mcc_cq = 0;
  1599. spin_unlock_irqrestore(&phba->isr_lock, flags);
  1600. beiscsi_process_mcc_isr(phba);
  1601. }
  1602. if (phba->todo_cq) {
  1603. spin_lock_irqsave(&phba->isr_lock, flags);
  1604. phba->todo_cq = 0;
  1605. spin_unlock_irqrestore(&phba->isr_lock, flags);
  1606. beiscsi_process_cq(pbe_eq);
  1607. }
  1608. }
  1609. static int be_iopoll(struct blk_iopoll *iop, int budget)
  1610. {
  1611. static unsigned int ret;
  1612. struct beiscsi_hba *phba;
  1613. struct be_eq_obj *pbe_eq;
  1614. pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
  1615. ret = beiscsi_process_cq(pbe_eq);
  1616. if (ret < budget) {
  1617. phba = pbe_eq->phba;
  1618. blk_iopoll_complete(iop);
  1619. SE_DEBUG(DBG_LVL_8, "rearm pbe_eq->q.id =%d\n", pbe_eq->q.id);
  1620. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1621. }
  1622. return ret;
  1623. }
  1624. static void
  1625. hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
  1626. unsigned int num_sg, struct beiscsi_io_task *io_task)
  1627. {
  1628. struct iscsi_sge *psgl;
  1629. unsigned short sg_len, index;
  1630. unsigned int sge_len = 0;
  1631. unsigned long long addr;
  1632. struct scatterlist *l_sg;
  1633. unsigned int offset;
  1634. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  1635. io_task->bhs_pa.u.a32.address_lo);
  1636. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  1637. io_task->bhs_pa.u.a32.address_hi);
  1638. l_sg = sg;
  1639. for (index = 0; (index < num_sg) && (index < 2); index++,
  1640. sg = sg_next(sg)) {
  1641. if (index == 0) {
  1642. sg_len = sg_dma_len(sg);
  1643. addr = (u64) sg_dma_address(sg);
  1644. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  1645. ((u32)(addr & 0xFFFFFFFF)));
  1646. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  1647. ((u32)(addr >> 32)));
  1648. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  1649. sg_len);
  1650. sge_len = sg_len;
  1651. } else {
  1652. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
  1653. pwrb, sge_len);
  1654. sg_len = sg_dma_len(sg);
  1655. addr = (u64) sg_dma_address(sg);
  1656. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
  1657. ((u32)(addr & 0xFFFFFFFF)));
  1658. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
  1659. ((u32)(addr >> 32)));
  1660. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
  1661. sg_len);
  1662. }
  1663. }
  1664. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1665. memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
  1666. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
  1667. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1668. io_task->bhs_pa.u.a32.address_hi);
  1669. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1670. io_task->bhs_pa.u.a32.address_lo);
  1671. if (num_sg == 1) {
  1672. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1673. 1);
  1674. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1675. 0);
  1676. } else if (num_sg == 2) {
  1677. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1678. 0);
  1679. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1680. 1);
  1681. } else {
  1682. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1683. 0);
  1684. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1685. 0);
  1686. }
  1687. sg = l_sg;
  1688. psgl++;
  1689. psgl++;
  1690. offset = 0;
  1691. for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
  1692. sg_len = sg_dma_len(sg);
  1693. addr = (u64) sg_dma_address(sg);
  1694. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1695. (addr & 0xFFFFFFFF));
  1696. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1697. (addr >> 32));
  1698. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
  1699. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
  1700. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  1701. offset += sg_len;
  1702. }
  1703. psgl--;
  1704. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  1705. }
  1706. static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
  1707. {
  1708. struct iscsi_sge *psgl;
  1709. unsigned long long addr;
  1710. struct beiscsi_io_task *io_task = task->dd_data;
  1711. struct beiscsi_conn *beiscsi_conn = io_task->conn;
  1712. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1713. io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
  1714. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  1715. io_task->bhs_pa.u.a32.address_lo);
  1716. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  1717. io_task->bhs_pa.u.a32.address_hi);
  1718. if (task->data) {
  1719. if (task->data_count) {
  1720. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  1721. addr = (u64) pci_map_single(phba->pcidev,
  1722. task->data,
  1723. task->data_count, 1);
  1724. } else {
  1725. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  1726. addr = 0;
  1727. }
  1728. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  1729. ((u32)(addr & 0xFFFFFFFF)));
  1730. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  1731. ((u32)(addr >> 32)));
  1732. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  1733. task->data_count);
  1734. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
  1735. } else {
  1736. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  1737. addr = 0;
  1738. }
  1739. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1740. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
  1741. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1742. io_task->bhs_pa.u.a32.address_hi);
  1743. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1744. io_task->bhs_pa.u.a32.address_lo);
  1745. if (task->data) {
  1746. psgl++;
  1747. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
  1748. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
  1749. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
  1750. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
  1751. AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
  1752. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  1753. psgl++;
  1754. if (task->data) {
  1755. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1756. ((u32)(addr & 0xFFFFFFFF)));
  1757. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1758. ((u32)(addr >> 32)));
  1759. }
  1760. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
  1761. }
  1762. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  1763. }
  1764. static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
  1765. {
  1766. unsigned int num_cq_pages, num_async_pdu_buf_pages;
  1767. unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
  1768. unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
  1769. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  1770. sizeof(struct sol_cqe));
  1771. num_async_pdu_buf_pages =
  1772. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1773. phba->params.defpdu_hdr_sz);
  1774. num_async_pdu_buf_sgl_pages =
  1775. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1776. sizeof(struct phys_addr));
  1777. num_async_pdu_data_pages =
  1778. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1779. phba->params.defpdu_data_sz);
  1780. num_async_pdu_data_sgl_pages =
  1781. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1782. sizeof(struct phys_addr));
  1783. phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
  1784. phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
  1785. BE_ISCSI_PDU_HEADER_SIZE;
  1786. phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
  1787. sizeof(struct hwi_context_memory);
  1788. phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
  1789. * (phba->params.wrbs_per_cxn)
  1790. * phba->params.cxns_per_ctrl;
  1791. wrb_sz_per_cxn = sizeof(struct wrb_handle) *
  1792. (phba->params.wrbs_per_cxn);
  1793. phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
  1794. phba->params.cxns_per_ctrl);
  1795. phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
  1796. phba->params.icds_per_ctrl;
  1797. phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
  1798. phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
  1799. phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
  1800. num_async_pdu_buf_pages * PAGE_SIZE;
  1801. phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
  1802. num_async_pdu_data_pages * PAGE_SIZE;
  1803. phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
  1804. num_async_pdu_buf_sgl_pages * PAGE_SIZE;
  1805. phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
  1806. num_async_pdu_data_sgl_pages * PAGE_SIZE;
  1807. phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
  1808. phba->params.asyncpdus_per_ctrl *
  1809. sizeof(struct async_pdu_handle);
  1810. phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
  1811. phba->params.asyncpdus_per_ctrl *
  1812. sizeof(struct async_pdu_handle);
  1813. phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
  1814. sizeof(struct hwi_async_pdu_context) +
  1815. (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
  1816. }
  1817. static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
  1818. {
  1819. struct be_mem_descriptor *mem_descr;
  1820. dma_addr_t bus_add;
  1821. struct mem_array *mem_arr, *mem_arr_orig;
  1822. unsigned int i, j, alloc_size, curr_alloc_size;
  1823. phba->phwi_ctrlr = kmalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
  1824. if (!phba->phwi_ctrlr)
  1825. return -ENOMEM;
  1826. phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
  1827. GFP_KERNEL);
  1828. if (!phba->init_mem) {
  1829. kfree(phba->phwi_ctrlr);
  1830. return -ENOMEM;
  1831. }
  1832. mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
  1833. GFP_KERNEL);
  1834. if (!mem_arr_orig) {
  1835. kfree(phba->init_mem);
  1836. kfree(phba->phwi_ctrlr);
  1837. return -ENOMEM;
  1838. }
  1839. mem_descr = phba->init_mem;
  1840. for (i = 0; i < SE_MEM_MAX; i++) {
  1841. j = 0;
  1842. mem_arr = mem_arr_orig;
  1843. alloc_size = phba->mem_req[i];
  1844. memset(mem_arr, 0, sizeof(struct mem_array) *
  1845. BEISCSI_MAX_FRAGS_INIT);
  1846. curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
  1847. do {
  1848. mem_arr->virtual_address = pci_alloc_consistent(
  1849. phba->pcidev,
  1850. curr_alloc_size,
  1851. &bus_add);
  1852. if (!mem_arr->virtual_address) {
  1853. if (curr_alloc_size <= BE_MIN_MEM_SIZE)
  1854. goto free_mem;
  1855. if (curr_alloc_size -
  1856. rounddown_pow_of_two(curr_alloc_size))
  1857. curr_alloc_size = rounddown_pow_of_two
  1858. (curr_alloc_size);
  1859. else
  1860. curr_alloc_size = curr_alloc_size / 2;
  1861. } else {
  1862. mem_arr->bus_address.u.
  1863. a64.address = (__u64) bus_add;
  1864. mem_arr->size = curr_alloc_size;
  1865. alloc_size -= curr_alloc_size;
  1866. curr_alloc_size = min(be_max_phys_size *
  1867. 1024, alloc_size);
  1868. j++;
  1869. mem_arr++;
  1870. }
  1871. } while (alloc_size);
  1872. mem_descr->num_elements = j;
  1873. mem_descr->size_in_bytes = phba->mem_req[i];
  1874. mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
  1875. GFP_KERNEL);
  1876. if (!mem_descr->mem_array)
  1877. goto free_mem;
  1878. memcpy(mem_descr->mem_array, mem_arr_orig,
  1879. sizeof(struct mem_array) * j);
  1880. mem_descr++;
  1881. }
  1882. kfree(mem_arr_orig);
  1883. return 0;
  1884. free_mem:
  1885. mem_descr->num_elements = j;
  1886. while ((i) || (j)) {
  1887. for (j = mem_descr->num_elements; j > 0; j--) {
  1888. pci_free_consistent(phba->pcidev,
  1889. mem_descr->mem_array[j - 1].size,
  1890. mem_descr->mem_array[j - 1].
  1891. virtual_address,
  1892. (unsigned long)mem_descr->
  1893. mem_array[j - 1].
  1894. bus_address.u.a64.address);
  1895. }
  1896. if (i) {
  1897. i--;
  1898. kfree(mem_descr->mem_array);
  1899. mem_descr--;
  1900. }
  1901. }
  1902. kfree(mem_arr_orig);
  1903. kfree(phba->init_mem);
  1904. kfree(phba->phwi_ctrlr);
  1905. return -ENOMEM;
  1906. }
  1907. static int beiscsi_get_memory(struct beiscsi_hba *phba)
  1908. {
  1909. beiscsi_find_mem_req(phba);
  1910. return beiscsi_alloc_mem(phba);
  1911. }
  1912. static void iscsi_init_global_templates(struct beiscsi_hba *phba)
  1913. {
  1914. struct pdu_data_out *pdata_out;
  1915. struct pdu_nop_out *pnop_out;
  1916. struct be_mem_descriptor *mem_descr;
  1917. mem_descr = phba->init_mem;
  1918. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  1919. pdata_out =
  1920. (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
  1921. memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  1922. AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
  1923. IIOC_SCSI_DATA);
  1924. pnop_out =
  1925. (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
  1926. virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
  1927. memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  1928. AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
  1929. AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
  1930. AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
  1931. }
  1932. static void beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
  1933. {
  1934. struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
  1935. struct wrb_handle *pwrb_handle;
  1936. struct hwi_controller *phwi_ctrlr;
  1937. struct hwi_wrb_context *pwrb_context;
  1938. struct iscsi_wrb *pwrb;
  1939. unsigned int num_cxn_wrbh;
  1940. unsigned int num_cxn_wrb, j, idx, index;
  1941. mem_descr_wrbh = phba->init_mem;
  1942. mem_descr_wrbh += HWI_MEM_WRBH;
  1943. mem_descr_wrb = phba->init_mem;
  1944. mem_descr_wrb += HWI_MEM_WRB;
  1945. idx = 0;
  1946. pwrb_handle = mem_descr_wrbh->mem_array[idx].virtual_address;
  1947. num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
  1948. ((sizeof(struct wrb_handle)) *
  1949. phba->params.wrbs_per_cxn));
  1950. phwi_ctrlr = phba->phwi_ctrlr;
  1951. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  1952. pwrb_context = &phwi_ctrlr->wrb_context[index];
  1953. pwrb_context->pwrb_handle_base =
  1954. kzalloc(sizeof(struct wrb_handle *) *
  1955. phba->params.wrbs_per_cxn, GFP_KERNEL);
  1956. pwrb_context->pwrb_handle_basestd =
  1957. kzalloc(sizeof(struct wrb_handle *) *
  1958. phba->params.wrbs_per_cxn, GFP_KERNEL);
  1959. if (num_cxn_wrbh) {
  1960. pwrb_context->alloc_index = 0;
  1961. pwrb_context->wrb_handles_available = 0;
  1962. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  1963. pwrb_context->pwrb_handle_base[j] = pwrb_handle;
  1964. pwrb_context->pwrb_handle_basestd[j] =
  1965. pwrb_handle;
  1966. pwrb_context->wrb_handles_available++;
  1967. pwrb_handle->wrb_index = j;
  1968. pwrb_handle++;
  1969. }
  1970. pwrb_context->free_index = 0;
  1971. num_cxn_wrbh--;
  1972. } else {
  1973. idx++;
  1974. pwrb_handle =
  1975. mem_descr_wrbh->mem_array[idx].virtual_address;
  1976. num_cxn_wrbh =
  1977. ((mem_descr_wrbh->mem_array[idx].size) /
  1978. ((sizeof(struct wrb_handle)) *
  1979. phba->params.wrbs_per_cxn));
  1980. pwrb_context->alloc_index = 0;
  1981. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  1982. pwrb_context->pwrb_handle_base[j] = pwrb_handle;
  1983. pwrb_context->pwrb_handle_basestd[j] =
  1984. pwrb_handle;
  1985. pwrb_context->wrb_handles_available++;
  1986. pwrb_handle->wrb_index = j;
  1987. pwrb_handle++;
  1988. }
  1989. pwrb_context->free_index = 0;
  1990. num_cxn_wrbh--;
  1991. }
  1992. }
  1993. idx = 0;
  1994. pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
  1995. num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
  1996. ((sizeof(struct iscsi_wrb) *
  1997. phba->params.wrbs_per_cxn));
  1998. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  1999. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2000. if (num_cxn_wrb) {
  2001. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2002. pwrb_handle = pwrb_context->pwrb_handle_base[j];
  2003. pwrb_handle->pwrb = pwrb;
  2004. pwrb++;
  2005. }
  2006. num_cxn_wrb--;
  2007. } else {
  2008. idx++;
  2009. pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
  2010. num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
  2011. ((sizeof(struct iscsi_wrb) *
  2012. phba->params.wrbs_per_cxn));
  2013. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2014. pwrb_handle = pwrb_context->pwrb_handle_base[j];
  2015. pwrb_handle->pwrb = pwrb;
  2016. pwrb++;
  2017. }
  2018. num_cxn_wrb--;
  2019. }
  2020. }
  2021. }
  2022. static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
  2023. {
  2024. struct hwi_controller *phwi_ctrlr;
  2025. struct hba_parameters *p = &phba->params;
  2026. struct hwi_async_pdu_context *pasync_ctx;
  2027. struct async_pdu_handle *pasync_header_h, *pasync_data_h;
  2028. unsigned int index;
  2029. struct be_mem_descriptor *mem_descr;
  2030. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2031. mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
  2032. phwi_ctrlr = phba->phwi_ctrlr;
  2033. phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
  2034. mem_descr->mem_array[0].virtual_address;
  2035. pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
  2036. memset(pasync_ctx, 0, sizeof(*pasync_ctx));
  2037. pasync_ctx->async_header.num_entries = p->asyncpdus_per_ctrl;
  2038. pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
  2039. pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
  2040. pasync_ctx->async_data.num_entries = p->asyncpdus_per_ctrl;
  2041. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2042. mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
  2043. if (mem_descr->mem_array[0].virtual_address) {
  2044. SE_DEBUG(DBG_LVL_8,
  2045. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_BUF"
  2046. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2047. } else
  2048. shost_printk(KERN_WARNING, phba->shost,
  2049. "No Virtual address\n");
  2050. pasync_ctx->async_header.va_base =
  2051. mem_descr->mem_array[0].virtual_address;
  2052. pasync_ctx->async_header.pa_base.u.a64.address =
  2053. mem_descr->mem_array[0].bus_address.u.a64.address;
  2054. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2055. mem_descr += HWI_MEM_ASYNC_HEADER_RING;
  2056. if (mem_descr->mem_array[0].virtual_address) {
  2057. SE_DEBUG(DBG_LVL_8,
  2058. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_RING"
  2059. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2060. } else
  2061. shost_printk(KERN_WARNING, phba->shost,
  2062. "No Virtual address\n");
  2063. pasync_ctx->async_header.ring_base =
  2064. mem_descr->mem_array[0].virtual_address;
  2065. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2066. mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
  2067. if (mem_descr->mem_array[0].virtual_address) {
  2068. SE_DEBUG(DBG_LVL_8,
  2069. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_HANDLE"
  2070. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2071. } else
  2072. shost_printk(KERN_WARNING, phba->shost,
  2073. "No Virtual address\n");
  2074. pasync_ctx->async_header.handle_base =
  2075. mem_descr->mem_array[0].virtual_address;
  2076. pasync_ctx->async_header.writables = 0;
  2077. INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
  2078. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2079. mem_descr += HWI_MEM_ASYNC_DATA_BUF;
  2080. if (mem_descr->mem_array[0].virtual_address) {
  2081. SE_DEBUG(DBG_LVL_8,
  2082. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_BUF"
  2083. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2084. } else
  2085. shost_printk(KERN_WARNING, phba->shost,
  2086. "No Virtual address\n");
  2087. pasync_ctx->async_data.va_base =
  2088. mem_descr->mem_array[0].virtual_address;
  2089. pasync_ctx->async_data.pa_base.u.a64.address =
  2090. mem_descr->mem_array[0].bus_address.u.a64.address;
  2091. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2092. mem_descr += HWI_MEM_ASYNC_DATA_RING;
  2093. if (mem_descr->mem_array[0].virtual_address) {
  2094. SE_DEBUG(DBG_LVL_8,
  2095. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_RING"
  2096. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2097. } else
  2098. shost_printk(KERN_WARNING, phba->shost,
  2099. "No Virtual address\n");
  2100. pasync_ctx->async_data.ring_base =
  2101. mem_descr->mem_array[0].virtual_address;
  2102. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2103. mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
  2104. if (!mem_descr->mem_array[0].virtual_address)
  2105. shost_printk(KERN_WARNING, phba->shost,
  2106. "No Virtual address\n");
  2107. pasync_ctx->async_data.handle_base =
  2108. mem_descr->mem_array[0].virtual_address;
  2109. pasync_ctx->async_data.writables = 0;
  2110. INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
  2111. pasync_header_h =
  2112. (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
  2113. pasync_data_h =
  2114. (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
  2115. for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
  2116. pasync_header_h->cri = -1;
  2117. pasync_header_h->index = (char)index;
  2118. INIT_LIST_HEAD(&pasync_header_h->link);
  2119. pasync_header_h->pbuffer =
  2120. (void *)((unsigned long)
  2121. (pasync_ctx->async_header.va_base) +
  2122. (p->defpdu_hdr_sz * index));
  2123. pasync_header_h->pa.u.a64.address =
  2124. pasync_ctx->async_header.pa_base.u.a64.address +
  2125. (p->defpdu_hdr_sz * index);
  2126. list_add_tail(&pasync_header_h->link,
  2127. &pasync_ctx->async_header.free_list);
  2128. pasync_header_h++;
  2129. pasync_ctx->async_header.free_entries++;
  2130. pasync_ctx->async_header.writables++;
  2131. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
  2132. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
  2133. header_busy_list);
  2134. pasync_data_h->cri = -1;
  2135. pasync_data_h->index = (char)index;
  2136. INIT_LIST_HEAD(&pasync_data_h->link);
  2137. pasync_data_h->pbuffer =
  2138. (void *)((unsigned long)
  2139. (pasync_ctx->async_data.va_base) +
  2140. (p->defpdu_data_sz * index));
  2141. pasync_data_h->pa.u.a64.address =
  2142. pasync_ctx->async_data.pa_base.u.a64.address +
  2143. (p->defpdu_data_sz * index);
  2144. list_add_tail(&pasync_data_h->link,
  2145. &pasync_ctx->async_data.free_list);
  2146. pasync_data_h++;
  2147. pasync_ctx->async_data.free_entries++;
  2148. pasync_ctx->async_data.writables++;
  2149. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
  2150. }
  2151. pasync_ctx->async_header.host_write_ptr = 0;
  2152. pasync_ctx->async_header.ep_read_ptr = -1;
  2153. pasync_ctx->async_data.host_write_ptr = 0;
  2154. pasync_ctx->async_data.ep_read_ptr = -1;
  2155. }
  2156. static int
  2157. be_sgl_create_contiguous(void *virtual_address,
  2158. u64 physical_address, u32 length,
  2159. struct be_dma_mem *sgl)
  2160. {
  2161. WARN_ON(!virtual_address);
  2162. WARN_ON(!physical_address);
  2163. WARN_ON(!length > 0);
  2164. WARN_ON(!sgl);
  2165. sgl->va = virtual_address;
  2166. sgl->dma = (unsigned long)physical_address;
  2167. sgl->size = length;
  2168. return 0;
  2169. }
  2170. static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
  2171. {
  2172. memset(sgl, 0, sizeof(*sgl));
  2173. }
  2174. static void
  2175. hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
  2176. struct mem_array *pmem, struct be_dma_mem *sgl)
  2177. {
  2178. if (sgl->va)
  2179. be_sgl_destroy_contiguous(sgl);
  2180. be_sgl_create_contiguous(pmem->virtual_address,
  2181. pmem->bus_address.u.a64.address,
  2182. pmem->size, sgl);
  2183. }
  2184. static void
  2185. hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
  2186. struct mem_array *pmem, struct be_dma_mem *sgl)
  2187. {
  2188. if (sgl->va)
  2189. be_sgl_destroy_contiguous(sgl);
  2190. be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
  2191. pmem->bus_address.u.a64.address,
  2192. pmem->size, sgl);
  2193. }
  2194. static int be_fill_queue(struct be_queue_info *q,
  2195. u16 len, u16 entry_size, void *vaddress)
  2196. {
  2197. struct be_dma_mem *mem = &q->dma_mem;
  2198. memset(q, 0, sizeof(*q));
  2199. q->len = len;
  2200. q->entry_size = entry_size;
  2201. mem->size = len * entry_size;
  2202. mem->va = vaddress;
  2203. if (!mem->va)
  2204. return -ENOMEM;
  2205. memset(mem->va, 0, mem->size);
  2206. return 0;
  2207. }
  2208. static int beiscsi_create_eqs(struct beiscsi_hba *phba,
  2209. struct hwi_context_memory *phwi_context)
  2210. {
  2211. unsigned int i, num_eq_pages;
  2212. int ret, eq_for_mcc;
  2213. struct be_queue_info *eq;
  2214. struct be_dma_mem *mem;
  2215. void *eq_vaddress;
  2216. dma_addr_t paddr;
  2217. num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
  2218. sizeof(struct be_eq_entry));
  2219. if (phba->msix_enabled)
  2220. eq_for_mcc = 1;
  2221. else
  2222. eq_for_mcc = 0;
  2223. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2224. eq = &phwi_context->be_eq[i].q;
  2225. mem = &eq->dma_mem;
  2226. phwi_context->be_eq[i].phba = phba;
  2227. eq_vaddress = pci_alloc_consistent(phba->pcidev,
  2228. num_eq_pages * PAGE_SIZE,
  2229. &paddr);
  2230. if (!eq_vaddress)
  2231. goto create_eq_error;
  2232. mem->va = eq_vaddress;
  2233. ret = be_fill_queue(eq, phba->params.num_eq_entries,
  2234. sizeof(struct be_eq_entry), eq_vaddress);
  2235. if (ret) {
  2236. shost_printk(KERN_ERR, phba->shost,
  2237. "be_fill_queue Failed for EQ\n");
  2238. goto create_eq_error;
  2239. }
  2240. mem->dma = paddr;
  2241. ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
  2242. phwi_context->cur_eqd);
  2243. if (ret) {
  2244. shost_printk(KERN_ERR, phba->shost,
  2245. "beiscsi_cmd_eq_create"
  2246. "Failedfor EQ\n");
  2247. goto create_eq_error;
  2248. }
  2249. SE_DEBUG(DBG_LVL_8, "eqid = %d\n", phwi_context->be_eq[i].q.id);
  2250. }
  2251. return 0;
  2252. create_eq_error:
  2253. for (i = 0; i < (phba->num_cpus + 1); i++) {
  2254. eq = &phwi_context->be_eq[i].q;
  2255. mem = &eq->dma_mem;
  2256. if (mem->va)
  2257. pci_free_consistent(phba->pcidev, num_eq_pages
  2258. * PAGE_SIZE,
  2259. mem->va, mem->dma);
  2260. }
  2261. return ret;
  2262. }
  2263. static int beiscsi_create_cqs(struct beiscsi_hba *phba,
  2264. struct hwi_context_memory *phwi_context)
  2265. {
  2266. unsigned int i, num_cq_pages;
  2267. int ret;
  2268. struct be_queue_info *cq, *eq;
  2269. struct be_dma_mem *mem;
  2270. struct be_eq_obj *pbe_eq;
  2271. void *cq_vaddress;
  2272. dma_addr_t paddr;
  2273. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  2274. sizeof(struct sol_cqe));
  2275. for (i = 0; i < phba->num_cpus; i++) {
  2276. cq = &phwi_context->be_cq[i];
  2277. eq = &phwi_context->be_eq[i].q;
  2278. pbe_eq = &phwi_context->be_eq[i];
  2279. pbe_eq->cq = cq;
  2280. pbe_eq->phba = phba;
  2281. mem = &cq->dma_mem;
  2282. cq_vaddress = pci_alloc_consistent(phba->pcidev,
  2283. num_cq_pages * PAGE_SIZE,
  2284. &paddr);
  2285. if (!cq_vaddress)
  2286. goto create_cq_error;
  2287. ret = be_fill_queue(cq, phba->params.num_cq_entries,
  2288. sizeof(struct sol_cqe), cq_vaddress);
  2289. if (ret) {
  2290. shost_printk(KERN_ERR, phba->shost,
  2291. "be_fill_queue Failed for ISCSI CQ\n");
  2292. goto create_cq_error;
  2293. }
  2294. mem->dma = paddr;
  2295. ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
  2296. false, 0);
  2297. if (ret) {
  2298. shost_printk(KERN_ERR, phba->shost,
  2299. "beiscsi_cmd_eq_create"
  2300. "Failed for ISCSI CQ\n");
  2301. goto create_cq_error;
  2302. }
  2303. SE_DEBUG(DBG_LVL_8, "iscsi cq_id is %d for eq_id %d\n",
  2304. cq->id, eq->id);
  2305. SE_DEBUG(DBG_LVL_8, "ISCSI CQ CREATED\n");
  2306. }
  2307. return 0;
  2308. create_cq_error:
  2309. for (i = 0; i < phba->num_cpus; i++) {
  2310. cq = &phwi_context->be_cq[i];
  2311. mem = &cq->dma_mem;
  2312. if (mem->va)
  2313. pci_free_consistent(phba->pcidev, num_cq_pages
  2314. * PAGE_SIZE,
  2315. mem->va, mem->dma);
  2316. }
  2317. return ret;
  2318. }
  2319. static int
  2320. beiscsi_create_def_hdr(struct beiscsi_hba *phba,
  2321. struct hwi_context_memory *phwi_context,
  2322. struct hwi_controller *phwi_ctrlr,
  2323. unsigned int def_pdu_ring_sz)
  2324. {
  2325. unsigned int idx;
  2326. int ret;
  2327. struct be_queue_info *dq, *cq;
  2328. struct be_dma_mem *mem;
  2329. struct be_mem_descriptor *mem_descr;
  2330. void *dq_vaddress;
  2331. idx = 0;
  2332. dq = &phwi_context->be_def_hdrq;
  2333. cq = &phwi_context->be_cq[0];
  2334. mem = &dq->dma_mem;
  2335. mem_descr = phba->init_mem;
  2336. mem_descr += HWI_MEM_ASYNC_HEADER_RING;
  2337. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2338. ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
  2339. sizeof(struct phys_addr),
  2340. sizeof(struct phys_addr), dq_vaddress);
  2341. if (ret) {
  2342. shost_printk(KERN_ERR, phba->shost,
  2343. "be_fill_queue Failed for DEF PDU HDR\n");
  2344. return ret;
  2345. }
  2346. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2347. bus_address.u.a64.address;
  2348. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
  2349. def_pdu_ring_sz,
  2350. phba->params.defpdu_hdr_sz);
  2351. if (ret) {
  2352. shost_printk(KERN_ERR, phba->shost,
  2353. "be_cmd_create_default_pdu_queue Failed DEFHDR\n");
  2354. return ret;
  2355. }
  2356. phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
  2357. SE_DEBUG(DBG_LVL_8, "iscsi def pdu id is %d\n",
  2358. phwi_context->be_def_hdrq.id);
  2359. hwi_post_async_buffers(phba, 1);
  2360. return 0;
  2361. }
  2362. static int
  2363. beiscsi_create_def_data(struct beiscsi_hba *phba,
  2364. struct hwi_context_memory *phwi_context,
  2365. struct hwi_controller *phwi_ctrlr,
  2366. unsigned int def_pdu_ring_sz)
  2367. {
  2368. unsigned int idx;
  2369. int ret;
  2370. struct be_queue_info *dataq, *cq;
  2371. struct be_dma_mem *mem;
  2372. struct be_mem_descriptor *mem_descr;
  2373. void *dq_vaddress;
  2374. idx = 0;
  2375. dataq = &phwi_context->be_def_dataq;
  2376. cq = &phwi_context->be_cq[0];
  2377. mem = &dataq->dma_mem;
  2378. mem_descr = phba->init_mem;
  2379. mem_descr += HWI_MEM_ASYNC_DATA_RING;
  2380. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2381. ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
  2382. sizeof(struct phys_addr),
  2383. sizeof(struct phys_addr), dq_vaddress);
  2384. if (ret) {
  2385. shost_printk(KERN_ERR, phba->shost,
  2386. "be_fill_queue Failed for DEF PDU DATA\n");
  2387. return ret;
  2388. }
  2389. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2390. bus_address.u.a64.address;
  2391. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
  2392. def_pdu_ring_sz,
  2393. phba->params.defpdu_data_sz);
  2394. if (ret) {
  2395. shost_printk(KERN_ERR, phba->shost,
  2396. "be_cmd_create_default_pdu_queue Failed"
  2397. " for DEF PDU DATA\n");
  2398. return ret;
  2399. }
  2400. phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
  2401. SE_DEBUG(DBG_LVL_8, "iscsi def data id is %d\n",
  2402. phwi_context->be_def_dataq.id);
  2403. hwi_post_async_buffers(phba, 0);
  2404. SE_DEBUG(DBG_LVL_8, "DEFAULT PDU DATA RING CREATED\n");
  2405. return 0;
  2406. }
  2407. static int
  2408. beiscsi_post_pages(struct beiscsi_hba *phba)
  2409. {
  2410. struct be_mem_descriptor *mem_descr;
  2411. struct mem_array *pm_arr;
  2412. unsigned int page_offset, i;
  2413. struct be_dma_mem sgl;
  2414. int status;
  2415. mem_descr = phba->init_mem;
  2416. mem_descr += HWI_MEM_SGE;
  2417. pm_arr = mem_descr->mem_array;
  2418. page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
  2419. phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
  2420. for (i = 0; i < mem_descr->num_elements; i++) {
  2421. hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
  2422. status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
  2423. page_offset,
  2424. (pm_arr->size / PAGE_SIZE));
  2425. page_offset += pm_arr->size / PAGE_SIZE;
  2426. if (status != 0) {
  2427. shost_printk(KERN_ERR, phba->shost,
  2428. "post sgl failed.\n");
  2429. return status;
  2430. }
  2431. pm_arr++;
  2432. }
  2433. SE_DEBUG(DBG_LVL_8, "POSTED PAGES\n");
  2434. return 0;
  2435. }
  2436. static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
  2437. {
  2438. struct be_dma_mem *mem = &q->dma_mem;
  2439. if (mem->va)
  2440. pci_free_consistent(phba->pcidev, mem->size,
  2441. mem->va, mem->dma);
  2442. }
  2443. static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
  2444. u16 len, u16 entry_size)
  2445. {
  2446. struct be_dma_mem *mem = &q->dma_mem;
  2447. memset(q, 0, sizeof(*q));
  2448. q->len = len;
  2449. q->entry_size = entry_size;
  2450. mem->size = len * entry_size;
  2451. mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
  2452. if (!mem->va)
  2453. return -ENOMEM;
  2454. memset(mem->va, 0, mem->size);
  2455. return 0;
  2456. }
  2457. static int
  2458. beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
  2459. struct hwi_context_memory *phwi_context,
  2460. struct hwi_controller *phwi_ctrlr)
  2461. {
  2462. unsigned int wrb_mem_index, offset, size, num_wrb_rings;
  2463. u64 pa_addr_lo;
  2464. unsigned int idx, num, i;
  2465. struct mem_array *pwrb_arr;
  2466. void *wrb_vaddr;
  2467. struct be_dma_mem sgl;
  2468. struct be_mem_descriptor *mem_descr;
  2469. int status;
  2470. idx = 0;
  2471. mem_descr = phba->init_mem;
  2472. mem_descr += HWI_MEM_WRB;
  2473. pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
  2474. GFP_KERNEL);
  2475. if (!pwrb_arr) {
  2476. shost_printk(KERN_ERR, phba->shost,
  2477. "Memory alloc failed in create wrb ring.\n");
  2478. return -ENOMEM;
  2479. }
  2480. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  2481. pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
  2482. num_wrb_rings = mem_descr->mem_array[idx].size /
  2483. (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
  2484. for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
  2485. if (num_wrb_rings) {
  2486. pwrb_arr[num].virtual_address = wrb_vaddr;
  2487. pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
  2488. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  2489. sizeof(struct iscsi_wrb);
  2490. wrb_vaddr += pwrb_arr[num].size;
  2491. pa_addr_lo += pwrb_arr[num].size;
  2492. num_wrb_rings--;
  2493. } else {
  2494. idx++;
  2495. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  2496. pa_addr_lo = mem_descr->mem_array[idx].\
  2497. bus_address.u.a64.address;
  2498. num_wrb_rings = mem_descr->mem_array[idx].size /
  2499. (phba->params.wrbs_per_cxn *
  2500. sizeof(struct iscsi_wrb));
  2501. pwrb_arr[num].virtual_address = wrb_vaddr;
  2502. pwrb_arr[num].bus_address.u.a64.address\
  2503. = pa_addr_lo;
  2504. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  2505. sizeof(struct iscsi_wrb);
  2506. wrb_vaddr += pwrb_arr[num].size;
  2507. pa_addr_lo += pwrb_arr[num].size;
  2508. num_wrb_rings--;
  2509. }
  2510. }
  2511. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  2512. wrb_mem_index = 0;
  2513. offset = 0;
  2514. size = 0;
  2515. hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
  2516. status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
  2517. &phwi_context->be_wrbq[i]);
  2518. if (status != 0) {
  2519. shost_printk(KERN_ERR, phba->shost,
  2520. "wrbq create failed.");
  2521. kfree(pwrb_arr);
  2522. return status;
  2523. }
  2524. phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
  2525. id;
  2526. }
  2527. kfree(pwrb_arr);
  2528. return 0;
  2529. }
  2530. static void free_wrb_handles(struct beiscsi_hba *phba)
  2531. {
  2532. unsigned int index;
  2533. struct hwi_controller *phwi_ctrlr;
  2534. struct hwi_wrb_context *pwrb_context;
  2535. phwi_ctrlr = phba->phwi_ctrlr;
  2536. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  2537. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2538. kfree(pwrb_context->pwrb_handle_base);
  2539. kfree(pwrb_context->pwrb_handle_basestd);
  2540. }
  2541. }
  2542. static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
  2543. {
  2544. struct be_queue_info *q;
  2545. struct be_ctrl_info *ctrl = &phba->ctrl;
  2546. q = &phba->ctrl.mcc_obj.q;
  2547. if (q->created)
  2548. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
  2549. be_queue_free(phba, q);
  2550. q = &phba->ctrl.mcc_obj.cq;
  2551. if (q->created)
  2552. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  2553. be_queue_free(phba, q);
  2554. }
  2555. static void hwi_cleanup(struct beiscsi_hba *phba)
  2556. {
  2557. struct be_queue_info *q;
  2558. struct be_ctrl_info *ctrl = &phba->ctrl;
  2559. struct hwi_controller *phwi_ctrlr;
  2560. struct hwi_context_memory *phwi_context;
  2561. int i, eq_num;
  2562. phwi_ctrlr = phba->phwi_ctrlr;
  2563. phwi_context = phwi_ctrlr->phwi_ctxt;
  2564. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  2565. q = &phwi_context->be_wrbq[i];
  2566. if (q->created)
  2567. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
  2568. }
  2569. free_wrb_handles(phba);
  2570. q = &phwi_context->be_def_hdrq;
  2571. if (q->created)
  2572. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  2573. q = &phwi_context->be_def_dataq;
  2574. if (q->created)
  2575. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  2576. beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
  2577. for (i = 0; i < (phba->num_cpus); i++) {
  2578. q = &phwi_context->be_cq[i];
  2579. if (q->created)
  2580. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  2581. }
  2582. if (phba->msix_enabled)
  2583. eq_num = 1;
  2584. else
  2585. eq_num = 0;
  2586. for (i = 0; i < (phba->num_cpus + eq_num); i++) {
  2587. q = &phwi_context->be_eq[i].q;
  2588. if (q->created)
  2589. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
  2590. }
  2591. be_mcc_queues_destroy(phba);
  2592. }
  2593. static int be_mcc_queues_create(struct beiscsi_hba *phba,
  2594. struct hwi_context_memory *phwi_context)
  2595. {
  2596. struct be_queue_info *q, *cq;
  2597. struct be_ctrl_info *ctrl = &phba->ctrl;
  2598. /* Alloc MCC compl queue */
  2599. cq = &phba->ctrl.mcc_obj.cq;
  2600. if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
  2601. sizeof(struct be_mcc_compl)))
  2602. goto err;
  2603. /* Ask BE to create MCC compl queue; */
  2604. if (phba->msix_enabled) {
  2605. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
  2606. [phba->num_cpus].q, false, true, 0))
  2607. goto mcc_cq_free;
  2608. } else {
  2609. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
  2610. false, true, 0))
  2611. goto mcc_cq_free;
  2612. }
  2613. /* Alloc MCC queue */
  2614. q = &phba->ctrl.mcc_obj.q;
  2615. if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
  2616. goto mcc_cq_destroy;
  2617. /* Ask BE to create MCC queue */
  2618. if (beiscsi_cmd_mccq_create(phba, q, cq))
  2619. goto mcc_q_free;
  2620. return 0;
  2621. mcc_q_free:
  2622. be_queue_free(phba, q);
  2623. mcc_cq_destroy:
  2624. beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
  2625. mcc_cq_free:
  2626. be_queue_free(phba, cq);
  2627. err:
  2628. return -ENOMEM;
  2629. }
  2630. static int find_num_cpus(void)
  2631. {
  2632. int num_cpus = 0;
  2633. num_cpus = num_online_cpus();
  2634. if (num_cpus >= MAX_CPUS)
  2635. num_cpus = MAX_CPUS - 1;
  2636. SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", num_cpus);
  2637. return num_cpus;
  2638. }
  2639. static int hwi_init_port(struct beiscsi_hba *phba)
  2640. {
  2641. struct hwi_controller *phwi_ctrlr;
  2642. struct hwi_context_memory *phwi_context;
  2643. unsigned int def_pdu_ring_sz;
  2644. struct be_ctrl_info *ctrl = &phba->ctrl;
  2645. int status;
  2646. def_pdu_ring_sz =
  2647. phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
  2648. phwi_ctrlr = phba->phwi_ctrlr;
  2649. phwi_context = phwi_ctrlr->phwi_ctxt;
  2650. phwi_context->max_eqd = 0;
  2651. phwi_context->min_eqd = 0;
  2652. phwi_context->cur_eqd = 64;
  2653. be_cmd_fw_initialize(&phba->ctrl);
  2654. status = beiscsi_create_eqs(phba, phwi_context);
  2655. if (status != 0) {
  2656. shost_printk(KERN_ERR, phba->shost, "EQ not created\n");
  2657. goto error;
  2658. }
  2659. status = be_mcc_queues_create(phba, phwi_context);
  2660. if (status != 0)
  2661. goto error;
  2662. status = mgmt_check_supported_fw(ctrl, phba);
  2663. if (status != 0) {
  2664. shost_printk(KERN_ERR, phba->shost,
  2665. "Unsupported fw version\n");
  2666. goto error;
  2667. }
  2668. status = beiscsi_create_cqs(phba, phwi_context);
  2669. if (status != 0) {
  2670. shost_printk(KERN_ERR, phba->shost, "CQ not created\n");
  2671. goto error;
  2672. }
  2673. status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
  2674. def_pdu_ring_sz);
  2675. if (status != 0) {
  2676. shost_printk(KERN_ERR, phba->shost,
  2677. "Default Header not created\n");
  2678. goto error;
  2679. }
  2680. status = beiscsi_create_def_data(phba, phwi_context,
  2681. phwi_ctrlr, def_pdu_ring_sz);
  2682. if (status != 0) {
  2683. shost_printk(KERN_ERR, phba->shost,
  2684. "Default Data not created\n");
  2685. goto error;
  2686. }
  2687. status = beiscsi_post_pages(phba);
  2688. if (status != 0) {
  2689. shost_printk(KERN_ERR, phba->shost, "Post SGL Pages Failed\n");
  2690. goto error;
  2691. }
  2692. status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
  2693. if (status != 0) {
  2694. shost_printk(KERN_ERR, phba->shost,
  2695. "WRB Rings not created\n");
  2696. goto error;
  2697. }
  2698. SE_DEBUG(DBG_LVL_8, "hwi_init_port success\n");
  2699. return 0;
  2700. error:
  2701. shost_printk(KERN_ERR, phba->shost, "hwi_init_port failed");
  2702. hwi_cleanup(phba);
  2703. return -ENOMEM;
  2704. }
  2705. static int hwi_init_controller(struct beiscsi_hba *phba)
  2706. {
  2707. struct hwi_controller *phwi_ctrlr;
  2708. phwi_ctrlr = phba->phwi_ctrlr;
  2709. if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
  2710. phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
  2711. init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
  2712. SE_DEBUG(DBG_LVL_8, " phwi_ctrlr->phwi_ctxt=%p\n",
  2713. phwi_ctrlr->phwi_ctxt);
  2714. } else {
  2715. shost_printk(KERN_ERR, phba->shost,
  2716. "HWI_MEM_ADDN_CONTEXT is more than one element."
  2717. "Failing to load\n");
  2718. return -ENOMEM;
  2719. }
  2720. iscsi_init_global_templates(phba);
  2721. beiscsi_init_wrb_handle(phba);
  2722. hwi_init_async_pdu_ctx(phba);
  2723. if (hwi_init_port(phba) != 0) {
  2724. shost_printk(KERN_ERR, phba->shost,
  2725. "hwi_init_controller failed\n");
  2726. return -ENOMEM;
  2727. }
  2728. return 0;
  2729. }
  2730. static void beiscsi_free_mem(struct beiscsi_hba *phba)
  2731. {
  2732. struct be_mem_descriptor *mem_descr;
  2733. int i, j;
  2734. mem_descr = phba->init_mem;
  2735. i = 0;
  2736. j = 0;
  2737. for (i = 0; i < SE_MEM_MAX; i++) {
  2738. for (j = mem_descr->num_elements; j > 0; j--) {
  2739. pci_free_consistent(phba->pcidev,
  2740. mem_descr->mem_array[j - 1].size,
  2741. mem_descr->mem_array[j - 1].virtual_address,
  2742. (unsigned long)mem_descr->mem_array[j - 1].
  2743. bus_address.u.a64.address);
  2744. }
  2745. kfree(mem_descr->mem_array);
  2746. mem_descr++;
  2747. }
  2748. kfree(phba->init_mem);
  2749. kfree(phba->phwi_ctrlr);
  2750. }
  2751. static int beiscsi_init_controller(struct beiscsi_hba *phba)
  2752. {
  2753. int ret = -ENOMEM;
  2754. ret = beiscsi_get_memory(phba);
  2755. if (ret < 0) {
  2756. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe -"
  2757. "Failed in beiscsi_alloc_memory\n");
  2758. return ret;
  2759. }
  2760. ret = hwi_init_controller(phba);
  2761. if (ret)
  2762. goto free_init;
  2763. SE_DEBUG(DBG_LVL_8, "Return success from beiscsi_init_controller");
  2764. return 0;
  2765. free_init:
  2766. beiscsi_free_mem(phba);
  2767. return -ENOMEM;
  2768. }
  2769. static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
  2770. {
  2771. struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
  2772. struct sgl_handle *psgl_handle;
  2773. struct iscsi_sge *pfrag;
  2774. unsigned int arr_index, i, idx;
  2775. phba->io_sgl_hndl_avbl = 0;
  2776. phba->eh_sgl_hndl_avbl = 0;
  2777. mem_descr_sglh = phba->init_mem;
  2778. mem_descr_sglh += HWI_MEM_SGLH;
  2779. if (1 == mem_descr_sglh->num_elements) {
  2780. phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  2781. phba->params.ios_per_ctrl,
  2782. GFP_KERNEL);
  2783. if (!phba->io_sgl_hndl_base) {
  2784. shost_printk(KERN_ERR, phba->shost,
  2785. "Mem Alloc Failed. Failing to load\n");
  2786. return -ENOMEM;
  2787. }
  2788. phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  2789. (phba->params.icds_per_ctrl -
  2790. phba->params.ios_per_ctrl),
  2791. GFP_KERNEL);
  2792. if (!phba->eh_sgl_hndl_base) {
  2793. kfree(phba->io_sgl_hndl_base);
  2794. shost_printk(KERN_ERR, phba->shost,
  2795. "Mem Alloc Failed. Failing to load\n");
  2796. return -ENOMEM;
  2797. }
  2798. } else {
  2799. shost_printk(KERN_ERR, phba->shost,
  2800. "HWI_MEM_SGLH is more than one element."
  2801. "Failing to load\n");
  2802. return -ENOMEM;
  2803. }
  2804. arr_index = 0;
  2805. idx = 0;
  2806. while (idx < mem_descr_sglh->num_elements) {
  2807. psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
  2808. for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
  2809. sizeof(struct sgl_handle)); i++) {
  2810. if (arr_index < phba->params.ios_per_ctrl) {
  2811. phba->io_sgl_hndl_base[arr_index] = psgl_handle;
  2812. phba->io_sgl_hndl_avbl++;
  2813. arr_index++;
  2814. } else {
  2815. phba->eh_sgl_hndl_base[arr_index -
  2816. phba->params.ios_per_ctrl] =
  2817. psgl_handle;
  2818. arr_index++;
  2819. phba->eh_sgl_hndl_avbl++;
  2820. }
  2821. psgl_handle++;
  2822. }
  2823. idx++;
  2824. }
  2825. SE_DEBUG(DBG_LVL_8,
  2826. "phba->io_sgl_hndl_avbl=%d"
  2827. "phba->eh_sgl_hndl_avbl=%d\n",
  2828. phba->io_sgl_hndl_avbl,
  2829. phba->eh_sgl_hndl_avbl);
  2830. mem_descr_sg = phba->init_mem;
  2831. mem_descr_sg += HWI_MEM_SGE;
  2832. SE_DEBUG(DBG_LVL_8, "\n mem_descr_sg->num_elements=%d\n",
  2833. mem_descr_sg->num_elements);
  2834. arr_index = 0;
  2835. idx = 0;
  2836. while (idx < mem_descr_sg->num_elements) {
  2837. pfrag = mem_descr_sg->mem_array[idx].virtual_address;
  2838. for (i = 0;
  2839. i < (mem_descr_sg->mem_array[idx].size) /
  2840. (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
  2841. i++) {
  2842. if (arr_index < phba->params.ios_per_ctrl)
  2843. psgl_handle = phba->io_sgl_hndl_base[arr_index];
  2844. else
  2845. psgl_handle = phba->eh_sgl_hndl_base[arr_index -
  2846. phba->params.ios_per_ctrl];
  2847. psgl_handle->pfrag = pfrag;
  2848. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
  2849. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
  2850. pfrag += phba->params.num_sge_per_io;
  2851. psgl_handle->sgl_index =
  2852. phba->fw_config.iscsi_icd_start + arr_index++;
  2853. }
  2854. idx++;
  2855. }
  2856. phba->io_sgl_free_index = 0;
  2857. phba->io_sgl_alloc_index = 0;
  2858. phba->eh_sgl_free_index = 0;
  2859. phba->eh_sgl_alloc_index = 0;
  2860. return 0;
  2861. }
  2862. static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
  2863. {
  2864. int i, new_cid;
  2865. phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
  2866. GFP_KERNEL);
  2867. if (!phba->cid_array) {
  2868. shost_printk(KERN_ERR, phba->shost,
  2869. "Failed to allocate memory in "
  2870. "hba_setup_cid_tbls\n");
  2871. return -ENOMEM;
  2872. }
  2873. phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
  2874. phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
  2875. if (!phba->ep_array) {
  2876. shost_printk(KERN_ERR, phba->shost,
  2877. "Failed to allocate memory in "
  2878. "hba_setup_cid_tbls\n");
  2879. kfree(phba->cid_array);
  2880. return -ENOMEM;
  2881. }
  2882. new_cid = phba->fw_config.iscsi_cid_start;
  2883. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  2884. phba->cid_array[i] = new_cid;
  2885. new_cid += 2;
  2886. }
  2887. phba->avlbl_cids = phba->params.cxns_per_ctrl;
  2888. return 0;
  2889. }
  2890. static void hwi_enable_intr(struct beiscsi_hba *phba)
  2891. {
  2892. struct be_ctrl_info *ctrl = &phba->ctrl;
  2893. struct hwi_controller *phwi_ctrlr;
  2894. struct hwi_context_memory *phwi_context;
  2895. struct be_queue_info *eq;
  2896. u8 __iomem *addr;
  2897. u32 reg, i;
  2898. u32 enabled;
  2899. phwi_ctrlr = phba->phwi_ctrlr;
  2900. phwi_context = phwi_ctrlr->phwi_ctxt;
  2901. addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
  2902. PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
  2903. reg = ioread32(addr);
  2904. SE_DEBUG(DBG_LVL_8, "reg =x%08x\n", reg);
  2905. enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  2906. if (!enabled) {
  2907. reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  2908. SE_DEBUG(DBG_LVL_8, "reg =x%08x addr=%p\n", reg, addr);
  2909. iowrite32(reg, addr);
  2910. if (!phba->msix_enabled) {
  2911. eq = &phwi_context->be_eq[0].q;
  2912. SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
  2913. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  2914. } else {
  2915. for (i = 0; i <= phba->num_cpus; i++) {
  2916. eq = &phwi_context->be_eq[i].q;
  2917. SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
  2918. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  2919. }
  2920. }
  2921. }
  2922. }
  2923. static void hwi_disable_intr(struct beiscsi_hba *phba)
  2924. {
  2925. struct be_ctrl_info *ctrl = &phba->ctrl;
  2926. u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
  2927. u32 reg = ioread32(addr);
  2928. u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  2929. if (enabled) {
  2930. reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  2931. iowrite32(reg, addr);
  2932. } else
  2933. shost_printk(KERN_WARNING, phba->shost,
  2934. "In hwi_disable_intr, Already Disabled\n");
  2935. }
  2936. static int beiscsi_init_port(struct beiscsi_hba *phba)
  2937. {
  2938. int ret;
  2939. ret = beiscsi_init_controller(phba);
  2940. if (ret < 0) {
  2941. shost_printk(KERN_ERR, phba->shost,
  2942. "beiscsi_dev_probe - Failed in"
  2943. "beiscsi_init_controller\n");
  2944. return ret;
  2945. }
  2946. ret = beiscsi_init_sgl_handle(phba);
  2947. if (ret < 0) {
  2948. shost_printk(KERN_ERR, phba->shost,
  2949. "beiscsi_dev_probe - Failed in"
  2950. "beiscsi_init_sgl_handle\n");
  2951. goto do_cleanup_ctrlr;
  2952. }
  2953. if (hba_setup_cid_tbls(phba)) {
  2954. shost_printk(KERN_ERR, phba->shost,
  2955. "Failed in hba_setup_cid_tbls\n");
  2956. kfree(phba->io_sgl_hndl_base);
  2957. kfree(phba->eh_sgl_hndl_base);
  2958. goto do_cleanup_ctrlr;
  2959. }
  2960. return ret;
  2961. do_cleanup_ctrlr:
  2962. hwi_cleanup(phba);
  2963. return ret;
  2964. }
  2965. static void hwi_purge_eq(struct beiscsi_hba *phba)
  2966. {
  2967. struct hwi_controller *phwi_ctrlr;
  2968. struct hwi_context_memory *phwi_context;
  2969. struct be_queue_info *eq;
  2970. struct be_eq_entry *eqe = NULL;
  2971. int i, eq_msix;
  2972. unsigned int num_processed;
  2973. phwi_ctrlr = phba->phwi_ctrlr;
  2974. phwi_context = phwi_ctrlr->phwi_ctxt;
  2975. if (phba->msix_enabled)
  2976. eq_msix = 1;
  2977. else
  2978. eq_msix = 0;
  2979. for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
  2980. eq = &phwi_context->be_eq[i].q;
  2981. eqe = queue_tail_node(eq);
  2982. num_processed = 0;
  2983. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  2984. & EQE_VALID_MASK) {
  2985. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  2986. queue_tail_inc(eq);
  2987. eqe = queue_tail_node(eq);
  2988. num_processed++;
  2989. }
  2990. if (num_processed)
  2991. hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
  2992. }
  2993. }
  2994. static void beiscsi_clean_port(struct beiscsi_hba *phba)
  2995. {
  2996. int mgmt_status;
  2997. mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
  2998. if (mgmt_status)
  2999. shost_printk(KERN_WARNING, phba->shost,
  3000. "mgmt_epfw_cleanup FAILED\n");
  3001. hwi_purge_eq(phba);
  3002. hwi_cleanup(phba);
  3003. kfree(phba->io_sgl_hndl_base);
  3004. kfree(phba->eh_sgl_hndl_base);
  3005. kfree(phba->cid_array);
  3006. kfree(phba->ep_array);
  3007. }
  3008. void
  3009. beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
  3010. struct beiscsi_offload_params *params)
  3011. {
  3012. struct wrb_handle *pwrb_handle;
  3013. struct iscsi_target_context_update_wrb *pwrb = NULL;
  3014. struct be_mem_descriptor *mem_descr;
  3015. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3016. u32 doorbell = 0;
  3017. /*
  3018. * We can always use 0 here because it is reserved by libiscsi for
  3019. * login/startup related tasks.
  3020. */
  3021. pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
  3022. phba->fw_config.iscsi_cid_start));
  3023. pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
  3024. memset(pwrb, 0, sizeof(*pwrb));
  3025. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3026. max_burst_length, pwrb, params->dw[offsetof
  3027. (struct amap_beiscsi_offload_params,
  3028. max_burst_length) / 32]);
  3029. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3030. max_send_data_segment_length, pwrb,
  3031. params->dw[offsetof(struct amap_beiscsi_offload_params,
  3032. max_send_data_segment_length) / 32]);
  3033. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3034. first_burst_length,
  3035. pwrb,
  3036. params->dw[offsetof(struct amap_beiscsi_offload_params,
  3037. first_burst_length) / 32]);
  3038. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
  3039. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3040. erl) / 32] & OFFLD_PARAMS_ERL));
  3041. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
  3042. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3043. dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
  3044. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
  3045. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3046. hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
  3047. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
  3048. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3049. ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
  3050. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
  3051. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3052. imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
  3053. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
  3054. pwrb,
  3055. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3056. exp_statsn) / 32] + 1));
  3057. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
  3058. 0x7);
  3059. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
  3060. pwrb, pwrb_handle->wrb_index);
  3061. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
  3062. pwrb, pwrb_handle->nxt_wrb_index);
  3063. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3064. session_state, pwrb, 0);
  3065. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
  3066. pwrb, 1);
  3067. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
  3068. pwrb, 0);
  3069. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
  3070. 0);
  3071. mem_descr = phba->init_mem;
  3072. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  3073. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3074. pad_buffer_addr_hi, pwrb,
  3075. mem_descr->mem_array[0].bus_address.u.a32.address_hi);
  3076. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3077. pad_buffer_addr_lo, pwrb,
  3078. mem_descr->mem_array[0].bus_address.u.a32.address_lo);
  3079. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
  3080. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3081. doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
  3082. << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3083. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3084. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3085. }
  3086. static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
  3087. int *index, int *age)
  3088. {
  3089. *index = (int)itt;
  3090. if (age)
  3091. *age = conn->session->age;
  3092. }
  3093. /**
  3094. * beiscsi_alloc_pdu - allocates pdu and related resources
  3095. * @task: libiscsi task
  3096. * @opcode: opcode of pdu for task
  3097. *
  3098. * This is called with the session lock held. It will allocate
  3099. * the wrb and sgl if needed for the command. And it will prep
  3100. * the pdu's itt. beiscsi_parse_pdu will later translate
  3101. * the pdu itt to the libiscsi task itt.
  3102. */
  3103. static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  3104. {
  3105. struct beiscsi_io_task *io_task = task->dd_data;
  3106. struct iscsi_conn *conn = task->conn;
  3107. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3108. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3109. struct hwi_wrb_context *pwrb_context;
  3110. struct hwi_controller *phwi_ctrlr;
  3111. itt_t itt;
  3112. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3113. dma_addr_t paddr;
  3114. io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
  3115. GFP_KERNEL, &paddr);
  3116. if (!io_task->cmd_bhs)
  3117. return -ENOMEM;
  3118. io_task->bhs_pa.u.a64.address = paddr;
  3119. io_task->libiscsi_itt = (itt_t)task->itt;
  3120. io_task->conn = beiscsi_conn;
  3121. task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
  3122. task->hdr_max = sizeof(struct be_cmd_bhs);
  3123. io_task->psgl_handle = NULL;
  3124. io_task->psgl_handle = NULL;
  3125. if (task->sc) {
  3126. spin_lock(&phba->io_sgl_lock);
  3127. io_task->psgl_handle = alloc_io_sgl_handle(phba);
  3128. spin_unlock(&phba->io_sgl_lock);
  3129. if (!io_task->psgl_handle)
  3130. goto free_hndls;
  3131. io_task->pwrb_handle = alloc_wrb_handle(phba,
  3132. beiscsi_conn->beiscsi_conn_cid -
  3133. phba->fw_config.iscsi_cid_start);
  3134. if (!io_task->pwrb_handle)
  3135. goto free_io_hndls;
  3136. } else {
  3137. io_task->scsi_cmnd = NULL;
  3138. if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
  3139. if (!beiscsi_conn->login_in_progress) {
  3140. spin_lock(&phba->mgmt_sgl_lock);
  3141. io_task->psgl_handle = (struct sgl_handle *)
  3142. alloc_mgmt_sgl_handle(phba);
  3143. spin_unlock(&phba->mgmt_sgl_lock);
  3144. if (!io_task->psgl_handle)
  3145. goto free_hndls;
  3146. beiscsi_conn->login_in_progress = 1;
  3147. beiscsi_conn->plogin_sgl_handle =
  3148. io_task->psgl_handle;
  3149. io_task->pwrb_handle =
  3150. alloc_wrb_handle(phba,
  3151. beiscsi_conn->beiscsi_conn_cid -
  3152. phba->fw_config.iscsi_cid_start);
  3153. if (!io_task->pwrb_handle)
  3154. goto free_io_hndls;
  3155. beiscsi_conn->plogin_wrb_handle =
  3156. io_task->pwrb_handle;
  3157. } else {
  3158. io_task->psgl_handle =
  3159. beiscsi_conn->plogin_sgl_handle;
  3160. io_task->pwrb_handle =
  3161. beiscsi_conn->plogin_wrb_handle;
  3162. }
  3163. } else {
  3164. spin_lock(&phba->mgmt_sgl_lock);
  3165. io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
  3166. spin_unlock(&phba->mgmt_sgl_lock);
  3167. if (!io_task->psgl_handle)
  3168. goto free_hndls;
  3169. io_task->pwrb_handle =
  3170. alloc_wrb_handle(phba,
  3171. beiscsi_conn->beiscsi_conn_cid -
  3172. phba->fw_config.iscsi_cid_start);
  3173. if (!io_task->pwrb_handle)
  3174. goto free_mgmt_hndls;
  3175. }
  3176. }
  3177. itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
  3178. wrb_index << 16) | (unsigned int)
  3179. (io_task->psgl_handle->sgl_index));
  3180. io_task->pwrb_handle->pio_handle = task;
  3181. io_task->cmd_bhs->iscsi_hdr.itt = itt;
  3182. return 0;
  3183. free_io_hndls:
  3184. spin_lock(&phba->io_sgl_lock);
  3185. free_io_sgl_handle(phba, io_task->psgl_handle);
  3186. spin_unlock(&phba->io_sgl_lock);
  3187. goto free_hndls;
  3188. free_mgmt_hndls:
  3189. spin_lock(&phba->mgmt_sgl_lock);
  3190. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  3191. spin_unlock(&phba->mgmt_sgl_lock);
  3192. free_hndls:
  3193. phwi_ctrlr = phba->phwi_ctrlr;
  3194. pwrb_context = &phwi_ctrlr->wrb_context[
  3195. beiscsi_conn->beiscsi_conn_cid -
  3196. phba->fw_config.iscsi_cid_start];
  3197. if (io_task->pwrb_handle)
  3198. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  3199. io_task->pwrb_handle = NULL;
  3200. pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3201. io_task->bhs_pa.u.a64.address);
  3202. SE_DEBUG(DBG_LVL_1, "Alloc of SGL_ICD Failed\n");
  3203. return -ENOMEM;
  3204. }
  3205. static void beiscsi_cleanup_task(struct iscsi_task *task)
  3206. {
  3207. struct beiscsi_io_task *io_task = task->dd_data;
  3208. struct iscsi_conn *conn = task->conn;
  3209. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3210. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3211. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3212. struct hwi_wrb_context *pwrb_context;
  3213. struct hwi_controller *phwi_ctrlr;
  3214. phwi_ctrlr = phba->phwi_ctrlr;
  3215. pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
  3216. - phba->fw_config.iscsi_cid_start];
  3217. if (io_task->pwrb_handle) {
  3218. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  3219. io_task->pwrb_handle = NULL;
  3220. }
  3221. if (io_task->cmd_bhs) {
  3222. pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3223. io_task->bhs_pa.u.a64.address);
  3224. }
  3225. if (task->sc) {
  3226. if (io_task->psgl_handle) {
  3227. spin_lock(&phba->io_sgl_lock);
  3228. free_io_sgl_handle(phba, io_task->psgl_handle);
  3229. spin_unlock(&phba->io_sgl_lock);
  3230. io_task->psgl_handle = NULL;
  3231. }
  3232. } else {
  3233. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN)
  3234. return;
  3235. if (io_task->psgl_handle) {
  3236. spin_lock(&phba->mgmt_sgl_lock);
  3237. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  3238. spin_unlock(&phba->mgmt_sgl_lock);
  3239. io_task->psgl_handle = NULL;
  3240. }
  3241. }
  3242. }
  3243. static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
  3244. unsigned int num_sg, unsigned int xferlen,
  3245. unsigned int writedir)
  3246. {
  3247. struct beiscsi_io_task *io_task = task->dd_data;
  3248. struct iscsi_conn *conn = task->conn;
  3249. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3250. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3251. struct iscsi_wrb *pwrb = NULL;
  3252. unsigned int doorbell = 0;
  3253. pwrb = io_task->pwrb_handle->pwrb;
  3254. io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
  3255. io_task->bhs_len = sizeof(struct be_cmd_bhs);
  3256. if (writedir) {
  3257. memset(&io_task->cmd_bhs->iscsi_data_pdu, 0, 48);
  3258. AMAP_SET_BITS(struct amap_pdu_data_out, itt,
  3259. &io_task->cmd_bhs->iscsi_data_pdu,
  3260. (unsigned int)io_task->cmd_bhs->iscsi_hdr.itt);
  3261. AMAP_SET_BITS(struct amap_pdu_data_out, opcode,
  3262. &io_task->cmd_bhs->iscsi_data_pdu,
  3263. ISCSI_OPCODE_SCSI_DATA_OUT);
  3264. AMAP_SET_BITS(struct amap_pdu_data_out, final_bit,
  3265. &io_task->cmd_bhs->iscsi_data_pdu, 1);
  3266. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3267. INI_WR_CMD);
  3268. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  3269. } else {
  3270. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3271. INI_RD_CMD);
  3272. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  3273. }
  3274. memcpy(&io_task->cmd_bhs->iscsi_data_pdu.
  3275. dw[offsetof(struct amap_pdu_data_out, lun) / 32],
  3276. io_task->cmd_bhs->iscsi_hdr.lun, sizeof(struct scsi_lun));
  3277. AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
  3278. cpu_to_be16((unsigned short)io_task->cmd_bhs->iscsi_hdr.
  3279. lun[0]));
  3280. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
  3281. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  3282. io_task->pwrb_handle->wrb_index);
  3283. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  3284. be32_to_cpu(task->cmdsn));
  3285. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  3286. io_task->psgl_handle->sgl_index);
  3287. hwi_write_sgl(pwrb, sg, num_sg, io_task);
  3288. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  3289. io_task->pwrb_handle->nxt_wrb_index);
  3290. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  3291. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3292. doorbell |= (io_task->pwrb_handle->wrb_index &
  3293. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3294. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3295. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3296. return 0;
  3297. }
  3298. static int beiscsi_mtask(struct iscsi_task *task)
  3299. {
  3300. struct beiscsi_io_task *io_task = task->dd_data;
  3301. struct iscsi_conn *conn = task->conn;
  3302. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3303. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3304. struct iscsi_wrb *pwrb = NULL;
  3305. unsigned int doorbell = 0;
  3306. unsigned int cid;
  3307. cid = beiscsi_conn->beiscsi_conn_cid;
  3308. pwrb = io_task->pwrb_handle->pwrb;
  3309. memset(pwrb, 0, sizeof(*pwrb));
  3310. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  3311. be32_to_cpu(task->cmdsn));
  3312. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  3313. io_task->pwrb_handle->wrb_index);
  3314. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  3315. io_task->psgl_handle->sgl_index);
  3316. switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
  3317. case ISCSI_OP_LOGIN:
  3318. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3319. TGT_DM_CMD);
  3320. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3321. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
  3322. hwi_write_buffer(pwrb, task);
  3323. break;
  3324. case ISCSI_OP_NOOP_OUT:
  3325. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3326. INI_RD_CMD);
  3327. if (task->hdr->ttt == ISCSI_RESERVED_TAG)
  3328. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3329. else
  3330. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
  3331. hwi_write_buffer(pwrb, task);
  3332. break;
  3333. case ISCSI_OP_TEXT:
  3334. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3335. TGT_DM_CMD);
  3336. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3337. hwi_write_buffer(pwrb, task);
  3338. break;
  3339. case ISCSI_OP_SCSI_TMFUNC:
  3340. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3341. INI_TMF_CMD);
  3342. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3343. hwi_write_buffer(pwrb, task);
  3344. break;
  3345. case ISCSI_OP_LOGOUT:
  3346. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3347. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3348. HWH_TYPE_LOGOUT);
  3349. hwi_write_buffer(pwrb, task);
  3350. break;
  3351. default:
  3352. SE_DEBUG(DBG_LVL_1, "opcode =%d Not supported\n",
  3353. task->hdr->opcode & ISCSI_OPCODE_MASK);
  3354. return -EINVAL;
  3355. }
  3356. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
  3357. task->data_count);
  3358. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  3359. io_task->pwrb_handle->nxt_wrb_index);
  3360. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  3361. doorbell |= cid & DB_WRB_POST_CID_MASK;
  3362. doorbell |= (io_task->pwrb_handle->wrb_index &
  3363. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3364. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3365. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3366. return 0;
  3367. }
  3368. static int beiscsi_task_xmit(struct iscsi_task *task)
  3369. {
  3370. struct beiscsi_io_task *io_task = task->dd_data;
  3371. struct scsi_cmnd *sc = task->sc;
  3372. struct scatterlist *sg;
  3373. int num_sg;
  3374. unsigned int writedir = 0, xferlen = 0;
  3375. if (!sc)
  3376. return beiscsi_mtask(task);
  3377. io_task->scsi_cmnd = sc;
  3378. num_sg = scsi_dma_map(sc);
  3379. if (num_sg < 0) {
  3380. SE_DEBUG(DBG_LVL_1, " scsi_dma_map Failed\n")
  3381. return num_sg;
  3382. }
  3383. xferlen = scsi_bufflen(sc);
  3384. sg = scsi_sglist(sc);
  3385. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  3386. writedir = 1;
  3387. SE_DEBUG(DBG_LVL_4, "task->imm_count=0x%08x\n",
  3388. task->imm_count);
  3389. } else
  3390. writedir = 0;
  3391. return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
  3392. }
  3393. static void beiscsi_remove(struct pci_dev *pcidev)
  3394. {
  3395. struct beiscsi_hba *phba = NULL;
  3396. struct hwi_controller *phwi_ctrlr;
  3397. struct hwi_context_memory *phwi_context;
  3398. struct be_eq_obj *pbe_eq;
  3399. unsigned int i, msix_vec;
  3400. u8 *real_offset = 0;
  3401. u32 value = 0;
  3402. phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
  3403. if (!phba) {
  3404. dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
  3405. return;
  3406. }
  3407. phwi_ctrlr = phba->phwi_ctrlr;
  3408. phwi_context = phwi_ctrlr->phwi_ctxt;
  3409. hwi_disable_intr(phba);
  3410. if (phba->msix_enabled) {
  3411. for (i = 0; i <= phba->num_cpus; i++) {
  3412. msix_vec = phba->msix_entries[i].vector;
  3413. free_irq(msix_vec, &phwi_context->be_eq[i]);
  3414. }
  3415. } else
  3416. if (phba->pcidev->irq)
  3417. free_irq(phba->pcidev->irq, phba);
  3418. pci_disable_msix(phba->pcidev);
  3419. destroy_workqueue(phba->wq);
  3420. if (blk_iopoll_enabled)
  3421. for (i = 0; i < phba->num_cpus; i++) {
  3422. pbe_eq = &phwi_context->be_eq[i];
  3423. blk_iopoll_disable(&pbe_eq->iopoll);
  3424. }
  3425. beiscsi_clean_port(phba);
  3426. beiscsi_free_mem(phba);
  3427. real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
  3428. value = readl((void *)real_offset);
  3429. if (value & 0x00010000) {
  3430. value &= 0xfffeffff;
  3431. writel(value, (void *)real_offset);
  3432. }
  3433. beiscsi_unmap_pci_function(phba);
  3434. pci_free_consistent(phba->pcidev,
  3435. phba->ctrl.mbox_mem_alloced.size,
  3436. phba->ctrl.mbox_mem_alloced.va,
  3437. phba->ctrl.mbox_mem_alloced.dma);
  3438. iscsi_host_remove(phba->shost);
  3439. pci_dev_put(phba->pcidev);
  3440. iscsi_host_free(phba->shost);
  3441. }
  3442. static void beiscsi_msix_enable(struct beiscsi_hba *phba)
  3443. {
  3444. int i, status;
  3445. for (i = 0; i <= phba->num_cpus; i++)
  3446. phba->msix_entries[i].entry = i;
  3447. status = pci_enable_msix(phba->pcidev, phba->msix_entries,
  3448. (phba->num_cpus + 1));
  3449. if (!status)
  3450. phba->msix_enabled = true;
  3451. return;
  3452. }
  3453. static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
  3454. const struct pci_device_id *id)
  3455. {
  3456. struct beiscsi_hba *phba = NULL;
  3457. struct hwi_controller *phwi_ctrlr;
  3458. struct hwi_context_memory *phwi_context;
  3459. struct be_eq_obj *pbe_eq;
  3460. int ret, num_cpus, i;
  3461. u8 *real_offset = 0;
  3462. u32 value = 0;
  3463. ret = beiscsi_enable_pci(pcidev);
  3464. if (ret < 0) {
  3465. dev_err(&pcidev->dev, "beiscsi_dev_probe-"
  3466. " Failed to enable pci device\n");
  3467. return ret;
  3468. }
  3469. phba = beiscsi_hba_alloc(pcidev);
  3470. if (!phba) {
  3471. dev_err(&pcidev->dev, "beiscsi_dev_probe-"
  3472. " Failed in beiscsi_hba_alloc\n");
  3473. goto disable_pci;
  3474. }
  3475. switch (pcidev->device) {
  3476. case BE_DEVICE_ID1:
  3477. case OC_DEVICE_ID1:
  3478. case OC_DEVICE_ID2:
  3479. phba->generation = BE_GEN2;
  3480. break;
  3481. case BE_DEVICE_ID2:
  3482. case OC_DEVICE_ID3:
  3483. phba->generation = BE_GEN3;
  3484. break;
  3485. default:
  3486. phba->generation = 0;
  3487. }
  3488. if (enable_msix)
  3489. num_cpus = find_num_cpus();
  3490. else
  3491. num_cpus = 1;
  3492. phba->num_cpus = num_cpus;
  3493. SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", phba->num_cpus);
  3494. if (enable_msix)
  3495. beiscsi_msix_enable(phba);
  3496. ret = be_ctrl_init(phba, pcidev);
  3497. if (ret) {
  3498. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3499. "Failed in be_ctrl_init\n");
  3500. goto hba_free;
  3501. }
  3502. if (!num_hba) {
  3503. real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
  3504. value = readl((void *)real_offset);
  3505. if (value & 0x00010000) {
  3506. gcrashmode++;
  3507. shost_printk(KERN_ERR, phba->shost,
  3508. "Loading Driver in crashdump mode\n");
  3509. ret = beiscsi_pci_soft_reset(phba);
  3510. if (ret) {
  3511. shost_printk(KERN_ERR, phba->shost,
  3512. "Reset Failed. Aborting Crashdump\n");
  3513. goto hba_free;
  3514. }
  3515. ret = be_chk_reset_complete(phba);
  3516. if (ret) {
  3517. shost_printk(KERN_ERR, phba->shost,
  3518. "Failed to get out of reset."
  3519. "Aborting Crashdump\n");
  3520. goto hba_free;
  3521. }
  3522. } else {
  3523. value |= 0x00010000;
  3524. writel(value, (void *)real_offset);
  3525. num_hba++;
  3526. }
  3527. }
  3528. spin_lock_init(&phba->io_sgl_lock);
  3529. spin_lock_init(&phba->mgmt_sgl_lock);
  3530. spin_lock_init(&phba->isr_lock);
  3531. ret = mgmt_get_fw_config(&phba->ctrl, phba);
  3532. if (ret != 0) {
  3533. shost_printk(KERN_ERR, phba->shost,
  3534. "Error getting fw config\n");
  3535. goto free_port;
  3536. }
  3537. phba->shost->max_id = phba->fw_config.iscsi_cid_count;
  3538. beiscsi_get_params(phba);
  3539. phba->shost->can_queue = phba->params.ios_per_ctrl;
  3540. ret = beiscsi_init_port(phba);
  3541. if (ret < 0) {
  3542. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3543. "Failed in beiscsi_init_port\n");
  3544. goto free_port;
  3545. }
  3546. for (i = 0; i < MAX_MCC_CMD ; i++) {
  3547. init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
  3548. phba->ctrl.mcc_tag[i] = i + 1;
  3549. phba->ctrl.mcc_numtag[i + 1] = 0;
  3550. phba->ctrl.mcc_tag_available++;
  3551. }
  3552. phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
  3553. snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_q_irq%u",
  3554. phba->shost->host_no);
  3555. phba->wq = create_workqueue(phba->wq_name);
  3556. if (!phba->wq) {
  3557. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3558. "Failed to allocate work queue\n");
  3559. goto free_twq;
  3560. }
  3561. INIT_WORK(&phba->work_cqs, beiscsi_process_all_cqs);
  3562. phwi_ctrlr = phba->phwi_ctrlr;
  3563. phwi_context = phwi_ctrlr->phwi_ctxt;
  3564. if (blk_iopoll_enabled) {
  3565. for (i = 0; i < phba->num_cpus; i++) {
  3566. pbe_eq = &phwi_context->be_eq[i];
  3567. blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
  3568. be_iopoll);
  3569. blk_iopoll_enable(&pbe_eq->iopoll);
  3570. }
  3571. }
  3572. ret = beiscsi_init_irqs(phba);
  3573. if (ret < 0) {
  3574. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3575. "Failed to beiscsi_init_irqs\n");
  3576. goto free_blkenbld;
  3577. }
  3578. hwi_enable_intr(phba);
  3579. SE_DEBUG(DBG_LVL_8, "\n\n\n SUCCESS - DRIVER LOADED\n\n\n");
  3580. return 0;
  3581. free_blkenbld:
  3582. destroy_workqueue(phba->wq);
  3583. if (blk_iopoll_enabled)
  3584. for (i = 0; i < phba->num_cpus; i++) {
  3585. pbe_eq = &phwi_context->be_eq[i];
  3586. blk_iopoll_disable(&pbe_eq->iopoll);
  3587. }
  3588. free_twq:
  3589. beiscsi_clean_port(phba);
  3590. beiscsi_free_mem(phba);
  3591. free_port:
  3592. real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
  3593. value = readl((void *)real_offset);
  3594. if (value & 0x00010000) {
  3595. value &= 0xfffeffff;
  3596. writel(value, (void *)real_offset);
  3597. }
  3598. pci_free_consistent(phba->pcidev,
  3599. phba->ctrl.mbox_mem_alloced.size,
  3600. phba->ctrl.mbox_mem_alloced.va,
  3601. phba->ctrl.mbox_mem_alloced.dma);
  3602. beiscsi_unmap_pci_function(phba);
  3603. hba_free:
  3604. if (phba->msix_enabled)
  3605. pci_disable_msix(phba->pcidev);
  3606. iscsi_host_remove(phba->shost);
  3607. pci_dev_put(phba->pcidev);
  3608. iscsi_host_free(phba->shost);
  3609. disable_pci:
  3610. pci_disable_device(pcidev);
  3611. return ret;
  3612. }
  3613. struct iscsi_transport beiscsi_iscsi_transport = {
  3614. .owner = THIS_MODULE,
  3615. .name = DRV_NAME,
  3616. .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
  3617. CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
  3618. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  3619. ISCSI_MAX_XMIT_DLENGTH |
  3620. ISCSI_HDRDGST_EN |
  3621. ISCSI_DATADGST_EN |
  3622. ISCSI_INITIAL_R2T_EN |
  3623. ISCSI_MAX_R2T |
  3624. ISCSI_IMM_DATA_EN |
  3625. ISCSI_FIRST_BURST |
  3626. ISCSI_MAX_BURST |
  3627. ISCSI_PDU_INORDER_EN |
  3628. ISCSI_DATASEQ_INORDER_EN |
  3629. ISCSI_ERL |
  3630. ISCSI_CONN_PORT |
  3631. ISCSI_CONN_ADDRESS |
  3632. ISCSI_EXP_STATSN |
  3633. ISCSI_PERSISTENT_PORT |
  3634. ISCSI_PERSISTENT_ADDRESS |
  3635. ISCSI_TARGET_NAME | ISCSI_TPGT |
  3636. ISCSI_USERNAME | ISCSI_PASSWORD |
  3637. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  3638. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  3639. ISCSI_LU_RESET_TMO |
  3640. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  3641. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  3642. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  3643. ISCSI_HOST_INITIATOR_NAME,
  3644. .create_session = beiscsi_session_create,
  3645. .destroy_session = beiscsi_session_destroy,
  3646. .create_conn = beiscsi_conn_create,
  3647. .bind_conn = beiscsi_conn_bind,
  3648. .destroy_conn = iscsi_conn_teardown,
  3649. .set_param = beiscsi_set_param,
  3650. .get_conn_param = beiscsi_conn_get_param,
  3651. .get_session_param = iscsi_session_get_param,
  3652. .get_host_param = beiscsi_get_host_param,
  3653. .start_conn = beiscsi_conn_start,
  3654. .stop_conn = iscsi_conn_stop,
  3655. .send_pdu = iscsi_conn_send_pdu,
  3656. .xmit_task = beiscsi_task_xmit,
  3657. .cleanup_task = beiscsi_cleanup_task,
  3658. .alloc_pdu = beiscsi_alloc_pdu,
  3659. .parse_pdu_itt = beiscsi_parse_pdu,
  3660. .get_stats = beiscsi_conn_get_stats,
  3661. .ep_connect = beiscsi_ep_connect,
  3662. .ep_poll = beiscsi_ep_poll,
  3663. .ep_disconnect = beiscsi_ep_disconnect,
  3664. .session_recovery_timedout = iscsi_session_recovery_timedout,
  3665. };
  3666. static struct pci_driver beiscsi_pci_driver = {
  3667. .name = DRV_NAME,
  3668. .probe = beiscsi_dev_probe,
  3669. .remove = beiscsi_remove,
  3670. .id_table = beiscsi_pci_id_table
  3671. };
  3672. static int __init beiscsi_module_init(void)
  3673. {
  3674. int ret;
  3675. beiscsi_scsi_transport =
  3676. iscsi_register_transport(&beiscsi_iscsi_transport);
  3677. if (!beiscsi_scsi_transport) {
  3678. SE_DEBUG(DBG_LVL_1,
  3679. "beiscsi_module_init - Unable to register beiscsi"
  3680. "transport.\n");
  3681. return -ENOMEM;
  3682. }
  3683. SE_DEBUG(DBG_LVL_8, "In beiscsi_module_init, tt=%p\n",
  3684. &beiscsi_iscsi_transport);
  3685. ret = pci_register_driver(&beiscsi_pci_driver);
  3686. if (ret) {
  3687. SE_DEBUG(DBG_LVL_1,
  3688. "beiscsi_module_init - Unable to register"
  3689. "beiscsi pci driver.\n");
  3690. goto unregister_iscsi_transport;
  3691. }
  3692. return 0;
  3693. unregister_iscsi_transport:
  3694. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  3695. return ret;
  3696. }
  3697. static void __exit beiscsi_module_exit(void)
  3698. {
  3699. pci_unregister_driver(&beiscsi_pci_driver);
  3700. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  3701. }
  3702. module_init(beiscsi_module_init);
  3703. module_exit(beiscsi_module_exit);