libiscsi.c 73 KB

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