ql4_os.c 71 KB

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