qla_iocb.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/blkdev.h>
  9. #include <linux/delay.h>
  10. #include <scsi/scsi_tcq.h>
  11. static void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *);
  12. static void qla25xx_set_que(srb_t *, struct rsp_que **);
  13. /**
  14. * qla2x00_get_cmd_direction() - Determine control_flag data direction.
  15. * @cmd: SCSI command
  16. *
  17. * Returns the proper CF_* direction based on CDB.
  18. */
  19. static inline uint16_t
  20. qla2x00_get_cmd_direction(srb_t *sp)
  21. {
  22. uint16_t cflags;
  23. cflags = 0;
  24. /* Set transfer direction */
  25. if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
  26. cflags = CF_WRITE;
  27. sp->fcport->vha->hw->qla_stats.output_bytes +=
  28. scsi_bufflen(sp->cmd);
  29. } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
  30. cflags = CF_READ;
  31. sp->fcport->vha->hw->qla_stats.input_bytes +=
  32. scsi_bufflen(sp->cmd);
  33. }
  34. return (cflags);
  35. }
  36. /**
  37. * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
  38. * Continuation Type 0 IOCBs to allocate.
  39. *
  40. * @dsds: number of data segment decriptors needed
  41. *
  42. * Returns the number of IOCB entries needed to store @dsds.
  43. */
  44. uint16_t
  45. qla2x00_calc_iocbs_32(uint16_t dsds)
  46. {
  47. uint16_t iocbs;
  48. iocbs = 1;
  49. if (dsds > 3) {
  50. iocbs += (dsds - 3) / 7;
  51. if ((dsds - 3) % 7)
  52. iocbs++;
  53. }
  54. return (iocbs);
  55. }
  56. /**
  57. * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
  58. * Continuation Type 1 IOCBs to allocate.
  59. *
  60. * @dsds: number of data segment decriptors needed
  61. *
  62. * Returns the number of IOCB entries needed to store @dsds.
  63. */
  64. uint16_t
  65. qla2x00_calc_iocbs_64(uint16_t dsds)
  66. {
  67. uint16_t iocbs;
  68. iocbs = 1;
  69. if (dsds > 2) {
  70. iocbs += (dsds - 2) / 5;
  71. if ((dsds - 2) % 5)
  72. iocbs++;
  73. }
  74. return (iocbs);
  75. }
  76. /**
  77. * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
  78. * @ha: HA context
  79. *
  80. * Returns a pointer to the Continuation Type 0 IOCB packet.
  81. */
  82. static inline cont_entry_t *
  83. qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
  84. {
  85. cont_entry_t *cont_pkt;
  86. struct req_que *req = vha->req;
  87. /* Adjust ring index. */
  88. req->ring_index++;
  89. if (req->ring_index == req->length) {
  90. req->ring_index = 0;
  91. req->ring_ptr = req->ring;
  92. } else {
  93. req->ring_ptr++;
  94. }
  95. cont_pkt = (cont_entry_t *)req->ring_ptr;
  96. /* Load packet defaults. */
  97. *((uint32_t *)(&cont_pkt->entry_type)) =
  98. __constant_cpu_to_le32(CONTINUE_TYPE);
  99. return (cont_pkt);
  100. }
  101. /**
  102. * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
  103. * @ha: HA context
  104. *
  105. * Returns a pointer to the continuation type 1 IOCB packet.
  106. */
  107. static inline cont_a64_entry_t *
  108. qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha)
  109. {
  110. cont_a64_entry_t *cont_pkt;
  111. struct req_que *req = vha->req;
  112. /* Adjust ring index. */
  113. req->ring_index++;
  114. if (req->ring_index == req->length) {
  115. req->ring_index = 0;
  116. req->ring_ptr = req->ring;
  117. } else {
  118. req->ring_ptr++;
  119. }
  120. cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
  121. /* Load packet defaults. */
  122. *((uint32_t *)(&cont_pkt->entry_type)) =
  123. __constant_cpu_to_le32(CONTINUE_A64_TYPE);
  124. return (cont_pkt);
  125. }
  126. static inline int
  127. qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
  128. {
  129. uint8_t guard = scsi_host_get_guard(sp->cmd->device->host);
  130. /* We only support T10 DIF right now */
  131. if (guard != SHOST_DIX_GUARD_CRC) {
  132. DEBUG2(printk(KERN_ERR "Unsupported guard: %d\n", guard));
  133. return 0;
  134. }
  135. /* We always use DIFF Bundling for best performance */
  136. *fw_prot_opts = 0;
  137. /* Translate SCSI opcode to a protection opcode */
  138. switch (scsi_get_prot_op(sp->cmd)) {
  139. case SCSI_PROT_READ_STRIP:
  140. *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  141. break;
  142. case SCSI_PROT_WRITE_INSERT:
  143. *fw_prot_opts |= PO_MODE_DIF_INSERT;
  144. break;
  145. case SCSI_PROT_READ_INSERT:
  146. *fw_prot_opts |= PO_MODE_DIF_INSERT;
  147. break;
  148. case SCSI_PROT_WRITE_STRIP:
  149. *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  150. break;
  151. case SCSI_PROT_READ_PASS:
  152. *fw_prot_opts |= PO_MODE_DIF_PASS;
  153. break;
  154. case SCSI_PROT_WRITE_PASS:
  155. *fw_prot_opts |= PO_MODE_DIF_PASS;
  156. break;
  157. default: /* Normal Request */
  158. *fw_prot_opts |= PO_MODE_DIF_PASS;
  159. break;
  160. }
  161. return scsi_prot_sg_count(sp->cmd);
  162. }
  163. /*
  164. * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
  165. * capable IOCB types.
  166. *
  167. * @sp: SRB command to process
  168. * @cmd_pkt: Command type 2 IOCB
  169. * @tot_dsds: Total number of segments to transfer
  170. */
  171. void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
  172. uint16_t tot_dsds)
  173. {
  174. uint16_t avail_dsds;
  175. uint32_t *cur_dsd;
  176. scsi_qla_host_t *vha;
  177. struct scsi_cmnd *cmd;
  178. struct scatterlist *sg;
  179. int i;
  180. cmd = sp->cmd;
  181. /* Update entry type to indicate Command Type 2 IOCB */
  182. *((uint32_t *)(&cmd_pkt->entry_type)) =
  183. __constant_cpu_to_le32(COMMAND_TYPE);
  184. /* No data transfer */
  185. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  186. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  187. return;
  188. }
  189. vha = sp->fcport->vha;
  190. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
  191. /* Three DSDs are available in the Command Type 2 IOCB */
  192. avail_dsds = 3;
  193. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  194. /* Load data segments */
  195. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  196. cont_entry_t *cont_pkt;
  197. /* Allocate additional continuation packets? */
  198. if (avail_dsds == 0) {
  199. /*
  200. * Seven DSDs are available in the Continuation
  201. * Type 0 IOCB.
  202. */
  203. cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
  204. cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
  205. avail_dsds = 7;
  206. }
  207. *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
  208. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  209. avail_dsds--;
  210. }
  211. }
  212. /**
  213. * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
  214. * capable IOCB types.
  215. *
  216. * @sp: SRB command to process
  217. * @cmd_pkt: Command type 3 IOCB
  218. * @tot_dsds: Total number of segments to transfer
  219. */
  220. void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
  221. uint16_t tot_dsds)
  222. {
  223. uint16_t avail_dsds;
  224. uint32_t *cur_dsd;
  225. scsi_qla_host_t *vha;
  226. struct scsi_cmnd *cmd;
  227. struct scatterlist *sg;
  228. int i;
  229. cmd = sp->cmd;
  230. /* Update entry type to indicate Command Type 3 IOCB */
  231. *((uint32_t *)(&cmd_pkt->entry_type)) =
  232. __constant_cpu_to_le32(COMMAND_A64_TYPE);
  233. /* No data transfer */
  234. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  235. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  236. return;
  237. }
  238. vha = sp->fcport->vha;
  239. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
  240. /* Two DSDs are available in the Command Type 3 IOCB */
  241. avail_dsds = 2;
  242. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  243. /* Load data segments */
  244. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  245. dma_addr_t sle_dma;
  246. cont_a64_entry_t *cont_pkt;
  247. /* Allocate additional continuation packets? */
  248. if (avail_dsds == 0) {
  249. /*
  250. * Five DSDs are available in the Continuation
  251. * Type 1 IOCB.
  252. */
  253. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  254. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  255. avail_dsds = 5;
  256. }
  257. sle_dma = sg_dma_address(sg);
  258. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  259. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  260. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  261. avail_dsds--;
  262. }
  263. }
  264. /**
  265. * qla2x00_start_scsi() - Send a SCSI command to the ISP
  266. * @sp: command to send to the ISP
  267. *
  268. * Returns non-zero if a failure occurred, else zero.
  269. */
  270. int
  271. qla2x00_start_scsi(srb_t *sp)
  272. {
  273. int ret, nseg;
  274. unsigned long flags;
  275. scsi_qla_host_t *vha;
  276. struct scsi_cmnd *cmd;
  277. uint32_t *clr_ptr;
  278. uint32_t index;
  279. uint32_t handle;
  280. cmd_entry_t *cmd_pkt;
  281. uint16_t cnt;
  282. uint16_t req_cnt;
  283. uint16_t tot_dsds;
  284. struct device_reg_2xxx __iomem *reg;
  285. struct qla_hw_data *ha;
  286. struct req_que *req;
  287. struct rsp_que *rsp;
  288. /* Setup device pointers. */
  289. ret = 0;
  290. vha = sp->fcport->vha;
  291. ha = vha->hw;
  292. reg = &ha->iobase->isp;
  293. cmd = sp->cmd;
  294. req = ha->req_q_map[0];
  295. rsp = ha->rsp_q_map[0];
  296. /* So we know we haven't pci_map'ed anything yet */
  297. tot_dsds = 0;
  298. /* Send marker if required */
  299. if (vha->marker_needed != 0) {
  300. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
  301. != QLA_SUCCESS)
  302. return (QLA_FUNCTION_FAILED);
  303. vha->marker_needed = 0;
  304. }
  305. /* Acquire ring specific lock */
  306. spin_lock_irqsave(&ha->hardware_lock, flags);
  307. /* Check for room in outstanding command list. */
  308. handle = req->current_outstanding_cmd;
  309. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  310. handle++;
  311. if (handle == MAX_OUTSTANDING_COMMANDS)
  312. handle = 1;
  313. if (!req->outstanding_cmds[handle])
  314. break;
  315. }
  316. if (index == MAX_OUTSTANDING_COMMANDS)
  317. goto queuing_error;
  318. /* Map the sg table so we have an accurate count of sg entries needed */
  319. if (scsi_sg_count(cmd)) {
  320. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  321. scsi_sg_count(cmd), cmd->sc_data_direction);
  322. if (unlikely(!nseg))
  323. goto queuing_error;
  324. } else
  325. nseg = 0;
  326. tot_dsds = nseg;
  327. /* Calculate the number of request entries needed. */
  328. req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
  329. if (req->cnt < (req_cnt + 2)) {
  330. cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
  331. if (req->ring_index < cnt)
  332. req->cnt = cnt - req->ring_index;
  333. else
  334. req->cnt = req->length -
  335. (req->ring_index - cnt);
  336. }
  337. if (req->cnt < (req_cnt + 2))
  338. goto queuing_error;
  339. /* Build command packet */
  340. req->current_outstanding_cmd = handle;
  341. req->outstanding_cmds[handle] = sp;
  342. sp->handle = handle;
  343. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  344. req->cnt -= req_cnt;
  345. cmd_pkt = (cmd_entry_t *)req->ring_ptr;
  346. cmd_pkt->handle = handle;
  347. /* Zero out remaining portion of packet. */
  348. clr_ptr = (uint32_t *)cmd_pkt + 2;
  349. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  350. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  351. /* Set target ID and LUN number*/
  352. SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
  353. cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
  354. /* Update tagged queuing modifier */
  355. cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
  356. /* Load SCSI command packet. */
  357. memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
  358. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  359. /* Build IOCB segments */
  360. ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
  361. /* Set total data segment count. */
  362. cmd_pkt->entry_count = (uint8_t)req_cnt;
  363. wmb();
  364. /* Adjust ring index. */
  365. req->ring_index++;
  366. if (req->ring_index == req->length) {
  367. req->ring_index = 0;
  368. req->ring_ptr = req->ring;
  369. } else
  370. req->ring_ptr++;
  371. sp->flags |= SRB_DMA_VALID;
  372. /* Set chip new ring index. */
  373. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
  374. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
  375. /* Manage unprocessed RIO/ZIO commands in response queue. */
  376. if (vha->flags.process_response_queue &&
  377. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  378. qla2x00_process_response_queue(rsp);
  379. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  380. return (QLA_SUCCESS);
  381. queuing_error:
  382. if (tot_dsds)
  383. scsi_dma_unmap(cmd);
  384. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  385. return (QLA_FUNCTION_FAILED);
  386. }
  387. /**
  388. * qla2x00_marker() - Send a marker IOCB to the firmware.
  389. * @ha: HA context
  390. * @loop_id: loop ID
  391. * @lun: LUN
  392. * @type: marker modifier
  393. *
  394. * Can be called from both normal and interrupt context.
  395. *
  396. * Returns non-zero if a failure occurred, else zero.
  397. */
  398. static int
  399. __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  400. struct rsp_que *rsp, uint16_t loop_id,
  401. uint16_t lun, uint8_t type)
  402. {
  403. mrk_entry_t *mrk;
  404. struct mrk_entry_24xx *mrk24;
  405. struct qla_hw_data *ha = vha->hw;
  406. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  407. mrk24 = NULL;
  408. mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, 0);
  409. if (mrk == NULL) {
  410. DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
  411. __func__, base_vha->host_no));
  412. return (QLA_FUNCTION_FAILED);
  413. }
  414. mrk->entry_type = MARKER_TYPE;
  415. mrk->modifier = type;
  416. if (type != MK_SYNC_ALL) {
  417. if (IS_FWI2_CAPABLE(ha)) {
  418. mrk24 = (struct mrk_entry_24xx *) mrk;
  419. mrk24->nport_handle = cpu_to_le16(loop_id);
  420. mrk24->lun[1] = LSB(lun);
  421. mrk24->lun[2] = MSB(lun);
  422. host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
  423. mrk24->vp_index = vha->vp_idx;
  424. mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
  425. } else {
  426. SET_TARGET_ID(ha, mrk->target, loop_id);
  427. mrk->lun = cpu_to_le16(lun);
  428. }
  429. }
  430. wmb();
  431. qla2x00_isp_cmd(vha, req);
  432. return (QLA_SUCCESS);
  433. }
  434. int
  435. qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  436. struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
  437. uint8_t type)
  438. {
  439. int ret;
  440. unsigned long flags = 0;
  441. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  442. ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
  443. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  444. return (ret);
  445. }
  446. /**
  447. * qla2x00_isp_cmd() - Modify the request ring pointer.
  448. * @ha: HA context
  449. *
  450. * Note: The caller must hold the hardware lock before calling this routine.
  451. */
  452. static void
  453. qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
  454. {
  455. struct qla_hw_data *ha = vha->hw;
  456. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  457. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  458. DEBUG5(printk("%s(): IOCB data:\n", __func__));
  459. DEBUG5(qla2x00_dump_buffer(
  460. (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
  461. /* Adjust ring index. */
  462. req->ring_index++;
  463. if (req->ring_index == req->length) {
  464. req->ring_index = 0;
  465. req->ring_ptr = req->ring;
  466. } else
  467. req->ring_ptr++;
  468. /* Set chip new ring index. */
  469. if (IS_QLA82XX(ha)) {
  470. uint32_t dbval = 0x04 | (ha->portnum << 5);
  471. /* write, read and verify logic */
  472. dbval = dbval | (req->id << 8) | (req->ring_index << 16);
  473. if (ql2xdbwr)
  474. qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
  475. else {
  476. WRT_REG_DWORD(
  477. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  478. dbval);
  479. wmb();
  480. while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
  481. WRT_REG_DWORD((unsigned long __iomem *)
  482. ha->nxdb_wr_ptr, dbval);
  483. wmb();
  484. }
  485. }
  486. } else if (ha->mqenable) {
  487. /* Set chip new ring index. */
  488. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  489. RD_REG_DWORD(&ioreg->hccr);
  490. } else {
  491. if (IS_FWI2_CAPABLE(ha)) {
  492. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  493. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  494. } else {
  495. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  496. req->ring_index);
  497. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  498. }
  499. }
  500. }
  501. /**
  502. * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
  503. * Continuation Type 1 IOCBs to allocate.
  504. *
  505. * @dsds: number of data segment decriptors needed
  506. *
  507. * Returns the number of IOCB entries needed to store @dsds.
  508. */
  509. inline uint16_t
  510. qla24xx_calc_iocbs(uint16_t dsds)
  511. {
  512. uint16_t iocbs;
  513. iocbs = 1;
  514. if (dsds > 1) {
  515. iocbs += (dsds - 1) / 5;
  516. if ((dsds - 1) % 5)
  517. iocbs++;
  518. }
  519. DEBUG3(printk(KERN_DEBUG "%s(): Required PKT(s) = %d\n",
  520. __func__, iocbs));
  521. return iocbs;
  522. }
  523. /**
  524. * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
  525. * IOCB types.
  526. *
  527. * @sp: SRB command to process
  528. * @cmd_pkt: Command type 3 IOCB
  529. * @tot_dsds: Total number of segments to transfer
  530. */
  531. inline void
  532. qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
  533. uint16_t tot_dsds)
  534. {
  535. uint16_t avail_dsds;
  536. uint32_t *cur_dsd;
  537. scsi_qla_host_t *vha;
  538. struct scsi_cmnd *cmd;
  539. struct scatterlist *sg;
  540. int i;
  541. struct req_que *req;
  542. cmd = sp->cmd;
  543. /* Update entry type to indicate Command Type 3 IOCB */
  544. *((uint32_t *)(&cmd_pkt->entry_type)) =
  545. __constant_cpu_to_le32(COMMAND_TYPE_7);
  546. /* No data transfer */
  547. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  548. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  549. return;
  550. }
  551. vha = sp->fcport->vha;
  552. req = vha->req;
  553. /* Set transfer direction */
  554. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  555. cmd_pkt->task_mgmt_flags =
  556. __constant_cpu_to_le16(TMF_WRITE_DATA);
  557. sp->fcport->vha->hw->qla_stats.output_bytes +=
  558. scsi_bufflen(sp->cmd);
  559. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  560. cmd_pkt->task_mgmt_flags =
  561. __constant_cpu_to_le16(TMF_READ_DATA);
  562. sp->fcport->vha->hw->qla_stats.input_bytes +=
  563. scsi_bufflen(sp->cmd);
  564. }
  565. /* One DSD is available in the Command Type 3 IOCB */
  566. avail_dsds = 1;
  567. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  568. /* Load data segments */
  569. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  570. dma_addr_t sle_dma;
  571. cont_a64_entry_t *cont_pkt;
  572. /* Allocate additional continuation packets? */
  573. if (avail_dsds == 0) {
  574. /*
  575. * Five DSDs are available in the Continuation
  576. * Type 1 IOCB.
  577. */
  578. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  579. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  580. avail_dsds = 5;
  581. }
  582. sle_dma = sg_dma_address(sg);
  583. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  584. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  585. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  586. avail_dsds--;
  587. }
  588. }
  589. struct fw_dif_context {
  590. uint32_t ref_tag;
  591. uint16_t app_tag;
  592. uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
  593. uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
  594. };
  595. /*
  596. * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
  597. *
  598. */
  599. static inline void
  600. qla24xx_set_t10dif_tags(struct scsi_cmnd *cmd, struct fw_dif_context *pkt,
  601. unsigned int protcnt)
  602. {
  603. struct sd_dif_tuple *spt;
  604. unsigned char op = scsi_get_prot_op(cmd);
  605. switch (scsi_get_prot_type(cmd)) {
  606. /* For TYPE 0 protection: no checking */
  607. case SCSI_PROT_DIF_TYPE0:
  608. pkt->ref_tag_mask[0] = 0x00;
  609. pkt->ref_tag_mask[1] = 0x00;
  610. pkt->ref_tag_mask[2] = 0x00;
  611. pkt->ref_tag_mask[3] = 0x00;
  612. break;
  613. /*
  614. * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
  615. * match LBA in CDB + N
  616. */
  617. case SCSI_PROT_DIF_TYPE2:
  618. if (!ql2xenablehba_err_chk)
  619. break;
  620. if (scsi_prot_sg_count(cmd)) {
  621. spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
  622. scsi_prot_sglist(cmd)[0].offset;
  623. pkt->app_tag = swab32(spt->app_tag);
  624. pkt->app_tag_mask[0] = 0xff;
  625. pkt->app_tag_mask[1] = 0xff;
  626. }
  627. pkt->ref_tag = cpu_to_le32((uint32_t)
  628. (0xffffffff & scsi_get_lba(cmd)));
  629. /* enable ALL bytes of the ref tag */
  630. pkt->ref_tag_mask[0] = 0xff;
  631. pkt->ref_tag_mask[1] = 0xff;
  632. pkt->ref_tag_mask[2] = 0xff;
  633. pkt->ref_tag_mask[3] = 0xff;
  634. break;
  635. /* For Type 3 protection: 16 bit GUARD only */
  636. case SCSI_PROT_DIF_TYPE3:
  637. pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
  638. pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
  639. 0x00;
  640. break;
  641. /*
  642. * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
  643. * 16 bit app tag.
  644. */
  645. case SCSI_PROT_DIF_TYPE1:
  646. if (!ql2xenablehba_err_chk)
  647. break;
  648. if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
  649. op == SCSI_PROT_WRITE_PASS)) {
  650. spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
  651. scsi_prot_sglist(cmd)[0].offset;
  652. DEBUG18(printk(KERN_DEBUG
  653. "%s(): LBA from user %p, lba = 0x%x\n",
  654. __func__, spt, (int)spt->ref_tag));
  655. pkt->ref_tag = swab32(spt->ref_tag);
  656. pkt->app_tag_mask[0] = 0x0;
  657. pkt->app_tag_mask[1] = 0x0;
  658. } else {
  659. pkt->ref_tag = cpu_to_le32((uint32_t)
  660. (0xffffffff & scsi_get_lba(cmd)));
  661. pkt->app_tag = __constant_cpu_to_le16(0);
  662. pkt->app_tag_mask[0] = 0x0;
  663. pkt->app_tag_mask[1] = 0x0;
  664. }
  665. /* enable ALL bytes of the ref tag */
  666. pkt->ref_tag_mask[0] = 0xff;
  667. pkt->ref_tag_mask[1] = 0xff;
  668. pkt->ref_tag_mask[2] = 0xff;
  669. pkt->ref_tag_mask[3] = 0xff;
  670. break;
  671. }
  672. DEBUG18(printk(KERN_DEBUG
  673. "%s(): Setting protection Tags: (BIG) ref tag = 0x%x,"
  674. " app tag = 0x%x, prot SG count %d , cmd lba 0x%x,"
  675. " prot_type=%u\n", __func__, pkt->ref_tag, pkt->app_tag, protcnt,
  676. (int)scsi_get_lba(cmd), scsi_get_prot_type(cmd)));
  677. }
  678. static int
  679. qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
  680. uint16_t tot_dsds)
  681. {
  682. void *next_dsd;
  683. uint8_t avail_dsds = 0;
  684. uint32_t dsd_list_len;
  685. struct dsd_dma *dsd_ptr;
  686. struct scatterlist *sg;
  687. uint32_t *cur_dsd = dsd;
  688. int i;
  689. uint16_t used_dsds = tot_dsds;
  690. uint8_t *cp;
  691. scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
  692. dma_addr_t sle_dma;
  693. /* Allocate additional continuation packets? */
  694. if (avail_dsds == 0) {
  695. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  696. QLA_DSDS_PER_IOCB : used_dsds;
  697. dsd_list_len = (avail_dsds + 1) * 12;
  698. used_dsds -= avail_dsds;
  699. /* allocate tracking DS */
  700. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  701. if (!dsd_ptr)
  702. return 1;
  703. /* allocate new list */
  704. dsd_ptr->dsd_addr = next_dsd =
  705. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  706. &dsd_ptr->dsd_list_dma);
  707. if (!next_dsd) {
  708. /*
  709. * Need to cleanup only this dsd_ptr, rest
  710. * will be done by sp_free_dma()
  711. */
  712. kfree(dsd_ptr);
  713. return 1;
  714. }
  715. list_add_tail(&dsd_ptr->list,
  716. &((struct crc_context *)sp->ctx)->dsd_list);
  717. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  718. /* add new list to cmd iocb or last list */
  719. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  720. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  721. *cur_dsd++ = dsd_list_len;
  722. cur_dsd = (uint32_t *)next_dsd;
  723. }
  724. sle_dma = sg_dma_address(sg);
  725. DEBUG18(printk("%s(): %p, sg entry %d - addr =0x%x 0x%x,"
  726. " len =%d\n", __func__ , cur_dsd, i, LSD(sle_dma),
  727. MSD(sle_dma), sg_dma_len(sg)));
  728. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  729. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  730. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  731. avail_dsds--;
  732. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  733. cp = page_address(sg_page(sg)) + sg->offset;
  734. DEBUG18(printk("%s(): User Data buffer= %p:\n",
  735. __func__ , cp));
  736. }
  737. }
  738. /* Null termination */
  739. *cur_dsd++ = 0;
  740. *cur_dsd++ = 0;
  741. *cur_dsd++ = 0;
  742. return 0;
  743. }
  744. static int
  745. qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
  746. uint32_t *dsd,
  747. uint16_t tot_dsds)
  748. {
  749. void *next_dsd;
  750. uint8_t avail_dsds = 0;
  751. uint32_t dsd_list_len;
  752. struct dsd_dma *dsd_ptr;
  753. struct scatterlist *sg;
  754. int i;
  755. struct scsi_cmnd *cmd;
  756. uint32_t *cur_dsd = dsd;
  757. uint16_t used_dsds = tot_dsds;
  758. uint8_t *cp;
  759. cmd = sp->cmd;
  760. scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
  761. dma_addr_t sle_dma;
  762. /* Allocate additional continuation packets? */
  763. if (avail_dsds == 0) {
  764. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  765. QLA_DSDS_PER_IOCB : used_dsds;
  766. dsd_list_len = (avail_dsds + 1) * 12;
  767. used_dsds -= avail_dsds;
  768. /* allocate tracking DS */
  769. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  770. if (!dsd_ptr)
  771. return 1;
  772. /* allocate new list */
  773. dsd_ptr->dsd_addr = next_dsd =
  774. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  775. &dsd_ptr->dsd_list_dma);
  776. if (!next_dsd) {
  777. /*
  778. * Need to cleanup only this dsd_ptr, rest
  779. * will be done by sp_free_dma()
  780. */
  781. kfree(dsd_ptr);
  782. return 1;
  783. }
  784. list_add_tail(&dsd_ptr->list,
  785. &((struct crc_context *)sp->ctx)->dsd_list);
  786. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  787. /* add new list to cmd iocb or last list */
  788. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  789. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  790. *cur_dsd++ = dsd_list_len;
  791. cur_dsd = (uint32_t *)next_dsd;
  792. }
  793. sle_dma = sg_dma_address(sg);
  794. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  795. DEBUG18(printk(KERN_DEBUG
  796. "%s(): %p, sg entry %d - addr =0x%x"
  797. "0x%x, len =%d\n", __func__ , cur_dsd, i,
  798. LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg)));
  799. }
  800. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  801. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  802. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  803. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  804. cp = page_address(sg_page(sg)) + sg->offset;
  805. DEBUG18(printk("%s(): Protection Data buffer = %p:\n",
  806. __func__ , cp));
  807. }
  808. avail_dsds--;
  809. }
  810. /* Null termination */
  811. *cur_dsd++ = 0;
  812. *cur_dsd++ = 0;
  813. *cur_dsd++ = 0;
  814. return 0;
  815. }
  816. /**
  817. * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
  818. * Type 6 IOCB types.
  819. *
  820. * @sp: SRB command to process
  821. * @cmd_pkt: Command type 3 IOCB
  822. * @tot_dsds: Total number of segments to transfer
  823. */
  824. static inline int
  825. qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
  826. uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
  827. {
  828. uint32_t *cur_dsd, *fcp_dl;
  829. scsi_qla_host_t *vha;
  830. struct scsi_cmnd *cmd;
  831. struct scatterlist *cur_seg;
  832. int sgc;
  833. uint32_t total_bytes;
  834. uint32_t data_bytes;
  835. uint32_t dif_bytes;
  836. uint8_t bundling = 1;
  837. uint16_t blk_size;
  838. uint8_t *clr_ptr;
  839. struct crc_context *crc_ctx_pkt = NULL;
  840. struct qla_hw_data *ha;
  841. uint8_t additional_fcpcdb_len;
  842. uint16_t fcp_cmnd_len;
  843. struct fcp_cmnd *fcp_cmnd;
  844. dma_addr_t crc_ctx_dma;
  845. cmd = sp->cmd;
  846. sgc = 0;
  847. /* Update entry type to indicate Command Type CRC_2 IOCB */
  848. *((uint32_t *)(&cmd_pkt->entry_type)) =
  849. __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
  850. /* No data transfer */
  851. data_bytes = scsi_bufflen(cmd);
  852. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  853. DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
  854. __func__, data_bytes));
  855. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  856. return QLA_SUCCESS;
  857. }
  858. vha = sp->fcport->vha;
  859. ha = vha->hw;
  860. DEBUG18(printk(KERN_DEBUG
  861. "%s(%ld): Executing cmd sp %p, prot_op=%u.\n", __func__,
  862. vha->host_no, sp, scsi_get_prot_op(sp->cmd)));
  863. cmd_pkt->vp_index = sp->fcport->vp_idx;
  864. /* Set transfer direction */
  865. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  866. cmd_pkt->control_flags =
  867. __constant_cpu_to_le16(CF_WRITE_DATA);
  868. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  869. cmd_pkt->control_flags =
  870. __constant_cpu_to_le16(CF_READ_DATA);
  871. }
  872. tot_prot_dsds = scsi_prot_sg_count(cmd);
  873. if (!tot_prot_dsds)
  874. bundling = 0;
  875. /* Allocate CRC context from global pool */
  876. crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
  877. GFP_ATOMIC, &crc_ctx_dma);
  878. if (!crc_ctx_pkt)
  879. goto crc_queuing_error;
  880. /* Zero out CTX area. */
  881. clr_ptr = (uint8_t *)crc_ctx_pkt;
  882. memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
  883. crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
  884. sp->flags |= SRB_CRC_CTX_DMA_VALID;
  885. /* Set handle */
  886. crc_ctx_pkt->handle = cmd_pkt->handle;
  887. INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
  888. qla24xx_set_t10dif_tags(cmd, (struct fw_dif_context *)
  889. &crc_ctx_pkt->ref_tag, tot_prot_dsds);
  890. cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
  891. cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
  892. cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
  893. /* Determine SCSI command length -- align to 4 byte boundary */
  894. if (cmd->cmd_len > 16) {
  895. DEBUG18(printk(KERN_INFO "%s(): **** SCSI CMD > 16\n",
  896. __func__));
  897. additional_fcpcdb_len = cmd->cmd_len - 16;
  898. if ((cmd->cmd_len % 4) != 0) {
  899. /* SCSI cmd > 16 bytes must be multiple of 4 */
  900. goto crc_queuing_error;
  901. }
  902. fcp_cmnd_len = 12 + cmd->cmd_len + 4;
  903. } else {
  904. additional_fcpcdb_len = 0;
  905. fcp_cmnd_len = 12 + 16 + 4;
  906. }
  907. fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
  908. fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
  909. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  910. fcp_cmnd->additional_cdb_len |= 1;
  911. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  912. fcp_cmnd->additional_cdb_len |= 2;
  913. int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
  914. host_to_fcp_swap((uint8_t *)&fcp_cmnd->lun, sizeof(fcp_cmnd->lun));
  915. memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
  916. cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
  917. cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
  918. LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  919. cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
  920. MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  921. fcp_cmnd->task_attribute = 0;
  922. fcp_cmnd->task_management = 0;
  923. cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
  924. DEBUG18(printk(KERN_INFO "%s(%ld): Total SG(s) Entries %d, Data"
  925. "entries %d, data bytes %d, Protection entries %d\n",
  926. __func__, vha->host_no, tot_dsds, (tot_dsds-tot_prot_dsds),
  927. data_bytes, tot_prot_dsds));
  928. /* Compute dif len and adjust data len to incude protection */
  929. total_bytes = data_bytes;
  930. dif_bytes = 0;
  931. blk_size = cmd->device->sector_size;
  932. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  933. dif_bytes = (data_bytes / blk_size) * 8;
  934. total_bytes += dif_bytes;
  935. }
  936. if (!ql2xenablehba_err_chk)
  937. fw_prot_opts |= 0x10; /* Disable Guard tag checking */
  938. if (!bundling) {
  939. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
  940. } else {
  941. /*
  942. * Configure Bundling if we need to fetch interlaving
  943. * protection PCI accesses
  944. */
  945. fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
  946. crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
  947. crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
  948. tot_prot_dsds);
  949. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
  950. }
  951. /* Finish the common fields of CRC pkt */
  952. crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
  953. crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
  954. crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
  955. crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
  956. /* Fibre channel byte count */
  957. cmd_pkt->byte_count = cpu_to_le32(total_bytes);
  958. fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
  959. additional_fcpcdb_len);
  960. *fcp_dl = htonl(total_bytes);
  961. DEBUG18(printk(KERN_INFO "%s(%ld): dif bytes = 0x%x (%d), total bytes"
  962. " = 0x%x (%d), dat block size =0x%x (%d)\n", __func__,
  963. vha->host_no, dif_bytes, dif_bytes, total_bytes, total_bytes,
  964. crc_ctx_pkt->blk_size, crc_ctx_pkt->blk_size));
  965. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  966. DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
  967. __func__, data_bytes));
  968. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  969. return QLA_SUCCESS;
  970. }
  971. /* Walks data segments */
  972. cmd_pkt->control_flags |=
  973. __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
  974. if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
  975. (tot_dsds - tot_prot_dsds)))
  976. goto crc_queuing_error;
  977. if (bundling && tot_prot_dsds) {
  978. /* Walks dif segments */
  979. cur_seg = scsi_prot_sglist(cmd);
  980. cmd_pkt->control_flags |=
  981. __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
  982. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
  983. if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
  984. tot_prot_dsds))
  985. goto crc_queuing_error;
  986. }
  987. return QLA_SUCCESS;
  988. crc_queuing_error:
  989. DEBUG18(qla_printk(KERN_INFO, ha,
  990. "CMD sent FAILED crc_q error:sp = %p\n", sp));
  991. /* Cleanup will be performed by the caller */
  992. return QLA_FUNCTION_FAILED;
  993. }
  994. /**
  995. * qla24xx_start_scsi() - Send a SCSI command to the ISP
  996. * @sp: command to send to the ISP
  997. *
  998. * Returns non-zero if a failure occurred, else zero.
  999. */
  1000. int
  1001. qla24xx_start_scsi(srb_t *sp)
  1002. {
  1003. int ret, nseg;
  1004. unsigned long flags;
  1005. uint32_t *clr_ptr;
  1006. uint32_t index;
  1007. uint32_t handle;
  1008. struct cmd_type_7 *cmd_pkt;
  1009. uint16_t cnt;
  1010. uint16_t req_cnt;
  1011. uint16_t tot_dsds;
  1012. struct req_que *req = NULL;
  1013. struct rsp_que *rsp = NULL;
  1014. struct scsi_cmnd *cmd = sp->cmd;
  1015. struct scsi_qla_host *vha = sp->fcport->vha;
  1016. struct qla_hw_data *ha = vha->hw;
  1017. /* Setup device pointers. */
  1018. ret = 0;
  1019. qla25xx_set_que(sp, &rsp);
  1020. req = vha->req;
  1021. /* So we know we haven't pci_map'ed anything yet */
  1022. tot_dsds = 0;
  1023. /* Send marker if required */
  1024. if (vha->marker_needed != 0) {
  1025. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
  1026. != QLA_SUCCESS)
  1027. return QLA_FUNCTION_FAILED;
  1028. vha->marker_needed = 0;
  1029. }
  1030. /* Acquire ring specific lock */
  1031. spin_lock_irqsave(&ha->hardware_lock, flags);
  1032. /* Check for room in outstanding command list. */
  1033. handle = req->current_outstanding_cmd;
  1034. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1035. handle++;
  1036. if (handle == MAX_OUTSTANDING_COMMANDS)
  1037. handle = 1;
  1038. if (!req->outstanding_cmds[handle])
  1039. break;
  1040. }
  1041. if (index == MAX_OUTSTANDING_COMMANDS)
  1042. goto queuing_error;
  1043. /* Map the sg table so we have an accurate count of sg entries needed */
  1044. if (scsi_sg_count(cmd)) {
  1045. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1046. scsi_sg_count(cmd), cmd->sc_data_direction);
  1047. if (unlikely(!nseg))
  1048. goto queuing_error;
  1049. } else
  1050. nseg = 0;
  1051. tot_dsds = nseg;
  1052. req_cnt = qla24xx_calc_iocbs(tot_dsds);
  1053. if (req->cnt < (req_cnt + 2)) {
  1054. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1055. if (req->ring_index < cnt)
  1056. req->cnt = cnt - req->ring_index;
  1057. else
  1058. req->cnt = req->length -
  1059. (req->ring_index - cnt);
  1060. }
  1061. if (req->cnt < (req_cnt + 2))
  1062. goto queuing_error;
  1063. /* Build command packet. */
  1064. req->current_outstanding_cmd = handle;
  1065. req->outstanding_cmds[handle] = sp;
  1066. sp->handle = handle;
  1067. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1068. req->cnt -= req_cnt;
  1069. cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
  1070. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1071. /* Zero out remaining portion of packet. */
  1072. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1073. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1074. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1075. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1076. /* Set NPORT-ID and LUN number*/
  1077. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1078. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1079. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1080. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1081. cmd_pkt->vp_index = sp->fcport->vp_idx;
  1082. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1083. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1084. /* Load SCSI command packet. */
  1085. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  1086. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  1087. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  1088. /* Build IOCB segments */
  1089. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  1090. /* Set total data segment count. */
  1091. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1092. /* Specify response queue number where completion should happen */
  1093. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1094. wmb();
  1095. /* Adjust ring index. */
  1096. req->ring_index++;
  1097. if (req->ring_index == req->length) {
  1098. req->ring_index = 0;
  1099. req->ring_ptr = req->ring;
  1100. } else
  1101. req->ring_ptr++;
  1102. sp->flags |= SRB_DMA_VALID;
  1103. /* Set chip new ring index. */
  1104. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1105. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1106. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1107. if (vha->flags.process_response_queue &&
  1108. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1109. qla24xx_process_response_queue(vha, rsp);
  1110. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1111. return QLA_SUCCESS;
  1112. queuing_error:
  1113. if (tot_dsds)
  1114. scsi_dma_unmap(cmd);
  1115. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1116. return QLA_FUNCTION_FAILED;
  1117. }
  1118. /**
  1119. * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
  1120. * @sp: command to send to the ISP
  1121. *
  1122. * Returns non-zero if a failure occurred, else zero.
  1123. */
  1124. int
  1125. qla24xx_dif_start_scsi(srb_t *sp)
  1126. {
  1127. int nseg;
  1128. unsigned long flags;
  1129. uint32_t *clr_ptr;
  1130. uint32_t index;
  1131. uint32_t handle;
  1132. uint16_t cnt;
  1133. uint16_t req_cnt = 0;
  1134. uint16_t tot_dsds;
  1135. uint16_t tot_prot_dsds;
  1136. uint16_t fw_prot_opts = 0;
  1137. struct req_que *req = NULL;
  1138. struct rsp_que *rsp = NULL;
  1139. struct scsi_cmnd *cmd = sp->cmd;
  1140. struct scsi_qla_host *vha = sp->fcport->vha;
  1141. struct qla_hw_data *ha = vha->hw;
  1142. struct cmd_type_crc_2 *cmd_pkt;
  1143. uint32_t status = 0;
  1144. #define QDSS_GOT_Q_SPACE BIT_0
  1145. /* Only process protection or >16 cdb in this routine */
  1146. if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
  1147. if (cmd->cmd_len <= 16)
  1148. return qla24xx_start_scsi(sp);
  1149. }
  1150. /* Setup device pointers. */
  1151. qla25xx_set_que(sp, &rsp);
  1152. req = vha->req;
  1153. /* So we know we haven't pci_map'ed anything yet */
  1154. tot_dsds = 0;
  1155. /* Send marker if required */
  1156. if (vha->marker_needed != 0) {
  1157. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  1158. QLA_SUCCESS)
  1159. return QLA_FUNCTION_FAILED;
  1160. vha->marker_needed = 0;
  1161. }
  1162. /* Acquire ring specific lock */
  1163. spin_lock_irqsave(&ha->hardware_lock, flags);
  1164. /* Check for room in outstanding command list. */
  1165. handle = req->current_outstanding_cmd;
  1166. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1167. handle++;
  1168. if (handle == MAX_OUTSTANDING_COMMANDS)
  1169. handle = 1;
  1170. if (!req->outstanding_cmds[handle])
  1171. break;
  1172. }
  1173. if (index == MAX_OUTSTANDING_COMMANDS)
  1174. goto queuing_error;
  1175. /* Compute number of required data segments */
  1176. /* Map the sg table so we have an accurate count of sg entries needed */
  1177. if (scsi_sg_count(cmd)) {
  1178. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1179. scsi_sg_count(cmd), cmd->sc_data_direction);
  1180. if (unlikely(!nseg))
  1181. goto queuing_error;
  1182. else
  1183. sp->flags |= SRB_DMA_VALID;
  1184. } else
  1185. nseg = 0;
  1186. /* number of required data segments */
  1187. tot_dsds = nseg;
  1188. /* Compute number of required protection segments */
  1189. if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
  1190. nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
  1191. scsi_prot_sg_count(cmd), cmd->sc_data_direction);
  1192. if (unlikely(!nseg))
  1193. goto queuing_error;
  1194. else
  1195. sp->flags |= SRB_CRC_PROT_DMA_VALID;
  1196. } else {
  1197. nseg = 0;
  1198. }
  1199. req_cnt = 1;
  1200. /* Total Data and protection sg segment(s) */
  1201. tot_prot_dsds = nseg;
  1202. tot_dsds += nseg;
  1203. if (req->cnt < (req_cnt + 2)) {
  1204. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1205. if (req->ring_index < cnt)
  1206. req->cnt = cnt - req->ring_index;
  1207. else
  1208. req->cnt = req->length -
  1209. (req->ring_index - cnt);
  1210. }
  1211. if (req->cnt < (req_cnt + 2))
  1212. goto queuing_error;
  1213. status |= QDSS_GOT_Q_SPACE;
  1214. /* Build header part of command packet (excluding the OPCODE). */
  1215. req->current_outstanding_cmd = handle;
  1216. req->outstanding_cmds[handle] = sp;
  1217. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1218. req->cnt -= req_cnt;
  1219. /* Fill-in common area */
  1220. cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
  1221. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1222. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1223. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1224. /* Set NPORT-ID and LUN number*/
  1225. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1226. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1227. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1228. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1229. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1230. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1231. /* Total Data and protection segment(s) */
  1232. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1233. /* Build IOCB segments and adjust for data protection segments */
  1234. if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
  1235. req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
  1236. QLA_SUCCESS)
  1237. goto queuing_error;
  1238. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1239. /* Specify response queue number where completion should happen */
  1240. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1241. cmd_pkt->timeout = __constant_cpu_to_le16(0);
  1242. wmb();
  1243. /* Adjust ring index. */
  1244. req->ring_index++;
  1245. if (req->ring_index == req->length) {
  1246. req->ring_index = 0;
  1247. req->ring_ptr = req->ring;
  1248. } else
  1249. req->ring_ptr++;
  1250. /* Set chip new ring index. */
  1251. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1252. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1253. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1254. if (vha->flags.process_response_queue &&
  1255. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1256. qla24xx_process_response_queue(vha, rsp);
  1257. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1258. return QLA_SUCCESS;
  1259. queuing_error:
  1260. if (status & QDSS_GOT_Q_SPACE) {
  1261. req->outstanding_cmds[handle] = NULL;
  1262. req->cnt += req_cnt;
  1263. }
  1264. /* Cleanup will be performed by the caller (queuecommand) */
  1265. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1266. DEBUG18(qla_printk(KERN_INFO, ha,
  1267. "CMD sent FAILED SCSI prot_op:%02x\n", scsi_get_prot_op(cmd)));
  1268. return QLA_FUNCTION_FAILED;
  1269. }
  1270. static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
  1271. {
  1272. struct scsi_cmnd *cmd = sp->cmd;
  1273. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1274. int affinity = cmd->request->cpu;
  1275. if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
  1276. affinity < ha->max_rsp_queues - 1)
  1277. *rsp = ha->rsp_q_map[affinity + 1];
  1278. else
  1279. *rsp = ha->rsp_q_map[0];
  1280. }
  1281. /* Generic Control-SRB manipulation functions. */
  1282. void *
  1283. qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
  1284. {
  1285. struct qla_hw_data *ha = vha->hw;
  1286. struct req_que *req = ha->req_q_map[0];
  1287. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1288. uint32_t index, handle;
  1289. request_t *pkt;
  1290. uint16_t cnt, req_cnt;
  1291. pkt = NULL;
  1292. req_cnt = 1;
  1293. handle = 0;
  1294. if (!sp)
  1295. goto skip_cmd_array;
  1296. /* Check for room in outstanding command list. */
  1297. handle = req->current_outstanding_cmd;
  1298. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1299. handle++;
  1300. if (handle == MAX_OUTSTANDING_COMMANDS)
  1301. handle = 1;
  1302. if (!req->outstanding_cmds[handle])
  1303. break;
  1304. }
  1305. if (index == MAX_OUTSTANDING_COMMANDS)
  1306. goto queuing_error;
  1307. /* Prep command array. */
  1308. req->current_outstanding_cmd = handle;
  1309. req->outstanding_cmds[handle] = sp;
  1310. sp->handle = handle;
  1311. skip_cmd_array:
  1312. /* Check for room on request queue. */
  1313. if (req->cnt < req_cnt) {
  1314. if (ha->mqenable)
  1315. cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
  1316. else if (IS_QLA82XX(ha))
  1317. cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
  1318. else if (IS_FWI2_CAPABLE(ha))
  1319. cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
  1320. else
  1321. cnt = qla2x00_debounce_register(
  1322. ISP_REQ_Q_OUT(ha, &reg->isp));
  1323. if (req->ring_index < cnt)
  1324. req->cnt = cnt - req->ring_index;
  1325. else
  1326. req->cnt = req->length -
  1327. (req->ring_index - cnt);
  1328. }
  1329. if (req->cnt < req_cnt)
  1330. goto queuing_error;
  1331. /* Prep packet */
  1332. req->cnt -= req_cnt;
  1333. pkt = req->ring_ptr;
  1334. memset(pkt, 0, REQUEST_ENTRY_SIZE);
  1335. pkt->entry_count = req_cnt;
  1336. pkt->handle = handle;
  1337. queuing_error:
  1338. return pkt;
  1339. }
  1340. static void
  1341. qla2x00_start_iocbs(srb_t *sp)
  1342. {
  1343. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1344. struct req_que *req = ha->req_q_map[0];
  1345. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1346. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  1347. if (IS_QLA82XX(ha)) {
  1348. qla82xx_start_iocbs(sp);
  1349. } else {
  1350. /* Adjust ring index. */
  1351. req->ring_index++;
  1352. if (req->ring_index == req->length) {
  1353. req->ring_index = 0;
  1354. req->ring_ptr = req->ring;
  1355. } else
  1356. req->ring_ptr++;
  1357. /* Set chip new ring index. */
  1358. if (ha->mqenable) {
  1359. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  1360. RD_REG_DWORD(&ioreg->hccr);
  1361. } else if (IS_QLA82XX(ha)) {
  1362. qla82xx_start_iocbs(sp);
  1363. } else if (IS_FWI2_CAPABLE(ha)) {
  1364. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  1365. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  1366. } else {
  1367. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  1368. req->ring_index);
  1369. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  1370. }
  1371. }
  1372. }
  1373. static void
  1374. qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1375. {
  1376. struct srb_ctx *ctx = sp->ctx;
  1377. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1378. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1379. logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
  1380. if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
  1381. logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
  1382. if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
  1383. logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
  1384. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1385. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1386. logio->port_id[1] = sp->fcport->d_id.b.area;
  1387. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1388. logio->vp_index = sp->fcport->vp_idx;
  1389. }
  1390. static void
  1391. qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
  1392. {
  1393. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1394. struct srb_ctx *ctx = sp->ctx;
  1395. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1396. uint16_t opts;
  1397. mbx->entry_type = MBX_IOCB_TYPE;
  1398. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1399. mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
  1400. opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
  1401. opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
  1402. if (HAS_EXTENDED_IDS(ha)) {
  1403. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1404. mbx->mb10 = cpu_to_le16(opts);
  1405. } else {
  1406. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
  1407. }
  1408. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1409. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1410. sp->fcport->d_id.b.al_pa);
  1411. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1412. }
  1413. static void
  1414. qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1415. {
  1416. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1417. logio->control_flags =
  1418. cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
  1419. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1420. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1421. logio->port_id[1] = sp->fcport->d_id.b.area;
  1422. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1423. logio->vp_index = sp->fcport->vp_idx;
  1424. }
  1425. static void
  1426. qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
  1427. {
  1428. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1429. mbx->entry_type = MBX_IOCB_TYPE;
  1430. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1431. mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
  1432. mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
  1433. cpu_to_le16(sp->fcport->loop_id):
  1434. cpu_to_le16(sp->fcport->loop_id << 8);
  1435. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1436. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1437. sp->fcport->d_id.b.al_pa);
  1438. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1439. /* Implicit: mbx->mbx10 = 0. */
  1440. }
  1441. static void
  1442. qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1443. {
  1444. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1445. logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
  1446. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1447. logio->vp_index = sp->fcport->vp_idx;
  1448. }
  1449. static void
  1450. qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
  1451. {
  1452. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1453. mbx->entry_type = MBX_IOCB_TYPE;
  1454. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1455. mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
  1456. if (HAS_EXTENDED_IDS(ha)) {
  1457. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1458. mbx->mb10 = cpu_to_le16(BIT_0);
  1459. } else {
  1460. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
  1461. }
  1462. mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
  1463. mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
  1464. mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
  1465. mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
  1466. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1467. }
  1468. static void
  1469. qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
  1470. {
  1471. uint32_t flags;
  1472. unsigned int lun;
  1473. struct fc_port *fcport = sp->fcport;
  1474. scsi_qla_host_t *vha = fcport->vha;
  1475. struct qla_hw_data *ha = vha->hw;
  1476. struct srb_ctx *ctx = sp->ctx;
  1477. struct srb_iocb *iocb = ctx->u.iocb_cmd;
  1478. struct req_que *req = vha->req;
  1479. flags = iocb->u.tmf.flags;
  1480. lun = iocb->u.tmf.lun;
  1481. tsk->entry_type = TSK_MGMT_IOCB_TYPE;
  1482. tsk->entry_count = 1;
  1483. tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
  1484. tsk->nport_handle = cpu_to_le16(fcport->loop_id);
  1485. tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
  1486. tsk->control_flags = cpu_to_le32(flags);
  1487. tsk->port_id[0] = fcport->d_id.b.al_pa;
  1488. tsk->port_id[1] = fcport->d_id.b.area;
  1489. tsk->port_id[2] = fcport->d_id.b.domain;
  1490. tsk->vp_index = fcport->vp_idx;
  1491. if (flags == TCF_LUN_RESET) {
  1492. int_to_scsilun(lun, &tsk->lun);
  1493. host_to_fcp_swap((uint8_t *)&tsk->lun,
  1494. sizeof(tsk->lun));
  1495. }
  1496. }
  1497. static void
  1498. qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
  1499. {
  1500. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1501. els_iocb->entry_type = ELS_IOCB_TYPE;
  1502. els_iocb->entry_count = 1;
  1503. els_iocb->sys_define = 0;
  1504. els_iocb->entry_status = 0;
  1505. els_iocb->handle = sp->handle;
  1506. els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1507. els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1508. els_iocb->vp_index = sp->fcport->vp_idx;
  1509. els_iocb->sof_type = EST_SOFI3;
  1510. els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1511. els_iocb->opcode =
  1512. (((struct srb_ctx *)sp->ctx)->type == SRB_ELS_CMD_RPT) ?
  1513. bsg_job->request->rqst_data.r_els.els_code :
  1514. bsg_job->request->rqst_data.h_els.command_code;
  1515. els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
  1516. els_iocb->port_id[1] = sp->fcport->d_id.b.area;
  1517. els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
  1518. els_iocb->control_flags = 0;
  1519. els_iocb->rx_byte_count =
  1520. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1521. els_iocb->tx_byte_count =
  1522. cpu_to_le32(bsg_job->request_payload.payload_len);
  1523. els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1524. (bsg_job->request_payload.sg_list)));
  1525. els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1526. (bsg_job->request_payload.sg_list)));
  1527. els_iocb->tx_len = cpu_to_le32(sg_dma_len
  1528. (bsg_job->request_payload.sg_list));
  1529. els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1530. (bsg_job->reply_payload.sg_list)));
  1531. els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1532. (bsg_job->reply_payload.sg_list)));
  1533. els_iocb->rx_len = cpu_to_le32(sg_dma_len
  1534. (bsg_job->reply_payload.sg_list));
  1535. }
  1536. static void
  1537. qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
  1538. {
  1539. uint16_t avail_dsds;
  1540. uint32_t *cur_dsd;
  1541. struct scatterlist *sg;
  1542. int index;
  1543. uint16_t tot_dsds;
  1544. scsi_qla_host_t *vha = sp->fcport->vha;
  1545. struct qla_hw_data *ha = vha->hw;
  1546. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1547. int loop_iterartion = 0;
  1548. int cont_iocb_prsnt = 0;
  1549. int entry_count = 1;
  1550. memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
  1551. ct_iocb->entry_type = CT_IOCB_TYPE;
  1552. ct_iocb->entry_status = 0;
  1553. ct_iocb->handle1 = sp->handle;
  1554. SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
  1555. ct_iocb->status = __constant_cpu_to_le16(0);
  1556. ct_iocb->control_flags = __constant_cpu_to_le16(0);
  1557. ct_iocb->timeout = 0;
  1558. ct_iocb->cmd_dsd_count =
  1559. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1560. ct_iocb->total_dsd_count =
  1561. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
  1562. ct_iocb->req_bytecount =
  1563. cpu_to_le32(bsg_job->request_payload.payload_len);
  1564. ct_iocb->rsp_bytecount =
  1565. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1566. ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
  1567. (bsg_job->request_payload.sg_list)));
  1568. ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
  1569. (bsg_job->request_payload.sg_list)));
  1570. ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
  1571. ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
  1572. (bsg_job->reply_payload.sg_list)));
  1573. ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
  1574. (bsg_job->reply_payload.sg_list)));
  1575. ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
  1576. avail_dsds = 1;
  1577. cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
  1578. index = 0;
  1579. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1580. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1581. dma_addr_t sle_dma;
  1582. cont_a64_entry_t *cont_pkt;
  1583. /* Allocate additional continuation packets? */
  1584. if (avail_dsds == 0) {
  1585. /*
  1586. * Five DSDs are available in the Cont.
  1587. * Type 1 IOCB.
  1588. */
  1589. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  1590. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1591. avail_dsds = 5;
  1592. cont_iocb_prsnt = 1;
  1593. entry_count++;
  1594. }
  1595. sle_dma = sg_dma_address(sg);
  1596. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1597. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1598. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1599. loop_iterartion++;
  1600. avail_dsds--;
  1601. }
  1602. ct_iocb->entry_count = entry_count;
  1603. }
  1604. static void
  1605. qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
  1606. {
  1607. uint16_t avail_dsds;
  1608. uint32_t *cur_dsd;
  1609. struct scatterlist *sg;
  1610. int index;
  1611. uint16_t tot_dsds;
  1612. scsi_qla_host_t *vha = sp->fcport->vha;
  1613. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1614. int loop_iterartion = 0;
  1615. int cont_iocb_prsnt = 0;
  1616. int entry_count = 1;
  1617. ct_iocb->entry_type = CT_IOCB_TYPE;
  1618. ct_iocb->entry_status = 0;
  1619. ct_iocb->sys_define = 0;
  1620. ct_iocb->handle = sp->handle;
  1621. ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1622. ct_iocb->vp_index = sp->fcport->vp_idx;
  1623. ct_iocb->comp_status = __constant_cpu_to_le16(0);
  1624. ct_iocb->cmd_dsd_count =
  1625. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1626. ct_iocb->timeout = 0;
  1627. ct_iocb->rsp_dsd_count =
  1628. __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1629. ct_iocb->rsp_byte_count =
  1630. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1631. ct_iocb->cmd_byte_count =
  1632. cpu_to_le32(bsg_job->request_payload.payload_len);
  1633. ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
  1634. (bsg_job->request_payload.sg_list)));
  1635. ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
  1636. (bsg_job->request_payload.sg_list)));
  1637. ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
  1638. (bsg_job->request_payload.sg_list));
  1639. avail_dsds = 1;
  1640. cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
  1641. index = 0;
  1642. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1643. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1644. dma_addr_t sle_dma;
  1645. cont_a64_entry_t *cont_pkt;
  1646. /* Allocate additional continuation packets? */
  1647. if (avail_dsds == 0) {
  1648. /*
  1649. * Five DSDs are available in the Cont.
  1650. * Type 1 IOCB.
  1651. */
  1652. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  1653. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1654. avail_dsds = 5;
  1655. cont_iocb_prsnt = 1;
  1656. entry_count++;
  1657. }
  1658. sle_dma = sg_dma_address(sg);
  1659. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1660. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1661. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1662. loop_iterartion++;
  1663. avail_dsds--;
  1664. }
  1665. ct_iocb->entry_count = entry_count;
  1666. }
  1667. int
  1668. qla2x00_start_sp(srb_t *sp)
  1669. {
  1670. int rval;
  1671. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1672. void *pkt;
  1673. struct srb_ctx *ctx = sp->ctx;
  1674. unsigned long flags;
  1675. rval = QLA_FUNCTION_FAILED;
  1676. spin_lock_irqsave(&ha->hardware_lock, flags);
  1677. pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
  1678. if (!pkt)
  1679. goto done;
  1680. rval = QLA_SUCCESS;
  1681. switch (ctx->type) {
  1682. case SRB_LOGIN_CMD:
  1683. IS_FWI2_CAPABLE(ha) ?
  1684. qla24xx_login_iocb(sp, pkt) :
  1685. qla2x00_login_iocb(sp, pkt);
  1686. break;
  1687. case SRB_LOGOUT_CMD:
  1688. IS_FWI2_CAPABLE(ha) ?
  1689. qla24xx_logout_iocb(sp, pkt) :
  1690. qla2x00_logout_iocb(sp, pkt);
  1691. break;
  1692. case SRB_ELS_CMD_RPT:
  1693. case SRB_ELS_CMD_HST:
  1694. qla24xx_els_iocb(sp, pkt);
  1695. break;
  1696. case SRB_CT_CMD:
  1697. IS_FWI2_CAPABLE(ha) ?
  1698. qla24xx_ct_iocb(sp, pkt) :
  1699. qla2x00_ct_iocb(sp, pkt);
  1700. break;
  1701. case SRB_ADISC_CMD:
  1702. IS_FWI2_CAPABLE(ha) ?
  1703. qla24xx_adisc_iocb(sp, pkt) :
  1704. qla2x00_adisc_iocb(sp, pkt);
  1705. break;
  1706. case SRB_TM_CMD:
  1707. qla24xx_tm_iocb(sp, pkt);
  1708. break;
  1709. default:
  1710. break;
  1711. }
  1712. wmb();
  1713. qla2x00_start_iocbs(sp);
  1714. done:
  1715. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1716. return rval;
  1717. }