libiscsi.c 70 KB

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