libiscsi.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.h>
  28. #include <asm/unaligned.h>
  29. #include <net/tcp.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_eh.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/iscsi_proto.h>
  37. #include <scsi/scsi_transport.h>
  38. #include <scsi/scsi_transport_iscsi.h>
  39. #include <scsi/libiscsi.h>
  40. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  41. #define SNA32_CHECK 2147483648UL
  42. static int iscsi_sna_lt(u32 n1, u32 n2)
  43. {
  44. return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  45. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  46. }
  47. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  48. static int iscsi_sna_lte(u32 n1, u32 n2)
  49. {
  50. return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  51. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  52. }
  53. void
  54. iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  55. {
  56. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  57. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  58. /*
  59. * standard specifies this check for when to update expected and
  60. * max sequence numbers
  61. */
  62. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  63. return;
  64. if (exp_cmdsn != session->exp_cmdsn &&
  65. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  66. session->exp_cmdsn = exp_cmdsn;
  67. if (max_cmdsn != session->max_cmdsn &&
  68. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  69. session->max_cmdsn = max_cmdsn;
  70. /*
  71. * if the window closed with IO queued, then kick the
  72. * xmit thread
  73. */
  74. if (!list_empty(&session->leadconn->xmitqueue) ||
  75. !list_empty(&session->leadconn->mgmtqueue)) {
  76. if (!(session->tt->caps & CAP_DATA_PATH_OFFLOAD))
  77. scsi_queue_work(session->host,
  78. &session->leadconn->xmitwork);
  79. }
  80. }
  81. }
  82. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  83. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  84. struct iscsi_data *hdr)
  85. {
  86. struct iscsi_conn *conn = ctask->conn;
  87. memset(hdr, 0, sizeof(struct iscsi_data));
  88. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  89. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  90. ctask->unsol_datasn++;
  91. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  92. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  93. hdr->itt = ctask->hdr->itt;
  94. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  95. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  96. if (ctask->unsol_count > conn->max_xmit_dlength) {
  97. hton24(hdr->dlength, conn->max_xmit_dlength);
  98. ctask->data_count = conn->max_xmit_dlength;
  99. ctask->unsol_offset += ctask->data_count;
  100. hdr->flags = 0;
  101. } else {
  102. hton24(hdr->dlength, ctask->unsol_count);
  103. ctask->data_count = ctask->unsol_count;
  104. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  105. }
  106. }
  107. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  108. static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
  109. {
  110. unsigned exp_len = ctask->hdr_len + len;
  111. if (exp_len > ctask->hdr_max) {
  112. WARN_ON(1);
  113. return -EINVAL;
  114. }
  115. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  116. ctask->hdr_len = exp_len;
  117. return 0;
  118. }
  119. /*
  120. * make an extended cdb AHS
  121. */
  122. static int iscsi_prep_ecdb_ahs(struct iscsi_cmd_task *ctask)
  123. {
  124. struct scsi_cmnd *cmd = ctask->sc;
  125. unsigned rlen, pad_len;
  126. unsigned short ahslength;
  127. struct iscsi_ecdb_ahdr *ecdb_ahdr;
  128. int rc;
  129. ecdb_ahdr = iscsi_next_hdr(ctask);
  130. rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
  131. BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
  132. ahslength = rlen + sizeof(ecdb_ahdr->reserved);
  133. pad_len = iscsi_padding(rlen);
  134. rc = iscsi_add_hdr(ctask, sizeof(ecdb_ahdr->ahslength) +
  135. sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
  136. if (rc)
  137. return rc;
  138. if (pad_len)
  139. memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
  140. ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
  141. ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
  142. ecdb_ahdr->reserved = 0;
  143. memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
  144. debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
  145. "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
  146. cmd->cmd_len, rlen, pad_len, ahslength, ctask->hdr_len);
  147. return 0;
  148. }
  149. static int iscsi_prep_bidi_ahs(struct iscsi_cmd_task *ctask)
  150. {
  151. struct scsi_cmnd *sc = ctask->sc;
  152. struct iscsi_rlength_ahdr *rlen_ahdr;
  153. int rc;
  154. rlen_ahdr = iscsi_next_hdr(ctask);
  155. rc = iscsi_add_hdr(ctask, sizeof(*rlen_ahdr));
  156. if (rc)
  157. return rc;
  158. rlen_ahdr->ahslength =
  159. cpu_to_be16(sizeof(rlen_ahdr->read_length) +
  160. sizeof(rlen_ahdr->reserved));
  161. rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
  162. rlen_ahdr->reserved = 0;
  163. rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
  164. debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
  165. "rlen_ahdr->ahslength(%d)\n",
  166. be32_to_cpu(rlen_ahdr->read_length),
  167. be16_to_cpu(rlen_ahdr->ahslength));
  168. return 0;
  169. }
  170. /**
  171. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  172. * @ctask: iscsi cmd task
  173. *
  174. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  175. * fields like dlength or final based on how much data it sends
  176. */
  177. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  178. {
  179. struct iscsi_conn *conn = ctask->conn;
  180. struct iscsi_session *session = conn->session;
  181. struct iscsi_cmd *hdr = ctask->hdr;
  182. struct scsi_cmnd *sc = ctask->sc;
  183. unsigned hdrlength, cmd_len;
  184. int rc;
  185. ctask->hdr_len = 0;
  186. rc = iscsi_add_hdr(ctask, sizeof(*hdr));
  187. if (rc)
  188. return rc;
  189. hdr->opcode = ISCSI_OP_SCSI_CMD;
  190. hdr->flags = ISCSI_ATTR_SIMPLE;
  191. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  192. hdr->itt = build_itt(ctask->itt, session->age);
  193. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  194. session->cmdsn++;
  195. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  196. cmd_len = sc->cmd_len;
  197. if (cmd_len < ISCSI_CDB_SIZE)
  198. memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
  199. else if (cmd_len > ISCSI_CDB_SIZE) {
  200. rc = iscsi_prep_ecdb_ahs(ctask);
  201. if (rc)
  202. return rc;
  203. cmd_len = ISCSI_CDB_SIZE;
  204. }
  205. memcpy(hdr->cdb, sc->cmnd, cmd_len);
  206. ctask->imm_count = 0;
  207. if (scsi_bidi_cmnd(sc)) {
  208. hdr->flags |= ISCSI_FLAG_CMD_READ;
  209. rc = iscsi_prep_bidi_ahs(ctask);
  210. if (rc)
  211. return rc;
  212. }
  213. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  214. unsigned out_len = scsi_out(sc)->length;
  215. hdr->data_length = cpu_to_be32(out_len);
  216. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  217. /*
  218. * Write counters:
  219. *
  220. * imm_count bytes to be sent right after
  221. * SCSI PDU Header
  222. *
  223. * unsol_count bytes(as Data-Out) to be sent
  224. * without R2T ack right after
  225. * immediate data
  226. *
  227. * r2t_data_count bytes to be sent via R2T ack's
  228. *
  229. * pad_count bytes to be sent as zero-padding
  230. */
  231. ctask->unsol_count = 0;
  232. ctask->unsol_offset = 0;
  233. ctask->unsol_datasn = 0;
  234. if (session->imm_data_en) {
  235. if (out_len >= session->first_burst)
  236. ctask->imm_count = min(session->first_burst,
  237. conn->max_xmit_dlength);
  238. else
  239. ctask->imm_count = min(out_len,
  240. conn->max_xmit_dlength);
  241. hton24(hdr->dlength, ctask->imm_count);
  242. } else
  243. zero_data(hdr->dlength);
  244. if (!session->initial_r2t_en) {
  245. ctask->unsol_count = min(session->first_burst, out_len)
  246. - ctask->imm_count;
  247. ctask->unsol_offset = ctask->imm_count;
  248. }
  249. if (!ctask->unsol_count)
  250. /* No unsolicit Data-Out's */
  251. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  252. } else {
  253. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  254. zero_data(hdr->dlength);
  255. hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
  256. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  257. hdr->flags |= ISCSI_FLAG_CMD_READ;
  258. }
  259. /* calculate size of additional header segments (AHSs) */
  260. hdrlength = ctask->hdr_len - sizeof(*hdr);
  261. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  262. hdrlength /= ISCSI_PAD_LEN;
  263. WARN_ON(hdrlength >= 256);
  264. hdr->hlength = hdrlength & 0xFF;
  265. if (conn->session->tt->init_cmd_task &&
  266. conn->session->tt->init_cmd_task(ctask))
  267. return -EIO;
  268. ctask->state = ISCSI_TASK_RUNNING;
  269. list_move_tail(&ctask->running, &conn->run_list);
  270. conn->scsicmd_pdus_cnt++;
  271. debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x "
  272. "len %d bidi_len %d cmdsn %d win %d]\n",
  273. scsi_bidi_cmnd(sc) ? "bidirectional" :
  274. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  275. conn->id, sc, sc->cmnd[0], ctask->itt,
  276. scsi_bufflen(sc), scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
  277. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  278. return 0;
  279. }
  280. /**
  281. * iscsi_complete_command - return command back to scsi-ml
  282. * @ctask: iscsi cmd task
  283. *
  284. * Must be called with session lock.
  285. * This function returns the scsi command to scsi-ml and returns
  286. * the cmd task to the pool of available cmd tasks.
  287. */
  288. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  289. {
  290. struct iscsi_conn *conn = ctask->conn;
  291. struct iscsi_session *session = conn->session;
  292. struct scsi_cmnd *sc = ctask->sc;
  293. ctask->state = ISCSI_TASK_COMPLETED;
  294. ctask->sc = NULL;
  295. /* SCSI eh reuses commands to verify us */
  296. sc->SCp.ptr = NULL;
  297. if (conn->ctask == ctask)
  298. conn->ctask = NULL;
  299. list_del_init(&ctask->running);
  300. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  301. if (sc->scsi_done)
  302. sc->scsi_done(sc);
  303. }
  304. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  305. {
  306. atomic_inc(&ctask->refcount);
  307. }
  308. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  309. {
  310. if (atomic_dec_and_test(&ctask->refcount))
  311. iscsi_complete_command(ctask);
  312. }
  313. /*
  314. * session lock must be held
  315. */
  316. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  317. int err)
  318. {
  319. struct scsi_cmnd *sc;
  320. sc = ctask->sc;
  321. if (!sc)
  322. return;
  323. if (ctask->state == ISCSI_TASK_PENDING)
  324. /*
  325. * cmd never made it to the xmit thread, so we should not count
  326. * the cmd in the sequencing
  327. */
  328. conn->session->queued_cmdsn--;
  329. else
  330. conn->session->tt->cleanup_cmd_task(conn, ctask);
  331. sc->result = err;
  332. if (!scsi_bidi_cmnd(sc))
  333. scsi_set_resid(sc, scsi_bufflen(sc));
  334. else {
  335. scsi_out(sc)->resid = scsi_out(sc)->length;
  336. scsi_in(sc)->resid = scsi_in(sc)->length;
  337. }
  338. if (conn->ctask == ctask)
  339. conn->ctask = NULL;
  340. /* release ref from queuecommand */
  341. __iscsi_put_ctask(ctask);
  342. }
  343. /**
  344. * iscsi_free_mgmt_task - return mgmt task back to pool
  345. * @conn: iscsi connection
  346. * @mtask: mtask
  347. *
  348. * Must be called with session lock.
  349. */
  350. void iscsi_free_mgmt_task(struct iscsi_conn *conn,
  351. struct iscsi_mgmt_task *mtask)
  352. {
  353. list_del_init(&mtask->running);
  354. if (conn->login_mtask == mtask)
  355. return;
  356. if (conn->ping_mtask == mtask)
  357. conn->ping_mtask = NULL;
  358. __kfifo_put(conn->session->mgmtpool.queue,
  359. (void*)&mtask, sizeof(void*));
  360. }
  361. EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
  362. static int iscsi_prep_mtask(struct iscsi_conn *conn,
  363. struct iscsi_mgmt_task *mtask)
  364. {
  365. struct iscsi_session *session = conn->session;
  366. struct iscsi_hdr *hdr = mtask->hdr;
  367. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  368. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  369. return -ENOTCONN;
  370. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  371. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  372. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  373. /*
  374. * pre-format CmdSN for outgoing PDU.
  375. */
  376. nop->cmdsn = cpu_to_be32(session->cmdsn);
  377. if (hdr->itt != RESERVED_ITT) {
  378. hdr->itt = build_itt(mtask->itt, session->age);
  379. /*
  380. * TODO: We always use immediate, so we never hit this.
  381. * If we start to send tmfs or nops as non-immediate then
  382. * we should start checking the cmdsn numbers for mgmt tasks.
  383. */
  384. if (conn->c_stage == ISCSI_CONN_STARTED &&
  385. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  386. session->queued_cmdsn++;
  387. session->cmdsn++;
  388. }
  389. }
  390. if (session->tt->init_mgmt_task)
  391. session->tt->init_mgmt_task(conn, mtask);
  392. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  393. session->state = ISCSI_STATE_LOGGING_OUT;
  394. list_move_tail(&mtask->running, &conn->mgmt_run_list);
  395. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  396. hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
  397. mtask->data_count);
  398. return 0;
  399. }
  400. static struct iscsi_mgmt_task *
  401. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  402. char *data, uint32_t data_size)
  403. {
  404. struct iscsi_session *session = conn->session;
  405. struct iscsi_mgmt_task *mtask;
  406. if (session->state == ISCSI_STATE_TERMINATE)
  407. return NULL;
  408. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  409. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  410. /*
  411. * Login and Text are sent serially, in
  412. * request-followed-by-response sequence.
  413. * Same mtask can be used. Same ITT must be used.
  414. * Note that login_mtask is preallocated at conn_create().
  415. */
  416. mtask = conn->login_mtask;
  417. else {
  418. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  419. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  420. if (!__kfifo_get(session->mgmtpool.queue,
  421. (void*)&mtask, sizeof(void*)))
  422. return NULL;
  423. if ((hdr->opcode == (ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE)) &&
  424. hdr->ttt == RESERVED_ITT) {
  425. conn->ping_mtask = mtask;
  426. conn->last_ping = jiffies;
  427. }
  428. }
  429. if (data_size) {
  430. memcpy(mtask->data, data, data_size);
  431. mtask->data_count = data_size;
  432. } else
  433. mtask->data_count = 0;
  434. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  435. INIT_LIST_HEAD(&mtask->running);
  436. list_add_tail(&mtask->running, &conn->mgmtqueue);
  437. if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
  438. if (iscsi_prep_mtask(conn, mtask)) {
  439. iscsi_free_mgmt_task(conn, mtask);
  440. return NULL;
  441. }
  442. if (session->tt->xmit_mgmt_task(conn, mtask))
  443. mtask = NULL;
  444. } else
  445. scsi_queue_work(conn->session->host, &conn->xmitwork);
  446. return mtask;
  447. }
  448. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  449. char *data, uint32_t data_size)
  450. {
  451. struct iscsi_conn *conn = cls_conn->dd_data;
  452. struct iscsi_session *session = conn->session;
  453. int err = 0;
  454. spin_lock_bh(&session->lock);
  455. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  456. err = -EPERM;
  457. spin_unlock_bh(&session->lock);
  458. return err;
  459. }
  460. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  461. /**
  462. * iscsi_cmd_rsp - SCSI Command Response processing
  463. * @conn: iscsi connection
  464. * @hdr: iscsi header
  465. * @ctask: scsi command task
  466. * @data: cmd data buffer
  467. * @datalen: len of buffer
  468. *
  469. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  470. * then completes the command and task.
  471. **/
  472. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  473. struct iscsi_cmd_task *ctask, char *data,
  474. int datalen)
  475. {
  476. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  477. struct iscsi_session *session = conn->session;
  478. struct scsi_cmnd *sc = ctask->sc;
  479. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  480. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  481. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  482. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  483. sc->result = DID_ERROR << 16;
  484. goto out;
  485. }
  486. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  487. uint16_t senselen;
  488. if (datalen < 2) {
  489. invalid_datalen:
  490. iscsi_conn_printk(KERN_ERR, conn,
  491. "Got CHECK_CONDITION but invalid data "
  492. "buffer size of %d\n", datalen);
  493. sc->result = DID_BAD_TARGET << 16;
  494. goto out;
  495. }
  496. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  497. if (datalen < senselen)
  498. goto invalid_datalen;
  499. memcpy(sc->sense_buffer, data + 2,
  500. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  501. debug_scsi("copied %d bytes of sense\n",
  502. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  503. }
  504. if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  505. ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
  506. int res_count = be32_to_cpu(rhdr->bi_residual_count);
  507. if (scsi_bidi_cmnd(sc) && res_count > 0 &&
  508. (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
  509. res_count <= scsi_in(sc)->length))
  510. scsi_in(sc)->resid = res_count;
  511. else
  512. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  513. }
  514. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  515. ISCSI_FLAG_CMD_OVERFLOW)) {
  516. int res_count = be32_to_cpu(rhdr->residual_count);
  517. if (res_count > 0 &&
  518. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  519. res_count <= scsi_bufflen(sc)))
  520. /* write side for bidi or uni-io set_resid */
  521. scsi_set_resid(sc, res_count);
  522. else
  523. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  524. }
  525. out:
  526. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  527. (long)sc, sc->result, ctask->itt);
  528. conn->scsirsp_pdus_cnt++;
  529. __iscsi_put_ctask(ctask);
  530. }
  531. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  532. {
  533. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  534. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  535. conn->tmfrsp_pdus_cnt++;
  536. if (conn->tmf_state != TMF_QUEUED)
  537. return;
  538. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  539. conn->tmf_state = TMF_SUCCESS;
  540. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  541. conn->tmf_state = TMF_NOT_FOUND;
  542. else
  543. conn->tmf_state = TMF_FAILED;
  544. wake_up(&conn->ehwait);
  545. }
  546. static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  547. {
  548. struct iscsi_nopout hdr;
  549. struct iscsi_mgmt_task *mtask;
  550. if (!rhdr && conn->ping_mtask)
  551. return;
  552. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  553. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  554. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  555. if (rhdr) {
  556. memcpy(hdr.lun, rhdr->lun, 8);
  557. hdr.ttt = rhdr->ttt;
  558. hdr.itt = RESERVED_ITT;
  559. } else
  560. hdr.ttt = RESERVED_ITT;
  561. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  562. if (!mtask)
  563. iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
  564. }
  565. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  566. char *data, int datalen)
  567. {
  568. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  569. struct iscsi_hdr rejected_pdu;
  570. uint32_t itt;
  571. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  572. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  573. if (ntoh24(reject->dlength) > datalen)
  574. return ISCSI_ERR_PROTO;
  575. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  576. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  577. itt = get_itt(rejected_pdu.itt);
  578. iscsi_conn_printk(KERN_ERR, conn,
  579. "itt 0x%x had pdu (op 0x%x) rejected "
  580. "due to DataDigest error.\n", itt,
  581. rejected_pdu.opcode);
  582. }
  583. }
  584. return 0;
  585. }
  586. /**
  587. * __iscsi_complete_pdu - complete pdu
  588. * @conn: iscsi conn
  589. * @hdr: iscsi header
  590. * @data: data buffer
  591. * @datalen: len of data buffer
  592. *
  593. * Completes pdu processing by freeing any resources allocated at
  594. * queuecommand or send generic. session lock must be held and verify
  595. * itt must have been called.
  596. */
  597. static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  598. char *data, int datalen)
  599. {
  600. struct iscsi_session *session = conn->session;
  601. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  602. struct iscsi_cmd_task *ctask;
  603. struct iscsi_mgmt_task *mtask;
  604. uint32_t itt;
  605. conn->last_recv = jiffies;
  606. rc = iscsi_verify_itt(conn, hdr->itt);
  607. if (rc)
  608. return rc;
  609. if (hdr->itt != RESERVED_ITT)
  610. itt = get_itt(hdr->itt);
  611. else
  612. itt = ~0U;
  613. if (itt < session->cmds_max) {
  614. ctask = session->cmds[itt];
  615. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  616. opcode, conn->id, ctask->itt, datalen);
  617. switch(opcode) {
  618. case ISCSI_OP_SCSI_CMD_RSP:
  619. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  620. iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  621. datalen);
  622. break;
  623. case ISCSI_OP_SCSI_DATA_IN:
  624. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  625. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  626. conn->scsirsp_pdus_cnt++;
  627. __iscsi_put_ctask(ctask);
  628. }
  629. break;
  630. case ISCSI_OP_R2T:
  631. /* LLD handles this for now */
  632. break;
  633. default:
  634. rc = ISCSI_ERR_BAD_OPCODE;
  635. break;
  636. }
  637. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  638. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  639. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  640. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  641. opcode, conn->id, mtask->itt, datalen);
  642. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  643. switch(opcode) {
  644. case ISCSI_OP_LOGOUT_RSP:
  645. if (datalen) {
  646. rc = ISCSI_ERR_PROTO;
  647. break;
  648. }
  649. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  650. /* fall through */
  651. case ISCSI_OP_LOGIN_RSP:
  652. case ISCSI_OP_TEXT_RSP:
  653. /*
  654. * login related PDU's exp_statsn is handled in
  655. * userspace
  656. */
  657. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  658. rc = ISCSI_ERR_CONN_FAILED;
  659. iscsi_free_mgmt_task(conn, mtask);
  660. break;
  661. case ISCSI_OP_SCSI_TMFUNC_RSP:
  662. if (datalen) {
  663. rc = ISCSI_ERR_PROTO;
  664. break;
  665. }
  666. iscsi_tmf_rsp(conn, hdr);
  667. iscsi_free_mgmt_task(conn, mtask);
  668. break;
  669. case ISCSI_OP_NOOP_IN:
  670. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
  671. datalen) {
  672. rc = ISCSI_ERR_PROTO;
  673. break;
  674. }
  675. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  676. if (conn->ping_mtask != mtask) {
  677. /*
  678. * If this is not in response to one of our
  679. * nops then it must be from userspace.
  680. */
  681. if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
  682. datalen))
  683. rc = ISCSI_ERR_CONN_FAILED;
  684. } else
  685. mod_timer(&conn->transport_timer,
  686. jiffies + conn->recv_timeout);
  687. iscsi_free_mgmt_task(conn, mtask);
  688. break;
  689. default:
  690. rc = ISCSI_ERR_BAD_OPCODE;
  691. break;
  692. }
  693. } else if (itt == ~0U) {
  694. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  695. switch(opcode) {
  696. case ISCSI_OP_NOOP_IN:
  697. if (datalen) {
  698. rc = ISCSI_ERR_PROTO;
  699. break;
  700. }
  701. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  702. break;
  703. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  704. break;
  705. case ISCSI_OP_REJECT:
  706. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  707. break;
  708. case ISCSI_OP_ASYNC_EVENT:
  709. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  710. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  711. rc = ISCSI_ERR_CONN_FAILED;
  712. break;
  713. default:
  714. rc = ISCSI_ERR_BAD_OPCODE;
  715. break;
  716. }
  717. } else
  718. rc = ISCSI_ERR_BAD_ITT;
  719. return rc;
  720. }
  721. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  722. char *data, int datalen)
  723. {
  724. int rc;
  725. spin_lock(&conn->session->lock);
  726. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  727. spin_unlock(&conn->session->lock);
  728. return rc;
  729. }
  730. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  731. int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
  732. {
  733. struct iscsi_session *session = conn->session;
  734. struct iscsi_cmd_task *ctask;
  735. if (itt == RESERVED_ITT)
  736. return 0;
  737. if (((__force u32)itt & ISCSI_AGE_MASK) !=
  738. (session->age << ISCSI_AGE_SHIFT)) {
  739. iscsi_conn_printk(KERN_ERR, conn,
  740. "received itt %x expected session age (%x)\n",
  741. (__force u32)itt,
  742. session->age & ISCSI_AGE_MASK);
  743. return ISCSI_ERR_BAD_ITT;
  744. }
  745. if (itt < session->cmds_max) {
  746. ctask = session->cmds[itt];
  747. if (!ctask->sc) {
  748. iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
  749. "with itt 0x%x\n", ctask->itt);
  750. /* force drop */
  751. return ISCSI_ERR_NO_SCSI_CMD;
  752. }
  753. if (ctask->sc->SCp.phase != session->age) {
  754. iscsi_conn_printk(KERN_ERR, conn,
  755. "iscsi: ctask's session age %d, "
  756. "expected %d\n", ctask->sc->SCp.phase,
  757. session->age);
  758. return ISCSI_ERR_SESSION_FAILED;
  759. }
  760. }
  761. return 0;
  762. }
  763. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  764. struct iscsi_cmd_task *
  765. iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
  766. {
  767. struct iscsi_session *session = conn->session;
  768. struct iscsi_cmd_task *ctask;
  769. uint32_t i;
  770. if (iscsi_verify_itt(conn, itt))
  771. return NULL;
  772. if (itt == RESERVED_ITT)
  773. return NULL;
  774. i = get_itt(itt);
  775. if (i >= session->cmds_max)
  776. return NULL;
  777. ctask = session->cmds[i];
  778. if (!ctask->sc)
  779. return NULL;
  780. if (ctask->sc->SCp.phase != session->age)
  781. return NULL;
  782. return ctask;
  783. }
  784. EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
  785. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  786. {
  787. struct iscsi_session *session = conn->session;
  788. unsigned long flags;
  789. spin_lock_irqsave(&session->lock, flags);
  790. if (session->state == ISCSI_STATE_FAILED) {
  791. spin_unlock_irqrestore(&session->lock, flags);
  792. return;
  793. }
  794. if (conn->stop_stage == 0)
  795. session->state = ISCSI_STATE_FAILED;
  796. spin_unlock_irqrestore(&session->lock, flags);
  797. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  798. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  799. iscsi_conn_error(conn->cls_conn, err);
  800. }
  801. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  802. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  803. {
  804. int rc;
  805. spin_unlock_bh(&conn->session->lock);
  806. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  807. spin_lock_bh(&conn->session->lock);
  808. if (rc)
  809. return rc;
  810. /* done with this in-progress mtask */
  811. conn->mtask = NULL;
  812. return 0;
  813. }
  814. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  815. {
  816. struct iscsi_session *session = conn->session;
  817. /*
  818. * Check for iSCSI window and take care of CmdSN wrap-around
  819. */
  820. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  821. debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
  822. "CmdSN %u/%u\n", session->exp_cmdsn,
  823. session->max_cmdsn, session->cmdsn,
  824. session->queued_cmdsn);
  825. return -ENOSPC;
  826. }
  827. return 0;
  828. }
  829. static int iscsi_xmit_ctask(struct iscsi_conn *conn)
  830. {
  831. struct iscsi_cmd_task *ctask = conn->ctask;
  832. int rc;
  833. __iscsi_get_ctask(ctask);
  834. spin_unlock_bh(&conn->session->lock);
  835. rc = conn->session->tt->xmit_cmd_task(conn, ctask);
  836. spin_lock_bh(&conn->session->lock);
  837. __iscsi_put_ctask(ctask);
  838. if (!rc)
  839. /* done with this ctask */
  840. conn->ctask = NULL;
  841. return rc;
  842. }
  843. /**
  844. * iscsi_requeue_ctask - requeue ctask to run from session workqueue
  845. * @ctask: ctask to requeue
  846. *
  847. * LLDs that need to run a ctask from the session workqueue should call
  848. * this. The session lock must be held. This should only be called
  849. * by software drivers.
  850. */
  851. void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
  852. {
  853. struct iscsi_conn *conn = ctask->conn;
  854. list_move_tail(&ctask->running, &conn->requeue);
  855. scsi_queue_work(conn->session->host, &conn->xmitwork);
  856. }
  857. EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
  858. /**
  859. * iscsi_data_xmit - xmit any command into the scheduled connection
  860. * @conn: iscsi connection
  861. *
  862. * Notes:
  863. * The function can return -EAGAIN in which case the caller must
  864. * re-schedule it again later or recover. '0' return code means
  865. * successful xmit.
  866. **/
  867. static int iscsi_data_xmit(struct iscsi_conn *conn)
  868. {
  869. int rc = 0;
  870. spin_lock_bh(&conn->session->lock);
  871. if (unlikely(conn->suspend_tx)) {
  872. debug_scsi("conn %d Tx suspended!\n", conn->id);
  873. spin_unlock_bh(&conn->session->lock);
  874. return -ENODATA;
  875. }
  876. if (conn->ctask) {
  877. rc = iscsi_xmit_ctask(conn);
  878. if (rc)
  879. goto again;
  880. }
  881. if (conn->mtask) {
  882. rc = iscsi_xmit_mtask(conn);
  883. if (rc)
  884. goto again;
  885. }
  886. /*
  887. * process mgmt pdus like nops before commands since we should
  888. * only have one nop-out as a ping from us and targets should not
  889. * overflow us with nop-ins
  890. */
  891. check_mgmt:
  892. while (!list_empty(&conn->mgmtqueue)) {
  893. conn->mtask = list_entry(conn->mgmtqueue.next,
  894. struct iscsi_mgmt_task, running);
  895. if (iscsi_prep_mtask(conn, conn->mtask)) {
  896. iscsi_free_mgmt_task(conn, conn->mtask);
  897. conn->mtask = NULL;
  898. continue;
  899. }
  900. rc = iscsi_xmit_mtask(conn);
  901. if (rc)
  902. goto again;
  903. }
  904. /* process pending command queue */
  905. while (!list_empty(&conn->xmitqueue)) {
  906. if (conn->tmf_state == TMF_QUEUED)
  907. break;
  908. conn->ctask = list_entry(conn->xmitqueue.next,
  909. struct iscsi_cmd_task, running);
  910. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  911. fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
  912. continue;
  913. }
  914. if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
  915. fail_command(conn, conn->ctask, DID_ABORT << 16);
  916. continue;
  917. }
  918. rc = iscsi_xmit_ctask(conn);
  919. if (rc)
  920. goto again;
  921. /*
  922. * we could continuously get new ctask requests so
  923. * we need to check the mgmt queue for nops that need to
  924. * be sent to aviod starvation
  925. */
  926. if (!list_empty(&conn->mgmtqueue))
  927. goto check_mgmt;
  928. }
  929. while (!list_empty(&conn->requeue)) {
  930. if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
  931. break;
  932. /*
  933. * we always do fastlogout - conn stop code will clean up.
  934. */
  935. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  936. break;
  937. conn->ctask = list_entry(conn->requeue.next,
  938. struct iscsi_cmd_task, running);
  939. conn->ctask->state = ISCSI_TASK_RUNNING;
  940. list_move_tail(conn->requeue.next, &conn->run_list);
  941. rc = iscsi_xmit_ctask(conn);
  942. if (rc)
  943. goto again;
  944. if (!list_empty(&conn->mgmtqueue))
  945. goto check_mgmt;
  946. }
  947. spin_unlock_bh(&conn->session->lock);
  948. return -ENODATA;
  949. again:
  950. if (unlikely(conn->suspend_tx))
  951. rc = -ENODATA;
  952. spin_unlock_bh(&conn->session->lock);
  953. return rc;
  954. }
  955. static void iscsi_xmitworker(struct work_struct *work)
  956. {
  957. struct iscsi_conn *conn =
  958. container_of(work, struct iscsi_conn, xmitwork);
  959. int rc;
  960. /*
  961. * serialize Xmit worker on a per-connection basis.
  962. */
  963. do {
  964. rc = iscsi_data_xmit(conn);
  965. } while (rc >= 0 || rc == -EAGAIN);
  966. }
  967. enum {
  968. FAILURE_BAD_HOST = 1,
  969. FAILURE_SESSION_FAILED,
  970. FAILURE_SESSION_FREED,
  971. FAILURE_WINDOW_CLOSED,
  972. FAILURE_OOM,
  973. FAILURE_SESSION_TERMINATE,
  974. FAILURE_SESSION_IN_RECOVERY,
  975. FAILURE_SESSION_RECOVERY_TIMEOUT,
  976. FAILURE_SESSION_LOGGING_OUT,
  977. FAILURE_SESSION_NOT_READY,
  978. };
  979. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  980. {
  981. struct iscsi_cls_session *cls_session;
  982. struct Scsi_Host *host;
  983. int reason = 0;
  984. struct iscsi_session *session;
  985. struct iscsi_conn *conn;
  986. struct iscsi_cmd_task *ctask = NULL;
  987. sc->scsi_done = done;
  988. sc->result = 0;
  989. sc->SCp.ptr = NULL;
  990. host = sc->device->host;
  991. spin_unlock(host->host_lock);
  992. cls_session = starget_to_session(scsi_target(sc->device));
  993. session = cls_session->dd_data;
  994. spin_lock(&session->lock);
  995. reason = iscsi_session_chkready(cls_session);
  996. if (reason) {
  997. sc->result = reason;
  998. goto fault;
  999. }
  1000. /*
  1001. * ISCSI_STATE_FAILED is a temp. state. The recovery
  1002. * code will decide what is best to do with command queued
  1003. * during this time
  1004. */
  1005. if (session->state != ISCSI_STATE_LOGGED_IN &&
  1006. session->state != ISCSI_STATE_FAILED) {
  1007. /*
  1008. * to handle the race between when we set the recovery state
  1009. * and block the session we requeue here (commands could
  1010. * be entering our queuecommand while a block is starting
  1011. * up because the block code is not locked)
  1012. */
  1013. switch (session->state) {
  1014. case ISCSI_STATE_IN_RECOVERY:
  1015. reason = FAILURE_SESSION_IN_RECOVERY;
  1016. sc->result = DID_IMM_RETRY << 16;
  1017. break;
  1018. case ISCSI_STATE_LOGGING_OUT:
  1019. reason = FAILURE_SESSION_LOGGING_OUT;
  1020. sc->result = DID_IMM_RETRY << 16;
  1021. break;
  1022. case ISCSI_STATE_RECOVERY_FAILED:
  1023. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  1024. sc->result = DID_NO_CONNECT << 16;
  1025. break;
  1026. case ISCSI_STATE_TERMINATE:
  1027. reason = FAILURE_SESSION_TERMINATE;
  1028. sc->result = DID_NO_CONNECT << 16;
  1029. break;
  1030. default:
  1031. reason = FAILURE_SESSION_FREED;
  1032. sc->result = DID_NO_CONNECT << 16;
  1033. }
  1034. goto fault;
  1035. }
  1036. conn = session->leadconn;
  1037. if (!conn) {
  1038. reason = FAILURE_SESSION_FREED;
  1039. sc->result = DID_NO_CONNECT << 16;
  1040. goto fault;
  1041. }
  1042. if (iscsi_check_cmdsn_window_closed(conn)) {
  1043. reason = FAILURE_WINDOW_CLOSED;
  1044. goto reject;
  1045. }
  1046. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  1047. sizeof(void*))) {
  1048. reason = FAILURE_OOM;
  1049. goto reject;
  1050. }
  1051. sc->SCp.phase = session->age;
  1052. sc->SCp.ptr = (char *)ctask;
  1053. atomic_set(&ctask->refcount, 1);
  1054. ctask->state = ISCSI_TASK_PENDING;
  1055. ctask->conn = conn;
  1056. ctask->sc = sc;
  1057. INIT_LIST_HEAD(&ctask->running);
  1058. list_add_tail(&ctask->running, &conn->xmitqueue);
  1059. if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
  1060. if (iscsi_prep_scsi_cmd_pdu(ctask)) {
  1061. sc->result = DID_ABORT << 16;
  1062. sc->scsi_done = NULL;
  1063. iscsi_complete_command(ctask);
  1064. goto fault;
  1065. }
  1066. if (session->tt->xmit_cmd_task(conn, ctask)) {
  1067. sc->scsi_done = NULL;
  1068. iscsi_complete_command(ctask);
  1069. reason = FAILURE_SESSION_NOT_READY;
  1070. goto reject;
  1071. }
  1072. } else
  1073. scsi_queue_work(session->host, &conn->xmitwork);
  1074. session->queued_cmdsn++;
  1075. spin_unlock(&session->lock);
  1076. spin_lock(host->host_lock);
  1077. return 0;
  1078. reject:
  1079. spin_unlock(&session->lock);
  1080. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  1081. spin_lock(host->host_lock);
  1082. return SCSI_MLQUEUE_HOST_BUSY;
  1083. fault:
  1084. spin_unlock(&session->lock);
  1085. debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
  1086. if (!scsi_bidi_cmnd(sc))
  1087. scsi_set_resid(sc, scsi_bufflen(sc));
  1088. else {
  1089. scsi_out(sc)->resid = scsi_out(sc)->length;
  1090. scsi_in(sc)->resid = scsi_in(sc)->length;
  1091. }
  1092. done(sc);
  1093. spin_lock(host->host_lock);
  1094. return 0;
  1095. }
  1096. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1097. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  1098. {
  1099. if (depth > ISCSI_MAX_CMD_PER_LUN)
  1100. depth = ISCSI_MAX_CMD_PER_LUN;
  1101. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  1102. return sdev->queue_depth;
  1103. }
  1104. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  1105. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  1106. {
  1107. struct iscsi_session *session = cls_session->dd_data;
  1108. spin_lock_bh(&session->lock);
  1109. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1110. session->state = ISCSI_STATE_RECOVERY_FAILED;
  1111. if (session->leadconn)
  1112. wake_up(&session->leadconn->ehwait);
  1113. }
  1114. spin_unlock_bh(&session->lock);
  1115. }
  1116. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  1117. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  1118. {
  1119. struct iscsi_cls_session *cls_session;
  1120. struct iscsi_session *session;
  1121. struct iscsi_conn *conn;
  1122. cls_session = starget_to_session(scsi_target(sc->device));
  1123. session = cls_session->dd_data;
  1124. conn = session->leadconn;
  1125. mutex_lock(&session->eh_mutex);
  1126. spin_lock_bh(&session->lock);
  1127. if (session->state == ISCSI_STATE_TERMINATE) {
  1128. failed:
  1129. debug_scsi("failing host reset: session terminated "
  1130. "[CID %d age %d]\n", conn->id, session->age);
  1131. spin_unlock_bh(&session->lock);
  1132. mutex_unlock(&session->eh_mutex);
  1133. return FAILED;
  1134. }
  1135. spin_unlock_bh(&session->lock);
  1136. mutex_unlock(&session->eh_mutex);
  1137. /*
  1138. * we drop the lock here but the leadconn cannot be destoyed while
  1139. * we are in the scsi eh
  1140. */
  1141. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1142. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  1143. wait_event_interruptible(conn->ehwait,
  1144. session->state == ISCSI_STATE_TERMINATE ||
  1145. session->state == ISCSI_STATE_LOGGED_IN ||
  1146. session->state == ISCSI_STATE_RECOVERY_FAILED);
  1147. if (signal_pending(current))
  1148. flush_signals(current);
  1149. mutex_lock(&session->eh_mutex);
  1150. spin_lock_bh(&session->lock);
  1151. if (session->state == ISCSI_STATE_LOGGED_IN)
  1152. iscsi_session_printk(KERN_INFO, session,
  1153. "host reset succeeded\n");
  1154. else
  1155. goto failed;
  1156. spin_unlock_bh(&session->lock);
  1157. mutex_unlock(&session->eh_mutex);
  1158. return SUCCESS;
  1159. }
  1160. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  1161. static void iscsi_tmf_timedout(unsigned long data)
  1162. {
  1163. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1164. struct iscsi_session *session = conn->session;
  1165. spin_lock(&session->lock);
  1166. if (conn->tmf_state == TMF_QUEUED) {
  1167. conn->tmf_state = TMF_TIMEDOUT;
  1168. debug_scsi("tmf timedout\n");
  1169. /* unblock eh_abort() */
  1170. wake_up(&conn->ehwait);
  1171. }
  1172. spin_unlock(&session->lock);
  1173. }
  1174. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1175. struct iscsi_tm *hdr, int age,
  1176. int timeout)
  1177. {
  1178. struct iscsi_session *session = conn->session;
  1179. struct iscsi_mgmt_task *mtask;
  1180. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1181. NULL, 0);
  1182. if (!mtask) {
  1183. spin_unlock_bh(&session->lock);
  1184. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1185. spin_lock_bh(&session->lock);
  1186. debug_scsi("tmf exec failure\n");
  1187. return -EPERM;
  1188. }
  1189. conn->tmfcmd_pdus_cnt++;
  1190. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1191. conn->tmf_timer.function = iscsi_tmf_timedout;
  1192. conn->tmf_timer.data = (unsigned long)conn;
  1193. add_timer(&conn->tmf_timer);
  1194. debug_scsi("tmf set timeout\n");
  1195. spin_unlock_bh(&session->lock);
  1196. mutex_unlock(&session->eh_mutex);
  1197. /*
  1198. * block eh thread until:
  1199. *
  1200. * 1) tmf response
  1201. * 2) tmf timeout
  1202. * 3) session is terminated or restarted or userspace has
  1203. * given up on recovery
  1204. */
  1205. wait_event_interruptible(conn->ehwait, age != session->age ||
  1206. session->state != ISCSI_STATE_LOGGED_IN ||
  1207. conn->tmf_state != TMF_QUEUED);
  1208. if (signal_pending(current))
  1209. flush_signals(current);
  1210. del_timer_sync(&conn->tmf_timer);
  1211. mutex_lock(&session->eh_mutex);
  1212. spin_lock_bh(&session->lock);
  1213. /* if the session drops it will clean up the mtask */
  1214. if (age != session->age ||
  1215. session->state != ISCSI_STATE_LOGGED_IN)
  1216. return -ENOTCONN;
  1217. return 0;
  1218. }
  1219. /*
  1220. * Fail commands. session lock held and recv side suspended and xmit
  1221. * thread flushed
  1222. */
  1223. static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
  1224. int error)
  1225. {
  1226. struct iscsi_cmd_task *ctask, *tmp;
  1227. if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
  1228. conn->ctask = NULL;
  1229. /* flush pending */
  1230. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1231. if (lun == ctask->sc->device->lun || lun == -1) {
  1232. debug_scsi("failing pending sc %p itt 0x%x\n",
  1233. ctask->sc, ctask->itt);
  1234. fail_command(conn, ctask, error << 16);
  1235. }
  1236. }
  1237. list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
  1238. if (lun == ctask->sc->device->lun || lun == -1) {
  1239. debug_scsi("failing requeued sc %p itt 0x%x\n",
  1240. ctask->sc, ctask->itt);
  1241. fail_command(conn, ctask, error << 16);
  1242. }
  1243. }
  1244. /* fail all other running */
  1245. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1246. if (lun == ctask->sc->device->lun || lun == -1) {
  1247. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1248. ctask->sc, ctask->itt);
  1249. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1250. }
  1251. }
  1252. }
  1253. void iscsi_suspend_tx(struct iscsi_conn *conn)
  1254. {
  1255. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1256. if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
  1257. scsi_flush_work(conn->session->host);
  1258. }
  1259. EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
  1260. static void iscsi_start_tx(struct iscsi_conn *conn)
  1261. {
  1262. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1263. if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
  1264. scsi_queue_work(conn->session->host, &conn->xmitwork);
  1265. }
  1266. static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
  1267. {
  1268. struct iscsi_cls_session *cls_session;
  1269. struct iscsi_session *session;
  1270. struct iscsi_conn *conn;
  1271. enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
  1272. cls_session = starget_to_session(scsi_target(scmd->device));
  1273. session = cls_session->dd_data;
  1274. debug_scsi("scsi cmd %p timedout\n", scmd);
  1275. spin_lock(&session->lock);
  1276. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1277. /*
  1278. * We are probably in the middle of iscsi recovery so let
  1279. * that complete and handle the error.
  1280. */
  1281. rc = EH_RESET_TIMER;
  1282. goto done;
  1283. }
  1284. conn = session->leadconn;
  1285. if (!conn) {
  1286. /* In the middle of shuting down */
  1287. rc = EH_RESET_TIMER;
  1288. goto done;
  1289. }
  1290. if (!conn->recv_timeout && !conn->ping_timeout)
  1291. goto done;
  1292. /*
  1293. * if the ping timedout then we are in the middle of cleaning up
  1294. * and can let the iscsi eh handle it
  1295. */
  1296. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1297. (conn->ping_timeout * HZ), jiffies))
  1298. rc = EH_RESET_TIMER;
  1299. /*
  1300. * if we are about to check the transport then give the command
  1301. * more time
  1302. */
  1303. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
  1304. jiffies))
  1305. rc = EH_RESET_TIMER;
  1306. /* if in the middle of checking the transport then give us more time */
  1307. if (conn->ping_mtask)
  1308. rc = EH_RESET_TIMER;
  1309. done:
  1310. spin_unlock(&session->lock);
  1311. debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
  1312. return rc;
  1313. }
  1314. static void iscsi_check_transport_timeouts(unsigned long data)
  1315. {
  1316. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1317. struct iscsi_session *session = conn->session;
  1318. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1319. spin_lock(&session->lock);
  1320. if (session->state != ISCSI_STATE_LOGGED_IN)
  1321. goto done;
  1322. recv_timeout = conn->recv_timeout;
  1323. if (!recv_timeout)
  1324. goto done;
  1325. recv_timeout *= HZ;
  1326. last_recv = conn->last_recv;
  1327. if (conn->ping_mtask &&
  1328. time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
  1329. jiffies)) {
  1330. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1331. "expired, last rx %lu, last ping %lu, "
  1332. "now %lu\n", conn->ping_timeout, last_recv,
  1333. conn->last_ping, jiffies);
  1334. spin_unlock(&session->lock);
  1335. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1336. return;
  1337. }
  1338. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  1339. /* send a ping to try to provoke some traffic */
  1340. debug_scsi("Sending nopout as ping on conn %p\n", conn);
  1341. iscsi_send_nopout(conn, NULL);
  1342. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  1343. } else
  1344. next_timeout = last_recv + recv_timeout;
  1345. debug_scsi("Setting next tmo %lu\n", next_timeout);
  1346. mod_timer(&conn->transport_timer, next_timeout);
  1347. done:
  1348. spin_unlock(&session->lock);
  1349. }
  1350. static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
  1351. struct iscsi_tm *hdr)
  1352. {
  1353. memset(hdr, 0, sizeof(*hdr));
  1354. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1355. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1356. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1357. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1358. hdr->rtt = ctask->hdr->itt;
  1359. hdr->refcmdsn = ctask->hdr->cmdsn;
  1360. }
  1361. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1362. {
  1363. struct iscsi_cls_session *cls_session;
  1364. struct iscsi_session *session;
  1365. struct iscsi_conn *conn;
  1366. struct iscsi_cmd_task *ctask;
  1367. struct iscsi_tm *hdr;
  1368. int rc, age;
  1369. cls_session = starget_to_session(scsi_target(sc->device));
  1370. session = cls_session->dd_data;
  1371. mutex_lock(&session->eh_mutex);
  1372. spin_lock_bh(&session->lock);
  1373. /*
  1374. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1375. * got the command.
  1376. */
  1377. if (!sc->SCp.ptr) {
  1378. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1379. spin_unlock_bh(&session->lock);
  1380. mutex_unlock(&session->eh_mutex);
  1381. return SUCCESS;
  1382. }
  1383. /*
  1384. * If we are not logged in or we have started a new session
  1385. * then let the host reset code handle this
  1386. */
  1387. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1388. sc->SCp.phase != session->age) {
  1389. spin_unlock_bh(&session->lock);
  1390. mutex_unlock(&session->eh_mutex);
  1391. return FAILED;
  1392. }
  1393. conn = session->leadconn;
  1394. conn->eh_abort_cnt++;
  1395. age = session->age;
  1396. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1397. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1398. /* ctask completed before time out */
  1399. if (!ctask->sc) {
  1400. debug_scsi("sc completed while abort in progress\n");
  1401. goto success;
  1402. }
  1403. if (ctask->state == ISCSI_TASK_PENDING) {
  1404. fail_command(conn, ctask, DID_ABORT << 16);
  1405. goto success;
  1406. }
  1407. /* only have one tmf outstanding at a time */
  1408. if (conn->tmf_state != TMF_INITIAL)
  1409. goto failed;
  1410. conn->tmf_state = TMF_QUEUED;
  1411. hdr = &conn->tmhdr;
  1412. iscsi_prep_abort_task_pdu(ctask, hdr);
  1413. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1414. rc = FAILED;
  1415. goto failed;
  1416. }
  1417. switch (conn->tmf_state) {
  1418. case TMF_SUCCESS:
  1419. spin_unlock_bh(&session->lock);
  1420. iscsi_suspend_tx(conn);
  1421. /*
  1422. * clean up task if aborted. grab the recv lock as a writer
  1423. */
  1424. write_lock_bh(conn->recv_lock);
  1425. spin_lock(&session->lock);
  1426. fail_command(conn, ctask, DID_ABORT << 16);
  1427. conn->tmf_state = TMF_INITIAL;
  1428. spin_unlock(&session->lock);
  1429. write_unlock_bh(conn->recv_lock);
  1430. iscsi_start_tx(conn);
  1431. goto success_unlocked;
  1432. case TMF_TIMEDOUT:
  1433. spin_unlock_bh(&session->lock);
  1434. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1435. goto failed_unlocked;
  1436. case TMF_NOT_FOUND:
  1437. if (!sc->SCp.ptr) {
  1438. conn->tmf_state = TMF_INITIAL;
  1439. /* ctask completed before tmf abort response */
  1440. debug_scsi("sc completed while abort in progress\n");
  1441. goto success;
  1442. }
  1443. /* fall through */
  1444. default:
  1445. conn->tmf_state = TMF_INITIAL;
  1446. goto failed;
  1447. }
  1448. success:
  1449. spin_unlock_bh(&session->lock);
  1450. success_unlocked:
  1451. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1452. mutex_unlock(&session->eh_mutex);
  1453. return SUCCESS;
  1454. failed:
  1455. spin_unlock_bh(&session->lock);
  1456. failed_unlocked:
  1457. debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
  1458. ctask ? ctask->itt : 0);
  1459. mutex_unlock(&session->eh_mutex);
  1460. return FAILED;
  1461. }
  1462. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1463. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1464. {
  1465. memset(hdr, 0, sizeof(*hdr));
  1466. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1467. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1468. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1469. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  1470. hdr->rtt = RESERVED_ITT;
  1471. }
  1472. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1473. {
  1474. struct iscsi_cls_session *cls_session;
  1475. struct iscsi_session *session;
  1476. struct iscsi_conn *conn;
  1477. struct iscsi_tm *hdr;
  1478. int rc = FAILED;
  1479. cls_session = starget_to_session(scsi_target(sc->device));
  1480. session = cls_session->dd_data;
  1481. debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
  1482. mutex_lock(&session->eh_mutex);
  1483. spin_lock_bh(&session->lock);
  1484. /*
  1485. * Just check if we are not logged in. We cannot check for
  1486. * the phase because the reset could come from a ioctl.
  1487. */
  1488. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1489. goto unlock;
  1490. conn = session->leadconn;
  1491. /* only have one tmf outstanding at a time */
  1492. if (conn->tmf_state != TMF_INITIAL)
  1493. goto unlock;
  1494. conn->tmf_state = TMF_QUEUED;
  1495. hdr = &conn->tmhdr;
  1496. iscsi_prep_lun_reset_pdu(sc, hdr);
  1497. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  1498. session->lu_reset_timeout)) {
  1499. rc = FAILED;
  1500. goto unlock;
  1501. }
  1502. switch (conn->tmf_state) {
  1503. case TMF_SUCCESS:
  1504. break;
  1505. case TMF_TIMEDOUT:
  1506. spin_unlock_bh(&session->lock);
  1507. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1508. goto done;
  1509. default:
  1510. conn->tmf_state = TMF_INITIAL;
  1511. goto unlock;
  1512. }
  1513. rc = SUCCESS;
  1514. spin_unlock_bh(&session->lock);
  1515. iscsi_suspend_tx(conn);
  1516. /* need to grab the recv lock then session lock */
  1517. write_lock_bh(conn->recv_lock);
  1518. spin_lock(&session->lock);
  1519. fail_all_commands(conn, sc->device->lun, DID_ERROR);
  1520. conn->tmf_state = TMF_INITIAL;
  1521. spin_unlock(&session->lock);
  1522. write_unlock_bh(conn->recv_lock);
  1523. iscsi_start_tx(conn);
  1524. goto done;
  1525. unlock:
  1526. spin_unlock_bh(&session->lock);
  1527. done:
  1528. debug_scsi("iscsi_eh_device_reset %s\n",
  1529. rc == SUCCESS ? "SUCCESS" : "FAILED");
  1530. mutex_unlock(&session->eh_mutex);
  1531. return rc;
  1532. }
  1533. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  1534. /*
  1535. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  1536. * should be accessed via kfifo_{get,put} on q->queue.
  1537. * Optionally, the caller can obtain the array of object pointers
  1538. * by passing in a non-NULL @items pointer
  1539. */
  1540. int
  1541. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  1542. {
  1543. int i, num_arrays = 1;
  1544. memset(q, 0, sizeof(*q));
  1545. q->max = max;
  1546. /* If the user passed an items pointer, he wants a copy of
  1547. * the array. */
  1548. if (items)
  1549. num_arrays++;
  1550. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  1551. if (q->pool == NULL)
  1552. goto enomem;
  1553. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1554. GFP_KERNEL, NULL);
  1555. if (q->queue == ERR_PTR(-ENOMEM))
  1556. goto enomem;
  1557. for (i = 0; i < max; i++) {
  1558. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  1559. if (q->pool[i] == NULL) {
  1560. q->max = i;
  1561. goto enomem;
  1562. }
  1563. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1564. }
  1565. if (items) {
  1566. *items = q->pool + max;
  1567. memcpy(*items, q->pool, max * sizeof(void *));
  1568. }
  1569. return 0;
  1570. enomem:
  1571. iscsi_pool_free(q);
  1572. return -ENOMEM;
  1573. }
  1574. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1575. void iscsi_pool_free(struct iscsi_pool *q)
  1576. {
  1577. int i;
  1578. for (i = 0; i < q->max; i++)
  1579. kfree(q->pool[i]);
  1580. if (q->pool)
  1581. kfree(q->pool);
  1582. }
  1583. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1584. /**
  1585. * iscsi_host_add - add host to system
  1586. * @shost: scsi host
  1587. * @pdev: parent device
  1588. *
  1589. * This should be called by partial offload and software iscsi drivers
  1590. * to add a host to the system.
  1591. */
  1592. int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
  1593. {
  1594. return scsi_add_host(shost, pdev);
  1595. }
  1596. EXPORT_SYMBOL_GPL(iscsi_host_add);
  1597. /**
  1598. * iscsi_host_alloc - allocate a host and driver data
  1599. * @sht: scsi host template
  1600. * @dd_data_size: driver host data size
  1601. * @qdepth: default device queue depth
  1602. *
  1603. * This should be called by partial offload and software iscsi drivers.
  1604. * To access the driver specific memory use the iscsi_host_priv() macro.
  1605. */
  1606. struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  1607. int dd_data_size, uint16_t qdepth)
  1608. {
  1609. struct Scsi_Host *shost;
  1610. shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
  1611. if (!shost)
  1612. return NULL;
  1613. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  1614. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1615. if (qdepth != 0)
  1616. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1617. "Queue depth must be between 1 and %d.\n",
  1618. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1619. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1620. }
  1621. shost->cmd_per_lun = qdepth;
  1622. return shost;
  1623. }
  1624. EXPORT_SYMBOL_GPL(iscsi_host_alloc);
  1625. /**
  1626. * iscsi_host_remove - remove host and sessions
  1627. * @shost: scsi host
  1628. *
  1629. * This will also remove any sessions attached to the host, but if userspace
  1630. * is managing the session at the same time this will break. TODO: add
  1631. * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
  1632. * does not remove the memory from under us.
  1633. */
  1634. void iscsi_host_remove(struct Scsi_Host *shost)
  1635. {
  1636. iscsi_host_for_each_session(shost, iscsi_session_teardown);
  1637. scsi_remove_host(shost);
  1638. }
  1639. EXPORT_SYMBOL_GPL(iscsi_host_remove);
  1640. void iscsi_host_free(struct Scsi_Host *shost)
  1641. {
  1642. struct iscsi_host *ihost = shost_priv(shost);
  1643. kfree(ihost->netdev);
  1644. kfree(ihost->hwaddress);
  1645. kfree(ihost->initiatorname);
  1646. scsi_host_put(shost);
  1647. }
  1648. EXPORT_SYMBOL_GPL(iscsi_host_free);
  1649. /**
  1650. * iscsi_session_setup - create iscsi cls session and host and session
  1651. * @iscsit: iscsi transport template
  1652. * @shost: scsi host
  1653. * @cmds_max: session can queue
  1654. * @cmd_task_size: LLD ctask private data size
  1655. * @mgmt_task_size: LLD mtask private data size
  1656. * @initial_cmdsn: initial CmdSN
  1657. *
  1658. * This can be used by software iscsi_transports that allocate
  1659. * a session per scsi host.
  1660. */
  1661. struct iscsi_cls_session *
  1662. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  1663. uint16_t cmds_max, int cmd_task_size, int mgmt_task_size,
  1664. uint32_t initial_cmdsn)
  1665. {
  1666. struct iscsi_session *session;
  1667. struct iscsi_cls_session *cls_session;
  1668. int cmd_i;
  1669. if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
  1670. cmds_max < 2) {
  1671. if (cmds_max != 0)
  1672. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1673. "can_queue must be a power of 2 and between "
  1674. "2 and %d - setting to %d.\n", cmds_max,
  1675. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1676. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1677. }
  1678. cls_session = iscsi_alloc_session(shost, iscsit,
  1679. sizeof(struct iscsi_session));
  1680. if (!cls_session)
  1681. return NULL;
  1682. session = cls_session->dd_data;
  1683. session->cls_session = cls_session;
  1684. session->host = shost;
  1685. session->state = ISCSI_STATE_FREE;
  1686. session->fast_abort = 1;
  1687. session->lu_reset_timeout = 15;
  1688. session->abort_timeout = 10;
  1689. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1690. session->cmds_max = cmds_max;
  1691. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  1692. session->exp_cmdsn = initial_cmdsn + 1;
  1693. session->max_cmdsn = initial_cmdsn + 1;
  1694. session->max_r2t = 1;
  1695. session->tt = iscsit;
  1696. mutex_init(&session->eh_mutex);
  1697. spin_lock_init(&session->lock);
  1698. /* initialize SCSI PDU commands pool */
  1699. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1700. (void***)&session->cmds,
  1701. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1702. goto cmdpool_alloc_fail;
  1703. /* pre-format cmds pool with ITT */
  1704. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1705. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1706. if (cmd_task_size)
  1707. ctask->dd_data = &ctask[1];
  1708. ctask->itt = cmd_i;
  1709. INIT_LIST_HEAD(&ctask->running);
  1710. }
  1711. /* initialize immediate command pool */
  1712. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1713. (void***)&session->mgmt_cmds,
  1714. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1715. goto mgmtpool_alloc_fail;
  1716. /* pre-format immediate cmds pool with ITT */
  1717. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1718. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1719. if (mgmt_task_size)
  1720. mtask->dd_data = &mtask[1];
  1721. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1722. INIT_LIST_HEAD(&mtask->running);
  1723. }
  1724. if (!try_module_get(iscsit->owner))
  1725. goto module_get_fail;
  1726. if (iscsi_add_session(cls_session, 0))
  1727. goto cls_session_fail;
  1728. return cls_session;
  1729. cls_session_fail:
  1730. module_put(iscsit->owner);
  1731. module_get_fail:
  1732. iscsi_pool_free(&session->mgmtpool);
  1733. mgmtpool_alloc_fail:
  1734. iscsi_pool_free(&session->cmdpool);
  1735. cmdpool_alloc_fail:
  1736. iscsi_free_session(cls_session);
  1737. return NULL;
  1738. }
  1739. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1740. /**
  1741. * iscsi_session_teardown - destroy session, host, and cls_session
  1742. * @cls_session: iscsi session
  1743. *
  1744. * The driver must have called iscsi_remove_session before
  1745. * calling this.
  1746. */
  1747. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1748. {
  1749. struct iscsi_session *session = cls_session->dd_data;
  1750. struct module *owner = cls_session->transport->owner;
  1751. iscsi_pool_free(&session->mgmtpool);
  1752. iscsi_pool_free(&session->cmdpool);
  1753. kfree(session->password);
  1754. kfree(session->password_in);
  1755. kfree(session->username);
  1756. kfree(session->username_in);
  1757. kfree(session->targetname);
  1758. iscsi_destroy_session(cls_session);
  1759. module_put(owner);
  1760. }
  1761. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1762. /**
  1763. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1764. * @cls_session: iscsi_cls_session
  1765. * @dd_size: private driver data size
  1766. * @conn_idx: cid
  1767. */
  1768. struct iscsi_cls_conn *
  1769. iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
  1770. uint32_t conn_idx)
  1771. {
  1772. struct iscsi_session *session = cls_session->dd_data;
  1773. struct iscsi_conn *conn;
  1774. struct iscsi_cls_conn *cls_conn;
  1775. char *data;
  1776. cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
  1777. conn_idx);
  1778. if (!cls_conn)
  1779. return NULL;
  1780. conn = cls_conn->dd_data;
  1781. memset(conn, 0, sizeof(*conn) + dd_size);
  1782. conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  1783. conn->session = session;
  1784. conn->cls_conn = cls_conn;
  1785. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1786. conn->id = conn_idx;
  1787. conn->exp_statsn = 0;
  1788. conn->tmf_state = TMF_INITIAL;
  1789. init_timer(&conn->transport_timer);
  1790. conn->transport_timer.data = (unsigned long)conn;
  1791. conn->transport_timer.function = iscsi_check_transport_timeouts;
  1792. INIT_LIST_HEAD(&conn->run_list);
  1793. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1794. INIT_LIST_HEAD(&conn->mgmtqueue);
  1795. INIT_LIST_HEAD(&conn->xmitqueue);
  1796. INIT_LIST_HEAD(&conn->requeue);
  1797. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1798. /* allocate login_mtask used for the login/text sequences */
  1799. spin_lock_bh(&session->lock);
  1800. if (!__kfifo_get(session->mgmtpool.queue,
  1801. (void*)&conn->login_mtask,
  1802. sizeof(void*))) {
  1803. spin_unlock_bh(&session->lock);
  1804. goto login_mtask_alloc_fail;
  1805. }
  1806. spin_unlock_bh(&session->lock);
  1807. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1808. if (!data)
  1809. goto login_mtask_data_alloc_fail;
  1810. conn->login_mtask->data = conn->data = data;
  1811. init_timer(&conn->tmf_timer);
  1812. init_waitqueue_head(&conn->ehwait);
  1813. return cls_conn;
  1814. login_mtask_data_alloc_fail:
  1815. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1816. sizeof(void*));
  1817. login_mtask_alloc_fail:
  1818. iscsi_destroy_conn(cls_conn);
  1819. return NULL;
  1820. }
  1821. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1822. /**
  1823. * iscsi_conn_teardown - teardown iscsi connection
  1824. * cls_conn: iscsi class connection
  1825. *
  1826. * TODO: we may need to make this into a two step process
  1827. * like scsi-mls remove + put host
  1828. */
  1829. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1830. {
  1831. struct iscsi_conn *conn = cls_conn->dd_data;
  1832. struct iscsi_session *session = conn->session;
  1833. unsigned long flags;
  1834. del_timer_sync(&conn->transport_timer);
  1835. spin_lock_bh(&session->lock);
  1836. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1837. if (session->leadconn == conn) {
  1838. /*
  1839. * leading connection? then give up on recovery.
  1840. */
  1841. session->state = ISCSI_STATE_TERMINATE;
  1842. wake_up(&conn->ehwait);
  1843. }
  1844. spin_unlock_bh(&session->lock);
  1845. /*
  1846. * Block until all in-progress commands for this connection
  1847. * time out or fail.
  1848. */
  1849. for (;;) {
  1850. spin_lock_irqsave(session->host->host_lock, flags);
  1851. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1852. spin_unlock_irqrestore(session->host->host_lock, flags);
  1853. break;
  1854. }
  1855. spin_unlock_irqrestore(session->host->host_lock, flags);
  1856. msleep_interruptible(500);
  1857. iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
  1858. "host_busy %d host_failed %d\n",
  1859. session->host->host_busy,
  1860. session->host->host_failed);
  1861. /*
  1862. * force eh_abort() to unblock
  1863. */
  1864. wake_up(&conn->ehwait);
  1865. }
  1866. /* flush queued up work because we free the connection below */
  1867. iscsi_suspend_tx(conn);
  1868. spin_lock_bh(&session->lock);
  1869. kfree(conn->data);
  1870. kfree(conn->persistent_address);
  1871. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1872. sizeof(void*));
  1873. if (session->leadconn == conn)
  1874. session->leadconn = NULL;
  1875. spin_unlock_bh(&session->lock);
  1876. iscsi_destroy_conn(cls_conn);
  1877. }
  1878. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1879. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1880. {
  1881. struct iscsi_conn *conn = cls_conn->dd_data;
  1882. struct iscsi_session *session = conn->session;
  1883. if (!session) {
  1884. iscsi_conn_printk(KERN_ERR, conn,
  1885. "can't start unbound connection\n");
  1886. return -EPERM;
  1887. }
  1888. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1889. session->first_burst > session->max_burst) {
  1890. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  1891. "first_burst %d max_burst %d\n",
  1892. session->first_burst, session->max_burst);
  1893. return -EINVAL;
  1894. }
  1895. if (conn->ping_timeout && !conn->recv_timeout) {
  1896. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  1897. "zero. Using 5 seconds\n.");
  1898. conn->recv_timeout = 5;
  1899. }
  1900. if (conn->recv_timeout && !conn->ping_timeout) {
  1901. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  1902. "zero. Using 5 seconds.\n");
  1903. conn->ping_timeout = 5;
  1904. }
  1905. spin_lock_bh(&session->lock);
  1906. conn->c_stage = ISCSI_CONN_STARTED;
  1907. session->state = ISCSI_STATE_LOGGED_IN;
  1908. session->queued_cmdsn = session->cmdsn;
  1909. conn->last_recv = jiffies;
  1910. conn->last_ping = jiffies;
  1911. if (conn->recv_timeout && conn->ping_timeout)
  1912. mod_timer(&conn->transport_timer,
  1913. jiffies + (conn->recv_timeout * HZ));
  1914. switch(conn->stop_stage) {
  1915. case STOP_CONN_RECOVER:
  1916. /*
  1917. * unblock eh_abort() if it is blocked. re-try all
  1918. * commands after successful recovery
  1919. */
  1920. conn->stop_stage = 0;
  1921. conn->tmf_state = TMF_INITIAL;
  1922. session->age++;
  1923. if (session->age == 16)
  1924. session->age = 0;
  1925. break;
  1926. case STOP_CONN_TERM:
  1927. conn->stop_stage = 0;
  1928. break;
  1929. default:
  1930. break;
  1931. }
  1932. spin_unlock_bh(&session->lock);
  1933. iscsi_unblock_session(session->cls_session);
  1934. wake_up(&conn->ehwait);
  1935. return 0;
  1936. }
  1937. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1938. static void
  1939. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1940. {
  1941. struct iscsi_mgmt_task *mtask, *tmp;
  1942. /* handle pending */
  1943. list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
  1944. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1945. iscsi_free_mgmt_task(conn, mtask);
  1946. }
  1947. /* handle running */
  1948. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1949. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1950. iscsi_free_mgmt_task(conn, mtask);
  1951. }
  1952. conn->mtask = NULL;
  1953. }
  1954. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1955. struct iscsi_conn *conn, int flag)
  1956. {
  1957. int old_stop_stage;
  1958. del_timer_sync(&conn->transport_timer);
  1959. mutex_lock(&session->eh_mutex);
  1960. spin_lock_bh(&session->lock);
  1961. if (conn->stop_stage == STOP_CONN_TERM) {
  1962. spin_unlock_bh(&session->lock);
  1963. mutex_unlock(&session->eh_mutex);
  1964. return;
  1965. }
  1966. /*
  1967. * The LLD either freed/unset the lock on us, or userspace called
  1968. * stop but did not create a proper connection (connection was never
  1969. * bound or it was unbound then stop was called).
  1970. */
  1971. if (!conn->recv_lock) {
  1972. spin_unlock_bh(&session->lock);
  1973. mutex_unlock(&session->eh_mutex);
  1974. return;
  1975. }
  1976. /*
  1977. * When this is called for the in_login state, we only want to clean
  1978. * up the login task and connection. We do not need to block and set
  1979. * the recovery state again
  1980. */
  1981. if (flag == STOP_CONN_TERM)
  1982. session->state = ISCSI_STATE_TERMINATE;
  1983. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1984. session->state = ISCSI_STATE_IN_RECOVERY;
  1985. old_stop_stage = conn->stop_stage;
  1986. conn->stop_stage = flag;
  1987. conn->c_stage = ISCSI_CONN_STOPPED;
  1988. spin_unlock_bh(&session->lock);
  1989. iscsi_suspend_tx(conn);
  1990. write_lock_bh(conn->recv_lock);
  1991. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1992. write_unlock_bh(conn->recv_lock);
  1993. /*
  1994. * for connection level recovery we should not calculate
  1995. * header digest. conn->hdr_size used for optimization
  1996. * in hdr_extract() and will be re-negotiated at
  1997. * set_param() time.
  1998. */
  1999. if (flag == STOP_CONN_RECOVER) {
  2000. conn->hdrdgst_en = 0;
  2001. conn->datadgst_en = 0;
  2002. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  2003. old_stop_stage != STOP_CONN_RECOVER) {
  2004. debug_scsi("blocking session\n");
  2005. iscsi_block_session(session->cls_session);
  2006. }
  2007. }
  2008. /*
  2009. * flush queues.
  2010. */
  2011. spin_lock_bh(&session->lock);
  2012. fail_all_commands(conn, -1,
  2013. STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
  2014. flush_control_queues(session, conn);
  2015. spin_unlock_bh(&session->lock);
  2016. mutex_unlock(&session->eh_mutex);
  2017. }
  2018. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  2019. {
  2020. struct iscsi_conn *conn = cls_conn->dd_data;
  2021. struct iscsi_session *session = conn->session;
  2022. switch (flag) {
  2023. case STOP_CONN_RECOVER:
  2024. case STOP_CONN_TERM:
  2025. iscsi_start_session_recovery(session, conn, flag);
  2026. break;
  2027. default:
  2028. iscsi_conn_printk(KERN_ERR, conn,
  2029. "invalid stop flag %d\n", flag);
  2030. }
  2031. }
  2032. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  2033. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  2034. struct iscsi_cls_conn *cls_conn, int is_leading)
  2035. {
  2036. struct iscsi_session *session = cls_session->dd_data;
  2037. struct iscsi_conn *conn = cls_conn->dd_data;
  2038. spin_lock_bh(&session->lock);
  2039. if (is_leading)
  2040. session->leadconn = conn;
  2041. spin_unlock_bh(&session->lock);
  2042. /*
  2043. * Unblock xmitworker(), Login Phase will pass through.
  2044. */
  2045. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  2046. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  2047. return 0;
  2048. }
  2049. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  2050. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  2051. enum iscsi_param param, char *buf, int buflen)
  2052. {
  2053. struct iscsi_conn *conn = cls_conn->dd_data;
  2054. struct iscsi_session *session = conn->session;
  2055. uint32_t value;
  2056. switch(param) {
  2057. case ISCSI_PARAM_FAST_ABORT:
  2058. sscanf(buf, "%d", &session->fast_abort);
  2059. break;
  2060. case ISCSI_PARAM_ABORT_TMO:
  2061. sscanf(buf, "%d", &session->abort_timeout);
  2062. break;
  2063. case ISCSI_PARAM_LU_RESET_TMO:
  2064. sscanf(buf, "%d", &session->lu_reset_timeout);
  2065. break;
  2066. case ISCSI_PARAM_PING_TMO:
  2067. sscanf(buf, "%d", &conn->ping_timeout);
  2068. break;
  2069. case ISCSI_PARAM_RECV_TMO:
  2070. sscanf(buf, "%d", &conn->recv_timeout);
  2071. break;
  2072. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2073. sscanf(buf, "%d", &conn->max_recv_dlength);
  2074. break;
  2075. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2076. sscanf(buf, "%d", &conn->max_xmit_dlength);
  2077. break;
  2078. case ISCSI_PARAM_HDRDGST_EN:
  2079. sscanf(buf, "%d", &conn->hdrdgst_en);
  2080. break;
  2081. case ISCSI_PARAM_DATADGST_EN:
  2082. sscanf(buf, "%d", &conn->datadgst_en);
  2083. break;
  2084. case ISCSI_PARAM_INITIAL_R2T_EN:
  2085. sscanf(buf, "%d", &session->initial_r2t_en);
  2086. break;
  2087. case ISCSI_PARAM_MAX_R2T:
  2088. sscanf(buf, "%d", &session->max_r2t);
  2089. break;
  2090. case ISCSI_PARAM_IMM_DATA_EN:
  2091. sscanf(buf, "%d", &session->imm_data_en);
  2092. break;
  2093. case ISCSI_PARAM_FIRST_BURST:
  2094. sscanf(buf, "%d", &session->first_burst);
  2095. break;
  2096. case ISCSI_PARAM_MAX_BURST:
  2097. sscanf(buf, "%d", &session->max_burst);
  2098. break;
  2099. case ISCSI_PARAM_PDU_INORDER_EN:
  2100. sscanf(buf, "%d", &session->pdu_inorder_en);
  2101. break;
  2102. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2103. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2104. break;
  2105. case ISCSI_PARAM_ERL:
  2106. sscanf(buf, "%d", &session->erl);
  2107. break;
  2108. case ISCSI_PARAM_IFMARKER_EN:
  2109. sscanf(buf, "%d", &value);
  2110. BUG_ON(value);
  2111. break;
  2112. case ISCSI_PARAM_OFMARKER_EN:
  2113. sscanf(buf, "%d", &value);
  2114. BUG_ON(value);
  2115. break;
  2116. case ISCSI_PARAM_EXP_STATSN:
  2117. sscanf(buf, "%u", &conn->exp_statsn);
  2118. break;
  2119. case ISCSI_PARAM_USERNAME:
  2120. kfree(session->username);
  2121. session->username = kstrdup(buf, GFP_KERNEL);
  2122. if (!session->username)
  2123. return -ENOMEM;
  2124. break;
  2125. case ISCSI_PARAM_USERNAME_IN:
  2126. kfree(session->username_in);
  2127. session->username_in = kstrdup(buf, GFP_KERNEL);
  2128. if (!session->username_in)
  2129. return -ENOMEM;
  2130. break;
  2131. case ISCSI_PARAM_PASSWORD:
  2132. kfree(session->password);
  2133. session->password = kstrdup(buf, GFP_KERNEL);
  2134. if (!session->password)
  2135. return -ENOMEM;
  2136. break;
  2137. case ISCSI_PARAM_PASSWORD_IN:
  2138. kfree(session->password_in);
  2139. session->password_in = kstrdup(buf, GFP_KERNEL);
  2140. if (!session->password_in)
  2141. return -ENOMEM;
  2142. break;
  2143. case ISCSI_PARAM_TARGET_NAME:
  2144. /* this should not change between logins */
  2145. if (session->targetname)
  2146. break;
  2147. session->targetname = kstrdup(buf, GFP_KERNEL);
  2148. if (!session->targetname)
  2149. return -ENOMEM;
  2150. break;
  2151. case ISCSI_PARAM_TPGT:
  2152. sscanf(buf, "%d", &session->tpgt);
  2153. break;
  2154. case ISCSI_PARAM_PERSISTENT_PORT:
  2155. sscanf(buf, "%d", &conn->persistent_port);
  2156. break;
  2157. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2158. /*
  2159. * this is the address returned in discovery so it should
  2160. * not change between logins.
  2161. */
  2162. if (conn->persistent_address)
  2163. break;
  2164. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  2165. if (!conn->persistent_address)
  2166. return -ENOMEM;
  2167. break;
  2168. default:
  2169. return -ENOSYS;
  2170. }
  2171. return 0;
  2172. }
  2173. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2174. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2175. enum iscsi_param param, char *buf)
  2176. {
  2177. struct iscsi_session *session = cls_session->dd_data;
  2178. int len;
  2179. switch(param) {
  2180. case ISCSI_PARAM_FAST_ABORT:
  2181. len = sprintf(buf, "%d\n", session->fast_abort);
  2182. break;
  2183. case ISCSI_PARAM_ABORT_TMO:
  2184. len = sprintf(buf, "%d\n", session->abort_timeout);
  2185. break;
  2186. case ISCSI_PARAM_LU_RESET_TMO:
  2187. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2188. break;
  2189. case ISCSI_PARAM_INITIAL_R2T_EN:
  2190. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2191. break;
  2192. case ISCSI_PARAM_MAX_R2T:
  2193. len = sprintf(buf, "%hu\n", session->max_r2t);
  2194. break;
  2195. case ISCSI_PARAM_IMM_DATA_EN:
  2196. len = sprintf(buf, "%d\n", session->imm_data_en);
  2197. break;
  2198. case ISCSI_PARAM_FIRST_BURST:
  2199. len = sprintf(buf, "%u\n", session->first_burst);
  2200. break;
  2201. case ISCSI_PARAM_MAX_BURST:
  2202. len = sprintf(buf, "%u\n", session->max_burst);
  2203. break;
  2204. case ISCSI_PARAM_PDU_INORDER_EN:
  2205. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2206. break;
  2207. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2208. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2209. break;
  2210. case ISCSI_PARAM_ERL:
  2211. len = sprintf(buf, "%d\n", session->erl);
  2212. break;
  2213. case ISCSI_PARAM_TARGET_NAME:
  2214. len = sprintf(buf, "%s\n", session->targetname);
  2215. break;
  2216. case ISCSI_PARAM_TPGT:
  2217. len = sprintf(buf, "%d\n", session->tpgt);
  2218. break;
  2219. case ISCSI_PARAM_USERNAME:
  2220. len = sprintf(buf, "%s\n", session->username);
  2221. break;
  2222. case ISCSI_PARAM_USERNAME_IN:
  2223. len = sprintf(buf, "%s\n", session->username_in);
  2224. break;
  2225. case ISCSI_PARAM_PASSWORD:
  2226. len = sprintf(buf, "%s\n", session->password);
  2227. break;
  2228. case ISCSI_PARAM_PASSWORD_IN:
  2229. len = sprintf(buf, "%s\n", session->password_in);
  2230. break;
  2231. default:
  2232. return -ENOSYS;
  2233. }
  2234. return len;
  2235. }
  2236. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  2237. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  2238. enum iscsi_param param, char *buf)
  2239. {
  2240. struct iscsi_conn *conn = cls_conn->dd_data;
  2241. int len;
  2242. switch(param) {
  2243. case ISCSI_PARAM_PING_TMO:
  2244. len = sprintf(buf, "%u\n", conn->ping_timeout);
  2245. break;
  2246. case ISCSI_PARAM_RECV_TMO:
  2247. len = sprintf(buf, "%u\n", conn->recv_timeout);
  2248. break;
  2249. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2250. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  2251. break;
  2252. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2253. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  2254. break;
  2255. case ISCSI_PARAM_HDRDGST_EN:
  2256. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  2257. break;
  2258. case ISCSI_PARAM_DATADGST_EN:
  2259. len = sprintf(buf, "%d\n", conn->datadgst_en);
  2260. break;
  2261. case ISCSI_PARAM_IFMARKER_EN:
  2262. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  2263. break;
  2264. case ISCSI_PARAM_OFMARKER_EN:
  2265. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  2266. break;
  2267. case ISCSI_PARAM_EXP_STATSN:
  2268. len = sprintf(buf, "%u\n", conn->exp_statsn);
  2269. break;
  2270. case ISCSI_PARAM_PERSISTENT_PORT:
  2271. len = sprintf(buf, "%d\n", conn->persistent_port);
  2272. break;
  2273. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2274. len = sprintf(buf, "%s\n", conn->persistent_address);
  2275. break;
  2276. default:
  2277. return -ENOSYS;
  2278. }
  2279. return len;
  2280. }
  2281. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  2282. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2283. char *buf)
  2284. {
  2285. struct iscsi_host *ihost = shost_priv(shost);
  2286. int len;
  2287. switch (param) {
  2288. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2289. if (!ihost->netdev)
  2290. len = sprintf(buf, "%s\n", "default");
  2291. else
  2292. len = sprintf(buf, "%s\n", ihost->netdev);
  2293. break;
  2294. case ISCSI_HOST_PARAM_HWADDRESS:
  2295. if (!ihost->hwaddress)
  2296. len = sprintf(buf, "%s\n", "default");
  2297. else
  2298. len = sprintf(buf, "%s\n", ihost->hwaddress);
  2299. break;
  2300. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2301. if (!ihost->initiatorname)
  2302. len = sprintf(buf, "%s\n", "unknown");
  2303. else
  2304. len = sprintf(buf, "%s\n", ihost->initiatorname);
  2305. break;
  2306. case ISCSI_HOST_PARAM_IPADDRESS:
  2307. if (!strlen(ihost->local_address))
  2308. len = sprintf(buf, "%s\n", "unknown");
  2309. else
  2310. len = sprintf(buf, "%s\n",
  2311. ihost->local_address);
  2312. default:
  2313. return -ENOSYS;
  2314. }
  2315. return len;
  2316. }
  2317. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  2318. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2319. char *buf, int buflen)
  2320. {
  2321. struct iscsi_host *ihost = shost_priv(shost);
  2322. switch (param) {
  2323. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2324. if (!ihost->netdev)
  2325. ihost->netdev = kstrdup(buf, GFP_KERNEL);
  2326. break;
  2327. case ISCSI_HOST_PARAM_HWADDRESS:
  2328. if (!ihost->hwaddress)
  2329. ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
  2330. break;
  2331. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2332. if (!ihost->initiatorname)
  2333. ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
  2334. break;
  2335. default:
  2336. return -ENOSYS;
  2337. }
  2338. return 0;
  2339. }
  2340. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  2341. MODULE_AUTHOR("Mike Christie");
  2342. MODULE_DESCRIPTION("iSCSI library functions");
  2343. MODULE_LICENSE("GPL");