libiscsi.c 75 KB

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