libiscsi.c 70 KB

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