libiscsi.c 76 KB

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