ql4_os.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/slab.h>
  9. #include <scsi/scsi_tcq.h>
  10. #include <scsi/scsicam.h>
  11. #include "ql4_def.h"
  12. #include "ql4_version.h"
  13. #include "ql4_glbl.h"
  14. #include "ql4_dbg.h"
  15. #include "ql4_inline.h"
  16. /*
  17. * Driver version
  18. */
  19. static char qla4xxx_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static struct kmem_cache *srb_cachep;
  24. /*
  25. * Module parameter information and variables
  26. */
  27. int ql4xdontresethba = 0;
  28. module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(ql4xdontresethba,
  30. "Don't reset the HBA for driver recovery \n"
  31. " 0 - It will reset HBA (Default)\n"
  32. " 1 - It will NOT reset HBA");
  33. int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
  34. module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
  35. MODULE_PARM_DESC(ql4xextended_error_logging,
  36. "Option to enable extended error logging, "
  37. "Default is 0 - no logging, 1 - debug logging");
  38. int ql4xenablemsix = 1;
  39. module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  40. MODULE_PARM_DESC(ql4xenablemsix,
  41. "Set to enable MSI or MSI-X interrupt mechanism.\n"
  42. " 0 = enable INTx interrupt mechanism.\n"
  43. " 1 = enable MSI-X interrupt mechanism (Default).\n"
  44. " 2 = enable MSI interrupt mechanism.");
  45. #define QL4_DEF_QDEPTH 32
  46. static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
  47. module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(ql4xmaxqdepth,
  49. "Maximum queue depth to report for target devices.\n"
  50. " Default: 32.");
  51. static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
  52. module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
  53. MODULE_PARM_DESC(ql4xsess_recovery_tmo,
  54. "Target Session Recovery Timeout.\n"
  55. " Default: 30 sec.");
  56. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
  57. /*
  58. * SCSI host template entry points
  59. */
  60. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
  61. /*
  62. * iSCSI template entry points
  63. */
  64. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  65. enum iscsi_param param, char *buf);
  66. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  67. enum iscsi_host_param param, char *buf);
  68. static int qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data,
  69. int count);
  70. static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  71. enum iscsi_param_type param_type,
  72. int param, char *buf);
  73. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
  74. static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
  75. struct sockaddr *dst_addr,
  76. int non_blocking);
  77. static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
  78. static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
  79. static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
  80. enum iscsi_param param, char *buf);
  81. static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
  82. static struct iscsi_cls_conn *
  83. qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
  84. static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
  85. struct iscsi_cls_conn *cls_conn,
  86. uint64_t transport_fd, int is_leading);
  87. static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
  88. static struct iscsi_cls_session *
  89. qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  90. uint16_t qdepth, uint32_t initial_cmdsn);
  91. static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
  92. static void qla4xxx_task_work(struct work_struct *wdata);
  93. static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
  94. static int qla4xxx_task_xmit(struct iscsi_task *);
  95. static void qla4xxx_task_cleanup(struct iscsi_task *);
  96. static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
  97. static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  98. struct iscsi_stats *stats);
  99. /*
  100. * SCSI host template entry points
  101. */
  102. static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  103. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
  104. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  105. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
  106. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  107. static int qla4xxx_slave_alloc(struct scsi_device *device);
  108. static int qla4xxx_slave_configure(struct scsi_device *device);
  109. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  110. static mode_t ql4_attr_is_visible(int param_type, int param);
  111. static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
  112. QLA82XX_LEGACY_INTR_CONFIG;
  113. static struct scsi_host_template qla4xxx_driver_template = {
  114. .module = THIS_MODULE,
  115. .name = DRIVER_NAME,
  116. .proc_name = DRIVER_NAME,
  117. .queuecommand = qla4xxx_queuecommand,
  118. .eh_abort_handler = qla4xxx_eh_abort,
  119. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  120. .eh_target_reset_handler = qla4xxx_eh_target_reset,
  121. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  122. .eh_timed_out = qla4xxx_eh_cmd_timed_out,
  123. .slave_configure = qla4xxx_slave_configure,
  124. .slave_alloc = qla4xxx_slave_alloc,
  125. .slave_destroy = qla4xxx_slave_destroy,
  126. .this_id = -1,
  127. .cmd_per_lun = 3,
  128. .use_clustering = ENABLE_CLUSTERING,
  129. .sg_tablesize = SG_ALL,
  130. .max_sectors = 0xFFFF,
  131. .shost_attrs = qla4xxx_host_attrs,
  132. .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
  133. };
  134. static struct iscsi_transport qla4xxx_iscsi_transport = {
  135. .owner = THIS_MODULE,
  136. .name = DRIVER_NAME,
  137. .caps = CAP_TEXT_NEGO |
  138. CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
  139. CAP_DATADGST | CAP_LOGIN_OFFLOAD |
  140. CAP_MULTI_R2T,
  141. .attr_is_visible = ql4_attr_is_visible,
  142. .create_session = qla4xxx_session_create,
  143. .destroy_session = qla4xxx_session_destroy,
  144. .start_conn = qla4xxx_conn_start,
  145. .create_conn = qla4xxx_conn_create,
  146. .bind_conn = qla4xxx_conn_bind,
  147. .stop_conn = iscsi_conn_stop,
  148. .destroy_conn = qla4xxx_conn_destroy,
  149. .set_param = iscsi_set_param,
  150. .get_conn_param = qla4xxx_conn_get_param,
  151. .get_session_param = iscsi_session_get_param,
  152. .get_ep_param = qla4xxx_get_ep_param,
  153. .ep_connect = qla4xxx_ep_connect,
  154. .ep_poll = qla4xxx_ep_poll,
  155. .ep_disconnect = qla4xxx_ep_disconnect,
  156. .get_stats = qla4xxx_conn_get_stats,
  157. .send_pdu = iscsi_conn_send_pdu,
  158. .xmit_task = qla4xxx_task_xmit,
  159. .cleanup_task = qla4xxx_task_cleanup,
  160. .alloc_pdu = qla4xxx_alloc_pdu,
  161. .get_host_param = qla4xxx_host_get_param,
  162. .set_iface_param = qla4xxx_iface_set_param,
  163. .get_iface_param = qla4xxx_get_iface_param,
  164. .bsg_request = qla4xxx_bsg_request,
  165. };
  166. static struct scsi_transport_template *qla4xxx_scsi_transport;
  167. static mode_t ql4_attr_is_visible(int param_type, int param)
  168. {
  169. switch (param_type) {
  170. case ISCSI_HOST_PARAM:
  171. switch (param) {
  172. case ISCSI_HOST_PARAM_HWADDRESS:
  173. case ISCSI_HOST_PARAM_IPADDRESS:
  174. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  175. return S_IRUGO;
  176. default:
  177. return 0;
  178. }
  179. case ISCSI_PARAM:
  180. switch (param) {
  181. case ISCSI_PARAM_CONN_ADDRESS:
  182. case ISCSI_PARAM_CONN_PORT:
  183. case ISCSI_PARAM_TARGET_NAME:
  184. case ISCSI_PARAM_TPGT:
  185. case ISCSI_PARAM_TARGET_ALIAS:
  186. case ISCSI_PARAM_MAX_BURST:
  187. case ISCSI_PARAM_MAX_R2T:
  188. case ISCSI_PARAM_FIRST_BURST:
  189. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  190. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  191. return S_IRUGO;
  192. default:
  193. return 0;
  194. }
  195. case ISCSI_NET_PARAM:
  196. switch (param) {
  197. case ISCSI_NET_PARAM_IPV4_ADDR:
  198. case ISCSI_NET_PARAM_IPV4_SUBNET:
  199. case ISCSI_NET_PARAM_IPV4_GW:
  200. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  201. case ISCSI_NET_PARAM_IFACE_ENABLE:
  202. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  203. case ISCSI_NET_PARAM_IPV6_ADDR:
  204. case ISCSI_NET_PARAM_IPV6_ROUTER:
  205. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  206. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  207. case ISCSI_NET_PARAM_VLAN_ID:
  208. case ISCSI_NET_PARAM_VLAN_PRIORITY:
  209. case ISCSI_NET_PARAM_VLAN_ENABLED:
  210. return S_IRUGO;
  211. default:
  212. return 0;
  213. }
  214. }
  215. return 0;
  216. }
  217. static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  218. enum iscsi_param_type param_type,
  219. int param, char *buf)
  220. {
  221. struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
  222. struct scsi_qla_host *ha = to_qla_host(shost);
  223. int len = -ENOSYS;
  224. if (param_type != ISCSI_NET_PARAM)
  225. return -ENOSYS;
  226. switch (param) {
  227. case ISCSI_NET_PARAM_IPV4_ADDR:
  228. len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
  229. break;
  230. case ISCSI_NET_PARAM_IPV4_SUBNET:
  231. len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
  232. break;
  233. case ISCSI_NET_PARAM_IPV4_GW:
  234. len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
  235. break;
  236. case ISCSI_NET_PARAM_IFACE_ENABLE:
  237. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  238. len = sprintf(buf, "%s\n",
  239. (ha->ip_config.ipv4_options &
  240. IPOPT_IPV4_PROTOCOL_ENABLE) ?
  241. "enabled" : "disabled");
  242. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  243. len = sprintf(buf, "%s\n",
  244. (ha->ip_config.ipv6_options &
  245. IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
  246. "enabled" : "disabled");
  247. break;
  248. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  249. len = sprintf(buf, "%s\n",
  250. (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
  251. "dhcp" : "static");
  252. break;
  253. case ISCSI_NET_PARAM_IPV6_ADDR:
  254. if (iface->iface_num == 0)
  255. len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
  256. if (iface->iface_num == 1)
  257. len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
  258. break;
  259. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  260. len = sprintf(buf, "%pI6\n",
  261. &ha->ip_config.ipv6_link_local_addr);
  262. break;
  263. case ISCSI_NET_PARAM_IPV6_ROUTER:
  264. len = sprintf(buf, "%pI6\n",
  265. &ha->ip_config.ipv6_default_router_addr);
  266. break;
  267. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  268. len = sprintf(buf, "%s\n",
  269. (ha->ip_config.ipv6_addl_options &
  270. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
  271. "nd" : "static");
  272. break;
  273. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  274. len = sprintf(buf, "%s\n",
  275. (ha->ip_config.ipv6_addl_options &
  276. IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
  277. "auto" : "static");
  278. break;
  279. case ISCSI_NET_PARAM_VLAN_ID:
  280. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  281. len = sprintf(buf, "%d\n",
  282. (ha->ip_config.ipv4_vlan_tag &
  283. ISCSI_MAX_VLAN_ID));
  284. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  285. len = sprintf(buf, "%d\n",
  286. (ha->ip_config.ipv6_vlan_tag &
  287. ISCSI_MAX_VLAN_ID));
  288. break;
  289. case ISCSI_NET_PARAM_VLAN_PRIORITY:
  290. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  291. len = sprintf(buf, "%d\n",
  292. ((ha->ip_config.ipv4_vlan_tag >> 13) &
  293. ISCSI_MAX_VLAN_PRIORITY));
  294. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  295. len = sprintf(buf, "%d\n",
  296. ((ha->ip_config.ipv6_vlan_tag >> 13) &
  297. ISCSI_MAX_VLAN_PRIORITY));
  298. break;
  299. case ISCSI_NET_PARAM_VLAN_ENABLED:
  300. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  301. len = sprintf(buf, "%s\n",
  302. (ha->ip_config.ipv4_options &
  303. IPOPT_VLAN_TAGGING_ENABLE) ?
  304. "enabled" : "disabled");
  305. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  306. len = sprintf(buf, "%s\n",
  307. (ha->ip_config.ipv6_options &
  308. IPV6_OPT_VLAN_TAGGING_ENABLE) ?
  309. "enabled" : "disabled");
  310. break;
  311. default:
  312. len = -ENOSYS;
  313. }
  314. return len;
  315. }
  316. static struct iscsi_endpoint *
  317. qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  318. int non_blocking)
  319. {
  320. int ret;
  321. struct iscsi_endpoint *ep;
  322. struct qla_endpoint *qla_ep;
  323. struct scsi_qla_host *ha;
  324. struct sockaddr_in *addr;
  325. struct sockaddr_in6 *addr6;
  326. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  327. if (!shost) {
  328. ret = -ENXIO;
  329. printk(KERN_ERR "%s: shost is NULL\n",
  330. __func__);
  331. return ERR_PTR(ret);
  332. }
  333. ha = iscsi_host_priv(shost);
  334. ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
  335. if (!ep) {
  336. ret = -ENOMEM;
  337. return ERR_PTR(ret);
  338. }
  339. qla_ep = ep->dd_data;
  340. memset(qla_ep, 0, sizeof(struct qla_endpoint));
  341. if (dst_addr->sa_family == AF_INET) {
  342. memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
  343. addr = (struct sockaddr_in *)&qla_ep->dst_addr;
  344. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
  345. (char *)&addr->sin_addr));
  346. } else if (dst_addr->sa_family == AF_INET6) {
  347. memcpy(&qla_ep->dst_addr, dst_addr,
  348. sizeof(struct sockaddr_in6));
  349. addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
  350. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
  351. (char *)&addr6->sin6_addr));
  352. }
  353. qla_ep->host = shost;
  354. return ep;
  355. }
  356. static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  357. {
  358. struct qla_endpoint *qla_ep;
  359. struct scsi_qla_host *ha;
  360. int ret = 0;
  361. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  362. qla_ep = ep->dd_data;
  363. ha = to_qla_host(qla_ep->host);
  364. if (adapter_up(ha))
  365. ret = 1;
  366. return ret;
  367. }
  368. static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
  369. {
  370. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  371. iscsi_destroy_endpoint(ep);
  372. }
  373. static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
  374. enum iscsi_param param,
  375. char *buf)
  376. {
  377. struct qla_endpoint *qla_ep = ep->dd_data;
  378. struct sockaddr *dst_addr;
  379. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  380. switch (param) {
  381. case ISCSI_PARAM_CONN_PORT:
  382. case ISCSI_PARAM_CONN_ADDRESS:
  383. if (!qla_ep)
  384. return -ENOTCONN;
  385. dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
  386. if (!dst_addr)
  387. return -ENOTCONN;
  388. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  389. &qla_ep->dst_addr, param, buf);
  390. default:
  391. return -ENOSYS;
  392. }
  393. }
  394. static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  395. struct iscsi_stats *stats)
  396. {
  397. struct iscsi_session *sess;
  398. struct iscsi_cls_session *cls_sess;
  399. struct ddb_entry *ddb_entry;
  400. struct scsi_qla_host *ha;
  401. struct ql_iscsi_stats *ql_iscsi_stats;
  402. int stats_size;
  403. int ret;
  404. dma_addr_t iscsi_stats_dma;
  405. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  406. cls_sess = iscsi_conn_to_session(cls_conn);
  407. sess = cls_sess->dd_data;
  408. ddb_entry = sess->dd_data;
  409. ha = ddb_entry->ha;
  410. stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
  411. /* Allocate memory */
  412. ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
  413. &iscsi_stats_dma, GFP_KERNEL);
  414. if (!ql_iscsi_stats) {
  415. ql4_printk(KERN_ERR, ha,
  416. "Unable to allocate memory for iscsi stats\n");
  417. goto exit_get_stats;
  418. }
  419. ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
  420. iscsi_stats_dma);
  421. if (ret != QLA_SUCCESS) {
  422. ql4_printk(KERN_ERR, ha,
  423. "Unable to retreive iscsi stats\n");
  424. goto free_stats;
  425. }
  426. /* octets */
  427. stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
  428. stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
  429. /* xmit pdus */
  430. stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
  431. stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
  432. stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
  433. stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
  434. stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
  435. stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
  436. stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
  437. stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
  438. /* recv pdus */
  439. stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
  440. stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
  441. stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
  442. stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
  443. stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
  444. stats->logoutrsp_pdus =
  445. le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
  446. stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
  447. stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
  448. stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
  449. free_stats:
  450. dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
  451. iscsi_stats_dma);
  452. exit_get_stats:
  453. return;
  454. }
  455. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  456. {
  457. struct iscsi_cls_session *session;
  458. struct iscsi_session *sess;
  459. unsigned long flags;
  460. enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
  461. session = starget_to_session(scsi_target(sc->device));
  462. sess = session->dd_data;
  463. spin_lock_irqsave(&session->lock, flags);
  464. if (session->state == ISCSI_SESSION_FAILED)
  465. ret = BLK_EH_RESET_TIMER;
  466. spin_unlock_irqrestore(&session->lock, flags);
  467. return ret;
  468. }
  469. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  470. enum iscsi_host_param param, char *buf)
  471. {
  472. struct scsi_qla_host *ha = to_qla_host(shost);
  473. int len;
  474. switch (param) {
  475. case ISCSI_HOST_PARAM_HWADDRESS:
  476. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  477. break;
  478. case ISCSI_HOST_PARAM_IPADDRESS:
  479. len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
  480. break;
  481. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  482. len = sprintf(buf, "%s\n", ha->name_string);
  483. break;
  484. default:
  485. return -ENOSYS;
  486. }
  487. return len;
  488. }
  489. static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
  490. {
  491. if (ha->iface_ipv4)
  492. return;
  493. /* IPv4 */
  494. ha->iface_ipv4 = iscsi_create_iface(ha->host,
  495. &qla4xxx_iscsi_transport,
  496. ISCSI_IFACE_TYPE_IPV4, 0, 0);
  497. if (!ha->iface_ipv4)
  498. ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
  499. "iface0.\n");
  500. }
  501. static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
  502. {
  503. if (!ha->iface_ipv6_0)
  504. /* IPv6 iface-0 */
  505. ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
  506. &qla4xxx_iscsi_transport,
  507. ISCSI_IFACE_TYPE_IPV6, 0,
  508. 0);
  509. if (!ha->iface_ipv6_0)
  510. ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  511. "iface0.\n");
  512. if (!ha->iface_ipv6_1)
  513. /* IPv6 iface-1 */
  514. ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
  515. &qla4xxx_iscsi_transport,
  516. ISCSI_IFACE_TYPE_IPV6, 1,
  517. 0);
  518. if (!ha->iface_ipv6_1)
  519. ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  520. "iface1.\n");
  521. }
  522. static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
  523. {
  524. if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
  525. qla4xxx_create_ipv4_iface(ha);
  526. if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
  527. qla4xxx_create_ipv6_iface(ha);
  528. }
  529. static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
  530. {
  531. if (ha->iface_ipv4) {
  532. iscsi_destroy_iface(ha->iface_ipv4);
  533. ha->iface_ipv4 = NULL;
  534. }
  535. }
  536. static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
  537. {
  538. if (ha->iface_ipv6_0) {
  539. iscsi_destroy_iface(ha->iface_ipv6_0);
  540. ha->iface_ipv6_0 = NULL;
  541. }
  542. if (ha->iface_ipv6_1) {
  543. iscsi_destroy_iface(ha->iface_ipv6_1);
  544. ha->iface_ipv6_1 = NULL;
  545. }
  546. }
  547. static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
  548. {
  549. qla4xxx_destroy_ipv4_iface(ha);
  550. qla4xxx_destroy_ipv6_iface(ha);
  551. }
  552. static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
  553. struct iscsi_iface_param_info *iface_param,
  554. struct addr_ctrl_blk *init_fw_cb)
  555. {
  556. /*
  557. * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
  558. * iface_num 1 is valid only for IPv6 Addr.
  559. */
  560. switch (iface_param->param) {
  561. case ISCSI_NET_PARAM_IPV6_ADDR:
  562. if (iface_param->iface_num & 0x1)
  563. /* IPv6 Addr 1 */
  564. memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
  565. sizeof(init_fw_cb->ipv6_addr1));
  566. else
  567. /* IPv6 Addr 0 */
  568. memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
  569. sizeof(init_fw_cb->ipv6_addr0));
  570. break;
  571. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  572. if (iface_param->iface_num & 0x1)
  573. break;
  574. memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
  575. sizeof(init_fw_cb->ipv6_if_id));
  576. break;
  577. case ISCSI_NET_PARAM_IPV6_ROUTER:
  578. if (iface_param->iface_num & 0x1)
  579. break;
  580. memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
  581. sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  582. break;
  583. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  584. /* Autocfg applies to even interface */
  585. if (iface_param->iface_num & 0x1)
  586. break;
  587. if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
  588. init_fw_cb->ipv6_addtl_opts &=
  589. cpu_to_le16(
  590. ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  591. else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
  592. init_fw_cb->ipv6_addtl_opts |=
  593. cpu_to_le16(
  594. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  595. else
  596. ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  597. "IPv6 addr\n");
  598. break;
  599. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  600. /* Autocfg applies to even interface */
  601. if (iface_param->iface_num & 0x1)
  602. break;
  603. if (iface_param->value[0] ==
  604. ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
  605. init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
  606. IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  607. else if (iface_param->value[0] ==
  608. ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
  609. init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
  610. ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  611. else
  612. ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  613. "IPv6 linklocal addr\n");
  614. break;
  615. case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
  616. /* Autocfg applies to even interface */
  617. if (iface_param->iface_num & 0x1)
  618. break;
  619. if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
  620. memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
  621. sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  622. break;
  623. case ISCSI_NET_PARAM_IFACE_ENABLE:
  624. if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
  625. init_fw_cb->ipv6_opts |=
  626. cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
  627. qla4xxx_create_ipv6_iface(ha);
  628. } else {
  629. init_fw_cb->ipv6_opts &=
  630. cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
  631. 0xFFFF);
  632. qla4xxx_destroy_ipv6_iface(ha);
  633. }
  634. break;
  635. case ISCSI_NET_PARAM_VLAN_ID:
  636. if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
  637. break;
  638. init_fw_cb->ipv6_vlan_tag =
  639. cpu_to_be16(*(uint16_t *)iface_param->value);
  640. break;
  641. case ISCSI_NET_PARAM_VLAN_ENABLED:
  642. if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
  643. init_fw_cb->ipv6_opts |=
  644. cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
  645. else
  646. init_fw_cb->ipv6_opts &=
  647. cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
  648. break;
  649. default:
  650. ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
  651. iface_param->param);
  652. break;
  653. }
  654. }
  655. static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
  656. struct iscsi_iface_param_info *iface_param,
  657. struct addr_ctrl_blk *init_fw_cb)
  658. {
  659. switch (iface_param->param) {
  660. case ISCSI_NET_PARAM_IPV4_ADDR:
  661. memcpy(init_fw_cb->ipv4_addr, iface_param->value,
  662. sizeof(init_fw_cb->ipv4_addr));
  663. break;
  664. case ISCSI_NET_PARAM_IPV4_SUBNET:
  665. memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
  666. sizeof(init_fw_cb->ipv4_subnet));
  667. break;
  668. case ISCSI_NET_PARAM_IPV4_GW:
  669. memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
  670. sizeof(init_fw_cb->ipv4_gw_addr));
  671. break;
  672. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  673. if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
  674. init_fw_cb->ipv4_tcp_opts |=
  675. cpu_to_le16(TCPOPT_DHCP_ENABLE);
  676. else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
  677. init_fw_cb->ipv4_tcp_opts &=
  678. cpu_to_le16(~TCPOPT_DHCP_ENABLE);
  679. else
  680. ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
  681. break;
  682. case ISCSI_NET_PARAM_IFACE_ENABLE:
  683. if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
  684. init_fw_cb->ipv4_ip_opts |=
  685. cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
  686. qla4xxx_create_ipv4_iface(ha);
  687. } else {
  688. init_fw_cb->ipv4_ip_opts &=
  689. cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
  690. 0xFFFF);
  691. qla4xxx_destroy_ipv4_iface(ha);
  692. }
  693. break;
  694. case ISCSI_NET_PARAM_VLAN_ID:
  695. if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
  696. break;
  697. init_fw_cb->ipv4_vlan_tag =
  698. cpu_to_be16(*(uint16_t *)iface_param->value);
  699. break;
  700. case ISCSI_NET_PARAM_VLAN_ENABLED:
  701. if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
  702. init_fw_cb->ipv4_ip_opts |=
  703. cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
  704. else
  705. init_fw_cb->ipv4_ip_opts &=
  706. cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
  707. break;
  708. default:
  709. ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
  710. iface_param->param);
  711. break;
  712. }
  713. }
  714. static void
  715. qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
  716. {
  717. struct addr_ctrl_blk_def *acb;
  718. acb = (struct addr_ctrl_blk_def *)init_fw_cb;
  719. memset(acb->reserved1, 0, sizeof(acb->reserved1));
  720. memset(acb->reserved2, 0, sizeof(acb->reserved2));
  721. memset(acb->reserved3, 0, sizeof(acb->reserved3));
  722. memset(acb->reserved4, 0, sizeof(acb->reserved4));
  723. memset(acb->reserved5, 0, sizeof(acb->reserved5));
  724. memset(acb->reserved6, 0, sizeof(acb->reserved6));
  725. memset(acb->reserved7, 0, sizeof(acb->reserved7));
  726. memset(acb->reserved8, 0, sizeof(acb->reserved8));
  727. memset(acb->reserved9, 0, sizeof(acb->reserved9));
  728. memset(acb->reserved10, 0, sizeof(acb->reserved10));
  729. memset(acb->reserved11, 0, sizeof(acb->reserved11));
  730. memset(acb->reserved12, 0, sizeof(acb->reserved12));
  731. memset(acb->reserved13, 0, sizeof(acb->reserved13));
  732. memset(acb->reserved14, 0, sizeof(acb->reserved14));
  733. memset(acb->reserved15, 0, sizeof(acb->reserved15));
  734. }
  735. static int
  736. qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
  737. {
  738. struct scsi_qla_host *ha = to_qla_host(shost);
  739. int rval = 0;
  740. struct iscsi_iface_param_info *iface_param = NULL;
  741. struct addr_ctrl_blk *init_fw_cb = NULL;
  742. dma_addr_t init_fw_cb_dma;
  743. uint32_t mbox_cmd[MBOX_REG_COUNT];
  744. uint32_t mbox_sts[MBOX_REG_COUNT];
  745. uint32_t total_param_count;
  746. uint32_t length;
  747. init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
  748. sizeof(struct addr_ctrl_blk),
  749. &init_fw_cb_dma, GFP_KERNEL);
  750. if (!init_fw_cb) {
  751. ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
  752. __func__);
  753. return -ENOMEM;
  754. }
  755. memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  756. memset(&mbox_cmd, 0, sizeof(mbox_cmd));
  757. memset(&mbox_sts, 0, sizeof(mbox_sts));
  758. if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
  759. ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
  760. rval = -EIO;
  761. goto exit_init_fw_cb;
  762. }
  763. total_param_count = count;
  764. iface_param = (struct iscsi_iface_param_info *)data;
  765. for ( ; total_param_count != 0; total_param_count--) {
  766. length = iface_param->len;
  767. if (iface_param->param_type != ISCSI_NET_PARAM)
  768. continue;
  769. switch (iface_param->iface_type) {
  770. case ISCSI_IFACE_TYPE_IPV4:
  771. switch (iface_param->iface_num) {
  772. case 0:
  773. qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
  774. break;
  775. default:
  776. /* Cannot have more than one IPv4 interface */
  777. ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
  778. "number = %d\n",
  779. iface_param->iface_num);
  780. break;
  781. }
  782. break;
  783. case ISCSI_IFACE_TYPE_IPV6:
  784. switch (iface_param->iface_num) {
  785. case 0:
  786. case 1:
  787. qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
  788. break;
  789. default:
  790. /* Cannot have more than two IPv6 interface */
  791. ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
  792. "number = %d\n",
  793. iface_param->iface_num);
  794. break;
  795. }
  796. break;
  797. default:
  798. ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
  799. break;
  800. }
  801. iface_param = (struct iscsi_iface_param_info *)
  802. ((uint8_t *)iface_param +
  803. sizeof(struct iscsi_iface_param_info) + length);
  804. }
  805. init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
  806. rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
  807. sizeof(struct addr_ctrl_blk),
  808. FLASH_OPT_RMW_COMMIT);
  809. if (rval != QLA_SUCCESS) {
  810. ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
  811. __func__);
  812. rval = -EIO;
  813. goto exit_init_fw_cb;
  814. }
  815. qla4xxx_disable_acb(ha);
  816. qla4xxx_initcb_to_acb(init_fw_cb);
  817. rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
  818. if (rval != QLA_SUCCESS) {
  819. ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
  820. __func__);
  821. rval = -EIO;
  822. goto exit_init_fw_cb;
  823. }
  824. memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  825. qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
  826. init_fw_cb_dma);
  827. exit_init_fw_cb:
  828. dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
  829. init_fw_cb, init_fw_cb_dma);
  830. return rval;
  831. }
  832. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
  833. enum iscsi_param param, char *buf)
  834. {
  835. struct iscsi_conn *conn;
  836. struct qla_conn *qla_conn;
  837. struct sockaddr *dst_addr;
  838. int len = 0;
  839. conn = cls_conn->dd_data;
  840. qla_conn = conn->dd_data;
  841. dst_addr = &qla_conn->qla_ep->dst_addr;
  842. switch (param) {
  843. case ISCSI_PARAM_CONN_PORT:
  844. case ISCSI_PARAM_CONN_ADDRESS:
  845. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  846. dst_addr, param, buf);
  847. default:
  848. return iscsi_conn_get_param(cls_conn, param, buf);
  849. }
  850. return len;
  851. }
  852. static struct iscsi_cls_session *
  853. qla4xxx_session_create(struct iscsi_endpoint *ep,
  854. uint16_t cmds_max, uint16_t qdepth,
  855. uint32_t initial_cmdsn)
  856. {
  857. struct iscsi_cls_session *cls_sess;
  858. struct scsi_qla_host *ha;
  859. struct qla_endpoint *qla_ep;
  860. struct ddb_entry *ddb_entry;
  861. uint32_t ddb_index;
  862. uint32_t mbx_sts = 0;
  863. struct iscsi_session *sess;
  864. struct sockaddr *dst_addr;
  865. int ret;
  866. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  867. if (!ep) {
  868. printk(KERN_ERR "qla4xxx: missing ep.\n");
  869. return NULL;
  870. }
  871. qla_ep = ep->dd_data;
  872. dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
  873. ha = to_qla_host(qla_ep->host);
  874. get_ddb_index:
  875. ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
  876. if (ddb_index >= MAX_DDB_ENTRIES) {
  877. DEBUG2(ql4_printk(KERN_INFO, ha,
  878. "Free DDB index not available\n"));
  879. return NULL;
  880. }
  881. if (test_and_set_bit(ddb_index, ha->ddb_idx_map))
  882. goto get_ddb_index;
  883. DEBUG2(ql4_printk(KERN_INFO, ha,
  884. "Found a free DDB index at %d\n", ddb_index));
  885. ret = qla4xxx_req_ddb_entry(ha, ddb_index, &mbx_sts);
  886. if (ret == QLA_ERROR) {
  887. if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
  888. ql4_printk(KERN_INFO, ha,
  889. "DDB index = %d not available trying next\n",
  890. ddb_index);
  891. goto get_ddb_index;
  892. }
  893. DEBUG2(ql4_printk(KERN_INFO, ha,
  894. "Free FW DDB not available\n"));
  895. return NULL;
  896. }
  897. cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
  898. cmds_max, sizeof(struct ddb_entry),
  899. sizeof(struct ql4_task_data),
  900. initial_cmdsn, ddb_index);
  901. if (!cls_sess)
  902. return NULL;
  903. sess = cls_sess->dd_data;
  904. ddb_entry = sess->dd_data;
  905. ddb_entry->fw_ddb_index = ddb_index;
  906. ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  907. ddb_entry->ha = ha;
  908. ddb_entry->sess = cls_sess;
  909. cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  910. ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
  911. ha->tot_ddbs++;
  912. return cls_sess;
  913. }
  914. static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
  915. {
  916. struct iscsi_session *sess;
  917. struct ddb_entry *ddb_entry;
  918. struct scsi_qla_host *ha;
  919. unsigned long flags;
  920. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  921. sess = cls_sess->dd_data;
  922. ddb_entry = sess->dd_data;
  923. ha = ddb_entry->ha;
  924. spin_lock_irqsave(&ha->hardware_lock, flags);
  925. qla4xxx_free_ddb(ha, ddb_entry);
  926. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  927. iscsi_session_teardown(cls_sess);
  928. }
  929. static struct iscsi_cls_conn *
  930. qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
  931. {
  932. struct iscsi_cls_conn *cls_conn;
  933. struct iscsi_session *sess;
  934. struct ddb_entry *ddb_entry;
  935. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  936. cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
  937. conn_idx);
  938. sess = cls_sess->dd_data;
  939. ddb_entry = sess->dd_data;
  940. ddb_entry->conn = cls_conn;
  941. return cls_conn;
  942. }
  943. static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
  944. struct iscsi_cls_conn *cls_conn,
  945. uint64_t transport_fd, int is_leading)
  946. {
  947. struct iscsi_conn *conn;
  948. struct qla_conn *qla_conn;
  949. struct iscsi_endpoint *ep;
  950. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  951. if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
  952. return -EINVAL;
  953. ep = iscsi_lookup_endpoint(transport_fd);
  954. conn = cls_conn->dd_data;
  955. qla_conn = conn->dd_data;
  956. qla_conn->qla_ep = ep->dd_data;
  957. return 0;
  958. }
  959. static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
  960. {
  961. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  962. struct iscsi_session *sess;
  963. struct ddb_entry *ddb_entry;
  964. struct scsi_qla_host *ha;
  965. struct dev_db_entry *fw_ddb_entry;
  966. dma_addr_t fw_ddb_entry_dma;
  967. uint32_t mbx_sts = 0;
  968. int ret = 0;
  969. int status = QLA_SUCCESS;
  970. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  971. sess = cls_sess->dd_data;
  972. ddb_entry = sess->dd_data;
  973. ha = ddb_entry->ha;
  974. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  975. &fw_ddb_entry_dma, GFP_KERNEL);
  976. if (!fw_ddb_entry) {
  977. ql4_printk(KERN_ERR, ha,
  978. "%s: Unable to allocate dma buffer\n", __func__);
  979. return -ENOMEM;
  980. }
  981. ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
  982. if (ret) {
  983. /* If iscsid is stopped and started then no need to do
  984. * set param again since ddb state will be already
  985. * active and FW does not allow set ddb to an
  986. * active session.
  987. */
  988. if (mbx_sts)
  989. if (ddb_entry->fw_ddb_device_state ==
  990. DDB_DS_SESSION_ACTIVE)
  991. goto exit_set_param;
  992. ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
  993. __func__, ddb_entry->fw_ddb_index);
  994. goto exit_conn_start;
  995. }
  996. status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
  997. if (status == QLA_ERROR) {
  998. ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
  999. sess->targetname);
  1000. ret = -EINVAL;
  1001. goto exit_conn_start;
  1002. }
  1003. ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
  1004. exit_set_param:
  1005. iscsi_conn_start(cls_conn);
  1006. ret = 0;
  1007. exit_conn_start:
  1008. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1009. fw_ddb_entry, fw_ddb_entry_dma);
  1010. return ret;
  1011. }
  1012. static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1013. {
  1014. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  1015. struct iscsi_session *sess;
  1016. struct scsi_qla_host *ha;
  1017. struct ddb_entry *ddb_entry;
  1018. int options;
  1019. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1020. sess = cls_sess->dd_data;
  1021. ddb_entry = sess->dd_data;
  1022. ha = ddb_entry->ha;
  1023. options = LOGOUT_OPTION_CLOSE_SESSION;
  1024. if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
  1025. ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
  1026. else
  1027. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  1028. /*
  1029. * Clear the DDB bit so that next login can use the bit
  1030. * if FW is not clearing the DDB entry then set DDB will fail anyways
  1031. */
  1032. clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
  1033. }
  1034. static void qla4xxx_task_work(struct work_struct *wdata)
  1035. {
  1036. struct ql4_task_data *task_data;
  1037. struct scsi_qla_host *ha;
  1038. struct passthru_status *sts;
  1039. struct iscsi_task *task;
  1040. struct iscsi_hdr *hdr;
  1041. uint8_t *data;
  1042. uint32_t data_len;
  1043. struct iscsi_conn *conn;
  1044. int hdr_len;
  1045. itt_t itt;
  1046. task_data = container_of(wdata, struct ql4_task_data, task_work);
  1047. ha = task_data->ha;
  1048. task = task_data->task;
  1049. sts = &task_data->sts;
  1050. hdr_len = sizeof(struct iscsi_hdr);
  1051. DEBUG3(printk(KERN_INFO "Status returned\n"));
  1052. DEBUG3(qla4xxx_dump_buffer(sts, 64));
  1053. DEBUG3(printk(KERN_INFO "Response buffer"));
  1054. DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
  1055. conn = task->conn;
  1056. switch (sts->completionStatus) {
  1057. case PASSTHRU_STATUS_COMPLETE:
  1058. hdr = (struct iscsi_hdr *)task_data->resp_buffer;
  1059. /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
  1060. itt = sts->handle;
  1061. hdr->itt = itt;
  1062. data = task_data->resp_buffer + hdr_len;
  1063. data_len = task_data->resp_len - hdr_len;
  1064. iscsi_complete_pdu(conn, hdr, data, data_len);
  1065. break;
  1066. default:
  1067. ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
  1068. sts->completionStatus);
  1069. break;
  1070. }
  1071. return;
  1072. }
  1073. static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  1074. {
  1075. struct ql4_task_data *task_data;
  1076. struct iscsi_session *sess;
  1077. struct ddb_entry *ddb_entry;
  1078. struct scsi_qla_host *ha;
  1079. int hdr_len;
  1080. sess = task->conn->session;
  1081. ddb_entry = sess->dd_data;
  1082. ha = ddb_entry->ha;
  1083. task_data = task->dd_data;
  1084. memset(task_data, 0, sizeof(struct ql4_task_data));
  1085. if (task->sc) {
  1086. ql4_printk(KERN_INFO, ha,
  1087. "%s: SCSI Commands not implemented\n", __func__);
  1088. return -EINVAL;
  1089. }
  1090. hdr_len = sizeof(struct iscsi_hdr);
  1091. task_data->ha = ha;
  1092. task_data->task = task;
  1093. if (task->data_count) {
  1094. task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
  1095. task->data_count,
  1096. PCI_DMA_TODEVICE);
  1097. }
  1098. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
  1099. __func__, task->conn->max_recv_dlength, hdr_len));
  1100. task_data->resp_len = task->conn->max_recv_dlength;
  1101. task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
  1102. task_data->resp_len,
  1103. &task_data->resp_dma,
  1104. GFP_ATOMIC);
  1105. if (!task_data->resp_buffer)
  1106. goto exit_alloc_pdu;
  1107. task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
  1108. task->data_count + hdr_len,
  1109. &task_data->req_dma,
  1110. GFP_ATOMIC);
  1111. if (!task_data->req_buffer)
  1112. goto exit_alloc_pdu;
  1113. task->hdr = task_data->req_buffer;
  1114. INIT_WORK(&task_data->task_work, qla4xxx_task_work);
  1115. return 0;
  1116. exit_alloc_pdu:
  1117. if (task_data->resp_buffer)
  1118. dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  1119. task_data->resp_buffer, task_data->resp_dma);
  1120. if (task_data->req_buffer)
  1121. dma_free_coherent(&ha->pdev->dev, task->data_count + hdr_len,
  1122. task_data->req_buffer, task_data->req_dma);
  1123. return -ENOMEM;
  1124. }
  1125. static void qla4xxx_task_cleanup(struct iscsi_task *task)
  1126. {
  1127. struct ql4_task_data *task_data;
  1128. struct iscsi_session *sess;
  1129. struct ddb_entry *ddb_entry;
  1130. struct scsi_qla_host *ha;
  1131. int hdr_len;
  1132. hdr_len = sizeof(struct iscsi_hdr);
  1133. sess = task->conn->session;
  1134. ddb_entry = sess->dd_data;
  1135. ha = ddb_entry->ha;
  1136. task_data = task->dd_data;
  1137. if (task->data_count) {
  1138. dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
  1139. task->data_count, PCI_DMA_TODEVICE);
  1140. }
  1141. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
  1142. __func__, task->conn->max_recv_dlength, hdr_len));
  1143. dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  1144. task_data->resp_buffer, task_data->resp_dma);
  1145. dma_free_coherent(&ha->pdev->dev, task->data_count + hdr_len,
  1146. task_data->req_buffer, task_data->req_dma);
  1147. return;
  1148. }
  1149. static int qla4xxx_task_xmit(struct iscsi_task *task)
  1150. {
  1151. struct scsi_cmnd *sc = task->sc;
  1152. struct iscsi_session *sess = task->conn->session;
  1153. struct ddb_entry *ddb_entry = sess->dd_data;
  1154. struct scsi_qla_host *ha = ddb_entry->ha;
  1155. if (!sc)
  1156. return qla4xxx_send_passthru0(task);
  1157. ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
  1158. __func__);
  1159. return -ENOSYS;
  1160. }
  1161. void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
  1162. struct ddb_entry *ddb_entry)
  1163. {
  1164. struct iscsi_cls_session *cls_sess;
  1165. struct iscsi_cls_conn *cls_conn;
  1166. struct iscsi_session *sess;
  1167. struct iscsi_conn *conn;
  1168. uint32_t ddb_state;
  1169. dma_addr_t fw_ddb_entry_dma;
  1170. struct dev_db_entry *fw_ddb_entry;
  1171. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1172. &fw_ddb_entry_dma, GFP_KERNEL);
  1173. if (!fw_ddb_entry) {
  1174. ql4_printk(KERN_ERR, ha,
  1175. "%s: Unable to allocate dma buffer\n", __func__);
  1176. return;
  1177. }
  1178. if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
  1179. fw_ddb_entry_dma, NULL, NULL, &ddb_state,
  1180. NULL, NULL, NULL) == QLA_ERROR) {
  1181. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
  1182. "get_ddb_entry for fw_ddb_index %d\n",
  1183. ha->host_no, __func__,
  1184. ddb_entry->fw_ddb_index));
  1185. return;
  1186. }
  1187. cls_sess = ddb_entry->sess;
  1188. sess = cls_sess->dd_data;
  1189. cls_conn = ddb_entry->conn;
  1190. conn = cls_conn->dd_data;
  1191. /* Update params */
  1192. conn->max_recv_dlength = BYTE_UNITS *
  1193. le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
  1194. conn->max_xmit_dlength = BYTE_UNITS *
  1195. le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
  1196. sess->initial_r2t_en =
  1197. (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1198. sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
  1199. sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1200. sess->first_burst = BYTE_UNITS *
  1201. le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
  1202. sess->max_burst = BYTE_UNITS *
  1203. le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
  1204. sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  1205. sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
  1206. sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  1207. memcpy(sess->initiatorname, ha->name_string,
  1208. min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
  1209. }
  1210. /*
  1211. * Timer routines
  1212. */
  1213. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  1214. unsigned long interval)
  1215. {
  1216. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  1217. __func__, ha->host->host_no));
  1218. init_timer(&ha->timer);
  1219. ha->timer.expires = jiffies + interval * HZ;
  1220. ha->timer.data = (unsigned long)ha;
  1221. ha->timer.function = (void (*)(unsigned long))func;
  1222. add_timer(&ha->timer);
  1223. ha->timer_active = 1;
  1224. }
  1225. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  1226. {
  1227. del_timer_sync(&ha->timer);
  1228. ha->timer_active = 0;
  1229. }
  1230. /***
  1231. * qla4xxx_mark_device_missing - blocks the session
  1232. * @cls_session: Pointer to the session to be blocked
  1233. * @ddb_entry: Pointer to device database entry
  1234. *
  1235. * This routine marks a device missing and close connection.
  1236. **/
  1237. void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
  1238. {
  1239. iscsi_block_session(cls_session);
  1240. }
  1241. /**
  1242. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  1243. * @ha: Pointer to host adapter structure.
  1244. *
  1245. * This routine marks a device missing and resets the relogin retry count.
  1246. **/
  1247. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  1248. {
  1249. iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
  1250. }
  1251. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  1252. struct ddb_entry *ddb_entry,
  1253. struct scsi_cmnd *cmd)
  1254. {
  1255. struct srb *srb;
  1256. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  1257. if (!srb)
  1258. return srb;
  1259. kref_init(&srb->srb_ref);
  1260. srb->ha = ha;
  1261. srb->ddb = ddb_entry;
  1262. srb->cmd = cmd;
  1263. srb->flags = 0;
  1264. CMD_SP(cmd) = (void *)srb;
  1265. return srb;
  1266. }
  1267. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  1268. {
  1269. struct scsi_cmnd *cmd = srb->cmd;
  1270. if (srb->flags & SRB_DMA_VALID) {
  1271. scsi_dma_unmap(cmd);
  1272. srb->flags &= ~SRB_DMA_VALID;
  1273. }
  1274. CMD_SP(cmd) = NULL;
  1275. }
  1276. void qla4xxx_srb_compl(struct kref *ref)
  1277. {
  1278. struct srb *srb = container_of(ref, struct srb, srb_ref);
  1279. struct scsi_cmnd *cmd = srb->cmd;
  1280. struct scsi_qla_host *ha = srb->ha;
  1281. qla4xxx_srb_free_dma(ha, srb);
  1282. mempool_free(srb, ha->srb_mempool);
  1283. cmd->scsi_done(cmd);
  1284. }
  1285. /**
  1286. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  1287. * @host: scsi host
  1288. * @cmd: Pointer to Linux's SCSI command structure
  1289. *
  1290. * Remarks:
  1291. * This routine is invoked by Linux to send a SCSI command to the driver.
  1292. * The mid-level driver tries to ensure that queuecommand never gets
  1293. * invoked concurrently with itself or the interrupt handler (although
  1294. * the interrupt handler may call this routine as part of request-
  1295. * completion handling). Unfortunely, it sometimes calls the scheduler
  1296. * in interrupt context which is a big NO! NO!.
  1297. **/
  1298. static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  1299. {
  1300. struct scsi_qla_host *ha = to_qla_host(host);
  1301. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1302. struct iscsi_cls_session *sess = ddb_entry->sess;
  1303. struct srb *srb;
  1304. int rval;
  1305. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1306. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  1307. cmd->result = DID_NO_CONNECT << 16;
  1308. else
  1309. cmd->result = DID_REQUEUE << 16;
  1310. goto qc_fail_command;
  1311. }
  1312. if (!sess) {
  1313. cmd->result = DID_IMM_RETRY << 16;
  1314. goto qc_fail_command;
  1315. }
  1316. rval = iscsi_session_chkready(sess);
  1317. if (rval) {
  1318. cmd->result = rval;
  1319. goto qc_fail_command;
  1320. }
  1321. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1322. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  1323. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1324. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  1325. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  1326. !test_bit(AF_ONLINE, &ha->flags) ||
  1327. !test_bit(AF_LINK_UP, &ha->flags) ||
  1328. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  1329. goto qc_host_busy;
  1330. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
  1331. if (!srb)
  1332. goto qc_host_busy;
  1333. rval = qla4xxx_send_command_to_isp(ha, srb);
  1334. if (rval != QLA_SUCCESS)
  1335. goto qc_host_busy_free_sp;
  1336. return 0;
  1337. qc_host_busy_free_sp:
  1338. qla4xxx_srb_free_dma(ha, srb);
  1339. mempool_free(srb, ha->srb_mempool);
  1340. qc_host_busy:
  1341. return SCSI_MLQUEUE_HOST_BUSY;
  1342. qc_fail_command:
  1343. cmd->scsi_done(cmd);
  1344. return 0;
  1345. }
  1346. /**
  1347. * qla4xxx_mem_free - frees memory allocated to adapter
  1348. * @ha: Pointer to host adapter structure.
  1349. *
  1350. * Frees memory previously allocated by qla4xxx_mem_alloc
  1351. **/
  1352. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  1353. {
  1354. if (ha->queues)
  1355. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  1356. ha->queues_dma);
  1357. ha->queues_len = 0;
  1358. ha->queues = NULL;
  1359. ha->queues_dma = 0;
  1360. ha->request_ring = NULL;
  1361. ha->request_dma = 0;
  1362. ha->response_ring = NULL;
  1363. ha->response_dma = 0;
  1364. ha->shadow_regs = NULL;
  1365. ha->shadow_regs_dma = 0;
  1366. /* Free srb pool. */
  1367. if (ha->srb_mempool)
  1368. mempool_destroy(ha->srb_mempool);
  1369. ha->srb_mempool = NULL;
  1370. if (ha->chap_dma_pool)
  1371. dma_pool_destroy(ha->chap_dma_pool);
  1372. /* release io space registers */
  1373. if (is_qla8022(ha)) {
  1374. if (ha->nx_pcibase)
  1375. iounmap(
  1376. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  1377. } else if (ha->reg)
  1378. iounmap(ha->reg);
  1379. pci_release_regions(ha->pdev);
  1380. }
  1381. /**
  1382. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  1383. * @ha: Pointer to host adapter structure
  1384. *
  1385. * Allocates DMA memory for request and response queues. Also allocates memory
  1386. * for srbs.
  1387. **/
  1388. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  1389. {
  1390. unsigned long align;
  1391. /* Allocate contiguous block of DMA memory for queues. */
  1392. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  1393. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  1394. sizeof(struct shadow_regs) +
  1395. MEM_ALIGN_VALUE +
  1396. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  1397. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  1398. &ha->queues_dma, GFP_KERNEL);
  1399. if (ha->queues == NULL) {
  1400. ql4_printk(KERN_WARNING, ha,
  1401. "Memory Allocation failed - queues.\n");
  1402. goto mem_alloc_error_exit;
  1403. }
  1404. memset(ha->queues, 0, ha->queues_len);
  1405. /*
  1406. * As per RISC alignment requirements -- the bus-address must be a
  1407. * multiple of the request-ring size (in bytes).
  1408. */
  1409. align = 0;
  1410. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  1411. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  1412. (MEM_ALIGN_VALUE - 1));
  1413. /* Update request and response queue pointers. */
  1414. ha->request_dma = ha->queues_dma + align;
  1415. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  1416. ha->response_dma = ha->queues_dma + align +
  1417. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  1418. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  1419. (REQUEST_QUEUE_DEPTH *
  1420. QUEUE_SIZE));
  1421. ha->shadow_regs_dma = ha->queues_dma + align +
  1422. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  1423. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  1424. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  1425. (REQUEST_QUEUE_DEPTH *
  1426. QUEUE_SIZE) +
  1427. (RESPONSE_QUEUE_DEPTH *
  1428. QUEUE_SIZE));
  1429. /* Allocate memory for srb pool. */
  1430. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  1431. mempool_free_slab, srb_cachep);
  1432. if (ha->srb_mempool == NULL) {
  1433. ql4_printk(KERN_WARNING, ha,
  1434. "Memory Allocation failed - SRB Pool.\n");
  1435. goto mem_alloc_error_exit;
  1436. }
  1437. ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
  1438. CHAP_DMA_BLOCK_SIZE, 8, 0);
  1439. if (ha->chap_dma_pool == NULL) {
  1440. ql4_printk(KERN_WARNING, ha,
  1441. "%s: chap_dma_pool allocation failed..\n", __func__);
  1442. goto mem_alloc_error_exit;
  1443. }
  1444. return QLA_SUCCESS;
  1445. mem_alloc_error_exit:
  1446. qla4xxx_mem_free(ha);
  1447. return QLA_ERROR;
  1448. }
  1449. /**
  1450. * qla4_8xxx_check_fw_alive - Check firmware health
  1451. * @ha: Pointer to host adapter structure.
  1452. *
  1453. * Context: Interrupt
  1454. **/
  1455. static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  1456. {
  1457. uint32_t fw_heartbeat_counter, halt_status;
  1458. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  1459. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  1460. if (fw_heartbeat_counter == 0xffffffff) {
  1461. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  1462. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  1463. ha->host_no, __func__));
  1464. return;
  1465. }
  1466. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  1467. ha->seconds_since_last_heartbeat++;
  1468. /* FW not alive after 2 seconds */
  1469. if (ha->seconds_since_last_heartbeat == 2) {
  1470. ha->seconds_since_last_heartbeat = 0;
  1471. halt_status = qla4_8xxx_rd_32(ha,
  1472. QLA82XX_PEG_HALT_STATUS1);
  1473. ql4_printk(KERN_INFO, ha,
  1474. "scsi(%ld): %s, Dumping hw/fw registers:\n "
  1475. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
  1476. " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
  1477. " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
  1478. " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
  1479. ha->host_no, __func__, halt_status,
  1480. qla4_8xxx_rd_32(ha,
  1481. QLA82XX_PEG_HALT_STATUS2),
  1482. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
  1483. 0x3c),
  1484. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
  1485. 0x3c),
  1486. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
  1487. 0x3c),
  1488. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
  1489. 0x3c),
  1490. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
  1491. 0x3c));
  1492. /* Since we cannot change dev_state in interrupt
  1493. * context, set appropriate DPC flag then wakeup
  1494. * DPC */
  1495. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  1496. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  1497. else {
  1498. printk("scsi%ld: %s: detect abort needed!\n",
  1499. ha->host_no, __func__);
  1500. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1501. }
  1502. qla4xxx_wake_dpc(ha);
  1503. qla4xxx_mailbox_premature_completion(ha);
  1504. }
  1505. } else
  1506. ha->seconds_since_last_heartbeat = 0;
  1507. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  1508. }
  1509. /**
  1510. * qla4_8xxx_watchdog - Poll dev state
  1511. * @ha: Pointer to host adapter structure.
  1512. *
  1513. * Context: Interrupt
  1514. **/
  1515. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  1516. {
  1517. uint32_t dev_state;
  1518. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  1519. /* don't poll if reset is going on */
  1520. if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  1521. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1522. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
  1523. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  1524. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1525. if (!ql4xdontresethba) {
  1526. ql4_printk(KERN_INFO, ha, "%s: HW State: "
  1527. "NEED RESET!\n", __func__);
  1528. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1529. qla4xxx_wake_dpc(ha);
  1530. qla4xxx_mailbox_premature_completion(ha);
  1531. }
  1532. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  1533. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1534. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
  1535. __func__);
  1536. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  1537. qla4xxx_wake_dpc(ha);
  1538. } else {
  1539. /* Check firmware health */
  1540. qla4_8xxx_check_fw_alive(ha);
  1541. }
  1542. }
  1543. }
  1544. /**
  1545. * qla4xxx_timer - checks every second for work to do.
  1546. * @ha: Pointer to host adapter structure.
  1547. **/
  1548. static void qla4xxx_timer(struct scsi_qla_host *ha)
  1549. {
  1550. int start_dpc = 0;
  1551. uint16_t w;
  1552. /* If we are in the middle of AER/EEH processing
  1553. * skip any processing and reschedule the timer
  1554. */
  1555. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1556. mod_timer(&ha->timer, jiffies + HZ);
  1557. return;
  1558. }
  1559. /* Hardware read to trigger an EEH error during mailbox waits. */
  1560. if (!pci_channel_offline(ha->pdev))
  1561. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  1562. if (is_qla8022(ha)) {
  1563. qla4_8xxx_watchdog(ha);
  1564. }
  1565. if (!is_qla8022(ha)) {
  1566. /* Check for heartbeat interval. */
  1567. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  1568. ha->heartbeat_interval != 0) {
  1569. ha->seconds_since_last_heartbeat++;
  1570. if (ha->seconds_since_last_heartbeat >
  1571. ha->heartbeat_interval + 2)
  1572. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1573. }
  1574. }
  1575. /* Wakeup the dpc routine for this adapter, if needed. */
  1576. if (start_dpc ||
  1577. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1578. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  1579. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  1580. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1581. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1582. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  1583. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  1584. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  1585. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  1586. test_bit(DPC_AEN, &ha->dpc_flags)) {
  1587. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  1588. " - dpc flags = 0x%lx\n",
  1589. ha->host_no, __func__, ha->dpc_flags));
  1590. qla4xxx_wake_dpc(ha);
  1591. }
  1592. /* Reschedule timer thread to call us back in one second */
  1593. mod_timer(&ha->timer, jiffies + HZ);
  1594. DEBUG2(ha->seconds_since_last_intr++);
  1595. }
  1596. /**
  1597. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  1598. * @ha: Pointer to host adapter structure.
  1599. *
  1600. * This routine stalls the driver until all outstanding commands are returned.
  1601. * Caller must release the Hardware Lock prior to calling this routine.
  1602. **/
  1603. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  1604. {
  1605. uint32_t index = 0;
  1606. unsigned long flags;
  1607. struct scsi_cmnd *cmd;
  1608. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  1609. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  1610. "complete\n", WAIT_CMD_TOV));
  1611. while (!time_after_eq(jiffies, wtime)) {
  1612. spin_lock_irqsave(&ha->hardware_lock, flags);
  1613. /* Find a command that hasn't completed. */
  1614. for (index = 0; index < ha->host->can_queue; index++) {
  1615. cmd = scsi_host_find_tag(ha->host, index);
  1616. /*
  1617. * We cannot just check if the index is valid,
  1618. * becase if we are run from the scsi eh, then
  1619. * the scsi/block layer is going to prevent
  1620. * the tag from being released.
  1621. */
  1622. if (cmd != NULL && CMD_SP(cmd))
  1623. break;
  1624. }
  1625. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1626. /* If No Commands are pending, wait is complete */
  1627. if (index == ha->host->can_queue)
  1628. return QLA_SUCCESS;
  1629. msleep(1000);
  1630. }
  1631. /* If we timed out on waiting for commands to come back
  1632. * return ERROR. */
  1633. return QLA_ERROR;
  1634. }
  1635. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  1636. {
  1637. uint32_t ctrl_status;
  1638. unsigned long flags = 0;
  1639. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  1640. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  1641. return QLA_ERROR;
  1642. spin_lock_irqsave(&ha->hardware_lock, flags);
  1643. /*
  1644. * If the SCSI Reset Interrupt bit is set, clear it.
  1645. * Otherwise, the Soft Reset won't work.
  1646. */
  1647. ctrl_status = readw(&ha->reg->ctrl_status);
  1648. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  1649. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  1650. /* Issue Soft Reset */
  1651. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  1652. readl(&ha->reg->ctrl_status);
  1653. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1654. return QLA_SUCCESS;
  1655. }
  1656. /**
  1657. * qla4xxx_soft_reset - performs soft reset.
  1658. * @ha: Pointer to host adapter structure.
  1659. **/
  1660. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  1661. {
  1662. uint32_t max_wait_time;
  1663. unsigned long flags = 0;
  1664. int status;
  1665. uint32_t ctrl_status;
  1666. status = qla4xxx_hw_reset(ha);
  1667. if (status != QLA_SUCCESS)
  1668. return status;
  1669. status = QLA_ERROR;
  1670. /* Wait until the Network Reset Intr bit is cleared */
  1671. max_wait_time = RESET_INTR_TOV;
  1672. do {
  1673. spin_lock_irqsave(&ha->hardware_lock, flags);
  1674. ctrl_status = readw(&ha->reg->ctrl_status);
  1675. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1676. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  1677. break;
  1678. msleep(1000);
  1679. } while ((--max_wait_time));
  1680. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  1681. DEBUG2(printk(KERN_WARNING
  1682. "scsi%ld: Network Reset Intr not cleared by "
  1683. "Network function, clearing it now!\n",
  1684. ha->host_no));
  1685. spin_lock_irqsave(&ha->hardware_lock, flags);
  1686. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  1687. readl(&ha->reg->ctrl_status);
  1688. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1689. }
  1690. /* Wait until the firmware tells us the Soft Reset is done */
  1691. max_wait_time = SOFT_RESET_TOV;
  1692. do {
  1693. spin_lock_irqsave(&ha->hardware_lock, flags);
  1694. ctrl_status = readw(&ha->reg->ctrl_status);
  1695. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1696. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  1697. status = QLA_SUCCESS;
  1698. break;
  1699. }
  1700. msleep(1000);
  1701. } while ((--max_wait_time));
  1702. /*
  1703. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  1704. * after the soft reset has taken place.
  1705. */
  1706. spin_lock_irqsave(&ha->hardware_lock, flags);
  1707. ctrl_status = readw(&ha->reg->ctrl_status);
  1708. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  1709. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  1710. readl(&ha->reg->ctrl_status);
  1711. }
  1712. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1713. /* If soft reset fails then most probably the bios on other
  1714. * function is also enabled.
  1715. * Since the initialization is sequential the other fn
  1716. * wont be able to acknowledge the soft reset.
  1717. * Issue a force soft reset to workaround this scenario.
  1718. */
  1719. if (max_wait_time == 0) {
  1720. /* Issue Force Soft Reset */
  1721. spin_lock_irqsave(&ha->hardware_lock, flags);
  1722. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  1723. readl(&ha->reg->ctrl_status);
  1724. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1725. /* Wait until the firmware tells us the Soft Reset is done */
  1726. max_wait_time = SOFT_RESET_TOV;
  1727. do {
  1728. spin_lock_irqsave(&ha->hardware_lock, flags);
  1729. ctrl_status = readw(&ha->reg->ctrl_status);
  1730. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1731. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  1732. status = QLA_SUCCESS;
  1733. break;
  1734. }
  1735. msleep(1000);
  1736. } while ((--max_wait_time));
  1737. }
  1738. return status;
  1739. }
  1740. /**
  1741. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  1742. * @ha: Pointer to host adapter structure.
  1743. * @res: returned scsi status
  1744. *
  1745. * This routine is called just prior to a HARD RESET to return all
  1746. * outstanding commands back to the Operating System.
  1747. * Caller should make sure that the following locks are released
  1748. * before this calling routine: Hardware lock, and io_request_lock.
  1749. **/
  1750. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  1751. {
  1752. struct srb *srb;
  1753. int i;
  1754. unsigned long flags;
  1755. spin_lock_irqsave(&ha->hardware_lock, flags);
  1756. for (i = 0; i < ha->host->can_queue; i++) {
  1757. srb = qla4xxx_del_from_active_array(ha, i);
  1758. if (srb != NULL) {
  1759. srb->cmd->result = res;
  1760. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  1761. }
  1762. }
  1763. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1764. }
  1765. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  1766. {
  1767. clear_bit(AF_ONLINE, &ha->flags);
  1768. /* Disable the board */
  1769. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  1770. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  1771. qla4xxx_mark_all_devices_missing(ha);
  1772. clear_bit(AF_INIT_DONE, &ha->flags);
  1773. }
  1774. static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
  1775. {
  1776. struct iscsi_session *sess;
  1777. struct ddb_entry *ddb_entry;
  1778. sess = cls_session->dd_data;
  1779. ddb_entry = sess->dd_data;
  1780. ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
  1781. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
  1782. }
  1783. /**
  1784. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  1785. * @ha: Pointer to host adapter structure.
  1786. **/
  1787. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  1788. {
  1789. int status = QLA_ERROR;
  1790. uint8_t reset_chip = 0;
  1791. /* Stall incoming I/O until we are done */
  1792. scsi_block_requests(ha->host);
  1793. clear_bit(AF_ONLINE, &ha->flags);
  1794. clear_bit(AF_LINK_UP, &ha->flags);
  1795. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  1796. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1797. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  1798. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1799. reset_chip = 1;
  1800. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  1801. * do not reset adapter, jump to initialize_adapter */
  1802. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1803. status = QLA_SUCCESS;
  1804. goto recover_ha_init_adapter;
  1805. }
  1806. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  1807. * from eh_host_reset or ioctl module */
  1808. if (is_qla8022(ha) && !reset_chip &&
  1809. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  1810. DEBUG2(ql4_printk(KERN_INFO, ha,
  1811. "scsi%ld: %s - Performing stop_firmware...\n",
  1812. ha->host_no, __func__));
  1813. status = ha->isp_ops->reset_firmware(ha);
  1814. if (status == QLA_SUCCESS) {
  1815. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  1816. qla4xxx_cmd_wait(ha);
  1817. ha->isp_ops->disable_intrs(ha);
  1818. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1819. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1820. } else {
  1821. /* If the stop_firmware fails then
  1822. * reset the entire chip */
  1823. reset_chip = 1;
  1824. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1825. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1826. }
  1827. }
  1828. /* Issue full chip reset if recovering from a catastrophic error,
  1829. * or if stop_firmware fails for ISP-82xx.
  1830. * This is the default case for ISP-4xxx */
  1831. if (!is_qla8022(ha) || reset_chip) {
  1832. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  1833. qla4xxx_cmd_wait(ha);
  1834. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1835. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1836. DEBUG2(ql4_printk(KERN_INFO, ha,
  1837. "scsi%ld: %s - Performing chip reset..\n",
  1838. ha->host_no, __func__));
  1839. status = ha->isp_ops->reset_chip(ha);
  1840. }
  1841. /* Flush any pending ddb changed AENs */
  1842. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1843. recover_ha_init_adapter:
  1844. /* Upon successful firmware/chip reset, re-initialize the adapter */
  1845. if (status == QLA_SUCCESS) {
  1846. /* For ISP-4xxx, force function 1 to always initialize
  1847. * before function 3 to prevent both funcions from
  1848. * stepping on top of the other */
  1849. if (!is_qla8022(ha) && (ha->mac_index == 3))
  1850. ssleep(6);
  1851. /* NOTE: AF_ONLINE flag set upon successful completion of
  1852. * qla4xxx_initialize_adapter */
  1853. status = qla4xxx_initialize_adapter(ha);
  1854. }
  1855. /* Retry failed adapter initialization, if necessary
  1856. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  1857. * case to prevent ping-pong resets between functions */
  1858. if (!test_bit(AF_ONLINE, &ha->flags) &&
  1859. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1860. /* Adapter initialization failed, see if we can retry
  1861. * resetting the ha.
  1862. * Since we don't want to block the DPC for too long
  1863. * with multiple resets in the same thread,
  1864. * utilize DPC to retry */
  1865. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  1866. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  1867. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  1868. "(%d) more times\n", ha->host_no,
  1869. ha->retry_reset_ha_cnt));
  1870. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1871. status = QLA_ERROR;
  1872. } else {
  1873. if (ha->retry_reset_ha_cnt > 0) {
  1874. /* Schedule another Reset HA--DPC will retry */
  1875. ha->retry_reset_ha_cnt--;
  1876. DEBUG2(printk("scsi%ld: recover adapter - "
  1877. "retry remaining %d\n",
  1878. ha->host_no,
  1879. ha->retry_reset_ha_cnt));
  1880. status = QLA_ERROR;
  1881. }
  1882. if (ha->retry_reset_ha_cnt == 0) {
  1883. /* Recover adapter retries have been exhausted.
  1884. * Adapter DEAD */
  1885. DEBUG2(printk("scsi%ld: recover adapter "
  1886. "failed - board disabled\n",
  1887. ha->host_no));
  1888. qla4xxx_dead_adapter_cleanup(ha);
  1889. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1890. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1891. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  1892. &ha->dpc_flags);
  1893. status = QLA_ERROR;
  1894. }
  1895. }
  1896. } else {
  1897. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1898. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1899. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1900. }
  1901. ha->adapter_error_count++;
  1902. if (test_bit(AF_ONLINE, &ha->flags))
  1903. ha->isp_ops->enable_intrs(ha);
  1904. scsi_unblock_requests(ha->host);
  1905. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1906. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  1907. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  1908. return status;
  1909. }
  1910. static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
  1911. {
  1912. struct iscsi_session *sess;
  1913. struct ddb_entry *ddb_entry;
  1914. struct scsi_qla_host *ha;
  1915. sess = cls_session->dd_data;
  1916. ddb_entry = sess->dd_data;
  1917. ha = ddb_entry->ha;
  1918. if (!iscsi_is_session_online(cls_session)) {
  1919. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  1920. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  1921. " unblock session\n", ha->host_no, __func__,
  1922. ddb_entry->fw_ddb_index);
  1923. iscsi_unblock_session(ddb_entry->sess);
  1924. } else {
  1925. /* Trigger relogin */
  1926. iscsi_session_failure(cls_session->dd_data,
  1927. ISCSI_ERR_CONN_FAILED);
  1928. }
  1929. }
  1930. }
  1931. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  1932. {
  1933. iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
  1934. }
  1935. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  1936. {
  1937. if (ha->dpc_thread)
  1938. queue_work(ha->dpc_thread, &ha->dpc_work);
  1939. }
  1940. /**
  1941. * qla4xxx_do_dpc - dpc routine
  1942. * @data: in our case pointer to adapter structure
  1943. *
  1944. * This routine is a task that is schedule by the interrupt handler
  1945. * to perform the background processing for interrupts. We put it
  1946. * on a task queue that is consumed whenever the scheduler runs; that's
  1947. * so you can do anything (i.e. put the process to sleep etc). In fact,
  1948. * the mid-level tries to sleep when it reaches the driver threshold
  1949. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  1950. **/
  1951. static void qla4xxx_do_dpc(struct work_struct *work)
  1952. {
  1953. struct scsi_qla_host *ha =
  1954. container_of(work, struct scsi_qla_host, dpc_work);
  1955. int status = QLA_ERROR;
  1956. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  1957. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  1958. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  1959. /* Initialization not yet finished. Don't do anything yet. */
  1960. if (!test_bit(AF_INIT_DONE, &ha->flags))
  1961. return;
  1962. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1963. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  1964. ha->host_no, __func__, ha->flags));
  1965. return;
  1966. }
  1967. if (is_qla8022(ha)) {
  1968. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  1969. qla4_8xxx_idc_lock(ha);
  1970. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1971. QLA82XX_DEV_FAILED);
  1972. qla4_8xxx_idc_unlock(ha);
  1973. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  1974. qla4_8xxx_device_state_handler(ha);
  1975. }
  1976. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1977. qla4_8xxx_need_qsnt_handler(ha);
  1978. }
  1979. }
  1980. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  1981. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1982. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1983. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  1984. if (ql4xdontresethba) {
  1985. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1986. ha->host_no, __func__));
  1987. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1988. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1989. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1990. goto dpc_post_reset_ha;
  1991. }
  1992. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1993. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1994. qla4xxx_recover_adapter(ha);
  1995. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1996. uint8_t wait_time = RESET_INTR_TOV;
  1997. while ((readw(&ha->reg->ctrl_status) &
  1998. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  1999. if (--wait_time == 0)
  2000. break;
  2001. msleep(1000);
  2002. }
  2003. if (wait_time == 0)
  2004. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  2005. "bit not cleared-- resetting\n",
  2006. ha->host_no, __func__));
  2007. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2008. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  2009. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2010. status = qla4xxx_recover_adapter(ha);
  2011. }
  2012. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  2013. if (status == QLA_SUCCESS)
  2014. ha->isp_ops->enable_intrs(ha);
  2015. }
  2016. }
  2017. dpc_post_reset_ha:
  2018. /* ---- process AEN? --- */
  2019. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  2020. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  2021. /* ---- Get DHCP IP Address? --- */
  2022. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  2023. qla4xxx_get_dhcp_ip_address(ha);
  2024. /* ---- link change? --- */
  2025. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  2026. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  2027. /* ---- link down? --- */
  2028. qla4xxx_mark_all_devices_missing(ha);
  2029. } else {
  2030. /* ---- link up? --- *
  2031. * F/W will auto login to all devices ONLY ONCE after
  2032. * link up during driver initialization and runtime
  2033. * fatal error recovery. Therefore, the driver must
  2034. * manually relogin to devices when recovering from
  2035. * connection failures, logouts, expired KATO, etc. */
  2036. qla4xxx_relogin_all_devices(ha);
  2037. }
  2038. }
  2039. }
  2040. /**
  2041. * qla4xxx_free_adapter - release the adapter
  2042. * @ha: pointer to adapter structure
  2043. **/
  2044. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  2045. {
  2046. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  2047. /* Turn-off interrupts on the card. */
  2048. ha->isp_ops->disable_intrs(ha);
  2049. }
  2050. /* Remove timer thread, if present */
  2051. if (ha->timer_active)
  2052. qla4xxx_stop_timer(ha);
  2053. /* Kill the kernel thread for this host */
  2054. if (ha->dpc_thread)
  2055. destroy_workqueue(ha->dpc_thread);
  2056. /* Kill the kernel thread for this host */
  2057. if (ha->task_wq)
  2058. destroy_workqueue(ha->task_wq);
  2059. /* Put firmware in known state */
  2060. ha->isp_ops->reset_firmware(ha);
  2061. if (is_qla8022(ha)) {
  2062. qla4_8xxx_idc_lock(ha);
  2063. qla4_8xxx_clear_drv_active(ha);
  2064. qla4_8xxx_idc_unlock(ha);
  2065. }
  2066. /* Detach interrupts */
  2067. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  2068. qla4xxx_free_irqs(ha);
  2069. /* free extra memory */
  2070. qla4xxx_mem_free(ha);
  2071. }
  2072. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  2073. {
  2074. int status = 0;
  2075. uint8_t revision_id;
  2076. unsigned long mem_base, mem_len, db_base, db_len;
  2077. struct pci_dev *pdev = ha->pdev;
  2078. status = pci_request_regions(pdev, DRIVER_NAME);
  2079. if (status) {
  2080. printk(KERN_WARNING
  2081. "scsi(%ld) Failed to reserve PIO regions (%s) "
  2082. "status=%d\n", ha->host_no, pci_name(pdev), status);
  2083. goto iospace_error_exit;
  2084. }
  2085. pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
  2086. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  2087. __func__, revision_id));
  2088. ha->revision_id = revision_id;
  2089. /* remap phys address */
  2090. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  2091. mem_len = pci_resource_len(pdev, 0);
  2092. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  2093. __func__, mem_base, mem_len));
  2094. /* mapping of pcibase pointer */
  2095. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  2096. if (!ha->nx_pcibase) {
  2097. printk(KERN_ERR
  2098. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  2099. pci_release_regions(ha->pdev);
  2100. goto iospace_error_exit;
  2101. }
  2102. /* Mapping of IO base pointer, door bell read and write pointer */
  2103. /* mapping of IO base pointer */
  2104. ha->qla4_8xxx_reg =
  2105. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  2106. 0xbc000 + (ha->pdev->devfn << 11));
  2107. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  2108. db_len = pci_resource_len(pdev, 4);
  2109. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  2110. QLA82XX_CAM_RAM_DB2);
  2111. return 0;
  2112. iospace_error_exit:
  2113. return -ENOMEM;
  2114. }
  2115. /***
  2116. * qla4xxx_iospace_config - maps registers
  2117. * @ha: pointer to adapter structure
  2118. *
  2119. * This routines maps HBA's registers from the pci address space
  2120. * into the kernel virtual address space for memory mapped i/o.
  2121. **/
  2122. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  2123. {
  2124. unsigned long pio, pio_len, pio_flags;
  2125. unsigned long mmio, mmio_len, mmio_flags;
  2126. pio = pci_resource_start(ha->pdev, 0);
  2127. pio_len = pci_resource_len(ha->pdev, 0);
  2128. pio_flags = pci_resource_flags(ha->pdev, 0);
  2129. if (pio_flags & IORESOURCE_IO) {
  2130. if (pio_len < MIN_IOBASE_LEN) {
  2131. ql4_printk(KERN_WARNING, ha,
  2132. "Invalid PCI I/O region size\n");
  2133. pio = 0;
  2134. }
  2135. } else {
  2136. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  2137. pio = 0;
  2138. }
  2139. /* Use MMIO operations for all accesses. */
  2140. mmio = pci_resource_start(ha->pdev, 1);
  2141. mmio_len = pci_resource_len(ha->pdev, 1);
  2142. mmio_flags = pci_resource_flags(ha->pdev, 1);
  2143. if (!(mmio_flags & IORESOURCE_MEM)) {
  2144. ql4_printk(KERN_ERR, ha,
  2145. "region #0 not an MMIO resource, aborting\n");
  2146. goto iospace_error_exit;
  2147. }
  2148. if (mmio_len < MIN_IOBASE_LEN) {
  2149. ql4_printk(KERN_ERR, ha,
  2150. "Invalid PCI mem region size, aborting\n");
  2151. goto iospace_error_exit;
  2152. }
  2153. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  2154. ql4_printk(KERN_WARNING, ha,
  2155. "Failed to reserve PIO/MMIO regions\n");
  2156. goto iospace_error_exit;
  2157. }
  2158. ha->pio_address = pio;
  2159. ha->pio_length = pio_len;
  2160. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  2161. if (!ha->reg) {
  2162. ql4_printk(KERN_ERR, ha,
  2163. "cannot remap MMIO, aborting\n");
  2164. goto iospace_error_exit;
  2165. }
  2166. return 0;
  2167. iospace_error_exit:
  2168. return -ENOMEM;
  2169. }
  2170. static struct isp_operations qla4xxx_isp_ops = {
  2171. .iospace_config = qla4xxx_iospace_config,
  2172. .pci_config = qla4xxx_pci_config,
  2173. .disable_intrs = qla4xxx_disable_intrs,
  2174. .enable_intrs = qla4xxx_enable_intrs,
  2175. .start_firmware = qla4xxx_start_firmware,
  2176. .intr_handler = qla4xxx_intr_handler,
  2177. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  2178. .reset_chip = qla4xxx_soft_reset,
  2179. .reset_firmware = qla4xxx_hw_reset,
  2180. .queue_iocb = qla4xxx_queue_iocb,
  2181. .complete_iocb = qla4xxx_complete_iocb,
  2182. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  2183. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  2184. .get_sys_info = qla4xxx_get_sys_info,
  2185. };
  2186. static struct isp_operations qla4_8xxx_isp_ops = {
  2187. .iospace_config = qla4_8xxx_iospace_config,
  2188. .pci_config = qla4_8xxx_pci_config,
  2189. .disable_intrs = qla4_8xxx_disable_intrs,
  2190. .enable_intrs = qla4_8xxx_enable_intrs,
  2191. .start_firmware = qla4_8xxx_load_risc,
  2192. .intr_handler = qla4_8xxx_intr_handler,
  2193. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  2194. .reset_chip = qla4_8xxx_isp_reset,
  2195. .reset_firmware = qla4_8xxx_stop_firmware,
  2196. .queue_iocb = qla4_8xxx_queue_iocb,
  2197. .complete_iocb = qla4_8xxx_complete_iocb,
  2198. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  2199. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  2200. .get_sys_info = qla4_8xxx_get_sys_info,
  2201. };
  2202. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  2203. {
  2204. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  2205. }
  2206. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  2207. {
  2208. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  2209. }
  2210. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  2211. {
  2212. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  2213. }
  2214. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  2215. {
  2216. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  2217. }
  2218. /**
  2219. * qla4xxx_probe_adapter - callback function to probe HBA
  2220. * @pdev: pointer to pci_dev structure
  2221. * @pci_device_id: pointer to pci_device entry
  2222. *
  2223. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  2224. * It returns zero if successful. It also initializes all data necessary for
  2225. * the driver.
  2226. **/
  2227. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  2228. const struct pci_device_id *ent)
  2229. {
  2230. int ret = -ENODEV, status;
  2231. struct Scsi_Host *host;
  2232. struct scsi_qla_host *ha;
  2233. uint8_t init_retry_count = 0;
  2234. char buf[34];
  2235. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  2236. uint32_t dev_state;
  2237. if (pci_enable_device(pdev))
  2238. return -1;
  2239. host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
  2240. if (host == NULL) {
  2241. printk(KERN_WARNING
  2242. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  2243. goto probe_disable_device;
  2244. }
  2245. /* Clear our data area */
  2246. ha = to_qla_host(host);
  2247. memset(ha, 0, sizeof(*ha));
  2248. /* Save the information from PCI BIOS. */
  2249. ha->pdev = pdev;
  2250. ha->host = host;
  2251. ha->host_no = host->host_no;
  2252. pci_enable_pcie_error_reporting(pdev);
  2253. /* Setup Runtime configurable options */
  2254. if (is_qla8022(ha)) {
  2255. ha->isp_ops = &qla4_8xxx_isp_ops;
  2256. rwlock_init(&ha->hw_lock);
  2257. ha->qdr_sn_window = -1;
  2258. ha->ddr_mn_window = -1;
  2259. ha->curr_window = 255;
  2260. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  2261. nx_legacy_intr = &legacy_intr[ha->func_num];
  2262. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  2263. ha->nx_legacy_intr.tgt_status_reg =
  2264. nx_legacy_intr->tgt_status_reg;
  2265. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  2266. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  2267. } else {
  2268. ha->isp_ops = &qla4xxx_isp_ops;
  2269. }
  2270. /* Set EEH reset type to fundamental if required by hba */
  2271. if (is_qla8022(ha))
  2272. pdev->needs_freset = 1;
  2273. /* Configure PCI I/O space. */
  2274. ret = ha->isp_ops->iospace_config(ha);
  2275. if (ret)
  2276. goto probe_failed_ioconfig;
  2277. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  2278. pdev->device, pdev->irq, ha->reg);
  2279. qla4xxx_config_dma_addressing(ha);
  2280. /* Initialize lists and spinlocks. */
  2281. INIT_LIST_HEAD(&ha->free_srb_q);
  2282. mutex_init(&ha->mbox_sem);
  2283. init_completion(&ha->mbx_intr_comp);
  2284. spin_lock_init(&ha->hardware_lock);
  2285. /* Allocate dma buffers */
  2286. if (qla4xxx_mem_alloc(ha)) {
  2287. ql4_printk(KERN_WARNING, ha,
  2288. "[ERROR] Failed to allocate memory for adapter\n");
  2289. ret = -ENOMEM;
  2290. goto probe_failed;
  2291. }
  2292. host->cmd_per_lun = 3;
  2293. host->max_channel = 0;
  2294. host->max_lun = MAX_LUNS - 1;
  2295. host->max_id = MAX_TARGETS;
  2296. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  2297. host->can_queue = MAX_SRBS ;
  2298. host->transportt = qla4xxx_scsi_transport;
  2299. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  2300. if (ret) {
  2301. ql4_printk(KERN_WARNING, ha,
  2302. "%s: scsi_init_shared_tag_map failed\n", __func__);
  2303. goto probe_failed;
  2304. }
  2305. pci_set_drvdata(pdev, ha);
  2306. ret = scsi_add_host(host, &pdev->dev);
  2307. if (ret)
  2308. goto probe_failed;
  2309. if (is_qla8022(ha))
  2310. (void) qla4_8xxx_get_flash_info(ha);
  2311. /*
  2312. * Initialize the Host adapter request/response queues and
  2313. * firmware
  2314. * NOTE: interrupts enabled upon successful completion
  2315. */
  2316. status = qla4xxx_initialize_adapter(ha);
  2317. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  2318. init_retry_count++ < MAX_INIT_RETRIES) {
  2319. if (is_qla8022(ha)) {
  2320. qla4_8xxx_idc_lock(ha);
  2321. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  2322. qla4_8xxx_idc_unlock(ha);
  2323. if (dev_state == QLA82XX_DEV_FAILED) {
  2324. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  2325. "initialize adapter. H/W is in failed state\n",
  2326. __func__);
  2327. break;
  2328. }
  2329. }
  2330. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  2331. "(%d)\n", __func__, init_retry_count));
  2332. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  2333. continue;
  2334. status = qla4xxx_initialize_adapter(ha);
  2335. }
  2336. if (!test_bit(AF_ONLINE, &ha->flags)) {
  2337. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  2338. if (is_qla8022(ha) && ql4xdontresethba) {
  2339. /* Put the device in failed state. */
  2340. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  2341. qla4_8xxx_idc_lock(ha);
  2342. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2343. QLA82XX_DEV_FAILED);
  2344. qla4_8xxx_idc_unlock(ha);
  2345. }
  2346. ret = -ENODEV;
  2347. goto remove_host;
  2348. }
  2349. /* Startup the kernel thread for this host adapter. */
  2350. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  2351. "qla4xxx_dpc\n", __func__));
  2352. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  2353. ha->dpc_thread = create_singlethread_workqueue(buf);
  2354. if (!ha->dpc_thread) {
  2355. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  2356. ret = -ENODEV;
  2357. goto remove_host;
  2358. }
  2359. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  2360. sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
  2361. ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
  2362. if (!ha->task_wq) {
  2363. ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
  2364. ret = -ENODEV;
  2365. goto remove_host;
  2366. }
  2367. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  2368. * (which is called indirectly by qla4xxx_initialize_adapter),
  2369. * so that irqs will be registered after crbinit but before
  2370. * mbx_intr_enable.
  2371. */
  2372. if (!is_qla8022(ha)) {
  2373. ret = qla4xxx_request_irqs(ha);
  2374. if (ret) {
  2375. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  2376. "interrupt %d already in use.\n", pdev->irq);
  2377. goto remove_host;
  2378. }
  2379. }
  2380. pci_save_state(ha->pdev);
  2381. ha->isp_ops->enable_intrs(ha);
  2382. /* Start timer thread. */
  2383. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  2384. set_bit(AF_INIT_DONE, &ha->flags);
  2385. printk(KERN_INFO
  2386. " QLogic iSCSI HBA Driver version: %s\n"
  2387. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  2388. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  2389. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  2390. ha->patch_number, ha->build_number);
  2391. qla4xxx_create_ifaces(ha);
  2392. return 0;
  2393. remove_host:
  2394. scsi_remove_host(ha->host);
  2395. probe_failed:
  2396. qla4xxx_free_adapter(ha);
  2397. probe_failed_ioconfig:
  2398. pci_disable_pcie_error_reporting(pdev);
  2399. scsi_host_put(ha->host);
  2400. probe_disable_device:
  2401. pci_disable_device(pdev);
  2402. return ret;
  2403. }
  2404. /**
  2405. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  2406. * @ha: pointer to adapter structure
  2407. *
  2408. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  2409. * so that the other port will not re-initialize while in the process of
  2410. * removing the ha due to driver unload or hba hotplug.
  2411. **/
  2412. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  2413. {
  2414. struct scsi_qla_host *other_ha = NULL;
  2415. struct pci_dev *other_pdev = NULL;
  2416. int fn = ISP4XXX_PCI_FN_2;
  2417. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  2418. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  2419. fn = ISP4XXX_PCI_FN_1;
  2420. other_pdev =
  2421. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  2422. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  2423. fn));
  2424. /* Get other_ha if other_pdev is valid and state is enable*/
  2425. if (other_pdev) {
  2426. if (atomic_read(&other_pdev->enable_cnt)) {
  2427. other_ha = pci_get_drvdata(other_pdev);
  2428. if (other_ha) {
  2429. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  2430. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  2431. "Prevent %s reinit\n", __func__,
  2432. dev_name(&other_ha->pdev->dev)));
  2433. }
  2434. }
  2435. pci_dev_put(other_pdev);
  2436. }
  2437. }
  2438. /**
  2439. * qla4xxx_remove_adapter - calback function to remove adapter.
  2440. * @pci_dev: PCI device pointer
  2441. **/
  2442. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  2443. {
  2444. struct scsi_qla_host *ha;
  2445. ha = pci_get_drvdata(pdev);
  2446. if (!is_qla8022(ha))
  2447. qla4xxx_prevent_other_port_reinit(ha);
  2448. /* destroy iface from sysfs */
  2449. qla4xxx_destroy_ifaces(ha);
  2450. scsi_remove_host(ha->host);
  2451. qla4xxx_free_adapter(ha);
  2452. scsi_host_put(ha->host);
  2453. pci_disable_pcie_error_reporting(pdev);
  2454. pci_disable_device(pdev);
  2455. pci_set_drvdata(pdev, NULL);
  2456. }
  2457. /**
  2458. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  2459. * @ha: HA context
  2460. *
  2461. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  2462. * supported addressing method.
  2463. */
  2464. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  2465. {
  2466. int retval;
  2467. /* Update our PCI device dma_mask for full 64 bit mask */
  2468. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  2469. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  2470. dev_dbg(&ha->pdev->dev,
  2471. "Failed to set 64 bit PCI consistent mask; "
  2472. "using 32 bit.\n");
  2473. retval = pci_set_consistent_dma_mask(ha->pdev,
  2474. DMA_BIT_MASK(32));
  2475. }
  2476. } else
  2477. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  2478. }
  2479. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  2480. {
  2481. struct iscsi_cls_session *cls_sess;
  2482. struct iscsi_session *sess;
  2483. struct ddb_entry *ddb;
  2484. int queue_depth = QL4_DEF_QDEPTH;
  2485. cls_sess = starget_to_session(sdev->sdev_target);
  2486. sess = cls_sess->dd_data;
  2487. ddb = sess->dd_data;
  2488. sdev->hostdata = ddb;
  2489. sdev->tagged_supported = 1;
  2490. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  2491. queue_depth = ql4xmaxqdepth;
  2492. scsi_activate_tcq(sdev, queue_depth);
  2493. return 0;
  2494. }
  2495. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  2496. {
  2497. sdev->tagged_supported = 1;
  2498. return 0;
  2499. }
  2500. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  2501. {
  2502. scsi_deactivate_tcq(sdev, 1);
  2503. }
  2504. /**
  2505. * qla4xxx_del_from_active_array - returns an active srb
  2506. * @ha: Pointer to host adapter structure.
  2507. * @index: index into the active_array
  2508. *
  2509. * This routine removes and returns the srb at the specified index
  2510. **/
  2511. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  2512. uint32_t index)
  2513. {
  2514. struct srb *srb = NULL;
  2515. struct scsi_cmnd *cmd = NULL;
  2516. cmd = scsi_host_find_tag(ha->host, index);
  2517. if (!cmd)
  2518. return srb;
  2519. srb = (struct srb *)CMD_SP(cmd);
  2520. if (!srb)
  2521. return srb;
  2522. /* update counters */
  2523. if (srb->flags & SRB_DMA_VALID) {
  2524. ha->req_q_count += srb->iocb_cnt;
  2525. ha->iocb_cnt -= srb->iocb_cnt;
  2526. if (srb->cmd)
  2527. srb->cmd->host_scribble =
  2528. (unsigned char *)(unsigned long) MAX_SRBS;
  2529. }
  2530. return srb;
  2531. }
  2532. /**
  2533. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  2534. * @ha: Pointer to host adapter structure.
  2535. * @cmd: Scsi Command to wait on.
  2536. *
  2537. * This routine waits for the command to be returned by the Firmware
  2538. * for some max time.
  2539. **/
  2540. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  2541. struct scsi_cmnd *cmd)
  2542. {
  2543. int done = 0;
  2544. struct srb *rp;
  2545. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  2546. int ret = SUCCESS;
  2547. /* Dont wait on command if PCI error is being handled
  2548. * by PCI AER driver
  2549. */
  2550. if (unlikely(pci_channel_offline(ha->pdev)) ||
  2551. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  2552. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  2553. ha->host_no, __func__);
  2554. return ret;
  2555. }
  2556. do {
  2557. /* Checking to see if its returned to OS */
  2558. rp = (struct srb *) CMD_SP(cmd);
  2559. if (rp == NULL) {
  2560. done++;
  2561. break;
  2562. }
  2563. msleep(2000);
  2564. } while (max_wait_time--);
  2565. return done;
  2566. }
  2567. /**
  2568. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  2569. * @ha: Pointer to host adapter structure
  2570. **/
  2571. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  2572. {
  2573. unsigned long wait_online;
  2574. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  2575. while (time_before(jiffies, wait_online)) {
  2576. if (adapter_up(ha))
  2577. return QLA_SUCCESS;
  2578. msleep(2000);
  2579. }
  2580. return QLA_ERROR;
  2581. }
  2582. /**
  2583. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  2584. * @ha: pointer to HBA
  2585. * @t: target id
  2586. * @l: lun id
  2587. *
  2588. * This function waits for all outstanding commands to a lun to complete. It
  2589. * returns 0 if all pending commands are returned and 1 otherwise.
  2590. **/
  2591. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  2592. struct scsi_target *stgt,
  2593. struct scsi_device *sdev)
  2594. {
  2595. int cnt;
  2596. int status = 0;
  2597. struct scsi_cmnd *cmd;
  2598. /*
  2599. * Waiting for all commands for the designated target or dev
  2600. * in the active array
  2601. */
  2602. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  2603. cmd = scsi_host_find_tag(ha->host, cnt);
  2604. if (cmd && stgt == scsi_target(cmd->device) &&
  2605. (!sdev || sdev == cmd->device)) {
  2606. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  2607. status++;
  2608. break;
  2609. }
  2610. }
  2611. }
  2612. return status;
  2613. }
  2614. /**
  2615. * qla4xxx_eh_abort - callback for abort task.
  2616. * @cmd: Pointer to Linux's SCSI command structure
  2617. *
  2618. * This routine is called by the Linux OS to abort the specified
  2619. * command.
  2620. **/
  2621. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  2622. {
  2623. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  2624. unsigned int id = cmd->device->id;
  2625. unsigned int lun = cmd->device->lun;
  2626. unsigned long flags;
  2627. struct srb *srb = NULL;
  2628. int ret = SUCCESS;
  2629. int wait = 0;
  2630. ql4_printk(KERN_INFO, ha,
  2631. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  2632. ha->host_no, id, lun, cmd);
  2633. spin_lock_irqsave(&ha->hardware_lock, flags);
  2634. srb = (struct srb *) CMD_SP(cmd);
  2635. if (!srb) {
  2636. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2637. return SUCCESS;
  2638. }
  2639. kref_get(&srb->srb_ref);
  2640. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2641. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  2642. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  2643. ha->host_no, id, lun));
  2644. ret = FAILED;
  2645. } else {
  2646. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  2647. ha->host_no, id, lun));
  2648. wait = 1;
  2649. }
  2650. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  2651. /* Wait for command to complete */
  2652. if (wait) {
  2653. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  2654. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  2655. ha->host_no, id, lun));
  2656. ret = FAILED;
  2657. }
  2658. }
  2659. ql4_printk(KERN_INFO, ha,
  2660. "scsi%ld:%d:%d: Abort command - %s\n",
  2661. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  2662. return ret;
  2663. }
  2664. /**
  2665. * qla4xxx_eh_device_reset - callback for target reset.
  2666. * @cmd: Pointer to Linux's SCSI command structure
  2667. *
  2668. * This routine is called by the Linux OS to reset all luns on the
  2669. * specified target.
  2670. **/
  2671. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  2672. {
  2673. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  2674. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  2675. int ret = FAILED, stat;
  2676. if (!ddb_entry)
  2677. return ret;
  2678. ret = iscsi_block_scsi_eh(cmd);
  2679. if (ret)
  2680. return ret;
  2681. ret = FAILED;
  2682. ql4_printk(KERN_INFO, ha,
  2683. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  2684. cmd->device->channel, cmd->device->id, cmd->device->lun);
  2685. DEBUG2(printk(KERN_INFO
  2686. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  2687. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  2688. cmd, jiffies, cmd->request->timeout / HZ,
  2689. ha->dpc_flags, cmd->result, cmd->allowed));
  2690. /* FIXME: wait for hba to go online */
  2691. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  2692. if (stat != QLA_SUCCESS) {
  2693. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  2694. goto eh_dev_reset_done;
  2695. }
  2696. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  2697. cmd->device)) {
  2698. ql4_printk(KERN_INFO, ha,
  2699. "DEVICE RESET FAILED - waiting for "
  2700. "commands.\n");
  2701. goto eh_dev_reset_done;
  2702. }
  2703. /* Send marker. */
  2704. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  2705. MM_LUN_RESET) != QLA_SUCCESS)
  2706. goto eh_dev_reset_done;
  2707. ql4_printk(KERN_INFO, ha,
  2708. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  2709. ha->host_no, cmd->device->channel, cmd->device->id,
  2710. cmd->device->lun);
  2711. ret = SUCCESS;
  2712. eh_dev_reset_done:
  2713. return ret;
  2714. }
  2715. /**
  2716. * qla4xxx_eh_target_reset - callback for target reset.
  2717. * @cmd: Pointer to Linux's SCSI command structure
  2718. *
  2719. * This routine is called by the Linux OS to reset the target.
  2720. **/
  2721. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  2722. {
  2723. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  2724. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  2725. int stat, ret;
  2726. if (!ddb_entry)
  2727. return FAILED;
  2728. ret = iscsi_block_scsi_eh(cmd);
  2729. if (ret)
  2730. return ret;
  2731. starget_printk(KERN_INFO, scsi_target(cmd->device),
  2732. "WARM TARGET RESET ISSUED.\n");
  2733. DEBUG2(printk(KERN_INFO
  2734. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  2735. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  2736. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  2737. ha->dpc_flags, cmd->result, cmd->allowed));
  2738. stat = qla4xxx_reset_target(ha, ddb_entry);
  2739. if (stat != QLA_SUCCESS) {
  2740. starget_printk(KERN_INFO, scsi_target(cmd->device),
  2741. "WARM TARGET RESET FAILED.\n");
  2742. return FAILED;
  2743. }
  2744. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  2745. NULL)) {
  2746. starget_printk(KERN_INFO, scsi_target(cmd->device),
  2747. "WARM TARGET DEVICE RESET FAILED - "
  2748. "waiting for commands.\n");
  2749. return FAILED;
  2750. }
  2751. /* Send marker. */
  2752. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  2753. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  2754. starget_printk(KERN_INFO, scsi_target(cmd->device),
  2755. "WARM TARGET DEVICE RESET FAILED - "
  2756. "marker iocb failed.\n");
  2757. return FAILED;
  2758. }
  2759. starget_printk(KERN_INFO, scsi_target(cmd->device),
  2760. "WARM TARGET RESET SUCCEEDED.\n");
  2761. return SUCCESS;
  2762. }
  2763. /**
  2764. * qla4xxx_eh_host_reset - kernel callback
  2765. * @cmd: Pointer to Linux's SCSI command structure
  2766. *
  2767. * This routine is invoked by the Linux kernel to perform fatal error
  2768. * recovery on the specified adapter.
  2769. **/
  2770. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  2771. {
  2772. int return_status = FAILED;
  2773. struct scsi_qla_host *ha;
  2774. ha = to_qla_host(cmd->device->host);
  2775. if (ql4xdontresethba) {
  2776. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  2777. ha->host_no, __func__));
  2778. return FAILED;
  2779. }
  2780. ql4_printk(KERN_INFO, ha,
  2781. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  2782. cmd->device->channel, cmd->device->id, cmd->device->lun);
  2783. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  2784. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  2785. "DEAD.\n", ha->host_no, cmd->device->channel,
  2786. __func__));
  2787. return FAILED;
  2788. }
  2789. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  2790. if (is_qla8022(ha))
  2791. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2792. else
  2793. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2794. }
  2795. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  2796. return_status = SUCCESS;
  2797. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  2798. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  2799. return return_status;
  2800. }
  2801. /* PCI AER driver recovers from all correctable errors w/o
  2802. * driver intervention. For uncorrectable errors PCI AER
  2803. * driver calls the following device driver's callbacks
  2804. *
  2805. * - Fatal Errors - link_reset
  2806. * - Non-Fatal Errors - driver's pci_error_detected() which
  2807. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  2808. *
  2809. * PCI AER driver calls
  2810. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  2811. * returns RECOVERED or NEED_RESET if fw_hung
  2812. * NEED_RESET - driver's slot_reset()
  2813. * DISCONNECT - device is dead & cannot recover
  2814. * RECOVERED - driver's pci_resume()
  2815. */
  2816. static pci_ers_result_t
  2817. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  2818. {
  2819. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2820. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  2821. ha->host_no, __func__, state);
  2822. if (!is_aer_supported(ha))
  2823. return PCI_ERS_RESULT_NONE;
  2824. switch (state) {
  2825. case pci_channel_io_normal:
  2826. clear_bit(AF_EEH_BUSY, &ha->flags);
  2827. return PCI_ERS_RESULT_CAN_RECOVER;
  2828. case pci_channel_io_frozen:
  2829. set_bit(AF_EEH_BUSY, &ha->flags);
  2830. qla4xxx_mailbox_premature_completion(ha);
  2831. qla4xxx_free_irqs(ha);
  2832. pci_disable_device(pdev);
  2833. /* Return back all IOs */
  2834. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2835. return PCI_ERS_RESULT_NEED_RESET;
  2836. case pci_channel_io_perm_failure:
  2837. set_bit(AF_EEH_BUSY, &ha->flags);
  2838. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  2839. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  2840. return PCI_ERS_RESULT_DISCONNECT;
  2841. }
  2842. return PCI_ERS_RESULT_NEED_RESET;
  2843. }
  2844. /**
  2845. * qla4xxx_pci_mmio_enabled() gets called if
  2846. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  2847. * and read/write to the device still works.
  2848. **/
  2849. static pci_ers_result_t
  2850. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  2851. {
  2852. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2853. if (!is_aer_supported(ha))
  2854. return PCI_ERS_RESULT_NONE;
  2855. return PCI_ERS_RESULT_RECOVERED;
  2856. }
  2857. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  2858. {
  2859. uint32_t rval = QLA_ERROR;
  2860. uint32_t ret = 0;
  2861. int fn;
  2862. struct pci_dev *other_pdev = NULL;
  2863. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  2864. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2865. if (test_bit(AF_ONLINE, &ha->flags)) {
  2866. clear_bit(AF_ONLINE, &ha->flags);
  2867. clear_bit(AF_LINK_UP, &ha->flags);
  2868. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  2869. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2870. }
  2871. fn = PCI_FUNC(ha->pdev->devfn);
  2872. while (fn > 0) {
  2873. fn--;
  2874. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  2875. "func %x\n", ha->host_no, __func__, fn);
  2876. /* Get the pci device given the domain, bus,
  2877. * slot/function number */
  2878. other_pdev =
  2879. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  2880. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  2881. fn));
  2882. if (!other_pdev)
  2883. continue;
  2884. if (atomic_read(&other_pdev->enable_cnt)) {
  2885. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  2886. "func in enabled state%x\n", ha->host_no,
  2887. __func__, fn);
  2888. pci_dev_put(other_pdev);
  2889. break;
  2890. }
  2891. pci_dev_put(other_pdev);
  2892. }
  2893. /* The first function on the card, the reset owner will
  2894. * start & initialize the firmware. The other functions
  2895. * on the card will reset the firmware context
  2896. */
  2897. if (!fn) {
  2898. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  2899. "0x%x is the owner\n", ha->host_no, __func__,
  2900. ha->pdev->devfn);
  2901. qla4_8xxx_idc_lock(ha);
  2902. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2903. QLA82XX_DEV_COLD);
  2904. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  2905. QLA82XX_IDC_VERSION);
  2906. qla4_8xxx_idc_unlock(ha);
  2907. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2908. rval = qla4xxx_initialize_adapter(ha);
  2909. qla4_8xxx_idc_lock(ha);
  2910. if (rval != QLA_SUCCESS) {
  2911. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2912. "FAILED\n", ha->host_no, __func__);
  2913. qla4_8xxx_clear_drv_active(ha);
  2914. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2915. QLA82XX_DEV_FAILED);
  2916. } else {
  2917. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2918. "READY\n", ha->host_no, __func__);
  2919. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2920. QLA82XX_DEV_READY);
  2921. /* Clear driver state register */
  2922. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  2923. qla4_8xxx_set_drv_active(ha);
  2924. ret = qla4xxx_request_irqs(ha);
  2925. if (ret) {
  2926. ql4_printk(KERN_WARNING, ha, "Failed to "
  2927. "reserve interrupt %d already in use.\n",
  2928. ha->pdev->irq);
  2929. rval = QLA_ERROR;
  2930. } else {
  2931. ha->isp_ops->enable_intrs(ha);
  2932. rval = QLA_SUCCESS;
  2933. }
  2934. }
  2935. qla4_8xxx_idc_unlock(ha);
  2936. } else {
  2937. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  2938. "the reset owner\n", ha->host_no, __func__,
  2939. ha->pdev->devfn);
  2940. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  2941. QLA82XX_DEV_READY)) {
  2942. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2943. rval = qla4xxx_initialize_adapter(ha);
  2944. if (rval == QLA_SUCCESS) {
  2945. ret = qla4xxx_request_irqs(ha);
  2946. if (ret) {
  2947. ql4_printk(KERN_WARNING, ha, "Failed to"
  2948. " reserve interrupt %d already in"
  2949. " use.\n", ha->pdev->irq);
  2950. rval = QLA_ERROR;
  2951. } else {
  2952. ha->isp_ops->enable_intrs(ha);
  2953. rval = QLA_SUCCESS;
  2954. }
  2955. }
  2956. qla4_8xxx_idc_lock(ha);
  2957. qla4_8xxx_set_drv_active(ha);
  2958. qla4_8xxx_idc_unlock(ha);
  2959. }
  2960. }
  2961. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2962. return rval;
  2963. }
  2964. static pci_ers_result_t
  2965. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  2966. {
  2967. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  2968. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2969. int rc;
  2970. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  2971. ha->host_no, __func__);
  2972. if (!is_aer_supported(ha))
  2973. return PCI_ERS_RESULT_NONE;
  2974. /* Restore the saved state of PCIe device -
  2975. * BAR registers, PCI Config space, PCIX, MSI,
  2976. * IOV states
  2977. */
  2978. pci_restore_state(pdev);
  2979. /* pci_restore_state() clears the saved_state flag of the device
  2980. * save restored state which resets saved_state flag
  2981. */
  2982. pci_save_state(pdev);
  2983. /* Initialize device or resume if in suspended state */
  2984. rc = pci_enable_device(pdev);
  2985. if (rc) {
  2986. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  2987. "device after reset\n", ha->host_no, __func__);
  2988. goto exit_slot_reset;
  2989. }
  2990. ha->isp_ops->disable_intrs(ha);
  2991. if (is_qla8022(ha)) {
  2992. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  2993. ret = PCI_ERS_RESULT_RECOVERED;
  2994. goto exit_slot_reset;
  2995. } else
  2996. goto exit_slot_reset;
  2997. }
  2998. exit_slot_reset:
  2999. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  3000. "device after reset\n", ha->host_no, __func__, ret);
  3001. return ret;
  3002. }
  3003. static void
  3004. qla4xxx_pci_resume(struct pci_dev *pdev)
  3005. {
  3006. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  3007. int ret;
  3008. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  3009. ha->host_no, __func__);
  3010. ret = qla4xxx_wait_for_hba_online(ha);
  3011. if (ret != QLA_SUCCESS) {
  3012. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  3013. "resume I/O from slot/link_reset\n", ha->host_no,
  3014. __func__);
  3015. }
  3016. pci_cleanup_aer_uncorrect_error_status(pdev);
  3017. clear_bit(AF_EEH_BUSY, &ha->flags);
  3018. }
  3019. static struct pci_error_handlers qla4xxx_err_handler = {
  3020. .error_detected = qla4xxx_pci_error_detected,
  3021. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  3022. .slot_reset = qla4xxx_pci_slot_reset,
  3023. .resume = qla4xxx_pci_resume,
  3024. };
  3025. static struct pci_device_id qla4xxx_pci_tbl[] = {
  3026. {
  3027. .vendor = PCI_VENDOR_ID_QLOGIC,
  3028. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  3029. .subvendor = PCI_ANY_ID,
  3030. .subdevice = PCI_ANY_ID,
  3031. },
  3032. {
  3033. .vendor = PCI_VENDOR_ID_QLOGIC,
  3034. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  3035. .subvendor = PCI_ANY_ID,
  3036. .subdevice = PCI_ANY_ID,
  3037. },
  3038. {
  3039. .vendor = PCI_VENDOR_ID_QLOGIC,
  3040. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  3041. .subvendor = PCI_ANY_ID,
  3042. .subdevice = PCI_ANY_ID,
  3043. },
  3044. {
  3045. .vendor = PCI_VENDOR_ID_QLOGIC,
  3046. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  3047. .subvendor = PCI_ANY_ID,
  3048. .subdevice = PCI_ANY_ID,
  3049. },
  3050. {0, 0},
  3051. };
  3052. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  3053. static struct pci_driver qla4xxx_pci_driver = {
  3054. .name = DRIVER_NAME,
  3055. .id_table = qla4xxx_pci_tbl,
  3056. .probe = qla4xxx_probe_adapter,
  3057. .remove = qla4xxx_remove_adapter,
  3058. .err_handler = &qla4xxx_err_handler,
  3059. };
  3060. static int __init qla4xxx_module_init(void)
  3061. {
  3062. int ret;
  3063. /* Allocate cache for SRBs. */
  3064. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  3065. SLAB_HWCACHE_ALIGN, NULL);
  3066. if (srb_cachep == NULL) {
  3067. printk(KERN_ERR
  3068. "%s: Unable to allocate SRB cache..."
  3069. "Failing load!\n", DRIVER_NAME);
  3070. ret = -ENOMEM;
  3071. goto no_srp_cache;
  3072. }
  3073. /* Derive version string. */
  3074. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  3075. if (ql4xextended_error_logging)
  3076. strcat(qla4xxx_version_str, "-debug");
  3077. qla4xxx_scsi_transport =
  3078. iscsi_register_transport(&qla4xxx_iscsi_transport);
  3079. if (!qla4xxx_scsi_transport){
  3080. ret = -ENODEV;
  3081. goto release_srb_cache;
  3082. }
  3083. ret = pci_register_driver(&qla4xxx_pci_driver);
  3084. if (ret)
  3085. goto unregister_transport;
  3086. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  3087. return 0;
  3088. unregister_transport:
  3089. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  3090. release_srb_cache:
  3091. kmem_cache_destroy(srb_cachep);
  3092. no_srp_cache:
  3093. return ret;
  3094. }
  3095. static void __exit qla4xxx_module_exit(void)
  3096. {
  3097. pci_unregister_driver(&qla4xxx_pci_driver);
  3098. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  3099. kmem_cache_destroy(srb_cachep);
  3100. }
  3101. module_init(qla4xxx_module_init);
  3102. module_exit(qla4xxx_module_exit);
  3103. MODULE_AUTHOR("QLogic Corporation");
  3104. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  3105. MODULE_LICENSE("GPL");
  3106. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);