libiscsi.c 77 KB

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