be_main.c 118 KB

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