qla_bsg.c 53 KB

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