scsi_transport_spi.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. /*
  2. * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
  3. *
  4. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  5. * Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/ctype.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/mutex.h>
  27. #include <linux/sysfs.h>
  28. #include <scsi/scsi.h>
  29. #include "scsi_priv.h"
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_eh.h>
  34. #include <scsi/scsi_transport.h>
  35. #include <scsi/scsi_transport_spi.h>
  36. #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
  37. #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
  38. * on" attributes */
  39. #define SPI_HOST_ATTRS 1
  40. #define SPI_MAX_ECHO_BUFFER_SIZE 4096
  41. #define DV_LOOPS 3
  42. #define DV_TIMEOUT (10*HZ)
  43. #define DV_RETRIES 3 /* should only need at most
  44. * two cc/ua clears */
  45. /* Private data accessors (keep these out of the header file) */
  46. #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
  47. #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
  48. struct spi_internal {
  49. struct scsi_transport_template t;
  50. struct spi_function_template *f;
  51. };
  52. #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
  53. static const int ppr_to_ps[] = {
  54. /* The PPR values 0-6 are reserved, fill them in when
  55. * the committee defines them */
  56. -1, /* 0x00 */
  57. -1, /* 0x01 */
  58. -1, /* 0x02 */
  59. -1, /* 0x03 */
  60. -1, /* 0x04 */
  61. -1, /* 0x05 */
  62. -1, /* 0x06 */
  63. 3125, /* 0x07 */
  64. 6250, /* 0x08 */
  65. 12500, /* 0x09 */
  66. 25000, /* 0x0a */
  67. 30300, /* 0x0b */
  68. 50000, /* 0x0c */
  69. };
  70. /* The PPR values at which you calculate the period in ns by multiplying
  71. * by 4 */
  72. #define SPI_STATIC_PPR 0x0c
  73. static int sprint_frac(char *dest, int value, int denom)
  74. {
  75. int frac = value % denom;
  76. int result = sprintf(dest, "%d", value / denom);
  77. if (frac == 0)
  78. return result;
  79. dest[result++] = '.';
  80. do {
  81. denom /= 10;
  82. sprintf(dest + result, "%d", frac / denom);
  83. result++;
  84. frac %= denom;
  85. } while (frac);
  86. dest[result++] = '\0';
  87. return result;
  88. }
  89. static int spi_execute(struct scsi_device *sdev, const void *cmd,
  90. enum dma_data_direction dir,
  91. void *buffer, unsigned bufflen,
  92. struct scsi_sense_hdr *sshdr)
  93. {
  94. int i, result;
  95. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  96. for(i = 0; i < DV_RETRIES; i++) {
  97. result = scsi_execute(sdev, cmd, dir, buffer, bufflen,
  98. sense, DV_TIMEOUT, /* retries */ 1,
  99. REQ_FAILFAST);
  100. if (result & DRIVER_SENSE) {
  101. struct scsi_sense_hdr sshdr_tmp;
  102. if (!sshdr)
  103. sshdr = &sshdr_tmp;
  104. if (scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE,
  105. sshdr)
  106. && sshdr->sense_key == UNIT_ATTENTION)
  107. continue;
  108. }
  109. break;
  110. }
  111. return result;
  112. }
  113. static struct {
  114. enum spi_signal_type value;
  115. char *name;
  116. } signal_types[] = {
  117. { SPI_SIGNAL_UNKNOWN, "unknown" },
  118. { SPI_SIGNAL_SE, "SE" },
  119. { SPI_SIGNAL_LVD, "LVD" },
  120. { SPI_SIGNAL_HVD, "HVD" },
  121. };
  122. static inline const char *spi_signal_to_string(enum spi_signal_type type)
  123. {
  124. int i;
  125. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  126. if (type == signal_types[i].value)
  127. return signal_types[i].name;
  128. }
  129. return NULL;
  130. }
  131. static inline enum spi_signal_type spi_signal_to_value(const char *name)
  132. {
  133. int i, len;
  134. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  135. len = strlen(signal_types[i].name);
  136. if (strncmp(name, signal_types[i].name, len) == 0 &&
  137. (name[len] == '\n' || name[len] == '\0'))
  138. return signal_types[i].value;
  139. }
  140. return SPI_SIGNAL_UNKNOWN;
  141. }
  142. static int spi_host_setup(struct transport_container *tc, struct device *dev,
  143. struct device *cdev)
  144. {
  145. struct Scsi_Host *shost = dev_to_shost(dev);
  146. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  147. return 0;
  148. }
  149. static int spi_host_configure(struct transport_container *tc,
  150. struct device *dev,
  151. struct device *cdev);
  152. static DECLARE_TRANSPORT_CLASS(spi_host_class,
  153. "spi_host",
  154. spi_host_setup,
  155. NULL,
  156. spi_host_configure);
  157. static int spi_host_match(struct attribute_container *cont,
  158. struct device *dev)
  159. {
  160. struct Scsi_Host *shost;
  161. if (!scsi_is_host_device(dev))
  162. return 0;
  163. shost = dev_to_shost(dev);
  164. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  165. != &spi_host_class.class)
  166. return 0;
  167. return &shost->transportt->host_attrs.ac == cont;
  168. }
  169. static int spi_target_configure(struct transport_container *tc,
  170. struct device *dev,
  171. struct device *cdev);
  172. static int spi_device_configure(struct transport_container *tc,
  173. struct device *dev,
  174. struct device *cdev)
  175. {
  176. struct scsi_device *sdev = to_scsi_device(dev);
  177. struct scsi_target *starget = sdev->sdev_target;
  178. /* Populate the target capability fields with the values
  179. * gleaned from the device inquiry */
  180. spi_support_sync(starget) = scsi_device_sync(sdev);
  181. spi_support_wide(starget) = scsi_device_wide(sdev);
  182. spi_support_dt(starget) = scsi_device_dt(sdev);
  183. spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
  184. spi_support_ius(starget) = scsi_device_ius(sdev);
  185. spi_support_qas(starget) = scsi_device_qas(sdev);
  186. return 0;
  187. }
  188. static int spi_setup_transport_attrs(struct transport_container *tc,
  189. struct device *dev,
  190. struct device *cdev)
  191. {
  192. struct scsi_target *starget = to_scsi_target(dev);
  193. spi_period(starget) = -1; /* illegal value */
  194. spi_min_period(starget) = 0;
  195. spi_offset(starget) = 0; /* async */
  196. spi_max_offset(starget) = 255;
  197. spi_width(starget) = 0; /* narrow */
  198. spi_max_width(starget) = 1;
  199. spi_iu(starget) = 0; /* no IU */
  200. spi_dt(starget) = 0; /* ST */
  201. spi_qas(starget) = 0;
  202. spi_wr_flow(starget) = 0;
  203. spi_rd_strm(starget) = 0;
  204. spi_rti(starget) = 0;
  205. spi_pcomp_en(starget) = 0;
  206. spi_hold_mcs(starget) = 0;
  207. spi_dv_pending(starget) = 0;
  208. spi_dv_in_progress(starget) = 0;
  209. spi_initial_dv(starget) = 0;
  210. mutex_init(&spi_dv_mutex(starget));
  211. return 0;
  212. }
  213. #define spi_transport_show_simple(field, format_string) \
  214. \
  215. static ssize_t \
  216. show_spi_transport_##field(struct device *dev, \
  217. struct device_attribute *attr, char *buf) \
  218. { \
  219. struct scsi_target *starget = transport_class_to_starget(dev); \
  220. struct spi_transport_attrs *tp; \
  221. \
  222. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  223. return snprintf(buf, 20, format_string, tp->field); \
  224. }
  225. #define spi_transport_store_simple(field, format_string) \
  226. \
  227. static ssize_t \
  228. store_spi_transport_##field(struct device *dev, \
  229. struct device_attribute *attr, \
  230. const char *buf, size_t count) \
  231. { \
  232. int val; \
  233. struct scsi_target *starget = transport_class_to_starget(dev); \
  234. struct spi_transport_attrs *tp; \
  235. \
  236. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  237. val = simple_strtoul(buf, NULL, 0); \
  238. tp->field = val; \
  239. return count; \
  240. }
  241. #define spi_transport_show_function(field, format_string) \
  242. \
  243. static ssize_t \
  244. show_spi_transport_##field(struct device *dev, \
  245. struct device_attribute *attr, char *buf) \
  246. { \
  247. struct scsi_target *starget = transport_class_to_starget(dev); \
  248. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  249. struct spi_transport_attrs *tp; \
  250. struct spi_internal *i = to_spi_internal(shost->transportt); \
  251. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  252. if (i->f->get_##field) \
  253. i->f->get_##field(starget); \
  254. return snprintf(buf, 20, format_string, tp->field); \
  255. }
  256. #define spi_transport_store_function(field, format_string) \
  257. static ssize_t \
  258. store_spi_transport_##field(struct device *dev, \
  259. struct device_attribute *attr, \
  260. const char *buf, size_t count) \
  261. { \
  262. int val; \
  263. struct scsi_target *starget = transport_class_to_starget(dev); \
  264. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  265. struct spi_internal *i = to_spi_internal(shost->transportt); \
  266. \
  267. if (!i->f->set_##field) \
  268. return -EINVAL; \
  269. val = simple_strtoul(buf, NULL, 0); \
  270. i->f->set_##field(starget, val); \
  271. return count; \
  272. }
  273. #define spi_transport_store_max(field, format_string) \
  274. static ssize_t \
  275. store_spi_transport_##field(struct device *dev, \
  276. struct device_attribute *attr, \
  277. const char *buf, size_t count) \
  278. { \
  279. int val; \
  280. struct scsi_target *starget = transport_class_to_starget(dev); \
  281. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  282. struct spi_internal *i = to_spi_internal(shost->transportt); \
  283. struct spi_transport_attrs *tp \
  284. = (struct spi_transport_attrs *)&starget->starget_data; \
  285. \
  286. if (i->f->set_##field) \
  287. return -EINVAL; \
  288. val = simple_strtoul(buf, NULL, 0); \
  289. if (val > tp->max_##field) \
  290. val = tp->max_##field; \
  291. i->f->set_##field(starget, val); \
  292. return count; \
  293. }
  294. #define spi_transport_rd_attr(field, format_string) \
  295. spi_transport_show_function(field, format_string) \
  296. spi_transport_store_function(field, format_string) \
  297. static DEVICE_ATTR(field, S_IRUGO, \
  298. show_spi_transport_##field, \
  299. store_spi_transport_##field);
  300. #define spi_transport_simple_attr(field, format_string) \
  301. spi_transport_show_simple(field, format_string) \
  302. spi_transport_store_simple(field, format_string) \
  303. static DEVICE_ATTR(field, S_IRUGO, \
  304. show_spi_transport_##field, \
  305. store_spi_transport_##field);
  306. #define spi_transport_max_attr(field, format_string) \
  307. spi_transport_show_function(field, format_string) \
  308. spi_transport_store_max(field, format_string) \
  309. spi_transport_simple_attr(max_##field, format_string) \
  310. static DEVICE_ATTR(field, S_IRUGO, \
  311. show_spi_transport_##field, \
  312. store_spi_transport_##field);
  313. /* The Parallel SCSI Tranport Attributes: */
  314. spi_transport_max_attr(offset, "%d\n");
  315. spi_transport_max_attr(width, "%d\n");
  316. spi_transport_rd_attr(iu, "%d\n");
  317. spi_transport_rd_attr(dt, "%d\n");
  318. spi_transport_rd_attr(qas, "%d\n");
  319. spi_transport_rd_attr(wr_flow, "%d\n");
  320. spi_transport_rd_attr(rd_strm, "%d\n");
  321. spi_transport_rd_attr(rti, "%d\n");
  322. spi_transport_rd_attr(pcomp_en, "%d\n");
  323. spi_transport_rd_attr(hold_mcs, "%d\n");
  324. /* we only care about the first child device that's a real SCSI device
  325. * so we return 1 to terminate the iteration when we find it */
  326. static int child_iter(struct device *dev, void *data)
  327. {
  328. if (!scsi_is_sdev_device(dev))
  329. return 0;
  330. spi_dv_device(to_scsi_device(dev));
  331. return 1;
  332. }
  333. static ssize_t
  334. store_spi_revalidate(struct device *dev, struct device_attribute *attr,
  335. const char *buf, size_t count)
  336. {
  337. struct scsi_target *starget = transport_class_to_starget(dev);
  338. device_for_each_child(&starget->dev, NULL, child_iter);
  339. return count;
  340. }
  341. static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  342. /* Translate the period into ns according to the current spec
  343. * for SDTR/PPR messages */
  344. static int period_to_str(char *buf, int period)
  345. {
  346. int len, picosec;
  347. if (period < 0 || period > 0xff) {
  348. picosec = -1;
  349. } else if (period <= SPI_STATIC_PPR) {
  350. picosec = ppr_to_ps[period];
  351. } else {
  352. picosec = period * 4000;
  353. }
  354. if (picosec == -1) {
  355. len = sprintf(buf, "reserved");
  356. } else {
  357. len = sprint_frac(buf, picosec, 1000);
  358. }
  359. return len;
  360. }
  361. static ssize_t
  362. show_spi_transport_period_helper(char *buf, int period)
  363. {
  364. int len = period_to_str(buf, period);
  365. buf[len++] = '\n';
  366. buf[len] = '\0';
  367. return len;
  368. }
  369. static ssize_t
  370. store_spi_transport_period_helper(struct device *dev, const char *buf,
  371. size_t count, int *periodp)
  372. {
  373. int j, picosec, period = -1;
  374. char *endp;
  375. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  376. if (*endp == '.') {
  377. int mult = 100;
  378. do {
  379. endp++;
  380. if (!isdigit(*endp))
  381. break;
  382. picosec += (*endp - '0') * mult;
  383. mult /= 10;
  384. } while (mult > 0);
  385. }
  386. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  387. if (ppr_to_ps[j] < picosec)
  388. continue;
  389. period = j;
  390. break;
  391. }
  392. if (period == -1)
  393. period = picosec / 4000;
  394. if (period > 0xff)
  395. period = 0xff;
  396. *periodp = period;
  397. return count;
  398. }
  399. static ssize_t
  400. show_spi_transport_period(struct device *dev,
  401. struct device_attribute *attr, char *buf)
  402. {
  403. struct scsi_target *starget = transport_class_to_starget(dev);
  404. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  405. struct spi_internal *i = to_spi_internal(shost->transportt);
  406. struct spi_transport_attrs *tp =
  407. (struct spi_transport_attrs *)&starget->starget_data;
  408. if (i->f->get_period)
  409. i->f->get_period(starget);
  410. return show_spi_transport_period_helper(buf, tp->period);
  411. }
  412. static ssize_t
  413. store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
  414. const char *buf, size_t count)
  415. {
  416. struct scsi_target *starget = transport_class_to_starget(cdev);
  417. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  418. struct spi_internal *i = to_spi_internal(shost->transportt);
  419. struct spi_transport_attrs *tp =
  420. (struct spi_transport_attrs *)&starget->starget_data;
  421. int period, retval;
  422. if (!i->f->set_period)
  423. return -EINVAL;
  424. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  425. if (period < tp->min_period)
  426. period = tp->min_period;
  427. i->f->set_period(starget, period);
  428. return retval;
  429. }
  430. static DEVICE_ATTR(period, S_IRUGO,
  431. show_spi_transport_period,
  432. store_spi_transport_period);
  433. static ssize_t
  434. show_spi_transport_min_period(struct device *cdev,
  435. struct device_attribute *attr, char *buf)
  436. {
  437. struct scsi_target *starget = transport_class_to_starget(cdev);
  438. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  439. struct spi_internal *i = to_spi_internal(shost->transportt);
  440. struct spi_transport_attrs *tp =
  441. (struct spi_transport_attrs *)&starget->starget_data;
  442. if (!i->f->set_period)
  443. return -EINVAL;
  444. return show_spi_transport_period_helper(buf, tp->min_period);
  445. }
  446. static ssize_t
  447. store_spi_transport_min_period(struct device *cdev,
  448. struct device_attribute *attr,
  449. const char *buf, size_t count)
  450. {
  451. struct scsi_target *starget = transport_class_to_starget(cdev);
  452. struct spi_transport_attrs *tp =
  453. (struct spi_transport_attrs *)&starget->starget_data;
  454. return store_spi_transport_period_helper(cdev, buf, count,
  455. &tp->min_period);
  456. }
  457. static DEVICE_ATTR(min_period, S_IRUGO,
  458. show_spi_transport_min_period,
  459. store_spi_transport_min_period);
  460. static ssize_t show_spi_host_signalling(struct device *cdev,
  461. struct device_attribute *attr,
  462. char *buf)
  463. {
  464. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  465. struct spi_internal *i = to_spi_internal(shost->transportt);
  466. if (i->f->get_signalling)
  467. i->f->get_signalling(shost);
  468. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  469. }
  470. static ssize_t store_spi_host_signalling(struct device *dev,
  471. struct device_attribute *attr,
  472. const char *buf, size_t count)
  473. {
  474. struct Scsi_Host *shost = transport_class_to_shost(dev);
  475. struct spi_internal *i = to_spi_internal(shost->transportt);
  476. enum spi_signal_type type = spi_signal_to_value(buf);
  477. if (!i->f->set_signalling)
  478. return -EINVAL;
  479. if (type != SPI_SIGNAL_UNKNOWN)
  480. i->f->set_signalling(shost, type);
  481. return count;
  482. }
  483. static DEVICE_ATTR(signalling, S_IRUGO,
  484. show_spi_host_signalling,
  485. store_spi_host_signalling);
  486. #define DV_SET(x, y) \
  487. if(i->f->set_##x) \
  488. i->f->set_##x(sdev->sdev_target, y)
  489. enum spi_compare_returns {
  490. SPI_COMPARE_SUCCESS,
  491. SPI_COMPARE_FAILURE,
  492. SPI_COMPARE_SKIP_TEST,
  493. };
  494. /* This is for read/write Domain Validation: If the device supports
  495. * an echo buffer, we do read/write tests to it */
  496. static enum spi_compare_returns
  497. spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
  498. u8 *ptr, const int retries)
  499. {
  500. int len = ptr - buffer;
  501. int j, k, r, result;
  502. unsigned int pattern = 0x0000ffff;
  503. struct scsi_sense_hdr sshdr;
  504. const char spi_write_buffer[] = {
  505. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  506. };
  507. const char spi_read_buffer[] = {
  508. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  509. };
  510. /* set up the pattern buffer. Doesn't matter if we spill
  511. * slightly beyond since that's where the read buffer is */
  512. for (j = 0; j < len; ) {
  513. /* fill the buffer with counting (test a) */
  514. for ( ; j < min(len, 32); j++)
  515. buffer[j] = j;
  516. k = j;
  517. /* fill the buffer with alternating words of 0x0 and
  518. * 0xffff (test b) */
  519. for ( ; j < min(len, k + 32); j += 2) {
  520. u16 *word = (u16 *)&buffer[j];
  521. *word = (j & 0x02) ? 0x0000 : 0xffff;
  522. }
  523. k = j;
  524. /* fill with crosstalk (alternating 0x5555 0xaaa)
  525. * (test c) */
  526. for ( ; j < min(len, k + 32); j += 2) {
  527. u16 *word = (u16 *)&buffer[j];
  528. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  529. }
  530. k = j;
  531. /* fill with shifting bits (test d) */
  532. for ( ; j < min(len, k + 32); j += 4) {
  533. u32 *word = (unsigned int *)&buffer[j];
  534. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  535. *word = pattern;
  536. pattern = (pattern << 1) | roll;
  537. }
  538. /* don't bother with random data (test e) */
  539. }
  540. for (r = 0; r < retries; r++) {
  541. result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
  542. buffer, len, &sshdr);
  543. if(result || !scsi_device_online(sdev)) {
  544. scsi_device_set_state(sdev, SDEV_QUIESCE);
  545. if (scsi_sense_valid(&sshdr)
  546. && sshdr.sense_key == ILLEGAL_REQUEST
  547. /* INVALID FIELD IN CDB */
  548. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  549. /* This would mean that the drive lied
  550. * to us about supporting an echo
  551. * buffer (unfortunately some Western
  552. * Digital drives do precisely this)
  553. */
  554. return SPI_COMPARE_SKIP_TEST;
  555. sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
  556. return SPI_COMPARE_FAILURE;
  557. }
  558. memset(ptr, 0, len);
  559. spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
  560. ptr, len, NULL);
  561. scsi_device_set_state(sdev, SDEV_QUIESCE);
  562. if (memcmp(buffer, ptr, len) != 0)
  563. return SPI_COMPARE_FAILURE;
  564. }
  565. return SPI_COMPARE_SUCCESS;
  566. }
  567. /* This is for the simplest form of Domain Validation: a read test
  568. * on the inquiry data from the device */
  569. static enum spi_compare_returns
  570. spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
  571. u8 *ptr, const int retries)
  572. {
  573. int r, result;
  574. const int len = sdev->inquiry_len;
  575. const char spi_inquiry[] = {
  576. INQUIRY, 0, 0, 0, len, 0
  577. };
  578. for (r = 0; r < retries; r++) {
  579. memset(ptr, 0, len);
  580. result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
  581. ptr, len, NULL);
  582. if(result || !scsi_device_online(sdev)) {
  583. scsi_device_set_state(sdev, SDEV_QUIESCE);
  584. return SPI_COMPARE_FAILURE;
  585. }
  586. /* If we don't have the inquiry data already, the
  587. * first read gets it */
  588. if (ptr == buffer) {
  589. ptr += len;
  590. --r;
  591. continue;
  592. }
  593. if (memcmp(buffer, ptr, len) != 0)
  594. /* failure */
  595. return SPI_COMPARE_FAILURE;
  596. }
  597. return SPI_COMPARE_SUCCESS;
  598. }
  599. static enum spi_compare_returns
  600. spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
  601. enum spi_compare_returns
  602. (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
  603. {
  604. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  605. struct scsi_target *starget = sdev->sdev_target;
  606. int period = 0, prevperiod = 0;
  607. enum spi_compare_returns retval;
  608. for (;;) {
  609. int newperiod;
  610. retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
  611. if (retval == SPI_COMPARE_SUCCESS
  612. || retval == SPI_COMPARE_SKIP_TEST)
  613. break;
  614. /* OK, retrain, fallback */
  615. if (i->f->get_iu)
  616. i->f->get_iu(starget);
  617. if (i->f->get_qas)
  618. i->f->get_qas(starget);
  619. if (i->f->get_period)
  620. i->f->get_period(sdev->sdev_target);
  621. /* Here's the fallback sequence; first try turning off
  622. * IU, then QAS (if we can control them), then finally
  623. * fall down the periods */
  624. if (i->f->set_iu && spi_iu(starget)) {
  625. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Information Units\n");
  626. DV_SET(iu, 0);
  627. } else if (i->f->set_qas && spi_qas(starget)) {
  628. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n");
  629. DV_SET(qas, 0);
  630. } else {
  631. newperiod = spi_period(starget);
  632. period = newperiod > period ? newperiod : period;
  633. if (period < 0x0d)
  634. period++;
  635. else
  636. period += period >> 1;
  637. if (unlikely(period > 0xff || period == prevperiod)) {
  638. /* Total failure; set to async and return */
  639. starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
  640. DV_SET(offset, 0);
  641. return SPI_COMPARE_FAILURE;
  642. }
  643. starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
  644. DV_SET(period, period);
  645. prevperiod = period;
  646. }
  647. }
  648. return retval;
  649. }
  650. static int
  651. spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
  652. {
  653. int l, result;
  654. /* first off do a test unit ready. This can error out
  655. * because of reservations or some other reason. If it
  656. * fails, the device won't let us write to the echo buffer
  657. * so just return failure */
  658. const char spi_test_unit_ready[] = {
  659. TEST_UNIT_READY, 0, 0, 0, 0, 0
  660. };
  661. const char spi_read_buffer_descriptor[] = {
  662. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  663. };
  664. /* We send a set of three TURs to clear any outstanding
  665. * unit attention conditions if they exist (Otherwise the
  666. * buffer tests won't be happy). If the TUR still fails
  667. * (reservation conflict, device not ready, etc) just
  668. * skip the write tests */
  669. for (l = 0; ; l++) {
  670. result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
  671. NULL, 0, NULL);
  672. if(result) {
  673. if(l >= 3)
  674. return 0;
  675. } else {
  676. /* TUR succeeded */
  677. break;
  678. }
  679. }
  680. result = spi_execute(sdev, spi_read_buffer_descriptor,
  681. DMA_FROM_DEVICE, buffer, 4, NULL);
  682. if (result)
  683. /* Device has no echo buffer */
  684. return 0;
  685. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  686. }
  687. static void
  688. spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
  689. {
  690. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  691. struct scsi_target *starget = sdev->sdev_target;
  692. struct Scsi_Host *shost = sdev->host;
  693. int len = sdev->inquiry_len;
  694. int min_period = spi_min_period(starget);
  695. int max_width = spi_max_width(starget);
  696. /* first set us up for narrow async */
  697. DV_SET(offset, 0);
  698. DV_SET(width, 0);
  699. if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
  700. != SPI_COMPARE_SUCCESS) {
  701. starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
  702. /* FIXME: should probably offline the device here? */
  703. return;
  704. }
  705. if (!scsi_device_wide(sdev)) {
  706. spi_max_width(starget) = 0;
  707. max_width = 0;
  708. }
  709. /* test width */
  710. if (i->f->set_width && max_width) {
  711. i->f->set_width(starget, 1);
  712. if (spi_dv_device_compare_inquiry(sdev, buffer,
  713. buffer + len,
  714. DV_LOOPS)
  715. != SPI_COMPARE_SUCCESS) {
  716. starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
  717. i->f->set_width(starget, 0);
  718. /* Make sure we don't force wide back on by asking
  719. * for a transfer period that requires it */
  720. max_width = 0;
  721. if (min_period < 10)
  722. min_period = 10;
  723. }
  724. }
  725. if (!i->f->set_period)
  726. return;
  727. /* device can't handle synchronous */
  728. if (!scsi_device_sync(sdev) && !scsi_device_dt(sdev))
  729. return;
  730. /* len == -1 is the signal that we need to ascertain the
  731. * presence of an echo buffer before trying to use it. len ==
  732. * 0 means we don't have an echo buffer */
  733. len = -1;
  734. retry:
  735. /* now set up to the maximum */
  736. DV_SET(offset, spi_max_offset(starget));
  737. DV_SET(period, min_period);
  738. /* try QAS requests; this should be harmless to set if the
  739. * target supports it */
  740. if (scsi_device_qas(sdev)) {
  741. DV_SET(qas, 1);
  742. } else {
  743. DV_SET(qas, 0);
  744. }
  745. if (scsi_device_ius(sdev) && min_period < 9) {
  746. /* This u320 (or u640). Set IU transfers */
  747. DV_SET(iu, 1);
  748. /* Then set the optional parameters */
  749. DV_SET(rd_strm, 1);
  750. DV_SET(wr_flow, 1);
  751. DV_SET(rti, 1);
  752. if (min_period == 8)
  753. DV_SET(pcomp_en, 1);
  754. } else {
  755. DV_SET(iu, 0);
  756. }
  757. /* now that we've done all this, actually check the bus
  758. * signal type (if known). Some devices are stupid on
  759. * a SE bus and still claim they can try LVD only settings */
  760. if (i->f->get_signalling)
  761. i->f->get_signalling(shost);
  762. if (spi_signalling(shost) == SPI_SIGNAL_SE ||
  763. spi_signalling(shost) == SPI_SIGNAL_HVD ||
  764. !scsi_device_dt(sdev)) {
  765. DV_SET(dt, 0);
  766. } else {
  767. DV_SET(dt, 1);
  768. }
  769. /* set width last because it will pull all the other
  770. * parameters down to required values */
  771. DV_SET(width, max_width);
  772. /* Do the read only INQUIRY tests */
  773. spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
  774. spi_dv_device_compare_inquiry);
  775. /* See if we actually managed to negotiate and sustain DT */
  776. if (i->f->get_dt)
  777. i->f->get_dt(starget);
  778. /* see if the device has an echo buffer. If it does we can do
  779. * the SPI pattern write tests. Because of some broken
  780. * devices, we *only* try this on a device that has actually
  781. * negotiated DT */
  782. if (len == -1 && spi_dt(starget))
  783. len = spi_dv_device_get_echo_buffer(sdev, buffer);
  784. if (len <= 0) {
  785. starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
  786. return;
  787. }
  788. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  789. starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  790. len = SPI_MAX_ECHO_BUFFER_SIZE;
  791. }
  792. if (spi_dv_retrain(sdev, buffer, buffer + len,
  793. spi_dv_device_echo_buffer)
  794. == SPI_COMPARE_SKIP_TEST) {
  795. /* OK, the stupid drive can't do a write echo buffer
  796. * test after all, fall back to the read tests */
  797. len = 0;
  798. goto retry;
  799. }
  800. }
  801. /** spi_dv_device - Do Domain Validation on the device
  802. * @sdev: scsi device to validate
  803. *
  804. * Performs the domain validation on the given device in the
  805. * current execution thread. Since DV operations may sleep,
  806. * the current thread must have user context. Also no SCSI
  807. * related locks that would deadlock I/O issued by the DV may
  808. * be held.
  809. */
  810. void
  811. spi_dv_device(struct scsi_device *sdev)
  812. {
  813. struct scsi_target *starget = sdev->sdev_target;
  814. u8 *buffer;
  815. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  816. if (unlikely(scsi_device_get(sdev)))
  817. return;
  818. if (unlikely(spi_dv_in_progress(starget)))
  819. return;
  820. spi_dv_in_progress(starget) = 1;
  821. buffer = kzalloc(len, GFP_KERNEL);
  822. if (unlikely(!buffer))
  823. goto out_put;
  824. /* We need to verify that the actual device will quiesce; the
  825. * later target quiesce is just a nice to have */
  826. if (unlikely(scsi_device_quiesce(sdev)))
  827. goto out_free;
  828. scsi_target_quiesce(starget);
  829. spi_dv_pending(starget) = 1;
  830. mutex_lock(&spi_dv_mutex(starget));
  831. starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
  832. spi_dv_device_internal(sdev, buffer);
  833. starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
  834. mutex_unlock(&spi_dv_mutex(starget));
  835. spi_dv_pending(starget) = 0;
  836. scsi_target_resume(starget);
  837. spi_initial_dv(starget) = 1;
  838. out_free:
  839. kfree(buffer);
  840. out_put:
  841. spi_dv_in_progress(starget) = 0;
  842. scsi_device_put(sdev);
  843. }
  844. EXPORT_SYMBOL(spi_dv_device);
  845. struct work_queue_wrapper {
  846. struct work_struct work;
  847. struct scsi_device *sdev;
  848. };
  849. static void
  850. spi_dv_device_work_wrapper(struct work_struct *work)
  851. {
  852. struct work_queue_wrapper *wqw =
  853. container_of(work, struct work_queue_wrapper, work);
  854. struct scsi_device *sdev = wqw->sdev;
  855. kfree(wqw);
  856. spi_dv_device(sdev);
  857. spi_dv_pending(sdev->sdev_target) = 0;
  858. scsi_device_put(sdev);
  859. }
  860. /**
  861. * spi_schedule_dv_device - schedule domain validation to occur on the device
  862. * @sdev: The device to validate
  863. *
  864. * Identical to spi_dv_device() above, except that the DV will be
  865. * scheduled to occur in a workqueue later. All memory allocations
  866. * are atomic, so may be called from any context including those holding
  867. * SCSI locks.
  868. */
  869. void
  870. spi_schedule_dv_device(struct scsi_device *sdev)
  871. {
  872. struct work_queue_wrapper *wqw =
  873. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  874. if (unlikely(!wqw))
  875. return;
  876. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  877. kfree(wqw);
  878. return;
  879. }
  880. /* Set pending early (dv_device doesn't check it, only sets it) */
  881. spi_dv_pending(sdev->sdev_target) = 1;
  882. if (unlikely(scsi_device_get(sdev))) {
  883. kfree(wqw);
  884. spi_dv_pending(sdev->sdev_target) = 0;
  885. return;
  886. }
  887. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
  888. wqw->sdev = sdev;
  889. schedule_work(&wqw->work);
  890. }
  891. EXPORT_SYMBOL(spi_schedule_dv_device);
  892. /**
  893. * spi_display_xfer_agreement - Print the current target transfer agreement
  894. * @starget: The target for which to display the agreement
  895. *
  896. * Each SPI port is required to maintain a transfer agreement for each
  897. * other port on the bus. This function prints a one-line summary of
  898. * the current agreement; more detailed information is available in sysfs.
  899. */
  900. void spi_display_xfer_agreement(struct scsi_target *starget)
  901. {
  902. struct spi_transport_attrs *tp;
  903. tp = (struct spi_transport_attrs *)&starget->starget_data;
  904. if (tp->offset > 0 && tp->period > 0) {
  905. unsigned int picosec, kb100;
  906. char *scsi = "FAST-?";
  907. char tmp[8];
  908. if (tp->period <= SPI_STATIC_PPR) {
  909. picosec = ppr_to_ps[tp->period];
  910. switch (tp->period) {
  911. case 7: scsi = "FAST-320"; break;
  912. case 8: scsi = "FAST-160"; break;
  913. case 9: scsi = "FAST-80"; break;
  914. case 10:
  915. case 11: scsi = "FAST-40"; break;
  916. case 12: scsi = "FAST-20"; break;
  917. }
  918. } else {
  919. picosec = tp->period * 4000;
  920. if (tp->period < 25)
  921. scsi = "FAST-20";
  922. else if (tp->period < 50)
  923. scsi = "FAST-10";
  924. else
  925. scsi = "FAST-5";
  926. }
  927. kb100 = (10000000 + picosec / 2) / picosec;
  928. if (tp->width)
  929. kb100 *= 2;
  930. sprint_frac(tmp, picosec, 1000);
  931. dev_info(&starget->dev,
  932. "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
  933. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  934. tp->dt ? "DT" : "ST",
  935. tp->iu ? " IU" : "",
  936. tp->qas ? " QAS" : "",
  937. tp->rd_strm ? " RDSTRM" : "",
  938. tp->rti ? " RTI" : "",
  939. tp->wr_flow ? " WRFLOW" : "",
  940. tp->pcomp_en ? " PCOMP" : "",
  941. tp->hold_mcs ? " HMCS" : "",
  942. tmp, tp->offset);
  943. } else {
  944. dev_info(&starget->dev, "%sasynchronous\n",
  945. tp->width ? "wide " : "");
  946. }
  947. }
  948. EXPORT_SYMBOL(spi_display_xfer_agreement);
  949. int spi_populate_width_msg(unsigned char *msg, int width)
  950. {
  951. msg[0] = EXTENDED_MESSAGE;
  952. msg[1] = 2;
  953. msg[2] = EXTENDED_WDTR;
  954. msg[3] = width;
  955. return 4;
  956. }
  957. EXPORT_SYMBOL_GPL(spi_populate_width_msg);
  958. int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
  959. {
  960. msg[0] = EXTENDED_MESSAGE;
  961. msg[1] = 3;
  962. msg[2] = EXTENDED_SDTR;
  963. msg[3] = period;
  964. msg[4] = offset;
  965. return 5;
  966. }
  967. EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
  968. int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
  969. int width, int options)
  970. {
  971. msg[0] = EXTENDED_MESSAGE;
  972. msg[1] = 6;
  973. msg[2] = EXTENDED_PPR;
  974. msg[3] = period;
  975. msg[4] = 0;
  976. msg[5] = offset;
  977. msg[6] = width;
  978. msg[7] = options;
  979. return 8;
  980. }
  981. EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
  982. #ifdef CONFIG_SCSI_CONSTANTS
  983. static const char * const one_byte_msgs[] = {
  984. /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
  985. /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
  986. /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
  987. /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
  988. /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
  989. /* 0x0f */ "Initiate Recovery", "Release Recovery",
  990. /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
  991. /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
  992. };
  993. static const char * const two_byte_msgs[] = {
  994. /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
  995. /* 0x23 */ "Ignore Wide Residue", "ACA"
  996. };
  997. static const char * const extended_msgs[] = {
  998. /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
  999. /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
  1000. /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
  1001. };
  1002. static void print_nego(const unsigned char *msg, int per, int off, int width)
  1003. {
  1004. if (per) {
  1005. char buf[20];
  1006. period_to_str(buf, msg[per]);
  1007. printk("period = %s ns ", buf);
  1008. }
  1009. if (off)
  1010. printk("offset = %d ", msg[off]);
  1011. if (width)
  1012. printk("width = %d ", 8 << msg[width]);
  1013. }
  1014. static void print_ptr(const unsigned char *msg, int msb, const char *desc)
  1015. {
  1016. int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
  1017. msg[msb+3];
  1018. printk("%s = %d ", desc, ptr);
  1019. }
  1020. int spi_print_msg(const unsigned char *msg)
  1021. {
  1022. int len = 1, i;
  1023. if (msg[0] == EXTENDED_MESSAGE) {
  1024. len = 2 + msg[1];
  1025. if (len == 2)
  1026. len += 256;
  1027. if (msg[2] < ARRAY_SIZE(extended_msgs))
  1028. printk ("%s ", extended_msgs[msg[2]]);
  1029. else
  1030. printk ("Extended Message, reserved code (0x%02x) ",
  1031. (int) msg[2]);
  1032. switch (msg[2]) {
  1033. case EXTENDED_MODIFY_DATA_POINTER:
  1034. print_ptr(msg, 3, "pointer");
  1035. break;
  1036. case EXTENDED_SDTR:
  1037. print_nego(msg, 3, 4, 0);
  1038. break;
  1039. case EXTENDED_WDTR:
  1040. print_nego(msg, 0, 0, 3);
  1041. break;
  1042. case EXTENDED_PPR:
  1043. print_nego(msg, 3, 5, 6);
  1044. break;
  1045. case EXTENDED_MODIFY_BIDI_DATA_PTR:
  1046. print_ptr(msg, 3, "out");
  1047. print_ptr(msg, 7, "in");
  1048. break;
  1049. default:
  1050. for (i = 2; i < len; ++i)
  1051. printk("%02x ", msg[i]);
  1052. }
  1053. /* Identify */
  1054. } else if (msg[0] & 0x80) {
  1055. printk("Identify disconnect %sallowed %s %d ",
  1056. (msg[0] & 0x40) ? "" : "not ",
  1057. (msg[0] & 0x20) ? "target routine" : "lun",
  1058. msg[0] & 0x7);
  1059. /* Normal One byte */
  1060. } else if (msg[0] < 0x1f) {
  1061. if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
  1062. printk("%s ", one_byte_msgs[msg[0]]);
  1063. else
  1064. printk("reserved (%02x) ", msg[0]);
  1065. } else if (msg[0] == 0x55) {
  1066. printk("QAS Request ");
  1067. /* Two byte */
  1068. } else if (msg[0] <= 0x2f) {
  1069. if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
  1070. printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
  1071. msg[1]);
  1072. else
  1073. printk("reserved two byte (%02x %02x) ",
  1074. msg[0], msg[1]);
  1075. len = 2;
  1076. } else
  1077. printk("reserved ");
  1078. return len;
  1079. }
  1080. EXPORT_SYMBOL(spi_print_msg);
  1081. #else /* ifndef CONFIG_SCSI_CONSTANTS */
  1082. int spi_print_msg(const unsigned char *msg)
  1083. {
  1084. int len = 1, i;
  1085. if (msg[0] == EXTENDED_MESSAGE) {
  1086. len = 2 + msg[1];
  1087. if (len == 2)
  1088. len += 256;
  1089. for (i = 0; i < len; ++i)
  1090. printk("%02x ", msg[i]);
  1091. /* Identify */
  1092. } else if (msg[0] & 0x80) {
  1093. printk("%02x ", msg[0]);
  1094. /* Normal One byte */
  1095. } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
  1096. printk("%02x ", msg[0]);
  1097. /* Two byte */
  1098. } else if (msg[0] <= 0x2f) {
  1099. printk("%02x %02x", msg[0], msg[1]);
  1100. len = 2;
  1101. } else
  1102. printk("%02x ", msg[0]);
  1103. return len;
  1104. }
  1105. EXPORT_SYMBOL(spi_print_msg);
  1106. #endif /* ! CONFIG_SCSI_CONSTANTS */
  1107. static int spi_device_match(struct attribute_container *cont,
  1108. struct device *dev)
  1109. {
  1110. struct scsi_device *sdev;
  1111. struct Scsi_Host *shost;
  1112. struct spi_internal *i;
  1113. if (!scsi_is_sdev_device(dev))
  1114. return 0;
  1115. sdev = to_scsi_device(dev);
  1116. shost = sdev->host;
  1117. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1118. != &spi_host_class.class)
  1119. return 0;
  1120. /* Note: this class has no device attributes, so it has
  1121. * no per-HBA allocation and thus we don't need to distinguish
  1122. * the attribute containers for the device */
  1123. i = to_spi_internal(shost->transportt);
  1124. if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
  1125. return 0;
  1126. return 1;
  1127. }
  1128. static int spi_target_match(struct attribute_container *cont,
  1129. struct device *dev)
  1130. {
  1131. struct Scsi_Host *shost;
  1132. struct scsi_target *starget;
  1133. struct spi_internal *i;
  1134. if (!scsi_is_target_device(dev))
  1135. return 0;
  1136. shost = dev_to_shost(dev->parent);
  1137. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1138. != &spi_host_class.class)
  1139. return 0;
  1140. i = to_spi_internal(shost->transportt);
  1141. starget = to_scsi_target(dev);
  1142. if (i->f->deny_binding && i->f->deny_binding(starget))
  1143. return 0;
  1144. return &i->t.target_attrs.ac == cont;
  1145. }
  1146. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  1147. "spi_transport",
  1148. spi_setup_transport_attrs,
  1149. NULL,
  1150. spi_target_configure);
  1151. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  1152. spi_device_match,
  1153. spi_device_configure);
  1154. static struct attribute *host_attributes[] = {
  1155. &dev_attr_signalling.attr,
  1156. NULL
  1157. };
  1158. static struct attribute_group host_attribute_group = {
  1159. .attrs = host_attributes,
  1160. };
  1161. static int spi_host_configure(struct transport_container *tc,
  1162. struct device *dev,
  1163. struct device *cdev)
  1164. {
  1165. struct kobject *kobj = &cdev->kobj;
  1166. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1167. struct spi_internal *si = to_spi_internal(shost->transportt);
  1168. struct attribute *attr = &dev_attr_signalling.attr;
  1169. int rc = 0;
  1170. if (si->f->set_signalling)
  1171. rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
  1172. return rc;
  1173. }
  1174. /* returns true if we should be showing the variable. Also
  1175. * overloads the return by setting 1<<1 if the attribute should
  1176. * be writeable */
  1177. #define TARGET_ATTRIBUTE_HELPER(name) \
  1178. (si->f->show_##name ? S_IRUGO : 0) | \
  1179. (si->f->set_##name ? S_IWUSR : 0)
  1180. static mode_t target_attribute_is_visible(struct kobject *kobj,
  1181. struct attribute *attr, int i)
  1182. {
  1183. struct device *cdev = container_of(kobj, struct device, kobj);
  1184. struct scsi_target *starget = transport_class_to_starget(cdev);
  1185. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1186. struct spi_internal *si = to_spi_internal(shost->transportt);
  1187. if (attr == &dev_attr_period.attr &&
  1188. spi_support_sync(starget))
  1189. return TARGET_ATTRIBUTE_HELPER(period);
  1190. else if (attr == &dev_attr_min_period.attr &&
  1191. spi_support_sync(starget))
  1192. return TARGET_ATTRIBUTE_HELPER(period);
  1193. else if (attr == &dev_attr_offset.attr &&
  1194. spi_support_sync(starget))
  1195. return TARGET_ATTRIBUTE_HELPER(offset);
  1196. else if (attr == &dev_attr_max_offset.attr &&
  1197. spi_support_sync(starget))
  1198. return TARGET_ATTRIBUTE_HELPER(offset);
  1199. else if (attr == &dev_attr_width.attr &&
  1200. spi_support_wide(starget))
  1201. return TARGET_ATTRIBUTE_HELPER(width);
  1202. else if (attr == &dev_attr_max_width.attr &&
  1203. spi_support_wide(starget))
  1204. return TARGET_ATTRIBUTE_HELPER(width);
  1205. else if (attr == &dev_attr_iu.attr &&
  1206. spi_support_ius(starget))
  1207. return TARGET_ATTRIBUTE_HELPER(iu);
  1208. else if (attr == &dev_attr_dt.attr &&
  1209. spi_support_dt(starget))
  1210. return TARGET_ATTRIBUTE_HELPER(dt);
  1211. else if (attr == &dev_attr_qas.attr &&
  1212. spi_support_qas(starget))
  1213. return TARGET_ATTRIBUTE_HELPER(qas);
  1214. else if (attr == &dev_attr_wr_flow.attr &&
  1215. spi_support_ius(starget))
  1216. return TARGET_ATTRIBUTE_HELPER(wr_flow);
  1217. else if (attr == &dev_attr_rd_strm.attr &&
  1218. spi_support_ius(starget))
  1219. return TARGET_ATTRIBUTE_HELPER(rd_strm);
  1220. else if (attr == &dev_attr_rti.attr &&
  1221. spi_support_ius(starget))
  1222. return TARGET_ATTRIBUTE_HELPER(rti);
  1223. else if (attr == &dev_attr_pcomp_en.attr &&
  1224. spi_support_ius(starget))
  1225. return TARGET_ATTRIBUTE_HELPER(pcomp_en);
  1226. else if (attr == &dev_attr_hold_mcs.attr &&
  1227. spi_support_ius(starget))
  1228. return TARGET_ATTRIBUTE_HELPER(hold_mcs);
  1229. else if (attr == &dev_attr_revalidate.attr)
  1230. return S_IWUSR;
  1231. return 0;
  1232. }
  1233. static struct attribute *target_attributes[] = {
  1234. &dev_attr_period.attr,
  1235. &dev_attr_min_period.attr,
  1236. &dev_attr_offset.attr,
  1237. &dev_attr_max_offset.attr,
  1238. &dev_attr_width.attr,
  1239. &dev_attr_max_width.attr,
  1240. &dev_attr_iu.attr,
  1241. &dev_attr_dt.attr,
  1242. &dev_attr_qas.attr,
  1243. &dev_attr_wr_flow.attr,
  1244. &dev_attr_rd_strm.attr,
  1245. &dev_attr_rti.attr,
  1246. &dev_attr_pcomp_en.attr,
  1247. &dev_attr_hold_mcs.attr,
  1248. &dev_attr_revalidate.attr,
  1249. NULL
  1250. };
  1251. static struct attribute_group target_attribute_group = {
  1252. .attrs = target_attributes,
  1253. .is_visible = target_attribute_is_visible,
  1254. };
  1255. static int spi_target_configure(struct transport_container *tc,
  1256. struct device *dev,
  1257. struct device *cdev)
  1258. {
  1259. struct kobject *kobj = &cdev->kobj;
  1260. /* force an update based on parameters read from the device */
  1261. sysfs_update_group(kobj, &target_attribute_group);
  1262. return 0;
  1263. }
  1264. struct scsi_transport_template *
  1265. spi_attach_transport(struct spi_function_template *ft)
  1266. {
  1267. struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
  1268. GFP_KERNEL);
  1269. if (unlikely(!i))
  1270. return NULL;
  1271. i->t.target_attrs.ac.class = &spi_transport_class.class;
  1272. i->t.target_attrs.ac.grp = &target_attribute_group;
  1273. i->t.target_attrs.ac.match = spi_target_match;
  1274. transport_container_register(&i->t.target_attrs);
  1275. i->t.target_size = sizeof(struct spi_transport_attrs);
  1276. i->t.host_attrs.ac.class = &spi_host_class.class;
  1277. i->t.host_attrs.ac.grp = &host_attribute_group;
  1278. i->t.host_attrs.ac.match = spi_host_match;
  1279. transport_container_register(&i->t.host_attrs);
  1280. i->t.host_size = sizeof(struct spi_host_attrs);
  1281. i->f = ft;
  1282. return &i->t;
  1283. }
  1284. EXPORT_SYMBOL(spi_attach_transport);
  1285. void spi_release_transport(struct scsi_transport_template *t)
  1286. {
  1287. struct spi_internal *i = to_spi_internal(t);
  1288. transport_container_unregister(&i->t.target_attrs);
  1289. transport_container_unregister(&i->t.host_attrs);
  1290. kfree(i);
  1291. }
  1292. EXPORT_SYMBOL(spi_release_transport);
  1293. static __init int spi_transport_init(void)
  1294. {
  1295. int error = transport_class_register(&spi_transport_class);
  1296. if (error)
  1297. return error;
  1298. error = anon_transport_class_register(&spi_device_class);
  1299. return transport_class_register(&spi_host_class);
  1300. }
  1301. static void __exit spi_transport_exit(void)
  1302. {
  1303. transport_class_unregister(&spi_transport_class);
  1304. anon_transport_class_unregister(&spi_device_class);
  1305. transport_class_unregister(&spi_host_class);
  1306. }
  1307. MODULE_AUTHOR("Martin Hicks");
  1308. MODULE_DESCRIPTION("SPI Transport Attributes");
  1309. MODULE_LICENSE("GPL");
  1310. module_init(spi_transport_init);
  1311. module_exit(spi_transport_exit);