ql4_os.c 72 KB

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