scsi_transport_spi.c 37 KB

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