ql4_os.c 86 KB

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