ql4_os.c 81 KB

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