qla_bsg.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2014 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/kthread.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/delay.h>
  11. #include <linux/bsg-lib.h>
  12. /* BSG support for ELS/CT pass through */
  13. void
  14. qla2x00_bsg_job_done(void *data, void *ptr, int res)
  15. {
  16. srb_t *sp = (srb_t *)ptr;
  17. struct scsi_qla_host *vha = (scsi_qla_host_t *)data;
  18. struct bsg_job *bsg_job = sp->u.bsg_job;
  19. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  20. bsg_reply->result = res;
  21. bsg_job_done(bsg_job, bsg_reply->result,
  22. bsg_reply->reply_payload_rcv_len);
  23. sp->free(vha, sp);
  24. }
  25. void
  26. qla2x00_bsg_sp_free(void *data, void *ptr)
  27. {
  28. srb_t *sp = (srb_t *)ptr;
  29. struct scsi_qla_host *vha = sp->fcport->vha;
  30. struct bsg_job *bsg_job = sp->u.bsg_job;
  31. struct fc_bsg_request *bsg_request = bsg_job->request;
  32. struct qla_hw_data *ha = vha->hw;
  33. struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
  34. if (sp->type == SRB_FXIOCB_BCMD) {
  35. piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
  36. &bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  37. if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
  38. dma_unmap_sg(&ha->pdev->dev,
  39. bsg_job->request_payload.sg_list,
  40. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  41. if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
  42. dma_unmap_sg(&ha->pdev->dev,
  43. bsg_job->reply_payload.sg_list,
  44. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  45. } else {
  46. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  47. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  48. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  49. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  50. }
  51. if (sp->type == SRB_CT_CMD ||
  52. sp->type == SRB_FXIOCB_BCMD ||
  53. sp->type == SRB_ELS_CMD_HST)
  54. kfree(sp->fcport);
  55. qla2x00_rel_sp(vha, sp);
  56. }
  57. int
  58. qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
  59. struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
  60. {
  61. int i, ret, num_valid;
  62. uint8_t *bcode;
  63. struct qla_fcp_prio_entry *pri_entry;
  64. uint32_t *bcode_val_ptr, bcode_val;
  65. ret = 1;
  66. num_valid = 0;
  67. bcode = (uint8_t *)pri_cfg;
  68. bcode_val_ptr = (uint32_t *)pri_cfg;
  69. bcode_val = (uint32_t)(*bcode_val_ptr);
  70. if (bcode_val == 0xFFFFFFFF) {
  71. /* No FCP Priority config data in flash */
  72. ql_dbg(ql_dbg_user, vha, 0x7051,
  73. "No FCP Priority config data.\n");
  74. return 0;
  75. }
  76. if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
  77. bcode[3] != 'S') {
  78. /* Invalid FCP priority data header*/
  79. ql_dbg(ql_dbg_user, vha, 0x7052,
  80. "Invalid FCP Priority data header. bcode=0x%x.\n",
  81. bcode_val);
  82. return 0;
  83. }
  84. if (flag != 1)
  85. return ret;
  86. pri_entry = &pri_cfg->entry[0];
  87. for (i = 0; i < pri_cfg->num_entries; i++) {
  88. if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
  89. num_valid++;
  90. pri_entry++;
  91. }
  92. if (num_valid == 0) {
  93. /* No valid FCP priority data entries */
  94. ql_dbg(ql_dbg_user, vha, 0x7053,
  95. "No valid FCP Priority data entries.\n");
  96. ret = 0;
  97. } else {
  98. /* FCP priority data is valid */
  99. ql_dbg(ql_dbg_user, vha, 0x7054,
  100. "Valid FCP priority data. num entries = %d.\n",
  101. num_valid);
  102. }
  103. return ret;
  104. }
  105. static int
  106. qla24xx_proc_fcp_prio_cfg_cmd(struct bsg_job *bsg_job)
  107. {
  108. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  109. struct fc_bsg_request *bsg_request = bsg_job->request;
  110. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  111. scsi_qla_host_t *vha = shost_priv(host);
  112. struct qla_hw_data *ha = vha->hw;
  113. int ret = 0;
  114. uint32_t len;
  115. uint32_t oper;
  116. if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_P3P_TYPE(ha))) {
  117. ret = -EINVAL;
  118. goto exit_fcp_prio_cfg;
  119. }
  120. /* Get the sub command */
  121. oper = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  122. /* Only set config is allowed if config memory is not allocated */
  123. if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
  124. ret = -EINVAL;
  125. goto exit_fcp_prio_cfg;
  126. }
  127. switch (oper) {
  128. case QLFC_FCP_PRIO_DISABLE:
  129. if (ha->flags.fcp_prio_enabled) {
  130. ha->flags.fcp_prio_enabled = 0;
  131. ha->fcp_prio_cfg->attributes &=
  132. ~FCP_PRIO_ATTR_ENABLE;
  133. qla24xx_update_all_fcp_prio(vha);
  134. bsg_reply->result = DID_OK;
  135. } else {
  136. ret = -EINVAL;
  137. bsg_reply->result = (DID_ERROR << 16);
  138. goto exit_fcp_prio_cfg;
  139. }
  140. break;
  141. case QLFC_FCP_PRIO_ENABLE:
  142. if (!ha->flags.fcp_prio_enabled) {
  143. if (ha->fcp_prio_cfg) {
  144. ha->flags.fcp_prio_enabled = 1;
  145. ha->fcp_prio_cfg->attributes |=
  146. FCP_PRIO_ATTR_ENABLE;
  147. qla24xx_update_all_fcp_prio(vha);
  148. bsg_reply->result = DID_OK;
  149. } else {
  150. ret = -EINVAL;
  151. bsg_reply->result = (DID_ERROR << 16);
  152. goto exit_fcp_prio_cfg;
  153. }
  154. }
  155. break;
  156. case QLFC_FCP_PRIO_GET_CONFIG:
  157. len = bsg_job->reply_payload.payload_len;
  158. if (!len || len > FCP_PRIO_CFG_SIZE) {
  159. ret = -EINVAL;
  160. bsg_reply->result = (DID_ERROR << 16);
  161. goto exit_fcp_prio_cfg;
  162. }
  163. bsg_reply->result = DID_OK;
  164. bsg_reply->reply_payload_rcv_len =
  165. sg_copy_from_buffer(
  166. bsg_job->reply_payload.sg_list,
  167. bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
  168. len);
  169. break;
  170. case QLFC_FCP_PRIO_SET_CONFIG:
  171. len = bsg_job->request_payload.payload_len;
  172. if (!len || len > FCP_PRIO_CFG_SIZE) {
  173. bsg_reply->result = (DID_ERROR << 16);
  174. ret = -EINVAL;
  175. goto exit_fcp_prio_cfg;
  176. }
  177. if (!ha->fcp_prio_cfg) {
  178. ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
  179. if (!ha->fcp_prio_cfg) {
  180. ql_log(ql_log_warn, vha, 0x7050,
  181. "Unable to allocate memory for fcp prio "
  182. "config data (%x).\n", FCP_PRIO_CFG_SIZE);
  183. bsg_reply->result = (DID_ERROR << 16);
  184. ret = -ENOMEM;
  185. goto exit_fcp_prio_cfg;
  186. }
  187. }
  188. memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
  189. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  190. bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
  191. FCP_PRIO_CFG_SIZE);
  192. /* validate fcp priority data */
  193. if (!qla24xx_fcp_prio_cfg_valid(vha,
  194. (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
  195. bsg_reply->result = (DID_ERROR << 16);
  196. ret = -EINVAL;
  197. /* If buffer was invalidatic int
  198. * fcp_prio_cfg is of no use
  199. */
  200. vfree(ha->fcp_prio_cfg);
  201. ha->fcp_prio_cfg = NULL;
  202. goto exit_fcp_prio_cfg;
  203. }
  204. ha->flags.fcp_prio_enabled = 0;
  205. if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
  206. ha->flags.fcp_prio_enabled = 1;
  207. qla24xx_update_all_fcp_prio(vha);
  208. bsg_reply->result = DID_OK;
  209. break;
  210. default:
  211. ret = -EINVAL;
  212. break;
  213. }
  214. exit_fcp_prio_cfg:
  215. if (!ret)
  216. bsg_job_done(bsg_job, bsg_reply->result,
  217. bsg_reply->reply_payload_rcv_len);
  218. return ret;
  219. }
  220. static int
  221. qla2x00_process_els(struct bsg_job *bsg_job)
  222. {
  223. struct fc_bsg_request *bsg_request = bsg_job->request;
  224. struct fc_rport *rport;
  225. fc_port_t *fcport = NULL;
  226. struct Scsi_Host *host;
  227. scsi_qla_host_t *vha;
  228. struct qla_hw_data *ha;
  229. srb_t *sp;
  230. const char *type;
  231. int req_sg_cnt, rsp_sg_cnt;
  232. int rval = (DRIVER_ERROR << 16);
  233. uint16_t nextlid = 0;
  234. if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
  235. rport = fc_bsg_to_rport(bsg_job);
  236. fcport = *(fc_port_t **) rport->dd_data;
  237. host = rport_to_shost(rport);
  238. vha = shost_priv(host);
  239. ha = vha->hw;
  240. type = "FC_BSG_RPT_ELS";
  241. } else {
  242. host = fc_bsg_to_shost(bsg_job);
  243. vha = shost_priv(host);
  244. ha = vha->hw;
  245. type = "FC_BSG_HST_ELS_NOLOGIN";
  246. }
  247. if (!vha->flags.online) {
  248. ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
  249. rval = -EIO;
  250. goto done;
  251. }
  252. /* pass through is supported only for ISP 4Gb or higher */
  253. if (!IS_FWI2_CAPABLE(ha)) {
  254. ql_dbg(ql_dbg_user, vha, 0x7001,
  255. "ELS passthru not supported for ISP23xx based adapters.\n");
  256. rval = -EPERM;
  257. goto done;
  258. }
  259. /* Multiple SG's are not supported for ELS requests */
  260. if (bsg_job->request_payload.sg_cnt > 1 ||
  261. bsg_job->reply_payload.sg_cnt > 1) {
  262. ql_dbg(ql_dbg_user, vha, 0x7002,
  263. "Multiple SG's are not suppored for ELS requests, "
  264. "request_sg_cnt=%x reply_sg_cnt=%x.\n",
  265. bsg_job->request_payload.sg_cnt,
  266. bsg_job->reply_payload.sg_cnt);
  267. rval = -EPERM;
  268. goto done;
  269. }
  270. /* ELS request for rport */
  271. if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
  272. /* make sure the rport is logged in,
  273. * if not perform fabric login
  274. */
  275. if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
  276. ql_dbg(ql_dbg_user, vha, 0x7003,
  277. "Failed to login port %06X for ELS passthru.\n",
  278. fcport->d_id.b24);
  279. rval = -EIO;
  280. goto done;
  281. }
  282. } else {
  283. /* Allocate a dummy fcport structure, since functions
  284. * preparing the IOCB and mailbox command retrieves port
  285. * specific information from fcport structure. For Host based
  286. * ELS commands there will be no fcport structure allocated
  287. */
  288. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  289. if (!fcport) {
  290. rval = -ENOMEM;
  291. goto done;
  292. }
  293. /* Initialize all required fields of fcport */
  294. fcport->vha = vha;
  295. fcport->d_id.b.al_pa =
  296. bsg_request->rqst_data.h_els.port_id[0];
  297. fcport->d_id.b.area =
  298. bsg_request->rqst_data.h_els.port_id[1];
  299. fcport->d_id.b.domain =
  300. bsg_request->rqst_data.h_els.port_id[2];
  301. fcport->loop_id =
  302. (fcport->d_id.b.al_pa == 0xFD) ?
  303. NPH_FABRIC_CONTROLLER : NPH_F_PORT;
  304. }
  305. req_sg_cnt =
  306. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  307. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  308. if (!req_sg_cnt) {
  309. rval = -ENOMEM;
  310. goto done_free_fcport;
  311. }
  312. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  313. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  314. if (!rsp_sg_cnt) {
  315. rval = -ENOMEM;
  316. goto done_free_fcport;
  317. }
  318. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  319. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  320. ql_log(ql_log_warn, vha, 0x7008,
  321. "dma mapping resulted in different sg counts, "
  322. "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
  323. "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
  324. req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  325. rval = -EAGAIN;
  326. goto done_unmap_sg;
  327. }
  328. /* Alloc SRB structure */
  329. sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
  330. if (!sp) {
  331. rval = -ENOMEM;
  332. goto done_unmap_sg;
  333. }
  334. sp->type =
  335. (bsg_request->msgcode == FC_BSG_RPT_ELS ?
  336. SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
  337. sp->name =
  338. (bsg_request->msgcode == FC_BSG_RPT_ELS ?
  339. "bsg_els_rpt" : "bsg_els_hst");
  340. sp->u.bsg_job = bsg_job;
  341. sp->free = qla2x00_bsg_sp_free;
  342. sp->done = qla2x00_bsg_job_done;
  343. ql_dbg(ql_dbg_user, vha, 0x700a,
  344. "bsg rqst type: %s els type: %x - loop-id=%x "
  345. "portid=%-2x%02x%02x.\n", type,
  346. bsg_request->rqst_data.h_els.command_code, fcport->loop_id,
  347. fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
  348. rval = qla2x00_start_sp(sp);
  349. if (rval != QLA_SUCCESS) {
  350. ql_log(ql_log_warn, vha, 0x700e,
  351. "qla2x00_start_sp failed = %d\n", rval);
  352. qla2x00_rel_sp(vha, sp);
  353. rval = -EIO;
  354. goto done_unmap_sg;
  355. }
  356. return rval;
  357. done_unmap_sg:
  358. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  359. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  360. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  361. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  362. goto done_free_fcport;
  363. done_free_fcport:
  364. if (bsg_request->msgcode == FC_BSG_RPT_ELS)
  365. kfree(fcport);
  366. done:
  367. return rval;
  368. }
  369. static inline uint16_t
  370. qla24xx_calc_ct_iocbs(uint16_t dsds)
  371. {
  372. uint16_t iocbs;
  373. iocbs = 1;
  374. if (dsds > 2) {
  375. iocbs += (dsds - 2) / 5;
  376. if ((dsds - 2) % 5)
  377. iocbs++;
  378. }
  379. return iocbs;
  380. }
  381. static int
  382. qla2x00_process_ct(struct bsg_job *bsg_job)
  383. {
  384. srb_t *sp;
  385. struct fc_bsg_request *bsg_request = bsg_job->request;
  386. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  387. scsi_qla_host_t *vha = shost_priv(host);
  388. struct qla_hw_data *ha = vha->hw;
  389. int rval = (DRIVER_ERROR << 16);
  390. int req_sg_cnt, rsp_sg_cnt;
  391. uint16_t loop_id;
  392. struct fc_port *fcport;
  393. char *type = "FC_BSG_HST_CT";
  394. req_sg_cnt =
  395. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  396. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  397. if (!req_sg_cnt) {
  398. ql_log(ql_log_warn, vha, 0x700f,
  399. "dma_map_sg return %d for request\n", req_sg_cnt);
  400. rval = -ENOMEM;
  401. goto done;
  402. }
  403. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  404. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  405. if (!rsp_sg_cnt) {
  406. ql_log(ql_log_warn, vha, 0x7010,
  407. "dma_map_sg return %d for reply\n", rsp_sg_cnt);
  408. rval = -ENOMEM;
  409. goto done;
  410. }
  411. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  412. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  413. ql_log(ql_log_warn, vha, 0x7011,
  414. "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
  415. "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
  416. req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  417. rval = -EAGAIN;
  418. goto done_unmap_sg;
  419. }
  420. if (!vha->flags.online) {
  421. ql_log(ql_log_warn, vha, 0x7012,
  422. "Host is not online.\n");
  423. rval = -EIO;
  424. goto done_unmap_sg;
  425. }
  426. loop_id =
  427. (bsg_request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
  428. >> 24;
  429. switch (loop_id) {
  430. case 0xFC:
  431. loop_id = cpu_to_le16(NPH_SNS);
  432. break;
  433. case 0xFA:
  434. loop_id = vha->mgmt_svr_loop_id;
  435. break;
  436. default:
  437. ql_dbg(ql_dbg_user, vha, 0x7013,
  438. "Unknown loop id: %x.\n", loop_id);
  439. rval = -EINVAL;
  440. goto done_unmap_sg;
  441. }
  442. /* Allocate a dummy fcport structure, since functions preparing the
  443. * IOCB and mailbox command retrieves port specific information
  444. * from fcport structure. For Host based ELS commands there will be
  445. * no fcport structure allocated
  446. */
  447. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  448. if (!fcport) {
  449. ql_log(ql_log_warn, vha, 0x7014,
  450. "Failed to allocate fcport.\n");
  451. rval = -ENOMEM;
  452. goto done_unmap_sg;
  453. }
  454. /* Initialize all required fields of fcport */
  455. fcport->vha = vha;
  456. fcport->d_id.b.al_pa = bsg_request->rqst_data.h_ct.port_id[0];
  457. fcport->d_id.b.area = bsg_request->rqst_data.h_ct.port_id[1];
  458. fcport->d_id.b.domain = bsg_request->rqst_data.h_ct.port_id[2];
  459. fcport->loop_id = loop_id;
  460. /* Alloc SRB structure */
  461. sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
  462. if (!sp) {
  463. ql_log(ql_log_warn, vha, 0x7015,
  464. "qla2x00_get_sp failed.\n");
  465. rval = -ENOMEM;
  466. goto done_free_fcport;
  467. }
  468. sp->type = SRB_CT_CMD;
  469. sp->name = "bsg_ct";
  470. sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
  471. sp->u.bsg_job = bsg_job;
  472. sp->free = qla2x00_bsg_sp_free;
  473. sp->done = qla2x00_bsg_job_done;
  474. ql_dbg(ql_dbg_user, vha, 0x7016,
  475. "bsg rqst type: %s else type: %x - "
  476. "loop-id=%x portid=%02x%02x%02x.\n", type,
  477. (bsg_request->rqst_data.h_ct.preamble_word2 >> 16),
  478. fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
  479. fcport->d_id.b.al_pa);
  480. rval = qla2x00_start_sp(sp);
  481. if (rval != QLA_SUCCESS) {
  482. ql_log(ql_log_warn, vha, 0x7017,
  483. "qla2x00_start_sp failed=%d.\n", rval);
  484. qla2x00_rel_sp(vha, sp);
  485. rval = -EIO;
  486. goto done_free_fcport;
  487. }
  488. return rval;
  489. done_free_fcport:
  490. kfree(fcport);
  491. done_unmap_sg:
  492. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  493. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  494. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  495. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  496. done:
  497. return rval;
  498. }
  499. /* Disable loopback mode */
  500. static inline int
  501. qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
  502. int wait, int wait2)
  503. {
  504. int ret = 0;
  505. int rval = 0;
  506. uint16_t new_config[4];
  507. struct qla_hw_data *ha = vha->hw;
  508. if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
  509. goto done_reset_internal;
  510. memset(new_config, 0 , sizeof(new_config));
  511. if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
  512. ENABLE_INTERNAL_LOOPBACK ||
  513. (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
  514. ENABLE_EXTERNAL_LOOPBACK) {
  515. new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
  516. ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
  517. (new_config[0] & INTERNAL_LOOPBACK_MASK));
  518. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
  519. ha->notify_dcbx_comp = wait;
  520. ha->notify_lb_portup_comp = wait2;
  521. ret = qla81xx_set_port_config(vha, new_config);
  522. if (ret != QLA_SUCCESS) {
  523. ql_log(ql_log_warn, vha, 0x7025,
  524. "Set port config failed.\n");
  525. ha->notify_dcbx_comp = 0;
  526. ha->notify_lb_portup_comp = 0;
  527. rval = -EINVAL;
  528. goto done_reset_internal;
  529. }
  530. /* Wait for DCBX complete event */
  531. if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
  532. (DCBX_COMP_TIMEOUT * HZ))) {
  533. ql_dbg(ql_dbg_user, vha, 0x7026,
  534. "DCBX completion not received.\n");
  535. ha->notify_dcbx_comp = 0;
  536. ha->notify_lb_portup_comp = 0;
  537. rval = -EINVAL;
  538. goto done_reset_internal;
  539. } else
  540. ql_dbg(ql_dbg_user, vha, 0x7027,
  541. "DCBX completion received.\n");
  542. if (wait2 &&
  543. !wait_for_completion_timeout(&ha->lb_portup_comp,
  544. (LB_PORTUP_COMP_TIMEOUT * HZ))) {
  545. ql_dbg(ql_dbg_user, vha, 0x70c5,
  546. "Port up completion not received.\n");
  547. ha->notify_lb_portup_comp = 0;
  548. rval = -EINVAL;
  549. goto done_reset_internal;
  550. } else
  551. ql_dbg(ql_dbg_user, vha, 0x70c6,
  552. "Port up completion received.\n");
  553. ha->notify_dcbx_comp = 0;
  554. ha->notify_lb_portup_comp = 0;
  555. }
  556. done_reset_internal:
  557. return rval;
  558. }
  559. /*
  560. * Set the port configuration to enable the internal or external loopback
  561. * depending on the loopback mode.
  562. */
  563. static inline int
  564. qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
  565. uint16_t *new_config, uint16_t mode)
  566. {
  567. int ret = 0;
  568. int rval = 0;
  569. unsigned long rem_tmo = 0, current_tmo = 0;
  570. struct qla_hw_data *ha = vha->hw;
  571. if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
  572. goto done_set_internal;
  573. if (mode == INTERNAL_LOOPBACK)
  574. new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
  575. else if (mode == EXTERNAL_LOOPBACK)
  576. new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
  577. ql_dbg(ql_dbg_user, vha, 0x70be,
  578. "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));
  579. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
  580. ha->notify_dcbx_comp = 1;
  581. ret = qla81xx_set_port_config(vha, new_config);
  582. if (ret != QLA_SUCCESS) {
  583. ql_log(ql_log_warn, vha, 0x7021,
  584. "set port config failed.\n");
  585. ha->notify_dcbx_comp = 0;
  586. rval = -EINVAL;
  587. goto done_set_internal;
  588. }
  589. /* Wait for DCBX complete event */
  590. current_tmo = DCBX_COMP_TIMEOUT * HZ;
  591. while (1) {
  592. rem_tmo = wait_for_completion_timeout(&ha->dcbx_comp,
  593. current_tmo);
  594. if (!ha->idc_extend_tmo || rem_tmo) {
  595. ha->idc_extend_tmo = 0;
  596. break;
  597. }
  598. current_tmo = ha->idc_extend_tmo * HZ;
  599. ha->idc_extend_tmo = 0;
  600. }
  601. if (!rem_tmo) {
  602. ql_dbg(ql_dbg_user, vha, 0x7022,
  603. "DCBX completion not received.\n");
  604. ret = qla81xx_reset_loopback_mode(vha, new_config, 0, 0);
  605. /*
  606. * If the reset of the loopback mode doesn't work take a FCoE
  607. * dump and reset the chip.
  608. */
  609. if (ret) {
  610. ha->isp_ops->fw_dump(vha, 0);
  611. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  612. }
  613. rval = -EINVAL;
  614. } else {
  615. if (ha->flags.idc_compl_status) {
  616. ql_dbg(ql_dbg_user, vha, 0x70c3,
  617. "Bad status in IDC Completion AEN\n");
  618. rval = -EINVAL;
  619. ha->flags.idc_compl_status = 0;
  620. } else
  621. ql_dbg(ql_dbg_user, vha, 0x7023,
  622. "DCBX completion received.\n");
  623. }
  624. ha->notify_dcbx_comp = 0;
  625. ha->idc_extend_tmo = 0;
  626. done_set_internal:
  627. return rval;
  628. }
  629. static int
  630. qla2x00_process_loopback(struct bsg_job *bsg_job)
  631. {
  632. struct fc_bsg_request *bsg_request = bsg_job->request;
  633. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  634. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  635. scsi_qla_host_t *vha = shost_priv(host);
  636. struct qla_hw_data *ha = vha->hw;
  637. int rval;
  638. uint8_t command_sent;
  639. char *type;
  640. struct msg_echo_lb elreq;
  641. uint16_t response[MAILBOX_REGISTER_COUNT];
  642. uint16_t config[4], new_config[4];
  643. uint8_t *fw_sts_ptr;
  644. uint8_t *req_data = NULL;
  645. dma_addr_t req_data_dma;
  646. uint32_t req_data_len;
  647. uint8_t *rsp_data = NULL;
  648. dma_addr_t rsp_data_dma;
  649. uint32_t rsp_data_len;
  650. if (!vha->flags.online) {
  651. ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
  652. return -EIO;
  653. }
  654. elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  655. bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
  656. DMA_TO_DEVICE);
  657. if (!elreq.req_sg_cnt) {
  658. ql_log(ql_log_warn, vha, 0x701a,
  659. "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
  660. return -ENOMEM;
  661. }
  662. elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  663. bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
  664. DMA_FROM_DEVICE);
  665. if (!elreq.rsp_sg_cnt) {
  666. ql_log(ql_log_warn, vha, 0x701b,
  667. "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
  668. rval = -ENOMEM;
  669. goto done_unmap_req_sg;
  670. }
  671. if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  672. (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  673. ql_log(ql_log_warn, vha, 0x701c,
  674. "dma mapping resulted in different sg counts, "
  675. "request_sg_cnt: %x dma_request_sg_cnt: %x "
  676. "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
  677. bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
  678. bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
  679. rval = -EAGAIN;
  680. goto done_unmap_sg;
  681. }
  682. req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
  683. req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
  684. &req_data_dma, GFP_KERNEL);
  685. if (!req_data) {
  686. ql_log(ql_log_warn, vha, 0x701d,
  687. "dma alloc failed for req_data.\n");
  688. rval = -ENOMEM;
  689. goto done_unmap_sg;
  690. }
  691. rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
  692. &rsp_data_dma, GFP_KERNEL);
  693. if (!rsp_data) {
  694. ql_log(ql_log_warn, vha, 0x7004,
  695. "dma alloc failed for rsp_data.\n");
  696. rval = -ENOMEM;
  697. goto done_free_dma_req;
  698. }
  699. /* Copy the request buffer in req_data now */
  700. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  701. bsg_job->request_payload.sg_cnt, req_data, req_data_len);
  702. elreq.send_dma = req_data_dma;
  703. elreq.rcv_dma = rsp_data_dma;
  704. elreq.transfer_size = req_data_len;
  705. elreq.options = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  706. elreq.iteration_count =
  707. bsg_request->rqst_data.h_vendor.vendor_cmd[2];
  708. if (atomic_read(&vha->loop_state) == LOOP_READY &&
  709. (ha->current_topology == ISP_CFG_F ||
  710. ((IS_QLA81XX(ha) || IS_QLA8031(ha) || IS_QLA8044(ha)) &&
  711. le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
  712. && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
  713. elreq.options == EXTERNAL_LOOPBACK) {
  714. type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
  715. ql_dbg(ql_dbg_user, vha, 0x701e,
  716. "BSG request type: %s.\n", type);
  717. command_sent = INT_DEF_LB_ECHO_CMD;
  718. rval = qla2x00_echo_test(vha, &elreq, response);
  719. } else {
  720. if (IS_QLA81XX(ha) || IS_QLA8031(ha) || IS_QLA8044(ha)) {
  721. memset(config, 0, sizeof(config));
  722. memset(new_config, 0, sizeof(new_config));
  723. if (qla81xx_get_port_config(vha, config)) {
  724. ql_log(ql_log_warn, vha, 0x701f,
  725. "Get port config failed.\n");
  726. rval = -EPERM;
  727. goto done_free_dma_rsp;
  728. }
  729. if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
  730. ql_dbg(ql_dbg_user, vha, 0x70c4,
  731. "Loopback operation already in "
  732. "progress.\n");
  733. rval = -EAGAIN;
  734. goto done_free_dma_rsp;
  735. }
  736. ql_dbg(ql_dbg_user, vha, 0x70c0,
  737. "elreq.options=%04x\n", elreq.options);
  738. if (elreq.options == EXTERNAL_LOOPBACK)
  739. if (IS_QLA8031(ha) || IS_QLA8044(ha))
  740. rval = qla81xx_set_loopback_mode(vha,
  741. config, new_config, elreq.options);
  742. else
  743. rval = qla81xx_reset_loopback_mode(vha,
  744. config, 1, 0);
  745. else
  746. rval = qla81xx_set_loopback_mode(vha, config,
  747. new_config, elreq.options);
  748. if (rval) {
  749. rval = -EPERM;
  750. goto done_free_dma_rsp;
  751. }
  752. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  753. ql_dbg(ql_dbg_user, vha, 0x7028,
  754. "BSG request type: %s.\n", type);
  755. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  756. rval = qla2x00_loopback_test(vha, &elreq, response);
  757. if (response[0] == MBS_COMMAND_ERROR &&
  758. response[1] == MBS_LB_RESET) {
  759. ql_log(ql_log_warn, vha, 0x7029,
  760. "MBX command error, Aborting ISP.\n");
  761. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  762. qla2xxx_wake_dpc(vha);
  763. qla2x00_wait_for_chip_reset(vha);
  764. /* Also reset the MPI */
  765. if (IS_QLA81XX(ha)) {
  766. if (qla81xx_restart_mpi_firmware(vha) !=
  767. QLA_SUCCESS) {
  768. ql_log(ql_log_warn, vha, 0x702a,
  769. "MPI reset failed.\n");
  770. }
  771. }
  772. rval = -EIO;
  773. goto done_free_dma_rsp;
  774. }
  775. if (new_config[0]) {
  776. int ret;
  777. /* Revert back to original port config
  778. * Also clear internal loopback
  779. */
  780. ret = qla81xx_reset_loopback_mode(vha,
  781. new_config, 0, 1);
  782. if (ret) {
  783. /*
  784. * If the reset of the loopback mode
  785. * doesn't work take FCoE dump and then
  786. * reset the chip.
  787. */
  788. ha->isp_ops->fw_dump(vha, 0);
  789. set_bit(ISP_ABORT_NEEDED,
  790. &vha->dpc_flags);
  791. }
  792. }
  793. } else {
  794. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  795. ql_dbg(ql_dbg_user, vha, 0x702b,
  796. "BSG request type: %s.\n", type);
  797. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  798. rval = qla2x00_loopback_test(vha, &elreq, response);
  799. }
  800. }
  801. if (rval) {
  802. ql_log(ql_log_warn, vha, 0x702c,
  803. "Vendor request %s failed.\n", type);
  804. rval = 0;
  805. bsg_reply->result = (DID_ERROR << 16);
  806. bsg_reply->reply_payload_rcv_len = 0;
  807. } else {
  808. ql_dbg(ql_dbg_user, vha, 0x702d,
  809. "Vendor request %s completed.\n", type);
  810. bsg_reply->result = (DID_OK << 16);
  811. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  812. bsg_job->reply_payload.sg_cnt, rsp_data,
  813. rsp_data_len);
  814. }
  815. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  816. sizeof(response) + sizeof(uint8_t);
  817. fw_sts_ptr = ((uint8_t *)scsi_req(bsg_job->req)->sense) +
  818. sizeof(struct fc_bsg_reply);
  819. memcpy(fw_sts_ptr, response, sizeof(response));
  820. fw_sts_ptr += sizeof(response);
  821. *fw_sts_ptr = command_sent;
  822. done_free_dma_rsp:
  823. dma_free_coherent(&ha->pdev->dev, rsp_data_len,
  824. rsp_data, rsp_data_dma);
  825. done_free_dma_req:
  826. dma_free_coherent(&ha->pdev->dev, req_data_len,
  827. req_data, req_data_dma);
  828. done_unmap_sg:
  829. dma_unmap_sg(&ha->pdev->dev,
  830. bsg_job->reply_payload.sg_list,
  831. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  832. done_unmap_req_sg:
  833. dma_unmap_sg(&ha->pdev->dev,
  834. bsg_job->request_payload.sg_list,
  835. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  836. if (!rval)
  837. bsg_job_done(bsg_job, bsg_reply->result,
  838. bsg_reply->reply_payload_rcv_len);
  839. return rval;
  840. }
  841. static int
  842. qla84xx_reset(struct bsg_job *bsg_job)
  843. {
  844. struct fc_bsg_request *bsg_request = bsg_job->request;
  845. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  846. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  847. scsi_qla_host_t *vha = shost_priv(host);
  848. struct qla_hw_data *ha = vha->hw;
  849. int rval = 0;
  850. uint32_t flag;
  851. if (!IS_QLA84XX(ha)) {
  852. ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
  853. return -EINVAL;
  854. }
  855. flag = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  856. rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
  857. if (rval) {
  858. ql_log(ql_log_warn, vha, 0x7030,
  859. "Vendor request 84xx reset failed.\n");
  860. rval = (DID_ERROR << 16);
  861. } else {
  862. ql_dbg(ql_dbg_user, vha, 0x7031,
  863. "Vendor request 84xx reset completed.\n");
  864. bsg_reply->result = DID_OK;
  865. bsg_job_done(bsg_job, bsg_reply->result,
  866. bsg_reply->reply_payload_rcv_len);
  867. }
  868. return rval;
  869. }
  870. static int
  871. qla84xx_updatefw(struct bsg_job *bsg_job)
  872. {
  873. struct fc_bsg_request *bsg_request = bsg_job->request;
  874. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  875. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  876. scsi_qla_host_t *vha = shost_priv(host);
  877. struct qla_hw_data *ha = vha->hw;
  878. struct verify_chip_entry_84xx *mn = NULL;
  879. dma_addr_t mn_dma, fw_dma;
  880. void *fw_buf = NULL;
  881. int rval = 0;
  882. uint32_t sg_cnt;
  883. uint32_t data_len;
  884. uint16_t options;
  885. uint32_t flag;
  886. uint32_t fw_ver;
  887. if (!IS_QLA84XX(ha)) {
  888. ql_dbg(ql_dbg_user, vha, 0x7032,
  889. "Not 84xx, exiting.\n");
  890. return -EINVAL;
  891. }
  892. sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  893. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  894. if (!sg_cnt) {
  895. ql_log(ql_log_warn, vha, 0x7033,
  896. "dma_map_sg returned %d for request.\n", sg_cnt);
  897. return -ENOMEM;
  898. }
  899. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  900. ql_log(ql_log_warn, vha, 0x7034,
  901. "DMA mapping resulted in different sg counts, "
  902. "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
  903. bsg_job->request_payload.sg_cnt, sg_cnt);
  904. rval = -EAGAIN;
  905. goto done_unmap_sg;
  906. }
  907. data_len = bsg_job->request_payload.payload_len;
  908. fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
  909. &fw_dma, GFP_KERNEL);
  910. if (!fw_buf) {
  911. ql_log(ql_log_warn, vha, 0x7035,
  912. "DMA alloc failed for fw_buf.\n");
  913. rval = -ENOMEM;
  914. goto done_unmap_sg;
  915. }
  916. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  917. bsg_job->request_payload.sg_cnt, fw_buf, data_len);
  918. mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  919. if (!mn) {
  920. ql_log(ql_log_warn, vha, 0x7036,
  921. "DMA alloc failed for fw buffer.\n");
  922. rval = -ENOMEM;
  923. goto done_free_fw_buf;
  924. }
  925. flag = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  926. fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
  927. memset(mn, 0, sizeof(struct access_chip_84xx));
  928. mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
  929. mn->entry_count = 1;
  930. options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
  931. if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
  932. options |= VCO_DIAG_FW;
  933. mn->options = cpu_to_le16(options);
  934. mn->fw_ver = cpu_to_le32(fw_ver);
  935. mn->fw_size = cpu_to_le32(data_len);
  936. mn->fw_seq_size = cpu_to_le32(data_len);
  937. mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
  938. mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
  939. mn->dseg_length = cpu_to_le32(data_len);
  940. mn->data_seg_cnt = cpu_to_le16(1);
  941. rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
  942. if (rval) {
  943. ql_log(ql_log_warn, vha, 0x7037,
  944. "Vendor request 84xx updatefw failed.\n");
  945. rval = (DID_ERROR << 16);
  946. } else {
  947. ql_dbg(ql_dbg_user, vha, 0x7038,
  948. "Vendor request 84xx updatefw completed.\n");
  949. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  950. bsg_reply->result = DID_OK;
  951. }
  952. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  953. done_free_fw_buf:
  954. dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
  955. done_unmap_sg:
  956. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  957. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  958. if (!rval)
  959. bsg_job_done(bsg_job, bsg_reply->result,
  960. bsg_reply->reply_payload_rcv_len);
  961. return rval;
  962. }
  963. static int
  964. qla84xx_mgmt_cmd(struct bsg_job *bsg_job)
  965. {
  966. struct fc_bsg_request *bsg_request = bsg_job->request;
  967. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  968. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  969. scsi_qla_host_t *vha = shost_priv(host);
  970. struct qla_hw_data *ha = vha->hw;
  971. struct access_chip_84xx *mn = NULL;
  972. dma_addr_t mn_dma, mgmt_dma;
  973. void *mgmt_b = NULL;
  974. int rval = 0;
  975. struct qla_bsg_a84_mgmt *ql84_mgmt;
  976. uint32_t sg_cnt;
  977. uint32_t data_len = 0;
  978. uint32_t dma_direction = DMA_NONE;
  979. if (!IS_QLA84XX(ha)) {
  980. ql_log(ql_log_warn, vha, 0x703a,
  981. "Not 84xx, exiting.\n");
  982. return -EINVAL;
  983. }
  984. mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  985. if (!mn) {
  986. ql_log(ql_log_warn, vha, 0x703c,
  987. "DMA alloc failed for fw buffer.\n");
  988. return -ENOMEM;
  989. }
  990. memset(mn, 0, sizeof(struct access_chip_84xx));
  991. mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
  992. mn->entry_count = 1;
  993. ql84_mgmt = (void *)bsg_request + sizeof(struct fc_bsg_request);
  994. switch (ql84_mgmt->mgmt.cmd) {
  995. case QLA84_MGMT_READ_MEM:
  996. case QLA84_MGMT_GET_INFO:
  997. sg_cnt = dma_map_sg(&ha->pdev->dev,
  998. bsg_job->reply_payload.sg_list,
  999. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1000. if (!sg_cnt) {
  1001. ql_log(ql_log_warn, vha, 0x703d,
  1002. "dma_map_sg returned %d for reply.\n", sg_cnt);
  1003. rval = -ENOMEM;
  1004. goto exit_mgmt;
  1005. }
  1006. dma_direction = DMA_FROM_DEVICE;
  1007. if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
  1008. ql_log(ql_log_warn, vha, 0x703e,
  1009. "DMA mapping resulted in different sg counts, "
  1010. "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
  1011. bsg_job->reply_payload.sg_cnt, sg_cnt);
  1012. rval = -EAGAIN;
  1013. goto done_unmap_sg;
  1014. }
  1015. data_len = bsg_job->reply_payload.payload_len;
  1016. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  1017. &mgmt_dma, GFP_KERNEL);
  1018. if (!mgmt_b) {
  1019. ql_log(ql_log_warn, vha, 0x703f,
  1020. "DMA alloc failed for mgmt_b.\n");
  1021. rval = -ENOMEM;
  1022. goto done_unmap_sg;
  1023. }
  1024. if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
  1025. mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
  1026. mn->parameter1 =
  1027. cpu_to_le32(
  1028. ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  1029. } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
  1030. mn->options = cpu_to_le16(ACO_REQUEST_INFO);
  1031. mn->parameter1 =
  1032. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
  1033. mn->parameter2 =
  1034. cpu_to_le32(
  1035. ql84_mgmt->mgmt.mgmtp.u.info.context);
  1036. }
  1037. break;
  1038. case QLA84_MGMT_WRITE_MEM:
  1039. sg_cnt = dma_map_sg(&ha->pdev->dev,
  1040. bsg_job->request_payload.sg_list,
  1041. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1042. if (!sg_cnt) {
  1043. ql_log(ql_log_warn, vha, 0x7040,
  1044. "dma_map_sg returned %d.\n", sg_cnt);
  1045. rval = -ENOMEM;
  1046. goto exit_mgmt;
  1047. }
  1048. dma_direction = DMA_TO_DEVICE;
  1049. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  1050. ql_log(ql_log_warn, vha, 0x7041,
  1051. "DMA mapping resulted in different sg counts, "
  1052. "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
  1053. bsg_job->request_payload.sg_cnt, sg_cnt);
  1054. rval = -EAGAIN;
  1055. goto done_unmap_sg;
  1056. }
  1057. data_len = bsg_job->request_payload.payload_len;
  1058. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  1059. &mgmt_dma, GFP_KERNEL);
  1060. if (!mgmt_b) {
  1061. ql_log(ql_log_warn, vha, 0x7042,
  1062. "DMA alloc failed for mgmt_b.\n");
  1063. rval = -ENOMEM;
  1064. goto done_unmap_sg;
  1065. }
  1066. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1067. bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
  1068. mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
  1069. mn->parameter1 =
  1070. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  1071. break;
  1072. case QLA84_MGMT_CHNG_CONFIG:
  1073. mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
  1074. mn->parameter1 =
  1075. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
  1076. mn->parameter2 =
  1077. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
  1078. mn->parameter3 =
  1079. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
  1080. break;
  1081. default:
  1082. rval = -EIO;
  1083. goto exit_mgmt;
  1084. }
  1085. if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
  1086. mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
  1087. mn->dseg_count = cpu_to_le16(1);
  1088. mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
  1089. mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
  1090. mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
  1091. }
  1092. rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
  1093. if (rval) {
  1094. ql_log(ql_log_warn, vha, 0x7043,
  1095. "Vendor request 84xx mgmt failed.\n");
  1096. rval = (DID_ERROR << 16);
  1097. } else {
  1098. ql_dbg(ql_dbg_user, vha, 0x7044,
  1099. "Vendor request 84xx mgmt completed.\n");
  1100. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1101. bsg_reply->result = DID_OK;
  1102. if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
  1103. (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
  1104. bsg_reply->reply_payload_rcv_len =
  1105. bsg_job->reply_payload.payload_len;
  1106. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1107. bsg_job->reply_payload.sg_cnt, mgmt_b,
  1108. data_len);
  1109. }
  1110. }
  1111. done_unmap_sg:
  1112. if (mgmt_b)
  1113. dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
  1114. if (dma_direction == DMA_TO_DEVICE)
  1115. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  1116. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1117. else if (dma_direction == DMA_FROM_DEVICE)
  1118. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  1119. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1120. exit_mgmt:
  1121. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  1122. if (!rval)
  1123. bsg_job_done(bsg_job, bsg_reply->result,
  1124. bsg_reply->reply_payload_rcv_len);
  1125. return rval;
  1126. }
  1127. static int
  1128. qla24xx_iidma(struct bsg_job *bsg_job)
  1129. {
  1130. struct fc_bsg_request *bsg_request = bsg_job->request;
  1131. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1132. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1133. scsi_qla_host_t *vha = shost_priv(host);
  1134. int rval = 0;
  1135. struct qla_port_param *port_param = NULL;
  1136. fc_port_t *fcport = NULL;
  1137. int found = 0;
  1138. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1139. uint8_t *rsp_ptr = NULL;
  1140. if (!IS_IIDMA_CAPABLE(vha->hw)) {
  1141. ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
  1142. return -EINVAL;
  1143. }
  1144. port_param = (void *)bsg_request + sizeof(struct fc_bsg_request);
  1145. if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
  1146. ql_log(ql_log_warn, vha, 0x7048,
  1147. "Invalid destination type.\n");
  1148. return -EINVAL;
  1149. }
  1150. list_for_each_entry(fcport, &vha->vp_fcports, list) {
  1151. if (fcport->port_type != FCT_TARGET)
  1152. continue;
  1153. if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
  1154. fcport->port_name, sizeof(fcport->port_name)))
  1155. continue;
  1156. found = 1;
  1157. break;
  1158. }
  1159. if (!found) {
  1160. ql_log(ql_log_warn, vha, 0x7049,
  1161. "Failed to find port.\n");
  1162. return -EINVAL;
  1163. }
  1164. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  1165. ql_log(ql_log_warn, vha, 0x704a,
  1166. "Port is not online.\n");
  1167. return -EINVAL;
  1168. }
  1169. if (fcport->flags & FCF_LOGIN_NEEDED) {
  1170. ql_log(ql_log_warn, vha, 0x704b,
  1171. "Remote port not logged in flags = 0x%x.\n", fcport->flags);
  1172. return -EINVAL;
  1173. }
  1174. if (port_param->mode)
  1175. rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
  1176. port_param->speed, mb);
  1177. else
  1178. rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
  1179. &port_param->speed, mb);
  1180. if (rval) {
  1181. ql_log(ql_log_warn, vha, 0x704c,
  1182. "iIDMA cmd failed for %8phN -- "
  1183. "%04x %x %04x %04x.\n", fcport->port_name,
  1184. rval, fcport->fp_speed, mb[0], mb[1]);
  1185. rval = (DID_ERROR << 16);
  1186. } else {
  1187. if (!port_param->mode) {
  1188. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  1189. sizeof(struct qla_port_param);
  1190. rsp_ptr = ((uint8_t *)bsg_reply) +
  1191. sizeof(struct fc_bsg_reply);
  1192. memcpy(rsp_ptr, port_param,
  1193. sizeof(struct qla_port_param));
  1194. }
  1195. bsg_reply->result = DID_OK;
  1196. bsg_job_done(bsg_job, bsg_reply->result,
  1197. bsg_reply->reply_payload_rcv_len);
  1198. }
  1199. return rval;
  1200. }
  1201. static int
  1202. qla2x00_optrom_setup(struct bsg_job *bsg_job, scsi_qla_host_t *vha,
  1203. uint8_t is_update)
  1204. {
  1205. struct fc_bsg_request *bsg_request = bsg_job->request;
  1206. uint32_t start = 0;
  1207. int valid = 0;
  1208. struct qla_hw_data *ha = vha->hw;
  1209. if (unlikely(pci_channel_offline(ha->pdev)))
  1210. return -EINVAL;
  1211. start = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  1212. if (start > ha->optrom_size) {
  1213. ql_log(ql_log_warn, vha, 0x7055,
  1214. "start %d > optrom_size %d.\n", start, ha->optrom_size);
  1215. return -EINVAL;
  1216. }
  1217. if (ha->optrom_state != QLA_SWAITING) {
  1218. ql_log(ql_log_info, vha, 0x7056,
  1219. "optrom_state %d.\n", ha->optrom_state);
  1220. return -EBUSY;
  1221. }
  1222. ha->optrom_region_start = start;
  1223. ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
  1224. if (is_update) {
  1225. if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
  1226. valid = 1;
  1227. else if (start == (ha->flt_region_boot * 4) ||
  1228. start == (ha->flt_region_fw * 4))
  1229. valid = 1;
  1230. else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
  1231. IS_CNA_CAPABLE(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha))
  1232. valid = 1;
  1233. if (!valid) {
  1234. ql_log(ql_log_warn, vha, 0x7058,
  1235. "Invalid start region 0x%x/0x%x.\n", start,
  1236. bsg_job->request_payload.payload_len);
  1237. return -EINVAL;
  1238. }
  1239. ha->optrom_region_size = start +
  1240. bsg_job->request_payload.payload_len > ha->optrom_size ?
  1241. ha->optrom_size - start :
  1242. bsg_job->request_payload.payload_len;
  1243. ha->optrom_state = QLA_SWRITING;
  1244. } else {
  1245. ha->optrom_region_size = start +
  1246. bsg_job->reply_payload.payload_len > ha->optrom_size ?
  1247. ha->optrom_size - start :
  1248. bsg_job->reply_payload.payload_len;
  1249. ha->optrom_state = QLA_SREADING;
  1250. }
  1251. ha->optrom_buffer = vmalloc(ha->optrom_region_size);
  1252. if (!ha->optrom_buffer) {
  1253. ql_log(ql_log_warn, vha, 0x7059,
  1254. "Read: Unable to allocate memory for optrom retrieval "
  1255. "(%x)\n", ha->optrom_region_size);
  1256. ha->optrom_state = QLA_SWAITING;
  1257. return -ENOMEM;
  1258. }
  1259. memset(ha->optrom_buffer, 0, ha->optrom_region_size);
  1260. return 0;
  1261. }
  1262. static int
  1263. qla2x00_read_optrom(struct bsg_job *bsg_job)
  1264. {
  1265. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1266. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1267. scsi_qla_host_t *vha = shost_priv(host);
  1268. struct qla_hw_data *ha = vha->hw;
  1269. int rval = 0;
  1270. if (ha->flags.nic_core_reset_hdlr_active)
  1271. return -EBUSY;
  1272. mutex_lock(&ha->optrom_mutex);
  1273. rval = qla2x00_optrom_setup(bsg_job, vha, 0);
  1274. if (rval) {
  1275. mutex_unlock(&ha->optrom_mutex);
  1276. return rval;
  1277. }
  1278. ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
  1279. ha->optrom_region_start, ha->optrom_region_size);
  1280. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1281. bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
  1282. ha->optrom_region_size);
  1283. bsg_reply->reply_payload_rcv_len = ha->optrom_region_size;
  1284. bsg_reply->result = DID_OK;
  1285. vfree(ha->optrom_buffer);
  1286. ha->optrom_buffer = NULL;
  1287. ha->optrom_state = QLA_SWAITING;
  1288. mutex_unlock(&ha->optrom_mutex);
  1289. bsg_job_done(bsg_job, bsg_reply->result,
  1290. bsg_reply->reply_payload_rcv_len);
  1291. return rval;
  1292. }
  1293. static int
  1294. qla2x00_update_optrom(struct bsg_job *bsg_job)
  1295. {
  1296. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1297. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1298. scsi_qla_host_t *vha = shost_priv(host);
  1299. struct qla_hw_data *ha = vha->hw;
  1300. int rval = 0;
  1301. mutex_lock(&ha->optrom_mutex);
  1302. rval = qla2x00_optrom_setup(bsg_job, vha, 1);
  1303. if (rval) {
  1304. mutex_unlock(&ha->optrom_mutex);
  1305. return rval;
  1306. }
  1307. /* Set the isp82xx_no_md_cap not to capture minidump */
  1308. ha->flags.isp82xx_no_md_cap = 1;
  1309. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1310. bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
  1311. ha->optrom_region_size);
  1312. ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
  1313. ha->optrom_region_start, ha->optrom_region_size);
  1314. bsg_reply->result = DID_OK;
  1315. vfree(ha->optrom_buffer);
  1316. ha->optrom_buffer = NULL;
  1317. ha->optrom_state = QLA_SWAITING;
  1318. mutex_unlock(&ha->optrom_mutex);
  1319. bsg_job_done(bsg_job, bsg_reply->result,
  1320. bsg_reply->reply_payload_rcv_len);
  1321. return rval;
  1322. }
  1323. static int
  1324. qla2x00_update_fru_versions(struct bsg_job *bsg_job)
  1325. {
  1326. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1327. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1328. scsi_qla_host_t *vha = shost_priv(host);
  1329. struct qla_hw_data *ha = vha->hw;
  1330. int rval = 0;
  1331. uint8_t bsg[DMA_POOL_SIZE];
  1332. struct qla_image_version_list *list = (void *)bsg;
  1333. struct qla_image_version *image;
  1334. uint32_t count;
  1335. dma_addr_t sfp_dma;
  1336. void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1337. if (!sfp) {
  1338. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1339. EXT_STATUS_NO_MEMORY;
  1340. goto done;
  1341. }
  1342. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1343. bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
  1344. image = list->version;
  1345. count = list->count;
  1346. while (count--) {
  1347. memcpy(sfp, &image->field_info, sizeof(image->field_info));
  1348. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1349. image->field_address.device, image->field_address.offset,
  1350. sizeof(image->field_info), image->field_address.option);
  1351. if (rval) {
  1352. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1353. EXT_STATUS_MAILBOX;
  1354. goto dealloc;
  1355. }
  1356. image++;
  1357. }
  1358. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1359. dealloc:
  1360. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1361. done:
  1362. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1363. bsg_reply->result = DID_OK << 16;
  1364. bsg_job_done(bsg_job, bsg_reply->result,
  1365. bsg_reply->reply_payload_rcv_len);
  1366. return 0;
  1367. }
  1368. static int
  1369. qla2x00_read_fru_status(struct bsg_job *bsg_job)
  1370. {
  1371. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1372. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1373. scsi_qla_host_t *vha = shost_priv(host);
  1374. struct qla_hw_data *ha = vha->hw;
  1375. int rval = 0;
  1376. uint8_t bsg[DMA_POOL_SIZE];
  1377. struct qla_status_reg *sr = (void *)bsg;
  1378. dma_addr_t sfp_dma;
  1379. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1380. if (!sfp) {
  1381. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1382. EXT_STATUS_NO_MEMORY;
  1383. goto done;
  1384. }
  1385. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1386. bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
  1387. rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
  1388. sr->field_address.device, sr->field_address.offset,
  1389. sizeof(sr->status_reg), sr->field_address.option);
  1390. sr->status_reg = *sfp;
  1391. if (rval) {
  1392. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1393. EXT_STATUS_MAILBOX;
  1394. goto dealloc;
  1395. }
  1396. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1397. bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
  1398. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1399. dealloc:
  1400. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1401. done:
  1402. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1403. bsg_reply->reply_payload_rcv_len = sizeof(*sr);
  1404. bsg_reply->result = DID_OK << 16;
  1405. bsg_job_done(bsg_job, bsg_reply->result,
  1406. bsg_reply->reply_payload_rcv_len);
  1407. return 0;
  1408. }
  1409. static int
  1410. qla2x00_write_fru_status(struct bsg_job *bsg_job)
  1411. {
  1412. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1413. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1414. scsi_qla_host_t *vha = shost_priv(host);
  1415. struct qla_hw_data *ha = vha->hw;
  1416. int rval = 0;
  1417. uint8_t bsg[DMA_POOL_SIZE];
  1418. struct qla_status_reg *sr = (void *)bsg;
  1419. dma_addr_t sfp_dma;
  1420. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1421. if (!sfp) {
  1422. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1423. EXT_STATUS_NO_MEMORY;
  1424. goto done;
  1425. }
  1426. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1427. bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
  1428. *sfp = sr->status_reg;
  1429. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1430. sr->field_address.device, sr->field_address.offset,
  1431. sizeof(sr->status_reg), sr->field_address.option);
  1432. if (rval) {
  1433. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1434. EXT_STATUS_MAILBOX;
  1435. goto dealloc;
  1436. }
  1437. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1438. dealloc:
  1439. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1440. done:
  1441. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1442. bsg_reply->result = DID_OK << 16;
  1443. bsg_job_done(bsg_job, bsg_reply->result,
  1444. bsg_reply->reply_payload_rcv_len);
  1445. return 0;
  1446. }
  1447. static int
  1448. qla2x00_write_i2c(struct bsg_job *bsg_job)
  1449. {
  1450. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1451. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1452. scsi_qla_host_t *vha = shost_priv(host);
  1453. struct qla_hw_data *ha = vha->hw;
  1454. int rval = 0;
  1455. uint8_t bsg[DMA_POOL_SIZE];
  1456. struct qla_i2c_access *i2c = (void *)bsg;
  1457. dma_addr_t sfp_dma;
  1458. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1459. if (!sfp) {
  1460. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1461. EXT_STATUS_NO_MEMORY;
  1462. goto done;
  1463. }
  1464. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1465. bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
  1466. memcpy(sfp, i2c->buffer, i2c->length);
  1467. rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
  1468. i2c->device, i2c->offset, i2c->length, i2c->option);
  1469. if (rval) {
  1470. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1471. EXT_STATUS_MAILBOX;
  1472. goto dealloc;
  1473. }
  1474. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1475. dealloc:
  1476. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1477. done:
  1478. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1479. bsg_reply->result = DID_OK << 16;
  1480. bsg_job_done(bsg_job, bsg_reply->result,
  1481. bsg_reply->reply_payload_rcv_len);
  1482. return 0;
  1483. }
  1484. static int
  1485. qla2x00_read_i2c(struct bsg_job *bsg_job)
  1486. {
  1487. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1488. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1489. scsi_qla_host_t *vha = shost_priv(host);
  1490. struct qla_hw_data *ha = vha->hw;
  1491. int rval = 0;
  1492. uint8_t bsg[DMA_POOL_SIZE];
  1493. struct qla_i2c_access *i2c = (void *)bsg;
  1494. dma_addr_t sfp_dma;
  1495. uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
  1496. if (!sfp) {
  1497. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1498. EXT_STATUS_NO_MEMORY;
  1499. goto done;
  1500. }
  1501. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1502. bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
  1503. rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
  1504. i2c->device, i2c->offset, i2c->length, i2c->option);
  1505. if (rval) {
  1506. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1507. EXT_STATUS_MAILBOX;
  1508. goto dealloc;
  1509. }
  1510. memcpy(i2c->buffer, sfp, i2c->length);
  1511. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1512. bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
  1513. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
  1514. dealloc:
  1515. dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
  1516. done:
  1517. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1518. bsg_reply->reply_payload_rcv_len = sizeof(*i2c);
  1519. bsg_reply->result = DID_OK << 16;
  1520. bsg_job_done(bsg_job, bsg_reply->result,
  1521. bsg_reply->reply_payload_rcv_len);
  1522. return 0;
  1523. }
  1524. static int
  1525. qla24xx_process_bidir_cmd(struct bsg_job *bsg_job)
  1526. {
  1527. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1528. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1529. scsi_qla_host_t *vha = shost_priv(host);
  1530. struct qla_hw_data *ha = vha->hw;
  1531. uint32_t rval = EXT_STATUS_OK;
  1532. uint16_t req_sg_cnt = 0;
  1533. uint16_t rsp_sg_cnt = 0;
  1534. uint16_t nextlid = 0;
  1535. uint32_t tot_dsds;
  1536. srb_t *sp = NULL;
  1537. uint32_t req_data_len = 0;
  1538. uint32_t rsp_data_len = 0;
  1539. /* Check the type of the adapter */
  1540. if (!IS_BIDI_CAPABLE(ha)) {
  1541. ql_log(ql_log_warn, vha, 0x70a0,
  1542. "This adapter is not supported\n");
  1543. rval = EXT_STATUS_NOT_SUPPORTED;
  1544. goto done;
  1545. }
  1546. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  1547. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  1548. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
  1549. rval = EXT_STATUS_BUSY;
  1550. goto done;
  1551. }
  1552. /* Check if host is online */
  1553. if (!vha->flags.online) {
  1554. ql_log(ql_log_warn, vha, 0x70a1,
  1555. "Host is not online\n");
  1556. rval = EXT_STATUS_DEVICE_OFFLINE;
  1557. goto done;
  1558. }
  1559. /* Check if cable is plugged in or not */
  1560. if (vha->device_flags & DFLG_NO_CABLE) {
  1561. ql_log(ql_log_warn, vha, 0x70a2,
  1562. "Cable is unplugged...\n");
  1563. rval = EXT_STATUS_INVALID_CFG;
  1564. goto done;
  1565. }
  1566. /* Check if the switch is connected or not */
  1567. if (ha->current_topology != ISP_CFG_F) {
  1568. ql_log(ql_log_warn, vha, 0x70a3,
  1569. "Host is not connected to the switch\n");
  1570. rval = EXT_STATUS_INVALID_CFG;
  1571. goto done;
  1572. }
  1573. /* Check if operating mode is P2P */
  1574. if (ha->operating_mode != P2P) {
  1575. ql_log(ql_log_warn, vha, 0x70a4,
  1576. "Host is operating mode is not P2p\n");
  1577. rval = EXT_STATUS_INVALID_CFG;
  1578. goto done;
  1579. }
  1580. mutex_lock(&ha->selflogin_lock);
  1581. if (vha->self_login_loop_id == 0) {
  1582. /* Initialize all required fields of fcport */
  1583. vha->bidir_fcport.vha = vha;
  1584. vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
  1585. vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
  1586. vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
  1587. vha->bidir_fcport.loop_id = vha->loop_id;
  1588. if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
  1589. ql_log(ql_log_warn, vha, 0x70a7,
  1590. "Failed to login port %06X for bidirectional IOCB\n",
  1591. vha->bidir_fcport.d_id.b24);
  1592. mutex_unlock(&ha->selflogin_lock);
  1593. rval = EXT_STATUS_MAILBOX;
  1594. goto done;
  1595. }
  1596. vha->self_login_loop_id = nextlid - 1;
  1597. }
  1598. /* Assign the self login loop id to fcport */
  1599. mutex_unlock(&ha->selflogin_lock);
  1600. vha->bidir_fcport.loop_id = vha->self_login_loop_id;
  1601. req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1602. bsg_job->request_payload.sg_list,
  1603. bsg_job->request_payload.sg_cnt,
  1604. DMA_TO_DEVICE);
  1605. if (!req_sg_cnt) {
  1606. rval = EXT_STATUS_NO_MEMORY;
  1607. goto done;
  1608. }
  1609. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1610. bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
  1611. DMA_FROM_DEVICE);
  1612. if (!rsp_sg_cnt) {
  1613. rval = EXT_STATUS_NO_MEMORY;
  1614. goto done_unmap_req_sg;
  1615. }
  1616. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  1617. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  1618. ql_dbg(ql_dbg_user, vha, 0x70a9,
  1619. "Dma mapping resulted in different sg counts "
  1620. "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
  1621. "%x dma_reply_sg_cnt: %x]\n",
  1622. bsg_job->request_payload.sg_cnt, req_sg_cnt,
  1623. bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  1624. rval = EXT_STATUS_NO_MEMORY;
  1625. goto done_unmap_sg;
  1626. }
  1627. if (req_data_len != rsp_data_len) {
  1628. rval = EXT_STATUS_BUSY;
  1629. ql_log(ql_log_warn, vha, 0x70aa,
  1630. "req_data_len != rsp_data_len\n");
  1631. goto done_unmap_sg;
  1632. }
  1633. req_data_len = bsg_job->request_payload.payload_len;
  1634. rsp_data_len = bsg_job->reply_payload.payload_len;
  1635. /* Alloc SRB structure */
  1636. sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
  1637. if (!sp) {
  1638. ql_dbg(ql_dbg_user, vha, 0x70ac,
  1639. "Alloc SRB structure failed\n");
  1640. rval = EXT_STATUS_NO_MEMORY;
  1641. goto done_unmap_sg;
  1642. }
  1643. /*Populate srb->ctx with bidir ctx*/
  1644. sp->u.bsg_job = bsg_job;
  1645. sp->free = qla2x00_bsg_sp_free;
  1646. sp->type = SRB_BIDI_CMD;
  1647. sp->done = qla2x00_bsg_job_done;
  1648. /* Add the read and write sg count */
  1649. tot_dsds = rsp_sg_cnt + req_sg_cnt;
  1650. rval = qla2x00_start_bidir(sp, vha, tot_dsds);
  1651. if (rval != EXT_STATUS_OK)
  1652. goto done_free_srb;
  1653. /* the bsg request will be completed in the interrupt handler */
  1654. return rval;
  1655. done_free_srb:
  1656. mempool_free(sp, ha->srb_mempool);
  1657. done_unmap_sg:
  1658. dma_unmap_sg(&ha->pdev->dev,
  1659. bsg_job->reply_payload.sg_list,
  1660. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1661. done_unmap_req_sg:
  1662. dma_unmap_sg(&ha->pdev->dev,
  1663. bsg_job->request_payload.sg_list,
  1664. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1665. done:
  1666. /* Return an error vendor specific response
  1667. * and complete the bsg request
  1668. */
  1669. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
  1670. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1671. bsg_reply->reply_payload_rcv_len = 0;
  1672. bsg_reply->result = (DID_OK) << 16;
  1673. bsg_job_done(bsg_job, bsg_reply->result,
  1674. bsg_reply->reply_payload_rcv_len);
  1675. /* Always return success, vendor rsp carries correct status */
  1676. return 0;
  1677. }
  1678. static int
  1679. qlafx00_mgmt_cmd(struct bsg_job *bsg_job)
  1680. {
  1681. struct fc_bsg_request *bsg_request = bsg_job->request;
  1682. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1683. scsi_qla_host_t *vha = shost_priv(host);
  1684. struct qla_hw_data *ha = vha->hw;
  1685. int rval = (DRIVER_ERROR << 16);
  1686. struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
  1687. srb_t *sp;
  1688. int req_sg_cnt = 0, rsp_sg_cnt = 0;
  1689. struct fc_port *fcport;
  1690. char *type = "FC_BSG_HST_FX_MGMT";
  1691. /* Copy the IOCB specific information */
  1692. piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
  1693. &bsg_request->rqst_data.h_vendor.vendor_cmd[1];
  1694. /* Dump the vendor information */
  1695. ql_dump_buffer(ql_dbg_user + ql_dbg_verbose , vha, 0x70cf,
  1696. (uint8_t *)piocb_rqst, sizeof(struct qla_mt_iocb_rqst_fx00));
  1697. if (!vha->flags.online) {
  1698. ql_log(ql_log_warn, vha, 0x70d0,
  1699. "Host is not online.\n");
  1700. rval = -EIO;
  1701. goto done;
  1702. }
  1703. if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID) {
  1704. req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1705. bsg_job->request_payload.sg_list,
  1706. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1707. if (!req_sg_cnt) {
  1708. ql_log(ql_log_warn, vha, 0x70c7,
  1709. "dma_map_sg return %d for request\n", req_sg_cnt);
  1710. rval = -ENOMEM;
  1711. goto done;
  1712. }
  1713. }
  1714. if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID) {
  1715. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  1716. bsg_job->reply_payload.sg_list,
  1717. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1718. if (!rsp_sg_cnt) {
  1719. ql_log(ql_log_warn, vha, 0x70c8,
  1720. "dma_map_sg return %d for reply\n", rsp_sg_cnt);
  1721. rval = -ENOMEM;
  1722. goto done_unmap_req_sg;
  1723. }
  1724. }
  1725. ql_dbg(ql_dbg_user, vha, 0x70c9,
  1726. "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
  1727. "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
  1728. req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
  1729. /* Allocate a dummy fcport structure, since functions preparing the
  1730. * IOCB and mailbox command retrieves port specific information
  1731. * from fcport structure. For Host based ELS commands there will be
  1732. * no fcport structure allocated
  1733. */
  1734. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  1735. if (!fcport) {
  1736. ql_log(ql_log_warn, vha, 0x70ca,
  1737. "Failed to allocate fcport.\n");
  1738. rval = -ENOMEM;
  1739. goto done_unmap_rsp_sg;
  1740. }
  1741. /* Alloc SRB structure */
  1742. sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
  1743. if (!sp) {
  1744. ql_log(ql_log_warn, vha, 0x70cb,
  1745. "qla2x00_get_sp failed.\n");
  1746. rval = -ENOMEM;
  1747. goto done_free_fcport;
  1748. }
  1749. /* Initialize all required fields of fcport */
  1750. fcport->vha = vha;
  1751. fcport->loop_id = piocb_rqst->dataword;
  1752. sp->type = SRB_FXIOCB_BCMD;
  1753. sp->name = "bsg_fx_mgmt";
  1754. sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
  1755. sp->u.bsg_job = bsg_job;
  1756. sp->free = qla2x00_bsg_sp_free;
  1757. sp->done = qla2x00_bsg_job_done;
  1758. ql_dbg(ql_dbg_user, vha, 0x70cc,
  1759. "bsg rqst type: %s fx_mgmt_type: %x id=%x\n",
  1760. type, piocb_rqst->func_type, fcport->loop_id);
  1761. rval = qla2x00_start_sp(sp);
  1762. if (rval != QLA_SUCCESS) {
  1763. ql_log(ql_log_warn, vha, 0x70cd,
  1764. "qla2x00_start_sp failed=%d.\n", rval);
  1765. mempool_free(sp, ha->srb_mempool);
  1766. rval = -EIO;
  1767. goto done_free_fcport;
  1768. }
  1769. return rval;
  1770. done_free_fcport:
  1771. kfree(fcport);
  1772. done_unmap_rsp_sg:
  1773. if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
  1774. dma_unmap_sg(&ha->pdev->dev,
  1775. bsg_job->reply_payload.sg_list,
  1776. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1777. done_unmap_req_sg:
  1778. if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
  1779. dma_unmap_sg(&ha->pdev->dev,
  1780. bsg_job->request_payload.sg_list,
  1781. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1782. done:
  1783. return rval;
  1784. }
  1785. static int
  1786. qla26xx_serdes_op(struct bsg_job *bsg_job)
  1787. {
  1788. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1789. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1790. scsi_qla_host_t *vha = shost_priv(host);
  1791. int rval = 0;
  1792. struct qla_serdes_reg sr;
  1793. memset(&sr, 0, sizeof(sr));
  1794. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1795. bsg_job->request_payload.sg_cnt, &sr, sizeof(sr));
  1796. switch (sr.cmd) {
  1797. case INT_SC_SERDES_WRITE_REG:
  1798. rval = qla2x00_write_serdes_word(vha, sr.addr, sr.val);
  1799. bsg_reply->reply_payload_rcv_len = 0;
  1800. break;
  1801. case INT_SC_SERDES_READ_REG:
  1802. rval = qla2x00_read_serdes_word(vha, sr.addr, &sr.val);
  1803. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1804. bsg_job->reply_payload.sg_cnt, &sr, sizeof(sr));
  1805. bsg_reply->reply_payload_rcv_len = sizeof(sr);
  1806. break;
  1807. default:
  1808. ql_dbg(ql_dbg_user, vha, 0x708c,
  1809. "Unknown serdes cmd %x.\n", sr.cmd);
  1810. rval = -EINVAL;
  1811. break;
  1812. }
  1813. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1814. rval ? EXT_STATUS_MAILBOX : 0;
  1815. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1816. bsg_reply->result = DID_OK << 16;
  1817. bsg_job_done(bsg_job, bsg_reply->result,
  1818. bsg_reply->reply_payload_rcv_len);
  1819. return 0;
  1820. }
  1821. static int
  1822. qla8044_serdes_op(struct bsg_job *bsg_job)
  1823. {
  1824. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1825. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1826. scsi_qla_host_t *vha = shost_priv(host);
  1827. int rval = 0;
  1828. struct qla_serdes_reg_ex sr;
  1829. memset(&sr, 0, sizeof(sr));
  1830. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1831. bsg_job->request_payload.sg_cnt, &sr, sizeof(sr));
  1832. switch (sr.cmd) {
  1833. case INT_SC_SERDES_WRITE_REG:
  1834. rval = qla8044_write_serdes_word(vha, sr.addr, sr.val);
  1835. bsg_reply->reply_payload_rcv_len = 0;
  1836. break;
  1837. case INT_SC_SERDES_READ_REG:
  1838. rval = qla8044_read_serdes_word(vha, sr.addr, &sr.val);
  1839. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1840. bsg_job->reply_payload.sg_cnt, &sr, sizeof(sr));
  1841. bsg_reply->reply_payload_rcv_len = sizeof(sr);
  1842. break;
  1843. default:
  1844. ql_dbg(ql_dbg_user, vha, 0x70cf,
  1845. "Unknown serdes cmd %x.\n", sr.cmd);
  1846. rval = -EINVAL;
  1847. break;
  1848. }
  1849. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1850. rval ? EXT_STATUS_MAILBOX : 0;
  1851. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1852. bsg_reply->result = DID_OK << 16;
  1853. bsg_job_done(bsg_job, bsg_reply->result,
  1854. bsg_reply->reply_payload_rcv_len);
  1855. return 0;
  1856. }
  1857. static int
  1858. qla27xx_get_flash_upd_cap(struct bsg_job *bsg_job)
  1859. {
  1860. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1861. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1862. scsi_qla_host_t *vha = shost_priv(host);
  1863. struct qla_hw_data *ha = vha->hw;
  1864. struct qla_flash_update_caps cap;
  1865. if (!(IS_QLA27XX(ha)))
  1866. return -EPERM;
  1867. memset(&cap, 0, sizeof(cap));
  1868. cap.capabilities = (uint64_t)ha->fw_attributes_ext[1] << 48 |
  1869. (uint64_t)ha->fw_attributes_ext[0] << 32 |
  1870. (uint64_t)ha->fw_attributes_h << 16 |
  1871. (uint64_t)ha->fw_attributes;
  1872. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1873. bsg_job->reply_payload.sg_cnt, &cap, sizeof(cap));
  1874. bsg_reply->reply_payload_rcv_len = sizeof(cap);
  1875. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1876. EXT_STATUS_OK;
  1877. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1878. bsg_reply->result = DID_OK << 16;
  1879. bsg_job_done(bsg_job, bsg_reply->result,
  1880. bsg_reply->reply_payload_rcv_len);
  1881. return 0;
  1882. }
  1883. static int
  1884. qla27xx_set_flash_upd_cap(struct bsg_job *bsg_job)
  1885. {
  1886. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1887. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1888. scsi_qla_host_t *vha = shost_priv(host);
  1889. struct qla_hw_data *ha = vha->hw;
  1890. uint64_t online_fw_attr = 0;
  1891. struct qla_flash_update_caps cap;
  1892. if (!(IS_QLA27XX(ha)))
  1893. return -EPERM;
  1894. memset(&cap, 0, sizeof(cap));
  1895. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1896. bsg_job->request_payload.sg_cnt, &cap, sizeof(cap));
  1897. online_fw_attr = (uint64_t)ha->fw_attributes_ext[1] << 48 |
  1898. (uint64_t)ha->fw_attributes_ext[0] << 32 |
  1899. (uint64_t)ha->fw_attributes_h << 16 |
  1900. (uint64_t)ha->fw_attributes;
  1901. if (online_fw_attr != cap.capabilities) {
  1902. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1903. EXT_STATUS_INVALID_PARAM;
  1904. return -EINVAL;
  1905. }
  1906. if (cap.outage_duration < MAX_LOOP_TIMEOUT) {
  1907. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1908. EXT_STATUS_INVALID_PARAM;
  1909. return -EINVAL;
  1910. }
  1911. bsg_reply->reply_payload_rcv_len = 0;
  1912. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  1913. EXT_STATUS_OK;
  1914. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1915. bsg_reply->result = DID_OK << 16;
  1916. bsg_job_done(bsg_job, bsg_reply->result,
  1917. bsg_reply->reply_payload_rcv_len);
  1918. return 0;
  1919. }
  1920. static int
  1921. qla27xx_get_bbcr_data(struct bsg_job *bsg_job)
  1922. {
  1923. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1924. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1925. scsi_qla_host_t *vha = shost_priv(host);
  1926. struct qla_hw_data *ha = vha->hw;
  1927. struct qla_bbcr_data bbcr;
  1928. uint16_t loop_id, topo, sw_cap;
  1929. uint8_t domain, area, al_pa, state;
  1930. int rval;
  1931. if (!(IS_QLA27XX(ha)))
  1932. return -EPERM;
  1933. memset(&bbcr, 0, sizeof(bbcr));
  1934. if (vha->flags.bbcr_enable)
  1935. bbcr.status = QLA_BBCR_STATUS_ENABLED;
  1936. else
  1937. bbcr.status = QLA_BBCR_STATUS_DISABLED;
  1938. if (bbcr.status == QLA_BBCR_STATUS_ENABLED) {
  1939. rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
  1940. &area, &domain, &topo, &sw_cap);
  1941. if (rval != QLA_SUCCESS) {
  1942. bbcr.status = QLA_BBCR_STATUS_UNKNOWN;
  1943. bbcr.state = QLA_BBCR_STATE_OFFLINE;
  1944. bbcr.mbx1 = loop_id;
  1945. goto done;
  1946. }
  1947. state = (vha->bbcr >> 12) & 0x1;
  1948. if (state) {
  1949. bbcr.state = QLA_BBCR_STATE_OFFLINE;
  1950. bbcr.offline_reason_code = QLA_BBCR_REASON_LOGIN_REJECT;
  1951. } else {
  1952. bbcr.state = QLA_BBCR_STATE_ONLINE;
  1953. bbcr.negotiated_bbscn = (vha->bbcr >> 8) & 0xf;
  1954. }
  1955. bbcr.configured_bbscn = vha->bbcr & 0xf;
  1956. }
  1957. done:
  1958. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1959. bsg_job->reply_payload.sg_cnt, &bbcr, sizeof(bbcr));
  1960. bsg_reply->reply_payload_rcv_len = sizeof(bbcr);
  1961. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = EXT_STATUS_OK;
  1962. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1963. bsg_reply->result = DID_OK << 16;
  1964. bsg_job_done(bsg_job, bsg_reply->result,
  1965. bsg_reply->reply_payload_rcv_len);
  1966. return 0;
  1967. }
  1968. static int
  1969. qla2x00_get_priv_stats(struct bsg_job *bsg_job)
  1970. {
  1971. struct fc_bsg_request *bsg_request = bsg_job->request;
  1972. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  1973. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  1974. scsi_qla_host_t *vha = shost_priv(host);
  1975. struct qla_hw_data *ha = vha->hw;
  1976. struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
  1977. struct link_statistics *stats = NULL;
  1978. dma_addr_t stats_dma;
  1979. int rval;
  1980. uint32_t *cmd = bsg_request->rqst_data.h_vendor.vendor_cmd;
  1981. uint options = cmd[0] == QL_VND_GET_PRIV_STATS_EX ? cmd[1] : 0;
  1982. if (test_bit(UNLOADING, &vha->dpc_flags))
  1983. return -ENODEV;
  1984. if (unlikely(pci_channel_offline(ha->pdev)))
  1985. return -ENODEV;
  1986. if (qla2x00_reset_active(vha))
  1987. return -EBUSY;
  1988. if (!IS_FWI2_CAPABLE(ha))
  1989. return -EPERM;
  1990. stats = dma_alloc_coherent(&ha->pdev->dev,
  1991. sizeof(*stats), &stats_dma, GFP_KERNEL);
  1992. if (!stats) {
  1993. ql_log(ql_log_warn, vha, 0x70e2,
  1994. "Failed to allocate memory for stats.\n");
  1995. return -ENOMEM;
  1996. }
  1997. memset(stats, 0, sizeof(*stats));
  1998. rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, options);
  1999. if (rval == QLA_SUCCESS) {
  2000. ql_dump_buffer(ql_dbg_user + ql_dbg_verbose, vha, 0x70e3,
  2001. (uint8_t *)stats, sizeof(*stats));
  2002. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  2003. bsg_job->reply_payload.sg_cnt, stats, sizeof(*stats));
  2004. }
  2005. bsg_reply->reply_payload_rcv_len = sizeof(*stats);
  2006. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  2007. rval ? EXT_STATUS_MAILBOX : EXT_STATUS_OK;
  2008. bsg_job->reply_len = sizeof(*bsg_reply);
  2009. bsg_reply->result = DID_OK << 16;
  2010. bsg_job_done(bsg_job, bsg_reply->result,
  2011. bsg_reply->reply_payload_rcv_len);
  2012. dma_free_coherent(&ha->pdev->dev, sizeof(*stats),
  2013. stats, stats_dma);
  2014. return 0;
  2015. }
  2016. static int
  2017. qla2x00_do_dport_diagnostics(struct bsg_job *bsg_job)
  2018. {
  2019. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  2020. struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
  2021. scsi_qla_host_t *vha = shost_priv(host);
  2022. int rval;
  2023. struct qla_dport_diag *dd;
  2024. if (!IS_QLA83XX(vha->hw) && !IS_QLA27XX(vha->hw))
  2025. return -EPERM;
  2026. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  2027. if (!dd) {
  2028. ql_log(ql_log_warn, vha, 0x70db,
  2029. "Failed to allocate memory for dport.\n");
  2030. return -ENOMEM;
  2031. }
  2032. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  2033. bsg_job->request_payload.sg_cnt, dd, sizeof(*dd));
  2034. rval = qla26xx_dport_diagnostics(
  2035. vha, dd->buf, sizeof(dd->buf), dd->options);
  2036. if (rval == QLA_SUCCESS) {
  2037. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  2038. bsg_job->reply_payload.sg_cnt, dd, sizeof(*dd));
  2039. }
  2040. bsg_reply->reply_payload_rcv_len = sizeof(*dd);
  2041. bsg_reply->reply_data.vendor_reply.vendor_rsp[0] =
  2042. rval ? EXT_STATUS_MAILBOX : EXT_STATUS_OK;
  2043. bsg_job->reply_len = sizeof(*bsg_reply);
  2044. bsg_reply->result = DID_OK << 16;
  2045. bsg_job_done(bsg_job, bsg_reply->result,
  2046. bsg_reply->reply_payload_rcv_len);
  2047. kfree(dd);
  2048. return 0;
  2049. }
  2050. static int
  2051. qla2x00_process_vendor_specific(struct bsg_job *bsg_job)
  2052. {
  2053. struct fc_bsg_request *bsg_request = bsg_job->request;
  2054. switch (bsg_request->rqst_data.h_vendor.vendor_cmd[0]) {
  2055. case QL_VND_LOOPBACK:
  2056. return qla2x00_process_loopback(bsg_job);
  2057. case QL_VND_A84_RESET:
  2058. return qla84xx_reset(bsg_job);
  2059. case QL_VND_A84_UPDATE_FW:
  2060. return qla84xx_updatefw(bsg_job);
  2061. case QL_VND_A84_MGMT_CMD:
  2062. return qla84xx_mgmt_cmd(bsg_job);
  2063. case QL_VND_IIDMA:
  2064. return qla24xx_iidma(bsg_job);
  2065. case QL_VND_FCP_PRIO_CFG_CMD:
  2066. return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
  2067. case QL_VND_READ_FLASH:
  2068. return qla2x00_read_optrom(bsg_job);
  2069. case QL_VND_UPDATE_FLASH:
  2070. return qla2x00_update_optrom(bsg_job);
  2071. case QL_VND_SET_FRU_VERSION:
  2072. return qla2x00_update_fru_versions(bsg_job);
  2073. case QL_VND_READ_FRU_STATUS:
  2074. return qla2x00_read_fru_status(bsg_job);
  2075. case QL_VND_WRITE_FRU_STATUS:
  2076. return qla2x00_write_fru_status(bsg_job);
  2077. case QL_VND_WRITE_I2C:
  2078. return qla2x00_write_i2c(bsg_job);
  2079. case QL_VND_READ_I2C:
  2080. return qla2x00_read_i2c(bsg_job);
  2081. case QL_VND_DIAG_IO_CMD:
  2082. return qla24xx_process_bidir_cmd(bsg_job);
  2083. case QL_VND_FX00_MGMT_CMD:
  2084. return qlafx00_mgmt_cmd(bsg_job);
  2085. case QL_VND_SERDES_OP:
  2086. return qla26xx_serdes_op(bsg_job);
  2087. case QL_VND_SERDES_OP_EX:
  2088. return qla8044_serdes_op(bsg_job);
  2089. case QL_VND_GET_FLASH_UPDATE_CAPS:
  2090. return qla27xx_get_flash_upd_cap(bsg_job);
  2091. case QL_VND_SET_FLASH_UPDATE_CAPS:
  2092. return qla27xx_set_flash_upd_cap(bsg_job);
  2093. case QL_VND_GET_BBCR_DATA:
  2094. return qla27xx_get_bbcr_data(bsg_job);
  2095. case QL_VND_GET_PRIV_STATS:
  2096. case QL_VND_GET_PRIV_STATS_EX:
  2097. return qla2x00_get_priv_stats(bsg_job);
  2098. case QL_VND_DPORT_DIAGNOSTICS:
  2099. return qla2x00_do_dport_diagnostics(bsg_job);
  2100. default:
  2101. return -ENOSYS;
  2102. }
  2103. }
  2104. int
  2105. qla24xx_bsg_request(struct bsg_job *bsg_job)
  2106. {
  2107. struct fc_bsg_request *bsg_request = bsg_job->request;
  2108. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  2109. int ret = -EINVAL;
  2110. struct fc_rport *rport;
  2111. struct Scsi_Host *host;
  2112. scsi_qla_host_t *vha;
  2113. /* In case no data transferred. */
  2114. bsg_reply->reply_payload_rcv_len = 0;
  2115. if (bsg_request->msgcode == FC_BSG_RPT_ELS) {
  2116. rport = fc_bsg_to_rport(bsg_job);
  2117. host = rport_to_shost(rport);
  2118. vha = shost_priv(host);
  2119. } else {
  2120. host = fc_bsg_to_shost(bsg_job);
  2121. vha = shost_priv(host);
  2122. }
  2123. if (qla2x00_reset_active(vha)) {
  2124. ql_dbg(ql_dbg_user, vha, 0x709f,
  2125. "BSG: ISP abort active/needed -- cmd=%d.\n",
  2126. bsg_request->msgcode);
  2127. return -EBUSY;
  2128. }
  2129. ql_dbg(ql_dbg_user, vha, 0x7000,
  2130. "Entered %s msgcode=0x%x.\n", __func__, bsg_request->msgcode);
  2131. switch (bsg_request->msgcode) {
  2132. case FC_BSG_RPT_ELS:
  2133. case FC_BSG_HST_ELS_NOLOGIN:
  2134. ret = qla2x00_process_els(bsg_job);
  2135. break;
  2136. case FC_BSG_HST_CT:
  2137. ret = qla2x00_process_ct(bsg_job);
  2138. break;
  2139. case FC_BSG_HST_VENDOR:
  2140. ret = qla2x00_process_vendor_specific(bsg_job);
  2141. break;
  2142. case FC_BSG_HST_ADD_RPORT:
  2143. case FC_BSG_HST_DEL_RPORT:
  2144. case FC_BSG_RPT_CT:
  2145. default:
  2146. ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
  2147. break;
  2148. }
  2149. return ret;
  2150. }
  2151. int
  2152. qla24xx_bsg_timeout(struct bsg_job *bsg_job)
  2153. {
  2154. struct fc_bsg_reply *bsg_reply = bsg_job->reply;
  2155. scsi_qla_host_t *vha = shost_priv(fc_bsg_to_shost(bsg_job));
  2156. struct qla_hw_data *ha = vha->hw;
  2157. srb_t *sp;
  2158. int cnt, que;
  2159. unsigned long flags;
  2160. struct req_que *req;
  2161. /* find the bsg job from the active list of commands */
  2162. spin_lock_irqsave(&ha->hardware_lock, flags);
  2163. for (que = 0; que < ha->max_req_queues; que++) {
  2164. req = ha->req_q_map[que];
  2165. if (!req)
  2166. continue;
  2167. for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
  2168. sp = req->outstanding_cmds[cnt];
  2169. if (sp) {
  2170. if (((sp->type == SRB_CT_CMD) ||
  2171. (sp->type == SRB_ELS_CMD_HST) ||
  2172. (sp->type == SRB_FXIOCB_BCMD))
  2173. && (sp->u.bsg_job == bsg_job)) {
  2174. req->outstanding_cmds[cnt] = NULL;
  2175. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2176. if (ha->isp_ops->abort_command(sp)) {
  2177. ql_log(ql_log_warn, vha, 0x7089,
  2178. "mbx abort_command "
  2179. "failed.\n");
  2180. bsg_job->req->errors =
  2181. bsg_reply->result = -EIO;
  2182. } else {
  2183. ql_dbg(ql_dbg_user, vha, 0x708a,
  2184. "mbx abort_command "
  2185. "success.\n");
  2186. bsg_job->req->errors =
  2187. bsg_reply->result = 0;
  2188. }
  2189. spin_lock_irqsave(&ha->hardware_lock, flags);
  2190. goto done;
  2191. }
  2192. }
  2193. }
  2194. }
  2195. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2196. ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
  2197. bsg_job->req->errors = bsg_reply->result = -ENXIO;
  2198. return 0;
  2199. done:
  2200. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2201. sp->free(vha, sp);
  2202. return 0;
  2203. }